Skip to content

Instantly share code, notes, and snippets.

@evanphx
Created January 22, 2009 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evanphx/50428 to your computer and use it in GitHub Desktop.
Save evanphx/50428 to your computer and use it in GitHub Desktop.
struct closure {
int a;
};
void show(int i);
typedef void (*block)(struct closure* data);
void block_code(struct closure* data) {
data->a += 1;
show(data->a);
}
int times(struct closure* data, int self, block blk) {
int i = 0;
while(i < self) {
blk(data);
i += 1;
}
return self;
}
void blah() {
struct closure data = { 10 };
times(&data, 10, block_code);
}
; ModuleID = 'scratch/times.c'
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
target triple = "i386-apple-darwin9"
%struct.closure = type { i32 }
define void @block_code(%struct.closure* %data) nounwind {
entry:
%tmp1 = getelementptr %struct.closure* %data, i32 0, i32 0 ; <i32*> [#uses=2]
%tmp2 = load i32* %tmp1, align 4 ; <i32> [#uses=1]
%tmp3 = add i32 %tmp2, 1 ; <i32> [#uses=2]
store i32 %tmp3, i32* %tmp1, align 4
tail call void @show( i32 %tmp3 ) nounwind
ret void
}
define i32 @times(%struct.closure* %data, i32 %self, void (%struct.closure*)* %blk) nounwind {
entry:
%tmp816 = icmp sgt i32 %self, 0 ; <i1> [#uses=1]
br i1 %tmp816, label %bb.preheader, label %bb10
bb.preheader: ; preds = %entry
%tmp = icmp slt i32 %self, 1 ; <i1> [#uses=1]
%smax = select i1 %tmp, i32 1, i32 %self ; <i32> [#uses=1]
br label %bb
bb: ; preds = %bb, %bb.preheader
%i.0.reg2mem.0 = phi i32 [ 0, %bb.preheader ], [ %indvar.next, %bb ] ; <i32> [#uses=1]
tail call void %blk( %struct.closure* %data ) nounwind
%indvar.next = add i32 %i.0.reg2mem.0, 1 ; <i32> [#uses=2]
%exitcond = icmp eq i32 %indvar.next, %smax ; <i1> [#uses=1]
br i1 %exitcond, label %bb10, label %bb
bb10: ; preds = %bb, %entry
ret i32 %self
}
declare void @show(i32)
define void @blah() nounwind {
entry:
br label %bb
bb: ; preds = %bb, %entry
%i.0.reg2mem.0 = phi i32 [ 0, %entry ], [ %indvar.next, %bb ] ; <i32> [#uses=2]
%tmp3.i = add i32 %i.0.reg2mem.0, 11 ; <i32> [#uses=1]
tail call void @show( i32 %tmp3.i ) nounwind
%indvar.next = add i32 %i.0.reg2mem.0, 1 ; <i32> [#uses=2]
%exitcond = icmp eq i32 %indvar.next, 10 ; <i1> [#uses=1]
br i1 %exitcond, label %bb14, label %bb
bb14: ; preds = %bb
ret void
}
_blah:
pushl %esi
subl $8, %esp
xorl %esi, %esi
.align 4,0x90
LBB3_1: ## bb
leal 11(%esi), %eax
movl %eax, (%esp)
call L_show$stub
incl %esi
cmpl $10, %esi
jne LBB3_1 ## bb
LBB3_2: ## bb14
addl $8, %esp
popl %esi
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment