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 @@...@@ -1,4 +1,5 @@
1const std = @import("std");1const std = @import("std");
2const assert = std.debug.assert;
2const Allocator = std.mem.Allocator;3const Allocator = std.mem.Allocator;
3const Compilation = @import("Compilation.zig");4const Compilation = @import("Compilation.zig");
4const llvm = @import("llvm_bindings.zig");5const llvm = @import("llvm_bindings.zig");
...@@ -433,15 +434,20 @@ pub const LLVMIRModule = struct {...@@ -433,15 +434,20 @@ pub const LLVMIRModule = struct {
433 }434 }
434435
435 fn genBreakpoint(self: *LLVMIRModule, inst: *Inst.NoOp) !?*const llvm.ValueRef {436 fn genBreakpoint(self: *LLVMIRModule, inst: *Inst.NoOp) !?*const llvm.ValueRef {
436 // TODO: Store this function somewhere such that we dont have to add it again437 const llvn_fn = self.getIntrinsic("llvm.debugtrap");
437 const fn_type = llvm.TypeRef.functionType(llvm.voidType(), null, 0, false);438 _ = self.builder.buildCall(llvn_fn, null, 0, "");
438 const func = self.llvm_module.addFunction("llvm.debugtrap", fn_type);
439
440 // TODO: add assertion: LLVMGetIntrinsicID
441 _ = self.builder.buildCall(func, null, 0, "");
442 return null;439 return null;
443 }440 }
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
445 fn resolveInst(self: *LLVMIRModule, inst: *ir.Inst) !*const llvm.ValueRef {451 fn resolveInst(self: *LLVMIRModule, inst: *ir.Inst) !*const llvm.ValueRef {
446 if (inst.castTag(.constant)) |const_inst| {452 if (inst.castTag(.constant)) |const_inst| {
447 return self.genTypedValue(inst.src, .{ .ty = inst.ty, .val = const_inst.val });453 return self.genTypedValue(inst.src, .{ .ty = inst.ty, .val = const_inst.val });
...@@ -514,7 +520,7 @@ pub const LLVMIRModule = struct {...@@ -514,7 +520,7 @@ pub const LLVMIRModule = struct {
514520
515 pub fn fail(self: *LLVMIRModule, src: usize, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } {521 pub fn fail(self: *LLVMIRModule, src: usize, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } {
516 @setCold(true);522 @setCold(true);
517 std.debug.assert(self.err_msg == null);523 assert(self.err_msg == null);
518 self.err_msg = try Compilation.ErrorMsg.create(self.gpa, src, format, args);524 self.err_msg = try Compilation.ErrorMsg.create(self.gpa, src, format, args);
519 return error.CodegenFail;525 return error.CodegenFail;
520 }526 }
src/llvm_bindings.zig+6
...@@ -63,10 +63,16 @@ pub const ModuleRef = opaque {...@@ -63,10 +63,16 @@ pub const ModuleRef = opaque {
63 pub const getNamedFunction = LLVMGetNamedFunction;63 pub const getNamedFunction = LLVMGetNamedFunction;
64 extern fn LLVMGetNamedFunction(*const ModuleRef, Name: [*:0]const u8) ?*const ValueRef;64 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
66 pub const printToString = LLVMPrintModuleToString;69 pub const printToString = LLVMPrintModuleToString;
67 extern fn LLVMPrintModuleToString(*const ModuleRef) [*:0]const u8;70 extern fn LLVMPrintModuleToString(*const ModuleRef) [*:0]const u8;
68};71};
6972
73pub const lookupIntrinsicID = LLVMLookupIntrinsicID;
74extern fn LLVMLookupIntrinsicID(Name: [*]const u8, NameLen: usize) c_uint;
75
70pub const disposeMessage = LLVMDisposeMessage;76pub const disposeMessage = LLVMDisposeMessage;
71extern fn LLVMDisposeMessage(Message: [*:0]const u8) void;77extern fn LLVMDisposeMessage(Message: [*:0]const u8) void;
7278