authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-04 10:38:28-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-02-04 10:38:38-05:00
log0d7abc6368a826490f8985634882300b50d81cba
tree4634f1a74c4cde41e08ca77db9e44061ca9d1729
parent0919ea0afd11a5f88146f84e8120cdd03b128b81

add compile error when setting non power of 2 alignment


2 files changed, 15 insertions(+), 1 deletions(-)

src/ir.cpp+8-1
......@@ -9451,6 +9451,10 @@ static TypeTableEntry *ir_analyze_instruction_set_fn_visible(IrAnalyze *ira,
94519451 return ira->codegen->builtin_types.entry_void;
94529452}
94539453
9454static bool is_power_of_2(uint64_t x) {
9455 return x != 0 && ((x & (~x + 1)) == x);
9456}
9457
94549458static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira,
94559459 IrInstructionSetGlobalAlign *instruction)
94569460{
......@@ -9461,7 +9465,10 @@ static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira,
94619465 if (!ir_resolve_usize(ira, align_value, &scalar_align))
94629466 return ira->codegen->builtin_types.entry_invalid;
94639467
9464 // TODO error if not power of 2
9468 if (!is_power_of_2(scalar_align)) {
9469 ir_add_error(ira, instruction->value, buf_sprintf("alignment value must be power of 2"));
9470 return ira->codegen->builtin_types.entry_invalid;
9471 }
94659472
94669473 AstNode *source_node = instruction->base.source_node;
94679474 if (tld_var->set_global_align_node) {
test/run_tests.cpp+7
......@@ -1662,6 +1662,13 @@ fn foo() {
16621662}
16631663 )SOURCE", 1, ".tmp_source.zig:3:24: error: integer value 753664 cannot be implicitly casted to type 'u16'");
16641664
1665 add_compile_fail_case("set global variable alignment to non power of 2", R"SOURCE(
1666const some_data: [100]u8 = {
1667 @setGlobalAlign(some_data, 3);
1668 undefined
1669};
1670 )SOURCE", 1, ".tmp_source.zig:3:32: error: alignment value must be power of 2");
1671
16651672}
16661673
16671674//////////////////////////////////////////////////////////////////////////////