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 {
8686
8787struct Expr {
8888 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
9390 LLVMValueRef const_llvm_val;
9491 ConstExprValue const_val;
......@@ -254,6 +251,7 @@ struct AstNodeVariableDeclaration {
254251 // populated by semantic analyzer
255252 TopLevelDecl top_level_decl;
256253 Expr resolved_expr;
254 VariableTableEntry *variable;
257255};
258256
259257struct AstNodeErrorValueDecl {
......@@ -440,7 +438,6 @@ struct AstNodeIfVarExpr {
440438
441439 // populated by semantic analyzer
442440 TypeTableEntry *type;
443 BlockContext *block_context;
444441 Expr resolved_expr;
445442};
446443
......@@ -464,7 +461,6 @@ struct AstNodeForExpr {
464461 // populated by semantic analyzer
465462 bool contains_break;
466463 Expr resolved_expr;
467 BlockContext *block_context;
468464 VariableTableEntry *elem_var;
469465 VariableTableEntry *index_var;
470466};
......@@ -684,6 +680,9 @@ struct AstNode {
684680 uint32_t create_index; // for determinism purposes
685681 ImportTableEntry *owner;
686682 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;
687686 union {
688687 AstNodeRoot root;
689688 AstNodeRootExportDecl root_export_decl;
......@@ -1007,8 +1006,6 @@ struct CodeGen {
10071006 OutType out_type;
10081007 FnTableEntry *cur_fn;
10091008 LLVMValueRef cur_ret_ptr;
1010 // TODO remove this in favor of get_resolved_expr(expr_node)->context
1011 BlockContext *cur_block_context;
10121009 ZigList<LLVMBasicBlockRef> break_block_stack;
10131010 ZigList<LLVMBasicBlockRef> continue_block_stack;
10141011 bool c_stdint_used;
src/analyze.cpp+12-3
......@@ -1663,6 +1663,8 @@ static TypeTableEntry *analyze_container_init_expr(CodeGen *g, ImportTableEntry
16631663 AstNode *val_field_node = container_init_expr->entries.at(i);
16641664 assert(val_field_node->type == NodeTypeStructValueField);
16651665
1666 val_field_node->block_context = context;
1667
16661668 TypeStructField *type_field = find_struct_type_field(container_type,
16671669 &val_field_node->data.struct_val_field.name);
16681670
......@@ -2158,6 +2160,7 @@ static TypeTableEntry *analyze_lvalue(CodeGen *g, ImportTableEntry *import, Bloc
21582160 AstNode *lhs_node, LValPurpose purpose, bool is_ptr_const)
21592161{
21602162 TypeTableEntry *expected_rhs_type = nullptr;
2163 lhs_node->block_context = block_context;
21612164 if (lhs_node->type == NodeTypeSymbol) {
21622165 Buf *name = &lhs_node->data.symbol_expr.symbol;
21632166 if (purpose == LValPurposeAddressOf) {
......@@ -2520,6 +2523,7 @@ static TypeTableEntry *analyze_unwrap_error_expr(CodeGen *g, ImportTableEntry *i
25202523 BlockContext *child_context;
25212524 if (var_node) {
25222525 child_context = new_block_context(node, parent_context);
2526 var_node->block_context = child_context;
25232527 Buf *var_name = &var_node->data.symbol_expr.symbol;
25242528 node->data.unwrap_err_expr.var = add_local_var(g, var_node, child_context, var_name,
25252529 g->builtin_types.entry_pure_error, true);
......@@ -2601,6 +2605,8 @@ static VariableTableEntry *analyze_variable_declaration_raw(CodeGen *g, ImportTa
26012605 VariableTableEntry *var = add_local_var(g, source_node, context,
26022606 &variable_declaration->symbol, type, is_const);
26032607
2608 variable_declaration->variable = var;
2609
26042610
26052611 bool is_pub = (variable_declaration->visib_mod != VisibModPrivate);
26062612 if (is_pub) {
......@@ -2785,15 +2791,16 @@ static TypeTableEntry *analyze_for_expr(CodeGen *g, ImportTableEntry *import, Bl
27852791 }
27862792
27872793 BlockContext *child_context = new_block_context(node, context);
2788 node->data.for_expr.block_context = child_context;
27892794
27902795 AstNode *elem_var_node = node->data.for_expr.elem_node;
2796 elem_var_node->block_context = child_context;
27912797 Buf *elem_var_name = &elem_var_node->data.symbol_expr.symbol;
27922798 node->data.for_expr.elem_var = add_local_var(g, elem_var_node, child_context, elem_var_name, child_type, true);
27932799
27942800 AstNode *index_var_node = node->data.for_expr.index_node;
27952801 if (index_var_node) {
27962802 Buf *index_var_name = &index_var_node->data.symbol_expr.symbol;
2803 index_var_node->block_context = child_context;
27972804 node->data.for_expr.index_var = add_local_var(g, index_var_node, child_context, index_var_name,
27982805 g->builtin_types.entry_isize, true);
27992806 } else {
......@@ -2872,7 +2879,6 @@ static TypeTableEntry *analyze_if_var_expr(CodeGen *g, ImportTableEntry *import,
28722879 assert(node->type == NodeTypeIfVarExpr);
28732880
28742881 BlockContext *child_context = new_block_context(node, context);
2875 node->data.if_var_expr.block_context = child_context;
28762882
28772883 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
34123418 }
34133419
34143420 if (fn_ref_expr->type == NodeTypeFieldAccessExpr) {
3421 fn_ref_expr->block_context = context;
34153422 AstNode *first_param_expr = fn_ref_expr->data.field_access_expr.struct_expr;
34163423 TypeTableEntry *struct_type = analyze_expression(g, import, context, nullptr, first_param_expr);
34173424 Buf *name = &fn_ref_expr->data.field_access_expr.field_name;
......@@ -3724,6 +3731,7 @@ static TypeTableEntry *analyze_switch_expr(CodeGen *g, ImportTableEntry *import,
37243731 if (var_node) {
37253732 assert(var_node->type == NodeTypeSymbol);
37263733 Buf *var_name = &var_node->data.symbol_expr.symbol;
3734 var_node->block_context = child_context;
37273735 prong_node->data.switch_prong.var = add_local_var(g, var_node, child_context, var_name,
37283736 var_type, true);
37293737 }
......@@ -3802,6 +3810,7 @@ static TypeTableEntry *analyze_block_expr(CodeGen *g, ImportTableEntry *import,
38023810 for (int i = 0; i < node->data.block.statements.length; i += 1) {
38033811 AstNode *child = node->data.block.statements.at(i);
38043812 if (child->type == NodeTypeLabel) {
3813 child->block_context = child_context;
38053814 LabelTableEntry *label_entry = child->data.label.label_entry;
38063815 assert(label_entry);
38073816 label_entry->entered_from_fallthrough = (return_type->id != TypeTableEntryIdUnreachable);
......@@ -3987,7 +3996,7 @@ static TypeTableEntry *analyze_expression(CodeGen *g, ImportTableEntry *import,
39873996
39883997 Expr *expr = get_resolved_expr(node);
39893998 expr->type_entry = return_type;
3990 expr->block_context = context;
3999 node->block_context = context;
39914000
39924001 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);
6868static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *expr_node, AstNode *node, TypeTableEntry **out_type_entry);
6969static LLVMValueRef gen_field_access_expr(CodeGen *g, AstNode *node, bool is_lvalue);
7070static 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);
7272static LLVMValueRef gen_assign_raw(CodeGen *g, AstNode *source_node, BinOpType bin_op,
7373 LLVMValueRef target_ref, LLVMValueRef value,
7474 TypeTableEntry *op1_type, TypeTableEntry *op2_type);
......@@ -82,10 +82,8 @@ static TypeTableEntry *get_type_for_type_node(AstNode *node) {
8282}
8383
8484static void add_debug_source_node(CodeGen *g, AstNode *node) {
85 if (!g->cur_block_context)
86 return;
87 LLVMZigSetCurrentDebugLocation(g->builder, node->line + 1, node->column + 1,
88 g->cur_block_context->di_scope);
85 assert(node->block_context);
86 LLVMZigSetCurrentDebugLocation(g->builder, node->line + 1, node->column + 1, node->block_context->di_scope);
8987}
9088
9189static TypeTableEntry *get_expr_type(AstNode *node) {
......@@ -557,7 +555,7 @@ static LLVMValueRef gen_field_ptr(CodeGen *g, AstNode *node, TypeTableEntry **ou
557555
558556 LLVMValueRef struct_ptr;
559557 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,
561559 &struct_expr_node->data.symbol_expr.symbol);
562560 assert(var);
563561
......@@ -745,7 +743,7 @@ static LLVMValueRef gen_lvalue(CodeGen *g, AstNode *expr_node, AstNode *node,
745743 LLVMValueRef target_ref;
746744
747745 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,
749747 &node->data.symbol_expr.symbol);
750748 assert(var);
751749 // semantic checking ensures no variables are constant
......@@ -1522,33 +1520,24 @@ static LLVMValueRef gen_if_var_expr(CodeGen *g, AstNode *node) {
15221520 assert(node->type == NodeTypeIfVarExpr);
15231521 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
15281523 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
15311526 // test if value is the maybe state
15321527 add_debug_source_node(g, node);
15331528 LLVMValueRef maybe_field_ptr = LLVMBuildStructGEP(g->builder, init_val, 1, "");
15341529 LLVMValueRef cond_value = LLVMBuildLoad(g->builder, maybe_field_ptr, "");
15351530
1536 g->cur_block_context = new_block_context;
1537
15381531 LLVMValueRef return_value = gen_if_bool_expr_raw(g, node, cond_value,
15391532 node->data.if_var_expr.then_block,
15401533 node->data.if_var_expr.else_node);
15411534
1542 g->cur_block_context = old_block_context;
15431535 return return_value;
15441536}
15451537
15461538static LLVMValueRef gen_block(CodeGen *g, AstNode *block_node, TypeTableEntry *implicit_return_type) {
15471539 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
15521541 LLVMValueRef return_value;
15531542 for (int i = 0; i < block_node->data.block.statements.length; i += 1) {
15541543 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
15561545 }
15571546
15581547 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;
15601551 }
1561
1562 g->cur_block_context = old_block_context;
1563
1564 return return_value;
15651552}
15661553
15671554static int find_asm_index(CodeGen *g, AstNode *node, AsmToken *tok) {
......@@ -1646,9 +1633,7 @@ static LLVMValueRef gen_asm_expr(CodeGen *g, AstNode *node) {
16461633 }
16471634
16481635 if (!is_return) {
1649 VariableTableEntry *variable = find_variable(
1650 get_resolved_expr(node)->block_context,
1651 &asm_output->variable_name);
1636 VariableTableEntry *variable = find_variable( node->block_context, &asm_output->variable_name);
16521637 assert(variable);
16531638 param_types[param_index] = LLVMTypeOf(variable->value_ref);
16541639 param_values[param_index] = variable->value_ref;
......@@ -1763,13 +1748,10 @@ static LLVMValueRef gen_while_expr(CodeGen *g, AstNode *node) {
17631748 assert(node->data.while_expr.condition);
17641749 assert(node->data.while_expr.body);
17651750
1766 BlockContext *old_block_context = g->cur_block_context;
1767
17681751 bool condition_always_true = node->data.while_expr.condition_always_true;
17691752 bool contains_break = node->data.while_expr.contains_break;
17701753 if (condition_always_true) {
17711754 // generate a forever loop
1772 g->cur_block_context = node->data.while_expr.block_context;
17731755
17741756 LLVMBasicBlockRef body_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "WhileBody");
17751757 LLVMBasicBlockRef end_block = nullptr;
......@@ -1806,7 +1788,6 @@ static LLVMValueRef gen_while_expr(CodeGen *g, AstNode *node) {
18061788 LLVMBuildBr(g->builder, cond_block);
18071789
18081790 LLVMPositionBuilderAtEnd(g->builder, cond_block);
1809 g->cur_block_context = old_block_context;
18101791 LLVMValueRef cond_val = gen_expr(g, node->data.while_expr.condition);
18111792 add_debug_source_node(g, node->data.while_expr.condition);
18121793 LLVMBuildCondBr(g->builder, cond_val, body_block, end_block);
......@@ -1814,7 +1795,6 @@ static LLVMValueRef gen_while_expr(CodeGen *g, AstNode *node) {
18141795 LLVMPositionBuilderAtEnd(g->builder, body_block);
18151796 g->break_block_stack.append(end_block);
18161797 g->continue_block_stack.append(cond_block);
1817 g->cur_block_context = node->data.while_expr.block_context;
18181798 gen_expr(g, node->data.while_expr.body);
18191799 g->break_block_stack.pop();
18201800 g->continue_block_stack.pop();
......@@ -1826,7 +1806,6 @@ static LLVMValueRef gen_while_expr(CodeGen *g, AstNode *node) {
18261806 LLVMPositionBuilderAtEnd(g->builder, end_block);
18271807 }
18281808
1829 g->cur_block_context = old_block_context;
18301809 return nullptr;
18311810}
18321811
......@@ -1845,8 +1824,6 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
18451824 LLVMValueRef index_ptr = index_var->value_ref;
18461825 LLVMValueRef one_const = LLVMConstInt(g->builtin_types.entry_isize->type_ref, 1, false);
18471826
1848 BlockContext *old_block_context = g->cur_block_context;
1849
18501827 LLVMBasicBlockRef cond_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForCond");
18511828 LLVMBasicBlockRef body_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForBody");
18521829 LLVMBasicBlockRef end_block = LLVMAppendBasicBlock(g->cur_fn->fn_value, "ForEnd");
......@@ -1884,7 +1861,6 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
18841861 elem_var->type, child_type);
18851862 g->break_block_stack.append(end_block);
18861863 g->continue_block_stack.append(cond_block);
1887 g->cur_block_context = node->data.for_expr.block_context;
18881864 gen_expr(g, node->data.for_expr.body);
18891865 g->break_block_stack.pop();
18901866 g->continue_block_stack.pop();
......@@ -1896,7 +1872,6 @@ static LLVMValueRef gen_for_expr(CodeGen *g, AstNode *node) {
18961872 }
18971873
18981874 LLVMPositionBuilderAtEnd(g->builder, end_block);
1899 g->cur_block_context = old_block_context;
19001875 return nullptr;
19011876}
19021877
......@@ -1917,9 +1892,9 @@ static LLVMValueRef gen_continue(CodeGen *g, AstNode *node) {
19171892}
19181893
19191894static 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)
19211896{
1922 VariableTableEntry *variable = find_variable(block_context, &var_decl->symbol);
1897 VariableTableEntry *variable = var_decl->variable;
19231898
19241899 assert(variable);
19251900 assert(variable->is_ptr);
......@@ -2010,7 +1985,7 @@ static LLVMValueRef gen_var_decl_raw(CodeGen *g, AstNode *source_node, AstNodeVa
20101985 }
20111986
20121987 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);
20141989 LLVMZigInsertDeclareAtEnd(g->dbuilder, variable->value_ref, variable->di_loc_var, debug_loc,
20151990 LLVMGetInsertBlock(g->builder));
20161991 return nullptr;
......@@ -2028,8 +2003,7 @@ static LLVMValueRef gen_var_decl_expr(CodeGen *g, AstNode *node) {
20282003 }
20292004
20302005 LLVMValueRef init_val;
2031 return gen_var_decl_raw(g, node, &node->data.variable_declaration,
2032 get_resolved_expr(node)->block_context, false, &init_val);
2006 return gen_var_decl_raw(g, node, &node->data.variable_declaration, false, &init_val);
20332007}
20342008
20352009static LLVMValueRef gen_symbol(CodeGen *g, AstNode *node) {
......@@ -2498,8 +2472,6 @@ static void do_code_gen(CodeGen *g) {
24982472 block_context->di_scope = LLVMZigLexicalBlockToScope(di_block);
24992473 }
25002474
2501 g->cur_block_context = block_context;
2502
25032475 for (int var_i = 0; var_i < block_context->variable_list.length; var_i += 1) {
25042476 VariableTableEntry *var = block_context->variable_list.at(var_i);
25052477