-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.rs
More file actions
93 lines (74 loc) · 1.3 KB
/
utils.rs
File metadata and controls
93 lines (74 loc) · 1.3 KB
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
use std::fs;
use crate::{instruction::RiscVInstruction, translate::translate_instrs};
/// Loop main() 10,000 times. Uses a3.
pub const RISCV_LOOP_START: &str = r#"
.text
.global _start
.global _main
.balign 4 # not sure if these are needed for RISC-V
_start:
# while i < 10,000
li a3, 10000
.loop:
addi a3, a3, -1
ble a3, x0, .end
# main()
jal ra, main
# while loop
j .loop
.end:
# exit(0)
li a7,93
ecall
.balign 4
_main:
main:
"#;
/// Loop main() 10,000 times. Uses x3
pub const ARM_LOOP_START: &str = r#"
.text
.global _start
.global _main
.balign 4
_start:
# i = 10,000
mov x3, #10000
# while i > 0
.loop:
sub x3, x3, 1
cmp x3, xzr
ble .end
# main()
bl main
b .loop
.end:
mov x8, #93
svc #0
.balign 4
_main:
main:
"#;
pub const ARM_START: &str = r#"
.text
.global _start
.global _main
.balign 4
_start:
bl main
mov x8, #93
svc #0
.balign 4
_main:
main:
"#;
pub fn translate_to_file(instrs: Vec<RiscVInstruction>, path: String) {
let arm_instrs = translate_instrs(instrs);
let mut contents = String::new();
for instr in arm_instrs {
let x: String = instr.into();
contents.push_str(&x);
contents.push_str("\n");
}
fs::write(&path, contents).expect("Unable to write file");
println!("Saved ARM assembly to {}", path);
}