Files
ziran/bootstrap/compiler_enum/compiler_enum.c
T
waozi 436d73cf3a Regenerate the bootstrap without the TinyCC guard
The checked-arithmetic helpers every generated module carries no longer
test __TINYC__.
2026-11-15 21:56:20 -03:00

509 lines
19 KiB
C

/* Generated by zi2c from compiler_enum.zi. */
#include <stdbool.h>
#include "zir_bounds.h"
#include "zir_string.h"
#define ZIRAN_COMPILER_ENUM_H_PRIVATE 1
#include "compiler_enum.h"
#undef ZIRAN_COMPILER_ENUM_H_PRIVATE
#ifndef ZIRAN_HELPER_ZiDivS64
#define ZIRAN_HELPER_ZiDivS64
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__OPTIMIZE__)
#define ZiDivS64(a, b, min) __extension__ ({ int64_t __zi_a = (a), __zi_b = (b); if(!__zi_b) abort(); __zi_b == -1 ? (__zi_a == (min) ? __zi_a : -__zi_a) : __zi_a / __zi_b; })
#else
static inline int64_t ZiDivS64(int64_t a, int64_t b, int64_t min) {
if(!b) abort();
if(b == -1) return a == min ? a : -a;
return a / b;
}
#endif
#endif
#ifndef ZIRAN_HELPER_ZiRemS64
#define ZIRAN_HELPER_ZiRemS64
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__OPTIMIZE__)
#define ZiRemS64(a, b) __extension__ ({ int64_t __zi_a = (a), __zi_b = (b); if(!__zi_b) abort(); __zi_b == -1 ? 0 : __zi_a % __zi_b; })
#else
static inline int64_t ZiRemS64(int64_t a, int64_t b) {
if(!b) abort();
return b == -1 ? 0 : a % b;
}
#endif
#endif
#ifndef ZIRAN_HELPER_ZiShl64
#define ZIRAN_HELPER_ZiShl64
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__OPTIMIZE__)
#define ZiShl64(a, s, w) __extension__ ({ uint64_t __zi_a = (a); uint64_t __zi_s = (s); if(__zi_s >= (uint64_t)(w)) abort(); (uint64_t)(__zi_a << __zi_s); })
#else
static inline uint64_t ZiShl64(uint64_t a, uint64_t s, int w) {
if(s >= (uint64_t)w) abort();
return (uint64_t)(a << s);
}
#endif
#endif
#ifndef ZIRAN_HELPER_ZiShrS64
#define ZIRAN_HELPER_ZiShrS64
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__OPTIMIZE__)
#define ZiShrS64(a, s, w) __extension__ ({ int64_t __zi_a = (a); uint64_t __zi_s = (s); if(__zi_s >= (uint64_t)(w)) abort(); __zi_a < 0 ? ~(~__zi_a >> __zi_s) : __zi_a >> __zi_s; })
#else
static inline int64_t ZiShrS64(int64_t a, uint64_t s, int w) {
if(s >= (uint64_t)w) abort();
return a < 0 ? ~(~a >> s) : a >> s;
}
#endif
#endif
#ifndef ZIRAN_MAYBE_UNUSED
#if defined(__GNUC__) || defined(__clang__)
#define ZIRAN_MAYBE_UNUSED __attribute__((unused))
#else
#define ZIRAN_MAYBE_UNUSED
#endif
#endif
static void compiler_enum_Space(Cursor* cursor) ZIRAN_MAYBE_UNUSED;
static bool compiler_enum_IdentifierStart(uint8_t byte) ZIRAN_MAYBE_UNUSED;
static int64_t compiler_enum_Number(Cursor* cursor, bool negative) ZIRAN_MAYBE_UNUSED;
static int64_t compiler_enum_Term(Cursor* cursor, Slice entries) ZIRAN_MAYBE_UNUSED;
static int64_t compiler_enum_Product(Cursor* cursor, Slice entries) ZIRAN_MAYBE_UNUSED;
static int64_t compiler_enum_Sum(Cursor* cursor, Slice entries) ZIRAN_MAYBE_UNUSED;
static int64_t compiler_enum_Expression(Cursor* cursor, Slice entries) ZIRAN_MAYBE_UNUSED;
static void
compiler_enum_Space(Cursor* cursor)
{
int64_t* value_0 = &(cursor->at);
int64_t value_1 = compiler_text_SkipSpace(cursor->source, cursor->at, false);
(*value_0) = value_1;
}
static bool
compiler_enum_IdentifierStart(uint8_t byte)
{
return byte == 95 || (byte >= 97 && byte <= 122) || (byte >= 65 && byte <= 90);
}
static int64_t
compiler_enum_Number(Cursor* cursor, bool negative)
{
int64_t begin = cursor->at;
while (true) {
int64_t value_0 = cursor->at;
int64_t value_1 = (int64_t)(cursor->source).length;
bool value_2 = value_0 < value_1;
if (!(value_2 && compiler_text_IdentifierByte((uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at)))) { break; }
int64_t* value_3 = &(cursor->at);
(*value_3) = (int64_t)((uint64_t)((*value_3)) + UINT64_C(1));
}
String value_4 = cursor->source;
LiteralBits literal = compiler_text_IntegerLiteral(StringRange(value_4, (int64_t)begin, (int64_t)cursor->at));
uint64_t limit = 9223372036854775807ULL;
if (negative) {
limit = (uint64_t)((uint64_t)limit + UINT64_C(1));
}
if (!literal.valid || literal.value > limit) {
bool* value_5 = &(cursor->valid);
(*value_5) = false;
return 0LL;
}
if (negative) {
return (int64_t)(((uint64_t)(UINT64_C(0) - (uint64_t)(literal.value))));
}
return (int64_t)(literal.value);
}
static int64_t
compiler_enum_Term(Cursor* cursor, Slice entries)
{
Cursor* value_0 = cursor;
{
Cursor* cursor = value_0;
int64_t* value_2 = &(cursor->at);
int64_t value_3 = compiler_text_SkipSpace(cursor->source, cursor->at, false);
(*value_2) = value_3;
}
bool negative = false;
int64_t value_4 = cursor->at;
int64_t value_5 = (int64_t)(cursor->source).length;
if (value_4 < value_5 && ((uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at) == 43 || (uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at) == 45)) {
negative = (uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at) == 45;
int64_t* value_6 = &(cursor->at);
(*value_6) = (int64_t)((uint64_t)((*value_6)) + UINT64_C(1));
}
Cursor* value_7 = cursor;
{
Cursor* cursor = value_7;
int64_t* value_9 = &(cursor->at);
int64_t value_10 = compiler_text_SkipSpace(cursor->source, cursor->at, false);
(*value_9) = value_10;
}
int64_t value_11 = cursor->at;
int64_t value_12 = (int64_t)(cursor->source).length;
if (value_11 == value_12) {
bool* value_13 = &(cursor->valid);
(*value_13) = false;
return 0LL;
}
uint8_t first = (uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at);
if (first >= 48 && first <= 57) {
return compiler_enum_Number(cursor, negative);
}
bool value_14 = false;
{
uint8_t byte = first;
value_14 = byte == 95 || (byte >= 97 && byte <= 122) || (byte >= 65 && byte <= 90);
}
if (!value_14) {
bool* value_16 = &(cursor->valid);
(*value_16) = false;
return 0LL;
}
int64_t begin = cursor->at;
while (true) {
int64_t value_17 = cursor->at;
int64_t value_18 = (int64_t)(cursor->source).length;
bool value_19 = value_17 < value_18;
if (!(value_19 && compiler_text_IdentifierByte((uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at)))) { break; }
int64_t* value_20 = &(cursor->at);
(*value_20) = (int64_t)((uint64_t)((*value_20)) + UINT64_C(1));
}
String value_21 = cursor->source;
String name = StringRange(value_21, (int64_t)begin, (int64_t)cursor->at);
for (int64_t it_index = 0LL; it_index < (int64_t)(entries).length; it_index++) {
Entry entry = ((Entry *)entries.data)[it_index];
if (StringEqual(entry.name, name)) {
if (!negative) {
return entry.value;
}
if (entry.value == (-INT64_C(9223372036854775807)-1)) {
bool* value_22 = &(cursor->valid);
(*value_22) = false;
return 0LL;
}
return (int64_t)(UINT64_C(0) - (uint64_t)(entry.value));
}
}
bool* value_23 = &(cursor->valid);
(*value_23) = false;
return 0LL;
}
static int64_t
compiler_enum_Product(Cursor* cursor, Slice entries)
{
int64_t value = compiler_enum_Term(cursor, entries);
while (cursor->valid) {
Cursor* value_0 = cursor;
{
Cursor* cursor = value_0;
int64_t* value_2 = &(cursor->at);
int64_t value_3 = compiler_text_SkipSpace(cursor->source, cursor->at, false);
(*value_2) = value_3;
}
int64_t value_4 = cursor->at;
int64_t value_5 = (int64_t)(cursor->source).length;
if (value_4 == value_5) {
return value;
}
uint8_t op = (uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at);
if (op != 42 && op != 47 && op != 37) {
return value;
}
int64_t* value_6 = &(cursor->at);
(*value_6) = (int64_t)((uint64_t)((*value_6)) + UINT64_C(1));
int64_t right = compiler_enum_Term(cursor, entries);
if (!cursor->valid) {
return 0LL;
}
if (op == 42) {
bool value_7 = value > 0LL && right > 0LL;
if (value_7) {
int64_t value_8 = ZiDivS64(9223372036854775807LL, right, INT64_MIN);
value_7 = (value > value_8);
}
bool value_9 = value_7;
if (!value_9) {
bool value_10 = value > 0LL && right < 0LL;
if (value_10) {
int64_t value_11 = ZiDivS64((-INT64_C(9223372036854775807)-1), value, INT64_MIN);
value_10 = (right < value_11);
}
value_9 = value_10;
}
bool value_12 = value_9;
if (!value_12) {
bool value_13 = value < 0LL && right > 0LL;
if (value_13) {
int64_t value_14 = ZiDivS64((-INT64_C(9223372036854775807)-1), right, INT64_MIN);
value_13 = (value < value_14);
}
value_12 = value_13;
}
bool value_15 = value_12;
if (!value_15) {
bool value_16 = value < 0LL && right < 0LL;
if (value_16) {
int64_t value_17 = ZiDivS64(9223372036854775807LL, value, INT64_MIN);
value_16 = (right < value_17);
}
value_15 = value_16;
}
if (value_15) {
bool* value_18 = &(cursor->valid);
(*value_18) = false;
return 0LL;
}
value = (int64_t)((uint64_t)value * (uint64_t)right);
} else {
if (right == 0LL || (value == (-INT64_C(9223372036854775807)-1) && right == -1LL)) {
bool* value_19 = &(cursor->valid);
(*value_19) = false;
return 0LL;
}
if (op == 47) {
value = ZiDivS64(value, right, INT64_MIN);
} else {
value = ZiRemS64(value, right);
}
}
}
return 0LL;
}
static int64_t
compiler_enum_Sum(Cursor* cursor, Slice entries)
{
int64_t value = compiler_enum_Product(cursor, entries);
while (cursor->valid) {
Cursor* value_0 = cursor;
{
Cursor* cursor = value_0;
int64_t* value_2 = &(cursor->at);
int64_t value_3 = compiler_text_SkipSpace(cursor->source, cursor->at, false);
(*value_2) = value_3;
}
int64_t value_4 = cursor->at;
int64_t value_5 = (int64_t)(cursor->source).length;
if (value_4 == value_5) {
return value;
}
uint8_t op = (uint8_t)ZIRAN_INDEX(cursor->source.data, cursor->source.length, cursor->at);
if (op != 43 && op != 45) {
return value;
}
int64_t* value_6 = &(cursor->at);
(*value_6) = (int64_t)((uint64_t)((*value_6)) + UINT64_C(1));
int64_t right = compiler_enum_Product(cursor, entries);
if (!cursor->valid) {
return 0LL;
}
bool value_7 = op == 43 && ((right > 0LL && value > (int64_t)(UINT64_C(9223372036854775807) - (uint64_t)right)) || (right < 0LL && value < (int64_t)((uint64_t)((-INT64_C(9223372036854775807)-1)) - (uint64_t)right)));
if (value_7 || (op == 45 && ((right < 0LL && value > (int64_t)(UINT64_C(9223372036854775807) + (uint64_t)right)) || (right > 0LL && value < (int64_t)((uint64_t)((-INT64_C(9223372036854775807)-1)) + (uint64_t)right))))) {
bool* value_8 = &(cursor->valid);
(*value_8) = false;
return 0LL;
}
if (op == 43) {
value = (int64_t)((uint64_t)value + (uint64_t)right);
} else {
value = (int64_t)((uint64_t)value - (uint64_t)right);
}
}
return 0LL;
}
static int64_t
compiler_enum_Expression(Cursor* cursor, Slice entries)
{
int64_t value = compiler_enum_Sum(cursor, entries);
while (cursor->valid) {
Cursor* value_0 = cursor;
{
Cursor* cursor = value_0;
int64_t* value_2 = &(cursor->at);
int64_t value_3 = compiler_text_SkipSpace(cursor->source, cursor->at, false);
(*value_2) = value_3;
}
int64_t value_4 = (int64_t)((uint64_t)(cursor->at) + UINT64_C(1));
int64_t value_5 = (int64_t)(cursor->source).length;
bool value_6 = value_4 >= value_5;
if (!value_6) {
String value_7 = cursor->source;
bool value_8 = !StringEqual(StringRange(value_7, (int64_t)cursor->at, (int64_t)((int64_t)((uint64_t)(cursor->at) + UINT64_C(2)))), StringLiteral("<<"));
value_6 = value_8;
}
if (value_6) {
break;
}
int64_t* value_9 = &(cursor->at);
(*value_9) = (int64_t)((uint64_t)((*value_9)) + UINT64_C(2));
int64_t amount = compiler_enum_Sum(cursor, entries);
bool value_10 = !cursor->valid || amount < 0LL || amount >= 63LL || value < 0LL;
if (!value_10) {
int64_t value_11 = ZiShrS64(9223372036854775807LL, (uint64_t)(amount), 64);
value_10 = (value > value_11);
}
if (value_10) {
bool* value_12 = &(cursor->valid);
(*value_12) = false;
return 0LL;
}
value = (int64_t)ZiShl64(value, (uint64_t)(amount), 64);
}
Cursor* value_13 = cursor;
{
Cursor* cursor = value_13;
int64_t* value_15 = &(cursor->at);
int64_t value_16 = compiler_text_SkipSpace(cursor->source, cursor->at, false);
(*value_15) = value_16;
}
int64_t value_17 = cursor->at;
int64_t value_18 = (int64_t)(cursor->source).length;
if (value_17 != value_18) {
bool* value_19 = &(cursor->valid);
(*value_19) = false;
}
return value;
}
bool
compiler_enum_FitsBacking(String backing, int64_t value)
{
if (StringEqual(backing, StringLiteral("s64"))) {
return true;
}
if (StringEqual(backing, StringLiteral("u64"))) {
return value >= 0LL;
}
if (StringEqual(backing, StringLiteral("s8"))) {
return value >= -128LL && value <= 127LL;
}
if (StringEqual(backing, StringLiteral("s16"))) {
return value >= -32768LL && value <= 32767LL;
}
if (StringEqual(backing, StringLiteral("s32"))) {
return value >= -2147483648LL && value <= 2147483647LL;
}
if (StringEqual(backing, StringLiteral("u8"))) {
return value >= 0LL && value <= 255LL;
}
if (StringEqual(backing, StringLiteral("u16"))) {
return value >= 0LL && value <= 65535LL;
}
if (StringEqual(backing, StringLiteral("u32"))) {
return value >= 0LL && value <= 4294967295LL;
}
return false;
}
Evaluation
compiler_enum_MemberValue(String body, String backing, String wanted, Slice entries, int64_t name_limit)
{
Evaluation result = {0};
int64_t at = 0LL;
int64_t next = 0LL;
bool next_valid = true;
while (at < (int64_t)(body).length) {
while (true) {
bool value_0 = at < (int64_t)(body).length;
bool value_1 = value_0;
if (value_1) {
bool value_2 = (uint8_t)ZIRAN_INDEX(body.data, body.length, at) == 44;
bool value_3 = value_2 || compiler_text_SpaceByte((uint8_t)ZIRAN_INDEX(body.data, body.length, at));
value_1 = value_3;
}
if (!value_1) { break; }
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
if (at == (int64_t)(body).length) {
break;
}
int64_t end = at;
while (end < (int64_t)(body).length && (uint8_t)ZIRAN_INDEX(body.data, body.length, end) != 44 && (uint8_t)ZIRAN_INDEX(body.data, body.length, end) != 10) {
end = (int64_t)((uint64_t)end + UINT64_C(1));
}
int64_t begin = at;
uint8_t value_4 = (uint8_t)ZIRAN_INDEX(body.data, body.length, at);
bool value_5 = false;
{
uint8_t byte = value_4;
value_5 = byte == 95 || (byte >= 97 && byte <= 122) || (byte >= 65 && byte <= 90);
}
if (!value_5) {
return (Evaluation){0};
}
while (true) {
bool value_7 = at < end;
if (!(value_7 && compiler_text_IdentifierByte((uint8_t)ZIRAN_INDEX(body.data, body.length, at)))) { break; }
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
String name = StringRange(body, (int64_t)begin, (int64_t)at);
if ((int64_t)(name).length >= name_limit || result.count >= (int64_t)(entries).length) {
return (Evaluation){0};
}
{
Slice value_8 = entries;
Slice value_9 = SliceRange(value_8, (int64_t)0LL, (int64_t)result.count, sizeof(Entry));
Slice loop_view_26 = value_9;
int64_t loop_count_26 = (int64_t)(loop_view_26).length;
int64_t loop_cursor_26 = 0LL;
while (loop_cursor_26 < loop_count_26) {
int64_t loop_index_26 = loop_cursor_26;
Entry entry = ((Entry *)loop_view_26.data)[loop_index_26];
if (StringEqual(entry.name, name)) {
return (Evaluation){0};
}
loop_cursor_26 = (int64_t)((uint64_t)loop_cursor_26 + UINT64_C(1));
}
}
Cursor cursor = {.source = StringRange(body, (int64_t)at, (int64_t)end), .at = 0LL, .valid = true};
Cursor* value_10 = &(cursor);
{
Cursor* cursor = value_10;
int64_t* value_12 = &(cursor->at);
int64_t value_13 = compiler_text_SkipSpace(cursor->source, cursor->at, false);
(*value_12) = value_13;
}
int64_t value = next;
if (cursor.at < (int64_t)(cursor.source).length) {
if ((uint8_t)ZIRAN_INDEX(cursor.source.data, cursor.source.length, cursor.at) != 61) {
return (Evaluation){0};
}
int64_t* value_14 = &(cursor.at);
(*value_14) = (int64_t)((uint64_t)((*value_14)) + UINT64_C(1));
Slice value_15 = entries;
value = compiler_enum_Expression(&(cursor), SliceRange(value_15, (int64_t)0LL, (int64_t)result.count, sizeof(Entry)));
if (!cursor.valid) {
return (Evaluation){0};
}
} else if (!next_valid) {
return (Evaluation){0};
}
bool value_16 = compiler_enum_FitsBacking(backing, value);
if (!value_16) {
return (Evaluation){0};
}
Entry* value_17 = &(((Entry *)entries.data)[SliceIndex(entries, (int64_t)result.count)]);
(*value_17) = (Entry){.name = name, .value = value};
if (StringEqual(name, wanted)) {
bool* value_18 = &(result.found);
(*value_18) = true;
int64_t* value_19 = &(result.value);
(*value_19) = value;
}
int64_t* value_20 = &(result.count);
(*value_20) = (int64_t)((uint64_t)((*value_20)) + UINT64_C(1));
next_valid = value != 9223372036854775807LL;
next = value;
if (next_valid) {
next = (int64_t)((uint64_t)next + UINT64_C(1));
}
at = end;
if (at < (int64_t)(body).length) {
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
}
bool* value_21 = &(result.valid);
(*value_21) = result.count > 0LL;
return result;
}