-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.c
183 lines (163 loc) · 4.02 KB
/
tree.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
#include "tree.h"
#include <stdio.h>
#include "symbol.h"
#include "temp.h"
#include "util.h"
T_expList T_ExpList(T_exp head, T_expList tail) {
T_expList p = (T_expList)checked_malloc(sizeof *p);
p->head = head;
p->tail = tail;
return p;
}
T_stmList T_StmList(T_stm head, T_stmList tail) {
T_stmList p = (T_stmList)checked_malloc(sizeof *p);
p->head = head;
p->tail = tail;
return p;
}
T_stm T_Seq(T_stm left, T_stm right) {
T_stm p = (T_stm)checked_malloc(sizeof *p);
p->kind = T_SEQ;
p->u.SEQ.left = left;
p->u.SEQ.right = right;
return p;
}
T_stm T_Label(Temp_label label) {
T_stm p = (T_stm)checked_malloc(sizeof *p);
p->kind = T_LABEL;
p->u.LABEL = label;
return p;
}
/**
* @param{labels} specifies all the possible locations that the expression e can
* evaluate to. A common case may be: T_Jump(l, Temp_LabelList(l, NULL))
*/
T_stm T_Jump(T_exp exp, Temp_labelList labels) {
T_stm p = (T_stm)checked_malloc(sizeof *p);
p->kind = T_JUMP;
p->u.JUMP.exp = exp;
p->u.JUMP.jumps = labels;
return p;
}
T_stm T_Cjump(T_relOp op, T_exp left, T_exp right, Temp_label true,
Temp_label false) {
T_stm p = (T_stm)checked_malloc(sizeof *p);
p->kind = T_CJUMP;
p->u.CJUMP.op = op;
p->u.CJUMP.left = left;
p->u.CJUMP.right = right;
p->u.CJUMP.true = true;
p->u.CJUMP.false = false;
return p;
}
T_stm T_Move(T_exp dst, T_exp src) {
T_stm p = (T_stm)checked_malloc(sizeof *p);
p->kind = T_MOVE;
p->u.MOVE.dst = dst;
p->u.MOVE.src = src;
return p;
}
T_stm T_Exp(T_exp exp) {
T_stm p = (T_stm)checked_malloc(sizeof *p);
p->kind = T_EXP;
p->u.EXP = exp;
return p;
}
T_exp T_Binop(T_binOp op, T_exp left, T_exp right) {
T_exp p = (T_exp)checked_malloc(sizeof *p);
p->kind = T_BINOP;
p->u.BINOP.op = op;
p->u.BINOP.left = left;
p->u.BINOP.right = right;
return p;
}
T_exp T_Mem(T_exp exp) {
T_exp p = (T_exp)checked_malloc(sizeof *p);
p->kind = T_MEM;
p->u.MEM = exp;
return p;
}
T_exp T_Temp(Temp_temp temp) {
T_exp p = (T_exp)checked_malloc(sizeof *p);
p->kind = T_TEMP;
p->u.TEMP = temp;
return p;
}
T_exp T_Eseq(T_stm stm, T_exp exp) {
T_exp p = (T_exp)checked_malloc(sizeof *p);
p->kind = T_ESEQ;
p->u.ESEQ.stm = stm;
p->u.ESEQ.exp = exp;
return p;
}
T_exp T_Name(Temp_label name) {
T_exp p = (T_exp)checked_malloc(sizeof *p);
p->kind = T_NAME;
p->u.NAME = name;
return p;
}
T_exp T_Const(int consti) {
T_exp p = (T_exp)checked_malloc(sizeof *p);
p->kind = T_CONST;
p->u.CONST = consti;
return p;
}
T_exp T_Call(T_exp fun, T_expList args) {
T_exp p = (T_exp)checked_malloc(sizeof *p);
p->kind = T_CALL;
p->u.CALL.fun = fun;
p->u.CALL.args = args;
return p;
}
T_relOp T_notRel(T_relOp r) {
switch (r) {
case T_eq:
return T_ne;
case T_ne:
return T_eq;
case T_lt:
return T_ge;
case T_ge:
return T_lt;
case T_gt:
return T_le;
case T_le:
return T_gt;
case T_ult:
return T_uge;
case T_uge:
return T_ult;
case T_ule:
return T_ugt;
case T_ugt:
return T_ule;
}
assert(0);
return 0;
}
T_relOp T_commute(T_relOp r) {
switch (r) {
case T_eq:
return T_eq;
case T_ne:
return T_ne;
case T_lt:
return T_gt;
case T_ge:
return T_le;
case T_gt:
return T_lt;
case T_le:
return T_ge;
case T_ult:
return T_ugt;
case T_uge:
return T_ule;
case T_ule:
return T_uge;
case T_ugt:
return T_ult;
}
assert(0);
return 0;
}