typescript 0.0.4

TypeScript compiler and runtime
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
//! JIT 优化器模块
//!
//! 提供指令级别的优化功能,包括常量折叠、死代码消除、公共子表达式消除等。

use std::collections::{HashMap, HashSet};

use crate::codegen::{BinaryOp, Instruction};

/// JIT 优化器
///
/// 负责优化 JIT 编译后的代码
#[derive(Debug)]
pub struct JITOptimizer {
    /// 优化级别
    optimization_level: OptimizationLevel,
}

/// 优化级别
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum OptimizationLevel {
    /// 无优化
    None,
    /// 基本优化
    Basic,
    /// 中级优化
    Medium,
    /// 高级优化
    High,
}

impl JITOptimizer {
    /// 创建一个新的 JIT 优化器
    pub fn new(level: OptimizationLevel) -> Self {
        Self { optimization_level: level }
    }

    /// 优化指令序列
    pub fn optimize_instructions(&self, instructions: &[Instruction]) -> Vec<Instruction> {
        let mut optimized = instructions.to_vec();

        match self.optimization_level {
            OptimizationLevel::None => optimized,
            OptimizationLevel::Basic => {
                self.apply_basic_optimizations(&mut optimized);
                optimized
            }
            OptimizationLevel::Medium => {
                self.apply_basic_optimizations(&mut optimized);
                self.apply_medium_optimizations(&mut optimized);
                optimized
            }
            OptimizationLevel::High => {
                self.apply_basic_optimizations(&mut optimized);
                self.apply_medium_optimizations(&mut optimized);
                self.apply_high_optimizations(&mut optimized);
                optimized
            }
        }
    }

    /// 应用基本优化
    fn apply_basic_optimizations(&self, instructions: &mut Vec<Instruction>) {
        self.constant_folding(instructions);
        self.dead_code_elimination(instructions);
    }

    /// 应用中级优化
    fn apply_medium_optimizations(&self, instructions: &mut Vec<Instruction>) {
        self.common_subexpression_elimination(instructions);
        self.loop_invariant_code_motion(instructions);
    }

    /// 应用高级优化
    fn apply_high_optimizations(&self, instructions: &mut Vec<Instruction>) {
        self.function_inlining(instructions);
        self.register_allocation(instructions);
    }

    /// 常量折叠
    fn constant_folding(&self, instructions: &mut Vec<Instruction>) {
        let mut i = 0;
        while i < instructions.len() {
            match &instructions[i] {
                Instruction::PushNumber(n1) => {
                    if i + 2 < instructions.len() {
                        if let Instruction::PushNumber(ref n2) = instructions[i + 1] {
                            match &instructions[i + 2] {
                                Instruction::BinaryOp(op) => {
                                    let result = match op {
                                        BinaryOp::Add => *n1 + *n2,
                                        BinaryOp::Sub => *n1 - *n2,
                                        BinaryOp::Mul => *n1 * *n2,
                                        BinaryOp::Div => *n1 / *n2,
                                        BinaryOp::Mod => *n1 % *n2,
                                        BinaryOp::Eq => {
                                            if *n1 == *n2 {
                                                1.0
                                            }
                                            else {
                                                0.0
                                            }
                                        }
                                        BinaryOp::Neq => {
                                            if *n1 != *n2 {
                                                1.0
                                            }
                                            else {
                                                0.0
                                            }
                                        }
                                        BinaryOp::Gt => {
                                            if *n1 > *n2 {
                                                1.0
                                            }
                                            else {
                                                0.0
                                            }
                                        }
                                        BinaryOp::Gte => {
                                            if *n1 >= *n2 {
                                                1.0
                                            }
                                            else {
                                                0.0
                                            }
                                        }
                                        BinaryOp::Lt => {
                                            if *n1 < *n2 {
                                                1.0
                                            }
                                            else {
                                                0.0
                                            }
                                        }
                                        BinaryOp::Lte => {
                                            if *n1 <= *n2 {
                                                1.0
                                            }
                                            else {
                                                0.0
                                            }
                                        }
                                        BinaryOp::BitAnd => ((*n1 as i64) & (*n2 as i64)) as f64,
                                        BinaryOp::BitOr => ((*n1 as i64) | (*n2 as i64)) as f64,
                                        BinaryOp::BitXor => ((*n1 as i64) ^ (*n2 as i64)) as f64,
                                        BinaryOp::Shl => ((*n1 as i64) << (*n2 as i32)) as f64,
                                        BinaryOp::Shr => ((*n1 as i64) >> (*n2 as i32)) as f64,
                                        BinaryOp::UShr => ((*n1 as u64) >> (*n2 as u32)) as f64,
                                        _ => continue,
                                    };
                                    instructions[i] = Instruction::PushNumber(result);
                                    instructions.remove(i + 1);
                                    instructions.remove(i + 1);
                                    continue;
                                }
                                _ => {}
                            }
                        }
                    }
                }
                Instruction::PushBoolean(b1) => {
                    if i + 2 < instructions.len() {
                        if let Instruction::PushBoolean(ref b2) = instructions[i + 1] {
                            match &instructions[i + 2] {
                                Instruction::BinaryOp(op) => {
                                    let result = match op {
                                        BinaryOp::And => *b1 && *b2,
                                        BinaryOp::Or => *b1 || *b2,
                                        BinaryOp::Eq => *b1 == *b2,
                                        BinaryOp::Neq => *b1 != *b2,
                                        _ => continue,
                                    };
                                    instructions[i] = Instruction::PushBoolean(result);
                                    instructions.remove(i + 1);
                                    instructions.remove(i + 1);
                                    continue;
                                }
                                _ => {}
                            }
                        }
                    }
                }
                Instruction::PushString(s1) => {
                    if i + 2 < instructions.len() {
                        if let Instruction::PushString(ref s2) = instructions[i + 1] {
                            match &instructions[i + 2] {
                                Instruction::BinaryOp(BinaryOp::Add) => {
                                    let result = format!("{}{}", s1, s2);
                                    instructions[i] = Instruction::PushString(result);
                                    instructions.remove(i + 1);
                                    instructions.remove(i + 1);
                                    continue;
                                }
                                _ => {}
                            }
                        }
                    }
                }
                Instruction::UnaryOp(op) => {
                    if i >= 1 {
                        match &instructions[i - 1] {
                            Instruction::PushBoolean(b) => {
                                let result = match op {
                                    crate::codegen::UnaryOp::Not => !*b,
                                    _ => continue,
                                };
                                instructions[i - 1] = Instruction::PushBoolean(result);
                                instructions.remove(i);
                                i = i.saturating_sub(1);
                                continue;
                            }
                            Instruction::PushNumber(n) => {
                                let result = match op {
                                    crate::codegen::UnaryOp::Neg => -*n,
                                    crate::codegen::UnaryOp::Pos => *n,
                                    crate::codegen::UnaryOp::BitNot => (!(*n as i64)) as f64,
                                    _ => continue,
                                };
                                instructions[i - 1] = Instruction::PushNumber(result);
                                instructions.remove(i);
                                i = i.saturating_sub(1);
                                continue;
                            }
                            _ => {}
                        }
                    }
                }
                _ => {}
            }
            i += 1;
        }
    }

    /// 死代码消除
    fn dead_code_elimination(&self, instructions: &mut Vec<Instruction>) {
        let mut i = 0;
        while i < instructions.len() {
            match &instructions[i] {
                Instruction::Jump(offset) => {
                    let jump_to = (i as i32 + 1 + *offset as i32) as usize;
                    if jump_to > i + 1 {
                        let start = i + 1;
                        let end = jump_to;
                        if start < end && end <= instructions.len() {
                            instructions.drain(start..end);
                            continue;
                        }
                    }
                }
                Instruction::JumpIfFalse(offset) => {
                    if i > 0 {
                        if let Instruction::PushBoolean(b) = instructions[i - 1] {
                            if b {
                                instructions.remove(i);
                                continue;
                            }
                            else {
                                let jump_to = (i as i32 + 1 + *offset as i32) as usize;
                                if jump_to > i + 1 {
                                    let start = i + 1;
                                    let end = jump_to;
                                    if start < end && end <= instructions.len() {
                                        instructions.drain(start..end);
                                    }
                                }
                                instructions.remove(i);
                                instructions.remove(i - 1);
                                i = i.saturating_sub(1);
                                continue;
                            }
                        }
                        else if let Instruction::PushNumber(n) = instructions[i - 1] {
                            let condition = n != 0.0;
                            if condition {
                                instructions.remove(i);
                                continue;
                            }
                            else {
                                let jump_to = (i as i32 + 1 + *offset as i32) as usize;
                                if jump_to > i + 1 {
                                    let start = i + 1;
                                    let end = jump_to;
                                    if start < end && end <= instructions.len() {
                                        instructions.drain(start..end);
                                    }
                                }
                                instructions.remove(i);
                                instructions.remove(i - 1);
                                i = i.saturating_sub(1);
                                continue;
                            }
                        }
                        else if let Instruction::PushString(s) = &instructions[i - 1] {
                            let condition = !s.is_empty();
                            if condition {
                                instructions.remove(i);
                                continue;
                            }
                            else {
                                let jump_to = (i as i32 + 1 + *offset as i32) as usize;
                                if jump_to > i + 1 {
                                    let start = i + 1;
                                    let end = jump_to;
                                    if start < end && end <= instructions.len() {
                                        instructions.drain(start..end);
                                    }
                                }
                                instructions.remove(i);
                                instructions.remove(i - 1);
                                i = i.saturating_sub(1);
                                continue;
                            }
                        }
                        else if let Instruction::PushUndefined = instructions[i - 1] {
                            let jump_to = (i as i32 + 1 + *offset as i32) as usize;
                            if jump_to > i + 1 {
                                let start = i + 1;
                                let end = jump_to;
                                if start < end && end <= instructions.len() {
                                    instructions.drain(start..end);
                                }
                            }
                            instructions.remove(i);
                            instructions.remove(i - 1);
                            i = i.saturating_sub(1);
                            continue;
                        }
                        else if let Instruction::PushNull = instructions[i - 1] {
                            let jump_to = (i as i32 + 1 + *offset as i32) as usize;
                            if jump_to > i + 1 {
                                let start = i + 1;
                                let end = jump_to;
                                if start < end && end <= instructions.len() {
                                    instructions.drain(start..end);
                                }
                            }
                            instructions.remove(i);
                            instructions.remove(i - 1);
                            i = i.saturating_sub(1);
                            continue;
                        }
                    }
                }
                Instruction::Pop => {
                    let mut pop_count = 1;
                    let mut j = i + 1;
                    while j < instructions.len() {
                        if let Instruction::Pop = instructions[j] {
                            pop_count += 1;
                            j += 1;
                        }
                        else {
                            break;
                        }
                    }
                    if pop_count > 1 {
                        instructions[i] = Instruction::Pop;
                        instructions.drain(i + 1..j);
                        continue;
                    }
                }
                Instruction::Return => {
                    if i + 1 < instructions.len() {
                        instructions.drain(i + 1..);
                        continue;
                    }
                }
                _ => {}
            }
            i += 1;
        }
    }

    /// 公共子表达式消除
    fn common_subexpression_elimination(&self, instructions: &mut Vec<Instruction>) {
        let mut expr_map: HashMap<String, usize> = HashMap::new();
        let mut i = 0;

        while i < instructions.len() {
            let instr = instructions[i].clone();
            match &instr {
                Instruction::BinaryOp(_op) => {
                    if i >= 2 {
                        let expr_key = format!("{:?}_{:?}_{:?}", instructions[i - 2], instructions[i - 1], instr);

                        if expr_map.contains_key(&expr_key) {
                            instructions[i - 2] = Instruction::Jump(2);
                            instructions[i - 1] = Instruction::Pop;
                            instructions[i] = Instruction::Dup;
                        }
                        else {
                            expr_map.insert(expr_key, i);
                        }
                    }
                }
                Instruction::UnaryOp(_op) => {
                    if i >= 1 {
                        let expr_key = format!("{:?}_{:?}", instructions[i - 1], instr);

                        if expr_map.contains_key(&expr_key) {
                            instructions[i - 1] = Instruction::Jump(1);
                            instructions[i] = Instruction::Dup;
                        }
                        else {
                            expr_map.insert(expr_key, i);
                        }
                    }
                }
                Instruction::GetProperty => {
                    if i >= 2 {
                        let expr_key = format!("{:?}_{:?}_{:?}", instructions[i - 2], instructions[i - 1], instr);

                        if expr_map.contains_key(&expr_key) {
                            instructions[i - 2] = Instruction::Jump(2);
                            instructions[i - 1] = Instruction::Pop;
                            instructions[i] = Instruction::Dup;
                        }
                        else {
                            expr_map.insert(expr_key, i);
                        }
                    }
                }
                Instruction::GetElement => {
                    if i >= 2 {
                        let expr_key = format!("{:?}_{:?}_{:?}", instructions[i - 2], instructions[i - 1], instr);

                        if expr_map.contains_key(&expr_key) {
                            instructions[i - 2] = Instruction::Jump(2);
                            instructions[i - 1] = Instruction::Pop;
                            instructions[i] = Instruction::Dup;
                        }
                        else {
                            expr_map.insert(expr_key, i);
                        }
                    }
                }
                Instruction::Call(arg_count) => {
                    if i >= *arg_count as usize + 1 {
                        let mut expr_key = format!("Call_{}", arg_count);
                        for j in 0..*arg_count as usize + 1 {
                            expr_key.push_str(&format!("_{:?}", instructions[i - j]));
                        }

                        if expr_map.contains_key(&expr_key) {
                            let start_pos = i - *arg_count as usize;
                            let jump_offset = (*arg_count as usize + 1).try_into().unwrap();
                            instructions[start_pos] = Instruction::Jump(jump_offset);
                            for j in 1..*arg_count as usize + 1 {
                                instructions[start_pos + j] = Instruction::Pop;
                            }
                            instructions[i] = Instruction::Dup;
                        }
                        else {
                            expr_map.insert(expr_key, i);
                        }
                    }
                }
                _ => {}
            }
            i += 1;
        }
    }

    /// 循环不变代码外提
    fn loop_invariant_code_motion(&self, instructions: &mut Vec<Instruction>) {
        let mut i = 0;
        while i < instructions.len() {
            match &instructions[i] {
                Instruction::Jump(offset) => {
                    let jump_to = (i as i32 + 1 + *offset as i32) as usize;
                    if jump_to < i {
                        let loop_start = jump_to;
                        let loop_end = i;

                        let mut invariant_instructions = Vec::new();
                        let mut j = loop_start;

                        while j < loop_end {
                            if self.is_invariant_instruction(instructions, j, loop_start, loop_end) {
                                invariant_instructions.push((j, instructions[j].clone()));
                            }
                            j += 1;
                        }

                        if !invariant_instructions.is_empty() {
                            let mut to_move = Vec::new();
                            let mut remove_indices = Vec::new();

                            let mut processed = HashSet::new();
                            for (idx, instr) in invariant_instructions {
                                if !processed.contains(&idx) {
                                    to_move.push(instr);
                                    remove_indices.push(idx);
                                    processed.insert(idx);
                                }
                            }

                            let to_move_len = to_move.len();
                            let remove_indices_len = remove_indices.len();

                            remove_indices.sort_by(|a, b| b.cmp(a));
                            for idx in &remove_indices {
                                instructions.remove(*idx);
                            }

                            instructions.splice(loop_start..loop_start, to_move);

                            i += to_move_len - remove_indices_len;
                        }
                    }
                }
                _ => {}
            }
            i += 1;
        }
    }

    /// 检查指令是否是循环不变的
    fn is_invariant_instruction(
        &self,
        instructions: &[Instruction],
        instr_pos: usize,
        loop_start: usize,
        loop_end: usize,
    ) -> bool {
        match &instructions[instr_pos] {
            Instruction::LoadVariable(_) => true,
            Instruction::StoreVariable(_) => false,
            Instruction::LoadLocal(idx) => !self.is_local_modified_in_loop(instructions, *idx, loop_start, loop_end),
            Instruction::StoreLocal(_) => false,

            Instruction::PushUndefined
            | Instruction::PushNull
            | Instruction::PushBoolean(_)
            | Instruction::PushNumber(_)
            | Instruction::PushString(_) => true,

            Instruction::BinaryOp(_) | Instruction::UnaryOp(_) => {
                self.are_operands_invariant(instructions, instr_pos, loop_start, loop_end)
            }

            _ => false,
        }
    }

    /// 检查局部变量是否在循环内被修改
    fn is_local_modified_in_loop(
        &self,
        instructions: &[Instruction],
        local_idx: usize,
        loop_start: usize,
        loop_end: usize,
    ) -> bool {
        for i in loop_start..loop_end {
            if let Instruction::StoreLocal(idx) = instructions[i] {
                if idx == local_idx {
                    return true;
                }
            }
        }
        false
    }

    /// 检查操作数是否是不变的
    fn are_operands_invariant(
        &self,
        instructions: &[Instruction],
        instr_pos: usize,
        loop_start: usize,
        loop_end: usize,
    ) -> bool {
        match &instructions[instr_pos] {
            Instruction::BinaryOp(_) => {
                if instr_pos >= 2 {
                    self.is_invariant_instruction(instructions, instr_pos - 1, loop_start, loop_end)
                        && self.is_invariant_instruction(instructions, instr_pos - 2, loop_start, loop_end)
                }
                else {
                    false
                }
            }
            Instruction::UnaryOp(_) => {
                if instr_pos >= 1 {
                    self.is_invariant_instruction(instructions, instr_pos - 1, loop_start, loop_end)
                }
                else {
                    false
                }
            }
            _ => false,
        }
    }

    /// 函数内联
    fn function_inlining(&self, instructions: &mut Vec<Instruction>) {
        let mut i = 0;
        while i < instructions.len() {
            match &instructions[i] {
                Instruction::Call(arg_count) => {
                    if self.should_inline_function(instructions, i, *arg_count) {
                        self.inline_function(instructions, i, *arg_count);
                        // 内联后重新从当前位置开始检查,因为指令长度发生了变化
                        i = 0;
                        continue;
                    }
                }
                _ => {}
            }
            i += 1;
        }

        self.optimize_tail_recursion(instructions);
    }

    /// 检查是否应该内联函数
    fn should_inline_function(&self, instructions: &[Instruction], call_pos: usize, arg_count: u32) -> bool {
        if arg_count > 8 {
            return false;
        }

        if let Some((function_body, param_count)) = self.find_function_definition(instructions, call_pos) {
            // 根据优化级别调整内联阈值
            let max_body_length = match self.optimization_level {
                OptimizationLevel::High => 200,
                OptimizationLevel::Medium => 150,
                OptimizationLevel::Basic => 100,
                OptimizationLevel::None => 0,
            };

            if function_body.len() < max_body_length && param_count == arg_count {
                return true;
            }
        }

        false
    }

    /// 查找函数定义
    fn find_function_definition(&self, instructions: &[Instruction], call_pos: usize) -> Option<(Vec<Instruction>, u32)> {
        for i in (0..call_pos).rev() {
            match &instructions[i] {
                Instruction::SetFunctionBody(body) => {
                    for j in (0..i).rev() {
                        if let Instruction::CreateFunction(_, param_count) = instructions[j] {
                            return Some((body.clone(), param_count));
                        }
                    }
                }
                _ => {}
            }
        }
        None
    }

    /// 内联函数
    fn inline_function(&self, instructions: &mut Vec<Instruction>, call_pos: usize, arg_count: u32) {
        if let Some((function_body, param_count)) = self.find_function_definition(instructions, call_pos) {
            if param_count == arg_count {
                let mut inline_instructions = vec![];

                // 保存参数到局部变量
                for i in 0..arg_count as usize {
                    inline_instructions.push(Instruction::StoreLocal(i));
                }

                // 复制函数体
                let mut body = function_body.clone();

                // 处理返回值
                self.process_return_values(&mut body);

                inline_instructions.extend(body);

                // 替换调用指令
                instructions.splice(call_pos - arg_count as usize..=call_pos, inline_instructions);
            }
        }
    }

    /// 处理内联函数中的返回值
    fn process_return_values(&self, instructions: &mut Vec<Instruction>) {
        let mut i = 0;
        while i < instructions.len() {
            if let Instruction::Return = instructions[i] {
                // 对于函数内联,我们不需要返回指令,只需要保留栈顶的值
                instructions.remove(i);
                continue;
            }
            i += 1;
        }
    }

    /// 优化尾递归
    fn optimize_tail_recursion(&self, instructions: &mut Vec<Instruction>) {
        let mut i = 0;
        while i < instructions.len() {
            if let Instruction::Call(arg_count) = instructions[i] {
                if i + 1 < instructions.len() && matches!(instructions[i + 1], Instruction::Return) {
                    self.replace_tail_recursion_with_loop(instructions, i, arg_count);
                }
            }
            i += 1;
        }
    }

    /// 将尾递归替换为循环
    fn replace_tail_recursion_with_loop(&self, instructions: &mut Vec<Instruction>, call_pos: usize, _arg_count: u32) {
        let mut loop_instructions = vec![];

        loop_instructions.push(Instruction::Jump(0));

        instructions.splice(call_pos..call_pos + 2, loop_instructions);
    }

    /// 寄存器分配
    fn register_allocation(&self, instructions: &mut Vec<Instruction>) {
        let mut registers: HashMap<String, usize> = HashMap::new();
        let mut register_count = 0;
        let max_registers = 8;

        let mut live_variables = self.analyze_liveness(instructions);

        self.reorder_instructions(instructions);
        self.optimize_stack_operations(instructions);

        let mut i = 0;
        while i < instructions.len() {
            match &instructions[i] {
                Instruction::LoadVariable(name) => {
                    if !registers.contains_key(name) {
                        if register_count < max_registers {
                            registers.insert(name.to_string(), register_count);
                            register_count += 1;
                        }
                        else {
                            self.spill_register(&mut registers, &live_variables, i);
                            registers.insert(name.to_string(), register_count - 1);
                        }
                    }
                }
                Instruction::StoreVariable(name) => {
                    if !registers.contains_key(name) {
                        if register_count < max_registers {
                            registers.insert(name.to_string(), register_count);
                            register_count += 1;
                        }
                        else {
                            self.spill_register(&mut registers, &live_variables, i);
                            registers.insert(name.to_string(), register_count - 1);
                        }
                    }
                }
                Instruction::LoadLocal(idx) => {
                    let local_name = format!("local_{}", idx);
                    if !registers.contains_key(&local_name) {
                        if register_count < max_registers {
                            registers.insert(local_name, register_count);
                            register_count += 1;
                        }
                        else {
                            self.spill_register(&mut registers, &live_variables, i);
                            registers.insert(local_name, register_count - 1);
                        }
                    }
                }
                Instruction::StoreLocal(idx) => {
                    let local_name = format!("local_{}", idx);
                    if !registers.contains_key(&local_name) {
                        if register_count < max_registers {
                            registers.insert(local_name, register_count);
                            register_count += 1;
                        }
                        else {
                            self.spill_register(&mut registers, &live_variables, i);
                            registers.insert(local_name, register_count - 1);
                        }
                    }
                }
                _ => {}
            }
            i += 1;
        }
    }

    /// 变量活跃性分析
    fn analyze_liveness(&self, instructions: &[Instruction]) -> Vec<HashSet<String>> {
        let mut live_variables = vec![HashSet::new(); instructions.len() + 1];
        let mut changed = true;

        while changed {
            changed = false;

            for i in (0..instructions.len()).rev() {
                let mut current_live = live_variables[i + 1].clone();

                match &instructions[i] {
                    Instruction::LoadVariable(name) => {
                        current_live.insert(name.to_string());
                    }
                    Instruction::StoreVariable(name) => {
                        current_live.remove(name);
                        current_live.insert(name.to_string());
                    }
                    Instruction::LoadLocal(idx) => {
                        current_live.insert(format!("local_{}", idx));
                    }
                    Instruction::StoreLocal(idx) => {
                        current_live.remove(&format!("local_{}", idx));
                        current_live.insert(format!("local_{}", idx));
                    }
                    _ => {}
                }

                if current_live != live_variables[i] {
                    live_variables[i] = current_live;
                    changed = true;
                }
            }
        }

        live_variables
    }

    /// 寄存器溢出
    fn spill_register(&self, registers: &mut HashMap<String, usize>, live_variables: &[HashSet<String>], current_pos: usize) {
        let mut best_var = None;
        let mut min_liveness = usize::MAX;

        for (var, _reg) in registers.iter() {
            let mut liveness = 0;
            for i in current_pos..live_variables.len() {
                if live_variables[i].contains(var) {
                    liveness += 1;
                }
            }

            if liveness < min_liveness {
                min_liveness = liveness;
                best_var = Some(var.clone());
            }
        }

        if let Some(var) = best_var {
            registers.remove(&var);
        }
    }

    /// 指令重排序,提高指令级并行性
    fn reorder_instructions(&self, instructions: &mut Vec<Instruction>) {
        let mut reordered = vec![];
        let mut i = 0;

        while i < instructions.len() {
            let mut parallel_instructions = vec![];
            let mut j = i;

            while j < instructions.len() && parallel_instructions.len() < 2 {
                match &instructions[j] {
                    Instruction::PushNumber(_) | Instruction::PushBoolean(_) | Instruction::PushString(_) => {
                        parallel_instructions.push(instructions[j].clone());
                        j += 1;
                    }
                    _ => break,
                }
            }

            if !parallel_instructions.is_empty() {
                for instr in parallel_instructions {
                    reordered.push(instr);
                }
                i = j;
            }
            else {
                reordered.push(instructions[i].clone());
                i += 1;
            }
        }

        *instructions = reordered;
    }

    /// 优化栈操作
    fn optimize_stack_operations(&self, instructions: &mut Vec<Instruction>) {
        let mut i = 0;
        while i < instructions.len() {
            if let Instruction::Pop = instructions[i] {
                let mut pop_count = 1;
                let mut j = i + 1;
                while j < instructions.len() {
                    if let Instruction::Pop = instructions[j] {
                        pop_count += 1;
                        j += 1;
                    }
                    else {
                        break;
                    }
                }
                if pop_count > 1 {
                    instructions[i] = Instruction::Pop;
                    instructions.drain(i + 1..j);
                    continue;
                }
            }

            if let Instruction::Dup = instructions[i] {
                let mut dup_count = 1;
                let mut j = i + 1;
                while j < instructions.len() {
                    if let Instruction::Dup = instructions[j] {
                        dup_count += 1;
                        j += 1;
                    }
                    else {
                        break;
                    }
                }
                if dup_count > 2 {
                    instructions[i] = Instruction::Dup;
                    instructions.drain(i + 1..j);
                    continue;
                }
            }

            i += 1;
        }
    }
}