Start rewriting the parser for better analysis
This commit is contained in:
parent
313f54c4ce
commit
c3be23370a
@ -145,12 +145,13 @@ pub fn build(b: *std.Build) !void {
|
||||
const download_dir = try std.mem.concat(b.allocator, u8, &.{ home_path, "/Downloads" });
|
||||
std.fs.makeDirAbsolute(download_dir) catch {};
|
||||
const cmake_build_type = if (is_ci) CmakeBuildType.from_zig_build_type(optimize) else CmakeBuildType.Release;
|
||||
const llvm_base = try std.mem.concat(b.allocator, u8, &.{ "llvm-", @tagName(target.result.cpu.arch), "-", @tagName(target.result.os.tag), "-", @tagName(cmake_build_type) });
|
||||
const version_string = "20.1.2";
|
||||
const llvm_base = try std.mem.concat(b.allocator, u8, &.{ "llvm_", version_string, "_", @tagName(target.result.cpu.arch), "-", @tagName(target.result.os.tag), "-", @tagName(cmake_build_type) });
|
||||
const base = try std.mem.concat(b.allocator, u8, &.{ download_dir, "/", llvm_base });
|
||||
const full_path = try std.mem.concat(b.allocator, u8, &.{ base, "/bin/llvm-config" });
|
||||
|
||||
const f = std.fs.cwd().openFile(full_path, .{}) catch {
|
||||
const url = try std.mem.concat(b.allocator, u8, &.{ "https://github.com/birth-software/llvm/releases/download/v19.1.7/", llvm_base, ".7z" });
|
||||
const url = try std.mem.concat(b.allocator, u8, &.{ "https://github.com/birth-software/llvm/releases/download/v", version_string, "/", llvm_base, ".7z" });
|
||||
var result = try std.process.Child.run(.{
|
||||
.allocator = b.allocator,
|
||||
.argv = &.{ "wget", "-P", download_dir, url },
|
||||
|
@ -1285,7 +1285,11 @@ pub const DI = struct {
|
||||
return @ptrCast(compile_unit);
|
||||
}
|
||||
};
|
||||
pub const File = opaque {};
|
||||
pub const File = opaque {
|
||||
pub fn to_scope(file: *File) *Scope {
|
||||
return @ptrCast(file);
|
||||
}
|
||||
};
|
||||
pub const Scope = opaque {};
|
||||
pub const Subprogram = opaque {};
|
||||
pub const Expression = opaque {};
|
||||
|
3767
src/bootstrap.zig
Normal file
3767
src/bootstrap.zig
Normal file
File diff suppressed because it is too large
Load Diff
@ -65,7 +65,6 @@ const GlobalKind = enum {
|
||||
|
||||
const FunctionKeyword = enum {
|
||||
cc,
|
||||
foo,
|
||||
};
|
||||
|
||||
const CallingConvention = enum {
|
||||
@ -93,7 +92,6 @@ pub const ResolvedCallingConvention = enum {
|
||||
win64,
|
||||
};
|
||||
|
||||
|
||||
const AttributeContainerType = enum {
|
||||
call,
|
||||
function,
|
||||
|
98
src/lib.zig
98
src/lib.zig
@ -1,4 +1,4 @@
|
||||
const builtin = @import("builtin");
|
||||
pub const builtin = @import("builtin");
|
||||
pub const is_test = builtin.is_test;
|
||||
pub const optimization_mode = builtin.mode;
|
||||
pub const VariableArguments = extern struct {
|
||||
@ -519,6 +519,7 @@ pub const os = struct {
|
||||
write: u1,
|
||||
execute: u1,
|
||||
};
|
||||
|
||||
const MapFlags = packed struct {
|
||||
private: u1,
|
||||
anonymous: u1,
|
||||
@ -2913,6 +2914,101 @@ pub fn print_string_stderr(str: []const u8) void {
|
||||
os.get_stderr().write(str);
|
||||
}
|
||||
|
||||
pub const RawVirtualBuffer = struct {
|
||||
pointer: [*]align(4096) u8,
|
||||
length: u32,
|
||||
capacity: u32,
|
||||
|
||||
pub fn initialize() RawVirtualBuffer {
|
||||
const pointer = os.reserve(0, 4 * 1024 * 1024, .{
|
||||
.read = 1,
|
||||
.write = 1,
|
||||
.execute = 0,
|
||||
}, .{
|
||||
.private = 1,
|
||||
.anonymous = 1,
|
||||
.no_reserve = 1,
|
||||
.populate = 0,
|
||||
});
|
||||
return .{
|
||||
.pointer = @ptrCast(pointer),
|
||||
.length = 0,
|
||||
.capacity = 0,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn ensure_capacity(vb: *RawVirtualBuffer, capacity: u64) void {
|
||||
const commit_offset: u64 = vb.capacity;
|
||||
const unaligned_existing_capacity = commit_offset - vb.length;
|
||||
if (unaligned_existing_capacity < capacity) {
|
||||
const aligned_wanted_capacity = align_forward_u64(commit_offset + capacity, 0x1000);
|
||||
if (aligned_wanted_capacity > @as(u64, ~@as(u32, 0)) + 1) {
|
||||
@trap();
|
||||
}
|
||||
|
||||
const commit_size = aligned_wanted_capacity - commit_offset;
|
||||
const commit_pointer = vb.pointer + commit_offset;
|
||||
vb.capacity = @intCast(aligned_wanted_capacity);
|
||||
|
||||
os.commit(commit_pointer, commit_size, .{
|
||||
.read = 1,
|
||||
.write = 1,
|
||||
.execute = 0,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_bytes(vb: *RawVirtualBuffer, byte_count: u64) []u8 {
|
||||
vb.ensure_capacity(byte_count);
|
||||
const current_length = vb.length;
|
||||
vb.length += @intCast(byte_count);
|
||||
const slice = vb.pointer[current_length..][0..byte_count];
|
||||
return slice;
|
||||
}
|
||||
|
||||
pub fn append_bytes(vb: *RawVirtualBuffer, bytes: []const u8) []u8 {
|
||||
const slice = vb.add_bytes(bytes.len);
|
||||
@memcpy(slice, bytes);
|
||||
return slice;
|
||||
}
|
||||
};
|
||||
|
||||
pub fn VirtualBuffer(comptime T: type) type {
|
||||
return struct {
|
||||
backing: RawVirtualBuffer,
|
||||
|
||||
const VB = @This();
|
||||
|
||||
pub fn initialize() VB {
|
||||
return .{
|
||||
.backing = .initialize(),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn get_slice(vb: VB) []align(4096) T {
|
||||
return @as([*]align(4096) T, @ptrCast(vb.backing.pointer))[0..@divExact(vb.backing.length, @sizeOf(T))];
|
||||
}
|
||||
|
||||
fn byte_to_slice(v: *const T) []const u8 {
|
||||
const byte_pointer: [*]const u8 = @ptrCast(v);
|
||||
const byte_length = @sizeOf(T);
|
||||
return byte_pointer[0..byte_length];
|
||||
}
|
||||
|
||||
pub fn append(vb: *VB, v: T) *T {
|
||||
const result = vb.backing.append_bytes(byte_to_slice(&v));
|
||||
const pointer: *T = @alignCast(@ptrCast(&result[0]));
|
||||
return pointer;
|
||||
}
|
||||
|
||||
pub fn add(vb: *VB) *T {
|
||||
const result = vb.backing.add_bytes(@sizeOf(T));
|
||||
const pointer: *T = @alignCast(@ptrCast(&result[0]));
|
||||
return pointer;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub const panic_struct = struct {
|
||||
const abort = os.abort;
|
||||
pub fn call(_: []const u8, _: ?usize) noreturn {
|
||||
|
12
src/llvm.cpp
12
src/llvm.cpp
@ -1148,9 +1148,8 @@ enum class BBLLVMBasicBlockSection : u8
|
||||
{
|
||||
all = 0,
|
||||
list = 1,
|
||||
labels = 2,
|
||||
preset = 3,
|
||||
none = 4,
|
||||
preset = 2,
|
||||
none = 3,
|
||||
};
|
||||
|
||||
enum class BBLLVMFloatAbi : u8
|
||||
@ -1396,7 +1395,6 @@ EXPORT TargetMachine* llvm_create_target_machine(const BBLLVMTargetMachineCreate
|
||||
{
|
||||
case BBLLVMBasicBlockSection::all: target_options.BBSections = BasicBlockSection::All; break;
|
||||
case BBLLVMBasicBlockSection::list: target_options.BBSections = BasicBlockSection::List; break;
|
||||
case BBLLVMBasicBlockSection::labels: target_options.BBSections = BasicBlockSection::Labels; break;
|
||||
case BBLLVMBasicBlockSection::preset: target_options.BBSections = BasicBlockSection::Preset; break;
|
||||
case BBLLVMBasicBlockSection::none: target_options.BBSections = BasicBlockSection::None; break;
|
||||
}
|
||||
@ -1494,10 +1492,10 @@ EXPORT TargetMachine* llvm_create_target_machine(const BBLLVMTargetMachineCreate
|
||||
target_options.MCOptions.AsSecureLogFile = { create.target_options.mc.as_secure_log_file.pointer, create.target_options.mc.as_secure_log_file.length };
|
||||
}
|
||||
|
||||
target_options.MCOptions.Argv0 = create.target_options.mc.argv0;
|
||||
|
||||
if (create.target_options.mc.argv_count)
|
||||
{
|
||||
target_options.MCOptions.Argv0 = create.target_options.mc.argv0;
|
||||
|
||||
// TODO:
|
||||
__builtin_trap();
|
||||
}
|
||||
@ -1683,7 +1681,7 @@ EXPORT void llvm_module_run_optimization_pipeline(Module& module, TargetMachine&
|
||||
} else if (lto) {
|
||||
__builtin_trap(); // TODO
|
||||
} else {
|
||||
module_pass_manager.addPass(pass_builder.buildPerModuleDefaultPipeline(optimization_level, lto));
|
||||
module_pass_manager.addPass(pass_builder.buildPerModuleDefaultPipeline(optimization_level));
|
||||
}
|
||||
|
||||
// TODO: if emit bitcode/IR
|
||||
|
53
src/main.zig
53
src/main.zig
@ -4,24 +4,19 @@ const os = lib.os;
|
||||
const llvm = @import("LLVM.zig");
|
||||
const Arena = lib.Arena;
|
||||
|
||||
const converter = @import("converter.zig");
|
||||
const BuildMode = converter.BuildMode;
|
||||
|
||||
pub const panic = lib.panic_struct;
|
||||
pub const std_options = lib.std_options;
|
||||
const compiler = @import("bootstrap.zig");
|
||||
const BuildMode = compiler.BuildMode;
|
||||
|
||||
test {
|
||||
_ = lib;
|
||||
_ = llvm;
|
||||
_ = converter;
|
||||
_ = compiler;
|
||||
}
|
||||
|
||||
fn fail() noreturn {
|
||||
lib.libc.exit(1);
|
||||
}
|
||||
|
||||
pub const main = lib.main;
|
||||
|
||||
const Command = enum {
|
||||
@"test",
|
||||
compile,
|
||||
@ -34,7 +29,7 @@ const Compile = struct {
|
||||
silent: bool,
|
||||
};
|
||||
|
||||
fn compile_file(arena: *Arena, compile: Compile) converter.Options {
|
||||
fn compile_file(arena: *Arena, compile: Compile) compiler.Options {
|
||||
const checkpoint = arena.position;
|
||||
defer arena.restore(checkpoint);
|
||||
|
||||
@ -81,7 +76,7 @@ fn compile_file(arena: *Arena, compile: Compile) converter.Options {
|
||||
const file_path = os.absolute_path(arena, relative_file_path);
|
||||
const c_abi_object_path = arena.duplicate_string(configuration.c_abi_object_path);
|
||||
|
||||
const convert_options = converter.Options{
|
||||
const convert_options = compiler.Options{
|
||||
.executable = output_executable_path,
|
||||
.objects = if (lib.string.equal(base_name, "c_abi")) &.{ output_object_path, c_abi_object_path } else &.{output_object_path},
|
||||
.name = base_name,
|
||||
@ -89,17 +84,21 @@ fn compile_file(arena: *Arena, compile: Compile) converter.Options {
|
||||
.content = file_content,
|
||||
.path = file_path,
|
||||
.has_debug_info = compile.has_debug_info,
|
||||
.target = converter.Target.get_native(),
|
||||
.target = compiler.Target.get_native(),
|
||||
.silent = compile.silent,
|
||||
};
|
||||
|
||||
converter.convert(arena, convert_options);
|
||||
compiler.compile(arena, convert_options);
|
||||
|
||||
return convert_options;
|
||||
}
|
||||
|
||||
const base_cache_dir = "bb-cache";
|
||||
|
||||
pub const panic = lib.panic_struct;
|
||||
pub const std_options = lib.std_options;
|
||||
pub const main = lib.main;
|
||||
|
||||
pub fn entry_point(arguments: []const [*:0]const u8, environment: [*:null]const ?[*:0]const u8) void {
|
||||
lib.GlobalState.initialize();
|
||||
const arena = lib.global.arena;
|
||||
@ -130,21 +129,21 @@ pub fn entry_point(arguments: []const [*:0]const u8, environment: [*:null]const
|
||||
|
||||
const names = &[_][]const u8{
|
||||
"minimal",
|
||||
"comments",
|
||||
"constant_add",
|
||||
"constant_and",
|
||||
"constant_div",
|
||||
"constant_mul",
|
||||
"constant_rem",
|
||||
"constant_or",
|
||||
"constant_sub",
|
||||
"constant_xor",
|
||||
"constant_shift_left",
|
||||
"constant_shift_right",
|
||||
"minimal_stack",
|
||||
"minimal_stack_arithmetic",
|
||||
"pointer",
|
||||
"extend",
|
||||
// "comments",
|
||||
// "constant_add",
|
||||
// "constant_and",
|
||||
// "constant_div",
|
||||
// "constant_mul",
|
||||
// "constant_rem",
|
||||
// "constant_or",
|
||||
// "constant_sub",
|
||||
// "constant_xor",
|
||||
// "constant_shift_left",
|
||||
// "constant_shift_right",
|
||||
// "minimal_stack",
|
||||
// "minimal_stack_arithmetic",
|
||||
// "pointer",
|
||||
// "extend",
|
||||
};
|
||||
|
||||
var build_modes: [@typeInfo(BuildMode).@"enum".fields.len]BuildMode = undefined;
|
||||
|
Loading…
x
Reference in New Issue
Block a user