]> git.mdlowis.com Git - proto/obnc.git/commitdiff
added new backend
authormike lowis <mike@mdlowis.com>
Wed, 9 Jun 2021 11:45:35 +0000 (07:45 -0400)
committermike lowis <mike@mdlowis.com>
Wed, 9 Jun 2021 11:45:35 +0000 (07:45 -0400)
cerise/backend/ssa/codegen.c [new file with mode: 0644]
cerise/build.sh
cerise/inc/cerise.h
cerise/src/sym.c

diff --git a/cerise/backend/ssa/codegen.c b/cerise/backend/ssa/codegen.c
new file mode 100644 (file)
index 0000000..7d67b17
--- /dev/null
@@ -0,0 +1,176 @@
+#include "cerise.h"
+#include <limits.h>
+#include <assert.h>
+
+Type BoolType = {
+    .form = FORM_BOOL,
+    .size = sizeof(bool)
+};
+
+Type IntType = {
+    .form = FORM_INT,
+    .size = sizeof(long)
+};
+
+Type RealType = {
+    .form = FORM_REAL,
+    .size = sizeof(double)
+};
+
+Type StringType = {
+    .form = FORM_STRING,
+    .size = -1
+};
+
+
+static size_t MaxBlocks = 0;
+static size_t NumBlocks = 0;
+static Block* Blocks = NULL;
+
+static size_t block_new(void)
+{
+    if (NumBlocks == MaxBlocks)
+    {
+        MaxBlocks = (MaxBlocks ? 16u : (MaxBlocks << 1u));
+        Blocks = realloc(Blocks, MaxBlocks * sizeof(Block));
+    }
+    memset(&Blocks[NumBlocks], 0, sizeof(Block));
+    NumBlocks++;
+    return (NumBlocks-1);
+}
+
+static void put_op(size_t blkid, Operation* op)
+{
+    assert(blkid < NumBlocks);
+    Block* block = &Blocks[blkid];
+    if (block->nops == block->mops)
+    {
+        block->mops = (block->mops ? 4u : (block->mops << 1u));
+        block->ops = realloc(block->ops, block->mops * sizeof(Operation));
+    }
+    block->ops[block->nops++] = *op;
+}
+
+
+void codegen_startmod(Parser* p)
+{
+    (void)p;
+}
+
+void codegen_endmod(Parser* p)
+{
+    (void)p;
+}
+
+void codegen_setint(Item* item, Type* type, long long val)
+{
+    item->mode  = ITEM_CONST;
+    item->type  = type;
+    item->reg   = 0;
+    item->imm.i = val;
+}
+
+void codegen_setreal(Item* item, double val)
+{
+    item->mode  = ITEM_CONST;
+    item->type  = &RealType;
+    item->reg   = 0;
+    item->imm.f = val;
+}
+
+void codegen_setstr(Item* item, char* val)
+{
+    item->mode  = ITEM_CONST;
+    item->type  = &StringType;
+    item->reg   = 0;
+    item->imm.s = val;
+}
+
+void codegen_imports(Parser* p)
+{
+    (void)p;
+}
+
+void codegen_var(Parser* p, Symbol* sym)
+{
+    (void)p, (void)sym;
+}
+
+void codegen_main(Parser* p)
+{
+    (void)p;
+}
+
+void codegen_startproc(Parser* p, Symbol* proc)
+{
+    (void)p, (void)proc;
+    printf("func %s(){\n", (proc ? proc->name : p->name));
+    printf("L%lu:\n", block_new());
+}
+
+void codegen_endproc(Parser* p)
+{
+    (void)p;
+    printf("}\n");
+}
+
+void codegen_unop(Parser* p, int op, Item* a)
+{
+    (void)p, (void)op, (void)a;
+}
+
+void codegen_binop(Parser* p, int op, Item* a, Item* b)
+{
+    (void)p, (void)op, (void)a, (void)b;
+}
+
+void codegen_store(Parser* p, Item* a, Item* b)
+{
+    (void)p, (void)a, (void)b;
+}
+
+void codegen_if(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
+
+void codegen_else(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
+
+void codegen_endif(Parser* p, long elsifs, Item* item)
+{
+    (void)p, (void)elsifs, (void)item;
+}
+
+void codegen_prepcall(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
+
+void codegen_call(Parser* p, Item* item, Item* args)
+{
+    (void)p, (void)item, (void)args;
+}
+
+void codegen_setarg(Parser* p, Item* item, bool firstarg)
+{
+    (void)p, (void)item, (void)firstarg;
+}
+
+void codegen_return(Parser* p, Item* item)
+{
+    (void)p, (void)item;
+}
+
+void codegen_index(Parser* p, Item* array, Item* index)
+{
+    (void)p, (void)array, (void)index;
+}
+
+void codegen_field(Parser* p, Item* record, char* name)
+{
+    (void)p, (void)record, (void)name;
+}
+
index 258525ae86bca26ec2b4b8804763569b8d247a43..2eafd8cb0ffba690c64fc8c9d74f6fd59363680f 100755 (executable)
@@ -6,10 +6,12 @@ TEST_BACKEND=backend/test
 ctags -R &
 
    $CCCMD -D CERISE_TESTS -o cerisec-test src/*.c backend/test/*.c \
-&& $CCCMD -o cerisec src/*.c "backend/c99"/*.c \
-&& $CCCMD -o cerisec-x86_64 src/*.c "backend/x86_64"/*.c \
+&& $CCCMD -o cerisec src/*.c "backend/ssa"/*.c \
 && ./cerisec-test \
 && ./cerisec tests/Module.m
 
+#&& $CCCMD -o cerisec src/*.c "backend/c99"/*.c \
+#&& $CCCMD -o cerisec-x86_64 src/*.c "backend/x86_64"/*.c \
+
 rm -f Module
 
index e1abc1c670ea87db79356666cfb1efa65b0c729d..716389a8149936ee627d4ed991d8e37821e0eb41 100644 (file)
@@ -251,7 +251,7 @@ typedef struct {
 typedef struct {
     size_t nops;
     size_t mops;
-    Operation** ops;
+    Operation* ops;
 } Block;
 
 long ir_startblock(Parser* p);
@@ -261,40 +261,3 @@ void ir_binopi(Parser* p, int op, long dest, long arg, long imm);
 void ir_binopf(Parser* p, int op, long dest, long arg, double imm);
 void ir_unnopi(Parser* p, int op, long dest, long imm);
 void ir_unopf(Parser* p, int op, long dest, double imm);
-
-/* <op> <reg>, <base>+<off> */
-/* <op> <base>+<off>, <reg> */
-/* <op> <reg>, <reg> */
-/* <op> <reg>, <reg>, <reg> */
-
-/*
-
-typedef struct AnfNode {
-    enum {
-        ANF_VAR, ANF_BEGIN, ANF_PROC, ANF_IF, ANF_SET, ANF_LET
-    } form;
-} AnfNode;
-
-;; <prog> ::= <dec> ...
-
-;; <dec> ::= (define <var> <exp>)
-;;        |  (begin <dec> ...)
-;;        |  <exp>
-
-;; <aexp> ::= (λ (<name> ...) <exp>)
-;;         |  <number>
-;;         |  <boolean>
-;;         |  <string>
-;;         |  <var>
-;;         |  (void)
-
-;; <cexp> ::= (<aexp> <aexp> ...)
-;;         |  (if <aexp> <exp> <exp>)
-;;         |  (set! <var> <exp>)
-
-;; <exp> ::= (let ([<var> <cexp>]) <exp>)
-;;        |  <aexp>
-;;        |  <cexp>
-
-
-*/
index 1a18752c296dc6c6c01d0aea55ee1e271ad22330..eb42ce4fb5e6b3152a1b9983e24b7503c79eb3c8 100644 (file)
@@ -71,6 +71,7 @@ Symbol* symbol_new(Parser* p, size_t scope, char* name, int class, bool export)
 //    puts("");
 
     /* insert */
+    memset(&p->syms[p->nsyms], 0, sizeof(Symbol));
     p->syms[p->nsyms].name = name;
     p->syms[p->nsyms].class = class;
     p->syms[p->nsyms].export = export;