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,...@@ -9887,6 +9887,10 @@ static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira,
9887 Tld *tld = instruction->tld;9887 Tld *tld = instruction->tld;
9888 IrInstruction *align_value = instruction->value->other;9888 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
9890 uint64_t scalar_align;9894 uint64_t scalar_align;
9891 if (!ir_resolve_usize(ira, align_value, &scalar_align))9895 if (!ir_resolve_usize(ira, align_value, &scalar_align))
9892 return ira->codegen->builtin_types.entry_invalid;9896 return ira->codegen->builtin_types.entry_invalid;
...@@ -9902,11 +9906,25 @@ static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira,...@@ -9902,11 +9906,25 @@ static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira,
9902 TldVar *tld_var = (TldVar *)tld;9906 TldVar *tld_var = (TldVar *)tld;
9903 set_global_align_node = &tld_var->set_global_align_node;9907 set_global_align_node = &tld_var->set_global_align_node;
9904 alignment_ptr = &tld_var->alignment;9908 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 }
9905 } else if (tld->id == TldIdFn) {9916 } else if (tld->id == TldIdFn) {
9906 TldFn *tld_fn = (TldFn *)tld;9917 TldFn *tld_fn = (TldFn *)tld;
9907 FnTableEntry *fn_entry = tld_fn->fn_entry;9918 FnTableEntry *fn_entry = tld_fn->fn_entry;
9908 set_global_align_node = &fn_entry->set_global_align_node;9919 set_global_align_node = &fn_entry->set_global_align_node;
9909 alignment_ptr = &fn_entry->alignment;9920 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 }
9910 } else {9928 } else {
9911 // error is caught in pass1 IR gen9929 // error is caught in pass1 IR gen
9912 zig_unreachable();9930 zig_unreachable();
...@@ -9932,6 +9950,10 @@ static TypeTableEntry *ir_analyze_instruction_set_global_section(IrAnalyze *ira,...@@ -9932,6 +9950,10 @@ static TypeTableEntry *ir_analyze_instruction_set_global_section(IrAnalyze *ira,
9932 Tld *tld = instruction->tld;9950 Tld *tld = instruction->tld;
9933 IrInstruction *section_value = instruction->value->other;9951 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
9935 Buf *section_name = ir_resolve_str(ira, section_value);9957 Buf *section_name = ir_resolve_str(ira, section_value);
9936 if (!section_name)9958 if (!section_name)
9937 return ira->codegen->builtin_types.entry_invalid;9959 return ira->codegen->builtin_types.entry_invalid;
...@@ -9942,11 +9964,25 @@ static TypeTableEntry *ir_analyze_instruction_set_global_section(IrAnalyze *ira,...@@ -9942,11 +9964,25 @@ static TypeTableEntry *ir_analyze_instruction_set_global_section(IrAnalyze *ira,
9942 TldVar *tld_var = (TldVar *)tld;9964 TldVar *tld_var = (TldVar *)tld;
9943 set_global_section_node = &tld_var->set_global_section_node;9965 set_global_section_node = &tld_var->set_global_section_node;
9944 section_name_ptr = &tld_var->section_name;9966 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 }
9945 } else if (tld->id == TldIdFn) {9974 } else if (tld->id == TldIdFn) {
9946 TldFn *tld_fn = (TldFn *)tld;9975 TldFn *tld_fn = (TldFn *)tld;
9947 FnTableEntry *fn_entry = tld_fn->fn_entry;9976 FnTableEntry *fn_entry = tld_fn->fn_entry;
9948 set_global_section_node = &fn_entry->set_global_section_node;9977 set_global_section_node = &fn_entry->set_global_section_node;
9949 section_name_ptr = &fn_entry->section_name;9978 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 }
9950 } else {9986 } else {
9951 // error is caught in pass1 IR gen9987 // error is caught in pass1 IR gen
9952 zig_unreachable();9988 zig_unreachable();
test/compile_errors.zig+40-4
...@@ -1267,12 +1267,12 @@ pub fn addCases(cases: &tests.CompileErrorContext) {...@@ -1267,12 +1267,12 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
1267 , ".tmp_source.zig:2:24: error: integer value 753664 cannot be implicitly casted to type 'u16'");1267 , ".tmp_source.zig:2:24: error: integer value 753664 cannot be implicitly casted to type 'u16'");
12681268
1269 cases.add("set global variable alignment to non power of 2",1269 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 {
1271 \\ @setGlobalAlign(some_data, 3);1272 \\ @setGlobalAlign(some_data, 3);
1272 \\ undefined1273 \\}
1273 \\};
1274 \\export fn entry() -> usize { @sizeOf(@typeOf(some_data)) }1274 \\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
1277 cases.add("compile log",1277 cases.add("compile log",
1278 \\export fn foo() {1278 \\export fn foo() {
...@@ -1536,4 +1536,40 @@ pub fn addCases(cases: &tests.CompileErrorContext) {...@@ -1536,4 +1536,40 @@ pub fn addCases(cases: &tests.CompileErrorContext) {
1536 ,1536 ,
1537 "error: 'main' is private",1537 "error: 'main' is private",
1538 ".tmp_source.zig:1:1: note: declared here");1538 ".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");
1539}1575}