commit 52be0374cd86a082d3250d79d5f51d885bc2e8de
parent dd913db9e0967fcb53981fbec944dd82ea20d41b
Author: citbl <citbl@citbl.org>
Date: Sun, 12 Oct 2025 21:02:45 +1000
fix symbols lookup for string types in puts
Diffstat:
7 files changed, 30 insertions(+), 16 deletions(-)
diff --git a/ex2.ox b/ex2.ox
@@ -1,6 +1,7 @@
void main() {
- string steve = "this is the wrong name if you print(harry) and this is steve because we get the first one found!";
- string harry = "his name is harry";
+ string steve = "His name is Steeeve";
+ string harry = "His name is Harry";
+ string jezza = "His name is Jezza";
int bob = 4;
print(harry);
}
diff --git a/src/gen.h b/src/gen.h
@@ -18,5 +18,5 @@ typedef struct {
const char *src;
} Gen;
-Gen gen_init(Scope *, const char *);
+Gen gen_init(Scope *, const char *, Node*);
void gen_next(Gen *, Node *);
diff --git a/src/gen/gen.c b/src/gen/gen.c
@@ -24,7 +24,7 @@ loc_from_node(Gen *gen, Node *node)
}
Gen
-gen_init(Scope *scope, const char *src)
+gen_init(Scope *scope, const char *src, Node *node)
{
if (scope == NULL || src == NULL) { panic("gen_init: no Scope or AST provided"); }
@@ -37,8 +37,6 @@ gen_init(Scope *scope, const char *src)
// needs loc* to work
// gcc_jit_context_set_bool_option(ctx, GCC_JIT_BOOL_OPTION_DEBUGINFO, 1);
// high level
- // gcc_jit_context_set_bool_option(ctx, GCC_JIT_BOOL_OPTION_DUMP_INITIAL_TREE,
- // 1); low level
gcc_jit_context_set_bool_option(ctx, GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE, 1);
// gcc_jit_context_set_bool_option(ctx, GCC_JIT_BOOL_OPTION_DUMP_SUMMARY, 1);
@@ -56,12 +54,14 @@ gen_init(Scope *scope, const char *src)
type_void = gcc_jit_context_get_type(ctx, GCC_JIT_TYPE_VOID);
type_cstr = gcc_jit_context_get_type(ctx, GCC_JIT_TYPE_CONST_CHAR_PTR);
- gcc_jit_param *pm_puts[] = { gcc_jit_context_new_param(ctx, NULL, type_cstr, "s") };
- gcc_jit_function *fn_puts = gcc_jit_context_new_function(ctx, NULL, GCC_JIT_FUNCTION_IMPORTED, type_int, "puts", 1, pm_puts, 0);
+ gcc_jit_location *loc = gcc_jit_context_new_location(ctx, node->filename, 0, 0);
- gcc_jit_param *pm_printf[] = { gcc_jit_context_new_param(ctx, NULL, type_cstr, "fmt") };
+ gcc_jit_param *pm_puts[] = { gcc_jit_context_new_param(ctx, loc, type_cstr, "s") };
+ gcc_jit_function *fn_puts = gcc_jit_context_new_function(ctx, loc, GCC_JIT_FUNCTION_IMPORTED, type_int, "puts", 1, pm_puts, 0);
+
+ gcc_jit_param *pm_printf[] = { gcc_jit_context_new_param(ctx, loc, type_cstr, "fmt") };
gcc_jit_function *fn_printf = gcc_jit_context_new_function(ctx,
- NULL,
+ loc,
GCC_JIT_FUNCTION_IMPORTED,
type_int,
"printf",
@@ -88,12 +88,23 @@ handle_ident_call(Gen *gen, Node *node)
// Look up the symbol in the current scope
for (size_t i = 0; i < gen->scope->len; i++) {
Symbol *sym = gen->scope->symbols[i];
+ const char *sym_name = span_str(gen->src, sym->name, (char[IDENTSZ]) { 0 });
+ printf("symbol %s\n", sym_name);
+
+ const char *ident_name = span_str(gen->src, node->data.ident.name, (char[IDENTSZ]) { 0 });
+ printf("ident %s\n", ident_name);
+
+ printf("comparing: %s vs. %s\n", sym_name, ident_name);
+ if (strncmp(sym_name, ident_name, strlen(sym_name)) == 0) {
+ printf("found %s\n", sym_name);
+ return gcc_jit_lvalue_as_rvalue(sym->d.lvalue);
+ }
// TODO @next right now we get the first one, which isn't good.
// we need to get the right one by name
// need to research what the best way is to store that and use it, right now, symbols are a mess
// and their relation between each other and the rest of the compiler is a mess.
- return gcc_jit_lvalue_as_rvalue(sym->d.lvalue);
+ // return gcc_jit_lvalue_as_rvalue(sym->d.lvalue);
// if (sym->name.start == node->data.var_decl.name.start && sym->name.end == node->data.var_decl.name.end) {
// gcc_jit_rvalue *p_r = gcc_jit_lvalue_as_rvalue(sym->d.lvalue);
@@ -346,7 +357,7 @@ build_func_decl(Gen *gen, Node *node)
if (gen->curr_block) {
gcc_jit_rvalue *ret_value = gcc_jit_context_new_rvalue_from_int(gen->ctx, type_int, 0);
- gcc_jit_block_end_with_return(gen->curr_block, NULL, ret_value);
+ gcc_jit_block_end_with_return(gen->curr_block, loc_from_node(gen, node), ret_value);
gen->curr_block = NULL;
}
diff --git a/src/main.c b/src/main.c
@@ -45,7 +45,7 @@ main(int argc, char* argv[])
printf("--- gen --- \n");
- Gen gen = gen_init(&program_scope, contents);
+ Gen gen = gen_init(&program_scope, contents, ast.node);
gen_next(&gen, ast.node);
gcc_jit_result* result;
@@ -72,6 +72,7 @@ main(int argc, char* argv[])
fflush(stdout);
+ gcc_jit_context_set_bool_option(gen.ctx, GCC_JIT_BOOL_OPTION_DUMP_INITIAL_GIMPLE, 0);
gcc_jit_context_compile_to_file(gen.ctx, GCC_JIT_OUTPUT_KIND_EXECUTABLE, "out");
gcc_jit_context_release(gen.ctx);
diff --git a/src/parser.h b/src/parser.h
@@ -46,7 +46,7 @@ Node *parse_assignment(Parser *);
Node *parse_break(Parser *);
Node *parse_continue_statement(Parser *);
Node *parse_expression(Parser *);
-Node *make_program_node(void);
+Node *make_program_node(Parser *);
Node *make_ident_node(Span name);
Node *make_param_decl(Parser *);
Node *make_postfix_node(UnaryOp, Node *);
diff --git a/src/parser/expr.c b/src/parser/expr.c
@@ -151,11 +151,12 @@ parse_postfix(Parser* par)
#define STARTING_ROOT_NODES 32
Node*
-make_program_node(void)
+make_program_node(Parser* par)
{
Node* node = (Node*)calloc(1, sizeof(Node));
if (node == NULL) panic("make_program_node: alloc failed");
node->type = NODE_PROGRAM;
+ node->filename = par->filename;
node->scope = NULL;
node->next = NULL;
node->data.program.cap = STARTING_ROOT_NODES;
diff --git a/src/parser/parser.c b/src/parser/parser.c
@@ -351,7 +351,7 @@ parser_parse(Ast* ast, Parser* par)
{
assert(par->token_count > 0 && "no tokens to parse");
Node* node;
- Node* program = make_program_node();
+ Node* program = make_program_node(par);
for (;;) {
node = parse_declarations(par);
if (node == NULL) break;