summaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
authorZuhaitz <zuhaitz.zechhub@gmail.com>2026-01-13 14:50:43 +0000
committerGitHub <noreply@github.com>2026-01-13 14:50:43 +0000
commit7eb91d69beb19da7a7cb7ac84625f701890ce6eb (patch)
tree4b86f4ad20a66b0e984feb4d5a2abaf6eaa1227f /src/codegen
parent8fc9c88304fde65d6c3a619c3aee7e6e00540695 (diff)
parent856267f36e60211ba537d63a80c5d36aa4436a4f (diff)
Merge branch 'main' into pr-34-testing
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/codegen.h2
-rw-r--r--src/codegen/codegen_decl.c9
-rw-r--r--src/codegen/codegen_main.c2
3 files changed, 8 insertions, 5 deletions
diff --git a/src/codegen/codegen.h b/src/codegen/codegen.h
index d489fb3..4b66f99 100644
--- a/src/codegen/codegen.h
+++ b/src/codegen/codegen.h
@@ -24,7 +24,7 @@ const char *parse_original_method_name(const char *mangled);
void emit_auto_type(ParserContext *ctx, ASTNode *init_expr, Token t, FILE *out);
// Declaration emission (codegen_decl.c).
-void emit_preamble(FILE *out);
+void emit_preamble(ParserContext *ctx, FILE *out);
void emit_includes_and_aliases(ASTNode *node, FILE *out);
void emit_struct_defs(ParserContext *ctx, ASTNode *node, FILE *out);
void emit_trait_defs(ASTNode *node, FILE *out);
diff --git a/src/codegen/codegen_decl.c b/src/codegen/codegen_decl.c
index ac4ae14..4979236 100644
--- a/src/codegen/codegen_decl.c
+++ b/src/codegen/codegen_decl.c
@@ -8,7 +8,7 @@
#include <string.h>
// Emit C preamble with standard includes and type definitions.
-void emit_preamble(FILE *out)
+void emit_preamble(ParserContext *ctx, FILE *out)
{
if (g_config.is_freestanding)
{
@@ -54,8 +54,11 @@ void emit_preamble(FILE *out)
fputs("#include <unistd.h>\n#include <fcntl.h>\n", out); // POSIX functions
fputs("#ifdef __TINYC__\n#define __auto_type __typeof__\n#endif\n", out);
fputs("typedef size_t usize;\ntypedef char* string;\n", out);
- fputs("#include <pthread.h>\n", out);
- fputs("typedef struct { pthread_t thread; void *result; } Async;\n", out);
+ if (ctx->has_async)
+ {
+ fputs("#include <pthread.h>\n", out);
+ fputs("typedef struct { pthread_t thread; void *result; } Async;\n", out);
+ }
fputs("typedef struct { void *func; void *ctx; } z_closure_T;\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",
diff --git a/src/codegen/codegen_main.c b/src/codegen/codegen_main.c
index c9c69f6..3634af3 100644
--- a/src/codegen/codegen_main.c
+++ b/src/codegen/codegen_main.c
@@ -202,7 +202,7 @@ void codegen_node(ParserContext *ctx, ASTNode *node, FILE *out)
if (!ctx->skip_preamble)
{
- emit_preamble(out);
+ emit_preamble(ctx, out);
}
emit_includes_and_aliases(kids, out);