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
signaturelock-open 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...@@ -23,6 +23,4 @@ inferred comptime
2323
24handle if with no else24handle if with no else
2525
26handle comptime if condition26audit usage of LVal because that's when ir_lval_wrap is not called which now has build_end_expr
27
28handle mixed runtime and comptime peers
src/ir.cpp+17-11
...@@ -168,6 +168,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_...@@ -168,6 +168,7 @@ static IrInstruction *ir_analyze_container_field_ptr(IrAnalyze *ira, Buf *field_
168static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction, ZigVar *var);168static IrInstruction *ir_get_var_ptr(IrAnalyze *ira, IrInstruction *instruction, ZigVar *var);
169static ZigType *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstruction *op);169static ZigType *ir_resolve_atomic_operand_type(IrAnalyze *ira, IrInstruction *op);
170static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval, ResultLoc *result_loc);170static 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);
171static ZigType *adjust_ptr_align(CodeGen *g, ZigType *ptr_type, uint32_t new_align);172static ZigType *adjust_ptr_align(CodeGen *g, ZigType *ptr_type, uint32_t new_align);
172static ZigType *adjust_slice_align(CodeGen *g, ZigType *slice_type, uint32_t new_align);173static ZigType *adjust_slice_align(CodeGen *g, ZigType *slice_type, uint32_t new_align);
173static Error buf_read_value_bytes(IrAnalyze *ira, CodeGen *codegen, AstNode *source_node, uint8_t *buf, ConstExprValue *val);174static 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...@@ -4011,7 +4012,7 @@ static void populate_invalid_variable_in_scope(CodeGen *g, Scope *scope, AstNode
4011 scope_decls->decl_table.put(var_name, &tld_var->base);4012 scope_decls->decl_table.put(var_name, &tld_var->base);
4012}4013}
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) {
4015 Error err;4016 Error err;
4016 assert(node->type == NodeTypeSymbol);4017 assert(node->type == NodeTypeSymbol);
40174018
...@@ -4053,10 +4054,11 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,...@@ -4053,10 +4054,11 @@ static IrInstruction *ir_gen_symbol(IrBuilder *irb, Scope *scope, AstNode *node,
4053 ZigVar *var = find_variable(irb->codegen, scope, variable_name, &crossed_fndef_scope);4054 ZigVar *var = find_variable(irb->codegen, scope, variable_name, &crossed_fndef_scope);
4054 if (var) {4055 if (var) {
4055 IrInstruction *var_ptr = ir_build_var_ptr_x(irb, scope, node, var, crossed_fndef_scope);4056 IrInstruction *var_ptr = ir_build_var_ptr_x(irb, scope, node, var, crossed_fndef_scope);
4056 if (lval == LValPtr)4057 if (lval == LValPtr) {
4057 return var_ptr;4058 return var_ptr;
4058 else4059 } else {
4059 return ir_build_load_ptr(irb, scope, node, var_ptr);4060 return ir_expr_wrap(irb, scope, ir_build_load_ptr(irb, scope, node, var_ptr), result_loc);
4061 }
4060 }4062 }
40614063
4062 Tld *tld = find_decl(irb->codegen, scope, variable_name);4064 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...@@ -5389,6 +5391,12 @@ static IrInstruction *ir_gen_prefix_op_id(IrBuilder *irb, Scope *scope, AstNode
5389 return ir_gen_prefix_op_id_lval(irb, scope, node, op_id, LValNone);5391 return ir_gen_prefix_op_id_lval(irb, scope, node, op_id, LValNone);
5390}5392}
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
5392static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval,5400static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *value, LVal lval,
5393 ResultLoc *result_loc)5401 ResultLoc *result_loc)
5394{5402{
...@@ -5408,9 +5416,7 @@ static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *...@@ -5408,9 +5416,7 @@ static IrInstruction *ir_lval_wrap(IrBuilder *irb, Scope *scope, IrInstruction *
5408 return ir_build_ref(irb, scope, value->source_node, value, false, false);5416 return ir_build_ref(irb, scope, value->source_node, value, false, false);
5409 }5417 }
54105418
5411 // TODO remove the lval parameter here5419 return ir_expr_wrap(irb, scope, value, result_loc);
5412 ir_build_end_expr(irb, scope, value->source_node, value, lval, result_loc);
5413 return value;
5414}5420}
54155421
5416static PtrLen star_token_to_ptr_len(TokenId token_id) {5422static 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...@@ -7747,7 +7753,7 @@ static IrInstruction *ir_gen_node_raw(IrBuilder *irb, AstNode *node, Scope *scop
7747 case NodeTypeCharLiteral:7753 case NodeTypeCharLiteral:
7748 return ir_lval_wrap(irb, scope, ir_gen_char_lit(irb, scope, node), lval, result_loc);7754 return ir_lval_wrap(irb, scope, ir_gen_char_lit(irb, scope, node), lval, result_loc);
7749 case NodeTypeSymbol:7755 case NodeTypeSymbol:
7750 return ir_gen_symbol(irb, scope, node, lval);7756 return ir_gen_symbol(irb, scope, node, lval, result_loc);
7751 case NodeTypeFnCallExpr:7757 case NodeTypeFnCallExpr:
7752 return ir_gen_fn_call(irb, scope, node, lval, result_loc);7758 return ir_gen_fn_call(irb, scope, node, lval, result_loc);
7753 case NodeTypeIfBoolExpr:7759 case NodeTypeIfBoolExpr:
...@@ -14345,8 +14351,6 @@ static ZigType *ir_result_loc_expected_type(IrAnalyze *ira, ResultLoc *result_lo...@@ -14345,8 +14351,6 @@ static ZigType *ir_result_loc_expected_type(IrAnalyze *ira, ResultLoc *result_lo
14345static IrInstruction *ir_resolve_result(IrAnalyze *ira, ResultLoc *result_loc, ZigType *value_type,14351static IrInstruction *ir_resolve_result(IrAnalyze *ira, ResultLoc *result_loc, ZigType *value_type,
14346 IrInstruction *value)14352 IrInstruction *value)
14347{14353{
14348 bool is_comptime = value != nullptr && value->value.special != ConstValSpecialRuntime;
14349
14350 result_loc->gen_instruction = value;14354 result_loc->gen_instruction = value;
14351 result_loc->implicit_elem_type = value_type;14355 result_loc->implicit_elem_type = value_type;
14352 switch (result_loc->id) {14356 switch (result_loc->id) {
...@@ -14357,10 +14361,12 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, ResultLoc *result_loc, Z...@@ -14357,10 +14361,12 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, ResultLoc *result_loc, Z
14357 return nullptr;14361 return nullptr;
14358 case ResultLocIdVar: {14362 case ResultLocIdVar: {
14359 // TODO implicit cast?14363 // TODO implicit cast?
14360 //ResultLocVar *result_loc_var = reinterpret_cast<ResultLocVar *>(result_loc);14364 ResultLocVar *result_loc_var = reinterpret_cast<ResultLocVar *>(result_loc);
14361 assert(result_loc->source_instruction->id == IrInstructionIdAllocaSrc);14365 assert(result_loc->source_instruction->id == IrInstructionIdAllocaSrc);
14362 IrInstructionAllocaSrc *alloca_src =14366 IrInstructionAllocaSrc *alloca_src =
14363 reinterpret_cast<IrInstructionAllocaSrc *>(result_loc->source_instruction);14367 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;
14364 if (alloca_src->base.child == nullptr) {14370 if (alloca_src->base.child == nullptr) {
14365 uint32_t align = 0; // TODO14371 uint32_t align = 0; // TODO
14366 bool force_comptime = false; // TODO14372 bool force_comptime = false; // TODO