Skip to content

Instantly share code, notes, and snippets.

@redraiment
Last active August 18, 2022 17:05
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 redraiment/376a2335760003aecb8665f4b6db0a13 to your computer and use it in GitHub Desktop.
Save redraiment/376a2335760003aecb8665f4b6db0a13 to your computer and use it in GitHub Desktop.
Hello world of Brainfuck in LLVM IR
; Translate "Hello world of brainfuck" to LLVM IR manually.
; ++++++++++
; [>+++++++>++++++++++>+++>+<<<<-]
; >++.>+.+++++++..+++.>++.<<+++++++++++++++.
; >.+++.------.--------.>+.>.
; ModuleID = 'helloworld.bf'
source_filename = "helloworld.bf"
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-linux-gnu"
@ds = global [30000 x i8] zeroinitializer
declare i32 @putchar(i32)
define i32 @main() {
%1 = getelementptr inbounds [30000 x i8], [30000 x i8]* @ds, i8 0, i8 0
;; + 10
%2 = load i8, i8* %1
%3 = add i8 %2, 10
store i8 %3, i8* %1
;; [
br label %4
4:
%5 = load i8, i8* %1
%6 = icmp ne i8 %5, 0
br i1 %6, label %7, label %23
7:
;; > 1
%8 = getelementptr i8, i8* %1, i8 1
;; + 7
%9 = load i8, i8* %8
%10 = add i8 %9, 7
store i8 %10, i8* %8
;; > 1
%11 = getelementptr i8, i8* %8, i8 1
;; + 10
%12 = load i8, i8* %11
%13 = add i8 %12, 10
store i8 %13, i8* %11
;; > 1
%14 = getelementptr i8, i8* %11, i8 1
;; + 3
%15 = load i8, i8* %14
%16 = add i8 %15, 3
store i8 %16, i8* %14
;; > 1
%17 = getelementptr i8, i8* %14, i8 1
;; + 1
%18 = load i8, i8* %17
%19 = add i8 %18, 1
store i8 %19, i8* %17
;; < 4
%20 = getelementptr i8, i8* %17, i8 -4
;; - 1
%21 = load i8, i8* %20
%22 = sub i8 %21, 1
store i8 %22, i8* %20
;; ]
br label %4
23:
;; H
%24 = getelementptr i8, i8* %20, i8 1
%25 = load i8, i8* %24
%26 = add i8 %25, 2
store i8 %26, i8* %24
%27 = load i8, i8* %24
%28 = sext i8 %27 to i32
%29 = call i32 @putchar(i32 %28)
;; e
%30 = getelementptr i8, i8* %24, i8 1
%31 = load i8, i8* %30
%32 = add i8 %31, 1
store i8 %32, i8* %30
%33 = load i8, i8* %30
%34 = sext i8 %33 to i32
%35 = call i32 @putchar(i32 %34)
;; l
%36 = load i8, i8* %30
%37 = add i8 %36, 7
store i8 %37, i8* %30
%38 = load i8, i8* %30
%39 = sext i8 %38 to i32
%40 = call i32 @putchar(i32 %39)
;; l
%41 = load i8, i8* %30
%42 = sext i8 %41 to i32
%43 = call i32 @putchar(i32 %42)
;; o
%44 = load i8, i8* %30
%45 = add i8 %44, 3
store i8 %45, i8* %30
%46 = load i8, i8* %30
%47 = sext i8 %46 to i32
%48 = call i32 @putchar(i32 %47)
;; space
%49 = getelementptr i8, i8* %30, i8 1
%50 = load i8, i8* %49
%51 = add i8 %50, 2
store i8 %51, i8* %49
%52 = load i8, i8* %49
%53 = sext i8 %52 to i32
%54 = call i32 @putchar(i32 %53)
;; W
%55 = getelementptr i8, i8* %49, i8 -2
%56 = load i8, i8* %55
%57 = add i8 %56, 15
store i8 %57, i8* %55
%58 = load i8, i8* %55
%59 = sext i8 %58 to i32
%60 = call i32 @putchar(i32 %59)
;; o
%61 = getelementptr i8, i8* %55, i8 1
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = call i32 @putchar(i32 %63)
;; r
%65 = load i8, i8* %61
%66 = add i8 %65, 3
store i8 %66, i8* %61
%67 = load i8, i8* %61
%68 = sext i8 %67 to i32
%69 = call i32 @putchar(i32 %68)
;; l
%70 = load i8, i8* %61
%71 = sub i8 %70, 6
store i8 %71, i8* %61
%72 = load i8, i8* %61
%73 = sext i8 %72 to i32
%74 = call i32 @putchar(i32 %73)
;; d
%75 = load i8, i8* %61
%76 = sub i8 %75, 8
store i8 %76, i8* %61
%77 = load i8, i8* %61
%78 = sext i8 %77 to i32
%79 = call i32 @putchar(i32 %78)
;; !
%80 = getelementptr i8, i8* %61, i8 1
%81 = load i8, i8* %80
%82 = add i8 %81, 1
store i8 %82, i8* %80
%83 = load i8, i8* %80
%84 = sext i8 %83 to i32
%85 = call i32 @putchar(i32 %84)
;; newline
%86 = getelementptr i8, i8* %80, i8 1
%87 = load i8, i8* %86
%88 = sext i8 %87 to i32
%89 = call i32 @putchar(i32 %88)
ret i32 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment