|
1 | 1 | #[cfg(test)] |
2 | 2 | mod tests { |
3 | | - use binary_room::translate::binary_translate; |
| 3 | + use binary_room::instruction::*; |
| 4 | + use binary_room::translate::*; |
4 | 5 |
|
5 | 6 | #[test] |
6 | 7 | fn test_binary_translate() { |
7 | | - let riscv_asm = " |
8 | | - addi sp,sp,-32 |
9 | | - sd ra,24(sp) |
10 | | - ld s0,16(sp) |
11 | | - addi s0,sp,32 |
12 | | - li a5,3 |
13 | | - sw a5,-20(s0) |
14 | | - li a5,4 |
15 | | - sw a5,-24(s0) |
16 | | - lw a5,-20(s0) |
17 | | - mv a4,a5 |
18 | | - lw a5,-24(s0) |
19 | | - addw a5,a4,a5 |
20 | | - sext.w a5,a5 |
21 | | - mv a0,a5 |
22 | | - ld ra,24(sp) |
23 | | - ld s0,16(sp) |
24 | | - addi sp,sp,32 |
25 | | - jr ra |
26 | | - "; |
27 | | - let translated_asm = binary_translate(riscv_asm); |
28 | | - let expected_output = " |
29 | | - Addi |
30 | | - Sd |
31 | | - Ld |
32 | | - Addi |
33 | | - Li |
34 | | - Sw |
35 | | - Li |
36 | | - Sw |
37 | | - Lw |
38 | | - Mv |
39 | | - Lw |
40 | | - Addw |
41 | | - SextW |
42 | | - Mv |
43 | | - Ld |
44 | | - Ld |
45 | | - Addi |
46 | | - Jr |
47 | | - "; |
48 | | - assert_eq!(translated_asm, expected_output); |
| 8 | + let riscv_asm: Vec<RiscVInstruction> = vec![ |
| 9 | + RiscVInstruction::Addi { |
| 10 | + dest: RiscVRegister::SP, |
| 11 | + src: RiscVRegister::SP, |
| 12 | + imm: -32, |
| 13 | + }, |
| 14 | + RiscVInstruction::S { |
| 15 | + width: RiscVWidth::Double, |
| 16 | + src: RiscVRegister::RA, |
| 17 | + dest: RiscVVal::Offset { |
| 18 | + register: RiscVRegister::SP, |
| 19 | + offset: 24, |
| 20 | + }, |
| 21 | + }, |
| 22 | + RiscVInstruction::S { |
| 23 | + width: RiscVWidth::Double, |
| 24 | + src: RiscVRegister::S0FP, |
| 25 | + dest: RiscVVal::Offset { |
| 26 | + register: RiscVRegister::SP, |
| 27 | + offset: 16, |
| 28 | + }, |
| 29 | + }, |
| 30 | + RiscVInstruction::Addi { |
| 31 | + dest: RiscVRegister::S0FP, |
| 32 | + src: RiscVRegister::SP, |
| 33 | + imm: 32, |
| 34 | + }, |
| 35 | + RiscVInstruction::Li { |
| 36 | + dest: RiscVRegister::A5, |
| 37 | + imm: 3, |
| 38 | + }, |
| 39 | + RiscVInstruction::S { |
| 40 | + width: RiscVWidth::Word, |
| 41 | + src: RiscVRegister::A5, |
| 42 | + dest: RiscVVal::Offset { |
| 43 | + register: RiscVRegister::S0FP, |
| 44 | + offset: -20, |
| 45 | + }, |
| 46 | + }, |
| 47 | + RiscVInstruction::Li { |
| 48 | + dest: RiscVRegister::A5, |
| 49 | + imm: 4, |
| 50 | + }, |
| 51 | + RiscVInstruction::S { |
| 52 | + width: RiscVWidth::Word, |
| 53 | + src: RiscVRegister::A5, |
| 54 | + dest: RiscVVal::Offset { |
| 55 | + register: RiscVRegister::S0FP, |
| 56 | + offset: -24, |
| 57 | + }, |
| 58 | + }, |
| 59 | + RiscVInstruction::L { |
| 60 | + width: RiscVWidth::Word, |
| 61 | + dest: RiscVRegister::A5, |
| 62 | + src: RiscVVal::Offset { |
| 63 | + register: RiscVRegister::S0FP, |
| 64 | + offset: -20, |
| 65 | + }, |
| 66 | + }, |
| 67 | + RiscVInstruction::Mv { |
| 68 | + dest: RiscVRegister::A4, |
| 69 | + src: RiscVRegister::A5, |
| 70 | + }, |
| 71 | + RiscVInstruction::L { |
| 72 | + width: RiscVWidth::Word, |
| 73 | + dest: RiscVRegister::A5, |
| 74 | + src: RiscVVal::Offset { |
| 75 | + register: RiscVRegister::S0FP, |
| 76 | + offset: -24, |
| 77 | + }, |
| 78 | + }, |
| 79 | + RiscVInstruction::Add { |
| 80 | + width: RiscVWidth::Word, |
| 81 | + dest: RiscVRegister::A5, |
| 82 | + arg1: RiscVRegister::A4, |
| 83 | + arg2: RiscVRegister::A5, |
| 84 | + }, |
| 85 | + RiscVInstruction::SextW { |
| 86 | + dest: RiscVRegister::A5, |
| 87 | + src: RiscVRegister::A5, |
| 88 | + }, |
| 89 | + RiscVInstruction::Mv { |
| 90 | + dest: RiscVRegister::A0, |
| 91 | + src: RiscVRegister::A5, |
| 92 | + }, |
| 93 | + RiscVInstruction::L { |
| 94 | + width: RiscVWidth::Double, |
| 95 | + dest: RiscVRegister::RA, |
| 96 | + src: RiscVVal::Offset { |
| 97 | + register: RiscVRegister::SP, |
| 98 | + offset: 24, |
| 99 | + }, |
| 100 | + }, |
| 101 | + RiscVInstruction::L { |
| 102 | + width: RiscVWidth::Double, |
| 103 | + dest: RiscVRegister::S0FP, |
| 104 | + src: RiscVVal::Offset { |
| 105 | + register: RiscVRegister::SP, |
| 106 | + offset: 16, |
| 107 | + }, |
| 108 | + }, |
| 109 | + RiscVInstruction::Addi { |
| 110 | + dest: RiscVRegister::SP, |
| 111 | + src: RiscVRegister::SP, |
| 112 | + imm: 32, |
| 113 | + }, |
| 114 | + RiscVInstruction::Jr { |
| 115 | + target: RiscVRegister::RA, |
| 116 | + }, |
| 117 | + ]; |
| 118 | + |
| 119 | + println!("{:?}", translate_instrs(riscv_asm)) |
| 120 | + // assert_eq!(translated_asm, expected_output); |
49 | 121 | } |
50 | 122 | } |
0 commit comments