aboutsummaryrefslogtreecommitdiff
path: root/sem3/osc/miniproject
diff options
context:
space:
mode:
Diffstat (limited to 'sem3/osc/miniproject')
-rw-r--r--sem3/osc/miniproject/cnasm/ast.c132
-rw-r--r--sem3/osc/miniproject/cnasm/ast.h51
-rw-r--r--sem3/osc/miniproject/cnasm/codegen.c222
-rw-r--r--sem3/osc/miniproject/cnasm/codegen.h4
4 files changed, 205 insertions, 204 deletions
diff --git a/sem3/osc/miniproject/cnasm/ast.c b/sem3/osc/miniproject/cnasm/ast.c
index badf090..cc53e24 100644
--- a/sem3/osc/miniproject/cnasm/ast.c
+++ b/sem3/osc/miniproject/cnasm/ast.c
@@ -4,101 +4,101 @@
#include <string.h>
#include <stdio.h>
-static ast_node_t *create_empty_node() {
- ast_node_t *n = malloc(sizeof(ast_node_t));
- memset(n, 0, sizeof(ast_node_t));
+static ast_node_t *create_empty_node()
+{
+ ast_node_t *n = malloc(sizeof(ast_node_t));
+ 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();
+ ast_node_t *n = create_empty_node();
- n->t = t;
- n->flowctrl.condition = c;
- n->flowctrl.iftrue = iftrue;
- n->flowctrl.iffalse = iffalse;
+ n->t = t;
+ n->flowctrl.condition = c;
+ n->flowctrl.iftrue = iftrue;
+ n->flowctrl.iffalse = iffalse;
- return n;
+ return n;
}
ast_node_t *insert_for(char *pre, cond_t *c, char *inc, ast_node_t *stuff)
{
- ast_node_t *n = create_empty_node();
+ ast_node_t *n = create_empty_node();
- n->t = TFOR;
- n->forloop.condition = c;
- n->forloop.pre = pre;
- n->forloop.inc = inc;
- n->forloop.stuff = stuff;
+ n->t = TFOR;
+ n->forloop.condition = c;
+ n->forloop.pre = pre;
+ n->forloop.inc = inc;
+ n->forloop.stuff = stuff;
- return n;
+ return n;
}
ast_node_t *insert_stm(ast_node_t *stm, ast_node_t *stm_list)
{
- ast_node_t *n = create_empty_node();
+ ast_node_t *n = create_empty_node();
- n->t = TSTM_LIST;
- n->list.children[0] = stm_list;
- n->list.children[1] = stm;
+ n->t = TSTM_LIST;
+ n->list.children[0] = stm_list;
+ n->list.children[1] = stm;
- return n;
+ return n;
}
ast_node_t *insert_ident(char *ident)
{
- ast_node_t *n = create_empty_node();
+ ast_node_t *n = create_empty_node();
- n->t = TIDENT;
- n->ident = ident;
+ n->t = TIDENT;
+ n->ident = ident;
- return n;
+ return n;
}
cond_t *insert_cond(uint8_t cmp, char *a, char *b)
{
- cond_t *c = malloc( sizeof(cond_t));
+ cond_t *c = malloc(sizeof(cond_t));
- c->cmp = cmp;
- c->a = a;
- c->b = b;
+ c->cmp = cmp;
+ c->a = a;
+ c->b = b;
}
void node_print(ast_node_t *node)
{
- if ( !node ){
- printf("Nil");
- return;
- }
- switch (node->t) {
- case TSTM_LIST:
- printf("Stm_list(");
- node_print(node->list.children[0]);
- printf(",");
- node_print(node->list.children[1]);
- printf(")");
- break;
- case TIF:
- printf("If");
- printf("(%s %c %s) {", node->flowctrl.condition->a,
- node->flowctrl.condition->cmp,
- node->flowctrl.condition->b);
- node_print(node->flowctrl.iftrue);
- printf("}{");
- node_print(node->flowctrl.iffalse);
- printf("}");
- break;
- case TIDENT:
- printf("%s", node->ident);
- break;
- case TWHILE:
- printf("while");
- printf("(%s %c %s) {", node->flowctrl.condition->a,
- node->flowctrl.condition->cmp,
- node->flowctrl.condition->b);
- node_print(node->flowctrl.iftrue);
- printf("}");
- break;
- default:
- printf("invalid");
- }
+ if (!node) {
+ printf("Nil");
+ return;
+ }
+ switch (node->t) {
+ case TSTM_LIST:
+ printf("Stm_list(");
+ node_print(node->list.children[0]);
+ printf(",");
+ node_print(node->list.children[1]);
+ printf(")");
+ break;
+ case TIF:
+ printf("If");
+ printf("(%s %c %s) {", node->flowctrl.condition->a,
+ node->flowctrl.condition->cmp, node->flowctrl.condition->b);
+ node_print(node->flowctrl.iftrue);
+ printf("}{");
+ node_print(node->flowctrl.iffalse);
+ printf("}");
+ break;
+ case TIDENT:
+ printf("%s", node->ident);
+ break;
+ case TWHILE:
+ printf("while");
+ printf("(%s %c %s) {", node->flowctrl.condition->a,
+ node->flowctrl.condition->cmp, node->flowctrl.condition->b);
+ node_print(node->flowctrl.iftrue);
+ printf("}");
+ break;
+ default:
+ printf("invalid");
+ }
}
diff --git a/sem3/osc/miniproject/cnasm/ast.h b/sem3/osc/miniproject/cnasm/ast.h
index 61a8f4f..4f37553 100644
--- a/sem3/osc/miniproject/cnasm/ast.h
+++ b/sem3/osc/miniproject/cnasm/ast.h
@@ -11,37 +11,38 @@
#define CLEQ CGT ^ 0x80
#define CGEQ CLT ^ 0x80
-enum ntype { TSTM_LIST, TIF, TFOR, TIDENT, TWHILE};
+enum ntype { TSTM_LIST, TIF, TFOR, TIDENT, TWHILE };
typedef struct cond {
- uint8_t cmp;
- char *a;
- char *b;
+ uint8_t cmp;
+ char *a;
+ char *b;
} cond_t;
typedef struct ast_node {
- enum ntype t;
- // Dependent on type
- union {
- struct {
- cond_t *condition;
- struct ast_node *iftrue;
- struct ast_node *iffalse;
- } flowctrl;
- struct {
- cond_t *condition;
- char *pre;
- char *inc;
- struct ast_node *stuff;
- } forloop;
- char *ident;
- struct {
- struct ast_node *children[2];
- } list;
- };
+ enum ntype t;
+ // Dependent on type
+ union {
+ struct {
+ cond_t *condition;
+ struct ast_node *iftrue;
+ struct ast_node *iffalse;
+ } flowctrl;
+ struct {
+ cond_t *condition;
+ char *pre;
+ char *inc;
+ struct ast_node *stuff;
+ } forloop;
+ char *ident;
+ struct {
+ struct ast_node *children[2];
+ } list;
+ };
} 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 *insert_stm(ast_node_t *stm, ast_node_t *stm_list);
ast_node_t *insert_ident(char *ident);
ast_node_t *insert_for(char *pre, cond_t *c, char *inc, ast_node_t *stuff);
@@ -50,4 +51,4 @@ cond_t *insert_cond(uint8_t cmp, char *a, char *b);
void node_print(ast_node_t *node);
-#endif
+#endif
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;
+ }
}
diff --git a/sem3/osc/miniproject/cnasm/codegen.h b/sem3/osc/miniproject/cnasm/codegen.h
index 24ad6c4..14c68fe 100644
--- a/sem3/osc/miniproject/cnasm/codegen.h
+++ b/sem3/osc/miniproject/cnasm/codegen.h
@@ -6,9 +6,9 @@
#include "ast.h"
struct genctx {
- unsigned int nested;
+ unsigned int nested;
};
void gentree(FILE *f, struct genctx *ctx, ast_node_t *n);
-#endif
+#endif