| ... | @@ -1,4 +1,5 @@ | ... | @@ -1,4 +1,5 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| | 2 | const assert = std.debug.assert; |
| 2 | const Allocator = std.mem.Allocator; | 3 | const Allocator = std.mem.Allocator; |
| 3 | const Compilation = @import("Compilation.zig"); | 4 | const Compilation = @import("Compilation.zig"); |
| 4 | const llvm = @import("llvm_bindings.zig"); | 5 | const llvm = @import("llvm_bindings.zig"); |
| ... | @@ -433,15 +434,20 @@ pub const LLVMIRModule = struct { | ... | @@ -433,15 +434,20 @@ pub const LLVMIRModule = struct { |
| 433 | } | 434 | } |
| 434 | | 435 | |
| 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 again | 437 | 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 | } |
| 444 | | 441 | |
| | 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 { |
| 514 | | 520 | |
| 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 | } |