Skip to content

Instantly share code, notes, and snippets.

@keveman
Created April 19, 2019 19:17
Show Gist options
  • Save keveman/9286301bbdfb76f4bfa249256fd363e1 to your computer and use it in GitHub Desktop.
Save keveman/9286301bbdfb76f4bfa249256fd363e1 to your computer and use it in GitHub Desktop.
Zig inline example
const C = struct {
x : u16,
inline fn post_inc(self: *C) u16 {
const tmp = self.*.x;
self.*.x += 1;
return tmp;
}
};
var c : C = C { .x = 0 };
export fn bar() u16 {
const x = c.post_inc();
const y = c.post_inc();
return x + y;
}
export fn baz() u16 {
const x = c.post_inc();
const y = c.post_inc();
const z = c.post_inc();
return x + y + z;
}
//////// bar()
define i16 @bar() #2 !dbg !37 {
Entry:
%x = alloca i16, align 2
%y = alloca i16, align 2
%0 = call fastcc i16 @C.post_inc(%C* @c), !dbg !46
store i16 %0, i16* %x, align 2, !dbg !47
call void @llvm.dbg.declare(metadata i16* %x, metadata !42, metadata !DIExpression()), !dbg !47
%1 = call fastcc i16 @C.post_inc(%C* @c), !dbg !48
store i16 %1, i16* %y, align 2, !dbg !49
call void @llvm.dbg.declare(metadata i16* %y, metadata !44, metadata !DIExpression()), !dbg !49
%2 = load i16, i16* %x, align 2, !dbg !50
%3 = load i16, i16* %y, align 2, !dbg !52
%4 = add nuw i16 %2, %3, !dbg !53
ret i16 %4, !dbg !54
}
//////// baz()
define i16 @baz() #2 !dbg !77 {
Entry:
%x = alloca i16, align 2
%y = alloca i16, align 2
%z = alloca i16, align 2
%0 = call fastcc i16 @C.post_inc(%C* @c), !dbg !85
store i16 %0, i16* %x, align 2, !dbg !86
call void @llvm.dbg.declare(metadata i16* %x, metadata !79, metadata !DIExpression()), !dbg !86
%1 = call fastcc i16 @C.post_inc(%C* @c), !dbg !87
store i16 %1, i16* %y, align 2, !dbg !88
call void @llvm.dbg.declare(metadata i16* %y, metadata !81, metadata !DIExpression()), !dbg !88
%2 = call fastcc i16 @C.post_inc(%C* @c), !dbg !89
store i16 %2, i16* %z, align 2, !dbg !90
call void @llvm.dbg.declare(metadata i16* %z, metadata !83, metadata !DIExpression()), !dbg !90
%3 = load i16, i16* %x, align 2, !dbg !91
%4 = load i16, i16* %y, align 2, !dbg !93
%5 = add nuw i16 %3, %4, !dbg !94
%6 = load i16, i16* %z, align 2, !dbg !95
%7 = add nuw i16 %5, %6, !dbg !96
ret i16 %7, !dbg !97
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment