From d3055480ec27de9107678fc9e2ac9075633f1384 Mon Sep 17 00:00:00 2001 From: antlilja Date: Sun, 13 Aug 2023 16:11:33 +0200 Subject: [PATCH] LLVM: Emit bitcode even if libllvm is not present --- src/codegen/llvm.zig | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index e057810109..578772d951 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -1238,7 +1238,26 @@ pub const Object = struct { if (options.asm_path == null and options.bin_path == null and options.post_ir_path == null and options.post_bc_path == null) return; - if (!self.builder.useLibLlvm()) unreachable; // caught in Compilation.Config.resolve + if (options.post_bc_path) |path| { + if (!self.builder.useLibLlvm()) { + var arena_allocator = std.heap.ArenaAllocator.init(self.gpa); + defer arena_allocator.deinit(); + const arena = arena_allocator.allocator(); + + var file = try std.fs.cwd().createFileZ(path, .{}); + defer file.close(); + const bitcode = try self.builder.toBitcode(arena); + + const ptr: [*]const u8 = @ptrCast(bitcode.ptr); + try file.writeAll(ptr[0..(bitcode.len * 4)]); + return; + } + } + + if (!self.builder.useLibLlvm()) { + log.err("emitting without libllvm not implemented", .{}); + return error.FailedToEmit; + } // Unfortunately, LLVM shits the bed when we ask for both binary and assembly. // So we call the entire pipeline multiple times if this is requested.