mimium-lang 4.0.0-alpha

mimium(minimal-musical-medium) an infrastructural programming language for sound and music.
Documentation
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
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
use std::collections::HashMap;
use std::sync::Arc;

use crate::interner::{Symbol, TypeNodeId};
use crate::mir::{self, Mir};
use crate::runtime::vm::bytecode::{ConstPos, GlobalPos, Reg};
use crate::runtime::vm::program::{JumpTable, WordSize};
use crate::runtime::vm::{self, StateOffset};
use crate::types::{PType, RecordTypeField, Type, TypeSize};
use crate::utils::half_float::HFloat;
use vm::bytecode::Instruction as VmInstruction;

#[derive(Debug, Default, Clone, Copy)]
struct MemoryRegion(Reg, TypeSize);
#[derive(Debug, Default)]
struct VRegister(HashMap<Arc<mir::Value>, MemoryRegion>);

impl VRegister {
    pub fn push_stack(&mut self, v: &Arc<mir::Value>, size: u64) -> Reg {
        self.add_newvalue_range(v, size)
    }
    pub fn add_newvalue(&mut self, v: &Arc<mir::Value>) -> Reg {
        let pos = self
            .0
            .iter()
            .max_by_key(|(_v, MemoryRegion(address, size))| address + size)
            .map(|(_, MemoryRegion(address, size))| address + size)
            .unwrap_or(0);
        self.0.insert(v.clone(), MemoryRegion(pos, 1));
        log::trace!("add {:?}", self.0);
        pos as Reg
    }
    pub fn add_newvalue_range(&mut self, v: &Arc<mir::Value>, size: u64) -> Reg {
        let pos = self
            .0
            .iter()
            .max_by_key(|(_v, MemoryRegion(address, size))| address + size)
            .map(|(_, MemoryRegion(address, size))| address + size)
            .unwrap_or(0);
        self.0.insert(v.clone(), MemoryRegion(pos, size as _));
        log::trace!("add_range {:#?}", self.0);
        pos as Reg
    }
    pub fn find(&mut self, v: &Arc<mir::Value>) -> Option<Reg> {
        log::trace!("find {v}");
        let res = self.0.get(v).map(|r| r.0);
        match (res, v.as_ref()) {
            //argument is registered in absolute position
            (Some(_), mir::Value::Argument(_)) | (Some(_), mir::Value::Global(_)) => res,
            (Some(_), _) => {
                self.0.remove(v);
                res
            }
            _ => None,
        }
    }
    //find for load and store instruction
    pub fn find_keep(&self, v: &Arc<mir::Value>) -> Option<Reg> {
        log::trace!("findkeep {v}");
        self.0.get(v).map(|r| r.0)
    }
}

#[derive(Debug, Default)]
struct VStack(Vec<VRegister>);
impl VStack {
    fn get_top(&mut self) -> &mut VRegister {
        self.0.last_mut().unwrap()
    }
    fn find_upvalue(&self, v: &Arc<mir::Value>) -> Option<Reg> {
        self.0
            .iter()
            .rev()
            .skip(1)
            .find_map(|vreg| vreg.find_keep(v))
    }
    pub fn push_stack(&mut self, v: &Arc<mir::Value>, size: u64) -> Reg {
        self.get_top().push_stack(v, size)
    }
    pub fn add_newvalue(&mut self, v: &Arc<mir::Value>) -> Reg {
        self.get_top().add_newvalue(v)
    }
    pub fn find(&mut self, v: &Arc<mir::Value>) -> Option<Reg> {
        self.get_top().find(v)
    }
    pub fn find_keep(&mut self, v: &Arc<mir::Value>) -> Option<Reg> {
        self.get_top().find_keep(v)
    }
}

#[derive(Debug, Default)]
pub struct ByteCodeGenerator {
    vregister: VStack,
    varray: Vec<Arc<mir::Value>>,
    fnmap: HashMap<Symbol, usize>,
    globals: HashMap<Arc<mir::Value>, usize>, //index to Program.global_vals
    program: vm::Program,
}

/// Option for scecifying the evaluation strategy of the function that uses `self`.
/// `SimpleState` inteprets `self` as simply internal state of the function which is initialized as 0 at time = 0.
/// `ZeroAtInit` inteprets *the return value of the function which uses `self`* returns 0 at time = 0.
/// For example, `| | {self + 1}` will return 1 at time = 0 in `SimpleState` mode, but 0 in `ZeroAtInit` mode.
/// For most of the cases, `SimpleState` is the default and recommended mode.
#[derive(Default, Debug, Clone, Copy)]
pub enum SelfEvalMode {
    #[default]
    SimpleState,
    ZeroAtInit,
}
#[derive(Default, Debug, Clone, Copy)]
pub struct Config {
    pub self_eval_mode: SelfEvalMode,
}

fn gen_raw_int(n: &i64) -> vm::RawVal {
    let raw = {
        let iptr = n as *const i64;
        iptr as *const vm::RawVal
    };
    unsafe { *raw }
}

fn gen_raw_float(n: &f64) -> vm::RawVal {
    let raw = {
        let iptr = n as *const f64;
        iptr as *const vm::RawVal
    };
    unsafe { *raw }
}

impl ByteCodeGenerator {
    /// Calculate byte size of the value for type T based on 1 word size (=currently 64bit).
    /// The base word size may change depending on the backend in the future.
    /// Calculate word size for a type. Now delegates to TypeNodeId::word_size().
    pub(crate) fn word_size_for_type(ty: TypeNodeId) -> TypeSize {
        ty.word_size()
    }

    fn get_binop(&mut self, v1: Arc<mir::Value>, v2: Arc<mir::Value>) -> (Reg, Reg) {
        let r1 = self.find(&v1);
        let r2 = self.find(&v2);
        (r1, r2)
    }
    fn emit_binop1<F>(
        &mut self,
        inst: F,
        dst: Arc<mir::Value>,
        v1: Arc<mir::Value>,
    ) -> Option<VmInstruction>
    where
        F: FnOnce(Reg, Reg) -> VmInstruction,
    {
        let r1 = self.find(&v1);
        let dst = self.get_destination(dst.clone(), 1);
        let i = inst(dst, r1);
        Some(i)
    }
    fn emit_binop2<F>(
        &mut self,
        inst: F,
        dst: Arc<mir::Value>,
        v1: Arc<mir::Value>,
        v2: Arc<mir::Value>,
    ) -> Option<VmInstruction>
    where
        F: FnOnce(Reg, Reg, Reg) -> VmInstruction,
    {
        //the order matters! get destination later on arguments
        let (r1, r2) = self.get_binop(v1, v2);
        let dst = self.get_destination(dst.clone(), 1);
        let i = inst(dst, r1, r2);
        Some(i)
    }
    fn get_destination(&mut self, dst: Arc<mir::Value>, size: TypeSize) -> Reg {
        self.vregister.push_stack(&dst, size as _)
    }
    fn get_or_insert_global(&mut self, gv: Arc<mir::Value>, ty: TypeNodeId) -> GlobalPos {
        match self.globals.get(&gv) {
            Some(idx) => *idx as GlobalPos,
            None => {
                let size = Self::word_size_for_type(ty) as usize;
                // The start offset is the sum of all existing global sizes.
                let idx: usize = self.program.global_vals.iter().map(|w| w.0 as usize).sum();
                self.globals.insert(gv.clone(), idx);
                let size = WordSize(size as _);
                self.program.global_vals.push(size);
                idx as GlobalPos
            }
        }
    }
    fn find(&mut self, v: &Arc<mir::Value>) -> Reg {
        if let mir::Value::Function(idx) = v.as_ref() {
            return *idx as Reg;
        }
        self.vregister
            .find(v)
            .or_else(|| self.globals.get(v).map(|&v| v as Reg))
            .or_else(|| self.varray.iter().position(|av| v == av).map(|v| v as Reg))
            .expect(format!("value {v} not found").as_str())
    }
    fn find_keep(&mut self, v: &Arc<mir::Value>) -> Reg {
        self.vregister
            .find_keep(v)
            .or_else(|| self.globals.get(v).map(|&v| v as Reg))
            .expect(format!("value {v} not found").as_str())
    }
    fn find_upvalue(&self, upval: &Arc<mir::Value>) -> Reg {
        self.vregister
            .find_upvalue(upval)
            .expect("failed to find upvalue")
    }
    fn prepare_function(
        &mut self,
        bytecodes_dst: &mut Vec<VmInstruction>,
        faddress: &Arc<mir::Value>,
        args: &[(Arc<mir::Value>, TypeNodeId)],
    ) -> (Reg, TypeSize) {
        let mut aoffsets = vec![];
        let mut offset: TypeSize = 0;
        for (a, ty) in args.iter() {
            let src = self.find(a);
            let size = Self::word_size_for_type(*ty);
            aoffsets.push((offset, src, size));
            offset = offset.checked_add(size).unwrap_or_else(|| {
                panic!(
                    "bytecodegen: offset overflow in prepare_function: offset={}, size={}, type={:?}",
                    offset, size, ty.to_type()
                )
            });
        }
        let faddress = self.find_keep(faddress);
        let placements: Vec<(TypeSize, Reg, TypeSize)> = aoffsets
            .iter()
            .map(|(adst, src, size)| {
                let address = adst
                    .checked_add(faddress)
                    .and_then(|v| v.checked_add(1))
                    .unwrap_or_else(|| {
                        panic!(
                            "bytecodegen: address overflow in prepare_function: adst={}, faddress={}",
                            adst, faddress
                        )
                    });
                (address, *src, *size)
            })
            .collect();

        let ranges_overlap =
            |a_start: TypeSize, a_size: TypeSize, b_start: TypeSize, b_size: TypeSize| {
                let a_end = a_start + a_size;
                let b_end = b_start + b_size;
                a_start < b_end && b_start < a_end
            };

        let has_overlap = placements.iter().any(|(dst, _src, dsize)| {
            placements.iter().any(|(_odst, osrc, osize)| {
                ranges_overlap(*dst, *dsize, *osrc, *osize) && !(*dst == *osrc && *dsize == *osize)
            })
        });

        let staged: Vec<(TypeSize, Reg, TypeSize)> = if has_overlap {
            placements
                .iter()
                .enumerate()
                .map(|(idx, (dst, src, size))| {
                    let temp_key = Arc::new(mir::Value::Register(u64::MAX - idx as u64));
                    let tmp = self
                        .vregister
                        .get_top()
                        .add_newvalue_range(&temp_key, *size as u64);
                    match size {
                        0 => unreachable!(),
                        1 => bytecodes_dst.push(VmInstruction::Move(tmp, *src)),
                        _ => bytecodes_dst.push(VmInstruction::MoveRange(tmp, *src, *size)),
                    }
                    (*dst, tmp, *size)
                })
                .collect()
        } else {
            placements
        };

        for (dst, src, size) in staged.iter() {
            let is_samedst = *dst == *src;
            if !is_samedst {
                match size {
                    0 => unreachable!(),
                    1 => bytecodes_dst.push(VmInstruction::Move(*dst as Reg, *src)),
                    _ => bytecodes_dst.push(VmInstruction::MoveRange(*dst as Reg, *src, *size)),
                }
            }
        }
        (faddress, offset)
    }
    fn get_or_insert_extfunid(&mut self, label: Symbol, ty: TypeNodeId) -> ConstPos {
        self.program
            .ext_fun_table
            .iter()
            .position(|(name, existing_ty)| name.as_str() == label.as_str() && *existing_ty == ty)
            .unwrap_or_else(|| {
                self.program.ext_fun_table.push((label.to_string(), ty));
                self.program.ext_fun_table.len() - 1
            }) as ConstPos
    }
    // fn get_or_insert_extclsid(&mut self, label: Symbol, ty: TypeNodeId) -> ConstPos {
    //     self.program
    //         .ext_cls_table
    //         .iter()
    //         .position(|(name, _ty)| *name == label)
    //         .unwrap_or_else(|| {
    //             self.program.ext_cls_table.push((label, ty));
    //             self.program.ext_cls_table.len() - 1
    //         }) as ConstPos
    // }
    fn prepare_extfun_or_cls(
        &mut self,
        funcproto: &mut vm::FuncProto,
        bytecodes_dst: Option<&mut Vec<VmInstruction>>,
        dst: Arc<mir::Value>,
        args: &[(Arc<mir::Value>, TypeNodeId)],
        idx: ConstPos,
        ty: TypeNodeId,
    ) -> (Reg, Reg, TypeSize) {
        let fi = funcproto.add_new_constant(idx as u64);
        let rsize = Self::word_size_for_type(ty);
        let bytecodes_dst = bytecodes_dst.unwrap_or_else(|| funcproto.bytecodes.as_mut());
        let f = self.vregister.push_stack(&dst, rsize as _);
        bytecodes_dst.push(VmInstruction::MoveConst(f, fi as ConstPos));
        let (dst, argsize) = self.prepare_function(bytecodes_dst, &dst, args);
        (dst, argsize, rsize)
    }
    fn prepare_extfun(
        &mut self,
        funcproto: &mut vm::FuncProto,
        bytecodes_dst: Option<&mut Vec<VmInstruction>>,
        dst: Arc<mir::Value>,
        args: &[(Arc<mir::Value>, TypeNodeId)],
        label: Symbol,
        fn_ty: TypeNodeId,
        ret_ty: TypeNodeId,
    ) -> (Reg, Reg, TypeSize) {
        let idx = self.get_or_insert_extfunid(label, fn_ty);
        self.prepare_extfun_or_cls(funcproto, bytecodes_dst, dst, args, idx, ret_ty)
    }
    // fn prepare_extcls(
    //     &mut self,
    //     funcproto: &mut vm::FuncProto,
    //     bytecodes_dst: Option<&mut Vec<VmInstruction>>,
    //     dst: Arc<mir::Value>,
    //     args: &[(Arc<mir::Value>, TypeNodeId)],
    //     label: Symbol,
    //     ty: TypeNodeId,
    // ) -> (Reg, Reg, TypeSize) {
    //     let idx = self.get_or_insert_extclsid(label, ty);
    //     self.prepare_extfun_or_cls(funcproto, bytecodes_dst, dst, args, idx, ty)
    // }
    fn emit_instruction(
        &mut self,
        funcproto: &mut vm::FuncProto,
        bytecodes_dst: Option<&mut Vec<VmInstruction>>,
        mirfunc: mir::Function,
        dst: Arc<mir::Value>,
        mirinst: mir::Instruction,
        config: Config,
    ) -> Option<VmInstruction> {
        match mirinst {
            mir::Instruction::Uinteger(u) => {
                let pos = funcproto.add_new_constant(u);
                Some(VmInstruction::MoveConst(
                    self.get_destination(dst, 1),
                    pos as ConstPos,
                ))
            }
            mir::Instruction::Integer(i) => {
                let pos = funcproto.add_new_constant(gen_raw_int(&i));
                Some(VmInstruction::MoveConst(
                    self.get_destination(dst, 1),
                    pos as ConstPos,
                ))
            }
            mir::Instruction::Float(n) => {
                let dst = self.get_destination(dst, 1);
                if let Ok(half_f) = HFloat::try_from(n) {
                    Some(VmInstruction::MoveImmF(dst, half_f))
                } else {
                    let pos = funcproto.add_new_constant(gen_raw_float(&n));
                    Some(VmInstruction::MoveConst(dst, pos as ConstPos))
                }
            }
            mir::Instruction::String(s) => {
                let pos = self.program.add_new_str(s.to_string());
                let cpos = funcproto.add_new_constant(pos as u64);
                Some(VmInstruction::MoveConst(
                    self.get_destination(dst, 1),
                    cpos as ConstPos,
                ))
            }
            mir::Instruction::Alloc(t) => {
                let size = Self::word_size_for_type(t) as u64;
                let _ = self.vregister.push_stack(&dst, size);
                None
            }
            mir::Instruction::Load(ptr, ty) => {
                let d = self.get_destination(dst, Self::word_size_for_type(ty));
                let s = self.find_keep(&ptr);
                let size = Self::word_size_for_type(ty);
                match (d, s, size) {
                    (d, s, 1) if d != s => Some(VmInstruction::Move(d, s)),
                    (d, s, size) if d != s => Some(VmInstruction::MoveRange(d, s, size)),
                    _ => None,
                }
            }
            mir::Instruction::Store(dst, src, ty) => {
                let s = self.find(&src);
                let d = self.find_keep(&dst);
                let size = Self::word_size_for_type(ty);
                match (d, s, size) {
                    (d, s, 1) if d != s => Some(VmInstruction::Move(d, s)),
                    (d, s, size) if d != s => Some(VmInstruction::MoveRange(d, s, size)),
                    _ => None,
                }
            }
            mir::Instruction::GetGlobal(v, ty) => {
                let dst = self.get_destination(dst, Self::word_size_for_type(ty));
                let idx = self.get_or_insert_global(v.clone(), ty);
                Some(VmInstruction::GetGlobal(
                    dst,
                    idx,
                    Self::word_size_for_type(ty),
                ))
            }
            mir::Instruction::SetGlobal(v, src, ty) => {
                let idx = self.get_or_insert_global(v.clone(), ty);
                let s = self.find(&src);
                Some(VmInstruction::SetGlobal(
                    idx,
                    s,
                    Self::word_size_for_type(ty),
                ))
            }
            mir::Instruction::GetElement {
                value,
                ty,
                tuple_offset,
            } => {
                let ptr = self.find_keep(&value) as usize;
                let ty = ty.to_type();
                let elem_tys: Vec<TypeNodeId> = match ty {
                    Type::Tuple(elems) => elems,
                    Type::Record(fields) => fields.iter().map(|f| f.ty).collect(),
                    _ => panic!("GetElement expects tuple or record type, got {:?}", ty),
                };
                let tsize = Self::word_size_for_type(elem_tys[tuple_offset as usize]);
                let t_offset: u64 = elem_tys[0..(tuple_offset as _)]
                    .iter()
                    .map(|t| Self::word_size_for_type(*t) as u64)
                    .sum();
                let address = (ptr + t_offset as usize) as Reg;
                self.vregister
                    .get_top()
                    .0
                    .insert(dst, MemoryRegion(address, tsize));
                None
            }
            mir::Instruction::Call(v, args, r_ty) => {
                let rsize = Self::word_size_for_type(r_ty);
                match v.as_ref() {
                    mir::Value::Register(_address) => {
                        let bytecodes_dst =
                            bytecodes_dst.unwrap_or_else(|| funcproto.bytecodes.as_mut());
                        let d = self.get_destination(dst.clone(), rsize);
                        let s = self.find(&v);
                        bytecodes_dst.push(VmInstruction::Move(d, s));
                        let (fadd, argsize) = self.prepare_function(bytecodes_dst, &dst, &args);
                        Some(VmInstruction::Call(fadd, argsize as u8, rsize))
                    }
                    mir::Value::Function(_idx) => {
                        unreachable!();
                    }
                    mir::Value::ExtFunction(label, f_ty) => {
                        //todo: use btreemap
                        let (dst, argsize, nret) = self.prepare_extfun(
                            funcproto,
                            bytecodes_dst,
                            dst,
                            &args,
                            *label,
                            *f_ty,
                            r_ty,
                        );
                        Some(VmInstruction::CallExtFun(dst, argsize as u8, nret))
                    }
                    _ => unreachable!(),
                }
            }
            mir::Instruction::CallCls(f, args, r_ty) => {
                let rsize = Self::word_size_for_type(r_ty);
                match f.as_ref() {
                    mir::Value::Register(_address) => {
                        let bytecodes_dst =
                            bytecodes_dst.unwrap_or_else(|| funcproto.bytecodes.as_mut());

                        let (fadd, argsize) = self.prepare_function(bytecodes_dst, &f, &args);
                        let s = self.find(&f);
                        let d = self.get_destination(dst.clone(), rsize);
                        bytecodes_dst.push(VmInstruction::CallCls(fadd, argsize as u8, rsize));
                        match rsize {
                            0 => None,
                            1 => Some(VmInstruction::Move(d, s)),
                            n => Some(VmInstruction::MoveRange(d, s, n)),
                        }
                    }
                    mir::Value::Function(_idx) => {
                        unreachable!();
                    }
                    mir::Value::ExtFunction(label, f_ty) => {
                        let (dst, argsize, nret) = self.prepare_extfun(
                            funcproto,
                            bytecodes_dst,
                            dst,
                            &args,
                            *label,
                            *f_ty,
                            r_ty,
                        );
                        Some(VmInstruction::CallExtFun(dst, argsize as u8, nret))
                    }
                    _ => unreachable!(),
                }
            }
            mir::Instruction::Closure(idxcell) => {
                let idx = match idxcell.as_ref() {
                    mir::Value::Function(idx) => *idx as Reg,
                    _ => self.find(&idxcell),
                };
                let dst = self.get_destination(dst, 1);
                Some(VmInstruction::Closure(dst, idx))
            }
            // New heap-based instructions (Phase 3)
            mir::Instruction::MakeClosure { fn_proto, size } => {
                let fn_idx = match fn_proto.as_ref() {
                    mir::Value::Function(idx) => *idx as Reg,
                    _ => self.find(&fn_proto),
                };
                let dst = self.get_destination(dst, 1);
                Some(VmInstruction::MakeHeapClosure(
                    dst,
                    fn_idx,
                    size as TypeSize,
                ))
            }
            mir::Instruction::CloseHeapClosure(src) => {
                // Close upvalues of the heap-based closure
                // This is called when a closure escapes its defining scope
                let base = self.vregister.find_keep(&src).unwrap();
                Some(VmInstruction::CloseHeapClosure(base))
            }
            mir::Instruction::CloneHeap(src) => {
                // Increment reference count for call-by-value cloning
                let base = self.vregister.find_keep(&src).unwrap();
                Some(VmInstruction::CloneHeap(base))
            }
            mir::Instruction::CallIndirect(f, args, r_ty) => {
                let rsize = Self::word_size_for_type(r_ty);
                match f.as_ref() {
                    mir::Value::Register(_address) => {
                        let bytecodes_dst =
                            bytecodes_dst.unwrap_or_else(|| funcproto.bytecodes.as_mut());

                        let (fadd, argsize) = self.prepare_function(bytecodes_dst, &f, &args);
                        let s = self.find(&f);
                        let d = self.get_destination(dst.clone(), rsize);
                        bytecodes_dst.push(VmInstruction::CallIndirect(fadd, argsize as u8, rsize));
                        match rsize {
                            0 => None,
                            1 => Some(VmInstruction::Move(d, s)),
                            n => Some(VmInstruction::MoveRange(d, s, n)),
                        }
                    }
                    mir::Value::Function(_idx) => {
                        unreachable!();
                    }
                    mir::Value::ExtFunction(label, f_ty) => {
                        let (dst, argsize, nret) = self.prepare_extfun(
                            funcproto,
                            bytecodes_dst,
                            dst,
                            &args,
                            *label,
                            *f_ty,
                            r_ty,
                        );
                        Some(VmInstruction::CallExtFun(dst, argsize as u8, nret))
                    }
                    _ => unreachable!(),
                }
            }
            mir::Instruction::CloseUpValues(src, ty) => {
                // src might contain multiple upvalues (e.g. tuple)
                let flattened = ty.flatten();
                let base = self.vregister.find_keep(&src).unwrap();

                let mut offset = 0;
                let bytecodes_dst = bytecodes_dst.unwrap_or_else(|| funcproto.bytecodes.as_mut());
                for elem_t in flattened {
                    let tsize = Self::word_size_for_type(elem_t);
                    if elem_t.to_type().is_function() {
                        bytecodes_dst.push(VmInstruction::Close(base + offset))
                    }
                    offset += tsize;
                }
                None
            }
            mir::Instruction::GetUpValue(i, ty) => {
                let upval = &mirfunc.upindexes[i as usize];
                let v = self.find_upvalue(upval);
                let size: TypeSize = Self::word_size_for_type(ty);
                let ouv = mir::OpenUpValue {
                    pos: v as usize,
                    size,
                    is_closure: ty.to_type().is_function(),
                };
                if let Some(ui) = funcproto.upindexes.get_mut(i as usize) {
                    *ui = ouv;
                } else {
                    funcproto.upindexes.push(ouv);
                }
                let d = self.vregister.get_top().add_newvalue_range(&dst, size as _);
                Some(VmInstruction::GetUpValue(
                    d,
                    i as Reg,
                    Self::word_size_for_type(ty),
                ))
            }
            mir::Instruction::SetUpValue(dst, src, ty) => {
                let upval = &mirfunc.upindexes[dst as usize];
                let v = self.find_upvalue(upval);
                let size: TypeSize = Self::word_size_for_type(ty);
                let ouv = mir::OpenUpValue {
                    pos: v as usize,
                    size,
                    is_closure: ty.to_type().is_function(),
                };
                if let Some(ui) = funcproto.upindexes.get_mut(dst as usize) {
                    *ui = ouv;
                } else {
                    funcproto.upindexes.push(ouv);
                }
                let s = self.find(&src);
                Some(VmInstruction::SetUpValue(
                    dst as Reg,
                    s,
                    Self::word_size_for_type(ty),
                ))
            }
            mir::Instruction::PushStateOffset(v) => {
                let state_size = StateOffset::try_from(v).expect("too much large state offset.");
                Some(VmInstruction::PushStatePos(state_size))
            }
            mir::Instruction::PopStateOffset(v) => {
                let state_size = StateOffset::try_from(v).expect("too much large state offset.");
                Some(VmInstruction::PopStatePos(state_size))
            }
            mir::Instruction::GetState(ty) => {
                let size = Self::word_size_for_type(ty);
                let d = self.vregister.push_stack(&dst, size as _);
                Some(VmInstruction::GetState(d, size))
            }

            mir::Instruction::JmpIf(cond, tbb, ebb, pbb) => {
                let c = self.find(&cond);

                // TODO: to allow &mut match, but there should be nicer way...
                let mut bytecodes_dst = bytecodes_dst;

                let mut then_bytecodes: Vec<VmInstruction> = vec![];
                let mut else_bytecodes: Vec<VmInstruction> = vec![];
                mirfunc.body[tbb as usize]
                    .0
                    .iter()
                    .for_each(|(dst, t_inst)| {
                        let res = self.emit_instruction(
                            funcproto,
                            Some(&mut then_bytecodes),
                            mirfunc.clone(),
                            dst.clone(),
                            t_inst.clone(),
                            config,
                        );
                        if let Some(inst) = res {
                            then_bytecodes.push(inst);
                        }
                    });

                mirfunc.body[ebb as usize]
                    .0
                    .iter()
                    .for_each(|(dst, t_inst)| {
                        if let Some(inst) = self.emit_instruction(
                            funcproto,
                            Some(&mut else_bytecodes),
                            mirfunc.clone(),
                            dst.clone(),
                            t_inst.clone(),
                            config,
                        ) {
                            else_bytecodes.push(inst);
                        };
                    });
                let phiblock = &mirfunc.body[pbb as usize].0;
                let (phidst, pinst) = phiblock.first().unwrap();
                if let mir::Instruction::Phi(t, e) = pinst {
                    let t_size = self
                        .vregister
                        .get_top()
                        .0
                        .get(t)
                        .map(|MemoryRegion(_, size)| *size)
                        .unwrap_or(1);
                    let e_size = self
                        .vregister
                        .get_top()
                        .0
                        .get(e)
                        .map(|MemoryRegion(_, size)| *size)
                        .unwrap_or(1);
                    let phi_size = std::cmp::max(t_size, e_size);
                    let phi = self.get_destination(phidst.clone(), phi_size);

                    let t = self.find(t);
                    then_bytecodes.push(if phi_size == 1 {
                        VmInstruction::Move(phi, t)
                    } else {
                        VmInstruction::MoveRange(phi, t, phi_size)
                    });
                    let e = self.find(e);
                    else_bytecodes.push(if phi_size == 1 {
                        VmInstruction::Move(phi, e)
                    } else {
                        VmInstruction::MoveRange(phi, e, phi_size)
                    });
                } else {
                    unreachable!("Unexpected inst: {pinst:?}");
                }
                let else_offset = then_bytecodes.len() + 2; // +1 for Jmp, which will be added later
                let inst = VmInstruction::JmpIfNeg(c, else_offset as _);
                match &mut bytecodes_dst {
                    Some(dst) => dst.push(inst),
                    None => funcproto.bytecodes.push(inst),
                }

                // bytes between the bottom of then block and phi
                let ret_offset = else_bytecodes.len() + 1;

                then_bytecodes.push(VmInstruction::Jmp(ret_offset as i16));

                for mut b in [then_bytecodes, else_bytecodes] {
                    match &mut bytecodes_dst {
                        Some(dst) => dst.append(&mut b),
                        None => funcproto.bytecodes.append(&mut b),
                    }
                }

                phiblock
                    .iter()
                    .skip(1)
                    .fold(vec![], |mut bytes: Vec<VmInstruction>, (dst, p_inst)| {
                        if let Some(inst) = self.emit_instruction(
                            funcproto,
                            Some(&mut bytes),
                            mirfunc.clone(),
                            dst.clone(),
                            p_inst.clone(),
                            config,
                        ) {
                            bytes.push(inst);
                        }
                        bytes
                    })
                    .into_iter()
                    .for_each(|inst| match &mut bytecodes_dst {
                        Some(dst) => dst.push(inst),
                        None => funcproto.bytecodes.push(inst),
                    });
                None
            }
            mir::Instruction::Jmp(offset) => Some(VmInstruction::Jmp(offset)),
            mir::Instruction::Phi(_, _) => {
                unreachable!()
            }
            mir::Instruction::PhiSwitch(_) => {
                unreachable!("PhiSwitch should be handled within Switch processing")
            }
            // Tagged Union instructions (Phase 2)
            // Represents tagged unions as (tag, value) tuples using consecutive registers
            mir::Instruction::TaggedUnionWrap {
                tag,
                value,
                union_type,
            } => {
                // Get total word size for the tagged union (includes tag + max variant size)
                let total_size = Self::word_size_for_type(union_type);
                // Value size is total - 1 (for the tag)
                let value_size = total_size - 1;

                // Allocate consecutive registers for (tag, value)
                let dst_reg = self.vregister.push_stack(&dst, total_size as u64);

                // Store tag in first register
                let tag_pos = funcproto.add_new_constant(tag);
                bytecodes_dst
                    .unwrap_or_else(|| funcproto.bytecodes.as_mut())
                    .push(VmInstruction::MoveConst(dst_reg, tag_pos as ConstPos));

                // For no-payload constructors (Value::None), skip the value copy
                if matches!(value.as_ref(), mir::Value::None) || value_size == 0 {
                    None
                } else {
                    // Store value in subsequent register(s)
                    let val_reg = self.find(&value);
                    let val_dst = dst_reg + 1;

                    if value_size == 1 {
                        Some(VmInstruction::Move(val_dst, val_reg))
                    } else {
                        Some(VmInstruction::MoveRange(val_dst, val_reg, value_size))
                    }
                }
            }
            mir::Instruction::TaggedUnionGetTag(union_val) => {
                // Extract tag (first register of the tagged union tuple)
                let union_reg = self.find_keep(&union_val);
                let dst_reg = self.get_destination(dst, 1);

                // Tag is at union_reg + 0
                Some(VmInstruction::Move(dst_reg, union_reg))
            }
            mir::Instruction::TaggedUnionGetValue(union_val, value_ty) => {
                // Extract value (second register onwards of the tagged union tuple)
                let union_reg = self.find_keep(&union_val);
                let value_size = Self::word_size_for_type(value_ty);
                let dst_reg = self.get_destination(dst, value_size);

                // Value starts at union_reg + 1
                let value_reg = union_reg + 1;

                if value_size == 1 {
                    Some(VmInstruction::Move(dst_reg, value_reg))
                } else {
                    Some(VmInstruction::MoveRange(dst_reg, value_reg, value_size))
                }
            }
            mir::Instruction::BoxAlloc { value, inner_type } => {
                let inner_size = Self::word_size_for_type(inner_type);
                let src_reg = self.find(&value);
                let dst_reg = self.get_destination(dst, 1); // HeapIdx is 1 word
                Some(VmInstruction::BoxAlloc(dst_reg, src_reg, inner_size))
            }
            mir::Instruction::BoxLoad { ptr, inner_type } => {
                let inner_size = Self::word_size_for_type(inner_type);
                let src_reg = self.find(&ptr);
                let dst_reg = self.get_destination(dst, inner_size);
                Some(VmInstruction::BoxLoad(dst_reg, src_reg, inner_size))
            }
            mir::Instruction::BoxClone { ptr } => {
                let src_reg = self.vregister.find_keep(&ptr).unwrap();
                Some(VmInstruction::BoxClone(src_reg))
            }
            mir::Instruction::BoxRelease { ptr, .. } => {
                let src_reg = self.vregister.find_keep(&ptr).unwrap();
                Some(VmInstruction::BoxRelease(src_reg))
            }
            mir::Instruction::BoxStore {
                ptr,
                value,
                inner_type,
            } => {
                let inner_size = Self::word_size_for_type(inner_type);
                let ptr_reg = self.vregister.find_keep(&ptr).unwrap();
                let val_reg = self.find(&value);
                Some(VmInstruction::BoxStore(ptr_reg, val_reg, inner_size))
            }
            mir::Instruction::CloneUserSum { value, ty } => {
                // Clone all boxed references within a UserSum value
                let value_reg = self.vregister.find_keep(&value).unwrap();
                let size = Self::word_size_for_type(ty);
                // Register type in type table and get index
                let type_idx = self
                    .program
                    .add_type_to_table(ty)
                    .expect("Type table overflow - too many UserSum types");
                Some(VmInstruction::CloneUserSum(value_reg, size, type_idx))
            }
            mir::Instruction::ReleaseUserSum { value, ty } => {
                // Release all boxed references within a UserSum value
                let value_reg = self.vregister.find_keep(&value).unwrap();
                let size = Self::word_size_for_type(ty);
                // Register type in type table and get index
                let type_idx = self
                    .program
                    .add_type_to_table(ty)
                    .expect("Type table overflow - too many UserSum types");
                Some(VmInstruction::ReleaseUserSum(value_reg, size, type_idx))
            }
            mir::Instruction::Switch {
                scrutinee,
                cases,
                default_block,
                merge_block,
            } => {
                // Switch is compiled using JmpTable instruction
                // For exhaustive matches (default_block = None):
                //   Structure: [JmpTable] [case0_body, Move, Jmp] ... [caseN_body, Move] [merge_block]
                // For non-exhaustive matches (default_block = Some):
                //   Structure: [JmpTable] [case0_body, Move, Jmp] ... [default_body, Move] [merge_block]
                let scrut_reg = self.find(&scrutinee);

                // Reserve jump table index BEFORE processing child blocks
                // This ensures the outer switch gets the correct index even if nested switches
                // add their tables first during child block processing
                let table_idx = funcproto.jump_tables.len() as u8;
                // Add placeholder - will be filled in later
                funcproto.jump_tables.push(JumpTable {
                    min: 0,
                    offsets: vec![],
                });

                // Get PhiSwitch destination register
                let merge_block_mir = &mirfunc.body[merge_block as usize];
                let (phi_dst, phi_inst) = merge_block_mir.0.first().unwrap();
                let phi_inputs = if let mir::Instruction::PhiSwitch(results) = phi_inst {
                    results.clone()
                } else {
                    panic!("Expected PhiSwitch in merge block");
                };

                // Helper closure to emit instructions for a basic block
                let mut emit_block = |this: &mut Self, block: &mir::Block| -> Vec<VmInstruction> {
                    block.0.iter().fold(vec![], |mut bytes, (bdst, binst)| {
                        if let Some(vm_inst) = this.emit_instruction(
                            funcproto,
                            Some(&mut bytes),
                            mirfunc.clone(),
                            bdst.clone(),
                            binst.clone(),
                            config,
                        ) {
                            bytes.push(vm_inst);
                        }
                        bytes
                    })
                };

                // Collect all bytecodes for each case block
                let all_block_bytes: Vec<Vec<VmInstruction>> = cases
                    .iter()
                    .map(|(_, block_idx)| {
                        let block = &mirfunc.body[*block_idx as usize];
                        emit_block(self, block)
                    })
                    .collect();

                // Default block - only emit if explicitly present
                let default_bytes = if let Some(default_idx) = default_block {
                    let default_block_mir = &mirfunc.body[default_idx as usize];
                    emit_block(self, default_block_mir)
                } else {
                    vec![]
                };
                let has_default = default_block.is_some();

                // Resolve PhiSwitch source registers after case/default blocks are emitted.
                let result_regs: Vec<(Reg, TypeSize)> = phi_inputs
                    .iter()
                    .map(|r| {
                        let reg = self.find_keep(r);
                        let size = self
                            .vregister
                            .get_top()
                            .0
                            .get(r)
                            .map(|MemoryRegion(_, size)| *size)
                            .unwrap_or(1);
                        (reg, size)
                    })
                    .collect();
                let phi_size = result_regs.iter().map(|(_, size)| *size).max().unwrap_or(1);
                let phi_reg = self.get_destination(phi_dst.clone(), phi_size);

                // Merge block remaining instructions
                let merge_bytes: Vec<VmInstruction> =
                    merge_block_mir
                        .0
                        .iter()
                        .skip(1)
                        .fold(vec![], |mut bytes, (bdst, binst)| {
                            if let Some(vm_inst) = self.emit_instruction(
                                funcproto,
                                Some(&mut bytes),
                                mirfunc.clone(),
                                bdst.clone(),
                                binst.clone(),
                                config,
                            ) {
                                bytes.push(vm_inst);
                            }
                            bytes
                        });

                // Calculate sizes:
                // - Most case segments: [body..., Move, Jmp]
                // - Last segment (exhaustive case or default): [body..., Move] (no Jmp needed)
                let num_cases = all_block_bytes.len();
                let case_segment_sizes: Vec<usize> = all_block_bytes
                    .iter()
                    .enumerate()
                    .map(|(i, b)| {
                        if !has_default && i == num_cases - 1 {
                            // Last case in exhaustive match - no Jmp needed
                            b.len() + 1 // body + Move
                        } else {
                            b.len() + 2 // body + Move + Jmp
                        }
                    })
                    .collect();

                // Default segment size (only if has_default)
                let default_segment_size = if has_default {
                    default_bytes.len() + 1 // body + Move (no Jmp needed)
                } else {
                    0
                };

                // Build dense jump table for O(1) lookup
                // Calculate min/max values from cases
                let min_val = cases.iter().map(|(v, _)| *v).min().unwrap_or(0);
                let max_val = cases.iter().map(|(v, _)| *v).max().unwrap_or(0);

                // Calculate offsets for each case using scan (cumulative sum)
                let case_offsets: Vec<(i64, i16)> = cases
                    .iter()
                    .zip(case_segment_sizes.iter())
                    .scan(1i16, |offset, ((lit_val, _), seg_size)| {
                        let current = *offset;
                        *offset += *seg_size as i16;
                        Some((*lit_val, current))
                    })
                    .collect();

                // Default offset: for exhaustive matches, use last case; otherwise, after all cases
                let default_offset = if has_default {
                    // Separate default block - offset is after all case blocks
                    case_offsets
                        .last()
                        .map(|(_, off)| {
                            *off + case_segment_sizes.last().copied().unwrap_or(0) as i16
                        })
                        .unwrap_or(1)
                } else {
                    // Exhaustive match - use last case as default
                    case_offsets.last().map(|(_, off)| *off).unwrap_or(1)
                };

                // Build dense array: fill with default, then set specific case offsets
                // Add one extra slot at the end for the default offset (used for out-of-range values)
                let table_size = (max_val - min_val + 1) as usize + 1; // +1 for default slot
                let offsets = case_offsets.iter().fold(
                    vec![default_offset; table_size],
                    |mut offsets, (lit_val, offset)| {
                        offsets[(lit_val - min_val) as usize] = *offset;
                        offsets
                    },
                );

                // Update the placeholder jump table that was reserved earlier
                funcproto.jump_tables[table_idx as usize] = JumpTable {
                    min: min_val,
                    offsets,
                };

                // Build the bytecode sequence
                let switch_bytes: Vec<VmInstruction> =
                    if has_default {
                        // Non-exhaustive: has a separate default block
                        std::iter::once(VmInstruction::JmpTable(scrut_reg, table_idx))
                            .chain(all_block_bytes.iter().enumerate().flat_map(
                                |(i, block_bytes)| {
                                    let remaining_size: usize =
                                        case_segment_sizes[i + 1..].iter().sum::<usize>()
                                            + default_segment_size
                                            + 1;
                                    block_bytes
                                        .iter()
                                        .cloned()
                                        .chain(std::iter::once(if result_regs[i].1 == 1 {
                                            VmInstruction::Move(phi_reg, result_regs[i].0)
                                        } else {
                                            VmInstruction::MoveRange(
                                                phi_reg,
                                                result_regs[i].0,
                                                result_regs[i].1,
                                            )
                                        }))
                                        .chain(std::iter::once(VmInstruction::Jmp(
                                            remaining_size as i16,
                                        )))
                                },
                            ))
                            .chain(default_bytes.iter().cloned())
                            .chain(std::iter::once(if result_regs.last().unwrap().1 == 1 {
                                VmInstruction::Move(phi_reg, result_regs.last().unwrap().0)
                            } else {
                                VmInstruction::MoveRange(
                                    phi_reg,
                                    result_regs.last().unwrap().0,
                                    result_regs.last().unwrap().1,
                                )
                            }))
                            .chain(merge_bytes.iter().cloned())
                            .collect()
                    } else {
                        // Exhaustive: last case falls through to merge
                        std::iter::once(VmInstruction::JmpTable(scrut_reg, table_idx))
                            .chain(all_block_bytes.iter().enumerate().flat_map(
                                |(i, block_bytes)| {
                                    let is_last = i == num_cases - 1;
                                    let remaining_size: usize =
                                        case_segment_sizes[i + 1..].iter().sum::<usize>() + 1;
                                    block_bytes
                                        .iter()
                                        .cloned()
                                        .chain(std::iter::once(if result_regs[i].1 == 1 {
                                            VmInstruction::Move(phi_reg, result_regs[i].0)
                                        } else {
                                            VmInstruction::MoveRange(
                                                phi_reg,
                                                result_regs[i].0,
                                                result_regs[i].1,
                                            )
                                        }))
                                        .chain(if is_last {
                                            // Last case - no Jmp, falls through to merge
                                            None
                                        } else {
                                            Some(VmInstruction::Jmp(remaining_size as i16))
                                        })
                                },
                            ))
                            .chain(merge_bytes.iter().cloned())
                            .collect()
                    };

                // Output everything to the destination
                let bytecodes_dst = bytecodes_dst.unwrap_or_else(|| funcproto.bytecodes.as_mut());
                bytecodes_dst.extend(switch_bytes);

                None
            }
            mir::Instruction::Return(v, rty) => {
                let nret = Self::word_size_for_type(rty);
                let inst = match v.as_ref() {
                    mir::Value::None => VmInstruction::Return0,
                    _ => VmInstruction::Return(self.find(&v), nret),
                };
                Some(inst)
            }
            mir::Instruction::ReturnFeed(new, rty) => {
                let size = Self::word_size_for_type(rty);
                let bytecodes_dst = bytecodes_dst.unwrap_or_else(|| funcproto.bytecodes.as_mut());
                match config.self_eval_mode {
                    SelfEvalMode::SimpleState => {
                        let new = self.find(&new);
                        bytecodes_dst.push(VmInstruction::SetState(new, size));
                        Some(VmInstruction::Return(new, size))
                    }
                    SelfEvalMode::ZeroAtInit => {
                        //for returning always 0 at t=0
                        let old = self.vregister.add_newvalue(&dst);
                        bytecodes_dst.push(VmInstruction::GetState(old, size));
                        let new = self.find(&new);
                        bytecodes_dst.push(VmInstruction::SetState(new, size));
                        Some(VmInstruction::Return(old, size))
                    }
                }
            }
            mir::Instruction::Delay(max, src, time) => {
                let s = self.find(&src);
                let t = self.find(&time);

                let dst = self.vregister.add_newvalue(&dst);
                funcproto.delay_sizes.push(max);
                Some(VmInstruction::Delay(dst, s, t))
            }
            mir::Instruction::Mem(src) => {
                let s = self.find(&src);
                let dst = self.vregister.add_newvalue(&dst);
                Some(VmInstruction::Mem(dst, s))
            }
            mir::Instruction::NegF(v1) => self.emit_binop1(VmInstruction::NegF, dst, v1),
            mir::Instruction::AddF(v1, v2) => self.emit_binop2(VmInstruction::AddF, dst, v1, v2),
            mir::Instruction::SubF(v1, v2) => self.emit_binop2(VmInstruction::SubF, dst, v1, v2),
            mir::Instruction::MulF(v1, v2) => self.emit_binop2(VmInstruction::MulF, dst, v1, v2),
            mir::Instruction::DivF(v1, v2) => self.emit_binop2(VmInstruction::DivF, dst, v1, v2),
            mir::Instruction::ModF(v1, v2) => self.emit_binop2(VmInstruction::ModF, dst, v1, v2),
            mir::Instruction::PowF(v1, v2) => self.emit_binop2(VmInstruction::PowF, dst, v1, v2),
            mir::Instruction::LogF(v1) => self.emit_binop1(VmInstruction::LogF, dst, v1),

            mir::Instruction::SinF(v1) => self.emit_binop1(VmInstruction::SinF, dst, v1),
            mir::Instruction::CosF(v1) => self.emit_binop1(VmInstruction::CosF, dst, v1),
            mir::Instruction::AbsF(v1) => self.emit_binop1(VmInstruction::AbsF, dst, v1),
            mir::Instruction::SqrtF(v1) => self.emit_binop1(VmInstruction::SqrtF, dst, v1),
            mir::Instruction::CastFtoI(v1) => self.emit_binop1(VmInstruction::CastFtoI, dst, v1),
            mir::Instruction::CastItoF(v1) => self.emit_binop1(VmInstruction::CastItoF, dst, v1),
            mir::Instruction::AddI(v1, v2) => self.emit_binop2(VmInstruction::AddI, dst, v1, v2),
            mir::Instruction::SubI(v1, v2) => self.emit_binop2(VmInstruction::SubI, dst, v1, v2),
            mir::Instruction::MulI(v1, v2) => self.emit_binop2(VmInstruction::MulI, dst, v1, v2),
            mir::Instruction::DivI(v1, v2) => self.emit_binop2(VmInstruction::DivI, dst, v1, v2),
            mir::Instruction::ModI(v1, v2) => self.emit_binop2(VmInstruction::ModI, dst, v1, v2),
            mir::Instruction::Gt(v1, v2) => self.emit_binop2(VmInstruction::Gt, dst, v1, v2),
            mir::Instruction::Ge(v1, v2) => self.emit_binop2(VmInstruction::Ge, dst, v1, v2),
            mir::Instruction::Lt(v1, v2) => self.emit_binop2(VmInstruction::Lt, dst, v1, v2),
            mir::Instruction::Le(v1, v2) => self.emit_binop2(VmInstruction::Le, dst, v1, v2),
            mir::Instruction::Eq(v1, v2) => self.emit_binop2(VmInstruction::Eq, dst, v1, v2),
            mir::Instruction::Ne(v1, v2) => self.emit_binop2(VmInstruction::Ne, dst, v1, v2),
            mir::Instruction::And(v1, v2) => self.emit_binop2(VmInstruction::And, dst, v1, v2),
            mir::Instruction::Or(v1, v2) => self.emit_binop2(VmInstruction::Or, dst, v1, v2),

            mir::Instruction::Array(values, ty) => {
                let elem_ty_size = Self::word_size_for_type(ty);
                let size = values.len();
                // Move each value into the array
                let bytecodes_dst = bytecodes_dst.unwrap_or_else(|| funcproto.bytecodes.as_mut());
                let dst_reg = self.get_destination(dst.clone(), 1 as _); //address for array is always 1 word;
                bytecodes_dst.push(VmInstruction::AllocArray(dst_reg, size as _, elem_ty_size));
                for (i, val) in values.iter().enumerate() {
                    let tmp_idx_ref = Arc::new(mir::Value::None);
                    let idx = self.vregister.add_newvalue(&tmp_idx_ref);
                    bytecodes_dst.push(VmInstruction::MoveImmF(
                        idx,
                        HFloat::try_from(i as f64).unwrap(),
                    ));
                    let idx = self.find(&tmp_idx_ref);
                    let src = self.find(val);
                    bytecodes_dst.push(VmInstruction::SetArrayElem(dst_reg, idx, src));
                }
                self.varray.push(dst);
                None // Instructions already added to bytecodes_dst
            }

            mir::Instruction::GetArrayElem(array, index, elem_ty) => {
                let array_reg = self.find(&array);
                let index_reg = self.find(&index);
                let dst_reg = self.get_destination(dst, Self::word_size_for_type(elem_ty));
                let bytecodes_dst = bytecodes_dst.unwrap_or_else(|| funcproto.bytecodes.as_mut());
                bytecodes_dst.push(VmInstruction::GetArrayElem(dst_reg, array_reg, index_reg));
                None
            }

            mir::Instruction::SetArrayElem(_array, _index, _value, _elem_ty) => {
                todo!("SetArrayElem is not used in the current implementation");
            }

            instr => {
                unimplemented!("Instruction not implemented: {:?}", instr)
            }
        }
    }
    fn generate_funcproto(
        &mut self,
        mirfunc: &mir::Function,
        fidx: usize,
        config: Config,
    ) -> (String, vm::FuncProto) {
        log::trace!("generating function {}", mirfunc.label.0);
        let mut func = vm::FuncProto {
            nparam: mirfunc.args.len(),
            nret: Self::word_size_for_type(
                *mirfunc
                    .return_type
                    .get()
                    .expect("return type not inferred correctly"),
            ) as _,
            state_skeleton: mirfunc.state_skeleton.clone(), // Transfer state skeleton from MIR
            ..Default::default()
        };
        self.vregister.0.push(VRegister::default());
        for (i, a) in mirfunc.args.iter().enumerate() {
            let size = Self::word_size_for_type(a.1);
            self.vregister
                .push_stack(&Arc::new(mir::Value::Argument(i)), size as _);
        }

        // succeeding block will be compiled recursively
        let block = &mirfunc.body[0];
        block.0.iter().for_each(|(dst, inst)| {
            let newinst = self.emit_instruction(
                &mut func,
                None,
                mirfunc.clone(),
                dst.clone(),
                inst.clone(),
                config,
            );
            if let Some(i) = newinst {
                func.bytecodes.push(i);
            }
        });

        (mirfunc.label.to_string(), func)
    }
    pub fn generate(&mut self, mir: Mir, config: Config) -> vm::Program {
        self.program.global_fn_table = mir
            .functions
            .iter()
            .enumerate()
            .map(|(i, func)| {
                self.fnmap.insert(func.label, i);
                self.generate_funcproto(func, i, config)
            })
            .collect();
        self.program.file_path = mir.file_path.clone();
        self.program.iochannels = mir.get_dsp_iochannels();
        log::debug!("iochannels: {:?}", self.program.iochannels);
        self.program.dsp_index = self.program.get_fun_index("dsp");
        self.program.clone()
    }
}
fn remove_redundunt_mov(program: vm::Program) -> vm::Program {
    let mut res = program.clone();
    for (_, f) in res.global_fn_table.iter_mut() {
        let mut remove_idx = std::collections::HashSet::<usize>::new();
        let mut reduce_idx = std::collections::HashMap::<usize, VmInstruction>::new();

        let mut removeconst_idx = std::collections::HashMap::<usize, VmInstruction>::new();

        for (i, pair) in f.bytecodes.windows(2).enumerate() {
            match *pair {
                [
                    VmInstruction::Move(dst, src),
                    VmInstruction::Move(dst2, src2),
                ] if dst == src2 && src == dst2 =>
                //case of swapping
                {
                    remove_idx.insert(i);
                    remove_idx.insert(i + 1);
                }
                [
                    VmInstruction::Move(dst, src),
                    VmInstruction::Move(dst2, src2),
                ] if dst == src2 => {
                    reduce_idx.insert(i, VmInstruction::Move(dst2, src));
                    remove_idx.insert(i + 1);
                }
                [
                    VmInstruction::MoveConst(dst, src),
                    VmInstruction::Move(dst2, src2),
                ] if dst == src2 => {
                    removeconst_idx.insert(i, VmInstruction::MoveConst(dst2, src));
                    remove_idx.insert(i + 1);
                }
                _ => {}
            }
        }
        let mut res_bytecodes = vec![];
        for (i, inst) in f.bytecodes.iter().enumerate() {
            if remove_idx.contains(&i) {
                // log::trace!("removed redundunt mov")
            } else if let Some(inst) = removeconst_idx.get(&i) {
                res_bytecodes.push(*inst);
            } else if let Some(inst) = reduce_idx.get(&i) {
                res_bytecodes.push(*inst);
            } else {
                res_bytecodes.push(*inst);
            }
        }
        f.bytecodes = res_bytecodes;
    }
    res
}
fn optimize(program: vm::Program) -> vm::Program {
    // remove_redundunt_mov(program);
    program
}
pub fn gen_bytecode(mir: mir::Mir, config: Config) -> vm::Program {
    let mut generator = ByteCodeGenerator::default();
    let program = generator.generate(mir, config);
    optimize(program)
}

#[cfg(test)]
mod test {

    use crate::compiler::IoChannelInfo;
    use crate::interner::ToSymbol;
    #[test]
    fn build() {
        use super::*;
        use crate::numeric;
        use crate::types::PType;
        use crate::types::Type;
        extern crate colog;
        // colog::default_builder()
        //     .filter_level(log::LevelFilter::Trace)
        //     .init();
        // fn test(hoge){
        //   hoge+1
        //}
        let mut src = mir::Mir::default();
        let arg = mir::Argument("hoge".to_symbol(), numeric!());
        let argv = Arc::new(mir::Value::Argument(0));
        let mut func = mir::Function::new(
            0,
            "dsp".to_symbol(),
            std::slice::from_ref(&arg),
            vec![],
            None,
        );
        func.return_type.get_or_init(|| numeric!());
        let mut block = mir::Block::default();
        let resint = Arc::new(mir::Value::Register(1));
        block.0.push((resint.clone(), mir::Instruction::Integer(1)));
        let res = Arc::new(mir::Value::Register(2));
        block
            .0
            .push((res.clone(), mir::Instruction::AddF(argv, resint)));
        block.0.push((
            Arc::new(mir::Value::None),
            mir::Instruction::Return(res.clone(), numeric!()),
        ));
        func.body = vec![block];
        src.functions.push(func);
        let mut generator = ByteCodeGenerator::default();
        let config = Config::default();
        let res = generator.generate(src, config);

        let mut answer = vm::Program {
            iochannels: Some(IoChannelInfo {
                input: 1,
                output: 1,
            }),
            ..Default::default()
        };

        let mut main = vm::FuncProto::new(1, 1);
        main.constants.push(1);
        main.bytecodes = vec![
            VmInstruction::MoveConst(1, 0),
            VmInstruction::AddF(1, 0, 1),
            VmInstruction::Return(1, 1),
        ];
        answer.global_fn_table.push(("dsp".to_string(), main));
        answer.dsp_index = Some(0);
        assert_eq!(res, answer);
    }

    #[test]
    fn phi_multiword_emits_moverange_and_two_word_return() {
        use super::*;
        use crate::numeric;
        use crate::types::Type;

        let mut src = mir::Mir::default();
        let tuple_ty = Type::Tuple(vec![numeric!(), numeric!()]).into_id();

        let mut func = mir::Function::new(0, "dsp".to_symbol(), &[], vec![], None);
        func.return_type.get_or_init(|| tuple_ty);

        let cond = Arc::new(mir::Value::Register(0));
        let then_val = Arc::new(mir::Value::Register(1));
        let else_val = Arc::new(mir::Value::Register(2));
        let phi_val = Arc::new(mir::Value::Register(3));

        let mut entry = mir::Block::default();
        entry.0.push((cond.clone(), mir::Instruction::Float(1.0)));
        entry.0.push((
            Arc::new(mir::Value::None),
            mir::Instruction::JmpIf(cond.clone(), 1, 2, 3),
        ));

        let mut then_block = mir::Block::default();
        then_block
            .0
            .push((then_val.clone(), mir::Instruction::Alloc(tuple_ty)));

        let mut else_block = mir::Block::default();
        else_block
            .0
            .push((else_val.clone(), mir::Instruction::Alloc(tuple_ty)));

        let mut merge_block = mir::Block::default();
        merge_block.0.push((
            phi_val.clone(),
            mir::Instruction::Phi(then_val.clone(), else_val.clone()),
        ));
        merge_block.0.push((
            Arc::new(mir::Value::None),
            mir::Instruction::Return(phi_val.clone(), tuple_ty),
        ));

        func.body = vec![entry, then_block, else_block, merge_block];
        src.functions.push(func);

        let mut generator = ByteCodeGenerator::default();
        let program = generator.generate(src, Config::default());
        let proto = &program.global_fn_table[0].1;

        assert!(
            proto
                .bytecodes
                .iter()
                .any(|inst| matches!(inst, VmInstruction::MoveRange(_, _, 2))),
            "Expected MoveRange for two-word Phi value, got: {:?}",
            proto.bytecodes
        );
        assert!(
            proto
                .bytecodes
                .iter()
                .any(|inst| matches!(inst, VmInstruction::Return(_, 2))),
            "Expected two-word Return for tuple type, got: {:?}",
            proto.bytecodes
        );
    }
}