Skip to content

Instantly share code, notes, and snippets.

@ytomino
Created May 10, 2011 16:36
Show Gist options
  • Save ytomino/964841 to your computer and use it in GitHub Desktop.
Save ytomino/964841 to your computer and use it in GitHub Desktop.
LLVM trampoline test
; llvm-as trampoline.ll && llvm-ld trampoline.bc && ./a.out
declare i8* @llvm.init.trampoline(i8*, i8*, i8*)
declare i32 @printf(i8*, ...)
declare void @__enable_execute_stack(i8*) ; libgcc
@flat.fmt = private constant [4 x i8] c"%s\0a\00"
define void @flat(i8* %x) {
%fmt = getelementptr [4 x i8]* @flat.fmt, i32 0, i32 0
call i32 (i8*, ...)* @printf(i8* %fmt, i8* %x)
ret void
}
@nested.fmt = private constant [7 x i8] c"%s,%s\0a\00"
define void @nested(i8* nest %context, i8* %x) {
%fmt = getelementptr [7 x i8]* @nested.fmt, i32 0, i32 0
call i32 (i8*, ...)* @printf(i8* %fmt, i8* %context, i8* %x)
ret void
}
@bob = private constant [4 x i8] c"Bob\00"
@alice = private constant [6 x i8] c"Alice\00"
@eve = private constant [4 x i8] c"Eve\00"
define void @iterate(void (i8*)* %f) noinline {
call void %f(i8* getelementptr ([4 x i8]* @bob, i32 0, i32 0))
call void %f(i8* getelementptr ([6 x i8]* @alice, i32 0, i32 0))
call void %f(i8* getelementptr ([4 x i8]* @eve, i32 0, i32 0))
ret void
}
@info = private constant [7 x i8] c"nested\00"
define i32 @main() {
call void @iterate(void (i8*)* @flat)
%context = getelementptr [7 x i8]* @info, i32 0, i32 0
%work = alloca [10 x i8], align 4
%work.first = getelementptr [10 x i8]* %work, i32 0, i32 0
%trampoline = call i8* @llvm.init.trampoline(
i8* %work.first,
i8* bitcast (void (i8* nest, i8*)* @nested to i8*),
i8* %context)
call void @__enable_execute_stack(i8* %trampoline)
%nested = bitcast i8* %trampoline to void (i8*)*
call void @iterate(void (i8*)* %nested)
ret i32 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment