-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.c
190 lines (176 loc) · 4.69 KB
/
codegen.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include "codegen.h"
#include "getSource.h"
#ifndef TBL
#define TBL
#include "table.h"
#endif
#define MAXCODE 200
#define MAXMEM 2000
#define MAXREG 20
#define MAXLEVEL 5
typedef struct inst {
OpCode opCode;
union {
RelativeAddr addr;
int value;
Operator optr;
} u;
} Inst;
static Inst code[MAXCODE];
static int codeIndex = -1;
static void printCode(int i);
int nextCode()
{
return codeIndex + 1;
}
int genCodeWithValue(OpCode op, int value)
{
codeIndex++;
code[codeIndex].opCode = op;
code[codeIndex].u.value = value;
return(codeIndex);
}
int genCodeWithAddr(OpCode op, int tableIndex)
{
codeIndex++;
code[codeIndex].opCode = op;
code[codeIndex].u.addr = relAddr(tableIndex);
return(codeIndex);
}
int genCodeOperator(Operator operator)
{
codeIndex++;
code[codeIndex].opCode = opr;
code[codeIndex].u.optr = operator;
return(codeIndex);
}
int genCodeReturn()
{
if (code[codeIndex].opCode == ret) return(codeIndex);
codeIndex++;
code[codeIndex].opCode = ret;
code[codeIndex].u.addr.level = currentBlockLevel();
code[codeIndex].u.addr.addr = functionParamCount();
return(codeIndex);
}
void backPatch(int index)
{
code[index].u.value = codeIndex + 1;
}
void listCode()
{
int i;
printf("\ncode\n");
for(i=0; i<=codeIndex; i++){
printf("%3d: ", i);
printCode(i);
}
}
void printCode(int i)
{
int flag;
switch (code[i].opCode) {
case lit: printf("lit"); flag=1; break;
case opr: printf("opr"); flag=3; break;
case lod: printf("lod"); flag=2; break;
case sto: printf("sto"); flag=2; break;
case cal: printf("cal"); flag=2; break;
case ret: printf("ret"); flag=2; break;
case ict: printf("ict"); flag=1; break;
case jmp: printf("jmp"); flag=1; break;
case jpc: printf("jpc"); flag=1; break;
}
switch (flag) {
case 1:
printf(",%d\n", code[i].u.value);
return;
case 2:
printf(",%d", code[i].u.addr.level);
printf(",%d\n", code[i].u.addr.addr);
return;
case 3:
switch (code[i].u.optr) {
case neg: printf(",neg\n"); return;
case add: printf(",add\n"); return;
case sub: printf(",sub\n"); return;
case mul: printf(",mul\n"); return;
case div: printf(",div\n"); return;
case odd: printf(",odd\n"); return;
case eq: printf(",eq\n"); return;
case ls: printf(",ls\n"); return;
case gr: printf(",gr\n"); return;
case neq: printf(",neq\n"); return;
case lseq: printf(",lseq\n"); return;
case greq: printf(",greq\n"); return;
case wrt: printf(",wrt\n"); return;
case wrl: printf(",wrl\n"); return;
}
}
}
void execute() {
int stack[MAXMEM];
int display[MAXLEVEL];
int pc, top, lev, temp;
Inst i;
top = 0;
pc = 0;
stack[0] = 0;
stack[1] = 0;
display[0] = 0;
do {
i = code[pc++];
switch (i.opCode) {
case lit:
stack[top++] = i.u.value;
break;
case lod:
stack[top++] = stack[display[i.u.addr.level] + i.u.addr.addr];
break;
case sto:
stack[display[i.u.addr.level] + i.u.addr.addr] = stack[--top];
break;
case cal:
lev = i.u.addr.level + 1;
stack[top] = display[lev];
stack[top+1] = pc;
display[lev] = top;
pc = i.u.addr.addr;
break;
case ret:
temp = stack[--top];
top = display[i.u.addr.level];
display[i.u.addr.level] = stack[top];
pc = stack[top+1];
top -= i.u.addr.addr;
stack[top++] = temp;
break;
case ict:
top += i.u.value;
break;
case jmp:
pc = i.u.value;
break;
case jpc:
if (stack[--top] == 0) pc = i.u.value;
break;
case opr:
switch(i.u.optr) {
case neg: stack[top-1] = -stack[top-1]; continue;
case add: --top; stack[top-1] += stack[top]; continue;
case sub: --top; stack[top-1] -= stack[top]; continue;
case mul: --top; stack[top-1] *= stack[top]; continue;
case div: --top; stack[top-1] /= stack[top]; continue;
case odd: stack[top-1] = stack[top-1] & 1; continue;
case eq: --top; stack[top-1] = (stack[top-1] == stack[top]); continue;
case ls: --top; stack[top-1] = (stack[top-1] < stack[top]); continue;
case gr: --top; stack[top-1] = (stack[top-1] > stack[top]); continue;
case neq: --top; stack[top-1] = (stack[top-1] != stack[top]); continue;
case lseq: --top; stack[top-1] = (stack[top-1] <= stack[top]); continue;
case greq: --top; stack[top-1] = (stack[top-1] >= stack[top]); continue;
case wrt: printf("%d ", stack[--top]); continue;
case wrl: printf("\n"); continue;
}
}
} while (pc != 0);
}