Rewrite and thread compiler

This commit is contained in:
David Gonzalez Martin 2024-05-21 19:20:34 -06:00
parent db6624a690
commit cd2316f7ee
16 changed files with 3963 additions and 319 deletions

2
.vscode/launch.json vendored
View File

@ -12,7 +12,7 @@
"args": [
"exe",
"-main_source_file",
"test/build/c-abi/main.nat",
"retest/standalone/first/main.nat"
],
"cwd": "${workspaceFolder}",
},

View File

@ -18,6 +18,8 @@ const starts_with_slice = library.starts_with_slice;
const PinnedArray = library.PinnedArray;
const PinnedArrayAdvanced = library.PinnedArrayAdvanced;
const PinnedHashMap = library.PinnedHashMap;
const realpath = library.realpath;
const self_exe_path = library.self_exe_path;
const span = library.span;
const format_int = library.format_int;
const my_hash = library.my_hash;
@ -2750,19 +2752,6 @@ pub fn argsCopyZ(arena: *Arena, args: []const []const u8) ![:null]?[*:0]u8 {
return result[0..args.len :null];
}
extern "c" fn NativityLLVMArchiverMain(argc: c_int, argv: [*:null]?[*:0]u8) c_int;
fn arMain(arena: *Arena, arguments: []const []const u8) !u8 {
const argv = try argsCopyZ(arena, arguments);
const exit_code = NativityLLVMArchiverMain(@as(c_int, @intCast(arguments.len)), argv.ptr);
return @as(u8, @bitCast(@as(i8, @truncate(exit_code))));
}
extern "c" fn NativityClangMain(argc: c_int, argv: [*:null]?[*:0]u8) c_int;
pub fn clangMain(arena: *Arena, arguments: []const []const u8) !u8 {
const argv = try argsCopyZ(arena, arguments);
const exit_code = NativityClangMain(@as(c_int, @intCast(arguments.len)), argv.ptr);
return @as(u8, @bitCast(@as(i8, @truncate(exit_code))));
}
const ExecutableOptions = struct {
is_test: bool,
@ -2836,46 +2825,6 @@ pub fn buildExecutable(context: *const Context, arguments: []const []const u8, o
} else {
reportUnterminatedArgumentError(current_argument);
}
} else if (byte_equal(current_argument, "-log")) {
if (i + 1 != arguments.len) {
i += 1;
// var log_argument_iterator = std.mem.splitScalar(u8, span(arguments[i]), ',');
//
// while (log_argument_iterator.next()) |log_argument| {
// var log_argument_splitter = std.mem.splitScalar(u8, log_argument, '.');
// const log_scope_candidate = log_argument_splitter.next() orelse unreachable;
// var recognized_scope = false;
//
// inline for (@typeInfo(LoggerScope).Enum.fields) |logger_scope_enum_field| {
// const log_scope = @field(LoggerScope, logger_scope_enum_field.name);
//
// if (byte_equal(@tagName(log_scope), log_scope_candidate)) {
// const LogScope = getLoggerScopeType(log_scope);
//
// if (log_argument_splitter.next()) |particular_log_candidate| {
// var recognized_particular = false;
// inline for (@typeInfo(LogScope.Logger).Enum.fields) |particular_log_field| {
// const particular_log = @field(LogScope.Logger, particular_log_field.name);
//
// if (byte_equal(particular_log_candidate, @tagName(particular_log))) {
// LogScope.Logger.bitset.setPresent(particular_log, true);
// recognized_particular = true;
// }
// } else if (!recognized_particular) @panic("Unrecognized particular log"); //std.debug.panic("Unrecognized particular log \"{s}\" in scope {s}", .{ particular_log_candidate, @tagName(log_scope) });
// } else {
// // LogScope.Logger.bitset = @TypeOf(LogScope.Logger.bitset).initFull();
// }
//
// logger_bitset.setPresent(log_scope, true);
//
// recognized_scope = true;
// }
// } else if (!recognized_scope) @panic("Unrecognized particular log"); //std.debug.panic("Unrecognized log scope: {s}", .{log_scope_candidate});
// }
} else {
reportUnterminatedArgumentError(current_argument);
}
} else if (byte_equal(current_argument, "-parse")) {
if (i + 1 != arguments.len) {
i += 1;
@ -3087,19 +3036,6 @@ fn createUnit(context: *const Context, arguments: struct {
return unit;
}
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));
}
pub fn realpath(arena: *Arena, dir: std.fs.Dir, relative_path: []const u8) ![]const u8 {
var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
const stack_realpath = try dir.realpath(relative_path, &buffer);
const heap_realpath = try arena.new_array(u8, stack_realpath.len);
@memcpy(heap_realpath, stack_realpath);
return heap_realpath;
}
pub const ContainerType = enum {
@"struct",
@"enum",
@ -17680,7 +17616,9 @@ pub const Unit = struct {
return unit.importPackage(context, package);
}
const ends_with_nat = import_name.len >= 4 and @as(u32, @bitCast(import_name[import_name.len - 4 ..][0..4].*)) == @as(u32, @bitCast(@as([*]const u8, ".nat")[0..4].*));
const nat_ending = ".nat";
const nat_ending_len = nat_ending.len;
const ends_with_nat = import_name.len >= 4 and @as(u32, @bitCast(import_name[import_name.len - nat_ending_len ..][0..nat_ending_len].*)) == @as(u32, @bitCast(@as([*]const u8, ".nat")[0..nat_ending_len].*));
if (!ends_with_nat) {
unreachable;
}

View File

@ -3424,17 +3424,6 @@ pub fn codegen(unit: *Compilation.Unit, context: *const Compilation.Context) !vo
const target_machine = target.createTargetMachine(target_triple.ptr, target_triple.len, cpu, cpu.len, features.pointer, features.length, LLVM.RelocationModel.static, code_model, is_code_model_present, codegen_optimization_level, jit) orelse unreachable;
llvm.module.setTargetMachineDataLayout(target_machine);
llvm.module.setTargetTriple(target_triple.ptr, target_triple.len);
const file_path = unit.descriptor.executable_path;
const object_file_path = blk: {
const slice = try context.arena.new_array(u8, file_path.len + 3);
@memcpy(slice[0..file_path.len], file_path);
slice[file_path.len] = '.';
slice[file_path.len + 1] = 'o';
slice[file_path.len + 2] = 0;
const object_file_path = slice[0 .. slice.len - 1 :0];
break :blk object_file_path;
};
if (unit.descriptor.optimization != .none) {
const optimization_level: LLVM.OptimizationLevel = switch (unit.descriptor.optimization) {
.none => unreachable,
@ -3448,6 +3437,17 @@ pub fn codegen(unit: *Compilation.Unit, context: *const Compilation.Context) !vo
llvm.module.runOptimizationPipeline(target_machine, optimization_level);
}
const file_path = unit.descriptor.executable_path;
const object_file_path = blk: {
const slice = try context.arena.new_array(u8, file_path.len + 3);
@memcpy(slice[0..file_path.len], file_path);
slice[file_path.len] = '.';
slice[file_path.len + 1] = 'o';
slice[file_path.len + 2] = 0;
const object_file_path = slice[0 .. slice.len - 1 :0];
break :blk object_file_path;
};
const disable_verify = false;
const result = llvm.module.addPassesToEmitFile(target_machine, object_file_path.ptr, object_file_path.len, LLVM.CodeGenFileType.object, disable_verify);

View File

@ -1,71 +1,73 @@
const llvm = @import("llvm.zig");
const LLVM = llvm.LLVM;
pub extern fn NativityLLVMCreateContext() ?*LLVM.Context;
pub extern fn NativityLLVMCreateModule(module_name_ptr: [*:0]const u8, module_name_len: usize, context: *LLVM.Context) ?*LLVM.Module;
pub extern fn NativityLLVMCreateBuilder(context: *LLVM.Context) ?*LLVM.Builder;
pub extern fn NativityLLVMGetFunctionType(return_type: *LLVM.Type, argument_type_ptr: [*]const *LLVM.Type, argument_type_len: usize, is_var_args: bool) ?*LLVM.Type.Function;
const compiler = @import("../compiler.zig");
const LLVM = compiler.LLVM;
pub extern fn NativityLLVMCreateContext() *LLVM.Context;
pub extern fn NativityLLVMCreateModule(module_name_ptr: [*]const u8, module_name_len: usize, context: *LLVM.Context) *LLVM.Module;
pub extern fn NativityLLVMCreateBuilder(context: *LLVM.Context) *LLVM.Builder;
pub extern fn NativityLLVMGetFunctionType(return_type: *LLVM.Type, argument_type_ptr: [*]const *LLVM.Type, argument_type_len: usize, is_var_args: bool) *LLVM.Type.Function;
pub extern fn NativityLLVMFunctionTypeGetArgumentType(function_type: *LLVM.Type.Function, argument_index: c_uint) *LLVM.Type;
pub extern fn NativityLLVMGetIntegerType(context: *LLVM.Context, bit_count: u32) ?*LLVM.Type.Integer;
pub extern fn NativityLLVMGetPointerType(context: *LLVM.Context, address_space: u32) ?*LLVM.Type.Pointer;
pub extern fn NativityLLVMGetIntegerType(context: *LLVM.Context, bit_count: u32) *LLVM.Type.Integer;
pub extern fn NativityLLVMGetPointerType(context: *LLVM.Context, address_space: u32) *LLVM.Type.Pointer;
pub extern fn NativityLLVMPointerTypeGetNull(pointer_type: *LLVM.Type.Pointer) *LLVM.Value.Constant.PointerNull;
pub extern fn NativityLLVMGetArrayType(element_type: *LLVM.Type, element_count: u64) ?*LLVM.Type.Array;
pub extern fn NativityLLVMGetStructType(context: *LLVM.Context, type_ptr: [*]const *LLVM.Type, type_count: usize, is_packed: bool) ?*LLVM.Type.Struct;
pub extern fn NativityLLVMConstantStruct(struct_type: *LLVM.Type.Struct, constant_ptr: [*]const *LLVM.Value.Constant, constant_count: usize) ?*LLVM.Value.Constant;
pub extern fn NativityLLVMGetArrayType(element_type: *LLVM.Type, element_count: u64) *LLVM.Type.Array;
pub extern fn NativityLLVMGetStructType(context: *LLVM.Context, type_ptr: [*]const *LLVM.Type, type_count: usize, is_packed: bool) *LLVM.Type.Struct;
pub extern fn NativityLLVMConstantStruct(struct_type: *LLVM.Type.Struct, constant_ptr: [*]const *LLVM.Value.Constant, constant_count: usize) *LLVM.Value.Constant;
pub extern fn NativityLLVMModuleGetFunction(module: *LLVM.Module, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value.Constant.Function;
pub extern fn NativityLLVModuleCreateFunction(module: *LLVM.Module, function_type: *LLVM.Type.Function, linkage: LLVM.Linkage, address_space: c_uint, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value.Constant.Function;
pub extern fn NativityLLVMModuleCreateDebugInfoBuilder(module: *LLVM.Module) ?*LLVM.DebugInfo.Builder;
pub extern fn NativityLLVMDebugInfoBuilderCreateFile(builder: *LLVM.DebugInfo.Builder, filename_ptr: [*]const u8, filename_len: usize, directory_ptr: [*]const u8, directory_len: usize) ?*LLVM.DebugInfo.File;
pub extern fn NativityLLVMDebugInfoBuilderCreateCompileUnit(builder: *LLVM.DebugInfo.Builder, language: LLVM.DebugInfo.Language, file: *LLVM.DebugInfo.File, producer_ptr: [*]const u8, producer_len: usize, is_optimized: bool, flags_ptr: [*]const u8, flags_len: usize, runtime_version: c_uint, split_name_ptr: [*]const u8, split_name_len: usize, debug_info_emission_kind: LLVM.DebugInfo.CompileUnit.EmissionKind, DWOId: u64, split_debug_inlining: bool, debug_info_for_profiling: bool, debug_info_name_table_kind: LLVM.DebugInfo.CompileUnit.NameTableKind, ranges_base_address: bool, sysroot_ptr: [*]const u8, sysroot_len: usize, sdk_ptr: [*]const u8, sdk_len: usize) ?*LLVM.DebugInfo.CompileUnit;
pub extern fn NativityLLVMDebugInfoBuilderCreateFunction(builder: *LLVM.DebugInfo.Builder, scope: *LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, linkage_name_ptr: [*]const u8, linkage_name_len: usize, file: *LLVM.DebugInfo.File, line_number: c_uint, type: *LLVM.DebugInfo.Type.Subroutine, scope_line: c_uint, flags: LLVM.DebugInfo.Node.Flags, subprogram_flags: LLVM.DebugInfo.Subprogram.Flags, declaration: ?*LLVM.DebugInfo.Subprogram) ?*LLVM.DebugInfo.Subprogram;
pub extern fn NativityLLVMDebugInfoBuilderCreateSubroutineType(builder: *LLVM.DebugInfo.Builder, parameter_types_ptr: [*]const *LLVM.DebugInfo.Type, parameter_type_count: usize, flags: LLVM.DebugInfo.Node.Flags, calling_convention: LLVM.DebugInfo.CallingConvention) ?*LLVM.DebugInfo.Type.Subroutine;
pub extern fn NativityLLVMDebugInfoBuilderCreateLexicalBlock(builder: *LLVM.DebugInfo.Builder, parent_scope: *LLVM.DebugInfo.Scope, parent_file: *LLVM.DebugInfo.File, line: c_uint, column: c_uint) ?*LLVM.DebugInfo.LexicalBlock;
pub extern fn NativityLLVModuleCreateFunction(module: *LLVM.Module, function_type: *LLVM.Type.Function, linkage: LLVM.Linkage, address_space: c_uint, name_ptr: [*]const u8, name_len: usize) *LLVM.Value.Constant.Function;
pub extern fn NativityLLVMModuleCreateDebugInfoBuilder(module: *LLVM.Module) *LLVM.DebugInfo.Builder;
pub extern fn NativityLLVMDebugInfoBuilderCreateFile(builder: *LLVM.DebugInfo.Builder, filename_ptr: [*]const u8, filename_len: usize, directory_ptr: [*]const u8, directory_len: usize) *LLVM.DebugInfo.File;
pub extern fn NativityLLVMDebugInfoBuilderCreateCompileUnit(builder: *LLVM.DebugInfo.Builder, language: LLVM.DebugInfo.Language, file: *LLVM.DebugInfo.File, producer_ptr: [*]const u8, producer_len: usize, is_optimized: bool, flags_ptr: [*]const u8, flags_len: usize, runtime_version: c_uint, split_name_ptr: [*]const u8, split_name_len: usize, debug_info_emission_kind: LLVM.DebugInfo.CompileUnit.EmissionKind, DWOId: u64, split_debug_inlining: bool, debug_info_for_profiling: bool, debug_info_name_table_kind: LLVM.DebugInfo.CompileUnit.NameTableKind, ranges_base_address: bool, sysroot_ptr: [*]const u8, sysroot_len: usize, sdk_ptr: [*]const u8, sdk_len: usize) *LLVM.DebugInfo.CompileUnit;
pub extern fn NativityLLVMDebugInfoBuilderCreateFunction(builder: *LLVM.DebugInfo.Builder, scope: *LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, linkage_name_ptr: [*]const u8, linkage_name_len: usize, file: *LLVM.DebugInfo.File, line_number: c_uint, type: *LLVM.DebugInfo.Type.Subroutine, scope_line: c_uint, flags: LLVM.DebugInfo.Node.Flags, subprogram_flags: LLVM.DebugInfo.Subprogram.Flags, declaration: ?*LLVM.DebugInfo.Subprogram) *LLVM.DebugInfo.Subprogram;
pub extern fn NativityLLVMDebugInfoBuilderCreateSubroutineType(builder: *LLVM.DebugInfo.Builder, parameter_types_ptr: [*]const *LLVM.DebugInfo.Type, parameter_type_count: usize, flags: LLVM.DebugInfo.Node.Flags, calling_convention: LLVM.DebugInfo.CallingConvention) *LLVM.DebugInfo.Type.Subroutine;
pub extern fn NativityLLVMDebugInfoBuilderCreateLexicalBlock(builder: *LLVM.DebugInfo.Builder, parent_scope: *LLVM.DebugInfo.Scope, parent_file: *LLVM.DebugInfo.File, line: c_uint, column: c_uint) *LLVM.DebugInfo.LexicalBlock;
pub extern fn NativityLLVMDebugInfoBuilderCreateExpression(builder: *LLVM.DebugInfo.Builder, address: [*]const u64, length: usize) *LLVM.DebugInfo.Expression;
pub extern fn NativityLLVMDebugInfoBuilderCreateGlobalVariableExpression(builder: *LLVM.DebugInfo.Builder, scope: *LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, linkage_name_ptr: [*]const u8, linkage_name_len: usize, file: *LLVM.DebugInfo.File, line_number: c_uint, type: *LLVM.DebugInfo.Type, is_local_to_unit: bool, is_defined: bool, expression: ?*LLVM.DebugInfo.Expression, declaration: ?*LLVM.Metadata.Node, template_parameters: ?*LLVM.Metadata.Tuple, alignment: u32) ?*LLVM.DebugInfo.GlobalVariableExpression;
pub extern fn NativityLLVMDebugInfoBuilderCreateParameterVariable(builder: *LLVM.DebugInfo.Builder, scope: *LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, argument_index: c_uint, file: *LLVM.DebugInfo.File, line_number: c_uint, type: *LLVM.DebugInfo.Type, always_preserve: bool, flags: LLVM.DebugInfo.Node.Flags) ?*LLVM.DebugInfo.LocalVariable;
pub extern fn NativityLLVMDebugInfoBuilderCreateAutoVariable(builder: *LLVM.DebugInfo.Builder, scope: *LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, file: *LLVM.DebugInfo.File, line_number: c_uint, type: *LLVM.DebugInfo.Type, always_preserve: bool, flags: LLVM.DebugInfo.Node.Flags, alignment: u32) ?*LLVM.DebugInfo.LocalVariable; // 0 means 1 << 0 (alignment of 1)
pub extern fn NativityLLVMDebugInfoBuilderInsertDeclare(builder: *LLVM.DebugInfo.Builder, pointer: *LLVM.Value, local_variable: *LLVM.DebugInfo.LocalVariable, context: *LLVM.Context, line: c_uint, column: c_uint, scope: *LLVM.DebugInfo.Scope, basic_block: *LLVM.Value.BasicBlock) ?*LLVM.Value.Instruction;
pub extern fn NativityLLVMDebugInfoBuilderCreateGlobalVariableExpression(builder: *LLVM.DebugInfo.Builder, scope: *LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, linkage_name_ptr: [*]const u8, linkage_name_len: usize, file: *LLVM.DebugInfo.File, line_number: c_uint, type: *LLVM.DebugInfo.Type, is_local_to_unit: bool, is_defined: bool, expression: ?*LLVM.DebugInfo.Expression, declaration: ?*LLVM.Metadata.Node, template_parameters: ?*LLVM.Metadata.Tuple, alignment: u32) *LLVM.DebugInfo.GlobalVariableExpression;
pub extern fn NativityLLVMDebugInfoBuilderCreateParameterVariable(builder: *LLVM.DebugInfo.Builder, scope: *LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, argument_index: c_uint, file: *LLVM.DebugInfo.File, line_number: c_uint, type: *LLVM.DebugInfo.Type, always_preserve: bool, flags: LLVM.DebugInfo.Node.Flags) *LLVM.DebugInfo.LocalVariable;
pub extern fn NativityLLVMDebugInfoBuilderCreateAutoVariable(builder: *LLVM.DebugInfo.Builder, scope: *LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, file: *LLVM.DebugInfo.File, line_number: c_uint, type: *LLVM.DebugInfo.Type, always_preserve: bool, flags: LLVM.DebugInfo.Node.Flags, alignment: u32) *LLVM.DebugInfo.LocalVariable; // 0 means 1 << 0 (alignment of 1)
pub extern fn NativityLLVMDebugInfoBuilderInsertDeclare(builder: *LLVM.DebugInfo.Builder, pointer: *LLVM.Value, local_variable: *LLVM.DebugInfo.LocalVariable, context: *LLVM.Context, line: c_uint, column: c_uint, scope: *LLVM.DebugInfo.Scope, basic_block: *LLVM.Value.BasicBlock) *LLVM.Value.Instruction;
pub extern fn NativityLLVMDebugInfoBuilderCreateBasicType(builder: *LLVM.DebugInfo.Builder, name_ptr: [*]const u8, name_len: usize, bit_count: u64, dwarf_encoding: LLVM.DebugInfo.AttributeType, flags: LLVM.DebugInfo.Node.Flags) ?*LLVM.DebugInfo.Type;
pub extern fn NativityLLVMDebugInfoBuilderCreatePointerType(builder: *LLVM.DebugInfo.Builder, element_type: *LLVM.DebugInfo.Type, pointer_bit_count: u64, alignment: u32, name_ptr: [*]const u8, name_len: usize) ?*LLVM.DebugInfo.Type.Derived;
pub extern fn NativityLLVMDebugInfoBuilderCreateStructType(builder: *LLVM.DebugInfo.Builder, scope: ?*LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, file: ?*LLVM.DebugInfo.File, line_number: c_uint, bit_count: u64, alignment: u32, flags: LLVM.DebugInfo.Node.Flags, derived_from: ?*LLVM.DebugInfo.Type, element_type_ptr: [*]const *LLVM.DebugInfo.Type, element_type_count: usize, forward_declaration: ?*LLVM.DebugInfo.Type.Composite) ?*LLVM.DebugInfo.Type.Composite;
pub extern fn NativityLLVMDebugInfoBuilderCreateArrayType(builder: *LLVM.DebugInfo.Builder, bit_size: u64, alignment: u32, type: *LLVM.DebugInfo.Type, element_count: usize) ?*LLVM.DebugInfo.Type.Composite;
pub extern fn NativityLLVMDebugInfoBuilderCreateEnumerationType(builder: *LLVM.DebugInfo.Builder, scope: ?*LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, file: *LLVM.DebugInfo.File, line: c_uint, bit_size: u64, alignment: u32, enumerator_ptr: [*]const *LLVM.DebugInfo.Type.Enumerator, enumerator_count: usize, underlying_type: *LLVM.DebugInfo.Type) ?*LLVM.DebugInfo.Type.Composite;
pub extern fn NativityLLVMDebugInfoBuilderCreateEnumerator(builder: *LLVM.DebugInfo.Builder, name_ptr: [*]const u8, name_len: usize, value: u64, is_unsigned: bool) ?*LLVM.DebugInfo.Type.Enumerator;
pub extern fn NativityLLVMDebugInfoBuilderCreateReplaceableCompositeType(builder: *LLVM.DebugInfo.Builder, tag: c_uint, name_ptr: [*]const u8, name_len: usize, scope: ?*LLVM.DebugInfo.Scope, file: ?*LLVM.DebugInfo.File, line: c_uint) ?*LLVM.DebugInfo.Type.Composite;
pub extern fn NativityLLVMDebugInfoBuilderCreateBasicType(builder: *LLVM.DebugInfo.Builder, name_ptr: [*]const u8, name_len: usize, bit_count: u64, dwarf_encoding: LLVM.DebugInfo.AttributeType, flags: LLVM.DebugInfo.Node.Flags) *LLVM.DebugInfo.Type;
pub extern fn NativityLLVMDebugInfoBuilderCreatePointerType(builder: *LLVM.DebugInfo.Builder, element_type: *LLVM.DebugInfo.Type, pointer_bit_count: u64, alignment: u32, name_ptr: [*]const u8, name_len: usize) *LLVM.DebugInfo.Type.Derived;
pub extern fn NativityLLVMDebugInfoBuilderCreateStructType(builder: *LLVM.DebugInfo.Builder, scope: ?*LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, file: ?*LLVM.DebugInfo.File, line_number: c_uint, bit_count: u64, alignment: u32, flags: LLVM.DebugInfo.Node.Flags, derived_from: ?*LLVM.DebugInfo.Type, element_type_ptr: [*]const *LLVM.DebugInfo.Type, element_type_count: usize, forward_declaration: ?*LLVM.DebugInfo.Type.Composite) *LLVM.DebugInfo.Type.Composite;
pub extern fn NativityLLVMDebugInfoBuilderCreateArrayType(builder: *LLVM.DebugInfo.Builder, bit_size: u64, alignment: u32, type: *LLVM.DebugInfo.Type, element_count: usize) *LLVM.DebugInfo.Type.Composite;
pub extern fn NativityLLVMDebugInfoBuilderCreateEnumerationType(builder: *LLVM.DebugInfo.Builder, scope: ?*LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, file: *LLVM.DebugInfo.File, line: c_uint, bit_size: u64, alignment: u32, enumerator_ptr: [*]const *LLVM.DebugInfo.Type.Enumerator, enumerator_count: usize, underlying_type: *LLVM.DebugInfo.Type) *LLVM.DebugInfo.Type.Composite;
pub extern fn NativityLLVMDebugInfoBuilderCreateEnumerator(builder: *LLVM.DebugInfo.Builder, name_ptr: [*]const u8, name_len: usize, value: u64, is_unsigned: bool) *LLVM.DebugInfo.Type.Enumerator;
pub extern fn NativityLLVMDebugInfoBuilderCreateReplaceableCompositeType(builder: *LLVM.DebugInfo.Builder, tag: c_uint, name_ptr: [*]const u8, name_len: usize, scope: ?*LLVM.DebugInfo.Scope, file: ?*LLVM.DebugInfo.File, line: c_uint) *LLVM.DebugInfo.Type.Composite;
pub extern fn NativityLLVMDebugInfoBuilderCreateMemberType(builder: *LLVM.DebugInfo.Builder, scope: ?*LLVM.DebugInfo.Scope, name_ptr: [*]const u8, name_len: usize, file: ?*LLVM.DebugInfo.File, line_number: c_uint, bit_size: u64, alignment: u32, bit_offset: u64, flags: LLVM.DebugInfo.Node.Flags, type: *LLVM.DebugInfo.Type) *LLVM.DebugInfo.Type.Derived;
pub extern fn NativityLLVMDebugInfoBuilderCompositeTypeReplaceTypes(builder: *LLVM.DebugInfo.Builder, type: *LLVM.DebugInfo.Type.Composite, element_type_ptr: [*]const *LLVM.DebugInfo.Type, element_type_count: usize) void;
pub extern fn NativityLLLVMDITypeIsResolved(type: *LLVM.DebugInfo.Type) bool;
pub extern fn NativityLLVMDebugInfoBuilderFinalizeSubprogram(builder: *LLVM.DebugInfo.Builder, subprogram: *LLVM.DebugInfo.Subprogram, function: *LLVM.Value.Constant.Function) void;
pub extern fn NativityLLVMDebugInfoBuilderFinalize(builder: *LLVM.DebugInfo.Builder) void;
pub extern fn NativityLLVMDebugInfoSubprogramGetFile(subprogram: *LLVM.DebugInfo.Subprogram) ?*LLVM.DebugInfo.File;
pub extern fn NativityLLVMDebugInfoSubprogramGetArgumentType(subprogram: *LLVM.DebugInfo.Subprogram, argument_index: usize) ?*LLVM.DebugInfo.Type;
pub extern fn NativityLLVMDebugInfoSubprogramGetFile(subprogram: *LLVM.DebugInfo.Subprogram) *LLVM.DebugInfo.File;
pub extern fn NativityLLVMDebugInfoSubprogramGetArgumentType(subprogram: *LLVM.DebugInfo.Subprogram, argument_index: usize) *LLVM.DebugInfo.Type;
pub extern fn NativityLLVMDebugInfoScopeToSubprogram(scope: *LLVM.DebugInfo.Scope) ?*LLVM.DebugInfo.Subprogram;
pub extern fn NativityLLVMCreateBasicBlock(context: *LLVM.Context, name_ptr: [*]const u8, name_len: usize, parent_function: ?*LLVM.Value.Constant.Function, insert_before: ?*LLVM.Value.BasicBlock) ?*LLVM.Value.BasicBlock;
pub extern fn NativityLLVMCreateBasicBlock(context: *LLVM.Context, name_ptr: [*]const u8, name_len: usize, parent_function: ?*LLVM.Value.Constant.Function, insert_before: ?*LLVM.Value.BasicBlock) *LLVM.Value.BasicBlock;
pub extern fn NativityLLVMBasicBlockRemoveFromParent(basic_block: *LLVM.Value.BasicBlock) void;
pub extern fn NativityLLVMBuilderSetInsertPoint(builder: *LLVM.Builder, basic_block: *LLVM.Value.BasicBlock) void;
pub extern fn NativityLLVMBuilderGetInsertBlock(builder: *LLVM.Builder) ?*LLVM.Value.BasicBlock;
pub extern fn NativityLLVMBuilderGetInsertBlock(builder: *LLVM.Builder) *LLVM.Value.BasicBlock;
pub extern fn NativityLLVMBuilderSetCurrentDebugLocation(builder: *LLVM.Builder, context: *LLVM.Context, line: c_uint, column: c_uint, scope: *LLVM.DebugInfo.Scope, function: *LLVM.Value.Constant.Function) void;
pub extern fn NativityLLVMValueSetName(value: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) void;
pub extern fn NativityLLVMValueGetType(value: *LLVM.Value) *LLVM.Type;
pub extern fn NativityLLVMValueGetContext(value: *LLVM.Value) *LLVM.Context;
pub extern fn NativityLLVMArgumentGetIndex(argument: *LLVM.Value.Argument) c_uint;
pub extern fn NativityLLVMFunctionGetArguments(function: *LLVM.Value.Constant.Function, argument_ptr: [*]*LLVM.Value.Argument, argument_len: *usize) void;
pub extern fn NativityLLVMFunctionGetArgument(function: *LLVM.Value.Constant.Function, index: c_uint) ?*LLVM.Value.Argument;
pub extern fn NativityLLVMFunctionGetArgument(function: *LLVM.Value.Constant.Function, index: c_uint) *LLVM.Value.Argument;
pub extern fn NativityLLVMFunctionGetType(function: *LLVM.Value.Constant.Function) *LLVM.Type.Function;
pub extern fn NativityLLVMFunctionTypeGetReturnType(function_type: *LLVM.Type.Function) *LLVM.Type;
pub extern fn NativityLLVMTypeIsVoid(type: *LLVM.Type) bool;
pub extern fn NativityLLVMBuilderCreateAlloca(builder: *LLVM.Builder, type: *LLVM.Type, address_space: c_uint, array_size: ?*LLVM.Value, name_ptr: [*]const u8, name_len: usize, alignment: u32) ?*LLVM.Value.Instruction.Alloca;
pub extern fn NativityLLVMBuilderCreateStore(builder: *LLVM.Builder, value: *LLVM.Value, pointer: *LLVM.Value, is_volatile: bool, alignment: u32) ?*LLVM.Value.Instruction.Store;
pub extern fn NativityLLVMBuilderCreateAlloca(builder: *LLVM.Builder, type: *LLVM.Type, address_space: c_uint, array_size: ?*LLVM.Value, name_ptr: [*]const u8, name_len: usize, alignment: u32) *LLVM.Value.Instruction.Alloca;
pub extern fn NativityLLVMBuilderCreateStore(builder: *LLVM.Builder, value: *LLVM.Value, pointer: *LLVM.Value, is_volatile: bool, alignment: u32) *LLVM.Value.Instruction.Store;
pub extern fn NativityLLVMBuilderCreateMemcpy(builder: *LLVM.Builder, destination: *LLVM.Value, destination_alignment: u32, source: *LLVM.Value, source_alignment: u32, size: u64, is_volatile: bool) *LLVM.Value.Instruction.Call;
pub extern fn NativityLLVMContextGetConstantInt(context: *LLVM.Context, bit_count: c_uint, value: u64, is_signed: bool) ?*LLVM.Value.Constant.Int;
pub extern fn NativityLLVMContextGetConstantString(context: *LLVM.Context, name_ptr: [*]const u8, name_len: usize, null_terminate: bool) ?*LLVM.Value.Constant;
pub extern fn NativityLLVMGetConstantArray(array_type: *LLVM.Type.Array, value_ptr: [*]const *LLVM.Value.Constant, value_count: usize) ?*LLVM.Value.Constant;
pub extern fn NativityLLVMGetConstantStruct(struct_type: *LLVM.Type.Struct, constant_ptr: [*]const *LLVM.Value.Constant, constant_len: usize) ?*LLVM.Value.Constant;
pub extern fn NativityLLVMContextGetConstantInt(context: *LLVM.Context, bit_count: c_uint, value: u64, is_signed: bool) *LLVM.Value.Constant.Int;
pub extern fn NativityLLVMContextGetConstantString(context: *LLVM.Context, name_ptr: [*]const u8, name_len: usize, null_terminate: bool) *LLVM.Value.Constant;
pub extern fn NativityLLVMGetConstantArray(array_type: *LLVM.Type.Array, value_ptr: [*]const *LLVM.Value.Constant, value_count: usize) *LLVM.Value.Constant;
pub extern fn NativityLLVMGetConstantStruct(struct_type: *LLVM.Type.Struct, constant_ptr: [*]const *LLVM.Value.Constant, constant_len: usize) *LLVM.Value.Constant;
pub extern fn NativityLLVMConstantToInt(constant: *LLVM.Value.Constant) ?*LLVM.Value.Constant.Int;
pub extern fn NativityLLVMBuilderCreateICmp(builder: *LLVM.Builder, integer_comparison: LLVM.Value.Instruction.ICmp.Kind, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateLoad(builder: *LLVM.Builder, type: *LLVM.Type, value: *LLVM.Value, is_volatile: bool, name_ptr: [*]const u8, name_len: usize, alignment: u32) ?*LLVM.Value.Instruction.Load;
pub extern fn NativityLLVMBuilderCreateRet(builder: *LLVM.Builder, value: ?*LLVM.Value) ?*LLVM.Value.Instruction.Ret;
pub extern fn NativityLLVMBuilderCreateCast(builder: *LLVM.Builder, cast_type: LLVM.Value.Instruction.Cast.Type, value: *LLVM.Value, type: *LLVM.Type, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateICmp(builder: *LLVM.Builder, integer_comparison: LLVM.Value.Instruction.ICmp.Kind, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateLoad(builder: *LLVM.Builder, type: *LLVM.Type, value: *LLVM.Value, is_volatile: bool, name_ptr: [*]const u8, name_len: usize, alignment: u32) *LLVM.Value.Instruction.Load;
pub extern fn NativityLLVMBuilderCreateRet(builder: *LLVM.Builder, value: ?*LLVM.Value) *LLVM.Value.Instruction.Ret;
pub extern fn NativityLLVMBuilderCreateCast(builder: *LLVM.Builder, cast_type: LLVM.Value.Instruction.Cast.Type, value: *LLVM.Value, type: *LLVM.Type, name_ptr: [*]const u8, name_len: usize) *LLVM.Value;
pub extern fn NativityLLVMContextGetAttributeFromEnum(context: *LLVM.Context, attribute_key: LLVM.Attribute.Id, attribute_value: u64) *LLVM.Attribute;
pub extern fn NativityLLVMContextGetAttributeFromString(context: *LLVM.Context, key_ptr: [*]const u8, key_len: usize, value_ptr: [*]const u8, value_len: usize) *LLVM.Attribute;
pub extern fn NativityLLVMContextGetAttributeFromType(context: *LLVM.Context, attribute_key: LLVM.Attribute.Id, type: *LLVM.Type) *LLVM.Attribute;
@ -73,50 +75,51 @@ pub extern fn NativityLLVMContextGetAttributeSet(context: *LLVM.Context, attribu
pub extern fn NativityLLVMFunctionSetAttributes(function: *LLVM.Value.Constant.Function, context: *LLVM.Context, function_attributes: *const LLVM.Attribute.Set, return_attributes: *const LLVM.Attribute.Set, parameter_attribute_set_ptr: [*]const *const LLVM.Attribute.Set, parameter_attribute_set_count: usize) void;
pub extern fn NativityLLVMCallSetAttributes(call: *LLVM.Value.Instruction.Call, context: *LLVM.Context, function_attributes: *const LLVM.Attribute.Set, return_attributes: *const LLVM.Attribute.Set, parameter_attribute_set_ptr: [*]const *const LLVM.Attribute.Set, parameter_attribute_set_count: usize) void;
// pub extern fn NativityLLVMFunctionAddAttributeKey(builder: *LLVM.Value.Constant.Function, attribute_key: LLVM.Attribute) void;
pub extern fn NativityLLVMGetVoidType(context: *LLVM.Context) ?*LLVM.Type;
pub extern fn NativityLLVMGetInlineAssembly(function_type: *LLVM.Type.Function, assembly_ptr: [*]const u8, assembly_len: usize, constraints_ptr: [*]const u8, constrains_len: usize, has_side_effects: bool, is_align_stack: bool, dialect: LLVM.Value.InlineAssembly.Dialect, can_throw: bool) ?*LLVM.Value.InlineAssembly;
pub extern fn NativityLLVMBuilderCreateCall(builder: *LLVM.Builder, function_type: *LLVM.Type.Function, callee: *LLVM.Value, argument_ptr: [*]const *LLVM.Value, argument_count: usize, name_ptr: [*]const u8, name_len: usize, fp_math_tag: ?*LLVM.Metadata.Node) ?*LLVM.Value.Instruction.Call;
pub extern fn NativityLLVMBuilderCreateUnreachable(builder: *LLVM.Builder) ?*LLVM.Value.Instruction.Unreachable;
pub extern fn NativityLLVMModuleAddGlobalVariable(module: *LLVM.Module, type: *LLVM.Type, is_constant: bool, linkage: LLVM.Linkage, initializer: ?*LLVM.Value.Constant, name_ptr: [*]const u8, name_len: usize, insert_before: ?*LLVM.Value.Constant.GlobalVariable, thread_local_mode: LLVM.ThreadLocalMode, address_space: c_uint, externally_initialized: bool) ?*LLVM.Value.Constant.GlobalVariable;
pub extern fn NativityLLVMGetVoidType(context: *LLVM.Context) *LLVM.Type;
pub extern fn NativityLLVMGetInlineAssembly(function_type: *LLVM.Type.Function, assembly_ptr: [*]const u8, assembly_len: usize, constraints_ptr: [*]const u8, constrains_len: usize, has_side_effects: bool, is_align_stack: bool, dialect: LLVM.Value.InlineAssembly.Dialect, can_throw: bool) *LLVM.Value.InlineAssembly;
pub extern fn NativityLLVMBuilderCreateCall(builder: *LLVM.Builder, function_type: *LLVM.Type.Function, callee: *LLVM.Value, argument_ptr: [*]const *LLVM.Value, argument_count: usize, name_ptr: [*]const u8, name_len: usize, fp_math_tag: ?*LLVM.Metadata.Node) *LLVM.Value.Instruction.Call;
pub extern fn NativityLLVMBuilderCreateUnreachable(builder: *LLVM.Builder) *LLVM.Value.Instruction.Unreachable;
pub extern fn NativityLLVMModuleAddGlobalVariable(module: *LLVM.Module, type: *LLVM.Type, is_constant: bool, linkage: LLVM.Linkage, initializer: ?*LLVM.Value.Constant, name_ptr: [*]const u8, name_len: usize, insert_before: ?*LLVM.Value.Constant.GlobalVariable, thread_local_mode: LLVM.ThreadLocalMode, address_space: c_uint, externally_initialized: bool) *LLVM.Value.Constant.GlobalVariable;
pub extern fn NativityLLVMBuilderCreateAdd(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, no_unsigned_wrapping: bool, no_signed_wrapping: bool) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateSub(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, no_unsigned_wrapping: bool, no_signed_wrapping: bool) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateMultiply(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, no_unsigned_wrapping: bool, no_signed_wrapping: bool) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateShiftLeft(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, no_unsigned_wrapping: bool, no_signed_wrapping: bool) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateAdd(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, no_unsigned_wrapping: bool, no_signed_wrapping: bool) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateSub(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, no_unsigned_wrapping: bool, no_signed_wrapping: bool) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateMultiply(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, no_unsigned_wrapping: bool, no_signed_wrapping: bool) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateShiftLeft(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, no_unsigned_wrapping: bool, no_signed_wrapping: bool) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateUDiv(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, is_exact: bool) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateSDiv(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, is_exact: bool) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateURem(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateSRem(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateLogicalShiftRight(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, is_exact: bool) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateArithmeticShiftRight(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, is_exact: bool) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateUDiv(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, is_exact: bool) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateSDiv(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, is_exact: bool) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateURem(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateSRem(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateLogicalShiftRight(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, is_exact: bool) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateArithmeticShiftRight(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize, is_exact: bool) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateXor(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateAnd(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateOr(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateGEP(builder: *LLVM.Builder, type: *LLVM.Type, pointer: *LLVM.Value, index_ptr: [*]const *LLVM.Value, index_count: usize, name_ptr: [*]const u8, name_len: usize, in_bounds: bool) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateStructGEP(builder: *LLVM.Builder, type: *LLVM.Type, pointer: *LLVM.Value, index: c_uint, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateBranch(builder: *LLVM.Builder, basic_block: *LLVM.Value.BasicBlock) ?*LLVM.Value.Instruction.Branch;
pub extern fn NativityLLVMBuilderCreateConditionalBranch(builder: *LLVM.Builder, condition: *LLVM.Value, true_block: *LLVM.Value.BasicBlock, false_block: *LLVM.Value.BasicBlock, branch_weights: ?*LLVM.Metadata.Node, unpredictable: ?*LLVM.Metadata.Node) ?*LLVM.Value.Instruction.Branch;
pub extern fn NativityLLVMBuilderCreateXor(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateAnd(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateOr(builder: *LLVM.Builder, left: *LLVM.Value, right: *LLVM.Value, name_ptr: [*]const u8, name_len: usize) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateGEP(builder: *LLVM.Builder, type: *LLVM.Type, pointer: *LLVM.Value, index_ptr: [*]const *LLVM.Value, index_count: usize, name_ptr: [*]const u8, name_len: usize, in_bounds: bool) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateStructGEP(builder: *LLVM.Builder, type: *LLVM.Type, pointer: *LLVM.Value, index: c_uint, name_ptr: [*]const u8, name_len: usize) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateBranch(builder: *LLVM.Builder, basic_block: *LLVM.Value.BasicBlock) *LLVM.Value.Instruction.Branch;
pub extern fn NativityLLVMBuilderCreateConditionalBranch(builder: *LLVM.Builder, condition: *LLVM.Value, true_block: *LLVM.Value.BasicBlock, false_block: *LLVM.Value.BasicBlock, branch_weights: ?*LLVM.Metadata.Node, unpredictable: ?*LLVM.Metadata.Node) *LLVM.Value.Instruction.Branch;
pub extern fn NativityLLVMBuilderCreateSwitch(builder: *LLVM.Builder, condition: *LLVM.Value, default_block: ?*LLVM.Value.BasicBlock, case_ptr: [*]const *LLVM.Value.Constant.Int, case_block_ptr: [*]const *LLVM.Value.BasicBlock, case_count: c_uint, branch_weights: ?*LLVM.Metadata.Node, unpredictable: ?*LLVM.Metadata.Node) *LLVM.Value.Instruction.Switch;
pub extern fn NativityLLVMVerifyFunction(function: *LLVM.Value.Constant.Function, message_ptr: *[*]const u8, message_len: *usize) bool;
pub extern fn NativityLLVMVerifyModule(module: *LLVM.Module, message_ptr: *[*]const u8, message_len: *usize) bool;
pub extern fn NativityLLVMModuleToString(module: *LLVM.Module, message_len: *usize) [*]const u8;
pub extern fn NativityLLVMFunctionToString(function: *LLVM.Value.Constant.Function, message_len: *usize) [*]const u8;
pub extern fn NativityLLVMValueToString(value: *LLVM.Value, message_len: *usize) [*]const u8;
pub extern fn NativityLLVMModuleToString(module: *LLVM.Module, message_pointer: *[*]const u8, message_len: *usize) void;
pub extern fn NativityLLVMFunctionToString(function: *LLVM.Value.Constant.Function, message_pointer: *[*]const u8, message_len: *usize) void;
pub extern fn NativityLLVMValueToString(value: *LLVM.Value, message_pointer: *[*]const u8, message_len: *usize) void;
pub extern fn NativityLLVMBuilderIsCurrentBlockTerminated(builder: *LLVM.Builder) bool;
pub extern fn NativityLLVMGetUndefined(type: *LLVM.Type) ?*LLVM.Value.Constant.Undefined;
pub extern fn NativityLLVMGetPoisonValue(type: *LLVM.Type) ?*LLVM.Value.Constant.Poison;
pub extern fn NativityLLVMGetUndefined(type: *LLVM.Type) *LLVM.Value.Constant.Undefined;
pub extern fn NativityLLVMGetPoisonValue(type: *LLVM.Type) *LLVM.Value.Constant.Poison;
pub extern fn NativityLLVMTypeGetContext(type: *LLVM.Type) *LLVM.Context;
pub extern fn NativityLLVMFunctionSetCallingConvention(function: *LLVM.Value.Constant.Function, calling_convention: LLVM.Value.Constant.Function.CallingConvention) void;
pub extern fn NativityLLVMFunctionGetCallingConvention(function: *LLVM.Value.Constant.Function) LLVM.Value.Constant.Function.CallingConvention;
pub extern fn NativityLLVMFunctionSetSubprogram(function: *LLVM.Value.Constant.Function, subprogram: *LLVM.DebugInfo.Subprogram) void;
pub extern fn NativityLLVMFunctionGetSubprogram(function: *LLVM.Value.Constant.Function) ?*LLVM.DebugInfo.Subprogram;
pub extern fn NativityLLVMFunctionGetSubprogram(function: *LLVM.Value.Constant.Function) *LLVM.DebugInfo.Subprogram;
pub extern fn NativityLLVMCallSetCallingConvention(instruction: *LLVM.Value.Instruction.Call, calling_convention: LLVM.Value.Constant.Function.CallingConvention) void;
pub extern fn NativityLLVMGetStruct(struct_type: *LLVM.Type.Struct, constant_ptr: [*]const *LLVM.Value.Constant, constant_len: usize) ?*LLVM.Value.Constant;
pub extern fn NativityLLVMGetStruct(struct_type: *LLVM.Type.Struct, constant_ptr: [*]const *LLVM.Value.Constant, constant_len: usize) *LLVM.Value.Constant;
pub extern fn NativityLLVMValueToConstant(value: *LLVM.Value) ?*LLVM.Value.Constant;
pub extern fn NativityLLVMValueToFunction(value: *LLVM.Value) ?*LLVM.Value.Constant.Function;
@ -129,29 +132,30 @@ pub extern fn NativityLLVMTypeToFunction(type: *LLVM.Type) ?*LLVM.Type.Function;
pub extern fn NativityLLVMTypeToArray(type: *LLVM.Type) ?*LLVM.Type.Array;
pub extern fn NativityLLVMTypeToPointer(Type: *LLVM.Type) ?*LLVM.Type.Pointer;
pub extern fn NativityLLVMArrayTypeGetElementType(array_type: *LLVM.Type.Array) ?*LLVM.Type;
pub extern fn NativityLLVMArrayTypeGetElementType(array_type: *LLVM.Type.Array) *LLVM.Type;
pub extern fn NativityLLVMLookupIntrinsic(name_ptr: [*]const u8, name_len: usize) LLVM.Value.IntrinsicID;
pub extern fn NativityLLVMModuleGetIntrinsicDeclaration(module: *LLVM.Module, intrinsic_id: LLVM.Value.IntrinsicID, parameter_types_ptr: [*]const *LLVM.Type, parameter_type_count: usize) ?*LLVM.Value.Constant.Function;
pub extern fn NativityLLVMContextGetIntrinsicType(context: *LLVM.Context, intrinsic_id: LLVM.Value.IntrinsicID, parameter_type_ptr: [*]const *LLVM.Type, parameter_type_count: usize) ?*LLVM.Type.Function;
pub extern fn NativityLLVMBuilderCreateExtractValue(builder: *LLVM.Builder, aggregate: *LLVM.Value, indices_ptr: [*]const c_uint, indices_len: usize, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateInsertValue(builder: *LLVM.Builder, aggregate: *LLVM.Value, value: *LLVM.Value, indices_ptr: [*]const c_uint, indices_len: usize, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value;
pub extern fn NativityLLVMBuilderCreateGlobalString(builder: *LLVM.Builder, string_ptr: [*]const u8, string_len: usize, name_ptr: [*]const u8, name_len: usize, address_space: c_uint, module: *LLVM.Module) ?*LLVM.Value.Constant.GlobalVariable;
pub extern fn NativityLLVMBuilderCreateGlobalStringPointer(builder: *LLVM.Builder, string_ptr: [*]const u8, string_len: usize, name_ptr: [*]const u8, name_len: usize, address_space: c_uint, module: *LLVM.Module) ?*LLVM.Value.Constant;
pub extern fn NativityLLVMModuleGetIntrinsicDeclaration(module: *LLVM.Module, intrinsic_id: LLVM.Value.IntrinsicID, parameter_types_ptr: [*]const *LLVM.Type, parameter_type_count: usize) *LLVM.Value.Constant.Function;
pub extern fn NativityLLVMContextGetIntrinsicType(context: *LLVM.Context, intrinsic_id: LLVM.Value.IntrinsicID, parameter_type_ptr: [*]const *LLVM.Type, parameter_type_count: usize) *LLVM.Type.Function;
pub extern fn NativityLLVMBuilderCreateExtractValue(builder: *LLVM.Builder, aggregate: *LLVM.Value, indices_ptr: [*]const c_uint, indices_len: usize, name_ptr: [*]const u8, name_len: usize) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateInsertValue(builder: *LLVM.Builder, aggregate: *LLVM.Value, value: *LLVM.Value, indices_ptr: [*]const c_uint, indices_len: usize, name_ptr: [*]const u8, name_len: usize) *LLVM.Value;
pub extern fn NativityLLVMBuilderCreateGlobalString(builder: *LLVM.Builder, string_ptr: [*]const u8, string_len: usize, name_ptr: [*]const u8, name_len: usize, address_space: c_uint, module: *LLVM.Module) *LLVM.Value.Constant.GlobalVariable;
pub extern fn NativityLLVMBuilderCreateGlobalStringPointer(builder: *LLVM.Builder, string_ptr: [*]const u8, string_len: usize, name_ptr: [*]const u8, name_len: usize, address_space: c_uint, module: *LLVM.Module) *LLVM.Value.Constant;
pub extern fn NativityLLVMCompareTypes(a: *LLVM.Type, b: *LLVM.Type) bool;
pub extern fn NativityLLVMBuilderCreatePhi(builder: *LLVM.Builder, type: *LLVM.Type, reserved_value_count: c_uint, name_ptr: [*]const u8, name_len: usize) ?*LLVM.Value.Instruction.PhiNode;
pub extern fn NativityLLVMBuilderCreatePhi(builder: *LLVM.Builder, type: *LLVM.Type, reserved_value_count: c_uint, name_ptr: [*]const u8, name_len: usize) *LLVM.Value.Instruction.PhiNode;
pub extern fn NativityLLVMPhiAddIncoming(phi: *LLVM.Value.Instruction.PhiNode, value: *LLVM.Value, basic_block: *LLVM.Value.BasicBlock) void;
pub extern fn NativityLLVMAllocatGetAllocatedType(alloca: *LLVM.Value.Instruction.Alloca) *LLVM.Type;
pub extern fn NativityLLVMValueToAlloca(value: *LLVM.Value) ?*LLVM.Value.Instruction.Alloca;
pub extern fn NativityLLVMGlobalVariableSetInitializer(global_variable: *LLVM.Value.Constant.GlobalVariable, constant_initializer: *LLVM.Value.Constant) void;
pub extern fn NativityLLVMInitializeCodeGeneration() void;
pub extern fn NativityLLVMLinkModules(destination: *LLVM.Module, source: *LLVM.Module, flags: LLVM.LinkFlags) bool;
pub extern fn NativityLLVMGetTarget(target_triple_ptr: [*]const u8, target_triple_len: usize, message_ptr: *[*]const u8, message_len: *usize) ?*LLVM.Target;
pub extern fn NativityLLVMTargetCreateTargetMachine(target: *LLVM.Target, target_triple_ptr: [*]const u8, target_triple_len: usize, cpu_ptr: [*]const u8, cpu_len: usize, features_ptr: [*]const u8, features_len: usize, relocation_model: LLVM.RelocationModel, maybe_code_model: LLVM.CodeModel, is_code_model_present: bool, optimization_level: LLVM.CodegenOptimizationLevel, jit: bool) ?*LLVM.Target.Machine;
pub extern fn NativityLLVMModuleSetTargetMachineDataLayout(module: *LLVM.Module, target_machine: *LLVM.Target.Machine) void;
pub extern fn NativityLLVMModuleSetTargetTriple(module: *LLVM.Module, target_triple_ptr: [*]const u8, target_triple_len: usize) void;
pub extern fn NativityLLVMTargetCreateTargetMachine(target: *LLVM.Target, target_triple_ptr: [*]const u8, target_triple_len: usize, cpu_ptr: [*]const u8, cpu_len: usize, features_ptr: [*]const u8, features_len: usize, relocation_model: LLVM.RelocationModel, maybe_code_model: LLVM.CodeModel, is_code_model_present: bool, optimization_level: LLVM.CodegenOptimizationLevel, jit: bool) *LLVM.Target.Machine;
pub extern fn NativityLLVMRunOptimizationPipeline(module: *LLVM.Module, target_machine: *LLVM.Target.Machine, optimization_level: LLVM.OptimizationLevel) void;
pub extern fn NativityLLVMModuleAddPassesToEmitFile(module: *LLVM.Module, target_machine: *LLVM.Target.Machine, object_file_path_ptr: [*]const u8, object_file_path_len: usize, codegen_file_type: LLVM.CodeGenFileType, disable_verify: bool) bool;
pub extern fn NativityLLVMModuleSetTargetMachineDataLayout(module: *LLVM.Module, target_machine: *LLVM.Target.Machine) void;
pub extern fn NativityLLVMModuleSetTargetTriple(module: *LLVM.Module, target_triple_ptr: [*]const u8, target_triple_len: usize) void;
pub extern fn NativityLLVMTypeAssertEqual(a: *LLVM.Type, b: *LLVM.Type) void;
pub extern fn LLVMInitializeAArch64TargetInfo() void;

3597
bootstrap/compiler.zig Normal file

File diff suppressed because it is too large Load Diff

View File

@ -70,12 +70,16 @@ pub const Arena = struct {
}
}
pub inline fn new(arena: *Arena, comptime T: type) !*T {
pub fn align_forward(arena: *Arena, alignment: u64) void {
arena.position = std.mem.alignForward(u64, arena.position, alignment);
}
pub fn new(arena: *Arena, comptime T: type) !*T {
const result: *T = @ptrCast(@alignCast(try arena.allocate(@sizeOf(T))));
return result;
}
pub inline fn new_array(arena: *Arena, comptime T: type, count: usize) ![]T {
pub fn new_array(arena: *Arena, comptime T: type, count: usize) ![]T {
const result: [*]T = @ptrCast(@alignCast(try arena.allocate(@sizeOf(T) * count)));
return result[0..count];
}
@ -155,17 +159,19 @@ const pinned_array_page_size = 2 * 1024 * 1024;
const pinned_array_max_size = std.math.maxInt(u32) - pinned_array_page_size;
const pinned_array_default_granularity = pinned_array_page_size;
const small_granularity = std.mem.page_size;
const large_granularity = 2 * 1024 * 1024;
// This must be used with big arrays, which are not resizeable (can't be cleared)
pub fn PinnedArray(comptime T: type) type {
return PinnedArrayAdvanced(T, null);
return PinnedArrayAdvanced(T, null, small_granularity);
}
// This must be used with big arrays, which are not resizeable (can't be cleared)
pub fn PinnedArrayAdvanced(comptime T: type, comptime MaybeIndex: ?type) type {
pub fn PinnedArrayAdvanced(comptime T: type, comptime MaybeIndex: ?type, comptime granularity: comptime_int) type {
return struct {
pointer: [*]T = @constCast((&[_]T{}).ptr),
pointer: [*]T = undefined,
length: u32 = 0,
granularity: u32 = 0,
committed: u32 = 0,
pub const Index = if (MaybeIndex) |I| getIndexForType(T, I) else enum(u32) {
null = 0xffff_ffff,
@ -193,38 +199,36 @@ pub fn PinnedArrayAdvanced(comptime T: type, comptime MaybeIndex: ?type) type {
return array.get_unchecked(i);
}
pub fn get_index(array: *Array, item: *T) Index {
pub fn get_index(array: *Array, item: *T) u32 {
const many_item: [*]T = @ptrCast(item);
const result = @intFromPtr(many_item) - @intFromPtr(array.pointer);
const result: u32 = @intCast(@intFromPtr(many_item) - @intFromPtr(array.pointer));
assert(result < pinned_array_max_size);
return @enumFromInt(@divExact(result, @sizeOf(T)));
return @divExact(result, @sizeOf(T));
}
pub fn init(granularity: u32) !Array {
assert(granularity & 0xfff == 0);
const raw_ptr = try reserve(pinned_array_max_size);
try commit(raw_ptr, granularity);
return Array{
.pointer = @alignCast(@ptrCast(raw_ptr)),
.length = 0,
.granularity = granularity,
};
}
pub fn init_with_default_granularity() !Array {
return try Array.init(pinned_array_default_granularity);
pub fn get_typed_index(array: *Array, item: *T) Index {
return @enumFromInt(array.get_index(item));
}
pub fn ensure_capacity(array: *Array, additional: u32) void {
if (array.committed == 0) {
assert(array.length == 0);
array.pointer = @alignCast(@ptrCast(reserve(pinned_array_max_size) catch unreachable));
}
const length = array.length;
const size = length * @sizeOf(T);
const granularity_aligned_size = align_forward(size, array.granularity);
const granularity_aligned_size = align_forward(size, granularity);
const new_size = size + additional * @sizeOf(T);
if (granularity_aligned_size < new_size) {
assert((length + additional) * @sizeOf(T) <= pinned_array_max_size);
const new_granularity_aligned_size = align_forward(new_size, array.granularity);
const ptr: [*]u8 = @ptrCast(array.pointer);
commit(ptr + granularity_aligned_size, new_granularity_aligned_size - granularity_aligned_size) catch unreachable;
const new_granularity_aligned_size = align_forward(new_size, granularity);
const pointer: [*]u8 = @ptrCast(array.pointer);
const commit_pointer = pointer + granularity_aligned_size;
const commit_size = new_granularity_aligned_size - granularity_aligned_size;
commit(commit_pointer, commit_size) catch unreachable;
array.committed += @intCast(@divExact(commit_size, granularity));
}
}
@ -233,30 +237,46 @@ pub fn PinnedArrayAdvanced(comptime T: type, comptime MaybeIndex: ?type) type {
return array.append_with_capacity(item);
}
pub fn append_index(array: *Array, item: T) Index {
pub fn append_index(array: *Array, item: T) u32 {
return array.get_index(array.append(item));
}
pub fn append_typed_index(array: *Array, item: T) Index {
return array.get_typed_index(array.append(item));
}
pub fn append_slice(array: *Array, items: []const T) void {
array.ensure_capacity(@intCast(items.len));
array.append_slice_with_capacity(items);
}
pub fn append_with_capacity(array: *Array, item: T) *T {
pub fn add_one_with_capacity(array: *Array) *T {
const index = array.length;
assert(index * @sizeOf(T) < pinned_array_max_size);
array.length += 1;
const ptr = &array.pointer[index];
return ptr;
}
pub fn add_one(array: *Array) *T{
array.ensure_capacity(1);
return array.add_one_with_capacity();
}
pub fn append_with_capacity(array: *Array, item: T) *T {
const ptr = array.add_one_with_capacity();
ptr.* = item;
return ptr;
}
pub fn append_slice_with_capacity(array: *Array, items: []const T) void {
const index = array.length;
const count: u32 = @intCast(items.len);
assert((index + count - 1) * @sizeOf(T) < pinned_array_max_size);
array.length += count;
@memcpy(array.pointer[index..][0..count], items);
if (items.len > 0) {
const index = array.length;
const count: u32 = @intCast(items.len);
assert((index + count - 1) * @sizeOf(T) < pinned_array_max_size);
array.length += count;
@memcpy(array.pointer[index..][0..count], items);
}
}
pub fn insert(array: *@This(), index: u32, item: T) void {
@ -268,6 +288,14 @@ pub fn PinnedArrayAdvanced(comptime T: type, comptime MaybeIndex: ?type) type {
copy_backwards(T, dst, src);
array.slice()[index] = item;
}
pub fn in_range(array: *@This(), item: *T) bool {
if (array.committed == 0) return false;
if (@intFromPtr(item) < @intFromPtr(array.pointer)) return false;
const top = @intFromPtr(array.pointer) + array.committed * granularity;
if (@intFromPtr(item) >= top) return false;
return true;
}
};
}
@ -385,31 +413,19 @@ const pinned_hash_map_max_size = std.math.maxInt(u32) - pinned_hash_map_page_siz
const pinned_hash_map_default_granularity = pinned_hash_map_page_size;
pub fn PinnedHashMap(comptime K: type, comptime V: type) type {
return PinnedHashMapAdvanced(K, V, small_granularity);
}
pub fn PinnedHashMapAdvanced(comptime K: type, comptime V: type, comptime granularity: comptime_int) type {
return struct {
key_pointer: [*]K,
value_pointer: [*]V,
length: u32,
granularity: u32,
committed: u32,
key_pointer: [*]K = undefined,
value_pointer: [*]V = undefined,
length: u64 = 0,
committed_key: u32 = 0,
committed_value: u32 = 0,
const Map = @This();
pub fn init(granularity: u32) !Map {
assert(granularity & 0xfff == 0);
const key_raw_pointer = try reserve(pinned_hash_map_max_size);
try commit(key_raw_pointer, granularity);
const value_raw_pointer = try reserve(pinned_hash_map_max_size);
try commit(value_raw_pointer, granularity);
return Map{
.key_pointer = @alignCast(@ptrCast(key_raw_pointer)),
.value_pointer = @alignCast(@ptrCast(value_raw_pointer)),
.length = 0,
.granularity = granularity,
.committed = 1,
};
}
pub fn get_pointer(map: *Map, key: K) ?*V {
for (map.keys(), 0..) |k, i| {
const is_equal = switch (@typeInfo(K)) {
@ -437,7 +453,7 @@ pub fn PinnedHashMap(comptime K: type, comptime V: type) type {
}
}
pub fn put(map: *@This(), key: K, value: V) !void {
pub fn put(map: *@This(), key: K, value: V) void {
if (map.get_pointer(key)) |value_pointer| {
value_pointer.* = value;
} else {
@ -447,45 +463,57 @@ pub fn PinnedHashMap(comptime K: type, comptime V: type) type {
}
}
pub fn put_no_clobber(map: *@This(), key: K, value: V) !void {
pub fn put_no_clobber(map: *@This(), key: K, value: V) void {
assert(map.get_pointer(key) == null);
const len = map.length;
map.ensure_capacity(len + 1);
map.put_at_with_capacity(len, key, value);
}
fn put_at_with_capacity(map: *@This(), index: u32, key: K, value: V) void {
fn put_at_with_capacity(map: *@This(), index: u64, key: K, value: V) void {
map.length += 1;
assert(index < map.length);
map.key_pointer[index] = key;
map.value_pointer[index] = value;
}
fn ensure_capacity(map: *Map, additional: u32) void {
fn ensure_capacity(map: *Map, additional: u64) void {
if (map.committed_key == 0) {
map.key_pointer = @alignCast(@ptrCast(reserve(pinned_hash_map_max_size) catch unreachable));
map.value_pointer = @alignCast(@ptrCast(reserve(pinned_hash_map_max_size) catch unreachable));
}
const length = map.length;
assert((length + additional) * @sizeOf(K) <= pinned_array_max_size);
{
const key_size = length * @sizeOf(K);
const key_granularity_aligned_size = align_forward(key_size, map.granularity);
const key_granularity_aligned_size = align_forward(key_size, granularity);
const key_new_size = key_size + additional * @sizeOf(K);
if (key_granularity_aligned_size < key_new_size) {
const new_key_granularity_aligned_size = align_forward(key_new_size, map.granularity);
const new_key_granularity_aligned_size = align_forward(key_new_size, granularity);
const key_pointer: [*]u8 = @ptrCast(map.key_pointer);
commit(key_pointer + key_granularity_aligned_size, new_key_granularity_aligned_size - key_granularity_aligned_size) catch unreachable;
const commit_pointer = key_pointer + key_granularity_aligned_size;
const commit_size = new_key_granularity_aligned_size - key_granularity_aligned_size;
commit(commit_pointer, commit_size) catch unreachable;
map.committed_key += @intCast(@divExact(commit_size, granularity));
}
}
{
const value_size = length * @sizeOf(V);
const value_granularity_aligned_size = align_forward(value_size, map.granularity);
const value_granularity_aligned_size = align_forward(value_size, granularity);
const value_new_size = value_size + additional * @sizeOf(K);
if (value_granularity_aligned_size < value_new_size) {
const new_value_granularity_aligned_size = align_forward(value_new_size, map.granularity);
const new_value_granularity_aligned_size = align_forward(value_new_size, granularity);
const value_pointer: [*]u8 = @ptrCast(map.value_pointer);
commit(value_pointer + value_granularity_aligned_size, new_value_granularity_aligned_size - value_granularity_aligned_size) catch unreachable;
const commit_pointer = value_pointer + value_granularity_aligned_size;
const commit_size = new_value_granularity_aligned_size - value_granularity_aligned_size;
commit(commit_pointer, commit_size) catch unreachable;
map.committed_value += @intCast(@divExact(commit_size, granularity));
}
}
}
@ -737,3 +765,50 @@ pub fn exit_with_error() noreturn {
@breakpoint();
std.posix.exit(1);
}
pub fn read_file(arena: *Arena, directory: std.fs.Dir, file_relative_path: []const u8) []const u8 {
const source_file = directory.openFile(file_relative_path, .{}) catch |err| {
const stdout = std.io.getStdOut();
stdout.writeAll("Can't find file '") catch {};
stdout.writeAll(file_relative_path) catch {};
// stdout.writeAll(" in directory ") catch {};
// stdout.writeAll(file.package.directory.path) catch {};
stdout.writeAll("' for error ") catch {};
stdout.writeAll(@errorName(err)) catch {};
@panic("Unrecoverable error");
};
const file_size = source_file.getEndPos() catch unreachable;
var file_buffer = arena.new_array(u8, file_size) catch unreachable;
const read_byte_count = source_file.readAll(file_buffer) catch unreachable;
assert(read_byte_count == file_size);
source_file.close();
//TODO: adjust file maximum size
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));
}
pub fn realpath(arena: *Arena, dir: std.fs.Dir, relative_path: []const u8) ![]const u8 {
var buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
const stack_realpath = try dir.realpath(relative_path, &buffer);
const heap_realpath = try arena.new_array(u8, stack_realpath.len);
@memcpy(heap_realpath, stack_realpath);
return heap_realpath;
}
pub fn argument_copy_zero_terminated(arena: *Arena, args: []const []const u8) ![:null]?[*:0]u8 {
var result = try arena.new_array(?[*:0]u8, args.len + 1);
result[args.len] = null;
for (args, 0..) |argument, i| {
result[i] = try arena.duplicate_bytes_zero_terminated(argument);
}
return result[0..args.len :null];
}

View File

@ -1,5 +1,7 @@
const std = @import("std");
const assert = std.debug.assert;
const builtin = @import("builtin");
const linker = @import("linker.zig");
const library = @import("../library.zig");
@ -11,7 +13,7 @@ const write = Compilation.write;
pub fn link(context: *const Compilation.Context, options: linker.Options) !void {
assert(options.backend == .lld);
var argv = try PinnedArray([]const u8).init_with_default_granularity();
const driver_program = switch (@import("builtin").os.tag) {
const driver_program = switch (builtin.os.tag) {
.windows => "lld-link",
.linux => "ld.lld",
.macos => "ld64.lld",
@ -20,8 +22,8 @@ pub fn link(context: *const Compilation.Context, options: linker.Options) !void
_ = argv.append(driver_program);
_ = argv.append("--error-limit=0");
switch (@import("builtin").cpu.arch) {
.aarch64 => switch (@import("builtin").os.tag) {
switch (builtin.cpu.arch) {
.aarch64 => switch (builtin.os.tag) {
.linux => {
_ = argv.append("-znow");
_ = argv.append_slice(&.{ "-m", "aarch64linux" });
@ -42,12 +44,12 @@ pub fn link(context: *const Compilation.Context, options: linker.Options) !void
}
const ci = @import("configuration").ci;
switch (@import("builtin").os.tag) {
switch (builtin.os.tag) {
.macos => {
_ = argv.append("-dynamic");
argv.append_slice(&.{ "-platform_version", "macos", "13.4.1", "13.3" });
_ = argv.append("-arch");
_ = argv.append(switch (@import("builtin").cpu.arch) {
_ = argv.append(switch (builtin.cpu.arch) {
.aarch64 => "arm64",
else => |t| @panic(@tagName(t)),
});
@ -98,7 +100,7 @@ pub fn link(context: *const Compilation.Context, options: linker.Options) !void
argv.append_slice(&.{ "-L", "/usr/lib64" });
_ = argv.append("-dynamic-linker");
switch (@import("builtin").cpu.arch) {
switch (builtin.cpu.arch) {
.x86_64 => _ = argv.append("/lib64/ld-linux-x86-64.so.2"),
.aarch64 => _ = argv.append("/lib/ld-linux-aarch64.so.1"),
else => unreachable,
@ -130,7 +132,7 @@ pub fn link(context: *const Compilation.Context, options: linker.Options) !void
var stdout_len: usize = 0;
var stderr_ptr: [*]const u8 = undefined;
var stderr_len: usize = 0;
const result = switch (@import("builtin").os.tag) {
const result = switch (builtin.os.tag) {
.linux => NativityLLDLinkELF(argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len),
.macos => NativityLLDLinkMachO(argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len),
.windows => NativityLLDLinkCOFF(argv_zero_terminated.ptr, argv_zero_terminated.len, &stdout_ptr, &stdout_len, &stderr_ptr, &stderr_len),

View File

@ -1,14 +1,15 @@
const std = @import("std");
const assert = std.debug.assert;
const Compilation = @import("Compilation.zig");
pub const panic = Compilation.panic;
const builtin = @import("builtin");
const library = @import("library.zig");
const byte_equal = library.byte_equal;
const configuration = @import("configuration");
const editor = @import("editor.zig");
const compiler = @import("compiler.zig");
pub const panic = compiler.panic;
const env_detecting_libc_paths = "NATIVITY_IS_DETECTING_LIBC_PATHS";
@ -16,58 +17,11 @@ test {
_ = library;
}
fn todo() noreturn {
@setCold(true);
@panic("TODO");
}
pub fn main() !void {
if (configuration.editor) {
editor.main();
} else {
var arg_iterator = std.process.ArgIterator.init();
var buffer = library.BoundedArray([]const u8, 512){};
while (arg_iterator.next()) |argument| {
buffer.appendAssumeCapacity(argument);
}
const arguments = buffer.slice();
const context = try Compilation.createContext();
if (arguments.len <= 1) {
return error.InvalidInput;
}
if (std.process.can_execv and std.posix.getenvZ(env_detecting_libc_paths) != null) {
todo();
}
const command = arguments[1];
const command_arguments = arguments[2..];
if (byte_equal(command, "build")) {
try Compilation.compileBuildExecutable(context, command_arguments);
} else if (byte_equal(command, "clang") or byte_equal(command, "-cc1") or byte_equal(command, "-cc1as")) {
const exit_code = try Compilation.clangMain(context.arena, arguments);
std.process.exit(exit_code);
} else if (byte_equal(command, "cc")) {
try Compilation.compileCSourceFile(context, command_arguments, .c);
} else if (byte_equal(command, "c++")) {
try Compilation.compileCSourceFile(context, command_arguments, .cpp);
} else if (byte_equal(command, "exe")) {
try Compilation.buildExecutable(context, command_arguments, .{
.is_test = false,
});
} else if (byte_equal(command, "lib")) {
todo();
} else if (byte_equal(command, "obj")) {
todo();
} else if (byte_equal(command, "test")) {
try Compilation.buildExecutable(context, command_arguments, .{
.is_test = true,
});
} else {
todo();
}
compiler.main();
}
}

View File

@ -26,8 +26,10 @@ pub fn build(b: *std.Build) !void {
const print_stack_trace = b.option(bool, "print_stack_trace", "This option enables printing stack traces inside the compiler") orelse is_ci;
const native_target = b.resolveTargetQuery(.{});
const optimization = b.standardOptimizeOption(.{});
const use_editor = b.option(bool, "editor", "Use the GUI editor to play around the programming language") orelse !is_ci;
const enable_editor = false;
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 sanitize = b.option(bool, "sanitize", "This option enables sanitizers for the compiler") 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,
.windows => true,
@ -40,7 +42,7 @@ pub fn build(b: *std.Build) !void {
const fetcher = b.addExecutable(.{
.name = "llvm_fetcher",
.root_source_file = .{ .path = "build/fetcher.zig" },
.root_source_file = b.path("build/fetcher.zig"),
.target = native_target,
.optimize = .Debug,
.single_threaded = true,
@ -82,7 +84,7 @@ pub fn build(b: *std.Build) !void {
const compiler = b.addExecutable(.{
.name = "nat",
.root_source_file = .{ .path = "bootstrap/main.zig" },
.root_source_file = b.path("bootstrap/main.zig"),
.target = target,
.optimize = optimization,
});
@ -90,10 +92,10 @@ pub fn build(b: *std.Build) !void {
const cpp_files = .{
"src/llvm/llvm.cpp",
"src/llvm/lld.cpp",
"src/llvm/clang_main.cpp",
"src/llvm/clang_cc1.cpp",
"src/llvm/clang_cc1as.cpp",
"src/llvm/ar.cpp",
// "src/llvm/clang_main.cpp",
// "src/llvm/clang_cc1.cpp",
// "src/llvm/clang_cc1as.cpp",
// "src/llvm/ar.cpp",
};
compiler.addCSourceFiles(.{
@ -120,6 +122,9 @@ pub fn build(b: *std.Build) !void {
compiler.root_module.omit_frame_pointer = false;
compiler.root_module.error_tracing = print_stack_trace;
compiler.want_lto = false;
if (sanitize) {
compiler.root_module.sanitize_thread = true;
}
compiler.linkLibC();
@ -408,8 +413,8 @@ pub fn build(b: *std.Build) !void {
break :blk llvm_directory.items;
} else {
break :blk switch (use_debug) {
true => "../llvm-17-static-debug",
false => "../llvm-17-static-release",
true => "../zig-bootstrap/out/x86_64-linux-musl-native",
false => "../zig-bootstrap/out/x86_64-linux-musl-native",
};
}
};
@ -422,15 +427,15 @@ pub fn build(b: *std.Build) !void {
compiler.addObjectFile(std.Build.path(b, try std.mem.concat(b.allocator, u8, &.{ llvm_lib_dir, "/", llvm_library })));
}
} else {
compiler.linkSystemLibrary("LLVM-17");
compiler.linkSystemLibrary("clang-cpp");
compiler.linkSystemLibrary("LLVM");
// compiler.linkSystemLibrary("clang-cpp");
compiler.linkSystemLibrary("lldCommon");
compiler.linkSystemLibrary("lldCOFF");
compiler.linkSystemLibrary("lldELF");
compiler.linkSystemLibrary("lldMachO");
compiler.linkSystemLibrary("lldWasm");
compiler.linkSystemLibrary("unwind");
compiler.linkSystemLibrary(if (is_ci) "z" else "zlib");
// compiler.linkSystemLibrary("unwind");
compiler.linkSystemLibrary(if (is_ci or builtin.os.tag == .macos) "z" else "zlib");
compiler.linkSystemLibrary("zstd");
switch (target.result.os.tag) {
@ -441,9 +446,9 @@ pub fn build(b: *std.Build) !void {
compiler.addIncludePath(.{ .cwd_relative = "/usr/include/x86_64-linux-gnu" });
compiler.addIncludePath(.{ .cwd_relative = "/usr/include/c++/11" });
compiler.addIncludePath(.{ .cwd_relative = "/usr/include/x86_64-linux-gnu/c++/11" });
compiler.addIncludePath(.{ .cwd_relative = "/usr/lib/llvm-17/include" });
compiler.addIncludePath(.{ .cwd_relative = "/usr/lib/llvm-18/include" });
compiler.addLibraryPath(.{ .cwd_relative = "/lib/x86_64-linux-gnu" });
compiler.addLibraryPath(.{ .cwd_relative = "/usr/lib/llvm-17/lib" });
compiler.addLibraryPath(.{ .cwd_relative = "/usr/lib/llvm-18/lib" });
} else {
const result = try std.ChildProcess.run(.{
.allocator = b.allocator,
@ -475,18 +480,18 @@ 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 = "/usr/lib64/llvm17/include/" });
compiler.addIncludePath(.{ .cwd_relative = if (use_debug) "../../local/llvm18-debug/include" else "../../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 = "/usr/lib64/llvm17/lib" });
compiler.addLibraryPath(.{ .cwd_relative = if (use_debug) "../../local/llvm18-debug/lib" else "../../local/llvm18-release/lib" });
compiler.addLibraryPath(.{ .cwd_relative = "/usr/lib64" });
}
},
.macos => {
compiler.linkLibCpp();
if (discover_brew_prefix(b, "llvm@17")) |llvm_prefix| {
if (discover_brew_prefix(b, "llvm@18")) |llvm_prefix| {
const llvm_include_path = try std.mem.concat(b.allocator, u8, &.{ llvm_prefix, "/include" });
const llvm_lib_path = try std.mem.concat(b.allocator, u8, &.{ llvm_prefix, "/lib" });
compiler.addIncludePath(.{ .cwd_relative = llvm_include_path });
@ -557,6 +562,7 @@ pub fn build(b: *std.Build) !void {
const debug_command = switch (os) {
.linux => blk: {
const result = b.addSystemCommand(&.{"gf2"});
result.addArgs(&.{ "-ex", "set debuginfod enabled off" });
result.addArgs(&.{ "-ex", "set disassembly-flavor intel" });
result.addArg("-ex=r");
result.addArgs(&.{ "-ex", "up" });
@ -581,7 +587,7 @@ pub fn build(b: *std.Build) !void {
const test_runner = b.addExecutable(.{
.name = "test_runner",
.root_source_file = .{ .path = "build/test_runner.zig" },
.root_source_file = b.path("build/test_runner.zig"),
.target = native_target,
.optimize = optimization,
.single_threaded = true,
@ -609,3 +615,5 @@ pub fn build(b: *std.Build) !void {
const test_all = b.step("test_all", "Test all");
test_all.dependOn(&test_command.step);
}

View File

@ -4,12 +4,21 @@ set -ex
# Install LLVM and system dependencies
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 17 all
sudo apt install liblld-17-dev libclang-17-dev liblld-17 ninja-build cmake -y
MY_LLVM_VERSION=18
sudo ./llvm.sh $MY_LLVM_VERSION all
sudo apt install liblld-$MY_LLVM_VERSION-dev libclang-$MY_LLVM_VERSION-dev liblld-$MY_LLVM_VERSION ninja-build cmake -y
# Install Zig
source ci/download_zig.sh
download_zig master x86_64-linux
# Build and test
zig build test -Dthird_party_ci=true
i=$((0))
while ((i < 1000)); do
zig build run -Dthird_party_ci=true -Dprint_stack_trace=true -Doptimize=Debug -- exe -main_source_file retest/standalone/first/main.nat
zig build run -Dthird_party_ci=true -Dprint_stack_trace=true -Doptimize=ReleaseSafe -- exe -main_source_file retest/standalone/first/main.nat
zig build run -Dthird_party_ci=true -Dprint_stack_trace=true -Doptimize=ReleaseSmall -- exe -main_source_file retest/standalone/first/main.nat
zig build run -Dthird_party_ci=true -Dprint_stack_trace=true -Doptimize=ReleaseFast -- exe -main_source_file retest/standalone/first/main.nat
i=$((i + 1))
done

View File

@ -3,11 +3,18 @@ set -ex
# Install LLVM and system dependencies
brew update
brew install llvm@17 ninja
brew install llvm@18 ninja
# Install Zig
source ci/download_zig.sh
download_zig master aarch64-macos
# Build and test
zig build test -Dthird_party_ci=true
i=$((0))
while ((i < 1000)); do
zig build run -Dprint_stack_trace=true -Doptimize=Debug -- exe -main_source_file retest/standalone/first/main.nat
zig build run -Dprint_stack_trace=true -Doptimize=ReleaseSafe -- exe -main_source_file retest/standalone/first/main.nat
zig build run -Dprint_stack_trace=true -Doptimize=ReleaseSmall -- exe -main_source_file retest/standalone/first/main.nat
zig build run -Dprint_stack_trace=true -Doptimize=ReleaseFast -- exe -main_source_file retest/standalone/first/main.nat
i=$((i + 1))
done

4
newlib/std/std.nat Normal file
View File

@ -0,0 +1,4 @@
fn[extern] exit(exit_code: s32) noreturn;
fn my_exit(exit_code: s32) noreturn {
exit(exit_code);
}

View File

@ -0,0 +1,4 @@
fn foo() s32
{
return 0;
}

View File

@ -0,0 +1,6 @@
import "file.nat";
fn [cc(.c)] main [export]() s32
{
return file.foo();
}

12
script.sh Normal file
View File

@ -0,0 +1,12 @@
#!/bin/bash
set -ex
i=$((0))
while ((i < 1000)); do
zig build run -Dprint_stack_trace=true -Doptimize=Debug -- exe -main_source_file retest/standalone/first/main.nat
zig build run -Dprint_stack_trace=true -Doptimize=ReleaseSafe -- exe -main_source_file retest/standalone/first/main.nat
zig build run -Dprint_stack_trace=true -Doptimize=ReleaseSmall -- exe -main_source_file retest/standalone/first/main.nat
zig build run -Dprint_stack_trace=true -Doptimize=ReleaseFast -- exe -main_source_file retest/standalone/first/main.nat
i=$((i + 1))
done

View File

@ -1,11 +1,16 @@
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Verifier.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/ExecutionEngine/Orc/ThreadSafeModule.h"
#include "llvm/Linker/Linker.h"
#include "llvm/Passes/PassBuilder.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/TargetParser/Host.h"
@ -17,6 +22,8 @@
using namespace llvm;
using llvm::orc::ThreadSafeContext;
using llvm::orc::ThreadSafeModule;
extern "C" LLVMContext* NativityLLVMCreateContext()
{
@ -280,6 +287,13 @@ extern "C" StructType* NativityLLVMGetStructType(LLVMContext& context, Type** ty
auto* struct_type = StructType::get(context, types, is_packed);
return struct_type;
}
extern "C" LLVMContext* NativityLLVMTypeGetContext(Type& type)
{
auto& context = type.getContext();
return &context;
}
extern "C" Function* NativityLLVMModuleGetFunction(Module& module, const char* name_ptr, size_t name_len)
{
auto name = StringRef(name_ptr, name_len);
@ -341,6 +355,12 @@ extern "C" Type* NativityLLVMValueGetType(Value* value)
return type;
}
extern "C" LLVMContext* NativityLLVMValueGetContext(Value& value)
{
auto& context = value.getContext();
return &context;
}
extern "C" PoisonValue* NativityLLVMGetPoisonValue(Type* type)
{
auto* poison_value = PoisonValue::get(type);
@ -737,15 +757,14 @@ extern "C" bool NativityLLVMVerifyModule(const Module& module, const char** mess
return !result;
}
extern "C" const char* NativityLLVMFunctionToString(const Function& function, size_t* len)
extern "C" void NativityLLVMFunctionToString(const Function& function, const char** ptr, size_t* len)
{
std::string buf;
raw_string_ostream os(buf);
function.print(os);
os.flush();
*len = buf.size();
auto* result = strdup(buf.c_str());
return result;
*ptr = strdup(buf.c_str());
}
extern "C" Type* NativityLLVMAllocatGetAllocatedType(AllocaInst& alloca)
@ -829,26 +848,24 @@ extern "C" Type* NativityLLVMArrayTypeGetElementType(ArrayType* array_type)
return element_type;
}
extern "C" const char* NativityLLVMModuleToString(const Module& module, size_t* len)
extern "C" void NativityLLVMModuleToString(const Module& module, const char** ptr, size_t* len)
{
std::string buf;
raw_string_ostream os(buf);
module.print(os, nullptr);
os.flush();
*len = buf.size();
auto* result = strdup(buf.c_str());
return result;
*ptr = strdup(buf.c_str());
}
extern "C" const char* NativityLLVMValueToString(const Value& value, size_t* len)
extern "C" void NativityLLVMValueToString(const Value& value, const char** ptr, size_t* len)
{
std::string buf;
raw_string_ostream os(buf);
value.print(os, true);
os.flush();
*len = buf.size();
auto* result = strdup(buf.c_str());
return result;
*ptr = strdup(buf.c_str());
}
extern "C" BasicBlock* NativityLLVMBuilderGetInsertBlock(IRBuilder<>& builder)
@ -910,7 +927,7 @@ extern "C" const Target* NativityLLVMGetTarget(const char* target_triple_ptr, si
return target;
}
extern "C" TargetMachine* NativityLLVMTargetCreateTargetMachine(Target& target, const char* target_triple_ptr, size_t target_triple_len, const char* cpu_ptr, size_t cpu_len, const char* features_ptr, size_t features_len, Reloc::Model relocation_model, CodeModel::Model maybe_code_model, bool is_code_model_present, CodeGenOpt::Level optimization_level, bool jit)
extern "C" TargetMachine* NativityLLVMTargetCreateTargetMachine(Target& target, const char* target_triple_ptr, size_t target_triple_len, const char* cpu_ptr, size_t cpu_len, const char* features_ptr, size_t features_len, Reloc::Model relocation_model, CodeModel::Model maybe_code_model, bool is_code_model_present, CodeGenOptLevel optimization_level, bool jit)
{
auto target_triple = StringRef(target_triple_ptr, target_triple_len);
auto cpu = StringRef(cpu_ptr, cpu_len);
@ -1065,6 +1082,13 @@ extern "C" CallInst* NativityLLVMBuilderCreateMemcpy(IRBuilder<>& builder, Value
return memcpy;
}
extern "C" bool NativityLLVMLinkModules(Module& destination, Module* source)
{
auto src = std::unique_ptr<Module>(source);
bool result = Linker::linkModules(destination, std::move(src));
// Invert the condition because LLVM boolean concept is lame
return !result;
}
extern "C" void NativityLLVMTypeAssertEqual(Type* a, Type* b)
{
assert(a == b);