authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-25 16:37:45-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-01-25 16:37:45-07:00
logfb85d3a0a2fd0774f78a38bbb63ac4ceb537d3ee
tree7e498822a2a9a73412588091d362067b91934ab5
parentdeb35868841fc3eb99228fe102a5b8ed1991d51f

codegen: get rid of cur_block_context


3 files changed, 30 insertions(+), 52 deletions(-)

src/all_types.hpp+4-7
...@@ -86,9 +86,6 @@ struct ConstExprValue {...@@ -86,9 +86,6 @@ struct ConstExprValue {
8686
87struct Expr {87struct Expr {
88 TypeTableEntry *type_entry;88 TypeTableEntry *type_entry;
89 // the context in which this expression is evaluated.
90 // for blocks, this points to the containing scope, not the block's own scope for its children.
91 BlockContext *block_context;
9289
93 LLVMValueRef const_llvm_val;90 LLVMValueRef const_llvm_val;
94 ConstExprValue const_val;91 ConstExprValue const_val;
...@@ -254,6 +251,7 @@ struct AstNodeVariableDeclaration {...@@ -254,6 +251,7 @@ struct AstNodeVariableDeclaration {
254 // populated by semantic analyzer251 // populated by semantic analyzer
255 TopLevelDecl top_level_decl;252 TopLevelDecl top_level_decl;
256 Expr resolved_expr;253 Expr resolved_expr;
254 VariableTableEntry *variable;
257};255};
258256
259struct AstNodeErrorValueDecl {257struct AstNodeErrorValueDecl {
...@@ -440,7 +438,6 @@ struct AstNodeIfVarExpr {...@@ -440,7 +438,6 @@ struct AstNodeIfVarExpr {
440438
441 // populated by semantic analyzer439 // populated by semantic analyzer
442 TypeTableEntry *type;440 TypeTableEntry *type;
443 BlockContext *block_context;
444 Expr resolved_expr;441 Expr resolved_expr;
445};442};
446443
...@@ -464,7 +461,6 @@ struct AstNodeForExpr {...@@ -464,7 +461,6 @@ struct AstNodeForExpr {
464 // populated by semantic analyzer461 // populated by semantic analyzer
465 bool contains_break;462 bool contains_break;
466 Expr resolved_expr;463 Expr resolved_expr;
467 BlockContext *block_context;
468 VariableTableEntry *elem_var;464 VariableTableEntry *elem_var;
469 VariableTableEntry *index_var;465 VariableTableEntry *index_var;
470};466};
...@@ -684,6 +680,9 @@ struct AstNode {...@@ -684,6 +680,9 @@ struct AstNode {
684 uint32_t create_index; // for determinism purposes680 uint32_t create_index; // for determinism purposes
685 ImportTableEntry *owner;681 ImportTableEntry *owner;
686 AstNode **parent_field; // for AST rewriting682 AstNode **parent_field; // for AST rewriting
683 // the context in which this expression/node is evaluated.
684 // for blocks, this points to the containing scope, not the block's own scope for its children.
685 BlockContext *block_context;
687 union {686 union {
688 AstNodeRoot root;687 AstNodeRoot root;
689 AstNodeRootExportDecl root_export_decl;688 AstNodeRootExportDecl root_export_decl;
...@@ -1007,8 +1006,6 @@ struct CodeGen {...@@ -1007,8 +1006,6 @@ struct CodeGen {
1007 OutType out_type;1006 OutType out_type;
1008 FnTableEntry *cur_fn;1007 FnTableEntry *cur_fn;
1009 LLVMValueRef cur_ret_ptr;1008 LLVMValueRef cur_ret_ptr;
1010 // TODO remove this in favor of get_resolved_expr(expr_node)->context
1011 BlockContext *cur_block_context;
1012 ZigList<LLVMBasicBlockRef> break_block_stack;1009 ZigList<LLVMBasicBlockRef> break_block_stack;
1013 ZigList<LLVMBasicBlockRef> continue_block_stack;1010 ZigList<LLVMBasicBlockRef> continue_block_stack;
1014 bool c_stdint_used;1011 bool c_stdint_used;
src/analyze.cpp+12-3
...@@ -1663,6 +1663,8 @@ static TypeTableEntry *analyze_container_init_expr(CodeGen *g, ImportTableEntry...@@ -1663,6 +1663,8 @@ static TypeTableEntry *analyze_container_init_expr(CodeGen *g, ImportTableEntry
1663 AstNode *val_field_node = container_init_expr->entries.at(i);1663 AstNode *val_field_node = container_init_expr->entries.at(i);
1664 assert(val_field_node->type == NodeTypeStructValueField);1664 assert(val_field_node->type == NodeTypeStructValueField);
16651665
1666 val_field_node->block_context = context;
1667
1666 TypeStructField *type_field = find_struct_type_field(container_type,1668 TypeStructField *type_field = find_struct_type_field(container_type,
1667 &val_field_node->data.struct_val_field.name);1669 &val_field_node->data.struct_val_field.name);
16681670
...@@ -2158,6 +2160,7 @@ static TypeTableEntry *analyze_lvalue(CodeGen *g, ImportTableEntry *import, Bloc...@@ -2158,6 +2160,7 @@ static TypeTableEntry *analyze_lvalue(CodeGen *g, ImportTableEntry *import, Bloc
2158 AstNode *lhs_node, LValPurpose purpose, bool is_ptr_const)2160 AstNode *lhs_node, LValPurpose purpose, bool is_ptr_const)
2159{2161{
2160 TypeTableEntry *expected_rhs_type = nullptr;2162 TypeTableEntry *expected_rhs_type = nullptr;
2163 lhs_node->block_context = block_context;
2161 if (lhs_node->type == NodeTypeSymbol) {2164 if (lhs_node->type == NodeTypeSymbol) {
2162 Buf *name = &lhs_node->data.symbol_expr.symbol;2165 Buf *name = &lhs_node->data.symbol_expr.symbol;
2163 if (purpose == LValPurposeAddressOf) {2166 if (purpose == LValPurposeAddressOf) {
...@@ -2520,6 +2523,7 @@ static TypeTableEntry *analyze_unwrap_error_expr(CodeGen *g, ImportTableEntry *i...@@ -2520,6 +2523,7 @@ static TypeTableEntry *analyze_unwrap_error_expr(CodeGen *g, ImportTableEntry *i
2520 BlockContext *child_context;2523 BlockContext *child_context;
2521 if (var_node) {2524 if (var_node) {
2522 child_context = new_block_context(node, parent_context);2525 child_context = new_block_context(node, parent_context);
2526 var_node->block_context = child_context;
2523 Buf *var_name = &var_node->data.symbol_expr.symbol;2527 Buf *var_name = &var_node->data.symbol_expr.symbol;
2524 node->data.unwrap_err_expr.var = add_local_var(g, var_node, child_context, var_name,2528 node->data.unwrap_err_expr.var = add_local_var(g, var_node, child_context, var_name,
2525 g->builtin_types.entry_pure_error, true);2529 g->builtin_types.entry_pure_error, true);
...@@ -2601,6 +2605,8 @@ static VariableTableEntry *analyze_variable_declaration_raw(CodeGen *g, ImportTa...@@ -2601,6 +2605,8 @@ static VariableTableEntry *analyze_variable_declaration_raw(CodeGen *g, ImportTa
2601 VariableTableEntry *var = add_local_var(g, source_node, context,2605 VariableTableEntry *var = add_local_var(g, source_node, context,
2602 &variable_declaration->symbol, type, is_const);2606 &variable_declaration->symbol, type, is_const);
26032607
2608 variable_declaration->variable = var;
2609
26042610
2605 bool is_pub = (variable_declaration->visib_mod != VisibModPrivate);2611 bool is_pub = (variable_declaration->visib_mod != VisibModPrivate);
2606 if (is_pub) {2612 if (is_pub) {
...@@ -2785,15 +2791,16 @@ static TypeTableEntry *analyze_for_expr(CodeGen *g, ImportTableEntry *import, Bl...@@ -2785,15 +2791,16 @@ static TypeTableEntry *analyze_for_expr(CodeGen *g, ImportTableEntry *import, Bl
2785 }2791 }
27862792
2787 BlockContext *child_context = new_block_context(node, context);2793 BlockContext *child_context = new_block_context(node, context);
2788 node->data.for_expr.block_context = child_context;
27892794
2790 AstNode *elem_var_node = node->data.for_expr.elem_node;2795 AstNode *elem_var_node = node->data.for_expr.elem_node;
2796 elem_var_node->block_context = child_context;
2791 Buf *elem_var_name = &elem_var_node->data.symbol_expr.symbol;2797 Buf *elem_var_name = &elem_var_node->data.symbol_expr.symbol;
2792 node->data.for_expr.elem_var = add_local_var(g, elem_var_node, child_context, elem_var_name, child_type, true);2798 node->data.for_expr.elem_var = add_local_var(g, elem_var_node, child_context, elem_var_name, child_type, true);
27932799
2794 AstNode *index_var_node = node->data.for_expr.index_node;2800 AstNode *index_var_node = node->data.for_expr.index_node;
2795 if (index_var_node) {2801 if (index_var_node) {
2796 Buf *index_var_name = &index_var_node->data.symbol_expr.symbol;2802 Buf *index_var_name = &index_var_node->data.symbol_expr.symbol;
2803 index_var_node->block_context = child_context;
2797 node->data.for_expr.index_var = add_local_var(g, index_var_node, child_context, index_var_name,2804 node->data.for_expr.index_var = add_local_var(g, index_var_node, child_context, index_var_name,
2798 g->builtin_types.entry_isize, true);2805 g->builtin_types.entry_isize, true);
2799 } else {2806 } else {
...@@ -2872,7 +2879,6 @@ static TypeTableEntry *analyze_if_var_expr(CodeGen *g, ImportTableEntry *import,...@@ -2872,7 +2879,6 @@ static TypeTableEntry *analyze_if_var_expr(CodeGen *g, ImportTableEntry *import,
2872 assert(node->type == NodeTypeIfVarExpr);2879 assert(node->type == NodeTypeIfVarExpr);
28732880
2874 BlockContext *child_context = new_block_context(node, context);2881 BlockContext *child_context = new_block_context(node, context);
2875 node->data.if_var_expr.block_context = child_context;
28762882
2877 analyze_variable_declaration_raw(g, import, child_context, node, &node->data.if_var_expr.var_decl, true);2883 analyze_variable_declaration_raw(g, import, child_context, node, &node->data.if_var_expr.var_decl, true);
28782884
...@@ -3412,6 +3418,7 @@ static TypeTableEntry *analyze_fn_call_expr(CodeGen *g, ImportTableEntry *import...@@ -3412,6 +3418,7 @@ static TypeTableEntry *analyze_fn_call_expr(CodeGen *g, ImportTableEntry *import
3412 }3418 }
34133419
3414 if (fn_ref_expr->type == NodeTypeFieldAccessExpr) {3420 if (fn_ref_expr->type == NodeTypeFieldAccessExpr) {
3421 fn_ref_expr->block_context = context;
3415 AstNode *first_param_expr = fn_ref_expr->data.field_access_expr.struct_expr;3422 AstNode *first_param_expr = fn_ref_expr->data.field_access_expr.struct_expr;
3416 TypeTableEntry *struct_type = analyze_expression(g, import, context, nullptr, first_param_expr);3423 TypeTableEntry *struct_type = analyze_expression(g, import, context, nullptr, first_param_expr);
3417 Buf *name = &fn_ref_expr->data.field_access_expr.field_name;3424 Buf *name = &fn_ref_expr->data.field_access_expr.field_name;
...@@ -3724,6 +3731,7 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,...@@ -3724,6 +3731,7 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
3724 if (var_node) {3731 if (var_node) {
3725 assert(var_node->type == NodeTypeSymbol);3732 assert(var_node->type == NodeTypeSymbol);
3726 Buf *var_name = &var_node->data.symbol_expr.symbol;3733 Buf *var_name = &var_node->data.symbol_expr.symbol;
3734 var_node->block_context = child_context;
3727 prong_node->data.switch_prong.var = add_local_var(g, var_node, child_context, var_name,3735 prong_node->data.switch_prong.var = add_local_var(g, var_node, child_context, var_name,
3728 var_type, true);3736 var_type, true);
3729 }3737 }
...@@ -3802,6 +3810,7 @@ static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import,...@@ -3802,6 +3810,7 @@ static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import,
3802 for (int i = 0; i < node->data.block.statements.length; i += 1) {3810 for (int i = 0; i < node->data.block.statements.length; i += 1) {
3803 AstNode *child = node->data.block.statements.at(i);3811 AstNode *child = node->data.block.statements.at(i);
3804 if (child->type == NodeTypeLabel) {3812 if (child->type == NodeTypeLabel) {
3813 child->block_context = child_context;
3805 LabelTableEntry *label_entry = child->data.label.label_entry;3814 LabelTableEntry *label_entry = child->data.label.label_entry;
3806 assert(label_entry);3815 assert(label_entry);
3807 label_entry->entered_from_fallthrough = (return_type->id != TypeTableEntryIdUnreachable);3816 label_entry->entered_from_fallthrough = (return_type->id != TypeTableEntryIdUnreachable);
...@@ -3987,7 +3996,7 @@ static TypeTableEntry *analyze_expression(CodeGen *g, ImportTableEntry *import,...@@ -3987,7 +3996,7 @@ static TypeTableEntry *analyze_expression(CodeGen *g, ImportTableEntry *import,
39873996
3988 Expr *expr = get_resolved_expr(node);3997 Expr *expr = get_resolved_expr(node);
3989 expr->type_entry = return_type;3998 expr->type_entry = return_type;
3990 expr->block_context = context;3999 node->block_context = context;
39914000
3992 add_global_const_expr(g, expr);4001 add_global_const_expr(g, expr);
39934002
src/codegen.cpp+14-42
...@@ -68,7 +68,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *expr_node);...@@ -68,7 +68,7 @@ static LLVMValueRef gen_expr(CodeGen *g, AstNode *expr_node);
68static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *expr_node, AstNode *node, TypeTableEntry **out_type_entry);68static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *expr_node, AstNode *node, TypeTableEntry **out_type_entry);
69static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node, bool is_lvalue);69static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node, bool is_lvalue);
70static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVariableDeclaration *var_decl,70static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVariableDeclaration *var_decl,
71 BlockContext *block_context, bool unwrap_maybe, LLVMValueRef *init_val);71 bool unwrap_maybe, LLVMValueRef *init_val);
72static LLVMValueRef gen_assign_raw(CodeGen *g, AstNode *source_node, BinOpType bin_op,72static LLVMValueRef gen_assign_raw(CodeGen *g, AstNode *source_node, BinOpType bin_op,
73 LLVMValueRef target_ref, LLVMValueRef value,73 LLVMValueRef target_ref, LLVMValueRef value,
74 TypeTableEntry *op1_type, TypeTableEntry *op2_type);74 TypeTableEntry *op1_type, TypeTableEntry *op2_type);
...@@ -82,10 +82,8 @@ static TypeTableEntry *get_type_for_type_node(AstNode *node) {...@@ -82,10 +82,8 @@ static TypeTableEntry *get_type_for_type_node(AstNode *node) {
82}82}
8383
84static void add_debug_source_node(CodeGen *g, AstNode *node) {84static void add_debug_source_node(CodeGen *g, AstNode *node) {
85 if (!g->cur_block_context)85 assert(node->block_context);
86 return;86 LLVMZigSetCurrentDebugLocation(g->builder, node->line + 1, node->column + 1, node->block_context->di_scope);
87 LLVMZigSetCurrentDebugLocation(g->builder, node->line + 1, node->column + 1,
88 g->cur_block_context->di_scope);
89}87}
9088
91static TypeTableEntry *get_expr_type(AstNode *node) {89static TypeTableEntry *get_expr_type(AstNode *node) {
...@@ -557,7 +555,7 @@ static LLVMValueRef gen_field_ptr(CodeGen *g, AstNode *node, TypeTableEntry **ou...@@ -557,7 +555,7 @@ static LLVMValueRef gen_field_ptr(CodeGen *g, AstNode *node, TypeTableEntry **ou
557555
558 LLVMValueRef struct_ptr;556 LLVMValueRef struct_ptr;
559 if (struct_expr_node->type == NodeTypeSymbol) {557 if (struct_expr_node->type == NodeTypeSymbol) {
560 VariableTableEntry *var = find_variable(get_resolved_expr(struct_expr_node)->block_context,558 VariableTableEntry *var = find_variable(struct_expr_node->block_context,
561 &struct_expr_node->data.symbol_expr.symbol);559 &struct_expr_node->data.symbol_expr.symbol);
562 assert(var);560 assert(var);
563561
...@@ -745,7 +743,7 @@ static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *expr_node, AstNode *node,...@@ -745,7 +743,7 @@ static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *expr_node, AstNode *node,
745 LLVMValueRef target_ref;743 LLVMValueRef target_ref;
746744
747 if (node->type == NodeTypeSymbol) {745 if (node->type == NodeTypeSymbol) {
748 VariableTableEntry *var = find_variable(get_resolved_expr(expr_node)->block_context,746 VariableTableEntry *var = find_variable(expr_node->block_context,
749 &node->data.symbol_expr.symbol);747 &node->data.symbol_expr.symbol);
750 assert(var);748 assert(var);
751 // semantic checking ensures no variables are constant749 // semantic checking ensures no variables are constant
...@@ -1522,33 +1520,24 @@ static LLVMValueRef gen_if_var_expr(CodeGen *g, AstNode *node) {...@@ -1522,33 +1520,24 @@ static LLVMValueRef gen_if_var_expr(CodeGen *g, AstNode *node) {
1522 assert(node->type == NodeTypeIfVarExpr);1520 assert(node->type == NodeTypeIfVarExpr);
1523 assert(node->data.if_var_expr.var_decl.expr);1521 assert(node->data.if_var_expr.var_decl.expr);
15241522
1525 BlockContext *old_block_context = g->cur_block_context;
1526 BlockContext *new_block_context = node->data.if_var_expr.block_context;
1527
1528 LLVMValueRef init_val;1523 LLVMValueRef init_val;
1529 gen_var_decl_raw(g, node, &node->data.if_var_expr.var_decl, new_block_context, true, &init_val);1524 gen_var_decl_raw(g, node, &node->data.if_var_expr.var_decl, true, &init_val);
15301525
1531 // test if value is the maybe state1526 // test if value is the maybe state
1532 add_debug_source_node(g, node);1527 add_debug_source_node(g, node);
1533 LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, init_val, 1, "");1528 LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, init_val, 1, "");
1534 LLVMValueRef cond_value = LLVMBuildLoad(g->builder, maybe_field_ptr, "");1529 LLVMValueRef cond_value = LLVMBuildLoad(g->builder, maybe_field_ptr, "");
15351530
1536 g->cur_block_context = new_block_context;
1537
1538 LLVMValueRef return_value = gen_if_bool_expr_raw(g, node, cond_value,1531 LLVMValueRef return_value = gen_if_bool_expr_raw(g, node, cond_value,
1539 node->data.if_var_expr.then_block,1532 node->data.if_var_expr.then_block,
1540 node->data.if_var_expr.else_node);1533 node->data.if_var_expr.else_node);
15411534
1542 g->cur_block_context = old_block_context;
1543 return return_value;1535 return return_value;
1544}1536}
15451537
1546static LLVMValueRef gen_block(CodeGen *g, AstNode *block_node, TypeTableEntry *implicit_return_type) {1538static LLVMValueRef gen_block(CodeGen *g, AstNode *block_node, TypeTableEntry *implicit_return_type) {
1547 assert(block_node->type == NodeTypeBlock);1539 assert(block_node->type == NodeTypeBlock);
15481540
1549 BlockContext *old_block_context = g->cur_block_context;
1550 g->cur_block_context = block_node->data.block.block_context;
1551
1552 LLVMValueRef return_value;1541 LLVMValueRef return_value;
1553 for (int i = 0; i < block_node->data.block.statements.length; i += 1) {1542 for (int i = 0; i < block_node->data.block.statements.length; i += 1) {
1554 AstNode *statement_node = block_node->data.block.statements.at(i);1543 AstNode *statement_node = block_node->data.block.statements.at(i);
...@@ -1556,12 +1545,10 @@ static LLVMValueRef gen_block(CodeGen *g, AstNode *block_node, TypeTableEntry *i...@@ -1556,12 +1545,10 @@ static LLVMValueRef gen_block(CodeGen *g, AstNode *block_node, TypeTableEntry *i
1556 }1545 }
15571546
1558 if (implicit_return_type && implicit_return_type->id != TypeTableEntryIdUnreachable) {1547 if (implicit_return_type && implicit_return_type->id != TypeTableEntryIdUnreachable) {
1559 gen_return(g, block_node, return_value);1548 return gen_return(g, block_node, return_value);
1549 } else {
1550 return return_value;
1560 }1551 }
1561
1562 g->cur_block_context = old_block_context;
1563
1564 return return_value;
1565}1552}
15661553
1567static int find_asm_index(CodeGen *g, AstNode *node, AsmToken *tok) {1554static int find_asm_index(CodeGen *g, AstNode *node, AsmToken *tok) {
...@@ -1646,9 +1633,7 @@ static LLVMValueRef gen_asm_expr(CodeGen *g, AstNode *node) {...@@ -1646,9 +1633,7 @@ static LLVMValueRef gen_asm_expr(CodeGen *g, AstNode *node) {
1646 }1633 }
16471634
1648 if (!is_return) {1635 if (!is_return) {
1649 VariableTableEntry *variable = find_variable(1636 VariableTableEntry *variable = find_variable( node->block_context, &asm_output->variable_name);
1650 get_resolved_expr(node)->block_context,
1651 &asm_output->variable_name);
1652 assert(variable);1637 assert(variable);
1653 param_types[param_index] = LLVMTypeOf(variable->value_ref);1638 param_types[param_index] = LLVMTypeOf(variable->value_ref);
1654 param_values[param_index] = variable->value_ref;1639 param_values[param_index] = variable->value_ref;
...@@ -1763,13 +1748,10 @@ static LLVMValueRef gen_while_expr(CodeGen *g, AstNode *node) {...@@ -1763,13 +1748,10 @@ static LLVMValueRef gen_while_expr(CodeGen *g, AstNode *node) {
1763 assert(node->data.while_expr.condition);1748 assert(node->data.while_expr.condition);
1764 assert(node->data.while_expr.body);1749 assert(node->data.while_expr.body);
17651750
1766 BlockContext *old_block_context = g->cur_block_context;
1767
1768 bool condition_always_true = node->data.while_expr.condition_always_true;1751 bool condition_always_true = node->data.while_expr.condition_always_true;
1769 bool contains_break = node->data.while_expr.contains_break;1752 bool contains_break = node->data.while_expr.contains_break;
1770 if (condition_always_true) {1753 if (condition_always_true) {
1771 // generate a forever loop1754 // generate a forever loop
1772 g->cur_block_context = node->data.while_expr.block_context;
17731755
1774 LLVMBasicBlockRef body_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "WhileBody");1756 LLVMBasicBlockRef body_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "WhileBody");
1775 LLVMBasicBlockRef end_block = nullptr;1757 LLVMBasicBlockRef end_block = nullptr;
...@@ -1806,7 +1788,6 @@ static LLVMValueRef gen_while_expr(CodeGen *g, AstNode *node) {...@@ -1806,7 +1788,6 @@ static LLVMValueRef gen_while_expr(CodeGen *g, AstNode *node) {
1806 LLVMBuildBr(g->builder, cond_block);1788 LLVMBuildBr(g->builder, cond_block);
18071789
1808 LLVMPositionBuilderAtEnd(g->builder, cond_block);1790 LLVMPositionBuilderAtEnd(g->builder, cond_block);
1809 g->cur_block_context = old_block_context;
1810 LLVMValueRef cond_val = gen_expr(g, node->data.while_expr.condition);1791 LLVMValueRef cond_val = gen_expr(g, node->data.while_expr.condition);
1811 add_debug_source_node(g, node->data.while_expr.condition);1792 add_debug_source_node(g, node->data.while_expr.condition);
1812 LLVMBuildCondBr(g->builder, cond_val, body_block, end_block);1793 LLVMBuildCondBr(g->builder, cond_val, body_block, end_block);
...@@ -1814,7 +1795,6 @@ static LLVMValueRef gen_while_expr(CodeGen *g, AstNode *node) {...@@ -1814,7 +1795,6 @@ static LLVMValueRef gen_while_expr(CodeGen *g, AstNode *node) {
1814 LLVMPositionBuilderAtEnd(g->builder, body_block);1795 LLVMPositionBuilderAtEnd(g->builder, body_block);
1815 g->break_block_stack.append(end_block);1796 g->break_block_stack.append(end_block);
1816 g->continue_block_stack.append(cond_block);1797 g->continue_block_stack.append(cond_block);
1817 g->cur_block_context = node->data.while_expr.block_context;
1818 gen_expr(g, node->data.while_expr.body);1798 gen_expr(g, node->data.while_expr.body);
1819 g->break_block_stack.pop();1799 g->break_block_stack.pop();
1820 g->continue_block_stack.pop();1800 g->continue_block_stack.pop();
...@@ -1826,7 +1806,6 @@ static LLVMValueRef gen_while_expr(CodeGen *g, AstNode *node) {...@@ -1826,7 +1806,6 @@ static LLVMValueRef gen_while_expr(CodeGen *g, AstNode *node) {
1826 LLVMPositionBuilderAtEnd(g->builder, end_block);1806 LLVMPositionBuilderAtEnd(g->builder, end_block);
1827 }1807 }
18281808
1829 g->cur_block_context = old_block_context;
1830 return nullptr;1809 return nullptr;
1831}1810}
18321811
...@@ -1845,8 +1824,6 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {...@@ -1845,8 +1824,6 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
1845 LLVMValueRef index_ptr = index_var->value_ref;1824 LLVMValueRef index_ptr = index_var->value_ref;
1846 LLVMValueRef one_const = LLVMConstInt(g->builtin_types.entry_isize->type_ref, 1, false);1825 LLVMValueRef one_const = LLVMConstInt(g->builtin_types.entry_isize->type_ref, 1, false);
18471826
1848 BlockContext *old_block_context = g->cur_block_context;
1849
1850 LLVMBasicBlockRef cond_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForCond");1827 LLVMBasicBlockRef cond_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForCond");
1851 LLVMBasicBlockRef body_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForBody");1828 LLVMBasicBlockRef body_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForBody");
1852 LLVMBasicBlockRef end_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForEnd");1829 LLVMBasicBlockRef end_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForEnd");
...@@ -1884,7 +1861,6 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {...@@ -1884,7 +1861,6 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
1884 elem_var->type, child_type);1861 elem_var->type, child_type);
1885 g->break_block_stack.append(end_block);1862 g->break_block_stack.append(end_block);
1886 g->continue_block_stack.append(cond_block);1863 g->continue_block_stack.append(cond_block);
1887 g->cur_block_context = node->data.for_expr.block_context;
1888 gen_expr(g, node->data.for_expr.body);1864 gen_expr(g, node->data.for_expr.body);
1889 g->break_block_stack.pop();1865 g->break_block_stack.pop();
1890 g->continue_block_stack.pop();1866 g->continue_block_stack.pop();
...@@ -1896,7 +1872,6 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {...@@ -1896,7 +1872,6 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
1896 }1872 }
18971873
1898 LLVMPositionBuilderAtEnd(g->builder, end_block);1874 LLVMPositionBuilderAtEnd(g->builder, end_block);
1899 g->cur_block_context = old_block_context;
1900 return nullptr;1875 return nullptr;
1901}1876}
19021877
...@@ -1917,9 +1892,9 @@ static LLVMValueRef gen_continue(CodeGen *g, AstNode *node) {...@@ -1917,9 +1892,9 @@ static LLVMValueRef gen_continue(CodeGen *g, AstNode *node) {
1917}1892}
19181893
1919static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVariableDeclaration *var_decl,1894static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVariableDeclaration *var_decl,
1920 BlockContext *block_context, bool unwrap_maybe, LLVMValueRef *init_value)1895 bool unwrap_maybe, LLVMValueRef *init_value)
1921{1896{
1922 VariableTableEntry *variable = find_variable(block_context, &var_decl->symbol);1897 VariableTableEntry *variable = var_decl->variable;
19231898
1924 assert(variable);1899 assert(variable);
1925 assert(variable->is_ptr);1900 assert(variable->is_ptr);
...@@ -2010,7 +1985,7 @@ static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVa...@@ -2010,7 +1985,7 @@ static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVa
2010 }1985 }
20111986
2012 LLVMZigDILocation *debug_loc = LLVMZigGetDebugLoc(source_node->line + 1, source_node->column + 1,1987 LLVMZigDILocation *debug_loc = LLVMZigGetDebugLoc(source_node->line + 1, source_node->column + 1,
2013 g->cur_block_context->di_scope);1988 source_node->block_context->di_scope);
2014 LLVMZigInsertDeclareAtEnd(g->dbuilder, variable->value_ref, variable->di_loc_var, debug_loc,1989 LLVMZigInsertDeclareAtEnd(g->dbuilder, variable->value_ref, variable->di_loc_var, debug_loc,
2015 LLVMGetInsertBlock(g->builder));1990 LLVMGetInsertBlock(g->builder));
2016 return nullptr;1991 return nullptr;
...@@ -2028,8 +2003,7 @@ static LLVMValueRef gen_var_decl_expr(CodeGen *g, AstNode *node) {...@@ -2028,8 +2003,7 @@ static LLVMValueRef gen_var_decl_expr(CodeGen *g, AstNode *node) {
2028 }2003 }
20292004
2030 LLVMValueRef init_val;2005 LLVMValueRef init_val;
2031 return gen_var_decl_raw(g, node, &node->data.variable_declaration,2006 return gen_var_decl_raw(g, node, &node->data.variable_declaration, false, &init_val);
2032 get_resolved_expr(node)->block_context, false, &init_val);
2033}2007}
20342008
2035static LLVMValueRef gen_symbol(CodeGen *g, AstNode *node) {2009static LLVMValueRef gen_symbol(CodeGen *g, AstNode *node) {
...@@ -2498,8 +2472,6 @@ static void do_code_gen(CodeGen *g) {...@@ -2498,8 +2472,6 @@ static void do_code_gen(CodeGen *g) {
2498 block_context->di_scope = LLVMZigLexicalBlockToScope(di_block);2472 block_context->di_scope = LLVMZigLexicalBlockToScope(di_block);
2499 }2473 }
25002474
2501 g->cur_block_context = block_context;
2502
2503 for (int var_i = 0; var_i < block_context->variable_list.length; var_i += 1) {2475 for (int var_i = 0; var_i < block_context->variable_list.length; var_i += 1) {
2504 VariableTableEntry *var = block_context->variable_list.at(var_i);2476 VariableTableEntry *var = block_context->variable_list.at(var_i);
25052477