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

[WIP] MacOS stack traces #1406

Merged
merged 11 commits into from
Aug 25, 2018
9 changes: 9 additions & 0 deletions example/shared_library/mathtest.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
// TODO Remove this workaround
comptime {
const builtin = @import("builtin");
if (builtin.os == builtin.Os.macosx) {
@export("__mh_execute_header", _mh_execute_header, builtin.GlobalLinkage.Weak);
}
}
var _mh_execute_header = extern struct {x: usize}{.x = 0};

export fn add(a: i32, b: i32) i32 {
return a + b;
}
214 changes: 129 additions & 85 deletions src/analyze.cpp

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/analyze.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define ZIG_ANALYZE_HPP

#include "all_types.hpp"
#include "result.hpp"

void semantic_analyze(CodeGen *g);
ErrorMsg *add_node_error(CodeGen *g, AstNode *node, Buf *msg);
Expand Down Expand Up @@ -88,8 +89,8 @@ void init_fn_type_id(FnTypeId *fn_type_id, AstNode *proto_node, size_t param_cou
AstNode *get_param_decl_node(FnTableEntry *fn_entry, size_t index);
FnTableEntry *scope_get_fn_if_root(Scope *scope);
bool type_requires_comptime(TypeTableEntry *type_entry);
void ensure_complete_type(CodeGen *g, TypeTableEntry *type_entry);
void type_ensure_zero_bits_known(CodeGen *g, TypeTableEntry *type_entry);
Error ATTRIBUTE_MUST_USE ensure_complete_type(CodeGen *g, TypeTableEntry *type_entry);
Error ATTRIBUTE_MUST_USE type_ensure_zero_bits_known(CodeGen *g, TypeTableEntry *type_entry);
void complete_enum(CodeGen *g, TypeTableEntry *enum_type);
bool ir_get_var_is_comptime(VariableTableEntry *var);
bool const_values_equal(ConstExprValue *a, ConstExprValue *b);
Expand Down Expand Up @@ -178,7 +179,7 @@ TypeTableEntryId type_id_at_index(size_t index);
size_t type_id_len();
size_t type_id_index(TypeTableEntry *entry);
TypeTableEntry *get_generic_fn_type(CodeGen *g, FnTypeId *fn_type_id);
bool type_is_copyable(CodeGen *g, TypeTableEntry *type_entry);
Result<bool> type_is_copyable(CodeGen *g, TypeTableEntry *type_entry);
LinkLib *create_link_lib(Buf *name);
bool calling_convention_does_first_arg_return(CallingConvention cc);
LinkLib *add_link_lib(CodeGen *codegen, Buf *lib);
Expand Down
14 changes: 9 additions & 5 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5812,12 +5812,16 @@ static void do_code_gen(CodeGen *g) {

LLVMValueRef global_value;
if (var->linkage == VarLinkageExternal) {
global_value = LLVMAddGlobal(g->module, var->value->type->type_ref, buf_ptr(&var->name));

// TODO debug info for the extern variable
LLVMValueRef existing_llvm_var = LLVMGetNamedGlobal(g->module, buf_ptr(&var->name));
if (existing_llvm_var) {
global_value = LLVMConstBitCast(existing_llvm_var, LLVMPointerType(var->value->type->type_ref, 0));
} else {
global_value = LLVMAddGlobal(g->module, var->value->type->type_ref, buf_ptr(&var->name));
// TODO debug info for the extern variable

LLVMSetLinkage(global_value, LLVMExternalLinkage);
LLVMSetAlignment(global_value, var->align_bytes);
LLVMSetLinkage(global_value, LLVMExternalLinkage);
LLVMSetAlignment(global_value, var->align_bytes);
}
} else {
bool exported = (var->linkage == VarLinkageExport);
const char *mangled_name = buf_ptr(get_mangled_name(g, &var->name, exported));
Expand Down
361 changes: 177 additions & 184 deletions src/ir.cpp

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions src/result.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2018 Andrew Kelley
*
* This file is part of zig, which is MIT licensed.
* See http://opensource.org/licenses/MIT
*/

#ifndef ZIG_RESULT_HPP
#define ZIG_RESULT_HPP

#include "error.hpp"

#include <assert.h>

static inline void assertNoError(Error err) {
assert(err == ErrorNone);
}

template<typename T>
struct Result {
T data;
Error err;

Result(T x) : data(x), err(ErrorNone) {}

Result(Error err) : err(err) {
assert(err != ErrorNone);
}

T unwrap() {
assert(err == ErrorNone);
return data;
}
};

#endif
2 changes: 2 additions & 0 deletions src/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
#define ATTRIBUTE_PRINTF(a, b)
#define ATTRIBUTE_RETURNS_NOALIAS __declspec(restrict)
#define ATTRIBUTE_NORETURN __declspec(noreturn)
#define ATTRIBUTE_MUST_USE

#else

#define ATTRIBUTE_COLD __attribute__((cold))
#define ATTRIBUTE_PRINTF(a, b) __attribute__((format(printf, a, b)))
#define ATTRIBUTE_RETURNS_NOALIAS __attribute__((__malloc__))
#define ATTRIBUTE_NORETURN __attribute__((noreturn))
#define ATTRIBUTE_MUST_USE __attribute__((warn_unused_result))

#endif

Expand Down
12 changes: 12 additions & 0 deletions std/c/darwin.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
const macho = @import("../macho.zig");

extern "c" fn __error() *c_int;
pub extern "c" fn _NSGetExecutablePath(buf: [*]u8, bufsize: *u32) c_int;
pub extern "c" fn _dyld_get_image_header(image_index: u32) ?*mach_header;

pub extern "c" fn __getdirentries64(fd: c_int, buf_ptr: [*]u8, buf_len: usize, basep: *i64) usize;

Expand Down Expand Up @@ -33,6 +36,15 @@ pub extern "c" fn sysctlnametomib(name: [*]const u8, mibp: ?*c_int, sizep: ?*usi
pub extern "c" fn bind(socket: c_int, address: ?*const sockaddr, address_len: socklen_t) c_int;
pub extern "c" fn socket(domain: c_int, type: c_int, protocol: c_int) c_int;

/// The value of the link editor defined symbol _MH_EXECUTE_SYM is the address
/// of the mach header in a Mach-O executable file type. It does not appear in
/// any file type other than a MH_EXECUTE file type. The type of the symbol is
/// absolute as the header is not part of any section.
pub extern "c" var _mh_execute_header: if (@sizeOf(usize) == 8) mach_header_64 else mach_header;

pub const mach_header_64 = macho.mach_header_64;
pub const mach_header = macho.mach_header;

pub use @import("../os/darwin/errno.zig");

pub const _errno = __error;
Expand Down
3 changes: 3 additions & 0 deletions std/c/linux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ pub const pthread_attr_t = extern struct {
__size: [56]u8,
__align: c_long,
};

/// See std.elf for constants for this
pub extern fn getauxval(__type: c_ulong) c_ulong;
Loading