authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-13 15:19:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 17:51:19-07:00
logbc4d2b646d5d09ecb86a3806886fed37e522fdc9
treeb476088e3785c0f2820a5fc1fee3bc880310bc71
parent1642c003b4bab4a53b6094b42d10f6934896801e

compiler: update references to target


10 files changed, 326 insertions(+), 283 deletions(-)

src/Compilation.zig+13-14
......@@ -1915,9 +1915,8 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
19151915}
19161916
19171917pub fn destroy(self: *Compilation) void {
1918 const optional_module = self.module;
1919 self.bin_file.destroy();
1920 if (optional_module) |module| module.deinit();
1918 if (self.bin_file) |lf| lf.destroy();
1919 if (self.module) |zcu| zcu.deinit();
19211920
19221921 const gpa = self.gpa;
19231922 self.work_queue.deinit();
......@@ -2059,9 +2058,9 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
20592058
20602059 // If using the whole caching strategy, we check for *everything* up front, including
20612060 // C source files.
2062 if (comp.bin_file.options.cache_mode == .whole) {
2061 if (comp.cache_mode == .whole) {
20632062 // We are about to obtain this lock, so here we give other processes a chance first.
2064 comp.bin_file.releaseLock();
2063 if (comp.bin_file) |lf| lf.releaseLock();
20652064
20662065 man = comp.cache_parent.obtain();
20672066 comp.whole_cache_manifest = &man;
......@@ -5948,14 +5947,14 @@ pub fn get_libc_crt_file(comp: *Compilation, arena: Allocator, basename: []const
59485947}
59495948
59505949fn wantBuildLibCFromSource(comp: Compilation) bool {
5951 const is_exe_or_dyn_lib = switch (comp.bin_file.options.output_mode) {
5950 const is_exe_or_dyn_lib = switch (comp.config.output_mode) {
59525951 .Obj => false,
5953 .Lib => comp.bin_file.options.link_mode == .Dynamic,
5952 .Lib => comp.config.link_mode == .Dynamic,
59545953 .Exe => true,
59555954 };
5955 const ofmt = comp.root_mod.resolved_target.result.ofmt;
59565956 return comp.config.link_libc and is_exe_or_dyn_lib and
5957 comp.bin_file.options.libc_installation == null and
5958 comp.bin_file.options.target.ofmt != .c;
5957 comp.libc_installation == null and ofmt != .c;
59595958}
59605959
59615960fn wantBuildGLibCFromSource(comp: Compilation) bool {
......@@ -5977,13 +5976,13 @@ fn wantBuildMinGWFromSource(comp: Compilation) bool {
59775976}
59785977
59795978fn wantBuildLibUnwindFromSource(comp: *Compilation) bool {
5980 const is_exe_or_dyn_lib = switch (comp.bin_file.options.output_mode) {
5979 const is_exe_or_dyn_lib = switch (comp.config.output_mode) {
59815980 .Obj => false,
5982 .Lib => comp.bin_file.options.link_mode == .Dynamic,
5981 .Lib => comp.config.link_mode == .Dynamic,
59835982 .Exe => true,
59845983 };
5985 return is_exe_or_dyn_lib and comp.bin_file.options.link_libunwind and
5986 comp.bin_file.options.target.ofmt != .c;
5984 const ofmt = comp.root_mod.resolved_target.result.ofmt;
5985 return is_exe_or_dyn_lib and comp.config.link_libunwind and ofmt != .c;
59875986}
59885987
59895988fn setAllocFailure(comp: *Compilation) void {
......@@ -6112,7 +6111,7 @@ fn canBuildZigLibC(target: std.Target, use_llvm: bool) bool {
61126111}
61136112
61146113pub fn getZigBackend(comp: Compilation) std.builtin.CompilerBackend {
6115 const target = comp.bin_file.options.target;
6114 const target = comp.root_mod.resolved_target.result;
61166115 return target_util.zigBackend(target, comp.bin_file.options.use_llvm);
61176116}
61186117
src/Module.zig+24-18
......@@ -623,7 +623,8 @@ pub const Decl = struct {
623623 // Sanitize the name for nvptx which is more restrictive.
624624 // TODO This should be handled by the backend, not the frontend. Have a
625625 // look at how the C backend does it for inspiration.
626 if (mod.comp.bin_file.options.target.cpu.arch.isNvptx()) {
626 const cpu_arch = mod.root_mod.resolved_target.cpu.arch;
627 if (cpu_arch.isNvptx()) {
627628 for (ip.string_bytes.items[start..]) |*byte| switch (byte.*) {
628629 '{', '}', '*', '[', ']', '(', ')', ',', ' ', '\'' => byte.* = '_',
629630 else => {},
......@@ -4873,12 +4874,18 @@ pub fn errNoteNonLazy(
48734874 };
48744875}
48754876
4876pub fn getTarget(mod: Module) Target {
4877 return mod.comp.bin_file.options.target;
4877/// Deprecated. There is no global target for a Zig Compilation Unit. Instead,
4878/// look up the target based on the Module that contains the source code being
4879/// analyzed.
4880pub fn getTarget(zcu: Module) Target {
4881 return zcu.root_mod.resolved_target.result;
48784882}
48794883
4880pub fn optimizeMode(mod: Module) std.builtin.OptimizeMode {
4881 return mod.comp.bin_file.options.optimize_mode;
4884/// Deprecated. There is no global optimization mode for a Zig Compilation
4885/// Unit. Instead, look up the optimization mode based on the Module that
4886/// contains the source code being analyzed.
4887pub fn optimizeMode(zcu: Module) std.builtin.OptimizeMode {
4888 return zcu.root_mod.optimize_mode;
48824889}
48834890
48844891fn lockAndClearFileCompileError(mod: *Module, file: *File) void {
......@@ -5620,20 +5627,19 @@ pub const Feature = enum {
56205627 safety_checked_instructions,
56215628};
56225629
5623pub fn backendSupportsFeature(mod: Module, feature: Feature) bool {
5630pub fn backendSupportsFeature(zcu: Module, feature: Feature) bool {
5631 const cpu_arch = zcu.root_mod.resolved_target.cpu.arch;
5632 const ofmt = zcu.root_mod.resolved_target.ofmt;
5633 const use_llvm = zcu.comp.config.use_llvm;
56245634 return switch (feature) {
5625 .panic_fn => mod.comp.bin_file.options.target.ofmt == .c or
5626 mod.comp.bin_file.options.use_llvm or
5627 mod.comp.bin_file.options.target.cpu.arch == .x86_64,
5628 .panic_unwrap_error => mod.comp.bin_file.options.target.ofmt == .c or
5629 mod.comp.bin_file.options.use_llvm,
5630 .safety_check_formatted => mod.comp.bin_file.options.target.ofmt == .c or
5631 mod.comp.bin_file.options.use_llvm,
5632 .error_return_trace => mod.comp.bin_file.options.use_llvm,
5633 .is_named_enum_value => mod.comp.bin_file.options.use_llvm,
5634 .error_set_has_value => mod.comp.bin_file.options.use_llvm or mod.comp.bin_file.options.target.isWasm(),
5635 .field_reordering => mod.comp.bin_file.options.use_llvm,
5636 .safety_checked_instructions => mod.comp.bin_file.options.use_llvm,
5635 .panic_fn => ofmt == .c or use_llvm or cpu_arch == .x86_64,
5636 .panic_unwrap_error => ofmt == .c or use_llvm,
5637 .safety_check_formatted => ofmt == .c or use_llvm,
5638 .error_return_trace => use_llvm,
5639 .is_named_enum_value => use_llvm,
5640 .error_set_has_value => use_llvm or cpu_arch.isWasm(),
5641 .field_reordering => use_llvm,
5642 .safety_checked_instructions => use_llvm,
56375643 };
56385644}
56395645
src/arch/aarch64/CodeGen.zig+22-23
......@@ -329,7 +329,7 @@ const BigTomb = struct {
329329const Self = @This();
330330
331331pub fn generate(
332 bin_file: *link.File,
332 lf: *link.File,
333333 src_loc: Module.SrcLoc,
334334 func_index: InternPool.Index,
335335 air: Air,
......@@ -337,31 +337,30 @@ pub fn generate(
337337 code: *std.ArrayList(u8),
338338 debug_output: DebugInfoOutput,
339339) CodeGenError!Result {
340 if (build_options.skip_non_native and builtin.cpu.arch != bin_file.options.target.cpu.arch) {
341 @panic("Attempted to compile for architecture that was disabled by build configuration");
342 }
343
344 const mod = bin_file.comp.module.?;
345 const func = mod.funcInfo(func_index);
346 const fn_owner_decl = mod.declPtr(func.owner_decl);
340 const gpa = lf.comp.gpa;
341 const zcu = lf.comp.module.?;
342 const func = zcu.funcInfo(func_index);
343 const fn_owner_decl = zcu.declPtr(func.owner_decl);
347344 assert(fn_owner_decl.has_tv);
348345 const fn_type = fn_owner_decl.ty;
346 const namespace = zcu.namespacePtr(fn_owner_decl.src_namespace);
347 const target = &namespace.file_scope.mod.target;
349348
350 var branch_stack = std.ArrayList(Branch).init(bin_file.allocator);
349 var branch_stack = std.ArrayList(Branch).init(gpa);
351350 defer {
352351 assert(branch_stack.items.len == 1);
353 branch_stack.items[0].deinit(bin_file.allocator);
352 branch_stack.items[0].deinit(gpa);
354353 branch_stack.deinit();
355354 }
356355 try branch_stack.append(.{});
357356
358357 var function = Self{
359 .gpa = bin_file.allocator,
358 .gpa = gpa,
360359 .air = air,
361360 .liveness = liveness,
362361 .debug_output = debug_output,
363 .target = &bin_file.options.target,
364 .bin_file = bin_file,
362 .target = target,
363 .bin_file = lf,
365364 .func_index = func_index,
366365 .owner_decl = func.owner_decl,
367366 .err_msg = null,
......@@ -375,15 +374,15 @@ pub fn generate(
375374 .end_di_line = func.rbrace_line,
376375 .end_di_column = func.rbrace_column,
377376 };
378 defer function.stack.deinit(bin_file.allocator);
379 defer function.blocks.deinit(bin_file.allocator);
380 defer function.exitlude_jump_relocs.deinit(bin_file.allocator);
381 defer function.dbg_info_relocs.deinit(bin_file.allocator);
377 defer function.stack.deinit(gpa);
378 defer function.blocks.deinit(gpa);
379 defer function.exitlude_jump_relocs.deinit(gpa);
380 defer function.dbg_info_relocs.deinit(gpa);
382381
383382 var call_info = function.resolveCallingConventionValues(fn_type) catch |err| switch (err) {
384383 error.CodegenFail => return Result{ .fail = function.err_msg.? },
385384 error.OutOfRegisters => return Result{
386 .fail = try ErrorMsg.create(bin_file.allocator, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
385 .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
387386 },
388387 else => |e| return e,
389388 };
......@@ -397,7 +396,7 @@ pub fn generate(
397396 function.gen() catch |err| switch (err) {
398397 error.CodegenFail => return Result{ .fail = function.err_msg.? },
399398 error.OutOfRegisters => return Result{
400 .fail = try ErrorMsg.create(bin_file.allocator, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
399 .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
401400 },
402401 else => |e| return e,
403402 };
......@@ -408,15 +407,15 @@ pub fn generate(
408407
409408 var mir = Mir{
410409 .instructions = function.mir_instructions.toOwnedSlice(),
411 .extra = try function.mir_extra.toOwnedSlice(bin_file.allocator),
410 .extra = try function.mir_extra.toOwnedSlice(gpa),
412411 };
413 defer mir.deinit(bin_file.allocator);
412 defer mir.deinit(gpa);
414413
415414 var emit = Emit{
416415 .mir = mir,
417 .bin_file = bin_file,
416 .bin_file = lf,
418417 .debug_output = debug_output,
419 .target = &bin_file.options.target,
418 .target = target,
420419 .src_loc = src_loc,
421420 .code = code,
422421 .prev_di_pc = 0,
src/arch/arm/CodeGen.zig+23-24
......@@ -336,7 +336,7 @@ const DbgInfoReloc = struct {
336336const Self = @This();
337337
338338pub fn generate(
339 bin_file: *link.File,
339 lf: *link.File,
340340 src_loc: Module.SrcLoc,
341341 func_index: InternPool.Index,
342342 air: Air,
......@@ -344,30 +344,29 @@ pub fn generate(
344344 code: *std.ArrayList(u8),
345345 debug_output: DebugInfoOutput,
346346) CodeGenError!Result {
347 if (build_options.skip_non_native and builtin.cpu.arch != bin_file.options.target.cpu.arch) {
348 @panic("Attempted to compile for architecture that was disabled by build configuration");
349 }
350
351 const mod = bin_file.comp.module.?;
352 const func = mod.funcInfo(func_index);
353 const fn_owner_decl = mod.declPtr(func.owner_decl);
347 const gpa = lf.comp.gpa;
348 const zcu = lf.comp.module.?;
349 const func = zcu.funcInfo(func_index);
350 const fn_owner_decl = zcu.declPtr(func.owner_decl);
354351 assert(fn_owner_decl.has_tv);
355352 const fn_type = fn_owner_decl.ty;
353 const namespace = zcu.namespacePtr(fn_owner_decl.src_namespace);
354 const target = &namespace.file_scope.mod.target;
356355
357 var branch_stack = std.ArrayList(Branch).init(bin_file.allocator);
356 var branch_stack = std.ArrayList(Branch).init(gpa);
358357 defer {
359358 assert(branch_stack.items.len == 1);
360 branch_stack.items[0].deinit(bin_file.allocator);
359 branch_stack.items[0].deinit(gpa);
361360 branch_stack.deinit();
362361 }
363362 try branch_stack.append(.{});
364363
365 var function = Self{
366 .gpa = bin_file.allocator,
364 var function: Self = .{
365 .gpa = gpa,
367366 .air = air,
368367 .liveness = liveness,
369 .target = &bin_file.options.target,
370 .bin_file = bin_file,
368 .target = target,
369 .bin_file = lf,
371370 .debug_output = debug_output,
372371 .func_index = func_index,
373372 .err_msg = null,
......@@ -381,15 +380,15 @@ pub fn generate(
381380 .end_di_line = func.rbrace_line,
382381 .end_di_column = func.rbrace_column,
383382 };
384 defer function.stack.deinit(bin_file.allocator);
385 defer function.blocks.deinit(bin_file.allocator);
386 defer function.exitlude_jump_relocs.deinit(bin_file.allocator);
387 defer function.dbg_info_relocs.deinit(bin_file.allocator);
383 defer function.stack.deinit(gpa);
384 defer function.blocks.deinit(gpa);
385 defer function.exitlude_jump_relocs.deinit(gpa);
386 defer function.dbg_info_relocs.deinit(gpa);
388387
389388 var call_info = function.resolveCallingConventionValues(fn_type) catch |err| switch (err) {
390389 error.CodegenFail => return Result{ .fail = function.err_msg.? },
391390 error.OutOfRegisters => return Result{
392 .fail = try ErrorMsg.create(bin_file.allocator, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
391 .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
393392 },
394393 else => |e| return e,
395394 };
......@@ -403,7 +402,7 @@ pub fn generate(
403402 function.gen() catch |err| switch (err) {
404403 error.CodegenFail => return Result{ .fail = function.err_msg.? },
405404 error.OutOfRegisters => return Result{
406 .fail = try ErrorMsg.create(bin_file.allocator, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
405 .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
407406 },
408407 else => |e| return e,
409408 };
......@@ -414,15 +413,15 @@ pub fn generate(
414413
415414 var mir = Mir{
416415 .instructions = function.mir_instructions.toOwnedSlice(),
417 .extra = try function.mir_extra.toOwnedSlice(bin_file.allocator),
416 .extra = try function.mir_extra.toOwnedSlice(gpa),
418417 };
419 defer mir.deinit(bin_file.allocator);
418 defer mir.deinit(gpa);
420419
421420 var emit = Emit{
422421 .mir = mir,
423 .bin_file = bin_file,
422 .bin_file = lf,
424423 .debug_output = debug_output,
425 .target = &bin_file.options.target,
424 .target = target,
426425 .src_loc = src_loc,
427426 .code = code,
428427 .prev_di_pc = 0,
src/arch/riscv64/CodeGen.zig+21-22
......@@ -217,7 +217,7 @@ const BigTomb = struct {
217217const Self = @This();
218218
219219pub fn generate(
220 bin_file: *link.File,
220 lf: *link.File,
221221 src_loc: Module.SrcLoc,
222222 func_index: InternPool.Index,
223223 air: Air,
......@@ -225,30 +225,29 @@ pub fn generate(
225225 code: *std.ArrayList(u8),
226226 debug_output: DebugInfoOutput,
227227) CodeGenError!Result {
228 if (build_options.skip_non_native and builtin.cpu.arch != bin_file.options.target.cpu.arch) {
229 @panic("Attempted to compile for architecture that was disabled by build configuration");
230 }
231
232 const mod = bin_file.comp.module.?;
233 const func = mod.funcInfo(func_index);
234 const fn_owner_decl = mod.declPtr(func.owner_decl);
228 const gpa = lf.comp.gpa;
229 const zcu = lf.comp.module.?;
230 const func = zcu.funcInfo(func_index);
231 const fn_owner_decl = zcu.declPtr(func.owner_decl);
235232 assert(fn_owner_decl.has_tv);
236233 const fn_type = fn_owner_decl.ty;
234 const namespace = zcu.namespacePtr(fn_owner_decl.src_namespace);
235 const target = &namespace.file_scope.mod.target;
237236
238 var branch_stack = std.ArrayList(Branch).init(bin_file.allocator);
237 var branch_stack = std.ArrayList(Branch).init(gpa);
239238 defer {
240239 assert(branch_stack.items.len == 1);
241 branch_stack.items[0].deinit(bin_file.allocator);
240 branch_stack.items[0].deinit(gpa);
242241 branch_stack.deinit();
243242 }
244243 try branch_stack.append(.{});
245244
246245 var function = Self{
247 .gpa = bin_file.allocator,
246 .gpa = gpa,
248247 .air = air,
249248 .liveness = liveness,
250 .target = &bin_file.options.target,
251 .bin_file = bin_file,
249 .target = target,
250 .bin_file = lf,
252251 .func_index = func_index,
253252 .code = code,
254253 .debug_output = debug_output,
......@@ -263,14 +262,14 @@ pub fn generate(
263262 .end_di_line = func.rbrace_line,
264263 .end_di_column = func.rbrace_column,
265264 };
266 defer function.stack.deinit(bin_file.allocator);
267 defer function.blocks.deinit(bin_file.allocator);
268 defer function.exitlude_jump_relocs.deinit(bin_file.allocator);
265 defer function.stack.deinit(gpa);
266 defer function.blocks.deinit(gpa);
267 defer function.exitlude_jump_relocs.deinit(gpa);
269268
270269 var call_info = function.resolveCallingConventionValues(fn_type) catch |err| switch (err) {
271270 error.CodegenFail => return Result{ .fail = function.err_msg.? },
272271 error.OutOfRegisters => return Result{
273 .fail = try ErrorMsg.create(bin_file.allocator, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
272 .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
274273 },
275274 else => |e| return e,
276275 };
......@@ -284,22 +283,22 @@ pub fn generate(
284283 function.gen() catch |err| switch (err) {
285284 error.CodegenFail => return Result{ .fail = function.err_msg.? },
286285 error.OutOfRegisters => return Result{
287 .fail = try ErrorMsg.create(bin_file.allocator, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
286 .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
288287 },
289288 else => |e| return e,
290289 };
291290
292291 var mir = Mir{
293292 .instructions = function.mir_instructions.toOwnedSlice(),
294 .extra = try function.mir_extra.toOwnedSlice(bin_file.allocator),
293 .extra = try function.mir_extra.toOwnedSlice(gpa),
295294 };
296 defer mir.deinit(bin_file.allocator);
295 defer mir.deinit(gpa);
297296
298297 var emit = Emit{
299298 .mir = mir,
300 .bin_file = bin_file,
299 .bin_file = lf,
301300 .debug_output = debug_output,
302 .target = &bin_file.options.target,
301 .target = target,
303302 .src_loc = src_loc,
304303 .code = code,
305304 .prev_di_pc = 0,
src/arch/sparc64/CodeGen.zig+21-22
......@@ -260,7 +260,7 @@ const BigTomb = struct {
260260};
261261
262262pub fn generate(
263 bin_file: *link.File,
263 lf: *link.File,
264264 src_loc: Module.SrcLoc,
265265 func_index: InternPool.Index,
266266 air: Air,
......@@ -268,31 +268,30 @@ pub fn generate(
268268 code: *std.ArrayList(u8),
269269 debug_output: DebugInfoOutput,
270270) CodeGenError!Result {
271 if (build_options.skip_non_native and builtin.cpu.arch != bin_file.options.target.cpu.arch) {
272 @panic("Attempted to compile for architecture that was disabled by build configuration");
273 }
274
275 const mod = bin_file.comp.module.?;
276 const func = mod.funcInfo(func_index);
277 const fn_owner_decl = mod.declPtr(func.owner_decl);
271 const gpa = lf.comp.gpa;
272 const zcu = lf.comp.module.?;
273 const func = zcu.funcInfo(func_index);
274 const fn_owner_decl = zcu.declPtr(func.owner_decl);
278275 assert(fn_owner_decl.has_tv);
279276 const fn_type = fn_owner_decl.ty;
277 const namespace = zcu.namespacePtr(fn_owner_decl.src_namespace);
278 const target = &namespace.file_scope.mod.target;
280279
281 var branch_stack = std.ArrayList(Branch).init(bin_file.allocator);
280 var branch_stack = std.ArrayList(Branch).init(gpa);
282281 defer {
283282 assert(branch_stack.items.len == 1);
284 branch_stack.items[0].deinit(bin_file.allocator);
283 branch_stack.items[0].deinit(gpa);
285284 branch_stack.deinit();
286285 }
287286 try branch_stack.append(.{});
288287
289288 var function = Self{
290 .gpa = bin_file.allocator,
289 .gpa = gpa,
291290 .air = air,
292291 .liveness = liveness,
293 .target = &bin_file.options.target,
292 .target = target,
294293 .func_index = func_index,
295 .bin_file = bin_file,
294 .bin_file = lf,
296295 .code = code,
297296 .debug_output = debug_output,
298297 .err_msg = null,
......@@ -306,14 +305,14 @@ pub fn generate(
306305 .end_di_line = func.rbrace_line,
307306 .end_di_column = func.rbrace_column,
308307 };
309 defer function.stack.deinit(bin_file.allocator);
310 defer function.blocks.deinit(bin_file.allocator);
311 defer function.exitlude_jump_relocs.deinit(bin_file.allocator);
308 defer function.stack.deinit(gpa);
309 defer function.blocks.deinit(gpa);
310 defer function.exitlude_jump_relocs.deinit(gpa);
312311
313312 var call_info = function.resolveCallingConventionValues(fn_type, .callee) catch |err| switch (err) {
314313 error.CodegenFail => return Result{ .fail = function.err_msg.? },
315314 error.OutOfRegisters => return Result{
316 .fail = try ErrorMsg.create(bin_file.allocator, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
315 .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
317316 },
318317 else => |e| return e,
319318 };
......@@ -327,22 +326,22 @@ pub fn generate(
327326 function.gen() catch |err| switch (err) {
328327 error.CodegenFail => return Result{ .fail = function.err_msg.? },
329328 error.OutOfRegisters => return Result{
330 .fail = try ErrorMsg.create(bin_file.allocator, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
329 .fail = try ErrorMsg.create(gpa, src_loc, "CodeGen ran out of registers. This is a bug in the Zig compiler.", .{}),
331330 },
332331 else => |e| return e,
333332 };
334333
335334 var mir = Mir{
336335 .instructions = function.mir_instructions.toOwnedSlice(),
337 .extra = try function.mir_extra.toOwnedSlice(bin_file.allocator),
336 .extra = try function.mir_extra.toOwnedSlice(gpa),
338337 };
339 defer mir.deinit(bin_file.allocator);
338 defer mir.deinit(gpa);
340339
341340 var emit = Emit{
342341 .mir = mir,
343 .bin_file = bin_file,
342 .bin_file = lf,
344343 .debug_output = debug_output,
345 .target = &bin_file.options.target,
344 .target = target,
346345 .src_loc = src_loc,
347346 .code = code,
348347 .prev_di_pc = 0,
src/arch/wasm/CodeGen.zig+5-2
......@@ -1212,16 +1212,19 @@ pub fn generate(
12121212 _ = src_loc;
12131213 const mod = bin_file.comp.module.?;
12141214 const func = mod.funcInfo(func_index);
1215 const decl = mod.declPtr(func.owner_decl);
1216 const namespace = mod.namespacePtr(decl.src_namespace);
1217 const target = namespace.file_scope.mod.target;
12151218 var code_gen: CodeGen = .{
12161219 .gpa = bin_file.allocator,
12171220 .air = air,
12181221 .liveness = liveness,
12191222 .code = code,
12201223 .decl_index = func.owner_decl,
1221 .decl = mod.declPtr(func.owner_decl),
1224 .decl = decl,
12221225 .err_msg = undefined,
12231226 .locals = .{},
1224 .target = bin_file.options.target,
1227 .target = target,
12251228 .bin_file = bin_file.cast(link.File.Wasm).?,
12261229 .debug_output = debug_output,
12271230 .func_index = func_index,
src/arch/x86_64/CodeGen.zig+10-7
......@@ -795,22 +795,20 @@ pub fn generate(
795795 code: *std.ArrayList(u8),
796796 debug_output: DebugInfoOutput,
797797) CodeGenError!Result {
798 if (build_options.skip_non_native and builtin.cpu.arch != bin_file.options.target.cpu.arch) {
799 @panic("Attempted to compile for architecture that was disabled by build configuration");
800 }
801
802798 const mod = bin_file.comp.module.?;
803799 const func = mod.funcInfo(func_index);
804800 const fn_owner_decl = mod.declPtr(func.owner_decl);
805801 assert(fn_owner_decl.has_tv);
806802 const fn_type = fn_owner_decl.ty;
803 const namespace = mod.namespacePtr(fn_owner_decl.src_namespace);
804 const target = namespace.file_scope.mod.target;
807805
808806 const gpa = bin_file.allocator;
809807 var function = Self{
810808 .gpa = gpa,
811809 .air = air,
812810 .liveness = liveness,
813 .target = &bin_file.options.target,
811 .target = target,
814812 .bin_file = bin_file,
815813 .debug_output = debug_output,
816814 .owner = .{ .func_index = func_index },
......@@ -882,7 +880,7 @@ pub fn generate(
882880 .size = Type.usize.abiSize(mod),
883881 .alignment = Alignment.min(
884882 call_info.stack_align,
885 Alignment.fromNonzeroByteUnits(bin_file.options.target.stackAlignment()),
883 Alignment.fromNonzeroByteUnits(target.stackAlignment()),
886884 ),
887885 }));
888886 function.frame_allocs.set(
......@@ -967,11 +965,16 @@ pub fn generateLazy(
967965 debug_output: DebugInfoOutput,
968966) CodeGenError!Result {
969967 const gpa = bin_file.allocator;
968 const zcu = bin_file.comp.module.?;
969 const decl_index = lazy_sym.ty.getOwnerDecl(zcu);
970 const decl = zcu.declPtr(decl_index);
971 const namespace = zcu.namespacePtr(decl.src_namespace);
972 const target = namespace.file_scope.mod.target;
970973 var function = Self{
971974 .gpa = gpa,
972975 .air = undefined,
973976 .liveness = undefined,
974 .target = &bin_file.options.target,
977 .target = target,
975978 .bin_file = bin_file,
976979 .debug_output = debug_output,
977980 .owner = .{ .lazy_sym = lazy_sym },
src/codegen.zig+153-129
......@@ -45,7 +45,7 @@ pub const DebugInfoOutput = union(enum) {
4545};
4646
4747pub fn generateFunction(
48 bin_file: *link.File,
48 lf: *link.File,
4949 src_loc: Module.SrcLoc,
5050 func_index: InternPool.Index,
5151 air: Air,
......@@ -53,33 +53,43 @@ pub fn generateFunction(
5353 code: *std.ArrayList(u8),
5454 debug_output: DebugInfoOutput,
5555) CodeGenError!Result {
56 switch (bin_file.options.target.cpu.arch) {
56 const zcu = lf.comp.module.?;
57 const func = zcu.funcInfo(func_index);
58 const decl = zcu.declPtr(func.owner_decl);
59 const namespace = zcu.namespacePtr(decl.src_namespace);
60 const target = namespace.file_scope.mod.target;
61 switch (target.cpu.arch) {
5762 .arm,
5863 .armeb,
59 => return @import("arch/arm/CodeGen.zig").generate(bin_file, src_loc, func_index, air, liveness, code, debug_output),
64 => return @import("arch/arm/CodeGen.zig").generate(lf, src_loc, func_index, air, liveness, code, debug_output),
6065 .aarch64,
6166 .aarch64_be,
6267 .aarch64_32,
63 => return @import("arch/aarch64/CodeGen.zig").generate(bin_file, src_loc, func_index, air, liveness, code, debug_output),
64 .riscv64 => return @import("arch/riscv64/CodeGen.zig").generate(bin_file, src_loc, func_index, air, liveness, code, debug_output),
65 .sparc64 => return @import("arch/sparc64/CodeGen.zig").generate(bin_file, src_loc, func_index, air, liveness, code, debug_output),
66 .x86_64 => return @import("arch/x86_64/CodeGen.zig").generate(bin_file, src_loc, func_index, air, liveness, code, debug_output),
68 => return @import("arch/aarch64/CodeGen.zig").generate(lf, src_loc, func_index, air, liveness, code, debug_output),
69 .riscv64 => return @import("arch/riscv64/CodeGen.zig").generate(lf, src_loc, func_index, air, liveness, code, debug_output),
70 .sparc64 => return @import("arch/sparc64/CodeGen.zig").generate(lf, src_loc, func_index, air, liveness, code, debug_output),
71 .x86_64 => return @import("arch/x86_64/CodeGen.zig").generate(lf, src_loc, func_index, air, liveness, code, debug_output),
6772 .wasm32,
6873 .wasm64,
69 => return @import("arch/wasm/CodeGen.zig").generate(bin_file, src_loc, func_index, air, liveness, code, debug_output),
74 => return @import("arch/wasm/CodeGen.zig").generate(lf, src_loc, func_index, air, liveness, code, debug_output),
7075 else => unreachable,
7176 }
7277}
7378
7479pub fn generateLazyFunction(
75 bin_file: *link.File,
80 lf: *link.File,
7681 src_loc: Module.SrcLoc,
7782 lazy_sym: link.File.LazySymbol,
7883 code: *std.ArrayList(u8),
7984 debug_output: DebugInfoOutput,
8085) CodeGenError!Result {
81 switch (bin_file.options.target.cpu.arch) {
82 .x86_64 => return @import("arch/x86_64/CodeGen.zig").generateLazy(bin_file, src_loc, lazy_sym, code, debug_output),
86 const zcu = lf.comp.module.?;
87 const decl_index = lazy_sym.ty.getOwnerDecl(zcu);
88 const decl = zcu.declPtr(decl_index);
89 const namespace = zcu.namespacePtr(decl.src_namespace);
90 const target = namespace.file_scope.mod.target;
91 switch (target.cpu.arch) {
92 .x86_64 => return @import("arch/x86_64/CodeGen.zig").generateLazy(lf, src_loc, lazy_sym, code, debug_output),
8393 else => unreachable,
8494 }
8595}
......@@ -107,13 +117,16 @@ pub fn generateLazySymbol(
107117 const tracy = trace(@src());
108118 defer tracy.end();
109119
110 const target = bin_file.options.target;
120 const zcu = bin_file.comp.module.?;
121 const decl_index = lazy_sym.ty.getOwnerDecl(zcu);
122 const decl = zcu.declPtr(decl_index);
123 const namespace = zcu.namespacePtr(decl.src_namespace);
124 const target = namespace.file_scope.mod.target;
111125 const endian = target.cpu.arch.endian();
112126
113 const mod = bin_file.comp.module.?;
114127 log.debug("generateLazySymbol: kind = {s}, ty = {}", .{
115128 @tagName(lazy_sym.kind),
116 lazy_sym.ty.fmt(mod),
129 lazy_sym.ty.fmt(zcu),
117130 });
118131
119132 if (lazy_sym.kind == .code) {
......@@ -121,14 +134,14 @@ pub fn generateLazySymbol(
121134 return generateLazyFunction(bin_file, src_loc, lazy_sym, code, debug_output);
122135 }
123136
124 if (lazy_sym.ty.isAnyError(mod)) {
137 if (lazy_sym.ty.isAnyError(zcu)) {
125138 alignment.* = .@"4";
126 const err_names = mod.global_error_set.keys();
139 const err_names = zcu.global_error_set.keys();
127140 mem.writeInt(u32, try code.addManyAsArray(4), @as(u32, @intCast(err_names.len)), endian);
128141 var offset = code.items.len;
129142 try code.resize((1 + err_names.len + 1) * 4);
130143 for (err_names) |err_name_nts| {
131 const err_name = mod.intern_pool.stringToSlice(err_name_nts);
144 const err_name = zcu.intern_pool.stringToSlice(err_name_nts);
132145 mem.writeInt(u32, code.items[offset..][0..4], @as(u32, @intCast(code.items.len)), endian);
133146 offset += 4;
134147 try code.ensureUnusedCapacity(err_name.len + 1);
......@@ -137,10 +150,10 @@ pub fn generateLazySymbol(
137150 }
138151 mem.writeInt(u32, code.items[offset..][0..4], @as(u32, @intCast(code.items.len)), endian);
139152 return Result.ok;
140 } else if (lazy_sym.ty.zigTypeTag(mod) == .Enum) {
153 } else if (lazy_sym.ty.zigTypeTag(zcu) == .Enum) {
141154 alignment.* = .@"1";
142 for (lazy_sym.ty.enumFields(mod)) |tag_name_ip| {
143 const tag_name = mod.intern_pool.stringToSlice(tag_name_ip);
155 for (lazy_sym.ty.enumFields(zcu)) |tag_name_ip| {
156 const tag_name = zcu.intern_pool.stringToSlice(tag_name_ip);
144157 try code.ensureUnusedCapacity(tag_name.len + 1);
145158 code.appendSliceAssumeCapacity(tag_name);
146159 code.appendAssumeCapacity(0);
......@@ -150,7 +163,7 @@ pub fn generateLazySymbol(
150163 bin_file.allocator,
151164 src_loc,
152165 "TODO implement generateLazySymbol for {s} {}",
153 .{ @tagName(lazy_sym.kind), lazy_sym.ty.fmt(mod) },
166 .{ @tagName(lazy_sym.kind), lazy_sym.ty.fmt(zcu) },
154167 ) };
155168}
156169
......@@ -757,7 +770,7 @@ const RelocInfo = struct {
757770};
758771
759772fn lowerAnonDeclRef(
760 bin_file: *link.File,
773 lf: *link.File,
761774 src_loc: Module.SrcLoc,
762775 anon_decl: InternPool.Key.Ptr.Addr.AnonDecl,
763776 code: *std.ArrayList(u8),
......@@ -765,27 +778,27 @@ fn lowerAnonDeclRef(
765778 reloc_info: RelocInfo,
766779) CodeGenError!Result {
767780 _ = debug_output;
768 const target = bin_file.options.target;
769 const mod = bin_file.comp.module.?;
781 const zcu = lf.comp.module.?;
782 const target = lf.comp.root_mod.resolved_target.result;
770783
771784 const ptr_width_bytes = @divExact(target.ptrBitWidth(), 8);
772785 const decl_val = anon_decl.val;
773 const decl_ty = Type.fromInterned(mod.intern_pool.typeOf(decl_val));
774 log.debug("lowerAnonDecl: ty = {}", .{decl_ty.fmt(mod)});
775 const is_fn_body = decl_ty.zigTypeTag(mod) == .Fn;
776 if (!is_fn_body and !decl_ty.hasRuntimeBits(mod)) {
786 const decl_ty = Type.fromInterned(zcu.intern_pool.typeOf(decl_val));
787 log.debug("lowerAnonDecl: ty = {}", .{decl_ty.fmt(zcu)});
788 const is_fn_body = decl_ty.zigTypeTag(zcu) == .Fn;
789 if (!is_fn_body and !decl_ty.hasRuntimeBits(zcu)) {
777790 try code.appendNTimes(0xaa, ptr_width_bytes);
778791 return Result.ok;
779792 }
780793
781 const decl_align = mod.intern_pool.indexToKey(anon_decl.orig_ty).ptr_type.flags.alignment;
782 const res = try bin_file.lowerAnonDecl(decl_val, decl_align, src_loc);
794 const decl_align = zcu.intern_pool.indexToKey(anon_decl.orig_ty).ptr_type.flags.alignment;
795 const res = try lf.lowerAnonDecl(decl_val, decl_align, src_loc);
783796 switch (res) {
784797 .ok => {},
785798 .fail => |em| return .{ .fail = em },
786799 }
787800
788 const vaddr = try bin_file.getAnonDeclVAddr(decl_val, .{
801 const vaddr = try lf.getAnonDeclVAddr(decl_val, .{
789802 .parent_atom_index = reloc_info.parent_atom_index,
790803 .offset = code.items.len,
791804 .addend = reloc_info.addend orelse 0,
......@@ -802,7 +815,7 @@ fn lowerAnonDeclRef(
802815}
803816
804817fn lowerDeclRef(
805 bin_file: *link.File,
818 lf: *link.File,
806819 src_loc: Module.SrcLoc,
807820 decl_index: InternPool.DeclIndex,
808821 code: *std.ArrayList(u8),
......@@ -811,20 +824,21 @@ fn lowerDeclRef(
811824) CodeGenError!Result {
812825 _ = src_loc;
813826 _ = debug_output;
814 const target = bin_file.options.target;
815 const mod = bin_file.comp.module.?;
827 const zcu = lf.comp.module.?;
828 const decl = zcu.declPtr(decl_index);
829 const namespace = zcu.namespacePtr(decl.src_namespace);
830 const target = namespace.file_scope.mod.target;
816831
817832 const ptr_width = target.ptrBitWidth();
818 const decl = mod.declPtr(decl_index);
819 const is_fn_body = decl.ty.zigTypeTag(mod) == .Fn;
820 if (!is_fn_body and !decl.ty.hasRuntimeBits(mod)) {
833 const is_fn_body = decl.ty.zigTypeTag(zcu) == .Fn;
834 if (!is_fn_body and !decl.ty.hasRuntimeBits(zcu)) {
821835 try code.appendNTimes(0xaa, @divExact(ptr_width, 8));
822836 return Result.ok;
823837 }
824838
825 try mod.markDeclAlive(decl);
839 try zcu.markDeclAlive(decl);
826840
827 const vaddr = try bin_file.getDeclVAddr(decl_index, .{
841 const vaddr = try lf.getDeclVAddr(decl_index, .{
828842 .parent_atom_index = reloc_info.parent_atom_index,
829843 .offset = code.items.len,
830844 .addend = reloc_info.addend orelse 0,
......@@ -897,27 +911,29 @@ pub const GenResult = union(enum) {
897911};
898912
899913fn genDeclRef(
900 bin_file: *link.File,
914 lf: *link.File,
901915 src_loc: Module.SrcLoc,
902916 tv: TypedValue,
903917 ptr_decl_index: InternPool.DeclIndex,
904918) CodeGenError!GenResult {
905 const mod = bin_file.comp.module.?;
906 log.debug("genDeclRef: ty = {}, val = {}", .{ tv.ty.fmt(mod), tv.val.fmtValue(tv.ty, mod) });
919 const zcu = lf.comp.module.?;
920 log.debug("genDeclRef: ty = {}, val = {}", .{ tv.ty.fmt(zcu), tv.val.fmtValue(tv.ty, zcu) });
921
922 const ptr_decl = zcu.declPtr(ptr_decl_index);
923 const namespace = zcu.namespacePtr(ptr_decl.src_namespace);
924 const target = namespace.file_scope.mod.target;
907925
908 const target = bin_file.options.target;
909926 const ptr_bits = target.ptrBitWidth();
910927 const ptr_bytes: u64 = @divExact(ptr_bits, 8);
911928
912 const ptr_decl = mod.declPtr(ptr_decl_index);
913 const decl_index = switch (mod.intern_pool.indexToKey(try ptr_decl.internValue(mod))) {
929 const decl_index = switch (zcu.intern_pool.indexToKey(try ptr_decl.internValue(zcu))) {
914930 .func => |func| func.owner_decl,
915931 .extern_func => |extern_func| extern_func.decl,
916932 else => ptr_decl_index,
917933 };
918 const decl = mod.declPtr(decl_index);
934 const decl = zcu.declPtr(decl_index);
919935
920 if (!decl.ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) {
936 if (!decl.ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) {
921937 const imm: u64 = switch (ptr_bytes) {
922938 1 => 0xaa,
923939 2 => 0xaaaa,
......@@ -929,30 +945,30 @@ fn genDeclRef(
929945 }
930946
931947 // TODO this feels clunky. Perhaps we should check for it in `genTypedValue`?
932 if (tv.ty.castPtrToFn(mod)) |fn_ty| {
933 if (mod.typeToFunc(fn_ty).?.is_generic) {
934 return GenResult.mcv(.{ .immediate = fn_ty.abiAlignment(mod).toByteUnitsOptional().? });
948 if (tv.ty.castPtrToFn(zcu)) |fn_ty| {
949 if (zcu.typeToFunc(fn_ty).?.is_generic) {
950 return GenResult.mcv(.{ .immediate = fn_ty.abiAlignment(zcu).toByteUnitsOptional().? });
935951 }
936 } else if (tv.ty.zigTypeTag(mod) == .Pointer) {
937 const elem_ty = tv.ty.elemType2(mod);
938 if (!elem_ty.hasRuntimeBits(mod)) {
939 return GenResult.mcv(.{ .immediate = elem_ty.abiAlignment(mod).toByteUnitsOptional().? });
952 } else if (tv.ty.zigTypeTag(zcu) == .Pointer) {
953 const elem_ty = tv.ty.elemType2(zcu);
954 if (!elem_ty.hasRuntimeBits(zcu)) {
955 return GenResult.mcv(.{ .immediate = elem_ty.abiAlignment(zcu).toByteUnitsOptional().? });
940956 }
941957 }
942958
943 try mod.markDeclAlive(decl);
959 try zcu.markDeclAlive(decl);
944960
945 const decl_namespace = mod.namespacePtr(decl.namespace_index);
946 const single_threaded = decl_namespace.file_scope.mod.single_threaded;
947 const is_threadlocal = tv.val.isPtrToThreadLocal(mod) and !single_threaded;
948 const is_extern = decl.isExtern(mod);
961 const decl_namespace = zcu.namespacePtr(decl.namespace_index);
962 const single_threaded = decl_namespace.file_scope.zcu.single_threaded;
963 const is_threadlocal = tv.val.isPtrToThreadLocal(zcu) and !single_threaded;
964 const is_extern = decl.isExtern(zcu);
949965
950 if (bin_file.cast(link.File.Elf)) |elf_file| {
966 if (lf.cast(link.File.Elf)) |elf_file| {
951967 if (is_extern) {
952 const name = mod.intern_pool.stringToSlice(decl.name);
968 const name = zcu.intern_pool.stringToSlice(decl.name);
953969 // TODO audit this
954 const lib_name = if (decl.getOwnedVariable(mod)) |ov|
955 mod.intern_pool.stringToSliceUnwrap(ov.lib_name)
970 const lib_name = if (decl.getOwnedVariable(zcu)) |ov|
971 zcu.intern_pool.stringToSliceUnwrap(ov.lib_name)
956972 else
957973 null;
958974 const sym_index = try elf_file.getGlobalSymbol(name, lib_name);
......@@ -965,12 +981,12 @@ fn genDeclRef(
965981 return GenResult.mcv(.{ .load_tlv = sym.esym_index });
966982 }
967983 return GenResult.mcv(.{ .load_symbol = sym.esym_index });
968 } else if (bin_file.cast(link.File.MachO)) |macho_file| {
984 } else if (lf.cast(link.File.MachO)) |macho_file| {
969985 if (is_extern) {
970986 // TODO make this part of getGlobalSymbol
971 const name = mod.intern_pool.stringToSlice(decl.name);
972 const sym_name = try std.fmt.allocPrint(bin_file.allocator, "_{s}", .{name});
973 defer bin_file.allocator.free(sym_name);
987 const name = zcu.intern_pool.stringToSlice(decl.name);
988 const sym_name = try std.fmt.allocPrint(lf.allocator, "_{s}", .{name});
989 defer lf.allocator.free(sym_name);
974990 const global_index = try macho_file.addUndefined(sym_name, .{ .add_got = true });
975991 return GenResult.mcv(.{ .load_got = link.File.MachO.global_symbol_bit | global_index });
976992 }
......@@ -980,110 +996,118 @@ fn genDeclRef(
980996 return GenResult.mcv(.{ .load_tlv = sym_index });
981997 }
982998 return GenResult.mcv(.{ .load_got = sym_index });
983 } else if (bin_file.cast(link.File.Coff)) |coff_file| {
999 } else if (lf.cast(link.File.Coff)) |coff_file| {
9841000 if (is_extern) {
985 const name = mod.intern_pool.stringToSlice(decl.name);
1001 const name = zcu.intern_pool.stringToSlice(decl.name);
9861002 // TODO audit this
987 const lib_name = if (decl.getOwnedVariable(mod)) |ov|
988 mod.intern_pool.stringToSliceUnwrap(ov.lib_name)
1003 const lib_name = if (decl.getOwnedVariable(zcu)) |ov|
1004 zcu.intern_pool.stringToSliceUnwrap(ov.lib_name)
9891005 else
9901006 null;
9911007 const global_index = try coff_file.getGlobalSymbol(name, lib_name);
992 try coff_file.need_got_table.put(bin_file.allocator, global_index, {}); // needs GOT
1008 try coff_file.need_got_table.put(lf.allocator, global_index, {}); // needs GOT
9931009 return GenResult.mcv(.{ .load_got = link.File.Coff.global_symbol_bit | global_index });
9941010 }
9951011 const atom_index = try coff_file.getOrCreateAtomForDecl(decl_index);
9961012 const sym_index = coff_file.getAtom(atom_index).getSymbolIndex().?;
9971013 return GenResult.mcv(.{ .load_got = sym_index });
998 } else if (bin_file.cast(link.File.Plan9)) |p9| {
1014 } else if (lf.cast(link.File.Plan9)) |p9| {
9991015 const atom_index = try p9.seeDecl(decl_index);
10001016 const atom = p9.getAtom(atom_index);
10011017 return GenResult.mcv(.{ .memory = atom.getOffsetTableAddress(p9) });
10021018 } else {
1003 return GenResult.fail(bin_file.allocator, src_loc, "TODO genDeclRef for target {}", .{target});
1019 return GenResult.fail(lf.allocator, src_loc, "TODO genDeclRef for target {}", .{target});
10041020 }
10051021}
10061022
10071023fn genUnnamedConst(
1008 bin_file: *link.File,
1024 lf: *link.File,
10091025 src_loc: Module.SrcLoc,
10101026 tv: TypedValue,
10111027 owner_decl_index: InternPool.DeclIndex,
10121028) CodeGenError!GenResult {
1013 const mod = bin_file.comp.module.?;
1014 log.debug("genUnnamedConst: ty = {}, val = {}", .{ tv.ty.fmt(mod), tv.val.fmtValue(tv.ty, mod) });
1029 const zcu = lf.comp.module.?;
1030 const gpa = lf.comp.gpa;
1031 log.debug("genUnnamedConst: ty = {}, val = {}", .{ tv.ty.fmt(zcu), tv.val.fmtValue(tv.ty, zcu) });
10151032
1016 const target = bin_file.options.target;
1017 const local_sym_index = bin_file.lowerUnnamedConst(tv, owner_decl_index) catch |err| {
1018 return GenResult.fail(bin_file.allocator, src_loc, "lowering unnamed constant failed: {s}", .{@errorName(err)});
1033 const local_sym_index = lf.lowerUnnamedConst(tv, owner_decl_index) catch |err| {
1034 return GenResult.fail(gpa, src_loc, "lowering unnamed constant failed: {s}", .{@errorName(err)});
10191035 };
1020 if (bin_file.cast(link.File.Elf)) |elf_file| {
1021 const local = elf_file.symbol(local_sym_index);
1022 return GenResult.mcv(.{ .load_symbol = local.esym_index });
1023 } else if (bin_file.cast(link.File.MachO)) |_| {
1024 return GenResult.mcv(.{ .load_direct = local_sym_index });
1025 } else if (bin_file.cast(link.File.Coff)) |_| {
1026 return GenResult.mcv(.{ .load_direct = local_sym_index });
1027 } else if (bin_file.cast(link.File.Plan9)) |_| {
1028 const atom_index = local_sym_index; // plan9 returns the atom_index
1029 return GenResult.mcv(.{ .load_direct = atom_index });
1030 } else {
1031 return GenResult.fail(bin_file.allocator, src_loc, "TODO genUnnamedConst for target {}", .{target});
1036 switch (lf.tag) {
1037 .elf => {
1038 const elf_file = lf.cast(link.File.Elf).?;
1039 const local = elf_file.symbol(local_sym_index);
1040 return GenResult.mcv(.{ .load_symbol = local.esym_index });
1041 },
1042 .macho, .coff => {
1043 return GenResult.mcv(.{ .load_direct = local_sym_index });
1044 },
1045 .plan9 => {
1046 const atom_index = local_sym_index; // plan9 returns the atom_index
1047 return GenResult.mcv(.{ .load_direct = atom_index });
1048 },
1049
1050 .c => return GenResult.fail(gpa, src_loc, "TODO genUnnamedConst for -ofmt=c", .{}),
1051 .wasm => return GenResult.fail(gpa, src_loc, "TODO genUnnamedConst for wasm", .{}),
1052 .spirv => return GenResult.fail(gpa, src_loc, "TODO genUnnamedConst for spirv", .{}),
1053 .nvptx => return GenResult.fail(gpa, src_loc, "TODO genUnnamedConst for nvptx", .{}),
10321054 }
10331055}
10341056
10351057pub fn genTypedValue(
1036 bin_file: *link.File,
1058 lf: *link.File,
10371059 src_loc: Module.SrcLoc,
10381060 arg_tv: TypedValue,
10391061 owner_decl_index: InternPool.DeclIndex,
10401062) CodeGenError!GenResult {
1041 const mod = bin_file.comp.module.?;
1063 const zcu = lf.comp.module.?;
10421064 const typed_value = arg_tv;
10431065
10441066 log.debug("genTypedValue: ty = {}, val = {}", .{
1045 typed_value.ty.fmt(mod),
1046 typed_value.val.fmtValue(typed_value.ty, mod),
1067 typed_value.ty.fmt(zcu),
1068 typed_value.val.fmtValue(typed_value.ty, zcu),
10471069 });
10481070
1049 if (typed_value.val.isUndef(mod))
1071 if (typed_value.val.isUndef(zcu))
10501072 return GenResult.mcv(.undef);
10511073
1052 const target = bin_file.options.target;
1074 const owner_decl = zcu.declPtr(owner_decl_index);
1075 const namespace = zcu.namespacePtr(owner_decl.src_namespace);
1076 const target = namespace.file_scope.mod.target;
10531077 const ptr_bits = target.ptrBitWidth();
10541078
1055 if (!typed_value.ty.isSlice(mod)) switch (mod.intern_pool.indexToKey(typed_value.val.toIntern())) {
1079 if (!typed_value.ty.isSlice(zcu)) switch (zcu.intern_pool.indexToKey(typed_value.val.toIntern())) {
10561080 .ptr => |ptr| switch (ptr.addr) {
1057 .decl => |decl| return genDeclRef(bin_file, src_loc, typed_value, decl),
1058 .mut_decl => |mut_decl| return genDeclRef(bin_file, src_loc, typed_value, mut_decl.decl),
1081 .decl => |decl| return genDeclRef(lf, src_loc, typed_value, decl),
1082 .mut_decl => |mut_decl| return genDeclRef(lf, src_loc, typed_value, mut_decl.decl),
10591083 else => {},
10601084 },
10611085 else => {},
10621086 };
10631087
1064 switch (typed_value.ty.zigTypeTag(mod)) {
1088 switch (typed_value.ty.zigTypeTag(zcu)) {
10651089 .Void => return GenResult.mcv(.none),
1066 .Pointer => switch (typed_value.ty.ptrSize(mod)) {
1090 .Pointer => switch (typed_value.ty.ptrSize(zcu)) {
10671091 .Slice => {},
10681092 else => switch (typed_value.val.toIntern()) {
10691093 .null_value => {
10701094 return GenResult.mcv(.{ .immediate = 0 });
10711095 },
10721096 .none => {},
1073 else => switch (mod.intern_pool.indexToKey(typed_value.val.toIntern())) {
1097 else => switch (zcu.intern_pool.indexToKey(typed_value.val.toIntern())) {
10741098 .int => {
1075 return GenResult.mcv(.{ .immediate = typed_value.val.toUnsignedInt(mod) });
1099 return GenResult.mcv(.{ .immediate = typed_value.val.toUnsignedInt(zcu) });
10761100 },
10771101 else => {},
10781102 },
10791103 },
10801104 },
10811105 .Int => {
1082 const info = typed_value.ty.intInfo(mod);
1106 const info = typed_value.ty.intInfo(zcu);
10831107 if (info.bits <= ptr_bits) {
10841108 const unsigned = switch (info.signedness) {
1085 .signed => @as(u64, @bitCast(typed_value.val.toSignedInt(mod))),
1086 .unsigned => typed_value.val.toUnsignedInt(mod),
1109 .signed => @as(u64, @bitCast(typed_value.val.toSignedInt(zcu))),
1110 .unsigned => typed_value.val.toUnsignedInt(zcu),
10871111 };
10881112 return GenResult.mcv(.{ .immediate = unsigned });
10891113 }
......@@ -1092,45 +1116,45 @@ pub fn genTypedValue(
10921116 return GenResult.mcv(.{ .immediate = @intFromBool(typed_value.val.toBool()) });
10931117 },
10941118 .Optional => {
1095 if (typed_value.ty.isPtrLikeOptional(mod)) {
1096 return genTypedValue(bin_file, src_loc, .{
1097 .ty = typed_value.ty.optionalChild(mod),
1098 .val = typed_value.val.optionalValue(mod) orelse return GenResult.mcv(.{ .immediate = 0 }),
1119 if (typed_value.ty.isPtrLikeOptional(zcu)) {
1120 return genTypedValue(lf, src_loc, .{
1121 .ty = typed_value.ty.optionalChild(zcu),
1122 .val = typed_value.val.optionalValue(zcu) orelse return GenResult.mcv(.{ .immediate = 0 }),
10991123 }, owner_decl_index);
1100 } else if (typed_value.ty.abiSize(mod) == 1) {
1101 return GenResult.mcv(.{ .immediate = @intFromBool(!typed_value.val.isNull(mod)) });
1124 } else if (typed_value.ty.abiSize(zcu) == 1) {
1125 return GenResult.mcv(.{ .immediate = @intFromBool(!typed_value.val.isNull(zcu)) });
11021126 }
11031127 },
11041128 .Enum => {
1105 const enum_tag = mod.intern_pool.indexToKey(typed_value.val.toIntern()).enum_tag;
1106 const int_tag_ty = mod.intern_pool.typeOf(enum_tag.int);
1107 return genTypedValue(bin_file, src_loc, .{
1129 const enum_tag = zcu.intern_pool.indexToKey(typed_value.val.toIntern()).enum_tag;
1130 const int_tag_ty = zcu.intern_pool.typeOf(enum_tag.int);
1131 return genTypedValue(lf, src_loc, .{
11081132 .ty = Type.fromInterned(int_tag_ty),
11091133 .val = Value.fromInterned(enum_tag.int),
11101134 }, owner_decl_index);
11111135 },
11121136 .ErrorSet => {
1113 const err_name = mod.intern_pool.indexToKey(typed_value.val.toIntern()).err.name;
1114 const error_index = mod.global_error_set.getIndex(err_name).?;
1137 const err_name = zcu.intern_pool.indexToKey(typed_value.val.toIntern()).err.name;
1138 const error_index = zcu.global_error_set.getIndex(err_name).?;
11151139 return GenResult.mcv(.{ .immediate = error_index });
11161140 },
11171141 .ErrorUnion => {
1118 const err_type = typed_value.ty.errorUnionSet(mod);
1119 const payload_type = typed_value.ty.errorUnionPayload(mod);
1120 if (!payload_type.hasRuntimeBitsIgnoreComptime(mod)) {
1142 const err_type = typed_value.ty.errorUnionSet(zcu);
1143 const payload_type = typed_value.ty.errorUnionPayload(zcu);
1144 if (!payload_type.hasRuntimeBitsIgnoreComptime(zcu)) {
11211145 // We use the error type directly as the type.
1122 const err_int_ty = try mod.errorIntType();
1123 switch (mod.intern_pool.indexToKey(typed_value.val.toIntern()).error_union.val) {
1124 .err_name => |err_name| return genTypedValue(bin_file, src_loc, .{
1146 const err_int_ty = try zcu.errorIntType();
1147 switch (zcu.intern_pool.indexToKey(typed_value.val.toIntern()).error_union.val) {
1148 .err_name => |err_name| return genTypedValue(lf, src_loc, .{
11251149 .ty = err_type,
1126 .val = Value.fromInterned((try mod.intern(.{ .err = .{
1150 .val = Value.fromInterned((try zcu.intern(.{ .err = .{
11271151 .ty = err_type.toIntern(),
11281152 .name = err_name,
11291153 } }))),
11301154 }, owner_decl_index),
1131 .payload => return genTypedValue(bin_file, src_loc, .{
1155 .payload => return genTypedValue(lf, src_loc, .{
11321156 .ty = err_int_ty,
1133 .val = try mod.intValue(err_int_ty, 0),
1157 .val = try zcu.intValue(err_int_ty, 0),
11341158 }, owner_decl_index),
11351159 }
11361160 }
......@@ -1148,7 +1172,7 @@ pub fn genTypedValue(
11481172 else => {},
11491173 }
11501174
1151 return genUnnamedConst(bin_file, src_loc, typed_value, owner_decl_index);
1175 return genUnnamedConst(lf, src_loc, typed_value, owner_decl_index);
11521176}
11531177
11541178pub fn errUnionPayloadOffset(payload_ty: Type, mod: *Module) u64 {
src/link/Dwarf.zig+34-22
......@@ -1192,7 +1192,7 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: InternPool.DeclInde
11921192
11931193pub fn commitDeclState(
11941194 self: *Dwarf,
1195 mod: *Module,
1195 zcu: *Module,
11961196 decl_index: InternPool.DeclIndex,
11971197 sym_addr: u64,
11981198 sym_size: u64,
......@@ -1202,15 +1202,17 @@ pub fn commitDeclState(
12021202 defer tracy.end();
12031203
12041204 const gpa = self.allocator;
1205 const decl = zcu.declPtr(decl_index);
1206 const ip = &zcu.intern_pool;
1207 const namespace = zcu.namespacePtr(decl.src_namespace);
1208 const target = namespace.file_scope.mod.target;
1209 const target_endian = target.cpu.arch.endian();
1210
12051211 var dbg_line_buffer = &decl_state.dbg_line;
12061212 var dbg_info_buffer = &decl_state.dbg_info;
1207 const decl = mod.declPtr(decl_index);
1208 const ip = &mod.intern_pool;
1209
1210 const target_endian = self.bin_file.options.target.cpu.arch.endian();
12111213
12121214 assert(decl.has_tv);
1213 switch (decl.ty.zigTypeTag(mod)) {
1215 switch (decl.ty.zigTypeTag(zcu)) {
12141216 .Fn => {
12151217 try decl_state.setInlineFunc(decl.val.toIntern());
12161218
......@@ -1409,18 +1411,18 @@ pub fn commitDeclState(
14091411 if (ip.isErrorSetType(ty.toIntern())) continue;
14101412
14111413 symbol.offset = @intCast(dbg_info_buffer.items.len);
1412 try decl_state.addDbgInfoType(mod, di_atom_index, ty);
1414 try decl_state.addDbgInfoType(zcu, di_atom_index, ty);
14131415 }
14141416 }
14151417
14161418 try self.updateDeclDebugInfoAllocation(di_atom_index, @intCast(dbg_info_buffer.items.len));
14171419
14181420 while (decl_state.abbrev_relocs.popOrNull()) |reloc| {
1419 if (reloc.target) |target| {
1420 const symbol = decl_state.abbrev_table.items[target];
1421 if (reloc.target) |reloc_target| {
1422 const symbol = decl_state.abbrev_table.items[reloc_target];
14211423 const ty = symbol.type;
14221424 if (ip.isErrorSetType(ty.toIntern())) {
1423 log.debug("resolving %{d} deferred until flush", .{target});
1425 log.debug("resolving %{d} deferred until flush", .{reloc_target});
14241426 try self.global_abbrev_relocs.append(gpa, .{
14251427 .target = null,
14261428 .offset = reloc.offset,
......@@ -1433,8 +1435,8 @@ pub fn commitDeclState(
14331435 log.debug("{x}: [() => {x}] (%{d}, '{}')", .{
14341436 reloc.offset,
14351437 value,
1436 target,
1437 ty.fmt(mod),
1438 reloc_target,
1439 ty.fmt(zcu),
14381440 });
14391441 mem.writeInt(
14401442 u32,
......@@ -1897,7 +1899,7 @@ fn dbgInfoHeaderBytes(self: *Dwarf) usize {
18971899 return 120;
18981900}
18991901
1900pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u64) !void {
1902pub fn writeDbgInfoHeader(self: *Dwarf, zcu: *Module, low_pc: u64, high_pc: u64) !void {
19011903 // If this value is null it means there is an error in the module;
19021904 // leave debug_info_header_dirty=true.
19031905 const first_dbg_info_off = self.getDebugInfoOff() orelse return;
......@@ -1908,7 +1910,9 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u
19081910 var di_buf = try std.ArrayList(u8).initCapacity(self.allocator, needed_bytes);
19091911 defer di_buf.deinit();
19101912
1911 const target_endian = self.bin_file.options.target.cpu.arch.endian();
1913 const comp = self.bin_file.comp;
1914 const target = comp.root_mod.resolved_target.result;
1915 const target_endian = target.cpu.arch.endian();
19121916 const init_len_size: usize = switch (self.format) {
19131917 .dwarf32 => 4,
19141918 .dwarf64 => 12,
......@@ -1931,9 +1935,9 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u
19311935 di_buf.appendAssumeCapacity(self.ptrWidthBytes()); // address size
19321936
19331937 // Write the form for the compile unit, which must match the abbrev table above.
1934 const name_strp = try self.strtab.insert(self.allocator, module.root_mod.root_src_path);
1938 const name_strp = try self.strtab.insert(self.allocator, zcu.root_mod.root_src_path);
19351939 var compile_unit_dir_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined;
1936 const compile_unit_dir = resolveCompilationDir(module, &compile_unit_dir_buffer);
1940 const compile_unit_dir = resolveCompilationDir(zcu, &compile_unit_dir_buffer);
19371941 const comp_dir_strp = try self.strtab.insert(self.allocator, compile_unit_dir);
19381942 const producer_strp = try self.strtab.insert(self.allocator, link.producer_string);
19391943
......@@ -1997,7 +2001,9 @@ fn resolveCompilationDir(module: *Module, buffer: *[std.fs.MAX_PATH_BYTES]u8) []
19972001}
19982002
19992003fn writeAddrAssumeCapacity(self: *Dwarf, buf: *std.ArrayList(u8), addr: u64) void {
2000 const target_endian = self.bin_file.options.target.cpu.arch.endian();
2004 const comp = self.bin_file.comp;
2005 const target = comp.root_mod.resolved_target.result;
2006 const target_endian = target.cpu.arch.endian();
20012007 switch (self.ptr_width) {
20022008 .p32 => mem.writeInt(u32, buf.addManyAsArrayAssumeCapacity(4), @intCast(addr), target_endian),
20032009 .p64 => mem.writeInt(u64, buf.addManyAsArrayAssumeCapacity(8), addr, target_endian),
......@@ -2005,7 +2011,9 @@ fn writeAddrAssumeCapacity(self: *Dwarf, buf: *std.ArrayList(u8), addr: u64) voi
20052011}
20062012
20072013fn writeOffsetAssumeCapacity(self: *Dwarf, buf: *std.ArrayList(u8), off: u64) void {
2008 const target_endian = self.bin_file.options.target.cpu.arch.endian();
2014 const comp = self.bin_file.comp;
2015 const target = comp.root_mod.resolved_target.result;
2016 const target_endian = target.cpu.arch.endian();
20092017 switch (self.format) {
20102018 .dwarf32 => mem.writeInt(u32, buf.addManyAsArrayAssumeCapacity(4), @intCast(off), target_endian),
20112019 .dwarf64 => mem.writeInt(u64, buf.addManyAsArrayAssumeCapacity(8), off, target_endian),
......@@ -2227,7 +2235,9 @@ fn writeDbgInfoNopsToArrayList(
22272235}
22282236
22292237pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void {
2230 const target_endian = self.bin_file.options.target.cpu.arch.endian();
2238 const comp = self.bin_file.comp;
2239 const target = comp.root_mod.resolved_target.result;
2240 const target_endian = target.cpu.arch.endian();
22312241 const ptr_width_bytes = self.ptrWidthBytes();
22322242
22332243 // Enough for all the data without resizing. When support for more compilation units
......@@ -2299,9 +2309,10 @@ pub fn writeDbgAranges(self: *Dwarf, addr: u64, size: u64) !void {
22992309}
23002310
23012311pub fn writeDbgLineHeader(self: *Dwarf) !void {
2312 const comp = self.bin_file.comp;
23022313 const gpa = self.allocator;
2303
2304 const target_endian = self.bin_file.options.target.cpu.arch.endian();
2314 const target = comp.root_mod.resolved_target.result;
2315 const target_endian = target.cpu.arch.endian();
23052316 const init_len_size: usize = switch (self.format) {
23062317 .dwarf32 => 4,
23072318 .dwarf64 => 12,
......@@ -2565,7 +2576,8 @@ fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) {
25652576}
25662577
25672578pub fn flushModule(self: *Dwarf, module: *Module) !void {
2568 const target = self.bin_file.options.target;
2579 const comp = self.bin_file.comp;
2580 const target = comp.root_mod.resolved_target.result;
25692581
25702582 if (self.global_abbrev_relocs.items.len > 0) {
25712583 const gpa = self.allocator;