aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndrea Orru <andrea@orru.io>2018-04-11 00:33:19 -0700
committerAndrea Orru <andrea@orru.io>2018-04-11 00:33:19 -0700
commit135a335ce12a33666e44cb13e0be4bb893877565 (patch)
tree23b10bed3d36e3fb244c6a2e5d5765d8b7a71fb5 /src
parentb01c5a95c468650f143e0ae96f6c3865852fdcda (diff)
parentf43711e5fbbedafa1c28c933fdca0949427c77cd (diff)
downloadzig-135a335ce12a33666e44cb13e0be4bb893877565.tar.gz
zig-135a335ce12a33666e44cb13e0be4bb893877565.zip
Merge branch 'master' into zen_stdlib
Diffstat (limited to 'src')
-rw-r--r--src/all_types.hpp85
-rw-r--r--src/analyze.cpp169
-rw-r--r--src/analyze.hpp1
-rw-r--r--src/ast_render.cpp25
-rw-r--r--src/codegen.cpp424
-rw-r--r--src/config.h.in8
-rw-r--r--src/ir.cpp492
-rw-r--r--src/ir_print.cpp64
-rw-r--r--src/link.cpp61
-rw-r--r--src/main.cpp68
-rw-r--r--src/os.cpp101
-rw-r--r--src/os.hpp6
-rw-r--r--src/parser.cpp162
-rw-r--r--src/parser.hpp1
-rw-r--r--src/target.cpp4
-rw-r--r--src/tokenizer.cpp2
-rw-r--r--src/tokenizer.hpp1
17 files changed, 1310 insertions, 364 deletions
diff --git a/src/all_types.hpp b/src/all_types.hpp
index 6afa7ccf20..d27a5c7a1c 100644
--- a/src/all_types.hpp
+++ b/src/all_types.hpp
@@ -359,7 +359,6 @@ enum NodeType {
NodeTypeRoot,
NodeTypeFnProto,
NodeTypeFnDef,
- NodeTypeFnDecl,
NodeTypeParamDecl,
NodeTypeBlock,
NodeTypeGroupedExpr,
@@ -409,6 +408,7 @@ enum NodeType {
NodeTypeResume,
NodeTypeAwaitExpr,
NodeTypeSuspend,
+ NodeTypePromiseType,
};
struct AstNodeRoot {
@@ -452,10 +452,6 @@ struct AstNodeFnDef {
AstNode *body;
};
-struct AstNodeFnDecl {
- AstNode *fn_proto;
-};
-
struct AstNodeParamDecl {
Buf *name;
AstNode *type;
@@ -712,10 +708,6 @@ struct AstNodeSwitchRange {
AstNode *end;
};
-struct AstNodeLabel {
- Buf *name;
-};
-
struct AstNodeCompTime {
AstNode *expr;
};
@@ -879,6 +871,10 @@ struct AstNodeSuspend {
AstNode *promise_symbol;
};
+struct AstNodePromiseType {
+ AstNode *payload_type; // can be NULL
+};
+
struct AstNode {
enum NodeType type;
size_t line;
@@ -887,7 +883,6 @@ struct AstNode {
union {
AstNodeRoot root;
AstNodeFnDef fn_def;
- AstNodeFnDecl fn_decl;
AstNodeFnProto fn_proto;
AstNodeParamDecl param_decl;
AstNodeBlock block;
@@ -912,7 +907,6 @@ struct AstNode {
AstNodeSwitchExpr switch_expr;
AstNodeSwitchProng switch_prong;
AstNodeSwitchRange switch_range;
- AstNodeLabel label;
AstNodeCompTime comptime_expr;
AstNodeAsmExpr asm_expr;
AstNodeFieldAccessExpr field_access_expr;
@@ -939,6 +933,7 @@ struct AstNode {
AstNodeResumeExpr resume_expr;
AstNodeAwaitExpr await_expr;
AstNodeSuspend suspend;
+ AstNodePromiseType promise_type;
} data;
};
@@ -1251,7 +1246,10 @@ struct FnTableEntry {
ScopeBlock *def_scope; // parent is child_scope
Buf symbol_name;
TypeTableEntry *type_entry; // function type
- TypeTableEntry *implicit_return_type;
+ // in the case of normal functions this is the implicit return type
+ // in the case of async functions this is the implicit return type according to the
+ // zig source code, not according to zig ir
+ TypeTableEntry *src_implicit_return_type;
bool is_test;
FnInline fn_inline;
FnAnalState anal_state;
@@ -1612,7 +1610,8 @@ struct CodeGen {
FnTableEntry *panic_fn;
LLVMValueRef cur_ret_ptr;
LLVMValueRef cur_fn_val;
- LLVMValueRef cur_err_ret_trace_val;
+ LLVMValueRef cur_err_ret_trace_val_arg;
+ LLVMValueRef cur_err_ret_trace_val_stack;
bool c_want_stdint;
bool c_want_stdbool;
AstNode *root_export_decl;
@@ -1646,6 +1645,8 @@ struct CodeGen {
LLVMValueRef coro_save_fn_val;
LLVMValueRef coro_promise_fn_val;
LLVMValueRef coro_alloc_helper_fn_val;
+ LLVMValueRef merge_err_ret_traces_fn_val;
+ LLVMValueRef add_error_return_trace_addr_fn_val;
bool error_during_imports;
const char **clang_argv;
@@ -1751,6 +1752,7 @@ enum ScopeId {
ScopeIdLoop,
ScopeIdFnDef,
ScopeIdCompTime,
+ ScopeIdCoroPrelude,
};
struct Scope {
@@ -1858,6 +1860,12 @@ struct ScopeFnDef {
FnTableEntry *fn_entry;
};
+// This scope is created to indicate that the code in the scope
+// is auto-generated coroutine prelude stuff.
+struct ScopeCoroPrelude {
+ Scope base;
+};
+
// synchronized with code in define_builtin_compile_vars
enum AtomicOrder {
AtomicOrderUnordered,
@@ -1944,6 +1952,7 @@ enum IrInstructionId {
IrInstructionIdSetRuntimeSafety,
IrInstructionIdSetFloatMode,
IrInstructionIdArrayType,
+ IrInstructionIdPromiseType,
IrInstructionIdSliceType,
IrInstructionIdAsm,
IrInstructionIdSizeOf,
@@ -2034,6 +2043,10 @@ enum IrInstructionId {
IrInstructionIdAtomicRmw,
IrInstructionIdPromiseResultType,
IrInstructionIdAwaitBookkeeping,
+ IrInstructionIdSaveErrRetAddr,
+ IrInstructionIdAddImplicitReturnType,
+ IrInstructionIdMergeErrRetTraces,
+ IrInstructionIdMarkErrRetTracePtr,
};
struct IrInstruction {
@@ -2360,6 +2373,12 @@ struct IrInstructionArrayType {
IrInstruction *child_type;
};
+struct IrInstructionPromiseType {
+ IrInstruction base;
+
+ IrInstruction *payload_type;
+};
+
struct IrInstructionSliceType {
IrInstruction base;
@@ -2673,6 +2692,7 @@ struct IrInstructionFnProto {
IrInstruction **param_types;
IrInstruction *align_value;
IrInstruction *return_type;
+ IrInstruction *async_allocator_type_value;
bool is_var_args;
};
@@ -2865,6 +2885,11 @@ struct IrInstructionExport {
struct IrInstructionErrorReturnTrace {
IrInstruction base;
+
+ enum Nullable {
+ Null,
+ NonNull,
+ } nullable;
};
struct IrInstructionErrorUnion {
@@ -2987,6 +3012,30 @@ struct IrInstructionAwaitBookkeeping {
IrInstruction *promise_result_type;
};
+struct IrInstructionSaveErrRetAddr {
+ IrInstruction base;
+};
+
+struct IrInstructionAddImplicitReturnType {
+ IrInstruction base;
+
+ IrInstruction *value;
+};
+
+struct IrInstructionMergeErrRetTraces {
+ IrInstruction base;
+
+ IrInstruction *coro_promise_ptr;
+ IrInstruction *src_err_ret_trace_ptr;
+ IrInstruction *dest_err_ret_trace_ptr;
+};
+
+struct IrInstructionMarkErrRetTracePtr {
+ IrInstruction base;
+
+ IrInstruction *err_ret_trace_ptr;
+};
+
static const size_t slice_ptr_index = 0;
static const size_t slice_len_index = 1;
@@ -2996,10 +3045,18 @@ static const size_t maybe_null_index = 1;
static const size_t err_union_err_index = 0;
static const size_t err_union_payload_index = 1;
+// TODO call graph analysis to find out what this number needs to be for every function
+static const size_t stack_trace_ptr_count = 30;
+
+// these belong to the async function
+#define RETURN_ADDRESSES_FIELD_NAME "return_addresses"
+#define ERR_RET_TRACE_FIELD_NAME "err_ret_trace"
+#define RESULT_FIELD_NAME "result"
#define ASYNC_ALLOC_FIELD_NAME "allocFn"
#define ASYNC_FREE_FIELD_NAME "freeFn"
#define AWAITER_HANDLE_FIELD_NAME "awaiter_handle"
-#define RESULT_FIELD_NAME "result"
+// these point to data belonging to the awaiter
+#define ERR_RET_TRACE_PTR_FIELD_NAME "err_ret_trace_ptr"
#define RESULT_PTR_FIELD_NAME "result_ptr"
diff --git a/src/analyze.cpp b/src/analyze.cpp
index 395df229cd..c73e6b39e3 100644
--- a/src/analyze.cpp
+++ b/src/analyze.cpp
@@ -170,6 +170,12 @@ Scope *create_comptime_scope(AstNode *node, Scope *parent) {
return &scope->base;
}
+Scope *create_coro_prelude_scope(AstNode *node, Scope *parent) {
+ ScopeCoroPrelude *scope = allocate<ScopeCoroPrelude>(1);
+ init_scope(&scope->base, ScopeIdCoroPrelude, node, parent);
+ return &scope->base;
+}
+
ImportTableEntry *get_scope_import(Scope *scope) {
while (scope) {
if (scope->id == ScopeIdDecls) {
@@ -462,10 +468,30 @@ TypeTableEntry *get_promise_frame_type(CodeGen *g, TypeTableEntry *return_type)
TypeTableEntry *awaiter_handle_type = get_maybe_type(g, g->builtin_types.entry_promise);
TypeTableEntry *result_ptr_type = get_pointer_to_type(g, return_type, false);
- const char *field_names[] = {AWAITER_HANDLE_FIELD_NAME, RESULT_FIELD_NAME, RESULT_PTR_FIELD_NAME};
- TypeTableEntry *field_types[] = {awaiter_handle_type, return_type, result_ptr_type};
+
+ ZigList<const char *> field_names = {};
+ field_names.append(AWAITER_HANDLE_FIELD_NAME);
+ field_names.append(RESULT_FIELD_NAME);
+ field_names.append(RESULT_PTR_FIELD_NAME);
+ if (g->have_err_ret_tracing) {
+ field_names.append(ERR_RET_TRACE_PTR_FIELD_NAME);
+ field_names.append(ERR_RET_TRACE_FIELD_NAME);
+ field_names.append(RETURN_ADDRESSES_FIELD_NAME);
+ }
+
+ ZigList<TypeTableEntry *> field_types = {};
+ field_types.append(awaiter_handle_type);
+ field_types.append(return_type);
+ field_types.append(result_ptr_type);
+ if (g->have_err_ret_tracing) {
+ field_types.append(get_ptr_to_stack_trace_type(g));
+ field_types.append(g->stack_trace_type);
+ field_types.append(get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count));
+ }
+
+ assert(field_names.length == field_types.length);
Buf *name = buf_sprintf("AsyncFramePromise(%s)", buf_ptr(&return_type->name));
- TypeTableEntry *entry = get_struct_type(g, buf_ptr(name), field_names, field_types, 3);
+ TypeTableEntry *entry = get_struct_type(g, buf_ptr(name), field_names.items, field_types.items, field_names.length);
return_type->promise_frame_parent = entry;
return entry;
@@ -985,7 +1011,8 @@ TypeTableEntry *get_fn_type(CodeGen *g, FnTypeId *fn_type_id) {
// populate the name of the type
buf_resize(&fn_type->name, 0);
if (fn_type->data.fn.fn_type_id.cc == CallingConventionAsync) {
- buf_appendf(&fn_type->name, "async(%s) ", buf_ptr(&fn_type_id->async_allocator_type->name));
+ assert(fn_type_id->async_allocator_type != nullptr);
+ buf_appendf(&fn_type->name, "async<%s> ", buf_ptr(&fn_type_id->async_allocator_type->name));
} else {
const char *cc_str = calling_convention_fn_type_str(fn_type->data.fn.fn_type_id.cc);
buf_appendf(&fn_type->name, "%s", cc_str);
@@ -3209,7 +3236,6 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
break;
case NodeTypeContainerDecl:
case NodeTypeParamDecl:
- case NodeTypeFnDecl:
case NodeTypeReturnExpr:
case NodeTypeDefer:
case NodeTypeBlock:
@@ -3253,6 +3279,7 @@ void scan_decls(CodeGen *g, ScopeDecls *decls_scope, AstNode *node) {
case NodeTypeResume:
case NodeTypeAwaitExpr:
case NodeTypeSuspend:
+ case NodeTypePromiseType:
zig_unreachable();
}
}
@@ -3590,6 +3617,7 @@ FnTableEntry *scope_get_fn_if_root(Scope *scope) {
case ScopeIdCImport:
case ScopeIdLoop:
case ScopeIdCompTime:
+ case ScopeIdCoroPrelude:
scope = scope->parent;
continue;
case ScopeIdFnDef:
@@ -3864,7 +3892,7 @@ void analyze_fn_ir(CodeGen *g, FnTableEntry *fn_table_entry, AstNode *return_typ
TypeTableEntry *block_return_type = ir_analyze(g, &fn_table_entry->ir_executable,
&fn_table_entry->analyzed_executable, fn_type_id->return_type, return_type_node);
- fn_table_entry->implicit_return_type = block_return_type;
+ fn_table_entry->src_implicit_return_type = block_return_type;
if (type_is_invalid(block_return_type) || fn_table_entry->analyzed_executable.invalid) {
assert(g->errors.length > 0);
@@ -3876,10 +3904,10 @@ void analyze_fn_ir(CodeGen *g, FnTableEntry *fn_table_entry, AstNode *return_typ
TypeTableEntry *return_err_set_type = fn_type_id->return_type->data.error_union.err_set_type;
if (return_err_set_type->data.error_set.infer_fn != nullptr) {
TypeTableEntry *inferred_err_set_type;
- if (fn_table_entry->implicit_return_type->id == TypeTableEntryIdErrorSet) {
- inferred_err_set_type = fn_table_entry->implicit_return_type;
- } else if (fn_table_entry->implicit_return_type->id == TypeTableEntryIdErrorUnion) {
- inferred_err_set_type = fn_table_entry->implicit_return_type->data.error_union.err_set_type;
+ if (fn_table_entry->src_implicit_return_type->id == TypeTableEntryIdErrorSet) {
+ inferred_err_set_type = fn_table_entry->src_implicit_return_type;
+ } else if (fn_table_entry->src_implicit_return_type->id == TypeTableEntryIdErrorUnion) {
+ inferred_err_set_type = fn_table_entry->src_implicit_return_type->data.error_union.err_set_type;
} else {
add_node_error(g, return_type_node,
buf_sprintf("function with inferred error set must return at least one possible error"));
@@ -4276,26 +4304,118 @@ static ZigWindowsSDK *get_windows_sdk(CodeGen *g) {
return g->win_sdk;
}
+
+Buf *get_linux_libc_lib_path(const char *o_file) {
+ const char *cc_exe = getenv("CC");
+ cc_exe = (cc_exe == nullptr) ? "cc" : cc_exe;
+ ZigList<const char *> args = {};
+ args.append(buf_ptr(buf_sprintf("-print-file-name=%s", o_file)));
+ Termination term;
+ Buf *out_stderr = buf_alloc();
+ Buf *out_stdout = buf_alloc();
+ int err;
+ if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {
+ zig_panic("unable to determine libc lib path: executing C compiler: %s", err_str(err));
+ }
+ if (term.how != TerminationIdClean || term.code != 0) {
+ zig_panic("unable to determine libc lib path: executing C compiler command failed");
+ }
+ if (buf_ends_with_str(out_stdout, "\n")) {
+ buf_resize(out_stdout, buf_len(out_stdout) - 1);
+ }
+ if (buf_len(out_stdout) == 0 || buf_eql_str(out_stdout, o_file)) {
+ zig_panic("unable to determine libc lib path: C compiler could not find %s", o_file);
+ }
+ Buf *result = buf_alloc();
+ os_path_dirname(out_stdout, result);
+ return result;
+}
+
+Buf *get_linux_libc_include_path(void) {
+ const char *cc_exe = getenv("CC");
+ cc_exe = (cc_exe == nullptr) ? "cc" : cc_exe;
+ ZigList<const char *> args = {};
+ args.append("-E");
+ args.append("-Wp,-v");
+ args.append("-xc");
+ args.append("/dev/null");
+ Termination term;
+ Buf *out_stderr = buf_alloc();
+ Buf *out_stdout = buf_alloc();
+ int err;
+ if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {
+ zig_panic("unable to determine libc include path: executing C compiler: %s", err_str(err));
+ }
+ if (term.how != TerminationIdClean || term.code != 0) {
+ zig_panic("unable to determine libc include path: executing C compiler command failed");
+ }
+ char *prev_newline = buf_ptr(out_stderr);
+ ZigList<const char *> search_paths = {};
+ bool found_search_paths = false;
+ for (;;) {
+ char *newline = strchr(prev_newline, '\n');
+ if (newline == nullptr) {
+ zig_panic("unable to determine libc include path: bad output from C compiler command");
+ }
+ *newline = 0;
+ if (found_search_paths) {
+ if (strcmp(prev_newline, "End of search list.") == 0) {
+ break;
+ }
+ search_paths.append(prev_newline);
+ } else {
+ if (strcmp(prev_newline, "#include <...> search starts here:") == 0) {
+ found_search_paths = true;
+ }
+ }
+ prev_newline = newline + 1;
+ }
+ if (search_paths.length == 0) {
+ zig_panic("unable to determine libc include path: even C compiler does not know where libc headers are");
+ }
+ for (size_t i = 0; i < search_paths.length; i += 1) {
+ // search in reverse order
+ const char *search_path = search_paths.items[search_paths.length - i - 1];
+ // cut off spaces
+ while (*search_path == ' ') {
+ search_path += 1;
+ }
+ Buf *stdlib_path = buf_sprintf("%s/stdlib.h", search_path);
+ bool exists;
+ if ((err = os_file_exists(stdlib_path, &exists))) {
+ exists = false;
+ }
+ if (exists) {
+ return buf_create_from_str(search_path);
+ }
+ }
+ zig_panic("unable to determine libc include path: stdlib.h not found in C compiler search paths");
+}
+
void find_libc_include_path(CodeGen *g) {
- if (!g->libc_include_dir || buf_len(g->libc_include_dir) == 0) {
- ZigWindowsSDK *sdk = get_windows_sdk(g);
+ if (g->libc_include_dir == nullptr) {
if (g->zig_target.os == OsWindows) {
+ ZigWindowsSDK *sdk = get_windows_sdk(g);
+ g->libc_include_dir = buf_alloc();
if (os_get_win32_ucrt_include_path(sdk, g->libc_include_dir)) {
zig_panic("Unable to determine libc include path.");
}
+ } else if (g->zig_target.os == OsLinux) {
+ g->libc_include_dir = get_linux_libc_include_path();
+ } else if (g->zig_target.os == OsMacOSX) {
+ g->libc_include_dir = buf_create_from_str("/usr/include");
+ } else {
+ // TODO find libc at runtime for other operating systems
+ zig_panic("Unable to determine libc include path.");
}
}
-
- // TODO find libc at runtime for other operating systems
- if(!g->libc_include_dir || buf_len(g->libc_include_dir) == 0) {
- zig_panic("Unable to determine libc include path.");
- }
+ assert(buf_len(g->libc_include_dir) != 0);
}
void find_libc_lib_path(CodeGen *g) {
// later we can handle this better by reporting an error via the normal mechanism
- if (!g->libc_lib_dir || buf_len(g->libc_lib_dir) == 0 ||
+ if (g->libc_lib_dir == nullptr ||
(g->zig_target.os == OsWindows && (g->msvc_lib_dir == nullptr || g->kernel32_lib_dir == nullptr)))
{
if (g->zig_target.os == OsWindows) {
@@ -4319,18 +4439,25 @@ void find_libc_lib_path(CodeGen *g) {
g->msvc_lib_dir = vc_lib_dir;
g->libc_lib_dir = ucrt_lib_path;
g->kernel32_lib_dir = kern_lib_path;
+ } else if (g->zig_target.os == OsLinux) {
+ g->libc_lib_dir = get_linux_libc_lib_path("crt1.o");
} else {
zig_panic("Unable to determine libc lib path.");
}
+ } else {
+ assert(buf_len(g->libc_lib_dir) != 0);
}
- if (!g->libc_static_lib_dir || buf_len(g->libc_static_lib_dir) == 0) {
+ if (g->libc_static_lib_dir == nullptr) {
if ((g->zig_target.os == OsWindows) && (g->msvc_lib_dir != NULL)) {
return;
- }
- else {
+ } else if (g->zig_target.os == OsLinux) {
+ g->libc_static_lib_dir = get_linux_libc_lib_path("crtbegin.o");
+ } else {
zig_panic("Unable to determine libc static lib path.");
}
+ } else {
+ assert(buf_len(g->libc_static_lib_dir) != 0);
}
}
diff --git a/src/analyze.hpp b/src/analyze.hpp
index 936134030d..aa4557666b 100644
--- a/src/analyze.hpp
+++ b/src/analyze.hpp
@@ -107,6 +107,7 @@ ScopeLoop *create_loop_scope(AstNode *node, Scope *parent);
ScopeFnDef *create_fndef_scope(AstNode *node, Scope *parent, FnTableEntry *fn_entry);
ScopeDecls *create_decls_scope(AstNode *node, Scope *parent, TypeTableEntry *container_type, ImportTableEntry *import);
Scope *create_comptime_scope(AstNode *node, Scope *parent);
+Scope *create_coro_prelude_scope(AstNode *node, Scope *parent);
void init_const_str_lit(CodeGen *g, ConstExprValue *const_val, Buf *str);
ConstExprValue *create_const_str_lit(CodeGen *g, Buf *str);
diff --git a/src/ast_render.cpp b/src/ast_render.cpp
index f88feee856..2c3e1fc873 100644
--- a/src/ast_render.cpp
+++ b/src/ast_render.cpp
@@ -148,8 +148,6 @@ static const char *node_type_str(NodeType node_type) {
return "Root";
case NodeTypeFnDef:
return "FnDef";
- case NodeTypeFnDecl:
- return "FnDecl";
case NodeTypeFnProto:
return "FnProto";
case NodeTypeParamDecl:
@@ -250,6 +248,8 @@ static const char *node_type_str(NodeType node_type) {
return "AwaitExpr";
case NodeTypeSuspend:
return "Suspend";
+ case NodeTypePromiseType:
+ return "PromiseType";
}
zig_unreachable();
}
@@ -658,6 +658,15 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
if (node->data.fn_call_expr.is_builtin) {
fprintf(ar->f, "@");
}
+ if (node->data.fn_call_expr.is_async) {
+ fprintf(ar->f, "async");
+ if (node->data.fn_call_expr.async_allocator != nullptr) {
+ fprintf(ar->f, "<");
+ render_node_extra(ar, node->data.fn_call_expr.async_allocator, true);
+ fprintf(ar->f, ">");
+ }
+ fprintf(ar->f, " ");
+ }
AstNode *fn_ref_node = node->data.fn_call_expr.fn_ref_expr;
bool grouped = (fn_ref_node->type != NodeTypePrefixOpExpr && fn_ref_node->type != NodeTypeAddrOfExpr);
render_node_extra(ar, fn_ref_node, grouped);
@@ -772,6 +781,15 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
render_node_ungrouped(ar, node->data.array_type.child_type);
break;
}
+ case NodeTypePromiseType:
+ {
+ fprintf(ar->f, "promise");
+ if (node->data.promise_type.payload_type != nullptr) {
+ fprintf(ar->f, "->");
+ render_node_grouped(ar, node->data.promise_type.payload_type);
+ }
+ break;
+ }
case NodeTypeErrorType:
fprintf(ar->f, "error");
break;
@@ -1023,7 +1041,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
case NodeTypeUnwrapErrorExpr:
{
render_node_ungrouped(ar, node->data.unwrap_err_expr.op1);
- fprintf(ar->f, " %%%% ");
+ fprintf(ar->f, " catch ");
if (node->data.unwrap_err_expr.symbol) {
Buf *var_name = node->data.unwrap_err_expr.symbol->data.symbol_expr.symbol;
fprintf(ar->f, "|%s| ", buf_ptr(var_name));
@@ -1078,7 +1096,6 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) {
}
break;
}
- case NodeTypeFnDecl:
case NodeTypeParamDecl:
case NodeTypeTestDecl:
case NodeTypeStructField:
diff --git a/src/codegen.cpp b/src/codegen.cpp
index a44091bdbf..2aca143524 100644
--- a/src/codegen.cpp
+++ b/src/codegen.cpp
@@ -112,10 +112,10 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
// that's for native compilation
g->zig_target = *target;
resolve_target_object_format(&g->zig_target);
- g->dynamic_linker = buf_create_from_str("");
- g->libc_lib_dir = buf_create_from_str("");
- g->libc_static_lib_dir = buf_create_from_str("");
- g->libc_include_dir = buf_create_from_str("");
+ g->dynamic_linker = nullptr;
+ g->libc_lib_dir = nullptr;
+ g->libc_static_lib_dir = nullptr;
+ g->libc_include_dir = nullptr;
g->msvc_lib_dir = nullptr;
g->kernel32_lib_dir = nullptr;
g->each_lib_rpath = false;
@@ -123,16 +123,13 @@ CodeGen *codegen_create(Buf *root_src_path, const ZigTarget *target, OutType out
// native compilation, we can rely on the configuration stuff
g->is_native_target = true;
get_native_target(&g->zig_target);
- g->dynamic_linker = buf_create_from_str(ZIG_DYNAMIC_LINKER);
- g->libc_lib_dir = buf_create_from_str(ZIG_LIBC_LIB_DIR);
- g->libc_static_lib_dir = buf_create_from_str(ZIG_LIBC_STATIC_LIB_DIR);
- g->libc_include_dir = buf_create_from_str(ZIG_LIBC_INCLUDE_DIR);
+ g->dynamic_linker = nullptr; // find it at runtime
+ g->libc_lib_dir = nullptr; // find it at runtime
+ g->libc_static_lib_dir = nullptr; // find it at runtime
+ g->libc_include_dir = nullptr; // find it at runtime
g->msvc_lib_dir = nullptr; // find it at runtime
g->kernel32_lib_dir = nullptr; // find it at runtime
-
-#ifdef ZIG_EACH_LIB_RPATH
g->each_lib_rpath = true;
-#endif
if (g->zig_target.os == OsMacOSX ||
g->zig_target.os == OsIOS)
@@ -411,6 +408,9 @@ static uint32_t get_err_ret_trace_arg_index(CodeGen *g, FnTableEntry *fn_table_e
if (!g->have_err_ret_tracing) {
return UINT32_MAX;
}
+ if (fn_table_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync) {
+ return 0;
+ }
TypeTableEntry *fn_type = fn_table_entry->type_entry;
if (!fn_type_can_fail(&fn_type->data.fn.fn_type_id)) {
return UINT32_MAX;
@@ -653,6 +653,7 @@ static ZigLLVMDIScope *get_di_scope(CodeGen *g, Scope *scope) {
case ScopeIdDeferExpr:
case ScopeIdLoop:
case ScopeIdCompTime:
+ case ScopeIdCoroPrelude:
return get_di_scope(g, scope->parent);
}
zig_unreachable();
@@ -1116,22 +1117,19 @@ static LLVMValueRef get_return_address_fn_val(CodeGen *g) {
return g->return_address_fn_val;
}
-static LLVMValueRef get_return_err_fn(CodeGen *g) {
- if (g->return_err_fn != nullptr)
- return g->return_err_fn;
-
- assert(g->err_tag_type != nullptr);
+static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
+ if (g->add_error_return_trace_addr_fn_val != nullptr)
+ return g->add_error_return_trace_addr_fn_val;
LLVMTypeRef arg_types[] = {
- // error return trace pointer
get_ptr_to_stack_trace_type(g)->type_ref,
+ g->builtin_types.entry_usize->type_ref,
};
- LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 1, false);
+ LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 2, false);
- Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_return_error"), false);
+ Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_add_err_ret_trace_addr"), false);
LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);
- addLLVMFnAttr(fn_val, "noinline"); // so that we can look at return address
- addLLVMFnAttr(fn_val, "cold");
+ addLLVMFnAttr(fn_val, "alwaysinline");
LLVMSetLinkage(fn_val, LLVMInternalLinkage);
LLVMSetFunctionCallConv(fn_val, get_llvm_cc(g, CallingConventionUnspecified));
addLLVMFnAttr(fn_val, "nounwind");
@@ -1153,6 +1151,8 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
// stack_trace.instruction_addresses[stack_trace.index % stack_trace.instruction_addresses.len] = return_address;
LLVMValueRef err_ret_trace_ptr = LLVMGetParam(fn_val, 0);
+ LLVMValueRef address_value = LLVMGetParam(fn_val, 1);
+
size_t index_field_index = g->stack_trace_type->data.structure.fields[0].gen_index;
LLVMValueRef index_field_ptr = LLVMBuildStructGEP(g->builder, err_ret_trace_ptr, (unsigned)index_field_index, "");
size_t addresses_field_index = g->stack_trace_type->data.structure.fields[1].gen_index;
@@ -1174,15 +1174,10 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
LLVMValueRef ptr_value = gen_load_untyped(g, ptr_field_ptr, 0, false, "");
LLVMValueRef address_slot = LLVMBuildInBoundsGEP(g->builder, ptr_value, address_indices, 1, "");
- LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);
- LLVMValueRef return_address_ptr = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");
- LLVMValueRef return_address = LLVMBuildPtrToInt(g->builder, return_address_ptr, usize_type_ref, "");
-
- LLVMValueRef address_value = LLVMBuildPtrToInt(g->builder, return_address, usize_type_ref, "");
gen_store_untyped(g, address_value, address_slot, 0, false);
// stack_trace.index += 1;
- LLVMValueRef index_plus_one_val = LLVMBuildAdd(g->builder, index_val, LLVMConstInt(usize_type_ref, 1, false), "");
+ LLVMValueRef index_plus_one_val = LLVMBuildNUWAdd(g->builder, index_val, LLVMConstInt(usize_type_ref, 1, false), "");
gen_store_untyped(g, index_plus_one_val, index_field_ptr, 0, false);
// return;
@@ -1191,6 +1186,187 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
LLVMPositionBuilderAtEnd(g->builder, prev_block);
LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
+ g->add_error_return_trace_addr_fn_val = fn_val;
+ return fn_val;
+}
+
+static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
+ if (g->merge_err_ret_traces_fn_val)
+ return g->merge_err_ret_traces_fn_val;
+
+ assert(g->stack_trace_type != nullptr);
+
+ LLVMTypeRef param_types[] = {
+ get_ptr_to_stack_trace_type(g)->type_ref,
+ get_ptr_to_stack_trace_type(g)->type_ref,
+ };
+ LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), param_types, 2, false);
+
+ Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_merge_error_return_traces"), false);
+ LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);
+ LLVMSetLinkage(fn_val, LLVMInternalLinkage);
+ LLVMSetFunctionCallConv(fn_val, get_llvm_cc(g, CallingConventionUnspecified));
+ addLLVMFnAttr(fn_val, "nounwind");
+ add_uwtable_attr(g, fn_val);
+ addLLVMArgAttr(fn_val, (unsigned)0, "nonnull");
+ addLLVMArgAttr(fn_val, (unsigned)0, "noalias");
+ addLLVMArgAttr(fn_val, (unsigned)0, "writeonly");
+ addLLVMArgAttr(fn_val, (unsigned)1, "nonnull");
+ addLLVMArgAttr(fn_val, (unsigned)1, "noalias");
+ addLLVMArgAttr(fn_val, (unsigned)1, "readonly");
+ if (g->build_mode == BuildModeDebug) {
+ ZigLLVMAddFunctionAttr(fn_val, "no-frame-pointer-elim", "true");
+ ZigLLVMAddFunctionAttr(fn_val, "no-frame-pointer-elim-non-leaf", nullptr);
+ }
+
+ // this is above the ZigLLVMClearCurrentDebugLocation
+ LLVMValueRef add_error_return_trace_addr_fn_val = get_add_error_return_trace_addr_fn(g);
+
+ LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn_val, "Entry");
+ LLVMBasicBlockRef prev_block = LLVMGetInsertBlock(g->builder);
+ LLVMValueRef prev_debug_location = LLVMGetCurrentDebugLocation(g->builder);
+ LLVMPositionBuilderAtEnd(g->builder, entry_block);
+ ZigLLVMClearCurrentDebugLocation(g->builder);
+
+ // var frame_index: usize = undefined;
+ // var frames_left: usize = undefined;
+ // if (src_stack_trace.index < src_stack_trace.instruction_addresses.len) {
+ // frame_index = 0;
+ // frames_left = src_stack_trace.index;
+ // if (frames_left == 0) return;
+ // } else {
+ // frame_index = (src_stack_trace.index + 1) % src_stack_trace.instruction_addresses.len;
+ // frames_left = src_stack_trace.instruction_addresses.len;
+ // }
+ // while (true) {
+ // __zig_add_err_ret_trace_addr(dest_stack_trace, src_stack_trace.instruction_addresses[frame_index]);
+ // frames_left -= 1;
+ // if (frames_left == 0) return;
+ // frame_index = (frame_index + 1) % src_stack_trace.instruction_addresses.len;
+ // }
+ LLVMBasicBlockRef return_block = LLVMAppendBasicBlock(fn_val, "Return");
+
+ LLVMValueRef frame_index_ptr = LLVMBuildAlloca(g->builder, g->builtin_types.entry_usize->type_ref, "frame_index");
+ LLVMValueRef frames_left_ptr = LLVMBuildAlloca(g->builder, g->builtin_types.entry_usize->type_ref, "frames_left");
+
+ LLVMValueRef dest_stack_trace_ptr = LLVMGetParam(fn_val, 0);
+ LLVMValueRef src_stack_trace_ptr = LLVMGetParam(fn_val, 1);
+
+ size_t src_index_field_index = g->stack_trace_type->data.structure.fields[0].gen_index;
+ size_t src_addresses_field_index = g->stack_trace_type->data.structure.fields[1].gen_index;
+ LLVMValueRef src_index_field_ptr = LLVMBuildStructGEP(g->builder, src_stack_trace_ptr,
+ (unsigned)src_index_field_index, "");
+ LLVMValueRef src_addresses_field_ptr = LLVMBuildStructGEP(g->builder, src_stack_trace_ptr,
+ (unsigned)src_addresses_field_index, "");
+ TypeTableEntry *slice_type = g->stack_trace_type->data.structure.fields[1].type_entry;
+ size_t ptr_field_index = slice_type->data.structure.fields[slice_ptr_index].gen_index;
+ LLVMValueRef src_ptr_field_ptr = LLVMBuildStructGEP(g->builder, src_addresses_field_ptr, (unsigned)ptr_field_index, "");
+ size_t len_field_index = slice_type->data.structure.fields[slice_len_index].gen_index;
+ LLVMValueRef src_len_field_ptr = LLVMBuildStructGEP(g->builder, src_addresses_field_ptr, (unsigned)len_field_index, "");
+ LLVMValueRef src_index_val = LLVMBuildLoad(g->builder, src_index_field_ptr, "");
+ LLVMValueRef src_ptr_val = LLVMBuildLoad(g->builder, src_ptr_field_ptr, "");
+ LLVMValueRef src_len_val = LLVMBuildLoad(g->builder, src_len_field_ptr, "");
+ LLVMValueRef no_wrap_bit = LLVMBuildICmp(g->builder, LLVMIntULT, src_index_val, src_len_val, "");
+ LLVMBasicBlockRef no_wrap_block = LLVMAppendBasicBlock(fn_val, "NoWrap");
+ LLVMBasicBlockRef yes_wrap_block = LLVMAppendBasicBlock(fn_val, "YesWrap");
+ LLVMBasicBlockRef loop_block = LLVMAppendBasicBlock(fn_val, "Loop");
+ LLVMBuildCondBr(g->builder, no_wrap_bit, no_wrap_block, yes_wrap_block);
+
+ LLVMPositionBuilderAtEnd(g->builder, no_wrap_block);
+ LLVMValueRef usize_zero = LLVMConstNull(g->builtin_types.entry_usize->type_ref);
+ LLVMBuildStore(g->builder, usize_zero, frame_index_ptr);
+ LLVMBuildStore(g->builder, src_index_val, frames_left_ptr);
+ LLVMValueRef frames_left_eq_zero_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, src_index_val, usize_zero, "");
+ LLVMBuildCondBr(g->builder, frames_left_eq_zero_bit, return_block, loop_block);
+
+ LLVMPositionBuilderAtEnd(g->builder, yes_wrap_block);
+ LLVMValueRef usize_one = LLVMConstInt(g->builtin_types.entry_usize->type_ref, 1, false);
+ LLVMValueRef plus_one = LLVMBuildNUWAdd(g->builder, src_index_val, usize_one, "");
+ LLVMValueRef mod_len = LLVMBuildURem(g->builder, plus_one, src_len_val, "");
+ LLVMBuildStore(g->builder, mod_len, frame_index_ptr);
+ LLVMBuildStore(g->builder, src_len_val, frames_left_ptr);
+ LLVMBuildBr(g->builder, loop_block);
+
+ LLVMPositionBuilderAtEnd(g->builder, loop_block);
+ LLVMValueRef ptr_index = LLVMBuildLoad(g->builder, frame_index_ptr, "");
+ LLVMValueRef addr_ptr = LLVMBuildInBoundsGEP(g->builder, src_ptr_val, &ptr_index, 1, "");
+ LLVMValueRef this_addr_val = LLVMBuildLoad(g->builder, addr_ptr, "");
+ LLVMValueRef args[] = {dest_stack_trace_ptr, this_addr_val};
+ ZigLLVMBuildCall(g->builder, add_error_return_trace_addr_fn_val, args, 2, get_llvm_cc(g, CallingConventionUnspecified), ZigLLVM_FnInlineAlways, "");
+ LLVMValueRef prev_frames_left = LLVMBuildLoad(g->builder, frames_left_ptr, "");
+ LLVMValueRef new_frames_left = LLVMBuildNUWSub(g->builder, prev_frames_left, usize_one, "");
+ LLVMValueRef done_bit = LLVMBuildICmp(g->builder, LLVMIntEQ, new_frames_left, usize_zero, "");
+ LLVMBasicBlockRef continue_block = LLVMAppendBasicBlock(fn_val, "Continue");
+ LLVMBuildCondBr(g->builder, done_bit, return_block, continue_block);
+
+ LLVMPositionBuilderAtEnd(g->builder, return_block);
+ LLVMBuildRetVoid(g->builder);
+
+ LLVMPositionBuilderAtEnd(g->builder, continue_block);
+ LLVMBuildStore(g->builder, new_frames_left, frames_left_ptr);
+ LLVMValueRef prev_index = LLVMBuildLoad(g->builder, frame_index_ptr, "");
+ LLVMValueRef index_plus_one = LLVMBuildNUWAdd(g->builder, prev_index, usize_one, "");
+ LLVMValueRef index_mod_len = LLVMBuildURem(g->builder, index_plus_one, src_len_val, "");
+ LLVMBuildStore(g->builder, index_mod_len, frame_index_ptr);
+ LLVMBuildBr(g->builder, loop_block);
+
+ LLVMPositionBuilderAtEnd(g->builder, prev_block);
+ LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
+
+ g->merge_err_ret_traces_fn_val = fn_val;
+ return fn_val;
+
+}
+
+static LLVMValueRef get_return_err_fn(CodeGen *g) {
+ if (g->return_err_fn != nullptr)
+ return g->return_err_fn;
+
+ assert(g->err_tag_type != nullptr);
+
+ LLVMTypeRef arg_types[] = {
+ // error return trace pointer
+ get_ptr_to_stack_trace_type(g)->type_ref,
+ };
+ LLVMTypeRef fn_type_ref = LLVMFunctionType(LLVMVoidType(), arg_types, 1, false);
+
+ Buf *fn_name = get_mangled_name(g, buf_create_from_str("__zig_return_error"), false);
+ LLVMValueRef fn_val = LLVMAddFunction(g->module, buf_ptr(fn_name), fn_type_ref);
+ addLLVMFnAttr(fn_val, "noinline"); // so that we can look at return address
+ addLLVMFnAttr(fn_val, "cold");
+ LLVMSetLinkage(fn_val, LLVMInternalLinkage);
+ LLVMSetFunctionCallConv(fn_val, get_llvm_cc(g, CallingConventionUnspecified));
+ addLLVMFnAttr(fn_val, "nounwind");
+ add_uwtable_attr(g, fn_val);
+ addLLVMArgAttr(fn_val, (unsigned)0, "nonnull");
+ if (g->build_mode == BuildModeDebug) {
+ ZigLLVMAddFunctionAttr(fn_val, "no-frame-pointer-elim", "true");
+ ZigLLVMAddFunctionAttr(fn_val, "no-frame-pointer-elim-non-leaf", nullptr);
+ }
+
+ // this is above the ZigLLVMClearCurrentDebugLocation
+ LLVMValueRef add_error_return_trace_addr_fn_val = get_add_error_return_trace_addr_fn(g);
+
+ LLVMBasicBlockRef entry_block = LLVMAppendBasicBlock(fn_val, "Entry");
+ LLVMBasicBlockRef prev_block = LLVMGetInsertBlock(g->builder);
+ LLVMValueRef prev_debug_location = LLVMGetCurrentDebugLocation(g->builder);
+ LLVMPositionBuilderAtEnd(g->builder, entry_block);
+ ZigLLVMClearCurrentDebugLocation(g->builder);
+
+ LLVMValueRef err_ret_trace_ptr = LLVMGetParam(fn_val, 0);
+
+ LLVMTypeRef usize_type_ref = g->builtin_types.entry_usize->type_ref;
+ LLVMValueRef zero = LLVMConstNull(g->builtin_types.entry_i32->type_ref);
+ LLVMValueRef return_address_ptr = LLVMBuildCall(g->builder, get_return_address_fn_val(g), &zero, 1, "");
+ LLVMValueRef return_address = LLVMBuildPtrToInt(g->builder, return_address_ptr, usize_type_ref, "");
+
+ LLVMValueRef args[] = { err_ret_trace_ptr, return_address };
+ ZigLLVMBuildCall(g->builder, add_error_return_trace_addr_fn_val, args, 2, get_llvm_cc(g, CallingConventionUnspecified), ZigLLVM_FnInlineAlways, "");
+ LLVMBuildRetVoid(g->builder);
+
+ LLVMPositionBuilderAtEnd(g->builder, prev_block);
+ LLVMSetCurrentDebugLocation(g->builder, prev_debug_location);
+
g->return_err_fn = fn_val;
return fn_val;
}
@@ -1318,9 +1494,34 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
return fn_val;
}
-static void gen_safety_crash_for_err(CodeGen *g, LLVMValueRef err_val) {
+static bool is_coro_prelude_scope(Scope *scope) {
+ while (scope != nullptr) {
+ if (scope->id == ScopeIdCoroPrelude) {
+ return true;
+ } else if (scope->id == ScopeIdFnDef) {
+ break;
+ }
+ scope = scope->parent;
+ }
+ return false;
+}
+
+static LLVMValueRef get_cur_err_ret_trace_val(CodeGen *g, Scope *scope) {
+ if (!g->have_err_ret_tracing) {
+ return nullptr;
+ }
+ if (g->cur_fn->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync) {
+ return is_coro_prelude_scope(scope) ? g->cur_err_ret_trace_val_arg : g->cur_err_ret_trace_val_stack;
+ }
+ if (g->cur_err_ret_trace_val_stack != nullptr) {
+ return g->cur_err_ret_trace_val_stack;
+ }
+ return g->cur_err_ret_trace_val_arg;
+}
+
+static void gen_safety_crash_for_err(CodeGen *g, LLVMValueRef err_val, Scope *scope) {
LLVMValueRef safety_crash_err_fn = get_safety_crash_err_fn(g);
- LLVMValueRef err_ret_trace_val = g->cur_err_ret_trace_val;
+ LLVMValueRef err_ret_trace_val = get_cur_err_ret_trace_val(g, scope);
if (err_ret_trace_val == nullptr) {
TypeTableEntry *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);
err_ret_trace_val = LLVMConstNull(ptr_to_stack_trace_type->type_ref);
@@ -1607,32 +1808,24 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) {
return instruction->llvm_value;
}
+static LLVMValueRef ir_render_save_err_ret_addr(CodeGen *g, IrExecutable *executable,
+ IrInstructionSaveErrRetAddr *save_err_ret_addr_instruction)
+{
+ assert(g->have_err_ret_tracing);
+
+ LLVMValueRef return_err_fn = get_return_err_fn(g);
+ LLVMValueRef args[] = {
+ get_cur_err_ret_trace_val(g, save_err_ret_addr_instruction->base.scope),
+ };
+ LLVMValueRef call_instruction = ZigLLVMBuildCall(g->builder, return_err_fn, args, 1,
+ get_llvm_cc(g, CallingConventionUnspecified), ZigLLVM_FnInlineAuto, "");
+ return call_instruction;
+}
+
static LLVMValueRef ir_render_return(CodeGen *g, IrExecutable *executable, IrInstructionReturn *return_instruction) {
LLVMValueRef value = ir_llvm_value(g, return_instruction->value);
TypeTableEntry *return_type = return_instruction->value->value.type;
- if (g->have_err_ret_tracing) {
- bool is_err_return = false;
- if (return_type->id == TypeTableEntryIdErrorUnion) {
- if (return_instruction->value->value.special == ConstValSpecialStatic) {
- is_err_return = return_instruction->value->value.data.x_err_union.err != nullptr;
- } else if (return_instruction->value->value.special == ConstValSpecialRuntime) {
- is_err_return = return_instruction->value->value.data.rh_error_union == RuntimeHintErrorUnionError;
- // TODO: emit a branch to check if the return value is an error
- }
- } else if (return_type->id == TypeTableEntryIdErrorSet) {
- is_err_return = true;
- }
- if (is_err_return) {
- LLVMValueRef return_err_fn = get_return_err_fn(g);
- LLVMValueRef args[] = {
- g->cur_err_ret_trace_val,
- };
- LLVMValueRef call_instruction = ZigLLVMBuildCall(g->builder, return_err_fn, args, 1,
- get_llvm_cc(g, CallingConventionUnspecified), ZigLLVM_FnInlineAuto, "");
- LLVMSetTailCall(call_instruction, true);
- }
- }
if (handle_is_ptr(return_type)) {
if (calling_convention_does_first_arg_return(g->cur_fn->type_entry->data.fn.fn_type_id.cc)) {
assert(g->cur_ret_ptr);
@@ -2732,7 +2925,7 @@ static LLVMValueRef ir_render_call(CodeGen *g, IrExecutable *executable, IrInstr
gen_param_index += 1;
}
if (prefix_arg_err_ret_stack) {
- gen_param_values[gen_param_index] = g->cur_err_ret_trace_val;
+ gen_param_values[gen_param_index] = get_cur_err_ret_trace_val(g, instruction->base.scope);
gen_param_index += 1;
}
if (instruction->is_async) {
@@ -3299,11 +3492,12 @@ static LLVMValueRef ir_render_align_cast(CodeGen *g, IrExecutable *executable, I
static LLVMValueRef ir_render_error_return_trace(CodeGen *g, IrExecutable *executable,
IrInstructionErrorReturnTrace *instruction)
{
- if (g->cur_err_ret_trace_val == nullptr) {
+ LLVMValueRef cur_err_ret_trace_val = get_cur_err_ret_trace_val(g, instruction->base.scope);
+ if (cur_err_ret_trace_val == nullptr) {
TypeTableEntry *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(g);
return LLVMConstNull(ptr_to_stack_trace_type->type_ref);
}
- return g->cur_err_ret_trace_val;
+ return cur_err_ret_trace_val;
}
static LLVMValueRef ir_render_cancel(CodeGen *g, IrExecutable *executable, IrInstructionCancel *instruction) {
@@ -3733,7 +3927,7 @@ static LLVMValueRef ir_render_unwrap_err_payload(CodeGen *g, IrExecutable *execu
LLVMBuildCondBr(g->builder, cond_val, ok_block, err_block);
LLVMPositionBuilderAtEnd(g->builder, err_block);
- gen_safety_crash_for_err(g, err_val);
+ gen_safety_crash_for_err(g, err_val, instruction->base.scope);
LLVMPositionBuilderAtEnd(g->builder, ok_block);
}
@@ -3925,7 +4119,7 @@ static LLVMValueRef ir_render_container_init_list(CodeGen *g, IrExecutable *exec
}
static LLVMValueRef ir_render_panic(CodeGen *g, IrExecutable *executable, IrInstructionPanic *instruction) {
- gen_panic(g, ir_llvm_value(g, instruction->msg), g->cur_err_ret_trace_val);
+ gen_panic(g, ir_llvm_value(g, instruction->msg), get_cur_err_ret_trace_val(g, instruction->base.scope));
return nullptr;
}
@@ -4187,6 +4381,27 @@ static LLVMValueRef ir_render_atomic_rmw(CodeGen *g, IrExecutable *executable,
return LLVMBuildIntToPtr(g->builder, uncasted_result, operand_type->type_ref, "");
}
+static LLVMValueRef ir_render_merge_err_ret_traces(CodeGen *g, IrExecutable *executable,
+ IrInstructionMergeErrRetTraces *instruction)
+{
+ assert(g->have_err_ret_tracing);
+
+ LLVMValueRef src_trace_ptr = ir_llvm_value(g, instruction->src_err_ret_trace_ptr);
+ LLVMValueRef dest_trace_ptr = ir_llvm_value(g, instruction->dest_err_ret_trace_ptr);
+
+ LLVMValueRef args[] = { dest_trace_ptr, src_trace_ptr };
+ ZigLLVMBuildCall(g->builder, get_merge_err_ret_traces_fn_val(g), args, 2, get_llvm_cc(g, CallingConventionUnspecified), ZigLLVM_FnInlineAuto, "");
+ return nullptr;
+}
+
+static LLVMValueRef ir_render_mark_err_ret_trace_ptr(CodeGen *g, IrExecutable *executable,
+ IrInstructionMarkErrRetTracePtr *instruction)
+{
+ assert(g->have_err_ret_tracing);
+ g->cur_err_ret_trace_val_stack = ir_llvm_value(g, instruction->err_ret_trace_ptr);
+ return nullptr;
+}
+
static void set_debug_location(CodeGen *g, IrInstruction *instruction) {
AstNode *source_node = instruction->source_node;
Scope *scope = instruction->scope;
@@ -4212,6 +4427,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
case IrInstructionIdSetRuntimeSafety:
case IrInstructionIdSetFloatMode:
case IrInstructionIdArrayType:
+ case IrInstructionIdPromiseType:
case IrInstructionIdSliceType:
case IrInstructionIdSizeOf:
case IrInstructionIdSwitchTarget:
@@ -4252,6 +4468,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
case IrInstructionIdErrorUnion:
case IrInstructionIdPromiseResultType:
case IrInstructionIdAwaitBookkeeping:
+ case IrInstructionIdAddImplicitReturnType:
zig_unreachable();
case IrInstructionIdReturn:
@@ -4400,6 +4617,12 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
return ir_render_coro_alloc_helper(g, executable, (IrInstructionCoroAllocHelper *)instruction);
case IrInstructionIdAtomicRmw:
return ir_render_atomic_rmw(g, executable, (IrInstructionAtomicRmw *)instruction);
+ case IrInstructionIdSaveErrRetAddr:
+ return ir_render_save_err_ret_addr(g, executable, (IrInstructionSaveErrRetAddr *)instruction);
+ case IrInstructionIdMergeErrRetTraces:
+ return ir_render_merge_err_ret_traces(g, executable, (IrInstructionMergeErrRetTraces *)instruction);
+ case IrInstructionIdMarkErrRetTracePtr:
+ return ir_render_mark_err_ret_trace_ptr(g, executable, (IrInstructionMarkErrRetTracePtr *)instruction);
}
zig_unreachable();
}
@@ -5282,39 +5505,23 @@ static void do_code_gen(CodeGen *g) {
clear_debug_source_node(g);
uint32_t err_ret_trace_arg_index = get_err_ret_trace_arg_index(g, fn_table_entry);
- if (err_ret_trace_arg_index != UINT32_MAX) {
- g->cur_err_ret_trace_val = LLVMGetParam(fn, err_ret_trace_arg_index);
- } else if (g->have_err_ret_tracing && fn_table_entry->calls_or_awaits_errorable_fn) {
- // TODO call graph analysis to find out what this number needs to be for every function
- static const size_t stack_trace_ptr_count = 30;
-
- TypeTableEntry *usize = g->builtin_types.entry_usize;
- TypeTableEntry *array_type = get_array_type(g, usize, stack_trace_ptr_count);
- LLVMValueRef err_ret_array_val = build_alloca(g, array_type, "error_return_trace_addresses",
- get_abi_alignment(g, array_type));
- g->cur_err_ret_trace_val = build_alloca(g, g->stack_trace_type, "error_return_trace", get_abi_alignment(g, g->stack_trace_type));
- size_t index_field_index = g->stack_trace_type->data.structure.fields[0].gen_index;
- LLVMValueRef index_field_ptr = LLVMBuildStructGEP(g->builder, g->cur_err_ret_trace_val, (unsigned)index_field_index, "");
- gen_store_untyped(g, LLVMConstNull(usize->type_ref), index_field_ptr, 0, false);
-
- size_t addresses_field_index = g->stack_trace_type->data.structure.fields[1].gen_index;
- LLVMValueRef addresses_field_ptr = LLVMBuildStructGEP(g->builder, g->cur_err_ret_trace_val, (unsigned)addresses_field_index, "");
-
- TypeTableEntry *slice_type = g->stack_trace_type->data.structure.fields[1].type_entry;
- size_t ptr_field_index = slice_type->data.structure.fields[slice_ptr_index].gen_index;
- LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, addresses_field_ptr, (unsigned)ptr_field_index, "");
- LLVMValueRef zero = LLVMConstNull(usize->type_ref);
- LLVMValueRef indices[] = {zero, zero};
- LLVMValueRef err_ret_array_val_elem0_ptr = LLVMBuildInBoundsGEP(g->builder, err_ret_array_val,
- indices, 2, "");
- gen_store(g, err_ret_array_val_elem0_ptr, ptr_field_ptr,
- get_pointer_to_type(g, get_pointer_to_type(g, usize, false), false));
+ bool have_err_ret_trace_arg = err_ret_trace_arg_index != UINT32_MAX;
+ if (have_err_ret_trace_arg) {
+ g->cur_err_ret_trace_val_arg = LLVMGetParam(fn, err_ret_trace_arg_index);
+ } else {
+ g->cur_err_ret_trace_val_arg = nullptr;
+ }
- size_t len_field_index = slice_type->data.structure.fields[slice_len_index].gen_index;
- LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, addresses_field_ptr, (unsigned)len_field_index, "");
- gen_store(g, LLVMConstInt(usize->type_ref, stack_trace_ptr_count, false), len_field_ptr, get_pointer_to_type(g, usize, false));
+ // error return tracing setup
+ bool is_async = fn_table_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync;
+ bool have_err_ret_trace_stack = g->have_err_ret_tracing && fn_table_entry->calls_or_awaits_errorable_fn && !is_async && !have_err_ret_trace_arg;
+ LLVMValueRef err_ret_array_val = nullptr;
+ if (have_err_ret_trace_stack) {
+ TypeTableEntry *array_type = get_array_type(g, g->builtin_types.entry_usize, stack_trace_ptr_count);
+ err_ret_array_val = build_alloca(g, array_type, "error_return_trace_addresses", get_abi_alignment(g, array_type));
+ g->cur_err_ret_trace_val_stack = build_alloca(g, g->stack_trace_type, "error_return_trace", get_abi_alignment(g, g->stack_trace_type));
} else {
- g->cur_err_ret_trace_val = nullptr;
+ g->cur_err_ret_trace_val_stack = nullptr;
}
// allocate temporary stack data
@@ -5407,6 +5614,31 @@ static void do_code_gen(CodeGen *g) {
}
}
+ // finishing error return trace setup. we have to do this after all the allocas.
+ if (have_err_ret_trace_stack) {
+ TypeTableEntry *usize = g->builtin_types.entry_usize;
+ size_t index_field_index = g->stack_trace_type->data.structure.fields[0].gen_index;
+ LLVMValueRef index_field_ptr = LLVMBuildStructGEP(g->builder, g->cur_err_ret_trace_val_stack, (unsigned)index_field_index, "");
+ gen_store_untyped(g, LLVMConstNull(usize->type_ref), index_field_ptr, 0, false);
+
+ size_t addresses_field_index = g->stack_trace_type->data.structure.fields[1].gen_index;
+ LLVMValueRef addresses_field_ptr = LLVMBuildStructGEP(g->builder, g->cur_err_ret_trace_val_stack, (unsigned)addresses_field_index, "");
+
+ TypeTableEntry *slice_type = g->stack_trace_type->data.structure.fields[1].type_entry;
+ size_t ptr_field_index = slice_type->data.structure.fields[slice_ptr_index].gen_index;
+ LLVMValueRef ptr_field_ptr = LLVMBuildStructGEP(g->builder, addresses_field_ptr, (unsigned)ptr_field_index, "");
+ LLVMValueRef zero = LLVMConstNull(usize->type_ref);
+ LLVMValueRef indices[] = {zero, zero};
+ LLVMValueRef err_ret_array_val_elem0_ptr = LLVMBuildInBoundsGEP(g->builder, err_ret_array_val,
+ indices, 2, "");
+ TypeTableEntry *ptr_ptr_usize_type = get_pointer_to_type(g, get_pointer_to_type(g, usize, false), false);
+ gen_store(g, err_ret_array_val_elem0_ptr, ptr_field_ptr, ptr_ptr_usize_type);
+
+ size_t len_field_index = slice_type->data.structure.fields[slice_len_index].gen_index;
+ LLVMValueRef len_field_ptr = LLVMBuildStructGEP(g->builder, addresses_field_ptr, (unsigned)len_field_index, "");
+ gen_store(g, LLVMConstInt(usize->type_ref, stack_trace_ptr_count, false), len_field_ptr, get_pointer_to_type(g, usize, false));
+ }
+
FnTypeId *fn_type_id = &fn_table_entry->type_entry->data.fn.fn_type_id;
// create debug variable declarations for parameters
@@ -5914,6 +6146,8 @@ static void define_builtin_compile_vars(CodeGen *g) {
os_path_join(g->cache_dir, buf_create_from_str(builtin_zig_basename), builtin_zig_path);
Buf *contents = buf_alloc();
+ // Modifications to this struct must be coordinated with code that does anything with
+ // g->stack_trace_type. There are hard-coded references to the field indexes.
buf_append_str(contents,
"pub const StackTrace = struct {\n"
" index: usize,\n"
@@ -6178,7 +6412,9 @@ static void init(CodeGen *g) {
g->builder = LLVMCreateBuilder();
g->dbuilder = ZigLLVMCreateDIBuilder(g->module, true);
- Buf *producer = buf_sprintf("zig %s", ZIG_VERSION_STRING);
+ // Don't use ZIG_VERSION_STRING here, llvm misparses it when it includes
+ // the git revision.
+ Buf *producer = buf_sprintf("zig %d.%d.%d", ZIG_VERSION_MAJOR, ZIG_VERSION_MINOR, ZIG_VERSION_PATCH);
const char *flags = "";
unsigned runtime_version = 0;
ZigLLVMDIFile *compile_unit_file = ZigLLVMCreateFile(g->dbuilder, buf_ptr(g->root_out_name),
@@ -6257,7 +6493,7 @@ static ImportTableEntry *add_special_code(CodeGen *g, PackageTableEntry *package
zig_panic("unable to open '%s': %s", buf_ptr(&path_to_code_src), err_str(err));
}
Buf *import_code = buf_alloc();
- if ((err = os_fetch_file_path(abs_full_path, import_code))) {
+ if ((err = os_fetch_file_path(abs_full_path, import_code, false))) {
zig_panic("unable to open '%s': %s", buf_ptr(&path_to_code_src), err_str(err));
}
@@ -6345,7 +6581,7 @@ static void gen_root_source(CodeGen *g) {
}
Buf *source_code = buf_alloc();
- if ((err = os_fetch_file_path(rel_full_path, source_code))) {
+ if ((err = os_fetch_file_path(rel_full_path, source_code, true))) {
zig_panic("unable to open '%s': %s", buf_ptr(rel_full_path), err_str(err));
}
@@ -6410,7 +6646,7 @@ static void gen_global_asm(CodeGen *g) {
int err;
for (size_t i = 0; i < g->assembly_files.length; i += 1) {
Buf *asm_file = g->assembly_files.at(i);
- if ((err = os_fetch_file_path(asm_file, &contents))) {
+ if ((err = os_fetch_file_path(asm_file, &contents, false))) {
zig_panic("Unable to read %s: %s", buf_ptr(asm_file), err_str(err));
}
buf_append_buf(&g->global_asm, &contents);
@@ -6592,6 +6828,7 @@ static void get_c_type(CodeGen *g, GenH *gen_h, TypeTableEntry *type_entry, Buf
}
}
case TypeTableEntryIdStruct:
+ case TypeTableEntryIdOpaque:
{
buf_init_from_str(out_buf, "struct ");
buf_append_buf(out_buf, &type_entry->name);
@@ -6609,11 +6846,6 @@ static void get_c_type(CodeGen *g, GenH *gen_h, TypeTableEntry *type_entry, Buf
buf_append_buf(out_buf, &type_entry->name);
return;
}
- case TypeTableEntryIdOpaque:
- {
- buf_init_from_buf(out_buf, &type_entry->name);
- return;
- }
case TypeTableEntryIdArray:
{
TypeTableEntryArray *array_data = &type_entry->data.array;
diff --git a/src/config.h.in b/src/config.h.in
index 1fcc3fe12c..16896273b3 100644
--- a/src/config.h.in
+++ b/src/config.h.in
@@ -13,14 +13,6 @@
#define ZIG_VERSION_PATCH @ZIG_VERSION_PATCH@
#define ZIG_VERSION_STRING "@ZIG_VERSION@"
-#define ZIG_INSTALL_PREFIX "@CMAKE_INSTALL_PREFIX@"
-#define ZIG_LIBC_INCLUDE_DIR "@ZIG_LIBC_INCLUDE_DIR_ESCAPED@"
-#define ZIG_LIBC_LIB_DIR "@ZIG_LIBC_LIB_DIR_ESCAPED@"
-#define ZIG_LIBC_STATIC_LIB_DIR "@ZIG_LIBC_STATIC_LIB_DIR_ESCAPED@"
-#define ZIG_DYNAMIC_LINKER "@ZIG_DYNAMIC_LINKER@"
-
-#cmakedefine ZIG_EACH_LIB_RPATH
-
// Only used for running tests before installing.
#define ZIG_TEST_DIR "@CMAKE_SOURCE_DIR@/test"
diff --git a/src/ir.cpp b/src/ir.cpp
index db9a2b24c3..0b072cc696 100644
--- a/src/ir.cpp
+++ b/src/ir.cpp
@@ -34,7 +34,7 @@ struct IrAnalyze {
size_t old_bb_index;
size_t instruction_index;
TypeTableEntry *explicit_return_type;
- ZigList<IrInstruction *> implicit_return_type_list;
+ ZigList<IrInstruction *> src_implicit_return_type_list;
IrBasicBlock *const_predecessor_bb;
};
@@ -349,6 +349,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionArrayType *) {
return IrInstructionIdArrayType;
}
+static constexpr IrInstructionId ir_instruction_id(IrInstructionPromiseType *) {
+ return IrInstructionIdPromiseType;
+}
+
static constexpr IrInstructionId ir_instruction_id(IrInstructionSliceType *) {
return IrInstructionIdSliceType;
}
@@ -713,6 +717,22 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionAwaitBookkeeping
return IrInstructionIdAwaitBookkeeping;
}
+static constexpr IrInstructionId ir_instruction_id(IrInstructionSaveErrRetAddr *) {
+ return IrInstructionIdSaveErrRetAddr;
+}
+
+static constexpr IrInstructionId ir_instruction_id(IrInstructionAddImplicitReturnType *) {
+ return IrInstructionIdAddImplicitReturnType;
+}
+
+static constexpr IrInstructionId ir_instruction_id(IrInstructionMergeErrRetTraces *) {
+ return IrInstructionIdMergeErrRetTraces;
+}
+
+static constexpr IrInstructionId ir_instruction_id(IrInstructionMarkErrRetTracePtr *) {
+ return IrInstructionIdMarkErrRetTracePtr;
+}
+
template<typename T>
static T *ir_create_instruction(IrBuilder *irb, Scope *scope, AstNode *source_node) {
T *special_instruction = allocate<T>(1);
@@ -944,25 +964,6 @@ static IrInstruction *ir_build_const_c_str_lit(IrBuilder *irb, Scope *scope, Ast
return &const_instruction->base;
}
-static IrInstruction *ir_build_const_promise_init(IrBuilder *irb, Scope *scope, AstNode *source_node,
- TypeTableEntry *return_type)
-{
- TypeTableEntry *struct_type = get_promise_frame_type(irb->codegen, return_type);
-
- IrInstructionConst *const_instruction = ir_build_instruction<IrInstructionConst>(irb, scope, source_node);
- const_instruction->base.value.type = struct_type;
- const_instruction->base.value.special = ConstValSpecialStatic;
- const_instruction->base.value.data.x_struct.fields = allocate<ConstExprValue>(struct_type->data.structure.src_field_count);
- const_instruction->base.value.data.x_struct.fields[0].type = struct_type->data.structure.fields[0].type_entry;
- const_instruction->base.value.data.x_struct.fields[0].special = ConstValSpecialStatic;
- const_instruction->base.value.data.x_struct.fields[0].data.x_maybe = nullptr;
- const_instruction->base.value.data.x_struct.fields[1].type = return_type;
- const_instruction->base.value.data.x_struct.fields[1].special = ConstValSpecialUndef;
- const_instruction->base.value.data.x_struct.fields[2].type = struct_type->data.structure.fields[2].type_entry;
- const_instruction->base.value.data.x_struct.fields[2].special = ConstValSpecialUndef;
- return &const_instruction->base;
-}
-
static IrInstruction *ir_build_bin_op(IrBuilder *irb, Scope *scope, AstNode *source_node, IrBinOp op_id,
IrInstruction *op1, IrInstruction *op2, bool safety_check_on)
{
@@ -1461,6 +1462,17 @@ static IrInstruction *ir_build_array_type(IrBuilder *irb, Scope *scope, AstNode
return &instruction->base;
}
+static IrInstruction *ir_build_promise_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
+ IrInstruction *payload_type)
+{
+ IrInstructionPromiseType *instruction = ir_build_instruction<IrInstructionPromiseType>(irb, scope, source_node);
+ instruction->payload_type = payload_type;
+
+ if (payload_type != nullptr) ir_ref_instruction(payload_type, irb->current_basic_block);
+
+ return &instruction->base;
+}
+
static IrInstruction *ir_build_slice_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
IrInstruction *child_type, bool is_const, bool is_volatile, IrInstruction *align_value)
{
@@ -2141,12 +2153,14 @@ static IrInstruction *ir_build_unwrap_err_payload_from(IrBuilder *irb, IrInstruc
}
static IrInstruction *ir_build_fn_proto(IrBuilder *irb, Scope *scope, AstNode *source_node,
- IrInstruction **param_types, IrInstruction *align_value, IrInstruction *return_type, bool is_var_args)
+ IrInstruction **param_types, IrInstruction *align_value, IrInstruction *return_type,
+ IrInstruction *async_allocator_type_value, bool is_var_args)
{
IrInstructionFnProto *instruction = ir_build_instruction<IrInstructionFnProto>(irb, scope, source_node);
instruction->param_types = param_types;
instruction->align_value = align_value;
instruction->return_type = return_type;
+ instruction->async_allocator_type_value = async_allocator_type_value;
instruction->is_var_args = is_var_args;
assert(source_node->type == NodeTypeFnProto);
@@ -2156,6 +2170,7 @@ static IrInstruction *ir_build_fn_proto(IrBuilder *irb, Scope *scope, AstNode *s
if (param_types[i] != nullptr) ir_ref_instruction(param_types[i], irb->current_basic_block);
}
if (align_value != nullptr) ir_ref_instruction(align_value, irb->current_basic_block);
+ if (async_allocator_type_value != nullptr) ir_ref_instruction(async_allocator_type_value, irb->current_basic_block);
ir_ref_instruction(return_type, irb->current_basic_block);
return &instruction->base;
@@ -2469,8 +2484,9 @@ static IrInstruction *ir_build_arg_type(IrBuilder *irb, Scope *scope, AstNode *s
return &instruction->base;
}
-static IrInstruction *ir_build_error_return_trace(IrBuilder *irb, Scope *scope, AstNode *source_node) {
+static IrInstruction *ir_build_error_return_trace(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstructionErrorReturnTrace::Nullable nullable) {
IrInstructionErrorReturnTrace *instruction = ir_build_instruction<IrInstructionErrorReturnTrace>(irb, scope, source_node);
+ instruction->nullable = nullable;
return &instruction->base;
}
@@ -2675,6 +2691,46 @@ static IrInstruction *ir_build_await_bookkeeping(IrBuilder *irb, Scope *scope, A
return &instruction->base;
}
+static IrInstruction *ir_build_save_err_ret_addr(IrBuilder *irb, Scope *scope, AstNode *source_node) {
+ IrInstructionSaveErrRetAddr *instruction = ir_build_instruction<IrInstructionSaveErrRetAddr>(irb, scope, source_node);
+ return &instruction->base;
+}
+
+static IrInstruction *ir_build_add_implicit_return_type(IrBuilder *irb, Scope *scope, AstNode *source_node,
+ IrInstruction *value)
+{
+ IrInstructionAddImplicitReturnType *instruction = ir_build_instruction<IrInstructionAddImplicitReturnType>(irb, scope, source_node);
+ instruction->value = value;
+
+ ir_ref_instruction(value, irb->current_basic_block);
+
+ return &instruction->base;
+}
+
+static IrInstruction *ir_build_merge_err_ret_traces(IrBuilder *irb, Scope *scope, AstNode *source_node,
+ IrInstruction *coro_promise_ptr, IrInstruction *src_err_ret_trace_ptr, IrInstruction *dest_err_ret_trace_ptr)
+{
+ IrInstructionMergeErrRetTraces *instruction = ir_build_instruction<IrInstructionMergeErrRetTraces>(irb, scope, source_node);
+ instruction->coro_promise_ptr = coro_promise_ptr;
+ instruction->src_err_ret_trace_ptr = src_err_ret_trace_ptr;
+ instruction->dest_err_ret_trace_ptr = dest_err_ret_trace_ptr;
+
+ ir_ref_instruction(coro_promise_ptr, irb->current_basic_block);
+ ir_ref_instruction(src_err_ret_trace_ptr, irb->current_basic_block);
+ ir_ref_instruction(dest_err_ret_trace_ptr, irb->current_basic_block);
+
+ return &instruction->base;
+}
+
+static IrInstruction *ir_build_mark_err_ret_trace_ptr(IrBuilder *irb, Scope *scope, AstNode *source_node, IrInstruction *err_ret_trace_ptr) {
+ IrInstructionMarkErrRetTracePtr *instruction = ir_build_instruction<IrInstructionMarkErrRetTracePtr>(irb, scope, source_node);
+ instruction->err_ret_trace_ptr = err_ret_trace_ptr;
+
+ ir_ref_instruction(err_ret_trace_ptr, irb->current_basic_block);
+
+ return &instruction->base;
+}
+
static void ir_count_defers(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, size_t *results) {
results[ReturnKindUnconditional] = 0;
results[ReturnKindError] = 0;
@@ -2699,9 +2755,10 @@ static IrInstruction *ir_mark_gen(IrInstruction *instruction) {
static bool ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *outer_scope, bool gen_error_defers) {
Scope *scope = inner_scope;
+ bool is_noreturn = false;
while (scope != outer_scope) {
if (!scope)
- return false;
+ return is_noreturn;
if (scope->id == ScopeIdDefer) {
AstNode *defer_node = scope->source_node;
@@ -2714,14 +2771,18 @@ static bool ir_gen_defers_for_block(IrBuilder *irb, Scope *inner_scope, Scope *o
Scope *defer_expr_scope = defer_node->data.defer.expr_scope;
IrInstruction *defer_expr_value = ir_gen_node(irb, defer_expr_node, defer_expr_scope);
if (defer_expr_value != irb->codegen->invalid_instruction) {
- ir_mark_gen(ir_build_check_statement_is_void(irb, defer_expr_scope, defer_expr_node, defer_expr_value));
+ if (defer_expr_value->value.type != nullptr && defer_expr_value->value.type->id == TypeTableEntryIdUnreachable) {
+ is_noreturn = true;
+ } else {
+ ir_mark_gen(ir_build_check_statement_is_void(irb, defer_expr_scope, defer_expr_node, defer_expr_value));
+ }
}
}
}
scope = scope->parent;
}
- return true;
+ return is_noreturn;
}
static void ir_set_cursor_at_end(IrBuilder *irb, IrBasicBlock *basic_block) {
@@ -2747,16 +2808,18 @@ static ScopeDeferExpr *get_scope_defer_expr(Scope *scope) {
return nullptr;
}
+static bool exec_is_async(IrExecutable *exec) {
+ FnTableEntry *fn_entry = exec_fn_entry(exec);
+ return fn_entry != nullptr && fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync;
+}
+
static IrInstruction *ir_gen_async_return(IrBuilder *irb, Scope *scope, AstNode *node, IrInstruction *return_value,
bool is_generated_code)
{
- FnTableEntry *fn_entry = exec_fn_entry(irb->exec);
- bool is_async = fn_entry != nullptr && fn_entry->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync;
+ ir_mark_gen(ir_build_add_implicit_return_type(irb, scope, node, return_value));
+
+ bool is_async = exec_is_async(irb->exec);
if (!is_async) {
- //if (irb->codegen->have_err_ret_tracing) {
- // IrInstruction *stack_trace_ptr = ir_build_error_return_trace_nonnull(irb, scope, node);
- // ir_build_save_err_ret_addr(irb, scope, node, stack_trace_ptr);
- //}
IrInstruction *return_inst = ir_build_return(irb, scope, node, return_value);
return_inst->is_gen = is_generated_code;
return return_inst;
@@ -2778,22 +2841,6 @@ static IrInstruction *ir_gen_async_return(IrBuilder *irb, Scope *scope, AstNode
// the above blocks are rendered by ir_gen after the rest of codegen
}
-//static void ir_gen_save_err_ret_addr(IrBuilder *irb, Scope *scope, AstNode *node, bool is_async) {
-// if (!irb->codegen->have_err_ret_tracing)
-// return;
-//
-// if (is_async) {
-// IrInstruction *err_ret_addr_ptr = ir_build_load_ptr(irb, scope, node, irb->exec->coro_err_ret_addr_ptr);
-// IrInstruction *return_address_ptr = ir_build_return_address(irb, scope, node);
-// IrInstruction *return_address_usize = ir_build_ptr_to_int(irb, scope, node, return_address_ptr);
-// ir_build_store_ptr(irb, scope, node, err_ret_addr_ptr, return_address_usize);
-// return;
-// }
-//
-// IrInstruction *stack_trace_ptr = ir_build_error_return_trace_nonnull(irb, scope, node);
-// ir_build_save_err_ret_addr(irb, scope, node, stack_trace_ptr);
-//}
-
static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {
assert(node->type == NodeTypeReturnExpr);
@@ -2839,8 +2886,9 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,
IrInstruction *is_err = ir_build_test_err(irb, scope, node, return_value);
+ bool should_inline = ir_should_inline(irb->exec, scope);
IrInstruction *is_comptime;
- if (ir_should_inline(irb->exec, scope)) {
+ if (should_inline) {
is_comptime = ir_build_const_bool(irb, scope, node, true);
} else {
is_comptime = ir_build_test_comptime(irb, scope, node, is_err);
@@ -2853,7 +2901,9 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,
if (have_err_defers) {
ir_gen_defers_for_block(irb, scope, outer_scope, true);
}
- //ir_gen_save_err_ret_addr(irb, scope, node, is_async);
+ if (irb->codegen->have_err_ret_tracing && !should_inline) {
+ ir_build_save_err_ret_addr(irb, scope, node);
+ }
ir_build_br(irb, scope, node, ret_stmt_block, is_comptime);
ir_set_cursor_at_end_and_append_block(irb, ok_block);
@@ -2882,7 +2932,8 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,
IrBasicBlock *return_block = ir_create_basic_block(irb, scope, "ErrRetReturn");
IrBasicBlock *continue_block = ir_create_basic_block(irb, scope, "ErrRetContinue");
IrInstruction *is_comptime;
- if (ir_should_inline(irb->exec, scope)) {
+ bool should_inline = ir_should_inline(irb->exec, scope);
+ if (should_inline) {
is_comptime = ir_build_const_bool(irb, scope, node, true);
} else {
is_comptime = ir_build_test_comptime(irb, scope, node, is_err_val);
@@ -2890,9 +2941,13 @@ static IrInstruction *ir_gen_return(IrBuilder *irb, Scope *scope, AstNode *node,
ir_mark_gen(ir_build_cond_br(irb, scope, node, is_err_val, return_block, continue_block, is_comptime));
ir_set_cursor_at_end_and_append_block(irb, return_block);
- ir_gen_defers_for_block(irb, scope, outer_scope, true);
- IrInstruction *err_val = ir_build_unwrap_err_code(irb, scope, node, err_union_ptr);
- ir_gen_async_return(irb, scope, node, err_val, false);
+ if (!ir_gen_defers_for_block(irb, scope, outer_scope, true)) {
+ IrInstruction *err_val = ir_build_unwrap_err_code(irb, scope, node, err_union_ptr);
+ if (irb->codegen->have_err_ret_tracing && !should_inline) {
+ ir_build_save_err_ret_addr(irb, scope, node);
+ }
+ ir_gen_async_return(irb, scope, node, err_val, false);
+ }
ir_set_cursor_at_end_and_append_block(irb, continue_block);
IrInstruction *unwrapped_ptr = ir_build_unwrap_err_payload(irb, scope, node, err_union_ptr, false);
@@ -4185,7 +4240,7 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
}
case BuiltinFnIdErrorReturnTrace:
{
- return ir_build_error_return_trace(irb, scope, node);
+ return ir_build_error_return_trace(irb, scope, node, IrInstructionErrorReturnTrace::Null);
}
case BuiltinFnIdAtomicRmw:
{
@@ -5032,6 +5087,22 @@ static IrInstruction *ir_gen_array_type(IrBuilder *irb, Scope *scope, AstNode *n
}
}
+static IrInstruction *ir_gen_promise_type(IrBuilder *irb, Scope *scope, AstNode *node) {
+ assert(node->type == NodeTypePromiseType);
+
+ AstNode *payload_type_node = node->data.promise_type.payload_type;
+ IrInstruction *payload_type_value = nullptr;
+
+ if (payload_type_node != nullptr) {
+ payload_type_value = ir_gen_node(irb, payload_type_node, scope);
+ if (payload_type_value == irb->codegen->invalid_instruction)
+ return payload_type_value;
+
+ }
+
+ return ir_build_promise_type(irb, scope, node, payload_type_value);
+}
+
static IrInstruction *ir_gen_undefined_literal(IrBuilder *irb, Scope *scope, AstNode *node) {
assert(node->type == NodeTypeUndefinedLiteral);
return ir_build_const_undefined(irb, scope, node);
@@ -5630,7 +5701,7 @@ static IrInstruction *ir_gen_continue(IrBuilder *irb, Scope *continue_scope, Ast
IrBasicBlock *dest_block = loop_scope->continue_block;
ir_gen_defers_for_block(irb, continue_scope, dest_block->scope, false);
- return ir_build_br(irb, continue_scope, node, dest_block, is_comptime);
+ return ir_mark_gen(ir_build_br(irb, continue_scope, node, dest_block, is_comptime));
}
static IrInstruction *ir_gen_error_type(IrBuilder *irb, Scope *scope, AstNode *node) {
@@ -5989,7 +6060,15 @@ static IrInstruction *ir_gen_fn_proto(IrBuilder *irb, Scope *parent_scope, AstNo
return_type = nullptr;
}
- return ir_build_fn_proto(irb, parent_scope, node, param_types, align_value, return_type, is_var_args);
+ IrInstruction *async_allocator_type_value = nullptr;
+ if (node->data.fn_proto.async_allocator_type != nullptr) {
+ async_allocator_type_value = ir_gen_node(irb, node->data.fn_proto.async_allocator_type, parent_scope);
+ if (async_allocator_type_value == irb->codegen->invalid_instruction)
+ return irb->codegen->invalid_instruction;
+ }
+
+ return ir_build_fn_proto(irb, parent_scope, node, param_types, align_value, return_type,
+ async_allocator_type_value, is_var_args);
}
static IrInstruction *ir_gen_cancel(IrBuilder *irb, Scope *parent_scope, AstNode *node) {
@@ -6044,6 +6123,13 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *parent_scope, Ast
Buf *result_ptr_field_name = buf_create_from_str(RESULT_PTR_FIELD_NAME);
IrInstruction *result_ptr_field_ptr = ir_build_field_ptr(irb, parent_scope, node, coro_promise_ptr, result_ptr_field_name);
+ if (irb->codegen->have_err_ret_tracing) {
+ IrInstruction *err_ret_trace_ptr = ir_build_error_return_trace(irb, parent_scope, node, IrInstructionErrorReturnTrace::NonNull);
+ Buf *err_ret_trace_ptr_field_name = buf_create_from_str(ERR_RET_TRACE_PTR_FIELD_NAME);
+ IrInstruction *err_ret_trace_ptr_field_ptr = ir_build_field_ptr(irb, parent_scope, node, coro_promise_ptr, err_ret_trace_ptr_field_name);
+ ir_build_store_ptr(irb, parent_scope, node, err_ret_trace_ptr_field_ptr, err_ret_trace_ptr);
+ }
+
Buf *awaiter_handle_field_name = buf_create_from_str(AWAITER_HANDLE_FIELD_NAME);
IrInstruction *awaiter_field_ptr = ir_build_field_ptr(irb, parent_scope, node, coro_promise_ptr,
awaiter_handle_field_name);
@@ -6067,10 +6153,16 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *parent_scope, Ast
IrInstruction *is_non_null = ir_build_test_nonnull(irb, parent_scope, node, maybe_await_handle);
IrBasicBlock *yes_suspend_block = ir_create_basic_block(irb, parent_scope, "YesSuspend");
IrBasicBlock *no_suspend_block = ir_create_basic_block(irb, parent_scope, "NoSuspend");
- IrBasicBlock *merge_block = ir_create_basic_block(irb, parent_scope, "Merge");
+ IrBasicBlock *merge_block = ir_create_basic_block(irb, parent_scope, "MergeSuspend");
ir_build_cond_br(irb, parent_scope, node, is_non_null, no_suspend_block, yes_suspend_block, const_bool_false);
ir_set_cursor_at_end_and_append_block(irb, no_suspend_block);
+ if (irb->codegen->have_err_ret_tracing) {
+ Buf *err_ret_trace_field_name = buf_create_from_str(ERR_RET_TRACE_FIELD_NAME);
+ IrInstruction *src_err_ret_trace_ptr = ir_build_field_ptr(irb, parent_scope, node, coro_promise_ptr, err_ret_trace_field_name);
+ IrInstruction *dest_err_ret_trace_ptr = ir_build_error_return_trace(irb, parent_scope, node, IrInstructionErrorReturnTrace::NonNull);
+ ir_build_merge_err_ret_traces(irb, parent_scope, node, coro_promise_ptr, src_err_ret_trace_ptr, dest_err_ret_trace_ptr);
+ }
Buf *result_field_name = buf_create_from_str(RESULT_FIELD_NAME);
IrInstruction *promise_result_ptr = ir_build_field_ptr(irb, parent_scope, node, coro_promise_ptr, result_field_name);
IrInstruction *no_suspend_result = ir_build_load_ptr(irb, parent_scope, node, promise_result_ptr);
@@ -6092,7 +6184,7 @@ static IrInstruction *ir_gen_await_expr(IrBuilder *irb, Scope *parent_scope, Ast
ir_set_cursor_at_end_and_append_block(irb, cleanup_block);
ir_gen_defers_for_block(irb, parent_scope, outer_scope, true);
- ir_build_br(irb, parent_scope, node, irb->exec->coro_final_cleanup_block, const_bool_false);
+ ir_mark_gen(ir_build_br(irb, parent_scope, node, irb->exec->coro_final_cleanup_block, const_bool_false));
ir_set_cursor_at_end_and_append_block(irb, resume_block);
IrInstruction *yes_suspend_result = ir_build_load_ptr(irb, parent_scope, node, my_result_var_ptr);
@@ -6168,7 +6260,7 @@ static IrInstruction *ir_gen_suspend(IrBuilder *irb, Scope *parent_scope, AstNod
ir_set_cursor_at_end_and_append_block(irb, cleanup_block);
ir_gen_defers_for_block(irb, parent_scope, outer_scope, true);
- ir_build_br(irb, parent_scope, node, irb->exec->coro_final_cleanup_block, const_bool_false);
+ ir_mark_gen(ir_build_br(irb, parent_scope, node, irb->exec->coro_final_cleanup_block, const_bool_false));
ir_set_cursor_at_end_and_append_block(irb, resume_block);
return ir_build_const_void(irb, parent_scope, node);
@@ -6187,7 +6279,6 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
case NodeTypeSwitchRange:
case NodeTypeStructField:
case NodeTypeFnDef:
- case NodeTypeFnDecl:
case NodeTypeTestDecl:
zig_unreachable();
case NodeTypeBlock:
@@ -6232,6 +6323,8 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
return ir_lval_wrap(irb, scope, ir_gen_bool_literal(irb, scope, node), lval);
case NodeTypeArrayType:
return ir_lval_wrap(irb, scope, ir_gen_array_type(irb, scope, node), lval);
+ case NodeTypePromiseType:
+ return ir_lval_wrap(irb, scope, ir_gen_promise_type(irb, scope, node), lval);
case NodeTypeStringLiteral:
return ir_lval_wrap(irb, scope, ir_gen_string_literal(irb, scope, node), lval);
case NodeTypeUndefinedLiteral:
@@ -6324,63 +6417,92 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
IrInstruction *coro_id;
IrInstruction *u8_ptr_type;
IrInstruction *const_bool_false;
+ IrInstruction *coro_promise_ptr;
+ IrInstruction *err_ret_trace_ptr;
TypeTableEntry *return_type;
Buf *result_ptr_field_name;
VariableTableEntry *coro_size_var;
if (is_async) {
// create the coro promise
- const_bool_false = ir_build_const_bool(irb, scope, node, false);
- VariableTableEntry *promise_var = ir_create_var(irb, node, scope, nullptr, false, false, true, const_bool_false);
+ Scope *coro_scope = create_coro_prelude_scope(node, scope);
+ const_bool_false = ir_build_const_bool(irb, coro_scope, node, false);
+ VariableTableEntry *promise_var = ir_create_var(irb, node, coro_scope, nullptr, false, false, true, const_bool_false);
return_type = fn_entry->type_entry->data.fn.fn_type_id.return_type;
- IrInstruction *promise_init = ir_build_const_promise_init(irb, scope, node, return_type);
- ir_build_var_decl(irb, scope, node, promise_var, nullptr, nullptr, promise_init);
- IrInstruction *coro_promise_ptr = ir_build_var_ptr(irb, scope, node, promise_var, false, false);
-
- VariableTableEntry *await_handle_var = ir_create_var(irb, node, scope, nullptr, false, false, true, const_bool_false);
- IrInstruction *null_value = ir_build_const_null(irb, scope, node);
- IrInstruction *await_handle_type_val = ir_build_const_type(irb, scope, node,
+ IrInstruction *undef = ir_build_const_undefined(irb, coro_scope, node);
+ TypeTableEntry *coro_frame_type = get_promise_frame_type(irb->codegen, return_type);
+ IrInstruction *coro_frame_type_value = ir_build_const_type(irb, coro_scope, node, coro_frame_type);
+ // TODO mark this var decl as "no safety" e.g. disable initializing the undef value to 0xaa
+ ir_build_var_decl(irb, coro_scope, node, promise_var, coro_frame_type_value, nullptr, undef);
+ coro_promise_ptr = ir_build_var_ptr(irb, coro_scope, node, promise_var, false, false);
+
+ VariableTableEntry *await_handle_var = ir_create_var(irb, node, coro_scope, nullptr, false, false, true, const_bool_false);
+ IrInstruction *null_value = ir_build_const_null(irb, coro_scope, node);
+ IrInstruction *await_handle_type_val = ir_build_const_type(irb, coro_scope, node,
get_maybe_type(irb->codegen, irb->codegen->builtin_types.entry_promise));
- ir_build_var_decl(irb, scope, node, await_handle_var, await_handle_type_val, nullptr, null_value);
- irb->exec->await_handle_var_ptr = ir_build_var_ptr(irb, scope, node,
+ ir_build_var_decl(irb, coro_scope, node, await_handle_var, await_handle_type_val, nullptr, null_value);
+ irb->exec->await_handle_var_ptr = ir_build_var_ptr(irb, coro_scope, node,
await_handle_var, false, false);
- u8_ptr_type = ir_build_const_type(irb, scope, node,
+ u8_ptr_type = ir_build_const_type(irb, coro_scope, node,
get_pointer_to_type(irb->codegen, irb->codegen->builtin_types.entry_u8, false));
- IrInstruction *promise_as_u8_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, coro_promise_ptr);
- coro_id = ir_build_coro_id(irb, scope, node, promise_as_u8_ptr);
- coro_size_var = ir_create_var(irb, node, scope, nullptr, false, false, true, const_bool_false);
- IrInstruction *coro_size = ir_build_coro_size(irb, scope, node);
- ir_build_var_decl(irb, scope, node, coro_size_var, nullptr, nullptr, coro_size);
- IrInstruction *implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, scope, node,
+ IrInstruction *promise_as_u8_ptr = ir_build_ptr_cast(irb, coro_scope, node, u8_ptr_type, coro_promise_ptr);
+ coro_id = ir_build_coro_id(irb, coro_scope, node, promise_as_u8_ptr);
+ coro_size_var = ir_create_var(irb, node, coro_scope, nullptr, false, false, true, const_bool_false);
+ IrInstruction *coro_size = ir_build_coro_size(irb, coro_scope, node);
+ ir_build_var_decl(irb, coro_scope, node, coro_size_var, nullptr, nullptr, coro_size);
+ IrInstruction *implicit_allocator_ptr = ir_build_get_implicit_allocator(irb, coro_scope, node,
ImplicitAllocatorIdArg);
- irb->exec->coro_allocator_var = ir_create_var(irb, node, scope, nullptr, true, true, true, const_bool_false);
- ir_build_var_decl(irb, scope, node, irb->exec->coro_allocator_var, nullptr, nullptr, implicit_allocator_ptr);
+ irb->exec->coro_allocator_var = ir_create_var(irb, node, coro_scope, nullptr, true, true, true, const_bool_false);
+ ir_build_var_decl(irb, coro_scope, node, irb->exec->coro_allocator_var, nullptr, nullptr, implicit_allocator_ptr);
Buf *alloc_field_name = buf_create_from_str(ASYNC_ALLOC_FIELD_NAME);
- IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, scope, node, implicit_allocator_ptr, alloc_field_name);
- IrInstruction *alloc_fn = ir_build_load_ptr(irb, scope, node, alloc_fn_ptr);
- IrInstruction *maybe_coro_mem_ptr = ir_build_coro_alloc_helper(irb, scope, node, alloc_fn, coro_size);
- IrInstruction *alloc_result_is_ok = ir_build_test_nonnull(irb, scope, node, maybe_coro_mem_ptr);
- IrBasicBlock *alloc_err_block = ir_create_basic_block(irb, scope, "AllocError");
- IrBasicBlock *alloc_ok_block = ir_create_basic_block(irb, scope, "AllocOk");
- ir_build_cond_br(irb, scope, node, alloc_result_is_ok, alloc_ok_block, alloc_err_block, const_bool_false);
+ IrInstruction *alloc_fn_ptr = ir_build_field_ptr(irb, coro_scope, node, implicit_allocator_ptr, alloc_field_name);
+ IrInstruction *alloc_fn = ir_build_load_ptr(irb, coro_scope, node, alloc_fn_ptr);
+ IrInstruction *maybe_coro_mem_ptr = ir_build_coro_alloc_helper(irb, coro_scope, node, alloc_fn, coro_size);
+ IrInstruction *alloc_result_is_ok = ir_build_test_nonnull(irb, coro_scope, node, maybe_coro_mem_ptr);
+ IrBasicBlock *alloc_err_block = ir_create_basic_block(irb, coro_scope, "AllocError");
+ IrBasicBlock *alloc_ok_block = ir_create_basic_block(irb, coro_scope, "AllocOk");
+ ir_build_cond_br(irb, coro_scope, node, alloc_result_is_ok, alloc_ok_block, alloc_err_block, const_bool_false);
ir_set_cursor_at_end_and_append_block(irb, alloc_err_block);
- IrInstruction *undef = ir_build_const_undefined(irb, scope, node);
- ir_build_return(irb, scope, node, undef);
+ // we can return undefined here, because the caller passes a pointer to the error struct field
+ // in the error union result, and we populate it in case of allocation failure.
+ ir_build_return(irb, coro_scope, node, undef);
ir_set_cursor_at_end_and_append_block(irb, alloc_ok_block);
- IrInstruction *coro_mem_ptr = ir_build_ptr_cast(irb, scope, node, u8_ptr_type, maybe_coro_mem_ptr);
- irb->exec->coro_handle = ir_build_coro_begin(irb, scope, node, coro_id, coro_mem_ptr);
+ IrInstruction *coro_mem_ptr = ir_build_ptr_cast(irb, coro_scope, node, u8_ptr_type, maybe_coro_mem_ptr);
+ irb->exec->coro_handle = ir_build_coro_begin(irb, coro_scope, node, coro_id, coro_mem_ptr);
Buf *awaiter_handle_field_name = buf_create_from_str(AWAITER_HANDLE_FIELD_NAME);
irb->exec->coro_awaiter_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr,
awaiter_handle_field_name);
+ ir_build_store_ptr(irb, scope, node, irb->exec->coro_awaiter_field_ptr, null_value);
Buf *result_field_name = buf_create_from_str(RESULT_FIELD_NAME);
irb->exec->coro_result_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_field_name);
result_ptr_field_name = buf_create_from_str(RESULT_PTR_FIELD_NAME);
irb->exec->coro_result_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, result_ptr_field_name);
ir_build_store_ptr(irb, scope, node, irb->exec->coro_result_ptr_field_ptr, irb->exec->coro_result_field_ptr);
+ if (irb->codegen->have_err_ret_tracing) {
+ // initialize the error return trace
+ Buf *return_addresses_field_name = buf_create_from_str(RETURN_ADDRESSES_FIELD_NAME);
+ IrInstruction *return_addresses_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, return_addresses_field_name);
+
+ Buf *err_ret_trace_field_name = buf_create_from_str(ERR_RET_TRACE_FIELD_NAME);
+ err_ret_trace_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_field_name);
+ ir_build_mark_err_ret_trace_ptr(irb, scope, node, err_ret_trace_ptr);
+
+ // coordinate with builtin.zig
+ Buf *index_name = buf_create_from_str("index");
+ IrInstruction *index_ptr = ir_build_field_ptr(irb, scope, node, err_ret_trace_ptr, index_name);
+ IrInstruction *zero = ir_build_const_usize(irb, scope, node, 0);
+ ir_build_store_ptr(irb, scope, node, index_ptr, zero);
+
+ Buf *instruction_addresses_name = buf_create_from_str("instruction_addresses");
+ IrInstruction *addrs_slice_ptr = ir_build_field_ptr(irb, scope, node, err_ret_trace_ptr, instruction_addresses_name);
+
+ IrInstruction *slice_value = ir_build_slice(irb, scope, node, return_addresses_ptr, zero, nullptr, false);
+ ir_build_store_ptr(irb, scope, node, addrs_slice_ptr, slice_value);
+ }
irb->exec->coro_early_final = ir_create_basic_block(irb, scope, "CoroEarlyFinal");
@@ -6395,6 +6517,7 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
return false;
if (!instr_is_unreachable(result)) {
+ // no need for save_err_ret_addr because this cannot return error
ir_gen_async_return(irb, scope, result->source_node, result, true);
}
@@ -6430,6 +6553,12 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, IrExecutable *ir_exec
IrInstruction *size_of_ret_val = ir_build_size_of(irb, scope, node, return_type_inst);
ir_build_memcpy(irb, scope, node, result_ptr_as_u8_ptr, return_value_ptr_as_u8_ptr, size_of_ret_val);
}
+ if (irb->codegen->have_err_ret_tracing) {
+ Buf *err_ret_trace_ptr_field_name = buf_create_from_str(ERR_RET_TRACE_PTR_FIELD_NAME);
+ IrInstruction *err_ret_trace_ptr_field_ptr = ir_build_field_ptr(irb, scope, node, coro_promise_ptr, err_ret_trace_ptr_field_name);
+ IrInstruction *dest_err_ret_trace_ptr = ir_build_load_ptr(irb, scope, node, err_ret_trace_ptr_field_ptr);
+ ir_build_merge_err_ret_traces(irb, scope, node, coro_promise_ptr, err_ret_trace_ptr, dest_err_ret_trace_ptr);
+ }
ir_build_br(irb, scope, node, check_free_block, const_bool_false);
ir_set_cursor_at_end_and_append_block(irb, irb->exec->coro_final_cleanup_block);
@@ -10074,13 +10203,26 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
return result;
}
+static TypeTableEntry *ir_analyze_instruction_add_implicit_return_type(IrAnalyze *ira,
+ IrInstructionAddImplicitReturnType *instruction)
+{
+ IrInstruction *value = instruction->value->other;
+ if (type_is_invalid(value->value.type))
+ return ir_unreach_error(ira);
+
+ ira->src_implicit_return_type_list.append(value);
+
+ ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
+ out_val->type = ira->codegen->builtin_types.entry_void;
+ return out_val->type;
+}
+
static TypeTableEntry *ir_analyze_instruction_return(IrAnalyze *ira,
IrInstructionReturn *return_instruction)
{
IrInstruction *value = return_instruction->value->other;
if (type_is_invalid(value->value.type))
return ir_unreach_error(ira);
- ira->implicit_return_type_list.append(value);
IrInstruction *casted_value = ir_implicit_cast(ira, value, ira->explicit_return_type);
if (casted_value == ira->codegen->invalid_instruction)
@@ -10958,6 +11100,24 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
result_type = get_array_type(ira->codegen, child_type, new_len);
out_array_val = out_val;
+ } else if (is_slice(op1_type) || is_slice(op2_type)) {
+ TypeTableEntry *ptr_type = get_pointer_to_type(ira->codegen, child_type, true);
+ result_type = get_slice_type(ira->codegen, ptr_type);
+ out_array_val = create_const_vals(1);
+ out_array_val->special = ConstValSpecialStatic;
+ out_array_val->type = get_array_type(ira->codegen, child_type, new_len);
+
+ out_val->data.x_struct.fields = create_const_vals(2);
+
+ out_val->data.x_struct.fields[slice_ptr_index].type = ptr_type;
+ out_val->data.x_struct.fields[slice_ptr_index].special = ConstValSpecialStatic;
+ out_val->data.x_struct.fields[slice_ptr_index].data.x_ptr.special = ConstPtrSpecialBaseArray;
+ out_val->data.x_struct.fields[slice_ptr_index].data.x_ptr.data.base_array.array_val = out_array_val;
+ out_val->data.x_struct.fields[slice_ptr_index].data.x_ptr.data.base_array.elem_index = 0;
+
+ out_val->data.x_struct.fields[slice_len_index].type = ira->codegen->builtin_types.entry_usize;
+ out_val->data.x_struct.fields[slice_len_index].special = ConstValSpecialStatic;
+ bigint_init_unsigned(&out_val->data.x_struct.fields[slice_len_index].data.x_bigint, new_len);
} else {
new_len += 1; // null byte
@@ -11453,22 +11613,33 @@ static TypeTableEntry *ir_analyze_instruction_export(IrAnalyze *ira, IrInstructi
return ira->codegen->builtin_types.entry_void;
}
+static bool exec_has_err_ret_trace(CodeGen *g, IrExecutable *exec) {
+ FnTableEntry *fn_entry = exec_fn_entry(exec);
+ return fn_entry != nullptr && fn_entry->calls_or_awaits_errorable_fn && g->have_err_ret_tracing;
+}
+
static TypeTableEntry *ir_analyze_instruction_error_return_trace(IrAnalyze *ira,
IrInstructionErrorReturnTrace *instruction)
{
- FnTableEntry *fn_entry = exec_fn_entry(ira->new_irb.exec);
- TypeTableEntry *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(ira->codegen);
- TypeTableEntry *nullable_type = get_maybe_type(ira->codegen, ptr_to_stack_trace_type);
- if (fn_entry == nullptr || !fn_entry->calls_or_awaits_errorable_fn || !ira->codegen->have_err_ret_tracing) {
- ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
- out_val->data.x_maybe = nullptr;
+ if (instruction->nullable == IrInstructionErrorReturnTrace::Null) {
+ TypeTableEntry *ptr_to_stack_trace_type = get_ptr_to_stack_trace_type(ira->codegen);
+ TypeTableEntry *nullable_type = get_maybe_type(ira->codegen, ptr_to_stack_trace_type);
+ if (!exec_has_err_ret_trace(ira->codegen, ira->new_irb.exec)) {
+ ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
+ out_val->data.x_maybe = nullptr;
+ return nullable_type;
+ }
+ IrInstruction *new_instruction = ir_build_error_return_trace(&ira->new_irb, instruction->base.scope,
+ instruction->base.source_node, instruction->nullable);
+ ir_link_new_instruction(new_instruction, &instruction->base);
return nullable_type;
+ } else {
+ assert(ira->codegen->have_err_ret_tracing);
+ IrInstruction *new_instruction = ir_build_error_return_trace(&ira->new_irb, instruction->base.scope,
+ instruction->base.source_node, instruction->nullable);
+ ir_link_new_instruction(new_instruction, &instruction->base);
+ return get_ptr_to_stack_trace_type(ira->codegen);
}
-
- IrInstruction *new_instruction = ir_build_error_return_trace(&ira->new_irb, instruction->base.scope,
- instruction->base.source_node);
- ir_link_new_instruction(new_instruction, &instruction->base);
- return nullable_type;
}
static TypeTableEntry *ir_analyze_instruction_error_union(IrAnalyze *ira,
@@ -12950,6 +13121,7 @@ static IrInstruction *ir_analyze_container_member_access_inner(IrAnalyze *ira,
{
if (!is_slice(bare_struct_type)) {
ScopeDecls *container_scope = get_container_scope(bare_struct_type);
+ assert(container_scope != nullptr);
auto entry = container_scope->decl_table.maybe_get(field_name);
Tld *tld = entry ? entry->value : nullptr;
if (tld && tld->id == TldIdFn) {
@@ -13999,6 +14171,24 @@ static TypeTableEntry *ir_analyze_instruction_array_type(IrAnalyze *ira,
zig_unreachable();
}
+static TypeTableEntry *ir_analyze_instruction_promise_type(IrAnalyze *ira, IrInstructionPromiseType *instruction) {
+ TypeTableEntry *promise_type;
+
+ if (instruction->payload_type == nullptr) {
+ promise_type = ira->codegen->builtin_types.entry_promise;
+ } else {
+ TypeTableEntry *payload_type = ir_resolve_type(ira, instruction->payload_type->other);
+ if (type_is_invalid(payload_type))
+ return ira->codegen->builtin_types.entry_invalid;
+
+ promise_type = get_promise_type(ira->codegen, payload_type);
+ }
+
+ ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
+ out_val->data.x_type = promise_type;
+ return ira->codegen->builtin_types.entry_type;
+}
+
static TypeTableEntry *ir_analyze_instruction_size_of(IrAnalyze *ira,
IrInstructionSizeOf *size_of_instruction)
{
@@ -14569,7 +14759,7 @@ static TypeTableEntry *ir_analyze_instruction_import(IrAnalyze *ira, IrInstructi
return ira->codegen->builtin_types.entry_namespace;
}
- if ((err = os_fetch_file_path(abs_full_path, import_code))) {
+ if ((err = os_fetch_file_path(abs_full_path, import_code, true))) {
if (err == ErrorFileNotFound) {
ir_add_error_node(ira, source_node,
buf_sprintf("unable to find '%s'", buf_ptr(import_target_path)));
@@ -15430,7 +15620,7 @@ static TypeTableEntry *ir_analyze_instruction_embed_file(IrAnalyze *ira, IrInstr
// load from file system into const expr
Buf *file_contents = buf_alloc();
int err;
- if ((err = os_fetch_file_path(&file_path, file_contents))) {
+ if ((err = os_fetch_file_path(&file_path, file_contents, false))) {
if (err == ErrorFileNotFound) {
ir_add_error(ira, instruction->name, buf_sprintf("unable to find '%s'", buf_ptr(&file_path)));
return ira->codegen->builtin_types.entry_invalid;
@@ -16561,6 +16751,18 @@ static TypeTableEntry *ir_analyze_instruction_fn_proto(IrAnalyze *ira, IrInstruc
if (type_is_invalid(fn_type_id.return_type))
return ira->codegen->builtin_types.entry_invalid;
+ if (fn_type_id.cc == CallingConventionAsync) {
+ if (instruction->async_allocator_type_value == nullptr) {
+ ir_add_error(ira, &instruction->base,
+ buf_sprintf("async fn proto missing allocator type"));
+ return ira->codegen->builtin_types.entry_invalid;
+ }
+ IrInstruction *async_allocator_type_value = instruction->async_allocator_type_value->other;
+ fn_type_id.async_allocator_type = ir_resolve_type(ira, async_allocator_type_value);
+ if (type_is_invalid(fn_type_id.async_allocator_type))
+ return ira->codegen->builtin_types.entry_invalid;
+ }
+
ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
out_val->data.x_type = get_fn_type(ira->codegen, &fn_type_id);
return ira->codegen->builtin_types.entry_type;
@@ -16789,18 +16991,18 @@ static TypeTableEntry *ir_analyze_instruction_can_implicit_cast(IrAnalyze *ira,
static TypeTableEntry *ir_analyze_instruction_panic(IrAnalyze *ira, IrInstructionPanic *instruction) {
IrInstruction *msg = instruction->msg->other;
if (type_is_invalid(msg->value.type))
- return ira->codegen->builtin_types.entry_invalid;
+ return ir_unreach_error(ira);
if (ir_should_inline(ira->new_irb.exec, instruction->base.scope)) {
ir_add_error(ira, &instruction->base, buf_sprintf("encountered @panic at compile-time"));
- return ira->codegen->builtin_types.entry_invalid;
+ return ir_unreach_error(ira);
}
TypeTableEntry *u8_ptr_type = get_pointer_to_type(ira->codegen, ira->codegen->builtin_types.entry_u8, true);
TypeTableEntry *str_type = get_slice_type(ira->codegen, u8_ptr_type);
IrInstruction *casted_msg = ir_implicit_cast(ira, msg, str_type);
if (type_is_invalid(casted_msg->value.type))
- return ira->codegen->builtin_types.entry_invalid;
+ return ir_unreach_error(ira);
IrInstruction *new_instruction = ir_build_panic(&ira->new_irb, instruction->base.scope,
instruction->base.source_node, casted_msg);
@@ -17757,6 +17959,59 @@ static TypeTableEntry *ir_analyze_instruction_await_bookkeeping(IrAnalyze *ira,
return out_val->type;
}
+static TypeTableEntry *ir_analyze_instruction_merge_err_ret_traces(IrAnalyze *ira,
+ IrInstructionMergeErrRetTraces *instruction)
+{
+ IrInstruction *coro_promise_ptr = instruction->coro_promise_ptr->other;
+ if (type_is_invalid(coro_promise_ptr->value.type))
+ return ira->codegen->builtin_types.entry_invalid;
+
+ assert(coro_promise_ptr->value.type->id == TypeTableEntryIdPointer);
+ TypeTableEntry *promise_frame_type = coro_promise_ptr->value.type->data.pointer.child_type;
+ assert(promise_frame_type->id == TypeTableEntryIdStruct);
+ TypeTableEntry *promise_result_type = promise_frame_type->data.structure.fields[1].type_entry;
+
+ if (!type_can_fail(promise_result_type)) {
+ ConstExprValue *out_val = ir_build_const_from(ira, &instruction->base);
+ out_val->type = ira->codegen->builtin_types.entry_void;
+ return out_val->type;
+ }
+
+ IrInstruction *src_err_ret_trace_ptr = instruction->src_err_ret_trace_ptr->other;
+ if (type_is_invalid(src_err_ret_trace_ptr->value.type))
+ return ira->codegen->builtin_types.entry_invalid;
+
+ IrInstruction *dest_err_ret_trace_ptr = instruction->dest_err_ret_trace_ptr->other;
+ if (type_is_invalid(dest_err_ret_trace_ptr->value.type))
+ return ira->codegen->builtin_types.entry_invalid;
+
+ IrInstruction *result = ir_build_merge_err_ret_traces(&ira->new_irb, instruction->base.scope,
+ instruction->base.source_node, coro_promise_ptr, src_err_ret_trace_ptr, dest_err_ret_trace_ptr);
+ ir_link_new_instruction(result, &instruction->base);
+ result->value.type = ira->codegen->builtin_types.entry_void;
+ return result->value.type;
+}
+
+static TypeTableEntry *ir_analyze_instruction_save_err_ret_addr(IrAnalyze *ira, IrInstructionSaveErrRetAddr *instruction) {
+ IrInstruction *result = ir_build_save_err_ret_addr(&ira->new_irb, instruction->base.scope,
+ instruction->base.source_node);
+ ir_link_new_instruction(result, &instruction->base);
+ result->value.type = ira->codegen->builtin_types.entry_void;
+ return result->value.type;
+}
+
+static TypeTableEntry *ir_analyze_instruction_mark_err_ret_trace_ptr(IrAnalyze *ira, IrInstructionMarkErrRetTracePtr *instruction) {
+ IrInstruction *err_ret_trace_ptr = instruction->err_ret_trace_ptr->other;
+ if (type_is_invalid(err_ret_trace_ptr->value.type))
+ return ira->codegen->builtin_types.entry_invalid;
+
+ IrInstruction *result = ir_build_mark_err_ret_trace_ptr(&ira->new_irb, instruction->base.scope,
+ instruction->base.source_node, err_ret_trace_ptr);
+ ir_link_new_instruction(result, &instruction->base);
+ result->value.type = ira->codegen->builtin_types.entry_void;
+ return result->value.type;
+}
+
static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {
switch (instruction->id) {
case IrInstructionIdInvalid:
@@ -17822,6 +18077,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
return ir_analyze_instruction_asm(ira, (IrInstructionAsm *)instruction);
case IrInstructionIdArrayType:
return ir_analyze_instruction_array_type(ira, (IrInstructionArrayType *)instruction);
+ case IrInstructionIdPromiseType:
+ return ir_analyze_instruction_promise_type(ira, (IrInstructionPromiseType *)instruction);
case IrInstructionIdSizeOf:
return ir_analyze_instruction_size_of(ira, (IrInstructionSizeOf *)instruction);
case IrInstructionIdTestNonNull:
@@ -17994,6 +18251,14 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
return ir_analyze_instruction_promise_result_type(ira, (IrInstructionPromiseResultType *)instruction);
case IrInstructionIdAwaitBookkeeping:
return ir_analyze_instruction_await_bookkeeping(ira, (IrInstructionAwaitBookkeeping *)instruction);
+ case IrInstructionIdSaveErrRetAddr:
+ return ir_analyze_instruction_save_err_ret_addr(ira, (IrInstructionSaveErrRetAddr *)instruction);
+ case IrInstructionIdAddImplicitReturnType:
+ return ir_analyze_instruction_add_implicit_return_type(ira, (IrInstructionAddImplicitReturnType *)instruction);
+ case IrInstructionIdMergeErrRetTraces:
+ return ir_analyze_instruction_merge_err_ret_traces(ira, (IrInstructionMergeErrRetTraces *)instruction);
+ case IrInstructionIdMarkErrRetTracePtr:
+ return ir_analyze_instruction_mark_err_ret_trace_ptr(ira, (IrInstructionMarkErrRetTracePtr *)instruction);
}
zig_unreachable();
}
@@ -18067,11 +18332,11 @@ TypeTableEntry *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutabl
if (new_exec->invalid) {
return ira->codegen->builtin_types.entry_invalid;
- } else if (ira->implicit_return_type_list.length == 0) {
+ } else if (ira->src_implicit_return_type_list.length == 0) {
return codegen->builtin_types.entry_unreachable;
} else {
- return ir_resolve_peer_types(ira, expected_type_source_node, ira->implicit_return_type_list.items,
- ira->implicit_return_type_list.length);
+ return ir_resolve_peer_types(ira, expected_type_source_node, ira->src_implicit_return_type_list.items,
+ ira->src_implicit_return_type_list.length);
}
}
@@ -18119,6 +18384,10 @@ bool ir_has_side_effects(IrInstruction *instruction) {
case IrInstructionIdCoroSave:
case IrInstructionIdCoroAllocHelper:
case IrInstructionIdAwaitBookkeeping:
+ case IrInstructionIdSaveErrRetAddr:
+ case IrInstructionIdAddImplicitReturnType:
+ case IrInstructionIdMergeErrRetTraces:
+ case IrInstructionIdMarkErrRetTracePtr:
return true;
case IrInstructionIdPhi:
@@ -18141,6 +18410,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
case IrInstructionIdStructFieldPtr:
case IrInstructionIdUnionFieldPtr:
case IrInstructionIdArrayType:
+ case IrInstructionIdPromiseType:
case IrInstructionIdSliceType:
case IrInstructionIdSizeOf:
case IrInstructionIdTestNonNull:
diff --git a/src/ir_print.cpp b/src/ir_print.cpp
index 167bd20839..99f79ff75e 100644
--- a/src/ir_print.cpp
+++ b/src/ir_print.cpp
@@ -201,9 +201,9 @@ static void ir_print_call(IrPrint *irp, IrInstructionCall *call_instruction) {
if (call_instruction->is_async) {
fprintf(irp->f, "async");
if (call_instruction->async_allocator != nullptr) {
- fprintf(irp->f, "(");
+ fprintf(irp->f, "<");
ir_print_other_instruction(irp, call_instruction->async_allocator);
- fprintf(irp->f, ")");
+ fprintf(irp->f, ">");
}
fprintf(irp->f, " ");
}
@@ -404,6 +404,14 @@ static void ir_print_array_type(IrPrint *irp, IrInstructionArrayType *instructio
ir_print_other_instruction(irp, instruction->child_type);
}
+static void ir_print_promise_type(IrPrint *irp, IrInstructionPromiseType *instruction) {
+ fprintf(irp->f, "promise");
+ if (instruction->payload_type != nullptr) {
+ fprintf(irp->f, "->");
+ ir_print_other_instruction(irp, instruction->payload_type);
+ }
+}
+
static void ir_print_slice_type(IrPrint *irp, IrInstructionSliceType *instruction) {
const char *const_kw = instruction->is_const ? "const " : "";
fprintf(irp->f, "[]%s", const_kw);
@@ -1016,7 +1024,16 @@ static void ir_print_export(IrPrint *irp, IrInstructionExport *instruction) {
}
static void ir_print_error_return_trace(IrPrint *irp, IrInstructionErrorReturnTrace *instruction) {
- fprintf(irp->f, "@errorReturnTrace()");
+ fprintf(irp->f, "@errorReturnTrace(");
+ switch (instruction->nullable) {
+ case IrInstructionErrorReturnTrace::Null:
+ fprintf(irp->f, "Null");
+ break;
+ case IrInstructionErrorReturnTrace::NonNull:
+ fprintf(irp->f, "NonNull");
+ break;
+ }
+ fprintf(irp->f, ")");
}
static void ir_print_error_union(IrPrint *irp, IrInstructionErrorUnion *instruction) {
@@ -1161,6 +1178,32 @@ static void ir_print_await_bookkeeping(IrPrint *irp, IrInstructionAwaitBookkeepi
fprintf(irp->f, ")");
}
+static void ir_print_save_err_ret_addr(IrPrint *irp, IrInstructionSaveErrRetAddr *instruction) {
+ fprintf(irp->f, "@saveErrRetAddr()");
+}
+
+static void ir_print_add_implicit_return_type(IrPrint *irp, IrInstructionAddImplicitReturnType *instruction) {
+ fprintf(irp->f, "@addImplicitReturnType(");
+ ir_print_other_instruction(irp, instruction->value);
+ fprintf(irp->f, ")");
+}
+
+static void ir_print_merge_err_ret_traces(IrPrint *irp, IrInstructionMergeErrRetTraces *instruction) {
+ fprintf(irp->f, "@mergeErrRetTraces(");
+ ir_print_other_instruction(irp, instruction->coro_promise_ptr);
+ fprintf(irp->f, ",");
+ ir_print_other_instruction(irp, instruction->src_err_ret_trace_ptr);
+ fprintf(irp->f, ",");
+ ir_print_other_instruction(irp, instruction->dest_err_ret_trace_ptr);
+ fprintf(irp->f, ")");
+}
+
+static void ir_print_mark_err_ret_trace_ptr(IrPrint *irp, IrInstructionMarkErrRetTracePtr *instruction) {
+ fprintf(irp->f, "@markErrRetTracePtr(");
+ ir_print_other_instruction(irp, instruction->err_ret_trace_ptr);
+ fprintf(irp->f, ")");
+}
+
static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
ir_print_prefix(irp, instruction);
switch (instruction->id) {
@@ -1253,6 +1296,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
case IrInstructionIdArrayType:
ir_print_array_type(irp, (IrInstructionArrayType *)instruction);
break;
+ case IrInstructionIdPromiseType:
+ ir_print_promise_type(irp, (IrInstructionPromiseType *)instruction);
+ break;
case IrInstructionIdSliceType:
ir_print_slice_type(irp, (IrInstructionSliceType *)instruction);
break;
@@ -1532,6 +1578,18 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
case IrInstructionIdAwaitBookkeeping:
ir_print_await_bookkeeping(irp, (IrInstructionAwaitBookkeeping *)instruction);
break;
+ case IrInstructionIdSaveErrRetAddr:
+ ir_print_save_err_ret_addr(irp, (IrInstructionSaveErrRetAddr *)instruction);
+ break;
+ case IrInstructionIdAddImplicitReturnType:
+ ir_print_add_implicit_return_type(irp, (IrInstructionAddImplicitReturnType *)instruction);
+ break;
+ case IrInstructionIdMergeErrRetTraces:
+ ir_print_merge_err_ret_traces(irp, (IrInstructionMergeErrRetTraces *)instruction);
+ break;
+ case IrInstructionIdMarkErrRetTracePtr:
+ ir_print_mark_err_ret_trace_ptr(irp, (IrInstructionMarkErrRetTracePtr *)instruction);
+ break;
}
fprintf(irp->f, "\n");
}
diff --git a/src/link.cpp b/src/link.cpp
index f0537ffa0f..3c6e27e331 100644
--- a/src/link.cpp
+++ b/src/link.cpp
@@ -164,6 +164,47 @@ static void add_rpath(LinkJob *lj, Buf *rpath) {
lj->rpath_table.put(rpath, true);
}
+static Buf *try_dynamic_linker_path(const char *ld_name) {
+ const char *cc_exe = getenv("CC");
+ cc_exe = (cc_exe == nullptr) ? "cc" : cc_exe;
+ ZigList<const char *> args = {};
+ args.append(buf_ptr(buf_sprintf("-print-file-name=%s", ld_name)));
+ Termination term;
+ Buf *out_stderr = buf_alloc();
+ Buf *out_stdout = buf_alloc();
+ int err;
+ if ((err = os_exec_process(cc_exe, args, &term, out_stderr, out_stdout))) {
+ return nullptr;
+ }
+ if (term.how != TerminationIdClean || term.code != 0) {
+ return nullptr;
+ }
+ if (buf_ends_with_str(out_stdout, "\n")) {
+ buf_resize(out_stdout, buf_len(out_stdout) - 1);
+ }
+ if (buf_len(out_stdout) == 0 || buf_eql_str(out_stdout, ld_name)) {
+ return nullptr;
+ }
+ return out_stdout;
+}
+
+static Buf *get_dynamic_linker_path(CodeGen *g) {
+ if (g->is_native_target && g->zig_target.arch.arch == ZigLLVM_x86_64) {
+ static const char *ld_names[] = {
+ "ld-linux-x86-64.so.2",
+ "ld-musl-x86_64.so.1",
+ };
+ for (size_t i = 0; i < array_length(ld_names); i += 1) {
+ const char *ld_name = ld_names[i];
+ Buf *result = try_dynamic_linker_path(ld_name);
+ if (result != nullptr) {
+ return result;
+ }
+ }
+ }
+ return target_dynamic_linker(&g->zig_target);
+}
+
static void construct_linker_job_elf(LinkJob *lj) {
CodeGen *g = lj->codegen;
@@ -259,12 +300,16 @@ static void construct_linker_job_elf(LinkJob *lj) {
lj->args.append(buf_ptr(g->libc_static_lib_dir));
}
- if (g->dynamic_linker && buf_len(g->dynamic_linker) > 0) {
- lj->args.append("-dynamic-linker");
- lj->args.append(buf_ptr(g->dynamic_linker));
- } else {
- lj->args.append("-dynamic-linker");
- lj->args.append(buf_ptr(target_dynamic_linker(&g->zig_target)));
+ if (!g->is_static) {
+ if (g->dynamic_linker != nullptr) {
+ assert(buf_len(g->dynamic_linker) != 0);
+ lj->args.append("-dynamic-linker");
+ lj->args.append(buf_ptr(g->dynamic_linker));
+ } else {
+ Buf *resolved_dynamic_linker = get_dynamic_linker_path(g);
+ lj->args.append("-dynamic-linker");
+ lj->args.append(buf_ptr(resolved_dynamic_linker));
+ }
}
if (shared) {
@@ -423,7 +468,9 @@ static void construct_linker_job_coff(LinkJob *lj) {
lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->kernel32_lib_dir))));
lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_lib_dir))));
- lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_static_lib_dir))));
+ if (g->libc_static_lib_dir != nullptr) {
+ lj->args.append(buf_ptr(buf_sprintf("-LIBPATH:%s", buf_ptr(g->libc_static_lib_dir))));
+ }
}
if (lj->link_in_crt) {
diff --git a/src/main.cpp b/src/main.cpp
index eab7f29b10..63b077e833 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -23,6 +23,7 @@ static int usage(const char *arg0) {
" build-exe [source] create executable from source or object files\n"
" build-lib [source] create library from source or object files\n"
" build-obj [source] create object from source or assembly\n"
+ " run [source] create executable and run immediately\n"
" translate-c [source] convert c code to zig code\n"
" targets list available compilation targets\n"
" test [source] create and run a test build\n"
@@ -195,13 +196,6 @@ static int find_zig_lib_dir(Buf *out_path) {
}
}
- if (ZIG_INSTALL_PREFIX != nullptr) {
- if (test_zig_install_prefix(buf_create_from_str(ZIG_INSTALL_PREFIX), out_path)) {
- return 0;
- }
- }
-
-
return ErrorFileNotFound;
}
@@ -227,6 +221,7 @@ static Buf *resolve_zig_lib_dir(const char *zig_install_prefix_arg) {
enum Cmd {
CmdInvalid,
CmdBuild,
+ CmdRun,
CmdTest,
CmdVersion,
CmdZen,
@@ -336,6 +331,8 @@ int main(int argc, char **argv) {
CliPkg *cur_pkg = allocate<CliPkg>(1);
BuildMode build_mode = BuildModeDebug;
ZigList<const char *> test_exec_args = {0};
+ int comptime_args_end = 0;
+ int runtime_args_start = argc;
if (argc >= 2 && strcmp(argv[1], "build") == 0) {
const char *zig_exe_path = arg0;
@@ -452,10 +449,11 @@ int main(int argc, char **argv) {
if ((err = os_copy_file(build_template_path, &build_file_abs))) {
fprintf(stderr, "Unable to write build.zig template: %s\n", err_str(err));
+ return EXIT_FAILURE;
} else {
fprintf(stderr, "Wrote build.zig template\n");
+ return EXIT_SUCCESS;
}
- return EXIT_SUCCESS;
}
fprintf(stderr,
@@ -487,11 +485,15 @@ int main(int argc, char **argv) {
return (term.how == TerminationIdClean) ? term.code : -1;
}
- for (int i = 1; i < argc; i += 1) {
+ for (int i = 1; i < argc; i += 1, comptime_args_end += 1) {
char *arg = argv[i];
if (arg[0] == '-') {
- if (strcmp(arg, "--release-fast") == 0) {
+ if (strcmp(arg, "--") == 0) {
+ // ignore -- from both compile and runtime arg sets
+ runtime_args_start = i + 1;
+ break;
+ } else if (strcmp(arg, "--release-fast") == 0) {
build_mode = BuildModeFastRelease;
} else if (strcmp(arg, "--release-safe") == 0) {
build_mode = BuildModeSafeRelease;
@@ -658,6 +660,9 @@ int main(int argc, char **argv) {
} else if (strcmp(arg, "build-lib") == 0) {
cmd = CmdBuild;
out_type = OutTypeLib;
+ } else if (strcmp(arg, "run") == 0) {
+ cmd = CmdRun;
+ out_type = OutTypeExe;
} else if (strcmp(arg, "version") == 0) {
cmd = CmdVersion;
} else if (strcmp(arg, "zen") == 0) {
@@ -676,6 +681,7 @@ int main(int argc, char **argv) {
} else {
switch (cmd) {
case CmdBuild:
+ case CmdRun:
case CmdTranslateC:
case CmdTest:
if (!in_file) {
@@ -730,8 +736,8 @@ int main(int argc, char **argv) {
}
}
-
switch (cmd) {
+ case CmdRun:
case CmdBuild:
case CmdTranslateC:
case CmdTest:
@@ -739,7 +745,7 @@ int main(int argc, char **argv) {
if (cmd == CmdBuild && !in_file && objects.length == 0 && asm_files.length == 0) {
fprintf(stderr, "Expected source file argument or at least one --object or --assembly argument.\n");
return usage(arg0);
- } else if ((cmd == CmdTranslateC || cmd == CmdTest) && !in_file) {
+ } else if ((cmd == CmdTranslateC || cmd == CmdTest || cmd == CmdRun) && !in_file) {
fprintf(stderr, "Expected source file argument.\n");
return usage(arg0);
} else if (cmd == CmdBuild && out_type == OutTypeObj && objects.length != 0) {
@@ -751,6 +757,10 @@ int main(int argc, char **argv) {
bool need_name = (cmd == CmdBuild || cmd == CmdTranslateC);
+ if (cmd == CmdRun) {
+ out_name = "run";
+ }
+
Buf *in_file_buf = nullptr;
Buf *buf_out_name = (cmd == CmdTest) ? buf_create_from_str("test") :
@@ -775,9 +785,23 @@ int main(int argc, char **argv) {
Buf *zig_root_source_file = (cmd == CmdTranslateC) ? nullptr : in_file_buf;
Buf *full_cache_dir = buf_alloc();
- os_path_resolve(buf_create_from_str("."),
- buf_create_from_str((cache_dir == nullptr) ? default_zig_cache_name : cache_dir),
- full_cache_dir);
+ Buf *run_exec_path = buf_alloc();
+ if (cmd == CmdRun) {
+ if (buf_out_name == nullptr) {
+ buf_out_name = buf_create_from_str("run");
+ }
+
+ Buf *global_cache_dir = buf_alloc();
+ os_get_global_cache_directory(global_cache_dir);
+ os_path_join(global_cache_dir, buf_out_name, run_exec_path);
+ os_path_resolve(buf_create_from_str("."), global_cache_dir, full_cache_dir);
+
+ out_file = buf_ptr(run_exec_path);
+ } else {
+ os_path_resolve(buf_create_from_str("."),
+ buf_create_from_str((cache_dir == nullptr) ? default_zig_cache_name : cache_dir),
+ full_cache_dir);
+ }
Buf *zig_lib_dir_buf = resolve_zig_lib_dir(zig_install_prefix);
@@ -861,7 +885,7 @@ int main(int argc, char **argv) {
add_package(g, cur_pkg, g->root_package);
- if (cmd == CmdBuild) {
+ if (cmd == CmdBuild || cmd == CmdRun) {
codegen_set_emit_file_type(g, emit_file_type);
for (size_t i = 0; i < objects.length; i += 1) {
@@ -874,6 +898,18 @@ int main(int argc, char **argv) {
codegen_link(g, out_file);
if (timing_info)
codegen_print_timing_report(g, stdout);
+
+ if (cmd == CmdRun) {
+ ZigList<const char*> args = {0};
+ for (int i = runtime_args_start; i < argc; ++i) {
+ args.append(argv[i]);
+ }
+
+ Termination term;
+ os_spawn_process(buf_ptr(run_exec_path), args, &term);
+ return term.code;
+ }
+
return EXIT_SUCCESS;
} else if (cmd == CmdTranslateC) {
codegen_translate_c(g, in_file_buf);
diff --git a/src/os.cpp b/src/os.cpp
index e312854612..e0491b21de 100644
--- a/src/os.cpp
+++ b/src/os.cpp
@@ -45,6 +45,7 @@ typedef SSIZE_T ssize_t;
#if defined(__MACH__)
#include <mach/clock.h>
#include <mach/mach.h>
+#include <mach-o/dyld.h>
#endif
#if defined(ZIG_OS_WINDOWS)
@@ -57,10 +58,6 @@ static clock_serv_t cclock;
#include <errno.h>
#include <time.h>
-// these implementations are lazy. But who cares, we'll make a robust
-// implementation in the zig standard library and then this code all gets
-// deleted when we self-host. it works for now.
-
#if defined(ZIG_OS_POSIX)
static void populate_termination(Termination *term, int status) {
if (WIFEXITED(status)) {
@@ -291,13 +288,39 @@ void os_path_resolve(Buf *ref_path, Buf *target_path, Buf *out_abs_path) {
return;
}
-int os_fetch_file(FILE *f, Buf *out_buf) {
+int os_fetch_file(FILE *f, Buf *out_buf, bool skip_shebang) {
static const ssize_t buf_size = 0x2000;
buf_resize(out_buf, buf_size);
ssize_t actual_buf_len = 0;
+
+ bool first_read = true;
+
for (;;) {
size_t amt_read = fread(buf_ptr(out_buf) + actual_buf_len, 1, buf_size, f);
actual_buf_len += amt_read;
+
+ if (skip_shebang && first_read && buf_starts_with_str(out_buf, "#!")) {
+ size_t i = 0;
+ while (true) {
+ if (i > buf_len(out_buf)) {
+ zig_panic("shebang line exceeded %zd characters", buf_size);
+ }
+
+ size_t current_pos = i;
+ i += 1;
+
+ if (out_buf->list.at(current_pos) == '\n') {
+ break;
+ }
+ }
+
+ ZigList<char> *list = &out_buf->list;
+ memmove(list->items, list->items + i, list->length - i);
+ list->length -= i;
+
+ actual_buf_len -= i;
+ }
+
if (amt_read != buf_size) {
if (feof(f)) {
buf_resize(out_buf, actual_buf_len);
@@ -308,6 +331,7 @@ int os_fetch_file(FILE *f, Buf *out_buf) {
}
buf_resize(out_buf, actual_buf_len + buf_size);
+ first_read = false;
}
zig_unreachable();
}
@@ -377,8 +401,8 @@ static int os_exec_process_posix(const char *exe, ZigList<const char *> &args,
FILE *stdout_f = fdopen(stdout_pipe[0], "rb");
FILE *stderr_f = fdopen(stderr_pipe[0], "rb");
- os_fetch_file(stdout_f, out_stdout);
- os_fetch_file(stderr_f, out_stderr);
+ os_fetch_file(stdout_f, out_stdout, false);
+ os_fetch_file(stderr_f, out_stderr, false);
fclose(stdout_f);
fclose(stderr_f);
@@ -591,7 +615,7 @@ int os_copy_file(Buf *src_path, Buf *dest_path) {
}
}
-int os_fetch_file_path(Buf *full_path, Buf *out_contents) {
+int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang) {
FILE *f = fopen(buf_ptr(full_path), "rb");
if (!f) {
switch (errno) {
@@ -610,7 +634,7 @@ int os_fetch_file_path(Buf *full_path, Buf *out_contents) {
return ErrorFileSystem;
}
}
- int result = os_fetch_file(f, out_contents);
+ int result = os_fetch_file(f, out_contents, skip_shebang);
fclose(f);
return result;
}
@@ -783,6 +807,44 @@ int os_buf_to_tmp_file(Buf *contents, Buf *suffix, Buf *out_tmp_path) {
#endif
}
+#if defined(ZIG_OS_POSIX)
+int os_get_global_cache_directory(Buf *out_tmp_path) {
+ const char *tmp_dir = getenv("TMPDIR");
+ if (!tmp_dir) {
+ tmp_dir = P_tmpdir;
+ }
+
+ Buf *tmp_dir_buf = buf_create_from_str(tmp_dir);
+ Buf *cache_dirname_buf = buf_create_from_str("zig-cache");
+
+ buf_resize(out_tmp_path, 0);
+ os_path_join(tmp_dir_buf, cache_dirname_buf, out_tmp_path);
+
+ buf_deinit(tmp_dir_buf);
+ buf_deinit(cache_dirname_buf);
+ return 0;
+}
+#endif
+
+#if defined(ZIG_OS_WINDOWS)
+int os_get_global_cache_directory(Buf *out_tmp_path) {
+ char tmp_dir[MAX_PATH + 1];
+ if (GetTempPath(MAX_PATH, tmp_dir) == 0) {
+ zig_panic("GetTempPath failed");
+ }
+
+ Buf *tmp_dir_buf = buf_create_from_str(tmp_dir);
+ Buf *cache_dirname_buf = buf_create_from_str("zig-cache");
+
+ buf_resize(out_tmp_path, 0);
+ os_path_join(tmp_dir_buf, cache_dirname_buf, out_tmp_path);
+
+ buf_deinit(tmp_dir_buf);
+ buf_deinit(cache_dirname_buf);
+ return 0;
+}
+#endif
+
int os_delete_file(Buf *path) {
if (remove(buf_ptr(path))) {
return ErrorFileSystem;
@@ -927,9 +989,26 @@ int os_self_exe_path(Buf *out_path) {
}
#elif defined(ZIG_OS_DARWIN)
- return ErrorFileNotFound;
+ uint32_t u32_len = 0;
+ int ret1 = _NSGetExecutablePath(nullptr, &u32_len);
+ assert(ret1 != 0);
+ buf_resize(out_path, u32_len);
+ int ret2 = _NSGetExecutablePath(buf_ptr(out_path), &u32_len);
+ assert(ret2 == 0);
+ return 0;
#elif defined(ZIG_OS_LINUX)
- return ErrorFileNotFound;
+ buf_resize(out_path, 256);
+ for (;;) {
+ ssize_t amt = readlink("/proc/self/exe", buf_ptr(out_path), buf_len(out_path));
+ if (amt == -1) {
+ return ErrorUnexpected;
+ }
+ if (amt == (ssize_t)buf_len(out_path)) {
+ buf_resize(out_path, buf_len(out_path) * 2);
+ continue;
+ }
+ return 0;
+ }
#endif
return ErrorFileNotFound;
}
diff --git a/src/os.hpp b/src/os.hpp
index 5d29db0d07..b94e98ec3d 100644
--- a/src/os.hpp
+++ b/src/os.hpp
@@ -51,14 +51,16 @@ int os_path_real(Buf *rel_path, Buf *out_abs_path);
void os_path_resolve(Buf *ref_path, Buf *target_path, Buf *out_abs_path);
bool os_path_is_absolute(Buf *path);
+int os_get_global_cache_directory(Buf *out_tmp_path);
+
int os_make_path(Buf *path);
int os_make_dir(Buf *path);
void os_write_file(Buf *full_path, Buf *contents);
int os_copy_file(Buf *src_path, Buf *dest_path);
-int os_fetch_file(FILE *file, Buf *out_contents);
-int os_fetch_file_path(Buf *full_path, Buf *out_contents);
+int os_fetch_file(FILE *file, Buf *out_contents, bool skip_shebang);
+int os_fetch_file_path(Buf *full_path, Buf *out_contents, bool skip_shebang);
int os_get_cwd(Buf *out_cwd);
diff --git a/src/parser.cpp b/src/parser.cpp
index c7675ad67d..2bd94033cc 100644
--- a/src/parser.cpp
+++ b/src/parser.cpp
@@ -705,7 +705,7 @@ static AstNode *ast_parse_comptime_expr(ParseContext *pc, size_t *token_index, b
}
/*
-PrimaryExpression = Integer | Float | String | CharLiteral | KeywordLiteral | GroupedExpression | BlockExpression(BlockOrExpression) | Symbol | ("@" Symbol FnCallExpression) | ArrayType | FnProto | AsmExpression | ContainerDecl | ("continue" option(":" Symbol)) | ErrorSetDecl
+PrimaryExpression = Integer | Float | String | CharLiteral | KeywordLiteral | GroupedExpression | BlockExpression(BlockOrExpression) | Symbol | ("@" Symbol FnCallExpression) | ArrayType | FnProto | AsmExpression | ContainerDecl | ("continue" option(":" Symbol)) | ErrorSetDecl | PromiseType
KeywordLiteral = "true" | "false" | "null" | "undefined" | "error" | "this" | "unreachable" | "suspend"
ErrorSetDecl = "error" "{" list(Symbol, ",") "}"
*/
@@ -774,6 +774,15 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bo
AstNode *node = ast_create_node(pc, NodeTypeSuspend, token);
*token_index += 1;
return node;
+ } else if (token->id == TokenIdKeywordPromise) {
+ AstNode *node = ast_create_node(pc, NodeTypePromiseType, token);
+ *token_index += 1;
+ Token *arrow_tok = &pc->tokens->at(*token_index);
+ if (arrow_tok->id == TokenIdArrow) {
+ *token_index += 1;
+ node->data.promise_type.payload_type = ast_parse_type_expr(pc, token_index, true);
+ }
+ return node;
} else if (token->id == TokenIdKeywordError) {
Token *next_token = &pc->tokens->at(*token_index + 1);
if (next_token->id == TokenIdLBrace) {
@@ -955,8 +964,68 @@ static AstNode *ast_parse_curly_suffix_expr(ParseContext *pc, size_t *token_inde
}
}
+static AstNode *ast_parse_fn_proto_partial(ParseContext *pc, size_t *token_index, Token *fn_token,
+ AstNode *async_allocator_type_node, CallingConvention cc, bool is_extern, VisibMod visib_mod)
+{
+ AstNode *node = ast_create_node(pc, NodeTypeFnProto, fn_token);
+ node->data.fn_proto.visib_mod = visib_mod;
+ node->data.fn_proto.cc = cc;
+ node->data.fn_proto.is_extern = is_extern;
+ node->data.fn_proto.async_allocator_type = async_allocator_type_node;
+
+ Token *fn_name = &pc->tokens->at(*token_index);
+
+ if (fn_name->id == TokenIdSymbol) {
+ *token_index += 1;
+ node->data.fn_proto.name = token_buf(fn_name);
+ } else {
+ node->data.fn_proto.name = nullptr;
+ }
+
+ ast_parse_param_decl_list(pc, token_index, &node->data.fn_proto.params, &node->data.fn_proto.is_var_args);
+
+ Token *next_token = &pc->tokens->at(*token_index);
+ if (next_token->id == TokenIdKeywordAlign) {
+ *token_index += 1;
+ ast_eat_token(pc, token_index, TokenIdLParen);
+
+ node->data.fn_proto.align_expr = ast_parse_expression(pc, token_index, true);
+ ast_eat_token(pc, token_index, TokenIdRParen);
+ next_token = &pc->tokens->at(*token_index);
+ }
+ if (next_token->id == TokenIdKeywordSection) {
+ *token_index += 1;
+ ast_eat_token(pc, token_index, TokenIdLParen);
+
+ node->data.fn_proto.section_expr = ast_parse_expression(pc, token_index, true);
+ ast_eat_token(pc, token_index, TokenIdRParen);
+ next_token = &pc->tokens->at(*token_index);
+ }
+ if (next_token->id == TokenIdKeywordVar) {
+ node->data.fn_proto.return_var_token = next_token;
+ *token_index += 1;
+ next_token = &pc->tokens->at(*token_index);
+ } else {
+ if (next_token->id == TokenIdKeywordError) {
+ Token *maybe_lbrace_tok = &pc->tokens->at(*token_index + 1);
+ if (maybe_lbrace_tok->id == TokenIdLBrace) {
+ *token_index += 1;
+ node->data.fn_proto.return_type = ast_create_node(pc, NodeTypeErrorType, next_token);
+ return node;
+ }
+ } else if (next_token->id == TokenIdBang) {
+ *token_index += 1;
+ node->data.fn_proto.auto_err_set = true;
+ next_token = &pc->tokens->at(*token_index);
+ }
+ node->data.fn_proto.return_type = ast_parse_type_expr(pc, token_index, true);
+ }
+
+ return node;
+}
+
/*
-SuffixOpExpression = ("async" option("(" Expression ")") PrimaryExpression FnCallExpression) | PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
+SuffixOpExpression = ("async" option("<" SuffixOpExpression ">") SuffixOpExpression FnCallExpression) | PrimaryExpression option(FnCallExpression | ArrayAccessExpression | FieldAccessExpression | SliceExpression)
FnCallExpression : token(LParen) list(Expression, token(Comma)) token(RParen)
ArrayAccessExpression : token(LBracket) Expression token(RBracket)
SliceExpression = "[" Expression ".." option(Expression) "]"
@@ -972,19 +1041,25 @@ static AstNode *ast_parse_suffix_op_expr(ParseContext *pc, size_t *token_index,
AstNode *allocator_expr_node = nullptr;
Token *async_lparen_tok = &pc->tokens->at(*token_index);
- if (async_lparen_tok->id == TokenIdLParen) {
+ if (async_lparen_tok->id == TokenIdCmpLessThan) {
*token_index += 1;
- allocator_expr_node = ast_parse_expression(pc, token_index, true);
- ast_eat_token(pc, token_index, TokenIdRParen);
+ allocator_expr_node = ast_parse_prefix_op_expr(pc, token_index, true);
+ ast_eat_token(pc, token_index, TokenIdCmpGreaterThan);
}
- AstNode *fn_ref_expr_node = ast_parse_primary_expr(pc, token_index, true);
- Token *lparen_tok = ast_eat_token(pc, token_index, TokenIdLParen);
- AstNode *node = ast_create_node(pc, NodeTypeFnCallExpr, lparen_tok);
+ Token *fncall_token = &pc->tokens->at(*token_index);
+ if (fncall_token->id == TokenIdKeywordFn) {
+ *token_index += 1;
+ return ast_parse_fn_proto_partial(pc, token_index, fncall_token, allocator_expr_node, CallingConventionAsync,
+ false, VisibModPrivate);
+ }
+ AstNode *node = ast_parse_suffix_op_expr(pc, token_index, true);
+ if (node->type != NodeTypeFnCallExpr) {
+ ast_error(pc, fncall_token, "expected function call, found '%s'", token_name(fncall_token->id));
+ }
node->data.fn_call_expr.is_async = true;
node->data.fn_call_expr.async_allocator = allocator_expr_node;
- node->data.fn_call_expr.fn_ref_expr = fn_ref_expr_node;
- ast_parse_fn_call_param_list(pc, token_index, &node->data.fn_call_expr.params);
+ assert(node->data.fn_call_expr.fn_ref_expr != nullptr);
primary_expr = node;
} else {
@@ -2433,9 +2508,10 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool m
} else if (first_token->id == TokenIdKeywordAsync) {
*token_index += 1;
Token *next_token = &pc->tokens->at(*token_index);
- if (next_token->id == TokenIdLParen) {
+ if (next_token->id == TokenIdCmpLessThan) {
+ *token_index += 1;
async_allocator_type_node = ast_parse_type_expr(pc, token_index, true);
- ast_eat_token(pc, token_index, TokenIdRParen);
+ ast_eat_token(pc, token_index, TokenIdCmpGreaterThan);
}
fn_token = ast_eat_token(pc, token_index, TokenIdKeywordFn);
cc = CallingConventionAsync;
@@ -2469,61 +2545,7 @@ static AstNode *ast_parse_fn_proto(ParseContext *pc, size_t *token_index, bool m
return nullptr;
}
- AstNode *node = ast_create_node(pc, NodeTypeFnProto, fn_token);
- node->data.fn_proto.visib_mod = visib_mod;
- node->data.fn_proto.cc = cc;
- node->data.fn_proto.is_extern = is_extern;
- node->data.fn_proto.async_allocator_type = async_allocator_type_node;
-
- Token *fn_name = &pc->tokens->at(*token_index);
-
- if (fn_name->id == TokenIdSymbol) {
- *token_index += 1;
- node->data.fn_proto.name = token_buf(fn_name);
- } else {
- node->data.fn_proto.name = nullptr;
- }
-
- ast_parse_param_decl_list(pc, token_index, &node->data.fn_proto.params, &node->data.fn_proto.is_var_args);
-
- Token *next_token = &pc->tokens->at(*token_index);
- if (next_token->id == TokenIdKeywordAlign) {
- *token_index += 1;
- ast_eat_token(pc, token_index, TokenIdLParen);
-
- node->data.fn_proto.align_expr = ast_parse_expression(pc, token_index, true);
- ast_eat_token(pc, token_index, TokenIdRParen);
- next_token = &pc->tokens->at(*token_index);
- }
- if (next_token->id == TokenIdKeywordSection) {
- *token_index += 1;
- ast_eat_token(pc, token_index, TokenIdLParen);
-
- node->data.fn_proto.section_expr = ast_parse_expression(pc, token_index, true);
- ast_eat_token(pc, token_index, TokenIdRParen);
- next_token = &pc->tokens->at(*token_index);
- }
- if (next_token->id == TokenIdKeywordVar) {
- node->data.fn_proto.return_var_token = next_token;
- *token_index += 1;
- next_token = &pc->tokens->at(*token_index);
- } else {
- if (next_token->id == TokenIdKeywordError) {
- Token *maybe_lbrace_tok = &pc->tokens->at(*token_index + 1);
- if (maybe_lbrace_tok->id == TokenIdLBrace) {
- *token_index += 1;
- node->data.fn_proto.return_type = ast_create_node(pc, NodeTypeErrorType, next_token);
- return node;
- }
- } else if (next_token->id == TokenIdBang) {
- *token_index += 1;
- node->data.fn_proto.auto_err_set = true;
- next_token = &pc->tokens->at(*token_index);
- }
- node->data.fn_proto.return_type = ast_parse_type_expr(pc, token_index, true);
- }
-
- return node;
+ return ast_parse_fn_proto_partial(pc, token_index, fn_token, async_allocator_type_node, cc, is_extern, visib_mod);
}
/*
@@ -2901,9 +2923,6 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
visit_field(&node->data.fn_def.fn_proto, visit, context);
visit_field(&node->data.fn_def.body, visit, context);
break;
- case NodeTypeFnDecl:
- visit_field(&node->data.fn_decl.fn_proto, visit, context);
- break;
case NodeTypeParamDecl:
visit_field(&node->data.param_decl.type, visit, context);
break;
@@ -3068,6 +3087,9 @@ void ast_visit_node_children(AstNode *node, void (*visit)(AstNode **, void *cont
visit_field(&node->data.array_type.child_type, visit, context);
visit_field(&node->data.array_type.align_expr, visit, context);
break;
+ case NodeTypePromiseType:
+ visit_field(&node->data.promise_type.payload_type, visit, context);
+ break;
case NodeTypeErrorType:
// none
break;
diff --git a/src/parser.hpp b/src/parser.hpp
index 21922db4ed..224e289bf5 100644
--- a/src/parser.hpp
+++ b/src/parser.hpp
@@ -16,7 +16,6 @@ ATTRIBUTE_PRINTF(2, 3)
void ast_token_error(Token *token, const char *format, ...);
-// This function is provided by generated code, generated by parsergen.cpp
AstNode * ast_parse(Buf *buf, ZigList<Token> *tokens, ImportTableEntry *owner, ErrColor err_color);
void ast_print(AstNode *node, int indent);
diff --git a/src/target.cpp b/src/target.cpp
index 8e7c5ce578..5008b51a09 100644
--- a/src/target.cpp
+++ b/src/target.cpp
@@ -863,6 +863,10 @@ Buf *target_dynamic_linker(ZigTarget *target) {
env == ZigLLVM_GNUX32)
{
return buf_create_from_str("/libx32/ld-linux-x32.so.2");
+ } else if (arch == ZigLLVM_x86_64 &&
+ (env == ZigLLVM_Musl || env == ZigLLVM_MuslEABI || env == ZigLLVM_MuslEABIHF))
+ {
+ return buf_create_from_str("/lib/ld-musl-x86_64.so.1");
} else {
return buf_create_from_str("/lib64/ld-linux-x86-64.so.2");
}
diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp
index dc17829c0f..365b35cdfd 100644
--- a/src/tokenizer.cpp
+++ b/src/tokenizer.cpp
@@ -135,6 +135,7 @@ static const struct ZigKeyword zig_keywords[] = {
{"null", TokenIdKeywordNull},
{"or", TokenIdKeywordOr},
{"packed", TokenIdKeywordPacked},
+ {"promise", TokenIdKeywordPromise},
{"pub", TokenIdKeywordPub},
{"resume", TokenIdKeywordResume},
{"return", TokenIdKeywordReturn},
@@ -1558,6 +1559,7 @@ const char * token_name(TokenId id) {
case TokenIdKeywordNull: return "null";
case TokenIdKeywordOr: return "or";
case TokenIdKeywordPacked: return "packed";
+ case TokenIdKeywordPromise: return "promise";
case TokenIdKeywordPub: return "pub";
case TokenIdKeywordReturn: return "return";
case TokenIdKeywordSection: return "section";
diff --git a/src/tokenizer.hpp b/src/tokenizer.hpp
index 2d71427997..b719293704 100644
--- a/src/tokenizer.hpp
+++ b/src/tokenizer.hpp
@@ -76,6 +76,7 @@ enum TokenId {
TokenIdKeywordNull,
TokenIdKeywordOr,
TokenIdKeywordPacked,
+ TokenIdKeywordPromise,
TokenIdKeywordPub,
TokenIdKeywordResume,
TokenIdKeywordReturn,