| author | |
| committer | |
| log | bc4d2b646d5d09ecb86a3806886fed37e522fdc9 |
| tree | b476088e3785c0f2820a5fc1fee3bc880310bc71 |
| parent | 1642c003b4bab4a53b6094b42d10f6934896801e |
10 files changed, 326 insertions(+), 283 deletions(-)
src/Compilation.zig+13-14| ... | ... | @@ -1915,9 +1915,8 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation { |
| 1915 | 1915 | } |
| 1916 | 1916 | |
| 1917 | 1917 | pub 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(); | |
| 1921 | 1920 | |
| 1922 | 1921 | const gpa = self.gpa; |
| 1923 | 1922 | self.work_queue.deinit(); |
| ... | ... | @@ -2059,9 +2058,9 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void |
| 2059 | 2058 | |
| 2060 | 2059 | // If using the whole caching strategy, we check for *everything* up front, including |
| 2061 | 2060 | // C source files. |
| 2062 | if (comp.bin_file.options.cache_mode == .whole) { | |
| 2061 | if (comp.cache_mode == .whole) { | |
| 2063 | 2062 | // 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(); | |
| 2065 | 2064 | |
| 2066 | 2065 | man = comp.cache_parent.obtain(); |
| 2067 | 2066 | comp.whole_cache_manifest = &man; |
| ... | ... | @@ -5948,14 +5947,14 @@ pub fn get_libc_crt_file(comp: *Compilation, arena: Allocator, basename: []const |
| 5948 | 5947 | } |
| 5949 | 5948 | |
| 5950 | 5949 | fn 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) { | |
| 5952 | 5951 | .Obj => false, |
| 5953 | .Lib => comp.bin_file.options.link_mode == .Dynamic, | |
| 5952 | .Lib => comp.config.link_mode == .Dynamic, | |
| 5954 | 5953 | .Exe => true, |
| 5955 | 5954 | }; |
| 5955 | const ofmt = comp.root_mod.resolved_target.result.ofmt; | |
| 5956 | 5956 | 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; | |
| 5959 | 5958 | } |
| 5960 | 5959 | |
| 5961 | 5960 | fn wantBuildGLibCFromSource(comp: Compilation) bool { |
| ... | ... | @@ -5977,13 +5976,13 @@ fn wantBuildMinGWFromSource(comp: Compilation) bool { |
| 5977 | 5976 | } |
| 5978 | 5977 | |
| 5979 | 5978 | fn 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) { | |
| 5981 | 5980 | .Obj => false, |
| 5982 | .Lib => comp.bin_file.options.link_mode == .Dynamic, | |
| 5981 | .Lib => comp.config.link_mode == .Dynamic, | |
| 5983 | 5982 | .Exe => true, |
| 5984 | 5983 | }; |
| 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; | |
| 5987 | 5986 | } |
| 5988 | 5987 | |
| 5989 | 5988 | fn setAllocFailure(comp: *Compilation) void { |
| ... | ... | @@ -6112,7 +6111,7 @@ fn canBuildZigLibC(target: std.Target, use_llvm: bool) bool { |
| 6112 | 6111 | } |
| 6113 | 6112 | |
| 6114 | 6113 | pub fn getZigBackend(comp: Compilation) std.builtin.CompilerBackend { |
| 6115 | const target = comp.bin_file.options.target; | |
| 6114 | const target = comp.root_mod.resolved_target.result; | |
| 6116 | 6115 | return target_util.zigBackend(target, comp.bin_file.options.use_llvm); |
| 6117 | 6116 | } |
| 6118 | 6117 |
src/Module.zig+24-18| ... | ... | @@ -623,7 +623,8 @@ pub const Decl = struct { |
| 623 | 623 | // Sanitize the name for nvptx which is more restrictive. |
| 624 | 624 | // TODO This should be handled by the backend, not the frontend. Have a |
| 625 | 625 | // 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()) { | |
| 627 | 628 | for (ip.string_bytes.items[start..]) |*byte| switch (byte.*) { |
| 628 | 629 | '{', '}', '*', '[', ']', '(', ')', ',', ' ', '\'' => byte.* = '_', |
| 629 | 630 | else => {}, |
| ... | ... | @@ -4873,12 +4874,18 @@ pub fn errNoteNonLazy( |
| 4873 | 4874 | }; |
| 4874 | 4875 | } |
| 4875 | 4876 | |
| 4876 | pub 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. | |
| 4880 | pub fn getTarget(zcu: Module) Target { | |
| 4881 | return zcu.root_mod.resolved_target.result; | |
| 4878 | 4882 | } |
| 4879 | 4883 | |
| 4880 | pub 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. | |
| 4887 | pub fn optimizeMode(zcu: Module) std.builtin.OptimizeMode { | |
| 4888 | return zcu.root_mod.optimize_mode; | |
| 4882 | 4889 | } |
| 4883 | 4890 | |
| 4884 | 4891 | fn lockAndClearFileCompileError(mod: *Module, file: *File) void { |
| ... | ... | @@ -5620,20 +5627,19 @@ pub const Feature = enum { |
| 5620 | 5627 | safety_checked_instructions, |
| 5621 | 5628 | }; |
| 5622 | 5629 | |
| 5623 | pub fn backendSupportsFeature(mod: Module, feature: Feature) bool { | |
| 5630 | pub 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; | |
| 5624 | 5634 | 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, | |
| 5637 | 5643 | }; |
| 5638 | 5644 | } |
| 5639 | 5645 |
src/arch/aarch64/CodeGen.zig+22-23| ... | ... | @@ -329,7 +329,7 @@ const BigTomb = struct { |
| 329 | 329 | const Self = @This(); |
| 330 | 330 | |
| 331 | 331 | pub fn generate( |
| 332 | bin_file: *link.File, | |
| 332 | lf: *link.File, | |
| 333 | 333 | src_loc: Module.SrcLoc, |
| 334 | 334 | func_index: InternPool.Index, |
| 335 | 335 | air: Air, |
| ... | ... | @@ -337,31 +337,30 @@ pub fn generate( |
| 337 | 337 | code: *std.ArrayList(u8), |
| 338 | 338 | debug_output: DebugInfoOutput, |
| 339 | 339 | ) 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); | |
| 347 | 344 | assert(fn_owner_decl.has_tv); |
| 348 | 345 | 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; | |
| 349 | 348 | |
| 350 | var branch_stack = std.ArrayList(Branch).init(bin_file.allocator); | |
| 349 | var branch_stack = std.ArrayList(Branch).init(gpa); | |
| 351 | 350 | defer { |
| 352 | 351 | assert(branch_stack.items.len == 1); |
| 353 | branch_stack.items[0].deinit(bin_file.allocator); | |
| 352 | branch_stack.items[0].deinit(gpa); | |
| 354 | 353 | branch_stack.deinit(); |
| 355 | 354 | } |
| 356 | 355 | try branch_stack.append(.{}); |
| 357 | 356 | |
| 358 | 357 | var function = Self{ |
| 359 | .gpa = bin_file.allocator, | |
| 358 | .gpa = gpa, | |
| 360 | 359 | .air = air, |
| 361 | 360 | .liveness = liveness, |
| 362 | 361 | .debug_output = debug_output, |
| 363 | .target = &bin_file.options.target, | |
| 364 | .bin_file = bin_file, | |
| 362 | .target = target, | |
| 363 | .bin_file = lf, | |
| 365 | 364 | .func_index = func_index, |
| 366 | 365 | .owner_decl = func.owner_decl, |
| 367 | 366 | .err_msg = null, |
| ... | ... | @@ -375,15 +374,15 @@ pub fn generate( |
| 375 | 374 | .end_di_line = func.rbrace_line, |
| 376 | 375 | .end_di_column = func.rbrace_column, |
| 377 | 376 | }; |
| 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); | |
| 382 | 381 | |
| 383 | 382 | var call_info = function.resolveCallingConventionValues(fn_type) catch |err| switch (err) { |
| 384 | 383 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, |
| 385 | 384 | 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.", .{}), | |
| 387 | 386 | }, |
| 388 | 387 | else => |e| return e, |
| 389 | 388 | }; |
| ... | ... | @@ -397,7 +396,7 @@ pub fn generate( |
| 397 | 396 | function.gen() catch |err| switch (err) { |
| 398 | 397 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, |
| 399 | 398 | 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.", .{}), | |
| 401 | 400 | }, |
| 402 | 401 | else => |e| return e, |
| 403 | 402 | }; |
| ... | ... | @@ -408,15 +407,15 @@ pub fn generate( |
| 408 | 407 | |
| 409 | 408 | var mir = Mir{ |
| 410 | 409 | .instructions = function.mir_instructions.toOwnedSlice(), |
| 411 | .extra = try function.mir_extra.toOwnedSlice(bin_file.allocator), | |
| 410 | .extra = try function.mir_extra.toOwnedSlice(gpa), | |
| 412 | 411 | }; |
| 413 | defer mir.deinit(bin_file.allocator); | |
| 412 | defer mir.deinit(gpa); | |
| 414 | 413 | |
| 415 | 414 | var emit = Emit{ |
| 416 | 415 | .mir = mir, |
| 417 | .bin_file = bin_file, | |
| 416 | .bin_file = lf, | |
| 418 | 417 | .debug_output = debug_output, |
| 419 | .target = &bin_file.options.target, | |
| 418 | .target = target, | |
| 420 | 419 | .src_loc = src_loc, |
| 421 | 420 | .code = code, |
| 422 | 421 | .prev_di_pc = 0, |
src/arch/arm/CodeGen.zig+23-24| ... | ... | @@ -336,7 +336,7 @@ const DbgInfoReloc = struct { |
| 336 | 336 | const Self = @This(); |
| 337 | 337 | |
| 338 | 338 | pub fn generate( |
| 339 | bin_file: *link.File, | |
| 339 | lf: *link.File, | |
| 340 | 340 | src_loc: Module.SrcLoc, |
| 341 | 341 | func_index: InternPool.Index, |
| 342 | 342 | air: Air, |
| ... | ... | @@ -344,30 +344,29 @@ pub fn generate( |
| 344 | 344 | code: *std.ArrayList(u8), |
| 345 | 345 | debug_output: DebugInfoOutput, |
| 346 | 346 | ) 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); | |
| 354 | 351 | assert(fn_owner_decl.has_tv); |
| 355 | 352 | 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; | |
| 356 | 355 | |
| 357 | var branch_stack = std.ArrayList(Branch).init(bin_file.allocator); | |
| 356 | var branch_stack = std.ArrayList(Branch).init(gpa); | |
| 358 | 357 | defer { |
| 359 | 358 | assert(branch_stack.items.len == 1); |
| 360 | branch_stack.items[0].deinit(bin_file.allocator); | |
| 359 | branch_stack.items[0].deinit(gpa); | |
| 361 | 360 | branch_stack.deinit(); |
| 362 | 361 | } |
| 363 | 362 | try branch_stack.append(.{}); |
| 364 | 363 | |
| 365 | var function = Self{ | |
| 366 | .gpa = bin_file.allocator, | |
| 364 | var function: Self = .{ | |
| 365 | .gpa = gpa, | |
| 367 | 366 | .air = air, |
| 368 | 367 | .liveness = liveness, |
| 369 | .target = &bin_file.options.target, | |
| 370 | .bin_file = bin_file, | |
| 368 | .target = target, | |
| 369 | .bin_file = lf, | |
| 371 | 370 | .debug_output = debug_output, |
| 372 | 371 | .func_index = func_index, |
| 373 | 372 | .err_msg = null, |
| ... | ... | @@ -381,15 +380,15 @@ pub fn generate( |
| 381 | 380 | .end_di_line = func.rbrace_line, |
| 382 | 381 | .end_di_column = func.rbrace_column, |
| 383 | 382 | }; |
| 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); | |
| 388 | 387 | |
| 389 | 388 | var call_info = function.resolveCallingConventionValues(fn_type) catch |err| switch (err) { |
| 390 | 389 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, |
| 391 | 390 | 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.", .{}), | |
| 393 | 392 | }, |
| 394 | 393 | else => |e| return e, |
| 395 | 394 | }; |
| ... | ... | @@ -403,7 +402,7 @@ pub fn generate( |
| 403 | 402 | function.gen() catch |err| switch (err) { |
| 404 | 403 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, |
| 405 | 404 | 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.", .{}), | |
| 407 | 406 | }, |
| 408 | 407 | else => |e| return e, |
| 409 | 408 | }; |
| ... | ... | @@ -414,15 +413,15 @@ pub fn generate( |
| 414 | 413 | |
| 415 | 414 | var mir = Mir{ |
| 416 | 415 | .instructions = function.mir_instructions.toOwnedSlice(), |
| 417 | .extra = try function.mir_extra.toOwnedSlice(bin_file.allocator), | |
| 416 | .extra = try function.mir_extra.toOwnedSlice(gpa), | |
| 418 | 417 | }; |
| 419 | defer mir.deinit(bin_file.allocator); | |
| 418 | defer mir.deinit(gpa); | |
| 420 | 419 | |
| 421 | 420 | var emit = Emit{ |
| 422 | 421 | .mir = mir, |
| 423 | .bin_file = bin_file, | |
| 422 | .bin_file = lf, | |
| 424 | 423 | .debug_output = debug_output, |
| 425 | .target = &bin_file.options.target, | |
| 424 | .target = target, | |
| 426 | 425 | .src_loc = src_loc, |
| 427 | 426 | .code = code, |
| 428 | 427 | .prev_di_pc = 0, |
src/arch/riscv64/CodeGen.zig+21-22| ... | ... | @@ -217,7 +217,7 @@ const BigTomb = struct { |
| 217 | 217 | const Self = @This(); |
| 218 | 218 | |
| 219 | 219 | pub fn generate( |
| 220 | bin_file: *link.File, | |
| 220 | lf: *link.File, | |
| 221 | 221 | src_loc: Module.SrcLoc, |
| 222 | 222 | func_index: InternPool.Index, |
| 223 | 223 | air: Air, |
| ... | ... | @@ -225,30 +225,29 @@ pub fn generate( |
| 225 | 225 | code: *std.ArrayList(u8), |
| 226 | 226 | debug_output: DebugInfoOutput, |
| 227 | 227 | ) 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); | |
| 235 | 232 | assert(fn_owner_decl.has_tv); |
| 236 | 233 | 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; | |
| 237 | 236 | |
| 238 | var branch_stack = std.ArrayList(Branch).init(bin_file.allocator); | |
| 237 | var branch_stack = std.ArrayList(Branch).init(gpa); | |
| 239 | 238 | defer { |
| 240 | 239 | assert(branch_stack.items.len == 1); |
| 241 | branch_stack.items[0].deinit(bin_file.allocator); | |
| 240 | branch_stack.items[0].deinit(gpa); | |
| 242 | 241 | branch_stack.deinit(); |
| 243 | 242 | } |
| 244 | 243 | try branch_stack.append(.{}); |
| 245 | 244 | |
| 246 | 245 | var function = Self{ |
| 247 | .gpa = bin_file.allocator, | |
| 246 | .gpa = gpa, | |
| 248 | 247 | .air = air, |
| 249 | 248 | .liveness = liveness, |
| 250 | .target = &bin_file.options.target, | |
| 251 | .bin_file = bin_file, | |
| 249 | .target = target, | |
| 250 | .bin_file = lf, | |
| 252 | 251 | .func_index = func_index, |
| 253 | 252 | .code = code, |
| 254 | 253 | .debug_output = debug_output, |
| ... | ... | @@ -263,14 +262,14 @@ pub fn generate( |
| 263 | 262 | .end_di_line = func.rbrace_line, |
| 264 | 263 | .end_di_column = func.rbrace_column, |
| 265 | 264 | }; |
| 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); | |
| 269 | 268 | |
| 270 | 269 | var call_info = function.resolveCallingConventionValues(fn_type) catch |err| switch (err) { |
| 271 | 270 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, |
| 272 | 271 | 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.", .{}), | |
| 274 | 273 | }, |
| 275 | 274 | else => |e| return e, |
| 276 | 275 | }; |
| ... | ... | @@ -284,22 +283,22 @@ pub fn generate( |
| 284 | 283 | function.gen() catch |err| switch (err) { |
| 285 | 284 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, |
| 286 | 285 | 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.", .{}), | |
| 288 | 287 | }, |
| 289 | 288 | else => |e| return e, |
| 290 | 289 | }; |
| 291 | 290 | |
| 292 | 291 | var mir = Mir{ |
| 293 | 292 | .instructions = function.mir_instructions.toOwnedSlice(), |
| 294 | .extra = try function.mir_extra.toOwnedSlice(bin_file.allocator), | |
| 293 | .extra = try function.mir_extra.toOwnedSlice(gpa), | |
| 295 | 294 | }; |
| 296 | defer mir.deinit(bin_file.allocator); | |
| 295 | defer mir.deinit(gpa); | |
| 297 | 296 | |
| 298 | 297 | var emit = Emit{ |
| 299 | 298 | .mir = mir, |
| 300 | .bin_file = bin_file, | |
| 299 | .bin_file = lf, | |
| 301 | 300 | .debug_output = debug_output, |
| 302 | .target = &bin_file.options.target, | |
| 301 | .target = target, | |
| 303 | 302 | .src_loc = src_loc, |
| 304 | 303 | .code = code, |
| 305 | 304 | .prev_di_pc = 0, |
src/arch/sparc64/CodeGen.zig+21-22| ... | ... | @@ -260,7 +260,7 @@ const BigTomb = struct { |
| 260 | 260 | }; |
| 261 | 261 | |
| 262 | 262 | pub fn generate( |
| 263 | bin_file: *link.File, | |
| 263 | lf: *link.File, | |
| 264 | 264 | src_loc: Module.SrcLoc, |
| 265 | 265 | func_index: InternPool.Index, |
| 266 | 266 | air: Air, |
| ... | ... | @@ -268,31 +268,30 @@ pub fn generate( |
| 268 | 268 | code: *std.ArrayList(u8), |
| 269 | 269 | debug_output: DebugInfoOutput, |
| 270 | 270 | ) 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); | |
| 278 | 275 | assert(fn_owner_decl.has_tv); |
| 279 | 276 | 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; | |
| 280 | 279 | |
| 281 | var branch_stack = std.ArrayList(Branch).init(bin_file.allocator); | |
| 280 | var branch_stack = std.ArrayList(Branch).init(gpa); | |
| 282 | 281 | defer { |
| 283 | 282 | assert(branch_stack.items.len == 1); |
| 284 | branch_stack.items[0].deinit(bin_file.allocator); | |
| 283 | branch_stack.items[0].deinit(gpa); | |
| 285 | 284 | branch_stack.deinit(); |
| 286 | 285 | } |
| 287 | 286 | try branch_stack.append(.{}); |
| 288 | 287 | |
| 289 | 288 | var function = Self{ |
| 290 | .gpa = bin_file.allocator, | |
| 289 | .gpa = gpa, | |
| 291 | 290 | .air = air, |
| 292 | 291 | .liveness = liveness, |
| 293 | .target = &bin_file.options.target, | |
| 292 | .target = target, | |
| 294 | 293 | .func_index = func_index, |
| 295 | .bin_file = bin_file, | |
| 294 | .bin_file = lf, | |
| 296 | 295 | .code = code, |
| 297 | 296 | .debug_output = debug_output, |
| 298 | 297 | .err_msg = null, |
| ... | ... | @@ -306,14 +305,14 @@ pub fn generate( |
| 306 | 305 | .end_di_line = func.rbrace_line, |
| 307 | 306 | .end_di_column = func.rbrace_column, |
| 308 | 307 | }; |
| 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); | |
| 312 | 311 | |
| 313 | 312 | var call_info = function.resolveCallingConventionValues(fn_type, .callee) catch |err| switch (err) { |
| 314 | 313 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, |
| 315 | 314 | 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.", .{}), | |
| 317 | 316 | }, |
| 318 | 317 | else => |e| return e, |
| 319 | 318 | }; |
| ... | ... | @@ -327,22 +326,22 @@ pub fn generate( |
| 327 | 326 | function.gen() catch |err| switch (err) { |
| 328 | 327 | error.CodegenFail => return Result{ .fail = function.err_msg.? }, |
| 329 | 328 | 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.", .{}), | |
| 331 | 330 | }, |
| 332 | 331 | else => |e| return e, |
| 333 | 332 | }; |
| 334 | 333 | |
| 335 | 334 | var mir = Mir{ |
| 336 | 335 | .instructions = function.mir_instructions.toOwnedSlice(), |
| 337 | .extra = try function.mir_extra.toOwnedSlice(bin_file.allocator), | |
| 336 | .extra = try function.mir_extra.toOwnedSlice(gpa), | |
| 338 | 337 | }; |
| 339 | defer mir.deinit(bin_file.allocator); | |
| 338 | defer mir.deinit(gpa); | |
| 340 | 339 | |
| 341 | 340 | var emit = Emit{ |
| 342 | 341 | .mir = mir, |
| 343 | .bin_file = bin_file, | |
| 342 | .bin_file = lf, | |
| 344 | 343 | .debug_output = debug_output, |
| 345 | .target = &bin_file.options.target, | |
| 344 | .target = target, | |
| 346 | 345 | .src_loc = src_loc, |
| 347 | 346 | .code = code, |
| 348 | 347 | .prev_di_pc = 0, |
src/arch/wasm/CodeGen.zig+5-2| ... | ... | @@ -1212,16 +1212,19 @@ pub fn generate( |
| 1212 | 1212 | _ = src_loc; |
| 1213 | 1213 | const mod = bin_file.comp.module.?; |
| 1214 | 1214 | 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; | |
| 1215 | 1218 | var code_gen: CodeGen = .{ |
| 1216 | 1219 | .gpa = bin_file.allocator, |
| 1217 | 1220 | .air = air, |
| 1218 | 1221 | .liveness = liveness, |
| 1219 | 1222 | .code = code, |
| 1220 | 1223 | .decl_index = func.owner_decl, |
| 1221 | .decl = mod.declPtr(func.owner_decl), | |
| 1224 | .decl = decl, | |
| 1222 | 1225 | .err_msg = undefined, |
| 1223 | 1226 | .locals = .{}, |
| 1224 | .target = bin_file.options.target, | |
| 1227 | .target = target, | |
| 1225 | 1228 | .bin_file = bin_file.cast(link.File.Wasm).?, |
| 1226 | 1229 | .debug_output = debug_output, |
| 1227 | 1230 | .func_index = func_index, |
src/arch/x86_64/CodeGen.zig+10-7| ... | ... | @@ -795,22 +795,20 @@ pub fn generate( |
| 795 | 795 | code: *std.ArrayList(u8), |
| 796 | 796 | debug_output: DebugInfoOutput, |
| 797 | 797 | ) 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 | ||
| 802 | 798 | const mod = bin_file.comp.module.?; |
| 803 | 799 | const func = mod.funcInfo(func_index); |
| 804 | 800 | const fn_owner_decl = mod.declPtr(func.owner_decl); |
| 805 | 801 | assert(fn_owner_decl.has_tv); |
| 806 | 802 | 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; | |
| 807 | 805 | |
| 808 | 806 | const gpa = bin_file.allocator; |
| 809 | 807 | var function = Self{ |
| 810 | 808 | .gpa = gpa, |
| 811 | 809 | .air = air, |
| 812 | 810 | .liveness = liveness, |
| 813 | .target = &bin_file.options.target, | |
| 811 | .target = target, | |
| 814 | 812 | .bin_file = bin_file, |
| 815 | 813 | .debug_output = debug_output, |
| 816 | 814 | .owner = .{ .func_index = func_index }, |
| ... | ... | @@ -882,7 +880,7 @@ pub fn generate( |
| 882 | 880 | .size = Type.usize.abiSize(mod), |
| 883 | 881 | .alignment = Alignment.min( |
| 884 | 882 | call_info.stack_align, |
| 885 | Alignment.fromNonzeroByteUnits(bin_file.options.target.stackAlignment()), | |
| 883 | Alignment.fromNonzeroByteUnits(target.stackAlignment()), | |
| 886 | 884 | ), |
| 887 | 885 | })); |
| 888 | 886 | function.frame_allocs.set( |
| ... | ... | @@ -967,11 +965,16 @@ pub fn generateLazy( |
| 967 | 965 | debug_output: DebugInfoOutput, |
| 968 | 966 | ) CodeGenError!Result { |
| 969 | 967 | 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; | |
| 970 | 973 | var function = Self{ |
| 971 | 974 | .gpa = gpa, |
| 972 | 975 | .air = undefined, |
| 973 | 976 | .liveness = undefined, |
| 974 | .target = &bin_file.options.target, | |
| 977 | .target = target, | |
| 975 | 978 | .bin_file = bin_file, |
| 976 | 979 | .debug_output = debug_output, |
| 977 | 980 | .owner = .{ .lazy_sym = lazy_sym }, |
src/codegen.zig+153-129| ... | ... | @@ -45,7 +45,7 @@ pub const DebugInfoOutput = union(enum) { |
| 45 | 45 | }; |
| 46 | 46 | |
| 47 | 47 | pub fn generateFunction( |
| 48 | bin_file: *link.File, | |
| 48 | lf: *link.File, | |
| 49 | 49 | src_loc: Module.SrcLoc, |
| 50 | 50 | func_index: InternPool.Index, |
| 51 | 51 | air: Air, |
| ... | ... | @@ -53,33 +53,43 @@ pub fn generateFunction( |
| 53 | 53 | code: *std.ArrayList(u8), |
| 54 | 54 | debug_output: DebugInfoOutput, |
| 55 | 55 | ) 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) { | |
| 57 | 62 | .arm, |
| 58 | 63 | .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), | |
| 60 | 65 | .aarch64, |
| 61 | 66 | .aarch64_be, |
| 62 | 67 | .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), | |
| 67 | 72 | .wasm32, |
| 68 | 73 | .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), | |
| 70 | 75 | else => unreachable, |
| 71 | 76 | } |
| 72 | 77 | } |
| 73 | 78 | |
| 74 | 79 | pub fn generateLazyFunction( |
| 75 | bin_file: *link.File, | |
| 80 | lf: *link.File, | |
| 76 | 81 | src_loc: Module.SrcLoc, |
| 77 | 82 | lazy_sym: link.File.LazySymbol, |
| 78 | 83 | code: *std.ArrayList(u8), |
| 79 | 84 | debug_output: DebugInfoOutput, |
| 80 | 85 | ) 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), | |
| 83 | 93 | else => unreachable, |
| 84 | 94 | } |
| 85 | 95 | } |
| ... | ... | @@ -107,13 +117,16 @@ pub fn generateLazySymbol( |
| 107 | 117 | const tracy = trace(@src()); |
| 108 | 118 | defer tracy.end(); |
| 109 | 119 | |
| 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; | |
| 111 | 125 | const endian = target.cpu.arch.endian(); |
| 112 | 126 | |
| 113 | const mod = bin_file.comp.module.?; | |
| 114 | 127 | log.debug("generateLazySymbol: kind = {s}, ty = {}", .{ |
| 115 | 128 | @tagName(lazy_sym.kind), |
| 116 | lazy_sym.ty.fmt(mod), | |
| 129 | lazy_sym.ty.fmt(zcu), | |
| 117 | 130 | }); |
| 118 | 131 | |
| 119 | 132 | if (lazy_sym.kind == .code) { |
| ... | ... | @@ -121,14 +134,14 @@ pub fn generateLazySymbol( |
| 121 | 134 | return generateLazyFunction(bin_file, src_loc, lazy_sym, code, debug_output); |
| 122 | 135 | } |
| 123 | 136 | |
| 124 | if (lazy_sym.ty.isAnyError(mod)) { | |
| 137 | if (lazy_sym.ty.isAnyError(zcu)) { | |
| 125 | 138 | alignment.* = .@"4"; |
| 126 | const err_names = mod.global_error_set.keys(); | |
| 139 | const err_names = zcu.global_error_set.keys(); | |
| 127 | 140 | mem.writeInt(u32, try code.addManyAsArray(4), @as(u32, @intCast(err_names.len)), endian); |
| 128 | 141 | var offset = code.items.len; |
| 129 | 142 | try code.resize((1 + err_names.len + 1) * 4); |
| 130 | 143 | 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); | |
| 132 | 145 | mem.writeInt(u32, code.items[offset..][0..4], @as(u32, @intCast(code.items.len)), endian); |
| 133 | 146 | offset += 4; |
| 134 | 147 | try code.ensureUnusedCapacity(err_name.len + 1); |
| ... | ... | @@ -137,10 +150,10 @@ pub fn generateLazySymbol( |
| 137 | 150 | } |
| 138 | 151 | mem.writeInt(u32, code.items[offset..][0..4], @as(u32, @intCast(code.items.len)), endian); |
| 139 | 152 | return Result.ok; |
| 140 | } else if (lazy_sym.ty.zigTypeTag(mod) == .Enum) { | |
| 153 | } else if (lazy_sym.ty.zigTypeTag(zcu) == .Enum) { | |
| 141 | 154 | 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); | |
| 144 | 157 | try code.ensureUnusedCapacity(tag_name.len + 1); |
| 145 | 158 | code.appendSliceAssumeCapacity(tag_name); |
| 146 | 159 | code.appendAssumeCapacity(0); |
| ... | ... | @@ -150,7 +163,7 @@ pub fn generateLazySymbol( |
| 150 | 163 | bin_file.allocator, |
| 151 | 164 | src_loc, |
| 152 | 165 | "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) }, | |
| 154 | 167 | ) }; |
| 155 | 168 | } |
| 156 | 169 | |
| ... | ... | @@ -757,7 +770,7 @@ const RelocInfo = struct { |
| 757 | 770 | }; |
| 758 | 771 | |
| 759 | 772 | fn lowerAnonDeclRef( |
| 760 | bin_file: *link.File, | |
| 773 | lf: *link.File, | |
| 761 | 774 | src_loc: Module.SrcLoc, |
| 762 | 775 | anon_decl: InternPool.Key.Ptr.Addr.AnonDecl, |
| 763 | 776 | code: *std.ArrayList(u8), |
| ... | ... | @@ -765,27 +778,27 @@ fn lowerAnonDeclRef( |
| 765 | 778 | reloc_info: RelocInfo, |
| 766 | 779 | ) CodeGenError!Result { |
| 767 | 780 | _ = 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; | |
| 770 | 783 | |
| 771 | 784 | const ptr_width_bytes = @divExact(target.ptrBitWidth(), 8); |
| 772 | 785 | 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)) { | |
| 777 | 790 | try code.appendNTimes(0xaa, ptr_width_bytes); |
| 778 | 791 | return Result.ok; |
| 779 | 792 | } |
| 780 | 793 | |
| 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); | |
| 783 | 796 | switch (res) { |
| 784 | 797 | .ok => {}, |
| 785 | 798 | .fail => |em| return .{ .fail = em }, |
| 786 | 799 | } |
| 787 | 800 | |
| 788 | const vaddr = try bin_file.getAnonDeclVAddr(decl_val, .{ | |
| 801 | const vaddr = try lf.getAnonDeclVAddr(decl_val, .{ | |
| 789 | 802 | .parent_atom_index = reloc_info.parent_atom_index, |
| 790 | 803 | .offset = code.items.len, |
| 791 | 804 | .addend = reloc_info.addend orelse 0, |
| ... | ... | @@ -802,7 +815,7 @@ fn lowerAnonDeclRef( |
| 802 | 815 | } |
| 803 | 816 | |
| 804 | 817 | fn lowerDeclRef( |
| 805 | bin_file: *link.File, | |
| 818 | lf: *link.File, | |
| 806 | 819 | src_loc: Module.SrcLoc, |
| 807 | 820 | decl_index: InternPool.DeclIndex, |
| 808 | 821 | code: *std.ArrayList(u8), |
| ... | ... | @@ -811,20 +824,21 @@ fn lowerDeclRef( |
| 811 | 824 | ) CodeGenError!Result { |
| 812 | 825 | _ = src_loc; |
| 813 | 826 | _ = 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; | |
| 816 | 831 | |
| 817 | 832 | 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)) { | |
| 821 | 835 | try code.appendNTimes(0xaa, @divExact(ptr_width, 8)); |
| 822 | 836 | return Result.ok; |
| 823 | 837 | } |
| 824 | 838 | |
| 825 | try mod.markDeclAlive(decl); | |
| 839 | try zcu.markDeclAlive(decl); | |
| 826 | 840 | |
| 827 | const vaddr = try bin_file.getDeclVAddr(decl_index, .{ | |
| 841 | const vaddr = try lf.getDeclVAddr(decl_index, .{ | |
| 828 | 842 | .parent_atom_index = reloc_info.parent_atom_index, |
| 829 | 843 | .offset = code.items.len, |
| 830 | 844 | .addend = reloc_info.addend orelse 0, |
| ... | ... | @@ -897,27 +911,29 @@ pub const GenResult = union(enum) { |
| 897 | 911 | }; |
| 898 | 912 | |
| 899 | 913 | fn genDeclRef( |
| 900 | bin_file: *link.File, | |
| 914 | lf: *link.File, | |
| 901 | 915 | src_loc: Module.SrcLoc, |
| 902 | 916 | tv: TypedValue, |
| 903 | 917 | ptr_decl_index: InternPool.DeclIndex, |
| 904 | 918 | ) 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; | |
| 907 | 925 | |
| 908 | const target = bin_file.options.target; | |
| 909 | 926 | const ptr_bits = target.ptrBitWidth(); |
| 910 | 927 | const ptr_bytes: u64 = @divExact(ptr_bits, 8); |
| 911 | 928 | |
| 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))) { | |
| 914 | 930 | .func => |func| func.owner_decl, |
| 915 | 931 | .extern_func => |extern_func| extern_func.decl, |
| 916 | 932 | else => ptr_decl_index, |
| 917 | 933 | }; |
| 918 | const decl = mod.declPtr(decl_index); | |
| 934 | const decl = zcu.declPtr(decl_index); | |
| 919 | 935 | |
| 920 | if (!decl.ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { | |
| 936 | if (!decl.ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) { | |
| 921 | 937 | const imm: u64 = switch (ptr_bytes) { |
| 922 | 938 | 1 => 0xaa, |
| 923 | 939 | 2 => 0xaaaa, |
| ... | ... | @@ -929,30 +945,30 @@ fn genDeclRef( |
| 929 | 945 | } |
| 930 | 946 | |
| 931 | 947 | // 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().? }); | |
| 935 | 951 | } |
| 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().? }); | |
| 940 | 956 | } |
| 941 | 957 | } |
| 942 | 958 | |
| 943 | try mod.markDeclAlive(decl); | |
| 959 | try zcu.markDeclAlive(decl); | |
| 944 | 960 | |
| 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); | |
| 949 | 965 | |
| 950 | if (bin_file.cast(link.File.Elf)) |elf_file| { | |
| 966 | if (lf.cast(link.File.Elf)) |elf_file| { | |
| 951 | 967 | if (is_extern) { |
| 952 | const name = mod.intern_pool.stringToSlice(decl.name); | |
| 968 | const name = zcu.intern_pool.stringToSlice(decl.name); | |
| 953 | 969 | // 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) | |
| 956 | 972 | else |
| 957 | 973 | null; |
| 958 | 974 | const sym_index = try elf_file.getGlobalSymbol(name, lib_name); |
| ... | ... | @@ -965,12 +981,12 @@ fn genDeclRef( |
| 965 | 981 | return GenResult.mcv(.{ .load_tlv = sym.esym_index }); |
| 966 | 982 | } |
| 967 | 983 | 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| { | |
| 969 | 985 | if (is_extern) { |
| 970 | 986 | // 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); | |
| 974 | 990 | const global_index = try macho_file.addUndefined(sym_name, .{ .add_got = true }); |
| 975 | 991 | return GenResult.mcv(.{ .load_got = link.File.MachO.global_symbol_bit | global_index }); |
| 976 | 992 | } |
| ... | ... | @@ -980,110 +996,118 @@ fn genDeclRef( |
| 980 | 996 | return GenResult.mcv(.{ .load_tlv = sym_index }); |
| 981 | 997 | } |
| 982 | 998 | 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| { | |
| 984 | 1000 | if (is_extern) { |
| 985 | const name = mod.intern_pool.stringToSlice(decl.name); | |
| 1001 | const name = zcu.intern_pool.stringToSlice(decl.name); | |
| 986 | 1002 | // 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) | |
| 989 | 1005 | else |
| 990 | 1006 | null; |
| 991 | 1007 | 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 | |
| 993 | 1009 | return GenResult.mcv(.{ .load_got = link.File.Coff.global_symbol_bit | global_index }); |
| 994 | 1010 | } |
| 995 | 1011 | const atom_index = try coff_file.getOrCreateAtomForDecl(decl_index); |
| 996 | 1012 | const sym_index = coff_file.getAtom(atom_index).getSymbolIndex().?; |
| 997 | 1013 | 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| { | |
| 999 | 1015 | const atom_index = try p9.seeDecl(decl_index); |
| 1000 | 1016 | const atom = p9.getAtom(atom_index); |
| 1001 | 1017 | return GenResult.mcv(.{ .memory = atom.getOffsetTableAddress(p9) }); |
| 1002 | 1018 | } 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}); | |
| 1004 | 1020 | } |
| 1005 | 1021 | } |
| 1006 | 1022 | |
| 1007 | 1023 | fn genUnnamedConst( |
| 1008 | bin_file: *link.File, | |
| 1024 | lf: *link.File, | |
| 1009 | 1025 | src_loc: Module.SrcLoc, |
| 1010 | 1026 | tv: TypedValue, |
| 1011 | 1027 | owner_decl_index: InternPool.DeclIndex, |
| 1012 | 1028 | ) 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) }); | |
| 1015 | 1032 | |
| 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)}); | |
| 1019 | 1035 | }; |
| 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", .{}), | |
| 1032 | 1054 | } |
| 1033 | 1055 | } |
| 1034 | 1056 | |
| 1035 | 1057 | pub fn genTypedValue( |
| 1036 | bin_file: *link.File, | |
| 1058 | lf: *link.File, | |
| 1037 | 1059 | src_loc: Module.SrcLoc, |
| 1038 | 1060 | arg_tv: TypedValue, |
| 1039 | 1061 | owner_decl_index: InternPool.DeclIndex, |
| 1040 | 1062 | ) CodeGenError!GenResult { |
| 1041 | const mod = bin_file.comp.module.?; | |
| 1063 | const zcu = lf.comp.module.?; | |
| 1042 | 1064 | const typed_value = arg_tv; |
| 1043 | 1065 | |
| 1044 | 1066 | 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), | |
| 1047 | 1069 | }); |
| 1048 | 1070 | |
| 1049 | if (typed_value.val.isUndef(mod)) | |
| 1071 | if (typed_value.val.isUndef(zcu)) | |
| 1050 | 1072 | return GenResult.mcv(.undef); |
| 1051 | 1073 | |
| 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; | |
| 1053 | 1077 | const ptr_bits = target.ptrBitWidth(); |
| 1054 | 1078 | |
| 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())) { | |
| 1056 | 1080 | .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), | |
| 1059 | 1083 | else => {}, |
| 1060 | 1084 | }, |
| 1061 | 1085 | else => {}, |
| 1062 | 1086 | }; |
| 1063 | 1087 | |
| 1064 | switch (typed_value.ty.zigTypeTag(mod)) { | |
| 1088 | switch (typed_value.ty.zigTypeTag(zcu)) { | |
| 1065 | 1089 | .Void => return GenResult.mcv(.none), |
| 1066 | .Pointer => switch (typed_value.ty.ptrSize(mod)) { | |
| 1090 | .Pointer => switch (typed_value.ty.ptrSize(zcu)) { | |
| 1067 | 1091 | .Slice => {}, |
| 1068 | 1092 | else => switch (typed_value.val.toIntern()) { |
| 1069 | 1093 | .null_value => { |
| 1070 | 1094 | return GenResult.mcv(.{ .immediate = 0 }); |
| 1071 | 1095 | }, |
| 1072 | 1096 | .none => {}, |
| 1073 | else => switch (mod.intern_pool.indexToKey(typed_value.val.toIntern())) { | |
| 1097 | else => switch (zcu.intern_pool.indexToKey(typed_value.val.toIntern())) { | |
| 1074 | 1098 | .int => { |
| 1075 | return GenResult.mcv(.{ .immediate = typed_value.val.toUnsignedInt(mod) }); | |
| 1099 | return GenResult.mcv(.{ .immediate = typed_value.val.toUnsignedInt(zcu) }); | |
| 1076 | 1100 | }, |
| 1077 | 1101 | else => {}, |
| 1078 | 1102 | }, |
| 1079 | 1103 | }, |
| 1080 | 1104 | }, |
| 1081 | 1105 | .Int => { |
| 1082 | const info = typed_value.ty.intInfo(mod); | |
| 1106 | const info = typed_value.ty.intInfo(zcu); | |
| 1083 | 1107 | if (info.bits <= ptr_bits) { |
| 1084 | 1108 | 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), | |
| 1087 | 1111 | }; |
| 1088 | 1112 | return GenResult.mcv(.{ .immediate = unsigned }); |
| 1089 | 1113 | } |
| ... | ... | @@ -1092,45 +1116,45 @@ pub fn genTypedValue( |
| 1092 | 1116 | return GenResult.mcv(.{ .immediate = @intFromBool(typed_value.val.toBool()) }); |
| 1093 | 1117 | }, |
| 1094 | 1118 | .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 }), | |
| 1099 | 1123 | }, 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)) }); | |
| 1102 | 1126 | } |
| 1103 | 1127 | }, |
| 1104 | 1128 | .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, .{ | |
| 1108 | 1132 | .ty = Type.fromInterned(int_tag_ty), |
| 1109 | 1133 | .val = Value.fromInterned(enum_tag.int), |
| 1110 | 1134 | }, owner_decl_index); |
| 1111 | 1135 | }, |
| 1112 | 1136 | .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).?; | |
| 1115 | 1139 | return GenResult.mcv(.{ .immediate = error_index }); |
| 1116 | 1140 | }, |
| 1117 | 1141 | .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)) { | |
| 1121 | 1145 | // 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, .{ | |
| 1125 | 1149 | .ty = err_type, |
| 1126 | .val = Value.fromInterned((try mod.intern(.{ .err = .{ | |
| 1150 | .val = Value.fromInterned((try zcu.intern(.{ .err = .{ | |
| 1127 | 1151 | .ty = err_type.toIntern(), |
| 1128 | 1152 | .name = err_name, |
| 1129 | 1153 | } }))), |
| 1130 | 1154 | }, owner_decl_index), |
| 1131 | .payload => return genTypedValue(bin_file, src_loc, .{ | |
| 1155 | .payload => return genTypedValue(lf, src_loc, .{ | |
| 1132 | 1156 | .ty = err_int_ty, |
| 1133 | .val = try mod.intValue(err_int_ty, 0), | |
| 1157 | .val = try zcu.intValue(err_int_ty, 0), | |
| 1134 | 1158 | }, owner_decl_index), |
| 1135 | 1159 | } |
| 1136 | 1160 | } |
| ... | ... | @@ -1148,7 +1172,7 @@ pub fn genTypedValue( |
| 1148 | 1172 | else => {}, |
| 1149 | 1173 | } |
| 1150 | 1174 | |
| 1151 | return genUnnamedConst(bin_file, src_loc, typed_value, owner_decl_index); | |
| 1175 | return genUnnamedConst(lf, src_loc, typed_value, owner_decl_index); | |
| 1152 | 1176 | } |
| 1153 | 1177 | |
| 1154 | 1178 | pub 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 |
| 1192 | 1192 | |
| 1193 | 1193 | pub fn commitDeclState( |
| 1194 | 1194 | self: *Dwarf, |
| 1195 | mod: *Module, | |
| 1195 | zcu: *Module, | |
| 1196 | 1196 | decl_index: InternPool.DeclIndex, |
| 1197 | 1197 | sym_addr: u64, |
| 1198 | 1198 | sym_size: u64, |
| ... | ... | @@ -1202,15 +1202,17 @@ pub fn commitDeclState( |
| 1202 | 1202 | defer tracy.end(); |
| 1203 | 1203 | |
| 1204 | 1204 | 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 | ||
| 1205 | 1211 | var dbg_line_buffer = &decl_state.dbg_line; |
| 1206 | 1212 | 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(); | |
| 1211 | 1213 | |
| 1212 | 1214 | assert(decl.has_tv); |
| 1213 | switch (decl.ty.zigTypeTag(mod)) { | |
| 1215 | switch (decl.ty.zigTypeTag(zcu)) { | |
| 1214 | 1216 | .Fn => { |
| 1215 | 1217 | try decl_state.setInlineFunc(decl.val.toIntern()); |
| 1216 | 1218 | |
| ... | ... | @@ -1409,18 +1411,18 @@ pub fn commitDeclState( |
| 1409 | 1411 | if (ip.isErrorSetType(ty.toIntern())) continue; |
| 1410 | 1412 | |
| 1411 | 1413 | 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); | |
| 1413 | 1415 | } |
| 1414 | 1416 | } |
| 1415 | 1417 | |
| 1416 | 1418 | try self.updateDeclDebugInfoAllocation(di_atom_index, @intCast(dbg_info_buffer.items.len)); |
| 1417 | 1419 | |
| 1418 | 1420 | 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]; | |
| 1421 | 1423 | const ty = symbol.type; |
| 1422 | 1424 | if (ip.isErrorSetType(ty.toIntern())) { |
| 1423 | log.debug("resolving %{d} deferred until flush", .{target}); | |
| 1425 | log.debug("resolving %{d} deferred until flush", .{reloc_target}); | |
| 1424 | 1426 | try self.global_abbrev_relocs.append(gpa, .{ |
| 1425 | 1427 | .target = null, |
| 1426 | 1428 | .offset = reloc.offset, |
| ... | ... | @@ -1433,8 +1435,8 @@ pub fn commitDeclState( |
| 1433 | 1435 | log.debug("{x}: [() => {x}] (%{d}, '{}')", .{ |
| 1434 | 1436 | reloc.offset, |
| 1435 | 1437 | value, |
| 1436 | target, | |
| 1437 | ty.fmt(mod), | |
| 1438 | reloc_target, | |
| 1439 | ty.fmt(zcu), | |
| 1438 | 1440 | }); |
| 1439 | 1441 | mem.writeInt( |
| 1440 | 1442 | u32, |
| ... | ... | @@ -1897,7 +1899,7 @@ fn dbgInfoHeaderBytes(self: *Dwarf) usize { |
| 1897 | 1899 | return 120; |
| 1898 | 1900 | } |
| 1899 | 1901 | |
| 1900 | pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u64) !void { | |
| 1902 | pub fn writeDbgInfoHeader(self: *Dwarf, zcu: *Module, low_pc: u64, high_pc: u64) !void { | |
| 1901 | 1903 | // If this value is null it means there is an error in the module; |
| 1902 | 1904 | // leave debug_info_header_dirty=true. |
| 1903 | 1905 | 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 |
| 1908 | 1910 | var di_buf = try std.ArrayList(u8).initCapacity(self.allocator, needed_bytes); |
| 1909 | 1911 | defer di_buf.deinit(); |
| 1910 | 1912 | |
| 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(); | |
| 1912 | 1916 | const init_len_size: usize = switch (self.format) { |
| 1913 | 1917 | .dwarf32 => 4, |
| 1914 | 1918 | .dwarf64 => 12, |
| ... | ... | @@ -1931,9 +1935,9 @@ pub fn writeDbgInfoHeader(self: *Dwarf, module: *Module, low_pc: u64, high_pc: u |
| 1931 | 1935 | di_buf.appendAssumeCapacity(self.ptrWidthBytes()); // address size |
| 1932 | 1936 | |
| 1933 | 1937 | // 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); | |
| 1935 | 1939 | 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); | |
| 1937 | 1941 | const comp_dir_strp = try self.strtab.insert(self.allocator, compile_unit_dir); |
| 1938 | 1942 | const producer_strp = try self.strtab.insert(self.allocator, link.producer_string); |
| 1939 | 1943 | |
| ... | ... | @@ -1997,7 +2001,9 @@ fn resolveCompilationDir(module: *Module, buffer: *[std.fs.MAX_PATH_BYTES]u8) [] |
| 1997 | 2001 | } |
| 1998 | 2002 | |
| 1999 | 2003 | fn 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(); | |
| 2001 | 2007 | switch (self.ptr_width) { |
| 2002 | 2008 | .p32 => mem.writeInt(u32, buf.addManyAsArrayAssumeCapacity(4), @intCast(addr), target_endian), |
| 2003 | 2009 | .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 |
| 2005 | 2011 | } |
| 2006 | 2012 | |
| 2007 | 2013 | fn 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(); | |
| 2009 | 2017 | switch (self.format) { |
| 2010 | 2018 | .dwarf32 => mem.writeInt(u32, buf.addManyAsArrayAssumeCapacity(4), @intCast(off), target_endian), |
| 2011 | 2019 | .dwarf64 => mem.writeInt(u64, buf.addManyAsArrayAssumeCapacity(8), off, target_endian), |
| ... | ... | @@ -2227,7 +2235,9 @@ fn writeDbgInfoNopsToArrayList( |
| 2227 | 2235 | } |
| 2228 | 2236 | |
| 2229 | 2237 | pub 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(); | |
| 2231 | 2241 | const ptr_width_bytes = self.ptrWidthBytes(); |
| 2232 | 2242 | |
| 2233 | 2243 | // 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 { |
| 2299 | 2309 | } |
| 2300 | 2310 | |
| 2301 | 2311 | pub fn writeDbgLineHeader(self: *Dwarf) !void { |
| 2312 | const comp = self.bin_file.comp; | |
| 2302 | 2313 | 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(); | |
| 2305 | 2316 | const init_len_size: usize = switch (self.format) { |
| 2306 | 2317 | .dwarf32 => 4, |
| 2307 | 2318 | .dwarf64 => 12, |
| ... | ... | @@ -2565,7 +2576,8 @@ fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) { |
| 2565 | 2576 | } |
| 2566 | 2577 | |
| 2567 | 2578 | pub 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; | |
| 2569 | 2581 | |
| 2570 | 2582 | if (self.global_abbrev_relocs.items.len > 0) { |
| 2571 | 2583 | const gpa = self.allocator; |