authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-27 21:01:03-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-05-28 12:58:40-07:00
log4ea421f8cb3afc01f6822227b48123dd4844ec07
treed8a9005b080669a97a3719dfc6cd793395983ed4
parent42f4ee0aebf274e74ece73157a63bbc0c6383c31

stage1: move the ZigFn from Stage1Zir to Stage1AstGen

Part of an effort to make Stage1Zir immutable.

7 files changed, 51 insertions(+), 60 deletions(-)

src/stage1/all_types.hpp-2
...@@ -114,7 +114,6 @@ struct Stage1Zir {...@@ -114,7 +114,6 @@ struct Stage1Zir {
114 ZigList<IrBasicBlockSrc *> basic_block_list;114 ZigList<IrBasicBlockSrc *> basic_block_list;
115 Buf *name;115 Buf *name;
116 ZigFn *name_fn;116 ZigFn *name_fn;
117 ZigFn *fn_entry;
118 Buf *c_import_buf;117 Buf *c_import_buf;
119 AstNode *source_node;118 AstNode *source_node;
120 Scope *begin_scope;119 Scope *begin_scope;
...@@ -135,7 +134,6 @@ struct Stage1Air {...@@ -135,7 +134,6 @@ struct Stage1Air {
135 ZigFn *name_fn;134 ZigFn *name_fn;
136 size_t mem_slot_count;135 size_t mem_slot_count;
137 size_t next_debug_id;136 size_t next_debug_id;
138 ZigFn *fn_entry;
139 Buf *c_import_buf;137 Buf *c_import_buf;
140 AstNode *source_node;138 AstNode *source_node;
141 Stage1Air *parent_exec;139 Stage1Air *parent_exec;
src/stage1/analyze.cpp+2-5
...@@ -3654,9 +3654,6 @@ static void get_fully_qualified_decl_name(CodeGen *g, Buf *buf, Tld *tld, bool i...@@ -3654,9 +3654,6 @@ static void get_fully_qualified_decl_name(CodeGen *g, Buf *buf, Tld *tld, bool i
3654static ZigFn *create_fn_raw(CodeGen *g, bool is_noinline) {3654static ZigFn *create_fn_raw(CodeGen *g, bool is_noinline) {
3655 ZigFn *fn_entry = heap::c_allocator.create<ZigFn>();3655 ZigFn *fn_entry = heap::c_allocator.create<ZigFn>();
3656 fn_entry->ir_executable = heap::c_allocator.create<Stage1Zir>();3656 fn_entry->ir_executable = heap::c_allocator.create<Stage1Zir>();
3657
3658 fn_entry->analyzed_executable.fn_entry = fn_entry;
3659 fn_entry->ir_executable->fn_entry = fn_entry;
3660 fn_entry->is_noinline = is_noinline;3657 fn_entry->is_noinline = is_noinline;
36613658
3662 return fn_entry;3659 return fn_entry;
...@@ -5134,7 +5131,7 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) {...@@ -5134,7 +5131,7 @@ static void analyze_fn_ir(CodeGen *g, ZigFn *fn, AstNode *return_type_node) {
5134 size_t backward_branch_quota = max(fn->branch_quota, default_backward_branch_quota);5131 size_t backward_branch_quota = max(fn->branch_quota, default_backward_branch_quota);
5135 ZigType *block_return_type = ir_analyze(g, fn->ir_executable, &fn->analyzed_executable,5132 ZigType *block_return_type = ir_analyze(g, fn->ir_executable, &fn->analyzed_executable,
5136 &backward_branch_count, &backward_branch_quota,5133 &backward_branch_count, &backward_branch_quota,
5137 fn_type_id->return_type, return_type_node, nullptr);5134 fn_type_id->return_type, return_type_node, nullptr, fn);
5138 fn->src_implicit_return_type = block_return_type;5135 fn->src_implicit_return_type = block_return_type;
51395136
5140 if (type_is_invalid(block_return_type) || fn->analyzed_executable.first_err_trace_msg != nullptr) {5137 if (type_is_invalid(block_return_type) || fn->analyzed_executable.first_err_trace_msg != nullptr) {
...@@ -5231,7 +5228,7 @@ static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry) {...@@ -5231,7 +5228,7 @@ static void analyze_fn_body(CodeGen *g, ZigFn *fn_table_entry) {
5231 ZigType *fn_type = fn_table_entry->type_entry;5228 ZigType *fn_type = fn_table_entry->type_entry;
5232 assert(!fn_type->data.fn.is_generic);5229 assert(!fn_type->data.fn.is_generic);
52335230
5234 if (!ir_gen_fn(g, fn_table_entry)) {5231 if (!stage1_astgen_fn(g, fn_table_entry)) {
5235 fn_table_entry->anal_state = FnAnalStateInvalid;5232 fn_table_entry->anal_state = FnAnalStateInvalid;
5236 return;5233 return;
5237 }5234 }
src/stage1/astgen.cpp+16-22
...@@ -17,6 +17,7 @@ struct Stage1AstGen {...@@ -17,6 +17,7 @@ struct Stage1AstGen {
17 IrBasicBlockSrc *current_basic_block;17 IrBasicBlockSrc *current_basic_block;
18 AstNode *main_block_node;18 AstNode *main_block_node;
19 size_t next_debug_id;19 size_t next_debug_id;
20 ZigFn *fn;
20};21};
2122
22static IrInstSrc *ir_gen_node(Stage1AstGen *ag, AstNode *node, Scope *scope);23static IrInstSrc *ir_gen_node(Stage1AstGen *ag, AstNode *node, Scope *scope);
...@@ -368,10 +369,6 @@ static size_t irb_next_debug_id(Stage1AstGen *ag) {...@@ -368,10 +369,6 @@ static size_t irb_next_debug_id(Stage1AstGen *ag) {
368 return result;369 return result;
369}370}
370371
371static ZigFn *exec_fn_entry(Stage1Zir *exec) {
372 return exec->fn_entry;
373}
374
375static Buf *exec_c_import_buf(Stage1Zir *exec) {372static Buf *exec_c_import_buf(Stage1Zir *exec) {
376 return exec->c_import_buf;373 return exec->c_import_buf;
377}374}
...@@ -3043,7 +3040,7 @@ static IrInstSrc *ir_gen_return(Stage1AstGen *ag, Scope *scope, AstNode *node, L...@@ -3043,7 +3040,7 @@ static IrInstSrc *ir_gen_return(Stage1AstGen *ag, Scope *scope, AstNode *node, L
3043 if (expr_node) {3040 if (expr_node) {
3044 // Temporarily set this so that if we return a type it gets the name of the function3041 // Temporarily set this so that if we return a type it gets the name of the function
3045 ZigFn *prev_name_fn = ag->exec->name_fn;3042 ZigFn *prev_name_fn = ag->exec->name_fn;
3046 ag->exec->name_fn = exec_fn_entry(ag->exec);3043 ag->exec->name_fn = ag->fn;
3047 return_value = ir_gen_node_extra(ag, expr_node, scope, LValNone, &result_loc_ret->base);3044 return_value = ir_gen_node_extra(ag, expr_node, scope, LValNone, &result_loc_ret->base);
3048 ag->exec->name_fn = prev_name_fn;3045 ag->exec->name_fn = prev_name_fn;
3049 if (return_value == ag->codegen->invalid_inst_src)3046 if (return_value == ag->codegen->invalid_inst_src)
...@@ -4771,8 +4768,9 @@ static IrInstSrc *ir_gen_builtin_fn_call(Stage1AstGen *ag, Scope *scope, AstNode...@@ -4771,8 +4768,9 @@ static IrInstSrc *ir_gen_builtin_fn_call(Stage1AstGen *ag, Scope *scope, AstNode
4771 case BuiltinFnIdFrameAddress:4768 case BuiltinFnIdFrameAddress:
4772 return ir_lval_wrap(ag, scope, ir_build_frame_address_src(ag, scope, node), lval, result_loc);4769 return ir_lval_wrap(ag, scope, ir_build_frame_address_src(ag, scope, node), lval, result_loc);
4773 case BuiltinFnIdFrameHandle:4770 case BuiltinFnIdFrameHandle:
4774 if (!ag->exec->fn_entry) {4771 if (ag->fn == nullptr) {
4775 add_node_error(ag->codegen, node, buf_sprintf("@frame() called outside of function definition"));4772 add_node_error(ag->codegen, node,
4773 buf_sprintf("@frame() called outside of function definition"));
4776 return ag->codegen->invalid_inst_src;4774 return ag->codegen->invalid_inst_src;
4777 }4775 }
4778 return ir_lval_wrap(ag, scope, ir_build_handle_src(ag, scope, node), lval, result_loc);4776 return ir_lval_wrap(ag, scope, ir_build_handle_src(ag, scope, node), lval, result_loc);
...@@ -7700,8 +7698,7 @@ static IrInstSrc *ir_gen_await_expr(Stage1AstGen *ag, Scope *scope, AstNode *nod...@@ -7700,8 +7698,7 @@ static IrInstSrc *ir_gen_await_expr(Stage1AstGen *ag, Scope *scope, AstNode *nod
7700 }7698 }
7701 }7699 }
77027700
7703 ZigFn *fn_entry = exec_fn_entry(ag->exec);7701 if (!ag->fn) {
7704 if (!fn_entry) {
7705 add_node_error(ag->codegen, node, buf_sprintf("await outside function definition"));7702 add_node_error(ag->codegen, node, buf_sprintf("await outside function definition"));
7706 return ag->codegen->invalid_inst_src;7703 return ag->codegen->invalid_inst_src;
7707 }7704 }
...@@ -7726,8 +7723,7 @@ static IrInstSrc *ir_gen_await_expr(Stage1AstGen *ag, Scope *scope, AstNode *nod...@@ -7726,8 +7723,7 @@ static IrInstSrc *ir_gen_await_expr(Stage1AstGen *ag, Scope *scope, AstNode *nod
7726static IrInstSrc *ir_gen_suspend(Stage1AstGen *ag, Scope *parent_scope, AstNode *node) {7723static IrInstSrc *ir_gen_suspend(Stage1AstGen *ag, Scope *parent_scope, AstNode *node) {
7727 assert(node->type == NodeTypeSuspend);7724 assert(node->type == NodeTypeSuspend);
77287725
7729 ZigFn *fn_entry = exec_fn_entry(ag->exec);7726 if (!ag->fn) {
7730 if (!fn_entry) {
7731 add_node_error(ag->codegen, node, buf_sprintf("suspend outside function definition"));7727 add_node_error(ag->codegen, node, buf_sprintf("suspend outside function definition"));
7732 return ag->codegen->invalid_inst_src;7728 return ag->codegen->invalid_inst_src;
7733 }7729 }
...@@ -8019,7 +8015,7 @@ static IrInstSrc *ir_gen_node_extra(Stage1AstGen *ag, AstNode *node, Scope *scop...@@ -8019,7 +8015,7 @@ static IrInstSrc *ir_gen_node_extra(Stage1AstGen *ag, AstNode *node, Scope *scop
8019 }8015 }
8020 Scope *child_scope;8016 Scope *child_scope;
8021 if (ag->exec->is_inline ||8017 if (ag->exec->is_inline ||
8022 (ag->exec->fn_entry != nullptr && ag->exec->fn_entry->child_scope == scope))8018 (ag->fn != nullptr && ag->fn->child_scope == scope))
8023 {8019 {
8024 child_scope = scope;8020 child_scope = scope;
8025 } else {8021 } else {
...@@ -8038,13 +8034,16 @@ static IrInstSrc *ir_gen_node(Stage1AstGen *ag, AstNode *node, Scope *scope) {...@@ -8038,13 +8034,16 @@ static IrInstSrc *ir_gen_node(Stage1AstGen *ag, AstNode *node, Scope *scope) {
8038 return ir_gen_node_extra(ag, node, scope, LValNone, nullptr);8034 return ir_gen_node_extra(ag, node, scope, LValNone, nullptr);
8039}8035}
80408036
8041bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, Stage1Zir *ir_executable) {8037bool stage1_astgen(CodeGen *codegen, AstNode *node, Scope *scope, Stage1Zir *ir_executable,
8038 ZigFn *fn)
8039{
8042 assert(node->owner);8040 assert(node->owner);
80438041
8044 Stage1AstGen ir_builder = {0};8042 Stage1AstGen ir_builder = {0};
8045 Stage1AstGen *ag = &ir_builder;8043 Stage1AstGen *ag = &ir_builder;
80468044
8047 ag->codegen = codegen;8045 ag->codegen = codegen;
8046 ag->fn = fn;
8048 ag->exec = ir_executable;8047 ag->exec = ir_executable;
8049 ag->main_block_node = node;8048 ag->main_block_node = node;
80508049
...@@ -8076,15 +8075,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, Stage1Zir *ir_executa...@@ -8076,15 +8075,10 @@ bool ir_gen(CodeGen *codegen, AstNode *node, Scope *scope, Stage1Zir *ir_executa
8076 return true;8075 return true;
8077}8076}
80788077
8079bool ir_gen_fn(CodeGen *codegen, ZigFn *fn_entry) {8078bool stage1_astgen_fn(CodeGen *codegen, ZigFn *fn) {
8080 assert(fn_entry);8079 assert(fn != nullptr);
80818080 assert(fn->child_scope != nullptr);
8082 Stage1Zir *ir_executable = fn_entry->ir_executable;8081 return stage1_astgen(codegen, fn->body_node, fn->child_scope, fn->ir_executable, fn);
8083 AstNode *body_node = fn_entry->body_node;
8084
8085 assert(fn_entry->child_scope);
8086
8087 return ir_gen(codegen, body_node, fn_entry->child_scope, ir_executable);
8088}8082}
80898083
8090void invalidate_exec(Stage1Zir *exec, ErrorMsg *msg) {8084void invalidate_exec(Stage1Zir *exec, ErrorMsg *msg) {
src/stage1/astgen.hpp+2-2
...@@ -10,8 +10,8 @@...@@ -10,8 +10,8 @@
1010
11#include "all_types.hpp"11#include "all_types.hpp"
1212
13bool ir_gen(CodeGen *g, AstNode *node, Scope *scope, Stage1Zir *ir_executable);13bool stage1_astgen(CodeGen *g, AstNode *node, Scope *scope, Stage1Zir *ir_executable, ZigFn *fn);
14bool ir_gen_fn(CodeGen *g, ZigFn *fn_entry);14bool stage1_astgen_fn(CodeGen *g, ZigFn *fn_entry);
1515
16bool ir_inst_src_has_side_effects(IrInstSrc *inst);16bool ir_inst_src_has_side_effects(IrInstSrc *inst);
1717
src/stage1/ir.cpp+28-27
...@@ -49,6 +49,7 @@ struct IrAnalyze {...@@ -49,6 +49,7 @@ struct IrAnalyze {
49 Stage1Air *parent_exec;49 Stage1Air *parent_exec;
50 size_t *backward_branch_count;50 size_t *backward_branch_count;
51 size_t *backward_branch_quota;51 size_t *backward_branch_quota;
52 ZigFn *fn;
5253
53 // For the purpose of using in a debugger54 // For the purpose of using in a debugger
54 void dump();55 void dump();
...@@ -4259,7 +4260,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -4259,7 +4260,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
4259 continue;4260 continue;
4260 }4261 }
4261 bool allow_infer = cur_type->data.error_set.infer_fn != nullptr &&4262 bool allow_infer = cur_type->data.error_set.infer_fn != nullptr &&
4262 cur_type->data.error_set.infer_fn == ira->new_irb.exec->fn_entry;4263 cur_type->data.error_set.infer_fn == ira->fn;
4263 if (!allow_infer && !resolve_inferred_error_set(ira->codegen, cur_type, cur_inst->base.source_node)) {4264 if (!allow_infer && !resolve_inferred_error_set(ira->codegen, cur_type, cur_inst->base.source_node)) {
4264 return ira->codegen->builtin_types.entry_invalid;4265 return ira->codegen->builtin_types.entry_invalid;
4265 }4266 }
...@@ -4327,7 +4328,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -4327,7 +4328,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
4327 }4328 }
4328 ZigType *cur_err_set_type = cur_type->data.error_union.err_set_type;4329 ZigType *cur_err_set_type = cur_type->data.error_union.err_set_type;
4329 bool allow_infer = cur_err_set_type->data.error_set.infer_fn != nullptr &&4330 bool allow_infer = cur_err_set_type->data.error_set.infer_fn != nullptr &&
4330 cur_err_set_type->data.error_set.infer_fn == ira->new_irb.exec->fn_entry;4331 cur_err_set_type->data.error_set.infer_fn == ira->fn;
4331 if (!allow_infer && !resolve_inferred_error_set(ira->codegen, cur_err_set_type, cur_inst->base.source_node)) {4332 if (!allow_infer && !resolve_inferred_error_set(ira->codegen, cur_err_set_type, cur_inst->base.source_node)) {
4332 return ira->codegen->builtin_types.entry_invalid;4333 return ira->codegen->builtin_types.entry_invalid;
4333 }4334 }
...@@ -4382,7 +4383,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -4382,7 +4383,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
43824383
4383 if (cur_type->id == ZigTypeIdErrorSet) {4384 if (cur_type->id == ZigTypeIdErrorSet) {
4384 bool allow_infer = cur_type->data.error_set.infer_fn != nullptr &&4385 bool allow_infer = cur_type->data.error_set.infer_fn != nullptr &&
4385 cur_type->data.error_set.infer_fn == ira->new_irb.exec->fn_entry;4386 cur_type->data.error_set.infer_fn == ira->fn;
4386 if (!allow_infer && !resolve_inferred_error_set(ira->codegen, cur_type, cur_inst->base.source_node)) {4387 if (!allow_infer && !resolve_inferred_error_set(ira->codegen, cur_type, cur_inst->base.source_node)) {
4387 return ira->codegen->builtin_types.entry_invalid;4388 return ira->codegen->builtin_types.entry_invalid;
4388 }4389 }
...@@ -4401,7 +4402,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -4401,7 +4402,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
4401 if (prev_type->id == ZigTypeIdErrorUnion) {4402 if (prev_type->id == ZigTypeIdErrorUnion) {
4402 err_set_type = prev_type->data.error_union.err_set_type;4403 err_set_type = prev_type->data.error_union.err_set_type;
4403 allow_infer = err_set_type->data.error_set.infer_fn != nullptr &&4404 allow_infer = err_set_type->data.error_set.infer_fn != nullptr &&
4404 err_set_type->data.error_set.infer_fn == ira->new_irb.exec->fn_entry;4405 err_set_type->data.error_set.infer_fn == ira->fn;
4405 } else {4406 } else {
4406 err_set_type = cur_type;4407 err_set_type = cur_type;
4407 }4408 }
...@@ -4465,9 +4466,9 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -4465,9 +4466,9 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
4465 continue;4466 continue;
44664467
4467 bool allow_infer_prev = prev_err_set_type->data.error_set.infer_fn != nullptr &&4468 bool allow_infer_prev = prev_err_set_type->data.error_set.infer_fn != nullptr &&
4468 prev_err_set_type->data.error_set.infer_fn == ira->new_irb.exec->fn_entry;4469 prev_err_set_type->data.error_set.infer_fn == ira->fn;
4469 bool allow_infer_cur = cur_err_set_type->data.error_set.infer_fn != nullptr &&4470 bool allow_infer_cur = cur_err_set_type->data.error_set.infer_fn != nullptr &&
4470 cur_err_set_type->data.error_set.infer_fn == ira->new_irb.exec->fn_entry;4471 cur_err_set_type->data.error_set.infer_fn == ira->fn;
44714472
4472 if (!allow_infer_prev && !resolve_inferred_error_set(ira->codegen, prev_err_set_type, cur_inst->base.source_node)) {4473 if (!allow_infer_prev && !resolve_inferred_error_set(ira->codegen, prev_err_set_type, cur_inst->base.source_node)) {
4473 return ira->codegen->builtin_types.entry_invalid;4474 return ira->codegen->builtin_types.entry_invalid;
...@@ -4651,7 +4652,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -4651,7 +4652,7 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
4651 if (err_set_type != nullptr) {4652 if (err_set_type != nullptr) {
4652 ZigType *cur_err_set_type = cur_type->data.error_union.err_set_type;4653 ZigType *cur_err_set_type = cur_type->data.error_union.err_set_type;
4653 bool allow_infer = cur_err_set_type->data.error_set.infer_fn != nullptr &&4654 bool allow_infer = cur_err_set_type->data.error_set.infer_fn != nullptr &&
4654 cur_err_set_type->data.error_set.infer_fn == ira->new_irb.exec->fn_entry;4655 cur_err_set_type->data.error_set.infer_fn == ira->fn;
4655 if (!allow_infer && !resolve_inferred_error_set(ira->codegen, cur_err_set_type, cur_inst->base.source_node)) {4656 if (!allow_infer && !resolve_inferred_error_set(ira->codegen, cur_err_set_type, cur_inst->base.source_node)) {
4656 return ira->codegen->builtin_types.entry_invalid;4657 return ira->codegen->builtin_types.entry_invalid;
4657 }4658 }
...@@ -5595,11 +5596,10 @@ Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,...@@ -5595,11 +5596,10 @@ Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
5595 ir_executable->source_node = source_node;5596 ir_executable->source_node = source_node;
5596 ir_executable->name = exec_name;5597 ir_executable->name = exec_name;
5597 ir_executable->is_inline = true;5598 ir_executable->is_inline = true;
5598 ir_executable->fn_entry = fn_entry;
5599 ir_executable->c_import_buf = c_import_buf;5599 ir_executable->c_import_buf = c_import_buf;
5600 ir_executable->begin_scope = scope;5600 ir_executable->begin_scope = scope;
56015601
5602 if (!ir_gen(codegen, node, scope, ir_executable))5602 if (!stage1_astgen(codegen, node, scope, ir_executable, fn_entry))
5603 return ErrorSemanticAnalyzeFail;5603 return ErrorSemanticAnalyzeFail;
56045604
5605 if (ir_executable->first_err_trace_msg != nullptr) {5605 if (ir_executable->first_err_trace_msg != nullptr) {
...@@ -5618,12 +5618,12 @@ Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,...@@ -5618,12 +5618,12 @@ Error ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node,
5618 analyzed_executable->source_exec = ir_executable;5618 analyzed_executable->source_exec = ir_executable;
5619 analyzed_executable->name = exec_name;5619 analyzed_executable->name = exec_name;
5620 analyzed_executable->is_inline = true;5620 analyzed_executable->is_inline = true;
5621 analyzed_executable->fn_entry = fn_entry;
5622 analyzed_executable->c_import_buf = c_import_buf;5621 analyzed_executable->c_import_buf = c_import_buf;
5623 analyzed_executable->begin_scope = scope;5622 analyzed_executable->begin_scope = scope;
5624 ZigType *result_type = ir_analyze(codegen, ir_executable, analyzed_executable,5623 ZigType *result_type = ir_analyze(codegen, ir_executable, analyzed_executable,
5625 backward_branch_count, backward_branch_quota,5624 backward_branch_count, backward_branch_quota,
5626 return_ptr->type->data.pointer.child_type, expected_type_source_node, return_ptr);5625 return_ptr->type->data.pointer.child_type, expected_type_source_node, return_ptr,
5626 fn_entry);
5627 if (type_is_invalid(result_type)) {5627 if (type_is_invalid(result_type)) {
5628 return ErrorSemanticAnalyzeFail;5628 return ErrorSemanticAnalyzeFail;
5629 }5629 }
...@@ -11025,7 +11025,7 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV...@@ -11025,7 +11025,7 @@ static IrInstGen *ir_analyze_instruction_decl_var(IrAnalyze *ira, IrInstSrcDeclV
11025 return ira->codegen->invalid_inst_gen;11025 return ira->codegen->invalid_inst_gen;
11026 }11026 }
1102711027
11028 ZigFn *fn_entry = ira->new_irb.exec->fn_entry;11028 ZigFn *fn_entry = ira->fn;
11029 if (fn_entry)11029 if (fn_entry)
11030 fn_entry->variable_list.append(var);11030 fn_entry->variable_list.append(var);
1103111031
...@@ -11421,9 +11421,9 @@ static IrInstGen *ir_analyze_instruction_extern(IrAnalyze *ira, IrInstSrcExtern...@@ -11421,9 +11421,9 @@ static IrInstGen *ir_analyze_instruction_extern(IrAnalyze *ira, IrInstSrcExtern
11421 is_thread_local, expr_type);11421 is_thread_local, expr_type);
11422}11422}
1142311423
11424static bool exec_has_err_ret_trace(CodeGen *g, Stage1Zir *exec) {11424static bool ira_has_err_ret_trace(IrAnalyze *ira) {
11425 ZigFn *fn_entry = exec->fn_entry;11425 ZigFn *fn = ira->fn;
11426 return fn_entry != nullptr && fn_entry->calls_or_awaits_errorable_fn && g->have_err_ret_tracing;11426 return fn != nullptr && fn->calls_or_awaits_errorable_fn && ira->codegen->have_err_ret_tracing;
11427}11427}
1142811428
11429static IrInstGen *ir_analyze_instruction_error_return_trace(IrAnalyze *ira,11429static IrInstGen *ir_analyze_instruction_error_return_trace(IrAnalyze *ira,
...@@ -11432,7 +11432,7 @@ static IrInstGen *ir_analyze_instruction_error_return_trace(IrAnalyze *ira,...@@ -11432,7 +11432,7 @@ static IrInstGen *ir_analyze_instruction_error_return_trace(IrAnalyze *ira,
11432 ZigType *ptr_to_stack_trace_type = get_pointer_to_type(ira->codegen, get_stack_trace_type(ira->codegen), false);11432 ZigType *ptr_to_stack_trace_type = get_pointer_to_type(ira->codegen, get_stack_trace_type(ira->codegen), false);
11433 if (instruction->optional == IrInstErrorReturnTraceNull) {11433 if (instruction->optional == IrInstErrorReturnTraceNull) {
11434 ZigType *optional_type = get_optional_type(ira->codegen, ptr_to_stack_trace_type);11434 ZigType *optional_type = get_optional_type(ira->codegen, ptr_to_stack_trace_type);
11435 if (!exec_has_err_ret_trace(ira->codegen, ira->zir)) {11435 if (!ira_has_err_ret_trace(ira)) {
11436 IrInstGen *result = ir_const(ira, &instruction->base.base, optional_type);11436 IrInstGen *result = ir_const(ira, &instruction->base.base, optional_type);
11437 ZigValue *out_val = result->value;11437 ZigValue *out_val = result->value;
11438 assert(get_src_ptr_type(optional_type) != nullptr);11438 assert(get_src_ptr_type(optional_type) != nullptr);
...@@ -11504,7 +11504,7 @@ static IrInstGen *ir_analyze_alloca(IrAnalyze *ira, IrInst *source_inst, ZigType...@@ -11504,7 +11504,7 @@ static IrInstGen *ir_analyze_alloca(IrAnalyze *ira, IrInst *source_inst, ZigType
11504 PtrLenSingle, align, 0, 0, false);11504 PtrLenSingle, align, 0, 0, false);
1150511505
11506 if (!force_comptime) {11506 if (!force_comptime) {
11507 ZigFn *fn_entry = ira->new_irb.exec->fn_entry;11507 ZigFn *fn_entry = ira->fn;
11508 if (fn_entry != nullptr) {11508 if (fn_entry != nullptr) {
11509 fn_entry->alloca_gen_list.append(result);11509 fn_entry->alloca_gen_list.append(result);
11510 }11510 }
...@@ -11603,7 +11603,7 @@ static IrInstGen *ir_resolve_no_result_loc(IrAnalyze *ira, IrInst *suspend_sourc...@@ -11603,7 +11603,7 @@ static IrInstGen *ir_resolve_no_result_loc(IrAnalyze *ira, IrInst *suspend_sourc
11603 alloca_gen->base.value->type = get_pointer_to_type_extra(ira->codegen, value_type, false, false,11603 alloca_gen->base.value->type = get_pointer_to_type_extra(ira->codegen, value_type, false, false,
11604 PtrLenSingle, 0, 0, 0, false);11604 PtrLenSingle, 0, 0, 0, false);
11605 set_up_result_loc_for_inferred_comptime(ira, &alloca_gen->base);11605 set_up_result_loc_for_inferred_comptime(ira, &alloca_gen->base);
11606 ZigFn *fn_entry = ira->new_irb.exec->fn_entry;11606 ZigFn *fn_entry = ira->fn;
11607 if (fn_entry != nullptr && get_scope_typeof(suspend_source_instr->scope) == nullptr) {11607 if (fn_entry != nullptr && get_scope_typeof(suspend_source_instr->scope) == nullptr) {
11608 fn_entry->alloca_gen_list.append(alloca_gen);11608 fn_entry->alloca_gen_list.append(alloca_gen);
11609 }11609 }
...@@ -12131,7 +12131,7 @@ static IrInstGen *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrInstSr...@@ -12131,7 +12131,7 @@ static IrInstGen *ir_analyze_instruction_resolve_result(IrAnalyze *ira, IrInstSr
12131 if (result_loc != nullptr)12131 if (result_loc != nullptr)
12132 return result_loc;12132 return result_loc;
1213312133
12134 ZigFn *fn = ira->new_irb.exec->fn_entry;12134 ZigFn *fn = ira->fn;
12135 if (fn != nullptr && fn->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync &&12135 if (fn != nullptr && fn->type_entry->data.fn.fn_type_id.cc == CallingConventionAsync &&
12136 instruction->result_loc->id == ResultLocIdReturn)12136 instruction->result_loc->id == ResultLocIdReturn)
12137 {12137 {
...@@ -12849,7 +12849,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -12849,7 +12849,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
12849 }12849 }
12850 }12850 }
1285112851
12852 ZigFn *parent_fn_entry = ira->new_irb.exec->fn_entry;12852 ZigFn *parent_fn_entry = ira->fn;
12853 assert(parent_fn_entry);12853 assert(parent_fn_entry);
12854 for (size_t call_i = 0; call_i < args_len; call_i += 1) {12854 for (size_t call_i = 0; call_i < args_len; call_i += 1) {
12855 IrInstGen *arg = args_ptr[call_i];12855 IrInstGen *arg = args_ptr[call_i];
...@@ -13017,7 +13017,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,...@@ -13017,7 +13017,7 @@ static IrInstGen *ir_analyze_fn_call(IrAnalyze *ira, IrInst* source_instr,
13017 return ir_finish_anal(ira, &new_call_instruction->base);13017 return ir_finish_anal(ira, &new_call_instruction->base);
13018 }13018 }
1301913019
13020 ZigFn *parent_fn_entry = ira->new_irb.exec->fn_entry;13020 ZigFn *parent_fn_entry = ira->fn;
13021 assert(fn_type_id->return_type != nullptr);13021 assert(fn_type_id->return_type != nullptr);
13022 assert(parent_fn_entry != nullptr);13022 assert(parent_fn_entry != nullptr);
13023 if (fn_type_can_fail(fn_type_id)) {13023 if (fn_type_can_fail(fn_type_id)) {
...@@ -21069,7 +21069,7 @@ static IrInstGen *ir_analyze_instruction_frame_address(IrAnalyze *ira, IrInstSrc...@@ -21069,7 +21069,7 @@ static IrInstGen *ir_analyze_instruction_frame_address(IrAnalyze *ira, IrInstSrc
21069}21069}
2107021070
21071static IrInstGen *ir_analyze_instruction_frame_handle(IrAnalyze *ira, IrInstSrcFrameHandle *instruction) {21071static IrInstGen *ir_analyze_instruction_frame_handle(IrAnalyze *ira, IrInstSrcFrameHandle *instruction) {
21072 ZigFn *fn = ira->new_irb.exec->fn_entry;21072 ZigFn *fn = ira->fn;
21073 ir_assert(fn != nullptr, &instruction->base.base);21073 ir_assert(fn != nullptr, &instruction->base.base);
2107421074
21075 if (fn->inferred_async_node == nullptr) {21075 if (fn->inferred_async_node == nullptr) {
...@@ -22932,7 +22932,7 @@ static IrInstGen *ir_analyze_instruction_set_align_stack(IrAnalyze *ira, IrInstS...@@ -22932,7 +22932,7 @@ static IrInstGen *ir_analyze_instruction_set_align_stack(IrAnalyze *ira, IrInstS
22932 return ira->codegen->invalid_inst_gen;22932 return ira->codegen->invalid_inst_gen;
22933 }22933 }
2293422934
22935 ZigFn *fn_entry = ira->new_irb.exec->fn_entry;22935 ZigFn *fn_entry = ira->fn;
22936 if (fn_entry == nullptr) {22936 if (fn_entry == nullptr) {
22937 ir_add_error(ira, &instruction->base.base, buf_sprintf("@setAlignStack outside function"));22937 ir_add_error(ira, &instruction->base.base, buf_sprintf("@setAlignStack outside function"));
22938 return ira->codegen->invalid_inst_gen;22938 return ira->codegen->invalid_inst_gen;
...@@ -23952,7 +23952,7 @@ static IrInstGen *ir_analyze_instruction_suspend_finish(IrAnalyze *ira, IrInstSr...@@ -23952,7 +23952,7 @@ static IrInstGen *ir_analyze_instruction_suspend_finish(IrAnalyze *ira, IrInstSr
23952 ir_assert(begin_base->id == IrInstGenIdSuspendBegin, &instruction->base.base);23952 ir_assert(begin_base->id == IrInstGenIdSuspendBegin, &instruction->base.base);
23953 IrInstGenSuspendBegin *begin = reinterpret_cast<IrInstGenSuspendBegin *>(begin_base);23953 IrInstGenSuspendBegin *begin = reinterpret_cast<IrInstGenSuspendBegin *>(begin_base);
2395423954
23955 ZigFn *fn_entry = ira->new_irb.exec->fn_entry;23955 ZigFn *fn_entry = ira->fn;
23956 ir_assert(fn_entry != nullptr, &instruction->base.base);23956 ir_assert(fn_entry != nullptr, &instruction->base.base);
2395723957
23958 if (fn_entry->inferred_async_node == nullptr) {23958 if (fn_entry->inferred_async_node == nullptr) {
...@@ -24019,7 +24019,7 @@ static IrInstGen *ir_analyze_instruction_await(IrAnalyze *ira, IrInstSrcAwait *i...@@ -24019,7 +24019,7 @@ static IrInstGen *ir_analyze_instruction_await(IrAnalyze *ira, IrInstSrcAwait *i
2401924019
24020 ZigType *result_type = frame->value->type->data.any_frame.result_type;24020 ZigType *result_type = frame->value->type->data.any_frame.result_type;
2402124021
24022 ZigFn *fn_entry = ira->new_irb.exec->fn_entry;24022 ZigFn *fn_entry = ira->fn;
24023 ir_assert(fn_entry != nullptr, &instruction->base.base);24023 ir_assert(fn_entry != nullptr, &instruction->base.base);
2402424024
24025 // If it's not @Frame(func) then it's definitely a suspend point24025 // If it's not @Frame(func) then it's definitely a suspend point
...@@ -24465,16 +24465,17 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc...@@ -24465,16 +24465,17 @@ static IrInstGen *ir_analyze_instruction_base(IrAnalyze *ira, IrInstSrc *instruc
24465 zig_unreachable();24465 zig_unreachable();
24466}24466}
2446724467
24468// This function attempts to evaluate IR code while doing type checking and other analysis.24468// This function attempts to evaluate stage1 ZIR code while doing type checking and other analysis.
24469// It emits to a new Stage1Air which is partially evaluated IR code.24469// It emits to a new Stage1Air which is partially evaluated IR code.
24470ZigType *ir_analyze(CodeGen *codegen, Stage1Zir *stage1_zir, Stage1Air *stage1_air,24470ZigType *ir_analyze(CodeGen *codegen, Stage1Zir *stage1_zir, Stage1Air *stage1_air,
24471 size_t *backward_branch_count, size_t *backward_branch_quota,24471 size_t *backward_branch_count, size_t *backward_branch_quota,
24472 ZigType *expected_type, AstNode *expected_type_source_node, ZigValue *result_ptr)24472 ZigType *expected_type, AstNode *expected_type_source_node, ZigValue *result_ptr, ZigFn *fn)
24473{24473{
24474 assert(stage1_zir->first_err_trace_msg == nullptr);24474 assert(stage1_zir->first_err_trace_msg == nullptr);
24475 assert(expected_type == nullptr || !type_is_invalid(expected_type));24475 assert(expected_type == nullptr || !type_is_invalid(expected_type));
2447624476
24477 IrAnalyze *ira = heap::c_allocator.create<IrAnalyze>();24477 IrAnalyze *ira = heap::c_allocator.create<IrAnalyze>();
24478 ira->fn = fn;
24478 ira->backward_branch_count = backward_branch_count;24479 ira->backward_branch_count = backward_branch_count;
24479 ira->backward_branch_quota = backward_branch_quota;24480 ira->backward_branch_quota = backward_branch_quota;
24480 ira->ref_count = 1;24481 ira->ref_count = 1;
src/stage1/ir.hpp+2-2
...@@ -22,10 +22,10 @@ Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ZigValue *val);...@@ -22,10 +22,10 @@ Error ir_resolve_lazy(CodeGen *codegen, AstNode *source_node, ZigValue *val);
2222
23ZigType *ir_analyze(CodeGen *codegen, Stage1Zir *stage1_zir, Stage1Air *stage1_air,23ZigType *ir_analyze(CodeGen *codegen, Stage1Zir *stage1_zir, Stage1Air *stage1_air,
24 size_t *backward_branch_count, size_t *backward_branch_quota,24 size_t *backward_branch_count, size_t *backward_branch_quota,
25 ZigType *expected_type, AstNode *expected_type_source_node, ZigValue *result_ptr);25 ZigType *expected_type, AstNode *expected_type_source_node, ZigValue *result_ptr,
26 ZigFn *fn);
2627
27bool ir_inst_gen_has_side_effects(IrInstGen *inst);28bool ir_inst_gen_has_side_effects(IrInstGen *inst);
28bool ir_inst_src_has_side_effects(IrInstSrc *inst);
2929
30struct IrAnalyze;30struct IrAnalyze;
31ZigValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ZigValue *const_val,31ZigValue *const_ptr_pointee(IrAnalyze *ira, CodeGen *codegen, ZigValue *const_val,
src/stage1/ir_print.cpp+1
...@@ -8,6 +8,7 @@...@@ -8,6 +8,7 @@
8#include "all_types.hpp"8#include "all_types.hpp"
9#include "analyze.hpp"9#include "analyze.hpp"
10#include "ir.hpp"10#include "ir.hpp"
11#include "astgen.hpp"
11#include "ir_print.hpp"12#include "ir_print.hpp"
12#include "os.hpp"13#include "os.hpp"
1314