def compile_function(self, func_def, param_types): ns = infer_types.infer_function_types(self.runtime, func_def, param_types) retval = ns["return"] function_type = ll.FunctionType(retval.get_llvm_type(), [ns[arg.arg].get_llvm_type() for arg in func_def.args.args]) function = ll.Function(self.llvm_module, function_type, func_def.name) bb = function.append_basic_block("entry") builder = ll.IRBuilder() builder.position_at_end(bb) for arg_ast, arg_llvm in zip(func_def.args.args, function.args): arg_llvm.name = arg_ast.arg for k, v in ns.items(): v.alloca(builder, k) for arg_ast, arg_llvm in zip(func_def.args.args, function.args): ns[arg_ast.arg].auto_store(builder, arg_llvm) visitor = ast_body.Visitor(self.runtime, ns, builder) visitor.visit_statements(func_def.body) if not tools.is_terminated(builder.basic_block): if isinstance(retval, base_types.VNone): builder.ret_void() else: builder.ret(retval.auto_load(builder)) return function, retval
def compile_function(self, func_def, param_types): ns = infer_types.infer_function_types(self.env, func_def, param_types) retval = ns["return"] function_type = ll.FunctionType( retval.get_llvm_type(), [ns[arg.arg].get_llvm_type() for arg in func_def.args.args]) function = ll.Function(self.llvm_module, function_type, func_def.name) bb = function.append_basic_block("entry") builder = ll.IRBuilder() builder.position_at_end(bb) for arg_ast, arg_llvm in zip(func_def.args.args, function.args): arg_llvm.name = arg_ast.arg for k, v in ns.items(): v.alloca(builder, k) for arg_ast, arg_llvm in zip(func_def.args.args, function.args): ns[arg_ast.arg].auto_store(builder, arg_llvm) visitor = ast_body.Visitor(self.env, ns, builder) visitor.visit_statements(func_def.body) if not tools.is_terminated(builder.basic_block): if isinstance(retval, base_types.VNone): builder.ret_void() else: builder.ret(retval.auto_load(builder)) return function, retval
def _build_function_types(f): return infer_function_types( None, ast.parse(inspect.getsource(f)), dict())