Last active
July 19, 2020 07:27
-
-
Save shining-corn/5260795897a2c89965710e25f0b80692 to your computer and use it in GitHub Desktop.
LLVM IR Sample Code
This file contains 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
define i32 @main() { | |
br i1 1, label %label1, label %label2 | |
label1: | |
ret i32 0 | |
label2: | |
ret i32 1 | |
} |
This file contains 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
define i32 @main() { | |
%cond = icmp eq i32 111, 111 ; %condは真になる | |
; %cond = icmp eq i32 111, 222 ; こうすると偽になる | |
br i1 %cond, label %label1, label %label2 | |
label1: | |
ret i32 0 | |
label2: | |
ret i32 1 | |
} |
This file contains 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
define i32 @main() { | |
%ptr_return_value = alloca i32 | |
%cond = icmp eq i32 111, 111 | |
br i1 %cond, label %then, label %else | |
then: | |
store i32 1111, i32* %ptr_return_value | |
br label %end | |
else: | |
store i32 2222, i32* %ptr_return_value | |
br label %end | |
end: | |
%return_value = load i32, i32* %ptr_return_value | |
ret i32 %return_value | |
} |
This file contains 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
define i32 @main() { | |
%result = call i32 @func1() | |
ret i32 %result | |
} | |
define i32 @func1() { | |
ret i32 222 | |
} |
This file contains 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
define i32 @func1(i32 %x) { | |
ret i32 %x | |
} | |
define i32 @main() { | |
%result = call i32 @func1(i32 333) | |
ret i32 %result | |
} |
This file contains 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
target triple = "x86_64-pc-windows" | |
; target triple = "x86_64-unknown-linux-gnu" | |
define dso_local i32 @main() !dbg !7 { | |
%x.1 = alloca i32 | |
call void @llvm.dbg.declare(metadata i32* %x.1, metadata !8, metadata !DIExpression()), !dbg !9 | |
store i32 0, i32* %x.1, !dbg !10 | |
store i32 1, i32* %x.1, !dbg !11 | |
store i32 2, i32* %x.1, !dbg !12 | |
%x.2 = load i32, i32* %x.1, !dbg !13 | |
ret i32 %x.2, !dbg !14 | |
} | |
declare void @llvm.dbg.declare(metadata, metadata, metadata) | |
!llvm.dbg.cu = !{!0} | |
!llvm.module.flags = !{!2, !3} | |
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, emissionKind: FullDebug) | |
!1 = !DIFile(filename: "1.c", directory: "./") | |
; !2 = !{i32 2, !"Dwarf Version", i32 4} | |
!2 = !{i32 2, !"CodeView", i32 1} | |
!3 = !{i32 2, !"Debug Info Version", i32 3} | |
!4 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed) | |
!5 = !{!4} | |
!6 = !DISubroutineType(types: !5) | |
!7 = distinct !DISubprogram(name: "main", scope: !1, file: !1, line: 4, type: !6, scopeLine: 1, unit: !0) | |
!8 = !DILocalVariable(name: "x", scope: !7, file: !1, line: 5, type: !4) | |
!9 = !DILocation(line: 6, scope: !7) | |
!10 = !DILocation(line: 7, scope: !7) | |
!11 = !DILocation(line: 8, scope: !7) | |
!12 = !DILocation(line: 9, scope: !7) | |
!13 = !DILocation(line: 10, scope: !7) | |
!14 = !DILocation(line: 11, scope: !7) |
This file contains 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 dllimport i32 @foo(i32) | |
define i32 @main() { | |
%1 = call i32 @foo(i32 111) | |
ret i32 %1 | |
} |
This file contains 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 i32 @putchar(i32) | |
declare i32 @puts(i8*) | |
@fizz = constant [5 x i8] c"fizz\00" | |
@buzz = constant [5 x i8] c"buzz\00" | |
@fizzbuzz = constant [9 x i8] c"fizzbuzz\00" | |
define i32 @main() { | |
%ptr_fizz = getelementptr [5 x i8], [5 x i8]* @fizz, i32 0, i32 0 | |
%ptr_buzz = getelementptr [5 x i8], [5 x i8]* @buzz, i32 0, i32 0 | |
%ptr_fizzbuzz = getelementptr [9 x i8], [9 x i8]* @fizzbuzz, i32 0, i32 0 | |
; int i = 1 | |
%ptr_i = alloca i32 | |
store i32 1, i32* %ptr_i | |
br label %for_cond | |
for_cond: | |
; if (1 <= 100) continue; else break; | |
%i = load i32, i32* %ptr_i | |
%cond_for = icmp ule i32 %i, 100 | |
br i1 %cond_for, label %for_body, label %end | |
for_body: | |
; if (i % 15 == 0) | |
%rem15 = urem i32 %i, 15 | |
%cond_fizzbuzz = icmp eq i32 %rem15, 0 | |
br i1 %cond_fizzbuzz, label %print_fizzbuzz, label %check_buzz | |
check_buzz: | |
; else if (i % 5 == 0) | |
%rem5 = urem i32 %i, 5 | |
%cond_buzz = icmp eq i32 %rem5, 0 | |
br i1 %cond_buzz, label %print_buzz, label %check_fizz | |
check_fizz: | |
; else if (i % 3 == 0) | |
%rem3 = urem i32 %i, 3 | |
%cond_fizz = icmp eq i32 %rem3, 0 | |
br i1 %cond_fizz, label %print_fizz, label %print_number | |
print_fizzbuzz: | |
call i32 @puts(i8* %ptr_fizzbuzz) | |
br label %next | |
print_buzz: | |
call i32 @puts(i8* %ptr_buzz) | |
br label %next | |
print_fizz: | |
call i32 @puts(i8* %ptr_fizz) | |
br label %next | |
print_number: | |
call void @print_unsigned_integer(i32 %i) | |
call i32 @putchar(i32 13) ; '\r' | |
call i32 @putchar(i32 10) ; '\n' | |
br label %next | |
next: | |
%i_next = add i32 %i, 1 ; ` i + 1` | |
store i32 %i_next, i32* %ptr_i ; `i = ` | |
br label %for_cond | |
end: | |
ret i32 0 | |
} | |
; 符号なし整数を標準出力に出力 | |
define void @print_unsigned_integer(i32 %c) { | |
%tmp = alloca i32 | |
%buff = alloca [10 x i32] ; 文字列バッファ | |
%index = alloca i32 ; buffのインデックス | |
store i32 %c, i32* %tmp | |
store i32 0, i32* %index | |
br label %stringify | |
; 1の位から順番にASCIIコード化し、buff配列に格納 | |
stringify: | |
%1 = load i32, i32* %tmp | |
%2 = urem i32 %1, 10 | |
%3 = add i32 %2, 48 | |
%4 = load i32, i32* %index | |
%5 = getelementptr [10 x i32], [10 x i32]* %buff, i32 0, i32 %4 | |
store i32 %3, i32* %5 | |
%6 = udiv i32 %1, 10 | |
store i32 %6, i32* %tmp | |
%7 = add i32 %4, 1 | |
store i32 %7, i32* %index | |
%8 = icmp eq i32 %6, 0 | |
br i1 %8, label %print, label %stringify | |
; buff配列を逆順に出力 | |
print: | |
%9 = load i32, i32* %index | |
%10 = sub i32 %9, 1 | |
%11 = getelementptr [10 x i32], [10 x i32]* %buff, i32 0, i32 %10 | |
%12 = load i32, i32* %11 | |
call i32 @putchar(i32 %12) | |
store i32 %10, i32* %index | |
%14 = icmp eq i32 %10, 0 | |
br i1 %14, label %end, label %print | |
end: | |
ret void | |
} |
This file contains 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
@g_x = constant i32 444 | |
define i32 @main() { | |
%result = load i32, i32* @g_x | |
ret i32 %result | |
} |
This file contains 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
@hello_world = constant [12 x i8] c"hello world\00" | |
declare i32 @puts(i8*) | |
define i32 @main() { | |
%ptr = getelementptr [12 x i8], [12 x i8]* @hello_world, i32 0, i32 0 | |
call i32 @puts(i8* %ptr) | |
ret i32 0 | |
} |
This file contains 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
define i32 @main() { | |
entry: | |
%ptr_i = alloca i32 ; `int i ` | |
store i32 0, i32* %ptr_i ; ` = 0` | |
br label %for_cond | |
for_cond: | |
%i = load i32, i32* %ptr_i | |
%cond = icmp ult i32 %i, 10 ; `i < 10` | |
br i1 %cond, label %for_body, label %end | |
for_body: | |
%i_next = add i32 %i, 1 ; ` i + 1` | |
store i32 %i_next, i32* %ptr_i ; `i = ` | |
br label %for_cond | |
end: | |
ret i32 %i | |
} |
This file contains 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
define i32 @main() { | |
%some_condition = call i1 @func0() | |
br i1 %some_condition, label %then, label %else | |
then: | |
%result = call i32 @func1() | |
br label %end | |
else: | |
%result = call i32 @func2() | |
br label %end | |
end: | |
call i32 @some_function(i32 %result) | |
ret i32 0 | |
} | |
define i1 @func0() { | |
ret i1 1 | |
} | |
define i32 @func1() { | |
ret i32 1 | |
} | |
define i32 @func2() { | |
ret i32 2 | |
} | |
define i32 @some_function(i32 %n) { | |
ret i32 %n | |
} |
This file contains 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
extern "C" __declspec(dllexport) int foo(int n) { | |
return 2 * n; | |
} |
This file contains 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
extern "C" int bar() | |
{ | |
return 12345; | |
} |
This file contains 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
define i32 @main() { | |
br i1 1, label %then, label %else | |
then: | |
%result1 = call i32 @func1() | |
br label %end | |
else: | |
%result2 = call i32 @func2() | |
br label %end | |
end: | |
%result3 = phi i32 [%result1, %then], [%result2, %else] | |
call i32 @some_function(i32 %result3) | |
ret i32 0 | |
} | |
define i32 @func1() { | |
ret i32 1 | |
} | |
define i32 @func2() { | |
ret i32 2 | |
} | |
define i32 @some_function(i32 %n) { | |
ret i32 %n | |
} |
This file contains 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 i32 @putchar(i32) | |
declare i32 @puts(i8*) | |
@fizz = constant [5 x i8] c"fizz\00" | |
@buzz = constant [5 x i8] c"buzz\00" | |
@fizzbuzz = constant [9 x i8] c"fizzbuzz\00" | |
define i32 @main() { | |
entry: | |
%ptr_fizz = getelementptr [5 x i8], [5 x i8]* @fizz, i32 0, i32 0 | |
%ptr_buzz = getelementptr [5 x i8], [5 x i8]* @buzz, i32 0, i32 0 | |
%ptr_fizzbuzz = getelementptr [9 x i8], [9 x i8]* @fizzbuzz, i32 0, i32 0 | |
br label %for_cond | |
for_cond: | |
; `for (int i = 1` or `i = i_next)` | |
%i = phi i32 [1, %entry], [%i_next, %next] | |
; if (1 <= 100) continue; else break; | |
%cond_for = icmp ule i32 %i, 100 | |
br i1 %cond_for, label %for_body, label %end | |
for_body: | |
; if (i % 15 == 0) | |
%rem15 = urem i32 %i, 15 | |
%cond_fizzbuzz = icmp eq i32 %rem15, 0 | |
br i1 %cond_fizzbuzz, label %print_fizzbuzz, label %check_buzz | |
check_buzz: | |
; else if (i % 5 == 0) | |
%rem5 = urem i32 %i, 5 | |
%cond_buzz = icmp eq i32 %rem5, 0 | |
br i1 %cond_buzz, label %print_buzz, label %check_fizz | |
check_fizz: | |
; else if (i % 3 == 0) | |
%rem3 = urem i32 %i, 3 | |
%cond_fizz = icmp eq i32 %rem3, 0 | |
br i1 %cond_fizz, label %print_fizz, label %print_number | |
print_fizzbuzz: | |
call i32 @puts(i8* %ptr_fizzbuzz) | |
br label %next | |
print_buzz: | |
call i32 @puts(i8* %ptr_buzz) | |
br label %next | |
print_fizz: | |
call i32 @puts(i8* %ptr_fizz) | |
br label %next | |
print_number: | |
call void @print_unsigned_integer(i32 %i) | |
call i32 @putchar(i32 13) ; '\r' | |
call i32 @putchar(i32 10) ; '\n' | |
br label %next | |
next: | |
%i_next = add i32 %i, 1 ; `i_next = i + 1` | |
br label %for_cond | |
end: | |
ret i32 0 | |
} | |
; 符号なし整数を標準出力に出力 | |
define void @print_unsigned_integer(i32 %n) { | |
entry: | |
%buff = alloca [10 x i32] ; 文字列バッファ | |
br label %stringify | |
; 1の位から順番にASCIIコード化し、buff配列に格納 | |
stringify: | |
%n1 = phi i32 [%n, %entry], [%n2, %stringify] | |
%i = phi i32 [0, %entry], [%i1, %stringify] | |
%rem = urem i32 %n1, 10 | |
%c1 = add i32 %rem, 48 | |
%ptr1 = getelementptr [10 x i32], [10 x i32]* %buff, i32 0, i32 %i | |
store i32 %c1, i32* %ptr1 | |
%n2 = udiv i32 %n1, 10 | |
%i1 = add i32 %i, 1 | |
%cond_stringfy = icmp eq i32 %n2, 0 | |
br i1 %cond_stringfy, label %print, label %stringify | |
; buff配列を逆順に出力 | |
print: | |
%j = phi i32 [%i1, %stringify], [%j1, %print] | |
%j1 = sub i32 %j, 1 | |
%ptr2 = getelementptr [10 x i32], [10 x i32]* %buff, i32 0, i32 %j1 | |
%c2 = load i32, i32* %ptr2 | |
call i32 @putchar(i32 %c2) | |
%cond_print = icmp eq i32 %j1, 0 | |
br i1 %cond_print, label %end, label %print | |
end: | |
ret void | |
} |
This file contains 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
define i32 @main() { | |
entry: | |
br label %for_cond | |
for_cond: | |
%i = phi i32 [0, %entry], [%i_next, %for_body] ; `for (int i = 0` or `i = i_next)` | |
%cond = icmp ult i32 %i, 10 ; `i < 10` | |
br i1 %cond, label %for_body, label %end | |
for_body: | |
%i_next = add i32 %i, 1 ; `i_next = i + 1` | |
br label %for_cond | |
end: | |
ret i32 %i | |
} |
This file contains 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 i32 @putchar(i32) | |
; 符号なし整数を標準出力に出力 | |
define void @print_unsigned_integer(i32 %c) { | |
%tmp = alloca i32 | |
%buff = alloca [10 x i32] ; 文字列バッファ | |
%index = alloca i32 ; buffのインデックス | |
store i32 %c, i32* %tmp | |
store i32 0, i32* %index | |
br label %stringify | |
; 1の位から順番にASCIIコード化し、buff配列に格納 | |
stringify: | |
%1 = load i32, i32* %tmp | |
%2 = urem i32 %1, 10 | |
%3 = add i32 %2, 48 | |
%4 = load i32, i32* %index | |
%5 = getelementptr [10 x i32], [10 x i32]* %buff, i32 0, i32 %4 | |
store i32 %3, i32* %5 | |
%6 = udiv i32 %1, 10 | |
store i32 %6, i32* %tmp | |
%7 = add i32 %4, 1 | |
store i32 %7, i32* %index | |
%8 = icmp eq i32 %6, 0 | |
br i1 %8, label %print, label %stringify | |
; buff配列を逆順に出力 | |
print: | |
%9 = load i32, i32* %index | |
%10 = sub i32 %9, 1 | |
%11 = getelementptr [10 x i32], [10 x i32]* %buff, i32 0, i32 %10 | |
%12 = load i32, i32* %11 | |
call i32 @putchar(i32 %12) | |
store i32 %10, i32* %index | |
%14 = icmp eq i32 %10, 0 | |
br i1 %14, label %end, label %print | |
end: | |
ret void | |
} |
This file contains 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 i32 @putchar(i32) | |
define i32 @main() { | |
call i32 @putchar(i32 48) | |
call i32 @putchar(i32 49) | |
call i32 @putchar(i32 50) | |
call i32 @putchar(i32 51) | |
call i32 @putchar(i32 52) | |
call i32 @putchar(i32 53) | |
call i32 @putchar(i32 54) | |
call i32 @putchar(i32 55) | |
call i32 @putchar(i32 56) | |
call i32 @putchar(i32 57) | |
ret i32 0 | |
} |
This file contains 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
define i32 @main() { | |
ret i32 111 | |
} |
This file contains 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
define i32 @main() { | |
%1 = alloca i32 | |
store i32 111, i32* %1 | |
%2 = load i32, i32* %1 | |
ret i32 %2 | |
} |
This file contains 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 i32 @bar() | |
define i32 @main() { | |
%x = call i32 @bar(); | |
ret i32 %x | |
} |
This file contains 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
define i32 @main(i32, i8**) { | |
add i32 %0, 10 | |
ret i32 %3 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment