From 1f945a8877adca0e279f3c62a09adf7366867c89 Mon Sep 17 00:00:00 2001 From: David Gonzalez Martin Date: Sat, 11 May 2024 00:19:36 -0600 Subject: [PATCH] bitcode try --- bootstrap/compiler.zig | 347 ++++++++++++++++++++++++++++++++++++++++- bootstrap/library.zig | 6 +- build.zig | 16 +- 3 files changed, 357 insertions(+), 12 deletions(-) diff --git a/bootstrap/compiler.zig b/bootstrap/compiler.zig index f8cc28a..080db13 100644 --- a/bootstrap/compiler.zig +++ b/bootstrap/compiler.zig @@ -1046,6 +1046,330 @@ fn resolve_value(thread: *Thread, file_index: u32, value: *Value) void { // } } +const Bitcode = struct { +}; + +fn write_bitcode() void { + var buffer = PinnedArray(u8){}; + // Magic + buffer.append_slice(&.{0x42, 0x43, 0xc0, 0xde}); + buffer.append_slice(&.{0x35, 0x14, 0x00, 0x00}); + buffer.append_slice(sample_bitcode[buffer.length..]); + const context = LLVM.Context.create(); + const module = context.parse_bitcode(buffer.slice()) orelse exit(1); + _ = module; // autofix + // + exit(0); +} + +const sample_bitcode = [_]u8{ + 0x42, 0x43, 0xc0, 0xde, + 0x35, 0x14, 0x00, 0x00, + 0x05, 0x00, 0x00, 0x00, + 0x62, 0x0c, 0x30, 0x24, + 0x4a, 0x59, 0xbe, 0x66, + 0xbd, 0xfb, 0xb4, 0xaf, + 0x0b, 0x51, 0x80, 0x4c, + 0x01, 0x00, 0x00, 0x00, + 0x21, 0x0c, 0x00, 0x00, + 0x1f, 0x01, 0x00, 0x00, + 0x0b, 0x02, 0x21, 0x00, + 0x02, 0x00, 0x00, 0x00, + 0x16, 0x00, 0x00, 0x00, + 0x07, 0x81, 0x23, 0x91, + 0x41, 0xc8, 0x04, 0x49, + 0x06, 0x10, 0x32, 0x39, + 0x92, 0x01, 0x84, 0x0c, + 0x25, 0x05, 0x08, 0x19, + 0x1e, 0x04, 0x8b, 0x62, + 0x80, 0x04, 0x45, 0x02, + 0x42, 0x92, 0x0b, 0x42, + 0x24, 0x10, 0x32, 0x14, + 0x38, 0x08, 0x18, 0x4b, + 0x0a, 0x32, 0x12, 0x88, + 0x48, 0x70, 0xc4, 0x21, + 0x23, 0x44, 0x12, 0x87, + 0x8c, 0x10, 0x41, 0x92, + 0x02, 0x64, 0xc8, 0x08, + 0xb1, 0x14, 0x20, 0x43, + 0x46, 0x88, 0x20, 0xc9, + 0x01, 0x32, 0x12, 0x84, + 0x18, 0x2a, 0x28, 0x2a, + 0x90, 0x31, 0x7c, 0xb0, + 0x5c, 0x91, 0x20, 0xc1, + 0xc8, 0x00, 0x00, 0x00, + 0x89, 0x20, 0x00, 0x00, + 0x08, 0x00, 0x00, 0x00, + 0x22, 0x66, 0x04, 0x10, + 0xb2, 0x42, 0x82, 0x49, + 0x10, 0x52, 0x42, 0x82, + 0x49, 0x90, 0x71, 0xc2, + 0x50, 0x48, 0x0a, 0x09, + 0x26, 0x41, 0xc6, 0x05, + 0x42, 0x12, 0x26, 0x08, + 0x82, 0x81, 0x00, 0x00, + 0x1a, 0x21, 0x4c, 0x0e, + 0x13, 0x36, 0xdb, 0xde, + 0x6e, 0xb1, 0xd3, 0xee, + 0xb5, 0xc4, 0x06, 0x81, + 0xa2, 0x24, 0x01, 0x00, + 0x00, 0xb1, 0x18, 0x00, + 0xb9, 0x00, 0x00, 0x00, + 0x33, 0x08, 0x80, 0x1c, + 0xc4, 0xe1, 0x1c, 0x66, + 0x14, 0x01, 0x3d, 0x88, + 0x43, 0x38, 0x84, 0xc3, + 0x8c, 0x42, 0x80, 0x07, + 0x79, 0x78, 0x07, 0x73, + 0x98, 0x71, 0x0c, 0xe6, + 0x00, 0x0f, 0xed, 0x10, + 0x0e, 0xf4, 0x80, 0x0e, + 0x33, 0x0c, 0x42, 0x1e, + 0xc2, 0xc1, 0x1d, 0xce, + 0xa1, 0x1c, 0x66, 0x30, + 0x05, 0x3d, 0x88, 0x43, + 0x38, 0x84, 0x83, 0x1b, + 0xcc, 0x03, 0x3d, 0xc8, + 0x43, 0x3d, 0x8c, 0x03, + 0x3d, 0xcc, 0x78, 0x8c, + 0x74, 0x70, 0x07, 0x7b, + 0x08, 0x07, 0x79, 0x48, + 0x87, 0x70, 0x70, 0x07, + 0x7a, 0x70, 0x03, 0x76, + 0x78, 0x87, 0x70, 0x20, + 0x87, 0x19, 0xcc, 0x11, + 0x0e, 0xec, 0x90, 0x0e, + 0xe1, 0x30, 0x0f, 0x6e, + 0x30, 0x0f, 0xe3, 0xf0, + 0x0e, 0xf0, 0x50, 0x0e, + 0x33, 0x10, 0xc4, 0x1d, + 0xde, 0x21, 0x1c, 0xd8, + 0x21, 0x1d, 0xc2, 0x61, + 0x1e, 0x66, 0x30, 0x89, + 0x3b, 0xbc, 0x83, 0x3b, + 0xd0, 0x43, 0x39, 0xb4, + 0x03, 0x3c, 0xbc, 0x83, + 0x3c, 0x84, 0x03, 0x3b, + 0xcc, 0xf0, 0x14, 0x76, + 0x60, 0x07, 0x7b, 0x68, + 0x07, 0x37, 0x68, 0x87, + 0x72, 0x68, 0x07, 0x37, + 0x80, 0x87, 0x70, 0x90, + 0x87, 0x70, 0x60, 0x07, + 0x76, 0x28, 0x07, 0x76, + 0xf8, 0x05, 0x76, 0x78, + 0x87, 0x77, 0x80, 0x87, + 0x5f, 0x08, 0x87, 0x71, + 0x18, 0x87, 0x72, 0x98, + 0x87, 0x79, 0x98, 0x81, + 0x2c, 0xee, 0xf0, 0x0e, + 0xee, 0xe0, 0x0e, 0xf5, + 0xc0, 0x0e, 0xec, 0x30, + 0x03, 0x62, 0xc8, 0xa1, + 0x1c, 0xe4, 0xa1, 0x1c, + 0xcc, 0xa1, 0x1c, 0xe4, + 0xa1, 0x1c, 0xdc, 0x61, + 0x1c, 0xca, 0x21, 0x1c, + 0xc4, 0x81, 0x1d, 0xca, + 0x61, 0x06, 0xd6, 0x90, + 0x43, 0x39, 0xc8, 0x43, + 0x39, 0x98, 0x43, 0x39, + 0xc8, 0x43, 0x39, 0xb8, + 0xc3, 0x38, 0x94, 0x43, + 0x38, 0x88, 0x03, 0x3b, + 0x94, 0xc3, 0x2f, 0xbc, + 0x83, 0x3c, 0xfc, 0x82, + 0x3b, 0xd4, 0x03, 0x3b, + 0xb0, 0xc3, 0x0c, 0xc7, + 0x69, 0x87, 0x70, 0x58, + 0x87, 0x72, 0x70, 0x83, + 0x74, 0x68, 0x07, 0x78, + 0x60, 0x87, 0x74, 0x18, + 0x87, 0x74, 0xa0, 0x87, + 0x19, 0xce, 0x53, 0x0f, + 0xee, 0x00, 0x0f, 0xf2, + 0x50, 0x0e, 0xe4, 0x90, + 0x0e, 0xe3, 0x40, 0x0f, + 0xe1, 0x20, 0x0e, 0xec, + 0x50, 0x0e, 0x33, 0x20, + 0x28, 0x1d, 0xdc, 0xc1, + 0x1e, 0xc2, 0x41, 0x1e, + 0xd2, 0x21, 0x1c, 0xdc, + 0x81, 0x1e, 0xdc, 0xe0, + 0x1c, 0xe4, 0xe1, 0x1d, + 0xea, 0x01, 0x1e, 0x66, + 0x18, 0x51, 0x38, 0xb0, + 0x43, 0x3a, 0x9c, 0x83, + 0x3b, 0xcc, 0x50, 0x24, + 0x76, 0x60, 0x07, 0x7b, + 0x68, 0x07, 0x37, 0x60, + 0x87, 0x77, 0x78, 0x07, + 0x78, 0x98, 0x51, 0x4c, + 0xf4, 0x90, 0x0f, 0xf0, + 0x50, 0x0e, 0x33, 0x1e, + 0x6a, 0x1e, 0xca, 0x61, + 0x1c, 0xe8, 0x21, 0x1d, + 0xde, 0xc1, 0x1d, 0x7e, + 0x01, 0x1e, 0xe4, 0xa1, + 0x1c, 0xcc, 0x21, 0x1d, + 0xf0, 0x61, 0x06, 0x54, + 0x85, 0x83, 0x38, 0xcc, + 0xc3, 0x3b, 0xb0, 0x43, + 0x3d, 0xd0, 0x43, 0x39, + 0xfc, 0xc2, 0x3c, 0xe4, + 0x43, 0x3b, 0x88, 0xc3, + 0x3b, 0xb0, 0xc3, 0x8c, + 0xc5, 0x0a, 0x87, 0x79, + 0x98, 0x87, 0x77, 0x18, + 0x87, 0x74, 0x08, 0x07, + 0x7a, 0x28, 0x07, 0x72, + 0x98, 0x81, 0x5c, 0xe3, + 0x10, 0x0e, 0xec, 0xc0, + 0x0e, 0xe5, 0x50, 0x0e, + 0xf3, 0x30, 0x23, 0xc1, + 0xd2, 0x41, 0x1e, 0xe4, + 0xe1, 0x17, 0xd8, 0xe1, + 0x1d, 0xde, 0x01, 0x1e, + 0x66, 0x48, 0x19, 0x3b, + 0xb0, 0x83, 0x3d, 0xb4, + 0x83, 0x1b, 0x84, 0xc3, + 0x38, 0x8c, 0x43, 0x39, + 0xcc, 0xc3, 0x3c, 0xb8, + 0xc1, 0x39, 0xc8, 0xc3, + 0x3b, 0xd4, 0x03, 0x3c, + 0xcc, 0x48, 0xb4, 0x71, + 0x08, 0x07, 0x76, 0x60, + 0x07, 0x71, 0x08, 0x87, + 0x71, 0x58, 0x87, 0x19, + 0xdb, 0xc6, 0x0e, 0xec, + 0x60, 0x0f, 0xed, 0xe0, + 0x06, 0xf0, 0x20, 0x0f, + 0xe5, 0x30, 0x0f, 0xe5, + 0x20, 0x0f, 0xf6, 0x50, + 0x0e, 0x6e, 0x10, 0x0e, + 0xe3, 0x30, 0x0e, 0xe5, + 0x30, 0x0f, 0xf3, 0xe0, + 0x06, 0xe9, 0xe0, 0x0e, + 0xe4, 0x50, 0x0e, 0xf8, + 0x30, 0x23, 0xe2, 0xec, + 0x61, 0x1c, 0xc2, 0x81, + 0x1d, 0xd8, 0xe1, 0x17, + 0xec, 0x21, 0x1d, 0xe6, + 0x21, 0x1d, 0xc4, 0x21, + 0x1d, 0xd8, 0x21, 0x1d, + 0xe8, 0x21, 0x1f, 0x66, + 0x20, 0x9d, 0x3b, 0xbc, + 0x43, 0x3d, 0xb8, 0x03, + 0x39, 0x94, 0x83, 0x39, + 0xcc, 0x58, 0xbc, 0x70, + 0x70, 0x07, 0x77, 0x78, + 0x07, 0x7a, 0x08, 0x07, + 0x7a, 0x48, 0x87, 0x77, + 0x70, 0x87, 0x19, 0xcb, + 0xe7, 0x0e, 0xef, 0x30, + 0x0f, 0xe1, 0xe0, 0x0e, + 0xe9, 0x40, 0x0f, 0xe9, + 0xa0, 0x0f, 0xe5, 0x30, + 0xc3, 0x01, 0x03, 0x73, + 0xa8, 0x07, 0x77, 0x18, + 0x87, 0x5f, 0x98, 0x87, + 0x70, 0x70, 0x87, 0x74, + 0xa0, 0x87, 0x74, 0xd0, + 0x87, 0x72, 0x98, 0x81, + 0x84, 0x41, 0x39, 0xe0, + 0xc3, 0x38, 0xb0, 0x43, + 0x3d, 0x90, 0x43, 0x39, + 0xcc, 0x40, 0xc4, 0xa0, + 0x1d, 0xca, 0xa1, 0x1d, + 0xe0, 0x41, 0x1e, 0xde, + 0xc1, 0x1c, 0x66, 0x24, + 0x63, 0x30, 0x0e, 0xe1, + 0xc0, 0x0e, 0xec, 0x30, + 0x0f, 0xe9, 0x40, 0x0f, + 0xe5, 0x30, 0x43, 0x21, + 0x83, 0x75, 0x18, 0x07, + 0x73, 0x48, 0x87, 0x5f, + 0xa0, 0x87, 0x7c, 0x80, + 0x87, 0x72, 0x98, 0xb1, + 0x94, 0x01, 0x3c, 0x8c, + 0xc3, 0x3c, 0x94, 0xc3, + 0x38, 0xd0, 0x43, 0x3a, + 0xbc, 0x83, 0x3b, 0xcc, + 0xc3, 0x8c, 0xc5, 0x0c, + 0x48, 0x21, 0x15, 0x42, + 0x61, 0x1e, 0xe6, 0x21, + 0x1d, 0xce, 0xc1, 0x1d, + 0x52, 0x81, 0x14, 0x00, + 0xa9, 0x18, 0x00, 0x00, + 0x2d, 0x00, 0x00, 0x00, + 0x0b, 0x0a, 0x72, 0x28, + 0x87, 0x77, 0x80, 0x07, + 0x7a, 0x58, 0x70, 0x98, + 0x43, 0x3d, 0xb8, 0xc3, + 0x38, 0xb0, 0x43, 0x39, + 0xd0, 0xc3, 0x82, 0xe6, + 0x1c, 0xc6, 0xa1, 0x0d, + 0xe8, 0x41, 0x1e, 0xc2, + 0xc1, 0x1d, 0xe6, 0x21, + 0x1d, 0xe8, 0x21, 0x1d, + 0xde, 0xc1, 0x1d, 0x16, + 0x34, 0xe3, 0x60, 0x0e, + 0xe7, 0x50, 0x0f, 0xe1, + 0x20, 0x0f, 0xe4, 0x40, + 0x0f, 0xe1, 0x20, 0x0f, + 0xe7, 0x50, 0x0e, 0xf4, + 0xb0, 0x80, 0x81, 0x07, + 0x79, 0x28, 0x87, 0x70, + 0x60, 0x07, 0x76, 0x78, + 0x87, 0x71, 0x08, 0x07, + 0x7a, 0x28, 0x07, 0x72, + 0x58, 0x70, 0x9c, 0xc3, + 0x38, 0xb4, 0x01, 0x3b, + 0xa4, 0x83, 0x3d, 0x94, + 0xc3, 0x02, 0x6b, 0x1c, + 0xd8, 0x21, 0x1c, 0xdc, + 0xe1, 0x1c, 0xdc, 0x20, + 0x1c, 0xe4, 0x61, 0x1c, + 0xdc, 0x20, 0x1c, 0xe8, + 0x81, 0x1e, 0xc2, 0x61, + 0x1c, 0xd0, 0xa1, 0x1c, + 0xc8, 0x61, 0x1c, 0xc2, + 0x81, 0x1d, 0xd8, 0x61, + 0xc1, 0x01, 0x0f, 0xf4, + 0x20, 0x0f, 0xe1, 0x50, + 0x0f, 0xf4, 0x80, 0x0e, + 0x0b, 0x88, 0x75, 0x18, + 0x07, 0x73, 0x48, 0x87, + 0x05, 0xcf, 0x38, 0xbc, + 0x83, 0x3b, 0xd8, 0x43, + 0x39, 0xc8, 0xc3, 0x39, + 0x94, 0x83, 0x3b, 0x8c, + 0x43, 0x39, 0x8c, 0x03, + 0x3d, 0xc8, 0x03, 0x3b, + 0x00, 0x00, 0x00, 0x00, + 0xd1, 0x10, 0x00, 0x00, + 0x06, 0x00, 0x00, 0x00, + 0x07, 0xcc, 0x3c, 0xa4, + 0x83, 0x3b, 0x9c, 0x03, + 0x3b, 0x94, 0x03, 0x3d, + 0xa0, 0x83, 0x3c, 0x94, + 0x43, 0x38, 0x90, 0xc3, + 0x01, 0x00, 0x00, 0x00, + 0x71, 0x20, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, + 0x32, 0x0e, 0x10, 0x22, + 0x04, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, + 0x5d, 0x0c, 0x00, 0x00, + 0x06, 0x00, 0x00, 0x00, + 0x12, 0x03, 0x94, 0x0f, + 0x31, 0x37, 0x2e, 0x30, + 0x2e, 0x36, 0x6c, 0x6c, + 0x76, 0x6d, 0x2d, 0x6c, + 0x69, 0x6e, 0x6b, 0x00, + 0x00, 0x00, 0x00, 0x00, +}; + fn get_integer_type(thread: *Thread) void { _ = thread; // autofix } @@ -1252,10 +1576,24 @@ fn thread_callback(thread_index: u32) void { }, .llvm_codegen_thread_module => { if (thread.functions.length > 0) { - const context = LLVM.Context.create(); - const result = context.parse_bitcode("foo"); - _ = result; // autofix + // const bytes = library.read_file(instance.arena, std.fs.cwd(), "/home/david/empty.ll"); + // for (bytes, 0..) |b, i| { + // if (i % 4 == 0) { + // std.debug.print("\n ", .{}); + // } + // std.debug.print("0x{x:0>2}, ", .{b}); + // } + write_bitcode(); + // _ = result; // autofix exit(0); + // const bytes = library.read_file(instance.arena, std.fs.cwd(), "/home/david/mybitcode.ll"); + // for (bytes, 0..) |*b, i| { + // if (i % 4 == 0) { + // std.debug.print("\n[{}] 0x{x:<8}: ", .{i, @as(*const u32, @alignCast(@ptrCast(b))).*}); + // } + // std.debug.print("[{}] 0x{x:<2} ", .{i, b.*}); + // } + // write("\n"); // const debug_info = true; // // const ExternalRef = struct{ @@ -1948,7 +2286,8 @@ pub const LLVM = struct { pub fn parse_bitcode(context: *LLVM.Context, bytes: []const u8) ?*LLVM.Module { const memory_buffer = bindings.LLVMCreateMemoryBufferWithMemoryRange(bytes.ptr, bytes.len, null, 0); var out_module: *LLVM.Module = undefined; - if (bindings.LLVMParseBitcodeInContext2(context, memory_buffer, &out_module) != 0) { + + if (bindings.LLVMParseBitcodeInContext2(context, memory_buffer, &out_module) == 0) { return out_module; } else { return null; diff --git a/bootstrap/library.zig b/bootstrap/library.zig index eefdc51..fcd5d87 100644 --- a/bootstrap/library.zig +++ b/bootstrap/library.zig @@ -213,7 +213,7 @@ pub fn PinnedArrayAdvanced(comptime T: type, comptime MaybeIndex: ?type, comptim const length = array.length; const size = length * @sizeOf(T); - const granularity_aligned_size = align_forward(size, granularity); + const granularity_aligned_size = array.committed * granularity; const new_size = size + additional * @sizeOf(T); if (granularity_aligned_size < new_size) { @@ -483,7 +483,7 @@ pub fn PinnedHashMapAdvanced(comptime K: type, comptime V: type, comptime granul { const key_size = length * @sizeOf(K); - const key_granularity_aligned_size = align_forward(key_size, granularity); + const key_granularity_aligned_size = map.committed_key * granularity; const key_new_size = key_size + additional * @sizeOf(K); if (key_granularity_aligned_size < key_new_size) { @@ -498,7 +498,7 @@ pub fn PinnedHashMapAdvanced(comptime K: type, comptime V: type, comptime granul { const value_size = length * @sizeOf(V); - const value_granularity_aligned_size = align_forward(value_size, granularity); + const value_granularity_aligned_size = map.committed_value * granularity; const value_new_size = value_size + additional * @sizeOf(K); if (value_granularity_aligned_size < value_new_size) { diff --git a/build.zig b/build.zig index c6c5500..242df88 100644 --- a/build.zig +++ b/build.zig @@ -30,7 +30,7 @@ pub fn build(b: *std.Build) !void { const use_editor = b.option(bool, "editor", "Use the GUI editor to play around the programming language") orelse (!is_ci and enable_editor); const use_debug = b.option(bool, "use_debug", "This option enables the LLVM debug build in the development PC") orelse false; const static = b.option(bool, "static", "This option enables the compiler to be built statically") orelse switch (@import("builtin").os.tag) { - else => use_debug, + else => false, .windows => true, // .macos => true, }; @@ -476,11 +476,19 @@ pub fn build(b: *std.Build) !void { const cxx_include_base = try std.mem.concat(b.allocator, u8, &.{ "/usr/include/c++/", cxx_version }); const cxx_include_arch = try std.mem.concat(b.allocator, u8, &.{ cxx_include_base, "/" ++ @tagName(@import("builtin").cpu.arch) ++ "-pc-linux-gnu" }); compiler.addObjectFile(.{ .cwd_relative = "/usr/lib64/libstdc++.so.6" }); - compiler.addIncludePath(.{ .cwd_relative = "../../local/llvm18-debug/include" }); + if (use_debug) { + compiler.addIncludePath(.{ .cwd_relative = "../../local/llvm18-debug/include" }); + } else { + compiler.addIncludePath(.{ .cwd_relative = "../../local/llvm18-release/include" }); + } compiler.addIncludePath(.{ .cwd_relative = "/usr/include" }); compiler.addIncludePath(.{ .cwd_relative = cxx_include_base }); compiler.addIncludePath(.{ .cwd_relative = cxx_include_arch }); - compiler.addLibraryPath(.{ .cwd_relative = "../../local/llvm18-debug/lib" }); + if (use_debug) { + compiler.addLibraryPath(.{ .cwd_relative = "../../local/llvm18-debug/lib" }); + } else { + compiler.addLibraryPath(.{ .cwd_relative = "../../local/llvm18-release/lib" }); + } compiler.addLibraryPath(.{ .cwd_relative = "/usr/lib64" }); } }, @@ -611,5 +619,3 @@ pub fn build(b: *std.Build) !void { const test_all = b.step("test_all", "Test all"); test_all.dependOn(&test_command.step); } - -