diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2017-02-03 13:56:56 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2017-02-03 13:56:56 -0500 |
| commit | 8c9016b6d1a62da847faf95b6124515cdcd9fe0b (patch) | |
| tree | 315daeb2627a150321bc2af9a1ab9563b041fc81 /src/codegen.cpp | |
| parent | 3be4b6434c0d96f8f6975c13c1a84f06a9857ed8 (diff) | |
| download | zig-8c9016b6d1a62da847faf95b6124515cdcd9fe0b.tar.gz zig-8c9016b6d1a62da847faf95b6124515cdcd9fe0b.zip | |
add setGlobalAlign and setGlobalSection builtin functions
closes #241
Diffstat (limited to 'src/codegen.cpp')
| -rw-r--r-- | src/codegen.cpp | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/src/codegen.cpp b/src/codegen.cpp index dc496b9857..8ebee75ac0 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -231,7 +231,7 @@ void codegen_set_linker_script(CodeGen *g, const char *linker_script) { static void render_const_val(CodeGen *g, ConstExprValue *const_val); -static void render_const_val_global(CodeGen *g, ConstExprValue *const_val, bool is_export); +static void render_const_val_global(CodeGen *g, ConstExprValue *const_val); static LLVMValueRef fn_llvm_value(CodeGen *g, FnTableEntry *fn_table_entry) { if (fn_table_entry->llvm_value) @@ -686,7 +686,7 @@ static LLVMValueRef ir_llvm_value(CodeGen *g, IrInstruction *instruction) { // we might have to do some pointer casting here due to the way union // values are rendered with a type other than the one we expect if (handle_is_ptr(instruction->value.type)) { - render_const_val_global(g, &instruction->value, false); + render_const_val_global(g, &instruction->value); TypeTableEntry *ptr_type = get_pointer_to_type(g, instruction->value.type, true); instruction->llvm_value = LLVMBuildBitCast(g->builder, instruction->value.llvm_global, ptr_type->type_ref, ""); } else if (instruction->value.type->id == TypeTableEntryIdPointer) { @@ -2282,6 +2282,8 @@ static LLVMValueRef ir_render_instruction(CodeGen *g, IrExecutable *executable, case IrInstructionIdTestType: case IrInstructionIdTypeName: case IrInstructionIdCanImplicitCast: + case IrInstructionIdSetGlobalAlign: + case IrInstructionIdSetGlobalSection: zig_unreachable(); case IrInstructionIdReturn: return ir_render_return(g, executable, (IrInstructionReturn *)instruction); @@ -2420,7 +2422,7 @@ static LLVMValueRef gen_const_ptr_array_recursive(CodeGen *g, ConstExprValue *ar base_ptr = gen_const_ptr_array_recursive(g, parent_array, parent_array_index); } else { render_const_val(g, array_const_val); - render_const_val_global(g, array_const_val, false); + render_const_val_global(g, array_const_val); base_ptr = array_const_val->llvm_global; } TypeTableEntry *usize = g->builtin_types.entry_usize; @@ -2568,16 +2570,16 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) { return fn_llvm_value(g, const_val->data.x_fn); case TypeTableEntryIdPointer: { - render_const_val_global(g, const_val, false); + render_const_val_global(g, const_val); size_t index = const_val->data.x_ptr.index; ConstExprValue *base_ptr = const_val->data.x_ptr.base_ptr; if (base_ptr) { if (index == SIZE_MAX) { render_const_val(g, base_ptr); - render_const_val_global(g, base_ptr, false); + render_const_val_global(g, base_ptr); ConstExprValue *other_val = base_ptr; const_val->llvm_value = LLVMConstBitCast(other_val->llvm_global, const_val->type->type_ref); - render_const_val_global(g, const_val, false); + render_const_val_global(g, const_val); return const_val->llvm_value; } else { ConstExprValue *array_const_val = base_ptr; @@ -2587,19 +2589,19 @@ static LLVMValueRef gen_const_val(CodeGen *g, ConstExprValue *const_val) { TypeTableEntry *usize = g->builtin_types.entry_usize; const_val->llvm_value = LLVMConstIntToPtr(LLVMConstNull(usize->type_ref), const_val->type->type_ref); - render_const_val_global(g, const_val, false); + render_const_val_global(g, const_val); return const_val->llvm_value; } LLVMValueRef uncasted_ptr_val = gen_const_ptr_array_recursive(g, array_const_val, index); LLVMValueRef ptr_val = LLVMConstBitCast(uncasted_ptr_val, const_val->type->type_ref); const_val->llvm_value = ptr_val; - render_const_val_global(g, const_val, false); + render_const_val_global(g, const_val); return ptr_val; } } else { TypeTableEntry *usize = g->builtin_types.entry_usize; const_val->llvm_value = LLVMConstIntToPtr(LLVMConstInt(usize->type_ref, index, false), const_val->type->type_ref); - render_const_val_global(g, const_val, false); + render_const_val_global(g, const_val); return const_val->llvm_value; } } @@ -2654,11 +2656,11 @@ static void render_const_val(CodeGen *g, ConstExprValue *const_val) { LLVMSetInitializer(const_val->llvm_global, const_val->llvm_value); } -static void render_const_val_global(CodeGen *g, ConstExprValue *const_val, bool is_export) { +static void render_const_val_global(CodeGen *g, ConstExprValue *const_val) { if (!const_val->llvm_global) { LLVMTypeRef type_ref = const_val->llvm_value ? LLVMTypeOf(const_val->llvm_value) : const_val->type->type_ref; LLVMValueRef global_value = LLVMAddGlobal(g->module, type_ref, ""); - LLVMSetLinkage(global_value, is_export ? LLVMExternalLinkage : LLVMInternalLinkage); + LLVMSetLinkage(global_value, LLVMInternalLinkage); LLVMSetGlobalConstant(global_value, true); LLVMSetUnnamedAddr(global_value, true); @@ -2840,10 +2842,20 @@ static void do_code_gen(CodeGen *g) { LLVMSetLinkage(global_value, LLVMExternalLinkage); } else { - bool is_export = (var->linkage == VarLinkageExport); render_const_val(g, &var->value); - render_const_val_global(g, &var->value, is_export); + render_const_val_global(g, &var->value); global_value = var->value.llvm_global; + + if (var->linkage == VarLinkageExport) { + LLVMSetLinkage(global_value, LLVMExternalLinkage); + } + if (var->section_name) { + LLVMSetSection(global_value, buf_ptr(var->section_name)); + } + if (var->alignment) { + LLVMSetAlignment(global_value, var->alignment); + } + // TODO debug info for function pointers if (var->gen_is_const && var->value.type->id != TypeTableEntryIdFn) { gen_global_var(g, var, var->value.llvm_value, var->value.type); @@ -3672,6 +3684,8 @@ static void define_builtin_fns(CodeGen *g) { create_builtin_fn(g, BuiltinFnIdSetFnVisible, "setFnVisible", 2); create_builtin_fn(g, BuiltinFnIdSetDebugSafety, "setDebugSafety", 2); create_builtin_fn(g, BuiltinFnIdAlloca, "alloca", 2); + create_builtin_fn(g, BuiltinFnIdSetGlobalAlign, "setGlobalAlign", 2); + create_builtin_fn(g, BuiltinFnIdSetGlobalSection, "setGlobalSection", 2); } static void init(CodeGen *g, Buf *source_path) { |
