aboutsummaryrefslogtreecommitdiff
path: root/sem3/osc/miniproject/cnasm/codegen.c
blob: c995df9823354009af9639f6e9e16f2d1d67cd60 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "codegen.h"

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include "ast.h"

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 ");
	}
}

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);
}

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);
}

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);
}

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;
	}
}