Skip to content

Instantly share code, notes, and snippets.

@wbrown
Created November 11, 2013 22:42
Show Gist options
  • Save wbrown/7421865 to your computer and use it in GitHub Desktop.
Save wbrown/7421865 to your computer and use it in GitHub Desktop.
With noreturn added into the calls.
; ModuleID = 'bugpoint-reduced-simplified.bc'
target triple = "x86_64-apple-darwin13.0.0"
@valueString = internal constant [7 x i8] c"%llu\0D\0A\00"
@stackString = internal constant [13 x i8] c"%llu: %llu\0D\0A\00"
@heapPtr = weak global i64* null
@heapSize = weak global i64 0
@execIdx = weak global i64 0
@stackIdx = weak global i64 0
@currIns = weak global i64 0
declare i32 @puts(i8*)
declare i32 @read(i64, i8*, i64)
declare i32 @printf(i8*, ...)
define void @printValue32(i32 %value) {
%string = getelementptr [7 x i8]* @valueString, i32 0, i32 0
%printf_ret = call i32 (i8*, ...)* @printf(i8* %string, i32 %value)
ret void
}
define void @printValue64(i64 %value) {
%string = getelementptr [7 x i8]* @valueString, i32 0, i32 0
%printf_ret = call i32 (i8*, ...)* @printf(i8* %string, i64 %value)
ret void
}
define void @printValueInt(i64 %value) {
%string = getelementptr [7 x i8]* @valueString, i32 0, i32 0
%printf_ret = call i32 (i8*, ...)* @printf(i8* %string, i64 %value)
ret void
}
define void @printValueCell(i64 %value) {
%string = getelementptr [7 x i8]* @valueString, i32 0, i32 0
%printf_ret = call i32 (i8*, ...)* @printf(i8* %string, i64 %value)
ret void
}
define i64* @getHeap_ptr(i64 %index) {
%heapPtr = load i64** @heapPtr
%valuePtr = getelementptr i64* %heapPtr, i64 %index
ret i64* %valuePtr
}
define i64 @getHeap(i64 %index) {
%valuePtr = call i64* @getHeap_ptr(i64 %index)
%value = load i64* %valuePtr
ret i64 %value
}
define void @putHeap(i64 %index, i64 %value) {
%valuePtr = call i64* @getHeap_ptr(i64 %index)
store i64 %value, i64* %valuePtr
ret void
}
define i64 @nextExec() {
%execIdx = load i64* @execIdx
%ins = call i64 @getHeap(i64 %execIdx)
%nextExec = add i64 %execIdx, 1
store i64 %nextExec, i64* @execIdx
ret i64 %ins
}
define void @insertToken(i64 %index, void ()* %token) {
%insPtr = call i64* @getHeap_ptr(i64 %index)
%tokenPtrInt = ptrtoint void ()* %token to i64
call void @putHeap(i64 %index, i64 %tokenPtrInt)
ret void
}
define void @pushStack(i64 %value) {
%stackIdx = load i64* @stackIdx
%newStackIdx = sub i64 %stackIdx, 1
call void @putHeap(i64 %newStackIdx, i64 %value)
store i64 %newStackIdx, i64* @stackIdx
ret void
}
define i64 @popStack() {
%stackIdx = load i64* @stackIdx
%value = call i64 @getHeap(i64 %stackIdx)
%newStackIdx = add i64 %stackIdx, 1
store i64 %newStackIdx, i64* @stackIdx
ret i64 %value
}
define i64 @getTopStack() {
%stackIdx = load i64* @stackIdx
%value = call i64 @getHeap(i64 %stackIdx)
ret i64 %value
}
define void @next() {
%ins = call i64 @nextExec()
%is_done = icmp eq i64 %ins, 0
br i1 %is_done, label %done, label %execIns
execIns: ; preds = %0
%functionPtr = inttoptr i64 %ins to void ()*
call void %functionPtr() #0
ret void
done: ; preds = %0
ret void
}
define void @showStack() {
%stack_string = getelementptr [13 x i8]* @stackString, i64 0, i64 0
%currStackIdx = alloca i64
%getStackIdx = load i64* @stackIdx
store i64 %getStackIdx, i64* %currStackIdx
%heapSize = load i64* @heapSize
br label %loop
loop: ; preds = %continue_loop, %0
%stackIdx = load i64* %currStackIdx
%is_done = icmp uge i64 %stackIdx, %heapSize
br i1 %is_done, label %done, label %continue_loop
continue_loop: ; preds = %loop
%currStackValue = call i64 @getHeap(i64 %stackIdx)
%relStackIdx = sub i64 %heapSize, %stackIdx
%1 = call i32 (i8*, ...)* @printf(i8* %stack_string, i64 %relStackIdx, i64 %currStackValue)
%newStackIdx = add i64 %stackIdx, 1
store i64 %newStackIdx, i64* %currStackIdx
br label %loop
done: ; preds = %loop
ret void
}
define void @SWAP() {
%first = call i64 @popStack()
%second = call i64 @popStack()
call void @pushStack(i64 %first)
call void @pushStack(i64 %second)
call void @next() #0
ret void
}
define void @DUP() {
%first = call i64 @getTopStack()
call void @pushStack(i64 %first)
call void @next() #0
ret void
}
define void @ADD() {
%first = call i64 @popStack()
%second = call i64 @popStack()
%result = add i64 %first, %second
call void @pushStack(i64 %result)
call void @next() #0
ret void
}
define void @SUB() {
%first = call i64 @popStack()
%second = call i64 @popStack()
%result = sub i64 %second, %first
call void @pushStack(i64 %result)
call void @next() #0
ret void
}
define void @MUL() {
%first = call i64 @popStack()
%second = call i64 @popStack()
%result = mul i64 %first, %second
call void @pushStack(i64 %result)
call void @next() #0
ret void
}
define void @DIV() {
%first = call i64 @popStack()
%second = call i64 @popStack()
%result = udiv i64 %second, %first
call void @pushStack(i64 %result)
call void @next() #0
ret void
}
define void @DISPSTACK() {
call void @showStack()
call void @next() #0
ret void
}
define i64 @main() {
%heapSizePtr = alloca i64
store i64 0, i64* %heapSizePtr
%initHeapSize = load i64* %heapSizePtr
%heapSize = add i64 %initHeapSize, 1048576
store i64 %heapSize, i64* @heapSize
store i64 %heapSize, i64* @stackIdx
store i64 0, i64* @execIdx
%heapPtr = alloca i64, i64 %heapSize
store i64* %heapPtr, i64** @heapPtr
call void @pushStack(i64 1)
call void @pushStack(i64 2)
call void @pushStack(i64 3)
call void @insertToken(i64 0, void ()* @DISPSTACK)
call void @insertToken(i64 1, void ()* @ADD)
call void @insertToken(i64 2, void ()* @SWAP)
call void @insertToken(i64 3, void ()* @DISPSTACK)
call void @insertToken(i64 4, void ()* @SWAP)
call void @insertToken(i64 5, void ()* @DUP)
call void @insertToken(i64 6, void ()* @DUP)
call void @insertToken(i64 7, void ()* @MUL)
call void @insertToken(i64 8, void ()* @DISPSTACK)
call void @next()
ret i64 0
}
attributes #0 = { noreturn }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment