Files
ziran/bootstrap/compiler_runtime_emit/compiler_runtime_emit.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

1597 lines
66 KiB
C

/* Generated by zi2c from compiler_runtime_emit.zi. */
#include <stdbool.h>
#include "zir_bounds.h"
#include "zir_string.h"
#include "zir_vec.h"
#define ZIRAN_COMPILER_RUNTIME_EMIT_H_PRIVATE 1
#include "compiler_runtime_emit.h"
#undef ZIRAN_COMPILER_RUNTIME_EMIT_H_PRIVATE
#ifndef ZIRAN_HELPER_ZiShrU64
#define ZIRAN_HELPER_ZiShrU64
#if (defined(__GNUC__) || defined(__clang__)) && !defined(__OPTIMIZE__)
#define ZiShrU64(a, s, w) __extension__ ({ uint64_t __zi_a = (a); uint64_t __zi_s = (s); if(__zi_s >= (uint64_t)(w)) abort(); __zi_a >> __zi_s; })
#else
static inline uint64_t ZiShrU64(uint64_t a, uint64_t s, int w) {
if(s >= (uint64_t)w) abort();
return a >> s;
}
#endif
#endif
#ifndef ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_RuntimeHeaderCountForeign_98631b8e
#define ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_RuntimeHeaderCountForeign_98631b8e
#ifdef __APPLE__
int64_t zir_foreign_zir_f_a87e346e42fbd6c3_RuntimeHeaderCountForeign(void) __asm__("_CompilerRuntimeHeaderCount");
#else
int64_t zir_foreign_zir_f_a87e346e42fbd6c3_RuntimeHeaderCountForeign(void) __asm__("CompilerRuntimeHeaderCount");
#endif
static int64_t
zir_f_a87e346e42fbd6c3_RuntimeHeaderCountForeign(void)
{
return zir_foreign_zir_f_a87e346e42fbd6c3_RuntimeHeaderCountForeign();
}
#endif
#ifndef ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_RuntimeHeaderAtForeign_800aae4c
#define ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_RuntimeHeaderAtForeign_800aae4c
#ifdef __APPLE__
RuntimeHeader* zir_foreign_zir_f_a87e346e42fbd6c3_RuntimeHeaderAtForeign(int64_t index) __asm__("_CompilerRuntimeHeaderAt");
#else
RuntimeHeader* zir_foreign_zir_f_a87e346e42fbd6c3_RuntimeHeaderAtForeign(int64_t index) __asm__("CompilerRuntimeHeaderAt");
#endif
static RuntimeHeader*
zir_f_a87e346e42fbd6c3_RuntimeHeaderAtForeign(int64_t index)
{
return zir_foreign_zir_f_a87e346e42fbd6c3_RuntimeHeaderAtForeign(index);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_SimdNamesFind_123dd4ec
#define ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_SimdNamesFind_123dd4ec
#ifdef __APPLE__
int64_t zir_foreign_zir_f_a87e346e42fbd6c3_SimdNamesFind(SimdNames* names, uint8_t* name, size_t length, int32_t add) __asm__("_SimdNamesFind");
#else
int64_t zir_foreign_zir_f_a87e346e42fbd6c3_SimdNamesFind(SimdNames* names, uint8_t* name, size_t length, int32_t add) __asm__("SimdNamesFind");
#endif
static int64_t
zir_f_a87e346e42fbd6c3_SimdNamesFind(SimdNames* names, uint8_t* name, size_t length, int32_t add)
{
return zir_foreign_zir_f_a87e346e42fbd6c3_SimdNamesFind(names, name, length, add);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_SimdNamesFree_0b044a3e
#define ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_SimdNamesFree_0b044a3e
#ifdef __APPLE__
void zir_foreign_zir_f_a87e346e42fbd6c3_SimdNamesFree(SimdNames* names) __asm__("_SimdNamesFree");
#else
void zir_foreign_zir_f_a87e346e42fbd6c3_SimdNamesFree(SimdNames* names) __asm__("SimdNamesFree");
#endif
static void
zir_f_a87e346e42fbd6c3_SimdNamesFree(SimdNames* names)
{
zir_foreign_zir_f_a87e346e42fbd6c3_SimdNamesFree(names);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_SimdHelperWord_c5e0fce6
#define ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_SimdHelperWord_c5e0fce6
#ifdef __APPLE__
int32_t zir_foreign_zir_f_a87e346e42fbd6c3_SimdHelperWord(uint8_t** word, size_t* length) __asm__("_SimdHelperWord");
#else
int32_t zir_foreign_zir_f_a87e346e42fbd6c3_SimdHelperWord(uint8_t** word, size_t* length) __asm__("SimdHelperWord");
#endif
static int32_t
zir_f_a87e346e42fbd6c3_SimdHelperWord(uint8_t** word, size_t* length)
{
return zir_foreign_zir_f_a87e346e42fbd6c3_SimdHelperWord(word, length);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_GeneratedOutputRecord_ea447a8a
#define ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_GeneratedOutputRecord_ea447a8a
#ifdef __APPLE__
void zir_foreign_zir_f_a87e346e42fbd6c3_GeneratedOutputRecord(uint8_t* path) __asm__("_GeneratedOutputRecord");
#else
void zir_foreign_zir_f_a87e346e42fbd6c3_GeneratedOutputRecord(uint8_t* path) __asm__("GeneratedOutputRecord");
#endif
static void
zir_f_a87e346e42fbd6c3_GeneratedOutputRecord(uint8_t* path)
{
zir_foreign_zir_f_a87e346e42fbd6c3_GeneratedOutputRecord(path);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_GeneratedOutputPath_de1b771f
#define ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_GeneratedOutputPath_de1b771f
#ifdef __APPLE__
uint8_t* zir_foreign_zir_f_a87e346e42fbd6c3_GeneratedOutputPath(int32_t index) __asm__("_GeneratedOutputPath");
#else
uint8_t* zir_foreign_zir_f_a87e346e42fbd6c3_GeneratedOutputPath(int32_t index) __asm__("GeneratedOutputPath");
#endif
static uint8_t*
zir_f_a87e346e42fbd6c3_GeneratedOutputPath(int32_t index)
{
return zir_foreign_zir_f_a87e346e42fbd6c3_GeneratedOutputPath(index);
}
#endif
#ifndef ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_ProcessId_bd88b581
#define ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_ProcessId_bd88b581
#ifdef __APPLE__
int32_t zir_foreign_zir_f_a87e346e42fbd6c3_ProcessId(void) __asm__("_getpid");
#else
int32_t zir_foreign_zir_f_a87e346e42fbd6c3_ProcessId(void) __asm__("getpid");
#endif
static int32_t
zir_f_a87e346e42fbd6c3_ProcessId(void)
{
return zir_foreign_zir_f_a87e346e42fbd6c3_ProcessId();
}
#endif
#ifndef ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_FreeRaw_c75930da
#define ZIR_FOREIGN_zir_f_a87e346e42fbd6c3_FreeRaw_c75930da
#ifdef __APPLE__
void zir_foreign_zir_f_a87e346e42fbd6c3_FreeRaw(void* memory) __asm__("_free");
#else
void zir_foreign_zir_f_a87e346e42fbd6c3_FreeRaw(void* memory) __asm__("free");
#endif
static void
zir_f_a87e346e42fbd6c3_FreeRaw(void* memory)
{
zir_foreign_zir_f_a87e346e42fbd6c3_FreeRaw(memory);
}
#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_runtime_emit_ReleaseOwnedText(String value) ZIRAN_MAYBE_UNUSED;
static String compiler_runtime_emit_CText(uint8_t* value) ZIRAN_MAYBE_UNUSED;
static uint8_t* compiler_runtime_emit_CPointer(String value) ZIRAN_MAYBE_UNUSED;
static bool compiler_runtime_emit_TextHasPrefix(String value, String prefix) ZIRAN_MAYBE_UNUSED;
static zir_8e7ef9a1e6f28a8e_Results__string__bool compiler_runtime_emit_JoinPath(String directory, String name) ZIRAN_MAYBE_UNUSED;
static int64_t compiler_runtime_emit_DecimalCount(int64_t value) ZIRAN_MAYBE_UNUSED;
static zir_8e7ef9a1e6f28a8e_Results__string__bool compiler_runtime_emit_JoinTemporary(String path, int32_t process_id) ZIRAN_MAYBE_UNUSED;
static bool compiler_runtime_emit_SameBytes(String left, String right) ZIRAN_MAYBE_UNUSED;
static bool compiler_runtime_emit_WriteGenerated(String path, String text) ZIRAN_MAYBE_UNUSED;
static Slice compiler_runtime_emit_BytesFromText(String value) ZIRAN_MAYBE_UNUSED;
static bool compiler_runtime_emit_WordByte(uint8_t value) ZIRAN_MAYBE_UNUSED;
static bool compiler_runtime_emit_KeptSegments(String text, SimdNames* used, __type_838711075b90279a* output) ZIRAN_MAYBE_UNUSED;
static bool compiler_runtime_emit_SegmentNamed(String names, SimdNames* used) ZIRAN_MAYBE_UNUSED;
static void compiler_runtime_emit_NeedHelper(int64_t helper, __type_1f00ef809eed7c2e* needed, __type_1f00ef809eed7c2e* work) ZIRAN_MAYBE_UNUSED;
static void compiler_runtime_emit_NeedCalls(SimdNames* helpers, Segment segment, String full, __type_1f00ef809eed7c2e* needed, __type_1f00ef809eed7c2e* work) ZIRAN_MAYBE_UNUSED;
static bool compiler_runtime_emit_Cleanup(SimdNames* helpers, __type_2f893a38d978b828* segments, __type_1f00ef809eed7c2e* last, __type_1f00ef809eed7c2e* first, __type_1f00ef809eed7c2e* needed, __type_1f00ef809eed7c2e* work, __type_838711075b90279a* output, bool result) ZIRAN_MAYBE_UNUSED;
GeneratedPathStore GeneratedPaths = {};
bool
compiler_runtime_emit_RecordPath(String path)
{
if ((int64_t)(path).length == 0LL) {
return false;
}
{
Slice value_0 = {(GeneratedPaths.paths).data, (GeneratedPaths.paths).count};
Slice value_1 = SliceRange(value_0, 0LL, GeneratedPaths.paths.count, sizeof(*(GeneratedPaths.paths).data));
Slice loop_view_3 = value_1;
int64_t loop_count_3 = (int64_t)(loop_view_3).length;
int64_t loop_cursor_3 = 0LL;
while (loop_cursor_3 < loop_count_3) {
int64_t loop_index_3 = loop_cursor_3;
String existing = ((String *)loop_view_3.data)[loop_index_3];
if (StringEqual(existing, path)) {
return true;
}
loop_cursor_3 = (int64_t)((uint64_t)loop_cursor_3 + UINT64_C(1));
}
}
__type_838711075b90279a bytes = {0};
__type_838711075b90279a* value_2 = &bytes;
bool value_3 = false;
if ((path).length == 0) {
value_3 = true;
} else {
void *value_4 = ZirVecReserve((void *)(bytes).data, &(bytes).capacity, (bytes).count, (int64_t)(path).length, sizeof(*(bytes).data));
if (value_4 != NULL) {
(bytes).data = (uint8_t *)value_4;
memcpy((bytes).data + (bytes).count, (path).data, (size_t)(path).length);
(bytes).count += (int64_t)(path).length;
value_3 = true;
}
}
bool value_5 = !value_3;
if (!value_5) {
uint8_t value_6 = 0;
bool value_7 = false;
void *value_8 = ZirVecGrow((void *)(bytes).data, &(bytes).capacity, (bytes).count, sizeof(*(bytes).data));
if (value_8 != NULL) {
(bytes).data = (uint8_t *)value_8;
(bytes).data[(bytes).count++] = value_6;
value_7 = true;
}
value_5 = !value_7;
}
if (value_5) {
free((bytes).data);
(bytes).data = NULL;
bytes.capacity = 0;
bytes.count = 0;
bool value_9 = false;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_9;
}
String value_10 = {NULL, 0};
value_10.data = (bytes).count > 0 ? (const char *)(bytes).data : "";
value_10.length = (size_t)(bytes).count;
if ((bytes).count == 0) free((bytes).data);
(bytes).data = NULL;
bytes.capacity = 0;
bytes.count = 0;
String terminated = value_10;
String owned = StringRange(terminated, (int64_t)0LL, (int64_t)(int64_t)(path).length);
String value_11 = owned;
bool value_12 = false;
void *value_13 = ZirVecGrow((void *)(GeneratedPaths.paths).data, &(GeneratedPaths.paths).capacity, (GeneratedPaths.paths).count, sizeof(*(GeneratedPaths.paths).data));
if (value_13 != NULL) {
(GeneratedPaths.paths).data = (String *)value_13;
(GeneratedPaths.paths).data[(GeneratedPaths.paths).count++] = value_11;
value_12 = true;
}
if (!value_12) {
compiler_runtime_emit_ReleaseOwnedText(owned);
bool value_14 = false;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_14;
}
uint8_t* value_15 = (void *)0;
{
String value = owned;
if ((int64_t)(value).length == 0LL) {
value_15 = (void *)0;
goto value_16;
}
TextDescriptor* descriptor = (TextDescriptor*)(&(value));
value_15 = descriptor->data;
value_16: ;
}
zir_f_a87e346e42fbd6c3_GeneratedOutputRecord(value_15);
bool value_17 = true;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_17;
}
bool
compiler_runtime_emit_RecordPathC(uint8_t* path)
{
return compiler_runtime_emit_RecordPath(compiler_runtime_emit_CText(path));
}
int64_t
compiler_runtime_emit_GeneratedPathCount(void)
{
int64_t count = 0LL;
while (true) {
uint8_t* value_0 = zir_f_a87e346e42fbd6c3_GeneratedOutputPath((int32_t)(count));
if (!(value_0 != ((void *)0))) { break; }
count = (int64_t)((uint64_t)count + UINT64_C(1));
}
return count;
}
uint8_t*
compiler_runtime_emit_GeneratedPath(int64_t index)
{
if (index < 0LL || index > 2147483647LL) {
return (void *)0;
}
return zir_f_a87e346e42fbd6c3_GeneratedOutputPath((int32_t)(index));
}
bool
compiler_runtime_emit_IsRuntimeHeader(String name)
{
int64_t count = zir_f_a87e346e42fbd6c3_RuntimeHeaderCountForeign();
int64_t index = 0LL;
while (index < count) {
RuntimeHeader* header = zir_f_a87e346e42fbd6c3_RuntimeHeaderAtForeign(index);
bool value_0 = header != ((void *)0);
if (value_0 && StringEqual(compiler_runtime_emit_CText((uint8_t*)header->name), name)) {
return true;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
}
return false;
}
bool
compiler_runtime_emit_EmitRuntimeHeaders(String out_dir, String generated_path, ZirSimdUses* simd, SimdNames* used)
{
__type_838711075b90279a generated_bytes = {0};
__type_838711075b90279a* value_2 = &generated_bytes;
bool readable = file_ReadFileInto(generated_path, &(generated_bytes));
if (!readable) {
free((generated_bytes).data);
(generated_bytes).data = NULL;
generated_bytes.capacity = 0;
generated_bytes.count = 0;
bool value_3 = false;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_3;
}
Slice value_4 = {(generated_bytes).data, (generated_bytes).count};
Slice value_5 = SliceRange(value_4, 0LL, generated_bytes.count, sizeof(*(generated_bytes).data));
String generated = StringView((const char *)(value_5).data, (size_t)(value_5).length);
int64_t count = zir_f_a87e346e42fbd6c3_RuntimeHeaderCountForeign();
__type_9626e91dad9499a3 needed = {0};
__type_9626e91dad9499a3* value_6 = &needed;
int64_t value_8 = count;
bool value_9 = false;
if (value_8 >= 0) {
value_9 = value_8 <= (needed).capacity;
if (!value_9) {
void *value_10 = ZirVecReserve((void *)(needed).data, &(needed).capacity, (needed).count, value_8 - (needed).count, sizeof(*(needed).data));
if (value_10 != NULL) { (needed).data = (bool *)value_10; value_9 = true; }
}
if (value_9) {
if (value_8 > (needed).count) memset((needed).data + (needed).count, 0, (size_t)(value_8 - (needed).count) * sizeof(*(needed).data));
(needed).count = value_8;
}
}
if (!value_9) {
bool cleanup_return_0 = false;
{
free((generated_bytes).data);
(generated_bytes).data = NULL;
generated_bytes.capacity = 0;
generated_bytes.count = 0;
}
bool value_12 = cleanup_return_0;
free((*value_6).data);
(*value_6).data = NULL;
(*value_6).capacity = 0;
(*value_6).count = 0;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_12;
}
{
int64_t range_first_16 = 0LL;
int64_t range_last_16 = (int64_t)((uint64_t)count - UINT64_C(1));
int64_t index = range_first_16;
if (index <= range_last_16) for (;;) {
bool* value_13 = &(ZIRAN_VEC_INDEX((needed).data, (needed).count, index));
(*value_13) = false;
if (index == range_last_16) {
break;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
}
}
int64_t line_start = 0LL;
while (line_start < (int64_t)(generated).length) {
int64_t line_end = line_start;
while (line_end < (int64_t)(generated).length && (uint8_t)ZIRAN_INDEX(generated.data, generated.length, line_end) != 10) {
line_end = (int64_t)((uint64_t)line_end + UINT64_C(1));
}
String line = StringRange(generated, (int64_t)line_start, (int64_t)line_end);
String value_14 = StringLiteral("#include \"");
bool value_15 = false;
{
String value = line;
String prefix = value_14;
if ((int64_t)(prefix).length > (int64_t)(value).length) {
value_15 = false;
goto value_16;
}
value_15 = StringEqual(StringRange(value, (int64_t)0LL, (int64_t)(int64_t)(prefix).length), prefix);
value_16: ;
}
if (value_15) {
int64_t index = 0LL;
while (index < count) {
RuntimeHeader* header = zir_f_a87e346e42fbd6c3_RuntimeHeaderAtForeign(index);
String name = compiler_runtime_emit_CText((uint8_t*)header->name);
int64_t value_17 = (int64_t)(line).length;
int64_t value_18 = (int64_t)(((uint64_t)((int64_t)(StringLiteral("#include \"")).length) + (uint64_t)((int64_t)(name).length)) + UINT64_C(1));
bool value_19 = value_17 >= value_18;
if (value_19) {
int64_t value_20 = (int64_t)((uint64_t)((int64_t)(StringLiteral("#include \"")).length) + (uint64_t)((int64_t)(name).length));
bool value_21 = StringEqual(StringRange(line, (int64_t)(int64_t)(StringLiteral("#include \"")).length, (int64_t)value_20), name);
value_19 = value_21;
}
bool value_22 = value_19;
if (value_22) {
int64_t value_23 = (int64_t)((uint64_t)((int64_t)(StringLiteral("#include \"")).length) + (uint64_t)((int64_t)(name).length));
value_22 = ((uint8_t)ZIRAN_INDEX(line.data, line.length, value_23) == 34);
}
if (value_22) {
bool* value_24 = &(ZIRAN_VEC_INDEX((needed).data, (needed).count, index));
(*value_24) = true;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
}
}
line_start = (int64_t)((uint64_t)line_end + UINT64_C(1));
}
bool changed = true;
while (changed) {
changed = false;
{
int64_t range_first_42 = 0LL;
int64_t range_last_42 = (int64_t)((uint64_t)count - UINT64_C(1));
int64_t index = range_first_42;
if (index <= range_last_42) for (;;) {
if (!ZIRAN_VEC_INDEX((needed).data, (needed).count, index)) {
if (index == range_last_42) {
break;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
continue;
}
RuntimeHeader* value_25 = zir_f_a87e346e42fbd6c3_RuntimeHeaderAtForeign(index);
uint64_t bits = (uint64_t)(value_25->includes);
int64_t other = 0LL;
while (other < count) {
bool value_26 = !ZIRAN_VEC_INDEX((needed).data, (needed).count, other);
if (value_26) {
uint64_t value_27 = (uint64_t)((uint64_t)((ZiShrU64(bits, (uint64_t)((uint64_t)(other)), 64))) & UINT64_C(1));
value_26 = (value_27 != 0ULL);
}
if (value_26) {
bool* value_28 = &(ZIRAN_VEC_INDEX((needed).data, (needed).count, other));
(*value_28) = true;
changed = true;
}
other = (int64_t)((uint64_t)other + UINT64_C(1));
}
if (index == range_last_42) {
break;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
}
}
}
bool ok = true;
{
int64_t range_first_58 = 0LL;
int64_t range_last_58 = (int64_t)((uint64_t)count - UINT64_C(1));
int64_t index = range_first_58;
if (index <= range_last_58) for (;;) {
if (!ZIRAN_VEC_INDEX((needed).data, (needed).count, index)) {
if (index == range_last_58) {
break;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
continue;
}
RuntimeHeader* header = zir_f_a87e346e42fbd6c3_RuntimeHeaderAtForeign(index);
String header_name = compiler_runtime_emit_CText((uint8_t*)header->name);
if (simd != ((void *)0) && StringEqual(header_name, StringLiteral("ziran_simd_ops.h"))) {
int32_t* value_29 = &(simd->header);
(*value_29) = 1;
if (index == range_last_58) {
break;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
continue;
}
__type_838711075b90279a kept = {0};
__type_838711075b90279a* value_30 = &kept;
String text = compiler_runtime_emit_CText((uint8_t*)header->text);
bool built = false;
if (StringEqual(header_name, StringLiteral("ziran_simd_ops.h"))) {
bool value_31 = false;
if ((text).length == 0) {
value_31 = true;
} else {
void *value_32 = ZirVecReserve((void *)(kept).data, &(kept).capacity, (kept).count, (int64_t)(text).length, sizeof(*(kept).data));
if (value_32 != NULL) {
(kept).data = (uint8_t *)value_32;
memcpy((kept).data + (kept).count, (text).data, (size_t)(text).length);
(kept).count += (int64_t)(text).length;
value_31 = true;
}
}
built = value_31;
} else {
built = compiler_runtime_emit_KeptSegments(text, used, &(kept));
}
zir_8e7ef9a1e6f28a8e_Results__string__bool results_1_ = compiler_runtime_emit_JoinPath(out_dir, header_name);
String path = results_1_.value_0;
bool made = results_1_.value_1;
bool value_33 = built && made;
if (value_33) {
Slice value_34 = {(kept).data, (kept).count};
Slice value_35 = SliceRange(value_34, 0LL, kept.count, sizeof(*(kept).data));
bool value_36 = compiler_runtime_emit_WriteGenerated(path, StringView((const char *)(value_35).data, (size_t)(value_35).length));
value_33 = value_36;
}
bool wrote = value_33;
if (made) {
compiler_runtime_emit_ReleaseOwnedText(path);
}
free((kept).data);
(kept).data = NULL;
kept.capacity = 0;
kept.count = 0;
if (!wrote) {
ok = false;
free((*value_30).data);
(*value_30).data = NULL;
(*value_30).capacity = 0;
(*value_30).count = 0;
break;
}
if (index == range_last_58) {
free((*value_30).data);
(*value_30).data = NULL;
(*value_30).capacity = 0;
(*value_30).count = 0;
break;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
free((*value_30).data);
(*value_30).data = NULL;
(*value_30).capacity = 0;
(*value_30).count = 0;
}
}
free((needed).data);
(needed).data = NULL;
needed.capacity = 0;
needed.count = 0;
bool cleanup_return_1 = ok;
{
free((generated_bytes).data);
(generated_bytes).data = NULL;
generated_bytes.capacity = 0;
generated_bytes.count = 0;
}
bool value_37 = cleanup_return_1;
free((*value_6).data);
(*value_6).data = NULL;
(*value_6).capacity = 0;
(*value_6).count = 0;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_37;
}
bool
compiler_runtime_emit_EmitSimdOps(String out_dir, ZirSimdUses* uses)
{
RuntimeHeader* full_header = (void *)0;
int64_t header_count = zir_f_a87e346e42fbd6c3_RuntimeHeaderCountForeign();
int64_t index = 0LL;
while (index < header_count) {
RuntimeHeader* header = zir_f_a87e346e42fbd6c3_RuntimeHeaderAtForeign(index);
if (StringEqual(compiler_runtime_emit_CText((uint8_t*)header->name), StringLiteral("ziran_simd_ops.h"))) {
full_header = header;
}
index = (int64_t)((uint64_t)index + UINT64_C(1));
}
if (full_header == ((void *)0)) {
return false;
}
String full = compiler_runtime_emit_CText((uint8_t*)full_header->text);
SimdNames helpers = {0};
__type_2f893a38d978b828 segments = {0};
__type_2f893a38d978b828* value_2 = &segments;
__type_1f00ef809eed7c2e last = {0};
__type_1f00ef809eed7c2e* value_3 = &last;
__type_1f00ef809eed7c2e first = {0};
__type_1f00ef809eed7c2e* value_4 = &first;
__type_1f00ef809eed7c2e needed = {0};
__type_1f00ef809eed7c2e* value_5 = &needed;
__type_1f00ef809eed7c2e work = {0};
__type_1f00ef809eed7c2e* value_6 = &work;
__type_838711075b90279a output = {0};
__type_838711075b90279a* value_7 = &output;
int64_t common_end = -1LL;
int64_t at = 0LL;
while (at < (int64_t)(full).length) {
int64_t newline = at;
while (newline < (int64_t)(full).length && (uint8_t)ZIRAN_INDEX(full.data, full.length, newline) != 10) {
newline = (int64_t)((uint64_t)newline + UINT64_C(1));
}
int64_t value_8 = 0;
if (newline < (int64_t)(full).length) {
value_8 = (int64_t)((uint64_t)newline + UINT64_C(1));
} else {
value_8 = (int64_t)(full).length;
}
int64_t after = value_8;
int64_t length = (int64_t)((uint64_t)after - (uint64_t)at);
bool value_9 = length >= 6LL && (uint8_t)ZIRAN_INDEX(full.data, full.length, at) == 47 && (uint8_t)ZIRAN_INDEX(full.data, full.length, ((int64_t)((uint64_t)at + UINT64_C(1)))) == 42;
bool value_10 = value_9 && (uint8_t)ZIRAN_INDEX(full.data, full.length, ((int64_t)((uint64_t)at + UINT64_C(2)))) == 64;
bool value_11 = value_10 && newline < (int64_t)(full).length && (uint8_t)ZIRAN_INDEX(full.data, full.length, ((int64_t)((uint64_t)newline - UINT64_C(1)))) == 47;
bool marker = value_11 && (uint8_t)ZIRAN_INDEX(full.data, full.length, ((int64_t)((uint64_t)newline - UINT64_C(2)))) == 42;
if (marker) {
if (common_end < 0LL) {
common_end = at;
}
int64_t name_start = (int64_t)((uint64_t)at + UINT64_C(3));
int64_t name_length = (int64_t)((uint64_t)length - UINT64_C(6));
int64_t helper = -1LL;
if (name_length > 0LL) {
String value_12 = StringRange(full, (int64_t)name_start, (int64_t)((int64_t)((uint64_t)name_start + (uint64_t)name_length)));
uint8_t* value_13 = (void *)0;
{
String value = value_12;
if ((int64_t)(value).length == 0LL) {
value_13 = (void *)0;
goto value_14;
}
TextDescriptor* descriptor = (TextDescriptor*)(&(value));
value_13 = descriptor->data;
value_14: ;
}
helper = zir_f_a87e346e42fbd6c3_SimdNamesFind(&(helpers), value_13, (size_t)(name_length), 1);
}
Segment value_15 = (Segment){.helper = helper, .start = after, .length = 0LL, .next = 0LL};
bool value_16 = false;
void *value_17 = ZirVecGrow((void *)(segments).data, &(segments).capacity, (segments).count, sizeof(*(segments).data));
if (value_17 != NULL) {
(segments).data = (Segment *)value_17;
(segments).data[(segments).count++] = value_15;
value_16 = true;
}
if (!value_16) {
bool value_18 = compiler_runtime_emit_Cleanup(&(helpers), &(segments), &(last), &(first), &(needed), &(work), &(output), false);
free((*value_7).data);
(*value_7).data = NULL;
(*value_7).capacity = 0;
(*value_7).count = 0;
free((*value_6).data);
(*value_6).data = NULL;
(*value_6).capacity = 0;
(*value_6).count = 0;
free((*value_5).data);
(*value_5).data = NULL;
(*value_5).capacity = 0;
(*value_5).count = 0;
free((*value_4).data);
(*value_4).data = NULL;
(*value_4).capacity = 0;
(*value_4).count = 0;
free((*value_3).data);
(*value_3).data = NULL;
(*value_3).capacity = 0;
(*value_3).count = 0;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_18;
}
int64_t segment_index = (int64_t)((uint64_t)(segments.count) - UINT64_C(1));
if (helper >= 0LL) {
bool value_19 = helper >= last.count;
bool value_20 = value_19;
if (value_20) {
int64_t value_22 = (int64_t)((uint64_t)helper + UINT64_C(1));
bool value_23 = false;
if (value_22 >= 0) {
value_23 = value_22 <= (last).capacity;
if (!value_23) {
void *value_24 = ZirVecReserve((void *)(last).data, &(last).capacity, (last).count, value_22 - (last).count, sizeof(*(last).data));
if (value_24 != NULL) { (last).data = (int64_t *)value_24; value_23 = true; }
}
if (value_23) {
if (value_22 > (last).count) memset((last).data + (last).count, 0, (size_t)(value_22 - (last).count) * sizeof(*(last).data));
(last).count = value_22;
}
}
value_20 = !value_23;
}
if (value_20) {
bool value_26 = compiler_runtime_emit_Cleanup(&(helpers), &(segments), &(last), &(first), &(needed), &(work), &(output), false);
free((*value_7).data);
(*value_7).data = NULL;
(*value_7).capacity = 0;
(*value_7).count = 0;
free((*value_6).data);
(*value_6).data = NULL;
(*value_6).capacity = 0;
(*value_6).count = 0;
free((*value_5).data);
(*value_5).data = NULL;
(*value_5).capacity = 0;
(*value_5).count = 0;
free((*value_4).data);
(*value_4).data = NULL;
(*value_4).capacity = 0;
(*value_4).count = 0;
free((*value_3).data);
(*value_3).data = NULL;
(*value_3).capacity = 0;
(*value_3).count = 0;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_26;
}
int64_t previous = ZIRAN_VEC_INDEX((last).data, (last).count, helper);
if (previous != 0LL) {
int64_t* value_27 = &((ZIRAN_VEC_INDEX((segments).data, (segments).count, ((int64_t)((uint64_t)previous - UINT64_C(1))))).next);
(*value_27) = (int64_t)((uint64_t)segment_index + UINT64_C(1));
}
int64_t* value_28 = &(ZIRAN_VEC_INDEX((last).data, (last).count, helper));
(*value_28) = (int64_t)((uint64_t)segment_index + UINT64_C(1));
}
} else if (segments.count > 0LL) {
int64_t* value_29 = &((ZIRAN_VEC_INDEX((segments).data, (segments).count, ((int64_t)((uint64_t)(segments.count) - UINT64_C(1))))).length);
(*value_29) = (int64_t)((uint64_t)((*value_29)) + (uint64_t)length);
}
at = after;
}
if (common_end < 0LL) {
common_end = (int64_t)(full).length;
}
int64_t value_31 = (int64_t)((uint64_t)(helpers.count) + UINT64_C(1));
bool value_32 = false;
if (value_31 >= 0) {
value_32 = value_31 <= (first).capacity;
if (!value_32) {
void *value_33 = ZirVecReserve((void *)(first).data, &(first).capacity, (first).count, value_31 - (first).count, sizeof(*(first).data));
if (value_33 != NULL) { (first).data = (int64_t *)value_33; value_32 = true; }
}
if (value_32) {
if (value_31 > (first).count) memset((first).data + (first).count, 0, (size_t)(value_31 - (first).count) * sizeof(*(first).data));
(first).count = value_31;
}
}
bool value_35 = !value_32;
if (!value_35) {
int64_t value_37 = (int64_t)((uint64_t)(helpers.count) + UINT64_C(1));
bool value_38 = false;
if (value_37 >= 0) {
value_38 = value_37 <= (needed).capacity;
if (!value_38) {
void *value_39 = ZirVecReserve((void *)(needed).data, &(needed).capacity, (needed).count, value_37 - (needed).count, sizeof(*(needed).data));
if (value_39 != NULL) { (needed).data = (int64_t *)value_39; value_38 = true; }
}
if (value_38) {
if (value_37 > (needed).count) memset((needed).data + (needed).count, 0, (size_t)(value_37 - (needed).count) * sizeof(*(needed).data));
(needed).count = value_37;
}
}
value_35 = !value_38;
}
bool value_41 = value_35;
if (!value_41) {
int64_t value_43 = (int64_t)((uint64_t)(helpers.count) + UINT64_C(1));
bool value_44 = false;
if (value_43 >= 0) {
value_44 = value_43 <= (work).capacity;
if (!value_44) {
void *value_45 = ZirVecReserve((void *)(work).data, &(work).capacity, (work).count, value_43 - (work).count, sizeof(*(work).data));
if (value_45 != NULL) { (work).data = (int64_t *)value_45; value_44 = true; }
}
if (value_44) {
if (value_43 > (work).count) memset((work).data + (work).count, 0, (size_t)(value_43 - (work).count) * sizeof(*(work).data));
(work).count = value_43;
}
}
value_41 = !value_44;
}
bool value_47 = value_41;
if (!value_47) {
bool value_48 = false;
if ((StringRange(full, (int64_t)0LL, (int64_t)common_end)).length == 0) {
value_48 = true;
} else {
void *value_49 = ZirVecReserve((void *)(output).data, &(output).capacity, (output).count, (int64_t)(StringRange(full, (int64_t)0LL, (int64_t)common_end)).length, sizeof(*(output).data));
if (value_49 != NULL) {
(output).data = (uint8_t *)value_49;
memcpy((output).data + (output).count, (StringRange(full, (int64_t)0LL, (int64_t)common_end)).data, (size_t)(StringRange(full, (int64_t)0LL, (int64_t)common_end)).length);
(output).count += (int64_t)(StringRange(full, (int64_t)0LL, (int64_t)common_end)).length;
value_48 = true;
}
}
value_47 = !value_48;
}
if (value_47) {
bool value_50 = compiler_runtime_emit_Cleanup(&(helpers), &(segments), &(last), &(first), &(needed), &(work), &(output), false);
free((*value_7).data);
(*value_7).data = NULL;
(*value_7).capacity = 0;
(*value_7).count = 0;
free((*value_6).data);
(*value_6).data = NULL;
(*value_6).capacity = 0;
(*value_6).count = 0;
free((*value_5).data);
(*value_5).data = NULL;
(*value_5).capacity = 0;
(*value_5).count = 0;
free((*value_4).data);
(*value_4).data = NULL;
(*value_4).capacity = 0;
(*value_4).count = 0;
free((*value_3).data);
(*value_3).data = NULL;
(*value_3).capacity = 0;
(*value_3).count = 0;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_50;
}
index = (int64_t)((uint64_t)(segments.count) - UINT64_C(1));
while (index >= 0LL) {
int64_t value_51 = (ZIRAN_VEC_INDEX((segments).data, (segments).count, index)).helper;
if (value_51 >= 0LL) {
int64_t* value_52 = &(ZIRAN_VEC_INDEX((first).data, (first).count, (ZIRAN_VEC_INDEX((segments).data, (segments).count, index)).helper));
(*value_52) = (int64_t)((uint64_t)index + UINT64_C(1));
}
index = (int64_t)((uint64_t)index - UINT64_C(1));
}
{
int64_t range_first_74 = 0LL;
int64_t range_last_74 = (int64_t)((uint64_t)(uses->names.count) - UINT64_C(1));
int64_t use_index = range_first_74;
if (use_index <= range_last_74) for (;;) {
String name = compiler_runtime_emit_CText((uint8_t*)(uses->names.text[use_index]));
uint8_t* value_53 = (void *)0;
{
String value = name;
if ((int64_t)(value).length == 0LL) {
value_53 = (void *)0;
goto value_54;
}
TextDescriptor* descriptor = (TextDescriptor*)(&(value));
value_53 = descriptor->data;
value_54: ;
}
int64_t helper = zir_f_a87e346e42fbd6c3_SimdNamesFind(&(helpers), value_53, (size_t)((int64_t)(name).length), 0);
compiler_runtime_emit_NeedHelper(helper, &(needed), &(work));
if (use_index == range_last_74) {
break;
}
use_index = (int64_t)((uint64_t)use_index + UINT64_C(1));
}
}
int64_t queue_at = 0LL;
while (queue_at < work.count) {
int64_t helper = ZIRAN_VEC_INDEX((work).data, (work).count, queue_at);
queue_at = (int64_t)((uint64_t)queue_at + UINT64_C(1));
int64_t next = ZIRAN_VEC_INDEX((first).data, (first).count, helper);
while (next != 0LL) {
compiler_runtime_emit_NeedCalls(&(helpers), ZIRAN_VEC_INDEX((segments).data, (segments).count, ((int64_t)((uint64_t)next - UINT64_C(1)))), full, &(needed), &(work));
next = (ZIRAN_VEC_INDEX((segments).data, (segments).count, ((int64_t)((uint64_t)next - UINT64_C(1))))).next;
}
}
{
int64_t range_first_89 = 0LL;
int64_t range_last_89 = (int64_t)((uint64_t)(segments.count) - UINT64_C(1));
int64_t segment_index = range_first_89;
if (segment_index <= range_last_89) for (;;) {
Segment segment = ZIRAN_VEC_INDEX((segments).data, (segments).count, segment_index);
bool value_55 = segment.helper < 0LL;
if (!value_55) {
int64_t value_56 = ZIRAN_VEC_INDEX((needed).data, (needed).count, segment.helper);
value_55 = (value_56 == 1LL);
}
if (value_55) {
String value_57 = StringRange(full, (int64_t)segment.start, (int64_t)((int64_t)((uint64_t)(segment.start) + (uint64_t)(segment.length))));
bool value_58 = false;
if ((value_57).length == 0) {
value_58 = true;
} else {
void *value_59 = ZirVecReserve((void *)(output).data, &(output).capacity, (output).count, (int64_t)(value_57).length, sizeof(*(output).data));
if (value_59 != NULL) {
(output).data = (uint8_t *)value_59;
memcpy((output).data + (output).count, (value_57).data, (size_t)(value_57).length);
(output).count += (int64_t)(value_57).length;
value_58 = true;
}
}
if (!value_58) {
bool value_60 = compiler_runtime_emit_Cleanup(&(helpers), &(segments), &(last), &(first), &(needed), &(work), &(output), false);
free((*value_7).data);
(*value_7).data = NULL;
(*value_7).capacity = 0;
(*value_7).count = 0;
free((*value_6).data);
(*value_6).data = NULL;
(*value_6).capacity = 0;
(*value_6).count = 0;
free((*value_5).data);
(*value_5).data = NULL;
(*value_5).capacity = 0;
(*value_5).count = 0;
free((*value_4).data);
(*value_4).data = NULL;
(*value_4).capacity = 0;
(*value_4).count = 0;
free((*value_3).data);
(*value_3).data = NULL;
(*value_3).capacity = 0;
(*value_3).count = 0;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_60;
}
}
if (segment_index == range_last_89) {
break;
}
segment_index = (int64_t)((uint64_t)segment_index + UINT64_C(1));
}
}
zir_8e7ef9a1e6f28a8e_Results__string__bool results_2_ = compiler_runtime_emit_JoinPath(out_dir, StringLiteral("ziran_simd_ops.h"));
String path = results_2_.value_0;
bool made = results_2_.value_1;
bool ok = false;
if (made) {
Slice value_61 = {(output).data, (output).count};
Slice value_62 = SliceRange(value_61, 0LL, output.count, sizeof(*(output).data));
ok = compiler_runtime_emit_WriteGenerated(path, StringView((const char *)(value_62).data, (size_t)(value_62).length));
compiler_runtime_emit_ReleaseOwnedText(path);
}
bool value_63 = compiler_runtime_emit_Cleanup(&(helpers), &(segments), &(last), &(first), &(needed), &(work), &(output), ok);
free((*value_7).data);
(*value_7).data = NULL;
(*value_7).capacity = 0;
(*value_7).count = 0;
free((*value_6).data);
(*value_6).data = NULL;
(*value_6).capacity = 0;
(*value_6).count = 0;
free((*value_5).data);
(*value_5).data = NULL;
(*value_5).capacity = 0;
(*value_5).count = 0;
free((*value_4).data);
(*value_4).data = NULL;
(*value_4).capacity = 0;
(*value_4).count = 0;
free((*value_3).data);
(*value_3).data = NULL;
(*value_3).capacity = 0;
(*value_3).count = 0;
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_63;
}
static void
compiler_runtime_emit_ReleaseOwnedText(String value)
{
if ((int64_t)(value).length == 0LL) {
return;
}
TextDescriptor* descriptor = (TextDescriptor*)(&(value));
zir_f_a87e346e42fbd6c3_FreeRaw((void*)(descriptor->data));
}
static String
compiler_runtime_emit_CText(uint8_t* value)
{
String result = StringLiteral("");
if (value == ((void *)0)) {
return result;
}
size_t length = 0;
while (value[length] != 0) {
length = length + 1;
}
TextDescriptor* descriptor = (TextDescriptor*)(&(result));
uint8_t** value_0 = &(descriptor->data);
(*value_0) = value;
size_t* value_1 = &(descriptor->length);
(*value_1) = length;
return result;
}
static uint8_t*
compiler_runtime_emit_CPointer(String value)
{
if ((int64_t)(value).length == 0LL) {
return (void *)0;
}
TextDescriptor* descriptor = (TextDescriptor*)(&(value));
return descriptor->data;
}
static bool
compiler_runtime_emit_TextHasPrefix(String value, String prefix)
{
if ((int64_t)(prefix).length > (int64_t)(value).length) {
return false;
}
return StringEqual(StringRange(value, (int64_t)0LL, (int64_t)(int64_t)(prefix).length), prefix);
}
static zir_8e7ef9a1e6f28a8e_Results__string__bool
compiler_runtime_emit_JoinPath(String directory, String name)
{
__type_838711075b90279a bytes = {0};
__type_838711075b90279a* value_2 = &bytes;
bool value_3 = false;
if ((directory).length == 0) {
value_3 = true;
} else {
void *value_4 = ZirVecReserve((void *)(bytes).data, &(bytes).capacity, (bytes).count, (int64_t)(directory).length, sizeof(*(bytes).data));
if (value_4 != NULL) {
(bytes).data = (uint8_t *)value_4;
memcpy((bytes).data + (bytes).count, (directory).data, (size_t)(directory).length);
(bytes).count += (int64_t)(directory).length;
value_3 = true;
}
}
if (!value_3) {
zir_8e7ef9a1e6f28a8e_Results__string__bool value_5 = (zir_8e7ef9a1e6f28a8e_Results__string__bool){.value_0 = StringLiteral(""), .value_1 = false};
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_5;
}
uint8_t value_6 = 47;
bool value_7 = false;
void *value_8 = ZirVecGrow((void *)(bytes).data, &(bytes).capacity, (bytes).count, sizeof(*(bytes).data));
if (value_8 != NULL) {
(bytes).data = (uint8_t *)value_8;
(bytes).data[(bytes).count++] = value_6;
value_7 = true;
}
if (!value_7) {
free((bytes).data);
(bytes).data = NULL;
bytes.capacity = 0;
bytes.count = 0;
zir_8e7ef9a1e6f28a8e_Results__string__bool value_9 = (zir_8e7ef9a1e6f28a8e_Results__string__bool){.value_0 = StringLiteral(""), .value_1 = false};
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_9;
}
bool value_10 = false;
if ((name).length == 0) {
value_10 = true;
} else {
void *value_11 = ZirVecReserve((void *)(bytes).data, &(bytes).capacity, (bytes).count, (int64_t)(name).length, sizeof(*(bytes).data));
if (value_11 != NULL) {
(bytes).data = (uint8_t *)value_11;
memcpy((bytes).data + (bytes).count, (name).data, (size_t)(name).length);
(bytes).count += (int64_t)(name).length;
value_10 = true;
}
}
if (!value_10) {
free((bytes).data);
(bytes).data = NULL;
bytes.capacity = 0;
bytes.count = 0;
zir_8e7ef9a1e6f28a8e_Results__string__bool value_12 = (zir_8e7ef9a1e6f28a8e_Results__string__bool){.value_0 = StringLiteral(""), .value_1 = false};
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_12;
}
Slice value_13 = {(bytes).data, (bytes).count};
Slice value_14 = SliceRange(value_13, 0LL, bytes.count, sizeof(*(bytes).data));
String result = vec_TextDetach(vec_TextCopy(StringView((const char *)(value_14).data, (size_t)(value_14).length)));
free((bytes).data);
(bytes).data = NULL;
bytes.capacity = 0;
bytes.count = 0;
zir_8e7ef9a1e6f28a8e_Results__string__bool value_15 = (zir_8e7ef9a1e6f28a8e_Results__string__bool){.value_0 = result, .value_1 = true};
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_15;
}
static int64_t
compiler_runtime_emit_DecimalCount(int64_t value)
{
int64_t count = 1LL;
int64_t remaining = value;
while (remaining >= 10LL) {
count = (int64_t)((uint64_t)count + UINT64_C(1));
remaining = remaining / 10LL;
}
return count;
}
static zir_8e7ef9a1e6f28a8e_Results__string__bool
compiler_runtime_emit_JoinTemporary(String path, int32_t process_id)
{
int64_t digits = compiler_runtime_emit_DecimalCount((int64_t)(process_id));
__type_838711075b90279a bytes = {0};
__type_838711075b90279a* value_2 = &bytes;
bool value_3 = false;
if ((path).length == 0) {
value_3 = true;
} else {
void *value_4 = ZirVecReserve((void *)(bytes).data, &(bytes).capacity, (bytes).count, (int64_t)(path).length, sizeof(*(bytes).data));
if (value_4 != NULL) {
(bytes).data = (uint8_t *)value_4;
memcpy((bytes).data + (bytes).count, (path).data, (size_t)(path).length);
(bytes).count += (int64_t)(path).length;
value_3 = true;
}
}
bool value_5 = !value_3;
if (!value_5) {
uint8_t value_6 = 46;
bool value_7 = false;
void *value_8 = ZirVecGrow((void *)(bytes).data, &(bytes).capacity, (bytes).count, sizeof(*(bytes).data));
if (value_8 != NULL) {
(bytes).data = (uint8_t *)value_8;
(bytes).data[(bytes).count++] = value_6;
value_7 = true;
}
value_5 = !value_7;
}
if (value_5) {
free((bytes).data);
(bytes).data = NULL;
bytes.capacity = 0;
bytes.count = 0;
zir_8e7ef9a1e6f28a8e_Results__string__bool value_9 = (zir_8e7ef9a1e6f28a8e_Results__string__bool){.value_0 = StringLiteral(""), .value_1 = false};
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_9;
}
int64_t low = bytes.count;
int64_t value_11 = (int64_t)((uint64_t)low + (uint64_t)digits);
bool value_12 = false;
if (value_11 >= 0) {
value_12 = value_11 <= (bytes).capacity;
if (!value_12) {
void *value_13 = ZirVecReserve((void *)(bytes).data, &(bytes).capacity, (bytes).count, value_11 - (bytes).count, sizeof(*(bytes).data));
if (value_13 != NULL) { (bytes).data = (uint8_t *)value_13; value_12 = true; }
}
if (value_12) {
if (value_11 > (bytes).count) memset((bytes).data + (bytes).count, 0, (size_t)(value_11 - (bytes).count) * sizeof(*(bytes).data));
(bytes).count = value_11;
}
}
if (!value_12) {
free((bytes).data);
(bytes).data = NULL;
bytes.capacity = 0;
bytes.count = 0;
zir_8e7ef9a1e6f28a8e_Results__string__bool value_15 = (zir_8e7ef9a1e6f28a8e_Results__string__bool){.value_0 = StringLiteral(""), .value_1 = false};
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_15;
}
int64_t at = (int64_t)((uint64_t)(bytes.count) - UINT64_C(1));
int32_t remaining = process_id;
while (true) {
uint8_t* value_16 = &(ZIRAN_VEC_INDEX((bytes).data, (bytes).count, at));
(*value_16) = (uint8_t)(48u + (uint32_t)((remaining % 10)));
at = (int64_t)((uint64_t)at - UINT64_C(1));
remaining = remaining / 10;
if (remaining == 0) {
break;
}
}
bool value_17 = false;
if ((StringLiteral(".tmp")).length == 0) {
value_17 = true;
} else {
void *value_18 = ZirVecReserve((void *)(bytes).data, &(bytes).capacity, (bytes).count, (int64_t)(StringLiteral(".tmp")).length, sizeof(*(bytes).data));
if (value_18 != NULL) {
(bytes).data = (uint8_t *)value_18;
memcpy((bytes).data + (bytes).count, (StringLiteral(".tmp")).data, (size_t)(StringLiteral(".tmp")).length);
(bytes).count += (int64_t)(StringLiteral(".tmp")).length;
value_17 = true;
}
}
if (!value_17) {
free((bytes).data);
(bytes).data = NULL;
bytes.capacity = 0;
bytes.count = 0;
zir_8e7ef9a1e6f28a8e_Results__string__bool value_19 = (zir_8e7ef9a1e6f28a8e_Results__string__bool){.value_0 = StringLiteral(""), .value_1 = false};
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_19;
}
Slice value_20 = {(bytes).data, (bytes).count};
Slice value_21 = SliceRange(value_20, 0LL, bytes.count, sizeof(*(bytes).data));
String result = vec_TextDetach(vec_TextCopy(StringView((const char *)(value_21).data, (size_t)(value_21).length)));
free((bytes).data);
(bytes).data = NULL;
bytes.capacity = 0;
bytes.count = 0;
zir_8e7ef9a1e6f28a8e_Results__string__bool value_22 = (zir_8e7ef9a1e6f28a8e_Results__string__bool){.value_0 = result, .value_1 = true};
free((*value_2).data);
(*value_2).data = NULL;
(*value_2).capacity = 0;
(*value_2).count = 0;
return value_22;
}
static bool
compiler_runtime_emit_SameBytes(String left, String right)
{
return (int64_t)(left).length == (int64_t)(right).length && ((int64_t)(left).length == 0LL || StringEqual(StringRange(left, (int64_t)0LL, (int64_t)(int64_t)(left).length), StringRange(right, (int64_t)0LL, (int64_t)(int64_t)(right).length)));
}
static bool
compiler_runtime_emit_WriteGenerated(String path, String text)
{
bool value_2 = compiler_runtime_emit_RecordPath(path);
if (!value_2) {
return false;
}
__type_838711075b90279a current = {0};
__type_838711075b90279a* value_3 = &current;
bool readable = file_ReadFileInto(path, &(current));
bool value_4 = readable;
if (value_4) {
Slice value_5 = {(current).data, (current).count};
Slice value_6 = SliceRange(value_5, 0LL, current.count, sizeof(*(current).data));
String value_7 = StringView((const char *)(value_6).data, (size_t)(value_6).length);
bool value_8 = false;
{
String left = value_7;
String right = text;
value_8 = (int64_t)(left).length == (int64_t)(right).length && ((int64_t)(left).length == 0LL || StringEqual(StringRange(left, (int64_t)0LL, (int64_t)(int64_t)(left).length), StringRange(right, (int64_t)0LL, (int64_t)(int64_t)(right).length)));
}
value_4 = value_8;
}
bool unchanged = value_4;
free((current).data);
(current).data = NULL;
current.capacity = 0;
current.count = 0;
if (unchanged) {
bool value_10 = true;
free((*value_3).data);
(*value_3).data = NULL;
(*value_3).capacity = 0;
(*value_3).count = 0;
return value_10;
}
zir_8e7ef9a1e6f28a8e_Results__string__bool results_3_ = compiler_runtime_emit_JoinTemporary(path, zir_f_a87e346e42fbd6c3_ProcessId());
String temporary = results_3_.value_0;
bool made = results_3_.value_1;
if (!made) {
bool value_11 = false;
free((*value_3).data);
(*value_3).data = NULL;
(*value_3).capacity = 0;
(*value_3).count = 0;
return value_11;
}
String temporary_name = vec_TextDetach(vec_TextCopy(temporary));
compiler_runtime_emit_ReleaseOwnedText(temporary);
FileHandle file = OpenNew(temporary_name);
bool written = false;
bool closed = false;
if (file.valid) {
int64_t count = WriteAt(file, compiler_runtime_emit_BytesFromText(text), 0LL);
written = count == (int64_t)(text).length;
closed = CloseFile(file);
}
bool moved = written && closed && RenamePath(temporary_name, path);
if (!moved) {
RemoveFile(temporary_name);
}
compiler_runtime_emit_ReleaseOwnedText(temporary_name);
if (!moved) {
bool value_12 = false;
free((*value_3).data);
(*value_3).data = NULL;
(*value_3).capacity = 0;
(*value_3).count = 0;
return value_12;
}
bool value_13 = true;
free((*value_3).data);
(*value_3).data = NULL;
(*value_3).capacity = 0;
(*value_3).count = 0;
return value_13;
}
static Slice
compiler_runtime_emit_BytesFromText(String value)
{
Slice result = {0};
ByteDescriptor* descriptor = (ByteDescriptor*)(&(result));
uint8_t** value_0 = &(descriptor->data);
String value_1 = value;
uint8_t* value_2 = (void *)0;
{
String value = value_1;
if ((int64_t)(value).length == 0LL) {
value_2 = (void *)0;
goto value_3;
}
TextDescriptor* descriptor = (TextDescriptor*)(&(value));
value_2 = descriptor->data;
value_3: ;
}
(*value_0) = value_2;
int64_t* value_4 = &(descriptor->length);
(*value_4) = (int64_t)(value).length;
return result;
}
static bool
compiler_runtime_emit_WordByte(uint8_t value)
{
bool lower = value >= 97 && value <= 122;
bool upper = value >= 65 && value <= 90;
bool digit = value >= 48 && value <= 57;
return lower || upper || digit || value == 95;
}
static bool
compiler_runtime_emit_KeptSegments(String text, SimdNames* used, __type_838711075b90279a* output)
{
bool keep = true;
int64_t at = 0LL;
while (at < (int64_t)(text).length) {
int64_t newline = at;
while (newline < (int64_t)(text).length && (uint8_t)ZIRAN_INDEX(text.data, text.length, newline) != 10) {
newline = (int64_t)((uint64_t)newline + UINT64_C(1));
}
int64_t value_0 = 0;
if (newline < (int64_t)(text).length) {
value_0 = (int64_t)((uint64_t)newline + UINT64_C(1));
} else {
value_0 = (int64_t)(text).length;
}
int64_t after = value_0;
bool value_1 = (int64_t)((uint64_t)newline - (uint64_t)at) >= 5LL && (uint8_t)ZIRAN_INDEX(text.data, text.length, at) == 47;
bool value_2 = value_1 && (uint8_t)ZIRAN_INDEX(text.data, text.length, ((int64_t)((uint64_t)at + UINT64_C(1)))) == 42;
bool value_3 = value_2 && (uint8_t)ZIRAN_INDEX(text.data, text.length, ((int64_t)((uint64_t)at + UINT64_C(2)))) == 64;
bool value_4 = value_3 && (uint8_t)ZIRAN_INDEX(text.data, text.length, ((int64_t)((uint64_t)newline - UINT64_C(2)))) == 42;
bool marker = value_4 && (uint8_t)ZIRAN_INDEX(text.data, text.length, ((int64_t)((uint64_t)newline - UINT64_C(1)))) == 47;
if (marker) {
bool value_5 = used == ((void *)0);
keep = value_5 || compiler_runtime_emit_SegmentNamed(StringRange(text, (int64_t)((int64_t)((uint64_t)at + UINT64_C(3))), (int64_t)((int64_t)((uint64_t)newline - UINT64_C(2)))), used);
} else {
bool value_6 = keep;
if (value_6) {
bool value_7 = false;
if ((StringRange(text, (int64_t)at, (int64_t)after)).length == 0) {
value_7 = true;
} else {
void *value_8 = ZirVecReserve((void *)((*(output))).data, &((*(output))).capacity, ((*(output))).count, (int64_t)(StringRange(text, (int64_t)at, (int64_t)after)).length, sizeof(*((*(output))).data));
if (value_8 != NULL) {
((*(output))).data = (uint8_t *)value_8;
memcpy(((*(output))).data + ((*(output))).count, (StringRange(text, (int64_t)at, (int64_t)after)).data, (size_t)(StringRange(text, (int64_t)at, (int64_t)after)).length);
((*(output))).count += (int64_t)(StringRange(text, (int64_t)at, (int64_t)after)).length;
value_7 = true;
}
}
value_6 = !value_7;
}
if (value_6) {
return false;
}
}
at = after;
}
return true;
}
static bool
compiler_runtime_emit_SegmentNamed(String names, SimdNames* used)
{
if ((int64_t)(names).length == 0LL) {
return true;
}
int64_t start = 0LL;
while (start < (int64_t)(names).length) {
while (start < (int64_t)(names).length && (uint8_t)ZIRAN_INDEX(names.data, names.length, start) == 32) {
start = (int64_t)((uint64_t)start + UINT64_C(1));
}
int64_t end = start;
while (end < (int64_t)(names).length && (uint8_t)ZIRAN_INDEX(names.data, names.length, end) != 32) {
end = (int64_t)((uint64_t)end + UINT64_C(1));
}
bool value_0 = end > start;
if (value_0) {
String value_1 = StringRange(names, (int64_t)start, (int64_t)end);
uint8_t* value_2 = (void *)0;
{
String value = value_1;
if ((int64_t)(value).length == 0LL) {
value_2 = (void *)0;
goto value_3;
}
TextDescriptor* descriptor = (TextDescriptor*)(&(value));
value_2 = descriptor->data;
value_3: ;
}
int64_t value_4 = zir_f_a87e346e42fbd6c3_SimdNamesFind(used, value_2, (size_t)(((int64_t)((uint64_t)end - (uint64_t)start))), 0);
value_0 = (value_4 >= 0LL);
}
if (value_0) {
return true;
}
start = end;
}
return false;
}
static void
compiler_runtime_emit_NeedHelper(int64_t helper, __type_1f00ef809eed7c2e* needed, __type_1f00ef809eed7c2e* work)
{
bool value_0 = helper < 0LL;
if (!value_0) {
int64_t value_1 = ZIRAN_VEC_INDEX((*(needed)).data, (*(needed)).count, helper);
value_0 = (value_1 == 1LL);
}
if (value_0) {
return;
}
int64_t* value_2 = &(ZIRAN_VEC_INDEX(((*(needed))).data, ((*(needed))).count, helper));
(*value_2) = 1LL;
int64_t value_3 = helper;
bool value_4 = false;
void *value_5 = ZirVecGrow((void *)((*(work))).data, &((*(work))).capacity, ((*(work))).count, sizeof(*((*(work))).data));
if (value_5 != NULL) {
((*(work))).data = (int64_t *)value_5;
((*(work))).data[((*(work))).count++] = value_3;
value_4 = true;
}
(void)value_4;
}
static void
compiler_runtime_emit_NeedCalls(SimdNames* helpers, Segment segment, String full, __type_1f00ef809eed7c2e* needed, __type_1f00ef809eed7c2e* work)
{
int64_t start = segment.start;
while (start < (int64_t)((uint64_t)(segment.start) + (uint64_t)(segment.length))) {
uint8_t value_0 = (uint8_t)ZIRAN_INDEX(full.data, full.length, start);
bool value_1 = false;
{
uint8_t value = value_0;
bool lower = value >= 97 && value <= 122;
bool upper = value >= 65 && value <= 90;
bool digit = value >= 48 && value <= 57;
value_1 = lower || upper || digit || value == 95;
}
if (!value_1) {
start = (int64_t)((uint64_t)start + UINT64_C(1));
continue;
}
int64_t word_start = start;
while (true) {
bool value_3 = start < (int64_t)((uint64_t)(segment.start) + (uint64_t)(segment.length));
if (value_3) {
uint8_t value_4 = (uint8_t)ZIRAN_INDEX(full.data, full.length, start);
bool value_5 = false;
{
uint8_t value = value_4;
bool lower = value >= 97 && value <= 122;
bool upper = value >= 65 && value <= 90;
bool digit = value >= 48 && value <= 57;
value_5 = lower || upper || digit || value == 95;
}
value_3 = value_5;
}
if (!value_3) { break; }
start = (int64_t)((uint64_t)start + UINT64_C(1));
}
String word = StringRange(full, (int64_t)word_start, (int64_t)start);
bool value_7 = StringEqual(word, StringLiteral("ZI_N")) && start < (int64_t)((uint64_t)(segment.start) + (uint64_t)(segment.length));
if (value_7 && (uint8_t)ZIRAN_INDEX(full.data, full.length, start) == 40) {
int64_t name_start = (int64_t)((uint64_t)start + UINT64_C(1));
start = name_start;
while (true) {
bool value_8 = start < (int64_t)((uint64_t)(segment.start) + (uint64_t)(segment.length));
if (value_8) {
uint8_t value_9 = (uint8_t)ZIRAN_INDEX(full.data, full.length, start);
bool value_10 = false;
{
uint8_t value = value_9;
bool lower = value >= 97 && value <= 122;
bool upper = value >= 65 && value <= 90;
bool digit = value >= 48 && value <= 57;
value_10 = lower || upper || digit || value == 95;
}
value_8 = value_10;
}
if (!value_8) { break; }
start = (int64_t)((uint64_t)start + UINT64_C(1));
}
String name = StringRange(full, (int64_t)name_start, (int64_t)start);
if ((int64_t)(name).length > 3LL && StringEqual(StringRange(name, (int64_t)((int64_t)((uint64_t)((int64_t)(name).length) - UINT64_C(3))), (int64_t)(int64_t)(name).length), StringLiteral("_p_"))) {
name = StringRange(name, (int64_t)0LL, (int64_t)((int64_t)((uint64_t)((int64_t)(name).length) - UINT64_C(3))));
}
uint8_t* value_12 = (void *)0;
{
String value = name;
if ((int64_t)(value).length == 0LL) {
value_12 = (void *)0;
goto value_13;
}
TextDescriptor* descriptor = (TextDescriptor*)(&(value));
value_12 = descriptor->data;
value_13: ;
}
int64_t helper = zir_f_a87e346e42fbd6c3_SimdNamesFind(helpers, value_12, (size_t)((int64_t)(name).length), 0);
compiler_runtime_emit_NeedHelper(helper, needed, work);
} else if ((uint8_t)ZIRAN_INDEX(word.data, word.length, 0) == 122) {
uint8_t* value_14 = (void *)0;
{
String value = word;
if ((int64_t)(value).length == 0LL) {
value_14 = (void *)0;
goto value_15;
}
TextDescriptor* descriptor = (TextDescriptor*)(&(value));
value_14 = descriptor->data;
value_15: ;
}
uint8_t* word_pointer = value_14;
size_t word_length = (size_t)((int64_t)(word).length);
if (zir_f_a87e346e42fbd6c3_SimdHelperWord(&(word_pointer), &(word_length)) == 1) {
int64_t helper = zir_f_a87e346e42fbd6c3_SimdNamesFind(helpers, word_pointer, word_length, 0);
compiler_runtime_emit_NeedHelper(helper, needed, work);
}
}
}
}
static bool
compiler_runtime_emit_Cleanup(SimdNames* helpers, __type_2f893a38d978b828* segments, __type_1f00ef809eed7c2e* last, __type_1f00ef809eed7c2e* first, __type_1f00ef809eed7c2e* needed, __type_1f00ef809eed7c2e* work, __type_838711075b90279a* output, bool result)
{
zir_f_a87e346e42fbd6c3_SimdNamesFree(helpers);
free(((*(segments))).data);
((*(segments))).data = NULL;
(*(segments)).capacity = 0;
(*(segments)).count = 0;
free(((*(last))).data);
((*(last))).data = NULL;
(*(last)).capacity = 0;
(*(last)).count = 0;
free(((*(first))).data);
((*(first))).data = NULL;
(*(first)).capacity = 0;
(*(first)).count = 0;
free(((*(needed))).data);
((*(needed))).data = NULL;
(*(needed)).capacity = 0;
(*(needed)).count = 0;
free(((*(work))).data);
((*(work))).data = NULL;
(*(work)).capacity = 0;
(*(work)).count = 0;
free(((*(output))).data);
((*(output))).data = NULL;
(*(output)).capacity = 0;
(*(output)).count = 0;
return result;
}