Merge remote-tracking branch 'origin/master' into llvm8
This commit is contained in:
212
src/codegen.cpp
212
src/codegen.cpp
@@ -48,16 +48,17 @@ static void init_darwin_native(CodeGen *g) {
|
||||
}
|
||||
}
|
||||
|
||||
static PackageTableEntry *new_package(const char *root_src_dir, const char *root_src_path) {
|
||||
PackageTableEntry *entry = allocate<PackageTableEntry>(1);
|
||||
static ZigPackage *new_package(const char *root_src_dir, const char *root_src_path, const char *pkg_path) {
|
||||
ZigPackage *entry = allocate<ZigPackage>(1);
|
||||
entry->package_table.init(4);
|
||||
buf_init_from_str(&entry->root_src_dir, root_src_dir);
|
||||
buf_init_from_str(&entry->root_src_path, root_src_path);
|
||||
buf_init_from_str(&entry->pkg_path, pkg_path);
|
||||
return entry;
|
||||
}
|
||||
|
||||
PackageTableEntry *new_anonymous_package(void) {
|
||||
return new_package("", "");
|
||||
ZigPackage *new_anonymous_package() {
|
||||
return new_package("", "", "");
|
||||
}
|
||||
|
||||
static const char *symbols_that_llvm_depends_on[] = {
|
||||
@@ -87,8 +88,8 @@ static const char *symbols_that_llvm_depends_on[] = {
|
||||
// TODO probably all of compiler-rt needs to go here
|
||||
};
|
||||
|
||||
CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out_type, BuildMode build_mode,
|
||||
Buf *zig_lib_dir, Buf *override_std_dir, ZigLibCInstallation *libc)
|
||||
CodeGen *codegen_create(Buf *main_pkg_path, Buf *root_src_path, const ZigTarget *target,
|
||||
OutType out_type, BuildMode build_mode, Buf *zig_lib_dir, Buf *override_std_dir, ZigLibCInstallation *libc)
|
||||
{
|
||||
CodeGen *g = allocate<CodeGen>(1);
|
||||
|
||||
@@ -132,20 +133,39 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
|
||||
}
|
||||
|
||||
if (root_src_path) {
|
||||
Buf *src_basename = buf_alloc();
|
||||
Buf *src_dir = buf_alloc();
|
||||
os_path_split(root_src_path, src_dir, src_basename);
|
||||
Buf *root_pkg_path;
|
||||
Buf *rel_root_src_path;
|
||||
if (main_pkg_path == nullptr) {
|
||||
Buf *src_basename = buf_alloc();
|
||||
Buf *src_dir = buf_alloc();
|
||||
os_path_split(root_src_path, src_dir, src_basename);
|
||||
|
||||
if (buf_len(src_basename) == 0) {
|
||||
fprintf(stderr, "Invalid root source path: %s\n", buf_ptr(root_src_path));
|
||||
exit(1);
|
||||
if (buf_len(src_basename) == 0) {
|
||||
fprintf(stderr, "Invalid root source path: %s\n", buf_ptr(root_src_path));
|
||||
exit(1);
|
||||
}
|
||||
root_pkg_path = src_dir;
|
||||
rel_root_src_path = src_basename;
|
||||
} else {
|
||||
Buf resolved_root_src_path = os_path_resolve(&root_src_path, 1);
|
||||
Buf resolved_main_pkg_path = os_path_resolve(&main_pkg_path, 1);
|
||||
|
||||
if (!buf_starts_with_buf(&resolved_root_src_path, &resolved_main_pkg_path)) {
|
||||
fprintf(stderr, "Root source path '%s' outside main package path '%s'",
|
||||
buf_ptr(root_src_path), buf_ptr(main_pkg_path));
|
||||
exit(1);
|
||||
}
|
||||
root_pkg_path = main_pkg_path;
|
||||
rel_root_src_path = buf_create_from_mem(
|
||||
buf_ptr(&resolved_root_src_path) + buf_len(&resolved_main_pkg_path) + 1,
|
||||
buf_len(&resolved_root_src_path) - buf_len(&resolved_main_pkg_path) - 1);
|
||||
}
|
||||
|
||||
g->root_package = new_package(buf_ptr(src_dir), buf_ptr(src_basename));
|
||||
g->std_package = new_package(buf_ptr(g->zig_std_dir), "index.zig");
|
||||
g->root_package = new_package(buf_ptr(root_pkg_path), buf_ptr(rel_root_src_path), "");
|
||||
g->std_package = new_package(buf_ptr(g->zig_std_dir), "std.zig", "std");
|
||||
g->root_package->package_table.put(buf_create_from_str("std"), g->std_package);
|
||||
} else {
|
||||
g->root_package = new_package(".", "");
|
||||
g->root_package = new_package(".", "", "");
|
||||
}
|
||||
|
||||
g->zig_std_special_dir = buf_alloc();
|
||||
@@ -621,7 +641,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
|
||||
if (scope->di_scope)
|
||||
return scope->di_scope;
|
||||
|
||||
ImportTableEntry *import = get_scope_import(scope);
|
||||
ZigType *import = get_scope_import(scope);
|
||||
switch (scope->id) {
|
||||
case ScopeIdCImport:
|
||||
zig_unreachable();
|
||||
@@ -644,7 +664,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
|
||||
assert(fn_di_scope != nullptr);
|
||||
ZigLLVMDISubprogram *subprogram = ZigLLVMCreateFunction(g->dbuilder,
|
||||
fn_di_scope, buf_ptr(&fn_table_entry->symbol_name), "",
|
||||
import->di_file, line_number,
|
||||
import->data.structure.root_struct->di_file, line_number,
|
||||
fn_table_entry->type_entry->data.fn.raw_di_type, is_internal_linkage,
|
||||
is_definition, scope_line, flags, is_optimized, nullptr);
|
||||
|
||||
@@ -658,7 +678,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
|
||||
assert(decls_scope->container_type);
|
||||
scope->di_scope = ZigLLVMTypeToScope(decls_scope->container_type->di_type);
|
||||
} else {
|
||||
scope->di_scope = ZigLLVMFileToScope(import->di_file);
|
||||
scope->di_scope = ZigLLVMFileToScope(import->data.structure.root_struct->di_file);
|
||||
}
|
||||
return scope->di_scope;
|
||||
case ScopeIdBlock:
|
||||
@@ -668,7 +688,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
|
||||
assert(scope->parent);
|
||||
ZigLLVMDILexicalBlock *di_block = ZigLLVMCreateLexicalBlock(g->dbuilder,
|
||||
get_di_scope(g, scope->parent),
|
||||
import->di_file,
|
||||
import->data.structure.root_struct->di_file,
|
||||
(unsigned)scope->source_node->line + 1,
|
||||
(unsigned)scope->source_node->column + 1);
|
||||
scope->di_scope = ZigLLVMLexicalBlockToScope(di_block);
|
||||
@@ -1236,7 +1256,7 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
|
||||
|
||||
LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;
|
||||
|
||||
// stack_trace.instruction_addresses[stack_trace.index % stack_trace.instruction_addresses.len] = return_address;
|
||||
// stack_trace.instruction_addresses[stack_trace.index & (stack_trace.instruction_addresses.len - 1)] = return_address;
|
||||
|
||||
LLVMValueRef err_ret_trace_ptr = LLVMGetParam(fn_val, 0);
|
||||
LLVMValueRef address_value = LLVMGetParam(fn_val, 1);
|
||||
@@ -1254,9 +1274,10 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
|
||||
|
||||
LLVMValueRef len_value = gen_load_untyped(g, len_field_ptr, 0, false, "");
|
||||
LLVMValueRef index_val = gen_load_untyped(g, index_field_ptr, 0, false, "");
|
||||
LLVMValueRef modded_val = LLVMBuildURem(g->builder, index_val, len_value, "");
|
||||
LLVMValueRef len_val_minus_one = LLVMBuildSub(g->builder, len_value, LLVMConstInt(usize_type_ref, 1, false), "");
|
||||
LLVMValueRef masked_val = LLVMBuildAnd(g->builder, index_val, len_val_minus_one, "");
|
||||
LLVMValueRef address_indices[] = {
|
||||
modded_val,
|
||||
masked_val,
|
||||
};
|
||||
|
||||
LLVMValueRef ptr_value = gen_load_untyped(g, ptr_field_ptr, 0, false, "");
|
||||
@@ -2196,7 +2217,7 @@ var_ok:
|
||||
if (dest_ty != nullptr && var->decl_node) {
|
||||
// arg index + 1 because the 0 index is return value
|
||||
var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
|
||||
buf_ptr(&var->name), fn_walk->data.vars.import->di_file,
|
||||
buf_ptr(&var->name), fn_walk->data.vars.import->data.structure.root_struct->di_file,
|
||||
(unsigned)(var->decl_node->line + 1),
|
||||
dest_ty->di_type, !g->strip_debug_symbols, 0, di_arg_index + 1);
|
||||
}
|
||||
@@ -3938,7 +3959,7 @@ static LLVMValueRef gen_non_null_bit(CodeGen *g, ZigType *maybe_type, LLVMValueR
|
||||
if (child_type->zero_bits) {
|
||||
return maybe_handle;
|
||||
} else {
|
||||
bool is_scalar = type_is_codegen_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
|
||||
bool is_scalar = type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
|
||||
if (is_scalar) {
|
||||
return LLVMBuildICmp(g->builder, LLVMIntNE, maybe_handle, LLVMConstNull(maybe_type->type_ref), "");
|
||||
} else {
|
||||
@@ -3978,7 +3999,7 @@ static LLVMValueRef ir_render_optional_unwrap_ptr(CodeGen *g, IrExecutable *exec
|
||||
if (child_type->zero_bits) {
|
||||
return nullptr;
|
||||
} else {
|
||||
bool is_scalar = type_is_codegen_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
|
||||
bool is_scalar = type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet;
|
||||
if (is_scalar) {
|
||||
return maybe_ptr;
|
||||
} else {
|
||||
@@ -4549,7 +4570,7 @@ static LLVMValueRef ir_render_slice(CodeGen *g, IrExecutable *executable, IrInst
|
||||
|
||||
return tmp_struct_ptr;
|
||||
} else if (array_type->id == ZigTypeIdPointer) {
|
||||
assert(array_type->data.pointer.ptr_len == PtrLenUnknown);
|
||||
assert(array_type->data.pointer.ptr_len != PtrLenSingle);
|
||||
LLVMValueRef start_val = ir_llvm_value(g, instruction->start);
|
||||
LLVMValueRef end_val = ir_llvm_value(g, instruction->end);
|
||||
|
||||
@@ -4640,7 +4661,8 @@ static LLVMValueRef ir_render_return_address(CodeGen *g, IrExecutable *executabl
|
||||
IrInstructionReturnAddress *instruction)
|
||||
{
|
||||
LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);
|
||||
return LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");
|
||||
LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");
|
||||
return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->type_ref, "");
|
||||
}
|
||||
|
||||
static LLVMValueRef get_frame_address_fn_val(CodeGen *g) {
|
||||
@@ -4661,7 +4683,8 @@ static LLVMValueRef ir_render_frame_address(CodeGen *g, IrExecutable *executable
|
||||
IrInstructionFrameAddress *instruction)
|
||||
{
|
||||
LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);
|
||||
return LLVMBuildCall(g->builder, get_frame_address_fn_val(g), &zero, 1, "");
|
||||
LLVMValueRef ptr_val = LLVMBuildCall(g->builder, get_frame_address_fn_val(g), &zero, 1, "");
|
||||
return LLVMBuildPtrToInt(g->builder, ptr_val, g->builtin_types.entry_usize->type_ref, "");
|
||||
}
|
||||
|
||||
static LLVMValueRef get_handle_fn_val(CodeGen *g) {
|
||||
@@ -4839,7 +4862,7 @@ static LLVMValueRef ir_render_maybe_wrap(CodeGen *g, IrExecutable *executable, I
|
||||
}
|
||||
|
||||
LLVMValueRef payload_val = ir_llvm_value(g, instruction->value);
|
||||
if (type_is_codegen_pointer(child_type) || child_type->id == ZigTypeIdErrorSet) {
|
||||
if (type_is_non_optional_pointer(child_type) || child_type->id == ZigTypeIdErrorSet) {
|
||||
return payload_val;
|
||||
}
|
||||
|
||||
@@ -5800,7 +5823,6 @@ static LLVMValueRef pack_const_int(CodeGen *g, LLVMTypeRef big_int_type_ref, Con
|
||||
case ZigTypeIdNull:
|
||||
case ZigTypeIdErrorUnion:
|
||||
case ZigTypeIdErrorSet:
|
||||
case ZigTypeIdNamespace:
|
||||
case ZigTypeIdBoundFn:
|
||||
case ZigTypeIdArgTuple:
|
||||
case ZigTypeIdVoid:
|
||||
@@ -6077,9 +6099,9 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
|
||||
case ZigTypeIdOptional:
|
||||
{
|
||||
ZigType *child_type = type_entry->data.maybe.child_type;
|
||||
if (child_type->zero_bits) {
|
||||
if (!type_has_bits(child_type)) {
|
||||
return LLVMConstInt(LLVMInt1Type(), const_val->data.x_optional ? 1 : 0, false);
|
||||
} else if (type_is_codegen_pointer(child_type)) {
|
||||
} else if (get_codegen_ptr_type(type_entry) != nullptr) {
|
||||
return gen_const_val_ptr(g, const_val, name);
|
||||
} else if (child_type->id == ZigTypeIdErrorSet) {
|
||||
return gen_const_val_err_set(g, const_val, name);
|
||||
@@ -6400,7 +6422,6 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val, const c
|
||||
case ZigTypeIdComptimeInt:
|
||||
case ZigTypeIdUndefined:
|
||||
case ZigTypeIdNull:
|
||||
case ZigTypeIdNamespace:
|
||||
case ZigTypeIdBoundFn:
|
||||
case ZigTypeIdArgTuple:
|
||||
case ZigTypeIdOpaque:
|
||||
@@ -6506,12 +6527,12 @@ static void gen_global_var(CodeGen *g, ZigVar *var, LLVMValueRef init_val,
|
||||
assert(var->gen_is_const);
|
||||
assert(type_entry);
|
||||
|
||||
ImportTableEntry *import = get_scope_import(var->parent_scope);
|
||||
ZigType *import = get_scope_import(var->parent_scope);
|
||||
assert(import);
|
||||
|
||||
bool is_local_to_unit = true;
|
||||
ZigLLVMCreateGlobalVariable(g->dbuilder, get_di_scope(g, var->parent_scope), buf_ptr(&var->name),
|
||||
buf_ptr(&var->name), import->di_file,
|
||||
buf_ptr(&var->name), import->data.structure.root_struct->di_file,
|
||||
(unsigned)(var->decl_node->line + 1),
|
||||
type_entry->di_type, is_local_to_unit);
|
||||
|
||||
@@ -6767,7 +6788,7 @@ static void do_code_gen(CodeGen *g) {
|
||||
*slot = build_alloca(g, slot_type, "", alignment_bytes);
|
||||
}
|
||||
|
||||
ImportTableEntry *import = get_scope_import(&fn_table_entry->fndef_scope->base);
|
||||
ZigType *import = get_scope_import(&fn_table_entry->fndef_scope->base);
|
||||
|
||||
unsigned gen_i_init = want_first_arg_sret(g, fn_type_id) ? 1 : 0;
|
||||
|
||||
@@ -6799,7 +6820,7 @@ static void do_code_gen(CodeGen *g) {
|
||||
var->value_ref = build_alloca(g, var->var_type, buf_ptr(&var->name), var->align_bytes);
|
||||
|
||||
var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
|
||||
buf_ptr(&var->name), import->di_file, (unsigned)(var->decl_node->line + 1),
|
||||
buf_ptr(&var->name), import->data.structure.root_struct->di_file, (unsigned)(var->decl_node->line + 1),
|
||||
var->var_type->di_type, !g->strip_debug_symbols, 0);
|
||||
|
||||
} else if (is_c_abi) {
|
||||
@@ -6823,7 +6844,7 @@ static void do_code_gen(CodeGen *g) {
|
||||
}
|
||||
if (var->decl_node) {
|
||||
var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope),
|
||||
buf_ptr(&var->name), import->di_file,
|
||||
buf_ptr(&var->name), import->data.structure.root_struct->di_file,
|
||||
(unsigned)(var->decl_node->line + 1),
|
||||
gen_type->di_type, !g->strip_debug_symbols, 0, (unsigned)(var->gen_arg_index + 1));
|
||||
}
|
||||
@@ -6971,12 +6992,6 @@ static void define_builtin_types(CodeGen *g) {
|
||||
entry->zero_bits = true;
|
||||
g->builtin_types.entry_invalid = entry;
|
||||
}
|
||||
{
|
||||
ZigType *entry = new_type_table_entry(ZigTypeIdNamespace);
|
||||
buf_init_from_str(&entry->name, "(namespace)");
|
||||
entry->zero_bits = true;
|
||||
g->builtin_types.entry_namespace = entry;
|
||||
}
|
||||
{
|
||||
ZigType *entry = new_type_table_entry(ZigTypeIdComptimeFloat);
|
||||
buf_init_from_str(&entry->name, "comptime_float");
|
||||
@@ -7130,7 +7145,8 @@ static void define_builtin_types(CodeGen *g) {
|
||||
g->builtin_types.entry_i64 = get_int_type(g, true, 64);
|
||||
|
||||
{
|
||||
g->builtin_types.entry_c_void = get_opaque_type(g, nullptr, nullptr, "c_void");
|
||||
g->builtin_types.entry_c_void = get_opaque_type(g, nullptr, nullptr, "c_void",
|
||||
buf_create_from_str("c_void"));
|
||||
g->primitive_type_table.put(&g->builtin_types.entry_c_void->name, g->builtin_types.entry_c_void);
|
||||
}
|
||||
|
||||
@@ -7470,7 +7486,6 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
|
||||
" Enum: Enum,\n"
|
||||
" Union: Union,\n"
|
||||
" Fn: Fn,\n"
|
||||
" Namespace: void,\n"
|
||||
" BoundFn: Fn,\n"
|
||||
" ArgTuple: void,\n"
|
||||
" Opaque: void,\n"
|
||||
@@ -7479,18 +7494,18 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
|
||||
"\n\n"
|
||||
" pub const Int = struct {\n"
|
||||
" is_signed: bool,\n"
|
||||
" bits: u8,\n"
|
||||
" bits: comptime_int,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Float = struct {\n"
|
||||
" bits: u8,\n"
|
||||
" bits: comptime_int,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Pointer = struct {\n"
|
||||
" size: Size,\n"
|
||||
" is_const: bool,\n"
|
||||
" is_volatile: bool,\n"
|
||||
" alignment: u32,\n"
|
||||
" alignment: comptime_int,\n"
|
||||
" child: type,\n"
|
||||
"\n"
|
||||
" pub const Size = enum {\n"
|
||||
@@ -7502,7 +7517,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Array = struct {\n"
|
||||
" len: usize,\n"
|
||||
" len: comptime_int,\n"
|
||||
" child: type,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
@@ -7514,7 +7529,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
|
||||
"\n"
|
||||
" pub const StructField = struct {\n"
|
||||
" name: []const u8,\n"
|
||||
" offset: ?usize,\n"
|
||||
" offset: ?comptime_int,\n"
|
||||
" field_type: type,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
@@ -7535,7 +7550,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
|
||||
"\n"
|
||||
" pub const Error = struct {\n"
|
||||
" name: []const u8,\n"
|
||||
" value: usize,\n"
|
||||
" value: comptime_int,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const ErrorSet = struct {\n"
|
||||
@@ -7544,7 +7559,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
|
||||
"\n"
|
||||
" pub const EnumField = struct {\n"
|
||||
" name: []const u8,\n"
|
||||
" value: usize,\n"
|
||||
" value: comptime_int,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Enum = struct {\n"
|
||||
@@ -7596,7 +7611,7 @@ Buf *codegen_generate_builtin_source(CodeGen *g) {
|
||||
" };\n"
|
||||
"\n"
|
||||
" pub const Vector = struct {\n"
|
||||
" len: u32,\n"
|
||||
" len: comptime_int,\n"
|
||||
" child: type,\n"
|
||||
" };\n"
|
||||
"\n"
|
||||
@@ -7751,12 +7766,12 @@ static Error define_builtin_compile_vars(CodeGen *g) {
|
||||
|
||||
assert(g->root_package);
|
||||
assert(g->std_package);
|
||||
g->compile_var_package = new_package(buf_ptr(this_dir), builtin_zig_basename);
|
||||
g->compile_var_package = new_package(buf_ptr(this_dir), builtin_zig_basename, "builtin");
|
||||
g->root_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
|
||||
g->std_package->package_table.put(buf_create_from_str("builtin"), g->compile_var_package);
|
||||
g->std_package->package_table.put(buf_create_from_str("std"), g->std_package);
|
||||
g->compile_var_import = add_source_file(g, g->compile_var_package, builtin_zig_path, contents);
|
||||
scan_import(g, g->compile_var_import);
|
||||
g->compile_var_import = add_source_file(g, g->compile_var_package, builtin_zig_path, contents,
|
||||
SourceKindPkgMain);
|
||||
|
||||
return ErrorNone;
|
||||
}
|
||||
@@ -7948,17 +7963,20 @@ void codegen_translate_c(CodeGen *g, Buf *full_path) {
|
||||
Buf *src_dirname = buf_alloc();
|
||||
os_path_split(full_path, src_dirname, src_basename);
|
||||
|
||||
ImportTableEntry *import = allocate<ImportTableEntry>(1);
|
||||
import->source_code = nullptr;
|
||||
import->path = full_path;
|
||||
g->root_import = import;
|
||||
import->decls_scope = create_decls_scope(g, nullptr, nullptr, nullptr, import);
|
||||
Buf noextname = BUF_INIT;
|
||||
os_path_extname(src_basename, &noextname, nullptr);
|
||||
|
||||
detect_libc(g);
|
||||
|
||||
init(g);
|
||||
|
||||
import->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
|
||||
RootStruct *root_struct = allocate<RootStruct>(1);
|
||||
root_struct->source_code = nullptr;
|
||||
root_struct->path = full_path;
|
||||
root_struct->di_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(src_basename), buf_ptr(src_dirname));
|
||||
|
||||
ZigType *import = get_root_container_type(g, buf_ptr(&noextname), &noextname, root_struct);
|
||||
g->root_import = import;
|
||||
|
||||
ZigList<ErrorMsg *> errors = {0};
|
||||
Error err = parse_h_file(import, &errors, buf_ptr(full_path), g, nullptr);
|
||||
@@ -7977,7 +7995,7 @@ void codegen_translate_c(CodeGen *g, Buf *full_path) {
|
||||
}
|
||||
}
|
||||
|
||||
static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package, const char *basename) {
|
||||
static ZigType *add_special_code(CodeGen *g, ZigPackage *package, const char *basename) {
|
||||
Buf *code_basename = buf_create_from_str(basename);
|
||||
Buf path_to_code_src = BUF_INIT;
|
||||
os_path_join(g->zig_std_special_dir, code_basename, &path_to_code_src);
|
||||
@@ -7991,21 +8009,21 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package
|
||||
zig_panic("unable to open '%s': %s\n", buf_ptr(&path_to_code_src), err_str(err));
|
||||
}
|
||||
|
||||
return add_source_file(g, package, resolved_path, import_code);
|
||||
return add_source_file(g, package, resolved_path, import_code, SourceKindPkgMain);
|
||||
}
|
||||
|
||||
static PackageTableEntry *create_bootstrap_pkg(CodeGen *g, PackageTableEntry *pkg_with_main) {
|
||||
PackageTableEntry *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig");
|
||||
static ZigPackage *create_bootstrap_pkg(CodeGen *g, ZigPackage *pkg_with_main) {
|
||||
ZigPackage *package = codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "bootstrap.zig", "std.special");
|
||||
package->package_table.put(buf_create_from_str("@root"), pkg_with_main);
|
||||
return package;
|
||||
}
|
||||
|
||||
static PackageTableEntry *create_test_runner_pkg(CodeGen *g) {
|
||||
return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig");
|
||||
static ZigPackage *create_test_runner_pkg(CodeGen *g) {
|
||||
return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "test_runner.zig", "std.special");
|
||||
}
|
||||
|
||||
static PackageTableEntry *create_panic_pkg(CodeGen *g) {
|
||||
return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "panic.zig");
|
||||
static ZigPackage *create_panic_pkg(CodeGen *g) {
|
||||
return codegen_create_package(g, buf_ptr(g->zig_std_special_dir), "panic.zig", "std.special");
|
||||
}
|
||||
|
||||
static void create_test_compile_var_and_add_test_runner(CodeGen *g) {
|
||||
@@ -8089,24 +8107,24 @@ static void gen_root_source(CodeGen *g) {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
g->root_import = add_source_file(g, g->root_package, resolved_path, source_code);
|
||||
ZigType *root_import_alias = add_source_file(g, g->root_package, resolved_path, source_code, SourceKindRoot);
|
||||
assert(root_import_alias == g->root_import);
|
||||
|
||||
assert(g->root_out_name);
|
||||
assert(g->out_type != OutTypeUnknown);
|
||||
|
||||
{
|
||||
// Zig has lazy top level definitions. Here we semantically analyze the panic function.
|
||||
ImportTableEntry *import_with_panic;
|
||||
ZigType *import_with_panic;
|
||||
if (g->have_pub_panic) {
|
||||
import_with_panic = g->root_import;
|
||||
} else {
|
||||
g->panic_package = create_panic_pkg(g);
|
||||
import_with_panic = add_special_code(g, g->panic_package, "panic.zig");
|
||||
}
|
||||
scan_import(g, import_with_panic);
|
||||
Tld *panic_tld = find_decl(g, &import_with_panic->decls_scope->base, buf_create_from_str("panic"));
|
||||
Tld *panic_tld = find_decl(g, &get_container_scope(import_with_panic)->base, buf_create_from_str("panic"));
|
||||
assert(panic_tld != nullptr);
|
||||
resolve_top_level_decl(g, panic_tld, false, nullptr);
|
||||
resolve_top_level_decl(g, panic_tld, nullptr);
|
||||
}
|
||||
|
||||
|
||||
@@ -8358,7 +8376,6 @@ static void prepend_c_type_to_decl_list(CodeGen *g, GenH *gen_h, ZigType *type_e
|
||||
case ZigTypeIdComptimeInt:
|
||||
case ZigTypeIdUndefined:
|
||||
case ZigTypeIdNull:
|
||||
case ZigTypeIdNamespace:
|
||||
case ZigTypeIdBoundFn:
|
||||
case ZigTypeIdArgTuple:
|
||||
case ZigTypeIdErrorUnion:
|
||||
@@ -8496,7 +8513,7 @@ static void get_c_type(CodeGen *g, GenH *gen_h, ZigType *type_entry, Buf *out_bu
|
||||
if (child_type->zero_bits) {
|
||||
buf_init_from_str(out_buf, "bool");
|
||||
return;
|
||||
} else if (type_is_codegen_pointer(child_type)) {
|
||||
} else if (type_is_non_optional_pointer(child_type)) {
|
||||
return get_c_type(g, gen_h, child_type, out_buf);
|
||||
} else {
|
||||
zig_unreachable();
|
||||
@@ -8506,19 +8523,19 @@ static void get_c_type(CodeGen *g, GenH *gen_h, ZigType *type_entry, Buf *out_bu
|
||||
case ZigTypeIdOpaque:
|
||||
{
|
||||
buf_init_from_str(out_buf, "struct ");
|
||||
buf_append_buf(out_buf, &type_entry->name);
|
||||
buf_append_buf(out_buf, type_h_name(type_entry));
|
||||
return;
|
||||
}
|
||||
case ZigTypeIdUnion:
|
||||
{
|
||||
buf_init_from_str(out_buf, "union ");
|
||||
buf_append_buf(out_buf, &type_entry->name);
|
||||
buf_append_buf(out_buf, type_h_name(type_entry));
|
||||
return;
|
||||
}
|
||||
case ZigTypeIdEnum:
|
||||
{
|
||||
buf_init_from_str(out_buf, "enum ");
|
||||
buf_append_buf(out_buf, &type_entry->name);
|
||||
buf_append_buf(out_buf, type_h_name(type_entry));
|
||||
return;
|
||||
}
|
||||
case ZigTypeIdArray:
|
||||
@@ -8541,7 +8558,6 @@ static void get_c_type(CodeGen *g, GenH *gen_h, ZigType *type_entry, Buf *out_bu
|
||||
case ZigTypeIdInvalid:
|
||||
case ZigTypeIdMetaType:
|
||||
case ZigTypeIdBoundFn:
|
||||
case ZigTypeIdNamespace:
|
||||
case ZigTypeIdComptimeFloat:
|
||||
case ZigTypeIdComptimeInt:
|
||||
case ZigTypeIdUndefined:
|
||||
@@ -8614,10 +8630,17 @@ static void gen_h_file(CodeGen *g) {
|
||||
Buf return_type_c = BUF_INIT;
|
||||
get_c_type(g, gen_h, fn_type_id->return_type, &return_type_c);
|
||||
|
||||
Buf *symbol_name;
|
||||
if (fn_table_entry->export_list.length == 0) {
|
||||
symbol_name = &fn_table_entry->symbol_name;
|
||||
} else {
|
||||
FnExport *fn_export = &fn_table_entry->export_list.items[0];
|
||||
symbol_name = &fn_export->name;
|
||||
}
|
||||
buf_appendf(&h_buf, "%s %s %s(",
|
||||
buf_ptr(export_macro),
|
||||
buf_ptr(&return_type_c),
|
||||
buf_ptr(&fn_table_entry->symbol_name));
|
||||
buf_ptr(symbol_name));
|
||||
|
||||
Buf param_type_c = BUF_INIT;
|
||||
if (fn_type_id->param_count > 0) {
|
||||
@@ -8693,7 +8716,6 @@ static void gen_h_file(CodeGen *g) {
|
||||
case ZigTypeIdNull:
|
||||
case ZigTypeIdErrorUnion:
|
||||
case ZigTypeIdErrorSet:
|
||||
case ZigTypeIdNamespace:
|
||||
case ZigTypeIdBoundFn:
|
||||
case ZigTypeIdArgTuple:
|
||||
case ZigTypeIdOptional:
|
||||
@@ -8703,7 +8725,7 @@ static void gen_h_file(CodeGen *g) {
|
||||
zig_unreachable();
|
||||
case ZigTypeIdEnum:
|
||||
if (type_entry->data.enumeration.layout == ContainerLayoutExtern) {
|
||||
fprintf(out_h, "enum %s {\n", buf_ptr(&type_entry->name));
|
||||
fprintf(out_h, "enum %s {\n", buf_ptr(type_h_name(type_entry)));
|
||||
for (uint32_t field_i = 0; field_i < type_entry->data.enumeration.src_field_count; field_i += 1) {
|
||||
TypeEnumField *enum_field = &type_entry->data.enumeration.fields[field_i];
|
||||
Buf *value_buf = buf_alloc();
|
||||
@@ -8716,12 +8738,12 @@ static void gen_h_file(CodeGen *g) {
|
||||
}
|
||||
fprintf(out_h, "};\n\n");
|
||||
} else {
|
||||
fprintf(out_h, "enum %s;\n", buf_ptr(&type_entry->name));
|
||||
fprintf(out_h, "enum %s;\n", buf_ptr(type_h_name(type_entry)));
|
||||
}
|
||||
break;
|
||||
case ZigTypeIdStruct:
|
||||
if (type_entry->data.structure.layout == ContainerLayoutExtern) {
|
||||
fprintf(out_h, "struct %s {\n", buf_ptr(&type_entry->name));
|
||||
fprintf(out_h, "struct %s {\n", buf_ptr(type_h_name(type_entry)));
|
||||
for (uint32_t field_i = 0; field_i < type_entry->data.structure.src_field_count; field_i += 1) {
|
||||
TypeStructField *struct_field = &type_entry->data.structure.fields[field_i];
|
||||
|
||||
@@ -8739,12 +8761,12 @@ static void gen_h_file(CodeGen *g) {
|
||||
}
|
||||
fprintf(out_h, "};\n\n");
|
||||
} else {
|
||||
fprintf(out_h, "struct %s;\n", buf_ptr(&type_entry->name));
|
||||
fprintf(out_h, "struct %s;\n", buf_ptr(type_h_name(type_entry)));
|
||||
}
|
||||
break;
|
||||
case ZigTypeIdUnion:
|
||||
if (type_entry->data.unionation.layout == ContainerLayoutExtern) {
|
||||
fprintf(out_h, "union %s {\n", buf_ptr(&type_entry->name));
|
||||
fprintf(out_h, "union %s {\n", buf_ptr(type_h_name(type_entry)));
|
||||
for (uint32_t field_i = 0; field_i < type_entry->data.unionation.src_field_count; field_i += 1) {
|
||||
TypeUnionField *union_field = &type_entry->data.unionation.fields[field_i];
|
||||
|
||||
@@ -8754,11 +8776,11 @@ static void gen_h_file(CodeGen *g) {
|
||||
}
|
||||
fprintf(out_h, "};\n\n");
|
||||
} else {
|
||||
fprintf(out_h, "union %s;\n", buf_ptr(&type_entry->name));
|
||||
fprintf(out_h, "union %s;\n", buf_ptr(type_h_name(type_entry)));
|
||||
}
|
||||
break;
|
||||
case ZigTypeIdOpaque:
|
||||
fprintf(out_h, "struct %s;\n\n", buf_ptr(&type_entry->name));
|
||||
fprintf(out_h, "struct %s;\n\n", buf_ptr(type_h_name(type_entry)));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -8792,7 +8814,7 @@ void codegen_add_time_event(CodeGen *g, const char *name) {
|
||||
g->timing_events.append({os_get_time(), name});
|
||||
}
|
||||
|
||||
static void add_cache_pkg(CodeGen *g, CacheHash *ch, PackageTableEntry *pkg) {
|
||||
static void add_cache_pkg(CodeGen *g, CacheHash *ch, ZigPackage *pkg) {
|
||||
if (buf_len(&pkg->root_src_path) == 0)
|
||||
return;
|
||||
|
||||
@@ -9046,9 +9068,11 @@ void codegen_build_and_link(CodeGen *g) {
|
||||
codegen_add_time_event(g, "Done");
|
||||
}
|
||||
|
||||
PackageTableEntry *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path) {
|
||||
ZigPackage *codegen_create_package(CodeGen *g, const char *root_src_dir, const char *root_src_path,
|
||||
const char *pkg_path)
|
||||
{
|
||||
init(g);
|
||||
PackageTableEntry *pkg = new_package(root_src_dir, root_src_path);
|
||||
ZigPackage *pkg = new_package(root_src_dir, root_src_path, pkg_path);
|
||||
if (g->std_package != nullptr) {
|
||||
assert(g->compile_var_package != nullptr);
|
||||
pkg->package_table.put(buf_create_from_str("std"), g->std_package);
|
||||
|
||||
Reference in New Issue
Block a user