| author | |
| committer | |
| log | 0919ea0afd11a5f88146f84e8120cdd03b128b81 |
| tree | ae7ea0e9768b148e369c825b6a2302b673d997aa |
| parent | 67b02326f88805df8bbda5e93d1cf46e40c48862 |
..section in the initialization expression7 files changed, 38 insertions(+), 34 deletions(-)
src/all_types.hpp+7-7| ... | @@ -222,6 +222,10 @@ struct TldVar { | ... | @@ -222,6 +222,10 @@ struct TldVar { |
| 222 | Tld base; | 222 | Tld base; |
| 223 | 223 | ||
| 224 | VariableTableEntry *var; | 224 | VariableTableEntry *var; |
| 225 | AstNode *set_global_align_node; | ||
| 226 | uint64_t alignment; | ||
| 227 | AstNode *set_global_section_node; | ||
| 228 | Buf *section_name; | ||
| 225 | }; | 229 | }; |
| 226 | 230 | ||
| 227 | struct TldFn { | 231 | struct TldFn { |
| ... | @@ -1235,7 +1239,7 @@ struct CodeGen { | ... | @@ -1235,7 +1239,7 @@ struct CodeGen { |
| 1235 | // The function prototypes this module includes. In the case of external declarations, | 1239 | // The function prototypes this module includes. In the case of external declarations, |
| 1236 | // there will not be a corresponding fn_defs entry. | 1240 | // there will not be a corresponding fn_defs entry. |
| 1237 | ZigList<FnTableEntry *> fn_protos; | 1241 | ZigList<FnTableEntry *> fn_protos; |
| 1238 | ZigList<VariableTableEntry *> global_vars; | 1242 | ZigList<TldVar *> global_vars; |
| 1239 | 1243 | ||
| 1240 | OutType out_type; | 1244 | OutType out_type; |
| 1241 | FnTableEntry *cur_fn; | 1245 | FnTableEntry *cur_fn; |
| ... | @@ -1307,10 +1311,6 @@ struct VariableTableEntry { | ... | @@ -1307,10 +1311,6 @@ struct VariableTableEntry { |
| 1307 | size_t mem_slot_index; | 1311 | size_t mem_slot_index; |
| 1308 | size_t ref_count; | 1312 | size_t ref_count; |
| 1309 | VarLinkage linkage; | 1313 | VarLinkage linkage; |
| 1310 | AstNode *set_global_align_node; | ||
| 1311 | uint64_t alignment; | ||
| 1312 | AstNode *set_global_section_node; | ||
| 1313 | Buf *section_name; | ||
| 1314 | }; | 1314 | }; |
| 1315 | 1315 | ||
| 1316 | struct ErrorTableEntry { | 1316 | struct ErrorTableEntry { |
| ... | @@ -2256,14 +2256,14 @@ struct IrInstructionCanImplicitCast { | ... | @@ -2256,14 +2256,14 @@ struct IrInstructionCanImplicitCast { |
| 2256 | struct IrInstructionSetGlobalAlign { | 2256 | struct IrInstructionSetGlobalAlign { |
| 2257 | IrInstruction base; | 2257 | IrInstruction base; |
| 2258 | 2258 | ||
| 2259 | VariableTableEntry *var; | 2259 | TldVar *tld_var; |
| 2260 | IrInstruction *value; | 2260 | IrInstruction *value; |
| 2261 | }; | 2261 | }; |
| 2262 | 2262 | ||
| 2263 | struct IrInstructionSetGlobalSection { | 2263 | struct IrInstructionSetGlobalSection { |
| 2264 | IrInstruction base; | 2264 | IrInstruction base; |
| 2265 | 2265 | ||
| 2266 | VariableTableEntry *var; | 2266 | TldVar *tld_var; |
| 2267 | IrInstruction *value; | 2267 | IrInstruction *value; |
| 2268 | }; | 2268 | }; |
| 2269 | 2269 |
src/analyze.cpp+1-1| ... | @@ -1967,7 +1967,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) { | ... | @@ -1967,7 +1967,7 @@ static void resolve_decl_var(CodeGen *g, TldVar *tld_var) { |
| 1967 | tld_var->var = add_variable(g, source_node, tld_var->base.parent_scope, var_decl->symbol, is_const, init_val); | 1967 | tld_var->var = add_variable(g, source_node, tld_var->base.parent_scope, var_decl->symbol, is_const, init_val); |
| 1968 | tld_var->var->linkage = linkage; | 1968 | tld_var->var->linkage = linkage; |
| 1969 | 1969 | ||
| 1970 | g->global_vars.append(tld_var->var); | 1970 | g->global_vars.append(tld_var); |
| 1971 | } | 1971 | } |
| 1972 | 1972 | ||
| 1973 | static void resolve_decl_typedef(CodeGen *g, TldTypeDef *tld_typedef) { | 1973 | static void resolve_decl_typedef(CodeGen *g, TldTypeDef *tld_typedef) { |
src/ast_render.cpp+3-1| ... | @@ -628,6 +628,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) { | ... | @@ -628,6 +628,7 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) { |
| 628 | if (entry->type == NodeTypeStructValueField) { | 628 | if (entry->type == NodeTypeStructValueField) { |
| 629 | Buf *name = entry->data.struct_val_field.name; | 629 | Buf *name = entry->data.struct_val_field.name; |
| 630 | AstNode *expr = entry->data.struct_val_field.expr; | 630 | AstNode *expr = entry->data.struct_val_field.expr; |
| 631 | print_indent(ar); | ||
| 631 | fprintf(ar->f, ".%s = ", buf_ptr(name)); | 632 | fprintf(ar->f, ".%s = ", buf_ptr(name)); |
| 632 | render_node_grouped(ar, expr); | 633 | render_node_grouped(ar, expr); |
| 633 | fprintf(ar->f, ",\n"); | 634 | fprintf(ar->f, ",\n"); |
| ... | @@ -637,10 +638,11 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) { | ... | @@ -637,10 +638,11 @@ static void render_node_extra(AstRender *ar, AstNode *node, bool grouped) { |
| 637 | render_node_grouped(ar, entry); | 638 | render_node_grouped(ar, entry); |
| 638 | } | 639 | } |
| 639 | } | 640 | } |
| 640 | fprintf(ar->f, "}"); | ||
| 641 | if (node->data.container_init_expr.kind == ContainerInitKindStruct) { | 641 | if (node->data.container_init_expr.kind == ContainerInitKindStruct) { |
| 642 | ar->indent -= ar->indent_size; | 642 | ar->indent -= ar->indent_size; |
| 643 | } | 643 | } |
| 644 | print_indent(ar); | ||
| 645 | fprintf(ar->f, "}"); | ||
| 644 | break; | 646 | break; |
| 645 | case NodeTypeArrayType: | 647 | case NodeTypeArrayType: |
| 646 | { | 648 | { |
src/codegen.cpp+6-5| ... | @@ -2808,7 +2808,8 @@ static void do_code_gen(CodeGen *g) { | ... | @@ -2808,7 +2808,8 @@ static void do_code_gen(CodeGen *g) { |
| 2808 | 2808 | ||
| 2809 | // Generate module level variables | 2809 | // Generate module level variables |
| 2810 | for (size_t i = 0; i < g->global_vars.length; i += 1) { | 2810 | for (size_t i = 0; i < g->global_vars.length; i += 1) { |
| 2811 | VariableTableEntry *var = g->global_vars.at(i); | 2811 | TldVar *tld_var = g->global_vars.at(i); |
| 2812 | VariableTableEntry *var = tld_var->var; | ||
| 2812 | 2813 | ||
| 2813 | if (var->value.type->id == TypeTableEntryIdNumLitFloat) { | 2814 | if (var->value.type->id == TypeTableEntryIdNumLitFloat) { |
| 2814 | // Generate debug info for it but that's it. | 2815 | // Generate debug info for it but that's it. |
| ... | @@ -2852,11 +2853,11 @@ static void do_code_gen(CodeGen *g) { | ... | @@ -2852,11 +2853,11 @@ static void do_code_gen(CodeGen *g) { |
| 2852 | if (var->linkage == VarLinkageExport) { | 2853 | if (var->linkage == VarLinkageExport) { |
| 2853 | LLVMSetLinkage(global_value, LLVMExternalLinkage); | 2854 | LLVMSetLinkage(global_value, LLVMExternalLinkage); |
| 2854 | } | 2855 | } |
| 2855 | if (var->section_name) { | 2856 | if (tld_var->section_name) { |
| 2856 | LLVMSetSection(global_value, buf_ptr(var->section_name)); | 2857 | LLVMSetSection(global_value, buf_ptr(tld_var->section_name)); |
| 2857 | } | 2858 | } |
| 2858 | if (var->alignment) { | 2859 | if (tld_var->alignment) { |
| 2859 | LLVMSetAlignment(global_value, var->alignment); | 2860 | LLVMSetAlignment(global_value, tld_var->alignment); |
| 2860 | } | 2861 | } |
| 2861 | 2862 | ||
| 2862 | // TODO debug info for function pointers | 2863 | // TODO debug info for function pointers |
src/ir.cpp+18-17| ... | @@ -2062,11 +2062,11 @@ static IrInstruction *ir_build_can_implicit_cast(IrBuilder *irb, Scope *scope, A | ... | @@ -2062,11 +2062,11 @@ static IrInstruction *ir_build_can_implicit_cast(IrBuilder *irb, Scope *scope, A |
| 2062 | } | 2062 | } |
| 2063 | 2063 | ||
| 2064 | static IrInstruction *ir_build_set_global_align(IrBuilder *irb, Scope *scope, AstNode *source_node, | 2064 | static IrInstruction *ir_build_set_global_align(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 2065 | VariableTableEntry *var, IrInstruction *value) | 2065 | TldVar *tld_var, IrInstruction *value) |
| 2066 | { | 2066 | { |
| 2067 | IrInstructionSetGlobalAlign *instruction = ir_build_instruction<IrInstructionSetGlobalAlign>( | 2067 | IrInstructionSetGlobalAlign *instruction = ir_build_instruction<IrInstructionSetGlobalAlign>( |
| 2068 | irb, scope, source_node); | 2068 | irb, scope, source_node); |
| 2069 | instruction->var = var; | 2069 | instruction->tld_var = tld_var; |
| 2070 | instruction->value = value; | 2070 | instruction->value = value; |
| 2071 | 2071 | ||
| 2072 | ir_ref_instruction(value, irb->current_basic_block); | 2072 | ir_ref_instruction(value, irb->current_basic_block); |
| ... | @@ -2075,11 +2075,11 @@ static IrInstruction *ir_build_set_global_align(IrBuilder *irb, Scope *scope, As | ... | @@ -2075,11 +2075,11 @@ static IrInstruction *ir_build_set_global_align(IrBuilder *irb, Scope *scope, As |
| 2075 | } | 2075 | } |
| 2076 | 2076 | ||
| 2077 | static IrInstruction *ir_build_set_global_section(IrBuilder *irb, Scope *scope, AstNode *source_node, | 2077 | static IrInstruction *ir_build_set_global_section(IrBuilder *irb, Scope *scope, AstNode *source_node, |
| 2078 | VariableTableEntry *var, IrInstruction *value) | 2078 | TldVar *tld_var, IrInstruction *value) |
| 2079 | { | 2079 | { |
| 2080 | IrInstructionSetGlobalSection *instruction = ir_build_instruction<IrInstructionSetGlobalSection>( | 2080 | IrInstructionSetGlobalSection *instruction = ir_build_instruction<IrInstructionSetGlobalSection>( |
| 2081 | irb, scope, source_node); | 2081 | irb, scope, source_node); |
| 2082 | instruction->var = var; | 2082 | instruction->tld_var = tld_var; |
| 2083 | instruction->value = value; | 2083 | instruction->value = value; |
| 2084 | 2084 | ||
| 2085 | ir_ref_instruction(value, irb->current_basic_block); | 2085 | ir_ref_instruction(value, irb->current_basic_block); |
| ... | @@ -4187,7 +4187,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo | ... | @@ -4187,7 +4187,6 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo |
| 4187 | return irb->codegen->invalid_instruction; | 4187 | return irb->codegen->invalid_instruction; |
| 4188 | } | 4188 | } |
| 4189 | TldVar *tld_var = (TldVar *)tld; | 4189 | TldVar *tld_var = (TldVar *)tld; |
| 4190 | VariableTableEntry *var = tld_var->var; | ||
| 4191 | 4190 | ||
| 4192 | AstNode *arg1_node = node->data.fn_call_expr.params.at(1); | 4191 | AstNode *arg1_node = node->data.fn_call_expr.params.at(1); |
| 4193 | IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope); | 4192 | IrInstruction *arg1_value = ir_gen_node(irb, arg1_node, scope); |
| ... | @@ -4195,9 +4194,9 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo | ... | @@ -4195,9 +4194,9 @@ static IrInstruction *ir_gen_builtin_fn_call(IrBuilder *irb, Scope *scope, AstNo |
| 4195 | return arg1_value; | 4194 | return arg1_value; |
| 4196 | 4195 | ||
| 4197 | if (builtin_fn->id == BuiltinFnIdSetGlobalAlign) { | 4196 | if (builtin_fn->id == BuiltinFnIdSetGlobalAlign) { |
| 4198 | return ir_build_set_global_align(irb, scope, node, var, arg1_value); | 4197 | return ir_build_set_global_align(irb, scope, node, tld_var, arg1_value); |
| 4199 | } else { | 4198 | } else { |
| 4200 | return ir_build_set_global_section(irb, scope, node, var, arg1_value); | 4199 | return ir_build_set_global_section(irb, scope, node, tld_var, arg1_value); |
| 4201 | } | 4200 | } |
| 4202 | } | 4201 | } |
| 4203 | case BuiltinFnIdVolatileStore: | 4202 | case BuiltinFnIdVolatileStore: |
| ... | @@ -9455,22 +9454,24 @@ static TypeTableEntry *ir_analyze_instruction_set_fn_visible(IrAnalyze *ira, | ... | @@ -9455,22 +9454,24 @@ static TypeTableEntry *ir_analyze_instruction_set_fn_visible(IrAnalyze *ira, |
| 9455 | static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira, | 9454 | static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira, |
| 9456 | IrInstructionSetGlobalAlign *instruction) | 9455 | IrInstructionSetGlobalAlign *instruction) |
| 9457 | { | 9456 | { |
| 9458 | VariableTableEntry *var = instruction->var; | 9457 | TldVar *tld_var = instruction->tld_var; |
| 9459 | IrInstruction *align_value = instruction->value->other; | 9458 | IrInstruction *align_value = instruction->value->other; |
| 9460 | 9459 | ||
| 9461 | uint64_t scalar_align; | 9460 | uint64_t scalar_align; |
| 9462 | if (!ir_resolve_usize(ira, align_value, &scalar_align)) | 9461 | if (!ir_resolve_usize(ira, align_value, &scalar_align)) |
| 9463 | return ira->codegen->builtin_types.entry_invalid; | 9462 | return ira->codegen->builtin_types.entry_invalid; |
| 9464 | 9463 | ||
| 9464 | // TODO error if not power of 2 | ||
| 9465 | |||
| 9465 | AstNode *source_node = instruction->base.source_node; | 9466 | AstNode *source_node = instruction->base.source_node; |
| 9466 | if (var->set_global_align_node) { | 9467 | if (tld_var->set_global_align_node) { |
| 9467 | ErrorMsg *msg = ir_add_error_node(ira, source_node, | 9468 | ErrorMsg *msg = ir_add_error_node(ira, source_node, |
| 9468 | buf_sprintf("alignment set twice")); | 9469 | buf_sprintf("alignment set twice")); |
| 9469 | add_error_note(ira->codegen, msg, var->set_global_align_node, buf_sprintf("first set here")); | 9470 | add_error_note(ira->codegen, msg, tld_var->set_global_align_node, buf_sprintf("first set here")); |
| 9470 | return ira->codegen->builtin_types.entry_invalid; | 9471 | return ira->codegen->builtin_types.entry_invalid; |
| 9471 | } | 9472 | } |
| 9472 | var->set_global_align_node = source_node; | 9473 | tld_var->set_global_align_node = source_node; |
| 9473 | var->alignment = scalar_align; | 9474 | tld_var->alignment = scalar_align; |
| 9474 | 9475 | ||
| 9475 | ir_build_const_from(ira, &instruction->base, false); | 9476 | ir_build_const_from(ira, &instruction->base, false); |
| 9476 | return ira->codegen->builtin_types.entry_void; | 9477 | return ira->codegen->builtin_types.entry_void; |
| ... | @@ -9479,7 +9480,7 @@ static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira, | ... | @@ -9479,7 +9480,7 @@ static TypeTableEntry *ir_analyze_instruction_set_global_align(IrAnalyze *ira, |
| 9479 | static TypeTableEntry *ir_analyze_instruction_set_global_section(IrAnalyze *ira, | 9480 | static TypeTableEntry *ir_analyze_instruction_set_global_section(IrAnalyze *ira, |
| 9480 | IrInstructionSetGlobalSection *instruction) | 9481 | IrInstructionSetGlobalSection *instruction) |
| 9481 | { | 9482 | { |
| 9482 | VariableTableEntry *var = instruction->var; | 9483 | TldVar *tld_var = instruction->tld_var; |
| 9483 | IrInstruction *section_value = instruction->value->other; | 9484 | IrInstruction *section_value = instruction->value->other; |
| 9484 | 9485 | ||
| 9485 | Buf *section_name = ir_resolve_str(ira, section_value); | 9486 | Buf *section_name = ir_resolve_str(ira, section_value); |
| ... | @@ -9487,13 +9488,13 @@ static TypeTableEntry *ir_analyze_instruction_set_global_section(IrAnalyze *ira, | ... | @@ -9487,13 +9488,13 @@ static TypeTableEntry *ir_analyze_instruction_set_global_section(IrAnalyze *ira, |
| 9487 | return ira->codegen->builtin_types.entry_invalid; | 9488 | return ira->codegen->builtin_types.entry_invalid; |
| 9488 | 9489 | ||
| 9489 | AstNode *source_node = instruction->base.source_node; | 9490 | AstNode *source_node = instruction->base.source_node; |
| 9490 | if (var->set_global_section_node) { | 9491 | if (tld_var->set_global_section_node) { |
| 9491 | ErrorMsg *msg = ir_add_error_node(ira, source_node, buf_sprintf("section set twice")); | 9492 | ErrorMsg *msg = ir_add_error_node(ira, source_node, buf_sprintf("section set twice")); |
| 9492 | add_error_note(ira->codegen, msg, var->set_global_section_node, buf_sprintf("first set here")); | 9493 | add_error_note(ira->codegen, msg, tld_var->set_global_section_node, buf_sprintf("first set here")); |
| 9493 | return ira->codegen->builtin_types.entry_invalid; | 9494 | return ira->codegen->builtin_types.entry_invalid; |
| 9494 | } | 9495 | } |
| 9495 | var->set_global_section_node = source_node; | 9496 | tld_var->set_global_section_node = source_node; |
| 9496 | var->section_name = section_name; | 9497 | tld_var->section_name = section_name; |
| 9497 | 9498 | ||
| 9498 | ir_build_const_from(ira, &instruction->base, false); | 9499 | ir_build_const_from(ira, &instruction->base, false); |
| 9499 | return ira->codegen->builtin_types.entry_void; | 9500 | return ira->codegen->builtin_types.entry_void; |
src/ir_print.cpp+2-2| ... | @@ -835,13 +835,13 @@ static void ir_print_can_implicit_cast(IrPrint *irp, IrInstructionCanImplicitCas | ... | @@ -835,13 +835,13 @@ static void ir_print_can_implicit_cast(IrPrint *irp, IrInstructionCanImplicitCas |
| 835 | } | 835 | } |
| 836 | 836 | ||
| 837 | static void ir_print_set_global_align(IrPrint *irp, IrInstructionSetGlobalAlign *instruction) { | 837 | static void ir_print_set_global_align(IrPrint *irp, IrInstructionSetGlobalAlign *instruction) { |
| 838 | fprintf(irp->f, "@setGlobalAlign(%s,", buf_ptr(&instruction->var->name)); | 838 | fprintf(irp->f, "@setGlobalAlign(%s,", buf_ptr(instruction->tld_var->base.name)); |
| 839 | ir_print_other_instruction(irp, instruction->value); | 839 | ir_print_other_instruction(irp, instruction->value); |
| 840 | fprintf(irp->f, ")"); | 840 | fprintf(irp->f, ")"); |
| 841 | } | 841 | } |
| 842 | 842 | ||
| 843 | static void ir_print_set_global_section(IrPrint *irp, IrInstructionSetGlobalSection *instruction) { | 843 | static void ir_print_set_global_section(IrPrint *irp, IrInstructionSetGlobalSection *instruction) { |
| 844 | fprintf(irp->f, "@setGlobalSection(%s,", buf_ptr(&instruction->var->name)); | 844 | fprintf(irp->f, "@setGlobalSection(%s,", buf_ptr(instruction->tld_var->base.name)); |
| 845 | ir_print_other_instruction(irp, instruction->value); | 845 | ir_print_other_instruction(irp, instruction->value); |
| 846 | fprintf(irp->f, ")"); | 846 | fprintf(irp->f, ")"); |
| 847 | } | 847 | } |
src/parseh.cpp+1-1| ... | @@ -152,7 +152,7 @@ static TldVar *create_global_var(Context *c, Buf *name, ConstExprValue *var_valu | ... | @@ -152,7 +152,7 @@ static TldVar *create_global_var(Context *c, Buf *name, ConstExprValue *var_valu |
| 152 | TldVar *tld_var = allocate<TldVar>(1); | 152 | TldVar *tld_var = allocate<TldVar>(1); |
| 153 | parseh_init_tld(c, &tld_var->base, TldIdVar, name); | 153 | parseh_init_tld(c, &tld_var->base, TldIdVar, name); |
| 154 | tld_var->var = add_variable(c->codegen, c->source_node, &c->import->decls_scope->base, name, is_const, var_value); | 154 | tld_var->var = add_variable(c->codegen, c->source_node, &c->import->decls_scope->base, name, is_const, var_value); |
| 155 | c->codegen->global_vars.append(tld_var->var); | 155 | c->codegen->global_vars.append(tld_var); |
| 156 | return tld_var; | 156 | return tld_var; |
| 157 | } | 157 | } |
| 158 | 158 |