authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-31 00:54:10-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-05-31 00:54:10-04:00
log8aba0643a55e4a67c0e7e01c1946900164514f4c
tree9ab67676f7a30de7e1bef97ebbca7c3031b52a96
parent3702c278e3b2073bdf7aadb8bcdf1cd4156bec28
signature Commit is signed but in an unrecognized format.

peer result locations with mixed runtime/comptime

```zig export fn entry() void { var c = true; var a = u8(4); const x = if (c) a else u32(8); } ``` ```llvm define void @entry() #2 !dbg !35 { Entry: %c = alloca i1, align 1 %a = alloca i8, align 1 %x = alloca i32, align 4 store i1 true, i1* %c, align 1, !dbg !45 call void @llvm.dbg.declare(metadata i1* %c, metadata !39, metadata !DIExpression()), !dbg !46 store i8 4, i8* %a, align 1, !dbg !47 call void @llvm.dbg.declare(metadata i8* %a, metadata !42, metadata !DIExpression()), !dbg !48 %0 = load i1, i1* %c, align 1, !dbg !49 br i1 %0, label %Then, label %Else, !dbg !49 Then: ; preds = %Entry %1 = load i8, i8* %a, align 1, !dbg !50 %2 = zext i8 %1 to i32, !dbg !50 br label %EndIf, !dbg !51 Else: ; preds = %Entry br label %EndIf, !dbg !51 EndIf: ; preds = %Else, %Then %3 = phi i32 [ %2, %Then ], [ 8, %Else ], !dbg !51 store i32 %3, i32* %x, align 4, !dbg !51 call void @llvm.dbg.declare(metadata i32* %x, metadata !43, metadata !DIExpression()), !dbg !52 ret void, !dbg !53 } ```

2 files changed, 18 insertions(+), 14 deletions(-)

BRANCH_TODO+1-3
......@@ -23,6 +23,4 @@ inferred comptime
2323
2424handle if with no else
2525
26handle comptime if condition
27
28handle mixed runtime and comptime peers
26audit usage of LVal because that's when ir_lval_wrap is not called which now has build_end_expr
src/ir.cpp+17-11
......@@ -168,6 +168,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
168168static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction, ZigVar *var);
169169static ZigType *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstruction *op);
170170static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval, ResultLoc *result_loc);
171static IrInstruction *ir_expr_wrap(IrBuilder *irb, Scope *scope, IrInstruction *inst, ResultLoc *result_loc);
171172static ZigType *adjust_ptr_align(CodeGen *g, ZigType *ptr_type, uint32_t new_align);
172173static ZigType *adjust_slice_align(CodeGen *g, ZigType *slice_type, uint32_t new_align);
173174static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node, uint8_t *buf, ConstExprValue *val);
......@@ -4011,7 +4012,7 @@ static void populate_invalid_variable_in_scope(CodeGen *g, Scope *scope, AstNode
40114012 scope_decls->decl_table.put(var_name, &tld_var->base);
40124013}
40134014
4014static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval) {
4015static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node, LVal lval, ResultLoc *result_loc) {
40154016 Error err;
40164017 assert(node->type == NodeTypeSymbol);
40174018
......@@ -4053,10 +4054,11 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
40534054 ZigVar *var = find_variable(irb->codegen, scope, variable_name, &crossed_fndef_scope);
40544055 if (var) {
40554056 IrInstruction *var_ptr = ir_build_var_ptr_x(irb, scope, node, var, crossed_fndef_scope);
4056 if (lval == LValPtr)
4057 if (lval == LValPtr) {
40574058 return var_ptr;
4058 else
4059 return ir_build_load_ptr(irb, scope, node, var_ptr);
4059 } else {
4060 return ir_expr_wrap(irb, scope, ir_build_load_ptr(irb, scope, node, var_ptr), result_loc);
4061 }
40604062 }
40614063
40624064 Tld *tld = find_decl(irb->codegen, scope, variable_name);
......@@ -5389,6 +5391,12 @@ static IrInstruction *ir_gen_prefix_op_id(IrBuilder *irb, Scope *scope, AstNode
53895391 return ir_gen_prefix_op_id_lval(irb, scope, node, op_id, LValNone);
53905392}
53915393
5394static IrInstruction *ir_expr_wrap(IrBuilder *irb, Scope *scope, IrInstruction *inst, ResultLoc *result_loc) {
5395 // TODO remove the lval parameter here
5396 ir_build_end_expr(irb, scope, inst->source_node, inst, LValNone, result_loc);
5397 return inst;
5398}
5399
53925400static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval,
53935401 ResultLoc *result_loc)
53945402{
......@@ -5408,9 +5416,7 @@ static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *
54085416 return ir_build_ref(irb, scope, value->source_node, value, false, false);
54095417 }
54105418
5411 // TODO remove the lval parameter here
5412 ir_build_end_expr(irb, scope, value->source_node, value, lval, result_loc);
5413 return value;
5419 return ir_expr_wrap(irb, scope, value, result_loc);
54145420}
54155421
54165422static PtrLen star_token_to_ptr_len(TokenId token_id) {
......@@ -7747,7 +7753,7 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
77477753 case NodeTypeCharLiteral:
77487754 return ir_lval_wrap(irb, scope, ir_gen_char_lit(irb, scope, node), lval, result_loc);
77497755 case NodeTypeSymbol:
7750 return ir_gen_symbol(irb, scope, node, lval);
7756 return ir_gen_symbol(irb, scope, node, lval, result_loc);
77517757 case NodeTypeFnCallExpr:
77527758 return ir_gen_fn_call(irb, scope, node, lval, result_loc);
77537759 case NodeTypeIfBoolExpr:
......@@ -14345,8 +14351,6 @@ static ZigType *ir_result_loc_expected_type(IrAnalyze *ira, ResultLoc *result_lo
1434514351static IrInstruction *ir_resolve_result(IrAnalyze *ira, ResultLoc *result_loc, ZigType *value_type,
1434614352 IrInstruction *value)
1434714353{
14348 bool is_comptime = value != nullptr && value->value.special != ConstValSpecialRuntime;
14349
1435014354 result_loc->gen_instruction = value;
1435114355 result_loc->implicit_elem_type = value_type;
1435214356 switch (result_loc->id) {
......@@ -14357,10 +14361,12 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, ResultLoc *result_loc, Z
1435714361 return nullptr;
1435814362 case ResultLocIdVar: {
1435914363 // TODO implicit cast?
14360 //ResultLocVar *result_loc_var = reinterpret_cast<ResultLocVar *>(result_loc);
14364 ResultLocVar *result_loc_var = reinterpret_cast<ResultLocVar *>(result_loc);
1436114365 assert(result_loc->source_instruction->id == IrInstructionIdAllocaSrc);
1436214366 IrInstructionAllocaSrc *alloca_src =
1436314367 reinterpret_cast<IrInstructionAllocaSrc *>(result_loc->source_instruction);
14368 bool is_comptime = value != nullptr && value->value.special != ConstValSpecialRuntime &&
14369 result_loc_var->var->gen_is_const;
1436414370 if (alloca_src->base.child == nullptr) {
1436514371 uint32_t align = 0; // TODO
1436614372 bool force_comptime = false; // TODO