ox

The Ox programming language, compiler and tools (WIP)
Log | Files | Refs | README | LICENSE

commit b730bb7bf434fb1effb00859139810c19454d6a5
parent 9d9d7dbc2e4b51cf925a78b062eb43ceab4bad2a
Author: citbl <citbl@citbl.org>
Date:   Sun, 12 Oct 2025 18:35:39 +1000

gen wip

Diffstat:
M.clangd | 9++++++++-
Mex2.ox | 4++--
Mmakefile | 4++--
Msrc/gen.h | 7+++----
Msrc/gen/gen.c | 41+++++++++++++++++++++++++++++------------
5 files changed, 44 insertions(+), 21 deletions(-)

diff --git a/.clangd b/.clangd @@ -3,8 +3,15 @@ CompileFlags: -Wall, -Wextra, -Wpedantic, + -Wshadow, + -Wpointer-arith, + -Wcast-qual, + -Wcast-align, + -Wstrict-prototypes, + -Wmissing-prototypes, + # -Wconversion, -xc, - -std=c2x, + -std=c23, -g, -I/opt/homebrew/opt/libgccjit/include, -L/opt/homebrew/opt/libgccjit/lib/gcc/current, diff --git a/ex2.ox b/ex2.ox @@ -3,6 +3,6 @@ // T add(T a, b) inline pure => a + b; void main() { - string peter = "steve"; - print(peter); + string steve = "his name is steeeeeve"; + print(steve); } diff --git a/makefile b/makefile @@ -19,10 +19,10 @@ endif SRC = */*/*.c */*.c BIN = oxc -STD = -std=c2x +STD = -std=c23 default: - cc ${STD} -g -Wall -Wextra -Wno-unused-parameter -Wno-unused-function -o ${BIN} ${SRC} ${LIB} # -Wpedantic -Wshadow -Wconversion + cc ${STD} -g -Wall -Wextra -Wno-unused-parameter -Wno-unused-function -fsanitize=address -fsanitize=undefined -o ${BIN} ${SRC} ${LIB} # -Wpedantic -Wshadow -Wconversion clean: rm -rf ${BIN} ${BIN}.* err.log diff --git a/src/gen.h b/src/gen.h @@ -3,15 +3,14 @@ #include <unistd.h> // for libgccjit #include <libgccjit.h> -#include "parser.h" -#include "sem.h" +#include "types.h" typedef struct { gcc_jit_context *ctx; gcc_jit_function *prev_func; gcc_jit_function *curr_func; - gcc_jit_function* printf_fn; - gcc_jit_function* puts_fn; + gcc_jit_function *printf_fn; + gcc_jit_function *puts_fn; gcc_jit_block *prev_block; gcc_jit_block *curr_block; // gcc_jit_type *type_kind; need type too? diff --git a/src/gen/gen.c b/src/gen/gen.c @@ -1,4 +1,5 @@ #include "../gen.h" +#include "../parser.h" #include "../utils.h" #include <stdint.h> @@ -15,7 +16,7 @@ static gcc_jit_type *type_cstr; #define MAXARGS 16 -gcc_jit_location * +static gcc_jit_location * loc_from_node(Gen *gen, Node *node) { if (node->filename == NULL) return NULL; @@ -118,7 +119,7 @@ lower_builtin_print(Gen *gen, Node *node) if (argc == 1) { gcc_jit_rvalue *arg = handle_expr(gen, node->data.call_expr.args[0]); // TODO [0] when many // cast common cases to const char* - if (gcc_jit_rvalue_get_type(arg) != type_cstr) arg = gcc_jit_context_new_cast(gen->ctx, loc_from_node(gen, node), arg, type_cstr); + // if (gcc_jit_rvalue_get_type(arg) != type_cstr) arg = gcc_jit_context_new_cast(gen->ctx, loc_from_node(gen, node), arg, type_cstr); gcc_jit_rvalue *args[] = { arg }; return gcc_jit_context_new_call(gen->ctx, loc_from_node(gen, node), gen->puts_fn, 1, args); } @@ -179,7 +180,7 @@ lower_builtin_print(Gen *gen, Node *node) // // TODO see todo below about linked list parameters... // } -void +static void lookup_symbol(Gen *gen) { // @next @@ -209,6 +210,23 @@ handle_func_call(Gen *gen, Node *node) } static gcc_jit_rvalue * +handle_ident_call(Gen *gen, Node *node) +{ + for (size_t i = 0; i < gen->scope->len; i++) { + Symbol *sym = gen->scope->symbols[i]; + 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); + // dereference pointer -> lvalue of the pointee: + gcc_jit_lvalue *pointee_lv = gcc_jit_rvalue_dereference(p_r, NULL); + // read the pointee as an rvalue (e.g., for arithmetic): + gcc_jit_rvalue *pointee_r = gcc_jit_lvalue_as_rvalue(pointee_lv); + return pointee_r; + } + } + return NULL; +} + +static gcc_jit_rvalue * handle_expr(Gen *gen, Node *node) { switch (node->type) { @@ -218,9 +236,13 @@ handle_expr(Gen *gen, Node *node) case NODE_STRING_LITERAL: return emit_literal_string(gen, node); break; - case NODE_CALL_EXPR: { + case NODE_CALL_EXPR: return handle_func_call(gen, node); - } break; + break; + case NODE_IDENT: { + return handle_ident_call(gen, node); + break; + } // case NODE_IDENT: { // return NULL; // fixme // } break; @@ -263,16 +285,11 @@ build_statement(Gen *gen, Node *node) gcc_jit_rvalue *rvalue = handle_expr(gen, node->data.var_decl.init); gcc_jit_block_add_assignment(gen->curr_block, loc, var_decl, rvalue); - printf("add the lvalue to node scope to be found later\n"); - - for (size_t i = 0; i < node->scope->len; i++) { - Symbol *sym = node->scope->symbols[i]; + for (size_t i = 0; i < gen->scope->len; i++) { + Symbol *sym = gen->scope->symbols[i]; if (sym->name.start == node->data.var_decl.name.start && sym->name.end == node->data.var_decl.name.end) { sym->ctype = declared_type; sym->d.lvalue = var_decl; - - printf("@next, when we parse the print(x) we know we can find the x in the symbols \n"); - break; } }