authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-22 12:54:00-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2017-04-22 12:54:00-04:00
logad9040443cc35b7c250015a272a121a93244db31
treed5fab07bcb2c186ff8f207f51b700261fa4ab9d0
parentaafb0b90822e55135e1c50962768e54e6c62b164

new compile errors for setGlobalAlign and setGlobalSection builtins

if you try to use them on an external variable or function then you get a compile error, since the alignment/section is set externally in this case. closes #244

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

src/ir.cpp+36
......@@ -9887,6 +9887,10 @@ static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira,
98879887 Tld *tld = instruction->tld;
98889888 IrInstruction *align_value = instruction->value->other;
98899889
9890 resolve_top_level_decl(ira->codegen, tld, true);
9891 if (tld->resolution == TldResolutionInvalid)
9892 return ira->codegen->builtin_types.entry_invalid;
9893
98909894 uint64_t scalar_align;
98919895 if (!ir_resolve_usize(ira, align_value, &scalar_align))
98929896 return ira->codegen->builtin_types.entry_invalid;
......@@ -9902,11 +9906,25 @@ static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira,
99029906 TldVar *tld_var = (TldVar *)tld;
99039907 set_global_align_node = &tld_var->set_global_align_node;
99049908 alignment_ptr = &tld_var->alignment;
9909
9910 if (tld_var->var->linkage == VarLinkageExternal) {
9911 ErrorMsg *msg = ir_add_error(ira, &instruction->base,
9912 buf_sprintf("cannot set alignment of external variable '%s'", buf_ptr(&tld_var->var->name)));
9913 add_error_note(ira->codegen, msg, tld->source_node, buf_sprintf("declared here"));
9914 return ira->codegen->builtin_types.entry_invalid;
9915 }
99059916 } else if (tld->id == TldIdFn) {
99069917 TldFn *tld_fn = (TldFn *)tld;
99079918 FnTableEntry *fn_entry = tld_fn->fn_entry;
99089919 set_global_align_node = &fn_entry->set_global_align_node;
99099920 alignment_ptr = &fn_entry->alignment;
9921
9922 if (fn_entry->def_scope == nullptr) {
9923 ErrorMsg *msg = ir_add_error(ira, &instruction->base,
9924 buf_sprintf("cannot set alignment of external function '%s'", buf_ptr(&fn_entry->symbol_name)));
9925 add_error_note(ira->codegen, msg, tld->source_node, buf_sprintf("declared here"));
9926 return ira->codegen->builtin_types.entry_invalid;
9927 }
99109928 } else {
99119929 // error is caught in pass1 IR gen
99129930 zig_unreachable();
......@@ -9932,6 +9950,10 @@ static TypeTableEntry *ir_analyze_instruction_set_global_section(IrAnalyze *ira,
99329950 Tld *tld = instruction->tld;
99339951 IrInstruction *section_value = instruction->value->other;
99349952
9953 resolve_top_level_decl(ira->codegen, tld, true);
9954 if (tld->resolution == TldResolutionInvalid)
9955 return ira->codegen->builtin_types.entry_invalid;
9956
99359957 Buf *section_name = ir_resolve_str(ira, section_value);
99369958 if (!section_name)
99379959 return ira->codegen->builtin_types.entry_invalid;
......@@ -9942,11 +9964,25 @@ static TypeTableEntry *ir_analyze_instruction_set_global_section(IrAnalyze *ira,
99429964 TldVar *tld_var = (TldVar *)tld;
99439965 set_global_section_node = &tld_var->set_global_section_node;
99449966 section_name_ptr = &tld_var->section_name;
9967
9968 if (tld_var->var->linkage == VarLinkageExternal) {
9969 ErrorMsg *msg = ir_add_error(ira, &instruction->base,
9970 buf_sprintf("cannot set section of external variable '%s'", buf_ptr(&tld_var->var->name)));
9971 add_error_note(ira->codegen, msg, tld->source_node, buf_sprintf("declared here"));
9972 return ira->codegen->builtin_types.entry_invalid;
9973 }
99459974 } else if (tld->id == TldIdFn) {
99469975 TldFn *tld_fn = (TldFn *)tld;
99479976 FnTableEntry *fn_entry = tld_fn->fn_entry;
99489977 set_global_section_node = &fn_entry->set_global_section_node;
99499978 section_name_ptr = &fn_entry->section_name;
9979
9980 if (fn_entry->def_scope == nullptr) {
9981 ErrorMsg *msg = ir_add_error(ira, &instruction->base,
9982 buf_sprintf("cannot set section of external function '%s'", buf_ptr(&fn_entry->symbol_name)));
9983 add_error_note(ira->codegen, msg, tld->source_node, buf_sprintf("declared here"));
9984 return ira->codegen->builtin_types.entry_invalid;
9985 }
99509986 } else {
99519987 // error is caught in pass1 IR gen
99529988 zig_unreachable();
test/compile_errors.zig+40-4
......@@ -1267,12 +1267,12 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
12671267 , ".tmp_source.zig:2:24: error: integer value 753664 cannot be implicitly casted to type 'u16'");
12681268
12691269 cases.add("set global variable alignment to non power of 2",
1270 \\const some_data: [100]u8 = {
1270 \\const some_data: [100]u8 = undefined;
1271 \\comptime {
12711272 \\ @setGlobalAlign(some_data, 3);
1272 \\ undefined
1273 \\};
1273 \\}
12741274 \\export fn entry() -> usize { @sizeOf(@typeOf(some_data)) }
1275 , ".tmp_source.zig:2:32: error: alignment value must be power of 2");
1275 , ".tmp_source.zig:3:32: error: alignment value must be power of 2");
12761276
12771277 cases.add("compile log",
12781278 \\export fn foo() {
......@@ -1536,4 +1536,40 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
15361536 ,
15371537 "error: 'main' is private",
15381538 ".tmp_source.zig:1:1: note: declared here");
1539
1540 cases.add("@setGlobalAlign extern variable",
1541 \\extern var foo: i32;
1542 \\comptime {
1543 \\ @setGlobalAlign(foo, 4);
1544 \\}
1545 ,
1546 ".tmp_source.zig:3:5: error: cannot set alignment of external variable 'foo'",
1547 ".tmp_source.zig:1:8: note: declared here");
1548
1549 cases.add("@setGlobalAlign extern fn",
1550 \\extern fn foo();
1551 \\comptime {
1552 \\ @setGlobalAlign(foo, 4);
1553 \\}
1554 ,
1555 ".tmp_source.zig:3:5: error: cannot set alignment of external function 'foo'",
1556 ".tmp_source.zig:1:8: note: declared here");
1557
1558 cases.add("@setGlobalSection extern variable",
1559 \\extern var foo: i32;
1560 \\comptime {
1561 \\ @setGlobalSection(foo, ".text2");
1562 \\}
1563 ,
1564 ".tmp_source.zig:3:5: error: cannot set section of external variable 'foo'",
1565 ".tmp_source.zig:1:8: note: declared here");
1566
1567 cases.add("@setGlobalSection extern fn",
1568 \\extern fn foo();
1569 \\comptime {
1570 \\ @setGlobalSection(foo, ".text2");
1571 \\}
1572 ,
1573 ".tmp_source.zig:3:5: error: cannot set section of external function 'foo'",
1574 ".tmp_source.zig:1:8: note: declared here");
15391575}