def init_module(self, module): self.module = module llvm_module = self.module.llvm_module # RPC func_type = ll.FunctionType(ll.IntType(32), [ll.IntType(32)], var_arg=1) self.rpc = ll.Function(llvm_module, func_type, "__syscall_rpc") # syscalls self.syscalls = dict() for func_name, func_type_str in _syscalls.items(): func_type = _str_to_functype(func_type_str) self.syscalls[func_name] = ll.Function(llvm_module, func_type, "__syscall_" + func_name) # exception handling func_type = ll.FunctionType(ll.IntType(32), [ll.PointerType(ll.IntType(8))]) self.eh_setjmp = ll.Function(llvm_module, func_type, "__eh_setjmp") self.eh_setjmp.attributes.add("nounwind") self.eh_setjmp.attributes.add("returns_twice") func_type = ll.FunctionType(ll.PointerType(ll.IntType(8)), []) self.eh_push = ll.Function(llvm_module, func_type, "__eh_push") func_type = ll.FunctionType(ll.VoidType(), [ll.IntType(32)]) self.eh_pop = ll.Function(llvm_module, func_type, "__eh_pop") func_type = ll.FunctionType(ll.IntType(32), []) self.eh_getid = ll.Function(llvm_module, func_type, "__eh_getid") func_type = ll.FunctionType(ll.VoidType(), [ll.IntType(32)]) self.eh_raise = ll.Function(llvm_module, func_type, "__eh_raise") self.eh_raise.attributes.add("noreturn")
def _chr_to_type(c): if c == "n": return ll.VoidType() if c == "b": return ll.IntType(1) if c == "i": return ll.IntType(32) if c == "I": return ll.IntType(64) raise ValueError
def main(): def process_diagnostic(diag): print("\n".join(diag.render())) if diag.level in ("fatal", "error"): exit(1) engine = diagnostic.Engine() engine.process = process_diagnostic mod = Module(Source.from_string("".join(fileinput.input()).expandtabs(), engine=engine)) target = NativeTarget() llmod = mod.build_llvm_ir(target=target) # Add main so that the result can be executed with lli llmain = ll.Function(llmod, ll.FunctionType(ll.VoidType(), []), "main") llbuilder = ll.IRBuilder(llmain.append_basic_block("entry")) llbuilder.call(llmod.get_global(llmod.name + ".__modinit__"), [ ll.Constant(ll.IntType(8).as_pointer(), None)]) llbuilder.ret_void() print(llmod)
def get_llvm_type(self): return ll.VoidType()