Skip to content

Commit b6e4099

Browse files
committed
basic control flow
1 parent 2c692d6 commit b6e4099

3 files changed

Lines changed: 66 additions & 4 deletions

File tree

src/instruction.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,13 @@ pub enum RiscVInstruction {
5656
arg1: RiscVRegister,
5757
arg2: RiscVRegister,
5858
},
59+
/// branch if less than or equal
60+
#[strum(serialize = "call")]
61+
Ble {
62+
arg1: RiscVRegister,
63+
arg2: RiscVRegister,
64+
target: RiscVVal
65+
},
5966
/// call label
6067
#[strum(serialize = "call")]
6168
Call {
@@ -113,6 +120,9 @@ pub enum RiscVInstruction {
113120
dest: RiscVRegister,
114121
src: RiscVRegister,
115122
},
123+
/// Jump label
124+
#[strum(serialize = "j")]
125+
J { target: RiscVVal },
116126
/// Jump Register
117127
/// Jump to address and place return address in rd.
118128
/// jal rd,offset
@@ -201,10 +211,13 @@ pub enum ArmInstruction {
201211
},
202212
/// B Branch R15 := address
203213
#[strum(serialize = "b")]
204-
B,
214+
B { target: ArmVal },
205215
/// BLR Xn
206216
#[strum(serialize = "blr")]
207217
Blr { target: ArmRegisterName },
218+
/// BLE label
219+
#[strum(serialize = "ble")]
220+
Ble { arg1: ArmRegister, arg2: ArmRegister, target: ArmVal },
208221
/// BL label
209222
#[strum(serialize = "bl")]
210223
Bl {target: ArmVal},
@@ -250,7 +263,11 @@ pub enum ArmInstruction {
250263

251264
impl Default for ArmInstruction {
252265
fn default() -> Self {
253-
ArmInstruction::B
266+
ArmInstruction::Mov {
267+
width: ArmWidth::Double,
268+
dest: ArmRegister { width: ArmWidth::Double, name: ArmRegisterName::X0 },
269+
src: ArmVal::Reg(ArmRegister { width: ArmWidth::Double, name: ArmRegisterName::X0 })
270+
}
254271
}
255272
}
256273

@@ -483,7 +500,12 @@ impl Into<String> for ArmInstruction {
483500
ArmInstruction::Adrp { dest, label } => {
484501
format!("adrp {}, {}", dest, label)
485502
}
486-
ArmInstruction::B => todo!(),
503+
ArmInstruction::B { target } => {
504+
format!("b {}", target)
505+
},
506+
ArmInstruction::Ble { arg1, arg2, target } => {
507+
format!("cmp {}, {}\n blt {}", arg1, arg2, target)
508+
},
487509
ArmInstruction::Blr { target } => {
488510
format!("blr {}", Into::<ArmRegister>::into(target))
489511
},

src/translate.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,18 @@ pub fn translate(riscv_instr: RiscVInstruction) -> ArmInstruction {
3535
arg2: ArmVal::Imm(imm.abs()),
3636
}
3737
}
38-
}
38+
},
39+
RiscVInstruction::Ble { arg1, arg2, target } => {
40+
let width = RiscVWidth::Double;
41+
ArmInstruction::Ble {
42+
arg1: map_register(arg1, &width),
43+
arg2: map_register(arg2, &width),
44+
target: map_val(target, &width)
45+
}
46+
},
47+
RiscVInstruction::J { target } => ArmInstruction::B {
48+
target: map_val(target, &RiscVWidth::Double)
49+
},
3950
RiscVInstruction::S { width, src, dest } => ArmInstruction::Str {
4051
width: map_width(&width),
4152
src: map_register(src, &width),

tests/test_translation.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,4 +229,33 @@ mod tests {
229229

230230
translate_to_file(riscv_asm, "test_binary_translate_write.S".to_string());
231231
}
232+
233+
#[test]
234+
fn test_loop() {
235+
let riscv_asm: Vec<RiscVInstruction> = vec![
236+
RiscVInstruction::Addi { dest: RiscVRegister::SP, src: RiscVRegister::SP, imm: -32 },
237+
RiscVInstruction::S { width: RiscVWidth::Double, src: RiscVRegister::S0FP, dest: RiscVVal::Offset { register: RiscVRegister::S0FP, offset: 24 } },
238+
RiscVInstruction::Addi { dest: RiscVRegister::SP, src: RiscVRegister::SP, imm: 32 },
239+
RiscVInstruction::S { width: RiscVWidth::Word, src: RiscVRegister::X0, dest: RiscVVal::Offset { register: RiscVRegister::S0FP, offset: -20 } },
240+
RiscVInstruction::J { target: RiscVVal::LabelOffset { label: ".L2".to_string(), offset: 0 }},
241+
RiscVInstruction::Label { name: ".L3".to_string() },
242+
RiscVInstruction::L { width: RiscVWidth::Word, dest: RiscVRegister::A5, src: RiscVVal::Offset { register:RiscVRegister::S0FP, offset: -24 } },
243+
RiscVInstruction::Addi { dest: RiscVRegister::A5, src: RiscVRegister::A5, imm: 1 },
244+
RiscVInstruction::S { width: RiscVWidth::Word, src: RiscVRegister::A5, dest: RiscVVal::Offset { register: RiscVRegister::S0FP, offset: -24 } },
245+
RiscVInstruction::L { width: RiscVWidth::Word, dest: RiscVRegister::A5, src: RiscVVal::Offset { register: RiscVRegister::S0FP, offset: -20 } },
246+
RiscVInstruction::Addi { dest: RiscVRegister::A5, src: RiscVRegister::A5, imm: 1 },
247+
RiscVInstruction::S { width: RiscVWidth::Word, src: RiscVRegister::A5, dest: RiscVVal::Offset { register: RiscVRegister::S0FP, offset: -20 } },
248+
RiscVInstruction::Label { name: ".L2".to_string() },
249+
RiscVInstruction::L { width: RiscVWidth::Word, dest: RiscVRegister::A5, src: RiscVVal::Offset { register:RiscVRegister::S0FP, offset: -20 } },
250+
RiscVInstruction::SextW { dest: RiscVRegister::A4, src: RiscVRegister::A5 },
251+
RiscVInstruction::Li { dest:RiscVRegister::A5, imm: 4 },
252+
RiscVInstruction::Ble { arg1: RiscVRegister::A4, arg2: RiscVRegister::A5, target: RiscVVal::LabelOffset { label: ".L3".to_string(), offset: 0 } },
253+
RiscVInstruction::L { width: RiscVWidth::Word, dest: RiscVRegister::A5, src: RiscVVal::Offset { register: RiscVRegister::S0FP, offset: -24 } },
254+
RiscVInstruction::Mv { dest: RiscVRegister::A0, src: RiscVRegister::A5 },
255+
RiscVInstruction::L { width: RiscVWidth::Double, dest: RiscVRegister::S0FP, src: RiscVVal::Offset { register: RiscVRegister::S0FP, offset: 24 } },
256+
RiscVInstruction::Addi { dest: RiscVRegister::SP, src: RiscVRegister::SP, imm: 32 },
257+
RiscVInstruction::Jr { target: RiscVRegister::RA },
258+
];
259+
translate_to_file(riscv_asm, "test_binary_translate_loop.S".to_string());
260+
}
232261
}

0 commit comments

Comments
 (0)