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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
struct Function {
~uint8 name,
utils.Vector
inputs,
outputs,
~parse.Node _up,
bool m
}
/; method Function
/; init (~parse.Node n)
self.name = utils.strcpy(n`.data)
self._up = n
self.m = false
Var v
self.inputs.init(len v)
self.outputs.init(len v)
;/
/; _resolve_dlist (~Module parent, ~parse.Node dl)
~parse.Node tn = NULL
~parse.Node n
int reg = 1
int stack_up = 0
/; loop (int i = 0; i < dl`.sub.count) [i++]
n = dl`.sub.get(i)
/; if (n`._type == parse.NTYPE_TYPE)
tn = n
;; else if (n`._type == parse.NTYPE_ID)
/; if (tn == NULL)
_printf("Identifier declared in parameter list before any type was found!\n\0")
return
;/
Var p
p.init(tn, n)
p._resolve_type(parent)
# TODO: This is wrong
/; if (p.regable() == true && reg < 7)
p.loc = reg
reg++
;; else
p.loc = 0 - 1
p.offset = stack_up
stack_up = stack_up + p.actual_size()
;/
self.inputs.push(~p)
;/
;/
;/
/; _resolve_tlist (~Module parent, ~parse.Node tl)
~parse.Node n
parse.Node dummy
dummy.data = "### OUTPUT ###\0"
int reg = 1
int stack = 0
/; loop (int i = 0; i < tl`.sub.count) [i++]
n = tl`.sub.get(i)
/; if (n`._type == parse.NTYPE_TYPE)
# TODO: also wrong
Var r
r.init(n, ~dummy)
r._resolve_type(parent)
/; if (r.regable() == true && reg < 7)
r.loc = reg
/; if (reg > 4)
r.loc = r.loc + 4
;/
reg++
;; else
r.loc = 0 - 1
r.offset = stack
stack = stack + r.actual_size()
;/
self.outputs.push(~r)
;/
;/
;/
/; _resolve_type (~Module parent)
~parse.Node _up = self._up
/; if (_up`.sub.count < 1)
return
;/
~parse.Node lst = _up`.sub.get(0)
/; if (lst`._type == parse.NTYPE_DLIST)
self._resolve_dlist(parent, lst)
/; if (_up`.sub.count > 1)
lst = _up`.sub.get(1)
;/
;/
/; if (lst`._type == parse.NTYPE_TLIST)
self._resolve_tlist(parent, lst)
;/
;/
/; _compute_scope_vars(~Scope s)
~Var in
/; loop (int i = 0; i < self.inputs.count) [i++]
in = self.inputs.get(i)
s`.mk_aware(in)
;/
int i = self._first_stmt_off()
/; if (i < 0)
return
;/
~parse.Node _up = self._up
~parse.Node n
/; loop (i < _up`.sub.count) [i++]
n = _up`.sub.get(i)
/; if (n`._type == parse.NTYPE_DECL)
s`.mk_aware_node(n)
;; else
s`.precheck_stmt(n)
;/
;/
;/
/; _build_func(~Module parent, ~CompBuf cb) [Scope]
Scope out
out.init(parent, cb, self.name)
out.parent = NULL
/; if (parent`.e == true)
# Add to the global exports if the parent is exported
~uint8 bl = out.base_label()
cb`.add_h("global \0")
cb`.add_h(bl)
cb`.add_h("\n\0")
_delete(bl)
;/
# Write label and opening
out.place_base_label()
cb`.add_c(" push rbp\n\0")
cb`.add_c(" lea rbp, [rsp + 8]\n\0")
cb`.add_c(" push r10\n\0")
cb`.add_c(" push r11\n\0")
cb`.add_c(" push r12\n\0")
cb`.add_c(" push r13\n\0")
cb`.add_c(" push r14\n\0")
cb`.add_c(" push r15 ; scope init\n\n\0")
self._compute_scope_vars(~out)
# Add all params to the scope
~Var in
/; loop (int i = 0; i < self.inputs.count) [i++]
in = self.inputs.get(i)
out.mk_set_var(in)
;/
return out
;/
/; _end_func(~Scope scope, ~CompBuf cb)
cb`.add_c("\n\0")
scope`.place_end_label()
cb`.add_c(" lea rsp, [rbp - 56]\n\0")
cb`.add_c(" pop r15\n\0")
cb`.add_c(" pop r14\n\0")
cb`.add_c(" pop r13\n\0")
cb`.add_c(" pop r12\n\0")
cb`.add_c(" pop r11\n\0")
cb`.add_c(" pop r10\n\0")
cb`.add_c(" pop rbp\n\0")
cb`.add_c(" ret ; scope end\n\n\n\0")
scope`.end()
;/
/; _first_stmt_off [int]
int i = 0
~parse.Node _up = self._up
/; if (_up`.sub.count < 1)
return i - 1
;/
~parse.Node n = _up`.sub.get(i)
/; if (n`._type == parse.NTYPE_DLIST)
i++
/; if (_up`.sub.count > 1)
n = _up`.sub.get(1)
;/
;/
/; if (n`._type == parse.NTYPE_TLIST)
i++
;/
return i
;/
/; _compile (~Module parent, ~CompBuf cb)
# Sanity check
int i = self._first_stmt_off()
# Create scope
Scope fscope = self._build_func(parent, cb)
/; if (i !< 0)
self._compile_statements(~fscope, i)
;/
# Compile and then end scope
self._end_func(~fscope, cb)
;/
#
# Compiling individual statements
#
/; _compile_statements (~Scope s, int off)
~parse.Node _up = self._up
~parse.Node n = NULL
/; loop (off < _up`.sub.count) [off++]
n = _up`.sub.get(off)
/; if (n`._type == parse.NTYPE_FLOW_CONTROL)
self._compile_flow_control(s, n)
;; else if (n`._type == parse.NTYPE_ASM)
s`.cb`.add_c(" \0")
s`.cb`.add_c(n`.data)
s`.cb`.add_c(" ; User defined asm\n\0")
;/
;/
;/
# Should handle break, continue, and return
/; _compile_flow_control (~Scope s, ~parse.Node n)
/; if (utils.strcmp(n`.data, "return\0") == true)
# Compute value and return
self._compile_value(s, n)
;/
;/
# Should handle computing a value, delegate to other funcs when needed
/; _compile_value (~Scope s, ~parse.Node n)
;/
/; _print (int idt)
_indent(idt)
_printf("{ Function : \0")
_printf(self.name)
_printf("\n\0")
_indent(idt + 1)
_printf("inputs:\n\0")
~Var prtv
/; loop (int i = 0; i < self.inputs.count) [i++]
prtv = self.inputs.get(i)
prtv`._print(idt + 2)
;/
_indent(idt + 1)
_printf("outputs:\n\0")
/; loop (int i = 0; i < self.outputs.count) [i++]
prtv = self.outputs.get(i)
prtv`._print(idt + 2)
;/
_indent(idt)
_printf("}\n\0")
;/
/; end
_delete(self.name)
~Var v
/; loop (int i = 0; i < self.inputs.count) [i++]
v = self.inputs.get(i)
v`.end()
;/
self.inputs.end()
/; loop (int i = 0; i < self.outputs.count) [i++]
v = self.outputs.get(i)
v`.end()
;/
self.outputs.end()
;/
;/
|