authorgravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2020-12-29 22:47:52+01:00
committergravatar for timonkruiper@gmail.comTimon Kruiper <timonkruiper@gmail.com> 2021-01-03 17:23:30+01:00
loge095ebf31254bf5eeba9c07a2ad724e880626f01
tree4ddbd56db65509da69f4179f0dffba8a3bf7bba1
parentda545d6a3195c1cafca498d8a695082982f74676

stage2: make use of proper LLVM intrinsic APIs in LLVM backend


2 files changed, 19 insertions(+), 7 deletions(-)

src/llvm_backend.zig+13-7
......@@ -1,4 +1,5 @@
11const std = @import("std");
2const assert = std.debug.assert;
23const Allocator = std.mem.Allocator;
34const Compilation = @import("Compilation.zig");
45const llvm = @import("llvm_bindings.zig");
......@@ -433,15 +434,20 @@ pub const LLVMIRModule = struct {
433434 }
434435
435436 fn genBreakpoint(self: *LLVMIRModule, inst: *Inst.NoOp) !?*const llvm.ValueRef {
436 // TODO: Store this function somewhere such that we dont have to add it again
437 const fn_type = llvm.TypeRef.functionType(llvm.voidType(), null, 0, false);
438 const func = self.llvm_module.addFunction("llvm.debugtrap", fn_type);
439
440 // TODO: add assertion: LLVMGetIntrinsicID
441 _ = self.builder.buildCall(func, null, 0, "");
437 const llvn_fn = self.getIntrinsic("llvm.debugtrap");
438 _ = self.builder.buildCall(llvn_fn, null, 0, "");
442439 return null;
443440 }
444441
442 fn getIntrinsic(self: *LLVMIRModule, name: []const u8) *const llvm.ValueRef {
443 const id = llvm.lookupIntrinsicID(name.ptr, name.len);
444 assert(id != 0);
445 // TODO: add support for overload intrinsics by passing the prefix of the intrinsic
446 // to `lookupIntrinsicID` and then passing the correct types to
447 // `getIntrinsicDeclaration`
448 return self.llvm_module.getIntrinsicDeclaration(id, null, 0);
449 }
450
445451 fn resolveInst(self: *LLVMIRModule, inst: *ir.Inst) !*const llvm.ValueRef {
446452 if (inst.castTag(.constant)) |const_inst| {
447453 return self.genTypedValue(inst.src, .{ .ty = inst.ty, .val = const_inst.val });
......@@ -514,7 +520,7 @@ pub const LLVMIRModule = struct {
514520
515521 pub fn fail(self: *LLVMIRModule, src: usize, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } {
516522 @setCold(true);
517 std.debug.assert(self.err_msg == null);
523 assert(self.err_msg == null);
518524 self.err_msg = try Compilation.ErrorMsg.create(self.gpa, src, format, args);
519525 return error.CodegenFail;
520526 }
src/llvm_bindings.zig+6
......@@ -63,10 +63,16 @@ pub const ModuleRef = opaque {
6363 pub const getNamedFunction = LLVMGetNamedFunction;
6464 extern fn LLVMGetNamedFunction(*const ModuleRef, Name: [*:0]const u8) ?*const ValueRef;
6565
66 pub const getIntrinsicDeclaration = LLVMGetIntrinsicDeclaration;
67 extern fn LLVMGetIntrinsicDeclaration(Mod: *const ModuleRef, ID: c_uint, ParamTypes: ?[*]*const TypeRef, ParamCount: usize) *const ValueRef;
68
6669 pub const printToString = LLVMPrintModuleToString;
6770 extern fn LLVMPrintModuleToString(*const ModuleRef) [*:0]const u8;
6871};
6972
73pub const lookupIntrinsicID = LLVMLookupIntrinsicID;
74extern fn LLVMLookupIntrinsicID(Name: [*]const u8, NameLen: usize) c_uint;
75
7076pub const disposeMessage = LLVMDisposeMessage;
7177extern fn LLVMDisposeMessage(Message: [*:0]const u8) void;
7278