/
stmt.v
345 lines (333 loc) · 8.27 KB
/
stmt.v
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module native
import v.ast
fn C.strtol(str &char, endptr &&char, base int) int
pub fn (mut g Gen) stmts(stmts []ast.Stmt) {
for stmt in stmts {
g.stmt(stmt)
}
}
fn (mut g Gen) stmt(node ast.Stmt) {
match node {
ast.AssignStmt {
g.code_gen.assign_stmt(node)
}
ast.Block {
g.stmts(node.stmts)
}
ast.BranchStmt {
label_name := node.label
for i := g.labels.branches.len - 1; i >= 0; i-- {
branch := g.labels.branches[i]
if label_name == '' || label_name == branch.name {
label := if node.kind == .key_break {
branch.end
} else { // continue
branch.start
}
jump_addr := g.code_gen.jmp(0)
g.labels.patches << LabelPatch{
id: label
pos: jump_addr
}
g.println('; jump to ${label}: ${node.kind}')
break
}
}
}
ast.ConstDecl {}
ast.DeferStmt {
name := '_defer${g.defer_stmts.len}'
defer_var := g.get_var_offset(name)
g.code_gen.mov_int_to_var(LocalVar{defer_var, ast.i64_type_idx, name}, 1)
g.defer_stmts << node
g.defer_stmts[g.defer_stmts.len - 1].idx_in_fn = g.defer_stmts.len - 1
}
ast.ExprStmt {
g.expr(node.expr)
}
ast.FnDecl {
g.fn_decl(node)
}
ast.ForCStmt {
g.gen_forc_stmt(node)
}
ast.ForInStmt {
if node.stmts.len == 0 {
// if no statements, just dont make it
return
}
g.for_in_stmt(node)
}
ast.ForStmt {
g.for_stmt(node)
}
ast.HashStmt {
words := node.val.split(' ')
mut unsupported := false
for word in words {
if word.len != 2 {
unsupported = true
break
}
b := unsafe { C.strtol(&char(word.str), 0, 16) }
// b := word.u8()
// println('"$word" $b')
g.write8(b)
}
if unsupported {
if !g.pref.experimental {
g.warning('opcodes format: xx xx xx xx\nhash statements are not allowed with the native backend, use the C backend for extended C interoperability.',
node.pos)
}
}
}
ast.Module {}
ast.Return {
g.code_gen.return_stmt(node)
}
ast.AsmStmt {
g.code_gen.gen_asm_stmt(node)
}
ast.AssertStmt {
g.gen_assert(node)
}
ast.GlobalDecl {
if !g.pref.experimental {
g.warning('globals are not supported yet', node.pos)
}
}
ast.Import {} // do nothing here
ast.StructDecl {}
ast.EnumDecl {}
ast.TypeDecl {}
ast.InterfaceDecl {}
else {
eprintln('native.stmt(): bad node: ' + node.type_name())
}
}
}
fn (mut g Gen) gen_forc_stmt(node ast.ForCStmt) {
if node.has_init {
g.stmts([node.init])
}
start := g.pos()
start_label := g.labels.new_label()
mut jump_addr := i64(0)
if node.has_cond {
cond := node.cond
match cond {
ast.InfixExpr {
match cond.left {
ast.Ident {
lit := cond.right as ast.IntegerLiteral
g.code_gen.cmp_var(cond.left as ast.Ident, lit.val.int())
match cond.op {
.gt {
jump_addr = g.code_gen.cjmp(.jle)
}
.ge {
jump_addr = g.code_gen.cjmp(.jl)
}
.lt {
jump_addr = g.code_gen.cjmp(.jge)
}
.le {
jump_addr = g.code_gen.cjmp(.jg)
}
else {
g.n_error('unsupported conditional in for-c loop')
}
}
}
else {
g.n_error('unhandled infix.left')
}
}
}
else {}
}
// dump(node.cond)
g.expr(node.cond)
}
end_label := g.labels.new_label()
g.labels.patches << LabelPatch{
id: end_label
pos: int(jump_addr)
}
g.println('; jump to label ${end_label}')
g.labels.branches << BranchLabel{
name: node.label
start: start_label
end: end_label
}
g.stmts(node.stmts)
g.labels.addrs[start_label] = g.pos()
g.println('; label ${start_label}')
if node.has_inc {
g.stmts([node.inc])
}
g.labels.branches.pop()
g.code_gen.jmp_back(start)
g.labels.addrs[end_label] = g.pos()
g.println('; jump to label ${end_label}')
// loop back
}
fn (mut g Gen) for_stmt(node ast.ForStmt) {
if node.is_inf {
if node.stmts.len == 0 {
g.code_gen.infloop()
return
}
// infinite loop
start := g.pos()
start_label := g.labels.new_label()
g.labels.addrs[start_label] = start
g.println('; label ${start_label}')
end_label := g.labels.new_label()
g.labels.branches << BranchLabel{
name: node.label
start: start_label
end: end_label
}
g.stmts(node.stmts)
g.labels.branches.pop()
g.code_gen.jmp_back(start)
g.println('jmp after infinite for')
g.labels.addrs[end_label] = g.pos()
g.println('; label ${end_label}')
return
}
infix_expr := node.cond as ast.InfixExpr
mut jump_addr := 0 // location of `jne *00 00 00 00*`
start := g.pos()
start_label := g.labels.new_label()
g.labels.addrs[start_label] = start
g.println('; label ${start_label}')
match infix_expr.left {
ast.Ident {
match infix_expr.right {
ast.Ident {
reg := g.code_gen.main_reg()
g.code_gen.mov_var_to_reg(reg, infix_expr.right as ast.Ident)
g.code_gen.cmp_var_reg(infix_expr.left as ast.Ident, reg)
}
ast.IntegerLiteral {
lit := infix_expr.right as ast.IntegerLiteral
g.code_gen.cmp_var(infix_expr.left as ast.Ident, lit.val.int())
}
else {
g.n_error('unhandled expression type')
}
}
match infix_expr.left.tok_kind {
.lt {
jump_addr = g.code_gen.cjmp(.jge)
}
.gt {
jump_addr = g.code_gen.cjmp(.jle)
}
.le {
jump_addr = g.code_gen.cjmp(.jg)
}
.ge {
jump_addr = g.code_gen.cjmp(.jl)
}
.ne {
jump_addr = g.code_gen.cjmp(.je)
}
.eq {
jump_addr = g.code_gen.cjmp(.jne)
}
else {
g.n_error('unhandled infix cond token')
}
}
}
else {
g.n_error('unhandled infix.left')
}
}
end_label := g.labels.new_label()
g.labels.patches << LabelPatch{
id: end_label
pos: jump_addr
}
g.println('; jump to label ${end_label}')
g.labels.branches << BranchLabel{
name: node.label
start: start_label
end: end_label
}
g.stmts(node.stmts)
g.labels.branches.pop()
// Go back to `cmp ...`
g.code_gen.jmp_back(start)
// Update the jump addr to current pos
g.labels.addrs[end_label] = g.pos()
g.println('; label ${end_label}')
g.println('jmp after for')
}
fn (mut g Gen) for_in_stmt(node ast.ForInStmt) { // Work on that
if node.is_range {
// for a in node.cond .. node.high {
i := g.code_gen.allocate_var(node.val_var, 8, 0) // iterator variable
g.expr(node.cond) // outputs the lower loop bound (initial value) to the main reg
main_reg := g.code_gen.main_reg()
g.code_gen.mov_reg_to_var(LocalVar{i, ast.i64_type_idx, node.val_var}, main_reg) // i = node.cond // initial value
start := g.pos() // label-begin:
start_label := g.labels.new_label()
g.code_gen.mov_var_to_reg(main_reg, LocalVar{i, ast.i64_type_idx, node.val_var})
g.code_gen.push(main_reg) // put the iterator on the stack
g.expr(node.high) // final value (upper bound) to the main reg
g.code_gen.cmp_to_stack_top(main_reg)
jump_addr := g.code_gen.cjmp(.jge) // leave loop i >= upper bound
end_label := g.labels.new_label()
g.labels.patches << LabelPatch{
id: end_label
pos: jump_addr
}
g.println('; jump to label ${end_label}')
g.labels.branches << BranchLabel{
name: node.label
start: start_label
end: end_label
}
g.stmts(node.stmts) // writes the actual body of the loop
g.labels.addrs[start_label] = g.pos()
g.println('; label ${start_label}')
g.code_gen.inc_var(LocalVar{i, ast.i64_type_idx, node.val_var})
g.labels.branches.pop()
g.code_gen.jmp_back(start) // loops
g.labels.addrs[end_label] = g.pos()
g.println('; label ${end_label}')
/*
} else if node.kind == .array {
} else if node.kind == .array_fixed {
} else if node.kind == .map {
} else if node.kind == .string {
} else if node.kind == .struct_ {
} else if it.kind in [.array, .string] || it.cond_type.has_flag(.variadic) {
} else if it.kind == .map {
*/
} else {
g.v_error('for-in statement is not yet implemented', node.pos)
}
}
fn (mut g Gen) gen_assert(assert_node ast.AssertStmt) {
mut cjmp_addr := 0
ane := assert_node.expr
label := g.labels.new_label()
cjmp_addr = g.condition(ane, true)
g.labels.patches << LabelPatch{
id: label
pos: cjmp_addr
}
g.println('; jump to label ${label}')
g.expr(assert_node.expr)
g.code_gen.trap()
g.labels.addrs[label] = g.pos()
g.println('; label ${label}')
}