authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-03 14:13:57-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-03 14:13:57-04:00
logc8333d0cc9a2941ea35f1202073dabd9058beebc
tree4af6d5e7611cfb3b1f24626a5c8618bde7550f78
parent1a0111d4c36578b64d8532724afd9be9ca61d3d1

add concept of inline for, inline while, inline var


8 files changed, 217 insertions(+), 108 deletions(-)

doc/langref.md+3-3
......@@ -15,7 +15,7 @@ ErrorValueDecl = "error" Symbol ";"
1515
1616GlobalVarDecl = VariableDeclaration ";"
1717
18VariableDeclaration = ("var" | "const") Symbol option(":" TypeExpr) "=" Expression
18VariableDeclaration = option("inline") ("var" | "const") Symbol option(":" TypeExpr) "=" Expression
1919
2020ContainerDecl = ("struct" | "enum" | "union") Symbol option(ParamDeclList) "{" many(StructMember) "}"
2121
......@@ -79,9 +79,9 @@ SwitchProng = (list(SwitchItem, ",") | "else") "=>" option("|" Symbol "|") Expre
7979
8080SwitchItem = Expression | (Expression "..." Expression)
8181
82WhileExpression = "while" "(" Expression option(";" Expression) ")" Expression
82WhileExpression = option("inline") "while" "(" Expression option(";" Expression) ")" Expression
8383
84ForExpression = "for" "(" Expression ")" option("|" option("*") Symbol option("," Symbol) "|") Expression
84ForExpression = option("inline") "for" "(" Expression ")" option("|" option("*") Symbol option("," Symbol) "|") Expression
8585
8686BoolOrExpression = BoolAndExpression "||" BoolOrExpression | BoolAndExpression
8787
src/all_types.hpp+13-1
......@@ -301,6 +301,7 @@ struct AstNodeVariableDeclaration {
301301 TopLevelDecl top_level_decl;
302302 Buf *symbol;
303303 bool is_const;
304 bool is_inline;
304305 bool is_extern;
305306 // one or both of type and expr will be non null
306307 AstNode *type;
......@@ -522,6 +523,7 @@ struct AstNodeWhileExpr {
522523 AstNode *condition;
523524 AstNode *continue_expr;
524525 AstNode *body;
526 bool is_inline;
525527
526528 // populated by semantic analyzer
527529 bool condition_always_true;
......@@ -536,6 +538,7 @@ struct AstNodeForExpr {
536538 AstNode *index_node; // always a symbol, might be null
537539 AstNode *body;
538540 bool elem_is_ptr;
541 bool is_inline;
539542
540543 // populated by semantic analyzer
541544 bool contains_break;
......@@ -1337,7 +1340,9 @@ struct VariableTableEntry {
13371340 Buf name;
13381341 TypeTableEntry *type;
13391342 LLVMValueRef value_ref;
1340 bool is_const;
1343 bool src_is_const;
1344 bool gen_is_const;
1345 bool is_inline;
13411346 // which node is the declaration of the variable
13421347 AstNode *decl_node;
13431348 // which node contains the ConstExprValue for this variable's value
......@@ -1457,6 +1462,10 @@ struct IrInstruction {
14571462 size_t ref_count;
14581463 IrInstruction *other;
14591464 ReturnKnowledge return_knowledge;
1465 // if this is true, this instruction should not cause compile errors.
1466 // for example, we can write to a variable with src_is_const true but
1467 // gen_is_const false.
1468 bool gen_only;
14601469};
14611470
14621471struct IrInstructionCondBr {
......@@ -1465,12 +1474,14 @@ struct IrInstructionCondBr {
14651474 IrInstruction *condition;
14661475 IrBasicBlock *then_block;
14671476 IrBasicBlock *else_block;
1477 bool is_inline;
14681478};
14691479
14701480struct IrInstructionBr {
14711481 IrInstruction base;
14721482
14731483 IrBasicBlock *dest_block;
1484 bool is_inline;
14741485};
14751486
14761487struct IrInstructionSwitchBrCase {
......@@ -1485,6 +1496,7 @@ struct IrInstructionSwitchBr {
14851496 IrBasicBlock *else_block;
14861497 size_t case_count;
14871498 IrInstructionSwitchBrCase *cases;
1499 bool is_inline;
14881500};
14891501
14901502struct IrInstructionPhi {
src/analyze.cpp+5-4
......@@ -3026,7 +3026,7 @@ static bool var_is_pure(VariableTableEntry *var, BlockContext *context) {
30263026 // variable was declared in the current function, so it's OK.
30273027 return true;
30283028 }
3029 return var->is_const && var->type->deep_const;
3029 return var->src_is_const && var->type->deep_const;
30303030}
30313031
30323032static TypeTableEntry *analyze_var_ref(CodeGen *g, AstNode *source_node, VariableTableEntry *var,
......@@ -3036,7 +3036,7 @@ static TypeTableEntry *analyze_var_ref(CodeGen *g, AstNode *source_node, Variabl
30363036 if (!var_is_pure(var, context)) {
30373037 mark_impure_fn(g, context, source_node);
30383038 }
3039 if (var->is_const && var->val_node) {
3039 if (var->src_is_const && var->val_node) {
30403040 ConstExprValue *other_const_val = &get_resolved_expr(var->val_node)->const_val;
30413041 if (other_const_val->ok) {
30423042 return resolve_expr_const_val_as_other_expr(g, source_node, var->val_node,
......@@ -3188,7 +3188,7 @@ static TypeTableEntry *analyze_lvalue(CodeGen *g, ImportTableEntry *import, Bloc
31883188 Buf *name = lhs_node->data.symbol_expr.symbol;
31893189 VariableTableEntry *var = find_variable(g, block_context, name);
31903190 if (var) {
3191 if (var->is_const) {
3191 if (var->src_is_const) {
31923192 add_node_error(g, lhs_node, buf_sprintf("cannot assign to constant"));
31933193 expected_rhs_type = g->builtin_types.entry_invalid;
31943194 } else {
......@@ -3736,7 +3736,8 @@ static VariableTableEntry *add_local_var_shadowable(CodeGen *g, AstNode *source_
37363736 context->fn_entry->variable_list.append(variable_entry);
37373737 }
37383738
3739 variable_entry->is_const = is_const;
3739 variable_entry->src_is_const = is_const;
3740 variable_entry->gen_is_const = is_const;
37403741 variable_entry->decl_node = source_node;
37413742 variable_entry->val_node = val_node;
37423743
src/codegen.cpp+3-3
......@@ -4105,7 +4105,7 @@ static void build_all_basic_blocks(CodeGen *g, FnTableEntry *fn) {
41054105static void gen_global_var(CodeGen *g, VariableTableEntry *var, LLVMValueRef init_val,
41064106 TypeTableEntry *type_entry)
41074107{
4108 assert(var->is_const);
4108 assert(var->gen_is_const);
41094109 assert(var->import);
41104110 assert(type_entry);
41114111 bool is_local_to_unit = true;
......@@ -4183,12 +4183,12 @@ static void do_code_gen(CodeGen *g) {
41834183 LLVMSetUnnamedAddr(global_value, true);
41844184
41854185 // TODO debug info for function pointers
4186 if (var->is_const && var->type->id != TypeTableEntryIdFn) {
4186 if (var->gen_is_const && var->type->id != TypeTableEntryIdFn) {
41874187 gen_global_var(g, var, init_val, var->type);
41884188 }
41894189 }
41904190
4191 LLVMSetGlobalConstant(global_value, var->is_const);
4191 LLVMSetGlobalConstant(global_value, var->gen_is_const);
41924192
41934193 var->value_ref = global_value;
41944194 }
src/ir.cpp+105-72
......@@ -200,6 +200,7 @@ static T *ir_create_instruction(IrExecutable *exec, AstNode *source_node) {
200200
201201template<typename T>
202202static T *ir_build_instruction(IrBuilder *irb, AstNode *source_node) {
203 assert(source_node);
203204 T *special_instruction = ir_create_instruction<T>(irb->exec, source_node);
204205 ir_instruction_append(irb->current_basic_block, &special_instruction->base);
205206 return special_instruction;
......@@ -220,7 +221,7 @@ static IrInstruction *ir_build_cast(IrBuilder *irb, AstNode *source_node, IrInst
220221}
221222
222223static IrInstruction *ir_build_cond_br(IrBuilder *irb, AstNode *source_node, IrInstruction *condition,
223 IrBasicBlock *then_block, IrBasicBlock *else_block)
224 IrBasicBlock *then_block, IrBasicBlock *else_block, bool is_inline)
224225{
225226 IrInstructionCondBr *cond_br_instruction = ir_build_instruction<IrInstructionCondBr>(irb, source_node);
226227 cond_br_instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable;
......@@ -228,6 +229,7 @@ static IrInstruction *ir_build_cond_br(IrBuilder *irb, AstNode *source_node, IrI
228229 cond_br_instruction->condition = condition;
229230 cond_br_instruction->then_block = then_block;
230231 cond_br_instruction->else_block = else_block;
232 cond_br_instruction->is_inline = is_inline;
231233
232234 ir_ref_instruction(condition);
233235 ir_ref_bb(then_block);
......@@ -237,10 +239,10 @@ static IrInstruction *ir_build_cond_br(IrBuilder *irb, AstNode *source_node, IrI
237239}
238240
239241static IrInstruction *ir_build_cond_br_from(IrBuilder *irb, IrInstruction *old_instruction,
240 IrInstruction *condition, IrBasicBlock *then_block, IrBasicBlock *else_block)
242 IrInstruction *condition, IrBasicBlock *then_block, IrBasicBlock *else_block, bool is_inline)
241243{
242244 IrInstruction *new_instruction = ir_build_cond_br(irb, old_instruction->source_node,
243 condition, then_block, else_block);
245 condition, then_block, else_block, is_inline);
244246 ir_link_new_instruction(new_instruction, old_instruction);
245247 return new_instruction;
246248}
......@@ -517,22 +519,23 @@ static IrInstruction *ir_build_phi_from(IrBuilder *irb, IrInstruction *old_instr
517519 return new_instruction;
518520}
519521
520static IrInstruction *ir_build_br(IrBuilder *irb, AstNode *source_node, IrBasicBlock *dest_block) {
522static IrInstruction *ir_build_br(IrBuilder *irb, AstNode *source_node, IrBasicBlock *dest_block, bool is_inline) {
521523 IrInstructionBr *br_instruction = ir_build_instruction<IrInstructionBr>(irb, source_node);
522524 br_instruction->base.type_entry = irb->codegen->builtin_types.entry_unreachable;
523525 br_instruction->base.static_value.ok = true;
524526 br_instruction->dest_block = dest_block;
527 br_instruction->is_inline = is_inline;
525528
526529 ir_ref_bb(dest_block);
527530
528531 return &br_instruction->base;
529532}
530533
531//static IrInstruction *ir_build_br_from(IrBuilder *irb, IrInstruction *old_instruction, IrBasicBlock *dest_block) {
532// IrInstruction *new_instruction = ir_build_br(irb, old_instruction->source_node, dest_block);
533// ir_link_new_instruction(new_instruction, old_instruction);
534// return new_instruction;
535//}
534static IrInstruction *ir_build_br_from(IrBuilder *irb, IrInstruction *old_instruction, IrBasicBlock *dest_block) {
535 IrInstruction *new_instruction = ir_build_br(irb, old_instruction->source_node, dest_block, false);
536 ir_link_new_instruction(new_instruction, old_instruction);
537 return new_instruction;
538}
536539
537540static IrInstruction *ir_build_un_op(IrBuilder *irb, AstNode *source_node, IrUnOp op_id, IrInstruction *value) {
538541 IrInstructionUnOp *br_instruction = ir_build_instruction<IrInstructionUnOp>(irb, source_node);
......@@ -634,7 +637,7 @@ static IrInstruction *ir_build_var_decl(IrBuilder *irb, AstNode *source_node,
634637 decl_var_instruction->var_type = var_type;
635638 decl_var_instruction->init_value = init_value;
636639
637 ir_ref_instruction(var_type);
640 if (var_type) ir_ref_instruction(var_type);
638641 ir_ref_instruction(init_value);
639642
640643 return &decl_var_instruction->base;
......@@ -775,13 +778,14 @@ static void ir_set_cursor_at_end(IrBuilder *irb, IrBasicBlock *basic_block) {
775778}
776779
777780static VariableTableEntry *add_local_var(CodeGen *codegen, AstNode *node, BlockContext *scope,
778 Buf *name, bool is_const, bool is_shadowable)
781 Buf *name, bool src_is_const, bool gen_is_const, bool is_shadowable, bool is_inline)
779782{
780783 VariableTableEntry *variable_entry = allocate<VariableTableEntry>(1);
781784 variable_entry->block_context = scope;
782785 variable_entry->import = node->owner;
783786 variable_entry->shadowable = is_shadowable;
784787 variable_entry->mem_slot_index = SIZE_MAX;
788 variable_entry->is_inline = is_inline;
785789
786790 if (name) {
787791 buf_init_from_buf(&variable_entry->name, name);
......@@ -817,7 +821,8 @@ static VariableTableEntry *add_local_var(CodeGen *codegen, AstNode *node, BlockC
817821 buf_init_from_str(&variable_entry->name, "_anon");
818822 }
819823
820 variable_entry->is_const = is_const;
824 variable_entry->src_is_const = src_is_const;
825 variable_entry->gen_is_const = gen_is_const;
821826 variable_entry->decl_node = node;
822827
823828 return variable_entry;
......@@ -825,10 +830,12 @@ static VariableTableEntry *add_local_var(CodeGen *codegen, AstNode *node, BlockC
825830
826831// Set name to nullptr to make the variable anonymous (not visible to programmer).
827832static VariableTableEntry *ir_add_local_var(IrBuilder *irb, AstNode *node, BlockContext *scope, Buf *name,
828 bool is_const, bool is_shadowable)
833 bool src_is_const, bool gen_is_const, bool is_shadowable, bool is_inline)
829834{
830 VariableTableEntry *var = add_local_var(irb->codegen, node, scope, name, is_const, is_shadowable);
831 var->mem_slot_index = exec_next_mem_slot(irb->exec);
835 VariableTableEntry *var = add_local_var(irb->codegen, node, scope, name,
836 src_is_const, gen_is_const, is_shadowable, is_inline);
837 if (is_inline || gen_is_const)
838 var->mem_slot_index = exec_next_mem_slot(irb->exec);
832839 return var;
833840}
834841
......@@ -1189,14 +1196,14 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, AstNode *node) {
11891196 IrBasicBlock *else_block = ir_build_basic_block(irb, "Else");
11901197 IrBasicBlock *endif_block = ir_build_basic_block(irb, "EndIf");
11911198
1192 ir_build_cond_br(irb, condition->source_node, condition, then_block, else_block);
1199 ir_build_cond_br(irb, condition->source_node, condition, then_block, else_block, false);
11931200
11941201 ir_set_cursor_at_end(irb, then_block);
11951202 IrInstruction *then_expr_result = ir_gen_node(irb, then_node, node->block_context);
11961203 if (then_expr_result == irb->codegen->invalid_instruction)
11971204 return then_expr_result;
11981205 IrBasicBlock *after_then_block = irb->current_basic_block;
1199 ir_build_br(irb, node, endif_block);
1206 ir_build_br(irb, node, endif_block, false);
12001207
12011208 ir_set_cursor_at_end(irb, else_block);
12021209 IrInstruction *else_expr_result;
......@@ -1208,7 +1215,7 @@ static IrInstruction *ir_gen_if_bool_expr(IrBuilder *irb, AstNode *node) {
12081215 else_expr_result = ir_build_const_void(irb, node);
12091216 }
12101217 IrBasicBlock *after_else_block = irb->current_basic_block;
1211 ir_build_br(irb, node, endif_block);
1218 ir_build_br(irb, node, endif_block, false);
12121219
12131220 ir_set_cursor_at_end(irb, endif_block);
12141221 IrInstruction **incoming_values = allocate<IrInstruction *>(2);
......@@ -1333,8 +1340,9 @@ static IrInstruction *ir_gen_var_decl(IrBuilder *irb, AstNode *node) {
13331340 bool is_shadowable = false;
13341341 bool is_const = variable_declaration->is_const;
13351342 bool is_extern = variable_declaration->is_extern;
1343 bool is_inline = variable_declaration->is_inline;
13361344 VariableTableEntry *var = ir_add_local_var(irb, node, node->block_context,
1337 variable_declaration->symbol, is_const, is_shadowable);
1345 variable_declaration->symbol, is_const, is_const, is_shadowable, is_inline);
13381346
13391347 if (!is_extern && !variable_declaration->expr) {
13401348 var->type = irb->codegen->builtin_types.entry_invalid;
......@@ -1356,18 +1364,18 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, AstNode *node) {
13561364 ir_build_basic_block(irb, "WhileContinue") : cond_block;
13571365 IrBasicBlock *end_block = ir_build_basic_block(irb, "WhileEnd");
13581366
1359 ir_build_br(irb, node, cond_block);
1367 bool is_inline = node->data.while_expr.is_inline;
1368 ir_build_br(irb, node, cond_block, is_inline);
13601369
13611370 if (continue_expr_node) {
13621371 ir_set_cursor_at_end(irb, continue_block);
13631372 ir_gen_node(irb, continue_expr_node, node->block_context);
1364 ir_build_br(irb, node, cond_block);
1365
1373 ir_build_br(irb, node, cond_block, is_inline);
13661374 }
13671375
13681376 ir_set_cursor_at_end(irb, cond_block);
13691377 IrInstruction *cond_val = ir_gen_node(irb, node->data.while_expr.condition, node->block_context);
1370 ir_build_cond_br(irb, node->data.while_expr.condition, cond_val, body_block, end_block);
1378 ir_build_cond_br(irb, node->data.while_expr.condition, cond_val, body_block, end_block, is_inline);
13711379
13721380 ir_set_cursor_at_end(irb, body_block);
13731381
......@@ -1377,7 +1385,7 @@ static IrInstruction *ir_gen_while_expr(IrBuilder *irb, AstNode *node) {
13771385 irb->break_block_stack.pop();
13781386 irb->continue_block_stack.pop();
13791387
1380 ir_build_br(irb, node, continue_block);
1388 ir_build_br(irb, node, continue_block, is_inline);
13811389 ir_set_cursor_at_end(irb, end_block);
13821390
13831391 return ir_build_const_void(irb, node);
......@@ -1411,30 +1419,36 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, AstNode *node) {
14111419 } else {
14121420 elem_var_type = ir_build_ptr_type_child(irb, elem_node, pointer_type);
14131421 }
1422 bool is_inline = node->data.for_expr.is_inline;
14141423
14151424 BlockContext *child_scope = new_block_context(node, parent_scope);
14161425 child_scope->parent_loop_node = node;
14171426 elem_node->block_context = child_scope;
14181427
14191428 // TODO make it an error to write to element variable or i variable.
1420
14211429 Buf *elem_var_name = elem_node->data.symbol_expr.symbol;
1422 node->data.for_expr.elem_var = ir_add_local_var(irb, elem_node, child_scope, elem_var_name, false, false);
1430 node->data.for_expr.elem_var = ir_add_local_var(irb, elem_node, child_scope, elem_var_name,
1431 true, false, false, is_inline);
14231432 IrInstruction *undefined_value = ir_build_const_undefined(irb, elem_node);
14241433 ir_build_var_decl(irb, elem_node, node->data.for_expr.elem_var, elem_var_type, undefined_value);
14251434 IrInstruction *elem_var_ptr = ir_build_var_ptr(irb, node, node->data.for_expr.elem_var);
14261435
1436 AstNode *index_var_source_node;
14271437 if (index_node) {
1438 index_var_source_node = index_node;
14281439 Buf *index_var_name = index_node->data.symbol_expr.symbol;
14291440 index_node->block_context = child_scope;
1430 node->data.for_expr.index_var = ir_add_local_var(irb, index_node, child_scope, index_var_name, false, false);
1441 node->data.for_expr.index_var = ir_add_local_var(irb, index_node, child_scope, index_var_name,
1442 true, false, false, is_inline);
14311443 } else {
1432 node->data.for_expr.index_var = ir_add_local_var(irb, node, child_scope, nullptr, false, true);
1444 index_var_source_node = node;
1445 node->data.for_expr.index_var = ir_add_local_var(irb, node, child_scope, nullptr,
1446 true, false, true, is_inline);
14331447 }
14341448 IrInstruction *usize = ir_build_const_type(irb, node, irb->codegen->builtin_types.entry_usize);
14351449 IrInstruction *zero = ir_build_const_usize(irb, node, 0);
14361450 IrInstruction *one = ir_build_const_usize(irb, node, 1);
1437 ir_build_var_decl(irb, index_node, node->data.for_expr.index_var, usize, zero);
1451 ir_build_var_decl(irb, index_var_source_node, node->data.for_expr.index_var, usize, zero);
14381452 IrInstruction *index_ptr = ir_build_var_ptr(irb, node, node->data.for_expr.index_var);
14391453
14401454
......@@ -1444,12 +1458,12 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, AstNode *node) {
14441458 IrBasicBlock *continue_block = ir_build_basic_block(irb, "ForContinue");
14451459
14461460 IrInstruction *len_val = ir_build_read_field(irb, node, array_val, irb->codegen->len_buf);
1447 ir_build_br(irb, node, cond_block);
1461 ir_build_br(irb, node, cond_block, is_inline);
14481462
14491463 ir_set_cursor_at_end(irb, cond_block);
14501464 IrInstruction *index_val = ir_build_load_ptr(irb, node, index_ptr);
14511465 IrInstruction *cond = ir_build_bin_op(irb, node, IrBinOpCmpLessThan, index_val, len_val);
1452 ir_build_cond_br(irb, node, cond, body_block, end_block);
1466 ir_build_cond_br(irb, node, cond, body_block, end_block, is_inline);
14531467
14541468 ir_set_cursor_at_end(irb, body_block);
14551469 IrInstruction *elem_ptr = ir_build_elem_ptr(irb, node, array_val, index_val);
......@@ -1467,12 +1481,12 @@ static IrInstruction *ir_gen_for_expr(IrBuilder *irb, AstNode *node) {
14671481 irb->break_block_stack.pop();
14681482 irb->continue_block_stack.pop();
14691483
1470 ir_build_br(irb, node, continue_block);
1484 ir_build_br(irb, node, continue_block, is_inline);
14711485
14721486 ir_set_cursor_at_end(irb, continue_block);
14731487 IrInstruction *new_index_val = ir_build_bin_op(irb, node, IrBinOpAdd, index_val, one);
14741488 ir_build_store_ptr(irb, node, index_ptr, new_index_val);
1475 ir_build_br(irb, node, cond_block);
1489 ir_build_br(irb, node, cond_block, is_inline);
14761490
14771491 ir_set_cursor_at_end(irb, end_block);
14781492 return ir_build_const_void(irb, node);
......@@ -1911,23 +1925,41 @@ static void ir_inline_bb(IrAnalyze *ira, IrBasicBlock *old_bb) {
19111925}
19121926
19131927
1914static ConstExprValue *ir_get_out_val(IrInstruction *instruction) {
1915 instruction->other = instruction;
1916 return &instruction->static_value;
1928static ConstExprValue *ir_build_const_from(IrAnalyze *ira, IrInstruction *old_instruction,
1929 bool depends_on_compile_var)
1930{
1931 IrInstruction *new_instruction;
1932 if (old_instruction->id == IrInstructionIdVarPtr) {
1933 IrInstructionVarPtr *old_var_ptr_instruction = (IrInstructionVarPtr *)old_instruction;
1934 IrInstructionVarPtr *var_ptr_instruction = ir_create_instruction<IrInstructionVarPtr>(ira->new_irb.exec,
1935 old_instruction->source_node);
1936 var_ptr_instruction->var = old_var_ptr_instruction->var;
1937 new_instruction = &var_ptr_instruction->base;
1938 } else if (old_instruction->id == IrInstructionIdFieldPtr) {
1939 zig_panic("TODO");
1940 } else if (old_instruction->id == IrInstructionIdElemPtr) {
1941 zig_panic("TODO");
1942 } else {
1943 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec,
1944 old_instruction->source_node);
1945 new_instruction = &const_instruction->base;
1946 }
1947 ir_link_new_instruction(new_instruction, old_instruction);
1948 ConstExprValue *const_val = &new_instruction->static_value;
1949 const_val->ok = true;
1950 const_val->depends_on_compile_var = depends_on_compile_var;
1951 return const_val;
19171952}
19181953
19191954static TypeTableEntry *ir_analyze_void(IrAnalyze *ira, IrInstruction *instruction) {
1920 ConstExprValue *const_val = ir_get_out_val(instruction);
1921 const_val->ok = true;
1955 ir_build_const_from(ira, instruction, false);
19221956 return ira->codegen->builtin_types.entry_void;
19231957}
19241958
19251959static TypeTableEntry *ir_analyze_const_usize(IrAnalyze *ira, IrInstruction *instruction, uint64_t value,
19261960 bool depends_on_compile_var)
19271961{
1928 ConstExprValue *const_val = ir_get_out_val(instruction);
1929 const_val->ok = true;
1930 const_val->depends_on_compile_var = depends_on_compile_var;
1962 ConstExprValue *const_val = ir_build_const_from(ira, instruction, depends_on_compile_var);
19311963 bignum_init_unsigned(&const_val->data.x_bignum, value);
19321964 return ira->codegen->builtin_types.entry_usize;
19331965}
......@@ -2295,7 +2327,8 @@ static TypeTableEntry *ir_analyze_bin_op_bool(IrAnalyze *ira, IrInstructionBinOp
22952327 ConstExprValue *op1_val = &casted_op1->static_value;
22962328 ConstExprValue *op2_val = &casted_op2->static_value;
22972329 if (op1_val->ok && op2_val->ok) {
2298 ConstExprValue *out_val = ir_get_out_val(&bin_op_instruction->base);
2330 bool depends_on_compile_var = op1_val->depends_on_compile_var || op2_val->depends_on_compile_var;
2331 ConstExprValue *out_val = ir_build_const_from(ira, &bin_op_instruction->base, depends_on_compile_var);
22992332
23002333 assert(op1->type_entry->id == TypeTableEntryIdBool);
23012334 assert(op2->type_entry->id == TypeTableEntryIdBool);
......@@ -2306,14 +2339,10 @@ static TypeTableEntry *ir_analyze_bin_op_bool(IrAnalyze *ira, IrInstructionBinOp
23062339 } else {
23072340 zig_unreachable();
23082341 }
2309 out_val->ok = true;
2310 out_val->depends_on_compile_var = op1_val->depends_on_compile_var ||
2311 op2_val->depends_on_compile_var;
23122342 return bool_type;
23132343 }
23142344
23152345 ir_build_bin_op_from(&ira->new_irb, &bin_op_instruction->base, bin_op_instruction->op_id, op1->other, op2->other);
2316
23172346 return bool_type;
23182347}
23192348
......@@ -2425,9 +2454,8 @@ static TypeTableEntry *ir_analyze_bin_op_cmp(IrAnalyze *ira, IrInstructionBinOp
24252454 }
24262455 }
24272456
2428 ConstExprValue *out_val = ir_get_out_val(&bin_op_instruction->base);
2429 out_val->ok = true;
2430 out_val->depends_on_compile_var = op1_val->depends_on_compile_var || op2_val->depends_on_compile_var;
2457 bool depends_on_compile_var = op1_val->depends_on_compile_var || op2_val->depends_on_compile_var;
2458 ConstExprValue *out_val = ir_build_const_from(ira, &bin_op_instruction->base, depends_on_compile_var);
24312459 out_val->data.x_bool = answer;
24322460 return ira->codegen->builtin_types.entry_bool;
24332461 }
......@@ -4108,12 +4136,15 @@ static TypeTableEntry *ir_analyze_instruction_br(IrAnalyze *ira, IrInstructionBr
41084136
41094137 // TODO detect backward jumps
41104138
4111 ir_inline_bb(ira, old_dest_block);
4112 return ira->codegen->builtin_types.entry_unreachable;
4139 if (br_instruction->is_inline || old_dest_block->ref_count == 1) {
4140 ir_inline_bb(ira, old_dest_block);
4141 return ira->codegen->builtin_types.entry_unreachable;
4142 }
41134143
4114 //IrBasicBlock *new_bb = ir_get_new_bb(ira, old_dest_block);
4115 //ir_build_br_from(&ira->new_irb, &br_instruction->base, new_bb);
4116 //return ira->codegen->builtin_types.entry_unreachable;
4144 IrBasicBlock *new_bb = ir_get_new_bb(ira, old_dest_block);
4145 ir_build_br_from(&ira->new_irb, &br_instruction->base, new_bb);
4146 ir_finish_bb(ira);
4147 return ira->codegen->builtin_types.entry_unreachable;
41174148}
41184149
41194150static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstructionCondBr *cond_br_instruction) {
......@@ -4129,13 +4160,20 @@ static TypeTableEntry *ir_analyze_instruction_cond_br(IrAnalyze *ira, IrInstruct
41294160 IrBasicBlock *old_dest_block = condition->static_value.data.x_bool ?
41304161 cond_br_instruction->then_block : cond_br_instruction->else_block;
41314162
4132 ir_inline_bb(ira, old_dest_block);
4163 if (cond_br_instruction->is_inline || old_dest_block->ref_count == 1) {
4164 ir_inline_bb(ira, old_dest_block);
4165 return ira->codegen->builtin_types.entry_unreachable;
4166 }
4167 } else if (cond_br_instruction->is_inline) {
4168 add_node_error(ira->codegen, condition->source_node,
4169 buf_sprintf("unable to evaluate constant expression"));
4170 ir_finish_bb(ira);
41334171 return ira->codegen->builtin_types.entry_unreachable;
41344172 }
41354173
41364174 IrBasicBlock *new_then_block = ir_get_new_bb(ira, cond_br_instruction->then_block);
41374175 IrBasicBlock *new_else_block = ir_get_new_bb(ira, cond_br_instruction->else_block);
4138 ir_build_cond_br_from(&ira->new_irb, &cond_br_instruction->base, condition, new_then_block, new_else_block);
4176 ir_build_cond_br_from(&ira->new_irb, &cond_br_instruction->base, condition, new_then_block, new_else_block, false);
41394177 ir_finish_bb(ira);
41404178 return ira->codegen->builtin_types.entry_unreachable;
41414179}
......@@ -4206,7 +4244,8 @@ static TypeTableEntry *ir_analyze_instruction_phi(IrAnalyze *ira, IrInstructionP
42064244 IrInstruction *value = phi_instruction->incoming_values[i]->other;
42074245 assert(value->type_entry);
42084246 if (value->static_value.ok) {
4209 ConstExprValue *out_val = ir_get_out_val(&phi_instruction->base);
4247 ConstExprValue *out_val = ir_build_const_from(ira, &phi_instruction->base,
4248 value->static_value.depends_on_compile_var);
42104249 *out_val = value->static_value;
42114250 } else {
42124251 phi_instruction->base.other = value;
......@@ -4259,9 +4298,9 @@ static TypeTableEntry *ir_analyze_instruction_var_ptr(IrAnalyze *ira, IrInstruct
42594298 if (var->mem_slot_index != SIZE_MAX) {
42604299 ConstExprValue *mem_slot = &ira->exec_context.mem_slot_list[var->mem_slot_index];
42614300 if (mem_slot->ok) {
4262 ConstExprValue *out_val = ir_get_out_val(&var_ptr_instruction->base);
4301 ConstExprValue *out_val = ir_build_const_from(ira, &var_ptr_instruction->base,
4302 mem_slot->depends_on_compile_var);
42634303
4264 out_val->ok = true;
42654304 out_val->data.x_ptr.len = 1;
42664305 out_val->data.x_ptr.is_c_str = false;
42674306 out_val->data.x_ptr.ptr = allocate<ConstExprValue *>(1);
......@@ -4477,7 +4516,8 @@ static TypeTableEntry *ir_analyze_instruction_load_ptr(IrAnalyze *ira, IrInstruc
44774516 if (ptr->static_value.ok) {
44784517 ConstExprValue *pointee = ptr->static_value.data.x_ptr.ptr[0];
44794518 if (pointee->ok) {
4480 ConstExprValue *out_val = ir_get_out_val(&load_ptr_instruction->base);
4519 ConstExprValue *out_val = ir_build_const_from(ira, &load_ptr_instruction->base,
4520 pointee->depends_on_compile_var);
44814521 *out_val = *pointee;
44824522 return child_type;
44834523 }
......@@ -4576,8 +4616,7 @@ static TypeTableEntry *ir_analyze_instruction_typeof(IrAnalyze *ira, IrInstructi
45764616 case TypeTableEntryIdFn:
45774617 case TypeTableEntryIdTypeDecl:
45784618 {
4579 ConstExprValue *out_val = ir_get_out_val(&typeof_instruction->base);
4580 out_val->ok = true;
4619 ConstExprValue *out_val = ir_build_const_from(ira, &typeof_instruction->base, false);
45814620 // TODO depends_on_compile_var should be set based on whether the type of the expression
45824621 // depends_on_compile_var. but we currently don't have a thing to tell us if the type of
45834622 // something depends on a compile var
......@@ -4609,9 +4648,8 @@ static TypeTableEntry *ir_analyze_instruction_to_ptr_type(IrAnalyze *ira,
46094648 return ira->codegen->builtin_types.entry_invalid;
46104649 }
46114650
4612 ConstExprValue *out_val = ir_get_out_val(&to_ptr_type_instruction->base);
4613 out_val->ok = true;
4614 out_val->depends_on_compile_var = type_value->static_value.depends_on_compile_var;
4651 ConstExprValue *out_val = ir_build_const_from(ira, &to_ptr_type_instruction->base,
4652 type_value->static_value.depends_on_compile_var);
46154653 out_val->data.x_type = ptr_type;
46164654 return ira->codegen->builtin_types.entry_type;
46174655}
......@@ -4630,9 +4668,8 @@ static TypeTableEntry *ir_analyze_instruction_ptr_type_child(IrAnalyze *ira,
46304668 return ira->codegen->builtin_types.entry_invalid;
46314669 }
46324670
4633 ConstExprValue *out_val = ir_get_out_val(&ptr_type_child_instruction->base);
4634 out_val->ok = true;
4635 out_val->depends_on_compile_var = type_value->static_value.depends_on_compile_var;
4671 ConstExprValue *out_val = ir_build_const_from(ira, &ptr_type_child_instruction->base,
4672 type_value->static_value.depends_on_compile_var);
46364673 out_val->data.x_type = type_entry->data.pointer.child_type;
46374674 return ira->codegen->builtin_types.entry_type;
46384675}
......@@ -4734,10 +4771,6 @@ TypeTableEntry *ir_analyze(CodeGen *codegen, IrExecutable *old_exec, IrExecutabl
47344771 while (ira->block_queue_index < ira->old_bb_queue.length) {
47354772 IrInstruction *old_instruction = ira->old_irb.current_basic_block->instruction_list.at(ira->instruction_index);
47364773
4737 fprintf(stderr, "===%zu====", old_instruction->debug_id);
4738 ir_print(stderr, ira->new_irb.exec, 4);
4739 fprintf(stderr, "========");
4740
47414774 if (old_instruction->ref_count == 0 && !ir_has_side_effects(old_instruction)) {
47424775 ira->instruction_index += 1;
47434776 continue;
src/ir_print.cpp+8-5
......@@ -231,14 +231,15 @@ static void ir_print_bin_op(IrPrint *irp, IrInstructionBinOp *bin_op_instruction
231231}
232232
233233static void ir_print_decl_var(IrPrint *irp, IrInstructionDeclVar *decl_var_instruction) {
234 const char *var_or_const = decl_var_instruction->var->is_const ? "const" : "var";
234 const char *inline_kw = decl_var_instruction->var->is_inline ? "inline " : "";
235 const char *var_or_const = decl_var_instruction->var->gen_is_const ? "const" : "var";
235236 const char *name = buf_ptr(&decl_var_instruction->var->name);
236237 if (decl_var_instruction->var_type) {
237 fprintf(irp->f, "%s %s: ", var_or_const, name);
238 fprintf(irp->f, "%s%s %s: ", inline_kw, var_or_const, name);
238239 ir_print_other_instruction(irp, decl_var_instruction->var_type);
239240 fprintf(irp->f, " = ");
240241 } else {
241 fprintf(irp->f, "%s %s = ", var_or_const, name);
242 fprintf(irp->f, "%s%s %s = ", inline_kw, var_or_const, name);
242243 }
243244 ir_print_other_instruction(irp, decl_var_instruction->init_value);
244245}
......@@ -275,7 +276,8 @@ static void ir_print_builtin_call(IrPrint *irp, IrInstructionBuiltinCall *call_i
275276
276277
277278static void ir_print_cond_br(IrPrint *irp, IrInstructionCondBr *cond_br_instruction) {
278 fprintf(irp->f, "if (");
279 const char *inline_kw = cond_br_instruction->is_inline ? "inline " : "";
280 fprintf(irp->f, "%sif (", inline_kw);
279281 ir_print_other_instruction(irp, cond_br_instruction->condition);
280282 fprintf(irp->f, ") ");
281283 ir_print_other_block(irp, cond_br_instruction->then_block);
......@@ -284,7 +286,8 @@ static void ir_print_cond_br(IrPrint *irp, IrInstructionCondBr *cond_br_instruct
284286}
285287
286288static void ir_print_br(IrPrint *irp, IrInstructionBr *br_instruction) {
287 fprintf(irp->f, "goto ");
289 const char *inline_kw = br_instruction->is_inline ? "inline " : "";
290 fprintf(irp->f, "%sgoto ", inline_kw);
288291 ir_print_other_block(irp, br_instruction->dest_block);
289292}
290293
src/parser.cpp+67-20
......@@ -1450,29 +1450,50 @@ static AstNode *ast_parse_defer_expr(ParseContext *pc, size_t *token_index) {
14501450}
14511451
14521452/*
1453VariableDeclaration : ("var" | "const") "Symbol" ("=" Expression | ":" PrefixOpExpression option("=" Expression))
1453VariableDeclaration = option("inline") ("var" | "const") Symbol option(":" TypeExpr) "=" Expression
14541454*/
14551455static AstNode *ast_parse_variable_declaration_expr(ParseContext *pc, size_t *token_index, bool mandatory,
14561456 VisibMod visib_mod)
14571457{
14581458 Token *first_token = &pc->tokens->at(*token_index);
1459 Token *var_token;
14591460
14601461 bool is_const;
1462 bool is_inline;
1463 if (first_token->id == TokenIdKeywordInline) {
1464 is_inline = true;
1465 var_token = &pc->tokens->at(*token_index + 1);
1466
1467 if (var_token->id == TokenIdKeywordVar) {
1468 is_const = false;
1469 } else if (var_token->id == TokenIdKeywordConst) {
1470 is_const = true;
1471 } else if (mandatory) {
1472 ast_invalid_token_error(pc, var_token);
1473 } else {
1474 return nullptr;
1475 }
14611476
1462 if (first_token->id == TokenIdKeywordVar) {
1477 *token_index += 2;
1478 } else if (first_token->id == TokenIdKeywordVar) {
1479 is_inline = false;
14631480 is_const = false;
1481 var_token = first_token;
1482 *token_index += 1;
14641483 } else if (first_token->id == TokenIdKeywordConst) {
1484 is_inline = false;
14651485 is_const = true;
1486 var_token = first_token;
1487 *token_index += 1;
14661488 } else if (mandatory) {
14671489 ast_invalid_token_error(pc, first_token);
14681490 } else {
14691491 return nullptr;
14701492 }
14711493
1472 *token_index += 1;
1473
1474 AstNode *node = ast_create_node(pc, NodeTypeVariableDeclaration, first_token);
1494 AstNode *node = ast_create_node(pc, NodeTypeVariableDeclaration, var_token);
14751495
1496 node->data.variable_declaration.is_inline = is_inline;
14761497 node->data.variable_declaration.is_const = is_const;
14771498 node->data.variable_declaration.top_level_decl.visib_mod = visib_mod;
14781499
......@@ -1529,21 +1550,33 @@ static AstNode *ast_parse_bool_or_expr(ParseContext *pc, size_t *token_index, bo
15291550}
15301551
15311552/*
1532WhileExpression = "while" "(" Expression option(";" Expression) ")" Expression
1553WhileExpression = option("inline") "while" "(" Expression option(";" Expression) ")" Expression
15331554*/
15341555static AstNode *ast_parse_while_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
1535 Token *token = &pc->tokens->at(*token_index);
1556 Token *first_token = &pc->tokens->at(*token_index);
1557 Token *while_token;
15361558
1537 if (token->id != TokenIdKeywordWhile) {
1538 if (mandatory) {
1539 ast_expect_token(pc, token, TokenIdKeywordWhile);
1559 bool is_inline;
1560 if (first_token->id == TokenIdKeywordInline) {
1561 is_inline = true;
1562 while_token = &pc->tokens->at(*token_index + 1);
1563 if (while_token->id == TokenIdKeywordWhile) {
1564 *token_index += 2;
1565 } else if (mandatory) {
1566 ast_expect_token(pc, first_token, TokenIdKeywordWhile);
15401567 } else {
15411568 return nullptr;
15421569 }
1570 } else if (first_token->id == TokenIdKeywordWhile) {
1571 is_inline = false;
1572 *token_index += 1;
1573 } else if (mandatory) {
1574 ast_expect_token(pc, first_token, TokenIdKeywordWhile);
1575 } else {
1576 return nullptr;
15431577 }
1544 *token_index += 1;
1545
1546 AstNode *node = ast_create_node(pc, NodeTypeWhileExpr, token);
1578 AstNode *node = ast_create_node(pc, NodeTypeWhileExpr, while_token);
1579 node->data.while_expr.is_inline = is_inline;
15471580
15481581 ast_eat_token(pc, token_index, TokenIdLParen);
15491582 node->data.while_expr.condition = ast_parse_expression(pc, token_index, true);
......@@ -1575,21 +1608,35 @@ static AstNode *ast_parse_symbol(ParseContext *pc, size_t *token_index) {
15751608}
15761609
15771610/*
1578ForExpression = "for" "(" Expression ")" option("|" option("*") "Symbol" option("," "Symbol") "|") Expression
1611ForExpression = option("inline") "for" "(" Expression ")" option("|" option("*") Symbol option("," Symbol) "|") Expression
15791612*/
15801613static AstNode *ast_parse_for_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
1581 Token *token = &pc->tokens->at(*token_index);
1614 Token *first_token = &pc->tokens->at(*token_index);
1615 Token *for_token;
15821616
1583 if (token->id != TokenIdKeywordFor) {
1584 if (mandatory) {
1585 ast_expect_token(pc, token, TokenIdKeywordFor);
1617 bool is_inline;
1618 if (first_token->id == TokenIdKeywordInline) {
1619 is_inline = true;
1620 for_token = &pc->tokens->at(*token_index + 1);
1621 if (for_token->id == TokenIdKeywordFor) {
1622 *token_index += 2;
1623 } else if (mandatory) {
1624 ast_expect_token(pc, first_token, TokenIdKeywordFor);
15861625 } else {
15871626 return nullptr;
15881627 }
1628 } else if (first_token->id == TokenIdKeywordFor) {
1629 for_token = first_token;
1630 is_inline = false;
1631 *token_index += 1;
1632 } else if (mandatory) {
1633 ast_expect_token(pc, first_token, TokenIdKeywordFor);
1634 } else {
1635 return nullptr;
15891636 }
1590 *token_index += 1;
15911637
1592 AstNode *node = ast_create_node(pc, NodeTypeForExpr, token);
1638 AstNode *node = ast_create_node(pc, NodeTypeForExpr, for_token);
1639 node->data.for_expr.is_inline = is_inline;
15931640
15941641 ast_eat_token(pc, token_index, TokenIdLParen);
15951642 node->data.for_expr.array_expr = ast_parse_expression(pc, token_index, true);
test/self_hosted2.zig created+13
......@@ -0,0 +1,13 @@
1fn add(a: i32, b: i32) -> i32 {
2 a + b
3}
4
5fn assert(ok: bool) {
6 if (!ok) @unreachable();
7}
8
9fn testAdd() {
10 @setFnTest(this, true);
11
12 assert(add(2, 3) == 5);
13}