Skip to content

Instantly share code, notes, and snippets.

@shimomura1004
Created August 4, 2014 13:59
Show Gist options
  • Save shimomura1004/06602231b7819e5a2990 to your computer and use it in GitHub Desktop.
Save shimomura1004/06602231b7819e5a2990 to your computer and use it in GitHub Desktop.
factor function in llvm assembler
@str = internal constant [4 x i8] c"%d\0a\00"
declare i32 @printf(i8*, ...)
define i32 @factor(i32 %n) {
head:
%cast_message = getelementptr [4 x i8]* @str, i32 0, i32 0
%i = alloca i32
store i32 2, i32* %i
br label %loop
loop:
%val = load i32* %i
%rem = urem i32 %n, %val
%isdivider = icmp eq i32 %rem, 0
br i1 %isdivider, label %print, label %next
print:
call i32 (i8*, ...)* @printf(i8* %cast_message, i32 %val)
br label %next
next:
%ival = add i32 %val, 1
store i32 %ival, i32* %i
%b = icmp sle i32 %ival, %n
br i1 %b, label %loop, label %tail
tail:
ret i32 0
}
define i32 @main() {
call i32(i32)* @factor(i32 40)
ret i32 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment