authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-28 00:02:53-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-11-28 00:02:53-05:00
logbcdb3a90066148dc5a91d176a8fc7f5d9c7487b1
tree727cee5fbbf65e81aff41b4e24b65fc065960275
parentbf3ac6615051143a9ef41180cd74e88de5dd573d
signaturelock-open Commit is signed but in an unrecognized format.

more progress


5 files changed, 126 insertions(+), 33 deletions(-)

src/ir.cpp+51-21
......@@ -41,6 +41,9 @@ struct IrAnalyze {
4141 ZigList<IrInstruction *> src_implicit_return_type_list;
4242 ZigList<IrSuspendPosition> resume_stack;
4343 IrBasicBlock *const_predecessor_bb;
44
45 // For the purpose of using in a debugger
46 void dump();
4447};
4548
4649enum ConstCastResultId {
......@@ -350,6 +353,7 @@ static bool types_have_same_zig_comptime_repr(CodeGen *codegen, ZigType *expecte
350353 case ZigTypeIdErrorSet:
351354 case ZigTypeIdOpaque:
352355 case ZigTypeIdAnyFrame:
356 case ZigTypeIdFn:
353357 return true;
354358 case ZigTypeIdFloat:
355359 return expected->data.floating.bit_count == actual->data.floating.bit_count;
......@@ -361,7 +365,6 @@ static bool types_have_same_zig_comptime_repr(CodeGen *codegen, ZigType *expecte
361365 case ZigTypeIdErrorUnion:
362366 case ZigTypeIdEnum:
363367 case ZigTypeIdUnion:
364 case ZigTypeIdFn:
365368 case ZigTypeIdArgTuple:
366369 case ZigTypeIdVector:
367370 case ZigTypeIdFnFrame:
......@@ -3941,6 +3944,7 @@ static IrInstruction *ir_gen_block(IrBuilder *irb, Scope *parent_scope, AstNode
39413944 scope_block->peer_parent = allocate<ResultLocPeerParent>(1, "ResultLocPeerParent");
39423945 scope_block->peer_parent->base.id = ResultLocIdPeerParent;
39433946 scope_block->peer_parent->base.source_instruction = scope_block->is_comptime;
3947 scope_block->peer_parent->base.allow_write_through_const = result_loc->allow_write_through_const;
39443948 scope_block->peer_parent->end_bb = scope_block->end_block;
39453949 scope_block->peer_parent->is_comptime = scope_block->is_comptime;
39463950 scope_block->peer_parent->parent = result_loc;
......@@ -4195,6 +4199,7 @@ static ResultLocPeerParent *ir_build_result_peers(IrBuilder *irb, IrInstruction
41954199 ResultLocPeerParent *peer_parent = allocate<ResultLocPeerParent>(1);
41964200 peer_parent->base.id = ResultLocIdPeerParent;
41974201 peer_parent->base.source_instruction = cond_br_inst;
4202 peer_parent->base.allow_write_through_const = parent->allow_write_through_const;
41984203 peer_parent->end_bb = end_block;
41994204 peer_parent->is_comptime = is_comptime;
42004205 peer_parent->parent = parent;
......@@ -6388,6 +6393,7 @@ static ResultLocVar *ir_build_var_result_loc(IrBuilder *irb, IrInstruction *allo
63886393 ResultLocVar *result_loc_var = allocate<ResultLocVar>(1);
63896394 result_loc_var->base.id = ResultLocIdVar;
63906395 result_loc_var->base.source_instruction = alloca;
6396 result_loc_var->base.allow_write_through_const = true;
63916397 result_loc_var->var = var;
63926398
63936399 ir_build_reset_result(irb, alloca->scope, alloca->source_node, &result_loc_var->base);
......@@ -6401,6 +6407,7 @@ static ResultLocCast *ir_build_cast_result_loc(IrBuilder *irb, IrInstruction *de
64016407 ResultLocCast *result_loc_cast = allocate<ResultLocCast>(1);
64026408 result_loc_cast->base.id = ResultLocIdCast;
64036409 result_loc_cast->base.source_instruction = dest_type;
6410 result_loc_cast->base.allow_write_through_const = parent_result_loc->allow_write_through_const;
64046411 ir_ref_instruction(dest_type, irb->current_basic_block);
64056412 result_loc_cast->parent = parent_result_loc;
64066413
......@@ -7581,6 +7588,7 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, Scope *scope, AstNode *
75817588
75827589 ResultLocPeerParent *peer_parent = allocate<ResultLocPeerParent>(1);
75837590 peer_parent->base.id = ResultLocIdPeerParent;
7591 peer_parent->base.allow_write_through_const = result_loc->allow_write_through_const;
75847592 peer_parent->end_bb = end_block;
75857593 peer_parent->is_comptime = is_comptime;
75867594 peer_parent->parent = result_loc;
......@@ -13396,16 +13404,24 @@ static IrInstruction *ir_analyze_cast(IrAnalyze *ira, IrInstruction *source_inst
1339613404 return ir_analyze_undefined_to_anything(ira, source_instr, value, wanted_type);
1339713405 }
1339813406
13399 // T to ?E!T
13400 if (wanted_type->id == ZigTypeIdOptional && wanted_type->data.maybe.child_type->id == ZigTypeIdErrorUnion &&
13401 actual_type->id != ZigTypeIdOptional)
13402 {
13407 // T to ?U, where T implicitly casts to U
13408 if (wanted_type->id == ZigTypeIdOptional && actual_type->id != ZigTypeIdOptional) {
1340313409 IrInstruction *cast1 = ir_implicit_cast2(ira, source_instr, value, wanted_type->data.maybe.child_type);
1340413410 if (type_is_invalid(cast1->value->type))
1340513411 return ira->codegen->invalid_instruction;
1340613412 return ir_implicit_cast2(ira, source_instr, cast1, wanted_type);
1340713413 }
1340813414
13415 // T to E!U, where T implicitly casts to U
13416 if (wanted_type->id == ZigTypeIdErrorUnion && actual_type->id != ZigTypeIdErrorUnion &&
13417 actual_type->id != ZigTypeIdErrorSet)
13418 {
13419 IrInstruction *cast1 = ir_implicit_cast2(ira, source_instr, value, wanted_type->data.error_union.payload_type);
13420 if (type_is_invalid(cast1->value->type))
13421 return ira->codegen->invalid_instruction;
13422 return ir_implicit_cast2(ira, source_instr, cast1, wanted_type);
13423 }
13424
1340913425 ErrorMsg *parent_msg = ir_add_error_node(ira, source_instr->source_node,
1341013426 buf_sprintf("expected type '%s', found '%s'",
1341113427 buf_ptr(&wanted_type->name),
......@@ -16046,7 +16062,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1604616062 bool force_comptime;
1604716063 if (!ir_resolve_comptime(ira, alloca_src->is_comptime->child, &force_comptime))
1604816064 return ira->codegen->invalid_instruction;
16049 bool is_comptime = force_comptime || (value != nullptr &&
16065 bool is_comptime = force_comptime || (!force_runtime && value != nullptr &&
1605016066 value->value->special != ConstValSpecialRuntime && result_loc_var->var->gen_is_const);
1605116067
1605216068 if (alloca_src->base.child == nullptr || is_comptime) {
......@@ -16064,7 +16080,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1606416080 alloca_gen = ir_analyze_alloca(ira, result_loc->source_instruction, value_type, align,
1606516081 alloca_src->name_hint, force_comptime);
1606616082 }
16067 if (alloca_src->base.child != nullptr) {
16083 if (alloca_src->base.child != nullptr && !result_loc->written) {
1606816084 alloca_src->base.child->ref_count = 0;
1606916085 }
1607016086 alloca_src->base.child = alloca_gen;
......@@ -16079,6 +16095,10 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1607916095 return result_loc->resolved_loc;
1608016096 }
1608116097 case ResultLocIdReturn: {
16098 if (value != nullptr) {
16099 reinterpret_cast<ResultLocReturn *>(result_loc)->implicit_return_type_done = true;
16100 ira->src_implicit_return_type_list.append(value);
16101 }
1608216102 if (!non_null_comptime) {
1608316103 bool is_comptime = value != nullptr && value->value->special != ConstValSpecialRuntime;
1608416104 if (is_comptime)
......@@ -16121,10 +16141,10 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1612116141 return result_loc->resolved_loc;
1612216142 }
1612316143
16124 bool is_comptime;
16125 if (!ir_resolve_comptime(ira, peer_parent->is_comptime->child, &is_comptime))
16144 bool is_condition_comptime;
16145 if (!ir_resolve_comptime(ira, peer_parent->is_comptime->child, &is_condition_comptime))
1612616146 return ira->codegen->invalid_instruction;
16127 if (is_comptime) {
16147 if (is_condition_comptime) {
1612816148 peer_parent->skipped = true;
1612916149 if (non_null_comptime) {
1613016150 return ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
......@@ -16136,17 +16156,18 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1613616156 if ((err = ir_result_has_type(ira, peer_parent->parent, &peer_parent_has_type)))
1613716157 return ira->codegen->invalid_instruction;
1613816158 if (peer_parent_has_type) {
16139 if (peer_parent->parent->id == ResultLocIdReturn && value != nullptr) {
16140 reinterpret_cast<ResultLocReturn *>(peer_parent->parent)->implicit_return_type_done = true;
16141 ira->src_implicit_return_type_list.append(value);
16142 }
1614316159 peer_parent->skipped = true;
1614416160 IrInstruction *parent_result_loc = ir_resolve_result(ira, suspend_source_instr, peer_parent->parent,
16145 value_type, value, force_runtime || !is_comptime, true, true);
16146 if (parent_result_loc != nullptr) {
16147 peer_parent->parent->written = true;
16161 value_type, value, force_runtime || !is_condition_comptime, true, true);
16162 if (parent_result_loc == nullptr || type_is_invalid(parent_result_loc->value->type) ||
16163 parent_result_loc->value->type->id == ZigTypeIdUnreachable)
16164 {
16165 return parent_result_loc;
1614816166 }
16149 return parent_result_loc;
16167 peer_parent->parent->written = true;
16168 result_loc->written = true;
16169 result_loc->resolved_loc = parent_result_loc;
16170 return result_loc->resolved_loc;
1615016171 }
1615116172
1615216173 if (peer_parent->resolved_type == nullptr) {
......@@ -16168,14 +16189,14 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1616816189 {
1616916190 return parent_result_loc;
1617016191 }
16171 // because is_comptime is false, we mark this a runtime pointer
16192 // because is_condition_comptime is false, we mark this a runtime pointer
1617216193 parent_result_loc->value->special = ConstValSpecialRuntime;
1617316194 result_loc->written = true;
1617416195 result_loc->resolved_loc = parent_result_loc;
1617516196 return result_loc->resolved_loc;
1617616197 }
1617716198 case ResultLocIdCast: {
16178 if (value != nullptr && value->value->special != ConstValSpecialRuntime)
16199 if (value != nullptr && value->value->special != ConstValSpecialRuntime && !non_null_comptime)
1617916200 return nullptr;
1618016201 ResultLocCast *result_cast = reinterpret_cast<ResultLocCast *>(result_loc);
1618116202 ZigType *dest_type = ir_resolve_type(ira, result_cast->base.source_instruction->child);
......@@ -16204,6 +16225,7 @@ static IrInstruction *ir_resolve_result_raw(IrAnalyze *ira, IrInstruction *suspe
1620416225 {
1620516226 return parent_result_loc;
1620616227 }
16228
1620716229 ZigType *parent_ptr_type = parent_result_loc->value->type;
1620816230 assert(parent_ptr_type->id == ZigTypeIdPointer);
1620916231 if ((err = type_resolve(ira->codegen, parent_ptr_type->data.pointer.child_type,
......@@ -16354,7 +16376,7 @@ static IrInstruction *ir_resolve_result(IrAnalyze *ira, IrInstruction *suspend_s
1635416376 {
1635516377 result_loc_pass1->written = false;
1635616378 return ir_analyze_unwrap_optional_payload(ira, suspend_source_instr, result_loc, false, true);
16357 } else if (actual_elem_type->id == ZigTypeIdErrorUnion && value_type->id != ZigTypeIdErrorUnion) {
16379 } else if (actual_elem_type->id == ZigTypeIdErrorUnion && value_type->id != ZigTypeIdErrorUnion && value == nullptr) {
1635816380 if (value_type->id == ZigTypeIdErrorSet) {
1635916381 return ir_analyze_unwrap_err_code(ira, suspend_source_instr, result_loc, true);
1636016382 } else {
......@@ -28238,3 +28260,11 @@ void IrInstruction::dump() {
2823828260 ir_print_instruction(inst->scope->codegen, stderr, inst->child, 0, IrPassGen);
2823928261 }
2824028262}
28263
28264void IrAnalyze::dump() {
28265 ir_print(this->codegen, stderr, this->new_irb.exec, 0, IrPassGen);
28266 if (this->new_irb.current_basic_block != nullptr) {
28267 fprintf(stderr, "Current basic block:\n");
28268 ir_print_basic_block(this->codegen, stderr, this->new_irb.current_basic_block, 1, IrPassGen);
28269 }
28270}
src/ir_print.cpp+32-12
......@@ -2530,6 +2530,37 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction, bool
25302530 fprintf(irp->f, "\n");
25312531}
25322532
2533static void irp_print_basic_block(IrPrint *irp, IrBasicBlock *current_block) {
2534 fprintf(irp->f, "%s_%" ZIG_PRI_usize ":\n", current_block->name_hint, current_block->debug_id);
2535 for (size_t instr_i = 0; instr_i < current_block->instruction_list.length; instr_i += 1) {
2536 IrInstruction *instruction = current_block->instruction_list.at(instr_i);
2537 if (irp->pass != IrPassSrc) {
2538 irp->printed.put(instruction, 0);
2539 irp->pending.clear();
2540 }
2541 ir_print_instruction(irp, instruction, false);
2542 for (size_t j = 0; j < irp->pending.length; ++j)
2543 ir_print_instruction(irp, irp->pending.at(j), true);
2544 }
2545}
2546
2547void ir_print_basic_block(CodeGen *codegen, FILE *f, IrBasicBlock *bb, int indent_size, IrPass pass) {
2548 IrPrint ir_print = {};
2549 ir_print.pass = pass;
2550 ir_print.codegen = codegen;
2551 ir_print.f = f;
2552 ir_print.indent = indent_size;
2553 ir_print.indent_size = indent_size;
2554 ir_print.printed = {};
2555 ir_print.printed.init(64);
2556 ir_print.pending = {};
2557
2558 irp_print_basic_block(&ir_print, bb);
2559
2560 ir_print.pending.deinit();
2561 ir_print.printed.deinit();
2562}
2563
25332564void ir_print(CodeGen *codegen, FILE *f, IrExecutable *executable, int indent_size, IrPass pass) {
25342565 IrPrint ir_print = {};
25352566 IrPrint *irp = &ir_print;
......@@ -2543,18 +2574,7 @@ void ir_print(CodeGen *codegen, FILE *f, IrExecutable *executable, int indent_si
25432574 irp->pending = {};
25442575
25452576 for (size_t bb_i = 0; bb_i < executable->basic_block_list.length; bb_i += 1) {
2546 IrBasicBlock *current_block = executable->basic_block_list.at(bb_i);
2547 fprintf(irp->f, "%s_%" ZIG_PRI_usize ":\n", current_block->name_hint, current_block->debug_id);
2548 for (size_t instr_i = 0; instr_i < current_block->instruction_list.length; instr_i += 1) {
2549 IrInstruction *instruction = current_block->instruction_list.at(instr_i);
2550 if (irp->pass != IrPassSrc) {
2551 irp->printed.put(instruction, 0);
2552 irp->pending.clear();
2553 }
2554 ir_print_instruction(irp, instruction, false);
2555 for (size_t j = 0; j < irp->pending.length; ++j)
2556 ir_print_instruction(irp, irp->pending.at(j), true);
2557 }
2577 irp_print_basic_block(irp, executable->basic_block_list.at(bb_i));
25582578 }
25592579
25602580 irp->pending.deinit();
src/ir_print.hpp+1
......@@ -15,6 +15,7 @@
1515void ir_print(CodeGen *codegen, FILE *f, IrExecutable *executable, int indent_size, IrPass pass);
1616void ir_print_instruction(CodeGen *codegen, FILE *f, IrInstruction *instruction, int indent_size, IrPass pass);
1717void ir_print_const_expr(CodeGen *codegen, FILE *f, ZigValue *value, int indent_size, IrPass pass);
18void ir_print_basic_block(CodeGen *codegen, FILE *f, IrBasicBlock *bb, int indent_size, IrPass pass);
1819
1920const char* ir_instruction_type_str(IrInstructionId id);
2021
test/stage1/behavior/cast.zig+13
......@@ -697,3 +697,16 @@ test "cast i8 fn call peers to i32 result" {
697697 S.doTheTest();
698698 comptime S.doTheTest();
699699}
700
701test "return u8 coercing into ?u32 return type" {
702 const S = struct {
703 fn doTheTest() void {
704 expect(foo(123).? == 123);
705 }
706 fn foo(arg: u8) ?u32 {
707 return arg;
708 }
709 };
710 S.doTheTest();
711 comptime S.doTheTest();
712}
test/stage1/behavior/union.zig+29
......@@ -582,3 +582,32 @@ test "update the tag value for zero-sized unions" {
582582 x = S{ .U1 = {} };
583583 expect(x == .U1);
584584}
585
586test "function call result coerces from tagged union to the tag" {
587 const S = struct {
588 const Arch = union(enum) {
589 One,
590 Two: usize,
591 };
592
593 const ArchTag = @TagType(Arch);
594
595 fn doTheTest() void {
596 var x: ArchTag = getArch1();
597 expect(x == .One);
598
599 var y: ArchTag = getArch2();
600 expect(y == .Two);
601 }
602
603 pub fn getArch1() Arch {
604 return .One;
605 }
606
607 pub fn getArch2() Arch {
608 return .{ .Two = 99 };
609 }
610 };
611 S.doTheTest();
612 comptime S.doTheTest();
613}