modified: Cargo.toml

modified:   src/ch2/quicksort.rs
This commit is contained in:
wangfiox 2024-06-10 20:52:47 +08:00
parent f8d2a01544
commit 8f796fc50d
2 changed files with 29 additions and 48 deletions

View File

@ -5,6 +5,3 @@ edition = "2021"
[dependencies] [dependencies]
hitc = { path = "./hitc" } hitc = { path = "./hitc" }
[features]
default = ["test"]

View File

@ -6,7 +6,7 @@ pub mod tests {
pub fn quicksort_example() { pub fn quicksort_example() {
let mut quickSort = Func::new("QuickSort".into(), Vec::new()); let mut quickSort = Func::new("QuickSort".into(), Vec::new());
let mut bb0 = Block::new("bb0".into()); let mut entry = Block::new("entry".into());
let addispsp_32 = Inst::Add(AddInst::new(REG_SP.into(), REG_SP.into(), (-32).into())); let addispsp_32 = Inst::Add(AddInst::new(REG_SP.into(), REG_SP.into(), (-32).into()));
let sdra24sp = Inst::Sd(SdInst::new(REG_RA.into(), (24).into(), REG_SP.into())); let sdra24sp = Inst::Sd(SdInst::new(REG_RA.into(), (24).into(), REG_SP.into()));
let sds016sp = Inst::Sd(SdInst::new(REG_S0.into(), (16).into(), REG_SP.into())); let sds016sp = Inst::Sd(SdInst::new(REG_S0.into(), (16).into(), REG_SP.into()));
@ -15,25 +15,23 @@ pub mod tests {
let bgea1a2lbb0_17 = Inst::Branch( let bgea1a2lbb0_17 = Inst::Branch(
BranchInst::new(BranchOp::Bge, REG_A1.into(), REG_A2.into(), "LBB0_17".into()) BranchInst::new(BranchOp::Bge, REG_A1.into(), REG_A2.into(), "LBB0_17".into())
); );
bb0.push_inst(addispsp_32); entry.push_inst(addispsp_32);
bb0.push_inst(sdra24sp); entry.push_inst(sdra24sp);
bb0.push_inst(sds016sp); entry.push_inst(sds016sp);
bb0.push_inst(sds18sp); entry.push_inst(sds18sp);
bb0.push_inst(sds20sp); entry.push_inst(sds20sp);
bb0.push_inst(bgea1a2lbb0_17); entry.push_inst(bgea1a2lbb0_17);
let mut bb1 = Block::new("bb1".into());
let mvs2a2 = Inst::Mv(MvInst::new(REG_S2.into(), REG_A2.into())); let mvs2a2 = Inst::Mv(MvInst::new(REG_S2.into(), REG_A2.into()));
let mvs1a0 = Inst::Mv(MvInst::new(REG_S1.into(), REG_A0.into())); let mvs1a0 = Inst::Mv(MvInst::new(REG_S1.into(), REG_A0.into()));
let jlbb0_3 = Inst::Jmp(JmpInst::new("LBB0_3".into())); let jlbb0_3 = Inst::Jmp(JmpInst::new("LBB0_3".into()));
bb1.push_inst(mvs2a2); entry.push_inst(mvs2a2);
bb1.push_inst(mvs1a0); entry.push_inst(mvs1a0);
bb1.push_inst(jlbb0_3); entry.push_inst(jlbb0_3);
let mut lbb0_2 = Block::new("lbb0_2".into()); let mut lbb0_2 = Block::new("lbb0_2".into());
let sllia2s02 = Inst::SLL(SllInst::new(REG_A2.into(), REG_S0.into(), (2).into())); let sllia2s02 = Inst::SLL(SllInst::new(REG_A2.into(), REG_S0.into(), (2).into()));
let adda2a2s1 = Inst::Add(AddInst::new(REG_A2.into(), REG_A2.into(), REG_S1.into())); let adda2a2s1 = Inst::Add(AddInst::new(REG_A2.into(), REG_A2.into(), REG_S1.into()));
let swa00a2 = Inst::Sd(SdInst::new(REG_A0.into(), (0).into(), REG_A2.into())); let swa00a2 = Inst::Sw(SwInst::new(REG_A0.into(), (0).into(), REG_A2.into()));
let addia2s0_1 = Inst::Add(AddInst::new(REG_A2.into(), REG_S0.into(), (-1).into())); let addia2s0_1 = Inst::Add(AddInst::new(REG_A2.into(), REG_S0.into(), (-1).into()));
let mva0s1 = Inst::Mv(MvInst::new(REG_A0.into(), REG_S1.into())); let mva0s1 = Inst::Mv(MvInst::new(REG_A0.into(), REG_S1.into()));
let callquicksort = Inst::Call(CallInst::new("QuickSort".into())); let callquicksort = Inst::Call(CallInst::new("QuickSort".into()));
@ -63,14 +61,12 @@ pub mod tests {
// lbb0_3.push_inst(lwa00a0) // lbb0_3.push_inst(lwa00a0)
lbb0_3.push_inst(mvs0a1); lbb0_3.push_inst(mvs0a1);
lbb0_3.push_inst(bgea1s2lbb0_2); lbb0_3.push_inst(bgea1s2lbb0_2);
let mut bb4 = Block::new("bb4".into());
let mvs0a1 = Inst::Mv(MvInst::new(REG_S0.into(), REG_A1.into())); let mvs0a1 = Inst::Mv(MvInst::new(REG_S0.into(), REG_A1.into()));
let mva2s2 = Inst::Mv(MvInst::new(REG_A2.into(), REG_S2.into())); let mva2s2 = Inst::Mv(MvInst::new(REG_A2.into(), REG_S2.into()));
let jlbb0_7 = Inst::Jmp(JmpInst::new("LBB0_7".into())); let jlbb0_7 = Inst::Jmp(JmpInst::new("LBB0_7".into()));
bb4.push_inst(mvs0a1); lbb0_3.push_inst(mvs0a1);
bb4.push_inst(mva2s2); lbb0_3.push_inst(mva2s2);
bb4.push_inst(jlbb0_7); lbb0_3.push_inst(jlbb0_7);
let mut lbb0_5 = Block::new("LBB0_5".into()); let mut lbb0_5 = Block::new("LBB0_5".into());
let sllis3s22 = Inst::SLL(SllInst::new(REG_S3.into(), REG_S2.into(), (2).into())); let sllis3s22 = Inst::SLL(SllInst::new(REG_S3.into(), REG_S2.into(), (2).into()));
@ -93,14 +89,12 @@ pub mod tests {
BranchInst::new(BranchOp::Bge, REG_S0.into(), REG_A2.into(), "LBB0_11".into()) BranchInst::new(BranchOp::Bge, REG_S0.into(), REG_A2.into(), "LBB0_11".into())
); );
lbb0_7.push_inst(bges0a2lbb0_11); lbb0_7.push_inst(bges0a2lbb0_11);
let mut bb8 = Block::new("bb8".into());
let sllia3a22 = Inst::SLL(SllInst::new(REG_A3.into(), REG_A2.into(), (2).into())); let sllia3a22 = Inst::SLL(SllInst::new(REG_A3.into(), REG_A2.into(), (2).into()));
let adda3a3s1 = Inst::Add(AddInst::new(REG_A3.into(), REG_A3.into(), REG_S1.into())); let adda3a3s1 = Inst::Add(AddInst::new(REG_A3.into(), REG_A3.into(), REG_S1.into()));
let mva4a2 = Inst::Mv(MvInst::new(REG_A4.into(), REG_A2.into())); let mva4a2 = Inst::Mv(MvInst::new(REG_A4.into(), REG_A2.into()));
bb8.push_inst(sllia3a22); lbb0_7.push_inst(sllia3a22);
bb8.push_inst(adda3a3s1); lbb0_7.push_inst(adda3a3s1);
bb8.push_inst(mva4a2); lbb0_7.push_inst(mva4a2);
let mut lbb0_9 = Block::new("LBB0_9".into()); let mut lbb0_9 = Block::new("LBB0_9".into());
let lwa50a3 = Inst::Ld(LdInst::new(REG_A5.into(), (0).into(), REG_A3.into())); let lwa50a3 = Inst::Ld(LdInst::new(REG_A5.into(), (0).into(), REG_A3.into()));
@ -109,17 +103,16 @@ pub mod tests {
); );
lbb0_9.push_inst(lwa50a3); lbb0_9.push_inst(lwa50a3);
lbb0_9.push_inst(blta5a0lbb0_12); lbb0_9.push_inst(blta5a0lbb0_12);
let mut bb10 = Block::new("bb10".into());
let addia4a4_1 = Inst::Add(AddInst::new(REG_A4.into(), REG_A4.into(), (-1).into())); let addia4a4_1 = Inst::Add(AddInst::new(REG_A4.into(), REG_A4.into(), (-1).into()));
let addia2a2_1 = Inst::Add(AddInst::new(REG_A2.into(), REG_A2.into(), (-1).into())); let addia2a2_1 = Inst::Add(AddInst::new(REG_A2.into(), REG_A2.into(), (-1).into()));
let addia3a3_4 = Inst::Add(AddInst::new(REG_A3.into(), REG_A3.into(), (-4).into())); let addia3a3_4 = Inst::Add(AddInst::new(REG_A3.into(), REG_A3.into(), (-4).into()));
let blts0a4lbb0_9 = Inst::Branch( let blts0a4lbb0_9 = Inst::Branch(
BranchInst::new(BranchOp::Blt, REG_S0.into(), REG_A4.into(), "LBB0_9".into()) BranchInst::new(BranchOp::Blt, REG_S0.into(), REG_A4.into(), "LBB0_9".into())
); );
bb10.push_inst(addia4a4_1); lbb0_9.push_inst(addia4a4_1);
bb10.push_inst(addia2a2_1); lbb0_9.push_inst(addia2a2_1);
bb10.push_inst(addia3a3_4); lbb0_9.push_inst(addia3a3_4);
lbb0_9.push_inst(blts0a4lbb0_9);
let mut lbb0_11 = Block::new("LBB0_11".into()); let mut lbb0_11 = Block::new("LBB0_11".into());
let bges0a2lbb0_6 = Inst::Branch( let bges0a2lbb0_6 = Inst::Branch(
@ -154,24 +147,20 @@ pub mod tests {
let bgea4a0lbb0_5 = Inst::Branch( let bgea4a0lbb0_5 = Inst::Branch(
BranchInst::new(BranchOp::Bge, REG_A4.into(), REG_A0.into(), "LBB0_5".into()) BranchInst::new(BranchOp::Bge, REG_A4.into(), REG_A0.into(), "LBB0_5".into())
); );
// lw
lbb0_14.push_inst(bgea4a0lbb0_5); lbb0_14.push_inst(bgea4a0lbb0_5);
// push_inst lw
let mut lbb0_15 = Block::new("LBB0_15".into());
let addis0s01 = Inst::Add(AddInst::new(REG_S0.into(), REG_S0.into(), (1).into())); let addis0s01 = Inst::Add(AddInst::new(REG_S0.into(), REG_S0.into(), (1).into()));
let addia3a34 = Inst::Add(AddInst::new(REG_A3.into(), REG_A3.into(), (4).into())); let addia3a34 = Inst::Add(AddInst::new(REG_A3.into(), REG_A3.into(), (4).into()));
let bnea2s0lbb0_14 = Inst::Branch( let bnea2s0lbb0_14 = Inst::Branch(
BranchInst::new(BranchOp::Bne, REG_A2.into(), REG_S0.into(), "LBB0_14".into()) BranchInst::new(BranchOp::Bne, REG_A2.into(), REG_S0.into(), "LBB0_14".into())
); );
lbb0_15.push_inst(addis0s01); lbb0_14.push_inst(addis0s01);
lbb0_15.push_inst(addia3a34); lbb0_14.push_inst(addia3a34);
lbb0_15.push_inst(bnea2s0lbb0_14); lbb0_14.push_inst(bnea2s0lbb0_14);
let mut bb16 = Block::new("bb16".into());
let mvs0a2 = Inst::Mv(MvInst::new(REG_S0.into(), REG_A2.into())); let mvs0a2 = Inst::Mv(MvInst::new(REG_S0.into(), REG_A2.into()));
let jlbb0_2 = Inst::Jmp(JmpInst::new("LBB0_2".into())); let jlbb0_2 = Inst::Jmp(JmpInst::new("LBB0_2".into()));
bb16.push_inst(mvs0a2); lbb0_14.push_inst(mvs0a2);
bb16.push_inst(jlbb0_2); lbb0_14.push_inst(jlbb0_2);
let mut lbb0_17 = Block::new("LBB0_17".into()); let mut lbb0_17 = Block::new("LBB0_17".into());
// let lia00 = Inst::Li(LiInst::new(REG_A0.into(), (0).into())); // let lia00 = Inst::Li(LiInst::new(REG_A0.into(), (0).into()));
@ -189,25 +178,20 @@ pub mod tests {
lbb0_17.push_inst(addispsp32); lbb0_17.push_inst(addispsp32);
lbb0_17.push_inst(ret); lbb0_17.push_inst(ret);
quickSort.push_bb(bb0); quickSort.push_bb(entry);
quickSort.push_bb(bb1);
quickSort.push_bb(lbb0_2); quickSort.push_bb(lbb0_2);
quickSort.push_bb(lbb0_3); quickSort.push_bb(lbb0_3);
quickSort.push_bb(bb4);
quickSort.push_bb(lbb0_5); quickSort.push_bb(lbb0_5);
quickSort.push_bb(llb0_6); quickSort.push_bb(llb0_6);
quickSort.push_bb(lbb0_7); quickSort.push_bb(lbb0_7);
quickSort.push_bb(bb8);
quickSort.push_bb(lbb0_9); quickSort.push_bb(lbb0_9);
quickSort.push_bb(bb10);
quickSort.push_bb(lbb0_11); quickSort.push_bb(lbb0_11);
quickSort.push_bb(lbb0_12); quickSort.push_bb(lbb0_12);
quickSort.push_bb(lbb0_13); quickSort.push_bb(lbb0_13);
quickSort.push_bb(lbb0_14); quickSort.push_bb(lbb0_14);
quickSort.push_bb(lbb0_15);
quickSort.push_bb(bb16);
quickSort.push_bb(lbb0_17); quickSort.push_bb(lbb0_17);
quickSort.set_entry("entry".into());
let asm = quickSort.gen_asm(); let asm = quickSort.gen_asm();
print!("{}", asm); print!("{}", asm);
} }