authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-17 22:13:15-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-17 22:13:15-05:00
loge73faf9a9efad4c3a8deb1ae8a21302f4366c0ac
tree055aa42c4f4fea4bb94230b5b534ff8961568bde
parentd245fabb80c83d1f2b845c42658b6f82d3f89b6f

IR: allow undefined compile time values sometimes


1 files changed, 46 insertions(+), 44 deletions(-)

src/ir.cpp+46-44
......@@ -4533,7 +4533,12 @@ static TypeTableEntry *ir_analyze_const_usize(IrAnalyze *ira, IrInstruction *ins
45334533 return ira->codegen->builtin_types.entry_usize;
45344534}
45354535
4536static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value) {
4536enum UndefAllowed {
4537 UndefOk,
4538 UndefBad,
4539};
4540
4541static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value, UndefAllowed undef_allowed) {
45374542 switch (value->static_value.special) {
45384543 case ConstValSpecialStatic:
45394544 return &value->static_value;
......@@ -4541,8 +4546,12 @@ static ConstExprValue *ir_resolve_const(IrAnalyze *ira, IrInstruction *value) {
45414546 ir_add_error(ira, value, buf_sprintf("unable to evaluate constant expression"));
45424547 return nullptr;
45434548 case ConstValSpecialUndef:
4544 ir_add_error(ira, value, buf_sprintf("use of undefined value"));
4545 return nullptr;
4549 if (undef_allowed == UndefOk) {
4550 return &value->static_value;
4551 } else {
4552 ir_add_error(ira, value, buf_sprintf("use of undefined value"));
4553 return nullptr;
4554 }
45464555 case ConstValSpecialZeroes:
45474556 ir_add_error(ira, value, buf_sprintf("zeroes is deprecated"));
45484557 return nullptr;
......@@ -4595,10 +4604,7 @@ IrInstruction *ir_eval_const_value(CodeGen *codegen, Scope *scope, AstNode *node
45954604 return result;
45964605}
45974606
4598static TypeTableEntry *ir_resolve_type_lval(IrAnalyze *ira, IrInstruction *type_value, LValPurpose lval) {
4599 if (lval != LValPurposeNone)
4600 zig_panic("TODO");
4601
4607static TypeTableEntry *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
46024608 if (type_value->type_entry->id == TypeTableEntryIdInvalid)
46034609 return ira->codegen->builtin_types.entry_invalid;
46044610
......@@ -4608,17 +4614,13 @@ static TypeTableEntry *ir_resolve_type_lval(IrAnalyze *ira, IrInstruction *type_
46084614 return ira->codegen->builtin_types.entry_invalid;
46094615 }
46104616
4611 ConstExprValue *const_val = ir_resolve_const(ira, type_value);
4617 ConstExprValue *const_val = ir_resolve_const(ira, type_value, UndefBad);
46124618 if (!const_val)
46134619 return ira->codegen->builtin_types.entry_invalid;
46144620
46154621 return const_val->data.x_type;
46164622}
46174623
4618static TypeTableEntry *ir_resolve_type(IrAnalyze *ira, IrInstruction *type_value) {
4619 return ir_resolve_type_lval(ira, type_value, LValPurposeNone);
4620}
4621
46224624static FnTableEntry *ir_resolve_fn(IrAnalyze *ira, IrInstruction *fn_value) {
46234625 if (fn_value == ira->codegen->invalid_instruction)
46244626 return nullptr;
......@@ -4632,7 +4634,7 @@ static FnTableEntry *ir_resolve_fn(IrAnalyze *ira, IrInstruction *fn_value) {
46324634 return nullptr;
46334635 }
46344636
4635 ConstExprValue *const_val = ir_resolve_const(ira, fn_value);
4637 ConstExprValue *const_val = ir_resolve_const(ira, fn_value, UndefBad);
46364638 if (!const_val)
46374639 return nullptr;
46384640
......@@ -4643,7 +4645,7 @@ static IrInstruction *ir_analyze_maybe_wrap(IrAnalyze *ira, IrInstruction *sourc
46434645 assert(wanted_type->id == TypeTableEntryIdMaybe);
46444646
46454647 if (instr_is_comptime(value)) {
4646 ConstExprValue *val = ir_resolve_const(ira, value);
4648 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
46474649 if (!val)
46484650 return ira->codegen->invalid_instruction;
46494651
......@@ -4667,7 +4669,7 @@ static IrInstruction *ir_analyze_err_wrap_payload(IrAnalyze *ira, IrInstruction
46674669 assert(wanted_type->id == TypeTableEntryIdErrorUnion);
46684670
46694671 if (instr_is_comptime(value)) {
4670 ConstExprValue *val = ir_resolve_const(ira, value);
4672 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
46714673 if (!val)
46724674 return ira->codegen->invalid_instruction;
46734675
......@@ -4692,7 +4694,7 @@ static IrInstruction *ir_analyze_err_wrap_code(IrAnalyze *ira, IrInstruction *so
46924694 assert(wanted_type->id == TypeTableEntryIdErrorUnion);
46934695
46944696 if (instr_is_comptime(value)) {
4695 ConstExprValue *val = ir_resolve_const(ira, value);
4697 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
46964698 if (!val)
46974699 return ira->codegen->invalid_instruction;
46984700
......@@ -4717,7 +4719,7 @@ static IrInstruction *ir_analyze_null_to_maybe(IrAnalyze *ira, IrInstruction *so
47174719 assert(wanted_type->id == TypeTableEntryIdMaybe);
47184720 assert(instr_is_comptime(value));
47194721
4720 ConstExprValue *val = ir_resolve_const(ira, value);
4722 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
47214723 assert(val);
47224724
47234725 IrInstructionConst *const_instruction = ir_create_instruction<IrInstructionConst>(ira->new_irb.exec, source_instr->scope, source_instr->source_node);
......@@ -5020,7 +5022,7 @@ static IrInstruction *ir_get_deref(IrAnalyze *ira, IrInstruction *source_instruc
50205022 load_ptr_instruction->type_entry = child_type;
50215023 return load_ptr_instruction;
50225024 } else if (type_entry->id == TypeTableEntryIdMetaType) {
5023 ConstExprValue *ptr_val = ir_resolve_const(ira, ptr);
5025 ConstExprValue *ptr_val = ir_resolve_const(ira, ptr, UndefBad);
50245026 if (!ptr_val)
50255027 return ira->codegen->invalid_instruction;
50265028
......@@ -5047,7 +5049,7 @@ static TypeTableEntry *ir_analyze_ref(IrAnalyze *ira, IrInstruction *source_inst
50475049
50485050 bool is_inline = ir_should_inline(&ira->new_irb);
50495051 if (is_inline || value->static_value.special != ConstValSpecialRuntime) {
5050 ConstExprValue *val = ir_resolve_const(ira, value);
5052 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
50515053 if (!val)
50525054 return ira->codegen->builtin_types.entry_invalid;
50535055 bool ptr_is_const = true;
......@@ -5071,7 +5073,7 @@ static bool ir_resolve_usize(IrAnalyze *ira, IrInstruction *value, uint64_t *out
50715073 if (casted_value->type_entry->id == TypeTableEntryIdInvalid)
50725074 return false;
50735075
5074 ConstExprValue *const_val = ir_resolve_const(ira, casted_value);
5076 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);
50755077 if (!const_val)
50765078 return false;
50775079
......@@ -5087,7 +5089,7 @@ static bool ir_resolve_bool(IrAnalyze *ira, IrInstruction *value, bool *out) {
50875089 if (casted_value->type_entry->id == TypeTableEntryIdInvalid)
50885090 return false;
50895091
5090 ConstExprValue *const_val = ir_resolve_const(ira, casted_value);
5092 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);
50915093 if (!const_val)
50925094 return false;
50935095
......@@ -5103,7 +5105,7 @@ static bool ir_resolve_atomic_order(IrAnalyze *ira, IrInstruction *value, Atomic
51035105 if (casted_value->type_entry->id == TypeTableEntryIdInvalid)
51045106 return false;
51055107
5106 ConstExprValue *const_val = ir_resolve_const(ira, casted_value);
5108 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);
51075109 if (!const_val)
51085110 return false;
51095111
......@@ -5120,7 +5122,7 @@ static Buf *ir_resolve_str(IrAnalyze *ira, IrInstruction *value) {
51205122 if (casted_value->type_entry->id == TypeTableEntryIdInvalid)
51215123 return nullptr;
51225124
5123 ConstExprValue *const_val = ir_resolve_const(ira, casted_value);
5125 ConstExprValue *const_val = ir_resolve_const(ira, casted_value, UndefBad);
51245126 if (!const_val)
51255127 return nullptr;
51265128
......@@ -5504,11 +5506,11 @@ static TypeTableEntry *ir_analyze_array_cat(IrAnalyze *ira, IrInstructionBinOp *
55045506 if (op2_canon_type->id == TypeTableEntryIdInvalid)
55055507 return ira->codegen->builtin_types.entry_invalid;
55065508
5507 ConstExprValue *op1_val = ir_resolve_const(ira, op1);
5509 ConstExprValue *op1_val = ir_resolve_const(ira, op1, UndefBad);
55085510 if (!op1_val)
55095511 return ira->codegen->builtin_types.entry_invalid;
55105512
5511 ConstExprValue *op2_val = ir_resolve_const(ira, op2);
5513 ConstExprValue *op2_val = ir_resolve_const(ira, op2, UndefBad);
55125514 if (!op2_val)
55135515 return ira->codegen->builtin_types.entry_invalid;
55145516
......@@ -5620,7 +5622,7 @@ static TypeTableEntry *ir_analyze_array_mult(IrAnalyze *ira, IrInstructionBinOp
56205622 if (op2->type_entry->id == TypeTableEntryIdInvalid)
56215623 return ira->codegen->builtin_types.entry_invalid;
56225624
5623 ConstExprValue *array_val = ir_resolve_const(ira, op1);
5625 ConstExprValue *array_val = ir_resolve_const(ira, op1, UndefBad);
56245626 if (!array_val)
56255627 return ira->codegen->builtin_types.entry_invalid;
56265628
......@@ -5825,7 +5827,7 @@ static bool ir_analyze_fn_call_inline_arg(IrAnalyze *ira, AstNode *fn_proto_node
58255827 if (casted_arg->type_entry->id == TypeTableEntryIdInvalid)
58265828 return false;
58275829
5828 ConstExprValue *first_arg_val = ir_resolve_const(ira, casted_arg);
5830 ConstExprValue *first_arg_val = ir_resolve_const(ira, casted_arg, UndefBad);
58295831 if (!first_arg_val)
58305832 return false;
58315833
......@@ -5862,7 +5864,7 @@ static bool ir_analyze_fn_call_generic_arg(IrAnalyze *ira, AstNode *fn_proto_nod
58625864
58635865 bool inline_arg = param_decl_node->data.param_decl.is_inline;
58645866 if (inline_arg || is_var_type) {
5865 ConstExprValue *arg_val = ir_resolve_const(ira, casted_arg);
5867 ConstExprValue *arg_val = ir_resolve_const(ira, casted_arg, UndefBad);
58665868 if (!arg_val)
58675869 return false;
58685870
......@@ -6797,7 +6799,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
67976799 return ira->codegen->builtin_types.entry_invalid;
67986800 }
67996801 } else if (container_type->id == TypeTableEntryIdMetaType) {
6800 ConstExprValue *container_ptr_val = ir_resolve_const(ira, container_ptr);
6802 ConstExprValue *container_ptr_val = ir_resolve_const(ira, container_ptr, UndefBad);
68016803 if (!container_ptr_val)
68026804 return ira->codegen->builtin_types.entry_invalid;
68036805
......@@ -6880,7 +6882,7 @@ static TypeTableEntry *ir_analyze_instruction_field_ptr(IrAnalyze *ira, IrInstru
68806882 }
68816883 } else if (container_type->id == TypeTableEntryIdNamespace) {
68826884 assert(container_ptr->type_entry->id == TypeTableEntryIdPointer);
6883 ConstExprValue *container_ptr_val = ir_resolve_const(ira, container_ptr);
6885 ConstExprValue *container_ptr_val = ir_resolve_const(ira, container_ptr, UndefBad);
68846886 if (!container_ptr_val)
68856887 return ira->codegen->builtin_types.entry_invalid;
68866888
......@@ -7148,7 +7150,7 @@ static TypeTableEntry *ir_analyze_instruction_set_debug_safety(IrAnalyze *ira,
71487150 TypeTableEntry *target_type = target_instruction->type_entry;
71497151 if (target_type->id == TypeTableEntryIdInvalid)
71507152 return ira->codegen->builtin_types.entry_invalid;
7151 ConstExprValue *target_val = ir_resolve_const(ira, target_instruction);
7153 ConstExprValue *target_val = ir_resolve_const(ira, target_instruction, UndefBad);
71527154 if (!target_val)
71537155 return ira->codegen->builtin_types.entry_invalid;
71547156
......@@ -7495,7 +7497,7 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_maybe(IrAnalyze *ira,
74957497 TypeTableEntry *result_type = get_pointer_to_type(ira->codegen, child_type, false);
74967498
74977499 if (instr_is_comptime(value)) {
7498 ConstExprValue *val = ir_resolve_const(ira, value);
7500 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
74997501 if (!val)
75007502 return ira->codegen->builtin_types.entry_invalid;
75017503 ConstExprValue *maybe_val = val->data.x_ptr.base_ptr;
......@@ -7579,7 +7581,7 @@ static IrInstruction *ir_analyze_enum_tag(IrAnalyze *ira, IrInstruction *source_
75797581 }
75807582
75817583 if (instr_is_comptime(value)) {
7582 ConstExprValue *val = ir_resolve_const(ira, value);
7584 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
75837585 if (!val)
75847586 return ira->codegen->invalid_instruction;
75857587
......@@ -7606,7 +7608,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
76067608 bool is_inline = ir_should_inline(&ira->new_irb) || switch_br_instruction->is_inline;
76077609
76087610 if (is_inline || instr_is_comptime(target_value)) {
7609 ConstExprValue *target_val = ir_resolve_const(ira, target_value);
7611 ConstExprValue *target_val = ir_resolve_const(ira, target_value, UndefBad);
76107612 if (!target_val)
76117613 return ir_unreach_error(ira);
76127614
......@@ -7626,7 +7628,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
76267628 if (casted_case_value->type_entry->id == TypeTableEntryIdInvalid)
76277629 return ir_unreach_error(ira);
76287630
7629 ConstExprValue *case_val = ir_resolve_const(ira, casted_case_value);
7631 ConstExprValue *case_val = ir_resolve_const(ira, casted_case_value, UndefBad);
76307632 if (!case_val)
76317633 return ir_unreach_error(ira);
76327634
......@@ -7666,7 +7668,7 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
76667668 if (casted_new_value->type_entry->id == TypeTableEntryIdInvalid)
76677669 continue;
76687670
7669 if (!ir_resolve_const(ira, casted_new_value))
7671 if (!ir_resolve_const(ira, casted_new_value, UndefBad))
76707672 continue;
76717673
76727674 new_case->value = casted_new_value;
......@@ -7776,7 +7778,7 @@ static TypeTableEntry *ir_analyze_instruction_static_eval(IrAnalyze *ira,
77767778 if (value->type_entry->id == TypeTableEntryIdInvalid)
77777779 return ira->codegen->builtin_types.entry_invalid;
77787780
7779 ConstExprValue *val = ir_resolve_const(ira, value);
7781 ConstExprValue *val = ir_resolve_const(ira, value, UndefBad);
77807782 if (!val)
77817783 return ira->codegen->builtin_types.entry_invalid;
77827784
......@@ -7949,7 +7951,7 @@ static TypeTableEntry *ir_analyze_container_init_fields(IrAnalyze *ira, IrInstru
79497951
79507952 if (const_val.special == ConstValSpecialStatic) {
79517953 if (outside_fn || field_value->static_value.special != ConstValSpecialRuntime) {
7952 ConstExprValue *field_val = ir_resolve_const(ira, field_value);
7954 ConstExprValue *field_val = ir_resolve_const(ira, field_value, UndefOk);
79537955 if (!field_val)
79547956 return ira->codegen->builtin_types.entry_invalid;
79557957
......@@ -8030,7 +8032,7 @@ static TypeTableEntry *ir_analyze_instruction_container_init_list(IrAnalyze *ira
80308032
80318033 if (const_val.special == ConstValSpecialStatic) {
80328034 if (outside_fn || arg_value->static_value.special != ConstValSpecialRuntime) {
8033 ConstExprValue *elem_val = ir_resolve_const(ira, arg_value);
8035 ConstExprValue *elem_val = ir_resolve_const(ira, arg_value, UndefBad);
80348036 if (!elem_val)
80358037 return ira->codegen->builtin_types.entry_invalid;
80368038
......@@ -8494,8 +8496,8 @@ static TypeTableEntry *ir_analyze_instruction_div_exact(IrAnalyze *ira, IrInstru
84948496 if (casted_op1->static_value.special == ConstValSpecialStatic &&
84958497 casted_op2->static_value.special == ConstValSpecialStatic)
84968498 {
8497 ConstExprValue *op1_val = ir_resolve_const(ira, casted_op1);
8498 ConstExprValue *op2_val = ir_resolve_const(ira, casted_op2);
8499 ConstExprValue *op1_val = ir_resolve_const(ira, casted_op1, UndefBad);
8500 ConstExprValue *op2_val = ir_resolve_const(ira, casted_op2, UndefBad);
84998501 assert(op1_val);
85008502 assert(op2_val);
85018503
......@@ -9140,7 +9142,7 @@ static TypeTableEntry *ir_analyze_instruction_test_err(IrAnalyze *ira, IrInstruc
91409142 return ira->codegen->builtin_types.entry_invalid;
91419143 } else if (canon_type->id == TypeTableEntryIdErrorUnion) {
91429144 if (instr_is_comptime(value)) {
9143 ConstExprValue *ptr_val = ir_resolve_const(ira, value);
9145 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
91449146 if (!ptr_val)
91459147 return ira->codegen->builtin_types.entry_invalid;
91469148 ConstExprValue *err_union_val = ptr_val->data.x_ptr.base_ptr;
......@@ -9181,7 +9183,7 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_code(IrAnalyze *ira,
91819183 return ira->codegen->builtin_types.entry_invalid;
91829184 } else if (canon_type->id == TypeTableEntryIdErrorUnion) {
91839185 if (instr_is_comptime(value)) {
9184 ConstExprValue *ptr_val = ir_resolve_const(ira, value);
9186 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
91859187 if (!ptr_val)
91869188 return ira->codegen->builtin_types.entry_invalid;
91879189 ConstExprValue *err_union_val = ptr_val->data.x_ptr.base_ptr;
......@@ -9226,7 +9228,7 @@ static TypeTableEntry *ir_analyze_instruction_unwrap_err_payload(IrAnalyze *ira,
92269228 TypeTableEntry *child_type = canon_type->data.error.child_type;
92279229 TypeTableEntry *result_type = get_pointer_to_type(ira->codegen, child_type, false);
92289230 if (instr_is_comptime(value)) {
9229 ConstExprValue *ptr_val = ir_resolve_const(ira, value);
9231 ConstExprValue *ptr_val = ir_resolve_const(ira, value, UndefBad);
92309232 if (!ptr_val)
92319233 return ira->codegen->builtin_types.entry_invalid;
92329234 ConstExprValue *err_union_val = ptr_val->data.x_ptr.base_ptr;