Skip to content

Instantly share code, notes, and snippets.

@jevinskie
Created March 5, 2024 07:39
Show Gist options
  • Save jevinskie/f2f6bfbc5613d71185ca1d48887e927e to your computer and use it in GitHub Desktop.
Save jevinskie/f2f6bfbc5613d71185ca1d48887e927e to your computer and use it in GitHub Desktop.
dumb llvmlite
jevin@ptblazer [02:38:19 AM] [~]
-> 🌩 % ./llvmlite-test.py | tee foo.ll
; ModuleID = "llvmlite-test.ll"
target triple = "x86_64-pc-linux-gnu"
target datalayout = ""
@"gvar" = constant i64* inttoptr (i64 4919 to i64*)
@"printf_fmt_buf" = constant [54 x i8] [i8 104, i8 101, i8 108, i8 108, i8 111, i8 32, i8 119, i8 111, i8 114, i8 108, i8 100, i8 32, i8 105, i8 39, i8 109, i8 32, i8 103, i8 114, i8 111, i8 117, i8 99, i8 104, i8 121, i8 32, i8 103, i8 118, i8 97, i8 114, i8 58, i8 32, i8 37, i8 112, i8 32, i8 109, i8 97, i8 116, i8 101, i8 114, i8 105, i8 97, i8 108, i8 105, i8 122, i8 101, i8 100, i8 32, i8 52, i8 50, i8 58, i8 32, i8 37, i8 100, i8 10, i8 0]
declare i64 @"printf"(i8* %".1", ...)
define i32 @"main"()
{
entry:
%".2" = getelementptr inbounds [54 x i8], [54 x i8]* @"printf_fmt_buf", i64 0, i64 0
%".3" = load i64*, i64** @"gvar"
%".4" = call i64 (i8*, ...) @"printf"(i8* %".2", i64* %".3", i32 42)
ret i32 0
}
jevin@ptblazer [02:38:33 AM] [~]
-> 🌩 % clang-14 -o foo foo.ll
jevin@ptblazer [02:38:39 AM] [~]
-> 🌩 % ./foo
hello world i'm grouchy gvar: 0x1337 materialized 42: 42
#!/usr/bin/env python3
import platform
from llvmlite import ir
m = ir.Module(name="llvmlite-test.ll")
m.triple = {
("x86_64", "Linux", "glibc"): "x86_64-pc-linux-gnu",
("x86_64", "Darwin", ""): "x86_64-apple-macosx",
("arm64", "Darwin", ""): "arm64-apple-macosx",
}[(platform.machine(), platform.system(), platform.libc_ver()[0])]
bld = ir.IRBuilder()
i8 = ir.IntType(8)
i32 = ir.IntType(32)
i64 = ir.IntType(64)
i8p = i8.as_pointer()
i32p = i32.as_pointer()
i64p = i64.as_pointer()
iptr = i64p if platform.architecture()[0] == '64bit' else i32p
gvar = ir.GlobalVariable(m, i64p, "gvar")
gvar.global_constant = True
gvar.initializer = ir.Constant(i64p, i64(0x1337).inttoptr(i64p))
printf_fmt_buf = b"hello world i'm grouchy gvar: %p materialized 42: %d\n\0"
printf_fmt_buf_ty = ir.ArrayType(i8, len(printf_fmt_buf))
printf_fmt_buf_gv = ir.GlobalVariable(m, printf_fmt_buf_ty, name="printf_fmt_buf")
printf_fmt_buf_gv.global_constant = True
# HAHAHA you thought you could use this? YOU FOOL THAT WOULD MAKE SENSE!
# printf_fmt_buf_gv.initializer = ir.Constant(printf_fmt_buf_ty, printf_fmt_buf)
printf_fmt_buf_gv.initializer = ir.Constant(printf_fmt_buf_ty, [b for b in printf_fmt_buf])
printf_t = ir.FunctionType(i64, [i8p])
printf_t.args[0].name = "fmt"
printf_t.var_arg = True
printf = ir.Function(m, printf_t, "printf")
main_t = ir.FunctionType(i32, [])
main = ir.Function(m, main_t, "main")
bb = main.append_basic_block("entry")
bld.position_at_end(bb)
bld.call(printf, [bld.gep(printf_fmt_buf_gv, [i64(0), i64(0)], True), bld.load(gvar), i32(42)])
bld.ret(i32(0))
print(str(m))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment