]> git.mdlowis.com Git - proto/obnc.git/commitdiff
added runtime module. has special handling for now that should be removed eventually
authorMichael D. Lowis <mike@mdlowis.com>
Mon, 12 Dec 2022 04:19:08 +0000 (23:19 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Mon, 12 Dec 2022 04:19:08 +0000 (23:19 -0500)
cerise/backend/ssa/codegen.c
cerise/inc/cerise.h
cerise/src/grammar.c
cerise/src/sym.c

index 4099bc5c6ea2ba630a5c0b43c29a6a703fb8bab0..8408d17d6c91a72c026d5fb644dae58306444db2 100644 (file)
@@ -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]);
     }
index 192d72566bf55ebc23da0bba824dd5f31be4c6a6..784933fb2293f503901079aacc9bf7296ab7d4fa 100644 (file)
@@ -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;
index 2fdc586174ae488142a9dec500b3a2ce12baca5f..e7be31105260cfba3ad300fcee512c7485c419d1 100644 (file)
@@ -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);
index 8375faef0a7b5238b80e6cb1571c778c7d7087e9..f008a40b4357e0e2f346e4078861ff6e1960bb4b 100644 (file)
@@ -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;
 }