self hosted compiler: various small fixes
This commit is contained in:
parent
bca672372a
commit
86d9563d15
@ -11,12 +11,12 @@ const assert = std.debug.assert;
|
|||||||
const DW = std.dwarf;
|
const DW = std.dwarf;
|
||||||
const maxInt = std.math.maxInt;
|
const maxInt = std.math.maxInt;
|
||||||
|
|
||||||
pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code) !void {
|
pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code) Compilation.BuildError!void {
|
||||||
fn_val.base.ref();
|
fn_val.base.ref();
|
||||||
defer fn_val.base.deref(comp);
|
defer fn_val.base.deref(comp);
|
||||||
defer code.destroy(comp.gpa());
|
defer code.destroy(comp.gpa());
|
||||||
|
|
||||||
var output_path = try comp.createRandomOutputPath(comp.target.objFileExt());
|
var output_path = try comp.createRandomOutputPath(comp.target.oFileExt());
|
||||||
errdefer output_path.deinit();
|
errdefer output_path.deinit();
|
||||||
|
|
||||||
const llvm_handle = try comp.zig_compiler.getAnyLlvmContext();
|
const llvm_handle = try comp.zig_compiler.getAnyLlvmContext();
|
||||||
@ -30,11 +30,11 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
|
|||||||
llvm.SetTarget(module, comp.llvm_triple.ptr());
|
llvm.SetTarget(module, comp.llvm_triple.ptr());
|
||||||
llvm.SetDataLayout(module, comp.target_layout_str);
|
llvm.SetDataLayout(module, comp.target_layout_str);
|
||||||
|
|
||||||
if (comp.target.getObjectFormat() == .coff) {
|
// if (comp.target.getObjectFormat() == .coff) {
|
||||||
llvm.AddModuleCodeViewFlag(module);
|
// llvm.AddModuleCodeViewFlag(module);
|
||||||
} else {
|
// } else {
|
||||||
llvm.AddModuleDebugInfoFlag(module);
|
llvm.AddModuleDebugInfoFlag(module);
|
||||||
}
|
// }
|
||||||
|
|
||||||
const builder = llvm.CreateBuilderInContext(context) orelse return error.OutOfMemory;
|
const builder = llvm.CreateBuilderInContext(context) orelse return error.OutOfMemory;
|
||||||
defer llvm.DisposeBuilder(builder);
|
defer llvm.DisposeBuilder(builder);
|
||||||
@ -113,7 +113,7 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
|
|||||||
comp.target_machine,
|
comp.target_machine,
|
||||||
module,
|
module,
|
||||||
output_path.ptr(),
|
output_path.ptr(),
|
||||||
.EmitBinary,
|
llvm.EmitBinary,
|
||||||
&err_msg,
|
&err_msg,
|
||||||
is_debug,
|
is_debug,
|
||||||
is_small,
|
is_small,
|
||||||
|
@ -857,7 +857,7 @@ pub const Compilation = struct {
|
|||||||
defer locked_table.release();
|
defer locked_table.release();
|
||||||
|
|
||||||
var decl_group = event.Group(BuildError!void).init(self.gpa());
|
var decl_group = event.Group(BuildError!void).init(self.gpa());
|
||||||
defer decl_group.deinit();
|
defer decl_group.wait() catch {};
|
||||||
|
|
||||||
try self.rebuildChangedDecls(
|
try self.rebuildChangedDecls(
|
||||||
&decl_group,
|
&decl_group,
|
||||||
@ -937,7 +937,7 @@ pub const Compilation = struct {
|
|||||||
.parent_scope = &decl_scope.base,
|
.parent_scope = &decl_scope.base,
|
||||||
.tree_scope = tree_scope,
|
.tree_scope = tree_scope,
|
||||||
},
|
},
|
||||||
.value = Decl.Fn.Val{ .Unresolved = {} },
|
.value = .Unresolved,
|
||||||
.fn_proto = fn_proto,
|
.fn_proto = fn_proto,
|
||||||
};
|
};
|
||||||
tree_scope.base.ref();
|
tree_scope.base.ref();
|
||||||
@ -1100,7 +1100,7 @@ pub const Compilation = struct {
|
|||||||
async fn addCompileErrorAsync(
|
async fn addCompileErrorAsync(
|
||||||
self: *Compilation,
|
self: *Compilation,
|
||||||
msg: *Msg,
|
msg: *Msg,
|
||||||
) !void {
|
) BuildError!void {
|
||||||
errdefer msg.destroy();
|
errdefer msg.destroy();
|
||||||
|
|
||||||
const compile_errors = self.compile_errors.acquire();
|
const compile_errors = self.compile_errors.acquire();
|
||||||
@ -1109,7 +1109,7 @@ pub const Compilation = struct {
|
|||||||
try compile_errors.value.append(msg);
|
try compile_errors.value.append(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn verifyUniqueSymbol(self: *Compilation, decl: *Decl) !void {
|
async fn verifyUniqueSymbol(self: *Compilation, decl: *Decl) BuildError!void {
|
||||||
const exported_symbol_names = self.exported_symbol_names.acquire();
|
const exported_symbol_names = self.exported_symbol_names.acquire();
|
||||||
defer exported_symbol_names.release();
|
defer exported_symbol_names.release();
|
||||||
|
|
||||||
@ -1264,7 +1264,7 @@ pub const Compilation = struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// This declaration has been blessed as going into the final code generation.
|
/// This declaration has been blessed as going into the final code generation.
|
||||||
pub async fn resolveDecl(comp: *Compilation, decl: *Decl) !void {
|
pub async fn resolveDecl(comp: *Compilation, decl: *Decl) BuildError!void {
|
||||||
if (decl.resolution.start()) |ptr| return ptr.*;
|
if (decl.resolution.start()) |ptr| return ptr.*;
|
||||||
|
|
||||||
decl.resolution.data = try generateDecl(comp, decl);
|
decl.resolution.data = try generateDecl(comp, decl);
|
||||||
@ -1363,7 +1363,7 @@ async fn generateDeclFn(comp: *Compilation, fn_decl: *Decl.Fn) !void {
|
|||||||
try comp.prelink_group.call(addFnToLinkSet, comp, fn_val);
|
try comp.prelink_group.call(addFnToLinkSet, comp, fn_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn addFnToLinkSet(comp: *Compilation, fn_val: *Value.Fn) void {
|
async fn addFnToLinkSet(comp: *Compilation, fn_val: *Value.Fn) Compilation.BuildError!void {
|
||||||
fn_val.base.ref();
|
fn_val.base.ref();
|
||||||
defer fn_val.base.deref(comp);
|
defer fn_val.base.deref(comp);
|
||||||
|
|
||||||
@ -1421,7 +1421,7 @@ async fn analyzeFnType(
|
|||||||
.return_type = return_type,
|
.return_type = return_type,
|
||||||
.params = params.toOwnedSlice(),
|
.params = params.toOwnedSlice(),
|
||||||
.is_var_args = false, // TODO
|
.is_var_args = false, // TODO
|
||||||
.cc = Type.Fn.CallingConvention.Auto, // TODO
|
.cc = .Unspecified, // TODO
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
@ -69,12 +69,15 @@ pub const Decl = struct {
|
|||||||
|
|
||||||
pub const Fn = struct {
|
pub const Fn = struct {
|
||||||
base: Decl,
|
base: Decl,
|
||||||
value: union(enum) {
|
value: Val,
|
||||||
|
fn_proto: *ast.Node.FnProto,
|
||||||
|
|
||||||
|
// TODO https://github.com/ziglang/zig/issues/683 and then make this anonymous
|
||||||
|
pub const Val = union(enum) {
|
||||||
Unresolved,
|
Unresolved,
|
||||||
Fn: *Value.Fn,
|
Fn: *Value.Fn,
|
||||||
FnProto: *Value.FnProto,
|
FnProto: *Value.FnProto,
|
||||||
},
|
};
|
||||||
fn_proto: *ast.Node.FnProto,
|
|
||||||
|
|
||||||
pub fn externLibName(self: Fn, tree: *ast.Tree) ?[]const u8 {
|
pub fn externLibName(self: Fn, tree: *ast.Tree) ?[]const u8 {
|
||||||
return if (self.fn_proto.extern_export_inline_token) |tok_index| x: {
|
return if (self.fn_proto.extern_export_inline_token) |tok_index| x: {
|
||||||
|
@ -521,7 +521,7 @@ pub const Inst = struct {
|
|||||||
.Const => @panic("TODO"),
|
.Const => @panic("TODO"),
|
||||||
.Param => |param| {
|
.Param => |param| {
|
||||||
const new_inst = try ira.irb.build(
|
const new_inst = try ira.irb.build(
|
||||||
.VarPtr,
|
Inst.VarPtr,
|
||||||
self.base.scope,
|
self.base.scope,
|
||||||
self.base.span,
|
self.base.span,
|
||||||
Inst.VarPtr.Params{ .var_scope = self.params.var_scope },
|
Inst.VarPtr.Params{ .var_scope = self.params.var_scope },
|
||||||
@ -1134,7 +1134,7 @@ pub const Builder = struct {
|
|||||||
.VarType => return error.Unimplemented,
|
.VarType => return error.Unimplemented,
|
||||||
.ErrorType => return error.Unimplemented,
|
.ErrorType => return error.Unimplemented,
|
||||||
.FnProto => return error.Unimplemented,
|
.FnProto => return error.Unimplemented,
|
||||||
.PromiseType => return error.Unimplemented,
|
.AnyFrameType => return error.Unimplemented,
|
||||||
.IntegerLiteral => {
|
.IntegerLiteral => {
|
||||||
const int_lit = @fieldParentPtr(ast.Node.IntegerLiteral, "base", node);
|
const int_lit = @fieldParentPtr(ast.Node.IntegerLiteral, "base", node);
|
||||||
return irb.lvalWrap(scope, try irb.genIntLit(int_lit, scope), lval);
|
return irb.lvalWrap(scope, try irb.genIntLit(int_lit, scope), lval);
|
||||||
@ -1812,9 +1812,9 @@ pub const Builder = struct {
|
|||||||
for (@field(inst.params, @memberName(I.Params, i))) |other|
|
for (@field(inst.params, @memberName(I.Params, i))) |other|
|
||||||
other.ref(self);
|
other.ref(self);
|
||||||
},
|
},
|
||||||
.Mut,
|
Type.Pointer.Mut,
|
||||||
.Vol,
|
Type.Pointer.Vol,
|
||||||
.Size,
|
Type.Pointer.Size,
|
||||||
LVal,
|
LVal,
|
||||||
*Decl,
|
*Decl,
|
||||||
*Scope.Var,
|
*Scope.Var,
|
||||||
|
@ -6,6 +6,7 @@ const Target = std.Target;
|
|||||||
const ObjectFormat = Target.ObjectFormat;
|
const ObjectFormat = Target.ObjectFormat;
|
||||||
const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
|
const LibCInstallation = @import("libc_installation.zig").LibCInstallation;
|
||||||
const assert = std.debug.assert;
|
const assert = std.debug.assert;
|
||||||
|
const util = @import("util.zig");
|
||||||
|
|
||||||
const Context = struct {
|
const Context = struct {
|
||||||
comp: *Compilation,
|
comp: *Compilation,
|
||||||
@ -44,10 +45,10 @@ pub async fn link(comp: *Compilation) !void {
|
|||||||
try ctx.out_file_path.append(comp.target.exeFileExt());
|
try ctx.out_file_path.append(comp.target.exeFileExt());
|
||||||
},
|
},
|
||||||
.Lib => {
|
.Lib => {
|
||||||
try ctx.out_file_path.append(comp.target.libFileExt(comp.is_static));
|
try ctx.out_file_path.append(if (comp.is_static) comp.target.staticLibSuffix() else comp.target.dynamicLibSuffix());
|
||||||
},
|
},
|
||||||
.Obj => {
|
.Obj => {
|
||||||
try ctx.out_file_path.append(comp.target.objFileExt());
|
try ctx.out_file_path.append(comp.target.oFileExt());
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -77,7 +78,7 @@ pub async fn link(comp: *Compilation) !void {
|
|||||||
std.debug.warn("\n");
|
std.debug.warn("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
const extern_ofmt = toExternObjectFormatType(comp.target.getObjectFormat());
|
const extern_ofmt = toExternObjectFormatType(.elf); //comp.target.getObjectFormat());
|
||||||
const args_slice = ctx.args.toSlice();
|
const args_slice = ctx.args.toSlice();
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -129,13 +130,14 @@ fn toExternObjectFormatType(ofmt: ObjectFormat) c.ZigLLVM_ObjectFormatType {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn constructLinkerArgs(ctx: *Context) !void {
|
fn constructLinkerArgs(ctx: *Context) !void {
|
||||||
switch (ctx.comp.target.getObjectFormat()) {
|
return constructLinkerArgsElf(ctx);
|
||||||
.unknown => unreachable,
|
// switch (ctx.comp.target.getObjectFormat()) {
|
||||||
.coff => return constructLinkerArgsCoff(ctx),
|
// .unknown => unreachable,
|
||||||
.elf => return constructLinkerArgsElf(ctx),
|
// .coff => return constructLinkerArgsCoff(ctx),
|
||||||
.macho => return constructLinkerArgsMachO(ctx),
|
// .elf => return constructLinkerArgsElf(ctx),
|
||||||
.wasm => return constructLinkerArgsWasm(ctx),
|
// .macho => return constructLinkerArgsMachO(ctx),
|
||||||
}
|
// .wasm => return constructLinkerArgsWasm(ctx),
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn constructLinkerArgsElf(ctx: *Context) !void {
|
fn constructLinkerArgsElf(ctx: *Context) !void {
|
||||||
|
@ -350,7 +350,7 @@ pub const Type = struct {
|
|||||||
|
|
||||||
fn ccFnTypeStr(cc: CallingConvention) []const u8 {
|
fn ccFnTypeStr(cc: CallingConvention) []const u8 {
|
||||||
return switch (cc) {
|
return switch (cc) {
|
||||||
.Auto => "",
|
.Unspecified => "",
|
||||||
.C => "extern ",
|
.C => "extern ",
|
||||||
.Cold => "coldcc ",
|
.Cold => "coldcc ",
|
||||||
.Naked => "nakedcc ",
|
.Naked => "nakedcc ",
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const Target = std.Target;
|
const Target = std.Target;
|
||||||
const llvm = @import("llvm.zig");
|
const llvm = @import("llvm.zig");
|
||||||
// const builtin = @import("builtin");
|
|
||||||
|
|
||||||
pub const FloatAbi = enum {
|
pub const FloatAbi = enum {
|
||||||
Hard,
|
Hard,
|
||||||
@ -9,20 +8,6 @@ pub const FloatAbi = enum {
|
|||||||
SoftFp,
|
SoftFp,
|
||||||
};
|
};
|
||||||
|
|
||||||
// pub const Cross = struct {
|
|
||||||
// arch: Target.Arch,
|
|
||||||
// os: Target.Os,
|
|
||||||
// abi: Target.Abi,
|
|
||||||
// object_format: builtin.ObjectFormat,
|
|
||||||
// };
|
|
||||||
|
|
||||||
// pub fn getObjectFormat(self: Target) builtin.ObjectFormat {
|
|
||||||
// return switch (self) {
|
|
||||||
// .Native => builtin.object_format,
|
|
||||||
// .Cross => |t| t.object_format,
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|
||||||
/// TODO expose the arch and subarch separately
|
/// TODO expose the arch and subarch separately
|
||||||
pub fn isArmOrThumb(self: Target) bool {
|
pub fn isArmOrThumb(self: Target) bool {
|
||||||
return switch (self.getArch()) {
|
return switch (self.getArch()) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user