modified: src/ch2/linkedlist.rs

This commit is contained in:
wangfiox 2024-06-12 14:39:31 +08:00
parent e20c34d2e9
commit 0dd21eca39
1 changed files with 140 additions and 3 deletions

View File

@ -1,13 +1,16 @@
pub mod tests {
use compiler::backend::Inst::{Call, Jmp};
use compiler::backend::{
AddInst, Block, BranchInst, BranchOp, CallInst, Func, Inst, JmpInst, LdInst, MvInst,
SdInst, REG_A0, REG_A1, REG_A2, REG_A3, REG_RA, REG_S0, REG_S1, REG_S2, REG_SP, REG_ZERO,
AddInst, Block, BranchInst, BranchOp, CallInst, Func, Inst, JmpInst, LdInst, LwInst,
MvInst, SdInst, SwInst, REG_A0, REG_A1, REG_A2, REG_A3, REG_A4, REG_A5, REG_RA, REG_S0,
REG_S1, REG_S2, REG_SP, REG_ZERO,
};
use compiler::middle::ir::InstPtr;
#[test]
pub fn linked() {
// createLinkedList
/* ---------- ---------- createLinkedList ---------- ---------- */
let mut entry = Block::new("entry".into());
let addispsp_16 = Inst::Add(AddInst::new(REG_SP.into(), REG_SP.into(), (-16).into()));
let lia08 = Inst::Add(AddInst::new(REG_A0.into(), REG_ZERO.into(), (-16).into()));
@ -67,6 +70,8 @@ pub mod tests {
createLinkedList.push_bb(l2);
createLinkedList.push_bb(l3);
/* ---------- ---------- destroyLinkedList ---------- ---------- */
let mut entry = Block::new("entry".into());
let addispsp_32 = Inst::Add(AddInst::new(REG_SP.into(), REG_SP.into(), (-32).into()));
let sds016sp = Inst::Sd(SdInst::new(REG_S0.into(), (16).into(), REG_SP.into()));
@ -114,5 +119,137 @@ pub mod tests {
let mut destroyLinkedList = Func::new("destroyLinkedList".into(), Vec::new(), entry);
destroyLinkedList.push_bb(l32);
destroyLinkedList.push_bb(l31);
/* ---------- ---------- findNode ---------- ---------- */
let mut entry = Block::new("findNode".into());
let lda50a0 = Inst::Ld(LdInst::new(REG_A5.into(), (0).into(), REG_A0.into()));
let lda08a5 = Inst::Ld(LdInst::new(REG_A0.into(), (8).into(), REG_A5.into()));
// bne a0,zero,.L22
let bnea0zerol22 = Inst::Branch(BranchInst::new(
BranchOp::Bne,
REG_A0.into(),
REG_ZERO.into(),
"L22".into(),
));
let jl29 = Inst::Jmp(JmpInst::new("L29".into()));
entry.extend_insts(vec![lda50a0, lda08a5, bnea0zerol22, jl29]);
//
//.L24:
let mut l24 = Block::new("L24".into());
let lda08a0 = Inst::Ld(LdInst::new(REG_A0.into(), (8).into(), REG_A0.into()));
let beqa0zerol21 = Inst::Branch(BranchInst::new(
BranchOp::Beq,
REG_A0.into(),
REG_ZERO.into(),
"L21".into(),
));
l24.extend_insts(vec![lda08a0, beqa0zerol21]);
let mut l22 = Block::new("L22".into());
let lwa50a0 = Inst::Lw(LwInst::new(REG_A5.into(), (0).into(), REG_A0.into()));
let bnea5a1l24 = Inst::Branch(BranchInst::new(
BranchOp::Bne,
REG_A5.into(),
REG_A1.into(),
"L24".into(),
));
l22.extend_insts(vec![lwa50a0, bnea5a1l24]);
let mut l21 = Block::new("L21".into());
let ret = Inst::Ret;
l21.extend_insts(vec![ret]);
let mut l29 = Block::new("L21".into());
let ret = Inst::Ret;
l29.extend_insts(vec![ret]);
let mut findNode = Func::new("findNode".into(), Vec::new(), entry);
findNode.push_bb(l24);
findNode.push_bb(l22);
findNode.push_bb(l21);
findNode.push_bb(l29);
/* ---------- ---------- deleteNode ---------- ---------- */
//deleteNode:
let mut entry = Block::new("entry".into());
let lda00a0 = Inst::Ld(LdInst::new(REG_A0.into(), (0).into(), REG_A0.into()));
let jl16 = Inst::Jmp(JmpInst::new("L16".into()));
entry.extend_insts(vec![lda00a0, jl16]);
//.L20:
let mut l20 = Block::new("L20".into());
let lwa50a0 = Inst::Lw(LwInst::new(REG_A5.into(), (0).into(), REG_A0.into()));
let beqa5a1L19 = Inst::Branch(BranchInst::new(
BranchOp::Beq,
REG_A5.into(),
REG_A1.into(),
"L19".into(),
));
l20.extend_insts(vec![lwa50a0, beqa5a1L19]);
//.L16:
let mut l16 = Block::new("L16".into());
let mva4a0 = Inst::Mv(MvInst::new(REG_A4.into(), REG_A0.into()));
let lda08a0 = Inst::Ld(LdInst::new(REG_A0.into(), (8).into(), REG_A0.into()));
let bnea0zeroL20 = Inst::Branch(BranchInst::new(
BranchOp::Bne,
REG_A0.into(),
REG_ZERO.into(),
"L20".into(),
));
let ret = Inst::Ret;
l16.extend_insts(vec![mva4a0, lda08a0, bnea0zeroL20, ret]);
let mut l19 = Block::new("L19".into());
let lda58a0 = Inst::Ld(LdInst::new(REG_A5.into(), (8).into(), REG_A0.into()));
let sda58a4 = Inst::Sd(SdInst::new(REG_A5.into(), (8).into(), REG_A4.into()));
// tail free@plt
l19.extend_insts(vec![lda58a0, sda58a4]);
let mut deleteNode = Func::new("deleteNode".into(), Vec::new(), entry);
deleteNode.push_bb(l20);
deleteNode.push_bb(l16);
deleteNode.push_bb(l19);
/* ---------- ---------- insertNode ---------- ---------- */
//insertNode:
let mut entry = Block::new("entry".into());
let addispsp_32 = Inst::Add(AddInst::new(REG_SP.into(), REG_SP.into(), (-32).into()));
let sds18sp = Inst::Sd(SdInst::new(REG_S1.into(), (8).into(), REG_SP.into()));
let mvs1a0 = Inst::Mv(MvInst::new(REG_S1.into(), REG_A0.into()));
let lia016 = Inst::Add(AddInst::new(REG_A0.into(), REG_ZERO.into(), (16).into()));
let sds016sp = Inst::Sd(SdInst::new(REG_S0.into(), (16).into(), REG_SP.into()));
let sdra24sp = Inst::Sd(SdInst::new(REG_RA.into(), (24).into(), REG_SP.into()));
let mvs0a1 = Inst::Mv(MvInst::new(REG_S0.into(), REG_A1.into()));
let callmalloc = Inst::Call(CallInst::new("malloc@plt".into()));
let lda50s1 = Inst::Ld(LdInst::new(REG_A5.into(), (0).into(), REG_S1.into()));
let sws00a0 = Inst::Sw(SwInst::new(REG_S0.into(), (0).into(), REG_A0.into()));
let sdzero8a0 = Inst::Sd(SdInst::new(REG_ZERO.into(), (8).into(), REG_A0.into()));
entry.extend_insts(vec![
addispsp_32,
sds18sp,
mvs1a0,
lia016,
sds016sp,
sdra24sp,
mvs0a1,
callmalloc,
lda50s1,
sws00a0,
sdzero8a0,
]);
//.L11:
// mv a4,a5
// ld a5,8(a5)
// bne a5,zero,.L11
// ld ra,24(sp)
// ld s0,16(sp)
// sd a0,8(a4)
// ld s1,8(sp)
// addi sp,sp,32
// jr ra
}
}