Compare commits

..

2 Commits

Author SHA1 Message Date
wangfiox 20f58e176a modified: Cargo.lock
modified:   hitc
	modified:   src/ch2/quicksort.rs
	modified:   src/ch3/FuncPoint.rs
	modified:   hitc (modified content)
2024-06-12 20:10:29 +08:00
wangfiox b3ec91ebfd 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
2024-06-12 20:05:05 +08:00
13 changed files with 566 additions and 100 deletions

81
Cargo.lock generated
View File

@ -2,15 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "atty"
version = "0.2.14"
@ -47,12 +38,6 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1"
[[package]]
name = "cc"
version = "1.0.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96c51067fd44124faa7f870b4b1c969379ad32b2ba805aa959430ceaa384f695"
[[package]]
name = "cfg-if"
version = "1.0.0"
@ -174,7 +159,6 @@ version = "0.1.0"
dependencies = [
"clap",
"lazy_static",
"llvm-ir",
"once_cell",
"rand",
"rayon",
@ -230,36 +214,6 @@ version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89"
[[package]]
name = "llvm-ir"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "434db35c1dc3bce4d16bb60c4f6a57587dfa8224ba92fc080fde38782c35fc90"
dependencies = [
"either",
"llvm-sys",
"log",
]
[[package]]
name = "llvm-sys"
version = "160.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49f9888529887dfda6c59e20f0e0727b17e826cd54ae1ddf0d4c83850fa23b69"
dependencies = [
"cc",
"lazy_static",
"libc",
"regex",
"semver",
]
[[package]]
name = "log"
version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]]
name = "memchr"
version = "2.7.2"
@ -352,35 +306,6 @@ dependencies = [
"crossbeam-utils",
]
[[package]]
name = "regex"
version = "1.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b91213439dad192326a0d7c6ee3955910425f441d7038e0d6933b0aec5c4517f"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b"
[[package]]
name = "rustix"
version = "0.38.34"
@ -400,12 +325,6 @@ version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "semver"
version = "1.0.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b"
[[package]]
name = "serde"
version = "1.0.203"

2
hitc

@ -1 +1 @@
Subproject commit f3b9eefa3cb53f181d09328a66efeb3343ea7d3f
Subproject commit c5a2f47e6a5817b47ac5670a8c22acc76b0662b4

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

@ -1,7 +1,7 @@
// #[cfg(test)]
pub mod tests {
use compiler::{backend::*, middle::ir::instruction::binary_inst::Add};
use var::{ArrVar, Str, Var};
use compiler::backend::*;
use var::*;
#[test]
pub fn quicksort_example() {
@ -40,7 +40,7 @@ pub mod tests {
mva0s1,
callquicksort,
addia1s01,
bgea1s2lbb0_17,
bgea1s2lbb0_17
]
);
@ -118,7 +118,6 @@ 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())
@ -158,34 +157,37 @@ 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"
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
]
);
@ -217,13 +219,12 @@ pub mod tests {
lds20sp,
lia00,
addispsp32,
ret,
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()),
@ -243,7 +244,7 @@ pub mod tests {
(6, 6),
(7, 5),
(8, 7),
(9, 9),
(9, 9)
],
is_const: false,
});

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;
}

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

@ -0,0 +1,49 @@
mod tests {
use compiler::backend::*;
#[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::*;