bitcode split

This commit is contained in:
David Gonzalez Martin 2024-05-11 11:50:03 -06:00
parent 1f945a8877
commit aade460a50
3 changed files with 719 additions and 385 deletions

View File

@ -473,7 +473,6 @@ const Type = struct {
};
};
fn integer_bit_count(t: *Type) u16 {
const integer: Type.Integer = @bitCast(t.sema);
return integer.bit_count;
@ -484,8 +483,7 @@ fn integer_signedness(t: *Type) Type.Integer.Signedness {
return integer.signedness;
}
const IntegerType = struct{
};
const IntegerType = struct {};
const Keyword = enum {
@"for",
@ -553,7 +551,6 @@ const Function = struct{
};
};
const Instruction = struct {
index: u24,
id: Id,
@ -571,7 +568,6 @@ const ConstantInt = struct{
type: *Type,
};
const Call = struct {
value: *Value,
const Index = PinnedArray(Call).Index;
@ -610,7 +606,6 @@ const Thread = struct{
attributes: LLVM.Attributes,
} = undefined,
fn add_thread_work(thread: *Thread, job: Job) void {
thread.task_system.job.queue_job(job);
}
@ -1046,24 +1041,14 @@ fn resolve_value(thread: *Thread, file_index: u32, value: *Value) void {
// }
}
const Bitcode = struct {
};
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,
const magic = [_]u8{ 0x42, 0x43, 0xc0, 0xde };
buffer.append_slice(&magic);
const identification_block = [_]u8{
0x35, 0x14, 0x00, 0x00,
0x05, 0x00, 0x00, 0x00,
0x62, 0x0c, 0x30, 0x24,
@ -1071,9 +1056,18 @@ const sample_bitcode = [_]u8{
0xbd, 0xfb, 0xb4, 0xaf,
0x0b, 0x51, 0x80, 0x4c,
0x01, 0x00, 0x00, 0x00,
};
buffer.append_slice(&identification_block);
// ==================
// MODULE BLOCK START
// ==================
const module_block_start = [_]u8{
0x21, 0x0c, 0x00, 0x00,
0x1f, 0x01, 0x00, 0x00,
0x0b, 0x02, 0x21, 0x00,
};
buffer.append_slice(&module_block_start);
const blockinfo_block = [_]u8{
0x02, 0x00, 0x00, 0x00,
0x16, 0x00, 0x00, 0x00,
0x07, 0x81, 0x23, 0x91,
@ -1098,6 +1092,9 @@ const sample_bitcode = [_]u8{
0x90, 0x31, 0x7c, 0xb0,
0x5c, 0x91, 0x20, 0xc1,
0xc8, 0x00, 0x00, 0x00,
};
buffer.append_slice(&blockinfo_block);
const typeinfo_block = [_]u8{
0x89, 0x20, 0x00, 0x00,
0x08, 0x00, 0x00, 0x00,
0x22, 0x66, 0x04, 0x10,
@ -1113,6 +1110,10 @@ const sample_bitcode = [_]u8{
0x6e, 0xb1, 0xd3, 0xee,
0xb5, 0xc4, 0x06, 0x81,
0xa2, 0x24, 0x01, 0x00,
};
buffer.append_slice(&typeinfo_block);
const metadata_kind_block = [_]u8{
0x00, 0xb1, 0x18, 0x00,
0xb9, 0x00, 0x00, 0x00,
0x33, 0x08, 0x80, 0x1c,
@ -1300,6 +1301,10 @@ const sample_bitcode = [_]u8{
0x61, 0x1e, 0xe6, 0x21,
0x1d, 0xce, 0xc1, 0x1d,
0x52, 0x81, 0x14, 0x00,
};
buffer.append_slice(&metadata_kind_block);
const operand_bundle_tag = [_]u8{
0xa9, 0x18, 0x00, 0x00,
0x2d, 0x00, 0x00, 0x00,
0x0b, 0x0a, 0x72, 0x28,
@ -1347,6 +1352,10 @@ const sample_bitcode = [_]u8{
0x43, 0x39, 0x8c, 0x03,
0x3d, 0xc8, 0x03, 0x3b,
0x00, 0x00, 0x00, 0x00,
};
buffer.append_slice(&operand_bundle_tag);
const value26 = [_]u8{
0xd1, 0x10, 0x00, 0x00,
0x06, 0x00, 0x00, 0x00,
0x07, 0xcc, 0x3c, 0xa4,
@ -1355,11 +1364,18 @@ const sample_bitcode = [_]u8{
0xa0, 0x83, 0x3c, 0x94,
0x43, 0x38, 0x90, 0xc3,
0x01, 0x00, 0x00, 0x00,
};
buffer.append_slice(&value26);
const value_symtab = [_]u8{
0x71, 0x20, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00,
0x32, 0x0e, 0x10, 0x22,
0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
};
buffer.append_slice(&value_symtab);
const strtab = [_]u8{
0x5d, 0x0c, 0x00, 0x00,
0x06, 0x00, 0x00, 0x00,
0x12, 0x03, 0x94, 0x0f,
@ -1370,10 +1386,335 @@ const sample_bitcode = [_]u8{
0x00, 0x00, 0x00, 0x00,
};
fn get_integer_type(thread: *Thread) void {
_ = thread; // autofix
buffer.append_slice(&strtab);
assert(buffer.length == sample_bitcode.len);
const context = LLVM.Context.create();
const module = context.parse_bitcode(buffer.slice()) orelse exit(1);
_ = module; // autofix
//
exit(0);
}
const sample_bitcode = [_]u8{
// Magic
0x42, 0x43, 0xc0, 0xde,
// Start of the identification block
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,
// It looks like this is the start of the module block
0x21, 0x0c, 0x00, 0x00,
0x1f, 0x01, 0x00, 0x00,
0x0b, 0x02, 0x21, 0x00,
// start of the blockinfo block?
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,
// type block?
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,
// metadata kind block?
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, 0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
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,
// operand bundle tags block?
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,
// Unknown block 26?
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,
// Value symtab block?
0x71, 0x20, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00,
0x32, 0x0e, 0x10, 0x22,
0x04, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
// Start of the strtab block
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,
};
const CallingConvention = enum {
c,
custom,
@ -1923,7 +2264,6 @@ pub fn analyze_file(thread: *Thread, file_index: u32) void {
const identifier = parser.parse_identifier(thread, src);
_ = identifier; // autofix
exit(1);
},
'f' => {
@ -2126,7 +2466,6 @@ pub fn analyze_file(thread: *Thread, file_index: u32) void {
const return_value = parser.parse_typed_expression(&analyzer, thread, file, function.declaration.return_type);
parser.expect_character(src, ';');
const return_expression = thread.returns.append_index(.{
.value = return_value,
});
@ -2282,7 +2621,6 @@ pub const LLVM = struct {
const getAttributeFromType = bindings.NativityLLVMContextGetAttributeFromType;
const getAttributeSet = bindings.NativityLLVMContextGetAttributeSet;
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;
@ -2930,7 +3268,6 @@ pub const LLVM = struct {
intrinsic,
array,
};
};
pub const MemoryBuffer = opaque {};

View File

@ -784,7 +784,6 @@ pub fn read_file(arena: *Arena, directory: std.fs.Dir, file_relative_path: []con
return file_buffer[0..read_byte_count];
}
pub fn self_exe_path(arena: *Arena) ![]const u8 {
var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
return try arena.duplicate_bytes(try std.fs.selfExePath(&buffer));

View File

@ -76,5 +76,3 @@ pub fn main() !void {
pub const std_options = std.Options{
.enable_segfault_handler = false,
};