authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-26 04:37:34-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-26 04:37:34-05:00
log24b65e41ee241c805e0eff8212ef49c5c39e4b8e
tree21a244d5af04f68a9eb4e88ea7ce025758f6fcab
parent697c768730ad4c095c376079adbb97854db84cb9

IR: add error for non static const on switch case range


5 files changed, 80 insertions(+), 38 deletions(-)

doc/langref.md+1-1
......@@ -531,7 +531,7 @@ Build scripts can set additional compile variables of any name and type.
531531The result of this function is a compile time constant that is marked as
532532depending on a compile variable.
533533
534### @constEval(expression) -> @typeof(expression)
534### @staticEval(expression) -> @typeOf(expression)
535535
536536This function wraps an expression and generates a compile error if the
537537expression is not known at compile time.
src/all_types.hpp+8-1
......@@ -1146,7 +1146,7 @@ enum BuiltinFnId {
11461146 BuiltinFnIdCUndef,
11471147 BuiltinFnIdCompileVar,
11481148 BuiltinFnIdCompileErr,
1149 BuiltinFnIdConstEval,
1149 BuiltinFnIdStaticEval,
11501150 BuiltinFnIdCtz,
11511151 BuiltinFnIdClz,
11521152 BuiltinFnIdImport,
......@@ -1458,6 +1458,7 @@ enum IrInstructionId {
14581458 IrInstructionIdEnumTag,
14591459 IrInstructionIdClz,
14601460 IrInstructionIdCtz,
1461 IrInstructionIdStaticEval,
14611462};
14621463
14631464struct IrInstruction {
......@@ -1808,6 +1809,12 @@ struct IrInstructionEnumTag {
18081809 IrInstruction *value;
18091810};
18101811
1812struct IrInstructionStaticEval {
1813 IrInstruction base;
1814
1815 IrInstruction *value;
1816};
1817
18111818enum LValPurpose {
18121819 LValPurposeNone,
18131820 LValPurposeAssign,
src/codegen.cpp+2-1
......@@ -1681,6 +1681,7 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable,
16811681 case IrInstructionIdCompileVar:
16821682 case IrInstructionIdSizeOf:
16831683 case IrInstructionIdSwitchTarget:
1684 case IrInstructionIdStaticEval:
16841685 zig_unreachable();
16851686 case IrInstructionIdReturn:
16861687 return ir_render_return(g, executable, (IrInstructionReturn *)instruction);
......@@ -2968,7 +2969,7 @@ static void define_builtin_fns(CodeGen *g) {
29682969 create_builtin_fn_with_arg_count(g, BuiltinFnIdCDefine, "cDefine", 2);
29692970 create_builtin_fn_with_arg_count(g, BuiltinFnIdCUndef, "cUndef", 1);
29702971 create_builtin_fn_with_arg_count(g, BuiltinFnIdCompileVar, "compileVar", 1);
2971 create_builtin_fn_with_arg_count(g, BuiltinFnIdConstEval, "constEval", 1);
2972 create_builtin_fn_with_arg_count(g, BuiltinFnIdStaticEval, "staticEval", 1);
29722973 create_builtin_fn_with_arg_count(g, BuiltinFnIdCtz, "ctz", 1);
29732974 create_builtin_fn_with_arg_count(g, BuiltinFnIdClz, "clz", 1);
29742975 create_builtin_fn_with_arg_count(g, BuiltinFnIdImport, "import", 1);
src/ir.cpp+60-35
......@@ -263,6 +263,10 @@ static constexpr IrInstructionId ir_instruction_id(IrInstructionEnumTag *) {
263263 return IrInstructionIdEnumTag;
264264}
265265
266static constexpr IrInstructionId ir_instruction_id(IrInstructionStaticEval *) {
267 return IrInstructionIdStaticEval;
268}
269
266270template<typename T>
267271static T *ir_create_instruction(IrExecutable *exec, AstNode *source_node) {
268272 T *special_instruction = allocate<T>(1);
......@@ -1074,6 +1078,15 @@ static IrInstruction *ir_build_enum_tag_from(IrBuilder *irb, IrInstruction *old_
10741078 return new_instruction;
10751079}
10761080
1081static IrInstruction *ir_build_static_eval(IrBuilder *irb, AstNode *source_node, IrInstruction *value) {
1082 IrInstructionStaticEval *instruction = ir_build_instruction<IrInstructionStaticEval>(irb, source_node);
1083 instruction->value = value;
1084
1085 ir_ref_instruction(value);
1086
1087 return &instruction->base;
1088}
1089
10771090static void ir_gen_defers_for_block(IrBuilder *irb, BlockContext *inner_block, BlockContext *outer_block,
10781091 bool gen_error_defers, bool gen_maybe_defers)
10791092{
......@@ -1606,6 +1619,15 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, AstNode *node) {
16061619
16071620 return ir_build_clz(irb, node, arg0_value);
16081621 }
1622 case BuiltinFnIdStaticEval:
1623 {
1624 AstNode *arg0_node = node->data.fn_call_expr.params.at(0);
1625 IrInstruction *arg0_value = ir_gen_node(irb, arg0_node, node->block_context);
1626 if (arg0_value == irb->codegen->invalid_instruction)
1627 return arg0_value;
1628
1629 return ir_build_static_eval(irb, node, arg0_value);
1630 }
16091631 case BuiltinFnIdMemcpy:
16101632 case BuiltinFnIdMemset:
16111633 case BuiltinFnIdAlignof:
......@@ -1620,7 +1642,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, AstNode *node) {
16201642 case BuiltinFnIdCDefine:
16211643 case BuiltinFnIdCUndef:
16221644 case BuiltinFnIdCompileErr:
1623 case BuiltinFnIdConstEval:
16241645 case BuiltinFnIdImport:
16251646 case BuiltinFnIdCImport:
16261647 case BuiltinFnIdErrName:
......@@ -2285,14 +2306,18 @@ static IrInstruction *ir_gen_switch_expr(IrBuilder *irb, AstNode *node) {
22852306 IrInstruction *start_value = ir_gen_node(irb, start_node, node->block_context);
22862307 if (start_value == irb->codegen->invalid_instruction)
22872308 return irb->codegen->invalid_instruction;
2309
22882310 IrInstruction *end_value = ir_gen_node(irb, end_node, node->block_context);
22892311 if (end_value == irb->codegen->invalid_instruction)
22902312 return irb->codegen->invalid_instruction;
22912313
2314 IrInstruction *start_value_const = ir_build_static_eval(irb, start_node, start_value);
2315 IrInstruction *end_value_const = ir_build_static_eval(irb, start_node, end_value);
2316
22922317 IrInstruction *lower_range_ok = ir_build_bin_op(irb, item_node, IrBinOpCmpGreaterOrEq,
2293 target_value, start_value);
2318 target_value, start_value_const);
22942319 IrInstruction *upper_range_ok = ir_build_bin_op(irb, item_node, IrBinOpCmpLessOrEq,
2295 target_value, end_value);
2320 target_value, end_value_const);
22962321 IrInstruction *both_ok = ir_build_bin_op(irb, item_node, IrBinOpBoolAnd,
22972322 lower_range_ok, upper_range_ok);
22982323 if (ok_bit) {
......@@ -3291,16 +3316,21 @@ static TypeTableEntry *ir_analyze_instruction_const(IrAnalyze *ira, IrInstructio
32913316}
32923317
32933318static TypeTableEntry *ir_analyze_bin_op_bool(IrAnalyze *ira, IrInstructionBinOp *bin_op_instruction) {
3294 IrInstruction *op1 = bin_op_instruction->op1;
3295 IrInstruction *op2 = bin_op_instruction->op2;
3319 IrInstruction *op1 = bin_op_instruction->op1->other;
3320 if (op1->type_entry->id == TypeTableEntryIdInvalid)
3321 return ira->codegen->builtin_types.entry_invalid;
3322
3323 IrInstruction *op2 = bin_op_instruction->op2->other;
3324 if (op2->type_entry->id == TypeTableEntryIdInvalid)
3325 return ira->codegen->builtin_types.entry_invalid;
32963326
32973327 TypeTableEntry *bool_type = ira->codegen->builtin_types.entry_bool;
32983328
3299 IrInstruction *casted_op1 = ir_get_casted_value(ira, op1->other, bool_type);
3329 IrInstruction *casted_op1 = ir_get_casted_value(ira, op1, bool_type);
33003330 if (casted_op1 == ira->codegen->invalid_instruction)
33013331 return ira->codegen->builtin_types.entry_invalid;
33023332
3303 IrInstruction *casted_op2 = ir_get_casted_value(ira, op2->other, bool_type);
3333 IrInstruction *casted_op2 = ir_get_casted_value(ira, op2, bool_type);
33043334 if (casted_op2 == ira->codegen->invalid_instruction)
33053335 return ira->codegen->builtin_types.entry_invalid;
33063336
......@@ -3310,8 +3340,8 @@ static TypeTableEntry *ir_analyze_bin_op_bool(IrAnalyze *ira, IrInstructionBinOp
33103340 bool depends_on_compile_var = op1_val->depends_on_compile_var || op2_val->depends_on_compile_var;
33113341 ConstExprValue *out_val = ir_build_const_from(ira, &bin_op_instruction->base, depends_on_compile_var);
33123342
3313 assert(op1->type_entry->id == TypeTableEntryIdBool);
3314 assert(op2->type_entry->id == TypeTableEntryIdBool);
3343 assert(casted_op1->type_entry->id == TypeTableEntryIdBool);
3344 assert(casted_op2->type_entry->id == TypeTableEntryIdBool);
33153345 if (bin_op_instruction->op_id == IrBinOpBoolOr) {
33163346 out_val->data.x_bool = op1_val->data.x_bool || op2_val->data.x_bool;
33173347 } else if (bin_op_instruction->op_id == IrBinOpBoolAnd) {
......@@ -3322,7 +3352,7 @@ static TypeTableEntry *ir_analyze_bin_op_bool(IrAnalyze *ira, IrInstructionBinOp
33223352 return bool_type;
33233353 }
33243354
3325 ir_build_bin_op_from(&ira->new_irb, &bin_op_instruction->base, bin_op_instruction->op_id, op1->other, op2->other);
3355 ir_build_bin_op_from(&ira->new_irb, &bin_op_instruction->base, bin_op_instruction->op_id, casted_op1, casted_op2);
33263356 return bool_type;
33273357}
33283358
......@@ -5238,11 +5268,8 @@ static TypeTableEntry *ir_analyze_instruction_switch_br(IrAnalyze *ira,
52385268 if (casted_new_value->type_entry->id == TypeTableEntryIdInvalid)
52395269 continue;
52405270
5241 if (casted_new_value->static_value.special != ConstValSpecialStatic) {
5242 add_node_error(ira->codegen, casted_new_value->source_node,
5243 buf_sprintf("unable to evaluate constant expression"));
5271 if (!ir_resolve_const(ira, casted_new_value))
52445272 continue;
5245 }
52465273
52475274 new_case->value = casted_new_value;
52485275 }
......@@ -5340,6 +5367,22 @@ static TypeTableEntry *ir_analyze_instruction_enum_tag(IrAnalyze *ira,
53405367 zig_panic("TODO ir_analyze_instruction_enum_tag");
53415368}
53425369
5370static TypeTableEntry *ir_analyze_instruction_static_eval(IrAnalyze *ira,
5371 IrInstructionStaticEval *static_eval_instruction)
5372{
5373 IrInstruction *value = static_eval_instruction->value->other;
5374 if (value->type_entry->id == TypeTableEntryIdInvalid)
5375 return ira->codegen->builtin_types.entry_invalid;
5376
5377 ConstExprValue *val = ir_resolve_const(ira, value);
5378 if (!val)
5379 return ira->codegen->builtin_types.entry_invalid;
5380
5381 ConstExprValue *out_val = ir_build_const_from(ira, &static_eval_instruction->base, val->depends_on_compile_var);
5382 *out_val = *val;
5383 return value->type_entry;
5384}
5385
53435386static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstruction *instruction) {
53445387 switch (instruction->id) {
53455388 case IrInstructionIdInvalid:
......@@ -5414,6 +5457,8 @@ static TypeTableEntry *ir_analyze_instruction_nocast(IrAnalyze *ira, IrInstructi
54145457 return ir_analyze_instruction_switch_var(ira, (IrInstructionSwitchVar *)instruction);
54155458 case IrInstructionIdEnumTag:
54165459 return ir_analyze_instruction_enum_tag(ira, (IrInstructionEnumTag *)instruction);
5460 case IrInstructionIdStaticEval:
5461 return ir_analyze_instruction_static_eval(ira, (IrInstructionStaticEval *)instruction);
54175462 case IrInstructionIdCast:
54185463 case IrInstructionIdContainerInitList:
54195464 case IrInstructionIdContainerInitFields:
......@@ -5533,6 +5578,7 @@ bool ir_has_side_effects(IrInstruction *instruction) {
55335578 case IrInstructionIdSwitchVar:
55345579 case IrInstructionIdSwitchTarget:
55355580 case IrInstructionIdEnumTag:
5581 case IrInstructionIdStaticEval:
55365582 return false;
55375583 case IrInstructionIdAsm:
55385584 {
......@@ -6243,26 +6289,6 @@ IrInstruction *ir_exec_const_result(IrExecutable *exec) {
62436289// case BuiltinFnIdCUndef:
62446290// zig_panic("TODO");
62456291//
6246// case BuiltinFnIdConstEval:
6247// {
6248// AstNode **expr_node = node->data.fn_call_expr.params.at(0)->parent_field;
6249// TypeTableEntry *resolved_type = analyze_expression(g, import, context, expected_type, *expr_node);
6250// if (resolved_type->id == TypeTableEntryIdInvalid) {
6251// return resolved_type;
6252// }
6253//
6254// ConstExprValue *const_expr_val = &get_resolved_expr(*expr_node)->const_val;
6255//
6256// if (!const_expr_val->ok) {
6257// add_node_error(g, *expr_node, buf_sprintf("unable to evaluate constant expression"));
6258// return g->builtin_types.entry_invalid;
6259// }
6260//
6261// ConstExprValue *const_val = &get_resolved_expr(node)->const_val;
6262// *const_val = *const_expr_val;
6263//
6264// return resolved_type;
6265// }
62666292// case BuiltinFnIdImport:
62676293// return analyze_import(g, import, context, node);
62686294// case BuiltinFnIdCImport:
......@@ -8553,7 +8579,6 @@ static void analyze_goto_pass2(CodeGen *g, ImportTableEntry *import, AstNode *no
85538579// case BuiltinFnIdMinValue:
85548580// case BuiltinFnIdMaxValue:
85558581// case BuiltinFnIdMemberCount:
8556// case BuiltinFnIdConstEval:
85578582// case BuiltinFnIdEmbedFile:
85588583// // caught by constant expression eval codegen
85598584// zig_unreachable();
src/ir_print.cpp+9
......@@ -569,6 +569,12 @@ static void ir_print_enum_tag(IrPrint *irp, IrInstructionEnumTag *instruction) {
569569 ir_print_other_instruction(irp, instruction->value);
570570}
571571
572static void ir_print_static_eval(IrPrint *irp, IrInstructionStaticEval *instruction) {
573 fprintf(irp->f, "@staticEval(");
574 ir_print_other_instruction(irp, instruction->value);
575 fprintf(irp->f, ")");
576}
577
572578static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
573579 ir_print_prefix(irp, instruction);
574580 switch (instruction->id) {
......@@ -691,6 +697,9 @@ static void ir_print_instruction(IrPrint *irp, IrInstruction *instruction) {
691697 case IrInstructionIdEnumTag:
692698 ir_print_enum_tag(irp, (IrInstructionEnumTag *)instruction);
693699 break;
700 case IrInstructionIdStaticEval:
701 ir_print_static_eval(irp, (IrInstructionStaticEval *)instruction);
702 break;
694703 }
695704 fprintf(irp->f, "\n");
696705}