Updated hw6 to a newer version
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
parent
9224001a22
commit
0c04936ccf
356 changed files with 8408 additions and 4725 deletions
32
hw6/llprograms/sp20_hw3/ackermann.ll
Normal file
32
hw6/llprograms/sp20_hw3/ackermann.ll
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
define i64 @ack(i64 %m, i64 %n) {
|
||||
%r = alloca i64
|
||||
%icm = icmp sgt i64 %m, 0
|
||||
br i1 %icm, label %mnonzero, label %mzero
|
||||
mzero:
|
||||
%tmp = add i64 %n, 1
|
||||
store i64 %tmp, i64* %r
|
||||
br label %end
|
||||
mnonzero:
|
||||
%icn = icmp sgt i64 %n, 0
|
||||
br i1 %icn, label %nnonzero, label %nzero
|
||||
nzero:
|
||||
%msub = sub i64 %m, 1
|
||||
%tmp = call i64 @ack(i64 %msub, i64 1)
|
||||
store i64 %tmp, i64* %r
|
||||
br label %end
|
||||
nnonzero:
|
||||
%msub = sub i64 %m, 1
|
||||
%nsub = sub i64 %n, 1
|
||||
%newn = call i64 @ack(i64 %m, i64 %nsub)
|
||||
%tmp = call i64 @ack(i64 %msub, i64 %newn)
|
||||
store i64 %tmp, i64* %r
|
||||
br label %end
|
||||
end:
|
||||
%rv1 = load i64, i64* %r
|
||||
ret i64 %rv1
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%ans = call i64 @ack(i64 2, i64 2)
|
||||
ret i64 %ans
|
||||
}
|
||||
21
hw6/llprograms/sp20_hw3/another_io_test.ll
Normal file
21
hw6/llprograms/sp20_hw3/another_io_test.ll
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
@format = global [49 x i8] c"Hello, world!%s %s %lld %lld %lld %lld %lld %lld\00"
|
||||
@text = global [5 x i8] c"2020\00"
|
||||
@integer = global i64 4
|
||||
|
||||
define void @changetext() {
|
||||
%1 = load i64, i64* @integer
|
||||
%2 = sub i64 %1, 1
|
||||
store i64 %2, i64* @integer
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%1 = getelementptr [49 x i8], [49 x i8]* @format, i32 0, i32 0
|
||||
%2 = getelementptr i8*, i8** %argv, i32 1
|
||||
%3 = load i8*, i8** %2
|
||||
%4 = getelementptr [5 x i8], [5 x i8]* @text, i32 0, i32 0
|
||||
call void @changetext()
|
||||
%5 = load i64, i64* @integer
|
||||
call void @printf(i8* %1, i8* %3, i8* %4, i64 %5, i64 4, i64 5, i64 6, i64 7, i64 8)
|
||||
ret i64 0
|
||||
}
|
||||
98
hw6/llprograms/sp20_hw3/countsort.ll
Normal file
98
hw6/llprograms/sp20_hw3/countsort.ll
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
@valuesSeen = global [15 x i64] [i64 0, i64 0, i64 0, i64 0, i64 0,
|
||||
i64 0, i64 0, i64 0, i64 0, i64 0,
|
||||
i64 0, i64 0, i64 0, i64 0, i64 0 ]
|
||||
|
||||
@inputArray = global [10 x i64] [i64 5, i64 7, i64 14, i64 0, i64 0,
|
||||
i64 9, i64 12, i64 5, i64 5, i64 6]
|
||||
|
||||
@outputArray = global [10 x i64] [i64 0, i64 0, i64 0, i64 0, i64 0,
|
||||
i64 0, i64 0, i64 0, i64 0, i64 0]
|
||||
|
||||
define i64 @insert(i64 %val, i64 %numCopies, i64 %startIndex) {
|
||||
%endIndex = add i64 %startIndex, %numCopies
|
||||
%currentInsertionIndexPtr = alloca i64
|
||||
store i64 %startIndex, i64* %currentInsertionIndexPtr
|
||||
%c3 = icmp eq i64 %startIndex, %endIndex
|
||||
|
||||
br i1 %c3, label %finishInsert, label %innerInsertLoop
|
||||
|
||||
innerInsertLoop:
|
||||
%currentInsertionIndex = load i64, i64* %currentInsertionIndexPtr
|
||||
|
||||
%insertionPtr = getelementptr [10 x i64], [10 x i64]* @outputArray, i32 0, i64 %currentInsertionIndex
|
||||
store i64 %val, i64* %insertionPtr
|
||||
|
||||
%newCurrentInsertionIndex = add i64 %currentInsertionIndex, 1
|
||||
store i64 %newCurrentInsertionIndex, i64* %currentInsertionIndexPtr
|
||||
%c0 = icmp eq i64 %newCurrentInsertionIndex, %endIndex
|
||||
br i1 %c0, label %finishInsert, label %innerInsertLoop
|
||||
|
||||
finishInsert:
|
||||
ret i64 %endIndex
|
||||
}
|
||||
|
||||
define void @countSortInPlace() {
|
||||
%currentIndexPtr = alloca i64
|
||||
store i64 0, i64* %currentIndexPtr
|
||||
br label %countLoop
|
||||
|
||||
; create all the counts
|
||||
countLoop:
|
||||
; load in the element at currentIndex
|
||||
%currentIndex = load i64, i64* %currentIndexPtr
|
||||
%currentElPtr = getelementptr [10 x i64], [10 x i64]* @inputArray, i32 0, i64 %currentIndex
|
||||
%currentEl = load i64, i64* %currentElPtr
|
||||
|
||||
; load in the count at currentIndex
|
||||
%countPtr = getelementptr [15 x i64], [15 x i64]* @valuesSeen, i32 0, i64 %currentEl
|
||||
%count = load i64, i64* %countPtr
|
||||
|
||||
; update the count and store it
|
||||
%newCount = add i64 %count, 1
|
||||
store i64 %newCount, i64* %countPtr
|
||||
|
||||
; Update the current index and store it
|
||||
%newCurrentIndex = add i64 %currentIndex, 1
|
||||
store i64 %newCurrentIndex, i64* %currentIndexPtr
|
||||
|
||||
; If we've reached the end of the list, end the loop
|
||||
%c1 = icmp eq i64 %newCurrentIndex, 10
|
||||
br i1 %c1, label %performInsertions, label %countLoop
|
||||
|
||||
; insert into the final array
|
||||
performInsertions:
|
||||
; the index in the list of counts
|
||||
store i64 0, i64* %currentIndexPtr
|
||||
%insertionPointPtr = alloca i64
|
||||
; the index in the output array
|
||||
store i64 0, i64* %insertionPointPtr
|
||||
br label %insertionLoop
|
||||
|
||||
insertionLoop:
|
||||
%currentVal = load i64, i64* %currentIndexPtr
|
||||
|
||||
%currentCountPtr = getelementptr [15 x i64], [15 x i64]* @valuesSeen, i32 0, i64 %currentVal
|
||||
%currentCount = load i64, i64* %currentCountPtr
|
||||
|
||||
%insertionPoint = load i64, i64* %insertionPointPtr
|
||||
%newInsertionPoint = call i64 @insert(i64 %currentVal, i64 %currentCount, i64 %insertionPoint)
|
||||
|
||||
store i64 %newInsertionPoint, i64* %insertionPointPtr
|
||||
|
||||
%newCurrentVal = add i64 %currentVal, 1
|
||||
store i64 %newCurrentVal, i64* %currentIndexPtr
|
||||
|
||||
%c2 = icmp eq i64 %newCurrentVal, 15
|
||||
br i1 %c2, label %complete, label %insertionLoop
|
||||
|
||||
complete:
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
call void @countSortInPlace()
|
||||
%testPtr = getelementptr [10 x i64], [10 x i64]* @outputArray, i32 0, i64 5
|
||||
%res = load i64, i64* %testPtr
|
||||
; should return 6
|
||||
ret i64 %res
|
||||
}
|
||||
58
hw6/llprograms/sp20_hw3/det3x3.ll
Normal file
58
hw6/llprograms/sp20_hw3/det3x3.ll
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
@row1 = global [3 x i64] [i64 4, i64 3, i64 6]
|
||||
@row2 = global [3 x i64] [i64 5, i64 1, i64 2]
|
||||
@row3 = global [3 x i64] [i64 6, i64 1, i64 1]
|
||||
|
||||
|
||||
define i64 @calc_det() {
|
||||
%1 = getelementptr [3 x i64], [3 x i64]* @row1, i64 0, i64 0
|
||||
%2 = getelementptr [3 x i64], [3 x i64]* @row1, i64 0, i64 1
|
||||
%3 = getelementptr [3 x i64], [3 x i64]* @row1, i64 0, i64 2
|
||||
%4 = getelementptr [3 x i64], [3 x i64]* @row2, i64 0, i64 0
|
||||
%5 = getelementptr [3 x i64], [3 x i64]* @row2, i64 0, i64 1
|
||||
%6 = getelementptr [3 x i64], [3 x i64]* @row2, i64 0, i64 2
|
||||
%7 = getelementptr [3 x i64], [3 x i64]* @row3, i64 0, i64 0
|
||||
%8 = getelementptr [3 x i64], [3 x i64]* @row3, i64 0, i64 1
|
||||
%9 = getelementptr [3 x i64], [3 x i64]* @row3, i64 0, i64 2
|
||||
|
||||
%10 = load i64, i64* %1
|
||||
%11 = load i64, i64* %2
|
||||
%12 = load i64, i64* %3
|
||||
%13 = load i64, i64* %4
|
||||
%14 = load i64, i64* %5
|
||||
%15 = load i64, i64* %6
|
||||
%16 = load i64, i64* %7
|
||||
%17 = load i64, i64* %8
|
||||
%18 = load i64, i64* %9
|
||||
|
||||
%19 = mul i64 %14, %18
|
||||
%20 = mul i64 %17, %15
|
||||
%21 = mul i64 %13, %18
|
||||
%22 = mul i64 %16, %15
|
||||
%23 = mul i64 %13, %17
|
||||
%24 = mul i64 %14, %16
|
||||
|
||||
%25 = sub i64 %19, %20
|
||||
%26 = sub i64 %22, %21
|
||||
%27 = sub i64 %23, %24
|
||||
|
||||
%28 = mul i64 %10, %25
|
||||
%29 = mul i64 %11, %26
|
||||
%30 = mul i64 %12, %27
|
||||
|
||||
%31 = add i64 %28, %29
|
||||
%32 = add i64 %30, %31
|
||||
|
||||
%33 = sub i64 %10, %12
|
||||
ret i64 %32
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%1 = call i64 @calc_det()
|
||||
ret i64 %1
|
||||
}
|
||||
|
||||
133
hw6/llprograms/sp20_hw3/editdistance.ll
Normal file
133
hw6/llprograms/sp20_hw3/editdistance.ll
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
%mat = type [7 x [12 x i64]]
|
||||
|
||||
@arr1 = global [6 x i64] [i64 107, i64 105, i64 116, i64 116, i64 101, i64 110]
|
||||
@arr2 = global [11 x i64] [i64 98, i64 97, i64 98, i64 121, i64 115, i64 105, i64 116, i64 116, i64 105, i64 110, i64 103]
|
||||
@arr1len = global i64 6
|
||||
@arr2len = global i64 11
|
||||
|
||||
|
||||
@dp = global %mat [
|
||||
[12 x i64] [i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0],
|
||||
[12 x i64] [i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0],
|
||||
[12 x i64] [i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0],
|
||||
[12 x i64] [i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0],
|
||||
[12 x i64] [i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0],
|
||||
[12 x i64] [i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0],
|
||||
[12 x i64] [i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0,i64 0]]
|
||||
|
||||
define void @writedp(i64 %i, i64 %j, i64 %val) {
|
||||
%ptr = getelementptr %mat, %mat* @dp, i64 0, i64 %i, i64 %j
|
||||
store i64 %val, i64* %ptr
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @getdp(i64 %i, i64 %j) {
|
||||
%ptr = getelementptr %mat, %mat* @dp, i64 0, i64 %i, i64 %j
|
||||
%1 = load i64, i64* %ptr
|
||||
ret i64 %1
|
||||
}
|
||||
|
||||
define i64 @getval(i64* %ptr) {
|
||||
%1 = load i64, i64* %ptr
|
||||
ret i64 %1
|
||||
}
|
||||
|
||||
define i64 @min2(i64 %a, i64 %b) {
|
||||
%cmp = icmp sgt i64 %a, %b
|
||||
br i1 %cmp, label %agt, label %bgt
|
||||
agt:
|
||||
ret i64 %b
|
||||
bgt:
|
||||
ret i64 %a
|
||||
}
|
||||
|
||||
define i64 @min3(i64 %a, i64 %b, i64 %c) {
|
||||
%1 = call i64 @min2(i64 %a, i64 %b)
|
||||
%2 = call i64 @min2(i64 %1, i64 %c)
|
||||
ret i64 %2
|
||||
}
|
||||
|
||||
define void @compute_cost(i64 %row, i64 %col) {
|
||||
%1 = sub i64 %row, 1
|
||||
%2 = getelementptr [6 x i64], [6 x i64]* @arr1, i64 0, i64 %1
|
||||
%3 = load i64, i64* %2
|
||||
%4 = sub i64 %col, 1
|
||||
%5 = getelementptr [11 x i64], [11 x i64]* @arr2, i64 0, i64 %4
|
||||
%6 = load i64, i64* %5
|
||||
%7 = alloca i64
|
||||
%8 = icmp eq i64 %3, %6
|
||||
br i1 %8, label %ifeq, label %ifnoteq
|
||||
ifeq:
|
||||
store i64 0, i64* %7
|
||||
br label %cont
|
||||
ifnoteq:
|
||||
store i64 1, i64* %7
|
||||
br label %cont
|
||||
cont:
|
||||
%9 = call i64 @getdp(i64 %1, i64 %col)
|
||||
%10 = call i64 @getdp(i64 %row, i64 %4)
|
||||
%11 = call i64 @getdp(i64 %1, i64 %4)
|
||||
%12 = load i64, i64* %7
|
||||
%13 = add i64 %9, 1
|
||||
%14 = add i64 %10, 1
|
||||
%15 = add i64 %11, %12
|
||||
%16 = call i64 @min3(i64 %13, i64 %14, i64 %15)
|
||||
call void @writedp(i64 %row, i64 %col, i64 %16)
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @levenshtein() {
|
||||
%1 = alloca i64
|
||||
store i64 0, i64* %1
|
||||
%2 = alloca i64
|
||||
store i64 0, i64* %2
|
||||
br label %init1
|
||||
init1:
|
||||
%3 = load i64, i64* %1
|
||||
call void @writedp(i64 %3, i64 0, i64 %3)
|
||||
%4 = add i64 %3, 1
|
||||
store i64 %4, i64* %1
|
||||
%5 = call i64 @getval(i64* @arr1len)
|
||||
%6 = icmp sge i64 %4, %5
|
||||
br i1 %6, label %init2, label %init1
|
||||
init2:
|
||||
%7 = load i64, i64* %2
|
||||
call void @writedp(i64 0, i64 %7, i64 %7)
|
||||
%8 = add i64 %7, 1
|
||||
store i64 %8, i64* %2
|
||||
%9 = call i64 @getval(i64* @arr2len)
|
||||
%10 = icmp sge i64 %8, %9
|
||||
br i1 %10, label %proc, label %init2
|
||||
proc:
|
||||
store i64 1, i64* %1
|
||||
store i64 1, i64* %2
|
||||
br label %iterrow
|
||||
iterrow:
|
||||
%row = load i64, i64* %1
|
||||
%col = load i64, i64* %2
|
||||
call void @compute_cost(i64 %row, i64 %col)
|
||||
%11 = add i64 %col, 1
|
||||
store i64 %11, i64* %2
|
||||
%12 = call i64 @getval(i64* @arr2len)
|
||||
%13 = icmp sge i64 %11, %12
|
||||
br i1 %13, label %newrow, label %iterrow
|
||||
newrow:
|
||||
%14 = add i64 %row, 1
|
||||
store i64 %14, i64* %1
|
||||
store i64 1, i64* %2
|
||||
%15 = call i64 @getval(i64* @arr1len)
|
||||
%16 = icmp sge i64 %14, %15
|
||||
br i1 %16, label %term, label %iterrow
|
||||
term:
|
||||
%17 = call i64 @getval(i64* @arr1len)
|
||||
%18 = sub i64 %17, 1
|
||||
%19 = call i64 @getval(i64* @arr2len)
|
||||
%20 = sub i64 %19, 1
|
||||
%21 = call i64 @getdp(i64 %18, i64 %20)
|
||||
ret i64 %21
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%1 = call i64 @levenshtein()
|
||||
ret i64 %1
|
||||
}
|
||||
20
hw6/llprograms/sp20_hw3/fibonacci.ll
Normal file
20
hw6/llprograms/sp20_hw3/fibonacci.ll
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
define i64 @fibonacci(i64 %x) {
|
||||
%1 = icmp sle i64 %x, 1
|
||||
br i1 %1, label %base_case, label %recurse
|
||||
base_case:
|
||||
ret i64 %x
|
||||
recurse:
|
||||
%2 = sub i64 %x, 1
|
||||
%3 = sub i64 %x, 2
|
||||
%4 = call i64 @fibonacci(i64 %2)
|
||||
%5 = call i64 @fibonacci(i64 %3)
|
||||
%6 = add i64 %4, %5
|
||||
ret i64 %6
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%result = call i64 @fibonacci(i64 6)
|
||||
ret i64 %result
|
||||
}
|
||||
|
||||
|
||||
41
hw6/llprograms/sp20_hw3/find_max.ll
Normal file
41
hw6/llprograms/sp20_hw3/find_max.ll
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
%arr = type [8 x i64]
|
||||
|
||||
@input = global %arr [i64 50, i64 60, i64 40, i64 70, i64 30, i64 80, i64 20, i64 80]
|
||||
@length = global i64 8
|
||||
|
||||
define i64 @find_max (){
|
||||
%max = alloca i64
|
||||
%ctr_addr = alloca i64
|
||||
store i64 0, i64* %ctr_addr ; initialize counter to 0
|
||||
%1 = getelementptr %arr, %arr* @input, i32 0, i64 0
|
||||
%l = load i64, i64* %1
|
||||
store i64 %l, i64* %max ; %max <- first element in array
|
||||
br label %loop
|
||||
|
||||
loop:
|
||||
%ctr = load i64, i64* %ctr_addr ; retrieve counter value
|
||||
%addr_of_current = getelementptr [8 x i64], [8 x i64]* @input, i32 0, i64 %ctr
|
||||
%current = load i64, i64* %addr_of_current ; retrieve current value
|
||||
%t = load i64, i64* %max
|
||||
%flag = icmp slt i64 %t, %current
|
||||
br i1 %flag, label %update_max, label %move_on
|
||||
|
||||
update_max:
|
||||
store i64 %current, i64* %max
|
||||
br label %move_on
|
||||
|
||||
move_on:
|
||||
%g = load i64, i64* %ctr_addr
|
||||
%2 = add i64 1, %g
|
||||
store i64 %2, i64* %ctr_addr
|
||||
%flag2 = icmp eq i64 %2, 8
|
||||
br i1 %flag2, label %end, label %loop
|
||||
|
||||
end:
|
||||
ret i64 %t
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%1 = call i64 @find_max()
|
||||
ret i64 %1
|
||||
}
|
||||
104
hw6/llprograms/sp20_hw3/fp32lite.ll
Normal file
104
hw6/llprograms/sp20_hw3/fp32lite.ll
Normal file
|
|
@ -0,0 +1,104 @@
|
|||
;*******************************************************************************
|
||||
; Convert fp32 in IEEE 32-bit floating-point format to truncated signed integer.
|
||||
;
|
||||
define i64 @fp32_to_int(i64 %fp32) {
|
||||
%neg = alloca i1 ; sign bit of fp32
|
||||
%bits = alloca i64 ; bits of fp32 without the sign bit
|
||||
%exp = alloca i64 ; expoent
|
||||
%int = alloca i64 ; integer part of fp32
|
||||
%nbits = alloca i64 ; number of bits after the dot if 2^exp is applied
|
||||
%fract = alloca i64 ; mantissa of fp32
|
||||
|
||||
; neg = fp32 & 0x80000000
|
||||
%1 = and i64 %fp32, 2147483648 ; 0x80000000
|
||||
%2 = icmp ne i64 %1, 0
|
||||
store i1 %2, i1* %neg
|
||||
|
||||
; bits = fp32 & 0x7FFFFFFF
|
||||
%3 = and i64 %fp32, 2147483647 ; 0x7FFFFFFF
|
||||
store i64 %3, i64* %bits
|
||||
|
||||
; exp = bits >> 23
|
||||
%4 = load i64, i64* %bits
|
||||
%5 = lshr i64 %4, 23
|
||||
store i64 %5, i64* %exp
|
||||
|
||||
; exp -= 127
|
||||
%6 = load i64, i64* %exp
|
||||
%7 = sub i64 %6, 127
|
||||
store i64 %7, i64* %exp
|
||||
|
||||
; int = 0
|
||||
store i64 0, i64* %int
|
||||
|
||||
; bits > 0
|
||||
%8 = load i64, i64* %bits
|
||||
%9 = icmp sgt i64 %8, 0
|
||||
br i1 %9, label %check_exponent, label %check_sign
|
||||
|
||||
check_exponent: ; preds = %1
|
||||
; exp >= 0
|
||||
%10 = load i64, i64* %exp
|
||||
%11 = icmp sge i64 %10, 0
|
||||
br i1 %11, label %compute, label %check_sign
|
||||
|
||||
compute: ; preds = %check_exponent
|
||||
; nbits = 23 - exp
|
||||
%12 = load i64, i64* %exp
|
||||
%13 = sub i64 23, %12
|
||||
store i64 %13, i64* %nbits
|
||||
|
||||
; fract = bits & 0x7FFFFF
|
||||
%14 = load i64, i64* %bits
|
||||
%15 = and i64 %14, 8388607 ; 0x7FFFFF
|
||||
store i64 %15, i64* %fract
|
||||
|
||||
; fract |= 0x800000
|
||||
%16 = load i64, i64* %fract
|
||||
%17 = or i64 %16, 8388608 ; 0x800000
|
||||
store i64 %17, i64* %fract
|
||||
|
||||
; nbits > 0
|
||||
%18 = load i64, i64* %nbits
|
||||
%19 = icmp sgt i64 %18, 0
|
||||
br i1 %19, label %shift_right, label %shift_left
|
||||
|
||||
shift_right: ; preds = %compute
|
||||
; int = fract >> nbits
|
||||
%20 = load i64, i64* %fract
|
||||
%21 = load i64, i64* %nbits
|
||||
%22 = lshr i64 %20, %21
|
||||
store i64 %22, i64* %int
|
||||
br label %check_sign
|
||||
|
||||
shift_left: ; preds = %compute
|
||||
; int = fraction << -nbits
|
||||
%23 = load i64, i64* %fract
|
||||
%24 = load i64, i64* %nbits
|
||||
%25 = sub i64 0, %24
|
||||
%26 = shl i64 %23, %25
|
||||
store i64 %26, i64* %int
|
||||
br label %check_sign
|
||||
|
||||
check_sign: ; preds = %shift_left, %shift_right, %check_exponent, %1
|
||||
; neg > 0
|
||||
%27 = load i1, i1* %neg
|
||||
br i1 %27, label %negate, label %return
|
||||
|
||||
negate: ; preds = %check_sign
|
||||
; int = -int
|
||||
%28 = load i64, i64* %int
|
||||
%29 = sub i64 0, %28
|
||||
store i64 %29, i64* %int
|
||||
br label %return
|
||||
|
||||
return: ; preds = %negate, %check_sign
|
||||
; return int
|
||||
%30 = load i64, i64* %int
|
||||
ret i64 %30
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%int = call i64 @fp32_to_int(i64 1132389990) ; 0x437EE666: 254.9
|
||||
ret i64 %int
|
||||
}
|
||||
70
hw6/llprograms/sp20_hw3/gnome_sort.ll
Normal file
70
hw6/llprograms/sp20_hw3/gnome_sort.ll
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
@glist = global [10 x i64] [i64 20, i64 17, i64 13, i64 14, i64 6, i64 5, i64 4, i64 3, i64 2, i64 1]
|
||||
|
||||
define void @sort ([10 x i64]* %list) {
|
||||
%i = alloca i64
|
||||
store i64 1, i64* %i
|
||||
br label %loop
|
||||
loop:
|
||||
%count = load i64, i64* %i
|
||||
%cmp1 = icmp slt i64 %count, 9
|
||||
br i1 %cmp1, label %body, label %done
|
||||
body:
|
||||
%p1 = getelementptr [10 x i64], [10 x i64]* %list, i32 0, i64 %count
|
||||
%v1 = load i64, i64* %p1
|
||||
%cb = add i64 %count, 1
|
||||
%p2 = getelementptr [10 x i64], [10 x i64]* %list, i32 0, i64 %cb
|
||||
%v2 = load i64, i64* %p2
|
||||
%cmp2 = icmp slt i64 %v1, %v2
|
||||
br i1 %cmp2, label %bumpc, label %swap
|
||||
bumpc:
|
||||
%a = add i64 1, %count
|
||||
store i64 %a, i64* %i
|
||||
br label %loop
|
||||
swap:
|
||||
store i64 %v1, i64* %p2
|
||||
store i64 %v2, i64* %p1
|
||||
%cmp2 = icmp sgt i64 %count, 0
|
||||
br i1 %cmp2, label %decc, label %loop
|
||||
decc:
|
||||
store i64 %v1, i64* %p2
|
||||
store i64 %v2, i64* %p1
|
||||
%a = sub i64 %count, 1
|
||||
store i64 %a, i64* %i
|
||||
br label %loop
|
||||
done:
|
||||
%max_ptr = getelementptr [10 x i64], [10 x i64]* %list, i32 0, i64 9
|
||||
%max = load i64, i64* %max_ptr
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%index1 = getelementptr [10 x i64], [10 x i64]* @glist, i32 0, i64 0
|
||||
%index2 = getelementptr [10 x i64], [10 x i64]* @glist, i32 0, i64 1
|
||||
%index3 = getelementptr [10 x i64], [10 x i64]* @glist, i32 0, i64 2
|
||||
%index4 = getelementptr [10 x i64], [10 x i64]* @glist, i32 0, i64 3
|
||||
%index5 = getelementptr [10 x i64], [10 x i64]* @glist, i32 0, i64 4
|
||||
%index6 = getelementptr [10 x i64], [10 x i64]* @glist, i32 0, i64 5
|
||||
%index7 = getelementptr [10 x i64], [10 x i64]* @glist, i32 0, i64 6
|
||||
%index8 = getelementptr [10 x i64], [10 x i64]* @glist, i32 0, i64 7
|
||||
%index9 = getelementptr [10 x i64], [10 x i64]* @glist, i32 0, i64 8
|
||||
%index10 = getelementptr [10 x i64], [10 x i64]* @glist, i32 0, i64 9
|
||||
|
||||
%r = call void @sort([10 x i64]* @glist)
|
||||
|
||||
%i1 = load i64, i64* %index1
|
||||
%i2 = load i64, i64* %index2
|
||||
%i3 = load i64, i64* %index3
|
||||
%i4 = load i64, i64* %index4
|
||||
%i5 = load i64, i64* %index5
|
||||
%i6 = load i64, i64* %index6
|
||||
%i7 = load i64, i64* %index7
|
||||
%i8 = load i64, i64* %index8
|
||||
%i9 = load i64, i64* %index9
|
||||
%i10 = load i64, i64* %index10
|
||||
|
||||
%first_two = add i64 %i1, %i2
|
||||
%last_one = add i64 %first_two, %i10
|
||||
%minus_second_last = sub i64 %last_one, %i9
|
||||
|
||||
ret i64 %minus_second_last
|
||||
}
|
||||
178
hw6/llprograms/sp20_hw3/heapsort.ll
Normal file
178
hw6/llprograms/sp20_hw3/heapsort.ll
Normal file
|
|
@ -0,0 +1,178 @@
|
|||
declare void @printf(i8*, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64)
|
||||
|
||||
@str = global [162 x i8] c"Sum of first element of each list before sorting: %lld; sum after: %lld; elements of arr2 after sort: %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld\00"
|
||||
|
||||
@arr1 = global [12 x i64] [i64 10, i64 9, i64 8, i64 7, i64 6, i64 5, i64 4, i64 3, i64 2, i64 1, i64 0, i64 -1]
|
||||
@arr1_length = global i64 12
|
||||
|
||||
@arr2 = global [12 x i64] [i64 5, i64 5, i64 10, i64 1, i64 6, i64 7, i64 -5, i64 49, i64 4512, i64 4851, i64 741, i64 84]
|
||||
|
||||
define void @swap([12 x i64]* %arr, i64 %i, i64 %j) {
|
||||
%1 = getelementptr [12 x i64], [12 x i64]* %arr, i64 0, i64 %i
|
||||
%2 = getelementptr [12 x i64], [12 x i64]* %arr, i64 0, i64 %j
|
||||
|
||||
%i_val = load i64, i64* %1
|
||||
%j_val = load i64, i64* %2
|
||||
|
||||
store i64 %j_val, i64* %1
|
||||
store i64 %i_val, i64* %2
|
||||
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @heapify([12 x i64]* %arr, i64 %arr_length, i64 %i) {
|
||||
%largest = alloca i64
|
||||
store i64 %i, i64* %largest
|
||||
|
||||
%1 = mul i64 %i, 2
|
||||
%left = add i64 %1, 1
|
||||
%right = add i64 %1, 2
|
||||
br label %if1_cond1
|
||||
|
||||
if1_cond1:
|
||||
%2 = icmp slt i64 %left, %arr_length
|
||||
br i1 %2, label %if1_cond2, label %if2_cond1
|
||||
|
||||
if1_cond2:
|
||||
%3 = getelementptr [12 x i64], [12 x i64]* %arr, i64 0, i64 %left
|
||||
%4 = load i64, i64* %largest
|
||||
%5 = getelementptr [12 x i64], [12 x i64]* %arr, i64 0, i64 %4
|
||||
%arr_left = load i64, i64* %3
|
||||
%arr_largest = load i64, i64* %5
|
||||
%6 = icmp sgt i64 %arr_left, %arr_largest
|
||||
br i1 %6, label %if1, label %if2_cond1
|
||||
|
||||
if1:
|
||||
store i64 %left, i64* %largest
|
||||
br label %if2_cond1
|
||||
|
||||
if2_cond1:
|
||||
%7 = icmp slt i64 %right, %arr_length
|
||||
br i1 %7, label %if2_cond2, label %if3_cond
|
||||
|
||||
if2_cond2:
|
||||
%8 = getelementptr [12 x i64], [12 x i64]* %arr, i64 0, i64 %right
|
||||
%9 = load i64, i64* %largest
|
||||
%10 = getelementptr [12 x i64], [12 x i64]* %arr, i64 0, i64 %9
|
||||
%arr_right = load i64, i64* %8
|
||||
%arr_largest2 = load i64, i64* %10
|
||||
%11 = icmp sgt i64 %arr_right, %arr_largest2
|
||||
br i1 %11, label %if2, label %if3_cond
|
||||
|
||||
if2:
|
||||
store i64 %right, i64* %largest
|
||||
br label %if3_cond
|
||||
|
||||
if3_cond:
|
||||
%12 = load i64, i64* %largest
|
||||
%13 = icmp ne i64 %12, %i
|
||||
br i1 %13, label %if3, label %end
|
||||
|
||||
if3:
|
||||
%14 = load i64, i64* %largest
|
||||
call void @swap([12 x i64]* %arr, i64 %i, i64 %14)
|
||||
call void @heapify([12 x i64]* %arr, i64 %arr_length, i64 %14)
|
||||
br label %end
|
||||
|
||||
end:
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @heapsort([12 x i64]* %arr, i64* %arr_length_p) {
|
||||
%arr_length = load i64, i64* %arr_length_p
|
||||
|
||||
%1 = lshr i64 %arr_length, 1
|
||||
%2 = sub i64 %1, 1
|
||||
|
||||
%i1 = alloca i64
|
||||
store i64 %2, i64* %i1
|
||||
br label %for1_cond
|
||||
|
||||
for1_cond:
|
||||
%3 = load i64, i64* %i1
|
||||
%4 = icmp sge i64 %3, 0
|
||||
br i1 %4, label %for1, label %for2_setup
|
||||
|
||||
for1:
|
||||
%5 = load i64, i64* %i1
|
||||
call void @heapify([12 x i64]* %arr, i64 %arr_length, i64 %5)
|
||||
%6 = sub i64 %5, 1
|
||||
store i64 %6, i64* %i1
|
||||
br label %for1_cond
|
||||
|
||||
for2_setup:
|
||||
%7 = sub i64 %arr_length, 1
|
||||
%i2 = alloca i64
|
||||
store i64 %7, i64* %i2
|
||||
br label %for2_cond
|
||||
|
||||
for2_cond:
|
||||
%8 = load i64, i64* %i2
|
||||
%9 = icmp sge i64 %8, 0
|
||||
br i1 %9, label %for2, label %end
|
||||
|
||||
for2:
|
||||
%10 = load i64, i64* %i2
|
||||
call void @swap([12 x i64]* %arr, i64 0, i64 %10)
|
||||
call void @heapify([12 x i64]* %arr, i64 %10, i64 0)
|
||||
|
||||
%11 = load i64, i64* %i2
|
||||
%12 = sub i64 %11, 1
|
||||
store i64 %12, i64* %i2
|
||||
br label %for2_cond
|
||||
|
||||
end:
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%1 = getelementptr [12 x i64], [12 x i64]* @arr1, i64 0, i64 0
|
||||
%2 = load i64, i64* %1
|
||||
%3 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 0
|
||||
%4 = load i64, i64* %3
|
||||
%5 = add i64 %2, %4
|
||||
|
||||
call void @heapsort([12 x i64]* @arr1, i64* @arr1_length)
|
||||
%6 = getelementptr [12 x i64], [12 x i64]* @arr1, i64 0, i64 0
|
||||
%7 = load i64, i64* %6
|
||||
|
||||
%arr2_length = alloca i64
|
||||
store i64 12, i64* %arr2_length
|
||||
call void @heapsort([12 x i64]* @arr2, i64* %arr2_length)
|
||||
%8 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 0
|
||||
%9 = load i64, i64* %8
|
||||
|
||||
%10 = add i64 %7, %9
|
||||
|
||||
%11 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 0
|
||||
%12 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 1
|
||||
%13 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 2
|
||||
%14 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 3
|
||||
%15 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 4
|
||||
%16 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 5
|
||||
%17 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 6
|
||||
%18 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 7
|
||||
%19 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 8
|
||||
%20 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 9
|
||||
%21 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 10
|
||||
%22 = getelementptr [12 x i64], [12 x i64]* @arr2, i64 0, i64 11
|
||||
|
||||
%23 = load i64, i64* %11
|
||||
%24 = load i64, i64* %12
|
||||
%25 = load i64, i64* %13
|
||||
%26 = load i64, i64* %14
|
||||
%27 = load i64, i64* %15
|
||||
%28 = load i64, i64* %16
|
||||
%29 = load i64, i64* %17
|
||||
%30 = load i64, i64* %18
|
||||
%31 = load i64, i64* %19
|
||||
%32 = load i64, i64* %20
|
||||
%33 = load i64, i64* %21
|
||||
%34 = load i64, i64* %22
|
||||
|
||||
%35 = getelementptr [162 x i8], [162 x i8]* @str, i32 0, i32 0
|
||||
|
||||
call void @printf(i8* %35, i64 %5, i64 %10, i64 %23, i64 %24, i64 %25, i64 %26, i64 %27, i64 %28, i64 %29, i64 %30, i64 %31, i64 %32, i64 %33, i64 %34)
|
||||
|
||||
ret i64 0
|
||||
}
|
||||
62
hw6/llprograms/sp20_hw3/insertion.ll
Normal file
62
hw6/llprograms/sp20_hw3/insertion.ll
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
%arr = type [10 x i64]
|
||||
|
||||
@input = global %arr [i64 10, i64 5, i64 134, i64 9, i64 11, i64 7, i64 200, i64 65, i64 74, i64 2]
|
||||
@length = global i64 10
|
||||
|
||||
define void @insertionSort() {
|
||||
%i = alloca i64
|
||||
%j = alloca i64
|
||||
%len = load i64, i64* @length
|
||||
store i64 1, i64* %i
|
||||
br label %while_cond_1
|
||||
while_cond_1:
|
||||
; while i < length(A)
|
||||
%1 = load i64, i64* %i ; i
|
||||
%2 = icmp slt i64 %1, %len
|
||||
br i1 %2, label %while_body_1, label %while_end_1
|
||||
while_body_1:
|
||||
; j = i
|
||||
%3 = load i64, i64* %i
|
||||
store i64 %3, i64* %j
|
||||
br label %while_cond_2
|
||||
while_cond_2:
|
||||
; while j > 0 and A[j-1] > A[j]
|
||||
%4 = load i64, i64* %j ; j
|
||||
%5 = icmp sgt i64 %4, 0 ; j>0
|
||||
%6 = sub i64 %4, 1 ; j-1
|
||||
%a_j0_ptr = getelementptr [10 x i64], [10 x i64]* @input, i32 0, i64 %6
|
||||
%a_j0 = load i64, i64* %a_j0_ptr; A[j-1]
|
||||
%a_j1_ptr = getelementptr [10 x i64], [10 x i64]* @input, i32 0, i64 %4
|
||||
%a_j1 = load i64, i64* %a_j1_ptr; A[j]
|
||||
%7 = icmp sgt i64 %a_j0, %a_j1
|
||||
; (j > 0) and (A[j-1] > A[j])
|
||||
%8 = and i1 %5, %7
|
||||
br i1 %8, label %while_body_2, label %while_end_2
|
||||
while_body_2:
|
||||
%9 = load i64, i64* %j ; j
|
||||
%10 = sub i64 %9, 1 ; j-1
|
||||
%tmp_ptr0 = getelementptr [10 x i64], [10 x i64]* @input, i32 0, i64 %9
|
||||
%tmp0 = load i64, i64* %tmp_ptr0 ; A[j]
|
||||
%tmp_ptr1 = getelementptr [10 x i64], [10 x i64]* @input, i32 0, i64 %10
|
||||
%tmp1 = load i64, i64* %tmp_ptr1 ; A[j-1]
|
||||
store i64 %tmp0, i64* %tmp_ptr1
|
||||
store i64 %tmp1, i64* %tmp_ptr0
|
||||
%11 = sub i64 %9, 1
|
||||
store i64 %11, i64* %j
|
||||
br label %while_cond_2
|
||||
while_end_2:
|
||||
; i = i + 1
|
||||
%12 = load i64, i64* %i
|
||||
%13 = add i64 %12, 1
|
||||
store i64 %13, i64* %i
|
||||
br label %while_cond_1
|
||||
while_end_1:
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
call void @insertionSort()
|
||||
%1 = getelementptr [10 x i64], [10 x i64]* @input, i32 0, i32 5
|
||||
%2 = load i64, i64* %1
|
||||
ret i64 %2
|
||||
}
|
||||
50
hw6/llprograms/sp20_hw3/lcs.ll
Normal file
50
hw6/llprograms/sp20_hw3/lcs.ll
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
%arr1 = type [7 x i64]
|
||||
%arr2 = type [6 x i64]
|
||||
|
||||
@str1 = global %arr1 [i64 1, i64 2, i64 3, i64 2, i64 4, i64 1, i64 2]
|
||||
@str2 = global %arr2 [i64 2, i64 4, i64 3, i64 1, i64 2 , i64 1]
|
||||
|
||||
define i64 @lcs(i64 %m, i64 %n) {
|
||||
%1 = icmp eq i64 %m, 0
|
||||
%2 = icmp eq i64 %n, 0
|
||||
br i1 %1, label %ret0, label %interm
|
||||
|
||||
interm:
|
||||
br i1 %2, label %ret0, label %then
|
||||
|
||||
then:
|
||||
%m1 = sub i64 %m, 1
|
||||
%n1 = sub i64 %n, 1
|
||||
%3 = getelementptr %arr1, %arr1* @str1, i32 0, i32 %m1
|
||||
%4 = load i64, i64* %3
|
||||
%5 = getelementptr %arr2, %arr2* @str2, i32 0, i32 %n1
|
||||
%6 = load i64, i64* %5
|
||||
%7 = icmp eq i64 %4, %6
|
||||
br i1 %7, label %eq_recurse, label %not_eq_recurse
|
||||
|
||||
eq_recurse:
|
||||
%ans = call i64 @lcs(i64 %m1, i64 %n1)
|
||||
%final = add i64 %ans, 1
|
||||
ret i64 %final
|
||||
|
||||
not_eq_recurse:
|
||||
%ans1 = call i64 @lcs(i64 %m1, i64 %n)
|
||||
%ans2 = call i64 @lcs(i64 %m, i64 %n1)
|
||||
%10 = icmp sge i64 %ans1, %ans2 %10 = icmp sge i64 %ans1, %ans2
|
||||
br i1 %10, label %ret1, label %ret2
|
||||
|
||||
ret1:
|
||||
ret i64 %ans1
|
||||
|
||||
ret2:
|
||||
ret i64 %ans2
|
||||
|
||||
ret0:
|
||||
ret i64 0
|
||||
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%1 = call i64 @lcs(i64 7, i64 6)
|
||||
ret i64 %1
|
||||
}
|
||||
17
hw6/llprograms/sp20_hw3/log.ll
Normal file
17
hw6/llprograms/sp20_hw3/log.ll
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
define i64 @log(i64 %num) {
|
||||
%1 = icmp eq i64 %num, 1
|
||||
br i1 %1, label %then, label %else
|
||||
then:
|
||||
ret i64 0
|
||||
else:
|
||||
%2 = lshr i64 %num, 1
|
||||
%3 = call i64 @log(i64 %2)
|
||||
%4 = add i64 %3, 1
|
||||
ret i64 %4
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%3 = add i64 16, 0
|
||||
%1 = call i64 @log(i64 %3)
|
||||
ret i64 %1
|
||||
}
|
||||
20
hw6/llprograms/sp20_hw3/many_calls.ll
Normal file
20
hw6/llprograms/sp20_hw3/many_calls.ll
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
define void @foo(i64 %a, i64 %b, i64 %c, i64 %d, i64 %e, i64 %f, i64 %g) {
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%1 = alloca i64
|
||||
store i64 9999999, i64* %1
|
||||
br label %loop_condition
|
||||
loop_condition:
|
||||
%2 = load i64, i64* %1
|
||||
%3 = icmp sgt i64 %2, 0
|
||||
br i1 %3, label %loop_body, label %post_loop
|
||||
loop_body:
|
||||
call void @foo(i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0)
|
||||
%5 = sub i64 %2, 1
|
||||
store i64 %5, i64* %1
|
||||
br label %loop_condition
|
||||
post_loop:
|
||||
ret i64 0
|
||||
}
|
||||
33
hw6/llprograms/sp20_hw3/ncr.ll
Normal file
33
hw6/llprograms/sp20_hw3/ncr.ll
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%1 = call i64 @fac(i64 9)
|
||||
%2 = call i64 @fac(i64 3)
|
||||
%3 = sub i64 9, 3
|
||||
%4 = call i64 @fac(i64 %3)
|
||||
%5 = mul i64 %2, %4
|
||||
%6 = call i64 @div(i64 %1, i64 %5)
|
||||
ret i64 %6
|
||||
}
|
||||
|
||||
define i64 @div(i64 %num, i64 %div) {
|
||||
%1 = icmp slt i64 %num, %div
|
||||
br i1 %1, label %rett, label %recc
|
||||
rett:
|
||||
ret i64 0
|
||||
recc:
|
||||
%2 = sub i64 %num, %div
|
||||
%3 = call i64 @div(i64 %2, i64 %div)
|
||||
%4 = add i64 %3, 1
|
||||
ret i64 %4
|
||||
}
|
||||
|
||||
define i64 @fac(i64 %n) {
|
||||
%1 = icmp sle i64 %n, 0
|
||||
br i1 %1, label %rett, label %rec
|
||||
rett:
|
||||
ret i64 1
|
||||
rec:
|
||||
%2 = sub i64 %n, 1
|
||||
%3 = call i64 @fac(i64 %2)
|
||||
%4 = mul i64 %n, %3
|
||||
ret i64 %4
|
||||
}
|
||||
26
hw6/llprograms/sp20_hw3/our_test.ll
Normal file
26
hw6/llprograms/sp20_hw3/our_test.ll
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
%arr = type [2 x i64]
|
||||
%var1 = type { i64, %arr* }
|
||||
%var2 = type { i64, %arr*, %var1* }
|
||||
|
||||
@tmp = global %arr [ i64 0, i64 0 ]
|
||||
@v1 = global %var1 {i64 2, %arr* @tmp}
|
||||
@v2 = global %var2 {i64 1, %arr* null, %var1* @v1}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%a = getelementptr %arr, %arr* @tmp, i32 0, i32 0
|
||||
%b = getelementptr %arr, %arr* @tmp, i32 0, i32 1
|
||||
store i64 10, i64* %a
|
||||
store i64 11, i64* %b
|
||||
%1 = getelementptr %var2, %var2* @v2, i32 0, i32 2
|
||||
%2 = load %var1*, %var1** %1
|
||||
%3 = getelementptr %var1, %var1* %2, i32 0, i32 1
|
||||
%4 = load %arr*, %arr** %3
|
||||
%5 = getelementptr %arr, %arr* %4, i32 0, i32 0
|
||||
store i64 11, i64* %5
|
||||
%6 = getelementptr %arr, %arr* %4, i32 0, i32 1
|
||||
store i64 10, i64* %5
|
||||
%9 = load i64, i64* %a
|
||||
%10 = load i64, i64* %b
|
||||
%result = sub i64 %9, %10
|
||||
ret i64 %result
|
||||
}
|
||||
174
hw6/llprograms/sp20_hw3/quicksort.ll
Normal file
174
hw6/llprograms/sp20_hw3/quicksort.ll
Normal file
|
|
@ -0,0 +1,174 @@
|
|||
declare void @printf(i8*, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64, i64)
|
||||
|
||||
@output_str = global [170 x i8] c"Initial array: %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld; Sorted array: %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld %lld\00"
|
||||
|
||||
|
||||
%arr = type [14 x i64]
|
||||
@input = global %arr [i64 4, i64 1, i64 50, i64 50, i64 7, i64 0, i64 5, i64 10, i64 9, i64 11, i64 0, i64 100, i64 3, i64 8]
|
||||
|
||||
; linear time and in place partition, given the lo and hi boundaries (chooses last element as in CLRS)
|
||||
define i64 @partition(%arr* %nums, i64 %lo, i64 %hi) {
|
||||
|
||||
; get pivot element
|
||||
%pivot_ptr = getelementptr %arr, %arr* %nums, i32 0, i64 %hi
|
||||
%pivot = load i64, i64* %pivot_ptr
|
||||
|
||||
; j is index through array
|
||||
%j_ptr = alloca i64
|
||||
store i64 %lo, i64* %j_ptr
|
||||
|
||||
; i is border of smaller elemenets
|
||||
%i_ptr = alloca i64
|
||||
store i64 %lo, i64* %i_ptr
|
||||
|
||||
br label %loop_condition
|
||||
|
||||
loop_condition:
|
||||
|
||||
; while j <= hi
|
||||
%j = load i64, i64* %j_ptr
|
||||
%1 = icmp sle i64 %j, %hi
|
||||
br i1 %1, label %loop_body, label %loop_exit
|
||||
|
||||
loop_body:
|
||||
|
||||
;if A[j] < pivot
|
||||
%p = load i64, i64* %j_ptr
|
||||
%a_j_ptr = getelementptr %arr, %arr* %nums, i32 0, i64 %p
|
||||
%a_j = load i64, i64* %a_j_ptr
|
||||
%2 = icmp slt i64 %a_j, %pivot
|
||||
br i1 %2, label %if_body, label %if_end
|
||||
|
||||
if_body:
|
||||
|
||||
%i = load i64, i64* %i_ptr
|
||||
|
||||
;swap A[i] and A[j]
|
||||
call void @swap(%arr* %nums, i64 %i, i64 %p)
|
||||
|
||||
;i++
|
||||
%3 = add i64 %i, 1
|
||||
store i64 %3, i64* %i_ptr
|
||||
|
||||
br label %if_end
|
||||
|
||||
if_end:
|
||||
|
||||
;incremennt j and go back to loop
|
||||
%4 = add i64 %j, 1
|
||||
store i64 %4, i64* %j_ptr
|
||||
br label %loop_condition
|
||||
|
||||
loop_exit:
|
||||
|
||||
;swap A[i] and A[hi]
|
||||
%q = load i64, i64* %i_ptr
|
||||
call void @swap(%arr* %nums, i64 %q, i64 %hi)
|
||||
|
||||
ret i64 %q
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
;swap nums[fst] and nums[snd]
|
||||
define void @swap(%arr* %nums, i64 %fst, i64 %snd) {
|
||||
|
||||
%fst_element_ptr = getelementptr %arr, %arr* %nums, i32 0, i64 %fst
|
||||
%snd_element_ptr = getelementptr %arr, %arr* %nums, i32 0, i64 %snd
|
||||
|
||||
%fst_element = load i64, i64* %fst_element_ptr
|
||||
%snd_element = load i64, i64* %snd_element_ptr
|
||||
|
||||
store i64 %fst_element, i64* %snd_element_ptr
|
||||
store i64 %snd_element, i64* %fst_element_ptr
|
||||
|
||||
ret void
|
||||
|
||||
}
|
||||
|
||||
;quicksort
|
||||
define void @quicksort(%arr* %nums, i64 %lo, i64 %hi) {
|
||||
;if lo < hi
|
||||
%1 = icmp slt i64 %lo, %hi
|
||||
br i1 %1, label %sort_case, label %trivial
|
||||
|
||||
sort_case:
|
||||
|
||||
%p = call i64 @partition(%arr* %nums, i64 %lo, i64 %hi)
|
||||
%2 = sub i64 %p, 1
|
||||
%3 = add i64 %p, 1
|
||||
|
||||
call void @quicksort(%arr* %nums, i64 %lo, i64 %2)
|
||||
call void @quicksort(%arr* %nums, i64 %3, i64 %hi)
|
||||
|
||||
ret void
|
||||
|
||||
trivial:
|
||||
|
||||
ret void
|
||||
}
|
||||
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
|
||||
;pointers for array elements
|
||||
%in0 = getelementptr %arr, %arr* @input, i32 0, i32 0
|
||||
%in1 = getelementptr %arr, %arr* @input, i32 0, i32 1
|
||||
%in2 = getelementptr %arr, %arr* @input, i32 0, i32 2
|
||||
%in3 = getelementptr %arr, %arr* @input, i32 0, i32 3
|
||||
%in4 = getelementptr %arr, %arr* @input, i32 0, i32 4
|
||||
%in5 = getelementptr %arr, %arr* @input, i32 0, i32 5
|
||||
%in6 = getelementptr %arr, %arr* @input, i32 0, i32 6
|
||||
%in7 = getelementptr %arr, %arr* @input, i32 0, i32 7
|
||||
%in8 = getelementptr %arr, %arr* @input, i32 0, i32 8
|
||||
%in9 = getelementptr %arr, %arr* @input, i32 0, i32 9
|
||||
%in10 = getelementptr %arr, %arr* @input, i32 0, i32 10
|
||||
%in11 = getelementptr %arr, %arr* @input, i32 0, i32 11
|
||||
%in12 = getelementptr %arr, %arr* @input, i32 0, i32 12
|
||||
%in13 = getelementptr %arr, %arr* @input, i32 0, i32 13
|
||||
|
||||
;initial elements
|
||||
%a0 = load i64, i64* %in0
|
||||
%a1 = load i64, i64* %in1
|
||||
%a2 = load i64, i64* %in2
|
||||
%a3 = load i64, i64* %in3
|
||||
%a4 = load i64, i64* %in4
|
||||
%a5 = load i64, i64* %in5
|
||||
%a6 = load i64, i64* %in6
|
||||
%a7 = load i64, i64* %in7
|
||||
%a8 = load i64, i64* %in8
|
||||
%a9 = load i64, i64* %in9
|
||||
%a10 = load i64, i64* %in10
|
||||
%a11 = load i64, i64* %in11
|
||||
%a12 = load i64, i64* %in12
|
||||
%a13 = load i64, i64* %in13
|
||||
|
||||
;sort
|
||||
call void @quicksort(%arr* @input, i64 0, i64 13)
|
||||
|
||||
;get sorted array
|
||||
%b0 = load i64, i64* %in0
|
||||
%b1 = load i64, i64* %in1
|
||||
%b2 = load i64, i64* %in2
|
||||
%b3 = load i64, i64* %in3
|
||||
%b4 = load i64, i64* %in4
|
||||
%b5 = load i64, i64* %in5
|
||||
%b6 = load i64, i64* %in6
|
||||
%b7 = load i64, i64* %in7
|
||||
%b8 = load i64, i64* %in8
|
||||
%b9 = load i64, i64* %in9
|
||||
%b10 = load i64, i64* %in10
|
||||
%b11 = load i64, i64* %in11
|
||||
%b12 = load i64, i64* %in12
|
||||
%b13 = load i64, i64* %in13
|
||||
|
||||
%s = getelementptr [170 x i8], [170 x i8]* @output_str, i32 0, i32 0
|
||||
|
||||
call void @printf(i8* %s, i64 %a0, i64 %a1, i64 %a2, i64 %a3, i64 %a4, i64 %a5, i64 %a6, i64 %a7, i64 %a8, i64 %a9, i64 %a10, i64 %a11, i64 %a12, i64 %a13, i64 %b0, i64 %b1, i64 %b2, i64 %b3, i64 %b4, i64 %b5, i64 %b6, i64 %b7, i64 %b8, i64 %b9, i64 %b10, i64 %b11, i64 %b12, i64 %b13)
|
||||
|
||||
ret i64 1
|
||||
|
||||
|
||||
}
|
||||
70
hw6/llprograms/sp20_hw3/slowsort.ll
Normal file
70
hw6/llprograms/sp20_hw3/slowsort.ll
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
%list = type [31 x i64]
|
||||
@length = global i64 31
|
||||
|
||||
@input = global %list [i64 5, i64 100, i64 2, i64 0, i64 18, i64 10, i64 2, i64 1, i64 22, i64 98, i64 107, i64 105, i64 116, i64 116, i64 101, i64 110, i64 20, i64 23, i64 102, i64 23, i64 98, i64 97, i64 98, i64 121, i64 15, i64 5, i64 16, i64 116, i64 105, i64 110, i64 155]
|
||||
|
||||
define void @slowsort(i64 %i, i64 %j) {
|
||||
%1 = icmp sge i64 %i, %j
|
||||
br i1 %1, label %done, label %notdone
|
||||
notdone:
|
||||
%2 = add i64 %i, %j
|
||||
%mid = ashr i64 %2, 1
|
||||
%mid1 = add i64 %mid, 1
|
||||
call void @slowsort(i64 %i, i64 %mid)
|
||||
call void @slowsort(i64 %mid1, i64 %j)
|
||||
%ptrm = getelementptr %list, %list* @input, i64 0, i64 %mid
|
||||
%valm = load i64, i64* %ptrm
|
||||
%ptrj = getelementptr %list, %list* @input, i64 0, i64 %j
|
||||
%valj = load i64, i64* %ptrj
|
||||
%isrev = icmp slt i64 %valj, %valm
|
||||
br i1 %isrev, label %swap, label %finish
|
||||
swap:
|
||||
store i64 %valj, i64* %ptrm
|
||||
store i64 %valm, i64* %ptrj
|
||||
br label %finish
|
||||
finish:
|
||||
%jminus = sub i64 %j, 1
|
||||
call void @slowsort(i64 %i, i64 %jminus)
|
||||
br label %done
|
||||
done:
|
||||
ret void
|
||||
}
|
||||
|
||||
define i1 @issorted() {
|
||||
%len = load i64, i64* @length
|
||||
%len1 = sub i64 %len, 1
|
||||
%iptr = alloca i64
|
||||
store i64 0, i64* %iptr
|
||||
br label %loop
|
||||
loop:
|
||||
%i = load i64, i64* %iptr
|
||||
%ptri = getelementptr %list, %list* @input, i64 0, i64 %i
|
||||
%vali = load i64, i64* %ptri
|
||||
%j = add i64 %i, 1
|
||||
%ptrj = getelementptr %list, %list* @input, i64 0, i64 %j
|
||||
%valj = load i64, i64* %ptrj
|
||||
store i64 %j, i64* %iptr
|
||||
%inorder = icmp sle i64 %vali, %valj
|
||||
br i1 %inorder, label %checkloop, label %succ
|
||||
checkloop:
|
||||
%isdone = icmp eq i64 %i, %len1
|
||||
br i1 %isdone, label %succ, label %loop
|
||||
fail:
|
||||
ret i1 0
|
||||
succ:
|
||||
ret i1 1
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%len = load i64, i64* @length
|
||||
%len1 = sub i64 %len, 1
|
||||
call void @slowsort(i64 0, i64 %len1)
|
||||
%test = call i1 @issorted()
|
||||
br i1 %test, label %succ, label %fail
|
||||
fail:
|
||||
ret i64 0
|
||||
succ:
|
||||
%p = getelementptr %list, %list* @input, i64 0, i64 %len1
|
||||
%big = load i64, i64* %p
|
||||
ret i64 %big
|
||||
}
|
||||
29
hw6/llprograms/sp20_hw3/sum_half_list.ll
Normal file
29
hw6/llprograms/sp20_hw3/sum_half_list.ll
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
%struct.List = type { i64, %struct.List* }
|
||||
|
||||
@one_elt = global %struct.List { i64 1, %struct.List* null }
|
||||
@two_elts = global %struct.List { i64 2, %struct.List* @one_elt }
|
||||
@three_elts = global %struct.List { i64 4, %struct.List* @two_elts }
|
||||
@four_elts = global %struct.List { i64 8, %struct.List* @three_elts }
|
||||
@test = global %struct.List { i64 16, %struct.List* @four_elts }
|
||||
|
||||
define i64 @sum_half_list(%struct.List* %l) {
|
||||
%1 = icmp eq %struct.List* %l, null
|
||||
br i1 %1, label %then, label %else
|
||||
then:
|
||||
ret i64 0
|
||||
else:
|
||||
%2 = getelementptr %struct.List, %struct.List* %l, i32 0, i32 0
|
||||
%3 = load i64, i64* %2
|
||||
%4 = ashr i64 %3, 1
|
||||
%5 = getelementptr %struct.List, %struct.List* %l, i32 0, i32 1
|
||||
%6 = load %struct.List*, %struct.List** %5
|
||||
%7 = call i64 @sum_half_list(%struct.List* %6)
|
||||
%8 = add i64 %4, %7
|
||||
ret i64 %8
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%1 = call i64 @sum_half_list(%struct.List* @test)
|
||||
ret i64 %1
|
||||
}
|
||||
|
||||
131
hw6/llprograms/sp20_hw3/tarjans_toposort.ll
Normal file
131
hw6/llprograms/sp20_hw3/tarjans_toposort.ll
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
%struct.LLNode = type { i64, %struct.LLNode* }
|
||||
|
||||
@nodeA = global %struct.LLNode { i64 2, %struct.LLNode* @nodeB }
|
||||
@nodeB = global %struct.LLNode { i64 7, %struct.LLNode* null }
|
||||
|
||||
@nodeC = global %struct.LLNode { i64 5, %struct.LLNode* @nodeD }
|
||||
@nodeD = global %struct.LLNode { i64 8, %struct.LLNode* @nodeE }
|
||||
@nodeE = global %struct.LLNode { i64 9, %struct.LLNode* null }
|
||||
|
||||
@nodeF = global %struct.LLNode { i64 4, %struct.LLNode* null }
|
||||
|
||||
@nodeG = global %struct.LLNode { i64 10, %struct.LLNode* null }
|
||||
|
||||
@nodeH = global %struct.LLNode { i64 2, %struct.LLNode* @nodeI }
|
||||
@nodeI = global %struct.LLNode { i64 8, %struct.LLNode* null }
|
||||
|
||||
@nodeJ = global %struct.LLNode { i64 4, %struct.LLNode* null }
|
||||
|
||||
@nodeK = global %struct.LLNode { i64 8, %struct.LLNode* null }
|
||||
|
||||
@nodeL = global %struct.LLNode { i64 0, %struct.LLNode* @nodeM }
|
||||
@nodeM = global %struct.LLNode { i64 4, %struct.LLNode* @nodeN }
|
||||
@nodeN = global %struct.LLNode { i64 7, %struct.LLNode* @nodeO }
|
||||
@nodeO = global %struct.LLNode { i64 8, %struct.LLNode* null }
|
||||
|
||||
@dag = global [12 x %struct.LLNode*] [ %struct.LLNode* @nodeA, %struct.LLNode* @nodeC, %struct.LLNode* @nodeF, %struct.LLNode* @nodeG, %struct.LLNode* null, %struct.LLNode* @nodeH, %struct.LLNode* null, %struct.LLNode* @nodeJ, %struct.LLNode* null, %struct.LLNode* @nodeK, %struct.LLNode* null, %struct.LLNode* @nodeL ]
|
||||
|
||||
@nodesVisited = global [12 x i1] [i1 0, i1 0, i1 0, i1 0, i1 0, i1 0, i1 0, i1 0, i1 0, i1 0, i1 0, i1 0 ]
|
||||
|
||||
@topoSortComputed = global [12 x i64] [ i64 -1, i64 -1, i64 -1, i64 -1, i64 -1, i64 -1, i64 -1, i64 -1, i64 -1, i64 -1, i64 -1, i64 -1 ]
|
||||
|
||||
@topoSortAnswer = global [12 x i64] [ i64 11, i64 6, i64 3, i64 10, i64 1, i64 9, i64 5, i64 8, i64 0, i64 7, i64 2, i64 4 ]
|
||||
|
||||
|
||||
|
||||
define void @DFSVisit([12 x %struct.LLNode*]* %adjList, [12 x i1]* %visited, [12 x i64]* %topoSort, i64 %curr, i64* %sortIndexAddr) {
|
||||
%visitedAddr = getelementptr [12 x i1], [12 x i1]* %visited, i32 0, i64 %curr
|
||||
store i1 1, i1* %visitedAddr
|
||||
|
||||
%neighborsAddr = getelementptr [12 x %struct.LLNode*], [12 x %struct.LLNode*]* %adjList, i32 0, i64 %curr
|
||||
br label %loadNeighbors
|
||||
|
||||
loadNeighbors:
|
||||
%neighbors = load %struct.LLNode*, %struct.LLNode** %neighborsAddr
|
||||
%nullNeighbors = icmp eq %struct.LLNode* %neighbors, null
|
||||
br i1 %nullNeighbors, label %done, label %neighborLoop
|
||||
|
||||
neighborLoop:
|
||||
%neighborAddr = getelementptr %struct.LLNode, %struct.LLNode* %neighbors, i32 0, i32 0
|
||||
%neighbor = load i64, i64* %neighborAddr
|
||||
|
||||
%nextNeighborAddrAddr = getelementptr %struct.LLNode, %struct.LLNode* %neighbors, i32 0, i32 1
|
||||
%nextNeighborAddr = load %struct.LLNode*, %struct.LLNode** %nextNeighborAddrAddr
|
||||
store %struct.LLNode* %nextNeighborAddr, %struct.LLNode** %neighborsAddr
|
||||
|
||||
%neighborVisitedAddr = getelementptr [12 x i1], [12 x i1]* %visited, i32 0, i64 %neighbor
|
||||
%neighborVisited = load i1, i1* %neighborVisitedAddr
|
||||
|
||||
br i1 %neighborVisited, label %loadNeighbors, label %visitNeighbor
|
||||
visitNeighbor:
|
||||
call void @DFSVisit([12 x %struct.LLNode*]* %adjList, [12 x i1]* %visited, [12 x i64]* %topoSort, i64 %neighbor, i64* %sortIndexAddr)
|
||||
br label %loadNeighbors
|
||||
done:
|
||||
%sortIndex = load i64, i64* %sortIndexAddr
|
||||
%topoSortAddr = getelementptr [12 x i64], [12 x i64]* %topoSort, i32 0, i64 %sortIndex
|
||||
store i64 %curr, i64* %topoSortAddr
|
||||
%newSortIndex = sub i64 %sortIndex, 1
|
||||
store i64 %newSortIndex, i64* %sortIndexAddr
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @tarjanTopoSort([12 x %struct.LLNode*]* %adjList, [12 x i1]* %visited, [12 x i64]* %topoSort) {
|
||||
%adjIndexSlot = alloca i64
|
||||
store i64 0, i64* %adjIndexSlot
|
||||
%sortIndexSlot = alloca i64
|
||||
store i64 11, i64* %sortIndexSlot
|
||||
|
||||
br label %DFSLoop
|
||||
DFSLoop:
|
||||
%adjIndex = load i64, i64* %adjIndexSlot
|
||||
%indexOOB = icmp eq i64 %adjIndex, 12
|
||||
br i1 %indexOOB, label %done, label %visitedCheck
|
||||
visitedCheck:
|
||||
%visitedBitAddr = getelementptr [12 x i1], [12 x i1]* %visited, i32 0, i64 %adjIndex
|
||||
%visitedBit = load i1, i1* %visitedBitAddr
|
||||
|
||||
%nextAdjIndex = add i64 %adjIndex, 1
|
||||
store i64 %nextAdjIndex, i64* %adjIndexSlot
|
||||
|
||||
br i1 %visitedBit, label %DFSLoop, label %visit
|
||||
visit:
|
||||
call void @DFSVisit([12 x %struct.LLNode*]* %adjList, [12 x i1]* %visited, [12 x i64]* %topoSort, i64 %adjIndex, i64* %sortIndexSlot)
|
||||
br label %DFSLoop
|
||||
done:
|
||||
ret void
|
||||
}
|
||||
|
||||
define i1 @sortsEqualRec(i64 %i, [12 x i64]* %expected, [12 x i64]* %computed) {
|
||||
%indexOOB = icmp eq i64 %i, 12
|
||||
br i1 %indexOOB, label %base, label %rec
|
||||
base:
|
||||
ret i1 1
|
||||
rec:
|
||||
%nextIndex = add i64 %i, 1
|
||||
%restEqual = call i1 @sortsEqualRec(i64 %nextIndex, [12 x i64]* %expected, [12 x i64]* %computed)
|
||||
|
||||
%ptrE = getelementptr [12 x i64], [12 x i64]* %expected, i32 0, i64 %i
|
||||
%ptrC = getelementptr [12 x i64], [12 x i64]* %computed, i32 0, i64 %i
|
||||
%valE = load i64, i64* %ptrE
|
||||
%valC = load i64, i64* %ptrC
|
||||
%currEqual = icmp eq i64 %valE, %valC
|
||||
|
||||
%bothEqual = and i1 %currEqual, %restEqual
|
||||
br i1 %bothEqual, label %equal, label %notEqual
|
||||
|
||||
equal:
|
||||
ret i1 1
|
||||
notEqual:
|
||||
ret i1 0
|
||||
}
|
||||
|
||||
define i1 @sortsEqual([12 x i64]* %expected, [12 x i64]* %computed) {
|
||||
%1 = call i1 @sortsEqualRec(i64 0, [12 x i64]* %expected, [12 x i64]* %computed)
|
||||
ret i1 %1
|
||||
}
|
||||
|
||||
define i1 @main(i64 %argc, i8** %argv) {
|
||||
call void @tarjanTopoSort([12 x %struct.LLNode*]* @dag, [12 x i1]* @nodesVisited, [12 x i64]* @topoSortComputed)
|
||||
%1 = call i1 @sortsEqual([12 x i64]* @topoSortAnswer, [12 x i64]* @topoSortComputed)
|
||||
ret i1 %1
|
||||
}
|
||||
36
hw6/llprograms/sp20_hw3/tree_search.ll
Normal file
36
hw6/llprograms/sp20_hw3/tree_search.ll
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
%node = type { %node*, %node*, i64 }
|
||||
|
||||
@root = global %node { %node* @node1, %node* @node2, i64 5 }
|
||||
@node1 = global %node { %node* @node3, %node* @node4, i64 2 }
|
||||
@node2 = global %node { %node* @node5, %node* null, i64 8 }
|
||||
@node3 = global %node { %node* null, %node* null, i64 1 }
|
||||
@node4 = global %node { %node* null, %node* null, i64 3 }
|
||||
@node5 = global %node { %node* null, %node* null, i64 7 }
|
||||
|
||||
define i64 @tree_search(%node* %root, i64 %target) {
|
||||
%is_null = icmp eq %node* %root, null
|
||||
br i1 %is_null, label %null_node, label %valid_node
|
||||
valid_node:
|
||||
%value_ptr = getelementptr %node, %node* %root, i32 0, i32 2
|
||||
%value = load i64, i64* %value_ptr
|
||||
%check = icmp eq i64 %target, %value
|
||||
br i1 %check, label %found, label %not_found
|
||||
null_node:
|
||||
ret i64 0
|
||||
found:
|
||||
ret i64 1
|
||||
not_found:
|
||||
%left_child = getelementptr %node, %node* %root, i32 0, i32 0
|
||||
%right_child = getelementptr %node, %node* %root, i32 0, i32 1
|
||||
%left_child_ptr = load %node*, %node** %left_child
|
||||
%right_child_ptr = load %node*, %node** %right_child
|
||||
%left_check = call i64 @tree_search(%node* %left_child_ptr, i64 %target)
|
||||
%right_check = call i64 @tree_search(%node* %right_child_ptr, i64 %target)
|
||||
%result = or i64 %left_check, %right_check
|
||||
ret i64 %result
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%answer = call i64 @tree_search(%node* @root, i64 7)
|
||||
ret i64 %answer
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue