Skip to content

Instantly share code, notes, and snippets.

@nektro
Last active August 8, 2020 00:27
Show Gist options
  • Save nektro/a61777bbcb818d99dddf579b031e9798 to your computer and use it in GitHub Desktop.
Save nektro/a61777bbcb818d99dddf579b031e9798 to your computer and use it in GitHub Desktop.
pub const main = func() {
print("Hello, world!\n",)
}
const print = func(s: string) {
# 1 is the file descriptor for std out
write(1, s,)
}
const write = func(fd: int, s: string) {
# 1 is the ID for the write syscall
syscall3(1, fd, s.ptr, s.len,)
}
const syscall3 = func(call: int, arg1: int, arg2: int, arg3: int) {
# this function only works on x86_64
asm "syscall", "=r,{rax},{rdi},{rsi},{rdx}", call, arg1, arg2, arg3
}
@0 = global [15 x i8] c"Hello, world!\0A\00"
@1 = global { i8*, i64 } { i8* getelementptr inbounds ([15 x i8], [15 x i8]* @0, i32 0, i32 0), i64 15 }
define void @2(i64 %0, i64 %1, i64 %2, i64 %3) {
4:
%5 = call i64 asm sideeffect "syscall", "=r,{rax},{rdi},{rsi},{rdx}"(i64 %0, i64 %1, i64 %2, i64 %3)
ret void
}
define void @3(i64 %0, { i8*, i64 }* %1) {
2:
%3 = getelementptr inbounds { i8*, i64 }, { i8*, i64 }* %1, i32 0, i32 0
%4 = load i8*, i8** %3
%5 = ptrtoint i8* %4 to i64
%6 = getelementptr inbounds { i8*, i64 }, { i8*, i64 }* %1, i32 0, i32 1
%7 = load i64, i64* %6
call void @2(i64 1, i64 %0, i64 %5, i64 %7)
ret void
}
define void @4({ i8*, i64 }* %0) {
1:
call void @3(i64 1, { i8*, i64 }* %0)
ret void
}
define i32 @main() {
0:
call void @4({ i8*, i64 }* @1)
ret i32 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment