Skip to content

Instantly share code, notes, and snippets.

@beholdnec
Created September 15, 2018 02:30
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 beholdnec/de82c6a2cd076a81f15257725d64aa22 to your computer and use it in GitHub Desktop.
Save beholdnec/de82c6a2cd076a81f15257725d64aa22 to your computer and use it in GitHub Desktop.
Simple NES example code compiled from C to LLVM IR (1)
; ModuleID = 'example1.c'
source_filename = "example1.c"
target datalayout = "e-p:16:8-n8"
target triple = "m6502"
@.str = private unnamed_addr constant [14 x i8] c"HELLO, WORLD!\00", align 1
@.str.1 = private unnamed_addr constant [27 x i8] c"THIS CODE PRINTS SOME TEXT\00", align 1
@.str.2 = private unnamed_addr constant [28 x i8] c"USING ASCII-ENCODED CHARSET\00", align 1
@.str.3 = private unnamed_addr constant [28 x i8] c"(WITH CAPITAL LETTERS ONLY)\00", align 1
@.str.4 = private unnamed_addr constant [28 x i8] c"TO USE CHR MORE EFFICIENTLY\00", align 1
@.str.5 = private unnamed_addr constant [29 x i8] c"YOU'D NEED A CUSTOM ENCODING\00", align 1
@.str.6 = private unnamed_addr constant [23 x i8] c"AND A CONVERSION TABLE\00", align 1
@.str.7 = private unnamed_addr constant [22 x i8] c"CURRENT VIDEO MODE IS\00", align 1
@.str.8 = private unnamed_addr constant [5 x i8] c"NTSC\00", align 1
@.str.9 = private unnamed_addr constant [4 x i8] c"PAL\00", align 1
; Function Attrs: noinline nounwind
define void @put_str(i16 %adr, i8* %str) #0 {
entry:
%adr.addr = alloca i16, align 1
%str.addr = alloca i8*, align 1
store i16 %adr, i16* %adr.addr, align 1
store i8* %str, i8** %str.addr, align 1
%0 = load i16, i16* %adr.addr, align 1
call void @vram_adr(i16 %0)
br label %while.body
while.body: ; preds = %entry, %if.end
%1 = load i8*, i8** %str.addr, align 1
%2 = load i8, i8* %1, align 1
%tobool = icmp ne i8 %2, 0
br i1 %tobool, label %if.end, label %if.then
if.then: ; preds = %while.body
br label %while.end
if.end: ; preds = %while.body
%3 = load i8*, i8** %str.addr, align 1
%incdec.ptr = getelementptr inbounds i8, i8* %3, i32 1
store i8* %incdec.ptr, i8** %str.addr, align 1
%4 = load i8, i8* %3, align 1
%conv = sext i8 %4 to i16
%sub = sub nsw i16 %conv, 32
%conv1 = trunc i16 %sub to i8
call void @vram_put(i8 zeroext %conv1)
br label %while.body
while.end: ; preds = %if.then
ret void
}
declare void @vram_adr(i16) #1
declare void @vram_put(i8 zeroext) #1
; Function Attrs: noinline nounwind
define void @main() #0 {
entry:
call void @pal_col(i8 zeroext 1, i8 zeroext 48)
call void @put_str(i16 8258, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i32 0, i32 0))
call void @put_str(i16 8386, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i32 0, i32 0))
call void @put_str(i16 8418, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i32 0, i32 0))
call void @put_str(i16 8450, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i32 0, i32 0))
call void @put_str(i16 8514, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i32 0, i32 0))
call void @put_str(i16 8546, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i32 0, i32 0))
call void @put_str(i16 8578, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i32 0, i32 0))
call void @put_str(i16 8706, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.7, i32 0, i32 0))
%call = call zeroext i8 @ppu_system()
%tobool = icmp ne i8 %call, 0
br i1 %tobool, label %if.then, label %if.else
if.then: ; preds = %entry
call void @put_str(i16 8728, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.8, i32 0, i32 0))
br label %if.end
if.else: ; preds = %entry
call void @put_str(i16 8728, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.9, i32 0, i32 0))
br label %if.end
if.end: ; preds = %if.else, %if.then
call void @ppu_on_all()
br label %while.body
while.body: ; preds = %if.end, %while.body
br label %while.body
return: ; No predecessors!
ret void
}
declare void @pal_col(i8 zeroext, i8 zeroext) #1
declare zeroext i8 @ppu_system() #1
declare void @ppu_on_all() #1
attributes #0 = { noinline nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
!llvm.ident = !{!0}
!0 = !{!"clang version 4.0.1 (https://github.com/beholdnec/clang-m6502.git a034dc6c29ac72e0e2d7ea23cbd49843fd3527b3) (https://github.com/beholdnec/llvm-m6502.git 5b1dc053fe1e55906e85135b4351eee1dae19190)"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment