modified: src/ch2/mod.rs

new file:   src/ch2/quicksort.c
	modified:   src/ch2/quicksort.rs
	new file:   src/ch2/quicksort.s
	new file:   src/ch2/student.c
	new file:   src/ch2/student.s
	new file:   src/ch3/FuncPoint.c
	new file:   src/ch3/FuncPoint.rs
	new file:   src/ch3/FuncPoint.s
	new file:   src/ch3/mod.rs
	modified:   src/main.rs
This commit is contained in:
wangfiox 2024-06-12 20:05:05 +08:00
parent aba131c4c3
commit b3ec91ebfd
11 changed files with 678 additions and 90 deletions

View File

@ -1,5 +1,5 @@
mod quicksort;
mod linkedlist;
mod quicksort;
pub use quicksort::*;
pub use linkedlist::*;
pub use quicksort::*;

50
src/ch2/quicksort.c Normal file
View File

@ -0,0 +1,50 @@
#include <stdio.h>
int QuickSort(int arr[], int low, int high) {
if (low < high) {
int i;
i = low;
int j;
j = high;
int k;
k = arr[low];
while (i < j) {
while (i < j && arr[j] > k - 1) {
j = j - 1;
}
if (i < j) {
arr[i] = arr[j];
i = i + 1;
}
while (i < j && arr[i] < k) {
i = i + 1;
}
if (i < j) {
arr[j] = arr[i];
j = j - 1;
}
}
arr[i] = k;
int tmp;
tmp = i - 1;
tmp = QuickSort(arr, low, tmp);
tmp = i + 1;
tmp = QuickSort(arr, tmp, high);
}
return 0;
}
int arr[10] = {4, 3, 9, 2, 0, 1, 6, 5, 7, 8};
int main() {
int tmp;
QuickSort(arr, 0, 9);
for (int i = 0; i < 10; i++) {
printf("%d\n", arr[i]);
}
return 0;
}

View File

@ -11,10 +11,20 @@ pub mod tests {
let sds016sp = Inst::Sd(SdInst::new(REG_S0.into(), (16).into(), REG_SP.into()));
let sds18sp = Inst::Sd(SdInst::new(REG_S1.into(), (8).into(), REG_SP.into()));
let sds20sp = Inst::Sd(SdInst::new(REG_S2.into(), (0).into(), REG_SP.into()));
let bgea1a2lbb0_17 = Inst::Branch(
BranchInst::new(BranchOp::Bge, REG_A1.into(), REG_A2.into(), "LBB0_17".into())
);
entry.extend_insts(vec![addispsp_32, sdra24sp, sds016sp, sds18sp, sds20sp, bgea1a2lbb0_17]);
let bgea1a2lbb0_17 = Inst::Branch(BranchInst::new(
BranchOp::Bge,
REG_A1.into(),
REG_A2.into(),
"LBB0_17".into(),
));
entry.extend_insts(vec![
addispsp_32,
sdra24sp,
sds016sp,
sds18sp,
sds20sp,
bgea1a2lbb0_17,
]);
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 jlbb0_3 = Inst::Jmp(JmpInst::new("LBB0_3".into()));
@ -28,11 +38,13 @@ pub mod tests {
let mva0s1 = Inst::Mv(MvInst::new(REG_A0.into(), REG_S1.into()));
let callquicksort = Inst::Call(CallInst::new("QuickSort".into()));
let addia1s01 = Inst::Add(AddInst::new(REG_A1.into(), REG_S0.into(), (1).into()));
let bgea1s2lbb0_17 = Inst::Branch(
BranchInst::new(BranchOp::Bge, REG_A1.into(), REG_S2.into(), "LBB0_17".into())
);
lbb0_2.extend_insts(
vec![
let bgea1s2lbb0_17 = Inst::Branch(BranchInst::new(
BranchOp::Bge,
REG_A1.into(),
REG_S2.into(),
"LBB0_17".into(),
));
lbb0_2.extend_insts(vec![
sllia2s02,
adda2a2s1,
swa00a2,
@ -41,17 +53,19 @@ pub mod tests {
callquicksort,
addia1s01,
bgea1s2lbb0_17,
]
);
]);
let mut lbb0_3 = Block::new("LBB0_3".into());
let sllia0a12 = Inst::SLL(SllInst::new(REG_A0.into(), REG_A1.into(), (2).into()));
let adda0a0s1 = Inst::Add(AddInst::new(REG_A0.into(), REG_A0.into(), REG_S1.into()));
let lwa00a0 = Inst::Lw(LwInst::new(REG_A0.into(), (0).into(), REG_A0.into()));
let mvs0a1 = Inst::Mv(MvInst::new(REG_S0.into(), REG_A1.into()));
let bgea1s2lbb0_2 = Inst::Branch(
BranchInst::new(BranchOp::Bge, REG_A1.into(), REG_S2.into(), "LBB0_2".into())
);
let bgea1s2lbb0_2 = Inst::Branch(BranchInst::new(
BranchOp::Bge,
REG_A1.into(),
REG_S2.into(),
"LBB0_2".into(),
));
lbb0_3.extend_insts(vec![sllia0a12, adda0a0s1, lwa00a0, mvs0a1, bgea1s2lbb0_2]);
let mvs0a1 = Inst::Mv(MvInst::new(REG_S0.into(), REG_A1.into()));
let mva2s2 = Inst::Mv(MvInst::new(REG_A2.into(), REG_S2.into()));
@ -66,15 +80,21 @@ pub mod tests {
lbb0_5.extend_insts(vec![sllis3s22, adda3a3s1, swa40a3, addia2a2_1]);
let mut llb0_6 = Block::new("LBB0_6".into());
let bges0a2lbb0_2 = Inst::Branch(
BranchInst::new(BranchOp::Bge, REG_S0.into(), REG_A2.into(), "LBB0_2".into())
);
let bges0a2lbb0_2 = Inst::Branch(BranchInst::new(
BranchOp::Bge,
REG_S0.into(),
REG_A2.into(),
"LBB0_2".into(),
));
llb0_6.extend_insts(vec![bges0a2lbb0_2]);
let mut lbb0_7 = Block::new("LBB0_7".into());
let bges0a2lbb0_11 = Inst::Branch(
BranchInst::new(BranchOp::Bge, REG_S0.into(), REG_A2.into(), "LBB0_11".into())
);
let bges0a2lbb0_11 = Inst::Branch(BranchInst::new(
BranchOp::Bge,
REG_S0.into(),
REG_A2.into(),
"LBB0_11".into(),
));
lbb0_7.extend_insts(vec![bges0a2lbb0_11]);
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()));
@ -83,22 +103,31 @@ pub mod tests {
let mut lbb0_9 = Block::new("LBB0_9".into());
let lwa50a3 = Inst::Lw(LwInst::new(REG_A5.into(), (0).into(), REG_A3.into()));
let blta5a0lbb0_12 = Inst::Branch(
BranchInst::new(BranchOp::Blt, REG_A5.into(), REG_A0.into(), "LBB0_12".into())
);
let blta5a0lbb0_12 = Inst::Branch(BranchInst::new(
BranchOp::Blt,
REG_A5.into(),
REG_A0.into(),
"LBB0_12".into(),
));
lbb0_9.extend_insts(vec![lwa50a3, blta5a0lbb0_12]);
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 addia3a3_4 = Inst::Add(AddInst::new(REG_A3.into(), REG_A3.into(), (-4).into()));
let blts0a4lbb0_9 = Inst::Branch(
BranchInst::new(BranchOp::Blt, REG_S0.into(), REG_A4.into(), "LBB0_9".into())
);
let blts0a4lbb0_9 = Inst::Branch(BranchInst::new(
BranchOp::Blt,
REG_S0.into(),
REG_A4.into(),
"LBB0_9".into(),
));
lbb0_9.extend_insts(vec![addia4a4_1, addia2a2_1, addia3a3_4, blts0a4lbb0_9]);
let mut lbb0_11 = Block::new("LBB0_11".into());
let bges0a2lbb0_6 = Inst::Branch(
BranchInst::new(BranchOp::Bge, REG_S0.into(), REG_A2.into(), "LBB0_6".into())
);
let bges0a2lbb0_6 = Inst::Branch(BranchInst::new(
BranchOp::Bge,
REG_S0.into(),
REG_A2.into(),
"LBB0_6".into(),
));
let jlbb0_13 = Inst::Jmp(JmpInst::new("LBB0_13".into()));
lbb0_11.extend_insts(vec![bges0a2lbb0_6, jlbb0_13]);
@ -107,10 +136,19 @@ pub mod tests {
let adda3a3s1 = Inst::Add(AddInst::new(REG_A3.into(), REG_A3.into(), REG_S1.into()));
let swa50a3 = Inst::Sw(SwInst::new(REG_A5.into(), (0).into(), REG_A3.into()));
let addis0s01 = Inst::Add(AddInst::new(REG_S0.into(), REG_S0.into(), (1).into()));
let bges0a2lbb0_6 = Inst::Branch(
BranchInst::new(BranchOp::Bge, REG_S0.into(), REG_A2.into(), "LBB0_6".into())
);
lbb0_12.extend_insts(vec![sllia3s02, adda3a3s1, swa50a3, addis0s01, bges0a2lbb0_6]);
let bges0a2lbb0_6 = Inst::Branch(BranchInst::new(
BranchOp::Bge,
REG_S0.into(),
REG_A2.into(),
"LBB0_6".into(),
));
lbb0_12.extend_insts(vec![
sllia3s02,
adda3a3s1,
swa50a3,
addis0s01,
bges0a2lbb0_6,
]);
let mut lbb0_13 = Block::new("LBB0_13".into());
let sllia3s02 = Inst::SLL(SllInst::new(REG_A3.into(), REG_S0.into(), (2).into()));
@ -118,18 +156,23 @@ pub mod tests {
lbb0_13.extend_insts(vec![sllia3s02, adda3a3s1]);
let mut lbb0_14 = Block::new("LBB0_14".into());
// lw a4, 0(a3)
let lwa40a3 = Inst::Lw(LwInst::new(REG_A4.into(), (0).into(), REG_A3.into()));
let bgea4a0lbb0_5 = Inst::Branch(
BranchInst::new(BranchOp::Bge, REG_A4.into(), REG_A0.into(), "LBB0_5".into())
);
let bgea4a0lbb0_5 = Inst::Branch(BranchInst::new(
BranchOp::Bge,
REG_A4.into(),
REG_A0.into(),
"LBB0_5".into(),
));
lbb0_14.extend_insts(vec![lwa40a3, bgea4a0lbb0_5]);
// push_inst lw
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 bnea2s0lbb0_14 = Inst::Branch(
BranchInst::new(BranchOp::Bne, REG_A2.into(), REG_S0.into(), "LBB0_14".into())
);
let bnea2s0lbb0_14 = Inst::Branch(BranchInst::new(
BranchOp::Bne,
REG_A2.into(),
REG_S0.into(),
"LBB0_14".into(),
));
lbb0_14.extend_insts(vec![addis0s01, addia3a34, bnea2s0lbb0_14]);
let mvs0a2 = Inst::Mv(MvInst::new(REG_S0.into(), REG_A2.into()));
let jlbb0_2 = Inst::Jmp(JmpInst::new("LBB0_2".into()));
@ -143,7 +186,9 @@ pub mod tests {
let lds20sp = Inst::Ld(LdInst::new(REG_S2.into(), (0).into(), REG_SP.into()));
let addispsp32 = Inst::Add(AddInst::new(REG_SP.into(), REG_SP.into(), (32).into()));
let ret = Inst::Ret;
lbb0_17.extend_insts(vec![lia00, ldra24sp, lds016sp, lds18sp, lds20sp, addispsp32, ret]);
lbb0_17.extend_insts(vec![
lia00, ldra24sp, lds016sp, lds18sp, lds20sp, addispsp32, ret,
]);
let mut quickSort = Func::new("QuickSort".into(), Vec::new(), entry);
quickSort.push_bb(lbb0_2);
@ -158,45 +203,49 @@ pub mod tests {
quickSort.push_bb(lbb0_14);
quickSort.push_bb(lbb0_17);
// .LC0:
// .string "%d\n"
let mut entry = Block::new("entry".into());
let addispsp_32 = Inst::Add(AddInst::new(REG_SP.into(), REG_SP.into(), (-32).into()));
let lia29 = Inst::Add(AddInst::new(REG_A2.into(), REG_ZERO.into(), (9).into()));
let lia10 = Inst::Add(AddInst::new(REG_A0.into(), REG_ZERO.into(), (9).into()));
// TODO lla a0 arr
// lla a0 arr
let llaa0arr = Inst::La(LaInst::new(REG_A0.into(), "arr".into()));
let sds016sp = Inst::Sd(SdInst::new(REG_S0.into(), (16).into(), REG_SP.into()));
let sds18sp = Inst::Sd(SdInst::new(REG_S1.into(), (8).into(), REG_SP.into()));
let sds20sp = Inst::Sd(SdInst::new(REG_S2.into(), (0).into(), REG_SP.into()));
let sdra24sp = Inst::Sd(SdInst::new(REG_RA.into(), (24).into(), REG_SP.into()));
// lla s0 arr
let llas0arr = Inst::La(LaInst::new(REG_S0.into(), "arr".into()));
let callquicksort = Inst::Call(CallInst::new("QuickSort".into()));
// lla s2 arr+40
let llas2arr = Inst::La(LaInst::new(REG_S2.into(), "arr+40".into()));
// lla s1 LC0 , lc0 is .string "%d\n"
entry.extend_insts(
vec![
let llas1lc0 = Inst::La(LaInst::new(REG_S1.into(), "LC0".into()));
entry.extend_insts(vec![
addispsp_32,
lia29,
lia10,
/* lla a0, arr */ sds016sp,
/* lla a0, arr */ llaa0arr,
sds016sp,
sds18sp,
sds20sp,
sdra24sp,
/* lla s0 arr */
callquicksort, /* lla s1 LC0 */
/* lla s2, arr+40 */
]
);
llas0arr,
callquicksort,
llas2arr,
llas1lc0,
]);
let mut l35 = Block::new("L35".into());
let lwa10s0 = Inst::Lw(LwInst::new(REG_A1.into(), (0).into(), REG_S0.into()));
let mva0s1 = Inst::Mv(MvInst::new(REG_A0.into(), REG_S1.into()));
let addis0s04 = Inst::Add(AddInst::new(REG_S0.into(), REG_S0.into(), (4).into()));
let callprintf = Inst::Call(CallInst::new("printf@plt".into()));
let bnes0s2l35 = Inst::Branch(
BranchInst::new(BranchOp::Bne, REG_S0.into(), REG_S2.into(), "L35".into())
);
let bnes0s2l35 = Inst::Branch(BranchInst::new(
BranchOp::Bne,
REG_S0.into(),
REG_S2.into(),
"L35".into(),
));
let ldra24sp = Inst::Ld(LdInst::new(REG_RA.into(), (24).into(), REG_SP.into()));
let lds016sp = Inst::Ld(LdInst::new(REG_S0.into(), (16).into(), REG_SP.into()));
let lds18sp = Inst::Ld(LdInst::new(REG_S1.into(), (8).into(), REG_SP.into()));
@ -204,26 +253,13 @@ pub mod tests {
let lia00 = Inst::Add(AddInst::new(REG_A0.into(), REG_ZERO.into(), (0).into()));
let addispsp32 = Inst::Add(AddInst::new(REG_SP.into(), REG_SP.into(), (32).into()));
let ret = Inst::Ret;
l35.extend_insts(
vec![
lwa10s0,
mva0s1,
addis0s04,
callprintf,
bnes0s2l35,
ldra24sp,
lds016sp,
lds18sp,
lds20sp,
lia00,
addispsp32,
ret,
]
);
l35.extend_insts(vec![
lwa10s0, mva0s1, addis0s04, callprintf, bnes0s2l35, ldra24sp, lds016sp, lds18sp,
lds20sp, lia00, addispsp32, ret,
]);
let mut Main = Func::new("main".into(), Vec::new(), entry);
Main.push_bb(l35);
// let lc0 = Var::Str { Str { name : "LC0", init : "%d\n", is_const : true } };
let lc0 = Var::Str(Str {
name: "LC0".into(),
init: Some("%d\n".to_string()),

159
src/ch2/quicksort.s Normal file
View File

@ -0,0 +1,159 @@
.file "quicksort.c"
.option pic
.attribute arch, "rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0_zifencei2p0"
.attribute unaligned_access, 0
.attribute stack_align, 16
.text
.align 1
.type QuickSort.part.0, @function
QuickSort.part.0:
addi sp,sp,-32
sd s1,8(sp)
sd s2,0(sp)
sd ra,24(sp)
sd s0,16(sp)
mv s1,a0
mv s2,a2
.L12:
slli a5,a1,2
add a5,s1,a5
lw a7,0(a5)
mv a4,s2
mv a3,a1
mv s0,a1
bge a1,s2,.L3
.L2:
slli a5,a4,2
add a5,s1,a5
j .L10
.L5:
addiw a4,a4,-1
beq a4,a3,.L22
.L10:
lw a6,0(a5)
addi a5,a5,-4
bge a6,a7,.L5
slli a5,a3,2
add a0,s1,a5
addiw s0,a3,1
ble a4,a3,.L4
slli t1,a4,2
add t1,s1,t1
lw a3,0(t1)
sw a3,0(a0)
ble a4,s0,.L24
addi a5,a5,4
add a5,s1,a5
j .L7
.L9:
addi a5,a5,4
beq a4,s0,.L25
.L7:
lw a6,0(a5)
mv a3,s0
addiw s0,s0,1
blt a6,a7,.L9
ble a4,a3,.L15
sw a6,0(t1)
addiw a4,a4,-1
bgt a4,a3,.L2
.L15:
mv s0,a3
j .L3
.L25:
slli a5,s0,2
add a5,s1,a5
.L3:
sw a7,0(a5)
addiw a2,s0,-1
bge a1,a2,.L11
mv a0,s1
call QuickSort.part.0
.L11:
addiw a1,s0,1
blt a1,s2,.L12
ld ra,24(sp)
ld s0,16(sp)
ld s1,8(sp)
ld s2,0(sp)
addi sp,sp,32
jr ra
.L24:
mv a3,s0
.L22:
slli a5,a3,2
.L4:
add a5,s1,a5
mv s0,a3
j .L3
.size QuickSort.part.0, .-QuickSort.part.0
.align 1
.globl QuickSort
.type QuickSort, @function
QuickSort:
bge a1,a2,.L29
addi sp,sp,-16
sd ra,8(sp)
call QuickSort.part.0
ld ra,8(sp)
li a0,0
addi sp,sp,16
jr ra
.L29:
li a0,0
ret
.size QuickSort, .-QuickSort
.section .rodata.str1.8,"aMS",@progbits,1
.align 3
.LC0:
.string "%d\n"
.section .text.startup,"ax",@progbits
.align 1
.globl main
.type main, @function
main:
addi sp,sp,-32
li a2,9
li a1,0
lla a0,.LANCHOR0
sd s0,16(sp)
sd s1,8(sp)
sd s2,0(sp)
sd ra,24(sp)
lla s0,.LANCHOR0
call QuickSort.part.0
lla s2,.LANCHOR0+40
lla s1,.LC0
.L33:
lw a1,0(s0)
mv a0,s1
addi s0,s0,4
call printf@plt
bne s0,s2,.L33
ld ra,24(sp)
ld s0,16(sp)
ld s1,8(sp)
ld s2,0(sp)
li a0,0
addi sp,sp,32
jr ra
.size main, .-main
.globl arr
.data
.align 3
.set .LANCHOR0,. + 0
.type arr, @object
.size arr, 40
arr:
.word 4
.word 3
.word 9
.word 2
.word 0
.word 1
.word 6
.word 5
.word 7
.word 8
.ident "GCC: (Debian 12.2.0-13) 12.2.0"
.section .note.GNU-stack,"",@progbits

23
src/ch2/student.c Normal file
View File

@ -0,0 +1,23 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct date {
int year;
int month;
int day;
} DATE;
typedef struct student {
long studentID;
char studentName[10];
char studentSex;
DATE birthday;
int score[4];
} STUDENT;
STUDENT changeSex(STUDENT stu, int n, int m) {
STUDENT newStu = stu;
newStu.studentSex = 10;
return newStu;
}

28
src/ch2/student.s Normal file
View File

@ -0,0 +1,28 @@
.file "student.c"
.option pic
.attribute arch, "rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0_zifencei2p0"
.attribute unaligned_access, 0
.attribute stack_align, 16
.text
.align 1
.globl changeSex
.type changeSex, @function
changeSex:
ld a4,40(a1)
ld t1,16(a1)
ld a7,0(a1)
ld a6,8(a1)
ld a2,24(a1)
ld a3,32(a1)
sd t1,16(a0)
sd a4,40(a0)
li a4,10
sd a7,0(a0)
sd a6,8(a0)
sd a2,24(a0)
sd a3,32(a0)
sb a4,18(a0)
ret
.size changeSex, .-changeSex
.ident "GCC: (Debian 12.2.0-13) 12.2.0"
.section .note.GNU-stack,"",@progbits

49
src/ch3/FuncPoint.c Normal file
View File

@ -0,0 +1,49 @@
#include <stdbool.h>
#include <stdio.h>
// 二元谓词函数原型,用于比较两个整数
bool ascending(int a, int b) { return a > b; }
bool descending(int a, int b) { return a < b; }
// 通用排序函数,使用函数指针作为二元谓词
void sort(int *array, int size, bool (*compare)(int, int)) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (compare(array[j], array[j + 1])) {
// 交换元素
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
// 打印数组
void printArray(int *array, int size) {
for (int i = 0; i < size; i++) {
printf("%d ", array[i]);
}
putchar('\n');
}
int main() {
int array[] = {5, 2, 9, 1, 5, 6};
int size = sizeof(array) / sizeof(array[0]);
printf("Original array: \n");
printArray(array, size);
// 使用升序排序
sort(array, size, ascending);
printf("Sorted array in ascending order: \n");
printArray(array, size);
// 使用降序排序
sort(array, size, descending);
printf("Sorted array in descending order: \n");
printArray(array, size);
return 0;
}

55
src/ch3/FuncPoint.rs Normal file
View File

@ -0,0 +1,55 @@
mod tests {
use compiler::backend::{
AddInst, Block, BranchInst, BranchOp, Func, Inst, MvInst, SdInst, REG_A0, REG_A1, REG_A5,
REG_RA, REG_S2, REG_S5, REG_SP, REG_ZERO,
};
#[test]
pub fn test() {
/* ---------- ---------- ascending ---------- ---------- */
//ascending:
// sgt a0,a0,a1
// ret
let mut entry = Block::new("entry".into());
// sgt
let ret = Inst::Ret;
entry.extend_insts(vec![ret]);
let ascending = Func::new("ascending".into(), Vec::new(), entry);
/* ---------- ---------- descending ---------- ---------- */
let mut entry = Block::new("entry".into());
// slt a0,a0,a1
let ret = Inst::Ret;
entry.extend_insts(vec![ret]);
let descending = Func::new("descending".into(), Vec::new(), entry);
/* ---------- ---------- sort ---------- ---------- */
let mut entry = Block::new("entry".into());
let lia51 = Inst::Add(AddInst::new(REG_A5.into(), REG_ZERO.into(), (1).into()));
let blea1a5L20 = Inst::Branch(BranchInst::new(
BranchOp::Ble,
REG_A1.into(),
REG_A5.into(),
"L20".into(),
));
let addispsp_64 = Inst::Add(AddInst::new(REG_SP.into(), REG_SP.into(), (-64).into()));
let sds232sp = Inst::Sd(SdInst::new(REG_S2.into(), (32).into(), REG_SP.into()));
let sds324sp = Inst::Sd(SdInst::new(REG_S3.into(), (24).into(), REG_SP.into()));
let sds416sp = Inst::Sd(SdInst::new(REG_S4.into(), (16).into(), REG_SP.into()));
let sds58sp = Inst::Sd(SdInst::new(REG_S5.into(), (8).into(), REG_SP.into()));
let sds60sp = Inst::Sd(SdInst::new(REG_S6.into(), (0).into(), REG_SP.into()));
let sdra56sp = Inst::Sd(SdInst::new(REG_RA.into(), (56).into(), REG_SP.into()));
let sds048sp = Inst::Sd(SdInst::new(REG_S0.into(), (48).into(), REG_SP.into()));
let sds140sp = Inst::Sd(SdInst::new(REG_S1.into(), (40).into(), REG_SP.into()));
let mvs5a0 = Inst::Mv(MvInst::new(REG_S5.into(), REG_A0.into()));
let mvs2a2 = Inst::Mv(MvInst::new(REG_S2.into(), REG_A2.into()));
// 4 │ mv> s5,a0
// 3 │ mv> s2,a2
// 2 │ sext.w> s3,a1
// 1 │ addi> s6,a0,4
// 40 │ li> s4,1
}
}

184
src/ch3/FuncPoint.s Normal file
View File

@ -0,0 +1,184 @@
.file "FuncPoint.c"
.option pic
.attribute arch, "rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0_zifencei2p0"
.attribute unaligned_access, 0
.attribute stack_align, 16
.text
.align 1
.globl ascending
.type ascending, @function
ascending:
sgt a0,a0,a1
ret
.size ascending, .-ascending
.align 1
.globl descending
.type descending, @function
descending:
slt a0,a0,a1
ret
.size descending, .-descending
.align 1
.globl sort
.type sort, @function
sort:
li a5,1
ble a1,a5,.L20
addi sp,sp,-64
sd s2,32(sp)
sd s3,24(sp)
sd s4,16(sp)
sd s5,8(sp)
sd s6,0(sp)
sd ra,56(sp)
sd s0,48(sp)
sd s1,40(sp)
mv s5,a0
mv s2,a2
sext.w s3,a1
addi s6,a0,4
li s4,1
.L6:
ble s3,s4,.L23
addiw s1,s3,-2
slli a5,s1,32
srli s1,a5,30
mv s0,s5
add s1,s1,s6
.L8:
lw a1,4(s0)
lw a0,0(s0)
jalr s2
beq a0,zero,.L7
lw a4,0(s0)
lw a5,4(s0)
sw a4,4(s0)
sw a5,0(s0)
.L7:
addi s0,s0,4
bne s0,s1,.L8
addiw s3,s3,-1
bne s3,s4,.L6
ld ra,56(sp)
ld s0,48(sp)
ld s1,40(sp)
ld s2,32(sp)
ld s3,24(sp)
ld s4,16(sp)
ld s5,8(sp)
ld s6,0(sp)
addi sp,sp,64
jr ra
.L23:
addiw s3,s3,-1
j .L6
.L20:
ret
.size sort, .-sort
.section .rodata.str1.8,"aMS",@progbits,1
.align 3
.LC1:
.string "%d "
.text
.align 1
.globl printArray
.type printArray, @function
printArray:
ble a1,zero,.L29
addi sp,sp,-32
slli a1,a1,2
sd s0,16(sp)
sd s1,8(sp)
sd s2,0(sp)
sd ra,24(sp)
mv s0,a0
add s1,a0,a1
lla s2,.LC1
.L26:
lw a1,0(s0)
mv a0,s2
addi s0,s0,4
call printf@plt
bne s0,s1,.L26
ld s0,16(sp)
la a5,stdout
ld ra,24(sp)
ld s1,8(sp)
ld s2,0(sp)
ld a1,0(a5)
li a0,10
addi sp,sp,32
tail putc@plt
.L29:
la a5,stdout
ld a1,0(a5)
li a0,10
tail putc@plt
.size printArray, .-printArray
.section .rodata.str1.8
.align 3
.LC2:
.string "Original array: "
.align 3
.LC3:
.string "Sorted array in ascending order: "
.align 3
.LC4:
.string "Sorted array in descending order: "
.section .text.startup,"ax",@progbits
.align 1
.globl main
.type main, @function
main:
lla a5,.LANCHOR0
ld a3,0(a5)
ld a4,8(a5)
ld a5,16(a5)
addi sp,sp,-48
lla a0,.LC2
sd ra,40(sp)
sd a3,8(sp)
sd a4,16(sp)
sd a5,24(sp)
sd s0,32(sp)
addi s0,sp,8
call puts@plt
mv a0,s0
li a1,6
call printArray
lla a2,ascending
li a1,6
mv a0,s0
call sort
lla a0,.LC3
call puts@plt
mv a0,s0
li a1,6
call printArray
lla a2,descending
li a1,6
mv a0,s0
call sort
lla a0,.LC4
call puts@plt
mv a0,s0
li a1,6
call printArray
ld ra,40(sp)
ld s0,32(sp)
li a0,0
addi sp,sp,48
jr ra
.size main, .-main
.section .rodata
.align 3
.set .LANCHOR0,. + 0
.LC0:
.word 5
.word 2
.word 9
.word 1
.word 5
.word 6
.ident "GCC: (Debian 12.2.0-13) 12.2.0"
.section .note.GNU-stack,"",@progbits

3
src/ch3/mod.rs Normal file
View File

@ -0,0 +1,3 @@
mod FuncPoint;
use FuncPoint::*;

View File

@ -1,6 +1,7 @@
mod ch2;
mod ch3;
use std::{ fs::File, io::Write };
use std::{fs::File, io::Write};
use compiler::backend::*;