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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
use std::collections::HashMap;
use log::{debug, trace};
use petgraph::visit::DfsPostOrder;
use super::ir::instruction::{Block, ControlFlowGraph, Instruction, Value};
use crate::bytecode::{Bytecode, Opcode, Operand, Register};
use super::regalloc::{Action, RegAlloc};
type PatchFn = Box<dyn Fn(&mut Codegen)>;
pub struct Codegen {
reg_alloc: RegAlloc,
codes: Vec<Bytecode>,
block_map: HashMap<isize, isize>,
inst_index: usize,
}
impl Codegen {
pub fn new(registers: &[Register]) -> Self {
Self {
reg_alloc: RegAlloc::new(registers),
codes: Vec::new(),
block_map: HashMap::new(),
inst_index: 0,
}
}
pub fn generate_code(&mut self, cfg: ControlFlowGraph) -> &[Bytecode] {
let mut cfg = cfg;
Self::resort_blocks(&mut cfg);
debug!("===IR===");
let mut pos = 0;
for block in &cfg.blocks {
debug!("block({}):", block.id.as_usize());
for inst in &block.instructions {
debug!("{pos}\t{inst}");
pos += 1;
}
}
debug!("===IR===");
self.reg_alloc.arrange(&cfg);
let mut patchs: Vec<PatchFn> = Vec::new();
// alloc stack frame, need rewrite with actual stack size
// rsp = rsp + stack_size
let pos = self.codes.len();
patchs.push(Box::new(move |this: &mut Self| {
this.codes[pos].operands[2] = Operand::new_immd(this.reg_alloc.stack_size() as isize)
}));
// placeholder
self.codes.push(Bytecode::triple(
Opcode::AddC,
Operand::Register(Register::Rsp),
Operand::Register(Register::Rsp),
Operand::new_immd(0),
));
for block in cfg.blocks.iter() {
self.block_map
.insert(block.id.as_usize() as isize, self.codes.len() as isize);
for inst in &block.instructions {
debug!("inst[{}]: {inst:?}", self.inst_index);
debug!("register: {}", self.reg_alloc.reg_set);
match inst.clone() {
// Function Call Instructions
Instruction::Call { func, args, result } => {
self.gen_call(func, &args, result);
}
Instruction::CallEx {
callable,
args,
result,
} => {
self.gen_call_ex(callable, &args, result);
}
Instruction::CallNative { func, args, result } => {
self.gen_call_native(func, &args, result);
}
Instruction::PropertyCall {
object,
property,
args,
result,
} => {
self.gen_prop_call(object, property, &args, result);
}
// Load and Move Instructions
Instruction::LoadArg { dst, index } => {
let dst = self.gen_operand(dst);
let stack = self.reg_alloc.load_arg(index);
self.codes.push(Bytecode::double(
Opcode::Mov,
dst,
Operand::new_stack(stack),
));
}
Instruction::LoadConst { dst, const_id } => {
let dst = self.gen_operand(dst);
self.codes.push(Bytecode::double(
Opcode::LoadConst,
dst,
const_id.to_operand(),
));
}
Instruction::LoadEnv { dst, name } => {
let dst = self.gen_operand(dst);
self.codes
.push(Bytecode::double(Opcode::LoadEnv, dst, name.to_operand()));
}
Instruction::Move { dst, src } => {
let src = self.gen_operand(src);
let dst = self.gen_operand(dst);
self.codes.push(Bytecode::double(Opcode::Mov, dst, src));
}
// Unary and Binary Operators
Instruction::UnaryOp { op, dst, src } => {
let src = self.gen_operand(src);
let dst = self.gen_operand(dst);
self.codes.push(Bytecode::double(op, dst, src));
}
Instruction::BinaryOp { op, dst, lhs, rhs } => {
let src1 = self.gen_operand(lhs);
let src2 = self.gen_operand(rhs);
let dst = self.gen_operand(dst);
self.codes.push(Bytecode::triple(op, dst, src1, src2));
}
// Range Instructions
Instruction::MakeRange {
op,
begin,
end,
result,
} => match (begin, end) {
(Some(begin), Some(end)) => {
let src1 = self.gen_operand(begin);
let src2 = self.gen_operand(end);
let dst = self.gen_operand(result);
self.codes.push(Bytecode::triple(op, dst, src1, src2));
}
(Some(begin), None) => {
let src1 = self.gen_operand(begin);
let dst = self.gen_operand(result);
self.codes
.push(Bytecode::double(Opcode::RangeFrom, dst, src1));
}
(None, Some(end)) => {
let src1 = self.gen_operand(end);
let dst = self.gen_operand(result);
match op {
Opcode::RangeInclusive => {
self.codes.push(Bytecode::double(
Opcode::RangeToInclusive,
dst,
src1,
));
}
Opcode::Range => {
self.codes
.push(Bytecode::double(Opcode::RangeTo, dst, src1));
}
_ => unreachable!("invalid op"),
}
}
(None, None) => {
let dst = self.gen_operand(result);
self.codes.push(Bytecode::single(Opcode::RangeFull, dst));
}
},
// Collection / Structural Operations
Instruction::MakeArray { dst } => {
let dst = self.gen_operand(dst);
self.codes.push(Bytecode::single(Opcode::MakeArray, dst));
}
Instruction::ArrayPush { array, value } => {
let array = self.gen_operand(array);
let value = self.gen_operand(value);
self.codes
.push(Bytecode::double(Opcode::ArrayPush, array, value));
}
Instruction::MakeMap { dst } => {
let dst = self.gen_operand(dst);
self.codes.push(Bytecode::single(Opcode::MakeMap, dst));
}
Instruction::IndexSet {
object,
index: idx,
value,
} => {
let object = self.gen_operand(object);
let idx = self.gen_operand(idx);
let value = self.gen_operand(value);
self.codes
.push(Bytecode::triple(Opcode::IndexSet, object, idx, value));
}
Instruction::IndexGet {
dst,
object,
index: idx,
} => {
let dst = self.gen_operand(dst);
let object = self.gen_operand(object);
let idx = self.gen_operand(idx);
self.codes
.push(Bytecode::triple(Opcode::IndexGet, dst, object, idx));
}
Instruction::MakeSlice { dst, object, range } => {
let dst = self.gen_operand(dst);
let object = self.gen_operand(object);
let range = self.gen_operand(range);
self.codes
.push(Bytecode::triple(Opcode::MakeSlice, dst, object, range));
}
Instruction::PropertyGet {
dst,
object,
property,
} => {
let dst = self.gen_operand(dst);
let object = self.gen_operand(object);
let property = self.gen_operand(property);
self.codes
.push(Bytecode::triple(Opcode::PropGet, dst, object, property));
}
Instruction::PropertySet {
object,
property,
value,
} => {
let object = self.gen_operand(object);
let property = self.gen_operand(property);
let value = self.gen_operand(value);
self.codes
.push(Bytecode::triple(Opcode::PropSet, object, property, value));
}
Instruction::MakeStruct { dst } => {
let dst = self.gen_operand(dst);
self.codes.push(Bytecode::single(Opcode::MakeStruct, dst));
}
Instruction::MakeStructField {
object,
field,
value,
} => {
let object = self.gen_operand(object);
let field = self.gen_operand(field);
let value = self.gen_operand(value);
self.codes.push(Bytecode::triple(
Opcode::MakeStructField,
object,
field,
value,
));
}
// Iteration Instructions
Instruction::MakeIterator {
src: iter,
dst: result,
} => {
let src = self.gen_operand(iter);
let dst = self.gen_operand(result);
self.codes
.push(Bytecode::double(Opcode::MakeIter, dst, src));
}
Instruction::IterateNext { iter, dst: next } => {
let src = self.gen_operand(iter);
let dst = self.gen_operand(next);
self.codes
.push(Bytecode::double(Opcode::IterNext, dst, src));
}
// Control Flow Instructions
Instruction::Return { value } => {
if let Some(v) = value {
let ret = self.gen_operand(v);
self.codes.push(Bytecode::double(
Opcode::Mov,
Operand::new_register(Register::Rv),
ret,
));
}
self.codes.push(Bytecode::empty(Opcode::Ret));
}
Instruction::Br { dst } => {
let dst = self.gen_operand(dst);
let pos = self.codes.len();
patchs.push(Box::new(move |this: &mut Self| {
let dst = this.codes[pos].operands[0].as_immd();
this.codes[pos].operands[0] =
Operand::new_immd(this.block_map[&dst] - pos as isize);
}));
self.codes.push(Bytecode::single(Opcode::Br, dst));
}
Instruction::BrIf {
condition,
true_blk,
false_blk,
} => {
let condition = self.gen_operand(condition);
let true_blk = self.gen_operand(true_blk);
let false_blk = self.gen_operand(false_blk);
let pos = self.codes.len();
patchs.push(Box::new(move |this: &mut Self| {
let true_blk = this.codes[pos].operands[1].as_immd();
this.codes[pos].operands[1] =
Operand::new_immd(this.block_map[&true_blk] - pos as isize);
let false_blk = this.codes[pos].operands[2].as_immd();
this.codes[pos].operands[2] =
Operand::new_immd(this.block_map[&false_blk] - pos as isize);
}));
self.codes.push(Bytecode::triple(
Opcode::BrIf,
condition,
true_blk,
false_blk,
));
}
Instruction::Halt => {
self.codes.push(Bytecode::empty(Opcode::Halt));
}
// Async Support
Instruction::Await { promise, dst } => {
let promise = self.gen_operand(promise);
let dst = self.gen_operand(dst);
self.codes
.push(Bytecode::double(Opcode::Await, dst, promise));
}
}
let (defined, used) = inst.defined_and_used_vars();
for var in defined {
if matches!(var, Value::Variable(_)) {
if let Some(Action::Spill { stack, register }) =
self.reg_alloc.release(var, self.inst_index)
{
trace!("spilling({var}) {register} -> [rbp+{stack}]");
self.codes.push(Bytecode::double(
Opcode::Mov,
Operand::Stack(stack as isize),
register.into(),
));
}
}
}
for var in used {
if matches!(var, Value::Variable(_)) {
if let Some(Action::Spill { stack, register }) =
self.reg_alloc.release(var, self.inst_index)
{
trace!("spilling({var}) {register} -> [rbp+{stack}]");
self.codes.push(Bytecode::double(
Opcode::Mov,
Operand::Stack(stack as isize),
register.into(),
));
}
}
}
self.inst_index += 1;
}
}
for patch in patchs {
patch(self);
}
&self.codes
}
fn gen_call(&mut self, func: Value, args: &[Value], result: Value) {
// 1. Backup used registers
let in_use_registers = self.reg_alloc.in_use_registers();
for reg in in_use_registers.iter().copied() {
self.codes.push(Bytecode::single(Opcode::Push, reg.into()));
}
// 2. Push arguments onto the stack
self.store_args(args, self.inst_index);
// 3. Set up new stack frame
self.codes.push(Bytecode::single(
Opcode::PushC,
Operand::new_register(Register::Rbp),
));
self.codes.push(Bytecode::double(
Opcode::MovC,
Operand::new_register(Register::Rbp),
Operand::new_register(Register::Rsp),
));
// 4. Call the function
self.codes
.push(Bytecode::single(Opcode::Call, func.to_operand()));
// 5. Restore stack pointer (reset to current base pointer)
self.codes.push(Bytecode::double(
Opcode::MovC,
Operand::new_register(Register::Rsp),
Operand::new_register(Register::Rbp),
));
// 6. Pop the saved base pointer
self.codes
.push(Bytecode::single(Opcode::PopC, Register::Rbp.into()));
// 7. Clean up arguments from the stack
self.codes.push(Bytecode::triple(
Opcode::SubC,
Operand::Register(Register::Rsp),
Operand::Register(Register::Rsp),
Operand::new_immd(args.len() as isize),
));
// 8. Restore backed-up registers
for reg in in_use_registers.iter().rev().copied() {
self.codes.push(Bytecode::single(Opcode::Pop, reg.into()));
}
// 9. Move return value to destination register
let result_reg = self.gen_operand(result);
self.codes.push(Bytecode::double(
Opcode::Mov,
result_reg,
Operand::new_register(Register::Rv),
));
}
fn gen_call_ex(&mut self, func: Value, args: &[Value], result: Value) {
let callable = self.gen_operand(func);
// 1. Backup used registers
let in_use_registers = self.reg_alloc.in_use_registers();
for reg in in_use_registers.iter().copied() {
self.codes.push(Bytecode::single(Opcode::Push, reg.into()));
}
// 2. Push arguments onto the stack
self.store_args(args, self.inst_index);
// 3. Set up new stack frame
self.codes.push(Bytecode::single(
Opcode::PushC,
Operand::new_register(Register::Rbp),
));
self.codes.push(Bytecode::double(
Opcode::MovC,
Operand::new_register(Register::Rbp),
Operand::new_register(Register::Rsp),
));
// 4. Call the function (CallEx)
self.codes.push(Bytecode::single(Opcode::CallEx, callable));
// 5. Restore stack pointer to current base pointer
self.codes.push(Bytecode::double(
Opcode::MovC,
Operand::new_register(Register::Rsp),
Operand::new_register(Register::Rbp),
));
// 6. Pop saved base pointer
self.codes
.push(Bytecode::single(Opcode::PopC, Register::Rbp.into()));
// 7. Clean up arguments from the stack
self.codes.push(Bytecode::triple(
Opcode::SubC,
Operand::Register(Register::Rsp),
Operand::Register(Register::Rsp),
Operand::new_immd(args.len() as isize),
));
// 8. Restore backed-up registers
for reg in in_use_registers.iter().rev().copied() {
self.codes.push(Bytecode::single(Opcode::Pop, reg.into()));
}
// 9. Move return value to destination register
let result_reg = self.gen_operand(result);
self.codes.push(Bytecode::double(
Opcode::Mov,
result_reg,
Operand::new_register(Register::Rv),
));
}
fn gen_call_native(&mut self, func: Value, args: &[Value], result: Value) {
let callable = self.gen_operand(func);
// 1. Push arguments onto the stack
self.store_args(args, self.inst_index);
// 2. Set up new stack frame
self.codes.push(Bytecode::single(
Opcode::PushC,
Operand::new_register(Register::Rbp),
));
self.codes.push(Bytecode::double(
Opcode::MovC,
Operand::new_register(Register::Rbp),
Operand::new_register(Register::Rsp),
));
// 3. Call the native function
self.codes.push(Bytecode::double(
Opcode::CallNative,
callable,
Operand::new_immd(args.len() as isize),
));
// 4. Restore stack pointer to current base pointer
self.codes.push(Bytecode::double(
Opcode::MovC,
Operand::new_register(Register::Rsp),
Operand::new_register(Register::Rbp),
));
// 5. Pop saved base pointer
self.codes
.push(Bytecode::single(Opcode::PopC, Register::Rbp.into()));
// 6. Clean up arguments from the stack
self.codes.push(Bytecode::triple(
Opcode::SubC,
Operand::Register(Register::Rsp),
Operand::Register(Register::Rsp),
Operand::new_immd(args.len() as isize),
));
// 7. Move return value to destination register
let result_reg = self.gen_operand(result);
self.codes.push(Bytecode::double(
Opcode::Mov,
result_reg,
Operand::new_register(Register::Rv),
));
}
fn gen_prop_call(&mut self, object: Value, property: Value, args: &[Value], result: Value) {
let callable = self.gen_operand(object);
// 1. Push arguments onto the stack
self.store_args(args, self.inst_index);
// 2. Set up new stack frame
self.codes.push(Bytecode::single(
Opcode::PushC,
Operand::new_register(Register::Rbp),
));
self.codes.push(Bytecode::double(
Opcode::MovC,
Operand::new_register(Register::Rbp),
Operand::new_register(Register::Rsp),
));
let prop = self.gen_operand(property);
// 3. Call method on the object
self.codes.push(Bytecode::triple(
Opcode::CallMethod,
callable,
prop,
Operand::new_immd(args.len() as isize),
));
// 4. Restore stack pointer to current base pointer
self.codes.push(Bytecode::double(
Opcode::MovC,
Operand::new_register(Register::Rsp),
Operand::new_register(Register::Rbp),
));
// 5. Pop saved base pointer
self.codes
.push(Bytecode::single(Opcode::PopC, Register::Rbp.into()));
// 6. Clean up arguments from the stack
self.codes.push(Bytecode::triple(
Opcode::SubC,
Operand::Register(Register::Rsp),
Operand::Register(Register::Rsp),
Operand::new_immd(args.len() as isize),
));
// 7. Move return value to destination register
let result_reg = self.gen_operand(result);
self.codes.push(Bytecode::double(
Opcode::Mov,
result_reg,
Operand::new_register(Register::Rv),
));
}
fn store_args(&mut self, args: &[Value], index: usize) {
for arg in args.iter().rev() {
let op = self.gen_operand(*arg);
self.codes.push(Bytecode::single(Opcode::Push, op));
if let Some(action) = self.reg_alloc.release(*arg, index) {
match action {
Action::Spill { stack, register } => {
trace!("spilling({arg}) {register} -> [rbp+{stack}]");
self.codes.push(Bytecode::double(
Opcode::Mov,
Operand::Stack(stack as isize),
register.into(),
));
}
_ => unreachable!("action must be spill"),
}
}
}
}
fn gen_operand(&mut self, value: Value) -> Operand {
match value {
Value::Primitive(v) => Operand::new_primitive(v),
Value::Constant(id) => Operand::new_immd(id.as_usize() as isize),
Value::Function(id) => Operand::new_symbol(id.as_usize() as u32),
Value::Block(id) => Operand::new_immd(id.as_usize() as isize),
Value::Variable(_) => {
let (register, unspill) = self.reg_alloc.alloc(value, self.inst_index);
if let Some(Action::Restore { stack, register }) = unspill {
trace!("unspilling({value}) [rbp+{stack}] -> {register}");
self.codes.push(Bytecode::double(
Opcode::Mov,
register.into(),
Operand::Stack(stack as isize),
));
}
Operand::new_register(register)
}
}
}
fn resort_blocks(control_flow_graph: &mut ControlFlowGraph) {
// sort blocks by post order
let mut sorted = sort_graph_blocks(control_flow_graph);
let mut iter = sorted.iter_mut().peekable();
while let Some(block) = iter.next() {
// remove instructions after return
if let Some(i) = block
.instructions
.iter()
.rposition(|item| matches!(item, Instruction::Return { .. }))
{
if i + 1 < block.instructions.len() {
block.instructions.drain(i + 1..);
}
}
let next_block_id = iter.peek().map(|b| b.id);
if let Some(Instruction::Br { dst }) = block.instructions.last() {
if next_block_id == dst.as_block() {
block.instructions.pop();
}
}
}
control_flow_graph.blocks = sorted;
}
}
fn sort_graph_blocks(control_flow_graph: &ControlFlowGraph) -> Vec<Block> {
let graph = &control_flow_graph.graph;
let entry = control_flow_graph.entry().expect("no entry block");
let start = control_flow_graph.block_node_map[&entry];
let mut dfs = DfsPostOrder::new(graph, start);
let mut sorted = Vec::new();
while let Some(node) = dfs.next(graph) {
let block_id = graph[node];
sorted.push(block_id);
}
sorted.reverse();
sorted
.into_iter()
.map(|block_id| {
let block = control_flow_graph
.get_block(block_id)
.expect("no such block");
block.clone()
})
.collect()
}