From d0475569a55c53f4693bef86a985e50fb0af1cea Mon Sep 17 00:00:00 2001 From: mike lowis Date: Wed, 9 Jun 2021 07:45:35 -0400 Subject: [PATCH] added new backend --- cerise/backend/ssa/codegen.c | 176 +++++++++++++++++++++++++++++++++++ cerise/build.sh | 6 +- cerise/inc/cerise.h | 39 +------- cerise/src/sym.c | 1 + 4 files changed, 182 insertions(+), 40 deletions(-) create mode 100644 cerise/backend/ssa/codegen.c diff --git a/cerise/backend/ssa/codegen.c b/cerise/backend/ssa/codegen.c new file mode 100644 index 0000000..7d67b17 --- /dev/null +++ b/cerise/backend/ssa/codegen.c @@ -0,0 +1,176 @@ +#include "cerise.h" +#include +#include + +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; +} + diff --git a/cerise/build.sh b/cerise/build.sh index 258525a..2eafd8c 100755 --- a/cerise/build.sh +++ b/cerise/build.sh @@ -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 diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index e1abc1c..716389a 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -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); - -/* , + */ -/* +, */ -/* , */ -/* , , */ - -/* - -typedef struct AnfNode { - enum { - ANF_VAR, ANF_BEGIN, ANF_PROC, ANF_IF, ANF_SET, ANF_LET - } form; -} AnfNode; - -;; ::= ... - -;; ::= (define ) -;; | (begin ...) -;; | - -;; ::= (λ ( ...) ) -;; | -;; | -;; | -;; | -;; | (void) - -;; ::= ( ...) -;; | (if ) -;; | (set! ) - -;; ::= (let ([ ]) ) -;; | -;; | - - -*/ diff --git a/cerise/src/sym.c b/cerise/src/sym.c index 1a18752..eb42ce4 100644 --- a/cerise/src/sym.c +++ b/cerise/src/sym.c @@ -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; -- 2.49.0