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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
use std::collections::{BTreeMap, HashMap};
use log::{debug, trace};
use super::ir::{BlockId, ControlFlowGraph, Instruction, Value, Variable};
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,
insts: BTreeMap<usize, Instruction>,
throw_to_handlers: HashMap<BlockId, Vec<BlockId>>,
/// Keep every variable in a fixed stack slot, using registers only as
/// per-instruction temporaries.
///
/// Default. A single-pass, layout-ordered code generator cannot know which
/// predecessor ran, so a value that this pass believes is in a register may
/// never have been written on the path that is actually taken. Addressing
/// variables through memory removes that entire class of bug. It is also
/// faster in practice: no registers have to be saved around a call.
/// `BIUJS_REG_VARS` opts into the register path (see `begin_block` /
/// `spill_live_out`), which keeps values in registers inside a block and
/// only routes them through memory at block boundaries.
memory_resident_vars: bool,
}
impl Codegen {
pub fn new(registers: &[Register], throw_to_handlers: HashMap<BlockId, Vec<BlockId>>) -> Self {
Self {
reg_alloc: RegAlloc::new(registers),
codes: Vec::new(),
block_map: HashMap::new(),
inst_index: 0,
insts: BTreeMap::new(),
throw_to_handlers,
// Memory-resident by default; opt into the register path explicitly.
memory_resident_vars: std::env::var("BIUJS_REG_VARS").is_err(),
}
}
pub fn generate_code(&mut self, cfg: ControlFlowGraph) -> &[Bytecode] {
// debug ir
log::debug!("=== IR CFG ===");
for block in cfg.blocks() {
log::debug!("B{} (params: {:?}):", block.id(), block.params());
for inst in block.instructions() {
log::debug!(" {}", inst);
}
}
log::debug!("=== end IR ===");
let block_layout = cfg.loop_root_reverse_postorder_layout2();
// A value can escape to an exception handler from the middle of a block,
// which the per-terminator hand-off of the register path cannot cover.
// Functions with a handler therefore always keep variables in memory,
// so any handler can reload them whichever instruction threw.
let uses_exception_handlers = cfg.blocks().iter().any(|block| {
block
.instructions()
.iter()
.any(|inst| matches!(inst, Instruction::PushSeh { .. }))
});
if uses_exception_handlers {
self.memory_resident_vars = true;
}
self.reg_alloc.arrange(&cfg, &block_layout);
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 block_layout.iter(&cfg) {
self.block_map
.insert(block.id().as_usize() as isize, self.codes.len() as isize);
// No register state survives a block boundary; each block reloads
// what it needs from memory (see `RegAlloc::begin_block`).
self.reg_alloc.begin_block();
for inst in block.instructions() {
// Hand the values a successor still needs over through memory
// before control leaves the block.
if inst.is_terminator() {
for (register, stack) in self.reg_alloc.spill_live_out(block.id()) {
trace!("block-end spill [rbp+{stack}] <- {register}");
self.codes.push(Bytecode::double(
Opcode::Mov,
Operand::Stack(stack as isize),
register.into(),
));
}
}
debug!("inst[{}]: {inst:?}", self.inst_index);
debug!("register: {}", self.reg_alloc.reg_set);
self.insts.insert(self.codes.len(), inst.clone());
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));
}
// Collection / Structural Operations
Instruction::MakeArray { dst } => {
let dst = self.gen_operand(dst);
self.codes.push(Bytecode::single(Opcode::MakeArray, dst));
}
Instruction::ArrayPushSpread { array, src } => {
let array = self.gen_operand(array);
let src = self.gen_operand(src);
self.codes.push(Bytecode::double(
Opcode::ArrayPushSpread,
array,
src,
));
}
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::MakeObject { dst } => {
let dst = self.gen_operand(dst);
self.codes.push(Bytecode::single(Opcode::MakeObject, 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::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::PropertyDelete {
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::PropDelete,
dst,
object,
property,
));
}
Instruction::IndexDelete {
dst,
object,
index,
} => {
let dst = self.gen_operand(dst);
let object = self.gen_operand(object);
let index = self.gen_operand(index);
self.codes
.push(Bytecode::triple(Opcode::IndexDelete, dst, object, index));
}
Instruction::StoreEnv { name, value } => {
let name = name.to_operand();
let value = self.gen_operand(value);
self.codes
.push(Bytecode::double(Opcode::StoreEnv, name, value));
}
Instruction::Arguments { dst } => {
let dst = self.gen_operand(dst);
self.codes.push(Bytecode::single(Opcode::Arguments, dst));
}
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));
}
// 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::IteratorClose { iter } => {
let iter = self.gen_operand(iter);
self.codes.push(Bytecode::single(Opcode::IterClose, iter));
}
Instruction::Yield { dst, src } => {
let dst = self.gen_operand(dst);
let src = match src {
Some(src) => self.gen_operand(src),
None => Operand::new_immd(-1),
};
self.codes.push(Bytecode::double(Opcode::Yield, dst, src));
}
Instruction::ToString { dst, src } => {
let dst = self.gen_operand(dst);
let src = self.gen_operand(src);
self.codes.push(Bytecode::double(Opcode::ToString, dst, src));
}
Instruction::MakeRest { dst, from } => {
let dst = self.gen_operand(dst);
self.codes.push(Bytecode::double(
Opcode::MakeRest,
dst,
Operand::new_immd(from as isize),
));
}
Instruction::CallSpread {
result,
callee,
this,
args,
} => {
let callee = self.gen_operand(callee);
let this = self.gen_operand(this);
let args = self.gen_operand(args);
// The callee runs in a nested frame and freely reuses
// registers, so live values must be saved across it —
// same contract as `gen_call`.
let in_use_registers = self.call_saved_registers();
for reg in in_use_registers.iter().copied() {
self.codes.push(Bytecode::single(Opcode::Push, reg.into()));
}
self.codes
.push(Bytecode::triple(Opcode::CallSpread, callee, this, args));
for reg in in_use_registers.iter().rev().copied() {
self.codes.push(Bytecode::single(Opcode::Pop, reg.into()));
}
// The result travels in Rv (same as CallMethod).
let result = self.gen_operand(result);
self.codes.push(Bytecode::double(
Opcode::Mov,
result,
Operand::new_register(Register::Rv),
));
}
Instruction::CallSuper {
result,
callee,
this,
args,
} => {
let callee = self.gen_operand(callee);
let this = self.gen_operand(this);
let args = self.gen_operand(args);
// Same register contract as `CallSpread`: the callee runs
// in a nested frame and reuses registers.
let in_use_registers = self.call_saved_registers();
for reg in in_use_registers.iter().copied() {
self.codes.push(Bytecode::single(Opcode::Push, reg.into()));
}
self.codes.push(Bytecode::triple(
Opcode::CallSuperSpread,
callee,
this,
args,
));
for reg in in_use_registers.iter().rev().copied() {
self.codes.push(Bytecode::single(Opcode::Pop, reg.into()));
}
let result = self.gen_operand(result);
self.codes.push(Bytecode::double(
Opcode::Mov,
result,
Operand::new_register(Register::Rv),
));
}
Instruction::NewSpread {
dst,
constructor,
args,
} => { let dst = self.gen_operand(dst);
let ctor = self.gen_operand(constructor);
let args = self.gen_operand(args);
let in_use_registers = self.call_saved_registers();
for reg in in_use_registers.iter().copied() {
self.codes.push(Bytecode::single(Opcode::Push, reg.into()));
}
self.codes
.push(Bytecode::triple(Opcode::NewSpread, dst, ctor, args));
for reg in in_use_registers.iter().rev().copied() {
self.codes.push(Bytecode::single(Opcode::Pop, reg.into()));
}
}
Instruction::IterateNext {
iter,
item,
has_next,
} => {
let src = self.gen_operand(iter);
let dst = self.gen_operand(item);
let has_next = self.gen_operand(has_next);
self.codes
.push(Bytecode::triple(Opcode::IterNext, dst, has_next, src));
}
// JS-specific Instructions
Instruction::TypeOf { dst, src } => {
let dst = self.gen_operand(dst);
let src = self.gen_operand(src);
self.codes.push(Bytecode::double(Opcode::TypeOf, dst, src));
}
Instruction::TypeOfEnv { dst, name } => {
let dst = self.gen_operand(dst);
self.codes
.push(Bytecode::double(Opcode::TypeOfEnv, dst, name.to_operand()));
}
Instruction::New {
dst,
constructor,
args,
} => {
self.gen_new(constructor, &args, dst);
}
Instruction::LoadThis { dst } => {
let dst = self.gen_operand(dst);
self.codes.push(Bytecode::single(Opcode::LoadThis, dst));
}
Instruction::LoadNewTarget { dst } => {
let dst = self.gen_operand(dst);
self.codes
.push(Bytecode::single(Opcode::LoadNewTarget, dst));
}
Instruction::LoadCurrentFunction { dst } => {
let dst = self.gen_operand(dst);
self.codes
.push(Bytecode::single(Opcode::LoadCurrentFunction, dst));
}
Instruction::MakeFuncObj { dst, func_id } => {
let dst = self.gen_operand(dst);
let func_id = self.gen_operand(func_id);
self.codes
.push(Bytecode::double(Opcode::MakeFuncObj, dst, func_id));
}
Instruction::MakeArrowFuncObj {
dst,
func_id,
captured_this,
} => {
let dst = self.gen_operand(dst);
let func_id = self.gen_operand(func_id);
let captured_this = self.gen_operand(captured_this);
self.codes.push(Bytecode::triple(
Opcode::MakeArrowFuncObj,
dst,
func_id,
captured_this,
));
}
Instruction::ClosureVar { name, value } => {
let name = name.to_operand();
let value = self.gen_operand(value);
self.codes
.push(Bytecode::double(Opcode::ClosureVar, name, value));
}
// 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,
));
} else {
// A value-less `return` (or falling off the end of a
// function) yields `undefined`. Rv still holds the
// result of the last call otherwise, which would make
// a constructor "return" that leftover object.
self.codes.push(Bytecode::double(
Opcode::Mov,
Operand::new_register(Register::Rv),
Operand::new_primitive(crate::bytecode::Primitive::Undefined),
));
}
self.codes.push(Bytecode::empty(Opcode::Ret));
}
Instruction::Jump { dst, args } => {
// Hand each block parameter over through its stack slot
// (see `store_jump_args` for why it must not travel in a
// register only).
let params = cfg
.get_block(dst.to_block())
.map(|b| b.params())
.unwrap_or_default();
self.store_jump_args(params, &args);
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::Jump, dst));
}
Instruction::BrIf {
condition,
true_blk,
false_blk,
true_args,
false_args,
} => {
// Hand both branches' parameters over through memory.
let true_params = cfg
.get_block(true_blk.to_block())
.map(|b| b.params())
.unwrap_or_default();
self.store_jump_args(true_params, &true_args);
let false_params = cfg
.get_block(false_blk.to_block())
.map(|b| b.params())
.unwrap_or_default();
self.store_jump_args(false_params, &false_args);
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 { value } => {
if let Some(v) = value {
let src = self.gen_operand(v);
self.codes.push(Bytecode::double(
Opcode::Mov,
Operand::new_register(Register::Rv),
src,
));
}
self.codes.push(Bytecode::empty(Opcode::Halt));
}
Instruction::PushSeh { handler, finally } => {
let handler_id = handler.as_usize() as isize;
let pos = self.codes.len();
patchs.push(Box::new(move |this: &mut Self| {
// Patch catch handler offset
this.codes[pos].operands[0] =
Operand::new_immd(this.block_map[&handler_id] - pos as isize);
// Patch finally handler offset if present
if let Some(finally_blk) = finally {
let finally_id = finally_blk.as_usize() as isize;
this.codes[pos].operands[1] =
Operand::new_immd(this.block_map[&finally_id] - pos as isize);
}
}));
// Use triple to hold both catch and finally offsets
self.codes.push(Bytecode::triple(
Opcode::Try,
Operand::new_immd(0),
Operand::new_immd(0),
Operand::new_immd(0),
));
}
Instruction::PopSeh => {
self.codes.push(Bytecode::empty(Opcode::EndTry));
}
Instruction::Throw { value, args } => {
// 为异常handler的phi参数生成mov指令
// 仅处理异常handler后继块,忽略正常跳转目标(死代码)
let handlers: Vec<BlockId> = self
.throw_to_handlers
.get(&block.id())
.map(|h| h.clone())
.unwrap_or_default();
for &succ_id in cfg.get_successors(block.id()) {
// 跳过非异常handler的后继块
if !handlers.contains(&succ_id) {
continue;
}
if let Some(succ_block) = cfg.get_block(succ_id) {
let params = succ_block.params();
if params.is_empty() {
continue;
}
// 这个后继块是catch handler,传递phi参数
self.store_jump_args(params, &args);
}
}
let val = self.gen_operand(value);
self.codes.push(Bytecode::single(Opcode::ThrowExc, val));
}
Instruction::LoadException { dst } => {
let reg = self.gen_operand(dst);
self.codes
.push(Bytecode::single(Opcode::LoadException, reg));
}
Instruction::ResumeException { args } => {
let handlers: Vec<BlockId> = self
.throw_to_handlers
.get(&block.id())
.map(|h| h.clone())
.unwrap_or_default();
for &succ_id in cfg.get_successors(block.id()) {
if !handlers.contains(&succ_id) {
continue;
}
if let Some(succ_block) = cfg.get_block(succ_id) {
let params = succ_block.params();
if params.is_empty() {
continue;
}
self.store_jump_args(params, &args);
}
}
self.codes.push(Bytecode::empty(Opcode::ResumeExc));
}
Instruction::DelayedJump { target, seh_depth } => {
let target_op = Operand::new_immd(target.as_usize() as isize);
let seh_depth_op = Operand::new_immd(seh_depth as isize);
let pos = self.codes.len();
patchs.push(Box::new(move |this: &mut Self| {
let target = this.codes[pos].operands[0].as_immd() as usize;
// The target is consumed later by `ResumeExc`, at the
// end of the finally block, so it must be an absolute
// PC: a pc-relative offset would be applied from the
// wrong instruction.
let absolute = this.block_map[&(target as isize)];
this.codes[pos].operands[0] = Operand::new_immd(absolute);
}));
self.codes.push(Bytecode::double(
Opcode::DelayedJump,
target_op,
seh_depth_op,
));
}
}
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.call_saved_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),
));
// 4. Call the function (the argument count travels along so the callee
// can materialise `arguments`).
self.codes.push(Bytecode::double(
Opcode::Call,
func.to_operand(),
Operand::new_immd(args.len() as isize),
));
// 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.call_saved_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),
));
// 4. Call the function (CallEx). The argument count travels with the
// instruction so the VM can read the pushed arguments when the
// callee turns out to be a native built-in.
self.codes.push(Bytecode::double(
Opcode::CallEx,
callable,
Operand::new_immd(args.len() as isize),
));
// 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),
));
// 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. Backup used registers
let in_use_registers = self.call_saved_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);
// 2. Set up new stack frame
self.codes.push(Bytecode::single(
Opcode::PushC,
Operand::new_register(Register::Rbp),
));
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. Restore backed-up registers
for reg in in_use_registers.iter().rev().copied() {
self.codes.push(Bytecode::single(Opcode::Pop, reg.into()));
}
// 8. 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_new(&mut self, constructor: Value, args: &[Value], result: Value) {
let callable = self.gen_operand(constructor);
// 1. Backup used registers
let in_use_registers = self.call_saved_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),
));
// 4. Call the constructor with New opcode (passing arg count)
self.codes.push(Bytecode::double(
Opcode::New,
callable,
Operand::new_immd(args.len() as isize),
));
// 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),
));
}
/// Registers that have to survive a nested call.
///
/// With variables resident in memory nothing is held in a register across a
/// call, so the callee cannot clobber a value the caller still needs.
fn call_saved_registers(&self) -> Vec<Register> {
if self.memory_resident_vars {
Vec::new()
} else {
self.reg_alloc.call_saved_registers()
}
}
/// Hand a block's parameters over at a jump.
///
/// A block parameter is not produced by an instruction inside its own block:
/// it is written by every predecessor's terminator. The value therefore
/// always travels through the parameter's dedicated stack slot, and the
/// block entry reloads it from there. Delivering only through a register is
/// unsound here: the predecessor and the target can be generated in either
/// order, so a register hand-off written by one edge is invisible (or stale)
/// when the block itself is generated — which is how a loop header ended up
/// reading a stale stack slot while the back edge updated only the register,
/// leaving the loop counter frozen.
fn store_jump_args(&mut self, params: &[Variable], args: &[Value]) {
for (param, arg) in params.iter().zip(args.iter()) {
let arg_op = self.gen_operand(*arg);
let stack = self.reg_alloc.ensure_stack_slot(*param);
self.codes.push(Bytecode::double(
Opcode::Mov,
Operand::Stack(stack as isize),
arg_op,
));
// The slot now holds the incoming value; any register copy is stale,
// so the block entry must reload from memory.
self.reg_alloc.mark_stack_written(*param);
self.reg_alloc.forget_register(param);
}
}
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 Value::Variable(arg) = arg
&& 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"),
}
}
}
}
/// Emit the bookkeeping that comes with a register allocation.
///
/// `alloc` may report that a victim was spilled (`Action::Spill`) and/or
/// that the value itself must be reloaded from its stack slot
/// (`Action::Restore`). The actions are emitted in the order given: a spill
/// must reach memory before the register is overwritten by a restore.
/// Callers that only take the register silently drop that work and lose
/// values.
fn apply_alloc_actions(&mut self, actions: Vec<Action>) {
for action in actions {
match action {
Action::Spill { register, stack } => {
trace!("spilling [rbp+{stack}] <- {register}");
self.codes.push(Bytecode::double(
Opcode::Mov,
Operand::Stack(stack as isize),
register.into(),
));
}
Action::Restore { register, stack } => {
trace!("unspilling [rbp+{stack}] -> {register}");
self.codes.push(Bytecode::double(
Opcode::Mov,
register.into(),
Operand::Stack(stack as isize),
));
}
}
}
}
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(var) if self.memory_resident_vars => {
// Memory-resident mode: every variable lives in a fixed stack
// slot, so no value can be lost by register reuse.
let stack = self.reg_alloc.ensure_stack_slot(var);
Operand::Stack(stack as isize)
}
Value::Variable(var) => {
let (register, actions) = self.reg_alloc.alloc(var, self.inst_index);
trace!("allocating {value} -> {register}, actions = {actions:?}");
self.apply_alloc_actions(actions);
Operand::new_register(register)
}
}
}
#[allow(dead_code)]
fn emit_code(&mut self, code: Bytecode) {
self.codes.push(code);
}
pub fn debug_insts(&self) -> &BTreeMap<usize, Instruction> {
&self.insts
}
}