diff options
author | Julian T <julian@jtle.dk> | 2020-10-12 16:26:42 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-10-12 16:54:48 +0200 |
commit | 4e05a55e373bd315e721d534a1711fec4c0054c5 (patch) | |
tree | 4fc0141760c2730ed79aaa4c7aa60ea45cc66cc0 /sem3/osc/miniproject/cnasm/codegen.c | |
parent | b7f9cf43c8a9ab3400cbb30d5e1cadb0c6c2cf23 (diff) |
Moved to linux kernel inspired clang-format
Diffstat (limited to 'sem3/osc/miniproject/cnasm/codegen.c')
-rw-r--r-- | sem3/osc/miniproject/cnasm/codegen.c | 222 |
1 files changed, 111 insertions, 111 deletions
diff --git a/sem3/osc/miniproject/cnasm/codegen.c b/sem3/osc/miniproject/cnasm/codegen.c index 57584a4..6f32982 100644 --- a/sem3/osc/miniproject/cnasm/codegen.c +++ b/sem3/osc/miniproject/cnasm/codegen.c @@ -7,131 +7,131 @@ 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) { - case CEQ: - fprintf(f, "je "); - break; - case CNEQ: - fprintf(f, "jne "); - break; - case CGT: - fprintf(f, "jg "); - break; - case CLT: - fprintf(f, "jl "); - break; - case CLEQ: - fprintf(f, "jle "); - break; - case CGEQ: - fprintf(f, "jge "); - break; - default: - fprintf(stderr, "Invalid cmp type %x", cmp); - fprintf(f, "jmp "); - } + uint8_t cmp = neg ? c->cmp ^ 0x80 : c->cmp; + fprintf(f, "cmp %s %s\n", c->a, c->b); + switch (cmp) { + case CEQ: + fprintf(f, "je "); + break; + case CNEQ: + fprintf(f, "jne "); + break; + case CGT: + fprintf(f, "jg "); + break; + case CLT: + fprintf(f, "jl "); + break; + case CLEQ: + fprintf(f, "jle "); + break; + case CGEQ: + fprintf(f, "jge "); + break; + default: + fprintf(stderr, "Invalid cmp type %x", cmp); + fprintf(f, "jmp "); + } } 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 ) { - fprintf(f, "else_%d\n", ctx->nested); - } else { - fprintf(f, "end_%d\n", ctx->nested); - } - - struct genctx octx = { ctx->nested+1 }; - // Paste code - gentree(f, &octx, n->flowctrl.iftrue); - - // Do else - if ( doElse ) { - fprintf(f, "jmp end_%d\n", ctx->nested); - fprintf(f, "else_%d:\n", ctx->nested); - gentree(f, &octx, n->flowctrl.iffalse); - } - - // End block - fprintf(f, "end_%d:\n", ctx->nested); + bool doElse = n->flowctrl.iffalse; + // Create conditional jump + gencondjmp(f, n->flowctrl.condition, true); + if (doElse) { + fprintf(f, "else_%d\n", ctx->nested); + } else { + fprintf(f, "end_%d\n", ctx->nested); + } + + struct genctx octx = { ctx->nested + 1 }; + // Paste code + gentree(f, &octx, n->flowctrl.iftrue); + + // Do else + if (doElse) { + fprintf(f, "jmp end_%d\n", ctx->nested); + fprintf(f, "else_%d:\n", ctx->nested); + gentree(f, &octx, n->flowctrl.iffalse); + } + + // End block + fprintf(f, "end_%d:\n", ctx->nested); } static void genwhile(FILE *f, struct genctx *ctx, ast_node_t *n) { - // Create loop label - fprintf(f, "loop_%d:\n", ctx->nested); - - // Create conditional jump - gencondjmp(f, n->flowctrl.condition, true); - fprintf(f, "end_%d\n", ctx->nested); - - struct genctx octx = { ctx->nested+1 }; - // Paste code - gentree(f, &octx, n->flowctrl.iftrue); - - // Jump to start - fprintf(f, "jmp loop_%d\n", ctx->nested); - - // End block - fprintf(f, "end_%d:\n", ctx->nested); + // Create loop label + fprintf(f, "loop_%d:\n", ctx->nested); + + // Create conditional jump + gencondjmp(f, n->flowctrl.condition, true); + fprintf(f, "end_%d\n", ctx->nested); + + struct genctx octx = { ctx->nested + 1 }; + // Paste code + gentree(f, &octx, n->flowctrl.iftrue); + + // Jump to start + fprintf(f, "jmp loop_%d\n", ctx->nested); + + // End block + fprintf(f, "end_%d:\n", ctx->nested); } static void genfor(FILE *f, struct genctx *ctx, ast_node_t *n) { - // Do pre stuff - fprintf(f, "%s\n", n->forloop.pre); - - // Create loop label - fprintf(f, "loop_%d:\n", ctx->nested); - - // Create conditional jump - gencondjmp(f, n->flowctrl.condition, true); - fprintf(f, "end_%d\n", ctx->nested); - - struct genctx octx = { ctx->nested+1 }; - // Paste code - gentree(f, &octx, n->forloop.stuff); - - // Do inc stuff - fprintf(f, "%s\n", n->forloop.inc); - // Jump to start - fprintf(f, "jmp loop_%d\n", ctx->nested); - - // End block - fprintf(f, "end_%d:\n", ctx->nested); + // Do pre stuff + fprintf(f, "%s\n", n->forloop.pre); + + // Create loop label + fprintf(f, "loop_%d:\n", ctx->nested); + + // Create conditional jump + gencondjmp(f, n->flowctrl.condition, true); + fprintf(f, "end_%d\n", ctx->nested); + + struct genctx octx = { ctx->nested + 1 }; + // Paste code + gentree(f, &octx, n->forloop.stuff); + + // Do inc stuff + fprintf(f, "%s\n", n->forloop.inc); + // Jump to start + fprintf(f, "jmp loop_%d\n", ctx->nested); + + // End block + fprintf(f, "end_%d:\n", ctx->nested); } void gentree(FILE *f, struct genctx *ctx, ast_node_t *n) { - if ( !n ) { - return; - } - if ( ctx == NULL ) { - ctx = malloc(sizeof(struct genctx)); - ctx->nested = 0; - } - switch (n->t) { - case TSTM_LIST: - gentree(f, ctx, n->list.children[0]); - gentree(f, ctx, n->list.children[1]); - break; - case TIF: - genif(f, ctx, n); - break; - case TIDENT: - fprintf(f, "%s\n", n->ident); - break; - case TFOR: - genfor(f, ctx, n); - break; - case TWHILE: - genwhile(f, ctx, n); - break; - default: - return; - } + if (!n) { + return; + } + if (ctx == NULL) { + ctx = malloc(sizeof(struct genctx)); + ctx->nested = 0; + } + switch (n->t) { + case TSTM_LIST: + gentree(f, ctx, n->list.children[0]); + gentree(f, ctx, n->list.children[1]); + break; + case TIF: + genif(f, ctx, n); + break; + case TIDENT: + fprintf(f, "%s\n", n->ident); + break; + case TFOR: + genfor(f, ctx, n); + break; + case TWHILE: + genwhile(f, ctx, n); + break; + default: + return; + } } |