Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates and cleanup in self hosted compiler #3748

Merged
merged 20 commits into from
Nov 24, 2019
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src-self-hosted/arg.zig
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ pub const Args = struct {

// MergeN creation disallows 0 length flag entry (doesn't make sense)
switch (flag_args) {
FlagArg.None => unreachable,
FlagArg.Single => |inner| try prev.append(inner),
FlagArg.Many => |inner| try prev.appendSlice(inner.toSliceConst()),
.None => unreachable,
.Single => |inner| try prev.append(inner),
.Many => |inner| try prev.appendSlice(inner.toSliceConst()),
}

_ = try parsed.flags.put(flag_name_trimmed, FlagArg{ .Many = prev });
Expand Down Expand Up @@ -158,7 +158,7 @@ pub const Args = struct {
pub fn single(self: *Args, name: []const u8) ?[]const u8 {
if (self.flags.get(name)) |entry| {
switch (entry.value) {
FlagArg.Single => |inner| {
.Single => |inner| {
return inner;
},
else => @panic("attempted to retrieve flag with wrong type"),
Expand All @@ -172,7 +172,7 @@ pub const Args = struct {
pub fn many(self: *Args, name: []const u8) []const []const u8 {
if (self.flags.get(name)) |entry| {
switch (entry.value) {
FlagArg.Many => |inner| {
.Many => |inner| {
return inner.toSliceConst();
},
else => @panic("attempted to retrieve flag with wrong type"),
Expand Down
118 changes: 110 additions & 8 deletions src-self-hosted/c_int.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const Target = @import("std").Target;

pub const CInt = struct {
id: Id,
zig_name: []const u8,
Expand All @@ -17,52 +19,152 @@ pub const CInt = struct {

pub const list = [_]CInt{
CInt{
.id = Id.Short,
.id = .Short,
.zig_name = "c_short",
.c_name = "short",
.is_signed = true,
},
CInt{
.id = Id.UShort,
.id = .UShort,
.zig_name = "c_ushort",
.c_name = "unsigned short",
.is_signed = false,
},
CInt{
.id = Id.Int,
.id = .Int,
.zig_name = "c_int",
.c_name = "int",
.is_signed = true,
},
CInt{
.id = Id.UInt,
.id = .UInt,
.zig_name = "c_uint",
.c_name = "unsigned int",
.is_signed = false,
},
CInt{
.id = Id.Long,
.id = .Long,
.zig_name = "c_long",
.c_name = "long",
.is_signed = true,
},
CInt{
.id = Id.ULong,
.id = .ULong,
.zig_name = "c_ulong",
.c_name = "unsigned long",
.is_signed = false,
},
CInt{
.id = Id.LongLong,
.id = .LongLong,
.zig_name = "c_longlong",
.c_name = "long long",
.is_signed = true,
},
CInt{
.id = Id.ULongLong,
.id = .ULongLong,
.zig_name = "c_ulonglong",
.c_name = "unsigned long long",
.is_signed = false,
},
};

pub fn sizeInBits(cint: CInt, self: Target) u32 {
const arch = self.getArch();
switch (self.getOs()) {
.freestanding => switch (self.getArch()) {
.msp430 => switch (cint.id) {
.Short,
.UShort,
.Int,
.UInt,
=> return 16,
.Long,
.ULong,
=> return 32,
.LongLong,
.ULongLong,
=> return 64,
},
else => switch (cint.id) {
.Short,
.UShort,
=> return 16,
.Int,
.UInt,
=> return 32,
.Long,
.ULong,
=> return self.getArchPtrBitWidth(),
.LongLong,
.ULongLong,
=> return 64,
},
},

.linux,
.macosx,
.freebsd,
.openbsd,
.zen,
=> switch (cint.id) {
.Short,
.UShort,
=> return 16,
.Int,
.UInt,
=> return 32,
.Long,
.ULong,
=> return self.getArchPtrBitWidth(),
.LongLong,
.ULongLong,
=> return 64,
},

.windows, .uefi => switch (cint.id) {
.Short,
.UShort,
=> return 16,
.Int,
.UInt,
=> return 32,
.Long,
.ULong,
.LongLong,
.ULongLong,
=> return 64,
},

.ananas,
.cloudabi,
.dragonfly,
.fuchsia,
.ios,
.kfreebsd,
.lv2,
.netbsd,
.solaris,
.haiku,
.minix,
.rtems,
.nacl,
.cnk,
.aix,
.cuda,
.nvcl,
.amdhsa,
.ps4,
.elfiamcu,
.tvos,
.watchos,
.mesa3d,
.contiki,
.amdpal,
.hermit,
.hurd,
.wasi,
.emscripten,
=> @panic("TODO specify the C integer type sizes for this OS"),
}
}
};
38 changes: 19 additions & 19 deletions src-self-hosted/codegen.zig
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
const std = @import("std");
const builtin = @import("builtin");
const Compilation = @import("compilation.zig").Compilation;
const llvm = @import("llvm.zig");
const c = @import("c.zig");
const ir = @import("ir.zig");
const Value = @import("value.zig").Value;
const Type = @import("type.zig").Type;
const Scope = @import("scope.zig").Scope;
const util = @import("util.zig");
const event = std.event;
const assert = std.debug.assert;
const DW = std.dwarf;
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();
defer fn_val.base.deref(comp);
defer code.destroy(comp.gpa());

var output_path = try await (async comp.createRandomOutputPath(comp.target.objFileExt()) catch unreachable);
var output_path = try comp.createRandomOutputPath(comp.target.oFileExt());
errdefer output_path.deinit();

const llvm_handle = try comp.zig_compiler.getAnyLlvmContext();
Expand All @@ -31,7 +31,7 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
llvm.SetTarget(module, comp.llvm_triple.ptr());
llvm.SetDataLayout(module, comp.target_layout_str);

if (comp.target.getObjectFormat() == builtin.ObjectFormat.coff) {
if (util.getObjectFormat(comp.target) == .coff) {
llvm.AddModuleCodeViewFlag(module);
} else {
llvm.AddModuleDebugInfoFlag(module);
Expand Down Expand Up @@ -59,7 +59,7 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
comp.name.ptr(),
comp.root_package.root_src_dir.ptr(),
) orelse return error.OutOfMemory;
const is_optimized = comp.build_mode != builtin.Mode.Debug;
const is_optimized = comp.build_mode != .Debug;
const compile_unit = llvm.CreateCompileUnit(
dibuilder,
DW.LANG_C99,
Expand All @@ -79,7 +79,7 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)
.builder = builder,
.dibuilder = dibuilder,
.context = context,
.lock = event.Lock.init(comp.loop),
.lock = event.Lock.init(),
.arena = &code.arena.allocator,
};

Expand All @@ -105,8 +105,8 @@ pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code)

assert(comp.emit_file_type == Compilation.Emit.Binary); // TODO support other types

const is_small = comp.build_mode == builtin.Mode.ReleaseSmall;
const is_debug = comp.build_mode == builtin.Mode.Debug;
const is_small = comp.build_mode == .ReleaseSmall;
const is_debug = comp.build_mode == .Debug;

var err_msg: [*]u8 = undefined;
// TODO integrate this with evented I/O
Expand Down Expand Up @@ -234,8 +234,8 @@ pub fn renderToLlvmModule(ofile: *ObjectFile, fn_val: *Value.Fn, code: *ir.Code)
// create debug variable declarations for variables and allocate all local variables
for (var_list) |var_scope, i| {
const var_type = switch (var_scope.data) {
Scope.Var.Data.Const => unreachable,
Scope.Var.Data.Param => |param| param.typ,
.Const => unreachable,
.Param => |param| param.typ,
};
// if (!type_has_bits(var->value->type)) {
// continue;
Expand Down Expand Up @@ -266,7 +266,7 @@ pub fn renderToLlvmModule(ofile: *ObjectFile, fn_val: *Value.Fn, code: *ir.Code)
var_scope.data.Param.llvm_value = llvm.GetParam(llvm_fn, @intCast(c_uint, i));
} else {
// gen_type = var->value->type;
var_scope.data.Param.llvm_value = try renderAlloca(ofile, var_type, var_scope.name, Type.Pointer.Align.Abi);
var_scope.data.Param.llvm_value = try renderAlloca(ofile, var_type, var_scope.name, .Abi);
}
// if (var->decl_node) {
// var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
Expand Down Expand Up @@ -300,8 +300,8 @@ pub fn renderToLlvmModule(ofile: *ObjectFile, fn_val: *Value.Fn, code: *ir.Code)
ofile,
llvm_param,
scope_var.data.Param.llvm_value,
Type.Pointer.Align.Abi,
Type.Pointer.Vol.Non,
.Abi,
.Non,
);
}

Expand Down Expand Up @@ -383,8 +383,8 @@ fn renderLoadUntyped(
) !*llvm.Value {
const result = llvm.BuildLoad(ofile.builder, ptr, name) orelse return error.OutOfMemory;
switch (vol) {
Type.Pointer.Vol.Non => {},
Type.Pointer.Vol.Volatile => llvm.SetVolatile(result, 1),
.Non => {},
.Volatile => llvm.SetVolatile(result, 1),
}
llvm.SetAlignment(result, resolveAlign(ofile, alignment, llvm.GetElementType(llvm.TypeOf(ptr))));
return result;
Expand Down Expand Up @@ -414,8 +414,8 @@ pub fn renderStoreUntyped(
) !*llvm.Value {
const result = llvm.BuildStore(ofile.builder, value, ptr) orelse return error.OutOfMemory;
switch (vol) {
Type.Pointer.Vol.Non => {},
Type.Pointer.Vol.Volatile => llvm.SetVolatile(result, 1),
.Non => {},
.Volatile => llvm.SetVolatile(result, 1),
}
llvm.SetAlignment(result, resolveAlign(ofile, alignment, llvm.TypeOf(value)));
return result;
Expand Down Expand Up @@ -445,7 +445,7 @@ pub fn renderAlloca(

pub fn resolveAlign(ofile: *ObjectFile, alignment: Type.Pointer.Align, llvm_type: *llvm.Type) u32 {
return switch (alignment) {
Type.Pointer.Align.Abi => return llvm.ABIAlignmentOfType(ofile.comp.target_data_ref, llvm_type),
Type.Pointer.Align.Override => |a| a,
.Abi => return llvm.ABIAlignmentOfType(ofile.comp.target_data_ref, llvm_type),
.Override => |a| a,
};
}
Loading