| ... | ... | @@ -1,447 +1,137 @@ |
| 1 | 1 | const std = @import("std"); |
| 2 | | const Compilation = @import("compilation.zig").Compilation; |
| 3 | | const llvm = @import("llvm.zig"); |
| 4 | | const c = @import("c.zig"); |
| 2 | const mem = std.mem; |
| 3 | const assert = std.debug.assert; |
| 5 | 4 | const ir = @import("ir.zig"); |
| 6 | | const Value = @import("value.zig").Value; |
| 7 | 5 | const Type = @import("type.zig").Type; |
| 8 | | const Scope = @import("scope.zig").Scope; |
| 9 | | const util = @import("util.zig"); |
| 10 | | const event = std.event; |
| 11 | | const assert = std.debug.assert; |
| 12 | | const DW = std.dwarf; |
| 13 | | const maxInt = std.math.maxInt; |
| 14 | | |
| 15 | | pub async fn renderToLlvm(comp: *Compilation, fn_val: *Value.Fn, code: *ir.Code) Compilation.BuildError!void { |
| 16 | | fn_val.base.ref(); |
| 17 | | defer fn_val.base.deref(comp); |
| 18 | | defer code.destroy(comp.gpa()); |
| 19 | | |
| 20 | | var output_path = try comp.createRandomOutputPath(comp.target.oFileExt()); |
| 21 | | errdefer output_path.deinit(); |
| 22 | | |
| 23 | | const llvm_handle = try comp.zig_compiler.getAnyLlvmContext(); |
| 24 | | defer llvm_handle.release(comp.zig_compiler); |
| 25 | | |
| 26 | | const context = llvm_handle.node.data; |
| 27 | | |
| 28 | | const module = llvm.ModuleCreateWithNameInContext(comp.name.span(), context) orelse return error.OutOfMemory; |
| 29 | | defer llvm.DisposeModule(module); |
| 30 | | |
| 31 | | llvm.SetTarget(module, comp.llvm_triple.span()); |
| 32 | | llvm.SetDataLayout(module, comp.target_layout_str); |
| 33 | | |
| 34 | | if (comp.target.getObjectFormat() == .coff) { |
| 35 | | llvm.AddModuleCodeViewFlag(module); |
| 36 | | } else { |
| 37 | | llvm.AddModuleDebugInfoFlag(module); |
| 38 | | } |
| 39 | | |
| 40 | | const builder = llvm.CreateBuilderInContext(context) orelse return error.OutOfMemory; |
| 41 | | defer llvm.DisposeBuilder(builder); |
| 42 | | |
| 43 | | const dibuilder = llvm.CreateDIBuilder(module, true) orelse return error.OutOfMemory; |
| 44 | | defer llvm.DisposeDIBuilder(dibuilder); |
| 45 | | |
| 46 | | // Don't use ZIG_VERSION_STRING here. LLVM misparses it when it includes |
| 47 | | // the git revision. |
| 48 | | const producer = try std.fmt.allocPrintZ(&code.arena.allocator, "zig {}.{}.{}", .{ |
| 49 | | @as(u32, c.ZIG_VERSION_MAJOR), |
| 50 | | @as(u32, c.ZIG_VERSION_MINOR), |
| 51 | | @as(u32, c.ZIG_VERSION_PATCH), |
| 52 | | }); |
| 53 | | const flags = ""; |
| 54 | | const runtime_version = 0; |
| 55 | | const compile_unit_file = llvm.CreateFile( |
| 56 | | dibuilder, |
| 57 | | comp.name.span(), |
| 58 | | comp.root_package.root_src_dir.span(), |
| 59 | | ) orelse return error.OutOfMemory; |
| 60 | | const is_optimized = comp.build_mode != .Debug; |
| 61 | | const compile_unit = llvm.CreateCompileUnit( |
| 62 | | dibuilder, |
| 63 | | DW.LANG_C99, |
| 64 | | compile_unit_file, |
| 65 | | producer, |
| 66 | | is_optimized, |
| 67 | | flags, |
| 68 | | runtime_version, |
| 69 | | "", |
| 70 | | 0, |
| 71 | | !comp.strip, |
| 72 | | ) orelse return error.OutOfMemory; |
| 73 | | |
| 74 | | var ofile = ObjectFile{ |
| 75 | | .comp = comp, |
| 76 | | .module = module, |
| 77 | | .builder = builder, |
| 78 | | .dibuilder = dibuilder, |
| 79 | | .context = context, |
| 80 | | .lock = event.Lock.init(), |
| 81 | | .arena = &code.arena.allocator, |
| 82 | | }; |
| 83 | | |
| 84 | | try renderToLlvmModule(&ofile, fn_val, code); |
| 85 | | |
| 86 | | // TODO module level assembly |
| 87 | | //if (buf_len(&g->global_asm) != 0) { |
| 88 | | // LLVMSetModuleInlineAsm(g->module, buf_ptr(&g->global_asm)); |
| 89 | | //} |
| 90 | | |
| 91 | | llvm.DIBuilderFinalize(dibuilder); |
| 92 | | |
| 93 | | if (comp.verbose_llvm_ir) { |
| 94 | | std.debug.warn("raw module:\n", .{}); |
| 95 | | llvm.DumpModule(ofile.module); |
| 96 | | } |
| 6 | const Value = @import("value.zig").Value; |
| 97 | 7 | |
| 98 | | // verify the llvm module when safety is on |
| 99 | | if (std.debug.runtime_safety) { |
| 100 | | var error_ptr: ?[*:0]u8 = null; |
| 101 | | _ = llvm.VerifyModule(ofile.module, llvm.AbortProcessAction, &error_ptr); |
| 102 | | } |
| 8 | pub const ErrorMsg = struct { |
| 9 | byte_offset: usize, |
| 10 | msg: []const u8, |
| 11 | }; |
| 103 | 12 | |
| 104 | | const is_small = comp.build_mode == .ReleaseSmall; |
| 105 | | const is_debug = comp.build_mode == .Debug; |
| 13 | pub const Symbol = struct { |
| 14 | errors: []ErrorMsg, |
| 106 | 15 | |
| 107 | | var err_msg: [*:0]u8 = undefined; |
| 108 | | // TODO integrate this with evented I/O |
| 109 | | if (llvm.TargetMachineEmitToFile( |
| 110 | | comp.target_machine, |
| 111 | | module, |
| 112 | | output_path.span(), |
| 113 | | llvm.EmitBinary, |
| 114 | | &err_msg, |
| 115 | | is_debug, |
| 116 | | is_small, |
| 117 | | )) { |
| 118 | | if (std.debug.runtime_safety) { |
| 119 | | std.debug.panic("unable to write object file {}: {s}\n", .{ output_path.span(), err_msg }); |
| 16 | pub fn deinit(self: *Symbol, allocator: *mem.Allocator) void { |
| 17 | for (self.errors) |err| { |
| 18 | allocator.free(err.msg); |
| 120 | 19 | } |
| 121 | | return error.WritingObjectFileFailed; |
| 122 | | } |
| 123 | | //validate_inline_fns(g); TODO |
| 124 | | fn_val.containing_object = output_path; |
| 125 | | if (comp.verbose_llvm_ir) { |
| 126 | | std.debug.warn("optimized module:\n", .{}); |
| 127 | | llvm.DumpModule(ofile.module); |
| 128 | | } |
| 129 | | if (comp.verbose_link) { |
| 130 | | std.debug.warn("created {}\n", .{output_path.span()}); |
| 131 | | } |
| 132 | | } |
| 133 | | |
| 134 | | pub const ObjectFile = struct { |
| 135 | | comp: *Compilation, |
| 136 | | module: *llvm.Module, |
| 137 | | builder: *llvm.Builder, |
| 138 | | dibuilder: *llvm.DIBuilder, |
| 139 | | context: *llvm.Context, |
| 140 | | lock: event.Lock, |
| 141 | | arena: *std.mem.Allocator, |
| 142 | | |
| 143 | | fn gpa(self: *ObjectFile) *std.mem.Allocator { |
| 144 | | return self.comp.gpa(); |
| 20 | allocator.free(self.errors); |
| 21 | self.* = undefined; |
| 145 | 22 | } |
| 146 | 23 | }; |
| 147 | 24 | |
| 148 | | pub fn renderToLlvmModule(ofile: *ObjectFile, fn_val: *Value.Fn, code: *ir.Code) !void { |
| 149 | | // TODO audit more of codegen.cpp:fn_llvm_value and port more logic |
| 150 | | const llvm_fn_type = try fn_val.base.typ.getLlvmType(ofile.arena, ofile.context); |
| 151 | | const llvm_fn = llvm.AddFunction( |
| 152 | | ofile.module, |
| 153 | | fn_val.symbol_name.span(), |
| 154 | | llvm_fn_type, |
| 155 | | ) orelse return error.OutOfMemory; |
| 156 | | |
| 157 | | const want_fn_safety = fn_val.block_scope.?.safety.get(ofile.comp); |
| 158 | | if (want_fn_safety and ofile.comp.haveLibC()) { |
| 159 | | try addLLVMFnAttr(ofile, llvm_fn, "sspstrong"); |
| 160 | | try addLLVMFnAttrStr(ofile, llvm_fn, "stack-protector-buffer-size", "4"); |
| 25 | pub fn generateSymbol(typed_value: ir.TypedValue, module: ir.Module, code: *std.ArrayList(u8)) !Symbol { |
| 26 | switch (typed_value.ty.zigTypeTag()) { |
| 27 | .Fn => { |
| 28 | const index = typed_value.val.cast(Value.Payload.Function).?.index; |
| 29 | const module_fn = module.fns[index]; |
| 30 | |
| 31 | var function = Function{ |
| 32 | .module = &module, |
| 33 | .mod_fn = &module_fn, |
| 34 | .code = code, |
| 35 | .inst_table = std.AutoHashMap(*ir.Inst, Function.MCValue).init(code.allocator), |
| 36 | .errors = std.ArrayList(ErrorMsg).init(code.allocator), |
| 37 | .constants = std.ArrayList(ir.TypedValue).init(code.allocator), |
| 38 | }; |
| 39 | defer function.inst_table.deinit(); |
| 40 | defer function.errors.deinit(); |
| 41 | |
| 42 | for (module_fn.body) |inst| { |
| 43 | const new_inst = function.genFuncInst(inst) catch |err| switch (err) { |
| 44 | error.CodegenFail => { |
| 45 | assert(function.errors.items.len != 0); |
| 46 | break; |
| 47 | }, |
| 48 | else => |e| return e, |
| 49 | }; |
| 50 | try function.inst_table.putNoClobber(inst, new_inst); |
| 51 | } |
| 52 | return Symbol{ .errors = function.errors.toOwnedSlice() }; |
| 53 | }, |
| 54 | else => @panic("TODO implement generateSymbol for non-function types"), |
| 161 | 55 | } |
| 56 | } |
| 162 | 57 | |
| 163 | | // TODO |
| 164 | | //if (fn_val.align_stack) |align_stack| { |
| 165 | | // try addLLVMFnAttrInt(ofile, llvm_fn, "alignstack", align_stack); |
| 166 | | //} |
| 167 | | |
| 168 | | const fn_type = fn_val.base.typ.cast(Type.Fn).?; |
| 169 | | const fn_type_normal = &fn_type.key.data.Normal; |
| 170 | | |
| 171 | | try addLLVMFnAttr(ofile, llvm_fn, "nounwind"); |
| 172 | | //add_uwtable_attr(g, fn_table_entry->llvm_value); |
| 173 | | try addLLVMFnAttr(ofile, llvm_fn, "nobuiltin"); |
| 174 | | |
| 175 | | //if (g->build_mode == BuildModeDebug && fn_table_entry->fn_inline != FnInlineAlways) { |
| 176 | | // ZigLLVMAddFunctionAttr(fn_table_entry->llvm_value, "no-frame-pointer-elim", "true"); |
| 177 | | // ZigLLVMAddFunctionAttr(fn_table_entry->llvm_value, "no-frame-pointer-elim-non-leaf", nullptr); |
| 178 | | //} |
| 179 | | |
| 180 | | //if (fn_table_entry->section_name) { |
| 181 | | // LLVMSetSection(fn_table_entry->llvm_value, buf_ptr(fn_table_entry->section_name)); |
| 182 | | //} |
| 183 | | //if (fn_table_entry->align_bytes > 0) { |
| 184 | | // LLVMSetAlignment(fn_table_entry->llvm_value, (unsigned)fn_table_entry->align_bytes); |
| 185 | | //} else { |
| 186 | | // // We'd like to set the best alignment for the function here, but on Darwin LLVM gives |
| 187 | | // // "Cannot getTypeInfo() on a type that is unsized!" assertion failure when calling |
| 188 | | // // any of the functions for getting alignment. Not specifying the alignment should |
| 189 | | // // use the ABI alignment, which is fine. |
| 190 | | //} |
| 191 | | |
| 192 | | //if (!type_has_bits(return_type)) { |
| 193 | | // // nothing to do |
| 194 | | //} else if (type_is_codegen_pointer(return_type)) { |
| 195 | | // addLLVMAttr(fn_table_entry->llvm_value, 0, "nonnull"); |
| 196 | | //} else if (handle_is_ptr(return_type) && |
| 197 | | // calling_convention_does_first_arg_return(fn_type->data.fn.fn_type_id.cc)) |
| 198 | | //{ |
| 199 | | // addLLVMArgAttr(fn_table_entry->llvm_value, 0, "sret"); |
| 200 | | // addLLVMArgAttr(fn_table_entry->llvm_value, 0, "nonnull"); |
| 201 | | //} |
| 202 | | |
| 203 | | // TODO set parameter attributes |
| 204 | | |
| 205 | | // TODO |
| 206 | | //uint32_t err_ret_trace_arg_index = get_err_ret_trace_arg_index(g, fn_table_entry); |
| 207 | | //if (err_ret_trace_arg_index != UINT32_MAX) { |
| 208 | | // addLLVMArgAttr(fn_table_entry->llvm_value, (unsigned)err_ret_trace_arg_index, "nonnull"); |
| 209 | | //} |
| 210 | | |
| 211 | | const cur_ret_ptr = if (fn_type_normal.return_type.handleIsPtr()) llvm.GetParam(llvm_fn, 0) else null; |
| 212 | | |
| 213 | | // build all basic blocks |
| 214 | | for (code.basic_block_list.span()) |bb| { |
| 215 | | bb.llvm_block = llvm.AppendBasicBlockInContext( |
| 216 | | ofile.context, |
| 217 | | llvm_fn, |
| 218 | | bb.name_hint, |
| 219 | | ) orelse return error.OutOfMemory; |
| 220 | | } |
| 221 | | const entry_bb = code.basic_block_list.at(0); |
| 222 | | llvm.PositionBuilderAtEnd(ofile.builder, entry_bb.llvm_block); |
| 223 | | |
| 224 | | llvm.ClearCurrentDebugLocation(ofile.builder); |
| 225 | | |
| 226 | | // TODO set up error return tracing |
| 227 | | // TODO allocate temporary stack values |
| 228 | | |
| 229 | | const var_list = fn_type.non_key.Normal.variable_list.span(); |
| 230 | | // create debug variable declarations for variables and allocate all local variables |
| 231 | | for (var_list) |var_scope, i| { |
| 232 | | const var_type = switch (var_scope.data) { |
| 233 | | .Const => unreachable, |
| 234 | | .Param => |param| param.typ, |
| 235 | | }; |
| 236 | | // if (!type_has_bits(var->value->type)) { |
| 237 | | // continue; |
| 238 | | // } |
| 239 | | // if (ir_get_var_is_comptime(var)) |
| 240 | | // continue; |
| 241 | | // if (type_requires_comptime(var->value->type)) |
| 242 | | // continue; |
| 243 | | // if (var->src_arg_index == SIZE_MAX) { |
| 244 | | // var->value_ref = build_alloca(g, var->value->type, buf_ptr(&var->name), var->align_bytes); |
| 245 | | |
| 246 | | // var->di_loc_var = ZigLLVMCreateAutoVariable(g->dbuilder, get_di_scope(g, var->parent_scope), |
| 247 | | // buf_ptr(&var->name), import->di_file, (unsigned)(var->decl_node->line + 1), |
| 248 | | // var->value->type->di_type, !g->strip_debug_symbols, 0); |
| 249 | | |
| 250 | | // } else { |
| 251 | | // it's a parameter |
| 252 | | // assert(var->gen_arg_index != SIZE_MAX); |
| 253 | | // TypeTableEntry *gen_type; |
| 254 | | // FnGenParamInfo *gen_info = &fn_table_entry->type_entry->data.fn.gen_param_info[var->src_arg_index]; |
| 58 | const Function = struct { |
| 59 | module: *const ir.Module, |
| 60 | mod_fn: *const ir.Module.Fn, |
| 61 | code: *std.ArrayList(u8), |
| 62 | inst_table: std.AutoHashMap(*ir.Inst, MCValue), |
| 63 | /// Constants are embedded within functions (at the end, after `ret`) |
| 64 | /// so that they are independently updateable. |
| 65 | /// This is a list of constants that must be appended to the symbol after `ret`. |
| 66 | constants: std.ArrayList(ir.TypedValue), |
| 67 | errors: std.ArrayList(ErrorMsg), |
| 68 | |
| 69 | const MCValue = union(enum) { |
| 70 | none, |
| 71 | unreach, |
| 72 | /// A pointer-sized integer that fits in a register. |
| 73 | immediate: u64, |
| 74 | /// Refers to the index into `constants` field of `Function`. |
| 75 | local_const_ptr: usize, |
| 76 | }; |
| 255 | 77 | |
| 256 | | if (var_type.handleIsPtr()) { |
| 257 | | // if (gen_info->is_byval) { |
| 258 | | // gen_type = var->value->type; |
| 259 | | // } else { |
| 260 | | // gen_type = gen_info->type; |
| 261 | | // } |
| 262 | | var_scope.data.Param.llvm_value = llvm.GetParam(llvm_fn, @intCast(c_uint, i)); |
| 263 | | } else { |
| 264 | | // gen_type = var->value->type; |
| 265 | | var_scope.data.Param.llvm_value = try renderAlloca(ofile, var_type, var_scope.name, .Abi); |
| 78 | fn genFuncInst(self: *Function, inst: *ir.Inst) !MCValue { |
| 79 | switch (inst.tag) { |
| 80 | .unreach => return self.genPanic(inst.src), |
| 81 | .constant => unreachable, // excluded from function bodies |
| 82 | .assembly => return self.genAsm(inst.cast(ir.Inst.Assembly).?), |
| 83 | .ptrtoint => return self.genPtrToInt(inst.cast(ir.Inst.PtrToInt).?), |
| 266 | 84 | } |
| 267 | | // if (var->decl_node) { |
| 268 | | // var->di_loc_var = ZigLLVMCreateParameterVariable(g->dbuilder, get_di_scope(g, var->parent_scope), |
| 269 | | // buf_ptr(&var->name), import->di_file, |
| 270 | | // (unsigned)(var->decl_node->line + 1), |
| 271 | | // gen_type->di_type, !g->strip_debug_symbols, 0, (unsigned)(var->gen_arg_index + 1)); |
| 272 | | // } |
| 273 | | |
| 274 | | // } |
| 275 | 85 | } |
| 276 | 86 | |
| 277 | | // TODO finishing error return trace setup. we have to do this after all the allocas. |
| 278 | | |
| 279 | | // create debug variable declarations for parameters |
| 280 | | // rely on the first variables in the variable_list being parameters. |
| 281 | | //size_t next_var_i = 0; |
| 282 | | for (fn_type.key.data.Normal.params) |param, i| { |
| 283 | | //FnGenParamInfo *info = &fn_table_entry->type_entry->data.fn.gen_param_info[param_i]; |
| 284 | | //if (info->gen_index == SIZE_MAX) |
| 285 | | // continue; |
| 286 | | const scope_var = var_list[i]; |
| 287 | | //assert(variable->src_arg_index != SIZE_MAX); |
| 288 | | //next_var_i += 1; |
| 289 | | //assert(variable); |
| 290 | | //assert(variable->value_ref); |
| 291 | | |
| 292 | | if (!param.typ.handleIsPtr()) { |
| 293 | | //clear_debug_source_node(g); |
| 294 | | const llvm_param = llvm.GetParam(llvm_fn, @intCast(c_uint, i)); |
| 295 | | _ = try renderStoreUntyped( |
| 296 | | ofile, |
| 297 | | llvm_param, |
| 298 | | scope_var.data.Param.llvm_value, |
| 299 | | .Abi, |
| 300 | | .Non, |
| 301 | | ); |
| 87 | fn genPanic(self: *Function, src: usize) !MCValue { |
| 88 | // TODO change this to call the panic function |
| 89 | switch (self.module.target.cpu.arch) { |
| 90 | .i386, .x86_64 => { |
| 91 | try self.code.append(0xcc); // x86 int3 |
| 92 | }, |
| 93 | else => return self.fail(src, "TODO implement panic for {}", .{self.module.target.cpu.arch}), |
| 302 | 94 | } |
| 303 | | |
| 304 | | //if (variable->decl_node) { |
| 305 | | // gen_var_debug_decl(g, variable); |
| 306 | | //} |
| 95 | return .unreach; |
| 307 | 96 | } |
| 308 | 97 | |
| 309 | | for (code.basic_block_list.span()) |current_block| { |
| 310 | | llvm.PositionBuilderAtEnd(ofile.builder, current_block.llvm_block); |
| 311 | | for (current_block.instruction_list.span()) |instruction| { |
| 312 | | if (instruction.ref_count == 0 and !instruction.hasSideEffects()) continue; |
| 313 | | |
| 314 | | instruction.llvm_value = try instruction.render(ofile, fn_val); |
| 315 | | } |
| 316 | | current_block.llvm_exit_block = llvm.GetInsertBlock(ofile.builder); |
| 98 | fn genAsm(self: *Function, inst: *ir.Inst.Assembly) !MCValue { |
| 99 | return self.fail(inst.base.src, "TODO machine code gen assembly", .{}); |
| 317 | 100 | } |
| 318 | | } |
| 319 | | |
| 320 | | fn addLLVMAttr( |
| 321 | | ofile: *ObjectFile, |
| 322 | | val: *llvm.Value, |
| 323 | | attr_index: llvm.AttributeIndex, |
| 324 | | attr_name: []const u8, |
| 325 | | ) !void { |
| 326 | | const kind_id = llvm.GetEnumAttributeKindForName(attr_name.ptr, attr_name.len); |
| 327 | | assert(kind_id != 0); |
| 328 | | const llvm_attr = llvm.CreateEnumAttribute(ofile.context, kind_id, 0) orelse return error.OutOfMemory; |
| 329 | | llvm.AddAttributeAtIndex(val, attr_index, llvm_attr); |
| 330 | | } |
| 331 | | |
| 332 | | fn addLLVMAttrStr( |
| 333 | | ofile: *ObjectFile, |
| 334 | | val: *llvm.Value, |
| 335 | | attr_index: llvm.AttributeIndex, |
| 336 | | attr_name: []const u8, |
| 337 | | attr_val: []const u8, |
| 338 | | ) !void { |
| 339 | | const llvm_attr = llvm.CreateStringAttribute( |
| 340 | | ofile.context, |
| 341 | | attr_name.ptr, |
| 342 | | @intCast(c_uint, attr_name.len), |
| 343 | | attr_val.ptr, |
| 344 | | @intCast(c_uint, attr_val.len), |
| 345 | | ) orelse return error.OutOfMemory; |
| 346 | | llvm.AddAttributeAtIndex(val, attr_index, llvm_attr); |
| 347 | | } |
| 348 | | |
| 349 | | fn addLLVMAttrInt( |
| 350 | | val: *llvm.Value, |
| 351 | | attr_index: llvm.AttributeIndex, |
| 352 | | attr_name: []const u8, |
| 353 | | attr_val: u64, |
| 354 | | ) !void { |
| 355 | | const kind_id = llvm.GetEnumAttributeKindForName(attr_name.ptr, attr_name.len); |
| 356 | | assert(kind_id != 0); |
| 357 | | const llvm_attr = llvm.CreateEnumAttribute(ofile.context, kind_id, attr_val) orelse return error.OutOfMemory; |
| 358 | | llvm.AddAttributeAtIndex(val, attr_index, llvm_attr); |
| 359 | | } |
| 360 | | |
| 361 | | fn addLLVMFnAttr(ofile: *ObjectFile, fn_val: *llvm.Value, attr_name: []const u8) !void { |
| 362 | | return addLLVMAttr(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name); |
| 363 | | } |
| 364 | 101 | |
| 365 | | fn addLLVMFnAttrStr(ofile: *ObjectFile, fn_val: *llvm.Value, attr_name: []const u8, attr_val: []const u8) !void { |
| 366 | | return addLLVMAttrStr(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name, attr_val); |
| 367 | | } |
| 368 | | |
| 369 | | fn addLLVMFnAttrInt(ofile: *ObjectFile, fn_val: *llvm.Value, attr_name: []const u8, attr_val: u64) !void { |
| 370 | | return addLLVMAttrInt(ofile, fn_val, maxInt(llvm.AttributeIndex), attr_name, attr_val); |
| 371 | | } |
| 372 | | |
| 373 | | fn renderLoadUntyped( |
| 374 | | ofile: *ObjectFile, |
| 375 | | ptr: *llvm.Value, |
| 376 | | alignment: Type.Pointer.Align, |
| 377 | | vol: Type.Pointer.Vol, |
| 378 | | name: [*:0]const u8, |
| 379 | | ) !*llvm.Value { |
| 380 | | const result = llvm.BuildLoad(ofile.builder, ptr, name) orelse return error.OutOfMemory; |
| 381 | | switch (vol) { |
| 382 | | .Non => {}, |
| 383 | | .Volatile => llvm.SetVolatile(result, 1), |
| 102 | fn genPtrToInt(self: *Function, inst: *ir.Inst.PtrToInt) !MCValue { |
| 103 | // no-op |
| 104 | return self.resolveInst(inst.args.ptr); |
| 384 | 105 | } |
| 385 | | llvm.SetAlignment(result, resolveAlign(ofile, alignment, llvm.GetElementType(llvm.TypeOf(ptr)))); |
| 386 | | return result; |
| 387 | | } |
| 388 | 106 | |
| 389 | | fn renderLoad(ofile: *ObjectFile, ptr: *llvm.Value, ptr_type: *Type.Pointer, name: [*:0]const u8) !*llvm.Value { |
| 390 | | return renderLoadUntyped(ofile, ptr, ptr_type.key.alignment, ptr_type.key.vol, name); |
| 391 | | } |
| 392 | | |
| 393 | | pub fn getHandleValue(ofile: *ObjectFile, ptr: *llvm.Value, ptr_type: *Type.Pointer) !?*llvm.Value { |
| 394 | | const child_type = ptr_type.key.child_type; |
| 395 | | if (!child_type.hasBits()) { |
| 396 | | return null; |
| 397 | | } |
| 398 | | if (child_type.handleIsPtr()) { |
| 399 | | return ptr; |
| 107 | fn resolveInst(self: *Function, inst: *ir.Inst) !MCValue { |
| 108 | if (inst.cast(ir.Inst.Constant)) |const_inst| { |
| 109 | switch (inst.ty.zigTypeTag()) { |
| 110 | .Int => { |
| 111 | const info = inst.ty.intInfo(self.module.target); |
| 112 | const ptr_bits = self.module.target.cpu.arch.ptrBitWidth(); |
| 113 | if (info.bits > ptr_bits or info.signed) { |
| 114 | return self.fail(inst.src, "TODO const int bigger than ptr and signed int", .{}); |
| 115 | } |
| 116 | return MCValue{ .immediate = const_inst.val.toUnsignedInt() }; |
| 117 | }, |
| 118 | else => return self.fail(inst.src, "TODO implement const of type '{}'", .{inst.ty}), |
| 119 | } |
| 120 | } else { |
| 121 | return self.inst_table.getValue(inst).?; |
| 122 | } |
| 400 | 123 | } |
| 401 | | return try renderLoad(ofile, ptr, ptr_type, ""); |
| 402 | | } |
| 403 | 124 | |
| 404 | | pub fn renderStoreUntyped( |
| 405 | | ofile: *ObjectFile, |
| 406 | | value: *llvm.Value, |
| 407 | | ptr: *llvm.Value, |
| 408 | | alignment: Type.Pointer.Align, |
| 409 | | vol: Type.Pointer.Vol, |
| 410 | | ) !*llvm.Value { |
| 411 | | const result = llvm.BuildStore(ofile.builder, value, ptr) orelse return error.OutOfMemory; |
| 412 | | switch (vol) { |
| 413 | | .Non => {}, |
| 414 | | .Volatile => llvm.SetVolatile(result, 1), |
| 125 | fn fail(self: *Function, src: usize, comptime format: []const u8, args: var) error{ CodegenFail, OutOfMemory } { |
| 126 | @setCold(true); |
| 127 | const msg = try std.fmt.allocPrint(self.errors.allocator, format, args); |
| 128 | { |
| 129 | errdefer self.errors.allocator.free(msg); |
| 130 | (try self.errors.addOne()).* = .{ |
| 131 | .byte_offset = src, |
| 132 | .msg = msg, |
| 133 | }; |
| 134 | } |
| 135 | return error.CodegenFail; |
| 415 | 136 | } |
| 416 | | llvm.SetAlignment(result, resolveAlign(ofile, alignment, llvm.TypeOf(value))); |
| 417 | | return result; |
| 418 | | } |
| 419 | | |
| 420 | | pub fn renderStore( |
| 421 | | ofile: *ObjectFile, |
| 422 | | value: *llvm.Value, |
| 423 | | ptr: *llvm.Value, |
| 424 | | ptr_type: *Type.Pointer, |
| 425 | | ) !*llvm.Value { |
| 426 | | return renderStoreUntyped(ofile, value, ptr, ptr_type.key.alignment, ptr_type.key.vol); |
| 427 | | } |
| 428 | | |
| 429 | | pub fn renderAlloca( |
| 430 | | ofile: *ObjectFile, |
| 431 | | var_type: *Type, |
| 432 | | name: []const u8, |
| 433 | | alignment: Type.Pointer.Align, |
| 434 | | ) !*llvm.Value { |
| 435 | | const llvm_var_type = try var_type.getLlvmType(ofile.arena, ofile.context); |
| 436 | | const name_with_null = try std.cstr.addNullByte(ofile.arena, name); |
| 437 | | const result = llvm.BuildAlloca(ofile.builder, llvm_var_type, @ptrCast([*:0]const u8, name_with_null.ptr)) orelse return error.OutOfMemory; |
| 438 | | llvm.SetAlignment(result, resolveAlign(ofile, alignment, llvm_var_type)); |
| 439 | | return result; |
| 440 | | } |
| 441 | | |
| 442 | | pub fn resolveAlign(ofile: *ObjectFile, alignment: Type.Pointer.Align, llvm_type: *llvm.Type) u32 { |
| 443 | | return switch (alignment) { |
| 444 | | .Abi => return llvm.ABIAlignmentOfType(ofile.comp.target_data_ref, llvm_type), |
| 445 | | .Override => |a| a, |
| 446 | | }; |
| 447 | | } |
| 137 | }; |