Skip to content

Instantly share code, notes, and snippets.

@lwakefield
Last active July 2, 2019 20:26
Show Gist options
  • Save lwakefield/241bb1bf0ef9ec58da0de88a9d58f3b6 to your computer and use it in GitHub Desktop.
Save lwakefield/241bb1bf0ef9ec58da0de88a9d58f3b6 to your computer and use it in GitHub Desktop.
%AnonEnv1 = type {
i32,
i32
}
%AnonFn1 = type {
i32*,
i32*
}
define i32 @anon_1(i32*) {
; unpack the env
%this = bitcast i32* %0 to %AnonEnv1*
%2 = getelementptr %AnonEnv1, %AnonEnv1* %this, i32 0, i32 0
%3 = getelementptr %AnonEnv1, %AnonEnv1* %this, i32 0, i32 1
%4 = load i32, i32* %2
%5 = load i32, i32* %3
%6 = add i32 %4, %5
ret i32 %6
}
define i32 @main() {
; ignore these first two instructions...
%1 = add i32 0, 1
%2 = add i32 0, 2
; set up the closure
%add = alloca %AnonFn1*
%3 = load %AnonFn1*, %AnonFn1** %add
; load the fn into the closure
%4 = getelementptr %AnonFn1, %AnonFn1* %3, i32 0, i32 0
%5 = bitcast i32 (i32*)* @anon_1 to i32*
store i32* %5, i32** %4
; package the env
%6 = alloca %AnonEnv1
%7 = getelementptr %AnonEnv1, %AnonEnv1* %6, i32 0, i32 0
store i32 2, i32* %7
%8 = getelementptr %AnonEnv1, %AnonEnv1* %6, i32 0, i32 1
store i32 1, i32* %8
; load the env into the closure
%9 = getelementptr %AnonFn1, %AnonFn1* %3, i32 0, i32 1
%10 = bitcast %AnonEnv1* %6 to i32*
store i32* %10, i32** %9
; some time passes...
; unpack the fn
%11 = getelementptr %AnonFn1, %AnonFn1* %3, i32 0, i32 0
%12 = load i32*, i32** %11
%13 = bitcast i32* %12 to i32 (i32*)*
; get pointer to the env
%14 = getelementptr %AnonFn1, %AnonFn1* %3, i32 0, i32 1
%15 = load i32*, i32** %14
%16 = call i32 %13 (i32* %15)
ret i32 %16
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment