Last active
September 28, 2021 15:59
-
-
Save brecert/4b8cb76d7565b8e6ecf944046300d481 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; declare { printf } = @cimport(@cinclude("stdio.h")) | |
; | |
; fn square(num: i32) { | |
; #start: { | |
; let #safe_mult = num *? num | |
; let [ #mult_val, #was_safe ] = #safe_mult | |
; let #should_panic = #was_safe == false | |
; #should_panic ? goto #panic : goto #return | |
; } | |
; | |
; #panic: { | |
; // @panic(`info`) | |
; unreachable | |
; } | |
; | |
; #return: { | |
; return #mult_value | |
; } | |
; } | |
; | |
; fn main { | |
; #start: { | |
; #square_result = square(56) | |
; printf("%d", #square_result) | |
; goto #return | |
; } | |
; | |
; #return: { | |
; return void | |
; } | |
; } | |
declare i32 @printf(i8*, ...) | |
define i32 @square(i32 %num) { | |
start: | |
%safe_mult = call { i32, i1 } @llvm.smul.with.overflow.i32(i32 %num, i32 %num) | |
%mult_val = extractvalue { i32, i1 } %safe_mult, 0 | |
%was_safe = extractvalue { i32, i1 } %safe_mult, 1 | |
%should_panic = call i1 @llvm.expect.i1(i1 %was_safe, i1 false) | |
br i1 %should_panic, label %panic, label %return | |
panic: | |
; call @panic message | |
unreachable | |
return: | |
ret i32 %mult_val | |
} | |
@.str = private unnamed_addr constant [3 x i8] c"%d\00", align 1 | |
define void @main() { | |
start: | |
%square_result = call i32 @square (i32 36) | |
call i32 (i8*, ...) @printf(i8* getelementptr ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %square_result) | |
br label %return | |
return: | |
ret void | |
} | |
declare i1 @llvm.expect.i1(i1, i1) | |
declare { i32, i1 } @llvm.smul.with.overflow.i32(i32, i32) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment