]> git.mdlowis.com Git - proto/obnc.git/commitdiff
compilation is working for the main module. Need to add compilation of imported modul...
authorMichael D. Lowis <mike@mdlowis.com>
Wed, 7 Dec 2022 04:11:58 +0000 (23:11 -0500)
committerMichael D. Lowis <mike@mdlowis.com>
Wed, 7 Dec 2022 04:11:58 +0000 (23:11 -0500)
cerise/backend/ssa/codegen.c
cerise/build.sh
cerise/inc/cerise.h
cerise/src/fs.c
cerise/src/grammar.c
cerise/src/stdlib.c
cerise/tests/A.l [new file with mode: 0644]
cerise/tests/Module.s [deleted file]

index 0c2e273357b72df580ba97ddc349ee26e8620296..d234662dd740758017035dc4a2be5df1a9a6b439 100644 (file)
@@ -30,14 +30,12 @@ void emit_type(Parser* p, Type* type);
 
 static void fout(Parser* p, char* fmt, ...)
 {
-    (void)p, (void)fmt;
     va_list args;
     va_start(args, fmt);
-    vfprintf(stdout, fmt, args);
+    vfprintf(p->out, fmt, args);
     va_end(args);
 }
 
-
 void declare_record_type(Parser* p, Type* type)
 {
     if (!type->name)
index d66bb4019c19c9234c4d9ba0361b1d8f2712f3a8..5f2543504347bf5a70695eb05bb704075a1c0a73 100755 (executable)
@@ -9,8 +9,7 @@ grep -rh TODO src/*.* backend/*/*.* | sed -E -e 's/ *\/\/ *//' -e 's/ *\/\* *(.*
 
 # Now build and test it
 $CCCMD -o cerisec src/*.c "backend/ssa"/*.c \
-&& (./cerisec tests/Module.m | tee test.l) \
-&& llc test.l
+&& ./cerisec tests/Module.m \
 
 rm -f Module
 
index 14fae8817f6f3d85177d3b0766a776387da7529f..2f690625ab6a48457350b6ff6c4154df9cbcfeeb 100644 (file)
@@ -220,6 +220,8 @@ typedef struct {
     LexFile* file;
     Tok tok;
     char* name;
+    char* outpath;
+    FILE* out;
     long curr_reg;
     size_t scope;
     size_t blockid;
@@ -240,12 +242,14 @@ typedef struct {
 // src/stdlib.c
 void fatal(char* estr);
 void* emalloc(size_t size);
+char* strmcat(char* first, ...);
 
 // src/fs.c
 void fs_init(void);
 char* fs_read(char* path);
 char* fs_modname(char* path);
 char* fs_modfind(Parser* p, char* path);
+bool fs_exists(char* path);
 
 // src/lex.c
 void lexfile(Parser* ctx, char* path);
index 1c1d944cb3b2daca1fb209dec522cd9057ed64c0..5c70c433e7138e9b1b52c0593e2fa20804ff6d47 100644 (file)
@@ -7,26 +7,6 @@
 size_t Num_Paths = 0;
 char** Paths = NULL;
 
-static char* strmcat(char* first, ...) {
-    va_list args;
-    /* calculate the length of the final string */
-    size_t len = strlen(first);
-    va_start(args, first);
-    for (char* s = NULL; (s = va_arg(args, char*));)
-        len += strlen(s);
-    va_end(args);
-    /* allocate the final string and copy the args into it */
-    char *str  = malloc(len+1), *curr = str;
-    while (first && *first) *(curr++) = *(first++);
-    va_start(args, first);
-    for (char* s = NULL; (s = va_arg(args, char*));)
-        while (s && *s) *(curr++) = *(s++);
-    va_end(args);
-    /* null terminate and return */
-    *curr = '\0';
-    return str;
-}
-
 void fs_init(void)
 {
     /* get path variable */
@@ -104,7 +84,7 @@ static char* check_modpath(char* path, char* name)
 {
     char* retpath = NULL;
     char* full_path = strmcat(path, "/", name, ".m", 0);
-    if (access(full_path, F_OK) == 0)
+    if (fs_exists(full_path))
     {
         retpath = full_path;
     }
@@ -125,3 +105,9 @@ char* fs_modfind(Parser* p, char* name)
     }
     return path;
 }
+
+bool fs_exists(char* path)
+{
+    return (access(path, F_OK) == 0);
+}
+
index d213473eccfbfeb96d36803d40626d7ab4ec4a39..3d08f0865a65b9cfef9e7b3f474f9153b4854e63 100644 (file)
@@ -728,6 +728,13 @@ static void module(Parser* p)
     EXIT_RULE();
 }
 
+static char* get_fpath(char* base, char ext)
+{
+    char* path = strdup(base);
+    path[strlen(path)-1] = ext;
+    return path;
+}
+
 static void init_parser(Parser* p, char* path)
 {
     char* fcontents = fs_read(path);
@@ -737,6 +744,8 @@ static void init_parser(Parser* p, char* path)
     p->file->fbeg  = fcontents;
     p->file->fpos  = fcontents;
     p->curr_reg = 0;
+    p->outpath = get_fpath(path, 'l');
+    p->out = fopen(p->outpath, "wb");
     if (p->name == NULL)
     {
         fatal("could not determine module name from path");
@@ -791,4 +800,28 @@ void compile(char* path)
     init_parser(&p, path);
     codegen_init(&p);
     module(&p);
+    fclose(p.out);
+
+    char* asm_path = get_fpath(path, 's');
+//    char* c_path = get_fpath(path, 'c');
+    char* obj_path = get_fpath(path, 'o');
+
+    /* run llc to generate assembly listing */
+    char* llc_cmd = strmcat("llc -o \"", asm_path , "\" \"", p.outpath, "\"", 0);
+    printf("%s\n", llc_cmd);
+    if (system(llc_cmd) != 0)
+    {
+        fatal("assembler failed");
+    }
+    remove(p.outpath);
+
+    char* as_cmd = strmcat("cc -c -o \"", obj_path, "\" \"", asm_path, "\" ",
+//        (fs_exists(c_path) ? strmcat("\"", c_path, "\"", 0) : ""),
+        0);
+    printf("%s\n", as_cmd);
+    if (system(as_cmd) != 0)
+    {
+        fatal("assembler failed");
+    }
+    remove(asm_path);
 }
index 95520ec555e7fdf44e2bdbe45e201d2dbdacf6db..2dace0987fe4e6635e1a7d9be4b03fd61689e357 100644 (file)
@@ -12,3 +12,24 @@ void* emalloc(size_t size)
     if (!ptr) fatal("malloc()");
     return ptr;
 }
+
+char* strmcat(char* first, ...)
+{
+    va_list args;
+    /* calculate the length of the final string */
+    size_t len = strlen(first);
+    va_start(args, first);
+    for (char* s = NULL; (s = va_arg(args, char*));)
+        len += strlen(s);
+    va_end(args);
+    /* allocate the final string and copy the args into it */
+    char *str  = emalloc(len+1), *curr = str;
+    while (first && *first) *(curr++) = *(first++);
+    va_start(args, first);
+    for (char* s = NULL; (s = va_arg(args, char*));)
+        while (s && *s) *(curr++) = *(s++);
+    va_end(args);
+    /* null terminate and return */
+    *curr = '\0';
+    return str;
+}
diff --git a/cerise/tests/A.l b/cerise/tests/A.l
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/cerise/tests/Module.s b/cerise/tests/Module.s
deleted file mode 100644 (file)
index 6c0af9f..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-a
-b
-c
-
-//  c = ...