-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodegen.c
81 lines (59 loc) · 1 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
#include <stdio.h>
#include "codegen.h"
#include "getSource.h"
#define MAXCODE 200
typedef struct inst{
OpCode opCode;
union{
RelAddr addr;
int value;
Operator optr;
} u;
} Inst;
static Inst code[MAXCODE];
static int cIndex = -1;
static void checkMax();
int nextCode()
{
return cIndex + 1;
}
int genCodeV(OpCode op, int v)
{
checkMax();
code[cIndex].opCode = op;
code[cIndex].u.value = v;
return cIndex;
}
int genCodeT(OpCode op, int ti)
{
checkMax();
code[cIndex].opCode = op;
code[cIndex].u.addr = relAddr(ti);
return cIndex;
}
int genCodeO(Operator p)
{
checkMax();
code[cIndex].opCode = opr;
code[cIndex].u.optr = p;
return cIndex;
}
int genCodeR()
{
if (code[cIndex].opCode == ret)
return cIndex;
checkMax();
code[cIndex].opCode = ret;
code[cIndex].u.addr.level = bLevel();
code[cIndex].u.addr.addr = fPars();
return cIndex;
}
void checkMax()
{
if(++cIndex < MAXCODE) return;
errorF("too many code");
}
void backPatch(int i)
{
code[i].u.value = cIndex + 1;
}