diff options
| author | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-22 22:40:43 +0000 |
|---|---|---|
| committer | Zuhaitz Méndez Fernández de Aránguiz <zuhaitz@debian> | 2026-01-22 22:40:43 +0000 |
| commit | ed4bbfd8cf4a72fdf4a5d6cba94d537cab340356 (patch) | |
| tree | 80f520cb36f25a210537b477007b0ac5a24e4b8e /src/codegen/codegen_decl.c | |
| parent | 3d1840e8690bef6e58a208d9ca33857a59a2e852 (diff) | |
Removing some duplicates and dissecting codegen/parser.
Diffstat (limited to 'src/codegen/codegen_decl.c')
| -rw-r--r-- | src/codegen/codegen_decl.c | 90 |
1 files changed, 40 insertions, 50 deletions
diff --git a/src/codegen/codegen_decl.c b/src/codegen/codegen_decl.c index e7bd3f1..4e6db6a 100644 --- a/src/codegen/codegen_decl.c +++ b/src/codegen/codegen_decl.c @@ -7,42 +7,44 @@ #include <stdlib.h> #include <string.h> -// Emit C preamble with standard includes and type definitions. +static void emit_freestanding_preamble(FILE *out) +{ + fputs("#include <stddef.h>\n#include <stdint.h>\n#include " + "<stdbool.h>\n#include <stdarg.h>\n", + out); + fputs("#ifdef __TINYC__\n#define __auto_type __typeof__\n#endif\n", out); + fputs("typedef size_t usize;\ntypedef char* string;\n", out); + fputs("#define U0 void\n#define I8 int8_t\n#define U8 uint8_t\n#define I16 " + "int16_t\n#define U16 uint16_t\n", + out); + fputs("#define I32 int32_t\n#define U32 uint32_t\n#define I64 " + "int64_t\n#define U64 " + "uint64_t\n", + out); + fputs("#define F32 float\n#define F64 double\n", out); + fputs("#define _z_str(x) _Generic((x), _Bool: \"%d\", char: \"%c\", " + "signed char: \"%c\", unsigned char: \"%u\", short: \"%d\", " + "unsigned short: \"%u\", int: \"%d\", unsigned int: \"%u\", " + "long: \"%ld\", unsigned long: \"%lu\", long long: \"%lld\", " + "unsigned long long: \"%llu\", float: \"%f\", double: \"%f\", " + "char*: \"%s\", void*: \"%p\")\n", + out); + fputs("typedef struct { void *func; void *ctx; } z_closure_T;\n", out); + + fputs("__attribute__((weak)) void* z_malloc(usize sz) { return NULL; }\n", out); + fputs("__attribute__((weak)) void* z_realloc(void* ptr, usize sz) { return " + "NULL; }\n", + out); + fputs("__attribute__((weak)) void z_free(void* ptr) { }\n", out); + fputs("__attribute__((weak)) void z_print(const char* fmt, ...) { }\n", out); + fputs("__attribute__((weak)) void z_panic(const char* msg) { while(1); }\n", out); +} + void emit_preamble(ParserContext *ctx, FILE *out) { if (g_config.is_freestanding) { - // Freestanding preamble. - // It actually needs more work, but yk. - fputs("#include <stddef.h>\n#include <stdint.h>\n#include " - "<stdbool.h>\n#include <stdarg.h>\n", - out); - fputs("#ifdef __TINYC__\n#define __auto_type __typeof__\n#endif\n", out); - fputs("typedef size_t usize;\ntypedef char* string;\n", out); - fputs("#define U0 void\n#define I8 int8_t\n#define U8 uint8_t\n#define I16 " - "int16_t\n#define U16 uint16_t\n", - out); - fputs("#define I32 int32_t\n#define U32 uint32_t\n#define I64 " - "int64_t\n#define U64 " - "uint64_t\n", - out); - fputs("#define F32 float\n#define F64 double\n", out); - fputs("#define _z_str(x) _Generic((x), _Bool: \"%d\", char: \"%c\", " - "signed char: \"%c\", unsigned char: \"%u\", short: \"%d\", " - "unsigned short: \"%u\", int: \"%d\", unsigned int: \"%u\", " - "long: \"%ld\", unsigned long: \"%lu\", long long: \"%lld\", " - "unsigned long long: \"%llu\", float: \"%f\", double: \"%f\", " - "char*: \"%s\", void*: \"%p\")\n", - out); - fputs("typedef struct { void *func; void *ctx; } z_closure_T;\n", out); - - fputs("__attribute__((weak)) void* z_malloc(usize sz) { return NULL; }\n", out); - fputs("__attribute__((weak)) void* z_realloc(void* ptr, usize sz) { return " - "NULL; }\n", - out); - fputs("__attribute__((weak)) void z_free(void* ptr) { }\n", out); - fputs("__attribute__((weak)) void z_print(const char* fmt, ...) { }\n", out); - fputs("__attribute__((weak)) void z_panic(const char* msg) { while(1); }\n", out); + emit_freestanding_preamble(out); } else { @@ -705,13 +707,9 @@ void emit_protos(ASTNode *node, FILE *out) } else { - char *lt = strchr(sname, '<'); - if (lt) + char *buf = strip_template_suffix(sname); + if (buf) { - int len = lt - sname; - char *buf = xmalloc(len + 1); - strncpy(buf, sname, len); - buf[len] = 0; def = find_struct_def_codegen(g_parser_ctx, buf); if (def && def->strct.is_template) { @@ -782,13 +780,9 @@ void emit_protos(ASTNode *node, FILE *out) } else { - char *lt = strchr(sname, '<'); - if (lt) + char *buf = strip_template_suffix(sname); + if (buf) { - int len = lt - sname; - char *buf = xmalloc(len + 1); - strncpy(buf, sname, len); - buf[len] = 0; def = find_struct_def_codegen(g_parser_ctx, buf); if (def && def->strct.is_template) { @@ -896,13 +890,9 @@ void emit_impl_vtables(ParserContext *ctx, FILE *out) } else { - char *lt = strchr(strct, '<'); - if (lt) + char *buf = strip_template_suffix(strct); + if (buf) { - int len = lt - strct; - char *buf = xmalloc(len + 1); - strncpy(buf, strct, len); - buf[len] = 0; def = find_struct_def_codegen(ctx, buf); if (def && def->strct.is_template) { |
