From ae9aa78c61eec793f0261f22d20e496a96a76c82 Mon Sep 17 00:00:00 2001 From: "Michael D. Lowis" Date: Sun, 11 Dec 2022 23:19:08 -0500 Subject: [PATCH] added runtime module. has special handling for now that should be removed eventually --- cerise/backend/ssa/codegen.c | 4 +-- cerise/inc/cerise.h | 4 --- cerise/src/grammar.c | 56 ++++++++++++++++++++++++++++++------ cerise/src/sym.c | 11 +------ 4 files changed, 50 insertions(+), 25 deletions(-) diff --git a/cerise/backend/ssa/codegen.c b/cerise/backend/ssa/codegen.c index 4099bc5..8408d17 100644 --- a/cerise/backend/ssa/codegen.c +++ b/cerise/backend/ssa/codegen.c @@ -555,13 +555,13 @@ void codegen_const(Parser* p, SsaNode* expr) void codegen_main(Parser* p) { if (!p->genmain) return; // Bail if no main required - for (size_t i = 0; i < p->nmods; i++) + for (size_t i = 1; i < p->nmods; i++) { fout(p, "declare void @%s()\n", p->mods[i]); } fout(p, "define i32 @main(i32 %%0, i8* %%1)\n{\n"); - for (size_t i = 0; i < p->nmods; i++) + for (size_t i = 1; i < p->nmods; i++) { fout(p, " call void @%s()\n", p->mods[i]); } diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index 192d725..784933f 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -237,10 +237,6 @@ typedef struct { size_t nsyms; Symbol* syms; - size_t mtypes; - size_t ntypes; - Type** types; - size_t mmods; size_t nmods; Module* mods; diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index 2fdc586..e7be311 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -48,6 +48,43 @@ static Field* add_field(Parser* p, Type* type, char* name, bool export) return field; } +/* Builtin Operations + *****************************************************************************/ +/* + String Operations: + + * String strcreate(i8* s) + * void strrefinc(String s) + * void strrefdec(String s) + +*/ + +void create_builtin(Parser* p, size_t rtmodid, char* name, char* spec) +{ + (void)spec; + Symbol* sym = symbol_new(p, rtmodid, name, SYM_PROC, 0); + sym->module = rtmodid; + sym->type = symbol_newtype(p); + sym->type->form = FORM_PROC; +} + +SsaNode* call_builtin(Parser* p, char* name) +{ + size_t modid = symbol_getid(p, 0, "Runtime", SYM_MODULE); + size_t symid = symbol_getid(p, modid, name, SYM_PROC); + SsaNode* builtin = ssa_ident(p, symid); + return ssa_call(p, builtin); +} + +SsaNode* string_create(Parser* p) +{ + SsaNode* expr = ssa_string(p, peek(p)->text); + SsaNode* call = call_builtin(p, "StringCreate"); + ssa_call_add(p, call, expr); + codegen_const(p, expr); + return expr; +} + /* Grammar Definition *****************************************************************************/ static SsaNode* expression(Parser* p); @@ -157,8 +194,7 @@ static SsaNode* factor(Parser* p) break; case STRING: - expr = ssa_string(p, peek(p)->text); - codegen_const(p, expr); + expr = string_create(p); consume(p); break; @@ -759,17 +795,18 @@ static void init_parser(Parser* p, char* path, bool genmain) { fatal("could not determine module name from path"); } - p->mtypes = 8; - p->types = calloc(p->mtypes, sizeof(Type*)); symbol_new(p, 0, "$", SYM_VAR, 0); symbol_new(p, 0, "Bool", SYM_TYPE, 0)->type = &BoolType; - p->types[p->ntypes++] = &BoolType; symbol_new(p, 0, "Int", SYM_TYPE, 0)->type = &IntType; - p->types[p->ntypes++] = &IntType; symbol_new(p, 0, "Real", SYM_TYPE, 0)->type = &RealType; - p->types[p->ntypes++] = &RealType; symbol_new(p, 0, "String", SYM_TYPE, 0)->type = &StringType; - p->types[p->ntypes++] = &StringType; + Symbol* rt = symbol_new(p, 0, "Runtime", SYM_MODULE, 0); + size_t rtmodid = rt - &p->syms[0]; + create_builtin(p, rtmodid, "StringCreate", "s"); +// Symbol* sym = symbol_new(p, rtmodid, "StringCreate", SYM_PROC, 0); +// sym->module = rtmodid; +// sym->type = symbol_newtype(p); +// sym->type->form = FORM_PROC; } static void compile_module(Parser* p, char* path, bool genmain) @@ -791,7 +828,7 @@ static void compile_module(Parser* p, char* path, bool genmain) { fatal("assembler failed"); } - remove(p->outpath); +// remove(p->outpath); /* compile the object file now */ char* as_cmd = strmcat("clang -static -c -o \"", obj_path, "\" \"", asm_path, "\"", 0); @@ -876,6 +913,7 @@ void compile(char* path) char* link_cmd = strmcat("clang -static -o ", p.name, " ", 0); for (size_t i = 0; i < p.nmods; i++) { + if (!p.mods[i].path) continue; char* object = strdup(p.mods[i].path); object[strlen(object)-1] = 'o'; link_cmd = strmcat(link_cmd, "\"", object, "\" ", 0); diff --git a/cerise/src/sym.c b/cerise/src/sym.c index 8375fae..f008a40 100644 --- a/cerise/src/sym.c +++ b/cerise/src/sym.c @@ -64,17 +64,8 @@ static size_t lookup(Parser* p, size_t scope, size_t module, char* name, int cla Type* symbol_newtype(Parser* p) { + (void)p; Type* type = calloc(1, sizeof(Type)); - - if (p->ntypes == p->mtypes) - { - p->mtypes = (p->mtypes ? (p->mtypes << 1) : 512u); - p->types = realloc(p->types, p->mtypes * sizeof(Type*)); - } - - p->types[p->ntypes] = type; - p->ntypes++; - return type; } -- 2.49.0