authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-11 15:44:06-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-06-11 15:44:06-04:00
loge1d14e73b5f9c7a8d1a92ccd36cb689d625faf57
tree29253dfee7859ccb360887e347f021335cdffaa7
parent60c386180570d9d3830f8375bbad761cf721c6d3
signature Commit is signed but in an unrecognized format.

fix `@bitCast` semantics when there is no parent result loc


4 files changed, 59 insertions(+), 2 deletions(-)

src/all_types.hpp+9
......@@ -44,6 +44,7 @@ struct IrAnalyze;
4444struct ResultLoc;
4545struct ResultLocPeer;
4646struct ResultLocPeerParent;
47struct ResultLocBitCast;
4748
4849enum X64CABIClass {
4950 X64CABIClass_Unknown,
......@@ -2272,6 +2273,7 @@ enum IrInstructionId {
22722273 IrInstructionIdTestComptime,
22732274 IrInstructionIdPtrCastSrc,
22742275 IrInstructionIdPtrCastGen,
2276 IrInstructionIdBitCastSrc,
22752277 IrInstructionIdBitCastGen,
22762278 IrInstructionIdWidenOrShorten,
22772279 IrInstructionIdIntToPtr,
......@@ -3159,6 +3161,13 @@ struct IrInstructionPtrCastGen {
31593161 bool safety_check_on;
31603162};
31613163
3164struct IrInstructionBitCastSrc {
3165 IrInstruction base;
3166
3167 IrInstruction *operand;
3168 ResultLocBitCast *result_loc_bit_cast;
3169};
3170
31623171struct IrInstructionBitCastGen {
31633172 IrInstruction base;
31643173
src/codegen.cpp+1
......@@ -5543,6 +5543,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
55435543 case IrInstructionIdContainerInitList:
55445544 case IrInstructionIdSliceSrc:
55455545 case IrInstructionIdRef:
5546 case IrInstructionIdBitCastSrc:
55465547 zig_unreachable();
55475548
55485549 case IrInstructionIdDeclVarGen:
src/ir.cpp+39-2
......@@ -792,6 +792,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionPtrCastGen *) {
792792 return IrInstructionIdPtrCastGen;
793793}
794794
795static constexpr IrInstructionId ir_instruction_id(IrInstructionBitCastSrc *) {
796 return IrInstructionIdBitCastSrc;
797}
798
795799static constexpr IrInstructionId ir_instruction_id(IrInstructionBitCastGen *) {
796800 return IrInstructionIdBitCastGen;
797801}
......@@ -2515,6 +2519,18 @@ static IrInstruction *ir_build_load_ptr_gen(IrAnalyze *ira, IrInstruction *sourc
25152519 return &instruction->base;
25162520}
25172521
2522static IrInstruction *ir_build_bit_cast_src(IrBuilder *irb, Scope *scope, AstNode *source_node,
2523 IrInstruction *operand, ResultLocBitCast *result_loc_bit_cast)
2524{
2525 IrInstructionBitCastSrc *instruction = ir_build_instruction<IrInstructionBitCastSrc>(irb, scope, source_node);
2526 instruction->operand = operand;
2527 instruction->result_loc_bit_cast = result_loc_bit_cast;
2528
2529 ir_ref_instruction(operand, irb->current_basic_block);
2530
2531 return &instruction->base;
2532}
2533
25182534static IrInstruction *ir_build_bit_cast_gen(IrAnalyze *ira, IrInstruction *source_instruction,
25192535 IrInstruction *operand, ZigType *ty)
25202536{
......@@ -4941,7 +4957,8 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo
49414957 if (arg1_value == irb->codegen->invalid_instruction)
49424958 return arg1_value;
49434959
4944 return ir_lval_wrap(irb, scope, arg1_value, lval, result_loc);
4960 IrInstruction *bitcast = ir_build_bit_cast_src(irb, scope, arg1_node, arg1_value, result_loc_bit_cast);
4961 return ir_lval_wrap(irb, scope, bitcast, lval, result_loc);
49454962 }
49464963 case BuiltinFnIdIntToPtr:
49474964 {
......@@ -14910,9 +14927,15 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
1491014927 return ira->codegen->invalid_instruction;
1491114928 }
1491214929
14930 IrInstruction *bitcasted_value;
14931 if (value != nullptr) {
14932 bitcasted_value = ir_analyze_bit_cast(ira, result_loc->source_instruction, value, dest_type);
14933 } else {
14934 bitcasted_value = nullptr;
14935 }
1491314936
1491414937 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, result_bit_cast->parent,
14915 dest_type, nullptr);
14938 dest_type, bitcasted_value);
1491614939 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value.type) ||
1491714940 parent_result_loc->value.type->id == ZigTypeIdUnreachable)
1491814941 {
......@@ -24168,6 +24191,17 @@ static IrInstruction *ir_analyze_instruction_end_expr(IrAnalyze *ira, IrInstruct
2416824191 return ir_const_void(ira, &instruction->base);
2416924192}
2417024193
24194static IrInstruction *ir_analyze_instruction_bit_cast_src(IrAnalyze *ira, IrInstructionBitCastSrc *instruction) {
24195 IrInstruction *operand = instruction->operand->child;
24196 if (type_is_invalid(operand->value.type) || instr_is_comptime(operand) ||
24197 instruction->result_loc_bit_cast->parent->gen_instruction == nullptr)
24198 {
24199 return operand;
24200 }
24201
24202 return instruction->result_loc_bit_cast->parent->gen_instruction;
24203}
24204
2417124205static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction *instruction) {
2417224206 switch (instruction->id) {
2417324207 case IrInstructionIdInvalid:
......@@ -24472,6 +24506,8 @@ static IrInstruction *ir_analyze_instruction_base(IrAnalyze *ira, IrInstruction
2447224506 return nullptr;
2447324507 case IrInstructionIdEndExpr:
2447424508 return ir_analyze_instruction_end_expr(ira, (IrInstructionEndExpr *)instruction);
24509 case IrInstructionIdBitCastSrc:
24510 return ir_analyze_instruction_bit_cast_src(ira, (IrInstructionBitCastSrc *)instruction);
2447524511 }
2447624512 zig_unreachable();
2447724513}
......@@ -24663,6 +24699,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
2466324699 case IrInstructionIdTestComptime:
2466424700 case IrInstructionIdPtrCastSrc:
2466524701 case IrInstructionIdPtrCastGen:
24702 case IrInstructionIdBitCastSrc:
2466624703 case IrInstructionIdBitCastGen:
2466724704 case IrInstructionIdWidenOrShorten:
2466824705 case IrInstructionIdPtrToInt:
src/ir_print.cpp+10
......@@ -1046,6 +1046,13 @@ static void ir_print_ptr_cast_gen(IrPrint *irp, IrInstructionPtrCastGen *instruc
10461046 fprintf(irp->f, ")");
10471047}
10481048
1049static void ir_print_bit_cast_src(IrPrint *irp, IrInstructionBitCastSrc *instruction) {
1050 fprintf(irp->f, "@bitCast(");
1051 ir_print_other_instruction(irp, instruction->operand);
1052 fprintf(irp->f, ")result=");
1053 ir_print_result_loc(irp, &instruction->result_loc_bit_cast->base);
1054}
1055
10491056static void ir_print_bit_cast_gen(IrPrint *irp, IrInstructionBitCastGen *instruction) {
10501057 fprintf(irp->f, "@bitCast(");
10511058 ir_print_other_instruction(irp, instruction->operand);
......@@ -1860,6 +1867,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
18601867 case IrInstructionIdPtrCastGen:
18611868 ir_print_ptr_cast_gen(irp, (IrInstructionPtrCastGen *)instruction);
18621869 break;
1870 case IrInstructionIdBitCastSrc:
1871 ir_print_bit_cast_src(irp, (IrInstructionBitCastSrc *)instruction);
1872 break;
18631873 case IrInstructionIdBitCastGen:
18641874 ir_print_bit_cast_gen(irp, (IrInstructionBitCastGen *)instruction);
18651875 break;