-
Notifications
You must be signed in to change notification settings - Fork 0
/
mrcl_codegen.rb
380 lines (291 loc) · 7.56 KB
/
mrcl_codegen.rb
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
require "json"
require_relative "common"
$label_id = 0
def asm_prologue
puts " push bp"
puts " mov bp sp"
end
def asm_epilogue
puts " mov sp bp"
puts " pop bp"
end
def to_fn_arg_disp(fn_arg_names, fn_arg_name)
index = fn_arg_names.index(fn_arg_name)
index + 2
end
def to_lvar_disp(lvar_names, lvar_name)
index = lvar_names.index(lvar_name)
-(index + 1)
end
# --------------------------------
def _gen_expr_add
puts " pop reg_b"
puts " pop reg_a"
puts " add reg_a reg_b"
end
def _gen_expr_mult
puts " pop reg_b"
puts " pop reg_a"
puts " mul reg_b"
end
def _gen_expr_eq
$label_id += 1
label_id = $label_id
label_end = "end_eq_#{label_id}"
label_then = "then_#{label_id}"
puts " pop reg_b"
puts " pop reg_a"
puts " cmp"
puts " je #{label_then}"
# else
puts " mov reg_a 0"
puts " jmp #{label_end}"
# then
puts "label #{label_then}"
puts " mov reg_a 1"
puts "label #{label_end}"
end
def _gen_expr_neq
$label_id += 1
label_id = $label_id
label_end = "end_neq_#{label_id}"
label_then = "then_#{label_id}"
puts " pop reg_b"
puts " pop reg_a"
puts " cmp"
puts " je #{label_then}"
# else
puts " mov reg_a 1"
puts " jmp #{label_end}"
# then
puts "label #{label_then}"
puts " mov reg_a 0"
puts "label #{label_end}"
end
def _gen_expr_binary(fn_arg_names, lvar_names, expr)
operator, arg_l, arg_r = expr
gen_expr(fn_arg_names, lvar_names, arg_l)
puts " push reg_a"
gen_expr(fn_arg_names, lvar_names, arg_r)
puts " push reg_a"
case operator
when "+" then _gen_expr_add()
when "*" then _gen_expr_mult()
when "==" then _gen_expr_eq()
when "!=" then _gen_expr_neq()
else
raise panic("operator", operator)
end
end
def gen_expr(fn_arg_names, lvar_names, expr)
case expr
when Integer
puts " mov reg_a #{expr}"
when String
case
when fn_arg_names.include?(expr)
disp = to_fn_arg_disp(fn_arg_names, expr)
puts " mov reg_a [bp:#{disp}]"
when lvar_names.include?(expr)
disp = to_lvar_disp(lvar_names, expr)
puts " mov reg_a [bp:#{disp}]"
else
raise panic("expr", expr)
end
when Array
_gen_expr_binary(fn_arg_names, lvar_names, expr)
else
raise panic("expr", expr)
end
end
def _gen_funcall(fn_arg_names, lvar_names, funcall)
fn_name, *fn_args = funcall
fn_args.reverse.each do |fn_arg|
gen_expr(fn_arg_names, lvar_names, fn_arg)
puts " push reg_a"
end
gen_vm_comment("call #{fn_name}")
puts " call #{fn_name}"
puts " add sp #{fn_args.size}"
end
def gen_call(fn_arg_names, lvar_names, stmt)
_, funcall = stmt
_gen_funcall(fn_arg_names, lvar_names, funcall)
end
def gen_call_set(fn_arg_names, lvar_names, stmt)
_, lvar_name, funcall = stmt
_gen_funcall(fn_arg_names, lvar_names, funcall)
disp = to_lvar_disp(lvar_names, lvar_name)
puts " mov [bp:#{disp}] reg_a"
end
def _gen_set(fn_arg_names, lvar_names, dest, expr)
gen_expr(fn_arg_names, lvar_names, expr)
src_val = "reg_a"
case
when lvar_names.include?(dest)
disp = to_lvar_disp(lvar_names, dest)
puts " mov [bp:#{disp}] #{src_val}"
else
raise panic("dest", dest)
end
end
def gen_set(fn_arg_names, lvar_names, stmt)
_, dest, expr = stmt
_gen_set(fn_arg_names, lvar_names, dest, expr)
end
def gen_return(fn_arg_names, lvar_names, stmt)
_, retval = stmt
gen_expr(fn_arg_names, lvar_names, retval)
puts ""
asm_epilogue()
puts " ret"
end
def gen_while(fn_arg_names, lvar_names, stmt)
_, cond_expr, stmts = stmt
$label_id += 1
label_id = $label_id
label_begin = "while_#{label_id}"
label_end = "end_while_#{label_id}"
puts ""
# ループの先頭
puts "label #{label_begin}"
# 条件を評価 ... 結果が reg_a に入る
puts " # -->> eval_expr_#{label_id}"
gen_expr(fn_arg_names, lvar_names, cond_expr)
puts " # <<-- eval_expr_#{label_id}"
# 条件の評価結果と比較するための値を reg_b にセットして比較
puts " mov reg_b 0"
puts " cmp"
# 結果が false の場合ループを抜ける
puts " je #{label_end}"
# 結果が true の場合
gen_stmts(fn_arg_names, lvar_names, stmts)
# ループの先頭に戻る
puts " jmp #{label_begin}"
puts "label #{label_end}"
puts ""
end
def gen_case(fn_arg_names, lvar_names, stmt)
_, *when_clauses = stmt
$label_id += 1
label_id = $label_id
when_idx = -1
label_end = "end_case_#{label_id}"
label_end_when_head = "end_when_#{label_id}"
puts ""
puts " # -->> case_#{label_id}"
when_clauses.each do |when_clause|
when_idx += 1
cond, *stmts = when_clause
puts " # when_#{label_id}_#{when_idx}: #{cond.inspect}"
# 条件を評価 ... 結果が reg_a に入る
puts " # -->> eval_expr_#{label_id}"
gen_expr(fn_arg_names, lvar_names, cond)
puts " # <<-- eval_expr_#{label_id}"
# 条件の評価結果と比較するための値を reg_b にセットして比較
puts " mov reg_b 0"
puts " cmp"
# 結果が false の場合 when 句の最後にジャンプ
puts " je #{label_end_when_head}_#{when_idx}"
# 結果が true の場合
gen_stmts(fn_arg_names, lvar_names, stmts)
puts " jmp #{label_end}"
# 結果が false の場合ここにジャンプ
puts "label #{label_end_when_head}_#{when_idx}"
end
puts "label #{label_end}"
puts " # <<-- case_#{label_id}"
puts ""
end
def gen_vm_comment(comment)
puts " _cmt " + comment.gsub(" ", "~")
end
def gen_debug
puts " _debug"
end
def gen_stmt(fn_arg_names, lvar_names, stmt)
case stmt[0]
when "set" then gen_set( fn_arg_names, lvar_names, stmt)
when "call" then gen_call( fn_arg_names, lvar_names, stmt)
when "call_set" then gen_call_set(fn_arg_names, lvar_names, stmt)
when "return" then gen_return( fn_arg_names, lvar_names, stmt)
when "while" then gen_while( fn_arg_names, lvar_names, stmt)
when "case" then gen_case( fn_arg_names, lvar_names, stmt)
when "_cmt" then gen_vm_comment(stmt[1])
when "_debug" then gen_debug()
else
raise panic("stmt", stmt)
end
end
def gen_stmts(fn_arg_names, lvar_names, stmts)
stmts.each do |stmt|
gen_stmt(fn_arg_names, lvar_names, stmt)
end
end
def gen_var(fn_arg_names, lvar_names, stmt)
puts " add sp -1"
if stmt.size == 3
_, dest, expr = stmt
_gen_set(fn_arg_names, lvar_names, dest, expr)
end
end
def gen_func_def(func_def)
_, fn_name, fn_arg_names, stmts = func_def
puts ""
puts "label #{fn_name}"
asm_prologue()
puts ""
puts " # 関数の処理本体"
lvar_names = []
stmts.each do |stmt|
if stmt[0] == "var"
lvar_names << stmt[1]
gen_var(fn_arg_names, lvar_names, stmt)
else
gen_stmt(fn_arg_names, lvar_names, stmt)
end
end
puts ""
asm_epilogue()
puts " ret"
end
def gen_top_stmts(tree)
_, *top_stmts = tree
top_stmts.each do |top_stmt|
case top_stmt[0]
when "func"
gen_func_def(top_stmt)
else
raise panic("top_stmt", top_stmt)
end
end
end
def gen_builtin_set_vram
puts ""
puts "label set_vram"
asm_prologue()
puts " set_vram [bp:2] [bp:3]" # vram_addr value
asm_epilogue()
puts " ret"
end
def gen_builtin_get_vram
puts ""
puts "label get_vram"
asm_prologue()
puts " get_vram [bp:2] reg_a" # vram_addr dest
asm_epilogue()
puts " ret"
end
def codegen(tree)
puts " call main"
puts " exit"
gen_top_stmts(tree)
gen_builtin_set_vram()
gen_builtin_get_vram()
end
# vgtコード読み込み
src = File.read(ARGV[0])
# 構文木に変換
tree = JSON.parse(src)
# コード生成(アセンブリコードに変換)
codegen(tree)