Change hw6 to an unsolved version.
Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
parent
0c04936ccf
commit
ee01a8f5b2
186 changed files with 9605 additions and 4019 deletions
5
hw6/llprograms/sp24_hw3/README.md
Normal file
5
hw6/llprograms/sp24_hw3/README.md
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
# HW3 Test case .ll files submitted by students for SP 2024
|
||||
|
||||
Add any `.ll` files needed by your test case to this directory.
|
||||
See the homework instructions and the comments in `tests/sp24_tests.ml` for
|
||||
more details.
|
||||
5
hw6/llprograms/sp24_hw3/add.ll
Normal file
5
hw6/llprograms/sp24_hw3/add.ll
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%1 = add i64 5, 9
|
||||
ret i64 %1
|
||||
}
|
||||
|
||||
49
hw6/llprograms/sp24_hw3/binary_search.ll
Normal file
49
hw6/llprograms/sp24_hw3/binary_search.ll
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
; binary search of an array
|
||||
%arr = type [10 x i64]
|
||||
@input_arr = global %arr [ i64 -13, i64 3, i64 5, i64 15, i64 17, i64 27, i64 28, i64 37, i64 45, i64 54]
|
||||
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%1 = getelementptr %arr, %arr* @input_arr, i64 0, i64 0
|
||||
%2 = call i64 @binary_search(i64* %1, i64 45, i64 0, i64 9)
|
||||
ret i64 %2
|
||||
}
|
||||
|
||||
define i64 @binary_search(i64* %a, i64 %x, i64 %lo, i64 %hi) {
|
||||
; return if hi < lo
|
||||
%1 = icmp slt i64 %hi, %lo
|
||||
br i1 %1, label %ret_base, label %loop
|
||||
|
||||
loop:
|
||||
; find midpoint index in the array
|
||||
%2 = add i64 %lo, %hi
|
||||
%m = ashr i64 %2, 1
|
||||
|
||||
; find midpoint element
|
||||
%3 = getelementptr i64, i64* %a, i64 %m
|
||||
%e = load i64, i64* %3
|
||||
|
||||
; see if midpoint element is equal to x
|
||||
%4 = icmp eq i64 %e, %x
|
||||
br i1 %4, label %ret_found, label %divide
|
||||
|
||||
divide:
|
||||
; see if x is less than midpoint elt
|
||||
%5 = icmp slt i64 %x, %e
|
||||
br i1 %5, label %is_less, label %is_greater
|
||||
|
||||
is_less: ; if x < arr[mid]
|
||||
%6 = sub i64 %m, 1
|
||||
%7 = call i64 @binary_search(i64* %a, i64 %x, i64 %lo, i64 %6)
|
||||
ret i64 %7
|
||||
is_greater: ; if x > arr[mid]
|
||||
%8 = add i64 %m, 1
|
||||
%9 = call i64 @binary_search(i64* %a, i64 %x, i64 %8, i64 %hi)
|
||||
ret i64 %9
|
||||
|
||||
ret_base:
|
||||
ret i64 -1
|
||||
|
||||
ret_found:
|
||||
ret i64 %m
|
||||
}
|
||||
83
hw6/llprograms/sp24_hw3/binary_tree_map.ll
Normal file
83
hw6/llprograms/sp24_hw3/binary_tree_map.ll
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
%struct.Tree = type { %struct.Tree*, %struct.Tree*, i64, i64 }
|
||||
|
||||
@test1 = global %struct.Tree { %struct.Tree* null, %struct.Tree* null, i64 1, i64 100 }
|
||||
@test2 = global %struct.Tree { %struct.Tree* @test1, %struct.Tree* null, i64 2, i64 200 }
|
||||
@test3 = global %struct.Tree { %struct.Tree* null, %struct.Tree* null, i64 4, i64 300 }
|
||||
@test = global %struct.Tree { %struct.Tree* @test2, %struct.Tree* @test3, i64 3, i64 400 }
|
||||
|
||||
@testB = global %struct.Tree { %struct.Tree* null, %struct.Tree* null, i64 1, i64 435 }
|
||||
@testC = global %struct.Tree { %struct.Tree* null, %struct.Tree* null, i64 3, i64 765 }
|
||||
@testA = global %struct.Tree { %struct.Tree* @testB, %struct.Tree* @testC, i64 2, i64 2356 }
|
||||
@testE = global %struct.Tree { %struct.Tree* null, %struct.Tree* null, i64 5, i64 65 }
|
||||
@testF = global %struct.Tree { %struct.Tree* null, %struct.Tree* null, i64 7, i64 42 }
|
||||
@testD = global %struct.Tree { %struct.Tree* @testE, %struct.Tree* @testF, i64 6, i64 658 }
|
||||
@testNew = global %struct.Tree { %struct.Tree* @testA, %struct.Tree* @testD, i64 4, i64 5346 }
|
||||
|
||||
|
||||
define i64 @find_value(%struct.Tree* %t, i64 %key) {
|
||||
%1 = icmp eq %struct.Tree* %t, null
|
||||
br i1 %1, label %not_found, label %found
|
||||
not_found:
|
||||
ret i64 -1
|
||||
found:
|
||||
%2 = getelementptr %struct.Tree, %struct.Tree* %t, i64 0, i64 2
|
||||
%3 = load i64, i64* %2
|
||||
%4 = icmp eq i64 %3, %key
|
||||
br i1 %4, label %return_value, label %search_subtree
|
||||
return_value:
|
||||
%5 = getelementptr %struct.Tree, %struct.Tree* %t, i64 0, i64 3
|
||||
%6 = load i64, i64* %5
|
||||
ret i64 %6
|
||||
search_subtree:
|
||||
%7 = icmp slt i64 %key, %3
|
||||
br i1 %7, label %left_subtree, label %right_subtree
|
||||
left_subtree:
|
||||
%8 = getelementptr %struct.Tree, %struct.Tree* %t, i64 0, i64 0
|
||||
%9 = load %struct.Tree*, %struct.Tree** %8
|
||||
%10 = call i64 @find_value(%struct.Tree* %9, i64 %key)
|
||||
ret i64 %10
|
||||
right_subtree:
|
||||
%11 = getelementptr %struct.Tree, %struct.Tree* %t, i64 0, i64 1
|
||||
%12 = load %struct.Tree*, %struct.Tree** %11
|
||||
%13 = call i64 @find_value(%struct.Tree* %12, i64 %key)
|
||||
ret i64 %13
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%1 = call i64 @find_value(%struct.Tree* @test, i64 1)
|
||||
%2 = call i64 @find_value(%struct.Tree* @test, i64 2)
|
||||
%3 = call i64 @find_value(%struct.Tree* @test, i64 3)
|
||||
%4 = icmp eq i64 %1, 100
|
||||
%5 = icmp eq i64 %2, 200
|
||||
%6 = icmp eq i64 %3, 400
|
||||
%7 = and i1 %4, %5
|
||||
%8 = and i1 %7, %6
|
||||
|
||||
%9 = call i64 @find_value(%struct.Tree* @testNew, i64 1)
|
||||
%10 = call i64 @find_value(%struct.Tree* @testNew, i64 2)
|
||||
%11 = call i64 @find_value(%struct.Tree* @testNew, i64 3)
|
||||
%12 = call i64 @find_value(%struct.Tree* @testNew, i64 4)
|
||||
%13 = call i64 @find_value(%struct.Tree* @testNew, i64 5)
|
||||
%14 = call i64 @find_value(%struct.Tree* @testNew, i64 6)
|
||||
%15 = call i64 @find_value(%struct.Tree* @testNew, i64 7)
|
||||
%16 = icmp eq i64 %9, 435
|
||||
%17 = icmp eq i64 %10, 2356
|
||||
%18 = icmp eq i64 %11, 765
|
||||
%19 = icmp eq i64 %12, 5346
|
||||
%20 = icmp eq i64 %13, 65
|
||||
%21 = icmp eq i64 %14, 658
|
||||
%22 = icmp eq i64 %15, 42
|
||||
%23 = and i1 %16, %17
|
||||
%24 = and i1 %18, %19
|
||||
%25 = and i1 %20, %21
|
||||
%26 = and i1 %22, %23
|
||||
%27 = and i1 %24, %25
|
||||
%28 = and i1 %26, %27
|
||||
%29 = and i1 %28, %8
|
||||
|
||||
br i1 %29, label %pass, label %fail
|
||||
pass:
|
||||
ret i64 0
|
||||
fail:
|
||||
ret i64 1
|
||||
}
|
||||
129
hw6/llprograms/sp24_hw3/bubblesort.ll
Normal file
129
hw6/llprograms/sp24_hw3/bubblesort.ll
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
%arraystruct = type {[12 x i64]}
|
||||
|
||||
; eloic + tom
|
||||
@unsorted_array_1 = global %arraystruct {[12 x i64] [i64 -95, i64 18, i64 -50, i64 -22, i64 -51, i64 -1, i64 94, i64 6, i64 82, i64 82, i64 -23, i64 44]}
|
||||
@unsorted_array_2 = global %arraystruct {[12 x i64] [i64 2, i64 79, i64 -8, i64 -86, i64 6, i64 -29, i64 88, i64 -80, i64 2, i64 21, i64 -26, i64 -13]}
|
||||
@unsorted_array_3 = global %arraystruct {[12 x i64] [i64 16, i64 -1, i64 3, i64 51, i64 30, i64 49, i64 -48, i64 -99, i64 -13, i64 57, i64 -63, i64 29]}
|
||||
@unsorted_array_4 = global %arraystruct {[12 x i64] [i64 91, i64 87, i64 -80, i64 60, i64 -43, i64 -79, i64 -12, i64 -52, i64 -42, i64 69, i64 87, i64 -86]}
|
||||
@unsorted_array_5 = global %arraystruct {[12 x i64] [i64 89, i64 89, i64 74, i64 89, i64 -50, i64 7, i64 -46, i64 -37, i64 30, i64 -50, i64 34, i64 -80]}
|
||||
|
||||
define i1 @is_sorted(%arraystruct* %arr) {
|
||||
%array_end_ptr = alloca i64
|
||||
store i64 11, i64* %array_end_ptr
|
||||
%array_end = load i64, i64* %array_end_ptr
|
||||
%i = alloca i64
|
||||
store i64 0, i64* %i
|
||||
br label %loop_cond
|
||||
|
||||
loop_cond:
|
||||
%i_val = load i64, i64* %i
|
||||
%loop_cmp = icmp slt i64 %i_val, %array_end
|
||||
br i1 %loop_cmp, label %loop_body, label %sorted
|
||||
|
||||
loop_body:
|
||||
%curr_ptr = getelementptr %arraystruct, %arraystruct* %arr, i64 0, i32 0, i64 %i_val
|
||||
%next_ptr = getelementptr i64, i64* %curr_ptr, i64 1
|
||||
|
||||
%curr_elem = load i64, i64* %curr_ptr
|
||||
%next_elem = load i64, i64* %next_ptr
|
||||
|
||||
%elem_cmp = icmp sle i64 %curr_elem, %next_elem
|
||||
br i1 %elem_cmp, label %increment, label %not_sorted
|
||||
|
||||
increment:
|
||||
%next_i = add i64 %i_val, 1
|
||||
store i64 %next_i, i64* %i
|
||||
br label %loop_cond
|
||||
|
||||
not_sorted:
|
||||
ret i1 0
|
||||
|
||||
sorted:
|
||||
ret i1 1
|
||||
}
|
||||
|
||||
define void @swap(i64* %x, i64* %y) {
|
||||
%temp1 = load i64, i64* %x
|
||||
%temp2 = load i64, i64* %y
|
||||
store i64 %temp1, i64* %y
|
||||
store i64 %temp2, i64* %x
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @bubblesort(%arraystruct* %arr) {
|
||||
%length = alloca i64
|
||||
store i64 12, i64* %length
|
||||
%i = alloca i64
|
||||
%j = alloca i64
|
||||
br label %outer_loop_entry
|
||||
|
||||
outer_loop_entry:
|
||||
store i64 0, i64* %i
|
||||
%length_val = load i64, i64* %length
|
||||
br label %outer_loop_cond
|
||||
|
||||
outer_loop_cond:
|
||||
%i_val = load i64, i64* %i
|
||||
%cmp1 = icmp slt i64 %i_val, %length_val
|
||||
br i1 %cmp1, label %inner_loop_entry, label %exit
|
||||
|
||||
inner_loop_entry:
|
||||
store i64 0, i64* %j
|
||||
%i_val_for_j = load i64, i64* %i
|
||||
%length_for_j = sub i64 %length_val, 1
|
||||
%length_for_j = sub i64 %length_for_j, %i_val_for_j
|
||||
br label %inner_loop_cond
|
||||
|
||||
inner_loop_cond:
|
||||
%j_val = load i64, i64* %j
|
||||
%cmp2 = icmp slt i64 %j_val, %length_for_j
|
||||
br i1 %cmp2, label %swap_check, label %inner_loop_exit
|
||||
|
||||
swap_check:
|
||||
%j_plus_1 = add i64 %j_val, 1
|
||||
%elem1_ptr = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 %j_val
|
||||
%elem2_ptr = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 %j_plus_1
|
||||
%elem1 = load i64, i64* %elem1_ptr
|
||||
%elem2 = load i64, i64* %elem2_ptr
|
||||
%cmp3 = icmp sgt i64 %elem1, %elem2
|
||||
br i1 %cmp3, label %do_swap, label %no_swap
|
||||
|
||||
do_swap:
|
||||
call void @swap(i64* %elem1_ptr, i64* %elem2_ptr)
|
||||
br label %no_swap
|
||||
|
||||
no_swap:
|
||||
%j_inc = add i64 %j_val, 1
|
||||
store i64 %j_inc, i64* %j
|
||||
br label %inner_loop_cond
|
||||
|
||||
inner_loop_exit:
|
||||
%i_inc = add i64 %i_val, 1
|
||||
store i64 %i_inc, i64* %i
|
||||
br label %outer_loop_cond
|
||||
|
||||
exit:
|
||||
ret void
|
||||
}
|
||||
|
||||
|
||||
define i1 @main(i64 %argc, i8** %arcv) {
|
||||
call void @bubblesort(%arraystruct* @unsorted_array_1, i64 0, i64 11)
|
||||
call void @bubblesort(%arraystruct* @unsorted_array_2, i64 0, i64 11)
|
||||
call void @bubblesort(%arraystruct* @unsorted_array_3, i64 0, i64 11)
|
||||
call void @bubblesort(%arraystruct* @unsorted_array_4, i64 0, i64 11)
|
||||
call void @bubblesort(%arraystruct* @unsorted_array_5, i64 0, i64 11)
|
||||
|
||||
%return_val_1 = call i1 @is_sorted(%arraystruct* @unsorted_array_1)
|
||||
%return_val_2 = call i1 @is_sorted(%arraystruct* @unsorted_array_2)
|
||||
%return_val_3 = call i1 @is_sorted(%arraystruct* @unsorted_array_5)
|
||||
%return_val_4 = call i1 @is_sorted(%arraystruct* @unsorted_array_4)
|
||||
%return_val_5 = call i1 @is_sorted(%arraystruct* @unsorted_array_3)
|
||||
|
||||
%ret = add i64 %return_val_1, %return_val_2
|
||||
%ret = add i64 %ret, %return_val_3
|
||||
%ret = add i64 %ret, %return_val_4
|
||||
%ret = add i64 %ret, %return_val_5
|
||||
|
||||
ret i1 %ret
|
||||
}
|
||||
60
hw6/llprograms/sp24_hw3/dilinir1_afuji_ts.ll
Normal file
60
hw6/llprograms/sp24_hw3/dilinir1_afuji_ts.ll
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
%arr = type [4 x i64]
|
||||
@tmp = global %arr [ i64 2, i64 7, i64 11, i64 15]
|
||||
@target = global i64 9
|
||||
|
||||
define i64 @two_sum() {
|
||||
%len = sub i64 4, 1
|
||||
%i = alloca i64
|
||||
%j = alloca i64
|
||||
|
||||
br label %outer_loop
|
||||
|
||||
outer_loop:
|
||||
store i64 0, i64* %i
|
||||
store i64 1, i64* %j
|
||||
br label %inner_loop
|
||||
|
||||
inner_loop:
|
||||
%idx1 = load i64, i64* %i
|
||||
%idx2 = load i64, i64* %j
|
||||
|
||||
%ptr1 = getelementptr %arr, arr* @tmp, i32 0, i64 %idx1
|
||||
%ptr2 = getelementptr %arr, arr* @tmp, i32 0, i64 %idx2
|
||||
|
||||
%val1 = load i64, i64* %ptr1
|
||||
%val2 = load i64, i64* %ptr2
|
||||
|
||||
%sum = add i64 %val1, %val2
|
||||
%tgt = load i64, i64* @target
|
||||
%cmp_sum = icmp eq i64 %sum, %tgt
|
||||
br i1 %cmp_sum, label %found, label %check_next
|
||||
|
||||
found:
|
||||
ret i64 1
|
||||
|
||||
check_next:
|
||||
%j_val = load i64, i64* %j
|
||||
%j_val_inc = add i64 %j_val, 1
|
||||
store i64 %j_val_inc, i64* %j
|
||||
|
||||
%cmp2 = icmp slt i64 %j_val_inc, 4
|
||||
br i1 %cmp2, label %inner_loop, label %check_outer_loop
|
||||
|
||||
check_outer_loop:
|
||||
%i_val = load i64, i64* %i
|
||||
%i_val_inc = add i64 %i_val, 1
|
||||
store i64 %i_val_inc, i64* %i
|
||||
|
||||
%cmp3 = icmp slt i64 %i_val_inc, 3
|
||||
br i1 %cmp3, label %outer_loop, label %not_found
|
||||
|
||||
not_found:
|
||||
ret i64 0
|
||||
|
||||
}
|
||||
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%1 = call i64 @two_sum()
|
||||
ret i64 %1
|
||||
}
|
||||
27
hw6/llprograms/sp24_hw3/fibonacci.ll
Normal file
27
hw6/llprograms/sp24_hw3/fibonacci.ll
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
%arr = type [2 x i64]
|
||||
@fib_arr = global %arr [i64 1, i64 0]
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%1 = call i64 @rec_fibonacci(i64 5, i64 1)
|
||||
ret i64 %1
|
||||
}
|
||||
|
||||
define i64 @rec_fibonacci(i64 %n, i64 %curr) {
|
||||
%check_done = icmp eq i64 %curr, %n
|
||||
br i1 %check_done, label %end, label %recurse
|
||||
recurse:
|
||||
%pt1 = getelementptr %arr, %arr* @fib_arr, i32 0, i64 0
|
||||
%pt2 = getelementptr %arr, %arr* @fib_arr, i32 0, i64 1
|
||||
%1 = load i64, i64* %pt1
|
||||
%2 = load i64, i64* %pt2
|
||||
%3 = add i64 %1, %2
|
||||
store i64 %3, i64* %pt1
|
||||
store i64 %1, i64* %pt2
|
||||
%curr2 = add i64 %curr, 1
|
||||
%ret = call i64 @rec_fibonacci(i64 %n, i64 %curr2)
|
||||
ret i64 %ret
|
||||
end:
|
||||
%rv_pt = getelementptr %arr, %arr* @fib_arr, i32 0, i64 0
|
||||
%rv = load i64, i64* %rv_pt
|
||||
ret i64 %rv
|
||||
}
|
||||
85
hw6/llprograms/sp24_hw3/find_duplicate.ll
Normal file
85
hw6/llprograms/sp24_hw3/find_duplicate.ll
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
|
||||
; Created by Adam & Hassan :)
|
||||
; Finds a duplicate to the array assuming there is only one duplicate and all items are in range [1, n]
|
||||
|
||||
; LeetCode #287
|
||||
; Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive.
|
||||
; There is only one repeated number in nums, return this repeated number.
|
||||
; You must solve the problem without modifying the array nums and uses only constant extra space.
|
||||
; Uses Floyd's Cycle Detection algo
|
||||
|
||||
%arr_t = type [5 x i64]
|
||||
@input_arr = global %arr_t [ i64 1, i64 2, i64 3, i64 3, i64 4 ]
|
||||
|
||||
|
||||
define i64 @find_dup() {
|
||||
|
||||
; initialize my slow and fast pointers
|
||||
%slow = alloca i64
|
||||
%fast = alloca i64
|
||||
|
||||
; second slow pointer for phase 2
|
||||
%slow_2 = alloca i64
|
||||
|
||||
; set them all to 0
|
||||
store i64 0, i64* %slow
|
||||
store i64 0, i64* %fast
|
||||
store i64 0, i64* %slow_2
|
||||
|
||||
; move on
|
||||
br label %phase_1_loop
|
||||
|
||||
phase_1_loop:
|
||||
; slow = nums[slow]
|
||||
; aka stepping through slow
|
||||
%slow_val = load i64, i64* %slow
|
||||
%slow_val_ptr = getelementptr %arr_t, %arr_t* @input_arr, i64 0, i64 %slow_val
|
||||
%slow_val_loaded = load i64, i64* %slow_val_ptr
|
||||
store i64 %slow_val_loaded, i64* %slow
|
||||
|
||||
; fast = nums[nums[fast]]
|
||||
; aka stepping through fast
|
||||
%fast_val = load i64, i64* %fast
|
||||
%fast_val_ptr = getelementptr %arr_t, %arr_t* @input_arr, i64 0, i64 %fast_val
|
||||
%fast_val_loaded = load i64, i64* %fast_val_ptr
|
||||
%fast_val_ptr_2 = getelementptr %arr_t, %arr_t* @input_arr, i64 0, i64 %fast_val_loaded
|
||||
%fast_val_loaded2 = load i64, i64* %fast_val_ptr_2
|
||||
store i64 %fast_val_loaded2, i64* %fast
|
||||
|
||||
; if slow == fast, break
|
||||
; otherwise, continue
|
||||
%cmp = icmp eq i64 %slow_val_loaded, %fast_val_loaded2
|
||||
br i1 %cmp, label %phase_2_loop, label %phase_1_loop
|
||||
|
||||
phase_2_loop:
|
||||
; slow = nums[slow]
|
||||
; aka stepping through slow
|
||||
%slow_val_p2 = load i64, i64* %slow
|
||||
%slow_val_ptr_p2 = getelementptr %arr_t, %arr_t* @input_arr, i64 0, i64 %slow_val_p2
|
||||
%slow_val_p2_2 = load i64, i64* %slow_val_ptr_p2
|
||||
store i64 %slow_val_p2_2, i64* %slow
|
||||
|
||||
; slow = nums[slow_2]
|
||||
; aka stepping through other slow
|
||||
%slow2_val = load i64, i64* %slow_2
|
||||
%slow2_val_ptr_p2 = getelementptr %arr_t, %arr_t* @input_arr, i64 0, i64 %slow2_val
|
||||
%slow2_val_2 = load i64, i64* %slow2_val_ptr_p2
|
||||
store i64 %slow2_val_2, i64* %slow_2
|
||||
|
||||
; if slow == slow_2, return slow, otherwise loop
|
||||
%cmp_p2 = icmp eq i64 %slow_val_p2_2, %slow2_val_2
|
||||
br i1 %cmp, label %return_slow, label %phase_2_loop
|
||||
|
||||
return_slow:
|
||||
ret i64 %slow_val_p2_2
|
||||
}
|
||||
|
||||
|
||||
; just takes in the global arr - to avoid compile issues
|
||||
; should return 3L
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%main_result = call i64 @find_dup()
|
||||
ret i64 %main_result
|
||||
}
|
||||
|
||||
|
||||
55
hw6/llprograms/sp24_hw3/find_power_of_two.ll
Normal file
55
hw6/llprograms/sp24_hw3/find_power_of_two.ll
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
%consList = type { i64, %consList* }
|
||||
|
||||
@nonPowerOfTwoElem = global %consList { i64 9, %consList* null }
|
||||
@powerOfTwoElem = global %consList { i64 64, %consList* @nonPowerOfTwoElem }
|
||||
@head = global %consList { i64 31, %consList* @powerOfTwoElem }
|
||||
|
||||
define i64 @numOneBits(i64 %n) {
|
||||
%isZero = icmp eq i64 %n, 0
|
||||
br i1 %isZero, label %retZero, label %rec
|
||||
retZero:
|
||||
ret i64 0
|
||||
rec:
|
||||
%shifted = lshr i64 %n, 1
|
||||
%recResult = call i64 @numOneBits(i64 %shifted)
|
||||
%lsb = and i64 %n, 1
|
||||
%result = add i64 %recResult, %lsb
|
||||
ret i64 %result
|
||||
}
|
||||
|
||||
define i1 @isPowerOfTwo(i64 %n) {
|
||||
%isNotPositive = icmp sle i64 %n, 0
|
||||
br i1 %isNotPositive, label %retFalse, label %continue
|
||||
retFalse:
|
||||
ret i1 0
|
||||
continue:
|
||||
%numOnes = call i64 @numOneBits(i64 %n)
|
||||
%hasSingleOneBit = icmp eq i64 %numOnes, 1
|
||||
br i1 %hasSingleOneBit, label %retTrue, label %retFalse
|
||||
retTrue:
|
||||
ret i1 1
|
||||
}
|
||||
|
||||
define i64 @findFirstPowerOfTwo(%consList* %l) {
|
||||
%isNull = icmp eq %consList* %l, null
|
||||
br i1 %isNull, label %retFalse, label %continue
|
||||
retFalse:
|
||||
ret i64 0
|
||||
continue:
|
||||
%currNumPtr = getelementptr %consList, %consList* %l, i32 0, i32 0
|
||||
%currNum = load i64, i64* %currNumPtr
|
||||
%isPowTwo = call i1 @isPowerOfTwo(i64 %currNum)
|
||||
br i1 %isPowTwo, label %retTrue, label %rec
|
||||
retTrue:
|
||||
ret i64 %currNum
|
||||
rec:
|
||||
%nextPtr = getelementptr %consList, %consList* %l, i32 0, i32 1
|
||||
%next = load %consList*, %consList** %nextPtr
|
||||
%recResult = call i64 @findFirstPowerOfTwo(%consList* %next)
|
||||
ret i64 %recResult
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%powTwoValue = call i64 @findFirstPowerOfTwo(%consList* @head)
|
||||
ret i64 %powTwoValue
|
||||
}
|
||||
59
hw6/llprograms/sp24_hw3/gabsmi_hojack.ll
Normal file
59
hw6/llprograms/sp24_hw3/gabsmi_hojack.ll
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
@my_arr1 = global [8 x i64] [i64 1, i64 2, i64 3, i64 4, i64 5, i64 4, i64 3, i64 2]
|
||||
@my_arr2 = global [8 x i64] [i64 1, i64 10, i64 9, i64 7, i64 6, i64 5, i64 4, i64 2]
|
||||
|
||||
define i64 @local_max([8 x i64]* %arr, i64 %lo, i64 %hi) {
|
||||
%dif = sub i64 %hi, %lo
|
||||
%1 = icmp eq i64 %dif, 2
|
||||
br i1 %1, label %base_case, label %general_case
|
||||
base_case:
|
||||
%ret_val = add i64 %lo, 1
|
||||
ret i64 %ret_val
|
||||
general_case:
|
||||
%2 = add i64 %hi, %lo
|
||||
%3 = ashr i64 %2, 1
|
||||
|
||||
%i = getelementptr [8 x i64], [8 x i64]* %arr, i32 0, i64 %3
|
||||
%ival = load i64, i64* %i
|
||||
|
||||
%4 = sub i64 %3, 1
|
||||
%left = getelementptr [8 x i64], [8 x i64]* %arr, i32 0, i64 %4
|
||||
%leftVal = load i64, i64* %left
|
||||
|
||||
%5 = add i64 %4, 1
|
||||
%right = getelementptr [8 x i64], [8 x i64]* %arr, i32 0, i64 %5
|
||||
%rightVal = load i64, i64* %right
|
||||
|
||||
%6 = icmp sge i64 %leftVal, %ival
|
||||
br i1 %6, label %rec_call_left, label %check2
|
||||
|
||||
check2:
|
||||
%7 = icmp sge i64 %rightVal, %ival
|
||||
br i1 %7, label %rec_call_right, label %is_max
|
||||
is_max:
|
||||
ret i64 %3
|
||||
rec_call_left:
|
||||
%8 = call i64 @local_max([8 x i64]* %arr, i64 %lo, i64 %3)
|
||||
ret i64 %8
|
||||
rec_call_right:
|
||||
%9 = call i64 @local_max([8 x i64]* %arr, i64 %3, i64 %hi)
|
||||
ret i64 %9
|
||||
test:
|
||||
ret i64 200
|
||||
}
|
||||
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%1 = call i64 @local_max([8 x i64]* @my_arr1, i64 0, i64 7)
|
||||
%test_1 = icmp sge i64 %1, 4
|
||||
br i1 %test_1, label %test2, label %fail
|
||||
|
||||
test2:
|
||||
%test_2_val = call i64 @local_max([8 x i64]* @my_arr2, i64 0, i64 7)
|
||||
%test_2 = icmp sge i64 %1, 2
|
||||
br i1 %test_1, label %pass, label %fail
|
||||
|
||||
fail:
|
||||
ret i64 0
|
||||
pass:
|
||||
ret i64 1
|
||||
}
|
||||
88
hw6/llprograms/sp24_hw3/geometric_series.ll
Normal file
88
hw6/llprograms/sp24_hw3/geometric_series.ll
Normal file
|
|
@ -0,0 +1,88 @@
|
|||
; fdustin, geneliu
|
||||
; generates a geometric series of 10 numbers given a base and a factor
|
||||
%array = type [10 x i64]
|
||||
|
||||
@base = global i64 2
|
||||
@factor = global i64 2
|
||||
@answer = global %array [ i64 2, i64 4, i64 8, i64 16, i64 32, i64 64, i64 128, i64 256, i64 512, i64 1024 ]
|
||||
@temp= global %array [ i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0 ]
|
||||
|
||||
define i64 @check_answer(%array* %expected, %array* %actual) {
|
||||
%curr_idx = alloca i64
|
||||
store i64 0, i64* %curr_idx
|
||||
br label %loop_check_cond
|
||||
|
||||
loop_check_cond:
|
||||
%idx = load i64, i64* %curr_idx
|
||||
%cond_keep = icmp slt i64 %idx, 10
|
||||
br i1 %cond_keep, label %loop_check_body, label %loop_check_exit
|
||||
|
||||
loop_check_body:
|
||||
%exp_ptr = getelementptr %array, %array* %expected, i32 0, i64 %idx
|
||||
%act_ptr = getelementptr %array, %array* %actual, i32 0, i64 %idx
|
||||
%exp_val = load i64, i64* %exp_ptr
|
||||
%act_val = load i64, i64* %act_ptr
|
||||
|
||||
%cmp = icmp eq i64 %exp_val, %act_val
|
||||
|
||||
%new_idx = add i64 %idx, 1
|
||||
store i64 %new_idx, i64* %curr_idx
|
||||
br i1 %cmp, label %loop_check_cond, label %loop_check_bad
|
||||
|
||||
loop_check_bad:
|
||||
ret i64 0
|
||||
|
||||
loop_check_exit:
|
||||
ret i64 1
|
||||
}
|
||||
|
||||
define void @geometric_series(i64 %base, i64 %factor, %array* %arr) {
|
||||
%curr_idx = alloca i64
|
||||
store i64 0, i64* %curr_idx
|
||||
br label %loop_geo_cond
|
||||
|
||||
loop_geo_cond:
|
||||
%idx = load i64, i64* %curr_idx
|
||||
%cond = icmp slt i64 %idx, 10
|
||||
br i1 %cond, label %loop_geo_body, label %loop_geo_exit
|
||||
|
||||
loop_geo_body:
|
||||
%should_use_base_value = icmp eq i64 %idx, 0
|
||||
br i1 %should_use_base_value, label %use_base_value, label %use_array_value
|
||||
use_base_value:
|
||||
; load the base value into the 1st element of the array
|
||||
%arr_index_zero = getelementptr %array, %array* %arr, i32 0, i64 0
|
||||
store i64 %base, i64* %arr_index_zero
|
||||
; increment the counter
|
||||
%new_idx_of_base = add i64 %idx, 1
|
||||
store i64 %new_idx_of_base, i64* %curr_idx
|
||||
br label %loop_geo_cond
|
||||
|
||||
use_array_value:
|
||||
; use the previous value to calculate the next value
|
||||
%prev_idx = sub i64 %idx, 1
|
||||
%prev_value_ptr = getelementptr %array, %array* %arr, i32 0, i64 %prev_idx
|
||||
%prev_value = load i64, i64* %prev_value_ptr
|
||||
%new_value = mul i64 %prev_value, %factor
|
||||
|
||||
; store the new value into the array
|
||||
%new_value_ptr = getelementptr %array, %array* %arr, i32 0, i64 %idx
|
||||
store i64 %new_value, i64* %new_value_ptr
|
||||
|
||||
; increment the counter
|
||||
%new_idx_of_non_base = add i64 %idx, 1
|
||||
store i64 %new_idx_of_non_base, i64* %curr_idx
|
||||
br label %loop_geo_cond
|
||||
|
||||
loop_geo_exit:
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @main() {
|
||||
%base_local = load i64, i64* @base
|
||||
%factor_local = load i64, i64* @factor
|
||||
call void @geometric_series(i64 %base_local, i64 %factor_local, %array* @temp)
|
||||
%res = call i64 @check_answer(%array* @answer, %array* @temp)
|
||||
|
||||
ret i64 %res
|
||||
}
|
||||
191
hw6/llprograms/sp24_hw3/heapsort.ll
Normal file
191
hw6/llprograms/sp24_hw3/heapsort.ll
Normal file
|
|
@ -0,0 +1,191 @@
|
|||
%arraystruct = type {[9 x i64]}
|
||||
|
||||
; 8,7,2,1,0,9,3,2,1
|
||||
@gtest1 = global %arraystruct {[9 x i64] [i64 8, i64 7, i64 2, i64 1, i64 0, i64 9, i64 3, i64 2, i64 1]}
|
||||
@ans1 = global %arraystruct {[9 x i64] [i64 0, i64 1, i64 1, i64 2, i64 2, i64 3, i64 7, i64 8, i64 9]}
|
||||
@gtest2 = global %arraystruct {[9 x i64] [i64 1, i64 5, i64 3, i64 8, i64 2, i64 6, i64 9, i64 4, i64 7]}
|
||||
@ans2 = global %arraystruct {[9 x i64] [i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9]}
|
||||
@gtest3 = global %arraystruct {[9 x i64] [i64 9, i64 3, i64 6, i64 1, i64 5, i64 2, i64 7, i64 8, i64 4]}
|
||||
@ans3 = global %arraystruct {[9 x i64] [i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9]}
|
||||
@gtest4 = global %arraystruct {[9 x i64] [i64 5, i64 2, i64 8, i64 9, i64 1, i64 4, i64 6, i64 3, i64 7]}
|
||||
@ans4 = global %arraystruct {[9 x i64] [i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9]}
|
||||
|
||||
define i1 @eq9(%arraystruct* %arr1, %arraystruct* %arr2) {
|
||||
%lengthptr = alloca i64
|
||||
store i64 9, i64* %lengthptr
|
||||
%length = load i64, i64* %lengthptr
|
||||
%iptr = alloca i64
|
||||
store i64 0, i64* %iptr
|
||||
br label %loop_entry
|
||||
loop_entry:
|
||||
%iteri = load i64, i64* %iptr
|
||||
%cmpll = icmp slt i64 %iteri, %length
|
||||
br i1 %cmpll, label %loop_body, label %loop_exit_s
|
||||
loop_body:
|
||||
%arri1ptr = getelementptr %arraystruct, %arraystruct* %arr1, i32 0, i32 0, i64 %iteri
|
||||
%arri1 = load i64, i64* %arri1ptr
|
||||
%arri2ptr = getelementptr %arraystruct, %arraystruct* %arr2, i32 0, i32 0, i64 %iteri
|
||||
%arri2 = load i64, i64* %arri2ptr
|
||||
%cmp12 = icmp eq i64 %arri1, %arri2
|
||||
br i1 %cmp12, label %loop_next, label %loop_exit_f
|
||||
loop_next:
|
||||
%i = load i64, i64* %iptr
|
||||
%iplus = add i64 %i, 1
|
||||
store i64 %iplus, i64* %iptr
|
||||
br label %loop_entry
|
||||
loop_exit_s:
|
||||
ret i1 1
|
||||
loop_exit_f:
|
||||
ret i1 0
|
||||
}
|
||||
|
||||
define void @swap(i64* %a, i64* %b) {
|
||||
%temp = load i64, i64* %a
|
||||
%temp2 = load i64, i64* %b
|
||||
store i64 %temp2, i64* %a
|
||||
store i64 %temp, i64* %b
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @heapify(%arraystruct* %arr, i64 %N, i64 %i) {
|
||||
%largest_ptr = alloca i64
|
||||
store i64 %i, i64* %largest_ptr
|
||||
|
||||
%l_ptr = alloca i64
|
||||
%heapify_1 = mul i64 %i, 2
|
||||
%heapify_2 = add i64 %heapify_1, 1
|
||||
store i64 %heapify_2, i64* %l_ptr
|
||||
|
||||
%r_ptr = alloca i64
|
||||
%heapify_3 = mul i64 %i, 2
|
||||
%heapify_4 = add i64 %heapify_3, 2
|
||||
store i64 %heapify_4, i64* %r_ptr
|
||||
|
||||
; if (l < N && arr[l] > arr[largest])
|
||||
%l = load i64, i64* %l_ptr
|
||||
%l_cond_1 = icmp slt i64 %l, %N
|
||||
|
||||
%arr_l_ptr = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 %l
|
||||
%largest = load i64, i64* %largest_ptr
|
||||
%arr_largest_ptr = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 %largest
|
||||
%arr_l = load i64, i64* %arr_l_ptr
|
||||
%arr_largest = load i64, i64* %arr_largest_ptr
|
||||
%l_cond_2 = icmp sgt i64 %arr_l, %arr_largest
|
||||
|
||||
%l_cond_res = and i1 %l_cond_1, %l_cond_2
|
||||
|
||||
br i1 %l_cond_res, label %l_update, label %r_cmp
|
||||
|
||||
l_update:
|
||||
store i64 %l, i64* %largest_ptr
|
||||
br label %r_cmp
|
||||
|
||||
r_cmp:
|
||||
; if (r < N && arr[r] > arr[largest])
|
||||
%r = load i64, i64* %r_ptr
|
||||
%r_cond_1 = icmp slt i64 %r, %N
|
||||
%arr_r_ptr = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 %r
|
||||
%largest_2 = load i64, i64* %largest_ptr
|
||||
%heapify_5_ptr = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 %largest_2
|
||||
%arr_r = load i64, i64* %arr_r_ptr
|
||||
%heapify_5 = load i64, i64* %heapify_5_ptr
|
||||
|
||||
%r_cond_2 = icmp sgt i64 %arr_r, %heapify_5
|
||||
%r_cond_res = and i1 %r_cond_1, %r_cond_2
|
||||
|
||||
br i1 %r_cond_res, label %r_update, label %check_for_swap
|
||||
|
||||
r_update:
|
||||
store i64 %r, i64* %largest_ptr
|
||||
br label %check_for_swap
|
||||
|
||||
check_for_swap:
|
||||
; if (largest != i)
|
||||
%largest_swap = load i64, i64* %largest_ptr
|
||||
%swap_cond = icmp ne i64 %largest_swap, %i
|
||||
|
||||
br i1 %swap_cond, label %do_swap, label %exit
|
||||
|
||||
do_swap:
|
||||
%arr_i_ptr = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 %i
|
||||
%largest_3 = load i64, i64* %largest_ptr
|
||||
%arr_largest_ptr_2 = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 %largest_3
|
||||
|
||||
%my_temp = load i64, i64* %arr_i_ptr
|
||||
|
||||
call void @swap(i64* %arr_i_ptr, i64* %arr_largest_ptr_2)
|
||||
call void @heapify(%arraystruct* %arr, i64 %N, i64 %largest_3)
|
||||
br label %exit
|
||||
|
||||
exit:
|
||||
ret void
|
||||
|
||||
}
|
||||
|
||||
define void @heapsort(%arraystruct* %arr, i64 %N) {
|
||||
%1 = alloca i64 ; N
|
||||
store i64 %N, i64* %1
|
||||
|
||||
%2 = lshr i64 %N, 1 ; half
|
||||
%3 = sub i64 %2, 1 ; half - 1
|
||||
|
||||
%fst_loop_ptr = alloca i64
|
||||
store i64 %3, i64* %fst_loop_ptr ; ptr to half - 1
|
||||
|
||||
%4 = sub i64 %N, 1 ; N - 1
|
||||
|
||||
%snd_loop_ptr = alloca i64
|
||||
store i64 %4, i64* %snd_loop_ptr ; ptr to half - 1
|
||||
|
||||
%5 = alloca i64
|
||||
store i64 0, i64* %5 ; zero, for loop comparisons
|
||||
br label %fst_loop_entry
|
||||
|
||||
fst_loop_entry:
|
||||
%loop_cond = load i64, i64* %fst_loop_ptr
|
||||
%five_temp = load i64, i64* %5
|
||||
%cmp_zero = icmp sge i64 %loop_cond, %five_temp
|
||||
br i1 %cmp_zero, label %loop_body, label %snd_loop_entry
|
||||
|
||||
loop_body:
|
||||
call void @heapify(%arraystruct* %arr, i64 %N, i64 %loop_cond)
|
||||
%next_iter = sub i64 %loop_cond, 1
|
||||
store i64 %next_iter, i64* %fst_loop_ptr
|
||||
br label %fst_loop_entry
|
||||
|
||||
snd_loop_entry:
|
||||
%snd_loop_cond = load i64, i64* %snd_loop_ptr
|
||||
%five_temp_two = load i64, i64* %5
|
||||
%cmp_zero_two = icmp sge i64 %snd_loop_cond, %five_temp_two
|
||||
br i1 %cmp_zero_two, label %snd_loop_body, label %snd_loop_exit
|
||||
|
||||
snd_loop_body:
|
||||
%arr_zero_ptr = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 0
|
||||
%arr_n_sub_one_ptr = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 %snd_loop_cond
|
||||
call void @swap(i64* %arr_zero_ptr, i64* %arr_n_sub_one_ptr)
|
||||
%call_five_val = load i64, i64* %5
|
||||
call void @heapify(%arraystruct* %arr, i64 %snd_loop_cond, i64 %call_five_val)
|
||||
|
||||
%snd_next_iter = sub i64 %snd_loop_cond, 1
|
||||
store i64 %snd_next_iter, i64* %snd_loop_ptr
|
||||
br label %snd_loop_entry
|
||||
|
||||
snd_loop_exit:
|
||||
ret void
|
||||
}
|
||||
|
||||
define i1 @main(i64 %argc, i8** %arcv) {
|
||||
call void @heapsort(%arraystruct* @gtest1, i64 9)
|
||||
%res1 = call i1 @eq9(%arraystruct* @gtest1, %arraystruct* @ans1)
|
||||
call void @heapsort(%arraystruct* @gtest2, i64 9)
|
||||
%res2 = call i1 @eq9(%arraystruct* @gtest2, %arraystruct* @ans2)
|
||||
call void @heapsort(%arraystruct* @gtest3, i64 9)
|
||||
%res3 = call i1 @eq9(%arraystruct* @gtest3, %arraystruct* @ans3)
|
||||
call void @heapsort(%arraystruct* @gtest4, i64 9)
|
||||
%res4 = call i1 @eq9(%arraystruct* @gtest4, %arraystruct* @ans4)
|
||||
%resagg1 = and i1 %res1, %res2
|
||||
%resagg2 = and i1 %resagg1, %res3
|
||||
%resagg3 = and i1 %resagg2, %res4
|
||||
|
||||
ret i1 %resagg3
|
||||
}
|
||||
9
hw6/llprograms/sp24_hw3/helloworld.ll
Normal file
9
hw6/llprograms/sp24_hw3/helloworld.ll
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
declare void @ll_puts(i8*)
|
||||
|
||||
@gstr = global [14 x i8] c"hello, world!\00"
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%1 = getelementptr [14 x i8], [14 x i8]* @gstr, i32 0, i32 0
|
||||
call void @ll_puts(i8* %1)
|
||||
ret i64 0
|
||||
}
|
||||
59
hw6/llprograms/sp24_hw3/horner.ll
Normal file
59
hw6/llprograms/sp24_hw3/horner.ll
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
%arr_t = type [4 x i64]
|
||||
|
||||
; coeff of the polynomial
|
||||
@coeff_arr = global %arr_t [i64 2, i64 -6, i64 2, i64 -1]
|
||||
|
||||
define i64 @horner(%arr_t* %poly, i64 %n, i64 %x)
|
||||
{
|
||||
|
||||
; make a slot for the result, and set to 0
|
||||
%result_p = alloca i64
|
||||
store i64 0, i64* %result_p
|
||||
|
||||
; make a slot for the loop index, and set to 0
|
||||
%i_p = alloca i64
|
||||
store i64 0, i64* %i_p
|
||||
|
||||
|
||||
; enter the loop
|
||||
br label %loop_bound
|
||||
|
||||
loop_bound:
|
||||
; check that the index < n
|
||||
%check_i = load i64, i64* %i_p
|
||||
%bound_check = icmp slt i64 %check_i, %n
|
||||
br i1 %bound_check, label %calc, label %horner_done
|
||||
|
||||
calc:
|
||||
|
||||
; get poly[i]
|
||||
%use_i = load i64, i64* %i_p
|
||||
%poly_i_p = getelementptr %arr_t, %arr_t* %poly, i64 0, i64 %use_i
|
||||
%poly_i = load i64, i64* %poly_i_p
|
||||
|
||||
; next result = result*x + poly[i]
|
||||
%current_result = load i64, i64* %result_p
|
||||
%temp_0 = mul i64 %current_result, %x
|
||||
%next_result = add i64 %temp_0, %poly_i
|
||||
store i64 %next_result, i64* %result_p
|
||||
|
||||
; i += 1, and loop back
|
||||
%new_i = add i64 %use_i, 1
|
||||
store i64 %new_i, i64* %i_p
|
||||
br label %loop_bound
|
||||
|
||||
horner_done:
|
||||
; return the final result
|
||||
%final_result = load i64, i64* %result_p
|
||||
ret i64 %final_result
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv){
|
||||
; uses horners method to evaluate a polynomial of degree n
|
||||
; at x using only n multiplications .
|
||||
|
||||
; Here we are evaluating the polynomial 2x^3 -6x^2 + 2x - 1 at 3
|
||||
; and we expect to get -5.
|
||||
%main_result = call i64 @horner(%arr_t* @coeff_arr, i64 4, i64 3)
|
||||
ret i64 %main_result
|
||||
}
|
||||
80
hw6/llprograms/sp24_hw3/insertion_sort.ll
Normal file
80
hw6/llprograms/sp24_hw3/insertion_sort.ll
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
%array = type [10 x i64]
|
||||
|
||||
@arr = global %array [ i64 10, i64 2, i64 7, i64 4, i64 5, i64 1, i64 9, i64 3, i64 6, i64 8 ]
|
||||
|
||||
@sorted_arr = global %array [ i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9, i64 10]
|
||||
|
||||
define i64 @check_sorted(%array* %expected, %array* %actual) {
|
||||
%idx_ptr = alloca i64
|
||||
store i64 0, i64* %idx_ptr
|
||||
br label %iterate
|
||||
iterate:
|
||||
%idx = load i64, i64* %idx_ptr
|
||||
%cmp4 = icmp slt i64 %idx, 10
|
||||
br i1 %cmp4, label %check_arrs, label %end_sorted
|
||||
check_arrs:
|
||||
%exp_ptr = getelementptr %array, %array* %expected, i32 0, i64 %idx
|
||||
%act_ptr = getelementptr %array, %array* %actual, i32 0, i64 %idx
|
||||
%exp_val = load i64, i64* %exp_ptr
|
||||
%act_val = load i64, i64* %act_ptr
|
||||
|
||||
%a = add i64 %idx, 1
|
||||
store i64 %a, i64* %idx_ptr
|
||||
|
||||
%cmp5 = icmp eq i64 %exp_val, %act_val
|
||||
br i1 %cmp5, label %iterate, label %end_not_sorted
|
||||
end_not_sorted:
|
||||
ret i64 0
|
||||
end_sorted:
|
||||
ret i64 1
|
||||
}
|
||||
|
||||
define void @insertion_sort(%array* %arr, i64 %len) {
|
||||
%i = alloca i64
|
||||
store i64 1, i64* %i
|
||||
%j = alloca i64
|
||||
br label %loop
|
||||
loop:
|
||||
%i_val = load i64, i64* %i
|
||||
%cmp1 = icmp eq i64 %i_val, %len
|
||||
br i1 %cmp1, label %end, label %loop_body
|
||||
loop_body:
|
||||
%key = getelementptr %array, %array* %arr, i32 0, i64 %i_val
|
||||
%key_val = load i64, i64* %key
|
||||
%j_val = sub i64 %i_val, 1
|
||||
store i64 %j_val, i64* %j
|
||||
br label %inner_loop_check
|
||||
inner_loop_check:
|
||||
%j_val = load i64, i64* %j
|
||||
%cmp2 = icmp sge i64 %j_val, 0
|
||||
br i1 %cmp2, label %inner_loop_check2, label %increment
|
||||
inner_loop_check2:
|
||||
%arr_j = getelementptr %array, %array* %arr, i32 0, i64 %j_val
|
||||
%arr_j_val = load i64, i64* %arr_j
|
||||
%cmp3 = icmp slt i64 %key_val, %arr_j_val
|
||||
br i1 %cmp3, label %inner_loop, label %increment
|
||||
inner_loop:
|
||||
%next_j = add i64 %j_val, 1
|
||||
%next_arr_j = getelementptr %array, %array* %arr, i32 0, i64 %next_j
|
||||
store i64 %arr_j_val, i64* %next_arr_j
|
||||
|
||||
%temp_j = sub i64 %j_val, 1
|
||||
store i64 %temp_j, i64* %j
|
||||
br label %inner_loop_check
|
||||
increment:
|
||||
%next_j = add i64 %j_val, 1
|
||||
%next_arr_j = getelementptr %array, %array* %arr, i32 0, i64 %next_j
|
||||
store i64 %key_val, i64* %next_arr_j
|
||||
|
||||
%temp_i = add i64 1, %i_val
|
||||
store i64 %temp_i, i64* %i
|
||||
br label %loop
|
||||
end:
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
call void @insertion_sort(%array* @arr, i64 10)
|
||||
%1 = call i64 @check_sorted(%array* @sorted_arr, %array* @arr)
|
||||
ret i64 %1
|
||||
}
|
||||
92
hw6/llprograms/sp24_hw3/is_same_tree.ll
Normal file
92
hw6/llprograms/sp24_hw3/is_same_tree.ll
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
; is same tree leetcode problem
|
||||
; sample Java code
|
||||
; class Solution {
|
||||
; public boolean isSameTree(TreeNode p, TreeNode q) {
|
||||
; return dfs(p, q);
|
||||
; }
|
||||
|
||||
; private boolean dfs(TreeNode p, TreeNode q) {
|
||||
; if (p == null && q == null) {
|
||||
; return true;
|
||||
; }
|
||||
|
||||
; if (p == null || q == null) {
|
||||
; return false;
|
||||
; }
|
||||
|
||||
; if (p.val != q.val) return false;
|
||||
|
||||
; boolean left = dfs(p.left, q.left);
|
||||
; boolean right = dfs(p.right, q.right);
|
||||
|
||||
; return left && right;
|
||||
; }
|
||||
; }
|
||||
|
||||
; val, left child, right child
|
||||
%TreeNode = type { %TreeNode*, %TreeNode*, i64 }
|
||||
|
||||
; test with root1 = [1,2,3], root2 = [1,2,3], type
|
||||
@n2 = global %TreeNode { %TreeNode* null, %TreeNode* null, i64 2 }
|
||||
@n3 = global %TreeNode { %TreeNode* null, %TreeNode* null, i64 3 }
|
||||
@root1 = global %TreeNode { %TreeNode* @n2, %TreeNode* @n3, i64 1 }
|
||||
|
||||
@n2_2 = global %TreeNode { %TreeNode* null, %TreeNode* null, i64 2 }
|
||||
@n3_2 = global %TreeNode { %TreeNode* null, %TreeNode* null, i64 3 }
|
||||
@root2 = global %TreeNode { %TreeNode* @n2_2, %TreeNode* @n3_2, i64 1 }
|
||||
|
||||
; root1 = [1,2,3], root3 = [1,null,3] false, modify input & expected to test
|
||||
@n3_3 = global %TreeNode { %TreeNode* null, %TreeNode* null, i64 3 }
|
||||
@root3 = global %TreeNode { %TreeNode* null, %TreeNode* @n3_3, i64 1 }
|
||||
|
||||
define i1 @is_same_tree(%TreeNode* %r1, %TreeNode* %r2) {
|
||||
entry:
|
||||
%r1_check = icmp eq %TreeNode* %r1, null
|
||||
%r2_check = icmp eq %TreeNode* %r2, null
|
||||
; check both leaves
|
||||
%both_true = and i1 %r1_check, %r2_check
|
||||
%both_cond = icmp eq i1 1, %both_true
|
||||
br i1 %both_cond, label %ret_true, label %one_null
|
||||
one_null:
|
||||
%one_true = or i1 %r1_check, %r2_check
|
||||
%one_cond = icmp eq i1 1, %one_true
|
||||
br i1 %one_cond, label %ret_false, label %no_null
|
||||
no_null:
|
||||
; compare value
|
||||
%val1_ptr= getelementptr %TreeNode, %TreeNode* %r1, i64 0, i32 2
|
||||
%val1 = load i64, i64* %val1_ptr
|
||||
|
||||
%val2_ptr = getelementptr %TreeNode, %TreeNode* %r2, i64 0, i32 2
|
||||
%val2 = load i64, i64* %val2_ptr
|
||||
|
||||
%same_val = icmp eq i64 %val1, %val2
|
||||
br i1 %same_val, label %equal_val, label %ret_false
|
||||
equal_val:
|
||||
; get both children from both roots
|
||||
%left1_p = getelementptr %TreeNode, %TreeNode* %r1, i64 0, i32 0
|
||||
%left1 = load %TreeNode*, %TreeNode** %left1_p
|
||||
%right1_p = getelementptr %TreeNode, %TreeNode* %r1, i64 0, i32 1
|
||||
%right1 = load %TreeNode*, %TreeNode** %right1_p
|
||||
|
||||
%left2_p = getelementptr %TreeNode, %TreeNode* %r2, i64 0, i32 0
|
||||
%left2 = load %TreeNode*, %TreeNode** %left2_p
|
||||
%right2_p = getelementptr %TreeNode, %TreeNode* %r2, i64 0, i32 1
|
||||
%right2 = load %TreeNode*, %TreeNode** %right2_p
|
||||
|
||||
; left subtree dfs
|
||||
%left_sub = call i1 @is_same_tree(%TreeNode* %left1, %TreeNode* %left2)
|
||||
; right subtree dfs
|
||||
%right_sub = call i1 @is_same_tree(%TreeNode* %right1, %TreeNode* %right2)
|
||||
; return left && right
|
||||
%subs_same = and i1 %left_sub, %right_sub
|
||||
br i1 %subs_same, label %ret_true, label %ret_false
|
||||
ret_true:
|
||||
ret i1 1
|
||||
ret_false:
|
||||
ret i1 0
|
||||
}
|
||||
|
||||
define i1 @main(i64 %argc, i8** %argv) {
|
||||
%res = call i1 @is_same_tree(%TreeNode* @root1, %TreeNode* @root2)
|
||||
ret i1 %res
|
||||
}
|
||||
124
hw6/llprograms/sp24_hw3/knapsack.ll
Normal file
124
hw6/llprograms/sp24_hw3/knapsack.ll
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
; 0/1 Knapsack Problem (Recursive solution)
|
||||
; * Input: array of weights, array of values, and a capacity of a knapsack
|
||||
; * Output: the maximum value that can be obtained by selecting items,
|
||||
; while keeping the total weight within the capacity of the knapsack.
|
||||
|
||||
; operates on a struct of an array
|
||||
; uses recursion
|
||||
; one helper function with use of call instruction
|
||||
|
||||
%arraystruct = type { [5 x i64] }
|
||||
|
||||
; Test 1: Basic functionality / correctness
|
||||
|
||||
@weights1 = global %arraystruct {[5 x i64] [i64 2, i64 3, i64 4, i64 5, i64 6]}
|
||||
@values1 = global %arraystruct {[5 x i64] [i64 3, i64 4, i64 5, i64 6, i64 7]}
|
||||
@capacity1 = global i64 5
|
||||
|
||||
@expected1 = global i64 7
|
||||
|
||||
; Test 2: Balance between maximizing the value and staying within the capacity limit
|
||||
|
||||
@weights2 = global %arraystruct {[5 x i64] [i64 1, i64 2, i64 3, i64 4, i64 5]}
|
||||
@values2 = global %arraystruct {[5 x i64] [i64 1, i64 3, i64 4, i64 5, i64 6]}
|
||||
@capacity2 = global i64 8
|
||||
|
||||
@expected2 = global i64 10
|
||||
|
||||
; Test 3: Lower weights have higher values
|
||||
|
||||
@weights3 = global %arraystruct {[5 x i64] [i64 5, i64 4, i64 3, i64 2, i64 1]}
|
||||
@values3 = global %arraystruct {[5 x i64] [i64 6, i64 5, i64 4, i64 3, i64 2]}
|
||||
@capacity3 = global i64 10
|
||||
|
||||
@expected3 = global i64 14
|
||||
|
||||
|
||||
define i64 @knapsack(i64 %n, %arraystruct* %weights, %arraystruct* %values, i64 %capacity) {
|
||||
; %n_minus_1 = sub i64 %n, 1
|
||||
; %1 = getelementptr %arraystruct, %arraystruct* %weights, i32 0, i32 0, i64 %n_minus_1
|
||||
; %2 = load i64, i64* %1
|
||||
; ret i64 %2
|
||||
|
||||
%is_n_0 = icmp eq i64 %n, 0
|
||||
%is_n_1 = icmp eq i64 %capacity, 0
|
||||
%base_case = or i1 %is_n_0, %is_n_1
|
||||
br i1 %base_case, label %base_case_end, label %continue1
|
||||
|
||||
base_case_end:
|
||||
ret i64 0
|
||||
|
||||
continue1:
|
||||
%n_minus_1 = sub i64 %n, 1
|
||||
%w_n_minus_1_ptr = getelementptr %arraystruct, %arraystruct* %weights, i32 0, i32 0, i64 %n_minus_1
|
||||
%w_n_minus_1 = load i64, i64* %w_n_minus_1_ptr
|
||||
|
||||
%2 = icmp sgt i64 %w_n_minus_1, %capacity
|
||||
br i1 %2, label %recursive_case1, label %continue2
|
||||
|
||||
recursive_case1:
|
||||
%knapsack_with_n_minus_1 = call i64 @knapsack(i64 %n_minus_1, %arraystruct* %weights, %arraystruct* %values, i64 %capacity)
|
||||
ret i64 %knapsack_with_n_minus_1
|
||||
|
||||
continue2:
|
||||
|
||||
%val_at_n_minus_1_ptr = getelementptr %arraystruct, %arraystruct* %values, i32 0, i32 0, i64 %n_minus_1
|
||||
%val_at_n_minus_1 = load i64, i64* %val_at_n_minus_1_ptr
|
||||
|
||||
%new_capacity = sub i64 %capacity, %w_n_minus_1
|
||||
%knapsack_with_n_minus_1_new_capacity = call i64 @knapsack(i64 %n_minus_1, %arraystruct* %weights, %arraystruct* %values, i64 %new_capacity)
|
||||
%left_for_max = add i64 %val_at_n_minus_1, %knapsack_with_n_minus_1_new_capacity
|
||||
%right_for_max = call i64 @knapsack(i64 %n_minus_1, %arraystruct* %weights, %arraystruct* %values, i64 %capacity)
|
||||
|
||||
|
||||
%max = icmp sgt i64 %left_for_max, %right_for_max
|
||||
br i1 %max, label %include_item, label %dont_include_item
|
||||
|
||||
include_item:
|
||||
ret i64 %left_for_max
|
||||
|
||||
dont_include_item:
|
||||
ret i64 %right_for_max
|
||||
}
|
||||
|
||||
define void @main(i64 %argc, i8** %arcv) {
|
||||
; get result of Test 1
|
||||
%ptr_weights1 = getelementptr %arraystruct, %arraystruct* @weights1, i32 0
|
||||
%ptr_values1 = getelementptr %arraystruct, %arraystruct* @values1, i32 0
|
||||
%capacity1_val = load i64, i64* @capacity1
|
||||
%res1 = call i64 @knapsack(i64 5, %arraystruct* %ptr_weights1, %arraystruct* %ptr_values1, i64 %capacity1_val)
|
||||
|
||||
; whether Test 1 passed or not (i1)
|
||||
%expected1_val = load i64, i64* @expected1
|
||||
%res1_pass = icmp eq i64 %res1, %expected1_val
|
||||
|
||||
|
||||
; get result of Test 2
|
||||
%ptr_weights2 = getelementptr %arraystruct, %arraystruct* @weights2, i32 0
|
||||
%ptr_values2 = getelementptr %arraystruct, %arraystruct* @values2, i32 0
|
||||
%capacity2_val = load i64, i64* @capacity2
|
||||
%res2 = call i64 @knapsack(i64 5, %arraystruct* %ptr_weights2, %arraystruct* %ptr_values2, i64 %capacity2_val)
|
||||
|
||||
; whether Test 2 passed or not (i1)
|
||||
%expected2_val = load i64, i64* @expected2
|
||||
%res2_pass = icmp eq i64 %res2, %expected2_val
|
||||
|
||||
|
||||
|
||||
; get result of Test 3
|
||||
%ptr_weights3 = getelementptr %arraystruct, %arraystruct* @weights3, i32 0
|
||||
%ptr_values3 = getelementptr %arraystruct, %arraystruct* @values3, i32 0
|
||||
%capacity3_val = load i64, i64* @capacity3
|
||||
%res3 = call i64 @knapsack(i64 5, %arraystruct* %ptr_weights3, %arraystruct* %ptr_values3, i64 %capacity3_val)
|
||||
|
||||
; whether Test 3 passed or not (i1)
|
||||
%expected3_val = load i64, i64* @expected3
|
||||
%res3_pass = icmp eq i64 %res3, %expected3_val
|
||||
|
||||
|
||||
|
||||
; return 1 if all tests passed
|
||||
%res1_and_res2_pass = and i1 %res1_pass, %res2_pass
|
||||
%all_pass = and i1 %res1_and_res2_pass, %res3_pass
|
||||
ret i64 %all_pass
|
||||
}
|
||||
118
hw6/llprograms/sp24_hw3/linkedlist_cycle.ll
Normal file
118
hw6/llprograms/sp24_hw3/linkedlist_cycle.ll
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
; Eric Zhao (PennKey zhaoer), Nathaniel Lao (PennKey nlao)
|
||||
|
||||
%node = type { i64, %node* }
|
||||
|
||||
define i1 @find_cycle_rec_helper(%node* %slow, %node* %fast) {
|
||||
%fast_is_null = icmp eq %node* %fast, null
|
||||
br i1 %fast_is_null, label %no_cycle_found, label %incr_fast
|
||||
|
||||
incr_fast:
|
||||
%fast_next_ptr = getelementptr %node, %node* %fast, i32 0, i32 1
|
||||
%fast_next = load %node*, %node** %fast_next_ptr
|
||||
%fast_next_is_null = icmp eq %node* %fast_next, null
|
||||
br i1 %fast_next_is_null, label %no_cycle_found, label %find_nexts
|
||||
|
||||
find_nexts:
|
||||
%fast_next_next_ptr = getelementptr %node, %node* %fast_next, i32 0, i32 1
|
||||
%fast_next_next = load %node*, %node** %fast_next_next_ptr
|
||||
%slow_next_ptr = getelementptr %node, %node* %slow, i32 0, i32 1
|
||||
%slow_next = load %node*, %node** %slow_next_ptr
|
||||
%pointers_equal = icmp eq %node* %fast_next_next, %slow_next
|
||||
br i1 %pointers_equal, label %cycle_found, label %recursive_call
|
||||
|
||||
recursive_call:
|
||||
%result = call i1 @find_cycle_rec_helper(%node* %slow_next, %node* %fast_next_next)
|
||||
ret i1 %result
|
||||
|
||||
cycle_found:
|
||||
ret i1 1
|
||||
no_cycle_found:
|
||||
ret i1 0
|
||||
}
|
||||
|
||||
define i1 @find_cycle(%node* %start) {
|
||||
%result = call i1 @find_cycle_rec_helper(%node* %start, %node* %start)
|
||||
ret i1 %result
|
||||
}
|
||||
|
||||
define i1 @main(i64 %argc, i8** %argv) {
|
||||
%1 = alloca %node
|
||||
%2 = alloca %node
|
||||
%3 = alloca %node
|
||||
%4 = alloca %node
|
||||
%5 = alloca %node
|
||||
%next_ptr_1 = getelementptr %node, %node* %1, i32 0, i32 1
|
||||
%next_ptr_2 = getelementptr %node, %node* %2, i32 0, i32 1
|
||||
%next_ptr_3 = getelementptr %node, %node* %3, i32 0, i32 1
|
||||
%next_ptr_4 = getelementptr %node, %node* %4, i32 0, i32 1
|
||||
%next_ptr_5 = getelementptr %node, %node* %5, i32 0, i32 1
|
||||
br label %empty_list
|
||||
|
||||
empty_list:
|
||||
%empty_list_result = call i1 @find_cycle(%node* null)
|
||||
br i1 %empty_list_result, label %failure, label %singleton
|
||||
|
||||
singleton:
|
||||
store %node* null, %node** %next_ptr_1
|
||||
%singleton_result = call i1 @find_cycle(%node* %1)
|
||||
br i1 %singleton_result, label %failure, label %self_loop
|
||||
|
||||
self_loop:
|
||||
store %node* %1, %node** %next_ptr_1
|
||||
%self_loop_result = call i1 @find_cycle(%node* %1)
|
||||
br i1 %self_loop_result, label %long_no_cycle_odd, label %failure
|
||||
|
||||
long_no_cycle_odd:
|
||||
store %node* %2, %node** %next_ptr_1
|
||||
store %node* %3, %node** %next_ptr_2
|
||||
store %node* %4, %node** %next_ptr_3
|
||||
store %node* %5, %node** %next_ptr_4
|
||||
store %node* null, %node** %next_ptr_5
|
||||
%long_no_cycle_odd_result = call i1 @find_cycle(%node* %1)
|
||||
br i1 %long_no_cycle_odd_result, label %failure, label %two_cycle
|
||||
|
||||
two_cycle:
|
||||
store %node* %2, %node** %next_ptr_1
|
||||
store %node* %1, %node** %next_ptr_2
|
||||
%two_cycle_result = call i1 @find_cycle(%node* %1)
|
||||
br i1 %two_cycle_result, label %long_no_cycle_even, label %failure
|
||||
|
||||
long_no_cycle_even:
|
||||
store %node* %2, %node** %next_ptr_1
|
||||
store %node* %3, %node** %next_ptr_2
|
||||
store %node* %4, %node** %next_ptr_3
|
||||
store %node* null, %node** %next_ptr_4
|
||||
%long_no_cycle_even_result = call i1 @find_cycle(%node* %1)
|
||||
br i1 %long_no_cycle_even_result, label %failure, label %long_even_cycle
|
||||
|
||||
long_even_cycle:
|
||||
store %node* %2, %node** %next_ptr_1
|
||||
store %node* %3, %node** %next_ptr_2
|
||||
store %node* %4, %node** %next_ptr_3
|
||||
store %node* %1, %node** %next_ptr_4
|
||||
%long_even_cycle_result = call i1 @find_cycle(%node* %1)
|
||||
br i1 %long_even_cycle_result, label %long_odd_cycle, label %failure
|
||||
|
||||
long_odd_cycle:
|
||||
store %node* %2, %node** %next_ptr_1
|
||||
store %node* %3, %node** %next_ptr_2
|
||||
store %node* %4, %node** %next_ptr_3
|
||||
store %node* %5, %node** %next_ptr_4
|
||||
store %node* %1, %node** %next_ptr_5
|
||||
%long_odd_cycle_result = call i1 @find_cycle(%node* %1)
|
||||
br i1 %long_odd_cycle_result, label %cycle_in_middle, label %failure
|
||||
|
||||
cycle_in_middle:
|
||||
store %node* %2, %node** %next_ptr_1
|
||||
store %node* %3, %node** %next_ptr_2
|
||||
store %node* %4, %node** %next_ptr_3
|
||||
store %node* %5, %node** %next_ptr_4
|
||||
store %node* %3, %node** %next_ptr_5
|
||||
%cycle_in_middle_result = call i1 @find_cycle(%node* %1)
|
||||
br i1 %cycle_in_middle_result, label %success, label %failure
|
||||
|
||||
success:
|
||||
ret i1 0
|
||||
failure:
|
||||
ret i1 1
|
||||
}
|
||||
121
hw6/llprograms/sp24_hw3/longest_incr_subseq.ll
Normal file
121
hw6/llprograms/sp24_hw3/longest_incr_subseq.ll
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
@test_case = global [8 x i64] [i64 10, i64 9, i64 2, i64 5, i64 3, i64 7, i64 101, i64 18]
|
||||
@memo = global [8 x i64] [i64 -1, i64 -1, i64 -1, i64 -1, i64 -1, i64 -1, i64 -1, i64 -1]
|
||||
|
||||
define i64 @max_in_memo() {
|
||||
%max = alloca i64
|
||||
store i64 -1, i64* %max
|
||||
|
||||
; loop variable, we can skip index 0 because it's the base case
|
||||
%i = alloca i64
|
||||
store i64 0, i64* %i
|
||||
|
||||
br label %loop
|
||||
|
||||
loop:
|
||||
%i_val = load i64, i64* %i
|
||||
%cmp1 = icmp eq i64 %i_val, 8
|
||||
|
||||
br i1 %cmp1, label %after_loop, label %loop_body
|
||||
|
||||
loop_body:
|
||||
%memo_val_ptr = getelementptr [8 x i64], [8 x i64]* @memo, i32 0, i64 %i_val
|
||||
%memo_val = load i64, i64* %memo_val_ptr
|
||||
%max_val1 = load i64, i64* %max
|
||||
%cmp2 = icmp sgt i64 %memo_val, %max_val1
|
||||
|
||||
br i1 %cmp2, label %new_max, label %repeat_loop
|
||||
|
||||
new_max:
|
||||
store i64 %memo_val, i64* %max
|
||||
|
||||
br label %repeat_loop
|
||||
|
||||
repeat_loop:
|
||||
%new_i_val = add i64 %i_val, 1
|
||||
store i64 %new_i_val, i64* %i
|
||||
|
||||
br label %loop
|
||||
|
||||
after_loop:
|
||||
%max_val2 = load i64, i64* %max
|
||||
|
||||
ret i64 %max_val2
|
||||
}
|
||||
|
||||
define i64 @lis(i64 %idx) {
|
||||
%max = alloca i64
|
||||
store i64 1, i64* %max
|
||||
|
||||
; loop variable
|
||||
%i = alloca i64
|
||||
store i64 0, i64* %i
|
||||
|
||||
br label %loop
|
||||
|
||||
loop:
|
||||
%i_val = load i64, i64* %i
|
||||
%cmp1 = icmp eq i64 %i_val, %idx
|
||||
|
||||
br i1 %cmp1, label %after_loop, label %loop_body
|
||||
|
||||
loop_body:
|
||||
%memo_val_ptr = getelementptr [8 x i64], [8 x i64]* @memo, i32 0, i64 %i_val
|
||||
%memo_val1 = load i64, i64* %memo_val_ptr
|
||||
|
||||
; check if we've previously memoized the result for this subproblem
|
||||
%cmp2 = icmp eq i64 %memo_val1, -1
|
||||
|
||||
br i1 %cmp2, label %recurse, label %after_memo
|
||||
|
||||
recurse:
|
||||
%res = call i64 @lis(i64 %i_val)
|
||||
store i64 %res, i64* %memo_val_ptr
|
||||
|
||||
br label %after_memo
|
||||
|
||||
after_memo:
|
||||
%arr_i_ptr = getelementptr [8 x i64], [8 x i64]* @test_case, i32 0, i64 %i_val
|
||||
%arr_i_val = load i64, i64* %arr_i_ptr
|
||||
|
||||
%arr_idx_ptr = getelementptr [8 x i64], [8 x i64]* @test_case, i32 0, i64 %idx
|
||||
%arr_idx_val = load i64, i64* %arr_idx_ptr
|
||||
|
||||
%cmp3 = icmp slt i64 %arr_i_val, %arr_idx_val
|
||||
br i1 %cmp3, label %can_incr_subseq, label %repeat_loop
|
||||
|
||||
can_incr_subseq:
|
||||
%max_val1 = load i64, i64* %max
|
||||
|
||||
; index again because we could've recursed and updated subproblem value
|
||||
%memo_val2 = load i64, i64* %memo_val_ptr
|
||||
%curr_max = add i64 %memo_val2, 1
|
||||
|
||||
%cmp4 = icmp sgt i64 %curr_max, %max_val1
|
||||
|
||||
br i1 %cmp4, label %new_max_found, label %repeat_loop
|
||||
|
||||
new_max_found:
|
||||
store i64 %curr_max, i64* %max
|
||||
|
||||
br label %repeat_loop
|
||||
|
||||
repeat_loop:
|
||||
%new_i_val = add i64 %i_val, 1
|
||||
store i64 %new_i_val, i64* %i
|
||||
|
||||
br label %loop
|
||||
|
||||
after_loop:
|
||||
%memo_idx_ptr = getelementptr [8 x i64], [8 x i64]* @memo, i32 0, i64 %idx
|
||||
%max_val2 = load i64, i64* %max
|
||||
store i64 %max_val2, i64* %memo_idx_ptr
|
||||
|
||||
ret i64 %max_val2
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%res = call i64 @lis(i64 7)
|
||||
%ans = call i64 @max_in_memo()
|
||||
|
||||
ret i64 %ans
|
||||
}
|
||||
91
hw6/llprograms/sp24_hw3/longest_sub_array_having_sum_k.ll
Normal file
91
hw6/llprograms/sp24_hw3/longest_sub_array_having_sum_k.ll
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
; Copied https://www.geeksforgeeks.org/longest-sub-array-sum-k/
|
||||
|
||||
%array = type [10 x i64]
|
||||
|
||||
@arr = global %array [ i64 10, i64 2, i64 7, i64 4, i64 1, i64 2, i64 3, i64 1, i64 1, i64 4 ]
|
||||
@arr_len = global i64 10
|
||||
@my_k = global i64 12
|
||||
|
||||
define i64 @lenOfLongSubarr(i64* %arr, i64 %N, i64 %K) {
|
||||
|
||||
entry:
|
||||
; maxlength = 0
|
||||
%maxlength = alloca i64
|
||||
store i64 0, i64* %maxlength
|
||||
%i = alloca i64
|
||||
; Initialize i = 0
|
||||
store i64 0, i64* %i
|
||||
br label %outer_loop
|
||||
|
||||
outer_loop:
|
||||
%i_val = load i64, i64* %i
|
||||
; is i < N?
|
||||
%cmp1 = icmp slt i64 %i_val, %N
|
||||
; if yes, move into inner loop ; else, end outer loop
|
||||
br i1 %cmp1, label %inner_loop_entry, label %outer_loop_end
|
||||
|
||||
inner_loop_entry:
|
||||
%Sum = alloca i64
|
||||
; Sum = 0
|
||||
store i64 0, i64* %Sum
|
||||
%j = alloca i64
|
||||
; Initialize j = i
|
||||
store i64 %i_val, i64* %j
|
||||
br label %inner_loop
|
||||
|
||||
inner_loop:
|
||||
%j_val = load i64, i64* %j
|
||||
; Is j < N?
|
||||
%cmp2 = icmp slt i64 %j_val, %N
|
||||
; If it is, jump to loop body; else, end inner loop
|
||||
br i1 %cmp2, label %loop_body, label %inner_loop_end
|
||||
|
||||
loop_body:
|
||||
%Sum_val = load i64, i64* %Sum
|
||||
; get arr[j]
|
||||
%arr_elem = getelementptr i64, i64* %arr, i64 %j_val
|
||||
%arr_val = load i64, i64* %arr_elem
|
||||
%new_Sum = add i64 %Sum_val, %arr_val
|
||||
; Sum += arr[j]
|
||||
store i64 %new_Sum, i64* %Sum
|
||||
%Sum_eq_K = icmp eq i64 %new_Sum, %K
|
||||
br i1 %Sum_eq_K, label %update_maxlength, label %inner_loop_cont
|
||||
|
||||
update_maxlength:
|
||||
%maxlength_val = load i64, i64* %maxlength
|
||||
%length = sub i64 %j_val, %i_val
|
||||
%new_length = add i64 %length, 1
|
||||
%is_longer = icmp sgt i64 %new_length, %maxlength_val
|
||||
br i1 %is_longer, label %store_maxlength, label %inner_loop_cont
|
||||
|
||||
store_maxlength:
|
||||
store i64 %new_length, i64* %maxlength
|
||||
br label %inner_loop_cont
|
||||
|
||||
inner_loop_cont:
|
||||
%j_inc = add i64 %j_val, 1
|
||||
; j = j + 1
|
||||
store i64 %j_inc, i64* %j
|
||||
br label %inner_loop
|
||||
|
||||
inner_loop_end:
|
||||
%i_inc = add i64 %i_val, 1
|
||||
; i = i + 1
|
||||
store i64 %i_inc, i64* %i
|
||||
br label %outer_loop
|
||||
|
||||
outer_loop_end:
|
||||
%maxlength_ret = load i64, i64* %maxlength
|
||||
ret i64 %maxlength_ret
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%size_ptr = getelementptr i64, i64* @arr_len, i64 0
|
||||
%arr_size = load i64, i64* %size_ptr
|
||||
|
||||
%my_k_ptr = getelementptr i64, i64* @my_k, i64 0
|
||||
%my_k_val = load i64, i64* %my_k_ptr
|
||||
|
||||
%my_res = call i64 @lenOfLongSubarr(%array* @arr, i64 %arr_size, i64 %my_k_val)
|
||||
ret i64 %my_res
|
||||
}
|
||||
109
hw6/llprograms/sp24_hw3/majority_element.ll
Normal file
109
hw6/llprograms/sp24_hw3/majority_element.ll
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
@test1 = global [10 x i64] [ i64 5, i64 1, i64 5, i64 2, i64 3, i64 4, i64 5, i64 5, i64 5, i64 5 ]
|
||||
@test2 = global [10 x i64] [ i64 2, i64 1, i64 2, i64 2, i64 2, i64 4, i64 2, i64 5, i64 5, i64 2 ]
|
||||
@test3 = global [3 x i64] [ i64 3, i64 4, i64 3 ]
|
||||
@test4 = global [1 x i64] [ i64 3 ]
|
||||
@test5 = global [6 x i64] [ i64 -1, i64 -1, i64 3, i64 5, i64 -1, i64 -1 ]
|
||||
@test6 = global [7 x i64] [ i64 2, i64 2, i64 1, i64 1, i64 1, i64 2, i64 2 ]
|
||||
|
||||
define i64 @at([0 x i64]* %arr, i64 %idx) {
|
||||
%address = getelementptr [0 x i64], [0 x i64]* %arr, i32 0, i64 %idx
|
||||
%val = load i64, i64* %address
|
||||
ret i64 %val
|
||||
}
|
||||
|
||||
define i64 @majority([0 x i64]* %arr, i64 %l, i64 %r) {
|
||||
%cmp1 = icmp eq i64 %l, %r
|
||||
br i1 %cmp1, label %base, label %label1
|
||||
label1:
|
||||
%tmp1 = add i64 %l, %r
|
||||
%midpoint = ashr i64 %tmp1, 1
|
||||
%mp1 = add i64 1, %midpoint
|
||||
%recleft = call i64 @majority([0 x i64]* %arr, i64 %l, i64 %midpoint)
|
||||
%recright = call i64 @majority([0 x i64]* %arr, i64 %mp1, i64 %r)
|
||||
br label %recresults
|
||||
recresults:
|
||||
%cmp2 = icmp eq i64 %recleft, %recright
|
||||
br i1 %cmp2, label %retleft, label %label2
|
||||
label2:
|
||||
%leftcntptr = alloca i64
|
||||
%rightcntptr = alloca i64
|
||||
%loopvarptr = alloca i64
|
||||
store i64 0, i64* %leftcntptr
|
||||
store i64 0, i64* %rightcntptr
|
||||
store i64 %l, i64* %loopvarptr
|
||||
br label %loopconditions
|
||||
loopconditions:
|
||||
%loopvar = load i64, i64* %loopvarptr
|
||||
%cmploop = icmp sgt i64 %loopvar, %r
|
||||
br i1 %cmploop, label %loopend, label %loopbody
|
||||
loopbody:
|
||||
%currval = call i64 @at([0 x i64]* %arr, i64 %loopvar)
|
||||
br label %left
|
||||
left:
|
||||
%cmpleft = icmp eq i64 %currval, %recleft
|
||||
br i1 %cmpleft, label %incrleft, label %right
|
||||
incrleft:
|
||||
%leftcnt = load i64, i64* %leftcntptr
|
||||
%leftcntplus = add i64 1, %leftcnt
|
||||
store i64 %leftcntplus, i64* %leftcntptr
|
||||
br label %right
|
||||
right:
|
||||
%cmpright = icmp eq i64 %currval, %recright
|
||||
br i1 %cmpright, label %incrright, label %loopincr
|
||||
incrright:
|
||||
%rightcnt = load i64, i64* %rightcntptr
|
||||
%rightcntplus = add i64 1, %rightcnt
|
||||
store i64 %rightcntplus, i64* %rightcntptr
|
||||
br label %loopincr
|
||||
loopincr:
|
||||
%loopvarforincr = load i64, i64* %loopvarptr
|
||||
%loopvarincr = add i64 1, %loopvarforincr
|
||||
store i64 %loopvarincr, i64* %loopvarptr
|
||||
br label %loopconditions
|
||||
loopend:
|
||||
%cmpret = icmp sge i64 %leftcnt, %rightcnt
|
||||
br i1 %cmpret, label %retleft, label %retright
|
||||
retleft:
|
||||
ret i64 %recleft
|
||||
retright:
|
||||
ret i64 %recright
|
||||
base:
|
||||
%retb = call i64 @at([0 x i64]* %arr, i64 %l)
|
||||
ret i64 %retb
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%arr1 = bitcast [10 x i64]* @test1 to [0 x i64]*
|
||||
%res1 = call i64 @majority([0 x i64]* %arr1, i64 0, i64 9)
|
||||
%cmp1 = icmp eq i64 %res1, 5
|
||||
br i1 %cmp1, label %test2, label %FAIL
|
||||
test2:
|
||||
%arr2 = bitcast [10 x i64]* @test2 to [0 x i64]*
|
||||
%res2 = call i64 @majority([0 x i64]* %arr2, i64 0, i64 9)
|
||||
%cmp2 = icmp eq i64 %res2, 2
|
||||
br i1 %cmp2, label %test3, label %FAIL
|
||||
test3:
|
||||
%arr3 = bitcast [3 x i64]* @test3 to [0 x i64]*
|
||||
%res3 = call i64 @majority([0 x i64]* %arr3, i64 0, i64 2)
|
||||
%cmp3 = icmp eq i64 %res3, 3
|
||||
br i1 %cmp3, label %test4, label %FAIL
|
||||
test4:
|
||||
%arr4 = bitcast [1 x i64]* @test4 to [0 x i64]*
|
||||
%res4 = call i64 @majority([0 x i64]* %arr4, i64 0, i64 0)
|
||||
%cmp4 = icmp eq i64 %res4, 3
|
||||
br i1 %cmp4, label %test5, label %FAIL
|
||||
test5:
|
||||
%arr5 = bitcast [6 x i64]* @test5 to [0 x i64]*
|
||||
%res5 = call i64 @majority([0 x i64]* %arr5, i64 0, i64 5)
|
||||
%cmp5 = icmp eq i64 %res5, -1
|
||||
br i1 %cmp5, label %test6, label %FAIL
|
||||
test6:
|
||||
%arr6 = bitcast [7 x i64]* @test6 to [0 x i64]*
|
||||
%res6 = call i64 @majority([0 x i64]* %arr6, i64 0, i64 6)
|
||||
%cmp6 = icmp eq i64 %res6, 2
|
||||
br i1 %cmp6, label %NEXT, label %FAIL
|
||||
NEXT:
|
||||
ret i64 1
|
||||
FAIL:
|
||||
ret i64 0
|
||||
}
|
||||
37
hw6/llprograms/sp24_hw3/mariaagn_matrixsum.ll
Normal file
37
hw6/llprograms/sp24_hw3/mariaagn_matrixsum.ll
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
%arr = type [6 x i64]
|
||||
|
||||
@my_arr = global [6 x %arr] [ %arr [ i64 1, i64 2, i64 3, i64 4, i64 5, i64 6 ],
|
||||
%arr [ i64 1, i64 2, i64 3, i64 4, i64 5, i64 6 ],
|
||||
%arr [ i64 1, i64 2, i64 3, i64 4, i64 5, i64 6 ],
|
||||
%arr [ i64 1, i64 2, i64 3, i64 4, i64 5, i64 6 ],
|
||||
%arr [ i64 1, i64 2, i64 3, i64 4, i64 5, i64 6 ],
|
||||
%arr [ i64 1, i64 2, i64 3, i64 4, i64 5, i64 6 ] ]
|
||||
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%2 = call i64 @get_matrix_sum([6 x %arr]* @my_arr, i64 0, i64 0, i64 0)
|
||||
ret i64 %2
|
||||
}
|
||||
|
||||
|
||||
define i64 @get_matrix_sum([6 x %arr]* %matrix, i64 %running_sum, i64 %row, i64 %col) {
|
||||
%check_row = icmp eq i64 %row, 6
|
||||
br i1 %check_row, label %end, label %check
|
||||
check:
|
||||
%check_col = icmp eq i64 %col, 6
|
||||
br i1 %check_col, label %next_row, label %add_col
|
||||
next_row:
|
||||
%nxt_row = add i64 %row, 1
|
||||
%rv1 = call i64 @get_matrix_sum([6 x %arr]* %matrix, i64 %running_sum, i64 %nxt_row, i64 0)
|
||||
ret i64 %rv1
|
||||
add_col:
|
||||
%c = getelementptr [6 x %arr], [6 x %arr]* %matrix, i64 0, i64 %row, i64 %col
|
||||
%val = load i64, i64* %c
|
||||
%update_sum = add i64 %running_sum, %val
|
||||
%nxt_col = add i64 %col, 1
|
||||
%rv2 = call i64 @get_matrix_sum([6 x %arr]* %matrix, i64 %update_sum, i64 %row, i64 %nxt_col)
|
||||
ret i64 %rv2
|
||||
end:
|
||||
ret i64 %running_sum
|
||||
}
|
||||
|
||||
145
hw6/llprograms/sp24_hw3/max_tree.ll
Normal file
145
hw6/llprograms/sp24_hw3/max_tree.ll
Normal file
|
|
@ -0,0 +1,145 @@
|
|||
; Finds the maximum value in a binary tree
|
||||
|
||||
%struct.Node = type { %struct.Node*, %struct.Node*, i64 }
|
||||
|
||||
@node1 = global %struct.Node { %struct.Node* null, %struct.Node* null, i64 100 }
|
||||
@node2 = global %struct.Node { %struct.Node* @node1, %struct.Node* null, i64 10 }
|
||||
@node3 = global %struct.Node { %struct.Node* null, %struct.Node* null, i64 1 }
|
||||
@test1 = global %struct.Node { %struct.Node* @node2, %struct.Node* @node3, i64 5 }
|
||||
@test2 = global %struct.Node { %struct.Node* @test1, %struct.Node* null, i64 1000 }
|
||||
@test3 = global %struct.Node { %struct.Node* @test2, %struct.Node* null, i64 100 }
|
||||
|
||||
define i64 @find_max(%struct.Node* %node) {
|
||||
entry:
|
||||
; Check if node is null
|
||||
%is_null = icmp eq %struct.Node* %node, null
|
||||
br i1 %is_null, label %return_default, label %not_null
|
||||
|
||||
return_default:
|
||||
; If node is null, return default value
|
||||
ret i64 0
|
||||
|
||||
not_null:
|
||||
; If node is not null, continue with the rest of the function
|
||||
; Initialize max to the value of the root node
|
||||
%value_ptr_root = getelementptr %struct.Node, %struct.Node* %node, i32 0, i32 2
|
||||
%root_value = load i64, i64* %value_ptr_root
|
||||
%max = alloca i64
|
||||
store i64 %root_value, i64* %max
|
||||
|
||||
; Initialize left and right maximum values to 0
|
||||
%left_max = alloca i64
|
||||
store i64 0, i64* %left_max
|
||||
%right_max = alloca i64
|
||||
store i64 0, i64* %right_max
|
||||
|
||||
; Start loop iteration
|
||||
br label %loop
|
||||
|
||||
loop:
|
||||
; Load current node's value
|
||||
%value_ptr = getelementptr %struct.Node, %struct.Node* %node, i32 0, i32 2
|
||||
%value = load i64, i64* %value_ptr
|
||||
|
||||
; Load the current maximum value
|
||||
%old_max = load i64, i64* %max
|
||||
|
||||
; Compare current value with the maximum value
|
||||
%is_greater = icmp sgt i64 %value, %old_max
|
||||
|
||||
; If current value is greater, update maximum value
|
||||
br i1 %is_greater, label %update_max, label %check_left
|
||||
|
||||
update_max:
|
||||
; Store the current value as the new maximum
|
||||
store i64 %value, i64* %max
|
||||
br label %check_left
|
||||
|
||||
check_left:
|
||||
; Check if left child exists
|
||||
%left_child_ptr = getelementptr %struct.Node, %struct.Node* %node, i32 0, i32 0
|
||||
%left_child = load %struct.Node*, %struct.Node** %left_child_ptr
|
||||
%is_left_null = icmp eq %struct.Node* %left_child, null
|
||||
br i1 %is_left_null, label %check_right, label %visit_left
|
||||
|
||||
visit_left:
|
||||
; Recurse on left child
|
||||
%left_max_value = call i64 @find_max(%struct.Node* %left_child)
|
||||
store i64 %left_max_value, i64* %left_max
|
||||
br label %check_right
|
||||
|
||||
check_right:
|
||||
; Check if right child exists
|
||||
%right_child_ptr = getelementptr %struct.Node, %struct.Node* %node, i32 0, i32 1
|
||||
%right_child = load %struct.Node*, %struct.Node** %right_child_ptr
|
||||
%is_right_null = icmp eq %struct.Node* %right_child, null
|
||||
br i1 %is_right_null, label %end_loop, label %visit_right
|
||||
|
||||
visit_right:
|
||||
; Recurse on right child
|
||||
%right_max_value = call i64 @find_max(%struct.Node* %right_child)
|
||||
store i64 %right_max_value, i64* %right_max
|
||||
br label %end_loop
|
||||
|
||||
end_loop:
|
||||
; Get current maximum value from root
|
||||
%final_max = load i64, i64* %max
|
||||
; Get the maximum value from left and right subtrees
|
||||
%final_left_max = load i64, i64* %left_max
|
||||
%final_right_max = load i64, i64* %right_max
|
||||
; Compare maximum values from left and right subtrees
|
||||
%is_left_greater = icmp sgt i64 %final_left_max, %final_right_max
|
||||
br i1 %is_left_greater, label %left_greater, label %right_greater
|
||||
|
||||
left_greater:
|
||||
; Check with middle
|
||||
%is_middle_greater_left = icmp sgt i64 %final_max, %final_left_max
|
||||
br i1 %is_middle_greater_left, label %return_middle_max, label %return_left_max
|
||||
|
||||
return_left_max:
|
||||
; If left maximum is greater, return it
|
||||
ret i64 %final_left_max
|
||||
|
||||
right_greater:
|
||||
; Check with middle
|
||||
%is_middle_greater_right = icmp sgt i64 %final_max, %final_right_max
|
||||
br i1 %is_middle_greater_right, label %return_middle_max, label %return_right_max
|
||||
|
||||
return_right_max:
|
||||
; If right maximum is greater, return it
|
||||
ret i64 %final_right_max
|
||||
|
||||
return_middle_max:
|
||||
; If middle maximum is greater, return it
|
||||
ret i64 %final_max
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%res1 = call i64 @find_max(%struct.Node* @test1)
|
||||
%cmp1 = icmp eq i64 %res1, 100
|
||||
br i1 %cmp1, label %test2, label %FAIL
|
||||
test2:
|
||||
%res2 = call i64 @find_max(%struct.Node* @test2)
|
||||
%cmp2 = icmp eq i64 %res2, 1000
|
||||
br i1 %cmp2, label %test3, label %FAIL
|
||||
test3:
|
||||
%res3 = call i64 @find_max(%struct.Node* @test3)
|
||||
%cmp3 = icmp eq i64 %res3, 1000
|
||||
br i1 %cmp3, label %node1, label %FAIL
|
||||
node1:
|
||||
%res4 = call i64 @find_max(%struct.Node* @node1)
|
||||
%cmp4 = icmp eq i64 %res4, 100
|
||||
br i1 %cmp4, label %node2, label %FAIL
|
||||
node2:
|
||||
%res5 = call i64 @find_max(%struct.Node* @node2)
|
||||
%cmp5 = icmp eq i64 %res5, 100
|
||||
br i1 %cmp5, label %node3, label %FAIL
|
||||
node3:
|
||||
%res6 = call i64 @find_max(%struct.Node* @node3)
|
||||
%cmp6 = icmp eq i64 %res6, 1
|
||||
br i1 %cmp6, label %PASS, label %FAIL
|
||||
PASS:
|
||||
ret i64 1
|
||||
FAIL:
|
||||
ret i64 0
|
||||
}
|
||||
137
hw6/llprograms/sp24_hw3/merge_sorted_arrs.ll
Normal file
137
hw6/llprograms/sp24_hw3/merge_sorted_arrs.ll
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
; Merge two sorted array inplace and put the sorted array in nums1
|
||||
|
||||
%first_arr_t = type [10 x i64]
|
||||
%second_arr_t = type [5 x i64]
|
||||
|
||||
@first_arr = global %first_arr_t [i64 1, i64 2, i64 3, i64 4, i64 5, i64 0, i64 0, i64 0, i64 0, i64 0]
|
||||
@second_arr = global %second_arr_t [i64 2, i64 4, i64 5, i64 8, i64 9]
|
||||
@expected_arr = global %first_arr_t [i64 1, i64 2, i64 2, i64 3, i64 4, i64 4, i64 5, i64 5, i64 8, i64 9]
|
||||
|
||||
@first_arr2 = global %first_arr_t [i64 1, i64 1, i64 1, i64 2, i64 2, i64 0, i64 0, i64 0, i64 0, i64 0]
|
||||
@second_arr2 = global %second_arr_t [i64 4, i64 4, i64 5, i64 5, i64 10]
|
||||
@expected_arr2 = global %first_arr_t [i64 1, i64 1, i64 1, i64 2, i64 2, i64 4, i64 4, i64 5, i64 5, i64 10]
|
||||
|
||||
@first_arr3 = global %first_arr_t [i64 1, i64 2, i64 3, i64 4, i64 5, i64 0, i64 0, i64 0, i64 0, i64 0]
|
||||
@second_arr3 = global %second_arr_t [i64 0, i64 0, i64 0, i64 0, i64 0]
|
||||
@expected_arr3 = global %first_arr_t [i64 0, i64 0, i64 0, i64 0, i64 0, i64 1, i64 2, i64 3, i64 4, i64 5]
|
||||
|
||||
@first_arr4 = global %first_arr_t [i64 3, i64 3, i64 3, i64 3, i64 3, i64 0, i64 0, i64 0, i64 0, i64 0]
|
||||
@second_arr4 = global %second_arr_t [i64 3, i64 3, i64 3, i64 3, i64 3]
|
||||
@expected_arr4 = global %first_arr_t [i64 3, i64 3, i64 3, i64 3, i64 3, i64 3, i64 3, i64 3, i64 3, i64 3]
|
||||
|
||||
@first_arr5 = global %first_arr_t [i64 7, i64 8, i64 9, i64 9, i64 10, i64 0, i64 0, i64 0, i64 0, i64 0]
|
||||
@second_arr5 = global %second_arr_t [i64 10, i64 11, i64 13, i64 13, i64 15]
|
||||
@expected_arr5 = global %first_arr_t [i64 7, i64 8, i64 9, i64 9, i64 10, i64 10, i64 11, i64 13, i64 13, i64 15]
|
||||
|
||||
define i1 @is_equal(%first_arr_t* %nums1, %first_arr_t* %expected_nums1) {
|
||||
%i_ptr = alloca i64
|
||||
store i64 0, i64* %i_ptr
|
||||
br label %for_condition
|
||||
|
||||
for_condition:
|
||||
%i = load i64, i64* %i_ptr
|
||||
%cmp = icmp slt i64 %i, 10
|
||||
br i1 %cmp, label %for_body, label %success
|
||||
|
||||
for_body:
|
||||
%nums1_i_ptr = getelementptr %first_arr_t, %first_arr_t* %nums1, i32 0, i64 %i
|
||||
%expected_nums1_i_ptr = getelementptr %first_arr_t, %first_arr_t* %expected_nums1, i32 0, i64 %i
|
||||
%nums1_i = load i64, i64* %nums1_i_ptr
|
||||
%expected_nums1_i = load i64, i64* %expected_nums1_i_ptr
|
||||
%cmp2 = icmp eq i64 %nums1_i, %expected_nums1_i
|
||||
br i1 %cmp2, label %continue, label %fail
|
||||
|
||||
continue:
|
||||
%i2 = load i64, i64* %i_ptr
|
||||
%i_plus_1 = add i64 %i2, 1
|
||||
store i64 %i_plus_1, i64* %i_ptr
|
||||
br label %for_condition
|
||||
|
||||
fail:
|
||||
ret i1 0
|
||||
success:
|
||||
ret i1 1
|
||||
}
|
||||
|
||||
define void @merge_sorted(%first_arr_t* %nums1, i64 %m, %second_arr_t* %nums2, i64 %n) {
|
||||
%i_ptr = alloca i64
|
||||
%j_ptr = alloca i64
|
||||
%k_ptr = alloca i64
|
||||
%i = add i64 %m, -1
|
||||
%j = add i64 %n, -1
|
||||
%m_and_n = add i64 %m, %n
|
||||
%k = add i64 %m_and_n, -1
|
||||
store i64 %i, i64* %i_ptr
|
||||
store i64 %j, i64* %j_ptr
|
||||
store i64 %k, i64* %k_ptr
|
||||
br label %while
|
||||
|
||||
while:
|
||||
%curr_j = load i64, i64* %j_ptr
|
||||
%is_greater_or_equal_0 = icmp sge i64 %curr_j, 0
|
||||
br i1 %is_greater_or_equal_0, label %check_i_greater_or_equal_0, label %done
|
||||
|
||||
check_i_greater_or_equal_0:
|
||||
%curr_i = load i64, i64* %i_ptr
|
||||
%is_greater_or_equal_0_2 = icmp sge i64 %curr_i, 0
|
||||
br i1 %is_greater_or_equal_0_2, label %num_comparison, label %nums2_greater
|
||||
|
||||
num_comparison:
|
||||
%i_for_num = load i64, i64* %i_ptr
|
||||
%j_for_num = load i64, i64* %j_ptr
|
||||
%nums1_i_ptr = getelementptr %first_arr_t, %first_arr_t* %nums1, i32 0, i64 %i_for_num
|
||||
%nums2_j_ptr = getelementptr %second_arr_t, %second_arr_t* %nums2, i32 0, i64 %j_for_num
|
||||
%nums1_i = load i64, i64* %nums1_i_ptr
|
||||
%nums2_j = load i64, i64* %nums2_j_ptr
|
||||
%is_nums1_greater = icmp sgt i64 %nums1_i, %nums2_j
|
||||
br i1 %is_nums1_greater, label %nums1_greater, label %nums2_greater
|
||||
|
||||
nums1_greater:
|
||||
%k_for_num = load i64, i64* %k_ptr
|
||||
%i_for_num2 = load i64, i64* %i_ptr
|
||||
%nums1_k_ptr = getelementptr %first_arr_t, %first_arr_t* %nums1, i32 0, i64 %k_for_num
|
||||
%nums1_i_ptr2 = getelementptr %first_arr_t, %first_arr_t* %nums1, i32 0, i64 %i_for_num2
|
||||
%nums2_i = load i64, i64* %nums1_i_ptr2
|
||||
store i64 %nums2_i, i64* %nums1_k_ptr
|
||||
%k_minus_1 = add i64 %k_for_num, -1
|
||||
%i_minus_1 = add i64 %i_for_num2, -1
|
||||
store i64 %k_minus_1, i64* %k_ptr
|
||||
store i64 %i_minus_1, i64* %i_ptr
|
||||
br label %while
|
||||
|
||||
nums2_greater:
|
||||
%k_for_num2= load i64, i64* %k_ptr
|
||||
%j_for_num2 = load i64, i64* %j_ptr
|
||||
%nums1_k_ptr2 = getelementptr %first_arr_t, %first_arr_t* %nums1, i32 0, i64 %k_for_num2
|
||||
%nums2_j_ptr2 = getelementptr %second_arr_t, %second_arr_t* %nums2, i32 0, i64 %j_for_num2
|
||||
%nums2_j2 = load i64, i64* %nums2_j_ptr2
|
||||
store i64 %nums2_j2, i64* %nums1_k_ptr2
|
||||
%k_minus_1_2 = add i64 %k_for_num2, -1
|
||||
%j_minus_1 = add i64 %j_for_num2, -1
|
||||
store i64 %k_minus_1_2, i64* %k_ptr
|
||||
store i64 %j_minus_1, i64* %j_ptr
|
||||
br label %while
|
||||
|
||||
done:
|
||||
ret void
|
||||
}
|
||||
|
||||
define i1 @main(i64 %argc, i8** %arcv) {
|
||||
call void @merge_sorted(%first_arr_t* @first_arr, i64 5, %second_arr_t* @second_arr, i64 5)
|
||||
%res1 = call i1 @is_equal(%first_arr_t* @first_arr, %first_arr_t* @expected_arr)
|
||||
call void @merge_sorted(%first_arr_t* @first_arr2, i64 5, %second_arr_t* @second_arr2, i64 5)
|
||||
%res2 = call i1 @is_equal(%first_arr_t* @first_arr2, %first_arr_t* @expected_arr2)
|
||||
call void @merge_sorted(%first_arr_t* @first_arr3, i64 5, %second_arr_t* @second_arr3, i64 5)
|
||||
%res3 = call i1 @is_equal(%first_arr_t* @first_arr3, %first_arr_t* @expected_arr3)
|
||||
call void @merge_sorted(%first_arr_t* @first_arr4, i64 5, %second_arr_t* @second_arr4, i64 5)
|
||||
%res4 = call i1 @is_equal(%first_arr_t* @first_arr4, %first_arr_t* @expected_arr4)
|
||||
call void @merge_sorted(%first_arr_t* @first_arr5, i64 5, %second_arr_t* @second_arr5, i64 5)
|
||||
%res5 = call i1 @is_equal(%first_arr_t* @first_arr5, %first_arr_t* @expected_arr5)
|
||||
|
||||
%and1 = and i1 %res1, %res2
|
||||
%and2 = and i1 %and1, %res3
|
||||
%and3 = and i1 %and2, %res4
|
||||
%and4 = and i1 %and3, %res5
|
||||
|
||||
ret i1 %and4
|
||||
}
|
||||
37
hw6/llprograms/sp24_hw3/nnair1_iter.ll
Normal file
37
hw6/llprograms/sp24_hw3/nnair1_iter.ll
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
declare i8* @ll_ltoa(i64)
|
||||
declare void @ll_puts(i8*)
|
||||
|
||||
%arr = type [10 x i64]
|
||||
|
||||
@nums = global %arr [ i64 5, i64 1, i64 4, i64 2, i64 3, i64 6, i64 10, i64 9, i64 7, i64 8 ]
|
||||
|
||||
define void @iter(void(i64)* %f, %arr* %l) {
|
||||
%1 = alloca i64
|
||||
store i64 0, i64* %1
|
||||
br label %loop
|
||||
loop:
|
||||
%i = load i64, i64* %1
|
||||
%valid = icmp slt i64 %i, 10
|
||||
br i1 %valid, label %body, label %post
|
||||
body:
|
||||
%idx = load i64, i64* %1
|
||||
%p = getelementptr %arr, %arr* %l, i32 0, i32 %idx
|
||||
%n = load i64, i64* %p
|
||||
call void %f(i64 %n)
|
||||
%newidx = add i64 %idx, 1
|
||||
store i64 %newidx, i64* %1
|
||||
br label %loop
|
||||
post:
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @print(i64 %x) {
|
||||
%1 = call i8* @ll_ltoa(i64 %x)
|
||||
call void @ll_puts(i8* %1)
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @main(i64 %argc, i8** %arcv) {
|
||||
call void @iter(void(i64)* @print, %arr* @nums)
|
||||
ret void
|
||||
}
|
||||
146
hw6/llprograms/sp24_hw3/quick_sort_with_print.ll
Normal file
146
hw6/llprograms/sp24_hw3/quick_sort_with_print.ll
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
declare void @ll_puts(i8*)
|
||||
declare i8* @ll_ltoa(i64)
|
||||
|
||||
@original_list = global [5 x i64] [ i64 2, i64 1, i64 3, i64 5, i64 4 ]
|
||||
@sorted_list = global [5 x i64] [ i64 1, i64 2, i64 3, i64 4, i64 5 ]
|
||||
|
||||
@pre_swap_list = global [5 x i64] [ i64 2, i64 1, i64 3, i64 5, i64 4 ]
|
||||
@swapped_list = global [5 x i64] [ i64 3, i64 1, i64 2, i64 5, i64 4 ]
|
||||
|
||||
@pre_partition_list = global [5 x i64] [ i64 2, i64 1, i64 3, i64 5, i64 4 ]
|
||||
@partitioned_list = global [5 x i64] [ i64 2, i64 1, i64 3, i64 4, i64 5 ]
|
||||
|
||||
define void @swap([5 x i64]* %list, i64 %i, i64 %j) {
|
||||
%iptr = getelementptr [5 x i64], [5 x i64]* %list, i64 0, i64 %i
|
||||
%jptr = getelementptr [5 x i64], [5 x i64]* %list, i64 0, i64 %j
|
||||
%ival = load i64, i64* %iptr
|
||||
%jval = load i64, i64* %jptr
|
||||
store i64 %ival, i64* %jptr
|
||||
store i64 %jval, i64* %iptr
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @partition([5 x i64]* %list, i64 %lo, i64 %hi) {
|
||||
; hi = last index of array
|
||||
%pivotptr = getelementptr [5 x i64], [5 x i64]* %list, i64 0, i64 %hi
|
||||
%pivotval = load i64, i64* %pivotptr
|
||||
|
||||
%tmp = sub i64 %lo, 1
|
||||
%i = alloca i64
|
||||
store i64 %tmp, i64* %i
|
||||
%j = alloca i64
|
||||
store i64 %lo, i64* %j
|
||||
br label %loop
|
||||
loop:
|
||||
%itmp = load i64, i64* %i
|
||||
%jtmp = load i64, i64* %j
|
||||
%cmp1 = icmp sle i64 %jtmp, %hi
|
||||
br i1 %cmp1, label %cont, label %end
|
||||
cont:
|
||||
%jptr = getelementptr [5 x i64], [5 x i64]* %list, i64 0, i64 %jtmp
|
||||
%jval = load i64, i64* %jptr
|
||||
%cmp2 = icmp slt i64 %jval, %pivotval
|
||||
br i1 %cmp2, label %swap, label %increment
|
||||
swap:
|
||||
%b = add i64 1, %itmp
|
||||
store i64 %b, i64* %i
|
||||
%itmp2 = load i64, i64* %i
|
||||
call void @swap([5 x i64]* %list, i64 %itmp2, i64 %jtmp)
|
||||
br label %increment
|
||||
increment:
|
||||
%a = add i64 1, %jtmp
|
||||
store i64 %a, i64* %j
|
||||
br label %loop
|
||||
end:
|
||||
%itmp3 = load i64, i64* %i
|
||||
%c = add i64 1, %itmp3
|
||||
call void @swap([5 x i64]* %list, i64 %c, i64 %hi)
|
||||
ret i64 %c
|
||||
}
|
||||
|
||||
define void @quicksort ([5 x i64]* %list, i64 %lo, i64 %hi) {
|
||||
; hi = last index of array
|
||||
%cmp1 = icmp slt i64 %lo, %hi
|
||||
br i1 %cmp1, label %sort, label %endsort
|
||||
sort:
|
||||
%pivotind = call i64 @partition([5 x i64]* %list, i64 %lo, i64 %hi)
|
||||
%pivotindmin1 = sub i64 %pivotind, 1
|
||||
call void @quicksort([5 x i64]* %list, i64 %lo, i64 %pivotindmin1)
|
||||
%pivotindadd1 = add i64 %pivotind, 1
|
||||
call void @quicksort([5 x i64]* %list, i64 %pivotindadd1, i64 %hi)
|
||||
br label %endsort
|
||||
endsort:
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @check ([5 x i64]* %r, [5 x i64]* %s) {
|
||||
; returns 0 if equal, 1 if not equal
|
||||
%i = alloca i64
|
||||
store i64 0, i64* %i
|
||||
br label %loop
|
||||
loop:
|
||||
%count = load i64, i64* %i
|
||||
%cmp1 = icmp eq i64 %count, 5
|
||||
br i1 %cmp1, label %equal, label %check
|
||||
check:
|
||||
%rptr = getelementptr [5 x i64], [5 x i64]* %r, i32 0, i64 %count
|
||||
%sptr = getelementptr [5 x i64], [5 x i64]* %s, i32 0, i64 %count
|
||||
%rval = load i64, i64* %rptr
|
||||
%sval = load i64, i64* %sptr
|
||||
%cmp2 = icmp eq i64 %rval, %sval
|
||||
%a = add i64 1, %count
|
||||
store i64 %a, i64* %i
|
||||
br i1 %cmp2, label %loop, label %noteq
|
||||
equal:
|
||||
ret i64 0
|
||||
noteq:
|
||||
ret i64 1
|
||||
}
|
||||
|
||||
define void @printarr([5 x i64]* %list, i64 %lo, i64 %hi) {
|
||||
; hi = length of array
|
||||
%i = alloca i64
|
||||
store i64 %lo, i64* %i
|
||||
br label %loop
|
||||
loop:
|
||||
%count = load i64, i64* %i
|
||||
%cmp1 = icmp eq i64 %count, %hi
|
||||
br i1 %cmp1, label %end, label %check
|
||||
check:
|
||||
%ptr = getelementptr [5 x i64], [5 x i64]* %list, i32 0, i64 %count
|
||||
%val = load i64, i64* %ptr
|
||||
%bug = call i8* @ll_ltoa(i64 %val)
|
||||
call void @ll_puts(i8* %bug)
|
||||
%a = add i64 1, %count
|
||||
store i64 %a, i64* %i
|
||||
br label %loop
|
||||
end:
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
; sort test
|
||||
call void @printarr([5 x i64]* @original_list, i64 0, i64 5)
|
||||
call void @quicksort([5 x i64]* @original_list, i64 0, i64 4)
|
||||
call void @printarr([5 x i64]* @original_list, i64 0, i64 5)
|
||||
%1 = call i64 @check([5 x i64]* @original_list, [5 x i64]* @sorted_list)
|
||||
|
||||
; swap test
|
||||
call void @printarr([5 x i64]* @pre_swap_list, i64 0, i64 5)
|
||||
call void @swap([5 x i64]* @pre_swap_list, i64 0, i64 2)
|
||||
call void @printarr([5 x i64]* @pre_swap_list, i64 0, i64 5)
|
||||
%2 = call i64 @check([5 x i64]* @pre_swap_list, [5 x i64]* @swapped_list)
|
||||
|
||||
; partition test
|
||||
call void @printarr([5 x i64]* @pre_partition_list, i64 0, i64 5)
|
||||
%pi = call i64 @partition([5 x i64]* @pre_partition_list, i64 0, i64 4)
|
||||
%pis = call i8* @ll_ltoa(i64 %pi)
|
||||
call void @ll_puts(i8* %pis)
|
||||
call void @printarr([5 x i64]* @pre_partition_list, i64 0, i64 5)
|
||||
%3 = call i64 @check([5 x i64]* @pre_partition_list, [5 x i64]* @partitioned_list)
|
||||
|
||||
%4 = add i64 %1, %2
|
||||
%5 = add i64 %3, %4
|
||||
|
||||
ret i64 %5
|
||||
}
|
||||
128
hw6/llprograms/sp24_hw3/quicksort.ll
Normal file
128
hw6/llprograms/sp24_hw3/quicksort.ll
Normal file
|
|
@ -0,0 +1,128 @@
|
|||
%arraystruct = type {[9 x i64]}
|
||||
|
||||
; 8,7,2,1,0,9,3,2,1
|
||||
@gtest1 = global %arraystruct {[9 x i64] [i64 8, i64 7, i64 2, i64 1, i64 0, i64 9, i64 3, i64 2, i64 1]}
|
||||
@ans1 = global %arraystruct {[9 x i64] [i64 0, i64 1, i64 1, i64 2, i64 2, i64 3, i64 7, i64 8, i64 9]}
|
||||
@gtest2 = global %arraystruct {[9 x i64] [i64 1, i64 5, i64 3, i64 8, i64 2, i64 6, i64 9, i64 4, i64 7]}
|
||||
@ans2 = global %arraystruct {[9 x i64] [i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9]}
|
||||
@gtest3 = global %arraystruct {[9 x i64] [i64 9, i64 3, i64 6, i64 1, i64 5, i64 2, i64 7, i64 8, i64 4]}
|
||||
@ans3 = global %arraystruct {[9 x i64] [i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9]}
|
||||
@gtest4 = global %arraystruct {[9 x i64] [i64 5, i64 2, i64 8, i64 9, i64 1, i64 4, i64 6, i64 3, i64 7]}
|
||||
@ans4 = global %arraystruct {[9 x i64] [i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9]}
|
||||
|
||||
|
||||
; @gtest1 = global %arraystruct [i64 2, i64 3, i64 1]
|
||||
; @ans1 = global %arraystruct [i64 1, i64 2, i64 3]
|
||||
|
||||
define i1 @eq9(%arraystruct* %arr1, %arraystruct* %arr2) {
|
||||
%lengthptr = alloca i64
|
||||
store i64 9, i64* %lengthptr
|
||||
%length = load i64, i64* %lengthptr
|
||||
%iptr = alloca i64
|
||||
store i64 0, i64* %iptr
|
||||
br label %loop_entry
|
||||
loop_entry:
|
||||
%iteri = load i64, i64* %iptr
|
||||
%cmpll = icmp slt i64 %iteri, %length
|
||||
br i1 %cmpll, label %loop_body, label %loop_exit_s
|
||||
loop_body:
|
||||
%arri1ptr = getelementptr %arraystruct, %arraystruct* %arr1, i32 0, i32 0, i64 %iteri
|
||||
%arri1 = load i64, i64* %arri1ptr
|
||||
%arri2ptr = getelementptr %arraystruct, %arraystruct* %arr2, i32 0, i32 0, i64 %iteri
|
||||
%arri2 = load i64, i64* %arri2ptr
|
||||
%cmp12 = icmp eq i64 %arri1, %arri2
|
||||
br i1 %cmp12, label %loop_next, label %loop_exit_f
|
||||
loop_next:
|
||||
%i = load i64, i64* %iptr
|
||||
%iplus = add i64 %i, 1
|
||||
store i64 %iplus, i64* %iptr
|
||||
br label %loop_entry
|
||||
loop_exit_s:
|
||||
ret i1 1
|
||||
loop_exit_f:
|
||||
ret i1 0
|
||||
}
|
||||
|
||||
define void @swap(i64* %0, i64* %1) {
|
||||
%int0 = load i64, i64* %0
|
||||
%int1 = load i64, i64* %1
|
||||
store i64 %int0, i64* %1
|
||||
store i64 %int1, i64* %0
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @partition(%arraystruct* %arr, i64 %lowint, i64 %highint) {
|
||||
; %highint = load i64, i64* %high
|
||||
; %lowint = load i64, i64* %low
|
||||
%pivotptr = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 %highint
|
||||
%pivotint = load i64, i64* %pivotptr
|
||||
%i = sub i64 %lowint, 1
|
||||
%iptr = alloca i64
|
||||
store i64 %i, i64* %iptr
|
||||
%jptr = alloca i64
|
||||
store i64 %lowint, i64* %jptr
|
||||
br label %loop_entry
|
||||
|
||||
loop_entry:
|
||||
%iterj = load i64, i64* %jptr
|
||||
%cmphigh = icmp slt i64 %iterj, %highint
|
||||
br i1 %cmphigh, label %loop_body, label %loop_exit
|
||||
|
||||
loop_body:
|
||||
%arrj = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 %iterj
|
||||
%arrjval = load i64, i64* %arrj
|
||||
%cmppivot = icmp sle i64 %arrjval, %pivotint
|
||||
br i1 %cmppivot, label %loop_swap, label %loop_next
|
||||
|
||||
loop_swap:
|
||||
%ival = load i64, i64* %iptr
|
||||
%iplus = add i64 1, %ival
|
||||
store i64 %iplus, i64* %iptr
|
||||
%iteri = load i64, i64* %iptr
|
||||
%arrj1 = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 %iterj
|
||||
%arri = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0, i64 %iteri
|
||||
call void @swap(i64* %arri, i64* %arrj1)
|
||||
br label %loop_next
|
||||
|
||||
loop_next:
|
||||
%jplus = add i64 %iterj, 1
|
||||
store i64 %jplus, i64* %jptr
|
||||
br label %loop_entry
|
||||
|
||||
loop_exit:
|
||||
%ifin = load i64, i64* %iptr
|
||||
%ifinplus = add i64 1, %ifin
|
||||
%arrifin = getelementptr %arraystruct, %arraystruct* %arr, i32 0, i32 0,i64 %ifinplus
|
||||
%arrhigh = getelementptr %arraystruct, %arraystruct* %arr, i32 0,i32 0, i64 %highint
|
||||
call void @swap(i64* %arrifin, i64* %arrhigh)
|
||||
ret i64 %ifinplus
|
||||
}
|
||||
|
||||
define void @quicksort(%arraystruct* %arr, i64 %lowint, i64 %highint) {
|
||||
%cmplh = icmp slt i64 %lowint, %highint
|
||||
br i1 %cmplh, label %recursion, label %exit
|
||||
recursion:
|
||||
%pivot = call i64 @partition(%arraystruct* %arr, i64 %lowint, i64 %highint)
|
||||
%pivots1 = sub i64 %pivot, 1
|
||||
%pivota1 = add i64 %pivot, 1
|
||||
call void @quicksort(%arraystruct* %arr, i64 %lowint, i64 %pivots1)
|
||||
call void @quicksort(%arraystruct* %arr, i64 %pivota1, i64 %highint)
|
||||
br label %exit
|
||||
exit:
|
||||
ret void
|
||||
}
|
||||
|
||||
define i1 @main(i64 %argc, i8** %arcv) {
|
||||
call void @quicksort(%arraystruct* @gtest1, i64 0, i64 8)
|
||||
%res1 = call i1 @eq9(%arraystruct* @gtest1, %arraystruct* @ans1)
|
||||
call void @quicksort(%arraystruct* @gtest2, i64 0, i64 8)
|
||||
%res2 = call i1 @eq9(%arraystruct* @gtest2, %arraystruct* @ans2)
|
||||
call void @quicksort(%arraystruct* @gtest3, i64 0, i64 8)
|
||||
%res3 = call i1 @eq9(%arraystruct* @gtest3, %arraystruct* @ans3)
|
||||
call void @quicksort(%arraystruct* @gtest4, i64 0, i64 8)
|
||||
%res4 = call i1 @eq9(%arraystruct* @gtest4, %arraystruct* @ans4)
|
||||
%resagg1 = and i1 %res1, %res2
|
||||
%resagg2 = and i1 %resagg1, %res3
|
||||
%resagg3 = and i1 %resagg1, %res4
|
||||
ret i1 1
|
||||
}
|
||||
101
hw6/llprograms/sp24_hw3/selection_sort.ll
Normal file
101
hw6/llprograms/sp24_hw3/selection_sort.ll
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
%struct = type { i64, [10 x i64] }
|
||||
|
||||
@gbl = global %struct { i64 10, [10 x i64] [ i64 5, i64 1, i64 3, i64 7, i64 4, i64 8, i64 2, i64 6, i64 9, i64 10] }
|
||||
|
||||
@expected_arr = global %struct { i64 10, [10 x i64] [ i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9, i64 10] }
|
||||
|
||||
|
||||
define i64 @is_sorted(%struct* %arr1, %struct* %arr2) {
|
||||
%size_ptr = getelementptr %struct, %struct* %arr1, i32 0, i32 0
|
||||
%iter_ptr = alloca i64
|
||||
store i64 0, i64* %iter_ptr
|
||||
br label %loop_entry
|
||||
loop_entry:
|
||||
%iter = load i64, i64* %iter_ptr
|
||||
%size = load i64, i64* %size_ptr
|
||||
%cmp = icmp slt i64 %iter, %size
|
||||
br i1 %cmp, label %loop_body, label %exit
|
||||
loop_body:
|
||||
%iter1 = load i64, i64* %iter_ptr
|
||||
%val1_ptr = getelementptr %struct, %struct* %arr1, i32 0, i32 1, i64 %iter1
|
||||
%val2_ptr = getelementptr %struct, %struct* %arr2, i32 0, i32 1, i64 %iter1
|
||||
%val1 = load i64, i64* %val1_ptr
|
||||
%val2 = load i64, i64* %val2_ptr
|
||||
%new_iter = add i64 %iter1, 1
|
||||
store i64 %new_iter, i64* %iter_ptr
|
||||
%cmp1 = icmp eq i64 %val1, %val2
|
||||
br i1 %cmp1, label %loop_entry, label %exit_false
|
||||
exit_false:
|
||||
ret i64 0
|
||||
exit:
|
||||
ret i64 1
|
||||
}
|
||||
|
||||
|
||||
define i64 @sort(%struct* %arr) {
|
||||
%size_ptr = getelementptr %struct, %struct* %arr, i32 0, i32 0
|
||||
%i_ptr = alloca i64
|
||||
%j_ptr = alloca i64
|
||||
%min_idx_ptr = alloca i64
|
||||
store i64 0, i64* %i_ptr
|
||||
br label %loop_entry1
|
||||
loop_entry1:
|
||||
%i_val = load i64, i64* %i_ptr
|
||||
%size_val = load i64, i64* %size_ptr
|
||||
%loop_1_ub = sub i64 %size_val, 1
|
||||
%cmp = icmp slt i64 %i_val, %loop_1_ub
|
||||
br i1 %cmp, label %loop_body1, label %exit
|
||||
loop_body1:
|
||||
%min_idx_val = load i64, i64* %i_ptr
|
||||
store i64 %min_idx_val, i64* %min_idx_ptr
|
||||
%i_val1 = load i64, i64* %i_ptr
|
||||
%i_val2 = add i64 1, %i_val1
|
||||
store i64 %i_val2, i64* %j_ptr
|
||||
br label %entry2
|
||||
entry2:
|
||||
%size_val1 = load i64, i64* %size_ptr
|
||||
%j_val2 = load i64, i64* %j_ptr
|
||||
%cmp1 = icmp slt i64 %j_val2, %size_val1
|
||||
br i1 %cmp1, label %loop_body2, label %swap
|
||||
loop_body2:
|
||||
%min_idx_val1 = load i64, i64* %min_idx_ptr
|
||||
%j_val3 = load i64, i64* %j_ptr
|
||||
%arr_min_idx_ptr = getelementptr %struct, %struct* %arr, i32 0, i32 1, i64 %min_idx_val1
|
||||
%arr_j_ptr = getelementptr %struct, %struct* %arr, i32 0, i32 1, i64 %j_val3
|
||||
%arr_min_idx_val = load i64, i64* %arr_min_idx_ptr
|
||||
%arr_j_val = load i64, i64* %arr_j_ptr
|
||||
%cmp2 = icmp slt i64 %arr_j_val, %arr_min_idx_val
|
||||
br i1 %cmp2, label %update_min_idx, label %merge
|
||||
update_min_idx:
|
||||
store i64 %j_val3, i64* %min_idx_ptr
|
||||
br label %merge
|
||||
merge:
|
||||
%j_val4 = add i64 1, %j_val3
|
||||
store i64 %j_val4, i64* %j_ptr
|
||||
br label %entry2
|
||||
swap:
|
||||
%min_idx_val2 = load i64, i64* %min_idx_ptr
|
||||
%i_val3 = load i64, i64* %i_ptr
|
||||
%arr_min_idx_ptr_1 = getelementptr %struct, %struct* %arr, i32 0, i32 1, i64 %min_idx_val2
|
||||
%arr_i_ptr = getelementptr %struct, %struct* %arr, i32 0, i32 1, i64 %i_val3
|
||||
|
||||
%arr_min_idx_val_1 = load i64, i64* %arr_min_idx_ptr_1
|
||||
%arr_i_val = load i64, i64* %arr_i_ptr
|
||||
|
||||
store i64 %arr_min_idx_val_1, i64* %arr_i_ptr
|
||||
store i64 %arr_i_val, i64* %arr_min_idx_ptr_1
|
||||
|
||||
%i_val4 = add i64 1, %i_val3
|
||||
store i64 %i_val4, i64* %i_ptr
|
||||
|
||||
br label %loop_entry1
|
||||
exit:
|
||||
ret i64 0
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%1 = call i64 @sort(%struct* @gbl)
|
||||
%ret_val = call i64 @is_sorted(%struct* @gbl, %struct* @expected_arr)
|
||||
ret i64 %ret_val
|
||||
}
|
||||
|
||||
48
hw6/llprograms/sp24_hw3/single_number.ll
Normal file
48
hw6/llprograms/sp24_hw3/single_number.ll
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
%arr = type [5 x i64]
|
||||
|
||||
@glb_arr = global %arr [i64 1, i64 1, i64 4, i64 3, i64 3]
|
||||
|
||||
define i64 @singleNumber(%arr* %myarr, i64 %size)
|
||||
{
|
||||
%result = alloca i64
|
||||
store i64 0, i64* %result
|
||||
|
||||
%index = alloca i64
|
||||
store i64 0, i64* %index
|
||||
|
||||
br label %loop
|
||||
loop:
|
||||
%l_idx = load i64, i64* %index
|
||||
%idx_check = icmp slt i64 %l_idx, %size
|
||||
br i1 %idx_check, label %compute, label %ret_ext
|
||||
|
||||
compute:
|
||||
%compute_index = load i64, i64* %index
|
||||
%elem = getelementptr %arr, %arr* %myarr, i32 0, i32 %compute_index
|
||||
%new_elem = load i64, i64* %elem
|
||||
%temp_res = load i64, i64* %result
|
||||
%new_res = xor i64 %new_elem, %temp_res
|
||||
store i64 %new_res, i64* %result
|
||||
|
||||
br label %loop_inc
|
||||
|
||||
loop_inc:
|
||||
%inc_idx = load i64, i64* %index
|
||||
%new_idx = add i64 %inc_idx, 1
|
||||
store i64 %new_idx, i64* %index
|
||||
br label %loop
|
||||
|
||||
ret_ext:
|
||||
%return_val = load i64, i64* %result
|
||||
ret i64 %return_val
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv){
|
||||
; Expected Return Value 4
|
||||
; This function XORs the elements of the array to find single unique element
|
||||
; Second argument is size of the array
|
||||
%ans = call i64 @singleNumber(%arr* @glb_arr, i64 5)
|
||||
ret i64 %ans
|
||||
}
|
||||
|
||||
|
||||
52
hw6/llprograms/sp24_hw3/square-sum.ll
Normal file
52
hw6/llprograms/sp24_hw3/square-sum.ll
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
%arr = type [5 x i64]
|
||||
|
||||
@tmp = global %arr [ i64 2, i64 4, i64 6, i64 8, i64 10 ]
|
||||
|
||||
define void @mapsquarehelper(%arr* %input, i64 %index) {
|
||||
%1 = icmp eq i64 %index, 5
|
||||
br i1 %1, label %ret1, label %recurse
|
||||
ret1:
|
||||
ret void
|
||||
recurse:
|
||||
%2 = getelementptr %arr, %arr* %input, i64 0, i64 %index
|
||||
%3 = load i64, i64* %2
|
||||
%4 = call i64 @square(i64 %3)
|
||||
store i64 %4, i64* %2
|
||||
%5 = add i64 %index, 1
|
||||
call void @mapsquarehelper(%arr* %input, i64 %5)
|
||||
ret void
|
||||
}
|
||||
|
||||
define void @mapsquare(%arr* %input) {
|
||||
call void @mapsquarehelper(%arr* %input, i64 0)
|
||||
ret void
|
||||
}
|
||||
|
||||
define i64 @square(i64 %x) {
|
||||
%1 = mul i64 %x, %x
|
||||
ret i64 %1
|
||||
}
|
||||
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
call void @mapsquare(%arr* @tmp)
|
||||
|
||||
%1 = getelementptr %arr, %arr* @tmp, i64 0, i64 0
|
||||
%2 = getelementptr %arr, %arr* @tmp, i64 0, i64 1
|
||||
%3 = getelementptr %arr, %arr* @tmp, i64 0, i64 2
|
||||
%4 = getelementptr %arr, %arr* @tmp, i64 0, i64 3
|
||||
%5 = getelementptr %arr, %arr* @tmp, i64 0, i64 4
|
||||
|
||||
%6 = load i64, i64* %1
|
||||
%7 = load i64, i64* %2
|
||||
%8 = load i64, i64* %3
|
||||
%9 = load i64, i64* %4
|
||||
%10 = load i64, i64* %5
|
||||
|
||||
%11 = add i64 %6, %7
|
||||
%12 = add i64 %11, %8
|
||||
%13 = add i64 %12, %9
|
||||
%14 = add i64 %13, %10
|
||||
|
||||
ret i64 %14
|
||||
}
|
||||
31
hw6/llprograms/sp24_hw3/sum_array.ll
Normal file
31
hw6/llprograms/sp24_hw3/sum_array.ll
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
%array = type [10 x i64]
|
||||
|
||||
@nums = global %array [i64 1, i64 2, i64 3, i64 4, i64 5, i64 6, i64 7, i64 8, i64 9, i64 10]
|
||||
|
||||
define i64 @sum() {
|
||||
%ipt = alloca i64
|
||||
store i64 0, i64* %ipt
|
||||
%sumpt = alloca i64
|
||||
store i64 0, i64* %sumpt
|
||||
br label %loop
|
||||
loop:
|
||||
%i = load i64, i64* %ipt
|
||||
%s = load i64, i64* %sumpt
|
||||
%elempt = getelementptr %array, %array* @nums, i32 0, i64 %i
|
||||
%elem = load i64, i64* %elempt
|
||||
%s2 = add i64 %s, %elem
|
||||
%i2 = add i64 %i, 1
|
||||
store i64 %s2, i64* %sumpt
|
||||
store i64 %i2, i64* %ipt
|
||||
|
||||
%test = icmp slt i64 %i2, 10
|
||||
br i1 %test, label %loop, label %end
|
||||
|
||||
end:
|
||||
ret i64 %s2
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
%ans = call i64 @sum()
|
||||
ret i64 %ans
|
||||
}
|
||||
64
hw6/llprograms/sp24_hw3/triangle_nums.ll
Normal file
64
hw6/llprograms/sp24_hw3/triangle_nums.ll
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
%array_t = type {[10 x i64]}
|
||||
|
||||
@array_g = global %array_t {[10 x i64] [i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0, i64 0]}
|
||||
|
||||
define i1 @triangle(i64 %max) {
|
||||
%counter = alloca i64
|
||||
store i64 1, i64* %counter
|
||||
|
||||
br label %loop
|
||||
|
||||
loop:
|
||||
%current_counter = load i64, i64* %counter
|
||||
%last_counter = add i64 %current_counter, -1
|
||||
%last_ptr = getelementptr %array_t, %array_t* @array_g, i64 %last_counter
|
||||
%last_val = load i64, i64* %last_ptr
|
||||
|
||||
; Store the triangle number in the array
|
||||
%current_val = add i64 %last_val, %current_counter
|
||||
%current_ptr = getelementptr %array_t, %array_t* @array_g, i64 %current_counter
|
||||
store i64 %current_val, i64* %current_ptr
|
||||
|
||||
%correct_check = call i1 @check(i64 %current_val, i64 %current_counter)
|
||||
br i1 %correct_check, label %success, label %fail
|
||||
|
||||
success:
|
||||
; Increment the counter & store
|
||||
%next_counter = add i64 %current_counter, 1
|
||||
store i64 %next_counter, i64* %counter
|
||||
|
||||
; Check if the counter exceeds the size of the array
|
||||
%less_than_size = icmp slt i1 %next_counter, %max
|
||||
br i1 %less_than_size, label %loop, label %exit
|
||||
|
||||
exit:
|
||||
ret i1 0
|
||||
|
||||
fail:
|
||||
ret i1 1
|
||||
}
|
||||
|
||||
define i1 @check(i64 %val, i64 %ind) {
|
||||
%t1 = add i64 %ind, 1
|
||||
%t3 = mul i64 %ind, %t1
|
||||
|
||||
; * 2 because no divide
|
||||
%val2 = mul i64 %val, 2
|
||||
%cmp = icmp eq i1 %val2, %t3
|
||||
br i1 %cmp, label %success, label %fail
|
||||
|
||||
fail:
|
||||
ret i1 0
|
||||
|
||||
success:
|
||||
ret i1 1
|
||||
}
|
||||
|
||||
define i1 @main(i64 %argc, i8** %arcv) {
|
||||
%ret = call i1 @triangle(i64 10)
|
||||
%last_check = call i1 @check(i64 45, i64 9)
|
||||
%invert = sub i1 1, %last_check
|
||||
%ret2 = add i64 %ret, %invert
|
||||
|
||||
ret i1 %ret2
|
||||
}
|
||||
73
hw6/llprograms/sp24_hw3/two_sum.ll
Normal file
73
hw6/llprograms/sp24_hw3/two_sum.ll
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
; judtin, jasyan
|
||||
; two sum: given a list nums and int target, return the sum of the two indices whose values add up to target
|
||||
|
||||
%array = type [10 x i64]
|
||||
|
||||
%pair = type [2 x i64]
|
||||
|
||||
@target = global i64 800
|
||||
@array_size = global i64 10
|
||||
@nums = global %array [ i64 123, i64 934, i64 3, i64 342, i64 732, i64 396, i64 502, i64 221, i64 6, i64 797 ]
|
||||
|
||||
define i64 @two_sum(i64* %arr) {
|
||||
%i_ptr = alloca i64
|
||||
store i64 0, i64* %i_ptr
|
||||
|
||||
%j_ptr = alloca i64
|
||||
%array_size_val = load i64, i64* @array_size
|
||||
|
||||
%result = alloca i64
|
||||
store i64 -1, i64* %result
|
||||
|
||||
br label %outer_loop
|
||||
|
||||
outer_loop:
|
||||
store i64 0, i64* %j_ptr
|
||||
br label %inner_loop
|
||||
|
||||
inner_loop:
|
||||
%i = load i64, i64* %i_ptr
|
||||
%j = load i64, i64* %j_ptr
|
||||
|
||||
%i_elem_ptr = getelementptr %array, %array* %arr, i32 0, i64 %i
|
||||
%j_elem_ptr = getelementptr %array, %array* %arr, i32 0, i64 %j
|
||||
%i_elem = load i64, i64* %i_elem_ptr
|
||||
%j_elem = load i64, i64* %j_elem_ptr
|
||||
|
||||
%sum = add i64 %i_elem, %j_elem
|
||||
|
||||
%target_val = load i64, i64* @target
|
||||
%cmp = icmp eq i64 %target_val, %sum
|
||||
|
||||
br i1 %cmp, label %found, label %not_found
|
||||
|
||||
found:
|
||||
%ans = add i64 %i, %j
|
||||
store i64 %ans, i64* %result
|
||||
br label %end_loop
|
||||
|
||||
not_found:
|
||||
%j_plus_1 = add i64 %j, 1
|
||||
store i64 %j_plus_1, i64* %j_ptr
|
||||
|
||||
%cmp1 = icmp eq i64 %array_size_val, %j
|
||||
br i1 %cmp1, label %end_inner_loop, label %inner_loop
|
||||
|
||||
end_inner_loop:
|
||||
%i_plus_1 = add i64 %i, 1
|
||||
store i64 %i_plus_1, i64* %i_ptr
|
||||
|
||||
%cmp2 = icmp eq i64 %array_size_val, %i
|
||||
br i1 %cmp2, label %end_loop, label %outer_loop
|
||||
|
||||
end_loop:
|
||||
%res_val = load i64, i64* %result
|
||||
ret i64 %res_val
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
%ans = call i64 @two_sum(%array* @nums)
|
||||
ret i64 %ans
|
||||
}
|
||||
|
||||
|
||||
66
hw6/llprograms/sp24_hw3/vishnuv_shantz_kadane.ll
Normal file
66
hw6/llprograms/sp24_hw3/vishnuv_shantz_kadane.ll
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
; To try out different cases, add them below as global and replace the values in the getelementptr in @main with the new globals.
|
||||
@input_array1 = global [8 x i64] [i64 -2, i64 -3, i64 4, i64 -1, i64 -2, i64 1, i64 5, i64 -3]
|
||||
@size1 = global i64 8
|
||||
|
||||
@input_array2 = global [5 x i64] [i64 1, i64 2, i64 3, i64 -2, i64 5]
|
||||
@size2 = global i64 5
|
||||
|
||||
@input_array3 = global [9 x i64] [i64 -2, i64 1, i64 -3, i64 4, i64 -1, i64 2, i64 1, i64 -5, i64 4]
|
||||
@size3 = global i64 9
|
||||
|
||||
define i64 @max_of_two(i64 %num1, i64 %num2) {
|
||||
entry:
|
||||
%cmp = icmp sgt i64 %num1, %num2
|
||||
br i1 %cmp, label %return_num1, label %return_num2
|
||||
return_num1:
|
||||
ret i64 %num1
|
||||
return_num2:
|
||||
ret i64 %num2
|
||||
}
|
||||
|
||||
define i64 @kadane(i64* %array, i64 %length) {
|
||||
%max_so_far = alloca i64
|
||||
%max_ending_here = alloca i64
|
||||
store i64 -2147483648, i64* %max_so_far
|
||||
store i64 0, i64* %max_ending_here
|
||||
|
||||
%i = alloca i64
|
||||
store i64 0, i64* %i
|
||||
br label %loop
|
||||
|
||||
loop:
|
||||
%i_val = load i64, i64* %i
|
||||
%cond = icmp slt i64 %i_val, %length
|
||||
br i1 %cond, label %body, label %end
|
||||
|
||||
body:
|
||||
%current = getelementptr i64, i64* %array, i64 %i_val
|
||||
%current_val = load i64, i64* %current
|
||||
%max_ending_here_val = load i64, i64* %max_ending_here
|
||||
%new_max_ending_here = add i64 %max_ending_here_val, %current_val
|
||||
%max_ending_here_next = call i64 @max_of_two(i64 %new_max_ending_here, i64 0)
|
||||
store i64 %max_ending_here_next, i64* %max_ending_here
|
||||
|
||||
%max_so_far_val = load i64, i64* %max_so_far
|
||||
%max_so_far_next = call i64 @max_of_two(i64 %max_ending_here_next, i64 %max_so_far_val)
|
||||
store i64 %max_so_far_next, i64* %max_so_far
|
||||
|
||||
%i_next = add i64 %i_val, 1
|
||||
store i64 %i_next, i64* %i
|
||||
br label %loop
|
||||
|
||||
end:
|
||||
%result = load i64, i64* %max_so_far
|
||||
ret i64 %result
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %argv) {
|
||||
; CHANGE THE VALUES IN THE NEXT TWO LINES FOR NEW TEST CASES
|
||||
; Remember to change the sizes of the input array in the first gep too
|
||||
%array_ptr = getelementptr [9 x i64], [9 x i64]* @input_array3, i64 0, i64 0
|
||||
%size_ptr = getelementptr i64, i64* @size3, i64 0
|
||||
|
||||
%size_val = load i64, i64* %size_ptr
|
||||
%main_result = call i64 @kadane(i64* %array_ptr, i64 %size_val)
|
||||
ret i64 %main_result
|
||||
}
|
||||
155
hw6/llprograms/sp24_hw3/xuselina_artemisl.ll
Normal file
155
hw6/llprograms/sp24_hw3/xuselina_artemisl.ll
Normal file
|
|
@ -0,0 +1,155 @@
|
|||
%array_ty = type [7 x i64]
|
||||
|
||||
@array = global %array_ty [i64 23, i64 10, i64 20, i64 11, i64 12, i64 6, i64 7]
|
||||
@sorted_array = global %array_ty [i64 6, i64 7, i64 10, i64 11, i64 12, i64 20, i64 23]
|
||||
|
||||
define void @flip(%array_ty* %array, i64 %len) {
|
||||
%start = alloca i64
|
||||
%end = alloca i64
|
||||
%temp = alloca i64
|
||||
store i64 0, i64* %start
|
||||
store i64 %len, i64* %end
|
||||
br label %setup_flip
|
||||
|
||||
setup_flip:
|
||||
%1 = load i64, i64* %start
|
||||
%2 = load i64, i64* %end
|
||||
%cmp1 = icmp slt i64 %1, %2
|
||||
br i1 %cmp1, label %flip_op, label %end_flip
|
||||
|
||||
flip_op:
|
||||
%3 = load i64, i64* %start
|
||||
%s_val_ptr = getelementptr %array_ty, %array_ty* %array, i32 0, i64 %3
|
||||
%s_val = load i64, i64* %s_val_ptr
|
||||
store i64 %s_val, i64* %temp
|
||||
%4 = load i64, i64* %end
|
||||
%e_val_ptr = getelementptr %array_ty, %array_ty* %array, i32 0, i64 %4
|
||||
%e_val = load i64, i64* %e_val_ptr
|
||||
store i64 %e_val, i64* %s_val_ptr
|
||||
%5 = load i64, i64* %temp
|
||||
store i64 %5, i64* %e_val_ptr
|
||||
%6 = load i64, i64* %start
|
||||
%7 = add i64 %6, 1
|
||||
store i64 %7, i64* %start
|
||||
%8 = load i64, i64* %end
|
||||
%9 = sub i64 %8, 1
|
||||
store i64 %9, i64* %end
|
||||
br label %setup_flip
|
||||
|
||||
end_flip:
|
||||
ret void
|
||||
}
|
||||
|
||||
|
||||
define i64 @findMax(%array_ty* %array, i64 %len) {
|
||||
%index = alloca i64
|
||||
%max = alloca i64
|
||||
%temp = alloca i64
|
||||
store i64 0, i64* %index
|
||||
br label %setup_cmp
|
||||
|
||||
setup_cmp:
|
||||
%1 = load i64, i64* %index
|
||||
%cmp2 = icmp slt i64 %1, %len
|
||||
br i1 %cmp2, label %compare, label %end
|
||||
|
||||
compare:
|
||||
%2 = load i64, i64* %index
|
||||
%ptr = getelementptr %array_ty, %array_ty* %array, i32 0, i64 %2
|
||||
%val = load i64, i64* %ptr
|
||||
%3 = load i64, i64* %max
|
||||
%cmp3 = icmp slt i64 %val, %3
|
||||
br i1 %cmp3, label %end, label %update_max
|
||||
|
||||
update_max:
|
||||
%4 = load i64, i64* %temp
|
||||
store i64 %4, i64* %ptr
|
||||
%5 = load i64, i64* %max
|
||||
store i64 %5, i64* %temp
|
||||
%6 = load i64, i64* %index
|
||||
%7 = add i64 %6, 1
|
||||
store i64 %7, i64* %index
|
||||
br label %setup_cmp
|
||||
|
||||
end:
|
||||
%8 = load i64, i64* %max
|
||||
ret i64 %8
|
||||
}
|
||||
|
||||
define void @pancakeSort(%array_ty %array, i64 %len) {
|
||||
%index = alloca i64
|
||||
store i64 %len, i64* %index
|
||||
br label %start_pancake
|
||||
|
||||
start_pancake:
|
||||
%1 = load i64, i64* %index
|
||||
%cmp3 = icmp sgt i64 %1, 1
|
||||
br i1 %cmp3, label %pancake_op, label %end_pancake
|
||||
|
||||
pancake_op:
|
||||
%max = call i64 @findMax(%array_ty* @array, i64 %len)
|
||||
%in = load i64, i64* %index
|
||||
%index_m_1 = sub i64 %in, 1
|
||||
%cmp4 = icmp ne i64 %max, %index_m_1
|
||||
br i1 %cmp4, label %flip, label %end_flip
|
||||
|
||||
flip:
|
||||
call void @flip(%array_ty* @array, i64 %max)
|
||||
call void @flip(%array_ty* @array, i64 %index_m_1)
|
||||
br label %end_flip
|
||||
|
||||
|
||||
end_flip:
|
||||
%2 = load i64, i64* %index
|
||||
%3 = sub i64 %2, 1
|
||||
store i64 %2, i64* %index
|
||||
br label %start_pancake
|
||||
|
||||
end_pancake:
|
||||
ret void
|
||||
|
||||
}
|
||||
|
||||
define i64 @isEqual(%array_ty* %array, %array_ty* %sorted_array) {
|
||||
%index = alloca i64
|
||||
store i64 0, i64* %index
|
||||
br label %iterate
|
||||
|
||||
iterate:
|
||||
%1 = load i64, i64* %index
|
||||
%cmp5 = icmp slt i64 %1, 7
|
||||
|
||||
br i1 %cmp5, label %check, label %equal
|
||||
|
||||
check:
|
||||
%2 = load i64, i64* %index
|
||||
%ptr = getelementptr %array_ty, %array_ty* %array, i32 0, i64 %2
|
||||
%val = load i64, i64* %ptr
|
||||
%3 = load i64, i64* %index
|
||||
%ptr2 = getelementptr %array_ty, %array_ty* %sorted_array, i32 0, i64 %3
|
||||
%val2 = load i64, i64* %ptr2
|
||||
%cmp6 = icmp eq i64 %val, %val2
|
||||
br i1 %cmp6, label %move_pointer, label %not_equal
|
||||
|
||||
move_pointer:
|
||||
%4 = load i64, i64* %index
|
||||
%5 = add i64 %4, 1
|
||||
store i64 %5, i64* %index
|
||||
|
||||
br label %iterate
|
||||
|
||||
equal:
|
||||
ret i64 0
|
||||
|
||||
not_equal:
|
||||
ret i64 1
|
||||
|
||||
}
|
||||
|
||||
define i64 @main(i64 %argc, i8** %arcv) {
|
||||
;call void @pancakeSort(%array_ty* @array, i64 7)
|
||||
;%result = call i64 @isEqual(%array_ty* @array, %array_ty* @sorted_array)
|
||||
;ret i64 %result
|
||||
ret i64 0
|
||||
|
||||
}
|
||||
40
hw6/llprograms/sp24_hw3/zhijing_rodrigo.ll
Normal file
40
hw6/llprograms/sp24_hw3/zhijing_rodrigo.ll
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
@matrixA = global [4 x i64] [i64 1, i64 2, i64 3, i64 4]
|
||||
@matrixB = global [4 x i64] [i64 5, i64 6, i64 7, i64 8]
|
||||
|
||||
define i64 @multiply(i64 %a, i64 %b) {
|
||||
%mul = mul i64 %a, %b
|
||||
ret i64 %mul
|
||||
}
|
||||
|
||||
define i64 @main() {
|
||||
%result = alloca i64
|
||||
store i64 0, i64* %result
|
||||
|
||||
%i = alloca i64
|
||||
store i64 0, i64* %i
|
||||
br label %loop
|
||||
|
||||
loop:
|
||||
%iVal = load i64, i64* %i
|
||||
%cond = icmp slt i64 %iVal, 4
|
||||
br i1 %cond, label %body, label %end
|
||||
|
||||
body:
|
||||
%aElemPtr = getelementptr [4 x i64], [4 x i64]* @matrixA, i64 0, i64 %iVal
|
||||
%bElemPtr = getelementptr [4 x i64], [4 x i64]* @matrixB, i64 0, i64 %iVal
|
||||
%aElem = load i64, i64* %aElemPtr
|
||||
%bElem = load i64, i64* %bElemPtr
|
||||
%product = call i64 @multiply(i64 %aElem, i64 %bElem)
|
||||
|
||||
%acc = load i64, i64* %result
|
||||
%newAcc = add i64 %acc, %product
|
||||
store i64 %newAcc, i64* %result
|
||||
|
||||
%nextVal = add i64 %iVal, 1
|
||||
store i64 %nextVal, i64* %i
|
||||
br label %loop
|
||||
|
||||
end:
|
||||
%finalResult = load i64, i64* %result
|
||||
ret i64 %finalResult
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue