Skip to content

Instantly share code, notes, and snippets.

@wbrown
Last active December 28, 2015 08:19
Show Gist options
  • Save wbrown/7471135 to your computer and use it in GitHub Desktop.
Save wbrown/7471135 to your computer and use it in GitHub Desktop.
I am not sure why, but values that I push onto the stack are disappearing past the last value I pushed. Output:
; clouddrift:experiments wbrown$ lli-3.3 test.ll
; 10 --> @140734656409551
; 11 --> @140734656409550
; 12 --> @140734656409549
; @140734656409549 --> 12
; @140734656409550 --> 0
; @140734656409551 --> 0
%cell = type i64
%cell.ptr = type i64*
%exec = type i64
%exec.ptr = type i64*
%ret = type i64
%ret.ptr = type i64*
%int = type i64
%addr = type i64
declare i32 @printf(i8*, ... )
@valueString = internal constant [7 x i8] c"%llu\0D\0A\00"
@pushString = internal constant [17 x i8] c"%llu --> @%llu\0D\0A\00"
@popString = internal constant [17 x i8] c"@%llu --> %llu\0D\0A\00"
define void @printStackPop(%int %addr, %int %value) {
%string = getelementptr [17 x i8]* @popString, i32 0, i32 0
%printf_ret = call i32 (i8*, ... )* @printf(i8* %string,
%int %addr, %int %value)
ret void
}
define void @printStackPush(%int %addr, %int %value) {
%string = getelementptr [17 x i8]* @pushString, i32 0, i32 0
%printf_ret = call i32 (i8*, ... )* @printf(i8* %string,
%int %value, %int %addr)
ret void
}
define void @printValueInt(%int %value) {
%string = getelementptr [7 x i8]* @valueString, i32 0, i32 0
%printf_ret = call i32 (i8*, ... )* @printf(i8* %string, %int %value)
ret void
}
; POP from our stack, haskell calling conventions with four registers
define cc 10 void @_SP_POP(%cell.ptr* %SP.ptr.ptr, %exec.ptr* %EIP.ptr.ptr,
%ret.ptr* %RSP.ptr.ptr, %cell* %DATA.ptr) {
; obtain the stack value that SP is pointing at
%SP.ptr = getelementptr %cell.ptr* %SP.ptr.ptr, i32 0
%value.ptr = load %cell.ptr* %SP.ptr
%value.cell.ptr = getelementptr %cell.ptr %value.ptr, i32 0
%value.cell = load %cell* %value.cell.ptr
store %cell %value.cell, %cell* %DATA.ptr
; increment SP
%SP.ptr.int = ptrtoint %cell.ptr %value.cell.ptr to %addr
%SP.next.ptr.int = add %addr %SP.ptr.int, 1
%SP.next.ptr = inttoptr %addr %SP.next.ptr.int to %cell.ptr
store %cell.ptr %SP.next.ptr, %cell.ptr* %SP.ptr.ptr
call void @printStackPop(%addr %SP.ptr.int, %cell %value.cell)
ret void
}
; PUSH to our stack, haskell calling conventions with four register pointers
define cc 10 void @_SP_PUSH(%cell.ptr* %SP.ptr.ptr, %exec.ptr* %EIP.ptr.ptr,
%ret.ptr* %RSP.ptr.ptr, %cell* %DATA.ptr) {
; obtain the stack position that SP is pointing at
%SP.ptr = getelementptr %cell.ptr* %SP.ptr.ptr, i32 0
%value.ptr = load %cell.ptr* %SP.ptr
%value.cell.ptr = getelementptr %cell.ptr %value.ptr, i32 0
; decrement SP
%SP.ptr.int = ptrtoint %cell.ptr %value.cell.ptr to %addr
%SP.next.ptr.int = sub %addr %SP.ptr.int, 1
%SP.next.ptr = inttoptr %addr %SP.next.ptr.int to %cell.ptr
store %cell.ptr %SP.next.ptr, %cell.ptr* %SP.ptr.ptr
; get our value to store and store in the new stack position
%DATA.value = load %cell* %DATA.ptr
store %cell %DATA.value, %cell* %SP.next.ptr
call void @printStackPush(%addr %SP.next.ptr.int, %cell %DATA.value)
ret void
}
define %int @main() {
%SP = alloca %cell.ptr
%EIP = alloca %cell.ptr
%RSP = alloca %cell.ptr
%DATA = alloca %cell
%heap = alloca %cell, i32 1024
%SP.ptr = getelementptr %cell.ptr %heap, i32 1023
store %cell.ptr %SP.ptr, %cell.ptr* %SP
%EIP.ptr = getelementptr %cell.ptr %heap, i32 0
store %cell.ptr %EIP.ptr, %cell.ptr* %EIP
%RSP.ptr = getelementptr %cell.ptr %heap, i32 511
store %cell.ptr %RSP.ptr, %cell.ptr* %RSP
store %cell 2, %cell.ptr %EIP.ptr
store %cell 3, %cell.ptr %RSP.ptr
; push 10
store %cell 10, %cell.ptr %DATA
call cc 10 void @_SP_PUSH(%cell.ptr* %SP, %cell.ptr* %EIP,
%cell.ptr* %RSP, %cell* %DATA)
; push 11
store %cell 11, %cell.ptr %DATA
call cc 10 void @_SP_PUSH(%cell.ptr* %SP, %cell.ptr* %EIP,
%cell.ptr* %RSP, %cell* %DATA)
; push 12
store %cell 12, %cell.ptr %DATA
call cc 10 void @_SP_PUSH(%cell.ptr* %SP, %cell.ptr* %EIP,
%cell.ptr* %RSP, %cell* %DATA)
; pop 12
call cc 10 void @_SP_POP(%cell.ptr* %SP, %cell.ptr* %EIP,
%cell.ptr* %RSP, %cell* %DATA)
; pop 11
call cc 10 void @_SP_POP(%cell.ptr* %SP, %cell.ptr* %EIP,
%cell.ptr* %RSP, %cell* %DATA)
; pop 10
call cc 10 void @_SP_POP(%cell.ptr* %SP, %cell.ptr* %EIP,
%cell.ptr* %RSP, %cell* %DATA)
ret %int 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment