Files
ziran/bootstrap/compiler_source/compiler_source.c
T
waozi e834d1ccfe Prove two-dimensional indexes from guards
Generated C leaves out the bounds check of an index such as
row * stride + column when a guard bounds it: an earlier if that returns,
breaks or continues, an enclosing if or while condition, or the earlier
operands of && and ||, until a value they compare changes. Indexes and
guard comparisons become polynomials over parameters, invariant locals
and slice counts; each loop counter is replaced by its upper bound, and
the index is proven below the count or length when every step is exact.
Guards that miss the read, values moved after the guard and else-if
exits keep their checks.

On the AV1 benchmark (fixed binaries, interleaved, one core) 1080p q20
drops from 185.1 to 182.2 ms and 720p 4:4:4 is unchanged, but 1080p
10-bit q30 rises from 201.5 to 212 ms. All of that comes from the one
check left out of FindCdefDirection's sample read; the function alone runs
faster without it (102 against 105 ns per call), so the loss is a layout
effect in the decoder's context.
2026-11-06 12:41:04 -03:00

1164 lines
47 KiB
C

/* Generated by zi2c from compiler_source.zi. */
#define ZIRAN_COMPILER_SOURCE_H_SOURCE 1
#include "zir_bounds.h"
#include "zir_string.h"
#include "compiler_source.h"
static uint8_t compiler_source_Byte(String source, int64_t at);
static bool compiler_source_Space(uint8_t byte);
static bool compiler_source_Letter(uint8_t byte);
static int64_t compiler_source_SkipSpace(String source, int64_t at);
static bool compiler_source_Matches(String source, int64_t at, String text);
static int64_t compiler_source_Find(String source, String text);
static int64_t compiler_source_AfterQuote(String source, int64_t at);
static int64_t compiler_source_ClosedQuote(String source, int64_t at);
static void compiler_source_Put(Slice output, int64_t* used, uint8_t byte);
static void compiler_source_PutScanned(Slice output, int64_t* used, uint8_t byte, int64_t* gaps);
static void compiler_source_PutRaw(Slice output, int64_t* used, uint8_t byte);
static uint8_t
compiler_source_Byte(String source, int64_t at)
{
if (at < 0LL || at >= (int64_t)(source).length) {
return 0;
}
return (uint8_t)source.data[at];
}
static bool
compiler_source_Space(uint8_t byte)
{
return byte == 32 || (byte >= 9 && byte <= 13);
}
static bool
compiler_source_Letter(uint8_t byte)
{
return (byte >= 65 && byte <= 90) || (byte >= 97 && byte <= 122) || byte == 95;
}
static int64_t
compiler_source_SkipSpace(String source, int64_t at)
{
while (at < (int64_t)(source).length && compiler_source_Space((uint8_t)ZIRAN_INDEX(source.data, source.length, at))) {
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
return at;
}
static bool
compiler_source_Matches(String source, int64_t at, String text)
{
bool value_0 = at >= 0LL && at <= (int64_t)(source).length && (int64_t)(text).length <= (int64_t)((uint64_t)((int64_t)(source).length) - (uint64_t)at);
return value_0 && StringEqual(StringRange(source, (int64_t)at, (int64_t)((int64_t)((uint64_t)at + (uint64_t)((int64_t)(text).length)))), text);
}
static int64_t
compiler_source_Find(String source, String text)
{
int64_t at = 0LL;
while (at <= (int64_t)(source).length) {
if (compiler_source_Matches(source, at, text)) {
return at;
}
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
return -1LL;
}
static int64_t
compiler_source_AfterQuote(String source, int64_t at)
{
uint8_t quote = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
at = (int64_t)((uint64_t)at + UINT64_C(1));
while (at < (int64_t)(source).length) {
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
at = (int64_t)((uint64_t)at + UINT64_C(1));
if (byte == 92 && at < (int64_t)(source).length) {
at = (int64_t)((uint64_t)at + UINT64_C(1));
} else if (byte == quote) {
break;
}
}
return at;
}
static int64_t
compiler_source_ClosedQuote(String source, int64_t at)
{
uint8_t quote = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
at = (int64_t)((uint64_t)at + UINT64_C(1));
while (at < (int64_t)(source).length) {
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
at = (int64_t)((uint64_t)at + UINT64_C(1));
if (byte == 92 && at < (int64_t)(source).length) {
at = (int64_t)((uint64_t)at + UINT64_C(1));
} else if (byte == quote) {
return at;
}
}
return -1LL;
}
static void
compiler_source_Put(Slice output, int64_t* used, uint8_t byte)
{
int64_t value_0 = *(used);
int64_t value_1 = (int64_t)(output).length;
if (value_0 < value_1) {
((uint8_t *)output.data)[SliceIndex(output, (int64_t)*(used))] = byte;
}
(*(used)) = (int64_t)((uint64_t)((*(used))) + UINT64_C(1));
}
static void
compiler_source_PutScanned(Slice output, int64_t* used, uint8_t byte, int64_t* gaps)
{
compiler_source_Put(output, used, byte);
if (byte == 10) {
while (true) {
int64_t value_0 = *(gaps);
if (!(value_0 > 0LL)) { break; }
compiler_source_Put(output, used, 10);
(*(gaps)) = (int64_t)((uint64_t)((*(gaps))) - UINT64_C(1));
}
}
}
static void
compiler_source_PutRaw(Slice output, int64_t* used, uint8_t byte)
{
uint8_t escaped = 0;
if (byte == 34 || byte == 92) {
escaped = byte;
} else if (byte == 10) {
escaped = 110;
} else if (byte == 13) {
escaped = 114;
} else if (byte == 9) {
escaped = 116;
}
if (escaped != 0) {
compiler_source_Put(output, used, 92);
compiler_source_Put(output, used, escaped);
} else if (byte < 32 || byte == 127) {
String digits = StringLiteral("0123456789abcdef");
compiler_source_Put(output, used, 92);
compiler_source_Put(output, used, 120);
compiler_source_Put(output, used, (uint8_t)ZIRAN_INDEX(digits.data, digits.length, ((uint8_t)(byte >> 4))));
compiler_source_Put(output, used, (uint8_t)ZIRAN_INDEX(digits.data, digits.length, ((uint8_t)((uint32_t)byte & 15u))));
} else {
compiler_source_Put(output, used, byte);
}
}
bool
compiler_source_SourceIdentifier(uint8_t byte)
{
bool value_0 = compiler_source_Letter(byte);
return value_0 || (byte >= 48 && byte <= 57);
}
bool
compiler_source_StartsWord(String source, String word)
{
bool value_0 = compiler_source_Matches(source, 0LL, word);
if (!value_0) {
return false;
}
uint8_t byte = compiler_source_Byte(source, (int64_t)(word).length);
return byte == 0 || byte == 32 || byte == 9 || byte == 40 || byte == 34 || byte == 123;
}
bool
compiler_source_IdentifierText(String source, bool member_path)
{
bool value_0 = (int64_t)(source).length == 0LL;
if (!value_0) {
bool value_1 = compiler_source_Letter((uint8_t)ZIRAN_INDEX(source.data, source.length, 0));
value_0 = !value_1;
}
if (value_0) {
return false;
}
int64_t at = 1LL;
while (at < (int64_t)(source).length) {
if (compiler_source_SourceIdentifier((uint8_t)ZIRAN_INDEX(source.data, source.length, at))) {
at = (int64_t)((uint64_t)at + UINT64_C(1));
} else {
bool value_2 = member_path && (uint8_t)ZIRAN_INDEX(source.data, source.length, at) == 46;
if (value_2 && compiler_source_Letter(compiler_source_Byte(source, (int64_t)((uint64_t)at + UINT64_C(1))))) {
at = (int64_t)((uint64_t)at + UINT64_C(2));
} else {
return false;
}
}
}
return true;
}
bool
compiler_source_NonJaiControl(String source, String word)
{
bool value_0 = compiler_source_StartsWord(source, word);
if (!value_0) {
return false;
}
int64_t at = compiler_source_SkipSpace(source, (int64_t)(word).length);
uint8_t byte = compiler_source_Byte(source, at);
if (byte == 40) {
int64_t value_1 = compiler_source_Find(StringRange(source, (int64_t)at, (int64_t)source.length), StringLiteral("{"));
return value_1 >= 0LL;
}
String operators = StringLiteral(":=.[+-*/%&|^<>!");
for (int64_t it_index = 0LL; it_index < (int64_t)(operators).length; it_index++) {
uint8_t operator_byte = (uint8_t)operators.data[it_index];
if (byte == operator_byte) {
return false;
}
}
return byte == 0 || byte == 59 || byte == 123 || compiler_source_SourceIdentifier(byte);
}
bool
compiler_source_LooksLikeLabel(String source)
{
int64_t at = 0LL;
while (compiler_source_SourceIdentifier(compiler_source_Byte(source, at))) {
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
if (at == 0LL || compiler_source_Byte(source, at) != 58) {
return false;
}
at = compiler_source_SkipSpace(source, (int64_t)((uint64_t)at + UINT64_C(1)));
return at == (int64_t)(source).length || StringEqual(StringRange(source, (int64_t)at, (int64_t)source.length), StringLiteral(";"));
}
StatementKind
compiler_source_ClassifyStatement(String source)
{
if (compiler_source_Byte(source, 0LL) == 125) {
return StatementKind_BlockClose;
}
if (StringEqual(source, StringLiteral("{"))) {
return StatementKind_BlockOpen;
}
bool value_0 = compiler_source_Byte(source, 0LL) == 35;
if (value_0 && compiler_source_StartsWord(source, StringLiteral("#asm"))) {
return StatementKind_BlockOpen;
}
bool value_1 = compiler_source_StartsWord(source, StringLiteral("if"));
if (value_1 || compiler_source_StartsWord(source, StringLiteral("else"))) {
return StatementKind_If;
}
if (compiler_source_StartsWord(source, StringLiteral("while"))) {
return StatementKind_While;
}
if (compiler_source_StartsWord(source, StringLiteral("for"))) {
return StatementKind_For;
}
bool value_2 = compiler_source_StartsWord(source, StringLiteral("case"));
if (value_2 || StringEqual(source, StringLiteral("case;"))) {
return StatementKind_Case;
}
bool value_3 = compiler_source_StartsWord(source, StringLiteral("return"));
if (value_3 || StringEqual(source, StringLiteral("return;"))) {
return StatementKind_Return;
}
if (StringEqual(source, StringLiteral("unreachable")) || StringEqual(source, StringLiteral("unreachable;"))) {
return StatementKind_Unreachable;
}
bool value_4 = compiler_source_StartsWord(source, StringLiteral("break"));
if (value_4 || StringEqual(source, StringLiteral("break;"))) {
return StatementKind_Break;
}
bool value_5 = compiler_source_StartsWord(source, StringLiteral("continue"));
if (value_5 || StringEqual(source, StringLiteral("continue;"))) {
return StatementKind_Continue;
}
if (compiler_source_StartsWord(source, StringLiteral("defer"))) {
return StatementKind_Defer;
}
if (compiler_source_StartsWord(source, StringLiteral("unused"))) {
return StatementKind_Unused;
}
int64_t colon = compiler_source_Find(source, StringLiteral(": "));
if (colon < 0LL) {
colon = compiler_source_Find(source, StringLiteral(": ["));
}
if (colon > 0LL && compiler_source_IdentifierText(StringRange(source, (int64_t)0LL, (int64_t)colon), false)) {
return StatementKind_Declaration;
}
String rejected[11] = {StringLiteral("s8 "), StringLiteral("s16 "), StringLiteral("s32 "), StringLiteral("s64 "), StringLiteral("float32 "), StringLiteral("float64 "), StringLiteral("bool "), StringLiteral("unsigned "), StringLiteral("long "), StringLiteral("const "), StringLiteral("struct ")};
for (int64_t it_index = 0LL; it_index < 11; it_index++) {
String prefix = (rejected)[it_index];
if (compiler_source_Matches(source, 0LL, prefix)) {
return StatementKind_Declaration;
}
}
int64_t at = 0LL;
int32_t depth = 0;
while (at < (int64_t)(source).length) {
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
if (byte == 34 || byte == 39) {
at = compiler_source_AfterQuote(source, at);
continue;
}
if (byte == 40 || byte == 91 || byte == 123) {
depth = (int32_t)((uint32_t)depth + 1u);
} else if (byte == 41 || byte == 93 || byte == 125) {
if (depth > 0) {
depth = (int32_t)((uint32_t)depth - 1u);
}
} else if (depth == 0 && compiler_source_Matches(source, at, StringLiteral(":="))) {
return StatementKind_Declaration;
} else if (byte == 61 && depth == 0) {
return StatementKind_Assignment;
}
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
int64_t value_6 = compiler_source_Find(source, StringLiteral("("));
bool value_7 = value_6 >= 0LL;
if (!value_7) {
int64_t value_8 = compiler_source_Find(source, StringLiteral("+"));
value_7 = (value_8 >= 0LL);
}
bool value_9 = value_7;
if (!value_9) {
int64_t value_10 = compiler_source_Find(source, StringLiteral("-"));
value_9 = (value_10 >= 0LL);
}
if (value_9) {
return StatementKind_Expression;
}
return StatementKind_Unknown;
}
void
compiler_source_NormalizeRecordSeparators(Slice body, bool split_commas)
{
int64_t at = 0LL;
bool quoted = false;
int32_t nesting = 0;
while (at < (int64_t)(body).length) {
uint8_t byte = ((uint8_t *)body.data)[SliceIndex(body, (int64_t)at)];
if (quoted) {
if (byte == 92 && (int64_t)((uint64_t)at + UINT64_C(1)) < (int64_t)(body).length) {
at = (int64_t)((uint64_t)at + UINT64_C(1));
} else if (byte == 34) {
quoted = false;
}
} else if (byte == 34) {
quoted = true;
} else if (byte == 91 || byte == 40) {
nesting = (int32_t)((uint32_t)nesting + 1u);
} else if (byte == 93 || byte == 41) {
nesting = (int32_t)((uint32_t)nesting - 1u);
} else if (nesting == 0 && (byte == 59 || (split_commas && byte == 44))) {
((uint8_t *)body.data)[SliceIndex(body, (int64_t)at)] = 10;
}
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
}
bool
compiler_source_ContainsDirective(String source, String directive)
{
int64_t at = 0LL;
while (at < (int64_t)(source).length) {
if ((uint8_t)ZIRAN_INDEX(source.data, source.length, at) == 34) {
at = compiler_source_AfterQuote(source, at);
} else if (compiler_source_Matches(source, at, StringLiteral("//"))) {
return false;
} else {
bool value_0 = compiler_source_Matches(source, at, directive);
bool value_1 = value_0;
if (value_1) {
bool value_2 = compiler_source_SourceIdentifier(compiler_source_Byte(source, (int64_t)((uint64_t)at + (uint64_t)((int64_t)(directive).length))));
value_1 = !value_2;
}
if (value_1) {
return true;
} else {
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
}
}
return false;
}
SourceRewrite
compiler_source_RewriteRawStrings(String source, Slice output, int64_t marker_limit)
{
SourceRewrite result = {0};
int64_t at = 0LL;
int64_t used = 0LL;
int64_t gaps = 0LL;
int32_t line = 1;
int32_t column = 1;
uint8_t quote = 0;
bool line_comment = false;
int32_t comment_depth = 0;
while (at < (int64_t)(source).length) {
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
bool value_0 = !line_comment && quote == 0;
if (value_0) {
bool value_1 = compiler_source_Matches(source, at, StringLiteral("/*"));
bool value_2 = value_1 || (comment_depth > 0 && compiler_source_Matches(source, at, StringLiteral("*/")));
value_0 = value_2;
}
if (value_0) {
if (byte == 47) {
comment_depth = (int32_t)((uint32_t)comment_depth + 1u);
} else {
comment_depth = (int32_t)((uint32_t)comment_depth - 1u);
}
compiler_source_Put(output, &(used), byte);
compiler_source_Put(output, &(used), (uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)at + UINT64_C(1)))));
at = (int64_t)((uint64_t)at + UINT64_C(2));
column = (int32_t)((uint32_t)column + 2u);
continue;
}
if (!line_comment && comment_depth == 0 && quote == 0) {
if (compiler_source_Matches(source, at, StringLiteral("//"))) {
line_comment = true;
} else if (byte == 34 || byte == 39) {
quote = byte;
} else {
bool value_3 = compiler_source_Matches(source, at, StringLiteral("#string"));
bool value_4 = value_3;
if (value_4) {
bool value_5 = at == 0LL;
if (!value_5) {
bool value_6 = compiler_source_SourceIdentifier((uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)at - UINT64_C(1)))));
value_5 = !value_6;
}
value_4 = value_5;
}
if (value_4 && ((int64_t)((uint64_t)at + UINT64_C(7)) == (int64_t)(source).length || compiler_source_Space((uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)at + UINT64_C(7))))))) {
result.line = line;
result.column = column;
int64_t cursor = (int64_t)((uint64_t)at + UINT64_C(7));
while (true) {
bool value_7 = compiler_source_Byte(source, cursor) == 32;
if (!(value_7 || compiler_source_Byte(source, cursor) == 9)) { break; }
cursor = (int64_t)((uint64_t)cursor + UINT64_C(1));
}
int64_t begin = cursor;
while (compiler_source_SourceIdentifier(compiler_source_Byte(source, cursor))) {
cursor = (int64_t)((uint64_t)cursor + UINT64_C(1));
}
int64_t end = cursor;
result.marker_begin = begin;
result.marker_end = end;
bool value_8 = end == begin || (int64_t)((uint64_t)end - (uint64_t)begin) >= marker_limit;
if (!value_8) {
bool value_9 = compiler_source_Letter(compiler_source_Byte(source, begin));
value_8 = !value_9;
}
if (value_8) {
result.error = SourceError_Delimiter;
return result;
}
while (true) {
bool value_10 = compiler_source_Byte(source, cursor) == 32;
if (!(value_10 || compiler_source_Byte(source, cursor) == 9)) { break; }
cursor = (int64_t)((uint64_t)cursor + UINT64_C(1));
}
if (compiler_source_Byte(source, cursor) == 13) {
cursor = (int64_t)((uint64_t)cursor + UINT64_C(1));
}
if (cursor >= (int64_t)(source).length || (uint8_t)ZIRAN_INDEX(source.data, source.length, cursor) != 10) {
result.error = SourceError_DelimiterLine;
return result;
}
int64_t body = (int64_t)((uint64_t)cursor + UINT64_C(1));
int64_t close = body;
String marker = StringRange(source, (int64_t)begin, (int64_t)end);
while (close < (int64_t)(source).length) {
bool value_11 = compiler_source_Matches(source, close, marker);
bool value_12 = value_11;
if (value_12) {
bool value_13 = compiler_source_SourceIdentifier(compiler_source_Byte(source, (int64_t)((uint64_t)close + (uint64_t)((int64_t)(marker).length))));
value_12 = !value_13;
}
if (value_12) {
break;
}
while (close < (int64_t)(source).length && (uint8_t)ZIRAN_INDEX(source.data, source.length, close) != 10) {
close = (int64_t)((uint64_t)close + UINT64_C(1));
}
if (close < (int64_t)(source).length) {
close = (int64_t)((uint64_t)close + UINT64_C(1));
}
}
if (close >= (int64_t)(source).length) {
result.error = SourceError_Unterminated;
return result;
}
compiler_source_Put(output, &(used), 34);
cursor = body;
while (cursor < close) {
compiler_source_PutRaw(output, &(used), (uint8_t)ZIRAN_INDEX(source.data, source.length, cursor));
cursor = (int64_t)((uint64_t)cursor + UINT64_C(1));
}
compiler_source_Put(output, &(used), 34);
while (at < close) {
if ((uint8_t)ZIRAN_INDEX(source.data, source.length, at) == 10) {
line = (int32_t)((uint32_t)line + 1u);
gaps = (int64_t)((uint64_t)gaps + UINT64_C(1));
}
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
column = (int32_t)((uint32_t)((int64_t)(marker).length) + 1u);
at = (int64_t)((uint64_t)at + (uint64_t)((int64_t)(marker).length));
continue;
}
}
compiler_source_PutScanned(output, &(used), byte, &(gaps));
at = (int64_t)((uint64_t)at + UINT64_C(1));
} else if (quote != 0) {
compiler_source_PutScanned(output, &(used), byte, &(gaps));
at = (int64_t)((uint64_t)at + UINT64_C(1));
if (byte == 92 && at < (int64_t)(source).length) {
column = (int32_t)((uint32_t)column + 1u);
byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
compiler_source_PutScanned(output, &(used), byte, &(gaps));
at = (int64_t)((uint64_t)at + UINT64_C(1));
} else if (byte == quote) {
quote = 0;
}
} else {
compiler_source_PutScanned(output, &(used), byte, &(gaps));
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
if (byte == 10) {
line = (int32_t)((uint32_t)line + 1u);
column = 1;
line_comment = false;
} else {
column = (int32_t)((uint32_t)column + 1u);
}
}
result.count = used;
return result;
}
CommentScan
compiler_source_StripComments(String source, Slice output, int32_t depth)
{
int64_t at = 0LL;
int64_t used = 0LL;
uint8_t quote = 0;
while (at < (int64_t)(source).length) {
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
if (depth > 0) {
if (compiler_source_Matches(source, at, StringLiteral("/*"))) {
depth = (int32_t)((uint32_t)depth + 1u);
at = (int64_t)((uint64_t)at + UINT64_C(2));
} else if (compiler_source_Matches(source, at, StringLiteral("*/"))) {
depth = (int32_t)((uint32_t)depth - 1u);
at = (int64_t)((uint64_t)at + UINT64_C(2));
if (depth == 0) {
compiler_source_Put(output, &(used), 32);
}
} else {
if (byte == 10) {
compiler_source_Put(output, &(used), byte);
}
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
} else if (quote != 0) {
compiler_source_Put(output, &(used), byte);
at = (int64_t)((uint64_t)at + UINT64_C(1));
if (byte == 92 && at < (int64_t)(source).length) {
compiler_source_Put(output, &(used), (uint8_t)ZIRAN_INDEX(source.data, source.length, at));
at = (int64_t)((uint64_t)at + UINT64_C(1));
} else if (byte == quote || byte == 10) {
quote = 0;
}
} else if (byte == 34 || byte == 39) {
quote = byte;
compiler_source_Put(output, &(used), byte);
at = (int64_t)((uint64_t)at + UINT64_C(1));
} else if (compiler_source_Matches(source, at, StringLiteral("//"))) {
while (at < (int64_t)(source).length && (uint8_t)ZIRAN_INDEX(source.data, source.length, at) != 10) {
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
} else if (compiler_source_Matches(source, at, StringLiteral("/*"))) {
depth = 1;
at = (int64_t)((uint64_t)at + UINT64_C(2));
} else {
compiler_source_Put(output, &(used), byte);
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
}
return (CommentScan){.count = used, .depth = depth};
}
int32_t
compiler_source_NetBlockBraces(String source)
{
int64_t at = 0LL;
int32_t parentheses = 0;
int32_t result = 0;
while (at < (int64_t)(source).length) {
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
if (byte == 34 || byte == 39) {
at = compiler_source_AfterQuote(source, at);
continue;
}
if (byte == 40 || byte == 91) {
parentheses = (int32_t)((uint32_t)parentheses + 1u);
} else if (byte == 41 || byte == 93) {
if (parentheses > 0) {
parentheses = (int32_t)((uint32_t)parentheses - 1u);
}
} else if (parentheses == 0) {
if (byte == 123) {
result = (int32_t)((uint32_t)result + 1u);
} else if (byte == 125) {
result = (int32_t)((uint32_t)result - 1u);
}
}
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
return result;
}
int64_t
compiler_source_StatementSeparator(String source)
{
bool value_0 = compiler_source_Byte(source, 0LL) == 125;
if (value_0 && compiler_source_Byte(source, compiler_source_SkipSpace(source, 1LL)) == 125) {
return 0LL;
}
int64_t at = 0LL;
int32_t parentheses = 0;
int32_t brackets = 0;
int32_t braces = 0;
int64_t first_brace = compiler_source_Find(source, StringLiteral("{"));
int64_t conditional = compiler_source_Find(source, StringLiteral("#ifx"));
while (at < (int64_t)(source).length) {
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
if (byte == 34 || byte == 39) {
at = compiler_source_AfterQuote(source, at);
continue;
}
if (byte == 40) {
parentheses = (int32_t)((uint32_t)parentheses + 1u);
} else if (byte == 41) {
parentheses = (int32_t)((uint32_t)parentheses - 1u);
} else if (byte == 91) {
brackets = (int32_t)((uint32_t)brackets + 1u);
} else if (byte == 93) {
brackets = (int32_t)((uint32_t)brackets - 1u);
} else if (byte == 123) {
braces = (int32_t)((uint32_t)braces + 1u);
} else if (byte == 125 && braces > 0) {
braces = (int32_t)((uint32_t)braces - 1u);
} else if (byte == 59 && parentheses == 0 && brackets == 0 && braces == 0) {
int64_t after = compiler_source_SkipSpace(source, (int64_t)((uint64_t)at + UINT64_C(1)));
bool value_1 = after < (int64_t)(source).length;
if (value_1) {
bool value_2 = compiler_source_StartsWord(source, StringLiteral("for"));
value_1 = !(value_2 && (first_brace < 0LL || at < first_brace));
}
bool value_3 = value_1;
if (value_3) {
bool value_4 = conditional >= 0LL && conditional < at && compiler_source_StartsWord(StringRange(source, (int64_t)after, (int64_t)source.length), StringLiteral("else"));
value_3 = !value_4;
}
if (value_3) {
return at;
}
}
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
return -1LL;
}
ControlBlock
compiler_source_SplitControlBlock(String source)
{
ControlBlock result = {0};
if ((int64_t)(source).length < 4LL) {
return result;
}
int64_t control = 0LL;
if ((uint8_t)source.data[0] == 125) {
int64_t after = compiler_source_SkipSpace(source, 1LL);
if (compiler_source_StartsWord(StringRange(source, (int64_t)after, (int64_t)source.length), StringLiteral("else"))) {
control = after;
}
}
int64_t end = control;
while (true) {
bool value_0 = end < (int64_t)(source).length && compiler_source_SourceIdentifier((uint8_t)ZIRAN_INDEX(source.data, source.length, end));
if (!(value_0 && (int64_t)((uint64_t)end - (uint64_t)control) < 15LL)) { break; }
end = (int64_t)((uint64_t)end + UINT64_C(1));
}
String word = StringRange(source, (int64_t)control, (int64_t)end);
bool value_1 = !StringEqual(word, StringLiteral("if")) && !StringEqual(word, StringLiteral("else")) && !StringEqual(word, StringLiteral("while"));
bool value_2 = value_1 && !StringEqual(word, StringLiteral("for")) && !StringEqual(word, StringLiteral("defer"));
bool value_3 = value_2 && !StringEqual(word, StringLiteral("switch")) && !StringEqual(word, StringLiteral("case"));
bool value_4 = value_3 && !StringEqual(word, StringLiteral("guard")) && !StringEqual(word, StringLiteral("do"));
if (value_4) {
bool value_5 = (uint8_t)source.data[0] == 35;
bool value_6 = value_5 && compiler_source_StartsWord(source, StringLiteral("#asm"));
value_4 = !value_6;
}
if (value_4) {
return result;
}
int64_t at = 0LL;
int32_t parentheses = 0;
int32_t expressions = 0;
int64_t opening = 0LL;
while (at < (int64_t)(source).length) {
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
if (byte == 34 || byte == 39) {
at = compiler_source_AfterQuote(source, at);
continue;
}
if (byte == 40 || byte == 91) {
parentheses = (int32_t)((uint32_t)parentheses + 1u);
} else if (byte == 41 || byte == 93) {
parentheses = (int32_t)((uint32_t)parentheses - 1u);
} else if (byte == 123 && parentheses == 0) {
int64_t before = at;
while (before > 0LL && compiler_source_Space((uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)before - UINT64_C(1)))))) {
before = (int64_t)((uint64_t)before - UINT64_C(1));
}
bool value_7 = expressions > 0;
if (!value_7) {
bool value_8 = before > 0LL && (uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)before - UINT64_C(1)))) == 46;
bool value_9 = value_8 && (before < 2LL || (uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)before - UINT64_C(2)))) != 46);
value_7 = value_9;
}
if (value_7) {
expressions = (int32_t)((uint32_t)expressions + 1u);
} else {
opening = at;
break;
}
} else if (byte == 125 && parentheses == 0 && expressions > 0) {
expressions = (int32_t)((uint32_t)expressions - 1u);
}
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
if (opening == 0LL || expressions != 0) {
return result;
}
int32_t depth = 1;
at = (int64_t)((uint64_t)opening + UINT64_C(1));
while (at < (int64_t)(source).length) {
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
if (byte == 34 || byte == 39) {
at = compiler_source_ClosedQuote(source, at);
if (at < 0LL) {
return result;
}
continue;
}
if (byte == 123) {
depth = (int32_t)((uint32_t)depth + 1u);
} else if (byte == 125) {
depth = (int32_t)((uint32_t)depth - 1u);
if (depth == 0) {
break;
}
}
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
if (at >= (int64_t)(source).length) {
int64_t value_10 = compiler_source_SkipSpace(source, (int64_t)((uint64_t)opening + UINT64_C(1)));
int64_t value_11 = (int64_t)(source).length;
if (value_10 >= value_11) {
return result;
}
return (ControlBlock){.valid = true, .head_end = (int64_t)((uint64_t)opening + UINT64_C(1)), .body_begin = (int64_t)((uint64_t)opening + UINT64_C(1)), .body_end = (int64_t)(source).length, .tail_begin = (int64_t)(source).length, .open = true};
}
int64_t tail = compiler_source_SkipSpace(source, (int64_t)((uint64_t)at + UINT64_C(1)));
if (compiler_source_Byte(source, tail) == 59) {
tail = compiler_source_SkipSpace(source, (int64_t)((uint64_t)tail + UINT64_C(1)));
}
bool value_12 = tail < (int64_t)(source).length;
if (value_12) {
bool value_13 = StringEqual(word, StringLiteral("do")) || StringEqual(word, StringLiteral("case")) || StringEqual(word, StringLiteral("guard"));
bool value_14 = value_13;
if (!value_14) {
bool value_15 = compiler_source_StartsWord(StringRange(source, (int64_t)tail, (int64_t)source.length), StringLiteral("else"));
value_14 = (value_15 && !StringEqual(word, StringLiteral("if")) && !StringEqual(word, StringLiteral("else")));
}
value_12 = value_14;
}
if (value_12) {
return result;
}
return (ControlBlock){.valid = true, .head_end = (int64_t)((uint64_t)opening + UINT64_C(1)), .body_begin = (int64_t)((uint64_t)opening + UINT64_C(1)), .body_end = at, .tail_begin = tail, .open = false};
}
int64_t
compiler_source_ClosingParenthesis(String source)
{
if ((int64_t)(source).length == 0LL || (uint8_t)ZIRAN_INDEX(source.data, source.length, 0) != 40) {
return -1LL;
}
int32_t depth = 0;
int64_t at = 0LL;
while (at < (int64_t)(source).length) {
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
if (byte == 34 || byte == 39) {
at = compiler_source_AfterQuote(source, at);
continue;
}
if (byte == 40) {
depth = (int32_t)((uint32_t)depth + 1u);
} else if (byte == 41) {
depth = (int32_t)((uint32_t)depth - 1u);
if (depth == 0) {
return at;
}
}
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
return -1LL;
}
ProcedureHeader
compiler_source_ProcedureHeaderParts(String source)
{
ProcedureHeader header = {.name = StringLiteral(""), .parameters = StringLiteral(""), .result = StringLiteral("void")};
int64_t colons = compiler_source_Find(source, StringLiteral("::"));
if (colons >= 0LL) {
int64_t begin = 0LL;
while (begin < colons && ((uint8_t)ZIRAN_INDEX(source.data, source.length, begin) == 32 || (uint8_t)ZIRAN_INDEX(source.data, source.length, begin) == 9)) {
begin = (int64_t)((uint64_t)begin + UINT64_C(1));
}
int64_t end = begin;
while (end < colons && compiler_source_SourceIdentifier((uint8_t)ZIRAN_INDEX(source.data, source.length, end))) {
end = (int64_t)((uint64_t)end + UINT64_C(1));
}
header.name = StringRange(source, (int64_t)begin, (int64_t)end);
}
int64_t opening = compiler_source_Find(source, StringLiteral("("));
if (opening < 0LL) {
return header;
}
int64_t closing = compiler_source_ClosingParenthesis(StringRange(source, (int64_t)opening, (int64_t)source.length));
if (closing < 0LL) {
return header;
}
closing = (int64_t)((uint64_t)closing + (uint64_t)opening);
int64_t end = closing;
while (end > (int64_t)((uint64_t)opening + UINT64_C(1)) && compiler_source_Space((uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)end - UINT64_C(1)))))) {
end = (int64_t)((uint64_t)end - UINT64_C(1));
}
header.parameters = StringRange(source, (int64_t)((int64_t)((uint64_t)opening + UINT64_C(1))), (int64_t)end);
int64_t at = (int64_t)((uint64_t)closing + UINT64_C(1));
while (at < (int64_t)(source).length && ((uint8_t)ZIRAN_INDEX(source.data, source.length, at) == 32 || (uint8_t)ZIRAN_INDEX(source.data, source.length, at) == 9)) {
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
bool value_0 = compiler_source_Matches(source, at, StringLiteral("->"));
if (!value_0) {
return header;
}
at = (int64_t)((uint64_t)at + UINT64_C(2));
while (at < (int64_t)(source).length && ((uint8_t)ZIRAN_INDEX(source.data, source.length, at) == 32 || (uint8_t)ZIRAN_INDEX(source.data, source.length, at) == 9)) {
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
end = at;
while (end < (int64_t)(source).length && (uint8_t)ZIRAN_INDEX(source.data, source.length, end) != 35 && (uint8_t)ZIRAN_INDEX(source.data, source.length, end) != 123) {
end = (int64_t)((uint64_t)end + UINT64_C(1));
}
while (end > at && compiler_source_Space((uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)end - UINT64_C(1)))))) {
end = (int64_t)((uint64_t)end - UINT64_C(1));
}
header.result = StringRange(source, (int64_t)at, (int64_t)end);
return header;
}
int64_t
compiler_source_NormalizeTypeSpacing(String source, Slice output)
{
int64_t used = 0LL;
int64_t at = 0LL;
bool quoted = false;
while (at < (int64_t)(source).length) {
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
compiler_source_Put(output, &(used), byte);
at = (int64_t)((uint64_t)at + UINT64_C(1));
if (quoted) {
if (byte == 92 && at < (int64_t)(source).length) {
compiler_source_Put(output, &(used), (uint8_t)ZIRAN_INDEX(source.data, source.length, at));
at = (int64_t)((uint64_t)at + UINT64_C(1));
} else if (byte == 34) {
quoted = false;
}
} else if (byte == 34) {
quoted = true;
} else if (byte == 93 || byte == 42) {
int64_t next = at;
while (next < (int64_t)(source).length && ((uint8_t)ZIRAN_INDEX(source.data, source.length, next) == 32 || (uint8_t)ZIRAN_INDEX(source.data, source.length, next) == 9)) {
next = (int64_t)((uint64_t)next + UINT64_C(1));
}
uint8_t following = compiler_source_Byte(source, next);
bool value_0 = next > at;
if (value_0) {
bool value_1 = compiler_source_Letter(following);
value_0 = (value_1 || following == 91 || following == 36);
}
if (value_0) {
at = next;
}
}
}
return used;
}
int64_t
compiler_source_CopyProcedureText(String source, Slice output, bool normalize_type)
{
if ((int64_t)(output).length == 0LL) {
return 0LL;
}
int64_t count = (int64_t)(source).length;
if (count >= (int64_t)(output).length) {
count = (int64_t)((uint64_t)((int64_t)(output).length) - UINT64_C(1));
}
int64_t used = 0LL;
if (normalize_type) {
while (count > 0LL && compiler_source_Space((uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)count - UINT64_C(1)))))) {
count = (int64_t)((uint64_t)count - UINT64_C(1));
}
Slice value_0 = output;
used = compiler_source_NormalizeTypeSpacing(StringRange(source, (int64_t)0LL, (int64_t)count), SliceRange(value_0, (int64_t)0LL, (int64_t)count, sizeof(uint8_t)));
} else {
{
int64_t range_first_15 = 0LL;
int64_t range_last_15 = (int64_t)((uint64_t)count - UINT64_C(1));
int64_t index = range_first_15;
while (index <= range_last_15) {
((uint8_t *)output.data)[SliceIndex(output, (int64_t)index)] = (uint8_t)ZIRAN_INDEX(source.data, source.length, index);
if (index == range_last_15) {
break;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
}
}
used = count;
}
((uint8_t *)output.data)[SliceIndex(output, (int64_t)used)] = 0;
return used;
}
int64_t
compiler_source_OpeningBraceOutsideLiterals(String source)
{
int64_t at = 0LL;
while (at < (int64_t)(source).length) {
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
if (byte == 92) {
at = (int64_t)((uint64_t)at + UINT64_C(1));
if (at < (int64_t)(source).length) {
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
} else if (byte == 34 || byte == 39) {
at = compiler_source_AfterQuote(source, at);
} else if (byte == 123) {
return at;
} else {
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
}
return -1LL;
}
bool
compiler_source_BraceOutsideLiterals(String source)
{
int64_t value_0 = compiler_source_OpeningBraceOutsideLiterals(source);
return value_0 >= 0LL;
}
bool
compiler_source_LooksLikeProcedureHeader(String source)
{
bool value_0 = compiler_source_StartsWord(source, StringLiteral("#import"));
if (value_0 || compiler_source_Matches(source, 0LL, StringLiteral("#import,"))) {
return false;
}
int64_t colons = compiler_source_Find(source, StringLiteral("::"));
if (colons < 0LL) {
return false;
}
int64_t at = compiler_source_SkipSpace(source, (int64_t)((uint64_t)colons + UINT64_C(2)));
int64_t end = (int64_t)(source).length;
while (end > at && compiler_source_Space((uint8_t)ZIRAN_INDEX(source.data, source.length, ((int64_t)((uint64_t)end - UINT64_C(1)))))) {
end = (int64_t)((uint64_t)end - UINT64_C(1));
}
String body = StringRange(source, (int64_t)at, (int64_t)end);
if (compiler_source_StartsWord(body, StringLiteral("#as"))) {
String value_1 = body;
int64_t value_2 = compiler_source_SkipSpace(body, 3LL);
body = StringRange(value_1, (int64_t)value_2, (int64_t)value_1.length);
}
bool value_3 = compiler_source_StartsWord(body, StringLiteral("#import"));
bool value_4 = value_3 || compiler_source_Matches(body, 0LL, StringLiteral("#import,"));
bool value_5 = value_4 || compiler_source_StartsWord(body, StringLiteral("#defined"));
bool value_6 = value_5 || compiler_source_StartsWord(body, StringLiteral("#define"));
bool value_7 = value_6 || compiler_source_StartsWord(body, StringLiteral("struct"));
bool value_8 = value_7 || compiler_source_StartsWord(body, StringLiteral("enum"));
bool value_9 = value_8 || compiler_source_StartsWord(body, StringLiteral("union"));
bool value_10 = value_9;
if (!value_10) {
int64_t value_11 = compiler_source_Find(body, StringLiteral("#type"));
value_10 = (value_11 >= 0LL);
}
if (value_10 || compiler_source_Byte(body, 0LL) != 40) {
return false;
}
int64_t value_12 = compiler_source_Find(body, StringLiteral("{"));
bool value_13 = value_12 >= 0LL;
if (!value_13) {
int64_t value_14 = compiler_source_Find(body, StringLiteral("->"));
value_13 = (value_14 >= 0LL);
}
if (value_13) {
return true;
}
int32_t depth = 0;
int64_t begin = -1LL;
end = -1LL;
at = 0LL;
while (at < (int64_t)(body).length) {
if ((uint8_t)ZIRAN_INDEX(body.data, body.length, at) == 40) {
if (depth == 0) {
begin = (int64_t)((uint64_t)at + UINT64_C(1));
}
depth = (int32_t)((uint32_t)depth + 1u);
} else if ((uint8_t)ZIRAN_INDEX(body.data, body.length, at) == 41) {
depth = (int32_t)((uint32_t)depth - 1u);
if (depth == 0) {
end = at;
break;
}
}
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
if (begin < 0LL || end < 0LL) {
return false;
}
while (begin < end && ((uint8_t)ZIRAN_INDEX(body.data, body.length, begin) == 32 || (uint8_t)ZIRAN_INDEX(body.data, body.length, begin) == 9)) {
begin = (int64_t)((uint64_t)begin + UINT64_C(1));
}
if (begin >= end) {
return true;
}
uint8_t byte = (uint8_t)ZIRAN_INDEX(body.data, body.length, begin);
if (byte == 34 || byte == 39 || (byte >= 48 && byte <= 57)) {
return false;
}
int64_t value_15 = compiler_source_Find(StringRange(body, (int64_t)begin, (int64_t)body.length), StringLiteral("\?"));
bool value_16 = value_15 < 0LL;
if (value_16) {
int64_t value_17 = compiler_source_Find(StringRange(body, (int64_t)begin, (int64_t)body.length), StringLiteral(":"));
value_16 = (value_17 >= 0LL);
}
return value_16;
}
ControlBlock
compiler_source_SplitProcedureBody(String source)
{
ControlBlock block = {0};
int64_t opening = compiler_source_OpeningBraceOutsideLiterals(source);
bool value_0 = opening < 0LL || compiler_source_Byte(source, (int64_t)((uint64_t)((int64_t)(source).length) - UINT64_C(1))) != 125;
bool value_1 = value_0;
if (!value_1) {
bool value_2 = compiler_source_LooksLikeProcedureHeader(source);
value_1 = !value_2;
}
if (value_1) {
return block;
}
int32_t depth = 0;
int64_t at = opening;
while (at < (int64_t)(source).length) {
uint8_t byte = (uint8_t)ZIRAN_INDEX(source.data, source.length, at);
if (byte == 34 || byte == 39) {
at = compiler_source_AfterQuote(source, at);
continue;
}
if (byte == 123) {
depth = (int32_t)((uint32_t)depth + 1u);
} else if (byte == 125) {
depth = (int32_t)((uint32_t)depth - 1u);
if (depth < 0 || (depth == 0 && at != (int64_t)((uint64_t)((int64_t)(source).length) - UINT64_C(1)))) {
return block;
}
}
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
if (depth != 0) {
return block;
}
return (ControlBlock){.valid = true, .head_end = (int64_t)((uint64_t)opening + UINT64_C(1)), .body_begin = (int64_t)((uint64_t)opening + UINT64_C(1)), .body_end = (int64_t)((uint64_t)((int64_t)(source).length) - UINT64_C(1)), .tail_begin = (int64_t)(source).length, .open = false};
}
MustUseModifier
compiler_source_ProcedureMustUse(String source, String result_type)
{
int64_t opening = compiler_source_Find(source, StringLiteral("("));
if (opening < 0LL) {
return MustUseModifier_None;
}
int64_t closing = compiler_source_ClosingParenthesis(StringRange(source, (int64_t)opening, (int64_t)source.length));
if (closing < 0LL) {
return MustUseModifier_None;
}
closing = (int64_t)((uint64_t)closing + (uint64_t)opening);
String tail = StringRange(source, (int64_t)((int64_t)((uint64_t)closing + UINT64_C(1))), (int64_t)source.length);
int64_t end = compiler_source_Find(tail, StringLiteral("{"));
if (end < 0LL) {
end = compiler_source_Find(tail, StringLiteral(";"));
}
if (end < 0LL) {
end = (int64_t)(tail).length;
}
int32_t count = 0;
int64_t at = 0LL;
while (at < end) {
if ((uint8_t)ZIRAN_INDEX(tail.data, tail.length, at) == 34) {
at = compiler_source_AfterQuote(StringRange(tail, (int64_t)0LL, (int64_t)end), at);
} else {
bool value_0 = compiler_source_Matches(StringRange(tail, (int64_t)0LL, (int64_t)end), at, StringLiteral("#must"));
bool value_1 = value_0;
if (value_1) {
bool value_2 = compiler_source_SourceIdentifier(compiler_source_Byte(tail, (int64_t)((uint64_t)at + UINT64_C(5))));
value_1 = !value_2;
}
if (value_1) {
int64_t after = compiler_source_SkipSpace(tail, (int64_t)((uint64_t)at + UINT64_C(5)));
if (after < end && (uint8_t)ZIRAN_INDEX(tail.data, tail.length, after) != 35) {
return MustUseModifier_Arguments;
}
count = (int32_t)((uint32_t)count + 1u);
at = (int64_t)((uint64_t)at + UINT64_C(5));
} else {
at = (int64_t)((uint64_t)at + UINT64_C(1));
}
}
}
if (count > 1) {
return MustUseModifier_Duplicate;
}
if (count == 0) {
return MustUseModifier_None;
}
if (StringEqual(result_type, StringLiteral("void"))) {
return MustUseModifier_NoResult;
}
return MustUseModifier_Required;
}