Skip to content

Commit

Permalink
zig build system: add setLinkerScript and setTarget
Browse files Browse the repository at this point in the history
 * See #204 - zig build system
 * allow builtin types Os, Environ, Arch to be used at runtime.
   they have zero_bits=false and debug info now.
 * Eradicate use of `@alloca` in std for syscalls. See #225
 * add `std.io.writeFile`
 * `std.debug.panic` adds a newline to format
  • Loading branch information
andrewrk committed Apr 3, 2017
1 parent a1363b5 commit c400cb4
Show file tree
Hide file tree
Showing 10 changed files with 374 additions and 148 deletions.
1 change: 1 addition & 0 deletions src/all_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,7 @@ struct TypeTableEntryEnum {
AstNode *decl_node;
ContainerLayout layout;
uint32_t src_field_count;
// number of fields in the union. 0 if enum with no payload
uint32_t gen_field_count;
TypeEnumField *fields;
bool is_invalid; // true if any fields are invalid
Expand Down
53 changes: 33 additions & 20 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3802,6 +3802,35 @@ static const GlobalLinkageValue global_linkage_values[] = {
{GlobalLinkageIdLinkOnce, "LinkOnce"},
};

static void init_enum_debug_info(CodeGen *g, TypeTableEntry *enum_type) {
uint32_t field_count = enum_type->data.enumeration.src_field_count;

TypeTableEntry *tag_type_entry = get_smallest_unsigned_int_type(g, field_count);
enum_type->data.enumeration.tag_type = tag_type_entry;

ZigLLVMDIEnumerator **di_enumerators = allocate<ZigLLVMDIEnumerator*>(field_count);
for (uint32_t i = 0; i < field_count; i += 1) {
TypeEnumField *field = &enum_type->data.enumeration.fields[i];
di_enumerators[i] = ZigLLVMCreateDebugEnumerator(g->dbuilder, buf_ptr(field->name), i);
}

// create debug type for tag
uint64_t tag_debug_size_in_bits = 8*LLVMStoreSizeOfType(g->target_data_ref, tag_type_entry->type_ref);
uint64_t tag_debug_align_in_bits = 8*LLVMABISizeOfType(g->target_data_ref, tag_type_entry->type_ref);
enum_type->di_type = ZigLLVMCreateDebugEnumerationType(g->dbuilder,
nullptr, buf_ptr(&enum_type->name),
nullptr, 0,
tag_debug_size_in_bits,
tag_debug_align_in_bits,
di_enumerators, field_count,
tag_type_entry->di_type, "");

enum_type->type_ref = tag_type_entry->type_ref;

enum_type->data.enumeration.complete = true;
enum_type->data.enumeration.zero_bits_known = true;
}

static void define_builtin_types(CodeGen *g) {
{
// if this type is anywhere in the AST, we should never hit codegen.
Expand Down Expand Up @@ -4022,7 +4051,6 @@ static void define_builtin_types(CodeGen *g) {

{
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
entry->zero_bits = true; // only allowed at compile time
buf_init_from_str(&entry->name, "Os");
uint32_t field_count = target_os_count();
entry->data.enumeration.src_field_count = field_count;
Expand All @@ -4038,19 +4066,15 @@ static void define_builtin_types(CodeGen *g) {
g->target_os_index = i;
}
}
entry->data.enumeration.complete = true;
entry->data.enumeration.zero_bits_known = true;

TypeTableEntry *tag_type_entry = get_smallest_unsigned_int_type(g, field_count);
entry->data.enumeration.tag_type = tag_type_entry;
init_enum_debug_info(g, entry);

g->builtin_types.entry_os_enum = entry;
g->primitive_type_table.put(&entry->name, entry);
}

{
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
entry->zero_bits = true; // only allowed at compile time
buf_init_from_str(&entry->name, "Arch");
uint32_t field_count = target_arch_count();
entry->data.enumeration.src_field_count = field_count;
Expand All @@ -4072,19 +4096,15 @@ static void define_builtin_types(CodeGen *g) {
g->target_arch_index = i;
}
}
entry->data.enumeration.complete = true;
entry->data.enumeration.zero_bits_known = true;

TypeTableEntry *tag_type_entry = get_smallest_unsigned_int_type(g, field_count);
entry->data.enumeration.tag_type = tag_type_entry;
init_enum_debug_info(g, entry);

g->builtin_types.entry_arch_enum = entry;
g->primitive_type_table.put(&entry->name, entry);
}

{
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
entry->zero_bits = true; // only allowed at compile time
buf_init_from_str(&entry->name, "Environ");
uint32_t field_count = target_environ_count();
entry->data.enumeration.src_field_count = field_count;
Expand All @@ -4100,19 +4120,15 @@ static void define_builtin_types(CodeGen *g) {
g->target_environ_index = i;
}
}
entry->data.enumeration.complete = true;
entry->data.enumeration.zero_bits_known = true;

TypeTableEntry *tag_type_entry = get_smallest_unsigned_int_type(g, field_count);
entry->data.enumeration.tag_type = tag_type_entry;
init_enum_debug_info(g, entry);

g->builtin_types.entry_environ_enum = entry;
g->primitive_type_table.put(&entry->name, entry);
}

{
TypeTableEntry *entry = new_type_table_entry(TypeTableEntryIdEnum);
entry->zero_bits = true; // only allowed at compile time
buf_init_from_str(&entry->name, "ObjectFormat");
uint32_t field_count = target_oformat_count();
entry->data.enumeration.src_field_count = field_count;
Expand All @@ -4128,11 +4144,8 @@ static void define_builtin_types(CodeGen *g) {
g->target_oformat_index = i;
}
}
entry->data.enumeration.complete = true;
entry->data.enumeration.zero_bits_known = true;

TypeTableEntry *tag_type_entry = get_smallest_unsigned_int_type(g, field_count);
entry->data.enumeration.tag_type = tag_type_entry;
init_enum_debug_info(g, entry);

g->builtin_types.entry_oformat_enum = entry;
g->primitive_type_table.put(&entry->name, entry);
Expand Down
202 changes: 202 additions & 0 deletions std/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub const Builder = struct {
*exe = Exe {
.root_src = root_src,
.name = name,
.target = Target.Native,
.linker_script = LinkerScript.None,
};
%return self.exe_list.append(exe);
return exe;
Expand All @@ -53,9 +55,43 @@ pub const Builder = struct {

%return zig_args.append("build_exe"[0...]); // TODO issue #296
%return zig_args.append(exe.root_src);

if (verbose) {
%return zig_args.append("--verbose"[0...]); // TODO issue #296
}

%return zig_args.append("--name"[0...]); // TODO issue #296
%return zig_args.append(exe.name);

switch (exe.target) {
Target.Native => {},
Target.Cross => |cross_target| {
%return zig_args.append("--target-arch"[0...]); // TODO issue #296
%return zig_args.append(targetArchName(cross_target.arch));

%return zig_args.append("--target-os"[0...]); // TODO issue #296
%return zig_args.append(targetOsName(cross_target.os));

%return zig_args.append("--target-environ"[0...]); // TODO issue #296
%return zig_args.append(targetEnvironName(cross_target.environ));
},
}

switch (exe.linker_script) {
LinkerScript.None => {},
LinkerScript.Embed => |script| {
const tmp_file_name = "linker.ld.tmp"; // TODO issue #298
io.writeFile(tmp_file_name, script, self.allocator)
%% |err| debug.panic("unable to write linker script: {}\n", @errorName(err));
%return zig_args.append("--linker-script"[0...]); // TODO issue #296
%return zig_args.append(tmp_file_name[0...]); // TODO issue #296
},
LinkerScript.Path => |path| {
%return zig_args.append("--linker-script"[0...]); // TODO issue #296
%return zig_args.append(path);
},
}

printInvocation(self.zig_exe, zig_args);
var child = %return os.ChildProcess.spawn(self.zig_exe, zig_args.toSliceConst(), os.environ,
StdIo.Ignore, StdIo.Inherit, StdIo.Inherit, self.allocator);
Expand All @@ -72,9 +108,48 @@ pub const Builder = struct {
}
};

const CrossTarget = struct {
arch: Arch,
os: Os,
environ: Environ,
};

const Target = enum {
Native,
Cross: CrossTarget,
};

const LinkerScript = enum {
None,
Embed: []const u8,
Path: []const u8,
};

const Exe = struct {
root_src: []const u8,
name: []const u8,
target: Target,
linker_script: LinkerScript,

fn setTarget(self: &Exe, target_arch: Arch, target_os: Os, target_environ: Environ) {
self.target = Target.Cross {
CrossTarget {
.arch = target_arch,
.os = target_os,
.environ = target_environ,
}
};
}

/// Exe keeps a reference to script for its lifetime or until this function
/// is called again.
fn setLinkerScriptContents(self: &Exe, script: []const u8) {
self.linker_script = LinkerScript.Embed { script };
}

fn setLinkerScriptPath(self: &Exe, path: []const u8) {
self.linker_script = LinkerScript.Path { path };
}
};

fn handleErr(err: error) -> noreturn {
Expand All @@ -88,3 +163,130 @@ fn printInvocation(exe_name: []const u8, args: &const List([]const u8)) {
}
%%io.stderr.printf("\n");
}

// TODO issue #299
fn targetOsName(target_os: Os) -> []const u8 {
return switch (target_os) {
Os.freestanding => ([]const u8)("freestanding"),
Os.cloudabi => ([]const u8)("cloudabi"),
Os.darwin => ([]const u8)("darwin"),
Os.dragonfly => ([]const u8)("dragonfly"),
Os.freebsd => ([]const u8)("freebsd"),
Os.ios => ([]const u8)("ios"),
Os.kfreebsd => ([]const u8)("kfreebsd"),
Os.linux => ([]const u8)("linux"),
Os.lv2 => ([]const u8)("lv2"),
Os.macosx => ([]const u8)("macosx"),
Os.netbsd => ([]const u8)("netbsd"),
Os.openbsd => ([]const u8)("openbsd"),
Os.solaris => ([]const u8)("solaris"),
Os.windows => ([]const u8)("windows"),
Os.haiku => ([]const u8)("haiku"),
Os.minix => ([]const u8)("minix"),
Os.rtems => ([]const u8)("rtems"),
Os.nacl => ([]const u8)("nacl"),
Os.cnk => ([]const u8)("cnk"),
Os.bitrig => ([]const u8)("bitrig"),
Os.aix => ([]const u8)("aix"),
Os.cuda => ([]const u8)("cuda"),
Os.nvcl => ([]const u8)("nvcl"),
Os.amdhsa => ([]const u8)("amdhsa"),
Os.ps4 => ([]const u8)("ps4"),
Os.elfiamcu => ([]const u8)("elfiamcu"),
Os.tvos => ([]const u8)("tvos"),
Os.watchos => ([]const u8)("watchos"),
Os.mesa3d => ([]const u8)("mesa3d"),
};
}

// TODO issue #299
fn targetArchName(target_arch: Arch) -> []const u8 {
return switch (target_arch) {
Arch.armv8_2a => ([]const u8)("armv8_2a"),
Arch.armv8_1a => ([]const u8)("armv8_1a"),
Arch.armv8 => ([]const u8)("armv8"),
Arch.armv8m_baseline => ([]const u8)("armv8m_baseline"),
Arch.armv8m_mainline => ([]const u8)("armv8m_mainline"),
Arch.armv7 => ([]const u8)("armv7"),
Arch.armv7em => ([]const u8)("armv7em"),
Arch.armv7m => ([]const u8)("armv7m"),
Arch.armv7s => ([]const u8)("armv7s"),
Arch.armv7k => ([]const u8)("armv7k"),
Arch.armv6 => ([]const u8)("armv6"),
Arch.armv6m => ([]const u8)("armv6m"),
Arch.armv6k => ([]const u8)("armv6k"),
Arch.armv6t2 => ([]const u8)("armv6t2"),
Arch.armv5 => ([]const u8)("armv5"),
Arch.armv5te => ([]const u8)("armv5te"),
Arch.armv4t => ([]const u8)("armv4t"),
Arch.armeb => ([]const u8)("armeb"),
Arch.aarch64 => ([]const u8)("aarch64"),
Arch.aarch64_be => ([]const u8)("aarch64_be"),
Arch.avr => ([]const u8)("avr"),
Arch.bpfel => ([]const u8)("bpfel"),
Arch.bpfeb => ([]const u8)("bpfeb"),
Arch.hexagon => ([]const u8)("hexagon"),
Arch.mips => ([]const u8)("mips"),
Arch.mipsel => ([]const u8)("mipsel"),
Arch.mips64 => ([]const u8)("mips64"),
Arch.mips64el => ([]const u8)("mips64el"),
Arch.msp430 => ([]const u8)("msp430"),
Arch.powerpc => ([]const u8)("powerpc"),
Arch.powerpc64 => ([]const u8)("powerpc64"),
Arch.powerpc64le => ([]const u8)("powerpc64le"),
Arch.r600 => ([]const u8)("r600"),
Arch.amdgcn => ([]const u8)("amdgcn"),
Arch.sparc => ([]const u8)("sparc"),
Arch.sparcv9 => ([]const u8)("sparcv9"),
Arch.sparcel => ([]const u8)("sparcel"),
Arch.s390x => ([]const u8)("s390x"),
Arch.tce => ([]const u8)("tce"),
Arch.thumb => ([]const u8)("thumb"),
Arch.thumbeb => ([]const u8)("thumbeb"),
Arch.i386 => ([]const u8)("i386"),
Arch.x86_64 => ([]const u8)("x86_64"),
Arch.xcore => ([]const u8)("xcore"),
Arch.nvptx => ([]const u8)("nvptx"),
Arch.nvptx64 => ([]const u8)("nvptx64"),
Arch.le32 => ([]const u8)("le32"),
Arch.le64 => ([]const u8)("le64"),
Arch.amdil => ([]const u8)("amdil"),
Arch.amdil64 => ([]const u8)("amdil64"),
Arch.hsail => ([]const u8)("hsail"),
Arch.hsail64 => ([]const u8)("hsail64"),
Arch.spir => ([]const u8)("spir"),
Arch.spir64 => ([]const u8)("spir64"),
Arch.kalimbav3 => ([]const u8)("kalimbav3"),
Arch.kalimbav4 => ([]const u8)("kalimbav4"),
Arch.kalimbav5 => ([]const u8)("kalimbav5"),
Arch.shave => ([]const u8)("shave"),
Arch.lanai => ([]const u8)("lanai"),
Arch.wasm32 => ([]const u8)("wasm32"),
Arch.wasm64 => ([]const u8)("wasm64"),
Arch.renderscript32 => ([]const u8)("renderscript32"),
Arch.renderscript64 => ([]const u8)("renderscript64"),
};
}

// TODO issue #299
fn targetEnvironName(target_environ: Environ) -> []const u8 {
return switch (target_environ) {
Environ.gnu => ([]const u8)("gnu"),
Environ.gnuabi64 => ([]const u8)("gnuabi64"),
Environ.gnueabi => ([]const u8)("gnueabi"),
Environ.gnueabihf => ([]const u8)("gnueabihf"),
Environ.gnux32 => ([]const u8)("gnux32"),
Environ.code16 => ([]const u8)("code16"),
Environ.eabi => ([]const u8)("eabi"),
Environ.eabihf => ([]const u8)("eabihf"),
Environ.android => ([]const u8)("android"),
Environ.musl => ([]const u8)("musl"),
Environ.musleabi => ([]const u8)("musleabi"),
Environ.musleabihf => ([]const u8)("musleabihf"),
Environ.msvc => ([]const u8)("msvc"),
Environ.itanium => ([]const u8)("itanium"),
Environ.cygnus => ([]const u8)("cygnus"),
Environ.amdopencl => ([]const u8)("amdopencl"),
Environ.coreclr => ([]const u8)("coreclr"),
};
}
6 changes: 3 additions & 3 deletions std/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub coldcc fn panic(comptime format: []const u8, args: ...) -> noreturn {
panicking = true;
}

%%io.stderr.printf(format, args);
%%io.stderr.printf(format ++ "\n", args);
%%printStackTrace();

os.abort();
Expand All @@ -52,8 +52,8 @@ pub fn writeStackTrace(out_stream: &io.OutStream) -> %void {
.compile_unit_list = List(CompileUnit).init(&global_allocator),
};
const st = &stack_trace;
%return io.openSelfExe(&st.self_exe_stream);
defer st.self_exe_stream.close() %% {};
st.self_exe_stream = %return io.openSelfExe();
defer st.self_exe_stream.close();

%return st.elf.openStream(&global_allocator, &st.self_exe_stream);
defer st.elf.close();
Expand Down
Loading

0 comments on commit c400cb4

Please sign in to comment.