Skip to content

Instantly share code, notes, and snippets.

@harlanhaskins
Created February 17, 2016 23:18
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 harlanhaskins/9641fb6e89f375b8b034 to your computer and use it in GitHub Desktop.
Save harlanhaskins/9641fb6e89f375b8b034 to your computer and use it in GitHub Desktop.
; ModuleID = '/Users/harlanhaskins/fact.ltr'
target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-apple-darwin15.0.0"
@printformat = private constant [3 x i8] c"%d\00", align 1
@printlnformat = private constant [4 x i8] c"%d\0A\00", align 1
declare i64 @printf(i8*, ...)
define private i64 @print(i64 %arg) {
entry:
%calltmp = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @printformat, i64 0, i64 0), i64 %arg)
ret i64 %arg
}
define private i64 @println(i64 %arg) {
entry:
%calltmp = call i64 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @printlnformat, i64 0, i64 0), i64 %arg)
ret i64 %arg
}
define private i64 @factorial(i64 %n) {
entry:
%n1 = alloca i64
store i64 %n, i64* %n1
%n2 = load i64, i64* %n1
%eqtmp = icmp eq i64 %n2, 0
%0 = sext i1 %eqtmp to i64
%ifcond = icmp ne i64 %0, 0
br i1 %ifcond, label %then, label %else
then: ; preds = %entry
br label %ifcont
else: ; preds = %entry
%n3 = load i64, i64* %n1
%n4 = load i64, i64* %n1
%subtmp = sub i64 %n4, 1
%calltmp = call i64 @factorial(i64 %subtmp)
%multmp = mul i64 %n3, %calltmp
br label %ifcont
ifcont: ; preds = %else, %then
%iftmp = phi i64 [ 1, %then ], [ %multmp, %else ]
ret i64 %iftmp
}
define i64 @main() {
entry:
ret i64 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment