diff options
Diffstat (limited to 'sem3/osc/miniproject/cnasm')
-rw-r--r-- | sem3/osc/miniproject/cnasm/ast.c | 22 | ||||
-rw-r--r-- | sem3/osc/miniproject/cnasm/codegen.c | 27 |
2 files changed, 30 insertions, 19 deletions
diff --git a/sem3/osc/miniproject/cnasm/ast.c b/sem3/osc/miniproject/cnasm/ast.c index 035b75d..badf090 100644 --- a/sem3/osc/miniproject/cnasm/ast.c +++ b/sem3/osc/miniproject/cnasm/ast.c @@ -9,7 +9,8 @@ static ast_node_t *create_empty_node() { memset(n, 0, sizeof(ast_node_t)); } -ast_node_t *insert_ctrl(enum ntype t, cond_t *c, ast_node_t *iftrue, ast_node_t *iffalse) { +ast_node_t *insert_ctrl(enum ntype t, cond_t *c, ast_node_t *iftrue, ast_node_t *iffalse) +{ ast_node_t *n = create_empty_node(); n->t = t; @@ -20,7 +21,8 @@ ast_node_t *insert_ctrl(enum ntype t, cond_t *c, ast_node_t *iftrue, ast_node_t return n; } -ast_node_t *insert_for(char *pre, cond_t *c, char *inc, ast_node_t *stuff) { +ast_node_t *insert_for(char *pre, cond_t *c, char *inc, ast_node_t *stuff) +{ ast_node_t *n = create_empty_node(); n->t = TFOR; @@ -32,7 +34,8 @@ ast_node_t *insert_for(char *pre, cond_t *c, char *inc, ast_node_t *stuff) { return n; } -ast_node_t *insert_stm(ast_node_t *stm, ast_node_t *stm_list) { +ast_node_t *insert_stm(ast_node_t *stm, ast_node_t *stm_list) +{ ast_node_t *n = create_empty_node(); n->t = TSTM_LIST; @@ -41,7 +44,8 @@ ast_node_t *insert_stm(ast_node_t *stm, ast_node_t *stm_list) { return n; } -ast_node_t *insert_ident(char *ident) { +ast_node_t *insert_ident(char *ident) +{ ast_node_t *n = create_empty_node(); n->t = TIDENT; @@ -50,7 +54,8 @@ ast_node_t *insert_ident(char *ident) { return n; } -cond_t *insert_cond(uint8_t cmp, char *a, char *b) { +cond_t *insert_cond(uint8_t cmp, char *a, char *b) +{ cond_t *c = malloc( sizeof(cond_t)); c->cmp = cmp; @@ -58,12 +63,13 @@ cond_t *insert_cond(uint8_t cmp, char *a, char *b) { c->b = b; } -void node_print(ast_node_t *node) { - if( !node ){ +void node_print(ast_node_t *node) +{ + if ( !node ){ printf("Nil"); return; } - switch(node->t) { + switch (node->t) { case TSTM_LIST: printf("Stm_list("); node_print(node->list.children[0]); diff --git a/sem3/osc/miniproject/cnasm/codegen.c b/sem3/osc/miniproject/cnasm/codegen.c index c995df9..57584a4 100644 --- a/sem3/osc/miniproject/cnasm/codegen.c +++ b/sem3/osc/miniproject/cnasm/codegen.c @@ -5,10 +5,11 @@ #include <stdlib.h> #include "ast.h" -static void gencondjmp(FILE *f, cond_t *c, bool neg) { +static void gencondjmp(FILE *f, cond_t *c, bool neg) +{ uint8_t cmp = neg ? c->cmp^0x80 : c->cmp; fprintf(f, "cmp %s %s\n", c->a, c->b); - switch(cmp) { + switch (cmp) { case CEQ: fprintf(f, "je "); break; @@ -33,11 +34,12 @@ static void gencondjmp(FILE *f, cond_t *c, bool neg) { } } -static void genif(FILE *f, struct genctx *ctx, ast_node_t *n) { +static void genif(FILE *f, struct genctx *ctx, ast_node_t *n) +{ bool doElse = n->flowctrl.iffalse; // Create conditional jump gencondjmp(f, n->flowctrl.condition, true); - if( doElse ) { + if ( doElse ) { fprintf(f, "else_%d\n", ctx->nested); } else { fprintf(f, "end_%d\n", ctx->nested); @@ -48,7 +50,7 @@ static void genif(FILE *f, struct genctx *ctx, ast_node_t *n) { gentree(f, &octx, n->flowctrl.iftrue); // Do else - if( doElse ) { + if ( doElse ) { fprintf(f, "jmp end_%d\n", ctx->nested); fprintf(f, "else_%d:\n", ctx->nested); gentree(f, &octx, n->flowctrl.iffalse); @@ -58,7 +60,8 @@ static void genif(FILE *f, struct genctx *ctx, ast_node_t *n) { fprintf(f, "end_%d:\n", ctx->nested); } -static void genwhile(FILE *f, struct genctx *ctx, ast_node_t *n) { +static void genwhile(FILE *f, struct genctx *ctx, ast_node_t *n) +{ // Create loop label fprintf(f, "loop_%d:\n", ctx->nested); @@ -77,7 +80,8 @@ static void genwhile(FILE *f, struct genctx *ctx, ast_node_t *n) { fprintf(f, "end_%d:\n", ctx->nested); } -static void genfor(FILE *f, struct genctx *ctx, ast_node_t *n) { +static void genfor(FILE *f, struct genctx *ctx, ast_node_t *n) +{ // Do pre stuff fprintf(f, "%s\n", n->forloop.pre); @@ -101,15 +105,16 @@ static void genfor(FILE *f, struct genctx *ctx, ast_node_t *n) { fprintf(f, "end_%d:\n", ctx->nested); } -void gentree(FILE *f, struct genctx *ctx, ast_node_t *n) { - if( !n ) { +void gentree(FILE *f, struct genctx *ctx, ast_node_t *n) +{ + if ( !n ) { return; } - if( ctx == NULL ) { + if ( ctx == NULL ) { ctx = malloc(sizeof(struct genctx)); ctx->nested = 0; } - switch(n->t) { + switch (n->t) { case TSTM_LIST: gentree(f, ctx, n->list.children[0]); gentree(f, ctx, n->list.children[1]); |