Skip to content

Commit e67d69d

Browse files
atar13Samir-Rashid
authored andcommitted
organize add test
1 parent a2e6ee3 commit e67d69d

5 files changed

Lines changed: 130 additions & 5 deletions

File tree

src/utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ pub fn translate_to_file(instrs: Vec<RiscVInstruction>, path: String) {
2727
contents.push_str(&x);
2828
contents.push_str("\n");
2929
}
30-
fs::write(path, contents).expect("Unable to write file");
30+
fs::write(&path, contents).expect("Unable to write file");
31+
println!("Saved ARM assembly to {}", path);
3132
}
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
.text
33

44
.global _start
5-
.global _main
65

7-
.balign 4
86
_start:
97
bl main
108
mov x8, #93
119
svc #0
1210

13-
.balign 4
14-
_main:
1511
main:
1612

1713
sub sp, sp, 32

tests/add/test_add.rs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#[cfg(test)]
2+
mod tests {
3+
use binary_room::instruction::*;
4+
use binary_room::translate::*;
5+
use binary_room::utils;
6+
use binary_room::utils::translate_to_file;
7+
use binary_room::utils::START;
8+
9+
#[test]
10+
fn test_binary_translate() {
11+
let riscv_asm: Vec<RiscVInstruction> = vec![
12+
RiscVInstruction::Verbatim { text: START.to_string() },
13+
RiscVInstruction::Addi {
14+
dest: RiscVRegister::SP,
15+
src: RiscVRegister::SP,
16+
imm: -32,
17+
},
18+
RiscVInstruction::S {
19+
width: RiscVWidth::Double,
20+
src: RiscVRegister::RA,
21+
dest: RiscVVal::Offset {
22+
register: RiscVRegister::SP,
23+
offset: 24,
24+
},
25+
},
26+
RiscVInstruction::S {
27+
width: RiscVWidth::Double,
28+
src: RiscVRegister::S0FP,
29+
dest: RiscVVal::Offset {
30+
register: RiscVRegister::SP,
31+
offset: 16,
32+
},
33+
},
34+
RiscVInstruction::Addi {
35+
dest: RiscVRegister::S0FP,
36+
src: RiscVRegister::SP,
37+
imm: 32,
38+
},
39+
RiscVInstruction::Li {
40+
dest: RiscVRegister::A5,
41+
imm: 3,
42+
},
43+
RiscVInstruction::S {
44+
width: RiscVWidth::Word,
45+
src: RiscVRegister::A5,
46+
dest: RiscVVal::Offset {
47+
register: RiscVRegister::S0FP,
48+
offset: -20,
49+
},
50+
},
51+
RiscVInstruction::Li {
52+
dest: RiscVRegister::A5,
53+
imm: 4,
54+
},
55+
RiscVInstruction::S {
56+
width: RiscVWidth::Word,
57+
src: RiscVRegister::A5,
58+
dest: RiscVVal::Offset {
59+
register: RiscVRegister::S0FP,
60+
offset: -24,
61+
},
62+
},
63+
RiscVInstruction::L {
64+
width: RiscVWidth::Word,
65+
dest: RiscVRegister::A5,
66+
src: RiscVVal::Offset {
67+
register: RiscVRegister::S0FP,
68+
offset: -20,
69+
},
70+
},
71+
RiscVInstruction::Mv {
72+
dest: RiscVRegister::A4,
73+
src: RiscVRegister::A5,
74+
},
75+
RiscVInstruction::L {
76+
width: RiscVWidth::Word,
77+
dest: RiscVRegister::A5,
78+
src: RiscVVal::Offset {
79+
register: RiscVRegister::S0FP,
80+
offset: -24,
81+
},
82+
},
83+
RiscVInstruction::Add {
84+
width: RiscVWidth::Word,
85+
dest: RiscVRegister::A5,
86+
arg1: RiscVRegister::A4,
87+
arg2: RiscVRegister::A5,
88+
},
89+
RiscVInstruction::SextW {
90+
dest: RiscVRegister::A5,
91+
src: RiscVRegister::A5,
92+
},
93+
RiscVInstruction::Mv {
94+
dest: RiscVRegister::A0,
95+
src: RiscVRegister::A5,
96+
},
97+
RiscVInstruction::L {
98+
width: RiscVWidth::Double,
99+
dest: RiscVRegister::RA,
100+
src: RiscVVal::Offset {
101+
register: RiscVRegister::SP,
102+
offset: 24,
103+
},
104+
},
105+
RiscVInstruction::L {
106+
width: RiscVWidth::Double,
107+
dest: RiscVRegister::S0FP,
108+
src: RiscVVal::Offset {
109+
register: RiscVRegister::SP,
110+
offset: 16,
111+
},
112+
},
113+
RiscVInstruction::Addi {
114+
dest: RiscVRegister::SP,
115+
src: RiscVRegister::SP,
116+
imm: 32,
117+
},
118+
RiscVInstruction::Jr {
119+
target: RiscVRegister::RA,
120+
},
121+
];
122+
123+
translate_to_file(riscv_asm, "./tests/add/add.arm.s".to_string());
124+
}
125+
}

tests/tests.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
#[path = "add/test_add.rs"]
3+
mod add;

0 commit comments

Comments
 (0)