| author | |
| committer | |
| log | 525f341f33af9b8aad53931fd5511f00a82cb090 |
| tree | cec3280498c1122858580946ac5e31f8feb807ce |
| parent | 8f20e81b8816aadd8ceb1b04bd3727cc1d124464 |
56 files changed, 11266 insertions(+), 9961 deletions(-)
CMakeLists.txt+1| ... | ... | @@ -525,6 +525,7 @@ set(ZIG_STAGE2_SOURCES |
| 525 | 525 | src/Type.zig |
| 526 | 526 | src/Value.zig |
| 527 | 527 | src/Zcu.zig |
| 528 | src/Zcu/PerThread.zig | |
| 528 | 529 | src/arch/aarch64/CodeGen.zig |
| 529 | 530 | src/arch/aarch64/Emit.zig |
| 530 | 531 | src/arch/aarch64/Mir.zig |
lib/std/Thread/Pool.zig+86-10| ... | ... | @@ -9,17 +9,19 @@ run_queue: RunQueue = .{}, |
| 9 | 9 | is_running: bool = true, |
| 10 | 10 | allocator: std.mem.Allocator, |
| 11 | 11 | threads: []std.Thread, |
| 12 | ids: std.AutoArrayHashMapUnmanaged(std.Thread.Id, void), | |
| 12 | 13 | |
| 13 | 14 | const RunQueue = std.SinglyLinkedList(Runnable); |
| 14 | 15 | const Runnable = struct { |
| 15 | 16 | runFn: RunProto, |
| 16 | 17 | }; |
| 17 | 18 | |
| 18 | const RunProto = *const fn (*Runnable) void; | |
| 19 | const RunProto = *const fn (*Runnable, id: ?usize) void; | |
| 19 | 20 | |
| 20 | 21 | pub const Options = struct { |
| 21 | 22 | allocator: std.mem.Allocator, |
| 22 | 23 | n_jobs: ?u32 = null, |
| 24 | track_ids: bool = false, | |
| 23 | 25 | }; |
| 24 | 26 | |
| 25 | 27 | pub fn init(pool: *Pool, options: Options) !void { |
| ... | ... | @@ -28,6 +30,7 @@ pub fn init(pool: *Pool, options: Options) !void { |
| 28 | 30 | pool.* = .{ |
| 29 | 31 | .allocator = allocator, |
| 30 | 32 | .threads = &[_]std.Thread{}, |
| 33 | .ids = .{}, | |
| 31 | 34 | }; |
| 32 | 35 | |
| 33 | 36 | if (builtin.single_threaded) { |
| ... | ... | @@ -35,6 +38,10 @@ pub fn init(pool: *Pool, options: Options) !void { |
| 35 | 38 | } |
| 36 | 39 | |
| 37 | 40 | const thread_count = options.n_jobs orelse @max(1, std.Thread.getCpuCount() catch 1); |
| 41 | if (options.track_ids) { | |
| 42 | try pool.ids.ensureTotalCapacity(allocator, 1 + thread_count); | |
| 43 | pool.ids.putAssumeCapacityNoClobber(std.Thread.getCurrentId(), {}); | |
| 44 | } | |
| 38 | 45 | |
| 39 | 46 | // kill and join any threads we spawned and free memory on error. |
| 40 | 47 | pool.threads = try allocator.alloc(std.Thread, thread_count); |
| ... | ... | @@ -49,6 +56,7 @@ pub fn init(pool: *Pool, options: Options) !void { |
| 49 | 56 | |
| 50 | 57 | pub fn deinit(pool: *Pool) void { |
| 51 | 58 | pool.join(pool.threads.len); // kill and join all threads. |
| 59 | pool.ids.deinit(pool.allocator); | |
| 52 | 60 | pool.* = undefined; |
| 53 | 61 | } |
| 54 | 62 | |
| ... | ... | @@ -96,7 +104,7 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args |
| 96 | 104 | run_node: RunQueue.Node = .{ .data = .{ .runFn = runFn } }, |
| 97 | 105 | wait_group: *WaitGroup, |
| 98 | 106 | |
| 99 | fn runFn(runnable: *Runnable) void { | |
| 107 | fn runFn(runnable: *Runnable, _: ?usize) void { | |
| 100 | 108 | const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable); |
| 101 | 109 | const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node)); |
| 102 | 110 | @call(.auto, func, closure.arguments); |
| ... | ... | @@ -134,6 +142,70 @@ pub fn spawnWg(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args |
| 134 | 142 | pool.cond.signal(); |
| 135 | 143 | } |
| 136 | 144 | |
| 145 | /// Runs `func` in the thread pool, calling `WaitGroup.start` beforehand, and | |
| 146 | /// `WaitGroup.finish` after it returns. | |
| 147 | /// | |
| 148 | /// The first argument passed to `func` is a dense `usize` thread id, the rest | |
| 149 | /// of the arguments are passed from `args`. Requires the pool to have been | |
| 150 | /// initialized with `.track_ids = true`. | |
| 151 | /// | |
| 152 | /// In the case that queuing the function call fails to allocate memory, or the | |
| 153 | /// target is single-threaded, the function is called directly. | |
| 154 | pub fn spawnWgId(pool: *Pool, wait_group: *WaitGroup, comptime func: anytype, args: anytype) void { | |
| 155 | wait_group.start(); | |
| 156 | ||
| 157 | if (builtin.single_threaded) { | |
| 158 | @call(.auto, func, .{0} ++ args); | |
| 159 | wait_group.finish(); | |
| 160 | return; | |
| 161 | } | |
| 162 | ||
| 163 | const Args = @TypeOf(args); | |
| 164 | const Closure = struct { | |
| 165 | arguments: Args, | |
| 166 | pool: *Pool, | |
| 167 | run_node: RunQueue.Node = .{ .data = .{ .runFn = runFn } }, | |
| 168 | wait_group: *WaitGroup, | |
| 169 | ||
| 170 | fn runFn(runnable: *Runnable, id: ?usize) void { | |
| 171 | const run_node: *RunQueue.Node = @fieldParentPtr("data", runnable); | |
| 172 | const closure: *@This() = @alignCast(@fieldParentPtr("run_node", run_node)); | |
| 173 | @call(.auto, func, .{id.?} ++ closure.arguments); | |
| 174 | closure.wait_group.finish(); | |
| 175 | ||
| 176 | // The thread pool's allocator is protected by the mutex. | |
| 177 | const mutex = &closure.pool.mutex; | |
| 178 | mutex.lock(); | |
| 179 | defer mutex.unlock(); | |
| 180 | ||
| 181 | closure.pool.allocator.destroy(closure); | |
| 182 | } | |
| 183 | }; | |
| 184 | ||
| 185 | { | |
| 186 | pool.mutex.lock(); | |
| 187 | ||
| 188 | const closure = pool.allocator.create(Closure) catch { | |
| 189 | const id = pool.ids.getIndex(std.Thread.getCurrentId()); | |
| 190 | pool.mutex.unlock(); | |
| 191 | @call(.auto, func, .{id.?} ++ args); | |
| 192 | wait_group.finish(); | |
| 193 | return; | |
| 194 | }; | |
| 195 | closure.* = .{ | |
| 196 | .arguments = args, | |
| 197 | .pool = pool, | |
| 198 | .wait_group = wait_group, | |
| 199 | }; | |
| 200 | ||
| 201 | pool.run_queue.prepend(&closure.run_node); | |
| 202 | pool.mutex.unlock(); | |
| 203 | } | |
| 204 | ||
| 205 | // Notify waiting threads outside the lock to try and keep the critical section small. | |
| 206 | pool.cond.signal(); | |
| 207 | } | |
| 208 | ||
| 137 | 209 | pub fn spawn(pool: *Pool, comptime func: anytype, args: anytype) !void { |
| 138 | 210 | if (builtin.single_threaded) { |
| 139 | 211 | @call(.auto, func, args); |
| ... | ... | @@ -181,14 +253,16 @@ fn worker(pool: *Pool) void { |
| 181 | 253 | pool.mutex.lock(); |
| 182 | 254 | defer pool.mutex.unlock(); |
| 183 | 255 | |
| 256 | const id = if (pool.ids.count() > 0) pool.ids.count() else null; | |
| 257 | if (id) |_| pool.ids.putAssumeCapacityNoClobber(std.Thread.getCurrentId(), {}); | |
| 258 | ||
| 184 | 259 | while (true) { |
| 185 | 260 | while (pool.run_queue.popFirst()) |run_node| { |
| 186 | 261 | // Temporarily unlock the mutex in order to execute the run_node |
| 187 | 262 | pool.mutex.unlock(); |
| 188 | 263 | defer pool.mutex.lock(); |
| 189 | 264 | |
| 190 | const runFn = run_node.data.runFn; | |
| 191 | runFn(&run_node.data); | |
| 265 | run_node.data.runFn(&run_node.data, id); | |
| 192 | 266 | } |
| 193 | 267 | |
| 194 | 268 | // Stop executing instead of waiting if the thread pool is no longer running. |
| ... | ... | @@ -201,16 +275,18 @@ fn worker(pool: *Pool) void { |
| 201 | 275 | } |
| 202 | 276 | |
| 203 | 277 | pub fn waitAndWork(pool: *Pool, wait_group: *WaitGroup) void { |
| 278 | var id: ?usize = null; | |
| 279 | ||
| 204 | 280 | while (!wait_group.isDone()) { |
| 205 | if (blk: { | |
| 206 | pool.mutex.lock(); | |
| 207 | defer pool.mutex.unlock(); | |
| 208 | break :blk pool.run_queue.popFirst(); | |
| 209 | }) |run_node| { | |
| 210 | run_node.data.runFn(&run_node.data); | |
| 281 | pool.mutex.lock(); | |
| 282 | if (pool.run_queue.popFirst()) |run_node| { | |
| 283 | id = id orelse pool.ids.getIndex(std.Thread.getCurrentId()); | |
| 284 | pool.mutex.unlock(); | |
| 285 | run_node.data.runFn(&run_node.data, id); | |
| 211 | 286 | continue; |
| 212 | 287 | } |
| 213 | 288 | |
| 289 | pool.mutex.unlock(); | |
| 214 | 290 | wait_group.wait(); |
| 215 | 291 | return; |
| 216 | 292 | } |
src/Air.zig+2-2| ... | ... | @@ -1563,12 +1563,12 @@ pub fn internedToRef(ip_index: InternPool.Index) Inst.Ref { |
| 1563 | 1563 | } |
| 1564 | 1564 | |
| 1565 | 1565 | /// Returns `null` if runtime-known. |
| 1566 | pub fn value(air: Air, inst: Inst.Ref, mod: *Module) !?Value { | |
| 1566 | pub fn value(air: Air, inst: Inst.Ref, pt: Zcu.PerThread) !?Value { | |
| 1567 | 1567 | if (inst.toInterned()) |ip_index| { |
| 1568 | 1568 | return Value.fromInterned(ip_index); |
| 1569 | 1569 | } |
| 1570 | 1570 | const index = inst.toIndex().?; |
| 1571 | return air.typeOfIndex(index, &mod.intern_pool).onePossibleValue(mod); | |
| 1571 | return air.typeOfIndex(index, &pt.zcu.intern_pool).onePossibleValue(pt); | |
| 1572 | 1572 | } |
| 1573 | 1573 | |
| 1574 | 1574 | pub fn nullTerminatedString(air: Air, index: usize) [:0]const u8 { |
src/Compilation.zig+31-29| ... | ... | @@ -2146,6 +2146,8 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) !void { |
| 2146 | 2146 | try comp.performAllTheWork(main_progress_node); |
| 2147 | 2147 | |
| 2148 | 2148 | if (comp.module) |zcu| { |
| 2149 | const pt: Zcu.PerThread = .{ .zcu = zcu, .tid = .main }; | |
| 2150 | ||
| 2149 | 2151 | if (build_options.enable_debug_extensions and comp.verbose_intern_pool) { |
| 2150 | 2152 | std.debug.print("intern pool stats for '{s}':\n", .{ |
| 2151 | 2153 | comp.root_name, |
| ... | ... | @@ -2165,10 +2167,10 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) !void { |
| 2165 | 2167 | // The `test_functions` decl has been intentionally postponed until now, |
| 2166 | 2168 | // at which point we must populate it with the list of test functions that |
| 2167 | 2169 | // have been discovered and not filtered out. |
| 2168 | try zcu.populateTestFunctions(main_progress_node); | |
| 2170 | try pt.populateTestFunctions(main_progress_node); | |
| 2169 | 2171 | } |
| 2170 | 2172 | |
| 2171 | try zcu.processExports(); | |
| 2173 | try pt.processExports(); | |
| 2172 | 2174 | } |
| 2173 | 2175 | |
| 2174 | 2176 | if (comp.totalErrorCount() != 0) { |
| ... | ... | @@ -2247,7 +2249,7 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) !void { |
| 2247 | 2249 | } |
| 2248 | 2250 | } |
| 2249 | 2251 | |
| 2250 | try flush(comp, arena, main_progress_node); | |
| 2252 | try flush(comp, arena, .main, main_progress_node); | |
| 2251 | 2253 | if (comp.totalErrorCount() != 0) return; |
| 2252 | 2254 | |
| 2253 | 2255 | // Failure here only means an unnecessary cache miss. |
| ... | ... | @@ -2264,16 +2266,16 @@ pub fn update(comp: *Compilation, main_progress_node: std.Progress.Node) !void { |
| 2264 | 2266 | whole.lock = man.toOwnedLock(); |
| 2265 | 2267 | }, |
| 2266 | 2268 | .incremental => { |
| 2267 | try flush(comp, arena, main_progress_node); | |
| 2269 | try flush(comp, arena, .main, main_progress_node); | |
| 2268 | 2270 | if (comp.totalErrorCount() != 0) return; |
| 2269 | 2271 | }, |
| 2270 | 2272 | } |
| 2271 | 2273 | } |
| 2272 | 2274 | |
| 2273 | fn flush(comp: *Compilation, arena: Allocator, prog_node: std.Progress.Node) !void { | |
| 2275 | fn flush(comp: *Compilation, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) !void { | |
| 2274 | 2276 | if (comp.bin_file) |lf| { |
| 2275 | 2277 | // This is needed before reading the error flags. |
| 2276 | lf.flush(arena, prog_node) catch |err| switch (err) { | |
| 2278 | lf.flush(arena, tid, prog_node) catch |err| switch (err) { | |
| 2277 | 2279 | error.FlushFailure => {}, // error reported through link_error_flags |
| 2278 | 2280 | error.LLDReportedFailure => {}, // error reported via lockAndParseLldStderr |
| 2279 | 2281 | else => |e| return e, |
| ... | ... | @@ -3419,7 +3421,7 @@ pub fn performAllTheWork( |
| 3419 | 3421 | |
| 3420 | 3422 | while (true) { |
| 3421 | 3423 | if (comp.work_queue.readItem()) |work_item| { |
| 3422 | try processOneJob(comp, work_item, main_progress_node); | |
| 3424 | try processOneJob(0, comp, work_item, main_progress_node); | |
| 3423 | 3425 | continue; |
| 3424 | 3426 | } |
| 3425 | 3427 | if (comp.module) |zcu| { |
| ... | ... | @@ -3447,11 +3449,11 @@ pub fn performAllTheWork( |
| 3447 | 3449 | } |
| 3448 | 3450 | } |
| 3449 | 3451 | |
| 3450 | fn processOneJob(comp: *Compilation, job: Job, prog_node: std.Progress.Node) !void { | |
| 3452 | fn processOneJob(tid: usize, comp: *Compilation, job: Job, prog_node: std.Progress.Node) !void { | |
| 3451 | 3453 | switch (job) { |
| 3452 | 3454 | .codegen_decl => |decl_index| { |
| 3453 | const zcu = comp.module.?; | |
| 3454 | const decl = zcu.declPtr(decl_index); | |
| 3455 | const pt: Zcu.PerThread = .{ .zcu = comp.module.?, .tid = @enumFromInt(tid) }; | |
| 3456 | const decl = pt.zcu.declPtr(decl_index); | |
| 3455 | 3457 | |
| 3456 | 3458 | switch (decl.analysis) { |
| 3457 | 3459 | .unreferenced => unreachable, |
| ... | ... | @@ -3469,7 +3471,7 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: std.Progress.Node) !vo |
| 3469 | 3471 | |
| 3470 | 3472 | assert(decl.has_tv); |
| 3471 | 3473 | |
| 3472 | try zcu.linkerUpdateDecl(decl_index); | |
| 3474 | try pt.linkerUpdateDecl(decl_index); | |
| 3473 | 3475 | return; |
| 3474 | 3476 | }, |
| 3475 | 3477 | } |
| ... | ... | @@ -3478,16 +3480,16 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: std.Progress.Node) !vo |
| 3478 | 3480 | const named_frame = tracy.namedFrame("codegen_func"); |
| 3479 | 3481 | defer named_frame.end(); |
| 3480 | 3482 | |
| 3481 | const zcu = comp.module.?; | |
| 3483 | const pt: Zcu.PerThread = .{ .zcu = comp.module.?, .tid = @enumFromInt(tid) }; | |
| 3482 | 3484 | // This call takes ownership of `func.air`. |
| 3483 | try zcu.linkerUpdateFunc(func.func, func.air); | |
| 3485 | try pt.linkerUpdateFunc(func.func, func.air); | |
| 3484 | 3486 | }, |
| 3485 | 3487 | .analyze_func => |func| { |
| 3486 | 3488 | const named_frame = tracy.namedFrame("analyze_func"); |
| 3487 | 3489 | defer named_frame.end(); |
| 3488 | 3490 | |
| 3489 | const zcu = comp.module.?; | |
| 3490 | zcu.ensureFuncBodyAnalyzed(func) catch |err| switch (err) { | |
| 3491 | const pt: Zcu.PerThread = .{ .zcu = comp.module.?, .tid = @enumFromInt(tid) }; | |
| 3492 | pt.ensureFuncBodyAnalyzed(func) catch |err| switch (err) { | |
| 3491 | 3493 | error.OutOfMemory => return error.OutOfMemory, |
| 3492 | 3494 | error.AnalysisFail => return, |
| 3493 | 3495 | }; |
| ... | ... | @@ -3496,8 +3498,8 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: std.Progress.Node) !vo |
| 3496 | 3498 | if (true) @panic("regressed compiler feature: emit-h should hook into updateExports, " ++ |
| 3497 | 3499 | "not decl analysis, which is too early to know about @export calls"); |
| 3498 | 3500 | |
| 3499 | const zcu = comp.module.?; | |
| 3500 | const decl = zcu.declPtr(decl_index); | |
| 3501 | const pt: Zcu.PerThread = .{ .zcu = comp.module.?, .tid = @enumFromInt(tid) }; | |
| 3502 | const decl = pt.zcu.declPtr(decl_index); | |
| 3501 | 3503 | |
| 3502 | 3504 | switch (decl.analysis) { |
| 3503 | 3505 | .unreferenced => unreachable, |
| ... | ... | @@ -3515,7 +3517,7 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: std.Progress.Node) !vo |
| 3515 | 3517 | defer named_frame.end(); |
| 3516 | 3518 | |
| 3517 | 3519 | const gpa = comp.gpa; |
| 3518 | const emit_h = zcu.emit_h.?; | |
| 3520 | const emit_h = pt.zcu.emit_h.?; | |
| 3519 | 3521 | _ = try emit_h.decl_table.getOrPut(gpa, decl_index); |
| 3520 | 3522 | const decl_emit_h = emit_h.declPtr(decl_index); |
| 3521 | 3523 | const fwd_decl = &decl_emit_h.fwd_decl; |
| ... | ... | @@ -3523,11 +3525,11 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: std.Progress.Node) !vo |
| 3523 | 3525 | var ctypes_arena = std.heap.ArenaAllocator.init(gpa); |
| 3524 | 3526 | defer ctypes_arena.deinit(); |
| 3525 | 3527 | |
| 3526 | const file_scope = zcu.namespacePtr(decl.src_namespace).fileScope(zcu); | |
| 3528 | const file_scope = pt.zcu.namespacePtr(decl.src_namespace).fileScope(pt.zcu); | |
| 3527 | 3529 | |
| 3528 | 3530 | var dg: c_codegen.DeclGen = .{ |
| 3529 | 3531 | .gpa = gpa, |
| 3530 | .zcu = zcu, | |
| 3532 | .pt = pt, | |
| 3531 | 3533 | .mod = file_scope.mod, |
| 3532 | 3534 | .error_msg = null, |
| 3533 | 3535 | .pass = .{ .decl = decl_index }, |
| ... | ... | @@ -3557,25 +3559,25 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: std.Progress.Node) !vo |
| 3557 | 3559 | } |
| 3558 | 3560 | }, |
| 3559 | 3561 | .analyze_decl => |decl_index| { |
| 3560 | const zcu = comp.module.?; | |
| 3561 | zcu.ensureDeclAnalyzed(decl_index) catch |err| switch (err) { | |
| 3562 | const pt: Zcu.PerThread = .{ .zcu = comp.module.?, .tid = @enumFromInt(tid) }; | |
| 3563 | pt.ensureDeclAnalyzed(decl_index) catch |err| switch (err) { | |
| 3562 | 3564 | error.OutOfMemory => return error.OutOfMemory, |
| 3563 | 3565 | error.AnalysisFail => return, |
| 3564 | 3566 | }; |
| 3565 | const decl = zcu.declPtr(decl_index); | |
| 3567 | const decl = pt.zcu.declPtr(decl_index); | |
| 3566 | 3568 | if (decl.kind == .@"test" and comp.config.is_test) { |
| 3567 | 3569 | // Tests are always emitted in test binaries. The decl_refs are created by |
| 3568 | 3570 | // Zcu.populateTestFunctions, but this will not queue body analysis, so do |
| 3569 | 3571 | // that now. |
| 3570 | try zcu.ensureFuncBodyAnalysisQueued(decl.val.toIntern()); | |
| 3572 | try pt.zcu.ensureFuncBodyAnalysisQueued(decl.val.toIntern()); | |
| 3571 | 3573 | } |
| 3572 | 3574 | }, |
| 3573 | 3575 | .resolve_type_fully => |ty| { |
| 3574 | 3576 | const named_frame = tracy.namedFrame("resolve_type_fully"); |
| 3575 | 3577 | defer named_frame.end(); |
| 3576 | 3578 | |
| 3577 | const zcu = comp.module.?; | |
| 3578 | Type.fromInterned(ty).resolveFully(zcu) catch |err| switch (err) { | |
| 3579 | const pt: Zcu.PerThread = .{ .zcu = comp.module.?, .tid = @enumFromInt(tid) }; | |
| 3580 | Type.fromInterned(ty).resolveFully(pt) catch |err| switch (err) { | |
| 3579 | 3581 | error.OutOfMemory => return error.OutOfMemory, |
| 3580 | 3582 | error.AnalysisFail => return, |
| 3581 | 3583 | }; |
| ... | ... | @@ -3603,12 +3605,12 @@ fn processOneJob(comp: *Compilation, job: Job, prog_node: std.Progress.Node) !vo |
| 3603 | 3605 | try zcu.retryable_failures.append(gpa, InternPool.AnalUnit.wrap(.{ .decl = decl_index })); |
| 3604 | 3606 | }; |
| 3605 | 3607 | }, |
| 3606 | .analyze_mod => |pkg| { | |
| 3608 | .analyze_mod => |mod| { | |
| 3607 | 3609 | const named_frame = tracy.namedFrame("analyze_mod"); |
| 3608 | 3610 | defer named_frame.end(); |
| 3609 | 3611 | |
| 3610 | const zcu = comp.module.?; | |
| 3611 | zcu.semaPkg(pkg) catch |err| switch (err) { | |
| 3612 | const pt: Zcu.PerThread = .{ .zcu = comp.module.?, .tid = @enumFromInt(tid) }; | |
| 3613 | pt.semaPkg(mod) catch |err| switch (err) { | |
| 3612 | 3614 | error.OutOfMemory => return error.OutOfMemory, |
| 3613 | 3615 | error.AnalysisFail => return, |
| 3614 | 3616 | }; |
src/InternPool.zig+156-86| ... | ... | @@ -4548,17 +4548,14 @@ pub fn init(ip: *InternPool, gpa: Allocator) !void { |
| 4548 | 4548 | |
| 4549 | 4549 | // This inserts all the statically-known values into the intern pool in the |
| 4550 | 4550 | // order expected. |
| 4551 | for (static_keys[0..@intFromEnum(Index.empty_struct_type)]) |key| { | |
| 4552 | _ = ip.get(gpa, key) catch unreachable; | |
| 4553 | } | |
| 4554 | _ = ip.getAnonStructType(gpa, .{ | |
| 4555 | .types = &.{}, | |
| 4556 | .names = &.{}, | |
| 4557 | .values = &.{}, | |
| 4558 | }) catch unreachable; | |
| 4559 | for (static_keys[@intFromEnum(Index.empty_struct_type) + 1 ..]) |key| { | |
| 4560 | _ = ip.get(gpa, key) catch unreachable; | |
| 4561 | } | |
| 4551 | for (&static_keys, 0..) |key, key_index| switch (@as(Index, @enumFromInt(key_index))) { | |
| 4552 | .empty_struct_type => assert(try ip.getAnonStructType(gpa, .main, .{ | |
| 4553 | .types = &.{}, | |
| 4554 | .names = &.{}, | |
| 4555 | .values = &.{}, | |
| 4556 | }) == .empty_struct_type), | |
| 4557 | else => |expected_index| assert(try ip.get(gpa, .main, key) == expected_index), | |
| 4558 | }; | |
| 4562 | 4559 | |
| 4563 | 4560 | if (std.debug.runtime_safety) { |
| 4564 | 4561 | // Sanity check. |
| ... | ... | @@ -5242,7 +5239,7 @@ fn indexToKeyBigInt(ip: *const InternPool, limb_index: u32, positive: bool) Key |
| 5242 | 5239 | } }; |
| 5243 | 5240 | } |
| 5244 | 5241 | |
| 5245 | pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index { | |
| 5242 | pub fn get(ip: *InternPool, gpa: Allocator, tid: Zcu.PerThread.Id, key: Key) Allocator.Error!Index { | |
| 5246 | 5243 | const adapter: KeyAdapter = .{ .intern_pool = ip }; |
| 5247 | 5244 | const gop = try ip.map.getOrPutAdapted(gpa, key, adapter); |
| 5248 | 5245 | if (gop.found_existing) return @enumFromInt(gop.index); |
| ... | ... | @@ -5266,8 +5263,9 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index { |
| 5266 | 5263 | _ = ip.map.pop(); |
| 5267 | 5264 | var new_key = key; |
| 5268 | 5265 | new_key.ptr_type.flags.size = .Many; |
| 5269 | const ptr_type_index = try ip.get(gpa, new_key); | |
| 5266 | const ptr_type_index = try ip.get(gpa, tid, new_key); | |
| 5270 | 5267 | assert(!(try ip.map.getOrPutAdapted(gpa, key, adapter)).found_existing); |
| 5268 | ||
| 5271 | 5269 | try ip.items.ensureUnusedCapacity(gpa, 1); |
| 5272 | 5270 | ip.items.appendAssumeCapacity(.{ |
| 5273 | 5271 | .tag = .type_slice, |
| ... | ... | @@ -5519,7 +5517,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index { |
| 5519 | 5517 | else => unreachable, |
| 5520 | 5518 | } |
| 5521 | 5519 | _ = ip.map.pop(); |
| 5522 | const index_index = try ip.get(gpa, .{ .int = .{ | |
| 5520 | const index_index = try ip.get(gpa, tid, .{ .int = .{ | |
| 5523 | 5521 | .ty = .usize_type, |
| 5524 | 5522 | .storage = .{ .u64 = base_index.index }, |
| 5525 | 5523 | } }); |
| ... | ... | @@ -5932,7 +5930,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index { |
| 5932 | 5930 | const elem = switch (aggregate.storage) { |
| 5933 | 5931 | .bytes => |bytes| elem: { |
| 5934 | 5932 | _ = ip.map.pop(); |
| 5935 | const elem = try ip.get(gpa, .{ .int = .{ | |
| 5933 | const elem = try ip.get(gpa, tid, .{ .int = .{ | |
| 5936 | 5934 | .ty = .u8_type, |
| 5937 | 5935 | .storage = .{ .u64 = bytes.at(0, ip) }, |
| 5938 | 5936 | } }); |
| ... | ... | @@ -6074,7 +6072,12 @@ pub const UnionTypeInit = struct { |
| 6074 | 6072 | }, |
| 6075 | 6073 | }; |
| 6076 | 6074 | |
| 6077 | pub fn getUnionType(ip: *InternPool, gpa: Allocator, ini: UnionTypeInit) Allocator.Error!WipNamespaceType.Result { | |
| 6075 | pub fn getUnionType( | |
| 6076 | ip: *InternPool, | |
| 6077 | gpa: Allocator, | |
| 6078 | _: Zcu.PerThread.Id, | |
| 6079 | ini: UnionTypeInit, | |
| 6080 | ) Allocator.Error!WipNamespaceType.Result { | |
| 6078 | 6081 | const adapter: KeyAdapter = .{ .intern_pool = ip }; |
| 6079 | 6082 | const gop = try ip.map.getOrPutAdapted(gpa, Key{ .union_type = switch (ini.key) { |
| 6080 | 6083 | .declared => |d| .{ .declared = .{ |
| ... | ... | @@ -6221,6 +6224,7 @@ pub const StructTypeInit = struct { |
| 6221 | 6224 | pub fn getStructType( |
| 6222 | 6225 | ip: *InternPool, |
| 6223 | 6226 | gpa: Allocator, |
| 6227 | _: Zcu.PerThread.Id, | |
| 6224 | 6228 | ini: StructTypeInit, |
| 6225 | 6229 | ) Allocator.Error!WipNamespaceType.Result { |
| 6226 | 6230 | const adapter: KeyAdapter = .{ .intern_pool = ip }; |
| ... | ... | @@ -6396,7 +6400,12 @@ pub const AnonStructTypeInit = struct { |
| 6396 | 6400 | values: []const Index, |
| 6397 | 6401 | }; |
| 6398 | 6402 | |
| 6399 | pub fn getAnonStructType(ip: *InternPool, gpa: Allocator, ini: AnonStructTypeInit) Allocator.Error!Index { | |
| 6403 | pub fn getAnonStructType( | |
| 6404 | ip: *InternPool, | |
| 6405 | gpa: Allocator, | |
| 6406 | _: Zcu.PerThread.Id, | |
| 6407 | ini: AnonStructTypeInit, | |
| 6408 | ) Allocator.Error!Index { | |
| 6400 | 6409 | assert(ini.types.len == ini.values.len); |
| 6401 | 6410 | for (ini.types) |elem| assert(elem != .none); |
| 6402 | 6411 | |
| ... | ... | @@ -6450,7 +6459,12 @@ pub const GetFuncTypeKey = struct { |
| 6450 | 6459 | addrspace_is_generic: bool = false, |
| 6451 | 6460 | }; |
| 6452 | 6461 | |
| 6453 | pub fn getFuncType(ip: *InternPool, gpa: Allocator, key: GetFuncTypeKey) Allocator.Error!Index { | |
| 6462 | pub fn getFuncType( | |
| 6463 | ip: *InternPool, | |
| 6464 | gpa: Allocator, | |
| 6465 | _: Zcu.PerThread.Id, | |
| 6466 | key: GetFuncTypeKey, | |
| 6467 | ) Allocator.Error!Index { | |
| 6454 | 6468 | // Validate input parameters. |
| 6455 | 6469 | assert(key.return_type != .none); |
| 6456 | 6470 | for (key.param_types) |param_type| assert(param_type != .none); |
| ... | ... | @@ -6503,7 +6517,12 @@ pub fn getFuncType(ip: *InternPool, gpa: Allocator, key: GetFuncTypeKey) Allocat |
| 6503 | 6517 | return @enumFromInt(ip.items.len - 1); |
| 6504 | 6518 | } |
| 6505 | 6519 | |
| 6506 | pub fn getExternFunc(ip: *InternPool, gpa: Allocator, key: Key.ExternFunc) Allocator.Error!Index { | |
| 6520 | pub fn getExternFunc( | |
| 6521 | ip: *InternPool, | |
| 6522 | gpa: Allocator, | |
| 6523 | _: Zcu.PerThread.Id, | |
| 6524 | key: Key.ExternFunc, | |
| 6525 | ) Allocator.Error!Index { | |
| 6507 | 6526 | const adapter: KeyAdapter = .{ .intern_pool = ip }; |
| 6508 | 6527 | const gop = try ip.map.getOrPutAdapted(gpa, Key{ .extern_func = key }, adapter); |
| 6509 | 6528 | if (gop.found_existing) return @enumFromInt(gop.index); |
| ... | ... | @@ -6531,7 +6550,12 @@ pub const GetFuncDeclKey = struct { |
| 6531 | 6550 | is_noinline: bool, |
| 6532 | 6551 | }; |
| 6533 | 6552 | |
| 6534 | pub fn getFuncDecl(ip: *InternPool, gpa: Allocator, key: GetFuncDeclKey) Allocator.Error!Index { | |
| 6553 | pub fn getFuncDecl( | |
| 6554 | ip: *InternPool, | |
| 6555 | gpa: Allocator, | |
| 6556 | _: Zcu.PerThread.Id, | |
| 6557 | key: GetFuncDeclKey, | |
| 6558 | ) Allocator.Error!Index { | |
| 6535 | 6559 | // The strategy here is to add the function type unconditionally, then to |
| 6536 | 6560 | // ask if it already exists, and if so, revert the lengths of the mutated |
| 6537 | 6561 | // arrays. This is similar to what `getOrPutTrailingString` does. |
| ... | ... | @@ -6598,7 +6622,12 @@ pub const GetFuncDeclIesKey = struct { |
| 6598 | 6622 | rbrace_column: u32, |
| 6599 | 6623 | }; |
| 6600 | 6624 | |
| 6601 | pub fn getFuncDeclIes(ip: *InternPool, gpa: Allocator, key: GetFuncDeclIesKey) Allocator.Error!Index { | |
| 6625 | pub fn getFuncDeclIes( | |
| 6626 | ip: *InternPool, | |
| 6627 | gpa: Allocator, | |
| 6628 | _: Zcu.PerThread.Id, | |
| 6629 | key: GetFuncDeclIesKey, | |
| 6630 | ) Allocator.Error!Index { | |
| 6602 | 6631 | // Validate input parameters. |
| 6603 | 6632 | assert(key.bare_return_type != .none); |
| 6604 | 6633 | for (key.param_types) |param_type| assert(param_type != .none); |
| ... | ... | @@ -6707,6 +6736,7 @@ pub fn getFuncDeclIes(ip: *InternPool, gpa: Allocator, key: GetFuncDeclIesKey) A |
| 6707 | 6736 | pub fn getErrorSetType( |
| 6708 | 6737 | ip: *InternPool, |
| 6709 | 6738 | gpa: Allocator, |
| 6739 | _: Zcu.PerThread.Id, | |
| 6710 | 6740 | names: []const NullTerminatedString, |
| 6711 | 6741 | ) Allocator.Error!Index { |
| 6712 | 6742 | assert(std.sort.isSorted(NullTerminatedString, names, {}, NullTerminatedString.indexLessThan)); |
| ... | ... | @@ -6770,11 +6800,16 @@ pub const GetFuncInstanceKey = struct { |
| 6770 | 6800 | inferred_error_set: bool, |
| 6771 | 6801 | }; |
| 6772 | 6802 | |
| 6773 | pub fn getFuncInstance(ip: *InternPool, gpa: Allocator, arg: GetFuncInstanceKey) Allocator.Error!Index { | |
| 6803 | pub fn getFuncInstance( | |
| 6804 | ip: *InternPool, | |
| 6805 | gpa: Allocator, | |
| 6806 | tid: Zcu.PerThread.Id, | |
| 6807 | arg: GetFuncInstanceKey, | |
| 6808 | ) Allocator.Error!Index { | |
| 6774 | 6809 | if (arg.inferred_error_set) |
| 6775 | return getFuncInstanceIes(ip, gpa, arg); | |
| 6810 | return getFuncInstanceIes(ip, gpa, tid, arg); | |
| 6776 | 6811 | |
| 6777 | const func_ty = try ip.getFuncType(gpa, .{ | |
| 6812 | const func_ty = try ip.getFuncType(gpa, tid, .{ | |
| 6778 | 6813 | .param_types = arg.param_types, |
| 6779 | 6814 | .return_type = arg.bare_return_type, |
| 6780 | 6815 | .noalias_bits = arg.noalias_bits, |
| ... | ... | @@ -6844,6 +6879,7 @@ pub fn getFuncInstance(ip: *InternPool, gpa: Allocator, arg: GetFuncInstanceKey) |
| 6844 | 6879 | pub fn getFuncInstanceIes( |
| 6845 | 6880 | ip: *InternPool, |
| 6846 | 6881 | gpa: Allocator, |
| 6882 | _: Zcu.PerThread.Id, | |
| 6847 | 6883 | arg: GetFuncInstanceKey, |
| 6848 | 6884 | ) Allocator.Error!Index { |
| 6849 | 6885 | // Validate input parameters. |
| ... | ... | @@ -6955,7 +6991,6 @@ pub fn getFuncInstanceIes( |
| 6955 | 6991 | assert(!ip.map.getOrPutAssumeCapacityAdapted(Key{ |
| 6956 | 6992 | .func_type = extraFuncType(ip, func_type_extra_index), |
| 6957 | 6993 | }, adapter).found_existing); |
| 6958 | ||
| 6959 | 6994 | return finishFuncInstance( |
| 6960 | 6995 | ip, |
| 6961 | 6996 | gpa, |
| ... | ... | @@ -7096,6 +7131,7 @@ pub const WipEnumType = struct { |
| 7096 | 7131 | pub fn getEnumType( |
| 7097 | 7132 | ip: *InternPool, |
| 7098 | 7133 | gpa: Allocator, |
| 7134 | _: Zcu.PerThread.Id, | |
| 7099 | 7135 | ini: EnumTypeInit, |
| 7100 | 7136 | ) Allocator.Error!WipEnumType.Result { |
| 7101 | 7137 | const adapter: KeyAdapter = .{ .intern_pool = ip }; |
| ... | ... | @@ -7172,7 +7208,7 @@ pub fn getEnumType( |
| 7172 | 7208 | break :m values_map.toOptional(); |
| 7173 | 7209 | }; |
| 7174 | 7210 | errdefer if (ini.has_values) { |
| 7175 | _ = ip.map.pop(); | |
| 7211 | _ = ip.maps.pop(); | |
| 7176 | 7212 | }; |
| 7177 | 7213 | |
| 7178 | 7214 | try ip.extra.ensureUnusedCapacity(gpa, @typeInfo(EnumExplicit).Struct.fields.len + |
| ... | ... | @@ -7245,7 +7281,12 @@ const GeneratedTagEnumTypeInit = struct { |
| 7245 | 7281 | /// Creates an enum type which was automatically-generated as the tag type of a |
| 7246 | 7282 | /// `union` with no explicit tag type. Since this is only called once per union |
| 7247 | 7283 | /// type, it asserts that no matching type yet exists. |
| 7248 | pub fn getGeneratedTagEnumType(ip: *InternPool, gpa: Allocator, ini: GeneratedTagEnumTypeInit) Allocator.Error!Index { | |
| 7284 | pub fn getGeneratedTagEnumType( | |
| 7285 | ip: *InternPool, | |
| 7286 | gpa: Allocator, | |
| 7287 | _: Zcu.PerThread.Id, | |
| 7288 | ini: GeneratedTagEnumTypeInit, | |
| 7289 | ) Allocator.Error!Index { | |
| 7249 | 7290 | assert(ip.isUnion(ini.owner_union_ty)); |
| 7250 | 7291 | assert(ip.isIntegerType(ini.tag_ty)); |
| 7251 | 7292 | for (ini.values) |val| assert(ip.typeOf(val) == ini.tag_ty); |
| ... | ... | @@ -7342,7 +7383,12 @@ pub const OpaqueTypeInit = struct { |
| 7342 | 7383 | }, |
| 7343 | 7384 | }; |
| 7344 | 7385 | |
| 7345 | pub fn getOpaqueType(ip: *InternPool, gpa: Allocator, ini: OpaqueTypeInit) Allocator.Error!WipNamespaceType.Result { | |
| 7386 | pub fn getOpaqueType( | |
| 7387 | ip: *InternPool, | |
| 7388 | gpa: Allocator, | |
| 7389 | _: Zcu.PerThread.Id, | |
| 7390 | ini: OpaqueTypeInit, | |
| 7391 | ) Allocator.Error!WipNamespaceType.Result { | |
| 7346 | 7392 | const adapter: KeyAdapter = .{ .intern_pool = ip }; |
| 7347 | 7393 | const gop = try ip.map.getOrPutAdapted(gpa, Key{ .opaque_type = switch (ini.key) { |
| 7348 | 7394 | .declared => |d| .{ .declared = .{ |
| ... | ... | @@ -7680,23 +7726,23 @@ test "basic usage" { |
| 7680 | 7726 | var ip: InternPool = .{}; |
| 7681 | 7727 | defer ip.deinit(gpa); |
| 7682 | 7728 | |
| 7683 | const i32_type = try ip.get(gpa, .{ .int_type = .{ | |
| 7729 | const i32_type = try ip.get(gpa, .main, .{ .int_type = .{ | |
| 7684 | 7730 | .signedness = .signed, |
| 7685 | 7731 | .bits = 32, |
| 7686 | 7732 | } }); |
| 7687 | const array_i32 = try ip.get(gpa, .{ .array_type = .{ | |
| 7733 | const array_i32 = try ip.get(gpa, .main, .{ .array_type = .{ | |
| 7688 | 7734 | .len = 10, |
| 7689 | 7735 | .child = i32_type, |
| 7690 | 7736 | .sentinel = .none, |
| 7691 | 7737 | } }); |
| 7692 | 7738 | |
| 7693 | const another_i32_type = try ip.get(gpa, .{ .int_type = .{ | |
| 7739 | const another_i32_type = try ip.get(gpa, .main, .{ .int_type = .{ | |
| 7694 | 7740 | .signedness = .signed, |
| 7695 | 7741 | .bits = 32, |
| 7696 | 7742 | } }); |
| 7697 | 7743 | try std.testing.expect(another_i32_type == i32_type); |
| 7698 | 7744 | |
| 7699 | const another_array_i32 = try ip.get(gpa, .{ .array_type = .{ | |
| 7745 | const another_array_i32 = try ip.get(gpa, .main, .{ .array_type = .{ | |
| 7700 | 7746 | .len = 10, |
| 7701 | 7747 | .child = i32_type, |
| 7702 | 7748 | .sentinel = .none, |
| ... | ... | @@ -7766,48 +7812,54 @@ pub fn sliceLen(ip: *const InternPool, i: Index) Index { |
| 7766 | 7812 | /// * payload => error union |
| 7767 | 7813 | /// * fn <=> fn |
| 7768 | 7814 | /// * aggregate <=> aggregate (where children can also be coerced) |
| 7769 | pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Allocator.Error!Index { | |
| 7815 | pub fn getCoerced( | |
| 7816 | ip: *InternPool, | |
| 7817 | gpa: Allocator, | |
| 7818 | tid: Zcu.PerThread.Id, | |
| 7819 | val: Index, | |
| 7820 | new_ty: Index, | |
| 7821 | ) Allocator.Error!Index { | |
| 7770 | 7822 | const old_ty = ip.typeOf(val); |
| 7771 | 7823 | if (old_ty == new_ty) return val; |
| 7772 | 7824 | |
| 7773 | 7825 | const tags = ip.items.items(.tag); |
| 7774 | 7826 | |
| 7775 | 7827 | switch (val) { |
| 7776 | .undef => return ip.get(gpa, .{ .undef = new_ty }), | |
| 7828 | .undef => return ip.get(gpa, tid, .{ .undef = new_ty }), | |
| 7777 | 7829 | .null_value => { |
| 7778 | if (ip.isOptionalType(new_ty)) return ip.get(gpa, .{ .opt = .{ | |
| 7830 | if (ip.isOptionalType(new_ty)) return ip.get(gpa, tid, .{ .opt = .{ | |
| 7779 | 7831 | .ty = new_ty, |
| 7780 | 7832 | .val = .none, |
| 7781 | 7833 | } }); |
| 7782 | 7834 | |
| 7783 | 7835 | if (ip.isPointerType(new_ty)) switch (ip.indexToKey(new_ty).ptr_type.flags.size) { |
| 7784 | .One, .Many, .C => return ip.get(gpa, .{ .ptr = .{ | |
| 7836 | .One, .Many, .C => return ip.get(gpa, tid, .{ .ptr = .{ | |
| 7785 | 7837 | .ty = new_ty, |
| 7786 | 7838 | .base_addr = .int, |
| 7787 | 7839 | .byte_offset = 0, |
| 7788 | 7840 | } }), |
| 7789 | .Slice => return ip.get(gpa, .{ .slice = .{ | |
| 7841 | .Slice => return ip.get(gpa, tid, .{ .slice = .{ | |
| 7790 | 7842 | .ty = new_ty, |
| 7791 | .ptr = try ip.get(gpa, .{ .ptr = .{ | |
| 7843 | .ptr = try ip.get(gpa, tid, .{ .ptr = .{ | |
| 7792 | 7844 | .ty = ip.slicePtrType(new_ty), |
| 7793 | 7845 | .base_addr = .int, |
| 7794 | 7846 | .byte_offset = 0, |
| 7795 | 7847 | } }), |
| 7796 | .len = try ip.get(gpa, .{ .undef = .usize_type }), | |
| 7848 | .len = try ip.get(gpa, tid, .{ .undef = .usize_type }), | |
| 7797 | 7849 | } }), |
| 7798 | 7850 | }; |
| 7799 | 7851 | }, |
| 7800 | 7852 | else => switch (tags[@intFromEnum(val)]) { |
| 7801 | .func_decl => return getCoercedFuncDecl(ip, gpa, val, new_ty), | |
| 7802 | .func_instance => return getCoercedFuncInstance(ip, gpa, val, new_ty), | |
| 7853 | .func_decl => return getCoercedFuncDecl(ip, gpa, tid, val, new_ty), | |
| 7854 | .func_instance => return getCoercedFuncInstance(ip, gpa, tid, val, new_ty), | |
| 7803 | 7855 | .func_coerced => { |
| 7804 | 7856 | const extra_index = ip.items.items(.data)[@intFromEnum(val)]; |
| 7805 | 7857 | const func: Index = @enumFromInt( |
| 7806 | 7858 | ip.extra.items[extra_index + std.meta.fieldIndex(Tag.FuncCoerced, "func").?], |
| 7807 | 7859 | ); |
| 7808 | 7860 | switch (tags[@intFromEnum(func)]) { |
| 7809 | .func_decl => return getCoercedFuncDecl(ip, gpa, val, new_ty), | |
| 7810 | .func_instance => return getCoercedFuncInstance(ip, gpa, val, new_ty), | |
| 7861 | .func_decl => return getCoercedFuncDecl(ip, gpa, tid, val, new_ty), | |
| 7862 | .func_instance => return getCoercedFuncInstance(ip, gpa, tid, val, new_ty), | |
| 7811 | 7863 | else => unreachable, |
| 7812 | 7864 | } |
| 7813 | 7865 | }, |
| ... | ... | @@ -7816,9 +7868,9 @@ pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Al |
| 7816 | 7868 | } |
| 7817 | 7869 | |
| 7818 | 7870 | switch (ip.indexToKey(val)) { |
| 7819 | .undef => return ip.get(gpa, .{ .undef = new_ty }), | |
| 7871 | .undef => return ip.get(gpa, tid, .{ .undef = new_ty }), | |
| 7820 | 7872 | .extern_func => |extern_func| if (ip.isFunctionType(new_ty)) |
| 7821 | return ip.get(gpa, .{ .extern_func = .{ | |
| 7873 | return ip.get(gpa, tid, .{ .extern_func = .{ | |
| 7822 | 7874 | .ty = new_ty, |
| 7823 | 7875 | .decl = extern_func.decl, |
| 7824 | 7876 | .lib_name = extern_func.lib_name, |
| ... | ... | @@ -7827,12 +7879,12 @@ pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Al |
| 7827 | 7879 | .func => unreachable, |
| 7828 | 7880 | |
| 7829 | 7881 | .int => |int| switch (ip.indexToKey(new_ty)) { |
| 7830 | .enum_type => return ip.get(gpa, .{ .enum_tag = .{ | |
| 7882 | .enum_type => return ip.get(gpa, tid, .{ .enum_tag = .{ | |
| 7831 | 7883 | .ty = new_ty, |
| 7832 | .int = try ip.getCoerced(gpa, val, ip.loadEnumType(new_ty).tag_ty), | |
| 7884 | .int = try ip.getCoerced(gpa, tid, val, ip.loadEnumType(new_ty).tag_ty), | |
| 7833 | 7885 | } }), |
| 7834 | 7886 | .ptr_type => switch (int.storage) { |
| 7835 | inline .u64, .i64 => |int_val| return ip.get(gpa, .{ .ptr = .{ | |
| 7887 | inline .u64, .i64 => |int_val| return ip.get(gpa, tid, .{ .ptr = .{ | |
| 7836 | 7888 | .ty = new_ty, |
| 7837 | 7889 | .base_addr = .int, |
| 7838 | 7890 | .byte_offset = @intCast(int_val), |
| ... | ... | @@ -7841,7 +7893,7 @@ pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Al |
| 7841 | 7893 | .lazy_align, .lazy_size => {}, |
| 7842 | 7894 | }, |
| 7843 | 7895 | else => if (ip.isIntegerType(new_ty)) |
| 7844 | return getCoercedInts(ip, gpa, int, new_ty), | |
| 7896 | return ip.getCoercedInts(gpa, tid, int, new_ty), | |
| 7845 | 7897 | }, |
| 7846 | 7898 | .float => |float| switch (ip.indexToKey(new_ty)) { |
| 7847 | 7899 | .simple_type => |simple| switch (simple) { |
| ... | ... | @@ -7852,7 +7904,7 @@ pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Al |
| 7852 | 7904 | .f128, |
| 7853 | 7905 | .c_longdouble, |
| 7854 | 7906 | .comptime_float, |
| 7855 | => return ip.get(gpa, .{ .float = .{ | |
| 7907 | => return ip.get(gpa, tid, .{ .float = .{ | |
| 7856 | 7908 | .ty = new_ty, |
| 7857 | 7909 | .storage = float.storage, |
| 7858 | 7910 | } }), |
| ... | ... | @@ -7861,17 +7913,17 @@ pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Al |
| 7861 | 7913 | else => {}, |
| 7862 | 7914 | }, |
| 7863 | 7915 | .enum_tag => |enum_tag| if (ip.isIntegerType(new_ty)) |
| 7864 | return getCoercedInts(ip, gpa, ip.indexToKey(enum_tag.int).int, new_ty), | |
| 7916 | return ip.getCoercedInts(gpa, tid, ip.indexToKey(enum_tag.int).int, new_ty), | |
| 7865 | 7917 | .enum_literal => |enum_literal| switch (ip.indexToKey(new_ty)) { |
| 7866 | 7918 | .enum_type => { |
| 7867 | 7919 | const enum_type = ip.loadEnumType(new_ty); |
| 7868 | 7920 | const index = enum_type.nameIndex(ip, enum_literal).?; |
| 7869 | return ip.get(gpa, .{ .enum_tag = .{ | |
| 7921 | return ip.get(gpa, tid, .{ .enum_tag = .{ | |
| 7870 | 7922 | .ty = new_ty, |
| 7871 | 7923 | .int = if (enum_type.values.len != 0) |
| 7872 | 7924 | enum_type.values.get(ip)[index] |
| 7873 | 7925 | else |
| 7874 | try ip.get(gpa, .{ .int = .{ | |
| 7926 | try ip.get(gpa, tid, .{ .int = .{ | |
| 7875 | 7927 | .ty = enum_type.tag_ty, |
| 7876 | 7928 | .storage = .{ .u64 = index }, |
| 7877 | 7929 | } }), |
| ... | ... | @@ -7880,22 +7932,22 @@ pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Al |
| 7880 | 7932 | else => {}, |
| 7881 | 7933 | }, |
| 7882 | 7934 | .slice => |slice| if (ip.isPointerType(new_ty) and ip.indexToKey(new_ty).ptr_type.flags.size == .Slice) |
| 7883 | return ip.get(gpa, .{ .slice = .{ | |
| 7935 | return ip.get(gpa, tid, .{ .slice = .{ | |
| 7884 | 7936 | .ty = new_ty, |
| 7885 | .ptr = try ip.getCoerced(gpa, slice.ptr, ip.slicePtrType(new_ty)), | |
| 7937 | .ptr = try ip.getCoerced(gpa, tid, slice.ptr, ip.slicePtrType(new_ty)), | |
| 7886 | 7938 | .len = slice.len, |
| 7887 | 7939 | } }) |
| 7888 | 7940 | else if (ip.isIntegerType(new_ty)) |
| 7889 | return ip.getCoerced(gpa, slice.ptr, new_ty), | |
| 7941 | return ip.getCoerced(gpa, tid, slice.ptr, new_ty), | |
| 7890 | 7942 | .ptr => |ptr| if (ip.isPointerType(new_ty) and ip.indexToKey(new_ty).ptr_type.flags.size != .Slice) |
| 7891 | return ip.get(gpa, .{ .ptr = .{ | |
| 7943 | return ip.get(gpa, tid, .{ .ptr = .{ | |
| 7892 | 7944 | .ty = new_ty, |
| 7893 | 7945 | .base_addr = ptr.base_addr, |
| 7894 | 7946 | .byte_offset = ptr.byte_offset, |
| 7895 | 7947 | } }) |
| 7896 | 7948 | else if (ip.isIntegerType(new_ty)) |
| 7897 | 7949 | switch (ptr.base_addr) { |
| 7898 | .int => return ip.get(gpa, .{ .int = .{ | |
| 7950 | .int => return ip.get(gpa, tid, .{ .int = .{ | |
| 7899 | 7951 | .ty = .usize_type, |
| 7900 | 7952 | .storage = .{ .u64 = @intCast(ptr.byte_offset) }, |
| 7901 | 7953 | } }), |
| ... | ... | @@ -7904,44 +7956,44 @@ pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Al |
| 7904 | 7956 | .opt => |opt| switch (ip.indexToKey(new_ty)) { |
| 7905 | 7957 | .ptr_type => |ptr_type| return switch (opt.val) { |
| 7906 | 7958 | .none => switch (ptr_type.flags.size) { |
| 7907 | .One, .Many, .C => try ip.get(gpa, .{ .ptr = .{ | |
| 7959 | .One, .Many, .C => try ip.get(gpa, tid, .{ .ptr = .{ | |
| 7908 | 7960 | .ty = new_ty, |
| 7909 | 7961 | .base_addr = .int, |
| 7910 | 7962 | .byte_offset = 0, |
| 7911 | 7963 | } }), |
| 7912 | .Slice => try ip.get(gpa, .{ .slice = .{ | |
| 7964 | .Slice => try ip.get(gpa, tid, .{ .slice = .{ | |
| 7913 | 7965 | .ty = new_ty, |
| 7914 | .ptr = try ip.get(gpa, .{ .ptr = .{ | |
| 7966 | .ptr = try ip.get(gpa, tid, .{ .ptr = .{ | |
| 7915 | 7967 | .ty = ip.slicePtrType(new_ty), |
| 7916 | 7968 | .base_addr = .int, |
| 7917 | 7969 | .byte_offset = 0, |
| 7918 | 7970 | } }), |
| 7919 | .len = try ip.get(gpa, .{ .undef = .usize_type }), | |
| 7971 | .len = try ip.get(gpa, tid, .{ .undef = .usize_type }), | |
| 7920 | 7972 | } }), |
| 7921 | 7973 | }, |
| 7922 | else => |payload| try ip.getCoerced(gpa, payload, new_ty), | |
| 7974 | else => |payload| try ip.getCoerced(gpa, tid, payload, new_ty), | |
| 7923 | 7975 | }, |
| 7924 | .opt_type => |child_type| return try ip.get(gpa, .{ .opt = .{ | |
| 7976 | .opt_type => |child_type| return try ip.get(gpa, tid, .{ .opt = .{ | |
| 7925 | 7977 | .ty = new_ty, |
| 7926 | 7978 | .val = switch (opt.val) { |
| 7927 | 7979 | .none => .none, |
| 7928 | else => try ip.getCoerced(gpa, opt.val, child_type), | |
| 7980 | else => try ip.getCoerced(gpa, tid, opt.val, child_type), | |
| 7929 | 7981 | }, |
| 7930 | 7982 | } }), |
| 7931 | 7983 | else => {}, |
| 7932 | 7984 | }, |
| 7933 | 7985 | .err => |err| if (ip.isErrorSetType(new_ty)) |
| 7934 | return ip.get(gpa, .{ .err = .{ | |
| 7986 | return ip.get(gpa, tid, .{ .err = .{ | |
| 7935 | 7987 | .ty = new_ty, |
| 7936 | 7988 | .name = err.name, |
| 7937 | 7989 | } }) |
| 7938 | 7990 | else if (ip.isErrorUnionType(new_ty)) |
| 7939 | return ip.get(gpa, .{ .error_union = .{ | |
| 7991 | return ip.get(gpa, tid, .{ .error_union = .{ | |
| 7940 | 7992 | .ty = new_ty, |
| 7941 | 7993 | .val = .{ .err_name = err.name }, |
| 7942 | 7994 | } }), |
| 7943 | 7995 | .error_union => |error_union| if (ip.isErrorUnionType(new_ty)) |
| 7944 | return ip.get(gpa, .{ .error_union = .{ | |
| 7996 | return ip.get(gpa, tid, .{ .error_union = .{ | |
| 7945 | 7997 | .ty = new_ty, |
| 7946 | 7998 | .val = error_union.val, |
| 7947 | 7999 | } }), |
| ... | ... | @@ -7960,20 +8012,20 @@ pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Al |
| 7960 | 8012 | }; |
| 7961 | 8013 | if (old_ty_child != new_ty_child) break :direct; |
| 7962 | 8014 | switch (aggregate.storage) { |
| 7963 | .bytes => |bytes| return ip.get(gpa, .{ .aggregate = .{ | |
| 8015 | .bytes => |bytes| return ip.get(gpa, tid, .{ .aggregate = .{ | |
| 7964 | 8016 | .ty = new_ty, |
| 7965 | 8017 | .storage = .{ .bytes = bytes }, |
| 7966 | 8018 | } }), |
| 7967 | 8019 | .elems => |elems| { |
| 7968 | 8020 | const elems_copy = try gpa.dupe(Index, elems[0..new_len]); |
| 7969 | 8021 | defer gpa.free(elems_copy); |
| 7970 | return ip.get(gpa, .{ .aggregate = .{ | |
| 8022 | return ip.get(gpa, tid, .{ .aggregate = .{ | |
| 7971 | 8023 | .ty = new_ty, |
| 7972 | 8024 | .storage = .{ .elems = elems_copy }, |
| 7973 | 8025 | } }); |
| 7974 | 8026 | }, |
| 7975 | 8027 | .repeated_elem => |elem| { |
| 7976 | return ip.get(gpa, .{ .aggregate = .{ | |
| 8028 | return ip.get(gpa, tid, .{ .aggregate = .{ | |
| 7977 | 8029 | .ty = new_ty, |
| 7978 | 8030 | .storage = .{ .repeated_elem = elem }, |
| 7979 | 8031 | } }); |
| ... | ... | @@ -7991,7 +8043,7 @@ pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Al |
| 7991 | 8043 | // We have to intern each value here, so unfortunately we can't easily avoid |
| 7992 | 8044 | // the repeated indexToKey calls. |
| 7993 | 8045 | for (agg_elems, 0..) |*elem, index| { |
| 7994 | elem.* = try ip.get(gpa, .{ .int = .{ | |
| 8046 | elem.* = try ip.get(gpa, tid, .{ .int = .{ | |
| 7995 | 8047 | .ty = .u8_type, |
| 7996 | 8048 | .storage = .{ .u64 = bytes.at(index, ip) }, |
| 7997 | 8049 | } }); |
| ... | ... | @@ -8008,27 +8060,27 @@ pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Al |
| 8008 | 8060 | .struct_type => ip.loadStructType(new_ty).field_types.get(ip)[i], |
| 8009 | 8061 | else => unreachable, |
| 8010 | 8062 | }; |
| 8011 | elem.* = try ip.getCoerced(gpa, elem.*, new_elem_ty); | |
| 8063 | elem.* = try ip.getCoerced(gpa, tid, elem.*, new_elem_ty); | |
| 8012 | 8064 | } |
| 8013 | return ip.get(gpa, .{ .aggregate = .{ .ty = new_ty, .storage = .{ .elems = agg_elems } } }); | |
| 8065 | return ip.get(gpa, tid, .{ .aggregate = .{ .ty = new_ty, .storage = .{ .elems = agg_elems } } }); | |
| 8014 | 8066 | }, |
| 8015 | 8067 | else => {}, |
| 8016 | 8068 | } |
| 8017 | 8069 | |
| 8018 | 8070 | switch (ip.indexToKey(new_ty)) { |
| 8019 | 8071 | .opt_type => |child_type| switch (val) { |
| 8020 | .null_value => return ip.get(gpa, .{ .opt = .{ | |
| 8072 | .null_value => return ip.get(gpa, tid, .{ .opt = .{ | |
| 8021 | 8073 | .ty = new_ty, |
| 8022 | 8074 | .val = .none, |
| 8023 | 8075 | } }), |
| 8024 | else => return ip.get(gpa, .{ .opt = .{ | |
| 8076 | else => return ip.get(gpa, tid, .{ .opt = .{ | |
| 8025 | 8077 | .ty = new_ty, |
| 8026 | .val = try ip.getCoerced(gpa, val, child_type), | |
| 8078 | .val = try ip.getCoerced(gpa, tid, val, child_type), | |
| 8027 | 8079 | } }), |
| 8028 | 8080 | }, |
| 8029 | .error_union_type => |error_union_type| return ip.get(gpa, .{ .error_union = .{ | |
| 8081 | .error_union_type => |error_union_type| return ip.get(gpa, tid, .{ .error_union = .{ | |
| 8030 | 8082 | .ty = new_ty, |
| 8031 | .val = .{ .payload = try ip.getCoerced(gpa, val, error_union_type.payload_type) }, | |
| 8083 | .val = .{ .payload = try ip.getCoerced(gpa, tid, val, error_union_type.payload_type) }, | |
| 8032 | 8084 | } }), |
| 8033 | 8085 | else => {}, |
| 8034 | 8086 | } |
| ... | ... | @@ -8042,27 +8094,45 @@ pub fn getCoerced(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Al |
| 8042 | 8094 | unreachable; |
| 8043 | 8095 | } |
| 8044 | 8096 | |
| 8045 | fn getCoercedFuncDecl(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Allocator.Error!Index { | |
| 8097 | fn getCoercedFuncDecl( | |
| 8098 | ip: *InternPool, | |
| 8099 | gpa: Allocator, | |
| 8100 | tid: Zcu.PerThread.Id, | |
| 8101 | val: Index, | |
| 8102 | new_ty: Index, | |
| 8103 | ) Allocator.Error!Index { | |
| 8046 | 8104 | const datas = ip.items.items(.data); |
| 8047 | 8105 | const extra_index = datas[@intFromEnum(val)]; |
| 8048 | 8106 | const prev_ty: Index = @enumFromInt( |
| 8049 | 8107 | ip.extra.items[extra_index + std.meta.fieldIndex(Tag.FuncDecl, "ty").?], |
| 8050 | 8108 | ); |
| 8051 | 8109 | if (new_ty == prev_ty) return val; |
| 8052 | return getCoercedFunc(ip, gpa, val, new_ty); | |
| 8110 | return getCoercedFunc(ip, gpa, tid, val, new_ty); | |
| 8053 | 8111 | } |
| 8054 | 8112 | |
| 8055 | fn getCoercedFuncInstance(ip: *InternPool, gpa: Allocator, val: Index, new_ty: Index) Allocator.Error!Index { | |
| 8113 | fn getCoercedFuncInstance( | |
| 8114 | ip: *InternPool, | |
| 8115 | gpa: Allocator, | |
| 8116 | tid: Zcu.PerThread.Id, | |
| 8117 | val: Index, | |
| 8118 | new_ty: Index, | |
| 8119 | ) Allocator.Error!Index { | |
| 8056 | 8120 | const datas = ip.items.items(.data); |
| 8057 | 8121 | const extra_index = datas[@intFromEnum(val)]; |
| 8058 | 8122 | const prev_ty: Index = @enumFromInt( |
| 8059 | 8123 | ip.extra.items[extra_index + std.meta.fieldIndex(Tag.FuncInstance, "ty").?], |
| 8060 | 8124 | ); |
| 8061 | 8125 | if (new_ty == prev_ty) return val; |
| 8062 | return getCoercedFunc(ip, gpa, val, new_ty); | |
| 8126 | return getCoercedFunc(ip, gpa, tid, val, new_ty); | |
| 8063 | 8127 | } |
| 8064 | 8128 | |
| 8065 | fn getCoercedFunc(ip: *InternPool, gpa: Allocator, func: Index, ty: Index) Allocator.Error!Index { | |
| 8129 | fn getCoercedFunc( | |
| 8130 | ip: *InternPool, | |
| 8131 | gpa: Allocator, | |
| 8132 | _: Zcu.PerThread.Id, | |
| 8133 | func: Index, | |
| 8134 | ty: Index, | |
| 8135 | ) Allocator.Error!Index { | |
| 8066 | 8136 | const prev_extra_len = ip.extra.items.len; |
| 8067 | 8137 | try ip.extra.ensureUnusedCapacity(gpa, @typeInfo(Tag.FuncCoerced).Struct.fields.len); |
| 8068 | 8138 | try ip.items.ensureUnusedCapacity(gpa, 1); |
| ... | ... | @@ -8092,7 +8162,7 @@ fn getCoercedFunc(ip: *InternPool, gpa: Allocator, func: Index, ty: Index) Alloc |
| 8092 | 8162 | |
| 8093 | 8163 | /// Asserts `val` has an integer type. |
| 8094 | 8164 | /// Assumes `new_ty` is an integer type. |
| 8095 | pub fn getCoercedInts(ip: *InternPool, gpa: Allocator, int: Key.Int, new_ty: Index) Allocator.Error!Index { | |
| 8165 | pub fn getCoercedInts(ip: *InternPool, gpa: Allocator, tid: Zcu.PerThread.Id, int: Key.Int, new_ty: Index) Allocator.Error!Index { | |
| 8096 | 8166 | // The key cannot be passed directly to `get`, otherwise in the case of |
| 8097 | 8167 | // big_int storage, the limbs would be invalidated before they are read. |
| 8098 | 8168 | // Here we pre-reserve the limbs to ensure that the logic in `addInt` will |
| ... | ... | @@ -8111,7 +8181,7 @@ pub fn getCoercedInts(ip: *InternPool, gpa: Allocator, int: Key.Int, new_ty: Ind |
| 8111 | 8181 | } }; |
| 8112 | 8182 | }, |
| 8113 | 8183 | }; |
| 8114 | return ip.get(gpa, .{ .int = .{ | |
| 8184 | return ip.get(gpa, tid, .{ .int = .{ | |
| 8115 | 8185 | .ty = new_ty, |
| 8116 | 8186 | .storage = new_storage, |
| 8117 | 8187 | } }); |
src/RangeSet.zig+15-17| ... | ... | @@ -6,13 +6,11 @@ const InternPool = @import("InternPool.zig"); |
| 6 | 6 | const Type = @import("Type.zig"); |
| 7 | 7 | const Value = @import("Value.zig"); |
| 8 | 8 | const Zcu = @import("Zcu.zig"); |
| 9 | /// Deprecated. | |
| 10 | const Module = Zcu; | |
| 11 | 9 | const RangeSet = @This(); |
| 12 | 10 | const LazySrcLoc = Zcu.LazySrcLoc; |
| 13 | 11 | |
| 12 | pt: Zcu.PerThread, | |
| 14 | 13 | ranges: std.ArrayList(Range), |
| 15 | module: *Module, | |
| 16 | 14 | |
| 17 | 15 | pub const Range = struct { |
| 18 | 16 | first: InternPool.Index, |
| ... | ... | @@ -20,10 +18,10 @@ pub const Range = struct { |
| 20 | 18 | src: LazySrcLoc, |
| 21 | 19 | }; |
| 22 | 20 | |
| 23 | pub fn init(allocator: std.mem.Allocator, module: *Module) RangeSet { | |
| 21 | pub fn init(allocator: std.mem.Allocator, pt: Zcu.PerThread) RangeSet { | |
| 24 | 22 | return .{ |
| 23 | .pt = pt, | |
| 25 | 24 | .ranges = std.ArrayList(Range).init(allocator), |
| 26 | .module = module, | |
| 27 | 25 | }; |
| 28 | 26 | } |
| 29 | 27 | |
| ... | ... | @@ -37,8 +35,8 @@ pub fn add( |
| 37 | 35 | last: InternPool.Index, |
| 38 | 36 | src: LazySrcLoc, |
| 39 | 37 | ) !?LazySrcLoc { |
| 40 | const mod = self.module; | |
| 41 | const ip = &mod.intern_pool; | |
| 38 | const pt = self.pt; | |
| 39 | const ip = &pt.zcu.intern_pool; | |
| 42 | 40 | |
| 43 | 41 | const ty = ip.typeOf(first); |
| 44 | 42 | assert(ty == ip.typeOf(last)); |
| ... | ... | @@ -47,8 +45,8 @@ pub fn add( |
| 47 | 45 | assert(ty == ip.typeOf(range.first)); |
| 48 | 46 | assert(ty == ip.typeOf(range.last)); |
| 49 | 47 | |
| 50 | if (Value.fromInterned(last).compareScalar(.gte, Value.fromInterned(range.first), Type.fromInterned(ty), mod) and | |
| 51 | Value.fromInterned(first).compareScalar(.lte, Value.fromInterned(range.last), Type.fromInterned(ty), mod)) | |
| 48 | if (Value.fromInterned(last).compareScalar(.gte, Value.fromInterned(range.first), Type.fromInterned(ty), pt) and | |
| 49 | Value.fromInterned(first).compareScalar(.lte, Value.fromInterned(range.last), Type.fromInterned(ty), pt)) | |
| 52 | 50 | { |
| 53 | 51 | return range.src; // They overlap. |
| 54 | 52 | } |
| ... | ... | @@ -63,20 +61,20 @@ pub fn add( |
| 63 | 61 | } |
| 64 | 62 | |
| 65 | 63 | /// Assumes a and b do not overlap |
| 66 | fn lessThan(mod: *Module, a: Range, b: Range) bool { | |
| 67 | const ty = Type.fromInterned(mod.intern_pool.typeOf(a.first)); | |
| 68 | return Value.fromInterned(a.first).compareScalar(.lt, Value.fromInterned(b.first), ty, mod); | |
| 64 | fn lessThan(pt: Zcu.PerThread, a: Range, b: Range) bool { | |
| 65 | const ty = Type.fromInterned(pt.zcu.intern_pool.typeOf(a.first)); | |
| 66 | return Value.fromInterned(a.first).compareScalar(.lt, Value.fromInterned(b.first), ty, pt); | |
| 69 | 67 | } |
| 70 | 68 | |
| 71 | 69 | pub fn spans(self: *RangeSet, first: InternPool.Index, last: InternPool.Index) !bool { |
| 72 | const mod = self.module; | |
| 73 | const ip = &mod.intern_pool; | |
| 70 | const pt = self.pt; | |
| 71 | const ip = &pt.zcu.intern_pool; | |
| 74 | 72 | assert(ip.typeOf(first) == ip.typeOf(last)); |
| 75 | 73 | |
| 76 | 74 | if (self.ranges.items.len == 0) |
| 77 | 75 | return false; |
| 78 | 76 | |
| 79 | std.mem.sort(Range, self.ranges.items, mod, lessThan); | |
| 77 | std.mem.sort(Range, self.ranges.items, pt, lessThan); | |
| 80 | 78 | |
| 81 | 79 | if (self.ranges.items[0].first != first or |
| 82 | 80 | self.ranges.items[self.ranges.items.len - 1].last != last) |
| ... | ... | @@ -95,10 +93,10 @@ pub fn spans(self: *RangeSet, first: InternPool.Index, last: InternPool.Index) ! |
| 95 | 93 | const prev = self.ranges.items[i]; |
| 96 | 94 | |
| 97 | 95 | // prev.last + 1 == cur.first |
| 98 | try counter.copy(Value.fromInterned(prev.last).toBigInt(&space, mod)); | |
| 96 | try counter.copy(Value.fromInterned(prev.last).toBigInt(&space, pt)); | |
| 99 | 97 | try counter.addScalar(&counter, 1); |
| 100 | 98 | |
| 101 | const cur_start_int = Value.fromInterned(cur.first).toBigInt(&space, mod); | |
| 99 | const cur_start_int = Value.fromInterned(cur.first).toBigInt(&space, pt); | |
| 102 | 100 | if (!cur_start_int.eql(counter.toConst())) { |
| 103 | 101 | return false; |
| 104 | 102 | } |
src/Sema.zig+2671-2297| ... | ... | @@ -5,7 +5,7 @@ |
| 5 | 5 | //! Does type checking, comptime control flow, and safety-check generation. |
| 6 | 6 | //! This is the the heart of the Zig compiler. |
| 7 | 7 | |
| 8 | mod: *Module, | |
| 8 | pt: Zcu.PerThread, | |
| 9 | 9 | /// Alias to `mod.gpa`. |
| 10 | 10 | gpa: Allocator, |
| 11 | 11 | /// Points to the temporary arena allocator of the Sema. |
| ... | ... | @@ -146,7 +146,7 @@ const ComptimeAlloc = struct { |
| 146 | 146 | fn newComptimeAlloc(sema: *Sema, block: *Block, ty: Type, alignment: Alignment) !ComptimeAllocIndex { |
| 147 | 147 | const idx = sema.comptime_allocs.items.len; |
| 148 | 148 | try sema.comptime_allocs.append(sema.gpa, .{ |
| 149 | .val = .{ .interned = try sema.mod.intern(.{ .undef = ty.toIntern() }) }, | |
| 149 | .val = .{ .interned = try sema.pt.intern(.{ .undef = ty.toIntern() }) }, | |
| 150 | 150 | .is_const = false, |
| 151 | 151 | .alignment = alignment, |
| 152 | 152 | .runtime_index = block.runtime_index, |
| ... | ... | @@ -433,7 +433,7 @@ pub const Block = struct { |
| 433 | 433 | |
| 434 | 434 | fn explain(cr: ComptimeReason, sema: *Sema, msg: ?*Module.ErrorMsg) !void { |
| 435 | 435 | const parent = msg orelse return; |
| 436 | const mod = sema.mod; | |
| 436 | const pt = sema.pt; | |
| 437 | 437 | const prefix = "expression is evaluated at comptime because "; |
| 438 | 438 | switch (cr) { |
| 439 | 439 | .c_import => |ci| { |
| ... | ... | @@ -451,7 +451,7 @@ pub const Block = struct { |
| 451 | 451 | ret_ty_src, |
| 452 | 452 | parent, |
| 453 | 453 | prefix ++ "the function returns a comptime-only type '{}'", |
| 454 | .{rt.return_ty.fmt(mod)}, | |
| 454 | .{rt.return_ty.fmt(pt)}, | |
| 455 | 455 | ); |
| 456 | 456 | try sema.explainWhyTypeIsComptime(parent, ret_ty_src, rt.return_ty); |
| 457 | 457 | }, |
| ... | ... | @@ -538,7 +538,7 @@ pub const Block = struct { |
| 538 | 538 | } |
| 539 | 539 | |
| 540 | 540 | pub fn wantSafety(block: *const Block) bool { |
| 541 | return block.want_safety orelse switch (block.sema.mod.optimizeMode()) { | |
| 541 | return block.want_safety orelse switch (block.sema.pt.zcu.optimizeMode()) { | |
| 542 | 542 | .Debug => true, |
| 543 | 543 | .ReleaseSafe => true, |
| 544 | 544 | .ReleaseFast => false, |
| ... | ... | @@ -737,11 +737,12 @@ pub const Block = struct { |
| 737 | 737 | |
| 738 | 738 | fn addCmpVector(block: *Block, lhs: Air.Inst.Ref, rhs: Air.Inst.Ref, cmp_op: std.math.CompareOperator) !Air.Inst.Ref { |
| 739 | 739 | const sema = block.sema; |
| 740 | const mod = sema.mod; | |
| 740 | const pt = sema.pt; | |
| 741 | const mod = pt.zcu; | |
| 741 | 742 | return block.addInst(.{ |
| 742 | 743 | .tag = if (block.float_mode == .optimized) .cmp_vector_optimized else .cmp_vector, |
| 743 | 744 | .data = .{ .ty_pl = .{ |
| 744 | .ty = Air.internedToRef((try mod.vectorType(.{ | |
| 745 | .ty = Air.internedToRef((try pt.vectorType(.{ | |
| 745 | 746 | .len = sema.typeOf(lhs).vectorLen(mod), |
| 746 | 747 | .child = .bool_type, |
| 747 | 748 | })).toIntern()), |
| ... | ... | @@ -829,14 +830,14 @@ pub const Block = struct { |
| 829 | 830 | } |
| 830 | 831 | |
| 831 | 832 | pub fn ownerModule(block: Block) *Package.Module { |
| 832 | const zcu = block.sema.mod; | |
| 833 | const zcu = block.sema.pt.zcu; | |
| 833 | 834 | return zcu.namespacePtr(block.namespace).fileScope(zcu).mod; |
| 834 | 835 | } |
| 835 | 836 | |
| 836 | 837 | fn trackZir(block: *Block, inst: Zir.Inst.Index) Allocator.Error!InternPool.TrackedInst.Index { |
| 837 | 838 | const sema = block.sema; |
| 838 | 839 | const gpa = sema.gpa; |
| 839 | const zcu = sema.mod; | |
| 840 | const zcu = sema.pt.zcu; | |
| 840 | 841 | const ip = &zcu.intern_pool; |
| 841 | 842 | const file_index = block.getFileScopeIndex(zcu); |
| 842 | 843 | return ip.trackZir(gpa, file_index, inst); |
| ... | ... | @@ -992,7 +993,8 @@ fn analyzeBodyInner( |
| 992 | 993 | |
| 993 | 994 | try sema.inst_map.ensureSpaceForInstructions(sema.gpa, body); |
| 994 | 995 | |
| 995 | const zcu = sema.mod; | |
| 996 | const pt = sema.pt; | |
| 997 | const zcu = pt.zcu; | |
| 996 | 998 | const map = &sema.inst_map; |
| 997 | 999 | const tags = sema.code.instructions.items(.tag); |
| 998 | 1000 | const datas = sema.code.instructions.items(.data); |
| ... | ... | @@ -1777,7 +1779,7 @@ fn analyzeBodyInner( |
| 1777 | 1779 | const err_union_ty = sema.typeOf(err_union); |
| 1778 | 1780 | if (err_union_ty.zigTypeTag(zcu) != .ErrorUnion) { |
| 1779 | 1781 | return sema.fail(block, operand_src, "expected error union type, found '{}'", .{ |
| 1780 | err_union_ty.fmt(zcu), | |
| 1782 | err_union_ty.fmt(pt), | |
| 1781 | 1783 | }); |
| 1782 | 1784 | } |
| 1783 | 1785 | const is_non_err = try sema.analyzeIsNonErrComptimeOnly(block, operand_src, err_union); |
| ... | ... | @@ -1910,10 +1912,11 @@ pub fn toConstString( |
| 1910 | 1912 | air_inst: Air.Inst.Ref, |
| 1911 | 1913 | reason: NeededComptimeReason, |
| 1912 | 1914 | ) ![]u8 { |
| 1915 | const pt = sema.pt; | |
| 1913 | 1916 | const coerced_inst = try sema.coerce(block, Type.slice_const_u8, air_inst, src); |
| 1914 | 1917 | const slice_val = try sema.resolveConstDefinedValue(block, src, coerced_inst, reason); |
| 1915 | 1918 | const arr_val = try sema.derefSliceAsArray(block, src, slice_val, reason); |
| 1916 | return arr_val.toAllocatedBytes(arr_val.typeOf(sema.mod), sema.arena, sema.mod); | |
| 1919 | return arr_val.toAllocatedBytes(arr_val.typeOf(pt.zcu), sema.arena, pt); | |
| 1917 | 1920 | } |
| 1918 | 1921 | |
| 1919 | 1922 | pub fn resolveConstStringIntern( |
| ... | ... | @@ -1945,7 +1948,8 @@ fn resolveDestType( |
| 1945 | 1948 | strat: enum { remove_eu_opt, remove_eu, remove_opt }, |
| 1946 | 1949 | builtin_name: []const u8, |
| 1947 | 1950 | ) !Type { |
| 1948 | const mod = sema.mod; | |
| 1951 | const pt = sema.pt; | |
| 1952 | const mod = pt.zcu; | |
| 1949 | 1953 | const remove_eu = switch (strat) { |
| 1950 | 1954 | .remove_eu_opt, .remove_eu => true, |
| 1951 | 1955 | .remove_opt => false, |
| ... | ... | @@ -2062,7 +2066,8 @@ fn analyzeAsType( |
| 2062 | 2066 | } |
| 2063 | 2067 | |
| 2064 | 2068 | pub fn setupErrorReturnTrace(sema: *Sema, block: *Block, last_arg_index: usize) !void { |
| 2065 | const mod = sema.mod; | |
| 2069 | const pt = sema.pt; | |
| 2070 | const mod = pt.zcu; | |
| 2066 | 2071 | const comp = mod.comp; |
| 2067 | 2072 | const gpa = sema.gpa; |
| 2068 | 2073 | const ip = &mod.intern_pool; |
| ... | ... | @@ -2076,16 +2081,16 @@ pub fn setupErrorReturnTrace(sema: *Sema, block: *Block, last_arg_index: usize) |
| 2076 | 2081 | |
| 2077 | 2082 | // var addrs: [err_return_trace_addr_count]usize = undefined; |
| 2078 | 2083 | const err_return_trace_addr_count = 32; |
| 2079 | const addr_arr_ty = try mod.arrayType(.{ | |
| 2084 | const addr_arr_ty = try pt.arrayType(.{ | |
| 2080 | 2085 | .len = err_return_trace_addr_count, |
| 2081 | 2086 | .child = .usize_type, |
| 2082 | 2087 | }); |
| 2083 | const addrs_ptr = try err_trace_block.addTy(.alloc, try mod.singleMutPtrType(addr_arr_ty)); | |
| 2088 | const addrs_ptr = try err_trace_block.addTy(.alloc, try pt.singleMutPtrType(addr_arr_ty)); | |
| 2084 | 2089 | |
| 2085 | 2090 | // var st: StackTrace = undefined; |
| 2086 | const stack_trace_ty = try mod.getBuiltinType("StackTrace"); | |
| 2087 | try stack_trace_ty.resolveFields(mod); | |
| 2088 | const st_ptr = try err_trace_block.addTy(.alloc, try mod.singleMutPtrType(stack_trace_ty)); | |
| 2091 | const stack_trace_ty = try pt.getBuiltinType("StackTrace"); | |
| 2092 | try stack_trace_ty.resolveFields(pt); | |
| 2093 | const st_ptr = try err_trace_block.addTy(.alloc, try pt.singleMutPtrType(stack_trace_ty)); | |
| 2089 | 2094 | |
| 2090 | 2095 | // st.instruction_addresses = &addrs; |
| 2091 | 2096 | const instruction_addresses_field_name = try ip.getOrPutString(gpa, "instruction_addresses", .no_embedded_nulls); |
| ... | ... | @@ -2109,7 +2114,7 @@ pub fn setupErrorReturnTrace(sema: *Sema, block: *Block, last_arg_index: usize) |
| 2109 | 2114 | fn resolveValue(sema: *Sema, inst: Air.Inst.Ref) CompileError!?Value { |
| 2110 | 2115 | const val = (try sema.resolveValueAllowVariables(inst)) orelse return null; |
| 2111 | 2116 | if (val.isGenericPoison()) return error.GenericPoison; |
| 2112 | if (sema.mod.intern_pool.isVariable(val.toIntern())) return null; | |
| 2117 | if (sema.pt.zcu.intern_pool.isVariable(val.toIntern())) return null; | |
| 2113 | 2118 | return val; |
| 2114 | 2119 | } |
| 2115 | 2120 | |
| ... | ... | @@ -2133,7 +2138,8 @@ fn resolveDefinedValue( |
| 2133 | 2138 | src: LazySrcLoc, |
| 2134 | 2139 | air_ref: Air.Inst.Ref, |
| 2135 | 2140 | ) CompileError!?Value { |
| 2136 | const mod = sema.mod; | |
| 2141 | const pt = sema.pt; | |
| 2142 | const mod = pt.zcu; | |
| 2137 | 2143 | const val = try sema.resolveValue(air_ref) orelse return null; |
| 2138 | 2144 | if (val.isUndef(mod)) { |
| 2139 | 2145 | return sema.failWithUseOfUndef(block, src); |
| ... | ... | @@ -2150,7 +2156,7 @@ fn resolveConstDefinedValue( |
| 2150 | 2156 | reason: NeededComptimeReason, |
| 2151 | 2157 | ) CompileError!Value { |
| 2152 | 2158 | const val = try sema.resolveConstValue(block, src, air_ref, reason); |
| 2153 | if (val.isUndef(sema.mod)) return sema.failWithUseOfUndef(block, src); | |
| 2159 | if (val.isUndef(sema.pt.zcu)) return sema.failWithUseOfUndef(block, src); | |
| 2154 | 2160 | return val; |
| 2155 | 2161 | } |
| 2156 | 2162 | |
| ... | ... | @@ -2164,7 +2170,7 @@ fn resolveValueResolveLazy(sema: *Sema, inst: Air.Inst.Ref) CompileError!?Value |
| 2164 | 2170 | /// Lazy values are recursively resolved. |
| 2165 | 2171 | fn resolveValueIntable(sema: *Sema, inst: Air.Inst.Ref) CompileError!?Value { |
| 2166 | 2172 | const val = (try sema.resolveValue(inst)) orelse return null; |
| 2167 | if (sema.mod.intern_pool.getBackingAddrTag(val.toIntern())) |addr| switch (addr) { | |
| 2173 | if (sema.pt.zcu.intern_pool.getBackingAddrTag(val.toIntern())) |addr| switch (addr) { | |
| 2168 | 2174 | .decl, .anon_decl, .comptime_alloc, .comptime_field => return null, |
| 2169 | 2175 | .int => {}, |
| 2170 | 2176 | .eu_payload, .opt_payload, .arr_elem, .field => unreachable, |
| ... | ... | @@ -2174,6 +2180,7 @@ fn resolveValueIntable(sema: *Sema, inst: Air.Inst.Ref) CompileError!?Value { |
| 2174 | 2180 | |
| 2175 | 2181 | /// Returns all InternPool keys representing values, including `variable`, `undef`, and `generic_poison`. |
| 2176 | 2182 | fn resolveValueAllowVariables(sema: *Sema, inst: Air.Inst.Ref) CompileError!?Value { |
| 2183 | const pt = sema.pt; | |
| 2177 | 2184 | assert(inst != .none); |
| 2178 | 2185 | // First section of indexes correspond to a set number of constant values. |
| 2179 | 2186 | if (@intFromEnum(inst) < InternPool.static_len) { |
| ... | ... | @@ -2184,7 +2191,7 @@ fn resolveValueAllowVariables(sema: *Sema, inst: Air.Inst.Ref) CompileError!?Val |
| 2184 | 2191 | if (try sema.typeHasOnePossibleValue(sema.typeOf(inst))) |opv| { |
| 2185 | 2192 | if (inst.toInterned()) |ip_index| { |
| 2186 | 2193 | const val = Value.fromInterned(ip_index); |
| 2187 | if (val.getVariable(sema.mod) != null) return val; | |
| 2194 | if (val.getVariable(pt.zcu) != null) return val; | |
| 2188 | 2195 | } |
| 2189 | 2196 | return opv; |
| 2190 | 2197 | } |
| ... | ... | @@ -2196,7 +2203,7 @@ fn resolveValueAllowVariables(sema: *Sema, inst: Air.Inst.Ref) CompileError!?Val |
| 2196 | 2203 | } |
| 2197 | 2204 | }; |
| 2198 | 2205 | const val = Value.fromInterned(ip_index); |
| 2199 | if (val.isPtrToThreadLocal(sema.mod)) return null; | |
| 2206 | if (val.isPtrToThreadLocal(pt.zcu)) return null; | |
| 2200 | 2207 | return val; |
| 2201 | 2208 | } |
| 2202 | 2209 | |
| ... | ... | @@ -2225,7 +2232,7 @@ pub fn resolveFinalDeclValue( |
| 2225 | 2232 | }); |
| 2226 | 2233 | }; |
| 2227 | 2234 | if (val.isGenericPoison()) return error.GenericPoison; |
| 2228 | if (val.canMutateComptimeVarState(sema.mod)) { | |
| 2235 | if (val.canMutateComptimeVarState(sema.pt.zcu)) { | |
| 2229 | 2236 | return sema.fail(block, src, "global variable contains reference to comptime var", .{}); |
| 2230 | 2237 | } |
| 2231 | 2238 | return val; |
| ... | ... | @@ -2254,19 +2261,20 @@ fn failWithDivideByZero(sema: *Sema, block: *Block, src: LazySrcLoc) CompileErro |
| 2254 | 2261 | } |
| 2255 | 2262 | |
| 2256 | 2263 | fn failWithModRemNegative(sema: *Sema, block: *Block, src: LazySrcLoc, lhs_ty: Type, rhs_ty: Type) CompileError { |
| 2264 | const pt = sema.pt; | |
| 2257 | 2265 | return sema.fail(block, src, "remainder division with '{}' and '{}': signed integers and floats must use @rem or @mod", .{ |
| 2258 | lhs_ty.fmt(sema.mod), rhs_ty.fmt(sema.mod), | |
| 2266 | lhs_ty.fmt(pt), rhs_ty.fmt(pt), | |
| 2259 | 2267 | }); |
| 2260 | 2268 | } |
| 2261 | 2269 | |
| 2262 | 2270 | fn failWithExpectedOptionalType(sema: *Sema, block: *Block, src: LazySrcLoc, non_optional_ty: Type) CompileError { |
| 2263 | const mod = sema.mod; | |
| 2271 | const pt = sema.pt; | |
| 2264 | 2272 | const msg = msg: { |
| 2265 | 2273 | const msg = try sema.errMsg(src, "expected optional type, found '{}'", .{ |
| 2266 | non_optional_ty.fmt(mod), | |
| 2274 | non_optional_ty.fmt(pt), | |
| 2267 | 2275 | }); |
| 2268 | 2276 | errdefer msg.destroy(sema.gpa); |
| 2269 | if (non_optional_ty.zigTypeTag(mod) == .ErrorUnion) { | |
| 2277 | if (non_optional_ty.zigTypeTag(pt.zcu) == .ErrorUnion) { | |
| 2270 | 2278 | try sema.errNote(src, msg, "consider using 'try', 'catch', or 'if'", .{}); |
| 2271 | 2279 | } |
| 2272 | 2280 | try addDeclaredHereNote(sema, msg, non_optional_ty); |
| ... | ... | @@ -2276,14 +2284,14 @@ fn failWithExpectedOptionalType(sema: *Sema, block: *Block, src: LazySrcLoc, non |
| 2276 | 2284 | } |
| 2277 | 2285 | |
| 2278 | 2286 | fn failWithArrayInitNotSupported(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError { |
| 2279 | const mod = sema.mod; | |
| 2287 | const pt = sema.pt; | |
| 2280 | 2288 | const msg = msg: { |
| 2281 | 2289 | const msg = try sema.errMsg(src, "type '{}' does not support array initialization syntax", .{ |
| 2282 | ty.fmt(mod), | |
| 2290 | ty.fmt(pt), | |
| 2283 | 2291 | }); |
| 2284 | 2292 | errdefer msg.destroy(sema.gpa); |
| 2285 | if (ty.isSlice(mod)) { | |
| 2286 | try sema.errNote(src, msg, "inferred array length is specified with an underscore: '[_]{}'", .{ty.elemType2(mod).fmt(mod)}); | |
| 2293 | if (ty.isSlice(pt.zcu)) { | |
| 2294 | try sema.errNote(src, msg, "inferred array length is specified with an underscore: '[_]{}'", .{ty.elemType2(pt.zcu).fmt(pt)}); | |
| 2287 | 2295 | } |
| 2288 | 2296 | break :msg msg; |
| 2289 | 2297 | }; |
| ... | ... | @@ -2291,8 +2299,9 @@ fn failWithArrayInitNotSupported(sema: *Sema, block: *Block, src: LazySrcLoc, ty |
| 2291 | 2299 | } |
| 2292 | 2300 | |
| 2293 | 2301 | fn failWithStructInitNotSupported(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError { |
| 2302 | const pt = sema.pt; | |
| 2294 | 2303 | return sema.fail(block, src, "type '{}' does not support struct initialization syntax", .{ |
| 2295 | ty.fmt(sema.mod), | |
| 2304 | ty.fmt(pt), | |
| 2296 | 2305 | }); |
| 2297 | 2306 | } |
| 2298 | 2307 | |
| ... | ... | @@ -2303,17 +2312,19 @@ fn failWithErrorSetCodeMissing( |
| 2303 | 2312 | dest_err_set_ty: Type, |
| 2304 | 2313 | src_err_set_ty: Type, |
| 2305 | 2314 | ) CompileError { |
| 2315 | const pt = sema.pt; | |
| 2306 | 2316 | return sema.fail(block, src, "expected type '{}', found type '{}'", .{ |
| 2307 | dest_err_set_ty.fmt(sema.mod), src_err_set_ty.fmt(sema.mod), | |
| 2317 | dest_err_set_ty.fmt(pt), src_err_set_ty.fmt(pt), | |
| 2308 | 2318 | }); |
| 2309 | 2319 | } |
| 2310 | 2320 | |
| 2311 | 2321 | fn failWithIntegerOverflow(sema: *Sema, block: *Block, src: LazySrcLoc, int_ty: Type, val: Value, vector_index: usize) CompileError { |
| 2312 | const zcu = sema.mod; | |
| 2322 | const pt = sema.pt; | |
| 2323 | const zcu = pt.zcu; | |
| 2313 | 2324 | if (int_ty.zigTypeTag(zcu) == .Vector) { |
| 2314 | 2325 | const msg = msg: { |
| 2315 | 2326 | const msg = try sema.errMsg(src, "overflow of vector type '{}' with value '{}'", .{ |
| 2316 | int_ty.fmt(zcu), val.fmtValue(zcu, sema), | |
| 2327 | int_ty.fmt(pt), val.fmtValue(pt, sema), | |
| 2317 | 2328 | }); |
| 2318 | 2329 | errdefer msg.destroy(sema.gpa); |
| 2319 | 2330 | try sema.errNote(src, msg, "when computing vector element at index '{d}'", .{vector_index}); |
| ... | ... | @@ -2322,12 +2333,13 @@ fn failWithIntegerOverflow(sema: *Sema, block: *Block, src: LazySrcLoc, int_ty: |
| 2322 | 2333 | return sema.failWithOwnedErrorMsg(block, msg); |
| 2323 | 2334 | } |
| 2324 | 2335 | return sema.fail(block, src, "overflow of integer type '{}' with value '{}'", .{ |
| 2325 | int_ty.fmt(zcu), val.fmtValue(zcu, sema), | |
| 2336 | int_ty.fmt(pt), val.fmtValue(pt, sema), | |
| 2326 | 2337 | }); |
| 2327 | 2338 | } |
| 2328 | 2339 | |
| 2329 | 2340 | fn failWithInvalidComptimeFieldStore(sema: *Sema, block: *Block, init_src: LazySrcLoc, container_ty: Type, field_index: usize) CompileError { |
| 2330 | const mod = sema.mod; | |
| 2341 | const pt = sema.pt; | |
| 2342 | const mod = pt.zcu; | |
| 2331 | 2343 | const msg = msg: { |
| 2332 | 2344 | const msg = try sema.errMsg(init_src, "value stored in comptime field does not match the default value of the field", .{}); |
| 2333 | 2345 | errdefer msg.destroy(sema.gpa); |
| ... | ... | @@ -2358,14 +2370,15 @@ fn failWithInvalidFieldAccess( |
| 2358 | 2370 | object_ty: Type, |
| 2359 | 2371 | field_name: InternPool.NullTerminatedString, |
| 2360 | 2372 | ) CompileError { |
| 2361 | const mod = sema.mod; | |
| 2373 | const pt = sema.pt; | |
| 2374 | const mod = pt.zcu; | |
| 2362 | 2375 | const inner_ty = if (object_ty.isSinglePointer(mod)) object_ty.childType(mod) else object_ty; |
| 2363 | 2376 | |
| 2364 | 2377 | if (inner_ty.zigTypeTag(mod) == .Optional) opt: { |
| 2365 | 2378 | const child_ty = inner_ty.optionalChild(mod); |
| 2366 | 2379 | if (!typeSupportsFieldAccess(mod, child_ty, field_name)) break :opt; |
| 2367 | 2380 | const msg = msg: { |
| 2368 | const msg = try sema.errMsg(src, "optional type '{}' does not support field access", .{object_ty.fmt(sema.mod)}); | |
| 2381 | const msg = try sema.errMsg(src, "optional type '{}' does not support field access", .{object_ty.fmt(pt)}); | |
| 2369 | 2382 | errdefer msg.destroy(sema.gpa); |
| 2370 | 2383 | try sema.errNote(src, msg, "consider using '.?', 'orelse', or 'if'", .{}); |
| 2371 | 2384 | break :msg msg; |
| ... | ... | @@ -2375,14 +2388,14 @@ fn failWithInvalidFieldAccess( |
| 2375 | 2388 | const child_ty = inner_ty.errorUnionPayload(mod); |
| 2376 | 2389 | if (!typeSupportsFieldAccess(mod, child_ty, field_name)) break :err; |
| 2377 | 2390 | const msg = msg: { |
| 2378 | const msg = try sema.errMsg(src, "error union type '{}' does not support field access", .{object_ty.fmt(sema.mod)}); | |
| 2391 | const msg = try sema.errMsg(src, "error union type '{}' does not support field access", .{object_ty.fmt(pt)}); | |
| 2379 | 2392 | errdefer msg.destroy(sema.gpa); |
| 2380 | 2393 | try sema.errNote(src, msg, "consider using 'try', 'catch', or 'if'", .{}); |
| 2381 | 2394 | break :msg msg; |
| 2382 | 2395 | }; |
| 2383 | 2396 | return sema.failWithOwnedErrorMsg(block, msg); |
| 2384 | 2397 | } |
| 2385 | return sema.fail(block, src, "type '{}' does not support field access", .{object_ty.fmt(sema.mod)}); | |
| 2398 | return sema.fail(block, src, "type '{}' does not support field access", .{object_ty.fmt(pt)}); | |
| 2386 | 2399 | } |
| 2387 | 2400 | |
| 2388 | 2401 | fn typeSupportsFieldAccess(mod: *const Module, ty: Type, field_name: InternPool.NullTerminatedString) bool { |
| ... | ... | @@ -2408,7 +2421,8 @@ fn failWithComptimeErrorRetTrace( |
| 2408 | 2421 | src: LazySrcLoc, |
| 2409 | 2422 | name: InternPool.NullTerminatedString, |
| 2410 | 2423 | ) CompileError { |
| 2411 | const mod = sema.mod; | |
| 2424 | const pt = sema.pt; | |
| 2425 | const mod = pt.zcu; | |
| 2412 | 2426 | const msg = msg: { |
| 2413 | 2427 | const msg = try sema.errMsg(src, "caught unexpected error '{}'", .{name.fmt(&mod.intern_pool)}); |
| 2414 | 2428 | errdefer msg.destroy(sema.gpa); |
| ... | ... | @@ -2430,7 +2444,7 @@ pub fn errNote( |
| 2430 | 2444 | comptime format: []const u8, |
| 2431 | 2445 | args: anytype, |
| 2432 | 2446 | ) error{OutOfMemory}!void { |
| 2433 | return sema.mod.errNote(src, parent, format, args); | |
| 2447 | return sema.pt.zcu.errNote(src, parent, format, args); | |
| 2434 | 2448 | } |
| 2435 | 2449 | |
| 2436 | 2450 | fn addFieldErrNote( |
| ... | ... | @@ -2442,8 +2456,7 @@ fn addFieldErrNote( |
| 2442 | 2456 | args: anytype, |
| 2443 | 2457 | ) !void { |
| 2444 | 2458 | @setCold(true); |
| 2445 | const zcu = sema.mod; | |
| 2446 | const type_src = container_ty.srcLocOrNull(zcu) orelse return; | |
| 2459 | const type_src = container_ty.srcLocOrNull(sema.pt.zcu) orelse return; | |
| 2447 | 2460 | const field_src: LazySrcLoc = .{ |
| 2448 | 2461 | .base_node_inst = type_src.base_node_inst, |
| 2449 | 2462 | .offset = .{ .container_field_name = @intCast(field_index) }, |
| ... | ... | @@ -2480,7 +2493,7 @@ pub fn fail( |
| 2480 | 2493 | pub fn failWithOwnedErrorMsg(sema: *Sema, block: ?*Block, err_msg: *Module.ErrorMsg) error{ AnalysisFail, OutOfMemory } { |
| 2481 | 2494 | @setCold(true); |
| 2482 | 2495 | const gpa = sema.gpa; |
| 2483 | const mod = sema.mod; | |
| 2496 | const mod = sema.pt.zcu; | |
| 2484 | 2497 | const ip = &mod.intern_pool; |
| 2485 | 2498 | |
| 2486 | 2499 | if (build_options.enable_debug_extensions and mod.comp.debug_compile_errors) { |
| ... | ... | @@ -2545,8 +2558,7 @@ fn reparentOwnedErrorMsg( |
| 2545 | 2558 | comptime format: []const u8, |
| 2546 | 2559 | args: anytype, |
| 2547 | 2560 | ) !void { |
| 2548 | const mod = sema.mod; | |
| 2549 | const msg_str = try std.fmt.allocPrint(mod.gpa, format, args); | |
| 2561 | const msg_str = try std.fmt.allocPrint(sema.gpa, format, args); | |
| 2550 | 2562 | |
| 2551 | 2563 | const orig_notes = msg.notes.len; |
| 2552 | 2564 | msg.notes = try sema.gpa.realloc(msg.notes, orig_notes + 1); |
| ... | ... | @@ -2630,16 +2642,16 @@ fn analyzeAsInt( |
| 2630 | 2642 | dest_ty: Type, |
| 2631 | 2643 | reason: NeededComptimeReason, |
| 2632 | 2644 | ) !u64 { |
| 2633 | const mod = sema.mod; | |
| 2634 | 2645 | const coerced = try sema.coerce(block, dest_ty, air_ref, src); |
| 2635 | 2646 | const val = try sema.resolveConstDefinedValue(block, src, coerced, reason); |
| 2636 | return (try val.getUnsignedIntAdvanced(mod, .sema)).?; | |
| 2647 | return (try val.getUnsignedIntAdvanced(sema.pt, .sema)).?; | |
| 2637 | 2648 | } |
| 2638 | 2649 | |
| 2639 | 2650 | /// Given a ZIR extra index which points to a list of `Zir.Inst.Capture`, |
| 2640 | 2651 | /// resolves this into a list of `InternPool.CaptureValue` allocated by `arena`. |
| 2641 | 2652 | fn getCaptures(sema: *Sema, block: *Block, type_src: LazySrcLoc, extra_index: usize, captures_len: u32) ![]InternPool.CaptureValue { |
| 2642 | const zcu = sema.mod; | |
| 2653 | const pt = sema.pt; | |
| 2654 | const zcu = pt.zcu; | |
| 2643 | 2655 | const ip = &zcu.intern_pool; |
| 2644 | 2656 | const parent_captures: InternPool.CaptureValue.Slice = zcu.namespacePtr(block.namespace).getType(zcu).getCaptures(zcu); |
| 2645 | 2657 | |
| ... | ... | @@ -2706,7 +2718,7 @@ fn wrapWipTy(sema: *Sema, wip_ty: anytype) @TypeOf(wip_ty) { |
| 2706 | 2718 | if (sema.builtin_type_target_index == .none) return wip_ty; |
| 2707 | 2719 | var new = wip_ty; |
| 2708 | 2720 | new.index = sema.builtin_type_target_index; |
| 2709 | sema.mod.intern_pool.resolveBuiltinType(new.index, wip_ty.index); | |
| 2721 | sema.pt.zcu.intern_pool.resolveBuiltinType(new.index, wip_ty.index); | |
| 2710 | 2722 | return new; |
| 2711 | 2723 | } |
| 2712 | 2724 | |
| ... | ... | @@ -2714,7 +2726,8 @@ fn wrapWipTy(sema: *Sema, wip_ty: anytype) @TypeOf(wip_ty) { |
| 2714 | 2726 | /// considered outdated on this update. If so, remove it from the pool |
| 2715 | 2727 | /// and return `true`. |
| 2716 | 2728 | fn maybeRemoveOutdatedType(sema: *Sema, ty: InternPool.Index) !bool { |
| 2717 | const zcu = sema.mod; | |
| 2729 | const pt = sema.pt; | |
| 2730 | const zcu = pt.zcu; | |
| 2718 | 2731 | |
| 2719 | 2732 | if (!zcu.comp.debug_incremental) return false; |
| 2720 | 2733 | |
| ... | ... | @@ -2737,7 +2750,8 @@ fn zirStructDecl( |
| 2737 | 2750 | extended: Zir.Inst.Extended.InstData, |
| 2738 | 2751 | inst: Zir.Inst.Index, |
| 2739 | 2752 | ) CompileError!Air.Inst.Ref { |
| 2740 | const mod = sema.mod; | |
| 2753 | const pt = sema.pt; | |
| 2754 | const mod = pt.zcu; | |
| 2741 | 2755 | const gpa = sema.gpa; |
| 2742 | 2756 | const ip = &mod.intern_pool; |
| 2743 | 2757 | const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small); |
| ... | ... | @@ -2796,10 +2810,10 @@ fn zirStructDecl( |
| 2796 | 2810 | .captures = captures, |
| 2797 | 2811 | } }, |
| 2798 | 2812 | }; |
| 2799 | const wip_ty = sema.wrapWipTy(switch (try ip.getStructType(gpa, struct_init)) { | |
| 2813 | const wip_ty = sema.wrapWipTy(switch (try ip.getStructType(gpa, pt.tid, struct_init)) { | |
| 2800 | 2814 | .existing => |ty| wip: { |
| 2801 | 2815 | if (!try sema.maybeRemoveOutdatedType(ty)) return Air.internedToRef(ty); |
| 2802 | break :wip (try ip.getStructType(gpa, struct_init)).wip; | |
| 2816 | break :wip (try ip.getStructType(gpa, pt.tid, struct_init)).wip; | |
| 2803 | 2817 | }, |
| 2804 | 2818 | .wip => |wip| wip, |
| 2805 | 2819 | }); |
| ... | ... | @@ -2815,7 +2829,7 @@ fn zirStructDecl( |
| 2815 | 2829 | mod.declPtr(new_decl_index).owns_tv = true; |
| 2816 | 2830 | errdefer mod.abortAnonDecl(new_decl_index); |
| 2817 | 2831 | |
| 2818 | if (sema.mod.comp.debug_incremental) { | |
| 2832 | if (pt.zcu.comp.debug_incremental) { | |
| 2819 | 2833 | try ip.addDependency( |
| 2820 | 2834 | sema.gpa, |
| 2821 | 2835 | AnalUnit.wrap(.{ .decl = new_decl_index }), |
| ... | ... | @@ -2836,7 +2850,7 @@ fn zirStructDecl( |
| 2836 | 2850 | try mod.scanNamespace(ns, decls, mod.declPtr(new_decl_index)); |
| 2837 | 2851 | } |
| 2838 | 2852 | |
| 2839 | try mod.finalizeAnonDecl(new_decl_index); | |
| 2853 | try pt.finalizeAnonDecl(new_decl_index); | |
| 2840 | 2854 | try mod.comp.work_queue.writeItem(.{ .resolve_type_fully = wip_ty.index }); |
| 2841 | 2855 | try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .decl = new_decl_index })); |
| 2842 | 2856 | return Air.internedToRef(wip_ty.finish(ip, new_decl_index, new_namespace_index)); |
| ... | ... | @@ -2850,7 +2864,8 @@ fn createAnonymousDeclTypeNamed( |
| 2850 | 2864 | anon_prefix: []const u8, |
| 2851 | 2865 | inst: ?Zir.Inst.Index, |
| 2852 | 2866 | ) !InternPool.DeclIndex { |
| 2853 | const zcu = sema.mod; | |
| 2867 | const pt = sema.pt; | |
| 2868 | const zcu = pt.zcu; | |
| 2854 | 2869 | const ip = &zcu.intern_pool; |
| 2855 | 2870 | const gpa = sema.gpa; |
| 2856 | 2871 | const namespace = block.namespace; |
| ... | ... | @@ -2892,7 +2907,7 @@ fn createAnonymousDeclTypeNamed( |
| 2892 | 2907 | // some tooling may not support very long symbol names. |
| 2893 | 2908 | try writer.print("{}", .{Value.fmtValueFull(.{ |
| 2894 | 2909 | .val = arg_val, |
| 2895 | .mod = zcu, | |
| 2910 | .pt = pt, | |
| 2896 | 2911 | .opt_sema = sema, |
| 2897 | 2912 | .depth = 1, |
| 2898 | 2913 | })}); |
| ... | ... | @@ -2953,7 +2968,8 @@ fn zirEnumDecl( |
| 2953 | 2968 | const tracy = trace(@src()); |
| 2954 | 2969 | defer tracy.end(); |
| 2955 | 2970 | |
| 2956 | const mod = sema.mod; | |
| 2971 | const pt = sema.pt; | |
| 2972 | const mod = pt.zcu; | |
| 2957 | 2973 | const gpa = sema.gpa; |
| 2958 | 2974 | const ip = &mod.intern_pool; |
| 2959 | 2975 | const small: Zir.Inst.EnumDecl.Small = @bitCast(extended.small); |
| ... | ... | @@ -3026,10 +3042,10 @@ fn zirEnumDecl( |
| 3026 | 3042 | .captures = captures, |
| 3027 | 3043 | } }, |
| 3028 | 3044 | }; |
| 3029 | const wip_ty = sema.wrapWipTy(switch (try ip.getEnumType(gpa, enum_init)) { | |
| 3045 | const wip_ty = sema.wrapWipTy(switch (try ip.getEnumType(gpa, pt.tid, enum_init)) { | |
| 3030 | 3046 | .existing => |ty| wip: { |
| 3031 | 3047 | if (!try sema.maybeRemoveOutdatedType(ty)) return Air.internedToRef(ty); |
| 3032 | break :wip (try ip.getEnumType(gpa, enum_init)).wip; | |
| 3048 | break :wip (try ip.getEnumType(gpa, pt.tid, enum_init)).wip; | |
| 3033 | 3049 | }, |
| 3034 | 3050 | .wip => |wip| wip, |
| 3035 | 3051 | }); |
| ... | ... | @@ -3051,7 +3067,7 @@ fn zirEnumDecl( |
| 3051 | 3067 | new_decl.owns_tv = true; |
| 3052 | 3068 | errdefer if (!done) mod.abortAnonDecl(new_decl_index); |
| 3053 | 3069 | |
| 3054 | if (sema.mod.comp.debug_incremental) { | |
| 3070 | if (pt.zcu.comp.debug_incremental) { | |
| 3055 | 3071 | try mod.intern_pool.addDependency( |
| 3056 | 3072 | gpa, |
| 3057 | 3073 | AnalUnit.wrap(.{ .decl = new_decl_index }), |
| ... | ... | @@ -3118,21 +3134,21 @@ fn zirEnumDecl( |
| 3118 | 3134 | if (tag_type_ref != .none) { |
| 3119 | 3135 | const ty = try sema.resolveType(&enum_block, tag_ty_src, tag_type_ref); |
| 3120 | 3136 | if (ty.zigTypeTag(mod) != .Int and ty.zigTypeTag(mod) != .ComptimeInt) { |
| 3121 | return sema.fail(&enum_block, tag_ty_src, "expected integer tag type, found '{}'", .{ty.fmt(sema.mod)}); | |
| 3137 | return sema.fail(&enum_block, tag_ty_src, "expected integer tag type, found '{}'", .{ty.fmt(pt)}); | |
| 3122 | 3138 | } |
| 3123 | 3139 | break :ty ty; |
| 3124 | 3140 | } else if (fields_len == 0) { |
| 3125 | break :ty try mod.intType(.unsigned, 0); | |
| 3141 | break :ty try pt.intType(.unsigned, 0); | |
| 3126 | 3142 | } else { |
| 3127 | 3143 | const bits = std.math.log2_int_ceil(usize, fields_len); |
| 3128 | break :ty try mod.intType(.unsigned, bits); | |
| 3144 | break :ty try pt.intType(.unsigned, bits); | |
| 3129 | 3145 | } |
| 3130 | 3146 | }; |
| 3131 | 3147 | |
| 3132 | 3148 | wip_ty.setTagTy(ip, int_tag_ty.toIntern()); |
| 3133 | 3149 | |
| 3134 | 3150 | if (small.nonexhaustive and int_tag_ty.toIntern() != .comptime_int_type) { |
| 3135 | if (fields_len > 1 and std.math.log2_int(u64, fields_len) == int_tag_ty.bitSize(mod)) { | |
| 3151 | if (fields_len > 1 and std.math.log2_int(u64, fields_len) == int_tag_ty.bitSize(pt)) { | |
| 3136 | 3152 | return sema.fail(block, src, "non-exhaustive enum specifies every value", .{}); |
| 3137 | 3153 | } |
| 3138 | 3154 | } |
| ... | ... | @@ -3171,7 +3187,7 @@ fn zirEnumDecl( |
| 3171 | 3187 | .needed_comptime_reason = "enum tag value must be comptime-known", |
| 3172 | 3188 | }); |
| 3173 | 3189 | if (!(try sema.intFitsInType(last_tag_val.?, int_tag_ty, null))) break :overflow true; |
| 3174 | last_tag_val = try mod.getCoerced(last_tag_val.?, int_tag_ty); | |
| 3190 | last_tag_val = try pt.getCoerced(last_tag_val.?, int_tag_ty); | |
| 3175 | 3191 | if (wip_ty.nextField(&mod.intern_pool, field_name, last_tag_val.?.toIntern())) |conflict| { |
| 3176 | 3192 | assert(conflict.kind == .value); // AstGen validated names are unique |
| 3177 | 3193 | const other_field_src: LazySrcLoc = .{ |
| ... | ... | @@ -3179,7 +3195,7 @@ fn zirEnumDecl( |
| 3179 | 3195 | .offset = .{ .container_field_value = conflict.prev_field_idx }, |
| 3180 | 3196 | }; |
| 3181 | 3197 | const msg = msg: { |
| 3182 | const msg = try sema.errMsg(value_src, "enum tag value {} already taken", .{last_tag_val.?.fmtValue(sema.mod, sema)}); | |
| 3198 | const msg = try sema.errMsg(value_src, "enum tag value {} already taken", .{last_tag_val.?.fmtValue(pt, sema)}); | |
| 3183 | 3199 | errdefer msg.destroy(gpa); |
| 3184 | 3200 | try sema.errNote(other_field_src, msg, "other occurrence here", .{}); |
| 3185 | 3201 | break :msg msg; |
| ... | ... | @@ -3190,9 +3206,9 @@ fn zirEnumDecl( |
| 3190 | 3206 | } else if (any_values) overflow: { |
| 3191 | 3207 | var overflow: ?usize = null; |
| 3192 | 3208 | last_tag_val = if (last_tag_val) |val| |
| 3193 | try sema.intAdd(val, try mod.intValue(int_tag_ty, 1), int_tag_ty, &overflow) | |
| 3209 | try sema.intAdd(val, try pt.intValue(int_tag_ty, 1), int_tag_ty, &overflow) | |
| 3194 | 3210 | else |
| 3195 | try mod.intValue(int_tag_ty, 0); | |
| 3211 | try pt.intValue(int_tag_ty, 0); | |
| 3196 | 3212 | if (overflow != null) break :overflow true; |
| 3197 | 3213 | if (wip_ty.nextField(&mod.intern_pool, field_name, last_tag_val.?.toIntern())) |conflict| { |
| 3198 | 3214 | assert(conflict.kind == .value); // AstGen validated names are unique |
| ... | ... | @@ -3201,7 +3217,7 @@ fn zirEnumDecl( |
| 3201 | 3217 | .offset = .{ .container_field_value = conflict.prev_field_idx }, |
| 3202 | 3218 | }; |
| 3203 | 3219 | const msg = msg: { |
| 3204 | const msg = try sema.errMsg(value_src, "enum tag value {} already taken", .{last_tag_val.?.fmtValue(sema.mod, sema)}); | |
| 3220 | const msg = try sema.errMsg(value_src, "enum tag value {} already taken", .{last_tag_val.?.fmtValue(pt, sema)}); | |
| 3205 | 3221 | errdefer msg.destroy(gpa); |
| 3206 | 3222 | try sema.errNote(other_field_src, msg, "other occurrence here", .{}); |
| 3207 | 3223 | break :msg msg; |
| ... | ... | @@ -3211,21 +3227,21 @@ fn zirEnumDecl( |
| 3211 | 3227 | break :overflow false; |
| 3212 | 3228 | } else overflow: { |
| 3213 | 3229 | assert(wip_ty.nextField(&mod.intern_pool, field_name, .none) == null); |
| 3214 | last_tag_val = try mod.intValue(Type.comptime_int, field_i); | |
| 3230 | last_tag_val = try pt.intValue(Type.comptime_int, field_i); | |
| 3215 | 3231 | if (!try sema.intFitsInType(last_tag_val.?, int_tag_ty, null)) break :overflow true; |
| 3216 | last_tag_val = try mod.getCoerced(last_tag_val.?, int_tag_ty); | |
| 3232 | last_tag_val = try pt.getCoerced(last_tag_val.?, int_tag_ty); | |
| 3217 | 3233 | break :overflow false; |
| 3218 | 3234 | }; |
| 3219 | 3235 | |
| 3220 | 3236 | if (tag_overflow) { |
| 3221 | 3237 | const msg = try sema.errMsg(value_src, "enumeration value '{}' too large for type '{}'", .{ |
| 3222 | last_tag_val.?.fmtValue(mod, sema), int_tag_ty.fmt(mod), | |
| 3238 | last_tag_val.?.fmtValue(pt, sema), int_tag_ty.fmt(pt), | |
| 3223 | 3239 | }); |
| 3224 | 3240 | return sema.failWithOwnedErrorMsg(block, msg); |
| 3225 | 3241 | } |
| 3226 | 3242 | } |
| 3227 | 3243 | |
| 3228 | try mod.finalizeAnonDecl(new_decl_index); | |
| 3244 | try pt.finalizeAnonDecl(new_decl_index); | |
| 3229 | 3245 | return Air.internedToRef(wip_ty.index); |
| 3230 | 3246 | } |
| 3231 | 3247 | |
| ... | ... | @@ -3238,7 +3254,8 @@ fn zirUnionDecl( |
| 3238 | 3254 | const tracy = trace(@src()); |
| 3239 | 3255 | defer tracy.end(); |
| 3240 | 3256 | |
| 3241 | const mod = sema.mod; | |
| 3257 | const pt = sema.pt; | |
| 3258 | const mod = pt.zcu; | |
| 3242 | 3259 | const gpa = sema.gpa; |
| 3243 | 3260 | const ip = &mod.intern_pool; |
| 3244 | 3261 | const small: Zir.Inst.UnionDecl.Small = @bitCast(extended.small); |
| ... | ... | @@ -3298,10 +3315,10 @@ fn zirUnionDecl( |
| 3298 | 3315 | .captures = captures, |
| 3299 | 3316 | } }, |
| 3300 | 3317 | }; |
| 3301 | const wip_ty = sema.wrapWipTy(switch (try ip.getUnionType(gpa, union_init)) { | |
| 3318 | const wip_ty = sema.wrapWipTy(switch (try ip.getUnionType(gpa, pt.tid, union_init)) { | |
| 3302 | 3319 | .existing => |ty| wip: { |
| 3303 | 3320 | if (!try sema.maybeRemoveOutdatedType(ty)) return Air.internedToRef(ty); |
| 3304 | break :wip (try ip.getUnionType(gpa, union_init)).wip; | |
| 3321 | break :wip (try ip.getUnionType(gpa, pt.tid, union_init)).wip; | |
| 3305 | 3322 | }, |
| 3306 | 3323 | .wip => |wip| wip, |
| 3307 | 3324 | }); |
| ... | ... | @@ -3317,7 +3334,7 @@ fn zirUnionDecl( |
| 3317 | 3334 | mod.declPtr(new_decl_index).owns_tv = true; |
| 3318 | 3335 | errdefer mod.abortAnonDecl(new_decl_index); |
| 3319 | 3336 | |
| 3320 | if (sema.mod.comp.debug_incremental) { | |
| 3337 | if (pt.zcu.comp.debug_incremental) { | |
| 3321 | 3338 | try mod.intern_pool.addDependency( |
| 3322 | 3339 | gpa, |
| 3323 | 3340 | AnalUnit.wrap(.{ .decl = new_decl_index }), |
| ... | ... | @@ -3338,7 +3355,7 @@ fn zirUnionDecl( |
| 3338 | 3355 | try mod.scanNamespace(ns, decls, mod.declPtr(new_decl_index)); |
| 3339 | 3356 | } |
| 3340 | 3357 | |
| 3341 | try mod.finalizeAnonDecl(new_decl_index); | |
| 3358 | try pt.finalizeAnonDecl(new_decl_index); | |
| 3342 | 3359 | try mod.comp.work_queue.writeItem(.{ .resolve_type_fully = wip_ty.index }); |
| 3343 | 3360 | try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .decl = new_decl_index })); |
| 3344 | 3361 | return Air.internedToRef(wip_ty.finish(ip, new_decl_index, new_namespace_index)); |
| ... | ... | @@ -3353,7 +3370,8 @@ fn zirOpaqueDecl( |
| 3353 | 3370 | const tracy = trace(@src()); |
| 3354 | 3371 | defer tracy.end(); |
| 3355 | 3372 | |
| 3356 | const mod = sema.mod; | |
| 3373 | const pt = sema.pt; | |
| 3374 | const mod = pt.zcu; | |
| 3357 | 3375 | const gpa = sema.gpa; |
| 3358 | 3376 | const ip = &mod.intern_pool; |
| 3359 | 3377 | |
| ... | ... | @@ -3387,10 +3405,10 @@ fn zirOpaqueDecl( |
| 3387 | 3405 | } }, |
| 3388 | 3406 | }; |
| 3389 | 3407 | // No `wrapWipTy` needed as no std.builtin types are opaque. |
| 3390 | const wip_ty = switch (try ip.getOpaqueType(gpa, opaque_init)) { | |
| 3408 | const wip_ty = switch (try ip.getOpaqueType(gpa, pt.tid, opaque_init)) { | |
| 3391 | 3409 | .existing => |ty| wip: { |
| 3392 | 3410 | if (!try sema.maybeRemoveOutdatedType(ty)) return Air.internedToRef(ty); |
| 3393 | break :wip (try ip.getOpaqueType(gpa, opaque_init)).wip; | |
| 3411 | break :wip (try ip.getOpaqueType(gpa, pt.tid, opaque_init)).wip; | |
| 3394 | 3412 | }, |
| 3395 | 3413 | .wip => |wip| wip, |
| 3396 | 3414 | }; |
| ... | ... | @@ -3406,7 +3424,7 @@ fn zirOpaqueDecl( |
| 3406 | 3424 | mod.declPtr(new_decl_index).owns_tv = true; |
| 3407 | 3425 | errdefer mod.abortAnonDecl(new_decl_index); |
| 3408 | 3426 | |
| 3409 | if (sema.mod.comp.debug_incremental) { | |
| 3427 | if (pt.zcu.comp.debug_incremental) { | |
| 3410 | 3428 | try ip.addDependency( |
| 3411 | 3429 | gpa, |
| 3412 | 3430 | AnalUnit.wrap(.{ .decl = new_decl_index }), |
| ... | ... | @@ -3426,7 +3444,7 @@ fn zirOpaqueDecl( |
| 3426 | 3444 | try mod.scanNamespace(ns, decls, mod.declPtr(new_decl_index)); |
| 3427 | 3445 | } |
| 3428 | 3446 | |
| 3429 | try mod.finalizeAnonDecl(new_decl_index); | |
| 3447 | try pt.finalizeAnonDecl(new_decl_index); | |
| 3430 | 3448 | |
| 3431 | 3449 | return Air.internedToRef(wip_ty.finish(ip, new_decl_index, new_namespace_index)); |
| 3432 | 3450 | } |
| ... | ... | @@ -3438,7 +3456,8 @@ fn zirErrorSetDecl( |
| 3438 | 3456 | const tracy = trace(@src()); |
| 3439 | 3457 | defer tracy.end(); |
| 3440 | 3458 | |
| 3441 | const mod = sema.mod; | |
| 3459 | const pt = sema.pt; | |
| 3460 | const mod = pt.zcu; | |
| 3442 | 3461 | const gpa = sema.gpa; |
| 3443 | 3462 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 3444 | 3463 | const extra = sema.code.extraData(Zir.Inst.ErrorSetDecl, inst_data.payload_index); |
| ... | ... | @@ -3457,20 +3476,22 @@ fn zirErrorSetDecl( |
| 3457 | 3476 | assert(!result.found_existing); // verified in AstGen |
| 3458 | 3477 | } |
| 3459 | 3478 | |
| 3460 | return Air.internedToRef((try mod.errorSetFromUnsortedNames(names.keys())).toIntern()); | |
| 3479 | return Air.internedToRef((try pt.errorSetFromUnsortedNames(names.keys())).toIntern()); | |
| 3461 | 3480 | } |
| 3462 | 3481 | |
| 3463 | 3482 | fn zirRetPtr(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref { |
| 3464 | 3483 | const tracy = trace(@src()); |
| 3465 | 3484 | defer tracy.end(); |
| 3466 | 3485 | |
| 3486 | const pt = sema.pt; | |
| 3487 | ||
| 3467 | 3488 | if (block.is_comptime or try sema.typeRequiresComptime(sema.fn_ret_ty)) { |
| 3468 | try sema.fn_ret_ty.resolveFields(sema.mod); | |
| 3489 | try sema.fn_ret_ty.resolveFields(pt); | |
| 3469 | 3490 | return sema.analyzeComptimeAlloc(block, sema.fn_ret_ty, .none); |
| 3470 | 3491 | } |
| 3471 | 3492 | |
| 3472 | const target = sema.mod.getTarget(); | |
| 3473 | const ptr_type = try sema.mod.ptrTypeSema(.{ | |
| 3493 | const target = pt.zcu.getTarget(); | |
| 3494 | const ptr_type = try pt.ptrTypeSema(.{ | |
| 3474 | 3495 | .child = sema.fn_ret_ty.toIntern(), |
| 3475 | 3496 | .flags = .{ .address_space = target_util.defaultAddressSpace(target, .local) }, |
| 3476 | 3497 | }); |
| ... | ... | @@ -3511,7 +3532,8 @@ fn ensureResultUsed( |
| 3511 | 3532 | ty: Type, |
| 3512 | 3533 | src: LazySrcLoc, |
| 3513 | 3534 | ) CompileError!void { |
| 3514 | const mod = sema.mod; | |
| 3535 | const pt = sema.pt; | |
| 3536 | const mod = pt.zcu; | |
| 3515 | 3537 | switch (ty.zigTypeTag(mod)) { |
| 3516 | 3538 | .Void, .NoReturn => return, |
| 3517 | 3539 | .ErrorSet => return sema.fail(block, src, "error set is ignored", .{}), |
| ... | ... | @@ -3526,7 +3548,7 @@ fn ensureResultUsed( |
| 3526 | 3548 | }, |
| 3527 | 3549 | else => { |
| 3528 | 3550 | const msg = msg: { |
| 3529 | const msg = try sema.errMsg(src, "value of type '{}' ignored", .{ty.fmt(sema.mod)}); | |
| 3551 | const msg = try sema.errMsg(src, "value of type '{}' ignored", .{ty.fmt(pt)}); | |
| 3530 | 3552 | errdefer msg.destroy(sema.gpa); |
| 3531 | 3553 | try sema.errNote(src, msg, "all non-void values must be used", .{}); |
| 3532 | 3554 | try sema.errNote(src, msg, "to discard the value, assign it to '_'", .{}); |
| ... | ... | @@ -3541,7 +3563,8 @@ fn zirEnsureResultNonError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com |
| 3541 | 3563 | const tracy = trace(@src()); |
| 3542 | 3564 | defer tracy.end(); |
| 3543 | 3565 | |
| 3544 | const mod = sema.mod; | |
| 3566 | const pt = sema.pt; | |
| 3567 | const mod = pt.zcu; | |
| 3545 | 3568 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 3546 | 3569 | const operand = try sema.resolveInst(inst_data.operand); |
| 3547 | 3570 | const src = block.nodeOffset(inst_data.src_node); |
| ... | ... | @@ -3565,7 +3588,8 @@ fn zirEnsureErrUnionPayloadVoid(sema: *Sema, block: *Block, inst: Zir.Inst.Index |
| 3565 | 3588 | const tracy = trace(@src()); |
| 3566 | 3589 | defer tracy.end(); |
| 3567 | 3590 | |
| 3568 | const mod = sema.mod; | |
| 3591 | const pt = sema.pt; | |
| 3592 | const mod = pt.zcu; | |
| 3569 | 3593 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 3570 | 3594 | const src = block.nodeOffset(inst_data.src_node); |
| 3571 | 3595 | const operand = try sema.resolveInst(inst_data.operand); |
| ... | ... | @@ -3604,7 +3628,8 @@ fn indexablePtrLen( |
| 3604 | 3628 | src: LazySrcLoc, |
| 3605 | 3629 | object: Air.Inst.Ref, |
| 3606 | 3630 | ) CompileError!Air.Inst.Ref { |
| 3607 | const mod = sema.mod; | |
| 3631 | const pt = sema.pt; | |
| 3632 | const mod = pt.zcu; | |
| 3608 | 3633 | const object_ty = sema.typeOf(object); |
| 3609 | 3634 | const is_pointer_to = object_ty.isSinglePointer(mod); |
| 3610 | 3635 | const indexable_ty = if (is_pointer_to) object_ty.childType(mod) else object_ty; |
| ... | ... | @@ -3619,7 +3644,8 @@ fn indexablePtrLenOrNone( |
| 3619 | 3644 | src: LazySrcLoc, |
| 3620 | 3645 | operand: Air.Inst.Ref, |
| 3621 | 3646 | ) CompileError!Air.Inst.Ref { |
| 3622 | const mod = sema.mod; | |
| 3647 | const pt = sema.pt; | |
| 3648 | const mod = pt.zcu; | |
| 3623 | 3649 | const operand_ty = sema.typeOf(operand); |
| 3624 | 3650 | try checkMemOperand(sema, block, src, operand_ty); |
| 3625 | 3651 | if (operand_ty.ptrSize(mod) == .Many) return .none; |
| ... | ... | @@ -3632,6 +3658,7 @@ fn zirAllocExtended( |
| 3632 | 3658 | block: *Block, |
| 3633 | 3659 | extended: Zir.Inst.Extended.InstData, |
| 3634 | 3660 | ) CompileError!Air.Inst.Ref { |
| 3661 | const pt = sema.pt; | |
| 3635 | 3662 | const gpa = sema.gpa; |
| 3636 | 3663 | const extra = sema.code.extraData(Zir.Inst.AllocExtended, extended.operand); |
| 3637 | 3664 | const ty_src = block.src(.{ .node_offset_var_decl_ty = extra.data.src_node }); |
| ... | ... | @@ -3673,9 +3700,9 @@ fn zirAllocExtended( |
| 3673 | 3700 | if (!small.is_const) { |
| 3674 | 3701 | try sema.validateVarType(block, ty_src, var_ty, false); |
| 3675 | 3702 | } |
| 3676 | const target = sema.mod.getTarget(); | |
| 3677 | try var_ty.resolveLayout(sema.mod); | |
| 3678 | const ptr_type = try sema.mod.ptrTypeSema(.{ | |
| 3703 | const target = pt.zcu.getTarget(); | |
| 3704 | try var_ty.resolveLayout(pt); | |
| 3705 | const ptr_type = try sema.pt.ptrTypeSema(.{ | |
| 3679 | 3706 | .child = var_ty.toIntern(), |
| 3680 | 3707 | .flags = .{ |
| 3681 | 3708 | .alignment = alignment, |
| ... | ... | @@ -3717,7 +3744,8 @@ fn zirAllocComptime(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr |
| 3717 | 3744 | } |
| 3718 | 3745 | |
| 3719 | 3746 | fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 3720 | const mod = sema.mod; | |
| 3747 | const pt = sema.pt; | |
| 3748 | const mod = pt.zcu; | |
| 3721 | 3749 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 3722 | 3750 | const alloc = try sema.resolveInst(inst_data.operand); |
| 3723 | 3751 | const alloc_ty = sema.typeOf(alloc); |
| ... | ... | @@ -3749,7 +3777,7 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 3749 | 3777 | assert(ptr.byte_offset == 0); |
| 3750 | 3778 | const alloc_index = ptr.base_addr.comptime_alloc; |
| 3751 | 3779 | const ct_alloc = sema.getComptimeAlloc(alloc_index); |
| 3752 | const interned = try ct_alloc.val.intern(mod, sema.arena); | |
| 3780 | const interned = try ct_alloc.val.intern(pt, sema.arena); | |
| 3753 | 3781 | if (interned.canMutateComptimeVarState(mod)) { |
| 3754 | 3782 | // Preserve the comptime alloc, just make the pointer const. |
| 3755 | 3783 | ct_alloc.val = .{ .interned = interned.toIntern() }; |
| ... | ... | @@ -3757,7 +3785,7 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 3757 | 3785 | return sema.makePtrConst(block, alloc); |
| 3758 | 3786 | } else { |
| 3759 | 3787 | // Promote the constant to an anon decl. |
| 3760 | const new_mut_ptr = Air.internedToRef(try mod.intern(.{ .ptr = .{ | |
| 3788 | const new_mut_ptr = Air.internedToRef(try pt.intern(.{ .ptr = .{ | |
| 3761 | 3789 | .ty = alloc_ty.toIntern(), |
| 3762 | 3790 | .base_addr = .{ .anon_decl = .{ |
| 3763 | 3791 | .val = interned.toIntern(), |
| ... | ... | @@ -3778,7 +3806,7 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 3778 | 3806 | // The value was initialized through RLS, so we didn't detect the runtime condition earlier. |
| 3779 | 3807 | // TODO: source location of runtime control flow |
| 3780 | 3808 | const init_src = block.src(.{ .node_offset_bin_rhs = inst_data.src_node }); |
| 3781 | return sema.fail(block, init_src, "value with comptime-only type '{}' depends on runtime control flow", .{elem_ty.fmt(mod)}); | |
| 3809 | return sema.fail(block, init_src, "value with comptime-only type '{}' depends on runtime control flow", .{elem_ty.fmt(pt)}); | |
| 3782 | 3810 | } |
| 3783 | 3811 | |
| 3784 | 3812 | // This is a runtime value. |
| ... | ... | @@ -3788,7 +3816,8 @@ fn zirMakePtrConst(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 3788 | 3816 | /// If `alloc` is an inferred allocation, `resolved_inferred_ty` is taken to be its resolved |
| 3789 | 3817 | /// type. Otherwise, it may be `null`, and the type will be inferred from `alloc`. |
| 3790 | 3818 | fn resolveComptimeKnownAllocPtr(sema: *Sema, block: *Block, alloc: Air.Inst.Ref, resolved_alloc_ty: ?Type) CompileError!?InternPool.Index { |
| 3791 | const zcu = sema.mod; | |
| 3819 | const pt = sema.pt; | |
| 3820 | const zcu = pt.zcu; | |
| 3792 | 3821 | |
| 3793 | 3822 | const alloc_ty = resolved_alloc_ty orelse sema.typeOf(alloc); |
| 3794 | 3823 | const ptr_info = alloc_ty.ptrInfo(zcu); |
| ... | ... | @@ -3831,7 +3860,7 @@ fn resolveComptimeKnownAllocPtr(sema: *Sema, block: *Block, alloc: Air.Inst.Ref, |
| 3831 | 3860 | |
| 3832 | 3861 | const ct_alloc = try sema.newComptimeAlloc(block, elem_ty, ptr_info.flags.alignment); |
| 3833 | 3862 | |
| 3834 | const alloc_ptr = try zcu.intern(.{ .ptr = .{ | |
| 3863 | const alloc_ptr = try pt.intern(.{ .ptr = .{ | |
| 3835 | 3864 | .ty = alloc_ty.toIntern(), |
| 3836 | 3865 | .base_addr = .{ .comptime_alloc = ct_alloc }, |
| 3837 | 3866 | .byte_offset = 0, |
| ... | ... | @@ -3909,7 +3938,7 @@ fn resolveComptimeKnownAllocPtr(sema: *Sema, block: *Block, alloc: Air.Inst.Ref, |
| 3909 | 3938 | const idx_val = (try sema.resolveValue(data.rhs)).?; |
| 3910 | 3939 | break :blk .{ |
| 3911 | 3940 | data.lhs, |
| 3912 | .{ .elem = try idx_val.toUnsignedIntSema(zcu) }, | |
| 3941 | .{ .elem = try idx_val.toUnsignedIntSema(pt) }, | |
| 3913 | 3942 | }; |
| 3914 | 3943 | }, |
| 3915 | 3944 | .bitcast => .{ |
| ... | ... | @@ -3935,32 +3964,32 @@ fn resolveComptimeKnownAllocPtr(sema: *Sema, block: *Block, alloc: Air.Inst.Ref, |
| 3935 | 3964 | }; |
| 3936 | 3965 | const new_ptr_ty = tmp_air.typeOfIndex(air_ptr, &zcu.intern_pool).toIntern(); |
| 3937 | 3966 | const new_ptr = switch (method) { |
| 3938 | .same_addr => try zcu.intern_pool.getCoerced(sema.gpa, decl_parent_ptr, new_ptr_ty), | |
| 3967 | .same_addr => try zcu.intern_pool.getCoerced(sema.gpa, pt.tid, decl_parent_ptr, new_ptr_ty), | |
| 3939 | 3968 | .opt_payload => ptr: { |
| 3940 | 3969 | // Set the optional to non-null at comptime. |
| 3941 | 3970 | // If the payload is OPV, we must use that value instead of undef. |
| 3942 | 3971 | const opt_ty = Value.fromInterned(decl_parent_ptr).typeOf(zcu).childType(zcu); |
| 3943 | 3972 | const payload_ty = opt_ty.optionalChild(zcu); |
| 3944 | const payload_val = try sema.typeHasOnePossibleValue(payload_ty) orelse try zcu.undefValue(payload_ty); | |
| 3945 | const opt_val = try zcu.intern(.{ .opt = .{ | |
| 3973 | const payload_val = try sema.typeHasOnePossibleValue(payload_ty) orelse try pt.undefValue(payload_ty); | |
| 3974 | const opt_val = try pt.intern(.{ .opt = .{ | |
| 3946 | 3975 | .ty = opt_ty.toIntern(), |
| 3947 | 3976 | .val = payload_val.toIntern(), |
| 3948 | 3977 | } }); |
| 3949 | 3978 | try sema.storePtrVal(block, LazySrcLoc.unneeded, Value.fromInterned(decl_parent_ptr), Value.fromInterned(opt_val), opt_ty); |
| 3950 | break :ptr (try Value.fromInterned(decl_parent_ptr).ptrOptPayload(zcu)).toIntern(); | |
| 3979 | break :ptr (try Value.fromInterned(decl_parent_ptr).ptrOptPayload(pt)).toIntern(); | |
| 3951 | 3980 | }, |
| 3952 | 3981 | .eu_payload => ptr: { |
| 3953 | 3982 | // Set the error union to non-error at comptime. |
| 3954 | 3983 | // If the payload is OPV, we must use that value instead of undef. |
| 3955 | 3984 | const eu_ty = Value.fromInterned(decl_parent_ptr).typeOf(zcu).childType(zcu); |
| 3956 | 3985 | const payload_ty = eu_ty.errorUnionPayload(zcu); |
| 3957 | const payload_val = try sema.typeHasOnePossibleValue(payload_ty) orelse try zcu.undefValue(payload_ty); | |
| 3958 | const eu_val = try zcu.intern(.{ .error_union = .{ | |
| 3986 | const payload_val = try sema.typeHasOnePossibleValue(payload_ty) orelse try pt.undefValue(payload_ty); | |
| 3987 | const eu_val = try pt.intern(.{ .error_union = .{ | |
| 3959 | 3988 | .ty = eu_ty.toIntern(), |
| 3960 | 3989 | .val = .{ .payload = payload_val.toIntern() }, |
| 3961 | 3990 | } }); |
| 3962 | 3991 | try sema.storePtrVal(block, LazySrcLoc.unneeded, Value.fromInterned(decl_parent_ptr), Value.fromInterned(eu_val), eu_ty); |
| 3963 | break :ptr (try Value.fromInterned(decl_parent_ptr).ptrEuPayload(zcu)).toIntern(); | |
| 3992 | break :ptr (try Value.fromInterned(decl_parent_ptr).ptrEuPayload(pt)).toIntern(); | |
| 3964 | 3993 | }, |
| 3965 | 3994 | .field => |idx| ptr: { |
| 3966 | 3995 | const maybe_union_ty = Value.fromInterned(decl_parent_ptr).typeOf(zcu).childType(zcu); |
| ... | ... | @@ -3969,14 +3998,14 @@ fn resolveComptimeKnownAllocPtr(sema: *Sema, block: *Block, alloc: Air.Inst.Ref, |
| 3969 | 3998 | // If the payload is OPV, there will not be a payload store, so we store that value. |
| 3970 | 3999 | // Otherwise, there will be a payload store to process later, so undef will suffice. |
| 3971 | 4000 | const payload_ty = Type.fromInterned(union_obj.field_types.get(&zcu.intern_pool)[idx]); |
| 3972 | const payload_val = try sema.typeHasOnePossibleValue(payload_ty) orelse try zcu.undefValue(payload_ty); | |
| 3973 | const tag_val = try zcu.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), idx); | |
| 3974 | const store_val = try zcu.unionValue(maybe_union_ty, tag_val, payload_val); | |
| 4001 | const payload_val = try sema.typeHasOnePossibleValue(payload_ty) orelse try pt.undefValue(payload_ty); | |
| 4002 | const tag_val = try pt.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), idx); | |
| 4003 | const store_val = try pt.unionValue(maybe_union_ty, tag_val, payload_val); | |
| 3975 | 4004 | try sema.storePtrVal(block, LazySrcLoc.unneeded, Value.fromInterned(decl_parent_ptr), store_val, maybe_union_ty); |
| 3976 | 4005 | } |
| 3977 | break :ptr (try Value.fromInterned(decl_parent_ptr).ptrField(idx, zcu)).toIntern(); | |
| 4006 | break :ptr (try Value.fromInterned(decl_parent_ptr).ptrField(idx, pt)).toIntern(); | |
| 3978 | 4007 | }, |
| 3979 | .elem => |idx| (try Value.fromInterned(decl_parent_ptr).ptrElem(idx, zcu)).toIntern(), | |
| 4008 | .elem => |idx| (try Value.fromInterned(decl_parent_ptr).ptrElem(idx, pt)).toIntern(), | |
| 3980 | 4009 | }; |
| 3981 | 4010 | try ptr_mapping.put(air_ptr, new_ptr); |
| 3982 | 4011 | } |
| ... | ... | @@ -4020,7 +4049,8 @@ fn finishResolveComptimeKnownAllocPtr( |
| 4020 | 4049 | alloc_inst: Air.Inst.Index, |
| 4021 | 4050 | comptime_info: MaybeComptimeAlloc, |
| 4022 | 4051 | ) CompileError!?InternPool.Index { |
| 4023 | const zcu = sema.mod; | |
| 4052 | const pt = sema.pt; | |
| 4053 | const zcu = pt.zcu; | |
| 4024 | 4054 | |
| 4025 | 4055 | // We're almost done - we have the resolved comptime value. We just need to |
| 4026 | 4056 | // eliminate the now-dead runtime instructions. |
| ... | ... | @@ -4041,19 +4071,19 @@ fn finishResolveComptimeKnownAllocPtr( |
| 4041 | 4071 | |
| 4042 | 4072 | if (Value.fromInterned(result_val).canMutateComptimeVarState(zcu)) { |
| 4043 | 4073 | const alloc_index = existing_comptime_alloc orelse a: { |
| 4044 | const idx = try sema.newComptimeAlloc(block, alloc_ty.childType(zcu), alloc_ty.ptrAlignment(zcu)); | |
| 4074 | const idx = try sema.newComptimeAlloc(block, alloc_ty.childType(zcu), alloc_ty.ptrAlignment(pt)); | |
| 4045 | 4075 | const alloc = sema.getComptimeAlloc(idx); |
| 4046 | 4076 | alloc.val = .{ .interned = result_val }; |
| 4047 | 4077 | break :a idx; |
| 4048 | 4078 | }; |
| 4049 | 4079 | sema.getComptimeAlloc(alloc_index).is_const = true; |
| 4050 | return try zcu.intern(.{ .ptr = .{ | |
| 4080 | return try pt.intern(.{ .ptr = .{ | |
| 4051 | 4081 | .ty = alloc_ty.toIntern(), |
| 4052 | 4082 | .base_addr = .{ .comptime_alloc = alloc_index }, |
| 4053 | 4083 | .byte_offset = 0, |
| 4054 | 4084 | } }); |
| 4055 | 4085 | } else { |
| 4056 | return try zcu.intern(.{ .ptr = .{ | |
| 4086 | return try pt.intern(.{ .ptr = .{ | |
| 4057 | 4087 | .ty = alloc_ty.toIntern(), |
| 4058 | 4088 | .base_addr = .{ .anon_decl = .{ |
| 4059 | 4089 | .orig_ty = alloc_ty.toIntern(), |
| ... | ... | @@ -4065,9 +4095,9 @@ fn finishResolveComptimeKnownAllocPtr( |
| 4065 | 4095 | } |
| 4066 | 4096 | |
| 4067 | 4097 | fn makePtrTyConst(sema: *Sema, ptr_ty: Type) CompileError!Type { |
| 4068 | var ptr_info = ptr_ty.ptrInfo(sema.mod); | |
| 4098 | var ptr_info = ptr_ty.ptrInfo(sema.pt.zcu); | |
| 4069 | 4099 | ptr_info.flags.is_const = true; |
| 4070 | return sema.mod.ptrTypeSema(ptr_info); | |
| 4100 | return sema.pt.ptrTypeSema(ptr_info); | |
| 4071 | 4101 | } |
| 4072 | 4102 | |
| 4073 | 4103 | fn makePtrConst(sema: *Sema, block: *Block, alloc: Air.Inst.Ref) CompileError!Air.Inst.Ref { |
| ... | ... | @@ -4076,7 +4106,7 @@ fn makePtrConst(sema: *Sema, block: *Block, alloc: Air.Inst.Ref) CompileError!Ai |
| 4076 | 4106 | |
| 4077 | 4107 | // Detect if a comptime value simply needs to have its type changed. |
| 4078 | 4108 | if (try sema.resolveValue(alloc)) |val| { |
| 4079 | return Air.internedToRef((try sema.mod.getCoerced(val, const_ptr_ty)).toIntern()); | |
| 4109 | return Air.internedToRef((try sema.pt.getCoerced(val, const_ptr_ty)).toIntern()); | |
| 4080 | 4110 | } |
| 4081 | 4111 | |
| 4082 | 4112 | return block.addBitCast(const_ptr_ty, alloc); |
| ... | ... | @@ -4103,14 +4133,16 @@ fn zirAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 4103 | 4133 | const tracy = trace(@src()); |
| 4104 | 4134 | defer tracy.end(); |
| 4105 | 4135 | |
| 4136 | const pt = sema.pt; | |
| 4137 | ||
| 4106 | 4138 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 4107 | 4139 | const ty_src = block.src(.{ .node_offset_var_decl_ty = inst_data.src_node }); |
| 4108 | 4140 | const var_ty = try sema.resolveType(block, ty_src, inst_data.operand); |
| 4109 | 4141 | if (block.is_comptime) { |
| 4110 | 4142 | return sema.analyzeComptimeAlloc(block, var_ty, .none); |
| 4111 | 4143 | } |
| 4112 | const target = sema.mod.getTarget(); | |
| 4113 | const ptr_type = try sema.mod.ptrTypeSema(.{ | |
| 4144 | const target = pt.zcu.getTarget(); | |
| 4145 | const ptr_type = try pt.ptrTypeSema(.{ | |
| 4114 | 4146 | .child = var_ty.toIntern(), |
| 4115 | 4147 | .flags = .{ .address_space = target_util.defaultAddressSpace(target, .local) }, |
| 4116 | 4148 | }); |
| ... | ... | @@ -4125,6 +4157,8 @@ fn zirAllocMut(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 4125 | 4157 | const tracy = trace(@src()); |
| 4126 | 4158 | defer tracy.end(); |
| 4127 | 4159 | |
| 4160 | const pt = sema.pt; | |
| 4161 | ||
| 4128 | 4162 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 4129 | 4163 | const ty_src = block.src(.{ .node_offset_var_decl_ty = inst_data.src_node }); |
| 4130 | 4164 | const var_ty = try sema.resolveType(block, ty_src, inst_data.operand); |
| ... | ... | @@ -4132,8 +4166,8 @@ fn zirAllocMut(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 4132 | 4166 | return sema.analyzeComptimeAlloc(block, var_ty, .none); |
| 4133 | 4167 | } |
| 4134 | 4168 | try sema.validateVarType(block, ty_src, var_ty, false); |
| 4135 | const target = sema.mod.getTarget(); | |
| 4136 | const ptr_type = try sema.mod.ptrTypeSema(.{ | |
| 4169 | const target = pt.zcu.getTarget(); | |
| 4170 | const ptr_type = try pt.ptrTypeSema(.{ | |
| 4137 | 4171 | .child = var_ty.toIntern(), |
| 4138 | 4172 | .flags = .{ .address_space = target_util.defaultAddressSpace(target, .local) }, |
| 4139 | 4173 | }); |
| ... | ... | @@ -4181,7 +4215,8 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com |
| 4181 | 4215 | const tracy = trace(@src()); |
| 4182 | 4216 | defer tracy.end(); |
| 4183 | 4217 | |
| 4184 | const mod = sema.mod; | |
| 4218 | const pt = sema.pt; | |
| 4219 | const mod = pt.zcu; | |
| 4185 | 4220 | const gpa = sema.gpa; |
| 4186 | 4221 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 4187 | 4222 | const src = block.nodeOffset(inst_data.src_node); |
| ... | ... | @@ -4206,7 +4241,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com |
| 4206 | 4241 | .anon_decl => |a| a.val, |
| 4207 | 4242 | .comptime_alloc => |i| val: { |
| 4208 | 4243 | const alloc = sema.getComptimeAlloc(i); |
| 4209 | break :val (try alloc.val.intern(mod, sema.arena)).toIntern(); | |
| 4244 | break :val (try alloc.val.intern(pt, sema.arena)).toIntern(); | |
| 4210 | 4245 | }, |
| 4211 | 4246 | else => unreachable, |
| 4212 | 4247 | }; |
| ... | ... | @@ -4232,7 +4267,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com |
| 4232 | 4267 | } |
| 4233 | 4268 | const final_elem_ty = try sema.resolvePeerTypes(block, ty_src, peer_vals, .none); |
| 4234 | 4269 | |
| 4235 | const final_ptr_ty = try mod.ptrTypeSema(.{ | |
| 4270 | const final_ptr_ty = try pt.ptrTypeSema(.{ | |
| 4236 | 4271 | .child = final_elem_ty.toIntern(), |
| 4237 | 4272 | .flags = .{ |
| 4238 | 4273 | .alignment = ia1.alignment, |
| ... | ... | @@ -4244,7 +4279,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com |
| 4244 | 4279 | try sema.validateVarType(block, ty_src, final_elem_ty, false); |
| 4245 | 4280 | } else if (try sema.resolveComptimeKnownAllocPtr(block, ptr, final_ptr_ty)) |ptr_val| { |
| 4246 | 4281 | const const_ptr_ty = try sema.makePtrTyConst(final_ptr_ty); |
| 4247 | const new_const_ptr = try mod.getCoerced(Value.fromInterned(ptr_val), const_ptr_ty); | |
| 4282 | const new_const_ptr = try pt.getCoerced(Value.fromInterned(ptr_val), const_ptr_ty); | |
| 4248 | 4283 | |
| 4249 | 4284 | // Remap the ZIR operand to the resolved pointer value |
| 4250 | 4285 | sema.inst_map.putAssumeCapacity(inst_data.operand.toIndex().?, Air.internedToRef(new_const_ptr.toIntern())); |
| ... | ... | @@ -4252,7 +4287,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com |
| 4252 | 4287 | // Unless the block is comptime, `alloc_inferred` always produces |
| 4253 | 4288 | // a runtime constant. The final inferred type needs to be |
| 4254 | 4289 | // fully resolved so it can be lowered in codegen. |
| 4255 | try final_elem_ty.resolveFully(mod); | |
| 4290 | try final_elem_ty.resolveFully(pt); | |
| 4256 | 4291 | |
| 4257 | 4292 | return; |
| 4258 | 4293 | } |
| ... | ... | @@ -4261,7 +4296,7 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com |
| 4261 | 4296 | // The alloc wasn't comptime-known per the above logic, so the |
| 4262 | 4297 | // type cannot be comptime-only. |
| 4263 | 4298 | // TODO: source location of runtime control flow |
| 4264 | return sema.fail(block, src, "value with comptime-only type '{}' depends on runtime control flow", .{final_elem_ty.fmt(mod)}); | |
| 4299 | return sema.fail(block, src, "value with comptime-only type '{}' depends on runtime control flow", .{final_elem_ty.fmt(pt)}); | |
| 4265 | 4300 | } |
| 4266 | 4301 | |
| 4267 | 4302 | // Change it to a normal alloc. |
| ... | ... | @@ -4318,7 +4353,8 @@ fn zirResolveInferredAlloc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com |
| 4318 | 4353 | } |
| 4319 | 4354 | |
| 4320 | 4355 | fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 4321 | const mod = sema.mod; | |
| 4356 | const pt = sema.pt; | |
| 4357 | const mod = pt.zcu; | |
| 4322 | 4358 | const gpa = sema.gpa; |
| 4323 | 4359 | const ip = &mod.intern_pool; |
| 4324 | 4360 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| ... | ... | @@ -4355,7 +4391,7 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 4355 | 4391 | if (!object_ty.isIndexable(mod)) { |
| 4356 | 4392 | // Instead of using checkIndexable we customize this error. |
| 4357 | 4393 | const msg = msg: { |
| 4358 | const msg = try sema.errMsg(arg_src, "type '{}' is not indexable and not a range", .{object_ty.fmt(sema.mod)}); | |
| 4394 | const msg = try sema.errMsg(arg_src, "type '{}' is not indexable and not a range", .{object_ty.fmt(pt)}); | |
| 4359 | 4395 | errdefer msg.destroy(sema.gpa); |
| 4360 | 4396 | try sema.errNote(arg_src, msg, "for loop operand must be a range, array, slice, tuple, or vector", .{}); |
| 4361 | 4397 | |
| ... | ... | @@ -4387,10 +4423,10 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 4387 | 4423 | .input_index = len_idx, |
| 4388 | 4424 | } }); |
| 4389 | 4425 | try sema.errNote(a_src, msg, "length {} here", .{ |
| 4390 | v.fmtValue(sema.mod, sema), | |
| 4426 | v.fmtValue(pt, sema), | |
| 4391 | 4427 | }); |
| 4392 | 4428 | try sema.errNote(arg_src, msg, "length {} here", .{ |
| 4393 | arg_val.fmtValue(sema.mod, sema), | |
| 4429 | arg_val.fmtValue(pt, sema), | |
| 4394 | 4430 | }); |
| 4395 | 4431 | break :msg msg; |
| 4396 | 4432 | }; |
| ... | ... | @@ -4427,7 +4463,7 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 4427 | 4463 | .input_index = i, |
| 4428 | 4464 | } }); |
| 4429 | 4465 | try sema.errNote(arg_src, msg, "type '{}' has no upper bound", .{ |
| 4430 | object_ty.fmt(sema.mod), | |
| 4466 | object_ty.fmt(pt), | |
| 4431 | 4467 | }); |
| 4432 | 4468 | } |
| 4433 | 4469 | break :msg msg; |
| ... | ... | @@ -4453,7 +4489,8 @@ fn zirForLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 4453 | 4489 | /// Given a `*E!?T`, returns a (valid) `*T`. |
| 4454 | 4490 | /// May invalidate already-stored payload data. |
| 4455 | 4491 | fn optEuBasePtrInit(sema: *Sema, block: *Block, ptr: Air.Inst.Ref, src: LazySrcLoc) CompileError!Air.Inst.Ref { |
| 4456 | const mod = sema.mod; | |
| 4492 | const pt = sema.pt; | |
| 4493 | const mod = pt.zcu; | |
| 4457 | 4494 | var base_ptr = ptr; |
| 4458 | 4495 | while (true) switch (sema.typeOf(base_ptr).childType(mod).zigTypeTag(mod)) { |
| 4459 | 4496 | .ErrorUnion => base_ptr = try sema.analyzeErrUnionPayloadPtr(block, src, base_ptr, false, true), |
| ... | ... | @@ -4471,7 +4508,8 @@ fn zirOptEuBasePtrInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compile |
| 4471 | 4508 | } |
| 4472 | 4509 | |
| 4473 | 4510 | fn zirCoercePtrElemTy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 4474 | const mod = sema.mod; | |
| 4511 | const pt = sema.pt; | |
| 4512 | const mod = pt.zcu; | |
| 4475 | 4513 | const pl_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 4476 | 4514 | const src = block.nodeOffset(pl_node.src_node); |
| 4477 | 4515 | const extra = sema.code.extraData(Zir.Inst.Bin, pl_node.payload_index).data; |
| ... | ... | @@ -4503,10 +4541,10 @@ fn zirCoercePtrElemTy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE |
| 4503 | 4541 | switch (val_ty.zigTypeTag(mod)) { |
| 4504 | 4542 | .Array, .Vector => {}, |
| 4505 | 4543 | else => if (!val_ty.isTuple(mod)) { |
| 4506 | return sema.fail(block, src, "expected array of '{}', found '{}'", .{ elem_ty.fmt(mod), val_ty.fmt(mod) }); | |
| 4544 | return sema.fail(block, src, "expected array of '{}', found '{}'", .{ elem_ty.fmt(pt), val_ty.fmt(pt) }); | |
| 4507 | 4545 | }, |
| 4508 | 4546 | } |
| 4509 | const want_ty = try mod.arrayType(.{ | |
| 4547 | const want_ty = try pt.arrayType(.{ | |
| 4510 | 4548 | .len = val_ty.arrayLen(mod), |
| 4511 | 4549 | .child = elem_ty.toIntern(), |
| 4512 | 4550 | .sentinel = if (ptr_ty.sentinel(mod)) |s| s.toIntern() else .none, |
| ... | ... | @@ -4522,7 +4560,8 @@ fn zirCoercePtrElemTy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE |
| 4522 | 4560 | } |
| 4523 | 4561 | |
| 4524 | 4562 | fn zirValidateRefTy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void { |
| 4525 | const mod = sema.mod; | |
| 4563 | const pt = sema.pt; | |
| 4564 | const mod = pt.zcu; | |
| 4526 | 4565 | const un_tok = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_tok; |
| 4527 | 4566 | const src = block.tokenOffset(un_tok.src_tok); |
| 4528 | 4567 | // In case of GenericPoison, we don't actually have a type, so this will be |
| ... | ... | @@ -4538,7 +4577,7 @@ fn zirValidateRefTy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr |
| 4538 | 4577 | if (ty_operand.isGenericPoison()) return; |
| 4539 | 4578 | if (ty_operand.optEuBaseType(mod).zigTypeTag(mod) != .Pointer) { |
| 4540 | 4579 | return sema.failWithOwnedErrorMsg(block, msg: { |
| 4541 | const msg = try sema.errMsg(src, "expected type '{}', found pointer", .{ty_operand.fmt(mod)}); | |
| 4580 | const msg = try sema.errMsg(src, "expected type '{}', found pointer", .{ty_operand.fmt(pt)}); | |
| 4542 | 4581 | errdefer msg.destroy(sema.gpa); |
| 4543 | 4582 | try sema.errNote(src, msg, "address-of operator always returns a pointer", .{}); |
| 4544 | 4583 | break :msg msg; |
| ... | ... | @@ -4551,7 +4590,8 @@ fn zirValidateArrayInitRefTy( |
| 4551 | 4590 | block: *Block, |
| 4552 | 4591 | inst: Zir.Inst.Index, |
| 4553 | 4592 | ) CompileError!Air.Inst.Ref { |
| 4554 | const mod = sema.mod; | |
| 4593 | const pt = sema.pt; | |
| 4594 | const mod = pt.zcu; | |
| 4555 | 4595 | const pl_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 4556 | 4596 | const src = block.nodeOffset(pl_node.src_node); |
| 4557 | 4597 | const extra = sema.code.extraData(Zir.Inst.ArrayInitRefTy, pl_node.payload_index).data; |
| ... | ... | @@ -4565,7 +4605,7 @@ fn zirValidateArrayInitRefTy( |
| 4565 | 4605 | .ptr_type => |ptr_type| switch (ptr_type.flags.size) { |
| 4566 | 4606 | .Slice, .Many => { |
| 4567 | 4607 | // Use array of correct length |
| 4568 | const arr_ty = try mod.arrayType(.{ | |
| 4608 | const arr_ty = try pt.arrayType(.{ | |
| 4569 | 4609 | .len = extra.elem_count, |
| 4570 | 4610 | .child = ptr_ty.childType(mod).toIntern(), |
| 4571 | 4611 | .sentinel = if (ptr_ty.sentinel(mod)) |s| s.toIntern() else .none, |
| ... | ... | @@ -4593,7 +4633,8 @@ fn zirValidateArrayInitTy( |
| 4593 | 4633 | inst: Zir.Inst.Index, |
| 4594 | 4634 | is_result_ty: bool, |
| 4595 | 4635 | ) CompileError!void { |
| 4596 | const mod = sema.mod; | |
| 4636 | const pt = sema.pt; | |
| 4637 | const mod = pt.zcu; | |
| 4597 | 4638 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 4598 | 4639 | const src = block.nodeOffset(inst_data.src_node); |
| 4599 | 4640 | const ty_src: LazySrcLoc = if (is_result_ty) src else block.src(.{ .node_offset_init_ty = inst_data.src_node }); |
| ... | ... | @@ -4615,7 +4656,8 @@ fn validateArrayInitTy( |
| 4615 | 4656 | init_count: u32, |
| 4616 | 4657 | ty: Type, |
| 4617 | 4658 | ) CompileError!void { |
| 4618 | const mod = sema.mod; | |
| 4659 | const pt = sema.pt; | |
| 4660 | const mod = pt.zcu; | |
| 4619 | 4661 | switch (ty.zigTypeTag(mod)) { |
| 4620 | 4662 | .Array => { |
| 4621 | 4663 | const array_len = ty.arrayLen(mod); |
| ... | ... | @@ -4636,7 +4678,7 @@ fn validateArrayInitTy( |
| 4636 | 4678 | return; |
| 4637 | 4679 | }, |
| 4638 | 4680 | .Struct => if (ty.isTuple(mod)) { |
| 4639 | try ty.resolveFields(mod); | |
| 4681 | try ty.resolveFields(pt); | |
| 4640 | 4682 | const array_len = ty.arrayLen(mod); |
| 4641 | 4683 | if (init_count > array_len) { |
| 4642 | 4684 | return sema.fail(block, src, "expected at most {d} tuple fields; found {d}", .{ |
| ... | ... | @@ -4656,7 +4698,8 @@ fn zirValidateStructInitTy( |
| 4656 | 4698 | inst: Zir.Inst.Index, |
| 4657 | 4699 | is_result_ty: bool, |
| 4658 | 4700 | ) CompileError!void { |
| 4659 | const mod = sema.mod; | |
| 4701 | const pt = sema.pt; | |
| 4702 | const mod = pt.zcu; | |
| 4660 | 4703 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 4661 | 4704 | const src = block.nodeOffset(inst_data.src_node); |
| 4662 | 4705 | const ty = sema.resolveType(block, src, inst_data.operand) catch |err| switch (err) { |
| ... | ... | @@ -4681,7 +4724,8 @@ fn zirValidatePtrStructInit( |
| 4681 | 4724 | const tracy = trace(@src()); |
| 4682 | 4725 | defer tracy.end(); |
| 4683 | 4726 | |
| 4684 | const mod = sema.mod; | |
| 4727 | const pt = sema.pt; | |
| 4728 | const mod = pt.zcu; | |
| 4685 | 4729 | const validate_inst = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 4686 | 4730 | const init_src = block.nodeOffset(validate_inst.src_node); |
| 4687 | 4731 | const validate_extra = sema.code.extraData(Zir.Inst.Block, validate_inst.payload_index); |
| ... | ... | @@ -4716,7 +4760,8 @@ fn validateUnionInit( |
| 4716 | 4760 | instrs: []const Zir.Inst.Index, |
| 4717 | 4761 | union_ptr: Air.Inst.Ref, |
| 4718 | 4762 | ) CompileError!void { |
| 4719 | const mod = sema.mod; | |
| 4763 | const pt = sema.pt; | |
| 4764 | const mod = pt.zcu; | |
| 4720 | 4765 | const gpa = sema.gpa; |
| 4721 | 4766 | |
| 4722 | 4767 | if (instrs.len != 1) { |
| ... | ... | @@ -4814,7 +4859,7 @@ fn validateUnionInit( |
| 4814 | 4859 | } |
| 4815 | 4860 | |
| 4816 | 4861 | const tag_ty = union_ty.unionTagTypeHypothetical(mod); |
| 4817 | const tag_val = try mod.enumValueFieldIndex(tag_ty, field_index); | |
| 4862 | const tag_val = try pt.enumValueFieldIndex(tag_ty, field_index); | |
| 4818 | 4863 | const field_type = union_ty.unionFieldType(tag_val, mod).?; |
| 4819 | 4864 | |
| 4820 | 4865 | if (try sema.typeHasOnePossibleValue(field_type)) |field_only_value| { |
| ... | ... | @@ -4848,7 +4893,7 @@ fn validateUnionInit( |
| 4848 | 4893 | } |
| 4849 | 4894 | block.instructions.shrinkRetainingCapacity(block_index); |
| 4850 | 4895 | |
| 4851 | const union_val = try mod.intern(.{ .un = .{ | |
| 4896 | const union_val = try pt.intern(.{ .un = .{ | |
| 4852 | 4897 | .ty = union_ty.toIntern(), |
| 4853 | 4898 | .tag = tag_val.toIntern(), |
| 4854 | 4899 | .val = val.toIntern(), |
| ... | ... | @@ -4875,7 +4920,8 @@ fn validateStructInit( |
| 4875 | 4920 | init_src: LazySrcLoc, |
| 4876 | 4921 | instrs: []const Zir.Inst.Index, |
| 4877 | 4922 | ) CompileError!void { |
| 4878 | const mod = sema.mod; | |
| 4923 | const pt = sema.pt; | |
| 4924 | const mod = pt.zcu; | |
| 4879 | 4925 | const gpa = sema.gpa; |
| 4880 | 4926 | const ip = &mod.intern_pool; |
| 4881 | 4927 | |
| ... | ... | @@ -4914,7 +4960,7 @@ fn validateStructInit( |
| 4914 | 4960 | if (block.is_comptime and |
| 4915 | 4961 | (try sema.resolveDefinedValue(block, init_src, struct_ptr)) != null) |
| 4916 | 4962 | { |
| 4917 | try struct_ty.resolveLayout(mod); | |
| 4963 | try struct_ty.resolveLayout(pt); | |
| 4918 | 4964 | // In this case the only thing we need to do is evaluate the implicit |
| 4919 | 4965 | // store instructions for default field values, and report any missing fields. |
| 4920 | 4966 | // Avoid the cost of the extra machinery for detecting a comptime struct init value. |
| ... | ... | @@ -4922,7 +4968,7 @@ fn validateStructInit( |
| 4922 | 4968 | const i: u32 = @intCast(i_usize); |
| 4923 | 4969 | if (field_ptr != .none) continue; |
| 4924 | 4970 | |
| 4925 | try struct_ty.resolveStructFieldInits(mod); | |
| 4971 | try struct_ty.resolveStructFieldInits(pt); | |
| 4926 | 4972 | const default_val = struct_ty.structFieldDefaultValue(i, mod); |
| 4927 | 4973 | if (default_val.toIntern() == .unreachable_value) { |
| 4928 | 4974 | const field_name = struct_ty.structFieldName(i, mod).unwrap() orelse { |
| ... | ... | @@ -4971,7 +5017,7 @@ fn validateStructInit( |
| 4971 | 5017 | const air_tags = sema.air_instructions.items(.tag); |
| 4972 | 5018 | const air_datas = sema.air_instructions.items(.data); |
| 4973 | 5019 | |
| 4974 | try struct_ty.resolveStructFieldInits(mod); | |
| 5020 | try struct_ty.resolveStructFieldInits(pt); | |
| 4975 | 5021 | |
| 4976 | 5022 | // We collect the comptime field values in case the struct initialization |
| 4977 | 5023 | // ends up being comptime-known. |
| ... | ... | @@ -5094,7 +5140,7 @@ fn validateStructInit( |
| 5094 | 5140 | for (block.instructions.items[first_block_index..]) |cur_inst| { |
| 5095 | 5141 | while (field_ptr_ref == .none and init_index < instrs.len) : (init_index += 1) { |
| 5096 | 5142 | const field_ty = struct_ty.structFieldType(field_indices[init_index], mod); |
| 5097 | if (try field_ty.onePossibleValue(mod)) |_| continue; | |
| 5143 | if (try field_ty.onePossibleValue(pt)) |_| continue; | |
| 5098 | 5144 | field_ptr_ref = sema.inst_map.get(instrs[init_index]).?; |
| 5099 | 5145 | } |
| 5100 | 5146 | switch (air_tags[@intFromEnum(cur_inst)]) { |
| ... | ... | @@ -5122,7 +5168,7 @@ fn validateStructInit( |
| 5122 | 5168 | } |
| 5123 | 5169 | block.instructions.shrinkRetainingCapacity(block_index); |
| 5124 | 5170 | |
| 5125 | const struct_val = try mod.intern(.{ .aggregate = .{ | |
| 5171 | const struct_val = try pt.intern(.{ .aggregate = .{ | |
| 5126 | 5172 | .ty = struct_ty.toIntern(), |
| 5127 | 5173 | .storage = .{ .elems = field_values }, |
| 5128 | 5174 | } }); |
| ... | ... | @@ -5130,7 +5176,7 @@ fn validateStructInit( |
| 5130 | 5176 | try sema.storePtr2(block, init_src, struct_ptr, init_src, struct_init, init_src, .store); |
| 5131 | 5177 | return; |
| 5132 | 5178 | } |
| 5133 | try struct_ty.resolveLayout(mod); | |
| 5179 | try struct_ty.resolveLayout(pt); | |
| 5134 | 5180 | |
| 5135 | 5181 | // Our task is to insert `store` instructions for all the default field values. |
| 5136 | 5182 | for (found_fields, 0..) |field_ptr, i| { |
| ... | ... | @@ -5152,7 +5198,8 @@ fn zirValidatePtrArrayInit( |
| 5152 | 5198 | block: *Block, |
| 5153 | 5199 | inst: Zir.Inst.Index, |
| 5154 | 5200 | ) CompileError!void { |
| 5155 | const mod = sema.mod; | |
| 5201 | const pt = sema.pt; | |
| 5202 | const mod = pt.zcu; | |
| 5156 | 5203 | const validate_inst = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 5157 | 5204 | const init_src = block.nodeOffset(validate_inst.src_node); |
| 5158 | 5205 | const validate_extra = sema.code.extraData(Zir.Inst.Block, validate_inst.payload_index); |
| ... | ... | @@ -5175,7 +5222,7 @@ fn zirValidatePtrArrayInit( |
| 5175 | 5222 | var root_msg: ?*Module.ErrorMsg = null; |
| 5176 | 5223 | errdefer if (root_msg) |msg| msg.destroy(sema.gpa); |
| 5177 | 5224 | |
| 5178 | try array_ty.resolveStructFieldInits(mod); | |
| 5225 | try array_ty.resolveStructFieldInits(pt); | |
| 5179 | 5226 | var i = instrs.len; |
| 5180 | 5227 | while (i < array_len) : (i += 1) { |
| 5181 | 5228 | const default_val = array_ty.structFieldDefaultValue(i, mod).toIntern(); |
| ... | ... | @@ -5218,7 +5265,7 @@ fn zirValidatePtrArrayInit( |
| 5218 | 5265 | // sentinel-terminated array, the sentinel will not have been populated by |
| 5219 | 5266 | // any ZIR instructions at comptime; we need to do that here. |
| 5220 | 5267 | if (array_ty.sentinel(mod)) |sentinel_val| { |
| 5221 | const array_len_ref = try mod.intRef(Type.usize, array_len); | |
| 5268 | const array_len_ref = try pt.intRef(Type.usize, array_len); | |
| 5222 | 5269 | const sentinel_ptr = try sema.elemPtrArray(block, init_src, init_src, array_ptr, init_src, array_len_ref, true, true); |
| 5223 | 5270 | const sentinel = Air.internedToRef(sentinel_val.toIntern()); |
| 5224 | 5271 | try sema.storePtr2(block, init_src, sentinel_ptr, init_src, sentinel, init_src, .store); |
| ... | ... | @@ -5244,8 +5291,8 @@ fn zirValidatePtrArrayInit( |
| 5244 | 5291 | |
| 5245 | 5292 | if (array_ty.isTuple(mod)) { |
| 5246 | 5293 | if (array_ty.structFieldIsComptime(i, mod)) |
| 5247 | try array_ty.resolveStructFieldInits(mod); | |
| 5248 | if (try array_ty.structFieldValueComptime(mod, i)) |opv| { | |
| 5294 | try array_ty.resolveStructFieldInits(pt); | |
| 5295 | if (try array_ty.structFieldValueComptime(pt, i)) |opv| { | |
| 5249 | 5296 | element_vals[i] = opv.toIntern(); |
| 5250 | 5297 | continue; |
| 5251 | 5298 | } |
| ... | ... | @@ -5347,7 +5394,7 @@ fn zirValidatePtrArrayInit( |
| 5347 | 5394 | } |
| 5348 | 5395 | block.instructions.shrinkRetainingCapacity(block_index); |
| 5349 | 5396 | |
| 5350 | const array_val = try mod.intern(.{ .aggregate = .{ | |
| 5397 | const array_val = try pt.intern(.{ .aggregate = .{ | |
| 5351 | 5398 | .ty = array_ty.toIntern(), |
| 5352 | 5399 | .storage = .{ .elems = element_vals }, |
| 5353 | 5400 | } }); |
| ... | ... | @@ -5357,18 +5404,19 @@ fn zirValidatePtrArrayInit( |
| 5357 | 5404 | } |
| 5358 | 5405 | |
| 5359 | 5406 | fn zirValidateDeref(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void { |
| 5360 | const mod = sema.mod; | |
| 5407 | const pt = sema.pt; | |
| 5408 | const mod = pt.zcu; | |
| 5361 | 5409 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 5362 | 5410 | const src = block.nodeOffset(inst_data.src_node); |
| 5363 | 5411 | const operand = try sema.resolveInst(inst_data.operand); |
| 5364 | 5412 | const operand_ty = sema.typeOf(operand); |
| 5365 | 5413 | |
| 5366 | 5414 | if (operand_ty.zigTypeTag(mod) != .Pointer) { |
| 5367 | return sema.fail(block, src, "cannot dereference non-pointer type '{}'", .{operand_ty.fmt(mod)}); | |
| 5415 | return sema.fail(block, src, "cannot dereference non-pointer type '{}'", .{operand_ty.fmt(pt)}); | |
| 5368 | 5416 | } else switch (operand_ty.ptrSize(mod)) { |
| 5369 | 5417 | .One, .C => {}, |
| 5370 | .Many => return sema.fail(block, src, "index syntax required for unknown-length pointer type '{}'", .{operand_ty.fmt(mod)}), | |
| 5371 | .Slice => return sema.fail(block, src, "index syntax required for slice type '{}'", .{operand_ty.fmt(mod)}), | |
| 5418 | .Many => return sema.fail(block, src, "index syntax required for unknown-length pointer type '{}'", .{operand_ty.fmt(pt)}), | |
| 5419 | .Slice => return sema.fail(block, src, "index syntax required for slice type '{}'", .{operand_ty.fmt(pt)}), | |
| 5372 | 5420 | } |
| 5373 | 5421 | |
| 5374 | 5422 | if ((try sema.typeHasOnePossibleValue(operand_ty.childType(mod))) != null) { |
| ... | ... | @@ -5386,7 +5434,7 @@ fn zirValidateDeref(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr |
| 5386 | 5434 | const msg = try sema.errMsg( |
| 5387 | 5435 | src, |
| 5388 | 5436 | "values of type '{}' must be comptime-known, but operand value is runtime-known", |
| 5389 | .{elem_ty.fmt(mod)}, | |
| 5437 | .{elem_ty.fmt(pt)}, | |
| 5390 | 5438 | ); |
| 5391 | 5439 | errdefer msg.destroy(sema.gpa); |
| 5392 | 5440 | |
| ... | ... | @@ -5398,7 +5446,8 @@ fn zirValidateDeref(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr |
| 5398 | 5446 | } |
| 5399 | 5447 | |
| 5400 | 5448 | fn zirValidateDestructure(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void { |
| 5401 | const mod = sema.mod; | |
| 5449 | const pt = sema.pt; | |
| 5450 | const mod = pt.zcu; | |
| 5402 | 5451 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 5403 | 5452 | const extra = sema.code.extraData(Zir.Inst.ValidateDestructure, inst_data.payload_index).data; |
| 5404 | 5453 | const src = block.nodeOffset(inst_data.src_node); |
| ... | ... | @@ -5414,7 +5463,7 @@ fn zirValidateDestructure(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp |
| 5414 | 5463 | |
| 5415 | 5464 | if (!can_destructure) { |
| 5416 | 5465 | return sema.failWithOwnedErrorMsg(block, msg: { |
| 5417 | const msg = try sema.errMsg(src, "type '{}' cannot be destructured", .{operand_ty.fmt(mod)}); | |
| 5466 | const msg = try sema.errMsg(src, "type '{}' cannot be destructured", .{operand_ty.fmt(pt)}); | |
| 5418 | 5467 | errdefer msg.destroy(sema.gpa); |
| 5419 | 5468 | try sema.errNote(destructure_src, msg, "result destructured here", .{}); |
| 5420 | 5469 | break :msg msg; |
| ... | ... | @@ -5441,7 +5490,8 @@ fn failWithBadMemberAccess( |
| 5441 | 5490 | field_src: LazySrcLoc, |
| 5442 | 5491 | field_name: InternPool.NullTerminatedString, |
| 5443 | 5492 | ) CompileError { |
| 5444 | const mod = sema.mod; | |
| 5493 | const pt = sema.pt; | |
| 5494 | const mod = pt.zcu; | |
| 5445 | 5495 | const kw_name = switch (agg_ty.zigTypeTag(mod)) { |
| 5446 | 5496 | .Union => "union", |
| 5447 | 5497 | .Struct => "struct", |
| ... | ... | @@ -5451,12 +5501,12 @@ fn failWithBadMemberAccess( |
| 5451 | 5501 | }; |
| 5452 | 5502 | if (agg_ty.getOwnerDeclOrNull(mod)) |some| if (mod.declIsRoot(some)) { |
| 5453 | 5503 | return sema.fail(block, field_src, "root struct of file '{}' has no member named '{}'", .{ |
| 5454 | agg_ty.fmt(mod), field_name.fmt(&mod.intern_pool), | |
| 5504 | agg_ty.fmt(pt), field_name.fmt(&mod.intern_pool), | |
| 5455 | 5505 | }); |
| 5456 | 5506 | }; |
| 5457 | 5507 | |
| 5458 | 5508 | return sema.fail(block, field_src, "{s} '{}' has no member named '{}'", .{ |
| 5459 | kw_name, agg_ty.fmt(mod), field_name.fmt(&mod.intern_pool), | |
| 5509 | kw_name, agg_ty.fmt(pt), field_name.fmt(&mod.intern_pool), | |
| 5460 | 5510 | }); |
| 5461 | 5511 | } |
| 5462 | 5512 | |
| ... | ... | @@ -5468,8 +5518,8 @@ fn failWithBadStructFieldAccess( |
| 5468 | 5518 | field_src: LazySrcLoc, |
| 5469 | 5519 | field_name: InternPool.NullTerminatedString, |
| 5470 | 5520 | ) CompileError { |
| 5471 | const zcu = sema.mod; | |
| 5472 | const gpa = sema.gpa; | |
| 5521 | const zcu = sema.pt.zcu; | |
| 5522 | const ip = &zcu.intern_pool; | |
| 5473 | 5523 | const decl = zcu.declPtr(struct_type.decl.unwrap().?); |
| 5474 | 5524 | const fqn = try decl.fullyQualifiedName(zcu); |
| 5475 | 5525 | |
| ... | ... | @@ -5477,9 +5527,9 @@ fn failWithBadStructFieldAccess( |
| 5477 | 5527 | const msg = try sema.errMsg( |
| 5478 | 5528 | field_src, |
| 5479 | 5529 | "no field named '{}' in struct '{}'", |
| 5480 | .{ field_name.fmt(&zcu.intern_pool), fqn.fmt(&zcu.intern_pool) }, | |
| 5530 | .{ field_name.fmt(ip), fqn.fmt(ip) }, | |
| 5481 | 5531 | ); |
| 5482 | errdefer msg.destroy(gpa); | |
| 5532 | errdefer msg.destroy(sema.gpa); | |
| 5483 | 5533 | try sema.errNote(struct_ty.srcLoc(zcu), msg, "struct declared here", .{}); |
| 5484 | 5534 | break :msg msg; |
| 5485 | 5535 | }; |
| ... | ... | @@ -5494,7 +5544,8 @@ fn failWithBadUnionFieldAccess( |
| 5494 | 5544 | field_src: LazySrcLoc, |
| 5495 | 5545 | field_name: InternPool.NullTerminatedString, |
| 5496 | 5546 | ) CompileError { |
| 5497 | const zcu = sema.mod; | |
| 5547 | const zcu = sema.pt.zcu; | |
| 5548 | const ip = &zcu.intern_pool; | |
| 5498 | 5549 | const gpa = sema.gpa; |
| 5499 | 5550 | |
| 5500 | 5551 | const decl = zcu.declPtr(union_obj.decl); |
| ... | ... | @@ -5504,7 +5555,7 @@ fn failWithBadUnionFieldAccess( |
| 5504 | 5555 | const msg = try sema.errMsg( |
| 5505 | 5556 | field_src, |
| 5506 | 5557 | "no field named '{}' in union '{}'", |
| 5507 | .{ field_name.fmt(&zcu.intern_pool), fqn.fmt(&zcu.intern_pool) }, | |
| 5558 | .{ field_name.fmt(ip), fqn.fmt(ip) }, | |
| 5508 | 5559 | ); |
| 5509 | 5560 | errdefer msg.destroy(gpa); |
| 5510 | 5561 | try sema.errNote(union_ty.srcLoc(zcu), msg, "union declared here", .{}); |
| ... | ... | @@ -5514,9 +5565,9 @@ fn failWithBadUnionFieldAccess( |
| 5514 | 5565 | } |
| 5515 | 5566 | |
| 5516 | 5567 | fn addDeclaredHereNote(sema: *Sema, parent: *Module.ErrorMsg, decl_ty: Type) !void { |
| 5517 | const mod = sema.mod; | |
| 5518 | const src_loc = decl_ty.srcLocOrNull(mod) orelse return; | |
| 5519 | const category = switch (decl_ty.zigTypeTag(mod)) { | |
| 5568 | const zcu = sema.pt.zcu; | |
| 5569 | const src_loc = decl_ty.srcLocOrNull(zcu) orelse return; | |
| 5570 | const category = switch (decl_ty.zigTypeTag(zcu)) { | |
| 5520 | 5571 | .Union => "union", |
| 5521 | 5572 | .Struct => "struct", |
| 5522 | 5573 | .Enum => "enum", |
| ... | ... | @@ -5575,7 +5626,8 @@ fn storeToInferredAllocComptime( |
| 5575 | 5626 | operand: Air.Inst.Ref, |
| 5576 | 5627 | iac: *Air.Inst.Data.InferredAllocComptime, |
| 5577 | 5628 | ) CompileError!void { |
| 5578 | const zcu = sema.mod; | |
| 5629 | const pt = sema.pt; | |
| 5630 | const zcu = pt.zcu; | |
| 5579 | 5631 | const operand_ty = sema.typeOf(operand); |
| 5580 | 5632 | // There will be only one store_to_inferred_ptr because we are running at comptime. |
| 5581 | 5633 | // The alloc will turn into a Decl or a ComptimeAlloc. |
| ... | ... | @@ -5584,7 +5636,7 @@ fn storeToInferredAllocComptime( |
| 5584 | 5636 | .needed_comptime_reason = "value being stored to a comptime variable must be comptime-known", |
| 5585 | 5637 | }); |
| 5586 | 5638 | }; |
| 5587 | const alloc_ty = try zcu.ptrTypeSema(.{ | |
| 5639 | const alloc_ty = try pt.ptrTypeSema(.{ | |
| 5588 | 5640 | .child = operand_ty.toIntern(), |
| 5589 | 5641 | .flags = .{ |
| 5590 | 5642 | .alignment = iac.alignment, |
| ... | ... | @@ -5592,7 +5644,7 @@ fn storeToInferredAllocComptime( |
| 5592 | 5644 | }, |
| 5593 | 5645 | }); |
| 5594 | 5646 | if (iac.is_const and !operand_val.canMutateComptimeVarState(zcu)) { |
| 5595 | iac.ptr = try zcu.intern(.{ .ptr = .{ | |
| 5647 | iac.ptr = try pt.intern(.{ .ptr = .{ | |
| 5596 | 5648 | .ty = alloc_ty.toIntern(), |
| 5597 | 5649 | .base_addr = .{ .anon_decl = .{ |
| 5598 | 5650 | .val = operand_val.toIntern(), |
| ... | ... | @@ -5603,7 +5655,7 @@ fn storeToInferredAllocComptime( |
| 5603 | 5655 | } else { |
| 5604 | 5656 | const alloc_index = try sema.newComptimeAlloc(block, operand_ty, iac.alignment); |
| 5605 | 5657 | sema.getComptimeAlloc(alloc_index).val = .{ .interned = operand_val.toIntern() }; |
| 5606 | iac.ptr = try zcu.intern(.{ .ptr = .{ | |
| 5658 | iac.ptr = try pt.intern(.{ .ptr = .{ | |
| 5607 | 5659 | .ty = alloc_ty.toIntern(), |
| 5608 | 5660 | .base_addr = .{ .comptime_alloc = alloc_index }, |
| 5609 | 5661 | .byte_offset = 0, |
| ... | ... | @@ -5624,7 +5676,8 @@ fn zirStoreNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!v |
| 5624 | 5676 | const tracy = trace(@src()); |
| 5625 | 5677 | defer tracy.end(); |
| 5626 | 5678 | |
| 5627 | const mod = sema.mod; | |
| 5679 | const pt = sema.pt; | |
| 5680 | const mod = pt.zcu; | |
| 5628 | 5681 | const zir_tags = sema.code.instructions.items(.tag); |
| 5629 | 5682 | const zir_datas = sema.code.instructions.items(.data); |
| 5630 | 5683 | const inst_data = zir_datas[@intFromEnum(inst)].pl_node; |
| ... | ... | @@ -5662,23 +5715,23 @@ fn zirStoreNode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!v |
| 5662 | 5715 | fn zirStr(sema: *Sema, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 5663 | 5716 | const bytes = sema.code.instructions.items(.data)[@intFromEnum(inst)].str.get(sema.code); |
| 5664 | 5717 | return sema.addStrLit( |
| 5665 | try sema.mod.intern_pool.getOrPutString(sema.gpa, bytes, .maybe_embedded_nulls), | |
| 5718 | try sema.pt.zcu.intern_pool.getOrPutString(sema.gpa, bytes, .maybe_embedded_nulls), | |
| 5666 | 5719 | bytes.len, |
| 5667 | 5720 | ); |
| 5668 | 5721 | } |
| 5669 | 5722 | |
| 5670 | 5723 | fn addNullTerminatedStrLit(sema: *Sema, string: InternPool.NullTerminatedString) CompileError!Air.Inst.Ref { |
| 5671 | return sema.addStrLit(string.toString(), string.length(&sema.mod.intern_pool)); | |
| 5724 | return sema.addStrLit(string.toString(), string.length(&sema.pt.zcu.intern_pool)); | |
| 5672 | 5725 | } |
| 5673 | 5726 | |
| 5674 | 5727 | fn addStrLit(sema: *Sema, string: InternPool.String, len: u64) CompileError!Air.Inst.Ref { |
| 5675 | const mod = sema.mod; | |
| 5676 | const array_ty = try mod.arrayType(.{ | |
| 5728 | const pt = sema.pt; | |
| 5729 | const array_ty = try pt.arrayType(.{ | |
| 5677 | 5730 | .len = len, |
| 5678 | 5731 | .sentinel = .zero_u8, |
| 5679 | 5732 | .child = .u8_type, |
| 5680 | 5733 | }); |
| 5681 | const val = try mod.intern(.{ .aggregate = .{ | |
| 5734 | const val = try pt.intern(.{ .aggregate = .{ | |
| 5682 | 5735 | .ty = array_ty.toIntern(), |
| 5683 | 5736 | .storage = .{ .bytes = string }, |
| 5684 | 5737 | } }); |
| ... | ... | @@ -5690,16 +5743,16 @@ fn anonDeclRef(sema: *Sema, val: InternPool.Index) CompileError!Air.Inst.Ref { |
| 5690 | 5743 | } |
| 5691 | 5744 | |
| 5692 | 5745 | fn refValue(sema: *Sema, val: InternPool.Index) CompileError!InternPool.Index { |
| 5693 | const mod = sema.mod; | |
| 5694 | const ptr_ty = (try mod.ptrTypeSema(.{ | |
| 5695 | .child = mod.intern_pool.typeOf(val), | |
| 5746 | const pt = sema.pt; | |
| 5747 | const ptr_ty = (try pt.ptrTypeSema(.{ | |
| 5748 | .child = pt.zcu.intern_pool.typeOf(val), | |
| 5696 | 5749 | .flags = .{ |
| 5697 | 5750 | .alignment = .none, |
| 5698 | 5751 | .is_const = true, |
| 5699 | 5752 | .address_space = .generic, |
| 5700 | 5753 | }, |
| 5701 | 5754 | })).toIntern(); |
| 5702 | return mod.intern(.{ .ptr = .{ | |
| 5755 | return pt.intern(.{ .ptr = .{ | |
| 5703 | 5756 | .ty = ptr_ty, |
| 5704 | 5757 | .base_addr = .{ .anon_decl = .{ |
| 5705 | 5758 | .val = val, |
| ... | ... | @@ -5715,7 +5768,7 @@ fn zirInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins |
| 5715 | 5768 | defer tracy.end(); |
| 5716 | 5769 | |
| 5717 | 5770 | const int = sema.code.instructions.items(.data)[@intFromEnum(inst)].int; |
| 5718 | return sema.mod.intRef(Type.comptime_int, int); | |
| 5771 | return sema.pt.intRef(Type.comptime_int, int); | |
| 5719 | 5772 | } |
| 5720 | 5773 | |
| 5721 | 5774 | fn zirIntBig(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| ... | ... | @@ -5723,7 +5776,6 @@ fn zirIntBig(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 5723 | 5776 | const tracy = trace(@src()); |
| 5724 | 5777 | defer tracy.end(); |
| 5725 | 5778 | |
| 5726 | const mod = sema.mod; | |
| 5727 | 5779 | const int = sema.code.instructions.items(.data)[@intFromEnum(inst)].str; |
| 5728 | 5780 | const byte_count = int.len * @sizeOf(std.math.big.Limb); |
| 5729 | 5781 | const limb_bytes = sema.code.string_bytes[@intFromEnum(int.start)..][0..byte_count]; |
| ... | ... | @@ -5734,7 +5786,7 @@ fn zirIntBig(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 5734 | 5786 | const limbs = try sema.arena.alloc(std.math.big.Limb, int.len); |
| 5735 | 5787 | @memcpy(mem.sliceAsBytes(limbs), limb_bytes); |
| 5736 | 5788 | |
| 5737 | return Air.internedToRef((try mod.intValue_big(Type.comptime_int, .{ | |
| 5789 | return Air.internedToRef((try sema.pt.intValue_big(Type.comptime_int, .{ | |
| 5738 | 5790 | .limbs = limbs, |
| 5739 | 5791 | .positive = true, |
| 5740 | 5792 | })).toIntern()); |
| ... | ... | @@ -5743,7 +5795,7 @@ fn zirIntBig(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 5743 | 5795 | fn zirFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 5744 | 5796 | _ = block; |
| 5745 | 5797 | const number = sema.code.instructions.items(.data)[@intFromEnum(inst)].float; |
| 5746 | return Air.internedToRef((try sema.mod.floatValue( | |
| 5798 | return Air.internedToRef((try sema.pt.floatValue( | |
| 5747 | 5799 | Type.comptime_float, |
| 5748 | 5800 | number, |
| 5749 | 5801 | )).toIntern()); |
| ... | ... | @@ -5754,7 +5806,7 @@ fn zirFloat128(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 5754 | 5806 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 5755 | 5807 | const extra = sema.code.extraData(Zir.Inst.Float128, inst_data.payload_index).data; |
| 5756 | 5808 | const number = extra.get(); |
| 5757 | return Air.internedToRef((try sema.mod.floatValue(Type.comptime_float, number)).toIntern()); | |
| 5809 | return Air.internedToRef((try sema.pt.floatValue(Type.comptime_float, number)).toIntern()); | |
| 5758 | 5810 | } |
| 5759 | 5811 | |
| 5760 | 5812 | fn zirCompileError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void { |
| ... | ... | @@ -5775,10 +5827,11 @@ fn zirCompileLog( |
| 5775 | 5827 | block: *Block, |
| 5776 | 5828 | extended: Zir.Inst.Extended.InstData, |
| 5777 | 5829 | ) CompileError!Air.Inst.Ref { |
| 5778 | const mod = sema.mod; | |
| 5830 | const pt = sema.pt; | |
| 5831 | const mod = pt.zcu; | |
| 5779 | 5832 | |
| 5780 | 5833 | var managed = mod.compile_log_text.toManaged(sema.gpa); |
| 5781 | defer sema.mod.compile_log_text = managed.moveToUnmanaged(); | |
| 5834 | defer pt.zcu.compile_log_text = managed.moveToUnmanaged(); | |
| 5782 | 5835 | const writer = managed.writer(); |
| 5783 | 5836 | |
| 5784 | 5837 | const extra = sema.code.extraData(Zir.Inst.NodeMultiOp, extended.operand); |
| ... | ... | @@ -5792,10 +5845,10 @@ fn zirCompileLog( |
| 5792 | 5845 | const arg_ty = sema.typeOf(arg); |
| 5793 | 5846 | if (try sema.resolveValueResolveLazy(arg)) |val| { |
| 5794 | 5847 | try writer.print("@as({}, {})", .{ |
| 5795 | arg_ty.fmt(mod), val.fmtValue(mod, sema), | |
| 5848 | arg_ty.fmt(pt), val.fmtValue(pt, sema), | |
| 5796 | 5849 | }); |
| 5797 | 5850 | } else { |
| 5798 | try writer.print("@as({}, [runtime value])", .{arg_ty.fmt(mod)}); | |
| 5851 | try writer.print("@as({}, [runtime value])", .{arg_ty.fmt(pt)}); | |
| 5799 | 5852 | } |
| 5800 | 5853 | } |
| 5801 | 5854 | try writer.print("\n", .{}); |
| ... | ... | @@ -5835,7 +5888,8 @@ fn zirLoop(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError |
| 5835 | 5888 | const tracy = trace(@src()); |
| 5836 | 5889 | defer tracy.end(); |
| 5837 | 5890 | |
| 5838 | const mod = sema.mod; | |
| 5891 | const pt = sema.pt; | |
| 5892 | const mod = pt.zcu; | |
| 5839 | 5893 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 5840 | 5894 | const src = parent_block.nodeOffset(inst_data.src_node); |
| 5841 | 5895 | const extra = sema.code.extraData(Zir.Inst.Block, inst_data.payload_index); |
| ... | ... | @@ -5906,7 +5960,8 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr |
| 5906 | 5960 | const tracy = trace(@src()); |
| 5907 | 5961 | defer tracy.end(); |
| 5908 | 5962 | |
| 5909 | const zcu = sema.mod; | |
| 5963 | const pt = sema.pt; | |
| 5964 | const zcu = pt.zcu; | |
| 5910 | 5965 | const comp = zcu.comp; |
| 5911 | 5966 | const gpa = sema.gpa; |
| 5912 | 5967 | const pl_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| ... | ... | @@ -6005,7 +6060,7 @@ fn zirCImport(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileEr |
| 6005 | 6060 | zcu.astGenFile(result.file, result.file_index, path_digest, root_decl) catch |err| |
| 6006 | 6061 | return sema.fail(&child_block, src, "C import failed: {s}", .{@errorName(err)}); |
| 6007 | 6062 | |
| 6008 | try zcu.ensureFileAnalyzed(result.file_index); | |
| 6063 | try pt.ensureFileAnalyzed(result.file_index); | |
| 6009 | 6064 | const file_root_decl_index = zcu.fileRootDecl(result.file_index).unwrap().?; |
| 6010 | 6065 | return sema.analyzeDeclVal(parent_block, src, file_root_decl_index); |
| 6011 | 6066 | } |
| ... | ... | @@ -6147,7 +6202,8 @@ fn resolveAnalyzedBlock( |
| 6147 | 6202 | defer tracy.end(); |
| 6148 | 6203 | |
| 6149 | 6204 | const gpa = sema.gpa; |
| 6150 | const mod = sema.mod; | |
| 6205 | const pt = sema.pt; | |
| 6206 | const mod = pt.zcu; | |
| 6151 | 6207 | |
| 6152 | 6208 | // Blocks must terminate with noreturn instruction. |
| 6153 | 6209 | assert(child_block.instructions.items.len != 0); |
| ... | ... | @@ -6258,7 +6314,7 @@ fn resolveAnalyzedBlock( |
| 6258 | 6314 | const type_src = src; // TODO: better source location |
| 6259 | 6315 | if (try sema.typeRequiresComptime(resolved_ty)) { |
| 6260 | 6316 | const msg = msg: { |
| 6261 | const msg = try sema.errMsg(type_src, "value with comptime-only type '{}' depends on runtime control flow", .{resolved_ty.fmt(mod)}); | |
| 6317 | const msg = try sema.errMsg(type_src, "value with comptime-only type '{}' depends on runtime control flow", .{resolved_ty.fmt(pt)}); | |
| 6262 | 6318 | errdefer msg.destroy(sema.gpa); |
| 6263 | 6319 | |
| 6264 | 6320 | const runtime_src = child_block.runtime_cond orelse child_block.runtime_loop.?; |
| ... | ... | @@ -6353,7 +6409,8 @@ fn zirExport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 6353 | 6409 | const tracy = trace(@src()); |
| 6354 | 6410 | defer tracy.end(); |
| 6355 | 6411 | |
| 6356 | const mod = sema.mod; | |
| 6412 | const pt = sema.pt; | |
| 6413 | const mod = pt.zcu; | |
| 6357 | 6414 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 6358 | 6415 | const extra = sema.code.extraData(Zir.Inst.Export, inst_data.payload_index).data; |
| 6359 | 6416 | const src = block.nodeOffset(inst_data.src_node); |
| ... | ... | @@ -6388,7 +6445,8 @@ fn zirExportValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 6388 | 6445 | const tracy = trace(@src()); |
| 6389 | 6446 | defer tracy.end(); |
| 6390 | 6447 | |
| 6391 | const mod = sema.mod; | |
| 6448 | const pt = sema.pt; | |
| 6449 | const mod = pt.zcu; | |
| 6392 | 6450 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 6393 | 6451 | const extra = sema.code.extraData(Zir.Inst.ExportValue, inst_data.payload_index).data; |
| 6394 | 6452 | const src = block.nodeOffset(inst_data.src_node); |
| ... | ... | @@ -6421,7 +6479,8 @@ pub fn analyzeExport( |
| 6421 | 6479 | exported_decl_index: InternPool.DeclIndex, |
| 6422 | 6480 | ) !void { |
| 6423 | 6481 | const gpa = sema.gpa; |
| 6424 | const mod = sema.mod; | |
| 6482 | const pt = sema.pt; | |
| 6483 | const mod = pt.zcu; | |
| 6425 | 6484 | |
| 6426 | 6485 | if (options.linkage == .internal) |
| 6427 | 6486 | return; |
| ... | ... | @@ -6433,7 +6492,7 @@ pub fn analyzeExport( |
| 6433 | 6492 | |
| 6434 | 6493 | if (!try sema.validateExternType(export_ty, .other)) { |
| 6435 | 6494 | const msg = msg: { |
| 6436 | const msg = try sema.errMsg(src, "unable to export type '{}'", .{export_ty.fmt(mod)}); | |
| 6495 | const msg = try sema.errMsg(src, "unable to export type '{}'", .{export_ty.fmt(pt)}); | |
| 6437 | 6496 | errdefer msg.destroy(gpa); |
| 6438 | 6497 | |
| 6439 | 6498 | try sema.explainWhyTypeIsNotExtern(msg, src, export_ty, .other); |
| ... | ... | @@ -6460,7 +6519,8 @@ pub fn analyzeExport( |
| 6460 | 6519 | } |
| 6461 | 6520 | |
| 6462 | 6521 | fn zirSetAlignStack(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void { |
| 6463 | const mod = sema.mod; | |
| 6522 | const pt = sema.pt; | |
| 6523 | const mod = pt.zcu; | |
| 6464 | 6524 | const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; |
| 6465 | 6525 | const operand_src = block.builtinCallArgSrc(extra.node, 0); |
| 6466 | 6526 | const src = block.nodeOffset(extra.node); |
| ... | ... | @@ -6502,7 +6562,8 @@ fn zirSetAlignStack(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Inst |
| 6502 | 6562 | } |
| 6503 | 6563 | |
| 6504 | 6564 | fn zirSetCold(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void { |
| 6505 | const mod = sema.mod; | |
| 6565 | const pt = sema.pt; | |
| 6566 | const mod = pt.zcu; | |
| 6506 | 6567 | const ip = &mod.intern_pool; |
| 6507 | 6568 | const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; |
| 6508 | 6569 | const operand_src = block.builtinCallArgSrc(extra.node, 0); |
| ... | ... | @@ -6628,7 +6689,8 @@ fn addDbgVar( |
| 6628 | 6689 | ) CompileError!void { |
| 6629 | 6690 | if (block.is_comptime or block.ownerModule().strip) return; |
| 6630 | 6691 | |
| 6631 | const mod = sema.mod; | |
| 6692 | const pt = sema.pt; | |
| 6693 | const mod = pt.zcu; | |
| 6632 | 6694 | const operand_ty = sema.typeOf(operand); |
| 6633 | 6695 | const val_ty = switch (air_tag) { |
| 6634 | 6696 | .dbg_var_ptr => operand_ty.childType(mod), |
| ... | ... | @@ -6669,7 +6731,8 @@ fn addDbgVar( |
| 6669 | 6731 | } |
| 6670 | 6732 | |
| 6671 | 6733 | fn zirDeclRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 6672 | const mod = sema.mod; | |
| 6734 | const pt = sema.pt; | |
| 6735 | const mod = pt.zcu; | |
| 6673 | 6736 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].str_tok; |
| 6674 | 6737 | const src = block.tokenOffset(inst_data.src_tok); |
| 6675 | 6738 | const decl_name = try mod.intern_pool.getOrPutString( |
| ... | ... | @@ -6682,7 +6745,8 @@ fn zirDeclRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 6682 | 6745 | } |
| 6683 | 6746 | |
| 6684 | 6747 | fn zirDeclVal(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 6685 | const mod = sema.mod; | |
| 6748 | const pt = sema.pt; | |
| 6749 | const mod = pt.zcu; | |
| 6686 | 6750 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].str_tok; |
| 6687 | 6751 | const src = block.tokenOffset(inst_data.src_tok); |
| 6688 | 6752 | const decl_name = try mod.intern_pool.getOrPutString( |
| ... | ... | @@ -6695,7 +6759,8 @@ fn zirDeclVal(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 6695 | 6759 | } |
| 6696 | 6760 | |
| 6697 | 6761 | fn lookupIdentifier(sema: *Sema, block: *Block, src: LazySrcLoc, name: InternPool.NullTerminatedString) !InternPool.DeclIndex { |
| 6698 | const mod = sema.mod; | |
| 6762 | const pt = sema.pt; | |
| 6763 | const mod = pt.zcu; | |
| 6699 | 6764 | var namespace = block.namespace; |
| 6700 | 6765 | while (true) { |
| 6701 | 6766 | if (try sema.lookupInNamespace(block, src, namespace.toOptional(), name, false)) |decl_index| { |
| ... | ... | @@ -6716,7 +6781,8 @@ fn lookupInNamespace( |
| 6716 | 6781 | ident_name: InternPool.NullTerminatedString, |
| 6717 | 6782 | observe_usingnamespace: bool, |
| 6718 | 6783 | ) CompileError!?InternPool.DeclIndex { |
| 6719 | const mod = sema.mod; | |
| 6784 | const pt = sema.pt; | |
| 6785 | const mod = pt.zcu; | |
| 6720 | 6786 | |
| 6721 | 6787 | const namespace_index = opt_namespace_index.unwrap() orelse return null; |
| 6722 | 6788 | const namespace = mod.namespacePtr(namespace_index); |
| ... | ... | @@ -6811,7 +6877,8 @@ fn lookupInNamespace( |
| 6811 | 6877 | } |
| 6812 | 6878 | |
| 6813 | 6879 | fn funcDeclSrc(sema: *Sema, func_inst: Air.Inst.Ref) !?*Decl { |
| 6814 | const mod = sema.mod; | |
| 6880 | const pt = sema.pt; | |
| 6881 | const mod = pt.zcu; | |
| 6815 | 6882 | const func_val = (try sema.resolveValue(func_inst)) orelse return null; |
| 6816 | 6883 | if (func_val.isUndef(mod)) return null; |
| 6817 | 6884 | const owner_decl_index = switch (mod.intern_pool.indexToKey(func_val.toIntern())) { |
| ... | ... | @@ -6827,18 +6894,19 @@ fn funcDeclSrc(sema: *Sema, func_inst: Air.Inst.Ref) !?*Decl { |
| 6827 | 6894 | } |
| 6828 | 6895 | |
| 6829 | 6896 | pub fn analyzeSaveErrRetIndex(sema: *Sema, block: *Block) SemaError!Air.Inst.Ref { |
| 6830 | const mod = sema.mod; | |
| 6897 | const pt = sema.pt; | |
| 6898 | const mod = pt.zcu; | |
| 6831 | 6899 | const gpa = sema.gpa; |
| 6832 | 6900 | |
| 6833 | 6901 | if (block.is_comptime or block.is_typeof) { |
| 6834 | const index_val = try mod.intValue_u64(Type.usize, sema.comptime_err_ret_trace.items.len); | |
| 6902 | const index_val = try pt.intValue_u64(Type.usize, sema.comptime_err_ret_trace.items.len); | |
| 6835 | 6903 | return Air.internedToRef(index_val.toIntern()); |
| 6836 | 6904 | } |
| 6837 | 6905 | |
| 6838 | 6906 | if (!block.ownerModule().error_tracing) return .none; |
| 6839 | 6907 | |
| 6840 | const stack_trace_ty = try mod.getBuiltinType("StackTrace"); | |
| 6841 | try stack_trace_ty.resolveFields(mod); | |
| 6908 | const stack_trace_ty = try pt.getBuiltinType("StackTrace"); | |
| 6909 | try stack_trace_ty.resolveFields(pt); | |
| 6842 | 6910 | const field_name = try mod.intern_pool.getOrPutString(gpa, "index", .no_embedded_nulls); |
| 6843 | 6911 | const field_index = sema.structFieldIndex(block, stack_trace_ty, field_name, LazySrcLoc.unneeded) catch |err| switch (err) { |
| 6844 | 6912 | error.AnalysisFail => @panic("std.builtin.StackTrace is corrupt"), |
| ... | ... | @@ -6864,7 +6932,8 @@ fn popErrorReturnTrace( |
| 6864 | 6932 | operand: Air.Inst.Ref, |
| 6865 | 6933 | saved_error_trace_index: Air.Inst.Ref, |
| 6866 | 6934 | ) CompileError!void { |
| 6867 | const mod = sema.mod; | |
| 6935 | const pt = sema.pt; | |
| 6936 | const mod = pt.zcu; | |
| 6868 | 6937 | const gpa = sema.gpa; |
| 6869 | 6938 | var is_non_error: ?bool = null; |
| 6870 | 6939 | var is_non_error_inst: Air.Inst.Ref = undefined; |
| ... | ... | @@ -6878,9 +6947,9 @@ fn popErrorReturnTrace( |
| 6878 | 6947 | // AstGen determined this result does not go to an error-handling expr (try/catch/return etc.), or |
| 6879 | 6948 | // the result is comptime-known to be a non-error. Either way, pop unconditionally. |
| 6880 | 6949 | |
| 6881 | const stack_trace_ty = try mod.getBuiltinType("StackTrace"); | |
| 6882 | try stack_trace_ty.resolveFields(mod); | |
| 6883 | const ptr_stack_trace_ty = try mod.singleMutPtrType(stack_trace_ty); | |
| 6950 | const stack_trace_ty = try pt.getBuiltinType("StackTrace"); | |
| 6951 | try stack_trace_ty.resolveFields(pt); | |
| 6952 | const ptr_stack_trace_ty = try pt.singleMutPtrType(stack_trace_ty); | |
| 6884 | 6953 | const err_return_trace = try block.addTy(.err_return_trace, ptr_stack_trace_ty); |
| 6885 | 6954 | const field_name = try mod.intern_pool.getOrPutString(gpa, "index", .no_embedded_nulls); |
| 6886 | 6955 | const field_ptr = try sema.structFieldPtr(block, src, err_return_trace, field_name, src, stack_trace_ty, true); |
| ... | ... | @@ -6904,9 +6973,9 @@ fn popErrorReturnTrace( |
| 6904 | 6973 | defer then_block.instructions.deinit(gpa); |
| 6905 | 6974 | |
| 6906 | 6975 | // If non-error, then pop the error return trace by restoring the index. |
| 6907 | const stack_trace_ty = try mod.getBuiltinType("StackTrace"); | |
| 6908 | try stack_trace_ty.resolveFields(mod); | |
| 6909 | const ptr_stack_trace_ty = try mod.singleMutPtrType(stack_trace_ty); | |
| 6976 | const stack_trace_ty = try pt.getBuiltinType("StackTrace"); | |
| 6977 | try stack_trace_ty.resolveFields(pt); | |
| 6978 | const ptr_stack_trace_ty = try pt.singleMutPtrType(stack_trace_ty); | |
| 6910 | 6979 | const err_return_trace = try then_block.addTy(.err_return_trace, ptr_stack_trace_ty); |
| 6911 | 6980 | const field_name = try mod.intern_pool.getOrPutString(gpa, "index", .no_embedded_nulls); |
| 6912 | 6981 | const field_ptr = try sema.structFieldPtr(&then_block, src, err_return_trace, field_name, src, stack_trace_ty, true); |
| ... | ... | @@ -6947,7 +7016,8 @@ fn zirCall( |
| 6947 | 7016 | const tracy = trace(@src()); |
| 6948 | 7017 | defer tracy.end(); |
| 6949 | 7018 | |
| 6950 | const mod = sema.mod; | |
| 7019 | const pt = sema.pt; | |
| 7020 | const mod = pt.zcu; | |
| 6951 | 7021 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 6952 | 7022 | const callee_src = block.src(.{ .node_offset_call_func = inst_data.src_node }); |
| 6953 | 7023 | const call_src = block.nodeOffset(inst_data.src_node); |
| ... | ... | @@ -7031,8 +7101,8 @@ fn zirCall( |
| 7031 | 7101 | // If any input is an error-type, we might need to pop any trace it generated. Otherwise, we only |
| 7032 | 7102 | // need to clean-up our own trace if we were passed to a non-error-handling expression. |
| 7033 | 7103 | if (input_is_error or (pop_error_return_trace and return_ty.isError(mod))) { |
| 7034 | const stack_trace_ty = try mod.getBuiltinType("StackTrace"); | |
| 7035 | try stack_trace_ty.resolveFields(mod); | |
| 7104 | const stack_trace_ty = try pt.getBuiltinType("StackTrace"); | |
| 7105 | try stack_trace_ty.resolveFields(pt); | |
| 7036 | 7106 | const field_name = try mod.intern_pool.getOrPutString(sema.gpa, "index", .no_embedded_nulls); |
| 7037 | 7107 | const field_index = try sema.structFieldIndex(block, stack_trace_ty, field_name, call_src); |
| 7038 | 7108 | |
| ... | ... | @@ -7065,7 +7135,8 @@ fn checkCallArgumentCount( |
| 7065 | 7135 | total_args: usize, |
| 7066 | 7136 | member_fn: bool, |
| 7067 | 7137 | ) !Type { |
| 7068 | const mod = sema.mod; | |
| 7138 | const pt = sema.pt; | |
| 7139 | const mod = pt.zcu; | |
| 7069 | 7140 | const func_ty = func_ty: { |
| 7070 | 7141 | switch (callee_ty.zigTypeTag(mod)) { |
| 7071 | 7142 | .Fn => break :func_ty callee_ty, |
| ... | ... | @@ -7082,7 +7153,7 @@ fn checkCallArgumentCount( |
| 7082 | 7153 | { |
| 7083 | 7154 | const msg = msg: { |
| 7084 | 7155 | const msg = try sema.errMsg(func_src, "cannot call optional type '{}'", .{ |
| 7085 | callee_ty.fmt(mod), | |
| 7156 | callee_ty.fmt(pt), | |
| 7086 | 7157 | }); |
| 7087 | 7158 | errdefer msg.destroy(sema.gpa); |
| 7088 | 7159 | try sema.errNote(func_src, msg, "consider using '.?', 'orelse' or 'if'", .{}); |
| ... | ... | @@ -7093,7 +7164,7 @@ fn checkCallArgumentCount( |
| 7093 | 7164 | }, |
| 7094 | 7165 | else => {}, |
| 7095 | 7166 | } |
| 7096 | return sema.fail(block, func_src, "type '{}' not a function", .{callee_ty.fmt(mod)}); | |
| 7167 | return sema.fail(block, func_src, "type '{}' not a function", .{callee_ty.fmt(pt)}); | |
| 7097 | 7168 | }; |
| 7098 | 7169 | |
| 7099 | 7170 | const func_ty_info = mod.typeToFunc(func_ty).?; |
| ... | ... | @@ -7142,7 +7213,8 @@ fn callBuiltin( |
| 7142 | 7213 | args: []const Air.Inst.Ref, |
| 7143 | 7214 | operation: CallOperation, |
| 7144 | 7215 | ) !void { |
| 7145 | const mod = sema.mod; | |
| 7216 | const pt = sema.pt; | |
| 7217 | const mod = pt.zcu; | |
| 7146 | 7218 | const callee_ty = sema.typeOf(builtin_fn); |
| 7147 | 7219 | const func_ty = func_ty: { |
| 7148 | 7220 | switch (callee_ty.zigTypeTag(mod)) { |
| ... | ... | @@ -7155,7 +7227,7 @@ fn callBuiltin( |
| 7155 | 7227 | }, |
| 7156 | 7228 | else => {}, |
| 7157 | 7229 | } |
| 7158 | std.debug.panic("type '{}' is not a function calling builtin fn", .{callee_ty.fmt(mod)}); | |
| 7230 | std.debug.panic("type '{}' is not a function calling builtin fn", .{callee_ty.fmt(pt)}); | |
| 7159 | 7231 | }; |
| 7160 | 7232 | |
| 7161 | 7233 | const func_ty_info = mod.typeToFunc(func_ty).?; |
| ... | ... | @@ -7261,7 +7333,8 @@ const CallArgsInfo = union(enum) { |
| 7261 | 7333 | func_ty_info: InternPool.Key.FuncType, |
| 7262 | 7334 | func_inst: Air.Inst.Ref, |
| 7263 | 7335 | ) CompileError!Air.Inst.Ref { |
| 7264 | const mod = sema.mod; | |
| 7336 | const pt = sema.pt; | |
| 7337 | const mod = pt.zcu; | |
| 7265 | 7338 | const param_count = func_ty_info.param_types.len; |
| 7266 | 7339 | const uncoerced_arg: Air.Inst.Ref = switch (cai) { |
| 7267 | 7340 | inline .resolved, .call_builtin => |resolved| resolved.args[arg_index], |
| ... | ... | @@ -7438,7 +7511,8 @@ fn analyzeCall( |
| 7438 | 7511 | call_dbg_node: ?Zir.Inst.Index, |
| 7439 | 7512 | operation: CallOperation, |
| 7440 | 7513 | ) CompileError!Air.Inst.Ref { |
| 7441 | const mod = sema.mod; | |
| 7514 | const pt = sema.pt; | |
| 7515 | const mod = pt.zcu; | |
| 7442 | 7516 | const ip = &mod.intern_pool; |
| 7443 | 7517 | |
| 7444 | 7518 | const callee_ty = sema.typeOf(func); |
| ... | ... | @@ -7741,10 +7815,10 @@ fn analyzeCall( |
| 7741 | 7815 | const ies = try sema.arena.create(InferredErrorSet); |
| 7742 | 7816 | ies.* = .{ .func = .none }; |
| 7743 | 7817 | sema.fn_ret_ty_ies = ies; |
| 7744 | sema.fn_ret_ty = Type.fromInterned((try ip.get(gpa, .{ .error_union_type = .{ | |
| 7818 | sema.fn_ret_ty = Type.fromInterned(try pt.intern(.{ .error_union_type = .{ | |
| 7745 | 7819 | .error_set_type = .adhoc_inferred_error_set_type, |
| 7746 | 7820 | .payload_type = sema.fn_ret_ty.toIntern(), |
| 7747 | } }))); | |
| 7821 | } })); | |
| 7748 | 7822 | } |
| 7749 | 7823 | |
| 7750 | 7824 | // This `res2` is here instead of directly breaking from `res` due to a stage1 |
| ... | ... | @@ -7816,7 +7890,7 @@ fn analyzeCall( |
| 7816 | 7890 | // TODO: check whether any external comptime memory was mutated by the |
| 7817 | 7891 | // comptime function call. If so, then do not memoize the call here. |
| 7818 | 7892 | if (should_memoize and !Value.fromInterned(result_interned).canMutateComptimeVarState(mod)) { |
| 7819 | _ = try mod.intern(.{ .memoized_call = .{ | |
| 7893 | _ = try pt.intern(.{ .memoized_call = .{ | |
| 7820 | 7894 | .func = module_fn_index, |
| 7821 | 7895 | .arg_values = memoized_arg_values, |
| 7822 | 7896 | .result = result_transformed, |
| ... | ... | @@ -7921,7 +7995,8 @@ fn analyzeCall( |
| 7921 | 7995 | } |
| 7922 | 7996 | |
| 7923 | 7997 | fn handleTailCall(sema: *Sema, block: *Block, call_src: LazySrcLoc, func_ty: Type, result: Air.Inst.Ref) !Air.Inst.Ref { |
| 7924 | const mod = sema.mod; | |
| 7998 | const pt = sema.pt; | |
| 7999 | const mod = pt.zcu; | |
| 7925 | 8000 | const target = mod.getTarget(); |
| 7926 | 8001 | const backend = mod.comp.getZigBackend(); |
| 7927 | 8002 | if (!target_util.supportsTailCall(target, backend)) { |
| ... | ... | @@ -7932,7 +8007,7 @@ fn handleTailCall(sema: *Sema, block: *Block, call_src: LazySrcLoc, func_ty: Typ |
| 7932 | 8007 | const func_decl = mod.funcOwnerDeclPtr(sema.owner_func_index); |
| 7933 | 8008 | if (!func_ty.eql(func_decl.typeOf(mod), mod)) { |
| 7934 | 8009 | return sema.fail(block, call_src, "unable to perform tail call: type of function being called '{}' does not match type of calling function '{}'", .{ |
| 7935 | func_ty.fmt(mod), func_decl.typeOf(mod).fmt(mod), | |
| 8010 | func_ty.fmt(pt), func_decl.typeOf(mod).fmt(pt), | |
| 7936 | 8011 | }); |
| 7937 | 8012 | } |
| 7938 | 8013 | _ = try block.addUnOp(.ret, result); |
| ... | ... | @@ -7954,7 +8029,7 @@ fn analyzeInlineCallArg( |
| 7954 | 8029 | func_ty_info: InternPool.Key.FuncType, |
| 7955 | 8030 | func_inst: Air.Inst.Ref, |
| 7956 | 8031 | ) !?Air.Inst.Ref { |
| 7957 | const mod = ics.sema.mod; | |
| 8032 | const mod = ics.sema.pt.zcu; | |
| 7958 | 8033 | const ip = &mod.intern_pool; |
| 7959 | 8034 | const zir_tags = ics.callee().code.instructions.items(.tag); |
| 7960 | 8035 | switch (zir_tags[@intFromEnum(inst)]) { |
| ... | ... | @@ -8084,7 +8159,8 @@ fn instantiateGenericCall( |
| 8084 | 8159 | call_tag: Air.Inst.Tag, |
| 8085 | 8160 | call_dbg_node: ?Zir.Inst.Index, |
| 8086 | 8161 | ) CompileError!Air.Inst.Ref { |
| 8087 | const zcu = sema.mod; | |
| 8162 | const pt = sema.pt; | |
| 8163 | const zcu = pt.zcu; | |
| 8088 | 8164 | const gpa = sema.gpa; |
| 8089 | 8165 | const ip = &zcu.intern_pool; |
| 8090 | 8166 | |
| ... | ... | @@ -8127,7 +8203,7 @@ fn instantiateGenericCall( |
| 8127 | 8203 | // `param_anytype_comptime` ZIR instructions to be ignored, resulting in a |
| 8128 | 8204 | // new, monomorphized function, with the comptime parameters elided. |
| 8129 | 8205 | var child_sema: Sema = .{ |
| 8130 | .mod = zcu, | |
| 8206 | .pt = pt, | |
| 8131 | 8207 | .gpa = gpa, |
| 8132 | 8208 | .arena = sema.arena, |
| 8133 | 8209 | .code = fn_zir, |
| ... | ... | @@ -8358,7 +8434,8 @@ fn instantiateGenericCall( |
| 8358 | 8434 | } |
| 8359 | 8435 | |
| 8360 | 8436 | fn resolveTupleLazyValues(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!void { |
| 8361 | const mod = sema.mod; | |
| 8437 | const pt = sema.pt; | |
| 8438 | const mod = pt.zcu; | |
| 8362 | 8439 | const ip = &mod.intern_pool; |
| 8363 | 8440 | const tuple = switch (ip.indexToKey(ty.toIntern())) { |
| 8364 | 8441 | .anon_struct_type => |tuple| tuple, |
| ... | ... | @@ -8373,9 +8450,8 @@ fn resolveTupleLazyValues(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) |
| 8373 | 8450 | } |
| 8374 | 8451 | |
| 8375 | 8452 | fn zirIntType(sema: *Sema, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 8376 | const mod = sema.mod; | |
| 8377 | 8453 | const int_type = sema.code.instructions.items(.data)[@intFromEnum(inst)].int_type; |
| 8378 | const ty = try mod.intType(int_type.signedness, int_type.bit_count); | |
| 8454 | const ty = try sema.pt.intType(int_type.signedness, int_type.bit_count); | |
| 8379 | 8455 | return Air.internedToRef(ty.toIntern()); |
| 8380 | 8456 | } |
| 8381 | 8457 | |
| ... | ... | @@ -8383,22 +8459,24 @@ fn zirOptionalType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 8383 | 8459 | const tracy = trace(@src()); |
| 8384 | 8460 | defer tracy.end(); |
| 8385 | 8461 | |
| 8386 | const mod = sema.mod; | |
| 8462 | const pt = sema.pt; | |
| 8463 | const mod = pt.zcu; | |
| 8387 | 8464 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 8388 | 8465 | const operand_src = block.src(.{ .node_offset_un_op = inst_data.src_node }); |
| 8389 | 8466 | const child_type = try sema.resolveType(block, operand_src, inst_data.operand); |
| 8390 | 8467 | if (child_type.zigTypeTag(mod) == .Opaque) { |
| 8391 | return sema.fail(block, operand_src, "opaque type '{}' cannot be optional", .{child_type.fmt(mod)}); | |
| 8468 | return sema.fail(block, operand_src, "opaque type '{}' cannot be optional", .{child_type.fmt(pt)}); | |
| 8392 | 8469 | } else if (child_type.zigTypeTag(mod) == .Null) { |
| 8393 | return sema.fail(block, operand_src, "type '{}' cannot be optional", .{child_type.fmt(mod)}); | |
| 8470 | return sema.fail(block, operand_src, "type '{}' cannot be optional", .{child_type.fmt(pt)}); | |
| 8394 | 8471 | } |
| 8395 | const opt_type = try mod.optionalType(child_type.toIntern()); | |
| 8472 | const opt_type = try pt.optionalType(child_type.toIntern()); | |
| 8396 | 8473 | |
| 8397 | 8474 | return Air.internedToRef(opt_type.toIntern()); |
| 8398 | 8475 | } |
| 8399 | 8476 | |
| 8400 | 8477 | fn zirArrayInitElemType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 8401 | const mod = sema.mod; | |
| 8478 | const pt = sema.pt; | |
| 8479 | const mod = pt.zcu; | |
| 8402 | 8480 | const bin = sema.code.instructions.items(.data)[@intFromEnum(inst)].bin; |
| 8403 | 8481 | const maybe_wrapped_indexable_ty = sema.resolveType(block, LazySrcLoc.unneeded, bin.lhs) catch |err| switch (err) { |
| 8404 | 8482 | // Since this is a ZIR instruction that returns a type, encountering |
| ... | ... | @@ -8409,7 +8487,7 @@ fn zirArrayInitElemType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil |
| 8409 | 8487 | else => |e| return e, |
| 8410 | 8488 | }; |
| 8411 | 8489 | const indexable_ty = maybe_wrapped_indexable_ty.optEuBaseType(mod); |
| 8412 | try indexable_ty.resolveFields(mod); | |
| 8490 | try indexable_ty.resolveFields(pt); | |
| 8413 | 8491 | assert(indexable_ty.isIndexable(mod)); // validated by a previous instruction |
| 8414 | 8492 | if (indexable_ty.zigTypeTag(mod) == .Struct) { |
| 8415 | 8493 | const elem_type = indexable_ty.structFieldType(@intFromEnum(bin.rhs), mod); |
| ... | ... | @@ -8421,7 +8499,8 @@ fn zirArrayInitElemType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil |
| 8421 | 8499 | } |
| 8422 | 8500 | |
| 8423 | 8501 | fn zirElemType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 8424 | const mod = sema.mod; | |
| 8502 | const pt = sema.pt; | |
| 8503 | const mod = pt.zcu; | |
| 8425 | 8504 | const un_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 8426 | 8505 | const maybe_wrapped_ptr_ty = sema.resolveType(block, LazySrcLoc.unneeded, un_node.operand) catch |err| switch (err) { |
| 8427 | 8506 | error.GenericPoison => return .generic_poison_type, |
| ... | ... | @@ -8439,7 +8518,8 @@ fn zirElemType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 8439 | 8518 | } |
| 8440 | 8519 | |
| 8441 | 8520 | fn zirIndexablePtrElemType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 8442 | const mod = sema.mod; | |
| 8521 | const pt = sema.pt; | |
| 8522 | const mod = pt.zcu; | |
| 8443 | 8523 | const un_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 8444 | 8524 | const src = block.nodeOffset(un_node.src_node); |
| 8445 | 8525 | const ptr_ty = sema.resolveType(block, src, un_node.operand) catch |err| switch (err) { |
| ... | ... | @@ -8455,7 +8535,8 @@ fn zirIndexablePtrElemType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com |
| 8455 | 8535 | } |
| 8456 | 8536 | |
| 8457 | 8537 | fn zirVectorElemType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 8458 | const mod = sema.mod; | |
| 8538 | const pt = sema.pt; | |
| 8539 | const mod = pt.zcu; | |
| 8459 | 8540 | const un_node = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 8460 | 8541 | const vec_ty = sema.resolveType(block, LazySrcLoc.unneeded, un_node.operand) catch |err| switch (err) { |
| 8461 | 8542 | // Since this is a ZIR instruction that returns a type, encountering |
| ... | ... | @@ -8466,13 +8547,12 @@ fn zirVectorElemType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr |
| 8466 | 8547 | else => |e| return e, |
| 8467 | 8548 | }; |
| 8468 | 8549 | if (!vec_ty.isVector(mod)) { |
| 8469 | return sema.fail(block, block.nodeOffset(un_node.src_node), "expected vector type, found '{}'", .{vec_ty.fmt(mod)}); | |
| 8550 | return sema.fail(block, block.nodeOffset(un_node.src_node), "expected vector type, found '{}'", .{vec_ty.fmt(pt)}); | |
| 8470 | 8551 | } |
| 8471 | 8552 | return Air.internedToRef(vec_ty.childType(mod).toIntern()); |
| 8472 | 8553 | } |
| 8473 | 8554 | |
| 8474 | 8555 | fn zirVectorType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 8475 | const mod = sema.mod; | |
| 8476 | 8556 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 8477 | 8557 | const len_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| 8478 | 8558 | const elem_type_src = block.builtinCallArgSrc(inst_data.src_node, 1); |
| ... | ... | @@ -8482,7 +8562,7 @@ fn zirVectorType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 8482 | 8562 | })); |
| 8483 | 8563 | const elem_type = try sema.resolveType(block, elem_type_src, extra.rhs); |
| 8484 | 8564 | try sema.checkVectorElemType(block, elem_type_src, elem_type); |
| 8485 | const vector_type = try mod.vectorType(.{ | |
| 8565 | const vector_type = try sema.pt.vectorType(.{ | |
| 8486 | 8566 | .len = len, |
| 8487 | 8567 | .child = elem_type.toIntern(), |
| 8488 | 8568 | }); |
| ... | ... | @@ -8502,7 +8582,7 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 8502 | 8582 | }); |
| 8503 | 8583 | const elem_type = try sema.resolveType(block, elem_src, extra.rhs); |
| 8504 | 8584 | try sema.validateArrayElemType(block, elem_type, elem_src); |
| 8505 | const array_ty = try sema.mod.arrayType(.{ | |
| 8585 | const array_ty = try sema.pt.arrayType(.{ | |
| 8506 | 8586 | .len = len, |
| 8507 | 8587 | .child = elem_type.toIntern(), |
| 8508 | 8588 | }); |
| ... | ... | @@ -8529,7 +8609,7 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil |
| 8529 | 8609 | const sentinel_val = try sema.resolveConstDefinedValue(block, sentinel_src, sentinel, .{ |
| 8530 | 8610 | .needed_comptime_reason = "array sentinel value must be comptime-known", |
| 8531 | 8611 | }); |
| 8532 | const array_ty = try sema.mod.arrayType(.{ | |
| 8612 | const array_ty = try sema.pt.arrayType(.{ | |
| 8533 | 8613 | .len = len, |
| 8534 | 8614 | .sentinel = sentinel_val.toIntern(), |
| 8535 | 8615 | .child = elem_type.toIntern(), |
| ... | ... | @@ -8539,9 +8619,10 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil |
| 8539 | 8619 | } |
| 8540 | 8620 | |
| 8541 | 8621 | fn validateArrayElemType(sema: *Sema, block: *Block, elem_type: Type, elem_src: LazySrcLoc) !void { |
| 8542 | const mod = sema.mod; | |
| 8622 | const pt = sema.pt; | |
| 8623 | const mod = pt.zcu; | |
| 8543 | 8624 | if (elem_type.zigTypeTag(mod) == .Opaque) { |
| 8544 | return sema.fail(block, elem_src, "array of opaque type '{}' not allowed", .{elem_type.fmt(mod)}); | |
| 8625 | return sema.fail(block, elem_src, "array of opaque type '{}' not allowed", .{elem_type.fmt(pt)}); | |
| 8545 | 8626 | } else if (elem_type.zigTypeTag(mod) == .NoReturn) { |
| 8546 | 8627 | return sema.fail(block, elem_src, "array of 'noreturn' not allowed", .{}); |
| 8547 | 8628 | } |
| ... | ... | @@ -8567,7 +8648,8 @@ fn zirErrorUnionType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr |
| 8567 | 8648 | const tracy = trace(@src()); |
| 8568 | 8649 | defer tracy.end(); |
| 8569 | 8650 | |
| 8570 | const mod = sema.mod; | |
| 8651 | const pt = sema.pt; | |
| 8652 | const mod = pt.zcu; | |
| 8571 | 8653 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 8572 | 8654 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 8573 | 8655 | const lhs_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node }); |
| ... | ... | @@ -8577,40 +8659,41 @@ fn zirErrorUnionType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr |
| 8577 | 8659 | |
| 8578 | 8660 | if (error_set.zigTypeTag(mod) != .ErrorSet) { |
| 8579 | 8661 | return sema.fail(block, lhs_src, "expected error set type, found '{}'", .{ |
| 8580 | error_set.fmt(mod), | |
| 8662 | error_set.fmt(pt), | |
| 8581 | 8663 | }); |
| 8582 | 8664 | } |
| 8583 | 8665 | try sema.validateErrorUnionPayloadType(block, payload, rhs_src); |
| 8584 | const err_union_ty = try mod.errorUnionType(error_set, payload); | |
| 8666 | const err_union_ty = try pt.errorUnionType(error_set, payload); | |
| 8585 | 8667 | return Air.internedToRef(err_union_ty.toIntern()); |
| 8586 | 8668 | } |
| 8587 | 8669 | |
| 8588 | 8670 | fn validateErrorUnionPayloadType(sema: *Sema, block: *Block, payload_ty: Type, payload_src: LazySrcLoc) !void { |
| 8589 | const mod = sema.mod; | |
| 8671 | const pt = sema.pt; | |
| 8672 | const mod = pt.zcu; | |
| 8590 | 8673 | if (payload_ty.zigTypeTag(mod) == .Opaque) { |
| 8591 | 8674 | return sema.fail(block, payload_src, "error union with payload of opaque type '{}' not allowed", .{ |
| 8592 | payload_ty.fmt(mod), | |
| 8675 | payload_ty.fmt(pt), | |
| 8593 | 8676 | }); |
| 8594 | 8677 | } else if (payload_ty.zigTypeTag(mod) == .ErrorSet) { |
| 8595 | 8678 | return sema.fail(block, payload_src, "error union with payload of error set type '{}' not allowed", .{ |
| 8596 | payload_ty.fmt(mod), | |
| 8679 | payload_ty.fmt(pt), | |
| 8597 | 8680 | }); |
| 8598 | 8681 | } |
| 8599 | 8682 | } |
| 8600 | 8683 | |
| 8601 | 8684 | fn zirErrorValue(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 8602 | 8685 | _ = block; |
| 8603 | const mod = sema.mod; | |
| 8686 | const pt = sema.pt; | |
| 8604 | 8687 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].str_tok; |
| 8605 | const name = try mod.intern_pool.getOrPutString( | |
| 8688 | const name = try pt.zcu.intern_pool.getOrPutString( | |
| 8606 | 8689 | sema.gpa, |
| 8607 | 8690 | inst_data.get(sema.code), |
| 8608 | 8691 | .no_embedded_nulls, |
| 8609 | 8692 | ); |
| 8610 | _ = try mod.getErrorValue(name); | |
| 8693 | _ = try pt.zcu.getErrorValue(name); | |
| 8611 | 8694 | // Create an error set type with only this error value, and return the value. |
| 8612 | const error_set_type = try mod.singleErrorSetType(name); | |
| 8613 | return Air.internedToRef((try mod.intern(.{ .err = .{ | |
| 8695 | const error_set_type = try pt.singleErrorSetType(name); | |
| 8696 | return Air.internedToRef((try pt.intern(.{ .err = .{ | |
| 8614 | 8697 | .ty = error_set_type.toIntern(), |
| 8615 | 8698 | .name = name, |
| 8616 | 8699 | } }))); |
| ... | ... | @@ -8620,21 +8703,22 @@ fn zirIntFromError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD |
| 8620 | 8703 | const tracy = trace(@src()); |
| 8621 | 8704 | defer tracy.end(); |
| 8622 | 8705 | |
| 8623 | const mod = sema.mod; | |
| 8706 | const pt = sema.pt; | |
| 8707 | const mod = pt.zcu; | |
| 8624 | 8708 | const ip = &mod.intern_pool; |
| 8625 | 8709 | const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; |
| 8626 | 8710 | const src = block.nodeOffset(extra.node); |
| 8627 | 8711 | const operand_src = block.builtinCallArgSrc(extra.node, 0); |
| 8628 | 8712 | const uncasted_operand = try sema.resolveInst(extra.operand); |
| 8629 | 8713 | const operand = try sema.coerce(block, Type.anyerror, uncasted_operand, operand_src); |
| 8630 | const err_int_ty = try mod.errorIntType(); | |
| 8714 | const err_int_ty = try pt.errorIntType(); | |
| 8631 | 8715 | |
| 8632 | 8716 | if (try sema.resolveValue(operand)) |val| { |
| 8633 | 8717 | if (val.isUndef(mod)) { |
| 8634 | return mod.undefRef(err_int_ty); | |
| 8718 | return pt.undefRef(err_int_ty); | |
| 8635 | 8719 | } |
| 8636 | 8720 | const err_name = ip.indexToKey(val.toIntern()).err.name; |
| 8637 | return Air.internedToRef((try mod.intValue( | |
| 8721 | return Air.internedToRef((try pt.intValue( | |
| 8638 | 8722 | err_int_ty, |
| 8639 | 8723 | try mod.getErrorValue(err_name), |
| 8640 | 8724 | )).toIntern()); |
| ... | ... | @@ -8646,10 +8730,10 @@ fn zirIntFromError(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD |
| 8646 | 8730 | else => |err_set_ty_index| { |
| 8647 | 8731 | const names = ip.indexToKey(err_set_ty_index).error_set_type.names; |
| 8648 | 8732 | switch (names.len) { |
| 8649 | 0 => return Air.internedToRef((try mod.intValue(err_int_ty, 0)).toIntern()), | |
| 8733 | 0 => return Air.internedToRef((try pt.intValue(err_int_ty, 0)).toIntern()), | |
| 8650 | 8734 | 1 => { |
| 8651 | 8735 | const int: Module.ErrorInt = @intCast(mod.global_error_set.getIndex(names.get(ip)[0]).?); |
| 8652 | return mod.intRef(err_int_ty, int); | |
| 8736 | return pt.intRef(err_int_ty, int); | |
| 8653 | 8737 | }, |
| 8654 | 8738 | else => {}, |
| 8655 | 8739 | } |
| ... | ... | @@ -8664,19 +8748,20 @@ fn zirErrorFromInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD |
| 8664 | 8748 | const tracy = trace(@src()); |
| 8665 | 8749 | defer tracy.end(); |
| 8666 | 8750 | |
| 8667 | const mod = sema.mod; | |
| 8751 | const pt = sema.pt; | |
| 8752 | const mod = pt.zcu; | |
| 8668 | 8753 | const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; |
| 8669 | 8754 | const src = block.nodeOffset(extra.node); |
| 8670 | 8755 | const operand_src = block.builtinCallArgSrc(extra.node, 0); |
| 8671 | 8756 | const uncasted_operand = try sema.resolveInst(extra.operand); |
| 8672 | const err_int_ty = try mod.errorIntType(); | |
| 8757 | const err_int_ty = try pt.errorIntType(); | |
| 8673 | 8758 | const operand = try sema.coerce(block, err_int_ty, uncasted_operand, operand_src); |
| 8674 | 8759 | |
| 8675 | 8760 | if (try sema.resolveDefinedValue(block, operand_src, operand)) |value| { |
| 8676 | const int = try sema.usizeCast(block, operand_src, try value.toUnsignedIntSema(mod)); | |
| 8761 | const int = try sema.usizeCast(block, operand_src, try value.toUnsignedIntSema(pt)); | |
| 8677 | 8762 | if (int > mod.global_error_set.count() or int == 0) |
| 8678 | 8763 | return sema.fail(block, operand_src, "integer value '{d}' represents no error", .{int}); |
| 8679 | return Air.internedToRef((try mod.intern(.{ .err = .{ | |
| 8764 | return Air.internedToRef((try pt.intern(.{ .err = .{ | |
| 8680 | 8765 | .ty = .anyerror_type, |
| 8681 | 8766 | .name = mod.global_error_set.keys()[int], |
| 8682 | 8767 | } }))); |
| ... | ... | @@ -8684,7 +8769,7 @@ fn zirErrorFromInt(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstD |
| 8684 | 8769 | try sema.requireRuntimeBlock(block, src, operand_src); |
| 8685 | 8770 | if (block.wantSafety()) { |
| 8686 | 8771 | const is_lt_len = try block.addUnOp(.cmp_lt_errors_len, operand); |
| 8687 | const zero_val = Air.internedToRef((try mod.intValue(err_int_ty, 0)).toIntern()); | |
| 8772 | const zero_val = Air.internedToRef((try pt.intValue(err_int_ty, 0)).toIntern()); | |
| 8688 | 8773 | const is_non_zero = try block.addBinOp(.cmp_neq, operand, zero_val); |
| 8689 | 8774 | const ok = try block.addBinOp(.bool_and, is_lt_len, is_non_zero); |
| 8690 | 8775 | try sema.addSafetyCheck(block, src, ok, .invalid_error_code); |
| ... | ... | @@ -8702,7 +8787,8 @@ fn zirMergeErrorSets(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr |
| 8702 | 8787 | const tracy = trace(@src()); |
| 8703 | 8788 | defer tracy.end(); |
| 8704 | 8789 | |
| 8705 | const mod = sema.mod; | |
| 8790 | const pt = sema.pt; | |
| 8791 | const mod = pt.zcu; | |
| 8706 | 8792 | const ip = &mod.intern_pool; |
| 8707 | 8793 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 8708 | 8794 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| ... | ... | @@ -8723,9 +8809,9 @@ fn zirMergeErrorSets(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr |
| 8723 | 8809 | const lhs_ty = try sema.analyzeAsType(block, lhs_src, lhs); |
| 8724 | 8810 | const rhs_ty = try sema.analyzeAsType(block, rhs_src, rhs); |
| 8725 | 8811 | if (lhs_ty.zigTypeTag(mod) != .ErrorSet) |
| 8726 | return sema.fail(block, lhs_src, "expected error set type, found '{}'", .{lhs_ty.fmt(mod)}); | |
| 8812 | return sema.fail(block, lhs_src, "expected error set type, found '{}'", .{lhs_ty.fmt(pt)}); | |
| 8727 | 8813 | if (rhs_ty.zigTypeTag(mod) != .ErrorSet) |
| 8728 | return sema.fail(block, rhs_src, "expected error set type, found '{}'", .{rhs_ty.fmt(mod)}); | |
| 8814 | return sema.fail(block, rhs_src, "expected error set type, found '{}'", .{rhs_ty.fmt(pt)}); | |
| 8729 | 8815 | |
| 8730 | 8816 | // Anything merged with anyerror is anyerror. |
| 8731 | 8817 | if (lhs_ty.toIntern() == .anyerror_type or rhs_ty.toIntern() == .anyerror_type) { |
| ... | ... | @@ -8758,16 +8844,18 @@ fn zirEnumLiteral(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 8758 | 8844 | const tracy = trace(@src()); |
| 8759 | 8845 | defer tracy.end(); |
| 8760 | 8846 | |
| 8761 | const mod = sema.mod; | |
| 8847 | const pt = sema.pt; | |
| 8848 | const mod = pt.zcu; | |
| 8762 | 8849 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].str_tok; |
| 8763 | 8850 | const name = inst_data.get(sema.code); |
| 8764 | return Air.internedToRef((try mod.intern(.{ | |
| 8851 | return Air.internedToRef((try pt.intern(.{ | |
| 8765 | 8852 | .enum_literal = try mod.intern_pool.getOrPutString(sema.gpa, name, .no_embedded_nulls), |
| 8766 | 8853 | }))); |
| 8767 | 8854 | } |
| 8768 | 8855 | |
| 8769 | 8856 | fn zirIntFromEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 8770 | const mod = sema.mod; | |
| 8857 | const pt = sema.pt; | |
| 8858 | const mod = pt.zcu; | |
| 8771 | 8859 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 8772 | 8860 | const src = block.nodeOffset(inst_data.src_node); |
| 8773 | 8861 | const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| ... | ... | @@ -8777,7 +8865,7 @@ fn zirIntFromEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 8777 | 8865 | const enum_tag: Air.Inst.Ref = switch (operand_ty.zigTypeTag(mod)) { |
| 8778 | 8866 | .Enum => operand, |
| 8779 | 8867 | .Union => blk: { |
| 8780 | try operand_ty.resolveFields(mod); | |
| 8868 | try operand_ty.resolveFields(pt); | |
| 8781 | 8869 | const tag_ty = operand_ty.unionTagType(mod) orelse { |
| 8782 | 8870 | return sema.fail( |
| 8783 | 8871 | block, |
| ... | ... | @@ -8791,7 +8879,7 @@ fn zirIntFromEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 8791 | 8879 | }, |
| 8792 | 8880 | else => { |
| 8793 | 8881 | return sema.fail(block, operand_src, "expected enum or tagged union, found '{}'", .{ |
| 8794 | operand_ty.fmt(mod), | |
| 8882 | operand_ty.fmt(pt), | |
| 8795 | 8883 | }); |
| 8796 | 8884 | }, |
| 8797 | 8885 | }; |
| ... | ... | @@ -8802,20 +8890,20 @@ fn zirIntFromEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 8802 | 8890 | // https://github.com/ziglang/zig/issues/15909 |
| 8803 | 8891 | if (enum_tag_ty.enumFieldCount(mod) == 0 and !enum_tag_ty.isNonexhaustiveEnum(mod)) { |
| 8804 | 8892 | return sema.fail(block, operand_src, "cannot use @intFromEnum on empty enum '{}'", .{ |
| 8805 | enum_tag_ty.fmt(mod), | |
| 8893 | enum_tag_ty.fmt(pt), | |
| 8806 | 8894 | }); |
| 8807 | 8895 | } |
| 8808 | 8896 | |
| 8809 | 8897 | if (try sema.typeHasOnePossibleValue(enum_tag_ty)) |opv| { |
| 8810 | return Air.internedToRef((try mod.getCoerced(opv, int_tag_ty)).toIntern()); | |
| 8898 | return Air.internedToRef((try pt.getCoerced(opv, int_tag_ty)).toIntern()); | |
| 8811 | 8899 | } |
| 8812 | 8900 | |
| 8813 | 8901 | if (try sema.resolveValue(enum_tag)) |enum_tag_val| { |
| 8814 | 8902 | if (enum_tag_val.isUndef(mod)) { |
| 8815 | return mod.undefRef(int_tag_ty); | |
| 8903 | return pt.undefRef(int_tag_ty); | |
| 8816 | 8904 | } |
| 8817 | 8905 | |
| 8818 | const val = try enum_tag_val.intFromEnum(enum_tag_ty, mod); | |
| 8906 | const val = try enum_tag_val.intFromEnum(enum_tag_ty, pt); | |
| 8819 | 8907 | return Air.internedToRef(val.toIntern()); |
| 8820 | 8908 | } |
| 8821 | 8909 | |
| ... | ... | @@ -8824,7 +8912,8 @@ fn zirIntFromEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 8824 | 8912 | } |
| 8825 | 8913 | |
| 8826 | 8914 | fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 8827 | const mod = sema.mod; | |
| 8915 | const pt = sema.pt; | |
| 8916 | const mod = pt.zcu; | |
| 8828 | 8917 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 8829 | 8918 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 8830 | 8919 | const src = block.nodeOffset(inst_data.src_node); |
| ... | ... | @@ -8833,7 +8922,7 @@ fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 8833 | 8922 | const operand = try sema.resolveInst(extra.rhs); |
| 8834 | 8923 | |
| 8835 | 8924 | if (dest_ty.zigTypeTag(mod) != .Enum) { |
| 8836 | return sema.fail(block, src, "expected enum, found '{}'", .{dest_ty.fmt(mod)}); | |
| 8925 | return sema.fail(block, src, "expected enum, found '{}'", .{dest_ty.fmt(pt)}); | |
| 8837 | 8926 | } |
| 8838 | 8927 | _ = try sema.checkIntType(block, operand_src, sema.typeOf(operand)); |
| 8839 | 8928 | |
| ... | ... | @@ -8841,10 +8930,10 @@ fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 8841 | 8930 | if (dest_ty.isNonexhaustiveEnum(mod)) { |
| 8842 | 8931 | const int_tag_ty = dest_ty.intTagType(mod); |
| 8843 | 8932 | if (try sema.intFitsInType(int_val, int_tag_ty, null)) { |
| 8844 | return Air.internedToRef((try mod.getCoerced(int_val, dest_ty)).toIntern()); | |
| 8933 | return Air.internedToRef((try pt.getCoerced(int_val, dest_ty)).toIntern()); | |
| 8845 | 8934 | } |
| 8846 | 8935 | return sema.fail(block, src, "int value '{}' out of range of non-exhaustive enum '{}'", .{ |
| 8847 | int_val.fmtValue(mod, sema), dest_ty.fmt(mod), | |
| 8936 | int_val.fmtValue(pt, sema), dest_ty.fmt(pt), | |
| 8848 | 8937 | }); |
| 8849 | 8938 | } |
| 8850 | 8939 | if (int_val.isUndef(mod)) { |
| ... | ... | @@ -8852,10 +8941,10 @@ fn zirEnumFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 8852 | 8941 | } |
| 8853 | 8942 | if (!(try sema.enumHasInt(dest_ty, int_val))) { |
| 8854 | 8943 | return sema.fail(block, src, "enum '{}' has no tag with value '{}'", .{ |
| 8855 | dest_ty.fmt(mod), int_val.fmtValue(mod, sema), | |
| 8944 | dest_ty.fmt(pt), int_val.fmtValue(pt, sema), | |
| 8856 | 8945 | }); |
| 8857 | 8946 | } |
| 8858 | return Air.internedToRef((try mod.getCoerced(int_val, dest_ty)).toIntern()); | |
| 8947 | return Air.internedToRef((try pt.getCoerced(int_val, dest_ty)).toIntern()); | |
| 8859 | 8948 | } |
| 8860 | 8949 | |
| 8861 | 8950 | if (dest_ty.intTagType(mod).zigTypeTag(mod) == .ComptimeInt) { |
| ... | ... | @@ -8909,7 +8998,8 @@ fn analyzeOptionalPayloadPtr( |
| 8909 | 8998 | safety_check: bool, |
| 8910 | 8999 | initializing: bool, |
| 8911 | 9000 | ) CompileError!Air.Inst.Ref { |
| 8912 | const zcu = sema.mod; | |
| 9001 | const pt = sema.pt; | |
| 9002 | const zcu = pt.zcu; | |
| 8913 | 9003 | const optional_ptr_ty = sema.typeOf(optional_ptr); |
| 8914 | 9004 | assert(optional_ptr_ty.zigTypeTag(zcu) == .Pointer); |
| 8915 | 9005 | |
| ... | ... | @@ -8919,7 +9009,7 @@ fn analyzeOptionalPayloadPtr( |
| 8919 | 9009 | } |
| 8920 | 9010 | |
| 8921 | 9011 | const child_type = opt_type.optionalChild(zcu); |
| 8922 | const child_pointer = try zcu.ptrTypeSema(.{ | |
| 9012 | const child_pointer = try pt.ptrTypeSema(.{ | |
| 8923 | 9013 | .child = child_type.toIntern(), |
| 8924 | 9014 | .flags = .{ |
| 8925 | 9015 | .is_const = optional_ptr_ty.isConstPtr(zcu), |
| ... | ... | @@ -8932,8 +9022,8 @@ fn analyzeOptionalPayloadPtr( |
| 8932 | 9022 | if (sema.isComptimeMutablePtr(ptr_val)) { |
| 8933 | 9023 | // Set the optional to non-null at comptime. |
| 8934 | 9024 | // If the payload is OPV, we must use that value instead of undef. |
| 8935 | const payload_val = try sema.typeHasOnePossibleValue(child_type) orelse try zcu.undefValue(child_type); | |
| 8936 | const opt_val = try zcu.intern(.{ .opt = .{ | |
| 9025 | const payload_val = try sema.typeHasOnePossibleValue(child_type) orelse try pt.undefValue(child_type); | |
| 9026 | const opt_val = try pt.intern(.{ .opt = .{ | |
| 8937 | 9027 | .ty = opt_type.toIntern(), |
| 8938 | 9028 | .val = payload_val.toIntern(), |
| 8939 | 9029 | } }); |
| ... | ... | @@ -8943,13 +9033,13 @@ fn analyzeOptionalPayloadPtr( |
| 8943 | 9033 | const opt_payload_ptr = try block.addTyOp(.optional_payload_ptr_set, child_pointer, optional_ptr); |
| 8944 | 9034 | try sema.checkKnownAllocPtr(block, optional_ptr, opt_payload_ptr); |
| 8945 | 9035 | } |
| 8946 | return Air.internedToRef((try ptr_val.ptrOptPayload(zcu)).toIntern()); | |
| 9036 | return Air.internedToRef((try ptr_val.ptrOptPayload(pt)).toIntern()); | |
| 8947 | 9037 | } |
| 8948 | 9038 | if (try sema.pointerDeref(block, src, ptr_val, optional_ptr_ty)) |val| { |
| 8949 | 9039 | if (val.isNull(zcu)) { |
| 8950 | 9040 | return sema.fail(block, src, "unable to unwrap null", .{}); |
| 8951 | 9041 | } |
| 8952 | return Air.internedToRef((try ptr_val.ptrOptPayload(zcu)).toIntern()); | |
| 9042 | return Air.internedToRef((try ptr_val.ptrOptPayload(pt)).toIntern()); | |
| 8953 | 9043 | } |
| 8954 | 9044 | } |
| 8955 | 9045 | |
| ... | ... | @@ -8978,7 +9068,8 @@ fn zirOptionalPayload( |
| 8978 | 9068 | const tracy = trace(@src()); |
| 8979 | 9069 | defer tracy.end(); |
| 8980 | 9070 | |
| 8981 | const mod = sema.mod; | |
| 9071 | const pt = sema.pt; | |
| 9072 | const mod = pt.zcu; | |
| 8982 | 9073 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 8983 | 9074 | const src = block.nodeOffset(inst_data.src_node); |
| 8984 | 9075 | const operand = try sema.resolveInst(inst_data.operand); |
| ... | ... | @@ -8992,7 +9083,7 @@ fn zirOptionalPayload( |
| 8992 | 9083 | // TODO https://github.com/ziglang/zig/issues/6597 |
| 8993 | 9084 | if (true) break :t operand_ty; |
| 8994 | 9085 | const ptr_info = operand_ty.ptrInfo(mod); |
| 8995 | break :t try mod.ptrTypeSema(.{ | |
| 9086 | break :t try pt.ptrTypeSema(.{ | |
| 8996 | 9087 | .child = ptr_info.child, |
| 8997 | 9088 | .flags = .{ |
| 8998 | 9089 | .alignment = ptr_info.flags.alignment, |
| ... | ... | @@ -9030,7 +9121,8 @@ fn zirErrUnionPayload( |
| 9030 | 9121 | const tracy = trace(@src()); |
| 9031 | 9122 | defer tracy.end(); |
| 9032 | 9123 | |
| 9033 | const mod = sema.mod; | |
| 9124 | const pt = sema.pt; | |
| 9125 | const mod = pt.zcu; | |
| 9034 | 9126 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 9035 | 9127 | const src = block.nodeOffset(inst_data.src_node); |
| 9036 | 9128 | const operand = try sema.resolveInst(inst_data.operand); |
| ... | ... | @@ -9038,7 +9130,7 @@ fn zirErrUnionPayload( |
| 9038 | 9130 | const err_union_ty = sema.typeOf(operand); |
| 9039 | 9131 | if (err_union_ty.zigTypeTag(mod) != .ErrorUnion) { |
| 9040 | 9132 | return sema.fail(block, operand_src, "expected error union type, found '{}'", .{ |
| 9041 | err_union_ty.fmt(mod), | |
| 9133 | err_union_ty.fmt(pt), | |
| 9042 | 9134 | }); |
| 9043 | 9135 | } |
| 9044 | 9136 | return sema.analyzeErrUnionPayload(block, src, err_union_ty, operand, operand_src, false); |
| ... | ... | @@ -9053,7 +9145,8 @@ fn analyzeErrUnionPayload( |
| 9053 | 9145 | operand_src: LazySrcLoc, |
| 9054 | 9146 | safety_check: bool, |
| 9055 | 9147 | ) CompileError!Air.Inst.Ref { |
| 9056 | const mod = sema.mod; | |
| 9148 | const pt = sema.pt; | |
| 9149 | const mod = pt.zcu; | |
| 9057 | 9150 | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| 9058 | 9151 | if (try sema.resolveDefinedValue(block, operand_src, operand)) |val| { |
| 9059 | 9152 | if (val.getErrorName(mod).unwrap()) |name| { |
| ... | ... | @@ -9098,19 +9191,20 @@ fn analyzeErrUnionPayloadPtr( |
| 9098 | 9191 | safety_check: bool, |
| 9099 | 9192 | initializing: bool, |
| 9100 | 9193 | ) CompileError!Air.Inst.Ref { |
| 9101 | const zcu = sema.mod; | |
| 9194 | const pt = sema.pt; | |
| 9195 | const zcu = pt.zcu; | |
| 9102 | 9196 | const operand_ty = sema.typeOf(operand); |
| 9103 | 9197 | assert(operand_ty.zigTypeTag(zcu) == .Pointer); |
| 9104 | 9198 | |
| 9105 | 9199 | if (operand_ty.childType(zcu).zigTypeTag(zcu) != .ErrorUnion) { |
| 9106 | 9200 | return sema.fail(block, src, "expected error union type, found '{}'", .{ |
| 9107 | operand_ty.childType(zcu).fmt(zcu), | |
| 9201 | operand_ty.childType(zcu).fmt(pt), | |
| 9108 | 9202 | }); |
| 9109 | 9203 | } |
| 9110 | 9204 | |
| 9111 | 9205 | const err_union_ty = operand_ty.childType(zcu); |
| 9112 | 9206 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| 9113 | const operand_pointer_ty = try zcu.ptrTypeSema(.{ | |
| 9207 | const operand_pointer_ty = try pt.ptrTypeSema(.{ | |
| 9114 | 9208 | .child = payload_ty.toIntern(), |
| 9115 | 9209 | .flags = .{ |
| 9116 | 9210 | .is_const = operand_ty.isConstPtr(zcu), |
| ... | ... | @@ -9123,8 +9217,8 @@ fn analyzeErrUnionPayloadPtr( |
| 9123 | 9217 | if (sema.isComptimeMutablePtr(ptr_val)) { |
| 9124 | 9218 | // Set the error union to non-error at comptime. |
| 9125 | 9219 | // If the payload is OPV, we must use that value instead of undef. |
| 9126 | const payload_val = try sema.typeHasOnePossibleValue(payload_ty) orelse try zcu.undefValue(payload_ty); | |
| 9127 | const eu_val = try zcu.intern(.{ .error_union = .{ | |
| 9220 | const payload_val = try sema.typeHasOnePossibleValue(payload_ty) orelse try pt.undefValue(payload_ty); | |
| 9221 | const eu_val = try pt.intern(.{ .error_union = .{ | |
| 9128 | 9222 | .ty = err_union_ty.toIntern(), |
| 9129 | 9223 | .val = .{ .payload = payload_val.toIntern() }, |
| 9130 | 9224 | } }); |
| ... | ... | @@ -9135,13 +9229,13 @@ fn analyzeErrUnionPayloadPtr( |
| 9135 | 9229 | const eu_payload_ptr = try block.addTyOp(.errunion_payload_ptr_set, operand_pointer_ty, operand); |
| 9136 | 9230 | try sema.checkKnownAllocPtr(block, operand, eu_payload_ptr); |
| 9137 | 9231 | } |
| 9138 | return Air.internedToRef((try ptr_val.ptrEuPayload(zcu)).toIntern()); | |
| 9232 | return Air.internedToRef((try ptr_val.ptrEuPayload(pt)).toIntern()); | |
| 9139 | 9233 | } |
| 9140 | 9234 | if (try sema.pointerDeref(block, src, ptr_val, operand_ty)) |val| { |
| 9141 | 9235 | if (val.getErrorName(zcu).unwrap()) |name| { |
| 9142 | 9236 | return sema.failWithComptimeErrorRetTrace(block, src, name); |
| 9143 | 9237 | } |
| 9144 | return Air.internedToRef((try ptr_val.ptrEuPayload(zcu)).toIntern()); | |
| 9238 | return Air.internedToRef((try ptr_val.ptrEuPayload(pt)).toIntern()); | |
| 9145 | 9239 | } |
| 9146 | 9240 | } |
| 9147 | 9241 | |
| ... | ... | @@ -9175,18 +9269,19 @@ fn zirErrUnionCode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 9175 | 9269 | } |
| 9176 | 9270 | |
| 9177 | 9271 | fn analyzeErrUnionCode(sema: *Sema, block: *Block, src: LazySrcLoc, operand: Air.Inst.Ref) CompileError!Air.Inst.Ref { |
| 9178 | const mod = sema.mod; | |
| 9272 | const pt = sema.pt; | |
| 9273 | const mod = pt.zcu; | |
| 9179 | 9274 | const operand_ty = sema.typeOf(operand); |
| 9180 | 9275 | if (operand_ty.zigTypeTag(mod) != .ErrorUnion) { |
| 9181 | 9276 | return sema.fail(block, src, "expected error union type, found '{}'", .{ |
| 9182 | operand_ty.fmt(mod), | |
| 9277 | operand_ty.fmt(pt), | |
| 9183 | 9278 | }); |
| 9184 | 9279 | } |
| 9185 | 9280 | |
| 9186 | 9281 | const result_ty = operand_ty.errorUnionSet(mod); |
| 9187 | 9282 | |
| 9188 | 9283 | if (try sema.resolveDefinedValue(block, src, operand)) |val| { |
| 9189 | return Air.internedToRef((try mod.intern(.{ .err = .{ | |
| 9284 | return Air.internedToRef((try pt.intern(.{ .err = .{ | |
| 9190 | 9285 | .ty = result_ty.toIntern(), |
| 9191 | 9286 | .name = mod.intern_pool.indexToKey(val.toIntern()).error_union.val.err_name, |
| 9192 | 9287 | } }))); |
| ... | ... | @@ -9208,13 +9303,14 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE |
| 9208 | 9303 | } |
| 9209 | 9304 | |
| 9210 | 9305 | fn analyzeErrUnionCodePtr(sema: *Sema, block: *Block, src: LazySrcLoc, operand: Air.Inst.Ref) CompileError!Air.Inst.Ref { |
| 9211 | const mod = sema.mod; | |
| 9306 | const pt = sema.pt; | |
| 9307 | const mod = pt.zcu; | |
| 9212 | 9308 | const operand_ty = sema.typeOf(operand); |
| 9213 | 9309 | assert(operand_ty.zigTypeTag(mod) == .Pointer); |
| 9214 | 9310 | |
| 9215 | 9311 | if (operand_ty.childType(mod).zigTypeTag(mod) != .ErrorUnion) { |
| 9216 | 9312 | return sema.fail(block, src, "expected error union type, found '{}'", .{ |
| 9217 | operand_ty.childType(mod).fmt(mod), | |
| 9313 | operand_ty.childType(mod).fmt(pt), | |
| 9218 | 9314 | }); |
| 9219 | 9315 | } |
| 9220 | 9316 | |
| ... | ... | @@ -9223,7 +9319,7 @@ fn analyzeErrUnionCodePtr(sema: *Sema, block: *Block, src: LazySrcLoc, operand: |
| 9223 | 9319 | if (try sema.resolveDefinedValue(block, src, operand)) |pointer_val| { |
| 9224 | 9320 | if (try sema.pointerDeref(block, src, pointer_val, operand_ty)) |val| { |
| 9225 | 9321 | assert(val.getErrorName(mod) != .none); |
| 9226 | return Air.internedToRef((try mod.intern(.{ .err = .{ | |
| 9322 | return Air.internedToRef((try pt.intern(.{ .err = .{ | |
| 9227 | 9323 | .ty = result_ty.toIntern(), |
| 9228 | 9324 | .name = mod.intern_pool.indexToKey(val.toIntern()).error_union.val.err_name, |
| 9229 | 9325 | } }))); |
| ... | ... | @@ -9240,10 +9336,11 @@ fn zirFunc( |
| 9240 | 9336 | inst: Zir.Inst.Index, |
| 9241 | 9337 | inferred_error_set: bool, |
| 9242 | 9338 | ) CompileError!Air.Inst.Ref { |
| 9243 | const mod = sema.mod; | |
| 9339 | const pt = sema.pt; | |
| 9340 | const mod = pt.zcu; | |
| 9244 | 9341 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 9245 | 9342 | const extra = sema.code.extraData(Zir.Inst.Func, inst_data.payload_index); |
| 9246 | const target = sema.mod.getTarget(); | |
| 9343 | const target = mod.getTarget(); | |
| 9247 | 9344 | const ret_ty_src = block.src(.{ .node_offset_fn_type_ret_ty = inst_data.src_node }); |
| 9248 | 9345 | |
| 9249 | 9346 | var extra_index = extra.end; |
| ... | ... | @@ -9372,7 +9469,8 @@ fn handleExternLibName( |
| 9372 | 9469 | lib_name: []const u8, |
| 9373 | 9470 | ) CompileError!void { |
| 9374 | 9471 | blk: { |
| 9375 | const mod = sema.mod; | |
| 9472 | const pt = sema.pt; | |
| 9473 | const mod = pt.zcu; | |
| 9376 | 9474 | const comp = mod.comp; |
| 9377 | 9475 | const target = mod.getTarget(); |
| 9378 | 9476 | log.debug("extern fn symbol expected in lib '{s}'", .{lib_name}); |
| ... | ... | @@ -9485,7 +9583,8 @@ fn funcCommon( |
| 9485 | 9583 | noalias_bits: u32, |
| 9486 | 9584 | is_noinline: bool, |
| 9487 | 9585 | ) CompileError!Air.Inst.Ref { |
| 9488 | const mod = sema.mod; | |
| 9586 | const pt = sema.pt; | |
| 9587 | const mod = pt.zcu; | |
| 9489 | 9588 | const gpa = sema.gpa; |
| 9490 | 9589 | const target = mod.getTarget(); |
| 9491 | 9590 | const ip = &mod.intern_pool; |
| ... | ... | @@ -9539,13 +9638,13 @@ fn funcCommon( |
| 9539 | 9638 | if (!param_ty.isValidParamType(mod)) { |
| 9540 | 9639 | const opaque_str = if (param_ty.zigTypeTag(mod) == .Opaque) "opaque " else ""; |
| 9541 | 9640 | return sema.fail(block, param_src, "parameter of {s}type '{}' not allowed", .{ |
| 9542 | opaque_str, param_ty.fmt(mod), | |
| 9641 | opaque_str, param_ty.fmt(pt), | |
| 9543 | 9642 | }); |
| 9544 | 9643 | } |
| 9545 | 9644 | if (!this_generic and !target_util.fnCallConvAllowsZigTypes(target, cc_resolved) and !try sema.validateExternType(param_ty, .param_ty)) { |
| 9546 | 9645 | const msg = msg: { |
| 9547 | 9646 | const msg = try sema.errMsg(param_src, "parameter of type '{}' not allowed in function with calling convention '{s}'", .{ |
| 9548 | param_ty.fmt(mod), @tagName(cc_resolved), | |
| 9647 | param_ty.fmt(pt), @tagName(cc_resolved), | |
| 9549 | 9648 | }); |
| 9550 | 9649 | errdefer msg.destroy(sema.gpa); |
| 9551 | 9650 | |
| ... | ... | @@ -9559,7 +9658,7 @@ fn funcCommon( |
| 9559 | 9658 | if (is_source_decl and requires_comptime and !param_is_comptime and has_body and !block.is_comptime) { |
| 9560 | 9659 | const msg = msg: { |
| 9561 | 9660 | const msg = try sema.errMsg(param_src, "parameter of type '{}' must be declared comptime", .{ |
| 9562 | param_ty.fmt(mod), | |
| 9661 | param_ty.fmt(pt), | |
| 9563 | 9662 | }); |
| 9564 | 9663 | errdefer msg.destroy(sema.gpa); |
| 9565 | 9664 | |
| ... | ... | @@ -9580,7 +9679,7 @@ fn funcCommon( |
| 9580 | 9679 | const err_code_size = target.ptrBitWidth(); |
| 9581 | 9680 | switch (i) { |
| 9582 | 9681 | 0 => if (param_ty.zigTypeTag(mod) != .Pointer) return sema.fail(block, param_src, "first parameter of function with 'Interrupt' calling convention must be a pointer type", .{}), |
| 9583 | 1 => if (param_ty.bitSize(mod) != err_code_size) return sema.fail(block, param_src, "second parameter of function with 'Interrupt' calling convention must be a {d}-bit integer", .{err_code_size}), | |
| 9682 | 1 => if (param_ty.bitSize(pt) != err_code_size) return sema.fail(block, param_src, "second parameter of function with 'Interrupt' calling convention must be a {d}-bit integer", .{err_code_size}), | |
| 9584 | 9683 | else => return sema.fail(block, param_src, "'Interrupt' calling convention supports up to 2 parameters, found {d}", .{i + 1}), |
| 9585 | 9684 | } |
| 9586 | 9685 | } else return sema.fail(block, param_src, "parameters are not allowed with 'Interrupt' calling convention", .{}), |
| ... | ... | @@ -9606,7 +9705,7 @@ fn funcCommon( |
| 9606 | 9705 | if (inferred_error_set) { |
| 9607 | 9706 | try sema.validateErrorUnionPayloadType(block, bare_return_type, ret_ty_src); |
| 9608 | 9707 | } |
| 9609 | const func_index = try ip.getFuncInstance(gpa, .{ | |
| 9708 | const func_index = try ip.getFuncInstance(gpa, pt.tid, .{ | |
| 9610 | 9709 | .param_types = param_types, |
| 9611 | 9710 | .noalias_bits = noalias_bits, |
| 9612 | 9711 | .bare_return_type = bare_return_type.toIntern(), |
| ... | ... | @@ -9655,7 +9754,7 @@ fn funcCommon( |
| 9655 | 9754 | assert(has_body); |
| 9656 | 9755 | if (!ret_poison) |
| 9657 | 9756 | try sema.validateErrorUnionPayloadType(block, bare_return_type, ret_ty_src); |
| 9658 | const func_index = try ip.getFuncDeclIes(gpa, .{ | |
| 9757 | const func_index = try ip.getFuncDeclIes(gpa, pt.tid, .{ | |
| 9659 | 9758 | .owner_decl = sema.owner_decl_index, |
| 9660 | 9759 | |
| 9661 | 9760 | .param_types = param_types, |
| ... | ... | @@ -9695,7 +9794,7 @@ fn funcCommon( |
| 9695 | 9794 | ); |
| 9696 | 9795 | } |
| 9697 | 9796 | |
| 9698 | const func_ty = try ip.getFuncType(gpa, .{ | |
| 9797 | const func_ty = try ip.getFuncType(gpa, pt.tid, .{ | |
| 9699 | 9798 | .param_types = param_types, |
| 9700 | 9799 | .noalias_bits = noalias_bits, |
| 9701 | 9800 | .comptime_bits = comptime_bits, |
| ... | ... | @@ -9718,7 +9817,7 @@ fn funcCommon( |
| 9718 | 9817 | if (opt_lib_name) |lib_name| try sema.handleExternLibName(block, block.src(.{ |
| 9719 | 9818 | .node_offset_lib_name = src_node_offset, |
| 9720 | 9819 | }), lib_name); |
| 9721 | const func_index = try ip.getExternFunc(gpa, .{ | |
| 9820 | const func_index = try ip.getExternFunc(gpa, pt.tid, .{ | |
| 9722 | 9821 | .ty = func_ty, |
| 9723 | 9822 | .decl = sema.owner_decl_index, |
| 9724 | 9823 | .lib_name = try mod.intern_pool.getOrPutStringOpt(gpa, opt_lib_name, .no_embedded_nulls), |
| ... | ... | @@ -9743,7 +9842,7 @@ fn funcCommon( |
| 9743 | 9842 | } |
| 9744 | 9843 | |
| 9745 | 9844 | if (has_body) { |
| 9746 | const func_index = try ip.getFuncDecl(gpa, .{ | |
| 9845 | const func_index = try ip.getFuncDecl(gpa, pt.tid, .{ | |
| 9747 | 9846 | .owner_decl = sema.owner_decl_index, |
| 9748 | 9847 | .ty = func_ty, |
| 9749 | 9848 | .cc = cc, |
| ... | ... | @@ -9809,7 +9908,8 @@ fn finishFunc( |
| 9809 | 9908 | is_generic: bool, |
| 9810 | 9909 | final_is_generic: bool, |
| 9811 | 9910 | ) CompileError!Air.Inst.Ref { |
| 9812 | const mod = sema.mod; | |
| 9911 | const pt = sema.pt; | |
| 9912 | const mod = pt.zcu; | |
| 9813 | 9913 | const ip = &mod.intern_pool; |
| 9814 | 9914 | const gpa = sema.gpa; |
| 9815 | 9915 | const target = mod.getTarget(); |
| ... | ... | @@ -9822,7 +9922,7 @@ fn finishFunc( |
| 9822 | 9922 | if (!return_type.isValidReturnType(mod)) { |
| 9823 | 9923 | const opaque_str = if (return_type.zigTypeTag(mod) == .Opaque) "opaque " else ""; |
| 9824 | 9924 | return sema.fail(block, ret_ty_src, "{s}return type '{}' not allowed", .{ |
| 9825 | opaque_str, return_type.fmt(mod), | |
| 9925 | opaque_str, return_type.fmt(pt), | |
| 9826 | 9926 | }); |
| 9827 | 9927 | } |
| 9828 | 9928 | if (!ret_poison and !target_util.fnCallConvAllowsZigTypes(target, cc_resolved) and |
| ... | ... | @@ -9830,7 +9930,7 @@ fn finishFunc( |
| 9830 | 9930 | { |
| 9831 | 9931 | const msg = msg: { |
| 9832 | 9932 | const msg = try sema.errMsg(ret_ty_src, "return type '{}' not allowed in function with calling convention '{s}'", .{ |
| 9833 | return_type.fmt(mod), @tagName(cc_resolved), | |
| 9933 | return_type.fmt(pt), @tagName(cc_resolved), | |
| 9834 | 9934 | }); |
| 9835 | 9935 | errdefer msg.destroy(gpa); |
| 9836 | 9936 | |
| ... | ... | @@ -9852,7 +9952,7 @@ fn finishFunc( |
| 9852 | 9952 | const msg = try sema.errMsg( |
| 9853 | 9953 | ret_ty_src, |
| 9854 | 9954 | "function with comptime-only return type '{}' requires all parameters to be comptime", |
| 9855 | .{return_type.fmt(mod)}, | |
| 9955 | .{return_type.fmt(pt)}, | |
| 9856 | 9956 | ); |
| 9857 | 9957 | try sema.explainWhyTypeIsComptime(msg, ret_ty_src, return_type); |
| 9858 | 9958 | |
| ... | ... | @@ -9938,8 +10038,8 @@ fn finishFunc( |
| 9938 | 10038 | if (!final_is_generic and sema.wantErrorReturnTracing(return_type)) { |
| 9939 | 10039 | // Make sure that StackTrace's fields are resolved so that the backend can |
| 9940 | 10040 | // lower this fn type. |
| 9941 | const unresolved_stack_trace_ty = try mod.getBuiltinType("StackTrace"); | |
| 9942 | try unresolved_stack_trace_ty.resolveFields(mod); | |
| 10041 | const unresolved_stack_trace_ty = try pt.getBuiltinType("StackTrace"); | |
| 10042 | try unresolved_stack_trace_ty.resolveFields(pt); | |
| 9943 | 10043 | } |
| 9944 | 10044 | |
| 9945 | 10045 | return Air.internedToRef(if (opt_func_index != .none) opt_func_index else func_ty); |
| ... | ... | @@ -10068,7 +10168,8 @@ fn analyzeAs( |
| 10068 | 10168 | zir_operand: Zir.Inst.Ref, |
| 10069 | 10169 | no_cast_to_comptime_int: bool, |
| 10070 | 10170 | ) CompileError!Air.Inst.Ref { |
| 10071 | const mod = sema.mod; | |
| 10171 | const pt = sema.pt; | |
| 10172 | const mod = pt.zcu; | |
| 10072 | 10173 | const operand = try sema.resolveInst(zir_operand); |
| 10073 | 10174 | const operand_air_inst = sema.resolveInst(zir_dest_type) catch |err| switch (err) { |
| 10074 | 10175 | error.GenericPoison => return operand, |
| ... | ... | @@ -10098,7 +10199,8 @@ fn zirIntFromPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 10098 | 10199 | const tracy = trace(@src()); |
| 10099 | 10200 | defer tracy.end(); |
| 10100 | 10201 | |
| 10101 | const zcu = sema.mod; | |
| 10202 | const pt = sema.pt; | |
| 10203 | const zcu = pt.zcu; | |
| 10102 | 10204 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 10103 | 10205 | const ptr_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| 10104 | 10206 | const operand = try sema.resolveInst(inst_data.operand); |
| ... | ... | @@ -10106,12 +10208,12 @@ fn zirIntFromPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 10106 | 10208 | const ptr_ty = operand_ty.scalarType(zcu); |
| 10107 | 10209 | const is_vector = operand_ty.zigTypeTag(zcu) == .Vector; |
| 10108 | 10210 | if (!ptr_ty.isPtrAtRuntime(zcu)) { |
| 10109 | return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty.fmt(zcu)}); | |
| 10211 | return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty.fmt(pt)}); | |
| 10110 | 10212 | } |
| 10111 | 10213 | const pointee_ty = ptr_ty.childType(zcu); |
| 10112 | 10214 | if (try sema.typeRequiresComptime(ptr_ty)) { |
| 10113 | 10215 | const msg = msg: { |
| 10114 | const msg = try sema.errMsg(ptr_src, "comptime-only type '{}' has no pointer address", .{pointee_ty.fmt(zcu)}); | |
| 10216 | const msg = try sema.errMsg(ptr_src, "comptime-only type '{}' has no pointer address", .{pointee_ty.fmt(pt)}); | |
| 10115 | 10217 | errdefer msg.destroy(sema.gpa); |
| 10116 | 10218 | try sema.explainWhyTypeIsComptime(msg, ptr_src, pointee_ty); |
| 10117 | 10219 | break :msg msg; |
| ... | ... | @@ -10121,32 +10223,32 @@ fn zirIntFromPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 10121 | 10223 | if (try sema.resolveValueIntable(operand)) |operand_val| ct: { |
| 10122 | 10224 | if (!is_vector) { |
| 10123 | 10225 | if (operand_val.isUndef(zcu)) { |
| 10124 | return Air.internedToRef((try zcu.undefValue(Type.usize)).toIntern()); | |
| 10226 | return Air.internedToRef((try pt.undefValue(Type.usize)).toIntern()); | |
| 10125 | 10227 | } |
| 10126 | return Air.internedToRef((try zcu.intValue( | |
| 10228 | return Air.internedToRef((try pt.intValue( | |
| 10127 | 10229 | Type.usize, |
| 10128 | (try operand_val.getUnsignedIntAdvanced(zcu, .sema)).?, | |
| 10230 | (try operand_val.getUnsignedIntAdvanced(pt, .sema)).?, | |
| 10129 | 10231 | )).toIntern()); |
| 10130 | 10232 | } |
| 10131 | 10233 | const len = operand_ty.vectorLen(zcu); |
| 10132 | const dest_ty = try zcu.vectorType(.{ .child = .usize_type, .len = len }); | |
| 10234 | const dest_ty = try pt.vectorType(.{ .child = .usize_type, .len = len }); | |
| 10133 | 10235 | const new_elems = try sema.arena.alloc(InternPool.Index, len); |
| 10134 | 10236 | for (new_elems, 0..) |*new_elem, i| { |
| 10135 | const ptr_val = try operand_val.elemValue(zcu, i); | |
| 10237 | const ptr_val = try operand_val.elemValue(pt, i); | |
| 10136 | 10238 | if (ptr_val.isUndef(zcu)) { |
| 10137 | new_elem.* = (try zcu.undefValue(Type.usize)).toIntern(); | |
| 10239 | new_elem.* = (try pt.undefValue(Type.usize)).toIntern(); | |
| 10138 | 10240 | continue; |
| 10139 | 10241 | } |
| 10140 | const addr = try ptr_val.getUnsignedIntAdvanced(zcu, .sema) orelse { | |
| 10242 | const addr = try ptr_val.getUnsignedIntAdvanced(pt, .sema) orelse { | |
| 10141 | 10243 | // A vector element wasn't an integer pointer. This is a runtime operation. |
| 10142 | 10244 | break :ct; |
| 10143 | 10245 | }; |
| 10144 | new_elem.* = (try zcu.intValue( | |
| 10246 | new_elem.* = (try pt.intValue( | |
| 10145 | 10247 | Type.usize, |
| 10146 | 10248 | addr, |
| 10147 | 10249 | )).toIntern(); |
| 10148 | 10250 | } |
| 10149 | return Air.internedToRef(try zcu.intern(.{ .aggregate = .{ | |
| 10251 | return Air.internedToRef(try pt.intern(.{ .aggregate = .{ | |
| 10150 | 10252 | .ty = dest_ty.toIntern(), |
| 10151 | 10253 | .storage = .{ .elems = new_elems }, |
| 10152 | 10254 | } })); |
| ... | ... | @@ -10157,10 +10259,10 @@ fn zirIntFromPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 10157 | 10259 | return block.addUnOp(.int_from_ptr, operand); |
| 10158 | 10260 | } |
| 10159 | 10261 | const len = operand_ty.vectorLen(zcu); |
| 10160 | const dest_ty = try zcu.vectorType(.{ .child = .usize_type, .len = len }); | |
| 10262 | const dest_ty = try pt.vectorType(.{ .child = .usize_type, .len = len }); | |
| 10161 | 10263 | const new_elems = try sema.arena.alloc(Air.Inst.Ref, len); |
| 10162 | 10264 | for (new_elems, 0..) |*new_elem, i| { |
| 10163 | const idx_ref = try zcu.intRef(Type.usize, i); | |
| 10265 | const idx_ref = try pt.intRef(Type.usize, i); | |
| 10164 | 10266 | const old_elem = try block.addBinOp(.array_elem_val, operand, idx_ref); |
| 10165 | 10267 | new_elem.* = try block.addUnOp(.int_from_ptr, old_elem); |
| 10166 | 10268 | } |
| ... | ... | @@ -10171,7 +10273,8 @@ fn zirFieldVal(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 10171 | 10273 | const tracy = trace(@src()); |
| 10172 | 10274 | defer tracy.end(); |
| 10173 | 10275 | |
| 10174 | const mod = sema.mod; | |
| 10276 | const pt = sema.pt; | |
| 10277 | const mod = pt.zcu; | |
| 10175 | 10278 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 10176 | 10279 | const src = block.nodeOffset(inst_data.src_node); |
| 10177 | 10280 | const field_name_src = block.src(.{ .node_offset_field_name = inst_data.src_node }); |
| ... | ... | @@ -10189,7 +10292,8 @@ fn zirFieldPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 10189 | 10292 | const tracy = trace(@src()); |
| 10190 | 10293 | defer tracy.end(); |
| 10191 | 10294 | |
| 10192 | const mod = sema.mod; | |
| 10295 | const pt = sema.pt; | |
| 10296 | const mod = pt.zcu; | |
| 10193 | 10297 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 10194 | 10298 | const src = block.nodeOffset(inst_data.src_node); |
| 10195 | 10299 | const field_name_src = block.src(.{ .node_offset_field_name = inst_data.src_node }); |
| ... | ... | @@ -10207,7 +10311,8 @@ fn zirStructInitFieldPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compi |
| 10207 | 10311 | const tracy = trace(@src()); |
| 10208 | 10312 | defer tracy.end(); |
| 10209 | 10313 | |
| 10210 | const mod = sema.mod; | |
| 10314 | const pt = sema.pt; | |
| 10315 | const mod = pt.zcu; | |
| 10211 | 10316 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 10212 | 10317 | const src = block.nodeOffset(inst_data.src_node); |
| 10213 | 10318 | const field_name_src = block.src(.{ .node_offset_field_name_init = inst_data.src_node }); |
| ... | ... | @@ -10284,7 +10389,8 @@ fn intCast( |
| 10284 | 10389 | operand_src: LazySrcLoc, |
| 10285 | 10390 | runtime_safety: bool, |
| 10286 | 10391 | ) CompileError!Air.Inst.Ref { |
| 10287 | const mod = sema.mod; | |
| 10392 | const pt = sema.pt; | |
| 10393 | const mod = pt.zcu; | |
| 10288 | 10394 | const operand_ty = sema.typeOf(operand); |
| 10289 | 10395 | const dest_scalar_ty = try sema.checkIntOrVectorAllowComptime(block, dest_ty, dest_ty_src); |
| 10290 | 10396 | const operand_scalar_ty = try sema.checkIntOrVectorAllowComptime(block, operand_ty, operand_src); |
| ... | ... | @@ -10307,7 +10413,7 @@ fn intCast( |
| 10307 | 10413 | |
| 10308 | 10414 | if (wanted_bits == 0) { |
| 10309 | 10415 | const ok = if (is_vector) ok: { |
| 10310 | const zeros = try sema.splat(operand_ty, try mod.intValue(operand_scalar_ty, 0)); | |
| 10416 | const zeros = try sema.splat(operand_ty, try pt.intValue(operand_scalar_ty, 0)); | |
| 10311 | 10417 | const zero_inst = Air.internedToRef(zeros.toIntern()); |
| 10312 | 10418 | const is_in_range = try block.addCmpVector(operand, zero_inst, .eq); |
| 10313 | 10419 | const all_in_range = try block.addInst(.{ |
| ... | ... | @@ -10316,7 +10422,7 @@ fn intCast( |
| 10316 | 10422 | }); |
| 10317 | 10423 | break :ok all_in_range; |
| 10318 | 10424 | } else ok: { |
| 10319 | const zero_inst = Air.internedToRef((try mod.intValue(operand_ty, 0)).toIntern()); | |
| 10425 | const zero_inst = Air.internedToRef((try pt.intValue(operand_ty, 0)).toIntern()); | |
| 10320 | 10426 | const is_in_range = try block.addBinOp(.cmp_lte, operand, zero_inst); |
| 10321 | 10427 | break :ok is_in_range; |
| 10322 | 10428 | }; |
| ... | ... | @@ -10339,7 +10445,7 @@ fn intCast( |
| 10339 | 10445 | // range shrinkage |
| 10340 | 10446 | // requirement: int value fits into target type |
| 10341 | 10447 | if (wanted_value_bits < actual_value_bits) { |
| 10342 | const dest_max_val_scalar = try dest_scalar_ty.maxIntScalar(mod, operand_scalar_ty); | |
| 10448 | const dest_max_val_scalar = try dest_scalar_ty.maxIntScalar(pt, operand_scalar_ty); | |
| 10343 | 10449 | const dest_max_val = try sema.splat(operand_ty, dest_max_val_scalar); |
| 10344 | 10450 | const dest_max = Air.internedToRef(dest_max_val.toIntern()); |
| 10345 | 10451 | |
| ... | ... | @@ -10348,8 +10454,8 @@ fn intCast( |
| 10348 | 10454 | |
| 10349 | 10455 | // Reinterpret the sign-bit as part of the value. This will make |
| 10350 | 10456 | // negative differences (`operand` > `dest_max`) appear too big. |
| 10351 | const unsigned_scalar_operand_ty = try mod.intType(.unsigned, actual_bits); | |
| 10352 | const unsigned_operand_ty = if (is_vector) try mod.vectorType(.{ | |
| 10457 | const unsigned_scalar_operand_ty = try pt.intType(.unsigned, actual_bits); | |
| 10458 | const unsigned_operand_ty = if (is_vector) try pt.vectorType(.{ | |
| 10353 | 10459 | .len = dest_ty.vectorLen(mod), |
| 10354 | 10460 | .child = unsigned_scalar_operand_ty.toIntern(), |
| 10355 | 10461 | }) else unsigned_scalar_operand_ty; |
| ... | ... | @@ -10358,14 +10464,14 @@ fn intCast( |
| 10358 | 10464 | // If the destination type is signed, then we need to double its |
| 10359 | 10465 | // range to account for negative values. |
| 10360 | 10466 | const dest_range_val = if (wanted_info.signedness == .signed) range_val: { |
| 10361 | const one_scalar = try mod.intValue(unsigned_scalar_operand_ty, 1); | |
| 10362 | const one = if (is_vector) Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 10467 | const one_scalar = try pt.intValue(unsigned_scalar_operand_ty, 1); | |
| 10468 | const one = if (is_vector) Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 10363 | 10469 | .ty = unsigned_operand_ty.toIntern(), |
| 10364 | 10470 | .storage = .{ .repeated_elem = one_scalar.toIntern() }, |
| 10365 | } }))) else one_scalar; | |
| 10366 | const range_minus_one = try dest_max_val.shl(one, unsigned_operand_ty, sema.arena, mod); | |
| 10471 | } })) else one_scalar; | |
| 10472 | const range_minus_one = try dest_max_val.shl(one, unsigned_operand_ty, sema.arena, pt); | |
| 10367 | 10473 | break :range_val try sema.intAdd(range_minus_one, one, unsigned_operand_ty, undefined); |
| 10368 | } else try mod.getCoerced(dest_max_val, unsigned_operand_ty); | |
| 10474 | } else try pt.getCoerced(dest_max_val, unsigned_operand_ty); | |
| 10369 | 10475 | const dest_range = Air.internedToRef(dest_range_val.toIntern()); |
| 10370 | 10476 | |
| 10371 | 10477 | const ok = if (is_vector) ok: { |
| ... | ... | @@ -10405,7 +10511,7 @@ fn intCast( |
| 10405 | 10511 | // no shrinkage, yes sign loss |
| 10406 | 10512 | // requirement: signed to unsigned >= 0 |
| 10407 | 10513 | const ok = if (is_vector) ok: { |
| 10408 | const scalar_zero = try mod.intValue(operand_scalar_ty, 0); | |
| 10514 | const scalar_zero = try pt.intValue(operand_scalar_ty, 0); | |
| 10409 | 10515 | const zero_val = try sema.splat(operand_ty, scalar_zero); |
| 10410 | 10516 | const zero_inst = Air.internedToRef(zero_val.toIntern()); |
| 10411 | 10517 | const is_in_range = try block.addCmpVector(operand, zero_inst, .gte); |
| ... | ... | @@ -10418,7 +10524,7 @@ fn intCast( |
| 10418 | 10524 | }); |
| 10419 | 10525 | break :ok all_in_range; |
| 10420 | 10526 | } else ok: { |
| 10421 | const zero_inst = Air.internedToRef((try mod.intValue(operand_ty, 0)).toIntern()); | |
| 10527 | const zero_inst = Air.internedToRef((try pt.intValue(operand_ty, 0)).toIntern()); | |
| 10422 | 10528 | const is_in_range = try block.addBinOp(.cmp_gte, operand, zero_inst); |
| 10423 | 10529 | break :ok is_in_range; |
| 10424 | 10530 | }; |
| ... | ... | @@ -10432,7 +10538,8 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 10432 | 10538 | const tracy = trace(@src()); |
| 10433 | 10539 | defer tracy.end(); |
| 10434 | 10540 | |
| 10435 | const mod = sema.mod; | |
| 10541 | const pt = sema.pt; | |
| 10542 | const mod = pt.zcu; | |
| 10436 | 10543 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 10437 | 10544 | const src = block.nodeOffset(inst_data.src_node); |
| 10438 | 10545 | const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| ... | ... | @@ -10457,14 +10564,14 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 10457 | 10564 | .Type, |
| 10458 | 10565 | .Undefined, |
| 10459 | 10566 | .Void, |
| 10460 | => return sema.fail(block, src, "cannot @bitCast to '{}'", .{dest_ty.fmt(mod)}), | |
| 10567 | => return sema.fail(block, src, "cannot @bitCast to '{}'", .{dest_ty.fmt(pt)}), | |
| 10461 | 10568 | |
| 10462 | 10569 | .Enum => { |
| 10463 | 10570 | const msg = msg: { |
| 10464 | const msg = try sema.errMsg(src, "cannot @bitCast to '{}'", .{dest_ty.fmt(mod)}); | |
| 10571 | const msg = try sema.errMsg(src, "cannot @bitCast to '{}'", .{dest_ty.fmt(pt)}); | |
| 10465 | 10572 | errdefer msg.destroy(sema.gpa); |
| 10466 | 10573 | switch (operand_ty.zigTypeTag(mod)) { |
| 10467 | .Int, .ComptimeInt => try sema.errNote(src, msg, "use @enumFromInt to cast from '{}'", .{operand_ty.fmt(mod)}), | |
| 10574 | .Int, .ComptimeInt => try sema.errNote(src, msg, "use @enumFromInt to cast from '{}'", .{operand_ty.fmt(pt)}), | |
| 10468 | 10575 | else => {}, |
| 10469 | 10576 | } |
| 10470 | 10577 | |
| ... | ... | @@ -10475,11 +10582,11 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 10475 | 10582 | |
| 10476 | 10583 | .Pointer => { |
| 10477 | 10584 | const msg = msg: { |
| 10478 | const msg = try sema.errMsg(src, "cannot @bitCast to '{}'", .{dest_ty.fmt(mod)}); | |
| 10585 | const msg = try sema.errMsg(src, "cannot @bitCast to '{}'", .{dest_ty.fmt(pt)}); | |
| 10479 | 10586 | errdefer msg.destroy(sema.gpa); |
| 10480 | 10587 | switch (operand_ty.zigTypeTag(mod)) { |
| 10481 | .Int, .ComptimeInt => try sema.errNote(src, msg, "use @ptrFromInt to cast from '{}'", .{operand_ty.fmt(mod)}), | |
| 10482 | .Pointer => try sema.errNote(src, msg, "use @ptrCast to cast from '{}'", .{operand_ty.fmt(mod)}), | |
| 10588 | .Int, .ComptimeInt => try sema.errNote(src, msg, "use @ptrFromInt to cast from '{}'", .{operand_ty.fmt(pt)}), | |
| 10589 | .Pointer => try sema.errNote(src, msg, "use @ptrCast to cast from '{}'", .{operand_ty.fmt(pt)}), | |
| 10483 | 10590 | else => {}, |
| 10484 | 10591 | } |
| 10485 | 10592 | |
| ... | ... | @@ -10494,7 +10601,7 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 10494 | 10601 | else => unreachable, |
| 10495 | 10602 | }; |
| 10496 | 10603 | return sema.fail(block, src, "cannot @bitCast to '{}'; {s} does not have a guaranteed in-memory layout", .{ |
| 10497 | dest_ty.fmt(mod), container, | |
| 10604 | dest_ty.fmt(pt), container, | |
| 10498 | 10605 | }); |
| 10499 | 10606 | }, |
| 10500 | 10607 | |
| ... | ... | @@ -10521,14 +10628,14 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 10521 | 10628 | .Type, |
| 10522 | 10629 | .Undefined, |
| 10523 | 10630 | .Void, |
| 10524 | => return sema.fail(block, operand_src, "cannot @bitCast from '{}'", .{operand_ty.fmt(mod)}), | |
| 10631 | => return sema.fail(block, operand_src, "cannot @bitCast from '{}'", .{operand_ty.fmt(pt)}), | |
| 10525 | 10632 | |
| 10526 | 10633 | .Enum => { |
| 10527 | 10634 | const msg = msg: { |
| 10528 | const msg = try sema.errMsg(operand_src, "cannot @bitCast from '{}'", .{operand_ty.fmt(mod)}); | |
| 10635 | const msg = try sema.errMsg(operand_src, "cannot @bitCast from '{}'", .{operand_ty.fmt(pt)}); | |
| 10529 | 10636 | errdefer msg.destroy(sema.gpa); |
| 10530 | 10637 | switch (dest_ty.zigTypeTag(mod)) { |
| 10531 | .Int, .ComptimeInt => try sema.errNote(operand_src, msg, "use @intFromEnum to cast to '{}'", .{dest_ty.fmt(mod)}), | |
| 10638 | .Int, .ComptimeInt => try sema.errNote(operand_src, msg, "use @intFromEnum to cast to '{}'", .{dest_ty.fmt(pt)}), | |
| 10532 | 10639 | else => {}, |
| 10533 | 10640 | } |
| 10534 | 10641 | |
| ... | ... | @@ -10538,11 +10645,11 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 10538 | 10645 | }, |
| 10539 | 10646 | .Pointer => { |
| 10540 | 10647 | const msg = msg: { |
| 10541 | const msg = try sema.errMsg(operand_src, "cannot @bitCast from '{}'", .{operand_ty.fmt(mod)}); | |
| 10648 | const msg = try sema.errMsg(operand_src, "cannot @bitCast from '{}'", .{operand_ty.fmt(pt)}); | |
| 10542 | 10649 | errdefer msg.destroy(sema.gpa); |
| 10543 | 10650 | switch (dest_ty.zigTypeTag(mod)) { |
| 10544 | .Int, .ComptimeInt => try sema.errNote(operand_src, msg, "use @intFromPtr to cast to '{}'", .{dest_ty.fmt(mod)}), | |
| 10545 | .Pointer => try sema.errNote(operand_src, msg, "use @ptrCast to cast to '{}'", .{dest_ty.fmt(mod)}), | |
| 10651 | .Int, .ComptimeInt => try sema.errNote(operand_src, msg, "use @intFromPtr to cast to '{}'", .{dest_ty.fmt(pt)}), | |
| 10652 | .Pointer => try sema.errNote(operand_src, msg, "use @ptrCast to cast to '{}'", .{dest_ty.fmt(pt)}), | |
| 10546 | 10653 | else => {}, |
| 10547 | 10654 | } |
| 10548 | 10655 | |
| ... | ... | @@ -10557,7 +10664,7 @@ fn zirBitcast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 10557 | 10664 | else => unreachable, |
| 10558 | 10665 | }; |
| 10559 | 10666 | return sema.fail(block, operand_src, "cannot @bitCast from '{}'; {s} does not have a guaranteed in-memory layout", .{ |
| 10560 | operand_ty.fmt(mod), container, | |
| 10667 | operand_ty.fmt(pt), container, | |
| 10561 | 10668 | }); |
| 10562 | 10669 | }, |
| 10563 | 10670 | |
| ... | ... | @@ -10575,7 +10682,8 @@ fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 10575 | 10682 | const tracy = trace(@src()); |
| 10576 | 10683 | defer tracy.end(); |
| 10577 | 10684 | |
| 10578 | const mod = sema.mod; | |
| 10685 | const pt = sema.pt; | |
| 10686 | const mod = pt.zcu; | |
| 10579 | 10687 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 10580 | 10688 | const src = block.nodeOffset(inst_data.src_node); |
| 10581 | 10689 | const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| ... | ... | @@ -10599,7 +10707,7 @@ fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 10599 | 10707 | block, |
| 10600 | 10708 | src, |
| 10601 | 10709 | "expected float or vector type, found '{}'", |
| 10602 | .{dest_ty.fmt(mod)}, | |
| 10710 | .{dest_ty.fmt(pt)}, | |
| 10603 | 10711 | ), |
| 10604 | 10712 | }; |
| 10605 | 10713 | |
| ... | ... | @@ -10609,21 +10717,21 @@ fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 10609 | 10717 | block, |
| 10610 | 10718 | operand_src, |
| 10611 | 10719 | "expected float or vector type, found '{}'", |
| 10612 | .{operand_ty.fmt(mod)}, | |
| 10720 | .{operand_ty.fmt(pt)}, | |
| 10613 | 10721 | ), |
| 10614 | 10722 | } |
| 10615 | 10723 | |
| 10616 | 10724 | if (try sema.resolveValue(operand)) |operand_val| { |
| 10617 | 10725 | if (!is_vector) { |
| 10618 | return Air.internedToRef((try operand_val.floatCast(dest_ty, mod)).toIntern()); | |
| 10726 | return Air.internedToRef((try operand_val.floatCast(dest_ty, pt)).toIntern()); | |
| 10619 | 10727 | } |
| 10620 | 10728 | const vec_len = operand_ty.vectorLen(mod); |
| 10621 | 10729 | const new_elems = try sema.arena.alloc(InternPool.Index, vec_len); |
| 10622 | 10730 | for (new_elems, 0..) |*new_elem, i| { |
| 10623 | const old_elem = try operand_val.elemValue(mod, i); | |
| 10624 | new_elem.* = (try old_elem.floatCast(dest_scalar_ty, mod)).toIntern(); | |
| 10731 | const old_elem = try operand_val.elemValue(pt, i); | |
| 10732 | new_elem.* = (try old_elem.floatCast(dest_scalar_ty, pt)).toIntern(); | |
| 10625 | 10733 | } |
| 10626 | return Air.internedToRef(try mod.intern(.{ .aggregate = .{ | |
| 10734 | return Air.internedToRef(try pt.intern(.{ .aggregate = .{ | |
| 10627 | 10735 | .ty = dest_ty.toIntern(), |
| 10628 | 10736 | .storage = .{ .elems = new_elems }, |
| 10629 | 10737 | } })); |
| ... | ... | @@ -10644,7 +10752,7 @@ fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 10644 | 10752 | const vec_len = operand_ty.vectorLen(mod); |
| 10645 | 10753 | const new_elems = try sema.arena.alloc(Air.Inst.Ref, vec_len); |
| 10646 | 10754 | for (new_elems, 0..) |*new_elem, i| { |
| 10647 | const idx_ref = try mod.intRef(Type.usize, i); | |
| 10755 | const idx_ref = try pt.intRef(Type.usize, i); | |
| 10648 | 10756 | const old_elem = try block.addBinOp(.array_elem_val, operand, idx_ref); |
| 10649 | 10757 | new_elem.* = try block.addTyOp(.fptrunc, dest_scalar_ty, old_elem); |
| 10650 | 10758 | } |
| ... | ... | @@ -10681,10 +10789,9 @@ fn zirElemValImm(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 10681 | 10789 | const tracy = trace(@src()); |
| 10682 | 10790 | defer tracy.end(); |
| 10683 | 10791 | |
| 10684 | const mod = sema.mod; | |
| 10685 | 10792 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].elem_val_imm; |
| 10686 | 10793 | const array = try sema.resolveInst(inst_data.operand); |
| 10687 | const elem_index = try mod.intRef(Type.usize, inst_data.idx); | |
| 10794 | const elem_index = try sema.pt.intRef(Type.usize, inst_data.idx); | |
| 10688 | 10795 | return sema.elemVal(block, LazySrcLoc.unneeded, array, elem_index, LazySrcLoc.unneeded, false); |
| 10689 | 10796 | } |
| 10690 | 10797 | |
| ... | ... | @@ -10692,7 +10799,8 @@ fn zirElemPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 10692 | 10799 | const tracy = trace(@src()); |
| 10693 | 10800 | defer tracy.end(); |
| 10694 | 10801 | |
| 10695 | const mod = sema.mod; | |
| 10802 | const pt = sema.pt; | |
| 10803 | const mod = pt.zcu; | |
| 10696 | 10804 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 10697 | 10805 | const src = block.nodeOffset(inst_data.src_node); |
| 10698 | 10806 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| ... | ... | @@ -10703,7 +10811,7 @@ fn zirElemPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 10703 | 10811 | const capture_src = block.src(.{ .for_capture_from_input = inst_data.src_node }); |
| 10704 | 10812 | const msg = msg: { |
| 10705 | 10813 | const msg = try sema.errMsg(capture_src, "pointer capture of non pointer type '{}'", .{ |
| 10706 | indexable_ty.fmt(mod), | |
| 10814 | indexable_ty.fmt(pt), | |
| 10707 | 10815 | }); |
| 10708 | 10816 | errdefer msg.destroy(sema.gpa); |
| 10709 | 10817 | if (indexable_ty.isIndexable(mod)) { |
| ... | ... | @@ -10734,12 +10842,13 @@ fn zirArrayInitElemPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compile |
| 10734 | 10842 | const tracy = trace(@src()); |
| 10735 | 10843 | defer tracy.end(); |
| 10736 | 10844 | |
| 10737 | const mod = sema.mod; | |
| 10845 | const pt = sema.pt; | |
| 10846 | const mod = pt.zcu; | |
| 10738 | 10847 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 10739 | 10848 | const src = block.nodeOffset(inst_data.src_node); |
| 10740 | 10849 | const extra = sema.code.extraData(Zir.Inst.ElemPtrImm, inst_data.payload_index).data; |
| 10741 | 10850 | const array_ptr = try sema.resolveInst(extra.ptr); |
| 10742 | const elem_index = try sema.mod.intRef(Type.usize, extra.index); | |
| 10851 | const elem_index = try pt.intRef(Type.usize, extra.index); | |
| 10743 | 10852 | const array_ty = sema.typeOf(array_ptr).childType(mod); |
| 10744 | 10853 | switch (array_ty.zigTypeTag(mod)) { |
| 10745 | 10854 | .Array, .Vector => {}, |
| ... | ... | @@ -10892,7 +11001,7 @@ const SwitchProngAnalysis = struct { |
| 10892 | 11001 | inline_case_capture, |
| 10893 | 11002 | ); |
| 10894 | 11003 | |
| 10895 | if (sema.typeOf(capture_ref).isNoReturn(sema.mod)) { | |
| 11004 | if (sema.typeOf(capture_ref).isNoReturn(sema.pt.zcu)) { | |
| 10896 | 11005 | // This prong should be unreachable! |
| 10897 | 11006 | return .unreachable_value; |
| 10898 | 11007 | } |
| ... | ... | @@ -10948,7 +11057,7 @@ const SwitchProngAnalysis = struct { |
| 10948 | 11057 | inline_case_capture, |
| 10949 | 11058 | ); |
| 10950 | 11059 | |
| 10951 | if (sema.typeOf(capture_ref).isNoReturn(sema.mod)) { | |
| 11060 | if (sema.typeOf(capture_ref).isNoReturn(sema.pt.zcu)) { | |
| 10952 | 11061 | // No need to analyze any further, the prong is unreachable |
| 10953 | 11062 | return; |
| 10954 | 11063 | } |
| ... | ... | @@ -10968,7 +11077,8 @@ const SwitchProngAnalysis = struct { |
| 10968 | 11077 | inline_case_capture: Air.Inst.Ref, |
| 10969 | 11078 | ) CompileError!Air.Inst.Ref { |
| 10970 | 11079 | const sema = spa.sema; |
| 10971 | const mod = sema.mod; | |
| 11080 | const pt = sema.pt; | |
| 11081 | const mod = pt.zcu; | |
| 10972 | 11082 | const operand_ty = sema.typeOf(spa.operand); |
| 10973 | 11083 | if (operand_ty.zigTypeTag(mod) != .Union) { |
| 10974 | 11084 | const tag_capture_src: LazySrcLoc = .{ |
| ... | ... | @@ -10976,7 +11086,7 @@ const SwitchProngAnalysis = struct { |
| 10976 | 11086 | .offset = .{ .switch_tag_capture = capture_src.offset.switch_capture }, |
| 10977 | 11087 | }; |
| 10978 | 11088 | return sema.fail(block, tag_capture_src, "cannot capture tag of non-union type '{}'", .{ |
| 10979 | operand_ty.fmt(mod), | |
| 11089 | operand_ty.fmt(pt), | |
| 10980 | 11090 | }); |
| 10981 | 11091 | } |
| 10982 | 11092 | assert(inline_case_capture != .none); |
| ... | ... | @@ -10993,7 +11103,8 @@ const SwitchProngAnalysis = struct { |
| 10993 | 11103 | inline_case_capture: Air.Inst.Ref, |
| 10994 | 11104 | ) CompileError!Air.Inst.Ref { |
| 10995 | 11105 | const sema = spa.sema; |
| 10996 | const zcu = sema.mod; | |
| 11106 | const pt = sema.pt; | |
| 11107 | const zcu = pt.zcu; | |
| 10997 | 11108 | const ip = &zcu.intern_pool; |
| 10998 | 11109 | |
| 10999 | 11110 | const zir_datas = sema.code.instructions.items(.data); |
| ... | ... | @@ -11010,7 +11121,7 @@ const SwitchProngAnalysis = struct { |
| 11010 | 11121 | const union_obj = zcu.typeToUnion(operand_ty).?; |
| 11011 | 11122 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 11012 | 11123 | if (capture_byref) { |
| 11013 | const ptr_field_ty = try zcu.ptrTypeSema(.{ | |
| 11124 | const ptr_field_ty = try pt.ptrTypeSema(.{ | |
| 11014 | 11125 | .child = field_ty.toIntern(), |
| 11015 | 11126 | .flags = .{ |
| 11016 | 11127 | .is_const = !operand_ptr_ty.ptrIsMutable(zcu), |
| ... | ... | @@ -11019,7 +11130,7 @@ const SwitchProngAnalysis = struct { |
| 11019 | 11130 | }, |
| 11020 | 11131 | }); |
| 11021 | 11132 | if (try sema.resolveDefinedValue(block, operand_src, spa.operand_ptr)) |union_ptr| { |
| 11022 | return Air.internedToRef((try union_ptr.ptrField(field_index, zcu)).toIntern()); | |
| 11133 | return Air.internedToRef((try union_ptr.ptrField(field_index, pt)).toIntern()); | |
| 11023 | 11134 | } |
| 11024 | 11135 | return block.addStructFieldPtr(spa.operand_ptr, field_index, ptr_field_ty); |
| 11025 | 11136 | } else { |
| ... | ... | @@ -11078,7 +11189,7 @@ const SwitchProngAnalysis = struct { |
| 11078 | 11189 | const dummy_captures = try sema.arena.alloc(Air.Inst.Ref, case_vals.len); |
| 11079 | 11190 | for (dummy_captures, field_indices) |*dummy, field_idx| { |
| 11080 | 11191 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_idx]); |
| 11081 | dummy.* = try zcu.undefRef(field_ty); | |
| 11192 | dummy.* = try pt.undefRef(field_ty); | |
| 11082 | 11193 | } |
| 11083 | 11194 | |
| 11084 | 11195 | const case_srcs = try sema.arena.alloc(?LazySrcLoc, case_vals.len); |
| ... | ... | @@ -11113,7 +11224,7 @@ const SwitchProngAnalysis = struct { |
| 11113 | 11224 | const dummy_captures = try sema.arena.alloc(Air.Inst.Ref, case_vals.len); |
| 11114 | 11225 | for (field_indices, dummy_captures) |field_idx, *dummy| { |
| 11115 | 11226 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_idx]); |
| 11116 | const field_ptr_ty = try zcu.ptrTypeSema(.{ | |
| 11227 | const field_ptr_ty = try pt.ptrTypeSema(.{ | |
| 11117 | 11228 | .child = field_ty.toIntern(), |
| 11118 | 11229 | .flags = .{ |
| 11119 | 11230 | .is_const = operand_ptr_info.flags.is_const, |
| ... | ... | @@ -11122,7 +11233,7 @@ const SwitchProngAnalysis = struct { |
| 11122 | 11233 | .alignment = union_obj.fieldAlign(ip, field_idx), |
| 11123 | 11234 | }, |
| 11124 | 11235 | }); |
| 11125 | dummy.* = try zcu.undefRef(field_ptr_ty); | |
| 11236 | dummy.* = try pt.undefRef(field_ptr_ty); | |
| 11126 | 11237 | } |
| 11127 | 11238 | const case_srcs = try sema.arena.alloc(?LazySrcLoc, case_vals.len); |
| 11128 | 11239 | for (case_srcs, 0..) |*case_src, i| { |
| ... | ... | @@ -11148,9 +11259,9 @@ const SwitchProngAnalysis = struct { |
| 11148 | 11259 | }; |
| 11149 | 11260 | |
| 11150 | 11261 | if (try sema.resolveDefinedValue(block, operand_src, spa.operand_ptr)) |op_ptr_val| { |
| 11151 | if (op_ptr_val.isUndef(zcu)) return zcu.undefRef(capture_ptr_ty); | |
| 11152 | const field_ptr_val = try op_ptr_val.ptrField(first_field_index, zcu); | |
| 11153 | return Air.internedToRef((try zcu.getCoerced(field_ptr_val, capture_ptr_ty)).toIntern()); | |
| 11262 | if (op_ptr_val.isUndef(zcu)) return pt.undefRef(capture_ptr_ty); | |
| 11263 | const field_ptr_val = try op_ptr_val.ptrField(first_field_index, pt); | |
| 11264 | return Air.internedToRef((try pt.getCoerced(field_ptr_val, capture_ptr_ty)).toIntern()); | |
| 11154 | 11265 | } |
| 11155 | 11266 | |
| 11156 | 11267 | try sema.requireRuntimeBlock(block, operand_src, null); |
| ... | ... | @@ -11158,9 +11269,9 @@ const SwitchProngAnalysis = struct { |
| 11158 | 11269 | } |
| 11159 | 11270 | |
| 11160 | 11271 | if (try sema.resolveDefinedValue(block, operand_src, spa.operand)) |operand_val| { |
| 11161 | if (operand_val.isUndef(zcu)) return zcu.undefRef(capture_ty); | |
| 11272 | if (operand_val.isUndef(zcu)) return pt.undefRef(capture_ty); | |
| 11162 | 11273 | const union_val = ip.indexToKey(operand_val.toIntern()).un; |
| 11163 | if (Value.fromInterned(union_val.tag).isUndef(zcu)) return zcu.undefRef(capture_ty); | |
| 11274 | if (Value.fromInterned(union_val.tag).isUndef(zcu)) return pt.undefRef(capture_ty); | |
| 11164 | 11275 | const uncoerced = Air.internedToRef(union_val.val); |
| 11165 | 11276 | return sema.coerce(block, capture_ty, uncoerced, operand_src); |
| 11166 | 11277 | } |
| ... | ... | @@ -11304,7 +11415,7 @@ const SwitchProngAnalysis = struct { |
| 11304 | 11415 | |
| 11305 | 11416 | if (case_vals.len == 1) { |
| 11306 | 11417 | const item_val = sema.resolveConstDefinedValue(block, LazySrcLoc.unneeded, case_vals[0], undefined) catch unreachable; |
| 11307 | const item_ty = try zcu.singleErrorSetType(item_val.getErrorName(zcu).unwrap().?); | |
| 11418 | const item_ty = try pt.singleErrorSetType(item_val.getErrorName(zcu).unwrap().?); | |
| 11308 | 11419 | return sema.bitCast(block, item_ty, spa.operand, operand_src, null); |
| 11309 | 11420 | } |
| 11310 | 11421 | |
| ... | ... | @@ -11314,7 +11425,7 @@ const SwitchProngAnalysis = struct { |
| 11314 | 11425 | const err_val = sema.resolveConstDefinedValue(block, LazySrcLoc.unneeded, err, undefined) catch unreachable; |
| 11315 | 11426 | names.putAssumeCapacityNoClobber(err_val.getErrorName(zcu).unwrap().?, {}); |
| 11316 | 11427 | } |
| 11317 | const error_ty = try zcu.errorSetFromUnsortedNames(names.keys()); | |
| 11428 | const error_ty = try pt.errorSetFromUnsortedNames(names.keys()); | |
| 11318 | 11429 | return sema.bitCast(block, error_ty, spa.operand, operand_src, null); |
| 11319 | 11430 | }, |
| 11320 | 11431 | else => { |
| ... | ... | @@ -11336,7 +11447,8 @@ fn switchCond( |
| 11336 | 11447 | src: LazySrcLoc, |
| 11337 | 11448 | operand: Air.Inst.Ref, |
| 11338 | 11449 | ) CompileError!Air.Inst.Ref { |
| 11339 | const mod = sema.mod; | |
| 11450 | const pt = sema.pt; | |
| 11451 | const mod = pt.zcu; | |
| 11340 | 11452 | const operand_ty = sema.typeOf(operand); |
| 11341 | 11453 | switch (operand_ty.zigTypeTag(mod)) { |
| 11342 | 11454 | .Type, |
| ... | ... | @@ -11353,7 +11465,7 @@ fn switchCond( |
| 11353 | 11465 | .Enum, |
| 11354 | 11466 | => { |
| 11355 | 11467 | if (operand_ty.isSlice(mod)) { |
| 11356 | return sema.fail(block, src, "switch on type '{}'", .{operand_ty.fmt(mod)}); | |
| 11468 | return sema.fail(block, src, "switch on type '{}'", .{operand_ty.fmt(pt)}); | |
| 11357 | 11469 | } |
| 11358 | 11470 | if ((try sema.typeHasOnePossibleValue(operand_ty))) |opv| { |
| 11359 | 11471 | return Air.internedToRef(opv.toIntern()); |
| ... | ... | @@ -11362,7 +11474,7 @@ fn switchCond( |
| 11362 | 11474 | }, |
| 11363 | 11475 | |
| 11364 | 11476 | .Union => { |
| 11365 | try operand_ty.resolveFields(mod); | |
| 11477 | try operand_ty.resolveFields(pt); | |
| 11366 | 11478 | const enum_ty = operand_ty.unionTagType(mod) orelse { |
| 11367 | 11479 | const msg = msg: { |
| 11368 | 11480 | const msg = try sema.errMsg(src, "switch on union with no attached enum", .{}); |
| ... | ... | @@ -11388,7 +11500,7 @@ fn switchCond( |
| 11388 | 11500 | .Vector, |
| 11389 | 11501 | .Frame, |
| 11390 | 11502 | .AnyFrame, |
| 11391 | => return sema.fail(block, src, "switch on type '{}'", .{operand_ty.fmt(mod)}), | |
| 11503 | => return sema.fail(block, src, "switch on type '{}'", .{operand_ty.fmt(pt)}), | |
| 11392 | 11504 | } |
| 11393 | 11505 | } |
| 11394 | 11506 | |
| ... | ... | @@ -11398,7 +11510,8 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp |
| 11398 | 11510 | const tracy = trace(@src()); |
| 11399 | 11511 | defer tracy.end(); |
| 11400 | 11512 | |
| 11401 | const mod = sema.mod; | |
| 11513 | const pt = sema.pt; | |
| 11514 | const mod = pt.zcu; | |
| 11402 | 11515 | const gpa = sema.gpa; |
| 11403 | 11516 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 11404 | 11517 | const switch_src = block.nodeOffset(inst_data.src_node); |
| ... | ... | @@ -11489,7 +11602,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp |
| 11489 | 11602 | |
| 11490 | 11603 | if (operand_err_set.zigTypeTag(mod) != .ErrorUnion) { |
| 11491 | 11604 | return sema.fail(block, switch_src, "expected error union type, found '{}'", .{ |
| 11492 | operand_ty.fmt(mod), | |
| 11605 | operand_ty.fmt(pt), | |
| 11493 | 11606 | }); |
| 11494 | 11607 | } |
| 11495 | 11608 | |
| ... | ... | @@ -11571,7 +11684,7 @@ fn zirSwitchBlockErrUnion(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Comp |
| 11571 | 11684 | if (operand_val.errorUnionIsPayload(mod)) { |
| 11572 | 11685 | return sema.resolveBlockBody(block, main_operand_src, &child_block, non_error_case.body, inst, merges); |
| 11573 | 11686 | } else { |
| 11574 | const err_val = Value.fromInterned(try mod.intern(.{ | |
| 11687 | const err_val = Value.fromInterned(try pt.intern(.{ | |
| 11575 | 11688 | .err = .{ |
| 11576 | 11689 | .ty = operand_err_set_ty.toIntern(), |
| 11577 | 11690 | .name = operand_val.getErrorName(mod).unwrap().?, |
| ... | ... | @@ -11708,7 +11821,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r |
| 11708 | 11821 | const tracy = trace(@src()); |
| 11709 | 11822 | defer tracy.end(); |
| 11710 | 11823 | |
| 11711 | const mod = sema.mod; | |
| 11824 | const pt = sema.pt; | |
| 11825 | const mod = pt.zcu; | |
| 11712 | 11826 | const gpa = sema.gpa; |
| 11713 | 11827 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 11714 | 11828 | const src = block.nodeOffset(inst_data.src_node); |
| ... | ... | @@ -11783,7 +11897,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r |
| 11783 | 11897 | // Duplicate checking variables later also used for `inline else`. |
| 11784 | 11898 | var seen_enum_fields: []?LazySrcLoc = &.{}; |
| 11785 | 11899 | var seen_errors = SwitchErrorSet.init(gpa); |
| 11786 | var range_set = RangeSet.init(gpa, mod); | |
| 11900 | var range_set = RangeSet.init(gpa, pt); | |
| 11787 | 11901 | var true_count: u8 = 0; |
| 11788 | 11902 | var false_count: u8 = 0; |
| 11789 | 11903 | |
| ... | ... | @@ -11924,7 +12038,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r |
| 11924 | 12038 | operand_ty.srcLoc(mod), |
| 11925 | 12039 | msg, |
| 11926 | 12040 | "enum '{}' declared here", |
| 11927 | .{operand_ty.fmt(mod)}, | |
| 12041 | .{operand_ty.fmt(pt)}, | |
| 11928 | 12042 | ); |
| 11929 | 12043 | break :msg msg; |
| 11930 | 12044 | }; |
| ... | ... | @@ -12030,8 +12144,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r |
| 12030 | 12144 | |
| 12031 | 12145 | check_range: { |
| 12032 | 12146 | if (operand_ty.zigTypeTag(mod) == .Int) { |
| 12033 | const min_int = try operand_ty.minInt(mod, operand_ty); | |
| 12034 | const max_int = try operand_ty.maxInt(mod, operand_ty); | |
| 12147 | const min_int = try operand_ty.minInt(pt, operand_ty); | |
| 12148 | const max_int = try operand_ty.maxInt(pt, operand_ty); | |
| 12035 | 12149 | if (try range_set.spans(min_int.toIntern(), max_int.toIntern())) { |
| 12036 | 12150 | if (special_prong == .@"else") { |
| 12037 | 12151 | return sema.fail( |
| ... | ... | @@ -12136,7 +12250,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r |
| 12136 | 12250 | block, |
| 12137 | 12251 | src, |
| 12138 | 12252 | "else prong required when switching on type '{}'", |
| 12139 | .{operand_ty.fmt(mod)}, | |
| 12253 | .{operand_ty.fmt(pt)}, | |
| 12140 | 12254 | ); |
| 12141 | 12255 | } |
| 12142 | 12256 | |
| ... | ... | @@ -12212,7 +12326,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index, operand_is_r |
| 12212 | 12326 | .ComptimeFloat, |
| 12213 | 12327 | .Float, |
| 12214 | 12328 | => return sema.fail(block, operand_src, "invalid switch operand type '{}'", .{ |
| 12215 | operand_ty.fmt(mod), | |
| 12329 | operand_ty.fmt(pt), | |
| 12216 | 12330 | }), |
| 12217 | 12331 | } |
| 12218 | 12332 | |
| ... | ... | @@ -12386,7 +12500,8 @@ fn analyzeSwitchRuntimeBlock( |
| 12386 | 12500 | cond_dbg_node_index: Zir.Inst.Index, |
| 12387 | 12501 | allow_err_code_unwrap: bool, |
| 12388 | 12502 | ) CompileError!Air.Inst.Ref { |
| 12389 | const mod = sema.mod; | |
| 12503 | const pt = sema.pt; | |
| 12504 | const mod = pt.zcu; | |
| 12390 | 12505 | const gpa = sema.gpa; |
| 12391 | 12506 | const ip = &mod.intern_pool; |
| 12392 | 12507 | |
| ... | ... | @@ -12496,9 +12611,9 @@ fn analyzeSwitchRuntimeBlock( |
| 12496 | 12611 | var item = sema.resolveConstDefinedValue(block, LazySrcLoc.unneeded, item_first_ref, undefined) catch unreachable; |
| 12497 | 12612 | const item_last = sema.resolveConstDefinedValue(block, LazySrcLoc.unneeded, item_last_ref, undefined) catch unreachable; |
| 12498 | 12613 | |
| 12499 | while (item.compareScalar(.lte, item_last, operand_ty, mod)) : ({ | |
| 12614 | while (item.compareScalar(.lte, item_last, operand_ty, pt)) : ({ | |
| 12500 | 12615 | // Previous validation has resolved any possible lazy values. |
| 12501 | item = sema.intAddScalar(item, try mod.intValue(operand_ty, 1), operand_ty) catch |err| switch (err) { | |
| 12616 | item = sema.intAddScalar(item, try pt.intValue(operand_ty, 1), operand_ty) catch |err| switch (err) { | |
| 12502 | 12617 | error.Overflow => unreachable, |
| 12503 | 12618 | else => |e| return e, |
| 12504 | 12619 | }; |
| ... | ... | @@ -12537,7 +12652,7 @@ fn analyzeSwitchRuntimeBlock( |
| 12537 | 12652 | cases_extra.appendAssumeCapacity(@intFromEnum(item_ref)); |
| 12538 | 12653 | cases_extra.appendSliceAssumeCapacity(@ptrCast(case_block.instructions.items)); |
| 12539 | 12654 | |
| 12540 | if (item.compareScalar(.eq, item_last, operand_ty, mod)) break; | |
| 12655 | if (item.compareScalar(.eq, item_last, operand_ty, pt)) break; | |
| 12541 | 12656 | } |
| 12542 | 12657 | } |
| 12543 | 12658 | |
| ... | ... | @@ -12744,14 +12859,14 @@ fn analyzeSwitchRuntimeBlock( |
| 12744 | 12859 | .Enum => { |
| 12745 | 12860 | if (operand_ty.isNonexhaustiveEnum(mod) and !union_originally) { |
| 12746 | 12861 | return sema.fail(block, special_prong_src, "cannot enumerate values of type '{}' for 'inline else'", .{ |
| 12747 | operand_ty.fmt(mod), | |
| 12862 | operand_ty.fmt(pt), | |
| 12748 | 12863 | }); |
| 12749 | 12864 | } |
| 12750 | 12865 | for (seen_enum_fields, 0..) |f, i| { |
| 12751 | 12866 | if (f != null) continue; |
| 12752 | 12867 | cases_len += 1; |
| 12753 | 12868 | |
| 12754 | const item_val = try mod.enumValueFieldIndex(operand_ty, @intCast(i)); | |
| 12869 | const item_val = try pt.enumValueFieldIndex(operand_ty, @intCast(i)); | |
| 12755 | 12870 | const item_ref = Air.internedToRef(item_val.toIntern()); |
| 12756 | 12871 | |
| 12757 | 12872 | case_block.instructions.shrinkRetainingCapacity(0); |
| ... | ... | @@ -12793,7 +12908,7 @@ fn analyzeSwitchRuntimeBlock( |
| 12793 | 12908 | .ErrorSet => { |
| 12794 | 12909 | if (operand_ty.isAnyError(mod)) { |
| 12795 | 12910 | return sema.fail(block, special_prong_src, "cannot enumerate values of type '{}' for 'inline else'", .{ |
| 12796 | operand_ty.fmt(mod), | |
| 12911 | operand_ty.fmt(pt), | |
| 12797 | 12912 | }); |
| 12798 | 12913 | } |
| 12799 | 12914 | const error_names = operand_ty.errorSetNames(mod); |
| ... | ... | @@ -12802,7 +12917,7 @@ fn analyzeSwitchRuntimeBlock( |
| 12802 | 12917 | if (seen_errors.contains(error_name)) continue; |
| 12803 | 12918 | cases_len += 1; |
| 12804 | 12919 | |
| 12805 | const item_val = try mod.intern(.{ .err = .{ | |
| 12920 | const item_val = try pt.intern(.{ .err = .{ | |
| 12806 | 12921 | .ty = operand_ty.toIntern(), |
| 12807 | 12922 | .name = error_name, |
| 12808 | 12923 | } }); |
| ... | ... | @@ -12930,7 +13045,7 @@ fn analyzeSwitchRuntimeBlock( |
| 12930 | 13045 | } |
| 12931 | 13046 | }, |
| 12932 | 13047 | else => return sema.fail(block, special_prong_src, "cannot enumerate values of type '{}' for 'inline else'", .{ |
| 12933 | operand_ty.fmt(mod), | |
| 13048 | operand_ty.fmt(pt), | |
| 12934 | 13049 | }), |
| 12935 | 13050 | }; |
| 12936 | 13051 | |
| ... | ... | @@ -13051,7 +13166,7 @@ fn resolveSwitchComptime( |
| 13051 | 13166 | |
| 13052 | 13167 | const item = case_vals.items[scalar_i]; |
| 13053 | 13168 | const item_val = sema.resolveConstDefinedValue(child_block, LazySrcLoc.unneeded, item, undefined) catch unreachable; |
| 13054 | if (operand_val.eql(item_val, operand_ty, sema.mod)) { | |
| 13169 | if (operand_val.eql(item_val, operand_ty, sema.pt.zcu)) { | |
| 13055 | 13170 | if (err_set) try sema.maybeErrorUnwrapComptime(child_block, body, cond_operand); |
| 13056 | 13171 | return spa.resolveProngComptime( |
| 13057 | 13172 | child_block, |
| ... | ... | @@ -13088,7 +13203,7 @@ fn resolveSwitchComptime( |
| 13088 | 13203 | for (items) |item| { |
| 13089 | 13204 | // Validation above ensured these will succeed. |
| 13090 | 13205 | const item_val = sema.resolveConstDefinedValue(child_block, LazySrcLoc.unneeded, item, undefined) catch unreachable; |
| 13091 | if (operand_val.eql(item_val, operand_ty, sema.mod)) { | |
| 13206 | if (operand_val.eql(item_val, operand_ty, sema.pt.zcu)) { | |
| 13092 | 13207 | if (err_set) try sema.maybeErrorUnwrapComptime(child_block, body, cond_operand); |
| 13093 | 13208 | return spa.resolveProngComptime( |
| 13094 | 13209 | child_block, |
| ... | ... | @@ -13162,7 +13277,7 @@ fn resolveSwitchComptime( |
| 13162 | 13277 | } |
| 13163 | 13278 | |
| 13164 | 13279 | const RangeSetUnhandledIterator = struct { |
| 13165 | mod: *Module, | |
| 13280 | pt: Zcu.PerThread, | |
| 13166 | 13281 | cur: ?InternPool.Index, |
| 13167 | 13282 | max: InternPool.Index, |
| 13168 | 13283 | range_i: usize, |
| ... | ... | @@ -13172,13 +13287,13 @@ const RangeSetUnhandledIterator = struct { |
| 13172 | 13287 | const preallocated_limbs = math.big.int.calcTwosCompLimbCount(128); |
| 13173 | 13288 | |
| 13174 | 13289 | fn init(sema: *Sema, ty: Type, range_set: RangeSet) !RangeSetUnhandledIterator { |
| 13175 | const mod = sema.mod; | |
| 13176 | const int_type = mod.intern_pool.indexToKey(ty.toIntern()).int_type; | |
| 13290 | const pt = sema.pt; | |
| 13291 | const int_type = pt.zcu.intern_pool.indexToKey(ty.toIntern()).int_type; | |
| 13177 | 13292 | const needed_limbs = math.big.int.calcTwosCompLimbCount(int_type.bits); |
| 13178 | 13293 | return .{ |
| 13179 | .mod = mod, | |
| 13180 | .cur = (try ty.minInt(mod, ty)).toIntern(), | |
| 13181 | .max = (try ty.maxInt(mod, ty)).toIntern(), | |
| 13294 | .pt = pt, | |
| 13295 | .cur = (try ty.minInt(pt, ty)).toIntern(), | |
| 13296 | .max = (try ty.maxInt(pt, ty)).toIntern(), | |
| 13182 | 13297 | .range_i = 0, |
| 13183 | 13298 | .ranges = range_set.ranges.items, |
| 13184 | 13299 | .limbs = if (needed_limbs > preallocated_limbs) |
| ... | ... | @@ -13190,13 +13305,13 @@ const RangeSetUnhandledIterator = struct { |
| 13190 | 13305 | |
| 13191 | 13306 | fn addOne(it: *const RangeSetUnhandledIterator, val: InternPool.Index) !?InternPool.Index { |
| 13192 | 13307 | if (val == it.max) return null; |
| 13193 | const int = it.mod.intern_pool.indexToKey(val).int; | |
| 13308 | const int = it.pt.zcu.intern_pool.indexToKey(val).int; | |
| 13194 | 13309 | |
| 13195 | 13310 | switch (int.storage) { |
| 13196 | 13311 | inline .u64, .i64 => |val_int| { |
| 13197 | 13312 | const next_int = @addWithOverflow(val_int, 1); |
| 13198 | 13313 | if (next_int[1] == 0) |
| 13199 | return (try it.mod.intValue(Type.fromInterned(int.ty), next_int[0])).toIntern(); | |
| 13314 | return (try it.pt.intValue(Type.fromInterned(int.ty), next_int[0])).toIntern(); | |
| 13200 | 13315 | }, |
| 13201 | 13316 | .big_int => {}, |
| 13202 | 13317 | .lazy_align, .lazy_size => unreachable, |
| ... | ... | @@ -13212,7 +13327,7 @@ const RangeSetUnhandledIterator = struct { |
| 13212 | 13327 | ); |
| 13213 | 13328 | |
| 13214 | 13329 | result_bigint.addScalar(val_bigint, 1); |
| 13215 | return (try it.mod.intValue_big(Type.fromInterned(int.ty), result_bigint.toConst())).toIntern(); | |
| 13330 | return (try it.pt.intValue_big(Type.fromInterned(int.ty), result_bigint.toConst())).toIntern(); | |
| 13216 | 13331 | } |
| 13217 | 13332 | |
| 13218 | 13333 | fn next(it: *RangeSetUnhandledIterator) !?InternPool.Index { |
| ... | ... | @@ -13274,7 +13389,8 @@ fn validateErrSetSwitch( |
| 13274 | 13389 | has_else: bool, |
| 13275 | 13390 | ) CompileError!?Type { |
| 13276 | 13391 | const gpa = sema.gpa; |
| 13277 | const mod = sema.mod; | |
| 13392 | const pt = sema.pt; | |
| 13393 | const mod = pt.zcu; | |
| 13278 | 13394 | const ip = &mod.intern_pool; |
| 13279 | 13395 | |
| 13280 | 13396 | const src_node_offset = inst_data.src_node; |
| ... | ... | @@ -13426,7 +13542,7 @@ fn validateErrSetSwitch( |
| 13426 | 13542 | } |
| 13427 | 13543 | // No need to keep the hash map metadata correct; here we |
| 13428 | 13544 | // extract the (sorted) keys only. |
| 13429 | return try mod.errorSetFromUnsortedNames(names.keys()); | |
| 13545 | return try pt.errorSetFromUnsortedNames(names.keys()); | |
| 13430 | 13546 | }, |
| 13431 | 13547 | } |
| 13432 | 13548 | return null; |
| ... | ... | @@ -13441,7 +13557,6 @@ fn validateSwitchRange( |
| 13441 | 13557 | operand_ty: Type, |
| 13442 | 13558 | item_src: LazySrcLoc, |
| 13443 | 13559 | ) CompileError![2]Air.Inst.Ref { |
| 13444 | const mod = sema.mod; | |
| 13445 | 13560 | const first_src: LazySrcLoc = .{ |
| 13446 | 13561 | .base_node_inst = item_src.base_node_inst, |
| 13447 | 13562 | .offset = .{ .switch_case_item_range_first = item_src.offset.switch_case_item }, |
| ... | ... | @@ -13452,7 +13567,7 @@ fn validateSwitchRange( |
| 13452 | 13567 | }; |
| 13453 | 13568 | const first = try sema.resolveSwitchItemVal(block, first_ref, operand_ty, first_src); |
| 13454 | 13569 | const last = try sema.resolveSwitchItemVal(block, last_ref, operand_ty, last_src); |
| 13455 | if (try Value.fromInterned(first.val).compareAll(.gt, Value.fromInterned(last.val), operand_ty, mod)) { | |
| 13570 | if (try Value.fromInterned(first.val).compareAll(.gt, Value.fromInterned(last.val), operand_ty, sema.pt)) { | |
| 13456 | 13571 | return sema.fail(block, item_src, "range start value is greater than the end value", .{}); |
| 13457 | 13572 | } |
| 13458 | 13573 | const maybe_prev_src = try range_set.add(first.val, last.val, item_src); |
| ... | ... | @@ -13483,7 +13598,7 @@ fn validateSwitchItemEnum( |
| 13483 | 13598 | operand_ty: Type, |
| 13484 | 13599 | item_src: LazySrcLoc, |
| 13485 | 13600 | ) CompileError!Air.Inst.Ref { |
| 13486 | const ip = &sema.mod.intern_pool; | |
| 13601 | const ip = &sema.pt.zcu.intern_pool; | |
| 13487 | 13602 | const item = try sema.resolveSwitchItemVal(block, item_ref, operand_ty, item_src); |
| 13488 | 13603 | const int = ip.indexToKey(item.val).enum_tag.int; |
| 13489 | 13604 | const field_index = ip.loadEnumType(ip.typeOf(item.val)).tagValueIndex(ip, int) orelse { |
| ... | ... | @@ -13505,9 +13620,8 @@ fn validateSwitchItemError( |
| 13505 | 13620 | operand_ty: Type, |
| 13506 | 13621 | item_src: LazySrcLoc, |
| 13507 | 13622 | ) CompileError!Air.Inst.Ref { |
| 13508 | const ip = &sema.mod.intern_pool; | |
| 13509 | 13623 | const item = try sema.resolveSwitchItemVal(block, item_ref, operand_ty, item_src); |
| 13510 | const error_name = ip.indexToKey(item.val).err.name; | |
| 13624 | const error_name = sema.pt.zcu.intern_pool.indexToKey(item.val).err.name; | |
| 13511 | 13625 | const maybe_prev_src = if (try seen_errors.fetchPut(error_name, item_src)) |prev| |
| 13512 | 13626 | prev.value |
| 13513 | 13627 | else |
| ... | ... | @@ -13593,7 +13707,7 @@ fn validateSwitchNoRange( |
| 13593 | 13707 | const msg = try sema.errMsg( |
| 13594 | 13708 | operand_src, |
| 13595 | 13709 | "ranges not allowed when switching on type '{}'", |
| 13596 | .{operand_ty.fmt(sema.mod)}, | |
| 13710 | .{operand_ty.fmt(sema.pt)}, | |
| 13597 | 13711 | ); |
| 13598 | 13712 | errdefer msg.destroy(sema.gpa); |
| 13599 | 13713 | try sema.errNote( |
| ... | ... | @@ -13615,7 +13729,8 @@ fn maybeErrorUnwrap( |
| 13615 | 13729 | operand_src: LazySrcLoc, |
| 13616 | 13730 | allow_err_code_inst: bool, |
| 13617 | 13731 | ) !bool { |
| 13618 | const mod = sema.mod; | |
| 13732 | const pt = sema.pt; | |
| 13733 | const mod = pt.zcu; | |
| 13619 | 13734 | if (!mod.backendSupportsFeature(.panic_unwrap_error)) return false; |
| 13620 | 13735 | |
| 13621 | 13736 | const tags = sema.code.instructions.items(.tag); |
| ... | ... | @@ -13654,7 +13769,7 @@ fn maybeErrorUnwrap( |
| 13654 | 13769 | return true; |
| 13655 | 13770 | } |
| 13656 | 13771 | |
| 13657 | const panic_fn = try mod.getBuiltin("panicUnwrapError"); | |
| 13772 | const panic_fn = try pt.getBuiltin("panicUnwrapError"); | |
| 13658 | 13773 | const err_return_trace = try sema.getErrorReturnTrace(block); |
| 13659 | 13774 | const args: [2]Air.Inst.Ref = .{ err_return_trace, operand }; |
| 13660 | 13775 | try sema.callBuiltin(block, operand_src, panic_fn, .auto, &args, .@"safety check"); |
| ... | ... | @@ -13664,7 +13779,7 @@ fn maybeErrorUnwrap( |
| 13664 | 13779 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 13665 | 13780 | const msg_inst = try sema.resolveInst(inst_data.operand); |
| 13666 | 13781 | |
| 13667 | const panic_fn = try mod.getBuiltin("panic"); | |
| 13782 | const panic_fn = try pt.getBuiltin("panic"); | |
| 13668 | 13783 | const err_return_trace = try sema.getErrorReturnTrace(block); |
| 13669 | 13784 | const args: [3]Air.Inst.Ref = .{ msg_inst, err_return_trace, .null_value }; |
| 13670 | 13785 | try sema.callBuiltin(block, operand_src, panic_fn, .auto, &args, .@"safety check"); |
| ... | ... | @@ -13680,7 +13795,8 @@ fn maybeErrorUnwrap( |
| 13680 | 13795 | } |
| 13681 | 13796 | |
| 13682 | 13797 | fn maybeErrorUnwrapCondbr(sema: *Sema, block: *Block, body: []const Zir.Inst.Index, cond: Zir.Inst.Ref, cond_src: LazySrcLoc) !void { |
| 13683 | const mod = sema.mod; | |
| 13798 | const pt = sema.pt; | |
| 13799 | const mod = pt.zcu; | |
| 13684 | 13800 | const index = cond.toIndex() orelse return; |
| 13685 | 13801 | if (sema.code.instructions.items(.tag)[@intFromEnum(index)] != .is_non_err) return; |
| 13686 | 13802 | |
| ... | ... | @@ -13713,14 +13829,15 @@ fn maybeErrorUnwrapComptime(sema: *Sema, block: *Block, body: []const Zir.Inst.I |
| 13713 | 13829 | const src = block.nodeOffset(inst_data.src_node); |
| 13714 | 13830 | |
| 13715 | 13831 | if (try sema.resolveDefinedValue(block, src, operand)) |val| { |
| 13716 | if (val.getErrorName(sema.mod).unwrap()) |name| { | |
| 13832 | if (val.getErrorName(sema.pt.zcu).unwrap()) |name| { | |
| 13717 | 13833 | return sema.failWithComptimeErrorRetTrace(block, src, name); |
| 13718 | 13834 | } |
| 13719 | 13835 | } |
| 13720 | 13836 | } |
| 13721 | 13837 | |
| 13722 | 13838 | fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 13723 | const mod = sema.mod; | |
| 13839 | const pt = sema.pt; | |
| 13840 | const mod = pt.zcu; | |
| 13724 | 13841 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 13725 | 13842 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 13726 | 13843 | const ty_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| ... | ... | @@ -13729,7 +13846,7 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 13729 | 13846 | const field_name = try sema.resolveConstStringIntern(block, name_src, extra.rhs, .{ |
| 13730 | 13847 | .needed_comptime_reason = "field name must be comptime-known", |
| 13731 | 13848 | }); |
| 13732 | try ty.resolveFields(mod); | |
| 13849 | try ty.resolveFields(pt); | |
| 13733 | 13850 | const ip = &mod.intern_pool; |
| 13734 | 13851 | |
| 13735 | 13852 | const has_field = hf: { |
| ... | ... | @@ -13764,14 +13881,15 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 13764 | 13881 | else => {}, |
| 13765 | 13882 | } |
| 13766 | 13883 | return sema.fail(block, ty_src, "type '{}' does not support '@hasField'", .{ |
| 13767 | ty.fmt(mod), | |
| 13884 | ty.fmt(pt), | |
| 13768 | 13885 | }); |
| 13769 | 13886 | }; |
| 13770 | 13887 | return if (has_field) .bool_true else .bool_false; |
| 13771 | 13888 | } |
| 13772 | 13889 | |
| 13773 | 13890 | fn zirHasDecl(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 13774 | const mod = sema.mod; | |
| 13891 | const pt = sema.pt; | |
| 13892 | const mod = pt.zcu; | |
| 13775 | 13893 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 13776 | 13894 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 13777 | 13895 | const src = block.nodeOffset(inst_data.src_node); |
| ... | ... | @@ -13804,7 +13922,8 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 13804 | 13922 | const tracy = trace(@src()); |
| 13805 | 13923 | defer tracy.end(); |
| 13806 | 13924 | |
| 13807 | const zcu = sema.mod; | |
| 13925 | const pt = sema.pt; | |
| 13926 | const zcu = pt.zcu; | |
| 13808 | 13927 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].str_tok; |
| 13809 | 13928 | const operand_src = block.tokenOffset(inst_data.src_tok); |
| 13810 | 13929 | const operand = inst_data.get(sema.code); |
| ... | ... | @@ -13824,7 +13943,7 @@ fn zirImport(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 13824 | 13943 | return sema.fail(block, operand_src, "unable to open '{s}': {s}", .{ operand, @errorName(err) }); |
| 13825 | 13944 | }, |
| 13826 | 13945 | }; |
| 13827 | try zcu.ensureFileAnalyzed(result.file_index); | |
| 13946 | try pt.ensureFileAnalyzed(result.file_index); | |
| 13828 | 13947 | const file_root_decl_index = zcu.fileRootDecl(result.file_index).unwrap().?; |
| 13829 | 13948 | return sema.analyzeDeclVal(block, operand_src, file_root_decl_index); |
| 13830 | 13949 | } |
| ... | ... | @@ -13833,7 +13952,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 13833 | 13952 | const tracy = trace(@src()); |
| 13834 | 13953 | defer tracy.end(); |
| 13835 | 13954 | |
| 13836 | const mod = sema.mod; | |
| 13955 | const pt = sema.pt; | |
| 13837 | 13956 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 13838 | 13957 | const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| 13839 | 13958 | const name = try sema.resolveConstString(block, operand_src, inst_data.operand, .{ |
| ... | ... | @@ -13844,7 +13963,7 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 13844 | 13963 | return sema.fail(block, operand_src, "file path name cannot be empty", .{}); |
| 13845 | 13964 | } |
| 13846 | 13965 | |
| 13847 | const val = mod.embedFile(block.getFileScope(mod), name, operand_src) catch |err| switch (err) { | |
| 13966 | const val = pt.embedFile(block.getFileScope(pt.zcu), name, operand_src) catch |err| switch (err) { | |
| 13848 | 13967 | error.ImportOutsideModulePath => { |
| 13849 | 13968 | return sema.fail(block, operand_src, "embed of file outside package path: '{s}'", .{name}); |
| 13850 | 13969 | }, |
| ... | ... | @@ -13859,7 +13978,8 @@ fn zirEmbedFile(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 13859 | 13978 | } |
| 13860 | 13979 | |
| 13861 | 13980 | fn zirRetErrValueCode(sema: *Sema, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 13862 | const mod = sema.mod; | |
| 13981 | const pt = sema.pt; | |
| 13982 | const mod = pt.zcu; | |
| 13863 | 13983 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].str_tok; |
| 13864 | 13984 | const name = try mod.intern_pool.getOrPutString( |
| 13865 | 13985 | sema.gpa, |
| ... | ... | @@ -13867,8 +13987,8 @@ fn zirRetErrValueCode(sema: *Sema, inst: Zir.Inst.Index) CompileError!Air.Inst.R |
| 13867 | 13987 | .no_embedded_nulls, |
| 13868 | 13988 | ); |
| 13869 | 13989 | _ = try mod.getErrorValue(name); |
| 13870 | const error_set_type = try mod.singleErrorSetType(name); | |
| 13871 | return Air.internedToRef((try mod.intern(.{ .err = .{ | |
| 13990 | const error_set_type = try pt.singleErrorSetType(name); | |
| 13991 | return Air.internedToRef((try pt.intern(.{ .err = .{ | |
| 13872 | 13992 | .ty = error_set_type.toIntern(), |
| 13873 | 13993 | .name = name, |
| 13874 | 13994 | } }))); |
| ... | ... | @@ -13883,7 +14003,8 @@ fn zirShl( |
| 13883 | 14003 | const tracy = trace(@src()); |
| 13884 | 14004 | defer tracy.end(); |
| 13885 | 14005 | |
| 13886 | const mod = sema.mod; | |
| 14006 | const pt = sema.pt; | |
| 14007 | const mod = pt.zcu; | |
| 13887 | 14008 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 13888 | 14009 | const src = block.nodeOffset(inst_data.src_node); |
| 13889 | 14010 | const lhs_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node }); |
| ... | ... | @@ -13906,53 +14027,53 @@ fn zirShl( |
| 13906 | 14027 | |
| 13907 | 14028 | if (maybe_rhs_val) |rhs_val| { |
| 13908 | 14029 | if (rhs_val.isUndef(mod)) { |
| 13909 | return mod.undefRef(sema.typeOf(lhs)); | |
| 14030 | return pt.undefRef(sema.typeOf(lhs)); | |
| 13910 | 14031 | } |
| 13911 | 14032 | // If rhs is 0, return lhs without doing any calculations. |
| 13912 | if (try rhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 14033 | if (try rhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 13913 | 14034 | return lhs; |
| 13914 | 14035 | } |
| 13915 | 14036 | if (scalar_ty.zigTypeTag(mod) != .ComptimeInt and air_tag != .shl_sat) { |
| 13916 | const bit_value = try mod.intValue(Type.comptime_int, scalar_ty.intInfo(mod).bits); | |
| 14037 | const bit_value = try pt.intValue(Type.comptime_int, scalar_ty.intInfo(mod).bits); | |
| 13917 | 14038 | if (rhs_ty.zigTypeTag(mod) == .Vector) { |
| 13918 | 14039 | var i: usize = 0; |
| 13919 | 14040 | while (i < rhs_ty.vectorLen(mod)) : (i += 1) { |
| 13920 | const rhs_elem = try rhs_val.elemValue(mod, i); | |
| 13921 | if (rhs_elem.compareHetero(.gte, bit_value, mod)) { | |
| 14041 | const rhs_elem = try rhs_val.elemValue(pt, i); | |
| 14042 | if (rhs_elem.compareHetero(.gte, bit_value, pt)) { | |
| 13922 | 14043 | return sema.fail(block, rhs_src, "shift amount '{}' at index '{d}' is too large for operand type '{}'", .{ |
| 13923 | rhs_elem.fmtValue(mod, sema), | |
| 14044 | rhs_elem.fmtValue(pt, sema), | |
| 13924 | 14045 | i, |
| 13925 | scalar_ty.fmt(mod), | |
| 14046 | scalar_ty.fmt(pt), | |
| 13926 | 14047 | }); |
| 13927 | 14048 | } |
| 13928 | 14049 | } |
| 13929 | } else if (rhs_val.compareHetero(.gte, bit_value, mod)) { | |
| 14050 | } else if (rhs_val.compareHetero(.gte, bit_value, pt)) { | |
| 13930 | 14051 | return sema.fail(block, rhs_src, "shift amount '{}' is too large for operand type '{}'", .{ |
| 13931 | rhs_val.fmtValue(mod, sema), | |
| 13932 | scalar_ty.fmt(mod), | |
| 14052 | rhs_val.fmtValue(pt, sema), | |
| 14053 | scalar_ty.fmt(pt), | |
| 13933 | 14054 | }); |
| 13934 | 14055 | } |
| 13935 | 14056 | } |
| 13936 | 14057 | if (rhs_ty.zigTypeTag(mod) == .Vector) { |
| 13937 | 14058 | var i: usize = 0; |
| 13938 | 14059 | while (i < rhs_ty.vectorLen(mod)) : (i += 1) { |
| 13939 | const rhs_elem = try rhs_val.elemValue(mod, i); | |
| 13940 | if (rhs_elem.compareHetero(.lt, try mod.intValue(scalar_rhs_ty, 0), mod)) { | |
| 14060 | const rhs_elem = try rhs_val.elemValue(pt, i); | |
| 14061 | if (rhs_elem.compareHetero(.lt, try pt.intValue(scalar_rhs_ty, 0), pt)) { | |
| 13941 | 14062 | return sema.fail(block, rhs_src, "shift by negative amount '{}' at index '{d}'", .{ |
| 13942 | rhs_elem.fmtValue(mod, sema), | |
| 14063 | rhs_elem.fmtValue(pt, sema), | |
| 13943 | 14064 | i, |
| 13944 | 14065 | }); |
| 13945 | 14066 | } |
| 13946 | 14067 | } |
| 13947 | } else if (rhs_val.compareHetero(.lt, try mod.intValue(rhs_ty, 0), mod)) { | |
| 14068 | } else if (rhs_val.compareHetero(.lt, try pt.intValue(rhs_ty, 0), pt)) { | |
| 13948 | 14069 | return sema.fail(block, rhs_src, "shift by negative amount '{}'", .{ |
| 13949 | rhs_val.fmtValue(mod, sema), | |
| 14070 | rhs_val.fmtValue(pt, sema), | |
| 13950 | 14071 | }); |
| 13951 | 14072 | } |
| 13952 | 14073 | } |
| 13953 | 14074 | |
| 13954 | 14075 | const runtime_src = if (maybe_lhs_val) |lhs_val| rs: { |
| 13955 | if (lhs_val.isUndef(mod)) return mod.undefRef(lhs_ty); | |
| 14076 | if (lhs_val.isUndef(mod)) return pt.undefRef(lhs_ty); | |
| 13956 | 14077 | const rhs_val = maybe_rhs_val orelse { |
| 13957 | 14078 | if (scalar_ty.zigTypeTag(mod) == .ComptimeInt) { |
| 13958 | 14079 | return sema.fail(block, src, "LHS of shift must be a fixed-width integer type, or RHS must be comptime-known", .{}); |
| ... | ... | @@ -13960,17 +14081,17 @@ fn zirShl( |
| 13960 | 14081 | break :rs rhs_src; |
| 13961 | 14082 | }; |
| 13962 | 14083 | const val = if (scalar_ty.zigTypeTag(mod) == .ComptimeInt) |
| 13963 | try lhs_val.shl(rhs_val, lhs_ty, sema.arena, mod) | |
| 14084 | try lhs_val.shl(rhs_val, lhs_ty, sema.arena, pt) | |
| 13964 | 14085 | else switch (air_tag) { |
| 13965 | 14086 | .shl_exact => val: { |
| 13966 | const shifted = try lhs_val.shlWithOverflow(rhs_val, lhs_ty, sema.arena, mod); | |
| 13967 | if (shifted.overflow_bit.compareAllWithZero(.eq, mod)) { | |
| 14087 | const shifted = try lhs_val.shlWithOverflow(rhs_val, lhs_ty, sema.arena, pt); | |
| 14088 | if (shifted.overflow_bit.compareAllWithZero(.eq, pt)) { | |
| 13968 | 14089 | break :val shifted.wrapped_result; |
| 13969 | 14090 | } |
| 13970 | 14091 | return sema.fail(block, src, "operation caused overflow", .{}); |
| 13971 | 14092 | }, |
| 13972 | .shl_sat => try lhs_val.shlSat(rhs_val, lhs_ty, sema.arena, mod), | |
| 13973 | .shl => try lhs_val.shlTrunc(rhs_val, lhs_ty, sema.arena, mod), | |
| 14093 | .shl_sat => try lhs_val.shlSat(rhs_val, lhs_ty, sema.arena, pt), | |
| 14094 | .shl => try lhs_val.shlTrunc(rhs_val, lhs_ty, sema.arena, pt), | |
| 13974 | 14095 | else => unreachable, |
| 13975 | 14096 | }; |
| 13976 | 14097 | return Air.internedToRef(val.toIntern()); |
| ... | ... | @@ -13981,7 +14102,7 @@ fn zirShl( |
| 13981 | 14102 | if (rhs_is_comptime_int or |
| 13982 | 14103 | scalar_rhs_ty.intInfo(mod).bits > scalar_ty.intInfo(mod).bits) |
| 13983 | 14104 | { |
| 13984 | const max_int = Air.internedToRef((try lhs_ty.maxInt(mod, lhs_ty)).toIntern()); | |
| 14105 | const max_int = Air.internedToRef((try lhs_ty.maxInt(pt, lhs_ty)).toIntern()); | |
| 13985 | 14106 | const rhs_limited = try sema.analyzeMinMax(block, rhs_src, .min, &.{ rhs, max_int }, &.{ rhs_src, rhs_src }); |
| 13986 | 14107 | break :rhs try sema.intCast(block, src, lhs_ty, rhs_src, rhs_limited, rhs_src, false); |
| 13987 | 14108 | } else { |
| ... | ... | @@ -13993,7 +14114,7 @@ fn zirShl( |
| 13993 | 14114 | if (block.wantSafety()) { |
| 13994 | 14115 | const bit_count = scalar_ty.intInfo(mod).bits; |
| 13995 | 14116 | if (!std.math.isPowerOfTwo(bit_count)) { |
| 13996 | const bit_count_val = try mod.intValue(scalar_rhs_ty, bit_count); | |
| 14117 | const bit_count_val = try pt.intValue(scalar_rhs_ty, bit_count); | |
| 13997 | 14118 | const ok = if (rhs_ty.zigTypeTag(mod) == .Vector) ok: { |
| 13998 | 14119 | const bit_count_inst = Air.internedToRef((try sema.splat(rhs_ty, bit_count_val)).toIntern()); |
| 13999 | 14120 | const lt = try block.addCmpVector(rhs, bit_count_inst, .lt); |
| ... | ... | @@ -14034,7 +14155,7 @@ fn zirShl( |
| 14034 | 14155 | }) |
| 14035 | 14156 | else |
| 14036 | 14157 | ov_bit; |
| 14037 | const zero_ov = Air.internedToRef((try mod.intValue(Type.u1, 0)).toIntern()); | |
| 14158 | const zero_ov = Air.internedToRef((try pt.intValue(Type.u1, 0)).toIntern()); | |
| 14038 | 14159 | const no_ov = try block.addBinOp(.cmp_eq, any_ov_bit, zero_ov); |
| 14039 | 14160 | |
| 14040 | 14161 | try sema.addSafetyCheck(block, src, no_ov, .shl_overflow); |
| ... | ... | @@ -14053,7 +14174,8 @@ fn zirShr( |
| 14053 | 14174 | const tracy = trace(@src()); |
| 14054 | 14175 | defer tracy.end(); |
| 14055 | 14176 | |
| 14056 | const mod = sema.mod; | |
| 14177 | const pt = sema.pt; | |
| 14178 | const mod = pt.zcu; | |
| 14057 | 14179 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 14058 | 14180 | const src = block.nodeOffset(inst_data.src_node); |
| 14059 | 14181 | const lhs_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node }); |
| ... | ... | @@ -14071,61 +14193,61 @@ fn zirShr( |
| 14071 | 14193 | |
| 14072 | 14194 | const runtime_src = if (maybe_rhs_val) |rhs_val| rs: { |
| 14073 | 14195 | if (rhs_val.isUndef(mod)) { |
| 14074 | return mod.undefRef(lhs_ty); | |
| 14196 | return pt.undefRef(lhs_ty); | |
| 14075 | 14197 | } |
| 14076 | 14198 | // If rhs is 0, return lhs without doing any calculations. |
| 14077 | if (try rhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 14199 | if (try rhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 14078 | 14200 | return lhs; |
| 14079 | 14201 | } |
| 14080 | 14202 | if (scalar_ty.zigTypeTag(mod) != .ComptimeInt) { |
| 14081 | const bit_value = try mod.intValue(Type.comptime_int, scalar_ty.intInfo(mod).bits); | |
| 14203 | const bit_value = try pt.intValue(Type.comptime_int, scalar_ty.intInfo(mod).bits); | |
| 14082 | 14204 | if (rhs_ty.zigTypeTag(mod) == .Vector) { |
| 14083 | 14205 | var i: usize = 0; |
| 14084 | 14206 | while (i < rhs_ty.vectorLen(mod)) : (i += 1) { |
| 14085 | const rhs_elem = try rhs_val.elemValue(mod, i); | |
| 14086 | if (rhs_elem.compareHetero(.gte, bit_value, mod)) { | |
| 14207 | const rhs_elem = try rhs_val.elemValue(pt, i); | |
| 14208 | if (rhs_elem.compareHetero(.gte, bit_value, pt)) { | |
| 14087 | 14209 | return sema.fail(block, rhs_src, "shift amount '{}' at index '{d}' is too large for operand type '{}'", .{ |
| 14088 | rhs_elem.fmtValue(mod, sema), | |
| 14210 | rhs_elem.fmtValue(pt, sema), | |
| 14089 | 14211 | i, |
| 14090 | scalar_ty.fmt(mod), | |
| 14212 | scalar_ty.fmt(pt), | |
| 14091 | 14213 | }); |
| 14092 | 14214 | } |
| 14093 | 14215 | } |
| 14094 | } else if (rhs_val.compareHetero(.gte, bit_value, mod)) { | |
| 14216 | } else if (rhs_val.compareHetero(.gte, bit_value, pt)) { | |
| 14095 | 14217 | return sema.fail(block, rhs_src, "shift amount '{}' is too large for operand type '{}'", .{ |
| 14096 | rhs_val.fmtValue(mod, sema), | |
| 14097 | scalar_ty.fmt(mod), | |
| 14218 | rhs_val.fmtValue(pt, sema), | |
| 14219 | scalar_ty.fmt(pt), | |
| 14098 | 14220 | }); |
| 14099 | 14221 | } |
| 14100 | 14222 | } |
| 14101 | 14223 | if (rhs_ty.zigTypeTag(mod) == .Vector) { |
| 14102 | 14224 | var i: usize = 0; |
| 14103 | 14225 | while (i < rhs_ty.vectorLen(mod)) : (i += 1) { |
| 14104 | const rhs_elem = try rhs_val.elemValue(mod, i); | |
| 14105 | if (rhs_elem.compareHetero(.lt, try mod.intValue(rhs_ty.childType(mod), 0), mod)) { | |
| 14226 | const rhs_elem = try rhs_val.elemValue(pt, i); | |
| 14227 | if (rhs_elem.compareHetero(.lt, try pt.intValue(rhs_ty.childType(mod), 0), pt)) { | |
| 14106 | 14228 | return sema.fail(block, rhs_src, "shift by negative amount '{}' at index '{d}'", .{ |
| 14107 | rhs_elem.fmtValue(mod, sema), | |
| 14229 | rhs_elem.fmtValue(pt, sema), | |
| 14108 | 14230 | i, |
| 14109 | 14231 | }); |
| 14110 | 14232 | } |
| 14111 | 14233 | } |
| 14112 | } else if (rhs_val.compareHetero(.lt, try mod.intValue(rhs_ty, 0), mod)) { | |
| 14234 | } else if (rhs_val.compareHetero(.lt, try pt.intValue(rhs_ty, 0), pt)) { | |
| 14113 | 14235 | return sema.fail(block, rhs_src, "shift by negative amount '{}'", .{ |
| 14114 | rhs_val.fmtValue(mod, sema), | |
| 14236 | rhs_val.fmtValue(pt, sema), | |
| 14115 | 14237 | }); |
| 14116 | 14238 | } |
| 14117 | 14239 | if (maybe_lhs_val) |lhs_val| { |
| 14118 | 14240 | if (lhs_val.isUndef(mod)) { |
| 14119 | return mod.undefRef(lhs_ty); | |
| 14241 | return pt.undefRef(lhs_ty); | |
| 14120 | 14242 | } |
| 14121 | 14243 | if (air_tag == .shr_exact) { |
| 14122 | 14244 | // Detect if any ones would be shifted out. |
| 14123 | const truncated = try lhs_val.intTruncBitsAsValue(lhs_ty, sema.arena, .unsigned, rhs_val, mod); | |
| 14124 | if (!(try truncated.compareAllWithZeroSema(.eq, mod))) { | |
| 14245 | const truncated = try lhs_val.intTruncBitsAsValue(lhs_ty, sema.arena, .unsigned, rhs_val, pt); | |
| 14246 | if (!(try truncated.compareAllWithZeroSema(.eq, pt))) { | |
| 14125 | 14247 | return sema.fail(block, src, "exact shift shifted out 1 bits", .{}); |
| 14126 | 14248 | } |
| 14127 | 14249 | } |
| 14128 | const val = try lhs_val.shr(rhs_val, lhs_ty, sema.arena, mod); | |
| 14250 | const val = try lhs_val.shr(rhs_val, lhs_ty, sema.arena, pt); | |
| 14129 | 14251 | return Air.internedToRef(val.toIntern()); |
| 14130 | 14252 | } else { |
| 14131 | 14253 | break :rs lhs_src; |
| ... | ... | @@ -14141,7 +14263,7 @@ fn zirShr( |
| 14141 | 14263 | if (block.wantSafety()) { |
| 14142 | 14264 | const bit_count = scalar_ty.intInfo(mod).bits; |
| 14143 | 14265 | if (!std.math.isPowerOfTwo(bit_count)) { |
| 14144 | const bit_count_val = try mod.intValue(rhs_ty.scalarType(mod), bit_count); | |
| 14266 | const bit_count_val = try pt.intValue(rhs_ty.scalarType(mod), bit_count); | |
| 14145 | 14267 | |
| 14146 | 14268 | const ok = if (rhs_ty.zigTypeTag(mod) == .Vector) ok: { |
| 14147 | 14269 | const bit_count_inst = Air.internedToRef((try sema.splat(rhs_ty, bit_count_val)).toIntern()); |
| ... | ... | @@ -14188,7 +14310,8 @@ fn zirBitwise( |
| 14188 | 14310 | const tracy = trace(@src()); |
| 14189 | 14311 | defer tracy.end(); |
| 14190 | 14312 | |
| 14191 | const mod = sema.mod; | |
| 14313 | const pt = sema.pt; | |
| 14314 | const mod = pt.zcu; | |
| 14192 | 14315 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 14193 | 14316 | const src = block.src(.{ .node_offset_bin_op = inst_data.src_node }); |
| 14194 | 14317 | const lhs_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node }); |
| ... | ... | @@ -14220,9 +14343,9 @@ fn zirBitwise( |
| 14220 | 14343 | if (try sema.resolveValueIntable(casted_lhs)) |lhs_val| { |
| 14221 | 14344 | if (try sema.resolveValueIntable(casted_rhs)) |rhs_val| { |
| 14222 | 14345 | const result_val = switch (air_tag) { |
| 14223 | .bit_and => try lhs_val.bitwiseAnd(rhs_val, resolved_type, sema.arena, mod), | |
| 14224 | .bit_or => try lhs_val.bitwiseOr(rhs_val, resolved_type, sema.arena, mod), | |
| 14225 | .xor => try lhs_val.bitwiseXor(rhs_val, resolved_type, sema.arena, mod), | |
| 14346 | .bit_and => try lhs_val.bitwiseAnd(rhs_val, resolved_type, sema.arena, pt), | |
| 14347 | .bit_or => try lhs_val.bitwiseOr(rhs_val, resolved_type, sema.arena, pt), | |
| 14348 | .xor => try lhs_val.bitwiseXor(rhs_val, resolved_type, sema.arena, pt), | |
| 14226 | 14349 | else => unreachable, |
| 14227 | 14350 | }; |
| 14228 | 14351 | return Air.internedToRef(result_val.toIntern()); |
| ... | ... | @@ -14242,7 +14365,8 @@ fn zirBitNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 14242 | 14365 | const tracy = trace(@src()); |
| 14243 | 14366 | defer tracy.end(); |
| 14244 | 14367 | |
| 14245 | const mod = sema.mod; | |
| 14368 | const pt = sema.pt; | |
| 14369 | const mod = pt.zcu; | |
| 14246 | 14370 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 14247 | 14371 | const src = block.nodeOffset(inst_data.src_node); |
| 14248 | 14372 | const operand_src = block.src(.{ .node_offset_un_op = inst_data.src_node }); |
| ... | ... | @@ -14253,26 +14377,26 @@ fn zirBitNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 14253 | 14377 | |
| 14254 | 14378 | if (scalar_type.zigTypeTag(mod) != .Int) { |
| 14255 | 14379 | return sema.fail(block, src, "unable to perform binary not operation on type '{}'", .{ |
| 14256 | operand_type.fmt(mod), | |
| 14380 | operand_type.fmt(pt), | |
| 14257 | 14381 | }); |
| 14258 | 14382 | } |
| 14259 | 14383 | |
| 14260 | 14384 | if (try sema.resolveValue(operand)) |val| { |
| 14261 | 14385 | if (val.isUndef(mod)) { |
| 14262 | return mod.undefRef(operand_type); | |
| 14386 | return pt.undefRef(operand_type); | |
| 14263 | 14387 | } else if (operand_type.zigTypeTag(mod) == .Vector) { |
| 14264 | 14388 | const vec_len = try sema.usizeCast(block, operand_src, operand_type.vectorLen(mod)); |
| 14265 | 14389 | const elems = try sema.arena.alloc(InternPool.Index, vec_len); |
| 14266 | 14390 | for (elems, 0..) |*elem, i| { |
| 14267 | const elem_val = try val.elemValue(mod, i); | |
| 14268 | elem.* = (try elem_val.bitwiseNot(scalar_type, sema.arena, mod)).toIntern(); | |
| 14391 | const elem_val = try val.elemValue(pt, i); | |
| 14392 | elem.* = (try elem_val.bitwiseNot(scalar_type, sema.arena, pt)).toIntern(); | |
| 14269 | 14393 | } |
| 14270 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 14394 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 14271 | 14395 | .ty = operand_type.toIntern(), |
| 14272 | 14396 | .storage = .{ .elems = elems }, |
| 14273 | 14397 | } }))); |
| 14274 | 14398 | } else { |
| 14275 | const result_val = try val.bitwiseNot(operand_type, sema.arena, mod); | |
| 14399 | const result_val = try val.bitwiseNot(operand_type, sema.arena, pt); | |
| 14276 | 14400 | return Air.internedToRef(result_val.toIntern()); |
| 14277 | 14401 | } |
| 14278 | 14402 | } |
| ... | ... | @@ -14288,7 +14412,8 @@ fn analyzeTupleCat( |
| 14288 | 14412 | lhs: Air.Inst.Ref, |
| 14289 | 14413 | rhs: Air.Inst.Ref, |
| 14290 | 14414 | ) CompileError!Air.Inst.Ref { |
| 14291 | const mod = sema.mod; | |
| 14415 | const pt = sema.pt; | |
| 14416 | const mod = pt.zcu; | |
| 14292 | 14417 | const lhs_ty = sema.typeOf(lhs); |
| 14293 | 14418 | const rhs_ty = sema.typeOf(rhs); |
| 14294 | 14419 | const src = block.nodeOffset(src_node); |
| ... | ... | @@ -14344,14 +14469,14 @@ fn analyzeTupleCat( |
| 14344 | 14469 | break :rs runtime_src; |
| 14345 | 14470 | }; |
| 14346 | 14471 | |
| 14347 | const tuple_ty = try mod.intern_pool.getAnonStructType(mod.gpa, .{ | |
| 14472 | const tuple_ty = try mod.intern_pool.getAnonStructType(mod.gpa, pt.tid, .{ | |
| 14348 | 14473 | .types = types, |
| 14349 | 14474 | .values = values, |
| 14350 | 14475 | .names = &.{}, |
| 14351 | 14476 | }); |
| 14352 | 14477 | |
| 14353 | 14478 | const runtime_src = opt_runtime_src orelse { |
| 14354 | const tuple_val = try mod.intern(.{ .aggregate = .{ | |
| 14479 | const tuple_val = try pt.intern(.{ .aggregate = .{ | |
| 14355 | 14480 | .ty = tuple_ty, |
| 14356 | 14481 | .storage = .{ .elems = values }, |
| 14357 | 14482 | } }); |
| ... | ... | @@ -14386,7 +14511,8 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14386 | 14511 | const tracy = trace(@src()); |
| 14387 | 14512 | defer tracy.end(); |
| 14388 | 14513 | |
| 14389 | const mod = sema.mod; | |
| 14514 | const pt = sema.pt; | |
| 14515 | const mod = pt.zcu; | |
| 14390 | 14516 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 14391 | 14517 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 14392 | 14518 | const lhs = try sema.resolveInst(extra.lhs); |
| ... | ... | @@ -14406,11 +14532,11 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14406 | 14532 | |
| 14407 | 14533 | const lhs_info = try sema.getArrayCatInfo(block, lhs_src, lhs, rhs_ty) orelse lhs_info: { |
| 14408 | 14534 | if (lhs_is_tuple) break :lhs_info @as(Type.ArrayInfo, undefined); |
| 14409 | return sema.fail(block, lhs_src, "expected indexable; found '{}'", .{lhs_ty.fmt(mod)}); | |
| 14535 | return sema.fail(block, lhs_src, "expected indexable; found '{}'", .{lhs_ty.fmt(pt)}); | |
| 14410 | 14536 | }; |
| 14411 | 14537 | const rhs_info = try sema.getArrayCatInfo(block, rhs_src, rhs, lhs_ty) orelse { |
| 14412 | 14538 | assert(!rhs_is_tuple); |
| 14413 | return sema.fail(block, rhs_src, "expected indexable; found '{}'", .{rhs_ty.fmt(mod)}); | |
| 14539 | return sema.fail(block, rhs_src, "expected indexable; found '{}'", .{rhs_ty.fmt(pt)}); | |
| 14414 | 14540 | }; |
| 14415 | 14541 | |
| 14416 | 14542 | const resolved_elem_ty = t: { |
| ... | ... | @@ -14472,7 +14598,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14472 | 14598 | ), |
| 14473 | 14599 | }; |
| 14474 | 14600 | |
| 14475 | const result_ty = try mod.arrayType(.{ | |
| 14601 | const result_ty = try pt.arrayType(.{ | |
| 14476 | 14602 | .len = result_len, |
| 14477 | 14603 | .sentinel = if (res_sent_val) |v| v.toIntern() else .none, |
| 14478 | 14604 | .child = resolved_elem_ty.toIntern(), |
| ... | ... | @@ -14512,7 +14638,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14512 | 14638 | while (elem_i < lhs_len) : (elem_i += 1) { |
| 14513 | 14639 | const lhs_elem_i = elem_i; |
| 14514 | 14640 | const elem_default_val = if (lhs_is_tuple) lhs_ty.structFieldDefaultValue(lhs_elem_i, mod) else Value.@"unreachable"; |
| 14515 | const elem_val = if (elem_default_val.toIntern() == .unreachable_value) try lhs_sub_val.elemValue(mod, lhs_elem_i) else elem_default_val; | |
| 14641 | const elem_val = if (elem_default_val.toIntern() == .unreachable_value) try lhs_sub_val.elemValue(pt, lhs_elem_i) else elem_default_val; | |
| 14516 | 14642 | const elem_val_inst = Air.internedToRef(elem_val.toIntern()); |
| 14517 | 14643 | const operand_src = block.src(.{ .array_cat_lhs = .{ |
| 14518 | 14644 | .array_cat_offset = inst_data.src_node, |
| ... | ... | @@ -14525,7 +14651,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14525 | 14651 | while (elem_i < result_len) : (elem_i += 1) { |
| 14526 | 14652 | const rhs_elem_i = elem_i - lhs_len; |
| 14527 | 14653 | const elem_default_val = if (rhs_is_tuple) rhs_ty.structFieldDefaultValue(rhs_elem_i, mod) else Value.@"unreachable"; |
| 14528 | const elem_val = if (elem_default_val.toIntern() == .unreachable_value) try rhs_sub_val.elemValue(mod, rhs_elem_i) else elem_default_val; | |
| 14654 | const elem_val = if (elem_default_val.toIntern() == .unreachable_value) try rhs_sub_val.elemValue(pt, rhs_elem_i) else elem_default_val; | |
| 14529 | 14655 | const elem_val_inst = Air.internedToRef(elem_val.toIntern()); |
| 14530 | 14656 | const operand_src = block.src(.{ .array_cat_rhs = .{ |
| 14531 | 14657 | .array_cat_offset = inst_data.src_node, |
| ... | ... | @@ -14535,7 +14661,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14535 | 14661 | const coerced_elem_val = try sema.resolveConstValue(block, operand_src, coerced_elem_val_inst, undefined); |
| 14536 | 14662 | element_vals[elem_i] = coerced_elem_val.toIntern(); |
| 14537 | 14663 | } |
| 14538 | return sema.addConstantMaybeRef(try mod.intern(.{ .aggregate = .{ | |
| 14664 | return sema.addConstantMaybeRef(try pt.intern(.{ .aggregate = .{ | |
| 14539 | 14665 | .ty = result_ty.toIntern(), |
| 14540 | 14666 | .storage = .{ .elems = element_vals }, |
| 14541 | 14667 | } }), ptr_addrspace != null); |
| ... | ... | @@ -14545,19 +14671,19 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14545 | 14671 | try sema.requireRuntimeBlock(block, src, runtime_src); |
| 14546 | 14672 | |
| 14547 | 14673 | if (ptr_addrspace) |ptr_as| { |
| 14548 | const alloc_ty = try mod.ptrTypeSema(.{ | |
| 14674 | const alloc_ty = try pt.ptrTypeSema(.{ | |
| 14549 | 14675 | .child = result_ty.toIntern(), |
| 14550 | 14676 | .flags = .{ .address_space = ptr_as }, |
| 14551 | 14677 | }); |
| 14552 | 14678 | const alloc = try block.addTy(.alloc, alloc_ty); |
| 14553 | const elem_ptr_ty = try mod.ptrTypeSema(.{ | |
| 14679 | const elem_ptr_ty = try pt.ptrTypeSema(.{ | |
| 14554 | 14680 | .child = resolved_elem_ty.toIntern(), |
| 14555 | 14681 | .flags = .{ .address_space = ptr_as }, |
| 14556 | 14682 | }); |
| 14557 | 14683 | |
| 14558 | 14684 | var elem_i: u32 = 0; |
| 14559 | 14685 | while (elem_i < lhs_len) : (elem_i += 1) { |
| 14560 | const elem_index = try mod.intRef(Type.usize, elem_i); | |
| 14686 | const elem_index = try pt.intRef(Type.usize, elem_i); | |
| 14561 | 14687 | const elem_ptr = try block.addPtrElemPtr(alloc, elem_index, elem_ptr_ty); |
| 14562 | 14688 | const operand_src = block.src(.{ .array_cat_lhs = .{ |
| 14563 | 14689 | .array_cat_offset = inst_data.src_node, |
| ... | ... | @@ -14568,8 +14694,8 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14568 | 14694 | } |
| 14569 | 14695 | while (elem_i < result_len) : (elem_i += 1) { |
| 14570 | 14696 | const rhs_elem_i = elem_i - lhs_len; |
| 14571 | const elem_index = try mod.intRef(Type.usize, elem_i); | |
| 14572 | const rhs_index = try mod.intRef(Type.usize, rhs_elem_i); | |
| 14697 | const elem_index = try pt.intRef(Type.usize, elem_i); | |
| 14698 | const rhs_index = try pt.intRef(Type.usize, rhs_elem_i); | |
| 14573 | 14699 | const elem_ptr = try block.addPtrElemPtr(alloc, elem_index, elem_ptr_ty); |
| 14574 | 14700 | const operand_src = block.src(.{ .array_cat_rhs = .{ |
| 14575 | 14701 | .array_cat_offset = inst_data.src_node, |
| ... | ... | @@ -14579,9 +14705,9 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14579 | 14705 | try sema.storePtr2(block, src, elem_ptr, src, init, operand_src, .store); |
| 14580 | 14706 | } |
| 14581 | 14707 | if (res_sent_val) |sent_val| { |
| 14582 | const elem_index = try mod.intRef(Type.usize, result_len); | |
| 14708 | const elem_index = try pt.intRef(Type.usize, result_len); | |
| 14583 | 14709 | const elem_ptr = try block.addPtrElemPtr(alloc, elem_index, elem_ptr_ty); |
| 14584 | const init = Air.internedToRef((try mod.getCoerced(sent_val, lhs_info.elem_type)).toIntern()); | |
| 14710 | const init = Air.internedToRef((try pt.getCoerced(sent_val, lhs_info.elem_type)).toIntern()); | |
| 14585 | 14711 | try sema.storePtr2(block, src, elem_ptr, src, init, lhs_src, .store); |
| 14586 | 14712 | } |
| 14587 | 14713 | |
| ... | ... | @@ -14592,7 +14718,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14592 | 14718 | { |
| 14593 | 14719 | var elem_i: u32 = 0; |
| 14594 | 14720 | while (elem_i < lhs_len) : (elem_i += 1) { |
| 14595 | const index = try mod.intRef(Type.usize, elem_i); | |
| 14721 | const index = try pt.intRef(Type.usize, elem_i); | |
| 14596 | 14722 | const operand_src = block.src(.{ .array_cat_lhs = .{ |
| 14597 | 14723 | .array_cat_offset = inst_data.src_node, |
| 14598 | 14724 | .elem_index = elem_i, |
| ... | ... | @@ -14602,7 +14728,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14602 | 14728 | } |
| 14603 | 14729 | while (elem_i < result_len) : (elem_i += 1) { |
| 14604 | 14730 | const rhs_elem_i = elem_i - lhs_len; |
| 14605 | const index = try mod.intRef(Type.usize, rhs_elem_i); | |
| 14731 | const index = try pt.intRef(Type.usize, rhs_elem_i); | |
| 14606 | 14732 | const operand_src = block.src(.{ .array_cat_rhs = .{ |
| 14607 | 14733 | .array_cat_offset = inst_data.src_node, |
| 14608 | 14734 | .elem_index = @intCast(rhs_elem_i), |
| ... | ... | @@ -14616,7 +14742,8 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14616 | 14742 | } |
| 14617 | 14743 | |
| 14618 | 14744 | fn getArrayCatInfo(sema: *Sema, block: *Block, src: LazySrcLoc, operand: Air.Inst.Ref, peer_ty: Type) !?Type.ArrayInfo { |
| 14619 | const mod = sema.mod; | |
| 14745 | const pt = sema.pt; | |
| 14746 | const mod = pt.zcu; | |
| 14620 | 14747 | const operand_ty = sema.typeOf(operand); |
| 14621 | 14748 | switch (operand_ty.zigTypeTag(mod)) { |
| 14622 | 14749 | .Array => return operand_ty.arrayInfo(mod), |
| ... | ... | @@ -14633,7 +14760,7 @@ fn getArrayCatInfo(sema: *Sema, block: *Block, src: LazySrcLoc, operand: Air.Ins |
| 14633 | 14760 | .none => null, |
| 14634 | 14761 | else => Value.fromInterned(ptr_info.sentinel), |
| 14635 | 14762 | }, |
| 14636 | .len = try val.sliceLen(mod), | |
| 14763 | .len = try val.sliceLen(pt), | |
| 14637 | 14764 | }; |
| 14638 | 14765 | }, |
| 14639 | 14766 | .One => { |
| ... | ... | @@ -14666,7 +14793,8 @@ fn analyzeTupleMul( |
| 14666 | 14793 | operand: Air.Inst.Ref, |
| 14667 | 14794 | factor: usize, |
| 14668 | 14795 | ) CompileError!Air.Inst.Ref { |
| 14669 | const mod = sema.mod; | |
| 14796 | const pt = sema.pt; | |
| 14797 | const mod = pt.zcu; | |
| 14670 | 14798 | const operand_ty = sema.typeOf(operand); |
| 14671 | 14799 | const src = block.nodeOffset(src_node); |
| 14672 | 14800 | const len_src = block.src(.{ .node_offset_bin_rhs = src_node }); |
| ... | ... | @@ -14702,14 +14830,14 @@ fn analyzeTupleMul( |
| 14702 | 14830 | break :rs runtime_src; |
| 14703 | 14831 | }; |
| 14704 | 14832 | |
| 14705 | const tuple_ty = try mod.intern_pool.getAnonStructType(mod.gpa, .{ | |
| 14833 | const tuple_ty = try mod.intern_pool.getAnonStructType(mod.gpa, pt.tid, .{ | |
| 14706 | 14834 | .types = types, |
| 14707 | 14835 | .values = values, |
| 14708 | 14836 | .names = &.{}, |
| 14709 | 14837 | }); |
| 14710 | 14838 | |
| 14711 | 14839 | const runtime_src = opt_runtime_src orelse { |
| 14712 | const tuple_val = try mod.intern(.{ .aggregate = .{ | |
| 14840 | const tuple_val = try pt.intern(.{ .aggregate = .{ | |
| 14713 | 14841 | .ty = tuple_ty, |
| 14714 | 14842 | .storage = .{ .elems = values }, |
| 14715 | 14843 | } }); |
| ... | ... | @@ -14739,7 +14867,8 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14739 | 14867 | const tracy = trace(@src()); |
| 14740 | 14868 | defer tracy.end(); |
| 14741 | 14869 | |
| 14742 | const mod = sema.mod; | |
| 14870 | const pt = sema.pt; | |
| 14871 | const mod = pt.zcu; | |
| 14743 | 14872 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 14744 | 14873 | const extra = sema.code.extraData(Zir.Inst.ArrayMul, inst_data.payload_index).data; |
| 14745 | 14874 | const uncoerced_lhs = try sema.resolveInst(extra.lhs); |
| ... | ... | @@ -14762,12 +14891,12 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14762 | 14891 | const lhs_len = uncoerced_lhs_ty.structFieldCount(mod); |
| 14763 | 14892 | const lhs_dest_ty = switch (res_ty.zigTypeTag(mod)) { |
| 14764 | 14893 | else => break :no_coerce, |
| 14765 | .Array => try mod.arrayType(.{ | |
| 14894 | .Array => try pt.arrayType(.{ | |
| 14766 | 14895 | .child = res_ty.childType(mod).toIntern(), |
| 14767 | 14896 | .len = lhs_len, |
| 14768 | 14897 | .sentinel = if (res_ty.sentinel(mod)) |s| s.toIntern() else .none, |
| 14769 | 14898 | }), |
| 14770 | .Vector => try mod.vectorType(.{ | |
| 14899 | .Vector => try pt.vectorType(.{ | |
| 14771 | 14900 | .child = res_ty.childType(mod).toIntern(), |
| 14772 | 14901 | .len = lhs_len, |
| 14773 | 14902 | }), |
| ... | ... | @@ -14796,7 +14925,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14796 | 14925 | // Analyze the lhs first, to catch the case that someone tried to do exponentiation |
| 14797 | 14926 | const lhs_info = try sema.getArrayCatInfo(block, lhs_src, lhs, lhs_ty) orelse { |
| 14798 | 14927 | const msg = msg: { |
| 14799 | const msg = try sema.errMsg(lhs_src, "expected indexable; found '{}'", .{lhs_ty.fmt(mod)}); | |
| 14928 | const msg = try sema.errMsg(lhs_src, "expected indexable; found '{}'", .{lhs_ty.fmt(pt)}); | |
| 14800 | 14929 | errdefer msg.destroy(sema.gpa); |
| 14801 | 14930 | switch (lhs_ty.zigTypeTag(mod)) { |
| 14802 | 14931 | .Int, .Float, .ComptimeFloat, .ComptimeInt, .Vector => { |
| ... | ... | @@ -14818,7 +14947,7 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14818 | 14947 | return sema.fail(block, rhs_src, "operation results in overflow", .{}); |
| 14819 | 14948 | const result_len = try sema.usizeCast(block, src, result_len_u64); |
| 14820 | 14949 | |
| 14821 | const result_ty = try mod.arrayType(.{ | |
| 14950 | const result_ty = try pt.arrayType(.{ | |
| 14822 | 14951 | .len = result_len, |
| 14823 | 14952 | .sentinel = if (lhs_info.sentinel) |s| s.toIntern() else .none, |
| 14824 | 14953 | .child = lhs_info.elem_type.toIntern(), |
| ... | ... | @@ -14839,8 +14968,8 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14839 | 14968 | // Optimization for the common pattern of a single element repeated N times, such |
| 14840 | 14969 | // as zero-filling a byte array. |
| 14841 | 14970 | if (lhs_len == 1 and lhs_info.sentinel == null) { |
| 14842 | const elem_val = try lhs_sub_val.elemValue(mod, 0); | |
| 14843 | break :v try mod.intern(.{ .aggregate = .{ | |
| 14971 | const elem_val = try lhs_sub_val.elemValue(pt, 0); | |
| 14972 | break :v try pt.intern(.{ .aggregate = .{ | |
| 14844 | 14973 | .ty = result_ty.toIntern(), |
| 14845 | 14974 | .storage = .{ .repeated_elem = elem_val.toIntern() }, |
| 14846 | 14975 | } }); |
| ... | ... | @@ -14851,12 +14980,12 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14851 | 14980 | while (elem_i < result_len) { |
| 14852 | 14981 | var lhs_i: usize = 0; |
| 14853 | 14982 | while (lhs_i < lhs_len) : (lhs_i += 1) { |
| 14854 | const elem_val = try lhs_sub_val.elemValue(mod, lhs_i); | |
| 14983 | const elem_val = try lhs_sub_val.elemValue(pt, lhs_i); | |
| 14855 | 14984 | element_vals[elem_i] = elem_val.toIntern(); |
| 14856 | 14985 | elem_i += 1; |
| 14857 | 14986 | } |
| 14858 | 14987 | } |
| 14859 | break :v try mod.intern(.{ .aggregate = .{ | |
| 14988 | break :v try pt.intern(.{ .aggregate = .{ | |
| 14860 | 14989 | .ty = result_ty.toIntern(), |
| 14861 | 14990 | .storage = .{ .elems = element_vals }, |
| 14862 | 14991 | } }); |
| ... | ... | @@ -14870,17 +14999,17 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14870 | 14999 | // to get the same elem values. |
| 14871 | 15000 | const lhs_vals = try sema.arena.alloc(Air.Inst.Ref, lhs_len); |
| 14872 | 15001 | for (lhs_vals, 0..) |*lhs_val, idx| { |
| 14873 | const idx_ref = try mod.intRef(Type.usize, idx); | |
| 15002 | const idx_ref = try pt.intRef(Type.usize, idx); | |
| 14874 | 15003 | lhs_val.* = try sema.elemVal(block, lhs_src, lhs, idx_ref, src, false); |
| 14875 | 15004 | } |
| 14876 | 15005 | |
| 14877 | 15006 | if (ptr_addrspace) |ptr_as| { |
| 14878 | const alloc_ty = try mod.ptrTypeSema(.{ | |
| 15007 | const alloc_ty = try pt.ptrTypeSema(.{ | |
| 14879 | 15008 | .child = result_ty.toIntern(), |
| 14880 | 15009 | .flags = .{ .address_space = ptr_as }, |
| 14881 | 15010 | }); |
| 14882 | 15011 | const alloc = try block.addTy(.alloc, alloc_ty); |
| 14883 | const elem_ptr_ty = try mod.ptrTypeSema(.{ | |
| 15012 | const elem_ptr_ty = try pt.ptrTypeSema(.{ | |
| 14884 | 15013 | .child = lhs_info.elem_type.toIntern(), |
| 14885 | 15014 | .flags = .{ .address_space = ptr_as }, |
| 14886 | 15015 | }); |
| ... | ... | @@ -14888,14 +15017,14 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14888 | 15017 | var elem_i: usize = 0; |
| 14889 | 15018 | while (elem_i < result_len) { |
| 14890 | 15019 | for (lhs_vals) |lhs_val| { |
| 14891 | const elem_index = try mod.intRef(Type.usize, elem_i); | |
| 15020 | const elem_index = try pt.intRef(Type.usize, elem_i); | |
| 14892 | 15021 | const elem_ptr = try block.addPtrElemPtr(alloc, elem_index, elem_ptr_ty); |
| 14893 | 15022 | try sema.storePtr2(block, src, elem_ptr, src, lhs_val, lhs_src, .store); |
| 14894 | 15023 | elem_i += 1; |
| 14895 | 15024 | } |
| 14896 | 15025 | } |
| 14897 | 15026 | if (lhs_info.sentinel) |sent_val| { |
| 14898 | const elem_index = try mod.intRef(Type.usize, result_len); | |
| 15027 | const elem_index = try pt.intRef(Type.usize, result_len); | |
| 14899 | 15028 | const elem_ptr = try block.addPtrElemPtr(alloc, elem_index, elem_ptr_ty); |
| 14900 | 15029 | const init = Air.internedToRef(sent_val.toIntern()); |
| 14901 | 15030 | try sema.storePtr2(block, src, elem_ptr, src, init, lhs_src, .store); |
| ... | ... | @@ -14912,7 +15041,8 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 14912 | 15041 | } |
| 14913 | 15042 | |
| 14914 | 15043 | fn zirNegate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 14915 | const mod = sema.mod; | |
| 15044 | const pt = sema.pt; | |
| 15045 | const mod = pt.zcu; | |
| 14916 | 15046 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 14917 | 15047 | const src = block.nodeOffset(inst_data.src_node); |
| 14918 | 15048 | const lhs_src = src; |
| ... | ... | @@ -14926,25 +15056,26 @@ fn zirNegate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 14926 | 15056 | .Int, .ComptimeInt, .Float, .ComptimeFloat => false, |
| 14927 | 15057 | else => true, |
| 14928 | 15058 | }) { |
| 14929 | return sema.fail(block, src, "negation of type '{}'", .{rhs_ty.fmt(mod)}); | |
| 15059 | return sema.fail(block, src, "negation of type '{}'", .{rhs_ty.fmt(pt)}); | |
| 14930 | 15060 | } |
| 14931 | 15061 | |
| 14932 | 15062 | if (rhs_scalar_ty.isAnyFloat()) { |
| 14933 | 15063 | // We handle float negation here to ensure negative zero is represented in the bits. |
| 14934 | 15064 | if (try sema.resolveValue(rhs)) |rhs_val| { |
| 14935 | if (rhs_val.isUndef(mod)) return mod.undefRef(rhs_ty); | |
| 14936 | return Air.internedToRef((try rhs_val.floatNeg(rhs_ty, sema.arena, mod)).toIntern()); | |
| 15065 | if (rhs_val.isUndef(mod)) return pt.undefRef(rhs_ty); | |
| 15066 | return Air.internedToRef((try rhs_val.floatNeg(rhs_ty, sema.arena, pt)).toIntern()); | |
| 14937 | 15067 | } |
| 14938 | 15068 | try sema.requireRuntimeBlock(block, src, null); |
| 14939 | 15069 | return block.addUnOp(if (block.float_mode == .optimized) .neg_optimized else .neg, rhs); |
| 14940 | 15070 | } |
| 14941 | 15071 | |
| 14942 | const lhs = Air.internedToRef((try sema.splat(rhs_ty, try mod.intValue(rhs_scalar_ty, 0))).toIntern()); | |
| 15072 | const lhs = Air.internedToRef((try sema.splat(rhs_ty, try pt.intValue(rhs_scalar_ty, 0))).toIntern()); | |
| 14943 | 15073 | return sema.analyzeArithmetic(block, .sub, lhs, rhs, src, lhs_src, rhs_src, true); |
| 14944 | 15074 | } |
| 14945 | 15075 | |
| 14946 | 15076 | fn zirNegateWrap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 14947 | const mod = sema.mod; | |
| 15077 | const pt = sema.pt; | |
| 15078 | const mod = pt.zcu; | |
| 14948 | 15079 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 14949 | 15080 | const src = block.nodeOffset(inst_data.src_node); |
| 14950 | 15081 | const lhs_src = src; |
| ... | ... | @@ -14956,10 +15087,10 @@ fn zirNegateWrap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 14956 | 15087 | |
| 14957 | 15088 | switch (rhs_scalar_ty.zigTypeTag(mod)) { |
| 14958 | 15089 | .Int, .ComptimeInt, .Float, .ComptimeFloat => {}, |
| 14959 | else => return sema.fail(block, src, "negation of type '{}'", .{rhs_ty.fmt(mod)}), | |
| 15090 | else => return sema.fail(block, src, "negation of type '{}'", .{rhs_ty.fmt(pt)}), | |
| 14960 | 15091 | } |
| 14961 | 15092 | |
| 14962 | const lhs = Air.internedToRef((try sema.splat(rhs_ty, try mod.intValue(rhs_scalar_ty, 0))).toIntern()); | |
| 15093 | const lhs = Air.internedToRef((try sema.splat(rhs_ty, try pt.intValue(rhs_scalar_ty, 0))).toIntern()); | |
| 14963 | 15094 | return sema.analyzeArithmetic(block, .subwrap, lhs, rhs, src, lhs_src, rhs_src, true); |
| 14964 | 15095 | } |
| 14965 | 15096 | |
| ... | ... | @@ -14985,7 +15116,8 @@ fn zirArithmetic( |
| 14985 | 15116 | } |
| 14986 | 15117 | |
| 14987 | 15118 | fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 14988 | const mod = sema.mod; | |
| 15119 | const pt = sema.pt; | |
| 15120 | const mod = pt.zcu; | |
| 14989 | 15121 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 14990 | 15122 | const src = block.src(.{ .node_offset_bin_op = inst_data.src_node }); |
| 14991 | 15123 | const lhs_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node }); |
| ... | ... | @@ -15026,13 +15158,13 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins |
| 15026 | 15158 | // If lhs % rhs is 0, it doesn't matter. |
| 15027 | 15159 | const lhs_val = maybe_lhs_val orelse unreachable; |
| 15028 | 15160 | const rhs_val = maybe_rhs_val orelse unreachable; |
| 15029 | const rem = lhs_val.floatRem(rhs_val, resolved_type, sema.arena, mod) catch unreachable; | |
| 15030 | if (!rem.compareAllWithZero(.eq, mod)) { | |
| 15161 | const rem = lhs_val.floatRem(rhs_val, resolved_type, sema.arena, pt) catch unreachable; | |
| 15162 | if (!rem.compareAllWithZero(.eq, pt)) { | |
| 15031 | 15163 | return sema.fail( |
| 15032 | 15164 | block, |
| 15033 | 15165 | src, |
| 15034 | 15166 | "ambiguous coercion of division operands '{}' and '{}'; non-zero remainder '{}'", |
| 15035 | .{ lhs_ty.fmt(mod), rhs_ty.fmt(mod), rem.fmtValue(mod, sema) }, | |
| 15167 | .{ lhs_ty.fmt(pt), rhs_ty.fmt(pt), rem.fmtValue(pt, sema) }, | |
| 15036 | 15168 | ); |
| 15037 | 15169 | } |
| 15038 | 15170 | } |
| ... | ... | @@ -15068,10 +15200,10 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins |
| 15068 | 15200 | .Int, .ComptimeInt, .ComptimeFloat => { |
| 15069 | 15201 | if (maybe_lhs_val) |lhs_val| { |
| 15070 | 15202 | if (!lhs_val.isUndef(mod)) { |
| 15071 | if (try lhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 15203 | if (try lhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 15072 | 15204 | const scalar_zero = switch (scalar_tag) { |
| 15073 | .ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0.0), | |
| 15074 | .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0), | |
| 15205 | .ComptimeFloat, .Float => try pt.floatValue(resolved_type.scalarType(mod), 0.0), | |
| 15206 | .ComptimeInt, .Int => try pt.intValue(resolved_type.scalarType(mod), 0), | |
| 15075 | 15207 | else => unreachable, |
| 15076 | 15208 | }; |
| 15077 | 15209 | const zero_val = try sema.splat(resolved_type, scalar_zero); |
| ... | ... | @@ -15083,7 +15215,7 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins |
| 15083 | 15215 | if (rhs_val.isUndef(mod)) { |
| 15084 | 15216 | return sema.failWithUseOfUndef(block, rhs_src); |
| 15085 | 15217 | } |
| 15086 | if (!(try rhs_val.compareAllWithZeroSema(.neq, mod))) { | |
| 15218 | if (!(try rhs_val.compareAllWithZeroSema(.neq, pt))) { | |
| 15087 | 15219 | return sema.failWithDivideByZero(block, rhs_src); |
| 15088 | 15220 | } |
| 15089 | 15221 | // TODO: if the RHS is one, return the LHS directly |
| ... | ... | @@ -15097,25 +15229,25 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins |
| 15097 | 15229 | if (lhs_val.isUndef(mod)) { |
| 15098 | 15230 | if (lhs_scalar_ty.isSignedInt(mod) and rhs_scalar_ty.isSignedInt(mod)) { |
| 15099 | 15231 | if (maybe_rhs_val) |rhs_val| { |
| 15100 | if (try sema.compareAll(rhs_val, .neq, try mod.intValue(resolved_type, -1), resolved_type)) { | |
| 15101 | return mod.undefRef(resolved_type); | |
| 15232 | if (try sema.compareAll(rhs_val, .neq, try pt.intValue(resolved_type, -1), resolved_type)) { | |
| 15233 | return pt.undefRef(resolved_type); | |
| 15102 | 15234 | } |
| 15103 | 15235 | } |
| 15104 | 15236 | return sema.failWithUseOfUndef(block, rhs_src); |
| 15105 | 15237 | } |
| 15106 | return mod.undefRef(resolved_type); | |
| 15238 | return pt.undefRef(resolved_type); | |
| 15107 | 15239 | } |
| 15108 | 15240 | |
| 15109 | 15241 | if (maybe_rhs_val) |rhs_val| { |
| 15110 | 15242 | if (is_int) { |
| 15111 | 15243 | var overflow_idx: ?usize = null; |
| 15112 | const res = try lhs_val.intDiv(rhs_val, resolved_type, &overflow_idx, sema.arena, mod); | |
| 15244 | const res = try lhs_val.intDiv(rhs_val, resolved_type, &overflow_idx, sema.arena, pt); | |
| 15113 | 15245 | if (overflow_idx) |vec_idx| { |
| 15114 | 15246 | return sema.failWithIntegerOverflow(block, src, resolved_type, res, vec_idx); |
| 15115 | 15247 | } |
| 15116 | 15248 | return Air.internedToRef(res.toIntern()); |
| 15117 | 15249 | } else { |
| 15118 | return Air.internedToRef((try lhs_val.floatDiv(rhs_val, resolved_type, sema.arena, mod)).toIntern()); | |
| 15250 | return Air.internedToRef((try lhs_val.floatDiv(rhs_val, resolved_type, sema.arena, pt)).toIntern()); | |
| 15119 | 15251 | } |
| 15120 | 15252 | } else { |
| 15121 | 15253 | break :rs rhs_src; |
| ... | ... | @@ -15138,7 +15270,7 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins |
| 15138 | 15270 | block, |
| 15139 | 15271 | src, |
| 15140 | 15272 | "division with '{}' and '{}': signed integers must use @divTrunc, @divFloor, or @divExact", |
| 15141 | .{ lhs_ty.fmt(mod), rhs_ty.fmt(mod) }, | |
| 15273 | .{ lhs_ty.fmt(pt), rhs_ty.fmt(pt) }, | |
| 15142 | 15274 | ); |
| 15143 | 15275 | } |
| 15144 | 15276 | break :blk Air.Inst.Tag.div_trunc; |
| ... | ... | @@ -15150,7 +15282,8 @@ fn zirDiv(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins |
| 15150 | 15282 | } |
| 15151 | 15283 | |
| 15152 | 15284 | fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 15153 | const mod = sema.mod; | |
| 15285 | const pt = sema.pt; | |
| 15286 | const mod = pt.zcu; | |
| 15154 | 15287 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 15155 | 15288 | const src = block.src(.{ .node_offset_bin_op = inst_data.src_node }); |
| 15156 | 15289 | const lhs_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node }); |
| ... | ... | @@ -15204,10 +15337,10 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 15204 | 15337 | if (lhs_val.isUndef(mod)) { |
| 15205 | 15338 | return sema.failWithUseOfUndef(block, rhs_src); |
| 15206 | 15339 | } else { |
| 15207 | if (try lhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 15340 | if (try lhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 15208 | 15341 | const scalar_zero = switch (scalar_tag) { |
| 15209 | .ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0.0), | |
| 15210 | .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0), | |
| 15342 | .ComptimeFloat, .Float => try pt.floatValue(resolved_type.scalarType(mod), 0.0), | |
| 15343 | .ComptimeInt, .Int => try pt.intValue(resolved_type.scalarType(mod), 0), | |
| 15211 | 15344 | else => unreachable, |
| 15212 | 15345 | }; |
| 15213 | 15346 | const zero_val = try sema.splat(resolved_type, scalar_zero); |
| ... | ... | @@ -15219,7 +15352,7 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 15219 | 15352 | if (rhs_val.isUndef(mod)) { |
| 15220 | 15353 | return sema.failWithUseOfUndef(block, rhs_src); |
| 15221 | 15354 | } |
| 15222 | if (!(try rhs_val.compareAllWithZeroSema(.neq, mod))) { | |
| 15355 | if (!(try rhs_val.compareAllWithZeroSema(.neq, pt))) { | |
| 15223 | 15356 | return sema.failWithDivideByZero(block, rhs_src); |
| 15224 | 15357 | } |
| 15225 | 15358 | // TODO: if the RHS is one, return the LHS directly |
| ... | ... | @@ -15227,22 +15360,22 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 15227 | 15360 | if (maybe_lhs_val) |lhs_val| { |
| 15228 | 15361 | if (maybe_rhs_val) |rhs_val| { |
| 15229 | 15362 | if (is_int) { |
| 15230 | const modulus_val = try lhs_val.intMod(rhs_val, resolved_type, sema.arena, mod); | |
| 15231 | if (!(modulus_val.compareAllWithZero(.eq, mod))) { | |
| 15363 | const modulus_val = try lhs_val.intMod(rhs_val, resolved_type, sema.arena, pt); | |
| 15364 | if (!(modulus_val.compareAllWithZero(.eq, pt))) { | |
| 15232 | 15365 | return sema.fail(block, src, "exact division produced remainder", .{}); |
| 15233 | 15366 | } |
| 15234 | 15367 | var overflow_idx: ?usize = null; |
| 15235 | const res = try lhs_val.intDiv(rhs_val, resolved_type, &overflow_idx, sema.arena, mod); | |
| 15368 | const res = try lhs_val.intDiv(rhs_val, resolved_type, &overflow_idx, sema.arena, pt); | |
| 15236 | 15369 | if (overflow_idx) |vec_idx| { |
| 15237 | 15370 | return sema.failWithIntegerOverflow(block, src, resolved_type, res, vec_idx); |
| 15238 | 15371 | } |
| 15239 | 15372 | return Air.internedToRef(res.toIntern()); |
| 15240 | 15373 | } else { |
| 15241 | const modulus_val = try lhs_val.floatMod(rhs_val, resolved_type, sema.arena, mod); | |
| 15242 | if (!(modulus_val.compareAllWithZero(.eq, mod))) { | |
| 15374 | const modulus_val = try lhs_val.floatMod(rhs_val, resolved_type, sema.arena, pt); | |
| 15375 | if (!(modulus_val.compareAllWithZero(.eq, pt))) { | |
| 15243 | 15376 | return sema.fail(block, src, "exact division produced remainder", .{}); |
| 15244 | 15377 | } |
| 15245 | return Air.internedToRef((try lhs_val.floatDiv(rhs_val, resolved_type, sema.arena, mod)).toIntern()); | |
| 15378 | return Air.internedToRef((try lhs_val.floatDiv(rhs_val, resolved_type, sema.arena, pt)).toIntern()); | |
| 15246 | 15379 | } |
| 15247 | 15380 | } else break :rs rhs_src; |
| 15248 | 15381 | } else break :rs lhs_src; |
| ... | ... | @@ -15286,8 +15419,8 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 15286 | 15419 | const remainder = try block.addBinOp(.rem, casted_lhs, casted_rhs); |
| 15287 | 15420 | |
| 15288 | 15421 | const scalar_zero = switch (scalar_tag) { |
| 15289 | .ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0.0), | |
| 15290 | .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0), | |
| 15422 | .ComptimeFloat, .Float => try pt.floatValue(resolved_type.scalarType(mod), 0.0), | |
| 15423 | .ComptimeInt, .Int => try pt.intValue(resolved_type.scalarType(mod), 0), | |
| 15291 | 15424 | else => unreachable, |
| 15292 | 15425 | }; |
| 15293 | 15426 | if (resolved_type.zigTypeTag(mod) == .Vector) { |
| ... | ... | @@ -15315,7 +15448,8 @@ fn zirDivExact(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 15315 | 15448 | } |
| 15316 | 15449 | |
| 15317 | 15450 | fn zirDivFloor(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 15318 | const mod = sema.mod; | |
| 15451 | const pt = sema.pt; | |
| 15452 | const mod = pt.zcu; | |
| 15319 | 15453 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 15320 | 15454 | const src = block.src(.{ .node_offset_bin_op = inst_data.src_node }); |
| 15321 | 15455 | const lhs_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node }); |
| ... | ... | @@ -15371,10 +15505,10 @@ fn zirDivFloor(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 15371 | 15505 | // If the lhs is undefined, result is undefined. |
| 15372 | 15506 | if (maybe_lhs_val) |lhs_val| { |
| 15373 | 15507 | if (!lhs_val.isUndef(mod)) { |
| 15374 | if (try lhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 15508 | if (try lhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 15375 | 15509 | const scalar_zero = switch (scalar_tag) { |
| 15376 | .ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0.0), | |
| 15377 | .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0), | |
| 15510 | .ComptimeFloat, .Float => try pt.floatValue(resolved_type.scalarType(mod), 0.0), | |
| 15511 | .ComptimeInt, .Int => try pt.intValue(resolved_type.scalarType(mod), 0), | |
| 15378 | 15512 | else => unreachable, |
| 15379 | 15513 | }; |
| 15380 | 15514 | const zero_val = try sema.splat(resolved_type, scalar_zero); |
| ... | ... | @@ -15386,7 +15520,7 @@ fn zirDivFloor(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 15386 | 15520 | if (rhs_val.isUndef(mod)) { |
| 15387 | 15521 | return sema.failWithUseOfUndef(block, rhs_src); |
| 15388 | 15522 | } |
| 15389 | if (!(try rhs_val.compareAllWithZeroSema(.neq, mod))) { | |
| 15523 | if (!(try rhs_val.compareAllWithZeroSema(.neq, pt))) { | |
| 15390 | 15524 | return sema.failWithDivideByZero(block, rhs_src); |
| 15391 | 15525 | } |
| 15392 | 15526 | // TODO: if the RHS is one, return the LHS directly |
| ... | ... | @@ -15395,20 +15529,20 @@ fn zirDivFloor(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 15395 | 15529 | if (lhs_val.isUndef(mod)) { |
| 15396 | 15530 | if (lhs_scalar_ty.isSignedInt(mod) and rhs_scalar_ty.isSignedInt(mod)) { |
| 15397 | 15531 | if (maybe_rhs_val) |rhs_val| { |
| 15398 | if (try sema.compareAll(rhs_val, .neq, try mod.intValue(resolved_type, -1), resolved_type)) { | |
| 15399 | return mod.undefRef(resolved_type); | |
| 15532 | if (try sema.compareAll(rhs_val, .neq, try pt.intValue(resolved_type, -1), resolved_type)) { | |
| 15533 | return pt.undefRef(resolved_type); | |
| 15400 | 15534 | } |
| 15401 | 15535 | } |
| 15402 | 15536 | return sema.failWithUseOfUndef(block, rhs_src); |
| 15403 | 15537 | } |
| 15404 | return mod.undefRef(resolved_type); | |
| 15538 | return pt.undefRef(resolved_type); | |
| 15405 | 15539 | } |
| 15406 | 15540 | |
| 15407 | 15541 | if (maybe_rhs_val) |rhs_val| { |
| 15408 | 15542 | if (is_int) { |
| 15409 | return Air.internedToRef((try lhs_val.intDivFloor(rhs_val, resolved_type, sema.arena, mod)).toIntern()); | |
| 15543 | return Air.internedToRef((try lhs_val.intDivFloor(rhs_val, resolved_type, sema.arena, pt)).toIntern()); | |
| 15410 | 15544 | } else { |
| 15411 | return Air.internedToRef((try lhs_val.floatDivFloor(rhs_val, resolved_type, sema.arena, mod)).toIntern()); | |
| 15545 | return Air.internedToRef((try lhs_val.floatDivFloor(rhs_val, resolved_type, sema.arena, pt)).toIntern()); | |
| 15412 | 15546 | } |
| 15413 | 15547 | } else break :rs rhs_src; |
| 15414 | 15548 | } else break :rs lhs_src; |
| ... | ... | @@ -15425,7 +15559,8 @@ fn zirDivFloor(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 15425 | 15559 | } |
| 15426 | 15560 | |
| 15427 | 15561 | fn zirDivTrunc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 15428 | const mod = sema.mod; | |
| 15562 | const pt = sema.pt; | |
| 15563 | const mod = pt.zcu; | |
| 15429 | 15564 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 15430 | 15565 | const src = block.src(.{ .node_offset_bin_op = inst_data.src_node }); |
| 15431 | 15566 | const lhs_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node }); |
| ... | ... | @@ -15481,10 +15616,10 @@ fn zirDivTrunc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 15481 | 15616 | // If the lhs is undefined, result is undefined. |
| 15482 | 15617 | if (maybe_lhs_val) |lhs_val| { |
| 15483 | 15618 | if (!lhs_val.isUndef(mod)) { |
| 15484 | if (try lhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 15619 | if (try lhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 15485 | 15620 | const scalar_zero = switch (scalar_tag) { |
| 15486 | .ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0.0), | |
| 15487 | .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0), | |
| 15621 | .ComptimeFloat, .Float => try pt.floatValue(resolved_type.scalarType(mod), 0.0), | |
| 15622 | .ComptimeInt, .Int => try pt.intValue(resolved_type.scalarType(mod), 0), | |
| 15488 | 15623 | else => unreachable, |
| 15489 | 15624 | }; |
| 15490 | 15625 | const zero_val = try sema.splat(resolved_type, scalar_zero); |
| ... | ... | @@ -15496,7 +15631,7 @@ fn zirDivTrunc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 15496 | 15631 | if (rhs_val.isUndef(mod)) { |
| 15497 | 15632 | return sema.failWithUseOfUndef(block, rhs_src); |
| 15498 | 15633 | } |
| 15499 | if (!(try rhs_val.compareAllWithZeroSema(.neq, mod))) { | |
| 15634 | if (!(try rhs_val.compareAllWithZeroSema(.neq, pt))) { | |
| 15500 | 15635 | return sema.failWithDivideByZero(block, rhs_src); |
| 15501 | 15636 | } |
| 15502 | 15637 | } |
| ... | ... | @@ -15504,25 +15639,25 @@ fn zirDivTrunc(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 15504 | 15639 | if (lhs_val.isUndef(mod)) { |
| 15505 | 15640 | if (lhs_scalar_ty.isSignedInt(mod) and rhs_scalar_ty.isSignedInt(mod)) { |
| 15506 | 15641 | if (maybe_rhs_val) |rhs_val| { |
| 15507 | if (try sema.compareAll(rhs_val, .neq, try mod.intValue(resolved_type, -1), resolved_type)) { | |
| 15508 | return mod.undefRef(resolved_type); | |
| 15642 | if (try sema.compareAll(rhs_val, .neq, try pt.intValue(resolved_type, -1), resolved_type)) { | |
| 15643 | return pt.undefRef(resolved_type); | |
| 15509 | 15644 | } |
| 15510 | 15645 | } |
| 15511 | 15646 | return sema.failWithUseOfUndef(block, rhs_src); |
| 15512 | 15647 | } |
| 15513 | return mod.undefRef(resolved_type); | |
| 15648 | return pt.undefRef(resolved_type); | |
| 15514 | 15649 | } |
| 15515 | 15650 | |
| 15516 | 15651 | if (maybe_rhs_val) |rhs_val| { |
| 15517 | 15652 | if (is_int) { |
| 15518 | 15653 | var overflow_idx: ?usize = null; |
| 15519 | const res = try lhs_val.intDiv(rhs_val, resolved_type, &overflow_idx, sema.arena, mod); | |
| 15654 | const res = try lhs_val.intDiv(rhs_val, resolved_type, &overflow_idx, sema.arena, pt); | |
| 15520 | 15655 | if (overflow_idx) |vec_idx| { |
| 15521 | 15656 | return sema.failWithIntegerOverflow(block, src, resolved_type, res, vec_idx); |
| 15522 | 15657 | } |
| 15523 | 15658 | return Air.internedToRef(res.toIntern()); |
| 15524 | 15659 | } else { |
| 15525 | return Air.internedToRef((try lhs_val.floatDivTrunc(rhs_val, resolved_type, sema.arena, mod)).toIntern()); | |
| 15660 | return Air.internedToRef((try lhs_val.floatDivTrunc(rhs_val, resolved_type, sema.arena, pt)).toIntern()); | |
| 15526 | 15661 | } |
| 15527 | 15662 | } else break :rs rhs_src; |
| 15528 | 15663 | } else break :rs lhs_src; |
| ... | ... | @@ -15550,7 +15685,8 @@ fn addDivIntOverflowSafety( |
| 15550 | 15685 | casted_rhs: Air.Inst.Ref, |
| 15551 | 15686 | is_int: bool, |
| 15552 | 15687 | ) CompileError!void { |
| 15553 | const mod = sema.mod; | |
| 15688 | const pt = sema.pt; | |
| 15689 | const mod = pt.zcu; | |
| 15554 | 15690 | if (!is_int) return; |
| 15555 | 15691 | |
| 15556 | 15692 | // If the LHS is unsigned, it cannot cause overflow. |
| ... | ... | @@ -15561,19 +15697,19 @@ fn addDivIntOverflowSafety( |
| 15561 | 15697 | return; |
| 15562 | 15698 | } |
| 15563 | 15699 | |
| 15564 | const min_int = try resolved_type.minInt(mod, resolved_type); | |
| 15565 | const neg_one_scalar = try mod.intValue(lhs_scalar_ty, -1); | |
| 15700 | const min_int = try resolved_type.minInt(pt, resolved_type); | |
| 15701 | const neg_one_scalar = try pt.intValue(lhs_scalar_ty, -1); | |
| 15566 | 15702 | const neg_one = try sema.splat(resolved_type, neg_one_scalar); |
| 15567 | 15703 | |
| 15568 | 15704 | // If the LHS is comptime-known to be not equal to the min int, |
| 15569 | 15705 | // no overflow is possible. |
| 15570 | 15706 | if (maybe_lhs_val) |lhs_val| { |
| 15571 | if (try lhs_val.compareAll(.neq, min_int, resolved_type, mod)) return; | |
| 15707 | if (try lhs_val.compareAll(.neq, min_int, resolved_type, pt)) return; | |
| 15572 | 15708 | } |
| 15573 | 15709 | |
| 15574 | 15710 | // If the RHS is comptime-known to not be equal to -1, no overflow is possible. |
| 15575 | 15711 | if (maybe_rhs_val) |rhs_val| { |
| 15576 | if (try rhs_val.compareAll(.neq, neg_one, resolved_type, mod)) return; | |
| 15712 | if (try rhs_val.compareAll(.neq, neg_one, resolved_type, pt)) return; | |
| 15577 | 15713 | } |
| 15578 | 15714 | |
| 15579 | 15715 | var ok: Air.Inst.Ref = .none; |
| ... | ... | @@ -15634,11 +15770,12 @@ fn addDivByZeroSafety( |
| 15634 | 15770 | // emitted above. |
| 15635 | 15771 | if (maybe_rhs_val != null) return; |
| 15636 | 15772 | |
| 15637 | const mod = sema.mod; | |
| 15773 | const pt = sema.pt; | |
| 15774 | const mod = pt.zcu; | |
| 15638 | 15775 | const scalar_zero = if (is_int) |
| 15639 | try mod.intValue(resolved_type.scalarType(mod), 0) | |
| 15776 | try pt.intValue(resolved_type.scalarType(mod), 0) | |
| 15640 | 15777 | else |
| 15641 | try mod.floatValue(resolved_type.scalarType(mod), 0.0); | |
| 15778 | try pt.floatValue(resolved_type.scalarType(mod), 0.0); | |
| 15642 | 15779 | const ok = if (resolved_type.zigTypeTag(mod) == .Vector) ok: { |
| 15643 | 15780 | const zero_val = try sema.splat(resolved_type, scalar_zero); |
| 15644 | 15781 | const zero = Air.internedToRef(zero_val.toIntern()); |
| ... | ... | @@ -15666,7 +15803,8 @@ fn airTag(block: *Block, is_int: bool, normal: Air.Inst.Tag, optimized: Air.Inst |
| 15666 | 15803 | } |
| 15667 | 15804 | |
| 15668 | 15805 | fn zirModRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 15669 | const mod = sema.mod; | |
| 15806 | const pt = sema.pt; | |
| 15807 | const mod = pt.zcu; | |
| 15670 | 15808 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 15671 | 15809 | const src = block.src(.{ .node_offset_bin_op = inst_data.src_node }); |
| 15672 | 15810 | const lhs_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node }); |
| ... | ... | @@ -15721,16 +15859,16 @@ fn zirModRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 15721 | 15859 | if (lhs_val.isUndef(mod)) { |
| 15722 | 15860 | return sema.failWithUseOfUndef(block, lhs_src); |
| 15723 | 15861 | } |
| 15724 | if (try lhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 15862 | if (try lhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 15725 | 15863 | const scalar_zero = switch (scalar_tag) { |
| 15726 | .ComptimeFloat, .Float => try mod.floatValue(resolved_type.scalarType(mod), 0.0), | |
| 15727 | .ComptimeInt, .Int => try mod.intValue(resolved_type.scalarType(mod), 0), | |
| 15864 | .ComptimeFloat, .Float => try pt.floatValue(resolved_type.scalarType(mod), 0.0), | |
| 15865 | .ComptimeInt, .Int => try pt.intValue(resolved_type.scalarType(mod), 0), | |
| 15728 | 15866 | else => unreachable, |
| 15729 | 15867 | }; |
| 15730 | const zero_val = if (is_vector) Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 15868 | const zero_val = if (is_vector) Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 15731 | 15869 | .ty = resolved_type.toIntern(), |
| 15732 | 15870 | .storage = .{ .repeated_elem = scalar_zero.toIntern() }, |
| 15733 | } }))) else scalar_zero; | |
| 15871 | } })) else scalar_zero; | |
| 15734 | 15872 | return Air.internedToRef(zero_val.toIntern()); |
| 15735 | 15873 | } |
| 15736 | 15874 | } else if (lhs_scalar_ty.isSignedInt(mod)) { |
| ... | ... | @@ -15740,18 +15878,18 @@ fn zirModRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 15740 | 15878 | if (rhs_val.isUndef(mod)) { |
| 15741 | 15879 | return sema.failWithUseOfUndef(block, rhs_src); |
| 15742 | 15880 | } |
| 15743 | if (!(try rhs_val.compareAllWithZeroSema(.neq, mod))) { | |
| 15881 | if (!(try rhs_val.compareAllWithZeroSema(.neq, pt))) { | |
| 15744 | 15882 | return sema.failWithDivideByZero(block, rhs_src); |
| 15745 | 15883 | } |
| 15746 | if (!(try rhs_val.compareAllWithZeroSema(.gte, mod))) { | |
| 15884 | if (!(try rhs_val.compareAllWithZeroSema(.gte, pt))) { | |
| 15747 | 15885 | return sema.failWithModRemNegative(block, rhs_src, lhs_ty, rhs_ty); |
| 15748 | 15886 | } |
| 15749 | 15887 | if (maybe_lhs_val) |lhs_val| { |
| 15750 | 15888 | const rem_result = try sema.intRem(resolved_type, lhs_val, rhs_val); |
| 15751 | 15889 | // If this answer could possibly be different by doing `intMod`, |
| 15752 | 15890 | // we must emit a compile error. Otherwise, it's OK. |
| 15753 | if (!(try lhs_val.compareAllWithZeroSema(.gte, mod)) and | |
| 15754 | !(try rem_result.compareAllWithZeroSema(.eq, mod))) | |
| 15891 | if (!(try lhs_val.compareAllWithZeroSema(.gte, pt)) and | |
| 15892 | !(try rem_result.compareAllWithZeroSema(.eq, pt))) | |
| 15755 | 15893 | { |
| 15756 | 15894 | return sema.failWithModRemNegative(block, lhs_src, lhs_ty, rhs_ty); |
| 15757 | 15895 | } |
| ... | ... | @@ -15769,17 +15907,17 @@ fn zirModRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 15769 | 15907 | if (rhs_val.isUndef(mod)) { |
| 15770 | 15908 | return sema.failWithUseOfUndef(block, rhs_src); |
| 15771 | 15909 | } |
| 15772 | if (!(try rhs_val.compareAllWithZeroSema(.neq, mod))) { | |
| 15910 | if (!(try rhs_val.compareAllWithZeroSema(.neq, pt))) { | |
| 15773 | 15911 | return sema.failWithDivideByZero(block, rhs_src); |
| 15774 | 15912 | } |
| 15775 | if (!(try rhs_val.compareAllWithZeroSema(.gte, mod))) { | |
| 15913 | if (!(try rhs_val.compareAllWithZeroSema(.gte, pt))) { | |
| 15776 | 15914 | return sema.failWithModRemNegative(block, rhs_src, lhs_ty, rhs_ty); |
| 15777 | 15915 | } |
| 15778 | 15916 | if (maybe_lhs_val) |lhs_val| { |
| 15779 | if (lhs_val.isUndef(mod) or !(try lhs_val.compareAllWithZeroSema(.gte, mod))) { | |
| 15917 | if (lhs_val.isUndef(mod) or !(try lhs_val.compareAllWithZeroSema(.gte, pt))) { | |
| 15780 | 15918 | return sema.failWithModRemNegative(block, lhs_src, lhs_ty, rhs_ty); |
| 15781 | 15919 | } |
| 15782 | return Air.internedToRef((try lhs_val.floatRem(rhs_val, resolved_type, sema.arena, mod)).toIntern()); | |
| 15920 | return Air.internedToRef((try lhs_val.floatRem(rhs_val, resolved_type, sema.arena, pt)).toIntern()); | |
| 15783 | 15921 | } else { |
| 15784 | 15922 | return sema.failWithModRemNegative(block, lhs_src, lhs_ty, rhs_ty); |
| 15785 | 15923 | } |
| ... | ... | @@ -15804,31 +15942,32 @@ fn intRem( |
| 15804 | 15942 | lhs: Value, |
| 15805 | 15943 | rhs: Value, |
| 15806 | 15944 | ) CompileError!Value { |
| 15807 | const mod = sema.mod; | |
| 15945 | const pt = sema.pt; | |
| 15946 | const mod = pt.zcu; | |
| 15808 | 15947 | if (ty.zigTypeTag(mod) == .Vector) { |
| 15809 | 15948 | const result_data = try sema.arena.alloc(InternPool.Index, ty.vectorLen(mod)); |
| 15810 | 15949 | const scalar_ty = ty.scalarType(mod); |
| 15811 | 15950 | for (result_data, 0..) |*scalar, i| { |
| 15812 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 15813 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 15951 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 15952 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 15814 | 15953 | scalar.* = (try sema.intRemScalar(lhs_elem, rhs_elem, scalar_ty)).toIntern(); |
| 15815 | 15954 | } |
| 15816 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 15955 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 15817 | 15956 | .ty = ty.toIntern(), |
| 15818 | 15957 | .storage = .{ .elems = result_data }, |
| 15819 | } }))); | |
| 15958 | } })); | |
| 15820 | 15959 | } |
| 15821 | 15960 | return sema.intRemScalar(lhs, rhs, ty); |
| 15822 | 15961 | } |
| 15823 | 15962 | |
| 15824 | 15963 | fn intRemScalar(sema: *Sema, lhs: Value, rhs: Value, scalar_ty: Type) CompileError!Value { |
| 15825 | const mod = sema.mod; | |
| 15964 | const pt = sema.pt; | |
| 15826 | 15965 | // TODO is this a performance issue? maybe we should try the operation without |
| 15827 | 15966 | // resorting to BigInt first. |
| 15828 | 15967 | var lhs_space: Value.BigIntSpace = undefined; |
| 15829 | 15968 | var rhs_space: Value.BigIntSpace = undefined; |
| 15830 | const lhs_bigint = try lhs.toBigIntAdvanced(&lhs_space, mod, .sema); | |
| 15831 | const rhs_bigint = try rhs.toBigIntAdvanced(&rhs_space, mod, .sema); | |
| 15969 | const lhs_bigint = try lhs.toBigIntAdvanced(&lhs_space, pt, .sema); | |
| 15970 | const rhs_bigint = try rhs.toBigIntAdvanced(&rhs_space, pt, .sema); | |
| 15832 | 15971 | const limbs_q = try sema.arena.alloc( |
| 15833 | 15972 | math.big.Limb, |
| 15834 | 15973 | lhs_bigint.limbs.len, |
| ... | ... | @@ -15846,11 +15985,12 @@ fn intRemScalar(sema: *Sema, lhs: Value, rhs: Value, scalar_ty: Type) CompileErr |
| 15846 | 15985 | var result_q = math.big.int.Mutable{ .limbs = limbs_q, .positive = undefined, .len = undefined }; |
| 15847 | 15986 | var result_r = math.big.int.Mutable{ .limbs = limbs_r, .positive = undefined, .len = undefined }; |
| 15848 | 15987 | result_q.divTrunc(&result_r, lhs_bigint, rhs_bigint, limbs_buffer); |
| 15849 | return mod.intValue_big(scalar_ty, result_r.toConst()); | |
| 15988 | return pt.intValue_big(scalar_ty, result_r.toConst()); | |
| 15850 | 15989 | } |
| 15851 | 15990 | |
| 15852 | 15991 | fn zirMod(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 15853 | const mod = sema.mod; | |
| 15992 | const pt = sema.pt; | |
| 15993 | const mod = pt.zcu; | |
| 15854 | 15994 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 15855 | 15995 | const src = block.src(.{ .node_offset_bin_op = inst_data.src_node }); |
| 15856 | 15996 | const lhs_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node }); |
| ... | ... | @@ -15904,11 +16044,11 @@ fn zirMod(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins |
| 15904 | 16044 | if (rhs_val.isUndef(mod)) { |
| 15905 | 16045 | return sema.failWithUseOfUndef(block, rhs_src); |
| 15906 | 16046 | } |
| 15907 | if (!(try rhs_val.compareAllWithZeroSema(.neq, mod))) { | |
| 16047 | if (!(try rhs_val.compareAllWithZeroSema(.neq, pt))) { | |
| 15908 | 16048 | return sema.failWithDivideByZero(block, rhs_src); |
| 15909 | 16049 | } |
| 15910 | 16050 | if (maybe_lhs_val) |lhs_val| { |
| 15911 | return Air.internedToRef((try lhs_val.intMod(rhs_val, resolved_type, sema.arena, mod)).toIntern()); | |
| 16051 | return Air.internedToRef((try lhs_val.intMod(rhs_val, resolved_type, sema.arena, pt)).toIntern()); | |
| 15912 | 16052 | } |
| 15913 | 16053 | break :rs lhs_src; |
| 15914 | 16054 | } else { |
| ... | ... | @@ -15920,16 +16060,16 @@ fn zirMod(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins |
| 15920 | 16060 | if (rhs_val.isUndef(mod)) { |
| 15921 | 16061 | return sema.failWithUseOfUndef(block, rhs_src); |
| 15922 | 16062 | } |
| 15923 | if (!(try rhs_val.compareAllWithZeroSema(.neq, mod))) { | |
| 16063 | if (!(try rhs_val.compareAllWithZeroSema(.neq, pt))) { | |
| 15924 | 16064 | return sema.failWithDivideByZero(block, rhs_src); |
| 15925 | 16065 | } |
| 15926 | 16066 | } |
| 15927 | 16067 | if (maybe_lhs_val) |lhs_val| { |
| 15928 | 16068 | if (lhs_val.isUndef(mod)) { |
| 15929 | return mod.undefRef(resolved_type); | |
| 16069 | return pt.undefRef(resolved_type); | |
| 15930 | 16070 | } |
| 15931 | 16071 | if (maybe_rhs_val) |rhs_val| { |
| 15932 | return Air.internedToRef((try lhs_val.floatMod(rhs_val, resolved_type, sema.arena, mod)).toIntern()); | |
| 16072 | return Air.internedToRef((try lhs_val.floatMod(rhs_val, resolved_type, sema.arena, pt)).toIntern()); | |
| 15933 | 16073 | } else break :rs rhs_src; |
| 15934 | 16074 | } else break :rs lhs_src; |
| 15935 | 16075 | }; |
| ... | ... | @@ -15945,7 +16085,8 @@ fn zirMod(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins |
| 15945 | 16085 | } |
| 15946 | 16086 | |
| 15947 | 16087 | fn zirRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 15948 | const mod = sema.mod; | |
| 16088 | const pt = sema.pt; | |
| 16089 | const mod = pt.zcu; | |
| 15949 | 16090 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 15950 | 16091 | const src = block.src(.{ .node_offset_bin_op = inst_data.src_node }); |
| 15951 | 16092 | const lhs_src = block.src(.{ .node_offset_bin_lhs = inst_data.src_node }); |
| ... | ... | @@ -15999,7 +16140,7 @@ fn zirRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins |
| 15999 | 16140 | if (rhs_val.isUndef(mod)) { |
| 16000 | 16141 | return sema.failWithUseOfUndef(block, rhs_src); |
| 16001 | 16142 | } |
| 16002 | if (!(try rhs_val.compareAllWithZeroSema(.neq, mod))) { | |
| 16143 | if (!(try rhs_val.compareAllWithZeroSema(.neq, pt))) { | |
| 16003 | 16144 | return sema.failWithDivideByZero(block, rhs_src); |
| 16004 | 16145 | } |
| 16005 | 16146 | if (maybe_lhs_val) |lhs_val| { |
| ... | ... | @@ -16015,16 +16156,16 @@ fn zirRem(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Ins |
| 16015 | 16156 | if (rhs_val.isUndef(mod)) { |
| 16016 | 16157 | return sema.failWithUseOfUndef(block, rhs_src); |
| 16017 | 16158 | } |
| 16018 | if (!(try rhs_val.compareAllWithZeroSema(.neq, mod))) { | |
| 16159 | if (!(try rhs_val.compareAllWithZeroSema(.neq, pt))) { | |
| 16019 | 16160 | return sema.failWithDivideByZero(block, rhs_src); |
| 16020 | 16161 | } |
| 16021 | 16162 | } |
| 16022 | 16163 | if (maybe_lhs_val) |lhs_val| { |
| 16023 | 16164 | if (lhs_val.isUndef(mod)) { |
| 16024 | return mod.undefRef(resolved_type); | |
| 16165 | return pt.undefRef(resolved_type); | |
| 16025 | 16166 | } |
| 16026 | 16167 | if (maybe_rhs_val) |rhs_val| { |
| 16027 | return Air.internedToRef((try lhs_val.floatRem(rhs_val, resolved_type, sema.arena, mod)).toIntern()); | |
| 16168 | return Air.internedToRef((try lhs_val.floatRem(rhs_val, resolved_type, sema.arena, pt)).toIntern()); | |
| 16028 | 16169 | } else break :rs rhs_src; |
| 16029 | 16170 | } else break :rs lhs_src; |
| 16030 | 16171 | }; |
| ... | ... | @@ -16059,7 +16200,8 @@ fn zirOverflowArithmetic( |
| 16059 | 16200 | |
| 16060 | 16201 | const lhs_ty = sema.typeOf(uncasted_lhs); |
| 16061 | 16202 | const rhs_ty = sema.typeOf(uncasted_rhs); |
| 16062 | const mod = sema.mod; | |
| 16203 | const pt = sema.pt; | |
| 16204 | const mod = pt.zcu; | |
| 16063 | 16205 | const ip = &mod.intern_pool; |
| 16064 | 16206 | |
| 16065 | 16207 | try sema.checkVectorizableBinaryOperands(block, src, lhs_ty, rhs_ty, lhs_src, rhs_src); |
| ... | ... | @@ -16081,7 +16223,7 @@ fn zirOverflowArithmetic( |
| 16081 | 16223 | const rhs = try sema.coerce(block, rhs_dest_ty, uncasted_rhs, rhs_src); |
| 16082 | 16224 | |
| 16083 | 16225 | if (dest_ty.scalarType(mod).zigTypeTag(mod) != .Int) { |
| 16084 | return sema.fail(block, src, "expected vector of integers or integer tag type, found '{}'", .{dest_ty.fmt(mod)}); | |
| 16226 | return sema.fail(block, src, "expected vector of integers or integer tag type, found '{}'", .{dest_ty.fmt(pt)}); | |
| 16085 | 16227 | } |
| 16086 | 16228 | |
| 16087 | 16229 | const maybe_lhs_val = try sema.resolveValue(lhs); |
| ... | ... | @@ -16095,19 +16237,19 @@ fn zirOverflowArithmetic( |
| 16095 | 16237 | wrapped: Value = Value.@"unreachable", |
| 16096 | 16238 | overflow_bit: Value, |
| 16097 | 16239 | } = result: { |
| 16098 | const zero_bit = try mod.intValue(Type.u1, 0); | |
| 16240 | const zero_bit = try pt.intValue(Type.u1, 0); | |
| 16099 | 16241 | switch (zir_tag) { |
| 16100 | 16242 | .add_with_overflow => { |
| 16101 | 16243 | // If either of the arguments is zero, `false` is returned and the other is stored |
| 16102 | 16244 | // to the result, even if it is undefined.. |
| 16103 | 16245 | // Otherwise, if either of the argument is undefined, undefined is returned. |
| 16104 | 16246 | if (maybe_lhs_val) |lhs_val| { |
| 16105 | if (!lhs_val.isUndef(mod) and (try lhs_val.compareAllWithZeroSema(.eq, mod))) { | |
| 16247 | if (!lhs_val.isUndef(mod) and (try lhs_val.compareAllWithZeroSema(.eq, pt))) { | |
| 16106 | 16248 | break :result .{ .overflow_bit = try sema.splat(overflow_ty, zero_bit), .inst = rhs }; |
| 16107 | 16249 | } |
| 16108 | 16250 | } |
| 16109 | 16251 | if (maybe_rhs_val) |rhs_val| { |
| 16110 | if (!rhs_val.isUndef(mod) and (try rhs_val.compareAllWithZeroSema(.eq, mod))) { | |
| 16252 | if (!rhs_val.isUndef(mod) and (try rhs_val.compareAllWithZeroSema(.eq, pt))) { | |
| 16111 | 16253 | break :result .{ .overflow_bit = try sema.splat(overflow_ty, zero_bit), .inst = lhs }; |
| 16112 | 16254 | } |
| 16113 | 16255 | } |
| ... | ... | @@ -16128,7 +16270,7 @@ fn zirOverflowArithmetic( |
| 16128 | 16270 | if (maybe_rhs_val) |rhs_val| { |
| 16129 | 16271 | if (rhs_val.isUndef(mod)) { |
| 16130 | 16272 | break :result .{ .overflow_bit = Value.undef, .wrapped = Value.undef }; |
| 16131 | } else if (try rhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 16273 | } else if (try rhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 16132 | 16274 | break :result .{ .overflow_bit = try sema.splat(overflow_ty, zero_bit), .inst = lhs }; |
| 16133 | 16275 | } else if (maybe_lhs_val) |lhs_val| { |
| 16134 | 16276 | if (lhs_val.isUndef(mod)) { |
| ... | ... | @@ -16144,10 +16286,10 @@ fn zirOverflowArithmetic( |
| 16144 | 16286 | // If either of the arguments is zero, the result is zero and no overflow occured. |
| 16145 | 16287 | // If either of the arguments is one, the result is the other and no overflow occured. |
| 16146 | 16288 | // Otherwise, if either of the arguments is undefined, both results are undefined. |
| 16147 | const scalar_one = try mod.intValue(dest_ty.scalarType(mod), 1); | |
| 16289 | const scalar_one = try pt.intValue(dest_ty.scalarType(mod), 1); | |
| 16148 | 16290 | if (maybe_lhs_val) |lhs_val| { |
| 16149 | 16291 | if (!lhs_val.isUndef(mod)) { |
| 16150 | if (try lhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 16292 | if (try lhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 16151 | 16293 | break :result .{ .overflow_bit = try sema.splat(overflow_ty, zero_bit), .inst = lhs }; |
| 16152 | 16294 | } else if (try sema.compareAll(lhs_val, .eq, try sema.splat(dest_ty, scalar_one), dest_ty)) { |
| 16153 | 16295 | break :result .{ .overflow_bit = try sema.splat(overflow_ty, zero_bit), .inst = rhs }; |
| ... | ... | @@ -16157,7 +16299,7 @@ fn zirOverflowArithmetic( |
| 16157 | 16299 | |
| 16158 | 16300 | if (maybe_rhs_val) |rhs_val| { |
| 16159 | 16301 | if (!rhs_val.isUndef(mod)) { |
| 16160 | if (try rhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 16302 | if (try rhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 16161 | 16303 | break :result .{ .overflow_bit = try sema.splat(overflow_ty, zero_bit), .inst = rhs }; |
| 16162 | 16304 | } else if (try sema.compareAll(rhs_val, .eq, try sema.splat(dest_ty, scalar_one), dest_ty)) { |
| 16163 | 16305 | break :result .{ .overflow_bit = try sema.splat(overflow_ty, zero_bit), .inst = lhs }; |
| ... | ... | @@ -16171,7 +16313,7 @@ fn zirOverflowArithmetic( |
| 16171 | 16313 | break :result .{ .overflow_bit = Value.undef, .wrapped = Value.undef }; |
| 16172 | 16314 | } |
| 16173 | 16315 | |
| 16174 | const result = try lhs_val.intMulWithOverflow(rhs_val, dest_ty, sema.arena, mod); | |
| 16316 | const result = try lhs_val.intMulWithOverflow(rhs_val, dest_ty, sema.arena, pt); | |
| 16175 | 16317 | break :result .{ .overflow_bit = result.overflow_bit, .wrapped = result.wrapped_result }; |
| 16176 | 16318 | } |
| 16177 | 16319 | } |
| ... | ... | @@ -16181,12 +16323,12 @@ fn zirOverflowArithmetic( |
| 16181 | 16323 | // If rhs is zero, the result is lhs (even if undefined) and no overflow occurred. |
| 16182 | 16324 | // Oterhwise if either of the arguments is undefined, both results are undefined. |
| 16183 | 16325 | if (maybe_lhs_val) |lhs_val| { |
| 16184 | if (!lhs_val.isUndef(mod) and (try lhs_val.compareAllWithZeroSema(.eq, mod))) { | |
| 16326 | if (!lhs_val.isUndef(mod) and (try lhs_val.compareAllWithZeroSema(.eq, pt))) { | |
| 16185 | 16327 | break :result .{ .overflow_bit = try sema.splat(overflow_ty, zero_bit), .inst = lhs }; |
| 16186 | 16328 | } |
| 16187 | 16329 | } |
| 16188 | 16330 | if (maybe_rhs_val) |rhs_val| { |
| 16189 | if (!rhs_val.isUndef(mod) and (try rhs_val.compareAllWithZeroSema(.eq, mod))) { | |
| 16331 | if (!rhs_val.isUndef(mod) and (try rhs_val.compareAllWithZeroSema(.eq, pt))) { | |
| 16190 | 16332 | break :result .{ .overflow_bit = try sema.splat(overflow_ty, zero_bit), .inst = lhs }; |
| 16191 | 16333 | } |
| 16192 | 16334 | } |
| ... | ... | @@ -16196,7 +16338,7 @@ fn zirOverflowArithmetic( |
| 16196 | 16338 | break :result .{ .overflow_bit = Value.undef, .wrapped = Value.undef }; |
| 16197 | 16339 | } |
| 16198 | 16340 | |
| 16199 | const result = try lhs_val.shlWithOverflow(rhs_val, dest_ty, sema.arena, mod); | |
| 16341 | const result = try lhs_val.shlWithOverflow(rhs_val, dest_ty, sema.arena, pt); | |
| 16200 | 16342 | break :result .{ .overflow_bit = result.overflow_bit, .wrapped = result.wrapped_result }; |
| 16201 | 16343 | } |
| 16202 | 16344 | } |
| ... | ... | @@ -16235,7 +16377,7 @@ fn zirOverflowArithmetic( |
| 16235 | 16377 | } |
| 16236 | 16378 | |
| 16237 | 16379 | if (result.inst == .none) { |
| 16238 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 16380 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 16239 | 16381 | .ty = tuple_ty.toIntern(), |
| 16240 | 16382 | .storage = .{ .elems = &.{ |
| 16241 | 16383 | result.wrapped.toIntern(), |
| ... | ... | @@ -16251,9 +16393,10 @@ fn zirOverflowArithmetic( |
| 16251 | 16393 | } |
| 16252 | 16394 | |
| 16253 | 16395 | fn splat(sema: *Sema, ty: Type, val: Value) !Value { |
| 16254 | const mod = sema.mod; | |
| 16396 | const pt = sema.pt; | |
| 16397 | const mod = pt.zcu; | |
| 16255 | 16398 | if (ty.zigTypeTag(mod) != .Vector) return val; |
| 16256 | const repeated = try mod.intern(.{ .aggregate = .{ | |
| 16399 | const repeated = try pt.intern(.{ .aggregate = .{ | |
| 16257 | 16400 | .ty = ty.toIntern(), |
| 16258 | 16401 | .storage = .{ .repeated_elem = val.toIntern() }, |
| 16259 | 16402 | } }); |
| ... | ... | @@ -16261,16 +16404,17 @@ fn splat(sema: *Sema, ty: Type, val: Value) !Value { |
| 16261 | 16404 | } |
| 16262 | 16405 | |
| 16263 | 16406 | fn overflowArithmeticTupleType(sema: *Sema, ty: Type) !Type { |
| 16264 | const mod = sema.mod; | |
| 16407 | const pt = sema.pt; | |
| 16408 | const mod = pt.zcu; | |
| 16265 | 16409 | const ip = &mod.intern_pool; |
| 16266 | const ov_ty = if (ty.zigTypeTag(mod) == .Vector) try mod.vectorType(.{ | |
| 16410 | const ov_ty = if (ty.zigTypeTag(mod) == .Vector) try pt.vectorType(.{ | |
| 16267 | 16411 | .len = ty.vectorLen(mod), |
| 16268 | 16412 | .child = .u1_type, |
| 16269 | 16413 | }) else Type.u1; |
| 16270 | 16414 | |
| 16271 | 16415 | const types = [2]InternPool.Index{ ty.toIntern(), ov_ty.toIntern() }; |
| 16272 | 16416 | const values = [2]InternPool.Index{ .none, .none }; |
| 16273 | const tuple_ty = try ip.getAnonStructType(mod.gpa, .{ | |
| 16417 | const tuple_ty = try ip.getAnonStructType(mod.gpa, pt.tid, .{ | |
| 16274 | 16418 | .types = &types, |
| 16275 | 16419 | .values = &values, |
| 16276 | 16420 | .names = &.{}, |
| ... | ... | @@ -16290,7 +16434,8 @@ fn analyzeArithmetic( |
| 16290 | 16434 | rhs_src: LazySrcLoc, |
| 16291 | 16435 | want_safety: bool, |
| 16292 | 16436 | ) CompileError!Air.Inst.Ref { |
| 16293 | const mod = sema.mod; | |
| 16437 | const pt = sema.pt; | |
| 16438 | const mod = pt.zcu; | |
| 16294 | 16439 | const lhs_ty = sema.typeOf(lhs); |
| 16295 | 16440 | const rhs_ty = sema.typeOf(rhs); |
| 16296 | 16441 | const lhs_zig_ty_tag = try lhs_ty.zigTypeTagOrPoison(mod); |
| ... | ... | @@ -16337,7 +16482,7 @@ fn analyzeArithmetic( |
| 16337 | 16482 | // overflow (max_int), causing illegal behavior. |
| 16338 | 16483 | // For floats: either operand being undef makes the result undef. |
| 16339 | 16484 | if (maybe_lhs_val) |lhs_val| { |
| 16340 | if (!lhs_val.isUndef(mod) and (try lhs_val.compareAllWithZeroSema(.eq, mod))) { | |
| 16485 | if (!lhs_val.isUndef(mod) and (try lhs_val.compareAllWithZeroSema(.eq, pt))) { | |
| 16341 | 16486 | return casted_rhs; |
| 16342 | 16487 | } |
| 16343 | 16488 | } |
| ... | ... | @@ -16346,10 +16491,10 @@ fn analyzeArithmetic( |
| 16346 | 16491 | if (is_int) { |
| 16347 | 16492 | return sema.failWithUseOfUndef(block, rhs_src); |
| 16348 | 16493 | } else { |
| 16349 | return mod.undefRef(resolved_type); | |
| 16494 | return pt.undefRef(resolved_type); | |
| 16350 | 16495 | } |
| 16351 | 16496 | } |
| 16352 | if (try rhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 16497 | if (try rhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 16353 | 16498 | return casted_lhs; |
| 16354 | 16499 | } |
| 16355 | 16500 | } |
| ... | ... | @@ -16359,7 +16504,7 @@ fn analyzeArithmetic( |
| 16359 | 16504 | if (is_int) { |
| 16360 | 16505 | return sema.failWithUseOfUndef(block, lhs_src); |
| 16361 | 16506 | } else { |
| 16362 | return mod.undefRef(resolved_type); | |
| 16507 | return pt.undefRef(resolved_type); | |
| 16363 | 16508 | } |
| 16364 | 16509 | } |
| 16365 | 16510 | if (maybe_rhs_val) |rhs_val| { |
| ... | ... | @@ -16371,7 +16516,7 @@ fn analyzeArithmetic( |
| 16371 | 16516 | } |
| 16372 | 16517 | return Air.internedToRef(sum.toIntern()); |
| 16373 | 16518 | } else { |
| 16374 | return Air.internedToRef((try Value.floatAdd(lhs_val, rhs_val, resolved_type, sema.arena, mod)).toIntern()); | |
| 16519 | return Air.internedToRef((try Value.floatAdd(lhs_val, rhs_val, resolved_type, sema.arena, pt)).toIntern()); | |
| 16375 | 16520 | } |
| 16376 | 16521 | } else break :rs .{ rhs_src, air_tag, .add_safe }; |
| 16377 | 16522 | } else break :rs .{ lhs_src, air_tag, .add_safe }; |
| ... | ... | @@ -16381,15 +16526,15 @@ fn analyzeArithmetic( |
| 16381 | 16526 | // If either of the operands are zero, the other operand is returned. |
| 16382 | 16527 | // If either of the operands are undefined, the result is undefined. |
| 16383 | 16528 | if (maybe_lhs_val) |lhs_val| { |
| 16384 | if (!lhs_val.isUndef(mod) and (try lhs_val.compareAllWithZeroSema(.eq, mod))) { | |
| 16529 | if (!lhs_val.isUndef(mod) and (try lhs_val.compareAllWithZeroSema(.eq, pt))) { | |
| 16385 | 16530 | return casted_rhs; |
| 16386 | 16531 | } |
| 16387 | 16532 | } |
| 16388 | 16533 | if (maybe_rhs_val) |rhs_val| { |
| 16389 | 16534 | if (rhs_val.isUndef(mod)) { |
| 16390 | return mod.undefRef(resolved_type); | |
| 16535 | return pt.undefRef(resolved_type); | |
| 16391 | 16536 | } |
| 16392 | if (try rhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 16537 | if (try rhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 16393 | 16538 | return casted_lhs; |
| 16394 | 16539 | } |
| 16395 | 16540 | if (maybe_lhs_val) |lhs_val| { |
| ... | ... | @@ -16402,26 +16547,26 @@ fn analyzeArithmetic( |
| 16402 | 16547 | // If either of the operands are zero, then the other operand is returned. |
| 16403 | 16548 | // If either of the operands are undefined, the result is undefined. |
| 16404 | 16549 | if (maybe_lhs_val) |lhs_val| { |
| 16405 | if (!lhs_val.isUndef(mod) and (try lhs_val.compareAllWithZeroSema(.eq, mod))) { | |
| 16550 | if (!lhs_val.isUndef(mod) and (try lhs_val.compareAllWithZeroSema(.eq, pt))) { | |
| 16406 | 16551 | return casted_rhs; |
| 16407 | 16552 | } |
| 16408 | 16553 | } |
| 16409 | 16554 | if (maybe_rhs_val) |rhs_val| { |
| 16410 | 16555 | if (rhs_val.isUndef(mod)) { |
| 16411 | return mod.undefRef(resolved_type); | |
| 16556 | return pt.undefRef(resolved_type); | |
| 16412 | 16557 | } |
| 16413 | if (try rhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 16558 | if (try rhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 16414 | 16559 | return casted_lhs; |
| 16415 | 16560 | } |
| 16416 | 16561 | if (maybe_lhs_val) |lhs_val| { |
| 16417 | 16562 | if (lhs_val.isUndef(mod)) { |
| 16418 | return mod.undefRef(resolved_type); | |
| 16563 | return pt.undefRef(resolved_type); | |
| 16419 | 16564 | } |
| 16420 | 16565 | |
| 16421 | 16566 | const val = if (scalar_tag == .ComptimeInt) |
| 16422 | 16567 | try sema.intAdd(lhs_val, rhs_val, resolved_type, undefined) |
| 16423 | 16568 | else |
| 16424 | try lhs_val.intAddSat(rhs_val, resolved_type, sema.arena, mod); | |
| 16569 | try lhs_val.intAddSat(rhs_val, resolved_type, sema.arena, pt); | |
| 16425 | 16570 | |
| 16426 | 16571 | return Air.internedToRef(val.toIntern()); |
| 16427 | 16572 | } else break :rs .{ |
| ... | ... | @@ -16448,10 +16593,10 @@ fn analyzeArithmetic( |
| 16448 | 16593 | if (is_int) { |
| 16449 | 16594 | return sema.failWithUseOfUndef(block, rhs_src); |
| 16450 | 16595 | } else { |
| 16451 | return mod.undefRef(resolved_type); | |
| 16596 | return pt.undefRef(resolved_type); | |
| 16452 | 16597 | } |
| 16453 | 16598 | } |
| 16454 | if (try rhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 16599 | if (try rhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 16455 | 16600 | return casted_lhs; |
| 16456 | 16601 | } |
| 16457 | 16602 | } |
| ... | ... | @@ -16461,7 +16606,7 @@ fn analyzeArithmetic( |
| 16461 | 16606 | if (is_int) { |
| 16462 | 16607 | return sema.failWithUseOfUndef(block, lhs_src); |
| 16463 | 16608 | } else { |
| 16464 | return mod.undefRef(resolved_type); | |
| 16609 | return pt.undefRef(resolved_type); | |
| 16465 | 16610 | } |
| 16466 | 16611 | } |
| 16467 | 16612 | if (maybe_rhs_val) |rhs_val| { |
| ... | ... | @@ -16473,7 +16618,7 @@ fn analyzeArithmetic( |
| 16473 | 16618 | } |
| 16474 | 16619 | return Air.internedToRef(diff.toIntern()); |
| 16475 | 16620 | } else { |
| 16476 | return Air.internedToRef((try Value.floatSub(lhs_val, rhs_val, resolved_type, sema.arena, mod)).toIntern()); | |
| 16621 | return Air.internedToRef((try Value.floatSub(lhs_val, rhs_val, resolved_type, sema.arena, pt)).toIntern()); | |
| 16477 | 16622 | } |
| 16478 | 16623 | } else break :rs .{ rhs_src, air_tag, .sub_safe }; |
| 16479 | 16624 | } else break :rs .{ lhs_src, air_tag, .sub_safe }; |
| ... | ... | @@ -16484,15 +16629,15 @@ fn analyzeArithmetic( |
| 16484 | 16629 | // If either of the operands are undefined, the result is undefined. |
| 16485 | 16630 | if (maybe_rhs_val) |rhs_val| { |
| 16486 | 16631 | if (rhs_val.isUndef(mod)) { |
| 16487 | return mod.undefRef(resolved_type); | |
| 16632 | return pt.undefRef(resolved_type); | |
| 16488 | 16633 | } |
| 16489 | if (try rhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 16634 | if (try rhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 16490 | 16635 | return casted_lhs; |
| 16491 | 16636 | } |
| 16492 | 16637 | } |
| 16493 | 16638 | if (maybe_lhs_val) |lhs_val| { |
| 16494 | 16639 | if (lhs_val.isUndef(mod)) { |
| 16495 | return mod.undefRef(resolved_type); | |
| 16640 | return pt.undefRef(resolved_type); | |
| 16496 | 16641 | } |
| 16497 | 16642 | if (maybe_rhs_val) |rhs_val| { |
| 16498 | 16643 | return Air.internedToRef((try sema.numberSubWrapScalar(lhs_val, rhs_val, resolved_type)).toIntern()); |
| ... | ... | @@ -16505,21 +16650,21 @@ fn analyzeArithmetic( |
| 16505 | 16650 | // If either of the operands are undefined, the result is undefined. |
| 16506 | 16651 | if (maybe_rhs_val) |rhs_val| { |
| 16507 | 16652 | if (rhs_val.isUndef(mod)) { |
| 16508 | return mod.undefRef(resolved_type); | |
| 16653 | return pt.undefRef(resolved_type); | |
| 16509 | 16654 | } |
| 16510 | if (try rhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 16655 | if (try rhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 16511 | 16656 | return casted_lhs; |
| 16512 | 16657 | } |
| 16513 | 16658 | } |
| 16514 | 16659 | if (maybe_lhs_val) |lhs_val| { |
| 16515 | 16660 | if (lhs_val.isUndef(mod)) { |
| 16516 | return mod.undefRef(resolved_type); | |
| 16661 | return pt.undefRef(resolved_type); | |
| 16517 | 16662 | } |
| 16518 | 16663 | if (maybe_rhs_val) |rhs_val| { |
| 16519 | 16664 | const val = if (scalar_tag == .ComptimeInt) |
| 16520 | 16665 | try sema.intSub(lhs_val, rhs_val, resolved_type, undefined) |
| 16521 | 16666 | else |
| 16522 | try lhs_val.intSubSat(rhs_val, resolved_type, sema.arena, mod); | |
| 16667 | try lhs_val.intSubSat(rhs_val, resolved_type, sema.arena, pt); | |
| 16523 | 16668 | |
| 16524 | 16669 | return Air.internedToRef(val.toIntern()); |
| 16525 | 16670 | } else break :rs .{ rhs_src, .sub_sat, .sub_sat }; |
| ... | ... | @@ -16540,13 +16685,13 @@ fn analyzeArithmetic( |
| 16540 | 16685 | // the result is nan. |
| 16541 | 16686 | // If either of the operands are nan, the result is nan. |
| 16542 | 16687 | const scalar_zero = switch (scalar_tag) { |
| 16543 | .ComptimeFloat, .Float => try mod.floatValue(scalar_type, 0.0), | |
| 16544 | .ComptimeInt, .Int => try mod.intValue(scalar_type, 0), | |
| 16688 | .ComptimeFloat, .Float => try pt.floatValue(scalar_type, 0.0), | |
| 16689 | .ComptimeInt, .Int => try pt.intValue(scalar_type, 0), | |
| 16545 | 16690 | else => unreachable, |
| 16546 | 16691 | }; |
| 16547 | 16692 | const scalar_one = switch (scalar_tag) { |
| 16548 | .ComptimeFloat, .Float => try mod.floatValue(scalar_type, 1.0), | |
| 16549 | .ComptimeInt, .Int => try mod.intValue(scalar_type, 1), | |
| 16693 | .ComptimeFloat, .Float => try pt.floatValue(scalar_type, 1.0), | |
| 16694 | .ComptimeInt, .Int => try pt.intValue(scalar_type, 1), | |
| 16550 | 16695 | else => unreachable, |
| 16551 | 16696 | }; |
| 16552 | 16697 | if (maybe_lhs_val) |lhs_val| { |
| ... | ... | @@ -16554,13 +16699,13 @@ fn analyzeArithmetic( |
| 16554 | 16699 | if (lhs_val.isNan(mod)) { |
| 16555 | 16700 | return Air.internedToRef(lhs_val.toIntern()); |
| 16556 | 16701 | } |
| 16557 | if (try lhs_val.compareAllWithZeroSema(.eq, mod)) lz: { | |
| 16702 | if (try lhs_val.compareAllWithZeroSema(.eq, pt)) lz: { | |
| 16558 | 16703 | if (maybe_rhs_val) |rhs_val| { |
| 16559 | 16704 | if (rhs_val.isNan(mod)) { |
| 16560 | 16705 | return Air.internedToRef(rhs_val.toIntern()); |
| 16561 | 16706 | } |
| 16562 | 16707 | if (rhs_val.isInf(mod)) { |
| 16563 | return Air.internedToRef((try mod.floatValue(resolved_type, std.math.nan(f128))).toIntern()); | |
| 16708 | return Air.internedToRef((try pt.floatValue(resolved_type, std.math.nan(f128))).toIntern()); | |
| 16564 | 16709 | } |
| 16565 | 16710 | } else if (resolved_type.isAnyFloat()) { |
| 16566 | 16711 | break :lz; |
| ... | ... | @@ -16579,16 +16724,16 @@ fn analyzeArithmetic( |
| 16579 | 16724 | if (is_int) { |
| 16580 | 16725 | return sema.failWithUseOfUndef(block, rhs_src); |
| 16581 | 16726 | } else { |
| 16582 | return mod.undefRef(resolved_type); | |
| 16727 | return pt.undefRef(resolved_type); | |
| 16583 | 16728 | } |
| 16584 | 16729 | } |
| 16585 | 16730 | if (rhs_val.isNan(mod)) { |
| 16586 | 16731 | return Air.internedToRef(rhs_val.toIntern()); |
| 16587 | 16732 | } |
| 16588 | if (try rhs_val.compareAllWithZeroSema(.eq, mod)) rz: { | |
| 16733 | if (try rhs_val.compareAllWithZeroSema(.eq, pt)) rz: { | |
| 16589 | 16734 | if (maybe_lhs_val) |lhs_val| { |
| 16590 | 16735 | if (lhs_val.isInf(mod)) { |
| 16591 | return Air.internedToRef((try mod.floatValue(resolved_type, std.math.nan(f128))).toIntern()); | |
| 16736 | return Air.internedToRef((try pt.floatValue(resolved_type, std.math.nan(f128))).toIntern()); | |
| 16592 | 16737 | } |
| 16593 | 16738 | } else if (resolved_type.isAnyFloat()) { |
| 16594 | 16739 | break :rz; |
| ... | ... | @@ -16604,18 +16749,18 @@ fn analyzeArithmetic( |
| 16604 | 16749 | if (is_int) { |
| 16605 | 16750 | return sema.failWithUseOfUndef(block, lhs_src); |
| 16606 | 16751 | } else { |
| 16607 | return mod.undefRef(resolved_type); | |
| 16752 | return pt.undefRef(resolved_type); | |
| 16608 | 16753 | } |
| 16609 | 16754 | } |
| 16610 | 16755 | if (is_int) { |
| 16611 | 16756 | var overflow_idx: ?usize = null; |
| 16612 | const product = try lhs_val.intMul(rhs_val, resolved_type, &overflow_idx, sema.arena, mod); | |
| 16757 | const product = try lhs_val.intMul(rhs_val, resolved_type, &overflow_idx, sema.arena, pt); | |
| 16613 | 16758 | if (overflow_idx) |vec_idx| { |
| 16614 | 16759 | return sema.failWithIntegerOverflow(block, src, resolved_type, product, vec_idx); |
| 16615 | 16760 | } |
| 16616 | 16761 | return Air.internedToRef(product.toIntern()); |
| 16617 | 16762 | } else { |
| 16618 | return Air.internedToRef((try lhs_val.floatMul(rhs_val, resolved_type, sema.arena, mod)).toIntern()); | |
| 16763 | return Air.internedToRef((try lhs_val.floatMul(rhs_val, resolved_type, sema.arena, pt)).toIntern()); | |
| 16619 | 16764 | } |
| 16620 | 16765 | } else break :rs .{ lhs_src, air_tag, .mul_safe }; |
| 16621 | 16766 | } else break :rs .{ rhs_src, air_tag, .mul_safe }; |
| ... | ... | @@ -16626,18 +16771,18 @@ fn analyzeArithmetic( |
| 16626 | 16771 | // If either of the operands are one, result is the other operand. |
| 16627 | 16772 | // If either of the operands are undefined, result is undefined. |
| 16628 | 16773 | const scalar_zero = switch (scalar_tag) { |
| 16629 | .ComptimeFloat, .Float => try mod.floatValue(scalar_type, 0.0), | |
| 16630 | .ComptimeInt, .Int => try mod.intValue(scalar_type, 0), | |
| 16774 | .ComptimeFloat, .Float => try pt.floatValue(scalar_type, 0.0), | |
| 16775 | .ComptimeInt, .Int => try pt.intValue(scalar_type, 0), | |
| 16631 | 16776 | else => unreachable, |
| 16632 | 16777 | }; |
| 16633 | 16778 | const scalar_one = switch (scalar_tag) { |
| 16634 | .ComptimeFloat, .Float => try mod.floatValue(scalar_type, 1.0), | |
| 16635 | .ComptimeInt, .Int => try mod.intValue(scalar_type, 1), | |
| 16779 | .ComptimeFloat, .Float => try pt.floatValue(scalar_type, 1.0), | |
| 16780 | .ComptimeInt, .Int => try pt.intValue(scalar_type, 1), | |
| 16636 | 16781 | else => unreachable, |
| 16637 | 16782 | }; |
| 16638 | 16783 | if (maybe_lhs_val) |lhs_val| { |
| 16639 | 16784 | if (!lhs_val.isUndef(mod)) { |
| 16640 | if (try lhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 16785 | if (try lhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 16641 | 16786 | const zero_val = try sema.splat(resolved_type, scalar_zero); |
| 16642 | 16787 | return Air.internedToRef(zero_val.toIntern()); |
| 16643 | 16788 | } |
| ... | ... | @@ -16648,9 +16793,9 @@ fn analyzeArithmetic( |
| 16648 | 16793 | } |
| 16649 | 16794 | if (maybe_rhs_val) |rhs_val| { |
| 16650 | 16795 | if (rhs_val.isUndef(mod)) { |
| 16651 | return mod.undefRef(resolved_type); | |
| 16796 | return pt.undefRef(resolved_type); | |
| 16652 | 16797 | } |
| 16653 | if (try rhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 16798 | if (try rhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 16654 | 16799 | const zero_val = try sema.splat(resolved_type, scalar_zero); |
| 16655 | 16800 | return Air.internedToRef(zero_val.toIntern()); |
| 16656 | 16801 | } |
| ... | ... | @@ -16659,9 +16804,9 @@ fn analyzeArithmetic( |
| 16659 | 16804 | } |
| 16660 | 16805 | if (maybe_lhs_val) |lhs_val| { |
| 16661 | 16806 | if (lhs_val.isUndef(mod)) { |
| 16662 | return mod.undefRef(resolved_type); | |
| 16807 | return pt.undefRef(resolved_type); | |
| 16663 | 16808 | } |
| 16664 | return Air.internedToRef((try lhs_val.numberMulWrap(rhs_val, resolved_type, sema.arena, mod)).toIntern()); | |
| 16809 | return Air.internedToRef((try lhs_val.numberMulWrap(rhs_val, resolved_type, sema.arena, pt)).toIntern()); | |
| 16665 | 16810 | } else break :rs .{ lhs_src, .mul_wrap, .mul_wrap }; |
| 16666 | 16811 | } else break :rs .{ rhs_src, .mul_wrap, .mul_wrap }; |
| 16667 | 16812 | }, |
| ... | ... | @@ -16671,18 +16816,18 @@ fn analyzeArithmetic( |
| 16671 | 16816 | // If either of the operands are one, result is the other operand. |
| 16672 | 16817 | // If either of the operands are undefined, result is undefined. |
| 16673 | 16818 | const scalar_zero = switch (scalar_tag) { |
| 16674 | .ComptimeFloat, .Float => try mod.floatValue(scalar_type, 0.0), | |
| 16675 | .ComptimeInt, .Int => try mod.intValue(scalar_type, 0), | |
| 16819 | .ComptimeFloat, .Float => try pt.floatValue(scalar_type, 0.0), | |
| 16820 | .ComptimeInt, .Int => try pt.intValue(scalar_type, 0), | |
| 16676 | 16821 | else => unreachable, |
| 16677 | 16822 | }; |
| 16678 | 16823 | const scalar_one = switch (scalar_tag) { |
| 16679 | .ComptimeFloat, .Float => try mod.floatValue(scalar_type, 1.0), | |
| 16680 | .ComptimeInt, .Int => try mod.intValue(scalar_type, 1), | |
| 16824 | .ComptimeFloat, .Float => try pt.floatValue(scalar_type, 1.0), | |
| 16825 | .ComptimeInt, .Int => try pt.intValue(scalar_type, 1), | |
| 16681 | 16826 | else => unreachable, |
| 16682 | 16827 | }; |
| 16683 | 16828 | if (maybe_lhs_val) |lhs_val| { |
| 16684 | 16829 | if (!lhs_val.isUndef(mod)) { |
| 16685 | if (try lhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 16830 | if (try lhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 16686 | 16831 | const zero_val = try sema.splat(resolved_type, scalar_zero); |
| 16687 | 16832 | return Air.internedToRef(zero_val.toIntern()); |
| 16688 | 16833 | } |
| ... | ... | @@ -16693,9 +16838,9 @@ fn analyzeArithmetic( |
| 16693 | 16838 | } |
| 16694 | 16839 | if (maybe_rhs_val) |rhs_val| { |
| 16695 | 16840 | if (rhs_val.isUndef(mod)) { |
| 16696 | return mod.undefRef(resolved_type); | |
| 16841 | return pt.undefRef(resolved_type); | |
| 16697 | 16842 | } |
| 16698 | if (try rhs_val.compareAllWithZeroSema(.eq, mod)) { | |
| 16843 | if (try rhs_val.compareAllWithZeroSema(.eq, pt)) { | |
| 16699 | 16844 | const zero_val = try sema.splat(resolved_type, scalar_zero); |
| 16700 | 16845 | return Air.internedToRef(zero_val.toIntern()); |
| 16701 | 16846 | } |
| ... | ... | @@ -16704,13 +16849,13 @@ fn analyzeArithmetic( |
| 16704 | 16849 | } |
| 16705 | 16850 | if (maybe_lhs_val) |lhs_val| { |
| 16706 | 16851 | if (lhs_val.isUndef(mod)) { |
| 16707 | return mod.undefRef(resolved_type); | |
| 16852 | return pt.undefRef(resolved_type); | |
| 16708 | 16853 | } |
| 16709 | 16854 | |
| 16710 | 16855 | const val = if (scalar_tag == .ComptimeInt) |
| 16711 | try lhs_val.intMul(rhs_val, resolved_type, undefined, sema.arena, mod) | |
| 16856 | try lhs_val.intMul(rhs_val, resolved_type, undefined, sema.arena, pt) | |
| 16712 | 16857 | else |
| 16713 | try lhs_val.intMulSat(rhs_val, resolved_type, sema.arena, mod); | |
| 16858 | try lhs_val.intMulSat(rhs_val, resolved_type, sema.arena, pt); | |
| 16714 | 16859 | |
| 16715 | 16860 | return Air.internedToRef(val.toIntern()); |
| 16716 | 16861 | } else break :rs .{ lhs_src, .mul_sat, .mul_sat }; |
| ... | ... | @@ -16758,7 +16903,7 @@ fn analyzeArithmetic( |
| 16758 | 16903 | }) |
| 16759 | 16904 | else |
| 16760 | 16905 | ov_bit; |
| 16761 | const zero_ov = Air.internedToRef((try mod.intValue(Type.u1, 0)).toIntern()); | |
| 16906 | const zero_ov = Air.internedToRef((try pt.intValue(Type.u1, 0)).toIntern()); | |
| 16762 | 16907 | const no_ov = try block.addBinOp(.cmp_eq, any_ov_bit, zero_ov); |
| 16763 | 16908 | |
| 16764 | 16909 | try sema.addSafetyCheck(block, src, no_ov, .integer_overflow); |
| ... | ... | @@ -16782,7 +16927,8 @@ fn analyzePtrArithmetic( |
| 16782 | 16927 | // TODO if the operand is comptime-known to be negative, or is a negative int, |
| 16783 | 16928 | // coerce to isize instead of usize. |
| 16784 | 16929 | const offset = try sema.coerce(block, Type.usize, uncasted_offset, offset_src); |
| 16785 | const mod = sema.mod; | |
| 16930 | const pt = sema.pt; | |
| 16931 | const mod = pt.zcu; | |
| 16786 | 16932 | const opt_ptr_val = try sema.resolveValue(ptr); |
| 16787 | 16933 | const opt_off_val = try sema.resolveDefinedValue(block, offset_src, offset); |
| 16788 | 16934 | const ptr_ty = sema.typeOf(ptr); |
| ... | ... | @@ -16800,7 +16946,7 @@ fn analyzePtrArithmetic( |
| 16800 | 16946 | // it being a multiple of the type size. |
| 16801 | 16947 | const elem_size = try sema.typeAbiSize(Type.fromInterned(ptr_info.child)); |
| 16802 | 16948 | const addend = if (opt_off_val) |off_val| a: { |
| 16803 | const off_int = try sema.usizeCast(block, offset_src, try off_val.toUnsignedIntSema(mod)); | |
| 16949 | const off_int = try sema.usizeCast(block, offset_src, try off_val.toUnsignedIntSema(pt)); | |
| 16804 | 16950 | break :a elem_size * off_int; |
| 16805 | 16951 | } else elem_size; |
| 16806 | 16952 | |
| ... | ... | @@ -16813,7 +16959,7 @@ fn analyzePtrArithmetic( |
| 16813 | 16959 | )); |
| 16814 | 16960 | assert(new_align != .none); |
| 16815 | 16961 | |
| 16816 | break :t try mod.ptrTypeSema(.{ | |
| 16962 | break :t try pt.ptrTypeSema(.{ | |
| 16817 | 16963 | .child = ptr_info.child, |
| 16818 | 16964 | .sentinel = ptr_info.sentinel, |
| 16819 | 16965 | .flags = .{ |
| ... | ... | @@ -16830,16 +16976,16 @@ fn analyzePtrArithmetic( |
| 16830 | 16976 | const runtime_src = rs: { |
| 16831 | 16977 | if (opt_ptr_val) |ptr_val| { |
| 16832 | 16978 | if (opt_off_val) |offset_val| { |
| 16833 | if (ptr_val.isUndef(mod)) return mod.undefRef(new_ptr_ty); | |
| 16979 | if (ptr_val.isUndef(mod)) return pt.undefRef(new_ptr_ty); | |
| 16834 | 16980 | |
| 16835 | const offset_int = try sema.usizeCast(block, offset_src, try offset_val.toUnsignedIntSema(mod)); | |
| 16981 | const offset_int = try sema.usizeCast(block, offset_src, try offset_val.toUnsignedIntSema(pt)); | |
| 16836 | 16982 | if (offset_int == 0) return ptr; |
| 16837 | 16983 | if (air_tag == .ptr_sub) { |
| 16838 | 16984 | const elem_size = try sema.typeAbiSize(Type.fromInterned(ptr_info.child)); |
| 16839 | 16985 | const new_ptr_val = try sema.ptrSubtract(block, op_src, ptr_val, offset_int * elem_size, new_ptr_ty); |
| 16840 | 16986 | return Air.internedToRef(new_ptr_val.toIntern()); |
| 16841 | 16987 | } else { |
| 16842 | const new_ptr_val = try mod.getCoerced(try ptr_val.ptrElem(offset_int, mod), new_ptr_ty); | |
| 16988 | const new_ptr_val = try pt.getCoerced(try ptr_val.ptrElem(offset_int, pt), new_ptr_ty); | |
| 16843 | 16989 | return Air.internedToRef(new_ptr_val.toIntern()); |
| 16844 | 16990 | } |
| 16845 | 16991 | } else break :rs offset_src; |
| ... | ... | @@ -16879,6 +17025,8 @@ fn zirAsm( |
| 16879 | 17025 | const tracy = trace(@src()); |
| 16880 | 17026 | defer tracy.end(); |
| 16881 | 17027 | |
| 17028 | const pt = sema.pt; | |
| 17029 | const mod = pt.zcu; | |
| 16882 | 17030 | const extra = sema.code.extraData(Zir.Inst.Asm, extended.operand); |
| 16883 | 17031 | const src = block.nodeOffset(extra.data.src_node); |
| 16884 | 17032 | const ret_ty_src = block.src(.{ .node_offset_asm_ret_ty = extra.data.src_node }); |
| ... | ... | @@ -16910,7 +17058,7 @@ fn zirAsm( |
| 16910 | 17058 | if (is_volatile) { |
| 16911 | 17059 | return sema.fail(block, src, "volatile keyword is redundant on module-level assembly", .{}); |
| 16912 | 17060 | } |
| 16913 | try sema.mod.addGlobalAssembly(sema.owner_decl_index, asm_source); | |
| 17061 | try mod.addGlobalAssembly(sema.owner_decl_index, asm_source); | |
| 16914 | 17062 | return .void_value; |
| 16915 | 17063 | } |
| 16916 | 17064 | |
| ... | ... | @@ -16959,7 +17107,6 @@ fn zirAsm( |
| 16959 | 17107 | |
| 16960 | 17108 | const args = try sema.arena.alloc(Air.Inst.Ref, inputs_len); |
| 16961 | 17109 | const inputs = try sema.arena.alloc(ConstraintName, inputs_len); |
| 16962 | const mod = sema.mod; | |
| 16963 | 17110 | |
| 16964 | 17111 | for (args, 0..) |*arg, arg_i| { |
| 16965 | 17112 | const input = sema.code.extraData(Zir.Inst.Asm.Input, extra_i); |
| ... | ... | @@ -17049,7 +17196,8 @@ fn zirCmpEq( |
| 17049 | 17196 | const tracy = trace(@src()); |
| 17050 | 17197 | defer tracy.end(); |
| 17051 | 17198 | |
| 17052 | const mod = sema.mod; | |
| 17199 | const pt = sema.pt; | |
| 17200 | const mod = pt.zcu; | |
| 17053 | 17201 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 17054 | 17202 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 17055 | 17203 | const src: LazySrcLoc = block.nodeOffset(inst_data.src_node); |
| ... | ... | @@ -17077,7 +17225,7 @@ fn zirCmpEq( |
| 17077 | 17225 | |
| 17078 | 17226 | if (lhs_ty_tag == .Null or rhs_ty_tag == .Null) { |
| 17079 | 17227 | const non_null_type = if (lhs_ty_tag == .Null) rhs_ty else lhs_ty; |
| 17080 | return sema.fail(block, src, "comparison of '{}' with null", .{non_null_type.fmt(mod)}); | |
| 17228 | return sema.fail(block, src, "comparison of '{}' with null", .{non_null_type.fmt(pt)}); | |
| 17081 | 17229 | } |
| 17082 | 17230 | |
| 17083 | 17231 | if (lhs_ty_tag == .Union and (rhs_ty_tag == .EnumLiteral or rhs_ty_tag == .Enum)) { |
| ... | ... | @@ -17092,7 +17240,7 @@ fn zirCmpEq( |
| 17092 | 17240 | if (try sema.resolveValue(lhs)) |lval| { |
| 17093 | 17241 | if (try sema.resolveValue(rhs)) |rval| { |
| 17094 | 17242 | if (lval.isUndef(mod) or rval.isUndef(mod)) { |
| 17095 | return mod.undefRef(Type.bool); | |
| 17243 | return pt.undefRef(Type.bool); | |
| 17096 | 17244 | } |
| 17097 | 17245 | const lkey = mod.intern_pool.indexToKey(lval.toIntern()); |
| 17098 | 17246 | const rkey = mod.intern_pool.indexToKey(rval.toIntern()); |
| ... | ... | @@ -17128,14 +17276,15 @@ fn analyzeCmpUnionTag( |
| 17128 | 17276 | tag_src: LazySrcLoc, |
| 17129 | 17277 | op: std.math.CompareOperator, |
| 17130 | 17278 | ) CompileError!Air.Inst.Ref { |
| 17131 | const mod = sema.mod; | |
| 17279 | const pt = sema.pt; | |
| 17280 | const mod = pt.zcu; | |
| 17132 | 17281 | const union_ty = sema.typeOf(un); |
| 17133 | try union_ty.resolveFields(mod); | |
| 17282 | try union_ty.resolveFields(pt); | |
| 17134 | 17283 | const union_tag_ty = union_ty.unionTagType(mod) orelse { |
| 17135 | 17284 | const msg = msg: { |
| 17136 | 17285 | const msg = try sema.errMsg(un_src, "comparison of union and enum literal is only valid for tagged union types", .{}); |
| 17137 | 17286 | errdefer msg.destroy(sema.gpa); |
| 17138 | try sema.errNote(union_ty.srcLoc(mod), msg, "union '{}' is not a tagged union", .{union_ty.fmt(mod)}); | |
| 17287 | try sema.errNote(union_ty.srcLoc(mod), msg, "union '{}' is not a tagged union", .{union_ty.fmt(pt)}); | |
| 17139 | 17288 | break :msg msg; |
| 17140 | 17289 | }; |
| 17141 | 17290 | return sema.failWithOwnedErrorMsg(block, msg); |
| ... | ... | @@ -17146,7 +17295,7 @@ fn analyzeCmpUnionTag( |
| 17146 | 17295 | const coerced_union = try sema.coerce(block, union_tag_ty, un, un_src); |
| 17147 | 17296 | |
| 17148 | 17297 | if (try sema.resolveValue(coerced_tag)) |enum_val| { |
| 17149 | if (enum_val.isUndef(mod)) return mod.undefRef(Type.bool); | |
| 17298 | if (enum_val.isUndef(mod)) return pt.undefRef(Type.bool); | |
| 17150 | 17299 | const field_ty = union_ty.unionFieldType(enum_val, mod).?; |
| 17151 | 17300 | if (field_ty.zigTypeTag(mod) == .NoReturn) { |
| 17152 | 17301 | return .bool_false; |
| ... | ... | @@ -17187,7 +17336,8 @@ fn analyzeCmp( |
| 17187 | 17336 | rhs_src: LazySrcLoc, |
| 17188 | 17337 | is_equality_cmp: bool, |
| 17189 | 17338 | ) CompileError!Air.Inst.Ref { |
| 17190 | const mod = sema.mod; | |
| 17339 | const pt = sema.pt; | |
| 17340 | const mod = pt.zcu; | |
| 17191 | 17341 | const lhs_ty = sema.typeOf(lhs); |
| 17192 | 17342 | const rhs_ty = sema.typeOf(rhs); |
| 17193 | 17343 | if (lhs_ty.zigTypeTag(mod) != .Optional and rhs_ty.zigTypeTag(mod) != .Optional) { |
| ... | ... | @@ -17215,7 +17365,7 @@ fn analyzeCmp( |
| 17215 | 17365 | const resolved_type = try sema.resolvePeerTypes(block, src, instructions, .{ .override = &[_]?LazySrcLoc{ lhs_src, rhs_src } }); |
| 17216 | 17366 | if (!resolved_type.isSelfComparable(mod, is_equality_cmp)) { |
| 17217 | 17367 | return sema.fail(block, src, "operator {s} not allowed for type '{}'", .{ |
| 17218 | compareOperatorName(op), resolved_type.fmt(mod), | |
| 17368 | compareOperatorName(op), resolved_type.fmt(pt), | |
| 17219 | 17369 | }); |
| 17220 | 17370 | } |
| 17221 | 17371 | const casted_lhs = try sema.coerce(block, resolved_type, lhs, lhs_src); |
| ... | ... | @@ -17244,13 +17394,14 @@ fn cmpSelf( |
| 17244 | 17394 | lhs_src: LazySrcLoc, |
| 17245 | 17395 | rhs_src: LazySrcLoc, |
| 17246 | 17396 | ) CompileError!Air.Inst.Ref { |
| 17247 | const mod = sema.mod; | |
| 17397 | const pt = sema.pt; | |
| 17398 | const mod = pt.zcu; | |
| 17248 | 17399 | const resolved_type = sema.typeOf(casted_lhs); |
| 17249 | 17400 | const runtime_src: LazySrcLoc = src: { |
| 17250 | 17401 | if (try sema.resolveValue(casted_lhs)) |lhs_val| { |
| 17251 | if (lhs_val.isUndef(mod)) return mod.undefRef(Type.bool); | |
| 17402 | if (lhs_val.isUndef(mod)) return pt.undefRef(Type.bool); | |
| 17252 | 17403 | if (try sema.resolveValue(casted_rhs)) |rhs_val| { |
| 17253 | if (rhs_val.isUndef(mod)) return mod.undefRef(Type.bool); | |
| 17404 | if (rhs_val.isUndef(mod)) return pt.undefRef(Type.bool); | |
| 17254 | 17405 | |
| 17255 | 17406 | if (resolved_type.zigTypeTag(mod) == .Vector) { |
| 17256 | 17407 | const cmp_val = try sema.compareVector(lhs_val, op, rhs_val, resolved_type); |
| ... | ... | @@ -17273,7 +17424,7 @@ fn cmpSelf( |
| 17273 | 17424 | // bool eq/neq more efficiently. |
| 17274 | 17425 | if (resolved_type.zigTypeTag(mod) == .Bool) { |
| 17275 | 17426 | if (try sema.resolveValue(casted_rhs)) |rhs_val| { |
| 17276 | if (rhs_val.isUndef(mod)) return mod.undefRef(Type.bool); | |
| 17427 | if (rhs_val.isUndef(mod)) return pt.undefRef(Type.bool); | |
| 17277 | 17428 | return sema.runtimeBoolCmp(block, src, op, casted_lhs, rhs_val.toBool(), lhs_src); |
| 17278 | 17429 | } |
| 17279 | 17430 | } |
| ... | ... | @@ -17310,24 +17461,24 @@ fn runtimeBoolCmp( |
| 17310 | 17461 | } |
| 17311 | 17462 | |
| 17312 | 17463 | fn zirSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 17313 | const mod = sema.mod; | |
| 17464 | const pt = sema.pt; | |
| 17314 | 17465 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 17315 | 17466 | const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| 17316 | 17467 | const ty = try sema.resolveType(block, operand_src, inst_data.operand); |
| 17317 | switch (ty.zigTypeTag(mod)) { | |
| 17468 | switch (ty.zigTypeTag(pt.zcu)) { | |
| 17318 | 17469 | .Fn, |
| 17319 | 17470 | .NoReturn, |
| 17320 | 17471 | .Undefined, |
| 17321 | 17472 | .Null, |
| 17322 | 17473 | .Opaque, |
| 17323 | => return sema.fail(block, operand_src, "no size available for type '{}'", .{ty.fmt(mod)}), | |
| 17474 | => return sema.fail(block, operand_src, "no size available for type '{}'", .{ty.fmt(pt)}), | |
| 17324 | 17475 | |
| 17325 | 17476 | .Type, |
| 17326 | 17477 | .EnumLiteral, |
| 17327 | 17478 | .ComptimeFloat, |
| 17328 | 17479 | .ComptimeInt, |
| 17329 | 17480 | .Void, |
| 17330 | => return mod.intRef(Type.comptime_int, 0), | |
| 17481 | => return pt.intRef(Type.comptime_int, 0), | |
| 17331 | 17482 | |
| 17332 | 17483 | .Bool, |
| 17333 | 17484 | .Int, |
| ... | ... | @@ -17345,12 +17496,13 @@ fn zirSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 17345 | 17496 | .AnyFrame, |
| 17346 | 17497 | => {}, |
| 17347 | 17498 | } |
| 17348 | const val = try ty.lazyAbiSize(mod); | |
| 17499 | const val = try ty.lazyAbiSize(pt); | |
| 17349 | 17500 | return Air.internedToRef(val.toIntern()); |
| 17350 | 17501 | } |
| 17351 | 17502 | |
| 17352 | 17503 | fn zirBitSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 17353 | const mod = sema.mod; | |
| 17504 | const pt = sema.pt; | |
| 17505 | const mod = pt.zcu; | |
| 17354 | 17506 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 17355 | 17507 | const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| 17356 | 17508 | const operand_ty = try sema.resolveType(block, operand_src, inst_data.operand); |
| ... | ... | @@ -17360,14 +17512,14 @@ fn zirBitSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 17360 | 17512 | .Undefined, |
| 17361 | 17513 | .Null, |
| 17362 | 17514 | .Opaque, |
| 17363 | => return sema.fail(block, operand_src, "no size available for type '{}'", .{operand_ty.fmt(mod)}), | |
| 17515 | => return sema.fail(block, operand_src, "no size available for type '{}'", .{operand_ty.fmt(pt)}), | |
| 17364 | 17516 | |
| 17365 | 17517 | .Type, |
| 17366 | 17518 | .EnumLiteral, |
| 17367 | 17519 | .ComptimeFloat, |
| 17368 | 17520 | .ComptimeInt, |
| 17369 | 17521 | .Void, |
| 17370 | => return mod.intRef(Type.comptime_int, 0), | |
| 17522 | => return pt.intRef(Type.comptime_int, 0), | |
| 17371 | 17523 | |
| 17372 | 17524 | .Bool, |
| 17373 | 17525 | .Int, |
| ... | ... | @@ -17385,8 +17537,8 @@ fn zirBitSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 17385 | 17537 | .AnyFrame, |
| 17386 | 17538 | => {}, |
| 17387 | 17539 | } |
| 17388 | const bit_size = try operand_ty.bitSizeAdvanced(mod, .sema); | |
| 17389 | return mod.intRef(Type.comptime_int, bit_size); | |
| 17540 | const bit_size = try operand_ty.bitSizeAdvanced(pt, .sema); | |
| 17541 | return pt.intRef(Type.comptime_int, bit_size); | |
| 17390 | 17542 | } |
| 17391 | 17543 | |
| 17392 | 17544 | fn zirThis( |
| ... | ... | @@ -17394,14 +17546,16 @@ fn zirThis( |
| 17394 | 17546 | block: *Block, |
| 17395 | 17547 | extended: Zir.Inst.Extended.InstData, |
| 17396 | 17548 | ) CompileError!Air.Inst.Ref { |
| 17397 | const mod = sema.mod; | |
| 17549 | const pt = sema.pt; | |
| 17550 | const mod = pt.zcu; | |
| 17398 | 17551 | const this_decl_index = mod.namespacePtr(block.namespace).decl_index; |
| 17399 | 17552 | const src = block.nodeOffset(@bitCast(extended.operand)); |
| 17400 | 17553 | return sema.analyzeDeclVal(block, src, this_decl_index); |
| 17401 | 17554 | } |
| 17402 | 17555 | |
| 17403 | 17556 | fn zirClosureGet(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { |
| 17404 | const mod = sema.mod; | |
| 17557 | const pt = sema.pt; | |
| 17558 | const mod = pt.zcu; | |
| 17405 | 17559 | const ip = &mod.intern_pool; |
| 17406 | 17560 | const captures = mod.namespacePtr(block.namespace).getType(mod).getCaptures(mod); |
| 17407 | 17561 | |
| ... | ... | @@ -17489,7 +17643,7 @@ fn zirRetAddr( |
| 17489 | 17643 | _ = extended; |
| 17490 | 17644 | if (block.is_comptime) { |
| 17491 | 17645 | // TODO: we could give a meaningful lazy value here. #14938 |
| 17492 | return sema.mod.intRef(Type.usize, 0); | |
| 17646 | return sema.pt.intRef(Type.usize, 0); | |
| 17493 | 17647 | } else { |
| 17494 | 17648 | return block.addNoOp(.ret_addr); |
| 17495 | 17649 | } |
| ... | ... | @@ -17514,7 +17668,8 @@ fn zirBuiltinSrc( |
| 17514 | 17668 | const tracy = trace(@src()); |
| 17515 | 17669 | defer tracy.end(); |
| 17516 | 17670 | |
| 17517 | const mod = sema.mod; | |
| 17671 | const pt = sema.pt; | |
| 17672 | const mod = pt.zcu; | |
| 17518 | 17673 | const extra = sema.code.extraData(Zir.Inst.Src, extended.operand).data; |
| 17519 | 17674 | const fn_owner_decl = mod.funcOwnerDeclPtr(sema.func_index); |
| 17520 | 17675 | const ip = &mod.intern_pool; |
| ... | ... | @@ -17522,43 +17677,43 @@ fn zirBuiltinSrc( |
| 17522 | 17677 | |
| 17523 | 17678 | const func_name_val = v: { |
| 17524 | 17679 | const func_name_len = fn_owner_decl.name.length(ip); |
| 17525 | const array_ty = try ip.get(gpa, .{ .array_type = .{ | |
| 17680 | const array_ty = try pt.intern(.{ .array_type = .{ | |
| 17526 | 17681 | .len = func_name_len, |
| 17527 | 17682 | .sentinel = .zero_u8, |
| 17528 | 17683 | .child = .u8_type, |
| 17529 | 17684 | } }); |
| 17530 | break :v try ip.get(gpa, .{ .slice = .{ | |
| 17685 | break :v try pt.intern(.{ .slice = .{ | |
| 17531 | 17686 | .ty = .slice_const_u8_sentinel_0_type, |
| 17532 | .ptr = try ip.get(gpa, .{ .ptr = .{ | |
| 17687 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 17533 | 17688 | .ty = .manyptr_const_u8_sentinel_0_type, |
| 17534 | 17689 | .base_addr = .{ .anon_decl = .{ |
| 17535 | 17690 | .orig_ty = .slice_const_u8_sentinel_0_type, |
| 17536 | .val = try ip.get(gpa, .{ .aggregate = .{ | |
| 17691 | .val = try pt.intern(.{ .aggregate = .{ | |
| 17537 | 17692 | .ty = array_ty, |
| 17538 | 17693 | .storage = .{ .bytes = fn_owner_decl.name.toString() }, |
| 17539 | 17694 | } }), |
| 17540 | 17695 | } }, |
| 17541 | 17696 | .byte_offset = 0, |
| 17542 | 17697 | } }), |
| 17543 | .len = (try mod.intValue(Type.usize, func_name_len)).toIntern(), | |
| 17698 | .len = (try pt.intValue(Type.usize, func_name_len)).toIntern(), | |
| 17544 | 17699 | } }); |
| 17545 | 17700 | }; |
| 17546 | 17701 | |
| 17547 | 17702 | const file_name_val = v: { |
| 17548 | 17703 | // The compiler must not call realpath anywhere. |
| 17549 | 17704 | const file_name = try fn_owner_decl.getFileScope(mod).fullPath(sema.arena); |
| 17550 | const array_ty = try ip.get(gpa, .{ .array_type = .{ | |
| 17705 | const array_ty = try pt.intern(.{ .array_type = .{ | |
| 17551 | 17706 | .len = file_name.len, |
| 17552 | 17707 | .sentinel = .zero_u8, |
| 17553 | 17708 | .child = .u8_type, |
| 17554 | 17709 | } }); |
| 17555 | break :v try ip.get(gpa, .{ .slice = .{ | |
| 17710 | break :v try pt.intern(.{ .slice = .{ | |
| 17556 | 17711 | .ty = .slice_const_u8_sentinel_0_type, |
| 17557 | .ptr = try ip.get(gpa, .{ .ptr = .{ | |
| 17712 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 17558 | 17713 | .ty = .manyptr_const_u8_sentinel_0_type, |
| 17559 | 17714 | .base_addr = .{ .anon_decl = .{ |
| 17560 | 17715 | .orig_ty = .slice_const_u8_sentinel_0_type, |
| 17561 | .val = try ip.get(gpa, .{ .aggregate = .{ | |
| 17716 | .val = try pt.intern(.{ .aggregate = .{ | |
| 17562 | 17717 | .ty = array_ty, |
| 17563 | 17718 | .storage = .{ |
| 17564 | 17719 | .bytes = try ip.getOrPutString(gpa, file_name, .maybe_embedded_nulls), |
| ... | ... | @@ -17567,35 +17722,36 @@ fn zirBuiltinSrc( |
| 17567 | 17722 | } }, |
| 17568 | 17723 | .byte_offset = 0, |
| 17569 | 17724 | } }), |
| 17570 | .len = (try mod.intValue(Type.usize, file_name.len)).toIntern(), | |
| 17725 | .len = (try pt.intValue(Type.usize, file_name.len)).toIntern(), | |
| 17571 | 17726 | } }); |
| 17572 | 17727 | }; |
| 17573 | 17728 | |
| 17574 | const src_loc_ty = try mod.getBuiltinType("SourceLocation"); | |
| 17729 | const src_loc_ty = try pt.getBuiltinType("SourceLocation"); | |
| 17575 | 17730 | const fields = .{ |
| 17576 | 17731 | // file: [:0]const u8, |
| 17577 | 17732 | file_name_val, |
| 17578 | 17733 | // fn_name: [:0]const u8, |
| 17579 | 17734 | func_name_val, |
| 17580 | 17735 | // line: u32, |
| 17581 | (try mod.intValue(Type.u32, extra.line + 1)).toIntern(), | |
| 17736 | (try pt.intValue(Type.u32, extra.line + 1)).toIntern(), | |
| 17582 | 17737 | // column: u32, |
| 17583 | (try mod.intValue(Type.u32, extra.column + 1)).toIntern(), | |
| 17738 | (try pt.intValue(Type.u32, extra.column + 1)).toIntern(), | |
| 17584 | 17739 | }; |
| 17585 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 17740 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 17586 | 17741 | .ty = src_loc_ty.toIntern(), |
| 17587 | 17742 | .storage = .{ .elems = &fields }, |
| 17588 | 17743 | } }))); |
| 17589 | 17744 | } |
| 17590 | 17745 | |
| 17591 | 17746 | fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 17592 | const mod = sema.mod; | |
| 17747 | const pt = sema.pt; | |
| 17748 | const mod = pt.zcu; | |
| 17593 | 17749 | const gpa = sema.gpa; |
| 17594 | 17750 | const ip = &mod.intern_pool; |
| 17595 | 17751 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 17596 | 17752 | const src = block.nodeOffset(inst_data.src_node); |
| 17597 | 17753 | const ty = try sema.resolveType(block, src, inst_data.operand); |
| 17598 | const type_info_ty = try mod.getBuiltinType("Type"); | |
| 17754 | const type_info_ty = try pt.getBuiltinType("Type"); | |
| 17599 | 17755 | const type_info_tag_ty = type_info_ty.unionTagType(mod).?; |
| 17600 | 17756 | |
| 17601 | 17757 | if (ty.typeDeclInst(mod)) |type_decl_inst| { |
| ... | ... | @@ -17612,9 +17768,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17612 | 17768 | .Undefined, |
| 17613 | 17769 | .Null, |
| 17614 | 17770 | .EnumLiteral, |
| 17615 | => |type_info_tag| return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 17771 | => |type_info_tag| return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 17616 | 17772 | .ty = type_info_ty.toIntern(), |
| 17617 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(type_info_tag))).toIntern(), | |
| 17773 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(type_info_tag))).toIntern(), | |
| 17618 | 17774 | .val = .void_value, |
| 17619 | 17775 | } }))), |
| 17620 | 17776 | .Fn => { |
| ... | ... | @@ -17643,8 +17799,8 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17643 | 17799 | for (param_vals, 0..) |*param_val, i| { |
| 17644 | 17800 | const param_ty = func_ty_info.param_types.get(ip)[i]; |
| 17645 | 17801 | const is_generic = param_ty == .generic_poison_type; |
| 17646 | const param_ty_val = try ip.get(gpa, .{ .opt = .{ | |
| 17647 | .ty = try ip.get(gpa, .{ .opt_type = .type_type }), | |
| 17802 | const param_ty_val = try pt.intern(.{ .opt = .{ | |
| 17803 | .ty = try pt.intern(.{ .opt_type = .type_type }), | |
| 17648 | 17804 | .val = if (is_generic) .none else param_ty, |
| 17649 | 17805 | } }); |
| 17650 | 17806 | |
| ... | ... | @@ -17661,22 +17817,22 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17661 | 17817 | // type: ?type, |
| 17662 | 17818 | param_ty_val, |
| 17663 | 17819 | }; |
| 17664 | param_val.* = try mod.intern(.{ .aggregate = .{ | |
| 17820 | param_val.* = try pt.intern(.{ .aggregate = .{ | |
| 17665 | 17821 | .ty = param_info_ty.toIntern(), |
| 17666 | 17822 | .storage = .{ .elems = &param_fields }, |
| 17667 | 17823 | } }); |
| 17668 | 17824 | } |
| 17669 | 17825 | |
| 17670 | 17826 | const args_val = v: { |
| 17671 | const new_decl_ty = try mod.arrayType(.{ | |
| 17827 | const new_decl_ty = try pt.arrayType(.{ | |
| 17672 | 17828 | .len = param_vals.len, |
| 17673 | 17829 | .child = param_info_ty.toIntern(), |
| 17674 | 17830 | }); |
| 17675 | const new_decl_val = try mod.intern(.{ .aggregate = .{ | |
| 17831 | const new_decl_val = try pt.intern(.{ .aggregate = .{ | |
| 17676 | 17832 | .ty = new_decl_ty.toIntern(), |
| 17677 | 17833 | .storage = .{ .elems = param_vals }, |
| 17678 | 17834 | } }); |
| 17679 | const slice_ty = (try mod.ptrTypeSema(.{ | |
| 17835 | const slice_ty = (try pt.ptrTypeSema(.{ | |
| 17680 | 17836 | .child = param_info_ty.toIntern(), |
| 17681 | 17837 | .flags = .{ |
| 17682 | 17838 | .size = .Slice, |
| ... | ... | @@ -17684,9 +17840,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17684 | 17840 | }, |
| 17685 | 17841 | })).toIntern(); |
| 17686 | 17842 | const manyptr_ty = Type.fromInterned(slice_ty).slicePtrFieldType(mod).toIntern(); |
| 17687 | break :v try mod.intern(.{ .slice = .{ | |
| 17843 | break :v try pt.intern(.{ .slice = .{ | |
| 17688 | 17844 | .ty = slice_ty, |
| 17689 | .ptr = try mod.intern(.{ .ptr = .{ | |
| 17845 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 17690 | 17846 | .ty = manyptr_ty, |
| 17691 | 17847 | .base_addr = .{ .anon_decl = .{ |
| 17692 | 17848 | .orig_ty = manyptr_ty, |
| ... | ... | @@ -17694,23 +17850,23 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17694 | 17850 | } }, |
| 17695 | 17851 | .byte_offset = 0, |
| 17696 | 17852 | } }), |
| 17697 | .len = (try mod.intValue(Type.usize, param_vals.len)).toIntern(), | |
| 17853 | .len = (try pt.intValue(Type.usize, param_vals.len)).toIntern(), | |
| 17698 | 17854 | } }); |
| 17699 | 17855 | }; |
| 17700 | 17856 | |
| 17701 | const ret_ty_opt = try mod.intern(.{ .opt = .{ | |
| 17702 | .ty = try ip.get(gpa, .{ .opt_type = .type_type }), | |
| 17857 | const ret_ty_opt = try pt.intern(.{ .opt = .{ | |
| 17858 | .ty = try pt.intern(.{ .opt_type = .type_type }), | |
| 17703 | 17859 | .val = if (func_ty_info.return_type == .generic_poison_type) |
| 17704 | 17860 | .none |
| 17705 | 17861 | else |
| 17706 | 17862 | func_ty_info.return_type, |
| 17707 | 17863 | } }); |
| 17708 | 17864 | |
| 17709 | const callconv_ty = try mod.getBuiltinType("CallingConvention"); | |
| 17865 | const callconv_ty = try pt.getBuiltinType("CallingConvention"); | |
| 17710 | 17866 | |
| 17711 | 17867 | const field_values = .{ |
| 17712 | 17868 | // calling_convention: CallingConvention, |
| 17713 | (try mod.enumValueFieldIndex(callconv_ty, @intFromEnum(func_ty_info.cc))).toIntern(), | |
| 17869 | (try pt.enumValueFieldIndex(callconv_ty, @intFromEnum(func_ty_info.cc))).toIntern(), | |
| 17714 | 17870 | // is_generic: bool, |
| 17715 | 17871 | Value.makeBool(func_ty_info.is_generic).toIntern(), |
| 17716 | 17872 | // is_var_args: bool, |
| ... | ... | @@ -17720,10 +17876,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17720 | 17876 | // args: []const Fn.Param, |
| 17721 | 17877 | args_val, |
| 17722 | 17878 | }; |
| 17723 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 17879 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 17724 | 17880 | .ty = type_info_ty.toIntern(), |
| 17725 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Fn))).toIntern(), | |
| 17726 | .val = try mod.intern(.{ .aggregate = .{ | |
| 17881 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Fn))).toIntern(), | |
| 17882 | .val = try pt.intern(.{ .aggregate = .{ | |
| 17727 | 17883 | .ty = fn_info_ty.toIntern(), |
| 17728 | 17884 | .storage = .{ .elems = &field_values }, |
| 17729 | 17885 | } }), |
| ... | ... | @@ -17740,18 +17896,18 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17740 | 17896 | const int_info_decl = mod.declPtr(int_info_decl_index); |
| 17741 | 17897 | const int_info_ty = int_info_decl.val.toType(); |
| 17742 | 17898 | |
| 17743 | const signedness_ty = try mod.getBuiltinType("Signedness"); | |
| 17899 | const signedness_ty = try pt.getBuiltinType("Signedness"); | |
| 17744 | 17900 | const info = ty.intInfo(mod); |
| 17745 | 17901 | const field_values = .{ |
| 17746 | 17902 | // signedness: Signedness, |
| 17747 | (try mod.enumValueFieldIndex(signedness_ty, @intFromEnum(info.signedness))).toIntern(), | |
| 17903 | (try pt.enumValueFieldIndex(signedness_ty, @intFromEnum(info.signedness))).toIntern(), | |
| 17748 | 17904 | // bits: u16, |
| 17749 | (try mod.intValue(Type.u16, info.bits)).toIntern(), | |
| 17905 | (try pt.intValue(Type.u16, info.bits)).toIntern(), | |
| 17750 | 17906 | }; |
| 17751 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 17907 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 17752 | 17908 | .ty = type_info_ty.toIntern(), |
| 17753 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Int))).toIntern(), | |
| 17754 | .val = try mod.intern(.{ .aggregate = .{ | |
| 17909 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Int))).toIntern(), | |
| 17910 | .val = try pt.intern(.{ .aggregate = .{ | |
| 17755 | 17911 | .ty = int_info_ty.toIntern(), |
| 17756 | 17912 | .storage = .{ .elems = &field_values }, |
| 17757 | 17913 | } }), |
| ... | ... | @@ -17770,12 +17926,12 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17770 | 17926 | |
| 17771 | 17927 | const field_vals = .{ |
| 17772 | 17928 | // bits: u16, |
| 17773 | (try mod.intValue(Type.u16, ty.bitSize(mod))).toIntern(), | |
| 17929 | (try pt.intValue(Type.u16, ty.bitSize(pt))).toIntern(), | |
| 17774 | 17930 | }; |
| 17775 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 17931 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 17776 | 17932 | .ty = type_info_ty.toIntern(), |
| 17777 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Float))).toIntern(), | |
| 17778 | .val = try mod.intern(.{ .aggregate = .{ | |
| 17933 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Float))).toIntern(), | |
| 17934 | .val = try pt.intern(.{ .aggregate = .{ | |
| 17779 | 17935 | .ty = float_info_ty.toIntern(), |
| 17780 | 17936 | .storage = .{ .elems = &field_vals }, |
| 17781 | 17937 | } }), |
| ... | ... | @@ -17784,16 +17940,16 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17784 | 17940 | .Pointer => { |
| 17785 | 17941 | const info = ty.ptrInfo(mod); |
| 17786 | 17942 | const alignment = if (info.flags.alignment.toByteUnits()) |alignment| |
| 17787 | try mod.intValue(Type.comptime_int, alignment) | |
| 17943 | try pt.intValue(Type.comptime_int, alignment) | |
| 17788 | 17944 | else |
| 17789 | try Type.fromInterned(info.child).lazyAbiAlignment(mod); | |
| 17945 | try Type.fromInterned(info.child).lazyAbiAlignment(pt); | |
| 17790 | 17946 | |
| 17791 | const addrspace_ty = try mod.getBuiltinType("AddressSpace"); | |
| 17947 | const addrspace_ty = try pt.getBuiltinType("AddressSpace"); | |
| 17792 | 17948 | const pointer_ty = t: { |
| 17793 | 17949 | const decl_index = (try sema.namespaceLookup( |
| 17794 | 17950 | block, |
| 17795 | 17951 | src, |
| 17796 | (try mod.getBuiltinType("Type")).getNamespaceIndex(mod), | |
| 17952 | (try pt.getBuiltinType("Type")).getNamespaceIndex(mod), | |
| 17797 | 17953 | try ip.getOrPutString(gpa, "Pointer", .no_embedded_nulls), |
| 17798 | 17954 | )).?; |
| 17799 | 17955 | try sema.ensureDeclAnalyzed(decl_index); |
| ... | ... | @@ -17814,7 +17970,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17814 | 17970 | |
| 17815 | 17971 | const field_values = .{ |
| 17816 | 17972 | // size: Size, |
| 17817 | (try mod.enumValueFieldIndex(ptr_size_ty, @intFromEnum(info.flags.size))).toIntern(), | |
| 17973 | (try pt.enumValueFieldIndex(ptr_size_ty, @intFromEnum(info.flags.size))).toIntern(), | |
| 17818 | 17974 | // is_const: bool, |
| 17819 | 17975 | Value.makeBool(info.flags.is_const).toIntern(), |
| 17820 | 17976 | // is_volatile: bool, |
| ... | ... | @@ -17822,7 +17978,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17822 | 17978 | // alignment: comptime_int, |
| 17823 | 17979 | alignment.toIntern(), |
| 17824 | 17980 | // address_space: AddressSpace |
| 17825 | (try mod.enumValueFieldIndex(addrspace_ty, @intFromEnum(info.flags.address_space))).toIntern(), | |
| 17981 | (try pt.enumValueFieldIndex(addrspace_ty, @intFromEnum(info.flags.address_space))).toIntern(), | |
| 17826 | 17982 | // child: type, |
| 17827 | 17983 | info.child, |
| 17828 | 17984 | // is_allowzero: bool, |
| ... | ... | @@ -17833,10 +17989,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17833 | 17989 | else => Value.fromInterned(info.sentinel), |
| 17834 | 17990 | })).toIntern(), |
| 17835 | 17991 | }; |
| 17836 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 17992 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 17837 | 17993 | .ty = type_info_ty.toIntern(), |
| 17838 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Pointer))).toIntern(), | |
| 17839 | .val = try mod.intern(.{ .aggregate = .{ | |
| 17994 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Pointer))).toIntern(), | |
| 17995 | .val = try pt.intern(.{ .aggregate = .{ | |
| 17840 | 17996 | .ty = pointer_ty.toIntern(), |
| 17841 | 17997 | .storage = .{ .elems = &field_values }, |
| 17842 | 17998 | } }), |
| ... | ... | @@ -17858,16 +18014,16 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17858 | 18014 | const info = ty.arrayInfo(mod); |
| 17859 | 18015 | const field_values = .{ |
| 17860 | 18016 | // len: comptime_int, |
| 17861 | (try mod.intValue(Type.comptime_int, info.len)).toIntern(), | |
| 18017 | (try pt.intValue(Type.comptime_int, info.len)).toIntern(), | |
| 17862 | 18018 | // child: type, |
| 17863 | 18019 | info.elem_type.toIntern(), |
| 17864 | 18020 | // sentinel: ?*const anyopaque, |
| 17865 | 18021 | (try sema.optRefValue(info.sentinel)).toIntern(), |
| 17866 | 18022 | }; |
| 17867 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 18023 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 17868 | 18024 | .ty = type_info_ty.toIntern(), |
| 17869 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Array))).toIntern(), | |
| 17870 | .val = try mod.intern(.{ .aggregate = .{ | |
| 18025 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Array))).toIntern(), | |
| 18026 | .val = try pt.intern(.{ .aggregate = .{ | |
| 17871 | 18027 | .ty = array_field_ty.toIntern(), |
| 17872 | 18028 | .storage = .{ .elems = &field_values }, |
| 17873 | 18029 | } }), |
| ... | ... | @@ -17889,14 +18045,14 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17889 | 18045 | const info = ty.arrayInfo(mod); |
| 17890 | 18046 | const field_values = .{ |
| 17891 | 18047 | // len: comptime_int, |
| 17892 | (try mod.intValue(Type.comptime_int, info.len)).toIntern(), | |
| 18048 | (try pt.intValue(Type.comptime_int, info.len)).toIntern(), | |
| 17893 | 18049 | // child: type, |
| 17894 | 18050 | info.elem_type.toIntern(), |
| 17895 | 18051 | }; |
| 17896 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 18052 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 17897 | 18053 | .ty = type_info_ty.toIntern(), |
| 17898 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Vector))).toIntern(), | |
| 17899 | .val = try mod.intern(.{ .aggregate = .{ | |
| 18054 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Vector))).toIntern(), | |
| 18055 | .val = try pt.intern(.{ .aggregate = .{ | |
| 17900 | 18056 | .ty = vector_field_ty.toIntern(), |
| 17901 | 18057 | .storage = .{ .elems = &field_values }, |
| 17902 | 18058 | } }), |
| ... | ... | @@ -17919,10 +18075,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17919 | 18075 | // child: type, |
| 17920 | 18076 | ty.optionalChild(mod).toIntern(), |
| 17921 | 18077 | }; |
| 17922 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 18078 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 17923 | 18079 | .ty = type_info_ty.toIntern(), |
| 17924 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Optional))).toIntern(), | |
| 17925 | .val = try mod.intern(.{ .aggregate = .{ | |
| 18080 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Optional))).toIntern(), | |
| 18081 | .val = try pt.intern(.{ .aggregate = .{ | |
| 17926 | 18082 | .ty = optional_field_ty.toIntern(), |
| 17927 | 18083 | .storage = .{ .elems = &field_values }, |
| 17928 | 18084 | } }), |
| ... | ... | @@ -17954,18 +18110,18 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17954 | 18110 | const error_name = names.get(ip)[error_index]; |
| 17955 | 18111 | const error_name_len = error_name.length(ip); |
| 17956 | 18112 | const error_name_val = v: { |
| 17957 | const new_decl_ty = try mod.arrayType(.{ | |
| 18113 | const new_decl_ty = try pt.arrayType(.{ | |
| 17958 | 18114 | .len = error_name_len, |
| 17959 | 18115 | .sentinel = .zero_u8, |
| 17960 | 18116 | .child = .u8_type, |
| 17961 | 18117 | }); |
| 17962 | const new_decl_val = try mod.intern(.{ .aggregate = .{ | |
| 18118 | const new_decl_val = try pt.intern(.{ .aggregate = .{ | |
| 17963 | 18119 | .ty = new_decl_ty.toIntern(), |
| 17964 | 18120 | .storage = .{ .bytes = error_name.toString() }, |
| 17965 | 18121 | } }); |
| 17966 | break :v try mod.intern(.{ .slice = .{ | |
| 18122 | break :v try pt.intern(.{ .slice = .{ | |
| 17967 | 18123 | .ty = .slice_const_u8_sentinel_0_type, |
| 17968 | .ptr = try mod.intern(.{ .ptr = .{ | |
| 18124 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 17969 | 18125 | .ty = .manyptr_const_u8_sentinel_0_type, |
| 17970 | 18126 | .base_addr = .{ .anon_decl = .{ |
| 17971 | 18127 | .val = new_decl_val, |
| ... | ... | @@ -17973,7 +18129,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17973 | 18129 | } }, |
| 17974 | 18130 | .byte_offset = 0, |
| 17975 | 18131 | } }), |
| 17976 | .len = (try mod.intValue(Type.usize, error_name_len)).toIntern(), | |
| 18132 | .len = (try pt.intValue(Type.usize, error_name_len)).toIntern(), | |
| 17977 | 18133 | } }); |
| 17978 | 18134 | }; |
| 17979 | 18135 | |
| ... | ... | @@ -17981,7 +18137,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17981 | 18137 | // name: [:0]const u8, |
| 17982 | 18138 | error_name_val, |
| 17983 | 18139 | }; |
| 17984 | field_val.* = try mod.intern(.{ .aggregate = .{ | |
| 18140 | field_val.* = try pt.intern(.{ .aggregate = .{ | |
| 17985 | 18141 | .ty = error_field_ty.toIntern(), |
| 17986 | 18142 | .storage = .{ .elems = &error_field_fields }, |
| 17987 | 18143 | } }); |
| ... | ... | @@ -17992,27 +18148,27 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 17992 | 18148 | }; |
| 17993 | 18149 | |
| 17994 | 18150 | // Build our ?[]const Error value |
| 17995 | const slice_errors_ty = try mod.ptrTypeSema(.{ | |
| 18151 | const slice_errors_ty = try pt.ptrTypeSema(.{ | |
| 17996 | 18152 | .child = error_field_ty.toIntern(), |
| 17997 | 18153 | .flags = .{ |
| 17998 | 18154 | .size = .Slice, |
| 17999 | 18155 | .is_const = true, |
| 18000 | 18156 | }, |
| 18001 | 18157 | }); |
| 18002 | const opt_slice_errors_ty = try mod.optionalType(slice_errors_ty.toIntern()); | |
| 18158 | const opt_slice_errors_ty = try pt.optionalType(slice_errors_ty.toIntern()); | |
| 18003 | 18159 | const errors_payload_val: InternPool.Index = if (error_field_vals) |vals| v: { |
| 18004 | const array_errors_ty = try mod.arrayType(.{ | |
| 18160 | const array_errors_ty = try pt.arrayType(.{ | |
| 18005 | 18161 | .len = vals.len, |
| 18006 | 18162 | .child = error_field_ty.toIntern(), |
| 18007 | 18163 | }); |
| 18008 | const new_decl_val = try mod.intern(.{ .aggregate = .{ | |
| 18164 | const new_decl_val = try pt.intern(.{ .aggregate = .{ | |
| 18009 | 18165 | .ty = array_errors_ty.toIntern(), |
| 18010 | 18166 | .storage = .{ .elems = vals }, |
| 18011 | 18167 | } }); |
| 18012 | 18168 | const manyptr_errors_ty = slice_errors_ty.slicePtrFieldType(mod).toIntern(); |
| 18013 | break :v try mod.intern(.{ .slice = .{ | |
| 18169 | break :v try pt.intern(.{ .slice = .{ | |
| 18014 | 18170 | .ty = slice_errors_ty.toIntern(), |
| 18015 | .ptr = try mod.intern(.{ .ptr = .{ | |
| 18171 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 18016 | 18172 | .ty = manyptr_errors_ty, |
| 18017 | 18173 | .base_addr = .{ .anon_decl = .{ |
| 18018 | 18174 | .orig_ty = manyptr_errors_ty, |
| ... | ... | @@ -18020,18 +18176,18 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18020 | 18176 | } }, |
| 18021 | 18177 | .byte_offset = 0, |
| 18022 | 18178 | } }), |
| 18023 | .len = (try mod.intValue(Type.usize, vals.len)).toIntern(), | |
| 18179 | .len = (try pt.intValue(Type.usize, vals.len)).toIntern(), | |
| 18024 | 18180 | } }); |
| 18025 | 18181 | } else .none; |
| 18026 | const errors_val = try mod.intern(.{ .opt = .{ | |
| 18182 | const errors_val = try pt.intern(.{ .opt = .{ | |
| 18027 | 18183 | .ty = opt_slice_errors_ty.toIntern(), |
| 18028 | 18184 | .val = errors_payload_val, |
| 18029 | 18185 | } }); |
| 18030 | 18186 | |
| 18031 | 18187 | // Construct Type{ .ErrorSet = errors_val } |
| 18032 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 18188 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 18033 | 18189 | .ty = type_info_ty.toIntern(), |
| 18034 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.ErrorSet))).toIntern(), | |
| 18190 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.ErrorSet))).toIntern(), | |
| 18035 | 18191 | .val = errors_val, |
| 18036 | 18192 | } }))); |
| 18037 | 18193 | }, |
| ... | ... | @@ -18054,10 +18210,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18054 | 18210 | // payload: type, |
| 18055 | 18211 | ty.errorUnionPayload(mod).toIntern(), |
| 18056 | 18212 | }; |
| 18057 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 18213 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 18058 | 18214 | .ty = type_info_ty.toIntern(), |
| 18059 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.ErrorUnion))).toIntern(), | |
| 18060 | .val = try mod.intern(.{ .aggregate = .{ | |
| 18215 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.ErrorUnion))).toIntern(), | |
| 18216 | .val = try pt.intern(.{ .aggregate = .{ | |
| 18061 | 18217 | .ty = error_union_field_ty.toIntern(), |
| 18062 | 18218 | .storage = .{ .elems = &field_values }, |
| 18063 | 18219 | } }), |
| ... | ... | @@ -18082,30 +18238,31 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18082 | 18238 | for (enum_field_vals, 0..) |*field_val, tag_index| { |
| 18083 | 18239 | const enum_type = ip.loadEnumType(ty.toIntern()); |
| 18084 | 18240 | const value_val = if (enum_type.values.len > 0) |
| 18085 | try mod.intern_pool.getCoercedInts( | |
| 18241 | try ip.getCoercedInts( | |
| 18086 | 18242 | mod.gpa, |
| 18087 | mod.intern_pool.indexToKey(enum_type.values.get(ip)[tag_index]).int, | |
| 18243 | pt.tid, | |
| 18244 | ip.indexToKey(enum_type.values.get(ip)[tag_index]).int, | |
| 18088 | 18245 | .comptime_int_type, |
| 18089 | 18246 | ) |
| 18090 | 18247 | else |
| 18091 | (try mod.intValue(Type.comptime_int, tag_index)).toIntern(); | |
| 18248 | (try pt.intValue(Type.comptime_int, tag_index)).toIntern(); | |
| 18092 | 18249 | |
| 18093 | 18250 | // TODO: write something like getCoercedInts to avoid needing to dupe |
| 18094 | 18251 | const name_val = v: { |
| 18095 | 18252 | const tag_name = enum_type.names.get(ip)[tag_index]; |
| 18096 | 18253 | const tag_name_len = tag_name.length(ip); |
| 18097 | const new_decl_ty = try mod.arrayType(.{ | |
| 18254 | const new_decl_ty = try pt.arrayType(.{ | |
| 18098 | 18255 | .len = tag_name_len, |
| 18099 | 18256 | .sentinel = .zero_u8, |
| 18100 | 18257 | .child = .u8_type, |
| 18101 | 18258 | }); |
| 18102 | const new_decl_val = try mod.intern(.{ .aggregate = .{ | |
| 18259 | const new_decl_val = try pt.intern(.{ .aggregate = .{ | |
| 18103 | 18260 | .ty = new_decl_ty.toIntern(), |
| 18104 | 18261 | .storage = .{ .bytes = tag_name.toString() }, |
| 18105 | 18262 | } }); |
| 18106 | break :v try mod.intern(.{ .slice = .{ | |
| 18263 | break :v try pt.intern(.{ .slice = .{ | |
| 18107 | 18264 | .ty = .slice_const_u8_sentinel_0_type, |
| 18108 | .ptr = try mod.intern(.{ .ptr = .{ | |
| 18265 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 18109 | 18266 | .ty = .manyptr_const_u8_sentinel_0_type, |
| 18110 | 18267 | .base_addr = .{ .anon_decl = .{ |
| 18111 | 18268 | .val = new_decl_val, |
| ... | ... | @@ -18113,7 +18270,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18113 | 18270 | } }, |
| 18114 | 18271 | .byte_offset = 0, |
| 18115 | 18272 | } }), |
| 18116 | .len = (try mod.intValue(Type.usize, tag_name_len)).toIntern(), | |
| 18273 | .len = (try pt.intValue(Type.usize, tag_name_len)).toIntern(), | |
| 18117 | 18274 | } }); |
| 18118 | 18275 | }; |
| 18119 | 18276 | |
| ... | ... | @@ -18123,22 +18280,22 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18123 | 18280 | // value: comptime_int, |
| 18124 | 18281 | value_val, |
| 18125 | 18282 | }; |
| 18126 | field_val.* = try mod.intern(.{ .aggregate = .{ | |
| 18283 | field_val.* = try pt.intern(.{ .aggregate = .{ | |
| 18127 | 18284 | .ty = enum_field_ty.toIntern(), |
| 18128 | 18285 | .storage = .{ .elems = &enum_field_fields }, |
| 18129 | 18286 | } }); |
| 18130 | 18287 | } |
| 18131 | 18288 | |
| 18132 | 18289 | const fields_val = v: { |
| 18133 | const fields_array_ty = try mod.arrayType(.{ | |
| 18290 | const fields_array_ty = try pt.arrayType(.{ | |
| 18134 | 18291 | .len = enum_field_vals.len, |
| 18135 | 18292 | .child = enum_field_ty.toIntern(), |
| 18136 | 18293 | }); |
| 18137 | const new_decl_val = try mod.intern(.{ .aggregate = .{ | |
| 18294 | const new_decl_val = try pt.intern(.{ .aggregate = .{ | |
| 18138 | 18295 | .ty = fields_array_ty.toIntern(), |
| 18139 | 18296 | .storage = .{ .elems = enum_field_vals }, |
| 18140 | 18297 | } }); |
| 18141 | const slice_ty = (try mod.ptrTypeSema(.{ | |
| 18298 | const slice_ty = (try pt.ptrTypeSema(.{ | |
| 18142 | 18299 | .child = enum_field_ty.toIntern(), |
| 18143 | 18300 | .flags = .{ |
| 18144 | 18301 | .size = .Slice, |
| ... | ... | @@ -18146,9 +18303,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18146 | 18303 | }, |
| 18147 | 18304 | })).toIntern(); |
| 18148 | 18305 | const manyptr_ty = Type.fromInterned(slice_ty).slicePtrFieldType(mod).toIntern(); |
| 18149 | break :v try mod.intern(.{ .slice = .{ | |
| 18306 | break :v try pt.intern(.{ .slice = .{ | |
| 18150 | 18307 | .ty = slice_ty, |
| 18151 | .ptr = try mod.intern(.{ .ptr = .{ | |
| 18308 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 18152 | 18309 | .ty = manyptr_ty, |
| 18153 | 18310 | .base_addr = .{ .anon_decl = .{ |
| 18154 | 18311 | .val = new_decl_val, |
| ... | ... | @@ -18156,7 +18313,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18156 | 18313 | } }, |
| 18157 | 18314 | .byte_offset = 0, |
| 18158 | 18315 | } }), |
| 18159 | .len = (try mod.intValue(Type.usize, enum_field_vals.len)).toIntern(), | |
| 18316 | .len = (try pt.intValue(Type.usize, enum_field_vals.len)).toIntern(), | |
| 18160 | 18317 | } }); |
| 18161 | 18318 | }; |
| 18162 | 18319 | |
| ... | ... | @@ -18184,10 +18341,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18184 | 18341 | // is_exhaustive: bool, |
| 18185 | 18342 | is_exhaustive.toIntern(), |
| 18186 | 18343 | }; |
| 18187 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 18344 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 18188 | 18345 | .ty = type_info_ty.toIntern(), |
| 18189 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Enum))).toIntern(), | |
| 18190 | .val = try mod.intern(.{ .aggregate = .{ | |
| 18346 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Enum))).toIntern(), | |
| 18347 | .val = try pt.intern(.{ .aggregate = .{ | |
| 18191 | 18348 | .ty = type_enum_ty.toIntern(), |
| 18192 | 18349 | .storage = .{ .elems = &field_values }, |
| 18193 | 18350 | } }), |
| ... | ... | @@ -18218,7 +18375,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18218 | 18375 | break :t union_field_ty_decl.val.toType(); |
| 18219 | 18376 | }; |
| 18220 | 18377 | |
| 18221 | try ty.resolveLayout(mod); // Getting alignment requires type layout | |
| 18378 | try ty.resolveLayout(pt); // Getting alignment requires type layout | |
| 18222 | 18379 | const union_obj = mod.typeToUnion(ty).?; |
| 18223 | 18380 | const tag_type = union_obj.loadTagType(ip); |
| 18224 | 18381 | const layout = union_obj.getLayout(ip); |
| ... | ... | @@ -18230,18 +18387,18 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18230 | 18387 | const name_val = v: { |
| 18231 | 18388 | const field_name = tag_type.names.get(ip)[field_index]; |
| 18232 | 18389 | const field_name_len = field_name.length(ip); |
| 18233 | const new_decl_ty = try mod.arrayType(.{ | |
| 18390 | const new_decl_ty = try pt.arrayType(.{ | |
| 18234 | 18391 | .len = field_name_len, |
| 18235 | 18392 | .sentinel = .zero_u8, |
| 18236 | 18393 | .child = .u8_type, |
| 18237 | 18394 | }); |
| 18238 | const new_decl_val = try mod.intern(.{ .aggregate = .{ | |
| 18395 | const new_decl_val = try pt.intern(.{ .aggregate = .{ | |
| 18239 | 18396 | .ty = new_decl_ty.toIntern(), |
| 18240 | 18397 | .storage = .{ .bytes = field_name.toString() }, |
| 18241 | 18398 | } }); |
| 18242 | break :v try mod.intern(.{ .slice = .{ | |
| 18399 | break :v try pt.intern(.{ .slice = .{ | |
| 18243 | 18400 | .ty = .slice_const_u8_sentinel_0_type, |
| 18244 | .ptr = try mod.intern(.{ .ptr = .{ | |
| 18401 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 18245 | 18402 | .ty = .manyptr_const_u8_sentinel_0_type, |
| 18246 | 18403 | .base_addr = .{ .anon_decl = .{ |
| 18247 | 18404 | .val = new_decl_val, |
| ... | ... | @@ -18249,12 +18406,12 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18249 | 18406 | } }, |
| 18250 | 18407 | .byte_offset = 0, |
| 18251 | 18408 | } }), |
| 18252 | .len = (try mod.intValue(Type.usize, field_name_len)).toIntern(), | |
| 18409 | .len = (try pt.intValue(Type.usize, field_name_len)).toIntern(), | |
| 18253 | 18410 | } }); |
| 18254 | 18411 | }; |
| 18255 | 18412 | |
| 18256 | 18413 | const alignment = switch (layout) { |
| 18257 | .auto, .@"extern" => try mod.unionFieldNormalAlignmentAdvanced(union_obj, @intCast(field_index), .sema), | |
| 18414 | .auto, .@"extern" => try pt.unionFieldNormalAlignmentAdvanced(union_obj, @intCast(field_index), .sema), | |
| 18258 | 18415 | .@"packed" => .none, |
| 18259 | 18416 | }; |
| 18260 | 18417 | |
| ... | ... | @@ -18265,24 +18422,24 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18265 | 18422 | // type: type, |
| 18266 | 18423 | field_ty, |
| 18267 | 18424 | // alignment: comptime_int, |
| 18268 | (try mod.intValue(Type.comptime_int, alignment.toByteUnits() orelse 0)).toIntern(), | |
| 18425 | (try pt.intValue(Type.comptime_int, alignment.toByteUnits() orelse 0)).toIntern(), | |
| 18269 | 18426 | }; |
| 18270 | field_val.* = try mod.intern(.{ .aggregate = .{ | |
| 18427 | field_val.* = try pt.intern(.{ .aggregate = .{ | |
| 18271 | 18428 | .ty = union_field_ty.toIntern(), |
| 18272 | 18429 | .storage = .{ .elems = &union_field_fields }, |
| 18273 | 18430 | } }); |
| 18274 | 18431 | } |
| 18275 | 18432 | |
| 18276 | 18433 | const fields_val = v: { |
| 18277 | const array_fields_ty = try mod.arrayType(.{ | |
| 18434 | const array_fields_ty = try pt.arrayType(.{ | |
| 18278 | 18435 | .len = union_field_vals.len, |
| 18279 | 18436 | .child = union_field_ty.toIntern(), |
| 18280 | 18437 | }); |
| 18281 | const new_decl_val = try mod.intern(.{ .aggregate = .{ | |
| 18438 | const new_decl_val = try pt.intern(.{ .aggregate = .{ | |
| 18282 | 18439 | .ty = array_fields_ty.toIntern(), |
| 18283 | 18440 | .storage = .{ .elems = union_field_vals }, |
| 18284 | 18441 | } }); |
| 18285 | const slice_ty = (try mod.ptrTypeSema(.{ | |
| 18442 | const slice_ty = (try pt.ptrTypeSema(.{ | |
| 18286 | 18443 | .child = union_field_ty.toIntern(), |
| 18287 | 18444 | .flags = .{ |
| 18288 | 18445 | .size = .Slice, |
| ... | ... | @@ -18290,9 +18447,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18290 | 18447 | }, |
| 18291 | 18448 | })).toIntern(); |
| 18292 | 18449 | const manyptr_ty = Type.fromInterned(slice_ty).slicePtrFieldType(mod).toIntern(); |
| 18293 | break :v try mod.intern(.{ .slice = .{ | |
| 18450 | break :v try pt.intern(.{ .slice = .{ | |
| 18294 | 18451 | .ty = slice_ty, |
| 18295 | .ptr = try mod.intern(.{ .ptr = .{ | |
| 18452 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 18296 | 18453 | .ty = manyptr_ty, |
| 18297 | 18454 | .base_addr = .{ .anon_decl = .{ |
| 18298 | 18455 | .orig_ty = manyptr_ty, |
| ... | ... | @@ -18300,14 +18457,14 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18300 | 18457 | } }, |
| 18301 | 18458 | .byte_offset = 0, |
| 18302 | 18459 | } }), |
| 18303 | .len = (try mod.intValue(Type.usize, union_field_vals.len)).toIntern(), | |
| 18460 | .len = (try pt.intValue(Type.usize, union_field_vals.len)).toIntern(), | |
| 18304 | 18461 | } }); |
| 18305 | 18462 | }; |
| 18306 | 18463 | |
| 18307 | 18464 | const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ty.getNamespaceIndex(mod)); |
| 18308 | 18465 | |
| 18309 | const enum_tag_ty_val = try mod.intern(.{ .opt = .{ | |
| 18310 | .ty = (try mod.optionalType(.type_type)).toIntern(), | |
| 18466 | const enum_tag_ty_val = try pt.intern(.{ .opt = .{ | |
| 18467 | .ty = (try pt.optionalType(.type_type)).toIntern(), | |
| 18311 | 18468 | .val = if (ty.unionTagType(mod)) |tag_ty| tag_ty.toIntern() else .none, |
| 18312 | 18469 | } }); |
| 18313 | 18470 | |
| ... | ... | @@ -18315,7 +18472,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18315 | 18472 | const decl_index = (try sema.namespaceLookup( |
| 18316 | 18473 | block, |
| 18317 | 18474 | src, |
| 18318 | (try mod.getBuiltinType("Type")).getNamespaceIndex(mod), | |
| 18475 | (try pt.getBuiltinType("Type")).getNamespaceIndex(mod), | |
| 18319 | 18476 | try ip.getOrPutString(gpa, "ContainerLayout", .no_embedded_nulls), |
| 18320 | 18477 | )).?; |
| 18321 | 18478 | try sema.ensureDeclAnalyzed(decl_index); |
| ... | ... | @@ -18325,7 +18482,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18325 | 18482 | |
| 18326 | 18483 | const field_values = .{ |
| 18327 | 18484 | // layout: ContainerLayout, |
| 18328 | (try mod.enumValueFieldIndex(container_layout_ty, @intFromEnum(layout))).toIntern(), | |
| 18485 | (try pt.enumValueFieldIndex(container_layout_ty, @intFromEnum(layout))).toIntern(), | |
| 18329 | 18486 | |
| 18330 | 18487 | // tag_type: ?type, |
| 18331 | 18488 | enum_tag_ty_val, |
| ... | ... | @@ -18334,10 +18491,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18334 | 18491 | // decls: []const Declaration, |
| 18335 | 18492 | decls_val, |
| 18336 | 18493 | }; |
| 18337 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 18494 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 18338 | 18495 | .ty = type_info_ty.toIntern(), |
| 18339 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Union))).toIntern(), | |
| 18340 | .val = try mod.intern(.{ .aggregate = .{ | |
| 18496 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Union))).toIntern(), | |
| 18497 | .val = try pt.intern(.{ .aggregate = .{ | |
| 18341 | 18498 | .ty = type_union_ty.toIntern(), |
| 18342 | 18499 | .storage = .{ .elems = &field_values }, |
| 18343 | 18500 | } }), |
| ... | ... | @@ -18368,7 +18525,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18368 | 18525 | break :t struct_field_ty_decl.val.toType(); |
| 18369 | 18526 | }; |
| 18370 | 18527 | |
| 18371 | try ty.resolveLayout(mod); // Getting alignment requires type layout | |
| 18528 | try ty.resolveLayout(pt); // Getting alignment requires type layout | |
| 18372 | 18529 | |
| 18373 | 18530 | var struct_field_vals: []InternPool.Index = &.{}; |
| 18374 | 18531 | defer gpa.free(struct_field_vals); |
| ... | ... | @@ -18385,18 +18542,18 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18385 | 18542 | else |
| 18386 | 18543 | try ip.getOrPutStringFmt(gpa, "{d}", .{field_index}, .no_embedded_nulls); |
| 18387 | 18544 | const field_name_len = field_name.length(ip); |
| 18388 | const new_decl_ty = try mod.arrayType(.{ | |
| 18545 | const new_decl_ty = try pt.arrayType(.{ | |
| 18389 | 18546 | .len = field_name_len, |
| 18390 | 18547 | .sentinel = .zero_u8, |
| 18391 | 18548 | .child = .u8_type, |
| 18392 | 18549 | }); |
| 18393 | const new_decl_val = try mod.intern(.{ .aggregate = .{ | |
| 18550 | const new_decl_val = try pt.intern(.{ .aggregate = .{ | |
| 18394 | 18551 | .ty = new_decl_ty.toIntern(), |
| 18395 | 18552 | .storage = .{ .bytes = field_name.toString() }, |
| 18396 | 18553 | } }); |
| 18397 | break :v try mod.intern(.{ .slice = .{ | |
| 18554 | break :v try pt.intern(.{ .slice = .{ | |
| 18398 | 18555 | .ty = .slice_const_u8_sentinel_0_type, |
| 18399 | .ptr = try mod.intern(.{ .ptr = .{ | |
| 18556 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 18400 | 18557 | .ty = .manyptr_const_u8_sentinel_0_type, |
| 18401 | 18558 | .base_addr = .{ .anon_decl = .{ |
| 18402 | 18559 | .val = new_decl_val, |
| ... | ... | @@ -18404,11 +18561,11 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18404 | 18561 | } }, |
| 18405 | 18562 | .byte_offset = 0, |
| 18406 | 18563 | } }), |
| 18407 | .len = (try mod.intValue(Type.usize, field_name_len)).toIntern(), | |
| 18564 | .len = (try pt.intValue(Type.usize, field_name_len)).toIntern(), | |
| 18408 | 18565 | } }); |
| 18409 | 18566 | }; |
| 18410 | 18567 | |
| 18411 | try Type.fromInterned(field_ty).resolveLayout(mod); | |
| 18568 | try Type.fromInterned(field_ty).resolveLayout(pt); | |
| 18412 | 18569 | |
| 18413 | 18570 | const is_comptime = field_val != .none; |
| 18414 | 18571 | const opt_default_val = if (is_comptime) Value.fromInterned(field_val) else null; |
| ... | ... | @@ -18423,9 +18580,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18423 | 18580 | // is_comptime: bool, |
| 18424 | 18581 | Value.makeBool(is_comptime).toIntern(), |
| 18425 | 18582 | // alignment: comptime_int, |
| 18426 | (try mod.intValue(Type.comptime_int, Type.fromInterned(field_ty).abiAlignment(mod).toByteUnits() orelse 0)).toIntern(), | |
| 18583 | (try pt.intValue(Type.comptime_int, Type.fromInterned(field_ty).abiAlignment(pt).toByteUnits() orelse 0)).toIntern(), | |
| 18427 | 18584 | }; |
| 18428 | struct_field_val.* = try mod.intern(.{ .aggregate = .{ | |
| 18585 | struct_field_val.* = try pt.intern(.{ .aggregate = .{ | |
| 18429 | 18586 | .ty = struct_field_ty.toIntern(), |
| 18430 | 18587 | .storage = .{ .elems = &struct_field_fields }, |
| 18431 | 18588 | } }); |
| ... | ... | @@ -18437,7 +18594,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18437 | 18594 | }; |
| 18438 | 18595 | struct_field_vals = try gpa.alloc(InternPool.Index, struct_type.field_types.len); |
| 18439 | 18596 | |
| 18440 | try ty.resolveStructFieldInits(mod); | |
| 18597 | try ty.resolveStructFieldInits(pt); | |
| 18441 | 18598 | |
| 18442 | 18599 | for (struct_field_vals, 0..) |*field_val, field_index| { |
| 18443 | 18600 | const field_name = if (struct_type.fieldName(ip, field_index).unwrap()) |field_name| |
| ... | ... | @@ -18449,18 +18606,18 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18449 | 18606 | const field_init = struct_type.fieldInit(ip, field_index); |
| 18450 | 18607 | const field_is_comptime = struct_type.fieldIsComptime(ip, field_index); |
| 18451 | 18608 | const name_val = v: { |
| 18452 | const new_decl_ty = try mod.arrayType(.{ | |
| 18609 | const new_decl_ty = try pt.arrayType(.{ | |
| 18453 | 18610 | .len = field_name_len, |
| 18454 | 18611 | .sentinel = .zero_u8, |
| 18455 | 18612 | .child = .u8_type, |
| 18456 | 18613 | }); |
| 18457 | const new_decl_val = try mod.intern(.{ .aggregate = .{ | |
| 18614 | const new_decl_val = try pt.intern(.{ .aggregate = .{ | |
| 18458 | 18615 | .ty = new_decl_ty.toIntern(), |
| 18459 | 18616 | .storage = .{ .bytes = field_name.toString() }, |
| 18460 | 18617 | } }); |
| 18461 | break :v try mod.intern(.{ .slice = .{ | |
| 18618 | break :v try pt.intern(.{ .slice = .{ | |
| 18462 | 18619 | .ty = .slice_const_u8_sentinel_0_type, |
| 18463 | .ptr = try mod.intern(.{ .ptr = .{ | |
| 18620 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 18464 | 18621 | .ty = .manyptr_const_u8_sentinel_0_type, |
| 18465 | 18622 | .base_addr = .{ .anon_decl = .{ |
| 18466 | 18623 | .val = new_decl_val, |
| ... | ... | @@ -18468,7 +18625,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18468 | 18625 | } }, |
| 18469 | 18626 | .byte_offset = 0, |
| 18470 | 18627 | } }), |
| 18471 | .len = (try mod.intValue(Type.usize, field_name_len)).toIntern(), | |
| 18628 | .len = (try pt.intValue(Type.usize, field_name_len)).toIntern(), | |
| 18472 | 18629 | } }); |
| 18473 | 18630 | }; |
| 18474 | 18631 | |
| ... | ... | @@ -18476,7 +18633,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18476 | 18633 | const default_val_ptr = try sema.optRefValue(opt_default_val); |
| 18477 | 18634 | const alignment = switch (struct_type.layout) { |
| 18478 | 18635 | .@"packed" => .none, |
| 18479 | else => try mod.structFieldAlignmentAdvanced( | |
| 18636 | else => try pt.structFieldAlignmentAdvanced( | |
| 18480 | 18637 | struct_type.fieldAlign(ip, field_index), |
| 18481 | 18638 | field_ty, |
| 18482 | 18639 | struct_type.layout, |
| ... | ... | @@ -18494,9 +18651,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18494 | 18651 | // is_comptime: bool, |
| 18495 | 18652 | Value.makeBool(field_is_comptime).toIntern(), |
| 18496 | 18653 | // alignment: comptime_int, |
| 18497 | (try mod.intValue(Type.comptime_int, alignment.toByteUnits() orelse 0)).toIntern(), | |
| 18654 | (try pt.intValue(Type.comptime_int, alignment.toByteUnits() orelse 0)).toIntern(), | |
| 18498 | 18655 | }; |
| 18499 | field_val.* = try mod.intern(.{ .aggregate = .{ | |
| 18656 | field_val.* = try pt.intern(.{ .aggregate = .{ | |
| 18500 | 18657 | .ty = struct_field_ty.toIntern(), |
| 18501 | 18658 | .storage = .{ .elems = &struct_field_fields }, |
| 18502 | 18659 | } }); |
| ... | ... | @@ -18504,15 +18661,15 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18504 | 18661 | } |
| 18505 | 18662 | |
| 18506 | 18663 | const fields_val = v: { |
| 18507 | const array_fields_ty = try mod.arrayType(.{ | |
| 18664 | const array_fields_ty = try pt.arrayType(.{ | |
| 18508 | 18665 | .len = struct_field_vals.len, |
| 18509 | 18666 | .child = struct_field_ty.toIntern(), |
| 18510 | 18667 | }); |
| 18511 | const new_decl_val = try mod.intern(.{ .aggregate = .{ | |
| 18668 | const new_decl_val = try pt.intern(.{ .aggregate = .{ | |
| 18512 | 18669 | .ty = array_fields_ty.toIntern(), |
| 18513 | 18670 | .storage = .{ .elems = struct_field_vals }, |
| 18514 | 18671 | } }); |
| 18515 | const slice_ty = (try mod.ptrTypeSema(.{ | |
| 18672 | const slice_ty = (try pt.ptrTypeSema(.{ | |
| 18516 | 18673 | .child = struct_field_ty.toIntern(), |
| 18517 | 18674 | .flags = .{ |
| 18518 | 18675 | .size = .Slice, |
| ... | ... | @@ -18520,9 +18677,9 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18520 | 18677 | }, |
| 18521 | 18678 | })).toIntern(); |
| 18522 | 18679 | const manyptr_ty = Type.fromInterned(slice_ty).slicePtrFieldType(mod).toIntern(); |
| 18523 | break :v try mod.intern(.{ .slice = .{ | |
| 18680 | break :v try pt.intern(.{ .slice = .{ | |
| 18524 | 18681 | .ty = slice_ty, |
| 18525 | .ptr = try mod.intern(.{ .ptr = .{ | |
| 18682 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 18526 | 18683 | .ty = manyptr_ty, |
| 18527 | 18684 | .base_addr = .{ .anon_decl = .{ |
| 18528 | 18685 | .orig_ty = manyptr_ty, |
| ... | ... | @@ -18530,14 +18687,14 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18530 | 18687 | } }, |
| 18531 | 18688 | .byte_offset = 0, |
| 18532 | 18689 | } }), |
| 18533 | .len = (try mod.intValue(Type.usize, struct_field_vals.len)).toIntern(), | |
| 18690 | .len = (try pt.intValue(Type.usize, struct_field_vals.len)).toIntern(), | |
| 18534 | 18691 | } }); |
| 18535 | 18692 | }; |
| 18536 | 18693 | |
| 18537 | 18694 | const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ty.getNamespaceIndex(mod)); |
| 18538 | 18695 | |
| 18539 | const backing_integer_val = try mod.intern(.{ .opt = .{ | |
| 18540 | .ty = (try mod.optionalType(.type_type)).toIntern(), | |
| 18696 | const backing_integer_val = try pt.intern(.{ .opt = .{ | |
| 18697 | .ty = (try pt.optionalType(.type_type)).toIntern(), | |
| 18541 | 18698 | .val = if (mod.typeToPackedStruct(ty)) |packed_struct| val: { |
| 18542 | 18699 | assert(Type.fromInterned(packed_struct.backingIntType(ip).*).isInt(mod)); |
| 18543 | 18700 | break :val packed_struct.backingIntType(ip).*; |
| ... | ... | @@ -18548,7 +18705,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18548 | 18705 | const decl_index = (try sema.namespaceLookup( |
| 18549 | 18706 | block, |
| 18550 | 18707 | src, |
| 18551 | (try mod.getBuiltinType("Type")).getNamespaceIndex(mod), | |
| 18708 | (try pt.getBuiltinType("Type")).getNamespaceIndex(mod), | |
| 18552 | 18709 | try ip.getOrPutString(gpa, "ContainerLayout", .no_embedded_nulls), |
| 18553 | 18710 | )).?; |
| 18554 | 18711 | try sema.ensureDeclAnalyzed(decl_index); |
| ... | ... | @@ -18560,7 +18717,7 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18560 | 18717 | |
| 18561 | 18718 | const field_values = [_]InternPool.Index{ |
| 18562 | 18719 | // layout: ContainerLayout, |
| 18563 | (try mod.enumValueFieldIndex(container_layout_ty, @intFromEnum(layout))).toIntern(), | |
| 18720 | (try pt.enumValueFieldIndex(container_layout_ty, @intFromEnum(layout))).toIntern(), | |
| 18564 | 18721 | // backing_integer: ?type, |
| 18565 | 18722 | backing_integer_val, |
| 18566 | 18723 | // fields: []const StructField, |
| ... | ... | @@ -18570,10 +18727,10 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18570 | 18727 | // is_tuple: bool, |
| 18571 | 18728 | Value.makeBool(ty.isTuple(mod)).toIntern(), |
| 18572 | 18729 | }; |
| 18573 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 18730 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 18574 | 18731 | .ty = type_info_ty.toIntern(), |
| 18575 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Struct))).toIntern(), | |
| 18576 | .val = try mod.intern(.{ .aggregate = .{ | |
| 18732 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Struct))).toIntern(), | |
| 18733 | .val = try pt.intern(.{ .aggregate = .{ | |
| 18577 | 18734 | .ty = type_struct_ty.toIntern(), |
| 18578 | 18735 | .storage = .{ .elems = &field_values }, |
| 18579 | 18736 | } }), |
| ... | ... | @@ -18592,17 +18749,17 @@ fn zirTypeInfo(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 18592 | 18749 | break :t type_opaque_ty_decl.val.toType(); |
| 18593 | 18750 | }; |
| 18594 | 18751 | |
| 18595 | try ty.resolveFields(mod); | |
| 18752 | try ty.resolveFields(pt); | |
| 18596 | 18753 | const decls_val = try sema.typeInfoDecls(block, src, type_info_ty, ty.getNamespaceIndex(mod)); |
| 18597 | 18754 | |
| 18598 | 18755 | const field_values = .{ |
| 18599 | 18756 | // decls: []const Declaration, |
| 18600 | 18757 | decls_val, |
| 18601 | 18758 | }; |
| 18602 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 18759 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 18603 | 18760 | .ty = type_info_ty.toIntern(), |
| 18604 | .tag = (try mod.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Opaque))).toIntern(), | |
| 18605 | .val = try mod.intern(.{ .aggregate = .{ | |
| 18761 | .tag = (try pt.enumValueFieldIndex(type_info_tag_ty, @intFromEnum(std.builtin.TypeId.Opaque))).toIntern(), | |
| 18762 | .val = try pt.intern(.{ .aggregate = .{ | |
| 18606 | 18763 | .ty = type_opaque_ty.toIntern(), |
| 18607 | 18764 | .storage = .{ .elems = &field_values }, |
| 18608 | 18765 | } }), |
| ... | ... | @@ -18620,7 +18777,8 @@ fn typeInfoDecls( |
| 18620 | 18777 | type_info_ty: Type, |
| 18621 | 18778 | opt_namespace: InternPool.OptionalNamespaceIndex, |
| 18622 | 18779 | ) CompileError!InternPool.Index { |
| 18623 | const mod = sema.mod; | |
| 18780 | const pt = sema.pt; | |
| 18781 | const mod = pt.zcu; | |
| 18624 | 18782 | const gpa = sema.gpa; |
| 18625 | 18783 | |
| 18626 | 18784 | const declaration_ty = t: { |
| ... | ... | @@ -18643,15 +18801,15 @@ fn typeInfoDecls( |
| 18643 | 18801 | |
| 18644 | 18802 | try sema.typeInfoNamespaceDecls(block, opt_namespace, declaration_ty, &decl_vals, &seen_namespaces); |
| 18645 | 18803 | |
| 18646 | const array_decl_ty = try mod.arrayType(.{ | |
| 18804 | const array_decl_ty = try pt.arrayType(.{ | |
| 18647 | 18805 | .len = decl_vals.items.len, |
| 18648 | 18806 | .child = declaration_ty.toIntern(), |
| 18649 | 18807 | }); |
| 18650 | const new_decl_val = try mod.intern(.{ .aggregate = .{ | |
| 18808 | const new_decl_val = try pt.intern(.{ .aggregate = .{ | |
| 18651 | 18809 | .ty = array_decl_ty.toIntern(), |
| 18652 | 18810 | .storage = .{ .elems = decl_vals.items }, |
| 18653 | 18811 | } }); |
| 18654 | const slice_ty = (try mod.ptrTypeSema(.{ | |
| 18812 | const slice_ty = (try pt.ptrTypeSema(.{ | |
| 18655 | 18813 | .child = declaration_ty.toIntern(), |
| 18656 | 18814 | .flags = .{ |
| 18657 | 18815 | .size = .Slice, |
| ... | ... | @@ -18659,9 +18817,9 @@ fn typeInfoDecls( |
| 18659 | 18817 | }, |
| 18660 | 18818 | })).toIntern(); |
| 18661 | 18819 | const manyptr_ty = Type.fromInterned(slice_ty).slicePtrFieldType(mod).toIntern(); |
| 18662 | return try mod.intern(.{ .slice = .{ | |
| 18820 | return try pt.intern(.{ .slice = .{ | |
| 18663 | 18821 | .ty = slice_ty, |
| 18664 | .ptr = try mod.intern(.{ .ptr = .{ | |
| 18822 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 18665 | 18823 | .ty = manyptr_ty, |
| 18666 | 18824 | .base_addr = .{ .anon_decl = .{ |
| 18667 | 18825 | .orig_ty = manyptr_ty, |
| ... | ... | @@ -18669,7 +18827,7 @@ fn typeInfoDecls( |
| 18669 | 18827 | } }, |
| 18670 | 18828 | .byte_offset = 0, |
| 18671 | 18829 | } }), |
| 18672 | .len = (try mod.intValue(Type.usize, decl_vals.items.len)).toIntern(), | |
| 18830 | .len = (try pt.intValue(Type.usize, decl_vals.items.len)).toIntern(), | |
| 18673 | 18831 | } }); |
| 18674 | 18832 | } |
| 18675 | 18833 | |
| ... | ... | @@ -18681,7 +18839,8 @@ fn typeInfoNamespaceDecls( |
| 18681 | 18839 | decl_vals: *std.ArrayList(InternPool.Index), |
| 18682 | 18840 | seen_namespaces: *std.AutoHashMap(*Namespace, void), |
| 18683 | 18841 | ) !void { |
| 18684 | const mod = sema.mod; | |
| 18842 | const pt = sema.pt; | |
| 18843 | const mod = pt.zcu; | |
| 18685 | 18844 | const ip = &mod.intern_pool; |
| 18686 | 18845 | |
| 18687 | 18846 | const namespace_index = opt_namespace_index.unwrap() orelse return; |
| ... | ... | @@ -18703,18 +18862,18 @@ fn typeInfoNamespaceDecls( |
| 18703 | 18862 | if (decl.kind != .named) continue; |
| 18704 | 18863 | const name_val = v: { |
| 18705 | 18864 | const decl_name_len = decl.name.length(ip); |
| 18706 | const new_decl_ty = try mod.arrayType(.{ | |
| 18865 | const new_decl_ty = try pt.arrayType(.{ | |
| 18707 | 18866 | .len = decl_name_len, |
| 18708 | 18867 | .sentinel = .zero_u8, |
| 18709 | 18868 | .child = .u8_type, |
| 18710 | 18869 | }); |
| 18711 | const new_decl_val = try mod.intern(.{ .aggregate = .{ | |
| 18870 | const new_decl_val = try pt.intern(.{ .aggregate = .{ | |
| 18712 | 18871 | .ty = new_decl_ty.toIntern(), |
| 18713 | 18872 | .storage = .{ .bytes = decl.name.toString() }, |
| 18714 | 18873 | } }); |
| 18715 | break :v try mod.intern(.{ .slice = .{ | |
| 18874 | break :v try pt.intern(.{ .slice = .{ | |
| 18716 | 18875 | .ty = .slice_const_u8_sentinel_0_type, |
| 18717 | .ptr = try mod.intern(.{ .ptr = .{ | |
| 18876 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 18718 | 18877 | .ty = .manyptr_const_u8_sentinel_0_type, |
| 18719 | 18878 | .base_addr = .{ .anon_decl = .{ |
| 18720 | 18879 | .orig_ty = .slice_const_u8_sentinel_0_type, |
| ... | ... | @@ -18722,7 +18881,7 @@ fn typeInfoNamespaceDecls( |
| 18722 | 18881 | } }, |
| 18723 | 18882 | .byte_offset = 0, |
| 18724 | 18883 | } }), |
| 18725 | .len = (try mod.intValue(Type.usize, decl_name_len)).toIntern(), | |
| 18884 | .len = (try pt.intValue(Type.usize, decl_name_len)).toIntern(), | |
| 18726 | 18885 | } }); |
| 18727 | 18886 | }; |
| 18728 | 18887 | |
| ... | ... | @@ -18730,7 +18889,7 @@ fn typeInfoNamespaceDecls( |
| 18730 | 18889 | //name: [:0]const u8, |
| 18731 | 18890 | name_val, |
| 18732 | 18891 | }; |
| 18733 | try decl_vals.append(try mod.intern(.{ .aggregate = .{ | |
| 18892 | try decl_vals.append(try pt.intern(.{ .aggregate = .{ | |
| 18734 | 18893 | .ty = declaration_ty.toIntern(), |
| 18735 | 18894 | .storage = .{ .elems = &fields }, |
| 18736 | 18895 | } })); |
| ... | ... | @@ -18782,11 +18941,12 @@ fn zirTypeofLog2IntType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil |
| 18782 | 18941 | } |
| 18783 | 18942 | |
| 18784 | 18943 | fn log2IntType(sema: *Sema, block: *Block, operand: Type, src: LazySrcLoc) CompileError!Type { |
| 18785 | const mod = sema.mod; | |
| 18944 | const pt = sema.pt; | |
| 18945 | const mod = pt.zcu; | |
| 18786 | 18946 | switch (operand.zigTypeTag(mod)) { |
| 18787 | 18947 | .ComptimeInt => return Type.comptime_int, |
| 18788 | 18948 | .Int => { |
| 18789 | const bits = operand.bitSize(mod); | |
| 18949 | const bits = operand.bitSize(pt); | |
| 18790 | 18950 | const count = if (bits == 0) |
| 18791 | 18951 | 0 |
| 18792 | 18952 | else blk: { |
| ... | ... | @@ -18797,12 +18957,12 @@ fn log2IntType(sema: *Sema, block: *Block, operand: Type, src: LazySrcLoc) Compi |
| 18797 | 18957 | } |
| 18798 | 18958 | break :blk count; |
| 18799 | 18959 | }; |
| 18800 | return mod.intType(.unsigned, count); | |
| 18960 | return pt.intType(.unsigned, count); | |
| 18801 | 18961 | }, |
| 18802 | 18962 | .Vector => { |
| 18803 | 18963 | const elem_ty = operand.elemType2(mod); |
| 18804 | 18964 | const log2_elem_ty = try sema.log2IntType(block, elem_ty, src); |
| 18805 | return mod.vectorType(.{ | |
| 18965 | return pt.vectorType(.{ | |
| 18806 | 18966 | .len = operand.vectorLen(mod), |
| 18807 | 18967 | .child = log2_elem_ty.toIntern(), |
| 18808 | 18968 | }); |
| ... | ... | @@ -18813,7 +18973,7 @@ fn log2IntType(sema: *Sema, block: *Block, operand: Type, src: LazySrcLoc) Compi |
| 18813 | 18973 | block, |
| 18814 | 18974 | src, |
| 18815 | 18975 | "bit shifting operation expected integer type, found '{}'", |
| 18816 | .{operand.fmt(mod)}, | |
| 18976 | .{operand.fmt(pt)}, | |
| 18817 | 18977 | ); |
| 18818 | 18978 | } |
| 18819 | 18979 | |
| ... | ... | @@ -18865,7 +19025,8 @@ fn zirBoolNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 18865 | 19025 | const tracy = trace(@src()); |
| 18866 | 19026 | defer tracy.end(); |
| 18867 | 19027 | |
| 18868 | const mod = sema.mod; | |
| 19028 | const pt = sema.pt; | |
| 19029 | const mod = pt.zcu; | |
| 18869 | 19030 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 18870 | 19031 | const src = block.nodeOffset(inst_data.src_node); |
| 18871 | 19032 | const operand_src = block.src(.{ .node_offset_un_op = inst_data.src_node }); |
| ... | ... | @@ -18874,7 +19035,7 @@ fn zirBoolNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 18874 | 19035 | const operand = try sema.coerce(block, Type.bool, uncasted_operand, operand_src); |
| 18875 | 19036 | if (try sema.resolveValue(operand)) |val| { |
| 18876 | 19037 | return if (val.isUndef(mod)) |
| 18877 | mod.undefRef(Type.bool) | |
| 19038 | pt.undefRef(Type.bool) | |
| 18878 | 19039 | else if (val.toBool()) .bool_false else .bool_true; |
| 18879 | 19040 | } |
| 18880 | 19041 | try sema.requireRuntimeBlock(block, src, null); |
| ... | ... | @@ -18890,7 +19051,8 @@ fn zirBoolBr( |
| 18890 | 19051 | const tracy = trace(@src()); |
| 18891 | 19052 | defer tracy.end(); |
| 18892 | 19053 | |
| 18893 | const mod = sema.mod; | |
| 19054 | const pt = sema.pt; | |
| 19055 | const mod = pt.zcu; | |
| 18894 | 19056 | const gpa = sema.gpa; |
| 18895 | 19057 | |
| 18896 | 19058 | const datas = sema.code.instructions.items(.data); |
| ... | ... | @@ -19006,7 +19168,8 @@ fn finishCondBr( |
| 19006 | 19168 | } |
| 19007 | 19169 | |
| 19008 | 19170 | fn checkNullableType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void { |
| 19009 | const mod = sema.mod; | |
| 19171 | const pt = sema.pt; | |
| 19172 | const mod = pt.zcu; | |
| 19010 | 19173 | switch (ty.zigTypeTag(mod)) { |
| 19011 | 19174 | .Optional, .Null, .Undefined => return, |
| 19012 | 19175 | .Pointer => if (ty.isPtrLikeOptional(mod)) return, |
| ... | ... | @@ -19038,7 +19201,8 @@ fn zirIsNonNullPtr( |
| 19038 | 19201 | const tracy = trace(@src()); |
| 19039 | 19202 | defer tracy.end(); |
| 19040 | 19203 | |
| 19041 | const mod = sema.mod; | |
| 19204 | const pt = sema.pt; | |
| 19205 | const mod = pt.zcu; | |
| 19042 | 19206 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 19043 | 19207 | const src = block.nodeOffset(inst_data.src_node); |
| 19044 | 19208 | const ptr = try sema.resolveInst(inst_data.operand); |
| ... | ... | @@ -19051,11 +19215,12 @@ fn zirIsNonNullPtr( |
| 19051 | 19215 | } |
| 19052 | 19216 | |
| 19053 | 19217 | fn checkErrorType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void { |
| 19054 | const mod = sema.mod; | |
| 19218 | const pt = sema.pt; | |
| 19219 | const mod = pt.zcu; | |
| 19055 | 19220 | switch (ty.zigTypeTag(mod)) { |
| 19056 | 19221 | .ErrorSet, .ErrorUnion, .Undefined => return, |
| 19057 | 19222 | else => return sema.fail(block, src, "expected error union type, found '{}'", .{ |
| 19058 | ty.fmt(mod), | |
| 19223 | ty.fmt(pt), | |
| 19059 | 19224 | }), |
| 19060 | 19225 | } |
| 19061 | 19226 | } |
| ... | ... | @@ -19075,7 +19240,8 @@ fn zirIsNonErrPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 19075 | 19240 | const tracy = trace(@src()); |
| 19076 | 19241 | defer tracy.end(); |
| 19077 | 19242 | |
| 19078 | const mod = sema.mod; | |
| 19243 | const pt = sema.pt; | |
| 19244 | const mod = pt.zcu; | |
| 19079 | 19245 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 19080 | 19246 | const src = block.nodeOffset(inst_data.src_node); |
| 19081 | 19247 | const ptr = try sema.resolveInst(inst_data.operand); |
| ... | ... | @@ -19102,7 +19268,8 @@ fn zirCondbr( |
| 19102 | 19268 | const tracy = trace(@src()); |
| 19103 | 19269 | defer tracy.end(); |
| 19104 | 19270 | |
| 19105 | const mod = sema.mod; | |
| 19271 | const pt = sema.pt; | |
| 19272 | const mod = pt.zcu; | |
| 19106 | 19273 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 19107 | 19274 | const cond_src = parent_block.src(.{ .node_offset_if_cond = inst_data.src_node }); |
| 19108 | 19275 | const extra = sema.code.extraData(Zir.Inst.CondBr, inst_data.payload_index); |
| ... | ... | @@ -19177,10 +19344,11 @@ fn zirTry(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileError! |
| 19177 | 19344 | const body = sema.code.bodySlice(extra.end, extra.data.body_len); |
| 19178 | 19345 | const err_union = try sema.resolveInst(extra.data.operand); |
| 19179 | 19346 | const err_union_ty = sema.typeOf(err_union); |
| 19180 | const mod = sema.mod; | |
| 19347 | const pt = sema.pt; | |
| 19348 | const mod = pt.zcu; | |
| 19181 | 19349 | if (err_union_ty.zigTypeTag(mod) != .ErrorUnion) { |
| 19182 | 19350 | return sema.fail(parent_block, operand_src, "expected error union type, found '{}'", .{ |
| 19183 | err_union_ty.fmt(mod), | |
| 19351 | err_union_ty.fmt(pt), | |
| 19184 | 19352 | }); |
| 19185 | 19353 | } |
| 19186 | 19354 | const is_non_err = try sema.analyzeIsNonErrComptimeOnly(parent_block, operand_src, err_union); |
| ... | ... | @@ -19225,10 +19393,11 @@ fn zirTryPtr(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileErr |
| 19225 | 19393 | const operand = try sema.resolveInst(extra.data.operand); |
| 19226 | 19394 | const err_union = try sema.analyzeLoad(parent_block, src, operand, operand_src); |
| 19227 | 19395 | const err_union_ty = sema.typeOf(err_union); |
| 19228 | const mod = sema.mod; | |
| 19396 | const pt = sema.pt; | |
| 19397 | const mod = pt.zcu; | |
| 19229 | 19398 | if (err_union_ty.zigTypeTag(mod) != .ErrorUnion) { |
| 19230 | 19399 | return sema.fail(parent_block, operand_src, "expected error union type, found '{}'", .{ |
| 19231 | err_union_ty.fmt(mod), | |
| 19400 | err_union_ty.fmt(pt), | |
| 19232 | 19401 | }); |
| 19233 | 19402 | } |
| 19234 | 19403 | const is_non_err = try sema.analyzeIsNonErrComptimeOnly(parent_block, operand_src, err_union); |
| ... | ... | @@ -19251,7 +19420,7 @@ fn zirTryPtr(sema: *Sema, parent_block: *Block, inst: Zir.Inst.Index) CompileErr |
| 19251 | 19420 | |
| 19252 | 19421 | const operand_ty = sema.typeOf(operand); |
| 19253 | 19422 | const ptr_info = operand_ty.ptrInfo(mod); |
| 19254 | const res_ty = try mod.ptrTypeSema(.{ | |
| 19423 | const res_ty = try pt.ptrTypeSema(.{ | |
| 19255 | 19424 | .child = err_union_ty.errorUnionPayload(mod).toIntern(), |
| 19256 | 19425 | .flags = .{ |
| 19257 | 19426 | .is_const = ptr_info.flags.is_const, |
| ... | ... | @@ -19366,7 +19535,8 @@ fn zirRetErrValue( |
| 19366 | 19535 | block: *Block, |
| 19367 | 19536 | inst: Zir.Inst.Index, |
| 19368 | 19537 | ) CompileError!void { |
| 19369 | const mod = sema.mod; | |
| 19538 | const pt = sema.pt; | |
| 19539 | const mod = pt.zcu; | |
| 19370 | 19540 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].str_tok; |
| 19371 | 19541 | const src = block.tokenOffset(inst_data.src_tok); |
| 19372 | 19542 | const err_name = try mod.intern_pool.getOrPutString( |
| ... | ... | @@ -19376,8 +19546,8 @@ fn zirRetErrValue( |
| 19376 | 19546 | ); |
| 19377 | 19547 | _ = try mod.getErrorValue(err_name); |
| 19378 | 19548 | // Return the error code from the function. |
| 19379 | const error_set_type = try mod.singleErrorSetType(err_name); | |
| 19380 | const result_inst = Air.internedToRef((try mod.intern(.{ .err = .{ | |
| 19549 | const error_set_type = try pt.singleErrorSetType(err_name); | |
| 19550 | const result_inst = Air.internedToRef((try pt.intern(.{ .err = .{ | |
| 19381 | 19551 | .ty = error_set_type.toIntern(), |
| 19382 | 19552 | .name = err_name, |
| 19383 | 19553 | } }))); |
| ... | ... | @@ -19392,7 +19562,8 @@ fn zirRetImplicit( |
| 19392 | 19562 | const tracy = trace(@src()); |
| 19393 | 19563 | defer tracy.end(); |
| 19394 | 19564 | |
| 19395 | const mod = sema.mod; | |
| 19565 | const pt = sema.pt; | |
| 19566 | const mod = pt.zcu; | |
| 19396 | 19567 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_tok; |
| 19397 | 19568 | const r_brace_src = block.tokenOffset(inst_data.src_tok); |
| 19398 | 19569 | if (block.inlining == null and sema.func_is_naked) { |
| ... | ... | @@ -19412,7 +19583,7 @@ fn zirRetImplicit( |
| 19412 | 19583 | if (base_tag == .NoReturn) { |
| 19413 | 19584 | const msg = msg: { |
| 19414 | 19585 | const msg = try sema.errMsg(ret_ty_src, "function declared '{}' implicitly returns", .{ |
| 19415 | sema.fn_ret_ty.fmt(mod), | |
| 19586 | sema.fn_ret_ty.fmt(pt), | |
| 19416 | 19587 | }); |
| 19417 | 19588 | errdefer msg.destroy(sema.gpa); |
| 19418 | 19589 | try sema.errNote(r_brace_src, msg, "control flow reaches end of body here", .{}); |
| ... | ... | @@ -19422,7 +19593,7 @@ fn zirRetImplicit( |
| 19422 | 19593 | } else if (base_tag != .Void) { |
| 19423 | 19594 | const msg = msg: { |
| 19424 | 19595 | const msg = try sema.errMsg(ret_ty_src, "function with non-void return type '{}' implicitly returns", .{ |
| 19425 | sema.fn_ret_ty.fmt(mod), | |
| 19596 | sema.fn_ret_ty.fmt(pt), | |
| 19426 | 19597 | }); |
| 19427 | 19598 | errdefer msg.destroy(sema.gpa); |
| 19428 | 19599 | try sema.errNote(r_brace_src, msg, "control flow reaches end of body here", .{}); |
| ... | ... | @@ -19474,7 +19645,7 @@ fn retWithErrTracing( |
| 19474 | 19645 | ret_tag: Air.Inst.Tag, |
| 19475 | 19646 | operand: Air.Inst.Ref, |
| 19476 | 19647 | ) CompileError!void { |
| 19477 | const mod = sema.mod; | |
| 19648 | const pt = sema.pt; | |
| 19478 | 19649 | const need_check = switch (is_non_err) { |
| 19479 | 19650 | .bool_true => { |
| 19480 | 19651 | _ = try block.addUnOp(ret_tag, operand); |
| ... | ... | @@ -19484,11 +19655,11 @@ fn retWithErrTracing( |
| 19484 | 19655 | else => true, |
| 19485 | 19656 | }; |
| 19486 | 19657 | const gpa = sema.gpa; |
| 19487 | const stack_trace_ty = try mod.getBuiltinType("StackTrace"); | |
| 19488 | try stack_trace_ty.resolveFields(mod); | |
| 19489 | const ptr_stack_trace_ty = try mod.singleMutPtrType(stack_trace_ty); | |
| 19658 | const stack_trace_ty = try pt.getBuiltinType("StackTrace"); | |
| 19659 | try stack_trace_ty.resolveFields(pt); | |
| 19660 | const ptr_stack_trace_ty = try pt.singleMutPtrType(stack_trace_ty); | |
| 19490 | 19661 | const err_return_trace = try block.addTy(.err_return_trace, ptr_stack_trace_ty); |
| 19491 | const return_err_fn = try mod.getBuiltin("returnError"); | |
| 19662 | const return_err_fn = try pt.getBuiltin("returnError"); | |
| 19492 | 19663 | const args: [1]Air.Inst.Ref = .{err_return_trace}; |
| 19493 | 19664 | |
| 19494 | 19665 | if (!need_check) { |
| ... | ... | @@ -19524,12 +19695,14 @@ fn retWithErrTracing( |
| 19524 | 19695 | } |
| 19525 | 19696 | |
| 19526 | 19697 | fn wantErrorReturnTracing(sema: *Sema, fn_ret_ty: Type) bool { |
| 19527 | const mod = sema.mod; | |
| 19698 | const pt = sema.pt; | |
| 19699 | const mod = pt.zcu; | |
| 19528 | 19700 | return fn_ret_ty.isError(mod) and mod.comp.config.any_error_tracing; |
| 19529 | 19701 | } |
| 19530 | 19702 | |
| 19531 | 19703 | fn zirSaveErrRetIndex(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void { |
| 19532 | const mod = sema.mod; | |
| 19704 | const pt = sema.pt; | |
| 19705 | const mod = pt.zcu; | |
| 19533 | 19706 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].save_err_ret_index; |
| 19534 | 19707 | |
| 19535 | 19708 | if (!block.ownerModule().error_tracing) return; |
| ... | ... | @@ -19559,7 +19732,8 @@ fn restoreErrRetIndex(sema: *Sema, start_block: *Block, src: LazySrcLoc, target_ |
| 19559 | 19732 | const tracy = trace(@src()); |
| 19560 | 19733 | defer tracy.end(); |
| 19561 | 19734 | |
| 19562 | const mod = sema.mod; | |
| 19735 | const pt = sema.pt; | |
| 19736 | const mod = pt.zcu; | |
| 19563 | 19737 | |
| 19564 | 19738 | const saved_index = if (target_block.toIndexAllowNone()) |zir_block| b: { |
| 19565 | 19739 | var block = start_block; |
| ... | ... | @@ -19597,7 +19771,7 @@ fn restoreErrRetIndex(sema: *Sema, start_block: *Block, src: LazySrcLoc, target_ |
| 19597 | 19771 | if (is_non_error) return; |
| 19598 | 19772 | |
| 19599 | 19773 | const saved_index_val = try sema.resolveDefinedValue(start_block, src, saved_index); |
| 19600 | const saved_index_int = saved_index_val.?.toUnsignedInt(mod); | |
| 19774 | const saved_index_int = saved_index_val.?.toUnsignedInt(pt); | |
| 19601 | 19775 | assert(saved_index_int <= sema.comptime_err_ret_trace.items.len); |
| 19602 | 19776 | sema.comptime_err_ret_trace.items.len = @intCast(saved_index_int); |
| 19603 | 19777 | return; |
| ... | ... | @@ -19612,7 +19786,8 @@ fn restoreErrRetIndex(sema: *Sema, start_block: *Block, src: LazySrcLoc, target_ |
| 19612 | 19786 | } |
| 19613 | 19787 | |
| 19614 | 19788 | fn addToInferredErrorSet(sema: *Sema, uncasted_operand: Air.Inst.Ref) !void { |
| 19615 | const mod = sema.mod; | |
| 19789 | const pt = sema.pt; | |
| 19790 | const mod = pt.zcu; | |
| 19616 | 19791 | const ip = &mod.intern_pool; |
| 19617 | 19792 | assert(sema.fn_ret_ty.zigTypeTag(mod) == .ErrorUnion); |
| 19618 | 19793 | const err_set_ty = sema.fn_ret_ty.errorUnionSet(mod).toIntern(); |
| ... | ... | @@ -19632,7 +19807,8 @@ fn addToInferredErrorSet(sema: *Sema, uncasted_operand: Air.Inst.Ref) !void { |
| 19632 | 19807 | |
| 19633 | 19808 | fn addToInferredErrorSetPtr(sema: *Sema, ies: *InferredErrorSet, op_ty: Type) !void { |
| 19634 | 19809 | const arena = sema.arena; |
| 19635 | const mod = sema.mod; | |
| 19810 | const pt = sema.pt; | |
| 19811 | const mod = pt.zcu; | |
| 19636 | 19812 | const ip = &mod.intern_pool; |
| 19637 | 19813 | switch (op_ty.zigTypeTag(mod)) { |
| 19638 | 19814 | .ErrorSet => try ies.addErrorSet(op_ty, ip, arena), |
| ... | ... | @@ -19651,7 +19827,8 @@ fn analyzeRet( |
| 19651 | 19827 | // Special case for returning an error to an inferred error set; we need to |
| 19652 | 19828 | // add the error tag to the inferred error set of the in-scope function, so |
| 19653 | 19829 | // that the coercion below works correctly. |
| 19654 | const mod = sema.mod; | |
| 19830 | const pt = sema.pt; | |
| 19831 | const mod = pt.zcu; | |
| 19655 | 19832 | if (sema.fn_ret_ty_ies != null and sema.fn_ret_ty.zigTypeTag(mod) == .ErrorUnion) { |
| 19656 | 19833 | try sema.addToInferredErrorSet(uncasted_operand); |
| 19657 | 19834 | } |
| ... | ... | @@ -19691,7 +19868,7 @@ fn analyzeRet( |
| 19691 | 19868 | return sema.failWithOwnedErrorMsg(block, msg); |
| 19692 | 19869 | } |
| 19693 | 19870 | |
| 19694 | try sema.fn_ret_ty.resolveLayout(mod); | |
| 19871 | try sema.fn_ret_ty.resolveLayout(pt); | |
| 19695 | 19872 | |
| 19696 | 19873 | try sema.validateRuntimeValue(block, operand_src, operand); |
| 19697 | 19874 | |
| ... | ... | @@ -19718,7 +19895,8 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 19718 | 19895 | const tracy = trace(@src()); |
| 19719 | 19896 | defer tracy.end(); |
| 19720 | 19897 | |
| 19721 | const mod = sema.mod; | |
| 19898 | const pt = sema.pt; | |
| 19899 | const mod = pt.zcu; | |
| 19722 | 19900 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].ptr_type; |
| 19723 | 19901 | const extra = sema.code.extraData(Zir.Inst.PtrType, inst_data.payload_index); |
| 19724 | 19902 | const elem_ty_src = block.src(.{ .node_offset_ptr_elem = extra.data.src_node }); |
| ... | ... | @@ -19773,7 +19951,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 19773 | 19951 | }, |
| 19774 | 19952 | else => {}, |
| 19775 | 19953 | } |
| 19776 | const align_bytes = (try val.getUnsignedIntAdvanced(mod, .sema)).?; | |
| 19954 | const align_bytes = (try val.getUnsignedIntAdvanced(pt, .sema)).?; | |
| 19777 | 19955 | break :blk try sema.validateAlignAllowZero(block, align_src, align_bytes); |
| 19778 | 19956 | } else .none; |
| 19779 | 19957 | |
| ... | ... | @@ -19804,13 +19982,13 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 19804 | 19982 | if (host_size != 0) { |
| 19805 | 19983 | if (bit_offset >= host_size * 8) { |
| 19806 | 19984 | return sema.fail(block, bitoffset_src, "packed type '{}' at bit offset {} starts {} bits after the end of a {} byte host integer", .{ |
| 19807 | elem_ty.fmt(mod), bit_offset, bit_offset - host_size * 8, host_size, | |
| 19985 | elem_ty.fmt(pt), bit_offset, bit_offset - host_size * 8, host_size, | |
| 19808 | 19986 | }); |
| 19809 | 19987 | } |
| 19810 | const elem_bit_size = try elem_ty.bitSizeAdvanced(mod, .sema); | |
| 19988 | const elem_bit_size = try elem_ty.bitSizeAdvanced(pt, .sema); | |
| 19811 | 19989 | if (elem_bit_size > host_size * 8 - bit_offset) { |
| 19812 | 19990 | return sema.fail(block, bitoffset_src, "packed type '{}' at bit offset {} ends {} bits after the end of a {} byte host integer", .{ |
| 19813 | elem_ty.fmt(mod), bit_offset, elem_bit_size - (host_size * 8 - bit_offset), host_size, | |
| 19991 | elem_ty.fmt(pt), bit_offset, elem_bit_size - (host_size * 8 - bit_offset), host_size, | |
| 19814 | 19992 | }); |
| 19815 | 19993 | } |
| 19816 | 19994 | } |
| ... | ... | @@ -19824,7 +20002,7 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 19824 | 20002 | } else if (inst_data.size == .C) { |
| 19825 | 20003 | if (!try sema.validateExternType(elem_ty, .other)) { |
| 19826 | 20004 | const msg = msg: { |
| 19827 | const msg = try sema.errMsg(elem_ty_src, "C pointers cannot point to non-C-ABI-compatible type '{}'", .{elem_ty.fmt(mod)}); | |
| 20005 | const msg = try sema.errMsg(elem_ty_src, "C pointers cannot point to non-C-ABI-compatible type '{}'", .{elem_ty.fmt(pt)}); | |
| 19828 | 20006 | errdefer msg.destroy(sema.gpa); |
| 19829 | 20007 | |
| 19830 | 20008 | try sema.explainWhyTypeIsNotExtern(msg, elem_ty_src, elem_ty, .other); |
| ... | ... | @@ -19841,14 +20019,14 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 19841 | 20019 | |
| 19842 | 20020 | if (host_size != 0 and !try sema.validatePackedType(elem_ty)) { |
| 19843 | 20021 | return sema.failWithOwnedErrorMsg(block, msg: { |
| 19844 | const msg = try sema.errMsg(elem_ty_src, "bit-pointer cannot refer to value of type '{}'", .{elem_ty.fmt(mod)}); | |
| 20022 | const msg = try sema.errMsg(elem_ty_src, "bit-pointer cannot refer to value of type '{}'", .{elem_ty.fmt(pt)}); | |
| 19845 | 20023 | errdefer msg.destroy(sema.gpa); |
| 19846 | 20024 | try sema.explainWhyTypeIsNotPacked(msg, elem_ty_src, elem_ty); |
| 19847 | 20025 | break :msg msg; |
| 19848 | 20026 | }); |
| 19849 | 20027 | } |
| 19850 | 20028 | |
| 19851 | const ty = try mod.ptrTypeSema(.{ | |
| 20029 | const ty = try pt.ptrTypeSema(.{ | |
| 19852 | 20030 | .child = elem_ty.toIntern(), |
| 19853 | 20031 | .sentinel = sentinel, |
| 19854 | 20032 | .flags = .{ |
| ... | ... | @@ -19875,7 +20053,8 @@ fn zirStructInitEmpty(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE |
| 19875 | 20053 | const src = block.nodeOffset(inst_data.src_node); |
| 19876 | 20054 | const ty_src = block.src(.{ .node_offset_init_ty = inst_data.src_node }); |
| 19877 | 20055 | const obj_ty = try sema.resolveType(block, ty_src, inst_data.operand); |
| 19878 | const mod = sema.mod; | |
| 20056 | const pt = sema.pt; | |
| 20057 | const mod = pt.zcu; | |
| 19879 | 20058 | |
| 19880 | 20059 | switch (obj_ty.zigTypeTag(mod)) { |
| 19881 | 20060 | .Struct => return sema.structInitEmpty(block, obj_ty, src, src), |
| ... | ... | @@ -19890,7 +20069,8 @@ fn zirStructInitEmptyResult(sema: *Sema, block: *Block, inst: Zir.Inst.Index, is |
| 19890 | 20069 | const tracy = trace(@src()); |
| 19891 | 20070 | defer tracy.end(); |
| 19892 | 20071 | |
| 19893 | const mod = sema.mod; | |
| 20072 | const pt = sema.pt; | |
| 20073 | const mod = pt.zcu; | |
| 19894 | 20074 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 19895 | 20075 | const src = block.nodeOffset(inst_data.src_node); |
| 19896 | 20076 | const ty_operand = sema.resolveType(block, src, inst_data.operand) catch |err| switch (err) { |
| ... | ... | @@ -19905,7 +20085,7 @@ fn zirStructInitEmptyResult(sema: *Sema, block: *Block, inst: Zir.Inst.Index, is |
| 19905 | 20085 | break :ty ptr_ty.childType(mod); |
| 19906 | 20086 | } |
| 19907 | 20087 | // To make `&.{}` a `[:s]T`, the init should be a `[0:s]T`. |
| 19908 | break :ty try mod.arrayType(.{ | |
| 20088 | break :ty try pt.arrayType(.{ | |
| 19909 | 20089 | .len = 0, |
| 19910 | 20090 | .sentinel = if (ptr_ty.sentinel(mod)) |s| s.toIntern() else .none, |
| 19911 | 20091 | .child = ptr_ty.childType(mod).toIntern(), |
| ... | ... | @@ -19936,10 +20116,11 @@ fn structInitEmpty( |
| 19936 | 20116 | dest_src: LazySrcLoc, |
| 19937 | 20117 | init_src: LazySrcLoc, |
| 19938 | 20118 | ) CompileError!Air.Inst.Ref { |
| 19939 | const mod = sema.mod; | |
| 20119 | const pt = sema.pt; | |
| 20120 | const mod = pt.zcu; | |
| 19940 | 20121 | const gpa = sema.gpa; |
| 19941 | 20122 | // This logic must be synchronized with that in `zirStructInit`. |
| 19942 | try struct_ty.resolveFields(mod); | |
| 20123 | try struct_ty.resolveFields(pt); | |
| 19943 | 20124 | |
| 19944 | 20125 | // The init values to use for the struct instance. |
| 19945 | 20126 | const field_inits = try gpa.alloc(Air.Inst.Ref, struct_ty.structFieldCount(mod)); |
| ... | ... | @@ -19950,7 +20131,8 @@ fn structInitEmpty( |
| 19950 | 20131 | } |
| 19951 | 20132 | |
| 19952 | 20133 | fn arrayInitEmpty(sema: *Sema, block: *Block, src: LazySrcLoc, obj_ty: Type) CompileError!Air.Inst.Ref { |
| 19953 | const mod = sema.mod; | |
| 20134 | const pt = sema.pt; | |
| 20135 | const mod = pt.zcu; | |
| 19954 | 20136 | const arr_len = obj_ty.arrayLen(mod); |
| 19955 | 20137 | if (arr_len != 0) { |
| 19956 | 20138 | if (obj_ty.zigTypeTag(mod) == .Array) { |
| ... | ... | @@ -19959,21 +20141,22 @@ fn arrayInitEmpty(sema: *Sema, block: *Block, src: LazySrcLoc, obj_ty: Type) Com |
| 19959 | 20141 | return sema.fail(block, src, "expected {d} vector elements; found 0", .{arr_len}); |
| 19960 | 20142 | } |
| 19961 | 20143 | } |
| 19962 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 20144 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 19963 | 20145 | .ty = obj_ty.toIntern(), |
| 19964 | 20146 | .storage = .{ .elems = &.{} }, |
| 19965 | 20147 | } }))); |
| 19966 | 20148 | } |
| 19967 | 20149 | |
| 19968 | 20150 | fn zirUnionInit(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 20151 | const pt = sema.pt; | |
| 19969 | 20152 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 19970 | 20153 | const ty_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| 19971 | 20154 | const field_src = block.builtinCallArgSrc(inst_data.src_node, 1); |
| 19972 | 20155 | const init_src = block.builtinCallArgSrc(inst_data.src_node, 2); |
| 19973 | 20156 | const extra = sema.code.extraData(Zir.Inst.UnionInit, inst_data.payload_index).data; |
| 19974 | 20157 | const union_ty = try sema.resolveType(block, ty_src, extra.union_type); |
| 19975 | if (union_ty.zigTypeTag(sema.mod) != .Union) { | |
| 19976 | return sema.fail(block, ty_src, "expected union type, found '{}'", .{union_ty.fmt(sema.mod)}); | |
| 20158 | if (union_ty.zigTypeTag(pt.zcu) != .Union) { | |
| 20159 | return sema.fail(block, ty_src, "expected union type, found '{}'", .{union_ty.fmt(pt)}); | |
| 19977 | 20160 | } |
| 19978 | 20161 | const field_name = try sema.resolveConstStringIntern(block, field_src, extra.field_name, .{ |
| 19979 | 20162 | .needed_comptime_reason = "name of field being initialized must be comptime-known", |
| ... | ... | @@ -19992,7 +20175,8 @@ fn unionInit( |
| 19992 | 20175 | field_name: InternPool.NullTerminatedString, |
| 19993 | 20176 | field_src: LazySrcLoc, |
| 19994 | 20177 | ) CompileError!Air.Inst.Ref { |
| 19995 | const mod = sema.mod; | |
| 20178 | const pt = sema.pt; | |
| 20179 | const mod = pt.zcu; | |
| 19996 | 20180 | const ip = &mod.intern_pool; |
| 19997 | 20181 | const field_index = try sema.unionFieldIndex(block, union_ty, field_name, field_src); |
| 19998 | 20182 | const field_ty = Type.fromInterned(mod.typeToUnion(union_ty).?.field_types.get(ip)[field_index]); |
| ... | ... | @@ -20000,8 +20184,8 @@ fn unionInit( |
| 20000 | 20184 | |
| 20001 | 20185 | if (try sema.resolveValue(init)) |init_val| { |
| 20002 | 20186 | const tag_ty = union_ty.unionTagTypeHypothetical(mod); |
| 20003 | const tag_val = try mod.enumValueFieldIndex(tag_ty, field_index); | |
| 20004 | return Air.internedToRef((try mod.intern(.{ .un = .{ | |
| 20187 | const tag_val = try pt.enumValueFieldIndex(tag_ty, field_index); | |
| 20188 | return Air.internedToRef((try pt.intern(.{ .un = .{ | |
| 20005 | 20189 | .ty = union_ty.toIntern(), |
| 20006 | 20190 | .tag = tag_val.toIntern(), |
| 20007 | 20191 | .val = init_val.toIntern(), |
| ... | ... | @@ -20025,7 +20209,8 @@ fn zirStructInit( |
| 20025 | 20209 | const extra = sema.code.extraData(Zir.Inst.StructInit, inst_data.payload_index); |
| 20026 | 20210 | const src = block.nodeOffset(inst_data.src_node); |
| 20027 | 20211 | |
| 20028 | const mod = sema.mod; | |
| 20212 | const pt = sema.pt; | |
| 20213 | const mod = pt.zcu; | |
| 20029 | 20214 | const ip = &mod.intern_pool; |
| 20030 | 20215 | const first_item = sema.code.extraData(Zir.Inst.StructInit.Item, extra.end).data; |
| 20031 | 20216 | const first_field_type_data = zir_datas[@intFromEnum(first_item.field_type)].pl_node; |
| ... | ... | @@ -20038,7 +20223,7 @@ fn zirStructInit( |
| 20038 | 20223 | else => |e| return e, |
| 20039 | 20224 | }; |
| 20040 | 20225 | const resolved_ty = result_ty.optEuBaseType(mod); |
| 20041 | try resolved_ty.resolveLayout(mod); | |
| 20226 | try resolved_ty.resolveLayout(pt); | |
| 20042 | 20227 | |
| 20043 | 20228 | if (resolved_ty.zigTypeTag(mod) == .Struct) { |
| 20044 | 20229 | // This logic must be synchronized with that in `zirStructInitEmpty`. |
| ... | ... | @@ -20079,8 +20264,8 @@ fn zirStructInit( |
| 20079 | 20264 | const field_ty = resolved_ty.structFieldType(field_index, mod); |
| 20080 | 20265 | field_inits[field_index] = try sema.coerce(block, field_ty, uncoerced_init, field_src); |
| 20081 | 20266 | if (!is_packed) { |
| 20082 | try resolved_ty.resolveStructFieldInits(mod); | |
| 20083 | if (try resolved_ty.structFieldValueComptime(mod, field_index)) |default_value| { | |
| 20267 | try resolved_ty.resolveStructFieldInits(pt); | |
| 20268 | if (try resolved_ty.structFieldValueComptime(pt, field_index)) |default_value| { | |
| 20084 | 20269 | const init_val = (try sema.resolveValue(field_inits[field_index])) orelse { |
| 20085 | 20270 | return sema.failWithNeededComptime(block, field_src, .{ |
| 20086 | 20271 | .needed_comptime_reason = "value stored in comptime field must be comptime-known", |
| ... | ... | @@ -20112,7 +20297,7 @@ fn zirStructInit( |
| 20112 | 20297 | ); |
| 20113 | 20298 | const field_index = try sema.unionFieldIndex(block, resolved_ty, field_name, field_src); |
| 20114 | 20299 | const tag_ty = resolved_ty.unionTagTypeHypothetical(mod); |
| 20115 | const tag_val = try mod.enumValueFieldIndex(tag_ty, field_index); | |
| 20300 | const tag_val = try pt.enumValueFieldIndex(tag_ty, field_index); | |
| 20116 | 20301 | const field_ty = Type.fromInterned(mod.typeToUnion(resolved_ty).?.field_types.get(ip)[field_index]); |
| 20117 | 20302 | |
| 20118 | 20303 | if (field_ty.zigTypeTag(mod) == .NoReturn) { |
| ... | ... | @@ -20132,11 +20317,11 @@ fn zirStructInit( |
| 20132 | 20317 | const init_inst = try sema.coerce(block, field_ty, uncoerced_init_inst, field_src); |
| 20133 | 20318 | |
| 20134 | 20319 | if (try sema.resolveValue(init_inst)) |val| { |
| 20135 | const struct_val = Value.fromInterned((try mod.intern(.{ .un = .{ | |
| 20320 | const struct_val = Value.fromInterned(try pt.intern(.{ .un = .{ | |
| 20136 | 20321 | .ty = resolved_ty.toIntern(), |
| 20137 | 20322 | .tag = tag_val.toIntern(), |
| 20138 | 20323 | .val = val.toIntern(), |
| 20139 | } }))); | |
| 20324 | } })); | |
| 20140 | 20325 | const final_val_inst = try sema.coerce(block, result_ty, Air.internedToRef(struct_val.toIntern()), src); |
| 20141 | 20326 | const final_val = (try sema.resolveValue(final_val_inst)).?; |
| 20142 | 20327 | return sema.addConstantMaybeRef(final_val.toIntern(), is_ref); |
| ... | ... | @@ -20152,7 +20337,7 @@ fn zirStructInit( |
| 20152 | 20337 | |
| 20153 | 20338 | if (is_ref) { |
| 20154 | 20339 | const target = mod.getTarget(); |
| 20155 | const alloc_ty = try mod.ptrTypeSema(.{ | |
| 20340 | const alloc_ty = try pt.ptrTypeSema(.{ | |
| 20156 | 20341 | .child = result_ty.toIntern(), |
| 20157 | 20342 | .flags = .{ .address_space = target_util.defaultAddressSpace(target, .local) }, |
| 20158 | 20343 | }); |
| ... | ... | @@ -20182,7 +20367,8 @@ fn finishStructInit( |
| 20182 | 20367 | result_ty: Type, |
| 20183 | 20368 | is_ref: bool, |
| 20184 | 20369 | ) CompileError!Air.Inst.Ref { |
| 20185 | const mod = sema.mod; | |
| 20370 | const pt = sema.pt; | |
| 20371 | const mod = pt.zcu; | |
| 20186 | 20372 | const ip = &mod.intern_pool; |
| 20187 | 20373 | |
| 20188 | 20374 | var root_msg: ?*Module.ErrorMsg = null; |
| ... | ... | @@ -20242,7 +20428,7 @@ fn finishStructInit( |
| 20242 | 20428 | continue; |
| 20243 | 20429 | } |
| 20244 | 20430 | |
| 20245 | try struct_ty.resolveStructFieldInits(mod); | |
| 20431 | try struct_ty.resolveStructFieldInits(pt); | |
| 20246 | 20432 | |
| 20247 | 20433 | const field_init = struct_type.fieldInit(ip, i); |
| 20248 | 20434 | if (field_init == .none) { |
| ... | ... | @@ -20289,7 +20475,7 @@ fn finishStructInit( |
| 20289 | 20475 | for (elems, field_inits) |*elem, field_init| { |
| 20290 | 20476 | elem.* = (sema.resolveValue(field_init) catch unreachable).?.toIntern(); |
| 20291 | 20477 | } |
| 20292 | const struct_val = try mod.intern(.{ .aggregate = .{ | |
| 20478 | const struct_val = try pt.intern(.{ .aggregate = .{ | |
| 20293 | 20479 | .ty = struct_ty.toIntern(), |
| 20294 | 20480 | .storage = .{ .elems = elems }, |
| 20295 | 20481 | } }); |
| ... | ... | @@ -20312,9 +20498,9 @@ fn finishStructInit( |
| 20312 | 20498 | } |
| 20313 | 20499 | |
| 20314 | 20500 | if (is_ref) { |
| 20315 | try struct_ty.resolveLayout(mod); | |
| 20316 | const target = sema.mod.getTarget(); | |
| 20317 | const alloc_ty = try mod.ptrTypeSema(.{ | |
| 20501 | try struct_ty.resolveLayout(pt); | |
| 20502 | const target = mod.getTarget(); | |
| 20503 | const alloc_ty = try pt.ptrTypeSema(.{ | |
| 20318 | 20504 | .child = result_ty.toIntern(), |
| 20319 | 20505 | .flags = .{ .address_space = target_util.defaultAddressSpace(target, .local) }, |
| 20320 | 20506 | }); |
| ... | ... | @@ -20334,7 +20520,7 @@ fn finishStructInit( |
| 20334 | 20520 | .init_node_offset = init_src.offset.node_offset.x, |
| 20335 | 20521 | .elem_index = @intCast(runtime_index), |
| 20336 | 20522 | } })); |
| 20337 | try struct_ty.resolveStructFieldInits(mod); | |
| 20523 | try struct_ty.resolveStructFieldInits(pt); | |
| 20338 | 20524 | const struct_val = try block.addAggregateInit(struct_ty, field_inits); |
| 20339 | 20525 | return sema.coerce(block, result_ty, struct_val, init_src); |
| 20340 | 20526 | } |
| ... | ... | @@ -20364,7 +20550,8 @@ fn structInitAnon( |
| 20364 | 20550 | extra_end: usize, |
| 20365 | 20551 | is_ref: bool, |
| 20366 | 20552 | ) CompileError!Air.Inst.Ref { |
| 20367 | const mod = sema.mod; | |
| 20553 | const pt = sema.pt; | |
| 20554 | const mod = pt.zcu; | |
| 20368 | 20555 | const gpa = sema.gpa; |
| 20369 | 20556 | const ip = &mod.intern_pool; |
| 20370 | 20557 | const zir_datas = sema.code.instructions.items(.data); |
| ... | ... | @@ -20422,14 +20609,14 @@ fn structInitAnon( |
| 20422 | 20609 | break :rs runtime_index; |
| 20423 | 20610 | }; |
| 20424 | 20611 | |
| 20425 | const tuple_ty = try ip.getAnonStructType(gpa, .{ | |
| 20612 | const tuple_ty = try ip.getAnonStructType(gpa, pt.tid, .{ | |
| 20426 | 20613 | .names = names, |
| 20427 | 20614 | .types = types, |
| 20428 | 20615 | .values = values, |
| 20429 | 20616 | }); |
| 20430 | 20617 | |
| 20431 | 20618 | const runtime_index = opt_runtime_index orelse { |
| 20432 | const tuple_val = try mod.intern(.{ .aggregate = .{ | |
| 20619 | const tuple_val = try pt.intern(.{ .aggregate = .{ | |
| 20433 | 20620 | .ty = tuple_ty, |
| 20434 | 20621 | .storage = .{ .elems = values }, |
| 20435 | 20622 | } }); |
| ... | ... | @@ -20443,7 +20630,7 @@ fn structInitAnon( |
| 20443 | 20630 | |
| 20444 | 20631 | if (is_ref) { |
| 20445 | 20632 | const target = mod.getTarget(); |
| 20446 | const alloc_ty = try mod.ptrTypeSema(.{ | |
| 20633 | const alloc_ty = try pt.ptrTypeSema(.{ | |
| 20447 | 20634 | .child = tuple_ty, |
| 20448 | 20635 | .flags = .{ .address_space = target_util.defaultAddressSpace(target, .local) }, |
| 20449 | 20636 | }); |
| ... | ... | @@ -20457,7 +20644,7 @@ fn structInitAnon( |
| 20457 | 20644 | }; |
| 20458 | 20645 | extra_index = item.end; |
| 20459 | 20646 | |
| 20460 | const field_ptr_ty = try mod.ptrTypeSema(.{ | |
| 20647 | const field_ptr_ty = try pt.ptrTypeSema(.{ | |
| 20461 | 20648 | .child = field_ty, |
| 20462 | 20649 | .flags = .{ .address_space = target_util.defaultAddressSpace(target, .local) }, |
| 20463 | 20650 | }); |
| ... | ... | @@ -20491,7 +20678,8 @@ fn zirArrayInit( |
| 20491 | 20678 | inst: Zir.Inst.Index, |
| 20492 | 20679 | is_ref: bool, |
| 20493 | 20680 | ) CompileError!Air.Inst.Ref { |
| 20494 | const mod = sema.mod; | |
| 20681 | const pt = sema.pt; | |
| 20682 | const mod = pt.zcu; | |
| 20495 | 20683 | const gpa = sema.gpa; |
| 20496 | 20684 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 20497 | 20685 | const src = block.nodeOffset(inst_data.src_node); |
| ... | ... | @@ -20550,8 +20738,8 @@ fn zirArrayInit( |
| 20550 | 20738 | dest.* = try sema.coerce(block, elem_ty, resolved_arg, elem_src); |
| 20551 | 20739 | if (is_tuple) { |
| 20552 | 20740 | if (array_ty.structFieldIsComptime(i, mod)) |
| 20553 | try array_ty.resolveStructFieldInits(mod); | |
| 20554 | if (try array_ty.structFieldValueComptime(mod, i)) |field_val| { | |
| 20741 | try array_ty.resolveStructFieldInits(pt); | |
| 20742 | if (try array_ty.structFieldValueComptime(pt, i)) |field_val| { | |
| 20555 | 20743 | const init_val = try sema.resolveValue(dest.*) orelse { |
| 20556 | 20744 | return sema.failWithNeededComptime(block, elem_src, .{ |
| 20557 | 20745 | .needed_comptime_reason = "value stored in comptime field must be comptime-known", |
| ... | ... | @@ -20581,7 +20769,7 @@ fn zirArrayInit( |
| 20581 | 20769 | // We checked that all args are comptime above. |
| 20582 | 20770 | val.* = (sema.resolveValue(arg) catch unreachable).?.toIntern(); |
| 20583 | 20771 | } |
| 20584 | const arr_val = try mod.intern(.{ .aggregate = .{ | |
| 20772 | const arr_val = try pt.intern(.{ .aggregate = .{ | |
| 20585 | 20773 | .ty = array_ty.toIntern(), |
| 20586 | 20774 | .storage = .{ .elems = elem_vals }, |
| 20587 | 20775 | } }); |
| ... | ... | @@ -20597,7 +20785,7 @@ fn zirArrayInit( |
| 20597 | 20785 | |
| 20598 | 20786 | if (is_ref) { |
| 20599 | 20787 | const target = mod.getTarget(); |
| 20600 | const alloc_ty = try mod.ptrTypeSema(.{ | |
| 20788 | const alloc_ty = try pt.ptrTypeSema(.{ | |
| 20601 | 20789 | .child = result_ty.toIntern(), |
| 20602 | 20790 | .flags = .{ .address_space = target_util.defaultAddressSpace(target, .local) }, |
| 20603 | 20791 | }); |
| ... | ... | @@ -20606,27 +20794,27 @@ fn zirArrayInit( |
| 20606 | 20794 | |
| 20607 | 20795 | if (is_tuple) { |
| 20608 | 20796 | for (resolved_args, 0..) |arg, i| { |
| 20609 | const elem_ptr_ty = try mod.ptrTypeSema(.{ | |
| 20797 | const elem_ptr_ty = try pt.ptrTypeSema(.{ | |
| 20610 | 20798 | .child = array_ty.structFieldType(i, mod).toIntern(), |
| 20611 | 20799 | .flags = .{ .address_space = target_util.defaultAddressSpace(target, .local) }, |
| 20612 | 20800 | }); |
| 20613 | 20801 | const elem_ptr_ty_ref = Air.internedToRef(elem_ptr_ty.toIntern()); |
| 20614 | 20802 | |
| 20615 | const index = try mod.intRef(Type.usize, i); | |
| 20803 | const index = try pt.intRef(Type.usize, i); | |
| 20616 | 20804 | const elem_ptr = try block.addPtrElemPtrTypeRef(base_ptr, index, elem_ptr_ty_ref); |
| 20617 | 20805 | _ = try block.addBinOp(.store, elem_ptr, arg); |
| 20618 | 20806 | } |
| 20619 | 20807 | return sema.makePtrConst(block, alloc); |
| 20620 | 20808 | } |
| 20621 | 20809 | |
| 20622 | const elem_ptr_ty = try mod.ptrTypeSema(.{ | |
| 20810 | const elem_ptr_ty = try pt.ptrTypeSema(.{ | |
| 20623 | 20811 | .child = array_ty.elemType2(mod).toIntern(), |
| 20624 | 20812 | .flags = .{ .address_space = target_util.defaultAddressSpace(target, .local) }, |
| 20625 | 20813 | }); |
| 20626 | 20814 | const elem_ptr_ty_ref = Air.internedToRef(elem_ptr_ty.toIntern()); |
| 20627 | 20815 | |
| 20628 | 20816 | for (resolved_args, 0..) |arg, i| { |
| 20629 | const index = try mod.intRef(Type.usize, i); | |
| 20817 | const index = try pt.intRef(Type.usize, i); | |
| 20630 | 20818 | const elem_ptr = try block.addPtrElemPtrTypeRef(base_ptr, index, elem_ptr_ty_ref); |
| 20631 | 20819 | _ = try block.addBinOp(.store, elem_ptr, arg); |
| 20632 | 20820 | } |
| ... | ... | @@ -20656,7 +20844,8 @@ fn arrayInitAnon( |
| 20656 | 20844 | operands: []const Zir.Inst.Ref, |
| 20657 | 20845 | is_ref: bool, |
| 20658 | 20846 | ) CompileError!Air.Inst.Ref { |
| 20659 | const mod = sema.mod; | |
| 20847 | const pt = sema.pt; | |
| 20848 | const mod = pt.zcu; | |
| 20660 | 20849 | const gpa = sema.gpa; |
| 20661 | 20850 | const ip = &mod.intern_pool; |
| 20662 | 20851 | |
| ... | ... | @@ -20689,14 +20878,14 @@ fn arrayInitAnon( |
| 20689 | 20878 | break :rs runtime_src; |
| 20690 | 20879 | }; |
| 20691 | 20880 | |
| 20692 | const tuple_ty = try ip.getAnonStructType(gpa, .{ | |
| 20881 | const tuple_ty = try ip.getAnonStructType(gpa, pt.tid, .{ | |
| 20693 | 20882 | .types = types, |
| 20694 | 20883 | .values = values, |
| 20695 | 20884 | .names = &.{}, |
| 20696 | 20885 | }); |
| 20697 | 20886 | |
| 20698 | 20887 | const runtime_src = opt_runtime_src orelse { |
| 20699 | const tuple_val = try mod.intern(.{ .aggregate = .{ | |
| 20888 | const tuple_val = try pt.intern(.{ .aggregate = .{ | |
| 20700 | 20889 | .ty = tuple_ty, |
| 20701 | 20890 | .storage = .{ .elems = values }, |
| 20702 | 20891 | } }); |
| ... | ... | @@ -20706,15 +20895,15 @@ fn arrayInitAnon( |
| 20706 | 20895 | try sema.requireRuntimeBlock(block, src, runtime_src); |
| 20707 | 20896 | |
| 20708 | 20897 | if (is_ref) { |
| 20709 | const target = sema.mod.getTarget(); | |
| 20710 | const alloc_ty = try mod.ptrTypeSema(.{ | |
| 20898 | const target = sema.pt.zcu.getTarget(); | |
| 20899 | const alloc_ty = try pt.ptrTypeSema(.{ | |
| 20711 | 20900 | .child = tuple_ty, |
| 20712 | 20901 | .flags = .{ .address_space = target_util.defaultAddressSpace(target, .local) }, |
| 20713 | 20902 | }); |
| 20714 | 20903 | const alloc = try block.addTy(.alloc, alloc_ty); |
| 20715 | 20904 | for (operands, 0..) |operand, i_usize| { |
| 20716 | 20905 | const i: u32 = @intCast(i_usize); |
| 20717 | const field_ptr_ty = try mod.ptrTypeSema(.{ | |
| 20906 | const field_ptr_ty = try pt.ptrTypeSema(.{ | |
| 20718 | 20907 | .child = types[i], |
| 20719 | 20908 | .flags = .{ .address_space = target_util.defaultAddressSpace(target, .local) }, |
| 20720 | 20909 | }); |
| ... | ... | @@ -20752,7 +20941,8 @@ fn zirFieldTypeRef(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 20752 | 20941 | } |
| 20753 | 20942 | |
| 20754 | 20943 | fn zirStructInitFieldType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 20755 | const mod = sema.mod; | |
| 20944 | const pt = sema.pt; | |
| 20945 | const mod = pt.zcu; | |
| 20756 | 20946 | const ip = &mod.intern_pool; |
| 20757 | 20947 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 20758 | 20948 | const extra = sema.code.extraData(Zir.Inst.FieldType, inst_data.payload_index).data; |
| ... | ... | @@ -20780,11 +20970,12 @@ fn fieldType( |
| 20780 | 20970 | field_src: LazySrcLoc, |
| 20781 | 20971 | ty_src: LazySrcLoc, |
| 20782 | 20972 | ) CompileError!Air.Inst.Ref { |
| 20783 | const mod = sema.mod; | |
| 20973 | const pt = sema.pt; | |
| 20974 | const mod = pt.zcu; | |
| 20784 | 20975 | const ip = &mod.intern_pool; |
| 20785 | 20976 | var cur_ty = aggregate_ty; |
| 20786 | 20977 | while (true) { |
| 20787 | try cur_ty.resolveFields(mod); | |
| 20978 | try cur_ty.resolveFields(pt); | |
| 20788 | 20979 | switch (cur_ty.zigTypeTag(mod)) { |
| 20789 | 20980 | .Struct => switch (ip.indexToKey(cur_ty.toIntern())) { |
| 20790 | 20981 | .anon_struct_type => |anon_struct| { |
| ... | ... | @@ -20823,7 +21014,7 @@ fn fieldType( |
| 20823 | 21014 | else => {}, |
| 20824 | 21015 | } |
| 20825 | 21016 | return sema.fail(block, ty_src, "expected struct or union; found '{}'", .{ |
| 20826 | cur_ty.fmt(sema.mod), | |
| 21017 | cur_ty.fmt(pt), | |
| 20827 | 21018 | }); |
| 20828 | 21019 | } |
| 20829 | 21020 | } |
| ... | ... | @@ -20833,12 +21024,13 @@ fn zirErrorReturnTrace(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref { |
| 20833 | 21024 | } |
| 20834 | 21025 | |
| 20835 | 21026 | fn getErrorReturnTrace(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref { |
| 20836 | const mod = sema.mod; | |
| 21027 | const pt = sema.pt; | |
| 21028 | const mod = pt.zcu; | |
| 20837 | 21029 | const ip = &mod.intern_pool; |
| 20838 | const stack_trace_ty = try mod.getBuiltinType("StackTrace"); | |
| 20839 | try stack_trace_ty.resolveFields(mod); | |
| 20840 | const ptr_stack_trace_ty = try mod.singleMutPtrType(stack_trace_ty); | |
| 20841 | const opt_ptr_stack_trace_ty = try mod.optionalType(ptr_stack_trace_ty.toIntern()); | |
| 21030 | const stack_trace_ty = try pt.getBuiltinType("StackTrace"); | |
| 21031 | try stack_trace_ty.resolveFields(pt); | |
| 21032 | const ptr_stack_trace_ty = try pt.singleMutPtrType(stack_trace_ty); | |
| 21033 | const opt_ptr_stack_trace_ty = try pt.optionalType(ptr_stack_trace_ty.toIntern()); | |
| 20842 | 21034 | |
| 20843 | 21035 | if (sema.owner_func_index != .none and |
| 20844 | 21036 | ip.funcAnalysis(sema.owner_func_index).calls_or_awaits_errorable_fn and |
| ... | ... | @@ -20846,7 +21038,7 @@ fn getErrorReturnTrace(sema: *Sema, block: *Block) CompileError!Air.Inst.Ref { |
| 20846 | 21038 | { |
| 20847 | 21039 | return block.addTy(.err_return_trace, opt_ptr_stack_trace_ty); |
| 20848 | 21040 | } |
| 20849 | return Air.internedToRef((try mod.intern(.{ .opt = .{ | |
| 21041 | return Air.internedToRef((try pt.intern(.{ .opt = .{ | |
| 20850 | 21042 | .ty = opt_ptr_stack_trace_ty.toIntern(), |
| 20851 | 21043 | .val = .none, |
| 20852 | 21044 | } }))); |
| ... | ... | @@ -20862,19 +21054,20 @@ fn zirFrame( |
| 20862 | 21054 | } |
| 20863 | 21055 | |
| 20864 | 21056 | fn zirAlignOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 20865 | const mod = sema.mod; | |
| 21057 | const pt = sema.pt; | |
| 20866 | 21058 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 20867 | 21059 | const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| 20868 | 21060 | const ty = try sema.resolveType(block, operand_src, inst_data.operand); |
| 20869 | if (ty.isNoReturn(mod)) { | |
| 20870 | return sema.fail(block, operand_src, "no align available for type '{}'", .{ty.fmt(sema.mod)}); | |
| 21061 | if (ty.isNoReturn(pt.zcu)) { | |
| 21062 | return sema.fail(block, operand_src, "no align available for type '{}'", .{ty.fmt(pt)}); | |
| 20871 | 21063 | } |
| 20872 | const val = try ty.lazyAbiAlignment(mod); | |
| 21064 | const val = try ty.lazyAbiAlignment(pt); | |
| 20873 | 21065 | return Air.internedToRef(val.toIntern()); |
| 20874 | 21066 | } |
| 20875 | 21067 | |
| 20876 | 21068 | fn zirIntFromBool(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 20877 | const mod = sema.mod; | |
| 21069 | const pt = sema.pt; | |
| 21070 | const mod = pt.zcu; | |
| 20878 | 21071 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 20879 | 21072 | const src = block.nodeOffset(inst_data.src_node); |
| 20880 | 21073 | const operand = try sema.resolveInst(inst_data.operand); |
| ... | ... | @@ -20886,25 +21079,25 @@ fn zirIntFromBool(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 20886 | 21079 | } |
| 20887 | 21080 | if (try sema.resolveValue(operand)) |val| { |
| 20888 | 21081 | if (!is_vector) { |
| 20889 | if (val.isUndef(mod)) return mod.undefRef(Type.u1); | |
| 20890 | if (val.toBool()) return Air.internedToRef((try mod.intValue(Type.u1, 1)).toIntern()); | |
| 20891 | return Air.internedToRef((try mod.intValue(Type.u1, 0)).toIntern()); | |
| 21082 | if (val.isUndef(mod)) return pt.undefRef(Type.u1); | |
| 21083 | if (val.toBool()) return Air.internedToRef((try pt.intValue(Type.u1, 1)).toIntern()); | |
| 21084 | return Air.internedToRef((try pt.intValue(Type.u1, 0)).toIntern()); | |
| 20892 | 21085 | } |
| 20893 | 21086 | const len = operand_ty.vectorLen(mod); |
| 20894 | const dest_ty = try mod.vectorType(.{ .child = .u1_type, .len = len }); | |
| 20895 | if (val.isUndef(mod)) return mod.undefRef(dest_ty); | |
| 21087 | const dest_ty = try pt.vectorType(.{ .child = .u1_type, .len = len }); | |
| 21088 | if (val.isUndef(mod)) return pt.undefRef(dest_ty); | |
| 20896 | 21089 | const new_elems = try sema.arena.alloc(InternPool.Index, len); |
| 20897 | 21090 | for (new_elems, 0..) |*new_elem, i| { |
| 20898 | const old_elem = try val.elemValue(mod, i); | |
| 21091 | const old_elem = try val.elemValue(pt, i); | |
| 20899 | 21092 | const new_val = if (old_elem.isUndef(mod)) |
| 20900 | try mod.undefValue(Type.u1) | |
| 21093 | try pt.undefValue(Type.u1) | |
| 20901 | 21094 | else if (old_elem.toBool()) |
| 20902 | try mod.intValue(Type.u1, 1) | |
| 21095 | try pt.intValue(Type.u1, 1) | |
| 20903 | 21096 | else |
| 20904 | try mod.intValue(Type.u1, 0); | |
| 21097 | try pt.intValue(Type.u1, 0); | |
| 20905 | 21098 | new_elem.* = new_val.toIntern(); |
| 20906 | 21099 | } |
| 20907 | return Air.internedToRef(try mod.intern(.{ .aggregate = .{ | |
| 21100 | return Air.internedToRef(try pt.intern(.{ .aggregate = .{ | |
| 20908 | 21101 | .ty = dest_ty.toIntern(), |
| 20909 | 21102 | .storage = .{ .elems = new_elems }, |
| 20910 | 21103 | } })); |
| ... | ... | @@ -20913,10 +21106,10 @@ fn zirIntFromBool(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 20913 | 21106 | return block.addUnOp(.int_from_bool, operand); |
| 20914 | 21107 | } |
| 20915 | 21108 | const len = operand_ty.vectorLen(mod); |
| 20916 | const dest_ty = try mod.vectorType(.{ .child = .u1_type, .len = len }); | |
| 21109 | const dest_ty = try pt.vectorType(.{ .child = .u1_type, .len = len }); | |
| 20917 | 21110 | const new_elems = try sema.arena.alloc(Air.Inst.Ref, len); |
| 20918 | 21111 | for (new_elems, 0..) |*new_elem, i| { |
| 20919 | const idx_ref = try mod.intRef(Type.usize, i); | |
| 21112 | const idx_ref = try pt.intRef(Type.usize, i); | |
| 20920 | 21113 | const old_elem = try block.addBinOp(.array_elem_val, operand, idx_ref); |
| 20921 | 21114 | new_elem.* = try block.addUnOp(.int_from_bool, old_elem); |
| 20922 | 21115 | } |
| ... | ... | @@ -20930,7 +21123,7 @@ fn zirErrorName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 20930 | 21123 | const operand = try sema.coerce(block, Type.anyerror, uncoerced_operand, operand_src); |
| 20931 | 21124 | |
| 20932 | 21125 | if (try sema.resolveDefinedValue(block, operand_src, operand)) |val| { |
| 20933 | const err_name = sema.mod.intern_pool.indexToKey(val.toIntern()).err.name; | |
| 21126 | const err_name = sema.pt.zcu.intern_pool.indexToKey(val.toIntern()).err.name; | |
| 20934 | 21127 | return sema.addNullTerminatedStrLit(err_name); |
| 20935 | 21128 | } |
| 20936 | 21129 | |
| ... | ... | @@ -20944,7 +21137,8 @@ fn zirAbs( |
| 20944 | 21137 | block: *Block, |
| 20945 | 21138 | inst: Zir.Inst.Index, |
| 20946 | 21139 | ) CompileError!Air.Inst.Ref { |
| 20947 | const mod = sema.mod; | |
| 21140 | const pt = sema.pt; | |
| 21141 | const mod = pt.zcu; | |
| 20948 | 21142 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 20949 | 21143 | const operand = try sema.resolveInst(inst_data.operand); |
| 20950 | 21144 | const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| ... | ... | @@ -20953,12 +21147,12 @@ fn zirAbs( |
| 20953 | 21147 | |
| 20954 | 21148 | const result_ty = switch (scalar_ty.zigTypeTag(mod)) { |
| 20955 | 21149 | .ComptimeFloat, .Float, .ComptimeInt => operand_ty, |
| 20956 | .Int => if (scalar_ty.isSignedInt(mod)) try operand_ty.toUnsigned(mod) else return operand, | |
| 21150 | .Int => if (scalar_ty.isSignedInt(mod)) try operand_ty.toUnsigned(pt) else return operand, | |
| 20957 | 21151 | else => return sema.fail( |
| 20958 | 21152 | block, |
| 20959 | 21153 | operand_src, |
| 20960 | 21154 | "expected integer, float, or vector of either integers or floats, found '{}'", |
| 20961 | .{operand_ty.fmt(mod)}, | |
| 21155 | .{operand_ty.fmt(pt)}, | |
| 20962 | 21156 | ), |
| 20963 | 21157 | }; |
| 20964 | 21158 | |
| ... | ... | @@ -20972,30 +21166,31 @@ fn maybeConstantUnaryMath( |
| 20972 | 21166 | sema: *Sema, |
| 20973 | 21167 | operand: Air.Inst.Ref, |
| 20974 | 21168 | result_ty: Type, |
| 20975 | comptime eval: fn (Value, Type, Allocator, *Module) Allocator.Error!Value, | |
| 21169 | comptime eval: fn (Value, Type, Allocator, Zcu.PerThread) Allocator.Error!Value, | |
| 20976 | 21170 | ) CompileError!?Air.Inst.Ref { |
| 20977 | const mod = sema.mod; | |
| 21171 | const pt = sema.pt; | |
| 21172 | const mod = pt.zcu; | |
| 20978 | 21173 | switch (result_ty.zigTypeTag(mod)) { |
| 20979 | 21174 | .Vector => if (try sema.resolveValue(operand)) |val| { |
| 20980 | 21175 | const scalar_ty = result_ty.scalarType(mod); |
| 20981 | 21176 | const vec_len = result_ty.vectorLen(mod); |
| 20982 | 21177 | if (val.isUndef(mod)) |
| 20983 | return try mod.undefRef(result_ty); | |
| 21178 | return try pt.undefRef(result_ty); | |
| 20984 | 21179 | |
| 20985 | 21180 | const elems = try sema.arena.alloc(InternPool.Index, vec_len); |
| 20986 | 21181 | for (elems, 0..) |*elem, i| { |
| 20987 | const elem_val = try val.elemValue(sema.mod, i); | |
| 20988 | elem.* = (try eval(elem_val, scalar_ty, sema.arena, sema.mod)).toIntern(); | |
| 21182 | const elem_val = try val.elemValue(pt, i); | |
| 21183 | elem.* = (try eval(elem_val, scalar_ty, sema.arena, pt)).toIntern(); | |
| 20989 | 21184 | } |
| 20990 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 21185 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 20991 | 21186 | .ty = result_ty.toIntern(), |
| 20992 | 21187 | .storage = .{ .elems = elems }, |
| 20993 | 21188 | } }))); |
| 20994 | 21189 | }, |
| 20995 | 21190 | else => if (try sema.resolveValue(operand)) |operand_val| { |
| 20996 | 21191 | if (operand_val.isUndef(mod)) |
| 20997 | return try mod.undefRef(result_ty); | |
| 20998 | const result_val = try eval(operand_val, result_ty, sema.arena, sema.mod); | |
| 21192 | return try pt.undefRef(result_ty); | |
| 21193 | const result_val = try eval(operand_val, result_ty, sema.arena, pt); | |
| 20999 | 21194 | return Air.internedToRef(result_val.toIntern()); |
| 21000 | 21195 | }, |
| 21001 | 21196 | } |
| ... | ... | @@ -21007,12 +21202,13 @@ fn zirUnaryMath( |
| 21007 | 21202 | block: *Block, |
| 21008 | 21203 | inst: Zir.Inst.Index, |
| 21009 | 21204 | air_tag: Air.Inst.Tag, |
| 21010 | comptime eval: fn (Value, Type, Allocator, *Module) Allocator.Error!Value, | |
| 21205 | comptime eval: fn (Value, Type, Allocator, Zcu.PerThread) Allocator.Error!Value, | |
| 21011 | 21206 | ) CompileError!Air.Inst.Ref { |
| 21012 | 21207 | const tracy = trace(@src()); |
| 21013 | 21208 | defer tracy.end(); |
| 21014 | 21209 | |
| 21015 | const mod = sema.mod; | |
| 21210 | const pt = sema.pt; | |
| 21211 | const mod = pt.zcu; | |
| 21016 | 21212 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 21017 | 21213 | const operand = try sema.resolveInst(inst_data.operand); |
| 21018 | 21214 | const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| ... | ... | @@ -21025,7 +21221,7 @@ fn zirUnaryMath( |
| 21025 | 21221 | block, |
| 21026 | 21222 | operand_src, |
| 21027 | 21223 | "expected vector of floats or float type, found '{}'", |
| 21028 | .{operand_ty.fmt(sema.mod)}, | |
| 21224 | .{operand_ty.fmt(pt)}, | |
| 21029 | 21225 | ), |
| 21030 | 21226 | } |
| 21031 | 21227 | |
| ... | ... | @@ -21041,10 +21237,11 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 21041 | 21237 | const src = block.nodeOffset(inst_data.src_node); |
| 21042 | 21238 | const operand = try sema.resolveInst(inst_data.operand); |
| 21043 | 21239 | const operand_ty = sema.typeOf(operand); |
| 21044 | const mod = sema.mod; | |
| 21240 | const pt = sema.pt; | |
| 21241 | const mod = pt.zcu; | |
| 21045 | 21242 | const ip = &mod.intern_pool; |
| 21046 | 21243 | |
| 21047 | try operand_ty.resolveLayout(mod); | |
| 21244 | try operand_ty.resolveLayout(pt); | |
| 21048 | 21245 | const enum_ty = switch (operand_ty.zigTypeTag(mod)) { |
| 21049 | 21246 | .EnumLiteral => { |
| 21050 | 21247 | const val = try sema.resolveConstDefinedValue(block, LazySrcLoc.unneeded, operand, undefined); |
| ... | ... | @@ -21053,9 +21250,9 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 21053 | 21250 | }, |
| 21054 | 21251 | .Enum => operand_ty, |
| 21055 | 21252 | .Union => operand_ty.unionTagType(mod) orelse |
| 21056 | return sema.fail(block, src, "union '{}' is untagged", .{operand_ty.fmt(sema.mod)}), | |
| 21253 | return sema.fail(block, src, "union '{}' is untagged", .{operand_ty.fmt(pt)}), | |
| 21057 | 21254 | else => return sema.fail(block, operand_src, "expected enum or union; found '{}'", .{ |
| 21058 | operand_ty.fmt(mod), | |
| 21255 | operand_ty.fmt(pt), | |
| 21059 | 21256 | }), |
| 21060 | 21257 | }; |
| 21061 | 21258 | if (enum_ty.enumFieldCount(mod) == 0) { |
| ... | ... | @@ -21063,7 +21260,7 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 21063 | 21260 | // it prevents a crash. |
| 21064 | 21261 | // https://github.com/ziglang/zig/issues/15909 |
| 21065 | 21262 | return sema.fail(block, operand_src, "cannot get @tagName of empty enum '{}'", .{ |
| 21066 | enum_ty.fmt(mod), | |
| 21263 | enum_ty.fmt(pt), | |
| 21067 | 21264 | }); |
| 21068 | 21265 | } |
| 21069 | 21266 | const enum_decl_index = enum_ty.getOwnerDecl(mod); |
| ... | ... | @@ -21072,7 +21269,7 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 21072 | 21269 | const field_index = enum_ty.enumTagFieldIndex(val, mod) orelse { |
| 21073 | 21270 | const msg = msg: { |
| 21074 | 21271 | const msg = try sema.errMsg(src, "no field with value '{}' in enum '{}'", .{ |
| 21075 | val.fmtValue(sema.mod, sema), mod.declPtr(enum_decl_index).name.fmt(ip), | |
| 21272 | val.fmtValue(pt, sema), mod.declPtr(enum_decl_index).name.fmt(ip), | |
| 21076 | 21273 | }); |
| 21077 | 21274 | errdefer msg.destroy(sema.gpa); |
| 21078 | 21275 | try sema.errNote(enum_ty.srcLoc(mod), msg, "declared here", .{}); |
| ... | ... | @@ -21085,7 +21282,7 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 21085 | 21282 | return sema.addNullTerminatedStrLit(field_name); |
| 21086 | 21283 | } |
| 21087 | 21284 | try sema.requireRuntimeBlock(block, src, operand_src); |
| 21088 | if (block.wantSafety() and sema.mod.backendSupportsFeature(.is_named_enum_value)) { | |
| 21285 | if (block.wantSafety() and mod.backendSupportsFeature(.is_named_enum_value)) { | |
| 21089 | 21286 | const ok = try block.addUnOp(.is_named_enum_value, casted_operand); |
| 21090 | 21287 | try sema.addSafetyCheck(block, src, ok, .invalid_enum_value); |
| 21091 | 21288 | } |
| ... | ... | @@ -21101,7 +21298,8 @@ fn zirReify( |
| 21101 | 21298 | extended: Zir.Inst.Extended.InstData, |
| 21102 | 21299 | inst: Zir.Inst.Index, |
| 21103 | 21300 | ) CompileError!Air.Inst.Ref { |
| 21104 | const mod = sema.mod; | |
| 21301 | const pt = sema.pt; | |
| 21302 | const mod = pt.zcu; | |
| 21105 | 21303 | const gpa = sema.gpa; |
| 21106 | 21304 | const ip = &mod.intern_pool; |
| 21107 | 21305 | const name_strategy: Zir.Inst.NameStrategy = @enumFromInt(extended.small); |
| ... | ... | @@ -21120,7 +21318,7 @@ fn zirReify( |
| 21120 | 21318 | }, |
| 21121 | 21319 | }, |
| 21122 | 21320 | }; |
| 21123 | const type_info_ty = try mod.getBuiltinType("Type"); | |
| 21321 | const type_info_ty = try pt.getBuiltinType("Type"); | |
| 21124 | 21322 | const uncasted_operand = try sema.resolveInst(extra.operand); |
| 21125 | 21323 | const type_info = try sema.coerce(block, type_info_ty, uncasted_operand, operand_src); |
| 21126 | 21324 | const val = try sema.resolveConstDefinedValue(block, operand_src, type_info, .{ |
| ... | ... | @@ -21145,36 +21343,36 @@ fn zirReify( |
| 21145 | 21343 | .Int => { |
| 21146 | 21344 | const struct_type = ip.loadStructType(ip.typeOf(union_val.val)); |
| 21147 | 21345 | const signedness_val = try Value.fromInterned(union_val.val).fieldValue( |
| 21148 | mod, | |
| 21346 | pt, | |
| 21149 | 21347 | struct_type.nameIndex(ip, try ip.getOrPutString(gpa, "signedness", .no_embedded_nulls)).?, |
| 21150 | 21348 | ); |
| 21151 | 21349 | const bits_val = try Value.fromInterned(union_val.val).fieldValue( |
| 21152 | mod, | |
| 21350 | pt, | |
| 21153 | 21351 | struct_type.nameIndex(ip, try ip.getOrPutString(gpa, "bits", .no_embedded_nulls)).?, |
| 21154 | 21352 | ); |
| 21155 | 21353 | |
| 21156 | 21354 | const signedness = mod.toEnum(std.builtin.Signedness, signedness_val); |
| 21157 | const bits: u16 = @intCast(try bits_val.toUnsignedIntSema(mod)); | |
| 21158 | const ty = try mod.intType(signedness, bits); | |
| 21355 | const bits: u16 = @intCast(try bits_val.toUnsignedIntSema(pt)); | |
| 21356 | const ty = try pt.intType(signedness, bits); | |
| 21159 | 21357 | return Air.internedToRef(ty.toIntern()); |
| 21160 | 21358 | }, |
| 21161 | 21359 | .Vector => { |
| 21162 | 21360 | const struct_type = ip.loadStructType(ip.typeOf(union_val.val)); |
| 21163 | const len_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21361 | const len_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21164 | 21362 | ip, |
| 21165 | 21363 | try ip.getOrPutString(gpa, "len", .no_embedded_nulls), |
| 21166 | 21364 | ).?); |
| 21167 | const child_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21365 | const child_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21168 | 21366 | ip, |
| 21169 | 21367 | try ip.getOrPutString(gpa, "child", .no_embedded_nulls), |
| 21170 | 21368 | ).?); |
| 21171 | 21369 | |
| 21172 | const len: u32 = @intCast(try len_val.toUnsignedIntSema(mod)); | |
| 21370 | const len: u32 = @intCast(try len_val.toUnsignedIntSema(pt)); | |
| 21173 | 21371 | const child_ty = child_val.toType(); |
| 21174 | 21372 | |
| 21175 | 21373 | try sema.checkVectorElemType(block, src, child_ty); |
| 21176 | 21374 | |
| 21177 | const ty = try mod.vectorType(.{ | |
| 21375 | const ty = try pt.vectorType(.{ | |
| 21178 | 21376 | .len = len, |
| 21179 | 21377 | .child = child_ty.toIntern(), |
| 21180 | 21378 | }); |
| ... | ... | @@ -21182,12 +21380,12 @@ fn zirReify( |
| 21182 | 21380 | }, |
| 21183 | 21381 | .Float => { |
| 21184 | 21382 | const struct_type = ip.loadStructType(ip.typeOf(union_val.val)); |
| 21185 | const bits_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21383 | const bits_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21186 | 21384 | ip, |
| 21187 | 21385 | try ip.getOrPutString(gpa, "bits", .no_embedded_nulls), |
| 21188 | 21386 | ).?); |
| 21189 | 21387 | |
| 21190 | const bits: u16 = @intCast(try bits_val.toUnsignedIntSema(mod)); | |
| 21388 | const bits: u16 = @intCast(try bits_val.toUnsignedIntSema(pt)); | |
| 21191 | 21389 | const ty = switch (bits) { |
| 21192 | 21390 | 16 => Type.f16, |
| 21193 | 21391 | 32 => Type.f32, |
| ... | ... | @@ -21200,35 +21398,35 @@ fn zirReify( |
| 21200 | 21398 | }, |
| 21201 | 21399 | .Pointer => { |
| 21202 | 21400 | const struct_type = ip.loadStructType(ip.typeOf(union_val.val)); |
| 21203 | const size_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21401 | const size_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21204 | 21402 | ip, |
| 21205 | 21403 | try ip.getOrPutString(gpa, "size", .no_embedded_nulls), |
| 21206 | 21404 | ).?); |
| 21207 | const is_const_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21405 | const is_const_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21208 | 21406 | ip, |
| 21209 | 21407 | try ip.getOrPutString(gpa, "is_const", .no_embedded_nulls), |
| 21210 | 21408 | ).?); |
| 21211 | const is_volatile_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21409 | const is_volatile_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21212 | 21410 | ip, |
| 21213 | 21411 | try ip.getOrPutString(gpa, "is_volatile", .no_embedded_nulls), |
| 21214 | 21412 | ).?); |
| 21215 | const alignment_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21413 | const alignment_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21216 | 21414 | ip, |
| 21217 | 21415 | try ip.getOrPutString(gpa, "alignment", .no_embedded_nulls), |
| 21218 | 21416 | ).?); |
| 21219 | const address_space_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21417 | const address_space_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21220 | 21418 | ip, |
| 21221 | 21419 | try ip.getOrPutString(gpa, "address_space", .no_embedded_nulls), |
| 21222 | 21420 | ).?); |
| 21223 | const child_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21421 | const child_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21224 | 21422 | ip, |
| 21225 | 21423 | try ip.getOrPutString(gpa, "child", .no_embedded_nulls), |
| 21226 | 21424 | ).?); |
| 21227 | const is_allowzero_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21425 | const is_allowzero_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21228 | 21426 | ip, |
| 21229 | 21427 | try ip.getOrPutString(gpa, "is_allowzero", .no_embedded_nulls), |
| 21230 | 21428 | ).?); |
| 21231 | const sentinel_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21429 | const sentinel_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21232 | 21430 | ip, |
| 21233 | 21431 | try ip.getOrPutString(gpa, "sentinel", .no_embedded_nulls), |
| 21234 | 21432 | ).?); |
| ... | ... | @@ -21237,7 +21435,7 @@ fn zirReify( |
| 21237 | 21435 | return sema.fail(block, src, "alignment must fit in 'u32'", .{}); |
| 21238 | 21436 | } |
| 21239 | 21437 | |
| 21240 | const alignment_val_int = (try alignment_val.getUnsignedIntAdvanced(mod, .sema)).?; | |
| 21438 | const alignment_val_int = (try alignment_val.getUnsignedIntAdvanced(pt, .sema)).?; | |
| 21241 | 21439 | if (alignment_val_int > 0 and !math.isPowerOfTwo(alignment_val_int)) { |
| 21242 | 21440 | return sema.fail(block, src, "alignment value '{d}' is not a power of two or zero", .{alignment_val_int}); |
| 21243 | 21441 | } |
| ... | ... | @@ -21245,7 +21443,7 @@ fn zirReify( |
| 21245 | 21443 | |
| 21246 | 21444 | const elem_ty = child_val.toType(); |
| 21247 | 21445 | if (abi_align != .none) { |
| 21248 | try elem_ty.resolveLayout(mod); | |
| 21446 | try elem_ty.resolveLayout(pt); | |
| 21249 | 21447 | } |
| 21250 | 21448 | |
| 21251 | 21449 | const ptr_size = mod.toEnum(std.builtin.Type.Pointer.Size, size_val); |
| ... | ... | @@ -21256,7 +21454,7 @@ fn zirReify( |
| 21256 | 21454 | return sema.fail(block, src, "sentinels are only allowed on slices and unknown-length pointers", .{}); |
| 21257 | 21455 | } |
| 21258 | 21456 | const sentinel_ptr_val = sentinel_val.optionalValue(mod).?; |
| 21259 | const ptr_ty = try mod.singleMutPtrType(elem_ty); | |
| 21457 | const ptr_ty = try pt.singleMutPtrType(elem_ty); | |
| 21260 | 21458 | const sent_val = (try sema.pointerDeref(block, src, sentinel_ptr_val, ptr_ty)).?; |
| 21261 | 21459 | break :s sent_val.toIntern(); |
| 21262 | 21460 | } |
| ... | ... | @@ -21274,7 +21472,7 @@ fn zirReify( |
| 21274 | 21472 | } else if (ptr_size == .C) { |
| 21275 | 21473 | if (!try sema.validateExternType(elem_ty, .other)) { |
| 21276 | 21474 | const msg = msg: { |
| 21277 | const msg = try sema.errMsg(src, "C pointers cannot point to non-C-ABI-compatible type '{}'", .{elem_ty.fmt(mod)}); | |
| 21475 | const msg = try sema.errMsg(src, "C pointers cannot point to non-C-ABI-compatible type '{}'", .{elem_ty.fmt(pt)}); | |
| 21278 | 21476 | errdefer msg.destroy(gpa); |
| 21279 | 21477 | |
| 21280 | 21478 | try sema.explainWhyTypeIsNotExtern(msg, src, elem_ty, .other); |
| ... | ... | @@ -21289,7 +21487,7 @@ fn zirReify( |
| 21289 | 21487 | } |
| 21290 | 21488 | } |
| 21291 | 21489 | |
| 21292 | const ty = try mod.ptrTypeSema(.{ | |
| 21490 | const ty = try pt.ptrTypeSema(.{ | |
| 21293 | 21491 | .child = elem_ty.toIntern(), |
| 21294 | 21492 | .sentinel = actual_sentinel, |
| 21295 | 21493 | .flags = .{ |
| ... | ... | @@ -21305,27 +21503,27 @@ fn zirReify( |
| 21305 | 21503 | }, |
| 21306 | 21504 | .Array => { |
| 21307 | 21505 | const struct_type = ip.loadStructType(ip.typeOf(union_val.val)); |
| 21308 | const len_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21506 | const len_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21309 | 21507 | ip, |
| 21310 | 21508 | try ip.getOrPutString(gpa, "len", .no_embedded_nulls), |
| 21311 | 21509 | ).?); |
| 21312 | const child_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21510 | const child_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21313 | 21511 | ip, |
| 21314 | 21512 | try ip.getOrPutString(gpa, "child", .no_embedded_nulls), |
| 21315 | 21513 | ).?); |
| 21316 | const sentinel_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21514 | const sentinel_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21317 | 21515 | ip, |
| 21318 | 21516 | try ip.getOrPutString(gpa, "sentinel", .no_embedded_nulls), |
| 21319 | 21517 | ).?); |
| 21320 | 21518 | |
| 21321 | const len = try len_val.toUnsignedIntSema(mod); | |
| 21519 | const len = try len_val.toUnsignedIntSema(pt); | |
| 21322 | 21520 | const child_ty = child_val.toType(); |
| 21323 | 21521 | const sentinel = if (sentinel_val.optionalValue(mod)) |p| blk: { |
| 21324 | const ptr_ty = try mod.singleMutPtrType(child_ty); | |
| 21522 | const ptr_ty = try pt.singleMutPtrType(child_ty); | |
| 21325 | 21523 | break :blk (try sema.pointerDeref(block, src, p, ptr_ty)).?; |
| 21326 | 21524 | } else null; |
| 21327 | 21525 | |
| 21328 | const ty = try mod.arrayType(.{ | |
| 21526 | const ty = try pt.arrayType(.{ | |
| 21329 | 21527 | .len = len, |
| 21330 | 21528 | .sentinel = if (sentinel) |s| s.toIntern() else .none, |
| 21331 | 21529 | .child = child_ty.toIntern(), |
| ... | ... | @@ -21334,23 +21532,23 @@ fn zirReify( |
| 21334 | 21532 | }, |
| 21335 | 21533 | .Optional => { |
| 21336 | 21534 | const struct_type = ip.loadStructType(ip.typeOf(union_val.val)); |
| 21337 | const child_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21535 | const child_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21338 | 21536 | ip, |
| 21339 | 21537 | try ip.getOrPutString(gpa, "child", .no_embedded_nulls), |
| 21340 | 21538 | ).?); |
| 21341 | 21539 | |
| 21342 | 21540 | const child_ty = child_val.toType(); |
| 21343 | 21541 | |
| 21344 | const ty = try mod.optionalType(child_ty.toIntern()); | |
| 21542 | const ty = try pt.optionalType(child_ty.toIntern()); | |
| 21345 | 21543 | return Air.internedToRef(ty.toIntern()); |
| 21346 | 21544 | }, |
| 21347 | 21545 | .ErrorUnion => { |
| 21348 | 21546 | const struct_type = ip.loadStructType(ip.typeOf(union_val.val)); |
| 21349 | const error_set_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21547 | const error_set_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21350 | 21548 | ip, |
| 21351 | 21549 | try ip.getOrPutString(gpa, "error_set", .no_embedded_nulls), |
| 21352 | 21550 | ).?); |
| 21353 | const payload_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21551 | const payload_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21354 | 21552 | ip, |
| 21355 | 21553 | try ip.getOrPutString(gpa, "payload", .no_embedded_nulls), |
| 21356 | 21554 | ).?); |
| ... | ... | @@ -21362,7 +21560,7 @@ fn zirReify( |
| 21362 | 21560 | return sema.fail(block, src, "Type.ErrorUnion.error_set must be an error set type", .{}); |
| 21363 | 21561 | } |
| 21364 | 21562 | |
| 21365 | const ty = try mod.errorUnionType(error_set_ty, payload_ty); | |
| 21563 | const ty = try pt.errorUnionType(error_set_ty, payload_ty); | |
| 21366 | 21564 | return Air.internedToRef(ty.toIntern()); |
| 21367 | 21565 | }, |
| 21368 | 21566 | .ErrorSet => { |
| ... | ... | @@ -21377,9 +21575,9 @@ fn zirReify( |
| 21377 | 21575 | var names: InferredErrorSet.NameMap = .{}; |
| 21378 | 21576 | try names.ensureUnusedCapacity(sema.arena, len); |
| 21379 | 21577 | for (0..len) |i| { |
| 21380 | const elem_val = try names_val.elemValue(mod, i); | |
| 21578 | const elem_val = try names_val.elemValue(pt, i); | |
| 21381 | 21579 | const elem_struct_type = ip.loadStructType(ip.typeOf(elem_val.toIntern())); |
| 21382 | const name_val = try elem_val.fieldValue(mod, elem_struct_type.nameIndex( | |
| 21580 | const name_val = try elem_val.fieldValue(pt, elem_struct_type.nameIndex( | |
| 21383 | 21581 | ip, |
| 21384 | 21582 | try ip.getOrPutString(gpa, "name", .no_embedded_nulls), |
| 21385 | 21583 | ).?); |
| ... | ... | @@ -21396,28 +21594,28 @@ fn zirReify( |
| 21396 | 21594 | } |
| 21397 | 21595 | } |
| 21398 | 21596 | |
| 21399 | const ty = try mod.errorSetFromUnsortedNames(names.keys()); | |
| 21597 | const ty = try pt.errorSetFromUnsortedNames(names.keys()); | |
| 21400 | 21598 | return Air.internedToRef(ty.toIntern()); |
| 21401 | 21599 | }, |
| 21402 | 21600 | .Struct => { |
| 21403 | 21601 | const struct_type = ip.loadStructType(ip.typeOf(union_val.val)); |
| 21404 | const layout_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21602 | const layout_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21405 | 21603 | ip, |
| 21406 | 21604 | try ip.getOrPutString(gpa, "layout", .no_embedded_nulls), |
| 21407 | 21605 | ).?); |
| 21408 | const backing_integer_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21606 | const backing_integer_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21409 | 21607 | ip, |
| 21410 | 21608 | try ip.getOrPutString(gpa, "backing_integer", .no_embedded_nulls), |
| 21411 | 21609 | ).?); |
| 21412 | const fields_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21610 | const fields_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21413 | 21611 | ip, |
| 21414 | 21612 | try ip.getOrPutString(gpa, "fields", .no_embedded_nulls), |
| 21415 | 21613 | ).?); |
| 21416 | const decls_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21614 | const decls_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21417 | 21615 | ip, |
| 21418 | 21616 | try ip.getOrPutString(gpa, "decls", .no_embedded_nulls), |
| 21419 | 21617 | ).?); |
| 21420 | const is_tuple_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21618 | const is_tuple_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21421 | 21619 | ip, |
| 21422 | 21620 | try ip.getOrPutString(gpa, "is_tuple", .no_embedded_nulls), |
| 21423 | 21621 | ).?); |
| ... | ... | @@ -21425,7 +21623,7 @@ fn zirReify( |
| 21425 | 21623 | const layout = mod.toEnum(std.builtin.Type.ContainerLayout, layout_val); |
| 21426 | 21624 | |
| 21427 | 21625 | // Decls |
| 21428 | if (try decls_val.sliceLen(mod) > 0) { | |
| 21626 | if (try decls_val.sliceLen(pt) > 0) { | |
| 21429 | 21627 | return sema.fail(block, src, "reified structs must have no decls", .{}); |
| 21430 | 21628 | } |
| 21431 | 21629 | |
| ... | ... | @@ -21441,24 +21639,24 @@ fn zirReify( |
| 21441 | 21639 | }, |
| 21442 | 21640 | .Enum => { |
| 21443 | 21641 | const struct_type = ip.loadStructType(ip.typeOf(union_val.val)); |
| 21444 | const tag_type_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21642 | const tag_type_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21445 | 21643 | ip, |
| 21446 | 21644 | try ip.getOrPutString(gpa, "tag_type", .no_embedded_nulls), |
| 21447 | 21645 | ).?); |
| 21448 | const fields_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21646 | const fields_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21449 | 21647 | ip, |
| 21450 | 21648 | try ip.getOrPutString(gpa, "fields", .no_embedded_nulls), |
| 21451 | 21649 | ).?); |
| 21452 | const decls_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21650 | const decls_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21453 | 21651 | ip, |
| 21454 | 21652 | try ip.getOrPutString(gpa, "decls", .no_embedded_nulls), |
| 21455 | 21653 | ).?); |
| 21456 | const is_exhaustive_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21654 | const is_exhaustive_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21457 | 21655 | ip, |
| 21458 | 21656 | try ip.getOrPutString(gpa, "is_exhaustive", .no_embedded_nulls), |
| 21459 | 21657 | ).?); |
| 21460 | 21658 | |
| 21461 | if (try decls_val.sliceLen(mod) > 0) { | |
| 21659 | if (try decls_val.sliceLen(pt) > 0) { | |
| 21462 | 21660 | return sema.fail(block, src, "reified enums must have no decls", .{}); |
| 21463 | 21661 | } |
| 21464 | 21662 | |
| ... | ... | @@ -21470,17 +21668,17 @@ fn zirReify( |
| 21470 | 21668 | }, |
| 21471 | 21669 | .Opaque => { |
| 21472 | 21670 | const struct_type = ip.loadStructType(ip.typeOf(union_val.val)); |
| 21473 | const decls_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21671 | const decls_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21474 | 21672 | ip, |
| 21475 | 21673 | try ip.getOrPutString(gpa, "decls", .no_embedded_nulls), |
| 21476 | 21674 | ).?); |
| 21477 | 21675 | |
| 21478 | 21676 | // Decls |
| 21479 | if (try decls_val.sliceLen(mod) > 0) { | |
| 21677 | if (try decls_val.sliceLen(pt) > 0) { | |
| 21480 | 21678 | return sema.fail(block, src, "reified opaque must have no decls", .{}); |
| 21481 | 21679 | } |
| 21482 | 21680 | |
| 21483 | const wip_ty = switch (try ip.getOpaqueType(gpa, .{ | |
| 21681 | const wip_ty = switch (try ip.getOpaqueType(gpa, pt.tid, .{ | |
| 21484 | 21682 | .has_namespace = false, |
| 21485 | 21683 | .key = .{ .reified = .{ |
| 21486 | 21684 | .zir_index = try block.trackZir(inst), |
| ... | ... | @@ -21501,30 +21699,30 @@ fn zirReify( |
| 21501 | 21699 | mod.declPtr(new_decl_index).owns_tv = true; |
| 21502 | 21700 | errdefer mod.abortAnonDecl(new_decl_index); |
| 21503 | 21701 | |
| 21504 | try mod.finalizeAnonDecl(new_decl_index); | |
| 21702 | try pt.finalizeAnonDecl(new_decl_index); | |
| 21505 | 21703 | |
| 21506 | 21704 | return Air.internedToRef(wip_ty.finish(ip, new_decl_index, .none)); |
| 21507 | 21705 | }, |
| 21508 | 21706 | .Union => { |
| 21509 | 21707 | const struct_type = ip.loadStructType(ip.typeOf(union_val.val)); |
| 21510 | const layout_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21708 | const layout_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21511 | 21709 | ip, |
| 21512 | 21710 | try ip.getOrPutString(gpa, "layout", .no_embedded_nulls), |
| 21513 | 21711 | ).?); |
| 21514 | const tag_type_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21712 | const tag_type_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21515 | 21713 | ip, |
| 21516 | 21714 | try ip.getOrPutString(gpa, "tag_type", .no_embedded_nulls), |
| 21517 | 21715 | ).?); |
| 21518 | const fields_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21716 | const fields_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21519 | 21717 | ip, |
| 21520 | 21718 | try ip.getOrPutString(gpa, "fields", .no_embedded_nulls), |
| 21521 | 21719 | ).?); |
| 21522 | const decls_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21720 | const decls_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21523 | 21721 | ip, |
| 21524 | 21722 | try ip.getOrPutString(gpa, "decls", .no_embedded_nulls), |
| 21525 | 21723 | ).?); |
| 21526 | 21724 | |
| 21527 | if (try decls_val.sliceLen(mod) > 0) { | |
| 21725 | if (try decls_val.sliceLen(pt) > 0) { | |
| 21528 | 21726 | return sema.fail(block, src, "reified unions must have no decls", .{}); |
| 21529 | 21727 | } |
| 21530 | 21728 | const layout = mod.toEnum(std.builtin.Type.ContainerLayout, layout_val); |
| ... | ... | @@ -21537,23 +21735,23 @@ fn zirReify( |
| 21537 | 21735 | }, |
| 21538 | 21736 | .Fn => { |
| 21539 | 21737 | const struct_type = ip.loadStructType(ip.typeOf(union_val.val)); |
| 21540 | const calling_convention_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21738 | const calling_convention_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21541 | 21739 | ip, |
| 21542 | 21740 | try ip.getOrPutString(gpa, "calling_convention", .no_embedded_nulls), |
| 21543 | 21741 | ).?); |
| 21544 | const is_generic_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21742 | const is_generic_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21545 | 21743 | ip, |
| 21546 | 21744 | try ip.getOrPutString(gpa, "is_generic", .no_embedded_nulls), |
| 21547 | 21745 | ).?); |
| 21548 | const is_var_args_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21746 | const is_var_args_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21549 | 21747 | ip, |
| 21550 | 21748 | try ip.getOrPutString(gpa, "is_var_args", .no_embedded_nulls), |
| 21551 | 21749 | ).?); |
| 21552 | const return_type_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21750 | const return_type_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21553 | 21751 | ip, |
| 21554 | 21752 | try ip.getOrPutString(gpa, "return_type", .no_embedded_nulls), |
| 21555 | 21753 | ).?); |
| 21556 | const params_slice_val = try Value.fromInterned(union_val.val).fieldValue(mod, struct_type.nameIndex( | |
| 21754 | const params_slice_val = try Value.fromInterned(union_val.val).fieldValue(pt, struct_type.nameIndex( | |
| 21557 | 21755 | ip, |
| 21558 | 21756 | try ip.getOrPutString(gpa, "params", .no_embedded_nulls), |
| 21559 | 21757 | ).?); |
| ... | ... | @@ -21581,17 +21779,17 @@ fn zirReify( |
| 21581 | 21779 | |
| 21582 | 21780 | var noalias_bits: u32 = 0; |
| 21583 | 21781 | for (param_types, 0..) |*param_type, i| { |
| 21584 | const elem_val = try params_val.elemValue(mod, i); | |
| 21782 | const elem_val = try params_val.elemValue(pt, i); | |
| 21585 | 21783 | const elem_struct_type = ip.loadStructType(ip.typeOf(elem_val.toIntern())); |
| 21586 | const param_is_generic_val = try elem_val.fieldValue(mod, elem_struct_type.nameIndex( | |
| 21784 | const param_is_generic_val = try elem_val.fieldValue(pt, elem_struct_type.nameIndex( | |
| 21587 | 21785 | ip, |
| 21588 | 21786 | try ip.getOrPutString(gpa, "is_generic", .no_embedded_nulls), |
| 21589 | 21787 | ).?); |
| 21590 | const param_is_noalias_val = try elem_val.fieldValue(mod, elem_struct_type.nameIndex( | |
| 21788 | const param_is_noalias_val = try elem_val.fieldValue(pt, elem_struct_type.nameIndex( | |
| 21591 | 21789 | ip, |
| 21592 | 21790 | try ip.getOrPutString(gpa, "is_noalias", .no_embedded_nulls), |
| 21593 | 21791 | ).?); |
| 21594 | const opt_param_type_val = try elem_val.fieldValue(mod, elem_struct_type.nameIndex( | |
| 21792 | const opt_param_type_val = try elem_val.fieldValue(pt, elem_struct_type.nameIndex( | |
| 21595 | 21793 | ip, |
| 21596 | 21794 | try ip.getOrPutString(gpa, "type", .no_embedded_nulls), |
| 21597 | 21795 | ).?); |
| ... | ... | @@ -21613,7 +21811,7 @@ fn zirReify( |
| 21613 | 21811 | } |
| 21614 | 21812 | } |
| 21615 | 21813 | |
| 21616 | const ty = try mod.funcType(.{ | |
| 21814 | const ty = try pt.funcType(.{ | |
| 21617 | 21815 | .param_types = param_types, |
| 21618 | 21816 | .noalias_bits = noalias_bits, |
| 21619 | 21817 | .return_type = return_type.toIntern(), |
| ... | ... | @@ -21636,7 +21834,8 @@ fn reifyEnum( |
| 21636 | 21834 | fields_val: Value, |
| 21637 | 21835 | name_strategy: Zir.Inst.NameStrategy, |
| 21638 | 21836 | ) CompileError!Air.Inst.Ref { |
| 21639 | const mod = sema.mod; | |
| 21837 | const pt = sema.pt; | |
| 21838 | const mod = pt.zcu; | |
| 21640 | 21839 | const gpa = sema.gpa; |
| 21641 | 21840 | const ip = &mod.intern_pool; |
| 21642 | 21841 | |
| ... | ... | @@ -21656,10 +21855,10 @@ fn reifyEnum( |
| 21656 | 21855 | std.hash.autoHash(&hasher, fields_len); |
| 21657 | 21856 | |
| 21658 | 21857 | for (0..fields_len) |field_idx| { |
| 21659 | const field_info = try fields_val.elemValue(mod, field_idx); | |
| 21858 | const field_info = try fields_val.elemValue(pt, field_idx); | |
| 21660 | 21859 | |
| 21661 | const field_name_val = try field_info.fieldValue(mod, 0); | |
| 21662 | const field_value_val = try sema.resolveLazyValue(try field_info.fieldValue(mod, 1)); | |
| 21860 | const field_name_val = try field_info.fieldValue(pt, 0); | |
| 21861 | const field_value_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 1)); | |
| 21663 | 21862 | |
| 21664 | 21863 | const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ |
| 21665 | 21864 | .needed_comptime_reason = "enum field name must be comptime-known", |
| ... | ... | @@ -21671,7 +21870,7 @@ fn reifyEnum( |
| 21671 | 21870 | }); |
| 21672 | 21871 | } |
| 21673 | 21872 | |
| 21674 | const wip_ty = switch (try ip.getEnumType(gpa, .{ | |
| 21873 | const wip_ty = switch (try ip.getEnumType(gpa, pt.tid, .{ | |
| 21675 | 21874 | .has_namespace = false, |
| 21676 | 21875 | .has_values = true, |
| 21677 | 21876 | .tag_mode = if (is_exhaustive) .explicit else .nonexhaustive, |
| ... | ... | @@ -21704,10 +21903,10 @@ fn reifyEnum( |
| 21704 | 21903 | wip_ty.setTagTy(ip, tag_ty.toIntern()); |
| 21705 | 21904 | |
| 21706 | 21905 | for (0..fields_len) |field_idx| { |
| 21707 | const field_info = try fields_val.elemValue(mod, field_idx); | |
| 21906 | const field_info = try fields_val.elemValue(pt, field_idx); | |
| 21708 | 21907 | |
| 21709 | const field_name_val = try field_info.fieldValue(mod, 0); | |
| 21710 | const field_value_val = try sema.resolveLazyValue(try field_info.fieldValue(mod, 1)); | |
| 21908 | const field_name_val = try field_info.fieldValue(pt, 0); | |
| 21909 | const field_value_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 1)); | |
| 21711 | 21910 | |
| 21712 | 21911 | // Don't pass a reason; first loop acts as an assertion that this is valid. |
| 21713 | 21912 | const field_name = try sema.sliceToIpString(block, src, field_name_val, undefined); |
| ... | ... | @@ -21716,12 +21915,12 @@ fn reifyEnum( |
| 21716 | 21915 | // TODO: better source location |
| 21717 | 21916 | return sema.fail(block, src, "field '{}' with enumeration value '{}' is too large for backing int type '{}'", .{ |
| 21718 | 21917 | field_name.fmt(ip), |
| 21719 | field_value_val.fmtValue(mod, sema), | |
| 21720 | tag_ty.fmt(mod), | |
| 21918 | field_value_val.fmtValue(pt, sema), | |
| 21919 | tag_ty.fmt(pt), | |
| 21721 | 21920 | }); |
| 21722 | 21921 | } |
| 21723 | 21922 | |
| 21724 | const coerced_field_val = try mod.getCoerced(field_value_val, tag_ty); | |
| 21923 | const coerced_field_val = try pt.getCoerced(field_value_val, tag_ty); | |
| 21725 | 21924 | if (wip_ty.nextField(ip, field_name, coerced_field_val.toIntern())) |conflict| { |
| 21726 | 21925 | return sema.failWithOwnedErrorMsg(block, switch (conflict.kind) { |
| 21727 | 21926 | .name => msg: { |
| ... | ... | @@ -21732,7 +21931,7 @@ fn reifyEnum( |
| 21732 | 21931 | break :msg msg; |
| 21733 | 21932 | }, |
| 21734 | 21933 | .value => msg: { |
| 21735 | const msg = try sema.errMsg(src, "enum tag value {} already taken", .{field_value_val.fmtValue(mod, sema)}); | |
| 21934 | const msg = try sema.errMsg(src, "enum tag value {} already taken", .{field_value_val.fmtValue(pt, sema)}); | |
| 21736 | 21935 | errdefer msg.destroy(gpa); |
| 21737 | 21936 | _ = conflict.prev_field_idx; // TODO: this note is incorrect |
| 21738 | 21937 | try sema.errNote(src, msg, "other enum tag value here", .{}); |
| ... | ... | @@ -21742,11 +21941,11 @@ fn reifyEnum( |
| 21742 | 21941 | } |
| 21743 | 21942 | } |
| 21744 | 21943 | |
| 21745 | if (!is_exhaustive and fields_len > 1 and std.math.log2_int(u64, fields_len) == tag_ty.bitSize(mod)) { | |
| 21944 | if (!is_exhaustive and fields_len > 1 and std.math.log2_int(u64, fields_len) == tag_ty.bitSize(pt)) { | |
| 21746 | 21945 | return sema.fail(block, src, "non-exhaustive enum specified every value", .{}); |
| 21747 | 21946 | } |
| 21748 | 21947 | |
| 21749 | try mod.finalizeAnonDecl(new_decl_index); | |
| 21948 | try pt.finalizeAnonDecl(new_decl_index); | |
| 21750 | 21949 | return Air.internedToRef(wip_ty.index); |
| 21751 | 21950 | } |
| 21752 | 21951 | |
| ... | ... | @@ -21760,7 +21959,8 @@ fn reifyUnion( |
| 21760 | 21959 | fields_val: Value, |
| 21761 | 21960 | name_strategy: Zir.Inst.NameStrategy, |
| 21762 | 21961 | ) CompileError!Air.Inst.Ref { |
| 21763 | const mod = sema.mod; | |
| 21962 | const pt = sema.pt; | |
| 21963 | const mod = pt.zcu; | |
| 21764 | 21964 | const gpa = sema.gpa; |
| 21765 | 21965 | const ip = &mod.intern_pool; |
| 21766 | 21966 | |
| ... | ... | @@ -21782,11 +21982,11 @@ fn reifyUnion( |
| 21782 | 21982 | var any_aligns = false; |
| 21783 | 21983 | |
| 21784 | 21984 | for (0..fields_len) |field_idx| { |
| 21785 | const field_info = try fields_val.elemValue(mod, field_idx); | |
| 21985 | const field_info = try fields_val.elemValue(pt, field_idx); | |
| 21786 | 21986 | |
| 21787 | const field_name_val = try field_info.fieldValue(mod, 0); | |
| 21788 | const field_type_val = try field_info.fieldValue(mod, 1); | |
| 21789 | const field_align_val = try sema.resolveLazyValue(try field_info.fieldValue(mod, 2)); | |
| 21987 | const field_name_val = try field_info.fieldValue(pt, 0); | |
| 21988 | const field_type_val = try field_info.fieldValue(pt, 1); | |
| 21989 | const field_align_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 2)); | |
| 21790 | 21990 | |
| 21791 | 21991 | const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ |
| 21792 | 21992 | .needed_comptime_reason = "union field name must be comptime-known", |
| ... | ... | @@ -21798,12 +21998,12 @@ fn reifyUnion( |
| 21798 | 21998 | field_align_val.toIntern(), |
| 21799 | 21999 | }); |
| 21800 | 22000 | |
| 21801 | if (field_align_val.toUnsignedInt(mod) != 0) { | |
| 22001 | if (field_align_val.toUnsignedInt(pt) != 0) { | |
| 21802 | 22002 | any_aligns = true; |
| 21803 | 22003 | } |
| 21804 | 22004 | } |
| 21805 | 22005 | |
| 21806 | const wip_ty = switch (try ip.getUnionType(gpa, .{ | |
| 22006 | const wip_ty = switch (try ip.getUnionType(gpa, pt.tid, .{ | |
| 21807 | 22007 | .flags = .{ |
| 21808 | 22008 | .layout = layout, |
| 21809 | 22009 | .status = .none, |
| ... | ... | @@ -21861,10 +22061,10 @@ fn reifyUnion( |
| 21861 | 22061 | var seen_tags = try std.DynamicBitSetUnmanaged.initEmpty(sema.arena, tag_ty_fields_len); |
| 21862 | 22062 | |
| 21863 | 22063 | for (field_types, 0..) |*field_ty, field_idx| { |
| 21864 | const field_info = try fields_val.elemValue(mod, field_idx); | |
| 22064 | const field_info = try fields_val.elemValue(pt, field_idx); | |
| 21865 | 22065 | |
| 21866 | const field_name_val = try field_info.fieldValue(mod, 0); | |
| 21867 | const field_type_val = try field_info.fieldValue(mod, 1); | |
| 22066 | const field_name_val = try field_info.fieldValue(pt, 0); | |
| 22067 | const field_type_val = try field_info.fieldValue(pt, 1); | |
| 21868 | 22068 | |
| 21869 | 22069 | // Don't pass a reason; first loop acts as an assertion that this is valid. |
| 21870 | 22070 | const field_name = try sema.sliceToIpString(block, src, field_name_val, undefined); |
| ... | ... | @@ -21872,7 +22072,7 @@ fn reifyUnion( |
| 21872 | 22072 | const enum_index = enum_tag_ty.enumFieldIndex(field_name, mod) orelse { |
| 21873 | 22073 | // TODO: better source location |
| 21874 | 22074 | return sema.fail(block, src, "no field named '{}' in enum '{}'", .{ |
| 21875 | field_name.fmt(ip), enum_tag_ty.fmt(mod), | |
| 22075 | field_name.fmt(ip), enum_tag_ty.fmt(pt), | |
| 21876 | 22076 | }); |
| 21877 | 22077 | }; |
| 21878 | 22078 | if (seen_tags.isSet(enum_index)) { |
| ... | ... | @@ -21883,7 +22083,7 @@ fn reifyUnion( |
| 21883 | 22083 | |
| 21884 | 22084 | field_ty.* = field_type_val.toIntern(); |
| 21885 | 22085 | if (any_aligns) { |
| 21886 | const byte_align = try (try field_info.fieldValue(mod, 2)).toUnsignedIntSema(mod); | |
| 22086 | const byte_align = try (try field_info.fieldValue(pt, 2)).toUnsignedIntSema(pt); | |
| 21887 | 22087 | if (byte_align > 0 and !math.isPowerOfTwo(byte_align)) { |
| 21888 | 22088 | // TODO: better source location |
| 21889 | 22089 | return sema.fail(block, src, "alignment value '{d}' is not a power of two or zero", .{byte_align}); |
| ... | ... | @@ -21913,10 +22113,10 @@ fn reifyUnion( |
| 21913 | 22113 | try field_names.ensureTotalCapacity(sema.arena, fields_len); |
| 21914 | 22114 | |
| 21915 | 22115 | for (field_types, 0..) |*field_ty, field_idx| { |
| 21916 | const field_info = try fields_val.elemValue(mod, field_idx); | |
| 22116 | const field_info = try fields_val.elemValue(pt, field_idx); | |
| 21917 | 22117 | |
| 21918 | const field_name_val = try field_info.fieldValue(mod, 0); | |
| 21919 | const field_type_val = try field_info.fieldValue(mod, 1); | |
| 22118 | const field_name_val = try field_info.fieldValue(pt, 0); | |
| 22119 | const field_type_val = try field_info.fieldValue(pt, 1); | |
| 21920 | 22120 | |
| 21921 | 22121 | // Don't pass a reason; first loop acts as an assertion that this is valid. |
| 21922 | 22122 | const field_name = try sema.sliceToIpString(block, src, field_name_val, undefined); |
| ... | ... | @@ -21928,7 +22128,7 @@ fn reifyUnion( |
| 21928 | 22128 | |
| 21929 | 22129 | field_ty.* = field_type_val.toIntern(); |
| 21930 | 22130 | if (any_aligns) { |
| 21931 | const byte_align = try (try field_info.fieldValue(mod, 2)).toUnsignedIntSema(mod); | |
| 22131 | const byte_align = try (try field_info.fieldValue(pt, 2)).toUnsignedIntSema(pt); | |
| 21932 | 22132 | if (byte_align > 0 and !math.isPowerOfTwo(byte_align)) { |
| 21933 | 22133 | // TODO: better source location |
| 21934 | 22134 | return sema.fail(block, src, "alignment value '{d}' is not a power of two or zero", .{byte_align}); |
| ... | ... | @@ -21955,7 +22155,7 @@ fn reifyUnion( |
| 21955 | 22155 | } |
| 21956 | 22156 | if (layout == .@"extern" and !try sema.validateExternType(field_ty, .union_field)) { |
| 21957 | 22157 | return sema.failWithOwnedErrorMsg(block, msg: { |
| 21958 | const msg = try sema.errMsg(src, "extern unions cannot contain fields of type '{}'", .{field_ty.fmt(mod)}); | |
| 22158 | const msg = try sema.errMsg(src, "extern unions cannot contain fields of type '{}'", .{field_ty.fmt(pt)}); | |
| 21959 | 22159 | errdefer msg.destroy(gpa); |
| 21960 | 22160 | |
| 21961 | 22161 | try sema.explainWhyTypeIsNotExtern(msg, src, field_ty, .union_field); |
| ... | ... | @@ -21965,7 +22165,7 @@ fn reifyUnion( |
| 21965 | 22165 | }); |
| 21966 | 22166 | } else if (layout == .@"packed" and !try sema.validatePackedType(field_ty)) { |
| 21967 | 22167 | return sema.failWithOwnedErrorMsg(block, msg: { |
| 21968 | const msg = try sema.errMsg(src, "packed unions cannot contain fields of type '{}'", .{field_ty.fmt(mod)}); | |
| 22168 | const msg = try sema.errMsg(src, "packed unions cannot contain fields of type '{}'", .{field_ty.fmt(pt)}); | |
| 21969 | 22169 | errdefer msg.destroy(gpa); |
| 21970 | 22170 | |
| 21971 | 22171 | try sema.explainWhyTypeIsNotPacked(msg, src, field_ty); |
| ... | ... | @@ -21984,7 +22184,7 @@ fn reifyUnion( |
| 21984 | 22184 | loaded_union.tagTypePtr(ip).* = enum_tag_ty; |
| 21985 | 22185 | loaded_union.flagsPtr(ip).status = .have_field_types; |
| 21986 | 22186 | |
| 21987 | try mod.finalizeAnonDecl(new_decl_index); | |
| 22187 | try pt.finalizeAnonDecl(new_decl_index); | |
| 21988 | 22188 | try mod.comp.work_queue.writeItem(.{ .resolve_type_fully = wip_ty.index }); |
| 21989 | 22189 | try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .decl = new_decl_index })); |
| 21990 | 22190 | return Air.internedToRef(wip_ty.finish(ip, new_decl_index, .none)); |
| ... | ... | @@ -22001,7 +22201,8 @@ fn reifyStruct( |
| 22001 | 22201 | name_strategy: Zir.Inst.NameStrategy, |
| 22002 | 22202 | is_tuple: bool, |
| 22003 | 22203 | ) CompileError!Air.Inst.Ref { |
| 22004 | const mod = sema.mod; | |
| 22204 | const pt = sema.pt; | |
| 22205 | const mod = pt.zcu; | |
| 22005 | 22206 | const gpa = sema.gpa; |
| 22006 | 22207 | const ip = &mod.intern_pool; |
| 22007 | 22208 | |
| ... | ... | @@ -22026,20 +22227,20 @@ fn reifyStruct( |
| 22026 | 22227 | var any_aligned_fields = false; |
| 22027 | 22228 | |
| 22028 | 22229 | for (0..fields_len) |field_idx| { |
| 22029 | const field_info = try fields_val.elemValue(mod, field_idx); | |
| 22230 | const field_info = try fields_val.elemValue(pt, field_idx); | |
| 22030 | 22231 | |
| 22031 | const field_name_val = try field_info.fieldValue(mod, 0); | |
| 22032 | const field_type_val = try field_info.fieldValue(mod, 1); | |
| 22033 | const field_default_value_val = try field_info.fieldValue(mod, 2); | |
| 22034 | const field_is_comptime_val = try field_info.fieldValue(mod, 3); | |
| 22035 | const field_alignment_val = try sema.resolveLazyValue(try field_info.fieldValue(mod, 4)); | |
| 22232 | const field_name_val = try field_info.fieldValue(pt, 0); | |
| 22233 | const field_type_val = try field_info.fieldValue(pt, 1); | |
| 22234 | const field_default_value_val = try field_info.fieldValue(pt, 2); | |
| 22235 | const field_is_comptime_val = try field_info.fieldValue(pt, 3); | |
| 22236 | const field_alignment_val = try sema.resolveLazyValue(try field_info.fieldValue(pt, 4)); | |
| 22036 | 22237 | |
| 22037 | 22238 | const field_name = try sema.sliceToIpString(block, src, field_name_val, .{ |
| 22038 | 22239 | .needed_comptime_reason = "struct field name must be comptime-known", |
| 22039 | 22240 | }); |
| 22040 | 22241 | const field_is_comptime = field_is_comptime_val.toBool(); |
| 22041 | 22242 | const field_default_value: InternPool.Index = if (field_default_value_val.optionalValue(mod)) |ptr_val| d: { |
| 22042 | const ptr_ty = try mod.singleConstPtrType(field_type_val.toType()); | |
| 22243 | const ptr_ty = try pt.singleConstPtrType(field_type_val.toType()); | |
| 22043 | 22244 | // We need to do this deref here, so we won't check for this error case later on. |
| 22044 | 22245 | const val = try sema.pointerDeref(block, src, ptr_val, ptr_ty) orelse return sema.failWithNeededComptime( |
| 22045 | 22246 | block, |
| ... | ... | @@ -22060,14 +22261,14 @@ fn reifyStruct( |
| 22060 | 22261 | |
| 22061 | 22262 | if (field_is_comptime) any_comptime_fields = true; |
| 22062 | 22263 | if (field_default_value != .none) any_default_inits = true; |
| 22063 | switch (try field_alignment_val.orderAgainstZeroAdvanced(mod, .sema)) { | |
| 22264 | switch (try field_alignment_val.orderAgainstZeroAdvanced(pt, .sema)) { | |
| 22064 | 22265 | .eq => {}, |
| 22065 | 22266 | .gt => any_aligned_fields = true, |
| 22066 | 22267 | .lt => unreachable, |
| 22067 | 22268 | } |
| 22068 | 22269 | } |
| 22069 | 22270 | |
| 22070 | const wip_ty = switch (try ip.getStructType(gpa, .{ | |
| 22271 | const wip_ty = switch (try ip.getStructType(gpa, pt.tid, .{ | |
| 22071 | 22272 | .layout = layout, |
| 22072 | 22273 | .fields_len = fields_len, |
| 22073 | 22274 | .known_non_opv = false, |
| ... | ... | @@ -22107,13 +22308,13 @@ fn reifyStruct( |
| 22107 | 22308 | const struct_type = ip.loadStructType(wip_ty.index); |
| 22108 | 22309 | |
| 22109 | 22310 | for (0..fields_len) |field_idx| { |
| 22110 | const field_info = try fields_val.elemValue(mod, field_idx); | |
| 22311 | const field_info = try fields_val.elemValue(pt, field_idx); | |
| 22111 | 22312 | |
| 22112 | const field_name_val = try field_info.fieldValue(mod, 0); | |
| 22113 | const field_type_val = try field_info.fieldValue(mod, 1); | |
| 22114 | const field_default_value_val = try field_info.fieldValue(mod, 2); | |
| 22115 | const field_is_comptime_val = try field_info.fieldValue(mod, 3); | |
| 22116 | const field_alignment_val = try field_info.fieldValue(mod, 4); | |
| 22313 | const field_name_val = try field_info.fieldValue(pt, 0); | |
| 22314 | const field_type_val = try field_info.fieldValue(pt, 1); | |
| 22315 | const field_default_value_val = try field_info.fieldValue(pt, 2); | |
| 22316 | const field_is_comptime_val = try field_info.fieldValue(pt, 3); | |
| 22317 | const field_alignment_val = try field_info.fieldValue(pt, 4); | |
| 22117 | 22318 | |
| 22118 | 22319 | const field_ty = field_type_val.toType(); |
| 22119 | 22320 | // Don't pass a reason; first loop acts as an assertion that this is valid. |
| ... | ... | @@ -22143,7 +22344,7 @@ fn reifyStruct( |
| 22143 | 22344 | return sema.fail(block, src, "alignment must fit in 'u32'", .{}); |
| 22144 | 22345 | } |
| 22145 | 22346 | |
| 22146 | const byte_align = try field_alignment_val.toUnsignedIntSema(mod); | |
| 22347 | const byte_align = try field_alignment_val.toUnsignedIntSema(pt); | |
| 22147 | 22348 | if (byte_align == 0) { |
| 22148 | 22349 | if (layout != .@"packed") { |
| 22149 | 22350 | struct_type.field_aligns.get(ip)[field_idx] = .none; |
| ... | ... | @@ -22168,7 +22369,7 @@ fn reifyStruct( |
| 22168 | 22369 | const field_default: InternPool.Index = d: { |
| 22169 | 22370 | if (!any_default_inits) break :d .none; |
| 22170 | 22371 | const ptr_val = field_default_value_val.optionalValue(mod) orelse break :d .none; |
| 22171 | const ptr_ty = try mod.singleConstPtrType(field_ty); | |
| 22372 | const ptr_ty = try pt.singleConstPtrType(field_ty); | |
| 22172 | 22373 | // Asserted comptime-dereferencable above. |
| 22173 | 22374 | const val = (try sema.pointerDeref(block, src, ptr_val, ptr_ty)).?; |
| 22174 | 22375 | // We already resolved this for deduplication, so we may as well do it now. |
| ... | ... | @@ -22204,7 +22405,7 @@ fn reifyStruct( |
| 22204 | 22405 | } |
| 22205 | 22406 | if (layout == .@"extern" and !try sema.validateExternType(field_ty, .struct_field)) { |
| 22206 | 22407 | return sema.failWithOwnedErrorMsg(block, msg: { |
| 22207 | const msg = try sema.errMsg(src, "extern structs cannot contain fields of type '{}'", .{field_ty.fmt(sema.mod)}); | |
| 22408 | const msg = try sema.errMsg(src, "extern structs cannot contain fields of type '{}'", .{field_ty.fmt(pt)}); | |
| 22208 | 22409 | errdefer msg.destroy(gpa); |
| 22209 | 22410 | |
| 22210 | 22411 | try sema.explainWhyTypeIsNotExtern(msg, src, field_ty, .struct_field); |
| ... | ... | @@ -22214,7 +22415,7 @@ fn reifyStruct( |
| 22214 | 22415 | }); |
| 22215 | 22416 | } else if (layout == .@"packed" and !try sema.validatePackedType(field_ty)) { |
| 22216 | 22417 | return sema.failWithOwnedErrorMsg(block, msg: { |
| 22217 | const msg = try sema.errMsg(src, "packed structs cannot contain fields of type '{}'", .{field_ty.fmt(sema.mod)}); | |
| 22418 | const msg = try sema.errMsg(src, "packed structs cannot contain fields of type '{}'", .{field_ty.fmt(pt)}); | |
| 22218 | 22419 | errdefer msg.destroy(gpa); |
| 22219 | 22420 | |
| 22220 | 22421 | try sema.explainWhyTypeIsNotPacked(msg, src, field_ty); |
| ... | ... | @@ -22229,7 +22430,7 @@ fn reifyStruct( |
| 22229 | 22430 | var fields_bit_sum: u64 = 0; |
| 22230 | 22431 | for (0..struct_type.field_types.len) |field_idx| { |
| 22231 | 22432 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_idx]); |
| 22232 | field_ty.resolveLayout(mod) catch |err| switch (err) { | |
| 22433 | field_ty.resolveLayout(pt) catch |err| switch (err) { | |
| 22233 | 22434 | error.AnalysisFail => { |
| 22234 | 22435 | const msg = sema.err orelse return err; |
| 22235 | 22436 | try sema.errNote(src, msg, "while checking a field of this struct", .{}); |
| ... | ... | @@ -22237,7 +22438,7 @@ fn reifyStruct( |
| 22237 | 22438 | }, |
| 22238 | 22439 | else => return err, |
| 22239 | 22440 | }; |
| 22240 | fields_bit_sum += field_ty.bitSize(mod); | |
| 22441 | fields_bit_sum += field_ty.bitSize(pt); | |
| 22241 | 22442 | } |
| 22242 | 22443 | |
| 22243 | 22444 | if (opt_backing_int_val.optionalValue(mod)) |backing_int_val| { |
| ... | ... | @@ -22245,20 +22446,21 @@ fn reifyStruct( |
| 22245 | 22446 | try sema.checkBackingIntType(block, src, backing_int_ty, fields_bit_sum); |
| 22246 | 22447 | struct_type.backingIntType(ip).* = backing_int_ty.toIntern(); |
| 22247 | 22448 | } else { |
| 22248 | const backing_int_ty = try mod.intType(.unsigned, @intCast(fields_bit_sum)); | |
| 22449 | const backing_int_ty = try pt.intType(.unsigned, @intCast(fields_bit_sum)); | |
| 22249 | 22450 | struct_type.backingIntType(ip).* = backing_int_ty.toIntern(); |
| 22250 | 22451 | } |
| 22251 | 22452 | } |
| 22252 | 22453 | |
| 22253 | try mod.finalizeAnonDecl(new_decl_index); | |
| 22454 | try pt.finalizeAnonDecl(new_decl_index); | |
| 22254 | 22455 | try mod.comp.work_queue.writeItem(.{ .resolve_type_fully = wip_ty.index }); |
| 22255 | 22456 | try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .decl = new_decl_index })); |
| 22256 | 22457 | return Air.internedToRef(wip_ty.finish(ip, new_decl_index, .none)); |
| 22257 | 22458 | } |
| 22258 | 22459 | |
| 22259 | 22460 | fn resolveVaListRef(sema: *Sema, block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref) CompileError!Air.Inst.Ref { |
| 22260 | const va_list_ty = try sema.mod.getBuiltinType("VaList"); | |
| 22261 | const va_list_ptr = try sema.mod.singleMutPtrType(va_list_ty); | |
| 22461 | const pt = sema.pt; | |
| 22462 | const va_list_ty = try pt.getBuiltinType("VaList"); | |
| 22463 | const va_list_ptr = try pt.singleMutPtrType(va_list_ty); | |
| 22262 | 22464 | |
| 22263 | 22465 | const inst = try sema.resolveInst(zir_ref); |
| 22264 | 22466 | return sema.coerce(block, va_list_ptr, inst, src); |
| ... | ... | @@ -22275,7 +22477,7 @@ fn zirCVaArg(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) C |
| 22275 | 22477 | |
| 22276 | 22478 | if (!try sema.validateExternType(arg_ty, .param_ty)) { |
| 22277 | 22479 | const msg = msg: { |
| 22278 | const msg = try sema.errMsg(ty_src, "cannot get '{}' from variadic argument", .{arg_ty.fmt(sema.mod)}); | |
| 22480 | const msg = try sema.errMsg(ty_src, "cannot get '{}' from variadic argument", .{arg_ty.fmt(sema.pt)}); | |
| 22279 | 22481 | errdefer msg.destroy(sema.gpa); |
| 22280 | 22482 | |
| 22281 | 22483 | try sema.explainWhyTypeIsNotExtern(msg, ty_src, arg_ty, .param_ty); |
| ... | ... | @@ -22296,7 +22498,7 @@ fn zirCVaCopy(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) |
| 22296 | 22498 | const va_list_src = block.builtinCallArgSrc(extra.node, 0); |
| 22297 | 22499 | |
| 22298 | 22500 | const va_list_ref = try sema.resolveVaListRef(block, va_list_src, extra.operand); |
| 22299 | const va_list_ty = try sema.mod.getBuiltinType("VaList"); | |
| 22501 | const va_list_ty = try sema.pt.getBuiltinType("VaList"); | |
| 22300 | 22502 | |
| 22301 | 22503 | try sema.requireRuntimeBlock(block, src, null); |
| 22302 | 22504 | return block.addTyOp(.c_va_copy, va_list_ty, va_list_ref); |
| ... | ... | @@ -22316,7 +22518,7 @@ fn zirCVaEnd(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) C |
| 22316 | 22518 | fn zirCVaStart(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { |
| 22317 | 22519 | const src = block.nodeOffset(@bitCast(extended.operand)); |
| 22318 | 22520 | |
| 22319 | const va_list_ty = try sema.mod.getBuiltinType("VaList"); | |
| 22521 | const va_list_ty = try sema.pt.getBuiltinType("VaList"); | |
| 22320 | 22522 | try sema.requireRuntimeBlock(block, src, null); |
| 22321 | 22523 | return block.addInst(.{ |
| 22322 | 22524 | .tag = .c_va_start, |
| ... | ... | @@ -22325,14 +22527,15 @@ fn zirCVaStart(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) |
| 22325 | 22527 | } |
| 22326 | 22528 | |
| 22327 | 22529 | fn zirTypeName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 22328 | const mod = sema.mod; | |
| 22530 | const pt = sema.pt; | |
| 22531 | const mod = pt.zcu; | |
| 22329 | 22532 | const ip = &mod.intern_pool; |
| 22330 | 22533 | |
| 22331 | 22534 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 22332 | 22535 | const ty_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| 22333 | 22536 | const ty = try sema.resolveType(block, ty_src, inst_data.operand); |
| 22334 | 22537 | |
| 22335 | const type_name = try ip.getOrPutStringFmt(sema.gpa, "{}", .{ty.fmt(mod)}, .no_embedded_nulls); | |
| 22538 | const type_name = try ip.getOrPutStringFmt(sema.gpa, "{}", .{ty.fmt(pt)}, .no_embedded_nulls); | |
| 22336 | 22539 | return sema.addNullTerminatedStrLit(type_name); |
| 22337 | 22540 | } |
| 22338 | 22541 | |
| ... | ... | @@ -22349,7 +22552,8 @@ fn zirFrameSize(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 22349 | 22552 | } |
| 22350 | 22553 | |
| 22351 | 22554 | fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 22352 | const mod = sema.mod; | |
| 22555 | const pt = sema.pt; | |
| 22556 | const mod = pt.zcu; | |
| 22353 | 22557 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 22354 | 22558 | const src = block.nodeOffset(inst_data.src_node); |
| 22355 | 22559 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| ... | ... | @@ -22380,23 +22584,23 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 22380 | 22584 | if (dest_scalar_ty.intInfo(mod).bits == 0) { |
| 22381 | 22585 | if (!is_vector) { |
| 22382 | 22586 | if (block.wantSafety()) { |
| 22383 | const ok = try block.addBinOp(if (block.float_mode == .optimized) .cmp_eq_optimized else .cmp_eq, operand, Air.internedToRef((try mod.floatValue(operand_ty, 0.0)).toIntern())); | |
| 22587 | const ok = try block.addBinOp(if (block.float_mode == .optimized) .cmp_eq_optimized else .cmp_eq, operand, Air.internedToRef((try pt.floatValue(operand_ty, 0.0)).toIntern())); | |
| 22384 | 22588 | try sema.addSafetyCheck(block, src, ok, .integer_part_out_of_bounds); |
| 22385 | 22589 | } |
| 22386 | return Air.internedToRef((try mod.intValue(dest_ty, 0)).toIntern()); | |
| 22590 | return Air.internedToRef((try pt.intValue(dest_ty, 0)).toIntern()); | |
| 22387 | 22591 | } |
| 22388 | 22592 | if (block.wantSafety()) { |
| 22389 | 22593 | const len = dest_ty.vectorLen(mod); |
| 22390 | 22594 | for (0..len) |i| { |
| 22391 | const idx_ref = try mod.intRef(Type.usize, i); | |
| 22595 | const idx_ref = try pt.intRef(Type.usize, i); | |
| 22392 | 22596 | const elem_ref = try block.addBinOp(.array_elem_val, operand, idx_ref); |
| 22393 | const ok = try block.addBinOp(if (block.float_mode == .optimized) .cmp_eq_optimized else .cmp_eq, elem_ref, Air.internedToRef((try mod.floatValue(operand_scalar_ty, 0.0)).toIntern())); | |
| 22597 | const ok = try block.addBinOp(if (block.float_mode == .optimized) .cmp_eq_optimized else .cmp_eq, elem_ref, Air.internedToRef((try pt.floatValue(operand_scalar_ty, 0.0)).toIntern())); | |
| 22394 | 22598 | try sema.addSafetyCheck(block, src, ok, .integer_part_out_of_bounds); |
| 22395 | 22599 | } |
| 22396 | 22600 | } |
| 22397 | return Air.internedToRef(try mod.intern(.{ .aggregate = .{ | |
| 22601 | return Air.internedToRef(try pt.intern(.{ .aggregate = .{ | |
| 22398 | 22602 | .ty = dest_ty.toIntern(), |
| 22399 | .storage = .{ .repeated_elem = (try mod.intValue(dest_scalar_ty, 0)).toIntern() }, | |
| 22603 | .storage = .{ .repeated_elem = (try pt.intValue(dest_scalar_ty, 0)).toIntern() }, | |
| 22400 | 22604 | } })); |
| 22401 | 22605 | } |
| 22402 | 22606 | if (!is_vector) { |
| ... | ... | @@ -22404,8 +22608,8 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 22404 | 22608 | if (block.wantSafety()) { |
| 22405 | 22609 | const back = try block.addTyOp(.float_from_int, operand_ty, result); |
| 22406 | 22610 | const diff = try block.addBinOp(.sub, operand, back); |
| 22407 | const ok_pos = try block.addBinOp(if (block.float_mode == .optimized) .cmp_lt_optimized else .cmp_lt, diff, Air.internedToRef((try mod.floatValue(operand_ty, 1.0)).toIntern())); | |
| 22408 | const ok_neg = try block.addBinOp(if (block.float_mode == .optimized) .cmp_gt_optimized else .cmp_gt, diff, Air.internedToRef((try mod.floatValue(operand_ty, -1.0)).toIntern())); | |
| 22611 | const ok_pos = try block.addBinOp(if (block.float_mode == .optimized) .cmp_lt_optimized else .cmp_lt, diff, Air.internedToRef((try pt.floatValue(operand_ty, 1.0)).toIntern())); | |
| 22612 | const ok_neg = try block.addBinOp(if (block.float_mode == .optimized) .cmp_gt_optimized else .cmp_gt, diff, Air.internedToRef((try pt.floatValue(operand_ty, -1.0)).toIntern())); | |
| 22409 | 22613 | const ok = try block.addBinOp(.bool_and, ok_pos, ok_neg); |
| 22410 | 22614 | try sema.addSafetyCheck(block, src, ok, .integer_part_out_of_bounds); |
| 22411 | 22615 | } |
| ... | ... | @@ -22414,14 +22618,14 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 22414 | 22618 | const len = dest_ty.vectorLen(mod); |
| 22415 | 22619 | const new_elems = try sema.arena.alloc(Air.Inst.Ref, len); |
| 22416 | 22620 | for (new_elems, 0..) |*new_elem, i| { |
| 22417 | const idx_ref = try mod.intRef(Type.usize, i); | |
| 22621 | const idx_ref = try pt.intRef(Type.usize, i); | |
| 22418 | 22622 | const old_elem = try block.addBinOp(.array_elem_val, operand, idx_ref); |
| 22419 | 22623 | const result = try block.addTyOp(if (block.float_mode == .optimized) .int_from_float_optimized else .int_from_float, dest_scalar_ty, old_elem); |
| 22420 | 22624 | if (block.wantSafety()) { |
| 22421 | 22625 | const back = try block.addTyOp(.float_from_int, operand_scalar_ty, result); |
| 22422 | 22626 | const diff = try block.addBinOp(.sub, old_elem, back); |
| 22423 | const ok_pos = try block.addBinOp(if (block.float_mode == .optimized) .cmp_lt_optimized else .cmp_lt, diff, Air.internedToRef((try mod.floatValue(operand_scalar_ty, 1.0)).toIntern())); | |
| 22424 | const ok_neg = try block.addBinOp(if (block.float_mode == .optimized) .cmp_gt_optimized else .cmp_gt, diff, Air.internedToRef((try mod.floatValue(operand_scalar_ty, -1.0)).toIntern())); | |
| 22627 | const ok_pos = try block.addBinOp(if (block.float_mode == .optimized) .cmp_lt_optimized else .cmp_lt, diff, Air.internedToRef((try pt.floatValue(operand_scalar_ty, 1.0)).toIntern())); | |
| 22628 | const ok_neg = try block.addBinOp(if (block.float_mode == .optimized) .cmp_gt_optimized else .cmp_gt, diff, Air.internedToRef((try pt.floatValue(operand_scalar_ty, -1.0)).toIntern())); | |
| 22425 | 22629 | const ok = try block.addBinOp(.bool_and, ok_pos, ok_neg); |
| 22426 | 22630 | try sema.addSafetyCheck(block, src, ok, .integer_part_out_of_bounds); |
| 22427 | 22631 | } |
| ... | ... | @@ -22431,7 +22635,8 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 22431 | 22635 | } |
| 22432 | 22636 | |
| 22433 | 22637 | fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 22434 | const mod = sema.mod; | |
| 22638 | const pt = sema.pt; | |
| 22639 | const mod = pt.zcu; | |
| 22435 | 22640 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 22436 | 22641 | const src = block.nodeOffset(inst_data.src_node); |
| 22437 | 22642 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| ... | ... | @@ -22450,7 +22655,7 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 22450 | 22655 | _ = try sema.checkIntType(block, operand_src, operand_scalar_ty); |
| 22451 | 22656 | |
| 22452 | 22657 | if (try sema.resolveValue(operand)) |operand_val| { |
| 22453 | const result_val = try operand_val.floatFromIntAdvanced(sema.arena, operand_ty, dest_ty, mod, .sema); | |
| 22658 | const result_val = try operand_val.floatFromIntAdvanced(sema.arena, operand_ty, dest_ty, pt, .sema); | |
| 22454 | 22659 | return Air.internedToRef(result_val.toIntern()); |
| 22455 | 22660 | } else if (dest_scalar_ty.zigTypeTag(mod) == .ComptimeFloat) { |
| 22456 | 22661 | return sema.failWithNeededComptime(block, operand_src, .{ |
| ... | ... | @@ -22465,7 +22670,7 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 22465 | 22670 | const len = operand_ty.vectorLen(mod); |
| 22466 | 22671 | const new_elems = try sema.arena.alloc(Air.Inst.Ref, len); |
| 22467 | 22672 | for (new_elems, 0..) |*new_elem, i| { |
| 22468 | const idx_ref = try mod.intRef(Type.usize, i); | |
| 22673 | const idx_ref = try pt.intRef(Type.usize, i); | |
| 22469 | 22674 | const old_elem = try block.addBinOp(.array_elem_val, operand, idx_ref); |
| 22470 | 22675 | new_elem.* = try block.addTyOp(.float_from_int, dest_scalar_ty, old_elem); |
| 22471 | 22676 | } |
| ... | ... | @@ -22473,7 +22678,8 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 22473 | 22678 | } |
| 22474 | 22679 | |
| 22475 | 22680 | fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 22476 | const mod = sema.mod; | |
| 22681 | const pt = sema.pt; | |
| 22682 | const mod = pt.zcu; | |
| 22477 | 22683 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 22478 | 22684 | const src = block.nodeOffset(inst_data.src_node); |
| 22479 | 22685 | |
| ... | ... | @@ -22489,7 +22695,7 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 22489 | 22695 | const is_vector = dest_ty.zigTypeTag(mod) == .Vector; |
| 22490 | 22696 | const operand_ty = if (is_vector) operand_ty: { |
| 22491 | 22697 | const len = dest_ty.vectorLen(mod); |
| 22492 | break :operand_ty try mod.vectorType(.{ .child = .usize_type, .len = len }); | |
| 22698 | break :operand_ty try pt.vectorType(.{ .child = .usize_type, .len = len }); | |
| 22493 | 22699 | } else Type.usize; |
| 22494 | 22700 | |
| 22495 | 22701 | const operand_coerced = try sema.coerce(block, operand_ty, operand_res, operand_src); |
| ... | ... | @@ -22498,11 +22704,11 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 22498 | 22704 | try sema.checkPtrType(block, src, ptr_ty, true); |
| 22499 | 22705 | |
| 22500 | 22706 | const elem_ty = ptr_ty.elemType2(mod); |
| 22501 | const ptr_align = try ptr_ty.ptrAlignmentAdvanced(mod, .sema); | |
| 22707 | const ptr_align = try ptr_ty.ptrAlignmentAdvanced(pt, .sema); | |
| 22502 | 22708 | |
| 22503 | 22709 | if (ptr_ty.isSlice(mod)) { |
| 22504 | 22710 | const msg = msg: { |
| 22505 | const msg = try sema.errMsg(src, "integer cannot be converted to slice type '{}'", .{ptr_ty.fmt(sema.mod)}); | |
| 22711 | const msg = try sema.errMsg(src, "integer cannot be converted to slice type '{}'", .{ptr_ty.fmt(pt)}); | |
| 22506 | 22712 | errdefer msg.destroy(sema.gpa); |
| 22507 | 22713 | try sema.errNote(src, msg, "slice length cannot be inferred from address", .{}); |
| 22508 | 22714 | break :msg msg; |
| ... | ... | @@ -22518,18 +22724,18 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 22518 | 22724 | const len = dest_ty.vectorLen(mod); |
| 22519 | 22725 | const new_elems = try sema.arena.alloc(InternPool.Index, len); |
| 22520 | 22726 | for (new_elems, 0..) |*new_elem, i| { |
| 22521 | const elem = try val.elemValue(mod, i); | |
| 22727 | const elem = try val.elemValue(pt, i); | |
| 22522 | 22728 | const ptr_val = try sema.ptrFromIntVal(block, operand_src, elem, ptr_ty, ptr_align); |
| 22523 | 22729 | new_elem.* = ptr_val.toIntern(); |
| 22524 | 22730 | } |
| 22525 | return Air.internedToRef(try mod.intern(.{ .aggregate = .{ | |
| 22731 | return Air.internedToRef(try pt.intern(.{ .aggregate = .{ | |
| 22526 | 22732 | .ty = dest_ty.toIntern(), |
| 22527 | 22733 | .storage = .{ .elems = new_elems }, |
| 22528 | 22734 | } })); |
| 22529 | 22735 | } |
| 22530 | 22736 | if (try sema.typeRequiresComptime(ptr_ty)) { |
| 22531 | 22737 | return sema.failWithOwnedErrorMsg(block, msg: { |
| 22532 | const msg = try sema.errMsg(src, "pointer to comptime-only type '{}' must be comptime-known, but operand is runtime-known", .{ptr_ty.fmt(mod)}); | |
| 22738 | const msg = try sema.errMsg(src, "pointer to comptime-only type '{}' must be comptime-known, but operand is runtime-known", .{ptr_ty.fmt(pt)}); | |
| 22533 | 22739 | errdefer msg.destroy(sema.gpa); |
| 22534 | 22740 | |
| 22535 | 22741 | try sema.explainWhyTypeIsComptime(msg, src, ptr_ty); |
| ... | ... | @@ -22545,7 +22751,7 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 22545 | 22751 | } |
| 22546 | 22752 | if (ptr_align.compare(.gt, .@"1")) { |
| 22547 | 22753 | const align_bytes_minus_1 = ptr_align.toByteUnits().? - 1; |
| 22548 | const align_minus_1 = Air.internedToRef((try mod.intValue(Type.usize, align_bytes_minus_1)).toIntern()); | |
| 22754 | const align_minus_1 = Air.internedToRef((try pt.intValue(Type.usize, align_bytes_minus_1)).toIntern()); | |
| 22549 | 22755 | const remainder = try block.addBinOp(.bit_and, operand_coerced, align_minus_1); |
| 22550 | 22756 | const is_aligned = try block.addBinOp(.cmp_eq, remainder, .zero_usize); |
| 22551 | 22757 | try sema.addSafetyCheck(block, src, is_aligned, .incorrect_alignment); |
| ... | ... | @@ -22557,7 +22763,7 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 22557 | 22763 | const len = dest_ty.vectorLen(mod); |
| 22558 | 22764 | if (block.wantSafety() and (try sema.typeHasRuntimeBits(elem_ty) or elem_ty.zigTypeTag(mod) == .Fn)) { |
| 22559 | 22765 | for (0..len) |i| { |
| 22560 | const idx_ref = try mod.intRef(Type.usize, i); | |
| 22766 | const idx_ref = try pt.intRef(Type.usize, i); | |
| 22561 | 22767 | const elem_coerced = try block.addBinOp(.array_elem_val, operand_coerced, idx_ref); |
| 22562 | 22768 | if (!ptr_ty.isAllowzeroPtr(mod)) { |
| 22563 | 22769 | const is_non_zero = try block.addBinOp(.cmp_neq, elem_coerced, .zero_usize); |
| ... | ... | @@ -22565,7 +22771,7 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 22565 | 22771 | } |
| 22566 | 22772 | if (ptr_align.compare(.gt, .@"1")) { |
| 22567 | 22773 | const align_bytes_minus_1 = ptr_align.toByteUnits().? - 1; |
| 22568 | const align_minus_1 = Air.internedToRef((try mod.intValue(Type.usize, align_bytes_minus_1)).toIntern()); | |
| 22774 | const align_minus_1 = Air.internedToRef((try pt.intValue(Type.usize, align_bytes_minus_1)).toIntern()); | |
| 22569 | 22775 | const remainder = try block.addBinOp(.bit_and, elem_coerced, align_minus_1); |
| 22570 | 22776 | const is_aligned = try block.addBinOp(.cmp_eq, remainder, .zero_usize); |
| 22571 | 22777 | try sema.addSafetyCheck(block, src, is_aligned, .incorrect_alignment); |
| ... | ... | @@ -22575,7 +22781,7 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 22575 | 22781 | |
| 22576 | 22782 | const new_elems = try sema.arena.alloc(Air.Inst.Ref, len); |
| 22577 | 22783 | for (new_elems, 0..) |*new_elem, i| { |
| 22578 | const idx_ref = try mod.intRef(Type.usize, i); | |
| 22784 | const idx_ref = try pt.intRef(Type.usize, i); | |
| 22579 | 22785 | const old_elem = try block.addBinOp(.array_elem_val, operand_coerced, idx_ref); |
| 22580 | 22786 | new_elem.* = try block.addBitCast(ptr_ty, old_elem); |
| 22581 | 22787 | } |
| ... | ... | @@ -22590,31 +22796,33 @@ fn ptrFromIntVal( |
| 22590 | 22796 | ptr_ty: Type, |
| 22591 | 22797 | ptr_align: Alignment, |
| 22592 | 22798 | ) !Value { |
| 22593 | const zcu = sema.mod; | |
| 22799 | const pt = sema.pt; | |
| 22800 | const zcu = pt.zcu; | |
| 22594 | 22801 | if (operand_val.isUndef(zcu)) { |
| 22595 | 22802 | if (ptr_ty.isAllowzeroPtr(zcu) and ptr_align == .@"1") { |
| 22596 | return zcu.undefValue(ptr_ty); | |
| 22803 | return pt.undefValue(ptr_ty); | |
| 22597 | 22804 | } |
| 22598 | 22805 | return sema.failWithUseOfUndef(block, operand_src); |
| 22599 | 22806 | } |
| 22600 | const addr = try operand_val.toUnsignedIntSema(zcu); | |
| 22807 | const addr = try operand_val.toUnsignedIntSema(pt); | |
| 22601 | 22808 | if (!ptr_ty.isAllowzeroPtr(zcu) and addr == 0) |
| 22602 | return sema.fail(block, operand_src, "pointer type '{}' does not allow address zero", .{ptr_ty.fmt(zcu)}); | |
| 22809 | return sema.fail(block, operand_src, "pointer type '{}' does not allow address zero", .{ptr_ty.fmt(pt)}); | |
| 22603 | 22810 | if (addr != 0 and ptr_align != .none and !ptr_align.check(addr)) |
| 22604 | return sema.fail(block, operand_src, "pointer type '{}' requires aligned address", .{ptr_ty.fmt(zcu)}); | |
| 22811 | return sema.fail(block, operand_src, "pointer type '{}' requires aligned address", .{ptr_ty.fmt(pt)}); | |
| 22605 | 22812 | |
| 22606 | 22813 | return switch (ptr_ty.zigTypeTag(zcu)) { |
| 22607 | .Optional => Value.fromInterned((try zcu.intern(.{ .opt = .{ | |
| 22814 | .Optional => Value.fromInterned(try pt.intern(.{ .opt = .{ | |
| 22608 | 22815 | .ty = ptr_ty.toIntern(), |
| 22609 | .val = if (addr == 0) .none else (try zcu.ptrIntValue(ptr_ty.childType(zcu), addr)).toIntern(), | |
| 22610 | } }))), | |
| 22611 | .Pointer => try zcu.ptrIntValue(ptr_ty, addr), | |
| 22816 | .val = if (addr == 0) .none else (try pt.ptrIntValue(ptr_ty.childType(zcu), addr)).toIntern(), | |
| 22817 | } })), | |
| 22818 | .Pointer => try pt.ptrIntValue(ptr_ty, addr), | |
| 22612 | 22819 | else => unreachable, |
| 22613 | 22820 | }; |
| 22614 | 22821 | } |
| 22615 | 22822 | |
| 22616 | 22823 | fn zirErrorCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { |
| 22617 | const mod = sema.mod; | |
| 22824 | const pt = sema.pt; | |
| 22825 | const mod = pt.zcu; | |
| 22618 | 22826 | const ip = &mod.intern_pool; |
| 22619 | 22827 | const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data; |
| 22620 | 22828 | const src = block.nodeOffset(extra.node); |
| ... | ... | @@ -22642,8 +22850,8 @@ fn zirErrorCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData |
| 22642 | 22850 | errdefer msg.destroy(sema.gpa); |
| 22643 | 22851 | const dest_ty = base_dest_ty.errorUnionPayload(mod); |
| 22644 | 22852 | const operand_ty = base_operand_ty.errorUnionPayload(mod); |
| 22645 | try sema.errNote(src, msg, "destination payload is '{}'", .{dest_ty.fmt(mod)}); | |
| 22646 | try sema.errNote(src, msg, "operand payload is '{}'", .{operand_ty.fmt(mod)}); | |
| 22853 | try sema.errNote(src, msg, "destination payload is '{}'", .{dest_ty.fmt(pt)}); | |
| 22854 | try sema.errNote(src, msg, "operand payload is '{}'", .{operand_ty.fmt(pt)}); | |
| 22647 | 22855 | try addDeclaredHereNote(sema, msg, dest_ty); |
| 22648 | 22856 | try addDeclaredHereNote(sema, msg, operand_ty); |
| 22649 | 22857 | break :msg msg; |
| ... | ... | @@ -22684,7 +22892,7 @@ fn zirErrorCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData |
| 22684 | 22892 | }; |
| 22685 | 22893 | if (disjoint and dest_tag != .ErrorUnion) { |
| 22686 | 22894 | return sema.fail(block, src, "error sets '{}' and '{}' have no common errors", .{ |
| 22687 | operand_ty.fmt(sema.mod), dest_ty.fmt(sema.mod), | |
| 22895 | operand_ty.fmt(pt), dest_ty.fmt(pt), | |
| 22688 | 22896 | }); |
| 22689 | 22897 | } |
| 22690 | 22898 | |
| ... | ... | @@ -22700,24 +22908,24 @@ fn zirErrorCast(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData |
| 22700 | 22908 | } |
| 22701 | 22909 | if (!Type.errorSetHasFieldIp(ip, dest_ty.toIntern(), error_name)) { |
| 22702 | 22910 | return sema.fail(block, src, "'error.{}' not a member of error set '{}'", .{ |
| 22703 | error_name.fmt(ip), dest_ty.fmt(sema.mod), | |
| 22911 | error_name.fmt(ip), dest_ty.fmt(pt), | |
| 22704 | 22912 | }); |
| 22705 | 22913 | } |
| 22706 | 22914 | } |
| 22707 | 22915 | |
| 22708 | return Air.internedToRef((try mod.getCoerced(val, base_dest_ty)).toIntern()); | |
| 22916 | return Air.internedToRef((try pt.getCoerced(val, base_dest_ty)).toIntern()); | |
| 22709 | 22917 | } |
| 22710 | 22918 | |
| 22711 | 22919 | try sema.requireRuntimeBlock(block, src, operand_src); |
| 22712 | const err_int_ty = try mod.errorIntType(); | |
| 22920 | const err_int_ty = try pt.errorIntType(); | |
| 22713 | 22921 | if (block.wantSafety() and !dest_ty.isAnyError(mod) and |
| 22714 | 22922 | dest_ty.toIntern() != .adhoc_inferred_error_set_type and |
| 22715 | sema.mod.backendSupportsFeature(.error_set_has_value)) | |
| 22923 | mod.backendSupportsFeature(.error_set_has_value)) | |
| 22716 | 22924 | { |
| 22717 | 22925 | if (dest_tag == .ErrorUnion) { |
| 22718 | 22926 | const err_code = try sema.analyzeErrUnionCode(block, operand_src, operand); |
| 22719 | 22927 | const err_int = try block.addBitCast(err_int_ty, err_code); |
| 22720 | const zero_err = try mod.intRef(try mod.errorIntType(), 0); | |
| 22928 | const zero_err = try pt.intRef(try pt.errorIntType(), 0); | |
| 22721 | 22929 | |
| 22722 | 22930 | const is_zero = try block.addBinOp(.cmp_eq, err_int, zero_err); |
| 22723 | 22931 | if (disjoint) { |
| ... | ... | @@ -22786,7 +22994,8 @@ fn ptrCastFull( |
| 22786 | 22994 | dest_ty: Type, |
| 22787 | 22995 | operation: []const u8, |
| 22788 | 22996 | ) CompileError!Air.Inst.Ref { |
| 22789 | const mod = sema.mod; | |
| 22997 | const pt = sema.pt; | |
| 22998 | const mod = pt.zcu; | |
| 22790 | 22999 | const operand_ty = sema.typeOf(operand); |
| 22791 | 23000 | |
| 22792 | 23001 | try sema.checkPtrType(block, src, dest_ty, true); |
| ... | ... | @@ -22795,8 +23004,8 @@ fn ptrCastFull( |
| 22795 | 23004 | const src_info = operand_ty.ptrInfo(mod); |
| 22796 | 23005 | const dest_info = dest_ty.ptrInfo(mod); |
| 22797 | 23006 | |
| 22798 | try Type.fromInterned(src_info.child).resolveLayout(mod); | |
| 22799 | try Type.fromInterned(dest_info.child).resolveLayout(mod); | |
| 23007 | try Type.fromInterned(src_info.child).resolveLayout(pt); | |
| 23008 | try Type.fromInterned(dest_info.child).resolveLayout(pt); | |
| 22800 | 23009 | |
| 22801 | 23010 | const src_slice_like = src_info.flags.size == .Slice or |
| 22802 | 23011 | (src_info.flags.size == .One and Type.fromInterned(src_info.child).zigTypeTag(mod) == .Array); |
| ... | ... | @@ -22810,12 +23019,12 @@ fn ptrCastFull( |
| 22810 | 23019 | |
| 22811 | 23020 | if (dest_info.flags.size == .Slice) { |
| 22812 | 23021 | const src_elem_size = switch (src_info.flags.size) { |
| 22813 | .Slice => Type.fromInterned(src_info.child).abiSize(mod), | |
| 23022 | .Slice => Type.fromInterned(src_info.child).abiSize(pt), | |
| 22814 | 23023 | // pointer to array |
| 22815 | .One => Type.fromInterned(src_info.child).childType(mod).abiSize(mod), | |
| 23024 | .One => Type.fromInterned(src_info.child).childType(mod).abiSize(pt), | |
| 22816 | 23025 | else => unreachable, |
| 22817 | 23026 | }; |
| 22818 | const dest_elem_size = Type.fromInterned(dest_info.child).abiSize(mod); | |
| 23027 | const dest_elem_size = Type.fromInterned(dest_info.child).abiSize(pt); | |
| 22819 | 23028 | if (src_elem_size != dest_elem_size) { |
| 22820 | 23029 | return sema.fail(block, src, "TODO: implement {s} between slices changing the length", .{operation}); |
| 22821 | 23030 | } |
| ... | ... | @@ -22867,8 +23076,7 @@ fn ptrCastFull( |
| 22867 | 23076 | if (imc_res == .ok) break :check_child; |
| 22868 | 23077 | return sema.failWithOwnedErrorMsg(block, msg: { |
| 22869 | 23078 | const msg = try sema.errMsg(src, "pointer element type '{}' cannot coerce into element type '{}'", .{ |
| 22870 | src_child.fmt(mod), | |
| 22871 | dest_child.fmt(mod), | |
| 23079 | src_child.fmt(pt), dest_child.fmt(pt), | |
| 22872 | 23080 | }); |
| 22873 | 23081 | errdefer msg.destroy(sema.gpa); |
| 22874 | 23082 | try imc_res.report(sema, src, msg); |
| ... | ... | @@ -22881,26 +23089,26 @@ fn ptrCastFull( |
| 22881 | 23089 | if (dest_info.sentinel == .none) break :check_sent; |
| 22882 | 23090 | if (src_info.flags.size == .C) break :check_sent; |
| 22883 | 23091 | if (src_info.sentinel != .none) { |
| 22884 | const coerced_sent = try mod.intern_pool.getCoerced(sema.gpa, src_info.sentinel, dest_info.child); | |
| 23092 | const coerced_sent = try mod.intern_pool.getCoerced(sema.gpa, pt.tid, src_info.sentinel, dest_info.child); | |
| 22885 | 23093 | if (dest_info.sentinel == coerced_sent) break :check_sent; |
| 22886 | 23094 | } |
| 22887 | 23095 | if (src_slice_like and src_info.flags.size == .One and dest_info.flags.size == .Slice) { |
| 22888 | 23096 | // [*]nT -> []T |
| 22889 | 23097 | const arr_ty = Type.fromInterned(src_info.child); |
| 22890 | 23098 | if (arr_ty.sentinel(mod)) |src_sentinel| { |
| 22891 | const coerced_sent = try mod.intern_pool.getCoerced(sema.gpa, src_sentinel.toIntern(), dest_info.child); | |
| 23099 | const coerced_sent = try mod.intern_pool.getCoerced(sema.gpa, pt.tid, src_sentinel.toIntern(), dest_info.child); | |
| 22892 | 23100 | if (dest_info.sentinel == coerced_sent) break :check_sent; |
| 22893 | 23101 | } |
| 22894 | 23102 | } |
| 22895 | 23103 | return sema.failWithOwnedErrorMsg(block, msg: { |
| 22896 | 23104 | const msg = if (src_info.sentinel == .none) blk: { |
| 22897 | 23105 | break :blk try sema.errMsg(src, "destination pointer requires '{}' sentinel", .{ |
| 22898 | Value.fromInterned(dest_info.sentinel).fmtValue(mod, sema), | |
| 23106 | Value.fromInterned(dest_info.sentinel).fmtValue(pt, sema), | |
| 22899 | 23107 | }); |
| 22900 | 23108 | } else blk: { |
| 22901 | 23109 | break :blk try sema.errMsg(src, "pointer sentinel '{}' cannot coerce into pointer sentinel '{}'", .{ |
| 22902 | Value.fromInterned(src_info.sentinel).fmtValue(mod, sema), | |
| 22903 | Value.fromInterned(dest_info.sentinel).fmtValue(mod, sema), | |
| 23110 | Value.fromInterned(src_info.sentinel).fmtValue(pt, sema), | |
| 23111 | Value.fromInterned(dest_info.sentinel).fmtValue(pt, sema), | |
| 22904 | 23112 | }); |
| 22905 | 23113 | }; |
| 22906 | 23114 | errdefer msg.destroy(sema.gpa); |
| ... | ... | @@ -22941,8 +23149,8 @@ fn ptrCastFull( |
| 22941 | 23149 | |
| 22942 | 23150 | return sema.failWithOwnedErrorMsg(block, msg: { |
| 22943 | 23151 | const msg = try sema.errMsg(src, "'{}' could have null values which are illegal in type '{}'", .{ |
| 22944 | operand_ty.fmt(mod), | |
| 22945 | dest_ty.fmt(mod), | |
| 23152 | operand_ty.fmt(pt), | |
| 23153 | dest_ty.fmt(pt), | |
| 22946 | 23154 | }); |
| 22947 | 23155 | errdefer msg.destroy(sema.gpa); |
| 22948 | 23156 | try sema.errNote(src, msg, "use @ptrCast to assert the pointer is not null", .{}); |
| ... | ... | @@ -22956,12 +23164,12 @@ fn ptrCastFull( |
| 22956 | 23164 | const src_align = if (src_info.flags.alignment != .none) |
| 22957 | 23165 | src_info.flags.alignment |
| 22958 | 23166 | else |
| 22959 | Type.fromInterned(src_info.child).abiAlignment(mod); | |
| 23167 | Type.fromInterned(src_info.child).abiAlignment(pt); | |
| 22960 | 23168 | |
| 22961 | 23169 | const dest_align = if (dest_info.flags.alignment != .none) |
| 22962 | 23170 | dest_info.flags.alignment |
| 22963 | 23171 | else |
| 22964 | Type.fromInterned(dest_info.child).abiAlignment(mod); | |
| 23172 | Type.fromInterned(dest_info.child).abiAlignment(pt); | |
| 22965 | 23173 | |
| 22966 | 23174 | if (!flags.align_cast) { |
| 22967 | 23175 | if (dest_align.compare(.gt, src_align)) { |
| ... | ... | @@ -22969,10 +23177,10 @@ fn ptrCastFull( |
| 22969 | 23177 | const msg = try sema.errMsg(src, "{s} increases pointer alignment", .{operation}); |
| 22970 | 23178 | errdefer msg.destroy(sema.gpa); |
| 22971 | 23179 | try sema.errNote(operand_src, msg, "'{}' has alignment '{d}'", .{ |
| 22972 | operand_ty.fmt(mod), src_align.toByteUnits() orelse 0, | |
| 23180 | operand_ty.fmt(pt), src_align.toByteUnits() orelse 0, | |
| 22973 | 23181 | }); |
| 22974 | 23182 | try sema.errNote(src, msg, "'{}' has alignment '{d}'", .{ |
| 22975 | dest_ty.fmt(mod), dest_align.toByteUnits() orelse 0, | |
| 23183 | dest_ty.fmt(pt), dest_align.toByteUnits() orelse 0, | |
| 22976 | 23184 | }); |
| 22977 | 23185 | try sema.errNote(src, msg, "use @alignCast to assert pointer alignment", .{}); |
| 22978 | 23186 | break :msg msg; |
| ... | ... | @@ -22986,10 +23194,10 @@ fn ptrCastFull( |
| 22986 | 23194 | const msg = try sema.errMsg(src, "{s} changes pointer address space", .{operation}); |
| 22987 | 23195 | errdefer msg.destroy(sema.gpa); |
| 22988 | 23196 | try sema.errNote(operand_src, msg, "'{}' has address space '{s}'", .{ |
| 22989 | operand_ty.fmt(mod), @tagName(src_info.flags.address_space), | |
| 23197 | operand_ty.fmt(pt), @tagName(src_info.flags.address_space), | |
| 22990 | 23198 | }); |
| 22991 | 23199 | try sema.errNote(src, msg, "'{}' has address space '{s}'", .{ |
| 22992 | dest_ty.fmt(mod), @tagName(dest_info.flags.address_space), | |
| 23200 | dest_ty.fmt(pt), @tagName(dest_info.flags.address_space), | |
| 22993 | 23201 | }); |
| 22994 | 23202 | try sema.errNote(src, msg, "use @addrSpaceCast to cast pointer address space", .{}); |
| 22995 | 23203 | break :msg msg; |
| ... | ... | @@ -23044,9 +23252,9 @@ fn ptrCastFull( |
| 23044 | 23252 | // Only convert to a many-pointer at first |
| 23045 | 23253 | var info = dest_info; |
| 23046 | 23254 | info.flags.size = .Many; |
| 23047 | const ty = try mod.ptrTypeSema(info); | |
| 23255 | const ty = try pt.ptrTypeSema(info); | |
| 23048 | 23256 | if (dest_ty.zigTypeTag(mod) == .Optional) { |
| 23049 | break :blk try mod.optionalType(ty.toIntern()); | |
| 23257 | break :blk try pt.optionalType(ty.toIntern()); | |
| 23050 | 23258 | } else { |
| 23051 | 23259 | break :blk ty; |
| 23052 | 23260 | } |
| ... | ... | @@ -23059,10 +23267,10 @@ fn ptrCastFull( |
| 23059 | 23267 | return sema.failWithUseOfUndef(block, operand_src); |
| 23060 | 23268 | } |
| 23061 | 23269 | if (!dest_ty.ptrAllowsZero(mod) and ptr_val.isNull(mod)) { |
| 23062 | return sema.fail(block, operand_src, "null pointer casted to type '{}'", .{dest_ty.fmt(mod)}); | |
| 23270 | return sema.fail(block, operand_src, "null pointer casted to type '{}'", .{dest_ty.fmt(pt)}); | |
| 23063 | 23271 | } |
| 23064 | 23272 | if (dest_align.compare(.gt, src_align)) { |
| 23065 | if (try ptr_val.getUnsignedIntAdvanced(mod, .sema)) |addr| { | |
| 23273 | if (try ptr_val.getUnsignedIntAdvanced(pt, .sema)) |addr| { | |
| 23066 | 23274 | if (!dest_align.check(addr)) { |
| 23067 | 23275 | return sema.fail(block, operand_src, "pointer address 0x{X} is not aligned to {d} bytes", .{ |
| 23068 | 23276 | addr, |
| ... | ... | @@ -23072,12 +23280,12 @@ fn ptrCastFull( |
| 23072 | 23280 | } |
| 23073 | 23281 | } |
| 23074 | 23282 | if (dest_info.flags.size == .Slice and src_info.flags.size != .Slice) { |
| 23075 | if (ptr_val.isUndef(mod)) return mod.undefRef(dest_ty); | |
| 23076 | const arr_len = try mod.intValue(Type.usize, Type.fromInterned(src_info.child).arrayLen(mod)); | |
| 23283 | if (ptr_val.isUndef(mod)) return pt.undefRef(dest_ty); | |
| 23284 | const arr_len = try pt.intValue(Type.usize, Type.fromInterned(src_info.child).arrayLen(mod)); | |
| 23077 | 23285 | const ptr_val_key = mod.intern_pool.indexToKey(ptr_val.toIntern()).ptr; |
| 23078 | return Air.internedToRef((try mod.intern(.{ .slice = .{ | |
| 23286 | return Air.internedToRef((try pt.intern(.{ .slice = .{ | |
| 23079 | 23287 | .ty = dest_ty.toIntern(), |
| 23080 | .ptr = try mod.intern(.{ .ptr = .{ | |
| 23288 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 23081 | 23289 | .ty = dest_ty.slicePtrFieldType(mod).toIntern(), |
| 23082 | 23290 | .base_addr = ptr_val_key.base_addr, |
| 23083 | 23291 | .byte_offset = ptr_val_key.byte_offset, |
| ... | ... | @@ -23086,7 +23294,7 @@ fn ptrCastFull( |
| 23086 | 23294 | } }))); |
| 23087 | 23295 | } else { |
| 23088 | 23296 | assert(dest_ptr_ty.eql(dest_ty, mod)); |
| 23089 | return Air.internedToRef((try mod.getCoerced(ptr_val, dest_ty)).toIntern()); | |
| 23297 | return Air.internedToRef((try pt.getCoerced(ptr_val, dest_ty)).toIntern()); | |
| 23090 | 23298 | } |
| 23091 | 23299 | } |
| 23092 | 23300 | } |
| ... | ... | @@ -23112,7 +23320,7 @@ fn ptrCastFull( |
| 23112 | 23320 | try sema.typeHasRuntimeBits(Type.fromInterned(dest_info.child))) |
| 23113 | 23321 | { |
| 23114 | 23322 | const align_bytes_minus_1 = dest_align.toByteUnits().? - 1; |
| 23115 | const align_minus_1 = Air.internedToRef((try mod.intValue(Type.usize, align_bytes_minus_1)).toIntern()); | |
| 23323 | const align_minus_1 = Air.internedToRef((try pt.intValue(Type.usize, align_bytes_minus_1)).toIntern()); | |
| 23116 | 23324 | const ptr_int = try block.addUnOp(.int_from_ptr, ptr); |
| 23117 | 23325 | const remainder = try block.addBinOp(.bit_and, ptr_int, align_minus_1); |
| 23118 | 23326 | const is_aligned = try block.addBinOp(.cmp_eq, remainder, .zero_usize); |
| ... | ... | @@ -23129,9 +23337,9 @@ fn ptrCastFull( |
| 23129 | 23337 | // We can't change address spaces with a bitcast, so this requires two instructions |
| 23130 | 23338 | var intermediate_info = src_info; |
| 23131 | 23339 | intermediate_info.flags.address_space = dest_info.flags.address_space; |
| 23132 | const intermediate_ptr_ty = try mod.ptrTypeSema(intermediate_info); | |
| 23340 | const intermediate_ptr_ty = try pt.ptrTypeSema(intermediate_info); | |
| 23133 | 23341 | const intermediate_ty = if (dest_ptr_ty.zigTypeTag(mod) == .Optional) blk: { |
| 23134 | break :blk try mod.optionalType(intermediate_ptr_ty.toIntern()); | |
| 23342 | break :blk try pt.optionalType(intermediate_ptr_ty.toIntern()); | |
| 23135 | 23343 | } else intermediate_ptr_ty; |
| 23136 | 23344 | const intermediate = try block.addInst(.{ |
| 23137 | 23345 | .tag = .addrspace_cast, |
| ... | ... | @@ -23152,7 +23360,7 @@ fn ptrCastFull( |
| 23152 | 23360 | if (dest_info.flags.size == .Slice and src_info.flags.size != .Slice) { |
| 23153 | 23361 | // We have to construct a slice using the operand's child's array length |
| 23154 | 23362 | // Note that we know from the check at the start of the function that operand_ty is slice-like |
| 23155 | const arr_len = Air.internedToRef((try mod.intValue(Type.usize, Type.fromInterned(src_info.child).arrayLen(mod))).toIntern()); | |
| 23363 | const arr_len = Air.internedToRef((try pt.intValue(Type.usize, Type.fromInterned(src_info.child).arrayLen(mod))).toIntern()); | |
| 23156 | 23364 | return block.addInst(.{ |
| 23157 | 23365 | .tag = .slice, |
| 23158 | 23366 | .data = .{ .ty_pl = .{ |
| ... | ... | @@ -23171,7 +23379,8 @@ fn ptrCastFull( |
| 23171 | 23379 | } |
| 23172 | 23380 | |
| 23173 | 23381 | fn zirPtrCastNoDest(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { |
| 23174 | const mod = sema.mod; | |
| 23382 | const pt = sema.pt; | |
| 23383 | const mod = pt.zcu; | |
| 23175 | 23384 | const FlagsInt = @typeInfo(Zir.Inst.FullPtrCastFlags).Struct.backing_integer.?; |
| 23176 | 23385 | const flags: Zir.Inst.FullPtrCastFlags = @bitCast(@as(FlagsInt, @truncate(extended.small))); |
| 23177 | 23386 | const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; |
| ... | ... | @@ -23186,15 +23395,15 @@ fn zirPtrCastNoDest(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Inst |
| 23186 | 23395 | if (flags.volatile_cast) ptr_info.flags.is_volatile = false; |
| 23187 | 23396 | |
| 23188 | 23397 | const dest_ty = blk: { |
| 23189 | const dest_ty = try mod.ptrTypeSema(ptr_info); | |
| 23398 | const dest_ty = try pt.ptrTypeSema(ptr_info); | |
| 23190 | 23399 | if (operand_ty.zigTypeTag(mod) == .Optional) { |
| 23191 | break :blk try mod.optionalType(dest_ty.toIntern()); | |
| 23400 | break :blk try pt.optionalType(dest_ty.toIntern()); | |
| 23192 | 23401 | } |
| 23193 | 23402 | break :blk dest_ty; |
| 23194 | 23403 | }; |
| 23195 | 23404 | |
| 23196 | 23405 | if (try sema.resolveValue(operand)) |operand_val| { |
| 23197 | return Air.internedToRef((try mod.getCoerced(operand_val, dest_ty)).toIntern()); | |
| 23406 | return Air.internedToRef((try pt.getCoerced(operand_val, dest_ty)).toIntern()); | |
| 23198 | 23407 | } |
| 23199 | 23408 | |
| 23200 | 23409 | try sema.requireRuntimeBlock(block, src, null); |
| ... | ... | @@ -23204,7 +23413,8 @@ fn zirPtrCastNoDest(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Inst |
| 23204 | 23413 | } |
| 23205 | 23414 | |
| 23206 | 23415 | fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 23207 | const mod = sema.mod; | |
| 23416 | const pt = sema.pt; | |
| 23417 | const mod = pt.zcu; | |
| 23208 | 23418 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 23209 | 23419 | const src = block.nodeOffset(inst_data.src_node); |
| 23210 | 23420 | const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| ... | ... | @@ -23218,7 +23428,7 @@ fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 23218 | 23428 | const operand_is_vector = operand_ty.zigTypeTag(mod) == .Vector; |
| 23219 | 23429 | const dest_is_vector = dest_ty.zigTypeTag(mod) == .Vector; |
| 23220 | 23430 | if (operand_is_vector != dest_is_vector) { |
| 23221 | return sema.fail(block, operand_src, "expected type '{}', found '{}'", .{ dest_ty.fmt(mod), operand_ty.fmt(mod) }); | |
| 23431 | return sema.fail(block, operand_src, "expected type '{}', found '{}'", .{ dest_ty.fmt(pt), operand_ty.fmt(pt) }); | |
| 23222 | 23432 | } |
| 23223 | 23433 | |
| 23224 | 23434 | if (dest_scalar_ty.zigTypeTag(mod) == .ComptimeInt) { |
| ... | ... | @@ -23239,7 +23449,7 @@ fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 23239 | 23449 | |
| 23240 | 23450 | if (operand_info.signedness != dest_info.signedness) { |
| 23241 | 23451 | return sema.fail(block, operand_src, "expected {s} integer type, found '{}'", .{ |
| 23242 | @tagName(dest_info.signedness), operand_ty.fmt(mod), | |
| 23452 | @tagName(dest_info.signedness), operand_ty.fmt(pt), | |
| 23243 | 23453 | }); |
| 23244 | 23454 | } |
| 23245 | 23455 | if (operand_info.bits < dest_info.bits) { |
| ... | ... | @@ -23247,7 +23457,7 @@ fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 23247 | 23457 | const msg = try sema.errMsg( |
| 23248 | 23458 | src, |
| 23249 | 23459 | "destination type '{}' has more bits than source type '{}'", |
| 23250 | .{ dest_ty.fmt(mod), operand_ty.fmt(mod) }, | |
| 23460 | .{ dest_ty.fmt(pt), operand_ty.fmt(pt) }, | |
| 23251 | 23461 | ); |
| 23252 | 23462 | errdefer msg.destroy(sema.gpa); |
| 23253 | 23463 | try sema.errNote(src, msg, "destination type has {d} bits", .{ |
| ... | ... | @@ -23263,20 +23473,20 @@ fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 23263 | 23473 | } |
| 23264 | 23474 | |
| 23265 | 23475 | if (try sema.resolveValueIntable(operand)) |val| { |
| 23266 | if (val.isUndef(mod)) return mod.undefRef(dest_ty); | |
| 23476 | if (val.isUndef(mod)) return pt.undefRef(dest_ty); | |
| 23267 | 23477 | if (!dest_is_vector) { |
| 23268 | return Air.internedToRef((try mod.getCoerced( | |
| 23269 | try val.intTrunc(operand_ty, sema.arena, dest_info.signedness, dest_info.bits, mod), | |
| 23478 | return Air.internedToRef((try pt.getCoerced( | |
| 23479 | try val.intTrunc(operand_ty, sema.arena, dest_info.signedness, dest_info.bits, pt), | |
| 23270 | 23480 | dest_ty, |
| 23271 | 23481 | )).toIntern()); |
| 23272 | 23482 | } |
| 23273 | 23483 | const elems = try sema.arena.alloc(InternPool.Index, operand_ty.vectorLen(mod)); |
| 23274 | 23484 | for (elems, 0..) |*elem, i| { |
| 23275 | const elem_val = try val.elemValue(mod, i); | |
| 23276 | const uncoerced_elem = try elem_val.intTrunc(operand_scalar_ty, sema.arena, dest_info.signedness, dest_info.bits, mod); | |
| 23277 | elem.* = (try mod.getCoerced(uncoerced_elem, dest_scalar_ty)).toIntern(); | |
| 23485 | const elem_val = try val.elemValue(pt, i); | |
| 23486 | const uncoerced_elem = try elem_val.intTrunc(operand_scalar_ty, sema.arena, dest_info.signedness, dest_info.bits, pt); | |
| 23487 | elem.* = (try pt.getCoerced(uncoerced_elem, dest_scalar_ty)).toIntern(); | |
| 23278 | 23488 | } |
| 23279 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 23489 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 23280 | 23490 | .ty = dest_ty.toIntern(), |
| 23281 | 23491 | .storage = .{ .elems = elems }, |
| 23282 | 23492 | } }))); |
| ... | ... | @@ -23291,9 +23501,10 @@ fn zirBitCount( |
| 23291 | 23501 | block: *Block, |
| 23292 | 23502 | inst: Zir.Inst.Index, |
| 23293 | 23503 | air_tag: Air.Inst.Tag, |
| 23294 | comptime comptimeOp: fn (val: Value, ty: Type, mod: *Module) u64, | |
| 23504 | comptime comptimeOp: fn (val: Value, ty: Type, pt: Zcu.PerThread) u64, | |
| 23295 | 23505 | ) CompileError!Air.Inst.Ref { |
| 23296 | const mod = sema.mod; | |
| 23506 | const pt = sema.pt; | |
| 23507 | const mod = pt.zcu; | |
| 23297 | 23508 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 23298 | 23509 | const src = block.nodeOffset(inst_data.src_node); |
| 23299 | 23510 | const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| ... | ... | @@ -23306,25 +23517,25 @@ fn zirBitCount( |
| 23306 | 23517 | return Air.internedToRef(val.toIntern()); |
| 23307 | 23518 | } |
| 23308 | 23519 | |
| 23309 | const result_scalar_ty = try mod.smallestUnsignedInt(bits); | |
| 23520 | const result_scalar_ty = try pt.smallestUnsignedInt(bits); | |
| 23310 | 23521 | switch (operand_ty.zigTypeTag(mod)) { |
| 23311 | 23522 | .Vector => { |
| 23312 | 23523 | const vec_len = operand_ty.vectorLen(mod); |
| 23313 | const result_ty = try mod.vectorType(.{ | |
| 23524 | const result_ty = try pt.vectorType(.{ | |
| 23314 | 23525 | .len = vec_len, |
| 23315 | 23526 | .child = result_scalar_ty.toIntern(), |
| 23316 | 23527 | }); |
| 23317 | 23528 | if (try sema.resolveValue(operand)) |val| { |
| 23318 | if (val.isUndef(mod)) return mod.undefRef(result_ty); | |
| 23529 | if (val.isUndef(mod)) return pt.undefRef(result_ty); | |
| 23319 | 23530 | |
| 23320 | 23531 | const elems = try sema.arena.alloc(InternPool.Index, vec_len); |
| 23321 | 23532 | const scalar_ty = operand_ty.scalarType(mod); |
| 23322 | 23533 | for (elems, 0..) |*elem, i| { |
| 23323 | const elem_val = try val.elemValue(mod, i); | |
| 23324 | const count = comptimeOp(elem_val, scalar_ty, mod); | |
| 23325 | elem.* = (try mod.intValue(result_scalar_ty, count)).toIntern(); | |
| 23534 | const elem_val = try val.elemValue(pt, i); | |
| 23535 | const count = comptimeOp(elem_val, scalar_ty, pt); | |
| 23536 | elem.* = (try pt.intValue(result_scalar_ty, count)).toIntern(); | |
| 23326 | 23537 | } |
| 23327 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 23538 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 23328 | 23539 | .ty = result_ty.toIntern(), |
| 23329 | 23540 | .storage = .{ .elems = elems }, |
| 23330 | 23541 | } }))); |
| ... | ... | @@ -23335,8 +23546,8 @@ fn zirBitCount( |
| 23335 | 23546 | }, |
| 23336 | 23547 | .Int => { |
| 23337 | 23548 | if (try sema.resolveValueResolveLazy(operand)) |val| { |
| 23338 | if (val.isUndef(mod)) return mod.undefRef(result_scalar_ty); | |
| 23339 | return mod.intRef(result_scalar_ty, comptimeOp(val, operand_ty, mod)); | |
| 23549 | if (val.isUndef(mod)) return pt.undefRef(result_scalar_ty); | |
| 23550 | return pt.intRef(result_scalar_ty, comptimeOp(val, operand_ty, pt)); | |
| 23340 | 23551 | } else { |
| 23341 | 23552 | try sema.requireRuntimeBlock(block, src, operand_src); |
| 23342 | 23553 | return block.addTyOp(air_tag, result_scalar_ty, operand); |
| ... | ... | @@ -23347,7 +23558,8 @@ fn zirBitCount( |
| 23347 | 23558 | } |
| 23348 | 23559 | |
| 23349 | 23560 | fn zirByteSwap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 23350 | const mod = sema.mod; | |
| 23561 | const pt = sema.pt; | |
| 23562 | const mod = pt.zcu; | |
| 23351 | 23563 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].un_node; |
| 23352 | 23564 | const src = block.nodeOffset(inst_data.src_node); |
| 23353 | 23565 | const operand_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| ... | ... | @@ -23360,7 +23572,7 @@ fn zirByteSwap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 23360 | 23572 | block, |
| 23361 | 23573 | operand_src, |
| 23362 | 23574 | "@byteSwap requires the number of bits to be evenly divisible by 8, but {} has {} bits", |
| 23363 | .{ scalar_ty.fmt(mod), bits }, | |
| 23575 | .{ scalar_ty.fmt(pt), bits }, | |
| 23364 | 23576 | ); |
| 23365 | 23577 | } |
| 23366 | 23578 | |
| ... | ... | @@ -23371,8 +23583,8 @@ fn zirByteSwap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 23371 | 23583 | switch (operand_ty.zigTypeTag(mod)) { |
| 23372 | 23584 | .Int => { |
| 23373 | 23585 | const runtime_src = if (try sema.resolveValue(operand)) |val| { |
| 23374 | if (val.isUndef(mod)) return mod.undefRef(operand_ty); | |
| 23375 | const result_val = try val.byteSwap(operand_ty, mod, sema.arena); | |
| 23586 | if (val.isUndef(mod)) return pt.undefRef(operand_ty); | |
| 23587 | const result_val = try val.byteSwap(operand_ty, pt, sema.arena); | |
| 23376 | 23588 | return Air.internedToRef(result_val.toIntern()); |
| 23377 | 23589 | } else operand_src; |
| 23378 | 23590 | |
| ... | ... | @@ -23382,15 +23594,15 @@ fn zirByteSwap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 23382 | 23594 | .Vector => { |
| 23383 | 23595 | const runtime_src = if (try sema.resolveValue(operand)) |val| { |
| 23384 | 23596 | if (val.isUndef(mod)) |
| 23385 | return mod.undefRef(operand_ty); | |
| 23597 | return pt.undefRef(operand_ty); | |
| 23386 | 23598 | |
| 23387 | 23599 | const vec_len = operand_ty.vectorLen(mod); |
| 23388 | 23600 | const elems = try sema.arena.alloc(InternPool.Index, vec_len); |
| 23389 | 23601 | for (elems, 0..) |*elem, i| { |
| 23390 | const elem_val = try val.elemValue(mod, i); | |
| 23391 | elem.* = (try elem_val.byteSwap(scalar_ty, mod, sema.arena)).toIntern(); | |
| 23602 | const elem_val = try val.elemValue(pt, i); | |
| 23603 | elem.* = (try elem_val.byteSwap(scalar_ty, pt, sema.arena)).toIntern(); | |
| 23392 | 23604 | } |
| 23393 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 23605 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 23394 | 23606 | .ty = operand_ty.toIntern(), |
| 23395 | 23607 | .storage = .{ .elems = elems }, |
| 23396 | 23608 | } }))); |
| ... | ... | @@ -23415,12 +23627,13 @@ fn zirBitReverse(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 23415 | 23627 | return Air.internedToRef(val.toIntern()); |
| 23416 | 23628 | } |
| 23417 | 23629 | |
| 23418 | const mod = sema.mod; | |
| 23630 | const pt = sema.pt; | |
| 23631 | const mod = pt.zcu; | |
| 23419 | 23632 | switch (operand_ty.zigTypeTag(mod)) { |
| 23420 | 23633 | .Int => { |
| 23421 | 23634 | const runtime_src = if (try sema.resolveValue(operand)) |val| { |
| 23422 | if (val.isUndef(mod)) return mod.undefRef(operand_ty); | |
| 23423 | const result_val = try val.bitReverse(operand_ty, mod, sema.arena); | |
| 23635 | if (val.isUndef(mod)) return pt.undefRef(operand_ty); | |
| 23636 | const result_val = try val.bitReverse(operand_ty, pt, sema.arena); | |
| 23424 | 23637 | return Air.internedToRef(result_val.toIntern()); |
| 23425 | 23638 | } else operand_src; |
| 23426 | 23639 | |
| ... | ... | @@ -23430,15 +23643,15 @@ fn zirBitReverse(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 23430 | 23643 | .Vector => { |
| 23431 | 23644 | const runtime_src = if (try sema.resolveValue(operand)) |val| { |
| 23432 | 23645 | if (val.isUndef(mod)) |
| 23433 | return mod.undefRef(operand_ty); | |
| 23646 | return pt.undefRef(operand_ty); | |
| 23434 | 23647 | |
| 23435 | 23648 | const vec_len = operand_ty.vectorLen(mod); |
| 23436 | 23649 | const elems = try sema.arena.alloc(InternPool.Index, vec_len); |
| 23437 | 23650 | for (elems, 0..) |*elem, i| { |
| 23438 | const elem_val = try val.elemValue(mod, i); | |
| 23439 | elem.* = (try elem_val.bitReverse(scalar_ty, mod, sema.arena)).toIntern(); | |
| 23651 | const elem_val = try val.elemValue(pt, i); | |
| 23652 | elem.* = (try elem_val.bitReverse(scalar_ty, pt, sema.arena)).toIntern(); | |
| 23440 | 23653 | } |
| 23441 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 23654 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 23442 | 23655 | .ty = operand_ty.toIntern(), |
| 23443 | 23656 | .storage = .{ .elems = elems }, |
| 23444 | 23657 | } }))); |
| ... | ... | @@ -23453,13 +23666,13 @@ fn zirBitReverse(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 23453 | 23666 | |
| 23454 | 23667 | fn zirBitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 23455 | 23668 | const offset = try sema.bitOffsetOf(block, inst); |
| 23456 | return sema.mod.intRef(Type.comptime_int, offset); | |
| 23669 | return sema.pt.intRef(Type.comptime_int, offset); | |
| 23457 | 23670 | } |
| 23458 | 23671 | |
| 23459 | 23672 | fn zirOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 23460 | 23673 | const offset = try sema.bitOffsetOf(block, inst); |
| 23461 | 23674 | // TODO reminder to make this a compile error for packed structs |
| 23462 | return sema.mod.intRef(Type.comptime_int, offset / 8); | |
| 23675 | return sema.pt.intRef(Type.comptime_int, offset / 8); | |
| 23463 | 23676 | } |
| 23464 | 23677 | |
| 23465 | 23678 | fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u64 { |
| ... | ... | @@ -23474,12 +23687,13 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6 |
| 23474 | 23687 | .needed_comptime_reason = "name of field must be comptime-known", |
| 23475 | 23688 | }); |
| 23476 | 23689 | |
| 23477 | const mod = sema.mod; | |
| 23690 | const pt = sema.pt; | |
| 23691 | const mod = pt.zcu; | |
| 23478 | 23692 | const ip = &mod.intern_pool; |
| 23479 | try ty.resolveLayout(mod); | |
| 23693 | try ty.resolveLayout(pt); | |
| 23480 | 23694 | switch (ty.zigTypeTag(mod)) { |
| 23481 | 23695 | .Struct => {}, |
| 23482 | else => return sema.fail(block, lhs_src, "expected struct type, found '{}'", .{ty.fmt(mod)}), | |
| 23696 | else => return sema.fail(block, lhs_src, "expected struct type, found '{}'", .{ty.fmt(pt)}), | |
| 23483 | 23697 | } |
| 23484 | 23698 | |
| 23485 | 23699 | const field_index = if (ty.isTuple(mod)) blk: { |
| ... | ... | @@ -23502,28 +23716,30 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6 |
| 23502 | 23716 | return bit_sum; |
| 23503 | 23717 | } |
| 23504 | 23718 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[i]); |
| 23505 | bit_sum += field_ty.bitSize(mod); | |
| 23719 | bit_sum += field_ty.bitSize(pt); | |
| 23506 | 23720 | } else unreachable; |
| 23507 | 23721 | }, |
| 23508 | else => return ty.structFieldOffset(field_index, mod) * 8, | |
| 23722 | else => return ty.structFieldOffset(field_index, pt) * 8, | |
| 23509 | 23723 | } |
| 23510 | 23724 | } |
| 23511 | 23725 | |
| 23512 | 23726 | fn checkNamespaceType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!void { |
| 23513 | const mod = sema.mod; | |
| 23727 | const pt = sema.pt; | |
| 23728 | const mod = pt.zcu; | |
| 23514 | 23729 | switch (ty.zigTypeTag(mod)) { |
| 23515 | 23730 | .Struct, .Enum, .Union, .Opaque => return, |
| 23516 | else => return sema.fail(block, src, "expected struct, enum, union, or opaque; found '{}'", .{ty.fmt(mod)}), | |
| 23731 | else => return sema.fail(block, src, "expected struct, enum, union, or opaque; found '{}'", .{ty.fmt(pt)}), | |
| 23517 | 23732 | } |
| 23518 | 23733 | } |
| 23519 | 23734 | |
| 23520 | 23735 | /// Returns `true` if the type was a comptime_int. |
| 23521 | 23736 | fn checkIntType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!bool { |
| 23522 | const mod = sema.mod; | |
| 23737 | const pt = sema.pt; | |
| 23738 | const mod = pt.zcu; | |
| 23523 | 23739 | switch (try ty.zigTypeTagOrPoison(mod)) { |
| 23524 | 23740 | .ComptimeInt => return true, |
| 23525 | 23741 | .Int => return false, |
| 23526 | else => return sema.fail(block, src, "expected integer type, found '{}'", .{ty.fmt(mod)}), | |
| 23742 | else => return sema.fail(block, src, "expected integer type, found '{}'", .{ty.fmt(pt)}), | |
| 23527 | 23743 | } |
| 23528 | 23744 | } |
| 23529 | 23745 | |
| ... | ... | @@ -23533,7 +23749,8 @@ fn checkInvalidPtrArithmetic( |
| 23533 | 23749 | src: LazySrcLoc, |
| 23534 | 23750 | ty: Type, |
| 23535 | 23751 | ) CompileError!void { |
| 23536 | const mod = sema.mod; | |
| 23752 | const pt = sema.pt; | |
| 23753 | const mod = pt.zcu; | |
| 23537 | 23754 | switch (try ty.zigTypeTagOrPoison(mod)) { |
| 23538 | 23755 | .Pointer => switch (ty.ptrSize(mod)) { |
| 23539 | 23756 | .One, .Slice => return, |
| ... | ... | @@ -23573,7 +23790,8 @@ fn checkPtrOperand( |
| 23573 | 23790 | ty_src: LazySrcLoc, |
| 23574 | 23791 | ty: Type, |
| 23575 | 23792 | ) CompileError!void { |
| 23576 | const mod = sema.mod; | |
| 23793 | const pt = sema.pt; | |
| 23794 | const mod = pt.zcu; | |
| 23577 | 23795 | switch (ty.zigTypeTag(mod)) { |
| 23578 | 23796 | .Pointer => return, |
| 23579 | 23797 | .Fn => { |
| ... | ... | @@ -23581,7 +23799,7 @@ fn checkPtrOperand( |
| 23581 | 23799 | const msg = try sema.errMsg( |
| 23582 | 23800 | ty_src, |
| 23583 | 23801 | "expected pointer, found '{}'", |
| 23584 | .{ty.fmt(mod)}, | |
| 23802 | .{ty.fmt(pt)}, | |
| 23585 | 23803 | ); |
| 23586 | 23804 | errdefer msg.destroy(sema.gpa); |
| 23587 | 23805 | |
| ... | ... | @@ -23594,7 +23812,7 @@ fn checkPtrOperand( |
| 23594 | 23812 | .Optional => if (ty.childType(mod).zigTypeTag(mod) == .Pointer) return, |
| 23595 | 23813 | else => {}, |
| 23596 | 23814 | } |
| 23597 | return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty.fmt(mod)}); | |
| 23815 | return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty.fmt(pt)}); | |
| 23598 | 23816 | } |
| 23599 | 23817 | |
| 23600 | 23818 | fn checkPtrType( |
| ... | ... | @@ -23604,7 +23822,8 @@ fn checkPtrType( |
| 23604 | 23822 | ty: Type, |
| 23605 | 23823 | allow_slice: bool, |
| 23606 | 23824 | ) CompileError!void { |
| 23607 | const mod = sema.mod; | |
| 23825 | const pt = sema.pt; | |
| 23826 | const mod = pt.zcu; | |
| 23608 | 23827 | switch (ty.zigTypeTag(mod)) { |
| 23609 | 23828 | .Pointer => if (allow_slice or !ty.isSlice(mod)) return, |
| 23610 | 23829 | .Fn => { |
| ... | ... | @@ -23612,7 +23831,7 @@ fn checkPtrType( |
| 23612 | 23831 | const msg = try sema.errMsg( |
| 23613 | 23832 | ty_src, |
| 23614 | 23833 | "expected pointer type, found '{}'", |
| 23615 | .{ty.fmt(mod)}, | |
| 23834 | .{ty.fmt(pt)}, | |
| 23616 | 23835 | ); |
| 23617 | 23836 | errdefer msg.destroy(sema.gpa); |
| 23618 | 23837 | |
| ... | ... | @@ -23625,7 +23844,7 @@ fn checkPtrType( |
| 23625 | 23844 | .Optional => if (ty.childType(mod).zigTypeTag(mod) == .Pointer) return, |
| 23626 | 23845 | else => {}, |
| 23627 | 23846 | } |
| 23628 | return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty.fmt(mod)}); | |
| 23847 | return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty.fmt(pt)}); | |
| 23629 | 23848 | } |
| 23630 | 23849 | |
| 23631 | 23850 | fn checkVectorElemType( |
| ... | ... | @@ -23634,13 +23853,14 @@ fn checkVectorElemType( |
| 23634 | 23853 | ty_src: LazySrcLoc, |
| 23635 | 23854 | ty: Type, |
| 23636 | 23855 | ) CompileError!void { |
| 23637 | const mod = sema.mod; | |
| 23856 | const pt = sema.pt; | |
| 23857 | const mod = pt.zcu; | |
| 23638 | 23858 | switch (ty.zigTypeTag(mod)) { |
| 23639 | 23859 | .Int, .Float, .Bool => return, |
| 23640 | 23860 | .Optional, .Pointer => if (ty.isPtrAtRuntime(mod)) return, |
| 23641 | 23861 | else => {}, |
| 23642 | 23862 | } |
| 23643 | return sema.fail(block, ty_src, "expected integer, float, bool, or pointer for the vector element type; found '{}'", .{ty.fmt(mod)}); | |
| 23863 | return sema.fail(block, ty_src, "expected integer, float, bool, or pointer for the vector element type; found '{}'", .{ty.fmt(pt)}); | |
| 23644 | 23864 | } |
| 23645 | 23865 | |
| 23646 | 23866 | fn checkFloatType( |
| ... | ... | @@ -23649,10 +23869,11 @@ fn checkFloatType( |
| 23649 | 23869 | ty_src: LazySrcLoc, |
| 23650 | 23870 | ty: Type, |
| 23651 | 23871 | ) CompileError!void { |
| 23652 | const mod = sema.mod; | |
| 23872 | const pt = sema.pt; | |
| 23873 | const mod = pt.zcu; | |
| 23653 | 23874 | switch (ty.zigTypeTag(mod)) { |
| 23654 | 23875 | .ComptimeInt, .ComptimeFloat, .Float => {}, |
| 23655 | else => return sema.fail(block, ty_src, "expected float type, found '{}'", .{ty.fmt(mod)}), | |
| 23876 | else => return sema.fail(block, ty_src, "expected float type, found '{}'", .{ty.fmt(pt)}), | |
| 23656 | 23877 | } |
| 23657 | 23878 | } |
| 23658 | 23879 | |
| ... | ... | @@ -23662,14 +23883,15 @@ fn checkNumericType( |
| 23662 | 23883 | ty_src: LazySrcLoc, |
| 23663 | 23884 | ty: Type, |
| 23664 | 23885 | ) CompileError!void { |
| 23665 | const mod = sema.mod; | |
| 23886 | const pt = sema.pt; | |
| 23887 | const mod = pt.zcu; | |
| 23666 | 23888 | switch (ty.zigTypeTag(mod)) { |
| 23667 | 23889 | .ComptimeFloat, .Float, .ComptimeInt, .Int => {}, |
| 23668 | 23890 | .Vector => switch (ty.childType(mod).zigTypeTag(mod)) { |
| 23669 | 23891 | .ComptimeFloat, .Float, .ComptimeInt, .Int => {}, |
| 23670 | 23892 | else => |t| return sema.fail(block, ty_src, "expected number, found '{}'", .{t}), |
| 23671 | 23893 | }, |
| 23672 | else => return sema.fail(block, ty_src, "expected number, found '{}'", .{ty.fmt(mod)}), | |
| 23894 | else => return sema.fail(block, ty_src, "expected number, found '{}'", .{ty.fmt(pt)}), | |
| 23673 | 23895 | } |
| 23674 | 23896 | } |
| 23675 | 23897 | |
| ... | ... | @@ -23683,7 +23905,8 @@ fn checkAtomicPtrOperand( |
| 23683 | 23905 | ptr_src: LazySrcLoc, |
| 23684 | 23906 | ptr_const: bool, |
| 23685 | 23907 | ) CompileError!Air.Inst.Ref { |
| 23686 | const mod = sema.mod; | |
| 23908 | const pt = sema.pt; | |
| 23909 | const mod = pt.zcu; | |
| 23687 | 23910 | var diag: Module.AtomicPtrAlignmentDiagnostics = .{}; |
| 23688 | 23911 | const alignment = mod.atomicPtrAlignment(elem_ty, &diag) catch |err| switch (err) { |
| 23689 | 23912 | error.OutOfMemory => return error.OutOfMemory, |
| ... | ... | @@ -23703,7 +23926,7 @@ fn checkAtomicPtrOperand( |
| 23703 | 23926 | block, |
| 23704 | 23927 | elem_ty_src, |
| 23705 | 23928 | "expected bool, integer, float, enum, or pointer type; found '{}'", |
| 23706 | .{elem_ty.fmt(mod)}, | |
| 23929 | .{elem_ty.fmt(pt)}, | |
| 23707 | 23930 | ), |
| 23708 | 23931 | }; |
| 23709 | 23932 | |
| ... | ... | @@ -23719,7 +23942,7 @@ fn checkAtomicPtrOperand( |
| 23719 | 23942 | const ptr_data = switch (try ptr_ty.zigTypeTagOrPoison(mod)) { |
| 23720 | 23943 | .Pointer => ptr_ty.ptrInfo(mod), |
| 23721 | 23944 | else => { |
| 23722 | const wanted_ptr_ty = try mod.ptrTypeSema(wanted_ptr_data); | |
| 23945 | const wanted_ptr_ty = try pt.ptrTypeSema(wanted_ptr_data); | |
| 23723 | 23946 | _ = try sema.coerce(block, wanted_ptr_ty, ptr, ptr_src); |
| 23724 | 23947 | unreachable; |
| 23725 | 23948 | }, |
| ... | ... | @@ -23729,7 +23952,7 @@ fn checkAtomicPtrOperand( |
| 23729 | 23952 | wanted_ptr_data.flags.is_allowzero = ptr_data.flags.is_allowzero; |
| 23730 | 23953 | wanted_ptr_data.flags.is_volatile = ptr_data.flags.is_volatile; |
| 23731 | 23954 | |
| 23732 | const wanted_ptr_ty = try mod.ptrTypeSema(wanted_ptr_data); | |
| 23955 | const wanted_ptr_ty = try pt.ptrTypeSema(wanted_ptr_data); | |
| 23733 | 23956 | const casted_ptr = try sema.coerce(block, wanted_ptr_ty, ptr, ptr_src); |
| 23734 | 23957 | |
| 23735 | 23958 | return casted_ptr; |
| ... | ... | @@ -23754,7 +23977,8 @@ fn checkIntOrVector( |
| 23754 | 23977 | operand: Air.Inst.Ref, |
| 23755 | 23978 | operand_src: LazySrcLoc, |
| 23756 | 23979 | ) CompileError!Type { |
| 23757 | const mod = sema.mod; | |
| 23980 | const pt = sema.pt; | |
| 23981 | const mod = pt.zcu; | |
| 23758 | 23982 | const operand_ty = sema.typeOf(operand); |
| 23759 | 23983 | switch (try operand_ty.zigTypeTagOrPoison(mod)) { |
| 23760 | 23984 | .Int => return operand_ty, |
| ... | ... | @@ -23763,12 +23987,12 @@ fn checkIntOrVector( |
| 23763 | 23987 | switch (try elem_ty.zigTypeTagOrPoison(mod)) { |
| 23764 | 23988 | .Int => return elem_ty, |
| 23765 | 23989 | else => return sema.fail(block, operand_src, "expected vector of integers; found vector of '{}'", .{ |
| 23766 | elem_ty.fmt(mod), | |
| 23990 | elem_ty.fmt(pt), | |
| 23767 | 23991 | }), |
| 23768 | 23992 | } |
| 23769 | 23993 | }, |
| 23770 | 23994 | else => return sema.fail(block, operand_src, "expected integer or vector, found '{}'", .{ |
| 23771 | operand_ty.fmt(mod), | |
| 23995 | operand_ty.fmt(pt), | |
| 23772 | 23996 | }), |
| 23773 | 23997 | } |
| 23774 | 23998 | } |
| ... | ... | @@ -23779,7 +24003,8 @@ fn checkIntOrVectorAllowComptime( |
| 23779 | 24003 | operand_ty: Type, |
| 23780 | 24004 | operand_src: LazySrcLoc, |
| 23781 | 24005 | ) CompileError!Type { |
| 23782 | const mod = sema.mod; | |
| 24006 | const pt = sema.pt; | |
| 24007 | const mod = pt.zcu; | |
| 23783 | 24008 | switch (try operand_ty.zigTypeTagOrPoison(mod)) { |
| 23784 | 24009 | .Int, .ComptimeInt => return operand_ty, |
| 23785 | 24010 | .Vector => { |
| ... | ... | @@ -23787,12 +24012,12 @@ fn checkIntOrVectorAllowComptime( |
| 23787 | 24012 | switch (try elem_ty.zigTypeTagOrPoison(mod)) { |
| 23788 | 24013 | .Int, .ComptimeInt => return elem_ty, |
| 23789 | 24014 | else => return sema.fail(block, operand_src, "expected vector of integers; found vector of '{}'", .{ |
| 23790 | elem_ty.fmt(mod), | |
| 24015 | elem_ty.fmt(pt), | |
| 23791 | 24016 | }), |
| 23792 | 24017 | } |
| 23793 | 24018 | }, |
| 23794 | 24019 | else => return sema.fail(block, operand_src, "expected integer or vector, found '{}'", .{ |
| 23795 | operand_ty.fmt(mod), | |
| 24020 | operand_ty.fmt(pt), | |
| 23796 | 24021 | }), |
| 23797 | 24022 | } |
| 23798 | 24023 | } |
| ... | ... | @@ -23819,7 +24044,8 @@ fn checkSimdBinOp( |
| 23819 | 24044 | lhs_src: LazySrcLoc, |
| 23820 | 24045 | rhs_src: LazySrcLoc, |
| 23821 | 24046 | ) CompileError!SimdBinOp { |
| 23822 | const mod = sema.mod; | |
| 24047 | const pt = sema.pt; | |
| 24048 | const mod = pt.zcu; | |
| 23823 | 24049 | const lhs_ty = sema.typeOf(uncasted_lhs); |
| 23824 | 24050 | const rhs_ty = sema.typeOf(uncasted_rhs); |
| 23825 | 24051 | |
| ... | ... | @@ -23851,7 +24077,8 @@ fn checkVectorizableBinaryOperands( |
| 23851 | 24077 | lhs_src: LazySrcLoc, |
| 23852 | 24078 | rhs_src: LazySrcLoc, |
| 23853 | 24079 | ) CompileError!void { |
| 23854 | const mod = sema.mod; | |
| 24080 | const pt = sema.pt; | |
| 24081 | const mod = pt.zcu; | |
| 23855 | 24082 | const lhs_zig_ty_tag = try lhs_ty.zigTypeTagOrPoison(mod); |
| 23856 | 24083 | const rhs_zig_ty_tag = try rhs_ty.zigTypeTagOrPoison(mod); |
| 23857 | 24084 | if (lhs_zig_ty_tag != .Vector and rhs_zig_ty_tag != .Vector) return; |
| ... | ... | @@ -23881,7 +24108,7 @@ fn checkVectorizableBinaryOperands( |
| 23881 | 24108 | } else { |
| 23882 | 24109 | const msg = msg: { |
| 23883 | 24110 | const msg = try sema.errMsg(src, "mixed scalar and vector operands: '{}' and '{}'", .{ |
| 23884 | lhs_ty.fmt(mod), rhs_ty.fmt(mod), | |
| 24111 | lhs_ty.fmt(pt), rhs_ty.fmt(pt), | |
| 23885 | 24112 | }); |
| 23886 | 24113 | errdefer msg.destroy(sema.gpa); |
| 23887 | 24114 | if (lhs_is_vector) { |
| ... | ... | @@ -23903,10 +24130,11 @@ fn resolveExportOptions( |
| 23903 | 24130 | src: LazySrcLoc, |
| 23904 | 24131 | zir_ref: Zir.Inst.Ref, |
| 23905 | 24132 | ) CompileError!Module.Export.Options { |
| 23906 | const mod = sema.mod; | |
| 24133 | const pt = sema.pt; | |
| 24134 | const mod = pt.zcu; | |
| 23907 | 24135 | const gpa = sema.gpa; |
| 23908 | 24136 | const ip = &mod.intern_pool; |
| 23909 | const export_options_ty = try mod.getBuiltinType("ExportOptions"); | |
| 24137 | const export_options_ty = try pt.getBuiltinType("ExportOptions"); | |
| 23910 | 24138 | const air_ref = try sema.resolveInst(zir_ref); |
| 23911 | 24139 | const options = try sema.coerce(block, export_options_ty, air_ref, src); |
| 23912 | 24140 | |
| ... | ... | @@ -23969,12 +24197,12 @@ fn resolveBuiltinEnum( |
| 23969 | 24197 | comptime name: []const u8, |
| 23970 | 24198 | reason: NeededComptimeReason, |
| 23971 | 24199 | ) CompileError!@field(std.builtin, name) { |
| 23972 | const mod = sema.mod; | |
| 23973 | const ty = try mod.getBuiltinType(name); | |
| 24200 | const pt = sema.pt; | |
| 24201 | const ty = try pt.getBuiltinType(name); | |
| 23974 | 24202 | const air_ref = try sema.resolveInst(zir_ref); |
| 23975 | 24203 | const coerced = try sema.coerce(block, ty, air_ref, src); |
| 23976 | 24204 | const val = try sema.resolveConstDefinedValue(block, src, coerced, reason); |
| 23977 | return mod.toEnum(@field(std.builtin, name), val); | |
| 24205 | return pt.zcu.toEnum(@field(std.builtin, name), val); | |
| 23978 | 24206 | } |
| 23979 | 24207 | |
| 23980 | 24208 | fn resolveAtomicOrder( |
| ... | ... | @@ -24003,7 +24231,8 @@ fn zirCmpxchg( |
| 24003 | 24231 | block: *Block, |
| 24004 | 24232 | extended: Zir.Inst.Extended.InstData, |
| 24005 | 24233 | ) CompileError!Air.Inst.Ref { |
| 24006 | const mod = sema.mod; | |
| 24234 | const pt = sema.pt; | |
| 24235 | const mod = pt.zcu; | |
| 24007 | 24236 | const extra = sema.code.extraData(Zir.Inst.Cmpxchg, extended.operand).data; |
| 24008 | 24237 | const air_tag: Air.Inst.Tag = switch (extended.small) { |
| 24009 | 24238 | 0 => .cmpxchg_weak, |
| ... | ... | @@ -24026,7 +24255,7 @@ fn zirCmpxchg( |
| 24026 | 24255 | block, |
| 24027 | 24256 | elem_ty_src, |
| 24028 | 24257 | "expected bool, integer, enum, or pointer type; found '{}'", |
| 24029 | .{elem_ty.fmt(mod)}, | |
| 24258 | .{elem_ty.fmt(pt)}, | |
| 24030 | 24259 | ); |
| 24031 | 24260 | } |
| 24032 | 24261 | const uncasted_ptr = try sema.resolveInst(extra.ptr); |
| ... | ... | @@ -24052,11 +24281,11 @@ fn zirCmpxchg( |
| 24052 | 24281 | return sema.fail(block, failure_order_src, "failure atomic ordering must not be release or acq_rel", .{}); |
| 24053 | 24282 | } |
| 24054 | 24283 | |
| 24055 | const result_ty = try mod.optionalType(elem_ty.toIntern()); | |
| 24284 | const result_ty = try pt.optionalType(elem_ty.toIntern()); | |
| 24056 | 24285 | |
| 24057 | 24286 | // special case zero bit types |
| 24058 | 24287 | if ((try sema.typeHasOnePossibleValue(elem_ty)) != null) { |
| 24059 | return Air.internedToRef((try mod.intern(.{ .opt = .{ | |
| 24288 | return Air.internedToRef((try pt.intern(.{ .opt = .{ | |
| 24060 | 24289 | .ty = result_ty.toIntern(), |
| 24061 | 24290 | .val = .none, |
| 24062 | 24291 | } }))); |
| ... | ... | @@ -24068,11 +24297,11 @@ fn zirCmpxchg( |
| 24068 | 24297 | if (expected_val.isUndef(mod) or new_val.isUndef(mod)) { |
| 24069 | 24298 | // TODO: this should probably cause the memory stored at the pointer |
| 24070 | 24299 | // to become undef as well |
| 24071 | return mod.undefRef(result_ty); | |
| 24300 | return pt.undefRef(result_ty); | |
| 24072 | 24301 | } |
| 24073 | 24302 | const ptr_ty = sema.typeOf(ptr); |
| 24074 | 24303 | const stored_val = (try sema.pointerDeref(block, ptr_src, ptr_val, ptr_ty)) orelse break :rs ptr_src; |
| 24075 | const result_val = try mod.intern(.{ .opt = .{ | |
| 24304 | const result_val = try pt.intern(.{ .opt = .{ | |
| 24076 | 24305 | .ty = result_ty.toIntern(), |
| 24077 | 24306 | .val = if (stored_val.eql(expected_val, elem_ty, mod)) blk: { |
| 24078 | 24307 | try sema.storePtr(block, src, ptr, new_value); |
| ... | ... | @@ -24103,17 +24332,18 @@ fn zirCmpxchg( |
| 24103 | 24332 | } |
| 24104 | 24333 | |
| 24105 | 24334 | fn zirSplat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 24106 | const mod = sema.mod; | |
| 24335 | const pt = sema.pt; | |
| 24336 | const mod = pt.zcu; | |
| 24107 | 24337 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 24108 | 24338 | const extra = sema.code.extraData(Zir.Inst.Bin, inst_data.payload_index).data; |
| 24109 | 24339 | const src = block.nodeOffset(inst_data.src_node); |
| 24110 | 24340 | const scalar_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| 24111 | 24341 | const dest_ty = try sema.resolveDestType(block, src, extra.lhs, .remove_eu_opt, "@splat"); |
| 24112 | 24342 | |
| 24113 | if (!dest_ty.isVector(mod)) return sema.fail(block, src, "expected vector type, found '{}'", .{dest_ty.fmt(mod)}); | |
| 24343 | if (!dest_ty.isVector(mod)) return sema.fail(block, src, "expected vector type, found '{}'", .{dest_ty.fmt(pt)}); | |
| 24114 | 24344 | |
| 24115 | if (!dest_ty.hasRuntimeBits(mod)) { | |
| 24116 | const empty_aggregate = try mod.intern(.{ .aggregate = .{ | |
| 24345 | if (!dest_ty.hasRuntimeBits(pt)) { | |
| 24346 | const empty_aggregate = try pt.intern(.{ .aggregate = .{ | |
| 24117 | 24347 | .ty = dest_ty.toIntern(), |
| 24118 | 24348 | .storage = .{ .elems = &[_]InternPool.Index{} }, |
| 24119 | 24349 | } }); |
| ... | ... | @@ -24124,7 +24354,7 @@ fn zirSplat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 24124 | 24354 | const scalar_ty = dest_ty.childType(mod); |
| 24125 | 24355 | const scalar = try sema.coerce(block, scalar_ty, operand, scalar_src); |
| 24126 | 24356 | if (try sema.resolveValue(scalar)) |scalar_val| { |
| 24127 | if (scalar_val.isUndef(mod)) return mod.undefRef(dest_ty); | |
| 24357 | if (scalar_val.isUndef(mod)) return pt.undefRef(dest_ty); | |
| 24128 | 24358 | return Air.internedToRef((try sema.splat(dest_ty, scalar_val)).toIntern()); |
| 24129 | 24359 | } |
| 24130 | 24360 | |
| ... | ... | @@ -24142,10 +24372,11 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 24142 | 24372 | }); |
| 24143 | 24373 | const operand = try sema.resolveInst(extra.rhs); |
| 24144 | 24374 | const operand_ty = sema.typeOf(operand); |
| 24145 | const mod = sema.mod; | |
| 24375 | const pt = sema.pt; | |
| 24376 | const mod = pt.zcu; | |
| 24146 | 24377 | |
| 24147 | 24378 | if (operand_ty.zigTypeTag(mod) != .Vector) { |
| 24148 | return sema.fail(block, operand_src, "expected vector, found '{}'", .{operand_ty.fmt(mod)}); | |
| 24379 | return sema.fail(block, operand_src, "expected vector, found '{}'", .{operand_ty.fmt(pt)}); | |
| 24149 | 24380 | } |
| 24150 | 24381 | |
| 24151 | 24382 | const scalar_ty = operand_ty.childType(mod); |
| ... | ... | @@ -24155,13 +24386,13 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 24155 | 24386 | .And, .Or, .Xor => switch (scalar_ty.zigTypeTag(mod)) { |
| 24156 | 24387 | .Int, .Bool => {}, |
| 24157 | 24388 | else => return sema.fail(block, operand_src, "@reduce operation '{s}' requires integer or boolean operand; found '{}'", .{ |
| 24158 | @tagName(operation), operand_ty.fmt(mod), | |
| 24389 | @tagName(operation), operand_ty.fmt(pt), | |
| 24159 | 24390 | }), |
| 24160 | 24391 | }, |
| 24161 | 24392 | .Min, .Max, .Add, .Mul => switch (scalar_ty.zigTypeTag(mod)) { |
| 24162 | 24393 | .Int, .Float => {}, |
| 24163 | 24394 | else => return sema.fail(block, operand_src, "@reduce operation '{s}' requires integer or float operand; found '{}'", .{ |
| 24164 | @tagName(operation), operand_ty.fmt(mod), | |
| 24395 | @tagName(operation), operand_ty.fmt(pt), | |
| 24165 | 24396 | }), |
| 24166 | 24397 | }, |
| 24167 | 24398 | } |
| ... | ... | @@ -24174,20 +24405,20 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 24174 | 24405 | } |
| 24175 | 24406 | |
| 24176 | 24407 | if (try sema.resolveValue(operand)) |operand_val| { |
| 24177 | if (operand_val.isUndef(mod)) return mod.undefRef(scalar_ty); | |
| 24408 | if (operand_val.isUndef(mod)) return pt.undefRef(scalar_ty); | |
| 24178 | 24409 | |
| 24179 | var accum: Value = try operand_val.elemValue(mod, 0); | |
| 24410 | var accum: Value = try operand_val.elemValue(pt, 0); | |
| 24180 | 24411 | var i: u32 = 1; |
| 24181 | 24412 | while (i < vec_len) : (i += 1) { |
| 24182 | const elem_val = try operand_val.elemValue(mod, i); | |
| 24413 | const elem_val = try operand_val.elemValue(pt, i); | |
| 24183 | 24414 | switch (operation) { |
| 24184 | .And => accum = try accum.bitwiseAnd(elem_val, scalar_ty, sema.arena, mod), | |
| 24185 | .Or => accum = try accum.bitwiseOr(elem_val, scalar_ty, sema.arena, mod), | |
| 24186 | .Xor => accum = try accum.bitwiseXor(elem_val, scalar_ty, sema.arena, mod), | |
| 24187 | .Min => accum = accum.numberMin(elem_val, mod), | |
| 24188 | .Max => accum = accum.numberMax(elem_val, mod), | |
| 24415 | .And => accum = try accum.bitwiseAnd(elem_val, scalar_ty, sema.arena, pt), | |
| 24416 | .Or => accum = try accum.bitwiseOr(elem_val, scalar_ty, sema.arena, pt), | |
| 24417 | .Xor => accum = try accum.bitwiseXor(elem_val, scalar_ty, sema.arena, pt), | |
| 24418 | .Min => accum = accum.numberMin(elem_val, pt), | |
| 24419 | .Max => accum = accum.numberMax(elem_val, pt), | |
| 24189 | 24420 | .Add => accum = try sema.numberAddWrapScalar(accum, elem_val, scalar_ty), |
| 24190 | .Mul => accum = try accum.numberMulWrap(elem_val, scalar_ty, sema.arena, mod), | |
| 24421 | .Mul => accum = try accum.numberMulWrap(elem_val, scalar_ty, sema.arena, pt), | |
| 24191 | 24422 | } |
| 24192 | 24423 | } |
| 24193 | 24424 | return Air.internedToRef(accum.toIntern()); |
| ... | ... | @@ -24204,7 +24435,8 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 24204 | 24435 | } |
| 24205 | 24436 | |
| 24206 | 24437 | fn zirShuffle(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 24207 | const mod = sema.mod; | |
| 24438 | const pt = sema.pt; | |
| 24439 | const mod = pt.zcu; | |
| 24208 | 24440 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 24209 | 24441 | const extra = sema.code.extraData(Zir.Inst.Shuffle, inst_data.payload_index).data; |
| 24210 | 24442 | const elem_ty_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| ... | ... | @@ -24219,9 +24451,9 @@ fn zirShuffle(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 24219 | 24451 | |
| 24220 | 24452 | const mask_len = switch (sema.typeOf(mask).zigTypeTag(mod)) { |
| 24221 | 24453 | .Array, .Vector => sema.typeOf(mask).arrayLen(mod), |
| 24222 | else => return sema.fail(block, mask_src, "expected vector or array, found '{}'", .{sema.typeOf(mask).fmt(sema.mod)}), | |
| 24454 | else => return sema.fail(block, mask_src, "expected vector or array, found '{}'", .{sema.typeOf(mask).fmt(pt)}), | |
| 24223 | 24455 | }; |
| 24224 | mask_ty = try mod.vectorType(.{ | |
| 24456 | mask_ty = try pt.vectorType(.{ | |
| 24225 | 24457 | .len = @intCast(mask_len), |
| 24226 | 24458 | .child = .i32_type, |
| 24227 | 24459 | }); |
| ... | ... | @@ -24242,51 +24474,51 @@ fn analyzeShuffle( |
| 24242 | 24474 | mask: Value, |
| 24243 | 24475 | mask_len: u32, |
| 24244 | 24476 | ) CompileError!Air.Inst.Ref { |
| 24245 | const mod = sema.mod; | |
| 24477 | const pt = sema.pt; | |
| 24246 | 24478 | const a_src = block.builtinCallArgSrc(src_node, 1); |
| 24247 | 24479 | const b_src = block.builtinCallArgSrc(src_node, 2); |
| 24248 | 24480 | const mask_src = block.builtinCallArgSrc(src_node, 3); |
| 24249 | 24481 | var a = a_arg; |
| 24250 | 24482 | var b = b_arg; |
| 24251 | 24483 | |
| 24252 | const res_ty = try mod.vectorType(.{ | |
| 24484 | const res_ty = try pt.vectorType(.{ | |
| 24253 | 24485 | .len = mask_len, |
| 24254 | 24486 | .child = elem_ty.toIntern(), |
| 24255 | 24487 | }); |
| 24256 | 24488 | |
| 24257 | const maybe_a_len = switch (sema.typeOf(a).zigTypeTag(mod)) { | |
| 24258 | .Array, .Vector => sema.typeOf(a).arrayLen(mod), | |
| 24489 | const maybe_a_len = switch (sema.typeOf(a).zigTypeTag(pt.zcu)) { | |
| 24490 | .Array, .Vector => sema.typeOf(a).arrayLen(pt.zcu), | |
| 24259 | 24491 | .Undefined => null, |
| 24260 | 24492 | else => return sema.fail(block, a_src, "expected vector or array with element type '{}', found '{}'", .{ |
| 24261 | elem_ty.fmt(sema.mod), | |
| 24262 | sema.typeOf(a).fmt(sema.mod), | |
| 24493 | elem_ty.fmt(pt), | |
| 24494 | sema.typeOf(a).fmt(pt), | |
| 24263 | 24495 | }), |
| 24264 | 24496 | }; |
| 24265 | const maybe_b_len = switch (sema.typeOf(b).zigTypeTag(mod)) { | |
| 24266 | .Array, .Vector => sema.typeOf(b).arrayLen(mod), | |
| 24497 | const maybe_b_len = switch (sema.typeOf(b).zigTypeTag(pt.zcu)) { | |
| 24498 | .Array, .Vector => sema.typeOf(b).arrayLen(pt.zcu), | |
| 24267 | 24499 | .Undefined => null, |
| 24268 | 24500 | else => return sema.fail(block, b_src, "expected vector or array with element type '{}', found '{}'", .{ |
| 24269 | elem_ty.fmt(sema.mod), | |
| 24270 | sema.typeOf(b).fmt(sema.mod), | |
| 24501 | elem_ty.fmt(pt), | |
| 24502 | sema.typeOf(b).fmt(pt), | |
| 24271 | 24503 | }), |
| 24272 | 24504 | }; |
| 24273 | 24505 | if (maybe_a_len == null and maybe_b_len == null) { |
| 24274 | return mod.undefRef(res_ty); | |
| 24506 | return pt.undefRef(res_ty); | |
| 24275 | 24507 | } |
| 24276 | 24508 | const a_len: u32 = @intCast(maybe_a_len orelse maybe_b_len.?); |
| 24277 | 24509 | const b_len: u32 = @intCast(maybe_b_len orelse a_len); |
| 24278 | 24510 | |
| 24279 | const a_ty = try mod.vectorType(.{ | |
| 24511 | const a_ty = try pt.vectorType(.{ | |
| 24280 | 24512 | .len = a_len, |
| 24281 | 24513 | .child = elem_ty.toIntern(), |
| 24282 | 24514 | }); |
| 24283 | const b_ty = try mod.vectorType(.{ | |
| 24515 | const b_ty = try pt.vectorType(.{ | |
| 24284 | 24516 | .len = b_len, |
| 24285 | 24517 | .child = elem_ty.toIntern(), |
| 24286 | 24518 | }); |
| 24287 | 24519 | |
| 24288 | if (maybe_a_len == null) a = try mod.undefRef(a_ty) else a = try sema.coerce(block, a_ty, a, a_src); | |
| 24289 | if (maybe_b_len == null) b = try mod.undefRef(b_ty) else b = try sema.coerce(block, b_ty, b, b_src); | |
| 24520 | if (maybe_a_len == null) a = try pt.undefRef(a_ty) else a = try sema.coerce(block, a_ty, a, a_src); | |
| 24521 | if (maybe_b_len == null) b = try pt.undefRef(b_ty) else b = try sema.coerce(block, b_ty, b, b_src); | |
| 24290 | 24522 | |
| 24291 | 24523 | const operand_info = [2]std.meta.Tuple(&.{ u64, LazySrcLoc, Type }){ |
| 24292 | 24524 | .{ a_len, a_src, a_ty }, |
| ... | ... | @@ -24294,10 +24526,10 @@ fn analyzeShuffle( |
| 24294 | 24526 | }; |
| 24295 | 24527 | |
| 24296 | 24528 | for (0..@intCast(mask_len)) |i| { |
| 24297 | const elem = try mask.elemValue(sema.mod, i); | |
| 24298 | if (elem.isUndef(mod)) continue; | |
| 24529 | const elem = try mask.elemValue(pt, i); | |
| 24530 | if (elem.isUndef(pt.zcu)) continue; | |
| 24299 | 24531 | const elem_resolved = try sema.resolveLazyValue(elem); |
| 24300 | const int = elem_resolved.toSignedInt(mod); | |
| 24532 | const int = elem_resolved.toSignedInt(pt); | |
| 24301 | 24533 | var unsigned: u32 = undefined; |
| 24302 | 24534 | var chosen: u32 = undefined; |
| 24303 | 24535 | if (int >= 0) { |
| ... | ... | @@ -24314,7 +24546,7 @@ fn analyzeShuffle( |
| 24314 | 24546 | |
| 24315 | 24547 | try sema.errNote(operand_info[chosen][1], msg, "selected index '{d}' out of bounds of '{}'", .{ |
| 24316 | 24548 | unsigned, |
| 24317 | operand_info[chosen][2].fmt(sema.mod), | |
| 24549 | operand_info[chosen][2].fmt(pt), | |
| 24318 | 24550 | }); |
| 24319 | 24551 | |
| 24320 | 24552 | if (chosen == 0) { |
| ... | ... | @@ -24331,16 +24563,16 @@ fn analyzeShuffle( |
| 24331 | 24563 | if (try sema.resolveValue(b)) |b_val| { |
| 24332 | 24564 | const values = try sema.arena.alloc(InternPool.Index, mask_len); |
| 24333 | 24565 | for (values, 0..) |*value, i| { |
| 24334 | const mask_elem_val = try mask.elemValue(sema.mod, i); | |
| 24335 | if (mask_elem_val.isUndef(mod)) { | |
| 24336 | value.* = try mod.intern(.{ .undef = elem_ty.toIntern() }); | |
| 24566 | const mask_elem_val = try mask.elemValue(pt, i); | |
| 24567 | if (mask_elem_val.isUndef(pt.zcu)) { | |
| 24568 | value.* = try pt.intern(.{ .undef = elem_ty.toIntern() }); | |
| 24337 | 24569 | continue; |
| 24338 | 24570 | } |
| 24339 | const int = mask_elem_val.toSignedInt(mod); | |
| 24571 | const int = mask_elem_val.toSignedInt(pt); | |
| 24340 | 24572 | const unsigned: u32 = @intCast(if (int >= 0) int else ~int); |
| 24341 | values[i] = (try (if (int >= 0) a_val else b_val).elemValue(mod, unsigned)).toIntern(); | |
| 24573 | values[i] = (try (if (int >= 0) a_val else b_val).elemValue(pt, unsigned)).toIntern(); | |
| 24342 | 24574 | } |
| 24343 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 24575 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 24344 | 24576 | .ty = res_ty.toIntern(), |
| 24345 | 24577 | .storage = .{ .elems = values }, |
| 24346 | 24578 | } }))); |
| ... | ... | @@ -24359,21 +24591,21 @@ fn analyzeShuffle( |
| 24359 | 24591 | |
| 24360 | 24592 | const expand_mask_values = try sema.arena.alloc(InternPool.Index, max_len); |
| 24361 | 24593 | for (@intCast(0)..@intCast(min_len)) |i| { |
| 24362 | expand_mask_values[i] = (try mod.intValue(Type.comptime_int, i)).toIntern(); | |
| 24594 | expand_mask_values[i] = (try pt.intValue(Type.comptime_int, i)).toIntern(); | |
| 24363 | 24595 | } |
| 24364 | 24596 | for (@intCast(min_len)..@intCast(max_len)) |i| { |
| 24365 | expand_mask_values[i] = (try mod.intValue(Type.comptime_int, -1)).toIntern(); | |
| 24597 | expand_mask_values[i] = (try pt.intValue(Type.comptime_int, -1)).toIntern(); | |
| 24366 | 24598 | } |
| 24367 | const expand_mask = try mod.intern(.{ .aggregate = .{ | |
| 24368 | .ty = (try mod.vectorType(.{ .len = @intCast(max_len), .child = .comptime_int_type })).toIntern(), | |
| 24599 | const expand_mask = try pt.intern(.{ .aggregate = .{ | |
| 24600 | .ty = (try pt.vectorType(.{ .len = @intCast(max_len), .child = .comptime_int_type })).toIntern(), | |
| 24369 | 24601 | .storage = .{ .elems = expand_mask_values }, |
| 24370 | 24602 | } }); |
| 24371 | 24603 | |
| 24372 | 24604 | if (a_len < b_len) { |
| 24373 | const undef = try mod.undefRef(a_ty); | |
| 24605 | const undef = try pt.undefRef(a_ty); | |
| 24374 | 24606 | a = try sema.analyzeShuffle(block, src_node, elem_ty, a, undef, Value.fromInterned(expand_mask), @intCast(max_len)); |
| 24375 | 24607 | } else { |
| 24376 | const undef = try mod.undefRef(b_ty); | |
| 24608 | const undef = try pt.undefRef(b_ty); | |
| 24377 | 24609 | b = try sema.analyzeShuffle(block, src_node, elem_ty, b, undef, Value.fromInterned(expand_mask), @intCast(max_len)); |
| 24378 | 24610 | } |
| 24379 | 24611 | } |
| ... | ... | @@ -24393,7 +24625,8 @@ fn analyzeShuffle( |
| 24393 | 24625 | } |
| 24394 | 24626 | |
| 24395 | 24627 | fn zirSelect(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { |
| 24396 | const mod = sema.mod; | |
| 24628 | const pt = sema.pt; | |
| 24629 | const mod = pt.zcu; | |
| 24397 | 24630 | const extra = sema.code.extraData(Zir.Inst.Select, extended.operand).data; |
| 24398 | 24631 | |
| 24399 | 24632 | const src = block.nodeOffset(extra.node); |
| ... | ... | @@ -24409,17 +24642,17 @@ fn zirSelect(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) C |
| 24409 | 24642 | |
| 24410 | 24643 | const vec_len_u64 = switch (try pred_ty.zigTypeTagOrPoison(mod)) { |
| 24411 | 24644 | .Vector, .Array => pred_ty.arrayLen(mod), |
| 24412 | else => return sema.fail(block, pred_src, "expected vector or array, found '{}'", .{pred_ty.fmt(mod)}), | |
| 24645 | else => return sema.fail(block, pred_src, "expected vector or array, found '{}'", .{pred_ty.fmt(pt)}), | |
| 24413 | 24646 | }; |
| 24414 | 24647 | const vec_len: u32 = @intCast(try sema.usizeCast(block, pred_src, vec_len_u64)); |
| 24415 | 24648 | |
| 24416 | const bool_vec_ty = try mod.vectorType(.{ | |
| 24649 | const bool_vec_ty = try pt.vectorType(.{ | |
| 24417 | 24650 | .len = vec_len, |
| 24418 | 24651 | .child = .bool_type, |
| 24419 | 24652 | }); |
| 24420 | 24653 | const pred = try sema.coerce(block, bool_vec_ty, pred_uncoerced, pred_src); |
| 24421 | 24654 | |
| 24422 | const vec_ty = try mod.vectorType(.{ | |
| 24655 | const vec_ty = try pt.vectorType(.{ | |
| 24423 | 24656 | .len = vec_len, |
| 24424 | 24657 | .child = elem_ty.toIntern(), |
| 24425 | 24658 | }); |
| ... | ... | @@ -24431,23 +24664,23 @@ fn zirSelect(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) C |
| 24431 | 24664 | const maybe_b = try sema.resolveValue(b); |
| 24432 | 24665 | |
| 24433 | 24666 | const runtime_src = if (maybe_pred) |pred_val| rs: { |
| 24434 | if (pred_val.isUndef(mod)) return mod.undefRef(vec_ty); | |
| 24667 | if (pred_val.isUndef(mod)) return pt.undefRef(vec_ty); | |
| 24435 | 24668 | |
| 24436 | 24669 | if (maybe_a) |a_val| { |
| 24437 | if (a_val.isUndef(mod)) return mod.undefRef(vec_ty); | |
| 24670 | if (a_val.isUndef(mod)) return pt.undefRef(vec_ty); | |
| 24438 | 24671 | |
| 24439 | 24672 | if (maybe_b) |b_val| { |
| 24440 | if (b_val.isUndef(mod)) return mod.undefRef(vec_ty); | |
| 24673 | if (b_val.isUndef(mod)) return pt.undefRef(vec_ty); | |
| 24441 | 24674 | |
| 24442 | 24675 | const elems = try sema.gpa.alloc(InternPool.Index, vec_len); |
| 24443 | 24676 | defer sema.gpa.free(elems); |
| 24444 | 24677 | for (elems, 0..) |*elem, i| { |
| 24445 | const pred_elem_val = try pred_val.elemValue(mod, i); | |
| 24678 | const pred_elem_val = try pred_val.elemValue(pt, i); | |
| 24446 | 24679 | const should_choose_a = pred_elem_val.toBool(); |
| 24447 | elem.* = (try (if (should_choose_a) a_val else b_val).elemValue(mod, i)).toIntern(); | |
| 24680 | elem.* = (try (if (should_choose_a) a_val else b_val).elemValue(pt, i)).toIntern(); | |
| 24448 | 24681 | } |
| 24449 | 24682 | |
| 24450 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 24683 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 24451 | 24684 | .ty = vec_ty.toIntern(), |
| 24452 | 24685 | .storage = .{ .elems = elems }, |
| 24453 | 24686 | } }))); |
| ... | ... | @@ -24456,16 +24689,16 @@ fn zirSelect(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) C |
| 24456 | 24689 | } |
| 24457 | 24690 | } else { |
| 24458 | 24691 | if (maybe_b) |b_val| { |
| 24459 | if (b_val.isUndef(mod)) return mod.undefRef(vec_ty); | |
| 24692 | if (b_val.isUndef(mod)) return pt.undefRef(vec_ty); | |
| 24460 | 24693 | } |
| 24461 | 24694 | break :rs a_src; |
| 24462 | 24695 | } |
| 24463 | 24696 | } else rs: { |
| 24464 | 24697 | if (maybe_a) |a_val| { |
| 24465 | if (a_val.isUndef(mod)) return mod.undefRef(vec_ty); | |
| 24698 | if (a_val.isUndef(mod)) return pt.undefRef(vec_ty); | |
| 24466 | 24699 | } |
| 24467 | 24700 | if (maybe_b) |b_val| { |
| 24468 | if (b_val.isUndef(mod)) return mod.undefRef(vec_ty); | |
| 24701 | if (b_val.isUndef(mod)) return pt.undefRef(vec_ty); | |
| 24469 | 24702 | } |
| 24470 | 24703 | break :rs pred_src; |
| 24471 | 24704 | }; |
| ... | ... | @@ -24531,7 +24764,8 @@ fn zirAtomicLoad(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 24531 | 24764 | } |
| 24532 | 24765 | |
| 24533 | 24766 | fn zirAtomicRmw(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| 24534 | const mod = sema.mod; | |
| 24767 | const pt = sema.pt; | |
| 24768 | const mod = pt.zcu; | |
| 24535 | 24769 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 24536 | 24770 | const extra = sema.code.extraData(Zir.Inst.AtomicRmw, inst_data.payload_index).data; |
| 24537 | 24771 | const src = block.nodeOffset(inst_data.src_node); |
| ... | ... | @@ -24588,12 +24822,12 @@ fn zirAtomicRmw(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 24588 | 24822 | .Xchg => operand_val, |
| 24589 | 24823 | .Add => try sema.numberAddWrapScalar(stored_val, operand_val, elem_ty), |
| 24590 | 24824 | .Sub => try sema.numberSubWrapScalar(stored_val, operand_val, elem_ty), |
| 24591 | .And => try stored_val.bitwiseAnd (operand_val, elem_ty, sema.arena, mod), | |
| 24592 | .Nand => try stored_val.bitwiseNand (operand_val, elem_ty, sema.arena, mod), | |
| 24593 | .Or => try stored_val.bitwiseOr (operand_val, elem_ty, sema.arena, mod), | |
| 24594 | .Xor => try stored_val.bitwiseXor (operand_val, elem_ty, sema.arena, mod), | |
| 24595 | .Max => stored_val.numberMax (operand_val, mod), | |
| 24596 | .Min => stored_val.numberMin (operand_val, mod), | |
| 24825 | .And => try stored_val.bitwiseAnd (operand_val, elem_ty, sema.arena, pt), | |
| 24826 | .Nand => try stored_val.bitwiseNand (operand_val, elem_ty, sema.arena, pt), | |
| 24827 | .Or => try stored_val.bitwiseOr (operand_val, elem_ty, sema.arena, pt), | |
| 24828 | .Xor => try stored_val.bitwiseXor (operand_val, elem_ty, sema.arena, pt), | |
| 24829 | .Max => stored_val.numberMax (operand_val, pt), | |
| 24830 | .Min => stored_val.numberMin (operand_val, pt), | |
| 24597 | 24831 | // zig fmt: on |
| 24598 | 24832 | }; |
| 24599 | 24833 | try sema.storePtrVal(block, src, ptr_val, new_val, elem_ty); |
| ... | ... | @@ -24669,36 +24903,37 @@ fn zirMulAdd(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 24669 | 24903 | const maybe_mulend1 = try sema.resolveValue(mulend1); |
| 24670 | 24904 | const maybe_mulend2 = try sema.resolveValue(mulend2); |
| 24671 | 24905 | const maybe_addend = try sema.resolveValue(addend); |
| 24672 | const mod = sema.mod; | |
| 24906 | const pt = sema.pt; | |
| 24907 | const mod = pt.zcu; | |
| 24673 | 24908 | |
| 24674 | 24909 | switch (ty.scalarType(mod).zigTypeTag(mod)) { |
| 24675 | 24910 | .ComptimeFloat, .Float => {}, |
| 24676 | else => return sema.fail(block, src, "expected vector of floats or float type, found '{}'", .{ty.fmt(sema.mod)}), | |
| 24911 | else => return sema.fail(block, src, "expected vector of floats or float type, found '{}'", .{ty.fmt(pt)}), | |
| 24677 | 24912 | } |
| 24678 | 24913 | |
| 24679 | 24914 | const runtime_src = if (maybe_mulend1) |mulend1_val| rs: { |
| 24680 | 24915 | if (maybe_mulend2) |mulend2_val| { |
| 24681 | if (mulend2_val.isUndef(mod)) return mod.undefRef(ty); | |
| 24916 | if (mulend2_val.isUndef(mod)) return pt.undefRef(ty); | |
| 24682 | 24917 | |
| 24683 | 24918 | if (maybe_addend) |addend_val| { |
| 24684 | if (addend_val.isUndef(mod)) return mod.undefRef(ty); | |
| 24685 | const result_val = try Value.mulAdd(ty, mulend1_val, mulend2_val, addend_val, sema.arena, sema.mod); | |
| 24919 | if (addend_val.isUndef(mod)) return pt.undefRef(ty); | |
| 24920 | const result_val = try Value.mulAdd(ty, mulend1_val, mulend2_val, addend_val, sema.arena, pt); | |
| 24686 | 24921 | return Air.internedToRef(result_val.toIntern()); |
| 24687 | 24922 | } else { |
| 24688 | 24923 | break :rs addend_src; |
| 24689 | 24924 | } |
| 24690 | 24925 | } else { |
| 24691 | 24926 | if (maybe_addend) |addend_val| { |
| 24692 | if (addend_val.isUndef(mod)) return mod.undefRef(ty); | |
| 24927 | if (addend_val.isUndef(mod)) return pt.undefRef(ty); | |
| 24693 | 24928 | } |
| 24694 | 24929 | break :rs mulend2_src; |
| 24695 | 24930 | } |
| 24696 | 24931 | } else rs: { |
| 24697 | 24932 | if (maybe_mulend2) |mulend2_val| { |
| 24698 | if (mulend2_val.isUndef(mod)) return mod.undefRef(ty); | |
| 24933 | if (mulend2_val.isUndef(mod)) return pt.undefRef(ty); | |
| 24699 | 24934 | } |
| 24700 | 24935 | if (maybe_addend) |addend_val| { |
| 24701 | if (addend_val.isUndef(mod)) return mod.undefRef(ty); | |
| 24936 | if (addend_val.isUndef(mod)) return pt.undefRef(ty); | |
| 24702 | 24937 | } |
| 24703 | 24938 | break :rs mulend1_src; |
| 24704 | 24939 | }; |
| ... | ... | @@ -24720,7 +24955,8 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 24720 | 24955 | const tracy = trace(@src()); |
| 24721 | 24956 | defer tracy.end(); |
| 24722 | 24957 | |
| 24723 | const mod = sema.mod; | |
| 24958 | const pt = sema.pt; | |
| 24959 | const mod = pt.zcu; | |
| 24724 | 24960 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 24725 | 24961 | const modifier_src = block.builtinCallArgSrc(inst_data.src_node, 0); |
| 24726 | 24962 | const func_src = block.builtinCallArgSrc(inst_data.src_node, 1); |
| ... | ... | @@ -24730,7 +24966,7 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 24730 | 24966 | const extra = sema.code.extraData(Zir.Inst.BuiltinCall, inst_data.payload_index).data; |
| 24731 | 24967 | const func = try sema.resolveInst(extra.callee); |
| 24732 | 24968 | |
| 24733 | const modifier_ty = try mod.getBuiltinType("CallModifier"); | |
| 24969 | const modifier_ty = try pt.getBuiltinType("CallModifier"); | |
| 24734 | 24970 | const air_ref = try sema.resolveInst(extra.modifier); |
| 24735 | 24971 | const modifier_ref = try sema.coerce(block, modifier_ty, air_ref, modifier_src); |
| 24736 | 24972 | const modifier_val = try sema.resolveConstDefinedValue(block, modifier_src, modifier_ref, .{ |
| ... | ... | @@ -24783,7 +25019,7 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 24783 | 25019 | |
| 24784 | 25020 | const args_ty = sema.typeOf(args); |
| 24785 | 25021 | if (!args_ty.isTuple(mod) and args_ty.toIntern() != .empty_struct_type) { |
| 24786 | return sema.fail(block, args_src, "expected a tuple, found '{}'", .{args_ty.fmt(sema.mod)}); | |
| 25022 | return sema.fail(block, args_src, "expected a tuple, found '{}'", .{args_ty.fmt(pt)}); | |
| 24787 | 25023 | } |
| 24788 | 25024 | |
| 24789 | 25025 | const resolved_args: []Air.Inst.Ref = try sema.arena.alloc(Air.Inst.Ref, args_ty.structFieldCount(mod)); |
| ... | ... | @@ -24812,7 +25048,8 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 24812 | 25048 | } |
| 24813 | 25049 | |
| 24814 | 25050 | fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!Air.Inst.Ref { |
| 24815 | const zcu = sema.mod; | |
| 25051 | const pt = sema.pt; | |
| 25052 | const zcu = pt.zcu; | |
| 24816 | 25053 | const ip = &zcu.intern_pool; |
| 24817 | 25054 | |
| 24818 | 25055 | const extra = sema.code.extraData(Zir.Inst.FieldParentPtr, extended.operand).data; |
| ... | ... | @@ -24827,14 +25064,14 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins |
| 24827 | 25064 | try sema.checkPtrType(block, inst_src, parent_ptr_ty, true); |
| 24828 | 25065 | const parent_ptr_info = parent_ptr_ty.ptrInfo(zcu); |
| 24829 | 25066 | if (parent_ptr_info.flags.size != .One) { |
| 24830 | return sema.fail(block, inst_src, "expected single pointer type, found '{}'", .{parent_ptr_ty.fmt(zcu)}); | |
| 25067 | return sema.fail(block, inst_src, "expected single pointer type, found '{}'", .{parent_ptr_ty.fmt(pt)}); | |
| 24831 | 25068 | } |
| 24832 | 25069 | const parent_ty = Type.fromInterned(parent_ptr_info.child); |
| 24833 | 25070 | switch (parent_ty.zigTypeTag(zcu)) { |
| 24834 | 25071 | .Struct, .Union => {}, |
| 24835 | else => return sema.fail(block, inst_src, "expected pointer to struct or union type, found '{}'", .{parent_ptr_ty.fmt(zcu)}), | |
| 25072 | else => return sema.fail(block, inst_src, "expected pointer to struct or union type, found '{}'", .{parent_ptr_ty.fmt(pt)}), | |
| 24836 | 25073 | } |
| 24837 | try parent_ty.resolveLayout(zcu); | |
| 25074 | try parent_ty.resolveLayout(pt); | |
| 24838 | 25075 | |
| 24839 | 25076 | const field_name = try sema.resolveConstStringIntern(block, field_name_src, extra.field_name, .{ |
| 24840 | 25077 | .needed_comptime_reason = "field name must be comptime-known", |
| ... | ... | @@ -24865,7 +25102,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins |
| 24865 | 25102 | var actual_parent_ptr_info: InternPool.Key.PtrType = .{ |
| 24866 | 25103 | .child = parent_ty.toIntern(), |
| 24867 | 25104 | .flags = .{ |
| 24868 | .alignment = try parent_ptr_ty.ptrAlignmentAdvanced(zcu, .sema), | |
| 25105 | .alignment = try parent_ptr_ty.ptrAlignmentAdvanced(pt, .sema), | |
| 24869 | 25106 | .is_const = field_ptr_info.flags.is_const, |
| 24870 | 25107 | .is_volatile = field_ptr_info.flags.is_volatile, |
| 24871 | 25108 | .is_allowzero = field_ptr_info.flags.is_allowzero, |
| ... | ... | @@ -24877,7 +25114,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins |
| 24877 | 25114 | var actual_field_ptr_info: InternPool.Key.PtrType = .{ |
| 24878 | 25115 | .child = field_ty.toIntern(), |
| 24879 | 25116 | .flags = .{ |
| 24880 | .alignment = try field_ptr_ty.ptrAlignmentAdvanced(zcu, .sema), | |
| 25117 | .alignment = try field_ptr_ty.ptrAlignmentAdvanced(pt, .sema), | |
| 24881 | 25118 | .is_const = field_ptr_info.flags.is_const, |
| 24882 | 25119 | .is_volatile = field_ptr_info.flags.is_volatile, |
| 24883 | 25120 | .is_allowzero = field_ptr_info.flags.is_allowzero, |
| ... | ... | @@ -24888,13 +25125,13 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins |
| 24888 | 25125 | switch (parent_ty.containerLayout(zcu)) { |
| 24889 | 25126 | .auto => { |
| 24890 | 25127 | actual_parent_ptr_info.flags.alignment = actual_field_ptr_info.flags.alignment.minStrict( |
| 24891 | if (zcu.typeToStruct(parent_ty)) |struct_obj| try zcu.structFieldAlignmentAdvanced( | |
| 25128 | if (zcu.typeToStruct(parent_ty)) |struct_obj| try pt.structFieldAlignmentAdvanced( | |
| 24892 | 25129 | struct_obj.fieldAlign(ip, field_index), |
| 24893 | 25130 | field_ty, |
| 24894 | 25131 | struct_obj.layout, |
| 24895 | 25132 | .sema, |
| 24896 | 25133 | ) else if (zcu.typeToUnion(parent_ty)) |union_obj| |
| 24897 | try zcu.unionFieldNormalAlignmentAdvanced(union_obj, field_index, .sema) | |
| 25134 | try pt.unionFieldNormalAlignmentAdvanced(union_obj, field_index, .sema) | |
| 24898 | 25135 | else |
| 24899 | 25136 | actual_field_ptr_info.flags.alignment, |
| 24900 | 25137 | ); |
| ... | ... | @@ -24903,7 +25140,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins |
| 24903 | 25140 | actual_field_ptr_info.packed_offset = .{ .bit_offset = 0, .host_size = 0 }; |
| 24904 | 25141 | }, |
| 24905 | 25142 | .@"extern" => { |
| 24906 | const field_offset = parent_ty.structFieldOffset(field_index, zcu); | |
| 25143 | const field_offset = parent_ty.structFieldOffset(field_index, pt); | |
| 24907 | 25144 | actual_parent_ptr_info.flags.alignment = actual_field_ptr_info.flags.alignment.minStrict(if (field_offset > 0) |
| 24908 | 25145 | Alignment.fromLog2Units(@ctz(field_offset)) |
| 24909 | 25146 | else |
| ... | ... | @@ -24914,7 +25151,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins |
| 24914 | 25151 | }, |
| 24915 | 25152 | .@"packed" => { |
| 24916 | 25153 | const byte_offset = std.math.divExact(u32, @abs(@as(i32, actual_parent_ptr_info.packed_offset.bit_offset) + |
| 24917 | (if (zcu.typeToStruct(parent_ty)) |struct_obj| zcu.structPackedFieldBitOffset(struct_obj, field_index) else 0) - | |
| 25154 | (if (zcu.typeToStruct(parent_ty)) |struct_obj| pt.structPackedFieldBitOffset(struct_obj, field_index) else 0) - | |
| 24918 | 25155 | actual_field_ptr_info.packed_offset.bit_offset), 8) catch |
| 24919 | 25156 | return sema.fail(block, inst_src, "pointer bit-offset mismatch", .{}); |
| 24920 | 25157 | actual_parent_ptr_info.flags.alignment = actual_field_ptr_info.flags.alignment.minStrict(if (byte_offset > 0) |
| ... | ... | @@ -24924,16 +25161,16 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins |
| 24924 | 25161 | }, |
| 24925 | 25162 | } |
| 24926 | 25163 | |
| 24927 | const actual_field_ptr_ty = try zcu.ptrTypeSema(actual_field_ptr_info); | |
| 25164 | const actual_field_ptr_ty = try pt.ptrTypeSema(actual_field_ptr_info); | |
| 24928 | 25165 | const casted_field_ptr = try sema.coerce(block, actual_field_ptr_ty, field_ptr, field_ptr_src); |
| 24929 | const actual_parent_ptr_ty = try zcu.ptrTypeSema(actual_parent_ptr_info); | |
| 25166 | const actual_parent_ptr_ty = try pt.ptrTypeSema(actual_parent_ptr_info); | |
| 24930 | 25167 | |
| 24931 | 25168 | const result = if (try sema.resolveDefinedValue(block, field_ptr_src, casted_field_ptr)) |field_ptr_val| result: { |
| 24932 | 25169 | switch (parent_ty.zigTypeTag(zcu)) { |
| 24933 | 25170 | .Struct => switch (parent_ty.containerLayout(zcu)) { |
| 24934 | 25171 | .auto => {}, |
| 24935 | 25172 | .@"extern" => { |
| 24936 | const byte_offset = parent_ty.structFieldOffset(field_index, zcu); | |
| 25173 | const byte_offset = parent_ty.structFieldOffset(field_index, pt); | |
| 24937 | 25174 | const parent_ptr_val = try sema.ptrSubtract(block, field_ptr_src, field_ptr_val, byte_offset, actual_parent_ptr_ty); |
| 24938 | 25175 | break :result Air.internedToRef(parent_ptr_val.toIntern()); |
| 24939 | 25176 | }, |
| ... | ... | @@ -24941,7 +25178,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins |
| 24941 | 25178 | // Logic lifted from type computation above - I'm just assuming it's correct. |
| 24942 | 25179 | // `catch unreachable` since error case handled above. |
| 24943 | 25180 | const byte_offset = std.math.divExact(u32, @abs(@as(i32, actual_parent_ptr_info.packed_offset.bit_offset) + |
| 24944 | zcu.structPackedFieldBitOffset(zcu.typeToStruct(parent_ty).?, field_index) - | |
| 25181 | pt.structPackedFieldBitOffset(zcu.typeToStruct(parent_ty).?, field_index) - | |
| 24945 | 25182 | actual_field_ptr_info.packed_offset.bit_offset), 8) catch unreachable; |
| 24946 | 25183 | const parent_ptr_val = try sema.ptrSubtract(block, field_ptr_src, field_ptr_val, byte_offset, actual_parent_ptr_ty); |
| 24947 | 25184 | break :result Air.internedToRef(parent_ptr_val.toIntern()); |
| ... | ... | @@ -24951,7 +25188,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins |
| 24951 | 25188 | .auto => {}, |
| 24952 | 25189 | .@"extern", .@"packed" => { |
| 24953 | 25190 | // For an extern or packed union, just coerce the pointer. |
| 24954 | const parent_ptr_val = try zcu.getCoerced(field_ptr_val, actual_parent_ptr_ty); | |
| 25191 | const parent_ptr_val = try pt.getCoerced(field_ptr_val, actual_parent_ptr_ty); | |
| 24955 | 25192 | break :result Air.internedToRef(parent_ptr_val.toIntern()); |
| 24956 | 25193 | }, |
| 24957 | 25194 | }, |
| ... | ... | @@ -24980,7 +25217,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins |
| 24980 | 25217 | |
| 24981 | 25218 | if (field.index != field_index) { |
| 24982 | 25219 | return sema.fail(block, inst_src, "field '{}' has index '{d}' but pointer value is index '{d}' of struct '{}'", .{ |
| 24983 | field_name.fmt(ip), field_index, field.index, parent_ty.fmt(zcu), | |
| 25220 | field_name.fmt(ip), field_index, field.index, parent_ty.fmt(pt), | |
| 24984 | 25221 | }); |
| 24985 | 25222 | } |
| 24986 | 25223 | break :result try sema.coerce(block, actual_parent_ptr_ty, Air.internedToRef(field.base), inst_src); |
| ... | ... | @@ -25001,8 +25238,9 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.Ins |
| 25001 | 25238 | } |
| 25002 | 25239 | |
| 25003 | 25240 | fn ptrSubtract(sema: *Sema, block: *Block, src: LazySrcLoc, ptr_val: Value, byte_subtract: u64, new_ty: Type) !Value { |
| 25004 | const zcu = sema.mod; | |
| 25005 | if (byte_subtract == 0) return zcu.getCoerced(ptr_val, new_ty); | |
| 25241 | const pt = sema.pt; | |
| 25242 | const zcu = pt.zcu; | |
| 25243 | if (byte_subtract == 0) return pt.getCoerced(ptr_val, new_ty); | |
| 25006 | 25244 | var ptr = switch (zcu.intern_pool.indexToKey(ptr_val.toIntern())) { |
| 25007 | 25245 | .undef => return sema.failWithUseOfUndef(block, src), |
| 25008 | 25246 | .ptr => |ptr| ptr, |
| ... | ... | @@ -25018,7 +25256,7 @@ fn ptrSubtract(sema: *Sema, block: *Block, src: LazySrcLoc, ptr_val: Value, byte |
| 25018 | 25256 | } |
| 25019 | 25257 | ptr.byte_offset -= byte_subtract; |
| 25020 | 25258 | ptr.ty = new_ty.toIntern(); |
| 25021 | return Value.fromInterned(try zcu.intern(.{ .ptr = ptr })); | |
| 25259 | return Value.fromInterned(try pt.intern(.{ .ptr = ptr })); | |
| 25022 | 25260 | } |
| 25023 | 25261 | |
| 25024 | 25262 | fn zirMinMax( |
| ... | ... | @@ -25072,7 +25310,8 @@ fn analyzeMinMax( |
| 25072 | 25310 | ) CompileError!Air.Inst.Ref { |
| 25073 | 25311 | assert(operands.len == operand_srcs.len); |
| 25074 | 25312 | assert(operands.len > 0); |
| 25075 | const mod = sema.mod; | |
| 25313 | const pt = sema.pt; | |
| 25314 | const mod = pt.zcu; | |
| 25076 | 25315 | |
| 25077 | 25316 | if (operands.len == 1) return operands[0]; |
| 25078 | 25317 | |
| ... | ... | @@ -25115,15 +25354,15 @@ fn analyzeMinMax( |
| 25115 | 25354 | break :refine_bounds; |
| 25116 | 25355 | } |
| 25117 | 25356 | const scalar_bounds: ?[2]Value = bounds: { |
| 25118 | if (!ty.isVector(mod)) break :bounds try uncoerced_val.intValueBounds(mod); | |
| 25119 | var cur_bounds: [2]Value = try Value.intValueBounds(try uncoerced_val.elemValue(mod, 0), mod) orelse break :bounds null; | |
| 25357 | if (!ty.isVector(mod)) break :bounds try uncoerced_val.intValueBounds(pt); | |
| 25358 | var cur_bounds: [2]Value = try Value.intValueBounds(try uncoerced_val.elemValue(pt, 0), pt) orelse break :bounds null; | |
| 25120 | 25359 | const len = try sema.usizeCast(block, src, ty.vectorLen(mod)); |
| 25121 | 25360 | for (1..len) |i| { |
| 25122 | const elem = try uncoerced_val.elemValue(mod, i); | |
| 25123 | const elem_bounds = try elem.intValueBounds(mod) orelse break :bounds null; | |
| 25361 | const elem = try uncoerced_val.elemValue(pt, i); | |
| 25362 | const elem_bounds = try elem.intValueBounds(pt) orelse break :bounds null; | |
| 25124 | 25363 | cur_bounds = .{ |
| 25125 | Value.numberMin(elem_bounds[0], cur_bounds[0], mod), | |
| 25126 | Value.numberMax(elem_bounds[1], cur_bounds[1], mod), | |
| 25364 | Value.numberMin(elem_bounds[0], cur_bounds[0], pt), | |
| 25365 | Value.numberMax(elem_bounds[1], cur_bounds[1], pt), | |
| 25127 | 25366 | }; |
| 25128 | 25367 | } |
| 25129 | 25368 | break :bounds cur_bounds; |
| ... | ... | @@ -25134,8 +25373,8 @@ fn analyzeMinMax( |
| 25134 | 25373 | cur_max_scalar = bounds[1]; |
| 25135 | 25374 | bounds_status = .defined; |
| 25136 | 25375 | } else { |
| 25137 | cur_min_scalar = opFunc(cur_min_scalar, bounds[0], mod); | |
| 25138 | cur_max_scalar = opFunc(cur_max_scalar, bounds[1], mod); | |
| 25376 | cur_min_scalar = opFunc(cur_min_scalar, bounds[0], pt); | |
| 25377 | cur_max_scalar = opFunc(cur_max_scalar, bounds[1], pt); | |
| 25139 | 25378 | } |
| 25140 | 25379 | } |
| 25141 | 25380 | }, |
| ... | ... | @@ -25153,18 +25392,18 @@ fn analyzeMinMax( |
| 25153 | 25392 | const operand_val = try sema.resolveLazyValue(simd_op.rhs_val.?); // we checked the operand was resolvable above |
| 25154 | 25393 | |
| 25155 | 25394 | const vec_len = simd_op.len orelse { |
| 25156 | const result_val = opFunc(cur_val, operand_val, mod); | |
| 25395 | const result_val = opFunc(cur_val, operand_val, pt); | |
| 25157 | 25396 | cur_minmax = Air.internedToRef(result_val.toIntern()); |
| 25158 | 25397 | continue; |
| 25159 | 25398 | }; |
| 25160 | 25399 | const elems = try sema.arena.alloc(InternPool.Index, vec_len); |
| 25161 | 25400 | for (elems, 0..) |*elem, i| { |
| 25162 | const lhs_elem_val = try cur_val.elemValue(mod, i); | |
| 25163 | const rhs_elem_val = try operand_val.elemValue(mod, i); | |
| 25164 | const uncoerced_elem = opFunc(lhs_elem_val, rhs_elem_val, mod); | |
| 25165 | elem.* = (try mod.getCoerced(uncoerced_elem, simd_op.scalar_ty)).toIntern(); | |
| 25401 | const lhs_elem_val = try cur_val.elemValue(pt, i); | |
| 25402 | const rhs_elem_val = try operand_val.elemValue(pt, i); | |
| 25403 | const uncoerced_elem = opFunc(lhs_elem_val, rhs_elem_val, pt); | |
| 25404 | elem.* = (try pt.getCoerced(uncoerced_elem, simd_op.scalar_ty)).toIntern(); | |
| 25166 | 25405 | } |
| 25167 | cur_minmax = Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 25406 | cur_minmax = Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 25168 | 25407 | .ty = simd_op.result_ty.toIntern(), |
| 25169 | 25408 | .storage = .{ .elems = elems }, |
| 25170 | 25409 | } }))); |
| ... | ... | @@ -25191,8 +25430,8 @@ fn analyzeMinMax( |
| 25191 | 25430 | |
| 25192 | 25431 | assert(bounds_status == .defined); // there was a non-comptime-int integral comptime-known arg |
| 25193 | 25432 | |
| 25194 | const refined_scalar_ty = try mod.intFittingRange(cur_min_scalar, cur_max_scalar); | |
| 25195 | const refined_ty = if (orig_ty.isVector(mod)) try mod.vectorType(.{ | |
| 25433 | const refined_scalar_ty = try pt.intFittingRange(cur_min_scalar, cur_max_scalar); | |
| 25434 | const refined_ty = if (orig_ty.isVector(mod)) try pt.vectorType(.{ | |
| 25196 | 25435 | .len = orig_ty.vectorLen(mod), |
| 25197 | 25436 | .child = refined_scalar_ty.toIntern(), |
| 25198 | 25437 | }) else refined_scalar_ty; |
| ... | ... | @@ -25226,8 +25465,8 @@ fn analyzeMinMax( |
| 25226 | 25465 | runtime_known.unset(0); // don't look at this operand in the loop below |
| 25227 | 25466 | const scalar_ty = sema.typeOf(cur_minmax.?).scalarType(mod); |
| 25228 | 25467 | if (scalar_ty.isInt(mod)) { |
| 25229 | cur_min_scalar = try scalar_ty.minInt(mod, scalar_ty); | |
| 25230 | cur_max_scalar = try scalar_ty.maxInt(mod, scalar_ty); | |
| 25468 | cur_min_scalar = try scalar_ty.minInt(pt, scalar_ty); | |
| 25469 | cur_max_scalar = try scalar_ty.maxInt(pt, scalar_ty); | |
| 25231 | 25470 | bounds_status = .defined; |
| 25232 | 25471 | } else { |
| 25233 | 25472 | bounds_status = .non_integral; |
| ... | ... | @@ -25242,7 +25481,7 @@ fn analyzeMinMax( |
| 25242 | 25481 | const rhs_src = operand_srcs[idx]; |
| 25243 | 25482 | const simd_op = try sema.checkSimdBinOp(block, src, lhs, rhs, lhs_src, rhs_src); |
| 25244 | 25483 | if (known_undef) { |
| 25245 | cur_minmax = try mod.undefRef(simd_op.result_ty); | |
| 25484 | cur_minmax = try pt.undefRef(simd_op.result_ty); | |
| 25246 | 25485 | } else { |
| 25247 | 25486 | cur_minmax = try block.addBinOp(air_tag, simd_op.lhs, simd_op.rhs); |
| 25248 | 25487 | } |
| ... | ... | @@ -25254,15 +25493,15 @@ fn analyzeMinMax( |
| 25254 | 25493 | bounds_status = .non_integral; |
| 25255 | 25494 | break :refine_bounds; |
| 25256 | 25495 | } |
| 25257 | const scalar_min = try scalar_ty.minInt(mod, scalar_ty); | |
| 25258 | const scalar_max = try scalar_ty.maxInt(mod, scalar_ty); | |
| 25496 | const scalar_min = try scalar_ty.minInt(pt, scalar_ty); | |
| 25497 | const scalar_max = try scalar_ty.maxInt(pt, scalar_ty); | |
| 25259 | 25498 | if (bounds_status == .unknown) { |
| 25260 | 25499 | cur_min_scalar = scalar_min; |
| 25261 | 25500 | cur_max_scalar = scalar_max; |
| 25262 | 25501 | bounds_status = .defined; |
| 25263 | 25502 | } else { |
| 25264 | cur_min_scalar = opFunc(cur_min_scalar, scalar_min, mod); | |
| 25265 | cur_max_scalar = opFunc(cur_max_scalar, scalar_max, mod); | |
| 25503 | cur_min_scalar = opFunc(cur_min_scalar, scalar_min, pt); | |
| 25504 | cur_max_scalar = opFunc(cur_max_scalar, scalar_max, pt); | |
| 25266 | 25505 | } |
| 25267 | 25506 | }, |
| 25268 | 25507 | .non_integral => {}, |
| ... | ... | @@ -25276,8 +25515,8 @@ fn analyzeMinMax( |
| 25276 | 25515 | return cur_minmax.?; |
| 25277 | 25516 | } |
| 25278 | 25517 | assert(bounds_status == .defined); // there were integral runtime operands |
| 25279 | const refined_scalar_ty = try mod.intFittingRange(cur_min_scalar, cur_max_scalar); | |
| 25280 | const refined_ty = if (unrefined_ty.isVector(mod)) try mod.vectorType(.{ | |
| 25518 | const refined_scalar_ty = try pt.intFittingRange(cur_min_scalar, cur_max_scalar); | |
| 25519 | const refined_ty = if (unrefined_ty.isVector(mod)) try pt.vectorType(.{ | |
| 25281 | 25520 | .len = unrefined_ty.vectorLen(mod), |
| 25282 | 25521 | .child = refined_scalar_ty.toIntern(), |
| 25283 | 25522 | }) else refined_scalar_ty; |
| ... | ... | @@ -25291,15 +25530,16 @@ fn analyzeMinMax( |
| 25291 | 25530 | } |
| 25292 | 25531 | |
| 25293 | 25532 | fn upgradeToArrayPtr(sema: *Sema, block: *Block, ptr: Air.Inst.Ref, len: u64) !Air.Inst.Ref { |
| 25294 | const mod = sema.mod; | |
| 25533 | const pt = sema.pt; | |
| 25534 | const mod = pt.zcu; | |
| 25295 | 25535 | const ptr_ty = sema.typeOf(ptr); |
| 25296 | 25536 | const info = ptr_ty.ptrInfo(mod); |
| 25297 | 25537 | if (info.flags.size == .One) { |
| 25298 | 25538 | // Already an array pointer. |
| 25299 | 25539 | return ptr; |
| 25300 | 25540 | } |
| 25301 | const new_ty = try mod.ptrTypeSema(.{ | |
| 25302 | .child = (try mod.arrayType(.{ | |
| 25541 | const new_ty = try pt.ptrTypeSema(.{ | |
| 25542 | .child = (try pt.arrayType(.{ | |
| 25303 | 25543 | .len = len, |
| 25304 | 25544 | .sentinel = info.sentinel, |
| 25305 | 25545 | .child = info.child, |
| ... | ... | @@ -25331,8 +25571,9 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 25331 | 25571 | const src_ty = sema.typeOf(src_ptr); |
| 25332 | 25572 | const dest_len = try indexablePtrLenOrNone(sema, block, dest_src, dest_ptr); |
| 25333 | 25573 | const src_len = try indexablePtrLenOrNone(sema, block, src_src, src_ptr); |
| 25334 | const target = sema.mod.getTarget(); | |
| 25335 | const mod = sema.mod; | |
| 25574 | const pt = sema.pt; | |
| 25575 | const mod = pt.zcu; | |
| 25576 | const target = mod.getTarget(); | |
| 25336 | 25577 | |
| 25337 | 25578 | if (dest_ty.isConstPtr(mod)) { |
| 25338 | 25579 | return sema.fail(block, dest_src, "cannot memcpy to constant pointer", .{}); |
| ... | ... | @@ -25343,10 +25584,10 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 25343 | 25584 | const msg = try sema.errMsg(src, "unknown @memcpy length", .{}); |
| 25344 | 25585 | errdefer msg.destroy(sema.gpa); |
| 25345 | 25586 | try sema.errNote(dest_src, msg, "destination type '{}' provides no length", .{ |
| 25346 | dest_ty.fmt(sema.mod), | |
| 25587 | dest_ty.fmt(pt), | |
| 25347 | 25588 | }); |
| 25348 | 25589 | try sema.errNote(src_src, msg, "source type '{}' provides no length", .{ |
| 25349 | src_ty.fmt(sema.mod), | |
| 25590 | src_ty.fmt(pt), | |
| 25350 | 25591 | }); |
| 25351 | 25592 | break :msg msg; |
| 25352 | 25593 | }; |
| ... | ... | @@ -25365,10 +25606,10 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 25365 | 25606 | const msg = try sema.errMsg(src, "non-matching @memcpy lengths", .{}); |
| 25366 | 25607 | errdefer msg.destroy(sema.gpa); |
| 25367 | 25608 | try sema.errNote(dest_src, msg, "length {} here", .{ |
| 25368 | dest_len_val.fmtValue(sema.mod, sema), | |
| 25609 | dest_len_val.fmtValue(pt, sema), | |
| 25369 | 25610 | }); |
| 25370 | 25611 | try sema.errNote(src_src, msg, "length {} here", .{ |
| 25371 | src_len_val.fmtValue(sema.mod, sema), | |
| 25612 | src_len_val.fmtValue(pt, sema), | |
| 25372 | 25613 | }); |
| 25373 | 25614 | break :msg msg; |
| 25374 | 25615 | }; |
| ... | ... | @@ -25397,10 +25638,10 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 25397 | 25638 | const runtime_src = if (try sema.resolveDefinedValue(block, dest_src, dest_ptr)) |dest_ptr_val| rs: { |
| 25398 | 25639 | if (!sema.isComptimeMutablePtr(dest_ptr_val)) break :rs dest_src; |
| 25399 | 25640 | if (try sema.resolveDefinedValue(block, src_src, src_ptr)) |_| { |
| 25400 | const len_u64 = (try len_val.?.getUnsignedIntAdvanced(mod, .sema)).?; | |
| 25641 | const len_u64 = (try len_val.?.getUnsignedIntAdvanced(pt, .sema)).?; | |
| 25401 | 25642 | const len = try sema.usizeCast(block, dest_src, len_u64); |
| 25402 | 25643 | for (0..len) |i| { |
| 25403 | const elem_index = try mod.intRef(Type.usize, i); | |
| 25644 | const elem_index = try pt.intRef(Type.usize, i); | |
| 25404 | 25645 | const dest_elem_ptr = try sema.elemPtrOneLayerOnly( |
| 25405 | 25646 | block, |
| 25406 | 25647 | src, |
| ... | ... | @@ -25456,7 +25697,7 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 25456 | 25697 | var new_dest_ptr = dest_ptr; |
| 25457 | 25698 | var new_src_ptr = src_ptr; |
| 25458 | 25699 | if (len_val) |val| { |
| 25459 | const len = try val.toUnsignedIntSema(mod); | |
| 25700 | const len = try val.toUnsignedIntSema(pt); | |
| 25460 | 25701 | if (len == 0) { |
| 25461 | 25702 | // This AIR instruction guarantees length > 0 if it is comptime-known. |
| 25462 | 25703 | return; |
| ... | ... | @@ -25503,7 +25744,7 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 25503 | 25744 | assert(dest_manyptr_ty_key.flags.size == .One); |
| 25504 | 25745 | dest_manyptr_ty_key.child = dest_elem_ty.toIntern(); |
| 25505 | 25746 | dest_manyptr_ty_key.flags.size = .Many; |
| 25506 | break :ptr try sema.coerceCompatiblePtrs(block, try mod.ptrTypeSema(dest_manyptr_ty_key), new_dest_ptr, dest_src); | |
| 25747 | break :ptr try sema.coerceCompatiblePtrs(block, try pt.ptrTypeSema(dest_manyptr_ty_key), new_dest_ptr, dest_src); | |
| 25507 | 25748 | } else new_dest_ptr; |
| 25508 | 25749 | |
| 25509 | 25750 | const new_src_ptr_ty = sema.typeOf(new_src_ptr); |
| ... | ... | @@ -25514,7 +25755,7 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 25514 | 25755 | assert(src_manyptr_ty_key.flags.size == .One); |
| 25515 | 25756 | src_manyptr_ty_key.child = src_elem_ty.toIntern(); |
| 25516 | 25757 | src_manyptr_ty_key.flags.size = .Many; |
| 25517 | break :ptr try sema.coerceCompatiblePtrs(block, try mod.ptrTypeSema(src_manyptr_ty_key), new_src_ptr, src_src); | |
| 25758 | break :ptr try sema.coerceCompatiblePtrs(block, try pt.ptrTypeSema(src_manyptr_ty_key), new_src_ptr, src_src); | |
| 25518 | 25759 | } else new_src_ptr; |
| 25519 | 25760 | |
| 25520 | 25761 | // ok1: dest >= src + len |
| ... | ... | @@ -25537,7 +25778,8 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 25537 | 25778 | } |
| 25538 | 25779 | |
| 25539 | 25780 | fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void { |
| 25540 | const mod = sema.mod; | |
| 25781 | const pt = sema.pt; | |
| 25782 | const mod = pt.zcu; | |
| 25541 | 25783 | const gpa = sema.gpa; |
| 25542 | 25784 | const ip = &mod.intern_pool; |
| 25543 | 25785 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| ... | ... | @@ -25569,7 +25811,7 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 25569 | 25811 | const msg = try sema.errMsg(src, "unknown @memset length", .{}); |
| 25570 | 25812 | errdefer msg.destroy(sema.gpa); |
| 25571 | 25813 | try sema.errNote(dest_src, msg, "destination type '{}' provides no length", .{ |
| 25572 | dest_ptr_ty.fmt(mod), | |
| 25814 | dest_ptr_ty.fmt(pt), | |
| 25573 | 25815 | }); |
| 25574 | 25816 | break :msg msg; |
| 25575 | 25817 | }); |
| ... | ... | @@ -25581,7 +25823,7 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 25581 | 25823 | const ptr_val = try sema.resolveDefinedValue(block, dest_src, dest_ptr) orelse break :rs dest_src; |
| 25582 | 25824 | const len_air_ref = try sema.fieldVal(block, src, dest_ptr, try ip.getOrPutString(gpa, "len", .no_embedded_nulls), dest_src); |
| 25583 | 25825 | const len_val = (try sema.resolveDefinedValue(block, dest_src, len_air_ref)) orelse break :rs dest_src; |
| 25584 | const len_u64 = (try len_val.getUnsignedIntAdvanced(mod, .sema)).?; | |
| 25826 | const len_u64 = (try len_val.getUnsignedIntAdvanced(pt, .sema)).?; | |
| 25585 | 25827 | const len = try sema.usizeCast(block, dest_src, len_u64); |
| 25586 | 25828 | if (len == 0) { |
| 25587 | 25829 | // This AIR instruction guarantees length > 0 if it is comptime-known. |
| ... | ... | @@ -25590,22 +25832,22 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 25590 | 25832 | |
| 25591 | 25833 | if (!sema.isComptimeMutablePtr(ptr_val)) break :rs dest_src; |
| 25592 | 25834 | const elem_val = try sema.resolveValue(elem) orelse break :rs value_src; |
| 25593 | const array_ty = try mod.arrayType(.{ | |
| 25835 | const array_ty = try pt.arrayType(.{ | |
| 25594 | 25836 | .child = dest_elem_ty.toIntern(), |
| 25595 | 25837 | .len = len_u64, |
| 25596 | 25838 | }); |
| 25597 | const array_val = Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 25839 | const array_val = Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 25598 | 25840 | .ty = array_ty.toIntern(), |
| 25599 | 25841 | .storage = .{ .repeated_elem = elem_val.toIntern() }, |
| 25600 | } }))); | |
| 25842 | } })); | |
| 25601 | 25843 | const array_ptr_ty = ty: { |
| 25602 | 25844 | var info = dest_ptr_ty.ptrInfo(mod); |
| 25603 | 25845 | info.flags.size = .One; |
| 25604 | 25846 | info.child = array_ty.toIntern(); |
| 25605 | break :ty try mod.ptrType(info); | |
| 25847 | break :ty try pt.ptrType(info); | |
| 25606 | 25848 | }; |
| 25607 | 25849 | const raw_ptr_val = if (dest_ptr_ty.isSlice(mod)) ptr_val.slicePtr(mod) else ptr_val; |
| 25608 | const array_ptr_val = try mod.getCoerced(raw_ptr_val, array_ptr_ty); | |
| 25850 | const array_ptr_val = try pt.getCoerced(raw_ptr_val, array_ptr_ty); | |
| 25609 | 25851 | return sema.storePtrVal(block, src, array_ptr_val, array_val, array_ty); |
| 25610 | 25852 | }; |
| 25611 | 25853 | |
| ... | ... | @@ -25658,7 +25900,8 @@ fn zirVarExtended( |
| 25658 | 25900 | block: *Block, |
| 25659 | 25901 | extended: Zir.Inst.Extended.InstData, |
| 25660 | 25902 | ) CompileError!Air.Inst.Ref { |
| 25661 | const mod = sema.mod; | |
| 25903 | const pt = sema.pt; | |
| 25904 | const mod = pt.zcu; | |
| 25662 | 25905 | const extra = sema.code.extraData(Zir.Inst.ExtendedVar, extended.operand); |
| 25663 | 25906 | const ty_src = block.src(.{ .node_offset_var_decl_ty = 0 }); |
| 25664 | 25907 | const init_src = block.src(.{ .node_offset_var_decl_init = 0 }); |
| ... | ... | @@ -25705,7 +25948,7 @@ fn zirVarExtended( |
| 25705 | 25948 | |
| 25706 | 25949 | try sema.validateVarType(block, ty_src, var_ty, small.is_extern); |
| 25707 | 25950 | |
| 25708 | return Air.internedToRef((try mod.intern(.{ .variable = .{ | |
| 25951 | return Air.internedToRef((try pt.intern(.{ .variable = .{ | |
| 25709 | 25952 | .ty = var_ty.toIntern(), |
| 25710 | 25953 | .init = init_val, |
| 25711 | 25954 | .decl = sema.owner_decl_index, |
| ... | ... | @@ -25721,7 +25964,8 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 25721 | 25964 | const tracy = trace(@src()); |
| 25722 | 25965 | defer tracy.end(); |
| 25723 | 25966 | |
| 25724 | const mod = sema.mod; | |
| 25967 | const pt = sema.pt; | |
| 25968 | const mod = pt.zcu; | |
| 25725 | 25969 | const inst_data = sema.code.instructions.items(.data)[@intFromEnum(inst)].pl_node; |
| 25726 | 25970 | const extra = sema.code.extraData(Zir.Inst.FuncFancy, inst_data.payload_index); |
| 25727 | 25971 | const target = mod.getTarget(); |
| ... | ... | @@ -25761,7 +26005,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 25761 | 26005 | if (val.isGenericPoison()) { |
| 25762 | 26006 | break :blk null; |
| 25763 | 26007 | } |
| 25764 | const alignment = try sema.validateAlignAllowZero(block, align_src, try val.toUnsignedIntSema(mod)); | |
| 26008 | const alignment = try sema.validateAlignAllowZero(block, align_src, try val.toUnsignedIntSema(pt)); | |
| 25765 | 26009 | const default = target_util.defaultFunctionAlignment(target); |
| 25766 | 26010 | break :blk if (alignment == default) .none else alignment; |
| 25767 | 26011 | } else if (extra.data.bits.has_align_ref) blk: { |
| ... | ... | @@ -25781,7 +26025,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 25781 | 26025 | error.GenericPoison => break :blk null, |
| 25782 | 26026 | else => |e| return e, |
| 25783 | 26027 | }; |
| 25784 | const alignment = try sema.validateAlignAllowZero(block, align_src, try align_val.toUnsignedIntSema(mod)); | |
| 26028 | const alignment = try sema.validateAlignAllowZero(block, align_src, try align_val.toUnsignedIntSema(pt)); | |
| 25785 | 26029 | const default = target_util.defaultFunctionAlignment(target); |
| 25786 | 26030 | break :blk if (alignment == default) .none else alignment; |
| 25787 | 26031 | } else .none; |
| ... | ... | @@ -25857,7 +26101,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 25857 | 26101 | const body = sema.code.bodySlice(extra_index, body_len); |
| 25858 | 26102 | extra_index += body.len; |
| 25859 | 26103 | |
| 25860 | const cc_ty = try mod.getBuiltinType("CallingConvention"); | |
| 26104 | const cc_ty = try pt.getBuiltinType("CallingConvention"); | |
| 25861 | 26105 | const val = try sema.resolveGenericBody(block, cc_src, body, inst, cc_ty, .{ |
| 25862 | 26106 | .needed_comptime_reason = "calling convention must be comptime-known", |
| 25863 | 26107 | }); |
| ... | ... | @@ -25986,7 +26230,8 @@ fn zirCDefine( |
| 25986 | 26230 | block: *Block, |
| 25987 | 26231 | extended: Zir.Inst.Extended.InstData, |
| 25988 | 26232 | ) CompileError!Air.Inst.Ref { |
| 25989 | const mod = sema.mod; | |
| 26233 | const pt = sema.pt; | |
| 26234 | const mod = pt.zcu; | |
| 25990 | 26235 | const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data; |
| 25991 | 26236 | const name_src = block.builtinCallArgSrc(extra.node, 0); |
| 25992 | 26237 | const val_src = block.builtinCallArgSrc(extra.node, 1); |
| ... | ... | @@ -26014,7 +26259,7 @@ fn zirWasmMemorySize( |
| 26014 | 26259 | const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; |
| 26015 | 26260 | const index_src = block.builtinCallArgSrc(extra.node, 0); |
| 26016 | 26261 | const builtin_src = block.nodeOffset(extra.node); |
| 26017 | const target = sema.mod.getTarget(); | |
| 26262 | const target = sema.pt.zcu.getTarget(); | |
| 26018 | 26263 | if (!target.isWasm()) { |
| 26019 | 26264 | return sema.fail(block, builtin_src, "builtin @wasmMemorySize is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)}); |
| 26020 | 26265 | } |
| ... | ... | @@ -26041,7 +26286,7 @@ fn zirWasmMemoryGrow( |
| 26041 | 26286 | const builtin_src = block.nodeOffset(extra.node); |
| 26042 | 26287 | const index_src = block.builtinCallArgSrc(extra.node, 0); |
| 26043 | 26288 | const delta_src = block.builtinCallArgSrc(extra.node, 1); |
| 26044 | const target = sema.mod.getTarget(); | |
| 26289 | const target = sema.pt.zcu.getTarget(); | |
| 26045 | 26290 | if (!target.isWasm()) { |
| 26046 | 26291 | return sema.fail(block, builtin_src, "builtin @wasmMemoryGrow is available when targeting WebAssembly; targeted CPU architecture is {s}", .{@tagName(target.cpu.arch)}); |
| 26047 | 26292 | } |
| ... | ... | @@ -26067,10 +26312,11 @@ fn resolvePrefetchOptions( |
| 26067 | 26312 | src: LazySrcLoc, |
| 26068 | 26313 | zir_ref: Zir.Inst.Ref, |
| 26069 | 26314 | ) CompileError!std.builtin.PrefetchOptions { |
| 26070 | const mod = sema.mod; | |
| 26315 | const pt = sema.pt; | |
| 26316 | const mod = pt.zcu; | |
| 26071 | 26317 | const gpa = sema.gpa; |
| 26072 | 26318 | const ip = &mod.intern_pool; |
| 26073 | const options_ty = try mod.getBuiltinType("PrefetchOptions"); | |
| 26319 | const options_ty = try pt.getBuiltinType("PrefetchOptions"); | |
| 26074 | 26320 | const options = try sema.coerce(block, options_ty, try sema.resolveInst(zir_ref), src); |
| 26075 | 26321 | |
| 26076 | 26322 | const rw_src = block.src(.{ .init_field_rw = src.offset.node_offset_builtin_call_arg.builtin_call_node }); |
| ... | ... | @@ -26094,7 +26340,7 @@ fn resolvePrefetchOptions( |
| 26094 | 26340 | |
| 26095 | 26341 | return std.builtin.PrefetchOptions{ |
| 26096 | 26342 | .rw = mod.toEnum(std.builtin.PrefetchOptions.Rw, rw_val), |
| 26097 | .locality = @intCast(try locality_val.toUnsignedIntSema(mod)), | |
| 26343 | .locality = @intCast(try locality_val.toUnsignedIntSema(pt)), | |
| 26098 | 26344 | .cache = mod.toEnum(std.builtin.PrefetchOptions.Cache, cache_val), |
| 26099 | 26345 | }; |
| 26100 | 26346 | } |
| ... | ... | @@ -26138,11 +26384,12 @@ fn resolveExternOptions( |
| 26138 | 26384 | linkage: std.builtin.GlobalLinkage = .strong, |
| 26139 | 26385 | is_thread_local: bool = false, |
| 26140 | 26386 | } { |
| 26141 | const mod = sema.mod; | |
| 26387 | const pt = sema.pt; | |
| 26388 | const mod = pt.zcu; | |
| 26142 | 26389 | const gpa = sema.gpa; |
| 26143 | 26390 | const ip = &mod.intern_pool; |
| 26144 | 26391 | const options_inst = try sema.resolveInst(zir_ref); |
| 26145 | const extern_options_ty = try mod.getBuiltinType("ExternOptions"); | |
| 26392 | const extern_options_ty = try pt.getBuiltinType("ExternOptions"); | |
| 26146 | 26393 | const options = try sema.coerce(block, extern_options_ty, options_inst, src); |
| 26147 | 26394 | |
| 26148 | 26395 | const name_src = block.src(.{ .init_field_name = src.offset.node_offset_builtin_call_arg.builtin_call_node }); |
| ... | ... | @@ -26203,7 +26450,8 @@ fn zirBuiltinExtern( |
| 26203 | 26450 | block: *Block, |
| 26204 | 26451 | extended: Zir.Inst.Extended.InstData, |
| 26205 | 26452 | ) CompileError!Air.Inst.Ref { |
| 26206 | const mod = sema.mod; | |
| 26453 | const pt = sema.pt; | |
| 26454 | const mod = pt.zcu; | |
| 26207 | 26455 | const ip = &mod.intern_pool; |
| 26208 | 26456 | const extra = sema.code.extraData(Zir.Inst.BinNode, extended.operand).data; |
| 26209 | 26457 | const ty_src = block.builtinCallArgSrc(extra.node, 0); |
| ... | ... | @@ -26215,7 +26463,7 @@ fn zirBuiltinExtern( |
| 26215 | 26463 | } |
| 26216 | 26464 | if (!try sema.validateExternType(ty, .other)) { |
| 26217 | 26465 | const msg = msg: { |
| 26218 | const msg = try sema.errMsg(ty_src, "extern symbol cannot have type '{}'", .{ty.fmt(mod)}); | |
| 26466 | const msg = try sema.errMsg(ty_src, "extern symbol cannot have type '{}'", .{ty.fmt(pt)}); | |
| 26219 | 26467 | errdefer msg.destroy(sema.gpa); |
| 26220 | 26468 | try sema.explainWhyTypeIsNotExtern(msg, ty_src, ty, .other); |
| 26221 | 26469 | break :msg msg; |
| ... | ... | @@ -26226,7 +26474,7 @@ fn zirBuiltinExtern( |
| 26226 | 26474 | const options = try sema.resolveExternOptions(block, options_src, extra.rhs); |
| 26227 | 26475 | |
| 26228 | 26476 | if (options.linkage == .weak and !ty.ptrAllowsZero(mod)) { |
| 26229 | ty = try mod.optionalType(ty.toIntern()); | |
| 26477 | ty = try pt.optionalType(ty.toIntern()); | |
| 26230 | 26478 | } |
| 26231 | 26479 | const ptr_info = ty.ptrInfo(mod); |
| 26232 | 26480 | |
| ... | ... | @@ -26237,13 +26485,13 @@ fn zirBuiltinExtern( |
| 26237 | 26485 | new_decl_index, |
| 26238 | 26486 | Value.fromInterned( |
| 26239 | 26487 | if (Type.fromInterned(ptr_info.child).zigTypeTag(mod) == .Fn) |
| 26240 | try ip.getExternFunc(sema.gpa, .{ | |
| 26488 | try ip.getExternFunc(sema.gpa, pt.tid, .{ | |
| 26241 | 26489 | .ty = ptr_info.child, |
| 26242 | 26490 | .decl = new_decl_index, |
| 26243 | 26491 | .lib_name = options.library_name, |
| 26244 | 26492 | }) |
| 26245 | 26493 | else |
| 26246 | try mod.intern(.{ .variable = .{ | |
| 26494 | try pt.intern(.{ .variable = .{ | |
| 26247 | 26495 | .ty = ptr_info.child, |
| 26248 | 26496 | .init = .none, |
| 26249 | 26497 | .decl = new_decl_index, |
| ... | ... | @@ -26259,9 +26507,9 @@ fn zirBuiltinExtern( |
| 26259 | 26507 | new_decl.owns_tv = true; |
| 26260 | 26508 | // Note that this will queue the anon decl for codegen, so that the backend can |
| 26261 | 26509 | // correctly handle the extern, including duplicate detection. |
| 26262 | try mod.finalizeAnonDecl(new_decl_index); | |
| 26510 | try pt.finalizeAnonDecl(new_decl_index); | |
| 26263 | 26511 | |
| 26264 | return Air.internedToRef((try mod.getCoerced(Value.fromInterned((try mod.intern(.{ .ptr = .{ | |
| 26512 | return Air.internedToRef((try pt.getCoerced(Value.fromInterned(try pt.intern(.{ .ptr = .{ | |
| 26265 | 26513 | .ty = switch (ip.indexToKey(ty.toIntern())) { |
| 26266 | 26514 | .ptr_type => ty.toIntern(), |
| 26267 | 26515 | .opt_type => |child_type| child_type, |
| ... | ... | @@ -26269,7 +26517,7 @@ fn zirBuiltinExtern( |
| 26269 | 26517 | }, |
| 26270 | 26518 | .base_addr = .{ .decl = new_decl_index }, |
| 26271 | 26519 | .byte_offset = 0, |
| 26272 | } }))), ty)).toIntern()); | |
| 26520 | } })), ty)).toIntern()); | |
| 26273 | 26521 | } |
| 26274 | 26522 | |
| 26275 | 26523 | fn zirWorkItem( |
| ... | ... | @@ -26281,7 +26529,7 @@ fn zirWorkItem( |
| 26281 | 26529 | const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data; |
| 26282 | 26530 | const dimension_src = block.builtinCallArgSrc(extra.node, 0); |
| 26283 | 26531 | const builtin_src = block.nodeOffset(extra.node); |
| 26284 | const target = sema.mod.getTarget(); | |
| 26532 | const target = sema.pt.zcu.getTarget(); | |
| 26285 | 26533 | |
| 26286 | 26534 | switch (target.cpu.arch) { |
| 26287 | 26535 | // TODO: Allow for other GPU targets. |
| ... | ... | @@ -26344,11 +26592,12 @@ fn validateVarType( |
| 26344 | 26592 | var_ty: Type, |
| 26345 | 26593 | is_extern: bool, |
| 26346 | 26594 | ) CompileError!void { |
| 26347 | const mod = sema.mod; | |
| 26595 | const pt = sema.pt; | |
| 26596 | const mod = pt.zcu; | |
| 26348 | 26597 | if (is_extern) { |
| 26349 | 26598 | if (!try sema.validateExternType(var_ty, .other)) { |
| 26350 | 26599 | const msg = msg: { |
| 26351 | const msg = try sema.errMsg(src, "extern variable cannot have type '{}'", .{var_ty.fmt(mod)}); | |
| 26600 | const msg = try sema.errMsg(src, "extern variable cannot have type '{}'", .{var_ty.fmt(pt)}); | |
| 26352 | 26601 | errdefer msg.destroy(sema.gpa); |
| 26353 | 26602 | try sema.explainWhyTypeIsNotExtern(msg, src, var_ty, .other); |
| 26354 | 26603 | break :msg msg; |
| ... | ... | @@ -26361,7 +26610,7 @@ fn validateVarType( |
| 26361 | 26610 | block, |
| 26362 | 26611 | src, |
| 26363 | 26612 | "non-extern variable with opaque type '{}'", |
| 26364 | .{var_ty.fmt(mod)}, | |
| 26613 | .{var_ty.fmt(pt)}, | |
| 26365 | 26614 | ); |
| 26366 | 26615 | } |
| 26367 | 26616 | } |
| ... | ... | @@ -26369,7 +26618,7 @@ fn validateVarType( |
| 26369 | 26618 | if (!try sema.typeRequiresComptime(var_ty)) return; |
| 26370 | 26619 | |
| 26371 | 26620 | const msg = msg: { |
| 26372 | const msg = try sema.errMsg(src, "variable of type '{}' must be const or comptime", .{var_ty.fmt(mod)}); | |
| 26621 | const msg = try sema.errMsg(src, "variable of type '{}' must be const or comptime", .{var_ty.fmt(pt)}); | |
| 26373 | 26622 | errdefer msg.destroy(sema.gpa); |
| 26374 | 26623 | |
| 26375 | 26624 | try sema.explainWhyTypeIsComptime(msg, src, var_ty); |
| ... | ... | @@ -26393,7 +26642,7 @@ fn explainWhyTypeIsComptime( |
| 26393 | 26642 | var type_set = TypeSet{}; |
| 26394 | 26643 | defer type_set.deinit(sema.gpa); |
| 26395 | 26644 | |
| 26396 | try ty.resolveFully(sema.mod); | |
| 26645 | try ty.resolveFully(sema.pt); | |
| 26397 | 26646 | return sema.explainWhyTypeIsComptimeInner(msg, src_loc, ty, &type_set); |
| 26398 | 26647 | } |
| 26399 | 26648 | |
| ... | ... | @@ -26404,7 +26653,8 @@ fn explainWhyTypeIsComptimeInner( |
| 26404 | 26653 | ty: Type, |
| 26405 | 26654 | type_set: *TypeSet, |
| 26406 | 26655 | ) CompileError!void { |
| 26407 | const mod = sema.mod; | |
| 26656 | const pt = sema.pt; | |
| 26657 | const mod = pt.zcu; | |
| 26408 | 26658 | const ip = &mod.intern_pool; |
| 26409 | 26659 | switch (ty.zigTypeTag(mod)) { |
| 26410 | 26660 | .Bool, |
| ... | ... | @@ -26418,9 +26668,7 @@ fn explainWhyTypeIsComptimeInner( |
| 26418 | 26668 | => return, |
| 26419 | 26669 | |
| 26420 | 26670 | .Fn => { |
| 26421 | try sema.errNote(src_loc, msg, "use '*const {}' for a function pointer type", .{ | |
| 26422 | ty.fmt(sema.mod), | |
| 26423 | }); | |
| 26671 | try sema.errNote(src_loc, msg, "use '*const {}' for a function pointer type", .{ty.fmt(pt)}); | |
| 26424 | 26672 | }, |
| 26425 | 26673 | |
| 26426 | 26674 | .Type => { |
| ... | ... | @@ -26436,7 +26684,7 @@ fn explainWhyTypeIsComptimeInner( |
| 26436 | 26684 | => return, |
| 26437 | 26685 | |
| 26438 | 26686 | .Opaque => { |
| 26439 | try sema.errNote(src_loc, msg, "opaque type '{}' has undefined size", .{ty.fmt(sema.mod)}); | |
| 26687 | try sema.errNote(src_loc, msg, "opaque type '{}' has undefined size", .{ty.fmt(pt)}); | |
| 26440 | 26688 | }, |
| 26441 | 26689 | |
| 26442 | 26690 | .Array, .Vector => { |
| ... | ... | @@ -26453,7 +26701,7 @@ fn explainWhyTypeIsComptimeInner( |
| 26453 | 26701 | .Inline => try sema.errNote(src_loc, msg, "function has inline calling convention", .{}), |
| 26454 | 26702 | else => {}, |
| 26455 | 26703 | } |
| 26456 | if (Type.fromInterned(fn_info.return_type).comptimeOnly(mod)) { | |
| 26704 | if (Type.fromInterned(fn_info.return_type).comptimeOnly(pt)) { | |
| 26457 | 26705 | try sema.errNote(src_loc, msg, "function has a comptime-only return type", .{}); |
| 26458 | 26706 | } |
| 26459 | 26707 | return; |
| ... | ... | @@ -26526,7 +26774,8 @@ fn validateExternType( |
| 26526 | 26774 | ty: Type, |
| 26527 | 26775 | position: ExternPosition, |
| 26528 | 26776 | ) !bool { |
| 26529 | const mod = sema.mod; | |
| 26777 | const pt = sema.pt; | |
| 26778 | const mod = pt.zcu; | |
| 26530 | 26779 | switch (ty.zigTypeTag(mod)) { |
| 26531 | 26780 | .Type, |
| 26532 | 26781 | .ComptimeFloat, |
| ... | ... | @@ -26557,7 +26806,7 @@ fn validateExternType( |
| 26557 | 26806 | }, |
| 26558 | 26807 | .Fn => { |
| 26559 | 26808 | if (position != .other) return false; |
| 26560 | const target = sema.mod.getTarget(); | |
| 26809 | const target = mod.getTarget(); | |
| 26561 | 26810 | // For now we want to authorize PTX kernel to use zig objects, even if we end up exposing the ABI. |
| 26562 | 26811 | // The goal is to experiment with more integrated CPU/GPU code. |
| 26563 | 26812 | if (ty.fnCallingConvention(mod) == .Kernel and (target.cpu.arch == .nvptx or target.cpu.arch == .nvptx64)) { |
| ... | ... | @@ -26571,7 +26820,7 @@ fn validateExternType( |
| 26571 | 26820 | .Struct, .Union => switch (ty.containerLayout(mod)) { |
| 26572 | 26821 | .@"extern" => return true, |
| 26573 | 26822 | .@"packed" => { |
| 26574 | const bit_size = try ty.bitSizeAdvanced(mod, .sema); | |
| 26823 | const bit_size = try ty.bitSizeAdvanced(pt, .sema); | |
| 26575 | 26824 | switch (bit_size) { |
| 26576 | 26825 | 0, 8, 16, 32, 64, 128 => return true, |
| 26577 | 26826 | else => return false, |
| ... | ... | @@ -26595,7 +26844,8 @@ fn explainWhyTypeIsNotExtern( |
| 26595 | 26844 | ty: Type, |
| 26596 | 26845 | position: ExternPosition, |
| 26597 | 26846 | ) CompileError!void { |
| 26598 | const mod = sema.mod; | |
| 26847 | const pt = sema.pt; | |
| 26848 | const mod = pt.zcu; | |
| 26599 | 26849 | switch (ty.zigTypeTag(mod)) { |
| 26600 | 26850 | .Opaque, |
| 26601 | 26851 | .Bool, |
| ... | ... | @@ -26622,7 +26872,7 @@ fn explainWhyTypeIsNotExtern( |
| 26622 | 26872 | if (!ty.isConstPtr(mod) and pointee_ty.zigTypeTag(mod) == .Fn) { |
| 26623 | 26873 | try sema.errNote(src_loc, msg, "pointer to extern function must be 'const'", .{}); |
| 26624 | 26874 | } else if (try sema.typeRequiresComptime(ty)) { |
| 26625 | try sema.errNote(src_loc, msg, "pointer to comptime-only type '{}'", .{pointee_ty.fmt(sema.mod)}); | |
| 26875 | try sema.errNote(src_loc, msg, "pointer to comptime-only type '{}'", .{pointee_ty.fmt(pt)}); | |
| 26626 | 26876 | try sema.explainWhyTypeIsComptime(msg, src_loc, ty); |
| 26627 | 26877 | } |
| 26628 | 26878 | try sema.explainWhyTypeIsNotExtern(msg, src_loc, pointee_ty, .other); |
| ... | ... | @@ -26650,7 +26900,7 @@ fn explainWhyTypeIsNotExtern( |
| 26650 | 26900 | }, |
| 26651 | 26901 | .Enum => { |
| 26652 | 26902 | const tag_ty = ty.intTagType(mod); |
| 26653 | try sema.errNote(src_loc, msg, "enum tag type '{}' is not extern compatible", .{tag_ty.fmt(sema.mod)}); | |
| 26903 | try sema.errNote(src_loc, msg, "enum tag type '{}' is not extern compatible", .{tag_ty.fmt(pt)}); | |
| 26654 | 26904 | try sema.explainWhyTypeIsNotExtern(msg, src_loc, tag_ty, position); |
| 26655 | 26905 | }, |
| 26656 | 26906 | .Struct => try sema.errNote(src_loc, msg, "only extern structs and ABI sized packed structs are extern compatible", .{}), |
| ... | ... | @@ -26671,7 +26921,8 @@ fn explainWhyTypeIsNotExtern( |
| 26671 | 26921 | /// Returns true if `ty` is allowed in packed types. |
| 26672 | 26922 | /// Does not require `ty` to be resolved in any way, but may resolve whether it is comptime-only. |
| 26673 | 26923 | fn validatePackedType(sema: *Sema, ty: Type) !bool { |
| 26674 | const zcu = sema.mod; | |
| 26924 | const pt = sema.pt; | |
| 26925 | const zcu = pt.zcu; | |
| 26675 | 26926 | return switch (ty.zigTypeTag(zcu)) { |
| 26676 | 26927 | .Type, |
| 26677 | 26928 | .ComptimeFloat, |
| ... | ... | @@ -26710,7 +26961,8 @@ fn explainWhyTypeIsNotPacked( |
| 26710 | 26961 | src_loc: LazySrcLoc, |
| 26711 | 26962 | ty: Type, |
| 26712 | 26963 | ) CompileError!void { |
| 26713 | const mod = sema.mod; | |
| 26964 | const pt = sema.pt; | |
| 26965 | const mod = pt.zcu; | |
| 26714 | 26966 | switch (ty.zigTypeTag(mod)) { |
| 26715 | 26967 | .Void, |
| 26716 | 26968 | .Bool, |
| ... | ... | @@ -26750,10 +27002,11 @@ fn explainWhyTypeIsNotPacked( |
| 26750 | 27002 | } |
| 26751 | 27003 | |
| 26752 | 27004 | fn prepareSimplePanic(sema: *Sema) !void { |
| 26753 | const mod = sema.mod; | |
| 27005 | const pt = sema.pt; | |
| 27006 | const mod = pt.zcu; | |
| 26754 | 27007 | |
| 26755 | 27008 | if (mod.panic_func_index == .none) { |
| 26756 | const decl_index = (try mod.getBuiltinDecl("panic")); | |
| 27009 | const decl_index = (try pt.getBuiltinDecl("panic")); | |
| 26757 | 27010 | // decl_index may be an alias; we must find the decl that actually |
| 26758 | 27011 | // owns the function. |
| 26759 | 27012 | try sema.ensureDeclAnalyzed(decl_index); |
| ... | ... | @@ -26766,17 +27019,17 @@ fn prepareSimplePanic(sema: *Sema) !void { |
| 26766 | 27019 | } |
| 26767 | 27020 | |
| 26768 | 27021 | if (mod.null_stack_trace == .none) { |
| 26769 | const stack_trace_ty = try mod.getBuiltinType("StackTrace"); | |
| 26770 | try stack_trace_ty.resolveFields(mod); | |
| 27022 | const stack_trace_ty = try pt.getBuiltinType("StackTrace"); | |
| 27023 | try stack_trace_ty.resolveFields(pt); | |
| 26771 | 27024 | const target = mod.getTarget(); |
| 26772 | const ptr_stack_trace_ty = try mod.ptrTypeSema(.{ | |
| 27025 | const ptr_stack_trace_ty = try pt.ptrTypeSema(.{ | |
| 26773 | 27026 | .child = stack_trace_ty.toIntern(), |
| 26774 | 27027 | .flags = .{ |
| 26775 | 27028 | .address_space = target_util.defaultAddressSpace(target, .global_constant), |
| 26776 | 27029 | }, |
| 26777 | 27030 | }); |
| 26778 | const opt_ptr_stack_trace_ty = try mod.optionalType(ptr_stack_trace_ty.toIntern()); | |
| 26779 | mod.null_stack_trace = try mod.intern(.{ .opt = .{ | |
| 27031 | const opt_ptr_stack_trace_ty = try pt.optionalType(ptr_stack_trace_ty.toIntern()); | |
| 27032 | mod.null_stack_trace = try pt.intern(.{ .opt = .{ | |
| 26780 | 27033 | .ty = opt_ptr_stack_trace_ty.toIntern(), |
| 26781 | 27034 | .val = .none, |
| 26782 | 27035 | } }); |
| ... | ... | @@ -26787,13 +27040,14 @@ fn prepareSimplePanic(sema: *Sema) !void { |
| 26787 | 27040 | /// instructions. This function ensures the panic function will be available to |
| 26788 | 27041 | /// be called during that time. |
| 26789 | 27042 | fn preparePanicId(sema: *Sema, block: *Block, panic_id: Module.PanicId) !InternPool.DeclIndex { |
| 26790 | const mod = sema.mod; | |
| 27043 | const pt = sema.pt; | |
| 27044 | const mod = pt.zcu; | |
| 26791 | 27045 | const gpa = sema.gpa; |
| 26792 | 27046 | if (mod.panic_messages[@intFromEnum(panic_id)].unwrap()) |x| return x; |
| 26793 | 27047 | |
| 26794 | 27048 | try sema.prepareSimplePanic(); |
| 26795 | 27049 | |
| 26796 | const panic_messages_ty = try mod.getBuiltinType("panic_messages"); | |
| 27050 | const panic_messages_ty = try pt.getBuiltinType("panic_messages"); | |
| 26797 | 27051 | const msg_decl_index = (sema.namespaceLookup( |
| 26798 | 27052 | block, |
| 26799 | 27053 | LazySrcLoc.unneeded, |
| ... | ... | @@ -26892,7 +27146,8 @@ fn addSafetyCheckExtra( |
| 26892 | 27146 | } |
| 26893 | 27147 | |
| 26894 | 27148 | fn panicWithMsg(sema: *Sema, block: *Block, src: LazySrcLoc, msg_inst: Air.Inst.Ref, operation: CallOperation) !void { |
| 26895 | const mod = sema.mod; | |
| 27149 | const pt = sema.pt; | |
| 27150 | const mod = pt.zcu; | |
| 26896 | 27151 | |
| 26897 | 27152 | if (!mod.backendSupportsFeature(.panic_fn)) { |
| 26898 | 27153 | _ = try block.addNoOp(.trap); |
| ... | ... | @@ -26905,8 +27160,8 @@ fn panicWithMsg(sema: *Sema, block: *Block, src: LazySrcLoc, msg_inst: Air.Inst. |
| 26905 | 27160 | const panic_fn = try sema.analyzeDeclVal(block, src, panic_func.owner_decl); |
| 26906 | 27161 | const null_stack_trace = Air.internedToRef(mod.null_stack_trace); |
| 26907 | 27162 | |
| 26908 | const opt_usize_ty = try mod.optionalType(.usize_type); | |
| 26909 | const null_ret_addr = Air.internedToRef((try mod.intern(.{ .opt = .{ | |
| 27163 | const opt_usize_ty = try pt.optionalType(.usize_type); | |
| 27164 | const null_ret_addr = Air.internedToRef((try pt.intern(.{ .opt = .{ | |
| 26910 | 27165 | .ty = opt_usize_ty.toIntern(), |
| 26911 | 27166 | .val = .none, |
| 26912 | 27167 | } }))); |
| ... | ... | @@ -26921,9 +27176,10 @@ fn panicUnwrapError( |
| 26921 | 27176 | unwrap_err_tag: Air.Inst.Tag, |
| 26922 | 27177 | is_non_err_tag: Air.Inst.Tag, |
| 26923 | 27178 | ) !void { |
| 27179 | const pt = sema.pt; | |
| 26924 | 27180 | assert(!parent_block.is_comptime); |
| 26925 | 27181 | const ok = try parent_block.addUnOp(is_non_err_tag, operand); |
| 26926 | if (!sema.mod.comp.formatted_panics) { | |
| 27182 | if (!pt.zcu.comp.formatted_panics) { | |
| 26927 | 27183 | return sema.addSafetyCheck(parent_block, src, ok, .unwrap_error); |
| 26928 | 27184 | } |
| 26929 | 27185 | const gpa = sema.gpa; |
| ... | ... | @@ -26942,10 +27198,10 @@ fn panicUnwrapError( |
| 26942 | 27198 | defer fail_block.instructions.deinit(gpa); |
| 26943 | 27199 | |
| 26944 | 27200 | { |
| 26945 | if (!sema.mod.backendSupportsFeature(.panic_unwrap_error)) { | |
| 27201 | if (!pt.zcu.backendSupportsFeature(.panic_unwrap_error)) { | |
| 26946 | 27202 | _ = try fail_block.addNoOp(.trap); |
| 26947 | 27203 | } else { |
| 26948 | const panic_fn = try sema.mod.getBuiltin("panicUnwrapError"); | |
| 27204 | const panic_fn = try sema.pt.getBuiltin("panicUnwrapError"); | |
| 26949 | 27205 | const err = try fail_block.addTyOp(unwrap_err_tag, Type.anyerror, operand); |
| 26950 | 27206 | const err_return_trace = try sema.getErrorReturnTrace(&fail_block); |
| 26951 | 27207 | const args: [2]Air.Inst.Ref = .{ err_return_trace, err }; |
| ... | ... | @@ -26965,7 +27221,7 @@ fn panicIndexOutOfBounds( |
| 26965 | 27221 | ) !void { |
| 26966 | 27222 | assert(!parent_block.is_comptime); |
| 26967 | 27223 | const ok = try parent_block.addBinOp(cmp_op, index, len); |
| 26968 | if (!sema.mod.comp.formatted_panics) { | |
| 27224 | if (!sema.pt.zcu.comp.formatted_panics) { | |
| 26969 | 27225 | return sema.addSafetyCheck(parent_block, src, ok, .index_out_of_bounds); |
| 26970 | 27226 | } |
| 26971 | 27227 | try sema.safetyCheckFormatted(parent_block, src, ok, "panicOutOfBounds", &.{ index, len }); |
| ... | ... | @@ -26980,7 +27236,7 @@ fn panicInactiveUnionField( |
| 26980 | 27236 | ) !void { |
| 26981 | 27237 | assert(!parent_block.is_comptime); |
| 26982 | 27238 | const ok = try parent_block.addBinOp(.cmp_eq, active_tag, wanted_tag); |
| 26983 | if (!sema.mod.comp.formatted_panics) { | |
| 27239 | if (!sema.pt.zcu.comp.formatted_panics) { | |
| 26984 | 27240 | return sema.addSafetyCheck(parent_block, src, ok, .inactive_union_field); |
| 26985 | 27241 | } |
| 26986 | 27242 | try sema.safetyCheckFormatted(parent_block, src, ok, "panicInactiveUnionField", &.{ active_tag, wanted_tag }); |
| ... | ... | @@ -26996,7 +27252,8 @@ fn panicSentinelMismatch( |
| 26996 | 27252 | sentinel_index: Air.Inst.Ref, |
| 26997 | 27253 | ) !void { |
| 26998 | 27254 | assert(!parent_block.is_comptime); |
| 26999 | const mod = sema.mod; | |
| 27255 | const pt = sema.pt; | |
| 27256 | const mod = pt.zcu; | |
| 27000 | 27257 | const expected_sentinel_val = maybe_sentinel orelse return; |
| 27001 | 27258 | const expected_sentinel = Air.internedToRef(expected_sentinel_val.toIntern()); |
| 27002 | 27259 | |
| ... | ... | @@ -27004,7 +27261,7 @@ fn panicSentinelMismatch( |
| 27004 | 27261 | const actual_sentinel = if (ptr_ty.isSlice(mod)) |
| 27005 | 27262 | try parent_block.addBinOp(.slice_elem_val, ptr, sentinel_index) |
| 27006 | 27263 | else blk: { |
| 27007 | const elem_ptr_ty = try ptr_ty.elemPtrType(null, mod); | |
| 27264 | const elem_ptr_ty = try ptr_ty.elemPtrType(null, pt); | |
| 27008 | 27265 | const sentinel_ptr = try parent_block.addPtrElemPtr(ptr, sentinel_index, elem_ptr_ty); |
| 27009 | 27266 | break :blk try parent_block.addTyOp(.load, sentinel_ty, sentinel_ptr); |
| 27010 | 27267 | }; |
| ... | ... | @@ -27022,13 +27279,13 @@ fn panicSentinelMismatch( |
| 27022 | 27279 | } else if (sentinel_ty.isSelfComparable(mod, true)) |
| 27023 | 27280 | try parent_block.addBinOp(.cmp_eq, expected_sentinel, actual_sentinel) |
| 27024 | 27281 | else { |
| 27025 | const panic_fn = try mod.getBuiltin("checkNonScalarSentinel"); | |
| 27282 | const panic_fn = try pt.getBuiltin("checkNonScalarSentinel"); | |
| 27026 | 27283 | const args: [2]Air.Inst.Ref = .{ expected_sentinel, actual_sentinel }; |
| 27027 | 27284 | try sema.callBuiltin(parent_block, src, panic_fn, .auto, &args, .@"safety check"); |
| 27028 | 27285 | return; |
| 27029 | 27286 | }; |
| 27030 | 27287 | |
| 27031 | if (!sema.mod.comp.formatted_panics) { | |
| 27288 | if (!pt.zcu.comp.formatted_panics) { | |
| 27032 | 27289 | return sema.addSafetyCheck(parent_block, src, ok, .sentinel_mismatch); |
| 27033 | 27290 | } |
| 27034 | 27291 | try sema.safetyCheckFormatted(parent_block, src, ok, "panicSentinelMismatch", &.{ expected_sentinel, actual_sentinel }); |
| ... | ... | @@ -27042,7 +27299,9 @@ fn safetyCheckFormatted( |
| 27042 | 27299 | func: []const u8, |
| 27043 | 27300 | args: []const Air.Inst.Ref, |
| 27044 | 27301 | ) CompileError!void { |
| 27045 | assert(sema.mod.comp.formatted_panics); | |
| 27302 | const pt = sema.pt; | |
| 27303 | const zcu = pt.zcu; | |
| 27304 | assert(zcu.comp.formatted_panics); | |
| 27046 | 27305 | const gpa = sema.gpa; |
| 27047 | 27306 | |
| 27048 | 27307 | var fail_block: Block = .{ |
| ... | ... | @@ -27058,10 +27317,10 @@ fn safetyCheckFormatted( |
| 27058 | 27317 | |
| 27059 | 27318 | defer fail_block.instructions.deinit(gpa); |
| 27060 | 27319 | |
| 27061 | if (!sema.mod.backendSupportsFeature(.safety_check_formatted)) { | |
| 27320 | if (!zcu.backendSupportsFeature(.safety_check_formatted)) { | |
| 27062 | 27321 | _ = try fail_block.addNoOp(.trap); |
| 27063 | 27322 | } else { |
| 27064 | const panic_fn = try sema.mod.getBuiltin(func); | |
| 27323 | const panic_fn = try pt.getBuiltin(func); | |
| 27065 | 27324 | try sema.callBuiltin(&fail_block, src, panic_fn, .auto, args, .@"safety check"); |
| 27066 | 27325 | } |
| 27067 | 27326 | try sema.addSafetyCheckExtra(parent_block, ok, &fail_block); |
| ... | ... | @@ -27102,7 +27361,8 @@ fn fieldVal( |
| 27102 | 27361 | // When editing this function, note that there is corresponding logic to be edited |
| 27103 | 27362 | // in `fieldPtr`. This function takes a value and returns a value. |
| 27104 | 27363 | |
| 27105 | const mod = sema.mod; | |
| 27364 | const pt = sema.pt; | |
| 27365 | const mod = pt.zcu; | |
| 27106 | 27366 | const ip = &mod.intern_pool; |
| 27107 | 27367 | const object_src = src; // TODO better source location |
| 27108 | 27368 | const object_ty = sema.typeOf(object); |
| ... | ... | @@ -27120,10 +27380,10 @@ fn fieldVal( |
| 27120 | 27380 | switch (inner_ty.zigTypeTag(mod)) { |
| 27121 | 27381 | .Array => { |
| 27122 | 27382 | if (field_name.eqlSlice("len", ip)) { |
| 27123 | return Air.internedToRef((try mod.intValue(Type.usize, inner_ty.arrayLen(mod))).toIntern()); | |
| 27383 | return Air.internedToRef((try pt.intValue(Type.usize, inner_ty.arrayLen(mod))).toIntern()); | |
| 27124 | 27384 | } else if (field_name.eqlSlice("ptr", ip) and is_pointer_to) { |
| 27125 | 27385 | const ptr_info = object_ty.ptrInfo(mod); |
| 27126 | const result_ty = try mod.ptrTypeSema(.{ | |
| 27386 | const result_ty = try pt.ptrTypeSema(.{ | |
| 27127 | 27387 | .child = Type.fromInterned(ptr_info.child).childType(mod).toIntern(), |
| 27128 | 27388 | .sentinel = if (inner_ty.sentinel(mod)) |s| s.toIntern() else .none, |
| 27129 | 27389 | .flags = .{ |
| ... | ... | @@ -27143,7 +27403,7 @@ fn fieldVal( |
| 27143 | 27403 | block, |
| 27144 | 27404 | field_name_src, |
| 27145 | 27405 | "no member named '{}' in '{}'", |
| 27146 | .{ field_name.fmt(ip), object_ty.fmt(mod) }, | |
| 27406 | .{ field_name.fmt(ip), object_ty.fmt(pt) }, | |
| 27147 | 27407 | ); |
| 27148 | 27408 | } |
| 27149 | 27409 | }, |
| ... | ... | @@ -27167,7 +27427,7 @@ fn fieldVal( |
| 27167 | 27427 | block, |
| 27168 | 27428 | field_name_src, |
| 27169 | 27429 | "no member named '{}' in '{}'", |
| 27170 | .{ field_name.fmt(ip), object_ty.fmt(mod) }, | |
| 27430 | .{ field_name.fmt(ip), object_ty.fmt(pt) }, | |
| 27171 | 27431 | ); |
| 27172 | 27432 | } |
| 27173 | 27433 | } |
| ... | ... | @@ -27194,7 +27454,7 @@ fn fieldVal( |
| 27194 | 27454 | .error_set_type => |error_set_type| blk: { |
| 27195 | 27455 | if (error_set_type.nameIndex(ip, field_name) != null) break :blk; |
| 27196 | 27456 | return sema.fail(block, src, "no error named '{}' in '{}'", .{ |
| 27197 | field_name.fmt(ip), child_type.fmt(mod), | |
| 27457 | field_name.fmt(ip), child_type.fmt(pt), | |
| 27198 | 27458 | }); |
| 27199 | 27459 | }, |
| 27200 | 27460 | .inferred_error_set_type => { |
| ... | ... | @@ -27210,8 +27470,8 @@ fn fieldVal( |
| 27210 | 27470 | const error_set_type = if (!child_type.isAnyError(mod)) |
| 27211 | 27471 | child_type |
| 27212 | 27472 | else |
| 27213 | try mod.singleErrorSetType(field_name); | |
| 27214 | return Air.internedToRef((try mod.intern(.{ .err = .{ | |
| 27473 | try pt.singleErrorSetType(field_name); | |
| 27474 | return Air.internedToRef((try pt.intern(.{ .err = .{ | |
| 27215 | 27475 | .ty = error_set_type.toIntern(), |
| 27216 | 27476 | .name = field_name, |
| 27217 | 27477 | } }))); |
| ... | ... | @@ -27220,11 +27480,11 @@ fn fieldVal( |
| 27220 | 27480 | if (try sema.namespaceLookupVal(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| { |
| 27221 | 27481 | return inst; |
| 27222 | 27482 | } |
| 27223 | try child_type.resolveFields(mod); | |
| 27483 | try child_type.resolveFields(pt); | |
| 27224 | 27484 | if (child_type.unionTagType(mod)) |enum_ty| { |
| 27225 | 27485 | if (enum_ty.enumFieldIndex(field_name, mod)) |field_index_usize| { |
| 27226 | 27486 | const field_index: u32 = @intCast(field_index_usize); |
| 27227 | return Air.internedToRef((try mod.enumValueFieldIndex(enum_ty, field_index)).toIntern()); | |
| 27487 | return Air.internedToRef((try pt.enumValueFieldIndex(enum_ty, field_index)).toIntern()); | |
| 27228 | 27488 | } |
| 27229 | 27489 | } |
| 27230 | 27490 | return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name); |
| ... | ... | @@ -27236,7 +27496,7 @@ fn fieldVal( |
| 27236 | 27496 | const field_index_usize = child_type.enumFieldIndex(field_name, mod) orelse |
| 27237 | 27497 | return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name); |
| 27238 | 27498 | const field_index: u32 = @intCast(field_index_usize); |
| 27239 | const enum_val = try mod.enumValueFieldIndex(child_type, field_index); | |
| 27499 | const enum_val = try pt.enumValueFieldIndex(child_type, field_index); | |
| 27240 | 27500 | return Air.internedToRef(enum_val.toIntern()); |
| 27241 | 27501 | }, |
| 27242 | 27502 | .Struct, .Opaque => { |
| ... | ... | @@ -27247,7 +27507,7 @@ fn fieldVal( |
| 27247 | 27507 | }, |
| 27248 | 27508 | else => { |
| 27249 | 27509 | const msg = msg: { |
| 27250 | const msg = try sema.errMsg(src, "type '{}' has no members", .{child_type.fmt(mod)}); | |
| 27510 | const msg = try sema.errMsg(src, "type '{}' has no members", .{child_type.fmt(pt)}); | |
| 27251 | 27511 | errdefer msg.destroy(sema.gpa); |
| 27252 | 27512 | if (child_type.isSlice(mod)) try sema.errNote(src, msg, "slice values have 'len' and 'ptr' members", .{}); |
| 27253 | 27513 | if (child_type.zigTypeTag(mod) == .Array) try sema.errNote(src, msg, "array values have 'len' member", .{}); |
| ... | ... | @@ -27288,13 +27548,14 @@ fn fieldPtr( |
| 27288 | 27548 | // When editing this function, note that there is corresponding logic to be edited |
| 27289 | 27549 | // in `fieldVal`. This function takes a pointer and returns a pointer. |
| 27290 | 27550 | |
| 27291 | const mod = sema.mod; | |
| 27551 | const pt = sema.pt; | |
| 27552 | const mod = pt.zcu; | |
| 27292 | 27553 | const ip = &mod.intern_pool; |
| 27293 | 27554 | const object_ptr_src = src; // TODO better source location |
| 27294 | 27555 | const object_ptr_ty = sema.typeOf(object_ptr); |
| 27295 | 27556 | const object_ty = switch (object_ptr_ty.zigTypeTag(mod)) { |
| 27296 | 27557 | .Pointer => object_ptr_ty.childType(mod), |
| 27297 | else => return sema.fail(block, object_ptr_src, "expected pointer, found '{}'", .{object_ptr_ty.fmt(mod)}), | |
| 27558 | else => return sema.fail(block, object_ptr_src, "expected pointer, found '{}'", .{object_ptr_ty.fmt(pt)}), | |
| 27298 | 27559 | }; |
| 27299 | 27560 | |
| 27300 | 27561 | // Zig allows dereferencing a single pointer during field lookup. Note that |
| ... | ... | @@ -27310,11 +27571,11 @@ fn fieldPtr( |
| 27310 | 27571 | switch (inner_ty.zigTypeTag(mod)) { |
| 27311 | 27572 | .Array => { |
| 27312 | 27573 | if (field_name.eqlSlice("len", ip)) { |
| 27313 | const int_val = try mod.intValue(Type.usize, inner_ty.arrayLen(mod)); | |
| 27574 | const int_val = try pt.intValue(Type.usize, inner_ty.arrayLen(mod)); | |
| 27314 | 27575 | return anonDeclRef(sema, int_val.toIntern()); |
| 27315 | 27576 | } else if (field_name.eqlSlice("ptr", ip) and is_pointer_to) { |
| 27316 | 27577 | const ptr_info = object_ty.ptrInfo(mod); |
| 27317 | const new_ptr_ty = try mod.ptrTypeSema(.{ | |
| 27578 | const new_ptr_ty = try pt.ptrTypeSema(.{ | |
| 27318 | 27579 | .child = Type.fromInterned(ptr_info.child).childType(mod).toIntern(), |
| 27319 | 27580 | .sentinel = if (object_ty.sentinel(mod)) |s| s.toIntern() else .none, |
| 27320 | 27581 | .flags = .{ |
| ... | ... | @@ -27329,7 +27590,7 @@ fn fieldPtr( |
| 27329 | 27590 | .packed_offset = ptr_info.packed_offset, |
| 27330 | 27591 | }); |
| 27331 | 27592 | const ptr_ptr_info = object_ptr_ty.ptrInfo(mod); |
| 27332 | const result_ty = try mod.ptrTypeSema(.{ | |
| 27593 | const result_ty = try pt.ptrTypeSema(.{ | |
| 27333 | 27594 | .child = new_ptr_ty.toIntern(), |
| 27334 | 27595 | .sentinel = if (object_ptr_ty.sentinel(mod)) |s| s.toIntern() else .none, |
| 27335 | 27596 | .flags = .{ |
| ... | ... | @@ -27348,7 +27609,7 @@ fn fieldPtr( |
| 27348 | 27609 | block, |
| 27349 | 27610 | field_name_src, |
| 27350 | 27611 | "no member named '{}' in '{}'", |
| 27351 | .{ field_name.fmt(ip), object_ty.fmt(mod) }, | |
| 27612 | .{ field_name.fmt(ip), object_ty.fmt(pt) }, | |
| 27352 | 27613 | ); |
| 27353 | 27614 | } |
| 27354 | 27615 | }, |
| ... | ... | @@ -27363,7 +27624,7 @@ fn fieldPtr( |
| 27363 | 27624 | if (field_name.eqlSlice("ptr", ip)) { |
| 27364 | 27625 | const slice_ptr_ty = inner_ty.slicePtrFieldType(mod); |
| 27365 | 27626 | |
| 27366 | const result_ty = try mod.ptrTypeSema(.{ | |
| 27627 | const result_ty = try pt.ptrTypeSema(.{ | |
| 27367 | 27628 | .child = slice_ptr_ty.toIntern(), |
| 27368 | 27629 | .flags = .{ |
| 27369 | 27630 | .is_const = !attr_ptr_ty.ptrIsMutable(mod), |
| ... | ... | @@ -27373,7 +27634,7 @@ fn fieldPtr( |
| 27373 | 27634 | }); |
| 27374 | 27635 | |
| 27375 | 27636 | if (try sema.resolveDefinedValue(block, object_ptr_src, inner_ptr)) |val| { |
| 27376 | return Air.internedToRef((try val.ptrField(Value.slice_ptr_index, mod)).toIntern()); | |
| 27637 | return Air.internedToRef((try val.ptrField(Value.slice_ptr_index, pt)).toIntern()); | |
| 27377 | 27638 | } |
| 27378 | 27639 | try sema.requireRuntimeBlock(block, src, null); |
| 27379 | 27640 | |
| ... | ... | @@ -27381,7 +27642,7 @@ fn fieldPtr( |
| 27381 | 27642 | try sema.checkKnownAllocPtr(block, inner_ptr, field_ptr); |
| 27382 | 27643 | return field_ptr; |
| 27383 | 27644 | } else if (field_name.eqlSlice("len", ip)) { |
| 27384 | const result_ty = try mod.ptrTypeSema(.{ | |
| 27645 | const result_ty = try pt.ptrTypeSema(.{ | |
| 27385 | 27646 | .child = .usize_type, |
| 27386 | 27647 | .flags = .{ |
| 27387 | 27648 | .is_const = !attr_ptr_ty.ptrIsMutable(mod), |
| ... | ... | @@ -27391,7 +27652,7 @@ fn fieldPtr( |
| 27391 | 27652 | }); |
| 27392 | 27653 | |
| 27393 | 27654 | if (try sema.resolveDefinedValue(block, object_ptr_src, inner_ptr)) |val| { |
| 27394 | return Air.internedToRef((try val.ptrField(Value.slice_len_index, mod)).toIntern()); | |
| 27655 | return Air.internedToRef((try val.ptrField(Value.slice_len_index, pt)).toIntern()); | |
| 27395 | 27656 | } |
| 27396 | 27657 | try sema.requireRuntimeBlock(block, src, null); |
| 27397 | 27658 | |
| ... | ... | @@ -27403,7 +27664,7 @@ fn fieldPtr( |
| 27403 | 27664 | block, |
| 27404 | 27665 | field_name_src, |
| 27405 | 27666 | "no member named '{}' in '{}'", |
| 27406 | .{ field_name.fmt(ip), object_ty.fmt(mod) }, | |
| 27667 | .{ field_name.fmt(ip), object_ty.fmt(pt) }, | |
| 27407 | 27668 | ); |
| 27408 | 27669 | } |
| 27409 | 27670 | }, |
| ... | ... | @@ -27433,7 +27694,7 @@ fn fieldPtr( |
| 27433 | 27694 | break :blk; |
| 27434 | 27695 | } |
| 27435 | 27696 | return sema.fail(block, src, "no error named '{}' in '{}'", .{ |
| 27436 | field_name.fmt(ip), child_type.fmt(mod), | |
| 27697 | field_name.fmt(ip), child_type.fmt(pt), | |
| 27437 | 27698 | }); |
| 27438 | 27699 | }, |
| 27439 | 27700 | .inferred_error_set_type => { |
| ... | ... | @@ -27449,8 +27710,8 @@ fn fieldPtr( |
| 27449 | 27710 | const error_set_type = if (!child_type.isAnyError(mod)) |
| 27450 | 27711 | child_type |
| 27451 | 27712 | else |
| 27452 | try mod.singleErrorSetType(field_name); | |
| 27453 | return anonDeclRef(sema, try mod.intern(.{ .err = .{ | |
| 27713 | try pt.singleErrorSetType(field_name); | |
| 27714 | return anonDeclRef(sema, try pt.intern(.{ .err = .{ | |
| 27454 | 27715 | .ty = error_set_type.toIntern(), |
| 27455 | 27716 | .name = field_name, |
| 27456 | 27717 | } })); |
| ... | ... | @@ -27459,11 +27720,11 @@ fn fieldPtr( |
| 27459 | 27720 | if (try sema.namespaceLookupRef(block, src, child_type.getNamespaceIndex(mod), field_name)) |inst| { |
| 27460 | 27721 | return inst; |
| 27461 | 27722 | } |
| 27462 | try child_type.resolveFields(mod); | |
| 27723 | try child_type.resolveFields(pt); | |
| 27463 | 27724 | if (child_type.unionTagType(mod)) |enum_ty| { |
| 27464 | 27725 | if (enum_ty.enumFieldIndex(field_name, mod)) |field_index| { |
| 27465 | 27726 | const field_index_u32: u32 = @intCast(field_index); |
| 27466 | const idx_val = try mod.enumValueFieldIndex(enum_ty, field_index_u32); | |
| 27727 | const idx_val = try pt.enumValueFieldIndex(enum_ty, field_index_u32); | |
| 27467 | 27728 | return anonDeclRef(sema, idx_val.toIntern()); |
| 27468 | 27729 | } |
| 27469 | 27730 | } |
| ... | ... | @@ -27477,7 +27738,7 @@ fn fieldPtr( |
| 27477 | 27738 | return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name); |
| 27478 | 27739 | }; |
| 27479 | 27740 | const field_index_u32: u32 = @intCast(field_index); |
| 27480 | const idx_val = try mod.enumValueFieldIndex(child_type, field_index_u32); | |
| 27741 | const idx_val = try pt.enumValueFieldIndex(child_type, field_index_u32); | |
| 27481 | 27742 | return anonDeclRef(sema, idx_val.toIntern()); |
| 27482 | 27743 | }, |
| 27483 | 27744 | .Struct, .Opaque => { |
| ... | ... | @@ -27486,7 +27747,7 @@ fn fieldPtr( |
| 27486 | 27747 | } |
| 27487 | 27748 | return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name); |
| 27488 | 27749 | }, |
| 27489 | else => return sema.fail(block, src, "type '{}' has no members", .{child_type.fmt(mod)}), | |
| 27750 | else => return sema.fail(block, src, "type '{}' has no members", .{child_type.fmt(pt)}), | |
| 27490 | 27751 | } |
| 27491 | 27752 | }, |
| 27492 | 27753 | .Struct => { |
| ... | ... | @@ -27533,14 +27794,15 @@ fn fieldCallBind( |
| 27533 | 27794 | // When editing this function, note that there is corresponding logic to be edited |
| 27534 | 27795 | // in `fieldVal`. This function takes a pointer and returns a pointer. |
| 27535 | 27796 | |
| 27536 | const mod = sema.mod; | |
| 27797 | const pt = sema.pt; | |
| 27798 | const mod = pt.zcu; | |
| 27537 | 27799 | const ip = &mod.intern_pool; |
| 27538 | 27800 | const raw_ptr_src = src; // TODO better source location |
| 27539 | 27801 | const raw_ptr_ty = sema.typeOf(raw_ptr); |
| 27540 | 27802 | const inner_ty = if (raw_ptr_ty.zigTypeTag(mod) == .Pointer and (raw_ptr_ty.ptrSize(mod) == .One or raw_ptr_ty.ptrSize(mod) == .C)) |
| 27541 | 27803 | raw_ptr_ty.childType(mod) |
| 27542 | 27804 | else |
| 27543 | return sema.fail(block, raw_ptr_src, "expected single pointer, found '{}'", .{raw_ptr_ty.fmt(mod)}); | |
| 27805 | return sema.fail(block, raw_ptr_src, "expected single pointer, found '{}'", .{raw_ptr_ty.fmt(pt)}); | |
| 27544 | 27806 | |
| 27545 | 27807 | // Optionally dereference a second pointer to get the concrete type. |
| 27546 | 27808 | const is_double_ptr = inner_ty.zigTypeTag(mod) == .Pointer and inner_ty.ptrSize(mod) == .One; |
| ... | ... | @@ -27554,7 +27816,7 @@ fn fieldCallBind( |
| 27554 | 27816 | find_field: { |
| 27555 | 27817 | switch (concrete_ty.zigTypeTag(mod)) { |
| 27556 | 27818 | .Struct => { |
| 27557 | try concrete_ty.resolveFields(mod); | |
| 27819 | try concrete_ty.resolveFields(pt); | |
| 27558 | 27820 | if (mod.typeToStruct(concrete_ty)) |struct_type| { |
| 27559 | 27821 | const field_index = struct_type.nameIndex(ip, field_name) orelse |
| 27560 | 27822 | break :find_field; |
| ... | ... | @@ -27563,7 +27825,7 @@ fn fieldCallBind( |
| 27563 | 27825 | return sema.finishFieldCallBind(block, src, ptr_ty, field_ty, field_index, object_ptr); |
| 27564 | 27826 | } else if (concrete_ty.isTuple(mod)) { |
| 27565 | 27827 | if (field_name.eqlSlice("len", ip)) { |
| 27566 | return .{ .direct = try mod.intRef(Type.usize, concrete_ty.structFieldCount(mod)) }; | |
| 27828 | return .{ .direct = try pt.intRef(Type.usize, concrete_ty.structFieldCount(mod)) }; | |
| 27567 | 27829 | } |
| 27568 | 27830 | if (field_name.toUnsigned(ip)) |field_index| { |
| 27569 | 27831 | if (field_index >= concrete_ty.structFieldCount(mod)) break :find_field; |
| ... | ... | @@ -27580,7 +27842,7 @@ fn fieldCallBind( |
| 27580 | 27842 | } |
| 27581 | 27843 | }, |
| 27582 | 27844 | .Union => { |
| 27583 | try concrete_ty.resolveFields(mod); | |
| 27845 | try concrete_ty.resolveFields(pt); | |
| 27584 | 27846 | const union_obj = mod.typeToUnion(concrete_ty).?; |
| 27585 | 27847 | _ = union_obj.loadTagType(ip).nameIndex(ip, field_name) orelse break :find_field; |
| 27586 | 27848 | const field_ptr = try unionFieldPtr(sema, block, src, object_ptr, field_name, field_name_src, concrete_ty, false); |
| ... | ... | @@ -27661,7 +27923,7 @@ fn fieldCallBind( |
| 27661 | 27923 | const msg = msg: { |
| 27662 | 27924 | const msg = try sema.errMsg(src, "no field or member function named '{}' in '{}'", .{ |
| 27663 | 27925 | field_name.fmt(ip), |
| 27664 | concrete_ty.fmt(mod), | |
| 27926 | concrete_ty.fmt(pt), | |
| 27665 | 27927 | }); |
| 27666 | 27928 | errdefer msg.destroy(sema.gpa); |
| 27667 | 27929 | try sema.addDeclaredHereNote(msg, concrete_ty); |
| ... | ... | @@ -27689,8 +27951,9 @@ fn finishFieldCallBind( |
| 27689 | 27951 | field_index: u32, |
| 27690 | 27952 | object_ptr: Air.Inst.Ref, |
| 27691 | 27953 | ) CompileError!ResolvedFieldCallee { |
| 27692 | const mod = sema.mod; | |
| 27693 | const ptr_field_ty = try mod.ptrTypeSema(.{ | |
| 27954 | const pt = sema.pt; | |
| 27955 | const mod = pt.zcu; | |
| 27956 | const ptr_field_ty = try pt.ptrTypeSema(.{ | |
| 27694 | 27957 | .child = field_ty.toIntern(), |
| 27695 | 27958 | .flags = .{ |
| 27696 | 27959 | .is_const = !ptr_ty.ptrIsMutable(mod), |
| ... | ... | @@ -27701,14 +27964,14 @@ fn finishFieldCallBind( |
| 27701 | 27964 | const container_ty = ptr_ty.childType(mod); |
| 27702 | 27965 | if (container_ty.zigTypeTag(mod) == .Struct) { |
| 27703 | 27966 | if (container_ty.structFieldIsComptime(field_index, mod)) { |
| 27704 | try container_ty.resolveStructFieldInits(mod); | |
| 27705 | const default_val = (try container_ty.structFieldValueComptime(mod, field_index)).?; | |
| 27967 | try container_ty.resolveStructFieldInits(pt); | |
| 27968 | const default_val = (try container_ty.structFieldValueComptime(pt, field_index)).?; | |
| 27706 | 27969 | return .{ .direct = Air.internedToRef(default_val.toIntern()) }; |
| 27707 | 27970 | } |
| 27708 | 27971 | } |
| 27709 | 27972 | |
| 27710 | 27973 | if (try sema.resolveDefinedValue(block, src, object_ptr)) |struct_ptr_val| { |
| 27711 | const ptr_val = try struct_ptr_val.ptrField(field_index, mod); | |
| 27974 | const ptr_val = try struct_ptr_val.ptrField(field_index, pt); | |
| 27712 | 27975 | const pointer = Air.internedToRef(ptr_val.toIntern()); |
| 27713 | 27976 | return .{ .direct = try sema.analyzeLoad(block, src, pointer, src) }; |
| 27714 | 27977 | } |
| ... | ... | @@ -27725,7 +27988,8 @@ fn namespaceLookup( |
| 27725 | 27988 | opt_namespace: InternPool.OptionalNamespaceIndex, |
| 27726 | 27989 | decl_name: InternPool.NullTerminatedString, |
| 27727 | 27990 | ) CompileError!?InternPool.DeclIndex { |
| 27728 | const mod = sema.mod; | |
| 27991 | const pt = sema.pt; | |
| 27992 | const mod = pt.zcu; | |
| 27729 | 27993 | const gpa = sema.gpa; |
| 27730 | 27994 | if (try sema.lookupInNamespace(block, src, opt_namespace, decl_name, true)) |decl_index| { |
| 27731 | 27995 | const decl = mod.declPtr(decl_index); |
| ... | ... | @@ -27780,16 +28044,17 @@ fn structFieldPtr( |
| 27780 | 28044 | struct_ty: Type, |
| 27781 | 28045 | initializing: bool, |
| 27782 | 28046 | ) CompileError!Air.Inst.Ref { |
| 27783 | const mod = sema.mod; | |
| 28047 | const pt = sema.pt; | |
| 28048 | const mod = pt.zcu; | |
| 27784 | 28049 | const ip = &mod.intern_pool; |
| 27785 | 28050 | assert(struct_ty.zigTypeTag(mod) == .Struct); |
| 27786 | 28051 | |
| 27787 | try struct_ty.resolveFields(mod); | |
| 27788 | try struct_ty.resolveLayout(mod); | |
| 28052 | try struct_ty.resolveFields(pt); | |
| 28053 | try struct_ty.resolveLayout(pt); | |
| 27789 | 28054 | |
| 27790 | 28055 | if (struct_ty.isTuple(mod)) { |
| 27791 | 28056 | if (field_name.eqlSlice("len", ip)) { |
| 27792 | const len_inst = try mod.intRef(Type.usize, struct_ty.structFieldCount(mod)); | |
| 28057 | const len_inst = try pt.intRef(Type.usize, struct_ty.structFieldCount(mod)); | |
| 27793 | 28058 | return sema.analyzeRef(block, src, len_inst); |
| 27794 | 28059 | } |
| 27795 | 28060 | const field_index = try sema.tupleFieldIndex(block, struct_ty, field_name, field_name_src); |
| ... | ... | @@ -27817,14 +28082,15 @@ fn structFieldPtrByIndex( |
| 27817 | 28082 | struct_ty: Type, |
| 27818 | 28083 | initializing: bool, |
| 27819 | 28084 | ) CompileError!Air.Inst.Ref { |
| 27820 | const mod = sema.mod; | |
| 28085 | const pt = sema.pt; | |
| 28086 | const mod = pt.zcu; | |
| 27821 | 28087 | const ip = &mod.intern_pool; |
| 27822 | 28088 | if (struct_ty.isAnonStruct(mod)) { |
| 27823 | 28089 | return sema.tupleFieldPtr(block, src, struct_ptr, field_src, field_index, initializing); |
| 27824 | 28090 | } |
| 27825 | 28091 | |
| 27826 | 28092 | if (try sema.resolveDefinedValue(block, src, struct_ptr)) |struct_ptr_val| { |
| 27827 | const val = try struct_ptr_val.ptrField(field_index, mod); | |
| 28093 | const val = try struct_ptr_val.ptrField(field_index, pt); | |
| 27828 | 28094 | return Air.internedToRef(val.toIntern()); |
| 27829 | 28095 | } |
| 27830 | 28096 | |
| ... | ... | @@ -27848,7 +28114,7 @@ fn structFieldPtrByIndex( |
| 27848 | 28114 | try sema.typeAbiAlignment(Type.fromInterned(struct_ptr_ty_info.child)); |
| 27849 | 28115 | |
| 27850 | 28116 | if (struct_type.layout == .@"packed") { |
| 27851 | switch (struct_ty.packedStructFieldPtrInfo(struct_ptr_ty, field_index, mod)) { | |
| 28117 | switch (struct_ty.packedStructFieldPtrInfo(struct_ptr_ty, field_index, pt)) { | |
| 27852 | 28118 | .bit_ptr => |packed_offset| { |
| 27853 | 28119 | ptr_ty_data.flags.alignment = parent_align; |
| 27854 | 28120 | ptr_ty_data.packed_offset = packed_offset; |
| ... | ... | @@ -27861,14 +28127,14 @@ fn structFieldPtrByIndex( |
| 27861 | 28127 | // For extern structs, field alignment might be bigger than type's |
| 27862 | 28128 | // natural alignment. Eg, in `extern struct { x: u32, y: u16 }` the |
| 27863 | 28129 | // second field is aligned as u32. |
| 27864 | const field_offset = struct_ty.structFieldOffset(field_index, mod); | |
| 28130 | const field_offset = struct_ty.structFieldOffset(field_index, pt); | |
| 27865 | 28131 | ptr_ty_data.flags.alignment = if (parent_align == .none) |
| 27866 | 28132 | .none |
| 27867 | 28133 | else |
| 27868 | 28134 | @enumFromInt(@min(@intFromEnum(parent_align), @ctz(field_offset))); |
| 27869 | 28135 | } else { |
| 27870 | 28136 | // Our alignment is capped at the field alignment. |
| 27871 | const field_align = try mod.structFieldAlignmentAdvanced( | |
| 28137 | const field_align = try pt.structFieldAlignmentAdvanced( | |
| 27872 | 28138 | struct_type.fieldAlign(ip, field_index), |
| 27873 | 28139 | Type.fromInterned(field_ty), |
| 27874 | 28140 | struct_type.layout, |
| ... | ... | @@ -27880,11 +28146,11 @@ fn structFieldPtrByIndex( |
| 27880 | 28146 | field_align.min(parent_align); |
| 27881 | 28147 | } |
| 27882 | 28148 | |
| 27883 | const ptr_field_ty = try mod.ptrTypeSema(ptr_ty_data); | |
| 28149 | const ptr_field_ty = try pt.ptrTypeSema(ptr_ty_data); | |
| 27884 | 28150 | |
| 27885 | 28151 | if (struct_type.fieldIsComptime(ip, field_index)) { |
| 27886 | try struct_ty.resolveStructFieldInits(mod); | |
| 27887 | const val = try mod.intern(.{ .ptr = .{ | |
| 28152 | try struct_ty.resolveStructFieldInits(pt); | |
| 28153 | const val = try pt.intern(.{ .ptr = .{ | |
| 27888 | 28154 | .ty = ptr_field_ty.toIntern(), |
| 27889 | 28155 | .base_addr = .{ .comptime_field = struct_type.field_inits.get(ip)[field_index] }, |
| 27890 | 28156 | .byte_offset = 0, |
| ... | ... | @@ -27905,11 +28171,12 @@ fn structFieldVal( |
| 27905 | 28171 | field_name_src: LazySrcLoc, |
| 27906 | 28172 | struct_ty: Type, |
| 27907 | 28173 | ) CompileError!Air.Inst.Ref { |
| 27908 | const mod = sema.mod; | |
| 28174 | const pt = sema.pt; | |
| 28175 | const mod = pt.zcu; | |
| 27909 | 28176 | const ip = &mod.intern_pool; |
| 27910 | 28177 | assert(struct_ty.zigTypeTag(mod) == .Struct); |
| 27911 | 28178 | |
| 27912 | try struct_ty.resolveFields(mod); | |
| 28179 | try struct_ty.resolveFields(pt); | |
| 27913 | 28180 | |
| 27914 | 28181 | switch (ip.indexToKey(struct_ty.toIntern())) { |
| 27915 | 28182 | .struct_type => { |
| ... | ... | @@ -27920,7 +28187,7 @@ fn structFieldVal( |
| 27920 | 28187 | const field_index = struct_type.nameIndex(ip, field_name) orelse |
| 27921 | 28188 | return sema.failWithBadStructFieldAccess(block, struct_ty, struct_type, field_name_src, field_name); |
| 27922 | 28189 | if (struct_type.fieldIsComptime(ip, field_index)) { |
| 27923 | try struct_ty.resolveStructFieldInits(mod); | |
| 28190 | try struct_ty.resolveStructFieldInits(pt); | |
| 27924 | 28191 | return Air.internedToRef(struct_type.field_inits.get(ip)[field_index]); |
| 27925 | 28192 | } |
| 27926 | 28193 | |
| ... | ... | @@ -27929,15 +28196,15 @@ fn structFieldVal( |
| 27929 | 28196 | return Air.internedToRef(field_val.toIntern()); |
| 27930 | 28197 | |
| 27931 | 28198 | if (try sema.resolveValue(struct_byval)) |struct_val| { |
| 27932 | if (struct_val.isUndef(mod)) return mod.undefRef(field_ty); | |
| 28199 | if (struct_val.isUndef(mod)) return pt.undefRef(field_ty); | |
| 27933 | 28200 | if ((try sema.typeHasOnePossibleValue(field_ty))) |opv| { |
| 27934 | 28201 | return Air.internedToRef(opv.toIntern()); |
| 27935 | 28202 | } |
| 27936 | return Air.internedToRef((try struct_val.fieldValue(mod, field_index)).toIntern()); | |
| 28203 | return Air.internedToRef((try struct_val.fieldValue(pt, field_index)).toIntern()); | |
| 27937 | 28204 | } |
| 27938 | 28205 | |
| 27939 | 28206 | try sema.requireRuntimeBlock(block, src, null); |
| 27940 | try field_ty.resolveLayout(mod); | |
| 28207 | try field_ty.resolveLayout(pt); | |
| 27941 | 28208 | return block.addStructFieldVal(struct_byval, field_index, field_ty); |
| 27942 | 28209 | }, |
| 27943 | 28210 | .anon_struct_type => |anon_struct| { |
| ... | ... | @@ -27961,9 +28228,10 @@ fn tupleFieldVal( |
| 27961 | 28228 | field_name_src: LazySrcLoc, |
| 27962 | 28229 | tuple_ty: Type, |
| 27963 | 28230 | ) CompileError!Air.Inst.Ref { |
| 27964 | const mod = sema.mod; | |
| 28231 | const pt = sema.pt; | |
| 28232 | const mod = pt.zcu; | |
| 27965 | 28233 | if (field_name.eqlSlice("len", &mod.intern_pool)) { |
| 27966 | return mod.intRef(Type.usize, tuple_ty.structFieldCount(mod)); | |
| 28234 | return pt.intRef(Type.usize, tuple_ty.structFieldCount(mod)); | |
| 27967 | 28235 | } |
| 27968 | 28236 | const field_index = try sema.tupleFieldIndex(block, tuple_ty, field_name, field_name_src); |
| 27969 | 28237 | return sema.tupleFieldValByIndex(block, src, tuple_byval, field_index, tuple_ty); |
| ... | ... | @@ -27977,18 +28245,18 @@ fn tupleFieldIndex( |
| 27977 | 28245 | field_name: InternPool.NullTerminatedString, |
| 27978 | 28246 | field_name_src: LazySrcLoc, |
| 27979 | 28247 | ) CompileError!u32 { |
| 27980 | const mod = sema.mod; | |
| 27981 | const ip = &mod.intern_pool; | |
| 28248 | const pt = sema.pt; | |
| 28249 | const ip = &pt.zcu.intern_pool; | |
| 27982 | 28250 | assert(!field_name.eqlSlice("len", ip)); |
| 27983 | 28251 | if (field_name.toUnsigned(ip)) |field_index| { |
| 27984 | if (field_index < tuple_ty.structFieldCount(mod)) return field_index; | |
| 28252 | if (field_index < tuple_ty.structFieldCount(pt.zcu)) return field_index; | |
| 27985 | 28253 | return sema.fail(block, field_name_src, "index '{}' out of bounds of tuple '{}'", .{ |
| 27986 | field_name.fmt(ip), tuple_ty.fmt(mod), | |
| 28254 | field_name.fmt(ip), tuple_ty.fmt(pt), | |
| 27987 | 28255 | }); |
| 27988 | 28256 | } |
| 27989 | 28257 | |
| 27990 | 28258 | return sema.fail(block, field_name_src, "no field named '{}' in tuple '{}'", .{ |
| 27991 | field_name.fmt(ip), tuple_ty.fmt(mod), | |
| 28259 | field_name.fmt(ip), tuple_ty.fmt(pt), | |
| 27992 | 28260 | }); |
| 27993 | 28261 | } |
| 27994 | 28262 | |
| ... | ... | @@ -28000,12 +28268,13 @@ fn tupleFieldValByIndex( |
| 28000 | 28268 | field_index: u32, |
| 28001 | 28269 | tuple_ty: Type, |
| 28002 | 28270 | ) CompileError!Air.Inst.Ref { |
| 28003 | const mod = sema.mod; | |
| 28271 | const pt = sema.pt; | |
| 28272 | const mod = pt.zcu; | |
| 28004 | 28273 | const field_ty = tuple_ty.structFieldType(field_index, mod); |
| 28005 | 28274 | |
| 28006 | 28275 | if (tuple_ty.structFieldIsComptime(field_index, mod)) |
| 28007 | try tuple_ty.resolveStructFieldInits(mod); | |
| 28008 | if (try tuple_ty.structFieldValueComptime(mod, field_index)) |default_value| { | |
| 28276 | try tuple_ty.resolveStructFieldInits(pt); | |
| 28277 | if (try tuple_ty.structFieldValueComptime(pt, field_index)) |default_value| { | |
| 28009 | 28278 | return Air.internedToRef(default_value.toIntern()); |
| 28010 | 28279 | } |
| 28011 | 28280 | |
| ... | ... | @@ -28014,9 +28283,9 @@ fn tupleFieldValByIndex( |
| 28014 | 28283 | return Air.internedToRef(opv.toIntern()); |
| 28015 | 28284 | } |
| 28016 | 28285 | return switch (mod.intern_pool.indexToKey(tuple_val.toIntern())) { |
| 28017 | .undef => mod.undefRef(field_ty), | |
| 28286 | .undef => pt.undefRef(field_ty), | |
| 28018 | 28287 | .aggregate => |aggregate| Air.internedToRef(switch (aggregate.storage) { |
| 28019 | .bytes => |bytes| try mod.intValue(Type.u8, bytes.at(field_index, &mod.intern_pool)), | |
| 28288 | .bytes => |bytes| try pt.intValue(Type.u8, bytes.at(field_index, &mod.intern_pool)), | |
| 28020 | 28289 | .elems => |elems| Value.fromInterned(elems[field_index]), |
| 28021 | 28290 | .repeated_elem => |elem| Value.fromInterned(elem), |
| 28022 | 28291 | }.toIntern()), |
| ... | ... | @@ -28025,7 +28294,7 @@ fn tupleFieldValByIndex( |
| 28025 | 28294 | } |
| 28026 | 28295 | |
| 28027 | 28296 | try sema.requireRuntimeBlock(block, src, null); |
| 28028 | try field_ty.resolveLayout(mod); | |
| 28297 | try field_ty.resolveLayout(pt); | |
| 28029 | 28298 | return block.addStructFieldVal(tuple_byval, field_index, field_ty); |
| 28030 | 28299 | } |
| 28031 | 28300 | |
| ... | ... | @@ -28039,18 +28308,19 @@ fn unionFieldPtr( |
| 28039 | 28308 | union_ty: Type, |
| 28040 | 28309 | initializing: bool, |
| 28041 | 28310 | ) CompileError!Air.Inst.Ref { |
| 28042 | const mod = sema.mod; | |
| 28311 | const pt = sema.pt; | |
| 28312 | const mod = pt.zcu; | |
| 28043 | 28313 | const ip = &mod.intern_pool; |
| 28044 | 28314 | |
| 28045 | 28315 | assert(union_ty.zigTypeTag(mod) == .Union); |
| 28046 | 28316 | |
| 28047 | 28317 | const union_ptr_ty = sema.typeOf(union_ptr); |
| 28048 | 28318 | const union_ptr_info = union_ptr_ty.ptrInfo(mod); |
| 28049 | try union_ty.resolveFields(mod); | |
| 28319 | try union_ty.resolveFields(pt); | |
| 28050 | 28320 | const union_obj = mod.typeToUnion(union_ty).?; |
| 28051 | 28321 | const field_index = try sema.unionFieldIndex(block, union_ty, field_name, field_name_src); |
| 28052 | 28322 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 28053 | const ptr_field_ty = try mod.ptrTypeSema(.{ | |
| 28323 | const ptr_field_ty = try pt.ptrTypeSema(.{ | |
| 28054 | 28324 | .child = field_ty.toIntern(), |
| 28055 | 28325 | .flags = .{ |
| 28056 | 28326 | .is_const = union_ptr_info.flags.is_const, |
| ... | ... | @@ -28061,7 +28331,7 @@ fn unionFieldPtr( |
| 28061 | 28331 | union_ptr_info.flags.alignment |
| 28062 | 28332 | else |
| 28063 | 28333 | try sema.typeAbiAlignment(union_ty); |
| 28064 | const field_align = try mod.unionFieldNormalAlignmentAdvanced(union_obj, field_index, .sema); | |
| 28334 | const field_align = try pt.unionFieldNormalAlignmentAdvanced(union_obj, field_index, .sema); | |
| 28065 | 28335 | break :blk union_align.min(field_align); |
| 28066 | 28336 | } else union_ptr_info.flags.alignment, |
| 28067 | 28337 | }, |
| ... | ... | @@ -28087,9 +28357,9 @@ fn unionFieldPtr( |
| 28087 | 28357 | switch (union_obj.getLayout(ip)) { |
| 28088 | 28358 | .auto => if (initializing) { |
| 28089 | 28359 | // Store to the union to initialize the tag. |
| 28090 | const field_tag = try mod.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), enum_field_index); | |
| 28360 | const field_tag = try pt.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), enum_field_index); | |
| 28091 | 28361 | const payload_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 28092 | const new_union_val = try mod.unionValue(union_ty, field_tag, try mod.undefValue(payload_ty)); | |
| 28362 | const new_union_val = try pt.unionValue(union_ty, field_tag, try pt.undefValue(payload_ty)); | |
| 28093 | 28363 | try sema.storePtrVal(block, src, union_ptr_val, new_union_val, union_ty); |
| 28094 | 28364 | } else { |
| 28095 | 28365 | const union_val = (try sema.pointerDeref(block, src, union_ptr_val, union_ptr_ty)) orelse |
| ... | ... | @@ -28098,7 +28368,7 @@ fn unionFieldPtr( |
| 28098 | 28368 | return sema.failWithUseOfUndef(block, src); |
| 28099 | 28369 | } |
| 28100 | 28370 | const un = ip.indexToKey(union_val.toIntern()).un; |
| 28101 | const field_tag = try mod.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), enum_field_index); | |
| 28371 | const field_tag = try pt.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), enum_field_index); | |
| 28102 | 28372 | const tag_matches = un.tag == field_tag.toIntern(); |
| 28103 | 28373 | if (!tag_matches) { |
| 28104 | 28374 | const msg = msg: { |
| ... | ... | @@ -28117,7 +28387,7 @@ fn unionFieldPtr( |
| 28117 | 28387 | }, |
| 28118 | 28388 | .@"packed", .@"extern" => {}, |
| 28119 | 28389 | } |
| 28120 | const field_ptr_val = try union_ptr_val.ptrField(field_index, mod); | |
| 28390 | const field_ptr_val = try union_ptr_val.ptrField(field_index, pt); | |
| 28121 | 28391 | return Air.internedToRef(field_ptr_val.toIntern()); |
| 28122 | 28392 | } |
| 28123 | 28393 | |
| ... | ... | @@ -28125,7 +28395,7 @@ fn unionFieldPtr( |
| 28125 | 28395 | if (!initializing and union_obj.getLayout(ip) == .auto and block.wantSafety() and |
| 28126 | 28396 | union_ty.unionTagTypeSafety(mod) != null and union_obj.field_types.len > 1) |
| 28127 | 28397 | { |
| 28128 | const wanted_tag_val = try mod.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), enum_field_index); | |
| 28398 | const wanted_tag_val = try pt.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), enum_field_index); | |
| 28129 | 28399 | const wanted_tag = Air.internedToRef(wanted_tag_val.toIntern()); |
| 28130 | 28400 | // TODO would it be better if get_union_tag supported pointers to unions? |
| 28131 | 28401 | const union_val = try block.addTyOp(.load, union_ty, union_ptr); |
| ... | ... | @@ -28148,21 +28418,22 @@ fn unionFieldVal( |
| 28148 | 28418 | field_name_src: LazySrcLoc, |
| 28149 | 28419 | union_ty: Type, |
| 28150 | 28420 | ) CompileError!Air.Inst.Ref { |
| 28151 | const zcu = sema.mod; | |
| 28421 | const pt = sema.pt; | |
| 28422 | const zcu = pt.zcu; | |
| 28152 | 28423 | const ip = &zcu.intern_pool; |
| 28153 | 28424 | assert(union_ty.zigTypeTag(zcu) == .Union); |
| 28154 | 28425 | |
| 28155 | try union_ty.resolveFields(zcu); | |
| 28426 | try union_ty.resolveFields(pt); | |
| 28156 | 28427 | const union_obj = zcu.typeToUnion(union_ty).?; |
| 28157 | 28428 | const field_index = try sema.unionFieldIndex(block, union_ty, field_name, field_name_src); |
| 28158 | 28429 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 28159 | 28430 | const enum_field_index: u32 = @intCast(Type.fromInterned(union_obj.enum_tag_ty).enumFieldIndex(field_name, zcu).?); |
| 28160 | 28431 | |
| 28161 | 28432 | if (try sema.resolveValue(union_byval)) |union_val| { |
| 28162 | if (union_val.isUndef(zcu)) return zcu.undefRef(field_ty); | |
| 28433 | if (union_val.isUndef(zcu)) return pt.undefRef(field_ty); | |
| 28163 | 28434 | |
| 28164 | 28435 | const un = ip.indexToKey(union_val.toIntern()).un; |
| 28165 | const field_tag = try zcu.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), enum_field_index); | |
| 28436 | const field_tag = try pt.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), enum_field_index); | |
| 28166 | 28437 | const tag_matches = un.tag == field_tag.toIntern(); |
| 28167 | 28438 | switch (union_obj.getLayout(ip)) { |
| 28168 | 28439 | .auto => { |
| ... | ... | @@ -28191,7 +28462,7 @@ fn unionFieldVal( |
| 28191 | 28462 | .@"packed" => if (tag_matches) { |
| 28192 | 28463 | // Fast path - no need to use bitcast logic. |
| 28193 | 28464 | return Air.internedToRef(un.val); |
| 28194 | } else if (try sema.bitCastVal(union_val, field_ty, 0, try union_ty.bitSizeAdvanced(zcu, .sema), 0)) |field_val| { | |
| 28465 | } else if (try sema.bitCastVal(union_val, field_ty, 0, try union_ty.bitSizeAdvanced(pt, .sema), 0)) |field_val| { | |
| 28195 | 28466 | return Air.internedToRef(field_val.toIntern()); |
| 28196 | 28467 | }, |
| 28197 | 28468 | } |
| ... | ... | @@ -28201,7 +28472,7 @@ fn unionFieldVal( |
| 28201 | 28472 | if (union_obj.getLayout(ip) == .auto and block.wantSafety() and |
| 28202 | 28473 | union_ty.unionTagTypeSafety(zcu) != null and union_obj.field_types.len > 1) |
| 28203 | 28474 | { |
| 28204 | const wanted_tag_val = try zcu.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), enum_field_index); | |
| 28475 | const wanted_tag_val = try pt.enumValueFieldIndex(Type.fromInterned(union_obj.enum_tag_ty), enum_field_index); | |
| 28205 | 28476 | const wanted_tag = Air.internedToRef(wanted_tag_val.toIntern()); |
| 28206 | 28477 | const active_tag = try block.addTyOp(.get_union_tag, Type.fromInterned(union_obj.enum_tag_ty), union_byval); |
| 28207 | 28478 | try sema.panicInactiveUnionField(block, src, active_tag, wanted_tag); |
| ... | ... | @@ -28210,7 +28481,7 @@ fn unionFieldVal( |
| 28210 | 28481 | _ = try block.addNoOp(.unreach); |
| 28211 | 28482 | return .unreachable_value; |
| 28212 | 28483 | } |
| 28213 | try field_ty.resolveLayout(zcu); | |
| 28484 | try field_ty.resolveLayout(pt); | |
| 28214 | 28485 | return block.addStructFieldVal(union_byval, field_index, field_ty); |
| 28215 | 28486 | } |
| 28216 | 28487 | |
| ... | ... | @@ -28224,13 +28495,14 @@ fn elemPtr( |
| 28224 | 28495 | init: bool, |
| 28225 | 28496 | oob_safety: bool, |
| 28226 | 28497 | ) CompileError!Air.Inst.Ref { |
| 28227 | const mod = sema.mod; | |
| 28498 | const pt = sema.pt; | |
| 28499 | const mod = pt.zcu; | |
| 28228 | 28500 | const indexable_ptr_src = src; // TODO better source location |
| 28229 | 28501 | const indexable_ptr_ty = sema.typeOf(indexable_ptr); |
| 28230 | 28502 | |
| 28231 | 28503 | const indexable_ty = switch (indexable_ptr_ty.zigTypeTag(mod)) { |
| 28232 | 28504 | .Pointer => indexable_ptr_ty.childType(mod), |
| 28233 | else => return sema.fail(block, indexable_ptr_src, "expected pointer, found '{}'", .{indexable_ptr_ty.fmt(mod)}), | |
| 28505 | else => return sema.fail(block, indexable_ptr_src, "expected pointer, found '{}'", .{indexable_ptr_ty.fmt(pt)}), | |
| 28234 | 28506 | }; |
| 28235 | 28507 | try checkIndexable(sema, block, src, indexable_ty); |
| 28236 | 28508 | |
| ... | ... | @@ -28241,7 +28513,7 @@ fn elemPtr( |
| 28241 | 28513 | const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{ |
| 28242 | 28514 | .needed_comptime_reason = "tuple field access index must be comptime-known", |
| 28243 | 28515 | }); |
| 28244 | const index: u32 = @intCast(try index_val.toUnsignedIntSema(mod)); | |
| 28516 | const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt)); | |
| 28245 | 28517 | break :blk try sema.tupleFieldPtr(block, src, indexable_ptr, elem_index_src, index, init); |
| 28246 | 28518 | }, |
| 28247 | 28519 | else => { |
| ... | ... | @@ -28267,7 +28539,8 @@ fn elemPtrOneLayerOnly( |
| 28267 | 28539 | ) CompileError!Air.Inst.Ref { |
| 28268 | 28540 | const indexable_src = src; // TODO better source location |
| 28269 | 28541 | const indexable_ty = sema.typeOf(indexable); |
| 28270 | const mod = sema.mod; | |
| 28542 | const pt = sema.pt; | |
| 28543 | const mod = pt.zcu; | |
| 28271 | 28544 | |
| 28272 | 28545 | try checkIndexable(sema, block, src, indexable_ty); |
| 28273 | 28546 | |
| ... | ... | @@ -28279,11 +28552,11 @@ fn elemPtrOneLayerOnly( |
| 28279 | 28552 | const runtime_src = rs: { |
| 28280 | 28553 | const ptr_val = maybe_ptr_val orelse break :rs indexable_src; |
| 28281 | 28554 | const index_val = maybe_index_val orelse break :rs elem_index_src; |
| 28282 | const index: usize = @intCast(try index_val.toUnsignedIntSema(mod)); | |
| 28283 | const elem_ptr = try ptr_val.ptrElem(index, mod); | |
| 28555 | const index: usize = @intCast(try index_val.toUnsignedIntSema(pt)); | |
| 28556 | const elem_ptr = try ptr_val.ptrElem(index, pt); | |
| 28284 | 28557 | return Air.internedToRef(elem_ptr.toIntern()); |
| 28285 | 28558 | }; |
| 28286 | const result_ty = try indexable_ty.elemPtrType(null, mod); | |
| 28559 | const result_ty = try indexable_ty.elemPtrType(null, pt); | |
| 28287 | 28560 | |
| 28288 | 28561 | try sema.requireRuntimeBlock(block, src, runtime_src); |
| 28289 | 28562 | return block.addPtrElemPtr(indexable, elem_index, result_ty); |
| ... | ... | @@ -28297,7 +28570,7 @@ fn elemPtrOneLayerOnly( |
| 28297 | 28570 | const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{ |
| 28298 | 28571 | .needed_comptime_reason = "tuple field access index must be comptime-known", |
| 28299 | 28572 | }); |
| 28300 | const index: u32 = @intCast(try index_val.toUnsignedIntSema(mod)); | |
| 28573 | const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt)); | |
| 28301 | 28574 | break :blk try sema.tupleFieldPtr(block, indexable_src, indexable, elem_index_src, index, false); |
| 28302 | 28575 | }, |
| 28303 | 28576 | else => unreachable, // Guaranteed by checkIndexable |
| ... | ... | @@ -28319,7 +28592,8 @@ fn elemVal( |
| 28319 | 28592 | ) CompileError!Air.Inst.Ref { |
| 28320 | 28593 | const indexable_src = src; // TODO better source location |
| 28321 | 28594 | const indexable_ty = sema.typeOf(indexable); |
| 28322 | const mod = sema.mod; | |
| 28595 | const pt = sema.pt; | |
| 28596 | const mod = pt.zcu; | |
| 28323 | 28597 | |
| 28324 | 28598 | try checkIndexable(sema, block, src, indexable_ty); |
| 28325 | 28599 | |
| ... | ... | @@ -28337,14 +28611,14 @@ fn elemVal( |
| 28337 | 28611 | const runtime_src = rs: { |
| 28338 | 28612 | const indexable_val = maybe_indexable_val orelse break :rs indexable_src; |
| 28339 | 28613 | const index_val = maybe_index_val orelse break :rs elem_index_src; |
| 28340 | const index: usize = @intCast(try index_val.toUnsignedIntSema(mod)); | |
| 28614 | const index: usize = @intCast(try index_val.toUnsignedIntSema(pt)); | |
| 28341 | 28615 | const elem_ty = indexable_ty.elemType2(mod); |
| 28342 | const many_ptr_ty = try mod.manyConstPtrType(elem_ty); | |
| 28343 | const many_ptr_val = try mod.getCoerced(indexable_val, many_ptr_ty); | |
| 28344 | const elem_ptr_ty = try mod.singleConstPtrType(elem_ty); | |
| 28345 | const elem_ptr_val = try many_ptr_val.ptrElem(index, mod); | |
| 28616 | const many_ptr_ty = try pt.manyConstPtrType(elem_ty); | |
| 28617 | const many_ptr_val = try pt.getCoerced(indexable_val, many_ptr_ty); | |
| 28618 | const elem_ptr_ty = try pt.singleConstPtrType(elem_ty); | |
| 28619 | const elem_ptr_val = try many_ptr_val.ptrElem(index, pt); | |
| 28346 | 28620 | if (try sema.pointerDeref(block, indexable_src, elem_ptr_val, elem_ptr_ty)) |elem_val| { |
| 28347 | return Air.internedToRef((try mod.getCoerced(elem_val, elem_ty)).toIntern()); | |
| 28621 | return Air.internedToRef((try pt.getCoerced(elem_val, elem_ty)).toIntern()); | |
| 28348 | 28622 | } |
| 28349 | 28623 | break :rs indexable_src; |
| 28350 | 28624 | }; |
| ... | ... | @@ -28358,7 +28632,7 @@ fn elemVal( |
| 28358 | 28632 | if (inner_ty.zigTypeTag(mod) != .Array) break :arr_sent; |
| 28359 | 28633 | const sentinel = inner_ty.sentinel(mod) orelse break :arr_sent; |
| 28360 | 28634 | const index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index) orelse break :arr_sent; |
| 28361 | const index = try sema.usizeCast(block, src, try index_val.toUnsignedIntSema(mod)); | |
| 28635 | const index = try sema.usizeCast(block, src, try index_val.toUnsignedIntSema(pt)); | |
| 28362 | 28636 | if (index != inner_ty.arrayLen(mod)) break :arr_sent; |
| 28363 | 28637 | return Air.internedToRef(sentinel.toIntern()); |
| 28364 | 28638 | } |
| ... | ... | @@ -28376,7 +28650,7 @@ fn elemVal( |
| 28376 | 28650 | const index_val = try sema.resolveConstDefinedValue(block, elem_index_src, elem_index, .{ |
| 28377 | 28651 | .needed_comptime_reason = "tuple field access index must be comptime-known", |
| 28378 | 28652 | }); |
| 28379 | const index: u32 = @intCast(try index_val.toUnsignedIntSema(mod)); | |
| 28653 | const index: u32 = @intCast(try index_val.toUnsignedIntSema(pt)); | |
| 28380 | 28654 | return sema.tupleField(block, indexable_src, indexable, elem_index_src, index); |
| 28381 | 28655 | }, |
| 28382 | 28656 | else => unreachable, |
| ... | ... | @@ -28391,13 +28665,12 @@ fn validateRuntimeElemAccess( |
| 28391 | 28665 | parent_ty: Type, |
| 28392 | 28666 | parent_src: LazySrcLoc, |
| 28393 | 28667 | ) CompileError!void { |
| 28394 | const mod = sema.mod; | |
| 28395 | 28668 | if (try sema.typeRequiresComptime(elem_ty)) { |
| 28396 | 28669 | const msg = msg: { |
| 28397 | 28670 | const msg = try sema.errMsg( |
| 28398 | 28671 | elem_index_src, |
| 28399 | 28672 | "values of type '{}' must be comptime-known, but index value is runtime-known", |
| 28400 | .{parent_ty.fmt(mod)}, | |
| 28673 | .{parent_ty.fmt(sema.pt)}, | |
| 28401 | 28674 | ); |
| 28402 | 28675 | errdefer msg.destroy(sema.gpa); |
| 28403 | 28676 | |
| ... | ... | @@ -28418,10 +28691,11 @@ fn tupleFieldPtr( |
| 28418 | 28691 | field_index: u32, |
| 28419 | 28692 | init: bool, |
| 28420 | 28693 | ) CompileError!Air.Inst.Ref { |
| 28421 | const mod = sema.mod; | |
| 28694 | const pt = sema.pt; | |
| 28695 | const mod = pt.zcu; | |
| 28422 | 28696 | const tuple_ptr_ty = sema.typeOf(tuple_ptr); |
| 28423 | 28697 | const tuple_ty = tuple_ptr_ty.childType(mod); |
| 28424 | try tuple_ty.resolveFields(mod); | |
| 28698 | try tuple_ty.resolveFields(pt); | |
| 28425 | 28699 | const field_count = tuple_ty.structFieldCount(mod); |
| 28426 | 28700 | |
| 28427 | 28701 | if (field_count == 0) { |
| ... | ... | @@ -28435,7 +28709,7 @@ fn tupleFieldPtr( |
| 28435 | 28709 | } |
| 28436 | 28710 | |
| 28437 | 28711 | const field_ty = tuple_ty.structFieldType(field_index, mod); |
| 28438 | const ptr_field_ty = try mod.ptrTypeSema(.{ | |
| 28712 | const ptr_field_ty = try pt.ptrTypeSema(.{ | |
| 28439 | 28713 | .child = field_ty.toIntern(), |
| 28440 | 28714 | .flags = .{ |
| 28441 | 28715 | .is_const = !tuple_ptr_ty.ptrIsMutable(mod), |
| ... | ... | @@ -28445,10 +28719,10 @@ fn tupleFieldPtr( |
| 28445 | 28719 | }); |
| 28446 | 28720 | |
| 28447 | 28721 | if (tuple_ty.structFieldIsComptime(field_index, mod)) |
| 28448 | try tuple_ty.resolveStructFieldInits(mod); | |
| 28722 | try tuple_ty.resolveStructFieldInits(pt); | |
| 28449 | 28723 | |
| 28450 | if (try tuple_ty.structFieldValueComptime(mod, field_index)) |default_val| { | |
| 28451 | return Air.internedToRef((try mod.intern(.{ .ptr = .{ | |
| 28724 | if (try tuple_ty.structFieldValueComptime(pt, field_index)) |default_val| { | |
| 28725 | return Air.internedToRef((try pt.intern(.{ .ptr = .{ | |
| 28452 | 28726 | .ty = ptr_field_ty.toIntern(), |
| 28453 | 28727 | .base_addr = .{ .comptime_field = default_val.toIntern() }, |
| 28454 | 28728 | .byte_offset = 0, |
| ... | ... | @@ -28456,7 +28730,7 @@ fn tupleFieldPtr( |
| 28456 | 28730 | } |
| 28457 | 28731 | |
| 28458 | 28732 | if (try sema.resolveValue(tuple_ptr)) |tuple_ptr_val| { |
| 28459 | const field_ptr_val = try tuple_ptr_val.ptrField(field_index, mod); | |
| 28733 | const field_ptr_val = try tuple_ptr_val.ptrField(field_index, pt); | |
| 28460 | 28734 | return Air.internedToRef(field_ptr_val.toIntern()); |
| 28461 | 28735 | } |
| 28462 | 28736 | |
| ... | ... | @@ -28476,9 +28750,10 @@ fn tupleField( |
| 28476 | 28750 | field_index_src: LazySrcLoc, |
| 28477 | 28751 | field_index: u32, |
| 28478 | 28752 | ) CompileError!Air.Inst.Ref { |
| 28479 | const mod = sema.mod; | |
| 28753 | const pt = sema.pt; | |
| 28754 | const mod = pt.zcu; | |
| 28480 | 28755 | const tuple_ty = sema.typeOf(tuple); |
| 28481 | try tuple_ty.resolveFields(mod); | |
| 28756 | try tuple_ty.resolveFields(pt); | |
| 28482 | 28757 | const field_count = tuple_ty.structFieldCount(mod); |
| 28483 | 28758 | |
| 28484 | 28759 | if (field_count == 0) { |
| ... | ... | @@ -28494,20 +28769,20 @@ fn tupleField( |
| 28494 | 28769 | const field_ty = tuple_ty.structFieldType(field_index, mod); |
| 28495 | 28770 | |
| 28496 | 28771 | if (tuple_ty.structFieldIsComptime(field_index, mod)) |
| 28497 | try tuple_ty.resolveStructFieldInits(mod); | |
| 28498 | if (try tuple_ty.structFieldValueComptime(mod, field_index)) |default_value| { | |
| 28772 | try tuple_ty.resolveStructFieldInits(pt); | |
| 28773 | if (try tuple_ty.structFieldValueComptime(pt, field_index)) |default_value| { | |
| 28499 | 28774 | return Air.internedToRef(default_value.toIntern()); // comptime field |
| 28500 | 28775 | } |
| 28501 | 28776 | |
| 28502 | 28777 | if (try sema.resolveValue(tuple)) |tuple_val| { |
| 28503 | if (tuple_val.isUndef(mod)) return mod.undefRef(field_ty); | |
| 28504 | return Air.internedToRef((try tuple_val.fieldValue(mod, field_index)).toIntern()); | |
| 28778 | if (tuple_val.isUndef(mod)) return pt.undefRef(field_ty); | |
| 28779 | return Air.internedToRef((try tuple_val.fieldValue(pt, field_index)).toIntern()); | |
| 28505 | 28780 | } |
| 28506 | 28781 | |
| 28507 | 28782 | try sema.validateRuntimeElemAccess(block, field_index_src, field_ty, tuple_ty, tuple_src); |
| 28508 | 28783 | |
| 28509 | 28784 | try sema.requireRuntimeBlock(block, tuple_src, null); |
| 28510 | try field_ty.resolveLayout(mod); | |
| 28785 | try field_ty.resolveLayout(pt); | |
| 28511 | 28786 | return block.addStructFieldVal(tuple, field_index, field_ty); |
| 28512 | 28787 | } |
| 28513 | 28788 | |
| ... | ... | @@ -28521,7 +28796,8 @@ fn elemValArray( |
| 28521 | 28796 | elem_index: Air.Inst.Ref, |
| 28522 | 28797 | oob_safety: bool, |
| 28523 | 28798 | ) CompileError!Air.Inst.Ref { |
| 28524 | const mod = sema.mod; | |
| 28799 | const pt = sema.pt; | |
| 28800 | const mod = pt.zcu; | |
| 28525 | 28801 | const array_ty = sema.typeOf(array); |
| 28526 | 28802 | const array_sent = array_ty.sentinel(mod); |
| 28527 | 28803 | const array_len = array_ty.arrayLen(mod); |
| ... | ... | @@ -28537,7 +28813,7 @@ fn elemValArray( |
| 28537 | 28813 | const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index); |
| 28538 | 28814 | |
| 28539 | 28815 | if (maybe_index_val) |index_val| { |
| 28540 | const index: usize = @intCast(try index_val.toUnsignedIntSema(mod)); | |
| 28816 | const index: usize = @intCast(try index_val.toUnsignedIntSema(pt)); | |
| 28541 | 28817 | if (array_sent) |s| { |
| 28542 | 28818 | if (index == array_len) { |
| 28543 | 28819 | return Air.internedToRef(s.toIntern()); |
| ... | ... | @@ -28550,11 +28826,11 @@ fn elemValArray( |
| 28550 | 28826 | } |
| 28551 | 28827 | if (maybe_undef_array_val) |array_val| { |
| 28552 | 28828 | if (array_val.isUndef(mod)) { |
| 28553 | return mod.undefRef(elem_ty); | |
| 28829 | return pt.undefRef(elem_ty); | |
| 28554 | 28830 | } |
| 28555 | 28831 | if (maybe_index_val) |index_val| { |
| 28556 | const index: usize = @intCast(try index_val.toUnsignedIntSema(mod)); | |
| 28557 | const elem_val = try array_val.elemValue(mod, index); | |
| 28832 | const index: usize = @intCast(try index_val.toUnsignedIntSema(pt)); | |
| 28833 | const elem_val = try array_val.elemValue(pt, index); | |
| 28558 | 28834 | return Air.internedToRef(elem_val.toIntern()); |
| 28559 | 28835 | } |
| 28560 | 28836 | } |
| ... | ... | @@ -28565,7 +28841,7 @@ fn elemValArray( |
| 28565 | 28841 | if (oob_safety and block.wantSafety()) { |
| 28566 | 28842 | // Runtime check is only needed if unable to comptime check |
| 28567 | 28843 | if (maybe_index_val == null) { |
| 28568 | const len_inst = try mod.intRef(Type.usize, array_len); | |
| 28844 | const len_inst = try pt.intRef(Type.usize, array_len); | |
| 28569 | 28845 | const cmp_op: Air.Inst.Tag = if (array_sent != null) .cmp_lte else .cmp_lt; |
| 28570 | 28846 | try sema.panicIndexOutOfBounds(block, src, elem_index, len_inst, cmp_op); |
| 28571 | 28847 | } |
| ... | ... | @@ -28589,7 +28865,8 @@ fn elemPtrArray( |
| 28589 | 28865 | init: bool, |
| 28590 | 28866 | oob_safety: bool, |
| 28591 | 28867 | ) CompileError!Air.Inst.Ref { |
| 28592 | const mod = sema.mod; | |
| 28868 | const pt = sema.pt; | |
| 28869 | const mod = pt.zcu; | |
| 28593 | 28870 | const array_ptr_ty = sema.typeOf(array_ptr); |
| 28594 | 28871 | const array_ty = array_ptr_ty.childType(mod); |
| 28595 | 28872 | const array_sent = array_ty.sentinel(mod) != null; |
| ... | ... | @@ -28603,7 +28880,7 @@ fn elemPtrArray( |
| 28603 | 28880 | const maybe_undef_array_ptr_val = try sema.resolveValue(array_ptr); |
| 28604 | 28881 | // The index must not be undefined since it can be out of bounds. |
| 28605 | 28882 | const offset: ?usize = if (try sema.resolveDefinedValue(block, elem_index_src, elem_index)) |index_val| o: { |
| 28606 | const index = try sema.usizeCast(block, elem_index_src, try index_val.toUnsignedIntSema(mod)); | |
| 28883 | const index = try sema.usizeCast(block, elem_index_src, try index_val.toUnsignedIntSema(pt)); | |
| 28607 | 28884 | if (index >= array_len_s) { |
| 28608 | 28885 | const sentinel_label: []const u8 = if (array_sent) " +1 (sentinel)" else ""; |
| 28609 | 28886 | return sema.fail(block, elem_index_src, "index {d} outside array of length {d}{s}", .{ index, array_len, sentinel_label }); |
| ... | ... | @@ -28611,14 +28888,14 @@ fn elemPtrArray( |
| 28611 | 28888 | break :o index; |
| 28612 | 28889 | } else null; |
| 28613 | 28890 | |
| 28614 | const elem_ptr_ty = try array_ptr_ty.elemPtrType(offset, mod); | |
| 28891 | const elem_ptr_ty = try array_ptr_ty.elemPtrType(offset, pt); | |
| 28615 | 28892 | |
| 28616 | 28893 | if (maybe_undef_array_ptr_val) |array_ptr_val| { |
| 28617 | 28894 | if (array_ptr_val.isUndef(mod)) { |
| 28618 | return mod.undefRef(elem_ptr_ty); | |
| 28895 | return pt.undefRef(elem_ptr_ty); | |
| 28619 | 28896 | } |
| 28620 | 28897 | if (offset) |index| { |
| 28621 | const elem_ptr = try array_ptr_val.ptrElem(index, mod); | |
| 28898 | const elem_ptr = try array_ptr_val.ptrElem(index, pt); | |
| 28622 | 28899 | return Air.internedToRef(elem_ptr.toIntern()); |
| 28623 | 28900 | } |
| 28624 | 28901 | } |
| ... | ... | @@ -28632,7 +28909,7 @@ fn elemPtrArray( |
| 28632 | 28909 | |
| 28633 | 28910 | // Runtime check is only needed if unable to comptime check. |
| 28634 | 28911 | if (oob_safety and block.wantSafety() and offset == null) { |
| 28635 | const len_inst = try mod.intRef(Type.usize, array_len); | |
| 28912 | const len_inst = try pt.intRef(Type.usize, array_len); | |
| 28636 | 28913 | const cmp_op: Air.Inst.Tag = if (array_sent) .cmp_lte else .cmp_lt; |
| 28637 | 28914 | try sema.panicIndexOutOfBounds(block, src, elem_index, len_inst, cmp_op); |
| 28638 | 28915 | } |
| ... | ... | @@ -28650,7 +28927,8 @@ fn elemValSlice( |
| 28650 | 28927 | elem_index: Air.Inst.Ref, |
| 28651 | 28928 | oob_safety: bool, |
| 28652 | 28929 | ) CompileError!Air.Inst.Ref { |
| 28653 | const mod = sema.mod; | |
| 28930 | const pt = sema.pt; | |
| 28931 | const mod = pt.zcu; | |
| 28654 | 28932 | const slice_ty = sema.typeOf(slice); |
| 28655 | 28933 | const slice_sent = slice_ty.sentinel(mod) != null; |
| 28656 | 28934 | const elem_ty = slice_ty.elemType2(mod); |
| ... | ... | @@ -28663,19 +28941,19 @@ fn elemValSlice( |
| 28663 | 28941 | |
| 28664 | 28942 | if (maybe_slice_val) |slice_val| { |
| 28665 | 28943 | runtime_src = elem_index_src; |
| 28666 | const slice_len = try slice_val.sliceLen(mod); | |
| 28944 | const slice_len = try slice_val.sliceLen(pt); | |
| 28667 | 28945 | const slice_len_s = slice_len + @intFromBool(slice_sent); |
| 28668 | 28946 | if (slice_len_s == 0) { |
| 28669 | 28947 | return sema.fail(block, slice_src, "indexing into empty slice is not allowed", .{}); |
| 28670 | 28948 | } |
| 28671 | 28949 | if (maybe_index_val) |index_val| { |
| 28672 | const index: usize = @intCast(try index_val.toUnsignedIntSema(mod)); | |
| 28950 | const index: usize = @intCast(try index_val.toUnsignedIntSema(pt)); | |
| 28673 | 28951 | if (index >= slice_len_s) { |
| 28674 | 28952 | const sentinel_label: []const u8 = if (slice_sent) " +1 (sentinel)" else ""; |
| 28675 | 28953 | return sema.fail(block, elem_index_src, "index {d} outside slice of length {d}{s}", .{ index, slice_len, sentinel_label }); |
| 28676 | 28954 | } |
| 28677 | const elem_ptr_ty = try slice_ty.elemPtrType(index, mod); | |
| 28678 | const elem_ptr_val = try slice_val.ptrElem(index, mod); | |
| 28955 | const elem_ptr_ty = try slice_ty.elemPtrType(index, pt); | |
| 28956 | const elem_ptr_val = try slice_val.ptrElem(index, pt); | |
| 28679 | 28957 | if (try sema.pointerDeref(block, slice_src, elem_ptr_val, elem_ptr_ty)) |elem_val| { |
| 28680 | 28958 | return Air.internedToRef(elem_val.toIntern()); |
| 28681 | 28959 | } |
| ... | ... | @@ -28688,7 +28966,7 @@ fn elemValSlice( |
| 28688 | 28966 | try sema.requireRuntimeBlock(block, src, runtime_src); |
| 28689 | 28967 | if (oob_safety and block.wantSafety()) { |
| 28690 | 28968 | const len_inst = if (maybe_slice_val) |slice_val| |
| 28691 | try mod.intRef(Type.usize, try slice_val.sliceLen(mod)) | |
| 28969 | try pt.intRef(Type.usize, try slice_val.sliceLen(pt)) | |
| 28692 | 28970 | else |
| 28693 | 28971 | try block.addTyOp(.slice_len, Type.usize, slice); |
| 28694 | 28972 | const cmp_op: Air.Inst.Tag = if (slice_sent) .cmp_lte else .cmp_lt; |
| ... | ... | @@ -28707,24 +28985,25 @@ fn elemPtrSlice( |
| 28707 | 28985 | elem_index: Air.Inst.Ref, |
| 28708 | 28986 | oob_safety: bool, |
| 28709 | 28987 | ) CompileError!Air.Inst.Ref { |
| 28710 | const mod = sema.mod; | |
| 28988 | const pt = sema.pt; | |
| 28989 | const mod = pt.zcu; | |
| 28711 | 28990 | const slice_ty = sema.typeOf(slice); |
| 28712 | 28991 | const slice_sent = slice_ty.sentinel(mod) != null; |
| 28713 | 28992 | |
| 28714 | 28993 | const maybe_undef_slice_val = try sema.resolveValue(slice); |
| 28715 | 28994 | // The index must not be undefined since it can be out of bounds. |
| 28716 | 28995 | const offset: ?usize = if (try sema.resolveDefinedValue(block, elem_index_src, elem_index)) |index_val| o: { |
| 28717 | const index = try sema.usizeCast(block, elem_index_src, try index_val.toUnsignedIntSema(mod)); | |
| 28996 | const index = try sema.usizeCast(block, elem_index_src, try index_val.toUnsignedIntSema(pt)); | |
| 28718 | 28997 | break :o index; |
| 28719 | 28998 | } else null; |
| 28720 | 28999 | |
| 28721 | const elem_ptr_ty = try slice_ty.elemPtrType(offset, mod); | |
| 29000 | const elem_ptr_ty = try slice_ty.elemPtrType(offset, pt); | |
| 28722 | 29001 | |
| 28723 | 29002 | if (maybe_undef_slice_val) |slice_val| { |
| 28724 | 29003 | if (slice_val.isUndef(mod)) { |
| 28725 | return mod.undefRef(elem_ptr_ty); | |
| 29004 | return pt.undefRef(elem_ptr_ty); | |
| 28726 | 29005 | } |
| 28727 | const slice_len = try slice_val.sliceLen(mod); | |
| 29006 | const slice_len = try slice_val.sliceLen(pt); | |
| 28728 | 29007 | const slice_len_s = slice_len + @intFromBool(slice_sent); |
| 28729 | 29008 | if (slice_len_s == 0) { |
| 28730 | 29009 | return sema.fail(block, slice_src, "indexing into empty slice is not allowed", .{}); |
| ... | ... | @@ -28734,7 +29013,7 @@ fn elemPtrSlice( |
| 28734 | 29013 | const sentinel_label: []const u8 = if (slice_sent) " +1 (sentinel)" else ""; |
| 28735 | 29014 | return sema.fail(block, elem_index_src, "index {d} outside slice of length {d}{s}", .{ index, slice_len, sentinel_label }); |
| 28736 | 29015 | } |
| 28737 | const elem_ptr_val = try slice_val.ptrElem(index, mod); | |
| 29016 | const elem_ptr_val = try slice_val.ptrElem(index, pt); | |
| 28738 | 29017 | return Air.internedToRef(elem_ptr_val.toIntern()); |
| 28739 | 29018 | } |
| 28740 | 29019 | } |
| ... | ... | @@ -28747,7 +29026,7 @@ fn elemPtrSlice( |
| 28747 | 29026 | const len_inst = len: { |
| 28748 | 29027 | if (maybe_undef_slice_val) |slice_val| |
| 28749 | 29028 | if (!slice_val.isUndef(mod)) |
| 28750 | break :len try mod.intRef(Type.usize, try slice_val.sliceLen(mod)); | |
| 29029 | break :len try pt.intRef(Type.usize, try slice_val.sliceLen(pt)); | |
| 28751 | 29030 | break :len try block.addTyOp(.slice_len, Type.usize, slice); |
| 28752 | 29031 | }; |
| 28753 | 29032 | const cmp_op: Air.Inst.Tag = if (slice_sent) .cmp_lte else .cmp_lt; |
| ... | ... | @@ -28810,11 +29089,12 @@ fn coerceExtra( |
| 28810 | 29089 | opts: CoerceOpts, |
| 28811 | 29090 | ) CoersionError!Air.Inst.Ref { |
| 28812 | 29091 | if (dest_ty.isGenericPoison()) return inst; |
| 28813 | const zcu = sema.mod; | |
| 29092 | const pt = sema.pt; | |
| 29093 | const zcu = pt.zcu; | |
| 28814 | 29094 | const dest_ty_src = inst_src; // TODO better source location |
| 28815 | try dest_ty.resolveFields(zcu); | |
| 29095 | try dest_ty.resolveFields(pt); | |
| 28816 | 29096 | const inst_ty = sema.typeOf(inst); |
| 28817 | try inst_ty.resolveFields(zcu); | |
| 29097 | try inst_ty.resolveFields(pt); | |
| 28818 | 29098 | const target = zcu.getTarget(); |
| 28819 | 29099 | // If the types are the same, we can return the operand. |
| 28820 | 29100 | if (dest_ty.eql(inst_ty, zcu)) |
| ... | ... | @@ -28838,12 +29118,12 @@ fn coerceExtra( |
| 28838 | 29118 | if (maybe_inst_val) |val| { |
| 28839 | 29119 | // undefined sets the optional bit also to undefined. |
| 28840 | 29120 | if (val.toIntern() == .undef) { |
| 28841 | return zcu.undefRef(dest_ty); | |
| 29121 | return pt.undefRef(dest_ty); | |
| 28842 | 29122 | } |
| 28843 | 29123 | |
| 28844 | 29124 | // null to ?T |
| 28845 | 29125 | if (val.toIntern() == .null_value) { |
| 28846 | return Air.internedToRef((try zcu.intern(.{ .opt = .{ | |
| 29126 | return Air.internedToRef((try pt.intern(.{ .opt = .{ | |
| 28847 | 29127 | .ty = dest_ty.toIntern(), |
| 28848 | 29128 | .val = .none, |
| 28849 | 29129 | } }))); |
| ... | ... | @@ -29018,7 +29298,7 @@ fn coerceExtra( |
| 29018 | 29298 | switch (dest_info.flags.size) { |
| 29019 | 29299 | // coercion to C pointer |
| 29020 | 29300 | .C => switch (inst_ty.zigTypeTag(zcu)) { |
| 29021 | .Null => return Air.internedToRef(try zcu.intern(.{ .ptr = .{ | |
| 29301 | .Null => return Air.internedToRef(try pt.intern(.{ .ptr = .{ | |
| 29022 | 29302 | .ty = dest_ty.toIntern(), |
| 29023 | 29303 | .base_addr = .int, |
| 29024 | 29304 | .byte_offset = 0, |
| ... | ... | @@ -29063,7 +29343,7 @@ fn coerceExtra( |
| 29063 | 29343 | if (inst_info.flags.size == .Slice) { |
| 29064 | 29344 | assert(dest_info.sentinel == .none); |
| 29065 | 29345 | if (inst_info.sentinel == .none or |
| 29066 | inst_info.sentinel != (try zcu.intValue(Type.fromInterned(inst_info.child), 0)).toIntern()) | |
| 29346 | inst_info.sentinel != (try pt.intValue(Type.fromInterned(inst_info.child), 0)).toIntern()) | |
| 29067 | 29347 | break :p; |
| 29068 | 29348 | |
| 29069 | 29349 | const slice_ptr = try sema.analyzeSlicePtr(block, inst_src, inst, inst_ty); |
| ... | ... | @@ -29112,7 +29392,7 @@ fn coerceExtra( |
| 29112 | 29392 | block, |
| 29113 | 29393 | inst_src, |
| 29114 | 29394 | "array literal requires address-of operator (&) to coerce to slice type '{}'", |
| 29115 | .{dest_ty.fmt(zcu)}, | |
| 29395 | .{dest_ty.fmt(pt)}, | |
| 29116 | 29396 | ); |
| 29117 | 29397 | } |
| 29118 | 29398 | |
| ... | ... | @@ -29123,10 +29403,10 @@ fn coerceExtra( |
| 29123 | 29403 | // empty tuple to zero-length slice |
| 29124 | 29404 | // note that this allows coercing to a mutable slice. |
| 29125 | 29405 | if (inst_child_ty.structFieldCount(zcu) == 0) { |
| 29126 | const align_val = try dest_ty.ptrAlignmentAdvanced(zcu, .sema); | |
| 29127 | return Air.internedToRef(try zcu.intern(.{ .slice = .{ | |
| 29406 | const align_val = try dest_ty.ptrAlignmentAdvanced(pt, .sema); | |
| 29407 | return Air.internedToRef(try pt.intern(.{ .slice = .{ | |
| 29128 | 29408 | .ty = dest_ty.toIntern(), |
| 29129 | .ptr = try zcu.intern(.{ .ptr = .{ | |
| 29409 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 29130 | 29410 | .ty = dest_ty.slicePtrFieldType(zcu).toIntern(), |
| 29131 | 29411 | .base_addr = .int, |
| 29132 | 29412 | .byte_offset = align_val.toByteUnits().?, |
| ... | ... | @@ -29138,7 +29418,7 @@ fn coerceExtra( |
| 29138 | 29418 | // pointer to tuple to slice |
| 29139 | 29419 | if (!dest_info.flags.is_const) { |
| 29140 | 29420 | const err_msg = err_msg: { |
| 29141 | const err_msg = try sema.errMsg(inst_src, "cannot cast pointer to tuple to '{}'", .{dest_ty.fmt(zcu)}); | |
| 29421 | const err_msg = try sema.errMsg(inst_src, "cannot cast pointer to tuple to '{}'", .{dest_ty.fmt(pt)}); | |
| 29142 | 29422 | errdefer err_msg.destroy(sema.gpa); |
| 29143 | 29423 | try sema.errNote(dest_ty_src, err_msg, "pointers to tuples can only coerce to constant pointers", .{}); |
| 29144 | 29424 | break :err_msg err_msg; |
| ... | ... | @@ -29194,12 +29474,12 @@ fn coerceExtra( |
| 29194 | 29474 | // comptime-known integer to other number |
| 29195 | 29475 | if (!(try sema.intFitsInType(val, dest_ty, null))) { |
| 29196 | 29476 | if (!opts.report_err) return error.NotCoercible; |
| 29197 | return sema.fail(block, inst_src, "type '{}' cannot represent integer value '{}'", .{ dest_ty.fmt(zcu), val.fmtValue(zcu, sema) }); | |
| 29477 | return sema.fail(block, inst_src, "type '{}' cannot represent integer value '{}'", .{ dest_ty.fmt(pt), val.fmtValue(pt, sema) }); | |
| 29198 | 29478 | } |
| 29199 | 29479 | return switch (zcu.intern_pool.indexToKey(val.toIntern())) { |
| 29200 | .undef => try zcu.undefRef(dest_ty), | |
| 29480 | .undef => try pt.undefRef(dest_ty), | |
| 29201 | 29481 | .int => |int| Air.internedToRef( |
| 29202 | try zcu.intern_pool.getCoercedInts(zcu.gpa, int, dest_ty.toIntern()), | |
| 29482 | try zcu.intern_pool.getCoercedInts(zcu.gpa, pt.tid, int, dest_ty.toIntern()), | |
| 29203 | 29483 | ), |
| 29204 | 29484 | else => unreachable, |
| 29205 | 29485 | }; |
| ... | ... | @@ -29228,18 +29508,18 @@ fn coerceExtra( |
| 29228 | 29508 | .Float, .ComptimeFloat => switch (inst_ty.zigTypeTag(zcu)) { |
| 29229 | 29509 | .ComptimeFloat => { |
| 29230 | 29510 | const val = try sema.resolveConstDefinedValue(block, LazySrcLoc.unneeded, inst, undefined); |
| 29231 | const result_val = try val.floatCast(dest_ty, zcu); | |
| 29511 | const result_val = try val.floatCast(dest_ty, pt); | |
| 29232 | 29512 | return Air.internedToRef(result_val.toIntern()); |
| 29233 | 29513 | }, |
| 29234 | 29514 | .Float => { |
| 29235 | 29515 | if (maybe_inst_val) |val| { |
| 29236 | const result_val = try val.floatCast(dest_ty, zcu); | |
| 29237 | if (!val.eql(try result_val.floatCast(inst_ty, zcu), inst_ty, zcu)) { | |
| 29516 | const result_val = try val.floatCast(dest_ty, pt); | |
| 29517 | if (!val.eql(try result_val.floatCast(inst_ty, pt), inst_ty, zcu)) { | |
| 29238 | 29518 | return sema.fail( |
| 29239 | 29519 | block, |
| 29240 | 29520 | inst_src, |
| 29241 | 29521 | "type '{}' cannot represent float value '{}'", |
| 29242 | .{ dest_ty.fmt(zcu), val.fmtValue(zcu, sema) }, | |
| 29522 | .{ dest_ty.fmt(pt), val.fmtValue(pt, sema) }, | |
| 29243 | 29523 | ); |
| 29244 | 29524 | } |
| 29245 | 29525 | return Air.internedToRef(result_val.toIntern()); |
| ... | ... | @@ -29268,7 +29548,7 @@ fn coerceExtra( |
| 29268 | 29548 | } |
| 29269 | 29549 | break :int; |
| 29270 | 29550 | }; |
| 29271 | const result_val = try val.floatFromIntAdvanced(sema.arena, inst_ty, dest_ty, zcu, .sema); | |
| 29551 | const result_val = try val.floatFromIntAdvanced(sema.arena, inst_ty, dest_ty, pt, .sema); | |
| 29272 | 29552 | // TODO implement this compile error |
| 29273 | 29553 | //const int_again_val = try result_val.intFromFloat(sema.arena, inst_ty); |
| 29274 | 29554 | //if (!int_again_val.eql(val, inst_ty, zcu)) { |
| ... | ... | @@ -29276,7 +29556,7 @@ fn coerceExtra( |
| 29276 | 29556 | // block, |
| 29277 | 29557 | // inst_src, |
| 29278 | 29558 | // "type '{}' cannot represent integer value '{}'", |
| 29279 | // .{ dest_ty.fmt(zcu), val }, | |
| 29559 | // .{ dest_ty.fmt(pt), val }, | |
| 29280 | 29560 | // ); |
| 29281 | 29561 | //} |
| 29282 | 29562 | return Air.internedToRef(result_val.toIntern()); |
| ... | ... | @@ -29290,10 +29570,10 @@ fn coerceExtra( |
| 29290 | 29570 | const string = zcu.intern_pool.indexToKey(val.toIntern()).enum_literal; |
| 29291 | 29571 | const field_index = dest_ty.enumFieldIndex(string, zcu) orelse { |
| 29292 | 29572 | return sema.fail(block, inst_src, "no field named '{}' in enum '{}'", .{ |
| 29293 | string.fmt(&zcu.intern_pool), dest_ty.fmt(zcu), | |
| 29573 | string.fmt(&zcu.intern_pool), dest_ty.fmt(pt), | |
| 29294 | 29574 | }); |
| 29295 | 29575 | }; |
| 29296 | return Air.internedToRef((try zcu.enumValueFieldIndex(dest_ty, @intCast(field_index))).toIntern()); | |
| 29576 | return Air.internedToRef((try pt.enumValueFieldIndex(dest_ty, @intCast(field_index))).toIntern()); | |
| 29297 | 29577 | }, |
| 29298 | 29578 | .Union => blk: { |
| 29299 | 29579 | // union to its own tag type |
| ... | ... | @@ -29308,12 +29588,12 @@ fn coerceExtra( |
| 29308 | 29588 | .ErrorUnion => eu: { |
| 29309 | 29589 | if (maybe_inst_val) |inst_val| { |
| 29310 | 29590 | switch (inst_val.toIntern()) { |
| 29311 | .undef => return zcu.undefRef(dest_ty), | |
| 29591 | .undef => return pt.undefRef(dest_ty), | |
| 29312 | 29592 | else => switch (zcu.intern_pool.indexToKey(inst_val.toIntern())) { |
| 29313 | 29593 | .error_union => |error_union| switch (error_union.val) { |
| 29314 | 29594 | .err_name => |err_name| { |
| 29315 | 29595 | const error_set_ty = inst_ty.errorUnionSet(zcu); |
| 29316 | const error_set_val = Air.internedToRef((try zcu.intern(.{ .err = .{ | |
| 29596 | const error_set_val = Air.internedToRef((try pt.intern(.{ .err = .{ | |
| 29317 | 29597 | .ty = error_set_ty.toIntern(), |
| 29318 | 29598 | .name = err_name, |
| 29319 | 29599 | } }))); |
| ... | ... | @@ -29370,7 +29650,7 @@ fn coerceExtra( |
| 29370 | 29650 | |
| 29371 | 29651 | if (dest_ty.sentinel(zcu)) |dest_sent| { |
| 29372 | 29652 | const src_sent = inst_ty.sentinel(zcu) orelse break :array_to_array; |
| 29373 | if (dest_sent.toIntern() != (try zcu.getCoerced(src_sent, dest_ty.childType(zcu))).toIntern()) { | |
| 29653 | if (dest_sent.toIntern() != (try pt.getCoerced(src_sent, dest_ty.childType(zcu))).toIntern()) { | |
| 29374 | 29654 | break :array_to_array; |
| 29375 | 29655 | } |
| 29376 | 29656 | } |
| ... | ... | @@ -29414,7 +29694,7 @@ fn coerceExtra( |
| 29414 | 29694 | // undefined to anything. We do this after the big switch above so that |
| 29415 | 29695 | // special logic has a chance to run first, such as `*[N]T` to `[]T` which |
| 29416 | 29696 | // should initialize the length field of the slice. |
| 29417 | if (maybe_inst_val) |val| if (val.toIntern() == .undef) return zcu.undefRef(dest_ty); | |
| 29697 | if (maybe_inst_val) |val| if (val.toIntern() == .undef) return pt.undefRef(dest_ty); | |
| 29418 | 29698 | |
| 29419 | 29699 | if (!opts.report_err) return error.NotCoercible; |
| 29420 | 29700 | |
| ... | ... | @@ -29434,7 +29714,7 @@ fn coerceExtra( |
| 29434 | 29714 | } |
| 29435 | 29715 | |
| 29436 | 29716 | const msg = msg: { |
| 29437 | const msg = try sema.errMsg(inst_src, "expected type '{}', found '{}'", .{ dest_ty.fmt(zcu), inst_ty.fmt(zcu) }); | |
| 29717 | const msg = try sema.errMsg(inst_src, "expected type '{}', found '{}'", .{ dest_ty.fmt(pt), inst_ty.fmt(pt) }); | |
| 29438 | 29718 | errdefer msg.destroy(sema.gpa); |
| 29439 | 29719 | |
| 29440 | 29720 | // E!T to T |
| ... | ... | @@ -29486,7 +29766,7 @@ fn coerceInMemory( |
| 29486 | 29766 | val: Value, |
| 29487 | 29767 | dst_ty: Type, |
| 29488 | 29768 | ) CompileError!Air.Inst.Ref { |
| 29489 | return Air.internedToRef((try sema.mod.getCoerced(val, dst_ty)).toIntern()); | |
| 29769 | return Air.internedToRef((try sema.pt.getCoerced(val, dst_ty)).toIntern()); | |
| 29490 | 29770 | } |
| 29491 | 29771 | |
| 29492 | 29772 | const InMemoryCoercionResult = union(enum) { |
| ... | ... | @@ -29607,7 +29887,7 @@ const InMemoryCoercionResult = union(enum) { |
| 29607 | 29887 | } |
| 29608 | 29888 | |
| 29609 | 29889 | fn report(res: *const InMemoryCoercionResult, sema: *Sema, src: LazySrcLoc, msg: *Module.ErrorMsg) !void { |
| 29610 | const mod = sema.mod; | |
| 29890 | const pt = sema.pt; | |
| 29611 | 29891 | var cur = res; |
| 29612 | 29892 | while (true) switch (cur.*) { |
| 29613 | 29893 | .ok => unreachable, |
| ... | ... | @@ -29624,7 +29904,7 @@ const InMemoryCoercionResult = union(enum) { |
| 29624 | 29904 | }, |
| 29625 | 29905 | .error_union_payload => |pair| { |
| 29626 | 29906 | try sema.errNote(src, msg, "error union payload '{}' cannot cast into error union payload '{}'", .{ |
| 29627 | pair.actual.fmt(mod), pair.wanted.fmt(mod), | |
| 29907 | pair.actual.fmt(pt), pair.wanted.fmt(pt), | |
| 29628 | 29908 | }); |
| 29629 | 29909 | cur = pair.child; |
| 29630 | 29910 | }, |
| ... | ... | @@ -29637,18 +29917,18 @@ const InMemoryCoercionResult = union(enum) { |
| 29637 | 29917 | .array_sentinel => |sentinel| { |
| 29638 | 29918 | if (sentinel.actual.toIntern() != .unreachable_value) { |
| 29639 | 29919 | try sema.errNote(src, msg, "array sentinel '{}' cannot cast into array sentinel '{}'", .{ |
| 29640 | sentinel.actual.fmtValue(mod, sema), sentinel.wanted.fmtValue(mod, sema), | |
| 29920 | sentinel.actual.fmtValue(pt, sema), sentinel.wanted.fmtValue(pt, sema), | |
| 29641 | 29921 | }); |
| 29642 | 29922 | } else { |
| 29643 | 29923 | try sema.errNote(src, msg, "destination array requires '{}' sentinel", .{ |
| 29644 | sentinel.wanted.fmtValue(mod, sema), | |
| 29924 | sentinel.wanted.fmtValue(pt, sema), | |
| 29645 | 29925 | }); |
| 29646 | 29926 | } |
| 29647 | 29927 | break; |
| 29648 | 29928 | }, |
| 29649 | 29929 | .array_elem => |pair| { |
| 29650 | 29930 | try sema.errNote(src, msg, "array element type '{}' cannot cast into array element type '{}'", .{ |
| 29651 | pair.actual.fmt(mod), pair.wanted.fmt(mod), | |
| 29931 | pair.actual.fmt(pt), pair.wanted.fmt(pt), | |
| 29652 | 29932 | }); |
| 29653 | 29933 | cur = pair.child; |
| 29654 | 29934 | }, |
| ... | ... | @@ -29660,19 +29940,19 @@ const InMemoryCoercionResult = union(enum) { |
| 29660 | 29940 | }, |
| 29661 | 29941 | .vector_elem => |pair| { |
| 29662 | 29942 | try sema.errNote(src, msg, "vector element type '{}' cannot cast into vector element type '{}'", .{ |
| 29663 | pair.actual.fmt(mod), pair.wanted.fmt(mod), | |
| 29943 | pair.actual.fmt(pt), pair.wanted.fmt(pt), | |
| 29664 | 29944 | }); |
| 29665 | 29945 | cur = pair.child; |
| 29666 | 29946 | }, |
| 29667 | 29947 | .optional_shape => |pair| { |
| 29668 | 29948 | try sema.errNote(src, msg, "optional type child '{}' cannot cast into optional type child '{}'", .{ |
| 29669 | pair.actual.optionalChild(mod).fmt(mod), pair.wanted.optionalChild(mod).fmt(mod), | |
| 29949 | pair.actual.optionalChild(pt.zcu).fmt(pt), pair.wanted.optionalChild(pt.zcu).fmt(pt), | |
| 29670 | 29950 | }); |
| 29671 | 29951 | break; |
| 29672 | 29952 | }, |
| 29673 | 29953 | .optional_child => |pair| { |
| 29674 | 29954 | try sema.errNote(src, msg, "optional type child '{}' cannot cast into optional type child '{}'", .{ |
| 29675 | pair.actual.fmt(mod), pair.wanted.fmt(mod), | |
| 29955 | pair.actual.fmt(pt), pair.wanted.fmt(pt), | |
| 29676 | 29956 | }); |
| 29677 | 29957 | cur = pair.child; |
| 29678 | 29958 | }, |
| ... | ... | @@ -29682,7 +29962,7 @@ const InMemoryCoercionResult = union(enum) { |
| 29682 | 29962 | }, |
| 29683 | 29963 | .missing_error => |missing_errors| { |
| 29684 | 29964 | for (missing_errors) |err| { |
| 29685 | try sema.errNote(src, msg, "'error.{}' not a member of destination error set", .{err.fmt(&mod.intern_pool)}); | |
| 29965 | try sema.errNote(src, msg, "'error.{}' not a member of destination error set", .{err.fmt(&pt.zcu.intern_pool)}); | |
| 29686 | 29966 | } |
| 29687 | 29967 | break; |
| 29688 | 29968 | }, |
| ... | ... | @@ -29736,7 +30016,7 @@ const InMemoryCoercionResult = union(enum) { |
| 29736 | 30016 | }, |
| 29737 | 30017 | .fn_param => |param| { |
| 29738 | 30018 | try sema.errNote(src, msg, "parameter {d} '{}' cannot cast into '{}'", .{ |
| 29739 | param.index, param.actual.fmt(mod), param.wanted.fmt(mod), | |
| 30019 | param.index, param.actual.fmt(pt), param.wanted.fmt(pt), | |
| 29740 | 30020 | }); |
| 29741 | 30021 | cur = param.child; |
| 29742 | 30022 | }, |
| ... | ... | @@ -29746,13 +30026,13 @@ const InMemoryCoercionResult = union(enum) { |
| 29746 | 30026 | }, |
| 29747 | 30027 | .fn_return_type => |pair| { |
| 29748 | 30028 | try sema.errNote(src, msg, "return type '{}' cannot cast into return type '{}'", .{ |
| 29749 | pair.actual.fmt(mod), pair.wanted.fmt(mod), | |
| 30029 | pair.actual.fmt(pt), pair.wanted.fmt(pt), | |
| 29750 | 30030 | }); |
| 29751 | 30031 | cur = pair.child; |
| 29752 | 30032 | }, |
| 29753 | 30033 | .ptr_child => |pair| { |
| 29754 | 30034 | try sema.errNote(src, msg, "pointer type child '{}' cannot cast into pointer type child '{}'", .{ |
| 29755 | pair.actual.fmt(mod), pair.wanted.fmt(mod), | |
| 30035 | pair.actual.fmt(pt), pair.wanted.fmt(pt), | |
| 29756 | 30036 | }); |
| 29757 | 30037 | cur = pair.child; |
| 29758 | 30038 | }, |
| ... | ... | @@ -29763,11 +30043,11 @@ const InMemoryCoercionResult = union(enum) { |
| 29763 | 30043 | .ptr_sentinel => |sentinel| { |
| 29764 | 30044 | if (sentinel.actual.toIntern() != .unreachable_value) { |
| 29765 | 30045 | try sema.errNote(src, msg, "pointer sentinel '{}' cannot cast into pointer sentinel '{}'", .{ |
| 29766 | sentinel.actual.fmtValue(mod, sema), sentinel.wanted.fmtValue(mod, sema), | |
| 30046 | sentinel.actual.fmtValue(pt, sema), sentinel.wanted.fmtValue(pt, sema), | |
| 29767 | 30047 | }); |
| 29768 | 30048 | } else { |
| 29769 | 30049 | try sema.errNote(src, msg, "destination pointer requires '{}' sentinel", .{ |
| 29770 | sentinel.wanted.fmtValue(mod, sema), | |
| 30050 | sentinel.wanted.fmtValue(pt, sema), | |
| 29771 | 30051 | }); |
| 29772 | 30052 | } |
| 29773 | 30053 | break; |
| ... | ... | @@ -29787,15 +30067,15 @@ const InMemoryCoercionResult = union(enum) { |
| 29787 | 30067 | break; |
| 29788 | 30068 | }, |
| 29789 | 30069 | .ptr_allowzero => |pair| { |
| 29790 | const wanted_allow_zero = pair.wanted.ptrAllowsZero(mod); | |
| 29791 | const actual_allow_zero = pair.actual.ptrAllowsZero(mod); | |
| 30070 | const wanted_allow_zero = pair.wanted.ptrAllowsZero(pt.zcu); | |
| 30071 | const actual_allow_zero = pair.actual.ptrAllowsZero(pt.zcu); | |
| 29792 | 30072 | if (actual_allow_zero and !wanted_allow_zero) { |
| 29793 | 30073 | try sema.errNote(src, msg, "'{}' could have null values which are illegal in type '{}'", .{ |
| 29794 | pair.actual.fmt(mod), pair.wanted.fmt(mod), | |
| 30074 | pair.actual.fmt(pt), pair.wanted.fmt(pt), | |
| 29795 | 30075 | }); |
| 29796 | 30076 | } else { |
| 29797 | 30077 | try sema.errNote(src, msg, "mutable '{}' allows illegal null values stored to type '{}'", .{ |
| 29798 | pair.actual.fmt(mod), pair.wanted.fmt(mod), | |
| 30078 | pair.actual.fmt(pt), pair.wanted.fmt(pt), | |
| 29799 | 30079 | }); |
| 29800 | 30080 | } |
| 29801 | 30081 | break; |
| ... | ... | @@ -29821,13 +30101,13 @@ const InMemoryCoercionResult = union(enum) { |
| 29821 | 30101 | }, |
| 29822 | 30102 | .double_ptr_to_anyopaque => |pair| { |
| 29823 | 30103 | try sema.errNote(src, msg, "cannot implicitly cast double pointer '{}' to anyopaque pointer '{}'", .{ |
| 29824 | pair.actual.fmt(mod), pair.wanted.fmt(mod), | |
| 30104 | pair.actual.fmt(pt), pair.wanted.fmt(pt), | |
| 29825 | 30105 | }); |
| 29826 | 30106 | break; |
| 29827 | 30107 | }, |
| 29828 | 30108 | .slice_to_anyopaque => |pair| { |
| 29829 | 30109 | try sema.errNote(src, msg, "cannot implicitly cast slice '{}' to anyopaque pointer '{}'", .{ |
| 29830 | pair.actual.fmt(mod), pair.wanted.fmt(mod), | |
| 30110 | pair.actual.fmt(pt), pair.wanted.fmt(pt), | |
| 29831 | 30111 | }); |
| 29832 | 30112 | try sema.errNote(src, msg, "consider using '.ptr'", .{}); |
| 29833 | 30113 | break; |
| ... | ... | @@ -29864,7 +30144,8 @@ pub fn coerceInMemoryAllowed( |
| 29864 | 30144 | dest_src: LazySrcLoc, |
| 29865 | 30145 | src_src: LazySrcLoc, |
| 29866 | 30146 | ) CompileError!InMemoryCoercionResult { |
| 29867 | const mod = sema.mod; | |
| 30147 | const pt = sema.pt; | |
| 30148 | const mod = pt.zcu; | |
| 29868 | 30149 | |
| 29869 | 30150 | if (dest_ty.eql(src_ty, mod)) |
| 29870 | 30151 | return .ok; |
| ... | ... | @@ -29968,7 +30249,7 @@ pub fn coerceInMemoryAllowed( |
| 29968 | 30249 | (src_info.sentinel != null and |
| 29969 | 30250 | dest_info.sentinel != null and |
| 29970 | 30251 | dest_info.sentinel.?.eql( |
| 29971 | try mod.getCoerced(src_info.sentinel.?, dest_info.elem_type), | |
| 30252 | try pt.getCoerced(src_info.sentinel.?, dest_info.elem_type), | |
| 29972 | 30253 | dest_info.elem_type, |
| 29973 | 30254 | mod, |
| 29974 | 30255 | )); |
| ... | ... | @@ -30045,8 +30326,8 @@ pub fn coerceInMemoryAllowed( |
| 30045 | 30326 | // The memory layout of @Vector(N, iM) is the same as the integer type i(N*M), |
| 30046 | 30327 | // that is to say, the padding bits are not in the same place as the array [N]iM. |
| 30047 | 30328 | // If there's no padding, the bitcast is possible. |
| 30048 | const elem_bit_size = dest_elem_ty.bitSize(mod); | |
| 30049 | const elem_abi_byte_size = dest_elem_ty.abiSize(mod); | |
| 30329 | const elem_bit_size = dest_elem_ty.bitSize(pt); | |
| 30330 | const elem_abi_byte_size = dest_elem_ty.abiSize(pt); | |
| 30050 | 30331 | if (elem_abi_byte_size * 8 == elem_bit_size) |
| 30051 | 30332 | return .ok; |
| 30052 | 30333 | } |
| ... | ... | @@ -30081,7 +30362,7 @@ pub fn coerceInMemoryAllowed( |
| 30081 | 30362 | const field_count = dest_ty.structFieldCount(mod); |
| 30082 | 30363 | for (0..field_count) |field_idx| { |
| 30083 | 30364 | if (dest_ty.structFieldIsComptime(field_idx, mod) != src_ty.structFieldIsComptime(field_idx, mod)) break :tuple; |
| 30084 | if (dest_ty.structFieldAlign(field_idx, mod) != src_ty.structFieldAlign(field_idx, mod)) break :tuple; | |
| 30365 | if (dest_ty.structFieldAlign(field_idx, pt) != src_ty.structFieldAlign(field_idx, pt)) break :tuple; | |
| 30085 | 30366 | const dest_field_ty = dest_ty.structFieldType(field_idx, mod); |
| 30086 | 30367 | const src_field_ty = src_ty.structFieldType(field_idx, mod); |
| 30087 | 30368 | const field = try sema.coerceInMemoryAllowed(block, dest_field_ty, src_field_ty, dest_is_mut, target, dest_src, src_src); |
| ... | ... | @@ -30104,7 +30385,8 @@ fn coerceInMemoryAllowedErrorSets( |
| 30104 | 30385 | dest_src: LazySrcLoc, |
| 30105 | 30386 | src_src: LazySrcLoc, |
| 30106 | 30387 | ) !InMemoryCoercionResult { |
| 30107 | const mod = sema.mod; | |
| 30388 | const pt = sema.pt; | |
| 30389 | const mod = pt.zcu; | |
| 30108 | 30390 | const gpa = sema.gpa; |
| 30109 | 30391 | const ip = &mod.intern_pool; |
| 30110 | 30392 | |
| ... | ... | @@ -30202,7 +30484,8 @@ fn coerceInMemoryAllowedFns( |
| 30202 | 30484 | dest_src: LazySrcLoc, |
| 30203 | 30485 | src_src: LazySrcLoc, |
| 30204 | 30486 | ) !InMemoryCoercionResult { |
| 30205 | const mod = sema.mod; | |
| 30487 | const pt = sema.pt; | |
| 30488 | const mod = pt.zcu; | |
| 30206 | 30489 | const ip = &mod.intern_pool; |
| 30207 | 30490 | |
| 30208 | 30491 | const dest_info = mod.typeToFunc(dest_ty).?; |
| ... | ... | @@ -30303,7 +30586,8 @@ fn coerceInMemoryAllowedPtrs( |
| 30303 | 30586 | dest_src: LazySrcLoc, |
| 30304 | 30587 | src_src: LazySrcLoc, |
| 30305 | 30588 | ) !InMemoryCoercionResult { |
| 30306 | const zcu = sema.mod; | |
| 30589 | const pt = sema.pt; | |
| 30590 | const zcu = pt.zcu; | |
| 30307 | 30591 | const dest_info = dest_ptr_ty.ptrInfo(zcu); |
| 30308 | 30592 | const src_info = src_ptr_ty.ptrInfo(zcu); |
| 30309 | 30593 | |
| ... | ... | @@ -30381,7 +30665,7 @@ fn coerceInMemoryAllowedPtrs( |
| 30381 | 30665 | |
| 30382 | 30666 | const ok_sent = dest_info.sentinel == .none or src_info.flags.size == .C or |
| 30383 | 30667 | (src_info.sentinel != .none and |
| 30384 | dest_info.sentinel == try zcu.intern_pool.getCoerced(sema.gpa, src_info.sentinel, dest_info.child)); | |
| 30668 | dest_info.sentinel == try zcu.intern_pool.getCoerced(sema.gpa, pt.tid, src_info.sentinel, dest_info.child)); | |
| 30385 | 30669 | if (!ok_sent) { |
| 30386 | 30670 | return InMemoryCoercionResult{ .ptr_sentinel = .{ |
| 30387 | 30671 | .actual = switch (src_info.sentinel) { |
| ... | ... | @@ -30432,7 +30716,8 @@ fn coerceVarArgParam( |
| 30432 | 30716 | ) !Air.Inst.Ref { |
| 30433 | 30717 | if (block.is_typeof) return inst; |
| 30434 | 30718 | |
| 30435 | const mod = sema.mod; | |
| 30719 | const pt = sema.pt; | |
| 30720 | const mod = pt.zcu; | |
| 30436 | 30721 | const uncasted_ty = sema.typeOf(inst); |
| 30437 | 30722 | const coerced = switch (uncasted_ty.zigTypeTag(mod)) { |
| 30438 | 30723 | // TODO consider casting to c_int/f64 if they fit |
| ... | ... | @@ -30449,9 +30734,9 @@ fn coerceVarArgParam( |
| 30449 | 30734 | }, |
| 30450 | 30735 | .Array => return sema.fail(block, inst_src, "arrays must be passed by reference to variadic function", .{}), |
| 30451 | 30736 | .Float => float: { |
| 30452 | const target = sema.mod.getTarget(); | |
| 30737 | const target = mod.getTarget(); | |
| 30453 | 30738 | const double_bits = target.c_type_bit_size(.double); |
| 30454 | const inst_bits = uncasted_ty.floatBits(sema.mod.getTarget()); | |
| 30739 | const inst_bits = uncasted_ty.floatBits(target); | |
| 30455 | 30740 | if (inst_bits >= double_bits) break :float inst; |
| 30456 | 30741 | switch (double_bits) { |
| 30457 | 30742 | 32 => break :float try sema.coerce(block, Type.f32, inst, inst_src), |
| ... | ... | @@ -30461,7 +30746,7 @@ fn coerceVarArgParam( |
| 30461 | 30746 | }, |
| 30462 | 30747 | else => if (uncasted_ty.isAbiInt(mod)) int: { |
| 30463 | 30748 | if (!try sema.validateExternType(uncasted_ty, .param_ty)) break :int inst; |
| 30464 | const target = sema.mod.getTarget(); | |
| 30749 | const target = mod.getTarget(); | |
| 30465 | 30750 | const uncasted_info = uncasted_ty.intInfo(mod); |
| 30466 | 30751 | if (uncasted_info.bits <= target.c_type_bit_size(switch (uncasted_info.signedness) { |
| 30467 | 30752 | .signed => .int, |
| ... | ... | @@ -30491,7 +30776,7 @@ fn coerceVarArgParam( |
| 30491 | 30776 | const coerced_ty = sema.typeOf(coerced); |
| 30492 | 30777 | if (!try sema.validateExternType(coerced_ty, .param_ty)) { |
| 30493 | 30778 | const msg = msg: { |
| 30494 | const msg = try sema.errMsg(inst_src, "cannot pass '{}' to variadic function", .{coerced_ty.fmt(sema.mod)}); | |
| 30779 | const msg = try sema.errMsg(inst_src, "cannot pass '{}' to variadic function", .{coerced_ty.fmt(pt)}); | |
| 30495 | 30780 | errdefer msg.destroy(sema.gpa); |
| 30496 | 30781 | |
| 30497 | 30782 | try sema.explainWhyTypeIsNotExtern(msg, inst_src, coerced_ty, .param_ty); |
| ... | ... | @@ -30526,7 +30811,8 @@ fn storePtr2( |
| 30526 | 30811 | operand_src: LazySrcLoc, |
| 30527 | 30812 | air_tag: Air.Inst.Tag, |
| 30528 | 30813 | ) CompileError!void { |
| 30529 | const mod = sema.mod; | |
| 30814 | const pt = sema.pt; | |
| 30815 | const mod = pt.zcu; | |
| 30530 | 30816 | const ptr_ty = sema.typeOf(ptr); |
| 30531 | 30817 | if (ptr_ty.isConstPtr(mod)) |
| 30532 | 30818 | return sema.fail(block, ptr_src, "cannot assign to constant", .{}); |
| ... | ... | @@ -30548,7 +30834,7 @@ fn storePtr2( |
| 30548 | 30834 | while (i < field_count) : (i += 1) { |
| 30549 | 30835 | const elem_src = operand_src; // TODO better source location |
| 30550 | 30836 | const elem = try sema.tupleField(block, operand_src, uncasted_operand, elem_src, i); |
| 30551 | const elem_index = try mod.intRef(Type.usize, i); | |
| 30837 | const elem_index = try pt.intRef(Type.usize, i); | |
| 30552 | 30838 | const elem_ptr = try sema.elemPtr(block, ptr_src, ptr, elem_index, elem_src, false, true); |
| 30553 | 30839 | try sema.storePtr2(block, src, elem_ptr, elem_src, elem, elem_src, .store); |
| 30554 | 30840 | } |
| ... | ... | @@ -30620,7 +30906,7 @@ fn storePtr2( |
| 30620 | 30906 | return; |
| 30621 | 30907 | } |
| 30622 | 30908 | return sema.fail(block, ptr_src, "unable to determine vector element index of type '{}'", .{ |
| 30623 | ptr_ty.fmt(sema.mod), | |
| 30909 | ptr_ty.fmt(pt), | |
| 30624 | 30910 | }); |
| 30625 | 30911 | } |
| 30626 | 30912 | |
| ... | ... | @@ -30734,7 +31020,8 @@ fn markMaybeComptimeAllocRuntime(sema: *Sema, block: *Block, alloc_inst: Air.Ins |
| 30734 | 31020 | /// pointer. Only if the final element type matches the vector element type, and the |
| 30735 | 31021 | /// lengths match. |
| 30736 | 31022 | fn obtainBitCastedVectorPtr(sema: *Sema, ptr: Air.Inst.Ref) ?Air.Inst.Ref { |
| 30737 | const mod = sema.mod; | |
| 31023 | const pt = sema.pt; | |
| 31024 | const mod = pt.zcu; | |
| 30738 | 31025 | const array_ty = sema.typeOf(ptr).childType(mod); |
| 30739 | 31026 | if (array_ty.zigTypeTag(mod) != .Array) return null; |
| 30740 | 31027 | var ptr_ref = ptr; |
| ... | ... | @@ -30751,7 +31038,7 @@ fn obtainBitCastedVectorPtr(sema: *Sema, ptr: Air.Inst.Ref) ?Air.Inst.Ref { |
| 30751 | 31038 | |
| 30752 | 31039 | // We have a pointer-to-array and a pointer-to-vector. If the elements and |
| 30753 | 31040 | // lengths match, return the result. |
| 30754 | if (array_ty.childType(mod).eql(vector_ty.childType(mod), sema.mod) and | |
| 31041 | if (array_ty.childType(mod).eql(vector_ty.childType(mod), mod) and | |
| 30755 | 31042 | array_ty.arrayLen(mod) == vector_ty.vectorLen(mod)) |
| 30756 | 31043 | { |
| 30757 | 31044 | return ptr_ref; |
| ... | ... | @@ -30770,17 +31057,18 @@ fn storePtrVal( |
| 30770 | 31057 | operand_val: Value, |
| 30771 | 31058 | operand_ty: Type, |
| 30772 | 31059 | ) !void { |
| 30773 | const zcu = sema.mod; | |
| 31060 | const pt = sema.pt; | |
| 31061 | const zcu = pt.zcu; | |
| 30774 | 31062 | const ip = &zcu.intern_pool; |
| 30775 | 31063 | // TODO: audit use sites to eliminate this coercion |
| 30776 | const coerced_operand_val = try zcu.getCoerced(operand_val, operand_ty); | |
| 31064 | const coerced_operand_val = try pt.getCoerced(operand_val, operand_ty); | |
| 30777 | 31065 | // TODO: audit use sites to eliminate this coercion |
| 30778 | const ptr_ty = try zcu.ptrType(info: { | |
| 31066 | const ptr_ty = try pt.ptrType(info: { | |
| 30779 | 31067 | var info = ptr_val.typeOf(zcu).ptrInfo(zcu); |
| 30780 | 31068 | info.child = operand_ty.toIntern(); |
| 30781 | 31069 | break :info info; |
| 30782 | 31070 | }); |
| 30783 | const coerced_ptr_val = try zcu.getCoerced(ptr_val, ptr_ty); | |
| 31071 | const coerced_ptr_val = try pt.getCoerced(ptr_val, ptr_ty); | |
| 30784 | 31072 | |
| 30785 | 31073 | switch (try sema.storeComptimePtr(block, src, coerced_ptr_val, coerced_operand_val)) { |
| 30786 | 31074 | .success => {}, |
| ... | ... | @@ -30800,13 +31088,13 @@ fn storePtrVal( |
| 30800 | 31088 | block, |
| 30801 | 31089 | src, |
| 30802 | 31090 | "comptime dereference requires '{}' to have a well-defined layout", |
| 30803 | .{ty.fmt(zcu)}, | |
| 31091 | .{ty.fmt(pt)}, | |
| 30804 | 31092 | ), |
| 30805 | 31093 | .out_of_bounds => |ty| return sema.fail( |
| 30806 | 31094 | block, |
| 30807 | 31095 | src, |
| 30808 | 31096 | "dereference of '{}' exceeds bounds of containing decl of type '{}'", |
| 30809 | .{ ptr_ty.fmt(zcu), ty.fmt(zcu) }, | |
| 31097 | .{ ptr_ty.fmt(pt), ty.fmt(pt) }, | |
| 30810 | 31098 | ), |
| 30811 | 31099 | .exceeds_host_size => return sema.fail(block, src, "bit-pointer target exceeds host size", .{}), |
| 30812 | 31100 | } |
| ... | ... | @@ -30820,31 +31108,32 @@ fn bitCast( |
| 30820 | 31108 | inst_src: LazySrcLoc, |
| 30821 | 31109 | operand_src: ?LazySrcLoc, |
| 30822 | 31110 | ) CompileError!Air.Inst.Ref { |
| 30823 | const zcu = sema.mod; | |
| 30824 | try dest_ty.resolveLayout(zcu); | |
| 31111 | const pt = sema.pt; | |
| 31112 | const zcu = pt.zcu; | |
| 31113 | try dest_ty.resolveLayout(pt); | |
| 30825 | 31114 | |
| 30826 | 31115 | const old_ty = sema.typeOf(inst); |
| 30827 | try old_ty.resolveLayout(zcu); | |
| 31116 | try old_ty.resolveLayout(pt); | |
| 30828 | 31117 | |
| 30829 | const dest_bits = dest_ty.bitSize(zcu); | |
| 30830 | const old_bits = old_ty.bitSize(zcu); | |
| 31118 | const dest_bits = dest_ty.bitSize(pt); | |
| 31119 | const old_bits = old_ty.bitSize(pt); | |
| 30831 | 31120 | |
| 30832 | 31121 | if (old_bits != dest_bits) { |
| 30833 | 31122 | return sema.fail(block, inst_src, "@bitCast size mismatch: destination type '{}' has {d} bits but source type '{}' has {d} bits", .{ |
| 30834 | dest_ty.fmt(zcu), | |
| 31123 | dest_ty.fmt(pt), | |
| 30835 | 31124 | dest_bits, |
| 30836 | old_ty.fmt(zcu), | |
| 31125 | old_ty.fmt(pt), | |
| 30837 | 31126 | old_bits, |
| 30838 | 31127 | }); |
| 30839 | 31128 | } |
| 30840 | 31129 | |
| 30841 | 31130 | if (try sema.resolveValue(inst)) |val| { |
| 30842 | 31131 | if (val.isUndef(zcu)) |
| 30843 | return zcu.undefRef(dest_ty); | |
| 31132 | return pt.undefRef(dest_ty); | |
| 30844 | 31133 | if (old_ty.zigTypeTag(zcu) == .ErrorSet and dest_ty.zigTypeTag(zcu) == .ErrorSet) { |
| 30845 | 31134 | // Special case: we sometimes call `bitCast` on error set values, but they |
| 30846 | 31135 | // don't have a well-defined layout, so we can't use `bitCastVal` on them. |
| 30847 | return Air.internedToRef((try zcu.getCoerced(val, dest_ty)).toIntern()); | |
| 31136 | return Air.internedToRef((try pt.getCoerced(val, dest_ty)).toIntern()); | |
| 30848 | 31137 | } |
| 30849 | 31138 | if (try sema.bitCastVal(val, dest_ty, 0, 0, 0)) |result_val| { |
| 30850 | 31139 | return Air.internedToRef(result_val.toIntern()); |
| ... | ... | @@ -30862,16 +31151,17 @@ fn coerceArrayPtrToSlice( |
| 30862 | 31151 | inst: Air.Inst.Ref, |
| 30863 | 31152 | inst_src: LazySrcLoc, |
| 30864 | 31153 | ) CompileError!Air.Inst.Ref { |
| 30865 | const mod = sema.mod; | |
| 31154 | const pt = sema.pt; | |
| 31155 | const mod = pt.zcu; | |
| 30866 | 31156 | if (try sema.resolveValue(inst)) |val| { |
| 30867 | 31157 | const ptr_array_ty = sema.typeOf(inst); |
| 30868 | 31158 | const array_ty = ptr_array_ty.childType(mod); |
| 30869 | 31159 | const slice_ptr_ty = dest_ty.slicePtrFieldType(mod); |
| 30870 | const slice_ptr = try mod.getCoerced(val, slice_ptr_ty); | |
| 30871 | const slice_val = try mod.intern(.{ .slice = .{ | |
| 31160 | const slice_ptr = try pt.getCoerced(val, slice_ptr_ty); | |
| 31161 | const slice_val = try pt.intern(.{ .slice = .{ | |
| 30872 | 31162 | .ty = dest_ty.toIntern(), |
| 30873 | 31163 | .ptr = slice_ptr.toIntern(), |
| 30874 | .len = (try mod.intValue(Type.usize, array_ty.arrayLen(mod))).toIntern(), | |
| 31164 | .len = (try pt.intValue(Type.usize, array_ty.arrayLen(mod))).toIntern(), | |
| 30875 | 31165 | } }); |
| 30876 | 31166 | return Air.internedToRef(slice_val); |
| 30877 | 31167 | } |
| ... | ... | @@ -30880,7 +31170,8 @@ fn coerceArrayPtrToSlice( |
| 30880 | 31170 | } |
| 30881 | 31171 | |
| 30882 | 31172 | fn checkPtrAttributes(sema: *Sema, dest_ty: Type, inst_ty: Type, in_memory_result: *InMemoryCoercionResult) bool { |
| 30883 | const mod = sema.mod; | |
| 31173 | const pt = sema.pt; | |
| 31174 | const mod = pt.zcu; | |
| 30884 | 31175 | const dest_info = dest_ty.ptrInfo(mod); |
| 30885 | 31176 | const inst_info = inst_ty.ptrInfo(mod); |
| 30886 | 31177 | const len0 = (Type.fromInterned(inst_info.child).zigTypeTag(mod) == .Array and (Type.fromInterned(inst_info.child).arrayLenIncludingSentinel(mod) == 0 or |
| ... | ... | @@ -30913,12 +31204,12 @@ fn checkPtrAttributes(sema: *Sema, dest_ty: Type, inst_ty: Type, in_memory_resul |
| 30913 | 31204 | const inst_align = if (inst_info.flags.alignment != .none) |
| 30914 | 31205 | inst_info.flags.alignment |
| 30915 | 31206 | else |
| 30916 | Type.fromInterned(inst_info.child).abiAlignment(mod); | |
| 31207 | Type.fromInterned(inst_info.child).abiAlignment(pt); | |
| 30917 | 31208 | |
| 30918 | 31209 | const dest_align = if (dest_info.flags.alignment != .none) |
| 30919 | 31210 | dest_info.flags.alignment |
| 30920 | 31211 | else |
| 30921 | Type.fromInterned(dest_info.child).abiAlignment(mod); | |
| 31212 | Type.fromInterned(dest_info.child).abiAlignment(pt); | |
| 30922 | 31213 | |
| 30923 | 31214 | if (dest_align.compare(.gt, inst_align)) { |
| 30924 | 31215 | in_memory_result.* = .{ .ptr_alignment = .{ |
| ... | ... | @@ -30937,15 +31228,16 @@ fn coerceCompatiblePtrs( |
| 30937 | 31228 | inst: Air.Inst.Ref, |
| 30938 | 31229 | inst_src: LazySrcLoc, |
| 30939 | 31230 | ) !Air.Inst.Ref { |
| 30940 | const mod = sema.mod; | |
| 31231 | const pt = sema.pt; | |
| 31232 | const mod = pt.zcu; | |
| 30941 | 31233 | const inst_ty = sema.typeOf(inst); |
| 30942 | 31234 | if (try sema.resolveValue(inst)) |val| { |
| 30943 | 31235 | if (!val.isUndef(mod) and val.isNull(mod) and !dest_ty.isAllowzeroPtr(mod)) { |
| 30944 | return sema.fail(block, inst_src, "null pointer casted to type '{}'", .{dest_ty.fmt(sema.mod)}); | |
| 31236 | return sema.fail(block, inst_src, "null pointer casted to type '{}'", .{dest_ty.fmt(pt)}); | |
| 30945 | 31237 | } |
| 30946 | 31238 | // The comptime Value representation is compatible with both types. |
| 30947 | 31239 | return Air.internedToRef( |
| 30948 | (try mod.getCoerced(val, dest_ty)).toIntern(), | |
| 31240 | (try pt.getCoerced(val, dest_ty)).toIntern(), | |
| 30949 | 31241 | ); |
| 30950 | 31242 | } |
| 30951 | 31243 | try sema.requireRuntimeBlock(block, inst_src, null); |
| ... | ... | @@ -30979,14 +31271,15 @@ fn coerceEnumToUnion( |
| 30979 | 31271 | inst: Air.Inst.Ref, |
| 30980 | 31272 | inst_src: LazySrcLoc, |
| 30981 | 31273 | ) !Air.Inst.Ref { |
| 30982 | const mod = sema.mod; | |
| 31274 | const pt = sema.pt; | |
| 31275 | const mod = pt.zcu; | |
| 30983 | 31276 | const ip = &mod.intern_pool; |
| 30984 | 31277 | const inst_ty = sema.typeOf(inst); |
| 30985 | 31278 | |
| 30986 | 31279 | const tag_ty = union_ty.unionTagType(mod) orelse { |
| 30987 | 31280 | const msg = msg: { |
| 30988 | 31281 | const msg = try sema.errMsg(inst_src, "expected type '{}', found '{}'", .{ |
| 30989 | union_ty.fmt(sema.mod), inst_ty.fmt(sema.mod), | |
| 31282 | union_ty.fmt(pt), inst_ty.fmt(pt), | |
| 30990 | 31283 | }); |
| 30991 | 31284 | errdefer msg.destroy(sema.gpa); |
| 30992 | 31285 | try sema.errNote(union_ty_src, msg, "cannot coerce enum to untagged union", .{}); |
| ... | ... | @@ -30998,15 +31291,15 @@ fn coerceEnumToUnion( |
| 30998 | 31291 | |
| 30999 | 31292 | const enum_tag = try sema.coerce(block, tag_ty, inst, inst_src); |
| 31000 | 31293 | if (try sema.resolveDefinedValue(block, inst_src, enum_tag)) |val| { |
| 31001 | const field_index = union_ty.unionTagFieldIndex(val, sema.mod) orelse { | |
| 31294 | const field_index = union_ty.unionTagFieldIndex(val, pt.zcu) orelse { | |
| 31002 | 31295 | return sema.fail(block, inst_src, "union '{}' has no tag with value '{}'", .{ |
| 31003 | union_ty.fmt(sema.mod), val.fmtValue(sema.mod, sema), | |
| 31296 | union_ty.fmt(pt), val.fmtValue(pt, sema), | |
| 31004 | 31297 | }); |
| 31005 | 31298 | }; |
| 31006 | 31299 | |
| 31007 | 31300 | const union_obj = mod.typeToUnion(union_ty).?; |
| 31008 | 31301 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 31009 | try field_ty.resolveFields(mod); | |
| 31302 | try field_ty.resolveFields(pt); | |
| 31010 | 31303 | if (field_ty.zigTypeTag(mod) == .NoReturn) { |
| 31011 | 31304 | const msg = msg: { |
| 31012 | 31305 | const msg = try sema.errMsg(inst_src, "cannot initialize 'noreturn' field of union", .{}); |
| ... | ... | @@ -31025,8 +31318,8 @@ fn coerceEnumToUnion( |
| 31025 | 31318 | const msg = msg: { |
| 31026 | 31319 | const field_name = union_obj.loadTagType(ip).names.get(ip)[field_index]; |
| 31027 | 31320 | const msg = try sema.errMsg(inst_src, "coercion from enum '{}' to union '{}' must initialize '{}' field '{}'", .{ |
| 31028 | inst_ty.fmt(sema.mod), union_ty.fmt(sema.mod), | |
| 31029 | field_ty.fmt(sema.mod), field_name.fmt(ip), | |
| 31321 | inst_ty.fmt(pt), union_ty.fmt(pt), | |
| 31322 | field_ty.fmt(pt), field_name.fmt(ip), | |
| 31030 | 31323 | }); |
| 31031 | 31324 | errdefer msg.destroy(sema.gpa); |
| 31032 | 31325 | |
| ... | ... | @@ -31039,7 +31332,7 @@ fn coerceEnumToUnion( |
| 31039 | 31332 | return sema.failWithOwnedErrorMsg(block, msg); |
| 31040 | 31333 | }; |
| 31041 | 31334 | |
| 31042 | return Air.internedToRef((try mod.unionValue(union_ty, val, opv)).toIntern()); | |
| 31335 | return Air.internedToRef((try pt.unionValue(union_ty, val, opv)).toIntern()); | |
| 31043 | 31336 | } |
| 31044 | 31337 | |
| 31045 | 31338 | try sema.requireRuntimeBlock(block, inst_src, null); |
| ... | ... | @@ -31047,7 +31340,7 @@ fn coerceEnumToUnion( |
| 31047 | 31340 | if (tag_ty.isNonexhaustiveEnum(mod)) { |
| 31048 | 31341 | const msg = msg: { |
| 31049 | 31342 | const msg = try sema.errMsg(inst_src, "runtime coercion to union '{}' from non-exhaustive enum", .{ |
| 31050 | union_ty.fmt(sema.mod), | |
| 31343 | union_ty.fmt(pt), | |
| 31051 | 31344 | }); |
| 31052 | 31345 | errdefer msg.destroy(sema.gpa); |
| 31053 | 31346 | try sema.addDeclaredHereNote(msg, tag_ty); |
| ... | ... | @@ -31066,7 +31359,7 @@ fn coerceEnumToUnion( |
| 31066 | 31359 | const err_msg = msg orelse try sema.errMsg( |
| 31067 | 31360 | inst_src, |
| 31068 | 31361 | "runtime coercion from enum '{}' to union '{}' which has a 'noreturn' field", |
| 31069 | .{ tag_ty.fmt(sema.mod), union_ty.fmt(sema.mod) }, | |
| 31362 | .{ tag_ty.fmt(pt), union_ty.fmt(pt) }, | |
| 31070 | 31363 | ); |
| 31071 | 31364 | msg = err_msg; |
| 31072 | 31365 | |
| ... | ... | @@ -31081,7 +31374,7 @@ fn coerceEnumToUnion( |
| 31081 | 31374 | } |
| 31082 | 31375 | |
| 31083 | 31376 | // If the union has all fields 0 bits, the union value is just the enum value. |
| 31084 | if (union_ty.unionHasAllZeroBitFieldTypes(mod)) { | |
| 31377 | if (union_ty.unionHasAllZeroBitFieldTypes(pt)) { | |
| 31085 | 31378 | return block.addBitCast(union_ty, enum_tag); |
| 31086 | 31379 | } |
| 31087 | 31380 | |
| ... | ... | @@ -31089,7 +31382,7 @@ fn coerceEnumToUnion( |
| 31089 | 31382 | const msg = try sema.errMsg( |
| 31090 | 31383 | inst_src, |
| 31091 | 31384 | "runtime coercion from enum '{}' to union '{}' which has non-void fields", |
| 31092 | .{ tag_ty.fmt(sema.mod), union_ty.fmt(sema.mod) }, | |
| 31385 | .{ tag_ty.fmt(pt), union_ty.fmt(pt) }, | |
| 31093 | 31386 | ); |
| 31094 | 31387 | errdefer msg.destroy(sema.gpa); |
| 31095 | 31388 | |
| ... | ... | @@ -31099,7 +31392,7 @@ fn coerceEnumToUnion( |
| 31099 | 31392 | if (!(try sema.typeHasRuntimeBits(field_ty))) continue; |
| 31100 | 31393 | try sema.addFieldErrNote(union_ty, field_index, msg, "field '{}' has type '{}'", .{ |
| 31101 | 31394 | field_name.fmt(ip), |
| 31102 | field_ty.fmt(sema.mod), | |
| 31395 | field_ty.fmt(pt), | |
| 31103 | 31396 | }); |
| 31104 | 31397 | } |
| 31105 | 31398 | try sema.addDeclaredHereNote(msg, union_ty); |
| ... | ... | @@ -31116,7 +31409,8 @@ fn coerceAnonStructToUnion( |
| 31116 | 31409 | inst: Air.Inst.Ref, |
| 31117 | 31410 | inst_src: LazySrcLoc, |
| 31118 | 31411 | ) !Air.Inst.Ref { |
| 31119 | const mod = sema.mod; | |
| 31412 | const pt = sema.pt; | |
| 31413 | const mod = pt.zcu; | |
| 31120 | 31414 | const ip = &mod.intern_pool; |
| 31121 | 31415 | const inst_ty = sema.typeOf(inst); |
| 31122 | 31416 | const field_info: union(enum) { |
| ... | ... | @@ -31174,7 +31468,8 @@ fn coerceAnonStructToUnionPtrs( |
| 31174 | 31468 | ptr_anon_struct: Air.Inst.Ref, |
| 31175 | 31469 | anon_struct_src: LazySrcLoc, |
| 31176 | 31470 | ) !Air.Inst.Ref { |
| 31177 | const mod = sema.mod; | |
| 31471 | const pt = sema.pt; | |
| 31472 | const mod = pt.zcu; | |
| 31178 | 31473 | const union_ty = ptr_union_ty.childType(mod); |
| 31179 | 31474 | const anon_struct = try sema.analyzeLoad(block, anon_struct_src, ptr_anon_struct, anon_struct_src); |
| 31180 | 31475 | const union_inst = try sema.coerceAnonStructToUnion(block, union_ty, union_ty_src, anon_struct, anon_struct_src); |
| ... | ... | @@ -31189,7 +31484,8 @@ fn coerceAnonStructToStructPtrs( |
| 31189 | 31484 | ptr_anon_struct: Air.Inst.Ref, |
| 31190 | 31485 | anon_struct_src: LazySrcLoc, |
| 31191 | 31486 | ) !Air.Inst.Ref { |
| 31192 | const mod = sema.mod; | |
| 31487 | const pt = sema.pt; | |
| 31488 | const mod = pt.zcu; | |
| 31193 | 31489 | const struct_ty = ptr_struct_ty.childType(mod); |
| 31194 | 31490 | const anon_struct = try sema.analyzeLoad(block, anon_struct_src, ptr_anon_struct, anon_struct_src); |
| 31195 | 31491 | const struct_inst = try sema.coerceTupleToStruct(block, struct_ty, anon_struct, anon_struct_src); |
| ... | ... | @@ -31205,7 +31501,8 @@ fn coerceArrayLike( |
| 31205 | 31501 | inst: Air.Inst.Ref, |
| 31206 | 31502 | inst_src: LazySrcLoc, |
| 31207 | 31503 | ) !Air.Inst.Ref { |
| 31208 | const mod = sema.mod; | |
| 31504 | const pt = sema.pt; | |
| 31505 | const mod = pt.zcu; | |
| 31209 | 31506 | const inst_ty = sema.typeOf(inst); |
| 31210 | 31507 | const target = mod.getTarget(); |
| 31211 | 31508 | |
| ... | ... | @@ -31226,7 +31523,7 @@ fn coerceArrayLike( |
| 31226 | 31523 | if (dest_len != inst_len) { |
| 31227 | 31524 | const msg = msg: { |
| 31228 | 31525 | const msg = try sema.errMsg(inst_src, "expected type '{}', found '{}'", .{ |
| 31229 | dest_ty.fmt(mod), inst_ty.fmt(mod), | |
| 31526 | dest_ty.fmt(pt), inst_ty.fmt(pt), | |
| 31230 | 31527 | }); |
| 31231 | 31528 | errdefer msg.destroy(sema.gpa); |
| 31232 | 31529 | try sema.errNote(dest_ty_src, msg, "destination has length {d}", .{dest_len}); |
| ... | ... | @@ -31270,7 +31567,7 @@ fn coerceArrayLike( |
| 31270 | 31567 | var runtime_src: ?LazySrcLoc = null; |
| 31271 | 31568 | |
| 31272 | 31569 | for (element_vals, element_refs, 0..) |*val, *ref, i| { |
| 31273 | const index_ref = Air.internedToRef((try mod.intValue(Type.usize, i)).toIntern()); | |
| 31570 | const index_ref = Air.internedToRef((try pt.intValue(Type.usize, i)).toIntern()); | |
| 31274 | 31571 | const src = inst_src; // TODO better source location |
| 31275 | 31572 | const elem_src = inst_src; // TODO better source location |
| 31276 | 31573 | const elem_ref = try sema.elemValArray(block, src, inst_src, inst, elem_src, index_ref, true); |
| ... | ... | @@ -31290,7 +31587,7 @@ fn coerceArrayLike( |
| 31290 | 31587 | return block.addAggregateInit(dest_ty, element_refs); |
| 31291 | 31588 | } |
| 31292 | 31589 | |
| 31293 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 31590 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 31294 | 31591 | .ty = dest_ty.toIntern(), |
| 31295 | 31592 | .storage = .{ .elems = element_vals }, |
| 31296 | 31593 | } }))); |
| ... | ... | @@ -31305,7 +31602,8 @@ fn coerceTupleToArray( |
| 31305 | 31602 | inst: Air.Inst.Ref, |
| 31306 | 31603 | inst_src: LazySrcLoc, |
| 31307 | 31604 | ) !Air.Inst.Ref { |
| 31308 | const mod = sema.mod; | |
| 31605 | const pt = sema.pt; | |
| 31606 | const mod = pt.zcu; | |
| 31309 | 31607 | const inst_ty = sema.typeOf(inst); |
| 31310 | 31608 | const inst_len = inst_ty.arrayLen(mod); |
| 31311 | 31609 | const dest_len = dest_ty.arrayLen(mod); |
| ... | ... | @@ -31313,7 +31611,7 @@ fn coerceTupleToArray( |
| 31313 | 31611 | if (dest_len != inst_len) { |
| 31314 | 31612 | const msg = msg: { |
| 31315 | 31613 | const msg = try sema.errMsg(inst_src, "expected type '{}', found '{}'", .{ |
| 31316 | dest_ty.fmt(sema.mod), inst_ty.fmt(sema.mod), | |
| 31614 | dest_ty.fmt(pt), inst_ty.fmt(pt), | |
| 31317 | 31615 | }); |
| 31318 | 31616 | errdefer msg.destroy(sema.gpa); |
| 31319 | 31617 | try sema.errNote(dest_ty_src, msg, "destination has length {d}", .{dest_len}); |
| ... | ... | @@ -31355,7 +31653,7 @@ fn coerceTupleToArray( |
| 31355 | 31653 | return block.addAggregateInit(dest_ty, element_refs); |
| 31356 | 31654 | } |
| 31357 | 31655 | |
| 31358 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 31656 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 31359 | 31657 | .ty = dest_ty.toIntern(), |
| 31360 | 31658 | .storage = .{ .elems = element_vals }, |
| 31361 | 31659 | } }))); |
| ... | ... | @@ -31370,11 +31668,12 @@ fn coerceTupleToSlicePtrs( |
| 31370 | 31668 | ptr_tuple: Air.Inst.Ref, |
| 31371 | 31669 | tuple_src: LazySrcLoc, |
| 31372 | 31670 | ) !Air.Inst.Ref { |
| 31373 | const mod = sema.mod; | |
| 31671 | const pt = sema.pt; | |
| 31672 | const mod = pt.zcu; | |
| 31374 | 31673 | const tuple_ty = sema.typeOf(ptr_tuple).childType(mod); |
| 31375 | 31674 | const tuple = try sema.analyzeLoad(block, tuple_src, ptr_tuple, tuple_src); |
| 31376 | 31675 | const slice_info = slice_ty.ptrInfo(mod); |
| 31377 | const array_ty = try mod.arrayType(.{ | |
| 31676 | const array_ty = try pt.arrayType(.{ | |
| 31378 | 31677 | .len = tuple_ty.structFieldCount(mod), |
| 31379 | 31678 | .sentinel = slice_info.sentinel, |
| 31380 | 31679 | .child = slice_info.child, |
| ... | ... | @@ -31396,7 +31695,8 @@ fn coerceTupleToArrayPtrs( |
| 31396 | 31695 | ptr_tuple: Air.Inst.Ref, |
| 31397 | 31696 | tuple_src: LazySrcLoc, |
| 31398 | 31697 | ) !Air.Inst.Ref { |
| 31399 | const mod = sema.mod; | |
| 31698 | const pt = sema.pt; | |
| 31699 | const mod = pt.zcu; | |
| 31400 | 31700 | const tuple = try sema.analyzeLoad(block, tuple_src, ptr_tuple, tuple_src); |
| 31401 | 31701 | const ptr_info = ptr_array_ty.ptrInfo(mod); |
| 31402 | 31702 | const array_ty = Type.fromInterned(ptr_info.child); |
| ... | ... | @@ -31417,10 +31717,11 @@ fn coerceTupleToStruct( |
| 31417 | 31717 | inst: Air.Inst.Ref, |
| 31418 | 31718 | inst_src: LazySrcLoc, |
| 31419 | 31719 | ) !Air.Inst.Ref { |
| 31420 | const mod = sema.mod; | |
| 31720 | const pt = sema.pt; | |
| 31721 | const mod = pt.zcu; | |
| 31421 | 31722 | const ip = &mod.intern_pool; |
| 31422 | try struct_ty.resolveFields(mod); | |
| 31423 | try struct_ty.resolveStructFieldInits(mod); | |
| 31723 | try struct_ty.resolveFields(pt); | |
| 31724 | try struct_ty.resolveStructFieldInits(pt); | |
| 31424 | 31725 | |
| 31425 | 31726 | if (struct_ty.isTupleOrAnonStruct(mod)) { |
| 31426 | 31727 | return sema.coerceTupleToTuple(block, struct_ty, inst, inst_src); |
| ... | ... | @@ -31461,7 +31762,7 @@ fn coerceTupleToStruct( |
| 31461 | 31762 | }; |
| 31462 | 31763 | |
| 31463 | 31764 | const field_init = Value.fromInterned(struct_type.field_inits.get(ip)[struct_field_index]); |
| 31464 | if (!init_val.eql(field_init, struct_field_ty, sema.mod)) { | |
| 31765 | if (!init_val.eql(field_init, struct_field_ty, pt.zcu)) { | |
| 31465 | 31766 | return sema.failWithInvalidComptimeFieldStore(block, field_src, inst_ty, tuple_field_index); |
| 31466 | 31767 | } |
| 31467 | 31768 | } |
| ... | ... | @@ -31512,7 +31813,7 @@ fn coerceTupleToStruct( |
| 31512 | 31813 | return block.addAggregateInit(struct_ty, field_refs); |
| 31513 | 31814 | } |
| 31514 | 31815 | |
| 31515 | const struct_val = try mod.intern(.{ .aggregate = .{ | |
| 31816 | const struct_val = try pt.intern(.{ .aggregate = .{ | |
| 31516 | 31817 | .ty = struct_ty.toIntern(), |
| 31517 | 31818 | .storage = .{ .elems = field_vals }, |
| 31518 | 31819 | } }); |
| ... | ... | @@ -31529,7 +31830,8 @@ fn coerceTupleToTuple( |
| 31529 | 31830 | inst: Air.Inst.Ref, |
| 31530 | 31831 | inst_src: LazySrcLoc, |
| 31531 | 31832 | ) !Air.Inst.Ref { |
| 31532 | const mod = sema.mod; | |
| 31833 | const pt = sema.pt; | |
| 31834 | const mod = pt.zcu; | |
| 31533 | 31835 | const ip = &mod.intern_pool; |
| 31534 | 31836 | const dest_field_count = switch (ip.indexToKey(tuple_ty.toIntern())) { |
| 31535 | 31837 | .anon_struct_type => |anon_struct_type| anon_struct_type.types.len, |
| ... | ... | @@ -31594,7 +31896,7 @@ fn coerceTupleToTuple( |
| 31594 | 31896 | }); |
| 31595 | 31897 | }; |
| 31596 | 31898 | |
| 31597 | if (!init_val.eql(Value.fromInterned(default_val), Type.fromInterned(field_ty), sema.mod)) { | |
| 31899 | if (!init_val.eql(Value.fromInterned(default_val), Type.fromInterned(field_ty), pt.zcu)) { | |
| 31598 | 31900 | return sema.failWithInvalidComptimeFieldStore(block, field_src, inst_ty, field_i); |
| 31599 | 31901 | } |
| 31600 | 31902 | } |
| ... | ... | @@ -31659,7 +31961,7 @@ fn coerceTupleToTuple( |
| 31659 | 31961 | return block.addAggregateInit(tuple_ty, field_refs); |
| 31660 | 31962 | } |
| 31661 | 31963 | |
| 31662 | return Air.internedToRef((try mod.intern(.{ .aggregate = .{ | |
| 31964 | return Air.internedToRef((try pt.intern(.{ .aggregate = .{ | |
| 31663 | 31965 | .ty = tuple_ty.toIntern(), |
| 31664 | 31966 | .storage = .{ .elems = field_vals }, |
| 31665 | 31967 | } }))); |
| ... | ... | @@ -31689,17 +31991,19 @@ fn addReferenceEntry( |
| 31689 | 31991 | src: LazySrcLoc, |
| 31690 | 31992 | referenced_unit: AnalUnit, |
| 31691 | 31993 | ) !void { |
| 31692 | if (sema.mod.comp.reference_trace == 0) return; | |
| 31994 | const zcu = sema.pt.zcu; | |
| 31995 | if (zcu.comp.reference_trace == 0) return; | |
| 31693 | 31996 | const gop = try sema.references.getOrPut(sema.gpa, referenced_unit); |
| 31694 | 31997 | if (gop.found_existing) return; |
| 31695 | 31998 | // TODO: we need to figure out how to model inline calls here. |
| 31696 | 31999 | // They aren't references in the analysis sense, but ought to show up in the reference trace! |
| 31697 | 32000 | // Would representing inline calls in the reference table cause excessive memory usage? |
| 31698 | try sema.mod.addUnitReference(sema.ownerUnit(), referenced_unit, src); | |
| 32001 | try zcu.addUnitReference(sema.ownerUnit(), referenced_unit, src); | |
| 31699 | 32002 | } |
| 31700 | 32003 | |
| 31701 | 32004 | pub fn ensureDeclAnalyzed(sema: *Sema, decl_index: InternPool.DeclIndex) CompileError!void { |
| 31702 | const mod = sema.mod; | |
| 32005 | const pt = sema.pt; | |
| 32006 | const mod = pt.zcu; | |
| 31703 | 32007 | const ip = &mod.intern_pool; |
| 31704 | 32008 | const decl = mod.declPtr(decl_index); |
| 31705 | 32009 | if (decl.analysis == .in_progress) { |
| ... | ... | @@ -31710,7 +32014,7 @@ pub fn ensureDeclAnalyzed(sema: *Sema, decl_index: InternPool.DeclIndex) Compile |
| 31710 | 32014 | return sema.failWithOwnedErrorMsg(null, msg); |
| 31711 | 32015 | } |
| 31712 | 32016 | |
| 31713 | mod.ensureDeclAnalyzed(decl_index) catch |err| { | |
| 32017 | pt.ensureDeclAnalyzed(decl_index) catch |err| { | |
| 31714 | 32018 | if (sema.owner_func_index != .none) { |
| 31715 | 32019 | ip.funcAnalysis(sema.owner_func_index).state = .dependency_failure; |
| 31716 | 32020 | } else { |
| ... | ... | @@ -31721,9 +32025,10 @@ pub fn ensureDeclAnalyzed(sema: *Sema, decl_index: InternPool.DeclIndex) Compile |
| 31721 | 32025 | } |
| 31722 | 32026 | |
| 31723 | 32027 | fn ensureFuncBodyAnalyzed(sema: *Sema, func: InternPool.Index) CompileError!void { |
| 31724 | const mod = sema.mod; | |
| 32028 | const pt = sema.pt; | |
| 32029 | const mod = pt.zcu; | |
| 31725 | 32030 | const ip = &mod.intern_pool; |
| 31726 | mod.ensureFuncBodyAnalyzed(func) catch |err| { | |
| 32031 | pt.ensureFuncBodyAnalyzed(func) catch |err| { | |
| 31727 | 32032 | if (sema.owner_func_index != .none) { |
| 31728 | 32033 | ip.funcAnalysis(sema.owner_func_index).state = .dependency_failure; |
| 31729 | 32034 | } else { |
| ... | ... | @@ -31734,15 +32039,15 @@ fn ensureFuncBodyAnalyzed(sema: *Sema, func: InternPool.Index) CompileError!void |
| 31734 | 32039 | } |
| 31735 | 32040 | |
| 31736 | 32041 | fn optRefValue(sema: *Sema, opt_val: ?Value) !Value { |
| 31737 | const mod = sema.mod; | |
| 31738 | const ptr_anyopaque_ty = try mod.singleConstPtrType(Type.anyopaque); | |
| 31739 | return Value.fromInterned((try mod.intern(.{ .opt = .{ | |
| 31740 | .ty = (try mod.optionalType(ptr_anyopaque_ty.toIntern())).toIntern(), | |
| 31741 | .val = if (opt_val) |val| (try mod.getCoerced( | |
| 32042 | const pt = sema.pt; | |
| 32043 | const ptr_anyopaque_ty = try pt.singleConstPtrType(Type.anyopaque); | |
| 32044 | return Value.fromInterned(try pt.intern(.{ .opt = .{ | |
| 32045 | .ty = (try pt.optionalType(ptr_anyopaque_ty.toIntern())).toIntern(), | |
| 32046 | .val = if (opt_val) |val| (try pt.getCoerced( | |
| 31742 | 32047 | Value.fromInterned(try sema.refValue(val.toIntern())), |
| 31743 | 32048 | ptr_anyopaque_ty, |
| 31744 | 32049 | )).toIntern() else .none, |
| 31745 | } }))); | |
| 32050 | } })); | |
| 31746 | 32051 | } |
| 31747 | 32052 | |
| 31748 | 32053 | fn analyzeDeclRef(sema: *Sema, src: LazySrcLoc, decl_index: InternPool.DeclIndex) CompileError!Air.Inst.Ref { |
| ... | ... | @@ -31754,7 +32059,8 @@ fn analyzeDeclRef(sema: *Sema, src: LazySrcLoc, decl_index: InternPool.DeclIndex |
| 31754 | 32059 | /// decl_ref to end up in runtime code, the function body must be analyzed: `analyzeDeclRef` wraps |
| 31755 | 32060 | /// this function with `analyze_fn_body` set to true. |
| 31756 | 32061 | fn analyzeDeclRefInner(sema: *Sema, src: LazySrcLoc, decl_index: InternPool.DeclIndex, analyze_fn_body: bool) CompileError!Air.Inst.Ref { |
| 31757 | const mod = sema.mod; | |
| 32062 | const pt = sema.pt; | |
| 32063 | const mod = pt.zcu; | |
| 31758 | 32064 | try sema.addReferenceEntry(src, AnalUnit.wrap(.{ .decl = decl_index })); |
| 31759 | 32065 | try sema.ensureDeclAnalyzed(decl_index); |
| 31760 | 32066 | |
| ... | ... | @@ -31767,7 +32073,7 @@ fn analyzeDeclRefInner(sema: *Sema, src: LazySrcLoc, decl_index: InternPool.Decl |
| 31767 | 32073 | }); |
| 31768 | 32074 | // TODO: if this is a `decl_ref` of a non-variable decl, only depend on decl type |
| 31769 | 32075 | try sema.declareDependency(.{ .decl_val = decl_index }); |
| 31770 | const ptr_ty = try mod.ptrTypeSema(.{ | |
| 32076 | const ptr_ty = try pt.ptrTypeSema(.{ | |
| 31771 | 32077 | .child = decl_val.typeOf(mod).toIntern(), |
| 31772 | 32078 | .flags = .{ |
| 31773 | 32079 | .alignment = owner_decl.alignment, |
| ... | ... | @@ -31778,7 +32084,7 @@ fn analyzeDeclRefInner(sema: *Sema, src: LazySrcLoc, decl_index: InternPool.Decl |
| 31778 | 32084 | if (analyze_fn_body) { |
| 31779 | 32085 | try sema.maybeQueueFuncBodyAnalysis(src, decl_index); |
| 31780 | 32086 | } |
| 31781 | return Air.internedToRef((try mod.intern(.{ .ptr = .{ | |
| 32087 | return Air.internedToRef((try pt.intern(.{ .ptr = .{ | |
| 31782 | 32088 | .ty = ptr_ty.toIntern(), |
| 31783 | 32089 | .base_addr = .{ .decl = decl_index }, |
| 31784 | 32090 | .byte_offset = 0, |
| ... | ... | @@ -31786,7 +32092,7 @@ fn analyzeDeclRefInner(sema: *Sema, src: LazySrcLoc, decl_index: InternPool.Decl |
| 31786 | 32092 | } |
| 31787 | 32093 | |
| 31788 | 32094 | fn maybeQueueFuncBodyAnalysis(sema: *Sema, src: LazySrcLoc, decl_index: InternPool.DeclIndex) !void { |
| 31789 | const mod = sema.mod; | |
| 32095 | const mod = sema.pt.zcu; | |
| 31790 | 32096 | const decl = mod.declPtr(decl_index); |
| 31791 | 32097 | const decl_val = try decl.valueOrFail(); |
| 31792 | 32098 | if (!mod.intern_pool.isFuncBody(decl_val.toIntern())) return; |
| ... | ... | @@ -31801,7 +32107,8 @@ fn analyzeRef( |
| 31801 | 32107 | src: LazySrcLoc, |
| 31802 | 32108 | operand: Air.Inst.Ref, |
| 31803 | 32109 | ) CompileError!Air.Inst.Ref { |
| 31804 | const mod = sema.mod; | |
| 32110 | const pt = sema.pt; | |
| 32111 | const mod = pt.zcu; | |
| 31805 | 32112 | const operand_ty = sema.typeOf(operand); |
| 31806 | 32113 | |
| 31807 | 32114 | if (try sema.resolveValue(operand)) |val| { |
| ... | ... | @@ -31814,14 +32121,14 @@ fn analyzeRef( |
| 31814 | 32121 | |
| 31815 | 32122 | try sema.requireRuntimeBlock(block, src, null); |
| 31816 | 32123 | const address_space = target_util.defaultAddressSpace(mod.getTarget(), .local); |
| 31817 | const ptr_type = try mod.ptrTypeSema(.{ | |
| 32124 | const ptr_type = try pt.ptrTypeSema(.{ | |
| 31818 | 32125 | .child = operand_ty.toIntern(), |
| 31819 | 32126 | .flags = .{ |
| 31820 | 32127 | .is_const = true, |
| 31821 | 32128 | .address_space = address_space, |
| 31822 | 32129 | }, |
| 31823 | 32130 | }); |
| 31824 | const mut_ptr_type = try mod.ptrTypeSema(.{ | |
| 32131 | const mut_ptr_type = try pt.ptrTypeSema(.{ | |
| 31825 | 32132 | .child = operand_ty.toIntern(), |
| 31826 | 32133 | .flags = .{ .address_space = address_space }, |
| 31827 | 32134 | }); |
| ... | ... | @@ -31839,14 +32146,15 @@ fn analyzeLoad( |
| 31839 | 32146 | ptr: Air.Inst.Ref, |
| 31840 | 32147 | ptr_src: LazySrcLoc, |
| 31841 | 32148 | ) CompileError!Air.Inst.Ref { |
| 31842 | const mod = sema.mod; | |
| 32149 | const pt = sema.pt; | |
| 32150 | const mod = pt.zcu; | |
| 31843 | 32151 | const ptr_ty = sema.typeOf(ptr); |
| 31844 | 32152 | const elem_ty = switch (ptr_ty.zigTypeTag(mod)) { |
| 31845 | 32153 | .Pointer => ptr_ty.childType(mod), |
| 31846 | else => return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty.fmt(sema.mod)}), | |
| 32154 | else => return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty.fmt(pt)}), | |
| 31847 | 32155 | }; |
| 31848 | 32156 | if (elem_ty.zigTypeTag(mod) == .Opaque) { |
| 31849 | return sema.fail(block, ptr_src, "cannot load opaque type '{}'", .{elem_ty.fmt(mod)}); | |
| 32157 | return sema.fail(block, ptr_src, "cannot load opaque type '{}'", .{elem_ty.fmt(pt)}); | |
| 31850 | 32158 | } |
| 31851 | 32159 | |
| 31852 | 32160 | if (try sema.typeHasOnePossibleValue(elem_ty)) |opv| { |
| ... | ... | @@ -31868,7 +32176,7 @@ fn analyzeLoad( |
| 31868 | 32176 | return block.addBinOp(.ptr_elem_val, bin_op.lhs, bin_op.rhs); |
| 31869 | 32177 | } |
| 31870 | 32178 | return sema.fail(block, ptr_src, "unable to determine vector element index of type '{}'", .{ |
| 31871 | ptr_ty.fmt(sema.mod), | |
| 32179 | ptr_ty.fmt(pt), | |
| 31872 | 32180 | }); |
| 31873 | 32181 | } |
| 31874 | 32182 | |
| ... | ... | @@ -31882,10 +32190,11 @@ fn analyzeSlicePtr( |
| 31882 | 32190 | slice: Air.Inst.Ref, |
| 31883 | 32191 | slice_ty: Type, |
| 31884 | 32192 | ) CompileError!Air.Inst.Ref { |
| 31885 | const mod = sema.mod; | |
| 32193 | const pt = sema.pt; | |
| 32194 | const mod = pt.zcu; | |
| 31886 | 32195 | const result_ty = slice_ty.slicePtrFieldType(mod); |
| 31887 | 32196 | if (try sema.resolveValue(slice)) |val| { |
| 31888 | if (val.isUndef(mod)) return mod.undefRef(result_ty); | |
| 32197 | if (val.isUndef(mod)) return pt.undefRef(result_ty); | |
| 31889 | 32198 | return Air.internedToRef(val.slicePtr(mod).toIntern()); |
| 31890 | 32199 | } |
| 31891 | 32200 | try sema.requireRuntimeBlock(block, slice_src, null); |
| ... | ... | @@ -31899,11 +32208,12 @@ fn analyzeOptionalSlicePtr( |
| 31899 | 32208 | opt_slice: Air.Inst.Ref, |
| 31900 | 32209 | opt_slice_ty: Type, |
| 31901 | 32210 | ) CompileError!Air.Inst.Ref { |
| 31902 | const mod = sema.mod; | |
| 32211 | const pt = sema.pt; | |
| 32212 | const mod = pt.zcu; | |
| 31903 | 32213 | const result_ty = opt_slice_ty.optionalChild(mod).slicePtrFieldType(mod); |
| 31904 | 32214 | |
| 31905 | 32215 | if (try sema.resolveValue(opt_slice)) |opt_val| { |
| 31906 | if (opt_val.isUndef(mod)) return mod.undefRef(result_ty); | |
| 32216 | if (opt_val.isUndef(mod)) return pt.undefRef(result_ty); | |
| 31907 | 32217 | const slice_ptr: InternPool.Index = if (opt_val.optionalValue(mod)) |val| |
| 31908 | 32218 | val.slicePtr(mod).toIntern() |
| 31909 | 32219 | else |
| ... | ... | @@ -31924,12 +32234,13 @@ fn analyzeSliceLen( |
| 31924 | 32234 | src: LazySrcLoc, |
| 31925 | 32235 | slice_inst: Air.Inst.Ref, |
| 31926 | 32236 | ) CompileError!Air.Inst.Ref { |
| 31927 | const mod = sema.mod; | |
| 32237 | const pt = sema.pt; | |
| 32238 | const mod = pt.zcu; | |
| 31928 | 32239 | if (try sema.resolveValue(slice_inst)) |slice_val| { |
| 31929 | 32240 | if (slice_val.isUndef(mod)) { |
| 31930 | return mod.undefRef(Type.usize); | |
| 32241 | return pt.undefRef(Type.usize); | |
| 31931 | 32242 | } |
| 31932 | return mod.intRef(Type.usize, try slice_val.sliceLen(mod)); | |
| 32243 | return pt.intRef(Type.usize, try slice_val.sliceLen(pt)); | |
| 31933 | 32244 | } |
| 31934 | 32245 | try sema.requireRuntimeBlock(block, src, null); |
| 31935 | 32246 | return block.addTyOp(.slice_len, Type.usize, slice_inst); |
| ... | ... | @@ -31942,11 +32253,12 @@ fn analyzeIsNull( |
| 31942 | 32253 | operand: Air.Inst.Ref, |
| 31943 | 32254 | invert_logic: bool, |
| 31944 | 32255 | ) CompileError!Air.Inst.Ref { |
| 31945 | const mod = sema.mod; | |
| 32256 | const pt = sema.pt; | |
| 32257 | const mod = pt.zcu; | |
| 31946 | 32258 | const result_ty = Type.bool; |
| 31947 | 32259 | if (try sema.resolveValue(operand)) |opt_val| { |
| 31948 | 32260 | if (opt_val.isUndef(mod)) { |
| 31949 | return mod.undefRef(result_ty); | |
| 32261 | return pt.undefRef(result_ty); | |
| 31950 | 32262 | } |
| 31951 | 32263 | const is_null = opt_val.isNull(mod); |
| 31952 | 32264 | const bool_value = if (invert_logic) !is_null else is_null; |
| ... | ... | @@ -31972,7 +32284,8 @@ fn analyzePtrIsNonErrComptimeOnly( |
| 31972 | 32284 | src: LazySrcLoc, |
| 31973 | 32285 | operand: Air.Inst.Ref, |
| 31974 | 32286 | ) CompileError!Air.Inst.Ref { |
| 31975 | const mod = sema.mod; | |
| 32287 | const pt = sema.pt; | |
| 32288 | const mod = pt.zcu; | |
| 31976 | 32289 | const ptr_ty = sema.typeOf(operand); |
| 31977 | 32290 | assert(ptr_ty.zigTypeTag(mod) == .Pointer); |
| 31978 | 32291 | const child_ty = ptr_ty.childType(mod); |
| ... | ... | @@ -31994,7 +32307,8 @@ fn analyzeIsNonErrComptimeOnly( |
| 31994 | 32307 | src: LazySrcLoc, |
| 31995 | 32308 | operand: Air.Inst.Ref, |
| 31996 | 32309 | ) CompileError!Air.Inst.Ref { |
| 31997 | const mod = sema.mod; | |
| 32310 | const pt = sema.pt; | |
| 32311 | const mod = pt.zcu; | |
| 31998 | 32312 | const ip = &mod.intern_pool; |
| 31999 | 32313 | const operand_ty = sema.typeOf(operand); |
| 32000 | 32314 | const ot = operand_ty.zigTypeTag(mod); |
| ... | ... | @@ -32014,7 +32328,7 @@ fn analyzeIsNonErrComptimeOnly( |
| 32014 | 32328 | else => {}, |
| 32015 | 32329 | } |
| 32016 | 32330 | } else if (operand == .undef) { |
| 32017 | return mod.undefRef(Type.bool); | |
| 32331 | return pt.undefRef(Type.bool); | |
| 32018 | 32332 | } else if (@intFromEnum(operand) < InternPool.static_len) { |
| 32019 | 32333 | // None of the ref tags can be errors. |
| 32020 | 32334 | return .bool_true; |
| ... | ... | @@ -32098,7 +32412,7 @@ fn analyzeIsNonErrComptimeOnly( |
| 32098 | 32412 | |
| 32099 | 32413 | if (maybe_operand_val) |err_union| { |
| 32100 | 32414 | if (err_union.isUndef(mod)) { |
| 32101 | return mod.undefRef(Type.bool); | |
| 32415 | return pt.undefRef(Type.bool); | |
| 32102 | 32416 | } |
| 32103 | 32417 | if (err_union.getErrorName(mod) == .none) { |
| 32104 | 32418 | return .bool_true; |
| ... | ... | @@ -32153,13 +32467,14 @@ fn analyzeSlice( |
| 32153 | 32467 | end_src: LazySrcLoc, |
| 32154 | 32468 | by_length: bool, |
| 32155 | 32469 | ) CompileError!Air.Inst.Ref { |
| 32156 | const mod = sema.mod; | |
| 32470 | const pt = sema.pt; | |
| 32471 | const mod = pt.zcu; | |
| 32157 | 32472 | // Slice expressions can operate on a variable whose type is an array. This requires |
| 32158 | 32473 | // the slice operand to be a pointer. In the case of a non-array, it will be a double pointer. |
| 32159 | 32474 | const ptr_ptr_ty = sema.typeOf(ptr_ptr); |
| 32160 | 32475 | const ptr_ptr_child_ty = switch (ptr_ptr_ty.zigTypeTag(mod)) { |
| 32161 | 32476 | .Pointer => ptr_ptr_ty.childType(mod), |
| 32162 | else => return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ptr_ty.fmt(mod)}), | |
| 32477 | else => return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ptr_ty.fmt(pt)}), | |
| 32163 | 32478 | }; |
| 32164 | 32479 | |
| 32165 | 32480 | var array_ty = ptr_ptr_child_ty; |
| ... | ... | @@ -32210,8 +32525,8 @@ fn analyzeSlice( |
| 32210 | 32525 | msg, |
| 32211 | 32526 | "expected '{}', found '{}'", |
| 32212 | 32527 | .{ |
| 32213 | Value.zero_comptime_int.fmtValue(mod, sema), | |
| 32214 | start_value.fmtValue(mod, sema), | |
| 32528 | Value.zero_comptime_int.fmtValue(pt, sema), | |
| 32529 | start_value.fmtValue(pt, sema), | |
| 32215 | 32530 | }, |
| 32216 | 32531 | ); |
| 32217 | 32532 | break :msg msg; |
| ... | ... | @@ -32226,8 +32541,8 @@ fn analyzeSlice( |
| 32226 | 32541 | msg, |
| 32227 | 32542 | "expected '{}', found '{}'", |
| 32228 | 32543 | .{ |
| 32229 | Value.one_comptime_int.fmtValue(mod, sema), | |
| 32230 | end_value.fmtValue(mod, sema), | |
| 32544 | Value.one_comptime_int.fmtValue(pt, sema), | |
| 32545 | end_value.fmtValue(pt, sema), | |
| 32231 | 32546 | }, |
| 32232 | 32547 | ); |
| 32233 | 32548 | break :msg msg; |
| ... | ... | @@ -32240,17 +32555,17 @@ fn analyzeSlice( |
| 32240 | 32555 | block, |
| 32241 | 32556 | end_src, |
| 32242 | 32557 | "end index {} out of bounds for slice of single-item pointer", |
| 32243 | .{end_value.fmtValue(mod, sema)}, | |
| 32558 | .{end_value.fmtValue(pt, sema)}, | |
| 32244 | 32559 | ); |
| 32245 | 32560 | } |
| 32246 | 32561 | } |
| 32247 | 32562 | |
| 32248 | array_ty = try mod.arrayType(.{ | |
| 32563 | array_ty = try pt.arrayType(.{ | |
| 32249 | 32564 | .len = 1, |
| 32250 | 32565 | .child = double_child_ty.toIntern(), |
| 32251 | 32566 | }); |
| 32252 | 32567 | const ptr_info = ptr_ptr_child_ty.ptrInfo(mod); |
| 32253 | slice_ty = try mod.ptrType(.{ | |
| 32568 | slice_ty = try pt.ptrType(.{ | |
| 32254 | 32569 | .child = array_ty.toIntern(), |
| 32255 | 32570 | .flags = .{ |
| 32256 | 32571 | .alignment = ptr_info.flags.alignment, |
| ... | ... | @@ -32286,7 +32601,7 @@ fn analyzeSlice( |
| 32286 | 32601 | elem_ty = ptr_ptr_child_ty.childType(mod); |
| 32287 | 32602 | }, |
| 32288 | 32603 | }, |
| 32289 | else => return sema.fail(block, src, "slice of non-array type '{}'", .{ptr_ptr_child_ty.fmt(mod)}), | |
| 32604 | else => return sema.fail(block, src, "slice of non-array type '{}'", .{ptr_ptr_child_ty.fmt(pt)}), | |
| 32290 | 32605 | } |
| 32291 | 32606 | |
| 32292 | 32607 | const ptr = if (slice_ty.isSlice(mod)) |
| ... | ... | @@ -32297,7 +32612,7 @@ fn analyzeSlice( |
| 32297 | 32612 | assert(manyptr_ty_key.flags.size == .One); |
| 32298 | 32613 | manyptr_ty_key.child = elem_ty.toIntern(); |
| 32299 | 32614 | manyptr_ty_key.flags.size = .Many; |
| 32300 | break :ptr try sema.coerceCompatiblePtrs(block, try mod.ptrTypeSema(manyptr_ty_key), ptr_or_slice, ptr_src); | |
| 32615 | break :ptr try sema.coerceCompatiblePtrs(block, try pt.ptrTypeSema(manyptr_ty_key), ptr_or_slice, ptr_src); | |
| 32301 | 32616 | } else ptr_or_slice; |
| 32302 | 32617 | |
| 32303 | 32618 | const start = try sema.coerce(block, Type.usize, uncasted_start, start_src); |
| ... | ... | @@ -32311,7 +32626,7 @@ fn analyzeSlice( |
| 32311 | 32626 | var end_is_len = uncasted_end_opt == .none; |
| 32312 | 32627 | const end = e: { |
| 32313 | 32628 | if (array_ty.zigTypeTag(mod) == .Array) { |
| 32314 | const len_val = try mod.intValue(Type.usize, array_ty.arrayLen(mod)); | |
| 32629 | const len_val = try pt.intValue(Type.usize, array_ty.arrayLen(mod)); | |
| 32315 | 32630 | |
| 32316 | 32631 | if (!end_is_len) { |
| 32317 | 32632 | const end = if (by_length) end: { |
| ... | ... | @@ -32320,7 +32635,7 @@ fn analyzeSlice( |
| 32320 | 32635 | break :end try sema.coerce(block, Type.usize, uncasted_end, end_src); |
| 32321 | 32636 | } else try sema.coerce(block, Type.usize, uncasted_end_opt, end_src); |
| 32322 | 32637 | if (try sema.resolveDefinedValue(block, end_src, end)) |end_val| { |
| 32323 | const len_s_val = try mod.intValue( | |
| 32638 | const len_s_val = try pt.intValue( | |
| 32324 | 32639 | Type.usize, |
| 32325 | 32640 | array_ty.arrayLenIncludingSentinel(mod), |
| 32326 | 32641 | ); |
| ... | ... | @@ -32335,8 +32650,8 @@ fn analyzeSlice( |
| 32335 | 32650 | end_src, |
| 32336 | 32651 | "end index {} out of bounds for array of length {}{s}", |
| 32337 | 32652 | .{ |
| 32338 | end_val.fmtValue(mod, sema), | |
| 32339 | len_val.fmtValue(mod, sema), | |
| 32653 | end_val.fmtValue(pt, sema), | |
| 32654 | len_val.fmtValue(pt, sema), | |
| 32340 | 32655 | sentinel_label, |
| 32341 | 32656 | }, |
| 32342 | 32657 | ); |
| ... | ... | @@ -32366,9 +32681,9 @@ fn analyzeSlice( |
| 32366 | 32681 | return sema.fail(block, src, "slice of undefined", .{}); |
| 32367 | 32682 | } |
| 32368 | 32683 | const has_sentinel = slice_ty.sentinel(mod) != null; |
| 32369 | const slice_len = try slice_val.sliceLen(mod); | |
| 32684 | const slice_len = try slice_val.sliceLen(pt); | |
| 32370 | 32685 | const len_plus_sent = slice_len + @intFromBool(has_sentinel); |
| 32371 | const slice_len_val_with_sentinel = try mod.intValue(Type.usize, len_plus_sent); | |
| 32686 | const slice_len_val_with_sentinel = try pt.intValue(Type.usize, len_plus_sent); | |
| 32372 | 32687 | if (!(try sema.compareAll(end_val, .lte, slice_len_val_with_sentinel, Type.usize))) { |
| 32373 | 32688 | const sentinel_label: []const u8 = if (has_sentinel) |
| 32374 | 32689 | " +1 (sentinel)" |
| ... | ... | @@ -32380,8 +32695,8 @@ fn analyzeSlice( |
| 32380 | 32695 | end_src, |
| 32381 | 32696 | "end index {} out of bounds for slice of length {d}{s}", |
| 32382 | 32697 | .{ |
| 32383 | end_val.fmtValue(mod, sema), | |
| 32384 | try slice_val.sliceLen(mod), | |
| 32698 | end_val.fmtValue(pt, sema), | |
| 32699 | try slice_val.sliceLen(pt), | |
| 32385 | 32700 | sentinel_label, |
| 32386 | 32701 | }, |
| 32387 | 32702 | ); |
| ... | ... | @@ -32390,7 +32705,7 @@ fn analyzeSlice( |
| 32390 | 32705 | // If the slice has a sentinel, we consider end_is_len |
| 32391 | 32706 | // is only true if it equals the length WITHOUT the |
| 32392 | 32707 | // sentinel, so we don't add a sentinel type. |
| 32393 | const slice_len_val = try mod.intValue(Type.usize, slice_len); | |
| 32708 | const slice_len_val = try pt.intValue(Type.usize, slice_len); | |
| 32394 | 32709 | if (end_val.eql(slice_len_val, Type.usize, mod)) { |
| 32395 | 32710 | end_is_len = true; |
| 32396 | 32711 | } |
| ... | ... | @@ -32440,21 +32755,21 @@ fn analyzeSlice( |
| 32440 | 32755 | start_src, |
| 32441 | 32756 | "start index {} is larger than end index {}", |
| 32442 | 32757 | .{ |
| 32443 | start_val.fmtValue(mod, sema), | |
| 32444 | end_val.fmtValue(mod, sema), | |
| 32758 | start_val.fmtValue(pt, sema), | |
| 32759 | end_val.fmtValue(pt, sema), | |
| 32445 | 32760 | }, |
| 32446 | 32761 | ); |
| 32447 | 32762 | } |
| 32448 | 32763 | checked_start_lte_end = true; |
| 32449 | 32764 | if (try sema.resolveValue(new_ptr)) |ptr_val| sentinel_check: { |
| 32450 | 32765 | const expected_sentinel = sentinel orelse break :sentinel_check; |
| 32451 | const start_int = start_val.getUnsignedInt(mod).?; | |
| 32452 | const end_int = end_val.getUnsignedInt(mod).?; | |
| 32766 | const start_int = start_val.getUnsignedInt(pt).?; | |
| 32767 | const end_int = end_val.getUnsignedInt(pt).?; | |
| 32453 | 32768 | const sentinel_index = try sema.usizeCast(block, end_src, end_int - start_int); |
| 32454 | 32769 | |
| 32455 | const many_ptr_ty = try mod.manyConstPtrType(elem_ty); | |
| 32456 | const many_ptr_val = try mod.getCoerced(ptr_val, many_ptr_ty); | |
| 32457 | const elem_ptr = try many_ptr_val.ptrElem(sentinel_index, mod); | |
| 32770 | const many_ptr_ty = try pt.manyConstPtrType(elem_ty); | |
| 32771 | const many_ptr_val = try pt.getCoerced(ptr_val, many_ptr_ty); | |
| 32772 | const elem_ptr = try many_ptr_val.ptrElem(sentinel_index, pt); | |
| 32458 | 32773 | const res = try sema.pointerDerefExtra(block, src, elem_ptr); |
| 32459 | 32774 | const actual_sentinel = switch (res) { |
| 32460 | 32775 | .runtime_load => break :sentinel_check, |
| ... | ... | @@ -32463,13 +32778,13 @@ fn analyzeSlice( |
| 32463 | 32778 | block, |
| 32464 | 32779 | src, |
| 32465 | 32780 | "comptime dereference requires '{}' to have a well-defined layout", |
| 32466 | .{ty.fmt(mod)}, | |
| 32781 | .{ty.fmt(pt)}, | |
| 32467 | 32782 | ), |
| 32468 | 32783 | .out_of_bounds => |ty| return sema.fail( |
| 32469 | 32784 | block, |
| 32470 | 32785 | end_src, |
| 32471 | 32786 | "slice end index {d} exceeds bounds of containing decl of type '{}'", |
| 32472 | .{ end_int, ty.fmt(mod) }, | |
| 32787 | .{ end_int, ty.fmt(pt) }, | |
| 32473 | 32788 | ), |
| 32474 | 32789 | }; |
| 32475 | 32790 | |
| ... | ... | @@ -32478,8 +32793,8 @@ fn analyzeSlice( |
| 32478 | 32793 | const msg = try sema.errMsg(src, "value in memory does not match slice sentinel", .{}); |
| 32479 | 32794 | errdefer msg.destroy(sema.gpa); |
| 32480 | 32795 | try sema.errNote(src, msg, "expected '{}', found '{}'", .{ |
| 32481 | expected_sentinel.fmtValue(mod, sema), | |
| 32482 | actual_sentinel.fmtValue(mod, sema), | |
| 32796 | expected_sentinel.fmtValue(pt, sema), | |
| 32797 | actual_sentinel.fmtValue(pt, sema), | |
| 32483 | 32798 | }); |
| 32484 | 32799 | |
| 32485 | 32800 | break :msg msg; |
| ... | ... | @@ -32501,7 +32816,7 @@ fn analyzeSlice( |
| 32501 | 32816 | assert(!block.is_comptime); |
| 32502 | 32817 | try sema.requireRuntimeBlock(block, src, runtime_src.?); |
| 32503 | 32818 | const ok = try block.addBinOp(.cmp_lte, start, end); |
| 32504 | if (!sema.mod.comp.formatted_panics) { | |
| 32819 | if (!pt.zcu.comp.formatted_panics) { | |
| 32505 | 32820 | try sema.addSafetyCheck(block, src, ok, .start_index_greater_than_end); |
| 32506 | 32821 | } else { |
| 32507 | 32822 | try sema.safetyCheckFormatted(block, src, ok, "panicStartGreaterThanEnd", &.{ start, end }); |
| ... | ... | @@ -32517,10 +32832,10 @@ fn analyzeSlice( |
| 32517 | 32832 | const new_allowzero = new_ptr_ty_info.flags.is_allowzero and sema.typeOf(ptr).ptrSize(mod) != .C; |
| 32518 | 32833 | |
| 32519 | 32834 | if (opt_new_len_val) |new_len_val| { |
| 32520 | const new_len_int = try new_len_val.toUnsignedIntSema(mod); | |
| 32835 | const new_len_int = try new_len_val.toUnsignedIntSema(pt); | |
| 32521 | 32836 | |
| 32522 | const return_ty = try mod.ptrTypeSema(.{ | |
| 32523 | .child = (try mod.arrayType(.{ | |
| 32837 | const return_ty = try pt.ptrTypeSema(.{ | |
| 32838 | .child = (try pt.arrayType(.{ | |
| 32524 | 32839 | .len = new_len_int, |
| 32525 | 32840 | .sentinel = if (sentinel) |s| s.toIntern() else .none, |
| 32526 | 32841 | .child = elem_ty.toIntern(), |
| ... | ... | @@ -32546,7 +32861,7 @@ fn analyzeSlice( |
| 32546 | 32861 | |
| 32547 | 32862 | bounds_check: { |
| 32548 | 32863 | const actual_len = if (array_ty.zigTypeTag(mod) == .Array) |
| 32549 | try mod.intRef(Type.usize, array_ty.arrayLenIncludingSentinel(mod)) | |
| 32864 | try pt.intRef(Type.usize, array_ty.arrayLenIncludingSentinel(mod)) | |
| 32550 | 32865 | else if (slice_ty.isSlice(mod)) l: { |
| 32551 | 32866 | const slice_len_inst = try block.addTyOp(.slice_len, Type.usize, ptr_or_slice); |
| 32552 | 32867 | break :l if (slice_ty.sentinel(mod) == null) |
| ... | ... | @@ -32570,18 +32885,18 @@ fn analyzeSlice( |
| 32570 | 32885 | }; |
| 32571 | 32886 | |
| 32572 | 32887 | if (!new_ptr_val.isUndef(mod)) { |
| 32573 | return Air.internedToRef((try mod.getCoerced(new_ptr_val, return_ty)).toIntern()); | |
| 32888 | return Air.internedToRef((try pt.getCoerced(new_ptr_val, return_ty)).toIntern()); | |
| 32574 | 32889 | } |
| 32575 | 32890 | |
| 32576 | 32891 | // Special case: @as([]i32, undefined)[x..x] |
| 32577 | 32892 | if (new_len_int == 0) { |
| 32578 | return mod.undefRef(return_ty); | |
| 32893 | return pt.undefRef(return_ty); | |
| 32579 | 32894 | } |
| 32580 | 32895 | |
| 32581 | 32896 | return sema.fail(block, src, "non-zero length slice of undefined pointer", .{}); |
| 32582 | 32897 | } |
| 32583 | 32898 | |
| 32584 | const return_ty = try mod.ptrTypeSema(.{ | |
| 32899 | const return_ty = try pt.ptrTypeSema(.{ | |
| 32585 | 32900 | .child = elem_ty.toIntern(), |
| 32586 | 32901 | .sentinel = if (sentinel) |s| s.toIntern() else .none, |
| 32587 | 32902 | .flags = .{ |
| ... | ... | @@ -32604,12 +32919,12 @@ fn analyzeSlice( |
| 32604 | 32919 | |
| 32605 | 32920 | // requirement: end <= len |
| 32606 | 32921 | const opt_len_inst = if (array_ty.zigTypeTag(mod) == .Array) |
| 32607 | try mod.intRef(Type.usize, array_ty.arrayLenIncludingSentinel(mod)) | |
| 32922 | try pt.intRef(Type.usize, array_ty.arrayLenIncludingSentinel(mod)) | |
| 32608 | 32923 | else if (slice_ty.isSlice(mod)) blk: { |
| 32609 | 32924 | if (try sema.resolveDefinedValue(block, src, ptr_or_slice)) |slice_val| { |
| 32610 | 32925 | // we don't need to add one for sentinels because the |
| 32611 | 32926 | // underlying value data includes the sentinel |
| 32612 | break :blk try mod.intRef(Type.usize, try slice_val.sliceLen(mod)); | |
| 32927 | break :blk try pt.intRef(Type.usize, try slice_val.sliceLen(pt)); | |
| 32613 | 32928 | } |
| 32614 | 32929 | |
| 32615 | 32930 | const slice_len_inst = try block.addTyOp(.slice_len, Type.usize, ptr_or_slice); |
| ... | ... | @@ -32657,7 +32972,8 @@ fn cmpNumeric( |
| 32657 | 32972 | lhs_src: LazySrcLoc, |
| 32658 | 32973 | rhs_src: LazySrcLoc, |
| 32659 | 32974 | ) CompileError!Air.Inst.Ref { |
| 32660 | const mod = sema.mod; | |
| 32975 | const pt = sema.pt; | |
| 32976 | const mod = pt.zcu; | |
| 32661 | 32977 | const lhs_ty = sema.typeOf(uncasted_lhs); |
| 32662 | 32978 | const rhs_ty = sema.typeOf(uncasted_rhs); |
| 32663 | 32979 | |
| ... | ... | @@ -32696,12 +33012,12 @@ fn cmpNumeric( |
| 32696 | 33012 | } |
| 32697 | 33013 | |
| 32698 | 33014 | if (lhs_val.isUndef(mod) or rhs_val.isUndef(mod)) { |
| 32699 | return mod.undefRef(Type.bool); | |
| 33015 | return pt.undefRef(Type.bool); | |
| 32700 | 33016 | } |
| 32701 | 33017 | if (lhs_val.isNan(mod) or rhs_val.isNan(mod)) { |
| 32702 | 33018 | return if (op == std.math.CompareOperator.neq) .bool_true else .bool_false; |
| 32703 | 33019 | } |
| 32704 | return if (try Value.compareHeteroAdvanced(lhs_val, op, rhs_val, mod, .sema)) | |
| 33020 | return if (try Value.compareHeteroAdvanced(lhs_val, op, rhs_val, pt, .sema)) | |
| 32705 | 33021 | .bool_true |
| 32706 | 33022 | else |
| 32707 | 33023 | .bool_false; |
| ... | ... | @@ -32770,11 +33086,11 @@ fn cmpNumeric( |
| 32770 | 33086 | // a signed integer with mantissa bits + 1, and if there was any non-integral part of the float, |
| 32771 | 33087 | // add/subtract 1. |
| 32772 | 33088 | const lhs_is_signed = if (try sema.resolveDefinedValue(block, lhs_src, lhs)) |lhs_val| |
| 32773 | !(try lhs_val.compareAllWithZeroSema(.gte, mod)) | |
| 33089 | !(try lhs_val.compareAllWithZeroSema(.gte, pt)) | |
| 32774 | 33090 | else |
| 32775 | 33091 | (lhs_ty.isRuntimeFloat() or lhs_ty.isSignedInt(mod)); |
| 32776 | 33092 | const rhs_is_signed = if (try sema.resolveDefinedValue(block, rhs_src, rhs)) |rhs_val| |
| 32777 | !(try rhs_val.compareAllWithZeroSema(.gte, mod)) | |
| 33093 | !(try rhs_val.compareAllWithZeroSema(.gte, pt)) | |
| 32778 | 33094 | else |
| 32779 | 33095 | (rhs_ty.isRuntimeFloat() or rhs_ty.isSignedInt(mod)); |
| 32780 | 33096 | const dest_int_is_signed = lhs_is_signed or rhs_is_signed; |
| ... | ... | @@ -32784,7 +33100,7 @@ fn cmpNumeric( |
| 32784 | 33100 | var lhs_bits: usize = undefined; |
| 32785 | 33101 | if (try sema.resolveValueResolveLazy(lhs)) |lhs_val| { |
| 32786 | 33102 | if (lhs_val.isUndef(mod)) |
| 32787 | return mod.undefRef(Type.bool); | |
| 33103 | return pt.undefRef(Type.bool); | |
| 32788 | 33104 | if (lhs_val.isNan(mod)) switch (op) { |
| 32789 | 33105 | .neq => return .bool_true, |
| 32790 | 33106 | else => return .bool_false, |
| ... | ... | @@ -32796,7 +33112,7 @@ fn cmpNumeric( |
| 32796 | 33112 | .lt, .lte => return if (lhs_val.isNegativeInf(mod)) .bool_true else .bool_false, |
| 32797 | 33113 | }; |
| 32798 | 33114 | if (!rhs_is_signed) { |
| 32799 | switch (lhs_val.orderAgainstZero(mod)) { | |
| 33115 | switch (lhs_val.orderAgainstZero(pt)) { | |
| 32800 | 33116 | .gt => {}, |
| 32801 | 33117 | .eq => switch (op) { // LHS = 0, RHS is unsigned |
| 32802 | 33118 | .lte => return .bool_true, |
| ... | ... | @@ -32818,7 +33134,7 @@ fn cmpNumeric( |
| 32818 | 33134 | } |
| 32819 | 33135 | } |
| 32820 | 33136 | |
| 32821 | var bigint = try float128IntPartToBigInt(sema.gpa, lhs_val.toFloat(f128, mod)); | |
| 33137 | var bigint = try float128IntPartToBigInt(sema.gpa, lhs_val.toFloat(f128, pt)); | |
| 32822 | 33138 | defer bigint.deinit(); |
| 32823 | 33139 | if (lhs_val.floatHasFraction(mod)) { |
| 32824 | 33140 | if (lhs_is_signed) { |
| ... | ... | @@ -32829,7 +33145,7 @@ fn cmpNumeric( |
| 32829 | 33145 | } |
| 32830 | 33146 | lhs_bits = bigint.toConst().bitCountTwosComp(); |
| 32831 | 33147 | } else { |
| 32832 | lhs_bits = lhs_val.intBitCountTwosComp(mod); | |
| 33148 | lhs_bits = lhs_val.intBitCountTwosComp(pt); | |
| 32833 | 33149 | } |
| 32834 | 33150 | lhs_bits += @intFromBool(!lhs_is_signed and dest_int_is_signed); |
| 32835 | 33151 | } else if (lhs_is_float) { |
| ... | ... | @@ -32842,7 +33158,7 @@ fn cmpNumeric( |
| 32842 | 33158 | var rhs_bits: usize = undefined; |
| 32843 | 33159 | if (try sema.resolveValueResolveLazy(rhs)) |rhs_val| { |
| 32844 | 33160 | if (rhs_val.isUndef(mod)) |
| 32845 | return mod.undefRef(Type.bool); | |
| 33161 | return pt.undefRef(Type.bool); | |
| 32846 | 33162 | if (rhs_val.isNan(mod)) switch (op) { |
| 32847 | 33163 | .neq => return .bool_true, |
| 32848 | 33164 | else => return .bool_false, |
| ... | ... | @@ -32854,7 +33170,7 @@ fn cmpNumeric( |
| 32854 | 33170 | .lt, .lte => return if (rhs_val.isNegativeInf(mod)) .bool_false else .bool_true, |
| 32855 | 33171 | }; |
| 32856 | 33172 | if (!lhs_is_signed) { |
| 32857 | switch (rhs_val.orderAgainstZero(mod)) { | |
| 33173 | switch (rhs_val.orderAgainstZero(pt)) { | |
| 32858 | 33174 | .gt => {}, |
| 32859 | 33175 | .eq => switch (op) { // RHS = 0, LHS is unsigned |
| 32860 | 33176 | .gte => return .bool_true, |
| ... | ... | @@ -32876,7 +33192,7 @@ fn cmpNumeric( |
| 32876 | 33192 | } |
| 32877 | 33193 | } |
| 32878 | 33194 | |
| 32879 | var bigint = try float128IntPartToBigInt(sema.gpa, rhs_val.toFloat(f128, mod)); | |
| 33195 | var bigint = try float128IntPartToBigInt(sema.gpa, rhs_val.toFloat(f128, pt)); | |
| 32880 | 33196 | defer bigint.deinit(); |
| 32881 | 33197 | if (rhs_val.floatHasFraction(mod)) { |
| 32882 | 33198 | if (rhs_is_signed) { |
| ... | ... | @@ -32887,7 +33203,7 @@ fn cmpNumeric( |
| 32887 | 33203 | } |
| 32888 | 33204 | rhs_bits = bigint.toConst().bitCountTwosComp(); |
| 32889 | 33205 | } else { |
| 32890 | rhs_bits = rhs_val.intBitCountTwosComp(mod); | |
| 33206 | rhs_bits = rhs_val.intBitCountTwosComp(pt); | |
| 32891 | 33207 | } |
| 32892 | 33208 | rhs_bits += @intFromBool(!rhs_is_signed and dest_int_is_signed); |
| 32893 | 33209 | } else if (rhs_is_float) { |
| ... | ... | @@ -32901,7 +33217,7 @@ fn cmpNumeric( |
| 32901 | 33217 | const max_bits = @max(lhs_bits, rhs_bits); |
| 32902 | 33218 | const casted_bits = std.math.cast(u16, max_bits) orelse return sema.fail(block, src, "{d} exceeds maximum integer bit count", .{max_bits}); |
| 32903 | 33219 | const signedness: std.builtin.Signedness = if (dest_int_is_signed) .signed else .unsigned; |
| 32904 | break :blk try mod.intType(signedness, casted_bits); | |
| 33220 | break :blk try pt.intType(signedness, casted_bits); | |
| 32905 | 33221 | }; |
| 32906 | 33222 | const casted_lhs = try sema.coerce(block, dest_ty, lhs, lhs_src); |
| 32907 | 33223 | const casted_rhs = try sema.coerce(block, dest_ty, rhs, rhs_src); |
| ... | ... | @@ -32920,9 +33236,10 @@ fn compareIntsOnlyPossibleResult( |
| 32920 | 33236 | op: std.math.CompareOperator, |
| 32921 | 33237 | rhs_ty: Type, |
| 32922 | 33238 | ) Allocator.Error!?bool { |
| 32923 | const mod = sema.mod; | |
| 33239 | const pt = sema.pt; | |
| 33240 | const mod = pt.zcu; | |
| 32924 | 33241 | const rhs_info = rhs_ty.intInfo(mod); |
| 32925 | const vs_zero = lhs_val.orderAgainstZeroAdvanced(mod, .sema) catch unreachable; | |
| 33242 | const vs_zero = lhs_val.orderAgainstZeroAdvanced(pt, .sema) catch unreachable; | |
| 32926 | 33243 | const is_zero = vs_zero == .eq; |
| 32927 | 33244 | const is_negative = vs_zero == .lt; |
| 32928 | 33245 | const is_positive = vs_zero == .gt; |
| ... | ... | @@ -32954,7 +33271,7 @@ fn compareIntsOnlyPossibleResult( |
| 32954 | 33271 | }; |
| 32955 | 33272 | |
| 32956 | 33273 | const sign_adj = @intFromBool(!is_negative and rhs_info.signedness == .signed); |
| 32957 | const req_bits = lhs_val.intBitCountTwosComp(mod) + sign_adj; | |
| 33274 | const req_bits = lhs_val.intBitCountTwosComp(pt) + sign_adj; | |
| 32958 | 33275 | |
| 32959 | 33276 | // No sized type can have more than 65535 bits. |
| 32960 | 33277 | // The RHS type operand is either a runtime value or sized (but undefined) constant. |
| ... | ... | @@ -32981,11 +33298,11 @@ fn compareIntsOnlyPossibleResult( |
| 32981 | 33298 | |
| 32982 | 33299 | if (req_bits != rhs_info.bits) break :edge .{ false, false }; |
| 32983 | 33300 | |
| 32984 | const ty = try mod.intType( | |
| 33301 | const ty = try pt.intType( | |
| 32985 | 33302 | if (is_negative) .signed else .unsigned, |
| 32986 | 33303 | @intCast(req_bits), |
| 32987 | 33304 | ); |
| 32988 | const pop_count = lhs_val.popCount(ty, mod); | |
| 33305 | const pop_count = lhs_val.popCount(ty, pt); | |
| 32989 | 33306 | |
| 32990 | 33307 | if (is_negative) { |
| 32991 | 33308 | break :edge .{ pop_count == 1, false }; |
| ... | ... | @@ -33015,7 +33332,8 @@ fn cmpVector( |
| 33015 | 33332 | lhs_src: LazySrcLoc, |
| 33016 | 33333 | rhs_src: LazySrcLoc, |
| 33017 | 33334 | ) CompileError!Air.Inst.Ref { |
| 33018 | const mod = sema.mod; | |
| 33335 | const pt = sema.pt; | |
| 33336 | const mod = pt.zcu; | |
| 33019 | 33337 | const lhs_ty = sema.typeOf(lhs); |
| 33020 | 33338 | const rhs_ty = sema.typeOf(rhs); |
| 33021 | 33339 | assert(lhs_ty.zigTypeTag(mod) == .Vector); |
| ... | ... | @@ -33026,7 +33344,7 @@ fn cmpVector( |
| 33026 | 33344 | const casted_lhs = try sema.coerce(block, resolved_ty, lhs, lhs_src); |
| 33027 | 33345 | const casted_rhs = try sema.coerce(block, resolved_ty, rhs, rhs_src); |
| 33028 | 33346 | |
| 33029 | const result_ty = try mod.vectorType(.{ | |
| 33347 | const result_ty = try pt.vectorType(.{ | |
| 33030 | 33348 | .len = lhs_ty.vectorLen(mod), |
| 33031 | 33349 | .child = .bool_type, |
| 33032 | 33350 | }); |
| ... | ... | @@ -33035,7 +33353,7 @@ fn cmpVector( |
| 33035 | 33353 | if (try sema.resolveValue(casted_lhs)) |lhs_val| { |
| 33036 | 33354 | if (try sema.resolveValue(casted_rhs)) |rhs_val| { |
| 33037 | 33355 | if (lhs_val.isUndef(mod) or rhs_val.isUndef(mod)) { |
| 33038 | return mod.undefRef(result_ty); | |
| 33356 | return pt.undefRef(result_ty); | |
| 33039 | 33357 | } |
| 33040 | 33358 | const cmp_val = try sema.compareVector(lhs_val, op, rhs_val, resolved_ty); |
| 33041 | 33359 | return Air.internedToRef(cmp_val.toIntern()); |
| ... | ... | @@ -33059,7 +33377,7 @@ fn wrapOptional( |
| 33059 | 33377 | inst_src: LazySrcLoc, |
| 33060 | 33378 | ) !Air.Inst.Ref { |
| 33061 | 33379 | if (try sema.resolveValue(inst)) |val| { |
| 33062 | return Air.internedToRef((try sema.mod.intern(.{ .opt = .{ | |
| 33380 | return Air.internedToRef((try sema.pt.intern(.{ .opt = .{ | |
| 33063 | 33381 | .ty = dest_ty.toIntern(), |
| 33064 | 33382 | .val = val.toIntern(), |
| 33065 | 33383 | } }))); |
| ... | ... | @@ -33076,11 +33394,12 @@ fn wrapErrorUnionPayload( |
| 33076 | 33394 | inst: Air.Inst.Ref, |
| 33077 | 33395 | inst_src: LazySrcLoc, |
| 33078 | 33396 | ) !Air.Inst.Ref { |
| 33079 | const mod = sema.mod; | |
| 33397 | const pt = sema.pt; | |
| 33398 | const mod = pt.zcu; | |
| 33080 | 33399 | const dest_payload_ty = dest_ty.errorUnionPayload(mod); |
| 33081 | 33400 | const coerced = try sema.coerceExtra(block, dest_payload_ty, inst, inst_src, .{ .report_err = false }); |
| 33082 | 33401 | if (try sema.resolveValue(coerced)) |val| { |
| 33083 | return Air.internedToRef((try mod.intern(.{ .error_union = .{ | |
| 33402 | return Air.internedToRef((try pt.intern(.{ .error_union = .{ | |
| 33084 | 33403 | .ty = dest_ty.toIntern(), |
| 33085 | 33404 | .val = .{ .payload = val.toIntern() }, |
| 33086 | 33405 | } }))); |
| ... | ... | @@ -33096,7 +33415,8 @@ fn wrapErrorUnionSet( |
| 33096 | 33415 | inst: Air.Inst.Ref, |
| 33097 | 33416 | inst_src: LazySrcLoc, |
| 33098 | 33417 | ) !Air.Inst.Ref { |
| 33099 | const mod = sema.mod; | |
| 33418 | const pt = sema.pt; | |
| 33419 | const mod = pt.zcu; | |
| 33100 | 33420 | const ip = &mod.intern_pool; |
| 33101 | 33421 | const inst_ty = sema.typeOf(inst); |
| 33102 | 33422 | const dest_err_set_ty = dest_ty.errorUnionSet(mod); |
| ... | ... | @@ -33140,7 +33460,7 @@ fn wrapErrorUnionSet( |
| 33140 | 33460 | else => unreachable, |
| 33141 | 33461 | }, |
| 33142 | 33462 | } |
| 33143 | return Air.internedToRef((try mod.intern(.{ .error_union = .{ | |
| 33463 | return Air.internedToRef((try pt.intern(.{ .error_union = .{ | |
| 33144 | 33464 | .ty = dest_ty.toIntern(), |
| 33145 | 33465 | .val = .{ .err_name = expected_name }, |
| 33146 | 33466 | } }))); |
| ... | ... | @@ -33158,14 +33478,15 @@ fn unionToTag( |
| 33158 | 33478 | un: Air.Inst.Ref, |
| 33159 | 33479 | un_src: LazySrcLoc, |
| 33160 | 33480 | ) !Air.Inst.Ref { |
| 33161 | const mod = sema.mod; | |
| 33481 | const pt = sema.pt; | |
| 33482 | const mod = pt.zcu; | |
| 33162 | 33483 | if ((try sema.typeHasOnePossibleValue(enum_ty))) |opv| { |
| 33163 | 33484 | return Air.internedToRef(opv.toIntern()); |
| 33164 | 33485 | } |
| 33165 | 33486 | if (try sema.resolveValue(un)) |un_val| { |
| 33166 | 33487 | const tag_val = un_val.unionTag(mod).?; |
| 33167 | 33488 | if (tag_val.isUndef(mod)) |
| 33168 | return try mod.undefRef(enum_ty); | |
| 33489 | return try pt.undefRef(enum_ty); | |
| 33169 | 33490 | return Air.internedToRef(tag_val.toIntern()); |
| 33170 | 33491 | } |
| 33171 | 33492 | try sema.requireRuntimeBlock(block, un_src, null); |
| ... | ... | @@ -33399,7 +33720,7 @@ const PeerResolveResult = union(enum) { |
| 33399 | 33720 | instructions: []const Air.Inst.Ref, |
| 33400 | 33721 | candidate_srcs: PeerTypeCandidateSrc, |
| 33401 | 33722 | ) !*Module.ErrorMsg { |
| 33402 | const mod = sema.mod; | |
| 33723 | const pt = sema.pt; | |
| 33403 | 33724 | |
| 33404 | 33725 | var opt_msg: ?*Module.ErrorMsg = null; |
| 33405 | 33726 | errdefer if (opt_msg) |msg| msg.destroy(sema.gpa); |
| ... | ... | @@ -33425,7 +33746,7 @@ const PeerResolveResult = union(enum) { |
| 33425 | 33746 | }, |
| 33426 | 33747 | .field_error => |field_error| { |
| 33427 | 33748 | const fmt = "struct field '{}' has conflicting types"; |
| 33428 | const args = .{field_error.field_name.fmt(&mod.intern_pool)}; | |
| 33749 | const args = .{field_error.field_name.fmt(&pt.zcu.intern_pool)}; | |
| 33429 | 33750 | if (opt_msg) |msg| { |
| 33430 | 33751 | try sema.errNote(src, msg, fmt, args); |
| 33431 | 33752 | } else { |
| ... | ... | @@ -33457,8 +33778,8 @@ const PeerResolveResult = union(enum) { |
| 33457 | 33778 | |
| 33458 | 33779 | const fmt = "incompatible types: '{}' and '{}'"; |
| 33459 | 33780 | const args = .{ |
| 33460 | conflict_tys[0].fmt(mod), | |
| 33461 | conflict_tys[1].fmt(mod), | |
| 33781 | conflict_tys[0].fmt(pt), | |
| 33782 | conflict_tys[1].fmt(pt), | |
| 33462 | 33783 | }; |
| 33463 | 33784 | const msg = if (opt_msg) |msg| msg: { |
| 33464 | 33785 | try sema.errNote(src, msg, fmt, args); |
| ... | ... | @@ -33469,8 +33790,8 @@ const PeerResolveResult = union(enum) { |
| 33469 | 33790 | break :msg msg; |
| 33470 | 33791 | }; |
| 33471 | 33792 | |
| 33472 | if (conflict_srcs[0]) |src_loc| try sema.errNote(src_loc, msg, "type '{}' here", .{conflict_tys[0].fmt(mod)}); | |
| 33473 | if (conflict_srcs[1]) |src_loc| try sema.errNote(src_loc, msg, "type '{}' here", .{conflict_tys[1].fmt(mod)}); | |
| 33793 | if (conflict_srcs[0]) |src_loc| try sema.errNote(src_loc, msg, "type '{}' here", .{conflict_tys[0].fmt(pt)}); | |
| 33794 | if (conflict_srcs[1]) |src_loc| try sema.errNote(src_loc, msg, "type '{}' here", .{conflict_tys[1].fmt(pt)}); | |
| 33474 | 33795 | |
| 33475 | 33796 | // No child error |
| 33476 | 33797 | break; |
| ... | ... | @@ -33517,7 +33838,8 @@ fn resolvePeerTypesInner( |
| 33517 | 33838 | peer_tys: []?Type, |
| 33518 | 33839 | peer_vals: []?Value, |
| 33519 | 33840 | ) !PeerResolveResult { |
| 33520 | const mod = sema.mod; | |
| 33841 | const pt = sema.pt; | |
| 33842 | const mod = pt.zcu; | |
| 33521 | 33843 | const ip = &mod.intern_pool; |
| 33522 | 33844 | |
| 33523 | 33845 | var strat_reason: usize = 0; |
| ... | ... | @@ -33581,7 +33903,7 @@ fn resolvePeerTypesInner( |
| 33581 | 33903 | .payload => |payload_ip| val_ptr.* = Value.fromInterned(payload_ip), |
| 33582 | 33904 | .err_name => val_ptr.* = null, |
| 33583 | 33905 | }, |
| 33584 | .undef => val_ptr.* = Value.fromInterned((try sema.mod.intern(.{ .undef = ty_ptr.*.?.toIntern() }))), | |
| 33906 | .undef => val_ptr.* = Value.fromInterned(try pt.intern(.{ .undef = ty_ptr.*.?.toIntern() })), | |
| 33585 | 33907 | else => unreachable, |
| 33586 | 33908 | }; |
| 33587 | 33909 | break :blk set_ty; |
| ... | ... | @@ -33604,7 +33926,7 @@ fn resolvePeerTypesInner( |
| 33604 | 33926 | .success => |ty| ty, |
| 33605 | 33927 | else => |result| return result, |
| 33606 | 33928 | }; |
| 33607 | return .{ .success = try mod.errorUnionType(final_set.?, final_payload) }; | |
| 33929 | return .{ .success = try pt.errorUnionType(final_set.?, final_payload) }; | |
| 33608 | 33930 | }, |
| 33609 | 33931 | |
| 33610 | 33932 | .nullable => { |
| ... | ... | @@ -33642,7 +33964,7 @@ fn resolvePeerTypesInner( |
| 33642 | 33964 | .success => |ty| ty, |
| 33643 | 33965 | else => |result| return result, |
| 33644 | 33966 | }; |
| 33645 | return .{ .success = try mod.optionalType(child_ty.toIntern()) }; | |
| 33967 | return .{ .success = try pt.optionalType(child_ty.toIntern()) }; | |
| 33646 | 33968 | }, |
| 33647 | 33969 | |
| 33648 | 33970 | .array => { |
| ... | ... | @@ -33730,7 +34052,7 @@ fn resolvePeerTypesInner( |
| 33730 | 34052 | // There should always be at least one array or vector peer |
| 33731 | 34053 | assert(opt_first_arr_idx != null); |
| 33732 | 34054 | |
| 33733 | return .{ .success = try mod.arrayType(.{ | |
| 34055 | return .{ .success = try pt.arrayType(.{ | |
| 33734 | 34056 | .len = len, |
| 33735 | 34057 | .child = elem_ty.toIntern(), |
| 33736 | 34058 | .sentinel = if (sentinel) |sent_val| sent_val.toIntern() else .none, |
| ... | ... | @@ -33792,7 +34114,7 @@ fn resolvePeerTypesInner( |
| 33792 | 34114 | else => |result| return result, |
| 33793 | 34115 | }; |
| 33794 | 34116 | |
| 33795 | return .{ .success = try mod.vectorType(.{ | |
| 34117 | return .{ .success = try pt.vectorType(.{ | |
| 33796 | 34118 | .len = @intCast(len.?), |
| 33797 | 34119 | .child = child_ty.toIntern(), |
| 33798 | 34120 | }) }; |
| ... | ... | @@ -33844,8 +34166,8 @@ fn resolvePeerTypesInner( |
| 33844 | 34166 | }).toIntern(); |
| 33845 | 34167 | |
| 33846 | 34168 | if (ptr_info.sentinel != .none and peer_info.sentinel != .none) { |
| 33847 | const peer_sent = try ip.getCoerced(sema.gpa, ptr_info.sentinel, ptr_info.child); | |
| 33848 | const ptr_sent = try ip.getCoerced(sema.gpa, peer_info.sentinel, ptr_info.child); | |
| 34169 | const peer_sent = try ip.getCoerced(sema.gpa, pt.tid, ptr_info.sentinel, ptr_info.child); | |
| 34170 | const ptr_sent = try ip.getCoerced(sema.gpa, pt.tid, peer_info.sentinel, ptr_info.child); | |
| 33849 | 34171 | if (ptr_sent == peer_sent) { |
| 33850 | 34172 | ptr_info.sentinel = ptr_sent; |
| 33851 | 34173 | } else { |
| ... | ... | @@ -33860,12 +34182,12 @@ fn resolvePeerTypesInner( |
| 33860 | 34182 | if (ptr_info.flags.alignment != .none) |
| 33861 | 34183 | ptr_info.flags.alignment |
| 33862 | 34184 | else |
| 33863 | Type.fromInterned(ptr_info.child).abiAlignment(mod), | |
| 34185 | Type.fromInterned(ptr_info.child).abiAlignment(pt), | |
| 33864 | 34186 | |
| 33865 | 34187 | if (peer_info.flags.alignment != .none) |
| 33866 | 34188 | peer_info.flags.alignment |
| 33867 | 34189 | else |
| 33868 | Type.fromInterned(peer_info.child).abiAlignment(mod), | |
| 34190 | Type.fromInterned(peer_info.child).abiAlignment(pt), | |
| 33869 | 34191 | ); |
| 33870 | 34192 | if (ptr_info.flags.address_space != peer_info.flags.address_space) { |
| 33871 | 34193 | return .{ .conflict = .{ |
| ... | ... | @@ -33888,7 +34210,7 @@ fn resolvePeerTypesInner( |
| 33888 | 34210 | |
| 33889 | 34211 | opt_ptr_info = ptr_info; |
| 33890 | 34212 | } |
| 33891 | return .{ .success = try mod.ptrTypeSema(opt_ptr_info.?) }; | |
| 34213 | return .{ .success = try pt.ptrTypeSema(opt_ptr_info.?) }; | |
| 33892 | 34214 | }, |
| 33893 | 34215 | |
| 33894 | 34216 | .ptr => { |
| ... | ... | @@ -34004,7 +34326,7 @@ fn resolvePeerTypesInner( |
| 34004 | 34326 | if (try sema.resolvePairInMemoryCoercible(block, src, cur_arr.elem_ty, peer_arr.elem_ty)) |elem_ty| { |
| 34005 | 34327 | // *[n:x]T + *[n:y]T = *[n]T |
| 34006 | 34328 | if (cur_arr.len == peer_arr.len) { |
| 34007 | ptr_info.child = (try mod.arrayType(.{ | |
| 34329 | ptr_info.child = (try pt.arrayType(.{ | |
| 34008 | 34330 | .len = cur_arr.len, |
| 34009 | 34331 | .child = elem_ty.toIntern(), |
| 34010 | 34332 | })).toIntern(); |
| ... | ... | @@ -34148,12 +34470,12 @@ fn resolvePeerTypesInner( |
| 34148 | 34470 | no_sentinel: { |
| 34149 | 34471 | if (peer_sentinel == .none) break :no_sentinel; |
| 34150 | 34472 | if (cur_sentinel == .none) break :no_sentinel; |
| 34151 | const peer_sent_coerced = try ip.getCoerced(sema.gpa, peer_sentinel, sentinel_ty); | |
| 34152 | const cur_sent_coerced = try ip.getCoerced(sema.gpa, cur_sentinel, sentinel_ty); | |
| 34473 | const peer_sent_coerced = try ip.getCoerced(sema.gpa, pt.tid, peer_sentinel, sentinel_ty); | |
| 34474 | const cur_sent_coerced = try ip.getCoerced(sema.gpa, pt.tid, cur_sentinel, sentinel_ty); | |
| 34153 | 34475 | if (peer_sent_coerced != cur_sent_coerced) break :no_sentinel; |
| 34154 | 34476 | // Sentinels match |
| 34155 | 34477 | if (ptr_info.flags.size == .One) switch (ip.indexToKey(ptr_info.child)) { |
| 34156 | .array_type => |array_type| ptr_info.child = (try mod.arrayType(.{ | |
| 34478 | .array_type => |array_type| ptr_info.child = (try pt.arrayType(.{ | |
| 34157 | 34479 | .len = array_type.len, |
| 34158 | 34480 | .child = array_type.child, |
| 34159 | 34481 | .sentinel = cur_sent_coerced, |
| ... | ... | @@ -34167,7 +34489,7 @@ fn resolvePeerTypesInner( |
| 34167 | 34489 | // Clear existing sentinel |
| 34168 | 34490 | ptr_info.sentinel = .none; |
| 34169 | 34491 | switch (ip.indexToKey(ptr_info.child)) { |
| 34170 | .array_type => |array_type| ptr_info.child = (try mod.arrayType(.{ | |
| 34492 | .array_type => |array_type| ptr_info.child = (try pt.arrayType(.{ | |
| 34171 | 34493 | .len = array_type.len, |
| 34172 | 34494 | .child = array_type.child, |
| 34173 | 34495 | .sentinel = .none, |
| ... | ... | @@ -34198,7 +34520,7 @@ fn resolvePeerTypesInner( |
| 34198 | 34520 | }, |
| 34199 | 34521 | } |
| 34200 | 34522 | |
| 34201 | return .{ .success = try mod.ptrTypeSema(opt_ptr_info.?) }; | |
| 34523 | return .{ .success = try pt.ptrTypeSema(opt_ptr_info.?) }; | |
| 34202 | 34524 | }, |
| 34203 | 34525 | |
| 34204 | 34526 | .func => { |
| ... | ... | @@ -34517,7 +34839,7 @@ fn resolvePeerTypesInner( |
| 34517 | 34839 | continue; |
| 34518 | 34840 | }; |
| 34519 | 34841 | peer_field_ty.* = ty.structFieldType(field_index, mod); |
| 34520 | peer_field_val.* = if (opt_val) |val| try val.fieldValue(mod, field_index) else null; | |
| 34842 | peer_field_val.* = if (opt_val) |val| try val.fieldValue(pt, field_index) else null; | |
| 34521 | 34843 | } |
| 34522 | 34844 | |
| 34523 | 34845 | // Resolve field type recursively |
| ... | ... | @@ -34555,9 +34877,9 @@ fn resolvePeerTypesInner( |
| 34555 | 34877 | var comptime_val: ?Value = null; |
| 34556 | 34878 | for (peer_tys) |opt_ty| { |
| 34557 | 34879 | const struct_ty = opt_ty orelse continue; |
| 34558 | try struct_ty.resolveStructFieldInits(mod); | |
| 34880 | try struct_ty.resolveStructFieldInits(pt); | |
| 34559 | 34881 | |
| 34560 | const uncoerced_field_val = try struct_ty.structFieldValueComptime(mod, field_index) orelse { | |
| 34882 | const uncoerced_field_val = try struct_ty.structFieldValueComptime(pt, field_index) orelse { | |
| 34561 | 34883 | comptime_val = null; |
| 34562 | 34884 | break; |
| 34563 | 34885 | }; |
| ... | ... | @@ -34584,7 +34906,7 @@ fn resolvePeerTypesInner( |
| 34584 | 34906 | field_val.* = if (comptime_val) |v| v.toIntern() else .none; |
| 34585 | 34907 | } |
| 34586 | 34908 | |
| 34587 | const final_ty = try ip.getAnonStructType(mod.gpa, .{ | |
| 34909 | const final_ty = try ip.getAnonStructType(mod.gpa, pt.tid, .{ | |
| 34588 | 34910 | .types = field_types, |
| 34589 | 34911 | .names = if (is_tuple) &.{} else field_names, |
| 34590 | 34912 | .values = field_vals, |
| ... | ... | @@ -34628,13 +34950,15 @@ fn maybeMergeErrorSets(sema: *Sema, block: *Block, src: LazySrcLoc, e0: Type, e1 |
| 34628 | 34950 | } |
| 34629 | 34951 | |
| 34630 | 34952 | fn resolvePairInMemoryCoercible(sema: *Sema, block: *Block, src: LazySrcLoc, ty_a: Type, ty_b: Type) !?Type { |
| 34953 | const target = sema.pt.zcu.getTarget(); | |
| 34954 | ||
| 34631 | 34955 | // ty_b -> ty_a |
| 34632 | if (.ok == try sema.coerceInMemoryAllowed(block, ty_a, ty_b, true, sema.mod.getTarget(), src, src)) { | |
| 34956 | if (.ok == try sema.coerceInMemoryAllowed(block, ty_a, ty_b, true, target, src, src)) { | |
| 34633 | 34957 | return ty_a; |
| 34634 | 34958 | } |
| 34635 | 34959 | |
| 34636 | 34960 | // ty_a -> ty_b |
| 34637 | if (.ok == try sema.coerceInMemoryAllowed(block, ty_b, ty_a, true, sema.mod.getTarget(), src, src)) { | |
| 34961 | if (.ok == try sema.coerceInMemoryAllowed(block, ty_b, ty_a, true, target, src, src)) { | |
| 34638 | 34962 | return ty_b; |
| 34639 | 34963 | } |
| 34640 | 34964 | |
| ... | ... | @@ -34647,7 +34971,8 @@ const ArrayLike = struct { |
| 34647 | 34971 | elem_ty: Type, |
| 34648 | 34972 | }; |
| 34649 | 34973 | fn typeIsArrayLike(sema: *Sema, ty: Type) ?ArrayLike { |
| 34650 | const mod = sema.mod; | |
| 34974 | const pt = sema.pt; | |
| 34975 | const mod = pt.zcu; | |
| 34651 | 34976 | return switch (ty.zigTypeTag(mod)) { |
| 34652 | 34977 | .Array => .{ |
| 34653 | 34978 | .len = ty.arrayLen(mod), |
| ... | ... | @@ -34676,7 +35001,8 @@ fn typeIsArrayLike(sema: *Sema, ty: Type) ?ArrayLike { |
| 34676 | 35001 | } |
| 34677 | 35002 | |
| 34678 | 35003 | pub fn resolveIes(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError!void { |
| 34679 | const mod = sema.mod; | |
| 35004 | const pt = sema.pt; | |
| 35005 | const mod = pt.zcu; | |
| 34680 | 35006 | const ip = &mod.intern_pool; |
| 34681 | 35007 | |
| 34682 | 35008 | if (sema.fn_ret_ty_ies) |ies| { |
| ... | ... | @@ -34687,26 +35013,27 @@ pub fn resolveIes(sema: *Sema, block: *Block, src: LazySrcLoc) CompileError!void |
| 34687 | 35013 | } |
| 34688 | 35014 | |
| 34689 | 35015 | pub fn resolveFnTypes(sema: *Sema, fn_ty: Type) CompileError!void { |
| 34690 | const mod = sema.mod; | |
| 35016 | const pt = sema.pt; | |
| 35017 | const mod = pt.zcu; | |
| 34691 | 35018 | const ip = &mod.intern_pool; |
| 34692 | 35019 | const fn_ty_info = mod.typeToFunc(fn_ty).?; |
| 34693 | 35020 | |
| 34694 | try Type.fromInterned(fn_ty_info.return_type).resolveFully(mod); | |
| 35021 | try Type.fromInterned(fn_ty_info.return_type).resolveFully(pt); | |
| 34695 | 35022 | |
| 34696 | 35023 | if (mod.comp.config.any_error_tracing and |
| 34697 | 35024 | Type.fromInterned(fn_ty_info.return_type).isError(mod)) |
| 34698 | 35025 | { |
| 34699 | 35026 | // Ensure the type exists so that backends can assume that. |
| 34700 | _ = try mod.getBuiltinType("StackTrace"); | |
| 35027 | _ = try pt.getBuiltinType("StackTrace"); | |
| 34701 | 35028 | } |
| 34702 | 35029 | |
| 34703 | 35030 | for (0..fn_ty_info.param_types.len) |i| { |
| 34704 | try Type.fromInterned(fn_ty_info.param_types.get(ip)[i]).resolveFully(mod); | |
| 35031 | try Type.fromInterned(fn_ty_info.param_types.get(ip)[i]).resolveFully(pt); | |
| 34705 | 35032 | } |
| 34706 | 35033 | } |
| 34707 | 35034 | |
| 34708 | 35035 | fn resolveLazyValue(sema: *Sema, val: Value) CompileError!Value { |
| 34709 | return val.resolveLazy(sema.arena, sema.mod); | |
| 35036 | return val.resolveLazy(sema.arena, sema.pt); | |
| 34710 | 35037 | } |
| 34711 | 35038 | |
| 34712 | 35039 | /// Resolve a struct's alignment only without triggering resolution of its layout. |
| ... | ... | @@ -34716,7 +35043,8 @@ pub fn resolveStructAlignment( |
| 34716 | 35043 | ty: InternPool.Index, |
| 34717 | 35044 | struct_type: InternPool.LoadedStructType, |
| 34718 | 35045 | ) SemaError!void { |
| 34719 | const mod = sema.mod; | |
| 35046 | const pt = sema.pt; | |
| 35047 | const mod = pt.zcu; | |
| 34720 | 35048 | const ip = &mod.intern_pool; |
| 34721 | 35049 | const target = mod.getTarget(); |
| 34722 | 35050 | |
| ... | ... | @@ -34754,7 +35082,7 @@ pub fn resolveStructAlignment( |
| 34754 | 35082 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[i]); |
| 34755 | 35083 | if (struct_type.fieldIsComptime(ip, i) or try sema.typeRequiresComptime(field_ty)) |
| 34756 | 35084 | continue; |
| 34757 | const field_align = try mod.structFieldAlignmentAdvanced( | |
| 35085 | const field_align = try pt.structFieldAlignmentAdvanced( | |
| 34758 | 35086 | struct_type.fieldAlign(ip, i), |
| 34759 | 35087 | field_ty, |
| 34760 | 35088 | struct_type.layout, |
| ... | ... | @@ -34767,7 +35095,8 @@ pub fn resolveStructAlignment( |
| 34767 | 35095 | } |
| 34768 | 35096 | |
| 34769 | 35097 | pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void { |
| 34770 | const zcu = sema.mod; | |
| 35098 | const pt = sema.pt; | |
| 35099 | const zcu = pt.zcu; | |
| 34771 | 35100 | const ip = &zcu.intern_pool; |
| 34772 | 35101 | const struct_type = zcu.typeToStruct(ty) orelse return; |
| 34773 | 35102 | |
| ... | ... | @@ -34776,10 +35105,10 @@ pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void { |
| 34776 | 35105 | if (struct_type.haveLayout(ip)) |
| 34777 | 35106 | return; |
| 34778 | 35107 | |
| 34779 | try ty.resolveFields(zcu); | |
| 35108 | try ty.resolveFields(pt); | |
| 34780 | 35109 | |
| 34781 | 35110 | if (struct_type.layout == .@"packed") { |
| 34782 | semaBackingIntType(zcu, struct_type) catch |err| switch (err) { | |
| 35111 | semaBackingIntType(pt, struct_type) catch |err| switch (err) { | |
| 34783 | 35112 | error.OutOfMemory, error.AnalysisFail => |e| return e, |
| 34784 | 35113 | error.ComptimeBreak, error.ComptimeReturn, error.GenericPoison => unreachable, |
| 34785 | 35114 | }; |
| ... | ... | @@ -34790,7 +35119,7 @@ pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void { |
| 34790 | 35119 | const msg = try sema.errMsg( |
| 34791 | 35120 | ty.srcLoc(zcu), |
| 34792 | 35121 | "struct '{}' depends on itself", |
| 34793 | .{ty.fmt(zcu)}, | |
| 35122 | .{ty.fmt(pt)}, | |
| 34794 | 35123 | ); |
| 34795 | 35124 | return sema.failWithOwnedErrorMsg(null, msg); |
| 34796 | 35125 | } |
| ... | ... | @@ -34818,7 +35147,7 @@ pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void { |
| 34818 | 35147 | }, |
| 34819 | 35148 | else => return err, |
| 34820 | 35149 | }; |
| 34821 | field_align.* = try zcu.structFieldAlignmentAdvanced( | |
| 35150 | field_align.* = try pt.structFieldAlignmentAdvanced( | |
| 34822 | 35151 | struct_type.fieldAlign(ip, i), |
| 34823 | 35152 | field_ty, |
| 34824 | 35153 | struct_type.layout, |
| ... | ... | @@ -34911,7 +35240,8 @@ pub fn resolveStructLayout(sema: *Sema, ty: Type) SemaError!void { |
| 34911 | 35240 | _ = try sema.typeRequiresComptime(ty); |
| 34912 | 35241 | } |
| 34913 | 35242 | |
| 34914 | fn semaBackingIntType(zcu: *Zcu, struct_type: InternPool.LoadedStructType) CompileError!void { | |
| 35243 | fn semaBackingIntType(pt: Zcu.PerThread, struct_type: InternPool.LoadedStructType) CompileError!void { | |
| 35244 | const zcu = pt.zcu; | |
| 34915 | 35245 | const gpa = zcu.gpa; |
| 34916 | 35246 | const ip = &zcu.intern_pool; |
| 34917 | 35247 | |
| ... | ... | @@ -34927,7 +35257,7 @@ fn semaBackingIntType(zcu: *Zcu, struct_type: InternPool.LoadedStructType) Compi |
| 34927 | 35257 | defer comptime_err_ret_trace.deinit(); |
| 34928 | 35258 | |
| 34929 | 35259 | var sema: Sema = .{ |
| 34930 | .mod = zcu, | |
| 35260 | .pt = pt, | |
| 34931 | 35261 | .gpa = gpa, |
| 34932 | 35262 | .arena = analysis_arena.allocator(), |
| 34933 | 35263 | .code = zir, |
| ... | ... | @@ -34958,7 +35288,7 @@ fn semaBackingIntType(zcu: *Zcu, struct_type: InternPool.LoadedStructType) Compi |
| 34958 | 35288 | var accumulator: u64 = 0; |
| 34959 | 35289 | for (0..struct_type.field_types.len) |i| { |
| 34960 | 35290 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[i]); |
| 34961 | accumulator += try field_ty.bitSizeAdvanced(zcu, .sema); | |
| 35291 | accumulator += try field_ty.bitSizeAdvanced(pt, .sema); | |
| 34962 | 35292 | } |
| 34963 | 35293 | break :blk accumulator; |
| 34964 | 35294 | }; |
| ... | ... | @@ -35004,7 +35334,7 @@ fn semaBackingIntType(zcu: *Zcu, struct_type: InternPool.LoadedStructType) Compi |
| 35004 | 35334 | if (fields_bit_sum > std.math.maxInt(u16)) { |
| 35005 | 35335 | return sema.fail(&block, block.nodeOffset(0), "size of packed struct '{d}' exceeds maximum bit width of 65535", .{fields_bit_sum}); |
| 35006 | 35336 | } |
| 35007 | const backing_int_ty = try zcu.intType(.unsigned, @intCast(fields_bit_sum)); | |
| 35337 | const backing_int_ty = try pt.intType(.unsigned, @intCast(fields_bit_sum)); | |
| 35008 | 35338 | struct_type.backingIntType(ip).* = backing_int_ty.toIntern(); |
| 35009 | 35339 | } |
| 35010 | 35340 | |
| ... | ... | @@ -35012,26 +35342,27 @@ fn semaBackingIntType(zcu: *Zcu, struct_type: InternPool.LoadedStructType) Compi |
| 35012 | 35342 | } |
| 35013 | 35343 | |
| 35014 | 35344 | fn checkBackingIntType(sema: *Sema, block: *Block, src: LazySrcLoc, backing_int_ty: Type, fields_bit_sum: u64) CompileError!void { |
| 35015 | const mod = sema.mod; | |
| 35345 | const pt = sema.pt; | |
| 35346 | const mod = pt.zcu; | |
| 35016 | 35347 | |
| 35017 | 35348 | if (!backing_int_ty.isInt(mod)) { |
| 35018 | return sema.fail(block, src, "expected backing integer type, found '{}'", .{backing_int_ty.fmt(sema.mod)}); | |
| 35349 | return sema.fail(block, src, "expected backing integer type, found '{}'", .{backing_int_ty.fmt(pt)}); | |
| 35019 | 35350 | } |
| 35020 | if (backing_int_ty.bitSize(mod) != fields_bit_sum) { | |
| 35351 | if (backing_int_ty.bitSize(pt) != fields_bit_sum) { | |
| 35021 | 35352 | return sema.fail( |
| 35022 | 35353 | block, |
| 35023 | 35354 | src, |
| 35024 | 35355 | "backing integer type '{}' has bit size {} but the struct fields have a total bit size of {}", |
| 35025 | .{ backing_int_ty.fmt(sema.mod), backing_int_ty.bitSize(mod), fields_bit_sum }, | |
| 35356 | .{ backing_int_ty.fmt(pt), backing_int_ty.bitSize(pt), fields_bit_sum }, | |
| 35026 | 35357 | ); |
| 35027 | 35358 | } |
| 35028 | 35359 | } |
| 35029 | 35360 | |
| 35030 | 35361 | fn checkIndexable(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void { |
| 35031 | const mod = sema.mod; | |
| 35032 | if (!ty.isIndexable(mod)) { | |
| 35362 | const pt = sema.pt; | |
| 35363 | if (!ty.isIndexable(pt.zcu)) { | |
| 35033 | 35364 | const msg = msg: { |
| 35034 | const msg = try sema.errMsg(src, "type '{}' does not support indexing", .{ty.fmt(sema.mod)}); | |
| 35365 | const msg = try sema.errMsg(src, "type '{}' does not support indexing", .{ty.fmt(pt)}); | |
| 35035 | 35366 | errdefer msg.destroy(sema.gpa); |
| 35036 | 35367 | try sema.errNote(src, msg, "operand must be an array, slice, tuple, or vector", .{}); |
| 35037 | 35368 | break :msg msg; |
| ... | ... | @@ -35041,7 +35372,8 @@ fn checkIndexable(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void { |
| 35041 | 35372 | } |
| 35042 | 35373 | |
| 35043 | 35374 | fn checkMemOperand(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void { |
| 35044 | const mod = sema.mod; | |
| 35375 | const pt = sema.pt; | |
| 35376 | const mod = pt.zcu; | |
| 35045 | 35377 | if (ty.zigTypeTag(mod) == .Pointer) { |
| 35046 | 35378 | switch (ty.ptrSize(mod)) { |
| 35047 | 35379 | .Slice, .Many, .C => return, |
| ... | ... | @@ -35054,7 +35386,7 @@ fn checkMemOperand(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !void |
| 35054 | 35386 | } |
| 35055 | 35387 | } |
| 35056 | 35388 | const msg = msg: { |
| 35057 | const msg = try sema.errMsg(src, "type '{}' is not an indexable pointer", .{ty.fmt(sema.mod)}); | |
| 35389 | const msg = try sema.errMsg(src, "type '{}' is not an indexable pointer", .{ty.fmt(pt)}); | |
| 35058 | 35390 | errdefer msg.destroy(sema.gpa); |
| 35059 | 35391 | try sema.errNote(src, msg, "operand must be a slice, a many pointer or a pointer to an array", .{}); |
| 35060 | 35392 | break :msg msg; |
| ... | ... | @@ -35069,9 +35401,9 @@ pub fn resolveUnionAlignment( |
| 35069 | 35401 | ty: Type, |
| 35070 | 35402 | union_type: InternPool.LoadedUnionType, |
| 35071 | 35403 | ) SemaError!void { |
| 35072 | const mod = sema.mod; | |
| 35073 | const ip = &mod.intern_pool; | |
| 35074 | const target = mod.getTarget(); | |
| 35404 | const zcu = sema.pt.zcu; | |
| 35405 | const ip = &zcu.intern_pool; | |
| 35406 | const target = zcu.getTarget(); | |
| 35075 | 35407 | |
| 35076 | 35408 | assert(sema.ownerUnit().unwrap().decl == union_type.decl); |
| 35077 | 35409 | |
| ... | ... | @@ -35108,8 +35440,8 @@ pub fn resolveUnionAlignment( |
| 35108 | 35440 | |
| 35109 | 35441 | /// This logic must be kept in sync with `Module.getUnionLayout`. |
| 35110 | 35442 | pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void { |
| 35111 | const zcu = sema.mod; | |
| 35112 | const ip = &zcu.intern_pool; | |
| 35443 | const pt = sema.pt; | |
| 35444 | const ip = &pt.zcu.intern_pool; | |
| 35113 | 35445 | |
| 35114 | 35446 | try sema.resolveTypeFieldsUnion(ty, ip.loadUnionType(ty.ip_index)); |
| 35115 | 35447 | |
| ... | ... | @@ -35122,9 +35454,9 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void { |
| 35122 | 35454 | .none, .have_field_types => {}, |
| 35123 | 35455 | .field_types_wip, .layout_wip => { |
| 35124 | 35456 | const msg = try sema.errMsg( |
| 35125 | ty.srcLoc(zcu), | |
| 35457 | ty.srcLoc(pt.zcu), | |
| 35126 | 35458 | "union '{}' depends on itself", |
| 35127 | .{ty.fmt(zcu)}, | |
| 35459 | .{ty.fmt(pt)}, | |
| 35128 | 35460 | ); |
| 35129 | 35461 | return sema.failWithOwnedErrorMsg(null, msg); |
| 35130 | 35462 | }, |
| ... | ... | @@ -35143,7 +35475,7 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void { |
| 35143 | 35475 | for (0..union_type.field_types.len) |field_index| { |
| 35144 | 35476 | const field_ty = Type.fromInterned(union_type.field_types.get(ip)[field_index]); |
| 35145 | 35477 | |
| 35146 | if (try sema.typeRequiresComptime(field_ty) or field_ty.zigTypeTag(zcu) == .NoReturn) continue; // TODO: should this affect alignment? | |
| 35478 | if (try sema.typeRequiresComptime(field_ty) or field_ty.zigTypeTag(pt.zcu) == .NoReturn) continue; // TODO: should this affect alignment? | |
| 35147 | 35479 | |
| 35148 | 35480 | max_size = @max(max_size, sema.typeAbiSize(field_ty) catch |err| switch (err) { |
| 35149 | 35481 | error.AnalysisFail => { |
| ... | ... | @@ -35185,7 +35517,7 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void { |
| 35185 | 35517 | } else { |
| 35186 | 35518 | // {Payload, Tag} |
| 35187 | 35519 | size += max_size; |
| 35188 | size = switch (zcu.getTarget().ofmt) { | |
| 35520 | size = switch (pt.zcu.getTarget().ofmt) { | |
| 35189 | 35521 | .c => max_align, |
| 35190 | 35522 | else => tag_align, |
| 35191 | 35523 | }.forward(size); |
| ... | ... | @@ -35205,7 +35537,7 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void { |
| 35205 | 35537 | |
| 35206 | 35538 | if (union_type.flagsPtr(ip).assumed_runtime_bits and !(try sema.typeHasRuntimeBits(ty))) { |
| 35207 | 35539 | const msg = try sema.errMsg( |
| 35208 | ty.srcLoc(zcu), | |
| 35540 | ty.srcLoc(pt.zcu), | |
| 35209 | 35541 | "union layout depends on it having runtime bits", |
| 35210 | 35542 | .{}, |
| 35211 | 35543 | ); |
| ... | ... | @@ -35213,10 +35545,10 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void { |
| 35213 | 35545 | } |
| 35214 | 35546 | |
| 35215 | 35547 | if (union_type.flagsPtr(ip).assumed_pointer_aligned and |
| 35216 | alignment.compareStrict(.neq, Alignment.fromByteUnits(@divExact(zcu.getTarget().ptrBitWidth(), 8)))) | |
| 35548 | alignment.compareStrict(.neq, Alignment.fromByteUnits(@divExact(pt.zcu.getTarget().ptrBitWidth(), 8)))) | |
| 35217 | 35549 | { |
| 35218 | 35550 | const msg = try sema.errMsg( |
| 35219 | ty.srcLoc(zcu), | |
| 35551 | ty.srcLoc(pt.zcu), | |
| 35220 | 35552 | "union layout depends on being pointer aligned", |
| 35221 | 35553 | .{}, |
| 35222 | 35554 | ); |
| ... | ... | @@ -35229,7 +35561,8 @@ pub fn resolveUnionLayout(sema: *Sema, ty: Type) SemaError!void { |
| 35229 | 35561 | pub fn resolveStructFully(sema: *Sema, ty: Type) SemaError!void { |
| 35230 | 35562 | try sema.resolveStructLayout(ty); |
| 35231 | 35563 | |
| 35232 | const mod = sema.mod; | |
| 35564 | const pt = sema.pt; | |
| 35565 | const mod = pt.zcu; | |
| 35233 | 35566 | const ip = &mod.intern_pool; |
| 35234 | 35567 | const struct_type = mod.typeToStruct(ty).?; |
| 35235 | 35568 | |
| ... | ... | @@ -35244,14 +35577,15 @@ pub fn resolveStructFully(sema: *Sema, ty: Type) SemaError!void { |
| 35244 | 35577 | |
| 35245 | 35578 | for (0..struct_type.field_types.len) |i| { |
| 35246 | 35579 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[i]); |
| 35247 | try field_ty.resolveFully(mod); | |
| 35580 | try field_ty.resolveFully(pt); | |
| 35248 | 35581 | } |
| 35249 | 35582 | } |
| 35250 | 35583 | |
| 35251 | 35584 | pub fn resolveUnionFully(sema: *Sema, ty: Type) SemaError!void { |
| 35252 | 35585 | try sema.resolveUnionLayout(ty); |
| 35253 | 35586 | |
| 35254 | const mod = sema.mod; | |
| 35587 | const pt = sema.pt; | |
| 35588 | const mod = pt.zcu; | |
| 35255 | 35589 | const ip = &mod.intern_pool; |
| 35256 | 35590 | const union_obj = mod.typeToUnion(ty).?; |
| 35257 | 35591 | |
| ... | ... | @@ -35272,7 +35606,7 @@ pub fn resolveUnionFully(sema: *Sema, ty: Type) SemaError!void { |
| 35272 | 35606 | union_obj.flagsPtr(ip).status = .fully_resolved_wip; |
| 35273 | 35607 | for (0..union_obj.field_types.len) |field_index| { |
| 35274 | 35608 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 35275 | try field_ty.resolveFully(mod); | |
| 35609 | try field_ty.resolveFully(pt); | |
| 35276 | 35610 | } |
| 35277 | 35611 | union_obj.flagsPtr(ip).status = .fully_resolved; |
| 35278 | 35612 | } |
| ... | ... | @@ -35286,7 +35620,8 @@ pub fn resolveTypeFieldsStruct( |
| 35286 | 35620 | ty: InternPool.Index, |
| 35287 | 35621 | struct_type: InternPool.LoadedStructType, |
| 35288 | 35622 | ) SemaError!void { |
| 35289 | const zcu = sema.mod; | |
| 35623 | const pt = sema.pt; | |
| 35624 | const zcu = pt.zcu; | |
| 35290 | 35625 | const ip = &zcu.intern_pool; |
| 35291 | 35626 | // If there is no owner decl it means the struct has no fields. |
| 35292 | 35627 | const owner_decl = struct_type.decl.unwrap() orelse return; |
| ... | ... | @@ -35310,13 +35645,13 @@ pub fn resolveTypeFieldsStruct( |
| 35310 | 35645 | const msg = try sema.errMsg( |
| 35311 | 35646 | Type.fromInterned(ty).srcLoc(zcu), |
| 35312 | 35647 | "struct '{}' depends on itself", |
| 35313 | .{Type.fromInterned(ty).fmt(zcu)}, | |
| 35648 | .{Type.fromInterned(ty).fmt(pt)}, | |
| 35314 | 35649 | ); |
| 35315 | 35650 | return sema.failWithOwnedErrorMsg(null, msg); |
| 35316 | 35651 | } |
| 35317 | 35652 | defer struct_type.clearTypesWip(ip); |
| 35318 | 35653 | |
| 35319 | semaStructFields(zcu, sema.arena, struct_type) catch |err| switch (err) { | |
| 35654 | semaStructFields(pt, sema.arena, struct_type) catch |err| switch (err) { | |
| 35320 | 35655 | error.AnalysisFail => { |
| 35321 | 35656 | if (zcu.declPtr(owner_decl).analysis == .complete) { |
| 35322 | 35657 | zcu.declPtr(owner_decl).analysis = .dependency_failure; |
| ... | ... | @@ -35329,7 +35664,8 @@ pub fn resolveTypeFieldsStruct( |
| 35329 | 35664 | } |
| 35330 | 35665 | |
| 35331 | 35666 | pub fn resolveStructFieldInits(sema: *Sema, ty: Type) SemaError!void { |
| 35332 | const zcu = sema.mod; | |
| 35667 | const pt = sema.pt; | |
| 35668 | const zcu = pt.zcu; | |
| 35333 | 35669 | const ip = &zcu.intern_pool; |
| 35334 | 35670 | const struct_type = zcu.typeToStruct(ty) orelse return; |
| 35335 | 35671 | const owner_decl = struct_type.decl.unwrap() orelse return; |
| ... | ... | @@ -35345,13 +35681,13 @@ pub fn resolveStructFieldInits(sema: *Sema, ty: Type) SemaError!void { |
| 35345 | 35681 | const msg = try sema.errMsg( |
| 35346 | 35682 | ty.srcLoc(zcu), |
| 35347 | 35683 | "struct '{}' depends on itself", |
| 35348 | .{ty.fmt(zcu)}, | |
| 35684 | .{ty.fmt(pt)}, | |
| 35349 | 35685 | ); |
| 35350 | 35686 | return sema.failWithOwnedErrorMsg(null, msg); |
| 35351 | 35687 | } |
| 35352 | 35688 | defer struct_type.clearInitsWip(ip); |
| 35353 | 35689 | |
| 35354 | semaStructFieldInits(zcu, sema.arena, struct_type) catch |err| switch (err) { | |
| 35690 | semaStructFieldInits(pt, sema.arena, struct_type) catch |err| switch (err) { | |
| 35355 | 35691 | error.AnalysisFail => { |
| 35356 | 35692 | if (zcu.declPtr(owner_decl).analysis == .complete) { |
| 35357 | 35693 | zcu.declPtr(owner_decl).analysis = .dependency_failure; |
| ... | ... | @@ -35365,7 +35701,8 @@ pub fn resolveStructFieldInits(sema: *Sema, ty: Type) SemaError!void { |
| 35365 | 35701 | } |
| 35366 | 35702 | |
| 35367 | 35703 | pub fn resolveTypeFieldsUnion(sema: *Sema, ty: Type, union_type: InternPool.LoadedUnionType) SemaError!void { |
| 35368 | const zcu = sema.mod; | |
| 35704 | const pt = sema.pt; | |
| 35705 | const zcu = pt.zcu; | |
| 35369 | 35706 | const ip = &zcu.intern_pool; |
| 35370 | 35707 | const owner_decl = zcu.declPtr(union_type.decl); |
| 35371 | 35708 | |
| ... | ... | @@ -35387,7 +35724,7 @@ pub fn resolveTypeFieldsUnion(sema: *Sema, ty: Type, union_type: InternPool.Load |
| 35387 | 35724 | const msg = try sema.errMsg( |
| 35388 | 35725 | ty.srcLoc(zcu), |
| 35389 | 35726 | "union '{}' depends on itself", |
| 35390 | .{ty.fmt(zcu)}, | |
| 35727 | .{ty.fmt(pt)}, | |
| 35391 | 35728 | ); |
| 35392 | 35729 | return sema.failWithOwnedErrorMsg(null, msg); |
| 35393 | 35730 | }, |
| ... | ... | @@ -35401,7 +35738,7 @@ pub fn resolveTypeFieldsUnion(sema: *Sema, ty: Type, union_type: InternPool.Load |
| 35401 | 35738 | |
| 35402 | 35739 | union_type.flagsPtr(ip).status = .field_types_wip; |
| 35403 | 35740 | errdefer union_type.flagsPtr(ip).status = .none; |
| 35404 | semaUnionFields(zcu, sema.arena, union_type) catch |err| switch (err) { | |
| 35741 | semaUnionFields(pt, sema.arena, union_type) catch |err| switch (err) { | |
| 35405 | 35742 | error.AnalysisFail => { |
| 35406 | 35743 | if (owner_decl.analysis == .complete) { |
| 35407 | 35744 | owner_decl.analysis = .dependency_failure; |
| ... | ... | @@ -35422,7 +35759,8 @@ fn resolveInferredErrorSet( |
| 35422 | 35759 | src: LazySrcLoc, |
| 35423 | 35760 | ies_index: InternPool.Index, |
| 35424 | 35761 | ) CompileError!InternPool.Index { |
| 35425 | const mod = sema.mod; | |
| 35762 | const pt = sema.pt; | |
| 35763 | const mod = pt.zcu; | |
| 35426 | 35764 | const ip = &mod.intern_pool; |
| 35427 | 35765 | const func_index = ip.iesFuncIndex(ies_index); |
| 35428 | 35766 | const func = mod.funcInfo(func_index); |
| ... | ... | @@ -35482,8 +35820,8 @@ pub fn resolveInferredErrorSetPtr( |
| 35482 | 35820 | src: LazySrcLoc, |
| 35483 | 35821 | ies: *InferredErrorSet, |
| 35484 | 35822 | ) CompileError!void { |
| 35485 | const mod = sema.mod; | |
| 35486 | const ip = &mod.intern_pool; | |
| 35823 | const pt = sema.pt; | |
| 35824 | const ip = &pt.zcu.intern_pool; | |
| 35487 | 35825 | |
| 35488 | 35826 | if (ies.resolved != .none) return; |
| 35489 | 35827 | |
| ... | ... | @@ -35505,7 +35843,7 @@ pub fn resolveInferredErrorSetPtr( |
| 35505 | 35843 | } |
| 35506 | 35844 | } |
| 35507 | 35845 | |
| 35508 | const resolved_error_set_ty = try mod.errorSetFromUnsortedNames(ies.errors.keys()); | |
| 35846 | const resolved_error_set_ty = try pt.errorSetFromUnsortedNames(ies.errors.keys()); | |
| 35509 | 35847 | ies.resolved = resolved_error_set_ty.toIntern(); |
| 35510 | 35848 | } |
| 35511 | 35849 | |
| ... | ... | @@ -35515,12 +35853,13 @@ fn resolveAdHocInferredErrorSet( |
| 35515 | 35853 | src: LazySrcLoc, |
| 35516 | 35854 | value: InternPool.Index, |
| 35517 | 35855 | ) CompileError!InternPool.Index { |
| 35518 | const mod = sema.mod; | |
| 35856 | const pt = sema.pt; | |
| 35857 | const mod = pt.zcu; | |
| 35519 | 35858 | const gpa = sema.gpa; |
| 35520 | 35859 | const ip = &mod.intern_pool; |
| 35521 | 35860 | const new_ty = try resolveAdHocInferredErrorSetTy(sema, block, src, ip.typeOf(value)); |
| 35522 | 35861 | if (new_ty == .none) return value; |
| 35523 | return ip.getCoerced(gpa, value, new_ty); | |
| 35862 | return ip.getCoerced(gpa, pt.tid, value, new_ty); | |
| 35524 | 35863 | } |
| 35525 | 35864 | |
| 35526 | 35865 | fn resolveAdHocInferredErrorSetTy( |
| ... | ... | @@ -35530,8 +35869,8 @@ fn resolveAdHocInferredErrorSetTy( |
| 35530 | 35869 | ty: InternPool.Index, |
| 35531 | 35870 | ) CompileError!InternPool.Index { |
| 35532 | 35871 | const ies = sema.fn_ret_ty_ies orelse return .none; |
| 35533 | const mod = sema.mod; | |
| 35534 | const gpa = sema.gpa; | |
| 35872 | const pt = sema.pt; | |
| 35873 | const mod = pt.zcu; | |
| 35535 | 35874 | const ip = &mod.intern_pool; |
| 35536 | 35875 | const error_union_info = switch (ip.indexToKey(ty)) { |
| 35537 | 35876 | .error_union_type => |x| x, |
| ... | ... | @@ -35541,7 +35880,7 @@ fn resolveAdHocInferredErrorSetTy( |
| 35541 | 35880 | return .none; |
| 35542 | 35881 | |
| 35543 | 35882 | try sema.resolveInferredErrorSetPtr(block, src, ies); |
| 35544 | const new_ty = try ip.get(gpa, .{ .error_union_type = .{ | |
| 35883 | const new_ty = try pt.intern(.{ .error_union_type = .{ | |
| 35545 | 35884 | .error_set_type = ies.resolved, |
| 35546 | 35885 | .payload_type = error_union_info.payload_type, |
| 35547 | 35886 | } }); |
| ... | ... | @@ -35554,7 +35893,8 @@ fn resolveInferredErrorSetTy( |
| 35554 | 35893 | src: LazySrcLoc, |
| 35555 | 35894 | ty: InternPool.Index, |
| 35556 | 35895 | ) CompileError!InternPool.Index { |
| 35557 | const mod = sema.mod; | |
| 35896 | const pt = sema.pt; | |
| 35897 | const mod = pt.zcu; | |
| 35558 | 35898 | const ip = &mod.intern_pool; |
| 35559 | 35899 | if (ty == .anyerror_type) return ty; |
| 35560 | 35900 | switch (ip.indexToKey(ty)) { |
| ... | ... | @@ -35614,10 +35954,11 @@ fn structZirInfo(zir: Zir, zir_index: Zir.Inst.Index) struct { |
| 35614 | 35954 | } |
| 35615 | 35955 | |
| 35616 | 35956 | fn semaStructFields( |
| 35617 | zcu: *Zcu, | |
| 35957 | pt: Zcu.PerThread, | |
| 35618 | 35958 | arena: Allocator, |
| 35619 | 35959 | struct_type: InternPool.LoadedStructType, |
| 35620 | 35960 | ) CompileError!void { |
| 35961 | const zcu = pt.zcu; | |
| 35621 | 35962 | const gpa = zcu.gpa; |
| 35622 | 35963 | const ip = &zcu.intern_pool; |
| 35623 | 35964 | const decl_index = struct_type.decl.unwrap() orelse return; |
| ... | ... | @@ -35630,7 +35971,7 @@ fn semaStructFields( |
| 35630 | 35971 | |
| 35631 | 35972 | if (fields_len == 0) switch (struct_type.layout) { |
| 35632 | 35973 | .@"packed" => { |
| 35633 | try semaBackingIntType(zcu, struct_type); | |
| 35974 | try semaBackingIntType(pt, struct_type); | |
| 35634 | 35975 | return; |
| 35635 | 35976 | }, |
| 35636 | 35977 | .auto, .@"extern" => { |
| ... | ... | @@ -35644,7 +35985,7 @@ fn semaStructFields( |
| 35644 | 35985 | defer comptime_err_ret_trace.deinit(); |
| 35645 | 35986 | |
| 35646 | 35987 | var sema: Sema = .{ |
| 35647 | .mod = zcu, | |
| 35988 | .pt = pt, | |
| 35648 | 35989 | .gpa = gpa, |
| 35649 | 35990 | .arena = arena, |
| 35650 | 35991 | .code = zir, |
| ... | ... | @@ -35789,7 +36130,7 @@ fn semaStructFields( |
| 35789 | 36130 | switch (struct_type.layout) { |
| 35790 | 36131 | .@"extern" => if (!try sema.validateExternType(field_ty, .struct_field)) { |
| 35791 | 36132 | const msg = msg: { |
| 35792 | const msg = try sema.errMsg(ty_src, "extern structs cannot contain fields of type '{}'", .{field_ty.fmt(zcu)}); | |
| 36133 | const msg = try sema.errMsg(ty_src, "extern structs cannot contain fields of type '{}'", .{field_ty.fmt(pt)}); | |
| 35793 | 36134 | errdefer msg.destroy(sema.gpa); |
| 35794 | 36135 | |
| 35795 | 36136 | try sema.explainWhyTypeIsNotExtern(msg, ty_src, field_ty, .struct_field); |
| ... | ... | @@ -35801,7 +36142,7 @@ fn semaStructFields( |
| 35801 | 36142 | }, |
| 35802 | 36143 | .@"packed" => if (!try sema.validatePackedType(field_ty)) { |
| 35803 | 36144 | const msg = msg: { |
| 35804 | const msg = try sema.errMsg(ty_src, "packed structs cannot contain fields of type '{}'", .{field_ty.fmt(zcu)}); | |
| 36145 | const msg = try sema.errMsg(ty_src, "packed structs cannot contain fields of type '{}'", .{field_ty.fmt(pt)}); | |
| 35805 | 36146 | errdefer msg.destroy(sema.gpa); |
| 35806 | 36147 | |
| 35807 | 36148 | try sema.explainWhyTypeIsNotPacked(msg, ty_src, field_ty); |
| ... | ... | @@ -35837,10 +36178,11 @@ fn semaStructFields( |
| 35837 | 36178 | |
| 35838 | 36179 | // This logic must be kept in sync with `semaStructFields` |
| 35839 | 36180 | fn semaStructFieldInits( |
| 35840 | zcu: *Zcu, | |
| 36181 | pt: Zcu.PerThread, | |
| 35841 | 36182 | arena: Allocator, |
| 35842 | 36183 | struct_type: InternPool.LoadedStructType, |
| 35843 | 36184 | ) CompileError!void { |
| 36185 | const zcu = pt.zcu; | |
| 35844 | 36186 | const gpa = zcu.gpa; |
| 35845 | 36187 | const ip = &zcu.intern_pool; |
| 35846 | 36188 | |
| ... | ... | @@ -35857,7 +36199,7 @@ fn semaStructFieldInits( |
| 35857 | 36199 | defer comptime_err_ret_trace.deinit(); |
| 35858 | 36200 | |
| 35859 | 36201 | var sema: Sema = .{ |
| 35860 | .mod = zcu, | |
| 36202 | .pt = pt, | |
| 35861 | 36203 | .gpa = gpa, |
| 35862 | 36204 | .arena = arena, |
| 35863 | 36205 | .code = zir, |
| ... | ... | @@ -35977,10 +36319,11 @@ fn semaStructFieldInits( |
| 35977 | 36319 | try sema.flushExports(); |
| 35978 | 36320 | } |
| 35979 | 36321 | |
| 35980 | fn semaUnionFields(zcu: *Zcu, arena: Allocator, union_type: InternPool.LoadedUnionType) CompileError!void { | |
| 36322 | fn semaUnionFields(pt: Zcu.PerThread, arena: Allocator, union_type: InternPool.LoadedUnionType) CompileError!void { | |
| 35981 | 36323 | const tracy = trace(@src()); |
| 35982 | 36324 | defer tracy.end(); |
| 35983 | 36325 | |
| 36326 | const zcu = pt.zcu; | |
| 35984 | 36327 | const gpa = zcu.gpa; |
| 35985 | 36328 | const ip = &zcu.intern_pool; |
| 35986 | 36329 | const decl_index = union_type.decl; |
| ... | ... | @@ -36034,7 +36377,7 @@ fn semaUnionFields(zcu: *Zcu, arena: Allocator, union_type: InternPool.LoadedUni |
| 36034 | 36377 | defer comptime_err_ret_trace.deinit(); |
| 36035 | 36378 | |
| 36036 | 36379 | var sema: Sema = .{ |
| 36037 | .mod = zcu, | |
| 36380 | .pt = pt, | |
| 36038 | 36381 | .gpa = gpa, |
| 36039 | 36382 | .arena = arena, |
| 36040 | 36383 | .code = zir, |
| ... | ... | @@ -36081,17 +36424,17 @@ fn semaUnionFields(zcu: *Zcu, arena: Allocator, union_type: InternPool.LoadedUni |
| 36081 | 36424 | // The provided type is an integer type and we must construct the enum tag type here. |
| 36082 | 36425 | int_tag_ty = provided_ty; |
| 36083 | 36426 | if (int_tag_ty.zigTypeTag(zcu) != .Int and int_tag_ty.zigTypeTag(zcu) != .ComptimeInt) { |
| 36084 | return sema.fail(&block_scope, tag_ty_src, "expected integer tag type, found '{}'", .{int_tag_ty.fmt(zcu)}); | |
| 36427 | return sema.fail(&block_scope, tag_ty_src, "expected integer tag type, found '{}'", .{int_tag_ty.fmt(pt)}); | |
| 36085 | 36428 | } |
| 36086 | 36429 | |
| 36087 | 36430 | if (fields_len > 0) { |
| 36088 | const field_count_val = try zcu.intValue(Type.comptime_int, fields_len - 1); | |
| 36431 | const field_count_val = try pt.intValue(Type.comptime_int, fields_len - 1); | |
| 36089 | 36432 | if (!(try sema.intFitsInType(field_count_val, int_tag_ty, null))) { |
| 36090 | 36433 | const msg = msg: { |
| 36091 | 36434 | const msg = try sema.errMsg(tag_ty_src, "specified integer tag type cannot represent every field", .{}); |
| 36092 | 36435 | errdefer msg.destroy(sema.gpa); |
| 36093 | 36436 | try sema.errNote(tag_ty_src, msg, "type '{}' cannot fit values in range 0...{d}", .{ |
| 36094 | int_tag_ty.fmt(zcu), | |
| 36437 | int_tag_ty.fmt(pt), | |
| 36095 | 36438 | fields_len - 1, |
| 36096 | 36439 | }); |
| 36097 | 36440 | break :msg msg; |
| ... | ... | @@ -36106,7 +36449,7 @@ fn semaUnionFields(zcu: *Zcu, arena: Allocator, union_type: InternPool.LoadedUni |
| 36106 | 36449 | union_type.tagTypePtr(ip).* = provided_ty.toIntern(); |
| 36107 | 36450 | const enum_type = switch (ip.indexToKey(provided_ty.toIntern())) { |
| 36108 | 36451 | .enum_type => ip.loadEnumType(provided_ty.toIntern()), |
| 36109 | else => return sema.fail(&block_scope, tag_ty_src, "expected enum tag type, found '{}'", .{provided_ty.fmt(zcu)}), | |
| 36452 | else => return sema.fail(&block_scope, tag_ty_src, "expected enum tag type, found '{}'", .{provided_ty.fmt(pt)}), | |
| 36110 | 36453 | }; |
| 36111 | 36454 | // The fields of the union must match the enum exactly. |
| 36112 | 36455 | // A flag per field is used to check for missing and extraneous fields. |
| ... | ... | @@ -36202,7 +36545,7 @@ fn semaUnionFields(zcu: *Zcu, arena: Allocator, union_type: InternPool.LoadedUni |
| 36202 | 36545 | const val = if (last_tag_val) |val| |
| 36203 | 36546 | try sema.intAdd(val, Value.one_comptime_int, int_tag_ty, undefined) |
| 36204 | 36547 | else |
| 36205 | try zcu.intValue(int_tag_ty, 0); | |
| 36548 | try pt.intValue(int_tag_ty, 0); | |
| 36206 | 36549 | last_tag_val = val; |
| 36207 | 36550 | |
| 36208 | 36551 | break :blk val; |
| ... | ... | @@ -36214,7 +36557,7 @@ fn semaUnionFields(zcu: *Zcu, arena: Allocator, union_type: InternPool.LoadedUni |
| 36214 | 36557 | .offset = .{ .container_field_value = @intCast(gop.index) }, |
| 36215 | 36558 | }; |
| 36216 | 36559 | const msg = msg: { |
| 36217 | const msg = try sema.errMsg(value_src, "enum tag value {} already taken", .{enum_tag_val.fmtValue(zcu, &sema)}); | |
| 36560 | const msg = try sema.errMsg(value_src, "enum tag value {} already taken", .{enum_tag_val.fmtValue(pt, &sema)}); | |
| 36218 | 36561 | errdefer msg.destroy(gpa); |
| 36219 | 36562 | try sema.errNote(other_value_src, msg, "other occurrence here", .{}); |
| 36220 | 36563 | break :msg msg; |
| ... | ... | @@ -36244,7 +36587,7 @@ fn semaUnionFields(zcu: *Zcu, arena: Allocator, union_type: InternPool.LoadedUni |
| 36244 | 36587 | const tag_info = ip.loadEnumType(union_type.tagTypePtr(ip).*); |
| 36245 | 36588 | const enum_index = tag_info.nameIndex(ip, field_name) orelse { |
| 36246 | 36589 | return sema.fail(&block_scope, name_src, "no field named '{}' in enum '{}'", .{ |
| 36247 | field_name.fmt(ip), Type.fromInterned(union_type.tagTypePtr(ip).*).fmt(zcu), | |
| 36590 | field_name.fmt(ip), Type.fromInterned(union_type.tagTypePtr(ip).*).fmt(pt), | |
| 36248 | 36591 | }); |
| 36249 | 36592 | }; |
| 36250 | 36593 | |
| ... | ... | @@ -36286,7 +36629,7 @@ fn semaUnionFields(zcu: *Zcu, arena: Allocator, union_type: InternPool.LoadedUni |
| 36286 | 36629 | !try sema.validateExternType(field_ty, .union_field)) |
| 36287 | 36630 | { |
| 36288 | 36631 | const msg = msg: { |
| 36289 | const msg = try sema.errMsg(type_src, "extern unions cannot contain fields of type '{}'", .{field_ty.fmt(zcu)}); | |
| 36632 | const msg = try sema.errMsg(type_src, "extern unions cannot contain fields of type '{}'", .{field_ty.fmt(pt)}); | |
| 36290 | 36633 | errdefer msg.destroy(sema.gpa); |
| 36291 | 36634 | |
| 36292 | 36635 | try sema.explainWhyTypeIsNotExtern(msg, type_src, field_ty, .union_field); |
| ... | ... | @@ -36297,7 +36640,7 @@ fn semaUnionFields(zcu: *Zcu, arena: Allocator, union_type: InternPool.LoadedUni |
| 36297 | 36640 | return sema.failWithOwnedErrorMsg(&block_scope, msg); |
| 36298 | 36641 | } else if (layout == .@"packed" and !try sema.validatePackedType(field_ty)) { |
| 36299 | 36642 | const msg = msg: { |
| 36300 | const msg = try sema.errMsg(type_src, "packed unions cannot contain fields of type '{}'", .{field_ty.fmt(zcu)}); | |
| 36643 | const msg = try sema.errMsg(type_src, "packed unions cannot contain fields of type '{}'", .{field_ty.fmt(pt)}); | |
| 36301 | 36644 | errdefer msg.destroy(sema.gpa); |
| 36302 | 36645 | |
| 36303 | 36646 | try sema.explainWhyTypeIsNotPacked(msg, type_src, field_ty); |
| ... | ... | @@ -36366,7 +36709,8 @@ fn generateUnionTagTypeNumbered( |
| 36366 | 36709 | enum_field_vals: []const InternPool.Index, |
| 36367 | 36710 | union_owner_decl: *Module.Decl, |
| 36368 | 36711 | ) !InternPool.Index { |
| 36369 | const mod = sema.mod; | |
| 36712 | const pt = sema.pt; | |
| 36713 | const mod = pt.zcu; | |
| 36370 | 36714 | const gpa = sema.gpa; |
| 36371 | 36715 | const ip = &mod.intern_pool; |
| 36372 | 36716 | |
| ... | ... | @@ -36390,11 +36734,11 @@ fn generateUnionTagTypeNumbered( |
| 36390 | 36734 | new_decl.owns_tv = true; |
| 36391 | 36735 | new_decl.name_fully_qualified = true; |
| 36392 | 36736 | |
| 36393 | const enum_ty = try ip.getGeneratedTagEnumType(gpa, .{ | |
| 36737 | const enum_ty = try ip.getGeneratedTagEnumType(gpa, pt.tid, .{ | |
| 36394 | 36738 | .decl = new_decl_index, |
| 36395 | 36739 | .owner_union_ty = union_owner_decl.val.toIntern(), |
| 36396 | 36740 | .tag_ty = if (enum_field_vals.len == 0) |
| 36397 | (try mod.intType(.unsigned, 0)).toIntern() | |
| 36741 | (try pt.intType(.unsigned, 0)).toIntern() | |
| 36398 | 36742 | else |
| 36399 | 36743 | ip.typeOf(enum_field_vals[0]), |
| 36400 | 36744 | .names = enum_field_names, |
| ... | ... | @@ -36404,7 +36748,7 @@ fn generateUnionTagTypeNumbered( |
| 36404 | 36748 | |
| 36405 | 36749 | new_decl.val = Value.fromInterned(enum_ty); |
| 36406 | 36750 | |
| 36407 | try mod.finalizeAnonDecl(new_decl_index); | |
| 36751 | try pt.finalizeAnonDecl(new_decl_index); | |
| 36408 | 36752 | return enum_ty; |
| 36409 | 36753 | } |
| 36410 | 36754 | |
| ... | ... | @@ -36414,7 +36758,8 @@ fn generateUnionTagTypeSimple( |
| 36414 | 36758 | enum_field_names: []const InternPool.NullTerminatedString, |
| 36415 | 36759 | union_owner_decl: *Module.Decl, |
| 36416 | 36760 | ) !InternPool.Index { |
| 36417 | const mod = sema.mod; | |
| 36761 | const pt = sema.pt; | |
| 36762 | const mod = pt.zcu; | |
| 36418 | 36763 | const ip = &mod.intern_pool; |
| 36419 | 36764 | const gpa = sema.gpa; |
| 36420 | 36765 | |
| ... | ... | @@ -36438,13 +36783,13 @@ fn generateUnionTagTypeSimple( |
| 36438 | 36783 | }; |
| 36439 | 36784 | errdefer mod.abortAnonDecl(new_decl_index); |
| 36440 | 36785 | |
| 36441 | const enum_ty = try ip.getGeneratedTagEnumType(gpa, .{ | |
| 36786 | const enum_ty = try ip.getGeneratedTagEnumType(gpa, pt.tid, .{ | |
| 36442 | 36787 | .decl = new_decl_index, |
| 36443 | 36788 | .owner_union_ty = union_owner_decl.val.toIntern(), |
| 36444 | 36789 | .tag_ty = if (enum_field_names.len == 0) |
| 36445 | (try mod.intType(.unsigned, 0)).toIntern() | |
| 36790 | (try pt.intType(.unsigned, 0)).toIntern() | |
| 36446 | 36791 | else |
| 36447 | (try mod.smallestUnsignedInt(enum_field_names.len - 1)).toIntern(), | |
| 36792 | (try pt.smallestUnsignedInt(enum_field_names.len - 1)).toIntern(), | |
| 36448 | 36793 | .names = enum_field_names, |
| 36449 | 36794 | .values = &.{}, |
| 36450 | 36795 | .tag_mode = .auto, |
| ... | ... | @@ -36454,7 +36799,7 @@ fn generateUnionTagTypeSimple( |
| 36454 | 36799 | new_decl.owns_tv = true; |
| 36455 | 36800 | new_decl.val = Value.fromInterned(enum_ty); |
| 36456 | 36801 | |
| 36457 | try mod.finalizeAnonDecl(new_decl_index); | |
| 36802 | try pt.finalizeAnonDecl(new_decl_index); | |
| 36458 | 36803 | return enum_ty; |
| 36459 | 36804 | } |
| 36460 | 36805 | |
| ... | ... | @@ -36464,12 +36809,13 @@ fn generateUnionTagTypeSimple( |
| 36464 | 36809 | /// that the types are already resolved. |
| 36465 | 36810 | /// TODO assert the return value matches `ty.onePossibleValue` |
| 36466 | 36811 | pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { |
| 36467 | const zcu = sema.mod; | |
| 36812 | const pt = sema.pt; | |
| 36813 | const zcu = pt.zcu; | |
| 36468 | 36814 | const ip = &zcu.intern_pool; |
| 36469 | 36815 | return switch (ty.toIntern()) { |
| 36470 | 36816 | .u0_type, |
| 36471 | 36817 | .i0_type, |
| 36472 | => try zcu.intValue(ty, 0), | |
| 36818 | => try pt.intValue(ty, 0), | |
| 36473 | 36819 | .u1_type, |
| 36474 | 36820 | .u8_type, |
| 36475 | 36821 | .i8_type, |
| ... | ... | @@ -36532,7 +36878,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { |
| 36532 | 36878 | .anyframe_type => unreachable, |
| 36533 | 36879 | .null_type => Value.null, |
| 36534 | 36880 | .undefined_type => Value.undef, |
| 36535 | .optional_noreturn_type => try zcu.nullValue(ty), | |
| 36881 | .optional_noreturn_type => try pt.nullValue(ty), | |
| 36536 | 36882 | .generic_poison_type => error.GenericPoison, |
| 36537 | 36883 | .empty_struct_type => Value.empty_struct, |
| 36538 | 36884 | // values, not types |
| ... | ... | @@ -36646,16 +36992,16 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { |
| 36646 | 36992 | => switch (ip.indexToKey(ty.toIntern())) { |
| 36647 | 36993 | inline .array_type, .vector_type => |seq_type, seq_tag| { |
| 36648 | 36994 | const has_sentinel = seq_tag == .array_type and seq_type.sentinel != .none; |
| 36649 | if (seq_type.len + @intFromBool(has_sentinel) == 0) return Value.fromInterned((try zcu.intern(.{ .aggregate = .{ | |
| 36995 | if (seq_type.len + @intFromBool(has_sentinel) == 0) return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 36650 | 36996 | .ty = ty.toIntern(), |
| 36651 | 36997 | .storage = .{ .elems = &.{} }, |
| 36652 | } }))); | |
| 36998 | } })); | |
| 36653 | 36999 | |
| 36654 | 37000 | if (try sema.typeHasOnePossibleValue(Type.fromInterned(seq_type.child))) |opv| { |
| 36655 | return Value.fromInterned((try zcu.intern(.{ .aggregate = .{ | |
| 37001 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 36656 | 37002 | .ty = ty.toIntern(), |
| 36657 | 37003 | .storage = .{ .repeated_elem = opv.toIntern() }, |
| 36658 | } }))); | |
| 37004 | } })); | |
| 36659 | 37005 | } |
| 36660 | 37006 | return null; |
| 36661 | 37007 | }, |
| ... | ... | @@ -36663,17 +37009,17 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { |
| 36663 | 37009 | .struct_type => { |
| 36664 | 37010 | // Resolving the layout first helps to avoid loops. |
| 36665 | 37011 | // If the type has a coherent layout, we can recurse through fields safely. |
| 36666 | try ty.resolveLayout(zcu); | |
| 37012 | try ty.resolveLayout(pt); | |
| 36667 | 37013 | |
| 36668 | 37014 | const struct_type = ip.loadStructType(ty.toIntern()); |
| 36669 | 37015 | |
| 36670 | 37016 | if (struct_type.field_types.len == 0) { |
| 36671 | 37017 | // In this case the struct has no fields at all and |
| 36672 | 37018 | // therefore has one possible value. |
| 36673 | return Value.fromInterned((try zcu.intern(.{ .aggregate = .{ | |
| 37019 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 36674 | 37020 | .ty = ty.toIntern(), |
| 36675 | 37021 | .storage = .{ .elems = &.{} }, |
| 36676 | } }))); | |
| 37022 | } })); | |
| 36677 | 37023 | } |
| 36678 | 37024 | |
| 36679 | 37025 | const field_vals = try sema.arena.alloc( |
| ... | ... | @@ -36682,7 +37028,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { |
| 36682 | 37028 | ); |
| 36683 | 37029 | for (field_vals, 0..) |*field_val, i| { |
| 36684 | 37030 | if (struct_type.fieldIsComptime(ip, i)) { |
| 36685 | try ty.resolveStructFieldInits(zcu); | |
| 37031 | try ty.resolveStructFieldInits(pt); | |
| 36686 | 37032 | field_val.* = struct_type.field_inits.get(ip)[i]; |
| 36687 | 37033 | continue; |
| 36688 | 37034 | } |
| ... | ... | @@ -36694,10 +37040,10 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { |
| 36694 | 37040 | |
| 36695 | 37041 | // In this case the struct has no runtime-known fields and |
| 36696 | 37042 | // therefore has one possible value. |
| 36697 | return Value.fromInterned((try zcu.intern(.{ .aggregate = .{ | |
| 37043 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 36698 | 37044 | .ty = ty.toIntern(), |
| 36699 | 37045 | .storage = .{ .elems = field_vals }, |
| 36700 | } }))); | |
| 37046 | } })); | |
| 36701 | 37047 | }, |
| 36702 | 37048 | |
| 36703 | 37049 | .anon_struct_type => |tuple| { |
| ... | ... | @@ -36707,28 +37053,28 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { |
| 36707 | 37053 | // In this case the struct has all comptime-known fields and |
| 36708 | 37054 | // therefore has one possible value. |
| 36709 | 37055 | // TODO: write something like getCoercedInts to avoid needing to dupe |
| 36710 | return Value.fromInterned((try zcu.intern(.{ .aggregate = .{ | |
| 37056 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 36711 | 37057 | .ty = ty.toIntern(), |
| 36712 | 37058 | .storage = .{ .elems = try sema.arena.dupe(InternPool.Index, tuple.values.get(ip)) }, |
| 36713 | } }))); | |
| 37059 | } })); | |
| 36714 | 37060 | }, |
| 36715 | 37061 | |
| 36716 | 37062 | .union_type => { |
| 36717 | 37063 | // Resolving the layout first helps to avoid loops. |
| 36718 | 37064 | // If the type has a coherent layout, we can recurse through fields safely. |
| 36719 | try ty.resolveLayout(zcu); | |
| 37065 | try ty.resolveLayout(pt); | |
| 36720 | 37066 | |
| 36721 | 37067 | const union_obj = ip.loadUnionType(ty.toIntern()); |
| 36722 | 37068 | const tag_val = (try sema.typeHasOnePossibleValue(Type.fromInterned(union_obj.tagTypePtr(ip).*))) orelse |
| 36723 | 37069 | return null; |
| 36724 | 37070 | if (union_obj.field_types.len == 0) { |
| 36725 | const only = try zcu.intern(.{ .empty_enum_value = ty.toIntern() }); | |
| 37071 | const only = try pt.intern(.{ .empty_enum_value = ty.toIntern() }); | |
| 36726 | 37072 | return Value.fromInterned(only); |
| 36727 | 37073 | } |
| 36728 | 37074 | const only_field_ty = Type.fromInterned(union_obj.field_types.get(ip)[0]); |
| 36729 | 37075 | const val_val = (try sema.typeHasOnePossibleValue(only_field_ty)) orelse |
| 36730 | 37076 | return null; |
| 36731 | const only = try zcu.intern(.{ .un = .{ | |
| 37077 | const only = try pt.intern(.{ .un = .{ | |
| 36732 | 37078 | .ty = ty.toIntern(), |
| 36733 | 37079 | .tag = tag_val.toIntern(), |
| 36734 | 37080 | .val = val_val.toIntern(), |
| ... | ... | @@ -36743,7 +37089,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { |
| 36743 | 37089 | if (enum_type.tag_ty == .comptime_int_type) return null; |
| 36744 | 37090 | |
| 36745 | 37091 | if (try sema.typeHasOnePossibleValue(Type.fromInterned(enum_type.tag_ty))) |int_opv| { |
| 36746 | const only = try zcu.intern(.{ .enum_tag = .{ | |
| 37092 | const only = try pt.intern(.{ .enum_tag = .{ | |
| 36747 | 37093 | .ty = ty.toIntern(), |
| 36748 | 37094 | .int = int_opv.toIntern(), |
| 36749 | 37095 | } }); |
| ... | ... | @@ -36753,18 +37099,19 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { |
| 36753 | 37099 | return null; |
| 36754 | 37100 | }, |
| 36755 | 37101 | .auto, .explicit => { |
| 36756 | if (Type.fromInterned(enum_type.tag_ty).hasRuntimeBits(zcu)) return null; | |
| 37102 | if (Type.fromInterned(enum_type.tag_ty).hasRuntimeBits(pt)) return null; | |
| 36757 | 37103 | |
| 36758 | 37104 | return Value.fromInterned(switch (enum_type.names.len) { |
| 36759 | 0 => try zcu.intern(.{ .empty_enum_value = ty.toIntern() }), | |
| 36760 | 1 => try zcu.intern(.{ .enum_tag = .{ | |
| 37105 | 0 => try pt.intern(.{ .empty_enum_value = ty.toIntern() }), | |
| 37106 | 1 => try pt.intern(.{ .enum_tag = .{ | |
| 36761 | 37107 | .ty = ty.toIntern(), |
| 36762 | 37108 | .int = if (enum_type.values.len == 0) |
| 36763 | (try zcu.intValue(Type.fromInterned(enum_type.tag_ty), 0)).toIntern() | |
| 37109 | (try pt.intValue(Type.fromInterned(enum_type.tag_ty), 0)).toIntern() | |
| 36764 | 37110 | else |
| 36765 | try zcu.intern_pool.getCoercedInts( | |
| 37111 | try ip.getCoercedInts( | |
| 36766 | 37112 | zcu.gpa, |
| 36767 | zcu.intern_pool.indexToKey(enum_type.values.get(ip)[0]).int, | |
| 37113 | pt.tid, | |
| 37114 | ip.indexToKey(enum_type.values.get(ip)[0]).int, | |
| 36768 | 37115 | enum_type.tag_ty, |
| 36769 | 37116 | ), |
| 36770 | 37117 | } }), |
| ... | ... | @@ -36782,7 +37129,7 @@ pub fn typeHasOnePossibleValue(sema: *Sema, ty: Type) CompileError!?Value { |
| 36782 | 37129 | |
| 36783 | 37130 | /// Returns the type of the AIR instruction. |
| 36784 | 37131 | fn typeOf(sema: *Sema, inst: Air.Inst.Ref) Type { |
| 36785 | return sema.getTmpAir().typeOf(inst, &sema.mod.intern_pool); | |
| 37132 | return sema.getTmpAir().typeOf(inst, &sema.pt.zcu.intern_pool); | |
| 36786 | 37133 | } |
| 36787 | 37134 | |
| 36788 | 37135 | pub fn getTmpAir(sema: Sema) Air { |
| ... | ... | @@ -36838,12 +37185,13 @@ fn analyzeComptimeAlloc( |
| 36838 | 37185 | var_type: Type, |
| 36839 | 37186 | alignment: Alignment, |
| 36840 | 37187 | ) CompileError!Air.Inst.Ref { |
| 36841 | const mod = sema.mod; | |
| 37188 | const pt = sema.pt; | |
| 37189 | const mod = pt.zcu; | |
| 36842 | 37190 | |
| 36843 | 37191 | // Needed to make an anon decl with type `var_type` (the `finish()` call below). |
| 36844 | 37192 | _ = try sema.typeHasOnePossibleValue(var_type); |
| 36845 | 37193 | |
| 36846 | const ptr_type = try mod.ptrTypeSema(.{ | |
| 37194 | const ptr_type = try pt.ptrTypeSema(.{ | |
| 36847 | 37195 | .child = var_type.toIntern(), |
| 36848 | 37196 | .flags = .{ |
| 36849 | 37197 | .alignment = alignment, |
| ... | ... | @@ -36853,7 +37201,7 @@ fn analyzeComptimeAlloc( |
| 36853 | 37201 | |
| 36854 | 37202 | const alloc = try sema.newComptimeAlloc(block, var_type, alignment); |
| 36855 | 37203 | |
| 36856 | return Air.internedToRef((try mod.intern(.{ .ptr = .{ | |
| 37204 | return Air.internedToRef((try pt.intern(.{ .ptr = .{ | |
| 36857 | 37205 | .ty = ptr_type.toIntern(), |
| 36858 | 37206 | .base_addr = .{ .comptime_alloc = alloc }, |
| 36859 | 37207 | .byte_offset = 0, |
| ... | ... | @@ -36896,13 +37244,14 @@ pub fn analyzeAsAddressSpace( |
| 36896 | 37244 | air_ref: Air.Inst.Ref, |
| 36897 | 37245 | ctx: AddressSpaceContext, |
| 36898 | 37246 | ) !std.builtin.AddressSpace { |
| 36899 | const mod = sema.mod; | |
| 37247 | const pt = sema.pt; | |
| 37248 | const mod = pt.zcu; | |
| 36900 | 37249 | const coerced = try sema.coerce(block, Type.fromInterned(.address_space_type), air_ref, src); |
| 36901 | 37250 | const addrspace_val = try sema.resolveConstDefinedValue(block, src, coerced, .{ |
| 36902 | 37251 | .needed_comptime_reason = "address space must be comptime-known", |
| 36903 | 37252 | }); |
| 36904 | 37253 | const address_space = mod.toEnum(std.builtin.AddressSpace, addrspace_val); |
| 36905 | const target = sema.mod.getTarget(); | |
| 37254 | const target = pt.zcu.getTarget(); | |
| 36906 | 37255 | const arch = target.cpu.arch; |
| 36907 | 37256 | |
| 36908 | 37257 | const is_nv = arch == .nvptx or arch == .nvptx64; |
| ... | ... | @@ -36946,7 +37295,8 @@ pub fn analyzeAsAddressSpace( |
| 36946 | 37295 | /// Returns `null` if the pointer contents cannot be loaded at comptime. |
| 36947 | 37296 | fn pointerDeref(sema: *Sema, block: *Block, src: LazySrcLoc, ptr_val: Value, ptr_ty: Type) CompileError!?Value { |
| 36948 | 37297 | // TODO: audit use sites to eliminate this coercion |
| 36949 | const coerced_ptr_val = try sema.mod.getCoerced(ptr_val, ptr_ty); | |
| 37298 | const pt = sema.pt; | |
| 37299 | const coerced_ptr_val = try pt.getCoerced(ptr_val, ptr_ty); | |
| 36950 | 37300 | switch (try sema.pointerDerefExtra(block, src, coerced_ptr_val)) { |
| 36951 | 37301 | .runtime_load => return null, |
| 36952 | 37302 | .val => |v| return v, |
| ... | ... | @@ -36954,13 +37304,13 @@ fn pointerDeref(sema: *Sema, block: *Block, src: LazySrcLoc, ptr_val: Value, ptr |
| 36954 | 37304 | block, |
| 36955 | 37305 | src, |
| 36956 | 37306 | "comptime dereference requires '{}' to have a well-defined layout", |
| 36957 | .{ty.fmt(sema.mod)}, | |
| 37307 | .{ty.fmt(pt)}, | |
| 36958 | 37308 | ), |
| 36959 | 37309 | .out_of_bounds => |ty| return sema.fail( |
| 36960 | 37310 | block, |
| 36961 | 37311 | src, |
| 36962 | 37312 | "dereference of '{}' exceeds bounds of containing decl of type '{}'", |
| 36963 | .{ ptr_ty.fmt(sema.mod), ty.fmt(sema.mod) }, | |
| 37313 | .{ ptr_ty.fmt(pt), ty.fmt(pt) }, | |
| 36964 | 37314 | ), |
| 36965 | 37315 | } |
| 36966 | 37316 | } |
| ... | ... | @@ -36973,10 +37323,10 @@ const DerefResult = union(enum) { |
| 36973 | 37323 | }; |
| 36974 | 37324 | |
| 36975 | 37325 | fn pointerDerefExtra(sema: *Sema, block: *Block, src: LazySrcLoc, ptr_val: Value) CompileError!DerefResult { |
| 36976 | const zcu = sema.mod; | |
| 36977 | const ip = &zcu.intern_pool; | |
| 37326 | const pt = sema.pt; | |
| 37327 | const ip = &pt.zcu.intern_pool; | |
| 36978 | 37328 | switch (try sema.loadComptimePtr(block, src, ptr_val)) { |
| 36979 | .success => |mv| return .{ .val = try mv.intern(zcu, sema.arena) }, | |
| 37329 | .success => |mv| return .{ .val = try mv.intern(pt, sema.arena) }, | |
| 36980 | 37330 | .runtime_load => return .runtime_load, |
| 36981 | 37331 | .undef => return sema.failWithUseOfUndef(block, src), |
| 36982 | 37332 | .err_payload => |err_name| return sema.fail(block, src, "attempt to unwrap error: {}", .{err_name.fmt(ip)}), |
| ... | ... | @@ -37001,7 +37351,8 @@ fn usizeCast(sema: *Sema, block: *Block, src: LazySrcLoc, int: u64) CompileError |
| 37001 | 37351 | /// a type has zero bits, which can cause a "foo depends on itself" compile error. |
| 37002 | 37352 | /// This logic must be kept in sync with `Type.isPtrLikeOptional`. |
| 37003 | 37353 | fn typePtrOrOptionalPtrTy(sema: *Sema, ty: Type) !?Type { |
| 37004 | const mod = sema.mod; | |
| 37354 | const pt = sema.pt; | |
| 37355 | const mod = pt.zcu; | |
| 37005 | 37356 | return switch (mod.intern_pool.indexToKey(ty.toIntern())) { |
| 37006 | 37357 | .ptr_type => |ptr_type| switch (ptr_type.flags.size) { |
| 37007 | 37358 | .One, .Many, .C => ty, |
| ... | ... | @@ -37031,27 +37382,28 @@ fn typePtrOrOptionalPtrTy(sema: *Sema, ty: Type) !?Type { |
| 37031 | 37382 | /// `generic_poison` will return false. |
| 37032 | 37383 | /// May return false negatives when structs and unions are having their field types resolved. |
| 37033 | 37384 | pub fn typeRequiresComptime(sema: *Sema, ty: Type) SemaError!bool { |
| 37034 | return ty.comptimeOnlyAdvanced(sema.mod, .sema); | |
| 37385 | return ty.comptimeOnlyAdvanced(sema.pt, .sema); | |
| 37035 | 37386 | } |
| 37036 | 37387 | |
| 37037 | 37388 | pub fn typeHasRuntimeBits(sema: *Sema, ty: Type) SemaError!bool { |
| 37038 | return ty.hasRuntimeBitsAdvanced(sema.mod, false, .sema) catch |err| switch (err) { | |
| 37389 | return ty.hasRuntimeBitsAdvanced(sema.pt, false, .sema) catch |err| switch (err) { | |
| 37039 | 37390 | error.NeedLazy => unreachable, |
| 37040 | 37391 | else => |e| return e, |
| 37041 | 37392 | }; |
| 37042 | 37393 | } |
| 37043 | 37394 | |
| 37044 | 37395 | pub fn typeAbiSize(sema: *Sema, ty: Type) SemaError!u64 { |
| 37045 | try ty.resolveLayout(sema.mod); | |
| 37046 | return ty.abiSize(sema.mod); | |
| 37396 | const pt = sema.pt; | |
| 37397 | try ty.resolveLayout(pt); | |
| 37398 | return ty.abiSize(pt); | |
| 37047 | 37399 | } |
| 37048 | 37400 | |
| 37049 | 37401 | pub fn typeAbiAlignment(sema: *Sema, ty: Type) SemaError!Alignment { |
| 37050 | return (try ty.abiAlignmentAdvanced(sema.mod, .sema)).scalar; | |
| 37402 | return (try ty.abiAlignmentAdvanced(sema.pt, .sema)).scalar; | |
| 37051 | 37403 | } |
| 37052 | 37404 | |
| 37053 | 37405 | pub fn fnHasRuntimeBits(sema: *Sema, ty: Type) CompileError!bool { |
| 37054 | return ty.fnHasRuntimeBitsAdvanced(sema.mod, .sema); | |
| 37406 | return ty.fnHasRuntimeBitsAdvanced(sema.pt, .sema); | |
| 37055 | 37407 | } |
| 37056 | 37408 | |
| 37057 | 37409 | fn unionFieldIndex( |
| ... | ... | @@ -37061,9 +37413,10 @@ fn unionFieldIndex( |
| 37061 | 37413 | field_name: InternPool.NullTerminatedString, |
| 37062 | 37414 | field_src: LazySrcLoc, |
| 37063 | 37415 | ) !u32 { |
| 37064 | const mod = sema.mod; | |
| 37416 | const pt = sema.pt; | |
| 37417 | const mod = pt.zcu; | |
| 37065 | 37418 | const ip = &mod.intern_pool; |
| 37066 | try union_ty.resolveFields(mod); | |
| 37419 | try union_ty.resolveFields(pt); | |
| 37067 | 37420 | const union_obj = mod.typeToUnion(union_ty).?; |
| 37068 | 37421 | const field_index = union_obj.loadTagType(ip).nameIndex(ip, field_name) orelse |
| 37069 | 37422 | return sema.failWithBadUnionFieldAccess(block, union_ty, union_obj, field_src, field_name); |
| ... | ... | @@ -37077,9 +37430,10 @@ fn structFieldIndex( |
| 37077 | 37430 | field_name: InternPool.NullTerminatedString, |
| 37078 | 37431 | field_src: LazySrcLoc, |
| 37079 | 37432 | ) !u32 { |
| 37080 | const mod = sema.mod; | |
| 37433 | const pt = sema.pt; | |
| 37434 | const mod = pt.zcu; | |
| 37081 | 37435 | const ip = &mod.intern_pool; |
| 37082 | try struct_ty.resolveFields(mod); | |
| 37436 | try struct_ty.resolveFields(pt); | |
| 37083 | 37437 | if (struct_ty.isAnonStruct(mod)) { |
| 37084 | 37438 | return sema.anonStructFieldIndex(block, struct_ty, field_name, field_src); |
| 37085 | 37439 | } else { |
| ... | ... | @@ -37096,7 +37450,8 @@ fn anonStructFieldIndex( |
| 37096 | 37450 | field_name: InternPool.NullTerminatedString, |
| 37097 | 37451 | field_src: LazySrcLoc, |
| 37098 | 37452 | ) !u32 { |
| 37099 | const mod = sema.mod; | |
| 37453 | const pt = sema.pt; | |
| 37454 | const mod = pt.zcu; | |
| 37100 | 37455 | const ip = &mod.intern_pool; |
| 37101 | 37456 | switch (ip.indexToKey(struct_ty.toIntern())) { |
| 37102 | 37457 | .anon_struct_type => |anon_struct_type| for (anon_struct_type.names.get(ip), 0..) |name, i| { |
| ... | ... | @@ -37106,20 +37461,21 @@ fn anonStructFieldIndex( |
| 37106 | 37461 | else => unreachable, |
| 37107 | 37462 | } |
| 37108 | 37463 | return sema.fail(block, field_src, "no field named '{}' in anonymous struct '{}'", .{ |
| 37109 | field_name.fmt(ip), struct_ty.fmt(sema.mod), | |
| 37464 | field_name.fmt(ip), struct_ty.fmt(pt), | |
| 37110 | 37465 | }); |
| 37111 | 37466 | } |
| 37112 | 37467 | |
| 37113 | 37468 | /// If the value overflowed the type, returns a comptime_int (or vector thereof) instead, setting |
| 37114 | 37469 | /// overflow_idx to the vector index the overflow was at (or 0 for a scalar). |
| 37115 | 37470 | fn intAdd(sema: *Sema, lhs: Value, rhs: Value, ty: Type, overflow_idx: *?usize) !Value { |
| 37471 | const pt = sema.pt; | |
| 37116 | 37472 | var overflow: usize = undefined; |
| 37117 | 37473 | return sema.intAddInner(lhs, rhs, ty, &overflow) catch |err| switch (err) { |
| 37118 | 37474 | error.Overflow => { |
| 37119 | const is_vec = ty.isVector(sema.mod); | |
| 37475 | const is_vec = ty.isVector(pt.zcu); | |
| 37120 | 37476 | overflow_idx.* = if (is_vec) overflow else 0; |
| 37121 | const safe_ty = if (is_vec) try sema.mod.vectorType(.{ | |
| 37122 | .len = ty.vectorLen(sema.mod), | |
| 37477 | const safe_ty = if (is_vec) try pt.vectorType(.{ | |
| 37478 | .len = ty.vectorLen(pt.zcu), | |
| 37123 | 37479 | .child = .comptime_int_type, |
| 37124 | 37480 | }) else Type.comptime_int; |
| 37125 | 37481 | return sema.intAddInner(lhs, rhs, safe_ty, undefined) catch |err1| switch (err1) { |
| ... | ... | @@ -37132,13 +37488,14 @@ fn intAdd(sema: *Sema, lhs: Value, rhs: Value, ty: Type, overflow_idx: *?usize) |
| 37132 | 37488 | } |
| 37133 | 37489 | |
| 37134 | 37490 | fn intAddInner(sema: *Sema, lhs: Value, rhs: Value, ty: Type, overflow_idx: *usize) !Value { |
| 37135 | const mod = sema.mod; | |
| 37491 | const pt = sema.pt; | |
| 37492 | const mod = pt.zcu; | |
| 37136 | 37493 | if (ty.zigTypeTag(mod) == .Vector) { |
| 37137 | 37494 | const result_data = try sema.arena.alloc(InternPool.Index, ty.vectorLen(mod)); |
| 37138 | 37495 | const scalar_ty = ty.scalarType(mod); |
| 37139 | 37496 | for (result_data, 0..) |*scalar, i| { |
| 37140 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 37141 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 37497 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 37498 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 37142 | 37499 | const val = sema.intAddScalar(lhs_elem, rhs_elem, scalar_ty) catch |err| switch (err) { |
| 37143 | 37500 | error.Overflow => { |
| 37144 | 37501 | overflow_idx.* = i; |
| ... | ... | @@ -37148,34 +37505,34 @@ fn intAddInner(sema: *Sema, lhs: Value, rhs: Value, ty: Type, overflow_idx: *usi |
| 37148 | 37505 | }; |
| 37149 | 37506 | scalar.* = val.toIntern(); |
| 37150 | 37507 | } |
| 37151 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 37508 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 37152 | 37509 | .ty = ty.toIntern(), |
| 37153 | 37510 | .storage = .{ .elems = result_data }, |
| 37154 | } }))); | |
| 37511 | } })); | |
| 37155 | 37512 | } |
| 37156 | 37513 | return sema.intAddScalar(lhs, rhs, ty); |
| 37157 | 37514 | } |
| 37158 | 37515 | |
| 37159 | 37516 | fn intAddScalar(sema: *Sema, lhs: Value, rhs: Value, scalar_ty: Type) !Value { |
| 37160 | const mod = sema.mod; | |
| 37517 | const pt = sema.pt; | |
| 37161 | 37518 | if (scalar_ty.toIntern() != .comptime_int_type) { |
| 37162 | 37519 | const res = try sema.intAddWithOverflowScalar(lhs, rhs, scalar_ty); |
| 37163 | if (res.overflow_bit.compareAllWithZero(.neq, mod)) return error.Overflow; | |
| 37520 | if (res.overflow_bit.compareAllWithZero(.neq, pt)) return error.Overflow; | |
| 37164 | 37521 | return res.wrapped_result; |
| 37165 | 37522 | } |
| 37166 | 37523 | // TODO is this a performance issue? maybe we should try the operation without |
| 37167 | 37524 | // resorting to BigInt first. |
| 37168 | 37525 | var lhs_space: Value.BigIntSpace = undefined; |
| 37169 | 37526 | var rhs_space: Value.BigIntSpace = undefined; |
| 37170 | const lhs_bigint = try lhs.toBigIntAdvanced(&lhs_space, mod, .sema); | |
| 37171 | const rhs_bigint = try rhs.toBigIntAdvanced(&rhs_space, mod, .sema); | |
| 37527 | const lhs_bigint = try lhs.toBigIntAdvanced(&lhs_space, pt, .sema); | |
| 37528 | const rhs_bigint = try rhs.toBigIntAdvanced(&rhs_space, pt, .sema); | |
| 37172 | 37529 | const limbs = try sema.arena.alloc( |
| 37173 | 37530 | std.math.big.Limb, |
| 37174 | 37531 | @max(lhs_bigint.limbs.len, rhs_bigint.limbs.len) + 1, |
| 37175 | 37532 | ); |
| 37176 | 37533 | var result_bigint = std.math.big.int.Mutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 37177 | 37534 | result_bigint.add(lhs_bigint, rhs_bigint); |
| 37178 | return mod.intValue_big(scalar_ty, result_bigint.toConst()); | |
| 37535 | return pt.intValue_big(scalar_ty, result_bigint.toConst()); | |
| 37179 | 37536 | } |
| 37180 | 37537 | |
| 37181 | 37538 | /// Supports both floats and ints; handles undefined. |
| ... | ... | @@ -37185,15 +37542,16 @@ fn numberAddWrapScalar( |
| 37185 | 37542 | rhs: Value, |
| 37186 | 37543 | ty: Type, |
| 37187 | 37544 | ) !Value { |
| 37188 | const mod = sema.mod; | |
| 37189 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) return mod.undefValue(ty); | |
| 37545 | const pt = sema.pt; | |
| 37546 | const mod = pt.zcu; | |
| 37547 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) return pt.undefValue(ty); | |
| 37190 | 37548 | |
| 37191 | 37549 | if (ty.zigTypeTag(mod) == .ComptimeInt) { |
| 37192 | 37550 | return sema.intAdd(lhs, rhs, ty, undefined); |
| 37193 | 37551 | } |
| 37194 | 37552 | |
| 37195 | 37553 | if (ty.isAnyFloat()) { |
| 37196 | return Value.floatAdd(lhs, rhs, ty, sema.arena, mod); | |
| 37554 | return Value.floatAdd(lhs, rhs, ty, sema.arena, pt); | |
| 37197 | 37555 | } |
| 37198 | 37556 | |
| 37199 | 37557 | const overflow_result = try sema.intAddWithOverflow(lhs, rhs, ty); |
| ... | ... | @@ -37203,13 +37561,14 @@ fn numberAddWrapScalar( |
| 37203 | 37561 | /// If the value overflowed the type, returns a comptime_int (or vector thereof) instead, setting |
| 37204 | 37562 | /// overflow_idx to the vector index the overflow was at (or 0 for a scalar). |
| 37205 | 37563 | fn intSub(sema: *Sema, lhs: Value, rhs: Value, ty: Type, overflow_idx: *?usize) !Value { |
| 37564 | const pt = sema.pt; | |
| 37206 | 37565 | var overflow: usize = undefined; |
| 37207 | 37566 | return sema.intSubInner(lhs, rhs, ty, &overflow) catch |err| switch (err) { |
| 37208 | 37567 | error.Overflow => { |
| 37209 | const is_vec = ty.isVector(sema.mod); | |
| 37568 | const is_vec = ty.isVector(pt.zcu); | |
| 37210 | 37569 | overflow_idx.* = if (is_vec) overflow else 0; |
| 37211 | const safe_ty = if (is_vec) try sema.mod.vectorType(.{ | |
| 37212 | .len = ty.vectorLen(sema.mod), | |
| 37570 | const safe_ty = if (is_vec) try pt.vectorType(.{ | |
| 37571 | .len = ty.vectorLen(pt.zcu), | |
| 37213 | 37572 | .child = .comptime_int_type, |
| 37214 | 37573 | }) else Type.comptime_int; |
| 37215 | 37574 | return sema.intSubInner(lhs, rhs, safe_ty, undefined) catch |err1| switch (err1) { |
| ... | ... | @@ -37222,13 +37581,13 @@ fn intSub(sema: *Sema, lhs: Value, rhs: Value, ty: Type, overflow_idx: *?usize) |
| 37222 | 37581 | } |
| 37223 | 37582 | |
| 37224 | 37583 | fn intSubInner(sema: *Sema, lhs: Value, rhs: Value, ty: Type, overflow_idx: *usize) !Value { |
| 37225 | const mod = sema.mod; | |
| 37226 | if (ty.zigTypeTag(mod) == .Vector) { | |
| 37227 | const result_data = try sema.arena.alloc(InternPool.Index, ty.vectorLen(mod)); | |
| 37228 | const scalar_ty = ty.scalarType(mod); | |
| 37584 | const pt = sema.pt; | |
| 37585 | if (ty.zigTypeTag(pt.zcu) == .Vector) { | |
| 37586 | const result_data = try sema.arena.alloc(InternPool.Index, ty.vectorLen(pt.zcu)); | |
| 37587 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 37229 | 37588 | for (result_data, 0..) |*scalar, i| { |
| 37230 | const lhs_elem = try lhs.elemValue(sema.mod, i); | |
| 37231 | const rhs_elem = try rhs.elemValue(sema.mod, i); | |
| 37589 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 37590 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 37232 | 37591 | const val = sema.intSubScalar(lhs_elem, rhs_elem, scalar_ty) catch |err| switch (err) { |
| 37233 | 37592 | error.Overflow => { |
| 37234 | 37593 | overflow_idx.* = i; |
| ... | ... | @@ -37238,34 +37597,34 @@ fn intSubInner(sema: *Sema, lhs: Value, rhs: Value, ty: Type, overflow_idx: *usi |
| 37238 | 37597 | }; |
| 37239 | 37598 | scalar.* = val.toIntern(); |
| 37240 | 37599 | } |
| 37241 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 37600 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 37242 | 37601 | .ty = ty.toIntern(), |
| 37243 | 37602 | .storage = .{ .elems = result_data }, |
| 37244 | } }))); | |
| 37603 | } })); | |
| 37245 | 37604 | } |
| 37246 | 37605 | return sema.intSubScalar(lhs, rhs, ty); |
| 37247 | 37606 | } |
| 37248 | 37607 | |
| 37249 | 37608 | fn intSubScalar(sema: *Sema, lhs: Value, rhs: Value, scalar_ty: Type) !Value { |
| 37250 | const mod = sema.mod; | |
| 37609 | const pt = sema.pt; | |
| 37251 | 37610 | if (scalar_ty.toIntern() != .comptime_int_type) { |
| 37252 | 37611 | const res = try sema.intSubWithOverflowScalar(lhs, rhs, scalar_ty); |
| 37253 | if (res.overflow_bit.compareAllWithZero(.neq, mod)) return error.Overflow; | |
| 37612 | if (res.overflow_bit.compareAllWithZero(.neq, pt)) return error.Overflow; | |
| 37254 | 37613 | return res.wrapped_result; |
| 37255 | 37614 | } |
| 37256 | 37615 | // TODO is this a performance issue? maybe we should try the operation without |
| 37257 | 37616 | // resorting to BigInt first. |
| 37258 | 37617 | var lhs_space: Value.BigIntSpace = undefined; |
| 37259 | 37618 | var rhs_space: Value.BigIntSpace = undefined; |
| 37260 | const lhs_bigint = try lhs.toBigIntAdvanced(&lhs_space, mod, .sema); | |
| 37261 | const rhs_bigint = try rhs.toBigIntAdvanced(&rhs_space, mod, .sema); | |
| 37619 | const lhs_bigint = try lhs.toBigIntAdvanced(&lhs_space, pt, .sema); | |
| 37620 | const rhs_bigint = try rhs.toBigIntAdvanced(&rhs_space, pt, .sema); | |
| 37262 | 37621 | const limbs = try sema.arena.alloc( |
| 37263 | 37622 | std.math.big.Limb, |
| 37264 | 37623 | @max(lhs_bigint.limbs.len, rhs_bigint.limbs.len) + 1, |
| 37265 | 37624 | ); |
| 37266 | 37625 | var result_bigint = std.math.big.int.Mutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 37267 | 37626 | result_bigint.sub(lhs_bigint, rhs_bigint); |
| 37268 | return mod.intValue_big(scalar_ty, result_bigint.toConst()); | |
| 37627 | return pt.intValue_big(scalar_ty, result_bigint.toConst()); | |
| 37269 | 37628 | } |
| 37270 | 37629 | |
| 37271 | 37630 | /// Supports both floats and ints; handles undefined. |
| ... | ... | @@ -37275,15 +37634,16 @@ fn numberSubWrapScalar( |
| 37275 | 37634 | rhs: Value, |
| 37276 | 37635 | ty: Type, |
| 37277 | 37636 | ) !Value { |
| 37278 | const mod = sema.mod; | |
| 37279 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) return mod.undefValue(ty); | |
| 37637 | const pt = sema.pt; | |
| 37638 | const mod = pt.zcu; | |
| 37639 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) return pt.undefValue(ty); | |
| 37280 | 37640 | |
| 37281 | 37641 | if (ty.zigTypeTag(mod) == .ComptimeInt) { |
| 37282 | 37642 | return sema.intSub(lhs, rhs, ty, undefined); |
| 37283 | 37643 | } |
| 37284 | 37644 | |
| 37285 | 37645 | if (ty.isAnyFloat()) { |
| 37286 | return Value.floatSub(lhs, rhs, ty, sema.arena, mod); | |
| 37646 | return Value.floatSub(lhs, rhs, ty, sema.arena, pt); | |
| 37287 | 37647 | } |
| 37288 | 37648 | |
| 37289 | 37649 | const overflow_result = try sema.intSubWithOverflow(lhs, rhs, ty); |
| ... | ... | @@ -37296,28 +37656,29 @@ fn intSubWithOverflow( |
| 37296 | 37656 | rhs: Value, |
| 37297 | 37657 | ty: Type, |
| 37298 | 37658 | ) !Value.OverflowArithmeticResult { |
| 37299 | const mod = sema.mod; | |
| 37659 | const pt = sema.pt; | |
| 37660 | const mod = pt.zcu; | |
| 37300 | 37661 | if (ty.zigTypeTag(mod) == .Vector) { |
| 37301 | 37662 | const vec_len = ty.vectorLen(mod); |
| 37302 | 37663 | const overflowed_data = try sema.arena.alloc(InternPool.Index, vec_len); |
| 37303 | 37664 | const result_data = try sema.arena.alloc(InternPool.Index, vec_len); |
| 37304 | 37665 | const scalar_ty = ty.scalarType(mod); |
| 37305 | 37666 | for (overflowed_data, result_data, 0..) |*of, *scalar, i| { |
| 37306 | const lhs_elem = try lhs.elemValue(sema.mod, i); | |
| 37307 | const rhs_elem = try rhs.elemValue(sema.mod, i); | |
| 37667 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 37668 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 37308 | 37669 | const of_math_result = try sema.intSubWithOverflowScalar(lhs_elem, rhs_elem, scalar_ty); |
| 37309 | 37670 | of.* = of_math_result.overflow_bit.toIntern(); |
| 37310 | 37671 | scalar.* = of_math_result.wrapped_result.toIntern(); |
| 37311 | 37672 | } |
| 37312 | 37673 | return Value.OverflowArithmeticResult{ |
| 37313 | .overflow_bit = Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 37314 | .ty = (try mod.vectorType(.{ .len = vec_len, .child = .u1_type })).toIntern(), | |
| 37674 | .overflow_bit = Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 37675 | .ty = (try pt.vectorType(.{ .len = vec_len, .child = .u1_type })).toIntern(), | |
| 37315 | 37676 | .storage = .{ .elems = overflowed_data }, |
| 37316 | } }))), | |
| 37317 | .wrapped_result = Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 37677 | } })), | |
| 37678 | .wrapped_result = Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 37318 | 37679 | .ty = ty.toIntern(), |
| 37319 | 37680 | .storage = .{ .elems = result_data }, |
| 37320 | } }))), | |
| 37681 | } })), | |
| 37321 | 37682 | }; |
| 37322 | 37683 | } |
| 37323 | 37684 | return sema.intSubWithOverflowScalar(lhs, rhs, ty); |
| ... | ... | @@ -37329,29 +37690,30 @@ fn intSubWithOverflowScalar( |
| 37329 | 37690 | rhs: Value, |
| 37330 | 37691 | ty: Type, |
| 37331 | 37692 | ) !Value.OverflowArithmeticResult { |
| 37332 | const mod = sema.mod; | |
| 37693 | const pt = sema.pt; | |
| 37694 | const mod = pt.zcu; | |
| 37333 | 37695 | const info = ty.intInfo(mod); |
| 37334 | 37696 | |
| 37335 | 37697 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) { |
| 37336 | 37698 | return .{ |
| 37337 | .overflow_bit = try mod.undefValue(Type.u1), | |
| 37338 | .wrapped_result = try mod.undefValue(ty), | |
| 37699 | .overflow_bit = try pt.undefValue(Type.u1), | |
| 37700 | .wrapped_result = try pt.undefValue(ty), | |
| 37339 | 37701 | }; |
| 37340 | 37702 | } |
| 37341 | 37703 | |
| 37342 | 37704 | var lhs_space: Value.BigIntSpace = undefined; |
| 37343 | 37705 | var rhs_space: Value.BigIntSpace = undefined; |
| 37344 | const lhs_bigint = try lhs.toBigIntAdvanced(&lhs_space, mod, .sema); | |
| 37345 | const rhs_bigint = try rhs.toBigIntAdvanced(&rhs_space, mod, .sema); | |
| 37706 | const lhs_bigint = try lhs.toBigIntAdvanced(&lhs_space, pt, .sema); | |
| 37707 | const rhs_bigint = try rhs.toBigIntAdvanced(&rhs_space, pt, .sema); | |
| 37346 | 37708 | const limbs = try sema.arena.alloc( |
| 37347 | 37709 | std.math.big.Limb, |
| 37348 | 37710 | std.math.big.int.calcTwosCompLimbCount(info.bits), |
| 37349 | 37711 | ); |
| 37350 | 37712 | var result_bigint = std.math.big.int.Mutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 37351 | 37713 | const overflowed = result_bigint.subWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits); |
| 37352 | const wrapped_result = try mod.intValue_big(ty, result_bigint.toConst()); | |
| 37714 | const wrapped_result = try pt.intValue_big(ty, result_bigint.toConst()); | |
| 37353 | 37715 | return Value.OverflowArithmeticResult{ |
| 37354 | .overflow_bit = try mod.intValue(Type.u1, @intFromBool(overflowed)), | |
| 37716 | .overflow_bit = try pt.intValue(Type.u1, @intFromBool(overflowed)), | |
| 37355 | 37717 | .wrapped_result = wrapped_result, |
| 37356 | 37718 | }; |
| 37357 | 37719 | } |
| ... | ... | @@ -37367,17 +37729,18 @@ fn intFromFloat( |
| 37367 | 37729 | int_ty: Type, |
| 37368 | 37730 | mode: IntFromFloatMode, |
| 37369 | 37731 | ) CompileError!Value { |
| 37370 | const mod = sema.mod; | |
| 37732 | const pt = sema.pt; | |
| 37733 | const mod = pt.zcu; | |
| 37371 | 37734 | if (float_ty.zigTypeTag(mod) == .Vector) { |
| 37372 | 37735 | const result_data = try sema.arena.alloc(InternPool.Index, float_ty.vectorLen(mod)); |
| 37373 | 37736 | for (result_data, 0..) |*scalar, i| { |
| 37374 | const elem_val = try val.elemValue(sema.mod, i); | |
| 37737 | const elem_val = try val.elemValue(pt, i); | |
| 37375 | 37738 | scalar.* = (try sema.intFromFloatScalar(block, src, elem_val, int_ty.scalarType(mod), mode)).toIntern(); |
| 37376 | 37739 | } |
| 37377 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 37740 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 37378 | 37741 | .ty = int_ty.toIntern(), |
| 37379 | 37742 | .storage = .{ .elems = result_data }, |
| 37380 | } }))); | |
| 37743 | } })); | |
| 37381 | 37744 | } |
| 37382 | 37745 | return sema.intFromFloatScalar(block, src, val, int_ty, mode); |
| 37383 | 37746 | } |
| ... | ... | @@ -37415,7 +37778,8 @@ fn intFromFloatScalar( |
| 37415 | 37778 | int_ty: Type, |
| 37416 | 37779 | mode: IntFromFloatMode, |
| 37417 | 37780 | ) CompileError!Value { |
| 37418 | const mod = sema.mod; | |
| 37781 | const pt = sema.pt; | |
| 37782 | const mod = pt.zcu; | |
| 37419 | 37783 | |
| 37420 | 37784 | if (val.isUndef(mod)) return sema.failWithUseOfUndef(block, src); |
| 37421 | 37785 | |
| ... | ... | @@ -37423,32 +37787,32 @@ fn intFromFloatScalar( |
| 37423 | 37787 | block, |
| 37424 | 37788 | src, |
| 37425 | 37789 | "fractional component prevents float value '{}' from coercion to type '{}'", |
| 37426 | .{ val.fmtValue(mod, sema), int_ty.fmt(mod) }, | |
| 37790 | .{ val.fmtValue(pt, sema), int_ty.fmt(pt) }, | |
| 37427 | 37791 | ); |
| 37428 | 37792 | |
| 37429 | const float = val.toFloat(f128, mod); | |
| 37793 | const float = val.toFloat(f128, pt); | |
| 37430 | 37794 | if (std.math.isNan(float)) { |
| 37431 | 37795 | return sema.fail(block, src, "float value NaN cannot be stored in integer type '{}'", .{ |
| 37432 | int_ty.fmt(sema.mod), | |
| 37796 | int_ty.fmt(pt), | |
| 37433 | 37797 | }); |
| 37434 | 37798 | } |
| 37435 | 37799 | if (std.math.isInf(float)) { |
| 37436 | 37800 | return sema.fail(block, src, "float value Inf cannot be stored in integer type '{}'", .{ |
| 37437 | int_ty.fmt(sema.mod), | |
| 37801 | int_ty.fmt(pt), | |
| 37438 | 37802 | }); |
| 37439 | 37803 | } |
| 37440 | 37804 | |
| 37441 | 37805 | var big_int = try float128IntPartToBigInt(sema.arena, float); |
| 37442 | 37806 | defer big_int.deinit(); |
| 37443 | 37807 | |
| 37444 | const cti_result = try mod.intValue_big(Type.comptime_int, big_int.toConst()); | |
| 37808 | const cti_result = try pt.intValue_big(Type.comptime_int, big_int.toConst()); | |
| 37445 | 37809 | |
| 37446 | 37810 | if (!(try sema.intFitsInType(cti_result, int_ty, null))) { |
| 37447 | 37811 | return sema.fail(block, src, "float value '{}' cannot be stored in integer type '{}'", .{ |
| 37448 | val.fmtValue(sema.mod, sema), int_ty.fmt(sema.mod), | |
| 37812 | val.fmtValue(pt, sema), int_ty.fmt(pt), | |
| 37449 | 37813 | }); |
| 37450 | 37814 | } |
| 37451 | return mod.getCoerced(cti_result, int_ty); | |
| 37815 | return pt.getCoerced(cti_result, int_ty); | |
| 37452 | 37816 | } |
| 37453 | 37817 | |
| 37454 | 37818 | /// Asserts the value is an integer, and the destination type is ComptimeInt or Int. |
| ... | ... | @@ -37461,7 +37825,8 @@ fn intFitsInType( |
| 37461 | 37825 | ty: Type, |
| 37462 | 37826 | vector_index: ?*usize, |
| 37463 | 37827 | ) CompileError!bool { |
| 37464 | const mod = sema.mod; | |
| 37828 | const pt = sema.pt; | |
| 37829 | const mod = pt.zcu; | |
| 37465 | 37830 | if (ty.toIntern() == .comptime_int_type) return true; |
| 37466 | 37831 | const info = ty.intInfo(mod); |
| 37467 | 37832 | switch (val.toIntern()) { |
| ... | ... | @@ -37528,22 +37893,23 @@ fn intFitsInType( |
| 37528 | 37893 | } |
| 37529 | 37894 | |
| 37530 | 37895 | fn intInRange(sema: *Sema, tag_ty: Type, int_val: Value, end: usize) !bool { |
| 37531 | const mod = sema.mod; | |
| 37532 | if (!(try int_val.compareAllWithZeroSema(.gte, mod))) return false; | |
| 37533 | const end_val = try mod.intValue(tag_ty, end); | |
| 37896 | const pt = sema.pt; | |
| 37897 | if (!(try int_val.compareAllWithZeroSema(.gte, pt))) return false; | |
| 37898 | const end_val = try pt.intValue(tag_ty, end); | |
| 37534 | 37899 | if (!(try sema.compareAll(int_val, .lt, end_val, tag_ty))) return false; |
| 37535 | 37900 | return true; |
| 37536 | 37901 | } |
| 37537 | 37902 | |
| 37538 | 37903 | /// Asserts the type is an enum. |
| 37539 | 37904 | fn enumHasInt(sema: *Sema, ty: Type, int: Value) CompileError!bool { |
| 37540 | const mod = sema.mod; | |
| 37905 | const pt = sema.pt; | |
| 37906 | const mod = pt.zcu; | |
| 37541 | 37907 | const enum_type = mod.intern_pool.loadEnumType(ty.toIntern()); |
| 37542 | 37908 | assert(enum_type.tag_mode != .nonexhaustive); |
| 37543 | 37909 | // The `tagValueIndex` function call below relies on the type being the integer tag type. |
| 37544 | 37910 | // `getCoerced` assumes the value will fit the new type. |
| 37545 | 37911 | if (!(try sema.intFitsInType(int, Type.fromInterned(enum_type.tag_ty), null))) return false; |
| 37546 | const int_coerced = try mod.getCoerced(int, Type.fromInterned(enum_type.tag_ty)); | |
| 37912 | const int_coerced = try pt.getCoerced(int, Type.fromInterned(enum_type.tag_ty)); | |
| 37547 | 37913 | |
| 37548 | 37914 | return enum_type.tagValueIndex(&mod.intern_pool, int_coerced.toIntern()) != null; |
| 37549 | 37915 | } |
| ... | ... | @@ -37554,28 +37920,29 @@ fn intAddWithOverflow( |
| 37554 | 37920 | rhs: Value, |
| 37555 | 37921 | ty: Type, |
| 37556 | 37922 | ) !Value.OverflowArithmeticResult { |
| 37557 | const mod = sema.mod; | |
| 37923 | const pt = sema.pt; | |
| 37924 | const mod = pt.zcu; | |
| 37558 | 37925 | if (ty.zigTypeTag(mod) == .Vector) { |
| 37559 | 37926 | const vec_len = ty.vectorLen(mod); |
| 37560 | 37927 | const overflowed_data = try sema.arena.alloc(InternPool.Index, vec_len); |
| 37561 | 37928 | const result_data = try sema.arena.alloc(InternPool.Index, vec_len); |
| 37562 | 37929 | const scalar_ty = ty.scalarType(mod); |
| 37563 | 37930 | for (overflowed_data, result_data, 0..) |*of, *scalar, i| { |
| 37564 | const lhs_elem = try lhs.elemValue(sema.mod, i); | |
| 37565 | const rhs_elem = try rhs.elemValue(sema.mod, i); | |
| 37931 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 37932 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 37566 | 37933 | const of_math_result = try sema.intAddWithOverflowScalar(lhs_elem, rhs_elem, scalar_ty); |
| 37567 | 37934 | of.* = of_math_result.overflow_bit.toIntern(); |
| 37568 | 37935 | scalar.* = of_math_result.wrapped_result.toIntern(); |
| 37569 | 37936 | } |
| 37570 | 37937 | return Value.OverflowArithmeticResult{ |
| 37571 | .overflow_bit = Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 37572 | .ty = (try mod.vectorType(.{ .len = vec_len, .child = .u1_type })).toIntern(), | |
| 37938 | .overflow_bit = Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 37939 | .ty = (try pt.vectorType(.{ .len = vec_len, .child = .u1_type })).toIntern(), | |
| 37573 | 37940 | .storage = .{ .elems = overflowed_data }, |
| 37574 | } }))), | |
| 37575 | .wrapped_result = Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 37941 | } })), | |
| 37942 | .wrapped_result = Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 37576 | 37943 | .ty = ty.toIntern(), |
| 37577 | 37944 | .storage = .{ .elems = result_data }, |
| 37578 | } }))), | |
| 37945 | } })), | |
| 37579 | 37946 | }; |
| 37580 | 37947 | } |
| 37581 | 37948 | return sema.intAddWithOverflowScalar(lhs, rhs, ty); |
| ... | ... | @@ -37587,29 +37954,30 @@ fn intAddWithOverflowScalar( |
| 37587 | 37954 | rhs: Value, |
| 37588 | 37955 | ty: Type, |
| 37589 | 37956 | ) !Value.OverflowArithmeticResult { |
| 37590 | const mod = sema.mod; | |
| 37957 | const pt = sema.pt; | |
| 37958 | const mod = pt.zcu; | |
| 37591 | 37959 | const info = ty.intInfo(mod); |
| 37592 | 37960 | |
| 37593 | 37961 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) { |
| 37594 | 37962 | return .{ |
| 37595 | .overflow_bit = try mod.undefValue(Type.u1), | |
| 37596 | .wrapped_result = try mod.undefValue(ty), | |
| 37963 | .overflow_bit = try pt.undefValue(Type.u1), | |
| 37964 | .wrapped_result = try pt.undefValue(ty), | |
| 37597 | 37965 | }; |
| 37598 | 37966 | } |
| 37599 | 37967 | |
| 37600 | 37968 | var lhs_space: Value.BigIntSpace = undefined; |
| 37601 | 37969 | var rhs_space: Value.BigIntSpace = undefined; |
| 37602 | const lhs_bigint = try lhs.toBigIntAdvanced(&lhs_space, mod, .sema); | |
| 37603 | const rhs_bigint = try rhs.toBigIntAdvanced(&rhs_space, mod, .sema); | |
| 37970 | const lhs_bigint = try lhs.toBigIntAdvanced(&lhs_space, pt, .sema); | |
| 37971 | const rhs_bigint = try rhs.toBigIntAdvanced(&rhs_space, pt, .sema); | |
| 37604 | 37972 | const limbs = try sema.arena.alloc( |
| 37605 | 37973 | std.math.big.Limb, |
| 37606 | 37974 | std.math.big.int.calcTwosCompLimbCount(info.bits), |
| 37607 | 37975 | ); |
| 37608 | 37976 | var result_bigint = std.math.big.int.Mutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 37609 | 37977 | const overflowed = result_bigint.addWrap(lhs_bigint, rhs_bigint, info.signedness, info.bits); |
| 37610 | const result = try mod.intValue_big(ty, result_bigint.toConst()); | |
| 37978 | const result = try pt.intValue_big(ty, result_bigint.toConst()); | |
| 37611 | 37979 | return Value.OverflowArithmeticResult{ |
| 37612 | .overflow_bit = try mod.intValue(Type.u1, @intFromBool(overflowed)), | |
| 37980 | .overflow_bit = try pt.intValue(Type.u1, @intFromBool(overflowed)), | |
| 37613 | 37981 | .wrapped_result = result, |
| 37614 | 37982 | }; |
| 37615 | 37983 | } |
| ... | ... | @@ -37625,12 +37993,13 @@ fn compareAll( |
| 37625 | 37993 | rhs: Value, |
| 37626 | 37994 | ty: Type, |
| 37627 | 37995 | ) CompileError!bool { |
| 37628 | const mod = sema.mod; | |
| 37996 | const pt = sema.pt; | |
| 37997 | const mod = pt.zcu; | |
| 37629 | 37998 | if (ty.zigTypeTag(mod) == .Vector) { |
| 37630 | 37999 | var i: usize = 0; |
| 37631 | 38000 | while (i < ty.vectorLen(mod)) : (i += 1) { |
| 37632 | const lhs_elem = try lhs.elemValue(sema.mod, i); | |
| 37633 | const rhs_elem = try rhs.elemValue(sema.mod, i); | |
| 38001 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 38002 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 37634 | 38003 | if (!(try sema.compareScalar(lhs_elem, op, rhs_elem, ty.scalarType(mod)))) { |
| 37635 | 38004 | return false; |
| 37636 | 38005 | } |
| ... | ... | @@ -37648,13 +38017,13 @@ fn compareScalar( |
| 37648 | 38017 | rhs: Value, |
| 37649 | 38018 | ty: Type, |
| 37650 | 38019 | ) CompileError!bool { |
| 37651 | const mod = sema.mod; | |
| 37652 | const coerced_lhs = try mod.getCoerced(lhs, ty); | |
| 37653 | const coerced_rhs = try mod.getCoerced(rhs, ty); | |
| 38020 | const pt = sema.pt; | |
| 38021 | const coerced_lhs = try pt.getCoerced(lhs, ty); | |
| 38022 | const coerced_rhs = try pt.getCoerced(rhs, ty); | |
| 37654 | 38023 | switch (op) { |
| 37655 | 38024 | .eq => return sema.valuesEqual(coerced_lhs, coerced_rhs, ty), |
| 37656 | 38025 | .neq => return !(try sema.valuesEqual(coerced_lhs, coerced_rhs, ty)), |
| 37657 | else => return Value.compareHeteroAdvanced(coerced_lhs, op, coerced_rhs, mod, .sema), | |
| 38026 | else => return Value.compareHeteroAdvanced(coerced_lhs, op, coerced_rhs, pt, .sema), | |
| 37658 | 38027 | } |
| 37659 | 38028 | } |
| 37660 | 38029 | |
| ... | ... | @@ -37664,7 +38033,7 @@ fn valuesEqual( |
| 37664 | 38033 | rhs: Value, |
| 37665 | 38034 | ty: Type, |
| 37666 | 38035 | ) CompileError!bool { |
| 37667 | return lhs.eql(rhs, ty, sema.mod); | |
| 38036 | return lhs.eql(rhs, ty, sema.pt.zcu); | |
| 37668 | 38037 | } |
| 37669 | 38038 | |
| 37670 | 38039 | /// Asserts the values are comparable vectors of type `ty`. |
| ... | ... | @@ -37675,29 +38044,30 @@ fn compareVector( |
| 37675 | 38044 | rhs: Value, |
| 37676 | 38045 | ty: Type, |
| 37677 | 38046 | ) !Value { |
| 37678 | const mod = sema.mod; | |
| 38047 | const pt = sema.pt; | |
| 38048 | const mod = pt.zcu; | |
| 37679 | 38049 | assert(ty.zigTypeTag(mod) == .Vector); |
| 37680 | 38050 | const result_data = try sema.arena.alloc(InternPool.Index, ty.vectorLen(mod)); |
| 37681 | 38051 | for (result_data, 0..) |*scalar, i| { |
| 37682 | const lhs_elem = try lhs.elemValue(sema.mod, i); | |
| 37683 | const rhs_elem = try rhs.elemValue(sema.mod, i); | |
| 38052 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 38053 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 37684 | 38054 | const res_bool = try sema.compareScalar(lhs_elem, op, rhs_elem, ty.scalarType(mod)); |
| 37685 | 38055 | scalar.* = Value.makeBool(res_bool).toIntern(); |
| 37686 | 38056 | } |
| 37687 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 37688 | .ty = (try mod.vectorType(.{ .len = ty.vectorLen(mod), .child = .bool_type })).toIntern(), | |
| 38057 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 38058 | .ty = (try pt.vectorType(.{ .len = ty.vectorLen(mod), .child = .bool_type })).toIntern(), | |
| 37689 | 38059 | .storage = .{ .elems = result_data }, |
| 37690 | } }))); | |
| 38060 | } })); | |
| 37691 | 38061 | } |
| 37692 | 38062 | |
| 37693 | 38063 | /// Merge lhs with rhs. |
| 37694 | 38064 | /// Asserts that lhs and rhs are both error sets and are resolved. |
| 37695 | 38065 | fn errorSetMerge(sema: *Sema, lhs: Type, rhs: Type) !Type { |
| 37696 | const mod = sema.mod; | |
| 37697 | const ip = &mod.intern_pool; | |
| 38066 | const pt = sema.pt; | |
| 38067 | const ip = &pt.zcu.intern_pool; | |
| 37698 | 38068 | const arena = sema.arena; |
| 37699 | const lhs_names = lhs.errorSetNames(mod); | |
| 37700 | const rhs_names = rhs.errorSetNames(mod); | |
| 38069 | const lhs_names = lhs.errorSetNames(pt.zcu); | |
| 38070 | const rhs_names = rhs.errorSetNames(pt.zcu); | |
| 37701 | 38071 | var names: InferredErrorSet.NameMap = .{}; |
| 37702 | 38072 | try names.ensureUnusedCapacity(arena, lhs_names.len); |
| 37703 | 38073 | |
| ... | ... | @@ -37708,7 +38078,7 @@ fn errorSetMerge(sema: *Sema, lhs: Type, rhs: Type) !Type { |
| 37708 | 38078 | try names.put(arena, rhs_names.get(ip)[rhs_index], {}); |
| 37709 | 38079 | } |
| 37710 | 38080 | |
| 37711 | return mod.errorSetFromUnsortedNames(names.keys()); | |
| 38081 | return pt.errorSetFromUnsortedNames(names.keys()); | |
| 37712 | 38082 | } |
| 37713 | 38083 | |
| 37714 | 38084 | /// Avoids crashing the compiler when asking if inferred allocations are noreturn. |
| ... | ... | @@ -37718,7 +38088,7 @@ fn isNoReturn(sema: *Sema, ref: Air.Inst.Ref) bool { |
| 37718 | 38088 | .inferred_alloc, .inferred_alloc_comptime => return false, |
| 37719 | 38089 | else => {}, |
| 37720 | 38090 | }; |
| 37721 | return sema.typeOf(ref).isNoReturn(sema.mod); | |
| 38091 | return sema.typeOf(ref).isNoReturn(sema.pt.zcu); | |
| 37722 | 38092 | } |
| 37723 | 38093 | |
| 37724 | 38094 | /// Avoids crashing the compiler when asking if inferred allocations are known to be a certain zig type. |
| ... | ... | @@ -37727,11 +38097,12 @@ fn isKnownZigType(sema: *Sema, ref: Air.Inst.Ref, tag: std.builtin.TypeId) bool |
| 37727 | 38097 | .inferred_alloc, .inferred_alloc_comptime => return false, |
| 37728 | 38098 | else => {}, |
| 37729 | 38099 | }; |
| 37730 | return sema.typeOf(ref).zigTypeTag(sema.mod) == tag; | |
| 38100 | return sema.typeOf(ref).zigTypeTag(sema.pt.zcu) == tag; | |
| 37731 | 38101 | } |
| 37732 | 38102 | |
| 37733 | 38103 | pub fn declareDependency(sema: *Sema, dependee: InternPool.Dependee) !void { |
| 37734 | if (!sema.mod.comp.debug_incremental) return; | |
| 38104 | const zcu = sema.pt.zcu; | |
| 38105 | if (!zcu.comp.debug_incremental) return; | |
| 37735 | 38106 | |
| 37736 | 38107 | // Avoid creating dependencies on ourselves. This situation can arise when we analyze the fields |
| 37737 | 38108 | // of a type and they use `@This()`. This dependency would be unnecessary, and in fact would |
| ... | ... | @@ -37747,11 +38118,11 @@ pub fn declareDependency(sema: *Sema, dependee: InternPool.Dependee) !void { |
| 37747 | 38118 | else |
| 37748 | 38119 | .{ .decl = sema.owner_decl_index }, |
| 37749 | 38120 | ); |
| 37750 | try sema.mod.intern_pool.addDependency(sema.gpa, depender, dependee); | |
| 38121 | try zcu.intern_pool.addDependency(sema.gpa, depender, dependee); | |
| 37751 | 38122 | } |
| 37752 | 38123 | |
| 37753 | 38124 | fn isComptimeMutablePtr(sema: *Sema, val: Value) bool { |
| 37754 | return switch (sema.mod.intern_pool.indexToKey(val.toIntern())) { | |
| 38125 | return switch (sema.pt.zcu.intern_pool.indexToKey(val.toIntern())) { | |
| 37755 | 38126 | .slice => |slice| sema.isComptimeMutablePtr(Value.fromInterned(slice.ptr)), |
| 37756 | 38127 | .ptr => |ptr| switch (ptr.base_addr) { |
| 37757 | 38128 | .anon_decl, .decl, .int => false, |
| ... | ... | @@ -37766,7 +38137,7 @@ fn isComptimeMutablePtr(sema: *Sema, val: Value) bool { |
| 37766 | 38137 | |
| 37767 | 38138 | fn checkRuntimeValue(sema: *Sema, ptr: Air.Inst.Ref) bool { |
| 37768 | 38139 | const val = ptr.toInterned() orelse return true; |
| 37769 | return !Value.fromInterned(val).canMutateComptimeVarState(sema.mod); | |
| 38140 | return !Value.fromInterned(val).canMutateComptimeVarState(sema.pt.zcu); | |
| 37770 | 38141 | } |
| 37771 | 38142 | |
| 37772 | 38143 | fn validateRuntimeValue(sema: *Sema, block: *Block, val_src: LazySrcLoc, val: Air.Inst.Ref) CompileError!void { |
| ... | ... | @@ -37781,7 +38152,8 @@ fn validateRuntimeValue(sema: *Sema, block: *Block, val_src: LazySrcLoc, val: Ai |
| 37781 | 38152 | |
| 37782 | 38153 | /// Returns true if any value contained in `val` is undefined. |
| 37783 | 38154 | fn anyUndef(sema: *Sema, block: *Block, src: LazySrcLoc, val: Value) !bool { |
| 37784 | const mod = sema.mod; | |
| 38155 | const pt = sema.pt; | |
| 38156 | const mod = pt.zcu; | |
| 37785 | 38157 | return switch (mod.intern_pool.indexToKey(val.toIntern())) { |
| 37786 | 38158 | .undef => true, |
| 37787 | 38159 | .simple_value => |v| v == .undefined, |
| ... | ... | @@ -37807,13 +38179,14 @@ fn sliceToIpString( |
| 37807 | 38179 | slice_val: Value, |
| 37808 | 38180 | reason: NeededComptimeReason, |
| 37809 | 38181 | ) CompileError!InternPool.NullTerminatedString { |
| 37810 | const zcu = sema.mod; | |
| 38182 | const pt = sema.pt; | |
| 38183 | const zcu = pt.zcu; | |
| 37811 | 38184 | const slice_ty = slice_val.typeOf(zcu); |
| 37812 | 38185 | assert(slice_ty.isSlice(zcu)); |
| 37813 | 38186 | assert(slice_ty.childType(zcu).toIntern() == .u8_type); |
| 37814 | 38187 | const array_val = try sema.derefSliceAsArray(block, src, slice_val, reason); |
| 37815 | 38188 | const array_ty = array_val.typeOf(zcu); |
| 37816 | return array_val.toIpString(array_ty, zcu); | |
| 38189 | return array_val.toIpString(array_ty, pt); | |
| 37817 | 38190 | } |
| 37818 | 38191 | |
| 37819 | 38192 | /// Given a slice value, attempts to dereference it into a comptime-known array. |
| ... | ... | @@ -37840,7 +38213,8 @@ fn maybeDerefSliceAsArray( |
| 37840 | 38213 | src: LazySrcLoc, |
| 37841 | 38214 | slice_val: Value, |
| 37842 | 38215 | ) CompileError!?Value { |
| 37843 | const zcu = sema.mod; | |
| 38216 | const pt = sema.pt; | |
| 38217 | const zcu = pt.zcu; | |
| 37844 | 38218 | const ip = &zcu.intern_pool; |
| 37845 | 38219 | assert(slice_val.typeOf(zcu).isSlice(zcu)); |
| 37846 | 38220 | const slice = switch (ip.indexToKey(slice_val.toIntern())) { |
| ... | ... | @@ -37849,19 +38223,19 @@ fn maybeDerefSliceAsArray( |
| 37849 | 38223 | else => unreachable, |
| 37850 | 38224 | }; |
| 37851 | 38225 | const elem_ty = Type.fromInterned(slice.ty).childType(zcu); |
| 37852 | const len = try Value.fromInterned(slice.len).toUnsignedIntSema(zcu); | |
| 37853 | const array_ty = try zcu.arrayType(.{ | |
| 38226 | const len = try Value.fromInterned(slice.len).toUnsignedIntSema(pt); | |
| 38227 | const array_ty = try pt.arrayType(.{ | |
| 37854 | 38228 | .child = elem_ty.toIntern(), |
| 37855 | 38229 | .len = len, |
| 37856 | 38230 | }); |
| 37857 | const ptr_ty = try zcu.ptrTypeSema(p: { | |
| 38231 | const ptr_ty = try pt.ptrTypeSema(p: { | |
| 37858 | 38232 | var p = Type.fromInterned(slice.ty).ptrInfo(zcu); |
| 37859 | 38233 | p.flags.size = .One; |
| 37860 | 38234 | p.child = array_ty.toIntern(); |
| 37861 | 38235 | p.sentinel = .none; |
| 37862 | 38236 | break :p p; |
| 37863 | 38237 | }); |
| 37864 | const casted_ptr = try zcu.getCoerced(Value.fromInterned(slice.ptr), ptr_ty); | |
| 38238 | const casted_ptr = try pt.getCoerced(Value.fromInterned(slice.ptr), ptr_ty); | |
| 37865 | 38239 | return sema.pointerDeref(block, src, casted_ptr, ptr_ty); |
| 37866 | 38240 | } |
| 37867 | 38241 | |
| ... | ... | @@ -37879,7 +38253,7 @@ fn analyzeUnreachable(sema: *Sema, block: *Block, src: LazySrcLoc, safety_check: |
| 37879 | 38253 | pub fn flushExports(sema: *Sema) !void { |
| 37880 | 38254 | if (sema.exports.items.len == 0) return; |
| 37881 | 38255 | |
| 37882 | const zcu = sema.mod; | |
| 38256 | const zcu = sema.pt.zcu; | |
| 37883 | 38257 | const gpa = zcu.gpa; |
| 37884 | 38258 | |
| 37885 | 38259 | const unit = sema.ownerUnit(); |
src/Sema/bitcast.zig+96-92| ... | ... | @@ -69,7 +69,8 @@ fn bitCastInner( |
| 69 | 69 | host_bits: u64, |
| 70 | 70 | bit_offset: u64, |
| 71 | 71 | ) BitCastError!Value { |
| 72 | const zcu = sema.mod; | |
| 72 | const pt = sema.pt; | |
| 73 | const zcu = pt.zcu; | |
| 73 | 74 | const endian = zcu.getTarget().cpu.arch.endian(); |
| 74 | 75 | |
| 75 | 76 | if (dest_ty.toIntern() == val.typeOf(zcu).toIntern() and bit_offset == 0) { |
| ... | ... | @@ -78,29 +79,29 @@ fn bitCastInner( |
| 78 | 79 | |
| 79 | 80 | const val_ty = val.typeOf(zcu); |
| 80 | 81 | |
| 81 | try val_ty.resolveLayout(zcu); | |
| 82 | try dest_ty.resolveLayout(zcu); | |
| 82 | try val_ty.resolveLayout(pt); | |
| 83 | try dest_ty.resolveLayout(pt); | |
| 83 | 84 | |
| 84 | 85 | assert(val_ty.hasWellDefinedLayout(zcu)); |
| 85 | 86 | |
| 86 | 87 | const abi_pad_bits, const host_pad_bits = if (host_bits > 0) |
| 87 | .{ val_ty.abiSize(zcu) * 8 - host_bits, host_bits - val_ty.bitSize(zcu) } | |
| 88 | .{ val_ty.abiSize(pt) * 8 - host_bits, host_bits - val_ty.bitSize(pt) } | |
| 88 | 89 | else |
| 89 | .{ val_ty.abiSize(zcu) * 8 - val_ty.bitSize(zcu), 0 }; | |
| 90 | .{ val_ty.abiSize(pt) * 8 - val_ty.bitSize(pt), 0 }; | |
| 90 | 91 | |
| 91 | 92 | const skip_bits = switch (endian) { |
| 92 | 93 | .little => bit_offset + byte_offset * 8, |
| 93 | 94 | .big => if (host_bits > 0) |
| 94 | val_ty.abiSize(zcu) * 8 - byte_offset * 8 - host_bits + bit_offset | |
| 95 | val_ty.abiSize(pt) * 8 - byte_offset * 8 - host_bits + bit_offset | |
| 95 | 96 | else |
| 96 | val_ty.abiSize(zcu) * 8 - byte_offset * 8 - dest_ty.bitSize(zcu), | |
| 97 | val_ty.abiSize(pt) * 8 - byte_offset * 8 - dest_ty.bitSize(pt), | |
| 97 | 98 | }; |
| 98 | 99 | |
| 99 | 100 | var unpack: UnpackValueBits = .{ |
| 100 | .zcu = zcu, | |
| 101 | .pt = sema.pt, | |
| 101 | 102 | .arena = sema.arena, |
| 102 | 103 | .skip_bits = skip_bits, |
| 103 | .remaining_bits = dest_ty.bitSize(zcu), | |
| 104 | .remaining_bits = dest_ty.bitSize(pt), | |
| 104 | 105 | .unpacked = std.ArrayList(InternPool.Index).init(sema.arena), |
| 105 | 106 | }; |
| 106 | 107 | switch (endian) { |
| ... | ... | @@ -116,7 +117,7 @@ fn bitCastInner( |
| 116 | 117 | try unpack.padding(host_pad_bits); |
| 117 | 118 | |
| 118 | 119 | var pack: PackValueBits = .{ |
| 119 | .zcu = zcu, | |
| 120 | .pt = sema.pt, | |
| 120 | 121 | .arena = sema.arena, |
| 121 | 122 | .unpacked = unpack.unpacked.items, |
| 122 | 123 | }; |
| ... | ... | @@ -131,33 +132,34 @@ fn bitCastSpliceInner( |
| 131 | 132 | host_bits: u64, |
| 132 | 133 | bit_offset: u64, |
| 133 | 134 | ) BitCastError!Value { |
| 134 | const zcu = sema.mod; | |
| 135 | const pt = sema.pt; | |
| 136 | const zcu = pt.zcu; | |
| 135 | 137 | const endian = zcu.getTarget().cpu.arch.endian(); |
| 136 | 138 | const val_ty = val.typeOf(zcu); |
| 137 | 139 | const splice_val_ty = splice_val.typeOf(zcu); |
| 138 | 140 | |
| 139 | try val_ty.resolveLayout(zcu); | |
| 140 | try splice_val_ty.resolveLayout(zcu); | |
| 141 | try val_ty.resolveLayout(pt); | |
| 142 | try splice_val_ty.resolveLayout(pt); | |
| 141 | 143 | |
| 142 | const splice_bits = splice_val_ty.bitSize(zcu); | |
| 144 | const splice_bits = splice_val_ty.bitSize(pt); | |
| 143 | 145 | |
| 144 | 146 | const splice_offset = switch (endian) { |
| 145 | 147 | .little => bit_offset + byte_offset * 8, |
| 146 | 148 | .big => if (host_bits > 0) |
| 147 | val_ty.abiSize(zcu) * 8 - byte_offset * 8 - host_bits + bit_offset | |
| 149 | val_ty.abiSize(pt) * 8 - byte_offset * 8 - host_bits + bit_offset | |
| 148 | 150 | else |
| 149 | val_ty.abiSize(zcu) * 8 - byte_offset * 8 - splice_bits, | |
| 151 | val_ty.abiSize(pt) * 8 - byte_offset * 8 - splice_bits, | |
| 150 | 152 | }; |
| 151 | 153 | |
| 152 | assert(splice_offset + splice_bits <= val_ty.abiSize(zcu) * 8); | |
| 154 | assert(splice_offset + splice_bits <= val_ty.abiSize(pt) * 8); | |
| 153 | 155 | |
| 154 | 156 | const abi_pad_bits, const host_pad_bits = if (host_bits > 0) |
| 155 | .{ val_ty.abiSize(zcu) * 8 - host_bits, host_bits - val_ty.bitSize(zcu) } | |
| 157 | .{ val_ty.abiSize(pt) * 8 - host_bits, host_bits - val_ty.bitSize(pt) } | |
| 156 | 158 | else |
| 157 | .{ val_ty.abiSize(zcu) * 8 - val_ty.bitSize(zcu), 0 }; | |
| 159 | .{ val_ty.abiSize(pt) * 8 - val_ty.bitSize(pt), 0 }; | |
| 158 | 160 | |
| 159 | 161 | var unpack: UnpackValueBits = .{ |
| 160 | .zcu = zcu, | |
| 162 | .pt = pt, | |
| 161 | 163 | .arena = sema.arena, |
| 162 | 164 | .skip_bits = 0, |
| 163 | 165 | .remaining_bits = splice_offset, |
| ... | ... | @@ -179,7 +181,7 @@ fn bitCastSpliceInner( |
| 179 | 181 | try unpack.add(splice_val); |
| 180 | 182 | |
| 181 | 183 | unpack.skip_bits = splice_offset + splice_bits; |
| 182 | unpack.remaining_bits = val_ty.abiSize(zcu) * 8 - splice_offset - splice_bits; | |
| 184 | unpack.remaining_bits = val_ty.abiSize(pt) * 8 - splice_offset - splice_bits; | |
| 183 | 185 | switch (endian) { |
| 184 | 186 | .little => { |
| 185 | 187 | try unpack.add(val); |
| ... | ... | @@ -193,7 +195,7 @@ fn bitCastSpliceInner( |
| 193 | 195 | try unpack.padding(host_pad_bits); |
| 194 | 196 | |
| 195 | 197 | var pack: PackValueBits = .{ |
| 196 | .zcu = zcu, | |
| 198 | .pt = pt, | |
| 197 | 199 | .arena = sema.arena, |
| 198 | 200 | .unpacked = unpack.unpacked.items, |
| 199 | 201 | }; |
| ... | ... | @@ -209,7 +211,7 @@ fn bitCastSpliceInner( |
| 209 | 211 | /// of values in *packed* memory - therefore, on big-endian targets, the first element of this |
| 210 | 212 | /// list contains bits from the *final* byte of the value. |
| 211 | 213 | const UnpackValueBits = struct { |
| 212 | zcu: *Zcu, | |
| 214 | pt: Zcu.PerThread, | |
| 213 | 215 | arena: Allocator, |
| 214 | 216 | skip_bits: u64, |
| 215 | 217 | remaining_bits: u64, |
| ... | ... | @@ -217,7 +219,8 @@ const UnpackValueBits = struct { |
| 217 | 219 | unpacked: std.ArrayList(InternPool.Index), |
| 218 | 220 | |
| 219 | 221 | fn add(unpack: *UnpackValueBits, val: Value) BitCastError!void { |
| 220 | const zcu = unpack.zcu; | |
| 222 | const pt = unpack.pt; | |
| 223 | const zcu = pt.zcu; | |
| 221 | 224 | const endian = zcu.getTarget().cpu.arch.endian(); |
| 222 | 225 | const ip = &zcu.intern_pool; |
| 223 | 226 | |
| ... | ... | @@ -226,7 +229,7 @@ const UnpackValueBits = struct { |
| 226 | 229 | } |
| 227 | 230 | |
| 228 | 231 | const ty = val.typeOf(zcu); |
| 229 | const bit_size = ty.bitSize(zcu); | |
| 232 | const bit_size = ty.bitSize(pt); | |
| 230 | 233 | |
| 231 | 234 | if (unpack.skip_bits >= bit_size) { |
| 232 | 235 | unpack.skip_bits -= bit_size; |
| ... | ... | @@ -279,7 +282,7 @@ const UnpackValueBits = struct { |
| 279 | 282 | .little => i, |
| 280 | 283 | .big => len - i - 1, |
| 281 | 284 | }; |
| 282 | const elem_val = try val.elemValue(zcu, real_idx); | |
| 285 | const elem_val = try val.elemValue(pt, real_idx); | |
| 283 | 286 | try unpack.add(elem_val); |
| 284 | 287 | } |
| 285 | 288 | }, |
| ... | ... | @@ -288,7 +291,7 @@ const UnpackValueBits = struct { |
| 288 | 291 | // The final element does not have trailing padding. |
| 289 | 292 | // Elements are reversed in packed memory on BE targets. |
| 290 | 293 | const elem_ty = ty.childType(zcu); |
| 291 | const pad_bits = elem_ty.abiSize(zcu) * 8 - elem_ty.bitSize(zcu); | |
| 294 | const pad_bits = elem_ty.abiSize(pt) * 8 - elem_ty.bitSize(pt); | |
| 292 | 295 | const len = ty.arrayLen(zcu); |
| 293 | 296 | const maybe_sent = ty.sentinel(zcu); |
| 294 | 297 | |
| ... | ... | @@ -303,7 +306,7 @@ const UnpackValueBits = struct { |
| 303 | 306 | .little => i, |
| 304 | 307 | .big => len - i - 1, |
| 305 | 308 | }; |
| 306 | const elem_val = try val.elemValue(zcu, @intCast(real_idx)); | |
| 309 | const elem_val = try val.elemValue(pt, @intCast(real_idx)); | |
| 307 | 310 | try unpack.add(elem_val); |
| 308 | 311 | if (i != len - 1) try unpack.padding(pad_bits); |
| 309 | 312 | } |
| ... | ... | @@ -320,12 +323,12 @@ const UnpackValueBits = struct { |
| 320 | 323 | var cur_bit_off: u64 = 0; |
| 321 | 324 | var it = zcu.typeToStruct(ty).?.iterateRuntimeOrder(ip); |
| 322 | 325 | while (it.next()) |field_idx| { |
| 323 | const want_bit_off = ty.structFieldOffset(field_idx, zcu) * 8; | |
| 326 | const want_bit_off = ty.structFieldOffset(field_idx, pt) * 8; | |
| 324 | 327 | const pad_bits = want_bit_off - cur_bit_off; |
| 325 | const field_val = try val.fieldValue(zcu, field_idx); | |
| 328 | const field_val = try val.fieldValue(pt, field_idx); | |
| 326 | 329 | try unpack.padding(pad_bits); |
| 327 | 330 | try unpack.add(field_val); |
| 328 | cur_bit_off = want_bit_off + field_val.typeOf(zcu).bitSize(zcu); | |
| 331 | cur_bit_off = want_bit_off + field_val.typeOf(zcu).bitSize(pt); | |
| 329 | 332 | } |
| 330 | 333 | // Add trailing padding bits. |
| 331 | 334 | try unpack.padding(bit_size - cur_bit_off); |
| ... | ... | @@ -334,13 +337,13 @@ const UnpackValueBits = struct { |
| 334 | 337 | var cur_bit_off: u64 = bit_size; |
| 335 | 338 | var it = zcu.typeToStruct(ty).?.iterateRuntimeOrderReverse(ip); |
| 336 | 339 | while (it.next()) |field_idx| { |
| 337 | const field_val = try val.fieldValue(zcu, field_idx); | |
| 340 | const field_val = try val.fieldValue(pt, field_idx); | |
| 338 | 341 | const field_ty = field_val.typeOf(zcu); |
| 339 | const want_bit_off = ty.structFieldOffset(field_idx, zcu) * 8 + field_ty.bitSize(zcu); | |
| 342 | const want_bit_off = ty.structFieldOffset(field_idx, pt) * 8 + field_ty.bitSize(pt); | |
| 340 | 343 | const pad_bits = cur_bit_off - want_bit_off; |
| 341 | 344 | try unpack.padding(pad_bits); |
| 342 | 345 | try unpack.add(field_val); |
| 343 | cur_bit_off = want_bit_off - field_ty.bitSize(zcu); | |
| 346 | cur_bit_off = want_bit_off - field_ty.bitSize(pt); | |
| 344 | 347 | } |
| 345 | 348 | assert(cur_bit_off == 0); |
| 346 | 349 | }, |
| ... | ... | @@ -349,7 +352,7 @@ const UnpackValueBits = struct { |
| 349 | 352 | // Just add all fields in order. There are no padding bits. |
| 350 | 353 | // This is identical between LE and BE targets. |
| 351 | 354 | for (0..ty.structFieldCount(zcu)) |i| { |
| 352 | const field_val = try val.fieldValue(zcu, i); | |
| 355 | const field_val = try val.fieldValue(pt, i); | |
| 353 | 356 | try unpack.add(field_val); |
| 354 | 357 | } |
| 355 | 358 | }, |
| ... | ... | @@ -363,7 +366,7 @@ const UnpackValueBits = struct { |
| 363 | 366 | // This correctly handles the case where `tag == .none`, since the payload is then |
| 364 | 367 | // either an integer or a byte array, both of which we can unpack. |
| 365 | 368 | const payload_val = Value.fromInterned(un.val); |
| 366 | const pad_bits = bit_size - payload_val.typeOf(zcu).bitSize(zcu); | |
| 369 | const pad_bits = bit_size - payload_val.typeOf(zcu).bitSize(pt); | |
| 367 | 370 | if (endian == .little or ty.containerLayout(zcu) == .@"packed") { |
| 368 | 371 | try unpack.add(payload_val); |
| 369 | 372 | try unpack.padding(pad_bits); |
| ... | ... | @@ -377,31 +380,31 @@ const UnpackValueBits = struct { |
| 377 | 380 | |
| 378 | 381 | fn padding(unpack: *UnpackValueBits, pad_bits: u64) BitCastError!void { |
| 379 | 382 | if (pad_bits == 0) return; |
| 380 | const zcu = unpack.zcu; | |
| 383 | const pt = unpack.pt; | |
| 381 | 384 | // Figure out how many full bytes and leftover bits there are. |
| 382 | 385 | const bytes = pad_bits / 8; |
| 383 | 386 | const bits = pad_bits % 8; |
| 384 | 387 | // Add undef u8 values for the bytes... |
| 385 | const undef_u8 = try zcu.undefValue(Type.u8); | |
| 388 | const undef_u8 = try pt.undefValue(Type.u8); | |
| 386 | 389 | for (0..@intCast(bytes)) |_| { |
| 387 | 390 | try unpack.primitive(undef_u8); |
| 388 | 391 | } |
| 389 | 392 | // ...and an undef int for the leftover bits. |
| 390 | 393 | if (bits == 0) return; |
| 391 | const bits_ty = try zcu.intType(.unsigned, @intCast(bits)); | |
| 392 | const bits_val = try zcu.undefValue(bits_ty); | |
| 394 | const bits_ty = try pt.intType(.unsigned, @intCast(bits)); | |
| 395 | const bits_val = try pt.undefValue(bits_ty); | |
| 393 | 396 | try unpack.primitive(bits_val); |
| 394 | 397 | } |
| 395 | 398 | |
| 396 | 399 | fn primitive(unpack: *UnpackValueBits, val: Value) BitCastError!void { |
| 397 | const zcu = unpack.zcu; | |
| 400 | const pt = unpack.pt; | |
| 398 | 401 | |
| 399 | 402 | if (unpack.remaining_bits == 0) { |
| 400 | 403 | return; |
| 401 | 404 | } |
| 402 | 405 | |
| 403 | const ty = val.typeOf(zcu); | |
| 404 | const bit_size = ty.bitSize(zcu); | |
| 406 | const ty = val.typeOf(pt.zcu); | |
| 407 | const bit_size = ty.bitSize(pt); | |
| 405 | 408 | |
| 406 | 409 | // Note that this skips all zero-bit types. |
| 407 | 410 | if (unpack.skip_bits >= bit_size) { |
| ... | ... | @@ -425,21 +428,21 @@ const UnpackValueBits = struct { |
| 425 | 428 | } |
| 426 | 429 | |
| 427 | 430 | fn splitPrimitive(unpack: *UnpackValueBits, val: Value, bit_offset: u64, bit_count: u64) BitCastError!void { |
| 428 | const zcu = unpack.zcu; | |
| 429 | const ty = val.typeOf(zcu); | |
| 431 | const pt = unpack.pt; | |
| 432 | const ty = val.typeOf(pt.zcu); | |
| 430 | 433 | |
| 431 | const val_bits = ty.bitSize(zcu); | |
| 434 | const val_bits = ty.bitSize(pt); | |
| 432 | 435 | assert(bit_offset + bit_count <= val_bits); |
| 433 | 436 | |
| 434 | switch (zcu.intern_pool.indexToKey(val.toIntern())) { | |
| 437 | switch (pt.zcu.intern_pool.indexToKey(val.toIntern())) { | |
| 435 | 438 | // In the `ptr` case, this will return `error.ReinterpretDeclRef` |
| 436 | 439 | // if we're trying to split a non-integer pointer value. |
| 437 | 440 | .int, .float, .enum_tag, .ptr, .opt => { |
| 438 | 441 | // This @intCast is okay because no primitive can exceed the size of a u16. |
| 439 | const int_ty = try zcu.intType(.unsigned, @intCast(bit_count)); | |
| 442 | const int_ty = try unpack.pt.intType(.unsigned, @intCast(bit_count)); | |
| 440 | 443 | const buf = try unpack.arena.alloc(u8, @intCast((val_bits + 7) / 8)); |
| 441 | try val.writeToPackedMemory(ty, zcu, buf, 0); | |
| 442 | const sub_val = try Value.readFromPackedMemory(int_ty, zcu, buf, @intCast(bit_offset), unpack.arena); | |
| 444 | try val.writeToPackedMemory(ty, unpack.pt, buf, 0); | |
| 445 | const sub_val = try Value.readFromPackedMemory(int_ty, unpack.pt, buf, @intCast(bit_offset), unpack.arena); | |
| 443 | 446 | try unpack.primitive(sub_val); |
| 444 | 447 | }, |
| 445 | 448 | .undef => try unpack.padding(bit_count), |
| ... | ... | @@ -456,13 +459,14 @@ const UnpackValueBits = struct { |
| 456 | 459 | /// reconstructs a value of an arbitrary type, with correct handling of `undefined` |
| 457 | 460 | /// values and of pointers which align in virtual memory. |
| 458 | 461 | const PackValueBits = struct { |
| 459 | zcu: *Zcu, | |
| 462 | pt: Zcu.PerThread, | |
| 460 | 463 | arena: Allocator, |
| 461 | 464 | bit_offset: u64 = 0, |
| 462 | 465 | unpacked: []const InternPool.Index, |
| 463 | 466 | |
| 464 | 467 | fn get(pack: *PackValueBits, ty: Type) BitCastError!Value { |
| 465 | const zcu = pack.zcu; | |
| 468 | const pt = pack.pt; | |
| 469 | const zcu = pt.zcu; | |
| 466 | 470 | const endian = zcu.getTarget().cpu.arch.endian(); |
| 467 | 471 | const ip = &zcu.intern_pool; |
| 468 | 472 | const arena = pack.arena; |
| ... | ... | @@ -485,7 +489,7 @@ const PackValueBits = struct { |
| 485 | 489 | } |
| 486 | 490 | }, |
| 487 | 491 | } |
| 488 | return Value.fromInterned(try zcu.intern(.{ .aggregate = .{ | |
| 492 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 489 | 493 | .ty = ty.toIntern(), |
| 490 | 494 | .storage = .{ .elems = elems }, |
| 491 | 495 | } })); |
| ... | ... | @@ -495,12 +499,12 @@ const PackValueBits = struct { |
| 495 | 499 | const len = ty.arrayLen(zcu); |
| 496 | 500 | const elem_ty = ty.childType(zcu); |
| 497 | 501 | const maybe_sent = ty.sentinel(zcu); |
| 498 | const pad_bits = elem_ty.abiSize(zcu) * 8 - elem_ty.bitSize(zcu); | |
| 502 | const pad_bits = elem_ty.abiSize(pt) * 8 - elem_ty.bitSize(pt); | |
| 499 | 503 | const elems = try arena.alloc(InternPool.Index, @intCast(len)); |
| 500 | 504 | |
| 501 | 505 | if (endian == .big and maybe_sent != null) { |
| 502 | 506 | // TODO: validate sentinel was preserved! |
| 503 | try pack.padding(elem_ty.bitSize(zcu)); | |
| 507 | try pack.padding(elem_ty.bitSize(pt)); | |
| 504 | 508 | if (len != 0) try pack.padding(pad_bits); |
| 505 | 509 | } |
| 506 | 510 | |
| ... | ... | @@ -516,10 +520,10 @@ const PackValueBits = struct { |
| 516 | 520 | if (endian == .little and maybe_sent != null) { |
| 517 | 521 | // TODO: validate sentinel was preserved! |
| 518 | 522 | if (len != 0) try pack.padding(pad_bits); |
| 519 | try pack.padding(elem_ty.bitSize(zcu)); | |
| 523 | try pack.padding(elem_ty.bitSize(pt)); | |
| 520 | 524 | } |
| 521 | 525 | |
| 522 | return Value.fromInterned(try zcu.intern(.{ .aggregate = .{ | |
| 526 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 523 | 527 | .ty = ty.toIntern(), |
| 524 | 528 | .storage = .{ .elems = elems }, |
| 525 | 529 | } })); |
| ... | ... | @@ -534,23 +538,23 @@ const PackValueBits = struct { |
| 534 | 538 | var cur_bit_off: u64 = 0; |
| 535 | 539 | var it = zcu.typeToStruct(ty).?.iterateRuntimeOrder(ip); |
| 536 | 540 | while (it.next()) |field_idx| { |
| 537 | const want_bit_off = ty.structFieldOffset(field_idx, zcu) * 8; | |
| 541 | const want_bit_off = ty.structFieldOffset(field_idx, pt) * 8; | |
| 538 | 542 | try pack.padding(want_bit_off - cur_bit_off); |
| 539 | 543 | const field_ty = ty.structFieldType(field_idx, zcu); |
| 540 | 544 | elems[field_idx] = (try pack.get(field_ty)).toIntern(); |
| 541 | cur_bit_off = want_bit_off + field_ty.bitSize(zcu); | |
| 545 | cur_bit_off = want_bit_off + field_ty.bitSize(pt); | |
| 542 | 546 | } |
| 543 | try pack.padding(ty.bitSize(zcu) - cur_bit_off); | |
| 547 | try pack.padding(ty.bitSize(pt) - cur_bit_off); | |
| 544 | 548 | }, |
| 545 | 549 | .big => { |
| 546 | var cur_bit_off: u64 = ty.bitSize(zcu); | |
| 550 | var cur_bit_off: u64 = ty.bitSize(pt); | |
| 547 | 551 | var it = zcu.typeToStruct(ty).?.iterateRuntimeOrderReverse(ip); |
| 548 | 552 | while (it.next()) |field_idx| { |
| 549 | 553 | const field_ty = ty.structFieldType(field_idx, zcu); |
| 550 | const want_bit_off = ty.structFieldOffset(field_idx, zcu) * 8 + field_ty.bitSize(zcu); | |
| 554 | const want_bit_off = ty.structFieldOffset(field_idx, pt) * 8 + field_ty.bitSize(pt); | |
| 551 | 555 | try pack.padding(cur_bit_off - want_bit_off); |
| 552 | 556 | elems[field_idx] = (try pack.get(field_ty)).toIntern(); |
| 553 | cur_bit_off = want_bit_off - field_ty.bitSize(zcu); | |
| 557 | cur_bit_off = want_bit_off - field_ty.bitSize(pt); | |
| 554 | 558 | } |
| 555 | 559 | assert(cur_bit_off == 0); |
| 556 | 560 | }, |
| ... | ... | @@ -559,10 +563,10 @@ const PackValueBits = struct { |
| 559 | 563 | // Fill those values now. |
| 560 | 564 | for (elems, 0..) |*elem, field_idx| { |
| 561 | 565 | if (elem.* != .none) continue; |
| 562 | const val = (try ty.structFieldValueComptime(zcu, field_idx)).?; | |
| 566 | const val = (try ty.structFieldValueComptime(pt, field_idx)).?; | |
| 563 | 567 | elem.* = val.toIntern(); |
| 564 | 568 | } |
| 565 | return Value.fromInterned(try zcu.intern(.{ .aggregate = .{ | |
| 569 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 566 | 570 | .ty = ty.toIntern(), |
| 567 | 571 | .storage = .{ .elems = elems }, |
| 568 | 572 | } })); |
| ... | ... | @@ -575,7 +579,7 @@ const PackValueBits = struct { |
| 575 | 579 | const field_ty = ty.structFieldType(i, zcu); |
| 576 | 580 | elem.* = (try pack.get(field_ty)).toIntern(); |
| 577 | 581 | } |
| 578 | return Value.fromInterned(try zcu.intern(.{ .aggregate = .{ | |
| 582 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 579 | 583 | .ty = ty.toIntern(), |
| 580 | 584 | .storage = .{ .elems = elems }, |
| 581 | 585 | } })); |
| ... | ... | @@ -591,7 +595,7 @@ const PackValueBits = struct { |
| 591 | 595 | const prev_unpacked = pack.unpacked; |
| 592 | 596 | const prev_bit_offset = pack.bit_offset; |
| 593 | 597 | |
| 594 | const backing_ty = try ty.unionBackingType(zcu); | |
| 598 | const backing_ty = try ty.unionBackingType(pt); | |
| 595 | 599 | |
| 596 | 600 | backing: { |
| 597 | 601 | const backing_val = pack.get(backing_ty) catch |err| switch (err) { |
| ... | ... | @@ -607,7 +611,7 @@ const PackValueBits = struct { |
| 607 | 611 | pack.bit_offset = prev_bit_offset; |
| 608 | 612 | break :backing; |
| 609 | 613 | } |
| 610 | return Value.fromInterned(try zcu.intern(.{ .un = .{ | |
| 614 | return Value.fromInterned(try pt.intern(.{ .un = .{ | |
| 611 | 615 | .ty = ty.toIntern(), |
| 612 | 616 | .tag = .none, |
| 613 | 617 | .val = backing_val.toIntern(), |
| ... | ... | @@ -618,16 +622,16 @@ const PackValueBits = struct { |
| 618 | 622 | for (field_order, 0..) |*f, i| f.* = @intCast(i); |
| 619 | 623 | // Sort `field_order` to put the fields with the largest bit sizes first. |
| 620 | 624 | const SizeSortCtx = struct { |
| 621 | zcu: *Zcu, | |
| 625 | pt: Zcu.PerThread, | |
| 622 | 626 | field_types: []const InternPool.Index, |
| 623 | 627 | fn lessThan(ctx: @This(), a_idx: u32, b_idx: u32) bool { |
| 624 | 628 | const a_ty = Type.fromInterned(ctx.field_types[a_idx]); |
| 625 | 629 | const b_ty = Type.fromInterned(ctx.field_types[b_idx]); |
| 626 | return a_ty.bitSize(ctx.zcu) > b_ty.bitSize(ctx.zcu); | |
| 630 | return a_ty.bitSize(ctx.pt) > b_ty.bitSize(ctx.pt); | |
| 627 | 631 | } |
| 628 | 632 | }; |
| 629 | 633 | std.mem.sortUnstable(u32, field_order, SizeSortCtx{ |
| 630 | .zcu = zcu, | |
| 634 | .pt = pt, | |
| 631 | 635 | .field_types = zcu.typeToUnion(ty).?.field_types.get(ip), |
| 632 | 636 | }, SizeSortCtx.lessThan); |
| 633 | 637 | |
| ... | ... | @@ -635,7 +639,7 @@ const PackValueBits = struct { |
| 635 | 639 | |
| 636 | 640 | for (field_order) |field_idx| { |
| 637 | 641 | const field_ty = Type.fromInterned(zcu.typeToUnion(ty).?.field_types.get(ip)[field_idx]); |
| 638 | const pad_bits = ty.bitSize(zcu) - field_ty.bitSize(zcu); | |
| 642 | const pad_bits = ty.bitSize(pt) - field_ty.bitSize(pt); | |
| 639 | 643 | if (!padding_after) try pack.padding(pad_bits); |
| 640 | 644 | const field_val = pack.get(field_ty) catch |err| switch (err) { |
| 641 | 645 | error.ReinterpretDeclRef => { |
| ... | ... | @@ -651,8 +655,8 @@ const PackValueBits = struct { |
| 651 | 655 | pack.bit_offset = prev_bit_offset; |
| 652 | 656 | continue; |
| 653 | 657 | } |
| 654 | const tag_val = try zcu.enumValueFieldIndex(ty.unionTagTypeHypothetical(zcu), field_idx); | |
| 655 | return Value.fromInterned(try zcu.intern(.{ .un = .{ | |
| 658 | const tag_val = try pt.enumValueFieldIndex(ty.unionTagTypeHypothetical(zcu), field_idx); | |
| 659 | return Value.fromInterned(try pt.intern(.{ .un = .{ | |
| 656 | 660 | .ty = ty.toIntern(), |
| 657 | 661 | .tag = tag_val.toIntern(), |
| 658 | 662 | .val = field_val.toIntern(), |
| ... | ... | @@ -662,7 +666,7 @@ const PackValueBits = struct { |
| 662 | 666 | // No field could represent the value. Just do whatever happens when we try to read |
| 663 | 667 | // the backing type - either `undefined` or `error.ReinterpretDeclRef`. |
| 664 | 668 | const backing_val = try pack.get(backing_ty); |
| 665 | return Value.fromInterned(try zcu.intern(.{ .un = .{ | |
| 669 | return Value.fromInterned(try pt.intern(.{ .un = .{ | |
| 666 | 670 | .ty = ty.toIntern(), |
| 667 | 671 | .tag = .none, |
| 668 | 672 | .val = backing_val.toIntern(), |
| ... | ... | @@ -677,14 +681,14 @@ const PackValueBits = struct { |
| 677 | 681 | } |
| 678 | 682 | |
| 679 | 683 | fn primitive(pack: *PackValueBits, want_ty: Type) BitCastError!Value { |
| 680 | const zcu = pack.zcu; | |
| 681 | const vals, const bit_offset = pack.prepareBits(want_ty.bitSize(zcu)); | |
| 684 | const pt = pack.pt; | |
| 685 | const vals, const bit_offset = pack.prepareBits(want_ty.bitSize(pt)); | |
| 682 | 686 | |
| 683 | 687 | for (vals) |val| { |
| 684 | if (!Value.fromInterned(val).isUndef(zcu)) break; | |
| 688 | if (!Value.fromInterned(val).isUndef(pt.zcu)) break; | |
| 685 | 689 | } else { |
| 686 | 690 | // All bits of the value are `undefined`. |
| 687 | return zcu.undefValue(want_ty); | |
| 691 | return pt.undefValue(want_ty); | |
| 688 | 692 | } |
| 689 | 693 | |
| 690 | 694 | // TODO: we need to decide how to handle partially-undef values here. |
| ... | ... | @@ -702,9 +706,9 @@ const PackValueBits = struct { |
| 702 | 706 | ptr_cast: { |
| 703 | 707 | if (vals.len != 1) break :ptr_cast; |
| 704 | 708 | const val = Value.fromInterned(vals[0]); |
| 705 | if (!val.typeOf(zcu).isPtrAtRuntime(zcu)) break :ptr_cast; | |
| 706 | if (!want_ty.isPtrAtRuntime(zcu)) break :ptr_cast; | |
| 707 | return zcu.getCoerced(val, want_ty); | |
| 709 | if (!val.typeOf(pt.zcu).isPtrAtRuntime(pt.zcu)) break :ptr_cast; | |
| 710 | if (!want_ty.isPtrAtRuntime(pt.zcu)) break :ptr_cast; | |
| 711 | return pt.getCoerced(val, want_ty); | |
| 708 | 712 | } |
| 709 | 713 | |
| 710 | 714 | // Reinterpret via an in-memory buffer. |
| ... | ... | @@ -712,8 +716,8 @@ const PackValueBits = struct { |
| 712 | 716 | var buf_bits: u64 = 0; |
| 713 | 717 | for (vals) |ip_val| { |
| 714 | 718 | const val = Value.fromInterned(ip_val); |
| 715 | const ty = val.typeOf(zcu); | |
| 716 | buf_bits += ty.bitSize(zcu); | |
| 719 | const ty = val.typeOf(pt.zcu); | |
| 720 | buf_bits += ty.bitSize(pt); | |
| 717 | 721 | } |
| 718 | 722 | |
| 719 | 723 | const buf = try pack.arena.alloc(u8, @intCast((buf_bits + 7) / 8)); |
| ... | ... | @@ -722,25 +726,25 @@ const PackValueBits = struct { |
| 722 | 726 | var cur_bit_off: usize = 0; |
| 723 | 727 | for (vals) |ip_val| { |
| 724 | 728 | const val = Value.fromInterned(ip_val); |
| 725 | const ty = val.typeOf(zcu); | |
| 726 | if (!val.isUndef(zcu)) { | |
| 727 | try val.writeToPackedMemory(ty, zcu, buf, cur_bit_off); | |
| 729 | const ty = val.typeOf(pt.zcu); | |
| 730 | if (!val.isUndef(pt.zcu)) { | |
| 731 | try val.writeToPackedMemory(ty, pt, buf, cur_bit_off); | |
| 728 | 732 | } |
| 729 | cur_bit_off += @intCast(ty.bitSize(zcu)); | |
| 733 | cur_bit_off += @intCast(ty.bitSize(pt)); | |
| 730 | 734 | } |
| 731 | 735 | |
| 732 | return Value.readFromPackedMemory(want_ty, zcu, buf, @intCast(bit_offset), pack.arena); | |
| 736 | return Value.readFromPackedMemory(want_ty, pt, buf, @intCast(bit_offset), pack.arena); | |
| 733 | 737 | } |
| 734 | 738 | |
| 735 | 739 | fn prepareBits(pack: *PackValueBits, need_bits: u64) struct { []const InternPool.Index, u64 } { |
| 736 | 740 | if (need_bits == 0) return .{ &.{}, 0 }; |
| 737 | 741 | |
| 738 | const zcu = pack.zcu; | |
| 742 | const pt = pack.pt; | |
| 739 | 743 | |
| 740 | 744 | var bits: u64 = 0; |
| 741 | 745 | var len: usize = 0; |
| 742 | 746 | while (bits < pack.bit_offset + need_bits) { |
| 743 | bits += Value.fromInterned(pack.unpacked[len]).typeOf(zcu).bitSize(zcu); | |
| 747 | bits += Value.fromInterned(pack.unpacked[len]).typeOf(pt.zcu).bitSize(pt); | |
| 744 | 748 | len += 1; |
| 745 | 749 | } |
| 746 | 750 | |
| ... | ... | @@ -753,7 +757,7 @@ const PackValueBits = struct { |
| 753 | 757 | pack.bit_offset = 0; |
| 754 | 758 | } else { |
| 755 | 759 | pack.unpacked = pack.unpacked[len - 1 ..]; |
| 756 | pack.bit_offset = Value.fromInterned(pack.unpacked[0]).typeOf(zcu).bitSize(zcu) - extra_bits; | |
| 760 | pack.bit_offset = Value.fromInterned(pack.unpacked[0]).typeOf(pt.zcu).bitSize(pt) - extra_bits; | |
| 757 | 761 | } |
| 758 | 762 | |
| 759 | 763 | return .{ result_vals, result_offset }; |
src/Sema/comptime_ptr_access.zig+57-54| ... | ... | @@ -12,19 +12,19 @@ pub const ComptimeLoadResult = union(enum) { |
| 12 | 12 | }; |
| 13 | 13 | |
| 14 | 14 | pub fn loadComptimePtr(sema: *Sema, block: *Block, src: LazySrcLoc, ptr: Value) !ComptimeLoadResult { |
| 15 | const zcu = sema.mod; | |
| 16 | const ptr_info = ptr.typeOf(zcu).ptrInfo(zcu); | |
| 15 | const pt = sema.pt; | |
| 16 | const ptr_info = ptr.typeOf(pt.zcu).ptrInfo(pt.zcu); | |
| 17 | 17 | // TODO: host size for vectors is terrible |
| 18 | 18 | const host_bits = switch (ptr_info.flags.vector_index) { |
| 19 | 19 | .none => ptr_info.packed_offset.host_size * 8, |
| 20 | else => ptr_info.packed_offset.host_size * Type.fromInterned(ptr_info.child).bitSize(zcu), | |
| 20 | else => ptr_info.packed_offset.host_size * Type.fromInterned(ptr_info.child).bitSize(pt), | |
| 21 | 21 | }; |
| 22 | 22 | const bit_offset = if (host_bits != 0) bit_offset: { |
| 23 | const child_bits = Type.fromInterned(ptr_info.child).bitSize(zcu); | |
| 23 | const child_bits = Type.fromInterned(ptr_info.child).bitSize(pt); | |
| 24 | 24 | const bit_offset = ptr_info.packed_offset.bit_offset + switch (ptr_info.flags.vector_index) { |
| 25 | 25 | .none => 0, |
| 26 | 26 | .runtime => return .runtime_load, |
| 27 | else => |idx| switch (zcu.getTarget().cpu.arch.endian()) { | |
| 27 | else => |idx| switch (pt.zcu.getTarget().cpu.arch.endian()) { | |
| 28 | 28 | .little => child_bits * @intFromEnum(idx), |
| 29 | 29 | .big => host_bits - child_bits * (@intFromEnum(idx) + 1), // element order reversed on big endian |
| 30 | 30 | }, |
| ... | ... | @@ -60,28 +60,29 @@ pub fn storeComptimePtr( |
| 60 | 60 | ptr: Value, |
| 61 | 61 | store_val: Value, |
| 62 | 62 | ) !ComptimeStoreResult { |
| 63 | const zcu = sema.mod; | |
| 63 | const pt = sema.pt; | |
| 64 | const zcu = pt.zcu; | |
| 64 | 65 | const ptr_info = ptr.typeOf(zcu).ptrInfo(zcu); |
| 65 | 66 | assert(store_val.typeOf(zcu).toIntern() == ptr_info.child); |
| 66 | 67 | // TODO: host size for vectors is terrible |
| 67 | 68 | const host_bits = switch (ptr_info.flags.vector_index) { |
| 68 | 69 | .none => ptr_info.packed_offset.host_size * 8, |
| 69 | else => ptr_info.packed_offset.host_size * Type.fromInterned(ptr_info.child).bitSize(zcu), | |
| 70 | else => ptr_info.packed_offset.host_size * Type.fromInterned(ptr_info.child).bitSize(pt), | |
| 70 | 71 | }; |
| 71 | 72 | const bit_offset = ptr_info.packed_offset.bit_offset + switch (ptr_info.flags.vector_index) { |
| 72 | 73 | .none => 0, |
| 73 | 74 | .runtime => return .runtime_store, |
| 74 | 75 | else => |idx| switch (zcu.getTarget().cpu.arch.endian()) { |
| 75 | .little => Type.fromInterned(ptr_info.child).bitSize(zcu) * @intFromEnum(idx), | |
| 76 | .big => host_bits - Type.fromInterned(ptr_info.child).bitSize(zcu) * (@intFromEnum(idx) + 1), // element order reversed on big endian | |
| 76 | .little => Type.fromInterned(ptr_info.child).bitSize(pt) * @intFromEnum(idx), | |
| 77 | .big => host_bits - Type.fromInterned(ptr_info.child).bitSize(pt) * (@intFromEnum(idx) + 1), // element order reversed on big endian | |
| 77 | 78 | }, |
| 78 | 79 | }; |
| 79 | 80 | const pseudo_store_ty = if (host_bits > 0) t: { |
| 80 | const need_bits = Type.fromInterned(ptr_info.child).bitSize(zcu); | |
| 81 | const need_bits = Type.fromInterned(ptr_info.child).bitSize(pt); | |
| 81 | 82 | if (need_bits + bit_offset > host_bits) { |
| 82 | 83 | return .exceeds_host_size; |
| 83 | 84 | } |
| 84 | break :t try zcu.intType(.unsigned, @intCast(host_bits)); | |
| 85 | break :t try sema.pt.intType(.unsigned, @intCast(host_bits)); | |
| 85 | 86 | } else Type.fromInterned(ptr_info.child); |
| 86 | 87 | |
| 87 | 88 | const strat = try prepareComptimePtrStore(sema, block, src, ptr, pseudo_store_ty, 0); |
| ... | ... | @@ -103,7 +104,7 @@ pub fn storeComptimePtr( |
| 103 | 104 | .needed_well_defined => |ty| return .{ .needed_well_defined = ty }, |
| 104 | 105 | .out_of_bounds => |ty| return .{ .out_of_bounds = ty }, |
| 105 | 106 | }; |
| 106 | const expected = try expected_mv.intern(zcu, sema.arena); | |
| 107 | const expected = try expected_mv.intern(pt, sema.arena); | |
| 107 | 108 | if (store_val.toIntern() != expected.toIntern()) { |
| 108 | 109 | return .{ .comptime_field_mismatch = expected }; |
| 109 | 110 | } |
| ... | ... | @@ -126,14 +127,14 @@ pub fn storeComptimePtr( |
| 126 | 127 | switch (strat) { |
| 127 | 128 | .direct => |direct| { |
| 128 | 129 | const want_ty = direct.val.typeOf(zcu); |
| 129 | const coerced_store_val = try zcu.getCoerced(store_val, want_ty); | |
| 130 | const coerced_store_val = try pt.getCoerced(store_val, want_ty); | |
| 130 | 131 | direct.val.* = .{ .interned = coerced_store_val.toIntern() }; |
| 131 | 132 | return .success; |
| 132 | 133 | }, |
| 133 | 134 | .index => |index| { |
| 134 | 135 | const want_ty = index.val.typeOf(zcu).childType(zcu); |
| 135 | const coerced_store_val = try zcu.getCoerced(store_val, want_ty); | |
| 136 | try index.val.setElem(zcu, sema.arena, @intCast(index.elem_index), .{ .interned = coerced_store_val.toIntern() }); | |
| 136 | const coerced_store_val = try pt.getCoerced(store_val, want_ty); | |
| 137 | try index.val.setElem(pt, sema.arena, @intCast(index.elem_index), .{ .interned = coerced_store_val.toIntern() }); | |
| 137 | 138 | return .success; |
| 138 | 139 | }, |
| 139 | 140 | .flat_index => |flat| { |
| ... | ... | @@ -149,7 +150,7 @@ pub fn storeComptimePtr( |
| 149 | 150 | // Better would be to gather all the store targets into an array. |
| 150 | 151 | var index: u64 = flat.flat_elem_index + idx; |
| 151 | 152 | const val_ptr, const final_idx = (try recursiveIndex(sema, flat.val, &index)).?; |
| 152 | try val_ptr.setElem(zcu, sema.arena, @intCast(final_idx), .{ .interned = elem }); | |
| 153 | try val_ptr.setElem(pt, sema.arena, @intCast(final_idx), .{ .interned = elem }); | |
| 153 | 154 | } |
| 154 | 155 | return .success; |
| 155 | 156 | }, |
| ... | ... | @@ -165,9 +166,9 @@ pub fn storeComptimePtr( |
| 165 | 166 | .direct => |direct| .{ direct.val, 0 }, |
| 166 | 167 | .index => |index| .{ |
| 167 | 168 | index.val, |
| 168 | index.elem_index * index.val.typeOf(zcu).childType(zcu).abiSize(zcu), | |
| 169 | index.elem_index * index.val.typeOf(zcu).childType(zcu).abiSize(pt), | |
| 169 | 170 | }, |
| 170 | .flat_index => |flat| .{ flat.val, flat.flat_elem_index * flat.val.typeOf(zcu).arrayBase(zcu)[0].abiSize(zcu) }, | |
| 171 | .flat_index => |flat| .{ flat.val, flat.flat_elem_index * flat.val.typeOf(zcu).arrayBase(zcu)[0].abiSize(pt) }, | |
| 171 | 172 | .reinterpret => |reinterpret| .{ reinterpret.val, reinterpret.byte_offset }, |
| 172 | 173 | else => unreachable, |
| 173 | 174 | }; |
| ... | ... | @@ -181,7 +182,7 @@ pub fn storeComptimePtr( |
| 181 | 182 | } |
| 182 | 183 | |
| 183 | 184 | const new_val = try sema.bitCastSpliceVal( |
| 184 | try val_ptr.intern(zcu, sema.arena), | |
| 185 | try val_ptr.intern(pt, sema.arena), | |
| 185 | 186 | store_val, |
| 186 | 187 | byte_offset, |
| 187 | 188 | host_bits, |
| ... | ... | @@ -205,7 +206,8 @@ fn loadComptimePtrInner( |
| 205 | 206 | /// before `load_ty`. Otherwise, it is ignored and may be `undefined`. |
| 206 | 207 | array_offset: u64, |
| 207 | 208 | ) !ComptimeLoadResult { |
| 208 | const zcu = sema.mod; | |
| 209 | const pt = sema.pt; | |
| 210 | const zcu = pt.zcu; | |
| 209 | 211 | const ip = &zcu.intern_pool; |
| 210 | 212 | |
| 211 | 213 | const ptr = switch (ip.indexToKey(ptr_val.toIntern())) { |
| ... | ... | @@ -263,7 +265,7 @@ fn loadComptimePtrInner( |
| 263 | 265 | const load_one_ty, const load_count = load_ty.arrayBase(zcu); |
| 264 | 266 | const count = if (load_one_ty.toIntern() == base_ty.toIntern()) load_count else 1; |
| 265 | 267 | |
| 266 | const want_ty = try zcu.arrayType(.{ | |
| 268 | const want_ty = try sema.pt.arrayType(.{ | |
| 267 | 269 | .len = count, |
| 268 | 270 | .child = base_ty.toIntern(), |
| 269 | 271 | }); |
| ... | ... | @@ -285,7 +287,7 @@ fn loadComptimePtrInner( |
| 285 | 287 | |
| 286 | 288 | const agg_ty = agg_val.typeOf(zcu); |
| 287 | 289 | switch (agg_ty.zigTypeTag(zcu)) { |
| 288 | .Struct, .Pointer => break :val try agg_val.getElem(zcu, @intCast(base_index.index)), | |
| 290 | .Struct, .Pointer => break :val try agg_val.getElem(sema.pt, @intCast(base_index.index)), | |
| 289 | 291 | .Union => { |
| 290 | 292 | const tag_val: Value, const payload_mv: MutableValue = switch (agg_val) { |
| 291 | 293 | .un => |un| .{ Value.fromInterned(un.tag), un.payload.* }, |
| ... | ... | @@ -427,7 +429,7 @@ fn loadComptimePtrInner( |
| 427 | 429 | const next_elem_off = elem_size * (elem_idx + 1); |
| 428 | 430 | if (cur_offset + need_bytes <= next_elem_off) { |
| 429 | 431 | // We can look at a single array element. |
| 430 | cur_val = try cur_val.getElem(zcu, @intCast(elem_idx)); | |
| 432 | cur_val = try cur_val.getElem(sema.pt, @intCast(elem_idx)); | |
| 431 | 433 | cur_offset -= elem_idx * elem_size; |
| 432 | 434 | } else { |
| 433 | 435 | break; |
| ... | ... | @@ -437,10 +439,10 @@ fn loadComptimePtrInner( |
| 437 | 439 | .auto => unreachable, // ill-defined layout |
| 438 | 440 | .@"packed" => break, // let the bitcast logic handle this |
| 439 | 441 | .@"extern" => for (0..cur_ty.structFieldCount(zcu)) |field_idx| { |
| 440 | const start_off = cur_ty.structFieldOffset(field_idx, zcu); | |
| 442 | const start_off = cur_ty.structFieldOffset(field_idx, pt); | |
| 441 | 443 | const end_off = start_off + try sema.typeAbiSize(cur_ty.structFieldType(field_idx, zcu)); |
| 442 | 444 | if (cur_offset >= start_off and cur_offset + need_bytes <= end_off) { |
| 443 | cur_val = try cur_val.getElem(zcu, field_idx); | |
| 445 | cur_val = try cur_val.getElem(sema.pt, field_idx); | |
| 444 | 446 | cur_offset -= start_off; |
| 445 | 447 | break; |
| 446 | 448 | } |
| ... | ... | @@ -482,7 +484,7 @@ fn loadComptimePtrInner( |
| 482 | 484 | } |
| 483 | 485 | |
| 484 | 486 | const result_val = try sema.bitCastVal( |
| 485 | try cur_val.intern(zcu, sema.arena), | |
| 487 | try cur_val.intern(sema.pt, sema.arena), | |
| 486 | 488 | load_ty, |
| 487 | 489 | cur_offset, |
| 488 | 490 | host_bits, |
| ... | ... | @@ -564,7 +566,8 @@ fn prepareComptimePtrStore( |
| 564 | 566 | /// before `store_ty`. Otherwise, it is ignored and may be `undefined`. |
| 565 | 567 | array_offset: u64, |
| 566 | 568 | ) !ComptimeStoreStrategy { |
| 567 | const zcu = sema.mod; | |
| 569 | const pt = sema.pt; | |
| 570 | const zcu = pt.zcu; | |
| 568 | 571 | const ip = &zcu.intern_pool; |
| 569 | 572 | |
| 570 | 573 | const ptr = switch (ip.indexToKey(ptr_val.toIntern())) { |
| ... | ... | @@ -587,14 +590,14 @@ fn prepareComptimePtrStore( |
| 587 | 590 | const eu_val_ptr, const alloc = switch (try prepareComptimePtrStore(sema, block, src, base_ptr, base_ty, undefined)) { |
| 588 | 591 | .direct => |direct| .{ direct.val, direct.alloc }, |
| 589 | 592 | .index => |index| .{ |
| 590 | try index.val.elem(zcu, sema.arena, @intCast(index.elem_index)), | |
| 593 | try index.val.elem(pt, sema.arena, @intCast(index.elem_index)), | |
| 591 | 594 | index.alloc, |
| 592 | 595 | }, |
| 593 | 596 | .flat_index => unreachable, // base_ty is not an array |
| 594 | 597 | .reinterpret => unreachable, // base_ty has ill-defined layout |
| 595 | 598 | else => |err| return err, |
| 596 | 599 | }; |
| 597 | try eu_val_ptr.unintern(zcu, sema.arena, false, false); | |
| 600 | try eu_val_ptr.unintern(pt, sema.arena, false, false); | |
| 598 | 601 | switch (eu_val_ptr.*) { |
| 599 | 602 | .interned => |ip_index| switch (ip.indexToKey(ip_index)) { |
| 600 | 603 | .undef => return .undef, |
| ... | ... | @@ -614,14 +617,14 @@ fn prepareComptimePtrStore( |
| 614 | 617 | const opt_val_ptr, const alloc = switch (try prepareComptimePtrStore(sema, block, src, base_ptr, base_ty, undefined)) { |
| 615 | 618 | .direct => |direct| .{ direct.val, direct.alloc }, |
| 616 | 619 | .index => |index| .{ |
| 617 | try index.val.elem(zcu, sema.arena, @intCast(index.elem_index)), | |
| 620 | try index.val.elem(pt, sema.arena, @intCast(index.elem_index)), | |
| 618 | 621 | index.alloc, |
| 619 | 622 | }, |
| 620 | 623 | .flat_index => unreachable, // base_ty is not an array |
| 621 | 624 | .reinterpret => unreachable, // base_ty has ill-defined layout |
| 622 | 625 | else => |err| return err, |
| 623 | 626 | }; |
| 624 | try opt_val_ptr.unintern(zcu, sema.arena, false, false); | |
| 627 | try opt_val_ptr.unintern(pt, sema.arena, false, false); | |
| 625 | 628 | switch (opt_val_ptr.*) { |
| 626 | 629 | .interned => |ip_index| switch (ip.indexToKey(ip_index)) { |
| 627 | 630 | .undef => return .undef, |
| ... | ... | @@ -648,7 +651,7 @@ fn prepareComptimePtrStore( |
| 648 | 651 | const store_one_ty, const store_count = store_ty.arrayBase(zcu); |
| 649 | 652 | const count = if (store_one_ty.toIntern() == base_ty.toIntern()) store_count else 1; |
| 650 | 653 | |
| 651 | const want_ty = try zcu.arrayType(.{ | |
| 654 | const want_ty = try pt.arrayType(.{ | |
| 652 | 655 | .len = count, |
| 653 | 656 | .child = base_ty.toIntern(), |
| 654 | 657 | }); |
| ... | ... | @@ -668,7 +671,7 @@ fn prepareComptimePtrStore( |
| 668 | 671 | const agg_val, const alloc = switch (try prepareComptimePtrStore(sema, block, src, base_ptr, base_ty, undefined)) { |
| 669 | 672 | .direct => |direct| .{ direct.val, direct.alloc }, |
| 670 | 673 | .index => |index| .{ |
| 671 | try index.val.elem(zcu, sema.arena, @intCast(index.elem_index)), | |
| 674 | try index.val.elem(pt, sema.arena, @intCast(index.elem_index)), | |
| 672 | 675 | index.alloc, |
| 673 | 676 | }, |
| 674 | 677 | .flat_index => unreachable, // base_ty is not an array |
| ... | ... | @@ -679,14 +682,14 @@ fn prepareComptimePtrStore( |
| 679 | 682 | const agg_ty = agg_val.typeOf(zcu); |
| 680 | 683 | switch (agg_ty.zigTypeTag(zcu)) { |
| 681 | 684 | .Struct, .Pointer => break :strat .{ .direct = .{ |
| 682 | .val = try agg_val.elem(zcu, sema.arena, @intCast(base_index.index)), | |
| 685 | .val = try agg_val.elem(pt, sema.arena, @intCast(base_index.index)), | |
| 683 | 686 | .alloc = alloc, |
| 684 | 687 | } }, |
| 685 | 688 | .Union => { |
| 686 | 689 | if (agg_val.* == .interned and Value.fromInterned(agg_val.interned).isUndef(zcu)) { |
| 687 | 690 | return .undef; |
| 688 | 691 | } |
| 689 | try agg_val.unintern(zcu, sema.arena, false, false); | |
| 692 | try agg_val.unintern(pt, sema.arena, false, false); | |
| 690 | 693 | const un = agg_val.un; |
| 691 | 694 | const tag_ty = agg_ty.unionTagTypeHypothetical(zcu); |
| 692 | 695 | if (tag_ty.enumTagFieldIndex(Value.fromInterned(un.tag), zcu).? != base_index.index) { |
| ... | ... | @@ -847,7 +850,7 @@ fn prepareComptimePtrStore( |
| 847 | 850 | const next_elem_off = elem_size * (elem_idx + 1); |
| 848 | 851 | if (cur_offset + need_bytes <= next_elem_off) { |
| 849 | 852 | // We can look at a single array element. |
| 850 | cur_val = try cur_val.elem(zcu, sema.arena, @intCast(elem_idx)); | |
| 853 | cur_val = try cur_val.elem(pt, sema.arena, @intCast(elem_idx)); | |
| 851 | 854 | cur_offset -= elem_idx * elem_size; |
| 852 | 855 | } else { |
| 853 | 856 | break; |
| ... | ... | @@ -857,10 +860,10 @@ fn prepareComptimePtrStore( |
| 857 | 860 | .auto => unreachable, // ill-defined layout |
| 858 | 861 | .@"packed" => break, // let the bitcast logic handle this |
| 859 | 862 | .@"extern" => for (0..cur_ty.structFieldCount(zcu)) |field_idx| { |
| 860 | const start_off = cur_ty.structFieldOffset(field_idx, zcu); | |
| 863 | const start_off = cur_ty.structFieldOffset(field_idx, pt); | |
| 861 | 864 | const end_off = start_off + try sema.typeAbiSize(cur_ty.structFieldType(field_idx, zcu)); |
| 862 | 865 | if (cur_offset >= start_off and cur_offset + need_bytes <= end_off) { |
| 863 | cur_val = try cur_val.elem(zcu, sema.arena, field_idx); | |
| 866 | cur_val = try cur_val.elem(pt, sema.arena, field_idx); | |
| 864 | 867 | cur_offset -= start_off; |
| 865 | 868 | break; |
| 866 | 869 | } |
| ... | ... | @@ -874,7 +877,7 @@ fn prepareComptimePtrStore( |
| 874 | 877 | // Otherwise, we might traverse into a union field which doesn't allow pointers. |
| 875 | 878 | // Figure out a solution! |
| 876 | 879 | if (true) break; |
| 877 | try cur_val.unintern(zcu, sema.arena, false, false); | |
| 880 | try cur_val.unintern(pt, sema.arena, false, false); | |
| 878 | 881 | const payload = switch (cur_val.*) { |
| 879 | 882 | .un => |un| un.payload, |
| 880 | 883 | else => unreachable, |
| ... | ... | @@ -918,7 +921,7 @@ fn flattenArray( |
| 918 | 921 | ) Allocator.Error!void { |
| 919 | 922 | if (next_idx.* == out.len) return; |
| 920 | 923 | |
| 921 | const zcu = sema.mod; | |
| 924 | const zcu = sema.pt.zcu; | |
| 922 | 925 | |
| 923 | 926 | const ty = val.typeOf(zcu); |
| 924 | 927 | const base_elem_count = ty.arrayBase(zcu)[1]; |
| ... | ... | @@ -928,7 +931,7 @@ fn flattenArray( |
| 928 | 931 | } |
| 929 | 932 | |
| 930 | 933 | if (ty.zigTypeTag(zcu) != .Array) { |
| 931 | out[@intCast(next_idx.*)] = (try val.intern(zcu, sema.arena)).toIntern(); | |
| 934 | out[@intCast(next_idx.*)] = (try val.intern(sema.pt, sema.arena)).toIntern(); | |
| 932 | 935 | next_idx.* += 1; |
| 933 | 936 | return; |
| 934 | 937 | } |
| ... | ... | @@ -942,7 +945,7 @@ fn flattenArray( |
| 942 | 945 | skip.* -= arr_base_elem_count; |
| 943 | 946 | continue; |
| 944 | 947 | } |
| 945 | try flattenArray(sema, try val.getElem(zcu, elem_idx), skip, next_idx, out); | |
| 948 | try flattenArray(sema, try val.getElem(sema.pt, elem_idx), skip, next_idx, out); | |
| 946 | 949 | } |
| 947 | 950 | if (ty.sentinel(zcu)) |s| { |
| 948 | 951 | try flattenArray(sema, .{ .interned = s.toIntern() }, skip, next_idx, out); |
| ... | ... | @@ -957,13 +960,13 @@ fn unflattenArray( |
| 957 | 960 | elems: []const InternPool.Index, |
| 958 | 961 | next_idx: *u64, |
| 959 | 962 | ) Allocator.Error!Value { |
| 960 | const zcu = sema.mod; | |
| 963 | const zcu = sema.pt.zcu; | |
| 961 | 964 | const arena = sema.arena; |
| 962 | 965 | |
| 963 | 966 | if (ty.zigTypeTag(zcu) != .Array) { |
| 964 | 967 | const val = Value.fromInterned(elems[@intCast(next_idx.*)]); |
| 965 | 968 | next_idx.* += 1; |
| 966 | return zcu.getCoerced(val, ty); | |
| 969 | return sema.pt.getCoerced(val, ty); | |
| 967 | 970 | } |
| 968 | 971 | |
| 969 | 972 | const elem_ty = ty.childType(zcu); |
| ... | ... | @@ -975,7 +978,7 @@ fn unflattenArray( |
| 975 | 978 | // TODO: validate sentinel |
| 976 | 979 | _ = try unflattenArray(sema, elem_ty, elems, next_idx); |
| 977 | 980 | } |
| 978 | return Value.fromInterned(try zcu.intern(.{ .aggregate = .{ | |
| 981 | return Value.fromInterned(try sema.pt.intern(.{ .aggregate = .{ | |
| 979 | 982 | .ty = ty.toIntern(), |
| 980 | 983 | .storage = .{ .elems = buf }, |
| 981 | 984 | } })); |
| ... | ... | @@ -990,25 +993,25 @@ fn recursiveIndex( |
| 990 | 993 | mv: *MutableValue, |
| 991 | 994 | index: *u64, |
| 992 | 995 | ) !?struct { *MutableValue, u64 } { |
| 993 | const zcu = sema.mod; | |
| 996 | const pt = sema.pt; | |
| 994 | 997 | |
| 995 | const ty = mv.typeOf(zcu); | |
| 996 | assert(ty.zigTypeTag(zcu) == .Array); | |
| 998 | const ty = mv.typeOf(pt.zcu); | |
| 999 | assert(ty.zigTypeTag(pt.zcu) == .Array); | |
| 997 | 1000 | |
| 998 | const ty_base_elems = ty.arrayBase(zcu)[1]; | |
| 1001 | const ty_base_elems = ty.arrayBase(pt.zcu)[1]; | |
| 999 | 1002 | if (index.* >= ty_base_elems) { |
| 1000 | 1003 | index.* -= ty_base_elems; |
| 1001 | 1004 | return null; |
| 1002 | 1005 | } |
| 1003 | 1006 | |
| 1004 | const elem_ty = ty.childType(zcu); | |
| 1005 | if (elem_ty.zigTypeTag(zcu) != .Array) { | |
| 1006 | assert(index.* < ty.arrayLenIncludingSentinel(zcu)); // should be handled by initial check | |
| 1007 | const elem_ty = ty.childType(pt.zcu); | |
| 1008 | if (elem_ty.zigTypeTag(pt.zcu) != .Array) { | |
| 1009 | assert(index.* < ty.arrayLenIncludingSentinel(pt.zcu)); // should be handled by initial check | |
| 1007 | 1010 | return .{ mv, index.* }; |
| 1008 | 1011 | } |
| 1009 | 1012 | |
| 1010 | for (0..@intCast(ty.arrayLenIncludingSentinel(zcu))) |elem_index| { | |
| 1011 | if (try recursiveIndex(sema, try mv.elem(zcu, sema.arena, elem_index), index)) |result| { | |
| 1013 | for (0..@intCast(ty.arrayLenIncludingSentinel(pt.zcu))) |elem_index| { | |
| 1014 | if (try recursiveIndex(sema, try mv.elem(pt, sema.arena, elem_index), index)) |result| { | |
| 1012 | 1015 | return result; |
| 1013 | 1016 | } |
| 1014 | 1017 | } |
src/Type.zig+376-361| ... | ... | @@ -136,16 +136,16 @@ pub fn format(ty: Type, comptime unused_fmt_string: []const u8, options: std.fmt |
| 136 | 136 | |
| 137 | 137 | pub const Formatter = std.fmt.Formatter(format2); |
| 138 | 138 | |
| 139 | pub fn fmt(ty: Type, module: *Module) Formatter { | |
| 139 | pub fn fmt(ty: Type, pt: Zcu.PerThread) Formatter { | |
| 140 | 140 | return .{ .data = .{ |
| 141 | 141 | .ty = ty, |
| 142 | .module = module, | |
| 142 | .pt = pt, | |
| 143 | 143 | } }; |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | const FormatContext = struct { |
| 147 | 147 | ty: Type, |
| 148 | module: *Module, | |
| 148 | pt: Zcu.PerThread, | |
| 149 | 149 | }; |
| 150 | 150 | |
| 151 | 151 | fn format2( |
| ... | ... | @@ -156,7 +156,7 @@ fn format2( |
| 156 | 156 | ) !void { |
| 157 | 157 | comptime assert(unused_format_string.len == 0); |
| 158 | 158 | _ = options; |
| 159 | return print(ctx.ty, writer, ctx.module); | |
| 159 | return print(ctx.ty, writer, ctx.pt); | |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | 162 | pub fn fmtDebug(ty: Type) std.fmt.Formatter(dump) { |
| ... | ... | @@ -178,7 +178,8 @@ pub fn dump( |
| 178 | 178 | |
| 179 | 179 | /// Prints a name suitable for `@typeName`. |
| 180 | 180 | /// TODO: take an `opt_sema` to pass to `fmtValue` when printing sentinels. |
| 181 | pub fn print(ty: Type, writer: anytype, mod: *Module) @TypeOf(writer).Error!void { | |
| 181 | pub fn print(ty: Type, writer: anytype, pt: Zcu.PerThread) @TypeOf(writer).Error!void { | |
| 182 | const mod = pt.zcu; | |
| 182 | 183 | const ip = &mod.intern_pool; |
| 183 | 184 | switch (ip.indexToKey(ty.toIntern())) { |
| 184 | 185 | .int_type => |int_type| { |
| ... | ... | @@ -193,8 +194,8 @@ pub fn print(ty: Type, writer: anytype, mod: *Module) @TypeOf(writer).Error!void |
| 193 | 194 | |
| 194 | 195 | if (info.sentinel != .none) switch (info.flags.size) { |
| 195 | 196 | .One, .C => unreachable, |
| 196 | .Many => try writer.print("[*:{}]", .{Value.fromInterned(info.sentinel).fmtValue(mod, null)}), | |
| 197 | .Slice => try writer.print("[:{}]", .{Value.fromInterned(info.sentinel).fmtValue(mod, null)}), | |
| 197 | .Many => try writer.print("[*:{}]", .{Value.fromInterned(info.sentinel).fmtValue(pt, null)}), | |
| 198 | .Slice => try writer.print("[:{}]", .{Value.fromInterned(info.sentinel).fmtValue(pt, null)}), | |
| 198 | 199 | } else switch (info.flags.size) { |
| 199 | 200 | .One => try writer.writeAll("*"), |
| 200 | 201 | .Many => try writer.writeAll("[*]"), |
| ... | ... | @@ -208,7 +209,7 @@ pub fn print(ty: Type, writer: anytype, mod: *Module) @TypeOf(writer).Error!void |
| 208 | 209 | const alignment = if (info.flags.alignment != .none) |
| 209 | 210 | info.flags.alignment |
| 210 | 211 | else |
| 211 | Type.fromInterned(info.child).abiAlignment(mod); | |
| 212 | Type.fromInterned(info.child).abiAlignment(pt); | |
| 212 | 213 | try writer.print("align({d}", .{alignment.toByteUnits() orelse 0}); |
| 213 | 214 | |
| 214 | 215 | if (info.packed_offset.bit_offset != 0 or info.packed_offset.host_size != 0) { |
| ... | ... | @@ -230,39 +231,39 @@ pub fn print(ty: Type, writer: anytype, mod: *Module) @TypeOf(writer).Error!void |
| 230 | 231 | if (info.flags.is_volatile) try writer.writeAll("volatile "); |
| 231 | 232 | if (info.flags.is_allowzero and info.flags.size != .C) try writer.writeAll("allowzero "); |
| 232 | 233 | |
| 233 | try print(Type.fromInterned(info.child), writer, mod); | |
| 234 | try print(Type.fromInterned(info.child), writer, pt); | |
| 234 | 235 | return; |
| 235 | 236 | }, |
| 236 | 237 | .array_type => |array_type| { |
| 237 | 238 | if (array_type.sentinel == .none) { |
| 238 | 239 | try writer.print("[{d}]", .{array_type.len}); |
| 239 | try print(Type.fromInterned(array_type.child), writer, mod); | |
| 240 | try print(Type.fromInterned(array_type.child), writer, pt); | |
| 240 | 241 | } else { |
| 241 | 242 | try writer.print("[{d}:{}]", .{ |
| 242 | 243 | array_type.len, |
| 243 | Value.fromInterned(array_type.sentinel).fmtValue(mod, null), | |
| 244 | Value.fromInterned(array_type.sentinel).fmtValue(pt, null), | |
| 244 | 245 | }); |
| 245 | try print(Type.fromInterned(array_type.child), writer, mod); | |
| 246 | try print(Type.fromInterned(array_type.child), writer, pt); | |
| 246 | 247 | } |
| 247 | 248 | return; |
| 248 | 249 | }, |
| 249 | 250 | .vector_type => |vector_type| { |
| 250 | 251 | try writer.print("@Vector({d}, ", .{vector_type.len}); |
| 251 | try print(Type.fromInterned(vector_type.child), writer, mod); | |
| 252 | try print(Type.fromInterned(vector_type.child), writer, pt); | |
| 252 | 253 | try writer.writeAll(")"); |
| 253 | 254 | return; |
| 254 | 255 | }, |
| 255 | 256 | .opt_type => |child| { |
| 256 | 257 | try writer.writeByte('?'); |
| 257 | return print(Type.fromInterned(child), writer, mod); | |
| 258 | return print(Type.fromInterned(child), writer, pt); | |
| 258 | 259 | }, |
| 259 | 260 | .error_union_type => |error_union_type| { |
| 260 | try print(Type.fromInterned(error_union_type.error_set_type), writer, mod); | |
| 261 | try print(Type.fromInterned(error_union_type.error_set_type), writer, pt); | |
| 261 | 262 | try writer.writeByte('!'); |
| 262 | 263 | if (error_union_type.payload_type == .generic_poison_type) { |
| 263 | 264 | try writer.writeAll("anytype"); |
| 264 | 265 | } else { |
| 265 | try print(Type.fromInterned(error_union_type.payload_type), writer, mod); | |
| 266 | try print(Type.fromInterned(error_union_type.payload_type), writer, pt); | |
| 266 | 267 | } |
| 267 | 268 | return; |
| 268 | 269 | }, |
| ... | ... | @@ -355,10 +356,10 @@ pub fn print(ty: Type, writer: anytype, mod: *Module) @TypeOf(writer).Error!void |
| 355 | 356 | try writer.print("{}: ", .{anon_struct.names.get(ip)[i].fmt(&mod.intern_pool)}); |
| 356 | 357 | } |
| 357 | 358 | |
| 358 | try print(Type.fromInterned(field_ty), writer, mod); | |
| 359 | try print(Type.fromInterned(field_ty), writer, pt); | |
| 359 | 360 | |
| 360 | 361 | if (val != .none) { |
| 361 | try writer.print(" = {}", .{Value.fromInterned(val).fmtValue(mod, null)}); | |
| 362 | try writer.print(" = {}", .{Value.fromInterned(val).fmtValue(pt, null)}); | |
| 362 | 363 | } |
| 363 | 364 | } |
| 364 | 365 | try writer.writeAll("}"); |
| ... | ... | @@ -395,7 +396,7 @@ pub fn print(ty: Type, writer: anytype, mod: *Module) @TypeOf(writer).Error!void |
| 395 | 396 | if (param_ty == .generic_poison_type) { |
| 396 | 397 | try writer.writeAll("anytype"); |
| 397 | 398 | } else { |
| 398 | try print(Type.fromInterned(param_ty), writer, mod); | |
| 399 | try print(Type.fromInterned(param_ty), writer, pt); | |
| 399 | 400 | } |
| 400 | 401 | } |
| 401 | 402 | if (fn_info.is_var_args) { |
| ... | ... | @@ -413,13 +414,13 @@ pub fn print(ty: Type, writer: anytype, mod: *Module) @TypeOf(writer).Error!void |
| 413 | 414 | if (fn_info.return_type == .generic_poison_type) { |
| 414 | 415 | try writer.writeAll("anytype"); |
| 415 | 416 | } else { |
| 416 | try print(Type.fromInterned(fn_info.return_type), writer, mod); | |
| 417 | try print(Type.fromInterned(fn_info.return_type), writer, pt); | |
| 417 | 418 | } |
| 418 | 419 | }, |
| 419 | 420 | .anyframe_type => |child| { |
| 420 | 421 | if (child == .none) return writer.writeAll("anyframe"); |
| 421 | 422 | try writer.writeAll("anyframe->"); |
| 422 | return print(Type.fromInterned(child), writer, mod); | |
| 423 | return print(Type.fromInterned(child), writer, pt); | |
| 423 | 424 | }, |
| 424 | 425 | |
| 425 | 426 | // values, not types |
| ... | ... | @@ -475,10 +476,11 @@ const RuntimeBitsError = SemaError || error{NeedLazy}; |
| 475 | 476 | /// may return false positives. |
| 476 | 477 | pub fn hasRuntimeBitsAdvanced( |
| 477 | 478 | ty: Type, |
| 478 | mod: *Module, | |
| 479 | pt: Zcu.PerThread, | |
| 479 | 480 | ignore_comptime_only: bool, |
| 480 | 481 | strat: ResolveStratLazy, |
| 481 | 482 | ) RuntimeBitsError!bool { |
| 483 | const mod = pt.zcu; | |
| 482 | 484 | const ip = &mod.intern_pool; |
| 483 | 485 | return switch (ty.toIntern()) { |
| 484 | 486 | // False because it is a comptime-only type. |
| ... | ... | @@ -490,16 +492,16 @@ pub fn hasRuntimeBitsAdvanced( |
| 490 | 492 | // to comptime-only types do not, with the exception of function pointers. |
| 491 | 493 | if (ignore_comptime_only) return true; |
| 492 | 494 | return switch (strat) { |
| 493 | .sema => !try ty.comptimeOnlyAdvanced(mod, .sema), | |
| 494 | .eager => !ty.comptimeOnly(mod), | |
| 495 | .sema => !try ty.comptimeOnlyAdvanced(pt, .sema), | |
| 496 | .eager => !ty.comptimeOnly(pt), | |
| 495 | 497 | .lazy => error.NeedLazy, |
| 496 | 498 | }; |
| 497 | 499 | }, |
| 498 | 500 | .anyframe_type => true, |
| 499 | 501 | .array_type => |array_type| return array_type.lenIncludingSentinel() > 0 and |
| 500 | try Type.fromInterned(array_type.child).hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat), | |
| 502 | try Type.fromInterned(array_type.child).hasRuntimeBitsAdvanced(pt, ignore_comptime_only, strat), | |
| 501 | 503 | .vector_type => |vector_type| return vector_type.len > 0 and |
| 502 | try Type.fromInterned(vector_type.child).hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat), | |
| 504 | try Type.fromInterned(vector_type.child).hasRuntimeBitsAdvanced(pt, ignore_comptime_only, strat), | |
| 503 | 505 | .opt_type => |child| { |
| 504 | 506 | const child_ty = Type.fromInterned(child); |
| 505 | 507 | if (child_ty.isNoReturn(mod)) { |
| ... | ... | @@ -508,8 +510,8 @@ pub fn hasRuntimeBitsAdvanced( |
| 508 | 510 | } |
| 509 | 511 | if (ignore_comptime_only) return true; |
| 510 | 512 | return switch (strat) { |
| 511 | .sema => !try child_ty.comptimeOnlyAdvanced(mod, .sema), | |
| 512 | .eager => !child_ty.comptimeOnly(mod), | |
| 513 | .sema => !try child_ty.comptimeOnlyAdvanced(pt, .sema), | |
| 514 | .eager => !child_ty.comptimeOnly(pt), | |
| 513 | 515 | .lazy => error.NeedLazy, |
| 514 | 516 | }; |
| 515 | 517 | }, |
| ... | ... | @@ -580,14 +582,14 @@ pub fn hasRuntimeBitsAdvanced( |
| 580 | 582 | return true; |
| 581 | 583 | } |
| 582 | 584 | switch (strat) { |
| 583 | .sema => try ty.resolveFields(mod), | |
| 585 | .sema => try ty.resolveFields(pt), | |
| 584 | 586 | .eager => assert(struct_type.haveFieldTypes(ip)), |
| 585 | 587 | .lazy => if (!struct_type.haveFieldTypes(ip)) return error.NeedLazy, |
| 586 | 588 | } |
| 587 | 589 | for (0..struct_type.field_types.len) |i| { |
| 588 | 590 | if (struct_type.comptime_bits.getBit(ip, i)) continue; |
| 589 | 591 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[i]); |
| 590 | if (try field_ty.hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat)) | |
| 592 | if (try field_ty.hasRuntimeBitsAdvanced(pt, ignore_comptime_only, strat)) | |
| 591 | 593 | return true; |
| 592 | 594 | } else { |
| 593 | 595 | return false; |
| ... | ... | @@ -596,7 +598,7 @@ pub fn hasRuntimeBitsAdvanced( |
| 596 | 598 | .anon_struct_type => |tuple| { |
| 597 | 599 | for (tuple.types.get(ip), tuple.values.get(ip)) |field_ty, val| { |
| 598 | 600 | if (val != .none) continue; // comptime field |
| 599 | if (try Type.fromInterned(field_ty).hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat)) return true; | |
| 601 | if (try Type.fromInterned(field_ty).hasRuntimeBitsAdvanced(pt, ignore_comptime_only, strat)) return true; | |
| 600 | 602 | } |
| 601 | 603 | return false; |
| 602 | 604 | }, |
| ... | ... | @@ -617,21 +619,21 @@ pub fn hasRuntimeBitsAdvanced( |
| 617 | 619 | // tag_ty will be `none` if this union's tag type is not resolved yet, |
| 618 | 620 | // in which case we want control flow to continue down below. |
| 619 | 621 | if (tag_ty != .none and |
| 620 | try Type.fromInterned(tag_ty).hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat)) | |
| 622 | try Type.fromInterned(tag_ty).hasRuntimeBitsAdvanced(pt, ignore_comptime_only, strat)) | |
| 621 | 623 | { |
| 622 | 624 | return true; |
| 623 | 625 | } |
| 624 | 626 | }, |
| 625 | 627 | } |
| 626 | 628 | switch (strat) { |
| 627 | .sema => try ty.resolveFields(mod), | |
| 629 | .sema => try ty.resolveFields(pt), | |
| 628 | 630 | .eager => assert(union_type.flagsPtr(ip).status.haveFieldTypes()), |
| 629 | 631 | .lazy => if (!union_type.flagsPtr(ip).status.haveFieldTypes()) |
| 630 | 632 | return error.NeedLazy, |
| 631 | 633 | } |
| 632 | 634 | for (0..union_type.field_types.len) |field_index| { |
| 633 | 635 | const field_ty = Type.fromInterned(union_type.field_types.get(ip)[field_index]); |
| 634 | if (try field_ty.hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat)) | |
| 636 | if (try field_ty.hasRuntimeBitsAdvanced(pt, ignore_comptime_only, strat)) | |
| 635 | 637 | return true; |
| 636 | 638 | } else { |
| 637 | 639 | return false; |
| ... | ... | @@ -639,7 +641,7 @@ pub fn hasRuntimeBitsAdvanced( |
| 639 | 641 | }, |
| 640 | 642 | |
| 641 | 643 | .opaque_type => true, |
| 642 | .enum_type => Type.fromInterned(ip.loadEnumType(ty.toIntern()).tag_ty).hasRuntimeBitsAdvanced(mod, ignore_comptime_only, strat), | |
| 644 | .enum_type => Type.fromInterned(ip.loadEnumType(ty.toIntern()).tag_ty).hasRuntimeBitsAdvanced(pt, ignore_comptime_only, strat), | |
| 643 | 645 | |
| 644 | 646 | // values, not types |
| 645 | 647 | .undef, |
| ... | ... | @@ -777,41 +779,41 @@ pub fn hasWellDefinedLayout(ty: Type, mod: *Module) bool { |
| 777 | 779 | }; |
| 778 | 780 | } |
| 779 | 781 | |
| 780 | pub fn hasRuntimeBits(ty: Type, mod: *Module) bool { | |
| 781 | return hasRuntimeBitsAdvanced(ty, mod, false, .eager) catch unreachable; | |
| 782 | pub fn hasRuntimeBits(ty: Type, pt: Zcu.PerThread) bool { | |
| 783 | return hasRuntimeBitsAdvanced(ty, pt, false, .eager) catch unreachable; | |
| 782 | 784 | } |
| 783 | 785 | |
| 784 | pub fn hasRuntimeBitsIgnoreComptime(ty: Type, mod: *Module) bool { | |
| 785 | return hasRuntimeBitsAdvanced(ty, mod, true, .eager) catch unreachable; | |
| 786 | pub fn hasRuntimeBitsIgnoreComptime(ty: Type, pt: Zcu.PerThread) bool { | |
| 787 | return hasRuntimeBitsAdvanced(ty, pt, true, .eager) catch unreachable; | |
| 786 | 788 | } |
| 787 | 789 | |
| 788 | pub fn fnHasRuntimeBits(ty: Type, mod: *Module) bool { | |
| 789 | return ty.fnHasRuntimeBitsAdvanced(mod, .normal) catch unreachable; | |
| 790 | pub fn fnHasRuntimeBits(ty: Type, pt: Zcu.PerThread) bool { | |
| 791 | return ty.fnHasRuntimeBitsAdvanced(pt, .normal) catch unreachable; | |
| 790 | 792 | } |
| 791 | 793 | |
| 792 | 794 | /// Determines whether a function type has runtime bits, i.e. whether a |
| 793 | 795 | /// function with this type can exist at runtime. |
| 794 | 796 | /// Asserts that `ty` is a function type. |
| 795 | pub fn fnHasRuntimeBitsAdvanced(ty: Type, mod: *Module, strat: ResolveStrat) SemaError!bool { | |
| 796 | const fn_info = mod.typeToFunc(ty).?; | |
| 797 | pub fn fnHasRuntimeBitsAdvanced(ty: Type, pt: Zcu.PerThread, strat: ResolveStrat) SemaError!bool { | |
| 798 | const fn_info = pt.zcu.typeToFunc(ty).?; | |
| 797 | 799 | if (fn_info.is_generic) return false; |
| 798 | 800 | if (fn_info.is_var_args) return true; |
| 799 | 801 | if (fn_info.cc == .Inline) return false; |
| 800 | return !try Type.fromInterned(fn_info.return_type).comptimeOnlyAdvanced(mod, strat); | |
| 802 | return !try Type.fromInterned(fn_info.return_type).comptimeOnlyAdvanced(pt, strat); | |
| 801 | 803 | } |
| 802 | 804 | |
| 803 | pub fn isFnOrHasRuntimeBits(ty: Type, mod: *Module) bool { | |
| 804 | switch (ty.zigTypeTag(mod)) { | |
| 805 | .Fn => return ty.fnHasRuntimeBits(mod), | |
| 806 | else => return ty.hasRuntimeBits(mod), | |
| 805 | pub fn isFnOrHasRuntimeBits(ty: Type, pt: Zcu.PerThread) bool { | |
| 806 | switch (ty.zigTypeTag(pt.zcu)) { | |
| 807 | .Fn => return ty.fnHasRuntimeBits(pt), | |
| 808 | else => return ty.hasRuntimeBits(pt), | |
| 807 | 809 | } |
| 808 | 810 | } |
| 809 | 811 | |
| 810 | 812 | /// Same as `isFnOrHasRuntimeBits` but comptime-only types may return a false positive. |
| 811 | pub fn isFnOrHasRuntimeBitsIgnoreComptime(ty: Type, mod: *Module) bool { | |
| 812 | return switch (ty.zigTypeTag(mod)) { | |
| 813 | pub fn isFnOrHasRuntimeBitsIgnoreComptime(ty: Type, pt: Zcu.PerThread) bool { | |
| 814 | return switch (ty.zigTypeTag(pt.zcu)) { | |
| 813 | 815 | .Fn => true, |
| 814 | else => return ty.hasRuntimeBitsIgnoreComptime(mod), | |
| 816 | else => return ty.hasRuntimeBitsIgnoreComptime(pt), | |
| 815 | 817 | }; |
| 816 | 818 | } |
| 817 | 819 | |
| ... | ... | @@ -820,24 +822,24 @@ pub fn isNoReturn(ty: Type, mod: *Module) bool { |
| 820 | 822 | } |
| 821 | 823 | |
| 822 | 824 | /// Returns `none` if the pointer is naturally aligned and the element type is 0-bit. |
| 823 | pub fn ptrAlignment(ty: Type, mod: *Module) Alignment { | |
| 824 | return ptrAlignmentAdvanced(ty, mod, .normal) catch unreachable; | |
| 825 | pub fn ptrAlignment(ty: Type, pt: Zcu.PerThread) Alignment { | |
| 826 | return ptrAlignmentAdvanced(ty, pt, .normal) catch unreachable; | |
| 825 | 827 | } |
| 826 | 828 | |
| 827 | pub fn ptrAlignmentAdvanced(ty: Type, mod: *Module, strat: ResolveStrat) !Alignment { | |
| 828 | return switch (mod.intern_pool.indexToKey(ty.toIntern())) { | |
| 829 | pub fn ptrAlignmentAdvanced(ty: Type, pt: Zcu.PerThread, strat: ResolveStrat) !Alignment { | |
| 830 | return switch (pt.zcu.intern_pool.indexToKey(ty.toIntern())) { | |
| 829 | 831 | .ptr_type => |ptr_type| { |
| 830 | 832 | if (ptr_type.flags.alignment != .none) |
| 831 | 833 | return ptr_type.flags.alignment; |
| 832 | 834 | |
| 833 | 835 | if (strat == .sema) { |
| 834 | const res = try Type.fromInterned(ptr_type.child).abiAlignmentAdvanced(mod, .sema); | |
| 836 | const res = try Type.fromInterned(ptr_type.child).abiAlignmentAdvanced(pt, .sema); | |
| 835 | 837 | return res.scalar; |
| 836 | 838 | } |
| 837 | 839 | |
| 838 | return (Type.fromInterned(ptr_type.child).abiAlignmentAdvanced(mod, .eager) catch unreachable).scalar; | |
| 840 | return (Type.fromInterned(ptr_type.child).abiAlignmentAdvanced(pt, .eager) catch unreachable).scalar; | |
| 839 | 841 | }, |
| 840 | .opt_type => |child| Type.fromInterned(child).ptrAlignmentAdvanced(mod, strat), | |
| 842 | .opt_type => |child| Type.fromInterned(child).ptrAlignmentAdvanced(pt, strat), | |
| 841 | 843 | else => unreachable, |
| 842 | 844 | }; |
| 843 | 845 | } |
| ... | ... | @@ -851,16 +853,16 @@ pub fn ptrAddressSpace(ty: Type, mod: *const Module) std.builtin.AddressSpace { |
| 851 | 853 | } |
| 852 | 854 | |
| 853 | 855 | /// Never returns `none`. Asserts that all necessary type resolution is already done. |
| 854 | pub fn abiAlignment(ty: Type, mod: *Module) Alignment { | |
| 855 | return (ty.abiAlignmentAdvanced(mod, .eager) catch unreachable).scalar; | |
| 856 | pub fn abiAlignment(ty: Type, pt: Zcu.PerThread) Alignment { | |
| 857 | return (ty.abiAlignmentAdvanced(pt, .eager) catch unreachable).scalar; | |
| 856 | 858 | } |
| 857 | 859 | |
| 858 | 860 | /// May capture a reference to `ty`. |
| 859 | 861 | /// Returned value has type `comptime_int`. |
| 860 | pub fn lazyAbiAlignment(ty: Type, mod: *Module) !Value { | |
| 861 | switch (try ty.abiAlignmentAdvanced(mod, .lazy)) { | |
| 862 | pub fn lazyAbiAlignment(ty: Type, pt: Zcu.PerThread) !Value { | |
| 863 | switch (try ty.abiAlignmentAdvanced(pt, .lazy)) { | |
| 862 | 864 | .val => |val| return val, |
| 863 | .scalar => |x| return mod.intValue(Type.comptime_int, x.toByteUnits() orelse 0), | |
| 865 | .scalar => |x| return pt.intValue(Type.comptime_int, x.toByteUnits() orelse 0), | |
| 864 | 866 | } |
| 865 | 867 | } |
| 866 | 868 | |
| ... | ... | @@ -907,38 +909,39 @@ pub const ResolveStrat = enum { |
| 907 | 909 | /// necessary, possibly returning a CompileError. |
| 908 | 910 | pub fn abiAlignmentAdvanced( |
| 909 | 911 | ty: Type, |
| 910 | mod: *Module, | |
| 912 | pt: Zcu.PerThread, | |
| 911 | 913 | strat: ResolveStratLazy, |
| 912 | 914 | ) SemaError!AbiAlignmentAdvanced { |
| 915 | const mod = pt.zcu; | |
| 913 | 916 | const target = mod.getTarget(); |
| 914 | 917 | const use_llvm = mod.comp.config.use_llvm; |
| 915 | 918 | const ip = &mod.intern_pool; |
| 916 | 919 | |
| 917 | 920 | switch (ty.toIntern()) { |
| 918 | .empty_struct_type => return AbiAlignmentAdvanced{ .scalar = .@"1" }, | |
| 921 | .empty_struct_type => return .{ .scalar = .@"1" }, | |
| 919 | 922 | else => switch (ip.indexToKey(ty.toIntern())) { |
| 920 | 923 | .int_type => |int_type| { |
| 921 | if (int_type.bits == 0) return AbiAlignmentAdvanced{ .scalar = .@"1" }; | |
| 924 | if (int_type.bits == 0) return .{ .scalar = .@"1" }; | |
| 922 | 925 | return .{ .scalar = intAbiAlignment(int_type.bits, target, use_llvm) }; |
| 923 | 926 | }, |
| 924 | 927 | .ptr_type, .anyframe_type => { |
| 925 | 928 | return .{ .scalar = ptrAbiAlignment(target) }; |
| 926 | 929 | }, |
| 927 | 930 | .array_type => |array_type| { |
| 928 | return Type.fromInterned(array_type.child).abiAlignmentAdvanced(mod, strat); | |
| 931 | return Type.fromInterned(array_type.child).abiAlignmentAdvanced(pt, strat); | |
| 929 | 932 | }, |
| 930 | 933 | .vector_type => |vector_type| { |
| 931 | 934 | if (vector_type.len == 0) return .{ .scalar = .@"1" }; |
| 932 | 935 | switch (mod.comp.getZigBackend()) { |
| 933 | 936 | else => { |
| 934 | const elem_bits: u32 = @intCast(try Type.fromInterned(vector_type.child).bitSizeAdvanced(mod, .sema)); | |
| 937 | const elem_bits: u32 = @intCast(try Type.fromInterned(vector_type.child).bitSizeAdvanced(pt, .sema)); | |
| 935 | 938 | if (elem_bits == 0) return .{ .scalar = .@"1" }; |
| 936 | 939 | const bytes = ((elem_bits * vector_type.len) + 7) / 8; |
| 937 | 940 | const alignment = std.math.ceilPowerOfTwoAssert(u32, bytes); |
| 938 | 941 | return .{ .scalar = Alignment.fromByteUnits(alignment) }; |
| 939 | 942 | }, |
| 940 | 943 | .stage2_c => { |
| 941 | return Type.fromInterned(vector_type.child).abiAlignmentAdvanced(mod, strat); | |
| 944 | return Type.fromInterned(vector_type.child).abiAlignmentAdvanced(pt, strat); | |
| 942 | 945 | }, |
| 943 | 946 | .stage2_x86_64 => { |
| 944 | 947 | if (vector_type.child == .bool_type) { |
| ... | ... | @@ -949,7 +952,7 @@ pub fn abiAlignmentAdvanced( |
| 949 | 952 | const alignment = std.math.ceilPowerOfTwoAssert(u32, bytes); |
| 950 | 953 | return .{ .scalar = Alignment.fromByteUnits(alignment) }; |
| 951 | 954 | } |
| 952 | const elem_bytes: u32 = @intCast((try Type.fromInterned(vector_type.child).abiSizeAdvanced(mod, strat)).scalar); | |
| 955 | const elem_bytes: u32 = @intCast((try Type.fromInterned(vector_type.child).abiSizeAdvanced(pt, strat)).scalar); | |
| 953 | 956 | if (elem_bytes == 0) return .{ .scalar = .@"1" }; |
| 954 | 957 | const bytes = elem_bytes * vector_type.len; |
| 955 | 958 | if (bytes > 32 and std.Target.x86.featureSetHas(target.cpu.features, .avx512f)) return .{ .scalar = .@"64" }; |
| ... | ... | @@ -959,12 +962,12 @@ pub fn abiAlignmentAdvanced( |
| 959 | 962 | } |
| 960 | 963 | }, |
| 961 | 964 | |
| 962 | .opt_type => return abiAlignmentAdvancedOptional(ty, mod, strat), | |
| 963 | .error_union_type => |info| return abiAlignmentAdvancedErrorUnion(ty, mod, strat, Type.fromInterned(info.payload_type)), | |
| 965 | .opt_type => return ty.abiAlignmentAdvancedOptional(pt, strat), | |
| 966 | .error_union_type => |info| return ty.abiAlignmentAdvancedErrorUnion(pt, strat, Type.fromInterned(info.payload_type)), | |
| 964 | 967 | |
| 965 | 968 | .error_set_type, .inferred_error_set_type => { |
| 966 | 969 | const bits = mod.errorSetBits(); |
| 967 | if (bits == 0) return AbiAlignmentAdvanced{ .scalar = .@"1" }; | |
| 970 | if (bits == 0) return .{ .scalar = .@"1" }; | |
| 968 | 971 | return .{ .scalar = intAbiAlignment(bits, target, use_llvm) }; |
| 969 | 972 | }, |
| 970 | 973 | |
| ... | ... | @@ -1012,10 +1015,7 @@ pub fn abiAlignmentAdvanced( |
| 1012 | 1015 | }, |
| 1013 | 1016 | .f80 => switch (target.c_type_bit_size(.longdouble)) { |
| 1014 | 1017 | 80 => return .{ .scalar = cTypeAlign(target, .longdouble) }, |
| 1015 | else => { | |
| 1016 | const u80_ty: Type = .{ .ip_index = .u80_type }; | |
| 1017 | return .{ .scalar = abiAlignment(u80_ty, mod) }; | |
| 1018 | }, | |
| 1018 | else => return .{ .scalar = Type.u80.abiAlignment(pt) }, | |
| 1019 | 1019 | }, |
| 1020 | 1020 | .f128 => switch (target.c_type_bit_size(.longdouble)) { |
| 1021 | 1021 | 128 => return .{ .scalar = cTypeAlign(target, .longdouble) }, |
| ... | ... | @@ -1024,7 +1024,7 @@ pub fn abiAlignmentAdvanced( |
| 1024 | 1024 | |
| 1025 | 1025 | .anyerror, .adhoc_inferred_error_set => { |
| 1026 | 1026 | const bits = mod.errorSetBits(); |
| 1027 | if (bits == 0) return AbiAlignmentAdvanced{ .scalar = .@"1" }; | |
| 1027 | if (bits == 0) return .{ .scalar = .@"1" }; | |
| 1028 | 1028 | return .{ .scalar = intAbiAlignment(bits, target, use_llvm) }; |
| 1029 | 1029 | }, |
| 1030 | 1030 | |
| ... | ... | @@ -1044,22 +1044,22 @@ pub fn abiAlignmentAdvanced( |
| 1044 | 1044 | const struct_type = ip.loadStructType(ty.toIntern()); |
| 1045 | 1045 | if (struct_type.layout == .@"packed") { |
| 1046 | 1046 | switch (strat) { |
| 1047 | .sema => try ty.resolveLayout(mod), | |
| 1047 | .sema => try ty.resolveLayout(pt), | |
| 1048 | 1048 | .lazy => if (struct_type.backingIntType(ip).* == .none) return .{ |
| 1049 | .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1049 | .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1050 | 1050 | .ty = .comptime_int_type, |
| 1051 | 1051 | .storage = .{ .lazy_align = ty.toIntern() }, |
| 1052 | } }))), | |
| 1052 | } })), | |
| 1053 | 1053 | }, |
| 1054 | 1054 | .eager => {}, |
| 1055 | 1055 | } |
| 1056 | return .{ .scalar = Type.fromInterned(struct_type.backingIntType(ip).*).abiAlignment(mod) }; | |
| 1056 | return .{ .scalar = Type.fromInterned(struct_type.backingIntType(ip).*).abiAlignment(pt) }; | |
| 1057 | 1057 | } |
| 1058 | 1058 | |
| 1059 | 1059 | if (struct_type.flagsPtr(ip).alignment == .none) switch (strat) { |
| 1060 | 1060 | .eager => unreachable, // struct alignment not resolved |
| 1061 | .sema => try ty.resolveStructAlignment(mod), | |
| 1062 | .lazy => return .{ .val = Value.fromInterned(try mod.intern(.{ .int = .{ | |
| 1061 | .sema => try ty.resolveStructAlignment(pt), | |
| 1062 | .lazy => return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1063 | 1063 | .ty = .comptime_int_type, |
| 1064 | 1064 | .storage = .{ .lazy_align = ty.toIntern() }, |
| 1065 | 1065 | } })) }, |
| ... | ... | @@ -1071,15 +1071,15 @@ pub fn abiAlignmentAdvanced( |
| 1071 | 1071 | var big_align: Alignment = .@"1"; |
| 1072 | 1072 | for (tuple.types.get(ip), tuple.values.get(ip)) |field_ty, val| { |
| 1073 | 1073 | if (val != .none) continue; // comptime field |
| 1074 | switch (try Type.fromInterned(field_ty).abiAlignmentAdvanced(mod, strat)) { | |
| 1074 | switch (try Type.fromInterned(field_ty).abiAlignmentAdvanced(pt, strat)) { | |
| 1075 | 1075 | .scalar => |field_align| big_align = big_align.max(field_align), |
| 1076 | 1076 | .val => switch (strat) { |
| 1077 | 1077 | .eager => unreachable, // field type alignment not resolved |
| 1078 | 1078 | .sema => unreachable, // passed to abiAlignmentAdvanced above |
| 1079 | .lazy => return .{ .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1079 | .lazy => return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1080 | 1080 | .ty = .comptime_int_type, |
| 1081 | 1081 | .storage = .{ .lazy_align = ty.toIntern() }, |
| 1082 | } }))) }, | |
| 1082 | } })) }, | |
| 1083 | 1083 | }, |
| 1084 | 1084 | } |
| 1085 | 1085 | } |
| ... | ... | @@ -1090,18 +1090,18 @@ pub fn abiAlignmentAdvanced( |
| 1090 | 1090 | |
| 1091 | 1091 | if (union_type.flagsPtr(ip).alignment == .none) switch (strat) { |
| 1092 | 1092 | .eager => unreachable, // union layout not resolved |
| 1093 | .sema => try ty.resolveUnionAlignment(mod), | |
| 1094 | .lazy => return .{ .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1093 | .sema => try ty.resolveUnionAlignment(pt), | |
| 1094 | .lazy => return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1095 | 1095 | .ty = .comptime_int_type, |
| 1096 | 1096 | .storage = .{ .lazy_align = ty.toIntern() }, |
| 1097 | } }))) }, | |
| 1097 | } })) }, | |
| 1098 | 1098 | }; |
| 1099 | 1099 | |
| 1100 | 1100 | return .{ .scalar = union_type.flagsPtr(ip).alignment }; |
| 1101 | 1101 | }, |
| 1102 | 1102 | .opaque_type => return .{ .scalar = .@"1" }, |
| 1103 | 1103 | .enum_type => return .{ |
| 1104 | .scalar = Type.fromInterned(ip.loadEnumType(ty.toIntern()).tag_ty).abiAlignment(mod), | |
| 1104 | .scalar = Type.fromInterned(ip.loadEnumType(ty.toIntern()).tag_ty).abiAlignment(pt), | |
| 1105 | 1105 | }, |
| 1106 | 1106 | |
| 1107 | 1107 | // values, not types |
| ... | ... | @@ -1131,91 +1131,92 @@ pub fn abiAlignmentAdvanced( |
| 1131 | 1131 | |
| 1132 | 1132 | fn abiAlignmentAdvancedErrorUnion( |
| 1133 | 1133 | ty: Type, |
| 1134 | mod: *Module, | |
| 1134 | pt: Zcu.PerThread, | |
| 1135 | 1135 | strat: ResolveStratLazy, |
| 1136 | 1136 | payload_ty: Type, |
| 1137 | 1137 | ) SemaError!AbiAlignmentAdvanced { |
| 1138 | 1138 | // This code needs to be kept in sync with the equivalent switch prong |
| 1139 | 1139 | // in abiSizeAdvanced. |
| 1140 | const code_align = abiAlignment(Type.anyerror, mod); | |
| 1140 | const code_align = Type.anyerror.abiAlignment(pt); | |
| 1141 | 1141 | switch (strat) { |
| 1142 | 1142 | .eager, .sema => { |
| 1143 | if (!(payload_ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) { | |
| 1144 | error.NeedLazy => return .{ .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1143 | if (!(payload_ty.hasRuntimeBitsAdvanced(pt, false, strat) catch |err| switch (err) { | |
| 1144 | error.NeedLazy => return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1145 | 1145 | .ty = .comptime_int_type, |
| 1146 | 1146 | .storage = .{ .lazy_align = ty.toIntern() }, |
| 1147 | } }))) }, | |
| 1147 | } })) }, | |
| 1148 | 1148 | else => |e| return e, |
| 1149 | 1149 | })) { |
| 1150 | 1150 | return .{ .scalar = code_align }; |
| 1151 | 1151 | } |
| 1152 | 1152 | return .{ .scalar = code_align.max( |
| 1153 | (try payload_ty.abiAlignmentAdvanced(mod, strat)).scalar, | |
| 1153 | (try payload_ty.abiAlignmentAdvanced(pt, strat)).scalar, | |
| 1154 | 1154 | ) }; |
| 1155 | 1155 | }, |
| 1156 | 1156 | .lazy => { |
| 1157 | switch (try payload_ty.abiAlignmentAdvanced(mod, strat)) { | |
| 1157 | switch (try payload_ty.abiAlignmentAdvanced(pt, strat)) { | |
| 1158 | 1158 | .scalar => |payload_align| return .{ .scalar = code_align.max(payload_align) }, |
| 1159 | 1159 | .val => {}, |
| 1160 | 1160 | } |
| 1161 | return .{ .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1161 | return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1162 | 1162 | .ty = .comptime_int_type, |
| 1163 | 1163 | .storage = .{ .lazy_align = ty.toIntern() }, |
| 1164 | } }))) }; | |
| 1164 | } })) }; | |
| 1165 | 1165 | }, |
| 1166 | 1166 | } |
| 1167 | 1167 | } |
| 1168 | 1168 | |
| 1169 | 1169 | fn abiAlignmentAdvancedOptional( |
| 1170 | 1170 | ty: Type, |
| 1171 | mod: *Module, | |
| 1171 | pt: Zcu.PerThread, | |
| 1172 | 1172 | strat: ResolveStratLazy, |
| 1173 | 1173 | ) SemaError!AbiAlignmentAdvanced { |
| 1174 | const mod = pt.zcu; | |
| 1174 | 1175 | const target = mod.getTarget(); |
| 1175 | 1176 | const child_type = ty.optionalChild(mod); |
| 1176 | 1177 | |
| 1177 | 1178 | switch (child_type.zigTypeTag(mod)) { |
| 1178 | 1179 | .Pointer => return .{ .scalar = ptrAbiAlignment(target) }, |
| 1179 | .ErrorSet => return abiAlignmentAdvanced(Type.anyerror, mod, strat), | |
| 1180 | .ErrorSet => return Type.anyerror.abiAlignmentAdvanced(pt, strat), | |
| 1180 | 1181 | .NoReturn => return .{ .scalar = .@"1" }, |
| 1181 | 1182 | else => {}, |
| 1182 | 1183 | } |
| 1183 | 1184 | |
| 1184 | 1185 | switch (strat) { |
| 1185 | 1186 | .eager, .sema => { |
| 1186 | if (!(child_type.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) { | |
| 1187 | error.NeedLazy => return .{ .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1187 | if (!(child_type.hasRuntimeBitsAdvanced(pt, false, strat) catch |err| switch (err) { | |
| 1188 | error.NeedLazy => return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1188 | 1189 | .ty = .comptime_int_type, |
| 1189 | 1190 | .storage = .{ .lazy_align = ty.toIntern() }, |
| 1190 | } }))) }, | |
| 1191 | } })) }, | |
| 1191 | 1192 | else => |e| return e, |
| 1192 | 1193 | })) { |
| 1193 | 1194 | return .{ .scalar = .@"1" }; |
| 1194 | 1195 | } |
| 1195 | return child_type.abiAlignmentAdvanced(mod, strat); | |
| 1196 | return child_type.abiAlignmentAdvanced(pt, strat); | |
| 1196 | 1197 | }, |
| 1197 | .lazy => switch (try child_type.abiAlignmentAdvanced(mod, strat)) { | |
| 1198 | .lazy => switch (try child_type.abiAlignmentAdvanced(pt, strat)) { | |
| 1198 | 1199 | .scalar => |x| return .{ .scalar = x.max(.@"1") }, |
| 1199 | .val => return .{ .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1200 | .val => return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1200 | 1201 | .ty = .comptime_int_type, |
| 1201 | 1202 | .storage = .{ .lazy_align = ty.toIntern() }, |
| 1202 | } }))) }, | |
| 1203 | } })) }, | |
| 1203 | 1204 | }, |
| 1204 | 1205 | } |
| 1205 | 1206 | } |
| 1206 | 1207 | |
| 1207 | 1208 | /// May capture a reference to `ty`. |
| 1208 | pub fn lazyAbiSize(ty: Type, mod: *Module) !Value { | |
| 1209 | switch (try ty.abiSizeAdvanced(mod, .lazy)) { | |
| 1209 | pub fn lazyAbiSize(ty: Type, pt: Zcu.PerThread) !Value { | |
| 1210 | switch (try ty.abiSizeAdvanced(pt, .lazy)) { | |
| 1210 | 1211 | .val => |val| return val, |
| 1211 | .scalar => |x| return mod.intValue(Type.comptime_int, x), | |
| 1212 | .scalar => |x| return pt.intValue(Type.comptime_int, x), | |
| 1212 | 1213 | } |
| 1213 | 1214 | } |
| 1214 | 1215 | |
| 1215 | 1216 | /// Asserts the type has the ABI size already resolved. |
| 1216 | 1217 | /// Types that return false for hasRuntimeBits() return 0. |
| 1217 | pub fn abiSize(ty: Type, mod: *Module) u64 { | |
| 1218 | return (abiSizeAdvanced(ty, mod, .eager) catch unreachable).scalar; | |
| 1218 | pub fn abiSize(ty: Type, pt: Zcu.PerThread) u64 { | |
| 1219 | return (abiSizeAdvanced(ty, pt, .eager) catch unreachable).scalar; | |
| 1219 | 1220 | } |
| 1220 | 1221 | |
| 1221 | 1222 | const AbiSizeAdvanced = union(enum) { |
| ... | ... | @@ -1231,38 +1232,39 @@ const AbiSizeAdvanced = union(enum) { |
| 1231 | 1232 | /// necessary, possibly returning a CompileError. |
| 1232 | 1233 | pub fn abiSizeAdvanced( |
| 1233 | 1234 | ty: Type, |
| 1234 | mod: *Module, | |
| 1235 | pt: Zcu.PerThread, | |
| 1235 | 1236 | strat: ResolveStratLazy, |
| 1236 | 1237 | ) SemaError!AbiSizeAdvanced { |
| 1238 | const mod = pt.zcu; | |
| 1237 | 1239 | const target = mod.getTarget(); |
| 1238 | 1240 | const use_llvm = mod.comp.config.use_llvm; |
| 1239 | 1241 | const ip = &mod.intern_pool; |
| 1240 | 1242 | |
| 1241 | 1243 | switch (ty.toIntern()) { |
| 1242 | .empty_struct_type => return AbiSizeAdvanced{ .scalar = 0 }, | |
| 1244 | .empty_struct_type => return .{ .scalar = 0 }, | |
| 1243 | 1245 | |
| 1244 | 1246 | else => switch (ip.indexToKey(ty.toIntern())) { |
| 1245 | 1247 | .int_type => |int_type| { |
| 1246 | if (int_type.bits == 0) return AbiSizeAdvanced{ .scalar = 0 }; | |
| 1247 | return AbiSizeAdvanced{ .scalar = intAbiSize(int_type.bits, target, use_llvm) }; | |
| 1248 | if (int_type.bits == 0) return .{ .scalar = 0 }; | |
| 1249 | return .{ .scalar = intAbiSize(int_type.bits, target, use_llvm) }; | |
| 1248 | 1250 | }, |
| 1249 | 1251 | .ptr_type => |ptr_type| switch (ptr_type.flags.size) { |
| 1250 | 1252 | .Slice => return .{ .scalar = @divExact(target.ptrBitWidth(), 8) * 2 }, |
| 1251 | 1253 | else => return .{ .scalar = @divExact(target.ptrBitWidth(), 8) }, |
| 1252 | 1254 | }, |
| 1253 | .anyframe_type => return AbiSizeAdvanced{ .scalar = @divExact(target.ptrBitWidth(), 8) }, | |
| 1255 | .anyframe_type => return .{ .scalar = @divExact(target.ptrBitWidth(), 8) }, | |
| 1254 | 1256 | |
| 1255 | 1257 | .array_type => |array_type| { |
| 1256 | 1258 | const len = array_type.lenIncludingSentinel(); |
| 1257 | 1259 | if (len == 0) return .{ .scalar = 0 }; |
| 1258 | switch (try Type.fromInterned(array_type.child).abiSizeAdvanced(mod, strat)) { | |
| 1260 | switch (try Type.fromInterned(array_type.child).abiSizeAdvanced(pt, strat)) { | |
| 1259 | 1261 | .scalar => |elem_size| return .{ .scalar = len * elem_size }, |
| 1260 | 1262 | .val => switch (strat) { |
| 1261 | 1263 | .sema, .eager => unreachable, |
| 1262 | .lazy => return .{ .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1264 | .lazy => return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1263 | 1265 | .ty = .comptime_int_type, |
| 1264 | 1266 | .storage = .{ .lazy_size = ty.toIntern() }, |
| 1265 | } }))) }, | |
| 1267 | } })) }, | |
| 1266 | 1268 | }, |
| 1267 | 1269 | } |
| 1268 | 1270 | }, |
| ... | ... | @@ -1270,71 +1272,71 @@ pub fn abiSizeAdvanced( |
| 1270 | 1272 | const sub_strat: ResolveStrat = switch (strat) { |
| 1271 | 1273 | .sema => .sema, |
| 1272 | 1274 | .eager => .normal, |
| 1273 | .lazy => return .{ .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1275 | .lazy => return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1274 | 1276 | .ty = .comptime_int_type, |
| 1275 | 1277 | .storage = .{ .lazy_size = ty.toIntern() }, |
| 1276 | } }))) }, | |
| 1278 | } })) }, | |
| 1277 | 1279 | }; |
| 1278 | const alignment = switch (try ty.abiAlignmentAdvanced(mod, strat)) { | |
| 1280 | const alignment = switch (try ty.abiAlignmentAdvanced(pt, strat)) { | |
| 1279 | 1281 | .scalar => |x| x, |
| 1280 | .val => return .{ .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1282 | .val => return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1281 | 1283 | .ty = .comptime_int_type, |
| 1282 | 1284 | .storage = .{ .lazy_size = ty.toIntern() }, |
| 1283 | } }))) }, | |
| 1285 | } })) }, | |
| 1284 | 1286 | }; |
| 1285 | 1287 | const total_bytes = switch (mod.comp.getZigBackend()) { |
| 1286 | 1288 | else => total_bytes: { |
| 1287 | const elem_bits = try Type.fromInterned(vector_type.child).bitSizeAdvanced(mod, sub_strat); | |
| 1289 | const elem_bits = try Type.fromInterned(vector_type.child).bitSizeAdvanced(pt, sub_strat); | |
| 1288 | 1290 | const total_bits = elem_bits * vector_type.len; |
| 1289 | 1291 | break :total_bytes (total_bits + 7) / 8; |
| 1290 | 1292 | }, |
| 1291 | 1293 | .stage2_c => total_bytes: { |
| 1292 | const elem_bytes: u32 = @intCast((try Type.fromInterned(vector_type.child).abiSizeAdvanced(mod, strat)).scalar); | |
| 1294 | const elem_bytes: u32 = @intCast((try Type.fromInterned(vector_type.child).abiSizeAdvanced(pt, strat)).scalar); | |
| 1293 | 1295 | break :total_bytes elem_bytes * vector_type.len; |
| 1294 | 1296 | }, |
| 1295 | 1297 | .stage2_x86_64 => total_bytes: { |
| 1296 | 1298 | if (vector_type.child == .bool_type) break :total_bytes std.math.divCeil(u32, vector_type.len, 8) catch unreachable; |
| 1297 | const elem_bytes: u32 = @intCast((try Type.fromInterned(vector_type.child).abiSizeAdvanced(mod, strat)).scalar); | |
| 1299 | const elem_bytes: u32 = @intCast((try Type.fromInterned(vector_type.child).abiSizeAdvanced(pt, strat)).scalar); | |
| 1298 | 1300 | break :total_bytes elem_bytes * vector_type.len; |
| 1299 | 1301 | }, |
| 1300 | 1302 | }; |
| 1301 | return AbiSizeAdvanced{ .scalar = alignment.forward(total_bytes) }; | |
| 1303 | return .{ .scalar = alignment.forward(total_bytes) }; | |
| 1302 | 1304 | }, |
| 1303 | 1305 | |
| 1304 | .opt_type => return ty.abiSizeAdvancedOptional(mod, strat), | |
| 1306 | .opt_type => return ty.abiSizeAdvancedOptional(pt, strat), | |
| 1305 | 1307 | |
| 1306 | 1308 | .error_set_type, .inferred_error_set_type => { |
| 1307 | 1309 | const bits = mod.errorSetBits(); |
| 1308 | if (bits == 0) return AbiSizeAdvanced{ .scalar = 0 }; | |
| 1309 | return AbiSizeAdvanced{ .scalar = intAbiSize(bits, target, use_llvm) }; | |
| 1310 | if (bits == 0) return .{ .scalar = 0 }; | |
| 1311 | return .{ .scalar = intAbiSize(bits, target, use_llvm) }; | |
| 1310 | 1312 | }, |
| 1311 | 1313 | |
| 1312 | 1314 | .error_union_type => |error_union_type| { |
| 1313 | 1315 | const payload_ty = Type.fromInterned(error_union_type.payload_type); |
| 1314 | 1316 | // This code needs to be kept in sync with the equivalent switch prong |
| 1315 | 1317 | // in abiAlignmentAdvanced. |
| 1316 | const code_size = abiSize(Type.anyerror, mod); | |
| 1317 | if (!(payload_ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) { | |
| 1318 | error.NeedLazy => return .{ .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1318 | const code_size = Type.anyerror.abiSize(pt); | |
| 1319 | if (!(payload_ty.hasRuntimeBitsAdvanced(pt, false, strat) catch |err| switch (err) { | |
| 1320 | error.NeedLazy => return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1319 | 1321 | .ty = .comptime_int_type, |
| 1320 | 1322 | .storage = .{ .lazy_size = ty.toIntern() }, |
| 1321 | } }))) }, | |
| 1323 | } })) }, | |
| 1322 | 1324 | else => |e| return e, |
| 1323 | 1325 | })) { |
| 1324 | 1326 | // Same as anyerror. |
| 1325 | return AbiSizeAdvanced{ .scalar = code_size }; | |
| 1327 | return .{ .scalar = code_size }; | |
| 1326 | 1328 | } |
| 1327 | const code_align = abiAlignment(Type.anyerror, mod); | |
| 1328 | const payload_align = abiAlignment(payload_ty, mod); | |
| 1329 | const payload_size = switch (try payload_ty.abiSizeAdvanced(mod, strat)) { | |
| 1329 | const code_align = Type.anyerror.abiAlignment(pt); | |
| 1330 | const payload_align = payload_ty.abiAlignment(pt); | |
| 1331 | const payload_size = switch (try payload_ty.abiSizeAdvanced(pt, strat)) { | |
| 1330 | 1332 | .scalar => |elem_size| elem_size, |
| 1331 | 1333 | .val => switch (strat) { |
| 1332 | 1334 | .sema => unreachable, |
| 1333 | 1335 | .eager => unreachable, |
| 1334 | .lazy => return .{ .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1336 | .lazy => return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1335 | 1337 | .ty = .comptime_int_type, |
| 1336 | 1338 | .storage = .{ .lazy_size = ty.toIntern() }, |
| 1337 | } }))) }, | |
| 1339 | } })) }, | |
| 1338 | 1340 | }, |
| 1339 | 1341 | }; |
| 1340 | 1342 | |
| ... | ... | @@ -1350,7 +1352,7 @@ pub fn abiSizeAdvanced( |
| 1350 | 1352 | size += code_size; |
| 1351 | 1353 | size = payload_align.forward(size); |
| 1352 | 1354 | } |
| 1353 | return AbiSizeAdvanced{ .scalar = size }; | |
| 1355 | return .{ .scalar = size }; | |
| 1354 | 1356 | }, |
| 1355 | 1357 | .func_type => unreachable, // represents machine code; not a pointer |
| 1356 | 1358 | .simple_type => |t| switch (t) { |
| ... | ... | @@ -1362,34 +1364,31 @@ pub fn abiSizeAdvanced( |
| 1362 | 1364 | .float_mode, |
| 1363 | 1365 | .reduce_op, |
| 1364 | 1366 | .call_modifier, |
| 1365 | => return AbiSizeAdvanced{ .scalar = 1 }, | |
| 1367 | => return .{ .scalar = 1 }, | |
| 1366 | 1368 | |
| 1367 | .f16 => return AbiSizeAdvanced{ .scalar = 2 }, | |
| 1368 | .f32 => return AbiSizeAdvanced{ .scalar = 4 }, | |
| 1369 | .f64 => return AbiSizeAdvanced{ .scalar = 8 }, | |
| 1370 | .f128 => return AbiSizeAdvanced{ .scalar = 16 }, | |
| 1369 | .f16 => return .{ .scalar = 2 }, | |
| 1370 | .f32 => return .{ .scalar = 4 }, | |
| 1371 | .f64 => return .{ .scalar = 8 }, | |
| 1372 | .f128 => return .{ .scalar = 16 }, | |
| 1371 | 1373 | .f80 => switch (target.c_type_bit_size(.longdouble)) { |
| 1372 | 80 => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.longdouble) }, | |
| 1373 | else => { | |
| 1374 | const u80_ty: Type = .{ .ip_index = .u80_type }; | |
| 1375 | return AbiSizeAdvanced{ .scalar = abiSize(u80_ty, mod) }; | |
| 1376 | }, | |
| 1374 | 80 => return .{ .scalar = target.c_type_byte_size(.longdouble) }, | |
| 1375 | else => return .{ .scalar = Type.u80.abiSize(pt) }, | |
| 1377 | 1376 | }, |
| 1378 | 1377 | |
| 1379 | 1378 | .usize, |
| 1380 | 1379 | .isize, |
| 1381 | => return AbiSizeAdvanced{ .scalar = @divExact(target.ptrBitWidth(), 8) }, | |
| 1382 | ||
| 1383 | .c_char => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.char) }, | |
| 1384 | .c_short => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.short) }, | |
| 1385 | .c_ushort => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.ushort) }, | |
| 1386 | .c_int => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.int) }, | |
| 1387 | .c_uint => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.uint) }, | |
| 1388 | .c_long => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.long) }, | |
| 1389 | .c_ulong => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.ulong) }, | |
| 1390 | .c_longlong => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.longlong) }, | |
| 1391 | .c_ulonglong => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.ulonglong) }, | |
| 1392 | .c_longdouble => return AbiSizeAdvanced{ .scalar = target.c_type_byte_size(.longdouble) }, | |
| 1380 | => return .{ .scalar = @divExact(target.ptrBitWidth(), 8) }, | |
| 1381 | ||
| 1382 | .c_char => return .{ .scalar = target.c_type_byte_size(.char) }, | |
| 1383 | .c_short => return .{ .scalar = target.c_type_byte_size(.short) }, | |
| 1384 | .c_ushort => return .{ .scalar = target.c_type_byte_size(.ushort) }, | |
| 1385 | .c_int => return .{ .scalar = target.c_type_byte_size(.int) }, | |
| 1386 | .c_uint => return .{ .scalar = target.c_type_byte_size(.uint) }, | |
| 1387 | .c_long => return .{ .scalar = target.c_type_byte_size(.long) }, | |
| 1388 | .c_ulong => return .{ .scalar = target.c_type_byte_size(.ulong) }, | |
| 1389 | .c_longlong => return .{ .scalar = target.c_type_byte_size(.longlong) }, | |
| 1390 | .c_ulonglong => return .{ .scalar = target.c_type_byte_size(.ulonglong) }, | |
| 1391 | .c_longdouble => return .{ .scalar = target.c_type_byte_size(.longdouble) }, | |
| 1393 | 1392 | |
| 1394 | 1393 | .anyopaque, |
| 1395 | 1394 | .void, |
| ... | ... | @@ -1399,12 +1398,12 @@ pub fn abiSizeAdvanced( |
| 1399 | 1398 | .null, |
| 1400 | 1399 | .undefined, |
| 1401 | 1400 | .enum_literal, |
| 1402 | => return AbiSizeAdvanced{ .scalar = 0 }, | |
| 1401 | => return .{ .scalar = 0 }, | |
| 1403 | 1402 | |
| 1404 | 1403 | .anyerror, .adhoc_inferred_error_set => { |
| 1405 | 1404 | const bits = mod.errorSetBits(); |
| 1406 | if (bits == 0) return AbiSizeAdvanced{ .scalar = 0 }; | |
| 1407 | return AbiSizeAdvanced{ .scalar = intAbiSize(bits, target, use_llvm) }; | |
| 1405 | if (bits == 0) return .{ .scalar = 0 }; | |
| 1406 | return .{ .scalar = intAbiSize(bits, target, use_llvm) }; | |
| 1408 | 1407 | }, |
| 1409 | 1408 | |
| 1410 | 1409 | .prefetch_options => unreachable, // missing call to resolveTypeFields |
| ... | ... | @@ -1418,22 +1417,22 @@ pub fn abiSizeAdvanced( |
| 1418 | 1417 | .struct_type => { |
| 1419 | 1418 | const struct_type = ip.loadStructType(ty.toIntern()); |
| 1420 | 1419 | switch (strat) { |
| 1421 | .sema => try ty.resolveLayout(mod), | |
| 1420 | .sema => try ty.resolveLayout(pt), | |
| 1422 | 1421 | .lazy => switch (struct_type.layout) { |
| 1423 | 1422 | .@"packed" => { |
| 1424 | 1423 | if (struct_type.backingIntType(ip).* == .none) return .{ |
| 1425 | .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1424 | .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1426 | 1425 | .ty = .comptime_int_type, |
| 1427 | 1426 | .storage = .{ .lazy_size = ty.toIntern() }, |
| 1428 | } }))), | |
| 1427 | } })), | |
| 1429 | 1428 | }; |
| 1430 | 1429 | }, |
| 1431 | 1430 | .auto, .@"extern" => { |
| 1432 | 1431 | if (!struct_type.haveLayout(ip)) return .{ |
| 1433 | .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1432 | .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1434 | 1433 | .ty = .comptime_int_type, |
| 1435 | 1434 | .storage = .{ .lazy_size = ty.toIntern() }, |
| 1436 | } }))), | |
| 1435 | } })), | |
| 1437 | 1436 | }; |
| 1438 | 1437 | }, |
| 1439 | 1438 | }, |
| ... | ... | @@ -1441,7 +1440,7 @@ pub fn abiSizeAdvanced( |
| 1441 | 1440 | } |
| 1442 | 1441 | switch (struct_type.layout) { |
| 1443 | 1442 | .@"packed" => return .{ |
| 1444 | .scalar = Type.fromInterned(struct_type.backingIntType(ip).*).abiSize(mod), | |
| 1443 | .scalar = Type.fromInterned(struct_type.backingIntType(ip).*).abiSize(pt), | |
| 1445 | 1444 | }, |
| 1446 | 1445 | .auto, .@"extern" => { |
| 1447 | 1446 | assert(struct_type.haveLayout(ip)); |
| ... | ... | @@ -1451,25 +1450,25 @@ pub fn abiSizeAdvanced( |
| 1451 | 1450 | }, |
| 1452 | 1451 | .anon_struct_type => |tuple| { |
| 1453 | 1452 | switch (strat) { |
| 1454 | .sema => try ty.resolveLayout(mod), | |
| 1453 | .sema => try ty.resolveLayout(pt), | |
| 1455 | 1454 | .lazy, .eager => {}, |
| 1456 | 1455 | } |
| 1457 | 1456 | const field_count = tuple.types.len; |
| 1458 | 1457 | if (field_count == 0) { |
| 1459 | return AbiSizeAdvanced{ .scalar = 0 }; | |
| 1458 | return .{ .scalar = 0 }; | |
| 1460 | 1459 | } |
| 1461 | return AbiSizeAdvanced{ .scalar = ty.structFieldOffset(field_count, mod) }; | |
| 1460 | return .{ .scalar = ty.structFieldOffset(field_count, pt) }; | |
| 1462 | 1461 | }, |
| 1463 | 1462 | |
| 1464 | 1463 | .union_type => { |
| 1465 | 1464 | const union_type = ip.loadUnionType(ty.toIntern()); |
| 1466 | 1465 | switch (strat) { |
| 1467 | .sema => try ty.resolveLayout(mod), | |
| 1466 | .sema => try ty.resolveLayout(pt), | |
| 1468 | 1467 | .lazy => if (!union_type.flagsPtr(ip).status.haveLayout()) return .{ |
| 1469 | .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1468 | .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1470 | 1469 | .ty = .comptime_int_type, |
| 1471 | 1470 | .storage = .{ .lazy_size = ty.toIntern() }, |
| 1472 | } }))), | |
| 1471 | } })), | |
| 1473 | 1472 | }, |
| 1474 | 1473 | .eager => {}, |
| 1475 | 1474 | } |
| ... | ... | @@ -1478,7 +1477,7 @@ pub fn abiSizeAdvanced( |
| 1478 | 1477 | return .{ .scalar = union_type.size(ip).* }; |
| 1479 | 1478 | }, |
| 1480 | 1479 | .opaque_type => unreachable, // no size available |
| 1481 | .enum_type => return .{ .scalar = Type.fromInterned(ip.loadEnumType(ty.toIntern()).tag_ty).abiSize(mod) }, | |
| 1480 | .enum_type => return .{ .scalar = Type.fromInterned(ip.loadEnumType(ty.toIntern()).tag_ty).abiSize(pt) }, | |
| 1482 | 1481 | |
| 1483 | 1482 | // values, not types |
| 1484 | 1483 | .undef, |
| ... | ... | @@ -1507,36 +1506,37 @@ pub fn abiSizeAdvanced( |
| 1507 | 1506 | |
| 1508 | 1507 | fn abiSizeAdvancedOptional( |
| 1509 | 1508 | ty: Type, |
| 1510 | mod: *Module, | |
| 1509 | pt: Zcu.PerThread, | |
| 1511 | 1510 | strat: ResolveStratLazy, |
| 1512 | 1511 | ) SemaError!AbiSizeAdvanced { |
| 1512 | const mod = pt.zcu; | |
| 1513 | 1513 | const child_ty = ty.optionalChild(mod); |
| 1514 | 1514 | |
| 1515 | 1515 | if (child_ty.isNoReturn(mod)) { |
| 1516 | return AbiSizeAdvanced{ .scalar = 0 }; | |
| 1516 | return .{ .scalar = 0 }; | |
| 1517 | 1517 | } |
| 1518 | 1518 | |
| 1519 | if (!(child_ty.hasRuntimeBitsAdvanced(mod, false, strat) catch |err| switch (err) { | |
| 1520 | error.NeedLazy => return .{ .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1519 | if (!(child_ty.hasRuntimeBitsAdvanced(pt, false, strat) catch |err| switch (err) { | |
| 1520 | error.NeedLazy => return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1521 | 1521 | .ty = .comptime_int_type, |
| 1522 | 1522 | .storage = .{ .lazy_size = ty.toIntern() }, |
| 1523 | } }))) }, | |
| 1523 | } })) }, | |
| 1524 | 1524 | else => |e| return e, |
| 1525 | })) return AbiSizeAdvanced{ .scalar = 1 }; | |
| 1525 | })) return .{ .scalar = 1 }; | |
| 1526 | 1526 | |
| 1527 | 1527 | if (ty.optionalReprIsPayload(mod)) { |
| 1528 | return abiSizeAdvanced(child_ty, mod, strat); | |
| 1528 | return child_ty.abiSizeAdvanced(pt, strat); | |
| 1529 | 1529 | } |
| 1530 | 1530 | |
| 1531 | const payload_size = switch (try child_ty.abiSizeAdvanced(mod, strat)) { | |
| 1531 | const payload_size = switch (try child_ty.abiSizeAdvanced(pt, strat)) { | |
| 1532 | 1532 | .scalar => |elem_size| elem_size, |
| 1533 | 1533 | .val => switch (strat) { |
| 1534 | 1534 | .sema => unreachable, |
| 1535 | 1535 | .eager => unreachable, |
| 1536 | .lazy => return .{ .val = Value.fromInterned((try mod.intern(.{ .int = .{ | |
| 1536 | .lazy => return .{ .val = Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1537 | 1537 | .ty = .comptime_int_type, |
| 1538 | 1538 | .storage = .{ .lazy_size = ty.toIntern() }, |
| 1539 | } }))) }, | |
| 1539 | } })) }, | |
| 1540 | 1540 | }, |
| 1541 | 1541 | }; |
| 1542 | 1542 | |
| ... | ... | @@ -1544,8 +1544,8 @@ fn abiSizeAdvancedOptional( |
| 1544 | 1544 | // field and a boolean as the second. Since the child type's abi alignment is |
| 1545 | 1545 | // guaranteed to be >= that of bool's (1 byte) the added size is exactly equal |
| 1546 | 1546 | // to the child type's ABI alignment. |
| 1547 | return AbiSizeAdvanced{ | |
| 1548 | .scalar = (child_ty.abiAlignment(mod).toByteUnits() orelse 0) + payload_size, | |
| 1547 | return .{ | |
| 1548 | .scalar = (child_ty.abiAlignment(pt).toByteUnits() orelse 0) + payload_size, | |
| 1549 | 1549 | }; |
| 1550 | 1550 | } |
| 1551 | 1551 | |
| ... | ... | @@ -1675,15 +1675,16 @@ pub fn maxIntAlignment(target: std.Target, use_llvm: bool) u16 { |
| 1675 | 1675 | }; |
| 1676 | 1676 | } |
| 1677 | 1677 | |
| 1678 | pub fn bitSize(ty: Type, mod: *Module) u64 { | |
| 1679 | return bitSizeAdvanced(ty, mod, .normal) catch unreachable; | |
| 1678 | pub fn bitSize(ty: Type, pt: Zcu.PerThread) u64 { | |
| 1679 | return bitSizeAdvanced(ty, pt, .normal) catch unreachable; | |
| 1680 | 1680 | } |
| 1681 | 1681 | |
| 1682 | 1682 | pub fn bitSizeAdvanced( |
| 1683 | 1683 | ty: Type, |
| 1684 | mod: *Module, | |
| 1684 | pt: Zcu.PerThread, | |
| 1685 | 1685 | strat: ResolveStrat, |
| 1686 | 1686 | ) SemaError!u64 { |
| 1687 | const mod = pt.zcu; | |
| 1687 | 1688 | const target = mod.getTarget(); |
| 1688 | 1689 | const ip = &mod.intern_pool; |
| 1689 | 1690 | |
| ... | ... | @@ -1702,22 +1703,22 @@ pub fn bitSizeAdvanced( |
| 1702 | 1703 | if (len == 0) return 0; |
| 1703 | 1704 | const elem_ty = Type.fromInterned(array_type.child); |
| 1704 | 1705 | const elem_size = @max( |
| 1705 | (try elem_ty.abiAlignmentAdvanced(mod, strat_lazy)).scalar.toByteUnits() orelse 0, | |
| 1706 | (try elem_ty.abiSizeAdvanced(mod, strat_lazy)).scalar, | |
| 1706 | (try elem_ty.abiAlignmentAdvanced(pt, strat_lazy)).scalar.toByteUnits() orelse 0, | |
| 1707 | (try elem_ty.abiSizeAdvanced(pt, strat_lazy)).scalar, | |
| 1707 | 1708 | ); |
| 1708 | 1709 | if (elem_size == 0) return 0; |
| 1709 | const elem_bit_size = try bitSizeAdvanced(elem_ty, mod, strat); | |
| 1710 | const elem_bit_size = try elem_ty.bitSizeAdvanced(pt, strat); | |
| 1710 | 1711 | return (len - 1) * 8 * elem_size + elem_bit_size; |
| 1711 | 1712 | }, |
| 1712 | 1713 | .vector_type => |vector_type| { |
| 1713 | 1714 | const child_ty = Type.fromInterned(vector_type.child); |
| 1714 | const elem_bit_size = try bitSizeAdvanced(child_ty, mod, strat); | |
| 1715 | const elem_bit_size = try child_ty.bitSizeAdvanced(pt, strat); | |
| 1715 | 1716 | return elem_bit_size * vector_type.len; |
| 1716 | 1717 | }, |
| 1717 | 1718 | .opt_type => { |
| 1718 | 1719 | // Optionals and error unions are not packed so their bitsize |
| 1719 | 1720 | // includes padding bits. |
| 1720 | return (try abiSizeAdvanced(ty, mod, strat_lazy)).scalar * 8; | |
| 1721 | return (try ty.abiSizeAdvanced(pt, strat_lazy)).scalar * 8; | |
| 1721 | 1722 | }, |
| 1722 | 1723 | |
| 1723 | 1724 | .error_set_type, .inferred_error_set_type => return mod.errorSetBits(), |
| ... | ... | @@ -1725,7 +1726,7 @@ pub fn bitSizeAdvanced( |
| 1725 | 1726 | .error_union_type => { |
| 1726 | 1727 | // Optionals and error unions are not packed so their bitsize |
| 1727 | 1728 | // includes padding bits. |
| 1728 | return (try abiSizeAdvanced(ty, mod, strat_lazy)).scalar * 8; | |
| 1729 | return (try ty.abiSizeAdvanced(pt, strat_lazy)).scalar * 8; | |
| 1729 | 1730 | }, |
| 1730 | 1731 | .func_type => unreachable, // represents machine code; not a pointer |
| 1731 | 1732 | .simple_type => |t| switch (t) { |
| ... | ... | @@ -1783,42 +1784,42 @@ pub fn bitSizeAdvanced( |
| 1783 | 1784 | const struct_type = ip.loadStructType(ty.toIntern()); |
| 1784 | 1785 | const is_packed = struct_type.layout == .@"packed"; |
| 1785 | 1786 | if (strat == .sema) { |
| 1786 | try ty.resolveFields(mod); | |
| 1787 | if (is_packed) try ty.resolveLayout(mod); | |
| 1787 | try ty.resolveFields(pt); | |
| 1788 | if (is_packed) try ty.resolveLayout(pt); | |
| 1788 | 1789 | } |
| 1789 | 1790 | if (is_packed) { |
| 1790 | return try Type.fromInterned(struct_type.backingIntType(ip).*).bitSizeAdvanced(mod, strat); | |
| 1791 | return try Type.fromInterned(struct_type.backingIntType(ip).*).bitSizeAdvanced(pt, strat); | |
| 1791 | 1792 | } |
| 1792 | return (try ty.abiSizeAdvanced(mod, strat_lazy)).scalar * 8; | |
| 1793 | return (try ty.abiSizeAdvanced(pt, strat_lazy)).scalar * 8; | |
| 1793 | 1794 | }, |
| 1794 | 1795 | |
| 1795 | 1796 | .anon_struct_type => { |
| 1796 | if (strat == .sema) try ty.resolveFields(mod); | |
| 1797 | return (try ty.abiSizeAdvanced(mod, strat_lazy)).scalar * 8; | |
| 1797 | if (strat == .sema) try ty.resolveFields(pt); | |
| 1798 | return (try ty.abiSizeAdvanced(pt, strat_lazy)).scalar * 8; | |
| 1798 | 1799 | }, |
| 1799 | 1800 | |
| 1800 | 1801 | .union_type => { |
| 1801 | 1802 | const union_type = ip.loadUnionType(ty.toIntern()); |
| 1802 | 1803 | const is_packed = ty.containerLayout(mod) == .@"packed"; |
| 1803 | 1804 | if (strat == .sema) { |
| 1804 | try ty.resolveFields(mod); | |
| 1805 | if (is_packed) try ty.resolveLayout(mod); | |
| 1805 | try ty.resolveFields(pt); | |
| 1806 | if (is_packed) try ty.resolveLayout(pt); | |
| 1806 | 1807 | } |
| 1807 | 1808 | if (!is_packed) { |
| 1808 | return (try ty.abiSizeAdvanced(mod, strat_lazy)).scalar * 8; | |
| 1809 | return (try ty.abiSizeAdvanced(pt, strat_lazy)).scalar * 8; | |
| 1809 | 1810 | } |
| 1810 | 1811 | assert(union_type.flagsPtr(ip).status.haveFieldTypes()); |
| 1811 | 1812 | |
| 1812 | 1813 | var size: u64 = 0; |
| 1813 | 1814 | for (0..union_type.field_types.len) |field_index| { |
| 1814 | 1815 | const field_ty = union_type.field_types.get(ip)[field_index]; |
| 1815 | size = @max(size, try bitSizeAdvanced(Type.fromInterned(field_ty), mod, strat)); | |
| 1816 | size = @max(size, try Type.fromInterned(field_ty).bitSizeAdvanced(pt, strat)); | |
| 1816 | 1817 | } |
| 1817 | 1818 | |
| 1818 | 1819 | return size; |
| 1819 | 1820 | }, |
| 1820 | 1821 | .opaque_type => unreachable, |
| 1821 | .enum_type => return bitSizeAdvanced(Type.fromInterned(ip.loadEnumType(ty.toIntern()).tag_ty), mod, strat), | |
| 1822 | .enum_type => return Type.fromInterned(ip.loadEnumType(ty.toIntern()).tag_ty).bitSizeAdvanced(pt, strat), | |
| 1822 | 1823 | |
| 1823 | 1824 | // values, not types |
| 1824 | 1825 | .undef, |
| ... | ... | @@ -1870,7 +1871,7 @@ pub fn isSinglePointer(ty: Type, mod: *const Module) bool { |
| 1870 | 1871 | |
| 1871 | 1872 | /// Asserts `ty` is a pointer. |
| 1872 | 1873 | pub fn ptrSize(ty: Type, mod: *const Module) std.builtin.Type.Pointer.Size { |
| 1873 | return ptrSizeOrNull(ty, mod).?; | |
| 1874 | return ty.ptrSizeOrNull(mod).?; | |
| 1874 | 1875 | } |
| 1875 | 1876 | |
| 1876 | 1877 | /// Returns `null` if `ty` is not a pointer. |
| ... | ... | @@ -2105,29 +2106,28 @@ pub fn unionTagFieldIndex(ty: Type, enum_tag: Value, mod: *Module) ?u32 { |
| 2105 | 2106 | return mod.unionTagFieldIndex(union_obj, enum_tag); |
| 2106 | 2107 | } |
| 2107 | 2108 | |
| 2108 | pub fn unionHasAllZeroBitFieldTypes(ty: Type, mod: *Module) bool { | |
| 2109 | const ip = &mod.intern_pool; | |
| 2110 | const union_obj = mod.typeToUnion(ty).?; | |
| 2109 | pub fn unionHasAllZeroBitFieldTypes(ty: Type, pt: Zcu.PerThread) bool { | |
| 2110 | const ip = &pt.zcu.intern_pool; | |
| 2111 | const union_obj = pt.zcu.typeToUnion(ty).?; | |
| 2111 | 2112 | for (union_obj.field_types.get(ip)) |field_ty| { |
| 2112 | if (Type.fromInterned(field_ty).hasRuntimeBits(mod)) return false; | |
| 2113 | if (Type.fromInterned(field_ty).hasRuntimeBits(pt)) return false; | |
| 2113 | 2114 | } |
| 2114 | 2115 | return true; |
| 2115 | 2116 | } |
| 2116 | 2117 | |
| 2117 | 2118 | /// Returns the type used for backing storage of this union during comptime operations. |
| 2118 | 2119 | /// Asserts the type is either an extern or packed union. |
| 2119 | pub fn unionBackingType(ty: Type, mod: *Module) !Type { | |
| 2120 | return switch (ty.containerLayout(mod)) { | |
| 2121 | .@"extern" => try mod.arrayType(.{ .len = ty.abiSize(mod), .child = .u8_type }), | |
| 2122 | .@"packed" => try mod.intType(.unsigned, @intCast(ty.bitSize(mod))), | |
| 2120 | pub fn unionBackingType(ty: Type, pt: Zcu.PerThread) !Type { | |
| 2121 | return switch (ty.containerLayout(pt.zcu)) { | |
| 2122 | .@"extern" => try pt.arrayType(.{ .len = ty.abiSize(pt), .child = .u8_type }), | |
| 2123 | .@"packed" => try pt.intType(.unsigned, @intCast(ty.bitSize(pt))), | |
| 2123 | 2124 | .auto => unreachable, |
| 2124 | 2125 | }; |
| 2125 | 2126 | } |
| 2126 | 2127 | |
| 2127 | pub fn unionGetLayout(ty: Type, mod: *Module) Module.UnionLayout { | |
| 2128 | const ip = &mod.intern_pool; | |
| 2129 | const union_obj = ip.loadUnionType(ty.toIntern()); | |
| 2130 | return mod.getUnionLayout(union_obj); | |
| 2128 | pub fn unionGetLayout(ty: Type, pt: Zcu.PerThread) Module.UnionLayout { | |
| 2129 | const union_obj = pt.zcu.intern_pool.loadUnionType(ty.toIntern()); | |
| 2130 | return pt.getUnionLayout(union_obj); | |
| 2131 | 2131 | } |
| 2132 | 2132 | |
| 2133 | 2133 | pub fn containerLayout(ty: Type, mod: *Module) std.builtin.Type.ContainerLayout { |
| ... | ... | @@ -2509,7 +2509,8 @@ pub fn isNumeric(ty: Type, mod: *const Module) bool { |
| 2509 | 2509 | |
| 2510 | 2510 | /// During semantic analysis, instead call `Sema.typeHasOnePossibleValue` which |
| 2511 | 2511 | /// resolves field types rather than asserting they are already resolved. |
| 2512 | pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value { | |
| 2512 | pub fn onePossibleValue(starting_type: Type, pt: Zcu.PerThread) !?Value { | |
| 2513 | const mod = pt.zcu; | |
| 2513 | 2514 | var ty = starting_type; |
| 2514 | 2515 | const ip = &mod.intern_pool; |
| 2515 | 2516 | while (true) switch (ty.toIntern()) { |
| ... | ... | @@ -2518,7 +2519,7 @@ pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value { |
| 2518 | 2519 | else => switch (ip.indexToKey(ty.toIntern())) { |
| 2519 | 2520 | .int_type => |int_type| { |
| 2520 | 2521 | if (int_type.bits == 0) { |
| 2521 | return try mod.intValue(ty, 0); | |
| 2522 | return try pt.intValue(ty, 0); | |
| 2522 | 2523 | } else { |
| 2523 | 2524 | return null; |
| 2524 | 2525 | } |
| ... | ... | @@ -2534,21 +2535,21 @@ pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value { |
| 2534 | 2535 | |
| 2535 | 2536 | inline .array_type, .vector_type => |seq_type, seq_tag| { |
| 2536 | 2537 | const has_sentinel = seq_tag == .array_type and seq_type.sentinel != .none; |
| 2537 | if (seq_type.len + @intFromBool(has_sentinel) == 0) return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2538 | if (seq_type.len + @intFromBool(has_sentinel) == 0) return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2538 | 2539 | .ty = ty.toIntern(), |
| 2539 | 2540 | .storage = .{ .elems = &.{} }, |
| 2540 | } }))); | |
| 2541 | if (try Type.fromInterned(seq_type.child).onePossibleValue(mod)) |opv| { | |
| 2542 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2541 | } })); | |
| 2542 | if (try Type.fromInterned(seq_type.child).onePossibleValue(pt)) |opv| { | |
| 2543 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2543 | 2544 | .ty = ty.toIntern(), |
| 2544 | 2545 | .storage = .{ .repeated_elem = opv.toIntern() }, |
| 2545 | } }))); | |
| 2546 | } })); | |
| 2546 | 2547 | } |
| 2547 | 2548 | return null; |
| 2548 | 2549 | }, |
| 2549 | 2550 | .opt_type => |child| { |
| 2550 | 2551 | if (child == .noreturn_type) { |
| 2551 | return try mod.nullValue(ty); | |
| 2552 | return try pt.nullValue(ty); | |
| 2552 | 2553 | } else { |
| 2553 | 2554 | return null; |
| 2554 | 2555 | } |
| ... | ... | @@ -2615,17 +2616,17 @@ pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value { |
| 2615 | 2616 | continue; |
| 2616 | 2617 | } |
| 2617 | 2618 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[i]); |
| 2618 | if (try field_ty.onePossibleValue(mod)) |field_opv| { | |
| 2619 | if (try field_ty.onePossibleValue(pt)) |field_opv| { | |
| 2619 | 2620 | field_val.* = field_opv.toIntern(); |
| 2620 | 2621 | } else return null; |
| 2621 | 2622 | } |
| 2622 | 2623 | |
| 2623 | 2624 | // In this case the struct has no runtime-known fields and |
| 2624 | 2625 | // therefore has one possible value. |
| 2625 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2626 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2626 | 2627 | .ty = ty.toIntern(), |
| 2627 | 2628 | .storage = .{ .elems = field_vals }, |
| 2628 | } }))); | |
| 2629 | } })); | |
| 2629 | 2630 | }, |
| 2630 | 2631 | |
| 2631 | 2632 | .anon_struct_type => |tuple| { |
| ... | ... | @@ -2637,24 +2638,24 @@ pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value { |
| 2637 | 2638 | // TODO: write something like getCoercedInts to avoid needing to dupe |
| 2638 | 2639 | const duped_values = try mod.gpa.dupe(InternPool.Index, tuple.values.get(ip)); |
| 2639 | 2640 | defer mod.gpa.free(duped_values); |
| 2640 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2641 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2641 | 2642 | .ty = ty.toIntern(), |
| 2642 | 2643 | .storage = .{ .elems = duped_values }, |
| 2643 | } }))); | |
| 2644 | } })); | |
| 2644 | 2645 | }, |
| 2645 | 2646 | |
| 2646 | 2647 | .union_type => { |
| 2647 | 2648 | const union_obj = ip.loadUnionType(ty.toIntern()); |
| 2648 | const tag_val = (try Type.fromInterned(union_obj.enum_tag_ty).onePossibleValue(mod)) orelse | |
| 2649 | const tag_val = (try Type.fromInterned(union_obj.enum_tag_ty).onePossibleValue(pt)) orelse | |
| 2649 | 2650 | return null; |
| 2650 | 2651 | if (union_obj.field_types.len == 0) { |
| 2651 | const only = try mod.intern(.{ .empty_enum_value = ty.toIntern() }); | |
| 2652 | const only = try pt.intern(.{ .empty_enum_value = ty.toIntern() }); | |
| 2652 | 2653 | return Value.fromInterned(only); |
| 2653 | 2654 | } |
| 2654 | 2655 | const only_field_ty = union_obj.field_types.get(ip)[0]; |
| 2655 | const val_val = (try Type.fromInterned(only_field_ty).onePossibleValue(mod)) orelse | |
| 2656 | const val_val = (try Type.fromInterned(only_field_ty).onePossibleValue(pt)) orelse | |
| 2656 | 2657 | return null; |
| 2657 | const only = try mod.intern(.{ .un = .{ | |
| 2658 | const only = try pt.intern(.{ .un = .{ | |
| 2658 | 2659 | .ty = ty.toIntern(), |
| 2659 | 2660 | .tag = tag_val.toIntern(), |
| 2660 | 2661 | .val = val_val.toIntern(), |
| ... | ... | @@ -2668,8 +2669,8 @@ pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value { |
| 2668 | 2669 | .nonexhaustive => { |
| 2669 | 2670 | if (enum_type.tag_ty == .comptime_int_type) return null; |
| 2670 | 2671 | |
| 2671 | if (try Type.fromInterned(enum_type.tag_ty).onePossibleValue(mod)) |int_opv| { | |
| 2672 | const only = try mod.intern(.{ .enum_tag = .{ | |
| 2672 | if (try Type.fromInterned(enum_type.tag_ty).onePossibleValue(pt)) |int_opv| { | |
| 2673 | const only = try pt.intern(.{ .enum_tag = .{ | |
| 2673 | 2674 | .ty = ty.toIntern(), |
| 2674 | 2675 | .int = int_opv.toIntern(), |
| 2675 | 2676 | } }); |
| ... | ... | @@ -2679,18 +2680,18 @@ pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value { |
| 2679 | 2680 | return null; |
| 2680 | 2681 | }, |
| 2681 | 2682 | .auto, .explicit => { |
| 2682 | if (Type.fromInterned(enum_type.tag_ty).hasRuntimeBits(mod)) return null; | |
| 2683 | if (Type.fromInterned(enum_type.tag_ty).hasRuntimeBits(pt)) return null; | |
| 2683 | 2684 | |
| 2684 | 2685 | switch (enum_type.names.len) { |
| 2685 | 2686 | 0 => { |
| 2686 | const only = try mod.intern(.{ .empty_enum_value = ty.toIntern() }); | |
| 2687 | const only = try pt.intern(.{ .empty_enum_value = ty.toIntern() }); | |
| 2687 | 2688 | return Value.fromInterned(only); |
| 2688 | 2689 | }, |
| 2689 | 2690 | 1 => { |
| 2690 | 2691 | if (enum_type.values.len == 0) { |
| 2691 | const only = try mod.intern(.{ .enum_tag = .{ | |
| 2692 | const only = try pt.intern(.{ .enum_tag = .{ | |
| 2692 | 2693 | .ty = ty.toIntern(), |
| 2693 | .int = try mod.intern(.{ .int = .{ | |
| 2694 | .int = try pt.intern(.{ .int = .{ | |
| 2694 | 2695 | .ty = enum_type.tag_ty, |
| 2695 | 2696 | .storage = .{ .u64 = 0 }, |
| 2696 | 2697 | } }), |
| ... | ... | @@ -2733,13 +2734,14 @@ pub fn onePossibleValue(starting_type: Type, mod: *Module) !?Value { |
| 2733 | 2734 | |
| 2734 | 2735 | /// During semantic analysis, instead call `Sema.typeRequiresComptime` which |
| 2735 | 2736 | /// resolves field types rather than asserting they are already resolved. |
| 2736 | pub fn comptimeOnly(ty: Type, mod: *Module) bool { | |
| 2737 | return ty.comptimeOnlyAdvanced(mod, .normal) catch unreachable; | |
| 2737 | pub fn comptimeOnly(ty: Type, pt: Zcu.PerThread) bool { | |
| 2738 | return ty.comptimeOnlyAdvanced(pt, .normal) catch unreachable; | |
| 2738 | 2739 | } |
| 2739 | 2740 | |
| 2740 | 2741 | /// `generic_poison` will return false. |
| 2741 | 2742 | /// May return false negatives when structs and unions are having their field types resolved. |
| 2742 | pub fn comptimeOnlyAdvanced(ty: Type, mod: *Module, strat: ResolveStrat) SemaError!bool { | |
| 2743 | pub fn comptimeOnlyAdvanced(ty: Type, pt: Zcu.PerThread, strat: ResolveStrat) SemaError!bool { | |
| 2744 | const mod = pt.zcu; | |
| 2743 | 2745 | const ip = &mod.intern_pool; |
| 2744 | 2746 | return switch (ty.toIntern()) { |
| 2745 | 2747 | .empty_struct_type => false, |
| ... | ... | @@ -2749,19 +2751,19 @@ pub fn comptimeOnlyAdvanced(ty: Type, mod: *Module, strat: ResolveStrat) SemaErr |
| 2749 | 2751 | .ptr_type => |ptr_type| { |
| 2750 | 2752 | const child_ty = Type.fromInterned(ptr_type.child); |
| 2751 | 2753 | switch (child_ty.zigTypeTag(mod)) { |
| 2752 | .Fn => return !try child_ty.fnHasRuntimeBitsAdvanced(mod, strat), | |
| 2754 | .Fn => return !try child_ty.fnHasRuntimeBitsAdvanced(pt, strat), | |
| 2753 | 2755 | .Opaque => return false, |
| 2754 | else => return child_ty.comptimeOnlyAdvanced(mod, strat), | |
| 2756 | else => return child_ty.comptimeOnlyAdvanced(pt, strat), | |
| 2755 | 2757 | } |
| 2756 | 2758 | }, |
| 2757 | 2759 | .anyframe_type => |child| { |
| 2758 | 2760 | if (child == .none) return false; |
| 2759 | return Type.fromInterned(child).comptimeOnlyAdvanced(mod, strat); | |
| 2761 | return Type.fromInterned(child).comptimeOnlyAdvanced(pt, strat); | |
| 2760 | 2762 | }, |
| 2761 | .array_type => |array_type| return Type.fromInterned(array_type.child).comptimeOnlyAdvanced(mod, strat), | |
| 2762 | .vector_type => |vector_type| return Type.fromInterned(vector_type.child).comptimeOnlyAdvanced(mod, strat), | |
| 2763 | .opt_type => |child| return Type.fromInterned(child).comptimeOnlyAdvanced(mod, strat), | |
| 2764 | .error_union_type => |error_union_type| return Type.fromInterned(error_union_type.payload_type).comptimeOnlyAdvanced(mod, strat), | |
| 2763 | .array_type => |array_type| return Type.fromInterned(array_type.child).comptimeOnlyAdvanced(pt, strat), | |
| 2764 | .vector_type => |vector_type| return Type.fromInterned(vector_type.child).comptimeOnlyAdvanced(pt, strat), | |
| 2765 | .opt_type => |child| return Type.fromInterned(child).comptimeOnlyAdvanced(pt, strat), | |
| 2766 | .error_union_type => |error_union_type| return Type.fromInterned(error_union_type.payload_type).comptimeOnlyAdvanced(pt, strat), | |
| 2765 | 2767 | |
| 2766 | 2768 | .error_set_type, |
| 2767 | 2769 | .inferred_error_set_type, |
| ... | ... | @@ -2836,13 +2838,13 @@ pub fn comptimeOnlyAdvanced(ty: Type, mod: *Module, strat: ResolveStrat) SemaErr |
| 2836 | 2838 | struct_type.flagsPtr(ip).requires_comptime = .wip; |
| 2837 | 2839 | errdefer struct_type.flagsPtr(ip).requires_comptime = .unknown; |
| 2838 | 2840 | |
| 2839 | try ty.resolveFields(mod); | |
| 2841 | try ty.resolveFields(pt); | |
| 2840 | 2842 | |
| 2841 | 2843 | for (0..struct_type.field_types.len) |i_usize| { |
| 2842 | 2844 | const i: u32 = @intCast(i_usize); |
| 2843 | 2845 | if (struct_type.fieldIsComptime(ip, i)) continue; |
| 2844 | 2846 | const field_ty = struct_type.field_types.get(ip)[i]; |
| 2845 | if (try Type.fromInterned(field_ty).comptimeOnlyAdvanced(mod, strat)) { | |
| 2847 | if (try Type.fromInterned(field_ty).comptimeOnlyAdvanced(pt, strat)) { | |
| 2846 | 2848 | // Note that this does not cause the layout to |
| 2847 | 2849 | // be considered resolved. Comptime-only types |
| 2848 | 2850 | // still maintain a layout of their |
| ... | ... | @@ -2861,7 +2863,7 @@ pub fn comptimeOnlyAdvanced(ty: Type, mod: *Module, strat: ResolveStrat) SemaErr |
| 2861 | 2863 | .anon_struct_type => |tuple| { |
| 2862 | 2864 | for (tuple.types.get(ip), tuple.values.get(ip)) |field_ty, val| { |
| 2863 | 2865 | const have_comptime_val = val != .none; |
| 2864 | if (!have_comptime_val and try Type.fromInterned(field_ty).comptimeOnlyAdvanced(mod, strat)) return true; | |
| 2866 | if (!have_comptime_val and try Type.fromInterned(field_ty).comptimeOnlyAdvanced(pt, strat)) return true; | |
| 2865 | 2867 | } |
| 2866 | 2868 | return false; |
| 2867 | 2869 | }, |
| ... | ... | @@ -2880,11 +2882,11 @@ pub fn comptimeOnlyAdvanced(ty: Type, mod: *Module, strat: ResolveStrat) SemaErr |
| 2880 | 2882 | union_type.flagsPtr(ip).requires_comptime = .wip; |
| 2881 | 2883 | errdefer union_type.flagsPtr(ip).requires_comptime = .unknown; |
| 2882 | 2884 | |
| 2883 | try ty.resolveFields(mod); | |
| 2885 | try ty.resolveFields(pt); | |
| 2884 | 2886 | |
| 2885 | 2887 | for (0..union_type.field_types.len) |field_idx| { |
| 2886 | 2888 | const field_ty = union_type.field_types.get(ip)[field_idx]; |
| 2887 | if (try Type.fromInterned(field_ty).comptimeOnlyAdvanced(mod, strat)) { | |
| 2889 | if (try Type.fromInterned(field_ty).comptimeOnlyAdvanced(pt, strat)) { | |
| 2888 | 2890 | union_type.flagsPtr(ip).requires_comptime = .yes; |
| 2889 | 2891 | return true; |
| 2890 | 2892 | } |
| ... | ... | @@ -2898,7 +2900,7 @@ pub fn comptimeOnlyAdvanced(ty: Type, mod: *Module, strat: ResolveStrat) SemaErr |
| 2898 | 2900 | |
| 2899 | 2901 | .opaque_type => false, |
| 2900 | 2902 | |
| 2901 | .enum_type => return Type.fromInterned(ip.loadEnumType(ty.toIntern()).tag_ty).comptimeOnlyAdvanced(mod, strat), | |
| 2903 | .enum_type => return Type.fromInterned(ip.loadEnumType(ty.toIntern()).tag_ty).comptimeOnlyAdvanced(pt, strat), | |
| 2902 | 2904 | |
| 2903 | 2905 | // values, not types |
| 2904 | 2906 | .undef, |
| ... | ... | @@ -2930,10 +2932,10 @@ pub fn isVector(ty: Type, mod: *const Module) bool { |
| 2930 | 2932 | } |
| 2931 | 2933 | |
| 2932 | 2934 | /// Returns 0 if not a vector, otherwise returns @bitSizeOf(Element) * vector_len. |
| 2933 | pub fn totalVectorBits(ty: Type, zcu: *Zcu) u64 { | |
| 2934 | if (!ty.isVector(zcu)) return 0; | |
| 2935 | const v = zcu.intern_pool.indexToKey(ty.toIntern()).vector_type; | |
| 2936 | return v.len * Type.fromInterned(v.child).bitSize(zcu); | |
| 2935 | pub fn totalVectorBits(ty: Type, pt: Zcu.PerThread) u64 { | |
| 2936 | if (!ty.isVector(pt.zcu)) return 0; | |
| 2937 | const v = pt.zcu.intern_pool.indexToKey(ty.toIntern()).vector_type; | |
| 2938 | return v.len * Type.fromInterned(v.child).bitSize(pt); | |
| 2937 | 2939 | } |
| 2938 | 2940 | |
| 2939 | 2941 | pub fn isArrayOrVector(ty: Type, mod: *const Module) bool { |
| ... | ... | @@ -3013,23 +3015,25 @@ pub fn getNamespace(ty: Type, zcu: *Zcu) ?InternPool.OptionalNamespaceIndex { |
| 3013 | 3015 | } |
| 3014 | 3016 | |
| 3015 | 3017 | // Works for vectors and vectors of integers. |
| 3016 | pub fn minInt(ty: Type, mod: *Module, dest_ty: Type) !Value { | |
| 3017 | const scalar = try minIntScalar(ty.scalarType(mod), mod, dest_ty.scalarType(mod)); | |
| 3018 | return if (ty.zigTypeTag(mod) == .Vector) Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3018 | pub fn minInt(ty: Type, pt: Zcu.PerThread, dest_ty: Type) !Value { | |
| 3019 | const mod = pt.zcu; | |
| 3020 | const scalar = try minIntScalar(ty.scalarType(mod), pt, dest_ty.scalarType(mod)); | |
| 3021 | return if (ty.zigTypeTag(mod) == .Vector) Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3019 | 3022 | .ty = dest_ty.toIntern(), |
| 3020 | 3023 | .storage = .{ .repeated_elem = scalar.toIntern() }, |
| 3021 | } }))) else scalar; | |
| 3024 | } })) else scalar; | |
| 3022 | 3025 | } |
| 3023 | 3026 | |
| 3024 | 3027 | /// Asserts that the type is an integer. |
| 3025 | pub fn minIntScalar(ty: Type, mod: *Module, dest_ty: Type) !Value { | |
| 3028 | pub fn minIntScalar(ty: Type, pt: Zcu.PerThread, dest_ty: Type) !Value { | |
| 3029 | const mod = pt.zcu; | |
| 3026 | 3030 | const info = ty.intInfo(mod); |
| 3027 | if (info.signedness == .unsigned) return mod.intValue(dest_ty, 0); | |
| 3028 | if (info.bits == 0) return mod.intValue(dest_ty, -1); | |
| 3031 | if (info.signedness == .unsigned) return pt.intValue(dest_ty, 0); | |
| 3032 | if (info.bits == 0) return pt.intValue(dest_ty, -1); | |
| 3029 | 3033 | |
| 3030 | 3034 | if (std.math.cast(u6, info.bits - 1)) |shift| { |
| 3031 | 3035 | const n = @as(i64, std.math.minInt(i64)) >> (63 - shift); |
| 3032 | return mod.intValue(dest_ty, n); | |
| 3036 | return pt.intValue(dest_ty, n); | |
| 3033 | 3037 | } |
| 3034 | 3038 | |
| 3035 | 3039 | var res = try std.math.big.int.Managed.init(mod.gpa); |
| ... | ... | @@ -3037,31 +3041,32 @@ pub fn minIntScalar(ty: Type, mod: *Module, dest_ty: Type) !Value { |
| 3037 | 3041 | |
| 3038 | 3042 | try res.setTwosCompIntLimit(.min, info.signedness, info.bits); |
| 3039 | 3043 | |
| 3040 | return mod.intValue_big(dest_ty, res.toConst()); | |
| 3044 | return pt.intValue_big(dest_ty, res.toConst()); | |
| 3041 | 3045 | } |
| 3042 | 3046 | |
| 3043 | 3047 | // Works for vectors and vectors of integers. |
| 3044 | 3048 | /// The returned Value will have type dest_ty. |
| 3045 | pub fn maxInt(ty: Type, mod: *Module, dest_ty: Type) !Value { | |
| 3046 | const scalar = try maxIntScalar(ty.scalarType(mod), mod, dest_ty.scalarType(mod)); | |
| 3047 | return if (ty.zigTypeTag(mod) == .Vector) Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3049 | pub fn maxInt(ty: Type, pt: Zcu.PerThread, dest_ty: Type) !Value { | |
| 3050 | const mod = pt.zcu; | |
| 3051 | const scalar = try maxIntScalar(ty.scalarType(mod), pt, dest_ty.scalarType(mod)); | |
| 3052 | return if (ty.zigTypeTag(mod) == .Vector) Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3048 | 3053 | .ty = dest_ty.toIntern(), |
| 3049 | 3054 | .storage = .{ .repeated_elem = scalar.toIntern() }, |
| 3050 | } }))) else scalar; | |
| 3055 | } })) else scalar; | |
| 3051 | 3056 | } |
| 3052 | 3057 | |
| 3053 | 3058 | /// The returned Value will have type dest_ty. |
| 3054 | pub fn maxIntScalar(ty: Type, mod: *Module, dest_ty: Type) !Value { | |
| 3055 | const info = ty.intInfo(mod); | |
| 3059 | pub fn maxIntScalar(ty: Type, pt: Zcu.PerThread, dest_ty: Type) !Value { | |
| 3060 | const info = ty.intInfo(pt.zcu); | |
| 3056 | 3061 | |
| 3057 | 3062 | switch (info.bits) { |
| 3058 | 3063 | 0 => return switch (info.signedness) { |
| 3059 | .signed => try mod.intValue(dest_ty, -1), | |
| 3060 | .unsigned => try mod.intValue(dest_ty, 0), | |
| 3064 | .signed => try pt.intValue(dest_ty, -1), | |
| 3065 | .unsigned => try pt.intValue(dest_ty, 0), | |
| 3061 | 3066 | }, |
| 3062 | 3067 | 1 => return switch (info.signedness) { |
| 3063 | .signed => try mod.intValue(dest_ty, 0), | |
| 3064 | .unsigned => try mod.intValue(dest_ty, 1), | |
| 3068 | .signed => try pt.intValue(dest_ty, 0), | |
| 3069 | .unsigned => try pt.intValue(dest_ty, 1), | |
| 3065 | 3070 | }, |
| 3066 | 3071 | else => {}, |
| 3067 | 3072 | } |
| ... | ... | @@ -3069,20 +3074,20 @@ pub fn maxIntScalar(ty: Type, mod: *Module, dest_ty: Type) !Value { |
| 3069 | 3074 | if (std.math.cast(u6, info.bits - 1)) |shift| switch (info.signedness) { |
| 3070 | 3075 | .signed => { |
| 3071 | 3076 | const n = @as(i64, std.math.maxInt(i64)) >> (63 - shift); |
| 3072 | return mod.intValue(dest_ty, n); | |
| 3077 | return pt.intValue(dest_ty, n); | |
| 3073 | 3078 | }, |
| 3074 | 3079 | .unsigned => { |
| 3075 | 3080 | const n = @as(u64, std.math.maxInt(u64)) >> (63 - shift); |
| 3076 | return mod.intValue(dest_ty, n); | |
| 3081 | return pt.intValue(dest_ty, n); | |
| 3077 | 3082 | }, |
| 3078 | 3083 | }; |
| 3079 | 3084 | |
| 3080 | var res = try std.math.big.int.Managed.init(mod.gpa); | |
| 3085 | var res = try std.math.big.int.Managed.init(pt.zcu.gpa); | |
| 3081 | 3086 | defer res.deinit(); |
| 3082 | 3087 | |
| 3083 | 3088 | try res.setTwosCompIntLimit(.max, info.signedness, info.bits); |
| 3084 | 3089 | |
| 3085 | return mod.intValue_big(dest_ty, res.toConst()); | |
| 3090 | return pt.intValue_big(dest_ty, res.toConst()); | |
| 3086 | 3091 | } |
| 3087 | 3092 | |
| 3088 | 3093 | /// Asserts the type is an enum or a union. |
| ... | ... | @@ -3188,26 +3193,26 @@ pub fn structFieldType(ty: Type, index: usize, mod: *Module) Type { |
| 3188 | 3193 | }; |
| 3189 | 3194 | } |
| 3190 | 3195 | |
| 3191 | pub fn structFieldAlign(ty: Type, index: usize, zcu: *Zcu) Alignment { | |
| 3192 | return ty.structFieldAlignAdvanced(index, zcu, .normal) catch unreachable; | |
| 3196 | pub fn structFieldAlign(ty: Type, index: usize, pt: Zcu.PerThread) Alignment { | |
| 3197 | return ty.structFieldAlignAdvanced(index, pt, .normal) catch unreachable; | |
| 3193 | 3198 | } |
| 3194 | 3199 | |
| 3195 | pub fn structFieldAlignAdvanced(ty: Type, index: usize, zcu: *Zcu, strat: ResolveStrat) !Alignment { | |
| 3196 | const ip = &zcu.intern_pool; | |
| 3200 | pub fn structFieldAlignAdvanced(ty: Type, index: usize, pt: Zcu.PerThread, strat: ResolveStrat) !Alignment { | |
| 3201 | const ip = &pt.zcu.intern_pool; | |
| 3197 | 3202 | switch (ip.indexToKey(ty.toIntern())) { |
| 3198 | 3203 | .struct_type => { |
| 3199 | 3204 | const struct_type = ip.loadStructType(ty.toIntern()); |
| 3200 | 3205 | assert(struct_type.layout != .@"packed"); |
| 3201 | 3206 | const explicit_align = struct_type.fieldAlign(ip, index); |
| 3202 | 3207 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[index]); |
| 3203 | return zcu.structFieldAlignmentAdvanced(explicit_align, field_ty, struct_type.layout, strat); | |
| 3208 | return pt.structFieldAlignmentAdvanced(explicit_align, field_ty, struct_type.layout, strat); | |
| 3204 | 3209 | }, |
| 3205 | 3210 | .anon_struct_type => |anon_struct| { |
| 3206 | return (try Type.fromInterned(anon_struct.types.get(ip)[index]).abiAlignmentAdvanced(zcu, strat.toLazy())).scalar; | |
| 3211 | return (try Type.fromInterned(anon_struct.types.get(ip)[index]).abiAlignmentAdvanced(pt, strat.toLazy())).scalar; | |
| 3207 | 3212 | }, |
| 3208 | 3213 | .union_type => { |
| 3209 | 3214 | const union_obj = ip.loadUnionType(ty.toIntern()); |
| 3210 | return zcu.unionFieldNormalAlignmentAdvanced(union_obj, @intCast(index), strat); | |
| 3215 | return pt.unionFieldNormalAlignmentAdvanced(union_obj, @intCast(index), strat); | |
| 3211 | 3216 | }, |
| 3212 | 3217 | else => unreachable, |
| 3213 | 3218 | } |
| ... | ... | @@ -3233,7 +3238,8 @@ pub fn structFieldDefaultValue(ty: Type, index: usize, mod: *Module) Value { |
| 3233 | 3238 | } |
| 3234 | 3239 | } |
| 3235 | 3240 | |
| 3236 | pub fn structFieldValueComptime(ty: Type, mod: *Module, index: usize) !?Value { | |
| 3241 | pub fn structFieldValueComptime(ty: Type, pt: Zcu.PerThread, index: usize) !?Value { | |
| 3242 | const mod = pt.zcu; | |
| 3237 | 3243 | const ip = &mod.intern_pool; |
| 3238 | 3244 | switch (ip.indexToKey(ty.toIntern())) { |
| 3239 | 3245 | .struct_type => { |
| ... | ... | @@ -3242,13 +3248,13 @@ pub fn structFieldValueComptime(ty: Type, mod: *Module, index: usize) !?Value { |
| 3242 | 3248 | assert(struct_type.haveFieldInits(ip)); |
| 3243 | 3249 | return Value.fromInterned(struct_type.field_inits.get(ip)[index]); |
| 3244 | 3250 | } else { |
| 3245 | return Type.fromInterned(struct_type.field_types.get(ip)[index]).onePossibleValue(mod); | |
| 3251 | return Type.fromInterned(struct_type.field_types.get(ip)[index]).onePossibleValue(pt); | |
| 3246 | 3252 | } |
| 3247 | 3253 | }, |
| 3248 | 3254 | .anon_struct_type => |tuple| { |
| 3249 | 3255 | const val = tuple.values.get(ip)[index]; |
| 3250 | 3256 | if (val == .none) { |
| 3251 | return Type.fromInterned(tuple.types.get(ip)[index]).onePossibleValue(mod); | |
| 3257 | return Type.fromInterned(tuple.types.get(ip)[index]).onePossibleValue(pt); | |
| 3252 | 3258 | } else { |
| 3253 | 3259 | return Value.fromInterned(val); |
| 3254 | 3260 | } |
| ... | ... | @@ -3272,7 +3278,8 @@ pub const FieldOffset = struct { |
| 3272 | 3278 | }; |
| 3273 | 3279 | |
| 3274 | 3280 | /// Supports structs and unions. |
| 3275 | pub fn structFieldOffset(ty: Type, index: usize, mod: *Module) u64 { | |
| 3281 | pub fn structFieldOffset(ty: Type, index: usize, pt: Zcu.PerThread) u64 { | |
| 3282 | const mod = pt.zcu; | |
| 3276 | 3283 | const ip = &mod.intern_pool; |
| 3277 | 3284 | switch (ip.indexToKey(ty.toIntern())) { |
| 3278 | 3285 | .struct_type => { |
| ... | ... | @@ -3287,17 +3294,17 @@ pub fn structFieldOffset(ty: Type, index: usize, mod: *Module) u64 { |
| 3287 | 3294 | var big_align: Alignment = .none; |
| 3288 | 3295 | |
| 3289 | 3296 | for (tuple.types.get(ip), tuple.values.get(ip), 0..) |field_ty, field_val, i| { |
| 3290 | if (field_val != .none or !Type.fromInterned(field_ty).hasRuntimeBits(mod)) { | |
| 3297 | if (field_val != .none or !Type.fromInterned(field_ty).hasRuntimeBits(pt)) { | |
| 3291 | 3298 | // comptime field |
| 3292 | 3299 | if (i == index) return offset; |
| 3293 | 3300 | continue; |
| 3294 | 3301 | } |
| 3295 | 3302 | |
| 3296 | const field_align = Type.fromInterned(field_ty).abiAlignment(mod); | |
| 3303 | const field_align = Type.fromInterned(field_ty).abiAlignment(pt); | |
| 3297 | 3304 | big_align = big_align.max(field_align); |
| 3298 | 3305 | offset = field_align.forward(offset); |
| 3299 | 3306 | if (i == index) return offset; |
| 3300 | offset += Type.fromInterned(field_ty).abiSize(mod); | |
| 3307 | offset += Type.fromInterned(field_ty).abiSize(pt); | |
| 3301 | 3308 | } |
| 3302 | 3309 | offset = big_align.max(.@"1").forward(offset); |
| 3303 | 3310 | return offset; |
| ... | ... | @@ -3307,7 +3314,7 @@ pub fn structFieldOffset(ty: Type, index: usize, mod: *Module) u64 { |
| 3307 | 3314 | const union_type = ip.loadUnionType(ty.toIntern()); |
| 3308 | 3315 | if (!union_type.hasTag(ip)) |
| 3309 | 3316 | return 0; |
| 3310 | const layout = mod.getUnionLayout(union_type); | |
| 3317 | const layout = pt.getUnionLayout(union_type); | |
| 3311 | 3318 | if (layout.tag_align.compare(.gte, layout.payload_align)) { |
| 3312 | 3319 | // {Tag, Payload} |
| 3313 | 3320 | return layout.payload_align.forward(layout.tag_size); |
| ... | ... | @@ -3421,12 +3428,13 @@ pub fn optEuBaseType(ty: Type, mod: *Module) Type { |
| 3421 | 3428 | }; |
| 3422 | 3429 | } |
| 3423 | 3430 | |
| 3424 | pub fn toUnsigned(ty: Type, mod: *Module) !Type { | |
| 3431 | pub fn toUnsigned(ty: Type, pt: Zcu.PerThread) !Type { | |
| 3432 | const mod = pt.zcu; | |
| 3425 | 3433 | return switch (ty.zigTypeTag(mod)) { |
| 3426 | .Int => mod.intType(.unsigned, ty.intInfo(mod).bits), | |
| 3427 | .Vector => try mod.vectorType(.{ | |
| 3434 | .Int => pt.intType(.unsigned, ty.intInfo(mod).bits), | |
| 3435 | .Vector => try pt.vectorType(.{ | |
| 3428 | 3436 | .len = ty.vectorLen(mod), |
| 3429 | .child = (try ty.childType(mod).toUnsigned(mod)).toIntern(), | |
| 3437 | .child = (try ty.childType(mod).toUnsigned(pt)).toIntern(), | |
| 3430 | 3438 | }), |
| 3431 | 3439 | else => unreachable, |
| 3432 | 3440 | }; |
| ... | ... | @@ -3492,7 +3500,7 @@ pub fn arrayBase(ty: Type, zcu: *const Zcu) struct { Type, u64 } { |
| 3492 | 3500 | return .{ cur_ty, cur_len }; |
| 3493 | 3501 | } |
| 3494 | 3502 | |
| 3495 | pub fn packedStructFieldPtrInfo(struct_ty: Type, parent_ptr_ty: Type, field_idx: u32, zcu: *Zcu) union(enum) { | |
| 3503 | pub fn packedStructFieldPtrInfo(struct_ty: Type, parent_ptr_ty: Type, field_idx: u32, pt: Zcu.PerThread) union(enum) { | |
| 3496 | 3504 | /// The result is a bit-pointer with the same value and a new packed offset. |
| 3497 | 3505 | bit_ptr: InternPool.Key.PtrType.PackedOffset, |
| 3498 | 3506 | /// The result is a standard pointer. |
| ... | ... | @@ -3505,6 +3513,7 @@ pub fn packedStructFieldPtrInfo(struct_ty: Type, parent_ptr_ty: Type, field_idx: |
| 3505 | 3513 | } { |
| 3506 | 3514 | comptime assert(Type.packed_struct_layout_version == 2); |
| 3507 | 3515 | |
| 3516 | const zcu = pt.zcu; | |
| 3508 | 3517 | const parent_ptr_info = parent_ptr_ty.ptrInfo(zcu); |
| 3509 | 3518 | const field_ty = struct_ty.structFieldType(field_idx, zcu); |
| 3510 | 3519 | |
| ... | ... | @@ -3515,7 +3524,7 @@ pub fn packedStructFieldPtrInfo(struct_ty: Type, parent_ptr_ty: Type, field_idx: |
| 3515 | 3524 | if (i == field_idx) { |
| 3516 | 3525 | bit_offset = running_bits; |
| 3517 | 3526 | } |
| 3518 | running_bits += @intCast(f_ty.bitSize(zcu)); | |
| 3527 | running_bits += @intCast(f_ty.bitSize(pt)); | |
| 3519 | 3528 | } |
| 3520 | 3529 | |
| 3521 | 3530 | const res_host_size: u16, const res_bit_offset: u16 = if (parent_ptr_info.packed_offset.host_size != 0) |
| ... | ... | @@ -3532,9 +3541,9 @@ pub fn packedStructFieldPtrInfo(struct_ty: Type, parent_ptr_ty: Type, field_idx: |
| 3532 | 3541 | // targets before adding the necessary complications to this code. This will not |
| 3533 | 3542 | // cause miscompilations; it only means the field pointer uses bit masking when it |
| 3534 | 3543 | // might not be strictly necessary. |
| 3535 | if (res_bit_offset % 8 == 0 and field_ty.bitSize(zcu) == field_ty.abiSize(zcu) * 8 and zcu.getTarget().cpu.arch.endian() == .little) { | |
| 3544 | if (res_bit_offset % 8 == 0 and field_ty.bitSize(pt) == field_ty.abiSize(pt) * 8 and zcu.getTarget().cpu.arch.endian() == .little) { | |
| 3536 | 3545 | const byte_offset = res_bit_offset / 8; |
| 3537 | const new_align = Alignment.fromLog2Units(@ctz(byte_offset | parent_ptr_ty.ptrAlignment(zcu).toByteUnits().?)); | |
| 3546 | const new_align = Alignment.fromLog2Units(@ctz(byte_offset | parent_ptr_ty.ptrAlignment(pt).toByteUnits().?)); | |
| 3538 | 3547 | return .{ .byte_ptr = .{ |
| 3539 | 3548 | .offset = byte_offset, |
| 3540 | 3549 | .alignment = new_align, |
| ... | ... | @@ -3547,34 +3556,35 @@ pub fn packedStructFieldPtrInfo(struct_ty: Type, parent_ptr_ty: Type, field_idx: |
| 3547 | 3556 | } }; |
| 3548 | 3557 | } |
| 3549 | 3558 | |
| 3550 | pub fn resolveLayout(ty: Type, zcu: *Zcu) SemaError!void { | |
| 3559 | pub fn resolveLayout(ty: Type, pt: Zcu.PerThread) SemaError!void { | |
| 3560 | const zcu = pt.zcu; | |
| 3551 | 3561 | const ip = &zcu.intern_pool; |
| 3552 | 3562 | switch (ip.indexToKey(ty.toIntern())) { |
| 3553 | .simple_type => |simple_type| return resolveSimpleType(simple_type, zcu), | |
| 3563 | .simple_type => |simple_type| return resolveSimpleType(simple_type, pt), | |
| 3554 | 3564 | else => {}, |
| 3555 | 3565 | } |
| 3556 | 3566 | switch (ty.zigTypeTag(zcu)) { |
| 3557 | 3567 | .Struct => switch (ip.indexToKey(ty.toIntern())) { |
| 3558 | 3568 | .anon_struct_type => |anon_struct_type| for (0..anon_struct_type.types.len) |i| { |
| 3559 | 3569 | const field_ty = Type.fromInterned(anon_struct_type.types.get(ip)[i]); |
| 3560 | try field_ty.resolveLayout(zcu); | |
| 3570 | try field_ty.resolveLayout(pt); | |
| 3561 | 3571 | }, |
| 3562 | .struct_type => return ty.resolveStructInner(zcu, .layout), | |
| 3572 | .struct_type => return ty.resolveStructInner(pt, .layout), | |
| 3563 | 3573 | else => unreachable, |
| 3564 | 3574 | }, |
| 3565 | .Union => return ty.resolveUnionInner(zcu, .layout), | |
| 3575 | .Union => return ty.resolveUnionInner(pt, .layout), | |
| 3566 | 3576 | .Array => { |
| 3567 | 3577 | if (ty.arrayLenIncludingSentinel(zcu) == 0) return; |
| 3568 | 3578 | const elem_ty = ty.childType(zcu); |
| 3569 | return elem_ty.resolveLayout(zcu); | |
| 3579 | return elem_ty.resolveLayout(pt); | |
| 3570 | 3580 | }, |
| 3571 | 3581 | .Optional => { |
| 3572 | 3582 | const payload_ty = ty.optionalChild(zcu); |
| 3573 | return payload_ty.resolveLayout(zcu); | |
| 3583 | return payload_ty.resolveLayout(pt); | |
| 3574 | 3584 | }, |
| 3575 | 3585 | .ErrorUnion => { |
| 3576 | 3586 | const payload_ty = ty.errorUnionPayload(zcu); |
| 3577 | return payload_ty.resolveLayout(zcu); | |
| 3587 | return payload_ty.resolveLayout(pt); | |
| 3578 | 3588 | }, |
| 3579 | 3589 | .Fn => { |
| 3580 | 3590 | const info = zcu.typeToFunc(ty).?; |
| ... | ... | @@ -3585,16 +3595,16 @@ pub fn resolveLayout(ty: Type, zcu: *Zcu) SemaError!void { |
| 3585 | 3595 | } |
| 3586 | 3596 | for (0..info.param_types.len) |i| { |
| 3587 | 3597 | const param_ty = info.param_types.get(ip)[i]; |
| 3588 | try Type.fromInterned(param_ty).resolveLayout(zcu); | |
| 3598 | try Type.fromInterned(param_ty).resolveLayout(pt); | |
| 3589 | 3599 | } |
| 3590 | try Type.fromInterned(info.return_type).resolveLayout(zcu); | |
| 3600 | try Type.fromInterned(info.return_type).resolveLayout(pt); | |
| 3591 | 3601 | }, |
| 3592 | 3602 | else => {}, |
| 3593 | 3603 | } |
| 3594 | 3604 | } |
| 3595 | 3605 | |
| 3596 | pub fn resolveFields(ty: Type, zcu: *Zcu) SemaError!void { | |
| 3597 | const ip = &zcu.intern_pool; | |
| 3606 | pub fn resolveFields(ty: Type, pt: Zcu.PerThread) SemaError!void { | |
| 3607 | const ip = &pt.zcu.intern_pool; | |
| 3598 | 3608 | const ty_ip = ty.toIntern(); |
| 3599 | 3609 | |
| 3600 | 3610 | switch (ty_ip) { |
| ... | ... | @@ -3680,22 +3690,23 @@ pub fn resolveFields(ty: Type, zcu: *Zcu) SemaError!void { |
| 3680 | 3690 | .type_struct, |
| 3681 | 3691 | .type_struct_packed, |
| 3682 | 3692 | .type_struct_packed_inits, |
| 3683 | => return ty.resolveStructInner(zcu, .fields), | |
| 3693 | => return ty.resolveStructInner(pt, .fields), | |
| 3684 | 3694 | |
| 3685 | .type_union => return ty.resolveUnionInner(zcu, .fields), | |
| 3695 | .type_union => return ty.resolveUnionInner(pt, .fields), | |
| 3686 | 3696 | |
| 3687 | .simple_type => return resolveSimpleType(ip.indexToKey(ty_ip).simple_type, zcu), | |
| 3697 | .simple_type => return resolveSimpleType(ip.indexToKey(ty_ip).simple_type, pt), | |
| 3688 | 3698 | |
| 3689 | 3699 | else => {}, |
| 3690 | 3700 | }, |
| 3691 | 3701 | } |
| 3692 | 3702 | } |
| 3693 | 3703 | |
| 3694 | pub fn resolveFully(ty: Type, zcu: *Zcu) SemaError!void { | |
| 3704 | pub fn resolveFully(ty: Type, pt: Zcu.PerThread) SemaError!void { | |
| 3705 | const zcu = pt.zcu; | |
| 3695 | 3706 | const ip = &zcu.intern_pool; |
| 3696 | 3707 | |
| 3697 | 3708 | switch (ip.indexToKey(ty.toIntern())) { |
| 3698 | .simple_type => |simple_type| return resolveSimpleType(simple_type, zcu), | |
| 3709 | .simple_type => |simple_type| return resolveSimpleType(simple_type, pt), | |
| 3699 | 3710 | else => {}, |
| 3700 | 3711 | } |
| 3701 | 3712 | |
| ... | ... | @@ -3719,52 +3730,53 @@ pub fn resolveFully(ty: Type, zcu: *Zcu) SemaError!void { |
| 3719 | 3730 | .EnumLiteral, |
| 3720 | 3731 | => {}, |
| 3721 | 3732 | |
| 3722 | .Pointer => return ty.childType(zcu).resolveFully(zcu), | |
| 3723 | .Array => return ty.childType(zcu).resolveFully(zcu), | |
| 3724 | .Optional => return ty.optionalChild(zcu).resolveFully(zcu), | |
| 3725 | .ErrorUnion => return ty.errorUnionPayload(zcu).resolveFully(zcu), | |
| 3733 | .Pointer => return ty.childType(zcu).resolveFully(pt), | |
| 3734 | .Array => return ty.childType(zcu).resolveFully(pt), | |
| 3735 | .Optional => return ty.optionalChild(zcu).resolveFully(pt), | |
| 3736 | .ErrorUnion => return ty.errorUnionPayload(zcu).resolveFully(pt), | |
| 3726 | 3737 | .Fn => { |
| 3727 | 3738 | const info = zcu.typeToFunc(ty).?; |
| 3728 | 3739 | if (info.is_generic) return; |
| 3729 | 3740 | for (0..info.param_types.len) |i| { |
| 3730 | 3741 | const param_ty = info.param_types.get(ip)[i]; |
| 3731 | try Type.fromInterned(param_ty).resolveFully(zcu); | |
| 3742 | try Type.fromInterned(param_ty).resolveFully(pt); | |
| 3732 | 3743 | } |
| 3733 | try Type.fromInterned(info.return_type).resolveFully(zcu); | |
| 3744 | try Type.fromInterned(info.return_type).resolveFully(pt); | |
| 3734 | 3745 | }, |
| 3735 | 3746 | |
| 3736 | 3747 | .Struct => switch (ip.indexToKey(ty.toIntern())) { |
| 3737 | 3748 | .anon_struct_type => |anon_struct_type| for (0..anon_struct_type.types.len) |i| { |
| 3738 | 3749 | const field_ty = Type.fromInterned(anon_struct_type.types.get(ip)[i]); |
| 3739 | try field_ty.resolveFully(zcu); | |
| 3750 | try field_ty.resolveFully(pt); | |
| 3740 | 3751 | }, |
| 3741 | .struct_type => return ty.resolveStructInner(zcu, .full), | |
| 3752 | .struct_type => return ty.resolveStructInner(pt, .full), | |
| 3742 | 3753 | else => unreachable, |
| 3743 | 3754 | }, |
| 3744 | .Union => return ty.resolveUnionInner(zcu, .full), | |
| 3755 | .Union => return ty.resolveUnionInner(pt, .full), | |
| 3745 | 3756 | } |
| 3746 | 3757 | } |
| 3747 | 3758 | |
| 3748 | pub fn resolveStructFieldInits(ty: Type, zcu: *Zcu) SemaError!void { | |
| 3759 | pub fn resolveStructFieldInits(ty: Type, pt: Zcu.PerThread) SemaError!void { | |
| 3749 | 3760 | // TODO: stop calling this for tuples! |
| 3750 | _ = zcu.typeToStruct(ty) orelse return; | |
| 3751 | return ty.resolveStructInner(zcu, .inits); | |
| 3761 | _ = pt.zcu.typeToStruct(ty) orelse return; | |
| 3762 | return ty.resolveStructInner(pt, .inits); | |
| 3752 | 3763 | } |
| 3753 | 3764 | |
| 3754 | pub fn resolveStructAlignment(ty: Type, zcu: *Zcu) SemaError!void { | |
| 3755 | return ty.resolveStructInner(zcu, .alignment); | |
| 3765 | pub fn resolveStructAlignment(ty: Type, pt: Zcu.PerThread) SemaError!void { | |
| 3766 | return ty.resolveStructInner(pt, .alignment); | |
| 3756 | 3767 | } |
| 3757 | 3768 | |
| 3758 | pub fn resolveUnionAlignment(ty: Type, zcu: *Zcu) SemaError!void { | |
| 3759 | return ty.resolveUnionInner(zcu, .alignment); | |
| 3769 | pub fn resolveUnionAlignment(ty: Type, pt: Zcu.PerThread) SemaError!void { | |
| 3770 | return ty.resolveUnionInner(pt, .alignment); | |
| 3760 | 3771 | } |
| 3761 | 3772 | |
| 3762 | 3773 | /// `ty` must be a struct. |
| 3763 | 3774 | fn resolveStructInner( |
| 3764 | 3775 | ty: Type, |
| 3765 | zcu: *Zcu, | |
| 3776 | pt: Zcu.PerThread, | |
| 3766 | 3777 | resolution: enum { fields, inits, alignment, layout, full }, |
| 3767 | 3778 | ) SemaError!void { |
| 3779 | const zcu = pt.zcu; | |
| 3768 | 3780 | const gpa = zcu.gpa; |
| 3769 | 3781 | |
| 3770 | 3782 | const struct_obj = zcu.typeToStruct(ty).?; |
| ... | ... | @@ -3777,7 +3789,7 @@ fn resolveStructInner( |
| 3777 | 3789 | defer comptime_err_ret_trace.deinit(); |
| 3778 | 3790 | |
| 3779 | 3791 | var sema: Sema = .{ |
| 3780 | .mod = zcu, | |
| 3792 | .pt = pt, | |
| 3781 | 3793 | .gpa = gpa, |
| 3782 | 3794 | .arena = analysis_arena.allocator(), |
| 3783 | 3795 | .code = undefined, // This ZIR will not be used. |
| ... | ... | @@ -3804,9 +3816,10 @@ fn resolveStructInner( |
| 3804 | 3816 | /// `ty` must be a union. |
| 3805 | 3817 | fn resolveUnionInner( |
| 3806 | 3818 | ty: Type, |
| 3807 | zcu: *Zcu, | |
| 3819 | pt: Zcu.PerThread, | |
| 3808 | 3820 | resolution: enum { fields, alignment, layout, full }, |
| 3809 | 3821 | ) SemaError!void { |
| 3822 | const zcu = pt.zcu; | |
| 3810 | 3823 | const gpa = zcu.gpa; |
| 3811 | 3824 | |
| 3812 | 3825 | const union_obj = zcu.typeToUnion(ty).?; |
| ... | ... | @@ -3819,7 +3832,7 @@ fn resolveUnionInner( |
| 3819 | 3832 | defer comptime_err_ret_trace.deinit(); |
| 3820 | 3833 | |
| 3821 | 3834 | var sema: Sema = .{ |
| 3822 | .mod = zcu, | |
| 3835 | .pt = pt, | |
| 3823 | 3836 | .gpa = gpa, |
| 3824 | 3837 | .arena = analysis_arena.allocator(), |
| 3825 | 3838 | .code = undefined, // This ZIR will not be used. |
| ... | ... | @@ -3845,7 +3858,7 @@ fn resolveUnionInner( |
| 3845 | 3858 | /// Fully resolves a simple type. This is usually a nop, but for builtin types with |
| 3846 | 3859 | /// special InternPool indices (such as std.builtin.Type) it will analyze and fully |
| 3847 | 3860 | /// resolve the type. |
| 3848 | fn resolveSimpleType(simple_type: InternPool.SimpleType, zcu: *Zcu) Allocator.Error!void { | |
| 3861 | fn resolveSimpleType(simple_type: InternPool.SimpleType, pt: Zcu.PerThread) Allocator.Error!void { | |
| 3849 | 3862 | const builtin_type_name: []const u8 = switch (simple_type) { |
| 3850 | 3863 | .atomic_order => "AtomicOrder", |
| 3851 | 3864 | .atomic_rmw_op => "AtomicRmwOp", |
| ... | ... | @@ -3861,7 +3874,7 @@ fn resolveSimpleType(simple_type: InternPool.SimpleType, zcu: *Zcu) Allocator.Er |
| 3861 | 3874 | else => return, |
| 3862 | 3875 | }; |
| 3863 | 3876 | // This will fully resolve the type. |
| 3864 | _ = try zcu.getBuiltinType(builtin_type_name); | |
| 3877 | _ = try pt.getBuiltinType(builtin_type_name); | |
| 3865 | 3878 | } |
| 3866 | 3879 | |
| 3867 | 3880 | /// Returns the type of a pointer to an element. |
| ... | ... | @@ -3874,7 +3887,8 @@ fn resolveSimpleType(simple_type: InternPool.SimpleType, zcu: *Zcu) Allocator.Er |
| 3874 | 3887 | /// Handles const-ness and address spaces in particular. |
| 3875 | 3888 | /// This code is duplicated in `Sema.analyzePtrArithmetic`. |
| 3876 | 3889 | /// May perform type resolution and return a transitive `error.AnalysisFail`. |
| 3877 | pub fn elemPtrType(ptr_ty: Type, offset: ?usize, zcu: *Zcu) !Type { | |
| 3890 | pub fn elemPtrType(ptr_ty: Type, offset: ?usize, pt: Zcu.PerThread) !Type { | |
| 3891 | const zcu = pt.zcu; | |
| 3878 | 3892 | const ptr_info = ptr_ty.ptrInfo(zcu); |
| 3879 | 3893 | const elem_ty = ptr_ty.elemType2(zcu); |
| 3880 | 3894 | const is_allowzero = ptr_info.flags.is_allowzero and (offset orelse 0) == 0; |
| ... | ... | @@ -3887,14 +3901,14 @@ pub fn elemPtrType(ptr_ty: Type, offset: ?usize, zcu: *Zcu) !Type { |
| 3887 | 3901 | alignment: Alignment = .none, |
| 3888 | 3902 | vector_index: VI = .none, |
| 3889 | 3903 | } = if (parent_ty.isVector(zcu) and ptr_info.flags.size == .One) blk: { |
| 3890 | const elem_bits = elem_ty.bitSize(zcu); | |
| 3904 | const elem_bits = elem_ty.bitSize(pt); | |
| 3891 | 3905 | if (elem_bits == 0) break :blk .{}; |
| 3892 | 3906 | const is_packed = elem_bits < 8 or !std.math.isPowerOfTwo(elem_bits); |
| 3893 | 3907 | if (!is_packed) break :blk .{}; |
| 3894 | 3908 | |
| 3895 | 3909 | break :blk .{ |
| 3896 | 3910 | .host_size = @intCast(parent_ty.arrayLen(zcu)), |
| 3897 | .alignment = parent_ty.abiAlignment(zcu), | |
| 3911 | .alignment = parent_ty.abiAlignment(pt), | |
| 3898 | 3912 | .vector_index = if (offset) |some| @enumFromInt(some) else .runtime, |
| 3899 | 3913 | }; |
| 3900 | 3914 | } else .{}; |
| ... | ... | @@ -3908,7 +3922,7 @@ pub fn elemPtrType(ptr_ty: Type, offset: ?usize, zcu: *Zcu) !Type { |
| 3908 | 3922 | } |
| 3909 | 3923 | // If the addend is not a comptime-known value we can still count on |
| 3910 | 3924 | // it being a multiple of the type size. |
| 3911 | const elem_size = (try elem_ty.abiSizeAdvanced(zcu, .sema)).scalar; | |
| 3925 | const elem_size = (try elem_ty.abiSizeAdvanced(pt, .sema)).scalar; | |
| 3912 | 3926 | const addend = if (offset) |off| elem_size * off else elem_size; |
| 3913 | 3927 | |
| 3914 | 3928 | // The resulting pointer is aligned to the lcd between the offset (an |
| ... | ... | @@ -3921,7 +3935,7 @@ pub fn elemPtrType(ptr_ty: Type, offset: ?usize, zcu: *Zcu) !Type { |
| 3921 | 3935 | assert(new_align != .none); |
| 3922 | 3936 | break :a new_align; |
| 3923 | 3937 | }; |
| 3924 | return zcu.ptrTypeSema(.{ | |
| 3938 | return pt.ptrTypeSema(.{ | |
| 3925 | 3939 | .child = elem_ty.toIntern(), |
| 3926 | 3940 | .flags = .{ |
| 3927 | 3941 | .alignment = alignment, |
| ... | ... | @@ -3944,6 +3958,7 @@ pub const @"u16": Type = .{ .ip_index = .u16_type }; |
| 3944 | 3958 | pub const @"u29": Type = .{ .ip_index = .u29_type }; |
| 3945 | 3959 | pub const @"u32": Type = .{ .ip_index = .u32_type }; |
| 3946 | 3960 | pub const @"u64": Type = .{ .ip_index = .u64_type }; |
| 3961 | pub const @"u80": Type = .{ .ip_index = .u80_type }; | |
| 3947 | 3962 | pub const @"u128": Type = .{ .ip_index = .u128_type }; |
| 3948 | 3963 | |
| 3949 | 3964 | pub const @"i8": Type = .{ .ip_index = .i8_type }; |
src/Value.zig+1163-1084| ... | ... | @@ -40,10 +40,10 @@ pub fn fmtDebug(val: Value) std.fmt.Formatter(dump) { |
| 40 | 40 | return .{ .data = val }; |
| 41 | 41 | } |
| 42 | 42 | |
| 43 | pub fn fmtValue(val: Value, mod: *Module, opt_sema: ?*Sema) std.fmt.Formatter(print_value.format) { | |
| 43 | pub fn fmtValue(val: Value, pt: Zcu.PerThread, opt_sema: ?*Sema) std.fmt.Formatter(print_value.format) { | |
| 44 | 44 | return .{ .data = .{ |
| 45 | 45 | .val = val, |
| 46 | .mod = mod, | |
| 46 | .pt = pt, | |
| 47 | 47 | .opt_sema = opt_sema, |
| 48 | 48 | .depth = 3, |
| 49 | 49 | } }; |
| ... | ... | @@ -55,15 +55,16 @@ pub fn fmtValueFull(ctx: print_value.FormatContext) std.fmt.Formatter(print_valu |
| 55 | 55 | |
| 56 | 56 | /// Converts `val` to a null-terminated string stored in the InternPool. |
| 57 | 57 | /// Asserts `val` is an array of `u8` |
| 58 | pub fn toIpString(val: Value, ty: Type, mod: *Module) !InternPool.NullTerminatedString { | |
| 58 | pub fn toIpString(val: Value, ty: Type, pt: Zcu.PerThread) !InternPool.NullTerminatedString { | |
| 59 | const mod = pt.zcu; | |
| 59 | 60 | assert(ty.zigTypeTag(mod) == .Array); |
| 60 | 61 | assert(ty.childType(mod).toIntern() == .u8_type); |
| 61 | 62 | const ip = &mod.intern_pool; |
| 62 | 63 | switch (mod.intern_pool.indexToKey(val.toIntern()).aggregate.storage) { |
| 63 | 64 | .bytes => |bytes| return bytes.toNullTerminatedString(ty.arrayLen(mod), ip), |
| 64 | .elems => return arrayToIpString(val, ty.arrayLen(mod), mod), | |
| 65 | .elems => return arrayToIpString(val, ty.arrayLen(mod), pt), | |
| 65 | 66 | .repeated_elem => |elem| { |
| 66 | const byte: u8 = @intCast(Value.fromInterned(elem).toUnsignedInt(mod)); | |
| 67 | const byte: u8 = @intCast(Value.fromInterned(elem).toUnsignedInt(pt)); | |
| 67 | 68 | const len: usize = @intCast(ty.arrayLen(mod)); |
| 68 | 69 | try ip.string_bytes.appendNTimes(mod.gpa, byte, len); |
| 69 | 70 | return ip.getOrPutTrailingString(mod.gpa, len, .no_embedded_nulls); |
| ... | ... | @@ -73,16 +74,17 @@ pub fn toIpString(val: Value, ty: Type, mod: *Module) !InternPool.NullTerminated |
| 73 | 74 | |
| 74 | 75 | /// Asserts that the value is representable as an array of bytes. |
| 75 | 76 | /// Copies the value into a freshly allocated slice of memory, which is owned by the caller. |
| 76 | pub fn toAllocatedBytes(val: Value, ty: Type, allocator: Allocator, mod: *Module) ![]u8 { | |
| 77 | pub fn toAllocatedBytes(val: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) ![]u8 { | |
| 78 | const mod = pt.zcu; | |
| 77 | 79 | const ip = &mod.intern_pool; |
| 78 | 80 | return switch (ip.indexToKey(val.toIntern())) { |
| 79 | 81 | .enum_literal => |enum_literal| allocator.dupe(u8, enum_literal.toSlice(ip)), |
| 80 | .slice => |slice| try arrayToAllocatedBytes(val, Value.fromInterned(slice.len).toUnsignedInt(mod), allocator, mod), | |
| 82 | .slice => |slice| try arrayToAllocatedBytes(val, Value.fromInterned(slice.len).toUnsignedInt(pt), allocator, pt), | |
| 81 | 83 | .aggregate => |aggregate| switch (aggregate.storage) { |
| 82 | 84 | .bytes => |bytes| try allocator.dupe(u8, bytes.toSlice(ty.arrayLenIncludingSentinel(mod), ip)), |
| 83 | .elems => try arrayToAllocatedBytes(val, ty.arrayLen(mod), allocator, mod), | |
| 85 | .elems => try arrayToAllocatedBytes(val, ty.arrayLen(mod), allocator, pt), | |
| 84 | 86 | .repeated_elem => |elem| { |
| 85 | const byte: u8 = @intCast(Value.fromInterned(elem).toUnsignedInt(mod)); | |
| 87 | const byte: u8 = @intCast(Value.fromInterned(elem).toUnsignedInt(pt)); | |
| 86 | 88 | const result = try allocator.alloc(u8, @intCast(ty.arrayLen(mod))); |
| 87 | 89 | @memset(result, byte); |
| 88 | 90 | return result; |
| ... | ... | @@ -92,16 +94,17 @@ pub fn toAllocatedBytes(val: Value, ty: Type, allocator: Allocator, mod: *Module |
| 92 | 94 | }; |
| 93 | 95 | } |
| 94 | 96 | |
| 95 | fn arrayToAllocatedBytes(val: Value, len: u64, allocator: Allocator, mod: *Module) ![]u8 { | |
| 97 | fn arrayToAllocatedBytes(val: Value, len: u64, allocator: Allocator, pt: Zcu.PerThread) ![]u8 { | |
| 96 | 98 | const result = try allocator.alloc(u8, @intCast(len)); |
| 97 | 99 | for (result, 0..) |*elem, i| { |
| 98 | const elem_val = try val.elemValue(mod, i); | |
| 99 | elem.* = @intCast(elem_val.toUnsignedInt(mod)); | |
| 100 | const elem_val = try val.elemValue(pt, i); | |
| 101 | elem.* = @intCast(elem_val.toUnsignedInt(pt)); | |
| 100 | 102 | } |
| 101 | 103 | return result; |
| 102 | 104 | } |
| 103 | 105 | |
| 104 | fn arrayToIpString(val: Value, len_u64: u64, mod: *Module) !InternPool.NullTerminatedString { | |
| 106 | fn arrayToIpString(val: Value, len_u64: u64, pt: Zcu.PerThread) !InternPool.NullTerminatedString { | |
| 107 | const mod = pt.zcu; | |
| 105 | 108 | const gpa = mod.gpa; |
| 106 | 109 | const ip = &mod.intern_pool; |
| 107 | 110 | const len: usize = @intCast(len_u64); |
| ... | ... | @@ -110,9 +113,9 @@ fn arrayToIpString(val: Value, len_u64: u64, mod: *Module) !InternPool.NullTermi |
| 110 | 113 | // I don't think elemValue has the possibility to affect ip.string_bytes. Let's |
| 111 | 114 | // assert just to be sure. |
| 112 | 115 | const prev = ip.string_bytes.items.len; |
| 113 | const elem_val = try val.elemValue(mod, i); | |
| 116 | const elem_val = try val.elemValue(pt, i); | |
| 114 | 117 | assert(ip.string_bytes.items.len == prev); |
| 115 | const byte: u8 = @intCast(elem_val.toUnsignedInt(mod)); | |
| 118 | const byte: u8 = @intCast(elem_val.toUnsignedInt(pt)); | |
| 116 | 119 | ip.string_bytes.appendAssumeCapacity(byte); |
| 117 | 120 | } |
| 118 | 121 | return ip.getOrPutTrailingString(gpa, len, .no_embedded_nulls); |
| ... | ... | @@ -133,14 +136,14 @@ pub fn toType(self: Value) Type { |
| 133 | 136 | return Type.fromInterned(self.toIntern()); |
| 134 | 137 | } |
| 135 | 138 | |
| 136 | pub fn intFromEnum(val: Value, ty: Type, mod: *Module) Allocator.Error!Value { | |
| 137 | const ip = &mod.intern_pool; | |
| 139 | pub fn intFromEnum(val: Value, ty: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 140 | const ip = &pt.zcu.intern_pool; | |
| 138 | 141 | const enum_ty = ip.typeOf(val.toIntern()); |
| 139 | 142 | return switch (ip.indexToKey(enum_ty)) { |
| 140 | 143 | // Assume it is already an integer and return it directly. |
| 141 | 144 | .simple_type, .int_type => val, |
| 142 | 145 | .enum_literal => |enum_literal| { |
| 143 | const field_index = ty.enumFieldIndex(enum_literal, mod).?; | |
| 146 | const field_index = ty.enumFieldIndex(enum_literal, pt.zcu).?; | |
| 144 | 147 | switch (ip.indexToKey(ty.toIntern())) { |
| 145 | 148 | // Assume it is already an integer and return it directly. |
| 146 | 149 | .simple_type, .int_type => return val, |
| ... | ... | @@ -150,13 +153,13 @@ pub fn intFromEnum(val: Value, ty: Type, mod: *Module) Allocator.Error!Value { |
| 150 | 153 | return Value.fromInterned(enum_type.values.get(ip)[field_index]); |
| 151 | 154 | } else { |
| 152 | 155 | // Field index and integer values are the same. |
| 153 | return mod.intValue(Type.fromInterned(enum_type.tag_ty), field_index); | |
| 156 | return pt.intValue(Type.fromInterned(enum_type.tag_ty), field_index); | |
| 154 | 157 | } |
| 155 | 158 | }, |
| 156 | 159 | else => unreachable, |
| 157 | 160 | } |
| 158 | 161 | }, |
| 159 | .enum_type => try mod.getCoerced(val, Type.fromInterned(ip.loadEnumType(enum_ty).tag_ty)), | |
| 162 | .enum_type => try pt.getCoerced(val, Type.fromInterned(ip.loadEnumType(enum_ty).tag_ty)), | |
| 160 | 163 | else => unreachable, |
| 161 | 164 | }; |
| 162 | 165 | } |
| ... | ... | @@ -164,38 +167,38 @@ pub fn intFromEnum(val: Value, ty: Type, mod: *Module) Allocator.Error!Value { |
| 164 | 167 | pub const ResolveStrat = Type.ResolveStrat; |
| 165 | 168 | |
| 166 | 169 | /// Asserts the value is an integer. |
| 167 | pub fn toBigInt(val: Value, space: *BigIntSpace, mod: *Module) BigIntConst { | |
| 168 | return val.toBigIntAdvanced(space, mod, .normal) catch unreachable; | |
| 170 | pub fn toBigInt(val: Value, space: *BigIntSpace, pt: Zcu.PerThread) BigIntConst { | |
| 171 | return val.toBigIntAdvanced(space, pt, .normal) catch unreachable; | |
| 169 | 172 | } |
| 170 | 173 | |
| 171 | 174 | /// Asserts the value is an integer. |
| 172 | 175 | pub fn toBigIntAdvanced( |
| 173 | 176 | val: Value, |
| 174 | 177 | space: *BigIntSpace, |
| 175 | mod: *Module, | |
| 178 | pt: Zcu.PerThread, | |
| 176 | 179 | strat: ResolveStrat, |
| 177 | 180 | ) Module.CompileError!BigIntConst { |
| 178 | 181 | return switch (val.toIntern()) { |
| 179 | 182 | .bool_false => BigIntMutable.init(&space.limbs, 0).toConst(), |
| 180 | 183 | .bool_true => BigIntMutable.init(&space.limbs, 1).toConst(), |
| 181 | 184 | .null_value => BigIntMutable.init(&space.limbs, 0).toConst(), |
| 182 | else => switch (mod.intern_pool.indexToKey(val.toIntern())) { | |
| 185 | else => switch (pt.zcu.intern_pool.indexToKey(val.toIntern())) { | |
| 183 | 186 | .int => |int| switch (int.storage) { |
| 184 | 187 | .u64, .i64, .big_int => int.storage.toBigInt(space), |
| 185 | 188 | .lazy_align, .lazy_size => |ty| { |
| 186 | if (strat == .sema) try Type.fromInterned(ty).resolveLayout(mod); | |
| 189 | if (strat == .sema) try Type.fromInterned(ty).resolveLayout(pt); | |
| 187 | 190 | const x = switch (int.storage) { |
| 188 | 191 | else => unreachable, |
| 189 | .lazy_align => Type.fromInterned(ty).abiAlignment(mod).toByteUnits() orelse 0, | |
| 190 | .lazy_size => Type.fromInterned(ty).abiSize(mod), | |
| 192 | .lazy_align => Type.fromInterned(ty).abiAlignment(pt).toByteUnits() orelse 0, | |
| 193 | .lazy_size => Type.fromInterned(ty).abiSize(pt), | |
| 191 | 194 | }; |
| 192 | 195 | return BigIntMutable.init(&space.limbs, x).toConst(); |
| 193 | 196 | }, |
| 194 | 197 | }, |
| 195 | .enum_tag => |enum_tag| Value.fromInterned(enum_tag.int).toBigIntAdvanced(space, mod, strat), | |
| 198 | .enum_tag => |enum_tag| Value.fromInterned(enum_tag.int).toBigIntAdvanced(space, pt, strat), | |
| 196 | 199 | .opt, .ptr => BigIntMutable.init( |
| 197 | 200 | &space.limbs, |
| 198 | (try val.getUnsignedIntAdvanced(mod, strat)).?, | |
| 201 | (try val.getUnsignedIntAdvanced(pt, strat)).?, | |
| 199 | 202 | ).toConst(), |
| 200 | 203 | else => unreachable, |
| 201 | 204 | }, |
| ... | ... | @@ -229,13 +232,14 @@ pub fn getVariable(val: Value, mod: *Module) ?InternPool.Key.Variable { |
| 229 | 232 | |
| 230 | 233 | /// If the value fits in a u64, return it, otherwise null. |
| 231 | 234 | /// Asserts not undefined. |
| 232 | pub fn getUnsignedInt(val: Value, mod: *Module) ?u64 { | |
| 233 | return getUnsignedIntAdvanced(val, mod, .normal) catch unreachable; | |
| 235 | pub fn getUnsignedInt(val: Value, pt: Zcu.PerThread) ?u64 { | |
| 236 | return getUnsignedIntAdvanced(val, pt, .normal) catch unreachable; | |
| 234 | 237 | } |
| 235 | 238 | |
| 236 | 239 | /// If the value fits in a u64, return it, otherwise null. |
| 237 | 240 | /// Asserts not undefined. |
| 238 | pub fn getUnsignedIntAdvanced(val: Value, mod: *Module, strat: ResolveStrat) !?u64 { | |
| 241 | pub fn getUnsignedIntAdvanced(val: Value, pt: Zcu.PerThread, strat: ResolveStrat) !?u64 { | |
| 242 | const mod = pt.zcu; | |
| 239 | 243 | return switch (val.toIntern()) { |
| 240 | 244 | .undef => unreachable, |
| 241 | 245 | .bool_false => 0, |
| ... | ... | @@ -246,22 +250,22 @@ pub fn getUnsignedIntAdvanced(val: Value, mod: *Module, strat: ResolveStrat) !?u |
| 246 | 250 | .big_int => |big_int| big_int.to(u64) catch null, |
| 247 | 251 | .u64 => |x| x, |
| 248 | 252 | .i64 => |x| std.math.cast(u64, x), |
| 249 | .lazy_align => |ty| (try Type.fromInterned(ty).abiAlignmentAdvanced(mod, strat.toLazy())).scalar.toByteUnits() orelse 0, | |
| 250 | .lazy_size => |ty| (try Type.fromInterned(ty).abiSizeAdvanced(mod, strat.toLazy())).scalar, | |
| 253 | .lazy_align => |ty| (try Type.fromInterned(ty).abiAlignmentAdvanced(pt, strat.toLazy())).scalar.toByteUnits() orelse 0, | |
| 254 | .lazy_size => |ty| (try Type.fromInterned(ty).abiSizeAdvanced(pt, strat.toLazy())).scalar, | |
| 251 | 255 | }, |
| 252 | 256 | .ptr => |ptr| switch (ptr.base_addr) { |
| 253 | 257 | .int => ptr.byte_offset, |
| 254 | 258 | .field => |field| { |
| 255 | const base_addr = (try Value.fromInterned(field.base).getUnsignedIntAdvanced(mod, strat)) orelse return null; | |
| 259 | const base_addr = (try Value.fromInterned(field.base).getUnsignedIntAdvanced(pt, strat)) orelse return null; | |
| 256 | 260 | const struct_ty = Value.fromInterned(field.base).typeOf(mod).childType(mod); |
| 257 | if (strat == .sema) try struct_ty.resolveLayout(mod); | |
| 258 | return base_addr + struct_ty.structFieldOffset(@intCast(field.index), mod) + ptr.byte_offset; | |
| 261 | if (strat == .sema) try struct_ty.resolveLayout(pt); | |
| 262 | return base_addr + struct_ty.structFieldOffset(@intCast(field.index), pt) + ptr.byte_offset; | |
| 259 | 263 | }, |
| 260 | 264 | else => null, |
| 261 | 265 | }, |
| 262 | 266 | .opt => |opt| switch (opt.val) { |
| 263 | 267 | .none => 0, |
| 264 | else => |payload| Value.fromInterned(payload).getUnsignedIntAdvanced(mod, strat), | |
| 268 | else => |payload| Value.fromInterned(payload).getUnsignedIntAdvanced(pt, strat), | |
| 265 | 269 | }, |
| 266 | 270 | else => null, |
| 267 | 271 | }, |
| ... | ... | @@ -269,27 +273,27 @@ pub fn getUnsignedIntAdvanced(val: Value, mod: *Module, strat: ResolveStrat) !?u |
| 269 | 273 | } |
| 270 | 274 | |
| 271 | 275 | /// Asserts the value is an integer and it fits in a u64 |
| 272 | pub fn toUnsignedInt(val: Value, zcu: *Zcu) u64 { | |
| 273 | return getUnsignedInt(val, zcu).?; | |
| 276 | pub fn toUnsignedInt(val: Value, pt: Zcu.PerThread) u64 { | |
| 277 | return getUnsignedInt(val, pt).?; | |
| 274 | 278 | } |
| 275 | 279 | |
| 276 | 280 | /// Asserts the value is an integer and it fits in a u64 |
| 277 | pub fn toUnsignedIntSema(val: Value, zcu: *Zcu) !u64 { | |
| 278 | return (try getUnsignedIntAdvanced(val, zcu, .sema)).?; | |
| 281 | pub fn toUnsignedIntSema(val: Value, pt: Zcu.PerThread) !u64 { | |
| 282 | return (try getUnsignedIntAdvanced(val, pt, .sema)).?; | |
| 279 | 283 | } |
| 280 | 284 | |
| 281 | 285 | /// Asserts the value is an integer and it fits in a i64 |
| 282 | pub fn toSignedInt(val: Value, mod: *Module) i64 { | |
| 286 | pub fn toSignedInt(val: Value, pt: Zcu.PerThread) i64 { | |
| 283 | 287 | return switch (val.toIntern()) { |
| 284 | 288 | .bool_false => 0, |
| 285 | 289 | .bool_true => 1, |
| 286 | else => switch (mod.intern_pool.indexToKey(val.toIntern())) { | |
| 290 | else => switch (pt.zcu.intern_pool.indexToKey(val.toIntern())) { | |
| 287 | 291 | .int => |int| switch (int.storage) { |
| 288 | 292 | .big_int => |big_int| big_int.to(i64) catch unreachable, |
| 289 | 293 | .i64 => |x| x, |
| 290 | 294 | .u64 => |x| @intCast(x), |
| 291 | .lazy_align => |ty| @intCast(Type.fromInterned(ty).abiAlignment(mod).toByteUnits() orelse 0), | |
| 292 | .lazy_size => |ty| @intCast(Type.fromInterned(ty).abiSize(mod)), | |
| 295 | .lazy_align => |ty| @intCast(Type.fromInterned(ty).abiAlignment(pt).toByteUnits() orelse 0), | |
| 296 | .lazy_size => |ty| @intCast(Type.fromInterned(ty).abiSize(pt)), | |
| 293 | 297 | }, |
| 294 | 298 | else => unreachable, |
| 295 | 299 | }, |
| ... | ... | @@ -321,16 +325,17 @@ fn ptrHasIntAddr(val: Value, mod: *Module) bool { |
| 321 | 325 | /// |
| 322 | 326 | /// Asserts that buffer.len >= ty.abiSize(). The buffer is allowed to extend past |
| 323 | 327 | /// the end of the value in memory. |
| 324 | pub fn writeToMemory(val: Value, ty: Type, mod: *Module, buffer: []u8) error{ | |
| 328 | pub fn writeToMemory(val: Value, ty: Type, pt: Zcu.PerThread, buffer: []u8) error{ | |
| 325 | 329 | ReinterpretDeclRef, |
| 326 | 330 | IllDefinedMemoryLayout, |
| 327 | 331 | Unimplemented, |
| 328 | 332 | OutOfMemory, |
| 329 | 333 | }!void { |
| 334 | const mod = pt.zcu; | |
| 330 | 335 | const target = mod.getTarget(); |
| 331 | 336 | const endian = target.cpu.arch.endian(); |
| 332 | 337 | if (val.isUndef(mod)) { |
| 333 | const size: usize = @intCast(ty.abiSize(mod)); | |
| 338 | const size: usize = @intCast(ty.abiSize(pt)); | |
| 334 | 339 | @memset(buffer[0..size], 0xaa); |
| 335 | 340 | return; |
| 336 | 341 | } |
| ... | ... | @@ -346,41 +351,41 @@ pub fn writeToMemory(val: Value, ty: Type, mod: *Module, buffer: []u8) error{ |
| 346 | 351 | const byte_count: u16 = @intCast((@as(u17, bits) + 7) / 8); |
| 347 | 352 | |
| 348 | 353 | var bigint_buffer: BigIntSpace = undefined; |
| 349 | const bigint = val.toBigInt(&bigint_buffer, mod); | |
| 354 | const bigint = val.toBigInt(&bigint_buffer, pt); | |
| 350 | 355 | bigint.writeTwosComplement(buffer[0..byte_count], endian); |
| 351 | 356 | }, |
| 352 | 357 | .Float => switch (ty.floatBits(target)) { |
| 353 | 16 => std.mem.writeInt(u16, buffer[0..2], @bitCast(val.toFloat(f16, mod)), endian), | |
| 354 | 32 => std.mem.writeInt(u32, buffer[0..4], @bitCast(val.toFloat(f32, mod)), endian), | |
| 355 | 64 => std.mem.writeInt(u64, buffer[0..8], @bitCast(val.toFloat(f64, mod)), endian), | |
| 356 | 80 => std.mem.writeInt(u80, buffer[0..10], @bitCast(val.toFloat(f80, mod)), endian), | |
| 357 | 128 => std.mem.writeInt(u128, buffer[0..16], @bitCast(val.toFloat(f128, mod)), endian), | |
| 358 | 16 => std.mem.writeInt(u16, buffer[0..2], @bitCast(val.toFloat(f16, pt)), endian), | |
| 359 | 32 => std.mem.writeInt(u32, buffer[0..4], @bitCast(val.toFloat(f32, pt)), endian), | |
| 360 | 64 => std.mem.writeInt(u64, buffer[0..8], @bitCast(val.toFloat(f64, pt)), endian), | |
| 361 | 80 => std.mem.writeInt(u80, buffer[0..10], @bitCast(val.toFloat(f80, pt)), endian), | |
| 362 | 128 => std.mem.writeInt(u128, buffer[0..16], @bitCast(val.toFloat(f128, pt)), endian), | |
| 358 | 363 | else => unreachable, |
| 359 | 364 | }, |
| 360 | 365 | .Array => { |
| 361 | 366 | const len = ty.arrayLen(mod); |
| 362 | 367 | const elem_ty = ty.childType(mod); |
| 363 | const elem_size: usize = @intCast(elem_ty.abiSize(mod)); | |
| 368 | const elem_size: usize = @intCast(elem_ty.abiSize(pt)); | |
| 364 | 369 | var elem_i: usize = 0; |
| 365 | 370 | var buf_off: usize = 0; |
| 366 | 371 | while (elem_i < len) : (elem_i += 1) { |
| 367 | const elem_val = try val.elemValue(mod, elem_i); | |
| 368 | try elem_val.writeToMemory(elem_ty, mod, buffer[buf_off..]); | |
| 372 | const elem_val = try val.elemValue(pt, elem_i); | |
| 373 | try elem_val.writeToMemory(elem_ty, pt, buffer[buf_off..]); | |
| 369 | 374 | buf_off += elem_size; |
| 370 | 375 | } |
| 371 | 376 | }, |
| 372 | 377 | .Vector => { |
| 373 | 378 | // We use byte_count instead of abi_size here, so that any padding bytes |
| 374 | 379 | // follow the data bytes, on both big- and little-endian systems. |
| 375 | const byte_count = (@as(usize, @intCast(ty.bitSize(mod))) + 7) / 8; | |
| 376 | return writeToPackedMemory(val, ty, mod, buffer[0..byte_count], 0); | |
| 380 | const byte_count = (@as(usize, @intCast(ty.bitSize(pt))) + 7) / 8; | |
| 381 | return writeToPackedMemory(val, ty, pt, buffer[0..byte_count], 0); | |
| 377 | 382 | }, |
| 378 | 383 | .Struct => { |
| 379 | 384 | const struct_type = mod.typeToStruct(ty) orelse return error.IllDefinedMemoryLayout; |
| 380 | 385 | switch (struct_type.layout) { |
| 381 | 386 | .auto => return error.IllDefinedMemoryLayout, |
| 382 | 387 | .@"extern" => for (0..struct_type.field_types.len) |field_index| { |
| 383 | const off: usize = @intCast(ty.structFieldOffset(field_index, mod)); | |
| 388 | const off: usize = @intCast(ty.structFieldOffset(field_index, pt)); | |
| 384 | 389 | const field_val = Value.fromInterned(switch (ip.indexToKey(val.toIntern()).aggregate.storage) { |
| 385 | 390 | .bytes => |bytes| { |
| 386 | 391 | buffer[off] = bytes.at(field_index, ip); |
| ... | ... | @@ -390,11 +395,11 @@ pub fn writeToMemory(val: Value, ty: Type, mod: *Module, buffer: []u8) error{ |
| 390 | 395 | .repeated_elem => |elem| elem, |
| 391 | 396 | }); |
| 392 | 397 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 393 | try writeToMemory(field_val, field_ty, mod, buffer[off..]); | |
| 398 | try writeToMemory(field_val, field_ty, pt, buffer[off..]); | |
| 394 | 399 | }, |
| 395 | 400 | .@"packed" => { |
| 396 | const byte_count = (@as(usize, @intCast(ty.bitSize(mod))) + 7) / 8; | |
| 397 | return writeToPackedMemory(val, ty, mod, buffer[0..byte_count], 0); | |
| 401 | const byte_count = (@as(usize, @intCast(ty.bitSize(pt))) + 7) / 8; | |
| 402 | return writeToPackedMemory(val, ty, pt, buffer[0..byte_count], 0); | |
| 398 | 403 | }, |
| 399 | 404 | } |
| 400 | 405 | }, |
| ... | ... | @@ -421,34 +426,34 @@ pub fn writeToMemory(val: Value, ty: Type, mod: *Module, buffer: []u8) error{ |
| 421 | 426 | const union_obj = mod.typeToUnion(ty).?; |
| 422 | 427 | const field_index = mod.unionTagFieldIndex(union_obj, union_tag).?; |
| 423 | 428 | const field_type = Type.fromInterned(union_obj.field_types.get(&mod.intern_pool)[field_index]); |
| 424 | const field_val = try val.fieldValue(mod, field_index); | |
| 425 | const byte_count: usize = @intCast(field_type.abiSize(mod)); | |
| 426 | return writeToMemory(field_val, field_type, mod, buffer[0..byte_count]); | |
| 429 | const field_val = try val.fieldValue(pt, field_index); | |
| 430 | const byte_count: usize = @intCast(field_type.abiSize(pt)); | |
| 431 | return writeToMemory(field_val, field_type, pt, buffer[0..byte_count]); | |
| 427 | 432 | } else { |
| 428 | const backing_ty = try ty.unionBackingType(mod); | |
| 429 | const byte_count: usize = @intCast(backing_ty.abiSize(mod)); | |
| 430 | return writeToMemory(val.unionValue(mod), backing_ty, mod, buffer[0..byte_count]); | |
| 433 | const backing_ty = try ty.unionBackingType(pt); | |
| 434 | const byte_count: usize = @intCast(backing_ty.abiSize(pt)); | |
| 435 | return writeToMemory(val.unionValue(mod), backing_ty, pt, buffer[0..byte_count]); | |
| 431 | 436 | } |
| 432 | 437 | }, |
| 433 | 438 | .@"packed" => { |
| 434 | const backing_ty = try ty.unionBackingType(mod); | |
| 435 | const byte_count: usize = @intCast(backing_ty.abiSize(mod)); | |
| 436 | return writeToPackedMemory(val, ty, mod, buffer[0..byte_count], 0); | |
| 439 | const backing_ty = try ty.unionBackingType(pt); | |
| 440 | const byte_count: usize = @intCast(backing_ty.abiSize(pt)); | |
| 441 | return writeToPackedMemory(val, ty, pt, buffer[0..byte_count], 0); | |
| 437 | 442 | }, |
| 438 | 443 | }, |
| 439 | 444 | .Pointer => { |
| 440 | 445 | if (ty.isSlice(mod)) return error.IllDefinedMemoryLayout; |
| 441 | 446 | if (!val.ptrHasIntAddr(mod)) return error.ReinterpretDeclRef; |
| 442 | return val.writeToMemory(Type.usize, mod, buffer); | |
| 447 | return val.writeToMemory(Type.usize, pt, buffer); | |
| 443 | 448 | }, |
| 444 | 449 | .Optional => { |
| 445 | 450 | if (!ty.isPtrLikeOptional(mod)) return error.IllDefinedMemoryLayout; |
| 446 | 451 | const child = ty.optionalChild(mod); |
| 447 | 452 | const opt_val = val.optionalValue(mod); |
| 448 | 453 | if (opt_val) |some| { |
| 449 | return some.writeToMemory(child, mod, buffer); | |
| 454 | return some.writeToMemory(child, pt, buffer); | |
| 450 | 455 | } else { |
| 451 | return writeToMemory(try mod.intValue(Type.usize, 0), Type.usize, mod, buffer); | |
| 456 | return writeToMemory(try pt.intValue(Type.usize, 0), Type.usize, pt, buffer); | |
| 452 | 457 | } |
| 453 | 458 | }, |
| 454 | 459 | else => return error.Unimplemented, |
| ... | ... | @@ -462,15 +467,16 @@ pub fn writeToMemory(val: Value, ty: Type, mod: *Module, buffer: []u8) error{ |
| 462 | 467 | pub fn writeToPackedMemory( |
| 463 | 468 | val: Value, |
| 464 | 469 | ty: Type, |
| 465 | mod: *Module, | |
| 470 | pt: Zcu.PerThread, | |
| 466 | 471 | buffer: []u8, |
| 467 | 472 | bit_offset: usize, |
| 468 | 473 | ) error{ ReinterpretDeclRef, OutOfMemory }!void { |
| 474 | const mod = pt.zcu; | |
| 469 | 475 | const ip = &mod.intern_pool; |
| 470 | 476 | const target = mod.getTarget(); |
| 471 | 477 | const endian = target.cpu.arch.endian(); |
| 472 | 478 | if (val.isUndef(mod)) { |
| 473 | const bit_size: usize = @intCast(ty.bitSize(mod)); | |
| 479 | const bit_size: usize = @intCast(ty.bitSize(pt)); | |
| 474 | 480 | if (bit_size != 0) { |
| 475 | 481 | std.mem.writeVarPackedInt(buffer, bit_offset, bit_size, @as(u1, 0), endian); |
| 476 | 482 | } |
| ... | ... | @@ -494,30 +500,30 @@ pub fn writeToPackedMemory( |
| 494 | 500 | const bits = ty.intInfo(mod).bits; |
| 495 | 501 | if (bits == 0) return; |
| 496 | 502 | |
| 497 | switch (ip.indexToKey((try val.intFromEnum(ty, mod)).toIntern()).int.storage) { | |
| 503 | switch (ip.indexToKey((try val.intFromEnum(ty, pt)).toIntern()).int.storage) { | |
| 498 | 504 | inline .u64, .i64 => |int| std.mem.writeVarPackedInt(buffer, bit_offset, bits, int, endian), |
| 499 | 505 | .big_int => |bigint| bigint.writePackedTwosComplement(buffer, bit_offset, bits, endian), |
| 500 | 506 | .lazy_align => |lazy_align| { |
| 501 | const num = Type.fromInterned(lazy_align).abiAlignment(mod).toByteUnits() orelse 0; | |
| 507 | const num = Type.fromInterned(lazy_align).abiAlignment(pt).toByteUnits() orelse 0; | |
| 502 | 508 | std.mem.writeVarPackedInt(buffer, bit_offset, bits, num, endian); |
| 503 | 509 | }, |
| 504 | 510 | .lazy_size => |lazy_size| { |
| 505 | const num = Type.fromInterned(lazy_size).abiSize(mod); | |
| 511 | const num = Type.fromInterned(lazy_size).abiSize(pt); | |
| 506 | 512 | std.mem.writeVarPackedInt(buffer, bit_offset, bits, num, endian); |
| 507 | 513 | }, |
| 508 | 514 | } |
| 509 | 515 | }, |
| 510 | 516 | .Float => switch (ty.floatBits(target)) { |
| 511 | 16 => std.mem.writePackedInt(u16, buffer, bit_offset, @bitCast(val.toFloat(f16, mod)), endian), | |
| 512 | 32 => std.mem.writePackedInt(u32, buffer, bit_offset, @bitCast(val.toFloat(f32, mod)), endian), | |
| 513 | 64 => std.mem.writePackedInt(u64, buffer, bit_offset, @bitCast(val.toFloat(f64, mod)), endian), | |
| 514 | 80 => std.mem.writePackedInt(u80, buffer, bit_offset, @bitCast(val.toFloat(f80, mod)), endian), | |
| 515 | 128 => std.mem.writePackedInt(u128, buffer, bit_offset, @bitCast(val.toFloat(f128, mod)), endian), | |
| 517 | 16 => std.mem.writePackedInt(u16, buffer, bit_offset, @bitCast(val.toFloat(f16, pt)), endian), | |
| 518 | 32 => std.mem.writePackedInt(u32, buffer, bit_offset, @bitCast(val.toFloat(f32, pt)), endian), | |
| 519 | 64 => std.mem.writePackedInt(u64, buffer, bit_offset, @bitCast(val.toFloat(f64, pt)), endian), | |
| 520 | 80 => std.mem.writePackedInt(u80, buffer, bit_offset, @bitCast(val.toFloat(f80, pt)), endian), | |
| 521 | 128 => std.mem.writePackedInt(u128, buffer, bit_offset, @bitCast(val.toFloat(f128, pt)), endian), | |
| 516 | 522 | else => unreachable, |
| 517 | 523 | }, |
| 518 | 524 | .Vector => { |
| 519 | 525 | const elem_ty = ty.childType(mod); |
| 520 | const elem_bit_size: u16 = @intCast(elem_ty.bitSize(mod)); | |
| 526 | const elem_bit_size: u16 = @intCast(elem_ty.bitSize(pt)); | |
| 521 | 527 | const len: usize = @intCast(ty.arrayLen(mod)); |
| 522 | 528 | |
| 523 | 529 | var bits: u16 = 0; |
| ... | ... | @@ -525,8 +531,8 @@ pub fn writeToPackedMemory( |
| 525 | 531 | while (elem_i < len) : (elem_i += 1) { |
| 526 | 532 | // On big-endian systems, LLVM reverses the element order of vectors by default |
| 527 | 533 | const tgt_elem_i = if (endian == .big) len - elem_i - 1 else elem_i; |
| 528 | const elem_val = try val.elemValue(mod, tgt_elem_i); | |
| 529 | try elem_val.writeToPackedMemory(elem_ty, mod, buffer, bit_offset + bits); | |
| 534 | const elem_val = try val.elemValue(pt, tgt_elem_i); | |
| 535 | try elem_val.writeToPackedMemory(elem_ty, pt, buffer, bit_offset + bits); | |
| 530 | 536 | bits += elem_bit_size; |
| 531 | 537 | } |
| 532 | 538 | }, |
| ... | ... | @@ -543,8 +549,8 @@ pub fn writeToPackedMemory( |
| 543 | 549 | .repeated_elem => |elem| elem, |
| 544 | 550 | }); |
| 545 | 551 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[i]); |
| 546 | const field_bits: u16 = @intCast(field_ty.bitSize(mod)); | |
| 547 | try field_val.writeToPackedMemory(field_ty, mod, buffer, bit_offset + bits); | |
| 552 | const field_bits: u16 = @intCast(field_ty.bitSize(pt)); | |
| 553 | try field_val.writeToPackedMemory(field_ty, pt, buffer, bit_offset + bits); | |
| 548 | 554 | bits += field_bits; |
| 549 | 555 | } |
| 550 | 556 | }, |
| ... | ... | @@ -556,11 +562,11 @@ pub fn writeToPackedMemory( |
| 556 | 562 | if (val.unionTag(mod)) |union_tag| { |
| 557 | 563 | const field_index = mod.unionTagFieldIndex(union_obj, union_tag).?; |
| 558 | 564 | const field_type = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 559 | const field_val = try val.fieldValue(mod, field_index); | |
| 560 | return field_val.writeToPackedMemory(field_type, mod, buffer, bit_offset); | |
| 565 | const field_val = try val.fieldValue(pt, field_index); | |
| 566 | return field_val.writeToPackedMemory(field_type, pt, buffer, bit_offset); | |
| 561 | 567 | } else { |
| 562 | const backing_ty = try ty.unionBackingType(mod); | |
| 563 | return val.unionValue(mod).writeToPackedMemory(backing_ty, mod, buffer, bit_offset); | |
| 568 | const backing_ty = try ty.unionBackingType(pt); | |
| 569 | return val.unionValue(mod).writeToPackedMemory(backing_ty, pt, buffer, bit_offset); | |
| 564 | 570 | } |
| 565 | 571 | }, |
| 566 | 572 | } |
| ... | ... | @@ -568,16 +574,16 @@ pub fn writeToPackedMemory( |
| 568 | 574 | .Pointer => { |
| 569 | 575 | assert(!ty.isSlice(mod)); // No well defined layout. |
| 570 | 576 | if (!val.ptrHasIntAddr(mod)) return error.ReinterpretDeclRef; |
| 571 | return val.writeToPackedMemory(Type.usize, mod, buffer, bit_offset); | |
| 577 | return val.writeToPackedMemory(Type.usize, pt, buffer, bit_offset); | |
| 572 | 578 | }, |
| 573 | 579 | .Optional => { |
| 574 | 580 | assert(ty.isPtrLikeOptional(mod)); |
| 575 | 581 | const child = ty.optionalChild(mod); |
| 576 | 582 | const opt_val = val.optionalValue(mod); |
| 577 | 583 | if (opt_val) |some| { |
| 578 | return some.writeToPackedMemory(child, mod, buffer, bit_offset); | |
| 584 | return some.writeToPackedMemory(child, pt, buffer, bit_offset); | |
| 579 | 585 | } else { |
| 580 | return writeToPackedMemory(try mod.intValue(Type.usize, 0), Type.usize, mod, buffer, bit_offset); | |
| 586 | return writeToPackedMemory(try pt.intValue(Type.usize, 0), Type.usize, pt, buffer, bit_offset); | |
| 581 | 587 | } |
| 582 | 588 | }, |
| 583 | 589 | else => @panic("TODO implement writeToPackedMemory for more types"), |
| ... | ... | @@ -590,7 +596,7 @@ pub fn writeToPackedMemory( |
| 590 | 596 | /// the end of the value in memory. |
| 591 | 597 | pub fn readFromMemory( |
| 592 | 598 | ty: Type, |
| 593 | mod: *Module, | |
| 599 | pt: Zcu.PerThread, | |
| 594 | 600 | buffer: []const u8, |
| 595 | 601 | arena: Allocator, |
| 596 | 602 | ) error{ |
| ... | ... | @@ -598,6 +604,7 @@ pub fn readFromMemory( |
| 598 | 604 | Unimplemented, |
| 599 | 605 | OutOfMemory, |
| 600 | 606 | }!Value { |
| 607 | const mod = pt.zcu; | |
| 601 | 608 | const ip = &mod.intern_pool; |
| 602 | 609 | const target = mod.getTarget(); |
| 603 | 610 | const endian = target.cpu.arch.endian(); |
| ... | ... | @@ -642,7 +649,7 @@ pub fn readFromMemory( |
| 642 | 649 | return mod.getCoerced(try mod.intValue_big(int_ty, bigint.toConst()), ty); |
| 643 | 650 | } |
| 644 | 651 | }, |
| 645 | .Float => return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 652 | .Float => return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 646 | 653 | .ty = ty.toIntern(), |
| 647 | 654 | .storage = switch (ty.floatBits(target)) { |
| 648 | 655 | 16 => .{ .f16 = @bitCast(std.mem.readInt(u16, buffer[0..2], endian)) }, |
| ... | ... | @@ -652,25 +659,25 @@ pub fn readFromMemory( |
| 652 | 659 | 128 => .{ .f128 = @bitCast(std.mem.readInt(u128, buffer[0..16], endian)) }, |
| 653 | 660 | else => unreachable, |
| 654 | 661 | }, |
| 655 | } }))), | |
| 662 | } })), | |
| 656 | 663 | .Array => { |
| 657 | 664 | const elem_ty = ty.childType(mod); |
| 658 | const elem_size = elem_ty.abiSize(mod); | |
| 665 | const elem_size = elem_ty.abiSize(pt); | |
| 659 | 666 | const elems = try arena.alloc(InternPool.Index, @intCast(ty.arrayLen(mod))); |
| 660 | 667 | var offset: usize = 0; |
| 661 | 668 | for (elems) |*elem| { |
| 662 | 669 | elem.* = (try readFromMemory(elem_ty, mod, buffer[offset..], arena)).toIntern(); |
| 663 | 670 | offset += @intCast(elem_size); |
| 664 | 671 | } |
| 665 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 672 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 666 | 673 | .ty = ty.toIntern(), |
| 667 | 674 | .storage = .{ .elems = elems }, |
| 668 | } }))); | |
| 675 | } })); | |
| 669 | 676 | }, |
| 670 | 677 | .Vector => { |
| 671 | 678 | // We use byte_count instead of abi_size here, so that any padding bytes |
| 672 | 679 | // follow the data bytes, on both big- and little-endian systems. |
| 673 | const byte_count = (@as(usize, @intCast(ty.bitSize(mod))) + 7) / 8; | |
| 680 | const byte_count = (@as(usize, @intCast(ty.bitSize(pt))) + 7) / 8; | |
| 674 | 681 | return readFromPackedMemory(ty, mod, buffer[0..byte_count], 0, arena); |
| 675 | 682 | }, |
| 676 | 683 | .Struct => { |
| ... | ... | @@ -683,16 +690,16 @@ pub fn readFromMemory( |
| 683 | 690 | for (field_vals, 0..) |*field_val, i| { |
| 684 | 691 | const field_ty = Type.fromInterned(field_types.get(ip)[i]); |
| 685 | 692 | const off: usize = @intCast(ty.structFieldOffset(i, mod)); |
| 686 | const sz: usize = @intCast(field_ty.abiSize(mod)); | |
| 693 | const sz: usize = @intCast(field_ty.abiSize(pt)); | |
| 687 | 694 | field_val.* = (try readFromMemory(field_ty, mod, buffer[off..(off + sz)], arena)).toIntern(); |
| 688 | 695 | } |
| 689 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 696 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 690 | 697 | .ty = ty.toIntern(), |
| 691 | 698 | .storage = .{ .elems = field_vals }, |
| 692 | } }))); | |
| 699 | } })); | |
| 693 | 700 | }, |
| 694 | 701 | .@"packed" => { |
| 695 | const byte_count = (@as(usize, @intCast(ty.bitSize(mod))) + 7) / 8; | |
| 702 | const byte_count = (@as(usize, @intCast(ty.bitSize(pt))) + 7) / 8; | |
| 696 | 703 | return readFromPackedMemory(ty, mod, buffer[0..byte_count], 0, arena); |
| 697 | 704 | }, |
| 698 | 705 | } |
| ... | ... | @@ -704,49 +711,49 @@ pub fn readFromMemory( |
| 704 | 711 | const index = (int << @as(u6, @intCast(64 - bits))) >> @as(u6, @intCast(64 - bits)); |
| 705 | 712 | const name = mod.global_error_set.keys()[@intCast(index)]; |
| 706 | 713 | |
| 707 | return Value.fromInterned((try mod.intern(.{ .err = .{ | |
| 714 | return Value.fromInterned(try pt.intern(.{ .err = .{ | |
| 708 | 715 | .ty = ty.toIntern(), |
| 709 | 716 | .name = name, |
| 710 | } }))); | |
| 717 | } })); | |
| 711 | 718 | }, |
| 712 | 719 | .Union => switch (ty.containerLayout(mod)) { |
| 713 | 720 | .auto => return error.IllDefinedMemoryLayout, |
| 714 | 721 | .@"extern" => { |
| 715 | const union_size = ty.abiSize(mod); | |
| 722 | const union_size = ty.abiSize(pt); | |
| 716 | 723 | const array_ty = try mod.arrayType(.{ .len = union_size, .child = .u8_type }); |
| 717 | 724 | const val = (try readFromMemory(array_ty, mod, buffer, arena)).toIntern(); |
| 718 | return Value.fromInterned((try mod.intern(.{ .un = .{ | |
| 725 | return Value.fromInterned(try pt.intern(.{ .un = .{ | |
| 719 | 726 | .ty = ty.toIntern(), |
| 720 | 727 | .tag = .none, |
| 721 | 728 | .val = val, |
| 722 | } }))); | |
| 729 | } })); | |
| 723 | 730 | }, |
| 724 | 731 | .@"packed" => { |
| 725 | const byte_count = (@as(usize, @intCast(ty.bitSize(mod))) + 7) / 8; | |
| 732 | const byte_count = (@as(usize, @intCast(ty.bitSize(pt))) + 7) / 8; | |
| 726 | 733 | return readFromPackedMemory(ty, mod, buffer[0..byte_count], 0, arena); |
| 727 | 734 | }, |
| 728 | 735 | }, |
| 729 | 736 | .Pointer => { |
| 730 | 737 | assert(!ty.isSlice(mod)); // No well defined layout. |
| 731 | 738 | const int_val = try readFromMemory(Type.usize, mod, buffer, arena); |
| 732 | return Value.fromInterned((try mod.intern(.{ .ptr = .{ | |
| 739 | return Value.fromInterned(try pt.intern(.{ .ptr = .{ | |
| 733 | 740 | .ty = ty.toIntern(), |
| 734 | 741 | .base_addr = .int, |
| 735 | .byte_offset = int_val.toUnsignedInt(mod), | |
| 736 | } }))); | |
| 742 | .byte_offset = int_val.toUnsignedInt(pt), | |
| 743 | } })); | |
| 737 | 744 | }, |
| 738 | 745 | .Optional => { |
| 739 | 746 | assert(ty.isPtrLikeOptional(mod)); |
| 740 | 747 | const child_ty = ty.optionalChild(mod); |
| 741 | 748 | const child_val = try readFromMemory(child_ty, mod, buffer, arena); |
| 742 | return Value.fromInterned((try mod.intern(.{ .opt = .{ | |
| 749 | return Value.fromInterned(try pt.intern(.{ .opt = .{ | |
| 743 | 750 | .ty = ty.toIntern(), |
| 744 | .val = switch (child_val.orderAgainstZero(mod)) { | |
| 751 | .val = switch (child_val.orderAgainstZero(pt)) { | |
| 745 | 752 | .lt => unreachable, |
| 746 | 753 | .eq => .none, |
| 747 | 754 | .gt => child_val.toIntern(), |
| 748 | 755 | }, |
| 749 | } }))); | |
| 756 | } })); | |
| 750 | 757 | }, |
| 751 | 758 | else => return error.Unimplemented, |
| 752 | 759 | } |
| ... | ... | @@ -758,7 +765,7 @@ pub fn readFromMemory( |
| 758 | 765 | /// big-endian packed memory layouts start at the end of the buffer. |
| 759 | 766 | pub fn readFromPackedMemory( |
| 760 | 767 | ty: Type, |
| 761 | mod: *Module, | |
| 768 | pt: Zcu.PerThread, | |
| 762 | 769 | buffer: []const u8, |
| 763 | 770 | bit_offset: usize, |
| 764 | 771 | arena: Allocator, |
| ... | ... | @@ -766,6 +773,7 @@ pub fn readFromPackedMemory( |
| 766 | 773 | IllDefinedMemoryLayout, |
| 767 | 774 | OutOfMemory, |
| 768 | 775 | }!Value { |
| 776 | const mod = pt.zcu; | |
| 769 | 777 | const ip = &mod.intern_pool; |
| 770 | 778 | const target = mod.getTarget(); |
| 771 | 779 | const endian = target.cpu.arch.endian(); |
| ... | ... | @@ -783,35 +791,35 @@ pub fn readFromPackedMemory( |
| 783 | 791 | } |
| 784 | 792 | }, |
| 785 | 793 | .Int => { |
| 786 | if (buffer.len == 0) return mod.intValue(ty, 0); | |
| 794 | if (buffer.len == 0) return pt.intValue(ty, 0); | |
| 787 | 795 | const int_info = ty.intInfo(mod); |
| 788 | 796 | const bits = int_info.bits; |
| 789 | if (bits == 0) return mod.intValue(ty, 0); | |
| 797 | if (bits == 0) return pt.intValue(ty, 0); | |
| 790 | 798 | |
| 791 | 799 | // Fast path for integers <= u64 |
| 792 | 800 | if (bits <= 64) switch (int_info.signedness) { |
| 793 | 801 | // Use different backing types for unsigned vs signed to avoid the need to go via |
| 794 | 802 | // a larger type like `i128`. |
| 795 | .unsigned => return mod.intValue(ty, std.mem.readVarPackedInt(u64, buffer, bit_offset, bits, endian, .unsigned)), | |
| 796 | .signed => return mod.intValue(ty, std.mem.readVarPackedInt(i64, buffer, bit_offset, bits, endian, .signed)), | |
| 803 | .unsigned => return pt.intValue(ty, std.mem.readVarPackedInt(u64, buffer, bit_offset, bits, endian, .unsigned)), | |
| 804 | .signed => return pt.intValue(ty, std.mem.readVarPackedInt(i64, buffer, bit_offset, bits, endian, .signed)), | |
| 797 | 805 | }; |
| 798 | 806 | |
| 799 | 807 | // Slow path, we have to construct a big-int |
| 800 | const abi_size: usize = @intCast(ty.abiSize(mod)); | |
| 808 | const abi_size: usize = @intCast(ty.abiSize(pt)); | |
| 801 | 809 | const Limb = std.math.big.Limb; |
| 802 | 810 | const limb_count = (abi_size + @sizeOf(Limb) - 1) / @sizeOf(Limb); |
| 803 | 811 | const limbs_buffer = try arena.alloc(Limb, limb_count); |
| 804 | 812 | |
| 805 | 813 | var bigint = BigIntMutable.init(limbs_buffer, 0); |
| 806 | 814 | bigint.readPackedTwosComplement(buffer, bit_offset, bits, endian, int_info.signedness); |
| 807 | return mod.intValue_big(ty, bigint.toConst()); | |
| 815 | return pt.intValue_big(ty, bigint.toConst()); | |
| 808 | 816 | }, |
| 809 | 817 | .Enum => { |
| 810 | 818 | const int_ty = ty.intTagType(mod); |
| 811 | const int_val = try Value.readFromPackedMemory(int_ty, mod, buffer, bit_offset, arena); | |
| 812 | return mod.getCoerced(int_val, ty); | |
| 819 | const int_val = try Value.readFromPackedMemory(int_ty, pt, buffer, bit_offset, arena); | |
| 820 | return pt.getCoerced(int_val, ty); | |
| 813 | 821 | }, |
| 814 | .Float => return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 822 | .Float => return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 815 | 823 | .ty = ty.toIntern(), |
| 816 | 824 | .storage = switch (ty.floatBits(target)) { |
| 817 | 825 | 16 => .{ .f16 = @bitCast(std.mem.readPackedInt(u16, buffer, bit_offset, endian)) }, |
| ... | ... | @@ -821,23 +829,23 @@ pub fn readFromPackedMemory( |
| 821 | 829 | 128 => .{ .f128 = @bitCast(std.mem.readPackedInt(u128, buffer, bit_offset, endian)) }, |
| 822 | 830 | else => unreachable, |
| 823 | 831 | }, |
| 824 | } }))), | |
| 832 | } })), | |
| 825 | 833 | .Vector => { |
| 826 | 834 | const elem_ty = ty.childType(mod); |
| 827 | 835 | const elems = try arena.alloc(InternPool.Index, @intCast(ty.arrayLen(mod))); |
| 828 | 836 | |
| 829 | 837 | var bits: u16 = 0; |
| 830 | const elem_bit_size: u16 = @intCast(elem_ty.bitSize(mod)); | |
| 838 | const elem_bit_size: u16 = @intCast(elem_ty.bitSize(pt)); | |
| 831 | 839 | for (elems, 0..) |_, i| { |
| 832 | 840 | // On big-endian systems, LLVM reverses the element order of vectors by default |
| 833 | 841 | const tgt_elem_i = if (endian == .big) elems.len - i - 1 else i; |
| 834 | elems[tgt_elem_i] = (try readFromPackedMemory(elem_ty, mod, buffer, bit_offset + bits, arena)).toIntern(); | |
| 842 | elems[tgt_elem_i] = (try readFromPackedMemory(elem_ty, pt, buffer, bit_offset + bits, arena)).toIntern(); | |
| 835 | 843 | bits += elem_bit_size; |
| 836 | 844 | } |
| 837 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 845 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 838 | 846 | .ty = ty.toIntern(), |
| 839 | 847 | .storage = .{ .elems = elems }, |
| 840 | } }))); | |
| 848 | } })); | |
| 841 | 849 | }, |
| 842 | 850 | .Struct => { |
| 843 | 851 | // Sema is supposed to have emitted a compile error already for Auto layout structs, |
| ... | ... | @@ -847,43 +855,43 @@ pub fn readFromPackedMemory( |
| 847 | 855 | const field_vals = try arena.alloc(InternPool.Index, struct_type.field_types.len); |
| 848 | 856 | for (field_vals, 0..) |*field_val, i| { |
| 849 | 857 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[i]); |
| 850 | const field_bits: u16 = @intCast(field_ty.bitSize(mod)); | |
| 851 | field_val.* = (try readFromPackedMemory(field_ty, mod, buffer, bit_offset + bits, arena)).toIntern(); | |
| 858 | const field_bits: u16 = @intCast(field_ty.bitSize(pt)); | |
| 859 | field_val.* = (try readFromPackedMemory(field_ty, pt, buffer, bit_offset + bits, arena)).toIntern(); | |
| 852 | 860 | bits += field_bits; |
| 853 | 861 | } |
| 854 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 862 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 855 | 863 | .ty = ty.toIntern(), |
| 856 | 864 | .storage = .{ .elems = field_vals }, |
| 857 | } }))); | |
| 865 | } })); | |
| 858 | 866 | }, |
| 859 | 867 | .Union => switch (ty.containerLayout(mod)) { |
| 860 | 868 | .auto, .@"extern" => unreachable, // Handled by non-packed readFromMemory |
| 861 | 869 | .@"packed" => { |
| 862 | const backing_ty = try ty.unionBackingType(mod); | |
| 863 | const val = (try readFromPackedMemory(backing_ty, mod, buffer, bit_offset, arena)).toIntern(); | |
| 864 | return Value.fromInterned((try mod.intern(.{ .un = .{ | |
| 870 | const backing_ty = try ty.unionBackingType(pt); | |
| 871 | const val = (try readFromPackedMemory(backing_ty, pt, buffer, bit_offset, arena)).toIntern(); | |
| 872 | return Value.fromInterned(try pt.intern(.{ .un = .{ | |
| 865 | 873 | .ty = ty.toIntern(), |
| 866 | 874 | .tag = .none, |
| 867 | 875 | .val = val, |
| 868 | } }))); | |
| 876 | } })); | |
| 869 | 877 | }, |
| 870 | 878 | }, |
| 871 | 879 | .Pointer => { |
| 872 | 880 | assert(!ty.isSlice(mod)); // No well defined layout. |
| 873 | const int_val = try readFromPackedMemory(Type.usize, mod, buffer, bit_offset, arena); | |
| 874 | return Value.fromInterned(try mod.intern(.{ .ptr = .{ | |
| 881 | const int_val = try readFromPackedMemory(Type.usize, pt, buffer, bit_offset, arena); | |
| 882 | return Value.fromInterned(try pt.intern(.{ .ptr = .{ | |
| 875 | 883 | .ty = ty.toIntern(), |
| 876 | 884 | .base_addr = .int, |
| 877 | .byte_offset = int_val.toUnsignedInt(mod), | |
| 885 | .byte_offset = int_val.toUnsignedInt(pt), | |
| 878 | 886 | } })); |
| 879 | 887 | }, |
| 880 | 888 | .Optional => { |
| 881 | 889 | assert(ty.isPtrLikeOptional(mod)); |
| 882 | 890 | const child_ty = ty.optionalChild(mod); |
| 883 | const child_val = try readFromPackedMemory(child_ty, mod, buffer, bit_offset, arena); | |
| 884 | return Value.fromInterned(try mod.intern(.{ .opt = .{ | |
| 891 | const child_val = try readFromPackedMemory(child_ty, pt, buffer, bit_offset, arena); | |
| 892 | return Value.fromInterned(try pt.intern(.{ .opt = .{ | |
| 885 | 893 | .ty = ty.toIntern(), |
| 886 | .val = switch (child_val.orderAgainstZero(mod)) { | |
| 894 | .val = switch (child_val.orderAgainstZero(pt)) { | |
| 887 | 895 | .lt => unreachable, |
| 888 | 896 | .eq => .none, |
| 889 | 897 | .gt => child_val.toIntern(), |
| ... | ... | @@ -895,8 +903,8 @@ pub fn readFromPackedMemory( |
| 895 | 903 | } |
| 896 | 904 | |
| 897 | 905 | /// Asserts that the value is a float or an integer. |
| 898 | pub fn toFloat(val: Value, comptime T: type, mod: *Module) T { | |
| 899 | return switch (mod.intern_pool.indexToKey(val.toIntern())) { | |
| 906 | pub fn toFloat(val: Value, comptime T: type, pt: Zcu.PerThread) T { | |
| 907 | return switch (pt.zcu.intern_pool.indexToKey(val.toIntern())) { | |
| 900 | 908 | .int => |int| switch (int.storage) { |
| 901 | 909 | .big_int => |big_int| @floatCast(bigIntToFloat(big_int.limbs, big_int.positive)), |
| 902 | 910 | inline .u64, .i64 => |x| { |
| ... | ... | @@ -905,8 +913,8 @@ pub fn toFloat(val: Value, comptime T: type, mod: *Module) T { |
| 905 | 913 | } |
| 906 | 914 | return @floatFromInt(x); |
| 907 | 915 | }, |
| 908 | .lazy_align => |ty| @floatFromInt(Type.fromInterned(ty).abiAlignment(mod).toByteUnits() orelse 0), | |
| 909 | .lazy_size => |ty| @floatFromInt(Type.fromInterned(ty).abiSize(mod)), | |
| 916 | .lazy_align => |ty| @floatFromInt(Type.fromInterned(ty).abiAlignment(pt).toByteUnits() orelse 0), | |
| 917 | .lazy_size => |ty| @floatFromInt(Type.fromInterned(ty).abiSize(pt)), | |
| 910 | 918 | }, |
| 911 | 919 | .float => |float| switch (float.storage) { |
| 912 | 920 | inline else => |x| @floatCast(x), |
| ... | ... | @@ -934,29 +942,30 @@ fn bigIntToFloat(limbs: []const std.math.big.Limb, positive: bool) f128 { |
| 934 | 942 | } |
| 935 | 943 | } |
| 936 | 944 | |
| 937 | pub fn clz(val: Value, ty: Type, mod: *Module) u64 { | |
| 945 | pub fn clz(val: Value, ty: Type, pt: Zcu.PerThread) u64 { | |
| 938 | 946 | var bigint_buf: BigIntSpace = undefined; |
| 939 | const bigint = val.toBigInt(&bigint_buf, mod); | |
| 940 | return bigint.clz(ty.intInfo(mod).bits); | |
| 947 | const bigint = val.toBigInt(&bigint_buf, pt); | |
| 948 | return bigint.clz(ty.intInfo(pt.zcu).bits); | |
| 941 | 949 | } |
| 942 | 950 | |
| 943 | pub fn ctz(val: Value, ty: Type, mod: *Module) u64 { | |
| 951 | pub fn ctz(val: Value, ty: Type, pt: Zcu.PerThread) u64 { | |
| 944 | 952 | var bigint_buf: BigIntSpace = undefined; |
| 945 | const bigint = val.toBigInt(&bigint_buf, mod); | |
| 946 | return bigint.ctz(ty.intInfo(mod).bits); | |
| 953 | const bigint = val.toBigInt(&bigint_buf, pt); | |
| 954 | return bigint.ctz(ty.intInfo(pt.zcu).bits); | |
| 947 | 955 | } |
| 948 | 956 | |
| 949 | pub fn popCount(val: Value, ty: Type, mod: *Module) u64 { | |
| 957 | pub fn popCount(val: Value, ty: Type, pt: Zcu.PerThread) u64 { | |
| 950 | 958 | var bigint_buf: BigIntSpace = undefined; |
| 951 | const bigint = val.toBigInt(&bigint_buf, mod); | |
| 952 | return @intCast(bigint.popCount(ty.intInfo(mod).bits)); | |
| 959 | const bigint = val.toBigInt(&bigint_buf, pt); | |
| 960 | return @intCast(bigint.popCount(ty.intInfo(pt.zcu).bits)); | |
| 953 | 961 | } |
| 954 | 962 | |
| 955 | pub fn bitReverse(val: Value, ty: Type, mod: *Module, arena: Allocator) !Value { | |
| 963 | pub fn bitReverse(val: Value, ty: Type, pt: Zcu.PerThread, arena: Allocator) !Value { | |
| 964 | const mod = pt.zcu; | |
| 956 | 965 | const info = ty.intInfo(mod); |
| 957 | 966 | |
| 958 | 967 | var buffer: Value.BigIntSpace = undefined; |
| 959 | const operand_bigint = val.toBigInt(&buffer, mod); | |
| 968 | const operand_bigint = val.toBigInt(&buffer, pt); | |
| 960 | 969 | |
| 961 | 970 | const limbs = try arena.alloc( |
| 962 | 971 | std.math.big.Limb, |
| ... | ... | @@ -965,17 +974,18 @@ pub fn bitReverse(val: Value, ty: Type, mod: *Module, arena: Allocator) !Value { |
| 965 | 974 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 966 | 975 | result_bigint.bitReverse(operand_bigint, info.signedness, info.bits); |
| 967 | 976 | |
| 968 | return mod.intValue_big(ty, result_bigint.toConst()); | |
| 977 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 969 | 978 | } |
| 970 | 979 | |
| 971 | pub fn byteSwap(val: Value, ty: Type, mod: *Module, arena: Allocator) !Value { | |
| 980 | pub fn byteSwap(val: Value, ty: Type, pt: Zcu.PerThread, arena: Allocator) !Value { | |
| 981 | const mod = pt.zcu; | |
| 972 | 982 | const info = ty.intInfo(mod); |
| 973 | 983 | |
| 974 | 984 | // Bit count must be evenly divisible by 8 |
| 975 | 985 | assert(info.bits % 8 == 0); |
| 976 | 986 | |
| 977 | 987 | var buffer: Value.BigIntSpace = undefined; |
| 978 | const operand_bigint = val.toBigInt(&buffer, mod); | |
| 988 | const operand_bigint = val.toBigInt(&buffer, pt); | |
| 979 | 989 | |
| 980 | 990 | const limbs = try arena.alloc( |
| 981 | 991 | std.math.big.Limb, |
| ... | ... | @@ -984,33 +994,33 @@ pub fn byteSwap(val: Value, ty: Type, mod: *Module, arena: Allocator) !Value { |
| 984 | 994 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 985 | 995 | result_bigint.byteSwap(operand_bigint, info.signedness, info.bits / 8); |
| 986 | 996 | |
| 987 | return mod.intValue_big(ty, result_bigint.toConst()); | |
| 997 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 988 | 998 | } |
| 989 | 999 | |
| 990 | 1000 | /// Asserts the value is an integer and not undefined. |
| 991 | 1001 | /// Returns the number of bits the value requires to represent stored in twos complement form. |
| 992 | pub fn intBitCountTwosComp(self: Value, mod: *Module) usize { | |
| 1002 | pub fn intBitCountTwosComp(self: Value, pt: Zcu.PerThread) usize { | |
| 993 | 1003 | var buffer: BigIntSpace = undefined; |
| 994 | const big_int = self.toBigInt(&buffer, mod); | |
| 1004 | const big_int = self.toBigInt(&buffer, pt); | |
| 995 | 1005 | return big_int.bitCountTwosComp(); |
| 996 | 1006 | } |
| 997 | 1007 | |
| 998 | 1008 | /// Converts an integer or a float to a float. May result in a loss of information. |
| 999 | 1009 | /// Caller can find out by equality checking the result against the operand. |
| 1000 | pub fn floatCast(val: Value, dest_ty: Type, zcu: *Zcu) !Value { | |
| 1001 | const target = zcu.getTarget(); | |
| 1002 | if (val.isUndef(zcu)) return zcu.undefValue(dest_ty); | |
| 1003 | return Value.fromInterned((try zcu.intern(.{ .float = .{ | |
| 1010 | pub fn floatCast(val: Value, dest_ty: Type, pt: Zcu.PerThread) !Value { | |
| 1011 | const target = pt.zcu.getTarget(); | |
| 1012 | if (val.isUndef(pt.zcu)) return pt.undefValue(dest_ty); | |
| 1013 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 1004 | 1014 | .ty = dest_ty.toIntern(), |
| 1005 | 1015 | .storage = switch (dest_ty.floatBits(target)) { |
| 1006 | 16 => .{ .f16 = val.toFloat(f16, zcu) }, | |
| 1007 | 32 => .{ .f32 = val.toFloat(f32, zcu) }, | |
| 1008 | 64 => .{ .f64 = val.toFloat(f64, zcu) }, | |
| 1009 | 80 => .{ .f80 = val.toFloat(f80, zcu) }, | |
| 1010 | 128 => .{ .f128 = val.toFloat(f128, zcu) }, | |
| 1016 | 16 => .{ .f16 = val.toFloat(f16, pt) }, | |
| 1017 | 32 => .{ .f32 = val.toFloat(f32, pt) }, | |
| 1018 | 64 => .{ .f64 = val.toFloat(f64, pt) }, | |
| 1019 | 80 => .{ .f80 = val.toFloat(f80, pt) }, | |
| 1020 | 128 => .{ .f128 = val.toFloat(f128, pt) }, | |
| 1011 | 1021 | else => unreachable, |
| 1012 | 1022 | }, |
| 1013 | } }))); | |
| 1023 | } })); | |
| 1014 | 1024 | } |
| 1015 | 1025 | |
| 1016 | 1026 | /// Asserts the value is a float |
| ... | ... | @@ -1023,19 +1033,19 @@ pub fn floatHasFraction(self: Value, mod: *const Module) bool { |
| 1023 | 1033 | }; |
| 1024 | 1034 | } |
| 1025 | 1035 | |
| 1026 | pub fn orderAgainstZero(lhs: Value, mod: *Module) std.math.Order { | |
| 1027 | return orderAgainstZeroAdvanced(lhs, mod, .normal) catch unreachable; | |
| 1036 | pub fn orderAgainstZero(lhs: Value, pt: Zcu.PerThread) std.math.Order { | |
| 1037 | return orderAgainstZeroAdvanced(lhs, pt, .normal) catch unreachable; | |
| 1028 | 1038 | } |
| 1029 | 1039 | |
| 1030 | 1040 | pub fn orderAgainstZeroAdvanced( |
| 1031 | 1041 | lhs: Value, |
| 1032 | mod: *Module, | |
| 1042 | pt: Zcu.PerThread, | |
| 1033 | 1043 | strat: ResolveStrat, |
| 1034 | 1044 | ) Module.CompileError!std.math.Order { |
| 1035 | 1045 | return switch (lhs.toIntern()) { |
| 1036 | 1046 | .bool_false => .eq, |
| 1037 | 1047 | .bool_true => .gt, |
| 1038 | else => switch (mod.intern_pool.indexToKey(lhs.toIntern())) { | |
| 1048 | else => switch (pt.zcu.intern_pool.indexToKey(lhs.toIntern())) { | |
| 1039 | 1049 | .ptr => |ptr| if (ptr.byte_offset > 0) .gt else switch (ptr.base_addr) { |
| 1040 | 1050 | .decl, .comptime_alloc, .comptime_field => .gt, |
| 1041 | 1051 | .int => .eq, |
| ... | ... | @@ -1046,7 +1056,7 @@ pub fn orderAgainstZeroAdvanced( |
| 1046 | 1056 | inline .u64, .i64 => |x| std.math.order(x, 0), |
| 1047 | 1057 | .lazy_align => .gt, // alignment is never 0 |
| 1048 | 1058 | .lazy_size => |ty| return if (Type.fromInterned(ty).hasRuntimeBitsAdvanced( |
| 1049 | mod, | |
| 1059 | pt, | |
| 1050 | 1060 | false, |
| 1051 | 1061 | strat.toLazy(), |
| 1052 | 1062 | ) catch |err| switch (err) { |
| ... | ... | @@ -1054,7 +1064,7 @@ pub fn orderAgainstZeroAdvanced( |
| 1054 | 1064 | else => |e| return e, |
| 1055 | 1065 | }) .gt else .eq, |
| 1056 | 1066 | }, |
| 1057 | .enum_tag => |enum_tag| Value.fromInterned(enum_tag.int).orderAgainstZeroAdvanced(mod, strat), | |
| 1067 | .enum_tag => |enum_tag| Value.fromInterned(enum_tag.int).orderAgainstZeroAdvanced(pt, strat), | |
| 1058 | 1068 | .float => |float| switch (float.storage) { |
| 1059 | 1069 | inline else => |x| std.math.order(x, 0), |
| 1060 | 1070 | }, |
| ... | ... | @@ -1064,14 +1074,14 @@ pub fn orderAgainstZeroAdvanced( |
| 1064 | 1074 | } |
| 1065 | 1075 | |
| 1066 | 1076 | /// Asserts the value is comparable. |
| 1067 | pub fn order(lhs: Value, rhs: Value, mod: *Module) std.math.Order { | |
| 1068 | return orderAdvanced(lhs, rhs, mod, .normal) catch unreachable; | |
| 1077 | pub fn order(lhs: Value, rhs: Value, pt: Zcu.PerThread) std.math.Order { | |
| 1078 | return orderAdvanced(lhs, rhs, pt, .normal) catch unreachable; | |
| 1069 | 1079 | } |
| 1070 | 1080 | |
| 1071 | 1081 | /// Asserts the value is comparable. |
| 1072 | pub fn orderAdvanced(lhs: Value, rhs: Value, mod: *Module, strat: ResolveStrat) !std.math.Order { | |
| 1073 | const lhs_against_zero = try lhs.orderAgainstZeroAdvanced(mod, strat); | |
| 1074 | const rhs_against_zero = try rhs.orderAgainstZeroAdvanced(mod, strat); | |
| 1082 | pub fn orderAdvanced(lhs: Value, rhs: Value, pt: Zcu.PerThread, strat: ResolveStrat) !std.math.Order { | |
| 1083 | const lhs_against_zero = try lhs.orderAgainstZeroAdvanced(pt, strat); | |
| 1084 | const rhs_against_zero = try rhs.orderAgainstZeroAdvanced(pt, strat); | |
| 1075 | 1085 | switch (lhs_against_zero) { |
| 1076 | 1086 | .lt => if (rhs_against_zero != .lt) return .lt, |
| 1077 | 1087 | .eq => return rhs_against_zero.invert(), |
| ... | ... | @@ -1083,34 +1093,34 @@ pub fn orderAdvanced(lhs: Value, rhs: Value, mod: *Module, strat: ResolveStrat) |
| 1083 | 1093 | .gt => {}, |
| 1084 | 1094 | } |
| 1085 | 1095 | |
| 1086 | if (lhs.isFloat(mod) or rhs.isFloat(mod)) { | |
| 1087 | const lhs_f128 = lhs.toFloat(f128, mod); | |
| 1088 | const rhs_f128 = rhs.toFloat(f128, mod); | |
| 1096 | if (lhs.isFloat(pt.zcu) or rhs.isFloat(pt.zcu)) { | |
| 1097 | const lhs_f128 = lhs.toFloat(f128, pt); | |
| 1098 | const rhs_f128 = rhs.toFloat(f128, pt); | |
| 1089 | 1099 | return std.math.order(lhs_f128, rhs_f128); |
| 1090 | 1100 | } |
| 1091 | 1101 | |
| 1092 | 1102 | var lhs_bigint_space: BigIntSpace = undefined; |
| 1093 | 1103 | var rhs_bigint_space: BigIntSpace = undefined; |
| 1094 | const lhs_bigint = try lhs.toBigIntAdvanced(&lhs_bigint_space, mod, strat); | |
| 1095 | const rhs_bigint = try rhs.toBigIntAdvanced(&rhs_bigint_space, mod, strat); | |
| 1104 | const lhs_bigint = try lhs.toBigIntAdvanced(&lhs_bigint_space, pt, strat); | |
| 1105 | const rhs_bigint = try rhs.toBigIntAdvanced(&rhs_bigint_space, pt, strat); | |
| 1096 | 1106 | return lhs_bigint.order(rhs_bigint); |
| 1097 | 1107 | } |
| 1098 | 1108 | |
| 1099 | 1109 | /// Asserts the value is comparable. Does not take a type parameter because it supports |
| 1100 | 1110 | /// comparisons between heterogeneous types. |
| 1101 | pub fn compareHetero(lhs: Value, op: std.math.CompareOperator, rhs: Value, mod: *Module) bool { | |
| 1102 | return compareHeteroAdvanced(lhs, op, rhs, mod, .normal) catch unreachable; | |
| 1111 | pub fn compareHetero(lhs: Value, op: std.math.CompareOperator, rhs: Value, pt: Zcu.PerThread) bool { | |
| 1112 | return compareHeteroAdvanced(lhs, op, rhs, pt, .normal) catch unreachable; | |
| 1103 | 1113 | } |
| 1104 | 1114 | |
| 1105 | 1115 | pub fn compareHeteroAdvanced( |
| 1106 | 1116 | lhs: Value, |
| 1107 | 1117 | op: std.math.CompareOperator, |
| 1108 | 1118 | rhs: Value, |
| 1109 | mod: *Module, | |
| 1119 | pt: Zcu.PerThread, | |
| 1110 | 1120 | strat: ResolveStrat, |
| 1111 | 1121 | ) !bool { |
| 1112 | if (lhs.pointerDecl(mod)) |lhs_decl| { | |
| 1113 | if (rhs.pointerDecl(mod)) |rhs_decl| { | |
| 1122 | if (lhs.pointerDecl(pt.zcu)) |lhs_decl| { | |
| 1123 | if (rhs.pointerDecl(pt.zcu)) |rhs_decl| { | |
| 1114 | 1124 | switch (op) { |
| 1115 | 1125 | .eq => return lhs_decl == rhs_decl, |
| 1116 | 1126 | .neq => return lhs_decl != rhs_decl, |
| ... | ... | @@ -1123,31 +1133,32 @@ pub fn compareHeteroAdvanced( |
| 1123 | 1133 | else => {}, |
| 1124 | 1134 | } |
| 1125 | 1135 | } |
| 1126 | } else if (rhs.pointerDecl(mod)) |_| { | |
| 1136 | } else if (rhs.pointerDecl(pt.zcu)) |_| { | |
| 1127 | 1137 | switch (op) { |
| 1128 | 1138 | .eq => return false, |
| 1129 | 1139 | .neq => return true, |
| 1130 | 1140 | else => {}, |
| 1131 | 1141 | } |
| 1132 | 1142 | } |
| 1133 | return (try orderAdvanced(lhs, rhs, mod, strat)).compare(op); | |
| 1143 | return (try orderAdvanced(lhs, rhs, pt, strat)).compare(op); | |
| 1134 | 1144 | } |
| 1135 | 1145 | |
| 1136 | 1146 | /// Asserts the values are comparable. Both operands have type `ty`. |
| 1137 | 1147 | /// For vectors, returns true if comparison is true for ALL elements. |
| 1138 | pub fn compareAll(lhs: Value, op: std.math.CompareOperator, rhs: Value, ty: Type, mod: *Module) !bool { | |
| 1148 | pub fn compareAll(lhs: Value, op: std.math.CompareOperator, rhs: Value, ty: Type, pt: Zcu.PerThread) !bool { | |
| 1149 | const mod = pt.zcu; | |
| 1139 | 1150 | if (ty.zigTypeTag(mod) == .Vector) { |
| 1140 | 1151 | const scalar_ty = ty.scalarType(mod); |
| 1141 | 1152 | for (0..ty.vectorLen(mod)) |i| { |
| 1142 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 1143 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 1144 | if (!compareScalar(lhs_elem, op, rhs_elem, scalar_ty, mod)) { | |
| 1153 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 1154 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 1155 | if (!compareScalar(lhs_elem, op, rhs_elem, scalar_ty, pt)) { | |
| 1145 | 1156 | return false; |
| 1146 | 1157 | } |
| 1147 | 1158 | } |
| 1148 | 1159 | return true; |
| 1149 | 1160 | } |
| 1150 | return compareScalar(lhs, op, rhs, ty, mod); | |
| 1161 | return compareScalar(lhs, op, rhs, ty, pt); | |
| 1151 | 1162 | } |
| 1152 | 1163 | |
| 1153 | 1164 | /// Asserts the values are comparable. Both operands have type `ty`. |
| ... | ... | @@ -1156,12 +1167,12 @@ pub fn compareScalar( |
| 1156 | 1167 | op: std.math.CompareOperator, |
| 1157 | 1168 | rhs: Value, |
| 1158 | 1169 | ty: Type, |
| 1159 | mod: *Module, | |
| 1170 | pt: Zcu.PerThread, | |
| 1160 | 1171 | ) bool { |
| 1161 | 1172 | return switch (op) { |
| 1162 | .eq => lhs.eql(rhs, ty, mod), | |
| 1163 | .neq => !lhs.eql(rhs, ty, mod), | |
| 1164 | else => compareHetero(lhs, op, rhs, mod), | |
| 1173 | .eq => lhs.eql(rhs, ty, pt.zcu), | |
| 1174 | .neq => !lhs.eql(rhs, ty, pt.zcu), | |
| 1175 | else => compareHetero(lhs, op, rhs, pt), | |
| 1165 | 1176 | }; |
| 1166 | 1177 | } |
| 1167 | 1178 | |
| ... | ... | @@ -1170,24 +1181,25 @@ pub fn compareScalar( |
| 1170 | 1181 | /// Returns `false` if the value or any vector element is undefined. |
| 1171 | 1182 | /// |
| 1172 | 1183 | /// Note that `!compareAllWithZero(.eq, ...) != compareAllWithZero(.neq, ...)` |
| 1173 | pub fn compareAllWithZero(lhs: Value, op: std.math.CompareOperator, mod: *Module) bool { | |
| 1174 | return compareAllWithZeroAdvancedExtra(lhs, op, mod, .normal) catch unreachable; | |
| 1184 | pub fn compareAllWithZero(lhs: Value, op: std.math.CompareOperator, pt: Zcu.PerThread) bool { | |
| 1185 | return compareAllWithZeroAdvancedExtra(lhs, op, pt, .normal) catch unreachable; | |
| 1175 | 1186 | } |
| 1176 | 1187 | |
| 1177 | 1188 | pub fn compareAllWithZeroSema( |
| 1178 | 1189 | lhs: Value, |
| 1179 | 1190 | op: std.math.CompareOperator, |
| 1180 | zcu: *Zcu, | |
| 1191 | pt: Zcu.PerThread, | |
| 1181 | 1192 | ) Module.CompileError!bool { |
| 1182 | return compareAllWithZeroAdvancedExtra(lhs, op, zcu, .sema); | |
| 1193 | return compareAllWithZeroAdvancedExtra(lhs, op, pt, .sema); | |
| 1183 | 1194 | } |
| 1184 | 1195 | |
| 1185 | 1196 | pub fn compareAllWithZeroAdvancedExtra( |
| 1186 | 1197 | lhs: Value, |
| 1187 | 1198 | op: std.math.CompareOperator, |
| 1188 | mod: *Module, | |
| 1199 | pt: Zcu.PerThread, | |
| 1189 | 1200 | strat: ResolveStrat, |
| 1190 | 1201 | ) Module.CompileError!bool { |
| 1202 | const mod = pt.zcu; | |
| 1191 | 1203 | if (lhs.isInf(mod)) { |
| 1192 | 1204 | switch (op) { |
| 1193 | 1205 | .neq => return true, |
| ... | ... | @@ -1206,14 +1218,14 @@ pub fn compareAllWithZeroAdvancedExtra( |
| 1206 | 1218 | if (!std.math.order(byte, 0).compare(op)) break false; |
| 1207 | 1219 | } else true, |
| 1208 | 1220 | .elems => |elems| for (elems) |elem| { |
| 1209 | if (!try Value.fromInterned(elem).compareAllWithZeroAdvancedExtra(op, mod, strat)) break false; | |
| 1221 | if (!try Value.fromInterned(elem).compareAllWithZeroAdvancedExtra(op, pt, strat)) break false; | |
| 1210 | 1222 | } else true, |
| 1211 | .repeated_elem => |elem| Value.fromInterned(elem).compareAllWithZeroAdvancedExtra(op, mod, strat), | |
| 1223 | .repeated_elem => |elem| Value.fromInterned(elem).compareAllWithZeroAdvancedExtra(op, pt, strat), | |
| 1212 | 1224 | }, |
| 1213 | 1225 | .undef => return false, |
| 1214 | 1226 | else => {}, |
| 1215 | 1227 | } |
| 1216 | return (try orderAgainstZeroAdvanced(lhs, mod, strat)).compare(op); | |
| 1228 | return (try orderAgainstZeroAdvanced(lhs, pt, strat)).compare(op); | |
| 1217 | 1229 | } |
| 1218 | 1230 | |
| 1219 | 1231 | pub fn eql(a: Value, b: Value, ty: Type, mod: *Module) bool { |
| ... | ... | @@ -1275,21 +1287,22 @@ pub fn slicePtr(val: Value, mod: *Module) Value { |
| 1275 | 1287 | |
| 1276 | 1288 | /// Gets the `len` field of a slice value as a `u64`. |
| 1277 | 1289 | /// Resolves the length using `Sema` if necessary. |
| 1278 | pub fn sliceLen(val: Value, zcu: *Zcu) !u64 { | |
| 1279 | return Value.fromInterned(zcu.intern_pool.sliceLen(val.toIntern())).toUnsignedIntSema(zcu); | |
| 1290 | pub fn sliceLen(val: Value, pt: Zcu.PerThread) !u64 { | |
| 1291 | return Value.fromInterned(pt.zcu.intern_pool.sliceLen(val.toIntern())).toUnsignedIntSema(pt); | |
| 1280 | 1292 | } |
| 1281 | 1293 | |
| 1282 | 1294 | /// Asserts the value is an aggregate, and returns the element value at the given index. |
| 1283 | pub fn elemValue(val: Value, zcu: *Zcu, index: usize) Allocator.Error!Value { | |
| 1295 | pub fn elemValue(val: Value, pt: Zcu.PerThread, index: usize) Allocator.Error!Value { | |
| 1296 | const zcu = pt.zcu; | |
| 1284 | 1297 | const ip = &zcu.intern_pool; |
| 1285 | 1298 | switch (zcu.intern_pool.indexToKey(val.toIntern())) { |
| 1286 | 1299 | .undef => |ty| { |
| 1287 | return Value.fromInterned(try zcu.intern(.{ .undef = Type.fromInterned(ty).childType(zcu).toIntern() })); | |
| 1300 | return Value.fromInterned(try pt.intern(.{ .undef = Type.fromInterned(ty).childType(zcu).toIntern() })); | |
| 1288 | 1301 | }, |
| 1289 | 1302 | .aggregate => |aggregate| { |
| 1290 | 1303 | const len = ip.aggregateTypeLen(aggregate.ty); |
| 1291 | 1304 | if (index < len) return Value.fromInterned(switch (aggregate.storage) { |
| 1292 | .bytes => |bytes| try zcu.intern(.{ .int = .{ | |
| 1305 | .bytes => |bytes| try pt.intern(.{ .int = .{ | |
| 1293 | 1306 | .ty = .u8_type, |
| 1294 | 1307 | .storage = .{ .u64 = bytes.at(index, ip) }, |
| 1295 | 1308 | } }), |
| ... | ... | @@ -1330,17 +1343,17 @@ pub fn sliceArray( |
| 1330 | 1343 | start: usize, |
| 1331 | 1344 | end: usize, |
| 1332 | 1345 | ) error{OutOfMemory}!Value { |
| 1333 | const mod = sema.mod; | |
| 1334 | const ip = &mod.intern_pool; | |
| 1335 | return Value.fromInterned(try mod.intern(.{ | |
| 1346 | const pt = sema.pt; | |
| 1347 | const ip = &pt.zcu.intern_pool; | |
| 1348 | return Value.fromInterned(try pt.intern(.{ | |
| 1336 | 1349 | .aggregate = .{ |
| 1337 | .ty = switch (mod.intern_pool.indexToKey(mod.intern_pool.typeOf(val.toIntern()))) { | |
| 1338 | .array_type => |array_type| try mod.arrayType(.{ | |
| 1350 | .ty = switch (pt.zcu.intern_pool.indexToKey(pt.zcu.intern_pool.typeOf(val.toIntern()))) { | |
| 1351 | .array_type => |array_type| try pt.arrayType(.{ | |
| 1339 | 1352 | .len = @intCast(end - start), |
| 1340 | 1353 | .child = array_type.child, |
| 1341 | 1354 | .sentinel = if (end == array_type.len) array_type.sentinel else .none, |
| 1342 | 1355 | }), |
| 1343 | .vector_type => |vector_type| try mod.vectorType(.{ | |
| 1356 | .vector_type => |vector_type| try pt.vectorType(.{ | |
| 1344 | 1357 | .len = @intCast(end - start), |
| 1345 | 1358 | .child = vector_type.child, |
| 1346 | 1359 | }), |
| ... | ... | @@ -1363,13 +1376,14 @@ pub fn sliceArray( |
| 1363 | 1376 | })); |
| 1364 | 1377 | } |
| 1365 | 1378 | |
| 1366 | pub fn fieldValue(val: Value, mod: *Module, index: usize) !Value { | |
| 1379 | pub fn fieldValue(val: Value, pt: Zcu.PerThread, index: usize) !Value { | |
| 1380 | const mod = pt.zcu; | |
| 1367 | 1381 | return switch (mod.intern_pool.indexToKey(val.toIntern())) { |
| 1368 | .undef => |ty| Value.fromInterned((try mod.intern(.{ | |
| 1382 | .undef => |ty| Value.fromInterned(try pt.intern(.{ | |
| 1369 | 1383 | .undef = Type.fromInterned(ty).structFieldType(index, mod).toIntern(), |
| 1370 | }))), | |
| 1384 | })), | |
| 1371 | 1385 | .aggregate => |aggregate| Value.fromInterned(switch (aggregate.storage) { |
| 1372 | .bytes => |bytes| try mod.intern(.{ .int = .{ | |
| 1386 | .bytes => |bytes| try pt.intern(.{ .int = .{ | |
| 1373 | 1387 | .ty = .u8_type, |
| 1374 | 1388 | .storage = .{ .u64 = bytes.at(index, &mod.intern_pool) }, |
| 1375 | 1389 | } }), |
| ... | ... | @@ -1483,40 +1497,49 @@ pub fn floatFromInt(val: Value, arena: Allocator, int_ty: Type, float_ty: Type, |
| 1483 | 1497 | }; |
| 1484 | 1498 | } |
| 1485 | 1499 | |
| 1486 | pub fn floatFromIntAdvanced(val: Value, arena: Allocator, int_ty: Type, float_ty: Type, mod: *Module, strat: ResolveStrat) !Value { | |
| 1500 | pub fn floatFromIntAdvanced( | |
| 1501 | val: Value, | |
| 1502 | arena: Allocator, | |
| 1503 | int_ty: Type, | |
| 1504 | float_ty: Type, | |
| 1505 | pt: Zcu.PerThread, | |
| 1506 | strat: ResolveStrat, | |
| 1507 | ) !Value { | |
| 1508 | const mod = pt.zcu; | |
| 1487 | 1509 | if (int_ty.zigTypeTag(mod) == .Vector) { |
| 1488 | 1510 | const result_data = try arena.alloc(InternPool.Index, int_ty.vectorLen(mod)); |
| 1489 | 1511 | const scalar_ty = float_ty.scalarType(mod); |
| 1490 | 1512 | for (result_data, 0..) |*scalar, i| { |
| 1491 | const elem_val = try val.elemValue(mod, i); | |
| 1492 | scalar.* = (try floatFromIntScalar(elem_val, scalar_ty, mod, strat)).toIntern(); | |
| 1513 | const elem_val = try val.elemValue(pt, i); | |
| 1514 | scalar.* = (try floatFromIntScalar(elem_val, scalar_ty, pt, strat)).toIntern(); | |
| 1493 | 1515 | } |
| 1494 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 1516 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 1495 | 1517 | .ty = float_ty.toIntern(), |
| 1496 | 1518 | .storage = .{ .elems = result_data }, |
| 1497 | } }))); | |
| 1519 | } })); | |
| 1498 | 1520 | } |
| 1499 | return floatFromIntScalar(val, float_ty, mod, strat); | |
| 1521 | return floatFromIntScalar(val, float_ty, pt, strat); | |
| 1500 | 1522 | } |
| 1501 | 1523 | |
| 1502 | pub fn floatFromIntScalar(val: Value, float_ty: Type, mod: *Module, strat: ResolveStrat) !Value { | |
| 1524 | pub fn floatFromIntScalar(val: Value, float_ty: Type, pt: Zcu.PerThread, strat: ResolveStrat) !Value { | |
| 1525 | const mod = pt.zcu; | |
| 1503 | 1526 | return switch (mod.intern_pool.indexToKey(val.toIntern())) { |
| 1504 | .undef => try mod.undefValue(float_ty), | |
| 1527 | .undef => try pt.undefValue(float_ty), | |
| 1505 | 1528 | .int => |int| switch (int.storage) { |
| 1506 | 1529 | .big_int => |big_int| { |
| 1507 | 1530 | const float = bigIntToFloat(big_int.limbs, big_int.positive); |
| 1508 | return mod.floatValue(float_ty, float); | |
| 1531 | return pt.floatValue(float_ty, float); | |
| 1509 | 1532 | }, |
| 1510 | inline .u64, .i64 => |x| floatFromIntInner(x, float_ty, mod), | |
| 1511 | .lazy_align => |ty| return floatFromIntInner((try Type.fromInterned(ty).abiAlignmentAdvanced(mod, strat.toLazy())).scalar.toByteUnits() orelse 0, float_ty, mod), | |
| 1512 | .lazy_size => |ty| return floatFromIntInner((try Type.fromInterned(ty).abiSizeAdvanced(mod, strat.toLazy())).scalar, float_ty, mod), | |
| 1533 | inline .u64, .i64 => |x| floatFromIntInner(x, float_ty, pt), | |
| 1534 | .lazy_align => |ty| return floatFromIntInner((try Type.fromInterned(ty).abiAlignmentAdvanced(pt, strat.toLazy())).scalar.toByteUnits() orelse 0, float_ty, pt), | |
| 1535 | .lazy_size => |ty| return floatFromIntInner((try Type.fromInterned(ty).abiSizeAdvanced(pt, strat.toLazy())).scalar, float_ty, pt), | |
| 1513 | 1536 | }, |
| 1514 | 1537 | else => unreachable, |
| 1515 | 1538 | }; |
| 1516 | 1539 | } |
| 1517 | 1540 | |
| 1518 | fn floatFromIntInner(x: anytype, dest_ty: Type, mod: *Module) !Value { | |
| 1519 | const target = mod.getTarget(); | |
| 1541 | fn floatFromIntInner(x: anytype, dest_ty: Type, pt: Zcu.PerThread) !Value { | |
| 1542 | const target = pt.zcu.getTarget(); | |
| 1520 | 1543 | const storage: InternPool.Key.Float.Storage = switch (dest_ty.floatBits(target)) { |
| 1521 | 1544 | 16 => .{ .f16 = @floatFromInt(x) }, |
| 1522 | 1545 | 32 => .{ .f32 = @floatFromInt(x) }, |
| ... | ... | @@ -1525,10 +1548,10 @@ fn floatFromIntInner(x: anytype, dest_ty: Type, mod: *Module) !Value { |
| 1525 | 1548 | 128 => .{ .f128 = @floatFromInt(x) }, |
| 1526 | 1549 | else => unreachable, |
| 1527 | 1550 | }; |
| 1528 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 1551 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 1529 | 1552 | .ty = dest_ty.toIntern(), |
| 1530 | 1553 | .storage = storage, |
| 1531 | } }))); | |
| 1554 | } })); | |
| 1532 | 1555 | } |
| 1533 | 1556 | |
| 1534 | 1557 | fn calcLimbLenFloat(scalar: anytype) usize { |
| ... | ... | @@ -1551,22 +1574,22 @@ pub fn intAddSat( |
| 1551 | 1574 | rhs: Value, |
| 1552 | 1575 | ty: Type, |
| 1553 | 1576 | arena: Allocator, |
| 1554 | mod: *Module, | |
| 1577 | pt: Zcu.PerThread, | |
| 1555 | 1578 | ) !Value { |
| 1556 | if (ty.zigTypeTag(mod) == .Vector) { | |
| 1557 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(mod)); | |
| 1558 | const scalar_ty = ty.scalarType(mod); | |
| 1579 | if (ty.zigTypeTag(pt.zcu) == .Vector) { | |
| 1580 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(pt.zcu)); | |
| 1581 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 1559 | 1582 | for (result_data, 0..) |*scalar, i| { |
| 1560 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 1561 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 1562 | scalar.* = (try intAddSatScalar(lhs_elem, rhs_elem, scalar_ty, arena, mod)).toIntern(); | |
| 1583 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 1584 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 1585 | scalar.* = (try intAddSatScalar(lhs_elem, rhs_elem, scalar_ty, arena, pt)).toIntern(); | |
| 1563 | 1586 | } |
| 1564 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 1587 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 1565 | 1588 | .ty = ty.toIntern(), |
| 1566 | 1589 | .storage = .{ .elems = result_data }, |
| 1567 | } }))); | |
| 1590 | } })); | |
| 1568 | 1591 | } |
| 1569 | return intAddSatScalar(lhs, rhs, ty, arena, mod); | |
| 1592 | return intAddSatScalar(lhs, rhs, ty, arena, pt); | |
| 1570 | 1593 | } |
| 1571 | 1594 | |
| 1572 | 1595 | /// Supports integers only; asserts neither operand is undefined. |
| ... | ... | @@ -1575,24 +1598,24 @@ pub fn intAddSatScalar( |
| 1575 | 1598 | rhs: Value, |
| 1576 | 1599 | ty: Type, |
| 1577 | 1600 | arena: Allocator, |
| 1578 | mod: *Module, | |
| 1601 | pt: Zcu.PerThread, | |
| 1579 | 1602 | ) !Value { |
| 1580 | assert(!lhs.isUndef(mod)); | |
| 1581 | assert(!rhs.isUndef(mod)); | |
| 1603 | assert(!lhs.isUndef(pt.zcu)); | |
| 1604 | assert(!rhs.isUndef(pt.zcu)); | |
| 1582 | 1605 | |
| 1583 | const info = ty.intInfo(mod); | |
| 1606 | const info = ty.intInfo(pt.zcu); | |
| 1584 | 1607 | |
| 1585 | 1608 | var lhs_space: Value.BigIntSpace = undefined; |
| 1586 | 1609 | var rhs_space: Value.BigIntSpace = undefined; |
| 1587 | const lhs_bigint = lhs.toBigInt(&lhs_space, mod); | |
| 1588 | const rhs_bigint = rhs.toBigInt(&rhs_space, mod); | |
| 1610 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 1611 | const rhs_bigint = rhs.toBigInt(&rhs_space, pt); | |
| 1589 | 1612 | const limbs = try arena.alloc( |
| 1590 | 1613 | std.math.big.Limb, |
| 1591 | 1614 | std.math.big.int.calcTwosCompLimbCount(info.bits), |
| 1592 | 1615 | ); |
| 1593 | 1616 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 1594 | 1617 | result_bigint.addSat(lhs_bigint, rhs_bigint, info.signedness, info.bits); |
| 1595 | return mod.intValue_big(ty, result_bigint.toConst()); | |
| 1618 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 1596 | 1619 | } |
| 1597 | 1620 | |
| 1598 | 1621 | /// Supports (vectors of) integers only; asserts neither operand is undefined. |
| ... | ... | @@ -1601,22 +1624,22 @@ pub fn intSubSat( |
| 1601 | 1624 | rhs: Value, |
| 1602 | 1625 | ty: Type, |
| 1603 | 1626 | arena: Allocator, |
| 1604 | mod: *Module, | |
| 1627 | pt: Zcu.PerThread, | |
| 1605 | 1628 | ) !Value { |
| 1606 | if (ty.zigTypeTag(mod) == .Vector) { | |
| 1607 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(mod)); | |
| 1608 | const scalar_ty = ty.scalarType(mod); | |
| 1629 | if (ty.zigTypeTag(pt.zcu) == .Vector) { | |
| 1630 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(pt.zcu)); | |
| 1631 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 1609 | 1632 | for (result_data, 0..) |*scalar, i| { |
| 1610 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 1611 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 1612 | scalar.* = (try intSubSatScalar(lhs_elem, rhs_elem, scalar_ty, arena, mod)).toIntern(); | |
| 1633 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 1634 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 1635 | scalar.* = (try intSubSatScalar(lhs_elem, rhs_elem, scalar_ty, arena, pt)).toIntern(); | |
| 1613 | 1636 | } |
| 1614 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 1637 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 1615 | 1638 | .ty = ty.toIntern(), |
| 1616 | 1639 | .storage = .{ .elems = result_data }, |
| 1617 | } }))); | |
| 1640 | } })); | |
| 1618 | 1641 | } |
| 1619 | return intSubSatScalar(lhs, rhs, ty, arena, mod); | |
| 1642 | return intSubSatScalar(lhs, rhs, ty, arena, pt); | |
| 1620 | 1643 | } |
| 1621 | 1644 | |
| 1622 | 1645 | /// Supports integers only; asserts neither operand is undefined. |
| ... | ... | @@ -1625,24 +1648,24 @@ pub fn intSubSatScalar( |
| 1625 | 1648 | rhs: Value, |
| 1626 | 1649 | ty: Type, |
| 1627 | 1650 | arena: Allocator, |
| 1628 | mod: *Module, | |
| 1651 | pt: Zcu.PerThread, | |
| 1629 | 1652 | ) !Value { |
| 1630 | assert(!lhs.isUndef(mod)); | |
| 1631 | assert(!rhs.isUndef(mod)); | |
| 1653 | assert(!lhs.isUndef(pt.zcu)); | |
| 1654 | assert(!rhs.isUndef(pt.zcu)); | |
| 1632 | 1655 | |
| 1633 | const info = ty.intInfo(mod); | |
| 1656 | const info = ty.intInfo(pt.zcu); | |
| 1634 | 1657 | |
| 1635 | 1658 | var lhs_space: Value.BigIntSpace = undefined; |
| 1636 | 1659 | var rhs_space: Value.BigIntSpace = undefined; |
| 1637 | const lhs_bigint = lhs.toBigInt(&lhs_space, mod); | |
| 1638 | const rhs_bigint = rhs.toBigInt(&rhs_space, mod); | |
| 1660 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 1661 | const rhs_bigint = rhs.toBigInt(&rhs_space, pt); | |
| 1639 | 1662 | const limbs = try arena.alloc( |
| 1640 | 1663 | std.math.big.Limb, |
| 1641 | 1664 | std.math.big.int.calcTwosCompLimbCount(info.bits), |
| 1642 | 1665 | ); |
| 1643 | 1666 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 1644 | 1667 | result_bigint.subSat(lhs_bigint, rhs_bigint, info.signedness, info.bits); |
| 1645 | return mod.intValue_big(ty, result_bigint.toConst()); | |
| 1668 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 1646 | 1669 | } |
| 1647 | 1670 | |
| 1648 | 1671 | pub fn intMulWithOverflow( |
| ... | ... | @@ -1650,32 +1673,33 @@ pub fn intMulWithOverflow( |
| 1650 | 1673 | rhs: Value, |
| 1651 | 1674 | ty: Type, |
| 1652 | 1675 | arena: Allocator, |
| 1653 | mod: *Module, | |
| 1676 | pt: Zcu.PerThread, | |
| 1654 | 1677 | ) !OverflowArithmeticResult { |
| 1678 | const mod = pt.zcu; | |
| 1655 | 1679 | if (ty.zigTypeTag(mod) == .Vector) { |
| 1656 | 1680 | const vec_len = ty.vectorLen(mod); |
| 1657 | 1681 | const overflowed_data = try arena.alloc(InternPool.Index, vec_len); |
| 1658 | 1682 | const result_data = try arena.alloc(InternPool.Index, vec_len); |
| 1659 | 1683 | const scalar_ty = ty.scalarType(mod); |
| 1660 | 1684 | for (overflowed_data, result_data, 0..) |*of, *scalar, i| { |
| 1661 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 1662 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 1663 | const of_math_result = try intMulWithOverflowScalar(lhs_elem, rhs_elem, scalar_ty, arena, mod); | |
| 1685 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 1686 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 1687 | const of_math_result = try intMulWithOverflowScalar(lhs_elem, rhs_elem, scalar_ty, arena, pt); | |
| 1664 | 1688 | of.* = of_math_result.overflow_bit.toIntern(); |
| 1665 | 1689 | scalar.* = of_math_result.wrapped_result.toIntern(); |
| 1666 | 1690 | } |
| 1667 | 1691 | return OverflowArithmeticResult{ |
| 1668 | .overflow_bit = Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 1669 | .ty = (try mod.vectorType(.{ .len = vec_len, .child = .u1_type })).toIntern(), | |
| 1692 | .overflow_bit = Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 1693 | .ty = (try pt.vectorType(.{ .len = vec_len, .child = .u1_type })).toIntern(), | |
| 1670 | 1694 | .storage = .{ .elems = overflowed_data }, |
| 1671 | } }))), | |
| 1672 | .wrapped_result = Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 1695 | } })), | |
| 1696 | .wrapped_result = Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 1673 | 1697 | .ty = ty.toIntern(), |
| 1674 | 1698 | .storage = .{ .elems = result_data }, |
| 1675 | } }))), | |
| 1699 | } })), | |
| 1676 | 1700 | }; |
| 1677 | 1701 | } |
| 1678 | return intMulWithOverflowScalar(lhs, rhs, ty, arena, mod); | |
| 1702 | return intMulWithOverflowScalar(lhs, rhs, ty, arena, pt); | |
| 1679 | 1703 | } |
| 1680 | 1704 | |
| 1681 | 1705 | pub fn intMulWithOverflowScalar( |
| ... | ... | @@ -1683,21 +1707,22 @@ pub fn intMulWithOverflowScalar( |
| 1683 | 1707 | rhs: Value, |
| 1684 | 1708 | ty: Type, |
| 1685 | 1709 | arena: Allocator, |
| 1686 | mod: *Module, | |
| 1710 | pt: Zcu.PerThread, | |
| 1687 | 1711 | ) !OverflowArithmeticResult { |
| 1712 | const mod = pt.zcu; | |
| 1688 | 1713 | const info = ty.intInfo(mod); |
| 1689 | 1714 | |
| 1690 | 1715 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) { |
| 1691 | 1716 | return .{ |
| 1692 | .overflow_bit = try mod.undefValue(Type.u1), | |
| 1693 | .wrapped_result = try mod.undefValue(ty), | |
| 1717 | .overflow_bit = try pt.undefValue(Type.u1), | |
| 1718 | .wrapped_result = try pt.undefValue(ty), | |
| 1694 | 1719 | }; |
| 1695 | 1720 | } |
| 1696 | 1721 | |
| 1697 | 1722 | var lhs_space: Value.BigIntSpace = undefined; |
| 1698 | 1723 | var rhs_space: Value.BigIntSpace = undefined; |
| 1699 | const lhs_bigint = lhs.toBigInt(&lhs_space, mod); | |
| 1700 | const rhs_bigint = rhs.toBigInt(&rhs_space, mod); | |
| 1724 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 1725 | const rhs_bigint = rhs.toBigInt(&rhs_space, pt); | |
| 1701 | 1726 | const limbs = try arena.alloc( |
| 1702 | 1727 | std.math.big.Limb, |
| 1703 | 1728 | lhs_bigint.limbs.len + rhs_bigint.limbs.len, |
| ... | ... | @@ -1715,8 +1740,8 @@ pub fn intMulWithOverflowScalar( |
| 1715 | 1740 | } |
| 1716 | 1741 | |
| 1717 | 1742 | return OverflowArithmeticResult{ |
| 1718 | .overflow_bit = try mod.intValue(Type.u1, @intFromBool(overflowed)), | |
| 1719 | .wrapped_result = try mod.intValue_big(ty, result_bigint.toConst()), | |
| 1743 | .overflow_bit = try pt.intValue(Type.u1, @intFromBool(overflowed)), | |
| 1744 | .wrapped_result = try pt.intValue_big(ty, result_bigint.toConst()), | |
| 1720 | 1745 | }; |
| 1721 | 1746 | } |
| 1722 | 1747 | |
| ... | ... | @@ -1726,22 +1751,23 @@ pub fn numberMulWrap( |
| 1726 | 1751 | rhs: Value, |
| 1727 | 1752 | ty: Type, |
| 1728 | 1753 | arena: Allocator, |
| 1729 | mod: *Module, | |
| 1754 | pt: Zcu.PerThread, | |
| 1730 | 1755 | ) !Value { |
| 1756 | const mod = pt.zcu; | |
| 1731 | 1757 | if (ty.zigTypeTag(mod) == .Vector) { |
| 1732 | 1758 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(mod)); |
| 1733 | 1759 | const scalar_ty = ty.scalarType(mod); |
| 1734 | 1760 | for (result_data, 0..) |*scalar, i| { |
| 1735 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 1736 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 1737 | scalar.* = (try numberMulWrapScalar(lhs_elem, rhs_elem, scalar_ty, arena, mod)).toIntern(); | |
| 1761 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 1762 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 1763 | scalar.* = (try numberMulWrapScalar(lhs_elem, rhs_elem, scalar_ty, arena, pt)).toIntern(); | |
| 1738 | 1764 | } |
| 1739 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 1765 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 1740 | 1766 | .ty = ty.toIntern(), |
| 1741 | 1767 | .storage = .{ .elems = result_data }, |
| 1742 | } }))); | |
| 1768 | } })); | |
| 1743 | 1769 | } |
| 1744 | return numberMulWrapScalar(lhs, rhs, ty, arena, mod); | |
| 1770 | return numberMulWrapScalar(lhs, rhs, ty, arena, pt); | |
| 1745 | 1771 | } |
| 1746 | 1772 | |
| 1747 | 1773 | /// Supports both floats and ints; handles undefined. |
| ... | ... | @@ -1750,19 +1776,20 @@ pub fn numberMulWrapScalar( |
| 1750 | 1776 | rhs: Value, |
| 1751 | 1777 | ty: Type, |
| 1752 | 1778 | arena: Allocator, |
| 1753 | mod: *Module, | |
| 1779 | pt: Zcu.PerThread, | |
| 1754 | 1780 | ) !Value { |
| 1781 | const mod = pt.zcu; | |
| 1755 | 1782 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) return Value.undef; |
| 1756 | 1783 | |
| 1757 | 1784 | if (ty.zigTypeTag(mod) == .ComptimeInt) { |
| 1758 | return intMul(lhs, rhs, ty, undefined, arena, mod); | |
| 1785 | return intMul(lhs, rhs, ty, undefined, arena, pt); | |
| 1759 | 1786 | } |
| 1760 | 1787 | |
| 1761 | 1788 | if (ty.isAnyFloat()) { |
| 1762 | return floatMul(lhs, rhs, ty, arena, mod); | |
| 1789 | return floatMul(lhs, rhs, ty, arena, pt); | |
| 1763 | 1790 | } |
| 1764 | 1791 | |
| 1765 | const overflow_result = try intMulWithOverflow(lhs, rhs, ty, arena, mod); | |
| 1792 | const overflow_result = try intMulWithOverflow(lhs, rhs, ty, arena, pt); | |
| 1766 | 1793 | return overflow_result.wrapped_result; |
| 1767 | 1794 | } |
| 1768 | 1795 | |
| ... | ... | @@ -1772,22 +1799,22 @@ pub fn intMulSat( |
| 1772 | 1799 | rhs: Value, |
| 1773 | 1800 | ty: Type, |
| 1774 | 1801 | arena: Allocator, |
| 1775 | mod: *Module, | |
| 1802 | pt: Zcu.PerThread, | |
| 1776 | 1803 | ) !Value { |
| 1777 | if (ty.zigTypeTag(mod) == .Vector) { | |
| 1778 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(mod)); | |
| 1779 | const scalar_ty = ty.scalarType(mod); | |
| 1804 | if (ty.zigTypeTag(pt.zcu) == .Vector) { | |
| 1805 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(pt.zcu)); | |
| 1806 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 1780 | 1807 | for (result_data, 0..) |*scalar, i| { |
| 1781 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 1782 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 1783 | scalar.* = (try intMulSatScalar(lhs_elem, rhs_elem, scalar_ty, arena, mod)).toIntern(); | |
| 1808 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 1809 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 1810 | scalar.* = (try intMulSatScalar(lhs_elem, rhs_elem, scalar_ty, arena, pt)).toIntern(); | |
| 1784 | 1811 | } |
| 1785 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 1812 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 1786 | 1813 | .ty = ty.toIntern(), |
| 1787 | 1814 | .storage = .{ .elems = result_data }, |
| 1788 | } }))); | |
| 1815 | } })); | |
| 1789 | 1816 | } |
| 1790 | return intMulSatScalar(lhs, rhs, ty, arena, mod); | |
| 1817 | return intMulSatScalar(lhs, rhs, ty, arena, pt); | |
| 1791 | 1818 | } |
| 1792 | 1819 | |
| 1793 | 1820 | /// Supports (vectors of) integers only; asserts neither operand is undefined. |
| ... | ... | @@ -1796,17 +1823,17 @@ pub fn intMulSatScalar( |
| 1796 | 1823 | rhs: Value, |
| 1797 | 1824 | ty: Type, |
| 1798 | 1825 | arena: Allocator, |
| 1799 | mod: *Module, | |
| 1826 | pt: Zcu.PerThread, | |
| 1800 | 1827 | ) !Value { |
| 1801 | assert(!lhs.isUndef(mod)); | |
| 1802 | assert(!rhs.isUndef(mod)); | |
| 1828 | assert(!lhs.isUndef(pt.zcu)); | |
| 1829 | assert(!rhs.isUndef(pt.zcu)); | |
| 1803 | 1830 | |
| 1804 | const info = ty.intInfo(mod); | |
| 1831 | const info = ty.intInfo(pt.zcu); | |
| 1805 | 1832 | |
| 1806 | 1833 | var lhs_space: Value.BigIntSpace = undefined; |
| 1807 | 1834 | var rhs_space: Value.BigIntSpace = undefined; |
| 1808 | const lhs_bigint = lhs.toBigInt(&lhs_space, mod); | |
| 1809 | const rhs_bigint = rhs.toBigInt(&rhs_space, mod); | |
| 1835 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 1836 | const rhs_bigint = rhs.toBigInt(&rhs_space, pt); | |
| 1810 | 1837 | const limbs = try arena.alloc( |
| 1811 | 1838 | std.math.big.Limb, |
| 1812 | 1839 | @max( |
| ... | ... | @@ -1822,53 +1849,55 @@ pub fn intMulSatScalar( |
| 1822 | 1849 | ); |
| 1823 | 1850 | result_bigint.mul(lhs_bigint, rhs_bigint, limbs_buffer, arena); |
| 1824 | 1851 | result_bigint.saturate(result_bigint.toConst(), info.signedness, info.bits); |
| 1825 | return mod.intValue_big(ty, result_bigint.toConst()); | |
| 1852 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 1826 | 1853 | } |
| 1827 | 1854 | |
| 1828 | 1855 | /// Supports both floats and ints; handles undefined. |
| 1829 | pub fn numberMax(lhs: Value, rhs: Value, mod: *Module) Value { | |
| 1830 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) return undef; | |
| 1831 | if (lhs.isNan(mod)) return rhs; | |
| 1832 | if (rhs.isNan(mod)) return lhs; | |
| 1856 | pub fn numberMax(lhs: Value, rhs: Value, pt: Zcu.PerThread) Value { | |
| 1857 | if (lhs.isUndef(pt.zcu) or rhs.isUndef(pt.zcu)) return undef; | |
| 1858 | if (lhs.isNan(pt.zcu)) return rhs; | |
| 1859 | if (rhs.isNan(pt.zcu)) return lhs; | |
| 1833 | 1860 | |
| 1834 | return switch (order(lhs, rhs, mod)) { | |
| 1861 | return switch (order(lhs, rhs, pt)) { | |
| 1835 | 1862 | .lt => rhs, |
| 1836 | 1863 | .gt, .eq => lhs, |
| 1837 | 1864 | }; |
| 1838 | 1865 | } |
| 1839 | 1866 | |
| 1840 | 1867 | /// Supports both floats and ints; handles undefined. |
| 1841 | pub fn numberMin(lhs: Value, rhs: Value, mod: *Module) Value { | |
| 1842 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) return undef; | |
| 1843 | if (lhs.isNan(mod)) return rhs; | |
| 1844 | if (rhs.isNan(mod)) return lhs; | |
| 1868 | pub fn numberMin(lhs: Value, rhs: Value, pt: Zcu.PerThread) Value { | |
| 1869 | if (lhs.isUndef(pt.zcu) or rhs.isUndef(pt.zcu)) return undef; | |
| 1870 | if (lhs.isNan(pt.zcu)) return rhs; | |
| 1871 | if (rhs.isNan(pt.zcu)) return lhs; | |
| 1845 | 1872 | |
| 1846 | return switch (order(lhs, rhs, mod)) { | |
| 1873 | return switch (order(lhs, rhs, pt)) { | |
| 1847 | 1874 | .lt => lhs, |
| 1848 | 1875 | .gt, .eq => rhs, |
| 1849 | 1876 | }; |
| 1850 | 1877 | } |
| 1851 | 1878 | |
| 1852 | 1879 | /// operands must be (vectors of) integers; handles undefined scalars. |
| 1853 | pub fn bitwiseNot(val: Value, ty: Type, arena: Allocator, mod: *Module) !Value { | |
| 1880 | pub fn bitwiseNot(val: Value, ty: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 1881 | const mod = pt.zcu; | |
| 1854 | 1882 | if (ty.zigTypeTag(mod) == .Vector) { |
| 1855 | 1883 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(mod)); |
| 1856 | 1884 | const scalar_ty = ty.scalarType(mod); |
| 1857 | 1885 | for (result_data, 0..) |*scalar, i| { |
| 1858 | const elem_val = try val.elemValue(mod, i); | |
| 1859 | scalar.* = (try bitwiseNotScalar(elem_val, scalar_ty, arena, mod)).toIntern(); | |
| 1886 | const elem_val = try val.elemValue(pt, i); | |
| 1887 | scalar.* = (try bitwiseNotScalar(elem_val, scalar_ty, arena, pt)).toIntern(); | |
| 1860 | 1888 | } |
| 1861 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 1889 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 1862 | 1890 | .ty = ty.toIntern(), |
| 1863 | 1891 | .storage = .{ .elems = result_data }, |
| 1864 | } }))); | |
| 1892 | } })); | |
| 1865 | 1893 | } |
| 1866 | return bitwiseNotScalar(val, ty, arena, mod); | |
| 1894 | return bitwiseNotScalar(val, ty, arena, pt); | |
| 1867 | 1895 | } |
| 1868 | 1896 | |
| 1869 | 1897 | /// operands must be integers; handles undefined. |
| 1870 | pub fn bitwiseNotScalar(val: Value, ty: Type, arena: Allocator, mod: *Module) !Value { | |
| 1871 | if (val.isUndef(mod)) return Value.fromInterned((try mod.intern(.{ .undef = ty.toIntern() }))); | |
| 1898 | pub fn bitwiseNotScalar(val: Value, ty: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 1899 | const mod = pt.zcu; | |
| 1900 | if (val.isUndef(mod)) return Value.fromInterned(try pt.intern(.{ .undef = ty.toIntern() })); | |
| 1872 | 1901 | if (ty.toIntern() == .bool_type) return makeBool(!val.toBool()); |
| 1873 | 1902 | |
| 1874 | 1903 | const info = ty.intInfo(mod); |
| ... | ... | @@ -1880,7 +1909,7 @@ pub fn bitwiseNotScalar(val: Value, ty: Type, arena: Allocator, mod: *Module) !V |
| 1880 | 1909 | // TODO is this a performance issue? maybe we should try the operation without |
| 1881 | 1910 | // resorting to BigInt first. |
| 1882 | 1911 | var val_space: Value.BigIntSpace = undefined; |
| 1883 | const val_bigint = val.toBigInt(&val_space, mod); | |
| 1912 | const val_bigint = val.toBigInt(&val_space, pt); | |
| 1884 | 1913 | const limbs = try arena.alloc( |
| 1885 | 1914 | std.math.big.Limb, |
| 1886 | 1915 | std.math.big.int.calcTwosCompLimbCount(info.bits), |
| ... | ... | @@ -1888,29 +1917,31 @@ pub fn bitwiseNotScalar(val: Value, ty: Type, arena: Allocator, mod: *Module) !V |
| 1888 | 1917 | |
| 1889 | 1918 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 1890 | 1919 | result_bigint.bitNotWrap(val_bigint, info.signedness, info.bits); |
| 1891 | return mod.intValue_big(ty, result_bigint.toConst()); | |
| 1920 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 1892 | 1921 | } |
| 1893 | 1922 | |
| 1894 | 1923 | /// operands must be (vectors of) integers; handles undefined scalars. |
| 1895 | pub fn bitwiseAnd(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *Module) !Value { | |
| 1924 | pub fn bitwiseAnd(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 1925 | const mod = pt.zcu; | |
| 1896 | 1926 | if (ty.zigTypeTag(mod) == .Vector) { |
| 1897 | 1927 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(mod)); |
| 1898 | 1928 | const scalar_ty = ty.scalarType(mod); |
| 1899 | 1929 | for (result_data, 0..) |*scalar, i| { |
| 1900 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 1901 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 1902 | scalar.* = (try bitwiseAndScalar(lhs_elem, rhs_elem, scalar_ty, allocator, mod)).toIntern(); | |
| 1930 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 1931 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 1932 | scalar.* = (try bitwiseAndScalar(lhs_elem, rhs_elem, scalar_ty, allocator, pt)).toIntern(); | |
| 1903 | 1933 | } |
| 1904 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 1934 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 1905 | 1935 | .ty = ty.toIntern(), |
| 1906 | 1936 | .storage = .{ .elems = result_data }, |
| 1907 | } }))); | |
| 1937 | } })); | |
| 1908 | 1938 | } |
| 1909 | return bitwiseAndScalar(lhs, rhs, ty, allocator, mod); | |
| 1939 | return bitwiseAndScalar(lhs, rhs, ty, allocator, pt); | |
| 1910 | 1940 | } |
| 1911 | 1941 | |
| 1912 | 1942 | /// operands must be integers; handles undefined. |
| 1913 | pub fn bitwiseAndScalar(orig_lhs: Value, orig_rhs: Value, ty: Type, arena: Allocator, zcu: *Zcu) !Value { | |
| 1943 | pub fn bitwiseAndScalar(orig_lhs: Value, orig_rhs: Value, ty: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 1944 | const zcu = pt.zcu; | |
| 1914 | 1945 | // If one operand is defined, we turn the other into `0xAA` so the bitwise AND can |
| 1915 | 1946 | // still zero out some bits. |
| 1916 | 1947 | // TODO: ideally we'd still like tracking for the undef bits. Related: #19634. |
| ... | ... | @@ -1919,9 +1950,9 @@ pub fn bitwiseAndScalar(orig_lhs: Value, orig_rhs: Value, ty: Type, arena: Alloc |
| 1919 | 1950 | const rhs_undef = orig_rhs.isUndef(zcu); |
| 1920 | 1951 | break :make_defined switch ((@as(u2, @intFromBool(lhs_undef)) << 1) | @intFromBool(rhs_undef)) { |
| 1921 | 1952 | 0b00 => .{ orig_lhs, orig_rhs }, |
| 1922 | 0b01 => .{ orig_lhs, try intValueAa(ty, arena, zcu) }, | |
| 1923 | 0b10 => .{ try intValueAa(ty, arena, zcu), orig_rhs }, | |
| 1924 | 0b11 => return zcu.undefValue(ty), | |
| 1953 | 0b01 => .{ orig_lhs, try intValueAa(ty, arena, pt) }, | |
| 1954 | 0b10 => .{ try intValueAa(ty, arena, pt), orig_rhs }, | |
| 1955 | 0b11 => return pt.undefValue(ty), | |
| 1925 | 1956 | }; |
| 1926 | 1957 | }; |
| 1927 | 1958 | |
| ... | ... | @@ -1931,8 +1962,8 @@ pub fn bitwiseAndScalar(orig_lhs: Value, orig_rhs: Value, ty: Type, arena: Alloc |
| 1931 | 1962 | // resorting to BigInt first. |
| 1932 | 1963 | var lhs_space: Value.BigIntSpace = undefined; |
| 1933 | 1964 | var rhs_space: Value.BigIntSpace = undefined; |
| 1934 | const lhs_bigint = lhs.toBigInt(&lhs_space, zcu); | |
| 1935 | const rhs_bigint = rhs.toBigInt(&rhs_space, zcu); | |
| 1965 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 1966 | const rhs_bigint = rhs.toBigInt(&rhs_space, pt); | |
| 1936 | 1967 | const limbs = try arena.alloc( |
| 1937 | 1968 | std.math.big.Limb, |
| 1938 | 1969 | // + 1 for negatives |
| ... | ... | @@ -1940,12 +1971,13 @@ pub fn bitwiseAndScalar(orig_lhs: Value, orig_rhs: Value, ty: Type, arena: Alloc |
| 1940 | 1971 | ); |
| 1941 | 1972 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 1942 | 1973 | result_bigint.bitAnd(lhs_bigint, rhs_bigint); |
| 1943 | return zcu.intValue_big(ty, result_bigint.toConst()); | |
| 1974 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 1944 | 1975 | } |
| 1945 | 1976 | |
| 1946 | 1977 | /// Given an integer or boolean type, creates an value of that with the bit pattern 0xAA. |
| 1947 | 1978 | /// This is used to convert undef values into 0xAA when performing e.g. bitwise operations. |
| 1948 | fn intValueAa(ty: Type, arena: Allocator, zcu: *Zcu) !Value { | |
| 1979 | fn intValueAa(ty: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 1980 | const zcu = pt.zcu; | |
| 1949 | 1981 | if (ty.toIntern() == .bool_type) return Value.true; |
| 1950 | 1982 | const info = ty.intInfo(zcu); |
| 1951 | 1983 | |
| ... | ... | @@ -1958,68 +1990,71 @@ fn intValueAa(ty: Type, arena: Allocator, zcu: *Zcu) !Value { |
| 1958 | 1990 | ); |
| 1959 | 1991 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 1960 | 1992 | result_bigint.readTwosComplement(buf, info.bits, zcu.getTarget().cpu.arch.endian(), info.signedness); |
| 1961 | return zcu.intValue_big(ty, result_bigint.toConst()); | |
| 1993 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 1962 | 1994 | } |
| 1963 | 1995 | |
| 1964 | 1996 | /// operands must be (vectors of) integers; handles undefined scalars. |
| 1965 | pub fn bitwiseNand(lhs: Value, rhs: Value, ty: Type, arena: Allocator, mod: *Module) !Value { | |
| 1997 | pub fn bitwiseNand(lhs: Value, rhs: Value, ty: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 1998 | const mod = pt.zcu; | |
| 1966 | 1999 | if (ty.zigTypeTag(mod) == .Vector) { |
| 1967 | 2000 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(mod)); |
| 1968 | 2001 | const scalar_ty = ty.scalarType(mod); |
| 1969 | 2002 | for (result_data, 0..) |*scalar, i| { |
| 1970 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 1971 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 1972 | scalar.* = (try bitwiseNandScalar(lhs_elem, rhs_elem, scalar_ty, arena, mod)).toIntern(); | |
| 2003 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2004 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2005 | scalar.* = (try bitwiseNandScalar(lhs_elem, rhs_elem, scalar_ty, arena, pt)).toIntern(); | |
| 1973 | 2006 | } |
| 1974 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2007 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 1975 | 2008 | .ty = ty.toIntern(), |
| 1976 | 2009 | .storage = .{ .elems = result_data }, |
| 1977 | } }))); | |
| 2010 | } })); | |
| 1978 | 2011 | } |
| 1979 | return bitwiseNandScalar(lhs, rhs, ty, arena, mod); | |
| 2012 | return bitwiseNandScalar(lhs, rhs, ty, arena, pt); | |
| 1980 | 2013 | } |
| 1981 | 2014 | |
| 1982 | 2015 | /// operands must be integers; handles undefined. |
| 1983 | pub fn bitwiseNandScalar(lhs: Value, rhs: Value, ty: Type, arena: Allocator, mod: *Module) !Value { | |
| 1984 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) return Value.fromInterned((try mod.intern(.{ .undef = ty.toIntern() }))); | |
| 2016 | pub fn bitwiseNandScalar(lhs: Value, rhs: Value, ty: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 2017 | const mod = pt.zcu; | |
| 2018 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) return Value.fromInterned(try pt.intern(.{ .undef = ty.toIntern() })); | |
| 1985 | 2019 | if (ty.toIntern() == .bool_type) return makeBool(!(lhs.toBool() and rhs.toBool())); |
| 1986 | 2020 | |
| 1987 | const anded = try bitwiseAnd(lhs, rhs, ty, arena, mod); | |
| 1988 | const all_ones = if (ty.isSignedInt(mod)) try mod.intValue(ty, -1) else try ty.maxIntScalar(mod, ty); | |
| 1989 | return bitwiseXor(anded, all_ones, ty, arena, mod); | |
| 2021 | const anded = try bitwiseAnd(lhs, rhs, ty, arena, pt); | |
| 2022 | const all_ones = if (ty.isSignedInt(mod)) try pt.intValue(ty, -1) else try ty.maxIntScalar(pt, ty); | |
| 2023 | return bitwiseXor(anded, all_ones, ty, arena, pt); | |
| 1990 | 2024 | } |
| 1991 | 2025 | |
| 1992 | 2026 | /// operands must be (vectors of) integers; handles undefined scalars. |
| 1993 | pub fn bitwiseOr(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *Module) !Value { | |
| 2027 | pub fn bitwiseOr(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2028 | const mod = pt.zcu; | |
| 1994 | 2029 | if (ty.zigTypeTag(mod) == .Vector) { |
| 1995 | 2030 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(mod)); |
| 1996 | 2031 | const scalar_ty = ty.scalarType(mod); |
| 1997 | 2032 | for (result_data, 0..) |*scalar, i| { |
| 1998 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 1999 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2000 | scalar.* = (try bitwiseOrScalar(lhs_elem, rhs_elem, scalar_ty, allocator, mod)).toIntern(); | |
| 2033 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2034 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2035 | scalar.* = (try bitwiseOrScalar(lhs_elem, rhs_elem, scalar_ty, allocator, pt)).toIntern(); | |
| 2001 | 2036 | } |
| 2002 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2037 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2003 | 2038 | .ty = ty.toIntern(), |
| 2004 | 2039 | .storage = .{ .elems = result_data }, |
| 2005 | } }))); | |
| 2040 | } })); | |
| 2006 | 2041 | } |
| 2007 | return bitwiseOrScalar(lhs, rhs, ty, allocator, mod); | |
| 2042 | return bitwiseOrScalar(lhs, rhs, ty, allocator, pt); | |
| 2008 | 2043 | } |
| 2009 | 2044 | |
| 2010 | 2045 | /// operands must be integers; handles undefined. |
| 2011 | pub fn bitwiseOrScalar(orig_lhs: Value, orig_rhs: Value, ty: Type, arena: Allocator, zcu: *Zcu) !Value { | |
| 2046 | pub fn bitwiseOrScalar(orig_lhs: Value, orig_rhs: Value, ty: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 2012 | 2047 | // If one operand is defined, we turn the other into `0xAA` so the bitwise AND can |
| 2013 | 2048 | // still zero out some bits. |
| 2014 | 2049 | // TODO: ideally we'd still like tracking for the undef bits. Related: #19634. |
| 2015 | 2050 | const lhs: Value, const rhs: Value = make_defined: { |
| 2016 | const lhs_undef = orig_lhs.isUndef(zcu); | |
| 2017 | const rhs_undef = orig_rhs.isUndef(zcu); | |
| 2051 | const lhs_undef = orig_lhs.isUndef(pt.zcu); | |
| 2052 | const rhs_undef = orig_rhs.isUndef(pt.zcu); | |
| 2018 | 2053 | break :make_defined switch ((@as(u2, @intFromBool(lhs_undef)) << 1) | @intFromBool(rhs_undef)) { |
| 2019 | 2054 | 0b00 => .{ orig_lhs, orig_rhs }, |
| 2020 | 0b01 => .{ orig_lhs, try intValueAa(ty, arena, zcu) }, | |
| 2021 | 0b10 => .{ try intValueAa(ty, arena, zcu), orig_rhs }, | |
| 2022 | 0b11 => return zcu.undefValue(ty), | |
| 2055 | 0b01 => .{ orig_lhs, try intValueAa(ty, arena, pt) }, | |
| 2056 | 0b10 => .{ try intValueAa(ty, arena, pt), orig_rhs }, | |
| 2057 | 0b11 => return pt.undefValue(ty), | |
| 2023 | 2058 | }; |
| 2024 | 2059 | }; |
| 2025 | 2060 | |
| ... | ... | @@ -2029,46 +2064,48 @@ pub fn bitwiseOrScalar(orig_lhs: Value, orig_rhs: Value, ty: Type, arena: Alloca |
| 2029 | 2064 | // resorting to BigInt first. |
| 2030 | 2065 | var lhs_space: Value.BigIntSpace = undefined; |
| 2031 | 2066 | var rhs_space: Value.BigIntSpace = undefined; |
| 2032 | const lhs_bigint = lhs.toBigInt(&lhs_space, zcu); | |
| 2033 | const rhs_bigint = rhs.toBigInt(&rhs_space, zcu); | |
| 2067 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 2068 | const rhs_bigint = rhs.toBigInt(&rhs_space, pt); | |
| 2034 | 2069 | const limbs = try arena.alloc( |
| 2035 | 2070 | std.math.big.Limb, |
| 2036 | 2071 | @max(lhs_bigint.limbs.len, rhs_bigint.limbs.len), |
| 2037 | 2072 | ); |
| 2038 | 2073 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 2039 | 2074 | result_bigint.bitOr(lhs_bigint, rhs_bigint); |
| 2040 | return zcu.intValue_big(ty, result_bigint.toConst()); | |
| 2075 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 2041 | 2076 | } |
| 2042 | 2077 | |
| 2043 | 2078 | /// operands must be (vectors of) integers; handles undefined scalars. |
| 2044 | pub fn bitwiseXor(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *Module) !Value { | |
| 2079 | pub fn bitwiseXor(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2080 | const mod = pt.zcu; | |
| 2045 | 2081 | if (ty.zigTypeTag(mod) == .Vector) { |
| 2046 | 2082 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(mod)); |
| 2047 | 2083 | const scalar_ty = ty.scalarType(mod); |
| 2048 | 2084 | for (result_data, 0..) |*scalar, i| { |
| 2049 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2050 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2051 | scalar.* = (try bitwiseXorScalar(lhs_elem, rhs_elem, scalar_ty, allocator, mod)).toIntern(); | |
| 2085 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2086 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2087 | scalar.* = (try bitwiseXorScalar(lhs_elem, rhs_elem, scalar_ty, allocator, pt)).toIntern(); | |
| 2052 | 2088 | } |
| 2053 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2089 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2054 | 2090 | .ty = ty.toIntern(), |
| 2055 | 2091 | .storage = .{ .elems = result_data }, |
| 2056 | } }))); | |
| 2092 | } })); | |
| 2057 | 2093 | } |
| 2058 | return bitwiseXorScalar(lhs, rhs, ty, allocator, mod); | |
| 2094 | return bitwiseXorScalar(lhs, rhs, ty, allocator, pt); | |
| 2059 | 2095 | } |
| 2060 | 2096 | |
| 2061 | 2097 | /// operands must be integers; handles undefined. |
| 2062 | pub fn bitwiseXorScalar(lhs: Value, rhs: Value, ty: Type, arena: Allocator, mod: *Module) !Value { | |
| 2063 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) return Value.fromInterned((try mod.intern(.{ .undef = ty.toIntern() }))); | |
| 2098 | pub fn bitwiseXorScalar(lhs: Value, rhs: Value, ty: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 2099 | const mod = pt.zcu; | |
| 2100 | if (lhs.isUndef(mod) or rhs.isUndef(mod)) return Value.fromInterned(try pt.intern(.{ .undef = ty.toIntern() })); | |
| 2064 | 2101 | if (ty.toIntern() == .bool_type) return makeBool(lhs.toBool() != rhs.toBool()); |
| 2065 | 2102 | |
| 2066 | 2103 | // TODO is this a performance issue? maybe we should try the operation without |
| 2067 | 2104 | // resorting to BigInt first. |
| 2068 | 2105 | var lhs_space: Value.BigIntSpace = undefined; |
| 2069 | 2106 | var rhs_space: Value.BigIntSpace = undefined; |
| 2070 | const lhs_bigint = lhs.toBigInt(&lhs_space, mod); | |
| 2071 | const rhs_bigint = rhs.toBigInt(&rhs_space, mod); | |
| 2107 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 2108 | const rhs_bigint = rhs.toBigInt(&rhs_space, pt); | |
| 2072 | 2109 | const limbs = try arena.alloc( |
| 2073 | 2110 | std.math.big.Limb, |
| 2074 | 2111 | // + 1 for negatives |
| ... | ... | @@ -2076,22 +2113,22 @@ pub fn bitwiseXorScalar(lhs: Value, rhs: Value, ty: Type, arena: Allocator, mod: |
| 2076 | 2113 | ); |
| 2077 | 2114 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 2078 | 2115 | result_bigint.bitXor(lhs_bigint, rhs_bigint); |
| 2079 | return mod.intValue_big(ty, result_bigint.toConst()); | |
| 2116 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 2080 | 2117 | } |
| 2081 | 2118 | |
| 2082 | 2119 | /// If the value overflowed the type, returns a comptime_int (or vector thereof) instead, setting |
| 2083 | 2120 | /// overflow_idx to the vector index the overflow was at (or 0 for a scalar). |
| 2084 | pub fn intDiv(lhs: Value, rhs: Value, ty: Type, overflow_idx: *?usize, allocator: Allocator, mod: *Module) !Value { | |
| 2121 | pub fn intDiv(lhs: Value, rhs: Value, ty: Type, overflow_idx: *?usize, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2085 | 2122 | var overflow: usize = undefined; |
| 2086 | return intDivInner(lhs, rhs, ty, &overflow, allocator, mod) catch |err| switch (err) { | |
| 2123 | return intDivInner(lhs, rhs, ty, &overflow, allocator, pt) catch |err| switch (err) { | |
| 2087 | 2124 | error.Overflow => { |
| 2088 | const is_vec = ty.isVector(mod); | |
| 2125 | const is_vec = ty.isVector(pt.zcu); | |
| 2089 | 2126 | overflow_idx.* = if (is_vec) overflow else 0; |
| 2090 | const safe_ty = if (is_vec) try mod.vectorType(.{ | |
| 2091 | .len = ty.vectorLen(mod), | |
| 2127 | const safe_ty = if (is_vec) try pt.vectorType(.{ | |
| 2128 | .len = ty.vectorLen(pt.zcu), | |
| 2092 | 2129 | .child = .comptime_int_type, |
| 2093 | 2130 | }) else Type.comptime_int; |
| 2094 | return intDivInner(lhs, rhs, safe_ty, undefined, allocator, mod) catch |err1| switch (err1) { | |
| 2131 | return intDivInner(lhs, rhs, safe_ty, undefined, allocator, pt) catch |err1| switch (err1) { | |
| 2095 | 2132 | error.Overflow => unreachable, |
| 2096 | 2133 | else => |e| return e, |
| 2097 | 2134 | }; |
| ... | ... | @@ -2100,14 +2137,14 @@ pub fn intDiv(lhs: Value, rhs: Value, ty: Type, overflow_idx: *?usize, allocator |
| 2100 | 2137 | }; |
| 2101 | 2138 | } |
| 2102 | 2139 | |
| 2103 | fn intDivInner(lhs: Value, rhs: Value, ty: Type, overflow_idx: *usize, allocator: Allocator, mod: *Module) !Value { | |
| 2104 | if (ty.zigTypeTag(mod) == .Vector) { | |
| 2105 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(mod)); | |
| 2106 | const scalar_ty = ty.scalarType(mod); | |
| 2140 | fn intDivInner(lhs: Value, rhs: Value, ty: Type, overflow_idx: *usize, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2141 | if (ty.zigTypeTag(pt.zcu) == .Vector) { | |
| 2142 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(pt.zcu)); | |
| 2143 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 2107 | 2144 | for (result_data, 0..) |*scalar, i| { |
| 2108 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2109 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2110 | const val = intDivScalar(lhs_elem, rhs_elem, scalar_ty, allocator, mod) catch |err| switch (err) { | |
| 2145 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2146 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2147 | const val = intDivScalar(lhs_elem, rhs_elem, scalar_ty, allocator, pt) catch |err| switch (err) { | |
| 2111 | 2148 | error.Overflow => { |
| 2112 | 2149 | overflow_idx.* = i; |
| 2113 | 2150 | return error.Overflow; |
| ... | ... | @@ -2116,21 +2153,21 @@ fn intDivInner(lhs: Value, rhs: Value, ty: Type, overflow_idx: *usize, allocator |
| 2116 | 2153 | }; |
| 2117 | 2154 | scalar.* = val.toIntern(); |
| 2118 | 2155 | } |
| 2119 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2156 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2120 | 2157 | .ty = ty.toIntern(), |
| 2121 | 2158 | .storage = .{ .elems = result_data }, |
| 2122 | } }))); | |
| 2159 | } })); | |
| 2123 | 2160 | } |
| 2124 | return intDivScalar(lhs, rhs, ty, allocator, mod); | |
| 2161 | return intDivScalar(lhs, rhs, ty, allocator, pt); | |
| 2125 | 2162 | } |
| 2126 | 2163 | |
| 2127 | pub fn intDivScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *Module) !Value { | |
| 2164 | pub fn intDivScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2128 | 2165 | // TODO is this a performance issue? maybe we should try the operation without |
| 2129 | 2166 | // resorting to BigInt first. |
| 2130 | 2167 | var lhs_space: Value.BigIntSpace = undefined; |
| 2131 | 2168 | var rhs_space: Value.BigIntSpace = undefined; |
| 2132 | const lhs_bigint = lhs.toBigInt(&lhs_space, mod); | |
| 2133 | const rhs_bigint = rhs.toBigInt(&rhs_space, mod); | |
| 2169 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 2170 | const rhs_bigint = rhs.toBigInt(&rhs_space, pt); | |
| 2134 | 2171 | const limbs_q = try allocator.alloc( |
| 2135 | 2172 | std.math.big.Limb, |
| 2136 | 2173 | lhs_bigint.limbs.len, |
| ... | ... | @@ -2147,38 +2184,38 @@ pub fn intDivScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: |
| 2147 | 2184 | var result_r = BigIntMutable{ .limbs = limbs_r, .positive = undefined, .len = undefined }; |
| 2148 | 2185 | result_q.divTrunc(&result_r, lhs_bigint, rhs_bigint, limbs_buffer); |
| 2149 | 2186 | if (ty.toIntern() != .comptime_int_type) { |
| 2150 | const info = ty.intInfo(mod); | |
| 2187 | const info = ty.intInfo(pt.zcu); | |
| 2151 | 2188 | if (!result_q.toConst().fitsInTwosComp(info.signedness, info.bits)) { |
| 2152 | 2189 | return error.Overflow; |
| 2153 | 2190 | } |
| 2154 | 2191 | } |
| 2155 | return mod.intValue_big(ty, result_q.toConst()); | |
| 2192 | return pt.intValue_big(ty, result_q.toConst()); | |
| 2156 | 2193 | } |
| 2157 | 2194 | |
| 2158 | pub fn intDivFloor(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *Module) !Value { | |
| 2159 | if (ty.zigTypeTag(mod) == .Vector) { | |
| 2160 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(mod)); | |
| 2161 | const scalar_ty = ty.scalarType(mod); | |
| 2195 | pub fn intDivFloor(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2196 | if (ty.zigTypeTag(pt.zcu) == .Vector) { | |
| 2197 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(pt.zcu)); | |
| 2198 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 2162 | 2199 | for (result_data, 0..) |*scalar, i| { |
| 2163 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2164 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2165 | scalar.* = (try intDivFloorScalar(lhs_elem, rhs_elem, scalar_ty, allocator, mod)).toIntern(); | |
| 2200 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2201 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2202 | scalar.* = (try intDivFloorScalar(lhs_elem, rhs_elem, scalar_ty, allocator, pt)).toIntern(); | |
| 2166 | 2203 | } |
| 2167 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2204 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2168 | 2205 | .ty = ty.toIntern(), |
| 2169 | 2206 | .storage = .{ .elems = result_data }, |
| 2170 | } }))); | |
| 2207 | } })); | |
| 2171 | 2208 | } |
| 2172 | return intDivFloorScalar(lhs, rhs, ty, allocator, mod); | |
| 2209 | return intDivFloorScalar(lhs, rhs, ty, allocator, pt); | |
| 2173 | 2210 | } |
| 2174 | 2211 | |
| 2175 | pub fn intDivFloorScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *Module) !Value { | |
| 2212 | pub fn intDivFloorScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2176 | 2213 | // TODO is this a performance issue? maybe we should try the operation without |
| 2177 | 2214 | // resorting to BigInt first. |
| 2178 | 2215 | var lhs_space: Value.BigIntSpace = undefined; |
| 2179 | 2216 | var rhs_space: Value.BigIntSpace = undefined; |
| 2180 | const lhs_bigint = lhs.toBigInt(&lhs_space, mod); | |
| 2181 | const rhs_bigint = rhs.toBigInt(&rhs_space, mod); | |
| 2217 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 2218 | const rhs_bigint = rhs.toBigInt(&rhs_space, pt); | |
| 2182 | 2219 | const limbs_q = try allocator.alloc( |
| 2183 | 2220 | std.math.big.Limb, |
| 2184 | 2221 | lhs_bigint.limbs.len, |
| ... | ... | @@ -2194,33 +2231,33 @@ pub fn intDivFloorScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, |
| 2194 | 2231 | var result_q = BigIntMutable{ .limbs = limbs_q, .positive = undefined, .len = undefined }; |
| 2195 | 2232 | var result_r = BigIntMutable{ .limbs = limbs_r, .positive = undefined, .len = undefined }; |
| 2196 | 2233 | result_q.divFloor(&result_r, lhs_bigint, rhs_bigint, limbs_buffer); |
| 2197 | return mod.intValue_big(ty, result_q.toConst()); | |
| 2234 | return pt.intValue_big(ty, result_q.toConst()); | |
| 2198 | 2235 | } |
| 2199 | 2236 | |
| 2200 | pub fn intMod(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *Module) !Value { | |
| 2201 | if (ty.zigTypeTag(mod) == .Vector) { | |
| 2202 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(mod)); | |
| 2203 | const scalar_ty = ty.scalarType(mod); | |
| 2237 | pub fn intMod(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2238 | if (ty.zigTypeTag(pt.zcu) == .Vector) { | |
| 2239 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(pt.zcu)); | |
| 2240 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 2204 | 2241 | for (result_data, 0..) |*scalar, i| { |
| 2205 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2206 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2207 | scalar.* = (try intModScalar(lhs_elem, rhs_elem, scalar_ty, allocator, mod)).toIntern(); | |
| 2242 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2243 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2244 | scalar.* = (try intModScalar(lhs_elem, rhs_elem, scalar_ty, allocator, pt)).toIntern(); | |
| 2208 | 2245 | } |
| 2209 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2246 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2210 | 2247 | .ty = ty.toIntern(), |
| 2211 | 2248 | .storage = .{ .elems = result_data }, |
| 2212 | } }))); | |
| 2249 | } })); | |
| 2213 | 2250 | } |
| 2214 | return intModScalar(lhs, rhs, ty, allocator, mod); | |
| 2251 | return intModScalar(lhs, rhs, ty, allocator, pt); | |
| 2215 | 2252 | } |
| 2216 | 2253 | |
| 2217 | pub fn intModScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *Module) !Value { | |
| 2254 | pub fn intModScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2218 | 2255 | // TODO is this a performance issue? maybe we should try the operation without |
| 2219 | 2256 | // resorting to BigInt first. |
| 2220 | 2257 | var lhs_space: Value.BigIntSpace = undefined; |
| 2221 | 2258 | var rhs_space: Value.BigIntSpace = undefined; |
| 2222 | const lhs_bigint = lhs.toBigInt(&lhs_space, mod); | |
| 2223 | const rhs_bigint = rhs.toBigInt(&rhs_space, mod); | |
| 2259 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 2260 | const rhs_bigint = rhs.toBigInt(&rhs_space, pt); | |
| 2224 | 2261 | const limbs_q = try allocator.alloc( |
| 2225 | 2262 | std.math.big.Limb, |
| 2226 | 2263 | lhs_bigint.limbs.len, |
| ... | ... | @@ -2236,7 +2273,7 @@ pub fn intModScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: |
| 2236 | 2273 | var result_q = BigIntMutable{ .limbs = limbs_q, .positive = undefined, .len = undefined }; |
| 2237 | 2274 | var result_r = BigIntMutable{ .limbs = limbs_r, .positive = undefined, .len = undefined }; |
| 2238 | 2275 | result_q.divFloor(&result_r, lhs_bigint, rhs_bigint, limbs_buffer); |
| 2239 | return mod.intValue_big(ty, result_r.toConst()); | |
| 2276 | return pt.intValue_big(ty, result_r.toConst()); | |
| 2240 | 2277 | } |
| 2241 | 2278 | |
| 2242 | 2279 | /// Returns true if the value is a floating point type and is NaN. Returns false otherwise. |
| ... | ... | @@ -2268,85 +2305,86 @@ pub fn isNegativeInf(val: Value, mod: *const Module) bool { |
| 2268 | 2305 | }; |
| 2269 | 2306 | } |
| 2270 | 2307 | |
| 2271 | pub fn floatRem(lhs: Value, rhs: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 2272 | if (float_type.zigTypeTag(mod) == .Vector) { | |
| 2273 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); | |
| 2274 | const scalar_ty = float_type.scalarType(mod); | |
| 2308 | pub fn floatRem(lhs: Value, rhs: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 2309 | if (float_type.zigTypeTag(pt.zcu) == .Vector) { | |
| 2310 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(pt.zcu)); | |
| 2311 | const scalar_ty = float_type.scalarType(pt.zcu); | |
| 2275 | 2312 | for (result_data, 0..) |*scalar, i| { |
| 2276 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2277 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2278 | scalar.* = (try floatRemScalar(lhs_elem, rhs_elem, scalar_ty, mod)).toIntern(); | |
| 2313 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2314 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2315 | scalar.* = (try floatRemScalar(lhs_elem, rhs_elem, scalar_ty, pt)).toIntern(); | |
| 2279 | 2316 | } |
| 2280 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2317 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2281 | 2318 | .ty = float_type.toIntern(), |
| 2282 | 2319 | .storage = .{ .elems = result_data }, |
| 2283 | } }))); | |
| 2320 | } })); | |
| 2284 | 2321 | } |
| 2285 | return floatRemScalar(lhs, rhs, float_type, mod); | |
| 2322 | return floatRemScalar(lhs, rhs, float_type, pt); | |
| 2286 | 2323 | } |
| 2287 | 2324 | |
| 2288 | pub fn floatRemScalar(lhs: Value, rhs: Value, float_type: Type, mod: *Module) !Value { | |
| 2289 | const target = mod.getTarget(); | |
| 2325 | pub fn floatRemScalar(lhs: Value, rhs: Value, float_type: Type, pt: Zcu.PerThread) !Value { | |
| 2326 | const target = pt.zcu.getTarget(); | |
| 2290 | 2327 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 2291 | 16 => .{ .f16 = @rem(lhs.toFloat(f16, mod), rhs.toFloat(f16, mod)) }, | |
| 2292 | 32 => .{ .f32 = @rem(lhs.toFloat(f32, mod), rhs.toFloat(f32, mod)) }, | |
| 2293 | 64 => .{ .f64 = @rem(lhs.toFloat(f64, mod), rhs.toFloat(f64, mod)) }, | |
| 2294 | 80 => .{ .f80 = @rem(lhs.toFloat(f80, mod), rhs.toFloat(f80, mod)) }, | |
| 2295 | 128 => .{ .f128 = @rem(lhs.toFloat(f128, mod), rhs.toFloat(f128, mod)) }, | |
| 2328 | 16 => .{ .f16 = @rem(lhs.toFloat(f16, pt), rhs.toFloat(f16, pt)) }, | |
| 2329 | 32 => .{ .f32 = @rem(lhs.toFloat(f32, pt), rhs.toFloat(f32, pt)) }, | |
| 2330 | 64 => .{ .f64 = @rem(lhs.toFloat(f64, pt), rhs.toFloat(f64, pt)) }, | |
| 2331 | 80 => .{ .f80 = @rem(lhs.toFloat(f80, pt), rhs.toFloat(f80, pt)) }, | |
| 2332 | 128 => .{ .f128 = @rem(lhs.toFloat(f128, pt), rhs.toFloat(f128, pt)) }, | |
| 2296 | 2333 | else => unreachable, |
| 2297 | 2334 | }; |
| 2298 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 2335 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 2299 | 2336 | .ty = float_type.toIntern(), |
| 2300 | 2337 | .storage = storage, |
| 2301 | } }))); | |
| 2338 | } })); | |
| 2302 | 2339 | } |
| 2303 | 2340 | |
| 2304 | pub fn floatMod(lhs: Value, rhs: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 2305 | if (float_type.zigTypeTag(mod) == .Vector) { | |
| 2306 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); | |
| 2307 | const scalar_ty = float_type.scalarType(mod); | |
| 2341 | pub fn floatMod(lhs: Value, rhs: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 2342 | if (float_type.zigTypeTag(pt.zcu) == .Vector) { | |
| 2343 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(pt.zcu)); | |
| 2344 | const scalar_ty = float_type.scalarType(pt.zcu); | |
| 2308 | 2345 | for (result_data, 0..) |*scalar, i| { |
| 2309 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2310 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2311 | scalar.* = (try floatModScalar(lhs_elem, rhs_elem, scalar_ty, mod)).toIntern(); | |
| 2346 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2347 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2348 | scalar.* = (try floatModScalar(lhs_elem, rhs_elem, scalar_ty, pt)).toIntern(); | |
| 2312 | 2349 | } |
| 2313 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2350 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2314 | 2351 | .ty = float_type.toIntern(), |
| 2315 | 2352 | .storage = .{ .elems = result_data }, |
| 2316 | } }))); | |
| 2353 | } })); | |
| 2317 | 2354 | } |
| 2318 | return floatModScalar(lhs, rhs, float_type, mod); | |
| 2355 | return floatModScalar(lhs, rhs, float_type, pt); | |
| 2319 | 2356 | } |
| 2320 | 2357 | |
| 2321 | pub fn floatModScalar(lhs: Value, rhs: Value, float_type: Type, mod: *Module) !Value { | |
| 2322 | const target = mod.getTarget(); | |
| 2358 | pub fn floatModScalar(lhs: Value, rhs: Value, float_type: Type, pt: Zcu.PerThread) !Value { | |
| 2359 | const target = pt.zcu.getTarget(); | |
| 2323 | 2360 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 2324 | 16 => .{ .f16 = @mod(lhs.toFloat(f16, mod), rhs.toFloat(f16, mod)) }, | |
| 2325 | 32 => .{ .f32 = @mod(lhs.toFloat(f32, mod), rhs.toFloat(f32, mod)) }, | |
| 2326 | 64 => .{ .f64 = @mod(lhs.toFloat(f64, mod), rhs.toFloat(f64, mod)) }, | |
| 2327 | 80 => .{ .f80 = @mod(lhs.toFloat(f80, mod), rhs.toFloat(f80, mod)) }, | |
| 2328 | 128 => .{ .f128 = @mod(lhs.toFloat(f128, mod), rhs.toFloat(f128, mod)) }, | |
| 2361 | 16 => .{ .f16 = @mod(lhs.toFloat(f16, pt), rhs.toFloat(f16, pt)) }, | |
| 2362 | 32 => .{ .f32 = @mod(lhs.toFloat(f32, pt), rhs.toFloat(f32, pt)) }, | |
| 2363 | 64 => .{ .f64 = @mod(lhs.toFloat(f64, pt), rhs.toFloat(f64, pt)) }, | |
| 2364 | 80 => .{ .f80 = @mod(lhs.toFloat(f80, pt), rhs.toFloat(f80, pt)) }, | |
| 2365 | 128 => .{ .f128 = @mod(lhs.toFloat(f128, pt), rhs.toFloat(f128, pt)) }, | |
| 2329 | 2366 | else => unreachable, |
| 2330 | 2367 | }; |
| 2331 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 2368 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 2332 | 2369 | .ty = float_type.toIntern(), |
| 2333 | 2370 | .storage = storage, |
| 2334 | } }))); | |
| 2371 | } })); | |
| 2335 | 2372 | } |
| 2336 | 2373 | |
| 2337 | 2374 | /// If the value overflowed the type, returns a comptime_int (or vector thereof) instead, setting |
| 2338 | 2375 | /// overflow_idx to the vector index the overflow was at (or 0 for a scalar). |
| 2339 | pub fn intMul(lhs: Value, rhs: Value, ty: Type, overflow_idx: *?usize, allocator: Allocator, mod: *Module) !Value { | |
| 2376 | pub fn intMul(lhs: Value, rhs: Value, ty: Type, overflow_idx: *?usize, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2377 | const mod = pt.zcu; | |
| 2340 | 2378 | var overflow: usize = undefined; |
| 2341 | return intMulInner(lhs, rhs, ty, &overflow, allocator, mod) catch |err| switch (err) { | |
| 2379 | return intMulInner(lhs, rhs, ty, &overflow, allocator, pt) catch |err| switch (err) { | |
| 2342 | 2380 | error.Overflow => { |
| 2343 | 2381 | const is_vec = ty.isVector(mod); |
| 2344 | 2382 | overflow_idx.* = if (is_vec) overflow else 0; |
| 2345 | const safe_ty = if (is_vec) try mod.vectorType(.{ | |
| 2383 | const safe_ty = if (is_vec) try pt.vectorType(.{ | |
| 2346 | 2384 | .len = ty.vectorLen(mod), |
| 2347 | 2385 | .child = .comptime_int_type, |
| 2348 | 2386 | }) else Type.comptime_int; |
| 2349 | return intMulInner(lhs, rhs, safe_ty, undefined, allocator, mod) catch |err1| switch (err1) { | |
| 2387 | return intMulInner(lhs, rhs, safe_ty, undefined, allocator, pt) catch |err1| switch (err1) { | |
| 2350 | 2388 | error.Overflow => unreachable, |
| 2351 | 2389 | else => |e| return e, |
| 2352 | 2390 | }; |
| ... | ... | @@ -2355,14 +2393,15 @@ pub fn intMul(lhs: Value, rhs: Value, ty: Type, overflow_idx: *?usize, allocator |
| 2355 | 2393 | }; |
| 2356 | 2394 | } |
| 2357 | 2395 | |
| 2358 | fn intMulInner(lhs: Value, rhs: Value, ty: Type, overflow_idx: *usize, allocator: Allocator, mod: *Module) !Value { | |
| 2396 | fn intMulInner(lhs: Value, rhs: Value, ty: Type, overflow_idx: *usize, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2397 | const mod = pt.zcu; | |
| 2359 | 2398 | if (ty.zigTypeTag(mod) == .Vector) { |
| 2360 | 2399 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(mod)); |
| 2361 | 2400 | const scalar_ty = ty.scalarType(mod); |
| 2362 | 2401 | for (result_data, 0..) |*scalar, i| { |
| 2363 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2364 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2365 | const val = intMulScalar(lhs_elem, rhs_elem, scalar_ty, allocator, mod) catch |err| switch (err) { | |
| 2402 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2403 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2404 | const val = intMulScalar(lhs_elem, rhs_elem, scalar_ty, allocator, pt) catch |err| switch (err) { | |
| 2366 | 2405 | error.Overflow => { |
| 2367 | 2406 | overflow_idx.* = i; |
| 2368 | 2407 | return error.Overflow; |
| ... | ... | @@ -2371,26 +2410,26 @@ fn intMulInner(lhs: Value, rhs: Value, ty: Type, overflow_idx: *usize, allocator |
| 2371 | 2410 | }; |
| 2372 | 2411 | scalar.* = val.toIntern(); |
| 2373 | 2412 | } |
| 2374 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2413 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2375 | 2414 | .ty = ty.toIntern(), |
| 2376 | 2415 | .storage = .{ .elems = result_data }, |
| 2377 | } }))); | |
| 2416 | } })); | |
| 2378 | 2417 | } |
| 2379 | return intMulScalar(lhs, rhs, ty, allocator, mod); | |
| 2418 | return intMulScalar(lhs, rhs, ty, allocator, pt); | |
| 2380 | 2419 | } |
| 2381 | 2420 | |
| 2382 | pub fn intMulScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *Module) !Value { | |
| 2421 | pub fn intMulScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2383 | 2422 | if (ty.toIntern() != .comptime_int_type) { |
| 2384 | const res = try intMulWithOverflowScalar(lhs, rhs, ty, allocator, mod); | |
| 2385 | if (res.overflow_bit.compareAllWithZero(.neq, mod)) return error.Overflow; | |
| 2423 | const res = try intMulWithOverflowScalar(lhs, rhs, ty, allocator, pt); | |
| 2424 | if (res.overflow_bit.compareAllWithZero(.neq, pt)) return error.Overflow; | |
| 2386 | 2425 | return res.wrapped_result; |
| 2387 | 2426 | } |
| 2388 | 2427 | // TODO is this a performance issue? maybe we should try the operation without |
| 2389 | 2428 | // resorting to BigInt first. |
| 2390 | 2429 | var lhs_space: Value.BigIntSpace = undefined; |
| 2391 | 2430 | var rhs_space: Value.BigIntSpace = undefined; |
| 2392 | const lhs_bigint = lhs.toBigInt(&lhs_space, mod); | |
| 2393 | const rhs_bigint = rhs.toBigInt(&rhs_space, mod); | |
| 2431 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 2432 | const rhs_bigint = rhs.toBigInt(&rhs_space, pt); | |
| 2394 | 2433 | const limbs = try allocator.alloc( |
| 2395 | 2434 | std.math.big.Limb, |
| 2396 | 2435 | lhs_bigint.limbs.len + rhs_bigint.limbs.len, |
| ... | ... | @@ -2402,23 +2441,24 @@ pub fn intMulScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: |
| 2402 | 2441 | ); |
| 2403 | 2442 | defer allocator.free(limbs_buffer); |
| 2404 | 2443 | result_bigint.mul(lhs_bigint, rhs_bigint, limbs_buffer, allocator); |
| 2405 | return mod.intValue_big(ty, result_bigint.toConst()); | |
| 2444 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 2406 | 2445 | } |
| 2407 | 2446 | |
| 2408 | pub fn intTrunc(val: Value, ty: Type, allocator: Allocator, signedness: std.builtin.Signedness, bits: u16, mod: *Module) !Value { | |
| 2447 | pub fn intTrunc(val: Value, ty: Type, allocator: Allocator, signedness: std.builtin.Signedness, bits: u16, pt: Zcu.PerThread) !Value { | |
| 2448 | const mod = pt.zcu; | |
| 2409 | 2449 | if (ty.zigTypeTag(mod) == .Vector) { |
| 2410 | 2450 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(mod)); |
| 2411 | 2451 | const scalar_ty = ty.scalarType(mod); |
| 2412 | 2452 | for (result_data, 0..) |*scalar, i| { |
| 2413 | const elem_val = try val.elemValue(mod, i); | |
| 2414 | scalar.* = (try intTruncScalar(elem_val, scalar_ty, allocator, signedness, bits, mod)).toIntern(); | |
| 2453 | const elem_val = try val.elemValue(pt, i); | |
| 2454 | scalar.* = (try intTruncScalar(elem_val, scalar_ty, allocator, signedness, bits, pt)).toIntern(); | |
| 2415 | 2455 | } |
| 2416 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2456 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2417 | 2457 | .ty = ty.toIntern(), |
| 2418 | 2458 | .storage = .{ .elems = result_data }, |
| 2419 | } }))); | |
| 2459 | } })); | |
| 2420 | 2460 | } |
| 2421 | return intTruncScalar(val, ty, allocator, signedness, bits, mod); | |
| 2461 | return intTruncScalar(val, ty, allocator, signedness, bits, pt); | |
| 2422 | 2462 | } |
| 2423 | 2463 | |
| 2424 | 2464 | /// This variant may vectorize on `bits`. Asserts that `bits` is a (vector of) `u16`. |
| ... | ... | @@ -2428,22 +2468,22 @@ pub fn intTruncBitsAsValue( |
| 2428 | 2468 | allocator: Allocator, |
| 2429 | 2469 | signedness: std.builtin.Signedness, |
| 2430 | 2470 | bits: Value, |
| 2431 | mod: *Module, | |
| 2471 | pt: Zcu.PerThread, | |
| 2432 | 2472 | ) !Value { |
| 2433 | if (ty.zigTypeTag(mod) == .Vector) { | |
| 2434 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(mod)); | |
| 2435 | const scalar_ty = ty.scalarType(mod); | |
| 2473 | if (ty.zigTypeTag(pt.zcu) == .Vector) { | |
| 2474 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(pt.zcu)); | |
| 2475 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 2436 | 2476 | for (result_data, 0..) |*scalar, i| { |
| 2437 | const elem_val = try val.elemValue(mod, i); | |
| 2438 | const bits_elem = try bits.elemValue(mod, i); | |
| 2439 | scalar.* = (try intTruncScalar(elem_val, scalar_ty, allocator, signedness, @intCast(bits_elem.toUnsignedInt(mod)), mod)).toIntern(); | |
| 2477 | const elem_val = try val.elemValue(pt, i); | |
| 2478 | const bits_elem = try bits.elemValue(pt, i); | |
| 2479 | scalar.* = (try intTruncScalar(elem_val, scalar_ty, allocator, signedness, @intCast(bits_elem.toUnsignedInt(pt)), pt)).toIntern(); | |
| 2440 | 2480 | } |
| 2441 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2481 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2442 | 2482 | .ty = ty.toIntern(), |
| 2443 | 2483 | .storage = .{ .elems = result_data }, |
| 2444 | } }))); | |
| 2484 | } })); | |
| 2445 | 2485 | } |
| 2446 | return intTruncScalar(val, ty, allocator, signedness, @intCast(bits.toUnsignedInt(mod)), mod); | |
| 2486 | return intTruncScalar(val, ty, allocator, signedness, @intCast(bits.toUnsignedInt(pt)), pt); | |
| 2447 | 2487 | } |
| 2448 | 2488 | |
| 2449 | 2489 | pub fn intTruncScalar( |
| ... | ... | @@ -2452,14 +2492,15 @@ pub fn intTruncScalar( |
| 2452 | 2492 | allocator: Allocator, |
| 2453 | 2493 | signedness: std.builtin.Signedness, |
| 2454 | 2494 | bits: u16, |
| 2455 | zcu: *Zcu, | |
| 2495 | pt: Zcu.PerThread, | |
| 2456 | 2496 | ) !Value { |
| 2457 | if (bits == 0) return zcu.intValue(ty, 0); | |
| 2497 | const zcu = pt.zcu; | |
| 2498 | if (bits == 0) return pt.intValue(ty, 0); | |
| 2458 | 2499 | |
| 2459 | if (val.isUndef(zcu)) return zcu.undefValue(ty); | |
| 2500 | if (val.isUndef(zcu)) return pt.undefValue(ty); | |
| 2460 | 2501 | |
| 2461 | 2502 | var val_space: Value.BigIntSpace = undefined; |
| 2462 | const val_bigint = val.toBigInt(&val_space, zcu); | |
| 2503 | const val_bigint = val.toBigInt(&val_space, pt); | |
| 2463 | 2504 | |
| 2464 | 2505 | const limbs = try allocator.alloc( |
| 2465 | 2506 | std.math.big.Limb, |
| ... | ... | @@ -2468,32 +2509,33 @@ pub fn intTruncScalar( |
| 2468 | 2509 | var result_bigint = BigIntMutable{ .limbs = limbs, .positive = undefined, .len = undefined }; |
| 2469 | 2510 | |
| 2470 | 2511 | result_bigint.truncate(val_bigint, signedness, bits); |
| 2471 | return zcu.intValue_big(ty, result_bigint.toConst()); | |
| 2512 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 2472 | 2513 | } |
| 2473 | 2514 | |
| 2474 | pub fn shl(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *Module) !Value { | |
| 2515 | pub fn shl(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2516 | const mod = pt.zcu; | |
| 2475 | 2517 | if (ty.zigTypeTag(mod) == .Vector) { |
| 2476 | 2518 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(mod)); |
| 2477 | 2519 | const scalar_ty = ty.scalarType(mod); |
| 2478 | 2520 | for (result_data, 0..) |*scalar, i| { |
| 2479 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2480 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2481 | scalar.* = (try shlScalar(lhs_elem, rhs_elem, scalar_ty, allocator, mod)).toIntern(); | |
| 2521 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2522 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2523 | scalar.* = (try shlScalar(lhs_elem, rhs_elem, scalar_ty, allocator, pt)).toIntern(); | |
| 2482 | 2524 | } |
| 2483 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2525 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2484 | 2526 | .ty = ty.toIntern(), |
| 2485 | 2527 | .storage = .{ .elems = result_data }, |
| 2486 | } }))); | |
| 2528 | } })); | |
| 2487 | 2529 | } |
| 2488 | return shlScalar(lhs, rhs, ty, allocator, mod); | |
| 2530 | return shlScalar(lhs, rhs, ty, allocator, pt); | |
| 2489 | 2531 | } |
| 2490 | 2532 | |
| 2491 | pub fn shlScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *Module) !Value { | |
| 2533 | pub fn shlScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2492 | 2534 | // TODO is this a performance issue? maybe we should try the operation without |
| 2493 | 2535 | // resorting to BigInt first. |
| 2494 | 2536 | var lhs_space: Value.BigIntSpace = undefined; |
| 2495 | const lhs_bigint = lhs.toBigInt(&lhs_space, mod); | |
| 2496 | const shift: usize = @intCast(rhs.toUnsignedInt(mod)); | |
| 2537 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 2538 | const shift: usize = @intCast(rhs.toUnsignedInt(pt)); | |
| 2497 | 2539 | const limbs = try allocator.alloc( |
| 2498 | 2540 | std.math.big.Limb, |
| 2499 | 2541 | lhs_bigint.limbs.len + (shift / (@sizeOf(std.math.big.Limb) * 8)) + 1, |
| ... | ... | @@ -2505,11 +2547,11 @@ pub fn shlScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *M |
| 2505 | 2547 | }; |
| 2506 | 2548 | result_bigint.shiftLeft(lhs_bigint, shift); |
| 2507 | 2549 | if (ty.toIntern() != .comptime_int_type) { |
| 2508 | const int_info = ty.intInfo(mod); | |
| 2550 | const int_info = ty.intInfo(pt.zcu); | |
| 2509 | 2551 | result_bigint.truncate(result_bigint.toConst(), int_info.signedness, int_info.bits); |
| 2510 | 2552 | } |
| 2511 | 2553 | |
| 2512 | return mod.intValue_big(ty, result_bigint.toConst()); | |
| 2554 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 2513 | 2555 | } |
| 2514 | 2556 | |
| 2515 | 2557 | pub fn shlWithOverflow( |
| ... | ... | @@ -2517,32 +2559,32 @@ pub fn shlWithOverflow( |
| 2517 | 2559 | rhs: Value, |
| 2518 | 2560 | ty: Type, |
| 2519 | 2561 | allocator: Allocator, |
| 2520 | mod: *Module, | |
| 2562 | pt: Zcu.PerThread, | |
| 2521 | 2563 | ) !OverflowArithmeticResult { |
| 2522 | if (ty.zigTypeTag(mod) == .Vector) { | |
| 2523 | const vec_len = ty.vectorLen(mod); | |
| 2564 | if (ty.zigTypeTag(pt.zcu) == .Vector) { | |
| 2565 | const vec_len = ty.vectorLen(pt.zcu); | |
| 2524 | 2566 | const overflowed_data = try allocator.alloc(InternPool.Index, vec_len); |
| 2525 | 2567 | const result_data = try allocator.alloc(InternPool.Index, vec_len); |
| 2526 | const scalar_ty = ty.scalarType(mod); | |
| 2568 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 2527 | 2569 | for (overflowed_data, result_data, 0..) |*of, *scalar, i| { |
| 2528 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2529 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2530 | const of_math_result = try shlWithOverflowScalar(lhs_elem, rhs_elem, scalar_ty, allocator, mod); | |
| 2570 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2571 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2572 | const of_math_result = try shlWithOverflowScalar(lhs_elem, rhs_elem, scalar_ty, allocator, pt); | |
| 2531 | 2573 | of.* = of_math_result.overflow_bit.toIntern(); |
| 2532 | 2574 | scalar.* = of_math_result.wrapped_result.toIntern(); |
| 2533 | 2575 | } |
| 2534 | 2576 | return OverflowArithmeticResult{ |
| 2535 | .overflow_bit = Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2536 | .ty = (try mod.vectorType(.{ .len = vec_len, .child = .u1_type })).toIntern(), | |
| 2577 | .overflow_bit = Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2578 | .ty = (try pt.vectorType(.{ .len = vec_len, .child = .u1_type })).toIntern(), | |
| 2537 | 2579 | .storage = .{ .elems = overflowed_data }, |
| 2538 | } }))), | |
| 2539 | .wrapped_result = Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2580 | } })), | |
| 2581 | .wrapped_result = Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2540 | 2582 | .ty = ty.toIntern(), |
| 2541 | 2583 | .storage = .{ .elems = result_data }, |
| 2542 | } }))), | |
| 2584 | } })), | |
| 2543 | 2585 | }; |
| 2544 | 2586 | } |
| 2545 | return shlWithOverflowScalar(lhs, rhs, ty, allocator, mod); | |
| 2587 | return shlWithOverflowScalar(lhs, rhs, ty, allocator, pt); | |
| 2546 | 2588 | } |
| 2547 | 2589 | |
| 2548 | 2590 | pub fn shlWithOverflowScalar( |
| ... | ... | @@ -2550,12 +2592,12 @@ pub fn shlWithOverflowScalar( |
| 2550 | 2592 | rhs: Value, |
| 2551 | 2593 | ty: Type, |
| 2552 | 2594 | allocator: Allocator, |
| 2553 | mod: *Module, | |
| 2595 | pt: Zcu.PerThread, | |
| 2554 | 2596 | ) !OverflowArithmeticResult { |
| 2555 | const info = ty.intInfo(mod); | |
| 2597 | const info = ty.intInfo(pt.zcu); | |
| 2556 | 2598 | var lhs_space: Value.BigIntSpace = undefined; |
| 2557 | const lhs_bigint = lhs.toBigInt(&lhs_space, mod); | |
| 2558 | const shift: usize = @intCast(rhs.toUnsignedInt(mod)); | |
| 2599 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 2600 | const shift: usize = @intCast(rhs.toUnsignedInt(pt)); | |
| 2559 | 2601 | const limbs = try allocator.alloc( |
| 2560 | 2602 | std.math.big.Limb, |
| 2561 | 2603 | lhs_bigint.limbs.len + (shift / (@sizeOf(std.math.big.Limb) * 8)) + 1, |
| ... | ... | @@ -2571,8 +2613,8 @@ pub fn shlWithOverflowScalar( |
| 2571 | 2613 | result_bigint.truncate(result_bigint.toConst(), info.signedness, info.bits); |
| 2572 | 2614 | } |
| 2573 | 2615 | return OverflowArithmeticResult{ |
| 2574 | .overflow_bit = try mod.intValue(Type.u1, @intFromBool(overflowed)), | |
| 2575 | .wrapped_result = try mod.intValue_big(ty, result_bigint.toConst()), | |
| 2616 | .overflow_bit = try pt.intValue(Type.u1, @intFromBool(overflowed)), | |
| 2617 | .wrapped_result = try pt.intValue_big(ty, result_bigint.toConst()), | |
| 2576 | 2618 | }; |
| 2577 | 2619 | } |
| 2578 | 2620 | |
| ... | ... | @@ -2581,22 +2623,22 @@ pub fn shlSat( |
| 2581 | 2623 | rhs: Value, |
| 2582 | 2624 | ty: Type, |
| 2583 | 2625 | arena: Allocator, |
| 2584 | mod: *Module, | |
| 2626 | pt: Zcu.PerThread, | |
| 2585 | 2627 | ) !Value { |
| 2586 | if (ty.zigTypeTag(mod) == .Vector) { | |
| 2587 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(mod)); | |
| 2588 | const scalar_ty = ty.scalarType(mod); | |
| 2628 | if (ty.zigTypeTag(pt.zcu) == .Vector) { | |
| 2629 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(pt.zcu)); | |
| 2630 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 2589 | 2631 | for (result_data, 0..) |*scalar, i| { |
| 2590 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2591 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2592 | scalar.* = (try shlSatScalar(lhs_elem, rhs_elem, scalar_ty, arena, mod)).toIntern(); | |
| 2632 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2633 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2634 | scalar.* = (try shlSatScalar(lhs_elem, rhs_elem, scalar_ty, arena, pt)).toIntern(); | |
| 2593 | 2635 | } |
| 2594 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2636 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2595 | 2637 | .ty = ty.toIntern(), |
| 2596 | 2638 | .storage = .{ .elems = result_data }, |
| 2597 | } }))); | |
| 2639 | } })); | |
| 2598 | 2640 | } |
| 2599 | return shlSatScalar(lhs, rhs, ty, arena, mod); | |
| 2641 | return shlSatScalar(lhs, rhs, ty, arena, pt); | |
| 2600 | 2642 | } |
| 2601 | 2643 | |
| 2602 | 2644 | pub fn shlSatScalar( |
| ... | ... | @@ -2604,15 +2646,15 @@ pub fn shlSatScalar( |
| 2604 | 2646 | rhs: Value, |
| 2605 | 2647 | ty: Type, |
| 2606 | 2648 | arena: Allocator, |
| 2607 | mod: *Module, | |
| 2649 | pt: Zcu.PerThread, | |
| 2608 | 2650 | ) !Value { |
| 2609 | 2651 | // TODO is this a performance issue? maybe we should try the operation without |
| 2610 | 2652 | // resorting to BigInt first. |
| 2611 | const info = ty.intInfo(mod); | |
| 2653 | const info = ty.intInfo(pt.zcu); | |
| 2612 | 2654 | |
| 2613 | 2655 | var lhs_space: Value.BigIntSpace = undefined; |
| 2614 | const lhs_bigint = lhs.toBigInt(&lhs_space, mod); | |
| 2615 | const shift: usize = @intCast(rhs.toUnsignedInt(mod)); | |
| 2656 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 2657 | const shift: usize = @intCast(rhs.toUnsignedInt(pt)); | |
| 2616 | 2658 | const limbs = try arena.alloc( |
| 2617 | 2659 | std.math.big.Limb, |
| 2618 | 2660 | std.math.big.int.calcTwosCompLimbCount(info.bits) + 1, |
| ... | ... | @@ -2623,7 +2665,7 @@ pub fn shlSatScalar( |
| 2623 | 2665 | .len = undefined, |
| 2624 | 2666 | }; |
| 2625 | 2667 | result_bigint.shiftLeftSat(lhs_bigint, shift, info.signedness, info.bits); |
| 2626 | return mod.intValue_big(ty, result_bigint.toConst()); | |
| 2668 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 2627 | 2669 | } |
| 2628 | 2670 | |
| 2629 | 2671 | pub fn shlTrunc( |
| ... | ... | @@ -2631,22 +2673,22 @@ pub fn shlTrunc( |
| 2631 | 2673 | rhs: Value, |
| 2632 | 2674 | ty: Type, |
| 2633 | 2675 | arena: Allocator, |
| 2634 | mod: *Module, | |
| 2676 | pt: Zcu.PerThread, | |
| 2635 | 2677 | ) !Value { |
| 2636 | if (ty.zigTypeTag(mod) == .Vector) { | |
| 2637 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(mod)); | |
| 2638 | const scalar_ty = ty.scalarType(mod); | |
| 2678 | if (ty.zigTypeTag(pt.zcu) == .Vector) { | |
| 2679 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(pt.zcu)); | |
| 2680 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 2639 | 2681 | for (result_data, 0..) |*scalar, i| { |
| 2640 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2641 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2642 | scalar.* = (try shlTruncScalar(lhs_elem, rhs_elem, scalar_ty, arena, mod)).toIntern(); | |
| 2682 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2683 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2684 | scalar.* = (try shlTruncScalar(lhs_elem, rhs_elem, scalar_ty, arena, pt)).toIntern(); | |
| 2643 | 2685 | } |
| 2644 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2686 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2645 | 2687 | .ty = ty.toIntern(), |
| 2646 | 2688 | .storage = .{ .elems = result_data }, |
| 2647 | } }))); | |
| 2689 | } })); | |
| 2648 | 2690 | } |
| 2649 | return shlTruncScalar(lhs, rhs, ty, arena, mod); | |
| 2691 | return shlTruncScalar(lhs, rhs, ty, arena, pt); | |
| 2650 | 2692 | } |
| 2651 | 2693 | |
| 2652 | 2694 | pub fn shlTruncScalar( |
| ... | ... | @@ -2654,46 +2696,46 @@ pub fn shlTruncScalar( |
| 2654 | 2696 | rhs: Value, |
| 2655 | 2697 | ty: Type, |
| 2656 | 2698 | arena: Allocator, |
| 2657 | mod: *Module, | |
| 2699 | pt: Zcu.PerThread, | |
| 2658 | 2700 | ) !Value { |
| 2659 | const shifted = try lhs.shl(rhs, ty, arena, mod); | |
| 2660 | const int_info = ty.intInfo(mod); | |
| 2661 | const truncated = try shifted.intTrunc(ty, arena, int_info.signedness, int_info.bits, mod); | |
| 2701 | const shifted = try lhs.shl(rhs, ty, arena, pt); | |
| 2702 | const int_info = ty.intInfo(pt.zcu); | |
| 2703 | const truncated = try shifted.intTrunc(ty, arena, int_info.signedness, int_info.bits, pt); | |
| 2662 | 2704 | return truncated; |
| 2663 | 2705 | } |
| 2664 | 2706 | |
| 2665 | pub fn shr(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *Module) !Value { | |
| 2666 | if (ty.zigTypeTag(mod) == .Vector) { | |
| 2667 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(mod)); | |
| 2668 | const scalar_ty = ty.scalarType(mod); | |
| 2707 | pub fn shr(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2708 | if (ty.zigTypeTag(pt.zcu) == .Vector) { | |
| 2709 | const result_data = try allocator.alloc(InternPool.Index, ty.vectorLen(pt.zcu)); | |
| 2710 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 2669 | 2711 | for (result_data, 0..) |*scalar, i| { |
| 2670 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2671 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2672 | scalar.* = (try shrScalar(lhs_elem, rhs_elem, scalar_ty, allocator, mod)).toIntern(); | |
| 2712 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2713 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2714 | scalar.* = (try shrScalar(lhs_elem, rhs_elem, scalar_ty, allocator, pt)).toIntern(); | |
| 2673 | 2715 | } |
| 2674 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2716 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2675 | 2717 | .ty = ty.toIntern(), |
| 2676 | 2718 | .storage = .{ .elems = result_data }, |
| 2677 | } }))); | |
| 2719 | } })); | |
| 2678 | 2720 | } |
| 2679 | return shrScalar(lhs, rhs, ty, allocator, mod); | |
| 2721 | return shrScalar(lhs, rhs, ty, allocator, pt); | |
| 2680 | 2722 | } |
| 2681 | 2723 | |
| 2682 | pub fn shrScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *Module) !Value { | |
| 2724 | pub fn shrScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, pt: Zcu.PerThread) !Value { | |
| 2683 | 2725 | // TODO is this a performance issue? maybe we should try the operation without |
| 2684 | 2726 | // resorting to BigInt first. |
| 2685 | 2727 | var lhs_space: Value.BigIntSpace = undefined; |
| 2686 | const lhs_bigint = lhs.toBigInt(&lhs_space, mod); | |
| 2687 | const shift: usize = @intCast(rhs.toUnsignedInt(mod)); | |
| 2728 | const lhs_bigint = lhs.toBigInt(&lhs_space, pt); | |
| 2729 | const shift: usize = @intCast(rhs.toUnsignedInt(pt)); | |
| 2688 | 2730 | |
| 2689 | 2731 | const result_limbs = lhs_bigint.limbs.len -| (shift / (@sizeOf(std.math.big.Limb) * 8)); |
| 2690 | 2732 | if (result_limbs == 0) { |
| 2691 | 2733 | // The shift is enough to remove all the bits from the number, which means the |
| 2692 | 2734 | // result is 0 or -1 depending on the sign. |
| 2693 | 2735 | if (lhs_bigint.positive) { |
| 2694 | return mod.intValue(ty, 0); | |
| 2736 | return pt.intValue(ty, 0); | |
| 2695 | 2737 | } else { |
| 2696 | return mod.intValue(ty, -1); | |
| 2738 | return pt.intValue(ty, -1); | |
| 2697 | 2739 | } |
| 2698 | 2740 | } |
| 2699 | 2741 | |
| ... | ... | @@ -2707,48 +2749,45 @@ pub fn shrScalar(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, mod: *M |
| 2707 | 2749 | .len = undefined, |
| 2708 | 2750 | }; |
| 2709 | 2751 | result_bigint.shiftRight(lhs_bigint, shift); |
| 2710 | return mod.intValue_big(ty, result_bigint.toConst()); | |
| 2752 | return pt.intValue_big(ty, result_bigint.toConst()); | |
| 2711 | 2753 | } |
| 2712 | 2754 | |
| 2713 | 2755 | pub fn floatNeg( |
| 2714 | 2756 | val: Value, |
| 2715 | 2757 | float_type: Type, |
| 2716 | 2758 | arena: Allocator, |
| 2717 | mod: *Module, | |
| 2759 | pt: Zcu.PerThread, | |
| 2718 | 2760 | ) !Value { |
| 2761 | const mod = pt.zcu; | |
| 2719 | 2762 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 2720 | 2763 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 2721 | 2764 | const scalar_ty = float_type.scalarType(mod); |
| 2722 | 2765 | for (result_data, 0..) |*scalar, i| { |
| 2723 | const elem_val = try val.elemValue(mod, i); | |
| 2724 | scalar.* = (try floatNegScalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 2766 | const elem_val = try val.elemValue(pt, i); | |
| 2767 | scalar.* = (try floatNegScalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 2725 | 2768 | } |
| 2726 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2769 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2727 | 2770 | .ty = float_type.toIntern(), |
| 2728 | 2771 | .storage = .{ .elems = result_data }, |
| 2729 | } }))); | |
| 2772 | } })); | |
| 2730 | 2773 | } |
| 2731 | return floatNegScalar(val, float_type, mod); | |
| 2774 | return floatNegScalar(val, float_type, pt); | |
| 2732 | 2775 | } |
| 2733 | 2776 | |
| 2734 | pub fn floatNegScalar( | |
| 2735 | val: Value, | |
| 2736 | float_type: Type, | |
| 2737 | mod: *Module, | |
| 2738 | ) !Value { | |
| 2739 | const target = mod.getTarget(); | |
| 2777 | pub fn floatNegScalar(val: Value, float_type: Type, pt: Zcu.PerThread) !Value { | |
| 2778 | const target = pt.zcu.getTarget(); | |
| 2740 | 2779 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 2741 | 16 => .{ .f16 = -val.toFloat(f16, mod) }, | |
| 2742 | 32 => .{ .f32 = -val.toFloat(f32, mod) }, | |
| 2743 | 64 => .{ .f64 = -val.toFloat(f64, mod) }, | |
| 2744 | 80 => .{ .f80 = -val.toFloat(f80, mod) }, | |
| 2745 | 128 => .{ .f128 = -val.toFloat(f128, mod) }, | |
| 2780 | 16 => .{ .f16 = -val.toFloat(f16, pt) }, | |
| 2781 | 32 => .{ .f32 = -val.toFloat(f32, pt) }, | |
| 2782 | 64 => .{ .f64 = -val.toFloat(f64, pt) }, | |
| 2783 | 80 => .{ .f80 = -val.toFloat(f80, pt) }, | |
| 2784 | 128 => .{ .f128 = -val.toFloat(f128, pt) }, | |
| 2746 | 2785 | else => unreachable, |
| 2747 | 2786 | }; |
| 2748 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 2787 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 2749 | 2788 | .ty = float_type.toIntern(), |
| 2750 | 2789 | .storage = storage, |
| 2751 | } }))); | |
| 2790 | } })); | |
| 2752 | 2791 | } |
| 2753 | 2792 | |
| 2754 | 2793 | pub fn floatAdd( |
| ... | ... | @@ -2756,43 +2795,45 @@ pub fn floatAdd( |
| 2756 | 2795 | rhs: Value, |
| 2757 | 2796 | float_type: Type, |
| 2758 | 2797 | arena: Allocator, |
| 2759 | mod: *Module, | |
| 2798 | pt: Zcu.PerThread, | |
| 2760 | 2799 | ) !Value { |
| 2800 | const mod = pt.zcu; | |
| 2761 | 2801 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 2762 | 2802 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 2763 | 2803 | const scalar_ty = float_type.scalarType(mod); |
| 2764 | 2804 | for (result_data, 0..) |*scalar, i| { |
| 2765 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2766 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2767 | scalar.* = (try floatAddScalar(lhs_elem, rhs_elem, scalar_ty, mod)).toIntern(); | |
| 2805 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2806 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2807 | scalar.* = (try floatAddScalar(lhs_elem, rhs_elem, scalar_ty, pt)).toIntern(); | |
| 2768 | 2808 | } |
| 2769 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2809 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2770 | 2810 | .ty = float_type.toIntern(), |
| 2771 | 2811 | .storage = .{ .elems = result_data }, |
| 2772 | } }))); | |
| 2812 | } })); | |
| 2773 | 2813 | } |
| 2774 | return floatAddScalar(lhs, rhs, float_type, mod); | |
| 2814 | return floatAddScalar(lhs, rhs, float_type, pt); | |
| 2775 | 2815 | } |
| 2776 | 2816 | |
| 2777 | 2817 | pub fn floatAddScalar( |
| 2778 | 2818 | lhs: Value, |
| 2779 | 2819 | rhs: Value, |
| 2780 | 2820 | float_type: Type, |
| 2781 | mod: *Module, | |
| 2821 | pt: Zcu.PerThread, | |
| 2782 | 2822 | ) !Value { |
| 2823 | const mod = pt.zcu; | |
| 2783 | 2824 | const target = mod.getTarget(); |
| 2784 | 2825 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 2785 | 16 => .{ .f16 = lhs.toFloat(f16, mod) + rhs.toFloat(f16, mod) }, | |
| 2786 | 32 => .{ .f32 = lhs.toFloat(f32, mod) + rhs.toFloat(f32, mod) }, | |
| 2787 | 64 => .{ .f64 = lhs.toFloat(f64, mod) + rhs.toFloat(f64, mod) }, | |
| 2788 | 80 => .{ .f80 = lhs.toFloat(f80, mod) + rhs.toFloat(f80, mod) }, | |
| 2789 | 128 => .{ .f128 = lhs.toFloat(f128, mod) + rhs.toFloat(f128, mod) }, | |
| 2826 | 16 => .{ .f16 = lhs.toFloat(f16, pt) + rhs.toFloat(f16, pt) }, | |
| 2827 | 32 => .{ .f32 = lhs.toFloat(f32, pt) + rhs.toFloat(f32, pt) }, | |
| 2828 | 64 => .{ .f64 = lhs.toFloat(f64, pt) + rhs.toFloat(f64, pt) }, | |
| 2829 | 80 => .{ .f80 = lhs.toFloat(f80, pt) + rhs.toFloat(f80, pt) }, | |
| 2830 | 128 => .{ .f128 = lhs.toFloat(f128, pt) + rhs.toFloat(f128, pt) }, | |
| 2790 | 2831 | else => unreachable, |
| 2791 | 2832 | }; |
| 2792 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 2833 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 2793 | 2834 | .ty = float_type.toIntern(), |
| 2794 | 2835 | .storage = storage, |
| 2795 | } }))); | |
| 2836 | } })); | |
| 2796 | 2837 | } |
| 2797 | 2838 | |
| 2798 | 2839 | pub fn floatSub( |
| ... | ... | @@ -2800,43 +2841,45 @@ pub fn floatSub( |
| 2800 | 2841 | rhs: Value, |
| 2801 | 2842 | float_type: Type, |
| 2802 | 2843 | arena: Allocator, |
| 2803 | mod: *Module, | |
| 2844 | pt: Zcu.PerThread, | |
| 2804 | 2845 | ) !Value { |
| 2846 | const mod = pt.zcu; | |
| 2805 | 2847 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 2806 | 2848 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 2807 | 2849 | const scalar_ty = float_type.scalarType(mod); |
| 2808 | 2850 | for (result_data, 0..) |*scalar, i| { |
| 2809 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2810 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2811 | scalar.* = (try floatSubScalar(lhs_elem, rhs_elem, scalar_ty, mod)).toIntern(); | |
| 2851 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2852 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2853 | scalar.* = (try floatSubScalar(lhs_elem, rhs_elem, scalar_ty, pt)).toIntern(); | |
| 2812 | 2854 | } |
| 2813 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2855 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2814 | 2856 | .ty = float_type.toIntern(), |
| 2815 | 2857 | .storage = .{ .elems = result_data }, |
| 2816 | } }))); | |
| 2858 | } })); | |
| 2817 | 2859 | } |
| 2818 | return floatSubScalar(lhs, rhs, float_type, mod); | |
| 2860 | return floatSubScalar(lhs, rhs, float_type, pt); | |
| 2819 | 2861 | } |
| 2820 | 2862 | |
| 2821 | 2863 | pub fn floatSubScalar( |
| 2822 | 2864 | lhs: Value, |
| 2823 | 2865 | rhs: Value, |
| 2824 | 2866 | float_type: Type, |
| 2825 | mod: *Module, | |
| 2867 | pt: Zcu.PerThread, | |
| 2826 | 2868 | ) !Value { |
| 2869 | const mod = pt.zcu; | |
| 2827 | 2870 | const target = mod.getTarget(); |
| 2828 | 2871 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 2829 | 16 => .{ .f16 = lhs.toFloat(f16, mod) - rhs.toFloat(f16, mod) }, | |
| 2830 | 32 => .{ .f32 = lhs.toFloat(f32, mod) - rhs.toFloat(f32, mod) }, | |
| 2831 | 64 => .{ .f64 = lhs.toFloat(f64, mod) - rhs.toFloat(f64, mod) }, | |
| 2832 | 80 => .{ .f80 = lhs.toFloat(f80, mod) - rhs.toFloat(f80, mod) }, | |
| 2833 | 128 => .{ .f128 = lhs.toFloat(f128, mod) - rhs.toFloat(f128, mod) }, | |
| 2872 | 16 => .{ .f16 = lhs.toFloat(f16, pt) - rhs.toFloat(f16, pt) }, | |
| 2873 | 32 => .{ .f32 = lhs.toFloat(f32, pt) - rhs.toFloat(f32, pt) }, | |
| 2874 | 64 => .{ .f64 = lhs.toFloat(f64, pt) - rhs.toFloat(f64, pt) }, | |
| 2875 | 80 => .{ .f80 = lhs.toFloat(f80, pt) - rhs.toFloat(f80, pt) }, | |
| 2876 | 128 => .{ .f128 = lhs.toFloat(f128, pt) - rhs.toFloat(f128, pt) }, | |
| 2834 | 2877 | else => unreachable, |
| 2835 | 2878 | }; |
| 2836 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 2879 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 2837 | 2880 | .ty = float_type.toIntern(), |
| 2838 | 2881 | .storage = storage, |
| 2839 | } }))); | |
| 2882 | } })); | |
| 2840 | 2883 | } |
| 2841 | 2884 | |
| 2842 | 2885 | pub fn floatDiv( |
| ... | ... | @@ -2844,43 +2887,43 @@ pub fn floatDiv( |
| 2844 | 2887 | rhs: Value, |
| 2845 | 2888 | float_type: Type, |
| 2846 | 2889 | arena: Allocator, |
| 2847 | mod: *Module, | |
| 2890 | pt: Zcu.PerThread, | |
| 2848 | 2891 | ) !Value { |
| 2849 | if (float_type.zigTypeTag(mod) == .Vector) { | |
| 2850 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); | |
| 2851 | const scalar_ty = float_type.scalarType(mod); | |
| 2892 | if (float_type.zigTypeTag(pt.zcu) == .Vector) { | |
| 2893 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(pt.zcu)); | |
| 2894 | const scalar_ty = float_type.scalarType(pt.zcu); | |
| 2852 | 2895 | for (result_data, 0..) |*scalar, i| { |
| 2853 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2854 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2855 | scalar.* = (try floatDivScalar(lhs_elem, rhs_elem, scalar_ty, mod)).toIntern(); | |
| 2896 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2897 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2898 | scalar.* = (try floatDivScalar(lhs_elem, rhs_elem, scalar_ty, pt)).toIntern(); | |
| 2856 | 2899 | } |
| 2857 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2900 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2858 | 2901 | .ty = float_type.toIntern(), |
| 2859 | 2902 | .storage = .{ .elems = result_data }, |
| 2860 | } }))); | |
| 2903 | } })); | |
| 2861 | 2904 | } |
| 2862 | return floatDivScalar(lhs, rhs, float_type, mod); | |
| 2905 | return floatDivScalar(lhs, rhs, float_type, pt); | |
| 2863 | 2906 | } |
| 2864 | 2907 | |
| 2865 | 2908 | pub fn floatDivScalar( |
| 2866 | 2909 | lhs: Value, |
| 2867 | 2910 | rhs: Value, |
| 2868 | 2911 | float_type: Type, |
| 2869 | mod: *Module, | |
| 2912 | pt: Zcu.PerThread, | |
| 2870 | 2913 | ) !Value { |
| 2871 | const target = mod.getTarget(); | |
| 2914 | const target = pt.zcu.getTarget(); | |
| 2872 | 2915 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 2873 | 16 => .{ .f16 = lhs.toFloat(f16, mod) / rhs.toFloat(f16, mod) }, | |
| 2874 | 32 => .{ .f32 = lhs.toFloat(f32, mod) / rhs.toFloat(f32, mod) }, | |
| 2875 | 64 => .{ .f64 = lhs.toFloat(f64, mod) / rhs.toFloat(f64, mod) }, | |
| 2876 | 80 => .{ .f80 = lhs.toFloat(f80, mod) / rhs.toFloat(f80, mod) }, | |
| 2877 | 128 => .{ .f128 = lhs.toFloat(f128, mod) / rhs.toFloat(f128, mod) }, | |
| 2916 | 16 => .{ .f16 = lhs.toFloat(f16, pt) / rhs.toFloat(f16, pt) }, | |
| 2917 | 32 => .{ .f32 = lhs.toFloat(f32, pt) / rhs.toFloat(f32, pt) }, | |
| 2918 | 64 => .{ .f64 = lhs.toFloat(f64, pt) / rhs.toFloat(f64, pt) }, | |
| 2919 | 80 => .{ .f80 = lhs.toFloat(f80, pt) / rhs.toFloat(f80, pt) }, | |
| 2920 | 128 => .{ .f128 = lhs.toFloat(f128, pt) / rhs.toFloat(f128, pt) }, | |
| 2878 | 2921 | else => unreachable, |
| 2879 | 2922 | }; |
| 2880 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 2923 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 2881 | 2924 | .ty = float_type.toIntern(), |
| 2882 | 2925 | .storage = storage, |
| 2883 | } }))); | |
| 2926 | } })); | |
| 2884 | 2927 | } |
| 2885 | 2928 | |
| 2886 | 2929 | pub fn floatDivFloor( |
| ... | ... | @@ -2888,43 +2931,43 @@ pub fn floatDivFloor( |
| 2888 | 2931 | rhs: Value, |
| 2889 | 2932 | float_type: Type, |
| 2890 | 2933 | arena: Allocator, |
| 2891 | mod: *Module, | |
| 2934 | pt: Zcu.PerThread, | |
| 2892 | 2935 | ) !Value { |
| 2893 | if (float_type.zigTypeTag(mod) == .Vector) { | |
| 2894 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); | |
| 2895 | const scalar_ty = float_type.scalarType(mod); | |
| 2936 | if (float_type.zigTypeTag(pt.zcu) == .Vector) { | |
| 2937 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(pt.zcu)); | |
| 2938 | const scalar_ty = float_type.scalarType(pt.zcu); | |
| 2896 | 2939 | for (result_data, 0..) |*scalar, i| { |
| 2897 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2898 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2899 | scalar.* = (try floatDivFloorScalar(lhs_elem, rhs_elem, scalar_ty, mod)).toIntern(); | |
| 2940 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2941 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2942 | scalar.* = (try floatDivFloorScalar(lhs_elem, rhs_elem, scalar_ty, pt)).toIntern(); | |
| 2900 | 2943 | } |
| 2901 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2944 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2902 | 2945 | .ty = float_type.toIntern(), |
| 2903 | 2946 | .storage = .{ .elems = result_data }, |
| 2904 | } }))); | |
| 2947 | } })); | |
| 2905 | 2948 | } |
| 2906 | return floatDivFloorScalar(lhs, rhs, float_type, mod); | |
| 2949 | return floatDivFloorScalar(lhs, rhs, float_type, pt); | |
| 2907 | 2950 | } |
| 2908 | 2951 | |
| 2909 | 2952 | pub fn floatDivFloorScalar( |
| 2910 | 2953 | lhs: Value, |
| 2911 | 2954 | rhs: Value, |
| 2912 | 2955 | float_type: Type, |
| 2913 | mod: *Module, | |
| 2956 | pt: Zcu.PerThread, | |
| 2914 | 2957 | ) !Value { |
| 2915 | const target = mod.getTarget(); | |
| 2958 | const target = pt.zcu.getTarget(); | |
| 2916 | 2959 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 2917 | 16 => .{ .f16 = @divFloor(lhs.toFloat(f16, mod), rhs.toFloat(f16, mod)) }, | |
| 2918 | 32 => .{ .f32 = @divFloor(lhs.toFloat(f32, mod), rhs.toFloat(f32, mod)) }, | |
| 2919 | 64 => .{ .f64 = @divFloor(lhs.toFloat(f64, mod), rhs.toFloat(f64, mod)) }, | |
| 2920 | 80 => .{ .f80 = @divFloor(lhs.toFloat(f80, mod), rhs.toFloat(f80, mod)) }, | |
| 2921 | 128 => .{ .f128 = @divFloor(lhs.toFloat(f128, mod), rhs.toFloat(f128, mod)) }, | |
| 2960 | 16 => .{ .f16 = @divFloor(lhs.toFloat(f16, pt), rhs.toFloat(f16, pt)) }, | |
| 2961 | 32 => .{ .f32 = @divFloor(lhs.toFloat(f32, pt), rhs.toFloat(f32, pt)) }, | |
| 2962 | 64 => .{ .f64 = @divFloor(lhs.toFloat(f64, pt), rhs.toFloat(f64, pt)) }, | |
| 2963 | 80 => .{ .f80 = @divFloor(lhs.toFloat(f80, pt), rhs.toFloat(f80, pt)) }, | |
| 2964 | 128 => .{ .f128 = @divFloor(lhs.toFloat(f128, pt), rhs.toFloat(f128, pt)) }, | |
| 2922 | 2965 | else => unreachable, |
| 2923 | 2966 | }; |
| 2924 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 2967 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 2925 | 2968 | .ty = float_type.toIntern(), |
| 2926 | 2969 | .storage = storage, |
| 2927 | } }))); | |
| 2970 | } })); | |
| 2928 | 2971 | } |
| 2929 | 2972 | |
| 2930 | 2973 | pub fn floatDivTrunc( |
| ... | ... | @@ -2932,43 +2975,43 @@ pub fn floatDivTrunc( |
| 2932 | 2975 | rhs: Value, |
| 2933 | 2976 | float_type: Type, |
| 2934 | 2977 | arena: Allocator, |
| 2935 | mod: *Module, | |
| 2978 | pt: Zcu.PerThread, | |
| 2936 | 2979 | ) !Value { |
| 2937 | if (float_type.zigTypeTag(mod) == .Vector) { | |
| 2938 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); | |
| 2939 | const scalar_ty = float_type.scalarType(mod); | |
| 2980 | if (float_type.zigTypeTag(pt.zcu) == .Vector) { | |
| 2981 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(pt.zcu)); | |
| 2982 | const scalar_ty = float_type.scalarType(pt.zcu); | |
| 2940 | 2983 | for (result_data, 0..) |*scalar, i| { |
| 2941 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2942 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2943 | scalar.* = (try floatDivTruncScalar(lhs_elem, rhs_elem, scalar_ty, mod)).toIntern(); | |
| 2984 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 2985 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 2986 | scalar.* = (try floatDivTruncScalar(lhs_elem, rhs_elem, scalar_ty, pt)).toIntern(); | |
| 2944 | 2987 | } |
| 2945 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 2988 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2946 | 2989 | .ty = float_type.toIntern(), |
| 2947 | 2990 | .storage = .{ .elems = result_data }, |
| 2948 | } }))); | |
| 2991 | } })); | |
| 2949 | 2992 | } |
| 2950 | return floatDivTruncScalar(lhs, rhs, float_type, mod); | |
| 2993 | return floatDivTruncScalar(lhs, rhs, float_type, pt); | |
| 2951 | 2994 | } |
| 2952 | 2995 | |
| 2953 | 2996 | pub fn floatDivTruncScalar( |
| 2954 | 2997 | lhs: Value, |
| 2955 | 2998 | rhs: Value, |
| 2956 | 2999 | float_type: Type, |
| 2957 | mod: *Module, | |
| 3000 | pt: Zcu.PerThread, | |
| 2958 | 3001 | ) !Value { |
| 2959 | const target = mod.getTarget(); | |
| 3002 | const target = pt.zcu.getTarget(); | |
| 2960 | 3003 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 2961 | 16 => .{ .f16 = @divTrunc(lhs.toFloat(f16, mod), rhs.toFloat(f16, mod)) }, | |
| 2962 | 32 => .{ .f32 = @divTrunc(lhs.toFloat(f32, mod), rhs.toFloat(f32, mod)) }, | |
| 2963 | 64 => .{ .f64 = @divTrunc(lhs.toFloat(f64, mod), rhs.toFloat(f64, mod)) }, | |
| 2964 | 80 => .{ .f80 = @divTrunc(lhs.toFloat(f80, mod), rhs.toFloat(f80, mod)) }, | |
| 2965 | 128 => .{ .f128 = @divTrunc(lhs.toFloat(f128, mod), rhs.toFloat(f128, mod)) }, | |
| 3004 | 16 => .{ .f16 = @divTrunc(lhs.toFloat(f16, pt), rhs.toFloat(f16, pt)) }, | |
| 3005 | 32 => .{ .f32 = @divTrunc(lhs.toFloat(f32, pt), rhs.toFloat(f32, pt)) }, | |
| 3006 | 64 => .{ .f64 = @divTrunc(lhs.toFloat(f64, pt), rhs.toFloat(f64, pt)) }, | |
| 3007 | 80 => .{ .f80 = @divTrunc(lhs.toFloat(f80, pt), rhs.toFloat(f80, pt)) }, | |
| 3008 | 128 => .{ .f128 = @divTrunc(lhs.toFloat(f128, pt), rhs.toFloat(f128, pt)) }, | |
| 2966 | 3009 | else => unreachable, |
| 2967 | 3010 | }; |
| 2968 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3011 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 2969 | 3012 | .ty = float_type.toIntern(), |
| 2970 | 3013 | .storage = storage, |
| 2971 | } }))); | |
| 3014 | } })); | |
| 2972 | 3015 | } |
| 2973 | 3016 | |
| 2974 | 3017 | pub fn floatMul( |
| ... | ... | @@ -2976,510 +3019,539 @@ pub fn floatMul( |
| 2976 | 3019 | rhs: Value, |
| 2977 | 3020 | float_type: Type, |
| 2978 | 3021 | arena: Allocator, |
| 2979 | mod: *Module, | |
| 3022 | pt: Zcu.PerThread, | |
| 2980 | 3023 | ) !Value { |
| 3024 | const mod = pt.zcu; | |
| 2981 | 3025 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 2982 | 3026 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 2983 | 3027 | const scalar_ty = float_type.scalarType(mod); |
| 2984 | 3028 | for (result_data, 0..) |*scalar, i| { |
| 2985 | const lhs_elem = try lhs.elemValue(mod, i); | |
| 2986 | const rhs_elem = try rhs.elemValue(mod, i); | |
| 2987 | scalar.* = (try floatMulScalar(lhs_elem, rhs_elem, scalar_ty, mod)).toIntern(); | |
| 3029 | const lhs_elem = try lhs.elemValue(pt, i); | |
| 3030 | const rhs_elem = try rhs.elemValue(pt, i); | |
| 3031 | scalar.* = (try floatMulScalar(lhs_elem, rhs_elem, scalar_ty, pt)).toIntern(); | |
| 2988 | 3032 | } |
| 2989 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3033 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 2990 | 3034 | .ty = float_type.toIntern(), |
| 2991 | 3035 | .storage = .{ .elems = result_data }, |
| 2992 | } }))); | |
| 3036 | } })); | |
| 2993 | 3037 | } |
| 2994 | return floatMulScalar(lhs, rhs, float_type, mod); | |
| 3038 | return floatMulScalar(lhs, rhs, float_type, pt); | |
| 2995 | 3039 | } |
| 2996 | 3040 | |
| 2997 | 3041 | pub fn floatMulScalar( |
| 2998 | 3042 | lhs: Value, |
| 2999 | 3043 | rhs: Value, |
| 3000 | 3044 | float_type: Type, |
| 3001 | mod: *Module, | |
| 3045 | pt: Zcu.PerThread, | |
| 3002 | 3046 | ) !Value { |
| 3047 | const mod = pt.zcu; | |
| 3003 | 3048 | const target = mod.getTarget(); |
| 3004 | 3049 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3005 | 16 => .{ .f16 = lhs.toFloat(f16, mod) * rhs.toFloat(f16, mod) }, | |
| 3006 | 32 => .{ .f32 = lhs.toFloat(f32, mod) * rhs.toFloat(f32, mod) }, | |
| 3007 | 64 => .{ .f64 = lhs.toFloat(f64, mod) * rhs.toFloat(f64, mod) }, | |
| 3008 | 80 => .{ .f80 = lhs.toFloat(f80, mod) * rhs.toFloat(f80, mod) }, | |
| 3009 | 128 => .{ .f128 = lhs.toFloat(f128, mod) * rhs.toFloat(f128, mod) }, | |
| 3050 | 16 => .{ .f16 = lhs.toFloat(f16, pt) * rhs.toFloat(f16, pt) }, | |
| 3051 | 32 => .{ .f32 = lhs.toFloat(f32, pt) * rhs.toFloat(f32, pt) }, | |
| 3052 | 64 => .{ .f64 = lhs.toFloat(f64, pt) * rhs.toFloat(f64, pt) }, | |
| 3053 | 80 => .{ .f80 = lhs.toFloat(f80, pt) * rhs.toFloat(f80, pt) }, | |
| 3054 | 128 => .{ .f128 = lhs.toFloat(f128, pt) * rhs.toFloat(f128, pt) }, | |
| 3010 | 3055 | else => unreachable, |
| 3011 | 3056 | }; |
| 3012 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3057 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3013 | 3058 | .ty = float_type.toIntern(), |
| 3014 | 3059 | .storage = storage, |
| 3015 | } }))); | |
| 3060 | } })); | |
| 3016 | 3061 | } |
| 3017 | 3062 | |
| 3018 | pub fn sqrt(val: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 3019 | if (float_type.zigTypeTag(mod) == .Vector) { | |
| 3020 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); | |
| 3021 | const scalar_ty = float_type.scalarType(mod); | |
| 3063 | pub fn sqrt(val: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3064 | if (float_type.zigTypeTag(pt.zcu) == .Vector) { | |
| 3065 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(pt.zcu)); | |
| 3066 | const scalar_ty = float_type.scalarType(pt.zcu); | |
| 3022 | 3067 | for (result_data, 0..) |*scalar, i| { |
| 3023 | const elem_val = try val.elemValue(mod, i); | |
| 3024 | scalar.* = (try sqrtScalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 3068 | const elem_val = try val.elemValue(pt, i); | |
| 3069 | scalar.* = (try sqrtScalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 3025 | 3070 | } |
| 3026 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3071 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3027 | 3072 | .ty = float_type.toIntern(), |
| 3028 | 3073 | .storage = .{ .elems = result_data }, |
| 3029 | } }))); | |
| 3074 | } })); | |
| 3030 | 3075 | } |
| 3031 | return sqrtScalar(val, float_type, mod); | |
| 3076 | return sqrtScalar(val, float_type, pt); | |
| 3032 | 3077 | } |
| 3033 | 3078 | |
| 3034 | pub fn sqrtScalar(val: Value, float_type: Type, mod: *Module) Allocator.Error!Value { | |
| 3079 | pub fn sqrtScalar(val: Value, float_type: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 3080 | const mod = pt.zcu; | |
| 3035 | 3081 | const target = mod.getTarget(); |
| 3036 | 3082 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3037 | 16 => .{ .f16 = @sqrt(val.toFloat(f16, mod)) }, | |
| 3038 | 32 => .{ .f32 = @sqrt(val.toFloat(f32, mod)) }, | |
| 3039 | 64 => .{ .f64 = @sqrt(val.toFloat(f64, mod)) }, | |
| 3040 | 80 => .{ .f80 = @sqrt(val.toFloat(f80, mod)) }, | |
| 3041 | 128 => .{ .f128 = @sqrt(val.toFloat(f128, mod)) }, | |
| 3083 | 16 => .{ .f16 = @sqrt(val.toFloat(f16, pt)) }, | |
| 3084 | 32 => .{ .f32 = @sqrt(val.toFloat(f32, pt)) }, | |
| 3085 | 64 => .{ .f64 = @sqrt(val.toFloat(f64, pt)) }, | |
| 3086 | 80 => .{ .f80 = @sqrt(val.toFloat(f80, pt)) }, | |
| 3087 | 128 => .{ .f128 = @sqrt(val.toFloat(f128, pt)) }, | |
| 3042 | 3088 | else => unreachable, |
| 3043 | 3089 | }; |
| 3044 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3090 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3045 | 3091 | .ty = float_type.toIntern(), |
| 3046 | 3092 | .storage = storage, |
| 3047 | } }))); | |
| 3093 | } })); | |
| 3048 | 3094 | } |
| 3049 | 3095 | |
| 3050 | pub fn sin(val: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 3096 | pub fn sin(val: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3097 | const mod = pt.zcu; | |
| 3051 | 3098 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 3052 | 3099 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 3053 | 3100 | const scalar_ty = float_type.scalarType(mod); |
| 3054 | 3101 | for (result_data, 0..) |*scalar, i| { |
| 3055 | const elem_val = try val.elemValue(mod, i); | |
| 3056 | scalar.* = (try sinScalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 3102 | const elem_val = try val.elemValue(pt, i); | |
| 3103 | scalar.* = (try sinScalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 3057 | 3104 | } |
| 3058 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3105 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3059 | 3106 | .ty = float_type.toIntern(), |
| 3060 | 3107 | .storage = .{ .elems = result_data }, |
| 3061 | } }))); | |
| 3108 | } })); | |
| 3062 | 3109 | } |
| 3063 | return sinScalar(val, float_type, mod); | |
| 3110 | return sinScalar(val, float_type, pt); | |
| 3064 | 3111 | } |
| 3065 | 3112 | |
| 3066 | pub fn sinScalar(val: Value, float_type: Type, mod: *Module) Allocator.Error!Value { | |
| 3113 | pub fn sinScalar(val: Value, float_type: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 3114 | const mod = pt.zcu; | |
| 3067 | 3115 | const target = mod.getTarget(); |
| 3068 | 3116 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3069 | 16 => .{ .f16 = @sin(val.toFloat(f16, mod)) }, | |
| 3070 | 32 => .{ .f32 = @sin(val.toFloat(f32, mod)) }, | |
| 3071 | 64 => .{ .f64 = @sin(val.toFloat(f64, mod)) }, | |
| 3072 | 80 => .{ .f80 = @sin(val.toFloat(f80, mod)) }, | |
| 3073 | 128 => .{ .f128 = @sin(val.toFloat(f128, mod)) }, | |
| 3117 | 16 => .{ .f16 = @sin(val.toFloat(f16, pt)) }, | |
| 3118 | 32 => .{ .f32 = @sin(val.toFloat(f32, pt)) }, | |
| 3119 | 64 => .{ .f64 = @sin(val.toFloat(f64, pt)) }, | |
| 3120 | 80 => .{ .f80 = @sin(val.toFloat(f80, pt)) }, | |
| 3121 | 128 => .{ .f128 = @sin(val.toFloat(f128, pt)) }, | |
| 3074 | 3122 | else => unreachable, |
| 3075 | 3123 | }; |
| 3076 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3124 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3077 | 3125 | .ty = float_type.toIntern(), |
| 3078 | 3126 | .storage = storage, |
| 3079 | } }))); | |
| 3127 | } })); | |
| 3080 | 3128 | } |
| 3081 | 3129 | |
| 3082 | pub fn cos(val: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 3130 | pub fn cos(val: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3131 | const mod = pt.zcu; | |
| 3083 | 3132 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 3084 | 3133 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 3085 | 3134 | const scalar_ty = float_type.scalarType(mod); |
| 3086 | 3135 | for (result_data, 0..) |*scalar, i| { |
| 3087 | const elem_val = try val.elemValue(mod, i); | |
| 3088 | scalar.* = (try cosScalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 3136 | const elem_val = try val.elemValue(pt, i); | |
| 3137 | scalar.* = (try cosScalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 3089 | 3138 | } |
| 3090 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3139 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3091 | 3140 | .ty = float_type.toIntern(), |
| 3092 | 3141 | .storage = .{ .elems = result_data }, |
| 3093 | } }))); | |
| 3142 | } })); | |
| 3094 | 3143 | } |
| 3095 | return cosScalar(val, float_type, mod); | |
| 3144 | return cosScalar(val, float_type, pt); | |
| 3096 | 3145 | } |
| 3097 | 3146 | |
| 3098 | pub fn cosScalar(val: Value, float_type: Type, mod: *Module) Allocator.Error!Value { | |
| 3147 | pub fn cosScalar(val: Value, float_type: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 3148 | const mod = pt.zcu; | |
| 3099 | 3149 | const target = mod.getTarget(); |
| 3100 | 3150 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3101 | 16 => .{ .f16 = @cos(val.toFloat(f16, mod)) }, | |
| 3102 | 32 => .{ .f32 = @cos(val.toFloat(f32, mod)) }, | |
| 3103 | 64 => .{ .f64 = @cos(val.toFloat(f64, mod)) }, | |
| 3104 | 80 => .{ .f80 = @cos(val.toFloat(f80, mod)) }, | |
| 3105 | 128 => .{ .f128 = @cos(val.toFloat(f128, mod)) }, | |
| 3151 | 16 => .{ .f16 = @cos(val.toFloat(f16, pt)) }, | |
| 3152 | 32 => .{ .f32 = @cos(val.toFloat(f32, pt)) }, | |
| 3153 | 64 => .{ .f64 = @cos(val.toFloat(f64, pt)) }, | |
| 3154 | 80 => .{ .f80 = @cos(val.toFloat(f80, pt)) }, | |
| 3155 | 128 => .{ .f128 = @cos(val.toFloat(f128, pt)) }, | |
| 3106 | 3156 | else => unreachable, |
| 3107 | 3157 | }; |
| 3108 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3158 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3109 | 3159 | .ty = float_type.toIntern(), |
| 3110 | 3160 | .storage = storage, |
| 3111 | } }))); | |
| 3161 | } })); | |
| 3112 | 3162 | } |
| 3113 | 3163 | |
| 3114 | pub fn tan(val: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 3164 | pub fn tan(val: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3165 | const mod = pt.zcu; | |
| 3115 | 3166 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 3116 | 3167 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 3117 | 3168 | const scalar_ty = float_type.scalarType(mod); |
| 3118 | 3169 | for (result_data, 0..) |*scalar, i| { |
| 3119 | const elem_val = try val.elemValue(mod, i); | |
| 3120 | scalar.* = (try tanScalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 3170 | const elem_val = try val.elemValue(pt, i); | |
| 3171 | scalar.* = (try tanScalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 3121 | 3172 | } |
| 3122 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3173 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3123 | 3174 | .ty = float_type.toIntern(), |
| 3124 | 3175 | .storage = .{ .elems = result_data }, |
| 3125 | } }))); | |
| 3176 | } })); | |
| 3126 | 3177 | } |
| 3127 | return tanScalar(val, float_type, mod); | |
| 3178 | return tanScalar(val, float_type, pt); | |
| 3128 | 3179 | } |
| 3129 | 3180 | |
| 3130 | pub fn tanScalar(val: Value, float_type: Type, mod: *Module) Allocator.Error!Value { | |
| 3181 | pub fn tanScalar(val: Value, float_type: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 3182 | const mod = pt.zcu; | |
| 3131 | 3183 | const target = mod.getTarget(); |
| 3132 | 3184 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3133 | 16 => .{ .f16 = @tan(val.toFloat(f16, mod)) }, | |
| 3134 | 32 => .{ .f32 = @tan(val.toFloat(f32, mod)) }, | |
| 3135 | 64 => .{ .f64 = @tan(val.toFloat(f64, mod)) }, | |
| 3136 | 80 => .{ .f80 = @tan(val.toFloat(f80, mod)) }, | |
| 3137 | 128 => .{ .f128 = @tan(val.toFloat(f128, mod)) }, | |
| 3185 | 16 => .{ .f16 = @tan(val.toFloat(f16, pt)) }, | |
| 3186 | 32 => .{ .f32 = @tan(val.toFloat(f32, pt)) }, | |
| 3187 | 64 => .{ .f64 = @tan(val.toFloat(f64, pt)) }, | |
| 3188 | 80 => .{ .f80 = @tan(val.toFloat(f80, pt)) }, | |
| 3189 | 128 => .{ .f128 = @tan(val.toFloat(f128, pt)) }, | |
| 3138 | 3190 | else => unreachable, |
| 3139 | 3191 | }; |
| 3140 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3192 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3141 | 3193 | .ty = float_type.toIntern(), |
| 3142 | 3194 | .storage = storage, |
| 3143 | } }))); | |
| 3195 | } })); | |
| 3144 | 3196 | } |
| 3145 | 3197 | |
| 3146 | pub fn exp(val: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 3198 | pub fn exp(val: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3199 | const mod = pt.zcu; | |
| 3147 | 3200 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 3148 | 3201 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 3149 | 3202 | const scalar_ty = float_type.scalarType(mod); |
| 3150 | 3203 | for (result_data, 0..) |*scalar, i| { |
| 3151 | const elem_val = try val.elemValue(mod, i); | |
| 3152 | scalar.* = (try expScalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 3204 | const elem_val = try val.elemValue(pt, i); | |
| 3205 | scalar.* = (try expScalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 3153 | 3206 | } |
| 3154 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3207 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3155 | 3208 | .ty = float_type.toIntern(), |
| 3156 | 3209 | .storage = .{ .elems = result_data }, |
| 3157 | } }))); | |
| 3210 | } })); | |
| 3158 | 3211 | } |
| 3159 | return expScalar(val, float_type, mod); | |
| 3212 | return expScalar(val, float_type, pt); | |
| 3160 | 3213 | } |
| 3161 | 3214 | |
| 3162 | pub fn expScalar(val: Value, float_type: Type, mod: *Module) Allocator.Error!Value { | |
| 3215 | pub fn expScalar(val: Value, float_type: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 3216 | const mod = pt.zcu; | |
| 3163 | 3217 | const target = mod.getTarget(); |
| 3164 | 3218 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3165 | 16 => .{ .f16 = @exp(val.toFloat(f16, mod)) }, | |
| 3166 | 32 => .{ .f32 = @exp(val.toFloat(f32, mod)) }, | |
| 3167 | 64 => .{ .f64 = @exp(val.toFloat(f64, mod)) }, | |
| 3168 | 80 => .{ .f80 = @exp(val.toFloat(f80, mod)) }, | |
| 3169 | 128 => .{ .f128 = @exp(val.toFloat(f128, mod)) }, | |
| 3219 | 16 => .{ .f16 = @exp(val.toFloat(f16, pt)) }, | |
| 3220 | 32 => .{ .f32 = @exp(val.toFloat(f32, pt)) }, | |
| 3221 | 64 => .{ .f64 = @exp(val.toFloat(f64, pt)) }, | |
| 3222 | 80 => .{ .f80 = @exp(val.toFloat(f80, pt)) }, | |
| 3223 | 128 => .{ .f128 = @exp(val.toFloat(f128, pt)) }, | |
| 3170 | 3224 | else => unreachable, |
| 3171 | 3225 | }; |
| 3172 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3226 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3173 | 3227 | .ty = float_type.toIntern(), |
| 3174 | 3228 | .storage = storage, |
| 3175 | } }))); | |
| 3229 | } })); | |
| 3176 | 3230 | } |
| 3177 | 3231 | |
| 3178 | pub fn exp2(val: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 3232 | pub fn exp2(val: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3233 | const mod = pt.zcu; | |
| 3179 | 3234 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 3180 | 3235 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 3181 | 3236 | const scalar_ty = float_type.scalarType(mod); |
| 3182 | 3237 | for (result_data, 0..) |*scalar, i| { |
| 3183 | const elem_val = try val.elemValue(mod, i); | |
| 3184 | scalar.* = (try exp2Scalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 3238 | const elem_val = try val.elemValue(pt, i); | |
| 3239 | scalar.* = (try exp2Scalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 3185 | 3240 | } |
| 3186 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3241 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3187 | 3242 | .ty = float_type.toIntern(), |
| 3188 | 3243 | .storage = .{ .elems = result_data }, |
| 3189 | } }))); | |
| 3244 | } })); | |
| 3190 | 3245 | } |
| 3191 | return exp2Scalar(val, float_type, mod); | |
| 3246 | return exp2Scalar(val, float_type, pt); | |
| 3192 | 3247 | } |
| 3193 | 3248 | |
| 3194 | pub fn exp2Scalar(val: Value, float_type: Type, mod: *Module) Allocator.Error!Value { | |
| 3249 | pub fn exp2Scalar(val: Value, float_type: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 3250 | const mod = pt.zcu; | |
| 3195 | 3251 | const target = mod.getTarget(); |
| 3196 | 3252 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3197 | 16 => .{ .f16 = @exp2(val.toFloat(f16, mod)) }, | |
| 3198 | 32 => .{ .f32 = @exp2(val.toFloat(f32, mod)) }, | |
| 3199 | 64 => .{ .f64 = @exp2(val.toFloat(f64, mod)) }, | |
| 3200 | 80 => .{ .f80 = @exp2(val.toFloat(f80, mod)) }, | |
| 3201 | 128 => .{ .f128 = @exp2(val.toFloat(f128, mod)) }, | |
| 3253 | 16 => .{ .f16 = @exp2(val.toFloat(f16, pt)) }, | |
| 3254 | 32 => .{ .f32 = @exp2(val.toFloat(f32, pt)) }, | |
| 3255 | 64 => .{ .f64 = @exp2(val.toFloat(f64, pt)) }, | |
| 3256 | 80 => .{ .f80 = @exp2(val.toFloat(f80, pt)) }, | |
| 3257 | 128 => .{ .f128 = @exp2(val.toFloat(f128, pt)) }, | |
| 3202 | 3258 | else => unreachable, |
| 3203 | 3259 | }; |
| 3204 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3260 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3205 | 3261 | .ty = float_type.toIntern(), |
| 3206 | 3262 | .storage = storage, |
| 3207 | } }))); | |
| 3263 | } })); | |
| 3208 | 3264 | } |
| 3209 | 3265 | |
| 3210 | pub fn log(val: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 3266 | pub fn log(val: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3267 | const mod = pt.zcu; | |
| 3211 | 3268 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 3212 | 3269 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 3213 | 3270 | const scalar_ty = float_type.scalarType(mod); |
| 3214 | 3271 | for (result_data, 0..) |*scalar, i| { |
| 3215 | const elem_val = try val.elemValue(mod, i); | |
| 3216 | scalar.* = (try logScalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 3272 | const elem_val = try val.elemValue(pt, i); | |
| 3273 | scalar.* = (try logScalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 3217 | 3274 | } |
| 3218 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3275 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3219 | 3276 | .ty = float_type.toIntern(), |
| 3220 | 3277 | .storage = .{ .elems = result_data }, |
| 3221 | } }))); | |
| 3278 | } })); | |
| 3222 | 3279 | } |
| 3223 | return logScalar(val, float_type, mod); | |
| 3280 | return logScalar(val, float_type, pt); | |
| 3224 | 3281 | } |
| 3225 | 3282 | |
| 3226 | pub fn logScalar(val: Value, float_type: Type, mod: *Module) Allocator.Error!Value { | |
| 3283 | pub fn logScalar(val: Value, float_type: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 3284 | const mod = pt.zcu; | |
| 3227 | 3285 | const target = mod.getTarget(); |
| 3228 | 3286 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3229 | 16 => .{ .f16 = @log(val.toFloat(f16, mod)) }, | |
| 3230 | 32 => .{ .f32 = @log(val.toFloat(f32, mod)) }, | |
| 3231 | 64 => .{ .f64 = @log(val.toFloat(f64, mod)) }, | |
| 3232 | 80 => .{ .f80 = @log(val.toFloat(f80, mod)) }, | |
| 3233 | 128 => .{ .f128 = @log(val.toFloat(f128, mod)) }, | |
| 3287 | 16 => .{ .f16 = @log(val.toFloat(f16, pt)) }, | |
| 3288 | 32 => .{ .f32 = @log(val.toFloat(f32, pt)) }, | |
| 3289 | 64 => .{ .f64 = @log(val.toFloat(f64, pt)) }, | |
| 3290 | 80 => .{ .f80 = @log(val.toFloat(f80, pt)) }, | |
| 3291 | 128 => .{ .f128 = @log(val.toFloat(f128, pt)) }, | |
| 3234 | 3292 | else => unreachable, |
| 3235 | 3293 | }; |
| 3236 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3294 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3237 | 3295 | .ty = float_type.toIntern(), |
| 3238 | 3296 | .storage = storage, |
| 3239 | } }))); | |
| 3297 | } })); | |
| 3240 | 3298 | } |
| 3241 | 3299 | |
| 3242 | pub fn log2(val: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 3300 | pub fn log2(val: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3301 | const mod = pt.zcu; | |
| 3243 | 3302 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 3244 | 3303 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 3245 | 3304 | const scalar_ty = float_type.scalarType(mod); |
| 3246 | 3305 | for (result_data, 0..) |*scalar, i| { |
| 3247 | const elem_val = try val.elemValue(mod, i); | |
| 3248 | scalar.* = (try log2Scalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 3306 | const elem_val = try val.elemValue(pt, i); | |
| 3307 | scalar.* = (try log2Scalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 3249 | 3308 | } |
| 3250 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3309 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3251 | 3310 | .ty = float_type.toIntern(), |
| 3252 | 3311 | .storage = .{ .elems = result_data }, |
| 3253 | } }))); | |
| 3312 | } })); | |
| 3254 | 3313 | } |
| 3255 | return log2Scalar(val, float_type, mod); | |
| 3314 | return log2Scalar(val, float_type, pt); | |
| 3256 | 3315 | } |
| 3257 | 3316 | |
| 3258 | pub fn log2Scalar(val: Value, float_type: Type, mod: *Module) Allocator.Error!Value { | |
| 3317 | pub fn log2Scalar(val: Value, float_type: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 3318 | const mod = pt.zcu; | |
| 3259 | 3319 | const target = mod.getTarget(); |
| 3260 | 3320 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3261 | 16 => .{ .f16 = @log2(val.toFloat(f16, mod)) }, | |
| 3262 | 32 => .{ .f32 = @log2(val.toFloat(f32, mod)) }, | |
| 3263 | 64 => .{ .f64 = @log2(val.toFloat(f64, mod)) }, | |
| 3264 | 80 => .{ .f80 = @log2(val.toFloat(f80, mod)) }, | |
| 3265 | 128 => .{ .f128 = @log2(val.toFloat(f128, mod)) }, | |
| 3321 | 16 => .{ .f16 = @log2(val.toFloat(f16, pt)) }, | |
| 3322 | 32 => .{ .f32 = @log2(val.toFloat(f32, pt)) }, | |
| 3323 | 64 => .{ .f64 = @log2(val.toFloat(f64, pt)) }, | |
| 3324 | 80 => .{ .f80 = @log2(val.toFloat(f80, pt)) }, | |
| 3325 | 128 => .{ .f128 = @log2(val.toFloat(f128, pt)) }, | |
| 3266 | 3326 | else => unreachable, |
| 3267 | 3327 | }; |
| 3268 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3328 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3269 | 3329 | .ty = float_type.toIntern(), |
| 3270 | 3330 | .storage = storage, |
| 3271 | } }))); | |
| 3331 | } })); | |
| 3272 | 3332 | } |
| 3273 | 3333 | |
| 3274 | pub fn log10(val: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 3334 | pub fn log10(val: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3335 | const mod = pt.zcu; | |
| 3275 | 3336 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 3276 | 3337 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 3277 | 3338 | const scalar_ty = float_type.scalarType(mod); |
| 3278 | 3339 | for (result_data, 0..) |*scalar, i| { |
| 3279 | const elem_val = try val.elemValue(mod, i); | |
| 3280 | scalar.* = (try log10Scalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 3340 | const elem_val = try val.elemValue(pt, i); | |
| 3341 | scalar.* = (try log10Scalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 3281 | 3342 | } |
| 3282 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3343 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3283 | 3344 | .ty = float_type.toIntern(), |
| 3284 | 3345 | .storage = .{ .elems = result_data }, |
| 3285 | } }))); | |
| 3346 | } })); | |
| 3286 | 3347 | } |
| 3287 | return log10Scalar(val, float_type, mod); | |
| 3348 | return log10Scalar(val, float_type, pt); | |
| 3288 | 3349 | } |
| 3289 | 3350 | |
| 3290 | pub fn log10Scalar(val: Value, float_type: Type, mod: *Module) Allocator.Error!Value { | |
| 3351 | pub fn log10Scalar(val: Value, float_type: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 3352 | const mod = pt.zcu; | |
| 3291 | 3353 | const target = mod.getTarget(); |
| 3292 | 3354 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3293 | 16 => .{ .f16 = @log10(val.toFloat(f16, mod)) }, | |
| 3294 | 32 => .{ .f32 = @log10(val.toFloat(f32, mod)) }, | |
| 3295 | 64 => .{ .f64 = @log10(val.toFloat(f64, mod)) }, | |
| 3296 | 80 => .{ .f80 = @log10(val.toFloat(f80, mod)) }, | |
| 3297 | 128 => .{ .f128 = @log10(val.toFloat(f128, mod)) }, | |
| 3355 | 16 => .{ .f16 = @log10(val.toFloat(f16, pt)) }, | |
| 3356 | 32 => .{ .f32 = @log10(val.toFloat(f32, pt)) }, | |
| 3357 | 64 => .{ .f64 = @log10(val.toFloat(f64, pt)) }, | |
| 3358 | 80 => .{ .f80 = @log10(val.toFloat(f80, pt)) }, | |
| 3359 | 128 => .{ .f128 = @log10(val.toFloat(f128, pt)) }, | |
| 3298 | 3360 | else => unreachable, |
| 3299 | 3361 | }; |
| 3300 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3362 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3301 | 3363 | .ty = float_type.toIntern(), |
| 3302 | 3364 | .storage = storage, |
| 3303 | } }))); | |
| 3365 | } })); | |
| 3304 | 3366 | } |
| 3305 | 3367 | |
| 3306 | pub fn abs(val: Value, ty: Type, arena: Allocator, mod: *Module) !Value { | |
| 3368 | pub fn abs(val: Value, ty: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3369 | const mod = pt.zcu; | |
| 3307 | 3370 | if (ty.zigTypeTag(mod) == .Vector) { |
| 3308 | 3371 | const result_data = try arena.alloc(InternPool.Index, ty.vectorLen(mod)); |
| 3309 | 3372 | const scalar_ty = ty.scalarType(mod); |
| 3310 | 3373 | for (result_data, 0..) |*scalar, i| { |
| 3311 | const elem_val = try val.elemValue(mod, i); | |
| 3312 | scalar.* = (try absScalar(elem_val, scalar_ty, mod, arena)).toIntern(); | |
| 3374 | const elem_val = try val.elemValue(pt, i); | |
| 3375 | scalar.* = (try absScalar(elem_val, scalar_ty, pt, arena)).toIntern(); | |
| 3313 | 3376 | } |
| 3314 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3377 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3315 | 3378 | .ty = ty.toIntern(), |
| 3316 | 3379 | .storage = .{ .elems = result_data }, |
| 3317 | } }))); | |
| 3380 | } })); | |
| 3318 | 3381 | } |
| 3319 | return absScalar(val, ty, mod, arena); | |
| 3382 | return absScalar(val, ty, pt, arena); | |
| 3320 | 3383 | } |
| 3321 | 3384 | |
| 3322 | pub fn absScalar(val: Value, ty: Type, mod: *Module, arena: Allocator) Allocator.Error!Value { | |
| 3385 | pub fn absScalar(val: Value, ty: Type, pt: Zcu.PerThread, arena: Allocator) Allocator.Error!Value { | |
| 3386 | const mod = pt.zcu; | |
| 3323 | 3387 | switch (ty.zigTypeTag(mod)) { |
| 3324 | 3388 | .Int => { |
| 3325 | 3389 | var buffer: Value.BigIntSpace = undefined; |
| 3326 | var operand_bigint = try val.toBigInt(&buffer, mod).toManaged(arena); | |
| 3390 | var operand_bigint = try val.toBigInt(&buffer, pt).toManaged(arena); | |
| 3327 | 3391 | operand_bigint.abs(); |
| 3328 | 3392 | |
| 3329 | return mod.intValue_big(try ty.toUnsigned(mod), operand_bigint.toConst()); | |
| 3393 | return pt.intValue_big(try ty.toUnsigned(pt), operand_bigint.toConst()); | |
| 3330 | 3394 | }, |
| 3331 | 3395 | .ComptimeInt => { |
| 3332 | 3396 | var buffer: Value.BigIntSpace = undefined; |
| 3333 | var operand_bigint = try val.toBigInt(&buffer, mod).toManaged(arena); | |
| 3397 | var operand_bigint = try val.toBigInt(&buffer, pt).toManaged(arena); | |
| 3334 | 3398 | operand_bigint.abs(); |
| 3335 | 3399 | |
| 3336 | return mod.intValue_big(ty, operand_bigint.toConst()); | |
| 3400 | return pt.intValue_big(ty, operand_bigint.toConst()); | |
| 3337 | 3401 | }, |
| 3338 | 3402 | .ComptimeFloat, .Float => { |
| 3339 | 3403 | const target = mod.getTarget(); |
| 3340 | 3404 | const storage: InternPool.Key.Float.Storage = switch (ty.floatBits(target)) { |
| 3341 | 16 => .{ .f16 = @abs(val.toFloat(f16, mod)) }, | |
| 3342 | 32 => .{ .f32 = @abs(val.toFloat(f32, mod)) }, | |
| 3343 | 64 => .{ .f64 = @abs(val.toFloat(f64, mod)) }, | |
| 3344 | 80 => .{ .f80 = @abs(val.toFloat(f80, mod)) }, | |
| 3345 | 128 => .{ .f128 = @abs(val.toFloat(f128, mod)) }, | |
| 3405 | 16 => .{ .f16 = @abs(val.toFloat(f16, pt)) }, | |
| 3406 | 32 => .{ .f32 = @abs(val.toFloat(f32, pt)) }, | |
| 3407 | 64 => .{ .f64 = @abs(val.toFloat(f64, pt)) }, | |
| 3408 | 80 => .{ .f80 = @abs(val.toFloat(f80, pt)) }, | |
| 3409 | 128 => .{ .f128 = @abs(val.toFloat(f128, pt)) }, | |
| 3346 | 3410 | else => unreachable, |
| 3347 | 3411 | }; |
| 3348 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3412 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3349 | 3413 | .ty = ty.toIntern(), |
| 3350 | 3414 | .storage = storage, |
| 3351 | } }))); | |
| 3415 | } })); | |
| 3352 | 3416 | }, |
| 3353 | 3417 | else => unreachable, |
| 3354 | 3418 | } |
| 3355 | 3419 | } |
| 3356 | 3420 | |
| 3357 | pub fn floor(val: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 3421 | pub fn floor(val: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3422 | const mod = pt.zcu; | |
| 3358 | 3423 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 3359 | 3424 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 3360 | 3425 | const scalar_ty = float_type.scalarType(mod); |
| 3361 | 3426 | for (result_data, 0..) |*scalar, i| { |
| 3362 | const elem_val = try val.elemValue(mod, i); | |
| 3363 | scalar.* = (try floorScalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 3427 | const elem_val = try val.elemValue(pt, i); | |
| 3428 | scalar.* = (try floorScalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 3364 | 3429 | } |
| 3365 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3430 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3366 | 3431 | .ty = float_type.toIntern(), |
| 3367 | 3432 | .storage = .{ .elems = result_data }, |
| 3368 | } }))); | |
| 3433 | } })); | |
| 3369 | 3434 | } |
| 3370 | return floorScalar(val, float_type, mod); | |
| 3435 | return floorScalar(val, float_type, pt); | |
| 3371 | 3436 | } |
| 3372 | 3437 | |
| 3373 | pub fn floorScalar(val: Value, float_type: Type, mod: *Module) Allocator.Error!Value { | |
| 3438 | pub fn floorScalar(val: Value, float_type: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 3439 | const mod = pt.zcu; | |
| 3374 | 3440 | const target = mod.getTarget(); |
| 3375 | 3441 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3376 | 16 => .{ .f16 = @floor(val.toFloat(f16, mod)) }, | |
| 3377 | 32 => .{ .f32 = @floor(val.toFloat(f32, mod)) }, | |
| 3378 | 64 => .{ .f64 = @floor(val.toFloat(f64, mod)) }, | |
| 3379 | 80 => .{ .f80 = @floor(val.toFloat(f80, mod)) }, | |
| 3380 | 128 => .{ .f128 = @floor(val.toFloat(f128, mod)) }, | |
| 3442 | 16 => .{ .f16 = @floor(val.toFloat(f16, pt)) }, | |
| 3443 | 32 => .{ .f32 = @floor(val.toFloat(f32, pt)) }, | |
| 3444 | 64 => .{ .f64 = @floor(val.toFloat(f64, pt)) }, | |
| 3445 | 80 => .{ .f80 = @floor(val.toFloat(f80, pt)) }, | |
| 3446 | 128 => .{ .f128 = @floor(val.toFloat(f128, pt)) }, | |
| 3381 | 3447 | else => unreachable, |
| 3382 | 3448 | }; |
| 3383 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3449 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3384 | 3450 | .ty = float_type.toIntern(), |
| 3385 | 3451 | .storage = storage, |
| 3386 | } }))); | |
| 3452 | } })); | |
| 3387 | 3453 | } |
| 3388 | 3454 | |
| 3389 | pub fn ceil(val: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 3455 | pub fn ceil(val: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3456 | const mod = pt.zcu; | |
| 3390 | 3457 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 3391 | 3458 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 3392 | 3459 | const scalar_ty = float_type.scalarType(mod); |
| 3393 | 3460 | for (result_data, 0..) |*scalar, i| { |
| 3394 | const elem_val = try val.elemValue(mod, i); | |
| 3395 | scalar.* = (try ceilScalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 3461 | const elem_val = try val.elemValue(pt, i); | |
| 3462 | scalar.* = (try ceilScalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 3396 | 3463 | } |
| 3397 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3464 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3398 | 3465 | .ty = float_type.toIntern(), |
| 3399 | 3466 | .storage = .{ .elems = result_data }, |
| 3400 | } }))); | |
| 3467 | } })); | |
| 3401 | 3468 | } |
| 3402 | return ceilScalar(val, float_type, mod); | |
| 3469 | return ceilScalar(val, float_type, pt); | |
| 3403 | 3470 | } |
| 3404 | 3471 | |
| 3405 | pub fn ceilScalar(val: Value, float_type: Type, mod: *Module) Allocator.Error!Value { | |
| 3472 | pub fn ceilScalar(val: Value, float_type: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 3473 | const mod = pt.zcu; | |
| 3406 | 3474 | const target = mod.getTarget(); |
| 3407 | 3475 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3408 | 16 => .{ .f16 = @ceil(val.toFloat(f16, mod)) }, | |
| 3409 | 32 => .{ .f32 = @ceil(val.toFloat(f32, mod)) }, | |
| 3410 | 64 => .{ .f64 = @ceil(val.toFloat(f64, mod)) }, | |
| 3411 | 80 => .{ .f80 = @ceil(val.toFloat(f80, mod)) }, | |
| 3412 | 128 => .{ .f128 = @ceil(val.toFloat(f128, mod)) }, | |
| 3476 | 16 => .{ .f16 = @ceil(val.toFloat(f16, pt)) }, | |
| 3477 | 32 => .{ .f32 = @ceil(val.toFloat(f32, pt)) }, | |
| 3478 | 64 => .{ .f64 = @ceil(val.toFloat(f64, pt)) }, | |
| 3479 | 80 => .{ .f80 = @ceil(val.toFloat(f80, pt)) }, | |
| 3480 | 128 => .{ .f128 = @ceil(val.toFloat(f128, pt)) }, | |
| 3413 | 3481 | else => unreachable, |
| 3414 | 3482 | }; |
| 3415 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3483 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3416 | 3484 | .ty = float_type.toIntern(), |
| 3417 | 3485 | .storage = storage, |
| 3418 | } }))); | |
| 3486 | } })); | |
| 3419 | 3487 | } |
| 3420 | 3488 | |
| 3421 | pub fn round(val: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 3489 | pub fn round(val: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3490 | const mod = pt.zcu; | |
| 3422 | 3491 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 3423 | 3492 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 3424 | 3493 | const scalar_ty = float_type.scalarType(mod); |
| 3425 | 3494 | for (result_data, 0..) |*scalar, i| { |
| 3426 | const elem_val = try val.elemValue(mod, i); | |
| 3427 | scalar.* = (try roundScalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 3495 | const elem_val = try val.elemValue(pt, i); | |
| 3496 | scalar.* = (try roundScalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 3428 | 3497 | } |
| 3429 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3498 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3430 | 3499 | .ty = float_type.toIntern(), |
| 3431 | 3500 | .storage = .{ .elems = result_data }, |
| 3432 | } }))); | |
| 3501 | } })); | |
| 3433 | 3502 | } |
| 3434 | return roundScalar(val, float_type, mod); | |
| 3503 | return roundScalar(val, float_type, pt); | |
| 3435 | 3504 | } |
| 3436 | 3505 | |
| 3437 | pub fn roundScalar(val: Value, float_type: Type, mod: *Module) Allocator.Error!Value { | |
| 3506 | pub fn roundScalar(val: Value, float_type: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 3507 | const mod = pt.zcu; | |
| 3438 | 3508 | const target = mod.getTarget(); |
| 3439 | 3509 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3440 | 16 => .{ .f16 = @round(val.toFloat(f16, mod)) }, | |
| 3441 | 32 => .{ .f32 = @round(val.toFloat(f32, mod)) }, | |
| 3442 | 64 => .{ .f64 = @round(val.toFloat(f64, mod)) }, | |
| 3443 | 80 => .{ .f80 = @round(val.toFloat(f80, mod)) }, | |
| 3444 | 128 => .{ .f128 = @round(val.toFloat(f128, mod)) }, | |
| 3510 | 16 => .{ .f16 = @round(val.toFloat(f16, pt)) }, | |
| 3511 | 32 => .{ .f32 = @round(val.toFloat(f32, pt)) }, | |
| 3512 | 64 => .{ .f64 = @round(val.toFloat(f64, pt)) }, | |
| 3513 | 80 => .{ .f80 = @round(val.toFloat(f80, pt)) }, | |
| 3514 | 128 => .{ .f128 = @round(val.toFloat(f128, pt)) }, | |
| 3445 | 3515 | else => unreachable, |
| 3446 | 3516 | }; |
| 3447 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3517 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3448 | 3518 | .ty = float_type.toIntern(), |
| 3449 | 3519 | .storage = storage, |
| 3450 | } }))); | |
| 3520 | } })); | |
| 3451 | 3521 | } |
| 3452 | 3522 | |
| 3453 | pub fn trunc(val: Value, float_type: Type, arena: Allocator, mod: *Module) !Value { | |
| 3523 | pub fn trunc(val: Value, float_type: Type, arena: Allocator, pt: Zcu.PerThread) !Value { | |
| 3524 | const mod = pt.zcu; | |
| 3454 | 3525 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 3455 | 3526 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 3456 | 3527 | const scalar_ty = float_type.scalarType(mod); |
| 3457 | 3528 | for (result_data, 0..) |*scalar, i| { |
| 3458 | const elem_val = try val.elemValue(mod, i); | |
| 3459 | scalar.* = (try truncScalar(elem_val, scalar_ty, mod)).toIntern(); | |
| 3529 | const elem_val = try val.elemValue(pt, i); | |
| 3530 | scalar.* = (try truncScalar(elem_val, scalar_ty, pt)).toIntern(); | |
| 3460 | 3531 | } |
| 3461 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3532 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3462 | 3533 | .ty = float_type.toIntern(), |
| 3463 | 3534 | .storage = .{ .elems = result_data }, |
| 3464 | } }))); | |
| 3535 | } })); | |
| 3465 | 3536 | } |
| 3466 | return truncScalar(val, float_type, mod); | |
| 3537 | return truncScalar(val, float_type, pt); | |
| 3467 | 3538 | } |
| 3468 | 3539 | |
| 3469 | pub fn truncScalar(val: Value, float_type: Type, mod: *Module) Allocator.Error!Value { | |
| 3540 | pub fn truncScalar(val: Value, float_type: Type, pt: Zcu.PerThread) Allocator.Error!Value { | |
| 3541 | const mod = pt.zcu; | |
| 3470 | 3542 | const target = mod.getTarget(); |
| 3471 | 3543 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3472 | 16 => .{ .f16 = @trunc(val.toFloat(f16, mod)) }, | |
| 3473 | 32 => .{ .f32 = @trunc(val.toFloat(f32, mod)) }, | |
| 3474 | 64 => .{ .f64 = @trunc(val.toFloat(f64, mod)) }, | |
| 3475 | 80 => .{ .f80 = @trunc(val.toFloat(f80, mod)) }, | |
| 3476 | 128 => .{ .f128 = @trunc(val.toFloat(f128, mod)) }, | |
| 3544 | 16 => .{ .f16 = @trunc(val.toFloat(f16, pt)) }, | |
| 3545 | 32 => .{ .f32 = @trunc(val.toFloat(f32, pt)) }, | |
| 3546 | 64 => .{ .f64 = @trunc(val.toFloat(f64, pt)) }, | |
| 3547 | 80 => .{ .f80 = @trunc(val.toFloat(f80, pt)) }, | |
| 3548 | 128 => .{ .f128 = @trunc(val.toFloat(f128, pt)) }, | |
| 3477 | 3549 | else => unreachable, |
| 3478 | 3550 | }; |
| 3479 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3551 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3480 | 3552 | .ty = float_type.toIntern(), |
| 3481 | 3553 | .storage = storage, |
| 3482 | } }))); | |
| 3554 | } })); | |
| 3483 | 3555 | } |
| 3484 | 3556 | |
| 3485 | 3557 | pub fn mulAdd( |
| ... | ... | @@ -3488,23 +3560,24 @@ pub fn mulAdd( |
| 3488 | 3560 | mulend2: Value, |
| 3489 | 3561 | addend: Value, |
| 3490 | 3562 | arena: Allocator, |
| 3491 | mod: *Module, | |
| 3563 | pt: Zcu.PerThread, | |
| 3492 | 3564 | ) !Value { |
| 3565 | const mod = pt.zcu; | |
| 3493 | 3566 | if (float_type.zigTypeTag(mod) == .Vector) { |
| 3494 | 3567 | const result_data = try arena.alloc(InternPool.Index, float_type.vectorLen(mod)); |
| 3495 | 3568 | const scalar_ty = float_type.scalarType(mod); |
| 3496 | 3569 | for (result_data, 0..) |*scalar, i| { |
| 3497 | const mulend1_elem = try mulend1.elemValue(mod, i); | |
| 3498 | const mulend2_elem = try mulend2.elemValue(mod, i); | |
| 3499 | const addend_elem = try addend.elemValue(mod, i); | |
| 3500 | scalar.* = (try mulAddScalar(scalar_ty, mulend1_elem, mulend2_elem, addend_elem, mod)).toIntern(); | |
| 3570 | const mulend1_elem = try mulend1.elemValue(pt, i); | |
| 3571 | const mulend2_elem = try mulend2.elemValue(pt, i); | |
| 3572 | const addend_elem = try addend.elemValue(pt, i); | |
| 3573 | scalar.* = (try mulAddScalar(scalar_ty, mulend1_elem, mulend2_elem, addend_elem, pt)).toIntern(); | |
| 3501 | 3574 | } |
| 3502 | return Value.fromInterned((try mod.intern(.{ .aggregate = .{ | |
| 3575 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 3503 | 3576 | .ty = float_type.toIntern(), |
| 3504 | 3577 | .storage = .{ .elems = result_data }, |
| 3505 | } }))); | |
| 3578 | } })); | |
| 3506 | 3579 | } |
| 3507 | return mulAddScalar(float_type, mulend1, mulend2, addend, mod); | |
| 3580 | return mulAddScalar(float_type, mulend1, mulend2, addend, pt); | |
| 3508 | 3581 | } |
| 3509 | 3582 | |
| 3510 | 3583 | pub fn mulAddScalar( |
| ... | ... | @@ -3512,32 +3585,33 @@ pub fn mulAddScalar( |
| 3512 | 3585 | mulend1: Value, |
| 3513 | 3586 | mulend2: Value, |
| 3514 | 3587 | addend: Value, |
| 3515 | mod: *Module, | |
| 3588 | pt: Zcu.PerThread, | |
| 3516 | 3589 | ) Allocator.Error!Value { |
| 3590 | const mod = pt.zcu; | |
| 3517 | 3591 | const target = mod.getTarget(); |
| 3518 | 3592 | const storage: InternPool.Key.Float.Storage = switch (float_type.floatBits(target)) { |
| 3519 | 16 => .{ .f16 = @mulAdd(f16, mulend1.toFloat(f16, mod), mulend2.toFloat(f16, mod), addend.toFloat(f16, mod)) }, | |
| 3520 | 32 => .{ .f32 = @mulAdd(f32, mulend1.toFloat(f32, mod), mulend2.toFloat(f32, mod), addend.toFloat(f32, mod)) }, | |
| 3521 | 64 => .{ .f64 = @mulAdd(f64, mulend1.toFloat(f64, mod), mulend2.toFloat(f64, mod), addend.toFloat(f64, mod)) }, | |
| 3522 | 80 => .{ .f80 = @mulAdd(f80, mulend1.toFloat(f80, mod), mulend2.toFloat(f80, mod), addend.toFloat(f80, mod)) }, | |
| 3523 | 128 => .{ .f128 = @mulAdd(f128, mulend1.toFloat(f128, mod), mulend2.toFloat(f128, mod), addend.toFloat(f128, mod)) }, | |
| 3593 | 16 => .{ .f16 = @mulAdd(f16, mulend1.toFloat(f16, pt), mulend2.toFloat(f16, pt), addend.toFloat(f16, pt)) }, | |
| 3594 | 32 => .{ .f32 = @mulAdd(f32, mulend1.toFloat(f32, pt), mulend2.toFloat(f32, pt), addend.toFloat(f32, pt)) }, | |
| 3595 | 64 => .{ .f64 = @mulAdd(f64, mulend1.toFloat(f64, pt), mulend2.toFloat(f64, pt), addend.toFloat(f64, pt)) }, | |
| 3596 | 80 => .{ .f80 = @mulAdd(f80, mulend1.toFloat(f80, pt), mulend2.toFloat(f80, pt), addend.toFloat(f80, pt)) }, | |
| 3597 | 128 => .{ .f128 = @mulAdd(f128, mulend1.toFloat(f128, pt), mulend2.toFloat(f128, pt), addend.toFloat(f128, pt)) }, | |
| 3524 | 3598 | else => unreachable, |
| 3525 | 3599 | }; |
| 3526 | return Value.fromInterned((try mod.intern(.{ .float = .{ | |
| 3600 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 3527 | 3601 | .ty = float_type.toIntern(), |
| 3528 | 3602 | .storage = storage, |
| 3529 | } }))); | |
| 3603 | } })); | |
| 3530 | 3604 | } |
| 3531 | 3605 | |
| 3532 | 3606 | /// If the value is represented in-memory as a series of bytes that all |
| 3533 | 3607 | /// have the same value, return that byte value, otherwise null. |
| 3534 | pub fn hasRepeatedByteRepr(val: Value, ty: Type, mod: *Module) !?u8 { | |
| 3535 | const abi_size = std.math.cast(usize, ty.abiSize(mod)) orelse return null; | |
| 3608 | pub fn hasRepeatedByteRepr(val: Value, ty: Type, pt: Zcu.PerThread) !?u8 { | |
| 3609 | const abi_size = std.math.cast(usize, ty.abiSize(pt)) orelse return null; | |
| 3536 | 3610 | assert(abi_size >= 1); |
| 3537 | const byte_buffer = try mod.gpa.alloc(u8, abi_size); | |
| 3538 | defer mod.gpa.free(byte_buffer); | |
| 3611 | const byte_buffer = try pt.zcu.gpa.alloc(u8, abi_size); | |
| 3612 | defer pt.zcu.gpa.free(byte_buffer); | |
| 3539 | 3613 | |
| 3540 | writeToMemory(val, ty, mod, byte_buffer) catch |err| switch (err) { | |
| 3614 | writeToMemory(val, ty, pt, byte_buffer) catch |err| switch (err) { | |
| 3541 | 3615 | error.OutOfMemory => return error.OutOfMemory, |
| 3542 | 3616 | error.ReinterpretDeclRef => return null, |
| 3543 | 3617 | // TODO: The writeToMemory function was originally created for the purpose |
| ... | ... | @@ -3567,13 +3641,13 @@ pub fn typeOf(val: Value, zcu: *const Zcu) Type { |
| 3567 | 3641 | /// If `val` is not undef, the bounds are both `val`. |
| 3568 | 3642 | /// If `val` is undef and has a fixed-width type, the bounds are the bounds of the type. |
| 3569 | 3643 | /// If `val` is undef and is a `comptime_int`, returns null. |
| 3570 | pub fn intValueBounds(val: Value, mod: *Module) !?[2]Value { | |
| 3571 | if (!val.isUndef(mod)) return .{ val, val }; | |
| 3572 | const ty = mod.intern_pool.typeOf(val.toIntern()); | |
| 3644 | pub fn intValueBounds(val: Value, pt: Zcu.PerThread) !?[2]Value { | |
| 3645 | if (!val.isUndef(pt.zcu)) return .{ val, val }; | |
| 3646 | const ty = pt.zcu.intern_pool.typeOf(val.toIntern()); | |
| 3573 | 3647 | if (ty == .comptime_int_type) return null; |
| 3574 | 3648 | return .{ |
| 3575 | try Type.fromInterned(ty).minInt(mod, Type.fromInterned(ty)), | |
| 3576 | try Type.fromInterned(ty).maxInt(mod, Type.fromInterned(ty)), | |
| 3649 | try Type.fromInterned(ty).minInt(pt, Type.fromInterned(ty)), | |
| 3650 | try Type.fromInterned(ty).maxInt(pt, Type.fromInterned(ty)), | |
| 3577 | 3651 | }; |
| 3578 | 3652 | } |
| 3579 | 3653 | |
| ... | ... | @@ -3604,14 +3678,15 @@ pub const RuntimeIndex = InternPool.RuntimeIndex; |
| 3604 | 3678 | /// `parent_ptr` must be a single-pointer to some optional. |
| 3605 | 3679 | /// Returns a pointer to the payload of the optional. |
| 3606 | 3680 | /// May perform type resolution. |
| 3607 | pub fn ptrOptPayload(parent_ptr: Value, zcu: *Zcu) !Value { | |
| 3681 | pub fn ptrOptPayload(parent_ptr: Value, pt: Zcu.PerThread) !Value { | |
| 3682 | const zcu = pt.zcu; | |
| 3608 | 3683 | const parent_ptr_ty = parent_ptr.typeOf(zcu); |
| 3609 | 3684 | const opt_ty = parent_ptr_ty.childType(zcu); |
| 3610 | 3685 | |
| 3611 | 3686 | assert(parent_ptr_ty.ptrSize(zcu) == .One); |
| 3612 | 3687 | assert(opt_ty.zigTypeTag(zcu) == .Optional); |
| 3613 | 3688 | |
| 3614 | const result_ty = try zcu.ptrTypeSema(info: { | |
| 3689 | const result_ty = try pt.ptrTypeSema(info: { | |
| 3615 | 3690 | var new = parent_ptr_ty.ptrInfo(zcu); |
| 3616 | 3691 | // We can correctly preserve alignment `.none`, since an optional has the same |
| 3617 | 3692 | // natural alignment as its child type. |
| ... | ... | @@ -3619,15 +3694,15 @@ pub fn ptrOptPayload(parent_ptr: Value, zcu: *Zcu) !Value { |
| 3619 | 3694 | break :info new; |
| 3620 | 3695 | }); |
| 3621 | 3696 | |
| 3622 | if (parent_ptr.isUndef(zcu)) return zcu.undefValue(result_ty); | |
| 3697 | if (parent_ptr.isUndef(zcu)) return pt.undefValue(result_ty); | |
| 3623 | 3698 | |
| 3624 | 3699 | if (opt_ty.isPtrLikeOptional(zcu)) { |
| 3625 | 3700 | // Just reinterpret the pointer, since the layout is well-defined |
| 3626 | return zcu.getCoerced(parent_ptr, result_ty); | |
| 3701 | return pt.getCoerced(parent_ptr, result_ty); | |
| 3627 | 3702 | } |
| 3628 | 3703 | |
| 3629 | const base_ptr = try parent_ptr.canonicalizeBasePtr(.One, opt_ty, zcu); | |
| 3630 | return Value.fromInterned(try zcu.intern(.{ .ptr = .{ | |
| 3704 | const base_ptr = try parent_ptr.canonicalizeBasePtr(.One, opt_ty, pt); | |
| 3705 | return Value.fromInterned(try pt.intern(.{ .ptr = .{ | |
| 3631 | 3706 | .ty = result_ty.toIntern(), |
| 3632 | 3707 | .base_addr = .{ .opt_payload = base_ptr.toIntern() }, |
| 3633 | 3708 | .byte_offset = 0, |
| ... | ... | @@ -3637,14 +3712,15 @@ pub fn ptrOptPayload(parent_ptr: Value, zcu: *Zcu) !Value { |
| 3637 | 3712 | /// `parent_ptr` must be a single-pointer to some error union. |
| 3638 | 3713 | /// Returns a pointer to the payload of the error union. |
| 3639 | 3714 | /// May perform type resolution. |
| 3640 | pub fn ptrEuPayload(parent_ptr: Value, zcu: *Zcu) !Value { | |
| 3715 | pub fn ptrEuPayload(parent_ptr: Value, pt: Zcu.PerThread) !Value { | |
| 3716 | const zcu = pt.zcu; | |
| 3641 | 3717 | const parent_ptr_ty = parent_ptr.typeOf(zcu); |
| 3642 | 3718 | const eu_ty = parent_ptr_ty.childType(zcu); |
| 3643 | 3719 | |
| 3644 | 3720 | assert(parent_ptr_ty.ptrSize(zcu) == .One); |
| 3645 | 3721 | assert(eu_ty.zigTypeTag(zcu) == .ErrorUnion); |
| 3646 | 3722 | |
| 3647 | const result_ty = try zcu.ptrTypeSema(info: { | |
| 3723 | const result_ty = try pt.ptrTypeSema(info: { | |
| 3648 | 3724 | var new = parent_ptr_ty.ptrInfo(zcu); |
| 3649 | 3725 | // We can correctly preserve alignment `.none`, since an error union has a |
| 3650 | 3726 | // natural alignment greater than or equal to that of its payload type. |
| ... | ... | @@ -3652,10 +3728,10 @@ pub fn ptrEuPayload(parent_ptr: Value, zcu: *Zcu) !Value { |
| 3652 | 3728 | break :info new; |
| 3653 | 3729 | }); |
| 3654 | 3730 | |
| 3655 | if (parent_ptr.isUndef(zcu)) return zcu.undefValue(result_ty); | |
| 3731 | if (parent_ptr.isUndef(zcu)) return pt.undefValue(result_ty); | |
| 3656 | 3732 | |
| 3657 | const base_ptr = try parent_ptr.canonicalizeBasePtr(.One, eu_ty, zcu); | |
| 3658 | return Value.fromInterned(try zcu.intern(.{ .ptr = .{ | |
| 3733 | const base_ptr = try parent_ptr.canonicalizeBasePtr(.One, eu_ty, pt); | |
| 3734 | return Value.fromInterned(try pt.intern(.{ .ptr = .{ | |
| 3659 | 3735 | .ty = result_ty.toIntern(), |
| 3660 | 3736 | .base_addr = .{ .eu_payload = base_ptr.toIntern() }, |
| 3661 | 3737 | .byte_offset = 0, |
| ... | ... | @@ -3666,7 +3742,8 @@ pub fn ptrEuPayload(parent_ptr: Value, zcu: *Zcu) !Value { |
| 3666 | 3742 | /// Returns a pointer to the aggregate field at the specified index. |
| 3667 | 3743 | /// For slices, uses `slice_ptr_index` and `slice_len_index`. |
| 3668 | 3744 | /// May perform type resolution. |
| 3669 | pub fn ptrField(parent_ptr: Value, field_idx: u32, zcu: *Zcu) !Value { | |
| 3745 | pub fn ptrField(parent_ptr: Value, field_idx: u32, pt: Zcu.PerThread) !Value { | |
| 3746 | const zcu = pt.zcu; | |
| 3670 | 3747 | const parent_ptr_ty = parent_ptr.typeOf(zcu); |
| 3671 | 3748 | const aggregate_ty = parent_ptr_ty.childType(zcu); |
| 3672 | 3749 | |
| ... | ... | @@ -3679,39 +3756,39 @@ pub fn ptrField(parent_ptr: Value, field_idx: u32, zcu: *Zcu) !Value { |
| 3679 | 3756 | .Struct => field: { |
| 3680 | 3757 | const field_ty = aggregate_ty.structFieldType(field_idx, zcu); |
| 3681 | 3758 | switch (aggregate_ty.containerLayout(zcu)) { |
| 3682 | .auto => break :field .{ field_ty, try aggregate_ty.structFieldAlignAdvanced(@intCast(field_idx), zcu, .sema) }, | |
| 3759 | .auto => break :field .{ field_ty, try aggregate_ty.structFieldAlignAdvanced(@intCast(field_idx), pt, .sema) }, | |
| 3683 | 3760 | .@"extern" => { |
| 3684 | 3761 | // Well-defined layout, so just offset the pointer appropriately. |
| 3685 | const byte_off = aggregate_ty.structFieldOffset(field_idx, zcu); | |
| 3762 | const byte_off = aggregate_ty.structFieldOffset(field_idx, pt); | |
| 3686 | 3763 | const field_align = a: { |
| 3687 | 3764 | const parent_align = if (parent_ptr_info.flags.alignment == .none) pa: { |
| 3688 | break :pa (try aggregate_ty.abiAlignmentAdvanced(zcu, .sema)).scalar; | |
| 3765 | break :pa (try aggregate_ty.abiAlignmentAdvanced(pt, .sema)).scalar; | |
| 3689 | 3766 | } else parent_ptr_info.flags.alignment; |
| 3690 | 3767 | break :a InternPool.Alignment.fromLog2Units(@min(parent_align.toLog2Units(), @ctz(byte_off))); |
| 3691 | 3768 | }; |
| 3692 | const result_ty = try zcu.ptrTypeSema(info: { | |
| 3769 | const result_ty = try pt.ptrTypeSema(info: { | |
| 3693 | 3770 | var new = parent_ptr_info; |
| 3694 | 3771 | new.child = field_ty.toIntern(); |
| 3695 | 3772 | new.flags.alignment = field_align; |
| 3696 | 3773 | break :info new; |
| 3697 | 3774 | }); |
| 3698 | return parent_ptr.getOffsetPtr(byte_off, result_ty, zcu); | |
| 3775 | return parent_ptr.getOffsetPtr(byte_off, result_ty, pt); | |
| 3699 | 3776 | }, |
| 3700 | .@"packed" => switch (aggregate_ty.packedStructFieldPtrInfo(parent_ptr_ty, field_idx, zcu)) { | |
| 3777 | .@"packed" => switch (aggregate_ty.packedStructFieldPtrInfo(parent_ptr_ty, field_idx, pt)) { | |
| 3701 | 3778 | .bit_ptr => |packed_offset| { |
| 3702 | const result_ty = try zcu.ptrType(info: { | |
| 3779 | const result_ty = try pt.ptrType(info: { | |
| 3703 | 3780 | var new = parent_ptr_info; |
| 3704 | 3781 | new.packed_offset = packed_offset; |
| 3705 | 3782 | new.child = field_ty.toIntern(); |
| 3706 | 3783 | if (new.flags.alignment == .none) { |
| 3707 | new.flags.alignment = (try aggregate_ty.abiAlignmentAdvanced(zcu, .sema)).scalar; | |
| 3784 | new.flags.alignment = (try aggregate_ty.abiAlignmentAdvanced(pt, .sema)).scalar; | |
| 3708 | 3785 | } |
| 3709 | 3786 | break :info new; |
| 3710 | 3787 | }); |
| 3711 | return zcu.getCoerced(parent_ptr, result_ty); | |
| 3788 | return pt.getCoerced(parent_ptr, result_ty); | |
| 3712 | 3789 | }, |
| 3713 | 3790 | .byte_ptr => |ptr_info| { |
| 3714 | const result_ty = try zcu.ptrTypeSema(info: { | |
| 3791 | const result_ty = try pt.ptrTypeSema(info: { | |
| 3715 | 3792 | var new = parent_ptr_info; |
| 3716 | 3793 | new.child = field_ty.toIntern(); |
| 3717 | 3794 | new.packed_offset = .{ |
| ... | ... | @@ -3721,7 +3798,7 @@ pub fn ptrField(parent_ptr: Value, field_idx: u32, zcu: *Zcu) !Value { |
| 3721 | 3798 | new.flags.alignment = ptr_info.alignment; |
| 3722 | 3799 | break :info new; |
| 3723 | 3800 | }); |
| 3724 | return parent_ptr.getOffsetPtr(ptr_info.offset, result_ty, zcu); | |
| 3801 | return parent_ptr.getOffsetPtr(ptr_info.offset, result_ty, pt); | |
| 3725 | 3802 | }, |
| 3726 | 3803 | }, |
| 3727 | 3804 | } |
| ... | ... | @@ -3730,46 +3807,46 @@ pub fn ptrField(parent_ptr: Value, field_idx: u32, zcu: *Zcu) !Value { |
| 3730 | 3807 | const union_obj = zcu.typeToUnion(aggregate_ty).?; |
| 3731 | 3808 | const field_ty = Type.fromInterned(union_obj.field_types.get(&zcu.intern_pool)[field_idx]); |
| 3732 | 3809 | switch (aggregate_ty.containerLayout(zcu)) { |
| 3733 | .auto => break :field .{ field_ty, try aggregate_ty.structFieldAlignAdvanced(@intCast(field_idx), zcu, .sema) }, | |
| 3810 | .auto => break :field .{ field_ty, try aggregate_ty.structFieldAlignAdvanced(@intCast(field_idx), pt, .sema) }, | |
| 3734 | 3811 | .@"extern" => { |
| 3735 | 3812 | // Point to the same address. |
| 3736 | const result_ty = try zcu.ptrTypeSema(info: { | |
| 3813 | const result_ty = try pt.ptrTypeSema(info: { | |
| 3737 | 3814 | var new = parent_ptr_info; |
| 3738 | 3815 | new.child = field_ty.toIntern(); |
| 3739 | 3816 | break :info new; |
| 3740 | 3817 | }); |
| 3741 | return zcu.getCoerced(parent_ptr, result_ty); | |
| 3818 | return pt.getCoerced(parent_ptr, result_ty); | |
| 3742 | 3819 | }, |
| 3743 | 3820 | .@"packed" => { |
| 3744 | 3821 | // If the field has an ABI size matching its bit size, then we can continue to use a |
| 3745 | 3822 | // non-bit pointer if the parent pointer is also a non-bit pointer. |
| 3746 | if (parent_ptr_info.packed_offset.host_size == 0 and (try field_ty.abiSizeAdvanced(zcu, .sema)).scalar * 8 == try field_ty.bitSizeAdvanced(zcu, .sema)) { | |
| 3823 | if (parent_ptr_info.packed_offset.host_size == 0 and (try field_ty.abiSizeAdvanced(pt, .sema)).scalar * 8 == try field_ty.bitSizeAdvanced(pt, .sema)) { | |
| 3747 | 3824 | // We must offset the pointer on big-endian targets, since the bits of packed memory don't align nicely. |
| 3748 | 3825 | const byte_offset = switch (zcu.getTarget().cpu.arch.endian()) { |
| 3749 | 3826 | .little => 0, |
| 3750 | .big => (try aggregate_ty.abiSizeAdvanced(zcu, .sema)).scalar - (try field_ty.abiSizeAdvanced(zcu, .sema)).scalar, | |
| 3827 | .big => (try aggregate_ty.abiSizeAdvanced(pt, .sema)).scalar - (try field_ty.abiSizeAdvanced(pt, .sema)).scalar, | |
| 3751 | 3828 | }; |
| 3752 | const result_ty = try zcu.ptrTypeSema(info: { | |
| 3829 | const result_ty = try pt.ptrTypeSema(info: { | |
| 3753 | 3830 | var new = parent_ptr_info; |
| 3754 | 3831 | new.child = field_ty.toIntern(); |
| 3755 | 3832 | new.flags.alignment = InternPool.Alignment.fromLog2Units( |
| 3756 | @ctz(byte_offset | (try parent_ptr_ty.ptrAlignmentAdvanced(zcu, .sema)).toByteUnits().?), | |
| 3833 | @ctz(byte_offset | (try parent_ptr_ty.ptrAlignmentAdvanced(pt, .sema)).toByteUnits().?), | |
| 3757 | 3834 | ); |
| 3758 | 3835 | break :info new; |
| 3759 | 3836 | }); |
| 3760 | return parent_ptr.getOffsetPtr(byte_offset, result_ty, zcu); | |
| 3837 | return parent_ptr.getOffsetPtr(byte_offset, result_ty, pt); | |
| 3761 | 3838 | } else { |
| 3762 | 3839 | // The result must be a bit-pointer if it is not already. |
| 3763 | const result_ty = try zcu.ptrTypeSema(info: { | |
| 3840 | const result_ty = try pt.ptrTypeSema(info: { | |
| 3764 | 3841 | var new = parent_ptr_info; |
| 3765 | 3842 | new.child = field_ty.toIntern(); |
| 3766 | 3843 | if (new.packed_offset.host_size == 0) { |
| 3767 | new.packed_offset.host_size = @intCast(((try aggregate_ty.bitSizeAdvanced(zcu, .sema)) + 7) / 8); | |
| 3844 | new.packed_offset.host_size = @intCast(((try aggregate_ty.bitSizeAdvanced(pt, .sema)) + 7) / 8); | |
| 3768 | 3845 | assert(new.packed_offset.bit_offset == 0); |
| 3769 | 3846 | } |
| 3770 | 3847 | break :info new; |
| 3771 | 3848 | }); |
| 3772 | return zcu.getCoerced(parent_ptr, result_ty); | |
| 3849 | return pt.getCoerced(parent_ptr, result_ty); | |
| 3773 | 3850 | } |
| 3774 | 3851 | }, |
| 3775 | 3852 | } |
| ... | ... | @@ -3777,8 +3854,8 @@ pub fn ptrField(parent_ptr: Value, field_idx: u32, zcu: *Zcu) !Value { |
| 3777 | 3854 | .Pointer => field_ty: { |
| 3778 | 3855 | assert(aggregate_ty.isSlice(zcu)); |
| 3779 | 3856 | break :field_ty switch (field_idx) { |
| 3780 | Value.slice_ptr_index => .{ aggregate_ty.slicePtrFieldType(zcu), Type.usize.abiAlignment(zcu) }, | |
| 3781 | Value.slice_len_index => .{ Type.usize, Type.usize.abiAlignment(zcu) }, | |
| 3857 | Value.slice_ptr_index => .{ aggregate_ty.slicePtrFieldType(zcu), Type.usize.abiAlignment(pt) }, | |
| 3858 | Value.slice_len_index => .{ Type.usize, Type.usize.abiAlignment(pt) }, | |
| 3782 | 3859 | else => unreachable, |
| 3783 | 3860 | }; |
| 3784 | 3861 | }, |
| ... | ... | @@ -3786,24 +3863,24 @@ pub fn ptrField(parent_ptr: Value, field_idx: u32, zcu: *Zcu) !Value { |
| 3786 | 3863 | }; |
| 3787 | 3864 | |
| 3788 | 3865 | const new_align: InternPool.Alignment = if (parent_ptr_info.flags.alignment != .none) a: { |
| 3789 | const ty_align = (try field_ty.abiAlignmentAdvanced(zcu, .sema)).scalar; | |
| 3866 | const ty_align = (try field_ty.abiAlignmentAdvanced(pt, .sema)).scalar; | |
| 3790 | 3867 | const true_field_align = if (field_align == .none) ty_align else field_align; |
| 3791 | 3868 | const new_align = true_field_align.min(parent_ptr_info.flags.alignment); |
| 3792 | 3869 | if (new_align == ty_align) break :a .none; |
| 3793 | 3870 | break :a new_align; |
| 3794 | 3871 | } else field_align; |
| 3795 | 3872 | |
| 3796 | const result_ty = try zcu.ptrTypeSema(info: { | |
| 3873 | const result_ty = try pt.ptrTypeSema(info: { | |
| 3797 | 3874 | var new = parent_ptr_info; |
| 3798 | 3875 | new.child = field_ty.toIntern(); |
| 3799 | 3876 | new.flags.alignment = new_align; |
| 3800 | 3877 | break :info new; |
| 3801 | 3878 | }); |
| 3802 | 3879 | |
| 3803 | if (parent_ptr.isUndef(zcu)) return zcu.undefValue(result_ty); | |
| 3880 | if (parent_ptr.isUndef(zcu)) return pt.undefValue(result_ty); | |
| 3804 | 3881 | |
| 3805 | const base_ptr = try parent_ptr.canonicalizeBasePtr(.One, aggregate_ty, zcu); | |
| 3806 | return Value.fromInterned(try zcu.intern(.{ .ptr = .{ | |
| 3882 | const base_ptr = try parent_ptr.canonicalizeBasePtr(.One, aggregate_ty, pt); | |
| 3883 | return Value.fromInterned(try pt.intern(.{ .ptr = .{ | |
| 3807 | 3884 | .ty = result_ty.toIntern(), |
| 3808 | 3885 | .base_addr = .{ .field = .{ |
| 3809 | 3886 | .base = base_ptr.toIntern(), |
| ... | ... | @@ -3816,7 +3893,8 @@ pub fn ptrField(parent_ptr: Value, field_idx: u32, zcu: *Zcu) !Value { |
| 3816 | 3893 | /// `orig_parent_ptr` must be either a single-pointer to an array or vector, or a many-pointer or C-pointer or slice. |
| 3817 | 3894 | /// Returns a pointer to the element at the specified index. |
| 3818 | 3895 | /// May perform type resolution. |
| 3819 | pub fn ptrElem(orig_parent_ptr: Value, field_idx: u64, zcu: *Zcu) !Value { | |
| 3896 | pub fn ptrElem(orig_parent_ptr: Value, field_idx: u64, pt: Zcu.PerThread) !Value { | |
| 3897 | const zcu = pt.zcu; | |
| 3820 | 3898 | const parent_ptr = switch (orig_parent_ptr.typeOf(zcu).ptrSize(zcu)) { |
| 3821 | 3899 | .One, .Many, .C => orig_parent_ptr, |
| 3822 | 3900 | .Slice => orig_parent_ptr.slicePtr(zcu), |
| ... | ... | @@ -3824,14 +3902,14 @@ pub fn ptrElem(orig_parent_ptr: Value, field_idx: u64, zcu: *Zcu) !Value { |
| 3824 | 3902 | |
| 3825 | 3903 | const parent_ptr_ty = parent_ptr.typeOf(zcu); |
| 3826 | 3904 | const elem_ty = parent_ptr_ty.childType(zcu); |
| 3827 | const result_ty = try parent_ptr_ty.elemPtrType(@intCast(field_idx), zcu); | |
| 3905 | const result_ty = try parent_ptr_ty.elemPtrType(@intCast(field_idx), pt); | |
| 3828 | 3906 | |
| 3829 | if (parent_ptr.isUndef(zcu)) return zcu.undefValue(result_ty); | |
| 3907 | if (parent_ptr.isUndef(zcu)) return pt.undefValue(result_ty); | |
| 3830 | 3908 | |
| 3831 | 3909 | if (result_ty.ptrInfo(zcu).packed_offset.host_size != 0) { |
| 3832 | 3910 | // Since we have a bit-pointer, the pointer address should be unchanged. |
| 3833 | 3911 | assert(elem_ty.zigTypeTag(zcu) == .Vector); |
| 3834 | return zcu.getCoerced(parent_ptr, result_ty); | |
| 3912 | return pt.getCoerced(parent_ptr, result_ty); | |
| 3835 | 3913 | } |
| 3836 | 3914 | |
| 3837 | 3915 | const PtrStrat = union(enum) { |
| ... | ... | @@ -3841,31 +3919,31 @@ pub fn ptrElem(orig_parent_ptr: Value, field_idx: u64, zcu: *Zcu) !Value { |
| 3841 | 3919 | |
| 3842 | 3920 | const strat: PtrStrat = switch (parent_ptr_ty.ptrSize(zcu)) { |
| 3843 | 3921 | .One => switch (elem_ty.zigTypeTag(zcu)) { |
| 3844 | .Vector => .{ .offset = field_idx * @divExact(try elem_ty.childType(zcu).bitSizeAdvanced(zcu, .sema), 8) }, | |
| 3922 | .Vector => .{ .offset = field_idx * @divExact(try elem_ty.childType(zcu).bitSizeAdvanced(pt, .sema), 8) }, | |
| 3845 | 3923 | .Array => strat: { |
| 3846 | 3924 | const arr_elem_ty = elem_ty.childType(zcu); |
| 3847 | if (try arr_elem_ty.comptimeOnlyAdvanced(zcu, .sema)) { | |
| 3925 | if (try arr_elem_ty.comptimeOnlyAdvanced(pt, .sema)) { | |
| 3848 | 3926 | break :strat .{ .elem_ptr = arr_elem_ty }; |
| 3849 | 3927 | } |
| 3850 | break :strat .{ .offset = field_idx * (try arr_elem_ty.abiSizeAdvanced(zcu, .sema)).scalar }; | |
| 3928 | break :strat .{ .offset = field_idx * (try arr_elem_ty.abiSizeAdvanced(pt, .sema)).scalar }; | |
| 3851 | 3929 | }, |
| 3852 | 3930 | else => unreachable, |
| 3853 | 3931 | }, |
| 3854 | 3932 | |
| 3855 | .Many, .C => if (try elem_ty.comptimeOnlyAdvanced(zcu, .sema)) | |
| 3933 | .Many, .C => if (try elem_ty.comptimeOnlyAdvanced(pt, .sema)) | |
| 3856 | 3934 | .{ .elem_ptr = elem_ty } |
| 3857 | 3935 | else |
| 3858 | .{ .offset = field_idx * (try elem_ty.abiSizeAdvanced(zcu, .sema)).scalar }, | |
| 3936 | .{ .offset = field_idx * (try elem_ty.abiSizeAdvanced(pt, .sema)).scalar }, | |
| 3859 | 3937 | |
| 3860 | 3938 | .Slice => unreachable, |
| 3861 | 3939 | }; |
| 3862 | 3940 | |
| 3863 | 3941 | switch (strat) { |
| 3864 | 3942 | .offset => |byte_offset| { |
| 3865 | return parent_ptr.getOffsetPtr(byte_offset, result_ty, zcu); | |
| 3943 | return parent_ptr.getOffsetPtr(byte_offset, result_ty, pt); | |
| 3866 | 3944 | }, |
| 3867 | 3945 | .elem_ptr => |manyptr_elem_ty| if (field_idx == 0) { |
| 3868 | return zcu.getCoerced(parent_ptr, result_ty); | |
| 3946 | return pt.getCoerced(parent_ptr, result_ty); | |
| 3869 | 3947 | } else { |
| 3870 | 3948 | const arr_base_ty, const arr_base_len = manyptr_elem_ty.arrayBase(zcu); |
| 3871 | 3949 | const base_idx = arr_base_len * field_idx; |
| ... | ... | @@ -3875,7 +3953,7 @@ pub fn ptrElem(orig_parent_ptr: Value, field_idx: u64, zcu: *Zcu) !Value { |
| 3875 | 3953 | if (Value.fromInterned(arr_elem.base).typeOf(zcu).childType(zcu).toIntern() == arr_base_ty.toIntern()) { |
| 3876 | 3954 | // We already have a pointer to an element of an array of this type. |
| 3877 | 3955 | // Just modify the index. |
| 3878 | return Value.fromInterned(try zcu.intern(.{ .ptr = ptr: { | |
| 3956 | return Value.fromInterned(try pt.intern(.{ .ptr = ptr: { | |
| 3879 | 3957 | var new = parent_info; |
| 3880 | 3958 | new.base_addr.arr_elem.index += base_idx; |
| 3881 | 3959 | new.ty = result_ty.toIntern(); |
| ... | ... | @@ -3885,8 +3963,8 @@ pub fn ptrElem(orig_parent_ptr: Value, field_idx: u64, zcu: *Zcu) !Value { |
| 3885 | 3963 | }, |
| 3886 | 3964 | else => {}, |
| 3887 | 3965 | } |
| 3888 | const base_ptr = try parent_ptr.canonicalizeBasePtr(.Many, arr_base_ty, zcu); | |
| 3889 | return Value.fromInterned(try zcu.intern(.{ .ptr = .{ | |
| 3966 | const base_ptr = try parent_ptr.canonicalizeBasePtr(.Many, arr_base_ty, pt); | |
| 3967 | return Value.fromInterned(try pt.intern(.{ .ptr = .{ | |
| 3890 | 3968 | .ty = result_ty.toIntern(), |
| 3891 | 3969 | .base_addr = .{ .arr_elem = .{ |
| 3892 | 3970 | .base = base_ptr.toIntern(), |
| ... | ... | @@ -3898,9 +3976,9 @@ pub fn ptrElem(orig_parent_ptr: Value, field_idx: u64, zcu: *Zcu) !Value { |
| 3898 | 3976 | } |
| 3899 | 3977 | } |
| 3900 | 3978 | |
| 3901 | fn canonicalizeBasePtr(base_ptr: Value, want_size: std.builtin.Type.Pointer.Size, want_child: Type, zcu: *Zcu) !Value { | |
| 3902 | const ptr_ty = base_ptr.typeOf(zcu); | |
| 3903 | const ptr_info = ptr_ty.ptrInfo(zcu); | |
| 3979 | fn canonicalizeBasePtr(base_ptr: Value, want_size: std.builtin.Type.Pointer.Size, want_child: Type, pt: Zcu.PerThread) !Value { | |
| 3980 | const ptr_ty = base_ptr.typeOf(pt.zcu); | |
| 3981 | const ptr_info = ptr_ty.ptrInfo(pt.zcu); | |
| 3904 | 3982 | |
| 3905 | 3983 | if (ptr_info.flags.size == want_size and |
| 3906 | 3984 | ptr_info.child == want_child.toIntern() and |
| ... | ... | @@ -3914,7 +3992,7 @@ fn canonicalizeBasePtr(base_ptr: Value, want_size: std.builtin.Type.Pointer.Size |
| 3914 | 3992 | return base_ptr; |
| 3915 | 3993 | } |
| 3916 | 3994 | |
| 3917 | const new_ty = try zcu.ptrType(.{ | |
| 3995 | const new_ty = try pt.ptrType(.{ | |
| 3918 | 3996 | .child = want_child.toIntern(), |
| 3919 | 3997 | .sentinel = .none, |
| 3920 | 3998 | .flags = .{ |
| ... | ... | @@ -3926,15 +4004,15 @@ fn canonicalizeBasePtr(base_ptr: Value, want_size: std.builtin.Type.Pointer.Size |
| 3926 | 4004 | .address_space = ptr_info.flags.address_space, |
| 3927 | 4005 | }, |
| 3928 | 4006 | }); |
| 3929 | return zcu.getCoerced(base_ptr, new_ty); | |
| 4007 | return pt.getCoerced(base_ptr, new_ty); | |
| 3930 | 4008 | } |
| 3931 | 4009 | |
| 3932 | pub fn getOffsetPtr(ptr_val: Value, byte_off: u64, new_ty: Type, zcu: *Zcu) !Value { | |
| 3933 | if (ptr_val.isUndef(zcu)) return ptr_val; | |
| 3934 | var ptr = zcu.intern_pool.indexToKey(ptr_val.toIntern()).ptr; | |
| 4010 | pub fn getOffsetPtr(ptr_val: Value, byte_off: u64, new_ty: Type, pt: Zcu.PerThread) !Value { | |
| 4011 | if (ptr_val.isUndef(pt.zcu)) return ptr_val; | |
| 4012 | var ptr = pt.zcu.intern_pool.indexToKey(ptr_val.toIntern()).ptr; | |
| 3935 | 4013 | ptr.ty = new_ty.toIntern(); |
| 3936 | 4014 | ptr.byte_offset += byte_off; |
| 3937 | return Value.fromInterned(try zcu.intern(.{ .ptr = ptr })); | |
| 4015 | return Value.fromInterned(try pt.intern(.{ .ptr = ptr })); | |
| 3938 | 4016 | } |
| 3939 | 4017 | |
| 3940 | 4018 | pub const PointerDeriveStep = union(enum) { |
| ... | ... | @@ -3977,21 +4055,21 @@ pub const PointerDeriveStep = union(enum) { |
| 3977 | 4055 | new_ptr_ty: Type, |
| 3978 | 4056 | }, |
| 3979 | 4057 | |
| 3980 | pub fn ptrType(step: PointerDeriveStep, zcu: *Zcu) !Type { | |
| 4058 | pub fn ptrType(step: PointerDeriveStep, pt: Zcu.PerThread) !Type { | |
| 3981 | 4059 | return switch (step) { |
| 3982 | 4060 | .int => |int| int.ptr_ty, |
| 3983 | .decl_ptr => |decl| try zcu.declPtr(decl).declPtrType(zcu), | |
| 4061 | .decl_ptr => |decl| try pt.zcu.declPtr(decl).declPtrType(pt), | |
| 3984 | 4062 | .anon_decl_ptr => |ad| Type.fromInterned(ad.orig_ty), |
| 3985 | 4063 | .comptime_alloc_ptr => |info| info.ptr_ty, |
| 3986 | .comptime_field_ptr => |val| try zcu.singleConstPtrType(val.typeOf(zcu)), | |
| 4064 | .comptime_field_ptr => |val| try pt.singleConstPtrType(val.typeOf(pt.zcu)), | |
| 3987 | 4065 | .offset_and_cast => |oac| oac.new_ptr_ty, |
| 3988 | 4066 | inline .eu_payload_ptr, .opt_payload_ptr, .field_ptr, .elem_ptr => |x| x.result_ptr_ty, |
| 3989 | 4067 | }; |
| 3990 | 4068 | } |
| 3991 | 4069 | }; |
| 3992 | 4070 | |
| 3993 | pub fn pointerDerivation(ptr_val: Value, arena: Allocator, zcu: *Zcu) Allocator.Error!PointerDeriveStep { | |
| 3994 | return ptr_val.pointerDerivationAdvanced(arena, zcu, null) catch |err| switch (err) { | |
| 4071 | pub fn pointerDerivation(ptr_val: Value, arena: Allocator, pt: Zcu.PerThread) Allocator.Error!PointerDeriveStep { | |
| 4072 | return ptr_val.pointerDerivationAdvanced(arena, pt, null) catch |err| switch (err) { | |
| 3995 | 4073 | error.OutOfMemory => |e| return e, |
| 3996 | 4074 | error.AnalysisFail => unreachable, |
| 3997 | 4075 | }; |
| ... | ... | @@ -4001,7 +4079,8 @@ pub fn pointerDerivation(ptr_val: Value, arena: Allocator, zcu: *Zcu) Allocator. |
| 4001 | 4079 | /// only field and element pointers with no casts. This can be used by codegen backends |
| 4002 | 4080 | /// which prefer field/elem accesses when lowering constant pointer values. |
| 4003 | 4081 | /// It is also used by the Value printing logic for pointers. |
| 4004 | pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, opt_sema: ?*Sema) !PointerDeriveStep { | |
| 4082 | pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, pt: Zcu.PerThread, opt_sema: ?*Sema) !PointerDeriveStep { | |
| 4083 | const zcu = pt.zcu; | |
| 4005 | 4084 | const ptr = zcu.intern_pool.indexToKey(ptr_val.toIntern()).ptr; |
| 4006 | 4085 | const base_derive: PointerDeriveStep = switch (ptr.base_addr) { |
| 4007 | 4086 | .int => return .{ .int = .{ |
| ... | ... | @@ -4012,7 +4091,7 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4012 | 4091 | .anon_decl => |ad| base: { |
| 4013 | 4092 | // A slight tweak: `orig_ty` here is sometimes not `const`, but it ought to be. |
| 4014 | 4093 | // TODO: fix this in the sites interning anon decls! |
| 4015 | const const_ty = try zcu.ptrType(info: { | |
| 4094 | const const_ty = try pt.ptrType(info: { | |
| 4016 | 4095 | var info = Type.fromInterned(ad.orig_ty).ptrInfo(zcu); |
| 4017 | 4096 | info.flags.is_const = true; |
| 4018 | 4097 | break :info info; |
| ... | ... | @@ -4024,11 +4103,11 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4024 | 4103 | }, |
| 4025 | 4104 | .comptime_alloc => |idx| base: { |
| 4026 | 4105 | const alloc = opt_sema.?.getComptimeAlloc(idx); |
| 4027 | const val = try alloc.val.intern(zcu, opt_sema.?.arena); | |
| 4106 | const val = try alloc.val.intern(pt, opt_sema.?.arena); | |
| 4028 | 4107 | const ty = val.typeOf(zcu); |
| 4029 | 4108 | break :base .{ .comptime_alloc_ptr = .{ |
| 4030 | 4109 | .val = val, |
| 4031 | .ptr_ty = try zcu.ptrType(.{ | |
| 4110 | .ptr_ty = try pt.ptrType(.{ | |
| 4032 | 4111 | .child = ty.toIntern(), |
| 4033 | 4112 | .flags = .{ |
| 4034 | 4113 | .alignment = alloc.alignment, |
| ... | ... | @@ -4041,20 +4120,20 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4041 | 4120 | const base_ptr = Value.fromInterned(eu_ptr); |
| 4042 | 4121 | const base_ptr_ty = base_ptr.typeOf(zcu); |
| 4043 | 4122 | const parent_step = try arena.create(PointerDeriveStep); |
| 4044 | parent_step.* = try pointerDerivationAdvanced(Value.fromInterned(eu_ptr), arena, zcu, opt_sema); | |
| 4123 | parent_step.* = try pointerDerivationAdvanced(Value.fromInterned(eu_ptr), arena, pt, opt_sema); | |
| 4045 | 4124 | break :base .{ .eu_payload_ptr = .{ |
| 4046 | 4125 | .parent = parent_step, |
| 4047 | .result_ptr_ty = try zcu.adjustPtrTypeChild(base_ptr_ty, base_ptr_ty.childType(zcu).errorUnionPayload(zcu)), | |
| 4126 | .result_ptr_ty = try pt.adjustPtrTypeChild(base_ptr_ty, base_ptr_ty.childType(zcu).errorUnionPayload(zcu)), | |
| 4048 | 4127 | } }; |
| 4049 | 4128 | }, |
| 4050 | 4129 | .opt_payload => |opt_ptr| base: { |
| 4051 | 4130 | const base_ptr = Value.fromInterned(opt_ptr); |
| 4052 | 4131 | const base_ptr_ty = base_ptr.typeOf(zcu); |
| 4053 | 4132 | const parent_step = try arena.create(PointerDeriveStep); |
| 4054 | parent_step.* = try pointerDerivationAdvanced(Value.fromInterned(opt_ptr), arena, zcu, opt_sema); | |
| 4133 | parent_step.* = try pointerDerivationAdvanced(Value.fromInterned(opt_ptr), arena, pt, opt_sema); | |
| 4055 | 4134 | break :base .{ .opt_payload_ptr = .{ |
| 4056 | 4135 | .parent = parent_step, |
| 4057 | .result_ptr_ty = try zcu.adjustPtrTypeChild(base_ptr_ty, base_ptr_ty.childType(zcu).optionalChild(zcu)), | |
| 4136 | .result_ptr_ty = try pt.adjustPtrTypeChild(base_ptr_ty, base_ptr_ty.childType(zcu).optionalChild(zcu)), | |
| 4058 | 4137 | } }; |
| 4059 | 4138 | }, |
| 4060 | 4139 | .field => |field| base: { |
| ... | ... | @@ -4062,22 +4141,22 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4062 | 4141 | const base_ptr_ty = base_ptr.typeOf(zcu); |
| 4063 | 4142 | const agg_ty = base_ptr_ty.childType(zcu); |
| 4064 | 4143 | const field_ty, const field_align = switch (agg_ty.zigTypeTag(zcu)) { |
| 4065 | .Struct => .{ agg_ty.structFieldType(@intCast(field.index), zcu), try agg_ty.structFieldAlignAdvanced(@intCast(field.index), zcu, .sema) }, | |
| 4066 | .Union => .{ agg_ty.unionFieldTypeByIndex(@intCast(field.index), zcu), try agg_ty.structFieldAlignAdvanced(@intCast(field.index), zcu, .sema) }, | |
| 4144 | .Struct => .{ agg_ty.structFieldType(@intCast(field.index), zcu), try agg_ty.structFieldAlignAdvanced(@intCast(field.index), pt, .sema) }, | |
| 4145 | .Union => .{ agg_ty.unionFieldTypeByIndex(@intCast(field.index), zcu), try agg_ty.structFieldAlignAdvanced(@intCast(field.index), pt, .sema) }, | |
| 4067 | 4146 | .Pointer => .{ switch (field.index) { |
| 4068 | 4147 | Value.slice_ptr_index => agg_ty.slicePtrFieldType(zcu), |
| 4069 | 4148 | Value.slice_len_index => Type.usize, |
| 4070 | 4149 | else => unreachable, |
| 4071 | }, Type.usize.abiAlignment(zcu) }, | |
| 4150 | }, Type.usize.abiAlignment(pt) }, | |
| 4072 | 4151 | else => unreachable, |
| 4073 | 4152 | }; |
| 4074 | const base_align = base_ptr_ty.ptrAlignment(zcu); | |
| 4153 | const base_align = base_ptr_ty.ptrAlignment(pt); | |
| 4075 | 4154 | const result_align = field_align.minStrict(base_align); |
| 4076 | const result_ty = try zcu.ptrType(.{ | |
| 4155 | const result_ty = try pt.ptrType(.{ | |
| 4077 | 4156 | .child = field_ty.toIntern(), |
| 4078 | 4157 | .flags = flags: { |
| 4079 | 4158 | var flags = base_ptr_ty.ptrInfo(zcu).flags; |
| 4080 | if (result_align == field_ty.abiAlignment(zcu)) { | |
| 4159 | if (result_align == field_ty.abiAlignment(pt)) { | |
| 4081 | 4160 | flags.alignment = .none; |
| 4082 | 4161 | } else { |
| 4083 | 4162 | flags.alignment = result_align; |
| ... | ... | @@ -4086,7 +4165,7 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4086 | 4165 | }, |
| 4087 | 4166 | }); |
| 4088 | 4167 | const parent_step = try arena.create(PointerDeriveStep); |
| 4089 | parent_step.* = try pointerDerivationAdvanced(base_ptr, arena, zcu, opt_sema); | |
| 4168 | parent_step.* = try pointerDerivationAdvanced(base_ptr, arena, pt, opt_sema); | |
| 4090 | 4169 | break :base .{ .field_ptr = .{ |
| 4091 | 4170 | .parent = parent_step, |
| 4092 | 4171 | .field_idx = @intCast(field.index), |
| ... | ... | @@ -4095,9 +4174,9 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4095 | 4174 | }, |
| 4096 | 4175 | .arr_elem => |arr_elem| base: { |
| 4097 | 4176 | const parent_step = try arena.create(PointerDeriveStep); |
| 4098 | parent_step.* = try pointerDerivationAdvanced(Value.fromInterned(arr_elem.base), arena, zcu, opt_sema); | |
| 4099 | const parent_ptr_info = (try parent_step.ptrType(zcu)).ptrInfo(zcu); | |
| 4100 | const result_ptr_ty = try zcu.ptrType(.{ | |
| 4177 | parent_step.* = try pointerDerivationAdvanced(Value.fromInterned(arr_elem.base), arena, pt, opt_sema); | |
| 4178 | const parent_ptr_info = (try parent_step.ptrType(pt)).ptrInfo(zcu); | |
| 4179 | const result_ptr_ty = try pt.ptrType(.{ | |
| 4101 | 4180 | .child = parent_ptr_info.child, |
| 4102 | 4181 | .flags = flags: { |
| 4103 | 4182 | var flags = parent_ptr_info.flags; |
| ... | ... | @@ -4113,12 +4192,12 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4113 | 4192 | }, |
| 4114 | 4193 | }; |
| 4115 | 4194 | |
| 4116 | if (ptr.byte_offset == 0 and ptr.ty == (try base_derive.ptrType(zcu)).toIntern()) { | |
| 4195 | if (ptr.byte_offset == 0 and ptr.ty == (try base_derive.ptrType(pt)).toIntern()) { | |
| 4117 | 4196 | return base_derive; |
| 4118 | 4197 | } |
| 4119 | 4198 | |
| 4120 | 4199 | const need_child = Type.fromInterned(ptr.ty).childType(zcu); |
| 4121 | if (need_child.comptimeOnly(zcu)) { | |
| 4200 | if (need_child.comptimeOnly(pt)) { | |
| 4122 | 4201 | // No refinement can happen - this pointer is presumably invalid. |
| 4123 | 4202 | // Just offset it. |
| 4124 | 4203 | const parent = try arena.create(PointerDeriveStep); |
| ... | ... | @@ -4129,7 +4208,7 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4129 | 4208 | .new_ptr_ty = Type.fromInterned(ptr.ty), |
| 4130 | 4209 | } }; |
| 4131 | 4210 | } |
| 4132 | const need_bytes = need_child.abiSize(zcu); | |
| 4211 | const need_bytes = need_child.abiSize(pt); | |
| 4133 | 4212 | |
| 4134 | 4213 | var cur_derive = base_derive; |
| 4135 | 4214 | var cur_offset = ptr.byte_offset; |
| ... | ... | @@ -4137,7 +4216,7 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4137 | 4216 | // Refine through fields and array elements as much as possible. |
| 4138 | 4217 | |
| 4139 | 4218 | if (need_bytes > 0) while (true) { |
| 4140 | const cur_ty = (try cur_derive.ptrType(zcu)).childType(zcu); | |
| 4219 | const cur_ty = (try cur_derive.ptrType(pt)).childType(zcu); | |
| 4141 | 4220 | if (cur_ty.toIntern() == need_child.toIntern() and cur_offset == 0) { |
| 4142 | 4221 | break; |
| 4143 | 4222 | } |
| ... | ... | @@ -4168,7 +4247,7 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4168 | 4247 | |
| 4169 | 4248 | .Array => { |
| 4170 | 4249 | const elem_ty = cur_ty.childType(zcu); |
| 4171 | const elem_size = elem_ty.abiSize(zcu); | |
| 4250 | const elem_size = elem_ty.abiSize(pt); | |
| 4172 | 4251 | const start_idx = cur_offset / elem_size; |
| 4173 | 4252 | const end_idx = (cur_offset + need_bytes + elem_size - 1) / elem_size; |
| 4174 | 4253 | if (end_idx == start_idx + 1) { |
| ... | ... | @@ -4177,7 +4256,7 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4177 | 4256 | cur_derive = .{ .elem_ptr = .{ |
| 4178 | 4257 | .parent = parent, |
| 4179 | 4258 | .elem_idx = start_idx, |
| 4180 | .result_ptr_ty = try zcu.adjustPtrTypeChild(try parent.ptrType(zcu), elem_ty), | |
| 4259 | .result_ptr_ty = try pt.adjustPtrTypeChild(try parent.ptrType(pt), elem_ty), | |
| 4181 | 4260 | } }; |
| 4182 | 4261 | cur_offset -= start_idx * elem_size; |
| 4183 | 4262 | } else { |
| ... | ... | @@ -4188,7 +4267,7 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4188 | 4267 | cur_derive = .{ .elem_ptr = .{ |
| 4189 | 4268 | .parent = parent, |
| 4190 | 4269 | .elem_idx = start_idx, |
| 4191 | .result_ptr_ty = try zcu.adjustPtrTypeChild(try parent.ptrType(zcu), elem_ty), | |
| 4270 | .result_ptr_ty = try pt.adjustPtrTypeChild(try parent.ptrType(pt), elem_ty), | |
| 4192 | 4271 | } }; |
| 4193 | 4272 | cur_offset -= start_idx * elem_size; |
| 4194 | 4273 | } |
| ... | ... | @@ -4199,19 +4278,19 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4199 | 4278 | .auto, .@"packed" => break, |
| 4200 | 4279 | .@"extern" => for (0..cur_ty.structFieldCount(zcu)) |field_idx| { |
| 4201 | 4280 | const field_ty = cur_ty.structFieldType(field_idx, zcu); |
| 4202 | const start_off = cur_ty.structFieldOffset(field_idx, zcu); | |
| 4203 | const end_off = start_off + field_ty.abiSize(zcu); | |
| 4281 | const start_off = cur_ty.structFieldOffset(field_idx, pt); | |
| 4282 | const end_off = start_off + field_ty.abiSize(pt); | |
| 4204 | 4283 | if (cur_offset >= start_off and cur_offset + need_bytes <= end_off) { |
| 4205 | const old_ptr_ty = try cur_derive.ptrType(zcu); | |
| 4206 | const parent_align = old_ptr_ty.ptrAlignment(zcu); | |
| 4284 | const old_ptr_ty = try cur_derive.ptrType(pt); | |
| 4285 | const parent_align = old_ptr_ty.ptrAlignment(pt); | |
| 4207 | 4286 | const field_align = InternPool.Alignment.fromLog2Units(@min(parent_align.toLog2Units(), @ctz(start_off))); |
| 4208 | 4287 | const parent = try arena.create(PointerDeriveStep); |
| 4209 | 4288 | parent.* = cur_derive; |
| 4210 | const new_ptr_ty = try zcu.ptrType(.{ | |
| 4289 | const new_ptr_ty = try pt.ptrType(.{ | |
| 4211 | 4290 | .child = field_ty.toIntern(), |
| 4212 | 4291 | .flags = flags: { |
| 4213 | 4292 | var flags = old_ptr_ty.ptrInfo(zcu).flags; |
| 4214 | if (field_align == field_ty.abiAlignment(zcu)) { | |
| 4293 | if (field_align == field_ty.abiAlignment(pt)) { | |
| 4215 | 4294 | flags.alignment = .none; |
| 4216 | 4295 | } else { |
| 4217 | 4296 | flags.alignment = field_align; |
| ... | ... | @@ -4232,7 +4311,7 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4232 | 4311 | } |
| 4233 | 4312 | }; |
| 4234 | 4313 | |
| 4235 | if (cur_offset == 0 and (try cur_derive.ptrType(zcu)).toIntern() == ptr.ty) { | |
| 4314 | if (cur_offset == 0 and (try cur_derive.ptrType(pt)).toIntern() == ptr.ty) { | |
| 4236 | 4315 | return cur_derive; |
| 4237 | 4316 | } |
| 4238 | 4317 | |
| ... | ... | @@ -4245,20 +4324,20 @@ pub fn pointerDerivationAdvanced(ptr_val: Value, arena: Allocator, zcu: *Zcu, op |
| 4245 | 4324 | } }; |
| 4246 | 4325 | } |
| 4247 | 4326 | |
| 4248 | pub fn resolveLazy(val: Value, arena: Allocator, zcu: *Zcu) Zcu.SemaError!Value { | |
| 4249 | switch (zcu.intern_pool.indexToKey(val.toIntern())) { | |
| 4327 | pub fn resolveLazy(val: Value, arena: Allocator, pt: Zcu.PerThread) Zcu.SemaError!Value { | |
| 4328 | switch (pt.zcu.intern_pool.indexToKey(val.toIntern())) { | |
| 4250 | 4329 | .int => |int| switch (int.storage) { |
| 4251 | 4330 | .u64, .i64, .big_int => return val, |
| 4252 | .lazy_align, .lazy_size => return zcu.intValue( | |
| 4331 | .lazy_align, .lazy_size => return pt.intValue( | |
| 4253 | 4332 | Type.fromInterned(int.ty), |
| 4254 | (try val.getUnsignedIntAdvanced(zcu, .sema)).?, | |
| 4333 | (try val.getUnsignedIntAdvanced(pt, .sema)).?, | |
| 4255 | 4334 | ), |
| 4256 | 4335 | }, |
| 4257 | 4336 | .slice => |slice| { |
| 4258 | const ptr = try Value.fromInterned(slice.ptr).resolveLazy(arena, zcu); | |
| 4259 | const len = try Value.fromInterned(slice.len).resolveLazy(arena, zcu); | |
| 4337 | const ptr = try Value.fromInterned(slice.ptr).resolveLazy(arena, pt); | |
| 4338 | const len = try Value.fromInterned(slice.len).resolveLazy(arena, pt); | |
| 4260 | 4339 | if (ptr.toIntern() == slice.ptr and len.toIntern() == slice.len) return val; |
| 4261 | return Value.fromInterned(try zcu.intern(.{ .slice = .{ | |
| 4340 | return Value.fromInterned(try pt.intern(.{ .slice = .{ | |
| 4262 | 4341 | .ty = slice.ty, |
| 4263 | 4342 | .ptr = ptr.toIntern(), |
| 4264 | 4343 | .len = len.toIntern(), |
| ... | ... | @@ -4268,22 +4347,22 @@ pub fn resolveLazy(val: Value, arena: Allocator, zcu: *Zcu) Zcu.SemaError!Value |
| 4268 | 4347 | switch (ptr.base_addr) { |
| 4269 | 4348 | .decl, .comptime_alloc, .anon_decl, .int => return val, |
| 4270 | 4349 | .comptime_field => |field_val| { |
| 4271 | const resolved_field_val = (try Value.fromInterned(field_val).resolveLazy(arena, zcu)).toIntern(); | |
| 4350 | const resolved_field_val = (try Value.fromInterned(field_val).resolveLazy(arena, pt)).toIntern(); | |
| 4272 | 4351 | return if (resolved_field_val == field_val) |
| 4273 | 4352 | val |
| 4274 | 4353 | else |
| 4275 | Value.fromInterned((try zcu.intern(.{ .ptr = .{ | |
| 4354 | Value.fromInterned(try pt.intern(.{ .ptr = .{ | |
| 4276 | 4355 | .ty = ptr.ty, |
| 4277 | 4356 | .base_addr = .{ .comptime_field = resolved_field_val }, |
| 4278 | 4357 | .byte_offset = ptr.byte_offset, |
| 4279 | } }))); | |
| 4358 | } })); | |
| 4280 | 4359 | }, |
| 4281 | 4360 | .eu_payload, .opt_payload => |base| { |
| 4282 | const resolved_base = (try Value.fromInterned(base).resolveLazy(arena, zcu)).toIntern(); | |
| 4361 | const resolved_base = (try Value.fromInterned(base).resolveLazy(arena, pt)).toIntern(); | |
| 4283 | 4362 | return if (resolved_base == base) |
| 4284 | 4363 | val |
| 4285 | 4364 | else |
| 4286 | Value.fromInterned((try zcu.intern(.{ .ptr = .{ | |
| 4365 | Value.fromInterned(try pt.intern(.{ .ptr = .{ | |
| 4287 | 4366 | .ty = ptr.ty, |
| 4288 | 4367 | .base_addr = switch (ptr.base_addr) { |
| 4289 | 4368 | .eu_payload => .{ .eu_payload = resolved_base }, |
| ... | ... | @@ -4291,14 +4370,14 @@ pub fn resolveLazy(val: Value, arena: Allocator, zcu: *Zcu) Zcu.SemaError!Value |
| 4291 | 4370 | else => unreachable, |
| 4292 | 4371 | }, |
| 4293 | 4372 | .byte_offset = ptr.byte_offset, |
| 4294 | } }))); | |
| 4373 | } })); | |
| 4295 | 4374 | }, |
| 4296 | 4375 | .arr_elem, .field => |base_index| { |
| 4297 | const resolved_base = (try Value.fromInterned(base_index.base).resolveLazy(arena, zcu)).toIntern(); | |
| 4376 | const resolved_base = (try Value.fromInterned(base_index.base).resolveLazy(arena, pt)).toIntern(); | |
| 4298 | 4377 | return if (resolved_base == base_index.base) |
| 4299 | 4378 | val |
| 4300 | 4379 | else |
| 4301 | Value.fromInterned((try zcu.intern(.{ .ptr = .{ | |
| 4380 | Value.fromInterned(try pt.intern(.{ .ptr = .{ | |
| 4302 | 4381 | .ty = ptr.ty, |
| 4303 | 4382 | .base_addr = switch (ptr.base_addr) { |
| 4304 | 4383 | .arr_elem => .{ .arr_elem = .{ |
| ... | ... | @@ -4312,7 +4391,7 @@ pub fn resolveLazy(val: Value, arena: Allocator, zcu: *Zcu) Zcu.SemaError!Value |
| 4312 | 4391 | else => unreachable, |
| 4313 | 4392 | }, |
| 4314 | 4393 | .byte_offset = ptr.byte_offset, |
| 4315 | } }))); | |
| 4394 | } })); | |
| 4316 | 4395 | }, |
| 4317 | 4396 | } |
| 4318 | 4397 | }, |
| ... | ... | @@ -4321,40 +4400,40 @@ pub fn resolveLazy(val: Value, arena: Allocator, zcu: *Zcu) Zcu.SemaError!Value |
| 4321 | 4400 | .elems => |elems| { |
| 4322 | 4401 | var resolved_elems: []InternPool.Index = &.{}; |
| 4323 | 4402 | for (elems, 0..) |elem, i| { |
| 4324 | const resolved_elem = (try Value.fromInterned(elem).resolveLazy(arena, zcu)).toIntern(); | |
| 4403 | const resolved_elem = (try Value.fromInterned(elem).resolveLazy(arena, pt)).toIntern(); | |
| 4325 | 4404 | if (resolved_elems.len == 0 and resolved_elem != elem) { |
| 4326 | 4405 | resolved_elems = try arena.alloc(InternPool.Index, elems.len); |
| 4327 | 4406 | @memcpy(resolved_elems[0..i], elems[0..i]); |
| 4328 | 4407 | } |
| 4329 | 4408 | if (resolved_elems.len > 0) resolved_elems[i] = resolved_elem; |
| 4330 | 4409 | } |
| 4331 | return if (resolved_elems.len == 0) val else Value.fromInterned((try zcu.intern(.{ .aggregate = .{ | |
| 4410 | return if (resolved_elems.len == 0) val else Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 4332 | 4411 | .ty = aggregate.ty, |
| 4333 | 4412 | .storage = .{ .elems = resolved_elems }, |
| 4334 | } }))); | |
| 4413 | } })); | |
| 4335 | 4414 | }, |
| 4336 | 4415 | .repeated_elem => |elem| { |
| 4337 | const resolved_elem = (try Value.fromInterned(elem).resolveLazy(arena, zcu)).toIntern(); | |
| 4338 | return if (resolved_elem == elem) val else Value.fromInterned((try zcu.intern(.{ .aggregate = .{ | |
| 4416 | const resolved_elem = (try Value.fromInterned(elem).resolveLazy(arena, pt)).toIntern(); | |
| 4417 | return if (resolved_elem == elem) val else Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 4339 | 4418 | .ty = aggregate.ty, |
| 4340 | 4419 | .storage = .{ .repeated_elem = resolved_elem }, |
| 4341 | } }))); | |
| 4420 | } })); | |
| 4342 | 4421 | }, |
| 4343 | 4422 | }, |
| 4344 | 4423 | .un => |un| { |
| 4345 | 4424 | const resolved_tag = if (un.tag == .none) |
| 4346 | 4425 | .none |
| 4347 | 4426 | else |
| 4348 | (try Value.fromInterned(un.tag).resolveLazy(arena, zcu)).toIntern(); | |
| 4349 | const resolved_val = (try Value.fromInterned(un.val).resolveLazy(arena, zcu)).toIntern(); | |
| 4427 | (try Value.fromInterned(un.tag).resolveLazy(arena, pt)).toIntern(); | |
| 4428 | const resolved_val = (try Value.fromInterned(un.val).resolveLazy(arena, pt)).toIntern(); | |
| 4350 | 4429 | return if (resolved_tag == un.tag and resolved_val == un.val) |
| 4351 | 4430 | val |
| 4352 | 4431 | else |
| 4353 | Value.fromInterned((try zcu.intern(.{ .un = .{ | |
| 4432 | Value.fromInterned(try pt.intern(.{ .un = .{ | |
| 4354 | 4433 | .ty = un.ty, |
| 4355 | 4434 | .tag = resolved_tag, |
| 4356 | 4435 | .val = resolved_val, |
| 4357 | } }))); | |
| 4436 | } })); | |
| 4358 | 4437 | }, |
| 4359 | 4438 | else => return val, |
| 4360 | 4439 | } |
src/Zcu.zig+103-2161| ... | ... | @@ -6,7 +6,6 @@ const std = @import("std"); |
| 6 | 6 | const builtin = @import("builtin"); |
| 7 | 7 | const mem = std.mem; |
| 8 | 8 | const Allocator = std.mem.Allocator; |
| 9 | const ArrayListUnmanaged = std.ArrayListUnmanaged; | |
| 10 | 9 | const assert = std.debug.assert; |
| 11 | 10 | const log = std.log.scoped(.module); |
| 12 | 11 | const BigIntConst = std.math.big.int.Const; |
| ... | ... | @@ -75,10 +74,10 @@ local_zir_cache: Compilation.Directory, |
| 75 | 74 | |
| 76 | 75 | /// This is where all `Export` values are stored. Not all values here are necessarily valid exports; |
| 77 | 76 | /// to enumerate all exports, `single_exports` and `multi_exports` must be consulted. |
| 78 | all_exports: ArrayListUnmanaged(Export) = .{}, | |
| 77 | all_exports: std.ArrayListUnmanaged(Export) = .{}, | |
| 79 | 78 | /// This is a list of free indices in `all_exports`. These indices may be reused by exports from |
| 80 | 79 | /// future semantic analysis. |
| 81 | free_exports: ArrayListUnmanaged(u32) = .{}, | |
| 80 | free_exports: std.ArrayListUnmanaged(u32) = .{}, | |
| 82 | 81 | /// Maps from an `AnalUnit` which performs a single export, to the index into `all_exports` of |
| 83 | 82 | /// the export it performs. Note that the key is not the `Decl` being exported, but the `AnalUnit` |
| 84 | 83 | /// whose analysis triggered the export. |
| ... | ... | @@ -179,7 +178,7 @@ stage1_flags: packed struct { |
| 179 | 178 | reserved: u2 = 0, |
| 180 | 179 | } = .{}, |
| 181 | 180 | |
| 182 | compile_log_text: ArrayListUnmanaged(u8) = .{}, | |
| 181 | compile_log_text: std.ArrayListUnmanaged(u8) = .{}, | |
| 183 | 182 | |
| 184 | 183 | emit_h: ?*GlobalEmitH, |
| 185 | 184 | |
| ... | ... | @@ -203,6 +202,8 @@ panic_messages: [PanicId.len]Decl.OptionalIndex = .{.none} ** PanicId.len, |
| 203 | 202 | panic_func_index: InternPool.Index = .none, |
| 204 | 203 | null_stack_trace: InternPool.Index = .none, |
| 205 | 204 | |
| 205 | pub const PerThread = @import("Zcu/PerThread.zig"); | |
| 206 | ||
| 206 | 207 | pub const PanicId = enum { |
| 207 | 208 | unreach, |
| 208 | 209 | unwrap_null, |
| ... | ... | @@ -519,24 +520,24 @@ pub const Decl = struct { |
| 519 | 520 | return decl.getExternDecl(zcu) != .none; |
| 520 | 521 | } |
| 521 | 522 | |
| 522 | pub fn getAlignment(decl: Decl, zcu: *Zcu) Alignment { | |
| 523 | pub fn getAlignment(decl: Decl, pt: Zcu.PerThread) Alignment { | |
| 523 | 524 | assert(decl.has_tv); |
| 524 | 525 | if (decl.alignment != .none) return decl.alignment; |
| 525 | return decl.typeOf(zcu).abiAlignment(zcu); | |
| 526 | return decl.typeOf(pt.zcu).abiAlignment(pt); | |
| 526 | 527 | } |
| 527 | 528 | |
| 528 | pub fn declPtrType(decl: Decl, zcu: *Zcu) !Type { | |
| 529 | pub fn declPtrType(decl: Decl, pt: Zcu.PerThread) !Type { | |
| 529 | 530 | assert(decl.has_tv); |
| 530 | const decl_ty = decl.typeOf(zcu); | |
| 531 | return zcu.ptrType(.{ | |
| 531 | const decl_ty = decl.typeOf(pt.zcu); | |
| 532 | return pt.ptrType(.{ | |
| 532 | 533 | .child = decl_ty.toIntern(), |
| 533 | 534 | .flags = .{ |
| 534 | .alignment = if (decl.alignment == decl_ty.abiAlignment(zcu)) | |
| 535 | .alignment = if (decl.alignment == decl_ty.abiAlignment(pt)) | |
| 535 | 536 | .none |
| 536 | 537 | else |
| 537 | 538 | decl.alignment, |
| 538 | 539 | .address_space = decl.@"addrspace", |
| 539 | .is_const = decl.getOwnedVariable(zcu) == null, | |
| 540 | .is_const = decl.getOwnedVariable(pt.zcu) == null, | |
| 540 | 541 | }, |
| 541 | 542 | }); |
| 542 | 543 | } |
| ... | ... | @@ -589,7 +590,7 @@ pub const Decl = struct { |
| 589 | 590 | |
| 590 | 591 | /// This state is attached to every Decl when Module emit_h is non-null. |
| 591 | 592 | pub const EmitH = struct { |
| 592 | fwd_decl: ArrayListUnmanaged(u8) = .{}, | |
| 593 | fwd_decl: std.ArrayListUnmanaged(u8) = .{}, | |
| 593 | 594 | }; |
| 594 | 595 | |
| 595 | 596 | pub const DeclAdapter = struct { |
| ... | ... | @@ -622,8 +623,8 @@ pub const Namespace = struct { |
| 622 | 623 | /// Value is whether the usingnamespace decl is marked `pub`. |
| 623 | 624 | usingnamespace_set: std.AutoHashMapUnmanaged(Decl.Index, bool) = .{}, |
| 624 | 625 | |
| 625 | const Index = InternPool.NamespaceIndex; | |
| 626 | const OptionalIndex = InternPool.OptionalNamespaceIndex; | |
| 626 | pub const Index = InternPool.NamespaceIndex; | |
| 627 | pub const OptionalIndex = InternPool.OptionalNamespaceIndex; | |
| 627 | 628 | |
| 628 | 629 | const DeclContext = struct { |
| 629 | 630 | zcu: *Zcu, |
| ... | ... | @@ -3079,7 +3080,7 @@ pub fn markDependeeOutdated(zcu: *Zcu, dependee: InternPool.Dependee) !void { |
| 3079 | 3080 | } |
| 3080 | 3081 | } |
| 3081 | 3082 | |
| 3082 | fn markPoDependeeUpToDate(zcu: *Zcu, dependee: InternPool.Dependee) !void { | |
| 3083 | pub fn markPoDependeeUpToDate(zcu: *Zcu, dependee: InternPool.Dependee) !void { | |
| 3083 | 3084 | var it = zcu.intern_pool.dependencyIterator(dependee); |
| 3084 | 3085 | while (it.next()) |depender| { |
| 3085 | 3086 | if (zcu.outdated.getPtr(depender)) |po_dep_count| { |
| ... | ... | @@ -3279,7 +3280,7 @@ pub fn mapOldZirToNew( |
| 3279 | 3280 | old_inst: Zir.Inst.Index, |
| 3280 | 3281 | new_inst: Zir.Inst.Index, |
| 3281 | 3282 | }; |
| 3282 | var match_stack: ArrayListUnmanaged(MatchedZirDecl) = .{}; | |
| 3283 | var match_stack: std.ArrayListUnmanaged(MatchedZirDecl) = .{}; | |
| 3283 | 3284 | defer match_stack.deinit(gpa); |
| 3284 | 3285 | |
| 3285 | 3286 | // Main struct inst is always matched |
| ... | ... | @@ -3394,357 +3395,6 @@ pub fn mapOldZirToNew( |
| 3394 | 3395 | } |
| 3395 | 3396 | } |
| 3396 | 3397 | |
| 3397 | /// Like `ensureDeclAnalyzed`, but the Decl is a file's root Decl. | |
| 3398 | pub fn ensureFileAnalyzed(zcu: *Zcu, file_index: File.Index) SemaError!void { | |
| 3399 | if (zcu.fileRootDecl(file_index).unwrap()) |existing_root| { | |
| 3400 | return zcu.ensureDeclAnalyzed(existing_root); | |
| 3401 | } else { | |
| 3402 | return zcu.semaFile(file_index); | |
| 3403 | } | |
| 3404 | } | |
| 3405 | ||
| 3406 | /// This ensures that the Decl will have an up-to-date Type and Value populated. | |
| 3407 | /// However the resolution status of the Type may not be fully resolved. | |
| 3408 | /// For example an inferred error set is not resolved until after `analyzeFnBody`. | |
| 3409 | /// is called. | |
| 3410 | pub fn ensureDeclAnalyzed(mod: *Module, decl_index: Decl.Index) SemaError!void { | |
| 3411 | const tracy = trace(@src()); | |
| 3412 | defer tracy.end(); | |
| 3413 | ||
| 3414 | const ip = &mod.intern_pool; | |
| 3415 | const decl = mod.declPtr(decl_index); | |
| 3416 | ||
| 3417 | log.debug("ensureDeclAnalyzed '{d}' (name '{}')", .{ | |
| 3418 | @intFromEnum(decl_index), | |
| 3419 | decl.name.fmt(ip), | |
| 3420 | }); | |
| 3421 | ||
| 3422 | // Determine whether or not this Decl is outdated, i.e. requires re-analysis | |
| 3423 | // even if `complete`. If a Decl is PO, we pessismistically assume that it | |
| 3424 | // *does* require re-analysis, to ensure that the Decl is definitely | |
| 3425 | // up-to-date when this function returns. | |
| 3426 | ||
| 3427 | // If analysis occurs in a poor order, this could result in over-analysis. | |
| 3428 | // We do our best to avoid this by the other dependency logic in this file | |
| 3429 | // which tries to limit re-analysis to Decls whose previously listed | |
| 3430 | // dependencies are all up-to-date. | |
| 3431 | ||
| 3432 | const decl_as_depender = AnalUnit.wrap(.{ .decl = decl_index }); | |
| 3433 | const decl_was_outdated = mod.outdated.swapRemove(decl_as_depender) or | |
| 3434 | mod.potentially_outdated.swapRemove(decl_as_depender); | |
| 3435 | ||
| 3436 | if (decl_was_outdated) { | |
| 3437 | _ = mod.outdated_ready.swapRemove(decl_as_depender); | |
| 3438 | } | |
| 3439 | ||
| 3440 | const was_outdated = mod.outdated_file_root.swapRemove(decl_index) or decl_was_outdated; | |
| 3441 | ||
| 3442 | switch (decl.analysis) { | |
| 3443 | .in_progress => unreachable, | |
| 3444 | ||
| 3445 | .file_failure => return error.AnalysisFail, | |
| 3446 | ||
| 3447 | .sema_failure, | |
| 3448 | .dependency_failure, | |
| 3449 | .codegen_failure, | |
| 3450 | => if (!was_outdated) return error.AnalysisFail, | |
| 3451 | ||
| 3452 | .complete => if (!was_outdated) return, | |
| 3453 | ||
| 3454 | .unreferenced => {}, | |
| 3455 | } | |
| 3456 | ||
| 3457 | if (was_outdated) { | |
| 3458 | // The exports this Decl performs will be re-discovered, so we remove them here | |
| 3459 | // prior to re-analysis. | |
| 3460 | if (build_options.only_c) unreachable; | |
| 3461 | mod.deleteUnitExports(decl_as_depender); | |
| 3462 | mod.deleteUnitReferences(decl_as_depender); | |
| 3463 | } | |
| 3464 | ||
| 3465 | const sema_result: SemaDeclResult = blk: { | |
| 3466 | if (decl.zir_decl_index == .none and !mod.declIsRoot(decl_index)) { | |
| 3467 | // Anonymous decl. We don't semantically analyze these. | |
| 3468 | break :blk .{ | |
| 3469 | .invalidate_decl_val = false, | |
| 3470 | .invalidate_decl_ref = false, | |
| 3471 | }; | |
| 3472 | } | |
| 3473 | ||
| 3474 | if (mod.declIsRoot(decl_index)) { | |
| 3475 | const changed = try mod.semaFileUpdate(decl.getFileScopeIndex(mod), decl_was_outdated); | |
| 3476 | break :blk .{ | |
| 3477 | .invalidate_decl_val = changed, | |
| 3478 | .invalidate_decl_ref = changed, | |
| 3479 | }; | |
| 3480 | } | |
| 3481 | ||
| 3482 | const decl_prog_node = mod.sema_prog_node.start((try decl.fullyQualifiedName(mod)).toSlice(ip), 0); | |
| 3483 | defer decl_prog_node.end(); | |
| 3484 | ||
| 3485 | break :blk mod.semaDecl(decl_index) catch |err| switch (err) { | |
| 3486 | error.AnalysisFail => { | |
| 3487 | if (decl.analysis == .in_progress) { | |
| 3488 | // If this decl caused the compile error, the analysis field would | |
| 3489 | // be changed to indicate it was this Decl's fault. Because this | |
| 3490 | // did not happen, we infer here that it was a dependency failure. | |
| 3491 | decl.analysis = .dependency_failure; | |
| 3492 | } | |
| 3493 | return error.AnalysisFail; | |
| 3494 | }, | |
| 3495 | error.GenericPoison => unreachable, | |
| 3496 | else => |e| { | |
| 3497 | decl.analysis = .sema_failure; | |
| 3498 | try mod.failed_analysis.ensureUnusedCapacity(mod.gpa, 1); | |
| 3499 | try mod.retryable_failures.append(mod.gpa, AnalUnit.wrap(.{ .decl = decl_index })); | |
| 3500 | mod.failed_analysis.putAssumeCapacityNoClobber(AnalUnit.wrap(.{ .decl = decl_index }), try ErrorMsg.create( | |
| 3501 | mod.gpa, | |
| 3502 | decl.navSrcLoc(mod), | |
| 3503 | "unable to analyze: {s}", | |
| 3504 | .{@errorName(e)}, | |
| 3505 | )); | |
| 3506 | return error.AnalysisFail; | |
| 3507 | }, | |
| 3508 | }; | |
| 3509 | }; | |
| 3510 | ||
| 3511 | // TODO: we do not yet have separate dependencies for decl values vs types. | |
| 3512 | if (decl_was_outdated) { | |
| 3513 | if (sema_result.invalidate_decl_val or sema_result.invalidate_decl_ref) { | |
| 3514 | log.debug("Decl tv invalidated ('{d}')", .{@intFromEnum(decl_index)}); | |
| 3515 | // This dependency was marked as PO, meaning dependees were waiting | |
| 3516 | // on its analysis result, and it has turned out to be outdated. | |
| 3517 | // Update dependees accordingly. | |
| 3518 | try mod.markDependeeOutdated(.{ .decl_val = decl_index }); | |
| 3519 | } else { | |
| 3520 | log.debug("Decl tv up-to-date ('{d}')", .{@intFromEnum(decl_index)}); | |
| 3521 | // This dependency was previously PO, but turned out to be up-to-date. | |
| 3522 | // We do not need to queue successive analysis. | |
| 3523 | try mod.markPoDependeeUpToDate(.{ .decl_val = decl_index }); | |
| 3524 | } | |
| 3525 | } | |
| 3526 | } | |
| 3527 | ||
| 3528 | pub fn ensureFuncBodyAnalyzed(zcu: *Zcu, maybe_coerced_func_index: InternPool.Index) SemaError!void { | |
| 3529 | const tracy = trace(@src()); | |
| 3530 | defer tracy.end(); | |
| 3531 | ||
| 3532 | const gpa = zcu.gpa; | |
| 3533 | const ip = &zcu.intern_pool; | |
| 3534 | ||
| 3535 | // We only care about the uncoerced function. | |
| 3536 | // We need to do this for the "orphaned function" check below to be valid. | |
| 3537 | const func_index = ip.unwrapCoercedFunc(maybe_coerced_func_index); | |
| 3538 | ||
| 3539 | const func = zcu.funcInfo(maybe_coerced_func_index); | |
| 3540 | const decl_index = func.owner_decl; | |
| 3541 | const decl = zcu.declPtr(decl_index); | |
| 3542 | ||
| 3543 | log.debug("ensureFuncBodyAnalyzed '{d}' (instance of '{}')", .{ | |
| 3544 | @intFromEnum(func_index), | |
| 3545 | decl.name.fmt(ip), | |
| 3546 | }); | |
| 3547 | ||
| 3548 | // First, our owner decl must be up-to-date. This will always be the case | |
| 3549 | // during the first update, but may not on successive updates if we happen | |
| 3550 | // to get analyzed before our parent decl. | |
| 3551 | try zcu.ensureDeclAnalyzed(decl_index); | |
| 3552 | ||
| 3553 | // On an update, it's possible this function changed such that our owner | |
| 3554 | // decl now refers to a different function, making this one orphaned. If | |
| 3555 | // that's the case, we should remove this function from the binary. | |
| 3556 | if (decl.val.ip_index != func_index) { | |
| 3557 | try zcu.markDependeeOutdated(.{ .func_ies = func_index }); | |
| 3558 | ip.removeDependenciesForDepender(gpa, AnalUnit.wrap(.{ .func = func_index })); | |
| 3559 | ip.remove(func_index); | |
| 3560 | @panic("TODO: remove orphaned function from binary"); | |
| 3561 | } | |
| 3562 | ||
| 3563 | // We'll want to remember what the IES used to be before the update for | |
| 3564 | // dependency invalidation purposes. | |
| 3565 | const old_resolved_ies = if (func.analysis(ip).inferred_error_set) | |
| 3566 | func.resolvedErrorSet(ip).* | |
| 3567 | else | |
| 3568 | .none; | |
| 3569 | ||
| 3570 | switch (decl.analysis) { | |
| 3571 | .unreferenced => unreachable, | |
| 3572 | .in_progress => unreachable, | |
| 3573 | ||
| 3574 | .codegen_failure => unreachable, // functions do not perform constant value generation | |
| 3575 | ||
| 3576 | .file_failure, | |
| 3577 | .sema_failure, | |
| 3578 | .dependency_failure, | |
| 3579 | => return error.AnalysisFail, | |
| 3580 | ||
| 3581 | .complete => {}, | |
| 3582 | } | |
| 3583 | ||
| 3584 | const func_as_depender = AnalUnit.wrap(.{ .func = func_index }); | |
| 3585 | const was_outdated = zcu.outdated.swapRemove(func_as_depender) or | |
| 3586 | zcu.potentially_outdated.swapRemove(func_as_depender); | |
| 3587 | ||
| 3588 | if (was_outdated) { | |
| 3589 | if (build_options.only_c) unreachable; | |
| 3590 | _ = zcu.outdated_ready.swapRemove(func_as_depender); | |
| 3591 | zcu.deleteUnitExports(func_as_depender); | |
| 3592 | zcu.deleteUnitReferences(func_as_depender); | |
| 3593 | } | |
| 3594 | ||
| 3595 | switch (func.analysis(ip).state) { | |
| 3596 | .success => if (!was_outdated) return, | |
| 3597 | .sema_failure, | |
| 3598 | .dependency_failure, | |
| 3599 | .codegen_failure, | |
| 3600 | => if (!was_outdated) return error.AnalysisFail, | |
| 3601 | .none, .queued => {}, | |
| 3602 | .in_progress => unreachable, | |
| 3603 | .inline_only => unreachable, // don't queue work for this | |
| 3604 | } | |
| 3605 | ||
| 3606 | log.debug("analyze and generate fn body '{d}'; reason='{s}'", .{ | |
| 3607 | @intFromEnum(func_index), | |
| 3608 | if (was_outdated) "outdated" else "never analyzed", | |
| 3609 | }); | |
| 3610 | ||
| 3611 | var tmp_arena = std.heap.ArenaAllocator.init(gpa); | |
| 3612 | defer tmp_arena.deinit(); | |
| 3613 | const sema_arena = tmp_arena.allocator(); | |
| 3614 | ||
| 3615 | var air = zcu.analyzeFnBody(func_index, sema_arena) catch |err| switch (err) { | |
| 3616 | error.AnalysisFail => { | |
| 3617 | if (func.analysis(ip).state == .in_progress) { | |
| 3618 | // If this decl caused the compile error, the analysis field would | |
| 3619 | // be changed to indicate it was this Decl's fault. Because this | |
| 3620 | // did not happen, we infer here that it was a dependency failure. | |
| 3621 | func.analysis(ip).state = .dependency_failure; | |
| 3622 | } | |
| 3623 | return error.AnalysisFail; | |
| 3624 | }, | |
| 3625 | error.OutOfMemory => return error.OutOfMemory, | |
| 3626 | }; | |
| 3627 | errdefer air.deinit(gpa); | |
| 3628 | ||
| 3629 | const invalidate_ies_deps = i: { | |
| 3630 | if (!was_outdated) break :i false; | |
| 3631 | if (!func.analysis(ip).inferred_error_set) break :i true; | |
| 3632 | const new_resolved_ies = func.resolvedErrorSet(ip).*; | |
| 3633 | break :i new_resolved_ies != old_resolved_ies; | |
| 3634 | }; | |
| 3635 | if (invalidate_ies_deps) { | |
| 3636 | log.debug("func IES invalidated ('{d}')", .{@intFromEnum(func_index)}); | |
| 3637 | try zcu.markDependeeOutdated(.{ .func_ies = func_index }); | |
| 3638 | } else if (was_outdated) { | |
| 3639 | log.debug("func IES up-to-date ('{d}')", .{@intFromEnum(func_index)}); | |
| 3640 | try zcu.markPoDependeeUpToDate(.{ .func_ies = func_index }); | |
| 3641 | } | |
| 3642 | ||
| 3643 | const comp = zcu.comp; | |
| 3644 | ||
| 3645 | const dump_air = build_options.enable_debug_extensions and comp.verbose_air; | |
| 3646 | const dump_llvm_ir = build_options.enable_debug_extensions and (comp.verbose_llvm_ir != null or comp.verbose_llvm_bc != null); | |
| 3647 | ||
| 3648 | if (comp.bin_file == null and zcu.llvm_object == null and !dump_air and !dump_llvm_ir) { | |
| 3649 | air.deinit(gpa); | |
| 3650 | return; | |
| 3651 | } | |
| 3652 | ||
| 3653 | try comp.work_queue.writeItem(.{ .codegen_func = .{ | |
| 3654 | .func = func_index, | |
| 3655 | .air = air, | |
| 3656 | } }); | |
| 3657 | } | |
| 3658 | ||
| 3659 | /// Takes ownership of `air`, even on error. | |
| 3660 | /// If any types referenced by `air` are unresolved, marks the codegen as failed. | |
| 3661 | pub fn linkerUpdateFunc(zcu: *Zcu, func_index: InternPool.Index, air: Air) Allocator.Error!void { | |
| 3662 | const gpa = zcu.gpa; | |
| 3663 | const ip = &zcu.intern_pool; | |
| 3664 | const comp = zcu.comp; | |
| 3665 | ||
| 3666 | defer { | |
| 3667 | var air_mut = air; | |
| 3668 | air_mut.deinit(gpa); | |
| 3669 | } | |
| 3670 | ||
| 3671 | const func = zcu.funcInfo(func_index); | |
| 3672 | const decl_index = func.owner_decl; | |
| 3673 | const decl = zcu.declPtr(decl_index); | |
| 3674 | ||
| 3675 | var liveness = try Liveness.analyze(gpa, air, ip); | |
| 3676 | defer liveness.deinit(gpa); | |
| 3677 | ||
| 3678 | if (build_options.enable_debug_extensions and comp.verbose_air) { | |
| 3679 | const fqn = try decl.fullyQualifiedName(zcu); | |
| 3680 | std.debug.print("# Begin Function AIR: {}:\n", .{fqn.fmt(ip)}); | |
| 3681 | @import("print_air.zig").dump(zcu, air, liveness); | |
| 3682 | std.debug.print("# End Function AIR: {}\n\n", .{fqn.fmt(ip)}); | |
| 3683 | } | |
| 3684 | ||
| 3685 | if (std.debug.runtime_safety) { | |
| 3686 | var verify: Liveness.Verify = .{ | |
| 3687 | .gpa = gpa, | |
| 3688 | .air = air, | |
| 3689 | .liveness = liveness, | |
| 3690 | .intern_pool = ip, | |
| 3691 | }; | |
| 3692 | defer verify.deinit(); | |
| 3693 | ||
| 3694 | verify.verify() catch |err| switch (err) { | |
| 3695 | error.OutOfMemory => return error.OutOfMemory, | |
| 3696 | else => { | |
| 3697 | try zcu.failed_analysis.ensureUnusedCapacity(gpa, 1); | |
| 3698 | zcu.failed_analysis.putAssumeCapacityNoClobber( | |
| 3699 | AnalUnit.wrap(.{ .func = func_index }), | |
| 3700 | try Module.ErrorMsg.create( | |
| 3701 | gpa, | |
| 3702 | decl.navSrcLoc(zcu), | |
| 3703 | "invalid liveness: {s}", | |
| 3704 | .{@errorName(err)}, | |
| 3705 | ), | |
| 3706 | ); | |
| 3707 | func.analysis(ip).state = .codegen_failure; | |
| 3708 | return; | |
| 3709 | }, | |
| 3710 | }; | |
| 3711 | } | |
| 3712 | ||
| 3713 | const codegen_prog_node = zcu.codegen_prog_node.start((try decl.fullyQualifiedName(zcu)).toSlice(ip), 0); | |
| 3714 | defer codegen_prog_node.end(); | |
| 3715 | ||
| 3716 | if (!air.typesFullyResolved(zcu)) { | |
| 3717 | // A type we depend on failed to resolve. This is a transitive failure. | |
| 3718 | // Correcting this failure will involve changing a type this function | |
| 3719 | // depends on, hence triggering re-analysis of this function, so this | |
| 3720 | // interacts correctly with incremental compilation. | |
| 3721 | func.analysis(ip).state = .codegen_failure; | |
| 3722 | } else if (comp.bin_file) |lf| { | |
| 3723 | lf.updateFunc(zcu, func_index, air, liveness) catch |err| switch (err) { | |
| 3724 | error.OutOfMemory => return error.OutOfMemory, | |
| 3725 | error.AnalysisFail => { | |
| 3726 | func.analysis(ip).state = .codegen_failure; | |
| 3727 | }, | |
| 3728 | else => { | |
| 3729 | try zcu.failed_analysis.ensureUnusedCapacity(gpa, 1); | |
| 3730 | zcu.failed_analysis.putAssumeCapacityNoClobber(AnalUnit.wrap(.{ .func = func_index }), try Module.ErrorMsg.create( | |
| 3731 | gpa, | |
| 3732 | decl.navSrcLoc(zcu), | |
| 3733 | "unable to codegen: {s}", | |
| 3734 | .{@errorName(err)}, | |
| 3735 | )); | |
| 3736 | func.analysis(ip).state = .codegen_failure; | |
| 3737 | try zcu.retryable_failures.append(zcu.gpa, AnalUnit.wrap(.{ .func = func_index })); | |
| 3738 | }, | |
| 3739 | }; | |
| 3740 | } else if (zcu.llvm_object) |llvm_object| { | |
| 3741 | if (build_options.only_c) unreachable; | |
| 3742 | llvm_object.updateFunc(zcu, func_index, air, liveness) catch |err| switch (err) { | |
| 3743 | error.OutOfMemory => return error.OutOfMemory, | |
| 3744 | }; | |
| 3745 | } | |
| 3746 | } | |
| 3747 | ||
| 3748 | 3398 | /// Ensure this function's body is or will be analyzed and emitted. This should |
| 3749 | 3399 | /// be called whenever a potential runtime call of a function is seen. |
| 3750 | 3400 | /// |
| ... | ... | @@ -3804,608 +3454,105 @@ pub fn ensureFuncBodyAnalysisQueued(mod: *Module, func_index: InternPool.Index) |
| 3804 | 3454 | func.analysis(ip).state = .queued; |
| 3805 | 3455 | } |
| 3806 | 3456 | |
| 3807 | pub fn semaPkg(zcu: *Zcu, pkg: *Package.Module) !void { | |
| 3808 | const import_file_result = try zcu.importPkg(pkg); | |
| 3809 | const root_decl_index = zcu.fileRootDecl(import_file_result.file_index); | |
| 3810 | if (root_decl_index == .none) { | |
| 3811 | return zcu.semaFile(import_file_result.file_index); | |
| 3812 | } | |
| 3813 | } | |
| 3814 | ||
| 3815 | fn getFileRootStruct( | |
| 3816 | zcu: *Zcu, | |
| 3817 | decl_index: Decl.Index, | |
| 3818 | namespace_index: Namespace.Index, | |
| 3819 | file_index: File.Index, | |
| 3820 | ) Allocator.Error!InternPool.Index { | |
| 3821 | const gpa = zcu.gpa; | |
| 3822 | const ip = &zcu.intern_pool; | |
| 3823 | const file = zcu.fileByIndex(file_index); | |
| 3824 | const extended = file.zir.instructions.items(.data)[@intFromEnum(Zir.Inst.Index.main_struct_inst)].extended; | |
| 3825 | assert(extended.opcode == .struct_decl); | |
| 3826 | const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small); | |
| 3827 | assert(!small.has_captures_len); | |
| 3828 | assert(!small.has_backing_int); | |
| 3829 | assert(small.layout == .auto); | |
| 3830 | var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len; | |
| 3831 | const fields_len = if (small.has_fields_len) blk: { | |
| 3832 | const fields_len = file.zir.extra[extra_index]; | |
| 3833 | extra_index += 1; | |
| 3834 | break :blk fields_len; | |
| 3835 | } else 0; | |
| 3836 | const decls_len = if (small.has_decls_len) blk: { | |
| 3837 | const decls_len = file.zir.extra[extra_index]; | |
| 3838 | extra_index += 1; | |
| 3839 | break :blk decls_len; | |
| 3840 | } else 0; | |
| 3841 | const decls = file.zir.bodySlice(extra_index, decls_len); | |
| 3842 | extra_index += decls_len; | |
| 3843 | ||
| 3844 | const tracked_inst = try ip.trackZir(gpa, file_index, .main_struct_inst); | |
| 3845 | const wip_ty = switch (try ip.getStructType(gpa, .{ | |
| 3846 | .layout = .auto, | |
| 3847 | .fields_len = fields_len, | |
| 3848 | .known_non_opv = small.known_non_opv, | |
| 3849 | .requires_comptime = if (small.known_comptime_only) .yes else .unknown, | |
| 3850 | .is_tuple = small.is_tuple, | |
| 3851 | .any_comptime_fields = small.any_comptime_fields, | |
| 3852 | .any_default_inits = small.any_default_inits, | |
| 3853 | .inits_resolved = false, | |
| 3854 | .any_aligned_fields = small.any_aligned_fields, | |
| 3855 | .has_namespace = true, | |
| 3856 | .key = .{ .declared = .{ | |
| 3857 | .zir_index = tracked_inst, | |
| 3858 | .captures = &.{}, | |
| 3859 | } }, | |
| 3860 | })) { | |
| 3861 | .existing => unreachable, // we wouldn't be analysing the file root if this type existed | |
| 3862 | .wip => |wip| wip, | |
| 3863 | }; | |
| 3864 | errdefer wip_ty.cancel(ip); | |
| 3865 | ||
| 3866 | if (zcu.comp.debug_incremental) { | |
| 3867 | try ip.addDependency( | |
| 3868 | gpa, | |
| 3869 | AnalUnit.wrap(.{ .decl = decl_index }), | |
| 3870 | .{ .src_hash = tracked_inst }, | |
| 3871 | ); | |
| 3872 | } | |
| 3457 | pub const SemaDeclResult = packed struct { | |
| 3458 | /// Whether the value of a `decl_val` of this Decl changed. | |
| 3459 | invalidate_decl_val: bool, | |
| 3460 | /// Whether the type of a `decl_ref` of this Decl changed. | |
| 3461 | invalidate_decl_ref: bool, | |
| 3462 | }; | |
| 3873 | 3463 | |
| 3464 | pub fn semaAnonOwnerDecl(zcu: *Zcu, decl_index: Decl.Index) !SemaDeclResult { | |
| 3874 | 3465 | const decl = zcu.declPtr(decl_index); |
| 3875 | decl.val = Value.fromInterned(wip_ty.index); | |
| 3876 | decl.has_tv = true; | |
| 3877 | decl.owns_tv = true; | |
| 3878 | decl.analysis = .complete; | |
| 3879 | ||
| 3880 | try zcu.scanNamespace(namespace_index, decls, decl); | |
| 3881 | try zcu.comp.work_queue.writeItem(.{ .resolve_type_fully = wip_ty.index }); | |
| 3882 | return wip_ty.finish(ip, decl_index, namespace_index.toOptional()); | |
| 3883 | } | |
| 3884 | ||
| 3885 | /// Re-analyze the root Decl of a file on an incremental update. | |
| 3886 | /// If `type_outdated`, the struct type itself is considered outdated and is | |
| 3887 | /// reconstructed at a new InternPool index. Otherwise, the namespace is just | |
| 3888 | /// re-analyzed. Returns whether the decl's tyval was invalidated. | |
| 3889 | fn semaFileUpdate(zcu: *Zcu, file_index: File.Index, type_outdated: bool) SemaError!bool { | |
| 3890 | const file = zcu.fileByIndex(file_index); | |
| 3891 | const decl = zcu.declPtr(zcu.fileRootDecl(file_index).unwrap().?); | |
| 3892 | ||
| 3893 | log.debug("semaFileUpdate mod={s} sub_file_path={s} type_outdated={}", .{ | |
| 3894 | file.mod.fully_qualified_name, | |
| 3895 | file.sub_file_path, | |
| 3896 | type_outdated, | |
| 3897 | }); | |
| 3898 | ||
| 3899 | if (file.status != .success_zir) { | |
| 3900 | if (decl.analysis == .file_failure) { | |
| 3901 | return false; | |
| 3902 | } else { | |
| 3903 | decl.analysis = .file_failure; | |
| 3904 | return true; | |
| 3905 | } | |
| 3906 | } | |
| 3907 | ||
| 3908 | if (decl.analysis == .file_failure) { | |
| 3909 | // No struct type currently exists. Create one! | |
| 3910 | const root_decl = zcu.fileRootDecl(file_index); | |
| 3911 | _ = try zcu.getFileRootStruct(root_decl.unwrap().?, decl.src_namespace, file_index); | |
| 3912 | return true; | |
| 3913 | } | |
| 3914 | 3466 | |
| 3915 | 3467 | assert(decl.has_tv); |
| 3916 | 3468 | assert(decl.owns_tv); |
| 3917 | 3469 | |
| 3918 | if (type_outdated) { | |
| 3919 | // Invalidate the existing type, reusing the decl and namespace. | |
| 3920 | const file_root_decl = zcu.fileRootDecl(file_index).unwrap().?; | |
| 3921 | zcu.intern_pool.removeDependenciesForDepender(zcu.gpa, AnalUnit.wrap(.{ | |
| 3922 | .decl = file_root_decl, | |
| 3923 | })); | |
| 3924 | zcu.intern_pool.remove(decl.val.toIntern()); | |
| 3925 | decl.val = undefined; | |
| 3926 | _ = try zcu.getFileRootStruct(file_root_decl, decl.src_namespace, file_index); | |
| 3927 | return true; | |
| 3928 | } | |
| 3929 | ||
| 3930 | // Only the struct's namespace is outdated. | |
| 3931 | // Preserve the type - just scan the namespace again. | |
| 3932 | ||
| 3933 | const extended = file.zir.instructions.items(.data)[@intFromEnum(Zir.Inst.Index.main_struct_inst)].extended; | |
| 3934 | const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small); | |
| 3935 | ||
| 3936 | var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len; | |
| 3937 | extra_index += @intFromBool(small.has_fields_len); | |
| 3938 | const decls_len = if (small.has_decls_len) blk: { | |
| 3939 | const decls_len = file.zir.extra[extra_index]; | |
| 3940 | extra_index += 1; | |
| 3941 | break :blk decls_len; | |
| 3942 | } else 0; | |
| 3943 | const decls = file.zir.bodySlice(extra_index, decls_len); | |
| 3470 | log.debug("semaAnonOwnerDecl '{d}'", .{@intFromEnum(decl_index)}); | |
| 3944 | 3471 | |
| 3945 | if (!type_outdated) { | |
| 3946 | try zcu.scanNamespace(decl.src_namespace, decls, decl); | |
| 3472 | switch (decl.typeOf(zcu).zigTypeTag(zcu)) { | |
| 3473 | .Fn => @panic("TODO: update fn instance"), | |
| 3474 | .Type => {}, | |
| 3475 | else => unreachable, | |
| 3947 | 3476 | } |
| 3948 | 3477 | |
| 3949 | return false; | |
| 3478 | // We are the owner Decl of a type, and we were marked as outdated. That means the *structure* | |
| 3479 | // of this type changed; not just its namespace. Therefore, we need a new InternPool index. | |
| 3480 | // | |
| 3481 | // However, as soon as we make that, the context that created us will require re-analysis anyway | |
| 3482 | // (as it depends on this Decl's value), meaning the `struct_decl` (or equivalent) instruction | |
| 3483 | // will be analyzed again. Since Sema already needs to be able to reconstruct types like this, | |
| 3484 | // why should we bother implementing it here too when the Sema logic will be hit right after? | |
| 3485 | // | |
| 3486 | // So instead, let's just mark this Decl as failed - so that any remaining Decls which genuinely | |
| 3487 | // reference it (via `@This`) end up silently erroring too - and we'll let Sema make a new type | |
| 3488 | // with a new Decl. | |
| 3489 | // | |
| 3490 | // Yes, this does mean that any type owner Decl has a constant value for its entire lifetime. | |
| 3491 | zcu.intern_pool.removeDependenciesForDepender(zcu.gpa, AnalUnit.wrap(.{ .decl = decl_index })); | |
| 3492 | zcu.intern_pool.remove(decl.val.toIntern()); | |
| 3493 | decl.analysis = .dependency_failure; | |
| 3494 | return .{ | |
| 3495 | .invalidate_decl_val = true, | |
| 3496 | .invalidate_decl_ref = true, | |
| 3497 | }; | |
| 3950 | 3498 | } |
| 3951 | 3499 | |
| 3952 | /// Regardless of the file status, will create a `Decl` if none exists so that we can track | |
| 3953 | /// dependencies and re-analyze when the file becomes outdated. | |
| 3954 | fn semaFile(zcu: *Zcu, file_index: File.Index) SemaError!void { | |
| 3955 | const tracy = trace(@src()); | |
| 3956 | defer tracy.end(); | |
| 3957 | ||
| 3958 | const file = zcu.fileByIndex(file_index); | |
| 3959 | assert(zcu.fileRootDecl(file_index) == .none); | |
| 3500 | pub const ImportFileResult = struct { | |
| 3501 | file: *File, | |
| 3502 | file_index: File.Index, | |
| 3503 | is_new: bool, | |
| 3504 | is_pkg: bool, | |
| 3505 | }; | |
| 3960 | 3506 | |
| 3507 | pub fn importPkg(zcu: *Zcu, mod: *Package.Module) !ImportFileResult { | |
| 3961 | 3508 | const gpa = zcu.gpa; |
| 3962 | log.debug("semaFile zcu={s} sub_file_path={s}", .{ | |
| 3963 | file.mod.fully_qualified_name, file.sub_file_path, | |
| 3964 | }); | |
| 3965 | 3509 | |
| 3966 | // Because these three things each reference each other, `undefined` | |
| 3967 | // placeholders are used before being set after the struct type gains an | |
| 3968 | // InternPool index. | |
| 3969 | const new_namespace_index = try zcu.createNamespace(.{ | |
| 3970 | .parent = .none, | |
| 3971 | .decl_index = undefined, | |
| 3972 | .file_scope = file_index, | |
| 3510 | // The resolved path is used as the key in the import table, to detect if | |
| 3511 | // an import refers to the same as another, despite different relative paths | |
| 3512 | // or differently mapped package names. | |
| 3513 | const resolved_path = try std.fs.path.resolve(gpa, &.{ | |
| 3514 | mod.root.root_dir.path orelse ".", | |
| 3515 | mod.root.sub_path, | |
| 3516 | mod.root_src_path, | |
| 3973 | 3517 | }); |
| 3974 | errdefer zcu.destroyNamespace(new_namespace_index); | |
| 3518 | var keep_resolved_path = false; | |
| 3519 | defer if (!keep_resolved_path) gpa.free(resolved_path); | |
| 3975 | 3520 | |
| 3976 | const new_decl_index = try zcu.allocateNewDecl(new_namespace_index); | |
| 3977 | const new_decl = zcu.declPtr(new_decl_index); | |
| 3978 | errdefer @panic("TODO error handling"); | |
| 3521 | const gop = try zcu.import_table.getOrPut(gpa, resolved_path); | |
| 3522 | errdefer _ = zcu.import_table.pop(); | |
| 3523 | if (gop.found_existing) { | |
| 3524 | try gop.value_ptr.*.addReference(zcu.*, .{ .root = mod }); | |
| 3525 | return .{ | |
| 3526 | .file = gop.value_ptr.*, | |
| 3527 | .file_index = @enumFromInt(gop.index), | |
| 3528 | .is_new = false, | |
| 3529 | .is_pkg = true, | |
| 3530 | }; | |
| 3531 | } | |
| 3979 | 3532 | |
| 3980 | zcu.setFileRootDecl(file_index, new_decl_index.toOptional()); | |
| 3981 | zcu.namespacePtr(new_namespace_index).decl_index = new_decl_index; | |
| 3533 | const ip = &zcu.intern_pool; | |
| 3982 | 3534 | |
| 3983 | new_decl.name = try file.fullyQualifiedName(zcu); | |
| 3984 | new_decl.name_fully_qualified = true; | |
| 3985 | new_decl.is_pub = true; | |
| 3986 | new_decl.is_exported = false; | |
| 3987 | new_decl.alignment = .none; | |
| 3988 | new_decl.@"linksection" = .none; | |
| 3989 | new_decl.analysis = .in_progress; | |
| 3535 | try ip.files.ensureUnusedCapacity(gpa, 1); | |
| 3990 | 3536 | |
| 3991 | if (file.status != .success_zir) { | |
| 3992 | new_decl.analysis = .file_failure; | |
| 3993 | return; | |
| 3537 | if (mod.builtin_file) |builtin_file| { | |
| 3538 | keep_resolved_path = true; // It's now owned by import_table. | |
| 3539 | gop.value_ptr.* = builtin_file; | |
| 3540 | try builtin_file.addReference(zcu.*, .{ .root = mod }); | |
| 3541 | const path_digest = computePathDigest(zcu, mod, builtin_file.sub_file_path); | |
| 3542 | ip.files.putAssumeCapacityNoClobber(path_digest, .none); | |
| 3543 | return .{ | |
| 3544 | .file = builtin_file, | |
| 3545 | .file_index = @enumFromInt(ip.files.entries.len - 1), | |
| 3546 | .is_new = false, | |
| 3547 | .is_pkg = true, | |
| 3548 | }; | |
| 3994 | 3549 | } |
| 3995 | assert(file.zir_loaded); | |
| 3996 | 3550 | |
| 3997 | const struct_ty = try zcu.getFileRootStruct(new_decl_index, new_namespace_index, file_index); | |
| 3998 | errdefer zcu.intern_pool.remove(struct_ty); | |
| 3551 | const sub_file_path = try gpa.dupe(u8, mod.root_src_path); | |
| 3552 | errdefer gpa.free(sub_file_path); | |
| 3999 | 3553 | |
| 4000 | switch (zcu.comp.cache_use) { | |
| 4001 | .whole => |whole| if (whole.cache_manifest) |man| { | |
| 4002 | const source = file.getSource(gpa) catch |err| { | |
| 4003 | try reportRetryableFileError(zcu, file_index, "unable to load source: {s}", .{@errorName(err)}); | |
| 4004 | return error.AnalysisFail; | |
| 4005 | }; | |
| 4006 | ||
| 4007 | const resolved_path = std.fs.path.resolve(gpa, &.{ | |
| 4008 | file.mod.root.root_dir.path orelse ".", | |
| 4009 | file.mod.root.sub_path, | |
| 4010 | file.sub_file_path, | |
| 4011 | }) catch |err| { | |
| 4012 | try reportRetryableFileError(zcu, file_index, "unable to resolve path: {s}", .{@errorName(err)}); | |
| 4013 | return error.AnalysisFail; | |
| 4014 | }; | |
| 4015 | errdefer gpa.free(resolved_path); | |
| 4016 | ||
| 4017 | whole.cache_manifest_mutex.lock(); | |
| 4018 | defer whole.cache_manifest_mutex.unlock(); | |
| 4019 | try man.addFilePostContents(resolved_path, source.bytes, source.stat); | |
| 4020 | }, | |
| 4021 | .incremental => {}, | |
| 4022 | } | |
| 4023 | } | |
| 4024 | ||
| 4025 | const SemaDeclResult = packed struct { | |
| 4026 | /// Whether the value of a `decl_val` of this Decl changed. | |
| 4027 | invalidate_decl_val: bool, | |
| 4028 | /// Whether the type of a `decl_ref` of this Decl changed. | |
| 4029 | invalidate_decl_ref: bool, | |
| 4030 | }; | |
| 4031 | ||
| 4032 | fn semaDecl(zcu: *Zcu, decl_index: Decl.Index) !SemaDeclResult { | |
| 4033 | const tracy = trace(@src()); | |
| 4034 | defer tracy.end(); | |
| 4035 | ||
| 4036 | const decl = zcu.declPtr(decl_index); | |
| 4037 | const ip = &zcu.intern_pool; | |
| 4038 | ||
| 4039 | if (decl.getFileScope(zcu).status != .success_zir) { | |
| 4040 | return error.AnalysisFail; | |
| 4041 | } | |
| 4042 | ||
| 4043 | assert(!zcu.declIsRoot(decl_index)); | |
| 4044 | ||
| 4045 | if (decl.zir_decl_index == .none and decl.owns_tv) { | |
| 4046 | // We are re-analyzing an anonymous owner Decl (for a function or a namespace type). | |
| 4047 | return zcu.semaAnonOwnerDecl(decl_index); | |
| 4048 | } | |
| 4049 | ||
| 4050 | log.debug("semaDecl '{d}'", .{@intFromEnum(decl_index)}); | |
| 4051 | log.debug("decl name '{}'", .{(try decl.fullyQualifiedName(zcu)).fmt(ip)}); | |
| 4052 | defer blk: { | |
| 4053 | log.debug("finish decl name '{}'", .{(decl.fullyQualifiedName(zcu) catch break :blk).fmt(ip)}); | |
| 4054 | } | |
| 4055 | ||
| 4056 | const old_has_tv = decl.has_tv; | |
| 4057 | // The following values are ignored if `!old_has_tv` | |
| 4058 | const old_ty = if (old_has_tv) decl.typeOf(zcu) else undefined; | |
| 4059 | const old_val = decl.val; | |
| 4060 | const old_align = decl.alignment; | |
| 4061 | const old_linksection = decl.@"linksection"; | |
| 4062 | const old_addrspace = decl.@"addrspace"; | |
| 4063 | const old_is_inline = if (decl.getOwnedFunction(zcu)) |prev_func| | |
| 4064 | prev_func.analysis(ip).state == .inline_only | |
| 4065 | else | |
| 4066 | false; | |
| 4067 | ||
| 4068 | const decl_inst = decl.zir_decl_index.unwrap().?.resolve(ip); | |
| 4069 | ||
| 4070 | const gpa = zcu.gpa; | |
| 4071 | const zir = decl.getFileScope(zcu).zir; | |
| 4072 | ||
| 4073 | const builtin_type_target_index: InternPool.Index = ip_index: { | |
| 4074 | const std_mod = zcu.std_mod; | |
| 4075 | if (decl.getFileScope(zcu).mod != std_mod) break :ip_index .none; | |
| 4076 | // We're in the std module. | |
| 4077 | const std_file_imported = try zcu.importPkg(std_mod); | |
| 4078 | const std_file_root_decl_index = zcu.fileRootDecl(std_file_imported.file_index); | |
| 4079 | const std_decl = zcu.declPtr(std_file_root_decl_index.unwrap().?); | |
| 4080 | const std_namespace = std_decl.getInnerNamespace(zcu).?; | |
| 4081 | const builtin_str = try ip.getOrPutString(gpa, "builtin", .no_embedded_nulls); | |
| 4082 | const builtin_decl = zcu.declPtr(std_namespace.decls.getKeyAdapted(builtin_str, DeclAdapter{ .zcu = zcu }) orelse break :ip_index .none); | |
| 4083 | const builtin_namespace = builtin_decl.getInnerNamespaceIndex(zcu).unwrap() orelse break :ip_index .none; | |
| 4084 | if (decl.src_namespace != builtin_namespace) break :ip_index .none; | |
| 4085 | // We're in builtin.zig. This could be a builtin we need to add to a specific InternPool index. | |
| 4086 | for ([_][]const u8{ | |
| 4087 | "AtomicOrder", | |
| 4088 | "AtomicRmwOp", | |
| 4089 | "CallingConvention", | |
| 4090 | "AddressSpace", | |
| 4091 | "FloatMode", | |
| 4092 | "ReduceOp", | |
| 4093 | "CallModifier", | |
| 4094 | "PrefetchOptions", | |
| 4095 | "ExportOptions", | |
| 4096 | "ExternOptions", | |
| 4097 | "Type", | |
| 4098 | }, [_]InternPool.Index{ | |
| 4099 | .atomic_order_type, | |
| 4100 | .atomic_rmw_op_type, | |
| 4101 | .calling_convention_type, | |
| 4102 | .address_space_type, | |
| 4103 | .float_mode_type, | |
| 4104 | .reduce_op_type, | |
| 4105 | .call_modifier_type, | |
| 4106 | .prefetch_options_type, | |
| 4107 | .export_options_type, | |
| 4108 | .extern_options_type, | |
| 4109 | .type_info_type, | |
| 4110 | }) |type_name, type_ip| { | |
| 4111 | if (decl.name.eqlSlice(type_name, ip)) break :ip_index type_ip; | |
| 4112 | } | |
| 4113 | break :ip_index .none; | |
| 4114 | }; | |
| 4115 | ||
| 4116 | zcu.intern_pool.removeDependenciesForDepender(gpa, AnalUnit.wrap(.{ .decl = decl_index })); | |
| 4117 | ||
| 4118 | decl.analysis = .in_progress; | |
| 4119 | ||
| 4120 | var analysis_arena = std.heap.ArenaAllocator.init(gpa); | |
| 4121 | defer analysis_arena.deinit(); | |
| 4122 | ||
| 4123 | var comptime_err_ret_trace = std.ArrayList(LazySrcLoc).init(gpa); | |
| 4124 | defer comptime_err_ret_trace.deinit(); | |
| 4125 | ||
| 4126 | var sema: Sema = .{ | |
| 4127 | .mod = zcu, | |
| 4128 | .gpa = gpa, | |
| 4129 | .arena = analysis_arena.allocator(), | |
| 4130 | .code = zir, | |
| 4131 | .owner_decl = decl, | |
| 4132 | .owner_decl_index = decl_index, | |
| 4133 | .func_index = .none, | |
| 4134 | .func_is_naked = false, | |
| 4135 | .fn_ret_ty = Type.void, | |
| 4136 | .fn_ret_ty_ies = null, | |
| 4137 | .owner_func_index = .none, | |
| 4138 | .comptime_err_ret_trace = &comptime_err_ret_trace, | |
| 4139 | .builtin_type_target_index = builtin_type_target_index, | |
| 4140 | }; | |
| 4141 | defer sema.deinit(); | |
| 4142 | ||
| 4143 | // Every Decl (other than file root Decls, which do not have a ZIR index) has a dependency on its own source. | |
| 4144 | try sema.declareDependency(.{ .src_hash = try ip.trackZir( | |
| 4145 | gpa, | |
| 4146 | decl.getFileScopeIndex(zcu), | |
| 4147 | decl_inst, | |
| 4148 | ) }); | |
| 4149 | ||
| 4150 | var block_scope: Sema.Block = .{ | |
| 4151 | .parent = null, | |
| 4152 | .sema = &sema, | |
| 4153 | .namespace = decl.src_namespace, | |
| 4154 | .instructions = .{}, | |
| 4155 | .inlining = null, | |
| 4156 | .is_comptime = true, | |
| 4157 | .src_base_inst = decl.zir_decl_index.unwrap().?, | |
| 4158 | .type_name_ctx = decl.name, | |
| 4159 | }; | |
| 4160 | defer block_scope.instructions.deinit(gpa); | |
| 4161 | ||
| 4162 | const decl_bodies = decl.zirBodies(zcu); | |
| 4163 | ||
| 4164 | const result_ref = try sema.resolveInlineBody(&block_scope, decl_bodies.value_body, decl_inst); | |
| 4165 | // We'll do some other bits with the Sema. Clear the type target index just | |
| 4166 | // in case they analyze any type. | |
| 4167 | sema.builtin_type_target_index = .none; | |
| 4168 | const align_src: LazySrcLoc = block_scope.src(.{ .node_offset_var_decl_align = 0 }); | |
| 4169 | const section_src: LazySrcLoc = block_scope.src(.{ .node_offset_var_decl_section = 0 }); | |
| 4170 | const address_space_src: LazySrcLoc = block_scope.src(.{ .node_offset_var_decl_addrspace = 0 }); | |
| 4171 | const ty_src: LazySrcLoc = block_scope.src(.{ .node_offset_var_decl_ty = 0 }); | |
| 4172 | const init_src: LazySrcLoc = block_scope.src(.{ .node_offset_var_decl_init = 0 }); | |
| 4173 | const decl_val = try sema.resolveFinalDeclValue(&block_scope, init_src, result_ref); | |
| 4174 | const decl_ty = decl_val.typeOf(zcu); | |
| 4175 | ||
| 4176 | // Note this resolves the type of the Decl, not the value; if this Decl | |
| 4177 | // is a struct, for example, this resolves `type` (which needs no resolution), | |
| 4178 | // not the struct itself. | |
| 4179 | try decl_ty.resolveLayout(zcu); | |
| 4180 | ||
| 4181 | if (decl.kind == .@"usingnamespace") { | |
| 4182 | if (!decl_ty.eql(Type.type, zcu)) { | |
| 4183 | return sema.fail(&block_scope, ty_src, "expected type, found {}", .{ | |
| 4184 | decl_ty.fmt(zcu), | |
| 4185 | }); | |
| 4186 | } | |
| 4187 | const ty = decl_val.toType(); | |
| 4188 | if (ty.getNamespace(zcu) == null) { | |
| 4189 | return sema.fail(&block_scope, ty_src, "type {} has no namespace", .{ty.fmt(zcu)}); | |
| 4190 | } | |
| 4191 | ||
| 4192 | decl.val = ty.toValue(); | |
| 4193 | decl.alignment = .none; | |
| 4194 | decl.@"linksection" = .none; | |
| 4195 | decl.has_tv = true; | |
| 4196 | decl.owns_tv = false; | |
| 4197 | decl.analysis = .complete; | |
| 4198 | ||
| 4199 | // TODO: usingnamespace cannot currently participate in incremental compilation | |
| 4200 | return .{ | |
| 4201 | .invalidate_decl_val = true, | |
| 4202 | .invalidate_decl_ref = true, | |
| 4203 | }; | |
| 4204 | } | |
| 4205 | ||
| 4206 | var queue_linker_work = true; | |
| 4207 | var is_func = false; | |
| 4208 | var is_inline = false; | |
| 4209 | switch (decl_val.toIntern()) { | |
| 4210 | .generic_poison => unreachable, | |
| 4211 | .unreachable_value => unreachable, | |
| 4212 | else => switch (ip.indexToKey(decl_val.toIntern())) { | |
| 4213 | .variable => |variable| { | |
| 4214 | decl.owns_tv = variable.decl == decl_index; | |
| 4215 | queue_linker_work = decl.owns_tv; | |
| 4216 | }, | |
| 4217 | ||
| 4218 | .extern_func => |extern_func| { | |
| 4219 | decl.owns_tv = extern_func.decl == decl_index; | |
| 4220 | queue_linker_work = decl.owns_tv; | |
| 4221 | is_func = decl.owns_tv; | |
| 4222 | }, | |
| 4223 | ||
| 4224 | .func => |func| { | |
| 4225 | decl.owns_tv = func.owner_decl == decl_index; | |
| 4226 | queue_linker_work = false; | |
| 4227 | is_inline = decl.owns_tv and decl_ty.fnCallingConvention(zcu) == .Inline; | |
| 4228 | is_func = decl.owns_tv; | |
| 4229 | }, | |
| 4230 | ||
| 4231 | else => {}, | |
| 4232 | }, | |
| 4233 | } | |
| 4234 | ||
| 4235 | decl.val = decl_val; | |
| 4236 | // Function linksection, align, and addrspace were already set by Sema | |
| 4237 | if (!is_func) { | |
| 4238 | decl.alignment = blk: { | |
| 4239 | const align_body = decl_bodies.align_body orelse break :blk .none; | |
| 4240 | const align_ref = try sema.resolveInlineBody(&block_scope, align_body, decl_inst); | |
| 4241 | break :blk try sema.analyzeAsAlign(&block_scope, align_src, align_ref); | |
| 4242 | }; | |
| 4243 | decl.@"linksection" = blk: { | |
| 4244 | const linksection_body = decl_bodies.linksection_body orelse break :blk .none; | |
| 4245 | const linksection_ref = try sema.resolveInlineBody(&block_scope, linksection_body, decl_inst); | |
| 4246 | const bytes = try sema.toConstString(&block_scope, section_src, linksection_ref, .{ | |
| 4247 | .needed_comptime_reason = "linksection must be comptime-known", | |
| 4248 | }); | |
| 4249 | if (mem.indexOfScalar(u8, bytes, 0) != null) { | |
| 4250 | return sema.fail(&block_scope, section_src, "linksection cannot contain null bytes", .{}); | |
| 4251 | } else if (bytes.len == 0) { | |
| 4252 | return sema.fail(&block_scope, section_src, "linksection cannot be empty", .{}); | |
| 4253 | } | |
| 4254 | break :blk try ip.getOrPutStringOpt(gpa, bytes, .no_embedded_nulls); | |
| 4255 | }; | |
| 4256 | decl.@"addrspace" = blk: { | |
| 4257 | const addrspace_ctx: Sema.AddressSpaceContext = switch (ip.indexToKey(decl_val.toIntern())) { | |
| 4258 | .variable => .variable, | |
| 4259 | .extern_func, .func => .function, | |
| 4260 | else => .constant, | |
| 4261 | }; | |
| 4262 | ||
| 4263 | const target = sema.mod.getTarget(); | |
| 4264 | ||
| 4265 | const addrspace_body = decl_bodies.addrspace_body orelse break :blk switch (addrspace_ctx) { | |
| 4266 | .function => target_util.defaultAddressSpace(target, .function), | |
| 4267 | .variable => target_util.defaultAddressSpace(target, .global_mutable), | |
| 4268 | .constant => target_util.defaultAddressSpace(target, .global_constant), | |
| 4269 | else => unreachable, | |
| 4270 | }; | |
| 4271 | const addrspace_ref = try sema.resolveInlineBody(&block_scope, addrspace_body, decl_inst); | |
| 4272 | break :blk try sema.analyzeAsAddressSpace(&block_scope, address_space_src, addrspace_ref, addrspace_ctx); | |
| 4273 | }; | |
| 4274 | } | |
| 4275 | decl.has_tv = true; | |
| 4276 | decl.analysis = .complete; | |
| 4277 | ||
| 4278 | const result: SemaDeclResult = if (old_has_tv) .{ | |
| 4279 | .invalidate_decl_val = !decl_ty.eql(old_ty, zcu) or | |
| 4280 | !decl.val.eql(old_val, decl_ty, zcu) or | |
| 4281 | is_inline != old_is_inline, | |
| 4282 | .invalidate_decl_ref = !decl_ty.eql(old_ty, zcu) or | |
| 4283 | decl.alignment != old_align or | |
| 4284 | decl.@"linksection" != old_linksection or | |
| 4285 | decl.@"addrspace" != old_addrspace or | |
| 4286 | is_inline != old_is_inline, | |
| 4287 | } else .{ | |
| 4288 | .invalidate_decl_val = true, | |
| 4289 | .invalidate_decl_ref = true, | |
| 4290 | }; | |
| 4291 | ||
| 4292 | const has_runtime_bits = queue_linker_work and (is_func or try sema.typeHasRuntimeBits(decl_ty)); | |
| 4293 | if (has_runtime_bits) { | |
| 4294 | // Needed for codegen_decl which will call updateDecl and then the | |
| 4295 | // codegen backend wants full access to the Decl Type. | |
| 4296 | try decl_ty.resolveFully(zcu); | |
| 4297 | ||
| 4298 | try zcu.comp.work_queue.writeItem(.{ .codegen_decl = decl_index }); | |
| 4299 | ||
| 4300 | if (result.invalidate_decl_ref and zcu.emit_h != null) { | |
| 4301 | try zcu.comp.work_queue.writeItem(.{ .emit_h_decl = decl_index }); | |
| 4302 | } | |
| 4303 | } | |
| 4304 | ||
| 4305 | if (decl.is_exported) { | |
| 4306 | const export_src: LazySrcLoc = block_scope.src(.{ .token_offset = @intFromBool(decl.is_pub) }); | |
| 4307 | if (is_inline) return sema.fail(&block_scope, export_src, "export of inline function", .{}); | |
| 4308 | // The scope needs to have the decl in it. | |
| 4309 | try sema.analyzeExport(&block_scope, export_src, .{ .name = decl.name }, decl_index); | |
| 4310 | } | |
| 4311 | ||
| 4312 | try sema.flushExports(); | |
| 4313 | ||
| 4314 | return result; | |
| 4315 | } | |
| 4316 | ||
| 4317 | fn semaAnonOwnerDecl(zcu: *Zcu, decl_index: Decl.Index) !SemaDeclResult { | |
| 4318 | const decl = zcu.declPtr(decl_index); | |
| 4319 | ||
| 4320 | assert(decl.has_tv); | |
| 4321 | assert(decl.owns_tv); | |
| 4322 | ||
| 4323 | log.debug("semaAnonOwnerDecl '{d}'", .{@intFromEnum(decl_index)}); | |
| 4324 | ||
| 4325 | switch (decl.typeOf(zcu).zigTypeTag(zcu)) { | |
| 4326 | .Fn => @panic("TODO: update fn instance"), | |
| 4327 | .Type => {}, | |
| 4328 | else => unreachable, | |
| 4329 | } | |
| 4330 | ||
| 4331 | // We are the owner Decl of a type, and we were marked as outdated. That means the *structure* | |
| 4332 | // of this type changed; not just its namespace. Therefore, we need a new InternPool index. | |
| 4333 | // | |
| 4334 | // However, as soon as we make that, the context that created us will require re-analysis anyway | |
| 4335 | // (as it depends on this Decl's value), meaning the `struct_decl` (or equivalent) instruction | |
| 4336 | // will be analyzed again. Since Sema already needs to be able to reconstruct types like this, | |
| 4337 | // why should we bother implementing it here too when the Sema logic will be hit right after? | |
| 4338 | // | |
| 4339 | // So instead, let's just mark this Decl as failed - so that any remaining Decls which genuinely | |
| 4340 | // reference it (via `@This`) end up silently erroring too - and we'll let Sema make a new type | |
| 4341 | // with a new Decl. | |
| 4342 | // | |
| 4343 | // Yes, this does mean that any type owner Decl has a constant value for its entire lifetime. | |
| 4344 | zcu.intern_pool.removeDependenciesForDepender(zcu.gpa, AnalUnit.wrap(.{ .decl = decl_index })); | |
| 4345 | zcu.intern_pool.remove(decl.val.toIntern()); | |
| 4346 | decl.analysis = .dependency_failure; | |
| 4347 | return .{ | |
| 4348 | .invalidate_decl_val = true, | |
| 4349 | .invalidate_decl_ref = true, | |
| 4350 | }; | |
| 4351 | } | |
| 4352 | ||
| 4353 | pub const ImportFileResult = struct { | |
| 4354 | file: *File, | |
| 4355 | file_index: File.Index, | |
| 4356 | is_new: bool, | |
| 4357 | is_pkg: bool, | |
| 4358 | }; | |
| 4359 | ||
| 4360 | pub fn importPkg(zcu: *Zcu, mod: *Package.Module) !ImportFileResult { | |
| 4361 | const gpa = zcu.gpa; | |
| 4362 | ||
| 4363 | // The resolved path is used as the key in the import table, to detect if | |
| 4364 | // an import refers to the same as another, despite different relative paths | |
| 4365 | // or differently mapped package names. | |
| 4366 | const resolved_path = try std.fs.path.resolve(gpa, &.{ | |
| 4367 | mod.root.root_dir.path orelse ".", | |
| 4368 | mod.root.sub_path, | |
| 4369 | mod.root_src_path, | |
| 4370 | }); | |
| 4371 | var keep_resolved_path = false; | |
| 4372 | defer if (!keep_resolved_path) gpa.free(resolved_path); | |
| 4373 | ||
| 4374 | const gop = try zcu.import_table.getOrPut(gpa, resolved_path); | |
| 4375 | errdefer _ = zcu.import_table.pop(); | |
| 4376 | if (gop.found_existing) { | |
| 4377 | try gop.value_ptr.*.addReference(zcu.*, .{ .root = mod }); | |
| 4378 | return .{ | |
| 4379 | .file = gop.value_ptr.*, | |
| 4380 | .file_index = @enumFromInt(gop.index), | |
| 4381 | .is_new = false, | |
| 4382 | .is_pkg = true, | |
| 4383 | }; | |
| 4384 | } | |
| 4385 | ||
| 4386 | const ip = &zcu.intern_pool; | |
| 4387 | ||
| 4388 | try ip.files.ensureUnusedCapacity(gpa, 1); | |
| 4389 | ||
| 4390 | if (mod.builtin_file) |builtin_file| { | |
| 4391 | keep_resolved_path = true; // It's now owned by import_table. | |
| 4392 | gop.value_ptr.* = builtin_file; | |
| 4393 | try builtin_file.addReference(zcu.*, .{ .root = mod }); | |
| 4394 | const path_digest = computePathDigest(zcu, mod, builtin_file.sub_file_path); | |
| 4395 | ip.files.putAssumeCapacityNoClobber(path_digest, .none); | |
| 4396 | return .{ | |
| 4397 | .file = builtin_file, | |
| 4398 | .file_index = @enumFromInt(ip.files.entries.len - 1), | |
| 4399 | .is_new = false, | |
| 4400 | .is_pkg = true, | |
| 4401 | }; | |
| 4402 | } | |
| 4403 | ||
| 4404 | const sub_file_path = try gpa.dupe(u8, mod.root_src_path); | |
| 4405 | errdefer gpa.free(sub_file_path); | |
| 4406 | ||
| 4407 | const new_file = try gpa.create(File); | |
| 4408 | errdefer gpa.destroy(new_file); | |
| 3554 | const new_file = try gpa.create(File); | |
| 3555 | errdefer gpa.destroy(new_file); | |
| 4409 | 3556 | |
| 4410 | 3557 | keep_resolved_path = true; // It's now owned by import_table. |
| 4411 | 3558 | gop.value_ptr.* = new_file; |
| ... | ... | @@ -4533,78 +3680,6 @@ pub fn importFile( |
| 4533 | 3680 | }; |
| 4534 | 3681 | } |
| 4535 | 3682 | |
| 4536 | pub fn embedFile( | |
| 4537 | mod: *Module, | |
| 4538 | cur_file: *File, | |
| 4539 | import_string: []const u8, | |
| 4540 | src_loc: LazySrcLoc, | |
| 4541 | ) !InternPool.Index { | |
| 4542 | const gpa = mod.gpa; | |
| 4543 | ||
| 4544 | if (cur_file.mod.deps.get(import_string)) |pkg| { | |
| 4545 | const resolved_path = try std.fs.path.resolve(gpa, &.{ | |
| 4546 | pkg.root.root_dir.path orelse ".", | |
| 4547 | pkg.root.sub_path, | |
| 4548 | pkg.root_src_path, | |
| 4549 | }); | |
| 4550 | var keep_resolved_path = false; | |
| 4551 | defer if (!keep_resolved_path) gpa.free(resolved_path); | |
| 4552 | ||
| 4553 | const gop = try mod.embed_table.getOrPut(gpa, resolved_path); | |
| 4554 | errdefer { | |
| 4555 | assert(std.mem.eql(u8, mod.embed_table.pop().key, resolved_path)); | |
| 4556 | keep_resolved_path = false; | |
| 4557 | } | |
| 4558 | if (gop.found_existing) return gop.value_ptr.*.val; | |
| 4559 | keep_resolved_path = true; | |
| 4560 | ||
| 4561 | const sub_file_path = try gpa.dupe(u8, pkg.root_src_path); | |
| 4562 | errdefer gpa.free(sub_file_path); | |
| 4563 | ||
| 4564 | return newEmbedFile(mod, pkg, sub_file_path, resolved_path, gop.value_ptr, src_loc); | |
| 4565 | } | |
| 4566 | ||
| 4567 | // The resolved path is used as the key in the table, to detect if a file | |
| 4568 | // refers to the same as another, despite different relative paths. | |
| 4569 | const resolved_path = try std.fs.path.resolve(gpa, &.{ | |
| 4570 | cur_file.mod.root.root_dir.path orelse ".", | |
| 4571 | cur_file.mod.root.sub_path, | |
| 4572 | cur_file.sub_file_path, | |
| 4573 | "..", | |
| 4574 | import_string, | |
| 4575 | }); | |
| 4576 | ||
| 4577 | var keep_resolved_path = false; | |
| 4578 | defer if (!keep_resolved_path) gpa.free(resolved_path); | |
| 4579 | ||
| 4580 | const gop = try mod.embed_table.getOrPut(gpa, resolved_path); | |
| 4581 | errdefer { | |
| 4582 | assert(std.mem.eql(u8, mod.embed_table.pop().key, resolved_path)); | |
| 4583 | keep_resolved_path = false; | |
| 4584 | } | |
| 4585 | if (gop.found_existing) return gop.value_ptr.*.val; | |
| 4586 | keep_resolved_path = true; | |
| 4587 | ||
| 4588 | const resolved_root_path = try std.fs.path.resolve(gpa, &.{ | |
| 4589 | cur_file.mod.root.root_dir.path orelse ".", | |
| 4590 | cur_file.mod.root.sub_path, | |
| 4591 | }); | |
| 4592 | defer gpa.free(resolved_root_path); | |
| 4593 | ||
| 4594 | const sub_file_path = p: { | |
| 4595 | const relative = try std.fs.path.relative(gpa, resolved_root_path, resolved_path); | |
| 4596 | errdefer gpa.free(relative); | |
| 4597 | ||
| 4598 | if (!isUpDir(relative) and !std.fs.path.isAbsolute(relative)) { | |
| 4599 | break :p relative; | |
| 4600 | } | |
| 4601 | return error.ImportOutsideModulePath; | |
| 4602 | }; | |
| 4603 | defer gpa.free(sub_file_path); | |
| 4604 | ||
| 4605 | return newEmbedFile(mod, cur_file.mod, sub_file_path, resolved_path, gop.value_ptr, src_loc); | |
| 4606 | } | |
| 4607 | ||
| 4608 | 3683 | fn computePathDigest(zcu: *Zcu, mod: *Package.Module, sub_file_path: []const u8) Cache.BinDigest { |
| 4609 | 3684 | const want_local_cache = mod == zcu.main_mod; |
| 4610 | 3685 | var path_hash: Cache.HashHelper = .{}; |
| ... | ... | @@ -4620,87 +3695,6 @@ fn computePathDigest(zcu: *Zcu, mod: *Package.Module, sub_file_path: []const u8) |
| 4620 | 3695 | return bin; |
| 4621 | 3696 | } |
| 4622 | 3697 | |
| 4623 | /// https://github.com/ziglang/zig/issues/14307 | |
| 4624 | fn newEmbedFile( | |
| 4625 | mod: *Module, | |
| 4626 | pkg: *Package.Module, | |
| 4627 | sub_file_path: []const u8, | |
| 4628 | resolved_path: []const u8, | |
| 4629 | result: **EmbedFile, | |
| 4630 | src_loc: LazySrcLoc, | |
| 4631 | ) !InternPool.Index { | |
| 4632 | const gpa = mod.gpa; | |
| 4633 | const ip = &mod.intern_pool; | |
| 4634 | ||
| 4635 | const new_file = try gpa.create(EmbedFile); | |
| 4636 | errdefer gpa.destroy(new_file); | |
| 4637 | ||
| 4638 | var file = try pkg.root.openFile(sub_file_path, .{}); | |
| 4639 | defer file.close(); | |
| 4640 | ||
| 4641 | const actual_stat = try file.stat(); | |
| 4642 | const stat: Cache.File.Stat = .{ | |
| 4643 | .size = actual_stat.size, | |
| 4644 | .inode = actual_stat.inode, | |
| 4645 | .mtime = actual_stat.mtime, | |
| 4646 | }; | |
| 4647 | const size = std.math.cast(usize, actual_stat.size) orelse return error.Overflow; | |
| 4648 | ||
| 4649 | const bytes = try ip.string_bytes.addManyAsSlice(gpa, try std.math.add(usize, size, 1)); | |
| 4650 | const actual_read = try file.readAll(bytes[0..size]); | |
| 4651 | if (actual_read != size) return error.UnexpectedEndOfFile; | |
| 4652 | bytes[size] = 0; | |
| 4653 | ||
| 4654 | const comp = mod.comp; | |
| 4655 | switch (comp.cache_use) { | |
| 4656 | .whole => |whole| if (whole.cache_manifest) |man| { | |
| 4657 | const copied_resolved_path = try gpa.dupe(u8, resolved_path); | |
| 4658 | errdefer gpa.free(copied_resolved_path); | |
| 4659 | whole.cache_manifest_mutex.lock(); | |
| 4660 | defer whole.cache_manifest_mutex.unlock(); | |
| 4661 | try man.addFilePostContents(copied_resolved_path, bytes[0..size], stat); | |
| 4662 | }, | |
| 4663 | .incremental => {}, | |
| 4664 | } | |
| 4665 | ||
| 4666 | const array_ty = try ip.get(gpa, .{ .array_type = .{ | |
| 4667 | .len = size, | |
| 4668 | .sentinel = .zero_u8, | |
| 4669 | .child = .u8_type, | |
| 4670 | } }); | |
| 4671 | const array_val = try ip.get(gpa, .{ .aggregate = .{ | |
| 4672 | .ty = array_ty, | |
| 4673 | .storage = .{ .bytes = try ip.getOrPutTrailingString(gpa, bytes.len, .maybe_embedded_nulls) }, | |
| 4674 | } }); | |
| 4675 | ||
| 4676 | const ptr_ty = (try mod.ptrType(.{ | |
| 4677 | .child = array_ty, | |
| 4678 | .flags = .{ | |
| 4679 | .alignment = .none, | |
| 4680 | .is_const = true, | |
| 4681 | .address_space = .generic, | |
| 4682 | }, | |
| 4683 | })).toIntern(); | |
| 4684 | const ptr_val = try ip.get(gpa, .{ .ptr = .{ | |
| 4685 | .ty = ptr_ty, | |
| 4686 | .base_addr = .{ .anon_decl = .{ | |
| 4687 | .val = array_val, | |
| 4688 | .orig_ty = ptr_ty, | |
| 4689 | } }, | |
| 4690 | .byte_offset = 0, | |
| 4691 | } }); | |
| 4692 | ||
| 4693 | result.* = new_file; | |
| 4694 | new_file.* = .{ | |
| 4695 | .sub_file_path = try ip.getOrPutString(gpa, sub_file_path, .no_embedded_nulls), | |
| 4696 | .owner = pkg, | |
| 4697 | .stat = stat, | |
| 4698 | .val = ptr_val, | |
| 4699 | .src_loc = src_loc, | |
| 4700 | }; | |
| 4701 | return ptr_val; | |
| 4702 | } | |
| 4703 | ||
| 4704 | 3698 | pub fn scanNamespace( |
| 4705 | 3699 | zcu: *Zcu, |
| 4706 | 3700 | namespace_index: Namespace.Index, |
| ... | ... | @@ -4970,13 +3964,6 @@ pub fn abortAnonDecl(mod: *Module, decl_index: Decl.Index) void { |
| 4970 | 3964 | mod.destroyDecl(decl_index); |
| 4971 | 3965 | } |
| 4972 | 3966 | |
| 4973 | /// Finalize the creation of an anon decl. | |
| 4974 | pub fn finalizeAnonDecl(mod: *Module, decl_index: Decl.Index) Allocator.Error!void { | |
| 4975 | if (mod.declPtr(decl_index).typeOf(mod).isFnOrHasRuntimeBits(mod)) { | |
| 4976 | try mod.comp.work_queue.writeItem(.{ .codegen_decl = decl_index }); | |
| 4977 | } | |
| 4978 | } | |
| 4979 | ||
| 4980 | 3967 | /// Delete all the Export objects that are caused by this `AnalUnit`. Re-analysis of |
| 4981 | 3968 | /// this `AnalUnit` will cause them to be re-created (or not). |
| 4982 | 3969 | pub fn deleteUnitExports(zcu: *Zcu, anal_unit: AnalUnit) void { |
| ... | ... | @@ -5019,7 +4006,7 @@ pub fn deleteUnitExports(zcu: *Zcu, anal_unit: AnalUnit) void { |
| 5019 | 4006 | |
| 5020 | 4007 | /// Delete all references in `reference_table` which are caused by this `AnalUnit`. |
| 5021 | 4008 | /// Re-analysis of the `AnalUnit` will cause appropriate references to be recreated. |
| 5022 | fn deleteUnitReferences(zcu: *Zcu, anal_unit: AnalUnit) void { | |
| 4009 | pub fn deleteUnitReferences(zcu: *Zcu, anal_unit: AnalUnit) void { | |
| 5023 | 4010 | const gpa = zcu.gpa; |
| 5024 | 4011 | |
| 5025 | 4012 | const kv = zcu.reference_table.fetchSwapRemove(anal_unit) orelse return; |
| ... | ... | @@ -5058,258 +4045,13 @@ pub fn addUnitReference(zcu: *Zcu, src_unit: AnalUnit, referenced_unit: AnalUnit |
| 5058 | 4045 | gop.value_ptr.* = @intCast(ref_idx); |
| 5059 | 4046 | } |
| 5060 | 4047 | |
| 5061 | pub fn analyzeFnBody(mod: *Module, func_index: InternPool.Index, arena: Allocator) SemaError!Air { | |
| 5062 | const tracy = trace(@src()); | |
| 5063 | defer tracy.end(); | |
| 5064 | ||
| 5065 | const gpa = mod.gpa; | |
| 5066 | const ip = &mod.intern_pool; | |
| 5067 | const func = mod.funcInfo(func_index); | |
| 5068 | const decl_index = func.owner_decl; | |
| 5069 | const decl = mod.declPtr(decl_index); | |
| 5070 | ||
| 5071 | log.debug("func name '{}'", .{(try decl.fullyQualifiedName(mod)).fmt(ip)}); | |
| 5072 | defer blk: { | |
| 5073 | log.debug("finish func name '{}'", .{(decl.fullyQualifiedName(mod) catch break :blk).fmt(ip)}); | |
| 5074 | } | |
| 5075 | ||
| 5076 | const decl_prog_node = mod.sema_prog_node.start((try decl.fullyQualifiedName(mod)).toSlice(ip), 0); | |
| 5077 | defer decl_prog_node.end(); | |
| 5078 | ||
| 5079 | mod.intern_pool.removeDependenciesForDepender(gpa, AnalUnit.wrap(.{ .func = func_index })); | |
| 5080 | ||
| 5081 | var comptime_err_ret_trace = std.ArrayList(LazySrcLoc).init(gpa); | |
| 5082 | defer comptime_err_ret_trace.deinit(); | |
| 4048 | pub fn createNamespace(mod: *Module, initialization: Namespace) !Namespace.Index { | |
| 4049 | return mod.intern_pool.createNamespace(mod.gpa, initialization); | |
| 4050 | } | |
| 5083 | 4051 | |
| 5084 | // In the case of a generic function instance, this is the type of the | |
| 5085 | // instance, which has comptime parameters elided. In other words, it is | |
| 5086 | // the runtime-known parameters only, not to be confused with the | |
| 5087 | // generic_owner function type, which potentially has more parameters, | |
| 5088 | // including comptime parameters. | |
| 5089 | const fn_ty = decl.typeOf(mod); | |
| 5090 | const fn_ty_info = mod.typeToFunc(fn_ty).?; | |
| 5091 | ||
| 5092 | var sema: Sema = .{ | |
| 5093 | .mod = mod, | |
| 5094 | .gpa = gpa, | |
| 5095 | .arena = arena, | |
| 5096 | .code = decl.getFileScope(mod).zir, | |
| 5097 | .owner_decl = decl, | |
| 5098 | .owner_decl_index = decl_index, | |
| 5099 | .func_index = func_index, | |
| 5100 | .func_is_naked = fn_ty_info.cc == .Naked, | |
| 5101 | .fn_ret_ty = Type.fromInterned(fn_ty_info.return_type), | |
| 5102 | .fn_ret_ty_ies = null, | |
| 5103 | .owner_func_index = func_index, | |
| 5104 | .branch_quota = @max(func.branchQuota(ip).*, Sema.default_branch_quota), | |
| 5105 | .comptime_err_ret_trace = &comptime_err_ret_trace, | |
| 5106 | }; | |
| 5107 | defer sema.deinit(); | |
| 5108 | ||
| 5109 | // Every runtime function has a dependency on the source of the Decl it originates from. | |
| 5110 | // It also depends on the value of its owner Decl. | |
| 5111 | try sema.declareDependency(.{ .src_hash = decl.zir_decl_index.unwrap().? }); | |
| 5112 | try sema.declareDependency(.{ .decl_val = decl_index }); | |
| 5113 | ||
| 5114 | if (func.analysis(ip).inferred_error_set) { | |
| 5115 | const ies = try arena.create(Sema.InferredErrorSet); | |
| 5116 | ies.* = .{ .func = func_index }; | |
| 5117 | sema.fn_ret_ty_ies = ies; | |
| 5118 | } | |
| 5119 | ||
| 5120 | // reset in case calls to errorable functions are removed. | |
| 5121 | func.analysis(ip).calls_or_awaits_errorable_fn = false; | |
| 5122 | ||
| 5123 | // First few indexes of extra are reserved and set at the end. | |
| 5124 | const reserved_count = @typeInfo(Air.ExtraIndex).Enum.fields.len; | |
| 5125 | try sema.air_extra.ensureTotalCapacity(gpa, reserved_count); | |
| 5126 | sema.air_extra.items.len += reserved_count; | |
| 5127 | ||
| 5128 | var inner_block: Sema.Block = .{ | |
| 5129 | .parent = null, | |
| 5130 | .sema = &sema, | |
| 5131 | .namespace = decl.src_namespace, | |
| 5132 | .instructions = .{}, | |
| 5133 | .inlining = null, | |
| 5134 | .is_comptime = false, | |
| 5135 | .src_base_inst = inst: { | |
| 5136 | const owner_info = if (func.generic_owner == .none) | |
| 5137 | func | |
| 5138 | else | |
| 5139 | mod.funcInfo(func.generic_owner); | |
| 5140 | const orig_decl = mod.declPtr(owner_info.owner_decl); | |
| 5141 | break :inst orig_decl.zir_decl_index.unwrap().?; | |
| 5142 | }, | |
| 5143 | .type_name_ctx = decl.name, | |
| 5144 | }; | |
| 5145 | defer inner_block.instructions.deinit(gpa); | |
| 5146 | ||
| 5147 | const fn_info = sema.code.getFnInfo(func.zirBodyInst(ip).resolve(ip)); | |
| 5148 | ||
| 5149 | // Here we are performing "runtime semantic analysis" for a function body, which means | |
| 5150 | // we must map the parameter ZIR instructions to `arg` AIR instructions. | |
| 5151 | // AIR requires the `arg` parameters to be the first N instructions. | |
| 5152 | // This could be a generic function instantiation, however, in which case we need to | |
| 5153 | // map the comptime parameters to constant values and only emit arg AIR instructions | |
| 5154 | // for the runtime ones. | |
| 5155 | const runtime_params_len = fn_ty_info.param_types.len; | |
| 5156 | try inner_block.instructions.ensureTotalCapacityPrecise(gpa, runtime_params_len); | |
| 5157 | try sema.air_instructions.ensureUnusedCapacity(gpa, fn_info.total_params_len); | |
| 5158 | try sema.inst_map.ensureSpaceForInstructions(gpa, fn_info.param_body); | |
| 5159 | ||
| 5160 | // In the case of a generic function instance, pre-populate all the comptime args. | |
| 5161 | if (func.comptime_args.len != 0) { | |
| 5162 | for ( | |
| 5163 | fn_info.param_body[0..func.comptime_args.len], | |
| 5164 | func.comptime_args.get(ip), | |
| 5165 | ) |inst, comptime_arg| { | |
| 5166 | if (comptime_arg == .none) continue; | |
| 5167 | sema.inst_map.putAssumeCapacityNoClobber(inst, Air.internedToRef(comptime_arg)); | |
| 5168 | } | |
| 5169 | } | |
| 5170 | ||
| 5171 | const src_params_len = if (func.comptime_args.len != 0) | |
| 5172 | func.comptime_args.len | |
| 5173 | else | |
| 5174 | runtime_params_len; | |
| 5175 | ||
| 5176 | var runtime_param_index: usize = 0; | |
| 5177 | for (fn_info.param_body[0..src_params_len], 0..) |inst, src_param_index| { | |
| 5178 | const gop = sema.inst_map.getOrPutAssumeCapacity(inst); | |
| 5179 | if (gop.found_existing) continue; // provided above by comptime arg | |
| 5180 | ||
| 5181 | const param_ty = fn_ty_info.param_types.get(ip)[runtime_param_index]; | |
| 5182 | runtime_param_index += 1; | |
| 5183 | ||
| 5184 | const opt_opv = sema.typeHasOnePossibleValue(Type.fromInterned(param_ty)) catch |err| switch (err) { | |
| 5185 | error.GenericPoison => unreachable, | |
| 5186 | error.ComptimeReturn => unreachable, | |
| 5187 | error.ComptimeBreak => unreachable, | |
| 5188 | else => |e| return e, | |
| 5189 | }; | |
| 5190 | if (opt_opv) |opv| { | |
| 5191 | gop.value_ptr.* = Air.internedToRef(opv.toIntern()); | |
| 5192 | continue; | |
| 5193 | } | |
| 5194 | const arg_index: Air.Inst.Index = @enumFromInt(sema.air_instructions.len); | |
| 5195 | gop.value_ptr.* = arg_index.toRef(); | |
| 5196 | inner_block.instructions.appendAssumeCapacity(arg_index); | |
| 5197 | sema.air_instructions.appendAssumeCapacity(.{ | |
| 5198 | .tag = .arg, | |
| 5199 | .data = .{ .arg = .{ | |
| 5200 | .ty = Air.internedToRef(param_ty), | |
| 5201 | .src_index = @intCast(src_param_index), | |
| 5202 | } }, | |
| 5203 | }); | |
| 5204 | } | |
| 5205 | ||
| 5206 | func.analysis(ip).state = .in_progress; | |
| 5207 | ||
| 5208 | const last_arg_index = inner_block.instructions.items.len; | |
| 5209 | ||
| 5210 | // Save the error trace as our first action in the function. | |
| 5211 | // If this is unnecessary after all, Liveness will clean it up for us. | |
| 5212 | const error_return_trace_index = try sema.analyzeSaveErrRetIndex(&inner_block); | |
| 5213 | sema.error_return_trace_index_on_fn_entry = error_return_trace_index; | |
| 5214 | inner_block.error_return_trace_index = error_return_trace_index; | |
| 5215 | ||
| 5216 | sema.analyzeFnBody(&inner_block, fn_info.body) catch |err| switch (err) { | |
| 5217 | // TODO make these unreachable instead of @panic | |
| 5218 | error.GenericPoison => @panic("zig compiler bug: GenericPoison"), | |
| 5219 | error.ComptimeReturn => @panic("zig compiler bug: ComptimeReturn"), | |
| 5220 | else => |e| return e, | |
| 5221 | }; | |
| 5222 | ||
| 5223 | for (sema.unresolved_inferred_allocs.keys()) |ptr_inst| { | |
| 5224 | // The lack of a resolve_inferred_alloc means that this instruction | |
| 5225 | // is unused so it just has to be a no-op. | |
| 5226 | sema.air_instructions.set(@intFromEnum(ptr_inst), .{ | |
| 5227 | .tag = .alloc, | |
| 5228 | .data = .{ .ty = Type.single_const_pointer_to_comptime_int }, | |
| 5229 | }); | |
| 5230 | } | |
| 5231 | ||
| 5232 | // If we don't get an error return trace from a caller, create our own. | |
| 5233 | if (func.analysis(ip).calls_or_awaits_errorable_fn and | |
| 5234 | mod.comp.config.any_error_tracing and | |
| 5235 | !sema.fn_ret_ty.isError(mod)) | |
| 5236 | { | |
| 5237 | sema.setupErrorReturnTrace(&inner_block, last_arg_index) catch |err| switch (err) { | |
| 5238 | // TODO make these unreachable instead of @panic | |
| 5239 | error.GenericPoison => @panic("zig compiler bug: GenericPoison"), | |
| 5240 | error.ComptimeReturn => @panic("zig compiler bug: ComptimeReturn"), | |
| 5241 | error.ComptimeBreak => @panic("zig compiler bug: ComptimeBreak"), | |
| 5242 | else => |e| return e, | |
| 5243 | }; | |
| 5244 | } | |
| 5245 | ||
| 5246 | // Copy the block into place and mark that as the main block. | |
| 5247 | try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.Block).Struct.fields.len + | |
| 5248 | inner_block.instructions.items.len); | |
| 5249 | const main_block_index = sema.addExtraAssumeCapacity(Air.Block{ | |
| 5250 | .body_len = @intCast(inner_block.instructions.items.len), | |
| 5251 | }); | |
| 5252 | sema.air_extra.appendSliceAssumeCapacity(@ptrCast(inner_block.instructions.items)); | |
| 5253 | sema.air_extra.items[@intFromEnum(Air.ExtraIndex.main_block)] = main_block_index; | |
| 5254 | ||
| 5255 | // Resolving inferred error sets is done *before* setting the function | |
| 5256 | // state to success, so that "unable to resolve inferred error set" errors | |
| 5257 | // can be emitted here. | |
| 5258 | if (sema.fn_ret_ty_ies) |ies| { | |
| 5259 | sema.resolveInferredErrorSetPtr(&inner_block, .{ | |
| 5260 | .base_node_inst = inner_block.src_base_inst, | |
| 5261 | .offset = LazySrcLoc.Offset.nodeOffset(0), | |
| 5262 | }, ies) catch |err| switch (err) { | |
| 5263 | error.GenericPoison => unreachable, | |
| 5264 | error.ComptimeReturn => unreachable, | |
| 5265 | error.ComptimeBreak => unreachable, | |
| 5266 | error.AnalysisFail => { | |
| 5267 | // In this case our function depends on a type that had a compile error. | |
| 5268 | // We should not try to lower this function. | |
| 5269 | decl.analysis = .dependency_failure; | |
| 5270 | return error.AnalysisFail; | |
| 5271 | }, | |
| 5272 | else => |e| return e, | |
| 5273 | }; | |
| 5274 | assert(ies.resolved != .none); | |
| 5275 | ip.funcIesResolved(func_index).* = ies.resolved; | |
| 5276 | } | |
| 5277 | ||
| 5278 | func.analysis(ip).state = .success; | |
| 5279 | ||
| 5280 | // Finally we must resolve the return type and parameter types so that backends | |
| 5281 | // have full access to type information. | |
| 5282 | // Crucially, this happens *after* we set the function state to success above, | |
| 5283 | // so that dependencies on the function body will now be satisfied rather than | |
| 5284 | // result in circular dependency errors. | |
| 5285 | sema.resolveFnTypes(fn_ty) catch |err| switch (err) { | |
| 5286 | error.GenericPoison => unreachable, | |
| 5287 | error.ComptimeReturn => unreachable, | |
| 5288 | error.ComptimeBreak => unreachable, | |
| 5289 | error.AnalysisFail => { | |
| 5290 | // In this case our function depends on a type that had a compile error. | |
| 5291 | // We should not try to lower this function. | |
| 5292 | decl.analysis = .dependency_failure; | |
| 5293 | return error.AnalysisFail; | |
| 5294 | }, | |
| 5295 | else => |e| return e, | |
| 5296 | }; | |
| 5297 | ||
| 5298 | try sema.flushExports(); | |
| 5299 | ||
| 5300 | return .{ | |
| 5301 | .instructions = sema.air_instructions.toOwnedSlice(), | |
| 5302 | .extra = try sema.air_extra.toOwnedSlice(gpa), | |
| 5303 | }; | |
| 5304 | } | |
| 5305 | ||
| 5306 | pub fn createNamespace(mod: *Module, initialization: Namespace) !Namespace.Index { | |
| 5307 | return mod.intern_pool.createNamespace(mod.gpa, initialization); | |
| 5308 | } | |
| 5309 | ||
| 5310 | pub fn destroyNamespace(mod: *Module, index: Namespace.Index) void { | |
| 5311 | return mod.intern_pool.destroyNamespace(mod.gpa, index); | |
| 5312 | } | |
| 4052 | pub fn destroyNamespace(mod: *Module, index: Namespace.Index) void { | |
| 4053 | return mod.intern_pool.destroyNamespace(mod.gpa, index); | |
| 4054 | } | |
| 5313 | 4055 | |
| 5314 | 4056 | pub fn allocateNewDecl(zcu: *Zcu, namespace: Namespace.Index) !Decl.Index { |
| 5315 | 4057 | const gpa = zcu.gpa; |
| ... | ... | @@ -5420,117 +4162,7 @@ fn lockAndClearFileCompileError(mod: *Module, file: *File) void { |
| 5420 | 4162 | } |
| 5421 | 4163 | } |
| 5422 | 4164 | |
| 5423 | /// Called from `Compilation.update`, after everything is done, just before | |
| 5424 | /// reporting compile errors. In this function we emit exported symbol collision | |
| 5425 | /// errors and communicate exported symbols to the linker backend. | |
| 5426 | pub fn processExports(zcu: *Zcu) !void { | |
| 5427 | const gpa = zcu.gpa; | |
| 5428 | ||
| 5429 | // First, construct a mapping of every exported value and Decl to the indices of all its different exports. | |
| 5430 | var decl_exports: std.AutoArrayHashMapUnmanaged(Decl.Index, ArrayListUnmanaged(u32)) = .{}; | |
| 5431 | var value_exports: std.AutoArrayHashMapUnmanaged(InternPool.Index, ArrayListUnmanaged(u32)) = .{}; | |
| 5432 | defer { | |
| 5433 | for (decl_exports.values()) |*exports| { | |
| 5434 | exports.deinit(gpa); | |
| 5435 | } | |
| 5436 | decl_exports.deinit(gpa); | |
| 5437 | for (value_exports.values()) |*exports| { | |
| 5438 | exports.deinit(gpa); | |
| 5439 | } | |
| 5440 | value_exports.deinit(gpa); | |
| 5441 | } | |
| 5442 | ||
| 5443 | // We note as a heuristic: | |
| 5444 | // * It is rare to export a value. | |
| 5445 | // * It is rare for one Decl to be exported multiple times. | |
| 5446 | // So, this ensureTotalCapacity serves as a reasonable (albeit very approximate) optimization. | |
| 5447 | try decl_exports.ensureTotalCapacity(gpa, zcu.single_exports.count() + zcu.multi_exports.count()); | |
| 5448 | ||
| 5449 | for (zcu.single_exports.values()) |export_idx| { | |
| 5450 | const exp = zcu.all_exports.items[export_idx]; | |
| 5451 | const value_ptr, const found_existing = switch (exp.exported) { | |
| 5452 | .decl_index => |i| gop: { | |
| 5453 | const gop = try decl_exports.getOrPut(gpa, i); | |
| 5454 | break :gop .{ gop.value_ptr, gop.found_existing }; | |
| 5455 | }, | |
| 5456 | .value => |i| gop: { | |
| 5457 | const gop = try value_exports.getOrPut(gpa, i); | |
| 5458 | break :gop .{ gop.value_ptr, gop.found_existing }; | |
| 5459 | }, | |
| 5460 | }; | |
| 5461 | if (!found_existing) value_ptr.* = .{}; | |
| 5462 | try value_ptr.append(gpa, export_idx); | |
| 5463 | } | |
| 5464 | ||
| 5465 | for (zcu.multi_exports.values()) |info| { | |
| 5466 | for (zcu.all_exports.items[info.index..][0..info.len], info.index..) |exp, export_idx| { | |
| 5467 | const value_ptr, const found_existing = switch (exp.exported) { | |
| 5468 | .decl_index => |i| gop: { | |
| 5469 | const gop = try decl_exports.getOrPut(gpa, i); | |
| 5470 | break :gop .{ gop.value_ptr, gop.found_existing }; | |
| 5471 | }, | |
| 5472 | .value => |i| gop: { | |
| 5473 | const gop = try value_exports.getOrPut(gpa, i); | |
| 5474 | break :gop .{ gop.value_ptr, gop.found_existing }; | |
| 5475 | }, | |
| 5476 | }; | |
| 5477 | if (!found_existing) value_ptr.* = .{}; | |
| 5478 | try value_ptr.append(gpa, @intCast(export_idx)); | |
| 5479 | } | |
| 5480 | } | |
| 5481 | ||
| 5482 | // Map symbol names to `Export` for name collision detection. | |
| 5483 | var symbol_exports: SymbolExports = .{}; | |
| 5484 | defer symbol_exports.deinit(gpa); | |
| 5485 | ||
| 5486 | for (decl_exports.keys(), decl_exports.values()) |exported_decl, exports_list| { | |
| 5487 | const exported: Exported = .{ .decl_index = exported_decl }; | |
| 5488 | try processExportsInner(zcu, &symbol_exports, exported, exports_list.items); | |
| 5489 | } | |
| 5490 | ||
| 5491 | for (value_exports.keys(), value_exports.values()) |exported_value, exports_list| { | |
| 5492 | const exported: Exported = .{ .value = exported_value }; | |
| 5493 | try processExportsInner(zcu, &symbol_exports, exported, exports_list.items); | |
| 5494 | } | |
| 5495 | } | |
| 5496 | ||
| 5497 | const SymbolExports = std.AutoArrayHashMapUnmanaged(InternPool.NullTerminatedString, u32); | |
| 5498 | ||
| 5499 | fn processExportsInner( | |
| 5500 | zcu: *Zcu, | |
| 5501 | symbol_exports: *SymbolExports, | |
| 5502 | exported: Exported, | |
| 5503 | export_indices: []const u32, | |
| 5504 | ) error{OutOfMemory}!void { | |
| 5505 | const gpa = zcu.gpa; | |
| 5506 | ||
| 5507 | for (export_indices) |export_idx| { | |
| 5508 | const new_export = &zcu.all_exports.items[export_idx]; | |
| 5509 | const gop = try symbol_exports.getOrPut(gpa, new_export.opts.name); | |
| 5510 | if (gop.found_existing) { | |
| 5511 | new_export.status = .failed_retryable; | |
| 5512 | try zcu.failed_exports.ensureUnusedCapacity(gpa, 1); | |
| 5513 | const msg = try ErrorMsg.create(gpa, new_export.src, "exported symbol collision: {}", .{ | |
| 5514 | new_export.opts.name.fmt(&zcu.intern_pool), | |
| 5515 | }); | |
| 5516 | errdefer msg.destroy(gpa); | |
| 5517 | const other_export = zcu.all_exports.items[gop.value_ptr.*]; | |
| 5518 | try zcu.errNote(other_export.src, msg, "other symbol here", .{}); | |
| 5519 | zcu.failed_exports.putAssumeCapacityNoClobber(export_idx, msg); | |
| 5520 | new_export.status = .failed; | |
| 5521 | } else { | |
| 5522 | gop.value_ptr.* = export_idx; | |
| 5523 | } | |
| 5524 | } | |
| 5525 | if (zcu.comp.bin_file) |lf| { | |
| 5526 | try handleUpdateExports(zcu, export_indices, lf.updateExports(zcu, exported, export_indices)); | |
| 5527 | } else if (zcu.llvm_object) |llvm_object| { | |
| 5528 | if (build_options.only_c) unreachable; | |
| 5529 | try handleUpdateExports(zcu, export_indices, llvm_object.updateExports(zcu, exported, export_indices)); | |
| 5530 | } | |
| 5531 | } | |
| 5532 | ||
| 5533 | fn handleUpdateExports( | |
| 4165 | pub fn handleUpdateExports( | |
| 5534 | 4166 | zcu: *Zcu, |
| 5535 | 4167 | export_indices: []const u32, |
| 5536 | 4168 | result: link.File.UpdateExportsError!void, |
| ... | ... | @@ -5551,180 +4183,7 @@ fn handleUpdateExports( |
| 5551 | 4183 | }; |
| 5552 | 4184 | } |
| 5553 | 4185 | |
| 5554 | pub fn populateTestFunctions( | |
| 5555 | zcu: *Zcu, | |
| 5556 | main_progress_node: std.Progress.Node, | |
| 5557 | ) !void { | |
| 5558 | const gpa = zcu.gpa; | |
| 5559 | const ip = &zcu.intern_pool; | |
| 5560 | const builtin_mod = zcu.root_mod.getBuiltinDependency(); | |
| 5561 | const builtin_file_index = (zcu.importPkg(builtin_mod) catch unreachable).file_index; | |
| 5562 | const root_decl_index = zcu.fileRootDecl(builtin_file_index); | |
| 5563 | const root_decl = zcu.declPtr(root_decl_index.unwrap().?); | |
| 5564 | const builtin_namespace = zcu.namespacePtr(root_decl.src_namespace); | |
| 5565 | const test_functions_str = try ip.getOrPutString(gpa, "test_functions", .no_embedded_nulls); | |
| 5566 | const decl_index = builtin_namespace.decls.getKeyAdapted( | |
| 5567 | test_functions_str, | |
| 5568 | DeclAdapter{ .zcu = zcu }, | |
| 5569 | ).?; | |
| 5570 | { | |
| 5571 | // We have to call `ensureDeclAnalyzed` here in case `builtin.test_functions` | |
| 5572 | // was not referenced by start code. | |
| 5573 | zcu.sema_prog_node = main_progress_node.start("Semantic Analysis", 0); | |
| 5574 | defer { | |
| 5575 | zcu.sema_prog_node.end(); | |
| 5576 | zcu.sema_prog_node = undefined; | |
| 5577 | } | |
| 5578 | try zcu.ensureDeclAnalyzed(decl_index); | |
| 5579 | } | |
| 5580 | ||
| 5581 | const decl = zcu.declPtr(decl_index); | |
| 5582 | const test_fn_ty = decl.typeOf(zcu).slicePtrFieldType(zcu).childType(zcu); | |
| 5583 | ||
| 5584 | const array_anon_decl: InternPool.Key.Ptr.BaseAddr.AnonDecl = array: { | |
| 5585 | // Add zcu.test_functions to an array decl then make the test_functions | |
| 5586 | // decl reference it as a slice. | |
| 5587 | const test_fn_vals = try gpa.alloc(InternPool.Index, zcu.test_functions.count()); | |
| 5588 | defer gpa.free(test_fn_vals); | |
| 5589 | ||
| 5590 | for (test_fn_vals, zcu.test_functions.keys()) |*test_fn_val, test_decl_index| { | |
| 5591 | const test_decl = zcu.declPtr(test_decl_index); | |
| 5592 | const test_decl_name = try test_decl.fullyQualifiedName(zcu); | |
| 5593 | const test_decl_name_len = test_decl_name.length(ip); | |
| 5594 | const test_name_anon_decl: InternPool.Key.Ptr.BaseAddr.AnonDecl = n: { | |
| 5595 | const test_name_ty = try zcu.arrayType(.{ | |
| 5596 | .len = test_decl_name_len, | |
| 5597 | .child = .u8_type, | |
| 5598 | }); | |
| 5599 | const test_name_val = try zcu.intern(.{ .aggregate = .{ | |
| 5600 | .ty = test_name_ty.toIntern(), | |
| 5601 | .storage = .{ .bytes = test_decl_name.toString() }, | |
| 5602 | } }); | |
| 5603 | break :n .{ | |
| 5604 | .orig_ty = (try zcu.singleConstPtrType(test_name_ty)).toIntern(), | |
| 5605 | .val = test_name_val, | |
| 5606 | }; | |
| 5607 | }; | |
| 5608 | ||
| 5609 | const test_fn_fields = .{ | |
| 5610 | // name | |
| 5611 | try zcu.intern(.{ .slice = .{ | |
| 5612 | .ty = .slice_const_u8_type, | |
| 5613 | .ptr = try zcu.intern(.{ .ptr = .{ | |
| 5614 | .ty = .manyptr_const_u8_type, | |
| 5615 | .base_addr = .{ .anon_decl = test_name_anon_decl }, | |
| 5616 | .byte_offset = 0, | |
| 5617 | } }), | |
| 5618 | .len = try zcu.intern(.{ .int = .{ | |
| 5619 | .ty = .usize_type, | |
| 5620 | .storage = .{ .u64 = test_decl_name_len }, | |
| 5621 | } }), | |
| 5622 | } }), | |
| 5623 | // func | |
| 5624 | try zcu.intern(.{ .ptr = .{ | |
| 5625 | .ty = try zcu.intern(.{ .ptr_type = .{ | |
| 5626 | .child = test_decl.typeOf(zcu).toIntern(), | |
| 5627 | .flags = .{ | |
| 5628 | .is_const = true, | |
| 5629 | }, | |
| 5630 | } }), | |
| 5631 | .base_addr = .{ .decl = test_decl_index }, | |
| 5632 | .byte_offset = 0, | |
| 5633 | } }), | |
| 5634 | }; | |
| 5635 | test_fn_val.* = try zcu.intern(.{ .aggregate = .{ | |
| 5636 | .ty = test_fn_ty.toIntern(), | |
| 5637 | .storage = .{ .elems = &test_fn_fields }, | |
| 5638 | } }); | |
| 5639 | } | |
| 5640 | ||
| 5641 | const array_ty = try zcu.arrayType(.{ | |
| 5642 | .len = test_fn_vals.len, | |
| 5643 | .child = test_fn_ty.toIntern(), | |
| 5644 | .sentinel = .none, | |
| 5645 | }); | |
| 5646 | const array_val = try zcu.intern(.{ .aggregate = .{ | |
| 5647 | .ty = array_ty.toIntern(), | |
| 5648 | .storage = .{ .elems = test_fn_vals }, | |
| 5649 | } }); | |
| 5650 | break :array .{ | |
| 5651 | .orig_ty = (try zcu.singleConstPtrType(array_ty)).toIntern(), | |
| 5652 | .val = array_val, | |
| 5653 | }; | |
| 5654 | }; | |
| 5655 | ||
| 5656 | { | |
| 5657 | const new_ty = try zcu.ptrType(.{ | |
| 5658 | .child = test_fn_ty.toIntern(), | |
| 5659 | .flags = .{ | |
| 5660 | .is_const = true, | |
| 5661 | .size = .Slice, | |
| 5662 | }, | |
| 5663 | }); | |
| 5664 | const new_val = decl.val; | |
| 5665 | const new_init = try zcu.intern(.{ .slice = .{ | |
| 5666 | .ty = new_ty.toIntern(), | |
| 5667 | .ptr = try zcu.intern(.{ .ptr = .{ | |
| 5668 | .ty = new_ty.slicePtrFieldType(zcu).toIntern(), | |
| 5669 | .base_addr = .{ .anon_decl = array_anon_decl }, | |
| 5670 | .byte_offset = 0, | |
| 5671 | } }), | |
| 5672 | .len = (try zcu.intValue(Type.usize, zcu.test_functions.count())).toIntern(), | |
| 5673 | } }); | |
| 5674 | ip.mutateVarInit(decl.val.toIntern(), new_init); | |
| 5675 | ||
| 5676 | // Since we are replacing the Decl's value we must perform cleanup on the | |
| 5677 | // previous value. | |
| 5678 | decl.val = new_val; | |
| 5679 | decl.has_tv = true; | |
| 5680 | } | |
| 5681 | { | |
| 5682 | zcu.codegen_prog_node = main_progress_node.start("Code Generation", 0); | |
| 5683 | defer { | |
| 5684 | zcu.codegen_prog_node.end(); | |
| 5685 | zcu.codegen_prog_node = undefined; | |
| 5686 | } | |
| 5687 | ||
| 5688 | try zcu.linkerUpdateDecl(decl_index); | |
| 5689 | } | |
| 5690 | } | |
| 5691 | ||
| 5692 | pub fn linkerUpdateDecl(zcu: *Zcu, decl_index: Decl.Index) !void { | |
| 5693 | const comp = zcu.comp; | |
| 5694 | ||
| 5695 | const decl = zcu.declPtr(decl_index); | |
| 5696 | ||
| 5697 | const codegen_prog_node = zcu.codegen_prog_node.start((try decl.fullyQualifiedName(zcu)).toSlice(&zcu.intern_pool), 0); | |
| 5698 | defer codegen_prog_node.end(); | |
| 5699 | ||
| 5700 | if (comp.bin_file) |lf| { | |
| 5701 | lf.updateDecl(zcu, decl_index) catch |err| switch (err) { | |
| 5702 | error.OutOfMemory => return error.OutOfMemory, | |
| 5703 | error.AnalysisFail => { | |
| 5704 | decl.analysis = .codegen_failure; | |
| 5705 | }, | |
| 5706 | else => { | |
| 5707 | const gpa = zcu.gpa; | |
| 5708 | try zcu.failed_analysis.ensureUnusedCapacity(gpa, 1); | |
| 5709 | zcu.failed_analysis.putAssumeCapacityNoClobber(AnalUnit.wrap(.{ .decl = decl_index }), try ErrorMsg.create( | |
| 5710 | gpa, | |
| 5711 | decl.navSrcLoc(zcu), | |
| 5712 | "unable to codegen: {s}", | |
| 5713 | .{@errorName(err)}, | |
| 5714 | )); | |
| 5715 | decl.analysis = .codegen_failure; | |
| 5716 | try zcu.retryable_failures.append(zcu.gpa, AnalUnit.wrap(.{ .decl = decl_index })); | |
| 5717 | }, | |
| 5718 | }; | |
| 5719 | } else if (zcu.llvm_object) |llvm_object| { | |
| 5720 | if (build_options.only_c) unreachable; | |
| 5721 | llvm_object.updateDecl(zcu, decl_index) catch |err| switch (err) { | |
| 5722 | error.OutOfMemory => return error.OutOfMemory, | |
| 5723 | }; | |
| 5724 | } | |
| 5725 | } | |
| 5726 | ||
| 5727 | fn reportRetryableFileError( | |
| 4186 | pub fn reportRetryableFileError( | |
| 5728 | 4187 | zcu: *Zcu, |
| 5729 | 4188 | file_index: File.Index, |
| 5730 | 4189 | comptime format: []const u8, |
| ... | ... | @@ -5795,344 +4254,6 @@ pub fn backendSupportsFeature(zcu: Module, feature: Feature) bool { |
| 5795 | 4254 | return target_util.backendSupportsFeature(cpu_arch, ofmt, use_llvm, feature); |
| 5796 | 4255 | } |
| 5797 | 4256 | |
| 5798 | /// Shortcut for calling `intern_pool.get`. | |
| 5799 | pub fn intern(mod: *Module, key: InternPool.Key) Allocator.Error!InternPool.Index { | |
| 5800 | return mod.intern_pool.get(mod.gpa, key); | |
| 5801 | } | |
| 5802 | ||
| 5803 | /// Shortcut for calling `intern_pool.getCoerced`. | |
| 5804 | pub fn getCoerced(mod: *Module, val: Value, new_ty: Type) Allocator.Error!Value { | |
| 5805 | return Value.fromInterned((try mod.intern_pool.getCoerced(mod.gpa, val.toIntern(), new_ty.toIntern()))); | |
| 5806 | } | |
| 5807 | ||
| 5808 | pub fn intType(mod: *Module, signedness: std.builtin.Signedness, bits: u16) Allocator.Error!Type { | |
| 5809 | return Type.fromInterned((try intern(mod, .{ .int_type = .{ | |
| 5810 | .signedness = signedness, | |
| 5811 | .bits = bits, | |
| 5812 | } }))); | |
| 5813 | } | |
| 5814 | ||
| 5815 | pub fn errorIntType(mod: *Module) std.mem.Allocator.Error!Type { | |
| 5816 | return mod.intType(.unsigned, mod.errorSetBits()); | |
| 5817 | } | |
| 5818 | ||
| 5819 | pub fn arrayType(mod: *Module, info: InternPool.Key.ArrayType) Allocator.Error!Type { | |
| 5820 | const i = try intern(mod, .{ .array_type = info }); | |
| 5821 | return Type.fromInterned(i); | |
| 5822 | } | |
| 5823 | ||
| 5824 | pub fn vectorType(mod: *Module, info: InternPool.Key.VectorType) Allocator.Error!Type { | |
| 5825 | const i = try intern(mod, .{ .vector_type = info }); | |
| 5826 | return Type.fromInterned(i); | |
| 5827 | } | |
| 5828 | ||
| 5829 | pub fn optionalType(mod: *Module, child_type: InternPool.Index) Allocator.Error!Type { | |
| 5830 | const i = try intern(mod, .{ .opt_type = child_type }); | |
| 5831 | return Type.fromInterned(i); | |
| 5832 | } | |
| 5833 | ||
| 5834 | pub fn ptrType(mod: *Module, info: InternPool.Key.PtrType) Allocator.Error!Type { | |
| 5835 | var canon_info = info; | |
| 5836 | ||
| 5837 | if (info.flags.size == .C) canon_info.flags.is_allowzero = true; | |
| 5838 | ||
| 5839 | // Canonicalize non-zero alignment. If it matches the ABI alignment of the pointee | |
| 5840 | // type, we change it to 0 here. If this causes an assertion trip because the | |
| 5841 | // pointee type needs to be resolved more, that needs to be done before calling | |
| 5842 | // this ptr() function. | |
| 5843 | if (info.flags.alignment != .none and | |
| 5844 | info.flags.alignment == Type.fromInterned(info.child).abiAlignment(mod)) | |
| 5845 | { | |
| 5846 | canon_info.flags.alignment = .none; | |
| 5847 | } | |
| 5848 | ||
| 5849 | switch (info.flags.vector_index) { | |
| 5850 | // Canonicalize host_size. If it matches the bit size of the pointee type, | |
| 5851 | // we change it to 0 here. If this causes an assertion trip, the pointee type | |
| 5852 | // needs to be resolved before calling this ptr() function. | |
| 5853 | .none => if (info.packed_offset.host_size != 0) { | |
| 5854 | const elem_bit_size = Type.fromInterned(info.child).bitSize(mod); | |
| 5855 | assert(info.packed_offset.bit_offset + elem_bit_size <= info.packed_offset.host_size * 8); | |
| 5856 | if (info.packed_offset.host_size * 8 == elem_bit_size) { | |
| 5857 | canon_info.packed_offset.host_size = 0; | |
| 5858 | } | |
| 5859 | }, | |
| 5860 | .runtime => {}, | |
| 5861 | _ => assert(@intFromEnum(info.flags.vector_index) < info.packed_offset.host_size), | |
| 5862 | } | |
| 5863 | ||
| 5864 | return Type.fromInterned((try intern(mod, .{ .ptr_type = canon_info }))); | |
| 5865 | } | |
| 5866 | ||
| 5867 | /// Like `ptrType`, but if `info` specifies an `alignment`, first ensures the pointer | |
| 5868 | /// child type's alignment is resolved so that an invalid alignment is not used. | |
| 5869 | /// In general, prefer this function during semantic analysis. | |
| 5870 | pub fn ptrTypeSema(zcu: *Zcu, info: InternPool.Key.PtrType) SemaError!Type { | |
| 5871 | if (info.flags.alignment != .none) { | |
| 5872 | _ = try Type.fromInterned(info.child).abiAlignmentAdvanced(zcu, .sema); | |
| 5873 | } | |
| 5874 | return zcu.ptrType(info); | |
| 5875 | } | |
| 5876 | ||
| 5877 | pub fn singleMutPtrType(mod: *Module, child_type: Type) Allocator.Error!Type { | |
| 5878 | return ptrType(mod, .{ .child = child_type.toIntern() }); | |
| 5879 | } | |
| 5880 | ||
| 5881 | pub fn singleConstPtrType(mod: *Module, child_type: Type) Allocator.Error!Type { | |
| 5882 | return ptrType(mod, .{ | |
| 5883 | .child = child_type.toIntern(), | |
| 5884 | .flags = .{ | |
| 5885 | .is_const = true, | |
| 5886 | }, | |
| 5887 | }); | |
| 5888 | } | |
| 5889 | ||
| 5890 | pub fn manyConstPtrType(mod: *Module, child_type: Type) Allocator.Error!Type { | |
| 5891 | return ptrType(mod, .{ | |
| 5892 | .child = child_type.toIntern(), | |
| 5893 | .flags = .{ | |
| 5894 | .size = .Many, | |
| 5895 | .is_const = true, | |
| 5896 | }, | |
| 5897 | }); | |
| 5898 | } | |
| 5899 | ||
| 5900 | pub fn adjustPtrTypeChild(mod: *Module, ptr_ty: Type, new_child: Type) Allocator.Error!Type { | |
| 5901 | var info = ptr_ty.ptrInfo(mod); | |
| 5902 | info.child = new_child.toIntern(); | |
| 5903 | return mod.ptrType(info); | |
| 5904 | } | |
| 5905 | ||
| 5906 | pub fn funcType(mod: *Module, key: InternPool.GetFuncTypeKey) Allocator.Error!Type { | |
| 5907 | return Type.fromInterned((try mod.intern_pool.getFuncType(mod.gpa, key))); | |
| 5908 | } | |
| 5909 | ||
| 5910 | /// Use this for `anyframe->T` only. | |
| 5911 | /// For `anyframe`, use the `InternPool.Index.anyframe` tag directly. | |
| 5912 | pub fn anyframeType(mod: *Module, payload_ty: Type) Allocator.Error!Type { | |
| 5913 | return Type.fromInterned((try intern(mod, .{ .anyframe_type = payload_ty.toIntern() }))); | |
| 5914 | } | |
| 5915 | ||
| 5916 | pub fn errorUnionType(mod: *Module, error_set_ty: Type, payload_ty: Type) Allocator.Error!Type { | |
| 5917 | return Type.fromInterned((try intern(mod, .{ .error_union_type = .{ | |
| 5918 | .error_set_type = error_set_ty.toIntern(), | |
| 5919 | .payload_type = payload_ty.toIntern(), | |
| 5920 | } }))); | |
| 5921 | } | |
| 5922 | ||
| 5923 | pub fn singleErrorSetType(mod: *Module, name: InternPool.NullTerminatedString) Allocator.Error!Type { | |
| 5924 | const names: *const [1]InternPool.NullTerminatedString = &name; | |
| 5925 | const new_ty = try mod.intern_pool.getErrorSetType(mod.gpa, names); | |
| 5926 | return Type.fromInterned(new_ty); | |
| 5927 | } | |
| 5928 | ||
| 5929 | /// Sorts `names` in place. | |
| 5930 | pub fn errorSetFromUnsortedNames( | |
| 5931 | mod: *Module, | |
| 5932 | names: []InternPool.NullTerminatedString, | |
| 5933 | ) Allocator.Error!Type { | |
| 5934 | std.mem.sort( | |
| 5935 | InternPool.NullTerminatedString, | |
| 5936 | names, | |
| 5937 | {}, | |
| 5938 | InternPool.NullTerminatedString.indexLessThan, | |
| 5939 | ); | |
| 5940 | const new_ty = try mod.intern_pool.getErrorSetType(mod.gpa, names); | |
| 5941 | return Type.fromInterned(new_ty); | |
| 5942 | } | |
| 5943 | ||
| 5944 | /// Supports only pointers, not pointer-like optionals. | |
| 5945 | pub fn ptrIntValue(mod: *Module, ty: Type, x: u64) Allocator.Error!Value { | |
| 5946 | assert(ty.zigTypeTag(mod) == .Pointer and !ty.isSlice(mod)); | |
| 5947 | assert(x != 0 or ty.isAllowzeroPtr(mod)); | |
| 5948 | const i = try intern(mod, .{ .ptr = .{ | |
| 5949 | .ty = ty.toIntern(), | |
| 5950 | .base_addr = .int, | |
| 5951 | .byte_offset = x, | |
| 5952 | } }); | |
| 5953 | return Value.fromInterned(i); | |
| 5954 | } | |
| 5955 | ||
| 5956 | /// Creates an enum tag value based on the integer tag value. | |
| 5957 | pub fn enumValue(mod: *Module, ty: Type, tag_int: InternPool.Index) Allocator.Error!Value { | |
| 5958 | if (std.debug.runtime_safety) { | |
| 5959 | const tag = ty.zigTypeTag(mod); | |
| 5960 | assert(tag == .Enum); | |
| 5961 | } | |
| 5962 | const i = try intern(mod, .{ .enum_tag = .{ | |
| 5963 | .ty = ty.toIntern(), | |
| 5964 | .int = tag_int, | |
| 5965 | } }); | |
| 5966 | return Value.fromInterned(i); | |
| 5967 | } | |
| 5968 | ||
| 5969 | /// Creates an enum tag value based on the field index according to source code | |
| 5970 | /// declaration order. | |
| 5971 | pub fn enumValueFieldIndex(mod: *Module, ty: Type, field_index: u32) Allocator.Error!Value { | |
| 5972 | const ip = &mod.intern_pool; | |
| 5973 | const gpa = mod.gpa; | |
| 5974 | const enum_type = ip.loadEnumType(ty.toIntern()); | |
| 5975 | ||
| 5976 | if (enum_type.values.len == 0) { | |
| 5977 | // Auto-numbered fields. | |
| 5978 | return Value.fromInterned((try ip.get(gpa, .{ .enum_tag = .{ | |
| 5979 | .ty = ty.toIntern(), | |
| 5980 | .int = try ip.get(gpa, .{ .int = .{ | |
| 5981 | .ty = enum_type.tag_ty, | |
| 5982 | .storage = .{ .u64 = field_index }, | |
| 5983 | } }), | |
| 5984 | } }))); | |
| 5985 | } | |
| 5986 | ||
| 5987 | return Value.fromInterned((try ip.get(gpa, .{ .enum_tag = .{ | |
| 5988 | .ty = ty.toIntern(), | |
| 5989 | .int = enum_type.values.get(ip)[field_index], | |
| 5990 | } }))); | |
| 5991 | } | |
| 5992 | ||
| 5993 | pub fn undefValue(mod: *Module, ty: Type) Allocator.Error!Value { | |
| 5994 | return Value.fromInterned((try mod.intern(.{ .undef = ty.toIntern() }))); | |
| 5995 | } | |
| 5996 | ||
| 5997 | pub fn undefRef(mod: *Module, ty: Type) Allocator.Error!Air.Inst.Ref { | |
| 5998 | return Air.internedToRef((try mod.undefValue(ty)).toIntern()); | |
| 5999 | } | |
| 6000 | ||
| 6001 | pub fn intValue(mod: *Module, ty: Type, x: anytype) Allocator.Error!Value { | |
| 6002 | if (std.math.cast(u64, x)) |casted| return intValue_u64(mod, ty, casted); | |
| 6003 | if (std.math.cast(i64, x)) |casted| return intValue_i64(mod, ty, casted); | |
| 6004 | var limbs_buffer: [4]usize = undefined; | |
| 6005 | var big_int = BigIntMutable.init(&limbs_buffer, x); | |
| 6006 | return intValue_big(mod, ty, big_int.toConst()); | |
| 6007 | } | |
| 6008 | ||
| 6009 | pub fn intRef(mod: *Module, ty: Type, x: anytype) Allocator.Error!Air.Inst.Ref { | |
| 6010 | return Air.internedToRef((try mod.intValue(ty, x)).toIntern()); | |
| 6011 | } | |
| 6012 | ||
| 6013 | pub fn intValue_big(mod: *Module, ty: Type, x: BigIntConst) Allocator.Error!Value { | |
| 6014 | const i = try intern(mod, .{ .int = .{ | |
| 6015 | .ty = ty.toIntern(), | |
| 6016 | .storage = .{ .big_int = x }, | |
| 6017 | } }); | |
| 6018 | return Value.fromInterned(i); | |
| 6019 | } | |
| 6020 | ||
| 6021 | pub fn intValue_u64(mod: *Module, ty: Type, x: u64) Allocator.Error!Value { | |
| 6022 | const i = try intern(mod, .{ .int = .{ | |
| 6023 | .ty = ty.toIntern(), | |
| 6024 | .storage = .{ .u64 = x }, | |
| 6025 | } }); | |
| 6026 | return Value.fromInterned(i); | |
| 6027 | } | |
| 6028 | ||
| 6029 | pub fn intValue_i64(mod: *Module, ty: Type, x: i64) Allocator.Error!Value { | |
| 6030 | const i = try intern(mod, .{ .int = .{ | |
| 6031 | .ty = ty.toIntern(), | |
| 6032 | .storage = .{ .i64 = x }, | |
| 6033 | } }); | |
| 6034 | return Value.fromInterned(i); | |
| 6035 | } | |
| 6036 | ||
| 6037 | pub fn unionValue(mod: *Module, union_ty: Type, tag: Value, val: Value) Allocator.Error!Value { | |
| 6038 | const i = try intern(mod, .{ .un = .{ | |
| 6039 | .ty = union_ty.toIntern(), | |
| 6040 | .tag = tag.toIntern(), | |
| 6041 | .val = val.toIntern(), | |
| 6042 | } }); | |
| 6043 | return Value.fromInterned(i); | |
| 6044 | } | |
| 6045 | ||
| 6046 | /// This function casts the float representation down to the representation of the type, potentially | |
| 6047 | /// losing data if the representation wasn't correct. | |
| 6048 | pub fn floatValue(mod: *Module, ty: Type, x: anytype) Allocator.Error!Value { | |
| 6049 | const storage: InternPool.Key.Float.Storage = switch (ty.floatBits(mod.getTarget())) { | |
| 6050 | 16 => .{ .f16 = @as(f16, @floatCast(x)) }, | |
| 6051 | 32 => .{ .f32 = @as(f32, @floatCast(x)) }, | |
| 6052 | 64 => .{ .f64 = @as(f64, @floatCast(x)) }, | |
| 6053 | 80 => .{ .f80 = @as(f80, @floatCast(x)) }, | |
| 6054 | 128 => .{ .f128 = @as(f128, @floatCast(x)) }, | |
| 6055 | else => unreachable, | |
| 6056 | }; | |
| 6057 | const i = try intern(mod, .{ .float = .{ | |
| 6058 | .ty = ty.toIntern(), | |
| 6059 | .storage = storage, | |
| 6060 | } }); | |
| 6061 | return Value.fromInterned(i); | |
| 6062 | } | |
| 6063 | ||
| 6064 | pub fn nullValue(mod: *Module, opt_ty: Type) Allocator.Error!Value { | |
| 6065 | const ip = &mod.intern_pool; | |
| 6066 | assert(ip.isOptionalType(opt_ty.toIntern())); | |
| 6067 | const result = try ip.get(mod.gpa, .{ .opt = .{ | |
| 6068 | .ty = opt_ty.toIntern(), | |
| 6069 | .val = .none, | |
| 6070 | } }); | |
| 6071 | return Value.fromInterned(result); | |
| 6072 | } | |
| 6073 | ||
| 6074 | pub fn smallestUnsignedInt(mod: *Module, max: u64) Allocator.Error!Type { | |
| 6075 | return intType(mod, .unsigned, Type.smallestUnsignedBits(max)); | |
| 6076 | } | |
| 6077 | ||
| 6078 | /// Returns the smallest possible integer type containing both `min` and | |
| 6079 | /// `max`. Asserts that neither value is undef. | |
| 6080 | /// TODO: if #3806 is implemented, this becomes trivial | |
| 6081 | pub fn intFittingRange(mod: *Module, min: Value, max: Value) !Type { | |
| 6082 | assert(!min.isUndef(mod)); | |
| 6083 | assert(!max.isUndef(mod)); | |
| 6084 | ||
| 6085 | if (std.debug.runtime_safety) { | |
| 6086 | assert(Value.order(min, max, mod).compare(.lte)); | |
| 6087 | } | |
| 6088 | ||
| 6089 | const sign = min.orderAgainstZero(mod) == .lt; | |
| 6090 | ||
| 6091 | const min_val_bits = intBitsForValue(mod, min, sign); | |
| 6092 | const max_val_bits = intBitsForValue(mod, max, sign); | |
| 6093 | ||
| 6094 | return mod.intType( | |
| 6095 | if (sign) .signed else .unsigned, | |
| 6096 | @max(min_val_bits, max_val_bits), | |
| 6097 | ); | |
| 6098 | } | |
| 6099 | ||
| 6100 | /// Given a value representing an integer, returns the number of bits necessary to represent | |
| 6101 | /// this value in an integer. If `sign` is true, returns the number of bits necessary in a | |
| 6102 | /// twos-complement integer; otherwise in an unsigned integer. | |
| 6103 | /// Asserts that `val` is not undef. If `val` is negative, asserts that `sign` is true. | |
| 6104 | pub fn intBitsForValue(mod: *Module, val: Value, sign: bool) u16 { | |
| 6105 | assert(!val.isUndef(mod)); | |
| 6106 | ||
| 6107 | const key = mod.intern_pool.indexToKey(val.toIntern()); | |
| 6108 | switch (key.int.storage) { | |
| 6109 | .i64 => |x| { | |
| 6110 | if (std.math.cast(u64, x)) |casted| return Type.smallestUnsignedBits(casted) + @intFromBool(sign); | |
| 6111 | assert(sign); | |
| 6112 | // Protect against overflow in the following negation. | |
| 6113 | if (x == std.math.minInt(i64)) return 64; | |
| 6114 | return Type.smallestUnsignedBits(@as(u64, @intCast(-(x + 1)))) + 1; | |
| 6115 | }, | |
| 6116 | .u64 => |x| { | |
| 6117 | return Type.smallestUnsignedBits(x) + @intFromBool(sign); | |
| 6118 | }, | |
| 6119 | .big_int => |big| { | |
| 6120 | if (big.positive) return @as(u16, @intCast(big.bitCountAbs() + @intFromBool(sign))); | |
| 6121 | ||
| 6122 | // Zero is still a possibility, in which case unsigned is fine | |
| 6123 | if (big.eqlZero()) return 0; | |
| 6124 | ||
| 6125 | return @as(u16, @intCast(big.bitCountTwosComp())); | |
| 6126 | }, | |
| 6127 | .lazy_align => |lazy_ty| { | |
| 6128 | return Type.smallestUnsignedBits(Type.fromInterned(lazy_ty).abiAlignment(mod).toByteUnits() orelse 0) + @intFromBool(sign); | |
| 6129 | }, | |
| 6130 | .lazy_size => |lazy_ty| { | |
| 6131 | return Type.smallestUnsignedBits(Type.fromInterned(lazy_ty).abiSize(mod)) + @intFromBool(sign); | |
| 6132 | }, | |
| 6133 | } | |
| 6134 | } | |
| 6135 | ||
| 6136 | 4257 | pub const AtomicPtrAlignmentError = error{ |
| 6137 | 4258 | FloatTooBig, |
| 6138 | 4259 | IntTooBig, |
| ... | ... | @@ -6371,101 +4492,6 @@ pub const UnionLayout = struct { |
| 6371 | 4492 | padding: u32, |
| 6372 | 4493 | }; |
| 6373 | 4494 | |
| 6374 | pub fn getUnionLayout(mod: *Module, loaded_union: InternPool.LoadedUnionType) UnionLayout { | |
| 6375 | const ip = &mod.intern_pool; | |
| 6376 | assert(loaded_union.haveLayout(ip)); | |
| 6377 | var most_aligned_field: u32 = undefined; | |
| 6378 | var most_aligned_field_size: u64 = undefined; | |
| 6379 | var biggest_field: u32 = undefined; | |
| 6380 | var payload_size: u64 = 0; | |
| 6381 | var payload_align: Alignment = .@"1"; | |
| 6382 | for (loaded_union.field_types.get(ip), 0..) |field_ty, field_index| { | |
| 6383 | if (!Type.fromInterned(field_ty).hasRuntimeBitsIgnoreComptime(mod)) continue; | |
| 6384 | ||
| 6385 | const explicit_align = loaded_union.fieldAlign(ip, field_index); | |
| 6386 | const field_align = if (explicit_align != .none) | |
| 6387 | explicit_align | |
| 6388 | else | |
| 6389 | Type.fromInterned(field_ty).abiAlignment(mod); | |
| 6390 | const field_size = Type.fromInterned(field_ty).abiSize(mod); | |
| 6391 | if (field_size > payload_size) { | |
| 6392 | payload_size = field_size; | |
| 6393 | biggest_field = @intCast(field_index); | |
| 6394 | } | |
| 6395 | if (field_align.compare(.gte, payload_align)) { | |
| 6396 | payload_align = field_align; | |
| 6397 | most_aligned_field = @intCast(field_index); | |
| 6398 | most_aligned_field_size = field_size; | |
| 6399 | } | |
| 6400 | } | |
| 6401 | const have_tag = loaded_union.flagsPtr(ip).runtime_tag.hasTag(); | |
| 6402 | if (!have_tag or !Type.fromInterned(loaded_union.enum_tag_ty).hasRuntimeBits(mod)) { | |
| 6403 | return .{ | |
| 6404 | .abi_size = payload_align.forward(payload_size), | |
| 6405 | .abi_align = payload_align, | |
| 6406 | .most_aligned_field = most_aligned_field, | |
| 6407 | .most_aligned_field_size = most_aligned_field_size, | |
| 6408 | .biggest_field = biggest_field, | |
| 6409 | .payload_size = payload_size, | |
| 6410 | .payload_align = payload_align, | |
| 6411 | .tag_align = .none, | |
| 6412 | .tag_size = 0, | |
| 6413 | .padding = 0, | |
| 6414 | }; | |
| 6415 | } | |
| 6416 | ||
| 6417 | const tag_size = Type.fromInterned(loaded_union.enum_tag_ty).abiSize(mod); | |
| 6418 | const tag_align = Type.fromInterned(loaded_union.enum_tag_ty).abiAlignment(mod).max(.@"1"); | |
| 6419 | return .{ | |
| 6420 | .abi_size = loaded_union.size(ip).*, | |
| 6421 | .abi_align = tag_align.max(payload_align), | |
| 6422 | .most_aligned_field = most_aligned_field, | |
| 6423 | .most_aligned_field_size = most_aligned_field_size, | |
| 6424 | .biggest_field = biggest_field, | |
| 6425 | .payload_size = payload_size, | |
| 6426 | .payload_align = payload_align, | |
| 6427 | .tag_align = tag_align, | |
| 6428 | .tag_size = tag_size, | |
| 6429 | .padding = loaded_union.padding(ip).*, | |
| 6430 | }; | |
| 6431 | } | |
| 6432 | ||
| 6433 | pub fn unionAbiSize(mod: *Module, loaded_union: InternPool.LoadedUnionType) u64 { | |
| 6434 | return mod.getUnionLayout(loaded_union).abi_size; | |
| 6435 | } | |
| 6436 | ||
| 6437 | /// Returns 0 if the union is represented with 0 bits at runtime. | |
| 6438 | pub fn unionAbiAlignment(mod: *Module, loaded_union: InternPool.LoadedUnionType) Alignment { | |
| 6439 | const ip = &mod.intern_pool; | |
| 6440 | const have_tag = loaded_union.flagsPtr(ip).runtime_tag.hasTag(); | |
| 6441 | var max_align: Alignment = .none; | |
| 6442 | if (have_tag) max_align = Type.fromInterned(loaded_union.enum_tag_ty).abiAlignment(mod); | |
| 6443 | for (loaded_union.field_types.get(ip), 0..) |field_ty, field_index| { | |
| 6444 | if (!Type.fromInterned(field_ty).hasRuntimeBits(mod)) continue; | |
| 6445 | ||
| 6446 | const field_align = mod.unionFieldNormalAlignment(loaded_union, @intCast(field_index)); | |
| 6447 | max_align = max_align.max(field_align); | |
| 6448 | } | |
| 6449 | return max_align; | |
| 6450 | } | |
| 6451 | ||
| 6452 | /// Returns the field alignment of a non-packed union. Asserts the layout is not packed. | |
| 6453 | pub fn unionFieldNormalAlignment(zcu: *Zcu, loaded_union: InternPool.LoadedUnionType, field_index: u32) Alignment { | |
| 6454 | return zcu.unionFieldNormalAlignmentAdvanced(loaded_union, field_index, .normal) catch unreachable; | |
| 6455 | } | |
| 6456 | ||
| 6457 | /// Returns the field alignment of a non-packed union. Asserts the layout is not packed. | |
| 6458 | /// If `strat` is `.sema`, may perform type resolution. | |
| 6459 | pub fn unionFieldNormalAlignmentAdvanced(zcu: *Zcu, loaded_union: InternPool.LoadedUnionType, field_index: u32, strat: Type.ResolveStrat) SemaError!Alignment { | |
| 6460 | const ip = &zcu.intern_pool; | |
| 6461 | assert(loaded_union.flagsPtr(ip).layout != .@"packed"); | |
| 6462 | const field_align = loaded_union.fieldAlign(ip, field_index); | |
| 6463 | if (field_align != .none) return field_align; | |
| 6464 | const field_ty = Type.fromInterned(loaded_union.field_types.get(ip)[field_index]); | |
| 6465 | if (field_ty.isNoReturn(zcu)) return .none; | |
| 6466 | return (try field_ty.abiAlignmentAdvanced(zcu, strat.toLazy())).scalar; | |
| 6467 | } | |
| 6468 | ||
| 6469 | 4495 | /// Returns the index of the active field, given the current tag value |
| 6470 | 4496 | pub fn unionTagFieldIndex(mod: *Module, loaded_union: InternPool.LoadedUnionType, enum_tag: Value) ?u32 { |
| 6471 | 4497 | const ip = &mod.intern_pool; |
| ... | ... | @@ -6474,63 +4500,6 @@ pub fn unionTagFieldIndex(mod: *Module, loaded_union: InternPool.LoadedUnionType |
| 6474 | 4500 | return loaded_union.loadTagType(ip).tagValueIndex(ip, enum_tag.toIntern()); |
| 6475 | 4501 | } |
| 6476 | 4502 | |
| 6477 | /// Returns the field alignment of a non-packed struct. Asserts the layout is not packed. | |
| 6478 | pub fn structFieldAlignment( | |
| 6479 | zcu: *Zcu, | |
| 6480 | explicit_alignment: InternPool.Alignment, | |
| 6481 | field_ty: Type, | |
| 6482 | layout: std.builtin.Type.ContainerLayout, | |
| 6483 | ) Alignment { | |
| 6484 | return zcu.structFieldAlignmentAdvanced(explicit_alignment, field_ty, layout, .normal) catch unreachable; | |
| 6485 | } | |
| 6486 | ||
| 6487 | /// Returns the field alignment of a non-packed struct. Asserts the layout is not packed. | |
| 6488 | /// If `strat` is `.sema`, may perform type resolution. | |
| 6489 | pub fn structFieldAlignmentAdvanced( | |
| 6490 | zcu: *Zcu, | |
| 6491 | explicit_alignment: InternPool.Alignment, | |
| 6492 | field_ty: Type, | |
| 6493 | layout: std.builtin.Type.ContainerLayout, | |
| 6494 | strat: Type.ResolveStrat, | |
| 6495 | ) SemaError!Alignment { | |
| 6496 | assert(layout != .@"packed"); | |
| 6497 | if (explicit_alignment != .none) return explicit_alignment; | |
| 6498 | const ty_abi_align = (try field_ty.abiAlignmentAdvanced(zcu, strat.toLazy())).scalar; | |
| 6499 | switch (layout) { | |
| 6500 | .@"packed" => unreachable, | |
| 6501 | .auto => if (zcu.getTarget().ofmt != .c) return ty_abi_align, | |
| 6502 | .@"extern" => {}, | |
| 6503 | } | |
| 6504 | // extern | |
| 6505 | if (field_ty.isAbiInt(zcu) and field_ty.intInfo(zcu).bits >= 128) { | |
| 6506 | return ty_abi_align.maxStrict(.@"16"); | |
| 6507 | } | |
| 6508 | return ty_abi_align; | |
| 6509 | } | |
| 6510 | ||
| 6511 | /// https://github.com/ziglang/zig/issues/17178 explored storing these bit offsets | |
| 6512 | /// into the packed struct InternPool data rather than computing this on the | |
| 6513 | /// fly, however it was found to perform worse when measured on real world | |
| 6514 | /// projects. | |
| 6515 | pub fn structPackedFieldBitOffset( | |
| 6516 | mod: *Module, | |
| 6517 | struct_type: InternPool.LoadedStructType, | |
| 6518 | field_index: u32, | |
| 6519 | ) u16 { | |
| 6520 | const ip = &mod.intern_pool; | |
| 6521 | assert(struct_type.layout == .@"packed"); | |
| 6522 | assert(struct_type.haveLayout(ip)); | |
| 6523 | var bit_sum: u64 = 0; | |
| 6524 | for (0..struct_type.field_types.len) |i| { | |
| 6525 | if (i == field_index) { | |
| 6526 | return @intCast(bit_sum); | |
| 6527 | } | |
| 6528 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[i]); | |
| 6529 | bit_sum += field_ty.bitSize(mod); | |
| 6530 | } | |
| 6531 | unreachable; // index out of bounds | |
| 6532 | } | |
| 6533 | ||
| 6534 | 4503 | pub const ResolvedReference = struct { |
| 6535 | 4504 | referencer: AnalUnit, |
| 6536 | 4505 | src: LazySrcLoc, |
| ... | ... | @@ -6564,33 +4533,6 @@ pub fn resolveReferences(zcu: *Zcu) !std.AutoHashMapUnmanaged(AnalUnit, Resolved |
| 6564 | 4533 | return result; |
| 6565 | 4534 | } |
| 6566 | 4535 | |
| 6567 | pub fn getBuiltin(zcu: *Zcu, name: []const u8) Allocator.Error!Air.Inst.Ref { | |
| 6568 | const decl_index = try zcu.getBuiltinDecl(name); | |
| 6569 | zcu.ensureDeclAnalyzed(decl_index) catch @panic("std.builtin is corrupt"); | |
| 6570 | return Air.internedToRef(zcu.declPtr(decl_index).val.toIntern()); | |
| 6571 | } | |
| 6572 | ||
| 6573 | pub fn getBuiltinDecl(zcu: *Zcu, name: []const u8) Allocator.Error!InternPool.DeclIndex { | |
| 6574 | const gpa = zcu.gpa; | |
| 6575 | const ip = &zcu.intern_pool; | |
| 6576 | const std_file_imported = zcu.importPkg(zcu.std_mod) catch @panic("failed to import lib/std.zig"); | |
| 6577 | const std_file_root_decl = zcu.fileRootDecl(std_file_imported.file_index).unwrap().?; | |
| 6578 | const std_namespace = zcu.declPtr(std_file_root_decl).getOwnedInnerNamespace(zcu).?; | |
| 6579 | const builtin_str = try ip.getOrPutString(gpa, "builtin", .no_embedded_nulls); | |
| 6580 | const builtin_decl = std_namespace.decls.getKeyAdapted(builtin_str, Zcu.DeclAdapter{ .zcu = zcu }) orelse @panic("lib/std.zig is corrupt and missing 'builtin'"); | |
| 6581 | zcu.ensureDeclAnalyzed(builtin_decl) catch @panic("std.builtin is corrupt"); | |
| 6582 | const builtin_namespace = zcu.declPtr(builtin_decl).getInnerNamespace(zcu) orelse @panic("std.builtin is corrupt"); | |
| 6583 | const name_str = try ip.getOrPutString(gpa, name, .no_embedded_nulls); | |
| 6584 | return builtin_namespace.decls.getKeyAdapted(name_str, Zcu.DeclAdapter{ .zcu = zcu }) orelse @panic("lib/std/builtin.zig is corrupt"); | |
| 6585 | } | |
| 6586 | ||
| 6587 | pub fn getBuiltinType(zcu: *Zcu, name: []const u8) Allocator.Error!Type { | |
| 6588 | const ty_inst = try zcu.getBuiltin(name); | |
| 6589 | const ty = Type.fromInterned(ty_inst.toInterned() orelse @panic("std.builtin is corrupt")); | |
| 6590 | ty.resolveFully(zcu) catch @panic("std.builtin is corrupt"); | |
| 6591 | return ty; | |
| 6592 | } | |
| 6593 | ||
| 6594 | 4536 | pub fn fileByIndex(zcu: *const Zcu, i: File.Index) *File { |
| 6595 | 4537 | return zcu.import_table.values()[@intFromEnum(i)]; |
| 6596 | 4538 | } |
src/Zcu/PerThread.zig created+2102| ... | ... | @@ -0,0 +1,2102 @@ |
| 1 | zcu: *Zcu, | |
| 2 | ||
| 3 | /// Dense, per-thread unique index. | |
| 4 | tid: Id, | |
| 5 | ||
| 6 | pub const Id = if (builtin.single_threaded) enum { main } else enum(usize) { main, _ }; | |
| 7 | ||
| 8 | /// Like `ensureDeclAnalyzed`, but the Decl is a file's root Decl. | |
| 9 | pub fn ensureFileAnalyzed(pt: Zcu.PerThread, file_index: Zcu.File.Index) Zcu.SemaError!void { | |
| 10 | if (pt.zcu.fileRootDecl(file_index).unwrap()) |existing_root| { | |
| 11 | return pt.ensureDeclAnalyzed(existing_root); | |
| 12 | } else { | |
| 13 | return pt.semaFile(file_index); | |
| 14 | } | |
| 15 | } | |
| 16 | ||
| 17 | /// This ensures that the Decl will have an up-to-date Type and Value populated. | |
| 18 | /// However the resolution status of the Type may not be fully resolved. | |
| 19 | /// For example an inferred error set is not resolved until after `analyzeFnBody`. | |
| 20 | /// is called. | |
| 21 | pub fn ensureDeclAnalyzed(pt: Zcu.PerThread, decl_index: Zcu.Decl.Index) Zcu.SemaError!void { | |
| 22 | const tracy = trace(@src()); | |
| 23 | defer tracy.end(); | |
| 24 | ||
| 25 | const mod = pt.zcu; | |
| 26 | const ip = &mod.intern_pool; | |
| 27 | const decl = mod.declPtr(decl_index); | |
| 28 | ||
| 29 | log.debug("ensureDeclAnalyzed '{d}' (name '{}')", .{ | |
| 30 | @intFromEnum(decl_index), | |
| 31 | decl.name.fmt(ip), | |
| 32 | }); | |
| 33 | ||
| 34 | // Determine whether or not this Decl is outdated, i.e. requires re-analysis | |
| 35 | // even if `complete`. If a Decl is PO, we pessismistically assume that it | |
| 36 | // *does* require re-analysis, to ensure that the Decl is definitely | |
| 37 | // up-to-date when this function returns. | |
| 38 | ||
| 39 | // If analysis occurs in a poor order, this could result in over-analysis. | |
| 40 | // We do our best to avoid this by the other dependency logic in this file | |
| 41 | // which tries to limit re-analysis to Decls whose previously listed | |
| 42 | // dependencies are all up-to-date. | |
| 43 | ||
| 44 | const decl_as_depender = InternPool.AnalUnit.wrap(.{ .decl = decl_index }); | |
| 45 | const decl_was_outdated = mod.outdated.swapRemove(decl_as_depender) or | |
| 46 | mod.potentially_outdated.swapRemove(decl_as_depender); | |
| 47 | ||
| 48 | if (decl_was_outdated) { | |
| 49 | _ = mod.outdated_ready.swapRemove(decl_as_depender); | |
| 50 | } | |
| 51 | ||
| 52 | const was_outdated = mod.outdated_file_root.swapRemove(decl_index) or decl_was_outdated; | |
| 53 | ||
| 54 | switch (decl.analysis) { | |
| 55 | .in_progress => unreachable, | |
| 56 | ||
| 57 | .file_failure => return error.AnalysisFail, | |
| 58 | ||
| 59 | .sema_failure, | |
| 60 | .dependency_failure, | |
| 61 | .codegen_failure, | |
| 62 | => if (!was_outdated) return error.AnalysisFail, | |
| 63 | ||
| 64 | .complete => if (!was_outdated) return, | |
| 65 | ||
| 66 | .unreferenced => {}, | |
| 67 | } | |
| 68 | ||
| 69 | if (was_outdated) { | |
| 70 | // The exports this Decl performs will be re-discovered, so we remove them here | |
| 71 | // prior to re-analysis. | |
| 72 | if (build_options.only_c) unreachable; | |
| 73 | mod.deleteUnitExports(decl_as_depender); | |
| 74 | mod.deleteUnitReferences(decl_as_depender); | |
| 75 | } | |
| 76 | ||
| 77 | const sema_result: Zcu.SemaDeclResult = blk: { | |
| 78 | if (decl.zir_decl_index == .none and !mod.declIsRoot(decl_index)) { | |
| 79 | // Anonymous decl. We don't semantically analyze these. | |
| 80 | break :blk .{ | |
| 81 | .invalidate_decl_val = false, | |
| 82 | .invalidate_decl_ref = false, | |
| 83 | }; | |
| 84 | } | |
| 85 | ||
| 86 | if (mod.declIsRoot(decl_index)) { | |
| 87 | const changed = try pt.semaFileUpdate(decl.getFileScopeIndex(mod), decl_was_outdated); | |
| 88 | break :blk .{ | |
| 89 | .invalidate_decl_val = changed, | |
| 90 | .invalidate_decl_ref = changed, | |
| 91 | }; | |
| 92 | } | |
| 93 | ||
| 94 | const decl_prog_node = mod.sema_prog_node.start((try decl.fullyQualifiedName(mod)).toSlice(ip), 0); | |
| 95 | defer decl_prog_node.end(); | |
| 96 | ||
| 97 | break :blk pt.semaDecl(decl_index) catch |err| switch (err) { | |
| 98 | error.AnalysisFail => { | |
| 99 | if (decl.analysis == .in_progress) { | |
| 100 | // If this decl caused the compile error, the analysis field would | |
| 101 | // be changed to indicate it was this Decl's fault. Because this | |
| 102 | // did not happen, we infer here that it was a dependency failure. | |
| 103 | decl.analysis = .dependency_failure; | |
| 104 | } | |
| 105 | return error.AnalysisFail; | |
| 106 | }, | |
| 107 | error.GenericPoison => unreachable, | |
| 108 | else => |e| { | |
| 109 | decl.analysis = .sema_failure; | |
| 110 | try mod.failed_analysis.ensureUnusedCapacity(mod.gpa, 1); | |
| 111 | try mod.retryable_failures.append(mod.gpa, InternPool.AnalUnit.wrap(.{ .decl = decl_index })); | |
| 112 | mod.failed_analysis.putAssumeCapacityNoClobber(InternPool.AnalUnit.wrap(.{ .decl = decl_index }), try Zcu.ErrorMsg.create( | |
| 113 | mod.gpa, | |
| 114 | decl.navSrcLoc(mod), | |
| 115 | "unable to analyze: {s}", | |
| 116 | .{@errorName(e)}, | |
| 117 | )); | |
| 118 | return error.AnalysisFail; | |
| 119 | }, | |
| 120 | }; | |
| 121 | }; | |
| 122 | ||
| 123 | // TODO: we do not yet have separate dependencies for decl values vs types. | |
| 124 | if (decl_was_outdated) { | |
| 125 | if (sema_result.invalidate_decl_val or sema_result.invalidate_decl_ref) { | |
| 126 | log.debug("Decl tv invalidated ('{d}')", .{@intFromEnum(decl_index)}); | |
| 127 | // This dependency was marked as PO, meaning dependees were waiting | |
| 128 | // on its analysis result, and it has turned out to be outdated. | |
| 129 | // Update dependees accordingly. | |
| 130 | try mod.markDependeeOutdated(.{ .decl_val = decl_index }); | |
| 131 | } else { | |
| 132 | log.debug("Decl tv up-to-date ('{d}')", .{@intFromEnum(decl_index)}); | |
| 133 | // This dependency was previously PO, but turned out to be up-to-date. | |
| 134 | // We do not need to queue successive analysis. | |
| 135 | try mod.markPoDependeeUpToDate(.{ .decl_val = decl_index }); | |
| 136 | } | |
| 137 | } | |
| 138 | } | |
| 139 | ||
| 140 | pub fn ensureFuncBodyAnalyzed(pt: Zcu.PerThread, maybe_coerced_func_index: InternPool.Index) Zcu.SemaError!void { | |
| 141 | const tracy = trace(@src()); | |
| 142 | defer tracy.end(); | |
| 143 | ||
| 144 | const zcu = pt.zcu; | |
| 145 | const gpa = zcu.gpa; | |
| 146 | const ip = &zcu.intern_pool; | |
| 147 | ||
| 148 | // We only care about the uncoerced function. | |
| 149 | // We need to do this for the "orphaned function" check below to be valid. | |
| 150 | const func_index = ip.unwrapCoercedFunc(maybe_coerced_func_index); | |
| 151 | ||
| 152 | const func = zcu.funcInfo(maybe_coerced_func_index); | |
| 153 | const decl_index = func.owner_decl; | |
| 154 | const decl = zcu.declPtr(decl_index); | |
| 155 | ||
| 156 | log.debug("ensureFuncBodyAnalyzed '{d}' (instance of '{}')", .{ | |
| 157 | @intFromEnum(func_index), | |
| 158 | decl.name.fmt(ip), | |
| 159 | }); | |
| 160 | ||
| 161 | // First, our owner decl must be up-to-date. This will always be the case | |
| 162 | // during the first update, but may not on successive updates if we happen | |
| 163 | // to get analyzed before our parent decl. | |
| 164 | try pt.ensureDeclAnalyzed(decl_index); | |
| 165 | ||
| 166 | // On an update, it's possible this function changed such that our owner | |
| 167 | // decl now refers to a different function, making this one orphaned. If | |
| 168 | // that's the case, we should remove this function from the binary. | |
| 169 | if (decl.val.ip_index != func_index) { | |
| 170 | try zcu.markDependeeOutdated(.{ .func_ies = func_index }); | |
| 171 | ip.removeDependenciesForDepender(gpa, InternPool.AnalUnit.wrap(.{ .func = func_index })); | |
| 172 | ip.remove(func_index); | |
| 173 | @panic("TODO: remove orphaned function from binary"); | |
| 174 | } | |
| 175 | ||
| 176 | // We'll want to remember what the IES used to be before the update for | |
| 177 | // dependency invalidation purposes. | |
| 178 | const old_resolved_ies = if (func.analysis(ip).inferred_error_set) | |
| 179 | func.resolvedErrorSet(ip).* | |
| 180 | else | |
| 181 | .none; | |
| 182 | ||
| 183 | switch (decl.analysis) { | |
| 184 | .unreferenced => unreachable, | |
| 185 | .in_progress => unreachable, | |
| 186 | ||
| 187 | .codegen_failure => unreachable, // functions do not perform constant value generation | |
| 188 | ||
| 189 | .file_failure, | |
| 190 | .sema_failure, | |
| 191 | .dependency_failure, | |
| 192 | => return error.AnalysisFail, | |
| 193 | ||
| 194 | .complete => {}, | |
| 195 | } | |
| 196 | ||
| 197 | const func_as_depender = InternPool.AnalUnit.wrap(.{ .func = func_index }); | |
| 198 | const was_outdated = zcu.outdated.swapRemove(func_as_depender) or | |
| 199 | zcu.potentially_outdated.swapRemove(func_as_depender); | |
| 200 | ||
| 201 | if (was_outdated) { | |
| 202 | if (build_options.only_c) unreachable; | |
| 203 | _ = zcu.outdated_ready.swapRemove(func_as_depender); | |
| 204 | zcu.deleteUnitExports(func_as_depender); | |
| 205 | zcu.deleteUnitReferences(func_as_depender); | |
| 206 | } | |
| 207 | ||
| 208 | switch (func.analysis(ip).state) { | |
| 209 | .success => if (!was_outdated) return, | |
| 210 | .sema_failure, | |
| 211 | .dependency_failure, | |
| 212 | .codegen_failure, | |
| 213 | => if (!was_outdated) return error.AnalysisFail, | |
| 214 | .none, .queued => {}, | |
| 215 | .in_progress => unreachable, | |
| 216 | .inline_only => unreachable, // don't queue work for this | |
| 217 | } | |
| 218 | ||
| 219 | log.debug("analyze and generate fn body '{d}'; reason='{s}'", .{ | |
| 220 | @intFromEnum(func_index), | |
| 221 | if (was_outdated) "outdated" else "never analyzed", | |
| 222 | }); | |
| 223 | ||
| 224 | var tmp_arena = std.heap.ArenaAllocator.init(gpa); | |
| 225 | defer tmp_arena.deinit(); | |
| 226 | const sema_arena = tmp_arena.allocator(); | |
| 227 | ||
| 228 | var air = pt.analyzeFnBody(func_index, sema_arena) catch |err| switch (err) { | |
| 229 | error.AnalysisFail => { | |
| 230 | if (func.analysis(ip).state == .in_progress) { | |
| 231 | // If this decl caused the compile error, the analysis field would | |
| 232 | // be changed to indicate it was this Decl's fault. Because this | |
| 233 | // did not happen, we infer here that it was a dependency failure. | |
| 234 | func.analysis(ip).state = .dependency_failure; | |
| 235 | } | |
| 236 | return error.AnalysisFail; | |
| 237 | }, | |
| 238 | error.OutOfMemory => return error.OutOfMemory, | |
| 239 | }; | |
| 240 | errdefer air.deinit(gpa); | |
| 241 | ||
| 242 | const invalidate_ies_deps = i: { | |
| 243 | if (!was_outdated) break :i false; | |
| 244 | if (!func.analysis(ip).inferred_error_set) break :i true; | |
| 245 | const new_resolved_ies = func.resolvedErrorSet(ip).*; | |
| 246 | break :i new_resolved_ies != old_resolved_ies; | |
| 247 | }; | |
| 248 | if (invalidate_ies_deps) { | |
| 249 | log.debug("func IES invalidated ('{d}')", .{@intFromEnum(func_index)}); | |
| 250 | try zcu.markDependeeOutdated(.{ .func_ies = func_index }); | |
| 251 | } else if (was_outdated) { | |
| 252 | log.debug("func IES up-to-date ('{d}')", .{@intFromEnum(func_index)}); | |
| 253 | try zcu.markPoDependeeUpToDate(.{ .func_ies = func_index }); | |
| 254 | } | |
| 255 | ||
| 256 | const comp = zcu.comp; | |
| 257 | ||
| 258 | const dump_air = build_options.enable_debug_extensions and comp.verbose_air; | |
| 259 | const dump_llvm_ir = build_options.enable_debug_extensions and (comp.verbose_llvm_ir != null or comp.verbose_llvm_bc != null); | |
| 260 | ||
| 261 | if (comp.bin_file == null and zcu.llvm_object == null and !dump_air and !dump_llvm_ir) { | |
| 262 | air.deinit(gpa); | |
| 263 | return; | |
| 264 | } | |
| 265 | ||
| 266 | try comp.work_queue.writeItem(.{ .codegen_func = .{ | |
| 267 | .func = func_index, | |
| 268 | .air = air, | |
| 269 | } }); | |
| 270 | } | |
| 271 | ||
| 272 | /// Takes ownership of `air`, even on error. | |
| 273 | /// If any types referenced by `air` are unresolved, marks the codegen as failed. | |
| 274 | pub fn linkerUpdateFunc(pt: Zcu.PerThread, func_index: InternPool.Index, air: Air) Allocator.Error!void { | |
| 275 | const zcu = pt.zcu; | |
| 276 | const gpa = zcu.gpa; | |
| 277 | const ip = &zcu.intern_pool; | |
| 278 | const comp = zcu.comp; | |
| 279 | ||
| 280 | defer { | |
| 281 | var air_mut = air; | |
| 282 | air_mut.deinit(gpa); | |
| 283 | } | |
| 284 | ||
| 285 | const func = zcu.funcInfo(func_index); | |
| 286 | const decl_index = func.owner_decl; | |
| 287 | const decl = zcu.declPtr(decl_index); | |
| 288 | ||
| 289 | var liveness = try Liveness.analyze(gpa, air, ip); | |
| 290 | defer liveness.deinit(gpa); | |
| 291 | ||
| 292 | if (build_options.enable_debug_extensions and comp.verbose_air) { | |
| 293 | const fqn = try decl.fullyQualifiedName(zcu); | |
| 294 | std.debug.print("# Begin Function AIR: {}:\n", .{fqn.fmt(ip)}); | |
| 295 | @import("../print_air.zig").dump(pt, air, liveness); | |
| 296 | std.debug.print("# End Function AIR: {}\n\n", .{fqn.fmt(ip)}); | |
| 297 | } | |
| 298 | ||
| 299 | if (std.debug.runtime_safety) { | |
| 300 | var verify: Liveness.Verify = .{ | |
| 301 | .gpa = gpa, | |
| 302 | .air = air, | |
| 303 | .liveness = liveness, | |
| 304 | .intern_pool = ip, | |
| 305 | }; | |
| 306 | defer verify.deinit(); | |
| 307 | ||
| 308 | verify.verify() catch |err| switch (err) { | |
| 309 | error.OutOfMemory => return error.OutOfMemory, | |
| 310 | else => { | |
| 311 | try zcu.failed_analysis.ensureUnusedCapacity(gpa, 1); | |
| 312 | zcu.failed_analysis.putAssumeCapacityNoClobber( | |
| 313 | InternPool.AnalUnit.wrap(.{ .func = func_index }), | |
| 314 | try Zcu.ErrorMsg.create( | |
| 315 | gpa, | |
| 316 | decl.navSrcLoc(zcu), | |
| 317 | "invalid liveness: {s}", | |
| 318 | .{@errorName(err)}, | |
| 319 | ), | |
| 320 | ); | |
| 321 | func.analysis(ip).state = .codegen_failure; | |
| 322 | return; | |
| 323 | }, | |
| 324 | }; | |
| 325 | } | |
| 326 | ||
| 327 | const codegen_prog_node = zcu.codegen_prog_node.start((try decl.fullyQualifiedName(zcu)).toSlice(ip), 0); | |
| 328 | defer codegen_prog_node.end(); | |
| 329 | ||
| 330 | if (!air.typesFullyResolved(zcu)) { | |
| 331 | // A type we depend on failed to resolve. This is a transitive failure. | |
| 332 | // Correcting this failure will involve changing a type this function | |
| 333 | // depends on, hence triggering re-analysis of this function, so this | |
| 334 | // interacts correctly with incremental compilation. | |
| 335 | func.analysis(ip).state = .codegen_failure; | |
| 336 | } else if (comp.bin_file) |lf| { | |
| 337 | lf.updateFunc(pt, func_index, air, liveness) catch |err| switch (err) { | |
| 338 | error.OutOfMemory => return error.OutOfMemory, | |
| 339 | error.AnalysisFail => { | |
| 340 | func.analysis(ip).state = .codegen_failure; | |
| 341 | }, | |
| 342 | else => { | |
| 343 | try zcu.failed_analysis.ensureUnusedCapacity(gpa, 1); | |
| 344 | zcu.failed_analysis.putAssumeCapacityNoClobber(InternPool.AnalUnit.wrap(.{ .func = func_index }), try Zcu.ErrorMsg.create( | |
| 345 | gpa, | |
| 346 | decl.navSrcLoc(zcu), | |
| 347 | "unable to codegen: {s}", | |
| 348 | .{@errorName(err)}, | |
| 349 | )); | |
| 350 | func.analysis(ip).state = .codegen_failure; | |
| 351 | try zcu.retryable_failures.append(zcu.gpa, InternPool.AnalUnit.wrap(.{ .func = func_index })); | |
| 352 | }, | |
| 353 | }; | |
| 354 | } else if (zcu.llvm_object) |llvm_object| { | |
| 355 | if (build_options.only_c) unreachable; | |
| 356 | llvm_object.updateFunc(pt, func_index, air, liveness) catch |err| switch (err) { | |
| 357 | error.OutOfMemory => return error.OutOfMemory, | |
| 358 | }; | |
| 359 | } | |
| 360 | } | |
| 361 | ||
| 362 | /// https://github.com/ziglang/zig/issues/14307 | |
| 363 | pub fn semaPkg(pt: Zcu.PerThread, pkg: *Module) !void { | |
| 364 | const import_file_result = try pt.zcu.importPkg(pkg); | |
| 365 | const root_decl_index = pt.zcu.fileRootDecl(import_file_result.file_index); | |
| 366 | if (root_decl_index == .none) { | |
| 367 | return pt.semaFile(import_file_result.file_index); | |
| 368 | } | |
| 369 | } | |
| 370 | ||
| 371 | fn getFileRootStruct( | |
| 372 | pt: Zcu.PerThread, | |
| 373 | decl_index: Zcu.Decl.Index, | |
| 374 | namespace_index: Zcu.Namespace.Index, | |
| 375 | file_index: Zcu.File.Index, | |
| 376 | ) Allocator.Error!InternPool.Index { | |
| 377 | const zcu = pt.zcu; | |
| 378 | const gpa = zcu.gpa; | |
| 379 | const ip = &zcu.intern_pool; | |
| 380 | const file = zcu.fileByIndex(file_index); | |
| 381 | const extended = file.zir.instructions.items(.data)[@intFromEnum(Zir.Inst.Index.main_struct_inst)].extended; | |
| 382 | assert(extended.opcode == .struct_decl); | |
| 383 | const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small); | |
| 384 | assert(!small.has_captures_len); | |
| 385 | assert(!small.has_backing_int); | |
| 386 | assert(small.layout == .auto); | |
| 387 | var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len; | |
| 388 | const fields_len = if (small.has_fields_len) blk: { | |
| 389 | const fields_len = file.zir.extra[extra_index]; | |
| 390 | extra_index += 1; | |
| 391 | break :blk fields_len; | |
| 392 | } else 0; | |
| 393 | const decls_len = if (small.has_decls_len) blk: { | |
| 394 | const decls_len = file.zir.extra[extra_index]; | |
| 395 | extra_index += 1; | |
| 396 | break :blk decls_len; | |
| 397 | } else 0; | |
| 398 | const decls = file.zir.bodySlice(extra_index, decls_len); | |
| 399 | extra_index += decls_len; | |
| 400 | ||
| 401 | const tracked_inst = try ip.trackZir(gpa, file_index, .main_struct_inst); | |
| 402 | const wip_ty = switch (try ip.getStructType(gpa, pt.tid, .{ | |
| 403 | .layout = .auto, | |
| 404 | .fields_len = fields_len, | |
| 405 | .known_non_opv = small.known_non_opv, | |
| 406 | .requires_comptime = if (small.known_comptime_only) .yes else .unknown, | |
| 407 | .is_tuple = small.is_tuple, | |
| 408 | .any_comptime_fields = small.any_comptime_fields, | |
| 409 | .any_default_inits = small.any_default_inits, | |
| 410 | .inits_resolved = false, | |
| 411 | .any_aligned_fields = small.any_aligned_fields, | |
| 412 | .has_namespace = true, | |
| 413 | .key = .{ .declared = .{ | |
| 414 | .zir_index = tracked_inst, | |
| 415 | .captures = &.{}, | |
| 416 | } }, | |
| 417 | })) { | |
| 418 | .existing => unreachable, // we wouldn't be analysing the file root if this type existed | |
| 419 | .wip => |wip| wip, | |
| 420 | }; | |
| 421 | errdefer wip_ty.cancel(ip); | |
| 422 | ||
| 423 | if (zcu.comp.debug_incremental) { | |
| 424 | try ip.addDependency( | |
| 425 | gpa, | |
| 426 | InternPool.AnalUnit.wrap(.{ .decl = decl_index }), | |
| 427 | .{ .src_hash = tracked_inst }, | |
| 428 | ); | |
| 429 | } | |
| 430 | ||
| 431 | const decl = zcu.declPtr(decl_index); | |
| 432 | decl.val = Value.fromInterned(wip_ty.index); | |
| 433 | decl.has_tv = true; | |
| 434 | decl.owns_tv = true; | |
| 435 | decl.analysis = .complete; | |
| 436 | ||
| 437 | try zcu.scanNamespace(namespace_index, decls, decl); | |
| 438 | try zcu.comp.work_queue.writeItem(.{ .resolve_type_fully = wip_ty.index }); | |
| 439 | return wip_ty.finish(ip, decl_index, namespace_index.toOptional()); | |
| 440 | } | |
| 441 | ||
| 442 | /// Re-analyze the root Decl of a file on an incremental update. | |
| 443 | /// If `type_outdated`, the struct type itself is considered outdated and is | |
| 444 | /// reconstructed at a new InternPool index. Otherwise, the namespace is just | |
| 445 | /// re-analyzed. Returns whether the decl's tyval was invalidated. | |
| 446 | fn semaFileUpdate(pt: Zcu.PerThread, file_index: Zcu.File.Index, type_outdated: bool) Zcu.SemaError!bool { | |
| 447 | const zcu = pt.zcu; | |
| 448 | const ip = &zcu.intern_pool; | |
| 449 | const file = zcu.fileByIndex(file_index); | |
| 450 | const decl = zcu.declPtr(zcu.fileRootDecl(file_index).unwrap().?); | |
| 451 | ||
| 452 | log.debug("semaFileUpdate mod={s} sub_file_path={s} type_outdated={}", .{ | |
| 453 | file.mod.fully_qualified_name, | |
| 454 | file.sub_file_path, | |
| 455 | type_outdated, | |
| 456 | }); | |
| 457 | ||
| 458 | if (file.status != .success_zir) { | |
| 459 | if (decl.analysis == .file_failure) { | |
| 460 | return false; | |
| 461 | } else { | |
| 462 | decl.analysis = .file_failure; | |
| 463 | return true; | |
| 464 | } | |
| 465 | } | |
| 466 | ||
| 467 | if (decl.analysis == .file_failure) { | |
| 468 | // No struct type currently exists. Create one! | |
| 469 | const root_decl = zcu.fileRootDecl(file_index); | |
| 470 | _ = try pt.getFileRootStruct(root_decl.unwrap().?, decl.src_namespace, file_index); | |
| 471 | return true; | |
| 472 | } | |
| 473 | ||
| 474 | assert(decl.has_tv); | |
| 475 | assert(decl.owns_tv); | |
| 476 | ||
| 477 | if (type_outdated) { | |
| 478 | // Invalidate the existing type, reusing the decl and namespace. | |
| 479 | const file_root_decl = zcu.fileRootDecl(file_index).unwrap().?; | |
| 480 | ip.removeDependenciesForDepender(zcu.gpa, InternPool.AnalUnit.wrap(.{ | |
| 481 | .decl = file_root_decl, | |
| 482 | })); | |
| 483 | ip.remove(decl.val.toIntern()); | |
| 484 | decl.val = undefined; | |
| 485 | _ = try pt.getFileRootStruct(file_root_decl, decl.src_namespace, file_index); | |
| 486 | return true; | |
| 487 | } | |
| 488 | ||
| 489 | // Only the struct's namespace is outdated. | |
| 490 | // Preserve the type - just scan the namespace again. | |
| 491 | ||
| 492 | const extended = file.zir.instructions.items(.data)[@intFromEnum(Zir.Inst.Index.main_struct_inst)].extended; | |
| 493 | const small: Zir.Inst.StructDecl.Small = @bitCast(extended.small); | |
| 494 | ||
| 495 | var extra_index: usize = extended.operand + @typeInfo(Zir.Inst.StructDecl).Struct.fields.len; | |
| 496 | extra_index += @intFromBool(small.has_fields_len); | |
| 497 | const decls_len = if (small.has_decls_len) blk: { | |
| 498 | const decls_len = file.zir.extra[extra_index]; | |
| 499 | extra_index += 1; | |
| 500 | break :blk decls_len; | |
| 501 | } else 0; | |
| 502 | const decls = file.zir.bodySlice(extra_index, decls_len); | |
| 503 | ||
| 504 | if (!type_outdated) { | |
| 505 | try zcu.scanNamespace(decl.src_namespace, decls, decl); | |
| 506 | } | |
| 507 | ||
| 508 | return false; | |
| 509 | } | |
| 510 | ||
| 511 | /// Regardless of the file status, will create a `Decl` if none exists so that we can track | |
| 512 | /// dependencies and re-analyze when the file becomes outdated. | |
| 513 | fn semaFile(pt: Zcu.PerThread, file_index: Zcu.File.Index) Zcu.SemaError!void { | |
| 514 | const tracy = trace(@src()); | |
| 515 | defer tracy.end(); | |
| 516 | ||
| 517 | const zcu = pt.zcu; | |
| 518 | const gpa = zcu.gpa; | |
| 519 | const file = zcu.fileByIndex(file_index); | |
| 520 | assert(zcu.fileRootDecl(file_index) == .none); | |
| 521 | log.debug("semaFile zcu={s} sub_file_path={s}", .{ | |
| 522 | file.mod.fully_qualified_name, file.sub_file_path, | |
| 523 | }); | |
| 524 | ||
| 525 | // Because these three things each reference each other, `undefined` | |
| 526 | // placeholders are used before being set after the struct type gains an | |
| 527 | // InternPool index. | |
| 528 | const new_namespace_index = try zcu.createNamespace(.{ | |
| 529 | .parent = .none, | |
| 530 | .decl_index = undefined, | |
| 531 | .file_scope = file_index, | |
| 532 | }); | |
| 533 | errdefer zcu.destroyNamespace(new_namespace_index); | |
| 534 | ||
| 535 | const new_decl_index = try zcu.allocateNewDecl(new_namespace_index); | |
| 536 | const new_decl = zcu.declPtr(new_decl_index); | |
| 537 | errdefer @panic("TODO error handling"); | |
| 538 | ||
| 539 | zcu.setFileRootDecl(file_index, new_decl_index.toOptional()); | |
| 540 | zcu.namespacePtr(new_namespace_index).decl_index = new_decl_index; | |
| 541 | ||
| 542 | new_decl.name = try file.fullyQualifiedName(zcu); | |
| 543 | new_decl.name_fully_qualified = true; | |
| 544 | new_decl.is_pub = true; | |
| 545 | new_decl.is_exported = false; | |
| 546 | new_decl.alignment = .none; | |
| 547 | new_decl.@"linksection" = .none; | |
| 548 | new_decl.analysis = .in_progress; | |
| 549 | ||
| 550 | if (file.status != .success_zir) { | |
| 551 | new_decl.analysis = .file_failure; | |
| 552 | return; | |
| 553 | } | |
| 554 | assert(file.zir_loaded); | |
| 555 | ||
| 556 | const struct_ty = try pt.getFileRootStruct(new_decl_index, new_namespace_index, file_index); | |
| 557 | errdefer zcu.intern_pool.remove(struct_ty); | |
| 558 | ||
| 559 | switch (zcu.comp.cache_use) { | |
| 560 | .whole => |whole| if (whole.cache_manifest) |man| { | |
| 561 | const source = file.getSource(gpa) catch |err| { | |
| 562 | try Zcu.reportRetryableFileError(zcu, file_index, "unable to load source: {s}", .{@errorName(err)}); | |
| 563 | return error.AnalysisFail; | |
| 564 | }; | |
| 565 | ||
| 566 | const resolved_path = std.fs.path.resolve(gpa, &.{ | |
| 567 | file.mod.root.root_dir.path orelse ".", | |
| 568 | file.mod.root.sub_path, | |
| 569 | file.sub_file_path, | |
| 570 | }) catch |err| { | |
| 571 | try Zcu.reportRetryableFileError(zcu, file_index, "unable to resolve path: {s}", .{@errorName(err)}); | |
| 572 | return error.AnalysisFail; | |
| 573 | }; | |
| 574 | errdefer gpa.free(resolved_path); | |
| 575 | ||
| 576 | whole.cache_manifest_mutex.lock(); | |
| 577 | defer whole.cache_manifest_mutex.unlock(); | |
| 578 | try man.addFilePostContents(resolved_path, source.bytes, source.stat); | |
| 579 | }, | |
| 580 | .incremental => {}, | |
| 581 | } | |
| 582 | } | |
| 583 | ||
| 584 | fn semaDecl(pt: Zcu.PerThread, decl_index: Zcu.Decl.Index) !Zcu.SemaDeclResult { | |
| 585 | const tracy = trace(@src()); | |
| 586 | defer tracy.end(); | |
| 587 | ||
| 588 | const zcu = pt.zcu; | |
| 589 | const decl = zcu.declPtr(decl_index); | |
| 590 | const ip = &zcu.intern_pool; | |
| 591 | ||
| 592 | if (decl.getFileScope(zcu).status != .success_zir) { | |
| 593 | return error.AnalysisFail; | |
| 594 | } | |
| 595 | ||
| 596 | assert(!zcu.declIsRoot(decl_index)); | |
| 597 | ||
| 598 | if (decl.zir_decl_index == .none and decl.owns_tv) { | |
| 599 | // We are re-analyzing an anonymous owner Decl (for a function or a namespace type). | |
| 600 | return zcu.semaAnonOwnerDecl(decl_index); | |
| 601 | } | |
| 602 | ||
| 603 | log.debug("semaDecl '{d}'", .{@intFromEnum(decl_index)}); | |
| 604 | log.debug("decl name '{}'", .{(try decl.fullyQualifiedName(zcu)).fmt(ip)}); | |
| 605 | defer blk: { | |
| 606 | log.debug("finish decl name '{}'", .{(decl.fullyQualifiedName(zcu) catch break :blk).fmt(ip)}); | |
| 607 | } | |
| 608 | ||
| 609 | const old_has_tv = decl.has_tv; | |
| 610 | // The following values are ignored if `!old_has_tv` | |
| 611 | const old_ty = if (old_has_tv) decl.typeOf(zcu) else undefined; | |
| 612 | const old_val = decl.val; | |
| 613 | const old_align = decl.alignment; | |
| 614 | const old_linksection = decl.@"linksection"; | |
| 615 | const old_addrspace = decl.@"addrspace"; | |
| 616 | const old_is_inline = if (decl.getOwnedFunction(zcu)) |prev_func| | |
| 617 | prev_func.analysis(ip).state == .inline_only | |
| 618 | else | |
| 619 | false; | |
| 620 | ||
| 621 | const decl_inst = decl.zir_decl_index.unwrap().?.resolve(ip); | |
| 622 | ||
| 623 | const gpa = zcu.gpa; | |
| 624 | const zir = decl.getFileScope(zcu).zir; | |
| 625 | ||
| 626 | const builtin_type_target_index: InternPool.Index = ip_index: { | |
| 627 | const std_mod = zcu.std_mod; | |
| 628 | if (decl.getFileScope(zcu).mod != std_mod) break :ip_index .none; | |
| 629 | // We're in the std module. | |
| 630 | const std_file_imported = try zcu.importPkg(std_mod); | |
| 631 | const std_file_root_decl_index = zcu.fileRootDecl(std_file_imported.file_index); | |
| 632 | const std_decl = zcu.declPtr(std_file_root_decl_index.unwrap().?); | |
| 633 | const std_namespace = std_decl.getInnerNamespace(zcu).?; | |
| 634 | const builtin_str = try ip.getOrPutString(gpa, "builtin", .no_embedded_nulls); | |
| 635 | const builtin_decl = zcu.declPtr(std_namespace.decls.getKeyAdapted(builtin_str, Zcu.DeclAdapter{ .zcu = zcu }) orelse break :ip_index .none); | |
| 636 | const builtin_namespace = builtin_decl.getInnerNamespaceIndex(zcu).unwrap() orelse break :ip_index .none; | |
| 637 | if (decl.src_namespace != builtin_namespace) break :ip_index .none; | |
| 638 | // We're in builtin.zig. This could be a builtin we need to add to a specific InternPool index. | |
| 639 | for ([_][]const u8{ | |
| 640 | "AtomicOrder", | |
| 641 | "AtomicRmwOp", | |
| 642 | "CallingConvention", | |
| 643 | "AddressSpace", | |
| 644 | "FloatMode", | |
| 645 | "ReduceOp", | |
| 646 | "CallModifier", | |
| 647 | "PrefetchOptions", | |
| 648 | "ExportOptions", | |
| 649 | "ExternOptions", | |
| 650 | "Type", | |
| 651 | }, [_]InternPool.Index{ | |
| 652 | .atomic_order_type, | |
| 653 | .atomic_rmw_op_type, | |
| 654 | .calling_convention_type, | |
| 655 | .address_space_type, | |
| 656 | .float_mode_type, | |
| 657 | .reduce_op_type, | |
| 658 | .call_modifier_type, | |
| 659 | .prefetch_options_type, | |
| 660 | .export_options_type, | |
| 661 | .extern_options_type, | |
| 662 | .type_info_type, | |
| 663 | }) |type_name, type_ip| { | |
| 664 | if (decl.name.eqlSlice(type_name, ip)) break :ip_index type_ip; | |
| 665 | } | |
| 666 | break :ip_index .none; | |
| 667 | }; | |
| 668 | ||
| 669 | zcu.intern_pool.removeDependenciesForDepender(gpa, InternPool.AnalUnit.wrap(.{ .decl = decl_index })); | |
| 670 | ||
| 671 | decl.analysis = .in_progress; | |
| 672 | ||
| 673 | var analysis_arena = std.heap.ArenaAllocator.init(gpa); | |
| 674 | defer analysis_arena.deinit(); | |
| 675 | ||
| 676 | var comptime_err_ret_trace = std.ArrayList(Zcu.LazySrcLoc).init(gpa); | |
| 677 | defer comptime_err_ret_trace.deinit(); | |
| 678 | ||
| 679 | var sema: Sema = .{ | |
| 680 | .pt = pt, | |
| 681 | .gpa = gpa, | |
| 682 | .arena = analysis_arena.allocator(), | |
| 683 | .code = zir, | |
| 684 | .owner_decl = decl, | |
| 685 | .owner_decl_index = decl_index, | |
| 686 | .func_index = .none, | |
| 687 | .func_is_naked = false, | |
| 688 | .fn_ret_ty = Type.void, | |
| 689 | .fn_ret_ty_ies = null, | |
| 690 | .owner_func_index = .none, | |
| 691 | .comptime_err_ret_trace = &comptime_err_ret_trace, | |
| 692 | .builtin_type_target_index = builtin_type_target_index, | |
| 693 | }; | |
| 694 | defer sema.deinit(); | |
| 695 | ||
| 696 | // Every Decl (other than file root Decls, which do not have a ZIR index) has a dependency on its own source. | |
| 697 | try sema.declareDependency(.{ .src_hash = try ip.trackZir( | |
| 698 | gpa, | |
| 699 | decl.getFileScopeIndex(zcu), | |
| 700 | decl_inst, | |
| 701 | ) }); | |
| 702 | ||
| 703 | var block_scope: Sema.Block = .{ | |
| 704 | .parent = null, | |
| 705 | .sema = &sema, | |
| 706 | .namespace = decl.src_namespace, | |
| 707 | .instructions = .{}, | |
| 708 | .inlining = null, | |
| 709 | .is_comptime = true, | |
| 710 | .src_base_inst = decl.zir_decl_index.unwrap().?, | |
| 711 | .type_name_ctx = decl.name, | |
| 712 | }; | |
| 713 | defer block_scope.instructions.deinit(gpa); | |
| 714 | ||
| 715 | const decl_bodies = decl.zirBodies(zcu); | |
| 716 | ||
| 717 | const result_ref = try sema.resolveInlineBody(&block_scope, decl_bodies.value_body, decl_inst); | |
| 718 | // We'll do some other bits with the Sema. Clear the type target index just | |
| 719 | // in case they analyze any type. | |
| 720 | sema.builtin_type_target_index = .none; | |
| 721 | const align_src = block_scope.src(.{ .node_offset_var_decl_align = 0 }); | |
| 722 | const section_src = block_scope.src(.{ .node_offset_var_decl_section = 0 }); | |
| 723 | const address_space_src = block_scope.src(.{ .node_offset_var_decl_addrspace = 0 }); | |
| 724 | const ty_src = block_scope.src(.{ .node_offset_var_decl_ty = 0 }); | |
| 725 | const init_src = block_scope.src(.{ .node_offset_var_decl_init = 0 }); | |
| 726 | const decl_val = try sema.resolveFinalDeclValue(&block_scope, init_src, result_ref); | |
| 727 | const decl_ty = decl_val.typeOf(zcu); | |
| 728 | ||
| 729 | // Note this resolves the type of the Decl, not the value; if this Decl | |
| 730 | // is a struct, for example, this resolves `type` (which needs no resolution), | |
| 731 | // not the struct itself. | |
| 732 | try decl_ty.resolveLayout(pt); | |
| 733 | ||
| 734 | if (decl.kind == .@"usingnamespace") { | |
| 735 | if (!decl_ty.eql(Type.type, zcu)) { | |
| 736 | return sema.fail(&block_scope, ty_src, "expected type, found {}", .{decl_ty.fmt(pt)}); | |
| 737 | } | |
| 738 | const ty = decl_val.toType(); | |
| 739 | if (ty.getNamespace(zcu) == null) { | |
| 740 | return sema.fail(&block_scope, ty_src, "type {} has no namespace", .{ty.fmt(pt)}); | |
| 741 | } | |
| 742 | ||
| 743 | decl.val = ty.toValue(); | |
| 744 | decl.alignment = .none; | |
| 745 | decl.@"linksection" = .none; | |
| 746 | decl.has_tv = true; | |
| 747 | decl.owns_tv = false; | |
| 748 | decl.analysis = .complete; | |
| 749 | ||
| 750 | // TODO: usingnamespace cannot currently participate in incremental compilation | |
| 751 | return .{ | |
| 752 | .invalidate_decl_val = true, | |
| 753 | .invalidate_decl_ref = true, | |
| 754 | }; | |
| 755 | } | |
| 756 | ||
| 757 | var queue_linker_work = true; | |
| 758 | var is_func = false; | |
| 759 | var is_inline = false; | |
| 760 | switch (decl_val.toIntern()) { | |
| 761 | .generic_poison => unreachable, | |
| 762 | .unreachable_value => unreachable, | |
| 763 | else => switch (ip.indexToKey(decl_val.toIntern())) { | |
| 764 | .variable => |variable| { | |
| 765 | decl.owns_tv = variable.decl == decl_index; | |
| 766 | queue_linker_work = decl.owns_tv; | |
| 767 | }, | |
| 768 | ||
| 769 | .extern_func => |extern_func| { | |
| 770 | decl.owns_tv = extern_func.decl == decl_index; | |
| 771 | queue_linker_work = decl.owns_tv; | |
| 772 | is_func = decl.owns_tv; | |
| 773 | }, | |
| 774 | ||
| 775 | .func => |func| { | |
| 776 | decl.owns_tv = func.owner_decl == decl_index; | |
| 777 | queue_linker_work = false; | |
| 778 | is_inline = decl.owns_tv and decl_ty.fnCallingConvention(zcu) == .Inline; | |
| 779 | is_func = decl.owns_tv; | |
| 780 | }, | |
| 781 | ||
| 782 | else => {}, | |
| 783 | }, | |
| 784 | } | |
| 785 | ||
| 786 | decl.val = decl_val; | |
| 787 | // Function linksection, align, and addrspace were already set by Sema | |
| 788 | if (!is_func) { | |
| 789 | decl.alignment = blk: { | |
| 790 | const align_body = decl_bodies.align_body orelse break :blk .none; | |
| 791 | const align_ref = try sema.resolveInlineBody(&block_scope, align_body, decl_inst); | |
| 792 | break :blk try sema.analyzeAsAlign(&block_scope, align_src, align_ref); | |
| 793 | }; | |
| 794 | decl.@"linksection" = blk: { | |
| 795 | const linksection_body = decl_bodies.linksection_body orelse break :blk .none; | |
| 796 | const linksection_ref = try sema.resolveInlineBody(&block_scope, linksection_body, decl_inst); | |
| 797 | const bytes = try sema.toConstString(&block_scope, section_src, linksection_ref, .{ | |
| 798 | .needed_comptime_reason = "linksection must be comptime-known", | |
| 799 | }); | |
| 800 | if (std.mem.indexOfScalar(u8, bytes, 0) != null) { | |
| 801 | return sema.fail(&block_scope, section_src, "linksection cannot contain null bytes", .{}); | |
| 802 | } else if (bytes.len == 0) { | |
| 803 | return sema.fail(&block_scope, section_src, "linksection cannot be empty", .{}); | |
| 804 | } | |
| 805 | break :blk try ip.getOrPutStringOpt(gpa, bytes, .no_embedded_nulls); | |
| 806 | }; | |
| 807 | decl.@"addrspace" = blk: { | |
| 808 | const addrspace_ctx: Sema.AddressSpaceContext = switch (ip.indexToKey(decl_val.toIntern())) { | |
| 809 | .variable => .variable, | |
| 810 | .extern_func, .func => .function, | |
| 811 | else => .constant, | |
| 812 | }; | |
| 813 | ||
| 814 | const target = zcu.getTarget(); | |
| 815 | ||
| 816 | const addrspace_body = decl_bodies.addrspace_body orelse break :blk switch (addrspace_ctx) { | |
| 817 | .function => target_util.defaultAddressSpace(target, .function), | |
| 818 | .variable => target_util.defaultAddressSpace(target, .global_mutable), | |
| 819 | .constant => target_util.defaultAddressSpace(target, .global_constant), | |
| 820 | else => unreachable, | |
| 821 | }; | |
| 822 | const addrspace_ref = try sema.resolveInlineBody(&block_scope, addrspace_body, decl_inst); | |
| 823 | break :blk try sema.analyzeAsAddressSpace(&block_scope, address_space_src, addrspace_ref, addrspace_ctx); | |
| 824 | }; | |
| 825 | } | |
| 826 | decl.has_tv = true; | |
| 827 | decl.analysis = .complete; | |
| 828 | ||
| 829 | const result: Zcu.SemaDeclResult = if (old_has_tv) .{ | |
| 830 | .invalidate_decl_val = !decl_ty.eql(old_ty, zcu) or | |
| 831 | !decl.val.eql(old_val, decl_ty, zcu) or | |
| 832 | is_inline != old_is_inline, | |
| 833 | .invalidate_decl_ref = !decl_ty.eql(old_ty, zcu) or | |
| 834 | decl.alignment != old_align or | |
| 835 | decl.@"linksection" != old_linksection or | |
| 836 | decl.@"addrspace" != old_addrspace or | |
| 837 | is_inline != old_is_inline, | |
| 838 | } else .{ | |
| 839 | .invalidate_decl_val = true, | |
| 840 | .invalidate_decl_ref = true, | |
| 841 | }; | |
| 842 | ||
| 843 | const has_runtime_bits = queue_linker_work and (is_func or try sema.typeHasRuntimeBits(decl_ty)); | |
| 844 | if (has_runtime_bits) { | |
| 845 | // Needed for codegen_decl which will call updateDecl and then the | |
| 846 | // codegen backend wants full access to the Decl Type. | |
| 847 | try decl_ty.resolveFully(pt); | |
| 848 | ||
| 849 | try zcu.comp.work_queue.writeItem(.{ .codegen_decl = decl_index }); | |
| 850 | ||
| 851 | if (result.invalidate_decl_ref and zcu.emit_h != null) { | |
| 852 | try zcu.comp.work_queue.writeItem(.{ .emit_h_decl = decl_index }); | |
| 853 | } | |
| 854 | } | |
| 855 | ||
| 856 | if (decl.is_exported) { | |
| 857 | const export_src = block_scope.src(.{ .token_offset = @intFromBool(decl.is_pub) }); | |
| 858 | if (is_inline) return sema.fail(&block_scope, export_src, "export of inline function", .{}); | |
| 859 | // The scope needs to have the decl in it. | |
| 860 | try sema.analyzeExport(&block_scope, export_src, .{ .name = decl.name }, decl_index); | |
| 861 | } | |
| 862 | ||
| 863 | try sema.flushExports(); | |
| 864 | ||
| 865 | return result; | |
| 866 | } | |
| 867 | ||
| 868 | pub fn embedFile( | |
| 869 | pt: Zcu.PerThread, | |
| 870 | cur_file: *Zcu.File, | |
| 871 | import_string: []const u8, | |
| 872 | src_loc: Zcu.LazySrcLoc, | |
| 873 | ) !InternPool.Index { | |
| 874 | const mod = pt.zcu; | |
| 875 | const gpa = mod.gpa; | |
| 876 | ||
| 877 | if (cur_file.mod.deps.get(import_string)) |pkg| { | |
| 878 | const resolved_path = try std.fs.path.resolve(gpa, &.{ | |
| 879 | pkg.root.root_dir.path orelse ".", | |
| 880 | pkg.root.sub_path, | |
| 881 | pkg.root_src_path, | |
| 882 | }); | |
| 883 | var keep_resolved_path = false; | |
| 884 | defer if (!keep_resolved_path) gpa.free(resolved_path); | |
| 885 | ||
| 886 | const gop = try mod.embed_table.getOrPut(gpa, resolved_path); | |
| 887 | errdefer { | |
| 888 | assert(std.mem.eql(u8, mod.embed_table.pop().key, resolved_path)); | |
| 889 | keep_resolved_path = false; | |
| 890 | } | |
| 891 | if (gop.found_existing) return gop.value_ptr.*.val; | |
| 892 | keep_resolved_path = true; | |
| 893 | ||
| 894 | const sub_file_path = try gpa.dupe(u8, pkg.root_src_path); | |
| 895 | errdefer gpa.free(sub_file_path); | |
| 896 | ||
| 897 | return pt.newEmbedFile(pkg, sub_file_path, resolved_path, gop.value_ptr, src_loc); | |
| 898 | } | |
| 899 | ||
| 900 | // The resolved path is used as the key in the table, to detect if a file | |
| 901 | // refers to the same as another, despite different relative paths. | |
| 902 | const resolved_path = try std.fs.path.resolve(gpa, &.{ | |
| 903 | cur_file.mod.root.root_dir.path orelse ".", | |
| 904 | cur_file.mod.root.sub_path, | |
| 905 | cur_file.sub_file_path, | |
| 906 | "..", | |
| 907 | import_string, | |
| 908 | }); | |
| 909 | ||
| 910 | var keep_resolved_path = false; | |
| 911 | defer if (!keep_resolved_path) gpa.free(resolved_path); | |
| 912 | ||
| 913 | const gop = try mod.embed_table.getOrPut(gpa, resolved_path); | |
| 914 | errdefer { | |
| 915 | assert(std.mem.eql(u8, mod.embed_table.pop().key, resolved_path)); | |
| 916 | keep_resolved_path = false; | |
| 917 | } | |
| 918 | if (gop.found_existing) return gop.value_ptr.*.val; | |
| 919 | keep_resolved_path = true; | |
| 920 | ||
| 921 | const resolved_root_path = try std.fs.path.resolve(gpa, &.{ | |
| 922 | cur_file.mod.root.root_dir.path orelse ".", | |
| 923 | cur_file.mod.root.sub_path, | |
| 924 | }); | |
| 925 | defer gpa.free(resolved_root_path); | |
| 926 | ||
| 927 | const sub_file_path = p: { | |
| 928 | const relative = try std.fs.path.relative(gpa, resolved_root_path, resolved_path); | |
| 929 | errdefer gpa.free(relative); | |
| 930 | ||
| 931 | if (!isUpDir(relative) and !std.fs.path.isAbsolute(relative)) { | |
| 932 | break :p relative; | |
| 933 | } | |
| 934 | return error.ImportOutsideModulePath; | |
| 935 | }; | |
| 936 | defer gpa.free(sub_file_path); | |
| 937 | ||
| 938 | return pt.newEmbedFile(cur_file.mod, sub_file_path, resolved_path, gop.value_ptr, src_loc); | |
| 939 | } | |
| 940 | ||
| 941 | /// Finalize the creation of an anon decl. | |
| 942 | pub fn finalizeAnonDecl(pt: Zcu.PerThread, decl_index: Zcu.Decl.Index) Allocator.Error!void { | |
| 943 | if (pt.zcu.declPtr(decl_index).typeOf(pt.zcu).isFnOrHasRuntimeBits(pt)) { | |
| 944 | try pt.zcu.comp.work_queue.writeItem(.{ .codegen_decl = decl_index }); | |
| 945 | } | |
| 946 | } | |
| 947 | ||
| 948 | /// https://github.com/ziglang/zig/issues/14307 | |
| 949 | fn newEmbedFile( | |
| 950 | pt: Zcu.PerThread, | |
| 951 | pkg: *Module, | |
| 952 | sub_file_path: []const u8, | |
| 953 | resolved_path: []const u8, | |
| 954 | result: **Zcu.EmbedFile, | |
| 955 | src_loc: Zcu.LazySrcLoc, | |
| 956 | ) !InternPool.Index { | |
| 957 | const mod = pt.zcu; | |
| 958 | const gpa = mod.gpa; | |
| 959 | const ip = &mod.intern_pool; | |
| 960 | ||
| 961 | const new_file = try gpa.create(Zcu.EmbedFile); | |
| 962 | errdefer gpa.destroy(new_file); | |
| 963 | ||
| 964 | var file = try pkg.root.openFile(sub_file_path, .{}); | |
| 965 | defer file.close(); | |
| 966 | ||
| 967 | const actual_stat = try file.stat(); | |
| 968 | const stat: Cache.File.Stat = .{ | |
| 969 | .size = actual_stat.size, | |
| 970 | .inode = actual_stat.inode, | |
| 971 | .mtime = actual_stat.mtime, | |
| 972 | }; | |
| 973 | const size = std.math.cast(usize, actual_stat.size) orelse return error.Overflow; | |
| 974 | ||
| 975 | const bytes = try ip.string_bytes.addManyAsSlice(gpa, try std.math.add(usize, size, 1)); | |
| 976 | const actual_read = try file.readAll(bytes[0..size]); | |
| 977 | if (actual_read != size) return error.UnexpectedEndOfFile; | |
| 978 | bytes[size] = 0; | |
| 979 | ||
| 980 | const comp = mod.comp; | |
| 981 | switch (comp.cache_use) { | |
| 982 | .whole => |whole| if (whole.cache_manifest) |man| { | |
| 983 | const copied_resolved_path = try gpa.dupe(u8, resolved_path); | |
| 984 | errdefer gpa.free(copied_resolved_path); | |
| 985 | whole.cache_manifest_mutex.lock(); | |
| 986 | defer whole.cache_manifest_mutex.unlock(); | |
| 987 | try man.addFilePostContents(copied_resolved_path, bytes[0..size], stat); | |
| 988 | }, | |
| 989 | .incremental => {}, | |
| 990 | } | |
| 991 | ||
| 992 | const array_ty = try pt.intern(.{ .array_type = .{ | |
| 993 | .len = size, | |
| 994 | .sentinel = .zero_u8, | |
| 995 | .child = .u8_type, | |
| 996 | } }); | |
| 997 | const array_val = try pt.intern(.{ .aggregate = .{ | |
| 998 | .ty = array_ty, | |
| 999 | .storage = .{ .bytes = try ip.getOrPutTrailingString(gpa, bytes.len, .maybe_embedded_nulls) }, | |
| 1000 | } }); | |
| 1001 | ||
| 1002 | const ptr_ty = (try pt.ptrType(.{ | |
| 1003 | .child = array_ty, | |
| 1004 | .flags = .{ | |
| 1005 | .alignment = .none, | |
| 1006 | .is_const = true, | |
| 1007 | .address_space = .generic, | |
| 1008 | }, | |
| 1009 | })).toIntern(); | |
| 1010 | const ptr_val = try pt.intern(.{ .ptr = .{ | |
| 1011 | .ty = ptr_ty, | |
| 1012 | .base_addr = .{ .anon_decl = .{ | |
| 1013 | .val = array_val, | |
| 1014 | .orig_ty = ptr_ty, | |
| 1015 | } }, | |
| 1016 | .byte_offset = 0, | |
| 1017 | } }); | |
| 1018 | ||
| 1019 | result.* = new_file; | |
| 1020 | new_file.* = .{ | |
| 1021 | .sub_file_path = try ip.getOrPutString(gpa, sub_file_path, .no_embedded_nulls), | |
| 1022 | .owner = pkg, | |
| 1023 | .stat = stat, | |
| 1024 | .val = ptr_val, | |
| 1025 | .src_loc = src_loc, | |
| 1026 | }; | |
| 1027 | return ptr_val; | |
| 1028 | } | |
| 1029 | ||
| 1030 | pub fn analyzeFnBody(pt: Zcu.PerThread, func_index: InternPool.Index, arena: Allocator) Zcu.SemaError!Air { | |
| 1031 | const tracy = trace(@src()); | |
| 1032 | defer tracy.end(); | |
| 1033 | ||
| 1034 | const mod = pt.zcu; | |
| 1035 | const gpa = mod.gpa; | |
| 1036 | const ip = &mod.intern_pool; | |
| 1037 | const func = mod.funcInfo(func_index); | |
| 1038 | const decl_index = func.owner_decl; | |
| 1039 | const decl = mod.declPtr(decl_index); | |
| 1040 | ||
| 1041 | log.debug("func name '{}'", .{(try decl.fullyQualifiedName(mod)).fmt(ip)}); | |
| 1042 | defer blk: { | |
| 1043 | log.debug("finish func name '{}'", .{(decl.fullyQualifiedName(mod) catch break :blk).fmt(ip)}); | |
| 1044 | } | |
| 1045 | ||
| 1046 | const decl_prog_node = mod.sema_prog_node.start((try decl.fullyQualifiedName(mod)).toSlice(ip), 0); | |
| 1047 | defer decl_prog_node.end(); | |
| 1048 | ||
| 1049 | mod.intern_pool.removeDependenciesForDepender(gpa, InternPool.AnalUnit.wrap(.{ .func = func_index })); | |
| 1050 | ||
| 1051 | var comptime_err_ret_trace = std.ArrayList(Zcu.LazySrcLoc).init(gpa); | |
| 1052 | defer comptime_err_ret_trace.deinit(); | |
| 1053 | ||
| 1054 | // In the case of a generic function instance, this is the type of the | |
| 1055 | // instance, which has comptime parameters elided. In other words, it is | |
| 1056 | // the runtime-known parameters only, not to be confused with the | |
| 1057 | // generic_owner function type, which potentially has more parameters, | |
| 1058 | // including comptime parameters. | |
| 1059 | const fn_ty = decl.typeOf(mod); | |
| 1060 | const fn_ty_info = mod.typeToFunc(fn_ty).?; | |
| 1061 | ||
| 1062 | var sema: Sema = .{ | |
| 1063 | .pt = pt, | |
| 1064 | .gpa = gpa, | |
| 1065 | .arena = arena, | |
| 1066 | .code = decl.getFileScope(mod).zir, | |
| 1067 | .owner_decl = decl, | |
| 1068 | .owner_decl_index = decl_index, | |
| 1069 | .func_index = func_index, | |
| 1070 | .func_is_naked = fn_ty_info.cc == .Naked, | |
| 1071 | .fn_ret_ty = Type.fromInterned(fn_ty_info.return_type), | |
| 1072 | .fn_ret_ty_ies = null, | |
| 1073 | .owner_func_index = func_index, | |
| 1074 | .branch_quota = @max(func.branchQuota(ip).*, Sema.default_branch_quota), | |
| 1075 | .comptime_err_ret_trace = &comptime_err_ret_trace, | |
| 1076 | }; | |
| 1077 | defer sema.deinit(); | |
| 1078 | ||
| 1079 | // Every runtime function has a dependency on the source of the Decl it originates from. | |
| 1080 | // It also depends on the value of its owner Decl. | |
| 1081 | try sema.declareDependency(.{ .src_hash = decl.zir_decl_index.unwrap().? }); | |
| 1082 | try sema.declareDependency(.{ .decl_val = decl_index }); | |
| 1083 | ||
| 1084 | if (func.analysis(ip).inferred_error_set) { | |
| 1085 | const ies = try arena.create(Sema.InferredErrorSet); | |
| 1086 | ies.* = .{ .func = func_index }; | |
| 1087 | sema.fn_ret_ty_ies = ies; | |
| 1088 | } | |
| 1089 | ||
| 1090 | // reset in case calls to errorable functions are removed. | |
| 1091 | func.analysis(ip).calls_or_awaits_errorable_fn = false; | |
| 1092 | ||
| 1093 | // First few indexes of extra are reserved and set at the end. | |
| 1094 | const reserved_count = @typeInfo(Air.ExtraIndex).Enum.fields.len; | |
| 1095 | try sema.air_extra.ensureTotalCapacity(gpa, reserved_count); | |
| 1096 | sema.air_extra.items.len += reserved_count; | |
| 1097 | ||
| 1098 | var inner_block: Sema.Block = .{ | |
| 1099 | .parent = null, | |
| 1100 | .sema = &sema, | |
| 1101 | .namespace = decl.src_namespace, | |
| 1102 | .instructions = .{}, | |
| 1103 | .inlining = null, | |
| 1104 | .is_comptime = false, | |
| 1105 | .src_base_inst = inst: { | |
| 1106 | const owner_info = if (func.generic_owner == .none) | |
| 1107 | func | |
| 1108 | else | |
| 1109 | mod.funcInfo(func.generic_owner); | |
| 1110 | const orig_decl = mod.declPtr(owner_info.owner_decl); | |
| 1111 | break :inst orig_decl.zir_decl_index.unwrap().?; | |
| 1112 | }, | |
| 1113 | .type_name_ctx = decl.name, | |
| 1114 | }; | |
| 1115 | defer inner_block.instructions.deinit(gpa); | |
| 1116 | ||
| 1117 | const fn_info = sema.code.getFnInfo(func.zirBodyInst(ip).resolve(ip)); | |
| 1118 | ||
| 1119 | // Here we are performing "runtime semantic analysis" for a function body, which means | |
| 1120 | // we must map the parameter ZIR instructions to `arg` AIR instructions. | |
| 1121 | // AIR requires the `arg` parameters to be the first N instructions. | |
| 1122 | // This could be a generic function instantiation, however, in which case we need to | |
| 1123 | // map the comptime parameters to constant values and only emit arg AIR instructions | |
| 1124 | // for the runtime ones. | |
| 1125 | const runtime_params_len = fn_ty_info.param_types.len; | |
| 1126 | try inner_block.instructions.ensureTotalCapacityPrecise(gpa, runtime_params_len); | |
| 1127 | try sema.air_instructions.ensureUnusedCapacity(gpa, fn_info.total_params_len); | |
| 1128 | try sema.inst_map.ensureSpaceForInstructions(gpa, fn_info.param_body); | |
| 1129 | ||
| 1130 | // In the case of a generic function instance, pre-populate all the comptime args. | |
| 1131 | if (func.comptime_args.len != 0) { | |
| 1132 | for ( | |
| 1133 | fn_info.param_body[0..func.comptime_args.len], | |
| 1134 | func.comptime_args.get(ip), | |
| 1135 | ) |inst, comptime_arg| { | |
| 1136 | if (comptime_arg == .none) continue; | |
| 1137 | sema.inst_map.putAssumeCapacityNoClobber(inst, Air.internedToRef(comptime_arg)); | |
| 1138 | } | |
| 1139 | } | |
| 1140 | ||
| 1141 | const src_params_len = if (func.comptime_args.len != 0) | |
| 1142 | func.comptime_args.len | |
| 1143 | else | |
| 1144 | runtime_params_len; | |
| 1145 | ||
| 1146 | var runtime_param_index: usize = 0; | |
| 1147 | for (fn_info.param_body[0..src_params_len], 0..) |inst, src_param_index| { | |
| 1148 | const gop = sema.inst_map.getOrPutAssumeCapacity(inst); | |
| 1149 | if (gop.found_existing) continue; // provided above by comptime arg | |
| 1150 | ||
| 1151 | const param_ty = fn_ty_info.param_types.get(ip)[runtime_param_index]; | |
| 1152 | runtime_param_index += 1; | |
| 1153 | ||
| 1154 | const opt_opv = sema.typeHasOnePossibleValue(Type.fromInterned(param_ty)) catch |err| switch (err) { | |
| 1155 | error.GenericPoison => unreachable, | |
| 1156 | error.ComptimeReturn => unreachable, | |
| 1157 | error.ComptimeBreak => unreachable, | |
| 1158 | else => |e| return e, | |
| 1159 | }; | |
| 1160 | if (opt_opv) |opv| { | |
| 1161 | gop.value_ptr.* = Air.internedToRef(opv.toIntern()); | |
| 1162 | continue; | |
| 1163 | } | |
| 1164 | const arg_index: Air.Inst.Index = @enumFromInt(sema.air_instructions.len); | |
| 1165 | gop.value_ptr.* = arg_index.toRef(); | |
| 1166 | inner_block.instructions.appendAssumeCapacity(arg_index); | |
| 1167 | sema.air_instructions.appendAssumeCapacity(.{ | |
| 1168 | .tag = .arg, | |
| 1169 | .data = .{ .arg = .{ | |
| 1170 | .ty = Air.internedToRef(param_ty), | |
| 1171 | .src_index = @intCast(src_param_index), | |
| 1172 | } }, | |
| 1173 | }); | |
| 1174 | } | |
| 1175 | ||
| 1176 | func.analysis(ip).state = .in_progress; | |
| 1177 | ||
| 1178 | const last_arg_index = inner_block.instructions.items.len; | |
| 1179 | ||
| 1180 | // Save the error trace as our first action in the function. | |
| 1181 | // If this is unnecessary after all, Liveness will clean it up for us. | |
| 1182 | const error_return_trace_index = try sema.analyzeSaveErrRetIndex(&inner_block); | |
| 1183 | sema.error_return_trace_index_on_fn_entry = error_return_trace_index; | |
| 1184 | inner_block.error_return_trace_index = error_return_trace_index; | |
| 1185 | ||
| 1186 | sema.analyzeFnBody(&inner_block, fn_info.body) catch |err| switch (err) { | |
| 1187 | // TODO make these unreachable instead of @panic | |
| 1188 | error.GenericPoison => @panic("zig compiler bug: GenericPoison"), | |
| 1189 | error.ComptimeReturn => @panic("zig compiler bug: ComptimeReturn"), | |
| 1190 | else => |e| return e, | |
| 1191 | }; | |
| 1192 | ||
| 1193 | for (sema.unresolved_inferred_allocs.keys()) |ptr_inst| { | |
| 1194 | // The lack of a resolve_inferred_alloc means that this instruction | |
| 1195 | // is unused so it just has to be a no-op. | |
| 1196 | sema.air_instructions.set(@intFromEnum(ptr_inst), .{ | |
| 1197 | .tag = .alloc, | |
| 1198 | .data = .{ .ty = Type.single_const_pointer_to_comptime_int }, | |
| 1199 | }); | |
| 1200 | } | |
| 1201 | ||
| 1202 | // If we don't get an error return trace from a caller, create our own. | |
| 1203 | if (func.analysis(ip).calls_or_awaits_errorable_fn and | |
| 1204 | mod.comp.config.any_error_tracing and | |
| 1205 | !sema.fn_ret_ty.isError(mod)) | |
| 1206 | { | |
| 1207 | sema.setupErrorReturnTrace(&inner_block, last_arg_index) catch |err| switch (err) { | |
| 1208 | // TODO make these unreachable instead of @panic | |
| 1209 | error.GenericPoison => @panic("zig compiler bug: GenericPoison"), | |
| 1210 | error.ComptimeReturn => @panic("zig compiler bug: ComptimeReturn"), | |
| 1211 | error.ComptimeBreak => @panic("zig compiler bug: ComptimeBreak"), | |
| 1212 | else => |e| return e, | |
| 1213 | }; | |
| 1214 | } | |
| 1215 | ||
| 1216 | // Copy the block into place and mark that as the main block. | |
| 1217 | try sema.air_extra.ensureUnusedCapacity(gpa, @typeInfo(Air.Block).Struct.fields.len + | |
| 1218 | inner_block.instructions.items.len); | |
| 1219 | const main_block_index = sema.addExtraAssumeCapacity(Air.Block{ | |
| 1220 | .body_len = @intCast(inner_block.instructions.items.len), | |
| 1221 | }); | |
| 1222 | sema.air_extra.appendSliceAssumeCapacity(@ptrCast(inner_block.instructions.items)); | |
| 1223 | sema.air_extra.items[@intFromEnum(Air.ExtraIndex.main_block)] = main_block_index; | |
| 1224 | ||
| 1225 | // Resolving inferred error sets is done *before* setting the function | |
| 1226 | // state to success, so that "unable to resolve inferred error set" errors | |
| 1227 | // can be emitted here. | |
| 1228 | if (sema.fn_ret_ty_ies) |ies| { | |
| 1229 | sema.resolveInferredErrorSetPtr(&inner_block, .{ | |
| 1230 | .base_node_inst = inner_block.src_base_inst, | |
| 1231 | .offset = Zcu.LazySrcLoc.Offset.nodeOffset(0), | |
| 1232 | }, ies) catch |err| switch (err) { | |
| 1233 | error.GenericPoison => unreachable, | |
| 1234 | error.ComptimeReturn => unreachable, | |
| 1235 | error.ComptimeBreak => unreachable, | |
| 1236 | error.AnalysisFail => { | |
| 1237 | // In this case our function depends on a type that had a compile error. | |
| 1238 | // We should not try to lower this function. | |
| 1239 | decl.analysis = .dependency_failure; | |
| 1240 | return error.AnalysisFail; | |
| 1241 | }, | |
| 1242 | else => |e| return e, | |
| 1243 | }; | |
| 1244 | assert(ies.resolved != .none); | |
| 1245 | ip.funcIesResolved(func_index).* = ies.resolved; | |
| 1246 | } | |
| 1247 | ||
| 1248 | func.analysis(ip).state = .success; | |
| 1249 | ||
| 1250 | // Finally we must resolve the return type and parameter types so that backends | |
| 1251 | // have full access to type information. | |
| 1252 | // Crucially, this happens *after* we set the function state to success above, | |
| 1253 | // so that dependencies on the function body will now be satisfied rather than | |
| 1254 | // result in circular dependency errors. | |
| 1255 | sema.resolveFnTypes(fn_ty) catch |err| switch (err) { | |
| 1256 | error.GenericPoison => unreachable, | |
| 1257 | error.ComptimeReturn => unreachable, | |
| 1258 | error.ComptimeBreak => unreachable, | |
| 1259 | error.AnalysisFail => { | |
| 1260 | // In this case our function depends on a type that had a compile error. | |
| 1261 | // We should not try to lower this function. | |
| 1262 | decl.analysis = .dependency_failure; | |
| 1263 | return error.AnalysisFail; | |
| 1264 | }, | |
| 1265 | else => |e| return e, | |
| 1266 | }; | |
| 1267 | ||
| 1268 | try sema.flushExports(); | |
| 1269 | ||
| 1270 | return .{ | |
| 1271 | .instructions = sema.air_instructions.toOwnedSlice(), | |
| 1272 | .extra = try sema.air_extra.toOwnedSlice(gpa), | |
| 1273 | }; | |
| 1274 | } | |
| 1275 | ||
| 1276 | /// Called from `Compilation.update`, after everything is done, just before | |
| 1277 | /// reporting compile errors. In this function we emit exported symbol collision | |
| 1278 | /// errors and communicate exported symbols to the linker backend. | |
| 1279 | pub fn processExports(pt: Zcu.PerThread) !void { | |
| 1280 | const zcu = pt.zcu; | |
| 1281 | const gpa = zcu.gpa; | |
| 1282 | ||
| 1283 | // First, construct a mapping of every exported value and Decl to the indices of all its different exports. | |
| 1284 | var decl_exports: std.AutoArrayHashMapUnmanaged(Zcu.Decl.Index, std.ArrayListUnmanaged(u32)) = .{}; | |
| 1285 | var value_exports: std.AutoArrayHashMapUnmanaged(InternPool.Index, std.ArrayListUnmanaged(u32)) = .{}; | |
| 1286 | defer { | |
| 1287 | for (decl_exports.values()) |*exports| { | |
| 1288 | exports.deinit(gpa); | |
| 1289 | } | |
| 1290 | decl_exports.deinit(gpa); | |
| 1291 | for (value_exports.values()) |*exports| { | |
| 1292 | exports.deinit(gpa); | |
| 1293 | } | |
| 1294 | value_exports.deinit(gpa); | |
| 1295 | } | |
| 1296 | ||
| 1297 | // We note as a heuristic: | |
| 1298 | // * It is rare to export a value. | |
| 1299 | // * It is rare for one Decl to be exported multiple times. | |
| 1300 | // So, this ensureTotalCapacity serves as a reasonable (albeit very approximate) optimization. | |
| 1301 | try decl_exports.ensureTotalCapacity(gpa, zcu.single_exports.count() + zcu.multi_exports.count()); | |
| 1302 | ||
| 1303 | for (zcu.single_exports.values()) |export_idx| { | |
| 1304 | const exp = zcu.all_exports.items[export_idx]; | |
| 1305 | const value_ptr, const found_existing = switch (exp.exported) { | |
| 1306 | .decl_index => |i| gop: { | |
| 1307 | const gop = try decl_exports.getOrPut(gpa, i); | |
| 1308 | break :gop .{ gop.value_ptr, gop.found_existing }; | |
| 1309 | }, | |
| 1310 | .value => |i| gop: { | |
| 1311 | const gop = try value_exports.getOrPut(gpa, i); | |
| 1312 | break :gop .{ gop.value_ptr, gop.found_existing }; | |
| 1313 | }, | |
| 1314 | }; | |
| 1315 | if (!found_existing) value_ptr.* = .{}; | |
| 1316 | try value_ptr.append(gpa, export_idx); | |
| 1317 | } | |
| 1318 | ||
| 1319 | for (zcu.multi_exports.values()) |info| { | |
| 1320 | for (zcu.all_exports.items[info.index..][0..info.len], info.index..) |exp, export_idx| { | |
| 1321 | const value_ptr, const found_existing = switch (exp.exported) { | |
| 1322 | .decl_index => |i| gop: { | |
| 1323 | const gop = try decl_exports.getOrPut(gpa, i); | |
| 1324 | break :gop .{ gop.value_ptr, gop.found_existing }; | |
| 1325 | }, | |
| 1326 | .value => |i| gop: { | |
| 1327 | const gop = try value_exports.getOrPut(gpa, i); | |
| 1328 | break :gop .{ gop.value_ptr, gop.found_existing }; | |
| 1329 | }, | |
| 1330 | }; | |
| 1331 | if (!found_existing) value_ptr.* = .{}; | |
| 1332 | try value_ptr.append(gpa, @intCast(export_idx)); | |
| 1333 | } | |
| 1334 | } | |
| 1335 | ||
| 1336 | // Map symbol names to `Export` for name collision detection. | |
| 1337 | var symbol_exports: SymbolExports = .{}; | |
| 1338 | defer symbol_exports.deinit(gpa); | |
| 1339 | ||
| 1340 | for (decl_exports.keys(), decl_exports.values()) |exported_decl, exports_list| { | |
| 1341 | const exported: Zcu.Exported = .{ .decl_index = exported_decl }; | |
| 1342 | try pt.processExportsInner(&symbol_exports, exported, exports_list.items); | |
| 1343 | } | |
| 1344 | ||
| 1345 | for (value_exports.keys(), value_exports.values()) |exported_value, exports_list| { | |
| 1346 | const exported: Zcu.Exported = .{ .value = exported_value }; | |
| 1347 | try pt.processExportsInner(&symbol_exports, exported, exports_list.items); | |
| 1348 | } | |
| 1349 | } | |
| 1350 | ||
| 1351 | const SymbolExports = std.AutoArrayHashMapUnmanaged(InternPool.NullTerminatedString, u32); | |
| 1352 | ||
| 1353 | fn processExportsInner( | |
| 1354 | pt: Zcu.PerThread, | |
| 1355 | symbol_exports: *SymbolExports, | |
| 1356 | exported: Zcu.Exported, | |
| 1357 | export_indices: []const u32, | |
| 1358 | ) error{OutOfMemory}!void { | |
| 1359 | const zcu = pt.zcu; | |
| 1360 | const gpa = zcu.gpa; | |
| 1361 | ||
| 1362 | for (export_indices) |export_idx| { | |
| 1363 | const new_export = &zcu.all_exports.items[export_idx]; | |
| 1364 | const gop = try symbol_exports.getOrPut(gpa, new_export.opts.name); | |
| 1365 | if (gop.found_existing) { | |
| 1366 | new_export.status = .failed_retryable; | |
| 1367 | try zcu.failed_exports.ensureUnusedCapacity(gpa, 1); | |
| 1368 | const msg = try Zcu.ErrorMsg.create(gpa, new_export.src, "exported symbol collision: {}", .{ | |
| 1369 | new_export.opts.name.fmt(&zcu.intern_pool), | |
| 1370 | }); | |
| 1371 | errdefer msg.destroy(gpa); | |
| 1372 | const other_export = zcu.all_exports.items[gop.value_ptr.*]; | |
| 1373 | try zcu.errNote(other_export.src, msg, "other symbol here", .{}); | |
| 1374 | zcu.failed_exports.putAssumeCapacityNoClobber(export_idx, msg); | |
| 1375 | new_export.status = .failed; | |
| 1376 | } else { | |
| 1377 | gop.value_ptr.* = export_idx; | |
| 1378 | } | |
| 1379 | } | |
| 1380 | if (zcu.comp.bin_file) |lf| { | |
| 1381 | try zcu.handleUpdateExports(export_indices, lf.updateExports(pt, exported, export_indices)); | |
| 1382 | } else if (zcu.llvm_object) |llvm_object| { | |
| 1383 | if (build_options.only_c) unreachable; | |
| 1384 | try zcu.handleUpdateExports(export_indices, llvm_object.updateExports(pt, exported, export_indices)); | |
| 1385 | } | |
| 1386 | } | |
| 1387 | ||
| 1388 | pub fn populateTestFunctions( | |
| 1389 | pt: Zcu.PerThread, | |
| 1390 | main_progress_node: std.Progress.Node, | |
| 1391 | ) !void { | |
| 1392 | const zcu = pt.zcu; | |
| 1393 | const gpa = zcu.gpa; | |
| 1394 | const ip = &zcu.intern_pool; | |
| 1395 | const builtin_mod = zcu.root_mod.getBuiltinDependency(); | |
| 1396 | const builtin_file_index = (zcu.importPkg(builtin_mod) catch unreachable).file_index; | |
| 1397 | const root_decl_index = zcu.fileRootDecl(builtin_file_index); | |
| 1398 | const root_decl = zcu.declPtr(root_decl_index.unwrap().?); | |
| 1399 | const builtin_namespace = zcu.namespacePtr(root_decl.src_namespace); | |
| 1400 | const test_functions_str = try ip.getOrPutString(gpa, "test_functions", .no_embedded_nulls); | |
| 1401 | const decl_index = builtin_namespace.decls.getKeyAdapted( | |
| 1402 | test_functions_str, | |
| 1403 | Zcu.DeclAdapter{ .zcu = zcu }, | |
| 1404 | ).?; | |
| 1405 | { | |
| 1406 | // We have to call `ensureDeclAnalyzed` here in case `builtin.test_functions` | |
| 1407 | // was not referenced by start code. | |
| 1408 | zcu.sema_prog_node = main_progress_node.start("Semantic Analysis", 0); | |
| 1409 | defer { | |
| 1410 | zcu.sema_prog_node.end(); | |
| 1411 | zcu.sema_prog_node = undefined; | |
| 1412 | } | |
| 1413 | try pt.ensureDeclAnalyzed(decl_index); | |
| 1414 | } | |
| 1415 | ||
| 1416 | const decl = zcu.declPtr(decl_index); | |
| 1417 | const test_fn_ty = decl.typeOf(zcu).slicePtrFieldType(zcu).childType(zcu); | |
| 1418 | ||
| 1419 | const array_anon_decl: InternPool.Key.Ptr.BaseAddr.AnonDecl = array: { | |
| 1420 | // Add zcu.test_functions to an array decl then make the test_functions | |
| 1421 | // decl reference it as a slice. | |
| 1422 | const test_fn_vals = try gpa.alloc(InternPool.Index, zcu.test_functions.count()); | |
| 1423 | defer gpa.free(test_fn_vals); | |
| 1424 | ||
| 1425 | for (test_fn_vals, zcu.test_functions.keys()) |*test_fn_val, test_decl_index| { | |
| 1426 | const test_decl = zcu.declPtr(test_decl_index); | |
| 1427 | const test_decl_name = try test_decl.fullyQualifiedName(zcu); | |
| 1428 | const test_decl_name_len = test_decl_name.length(ip); | |
| 1429 | const test_name_anon_decl: InternPool.Key.Ptr.BaseAddr.AnonDecl = n: { | |
| 1430 | const test_name_ty = try pt.arrayType(.{ | |
| 1431 | .len = test_decl_name_len, | |
| 1432 | .child = .u8_type, | |
| 1433 | }); | |
| 1434 | const test_name_val = try pt.intern(.{ .aggregate = .{ | |
| 1435 | .ty = test_name_ty.toIntern(), | |
| 1436 | .storage = .{ .bytes = test_decl_name.toString() }, | |
| 1437 | } }); | |
| 1438 | break :n .{ | |
| 1439 | .orig_ty = (try pt.singleConstPtrType(test_name_ty)).toIntern(), | |
| 1440 | .val = test_name_val, | |
| 1441 | }; | |
| 1442 | }; | |
| 1443 | ||
| 1444 | const test_fn_fields = .{ | |
| 1445 | // name | |
| 1446 | try pt.intern(.{ .slice = .{ | |
| 1447 | .ty = .slice_const_u8_type, | |
| 1448 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 1449 | .ty = .manyptr_const_u8_type, | |
| 1450 | .base_addr = .{ .anon_decl = test_name_anon_decl }, | |
| 1451 | .byte_offset = 0, | |
| 1452 | } }), | |
| 1453 | .len = try pt.intern(.{ .int = .{ | |
| 1454 | .ty = .usize_type, | |
| 1455 | .storage = .{ .u64 = test_decl_name_len }, | |
| 1456 | } }), | |
| 1457 | } }), | |
| 1458 | // func | |
| 1459 | try pt.intern(.{ .ptr = .{ | |
| 1460 | .ty = try pt.intern(.{ .ptr_type = .{ | |
| 1461 | .child = test_decl.typeOf(zcu).toIntern(), | |
| 1462 | .flags = .{ | |
| 1463 | .is_const = true, | |
| 1464 | }, | |
| 1465 | } }), | |
| 1466 | .base_addr = .{ .decl = test_decl_index }, | |
| 1467 | .byte_offset = 0, | |
| 1468 | } }), | |
| 1469 | }; | |
| 1470 | test_fn_val.* = try pt.intern(.{ .aggregate = .{ | |
| 1471 | .ty = test_fn_ty.toIntern(), | |
| 1472 | .storage = .{ .elems = &test_fn_fields }, | |
| 1473 | } }); | |
| 1474 | } | |
| 1475 | ||
| 1476 | const array_ty = try pt.arrayType(.{ | |
| 1477 | .len = test_fn_vals.len, | |
| 1478 | .child = test_fn_ty.toIntern(), | |
| 1479 | .sentinel = .none, | |
| 1480 | }); | |
| 1481 | const array_val = try pt.intern(.{ .aggregate = .{ | |
| 1482 | .ty = array_ty.toIntern(), | |
| 1483 | .storage = .{ .elems = test_fn_vals }, | |
| 1484 | } }); | |
| 1485 | break :array .{ | |
| 1486 | .orig_ty = (try pt.singleConstPtrType(array_ty)).toIntern(), | |
| 1487 | .val = array_val, | |
| 1488 | }; | |
| 1489 | }; | |
| 1490 | ||
| 1491 | { | |
| 1492 | const new_ty = try pt.ptrType(.{ | |
| 1493 | .child = test_fn_ty.toIntern(), | |
| 1494 | .flags = .{ | |
| 1495 | .is_const = true, | |
| 1496 | .size = .Slice, | |
| 1497 | }, | |
| 1498 | }); | |
| 1499 | const new_val = decl.val; | |
| 1500 | const new_init = try pt.intern(.{ .slice = .{ | |
| 1501 | .ty = new_ty.toIntern(), | |
| 1502 | .ptr = try pt.intern(.{ .ptr = .{ | |
| 1503 | .ty = new_ty.slicePtrFieldType(zcu).toIntern(), | |
| 1504 | .base_addr = .{ .anon_decl = array_anon_decl }, | |
| 1505 | .byte_offset = 0, | |
| 1506 | } }), | |
| 1507 | .len = (try pt.intValue(Type.usize, zcu.test_functions.count())).toIntern(), | |
| 1508 | } }); | |
| 1509 | ip.mutateVarInit(decl.val.toIntern(), new_init); | |
| 1510 | ||
| 1511 | // Since we are replacing the Decl's value we must perform cleanup on the | |
| 1512 | // previous value. | |
| 1513 | decl.val = new_val; | |
| 1514 | decl.has_tv = true; | |
| 1515 | } | |
| 1516 | { | |
| 1517 | zcu.codegen_prog_node = main_progress_node.start("Code Generation", 0); | |
| 1518 | defer { | |
| 1519 | zcu.codegen_prog_node.end(); | |
| 1520 | zcu.codegen_prog_node = undefined; | |
| 1521 | } | |
| 1522 | ||
| 1523 | try pt.linkerUpdateDecl(decl_index); | |
| 1524 | } | |
| 1525 | } | |
| 1526 | ||
| 1527 | pub fn linkerUpdateDecl(pt: Zcu.PerThread, decl_index: Zcu.Decl.Index) !void { | |
| 1528 | const zcu = pt.zcu; | |
| 1529 | const comp = zcu.comp; | |
| 1530 | ||
| 1531 | const decl = zcu.declPtr(decl_index); | |
| 1532 | ||
| 1533 | const codegen_prog_node = zcu.codegen_prog_node.start((try decl.fullyQualifiedName(zcu)).toSlice(&zcu.intern_pool), 0); | |
| 1534 | defer codegen_prog_node.end(); | |
| 1535 | ||
| 1536 | if (comp.bin_file) |lf| { | |
| 1537 | lf.updateDecl(pt, decl_index) catch |err| switch (err) { | |
| 1538 | error.OutOfMemory => return error.OutOfMemory, | |
| 1539 | error.AnalysisFail => { | |
| 1540 | decl.analysis = .codegen_failure; | |
| 1541 | }, | |
| 1542 | else => { | |
| 1543 | const gpa = zcu.gpa; | |
| 1544 | try zcu.failed_analysis.ensureUnusedCapacity(gpa, 1); | |
| 1545 | zcu.failed_analysis.putAssumeCapacityNoClobber(InternPool.AnalUnit.wrap(.{ .decl = decl_index }), try Zcu.ErrorMsg.create( | |
| 1546 | gpa, | |
| 1547 | decl.navSrcLoc(zcu), | |
| 1548 | "unable to codegen: {s}", | |
| 1549 | .{@errorName(err)}, | |
| 1550 | )); | |
| 1551 | decl.analysis = .codegen_failure; | |
| 1552 | try zcu.retryable_failures.append(zcu.gpa, InternPool.AnalUnit.wrap(.{ .decl = decl_index })); | |
| 1553 | }, | |
| 1554 | }; | |
| 1555 | } else if (zcu.llvm_object) |llvm_object| { | |
| 1556 | if (build_options.only_c) unreachable; | |
| 1557 | llvm_object.updateDecl(pt, decl_index) catch |err| switch (err) { | |
| 1558 | error.OutOfMemory => return error.OutOfMemory, | |
| 1559 | }; | |
| 1560 | } | |
| 1561 | } | |
| 1562 | ||
| 1563 | /// Shortcut for calling `intern_pool.get`. | |
| 1564 | pub fn intern(pt: Zcu.PerThread, key: InternPool.Key) Allocator.Error!InternPool.Index { | |
| 1565 | return pt.zcu.intern_pool.get(pt.zcu.gpa, pt.tid, key); | |
| 1566 | } | |
| 1567 | ||
| 1568 | /// Shortcut for calling `intern_pool.getCoerced`. | |
| 1569 | pub fn getCoerced(pt: Zcu.PerThread, val: Value, new_ty: Type) Allocator.Error!Value { | |
| 1570 | return Value.fromInterned(try pt.zcu.intern_pool.getCoerced(pt.zcu.gpa, pt.tid, val.toIntern(), new_ty.toIntern())); | |
| 1571 | } | |
| 1572 | ||
| 1573 | pub fn intType(pt: Zcu.PerThread, signedness: std.builtin.Signedness, bits: u16) Allocator.Error!Type { | |
| 1574 | return Type.fromInterned(try pt.intern(.{ .int_type = .{ | |
| 1575 | .signedness = signedness, | |
| 1576 | .bits = bits, | |
| 1577 | } })); | |
| 1578 | } | |
| 1579 | ||
| 1580 | pub fn errorIntType(pt: Zcu.PerThread) std.mem.Allocator.Error!Type { | |
| 1581 | return pt.intType(.unsigned, pt.zcu.errorSetBits()); | |
| 1582 | } | |
| 1583 | ||
| 1584 | pub fn arrayType(pt: Zcu.PerThread, info: InternPool.Key.ArrayType) Allocator.Error!Type { | |
| 1585 | return Type.fromInterned(try pt.intern(.{ .array_type = info })); | |
| 1586 | } | |
| 1587 | ||
| 1588 | pub fn vectorType(pt: Zcu.PerThread, info: InternPool.Key.VectorType) Allocator.Error!Type { | |
| 1589 | return Type.fromInterned(try pt.intern(.{ .vector_type = info })); | |
| 1590 | } | |
| 1591 | ||
| 1592 | pub fn optionalType(pt: Zcu.PerThread, child_type: InternPool.Index) Allocator.Error!Type { | |
| 1593 | return Type.fromInterned(try pt.intern(.{ .opt_type = child_type })); | |
| 1594 | } | |
| 1595 | ||
| 1596 | pub fn ptrType(pt: Zcu.PerThread, info: InternPool.Key.PtrType) Allocator.Error!Type { | |
| 1597 | var canon_info = info; | |
| 1598 | ||
| 1599 | if (info.flags.size == .C) canon_info.flags.is_allowzero = true; | |
| 1600 | ||
| 1601 | // Canonicalize non-zero alignment. If it matches the ABI alignment of the pointee | |
| 1602 | // type, we change it to 0 here. If this causes an assertion trip because the | |
| 1603 | // pointee type needs to be resolved more, that needs to be done before calling | |
| 1604 | // this ptr() function. | |
| 1605 | if (info.flags.alignment != .none and | |
| 1606 | info.flags.alignment == Type.fromInterned(info.child).abiAlignment(pt)) | |
| 1607 | { | |
| 1608 | canon_info.flags.alignment = .none; | |
| 1609 | } | |
| 1610 | ||
| 1611 | switch (info.flags.vector_index) { | |
| 1612 | // Canonicalize host_size. If it matches the bit size of the pointee type, | |
| 1613 | // we change it to 0 here. If this causes an assertion trip, the pointee type | |
| 1614 | // needs to be resolved before calling this ptr() function. | |
| 1615 | .none => if (info.packed_offset.host_size != 0) { | |
| 1616 | const elem_bit_size = Type.fromInterned(info.child).bitSize(pt); | |
| 1617 | assert(info.packed_offset.bit_offset + elem_bit_size <= info.packed_offset.host_size * 8); | |
| 1618 | if (info.packed_offset.host_size * 8 == elem_bit_size) { | |
| 1619 | canon_info.packed_offset.host_size = 0; | |
| 1620 | } | |
| 1621 | }, | |
| 1622 | .runtime => {}, | |
| 1623 | _ => assert(@intFromEnum(info.flags.vector_index) < info.packed_offset.host_size), | |
| 1624 | } | |
| 1625 | ||
| 1626 | return Type.fromInterned(try pt.intern(.{ .ptr_type = canon_info })); | |
| 1627 | } | |
| 1628 | ||
| 1629 | /// Like `ptrType`, but if `info` specifies an `alignment`, first ensures the pointer | |
| 1630 | /// child type's alignment is resolved so that an invalid alignment is not used. | |
| 1631 | /// In general, prefer this function during semantic analysis. | |
| 1632 | pub fn ptrTypeSema(pt: Zcu.PerThread, info: InternPool.Key.PtrType) Zcu.SemaError!Type { | |
| 1633 | if (info.flags.alignment != .none) { | |
| 1634 | _ = try Type.fromInterned(info.child).abiAlignmentAdvanced(pt, .sema); | |
| 1635 | } | |
| 1636 | return pt.ptrType(info); | |
| 1637 | } | |
| 1638 | ||
| 1639 | pub fn singleMutPtrType(pt: Zcu.PerThread, child_type: Type) Allocator.Error!Type { | |
| 1640 | return pt.ptrType(.{ .child = child_type.toIntern() }); | |
| 1641 | } | |
| 1642 | ||
| 1643 | pub fn singleConstPtrType(pt: Zcu.PerThread, child_type: Type) Allocator.Error!Type { | |
| 1644 | return pt.ptrType(.{ | |
| 1645 | .child = child_type.toIntern(), | |
| 1646 | .flags = .{ | |
| 1647 | .is_const = true, | |
| 1648 | }, | |
| 1649 | }); | |
| 1650 | } | |
| 1651 | ||
| 1652 | pub fn manyConstPtrType(pt: Zcu.PerThread, child_type: Type) Allocator.Error!Type { | |
| 1653 | return pt.ptrType(.{ | |
| 1654 | .child = child_type.toIntern(), | |
| 1655 | .flags = .{ | |
| 1656 | .size = .Many, | |
| 1657 | .is_const = true, | |
| 1658 | }, | |
| 1659 | }); | |
| 1660 | } | |
| 1661 | ||
| 1662 | pub fn adjustPtrTypeChild(pt: Zcu.PerThread, ptr_ty: Type, new_child: Type) Allocator.Error!Type { | |
| 1663 | var info = ptr_ty.ptrInfo(pt.zcu); | |
| 1664 | info.child = new_child.toIntern(); | |
| 1665 | return pt.ptrType(info); | |
| 1666 | } | |
| 1667 | ||
| 1668 | pub fn funcType(pt: Zcu.PerThread, key: InternPool.GetFuncTypeKey) Allocator.Error!Type { | |
| 1669 | return Type.fromInterned(try pt.zcu.intern_pool.getFuncType(pt.zcu.gpa, pt.tid, key)); | |
| 1670 | } | |
| 1671 | ||
| 1672 | /// Use this for `anyframe->T` only. | |
| 1673 | /// For `anyframe`, use the `InternPool.Index.anyframe` tag directly. | |
| 1674 | pub fn anyframeType(pt: Zcu.PerThread, payload_ty: Type) Allocator.Error!Type { | |
| 1675 | return Type.fromInterned(try pt.intern(.{ .anyframe_type = payload_ty.toIntern() })); | |
| 1676 | } | |
| 1677 | ||
| 1678 | pub fn errorUnionType(pt: Zcu.PerThread, error_set_ty: Type, payload_ty: Type) Allocator.Error!Type { | |
| 1679 | return Type.fromInterned(try pt.intern(.{ .error_union_type = .{ | |
| 1680 | .error_set_type = error_set_ty.toIntern(), | |
| 1681 | .payload_type = payload_ty.toIntern(), | |
| 1682 | } })); | |
| 1683 | } | |
| 1684 | ||
| 1685 | pub fn singleErrorSetType(pt: Zcu.PerThread, name: InternPool.NullTerminatedString) Allocator.Error!Type { | |
| 1686 | const names: *const [1]InternPool.NullTerminatedString = &name; | |
| 1687 | return Type.fromInterned(try pt.zcu.intern_pool.getErrorSetType(pt.zcu.gpa, pt.tid, names)); | |
| 1688 | } | |
| 1689 | ||
| 1690 | /// Sorts `names` in place. | |
| 1691 | pub fn errorSetFromUnsortedNames( | |
| 1692 | pt: Zcu.PerThread, | |
| 1693 | names: []InternPool.NullTerminatedString, | |
| 1694 | ) Allocator.Error!Type { | |
| 1695 | std.mem.sort( | |
| 1696 | InternPool.NullTerminatedString, | |
| 1697 | names, | |
| 1698 | {}, | |
| 1699 | InternPool.NullTerminatedString.indexLessThan, | |
| 1700 | ); | |
| 1701 | const new_ty = try pt.zcu.intern_pool.getErrorSetType(pt.zcu.gpa, pt.tid, names); | |
| 1702 | return Type.fromInterned(new_ty); | |
| 1703 | } | |
| 1704 | ||
| 1705 | /// Supports only pointers, not pointer-like optionals. | |
| 1706 | pub fn ptrIntValue(pt: Zcu.PerThread, ty: Type, x: u64) Allocator.Error!Value { | |
| 1707 | const mod = pt.zcu; | |
| 1708 | assert(ty.zigTypeTag(mod) == .Pointer and !ty.isSlice(mod)); | |
| 1709 | assert(x != 0 or ty.isAllowzeroPtr(mod)); | |
| 1710 | return Value.fromInterned(try pt.intern(.{ .ptr = .{ | |
| 1711 | .ty = ty.toIntern(), | |
| 1712 | .base_addr = .int, | |
| 1713 | .byte_offset = x, | |
| 1714 | } })); | |
| 1715 | } | |
| 1716 | ||
| 1717 | /// Creates an enum tag value based on the integer tag value. | |
| 1718 | pub fn enumValue(pt: Zcu.PerThread, ty: Type, tag_int: InternPool.Index) Allocator.Error!Value { | |
| 1719 | if (std.debug.runtime_safety) { | |
| 1720 | const tag = ty.zigTypeTag(pt.zcu); | |
| 1721 | assert(tag == .Enum); | |
| 1722 | } | |
| 1723 | return Value.fromInterned(try pt.intern(.{ .enum_tag = .{ | |
| 1724 | .ty = ty.toIntern(), | |
| 1725 | .int = tag_int, | |
| 1726 | } })); | |
| 1727 | } | |
| 1728 | ||
| 1729 | /// Creates an enum tag value based on the field index according to source code | |
| 1730 | /// declaration order. | |
| 1731 | pub fn enumValueFieldIndex(pt: Zcu.PerThread, ty: Type, field_index: u32) Allocator.Error!Value { | |
| 1732 | const ip = &pt.zcu.intern_pool; | |
| 1733 | const enum_type = ip.loadEnumType(ty.toIntern()); | |
| 1734 | ||
| 1735 | if (enum_type.values.len == 0) { | |
| 1736 | // Auto-numbered fields. | |
| 1737 | return Value.fromInterned(try pt.intern(.{ .enum_tag = .{ | |
| 1738 | .ty = ty.toIntern(), | |
| 1739 | .int = try pt.intern(.{ .int = .{ | |
| 1740 | .ty = enum_type.tag_ty, | |
| 1741 | .storage = .{ .u64 = field_index }, | |
| 1742 | } }), | |
| 1743 | } })); | |
| 1744 | } | |
| 1745 | ||
| 1746 | return Value.fromInterned(try pt.intern(.{ .enum_tag = .{ | |
| 1747 | .ty = ty.toIntern(), | |
| 1748 | .int = enum_type.values.get(ip)[field_index], | |
| 1749 | } })); | |
| 1750 | } | |
| 1751 | ||
| 1752 | pub fn undefValue(pt: Zcu.PerThread, ty: Type) Allocator.Error!Value { | |
| 1753 | return Value.fromInterned(try pt.intern(.{ .undef = ty.toIntern() })); | |
| 1754 | } | |
| 1755 | ||
| 1756 | pub fn undefRef(pt: Zcu.PerThread, ty: Type) Allocator.Error!Air.Inst.Ref { | |
| 1757 | return Air.internedToRef((try pt.undefValue(ty)).toIntern()); | |
| 1758 | } | |
| 1759 | ||
| 1760 | pub fn intValue(pt: Zcu.PerThread, ty: Type, x: anytype) Allocator.Error!Value { | |
| 1761 | if (std.math.cast(u64, x)) |casted| return pt.intValue_u64(ty, casted); | |
| 1762 | if (std.math.cast(i64, x)) |casted| return pt.intValue_i64(ty, casted); | |
| 1763 | var limbs_buffer: [4]usize = undefined; | |
| 1764 | var big_int = BigIntMutable.init(&limbs_buffer, x); | |
| 1765 | return pt.intValue_big(ty, big_int.toConst()); | |
| 1766 | } | |
| 1767 | ||
| 1768 | pub fn intRef(pt: Zcu.PerThread, ty: Type, x: anytype) Allocator.Error!Air.Inst.Ref { | |
| 1769 | return Air.internedToRef((try pt.intValue(ty, x)).toIntern()); | |
| 1770 | } | |
| 1771 | ||
| 1772 | pub fn intValue_big(pt: Zcu.PerThread, ty: Type, x: BigIntConst) Allocator.Error!Value { | |
| 1773 | return Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1774 | .ty = ty.toIntern(), | |
| 1775 | .storage = .{ .big_int = x }, | |
| 1776 | } })); | |
| 1777 | } | |
| 1778 | ||
| 1779 | pub fn intValue_u64(pt: Zcu.PerThread, ty: Type, x: u64) Allocator.Error!Value { | |
| 1780 | return Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1781 | .ty = ty.toIntern(), | |
| 1782 | .storage = .{ .u64 = x }, | |
| 1783 | } })); | |
| 1784 | } | |
| 1785 | ||
| 1786 | pub fn intValue_i64(pt: Zcu.PerThread, ty: Type, x: i64) Allocator.Error!Value { | |
| 1787 | return Value.fromInterned(try pt.intern(.{ .int = .{ | |
| 1788 | .ty = ty.toIntern(), | |
| 1789 | .storage = .{ .i64 = x }, | |
| 1790 | } })); | |
| 1791 | } | |
| 1792 | ||
| 1793 | pub fn unionValue(pt: Zcu.PerThread, union_ty: Type, tag: Value, val: Value) Allocator.Error!Value { | |
| 1794 | return Value.fromInterned(try pt.intern(.{ .un = .{ | |
| 1795 | .ty = union_ty.toIntern(), | |
| 1796 | .tag = tag.toIntern(), | |
| 1797 | .val = val.toIntern(), | |
| 1798 | } })); | |
| 1799 | } | |
| 1800 | ||
| 1801 | /// This function casts the float representation down to the representation of the type, potentially | |
| 1802 | /// losing data if the representation wasn't correct. | |
| 1803 | pub fn floatValue(pt: Zcu.PerThread, ty: Type, x: anytype) Allocator.Error!Value { | |
| 1804 | const storage: InternPool.Key.Float.Storage = switch (ty.floatBits(pt.zcu.getTarget())) { | |
| 1805 | 16 => .{ .f16 = @as(f16, @floatCast(x)) }, | |
| 1806 | 32 => .{ .f32 = @as(f32, @floatCast(x)) }, | |
| 1807 | 64 => .{ .f64 = @as(f64, @floatCast(x)) }, | |
| 1808 | 80 => .{ .f80 = @as(f80, @floatCast(x)) }, | |
| 1809 | 128 => .{ .f128 = @as(f128, @floatCast(x)) }, | |
| 1810 | else => unreachable, | |
| 1811 | }; | |
| 1812 | return Value.fromInterned(try pt.intern(.{ .float = .{ | |
| 1813 | .ty = ty.toIntern(), | |
| 1814 | .storage = storage, | |
| 1815 | } })); | |
| 1816 | } | |
| 1817 | ||
| 1818 | pub fn nullValue(pt: Zcu.PerThread, opt_ty: Type) Allocator.Error!Value { | |
| 1819 | assert(pt.zcu.intern_pool.isOptionalType(opt_ty.toIntern())); | |
| 1820 | return Value.fromInterned(try pt.intern(.{ .opt = .{ | |
| 1821 | .ty = opt_ty.toIntern(), | |
| 1822 | .val = .none, | |
| 1823 | } })); | |
| 1824 | } | |
| 1825 | ||
| 1826 | pub fn smallestUnsignedInt(pt: Zcu.PerThread, max: u64) Allocator.Error!Type { | |
| 1827 | return pt.intType(.unsigned, Type.smallestUnsignedBits(max)); | |
| 1828 | } | |
| 1829 | ||
| 1830 | /// Returns the smallest possible integer type containing both `min` and | |
| 1831 | /// `max`. Asserts that neither value is undef. | |
| 1832 | /// TODO: if #3806 is implemented, this becomes trivial | |
| 1833 | pub fn intFittingRange(pt: Zcu.PerThread, min: Value, max: Value) !Type { | |
| 1834 | const mod = pt.zcu; | |
| 1835 | assert(!min.isUndef(mod)); | |
| 1836 | assert(!max.isUndef(mod)); | |
| 1837 | ||
| 1838 | if (std.debug.runtime_safety) { | |
| 1839 | assert(Value.order(min, max, pt).compare(.lte)); | |
| 1840 | } | |
| 1841 | ||
| 1842 | const sign = min.orderAgainstZero(pt) == .lt; | |
| 1843 | ||
| 1844 | const min_val_bits = pt.intBitsForValue(min, sign); | |
| 1845 | const max_val_bits = pt.intBitsForValue(max, sign); | |
| 1846 | ||
| 1847 | return pt.intType( | |
| 1848 | if (sign) .signed else .unsigned, | |
| 1849 | @max(min_val_bits, max_val_bits), | |
| 1850 | ); | |
| 1851 | } | |
| 1852 | ||
| 1853 | /// Given a value representing an integer, returns the number of bits necessary to represent | |
| 1854 | /// this value in an integer. If `sign` is true, returns the number of bits necessary in a | |
| 1855 | /// twos-complement integer; otherwise in an unsigned integer. | |
| 1856 | /// Asserts that `val` is not undef. If `val` is negative, asserts that `sign` is true. | |
| 1857 | pub fn intBitsForValue(pt: Zcu.PerThread, val: Value, sign: bool) u16 { | |
| 1858 | const mod = pt.zcu; | |
| 1859 | assert(!val.isUndef(mod)); | |
| 1860 | ||
| 1861 | const key = mod.intern_pool.indexToKey(val.toIntern()); | |
| 1862 | switch (key.int.storage) { | |
| 1863 | .i64 => |x| { | |
| 1864 | if (std.math.cast(u64, x)) |casted| return Type.smallestUnsignedBits(casted) + @intFromBool(sign); | |
| 1865 | assert(sign); | |
| 1866 | // Protect against overflow in the following negation. | |
| 1867 | if (x == std.math.minInt(i64)) return 64; | |
| 1868 | return Type.smallestUnsignedBits(@as(u64, @intCast(-(x + 1)))) + 1; | |
| 1869 | }, | |
| 1870 | .u64 => |x| { | |
| 1871 | return Type.smallestUnsignedBits(x) + @intFromBool(sign); | |
| 1872 | }, | |
| 1873 | .big_int => |big| { | |
| 1874 | if (big.positive) return @as(u16, @intCast(big.bitCountAbs() + @intFromBool(sign))); | |
| 1875 | ||
| 1876 | // Zero is still a possibility, in which case unsigned is fine | |
| 1877 | if (big.eqlZero()) return 0; | |
| 1878 | ||
| 1879 | return @as(u16, @intCast(big.bitCountTwosComp())); | |
| 1880 | }, | |
| 1881 | .lazy_align => |lazy_ty| { | |
| 1882 | return Type.smallestUnsignedBits(Type.fromInterned(lazy_ty).abiAlignment(pt).toByteUnits() orelse 0) + @intFromBool(sign); | |
| 1883 | }, | |
| 1884 | .lazy_size => |lazy_ty| { | |
| 1885 | return Type.smallestUnsignedBits(Type.fromInterned(lazy_ty).abiSize(pt)) + @intFromBool(sign); | |
| 1886 | }, | |
| 1887 | } | |
| 1888 | } | |
| 1889 | ||
| 1890 | pub fn getUnionLayout(pt: Zcu.PerThread, loaded_union: InternPool.LoadedUnionType) Zcu.UnionLayout { | |
| 1891 | const mod = pt.zcu; | |
| 1892 | const ip = &mod.intern_pool; | |
| 1893 | assert(loaded_union.haveLayout(ip)); | |
| 1894 | var most_aligned_field: u32 = undefined; | |
| 1895 | var most_aligned_field_size: u64 = undefined; | |
| 1896 | var biggest_field: u32 = undefined; | |
| 1897 | var payload_size: u64 = 0; | |
| 1898 | var payload_align: InternPool.Alignment = .@"1"; | |
| 1899 | for (loaded_union.field_types.get(ip), 0..) |field_ty, field_index| { | |
| 1900 | if (!Type.fromInterned(field_ty).hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 1901 | ||
| 1902 | const explicit_align = loaded_union.fieldAlign(ip, field_index); | |
| 1903 | const field_align = if (explicit_align != .none) | |
| 1904 | explicit_align | |
| 1905 | else | |
| 1906 | Type.fromInterned(field_ty).abiAlignment(pt); | |
| 1907 | const field_size = Type.fromInterned(field_ty).abiSize(pt); | |
| 1908 | if (field_size > payload_size) { | |
| 1909 | payload_size = field_size; | |
| 1910 | biggest_field = @intCast(field_index); | |
| 1911 | } | |
| 1912 | if (field_align.compare(.gte, payload_align)) { | |
| 1913 | payload_align = field_align; | |
| 1914 | most_aligned_field = @intCast(field_index); | |
| 1915 | most_aligned_field_size = field_size; | |
| 1916 | } | |
| 1917 | } | |
| 1918 | const have_tag = loaded_union.flagsPtr(ip).runtime_tag.hasTag(); | |
| 1919 | if (!have_tag or !Type.fromInterned(loaded_union.enum_tag_ty).hasRuntimeBits(pt)) { | |
| 1920 | return .{ | |
| 1921 | .abi_size = payload_align.forward(payload_size), | |
| 1922 | .abi_align = payload_align, | |
| 1923 | .most_aligned_field = most_aligned_field, | |
| 1924 | .most_aligned_field_size = most_aligned_field_size, | |
| 1925 | .biggest_field = biggest_field, | |
| 1926 | .payload_size = payload_size, | |
| 1927 | .payload_align = payload_align, | |
| 1928 | .tag_align = .none, | |
| 1929 | .tag_size = 0, | |
| 1930 | .padding = 0, | |
| 1931 | }; | |
| 1932 | } | |
| 1933 | ||
| 1934 | const tag_size = Type.fromInterned(loaded_union.enum_tag_ty).abiSize(pt); | |
| 1935 | const tag_align = Type.fromInterned(loaded_union.enum_tag_ty).abiAlignment(pt).max(.@"1"); | |
| 1936 | return .{ | |
| 1937 | .abi_size = loaded_union.size(ip).*, | |
| 1938 | .abi_align = tag_align.max(payload_align), | |
| 1939 | .most_aligned_field = most_aligned_field, | |
| 1940 | .most_aligned_field_size = most_aligned_field_size, | |
| 1941 | .biggest_field = biggest_field, | |
| 1942 | .payload_size = payload_size, | |
| 1943 | .payload_align = payload_align, | |
| 1944 | .tag_align = tag_align, | |
| 1945 | .tag_size = tag_size, | |
| 1946 | .padding = loaded_union.padding(ip).*, | |
| 1947 | }; | |
| 1948 | } | |
| 1949 | ||
| 1950 | pub fn unionAbiSize(mod: *Module, loaded_union: InternPool.LoadedUnionType) u64 { | |
| 1951 | return mod.getUnionLayout(loaded_union).abi_size; | |
| 1952 | } | |
| 1953 | ||
| 1954 | /// Returns 0 if the union is represented with 0 bits at runtime. | |
| 1955 | pub fn unionAbiAlignment(pt: Zcu.PerThread, loaded_union: InternPool.LoadedUnionType) InternPool.Alignment { | |
| 1956 | const mod = pt.zcu; | |
| 1957 | const ip = &mod.intern_pool; | |
| 1958 | const have_tag = loaded_union.flagsPtr(ip).runtime_tag.hasTag(); | |
| 1959 | var max_align: InternPool.Alignment = .none; | |
| 1960 | if (have_tag) max_align = Type.fromInterned(loaded_union.enum_tag_ty).abiAlignment(pt); | |
| 1961 | for (loaded_union.field_types.get(ip), 0..) |field_ty, field_index| { | |
| 1962 | if (!Type.fromInterned(field_ty).hasRuntimeBits(pt)) continue; | |
| 1963 | ||
| 1964 | const field_align = mod.unionFieldNormalAlignment(loaded_union, @intCast(field_index)); | |
| 1965 | max_align = max_align.max(field_align); | |
| 1966 | } | |
| 1967 | return max_align; | |
| 1968 | } | |
| 1969 | ||
| 1970 | /// Returns the field alignment of a non-packed union. Asserts the layout is not packed. | |
| 1971 | pub fn unionFieldNormalAlignment( | |
| 1972 | pt: Zcu.PerThread, | |
| 1973 | loaded_union: InternPool.LoadedUnionType, | |
| 1974 | field_index: u32, | |
| 1975 | ) InternPool.Alignment { | |
| 1976 | return pt.unionFieldNormalAlignmentAdvanced(loaded_union, field_index, .normal) catch unreachable; | |
| 1977 | } | |
| 1978 | ||
| 1979 | /// Returns the field alignment of a non-packed union. Asserts the layout is not packed. | |
| 1980 | /// If `strat` is `.sema`, may perform type resolution. | |
| 1981 | pub fn unionFieldNormalAlignmentAdvanced( | |
| 1982 | pt: Zcu.PerThread, | |
| 1983 | loaded_union: InternPool.LoadedUnionType, | |
| 1984 | field_index: u32, | |
| 1985 | strat: Type.ResolveStrat, | |
| 1986 | ) Zcu.SemaError!InternPool.Alignment { | |
| 1987 | const ip = &pt.zcu.intern_pool; | |
| 1988 | assert(loaded_union.flagsPtr(ip).layout != .@"packed"); | |
| 1989 | const field_align = loaded_union.fieldAlign(ip, field_index); | |
| 1990 | if (field_align != .none) return field_align; | |
| 1991 | const field_ty = Type.fromInterned(loaded_union.field_types.get(ip)[field_index]); | |
| 1992 | if (field_ty.isNoReturn(pt.zcu)) return .none; | |
| 1993 | return (try field_ty.abiAlignmentAdvanced(pt, strat.toLazy())).scalar; | |
| 1994 | } | |
| 1995 | ||
| 1996 | /// Returns the field alignment of a non-packed struct. Asserts the layout is not packed. | |
| 1997 | pub fn structFieldAlignment( | |
| 1998 | pt: Zcu.PerThread, | |
| 1999 | explicit_alignment: InternPool.Alignment, | |
| 2000 | field_ty: Type, | |
| 2001 | layout: std.builtin.Type.ContainerLayout, | |
| 2002 | ) InternPool.Alignment { | |
| 2003 | return pt.structFieldAlignmentAdvanced(explicit_alignment, field_ty, layout, .normal) catch unreachable; | |
| 2004 | } | |
| 2005 | ||
| 2006 | /// Returns the field alignment of a non-packed struct. Asserts the layout is not packed. | |
| 2007 | /// If `strat` is `.sema`, may perform type resolution. | |
| 2008 | pub fn structFieldAlignmentAdvanced( | |
| 2009 | pt: Zcu.PerThread, | |
| 2010 | explicit_alignment: InternPool.Alignment, | |
| 2011 | field_ty: Type, | |
| 2012 | layout: std.builtin.Type.ContainerLayout, | |
| 2013 | strat: Type.ResolveStrat, | |
| 2014 | ) Zcu.SemaError!InternPool.Alignment { | |
| 2015 | assert(layout != .@"packed"); | |
| 2016 | if (explicit_alignment != .none) return explicit_alignment; | |
| 2017 | const ty_abi_align = (try field_ty.abiAlignmentAdvanced(pt, strat.toLazy())).scalar; | |
| 2018 | switch (layout) { | |
| 2019 | .@"packed" => unreachable, | |
| 2020 | .auto => if (pt.zcu.getTarget().ofmt != .c) return ty_abi_align, | |
| 2021 | .@"extern" => {}, | |
| 2022 | } | |
| 2023 | // extern | |
| 2024 | if (field_ty.isAbiInt(pt.zcu) and field_ty.intInfo(pt.zcu).bits >= 128) { | |
| 2025 | return ty_abi_align.maxStrict(.@"16"); | |
| 2026 | } | |
| 2027 | return ty_abi_align; | |
| 2028 | } | |
| 2029 | ||
| 2030 | /// https://github.com/ziglang/zig/issues/17178 explored storing these bit offsets | |
| 2031 | /// into the packed struct InternPool data rather than computing this on the | |
| 2032 | /// fly, however it was found to perform worse when measured on real world | |
| 2033 | /// projects. | |
| 2034 | pub fn structPackedFieldBitOffset( | |
| 2035 | pt: Zcu.PerThread, | |
| 2036 | struct_type: InternPool.LoadedStructType, | |
| 2037 | field_index: u32, | |
| 2038 | ) u16 { | |
| 2039 | const mod = pt.zcu; | |
| 2040 | const ip = &mod.intern_pool; | |
| 2041 | assert(struct_type.layout == .@"packed"); | |
| 2042 | assert(struct_type.haveLayout(ip)); | |
| 2043 | var bit_sum: u64 = 0; | |
| 2044 | for (0..struct_type.field_types.len) |i| { | |
| 2045 | if (i == field_index) { | |
| 2046 | return @intCast(bit_sum); | |
| 2047 | } | |
| 2048 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[i]); | |
| 2049 | bit_sum += field_ty.bitSize(pt); | |
| 2050 | } | |
| 2051 | unreachable; // index out of bounds | |
| 2052 | } | |
| 2053 | ||
| 2054 | pub fn getBuiltin(pt: Zcu.PerThread, name: []const u8) Allocator.Error!Air.Inst.Ref { | |
| 2055 | const decl_index = try pt.getBuiltinDecl(name); | |
| 2056 | pt.ensureDeclAnalyzed(decl_index) catch @panic("std.builtin is corrupt"); | |
| 2057 | return Air.internedToRef(pt.zcu.declPtr(decl_index).val.toIntern()); | |
| 2058 | } | |
| 2059 | ||
| 2060 | pub fn getBuiltinDecl(pt: Zcu.PerThread, name: []const u8) Allocator.Error!InternPool.DeclIndex { | |
| 2061 | const zcu = pt.zcu; | |
| 2062 | const gpa = zcu.gpa; | |
| 2063 | const ip = &zcu.intern_pool; | |
| 2064 | const std_file_imported = zcu.importPkg(zcu.std_mod) catch @panic("failed to import lib/std.zig"); | |
| 2065 | const std_file_root_decl = zcu.fileRootDecl(std_file_imported.file_index).unwrap().?; | |
| 2066 | const std_namespace = zcu.declPtr(std_file_root_decl).getOwnedInnerNamespace(zcu).?; | |
| 2067 | const builtin_str = try ip.getOrPutString(gpa, "builtin", .no_embedded_nulls); | |
| 2068 | const builtin_decl = std_namespace.decls.getKeyAdapted(builtin_str, Zcu.DeclAdapter{ .zcu = zcu }) orelse @panic("lib/std.zig is corrupt and missing 'builtin'"); | |
| 2069 | pt.ensureDeclAnalyzed(builtin_decl) catch @panic("std.builtin is corrupt"); | |
| 2070 | const builtin_namespace = zcu.declPtr(builtin_decl).getInnerNamespace(zcu) orelse @panic("std.builtin is corrupt"); | |
| 2071 | const name_str = try ip.getOrPutString(gpa, name, .no_embedded_nulls); | |
| 2072 | return builtin_namespace.decls.getKeyAdapted(name_str, Zcu.DeclAdapter{ .zcu = zcu }) orelse @panic("lib/std/builtin.zig is corrupt"); | |
| 2073 | } | |
| 2074 | ||
| 2075 | pub fn getBuiltinType(pt: Zcu.PerThread, name: []const u8) Allocator.Error!Type { | |
| 2076 | const ty_inst = try pt.getBuiltin(name); | |
| 2077 | const ty = Type.fromInterned(ty_inst.toInterned() orelse @panic("std.builtin is corrupt")); | |
| 2078 | ty.resolveFully(pt) catch @panic("std.builtin is corrupt"); | |
| 2079 | return ty; | |
| 2080 | } | |
| 2081 | ||
| 2082 | const Air = @import("../Air.zig"); | |
| 2083 | const Allocator = std.mem.Allocator; | |
| 2084 | const assert = std.debug.assert; | |
| 2085 | const BigIntConst = std.math.big.int.Const; | |
| 2086 | const BigIntMutable = std.math.big.int.Mutable; | |
| 2087 | const build_options = @import("build_options"); | |
| 2088 | const builtin = @import("builtin"); | |
| 2089 | const Cache = std.Build.Cache; | |
| 2090 | const InternPool = @import("../InternPool.zig"); | |
| 2091 | const isUpDir = @import("../introspect.zig").isUpDir; | |
| 2092 | const Liveness = @import("../Liveness.zig"); | |
| 2093 | const log = std.log.scoped(.zcu); | |
| 2094 | const Module = @import("../Package.zig").Module; | |
| 2095 | const Sema = @import("../Sema.zig"); | |
| 2096 | const std = @import("std"); | |
| 2097 | const target_util = @import("../target.zig"); | |
| 2098 | const trace = @import("../tracy.zig").trace; | |
| 2099 | const Type = @import("../Type.zig"); | |
| 2100 | const Value = @import("../Value.zig"); | |
| 2101 | const Zcu = @import("../Zcu.zig"); | |
| 2102 | const Zir = std.zig.Zir; |
src/arch/aarch64/CodeGen.zig+220-163| ... | ... | @@ -12,11 +12,9 @@ const Type = @import("../../Type.zig"); |
| 12 | 12 | const Value = @import("../../Value.zig"); |
| 13 | 13 | const link = @import("../../link.zig"); |
| 14 | 14 | const Zcu = @import("../../Zcu.zig"); |
| 15 | /// Deprecated. | |
| 16 | const Module = Zcu; | |
| 17 | 15 | const InternPool = @import("../../InternPool.zig"); |
| 18 | 16 | const Compilation = @import("../../Compilation.zig"); |
| 19 | const ErrorMsg = Module.ErrorMsg; | |
| 17 | const ErrorMsg = Zcu.ErrorMsg; | |
| 20 | 18 | const Target = std.Target; |
| 21 | 19 | const Allocator = mem.Allocator; |
| 22 | 20 | const trace = @import("../../tracy.zig").trace; |
| ... | ... | @@ -47,6 +45,7 @@ const gp = abi.RegisterClass.gp; |
| 47 | 45 | const InnerError = CodeGenError || error{OutOfRegisters}; |
| 48 | 46 | |
| 49 | 47 | gpa: Allocator, |
| 48 | pt: Zcu.PerThread, | |
| 50 | 49 | air: Air, |
| 51 | 50 | liveness: Liveness, |
| 52 | 51 | bin_file: *link.File, |
| ... | ... | @@ -59,7 +58,7 @@ args: []MCValue, |
| 59 | 58 | ret_mcv: MCValue, |
| 60 | 59 | fn_type: Type, |
| 61 | 60 | arg_index: u32, |
| 62 | src_loc: Module.LazySrcLoc, | |
| 61 | src_loc: Zcu.LazySrcLoc, | |
| 63 | 62 | stack_align: u32, |
| 64 | 63 | |
| 65 | 64 | /// MIR Instructions |
| ... | ... | @@ -331,15 +330,16 @@ const Self = @This(); |
| 331 | 330 | |
| 332 | 331 | pub fn generate( |
| 333 | 332 | lf: *link.File, |
| 334 | src_loc: Module.LazySrcLoc, | |
| 333 | pt: Zcu.PerThread, | |
| 334 | src_loc: Zcu.LazySrcLoc, | |
| 335 | 335 | func_index: InternPool.Index, |
| 336 | 336 | air: Air, |
| 337 | 337 | liveness: Liveness, |
| 338 | 338 | code: *std.ArrayList(u8), |
| 339 | 339 | debug_output: DebugInfoOutput, |
| 340 | 340 | ) CodeGenError!Result { |
| 341 | const gpa = lf.comp.gpa; | |
| 342 | const zcu = lf.comp.module.?; | |
| 341 | const zcu = pt.zcu; | |
| 342 | const gpa = zcu.gpa; | |
| 343 | 343 | const func = zcu.funcInfo(func_index); |
| 344 | 344 | const fn_owner_decl = zcu.declPtr(func.owner_decl); |
| 345 | 345 | assert(fn_owner_decl.has_tv); |
| ... | ... | @@ -355,8 +355,9 @@ pub fn generate( |
| 355 | 355 | } |
| 356 | 356 | try branch_stack.append(.{}); |
| 357 | 357 | |
| 358 | var function = Self{ | |
| 358 | var function: Self = .{ | |
| 359 | 359 | .gpa = gpa, |
| 360 | .pt = pt, | |
| 360 | 361 | .air = air, |
| 361 | 362 | .liveness = liveness, |
| 362 | 363 | .debug_output = debug_output, |
| ... | ... | @@ -476,7 +477,8 @@ pub fn addExtraAssumeCapacity(self: *Self, extra: anytype) u32 { |
| 476 | 477 | } |
| 477 | 478 | |
| 478 | 479 | fn gen(self: *Self) !void { |
| 479 | const mod = self.bin_file.comp.module.?; | |
| 480 | const pt = self.pt; | |
| 481 | const mod = pt.zcu; | |
| 480 | 482 | const cc = self.fn_type.fnCallingConvention(mod); |
| 481 | 483 | if (cc != .Naked) { |
| 482 | 484 | // stp fp, lr, [sp, #-16]! |
| ... | ... | @@ -526,8 +528,8 @@ fn gen(self: *Self) !void { |
| 526 | 528 | |
| 527 | 529 | const ty = self.typeOfIndex(inst); |
| 528 | 530 | |
| 529 | const abi_size = @as(u32, @intCast(ty.abiSize(mod))); | |
| 530 | const abi_align = ty.abiAlignment(mod); | |
| 531 | const abi_size = @as(u32, @intCast(ty.abiSize(pt))); | |
| 532 | const abi_align = ty.abiAlignment(pt); | |
| 531 | 533 | const stack_offset = try self.allocMem(abi_size, abi_align, inst); |
| 532 | 534 | try self.genSetStack(ty, stack_offset, MCValue{ .register = reg }); |
| 533 | 535 | |
| ... | ... | @@ -656,7 +658,8 @@ fn gen(self: *Self) !void { |
| 656 | 658 | } |
| 657 | 659 | |
| 658 | 660 | fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 659 | const mod = self.bin_file.comp.module.?; | |
| 661 | const pt = self.pt; | |
| 662 | const mod = pt.zcu; | |
| 660 | 663 | const ip = &mod.intern_pool; |
| 661 | 664 | const air_tags = self.air.instructions.items(.tag); |
| 662 | 665 | |
| ... | ... | @@ -1022,31 +1025,32 @@ fn allocMem( |
| 1022 | 1025 | |
| 1023 | 1026 | /// Use a pointer instruction as the basis for allocating stack memory. |
| 1024 | 1027 | fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 1025 | const mod = self.bin_file.comp.module.?; | |
| 1028 | const pt = self.pt; | |
| 1029 | const mod = pt.zcu; | |
| 1026 | 1030 | const elem_ty = self.typeOfIndex(inst).childType(mod); |
| 1027 | 1031 | |
| 1028 | if (!elem_ty.hasRuntimeBits(mod)) { | |
| 1032 | if (!elem_ty.hasRuntimeBits(pt)) { | |
| 1029 | 1033 | // return the stack offset 0. Stack offset 0 will be where all |
| 1030 | 1034 | // zero-sized stack allocations live as non-zero-sized |
| 1031 | 1035 | // allocations will always have an offset > 0. |
| 1032 | 1036 | return @as(u32, 0); |
| 1033 | 1037 | } |
| 1034 | 1038 | |
| 1035 | const abi_size = math.cast(u32, elem_ty.abiSize(mod)) orelse { | |
| 1036 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(mod)}); | |
| 1039 | const abi_size = math.cast(u32, elem_ty.abiSize(pt)) orelse { | |
| 1040 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(pt)}); | |
| 1037 | 1041 | }; |
| 1038 | 1042 | // TODO swap this for inst.ty.ptrAlign |
| 1039 | const abi_align = elem_ty.abiAlignment(mod); | |
| 1043 | const abi_align = elem_ty.abiAlignment(pt); | |
| 1040 | 1044 | |
| 1041 | 1045 | return self.allocMem(abi_size, abi_align, inst); |
| 1042 | 1046 | } |
| 1043 | 1047 | |
| 1044 | 1048 | fn allocRegOrMem(self: *Self, elem_ty: Type, reg_ok: bool, maybe_inst: ?Air.Inst.Index) !MCValue { |
| 1045 | const mod = self.bin_file.comp.module.?; | |
| 1046 | const abi_size = math.cast(u32, elem_ty.abiSize(mod)) orelse { | |
| 1047 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(mod)}); | |
| 1049 | const pt = self.pt; | |
| 1050 | const abi_size = math.cast(u32, elem_ty.abiSize(pt)) orelse { | |
| 1051 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(pt)}); | |
| 1048 | 1052 | }; |
| 1049 | const abi_align = elem_ty.abiAlignment(mod); | |
| 1053 | const abi_align = elem_ty.abiAlignment(pt); | |
| 1050 | 1054 | |
| 1051 | 1055 | if (reg_ok) { |
| 1052 | 1056 | // Make sure the type can fit in a register before we try to allocate one. |
| ... | ... | @@ -1133,14 +1137,15 @@ fn airAlloc(self: *Self, inst: Air.Inst.Index) !void { |
| 1133 | 1137 | } |
| 1134 | 1138 | |
| 1135 | 1139 | fn airRetPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 1136 | const mod = self.bin_file.comp.module.?; | |
| 1140 | const pt = self.pt; | |
| 1141 | const mod = pt.zcu; | |
| 1137 | 1142 | const result: MCValue = switch (self.ret_mcv) { |
| 1138 | 1143 | .none, .register => .{ .ptr_stack_offset = try self.allocMemPtr(inst) }, |
| 1139 | 1144 | .stack_offset => blk: { |
| 1140 | 1145 | // self.ret_mcv is an address to where this function |
| 1141 | 1146 | // should store its result into |
| 1142 | 1147 | const ret_ty = self.fn_type.fnReturnType(mod); |
| 1143 | const ptr_ty = try mod.singleMutPtrType(ret_ty); | |
| 1148 | const ptr_ty = try pt.singleMutPtrType(ret_ty); | |
| 1144 | 1149 | |
| 1145 | 1150 | // addr_reg will contain the address of where to store the |
| 1146 | 1151 | // result into |
| ... | ... | @@ -1170,7 +1175,8 @@ fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 1170 | 1175 | if (self.liveness.isUnused(inst)) |
| 1171 | 1176 | return self.finishAir(inst, .dead, .{ ty_op.operand, .none, .none }); |
| 1172 | 1177 | |
| 1173 | const mod = self.bin_file.comp.module.?; | |
| 1178 | const pt = self.pt; | |
| 1179 | const mod = pt.zcu; | |
| 1174 | 1180 | const operand = ty_op.operand; |
| 1175 | 1181 | const operand_mcv = try self.resolveInst(operand); |
| 1176 | 1182 | const operand_ty = self.typeOf(operand); |
| ... | ... | @@ -1251,7 +1257,8 @@ fn trunc( |
| 1251 | 1257 | operand_ty: Type, |
| 1252 | 1258 | dest_ty: Type, |
| 1253 | 1259 | ) !MCValue { |
| 1254 | const mod = self.bin_file.comp.module.?; | |
| 1260 | const pt = self.pt; | |
| 1261 | const mod = pt.zcu; | |
| 1255 | 1262 | const info_a = operand_ty.intInfo(mod); |
| 1256 | 1263 | const info_b = dest_ty.intInfo(mod); |
| 1257 | 1264 | |
| ... | ... | @@ -1314,7 +1321,8 @@ fn airIntFromBool(self: *Self, inst: Air.Inst.Index) !void { |
| 1314 | 1321 | |
| 1315 | 1322 | fn airNot(self: *Self, inst: Air.Inst.Index) !void { |
| 1316 | 1323 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1317 | const mod = self.bin_file.comp.module.?; | |
| 1324 | const pt = self.pt; | |
| 1325 | const mod = pt.zcu; | |
| 1318 | 1326 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 1319 | 1327 | const operand = try self.resolveInst(ty_op.operand); |
| 1320 | 1328 | const operand_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -1409,7 +1417,8 @@ fn minMax( |
| 1409 | 1417 | rhs_ty: Type, |
| 1410 | 1418 | maybe_inst: ?Air.Inst.Index, |
| 1411 | 1419 | ) !MCValue { |
| 1412 | const mod = self.bin_file.comp.module.?; | |
| 1420 | const pt = self.pt; | |
| 1421 | const mod = pt.zcu; | |
| 1413 | 1422 | switch (lhs_ty.zigTypeTag(mod)) { |
| 1414 | 1423 | .Float => return self.fail("TODO ARM min/max on floats", .{}), |
| 1415 | 1424 | .Vector => return self.fail("TODO ARM min/max on vectors", .{}), |
| ... | ... | @@ -1899,7 +1908,8 @@ fn addSub( |
| 1899 | 1908 | rhs_ty: Type, |
| 1900 | 1909 | maybe_inst: ?Air.Inst.Index, |
| 1901 | 1910 | ) InnerError!MCValue { |
| 1902 | const mod = self.bin_file.comp.module.?; | |
| 1911 | const pt = self.pt; | |
| 1912 | const mod = pt.zcu; | |
| 1903 | 1913 | switch (lhs_ty.zigTypeTag(mod)) { |
| 1904 | 1914 | .Float => return self.fail("TODO binary operations on floats", .{}), |
| 1905 | 1915 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| ... | ... | @@ -1960,7 +1970,8 @@ fn mul( |
| 1960 | 1970 | rhs_ty: Type, |
| 1961 | 1971 | maybe_inst: ?Air.Inst.Index, |
| 1962 | 1972 | ) InnerError!MCValue { |
| 1963 | const mod = self.bin_file.comp.module.?; | |
| 1973 | const pt = self.pt; | |
| 1974 | const mod = pt.zcu; | |
| 1964 | 1975 | switch (lhs_ty.zigTypeTag(mod)) { |
| 1965 | 1976 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| 1966 | 1977 | .Int => { |
| ... | ... | @@ -1992,7 +2003,8 @@ fn divFloat( |
| 1992 | 2003 | _ = rhs_ty; |
| 1993 | 2004 | _ = maybe_inst; |
| 1994 | 2005 | |
| 1995 | const mod = self.bin_file.comp.module.?; | |
| 2006 | const pt = self.pt; | |
| 2007 | const mod = pt.zcu; | |
| 1996 | 2008 | switch (lhs_ty.zigTypeTag(mod)) { |
| 1997 | 2009 | .Float => return self.fail("TODO div_float", .{}), |
| 1998 | 2010 | .Vector => return self.fail("TODO div_float on vectors", .{}), |
| ... | ... | @@ -2008,7 +2020,8 @@ fn divTrunc( |
| 2008 | 2020 | rhs_ty: Type, |
| 2009 | 2021 | maybe_inst: ?Air.Inst.Index, |
| 2010 | 2022 | ) InnerError!MCValue { |
| 2011 | const mod = self.bin_file.comp.module.?; | |
| 2023 | const pt = self.pt; | |
| 2024 | const mod = pt.zcu; | |
| 2012 | 2025 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2013 | 2026 | .Float => return self.fail("TODO div on floats", .{}), |
| 2014 | 2027 | .Vector => return self.fail("TODO div on vectors", .{}), |
| ... | ... | @@ -2042,7 +2055,8 @@ fn divFloor( |
| 2042 | 2055 | rhs_ty: Type, |
| 2043 | 2056 | maybe_inst: ?Air.Inst.Index, |
| 2044 | 2057 | ) InnerError!MCValue { |
| 2045 | const mod = self.bin_file.comp.module.?; | |
| 2058 | const pt = self.pt; | |
| 2059 | const mod = pt.zcu; | |
| 2046 | 2060 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2047 | 2061 | .Float => return self.fail("TODO div on floats", .{}), |
| 2048 | 2062 | .Vector => return self.fail("TODO div on vectors", .{}), |
| ... | ... | @@ -2075,7 +2089,8 @@ fn divExact( |
| 2075 | 2089 | rhs_ty: Type, |
| 2076 | 2090 | maybe_inst: ?Air.Inst.Index, |
| 2077 | 2091 | ) InnerError!MCValue { |
| 2078 | const mod = self.bin_file.comp.module.?; | |
| 2092 | const pt = self.pt; | |
| 2093 | const mod = pt.zcu; | |
| 2079 | 2094 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2080 | 2095 | .Float => return self.fail("TODO div on floats", .{}), |
| 2081 | 2096 | .Vector => return self.fail("TODO div on vectors", .{}), |
| ... | ... | @@ -2111,7 +2126,8 @@ fn rem( |
| 2111 | 2126 | ) InnerError!MCValue { |
| 2112 | 2127 | _ = maybe_inst; |
| 2113 | 2128 | |
| 2114 | const mod = self.bin_file.comp.module.?; | |
| 2129 | const pt = self.pt; | |
| 2130 | const mod = pt.zcu; | |
| 2115 | 2131 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2116 | 2132 | .Float => return self.fail("TODO rem/mod on floats", .{}), |
| 2117 | 2133 | .Vector => return self.fail("TODO rem/mod on vectors", .{}), |
| ... | ... | @@ -2182,7 +2198,8 @@ fn modulo( |
| 2182 | 2198 | _ = rhs_ty; |
| 2183 | 2199 | _ = maybe_inst; |
| 2184 | 2200 | |
| 2185 | const mod = self.bin_file.comp.module.?; | |
| 2201 | const pt = self.pt; | |
| 2202 | const mod = pt.zcu; | |
| 2186 | 2203 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2187 | 2204 | .Float => return self.fail("TODO mod on floats", .{}), |
| 2188 | 2205 | .Vector => return self.fail("TODO mod on vectors", .{}), |
| ... | ... | @@ -2200,7 +2217,8 @@ fn wrappingArithmetic( |
| 2200 | 2217 | rhs_ty: Type, |
| 2201 | 2218 | maybe_inst: ?Air.Inst.Index, |
| 2202 | 2219 | ) InnerError!MCValue { |
| 2203 | const mod = self.bin_file.comp.module.?; | |
| 2220 | const pt = self.pt; | |
| 2221 | const mod = pt.zcu; | |
| 2204 | 2222 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2205 | 2223 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| 2206 | 2224 | .Int => { |
| ... | ... | @@ -2235,7 +2253,8 @@ fn bitwise( |
| 2235 | 2253 | rhs_ty: Type, |
| 2236 | 2254 | maybe_inst: ?Air.Inst.Index, |
| 2237 | 2255 | ) InnerError!MCValue { |
| 2238 | const mod = self.bin_file.comp.module.?; | |
| 2256 | const pt = self.pt; | |
| 2257 | const mod = pt.zcu; | |
| 2239 | 2258 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2240 | 2259 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| 2241 | 2260 | .Int => { |
| ... | ... | @@ -2270,7 +2289,8 @@ fn shiftExact( |
| 2270 | 2289 | ) InnerError!MCValue { |
| 2271 | 2290 | _ = rhs_ty; |
| 2272 | 2291 | |
| 2273 | const mod = self.bin_file.comp.module.?; | |
| 2292 | const pt = self.pt; | |
| 2293 | const mod = pt.zcu; | |
| 2274 | 2294 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2275 | 2295 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| 2276 | 2296 | .Int => { |
| ... | ... | @@ -2320,7 +2340,8 @@ fn shiftNormal( |
| 2320 | 2340 | rhs_ty: Type, |
| 2321 | 2341 | maybe_inst: ?Air.Inst.Index, |
| 2322 | 2342 | ) InnerError!MCValue { |
| 2323 | const mod = self.bin_file.comp.module.?; | |
| 2343 | const pt = self.pt; | |
| 2344 | const mod = pt.zcu; | |
| 2324 | 2345 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2325 | 2346 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| 2326 | 2347 | .Int => { |
| ... | ... | @@ -2360,7 +2381,8 @@ fn booleanOp( |
| 2360 | 2381 | rhs_ty: Type, |
| 2361 | 2382 | maybe_inst: ?Air.Inst.Index, |
| 2362 | 2383 | ) InnerError!MCValue { |
| 2363 | const mod = self.bin_file.comp.module.?; | |
| 2384 | const pt = self.pt; | |
| 2385 | const mod = pt.zcu; | |
| 2364 | 2386 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2365 | 2387 | .Bool => { |
| 2366 | 2388 | assert((try lhs_bind.resolveToImmediate(self)) == null); // should have been handled by Sema |
| ... | ... | @@ -2387,7 +2409,8 @@ fn ptrArithmetic( |
| 2387 | 2409 | rhs_ty: Type, |
| 2388 | 2410 | maybe_inst: ?Air.Inst.Index, |
| 2389 | 2411 | ) InnerError!MCValue { |
| 2390 | const mod = self.bin_file.comp.module.?; | |
| 2412 | const pt = self.pt; | |
| 2413 | const mod = pt.zcu; | |
| 2391 | 2414 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2392 | 2415 | .Pointer => { |
| 2393 | 2416 | assert(rhs_ty.eql(Type.usize, mod)); |
| ... | ... | @@ -2397,7 +2420,7 @@ fn ptrArithmetic( |
| 2397 | 2420 | .One => ptr_ty.childType(mod).childType(mod), // ptr to array, so get array element type |
| 2398 | 2421 | else => ptr_ty.childType(mod), |
| 2399 | 2422 | }; |
| 2400 | const elem_size = elem_ty.abiSize(mod); | |
| 2423 | const elem_size = elem_ty.abiSize(pt); | |
| 2401 | 2424 | |
| 2402 | 2425 | const base_tag: Air.Inst.Tag = switch (tag) { |
| 2403 | 2426 | .ptr_add => .add, |
| ... | ... | @@ -2510,7 +2533,8 @@ fn airOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2510 | 2533 | const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 2511 | 2534 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2512 | 2535 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2513 | const mod = self.bin_file.comp.module.?; | |
| 2536 | const pt = self.pt; | |
| 2537 | const mod = pt.zcu; | |
| 2514 | 2538 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 2515 | 2539 | const lhs_bind: ReadArg.Bind = .{ .inst = extra.lhs }; |
| 2516 | 2540 | const rhs_bind: ReadArg.Bind = .{ .inst = extra.rhs }; |
| ... | ... | @@ -2518,9 +2542,9 @@ fn airOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2518 | 2542 | const rhs_ty = self.typeOf(extra.rhs); |
| 2519 | 2543 | |
| 2520 | 2544 | const tuple_ty = self.typeOfIndex(inst); |
| 2521 | const tuple_size = @as(u32, @intCast(tuple_ty.abiSize(mod))); | |
| 2522 | const tuple_align = tuple_ty.abiAlignment(mod); | |
| 2523 | const overflow_bit_offset = @as(u32, @intCast(tuple_ty.structFieldOffset(1, mod))); | |
| 2545 | const tuple_size = @as(u32, @intCast(tuple_ty.abiSize(pt))); | |
| 2546 | const tuple_align = tuple_ty.abiAlignment(pt); | |
| 2547 | const overflow_bit_offset = @as(u32, @intCast(tuple_ty.structFieldOffset(1, pt))); | |
| 2524 | 2548 | |
| 2525 | 2549 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2526 | 2550 | .Vector => return self.fail("TODO implement add_with_overflow/sub_with_overflow for vectors", .{}), |
| ... | ... | @@ -2638,7 +2662,8 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2638 | 2662 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2639 | 2663 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2640 | 2664 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .dead, .{ extra.lhs, extra.rhs, .none }); |
| 2641 | const mod = self.bin_file.comp.module.?; | |
| 2665 | const pt = self.pt; | |
| 2666 | const mod = pt.zcu; | |
| 2642 | 2667 | const result: MCValue = result: { |
| 2643 | 2668 | const lhs_bind: ReadArg.Bind = .{ .inst = extra.lhs }; |
| 2644 | 2669 | const rhs_bind: ReadArg.Bind = .{ .inst = extra.rhs }; |
| ... | ... | @@ -2646,9 +2671,9 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2646 | 2671 | const rhs_ty = self.typeOf(extra.rhs); |
| 2647 | 2672 | |
| 2648 | 2673 | const tuple_ty = self.typeOfIndex(inst); |
| 2649 | const tuple_size = @as(u32, @intCast(tuple_ty.abiSize(mod))); | |
| 2650 | const tuple_align = tuple_ty.abiAlignment(mod); | |
| 2651 | const overflow_bit_offset = @as(u32, @intCast(tuple_ty.structFieldOffset(1, mod))); | |
| 2674 | const tuple_size = @as(u32, @intCast(tuple_ty.abiSize(pt))); | |
| 2675 | const tuple_align = tuple_ty.abiAlignment(pt); | |
| 2676 | const overflow_bit_offset = @as(u32, @intCast(tuple_ty.structFieldOffset(1, pt))); | |
| 2652 | 2677 | |
| 2653 | 2678 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2654 | 2679 | .Vector => return self.fail("TODO implement mul_with_overflow for vectors", .{}), |
| ... | ... | @@ -2862,7 +2887,8 @@ fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2862 | 2887 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2863 | 2888 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2864 | 2889 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .dead, .{ extra.lhs, extra.rhs, .none }); |
| 2865 | const mod = self.bin_file.comp.module.?; | |
| 2890 | const pt = self.pt; | |
| 2891 | const mod = pt.zcu; | |
| 2866 | 2892 | const result: MCValue = result: { |
| 2867 | 2893 | const lhs_bind: ReadArg.Bind = .{ .inst = extra.lhs }; |
| 2868 | 2894 | const rhs_bind: ReadArg.Bind = .{ .inst = extra.rhs }; |
| ... | ... | @@ -2870,9 +2896,9 @@ fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2870 | 2896 | const rhs_ty = self.typeOf(extra.rhs); |
| 2871 | 2897 | |
| 2872 | 2898 | const tuple_ty = self.typeOfIndex(inst); |
| 2873 | const tuple_size = @as(u32, @intCast(tuple_ty.abiSize(mod))); | |
| 2874 | const tuple_align = tuple_ty.abiAlignment(mod); | |
| 2875 | const overflow_bit_offset = @as(u32, @intCast(tuple_ty.structFieldOffset(1, mod))); | |
| 2899 | const tuple_size = @as(u32, @intCast(tuple_ty.abiSize(pt))); | |
| 2900 | const tuple_align = tuple_ty.abiAlignment(pt); | |
| 2901 | const overflow_bit_offset = @as(u32, @intCast(tuple_ty.structFieldOffset(1, pt))); | |
| 2876 | 2902 | |
| 2877 | 2903 | switch (lhs_ty.zigTypeTag(mod)) { |
| 2878 | 2904 | .Vector => return self.fail("TODO implement shl_with_overflow for vectors", .{}), |
| ... | ... | @@ -3010,9 +3036,10 @@ fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 3010 | 3036 | } |
| 3011 | 3037 | |
| 3012 | 3038 | fn optionalPayload(self: *Self, inst: Air.Inst.Index, mcv: MCValue, optional_ty: Type) !MCValue { |
| 3013 | const mod = self.bin_file.comp.module.?; | |
| 3039 | const pt = self.pt; | |
| 3040 | const mod = pt.zcu; | |
| 3014 | 3041 | const payload_ty = optional_ty.optionalChild(mod); |
| 3015 | if (!payload_ty.hasRuntimeBits(mod)) return MCValue.none; | |
| 3042 | if (!payload_ty.hasRuntimeBits(pt)) return MCValue.none; | |
| 3016 | 3043 | if (optional_ty.isPtrLikeOptional(mod)) { |
| 3017 | 3044 | // TODO should we reuse the operand here? |
| 3018 | 3045 | const raw_reg = try self.register_manager.allocReg(inst, gp); |
| ... | ... | @@ -3054,17 +3081,18 @@ fn errUnionErr( |
| 3054 | 3081 | error_union_ty: Type, |
| 3055 | 3082 | maybe_inst: ?Air.Inst.Index, |
| 3056 | 3083 | ) !MCValue { |
| 3057 | const mod = self.bin_file.comp.module.?; | |
| 3084 | const pt = self.pt; | |
| 3085 | const mod = pt.zcu; | |
| 3058 | 3086 | const err_ty = error_union_ty.errorUnionSet(mod); |
| 3059 | 3087 | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 3060 | 3088 | if (err_ty.errorSetIsEmpty(mod)) { |
| 3061 | 3089 | return MCValue{ .immediate = 0 }; |
| 3062 | 3090 | } |
| 3063 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 3091 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3064 | 3092 | return try error_union_bind.resolveToMcv(self); |
| 3065 | 3093 | } |
| 3066 | 3094 | |
| 3067 | const err_offset = @as(u32, @intCast(errUnionErrorOffset(payload_ty, mod))); | |
| 3095 | const err_offset: u32 = @intCast(errUnionErrorOffset(payload_ty, pt)); | |
| 3068 | 3096 | switch (try error_union_bind.resolveToMcv(self)) { |
| 3069 | 3097 | .register => { |
| 3070 | 3098 | var operand_reg: Register = undefined; |
| ... | ... | @@ -3086,7 +3114,7 @@ fn errUnionErr( |
| 3086 | 3114 | ); |
| 3087 | 3115 | |
| 3088 | 3116 | const err_bit_offset = err_offset * 8; |
| 3089 | const err_bit_size = @as(u32, @intCast(err_ty.abiSize(mod))) * 8; | |
| 3117 | const err_bit_size = @as(u32, @intCast(err_ty.abiSize(pt))) * 8; | |
| 3090 | 3118 | |
| 3091 | 3119 | _ = try self.addInst(.{ |
| 3092 | 3120 | .tag = .ubfx, // errors are unsigned integers |
| ... | ... | @@ -3134,17 +3162,18 @@ fn errUnionPayload( |
| 3134 | 3162 | error_union_ty: Type, |
| 3135 | 3163 | maybe_inst: ?Air.Inst.Index, |
| 3136 | 3164 | ) !MCValue { |
| 3137 | const mod = self.bin_file.comp.module.?; | |
| 3165 | const pt = self.pt; | |
| 3166 | const mod = pt.zcu; | |
| 3138 | 3167 | const err_ty = error_union_ty.errorUnionSet(mod); |
| 3139 | 3168 | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 3140 | 3169 | if (err_ty.errorSetIsEmpty(mod)) { |
| 3141 | 3170 | return try error_union_bind.resolveToMcv(self); |
| 3142 | 3171 | } |
| 3143 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 3172 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3144 | 3173 | return MCValue.none; |
| 3145 | 3174 | } |
| 3146 | 3175 | |
| 3147 | const payload_offset = @as(u32, @intCast(errUnionPayloadOffset(payload_ty, mod))); | |
| 3176 | const payload_offset = @as(u32, @intCast(errUnionPayloadOffset(payload_ty, pt))); | |
| 3148 | 3177 | switch (try error_union_bind.resolveToMcv(self)) { |
| 3149 | 3178 | .register => { |
| 3150 | 3179 | var operand_reg: Register = undefined; |
| ... | ... | @@ -3166,7 +3195,7 @@ fn errUnionPayload( |
| 3166 | 3195 | ); |
| 3167 | 3196 | |
| 3168 | 3197 | const payload_bit_offset = payload_offset * 8; |
| 3169 | const payload_bit_size = @as(u32, @intCast(payload_ty.abiSize(mod))) * 8; | |
| 3198 | const payload_bit_size = @as(u32, @intCast(payload_ty.abiSize(pt))) * 8; | |
| 3170 | 3199 | |
| 3171 | 3200 | _ = try self.addInst(.{ |
| 3172 | 3201 | .tag = if (payload_ty.isSignedInt(mod)) Mir.Inst.Tag.sbfx else .ubfx, |
| ... | ... | @@ -3246,7 +3275,8 @@ fn airSaveErrReturnTraceIndex(self: *Self, inst: Air.Inst.Index) !void { |
| 3246 | 3275 | } |
| 3247 | 3276 | |
| 3248 | 3277 | fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { |
| 3249 | const mod = self.bin_file.comp.module.?; | |
| 3278 | const pt = self.pt; | |
| 3279 | const mod = pt.zcu; | |
| 3250 | 3280 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3251 | 3281 | |
| 3252 | 3282 | if (self.liveness.isUnused(inst)) { |
| ... | ... | @@ -3255,7 +3285,7 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { |
| 3255 | 3285 | |
| 3256 | 3286 | const result: MCValue = result: { |
| 3257 | 3287 | const payload_ty = self.typeOf(ty_op.operand); |
| 3258 | if (!payload_ty.hasRuntimeBits(mod)) { | |
| 3288 | if (!payload_ty.hasRuntimeBits(pt)) { | |
| 3259 | 3289 | break :result MCValue{ .immediate = 1 }; |
| 3260 | 3290 | } |
| 3261 | 3291 | |
| ... | ... | @@ -3275,9 +3305,9 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { |
| 3275 | 3305 | break :result MCValue{ .register = reg }; |
| 3276 | 3306 | } |
| 3277 | 3307 | |
| 3278 | const optional_abi_size: u32 = @intCast(optional_ty.abiSize(mod)); | |
| 3279 | const optional_abi_align = optional_ty.abiAlignment(mod); | |
| 3280 | const offset: u32 = @intCast(payload_ty.abiSize(mod)); | |
| 3308 | const optional_abi_size: u32 = @intCast(optional_ty.abiSize(pt)); | |
| 3309 | const optional_abi_align = optional_ty.abiAlignment(pt); | |
| 3310 | const offset: u32 = @intCast(payload_ty.abiSize(pt)); | |
| 3281 | 3311 | |
| 3282 | 3312 | const stack_offset = try self.allocMem(optional_abi_size, optional_abi_align, inst); |
| 3283 | 3313 | try self.genSetStack(payload_ty, stack_offset, operand); |
| ... | ... | @@ -3291,20 +3321,21 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { |
| 3291 | 3321 | |
| 3292 | 3322 | /// T to E!T |
| 3293 | 3323 | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 3294 | const mod = self.bin_file.comp.module.?; | |
| 3324 | const pt = self.pt; | |
| 3325 | const mod = pt.zcu; | |
| 3295 | 3326 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3296 | 3327 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 3297 | 3328 | const error_union_ty = ty_op.ty.toType(); |
| 3298 | 3329 | const error_ty = error_union_ty.errorUnionSet(mod); |
| 3299 | 3330 | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 3300 | 3331 | const operand = try self.resolveInst(ty_op.operand); |
| 3301 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result operand; | |
| 3332 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result operand; | |
| 3302 | 3333 | |
| 3303 | const abi_size = @as(u32, @intCast(error_union_ty.abiSize(mod))); | |
| 3304 | const abi_align = error_union_ty.abiAlignment(mod); | |
| 3334 | const abi_size = @as(u32, @intCast(error_union_ty.abiSize(pt))); | |
| 3335 | const abi_align = error_union_ty.abiAlignment(pt); | |
| 3305 | 3336 | const stack_offset = try self.allocMem(abi_size, abi_align, inst); |
| 3306 | const payload_off = errUnionPayloadOffset(payload_ty, mod); | |
| 3307 | const err_off = errUnionErrorOffset(payload_ty, mod); | |
| 3337 | const payload_off = errUnionPayloadOffset(payload_ty, pt); | |
| 3338 | const err_off = errUnionErrorOffset(payload_ty, pt); | |
| 3308 | 3339 | try self.genSetStack(payload_ty, stack_offset - @as(u32, @intCast(payload_off)), operand); |
| 3309 | 3340 | try self.genSetStack(error_ty, stack_offset - @as(u32, @intCast(err_off)), .{ .immediate = 0 }); |
| 3310 | 3341 | |
| ... | ... | @@ -3317,18 +3348,19 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 3317 | 3348 | fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void { |
| 3318 | 3349 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3319 | 3350 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 3320 | const mod = self.bin_file.comp.module.?; | |
| 3351 | const pt = self.pt; | |
| 3352 | const mod = pt.zcu; | |
| 3321 | 3353 | const error_union_ty = ty_op.ty.toType(); |
| 3322 | 3354 | const error_ty = error_union_ty.errorUnionSet(mod); |
| 3323 | 3355 | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 3324 | 3356 | const operand = try self.resolveInst(ty_op.operand); |
| 3325 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result operand; | |
| 3357 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result operand; | |
| 3326 | 3358 | |
| 3327 | const abi_size = @as(u32, @intCast(error_union_ty.abiSize(mod))); | |
| 3328 | const abi_align = error_union_ty.abiAlignment(mod); | |
| 3359 | const abi_size = @as(u32, @intCast(error_union_ty.abiSize(pt))); | |
| 3360 | const abi_align = error_union_ty.abiAlignment(pt); | |
| 3329 | 3361 | const stack_offset = try self.allocMem(abi_size, abi_align, inst); |
| 3330 | const payload_off = errUnionPayloadOffset(payload_ty, mod); | |
| 3331 | const err_off = errUnionErrorOffset(payload_ty, mod); | |
| 3362 | const payload_off = errUnionPayloadOffset(payload_ty, pt); | |
| 3363 | const err_off = errUnionErrorOffset(payload_ty, pt); | |
| 3332 | 3364 | try self.genSetStack(error_ty, stack_offset - @as(u32, @intCast(err_off)), operand); |
| 3333 | 3365 | try self.genSetStack(payload_ty, stack_offset - @as(u32, @intCast(payload_off)), .undef); |
| 3334 | 3366 | |
| ... | ... | @@ -3420,7 +3452,8 @@ fn airPtrSlicePtrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3420 | 3452 | } |
| 3421 | 3453 | |
| 3422 | 3454 | fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3423 | const mod = self.bin_file.comp.module.?; | |
| 3455 | const pt = self.pt; | |
| 3456 | const mod = pt.zcu; | |
| 3424 | 3457 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3425 | 3458 | const slice_ty = self.typeOf(bin_op.lhs); |
| 3426 | 3459 | const result: MCValue = if (!slice_ty.isVolatilePtr(mod) and self.liveness.isUnused(inst)) .dead else result: { |
| ... | ... | @@ -3444,9 +3477,10 @@ fn ptrElemVal( |
| 3444 | 3477 | ptr_ty: Type, |
| 3445 | 3478 | maybe_inst: ?Air.Inst.Index, |
| 3446 | 3479 | ) !MCValue { |
| 3447 | const mod = self.bin_file.comp.module.?; | |
| 3480 | const pt = self.pt; | |
| 3481 | const mod = pt.zcu; | |
| 3448 | 3482 | const elem_ty = ptr_ty.childType(mod); |
| 3449 | const elem_size = @as(u32, @intCast(elem_ty.abiSize(mod))); | |
| 3483 | const elem_size = @as(u32, @intCast(elem_ty.abiSize(pt))); | |
| 3450 | 3484 | |
| 3451 | 3485 | // TODO optimize for elem_sizes of 1, 2, 4, 8 |
| 3452 | 3486 | switch (elem_size) { |
| ... | ... | @@ -3486,7 +3520,8 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3486 | 3520 | } |
| 3487 | 3521 | |
| 3488 | 3522 | fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 3489 | const mod = self.bin_file.comp.module.?; | |
| 3523 | const pt = self.pt; | |
| 3524 | const mod = pt.zcu; | |
| 3490 | 3525 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3491 | 3526 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 3492 | 3527 | const result: MCValue = if (!ptr_ty.isVolatilePtr(mod) and self.liveness.isUnused(inst)) .dead else result: { |
| ... | ... | @@ -3609,9 +3644,10 @@ fn reuseOperand( |
| 3609 | 3644 | } |
| 3610 | 3645 | |
| 3611 | 3646 | fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!void { |
| 3612 | const mod = self.bin_file.comp.module.?; | |
| 3647 | const pt = self.pt; | |
| 3648 | const mod = pt.zcu; | |
| 3613 | 3649 | const elem_ty = ptr_ty.childType(mod); |
| 3614 | const elem_size = elem_ty.abiSize(mod); | |
| 3650 | const elem_size = elem_ty.abiSize(pt); | |
| 3615 | 3651 | |
| 3616 | 3652 | switch (ptr) { |
| 3617 | 3653 | .none => unreachable, |
| ... | ... | @@ -3857,12 +3893,13 @@ fn genInlineMemsetCode( |
| 3857 | 3893 | } |
| 3858 | 3894 | |
| 3859 | 3895 | fn airLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 3860 | const mod = self.bin_file.comp.module.?; | |
| 3896 | const pt = self.pt; | |
| 3897 | const mod = pt.zcu; | |
| 3861 | 3898 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3862 | 3899 | const elem_ty = self.typeOfIndex(inst); |
| 3863 | const elem_size = elem_ty.abiSize(mod); | |
| 3900 | const elem_size = elem_ty.abiSize(pt); | |
| 3864 | 3901 | const result: MCValue = result: { |
| 3865 | if (!elem_ty.hasRuntimeBits(mod)) | |
| 3902 | if (!elem_ty.hasRuntimeBits(pt)) | |
| 3866 | 3903 | break :result MCValue.none; |
| 3867 | 3904 | |
| 3868 | 3905 | const ptr = try self.resolveInst(ty_op.operand); |
| ... | ... | @@ -3888,8 +3925,9 @@ fn airLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 3888 | 3925 | } |
| 3889 | 3926 | |
| 3890 | 3927 | fn genLdrRegister(self: *Self, value_reg: Register, addr_reg: Register, ty: Type) !void { |
| 3891 | const mod = self.bin_file.comp.module.?; | |
| 3892 | const abi_size = ty.abiSize(mod); | |
| 3928 | const pt = self.pt; | |
| 3929 | const mod = pt.zcu; | |
| 3930 | const abi_size = ty.abiSize(pt); | |
| 3893 | 3931 | |
| 3894 | 3932 | const tag: Mir.Inst.Tag = switch (abi_size) { |
| 3895 | 3933 | 1 => if (ty.isSignedInt(mod)) Mir.Inst.Tag.ldrsb_immediate else .ldrb_immediate, |
| ... | ... | @@ -3911,8 +3949,8 @@ fn genLdrRegister(self: *Self, value_reg: Register, addr_reg: Register, ty: Type |
| 3911 | 3949 | } |
| 3912 | 3950 | |
| 3913 | 3951 | fn genStrRegister(self: *Self, value_reg: Register, addr_reg: Register, ty: Type) !void { |
| 3914 | const mod = self.bin_file.comp.module.?; | |
| 3915 | const abi_size = ty.abiSize(mod); | |
| 3952 | const pt = self.pt; | |
| 3953 | const abi_size = ty.abiSize(pt); | |
| 3916 | 3954 | |
| 3917 | 3955 | const tag: Mir.Inst.Tag = switch (abi_size) { |
| 3918 | 3956 | 1 => .strb_immediate, |
| ... | ... | @@ -3933,9 +3971,9 @@ fn genStrRegister(self: *Self, value_reg: Register, addr_reg: Register, ty: Type |
| 3933 | 3971 | } |
| 3934 | 3972 | |
| 3935 | 3973 | fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type) InnerError!void { |
| 3936 | const mod = self.bin_file.comp.module.?; | |
| 3974 | const pt = self.pt; | |
| 3937 | 3975 | log.debug("store: storing {} to {}", .{ value, ptr }); |
| 3938 | const abi_size = value_ty.abiSize(mod); | |
| 3976 | const abi_size = value_ty.abiSize(pt); | |
| 3939 | 3977 | |
| 3940 | 3978 | switch (ptr) { |
| 3941 | 3979 | .none => unreachable, |
| ... | ... | @@ -4087,11 +4125,12 @@ fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u8) !void { |
| 4087 | 4125 | |
| 4088 | 4126 | fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32) !MCValue { |
| 4089 | 4127 | return if (self.liveness.isUnused(inst)) .dead else result: { |
| 4090 | const mod = self.bin_file.comp.module.?; | |
| 4128 | const pt = self.pt; | |
| 4129 | const mod = pt.zcu; | |
| 4091 | 4130 | const mcv = try self.resolveInst(operand); |
| 4092 | 4131 | const ptr_ty = self.typeOf(operand); |
| 4093 | 4132 | const struct_ty = ptr_ty.childType(mod); |
| 4094 | const struct_field_offset = @as(u32, @intCast(struct_ty.structFieldOffset(index, mod))); | |
| 4133 | const struct_field_offset = @as(u32, @intCast(struct_ty.structFieldOffset(index, pt))); | |
| 4095 | 4134 | switch (mcv) { |
| 4096 | 4135 | .ptr_stack_offset => |off| { |
| 4097 | 4136 | break :result MCValue{ .ptr_stack_offset = off - struct_field_offset }; |
| ... | ... | @@ -4112,11 +4151,12 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 4112 | 4151 | const operand = extra.struct_operand; |
| 4113 | 4152 | const index = extra.field_index; |
| 4114 | 4153 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 4115 | const mod = self.bin_file.comp.module.?; | |
| 4154 | const pt = self.pt; | |
| 4155 | const mod = pt.zcu; | |
| 4116 | 4156 | const mcv = try self.resolveInst(operand); |
| 4117 | 4157 | const struct_ty = self.typeOf(operand); |
| 4118 | 4158 | const struct_field_ty = struct_ty.structFieldType(index, mod); |
| 4119 | const struct_field_offset = @as(u32, @intCast(struct_ty.structFieldOffset(index, mod))); | |
| 4159 | const struct_field_offset = @as(u32, @intCast(struct_ty.structFieldOffset(index, pt))); | |
| 4120 | 4160 | |
| 4121 | 4161 | switch (mcv) { |
| 4122 | 4162 | .dead, .unreach => unreachable, |
| ... | ... | @@ -4162,13 +4202,14 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 4162 | 4202 | } |
| 4163 | 4203 | |
| 4164 | 4204 | fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4165 | const mod = self.bin_file.comp.module.?; | |
| 4205 | const pt = self.pt; | |
| 4206 | const mod = pt.zcu; | |
| 4166 | 4207 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4167 | 4208 | const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| 4168 | 4209 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 4169 | 4210 | const field_ptr = try self.resolveInst(extra.field_ptr); |
| 4170 | 4211 | const struct_ty = ty_pl.ty.toType().childType(mod); |
| 4171 | const struct_field_offset = @as(u32, @intCast(struct_ty.structFieldOffset(extra.field_index, mod))); | |
| 4212 | const struct_field_offset = @as(u32, @intCast(struct_ty.structFieldOffset(extra.field_index, pt))); | |
| 4172 | 4213 | switch (field_ptr) { |
| 4173 | 4214 | .ptr_stack_offset => |off| { |
| 4174 | 4215 | break :result MCValue{ .ptr_stack_offset = off + struct_field_offset }; |
| ... | ... | @@ -4190,7 +4231,8 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void { |
| 4190 | 4231 | while (self.args[arg_index] == .none) arg_index += 1; |
| 4191 | 4232 | self.arg_index = arg_index + 1; |
| 4192 | 4233 | |
| 4193 | const mod = self.bin_file.comp.module.?; | |
| 4234 | const pt = self.pt; | |
| 4235 | const mod = pt.zcu; | |
| 4194 | 4236 | const ty = self.typeOfIndex(inst); |
| 4195 | 4237 | const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 4196 | 4238 | const src_index = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.src_index; |
| ... | ... | @@ -4245,7 +4287,8 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier |
| 4245 | 4287 | const extra = self.air.extraData(Air.Call, pl_op.payload); |
| 4246 | 4288 | const args = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[extra.end..][0..extra.data.args_len])); |
| 4247 | 4289 | const ty = self.typeOf(callee); |
| 4248 | const mod = self.bin_file.comp.module.?; | |
| 4290 | const pt = self.pt; | |
| 4291 | const mod = pt.zcu; | |
| 4249 | 4292 | |
| 4250 | 4293 | const fn_ty = switch (ty.zigTypeTag(mod)) { |
| 4251 | 4294 | .Fn => ty, |
| ... | ... | @@ -4269,13 +4312,13 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier |
| 4269 | 4312 | if (info.return_value == .stack_offset) { |
| 4270 | 4313 | log.debug("airCall: return by reference", .{}); |
| 4271 | 4314 | const ret_ty = fn_ty.fnReturnType(mod); |
| 4272 | const ret_abi_size: u32 = @intCast(ret_ty.abiSize(mod)); | |
| 4273 | const ret_abi_align = ret_ty.abiAlignment(mod); | |
| 4315 | const ret_abi_size: u32 = @intCast(ret_ty.abiSize(pt)); | |
| 4316 | const ret_abi_align = ret_ty.abiAlignment(pt); | |
| 4274 | 4317 | const stack_offset = try self.allocMem(ret_abi_size, ret_abi_align, inst); |
| 4275 | 4318 | |
| 4276 | 4319 | const ret_ptr_reg = self.registerAlias(.x0, Type.usize); |
| 4277 | 4320 | |
| 4278 | const ptr_ty = try mod.singleMutPtrType(ret_ty); | |
| 4321 | const ptr_ty = try pt.singleMutPtrType(ret_ty); | |
| 4279 | 4322 | try self.register_manager.getReg(ret_ptr_reg, null); |
| 4280 | 4323 | try self.genSetReg(ptr_ty, ret_ptr_reg, .{ .ptr_stack_offset = stack_offset }); |
| 4281 | 4324 | |
| ... | ... | @@ -4308,7 +4351,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier |
| 4308 | 4351 | |
| 4309 | 4352 | // Due to incremental compilation, how function calls are generated depends |
| 4310 | 4353 | // on linking. |
| 4311 | if (try self.air.value(callee, mod)) |func_value| { | |
| 4354 | if (try self.air.value(callee, pt)) |func_value| { | |
| 4312 | 4355 | if (func_value.getFunction(mod)) |func| { |
| 4313 | 4356 | if (self.bin_file.cast(link.File.Elf)) |elf_file| { |
| 4314 | 4357 | const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, func.owner_decl); |
| ... | ... | @@ -4421,7 +4464,8 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier |
| 4421 | 4464 | } |
| 4422 | 4465 | |
| 4423 | 4466 | fn airRet(self: *Self, inst: Air.Inst.Index) !void { |
| 4424 | const mod = self.bin_file.comp.module.?; | |
| 4467 | const pt = self.pt; | |
| 4468 | const mod = pt.zcu; | |
| 4425 | 4469 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4426 | 4470 | const operand = try self.resolveInst(un_op); |
| 4427 | 4471 | const ret_ty = self.fn_type.fnReturnType(mod); |
| ... | ... | @@ -4440,7 +4484,7 @@ fn airRet(self: *Self, inst: Air.Inst.Index) !void { |
| 4440 | 4484 | // |
| 4441 | 4485 | // self.ret_mcv is an address to where this function |
| 4442 | 4486 | // should store its result into |
| 4443 | const ptr_ty = try mod.singleMutPtrType(ret_ty); | |
| 4487 | const ptr_ty = try pt.singleMutPtrType(ret_ty); | |
| 4444 | 4488 | try self.store(self.ret_mcv, operand, ptr_ty, ret_ty); |
| 4445 | 4489 | }, |
| 4446 | 4490 | else => unreachable, |
| ... | ... | @@ -4453,7 +4497,8 @@ fn airRet(self: *Self, inst: Air.Inst.Index) !void { |
| 4453 | 4497 | } |
| 4454 | 4498 | |
| 4455 | 4499 | fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 4456 | const mod = self.bin_file.comp.module.?; | |
| 4500 | const pt = self.pt; | |
| 4501 | const mod = pt.zcu; | |
| 4457 | 4502 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4458 | 4503 | const ptr = try self.resolveInst(un_op); |
| 4459 | 4504 | const ptr_ty = self.typeOf(un_op); |
| ... | ... | @@ -4477,8 +4522,8 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 4477 | 4522 | // location. |
| 4478 | 4523 | const op_inst = un_op.toIndex().?; |
| 4479 | 4524 | if (self.air.instructions.items(.tag)[@intFromEnum(op_inst)] != .ret_ptr) { |
| 4480 | const abi_size = @as(u32, @intCast(ret_ty.abiSize(mod))); | |
| 4481 | const abi_align = ret_ty.abiAlignment(mod); | |
| 4525 | const abi_size = @as(u32, @intCast(ret_ty.abiSize(pt))); | |
| 4526 | const abi_align = ret_ty.abiAlignment(pt); | |
| 4482 | 4527 | |
| 4483 | 4528 | const offset = try self.allocMem(abi_size, abi_align, null); |
| 4484 | 4529 | |
| ... | ... | @@ -4513,11 +4558,12 @@ fn cmp( |
| 4513 | 4558 | lhs_ty: Type, |
| 4514 | 4559 | op: math.CompareOperator, |
| 4515 | 4560 | ) !MCValue { |
| 4516 | const mod = self.bin_file.comp.module.?; | |
| 4561 | const pt = self.pt; | |
| 4562 | const mod = pt.zcu; | |
| 4517 | 4563 | const int_ty = switch (lhs_ty.zigTypeTag(mod)) { |
| 4518 | 4564 | .Optional => blk: { |
| 4519 | 4565 | const payload_ty = lhs_ty.optionalChild(mod); |
| 4520 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4566 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4521 | 4567 | break :blk Type.u1; |
| 4522 | 4568 | } else if (lhs_ty.isPtrLikeOptional(mod)) { |
| 4523 | 4569 | break :blk Type.usize; |
| ... | ... | @@ -4620,7 +4666,8 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void { |
| 4620 | 4666 | } |
| 4621 | 4667 | |
| 4622 | 4668 | fn airDbgInlineBlock(self: *Self, inst: Air.Inst.Index) !void { |
| 4623 | const mod = self.bin_file.comp.module.?; | |
| 4669 | const pt = self.pt; | |
| 4670 | const mod = pt.zcu; | |
| 4624 | 4671 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4625 | 4672 | const extra = self.air.extraData(Air.DbgInlineBlock, ty_pl.payload); |
| 4626 | 4673 | const func = mod.funcInfo(extra.data.func); |
| ... | ... | @@ -4825,13 +4872,14 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void { |
| 4825 | 4872 | } |
| 4826 | 4873 | |
| 4827 | 4874 | fn isNull(self: *Self, operand_bind: ReadArg.Bind, operand_ty: Type) !MCValue { |
| 4828 | const mod = self.bin_file.comp.module.?; | |
| 4875 | const pt = self.pt; | |
| 4876 | const mod = pt.zcu; | |
| 4829 | 4877 | const sentinel: struct { ty: Type, bind: ReadArg.Bind } = if (!operand_ty.isPtrLikeOptional(mod)) blk: { |
| 4830 | 4878 | const payload_ty = operand_ty.optionalChild(mod); |
| 4831 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) | |
| 4879 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) | |
| 4832 | 4880 | break :blk .{ .ty = operand_ty, .bind = operand_bind }; |
| 4833 | 4881 | |
| 4834 | const offset = @as(u32, @intCast(payload_ty.abiSize(mod))); | |
| 4882 | const offset = @as(u32, @intCast(payload_ty.abiSize(pt))); | |
| 4835 | 4883 | const operand_mcv = try operand_bind.resolveToMcv(self); |
| 4836 | 4884 | const new_mcv: MCValue = switch (operand_mcv) { |
| 4837 | 4885 | .register => |source_reg| new: { |
| ... | ... | @@ -4881,7 +4929,8 @@ fn isErr( |
| 4881 | 4929 | error_union_bind: ReadArg.Bind, |
| 4882 | 4930 | error_union_ty: Type, |
| 4883 | 4931 | ) !MCValue { |
| 4884 | const mod = self.bin_file.comp.module.?; | |
| 4932 | const pt = self.pt; | |
| 4933 | const mod = pt.zcu; | |
| 4885 | 4934 | const error_type = error_union_ty.errorUnionSet(mod); |
| 4886 | 4935 | |
| 4887 | 4936 | if (error_type.errorSetIsEmpty(mod)) { |
| ... | ... | @@ -4923,7 +4972,8 @@ fn airIsNull(self: *Self, inst: Air.Inst.Index) !void { |
| 4923 | 4972 | } |
| 4924 | 4973 | |
| 4925 | 4974 | fn airIsNullPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4926 | const mod = self.bin_file.comp.module.?; | |
| 4975 | const pt = self.pt; | |
| 4976 | const mod = pt.zcu; | |
| 4927 | 4977 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4928 | 4978 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 4929 | 4979 | const operand_ptr = try self.resolveInst(un_op); |
| ... | ... | @@ -4950,7 +5000,8 @@ fn airIsNonNull(self: *Self, inst: Air.Inst.Index) !void { |
| 4950 | 5000 | } |
| 4951 | 5001 | |
| 4952 | 5002 | fn airIsNonNullPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4953 | const mod = self.bin_file.comp.module.?; | |
| 5003 | const pt = self.pt; | |
| 5004 | const mod = pt.zcu; | |
| 4954 | 5005 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4955 | 5006 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 4956 | 5007 | const operand_ptr = try self.resolveInst(un_op); |
| ... | ... | @@ -4977,7 +5028,8 @@ fn airIsErr(self: *Self, inst: Air.Inst.Index) !void { |
| 4977 | 5028 | } |
| 4978 | 5029 | |
| 4979 | 5030 | fn airIsErrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4980 | const mod = self.bin_file.comp.module.?; | |
| 5031 | const pt = self.pt; | |
| 5032 | const mod = pt.zcu; | |
| 4981 | 5033 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4982 | 5034 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 4983 | 5035 | const operand_ptr = try self.resolveInst(un_op); |
| ... | ... | @@ -5004,7 +5056,8 @@ fn airIsNonErr(self: *Self, inst: Air.Inst.Index) !void { |
| 5004 | 5056 | } |
| 5005 | 5057 | |
| 5006 | 5058 | fn airIsNonErrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 5007 | const mod = self.bin_file.comp.module.?; | |
| 5059 | const pt = self.pt; | |
| 5060 | const mod = pt.zcu; | |
| 5008 | 5061 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5009 | 5062 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 5010 | 5063 | const operand_ptr = try self.resolveInst(un_op); |
| ... | ... | @@ -5225,10 +5278,10 @@ fn airBr(self: *Self, inst: Air.Inst.Index) !void { |
| 5225 | 5278 | } |
| 5226 | 5279 | |
| 5227 | 5280 | fn br(self: *Self, block: Air.Inst.Index, operand: Air.Inst.Ref) !void { |
| 5228 | const mod = self.bin_file.comp.module.?; | |
| 5281 | const pt = self.pt; | |
| 5229 | 5282 | const block_data = self.blocks.getPtr(block).?; |
| 5230 | 5283 | |
| 5231 | if (self.typeOf(operand).hasRuntimeBits(mod)) { | |
| 5284 | if (self.typeOf(operand).hasRuntimeBits(pt)) { | |
| 5232 | 5285 | const operand_mcv = try self.resolveInst(operand); |
| 5233 | 5286 | const block_mcv = block_data.mcv; |
| 5234 | 5287 | if (block_mcv == .none) { |
| ... | ... | @@ -5402,8 +5455,9 @@ fn setRegOrMem(self: *Self, ty: Type, loc: MCValue, val: MCValue) !void { |
| 5402 | 5455 | } |
| 5403 | 5456 | |
| 5404 | 5457 | fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerError!void { |
| 5405 | const mod = self.bin_file.comp.module.?; | |
| 5406 | const abi_size = @as(u32, @intCast(ty.abiSize(mod))); | |
| 5458 | const pt = self.pt; | |
| 5459 | const mod = pt.zcu; | |
| 5460 | const abi_size = @as(u32, @intCast(ty.abiSize(pt))); | |
| 5407 | 5461 | switch (mcv) { |
| 5408 | 5462 | .dead => unreachable, |
| 5409 | 5463 | .unreach, .none => return, // Nothing to do. |
| ... | ... | @@ -5462,7 +5516,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro |
| 5462 | 5516 | try self.genSetStack(wrapped_ty, stack_offset, .{ .register = rwo.reg }); |
| 5463 | 5517 | |
| 5464 | 5518 | const overflow_bit_ty = ty.structFieldType(1, mod); |
| 5465 | const overflow_bit_offset = @as(u32, @intCast(ty.structFieldOffset(1, mod))); | |
| 5519 | const overflow_bit_offset = @as(u32, @intCast(ty.structFieldOffset(1, pt))); | |
| 5466 | 5520 | const raw_cond_reg = try self.register_manager.allocReg(null, gp); |
| 5467 | 5521 | const cond_reg = self.registerAlias(raw_cond_reg, overflow_bit_ty); |
| 5468 | 5522 | |
| ... | ... | @@ -5495,7 +5549,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro |
| 5495 | 5549 | const reg = try self.copyToTmpRegister(ty, mcv); |
| 5496 | 5550 | return self.genSetStack(ty, stack_offset, MCValue{ .register = reg }); |
| 5497 | 5551 | } else { |
| 5498 | const ptr_ty = try mod.singleMutPtrType(ty); | |
| 5552 | const ptr_ty = try pt.singleMutPtrType(ty); | |
| 5499 | 5553 | |
| 5500 | 5554 | // TODO call extern memcpy |
| 5501 | 5555 | const regs = try self.register_manager.allocRegs(5, .{ null, null, null, null, null }, gp); |
| ... | ... | @@ -5573,7 +5627,8 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro |
| 5573 | 5627 | } |
| 5574 | 5628 | |
| 5575 | 5629 | fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void { |
| 5576 | const mod = self.bin_file.comp.module.?; | |
| 5630 | const pt = self.pt; | |
| 5631 | const mod = pt.zcu; | |
| 5577 | 5632 | switch (mcv) { |
| 5578 | 5633 | .dead => unreachable, |
| 5579 | 5634 | .unreach, .none => return, // Nothing to do. |
| ... | ... | @@ -5685,7 +5740,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void |
| 5685 | 5740 | try self.genLdrRegister(reg, reg.toX(), ty); |
| 5686 | 5741 | }, |
| 5687 | 5742 | .stack_offset => |off| { |
| 5688 | const abi_size = ty.abiSize(mod); | |
| 5743 | const abi_size = ty.abiSize(pt); | |
| 5689 | 5744 | |
| 5690 | 5745 | switch (abi_size) { |
| 5691 | 5746 | 1, 2, 4, 8 => { |
| ... | ... | @@ -5709,7 +5764,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void |
| 5709 | 5764 | } |
| 5710 | 5765 | }, |
| 5711 | 5766 | .stack_argument_offset => |off| { |
| 5712 | const abi_size = ty.abiSize(mod); | |
| 5767 | const abi_size = ty.abiSize(pt); | |
| 5713 | 5768 | |
| 5714 | 5769 | switch (abi_size) { |
| 5715 | 5770 | 1, 2, 4, 8 => { |
| ... | ... | @@ -5736,8 +5791,8 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void |
| 5736 | 5791 | } |
| 5737 | 5792 | |
| 5738 | 5793 | fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerError!void { |
| 5739 | const mod = self.bin_file.comp.module.?; | |
| 5740 | const abi_size = @as(u32, @intCast(ty.abiSize(mod))); | |
| 5794 | const pt = self.pt; | |
| 5795 | const abi_size = @as(u32, @intCast(ty.abiSize(pt))); | |
| 5741 | 5796 | switch (mcv) { |
| 5742 | 5797 | .dead => unreachable, |
| 5743 | 5798 | .none, .unreach => return, |
| ... | ... | @@ -5745,7 +5800,7 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I |
| 5745 | 5800 | if (!self.wantSafety()) |
| 5746 | 5801 | return; // The already existing value will do just fine. |
| 5747 | 5802 | // TODO Upgrade this to a memset call when we have that available. |
| 5748 | switch (ty.abiSize(mod)) { | |
| 5803 | switch (ty.abiSize(pt)) { | |
| 5749 | 5804 | 1 => return self.genSetStack(ty, stack_offset, .{ .immediate = 0xaa }), |
| 5750 | 5805 | 2 => return self.genSetStack(ty, stack_offset, .{ .immediate = 0xaaaa }), |
| 5751 | 5806 | 4 => return self.genSetStack(ty, stack_offset, .{ .immediate = 0xaaaaaaaa }), |
| ... | ... | @@ -5815,7 +5870,7 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I |
| 5815 | 5870 | const reg = try self.copyToTmpRegister(ty, mcv); |
| 5816 | 5871 | return self.genSetStackArgument(ty, stack_offset, MCValue{ .register = reg }); |
| 5817 | 5872 | } else { |
| 5818 | const ptr_ty = try mod.singleMutPtrType(ty); | |
| 5873 | const ptr_ty = try pt.singleMutPtrType(ty); | |
| 5819 | 5874 | |
| 5820 | 5875 | // TODO call extern memcpy |
| 5821 | 5876 | const regs = try self.register_manager.allocRegs(5, .{ null, null, null, null, null }, gp); |
| ... | ... | @@ -5936,7 +5991,8 @@ fn airBitCast(self: *Self, inst: Air.Inst.Index) !void { |
| 5936 | 5991 | } |
| 5937 | 5992 | |
| 5938 | 5993 | fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 5939 | const mod = self.bin_file.comp.module.?; | |
| 5994 | const pt = self.pt; | |
| 5995 | const mod = pt.zcu; | |
| 5940 | 5996 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5941 | 5997 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 5942 | 5998 | const ptr_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -6056,7 +6112,8 @@ fn airReduce(self: *Self, inst: Air.Inst.Index) !void { |
| 6056 | 6112 | } |
| 6057 | 6113 | |
| 6058 | 6114 | fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| 6059 | const mod = self.bin_file.comp.module.?; | |
| 6115 | const pt = self.pt; | |
| 6116 | const mod = pt.zcu; | |
| 6060 | 6117 | const vector_ty = self.typeOfIndex(inst); |
| 6061 | 6118 | const len = vector_ty.vectorLen(mod); |
| 6062 | 6119 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| ... | ... | @@ -6100,15 +6157,15 @@ fn airMulAdd(self: *Self, inst: Air.Inst.Index) !void { |
| 6100 | 6157 | } |
| 6101 | 6158 | |
| 6102 | 6159 | fn airTry(self: *Self, inst: Air.Inst.Index) !void { |
| 6103 | const mod = self.bin_file.comp.module.?; | |
| 6160 | const pt = self.pt; | |
| 6104 | 6161 | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6105 | 6162 | const extra = self.air.extraData(Air.Try, pl_op.payload); |
| 6106 | 6163 | const body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra.end..][0..extra.data.body_len]); |
| 6107 | 6164 | const result: MCValue = result: { |
| 6108 | 6165 | const error_union_bind: ReadArg.Bind = .{ .inst = pl_op.operand }; |
| 6109 | 6166 | const error_union_ty = self.typeOf(pl_op.operand); |
| 6110 | const error_union_size = @as(u32, @intCast(error_union_ty.abiSize(mod))); | |
| 6111 | const error_union_align = error_union_ty.abiAlignment(mod); | |
| 6167 | const error_union_size = @as(u32, @intCast(error_union_ty.abiSize(pt))); | |
| 6168 | const error_union_align = error_union_ty.abiAlignment(pt); | |
| 6112 | 6169 | |
| 6113 | 6170 | // The error union will die in the body. However, we need the |
| 6114 | 6171 | // error union after the body in order to extract the payload |
| ... | ... | @@ -6137,14 +6194,15 @@ fn airTryPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 6137 | 6194 | } |
| 6138 | 6195 | |
| 6139 | 6196 | fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue { |
| 6140 | const mod = self.bin_file.comp.module.?; | |
| 6197 | const pt = self.pt; | |
| 6198 | const mod = pt.zcu; | |
| 6141 | 6199 | |
| 6142 | 6200 | // If the type has no codegen bits, no need to store it. |
| 6143 | 6201 | const inst_ty = self.typeOf(inst); |
| 6144 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(mod) and !inst_ty.isError(mod)) | |
| 6202 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(pt) and !inst_ty.isError(mod)) | |
| 6145 | 6203 | return MCValue{ .none = {} }; |
| 6146 | 6204 | |
| 6147 | const inst_index = inst.toIndex() orelse return self.genTypedValue((try self.air.value(inst, mod)).?); | |
| 6205 | const inst_index = inst.toIndex() orelse return self.genTypedValue((try self.air.value(inst, pt)).?); | |
| 6148 | 6206 | |
| 6149 | 6207 | return self.getResolvedInstValue(inst_index); |
| 6150 | 6208 | } |
| ... | ... | @@ -6164,6 +6222,7 @@ fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) MCValue { |
| 6164 | 6222 | fn genTypedValue(self: *Self, val: Value) InnerError!MCValue { |
| 6165 | 6223 | const mcv: MCValue = switch (try codegen.genTypedValue( |
| 6166 | 6224 | self.bin_file, |
| 6225 | self.pt, | |
| 6167 | 6226 | self.src_loc, |
| 6168 | 6227 | val, |
| 6169 | 6228 | self.owner_decl, |
| ... | ... | @@ -6199,7 +6258,8 @@ const CallMCValues = struct { |
| 6199 | 6258 | |
| 6200 | 6259 | /// Caller must call `CallMCValues.deinit`. |
| 6201 | 6260 | fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6202 | const mod = self.bin_file.comp.module.?; | |
| 6261 | const pt = self.pt; | |
| 6262 | const mod = pt.zcu; | |
| 6203 | 6263 | const ip = &mod.intern_pool; |
| 6204 | 6264 | const fn_info = mod.typeToFunc(fn_ty).?; |
| 6205 | 6265 | const cc = fn_info.cc; |
| ... | ... | @@ -6229,10 +6289,10 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6229 | 6289 | |
| 6230 | 6290 | if (ret_ty.zigTypeTag(mod) == .NoReturn) { |
| 6231 | 6291 | result.return_value = .{ .unreach = {} }; |
| 6232 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod) and !ret_ty.isError(mod)) { | |
| 6292 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt) and !ret_ty.isError(mod)) { | |
| 6233 | 6293 | result.return_value = .{ .none = {} }; |
| 6234 | 6294 | } else { |
| 6235 | const ret_ty_size = @as(u32, @intCast(ret_ty.abiSize(mod))); | |
| 6295 | const ret_ty_size: u32 = @intCast(ret_ty.abiSize(pt)); | |
| 6236 | 6296 | if (ret_ty_size == 0) { |
| 6237 | 6297 | assert(ret_ty.isError(mod)); |
| 6238 | 6298 | result.return_value = .{ .immediate = 0 }; |
| ... | ... | @@ -6244,7 +6304,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6244 | 6304 | } |
| 6245 | 6305 | |
| 6246 | 6306 | for (fn_info.param_types.get(ip), result.args) |ty, *result_arg| { |
| 6247 | const param_size = @as(u32, @intCast(Type.fromInterned(ty).abiSize(mod))); | |
| 6307 | const param_size = @as(u32, @intCast(Type.fromInterned(ty).abiSize(pt))); | |
| 6248 | 6308 | if (param_size == 0) { |
| 6249 | 6309 | result_arg.* = .{ .none = {} }; |
| 6250 | 6310 | continue; |
| ... | ... | @@ -6252,7 +6312,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6252 | 6312 | |
| 6253 | 6313 | // We round up NCRN only for non-Apple platforms which allow the 16-byte aligned |
| 6254 | 6314 | // values to spread across odd-numbered registers. |
| 6255 | if (Type.fromInterned(ty).abiAlignment(mod) == .@"16" and !self.target.isDarwin()) { | |
| 6315 | if (Type.fromInterned(ty).abiAlignment(pt) == .@"16" and !self.target.isDarwin()) { | |
| 6256 | 6316 | // Round up NCRN to the next even number |
| 6257 | 6317 | ncrn += ncrn % 2; |
| 6258 | 6318 | } |
| ... | ... | @@ -6270,7 +6330,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6270 | 6330 | ncrn = 8; |
| 6271 | 6331 | // TODO Apple allows the arguments on the stack to be non-8-byte aligned provided |
| 6272 | 6332 | // that the entire stack space consumed by the arguments is 8-byte aligned. |
| 6273 | if (Type.fromInterned(ty).abiAlignment(mod) == .@"8") { | |
| 6333 | if (Type.fromInterned(ty).abiAlignment(pt) == .@"8") { | |
| 6274 | 6334 | if (nsaa % 8 != 0) { |
| 6275 | 6335 | nsaa += 8 - (nsaa % 8); |
| 6276 | 6336 | } |
| ... | ... | @@ -6287,10 +6347,10 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6287 | 6347 | .Unspecified => { |
| 6288 | 6348 | if (ret_ty.zigTypeTag(mod) == .NoReturn) { |
| 6289 | 6349 | result.return_value = .{ .unreach = {} }; |
| 6290 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod) and !ret_ty.isError(mod)) { | |
| 6350 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt) and !ret_ty.isError(mod)) { | |
| 6291 | 6351 | result.return_value = .{ .none = {} }; |
| 6292 | 6352 | } else { |
| 6293 | const ret_ty_size = @as(u32, @intCast(ret_ty.abiSize(mod))); | |
| 6353 | const ret_ty_size = @as(u32, @intCast(ret_ty.abiSize(pt))); | |
| 6294 | 6354 | if (ret_ty_size == 0) { |
| 6295 | 6355 | assert(ret_ty.isError(mod)); |
| 6296 | 6356 | result.return_value = .{ .immediate = 0 }; |
| ... | ... | @@ -6309,9 +6369,9 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6309 | 6369 | var stack_offset: u32 = 0; |
| 6310 | 6370 | |
| 6311 | 6371 | for (fn_info.param_types.get(ip), result.args) |ty, *result_arg| { |
| 6312 | if (Type.fromInterned(ty).abiSize(mod) > 0) { | |
| 6313 | const param_size: u32 = @intCast(Type.fromInterned(ty).abiSize(mod)); | |
| 6314 | const param_alignment = Type.fromInterned(ty).abiAlignment(mod); | |
| 6372 | if (Type.fromInterned(ty).abiSize(pt) > 0) { | |
| 6373 | const param_size: u32 = @intCast(Type.fromInterned(ty).abiSize(pt)); | |
| 6374 | const param_alignment = Type.fromInterned(ty).abiAlignment(pt); | |
| 6315 | 6375 | |
| 6316 | 6376 | stack_offset = @intCast(param_alignment.forward(stack_offset)); |
| 6317 | 6377 | result_arg.* = .{ .stack_argument_offset = stack_offset }; |
| ... | ... | @@ -6330,7 +6390,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6330 | 6390 | return result; |
| 6331 | 6391 | } |
| 6332 | 6392 | |
| 6333 | /// TODO support scope overrides. Also note this logic is duplicated with `Module.wantSafety`. | |
| 6393 | /// TODO support scope overrides. Also note this logic is duplicated with `Zcu.wantSafety`. | |
| 6334 | 6394 | fn wantSafety(self: *Self) bool { |
| 6335 | 6395 | return switch (self.bin_file.comp.root_mod.optimize_mode) { |
| 6336 | 6396 | .Debug => true, |
| ... | ... | @@ -6362,8 +6422,7 @@ fn parseRegName(name: []const u8) ?Register { |
| 6362 | 6422 | } |
| 6363 | 6423 | |
| 6364 | 6424 | fn registerAlias(self: *Self, reg: Register, ty: Type) Register { |
| 6365 | const mod = self.bin_file.comp.module.?; | |
| 6366 | const abi_size = ty.abiSize(mod); | |
| 6425 | const abi_size = ty.abiSize(self.pt); | |
| 6367 | 6426 | |
| 6368 | 6427 | switch (reg.class()) { |
| 6369 | 6428 | .general_purpose => { |
| ... | ... | @@ -6391,11 +6450,9 @@ fn registerAlias(self: *Self, reg: Register, ty: Type) Register { |
| 6391 | 6450 | } |
| 6392 | 6451 | |
| 6393 | 6452 | fn typeOf(self: *Self, inst: Air.Inst.Ref) Type { |
| 6394 | const mod = self.bin_file.comp.module.?; | |
| 6395 | return self.air.typeOf(inst, &mod.intern_pool); | |
| 6453 | return self.air.typeOf(inst, &self.pt.zcu.intern_pool); | |
| 6396 | 6454 | } |
| 6397 | 6455 | |
| 6398 | 6456 | fn typeOfIndex(self: *Self, inst: Air.Inst.Index) Type { |
| 6399 | const mod = self.bin_file.comp.module.?; | |
| 6400 | return self.air.typeOfIndex(inst, &mod.intern_pool); | |
| 6457 | return self.air.typeOfIndex(inst, &self.pt.zcu.intern_pool); | |
| 6401 | 6458 | } |
src/arch/aarch64/Emit.zig+2-4| ... | ... | @@ -8,9 +8,7 @@ const Mir = @import("Mir.zig"); |
| 8 | 8 | const bits = @import("bits.zig"); |
| 9 | 9 | const link = @import("../../link.zig"); |
| 10 | 10 | const Zcu = @import("../../Zcu.zig"); |
| 11 | /// Deprecated. | |
| 12 | const Module = Zcu; | |
| 13 | const ErrorMsg = Module.ErrorMsg; | |
| 11 | const ErrorMsg = Zcu.ErrorMsg; | |
| 14 | 12 | const assert = std.debug.assert; |
| 15 | 13 | const Instruction = bits.Instruction; |
| 16 | 14 | const Register = bits.Register; |
| ... | ... | @@ -22,7 +20,7 @@ bin_file: *link.File, |
| 22 | 20 | debug_output: DebugInfoOutput, |
| 23 | 21 | target: *const std.Target, |
| 24 | 22 | err_msg: ?*ErrorMsg = null, |
| 25 | src_loc: Module.LazySrcLoc, | |
| 23 | src_loc: Zcu.LazySrcLoc, | |
| 26 | 24 | code: *std.ArrayList(u8), |
| 27 | 25 | |
| 28 | 26 | prev_di_line: u32, |
src/arch/aarch64/abi.zig+29-31| ... | ... | @@ -5,8 +5,6 @@ const Register = bits.Register; |
| 5 | 5 | const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager; |
| 6 | 6 | const Type = @import("../../Type.zig"); |
| 7 | 7 | const Zcu = @import("../../Zcu.zig"); |
| 8 | /// Deprecated. | |
| 9 | const Module = Zcu; | |
| 10 | 8 | |
| 11 | 9 | pub const Class = union(enum) { |
| 12 | 10 | memory, |
| ... | ... | @@ -17,44 +15,44 @@ pub const Class = union(enum) { |
| 17 | 15 | }; |
| 18 | 16 | |
| 19 | 17 | /// For `float_array` the second element will be the amount of floats. |
| 20 | pub fn classifyType(ty: Type, mod: *Module) Class { | |
| 21 | std.debug.assert(ty.hasRuntimeBitsIgnoreComptime(mod)); | |
| 18 | pub fn classifyType(ty: Type, pt: Zcu.PerThread) Class { | |
| 19 | std.debug.assert(ty.hasRuntimeBitsIgnoreComptime(pt)); | |
| 22 | 20 | |
| 23 | 21 | var maybe_float_bits: ?u16 = null; |
| 24 | switch (ty.zigTypeTag(mod)) { | |
| 22 | switch (ty.zigTypeTag(pt.zcu)) { | |
| 25 | 23 | .Struct => { |
| 26 | if (ty.containerLayout(mod) == .@"packed") return .byval; | |
| 27 | const float_count = countFloats(ty, mod, &maybe_float_bits); | |
| 24 | if (ty.containerLayout(pt.zcu) == .@"packed") return .byval; | |
| 25 | const float_count = countFloats(ty, pt.zcu, &maybe_float_bits); | |
| 28 | 26 | if (float_count <= sret_float_count) return .{ .float_array = float_count }; |
| 29 | 27 | |
| 30 | const bit_size = ty.bitSize(mod); | |
| 28 | const bit_size = ty.bitSize(pt); | |
| 31 | 29 | if (bit_size > 128) return .memory; |
| 32 | 30 | if (bit_size > 64) return .double_integer; |
| 33 | 31 | return .integer; |
| 34 | 32 | }, |
| 35 | 33 | .Union => { |
| 36 | if (ty.containerLayout(mod) == .@"packed") return .byval; | |
| 37 | const float_count = countFloats(ty, mod, &maybe_float_bits); | |
| 34 | if (ty.containerLayout(pt.zcu) == .@"packed") return .byval; | |
| 35 | const float_count = countFloats(ty, pt.zcu, &maybe_float_bits); | |
| 38 | 36 | if (float_count <= sret_float_count) return .{ .float_array = float_count }; |
| 39 | 37 | |
| 40 | const bit_size = ty.bitSize(mod); | |
| 38 | const bit_size = ty.bitSize(pt); | |
| 41 | 39 | if (bit_size > 128) return .memory; |
| 42 | 40 | if (bit_size > 64) return .double_integer; |
| 43 | 41 | return .integer; |
| 44 | 42 | }, |
| 45 | 43 | .Int, .Enum, .ErrorSet, .Float, .Bool => return .byval, |
| 46 | 44 | .Vector => { |
| 47 | const bit_size = ty.bitSize(mod); | |
| 45 | const bit_size = ty.bitSize(pt); | |
| 48 | 46 | // TODO is this controlled by a cpu feature? |
| 49 | 47 | if (bit_size > 128) return .memory; |
| 50 | 48 | return .byval; |
| 51 | 49 | }, |
| 52 | 50 | .Optional => { |
| 53 | std.debug.assert(ty.isPtrLikeOptional(mod)); | |
| 51 | std.debug.assert(ty.isPtrLikeOptional(pt.zcu)); | |
| 54 | 52 | return .byval; |
| 55 | 53 | }, |
| 56 | 54 | .Pointer => { |
| 57 | std.debug.assert(!ty.isSlice(mod)); | |
| 55 | std.debug.assert(!ty.isSlice(pt.zcu)); | |
| 58 | 56 | return .byval; |
| 59 | 57 | }, |
| 60 | 58 | .ErrorUnion, |
| ... | ... | @@ -76,16 +74,16 @@ pub fn classifyType(ty: Type, mod: *Module) Class { |
| 76 | 74 | } |
| 77 | 75 | |
| 78 | 76 | const sret_float_count = 4; |
| 79 | fn countFloats(ty: Type, mod: *Module, maybe_float_bits: *?u16) u8 { | |
| 80 | const ip = &mod.intern_pool; | |
| 81 | const target = mod.getTarget(); | |
| 77 | fn countFloats(ty: Type, zcu: *Zcu, maybe_float_bits: *?u16) u8 { | |
| 78 | const ip = &zcu.intern_pool; | |
| 79 | const target = zcu.getTarget(); | |
| 82 | 80 | const invalid = std.math.maxInt(u8); |
| 83 | switch (ty.zigTypeTag(mod)) { | |
| 81 | switch (ty.zigTypeTag(zcu)) { | |
| 84 | 82 | .Union => { |
| 85 | const union_obj = mod.typeToUnion(ty).?; | |
| 83 | const union_obj = zcu.typeToUnion(ty).?; | |
| 86 | 84 | var max_count: u8 = 0; |
| 87 | 85 | for (union_obj.field_types.get(ip)) |field_ty| { |
| 88 | const field_count = countFloats(Type.fromInterned(field_ty), mod, maybe_float_bits); | |
| 86 | const field_count = countFloats(Type.fromInterned(field_ty), zcu, maybe_float_bits); | |
| 89 | 87 | if (field_count == invalid) return invalid; |
| 90 | 88 | if (field_count > max_count) max_count = field_count; |
| 91 | 89 | if (max_count > sret_float_count) return invalid; |
| ... | ... | @@ -93,12 +91,12 @@ fn countFloats(ty: Type, mod: *Module, maybe_float_bits: *?u16) u8 { |
| 93 | 91 | return max_count; |
| 94 | 92 | }, |
| 95 | 93 | .Struct => { |
| 96 | const fields_len = ty.structFieldCount(mod); | |
| 94 | const fields_len = ty.structFieldCount(zcu); | |
| 97 | 95 | var count: u8 = 0; |
| 98 | 96 | var i: u32 = 0; |
| 99 | 97 | while (i < fields_len) : (i += 1) { |
| 100 | const field_ty = ty.structFieldType(i, mod); | |
| 101 | const field_count = countFloats(field_ty, mod, maybe_float_bits); | |
| 98 | const field_ty = ty.structFieldType(i, zcu); | |
| 99 | const field_count = countFloats(field_ty, zcu, maybe_float_bits); | |
| 102 | 100 | if (field_count == invalid) return invalid; |
| 103 | 101 | count += field_count; |
| 104 | 102 | if (count > sret_float_count) return invalid; |
| ... | ... | @@ -118,22 +116,22 @@ fn countFloats(ty: Type, mod: *Module, maybe_float_bits: *?u16) u8 { |
| 118 | 116 | } |
| 119 | 117 | } |
| 120 | 118 | |
| 121 | pub fn getFloatArrayType(ty: Type, mod: *Module) ?Type { | |
| 122 | const ip = &mod.intern_pool; | |
| 123 | switch (ty.zigTypeTag(mod)) { | |
| 119 | pub fn getFloatArrayType(ty: Type, zcu: *Zcu) ?Type { | |
| 120 | const ip = &zcu.intern_pool; | |
| 121 | switch (ty.zigTypeTag(zcu)) { | |
| 124 | 122 | .Union => { |
| 125 | const union_obj = mod.typeToUnion(ty).?; | |
| 123 | const union_obj = zcu.typeToUnion(ty).?; | |
| 126 | 124 | for (union_obj.field_types.get(ip)) |field_ty| { |
| 127 | if (getFloatArrayType(Type.fromInterned(field_ty), mod)) |some| return some; | |
| 125 | if (getFloatArrayType(Type.fromInterned(field_ty), zcu)) |some| return some; | |
| 128 | 126 | } |
| 129 | 127 | return null; |
| 130 | 128 | }, |
| 131 | 129 | .Struct => { |
| 132 | const fields_len = ty.structFieldCount(mod); | |
| 130 | const fields_len = ty.structFieldCount(zcu); | |
| 133 | 131 | var i: u32 = 0; |
| 134 | 132 | while (i < fields_len) : (i += 1) { |
| 135 | const field_ty = ty.structFieldType(i, mod); | |
| 136 | if (getFloatArrayType(field_ty, mod)) |some| return some; | |
| 133 | const field_ty = ty.structFieldType(i, zcu); | |
| 134 | if (getFloatArrayType(field_ty, zcu)) |some| return some; | |
| 137 | 135 | } |
| 138 | 136 | return null; |
| 139 | 137 | }, |
src/arch/arm/CodeGen.zig+219-164| ... | ... | @@ -12,11 +12,9 @@ const Type = @import("../../Type.zig"); |
| 12 | 12 | const Value = @import("../../Value.zig"); |
| 13 | 13 | const link = @import("../../link.zig"); |
| 14 | 14 | const Zcu = @import("../../Zcu.zig"); |
| 15 | /// Deprecated. | |
| 16 | const Module = Zcu; | |
| 17 | 15 | const InternPool = @import("../../InternPool.zig"); |
| 18 | 16 | const Compilation = @import("../../Compilation.zig"); |
| 19 | const ErrorMsg = Module.ErrorMsg; | |
| 17 | const ErrorMsg = Zcu.ErrorMsg; | |
| 20 | 18 | const Target = std.Target; |
| 21 | 19 | const Allocator = mem.Allocator; |
| 22 | 20 | const trace = @import("../../tracy.zig").trace; |
| ... | ... | @@ -48,6 +46,7 @@ const gp = abi.RegisterClass.gp; |
| 48 | 46 | const InnerError = CodeGenError || error{OutOfRegisters}; |
| 49 | 47 | |
| 50 | 48 | gpa: Allocator, |
| 49 | pt: Zcu.PerThread, | |
| 51 | 50 | air: Air, |
| 52 | 51 | liveness: Liveness, |
| 53 | 52 | bin_file: *link.File, |
| ... | ... | @@ -59,7 +58,7 @@ args: []MCValue, |
| 59 | 58 | ret_mcv: MCValue, |
| 60 | 59 | fn_type: Type, |
| 61 | 60 | arg_index: u32, |
| 62 | src_loc: Module.LazySrcLoc, | |
| 61 | src_loc: Zcu.LazySrcLoc, | |
| 63 | 62 | stack_align: u32, |
| 64 | 63 | |
| 65 | 64 | /// MIR Instructions |
| ... | ... | @@ -261,7 +260,6 @@ const DbgInfoReloc = struct { |
| 261 | 260 | } |
| 262 | 261 | |
| 263 | 262 | fn genArgDbgInfo(reloc: DbgInfoReloc, function: Self) error{OutOfMemory}!void { |
| 264 | const mod = function.bin_file.comp.module.?; | |
| 265 | 263 | switch (function.debug_output) { |
| 266 | 264 | .dwarf => |dw| { |
| 267 | 265 | const loc: link.File.Dwarf.DeclState.DbgInfoLoc = switch (reloc.mcv) { |
| ... | ... | @@ -282,7 +280,7 @@ const DbgInfoReloc = struct { |
| 282 | 280 | else => unreachable, // not a possible argument |
| 283 | 281 | }; |
| 284 | 282 | |
| 285 | try dw.genArgDbgInfo(reloc.name, reloc.ty, mod.funcOwnerDeclIndex(function.func_index), loc); | |
| 283 | try dw.genArgDbgInfo(reloc.name, reloc.ty, function.pt.zcu.funcOwnerDeclIndex(function.func_index), loc); | |
| 286 | 284 | }, |
| 287 | 285 | .plan9 => {}, |
| 288 | 286 | .none => {}, |
| ... | ... | @@ -290,7 +288,6 @@ const DbgInfoReloc = struct { |
| 290 | 288 | } |
| 291 | 289 | |
| 292 | 290 | fn genVarDbgInfo(reloc: DbgInfoReloc, function: Self) !void { |
| 293 | const mod = function.bin_file.comp.module.?; | |
| 294 | 291 | const is_ptr = switch (reloc.tag) { |
| 295 | 292 | .dbg_var_ptr => true, |
| 296 | 293 | .dbg_var_val => false, |
| ... | ... | @@ -326,7 +323,7 @@ const DbgInfoReloc = struct { |
| 326 | 323 | break :blk .nop; |
| 327 | 324 | }, |
| 328 | 325 | }; |
| 329 | try dw.genVarDbgInfo(reloc.name, reloc.ty, mod.funcOwnerDeclIndex(function.func_index), is_ptr, loc); | |
| 326 | try dw.genVarDbgInfo(reloc.name, reloc.ty, function.pt.zcu.funcOwnerDeclIndex(function.func_index), is_ptr, loc); | |
| 330 | 327 | }, |
| 331 | 328 | .plan9 => {}, |
| 332 | 329 | .none => {}, |
| ... | ... | @@ -338,15 +335,16 @@ const Self = @This(); |
| 338 | 335 | |
| 339 | 336 | pub fn generate( |
| 340 | 337 | lf: *link.File, |
| 341 | src_loc: Module.LazySrcLoc, | |
| 338 | pt: Zcu.PerThread, | |
| 339 | src_loc: Zcu.LazySrcLoc, | |
| 342 | 340 | func_index: InternPool.Index, |
| 343 | 341 | air: Air, |
| 344 | 342 | liveness: Liveness, |
| 345 | 343 | code: *std.ArrayList(u8), |
| 346 | 344 | debug_output: DebugInfoOutput, |
| 347 | 345 | ) CodeGenError!Result { |
| 348 | const gpa = lf.comp.gpa; | |
| 349 | const zcu = lf.comp.module.?; | |
| 346 | const zcu = pt.zcu; | |
| 347 | const gpa = zcu.gpa; | |
| 350 | 348 | const func = zcu.funcInfo(func_index); |
| 351 | 349 | const fn_owner_decl = zcu.declPtr(func.owner_decl); |
| 352 | 350 | assert(fn_owner_decl.has_tv); |
| ... | ... | @@ -364,6 +362,7 @@ pub fn generate( |
| 364 | 362 | |
| 365 | 363 | var function: Self = .{ |
| 366 | 364 | .gpa = gpa, |
| 365 | .pt = pt, | |
| 367 | 366 | .air = air, |
| 368 | 367 | .liveness = liveness, |
| 369 | 368 | .target = target, |
| ... | ... | @@ -482,7 +481,8 @@ pub fn addExtraAssumeCapacity(self: *Self, extra: anytype) u32 { |
| 482 | 481 | } |
| 483 | 482 | |
| 484 | 483 | fn gen(self: *Self) !void { |
| 485 | const mod = self.bin_file.comp.module.?; | |
| 484 | const pt = self.pt; | |
| 485 | const mod = pt.zcu; | |
| 486 | 486 | const cc = self.fn_type.fnCallingConvention(mod); |
| 487 | 487 | if (cc != .Naked) { |
| 488 | 488 | // push {fp, lr} |
| ... | ... | @@ -526,8 +526,8 @@ fn gen(self: *Self) !void { |
| 526 | 526 | |
| 527 | 527 | const ty = self.typeOfIndex(inst); |
| 528 | 528 | |
| 529 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 530 | const abi_align = ty.abiAlignment(mod); | |
| 529 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 530 | const abi_align = ty.abiAlignment(pt); | |
| 531 | 531 | const stack_offset = try self.allocMem(abi_size, abi_align, inst); |
| 532 | 532 | try self.genSetStack(ty, stack_offset, MCValue{ .register = reg }); |
| 533 | 533 | |
| ... | ... | @@ -642,7 +642,8 @@ fn gen(self: *Self) !void { |
| 642 | 642 | } |
| 643 | 643 | |
| 644 | 644 | fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 645 | const mod = self.bin_file.comp.module.?; | |
| 645 | const pt = self.pt; | |
| 646 | const mod = pt.zcu; | |
| 646 | 647 | const ip = &mod.intern_pool; |
| 647 | 648 | const air_tags = self.air.instructions.items(.tag); |
| 648 | 649 | |
| ... | ... | @@ -1004,10 +1005,11 @@ fn allocMem( |
| 1004 | 1005 | |
| 1005 | 1006 | /// Use a pointer instruction as the basis for allocating stack memory. |
| 1006 | 1007 | fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 1007 | const mod = self.bin_file.comp.module.?; | |
| 1008 | const pt = self.pt; | |
| 1009 | const mod = pt.zcu; | |
| 1008 | 1010 | const elem_ty = self.typeOfIndex(inst).childType(mod); |
| 1009 | 1011 | |
| 1010 | if (!elem_ty.hasRuntimeBits(mod)) { | |
| 1012 | if (!elem_ty.hasRuntimeBits(pt)) { | |
| 1011 | 1013 | // As this stack item will never be dereferenced at runtime, |
| 1012 | 1014 | // return the stack offset 0. Stack offset 0 will be where all |
| 1013 | 1015 | // zero-sized stack allocations live as non-zero-sized |
| ... | ... | @@ -1015,21 +1017,21 @@ fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 1015 | 1017 | return 0; |
| 1016 | 1018 | } |
| 1017 | 1019 | |
| 1018 | const abi_size = math.cast(u32, elem_ty.abiSize(mod)) orelse { | |
| 1019 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(mod)}); | |
| 1020 | const abi_size = math.cast(u32, elem_ty.abiSize(pt)) orelse { | |
| 1021 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(pt)}); | |
| 1020 | 1022 | }; |
| 1021 | 1023 | // TODO swap this for inst.ty.ptrAlign |
| 1022 | const abi_align = elem_ty.abiAlignment(mod); | |
| 1024 | const abi_align = elem_ty.abiAlignment(pt); | |
| 1023 | 1025 | |
| 1024 | 1026 | return self.allocMem(abi_size, abi_align, inst); |
| 1025 | 1027 | } |
| 1026 | 1028 | |
| 1027 | 1029 | fn allocRegOrMem(self: *Self, elem_ty: Type, reg_ok: bool, maybe_inst: ?Air.Inst.Index) !MCValue { |
| 1028 | const mod = self.bin_file.comp.module.?; | |
| 1029 | const abi_size = math.cast(u32, elem_ty.abiSize(mod)) orelse { | |
| 1030 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(mod)}); | |
| 1030 | const pt = self.pt; | |
| 1031 | const abi_size = math.cast(u32, elem_ty.abiSize(pt)) orelse { | |
| 1032 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(pt)}); | |
| 1031 | 1033 | }; |
| 1032 | const abi_align = elem_ty.abiAlignment(mod); | |
| 1034 | const abi_align = elem_ty.abiAlignment(pt); | |
| 1033 | 1035 | |
| 1034 | 1036 | if (reg_ok) { |
| 1035 | 1037 | // Make sure the type can fit in a register before we try to allocate one. |
| ... | ... | @@ -1112,14 +1114,15 @@ fn airAlloc(self: *Self, inst: Air.Inst.Index) !void { |
| 1112 | 1114 | } |
| 1113 | 1115 | |
| 1114 | 1116 | fn airRetPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 1115 | const mod = self.bin_file.comp.module.?; | |
| 1117 | const pt = self.pt; | |
| 1118 | const mod = pt.zcu; | |
| 1116 | 1119 | const result: MCValue = switch (self.ret_mcv) { |
| 1117 | 1120 | .none, .register => .{ .ptr_stack_offset = try self.allocMemPtr(inst) }, |
| 1118 | 1121 | .stack_offset => blk: { |
| 1119 | 1122 | // self.ret_mcv is an address to where this function |
| 1120 | 1123 | // should store its result into |
| 1121 | 1124 | const ret_ty = self.fn_type.fnReturnType(mod); |
| 1122 | const ptr_ty = try mod.singleMutPtrType(ret_ty); | |
| 1125 | const ptr_ty = try pt.singleMutPtrType(ret_ty); | |
| 1123 | 1126 | |
| 1124 | 1127 | // addr_reg will contain the address of where to store the |
| 1125 | 1128 | // result into |
| ... | ... | @@ -1145,7 +1148,8 @@ fn airFpext(self: *Self, inst: Air.Inst.Index) !void { |
| 1145 | 1148 | } |
| 1146 | 1149 | |
| 1147 | 1150 | fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 1148 | const mod = self.bin_file.comp.module.?; | |
| 1151 | const pt = self.pt; | |
| 1152 | const mod = pt.zcu; | |
| 1149 | 1153 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1150 | 1154 | if (self.liveness.isUnused(inst)) |
| 1151 | 1155 | return self.finishAir(inst, .dead, .{ ty_op.operand, .none, .none }); |
| ... | ... | @@ -1154,8 +1158,8 @@ fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 1154 | 1158 | const operand_ty = self.typeOf(ty_op.operand); |
| 1155 | 1159 | const dest_ty = self.typeOfIndex(inst); |
| 1156 | 1160 | |
| 1157 | const operand_abi_size = operand_ty.abiSize(mod); | |
| 1158 | const dest_abi_size = dest_ty.abiSize(mod); | |
| 1161 | const operand_abi_size = operand_ty.abiSize(pt); | |
| 1162 | const dest_abi_size = dest_ty.abiSize(pt); | |
| 1159 | 1163 | const info_a = operand_ty.intInfo(mod); |
| 1160 | 1164 | const info_b = dest_ty.intInfo(mod); |
| 1161 | 1165 | |
| ... | ... | @@ -1211,7 +1215,8 @@ fn trunc( |
| 1211 | 1215 | operand_ty: Type, |
| 1212 | 1216 | dest_ty: Type, |
| 1213 | 1217 | ) !MCValue { |
| 1214 | const mod = self.bin_file.comp.module.?; | |
| 1218 | const pt = self.pt; | |
| 1219 | const mod = pt.zcu; | |
| 1215 | 1220 | const info_a = operand_ty.intInfo(mod); |
| 1216 | 1221 | const info_b = dest_ty.intInfo(mod); |
| 1217 | 1222 | |
| ... | ... | @@ -1275,7 +1280,8 @@ fn airIntFromBool(self: *Self, inst: Air.Inst.Index) !void { |
| 1275 | 1280 | |
| 1276 | 1281 | fn airNot(self: *Self, inst: Air.Inst.Index) !void { |
| 1277 | 1282 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1278 | const mod = self.bin_file.comp.module.?; | |
| 1283 | const pt = self.pt; | |
| 1284 | const mod = pt.zcu; | |
| 1279 | 1285 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 1280 | 1286 | const operand_bind: ReadArg.Bind = .{ .inst = ty_op.operand }; |
| 1281 | 1287 | const operand_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -1371,7 +1377,8 @@ fn minMax( |
| 1371 | 1377 | rhs_ty: Type, |
| 1372 | 1378 | maybe_inst: ?Air.Inst.Index, |
| 1373 | 1379 | ) !MCValue { |
| 1374 | const mod = self.bin_file.comp.module.?; | |
| 1380 | const pt = self.pt; | |
| 1381 | const mod = pt.zcu; | |
| 1375 | 1382 | switch (lhs_ty.zigTypeTag(mod)) { |
| 1376 | 1383 | .Float => return self.fail("TODO ARM min/max on floats", .{}), |
| 1377 | 1384 | .Vector => return self.fail("TODO ARM min/max on vectors", .{}), |
| ... | ... | @@ -1580,7 +1587,8 @@ fn airOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 1580 | 1587 | const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 1581 | 1588 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 1582 | 1589 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 1583 | const mod = self.bin_file.comp.module.?; | |
| 1590 | const pt = self.pt; | |
| 1591 | const mod = pt.zcu; | |
| 1584 | 1592 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 1585 | 1593 | const lhs_bind: ReadArg.Bind = .{ .inst = extra.lhs }; |
| 1586 | 1594 | const rhs_bind: ReadArg.Bind = .{ .inst = extra.rhs }; |
| ... | ... | @@ -1588,9 +1596,9 @@ fn airOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 1588 | 1596 | const rhs_ty = self.typeOf(extra.rhs); |
| 1589 | 1597 | |
| 1590 | 1598 | const tuple_ty = self.typeOfIndex(inst); |
| 1591 | const tuple_size: u32 = @intCast(tuple_ty.abiSize(mod)); | |
| 1592 | const tuple_align = tuple_ty.abiAlignment(mod); | |
| 1593 | const overflow_bit_offset: u32 = @intCast(tuple_ty.structFieldOffset(1, mod)); | |
| 1599 | const tuple_size: u32 = @intCast(tuple_ty.abiSize(pt)); | |
| 1600 | const tuple_align = tuple_ty.abiAlignment(pt); | |
| 1601 | const overflow_bit_offset: u32 = @intCast(tuple_ty.structFieldOffset(1, pt)); | |
| 1594 | 1602 | |
| 1595 | 1603 | switch (lhs_ty.zigTypeTag(mod)) { |
| 1596 | 1604 | .Vector => return self.fail("TODO implement add_with_overflow/sub_with_overflow for vectors", .{}), |
| ... | ... | @@ -1693,7 +1701,8 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 1693 | 1701 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 1694 | 1702 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 1695 | 1703 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .dead, .{ extra.lhs, extra.rhs, .none }); |
| 1696 | const mod = self.bin_file.comp.module.?; | |
| 1704 | const pt = self.pt; | |
| 1705 | const mod = pt.zcu; | |
| 1697 | 1706 | const result: MCValue = result: { |
| 1698 | 1707 | const lhs_bind: ReadArg.Bind = .{ .inst = extra.lhs }; |
| 1699 | 1708 | const rhs_bind: ReadArg.Bind = .{ .inst = extra.rhs }; |
| ... | ... | @@ -1701,9 +1710,9 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 1701 | 1710 | const rhs_ty = self.typeOf(extra.rhs); |
| 1702 | 1711 | |
| 1703 | 1712 | const tuple_ty = self.typeOfIndex(inst); |
| 1704 | const tuple_size: u32 = @intCast(tuple_ty.abiSize(mod)); | |
| 1705 | const tuple_align = tuple_ty.abiAlignment(mod); | |
| 1706 | const overflow_bit_offset: u32 = @intCast(tuple_ty.structFieldOffset(1, mod)); | |
| 1713 | const tuple_size: u32 = @intCast(tuple_ty.abiSize(pt)); | |
| 1714 | const tuple_align = tuple_ty.abiAlignment(pt); | |
| 1715 | const overflow_bit_offset: u32 = @intCast(tuple_ty.structFieldOffset(1, pt)); | |
| 1707 | 1716 | |
| 1708 | 1717 | switch (lhs_ty.zigTypeTag(mod)) { |
| 1709 | 1718 | .Vector => return self.fail("TODO implement mul_with_overflow for vectors", .{}), |
| ... | ... | @@ -1857,15 +1866,16 @@ fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 1857 | 1866 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 1858 | 1867 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 1859 | 1868 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .dead, .{ extra.lhs, extra.rhs, .none }); |
| 1860 | const mod = self.bin_file.comp.module.?; | |
| 1869 | const pt = self.pt; | |
| 1870 | const mod = pt.zcu; | |
| 1861 | 1871 | const result: MCValue = result: { |
| 1862 | 1872 | const lhs_ty = self.typeOf(extra.lhs); |
| 1863 | 1873 | const rhs_ty = self.typeOf(extra.rhs); |
| 1864 | 1874 | |
| 1865 | 1875 | const tuple_ty = self.typeOfIndex(inst); |
| 1866 | const tuple_size: u32 = @intCast(tuple_ty.abiSize(mod)); | |
| 1867 | const tuple_align = tuple_ty.abiAlignment(mod); | |
| 1868 | const overflow_bit_offset: u32 = @intCast(tuple_ty.structFieldOffset(1, mod)); | |
| 1876 | const tuple_size: u32 = @intCast(tuple_ty.abiSize(pt)); | |
| 1877 | const tuple_align = tuple_ty.abiAlignment(pt); | |
| 1878 | const overflow_bit_offset: u32 = @intCast(tuple_ty.structFieldOffset(1, pt)); | |
| 1869 | 1879 | |
| 1870 | 1880 | switch (lhs_ty.zigTypeTag(mod)) { |
| 1871 | 1881 | .Vector => return self.fail("TODO implement shl_with_overflow for vectors", .{}), |
| ... | ... | @@ -2013,11 +2023,11 @@ fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void { |
| 2013 | 2023 | } |
| 2014 | 2024 | |
| 2015 | 2025 | fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { |
| 2016 | const mod = self.bin_file.comp.module.?; | |
| 2026 | const pt = self.pt; | |
| 2017 | 2027 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2018 | 2028 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 2019 | 2029 | const optional_ty = self.typeOfIndex(inst); |
| 2020 | const abi_size: u32 = @intCast(optional_ty.abiSize(mod)); | |
| 2030 | const abi_size: u32 = @intCast(optional_ty.abiSize(pt)); | |
| 2021 | 2031 | |
| 2022 | 2032 | // Optional with a zero-bit payload type is just a boolean true |
| 2023 | 2033 | if (abi_size == 1) { |
| ... | ... | @@ -2036,17 +2046,18 @@ fn errUnionErr( |
| 2036 | 2046 | error_union_ty: Type, |
| 2037 | 2047 | maybe_inst: ?Air.Inst.Index, |
| 2038 | 2048 | ) !MCValue { |
| 2039 | const mod = self.bin_file.comp.module.?; | |
| 2049 | const pt = self.pt; | |
| 2050 | const mod = pt.zcu; | |
| 2040 | 2051 | const err_ty = error_union_ty.errorUnionSet(mod); |
| 2041 | 2052 | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 2042 | 2053 | if (err_ty.errorSetIsEmpty(mod)) { |
| 2043 | 2054 | return MCValue{ .immediate = 0 }; |
| 2044 | 2055 | } |
| 2045 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 2056 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 2046 | 2057 | return try error_union_bind.resolveToMcv(self); |
| 2047 | 2058 | } |
| 2048 | 2059 | |
| 2049 | const err_offset: u32 = @intCast(errUnionErrorOffset(payload_ty, mod)); | |
| 2060 | const err_offset: u32 = @intCast(errUnionErrorOffset(payload_ty, pt)); | |
| 2050 | 2061 | switch (try error_union_bind.resolveToMcv(self)) { |
| 2051 | 2062 | .register => { |
| 2052 | 2063 | var operand_reg: Register = undefined; |
| ... | ... | @@ -2068,7 +2079,7 @@ fn errUnionErr( |
| 2068 | 2079 | ); |
| 2069 | 2080 | |
| 2070 | 2081 | const err_bit_offset = err_offset * 8; |
| 2071 | const err_bit_size: u32 = @intCast(err_ty.abiSize(mod) * 8); | |
| 2082 | const err_bit_size: u32 = @intCast(err_ty.abiSize(pt) * 8); | |
| 2072 | 2083 | |
| 2073 | 2084 | _ = try self.addInst(.{ |
| 2074 | 2085 | .tag = .ubfx, // errors are unsigned integers |
| ... | ... | @@ -2113,17 +2124,18 @@ fn errUnionPayload( |
| 2113 | 2124 | error_union_ty: Type, |
| 2114 | 2125 | maybe_inst: ?Air.Inst.Index, |
| 2115 | 2126 | ) !MCValue { |
| 2116 | const mod = self.bin_file.comp.module.?; | |
| 2127 | const pt = self.pt; | |
| 2128 | const mod = pt.zcu; | |
| 2117 | 2129 | const err_ty = error_union_ty.errorUnionSet(mod); |
| 2118 | 2130 | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 2119 | 2131 | if (err_ty.errorSetIsEmpty(mod)) { |
| 2120 | 2132 | return try error_union_bind.resolveToMcv(self); |
| 2121 | 2133 | } |
| 2122 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 2134 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 2123 | 2135 | return MCValue.none; |
| 2124 | 2136 | } |
| 2125 | 2137 | |
| 2126 | const payload_offset: u32 = @intCast(errUnionPayloadOffset(payload_ty, mod)); | |
| 2138 | const payload_offset: u32 = @intCast(errUnionPayloadOffset(payload_ty, pt)); | |
| 2127 | 2139 | switch (try error_union_bind.resolveToMcv(self)) { |
| 2128 | 2140 | .register => { |
| 2129 | 2141 | var operand_reg: Register = undefined; |
| ... | ... | @@ -2145,7 +2157,7 @@ fn errUnionPayload( |
| 2145 | 2157 | ); |
| 2146 | 2158 | |
| 2147 | 2159 | const payload_bit_offset = payload_offset * 8; |
| 2148 | const payload_bit_size: u32 = @intCast(payload_ty.abiSize(mod) * 8); | |
| 2160 | const payload_bit_size: u32 = @intCast(payload_ty.abiSize(pt) * 8); | |
| 2149 | 2161 | |
| 2150 | 2162 | _ = try self.addInst(.{ |
| 2151 | 2163 | .tag = if (payload_ty.isSignedInt(mod)) Mir.Inst.Tag.sbfx else .ubfx, |
| ... | ... | @@ -2223,20 +2235,21 @@ fn airSaveErrReturnTraceIndex(self: *Self, inst: Air.Inst.Index) !void { |
| 2223 | 2235 | |
| 2224 | 2236 | /// T to E!T |
| 2225 | 2237 | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 2226 | const mod = self.bin_file.comp.module.?; | |
| 2238 | const pt = self.pt; | |
| 2239 | const mod = pt.zcu; | |
| 2227 | 2240 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2228 | 2241 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 2229 | 2242 | const error_union_ty = ty_op.ty.toType(); |
| 2230 | 2243 | const error_ty = error_union_ty.errorUnionSet(mod); |
| 2231 | 2244 | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 2232 | 2245 | const operand = try self.resolveInst(ty_op.operand); |
| 2233 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result operand; | |
| 2246 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result operand; | |
| 2234 | 2247 | |
| 2235 | const abi_size: u32 = @intCast(error_union_ty.abiSize(mod)); | |
| 2236 | const abi_align = error_union_ty.abiAlignment(mod); | |
| 2248 | const abi_size: u32 = @intCast(error_union_ty.abiSize(pt)); | |
| 2249 | const abi_align = error_union_ty.abiAlignment(pt); | |
| 2237 | 2250 | const stack_offset: u32 = @intCast(try self.allocMem(abi_size, abi_align, inst)); |
| 2238 | const payload_off = errUnionPayloadOffset(payload_ty, mod); | |
| 2239 | const err_off = errUnionErrorOffset(payload_ty, mod); | |
| 2251 | const payload_off = errUnionPayloadOffset(payload_ty, pt); | |
| 2252 | const err_off = errUnionErrorOffset(payload_ty, pt); | |
| 2240 | 2253 | try self.genSetStack(payload_ty, stack_offset - @as(u32, @intCast(payload_off)), operand); |
| 2241 | 2254 | try self.genSetStack(error_ty, stack_offset - @as(u32, @intCast(err_off)), .{ .immediate = 0 }); |
| 2242 | 2255 | |
| ... | ... | @@ -2247,20 +2260,21 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 2247 | 2260 | |
| 2248 | 2261 | /// E to E!T |
| 2249 | 2262 | fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void { |
| 2250 | const mod = self.bin_file.comp.module.?; | |
| 2263 | const pt = self.pt; | |
| 2264 | const mod = pt.zcu; | |
| 2251 | 2265 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2252 | 2266 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 2253 | 2267 | const error_union_ty = ty_op.ty.toType(); |
| 2254 | 2268 | const error_ty = error_union_ty.errorUnionSet(mod); |
| 2255 | 2269 | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 2256 | 2270 | const operand = try self.resolveInst(ty_op.operand); |
| 2257 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result operand; | |
| 2271 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result operand; | |
| 2258 | 2272 | |
| 2259 | const abi_size: u32 = @intCast(error_union_ty.abiSize(mod)); | |
| 2260 | const abi_align = error_union_ty.abiAlignment(mod); | |
| 2273 | const abi_size: u32 = @intCast(error_union_ty.abiSize(pt)); | |
| 2274 | const abi_align = error_union_ty.abiAlignment(pt); | |
| 2261 | 2275 | const stack_offset: u32 = @intCast(try self.allocMem(abi_size, abi_align, inst)); |
| 2262 | const payload_off = errUnionPayloadOffset(payload_ty, mod); | |
| 2263 | const err_off = errUnionErrorOffset(payload_ty, mod); | |
| 2276 | const payload_off = errUnionPayloadOffset(payload_ty, pt); | |
| 2277 | const err_off = errUnionErrorOffset(payload_ty, pt); | |
| 2264 | 2278 | try self.genSetStack(error_ty, stack_offset - @as(u32, @intCast(err_off)), operand); |
| 2265 | 2279 | try self.genSetStack(payload_ty, stack_offset - @as(u32, @intCast(payload_off)), .undef); |
| 2266 | 2280 | |
| ... | ... | @@ -2364,9 +2378,10 @@ fn ptrElemVal( |
| 2364 | 2378 | ptr_ty: Type, |
| 2365 | 2379 | maybe_inst: ?Air.Inst.Index, |
| 2366 | 2380 | ) !MCValue { |
| 2367 | const mod = self.bin_file.comp.module.?; | |
| 2381 | const pt = self.pt; | |
| 2382 | const mod = pt.zcu; | |
| 2368 | 2383 | const elem_ty = ptr_ty.childType(mod); |
| 2369 | const elem_size: u32 = @intCast(elem_ty.abiSize(mod)); | |
| 2384 | const elem_size: u32 = @intCast(elem_ty.abiSize(pt)); | |
| 2370 | 2385 | |
| 2371 | 2386 | switch (elem_size) { |
| 2372 | 2387 | 1, 4 => { |
| ... | ... | @@ -2423,7 +2438,8 @@ fn ptrElemVal( |
| 2423 | 2438 | } |
| 2424 | 2439 | |
| 2425 | 2440 | fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 2426 | const mod = self.bin_file.comp.module.?; | |
| 2441 | const pt = self.pt; | |
| 2442 | const mod = pt.zcu; | |
| 2427 | 2443 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2428 | 2444 | const slice_ty = self.typeOf(bin_op.lhs); |
| 2429 | 2445 | const result: MCValue = if (!slice_ty.isVolatilePtr(mod) and self.liveness.isUnused(inst)) .dead else result: { |
| ... | ... | @@ -2466,7 +2482,8 @@ fn arrayElemVal( |
| 2466 | 2482 | array_ty: Type, |
| 2467 | 2483 | maybe_inst: ?Air.Inst.Index, |
| 2468 | 2484 | ) InnerError!MCValue { |
| 2469 | const mod = self.bin_file.comp.module.?; | |
| 2485 | const pt = self.pt; | |
| 2486 | const mod = pt.zcu; | |
| 2470 | 2487 | const elem_ty = array_ty.childType(mod); |
| 2471 | 2488 | |
| 2472 | 2489 | const mcv = try array_bind.resolveToMcv(self); |
| ... | ... | @@ -2501,7 +2518,7 @@ fn arrayElemVal( |
| 2501 | 2518 | |
| 2502 | 2519 | const base_bind: ReadArg.Bind = .{ .mcv = ptr_to_mcv }; |
| 2503 | 2520 | |
| 2504 | const ptr_ty = try mod.singleMutPtrType(elem_ty); | |
| 2521 | const ptr_ty = try pt.singleMutPtrType(elem_ty); | |
| 2505 | 2522 | |
| 2506 | 2523 | return try self.ptrElemVal(base_bind, index_bind, ptr_ty, maybe_inst); |
| 2507 | 2524 | }, |
| ... | ... | @@ -2522,7 +2539,8 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 2522 | 2539 | } |
| 2523 | 2540 | |
| 2524 | 2541 | fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 2525 | const mod = self.bin_file.comp.module.?; | |
| 2542 | const pt = self.pt; | |
| 2543 | const mod = pt.zcu; | |
| 2526 | 2544 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2527 | 2545 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 2528 | 2546 | const result: MCValue = if (!ptr_ty.isVolatilePtr(mod) and self.liveness.isUnused(inst)) .dead else result: { |
| ... | ... | @@ -2656,9 +2674,10 @@ fn reuseOperand( |
| 2656 | 2674 | } |
| 2657 | 2675 | |
| 2658 | 2676 | fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!void { |
| 2659 | const mod = self.bin_file.comp.module.?; | |
| 2677 | const pt = self.pt; | |
| 2678 | const mod = pt.zcu; | |
| 2660 | 2679 | const elem_ty = ptr_ty.childType(mod); |
| 2661 | const elem_size: u32 = @intCast(elem_ty.abiSize(mod)); | |
| 2680 | const elem_size: u32 = @intCast(elem_ty.abiSize(pt)); | |
| 2662 | 2681 | |
| 2663 | 2682 | switch (ptr) { |
| 2664 | 2683 | .none => unreachable, |
| ... | ... | @@ -2733,11 +2752,12 @@ fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!vo |
| 2733 | 2752 | } |
| 2734 | 2753 | |
| 2735 | 2754 | fn airLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 2736 | const mod = self.bin_file.comp.module.?; | |
| 2755 | const pt = self.pt; | |
| 2756 | const mod = pt.zcu; | |
| 2737 | 2757 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2738 | 2758 | const elem_ty = self.typeOfIndex(inst); |
| 2739 | 2759 | const result: MCValue = result: { |
| 2740 | if (!elem_ty.hasRuntimeBits(mod)) | |
| 2760 | if (!elem_ty.hasRuntimeBits(pt)) | |
| 2741 | 2761 | break :result MCValue.none; |
| 2742 | 2762 | |
| 2743 | 2763 | const ptr = try self.resolveInst(ty_op.operand); |
| ... | ... | @@ -2746,7 +2766,7 @@ fn airLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 2746 | 2766 | break :result MCValue.dead; |
| 2747 | 2767 | |
| 2748 | 2768 | const dest_mcv: MCValue = blk: { |
| 2749 | const ptr_fits_dest = elem_ty.abiSize(mod) <= 4; | |
| 2769 | const ptr_fits_dest = elem_ty.abiSize(pt) <= 4; | |
| 2750 | 2770 | if (ptr_fits_dest and self.reuseOperand(inst, ty_op.operand, 0, ptr)) { |
| 2751 | 2771 | // The MCValue that holds the pointer can be re-used as the value. |
| 2752 | 2772 | break :blk ptr; |
| ... | ... | @@ -2762,8 +2782,8 @@ fn airLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 2762 | 2782 | } |
| 2763 | 2783 | |
| 2764 | 2784 | fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type) InnerError!void { |
| 2765 | const mod = self.bin_file.comp.module.?; | |
| 2766 | const elem_size: u32 = @intCast(value_ty.abiSize(mod)); | |
| 2785 | const pt = self.pt; | |
| 2786 | const elem_size: u32 = @intCast(value_ty.abiSize(pt)); | |
| 2767 | 2787 | |
| 2768 | 2788 | switch (ptr) { |
| 2769 | 2789 | .none => unreachable, |
| ... | ... | @@ -2882,11 +2902,12 @@ fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u8) !void { |
| 2882 | 2902 | |
| 2883 | 2903 | fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32) !MCValue { |
| 2884 | 2904 | return if (self.liveness.isUnused(inst)) .dead else result: { |
| 2885 | const mod = self.bin_file.comp.module.?; | |
| 2905 | const pt = self.pt; | |
| 2906 | const mod = pt.zcu; | |
| 2886 | 2907 | const mcv = try self.resolveInst(operand); |
| 2887 | 2908 | const ptr_ty = self.typeOf(operand); |
| 2888 | 2909 | const struct_ty = ptr_ty.childType(mod); |
| 2889 | const struct_field_offset: u32 = @intCast(struct_ty.structFieldOffset(index, mod)); | |
| 2910 | const struct_field_offset: u32 = @intCast(struct_ty.structFieldOffset(index, pt)); | |
| 2890 | 2911 | switch (mcv) { |
| 2891 | 2912 | .ptr_stack_offset => |off| { |
| 2892 | 2913 | break :result MCValue{ .ptr_stack_offset = off - struct_field_offset }; |
| ... | ... | @@ -2906,11 +2927,12 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 2906 | 2927 | const extra = self.air.extraData(Air.StructField, ty_pl.payload).data; |
| 2907 | 2928 | const operand = extra.struct_operand; |
| 2908 | 2929 | const index = extra.field_index; |
| 2909 | const mod = self.bin_file.comp.module.?; | |
| 2930 | const pt = self.pt; | |
| 2931 | const mod = pt.zcu; | |
| 2910 | 2932 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 2911 | 2933 | const mcv = try self.resolveInst(operand); |
| 2912 | 2934 | const struct_ty = self.typeOf(operand); |
| 2913 | const struct_field_offset: u32 = @intCast(struct_ty.structFieldOffset(index, mod)); | |
| 2935 | const struct_field_offset: u32 = @intCast(struct_ty.structFieldOffset(index, pt)); | |
| 2914 | 2936 | const struct_field_ty = struct_ty.structFieldType(index, mod); |
| 2915 | 2937 | |
| 2916 | 2938 | switch (mcv) { |
| ... | ... | @@ -2974,7 +2996,7 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 2974 | 2996 | ); |
| 2975 | 2997 | |
| 2976 | 2998 | const field_bit_offset = struct_field_offset * 8; |
| 2977 | const field_bit_size: u32 = @intCast(struct_field_ty.abiSize(mod) * 8); | |
| 2999 | const field_bit_size: u32 = @intCast(struct_field_ty.abiSize(pt) * 8); | |
| 2978 | 3000 | |
| 2979 | 3001 | _ = try self.addInst(.{ |
| 2980 | 3002 | .tag = if (struct_field_ty.isSignedInt(mod)) Mir.Inst.Tag.sbfx else .ubfx, |
| ... | ... | @@ -2996,7 +3018,8 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 2996 | 3018 | } |
| 2997 | 3019 | |
| 2998 | 3020 | fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 2999 | const mod = self.bin_file.comp.module.?; | |
| 3021 | const pt = self.pt; | |
| 3022 | const mod = pt.zcu; | |
| 3000 | 3023 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3001 | 3024 | const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| 3002 | 3025 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| ... | ... | @@ -3007,7 +3030,7 @@ fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 3007 | 3030 | return self.fail("TODO implement @fieldParentPtr codegen for unions", .{}); |
| 3008 | 3031 | } |
| 3009 | 3032 | |
| 3010 | const struct_field_offset: u32 = @intCast(struct_ty.structFieldOffset(extra.field_index, mod)); | |
| 3033 | const struct_field_offset: u32 = @intCast(struct_ty.structFieldOffset(extra.field_index, pt)); | |
| 3011 | 3034 | switch (field_ptr) { |
| 3012 | 3035 | .ptr_stack_offset => |off| { |
| 3013 | 3036 | break :result MCValue{ .ptr_stack_offset = off + struct_field_offset }; |
| ... | ... | @@ -3390,7 +3413,8 @@ fn addSub( |
| 3390 | 3413 | rhs_ty: Type, |
| 3391 | 3414 | maybe_inst: ?Air.Inst.Index, |
| 3392 | 3415 | ) InnerError!MCValue { |
| 3393 | const mod = self.bin_file.comp.module.?; | |
| 3416 | const pt = self.pt; | |
| 3417 | const mod = pt.zcu; | |
| 3394 | 3418 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3395 | 3419 | .Float => return self.fail("TODO ARM binary operations on floats", .{}), |
| 3396 | 3420 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| ... | ... | @@ -3446,7 +3470,8 @@ fn mul( |
| 3446 | 3470 | rhs_ty: Type, |
| 3447 | 3471 | maybe_inst: ?Air.Inst.Index, |
| 3448 | 3472 | ) InnerError!MCValue { |
| 3449 | const mod = self.bin_file.comp.module.?; | |
| 3473 | const pt = self.pt; | |
| 3474 | const mod = pt.zcu; | |
| 3450 | 3475 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3451 | 3476 | .Float => return self.fail("TODO ARM binary operations on floats", .{}), |
| 3452 | 3477 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| ... | ... | @@ -3479,7 +3504,8 @@ fn divFloat( |
| 3479 | 3504 | _ = rhs_ty; |
| 3480 | 3505 | _ = maybe_inst; |
| 3481 | 3506 | |
| 3482 | const mod = self.bin_file.comp.module.?; | |
| 3507 | const pt = self.pt; | |
| 3508 | const mod = pt.zcu; | |
| 3483 | 3509 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3484 | 3510 | .Float => return self.fail("TODO ARM binary operations on floats", .{}), |
| 3485 | 3511 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| ... | ... | @@ -3495,7 +3521,8 @@ fn divTrunc( |
| 3495 | 3521 | rhs_ty: Type, |
| 3496 | 3522 | maybe_inst: ?Air.Inst.Index, |
| 3497 | 3523 | ) InnerError!MCValue { |
| 3498 | const mod = self.bin_file.comp.module.?; | |
| 3524 | const pt = self.pt; | |
| 3525 | const mod = pt.zcu; | |
| 3499 | 3526 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3500 | 3527 | .Float => return self.fail("TODO ARM binary operations on floats", .{}), |
| 3501 | 3528 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| ... | ... | @@ -3538,7 +3565,8 @@ fn divFloor( |
| 3538 | 3565 | rhs_ty: Type, |
| 3539 | 3566 | maybe_inst: ?Air.Inst.Index, |
| 3540 | 3567 | ) InnerError!MCValue { |
| 3541 | const mod = self.bin_file.comp.module.?; | |
| 3568 | const pt = self.pt; | |
| 3569 | const mod = pt.zcu; | |
| 3542 | 3570 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3543 | 3571 | .Float => return self.fail("TODO ARM binary operations on floats", .{}), |
| 3544 | 3572 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| ... | ... | @@ -3586,7 +3614,8 @@ fn divExact( |
| 3586 | 3614 | _ = rhs_ty; |
| 3587 | 3615 | _ = maybe_inst; |
| 3588 | 3616 | |
| 3589 | const mod = self.bin_file.comp.module.?; | |
| 3617 | const pt = self.pt; | |
| 3618 | const mod = pt.zcu; | |
| 3590 | 3619 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3591 | 3620 | .Float => return self.fail("TODO ARM binary operations on floats", .{}), |
| 3592 | 3621 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| ... | ... | @@ -3603,7 +3632,8 @@ fn rem( |
| 3603 | 3632 | rhs_ty: Type, |
| 3604 | 3633 | maybe_inst: ?Air.Inst.Index, |
| 3605 | 3634 | ) InnerError!MCValue { |
| 3606 | const mod = self.bin_file.comp.module.?; | |
| 3635 | const pt = self.pt; | |
| 3636 | const mod = pt.zcu; | |
| 3607 | 3637 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3608 | 3638 | .Float => return self.fail("TODO ARM binary operations on floats", .{}), |
| 3609 | 3639 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| ... | ... | @@ -3672,7 +3702,8 @@ fn modulo( |
| 3672 | 3702 | _ = rhs_ty; |
| 3673 | 3703 | _ = maybe_inst; |
| 3674 | 3704 | |
| 3675 | const mod = self.bin_file.comp.module.?; | |
| 3705 | const pt = self.pt; | |
| 3706 | const mod = pt.zcu; | |
| 3676 | 3707 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3677 | 3708 | .Float => return self.fail("TODO ARM binary operations on floats", .{}), |
| 3678 | 3709 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| ... | ... | @@ -3690,7 +3721,8 @@ fn wrappingArithmetic( |
| 3690 | 3721 | rhs_ty: Type, |
| 3691 | 3722 | maybe_inst: ?Air.Inst.Index, |
| 3692 | 3723 | ) InnerError!MCValue { |
| 3693 | const mod = self.bin_file.comp.module.?; | |
| 3724 | const pt = self.pt; | |
| 3725 | const mod = pt.zcu; | |
| 3694 | 3726 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3695 | 3727 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| 3696 | 3728 | .Int => { |
| ... | ... | @@ -3728,7 +3760,8 @@ fn bitwise( |
| 3728 | 3760 | rhs_ty: Type, |
| 3729 | 3761 | maybe_inst: ?Air.Inst.Index, |
| 3730 | 3762 | ) InnerError!MCValue { |
| 3731 | const mod = self.bin_file.comp.module.?; | |
| 3763 | const pt = self.pt; | |
| 3764 | const mod = pt.zcu; | |
| 3732 | 3765 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3733 | 3766 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| 3734 | 3767 | .Int => { |
| ... | ... | @@ -3773,7 +3806,8 @@ fn shiftExact( |
| 3773 | 3806 | rhs_ty: Type, |
| 3774 | 3807 | maybe_inst: ?Air.Inst.Index, |
| 3775 | 3808 | ) InnerError!MCValue { |
| 3776 | const mod = self.bin_file.comp.module.?; | |
| 3809 | const pt = self.pt; | |
| 3810 | const mod = pt.zcu; | |
| 3777 | 3811 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3778 | 3812 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| 3779 | 3813 | .Int => { |
| ... | ... | @@ -3812,7 +3846,8 @@ fn shiftNormal( |
| 3812 | 3846 | rhs_ty: Type, |
| 3813 | 3847 | maybe_inst: ?Air.Inst.Index, |
| 3814 | 3848 | ) InnerError!MCValue { |
| 3815 | const mod = self.bin_file.comp.module.?; | |
| 3849 | const pt = self.pt; | |
| 3850 | const mod = pt.zcu; | |
| 3816 | 3851 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3817 | 3852 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| 3818 | 3853 | .Int => { |
| ... | ... | @@ -3855,7 +3890,8 @@ fn booleanOp( |
| 3855 | 3890 | rhs_ty: Type, |
| 3856 | 3891 | maybe_inst: ?Air.Inst.Index, |
| 3857 | 3892 | ) InnerError!MCValue { |
| 3858 | const mod = self.bin_file.comp.module.?; | |
| 3893 | const pt = self.pt; | |
| 3894 | const mod = pt.zcu; | |
| 3859 | 3895 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3860 | 3896 | .Bool => { |
| 3861 | 3897 | const lhs_immediate = try lhs_bind.resolveToImmediate(self); |
| ... | ... | @@ -3889,7 +3925,8 @@ fn ptrArithmetic( |
| 3889 | 3925 | rhs_ty: Type, |
| 3890 | 3926 | maybe_inst: ?Air.Inst.Index, |
| 3891 | 3927 | ) InnerError!MCValue { |
| 3892 | const mod = self.bin_file.comp.module.?; | |
| 3928 | const pt = self.pt; | |
| 3929 | const mod = pt.zcu; | |
| 3893 | 3930 | switch (lhs_ty.zigTypeTag(mod)) { |
| 3894 | 3931 | .Pointer => { |
| 3895 | 3932 | assert(rhs_ty.eql(Type.usize, mod)); |
| ... | ... | @@ -3899,7 +3936,7 @@ fn ptrArithmetic( |
| 3899 | 3936 | .One => ptr_ty.childType(mod).childType(mod), // ptr to array, so get array element type |
| 3900 | 3937 | else => ptr_ty.childType(mod), |
| 3901 | 3938 | }; |
| 3902 | const elem_size: u32 = @intCast(elem_ty.abiSize(mod)); | |
| 3939 | const elem_size: u32 = @intCast(elem_ty.abiSize(pt)); | |
| 3903 | 3940 | |
| 3904 | 3941 | const base_tag: Air.Inst.Tag = switch (tag) { |
| 3905 | 3942 | .ptr_add => .add, |
| ... | ... | @@ -3926,8 +3963,9 @@ fn ptrArithmetic( |
| 3926 | 3963 | } |
| 3927 | 3964 | |
| 3928 | 3965 | fn genLdrRegister(self: *Self, dest_reg: Register, addr_reg: Register, ty: Type) !void { |
| 3929 | const mod = self.bin_file.comp.module.?; | |
| 3930 | const abi_size = ty.abiSize(mod); | |
| 3966 | const pt = self.pt; | |
| 3967 | const mod = pt.zcu; | |
| 3968 | const abi_size = ty.abiSize(pt); | |
| 3931 | 3969 | |
| 3932 | 3970 | const tag: Mir.Inst.Tag = switch (abi_size) { |
| 3933 | 3971 | 1 => if (ty.isSignedInt(mod)) Mir.Inst.Tag.ldrsb else .ldrb, |
| ... | ... | @@ -3961,8 +3999,8 @@ fn genLdrRegister(self: *Self, dest_reg: Register, addr_reg: Register, ty: Type) |
| 3961 | 3999 | } |
| 3962 | 4000 | |
| 3963 | 4001 | fn genStrRegister(self: *Self, source_reg: Register, addr_reg: Register, ty: Type) !void { |
| 3964 | const mod = self.bin_file.comp.module.?; | |
| 3965 | const abi_size = ty.abiSize(mod); | |
| 4002 | const pt = self.pt; | |
| 4003 | const abi_size = ty.abiSize(pt); | |
| 3966 | 4004 | |
| 3967 | 4005 | const tag: Mir.Inst.Tag = switch (abi_size) { |
| 3968 | 4006 | 1 => .strb, |
| ... | ... | @@ -4168,7 +4206,8 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void { |
| 4168 | 4206 | while (self.args[arg_index] == .none) arg_index += 1; |
| 4169 | 4207 | self.arg_index = arg_index + 1; |
| 4170 | 4208 | |
| 4171 | const mod = self.bin_file.comp.module.?; | |
| 4209 | const pt = self.pt; | |
| 4210 | const mod = pt.zcu; | |
| 4172 | 4211 | const ty = self.typeOfIndex(inst); |
| 4173 | 4212 | const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 4174 | 4213 | const src_index = self.air.instructions.items(.data)[@intFromEnum(inst)].arg.src_index; |
| ... | ... | @@ -4223,7 +4262,8 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier |
| 4223 | 4262 | const extra = self.air.extraData(Air.Call, pl_op.payload); |
| 4224 | 4263 | const args: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra.end..][0..extra.data.args_len]); |
| 4225 | 4264 | const ty = self.typeOf(callee); |
| 4226 | const mod = self.bin_file.comp.module.?; | |
| 4265 | const pt = self.pt; | |
| 4266 | const mod = pt.zcu; | |
| 4227 | 4267 | |
| 4228 | 4268 | const fn_ty = switch (ty.zigTypeTag(mod)) { |
| 4229 | 4269 | .Fn => ty, |
| ... | ... | @@ -4253,11 +4293,11 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier |
| 4253 | 4293 | const r0_lock: ?RegisterLock = if (info.return_value == .stack_offset) blk: { |
| 4254 | 4294 | log.debug("airCall: return by reference", .{}); |
| 4255 | 4295 | const ret_ty = fn_ty.fnReturnType(mod); |
| 4256 | const ret_abi_size: u32 = @intCast(ret_ty.abiSize(mod)); | |
| 4257 | const ret_abi_align = ret_ty.abiAlignment(mod); | |
| 4296 | const ret_abi_size: u32 = @intCast(ret_ty.abiSize(pt)); | |
| 4297 | const ret_abi_align = ret_ty.abiAlignment(pt); | |
| 4258 | 4298 | const stack_offset = try self.allocMem(ret_abi_size, ret_abi_align, inst); |
| 4259 | 4299 | |
| 4260 | const ptr_ty = try mod.singleMutPtrType(ret_ty); | |
| 4300 | const ptr_ty = try pt.singleMutPtrType(ret_ty); | |
| 4261 | 4301 | try self.register_manager.getReg(.r0, null); |
| 4262 | 4302 | try self.genSetReg(ptr_ty, .r0, .{ .ptr_stack_offset = stack_offset }); |
| 4263 | 4303 | |
| ... | ... | @@ -4293,7 +4333,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier |
| 4293 | 4333 | |
| 4294 | 4334 | // Due to incremental compilation, how function calls are generated depends |
| 4295 | 4335 | // on linking. |
| 4296 | if (try self.air.value(callee, mod)) |func_value| { | |
| 4336 | if (try self.air.value(callee, pt)) |func_value| { | |
| 4297 | 4337 | if (func_value.getFunction(mod)) |func| { |
| 4298 | 4338 | if (self.bin_file.cast(link.File.Elf)) |elf_file| { |
| 4299 | 4339 | const sym_index = try elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, func.owner_decl); |
| ... | ... | @@ -4374,7 +4414,8 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier |
| 4374 | 4414 | } |
| 4375 | 4415 | |
| 4376 | 4416 | fn airRet(self: *Self, inst: Air.Inst.Index) !void { |
| 4377 | const mod = self.bin_file.comp.module.?; | |
| 4417 | const pt = self.pt; | |
| 4418 | const mod = pt.zcu; | |
| 4378 | 4419 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4379 | 4420 | const operand = try self.resolveInst(un_op); |
| 4380 | 4421 | const ret_ty = self.fn_type.fnReturnType(mod); |
| ... | ... | @@ -4393,7 +4434,7 @@ fn airRet(self: *Self, inst: Air.Inst.Index) !void { |
| 4393 | 4434 | // |
| 4394 | 4435 | // self.ret_mcv is an address to where this function |
| 4395 | 4436 | // should store its result into |
| 4396 | const ptr_ty = try mod.singleMutPtrType(ret_ty); | |
| 4437 | const ptr_ty = try pt.singleMutPtrType(ret_ty); | |
| 4397 | 4438 | try self.store(self.ret_mcv, operand, ptr_ty, ret_ty); |
| 4398 | 4439 | }, |
| 4399 | 4440 | else => unreachable, // invalid return result |
| ... | ... | @@ -4406,7 +4447,8 @@ fn airRet(self: *Self, inst: Air.Inst.Index) !void { |
| 4406 | 4447 | } |
| 4407 | 4448 | |
| 4408 | 4449 | fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 4409 | const mod = self.bin_file.comp.module.?; | |
| 4450 | const pt = self.pt; | |
| 4451 | const mod = pt.zcu; | |
| 4410 | 4452 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4411 | 4453 | const ptr = try self.resolveInst(un_op); |
| 4412 | 4454 | const ptr_ty = self.typeOf(un_op); |
| ... | ... | @@ -4430,8 +4472,8 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 4430 | 4472 | // location. |
| 4431 | 4473 | const op_inst = un_op.toIndex().?; |
| 4432 | 4474 | if (self.air.instructions.items(.tag)[@intFromEnum(op_inst)] != .ret_ptr) { |
| 4433 | const abi_size: u32 = @intCast(ret_ty.abiSize(mod)); | |
| 4434 | const abi_align = ret_ty.abiAlignment(mod); | |
| 4475 | const abi_size: u32 = @intCast(ret_ty.abiSize(pt)); | |
| 4476 | const abi_align = ret_ty.abiAlignment(pt); | |
| 4435 | 4477 | |
| 4436 | 4478 | const offset = try self.allocMem(abi_size, abi_align, null); |
| 4437 | 4479 | |
| ... | ... | @@ -4467,11 +4509,12 @@ fn cmp( |
| 4467 | 4509 | lhs_ty: Type, |
| 4468 | 4510 | op: math.CompareOperator, |
| 4469 | 4511 | ) !MCValue { |
| 4470 | const mod = self.bin_file.comp.module.?; | |
| 4512 | const pt = self.pt; | |
| 4513 | const mod = pt.zcu; | |
| 4471 | 4514 | const int_ty = switch (lhs_ty.zigTypeTag(mod)) { |
| 4472 | 4515 | .Optional => blk: { |
| 4473 | 4516 | const payload_ty = lhs_ty.optionalChild(mod); |
| 4474 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4517 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4475 | 4518 | break :blk Type.u1; |
| 4476 | 4519 | } else if (lhs_ty.isPtrLikeOptional(mod)) { |
| 4477 | 4520 | break :blk Type.usize; |
| ... | ... | @@ -4573,7 +4616,8 @@ fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void { |
| 4573 | 4616 | } |
| 4574 | 4617 | |
| 4575 | 4618 | fn airDbgInlineBlock(self: *Self, inst: Air.Inst.Index) !void { |
| 4576 | const mod = self.bin_file.comp.module.?; | |
| 4619 | const pt = self.pt; | |
| 4620 | const mod = pt.zcu; | |
| 4577 | 4621 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4578 | 4622 | const extra = self.air.extraData(Air.DbgInlineBlock, ty_pl.payload); |
| 4579 | 4623 | const func = mod.funcInfo(extra.data.func); |
| ... | ... | @@ -4785,9 +4829,10 @@ fn isNull( |
| 4785 | 4829 | operand_bind: ReadArg.Bind, |
| 4786 | 4830 | operand_ty: Type, |
| 4787 | 4831 | ) !MCValue { |
| 4788 | const mod = self.bin_file.comp.module.?; | |
| 4832 | const pt = self.pt; | |
| 4833 | const mod = pt.zcu; | |
| 4789 | 4834 | if (operand_ty.isPtrLikeOptional(mod)) { |
| 4790 | assert(operand_ty.abiSize(mod) == 4); | |
| 4835 | assert(operand_ty.abiSize(pt) == 4); | |
| 4791 | 4836 | |
| 4792 | 4837 | const imm_bind: ReadArg.Bind = .{ .mcv = .{ .immediate = 0 } }; |
| 4793 | 4838 | return self.cmp(operand_bind, imm_bind, Type.usize, .eq); |
| ... | ... | @@ -4819,7 +4864,8 @@ fn airIsNull(self: *Self, inst: Air.Inst.Index) !void { |
| 4819 | 4864 | } |
| 4820 | 4865 | |
| 4821 | 4866 | fn airIsNullPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4822 | const mod = self.bin_file.comp.module.?; | |
| 4867 | const pt = self.pt; | |
| 4868 | const mod = pt.zcu; | |
| 4823 | 4869 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4824 | 4870 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 4825 | 4871 | const operand_ptr = try self.resolveInst(un_op); |
| ... | ... | @@ -4846,7 +4892,8 @@ fn airIsNonNull(self: *Self, inst: Air.Inst.Index) !void { |
| 4846 | 4892 | } |
| 4847 | 4893 | |
| 4848 | 4894 | fn airIsNonNullPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4849 | const mod = self.bin_file.comp.module.?; | |
| 4895 | const pt = self.pt; | |
| 4896 | const mod = pt.zcu; | |
| 4850 | 4897 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4851 | 4898 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 4852 | 4899 | const operand_ptr = try self.resolveInst(un_op); |
| ... | ... | @@ -4866,7 +4913,8 @@ fn isErr( |
| 4866 | 4913 | error_union_bind: ReadArg.Bind, |
| 4867 | 4914 | error_union_ty: Type, |
| 4868 | 4915 | ) !MCValue { |
| 4869 | const mod = self.bin_file.comp.module.?; | |
| 4916 | const pt = self.pt; | |
| 4917 | const mod = pt.zcu; | |
| 4870 | 4918 | const error_type = error_union_ty.errorUnionSet(mod); |
| 4871 | 4919 | |
| 4872 | 4920 | if (error_type.errorSetIsEmpty(mod)) { |
| ... | ... | @@ -4908,7 +4956,8 @@ fn airIsErr(self: *Self, inst: Air.Inst.Index) !void { |
| 4908 | 4956 | } |
| 4909 | 4957 | |
| 4910 | 4958 | fn airIsErrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4911 | const mod = self.bin_file.comp.module.?; | |
| 4959 | const pt = self.pt; | |
| 4960 | const mod = pt.zcu; | |
| 4912 | 4961 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4913 | 4962 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 4914 | 4963 | const operand_ptr = try self.resolveInst(un_op); |
| ... | ... | @@ -4935,7 +4984,8 @@ fn airIsNonErr(self: *Self, inst: Air.Inst.Index) !void { |
| 4935 | 4984 | } |
| 4936 | 4985 | |
| 4937 | 4986 | fn airIsNonErrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 4938 | const mod = self.bin_file.comp.module.?; | |
| 4987 | const pt = self.pt; | |
| 4988 | const mod = pt.zcu; | |
| 4939 | 4989 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4940 | 4990 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 4941 | 4991 | const operand_ptr = try self.resolveInst(un_op); |
| ... | ... | @@ -5154,10 +5204,10 @@ fn airBr(self: *Self, inst: Air.Inst.Index) !void { |
| 5154 | 5204 | } |
| 5155 | 5205 | |
| 5156 | 5206 | fn br(self: *Self, block: Air.Inst.Index, operand: Air.Inst.Ref) !void { |
| 5157 | const mod = self.bin_file.comp.module.?; | |
| 5207 | const pt = self.pt; | |
| 5158 | 5208 | const block_data = self.blocks.getPtr(block).?; |
| 5159 | 5209 | |
| 5160 | if (self.typeOf(operand).hasRuntimeBits(mod)) { | |
| 5210 | if (self.typeOf(operand).hasRuntimeBits(pt)) { | |
| 5161 | 5211 | const operand_mcv = try self.resolveInst(operand); |
| 5162 | 5212 | const block_mcv = block_data.mcv; |
| 5163 | 5213 | if (block_mcv == .none) { |
| ... | ... | @@ -5325,8 +5375,9 @@ fn setRegOrMem(self: *Self, ty: Type, loc: MCValue, val: MCValue) !void { |
| 5325 | 5375 | } |
| 5326 | 5376 | |
| 5327 | 5377 | fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerError!void { |
| 5328 | const mod = self.bin_file.comp.module.?; | |
| 5329 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 5378 | const pt = self.pt; | |
| 5379 | const mod = pt.zcu; | |
| 5380 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 5330 | 5381 | switch (mcv) { |
| 5331 | 5382 | .dead => unreachable, |
| 5332 | 5383 | .unreach, .none => return, // Nothing to do. |
| ... | ... | @@ -5407,7 +5458,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro |
| 5407 | 5458 | try self.genSetStack(wrapped_ty, stack_offset, .{ .register = reg }); |
| 5408 | 5459 | |
| 5409 | 5460 | const overflow_bit_ty = ty.structFieldType(1, mod); |
| 5410 | const overflow_bit_offset: u32 = @intCast(ty.structFieldOffset(1, mod)); | |
| 5461 | const overflow_bit_offset: u32 = @intCast(ty.structFieldOffset(1, pt)); | |
| 5411 | 5462 | const cond_reg = try self.register_manager.allocReg(null, gp); |
| 5412 | 5463 | |
| 5413 | 5464 | // C flag: movcs reg, #1 |
| ... | ... | @@ -5445,7 +5496,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro |
| 5445 | 5496 | const reg = try self.copyToTmpRegister(ty, mcv); |
| 5446 | 5497 | return self.genSetStack(ty, stack_offset, MCValue{ .register = reg }); |
| 5447 | 5498 | } else { |
| 5448 | const ptr_ty = try mod.singleMutPtrType(ty); | |
| 5499 | const ptr_ty = try pt.singleMutPtrType(ty); | |
| 5449 | 5500 | |
| 5450 | 5501 | // TODO call extern memcpy |
| 5451 | 5502 | const regs = try self.register_manager.allocRegs(5, .{ null, null, null, null, null }, gp); |
| ... | ... | @@ -5487,7 +5538,8 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro |
| 5487 | 5538 | } |
| 5488 | 5539 | |
| 5489 | 5540 | fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void { |
| 5490 | const mod = self.bin_file.comp.module.?; | |
| 5541 | const pt = self.pt; | |
| 5542 | const mod = pt.zcu; | |
| 5491 | 5543 | switch (mcv) { |
| 5492 | 5544 | .dead => unreachable, |
| 5493 | 5545 | .unreach, .none => return, // Nothing to do. |
| ... | ... | @@ -5662,7 +5714,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void |
| 5662 | 5714 | }, |
| 5663 | 5715 | .stack_offset => |off| { |
| 5664 | 5716 | // TODO: maybe addressing from sp instead of fp |
| 5665 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 5717 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 5666 | 5718 | |
| 5667 | 5719 | const tag: Mir.Inst.Tag = switch (abi_size) { |
| 5668 | 5720 | 1 => if (ty.isSignedInt(mod)) Mir.Inst.Tag.ldrsb else .ldrb, |
| ... | ... | @@ -5713,7 +5765,7 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void |
| 5713 | 5765 | } |
| 5714 | 5766 | }, |
| 5715 | 5767 | .stack_argument_offset => |off| { |
| 5716 | const abi_size = ty.abiSize(mod); | |
| 5768 | const abi_size = ty.abiSize(pt); | |
| 5717 | 5769 | |
| 5718 | 5770 | const tag: Mir.Inst.Tag = switch (abi_size) { |
| 5719 | 5771 | 1 => if (ty.isSignedInt(mod)) Mir.Inst.Tag.ldrsb_stack_argument else .ldrb_stack_argument, |
| ... | ... | @@ -5734,8 +5786,8 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void |
| 5734 | 5786 | } |
| 5735 | 5787 | |
| 5736 | 5788 | fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerError!void { |
| 5737 | const mod = self.bin_file.comp.module.?; | |
| 5738 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 5789 | const pt = self.pt; | |
| 5790 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 5739 | 5791 | switch (mcv) { |
| 5740 | 5792 | .dead => unreachable, |
| 5741 | 5793 | .none, .unreach => return, |
| ... | ... | @@ -5802,7 +5854,7 @@ fn genSetStackArgument(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) I |
| 5802 | 5854 | const reg = try self.copyToTmpRegister(ty, mcv); |
| 5803 | 5855 | return self.genSetStackArgument(ty, stack_offset, MCValue{ .register = reg }); |
| 5804 | 5856 | } else { |
| 5805 | const ptr_ty = try mod.singleMutPtrType(ty); | |
| 5857 | const ptr_ty = try pt.singleMutPtrType(ty); | |
| 5806 | 5858 | |
| 5807 | 5859 | // TODO call extern memcpy |
| 5808 | 5860 | const regs = try self.register_manager.allocRegs(5, .{ null, null, null, null, null }, gp); |
| ... | ... | @@ -5890,7 +5942,8 @@ fn airBitCast(self: *Self, inst: Air.Inst.Index) !void { |
| 5890 | 5942 | } |
| 5891 | 5943 | |
| 5892 | 5944 | fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 5893 | const mod = self.bin_file.comp.module.?; | |
| 5945 | const pt = self.pt; | |
| 5946 | const mod = pt.zcu; | |
| 5894 | 5947 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5895 | 5948 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 5896 | 5949 | const ptr_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -6009,7 +6062,8 @@ fn airReduce(self: *Self, inst: Air.Inst.Index) !void { |
| 6009 | 6062 | } |
| 6010 | 6063 | |
| 6011 | 6064 | fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| 6012 | const mod = self.bin_file.comp.module.?; | |
| 6065 | const pt = self.pt; | |
| 6066 | const mod = pt.zcu; | |
| 6013 | 6067 | const vector_ty = self.typeOfIndex(inst); |
| 6014 | 6068 | const len = vector_ty.vectorLen(mod); |
| 6015 | 6069 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| ... | ... | @@ -6054,15 +6108,15 @@ fn airMulAdd(self: *Self, inst: Air.Inst.Index) !void { |
| 6054 | 6108 | } |
| 6055 | 6109 | |
| 6056 | 6110 | fn airTry(self: *Self, inst: Air.Inst.Index) !void { |
| 6111 | const pt = self.pt; | |
| 6057 | 6112 | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6058 | 6113 | const extra = self.air.extraData(Air.Try, pl_op.payload); |
| 6059 | 6114 | const body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra.end..][0..extra.data.body_len]); |
| 6060 | 6115 | const result: MCValue = result: { |
| 6061 | 6116 | const error_union_bind: ReadArg.Bind = .{ .inst = pl_op.operand }; |
| 6062 | 6117 | const error_union_ty = self.typeOf(pl_op.operand); |
| 6063 | const mod = self.bin_file.comp.module.?; | |
| 6064 | const error_union_size: u32 = @intCast(error_union_ty.abiSize(mod)); | |
| 6065 | const error_union_align = error_union_ty.abiAlignment(mod); | |
| 6118 | const error_union_size: u32 = @intCast(error_union_ty.abiSize(pt)); | |
| 6119 | const error_union_align = error_union_ty.abiAlignment(pt); | |
| 6066 | 6120 | |
| 6067 | 6121 | // The error union will die in the body. However, we need the |
| 6068 | 6122 | // error union after the body in order to extract the payload |
| ... | ... | @@ -6091,14 +6145,15 @@ fn airTryPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 6091 | 6145 | } |
| 6092 | 6146 | |
| 6093 | 6147 | fn resolveInst(self: *Self, inst: Air.Inst.Ref) InnerError!MCValue { |
| 6094 | const mod = self.bin_file.comp.module.?; | |
| 6148 | const pt = self.pt; | |
| 6149 | const mod = pt.zcu; | |
| 6095 | 6150 | |
| 6096 | 6151 | // If the type has no codegen bits, no need to store it. |
| 6097 | 6152 | const inst_ty = self.typeOf(inst); |
| 6098 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(mod) and !inst_ty.isError(mod)) | |
| 6153 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(pt) and !inst_ty.isError(mod)) | |
| 6099 | 6154 | return MCValue{ .none = {} }; |
| 6100 | 6155 | |
| 6101 | const inst_index = inst.toIndex() orelse return self.genTypedValue((try self.air.value(inst, mod)).?); | |
| 6156 | const inst_index = inst.toIndex() orelse return self.genTypedValue((try self.air.value(inst, pt)).?); | |
| 6102 | 6157 | |
| 6103 | 6158 | return self.getResolvedInstValue(inst_index); |
| 6104 | 6159 | } |
| ... | ... | @@ -6116,12 +6171,13 @@ fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) MCValue { |
| 6116 | 6171 | } |
| 6117 | 6172 | |
| 6118 | 6173 | fn genTypedValue(self: *Self, val: Value) InnerError!MCValue { |
| 6119 | const mod = self.bin_file.comp.module.?; | |
| 6174 | const pt = self.pt; | |
| 6120 | 6175 | const mcv: MCValue = switch (try codegen.genTypedValue( |
| 6121 | 6176 | self.bin_file, |
| 6177 | pt, | |
| 6122 | 6178 | self.src_loc, |
| 6123 | 6179 | val, |
| 6124 | mod.funcOwnerDeclIndex(self.func_index), | |
| 6180 | pt.zcu.funcOwnerDeclIndex(self.func_index), | |
| 6125 | 6181 | )) { |
| 6126 | 6182 | .mcv => |mcv| switch (mcv) { |
| 6127 | 6183 | .none => .none, |
| ... | ... | @@ -6152,7 +6208,8 @@ const CallMCValues = struct { |
| 6152 | 6208 | |
| 6153 | 6209 | /// Caller must call `CallMCValues.deinit`. |
| 6154 | 6210 | fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6155 | const mod = self.bin_file.comp.module.?; | |
| 6211 | const pt = self.pt; | |
| 6212 | const mod = pt.zcu; | |
| 6156 | 6213 | const ip = &mod.intern_pool; |
| 6157 | 6214 | const fn_info = mod.typeToFunc(fn_ty).?; |
| 6158 | 6215 | const cc = fn_info.cc; |
| ... | ... | @@ -6182,10 +6239,10 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6182 | 6239 | |
| 6183 | 6240 | if (ret_ty.zigTypeTag(mod) == .NoReturn) { |
| 6184 | 6241 | result.return_value = .{ .unreach = {} }; |
| 6185 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 6242 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 6186 | 6243 | result.return_value = .{ .none = {} }; |
| 6187 | 6244 | } else { |
| 6188 | const ret_ty_size: u32 = @intCast(ret_ty.abiSize(mod)); | |
| 6245 | const ret_ty_size: u32 = @intCast(ret_ty.abiSize(pt)); | |
| 6189 | 6246 | // TODO handle cases where multiple registers are used |
| 6190 | 6247 | if (ret_ty_size <= 4) { |
| 6191 | 6248 | result.return_value = .{ .register = c_abi_int_return_regs[0] }; |
| ... | ... | @@ -6200,10 +6257,10 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6200 | 6257 | } |
| 6201 | 6258 | |
| 6202 | 6259 | for (fn_info.param_types.get(ip), result.args) |ty, *result_arg| { |
| 6203 | if (Type.fromInterned(ty).abiAlignment(mod) == .@"8") | |
| 6260 | if (Type.fromInterned(ty).abiAlignment(pt) == .@"8") | |
| 6204 | 6261 | ncrn = std.mem.alignForward(usize, ncrn, 2); |
| 6205 | 6262 | |
| 6206 | const param_size: u32 = @intCast(Type.fromInterned(ty).abiSize(mod)); | |
| 6263 | const param_size: u32 = @intCast(Type.fromInterned(ty).abiSize(pt)); | |
| 6207 | 6264 | if (std.math.divCeil(u32, param_size, 4) catch unreachable <= 4 - ncrn) { |
| 6208 | 6265 | if (param_size <= 4) { |
| 6209 | 6266 | result_arg.* = .{ .register = c_abi_int_param_regs[ncrn] }; |
| ... | ... | @@ -6215,7 +6272,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6215 | 6272 | return self.fail("TODO MCValues split between registers and stack", .{}); |
| 6216 | 6273 | } else { |
| 6217 | 6274 | ncrn = 4; |
| 6218 | if (Type.fromInterned(ty).abiAlignment(mod) == .@"8") | |
| 6275 | if (Type.fromInterned(ty).abiAlignment(pt) == .@"8") | |
| 6219 | 6276 | nsaa = std.mem.alignForward(u32, nsaa, 8); |
| 6220 | 6277 | |
| 6221 | 6278 | result_arg.* = .{ .stack_argument_offset = nsaa }; |
| ... | ... | @@ -6229,10 +6286,10 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6229 | 6286 | .Unspecified => { |
| 6230 | 6287 | if (ret_ty.zigTypeTag(mod) == .NoReturn) { |
| 6231 | 6288 | result.return_value = .{ .unreach = {} }; |
| 6232 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod) and !ret_ty.isError(mod)) { | |
| 6289 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt) and !ret_ty.isError(mod)) { | |
| 6233 | 6290 | result.return_value = .{ .none = {} }; |
| 6234 | 6291 | } else { |
| 6235 | const ret_ty_size: u32 = @intCast(ret_ty.abiSize(mod)); | |
| 6292 | const ret_ty_size: u32 = @intCast(ret_ty.abiSize(pt)); | |
| 6236 | 6293 | if (ret_ty_size == 0) { |
| 6237 | 6294 | assert(ret_ty.isError(mod)); |
| 6238 | 6295 | result.return_value = .{ .immediate = 0 }; |
| ... | ... | @@ -6250,9 +6307,9 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6250 | 6307 | var stack_offset: u32 = 0; |
| 6251 | 6308 | |
| 6252 | 6309 | for (fn_info.param_types.get(ip), result.args) |ty, *result_arg| { |
| 6253 | if (Type.fromInterned(ty).abiSize(mod) > 0) { | |
| 6254 | const param_size: u32 = @intCast(Type.fromInterned(ty).abiSize(mod)); | |
| 6255 | const param_alignment = Type.fromInterned(ty).abiAlignment(mod); | |
| 6310 | if (Type.fromInterned(ty).abiSize(pt) > 0) { | |
| 6311 | const param_size: u32 = @intCast(Type.fromInterned(ty).abiSize(pt)); | |
| 6312 | const param_alignment = Type.fromInterned(ty).abiAlignment(pt); | |
| 6256 | 6313 | |
| 6257 | 6314 | stack_offset = @intCast(param_alignment.forward(stack_offset)); |
| 6258 | 6315 | result_arg.* = .{ .stack_argument_offset = stack_offset }; |
| ... | ... | @@ -6271,7 +6328,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) !CallMCValues { |
| 6271 | 6328 | return result; |
| 6272 | 6329 | } |
| 6273 | 6330 | |
| 6274 | /// TODO support scope overrides. Also note this logic is duplicated with `Module.wantSafety`. | |
| 6331 | /// TODO support scope overrides. Also note this logic is duplicated with `Zcu.wantSafety`. | |
| 6275 | 6332 | fn wantSafety(self: *Self) bool { |
| 6276 | 6333 | return switch (self.bin_file.comp.root_mod.optimize_mode) { |
| 6277 | 6334 | .Debug => true, |
| ... | ... | @@ -6305,11 +6362,9 @@ fn parseRegName(name: []const u8) ?Register { |
| 6305 | 6362 | } |
| 6306 | 6363 | |
| 6307 | 6364 | fn typeOf(self: *Self, inst: Air.Inst.Ref) Type { |
| 6308 | const mod = self.bin_file.comp.module.?; | |
| 6309 | return self.air.typeOf(inst, &mod.intern_pool); | |
| 6365 | return self.air.typeOf(inst, &self.pt.zcu.intern_pool); | |
| 6310 | 6366 | } |
| 6311 | 6367 | |
| 6312 | 6368 | fn typeOfIndex(self: *Self, inst: Air.Inst.Index) Type { |
| 6313 | const mod = self.bin_file.comp.module.?; | |
| 6314 | return self.air.typeOfIndex(inst, &mod.intern_pool); | |
| 6369 | return self.air.typeOfIndex(inst, &self.pt.zcu.intern_pool); | |
| 6315 | 6370 | } |
src/arch/arm/Emit.zig+2-4| ... | ... | @@ -9,10 +9,8 @@ const Mir = @import("Mir.zig"); |
| 9 | 9 | const bits = @import("bits.zig"); |
| 10 | 10 | const link = @import("../../link.zig"); |
| 11 | 11 | const Zcu = @import("../../Zcu.zig"); |
| 12 | /// Deprecated. | |
| 13 | const Module = Zcu; | |
| 14 | 12 | const Type = @import("../../Type.zig"); |
| 15 | const ErrorMsg = Module.ErrorMsg; | |
| 13 | const ErrorMsg = Zcu.ErrorMsg; | |
| 16 | 14 | const Target = std.Target; |
| 17 | 15 | const assert = std.debug.assert; |
| 18 | 16 | const Instruction = bits.Instruction; |
| ... | ... | @@ -26,7 +24,7 @@ bin_file: *link.File, |
| 26 | 24 | debug_output: DebugInfoOutput, |
| 27 | 25 | target: *const std.Target, |
| 28 | 26 | err_msg: ?*ErrorMsg = null, |
| 29 | src_loc: Module.LazySrcLoc, | |
| 27 | src_loc: Zcu.LazySrcLoc, | |
| 30 | 28 | code: *std.ArrayList(u8), |
| 31 | 29 | |
| 32 | 30 | prev_di_line: u32, |
src/arch/arm/abi.zig+30-32| ... | ... | @@ -5,8 +5,6 @@ const Register = bits.Register; |
| 5 | 5 | const RegisterManagerFn = @import("../../register_manager.zig").RegisterManager; |
| 6 | 6 | const Type = @import("../../Type.zig"); |
| 7 | 7 | const Zcu = @import("../../Zcu.zig"); |
| 8 | /// Deprecated. | |
| 9 | const Module = Zcu; | |
| 10 | 8 | |
| 11 | 9 | pub const Class = union(enum) { |
| 12 | 10 | memory, |
| ... | ... | @@ -26,29 +24,29 @@ pub const Class = union(enum) { |
| 26 | 24 | |
| 27 | 25 | pub const Context = enum { ret, arg }; |
| 28 | 26 | |
| 29 | pub fn classifyType(ty: Type, mod: *Module, ctx: Context) Class { | |
| 30 | assert(ty.hasRuntimeBitsIgnoreComptime(mod)); | |
| 27 | pub fn classifyType(ty: Type, pt: Zcu.PerThread, ctx: Context) Class { | |
| 28 | assert(ty.hasRuntimeBitsIgnoreComptime(pt)); | |
| 31 | 29 | |
| 32 | 30 | var maybe_float_bits: ?u16 = null; |
| 33 | 31 | const max_byval_size = 512; |
| 34 | const ip = &mod.intern_pool; | |
| 35 | switch (ty.zigTypeTag(mod)) { | |
| 32 | const ip = &pt.zcu.intern_pool; | |
| 33 | switch (ty.zigTypeTag(pt.zcu)) { | |
| 36 | 34 | .Struct => { |
| 37 | const bit_size = ty.bitSize(mod); | |
| 38 | if (ty.containerLayout(mod) == .@"packed") { | |
| 35 | const bit_size = ty.bitSize(pt); | |
| 36 | if (ty.containerLayout(pt.zcu) == .@"packed") { | |
| 39 | 37 | if (bit_size > 64) return .memory; |
| 40 | 38 | return .byval; |
| 41 | 39 | } |
| 42 | 40 | if (bit_size > max_byval_size) return .memory; |
| 43 | const float_count = countFloats(ty, mod, &maybe_float_bits); | |
| 41 | const float_count = countFloats(ty, pt.zcu, &maybe_float_bits); | |
| 44 | 42 | if (float_count <= byval_float_count) return .byval; |
| 45 | 43 | |
| 46 | const fields = ty.structFieldCount(mod); | |
| 44 | const fields = ty.structFieldCount(pt.zcu); | |
| 47 | 45 | var i: u32 = 0; |
| 48 | 46 | while (i < fields) : (i += 1) { |
| 49 | const field_ty = ty.structFieldType(i, mod); | |
| 50 | const field_alignment = ty.structFieldAlign(i, mod); | |
| 51 | const field_size = field_ty.bitSize(mod); | |
| 47 | const field_ty = ty.structFieldType(i, pt.zcu); | |
| 48 | const field_alignment = ty.structFieldAlign(i, pt); | |
| 49 | const field_size = field_ty.bitSize(pt); | |
| 52 | 50 | if (field_size > 32 or field_alignment.compare(.gt, .@"32")) { |
| 53 | 51 | return Class.arrSize(bit_size, 64); |
| 54 | 52 | } |
| ... | ... | @@ -56,19 +54,19 @@ pub fn classifyType(ty: Type, mod: *Module, ctx: Context) Class { |
| 56 | 54 | return Class.arrSize(bit_size, 32); |
| 57 | 55 | }, |
| 58 | 56 | .Union => { |
| 59 | const bit_size = ty.bitSize(mod); | |
| 60 | const union_obj = mod.typeToUnion(ty).?; | |
| 57 | const bit_size = ty.bitSize(pt); | |
| 58 | const union_obj = pt.zcu.typeToUnion(ty).?; | |
| 61 | 59 | if (union_obj.getLayout(ip) == .@"packed") { |
| 62 | 60 | if (bit_size > 64) return .memory; |
| 63 | 61 | return .byval; |
| 64 | 62 | } |
| 65 | 63 | if (bit_size > max_byval_size) return .memory; |
| 66 | const float_count = countFloats(ty, mod, &maybe_float_bits); | |
| 64 | const float_count = countFloats(ty, pt.zcu, &maybe_float_bits); | |
| 67 | 65 | if (float_count <= byval_float_count) return .byval; |
| 68 | 66 | |
| 69 | 67 | for (union_obj.field_types.get(ip), 0..) |field_ty, field_index| { |
| 70 | if (Type.fromInterned(field_ty).bitSize(mod) > 32 or | |
| 71 | mod.unionFieldNormalAlignment(union_obj, @intCast(field_index)).compare(.gt, .@"32")) | |
| 68 | if (Type.fromInterned(field_ty).bitSize(pt) > 32 or | |
| 69 | pt.unionFieldNormalAlignment(union_obj, @intCast(field_index)).compare(.gt, .@"32")) | |
| 72 | 70 | { |
| 73 | 71 | return Class.arrSize(bit_size, 64); |
| 74 | 72 | } |
| ... | ... | @@ -79,28 +77,28 @@ pub fn classifyType(ty: Type, mod: *Module, ctx: Context) Class { |
| 79 | 77 | .Int => { |
| 80 | 78 | // TODO this is incorrect for _BitInt(128) but implementing |
| 81 | 79 | // this correctly makes implementing compiler-rt impossible. |
| 82 | // const bit_size = ty.bitSize(mod); | |
| 80 | // const bit_size = ty.bitSize(pt); | |
| 83 | 81 | // if (bit_size > 64) return .memory; |
| 84 | 82 | return .byval; |
| 85 | 83 | }, |
| 86 | 84 | .Enum, .ErrorSet => { |
| 87 | const bit_size = ty.bitSize(mod); | |
| 85 | const bit_size = ty.bitSize(pt); | |
| 88 | 86 | if (bit_size > 64) return .memory; |
| 89 | 87 | return .byval; |
| 90 | 88 | }, |
| 91 | 89 | .Vector => { |
| 92 | const bit_size = ty.bitSize(mod); | |
| 90 | const bit_size = ty.bitSize(pt); | |
| 93 | 91 | // TODO is this controlled by a cpu feature? |
| 94 | 92 | if (ctx == .ret and bit_size > 128) return .memory; |
| 95 | 93 | if (bit_size > 512) return .memory; |
| 96 | 94 | return .byval; |
| 97 | 95 | }, |
| 98 | 96 | .Optional => { |
| 99 | assert(ty.isPtrLikeOptional(mod)); | |
| 97 | assert(ty.isPtrLikeOptional(pt.zcu)); | |
| 100 | 98 | return .byval; |
| 101 | 99 | }, |
| 102 | 100 | .Pointer => { |
| 103 | assert(!ty.isSlice(mod)); | |
| 101 | assert(!ty.isSlice(pt.zcu)); | |
| 104 | 102 | return .byval; |
| 105 | 103 | }, |
| 106 | 104 | .ErrorUnion, |
| ... | ... | @@ -122,16 +120,16 @@ pub fn classifyType(ty: Type, mod: *Module, ctx: Context) Class { |
| 122 | 120 | } |
| 123 | 121 | |
| 124 | 122 | const byval_float_count = 4; |
| 125 | fn countFloats(ty: Type, mod: *Module, maybe_float_bits: *?u16) u32 { | |
| 126 | const ip = &mod.intern_pool; | |
| 127 | const target = mod.getTarget(); | |
| 123 | fn countFloats(ty: Type, zcu: *Zcu, maybe_float_bits: *?u16) u32 { | |
| 124 | const ip = &zcu.intern_pool; | |
| 125 | const target = zcu.getTarget(); | |
| 128 | 126 | const invalid = std.math.maxInt(u32); |
| 129 | switch (ty.zigTypeTag(mod)) { | |
| 127 | switch (ty.zigTypeTag(zcu)) { | |
| 130 | 128 | .Union => { |
| 131 | const union_obj = mod.typeToUnion(ty).?; | |
| 129 | const union_obj = zcu.typeToUnion(ty).?; | |
| 132 | 130 | var max_count: u32 = 0; |
| 133 | 131 | for (union_obj.field_types.get(ip)) |field_ty| { |
| 134 | const field_count = countFloats(Type.fromInterned(field_ty), mod, maybe_float_bits); | |
| 132 | const field_count = countFloats(Type.fromInterned(field_ty), zcu, maybe_float_bits); | |
| 135 | 133 | if (field_count == invalid) return invalid; |
| 136 | 134 | if (field_count > max_count) max_count = field_count; |
| 137 | 135 | if (max_count > byval_float_count) return invalid; |
| ... | ... | @@ -139,12 +137,12 @@ fn countFloats(ty: Type, mod: *Module, maybe_float_bits: *?u16) u32 { |
| 139 | 137 | return max_count; |
| 140 | 138 | }, |
| 141 | 139 | .Struct => { |
| 142 | const fields_len = ty.structFieldCount(mod); | |
| 140 | const fields_len = ty.structFieldCount(zcu); | |
| 143 | 141 | var count: u32 = 0; |
| 144 | 142 | var i: u32 = 0; |
| 145 | 143 | while (i < fields_len) : (i += 1) { |
| 146 | const field_ty = ty.structFieldType(i, mod); | |
| 147 | const field_count = countFloats(field_ty, mod, maybe_float_bits); | |
| 144 | const field_ty = ty.structFieldType(i, zcu); | |
| 145 | const field_count = countFloats(field_ty, zcu, maybe_float_bits); | |
| 148 | 146 | if (field_count == invalid) return invalid; |
| 149 | 147 | count += field_count; |
| 150 | 148 | if (count > byval_float_count) return invalid; |
src/arch/riscv64/CodeGen.zig+267-216| ... | ... | @@ -46,6 +46,7 @@ const RegisterLock = RegisterManager.RegisterLock; |
| 46 | 46 | const InnerError = CodeGenError || error{OutOfRegisters}; |
| 47 | 47 | |
| 48 | 48 | gpa: Allocator, |
| 49 | pt: Zcu.PerThread, | |
| 49 | 50 | air: Air, |
| 50 | 51 | mod: *Package.Module, |
| 51 | 52 | liveness: Liveness, |
| ... | ... | @@ -541,14 +542,14 @@ const FrameAlloc = struct { |
| 541 | 542 | .ref_count = 0, |
| 542 | 543 | }; |
| 543 | 544 | } |
| 544 | fn initType(ty: Type, zcu: *Zcu) FrameAlloc { | |
| 545 | fn initType(ty: Type, pt: Zcu.PerThread) FrameAlloc { | |
| 545 | 546 | return init(.{ |
| 546 | .size = ty.abiSize(zcu), | |
| 547 | .alignment = ty.abiAlignment(zcu), | |
| 547 | .size = ty.abiSize(pt), | |
| 548 | .alignment = ty.abiAlignment(pt), | |
| 548 | 549 | }); |
| 549 | 550 | } |
| 550 | fn initSpill(ty: Type, zcu: *Zcu) FrameAlloc { | |
| 551 | const abi_size = ty.abiSize(zcu); | |
| 551 | fn initSpill(ty: Type, pt: Zcu.PerThread) FrameAlloc { | |
| 552 | const abi_size = ty.abiSize(pt); | |
| 552 | 553 | const spill_size = if (abi_size < 8) |
| 553 | 554 | math.ceilPowerOfTwoAssert(u64, abi_size) |
| 554 | 555 | else |
| ... | ... | @@ -556,7 +557,7 @@ const FrameAlloc = struct { |
| 556 | 557 | return init(.{ |
| 557 | 558 | .size = spill_size, |
| 558 | 559 | .pad = @intCast(spill_size - abi_size), |
| 559 | .alignment = ty.abiAlignment(zcu).maxStrict( | |
| 560 | .alignment = ty.abiAlignment(pt).maxStrict( | |
| 560 | 561 | Alignment.fromNonzeroByteUnits(@min(spill_size, 8)), |
| 561 | 562 | ), |
| 562 | 563 | }); |
| ... | ... | @@ -696,6 +697,7 @@ const CallView = enum(u1) { |
| 696 | 697 | |
| 697 | 698 | pub fn generate( |
| 698 | 699 | bin_file: *link.File, |
| 700 | pt: Zcu.PerThread, | |
| 699 | 701 | src_loc: Zcu.LazySrcLoc, |
| 700 | 702 | func_index: InternPool.Index, |
| 701 | 703 | air: Air, |
| ... | ... | @@ -703,9 +705,9 @@ pub fn generate( |
| 703 | 705 | code: *std.ArrayList(u8), |
| 704 | 706 | debug_output: DebugInfoOutput, |
| 705 | 707 | ) CodeGenError!Result { |
| 706 | const comp = bin_file.comp; | |
| 707 | const gpa = comp.gpa; | |
| 708 | const zcu = comp.module.?; | |
| 708 | const zcu = pt.zcu; | |
| 709 | const comp = zcu.comp; | |
| 710 | const gpa = zcu.gpa; | |
| 709 | 711 | const ip = &zcu.intern_pool; |
| 710 | 712 | const func = zcu.funcInfo(func_index); |
| 711 | 713 | const fn_owner_decl = zcu.declPtr(func.owner_decl); |
| ... | ... | @@ -726,6 +728,7 @@ pub fn generate( |
| 726 | 728 | var function = Func{ |
| 727 | 729 | .gpa = gpa, |
| 728 | 730 | .air = air, |
| 731 | .pt = pt, | |
| 729 | 732 | .mod = mod, |
| 730 | 733 | .liveness = liveness, |
| 731 | 734 | .target = target, |
| ... | ... | @@ -787,11 +790,11 @@ pub fn generate( |
| 787 | 790 | function.args = call_info.args; |
| 788 | 791 | function.ret_mcv = call_info.return_value; |
| 789 | 792 | function.frame_allocs.set(@intFromEnum(FrameIndex.ret_addr), FrameAlloc.init(.{ |
| 790 | .size = Type.usize.abiSize(zcu), | |
| 791 | .alignment = Type.usize.abiAlignment(zcu).min(call_info.stack_align), | |
| 793 | .size = Type.usize.abiSize(pt), | |
| 794 | .alignment = Type.usize.abiAlignment(pt).min(call_info.stack_align), | |
| 792 | 795 | })); |
| 793 | 796 | function.frame_allocs.set(@intFromEnum(FrameIndex.base_ptr), FrameAlloc.init(.{ |
| 794 | .size = Type.usize.abiSize(zcu), | |
| 797 | .size = Type.usize.abiSize(pt), | |
| 795 | 798 | .alignment = Alignment.min( |
| 796 | 799 | call_info.stack_align, |
| 797 | 800 | Alignment.fromNonzeroByteUnits(function.target.stackAlignment()), |
| ... | ... | @@ -803,7 +806,7 @@ pub fn generate( |
| 803 | 806 | })); |
| 804 | 807 | function.frame_allocs.set(@intFromEnum(FrameIndex.spill_frame), FrameAlloc.init(.{ |
| 805 | 808 | .size = 0, |
| 806 | .alignment = Type.usize.abiAlignment(zcu), | |
| 809 | .alignment = Type.usize.abiAlignment(pt), | |
| 807 | 810 | })); |
| 808 | 811 | |
| 809 | 812 | function.gen() catch |err| switch (err) { |
| ... | ... | @@ -821,9 +824,10 @@ pub fn generate( |
| 821 | 824 | }; |
| 822 | 825 | defer mir.deinit(gpa); |
| 823 | 826 | |
| 824 | var emit = Emit{ | |
| 827 | var emit: Emit = .{ | |
| 828 | .bin_file = bin_file, | |
| 825 | 829 | .lower = .{ |
| 826 | .bin_file = bin_file, | |
| 830 | .pt = pt, | |
| 827 | 831 | .allocator = gpa, |
| 828 | 832 | .mir = mir, |
| 829 | 833 | .cc = fn_info.cc, |
| ... | ... | @@ -875,10 +879,10 @@ fn formatWipMir( |
| 875 | 879 | _: std.fmt.FormatOptions, |
| 876 | 880 | writer: anytype, |
| 877 | 881 | ) @TypeOf(writer).Error!void { |
| 878 | const comp = data.func.bin_file.comp; | |
| 879 | const mod = comp.root_mod; | |
| 880 | var lower = Lower{ | |
| 881 | .bin_file = data.func.bin_file, | |
| 882 | const pt = data.func.pt; | |
| 883 | const comp = pt.zcu.comp; | |
| 884 | var lower: Lower = .{ | |
| 885 | .pt = pt, | |
| 882 | 886 | .allocator = data.func.gpa, |
| 883 | 887 | .mir = .{ |
| 884 | 888 | .instructions = data.func.mir_instructions.slice(), |
| ... | ... | @@ -889,7 +893,7 @@ fn formatWipMir( |
| 889 | 893 | .src_loc = data.func.src_loc, |
| 890 | 894 | .output_mode = comp.config.output_mode, |
| 891 | 895 | .link_mode = comp.config.link_mode, |
| 892 | .pic = mod.pic, | |
| 896 | .pic = comp.root_mod.pic, | |
| 893 | 897 | }; |
| 894 | 898 | var first = true; |
| 895 | 899 | for ((lower.lowerMir(data.inst) catch |err| switch (err) { |
| ... | ... | @@ -933,7 +937,7 @@ fn formatDecl( |
| 933 | 937 | } |
| 934 | 938 | fn fmtDecl(func: *Func, decl_index: InternPool.DeclIndex) std.fmt.Formatter(formatDecl) { |
| 935 | 939 | return .{ .data = .{ |
| 936 | .mod = func.bin_file.comp.module.?, | |
| 940 | .mod = func.pt.zcu, | |
| 937 | 941 | .decl_index = decl_index, |
| 938 | 942 | } }; |
| 939 | 943 | } |
| ... | ... | @@ -950,7 +954,7 @@ fn formatAir( |
| 950 | 954 | ) @TypeOf(writer).Error!void { |
| 951 | 955 | @import("../../print_air.zig").dumpInst( |
| 952 | 956 | data.inst, |
| 953 | data.func.bin_file.comp.module.?, | |
| 957 | data.func.pt, | |
| 954 | 958 | data.func.air, |
| 955 | 959 | data.func.liveness, |
| 956 | 960 | ); |
| ... | ... | @@ -1044,8 +1048,9 @@ const required_features = [_]Target.riscv.Feature{ |
| 1044 | 1048 | }; |
| 1045 | 1049 | |
| 1046 | 1050 | fn gen(func: *Func) !void { |
| 1047 | const mod = func.bin_file.comp.module.?; | |
| 1048 | const fn_info = mod.typeToFunc(func.fn_type).?; | |
| 1051 | const pt = func.pt; | |
| 1052 | const zcu = pt.zcu; | |
| 1053 | const fn_info = zcu.typeToFunc(func.fn_type).?; | |
| 1049 | 1054 | |
| 1050 | 1055 | inline for (required_features) |feature| { |
| 1051 | 1056 | if (!func.hasFeature(feature)) { |
| ... | ... | @@ -1071,7 +1076,7 @@ fn gen(func: *Func) !void { |
| 1071 | 1076 | // The address where to store the return value for the caller is in a |
| 1072 | 1077 | // register which the callee is free to clobber. Therefore, we purposely |
| 1073 | 1078 | // spill it to stack immediately. |
| 1074 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(Type.usize, mod)); | |
| 1079 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(Type.usize, pt)); | |
| 1075 | 1080 | try func.genSetMem( |
| 1076 | 1081 | .{ .frame = frame_index }, |
| 1077 | 1082 | 0, |
| ... | ... | @@ -1205,7 +1210,8 @@ fn gen(func: *Func) !void { |
| 1205 | 1210 | } |
| 1206 | 1211 | |
| 1207 | 1212 | fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void { |
| 1208 | const zcu = func.bin_file.comp.module.?; | |
| 1213 | const pt = func.pt; | |
| 1214 | const zcu = pt.zcu; | |
| 1209 | 1215 | const ip = &zcu.intern_pool; |
| 1210 | 1216 | const air_tags = func.air.instructions.items(.tag); |
| 1211 | 1217 | |
| ... | ... | @@ -1672,44 +1678,46 @@ fn ensureProcessDeathCapacity(func: *Func, additional_count: usize) !void { |
| 1672 | 1678 | } |
| 1673 | 1679 | |
| 1674 | 1680 | fn memSize(func: *Func, ty: Type) Memory.Size { |
| 1675 | const mod = func.bin_file.comp.module.?; | |
| 1676 | return switch (ty.zigTypeTag(mod)) { | |
| 1681 | const pt = func.pt; | |
| 1682 | const zcu = pt.zcu; | |
| 1683 | return switch (ty.zigTypeTag(zcu)) { | |
| 1677 | 1684 | .Float => Memory.Size.fromBitSize(ty.floatBits(func.target.*)), |
| 1678 | else => Memory.Size.fromByteSize(ty.abiSize(mod)), | |
| 1685 | else => Memory.Size.fromByteSize(ty.abiSize(pt)), | |
| 1679 | 1686 | }; |
| 1680 | 1687 | } |
| 1681 | 1688 | |
| 1682 | 1689 | fn splitType(func: *Func, ty: Type) ![2]Type { |
| 1683 | const zcu = func.bin_file.comp.module.?; | |
| 1684 | const classes = mem.sliceTo(&abi.classifySystem(ty, zcu), .none); | |
| 1690 | const pt = func.pt; | |
| 1691 | const classes = mem.sliceTo(&abi.classifySystem(ty, pt), .none); | |
| 1685 | 1692 | var parts: [2]Type = undefined; |
| 1686 | 1693 | if (classes.len == 2) for (&parts, classes, 0..) |*part, class, part_i| { |
| 1687 | 1694 | part.* = switch (class) { |
| 1688 | 1695 | .integer => switch (part_i) { |
| 1689 | 1696 | 0 => Type.u64, |
| 1690 | 1697 | 1 => part: { |
| 1691 | const elem_size = ty.abiAlignment(zcu).minStrict(.@"8").toByteUnits().?; | |
| 1692 | const elem_ty = try zcu.intType(.unsigned, @intCast(elem_size * 8)); | |
| 1693 | break :part switch (@divExact(ty.abiSize(zcu) - 8, elem_size)) { | |
| 1698 | const elem_size = ty.abiAlignment(pt).minStrict(.@"8").toByteUnits().?; | |
| 1699 | const elem_ty = try pt.intType(.unsigned, @intCast(elem_size * 8)); | |
| 1700 | break :part switch (@divExact(ty.abiSize(pt) - 8, elem_size)) { | |
| 1694 | 1701 | 1 => elem_ty, |
| 1695 | else => |len| try zcu.arrayType(.{ .len = len, .child = elem_ty.toIntern() }), | |
| 1702 | else => |len| try pt.arrayType(.{ .len = len, .child = elem_ty.toIntern() }), | |
| 1696 | 1703 | }; |
| 1697 | 1704 | }, |
| 1698 | 1705 | else => unreachable, |
| 1699 | 1706 | }, |
| 1700 | 1707 | else => return func.fail("TODO: splitType class {}", .{class}), |
| 1701 | 1708 | }; |
| 1702 | } else if (parts[0].abiSize(zcu) + parts[1].abiSize(zcu) == ty.abiSize(zcu)) return parts; | |
| 1703 | return func.fail("TODO implement splitType for {}", .{ty.fmt(zcu)}); | |
| 1709 | } else if (parts[0].abiSize(pt) + parts[1].abiSize(pt) == ty.abiSize(pt)) return parts; | |
| 1710 | return func.fail("TODO implement splitType for {}", .{ty.fmt(pt)}); | |
| 1704 | 1711 | } |
| 1705 | 1712 | |
| 1706 | 1713 | /// Truncates the value in the register in place. |
| 1707 | 1714 | /// Clobbers any remaining bits. |
| 1708 | 1715 | fn truncateRegister(func: *Func, ty: Type, reg: Register) !void { |
| 1709 | const mod = func.bin_file.comp.module.?; | |
| 1710 | const int_info = if (ty.isAbiInt(mod)) ty.intInfo(mod) else std.builtin.Type.Int{ | |
| 1716 | const pt = func.pt; | |
| 1717 | const zcu = pt.zcu; | |
| 1718 | const int_info = if (ty.isAbiInt(zcu)) ty.intInfo(zcu) else std.builtin.Type.Int{ | |
| 1711 | 1719 | .signedness = .unsigned, |
| 1712 | .bits = @intCast(ty.bitSize(mod)), | |
| 1720 | .bits = @intCast(ty.bitSize(pt)), | |
| 1713 | 1721 | }; |
| 1714 | 1722 | const shift = math.cast(u6, 64 - int_info.bits % 64) orelse return; |
| 1715 | 1723 | switch (int_info.signedness) { |
| ... | ... | @@ -1780,7 +1788,8 @@ fn truncateRegister(func: *Func, ty: Type, reg: Register) !void { |
| 1780 | 1788 | } |
| 1781 | 1789 | |
| 1782 | 1790 | fn symbolIndex(func: *Func) !u32 { |
| 1783 | const zcu = func.bin_file.comp.module.?; | |
| 1791 | const pt = func.pt; | |
| 1792 | const zcu = pt.zcu; | |
| 1784 | 1793 | const decl_index = zcu.funcOwnerDeclIndex(func.func_index); |
| 1785 | 1794 | return switch (func.bin_file.tag) { |
| 1786 | 1795 | .elf => blk: { |
| ... | ... | @@ -1817,19 +1826,21 @@ fn allocFrameIndex(func: *Func, alloc: FrameAlloc) !FrameIndex { |
| 1817 | 1826 | |
| 1818 | 1827 | /// Use a pointer instruction as the basis for allocating stack memory. |
| 1819 | 1828 | fn allocMemPtr(func: *Func, inst: Air.Inst.Index) !FrameIndex { |
| 1820 | const zcu = func.bin_file.comp.module.?; | |
| 1829 | const pt = func.pt; | |
| 1830 | const zcu = pt.zcu; | |
| 1821 | 1831 | const ptr_ty = func.typeOfIndex(inst); |
| 1822 | 1832 | const val_ty = ptr_ty.childType(zcu); |
| 1823 | 1833 | return func.allocFrameIndex(FrameAlloc.init(.{ |
| 1824 | .size = math.cast(u32, val_ty.abiSize(zcu)) orelse { | |
| 1825 | return func.fail("type '{}' too big to fit into stack frame", .{val_ty.fmt(zcu)}); | |
| 1834 | .size = math.cast(u32, val_ty.abiSize(pt)) orelse { | |
| 1835 | return func.fail("type '{}' too big to fit into stack frame", .{val_ty.fmt(pt)}); | |
| 1826 | 1836 | }, |
| 1827 | .alignment = ptr_ty.ptrAlignment(zcu).max(.@"1"), | |
| 1837 | .alignment = ptr_ty.ptrAlignment(pt).max(.@"1"), | |
| 1828 | 1838 | })); |
| 1829 | 1839 | } |
| 1830 | 1840 | |
| 1831 | 1841 | fn typeRegClass(func: *Func, ty: Type) abi.RegisterClass { |
| 1832 | const zcu = func.bin_file.comp.module.?; | |
| 1842 | const pt = func.pt; | |
| 1843 | const zcu = pt.zcu; | |
| 1833 | 1844 | return switch (ty.zigTypeTag(zcu)) { |
| 1834 | 1845 | .Float => .float, |
| 1835 | 1846 | .Vector => @panic("TODO: typeRegClass for Vectors"), |
| ... | ... | @@ -1838,7 +1849,8 @@ fn typeRegClass(func: *Func, ty: Type) abi.RegisterClass { |
| 1838 | 1849 | } |
| 1839 | 1850 | |
| 1840 | 1851 | fn regGeneralClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet { |
| 1841 | const zcu = func.bin_file.comp.module.?; | |
| 1852 | const pt = func.pt; | |
| 1853 | const zcu = pt.zcu; | |
| 1842 | 1854 | return switch (ty.zigTypeTag(zcu)) { |
| 1843 | 1855 | .Float => abi.Registers.Float.general_purpose, |
| 1844 | 1856 | .Vector => @panic("TODO: regGeneralClassForType for Vectors"), |
| ... | ... | @@ -1847,7 +1859,8 @@ fn regGeneralClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet |
| 1847 | 1859 | } |
| 1848 | 1860 | |
| 1849 | 1861 | fn regTempClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet { |
| 1850 | const zcu = func.bin_file.comp.module.?; | |
| 1862 | const pt = func.pt; | |
| 1863 | const zcu = pt.zcu; | |
| 1851 | 1864 | return switch (ty.zigTypeTag(zcu)) { |
| 1852 | 1865 | .Float => abi.Registers.Float.temporary, |
| 1853 | 1866 | .Vector => @panic("TODO: regTempClassForType for Vectors"), |
| ... | ... | @@ -1856,13 +1869,13 @@ fn regTempClassForType(func: *Func, ty: Type) RegisterManager.RegisterBitSet { |
| 1856 | 1869 | } |
| 1857 | 1870 | |
| 1858 | 1871 | fn allocRegOrMem(func: *Func, elem_ty: Type, inst: ?Air.Inst.Index, reg_ok: bool) !MCValue { |
| 1859 | const zcu = func.bin_file.comp.module.?; | |
| 1872 | const pt = func.pt; | |
| 1860 | 1873 | |
| 1861 | const abi_size = math.cast(u32, elem_ty.abiSize(zcu)) orelse { | |
| 1862 | return func.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(zcu)}); | |
| 1874 | const abi_size = math.cast(u32, elem_ty.abiSize(pt)) orelse { | |
| 1875 | return func.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(pt)}); | |
| 1863 | 1876 | }; |
| 1864 | 1877 | |
| 1865 | const min_size: u32 = switch (elem_ty.zigTypeTag(zcu)) { | |
| 1878 | const min_size: u32 = switch (elem_ty.zigTypeTag(pt.zcu)) { | |
| 1866 | 1879 | .Float => 4, |
| 1867 | 1880 | .Vector => @panic("allocRegOrMem Vector"), |
| 1868 | 1881 | else => 8, |
| ... | ... | @@ -1874,7 +1887,7 @@ fn allocRegOrMem(func: *Func, elem_ty: Type, inst: ?Air.Inst.Index, reg_ok: bool |
| 1874 | 1887 | } |
| 1875 | 1888 | } |
| 1876 | 1889 | |
| 1877 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(elem_ty, zcu)); | |
| 1890 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(elem_ty, pt)); | |
| 1878 | 1891 | return .{ .load_frame = .{ .index = frame_index } }; |
| 1879 | 1892 | } |
| 1880 | 1893 | |
| ... | ... | @@ -1955,7 +1968,7 @@ pub fn spillInstruction(func: *Func, reg: Register, inst: Air.Inst.Index) !void |
| 1955 | 1968 | /// allocated. A second call to `copyToTmpRegister` may return the same register. |
| 1956 | 1969 | /// This can have a side effect of spilling instructions to the stack to free up a register. |
| 1957 | 1970 | fn copyToTmpRegister(func: *Func, ty: Type, mcv: MCValue) !Register { |
| 1958 | log.debug("copyToTmpRegister ty: {}", .{ty.fmt(func.bin_file.comp.module.?)}); | |
| 1971 | log.debug("copyToTmpRegister ty: {}", .{ty.fmt(func.pt)}); | |
| 1959 | 1972 | const reg = try func.register_manager.allocReg(null, func.regTempClassForType(ty)); |
| 1960 | 1973 | try func.genSetReg(ty, reg, mcv); |
| 1961 | 1974 | return reg; |
| ... | ... | @@ -2004,7 +2017,8 @@ fn airFpext(func: *Func, inst: Air.Inst.Index) !void { |
| 2004 | 2017 | } |
| 2005 | 2018 | |
| 2006 | 2019 | fn airIntCast(func: *Func, inst: Air.Inst.Index) !void { |
| 2007 | const zcu = func.bin_file.comp.module.?; | |
| 2020 | const pt = func.pt; | |
| 2021 | const zcu = pt.zcu; | |
| 2008 | 2022 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2009 | 2023 | const src_ty = func.typeOf(ty_op.operand); |
| 2010 | 2024 | const dst_ty = func.typeOfIndex(inst); |
| ... | ... | @@ -2040,7 +2054,7 @@ fn airIntCast(func: *Func, inst: Air.Inst.Index) !void { |
| 2040 | 2054 | |
| 2041 | 2055 | break :result dst_mcv; |
| 2042 | 2056 | } orelse return func.fail("TODO: implement airIntCast from {} to {}", .{ |
| 2043 | src_ty.fmt(zcu), dst_ty.fmt(zcu), | |
| 2057 | src_ty.fmt(pt), dst_ty.fmt(pt), | |
| 2044 | 2058 | }); |
| 2045 | 2059 | |
| 2046 | 2060 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| ... | ... | @@ -2067,7 +2081,8 @@ fn airIntFromBool(func: *Func, inst: Air.Inst.Index) !void { |
| 2067 | 2081 | fn airNot(func: *Func, inst: Air.Inst.Index) !void { |
| 2068 | 2082 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2069 | 2083 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 2070 | const zcu = func.bin_file.comp.module.?; | |
| 2084 | const pt = func.pt; | |
| 2085 | const zcu = pt.zcu; | |
| 2071 | 2086 | |
| 2072 | 2087 | const operand = try func.resolveInst(ty_op.operand); |
| 2073 | 2088 | const ty = func.typeOf(ty_op.operand); |
| ... | ... | @@ -2106,12 +2121,12 @@ fn airNot(func: *Func, inst: Air.Inst.Index) !void { |
| 2106 | 2121 | } |
| 2107 | 2122 | |
| 2108 | 2123 | fn airSlice(func: *Func, inst: Air.Inst.Index) !void { |
| 2109 | const zcu = func.bin_file.comp.module.?; | |
| 2124 | const pt = func.pt; | |
| 2110 | 2125 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2111 | 2126 | const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2112 | 2127 | |
| 2113 | 2128 | const slice_ty = func.typeOfIndex(inst); |
| 2114 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(slice_ty, zcu)); | |
| 2129 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(slice_ty, pt)); | |
| 2115 | 2130 | |
| 2116 | 2131 | const ptr_ty = func.typeOf(bin_op.lhs); |
| 2117 | 2132 | try func.genSetMem(.{ .frame = frame_index }, 0, ptr_ty, .{ .air_ref = bin_op.lhs }); |
| ... | ... | @@ -2119,7 +2134,7 @@ fn airSlice(func: *Func, inst: Air.Inst.Index) !void { |
| 2119 | 2134 | const len_ty = func.typeOf(bin_op.rhs); |
| 2120 | 2135 | try func.genSetMem( |
| 2121 | 2136 | .{ .frame = frame_index }, |
| 2122 | @intCast(ptr_ty.abiSize(zcu)), | |
| 2137 | @intCast(ptr_ty.abiSize(pt)), | |
| 2123 | 2138 | len_ty, |
| 2124 | 2139 | .{ .air_ref = bin_op.rhs }, |
| 2125 | 2140 | ); |
| ... | ... | @@ -2129,14 +2144,15 @@ fn airSlice(func: *Func, inst: Air.Inst.Index) !void { |
| 2129 | 2144 | } |
| 2130 | 2145 | |
| 2131 | 2146 | fn airBinOp(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 2132 | const zcu = func.bin_file.comp.module.?; | |
| 2147 | const pt = func.pt; | |
| 2148 | const zcu = pt.zcu; | |
| 2133 | 2149 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2134 | 2150 | const dst_mcv = try func.binOp(inst, tag, bin_op.lhs, bin_op.rhs); |
| 2135 | 2151 | |
| 2136 | 2152 | const dst_ty = func.typeOfIndex(inst); |
| 2137 | 2153 | if (dst_ty.isAbiInt(zcu)) { |
| 2138 | const abi_size: u32 = @intCast(dst_ty.abiSize(zcu)); | |
| 2139 | const bit_size: u32 = @intCast(dst_ty.bitSize(zcu)); | |
| 2154 | const abi_size: u32 = @intCast(dst_ty.abiSize(pt)); | |
| 2155 | const bit_size: u32 = @intCast(dst_ty.bitSize(pt)); | |
| 2140 | 2156 | if (abi_size * 8 > bit_size) { |
| 2141 | 2157 | const dst_lock = switch (dst_mcv) { |
| 2142 | 2158 | .register => |dst_reg| func.register_manager.lockRegAssumeUnused(dst_reg), |
| ... | ... | @@ -2150,7 +2166,7 @@ fn airBinOp(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 2150 | 2166 | const tmp_reg, const tmp_lock = try func.allocReg(.int); |
| 2151 | 2167 | defer func.register_manager.unlockReg(tmp_lock); |
| 2152 | 2168 | |
| 2153 | const hi_ty = try zcu.intType(.unsigned, @intCast((dst_ty.bitSize(zcu) - 1) % 64 + 1)); | |
| 2169 | const hi_ty = try pt.intType(.unsigned, @intCast((dst_ty.bitSize(pt) - 1) % 64 + 1)); | |
| 2154 | 2170 | const hi_mcv = dst_mcv.address().offset(@intCast(bit_size / 64 * 8)).deref(); |
| 2155 | 2171 | try func.genSetReg(hi_ty, tmp_reg, hi_mcv); |
| 2156 | 2172 | try func.truncateRegister(dst_ty, tmp_reg); |
| ... | ... | @@ -2170,7 +2186,7 @@ fn binOp( |
| 2170 | 2186 | rhs_air: Air.Inst.Ref, |
| 2171 | 2187 | ) !MCValue { |
| 2172 | 2188 | _ = maybe_inst; |
| 2173 | const zcu = func.bin_file.comp.module.?; | |
| 2189 | const pt = func.pt; | |
| 2174 | 2190 | const lhs_ty = func.typeOf(lhs_air); |
| 2175 | 2191 | const rhs_ty = func.typeOf(rhs_air); |
| 2176 | 2192 | |
| ... | ... | @@ -2189,7 +2205,7 @@ fn binOp( |
| 2189 | 2205 | return func.fail("binOp libcall runtime-float ops", .{}); |
| 2190 | 2206 | } |
| 2191 | 2207 | |
| 2192 | if (lhs_ty.bitSize(zcu) > 64) return func.fail("TODO: binOp >= 64 bits", .{}); | |
| 2208 | if (lhs_ty.bitSize(pt) > 64) return func.fail("TODO: binOp >= 64 bits", .{}); | |
| 2193 | 2209 | |
| 2194 | 2210 | const lhs_mcv = try func.resolveInst(lhs_air); |
| 2195 | 2211 | const rhs_mcv = try func.resolveInst(rhs_air); |
| ... | ... | @@ -2237,8 +2253,9 @@ fn genBinOp( |
| 2237 | 2253 | rhs_ty: Type, |
| 2238 | 2254 | dst_reg: Register, |
| 2239 | 2255 | ) !void { |
| 2240 | const zcu = func.bin_file.comp.module.?; | |
| 2241 | const bit_size = lhs_ty.bitSize(zcu); | |
| 2256 | const pt = func.pt; | |
| 2257 | const zcu = pt.zcu; | |
| 2258 | const bit_size = lhs_ty.bitSize(pt); | |
| 2242 | 2259 | assert(bit_size <= 64); |
| 2243 | 2260 | |
| 2244 | 2261 | const is_unsigned = lhs_ty.isUnsignedInt(zcu); |
| ... | ... | @@ -2349,7 +2366,7 @@ fn genBinOp( |
| 2349 | 2366 | defer func.register_manager.unlockReg(tmp_lock); |
| 2350 | 2367 | |
| 2351 | 2368 | // RISC-V has no immediate mul, so we copy the size to a temporary register |
| 2352 | const elem_size = lhs_ty.elemType2(zcu).abiSize(zcu); | |
| 2369 | const elem_size = lhs_ty.elemType2(zcu).abiSize(pt); | |
| 2353 | 2370 | const elem_size_reg = try func.copyToTmpRegister(Type.usize, .{ .immediate = elem_size }); |
| 2354 | 2371 | |
| 2355 | 2372 | try func.genBinOp( |
| ... | ... | @@ -2613,7 +2630,8 @@ fn airPtrArithmetic(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void |
| 2613 | 2630 | } |
| 2614 | 2631 | |
| 2615 | 2632 | fn airAddWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2616 | const zcu = func.bin_file.comp.module.?; | |
| 2633 | const pt = func.pt; | |
| 2634 | const zcu = pt.zcu; | |
| 2617 | 2635 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2618 | 2636 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2619 | 2637 | |
| ... | ... | @@ -2632,7 +2650,7 @@ fn airAddWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2632 | 2650 | const add_result_reg_lock = func.register_manager.lockRegAssumeUnused(add_result_reg); |
| 2633 | 2651 | defer func.register_manager.unlockReg(add_result_reg_lock); |
| 2634 | 2652 | |
| 2635 | const shift_amount: u6 = @intCast(Type.usize.bitSize(zcu) - int_info.bits); | |
| 2653 | const shift_amount: u6 = @intCast(Type.usize.bitSize(pt) - int_info.bits); | |
| 2636 | 2654 | |
| 2637 | 2655 | const shift_reg, const shift_lock = try func.allocReg(.int); |
| 2638 | 2656 | defer func.register_manager.unlockReg(shift_lock); |
| ... | ... | @@ -2663,7 +2681,7 @@ fn airAddWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2663 | 2681 | |
| 2664 | 2682 | try func.genSetMem( |
| 2665 | 2683 | .{ .frame = offset.index }, |
| 2666 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(0, zcu))), | |
| 2684 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(0, pt))), | |
| 2667 | 2685 | lhs_ty, |
| 2668 | 2686 | add_result, |
| 2669 | 2687 | ); |
| ... | ... | @@ -2682,7 +2700,7 @@ fn airAddWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2682 | 2700 | |
| 2683 | 2701 | try func.genSetMem( |
| 2684 | 2702 | .{ .frame = offset.index }, |
| 2685 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, zcu))), | |
| 2703 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, pt))), | |
| 2686 | 2704 | Type.u1, |
| 2687 | 2705 | .{ .register = overflow_reg }, |
| 2688 | 2706 | ); |
| ... | ... | @@ -2697,7 +2715,8 @@ fn airAddWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2697 | 2715 | } |
| 2698 | 2716 | |
| 2699 | 2717 | fn airSubWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2700 | const zcu = func.bin_file.comp.module.?; | |
| 2718 | const pt = func.pt; | |
| 2719 | const zcu = pt.zcu; | |
| 2701 | 2720 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2702 | 2721 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2703 | 2722 | |
| ... | ... | @@ -2727,7 +2746,7 @@ fn airSubWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2727 | 2746 | |
| 2728 | 2747 | try func.genSetMem( |
| 2729 | 2748 | .{ .frame = offset.index }, |
| 2730 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(0, zcu))), | |
| 2749 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(0, pt))), | |
| 2731 | 2750 | lhs_ty, |
| 2732 | 2751 | .{ .register = dest_reg }, |
| 2733 | 2752 | ); |
| ... | ... | @@ -2757,7 +2776,7 @@ fn airSubWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2757 | 2776 | |
| 2758 | 2777 | try func.genSetMem( |
| 2759 | 2778 | .{ .frame = offset.index }, |
| 2760 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, zcu))), | |
| 2779 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, pt))), | |
| 2761 | 2780 | Type.u1, |
| 2762 | 2781 | .{ .register = overflow_reg }, |
| 2763 | 2782 | ); |
| ... | ... | @@ -2808,7 +2827,7 @@ fn airSubWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2808 | 2827 | |
| 2809 | 2828 | try func.genSetMem( |
| 2810 | 2829 | .{ .frame = offset.index }, |
| 2811 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, zcu))), | |
| 2830 | offset.off + @as(i32, @intCast(tuple_ty.structFieldOffset(1, pt))), | |
| 2812 | 2831 | Type.u1, |
| 2813 | 2832 | .{ .register = overflow_reg }, |
| 2814 | 2833 | ); |
| ... | ... | @@ -2825,7 +2844,8 @@ fn airSubWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2825 | 2844 | } |
| 2826 | 2845 | |
| 2827 | 2846 | fn airMulWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2828 | const zcu = func.bin_file.comp.module.?; | |
| 2847 | const pt = func.pt; | |
| 2848 | const zcu = pt.zcu; | |
| 2829 | 2849 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2830 | 2850 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2831 | 2851 | |
| ... | ... | @@ -2840,8 +2860,8 @@ fn airMulWithOverflow(func: *Func, inst: Air.Inst.Index) !void { |
| 2840 | 2860 | // genSetReg needs to support register_offset src_mcv for this to be true. |
| 2841 | 2861 | const result_mcv = try func.allocRegOrMem(tuple_ty, inst, false); |
| 2842 | 2862 | |
| 2843 | const result_off: i32 = @intCast(tuple_ty.structFieldOffset(0, zcu)); | |
| 2844 | const overflow_off: i32 = @intCast(tuple_ty.structFieldOffset(1, zcu)); | |
| 2863 | const result_off: i32 = @intCast(tuple_ty.structFieldOffset(0, pt)); | |
| 2864 | const overflow_off: i32 = @intCast(tuple_ty.structFieldOffset(1, pt)); | |
| 2845 | 2865 | |
| 2846 | 2866 | const dest_reg, const dest_lock = try func.allocReg(.int); |
| 2847 | 2867 | defer func.register_manager.unlockReg(dest_lock); |
| ... | ... | @@ -2957,11 +2977,11 @@ fn airShlSat(func: *Func, inst: Air.Inst.Index) !void { |
| 2957 | 2977 | } |
| 2958 | 2978 | |
| 2959 | 2979 | fn airOptionalPayload(func: *Func, inst: Air.Inst.Index) !void { |
| 2960 | const zcu = func.bin_file.comp.module.?; | |
| 2980 | const pt = func.pt; | |
| 2961 | 2981 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2962 | 2982 | const result: MCValue = result: { |
| 2963 | 2983 | const pl_ty = func.typeOfIndex(inst); |
| 2964 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result .none; | |
| 2984 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result .none; | |
| 2965 | 2985 | |
| 2966 | 2986 | const opt_mcv = try func.resolveInst(ty_op.operand); |
| 2967 | 2987 | if (func.reuseOperand(inst, ty_op.operand, 0, opt_mcv)) { |
| ... | ... | @@ -2993,7 +3013,8 @@ fn airOptionalPayloadPtrSet(func: *Func, inst: Air.Inst.Index) !void { |
| 2993 | 3013 | |
| 2994 | 3014 | fn airUnwrapErrErr(func: *Func, inst: Air.Inst.Index) !void { |
| 2995 | 3015 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2996 | const zcu = func.bin_file.comp.module.?; | |
| 3016 | const pt = func.pt; | |
| 3017 | const zcu = pt.zcu; | |
| 2997 | 3018 | const err_union_ty = func.typeOf(ty_op.operand); |
| 2998 | 3019 | const err_ty = err_union_ty.errorUnionSet(zcu); |
| 2999 | 3020 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| ... | ... | @@ -3004,11 +3025,11 @@ fn airUnwrapErrErr(func: *Func, inst: Air.Inst.Index) !void { |
| 3004 | 3025 | break :result .{ .immediate = 0 }; |
| 3005 | 3026 | } |
| 3006 | 3027 | |
| 3007 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 3028 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3008 | 3029 | break :result operand; |
| 3009 | 3030 | } |
| 3010 | 3031 | |
| 3011 | const err_off: u32 = @intCast(errUnionErrorOffset(payload_ty, zcu)); | |
| 3032 | const err_off: u32 = @intCast(errUnionErrorOffset(payload_ty, pt)); | |
| 3012 | 3033 | |
| 3013 | 3034 | switch (operand) { |
| 3014 | 3035 | .register => |reg| { |
| ... | ... | @@ -3052,13 +3073,14 @@ fn genUnwrapErrUnionPayloadMir( |
| 3052 | 3073 | err_union_ty: Type, |
| 3053 | 3074 | err_union: MCValue, |
| 3054 | 3075 | ) !MCValue { |
| 3055 | const zcu = func.bin_file.comp.module.?; | |
| 3076 | const pt = func.pt; | |
| 3077 | const zcu = pt.zcu; | |
| 3056 | 3078 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| 3057 | 3079 | |
| 3058 | 3080 | const result: MCValue = result: { |
| 3059 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result .none; | |
| 3081 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result .none; | |
| 3060 | 3082 | |
| 3061 | const payload_off: u31 = @intCast(errUnionPayloadOffset(payload_ty, zcu)); | |
| 3083 | const payload_off: u31 = @intCast(errUnionPayloadOffset(payload_ty, pt)); | |
| 3062 | 3084 | switch (err_union) { |
| 3063 | 3085 | .load_frame => |frame_addr| break :result .{ .load_frame = .{ |
| 3064 | 3086 | .index = frame_addr.index, |
| ... | ... | @@ -3127,11 +3149,12 @@ fn airSaveErrReturnTraceIndex(func: *Func, inst: Air.Inst.Index) !void { |
| 3127 | 3149 | } |
| 3128 | 3150 | |
| 3129 | 3151 | fn airWrapOptional(func: *Func, inst: Air.Inst.Index) !void { |
| 3130 | const zcu = func.bin_file.comp.module.?; | |
| 3152 | const pt = func.pt; | |
| 3153 | const zcu = pt.zcu; | |
| 3131 | 3154 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3132 | 3155 | const result: MCValue = result: { |
| 3133 | 3156 | const pl_ty = func.typeOf(ty_op.operand); |
| 3134 | if (!pl_ty.hasRuntimeBits(zcu)) break :result .{ .immediate = 1 }; | |
| 3157 | if (!pl_ty.hasRuntimeBits(pt)) break :result .{ .immediate = 1 }; | |
| 3135 | 3158 | |
| 3136 | 3159 | const opt_ty = func.typeOfIndex(inst); |
| 3137 | 3160 | const pl_mcv = try func.resolveInst(ty_op.operand); |
| ... | ... | @@ -3148,7 +3171,7 @@ fn airWrapOptional(func: *Func, inst: Air.Inst.Index) !void { |
| 3148 | 3171 | try func.genCopy(pl_ty, opt_mcv, pl_mcv); |
| 3149 | 3172 | |
| 3150 | 3173 | if (!same_repr) { |
| 3151 | const pl_abi_size: i32 = @intCast(pl_ty.abiSize(zcu)); | |
| 3174 | const pl_abi_size: i32 = @intCast(pl_ty.abiSize(pt)); | |
| 3152 | 3175 | switch (opt_mcv) { |
| 3153 | 3176 | .load_frame => |frame_addr| try func.genSetMem( |
| 3154 | 3177 | .{ .frame = frame_addr.index }, |
| ... | ... | @@ -3167,7 +3190,8 @@ fn airWrapOptional(func: *Func, inst: Air.Inst.Index) !void { |
| 3167 | 3190 | |
| 3168 | 3191 | /// T to E!T |
| 3169 | 3192 | fn airWrapErrUnionPayload(func: *Func, inst: Air.Inst.Index) !void { |
| 3170 | const zcu = func.bin_file.comp.module.?; | |
| 3193 | const pt = func.pt; | |
| 3194 | const zcu = pt.zcu; | |
| 3171 | 3195 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3172 | 3196 | |
| 3173 | 3197 | const eu_ty = ty_op.ty.toType(); |
| ... | ... | @@ -3176,11 +3200,11 @@ fn airWrapErrUnionPayload(func: *Func, inst: Air.Inst.Index) !void { |
| 3176 | 3200 | const operand = try func.resolveInst(ty_op.operand); |
| 3177 | 3201 | |
| 3178 | 3202 | const result: MCValue = result: { |
| 3179 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result .{ .immediate = 0 }; | |
| 3203 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result .{ .immediate = 0 }; | |
| 3180 | 3204 | |
| 3181 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(eu_ty, zcu)); | |
| 3182 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, zcu)); | |
| 3183 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, zcu)); | |
| 3205 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(eu_ty, pt)); | |
| 3206 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, pt)); | |
| 3207 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, pt)); | |
| 3184 | 3208 | try func.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, operand); |
| 3185 | 3209 | try func.genSetMem(.{ .frame = frame_index }, err_off, err_ty, .{ .immediate = 0 }); |
| 3186 | 3210 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| ... | ... | @@ -3191,7 +3215,8 @@ fn airWrapErrUnionPayload(func: *Func, inst: Air.Inst.Index) !void { |
| 3191 | 3215 | |
| 3192 | 3216 | /// E to E!T |
| 3193 | 3217 | fn airWrapErrUnionErr(func: *Func, inst: Air.Inst.Index) !void { |
| 3194 | const zcu = func.bin_file.comp.module.?; | |
| 3218 | const pt = func.pt; | |
| 3219 | const zcu = pt.zcu; | |
| 3195 | 3220 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3196 | 3221 | |
| 3197 | 3222 | const eu_ty = ty_op.ty.toType(); |
| ... | ... | @@ -3199,11 +3224,11 @@ fn airWrapErrUnionErr(func: *Func, inst: Air.Inst.Index) !void { |
| 3199 | 3224 | const err_ty = eu_ty.errorUnionSet(zcu); |
| 3200 | 3225 | |
| 3201 | 3226 | const result: MCValue = result: { |
| 3202 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result try func.resolveInst(ty_op.operand); | |
| 3227 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result try func.resolveInst(ty_op.operand); | |
| 3203 | 3228 | |
| 3204 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(eu_ty, zcu)); | |
| 3205 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, zcu)); | |
| 3206 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, zcu)); | |
| 3229 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(eu_ty, pt)); | |
| 3230 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, pt)); | |
| 3231 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, pt)); | |
| 3207 | 3232 | try func.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, .undef); |
| 3208 | 3233 | const operand = try func.resolveInst(ty_op.operand); |
| 3209 | 3234 | try func.genSetMem(.{ .frame = frame_index }, err_off, err_ty, operand); |
| ... | ... | @@ -3327,15 +3352,16 @@ fn airPtrSlicePtrPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3327 | 3352 | } |
| 3328 | 3353 | |
| 3329 | 3354 | fn airSliceElemVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3330 | const mod = func.bin_file.comp.module.?; | |
| 3355 | const pt = func.pt; | |
| 3356 | const zcu = pt.zcu; | |
| 3331 | 3357 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3332 | 3358 | |
| 3333 | 3359 | const result: MCValue = result: { |
| 3334 | 3360 | const elem_ty = func.typeOfIndex(inst); |
| 3335 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result .none; | |
| 3361 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result .none; | |
| 3336 | 3362 | |
| 3337 | 3363 | const slice_ty = func.typeOf(bin_op.lhs); |
| 3338 | const slice_ptr_field_type = slice_ty.slicePtrFieldType(mod); | |
| 3364 | const slice_ptr_field_type = slice_ty.slicePtrFieldType(zcu); | |
| 3339 | 3365 | const elem_ptr = try func.genSliceElemPtr(bin_op.lhs, bin_op.rhs); |
| 3340 | 3366 | const dst_mcv = try func.allocRegOrMem(elem_ty, inst, false); |
| 3341 | 3367 | try func.load(dst_mcv, elem_ptr, slice_ptr_field_type); |
| ... | ... | @@ -3352,7 +3378,8 @@ fn airSliceElemPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3352 | 3378 | } |
| 3353 | 3379 | |
| 3354 | 3380 | fn genSliceElemPtr(func: *Func, lhs: Air.Inst.Ref, rhs: Air.Inst.Ref) !MCValue { |
| 3355 | const zcu = func.bin_file.comp.module.?; | |
| 3381 | const pt = func.pt; | |
| 3382 | const zcu = pt.zcu; | |
| 3356 | 3383 | const slice_ty = func.typeOf(lhs); |
| 3357 | 3384 | const slice_mcv = try func.resolveInst(lhs); |
| 3358 | 3385 | const slice_mcv_lock: ?RegisterLock = switch (slice_mcv) { |
| ... | ... | @@ -3362,7 +3389,7 @@ fn genSliceElemPtr(func: *Func, lhs: Air.Inst.Ref, rhs: Air.Inst.Ref) !MCValue { |
| 3362 | 3389 | defer if (slice_mcv_lock) |lock| func.register_manager.unlockReg(lock); |
| 3363 | 3390 | |
| 3364 | 3391 | const elem_ty = slice_ty.childType(zcu); |
| 3365 | const elem_size = elem_ty.abiSize(zcu); | |
| 3392 | const elem_size = elem_ty.abiSize(pt); | |
| 3366 | 3393 | |
| 3367 | 3394 | const index_ty = func.typeOf(rhs); |
| 3368 | 3395 | const index_mcv = try func.resolveInst(rhs); |
| ... | ... | @@ -3394,7 +3421,8 @@ fn genSliceElemPtr(func: *Func, lhs: Air.Inst.Ref, rhs: Air.Inst.Ref) !MCValue { |
| 3394 | 3421 | } |
| 3395 | 3422 | |
| 3396 | 3423 | fn airArrayElemVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3397 | const zcu = func.bin_file.comp.module.?; | |
| 3424 | const pt = func.pt; | |
| 3425 | const zcu = pt.zcu; | |
| 3398 | 3426 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3399 | 3427 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3400 | 3428 | const result_ty = func.typeOfIndex(inst); |
| ... | ... | @@ -3406,14 +3434,14 @@ fn airArrayElemVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3406 | 3434 | const index_ty = func.typeOf(bin_op.rhs); |
| 3407 | 3435 | |
| 3408 | 3436 | const elem_ty = array_ty.childType(zcu); |
| 3409 | const elem_abi_size = elem_ty.abiSize(zcu); | |
| 3437 | const elem_abi_size = elem_ty.abiSize(pt); | |
| 3410 | 3438 | |
| 3411 | 3439 | const addr_reg, const addr_reg_lock = try func.allocReg(.int); |
| 3412 | 3440 | defer func.register_manager.unlockReg(addr_reg_lock); |
| 3413 | 3441 | |
| 3414 | 3442 | switch (array_mcv) { |
| 3415 | 3443 | .register => { |
| 3416 | const frame_index = try func.allocFrameIndex(FrameAlloc.initType(array_ty, zcu)); | |
| 3444 | const frame_index = try func.allocFrameIndex(FrameAlloc.initType(array_ty, pt)); | |
| 3417 | 3445 | try func.genSetMem(.{ .frame = frame_index }, 0, array_ty, array_mcv); |
| 3418 | 3446 | try func.genSetReg(Type.usize, addr_reg, .{ .lea_frame = .{ .index = frame_index } }); |
| 3419 | 3447 | }, |
| ... | ... | @@ -3451,7 +3479,8 @@ fn airPtrElemVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3451 | 3479 | } |
| 3452 | 3480 | |
| 3453 | 3481 | fn airPtrElemPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3454 | const zcu = func.bin_file.comp.module.?; | |
| 3482 | const pt = func.pt; | |
| 3483 | const zcu = pt.zcu; | |
| 3455 | 3484 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3456 | 3485 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3457 | 3486 | |
| ... | ... | @@ -3474,7 +3503,7 @@ fn airPtrElemPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 3474 | 3503 | } |
| 3475 | 3504 | |
| 3476 | 3505 | const elem_ty = base_ptr_ty.elemType2(zcu); |
| 3477 | const elem_abi_size = elem_ty.abiSize(zcu); | |
| 3506 | const elem_abi_size = elem_ty.abiSize(pt); | |
| 3478 | 3507 | const index_ty = func.typeOf(extra.rhs); |
| 3479 | 3508 | const index_mcv = try func.resolveInst(extra.rhs); |
| 3480 | 3509 | const index_lock: ?RegisterLock = switch (index_mcv) { |
| ... | ... | @@ -3536,7 +3565,8 @@ fn airPopcount(func: *Func, inst: Air.Inst.Index) !void { |
| 3536 | 3565 | } |
| 3537 | 3566 | |
| 3538 | 3567 | fn airAbs(func: *Func, inst: Air.Inst.Index) !void { |
| 3539 | const zcu = func.bin_file.comp.module.?; | |
| 3568 | const pt = func.pt; | |
| 3569 | const zcu = pt.zcu; | |
| 3540 | 3570 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3541 | 3571 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3542 | 3572 | const ty = func.typeOf(ty_op.operand); |
| ... | ... | @@ -3545,7 +3575,7 @@ fn airAbs(func: *Func, inst: Air.Inst.Index) !void { |
| 3545 | 3575 | |
| 3546 | 3576 | switch (scalar_ty.zigTypeTag(zcu)) { |
| 3547 | 3577 | .Int => if (ty.zigTypeTag(zcu) == .Vector) { |
| 3548 | return func.fail("TODO implement airAbs for {}", .{ty.fmt(zcu)}); | |
| 3578 | return func.fail("TODO implement airAbs for {}", .{ty.fmt(pt)}); | |
| 3549 | 3579 | } else { |
| 3550 | 3580 | const return_mcv = try func.copyToNewRegister(inst, operand); |
| 3551 | 3581 | const operand_reg = return_mcv.register; |
| ... | ... | @@ -3615,7 +3645,7 @@ fn airAbs(func: *Func, inst: Air.Inst.Index) !void { |
| 3615 | 3645 | |
| 3616 | 3646 | break :result return_mcv; |
| 3617 | 3647 | }, |
| 3618 | else => return func.fail("TODO: implement airAbs {}", .{scalar_ty.fmt(zcu)}), | |
| 3648 | else => return func.fail("TODO: implement airAbs {}", .{scalar_ty.fmt(pt)}), | |
| 3619 | 3649 | } |
| 3620 | 3650 | |
| 3621 | 3651 | break :result .unreach; |
| ... | ... | @@ -3626,7 +3656,8 @@ fn airAbs(func: *Func, inst: Air.Inst.Index) !void { |
| 3626 | 3656 | fn airByteSwap(func: *Func, inst: Air.Inst.Index) !void { |
| 3627 | 3657 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3628 | 3658 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3629 | const zcu = func.bin_file.comp.module.?; | |
| 3659 | const pt = func.pt; | |
| 3660 | const zcu = pt.zcu; | |
| 3630 | 3661 | const ty = func.typeOf(ty_op.operand); |
| 3631 | 3662 | const operand = try func.resolveInst(ty_op.operand); |
| 3632 | 3663 | |
| ... | ... | @@ -3746,12 +3777,13 @@ fn reuseOperandAdvanced( |
| 3746 | 3777 | } |
| 3747 | 3778 | |
| 3748 | 3779 | fn airLoad(func: *Func, inst: Air.Inst.Index) !void { |
| 3749 | const zcu = func.bin_file.comp.module.?; | |
| 3780 | const pt = func.pt; | |
| 3781 | const zcu = pt.zcu; | |
| 3750 | 3782 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3751 | 3783 | const elem_ty = func.typeOfIndex(inst); |
| 3752 | 3784 | |
| 3753 | 3785 | const result: MCValue = result: { |
| 3754 | if (!elem_ty.hasRuntimeBits(zcu)) | |
| 3786 | if (!elem_ty.hasRuntimeBits(pt)) | |
| 3755 | 3787 | break :result .none; |
| 3756 | 3788 | |
| 3757 | 3789 | const ptr = try func.resolveInst(ty_op.operand); |
| ... | ... | @@ -3759,7 +3791,7 @@ fn airLoad(func: *Func, inst: Air.Inst.Index) !void { |
| 3759 | 3791 | if (func.liveness.isUnused(inst) and !is_volatile) |
| 3760 | 3792 | break :result .unreach; |
| 3761 | 3793 | |
| 3762 | const elem_size = elem_ty.abiSize(zcu); | |
| 3794 | const elem_size = elem_ty.abiSize(pt); | |
| 3763 | 3795 | |
| 3764 | 3796 | const dst_mcv: MCValue = blk: { |
| 3765 | 3797 | // Pointer is 8 bytes, and if the element is more than that, we cannot reuse it. |
| ... | ... | @@ -3778,10 +3810,11 @@ fn airLoad(func: *Func, inst: Air.Inst.Index) !void { |
| 3778 | 3810 | } |
| 3779 | 3811 | |
| 3780 | 3812 | fn load(func: *Func, dst_mcv: MCValue, ptr_mcv: MCValue, ptr_ty: Type) InnerError!void { |
| 3781 | const zcu = func.bin_file.comp.module.?; | |
| 3813 | const pt = func.pt; | |
| 3814 | const zcu = pt.zcu; | |
| 3782 | 3815 | const dst_ty = ptr_ty.childType(zcu); |
| 3783 | 3816 | |
| 3784 | log.debug("loading {}:{} into {}", .{ ptr_mcv, ptr_ty.fmt(zcu), dst_mcv }); | |
| 3817 | log.debug("loading {}:{} into {}", .{ ptr_mcv, ptr_ty.fmt(pt), dst_mcv }); | |
| 3785 | 3818 | |
| 3786 | 3819 | switch (ptr_mcv) { |
| 3787 | 3820 | .none, |
| ... | ... | @@ -3833,9 +3866,7 @@ fn airStore(func: *Func, inst: Air.Inst.Index, safety: bool) !void { |
| 3833 | 3866 | |
| 3834 | 3867 | /// Loads `value` into the "payload" of `pointer`. |
| 3835 | 3868 | fn store(func: *Func, ptr_mcv: MCValue, src_mcv: MCValue, ptr_ty: Type, src_ty: Type) !void { |
| 3836 | const zcu = func.bin_file.comp.module.?; | |
| 3837 | ||
| 3838 | log.debug("storing {}:{} in {}:{}", .{ src_mcv, src_ty.fmt(zcu), ptr_mcv, ptr_ty.fmt(zcu) }); | |
| 3869 | log.debug("storing {}:{} in {}:{}", .{ src_mcv, src_ty.fmt(func.pt), ptr_mcv, ptr_ty.fmt(func.pt) }); | |
| 3839 | 3870 | |
| 3840 | 3871 | switch (ptr_mcv) { |
| 3841 | 3872 | .none => unreachable, |
| ... | ... | @@ -3881,7 +3912,8 @@ fn airStructFieldPtrIndex(func: *Func, inst: Air.Inst.Index, index: u8) !void { |
| 3881 | 3912 | } |
| 3882 | 3913 | |
| 3883 | 3914 | fn structFieldPtr(func: *Func, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32) !MCValue { |
| 3884 | const zcu = func.bin_file.comp.module.?; | |
| 3915 | const pt = func.pt; | |
| 3916 | const zcu = pt.zcu; | |
| 3885 | 3917 | const ptr_field_ty = func.typeOfIndex(inst); |
| 3886 | 3918 | const ptr_container_ty = func.typeOf(operand); |
| 3887 | 3919 | const ptr_container_ty_info = ptr_container_ty.ptrInfo(zcu); |
| ... | ... | @@ -3889,12 +3921,12 @@ fn structFieldPtr(func: *Func, inst: Air.Inst.Index, operand: Air.Inst.Ref, inde |
| 3889 | 3921 | |
| 3890 | 3922 | const field_offset: i32 = if (zcu.typeToPackedStruct(container_ty)) |struct_obj| |
| 3891 | 3923 | if (ptr_field_ty.ptrInfo(zcu).packed_offset.host_size == 0) |
| 3892 | @divExact(zcu.structPackedFieldBitOffset(struct_obj, index) + | |
| 3924 | @divExact(pt.structPackedFieldBitOffset(struct_obj, index) + | |
| 3893 | 3925 | ptr_container_ty_info.packed_offset.bit_offset, 8) |
| 3894 | 3926 | else |
| 3895 | 3927 | 0 |
| 3896 | 3928 | else |
| 3897 | @intCast(container_ty.structFieldOffset(index, zcu)); | |
| 3929 | @intCast(container_ty.structFieldOffset(index, pt)); | |
| 3898 | 3930 | |
| 3899 | 3931 | const src_mcv = try func.resolveInst(operand); |
| 3900 | 3932 | const dst_mcv = if (switch (src_mcv) { |
| ... | ... | @@ -3906,7 +3938,8 @@ fn structFieldPtr(func: *Func, inst: Air.Inst.Index, operand: Air.Inst.Ref, inde |
| 3906 | 3938 | } |
| 3907 | 3939 | |
| 3908 | 3940 | fn airStructFieldVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3909 | const mod = func.bin_file.comp.module.?; | |
| 3941 | const pt = func.pt; | |
| 3942 | const zcu = pt.zcu; | |
| 3910 | 3943 | |
| 3911 | 3944 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3912 | 3945 | const extra = func.air.extraData(Air.StructField, ty_pl.payload).data; |
| ... | ... | @@ -3914,16 +3947,15 @@ fn airStructFieldVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3914 | 3947 | const index = extra.field_index; |
| 3915 | 3948 | |
| 3916 | 3949 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 3917 | const zcu = func.bin_file.comp.module.?; | |
| 3918 | 3950 | const src_mcv = try func.resolveInst(operand); |
| 3919 | 3951 | const struct_ty = func.typeOf(operand); |
| 3920 | 3952 | const field_ty = struct_ty.structFieldType(index, zcu); |
| 3921 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) break :result .none; | |
| 3953 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result .none; | |
| 3922 | 3954 | |
| 3923 | 3955 | const field_off: u32 = switch (struct_ty.containerLayout(zcu)) { |
| 3924 | .auto, .@"extern" => @intCast(struct_ty.structFieldOffset(index, zcu) * 8), | |
| 3956 | .auto, .@"extern" => @intCast(struct_ty.structFieldOffset(index, pt) * 8), | |
| 3925 | 3957 | .@"packed" => if (zcu.typeToStruct(struct_ty)) |struct_type| |
| 3926 | zcu.structPackedFieldBitOffset(struct_type, index) | |
| 3958 | pt.structPackedFieldBitOffset(struct_type, index) | |
| 3927 | 3959 | else |
| 3928 | 3960 | 0, |
| 3929 | 3961 | }; |
| ... | ... | @@ -3958,15 +3990,15 @@ fn airStructFieldVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3958 | 3990 | break :result if (field_off == 0) dst_mcv else try func.copyToNewRegister(inst, dst_mcv); |
| 3959 | 3991 | }, |
| 3960 | 3992 | .load_frame => { |
| 3961 | const field_abi_size: u32 = @intCast(field_ty.abiSize(mod)); | |
| 3993 | const field_abi_size: u32 = @intCast(field_ty.abiSize(pt)); | |
| 3962 | 3994 | if (field_off % 8 == 0) { |
| 3963 | 3995 | const field_byte_off = @divExact(field_off, 8); |
| 3964 | 3996 | const off_mcv = src_mcv.address().offset(@intCast(field_byte_off)).deref(); |
| 3965 | const field_bit_size = field_ty.bitSize(mod); | |
| 3997 | const field_bit_size = field_ty.bitSize(pt); | |
| 3966 | 3998 | |
| 3967 | 3999 | if (field_abi_size <= 8) { |
| 3968 | const int_ty = try mod.intType( | |
| 3969 | if (field_ty.isAbiInt(mod)) field_ty.intInfo(mod).signedness else .unsigned, | |
| 4000 | const int_ty = try pt.intType( | |
| 4001 | if (field_ty.isAbiInt(zcu)) field_ty.intInfo(zcu).signedness else .unsigned, | |
| 3970 | 4002 | @intCast(field_bit_size), |
| 3971 | 4003 | ); |
| 3972 | 4004 | |
| ... | ... | @@ -3978,7 +4010,7 @@ fn airStructFieldVal(func: *Func, inst: Air.Inst.Index) !void { |
| 3978 | 4010 | break :result try func.copyToNewRegister(inst, dst_mcv); |
| 3979 | 4011 | } |
| 3980 | 4012 | |
| 3981 | const container_abi_size: u32 = @intCast(struct_ty.abiSize(mod)); | |
| 4013 | const container_abi_size: u32 = @intCast(struct_ty.abiSize(pt)); | |
| 3982 | 4014 | const dst_mcv = if (field_byte_off + field_abi_size <= container_abi_size and |
| 3983 | 4015 | func.reuseOperand(inst, operand, 0, src_mcv)) |
| 3984 | 4016 | off_mcv |
| ... | ... | @@ -4014,7 +4046,8 @@ fn airFieldParentPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 4014 | 4046 | } |
| 4015 | 4047 | |
| 4016 | 4048 | fn genArgDbgInfo(func: Func, inst: Air.Inst.Index, mcv: MCValue) !void { |
| 4017 | const zcu = func.bin_file.comp.module.?; | |
| 4049 | const pt = func.pt; | |
| 4050 | const zcu = pt.zcu; | |
| 4018 | 4051 | const arg = func.air.instructions.items(.data)[@intFromEnum(inst)].arg; |
| 4019 | 4052 | const ty = arg.ty.toType(); |
| 4020 | 4053 | const owner_decl = zcu.funcOwnerDeclIndex(func.func_index); |
| ... | ... | @@ -4139,7 +4172,8 @@ fn genCall( |
| 4139 | 4172 | arg_tys: []const Type, |
| 4140 | 4173 | args: []const MCValue, |
| 4141 | 4174 | ) !MCValue { |
| 4142 | const zcu = func.bin_file.comp.module.?; | |
| 4175 | const pt = func.pt; | |
| 4176 | const zcu = pt.zcu; | |
| 4143 | 4177 | |
| 4144 | 4178 | const fn_ty = switch (info) { |
| 4145 | 4179 | .air => |callee| fn_info: { |
| ... | ... | @@ -4150,7 +4184,7 @@ fn genCall( |
| 4150 | 4184 | else => unreachable, |
| 4151 | 4185 | }; |
| 4152 | 4186 | }, |
| 4153 | .lib => |lib| try zcu.funcType(.{ | |
| 4187 | .lib => |lib| try pt.funcType(.{ | |
| 4154 | 4188 | .param_types = lib.param_types, |
| 4155 | 4189 | .return_type = lib.return_type, |
| 4156 | 4190 | .cc = .C, |
| ... | ... | @@ -4208,7 +4242,7 @@ fn genCall( |
| 4208 | 4242 | try reg_locks.appendSlice(&func.register_manager.lockRegs(2, regs)); |
| 4209 | 4243 | }, |
| 4210 | 4244 | .indirect => |reg_off| { |
| 4211 | frame_index.* = try func.allocFrameIndex(FrameAlloc.initType(arg_ty, zcu)); | |
| 4245 | frame_index.* = try func.allocFrameIndex(FrameAlloc.initType(arg_ty, pt)); | |
| 4212 | 4246 | try func.genSetMem(.{ .frame = frame_index.* }, 0, arg_ty, src_arg); |
| 4213 | 4247 | try func.register_manager.getReg(reg_off.reg, null); |
| 4214 | 4248 | try reg_locks.append(func.register_manager.lockReg(reg_off.reg)); |
| ... | ... | @@ -4221,7 +4255,7 @@ fn genCall( |
| 4221 | 4255 | .none, .unreach => {}, |
| 4222 | 4256 | .indirect => |reg_off| { |
| 4223 | 4257 | const ret_ty = Type.fromInterned(fn_info.return_type); |
| 4224 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(ret_ty, zcu)); | |
| 4258 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(ret_ty, pt)); | |
| 4225 | 4259 | try func.genSetReg(Type.usize, reg_off.reg, .{ |
| 4226 | 4260 | .lea_frame = .{ .index = frame_index, .off = -reg_off.off }, |
| 4227 | 4261 | }); |
| ... | ... | @@ -4251,7 +4285,7 @@ fn genCall( |
| 4251 | 4285 | // on linking. |
| 4252 | 4286 | switch (info) { |
| 4253 | 4287 | .air => |callee| { |
| 4254 | if (try func.air.value(callee, zcu)) |func_value| { | |
| 4288 | if (try func.air.value(callee, pt)) |func_value| { | |
| 4255 | 4289 | const func_key = zcu.intern_pool.indexToKey(func_value.ip_index); |
| 4256 | 4290 | switch (switch (func_key) { |
| 4257 | 4291 | else => func_key, |
| ... | ... | @@ -4324,7 +4358,8 @@ fn genCall( |
| 4324 | 4358 | } |
| 4325 | 4359 | |
| 4326 | 4360 | fn airRet(func: *Func, inst: Air.Inst.Index, safety: bool) !void { |
| 4327 | const zcu = func.bin_file.comp.module.?; | |
| 4361 | const pt = func.pt; | |
| 4362 | const zcu = pt.zcu; | |
| 4328 | 4363 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4329 | 4364 | |
| 4330 | 4365 | if (safety) { |
| ... | ... | @@ -4394,7 +4429,8 @@ fn airRetLoad(func: *Func, inst: Air.Inst.Index) !void { |
| 4394 | 4429 | |
| 4395 | 4430 | fn airCmp(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 4396 | 4431 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4397 | const zcu = func.bin_file.comp.module.?; | |
| 4432 | const pt = func.pt; | |
| 4433 | const zcu = pt.zcu; | |
| 4398 | 4434 | |
| 4399 | 4435 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4400 | 4436 | const lhs_ty = func.typeOf(bin_op.lhs); |
| ... | ... | @@ -4415,7 +4451,7 @@ fn airCmp(func: *Func, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 4415 | 4451 | .ErrorSet => Type.anyerror, |
| 4416 | 4452 | .Optional => blk: { |
| 4417 | 4453 | const payload_ty = lhs_ty.optionalChild(zcu); |
| 4418 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 4454 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4419 | 4455 | break :blk Type.u1; |
| 4420 | 4456 | } else if (lhs_ty.isPtrLikeOptional(zcu)) { |
| 4421 | 4457 | break :blk Type.usize; |
| ... | ... | @@ -4503,7 +4539,8 @@ fn genVarDbgInfo( |
| 4503 | 4539 | mcv: MCValue, |
| 4504 | 4540 | name: [:0]const u8, |
| 4505 | 4541 | ) !void { |
| 4506 | const zcu = func.bin_file.comp.module.?; | |
| 4542 | const pt = func.pt; | |
| 4543 | const zcu = pt.zcu; | |
| 4507 | 4544 | const is_ptr = switch (tag) { |
| 4508 | 4545 | .dbg_var_ptr => true, |
| 4509 | 4546 | .dbg_var_val => false, |
| ... | ... | @@ -4595,13 +4632,14 @@ fn condBr(func: *Func, cond_ty: Type, condition: MCValue) !Mir.Inst.Index { |
| 4595 | 4632 | } |
| 4596 | 4633 | |
| 4597 | 4634 | fn isNull(func: *Func, inst: Air.Inst.Index, opt_ty: Type, opt_mcv: MCValue) !MCValue { |
| 4598 | const zcu = func.bin_file.comp.module.?; | |
| 4635 | const pt = func.pt; | |
| 4636 | const zcu = pt.zcu; | |
| 4599 | 4637 | const pl_ty = opt_ty.optionalChild(zcu); |
| 4600 | 4638 | |
| 4601 | 4639 | const some_info: struct { off: i32, ty: Type } = if (opt_ty.optionalReprIsPayload(zcu)) |
| 4602 | 4640 | .{ .off = 0, .ty = if (pl_ty.isSlice(zcu)) pl_ty.slicePtrFieldType(zcu) else pl_ty } |
| 4603 | 4641 | else |
| 4604 | .{ .off = @intCast(pl_ty.abiSize(zcu)), .ty = Type.bool }; | |
| 4642 | .{ .off = @intCast(pl_ty.abiSize(pt)), .ty = Type.bool }; | |
| 4605 | 4643 | |
| 4606 | 4644 | const return_mcv = try func.allocRegOrMem(func.typeOfIndex(inst), inst, true); |
| 4607 | 4645 | assert(return_mcv == .register); // should not be larger 8 bytes |
| ... | ... | @@ -4642,7 +4680,7 @@ fn isNull(func: *Func, inst: Air.Inst.Index, opt_ty: Type, opt_mcv: MCValue) !MC |
| 4642 | 4680 | return return_mcv; |
| 4643 | 4681 | } |
| 4644 | 4682 | assert(some_info.ty.ip_index == .bool_type); |
| 4645 | const opt_abi_size: u32 = @intCast(opt_ty.abiSize(zcu)); | |
| 4683 | const opt_abi_size: u32 = @intCast(opt_ty.abiSize(pt)); | |
| 4646 | 4684 | _ = opt_abi_size; |
| 4647 | 4685 | return func.fail("TODO: isNull some_info.off != 0 register", .{}); |
| 4648 | 4686 | }, |
| ... | ... | @@ -4742,7 +4780,8 @@ fn airIsErr(func: *Func, inst: Air.Inst.Index) !void { |
| 4742 | 4780 | } |
| 4743 | 4781 | |
| 4744 | 4782 | fn airIsErrPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 4745 | const zcu = func.bin_file.comp.module.?; | |
| 4783 | const pt = func.pt; | |
| 4784 | const zcu = pt.zcu; | |
| 4746 | 4785 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4747 | 4786 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4748 | 4787 | const operand_ptr = try func.resolveInst(un_op); |
| ... | ... | @@ -4768,10 +4807,11 @@ fn airIsErrPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 4768 | 4807 | /// Result is in the return register. |
| 4769 | 4808 | fn isErr(func: *Func, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue { |
| 4770 | 4809 | _ = maybe_inst; |
| 4771 | const zcu = func.bin_file.comp.module.?; | |
| 4810 | const pt = func.pt; | |
| 4811 | const zcu = pt.zcu; | |
| 4772 | 4812 | const err_ty = eu_ty.errorUnionSet(zcu); |
| 4773 | 4813 | if (err_ty.errorSetIsEmpty(zcu)) return MCValue{ .immediate = 0 }; // always false |
| 4774 | const err_off: u31 = @intCast(errUnionErrorOffset(eu_ty.errorUnionPayload(zcu), zcu)); | |
| 4814 | const err_off: u31 = @intCast(errUnionErrorOffset(eu_ty.errorUnionPayload(zcu), pt)); | |
| 4775 | 4815 | |
| 4776 | 4816 | const return_reg, const return_lock = try func.allocReg(.int); |
| 4777 | 4817 | defer func.register_manager.unlockReg(return_lock); |
| ... | ... | @@ -4858,7 +4898,8 @@ fn isNonErr(func: *Func, inst: Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MC |
| 4858 | 4898 | } |
| 4859 | 4899 | |
| 4860 | 4900 | fn airIsNonErrPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 4861 | const zcu = func.bin_file.comp.module.?; | |
| 4901 | const pt = func.pt; | |
| 4902 | const zcu = pt.zcu; | |
| 4862 | 4903 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4863 | 4904 | const result: MCValue = if (func.liveness.isUnused(inst)) .unreach else result: { |
| 4864 | 4905 | const operand_ptr = try func.resolveInst(un_op); |
| ... | ... | @@ -5063,12 +5104,12 @@ fn performReloc(func: *Func, inst: Mir.Inst.Index) void { |
| 5063 | 5104 | } |
| 5064 | 5105 | |
| 5065 | 5106 | fn airBr(func: *Func, inst: Air.Inst.Index) !void { |
| 5066 | const mod = func.bin_file.comp.module.?; | |
| 5107 | const pt = func.pt; | |
| 5067 | 5108 | const br = func.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 5068 | 5109 | |
| 5069 | 5110 | const block_ty = func.typeOfIndex(br.block_inst); |
| 5070 | 5111 | const block_unused = |
| 5071 | !block_ty.hasRuntimeBitsIgnoreComptime(mod) or func.liveness.isUnused(br.block_inst); | |
| 5112 | !block_ty.hasRuntimeBitsIgnoreComptime(pt) or func.liveness.isUnused(br.block_inst); | |
| 5072 | 5113 | const block_tracking = func.inst_tracking.getPtr(br.block_inst).?; |
| 5073 | 5114 | const block_data = func.blocks.getPtr(br.block_inst).?; |
| 5074 | 5115 | const first_br = block_data.relocs.items.len == 0; |
| ... | ... | @@ -5288,8 +5329,6 @@ fn airAsm(func: *Func, inst: Air.Inst.Index) !void { |
| 5288 | 5329 | |
| 5289 | 5330 | /// Sets the value of `dst_mcv` to the value of `src_mcv`. |
| 5290 | 5331 | fn genCopy(func: *Func, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void { |
| 5291 | const zcu = func.bin_file.comp.module.?; | |
| 5292 | ||
| 5293 | 5332 | // There isn't anything to store |
| 5294 | 5333 | if (dst_mcv == .none) return; |
| 5295 | 5334 | |
| ... | ... | @@ -5362,7 +5401,7 @@ fn genCopy(func: *Func, ty: Type, dst_mcv: MCValue, src_mcv: MCValue) !void { |
| 5362 | 5401 | } }, |
| 5363 | 5402 | else => unreachable, |
| 5364 | 5403 | }); |
| 5365 | part_disp += @intCast(dst_ty.abiSize(zcu)); | |
| 5404 | part_disp += @intCast(dst_ty.abiSize(func.pt)); | |
| 5366 | 5405 | } |
| 5367 | 5406 | }, |
| 5368 | 5407 | else => return func.fail("TODO: genCopy to {s} from {s}", .{ @tagName(dst_mcv), @tagName(src_mcv) }), |
| ... | ... | @@ -5555,8 +5594,9 @@ fn genInlineMemset( |
| 5555 | 5594 | |
| 5556 | 5595 | /// Sets the value of `src_mcv` into `reg`. Assumes you have a lock on it. |
| 5557 | 5596 | fn genSetReg(func: *Func, ty: Type, reg: Register, src_mcv: MCValue) InnerError!void { |
| 5558 | const zcu = func.bin_file.comp.module.?; | |
| 5559 | const abi_size: u32 = @intCast(ty.abiSize(zcu)); | |
| 5597 | const pt = func.pt; | |
| 5598 | const zcu = pt.zcu; | |
| 5599 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 5560 | 5600 | |
| 5561 | 5601 | if (abi_size > 8) return std.debug.panic("tried to set reg with size {}", .{abi_size}); |
| 5562 | 5602 | |
| ... | ... | @@ -5784,8 +5824,8 @@ fn genSetMem( |
| 5784 | 5824 | ty: Type, |
| 5785 | 5825 | src_mcv: MCValue, |
| 5786 | 5826 | ) InnerError!void { |
| 5787 | const mod = func.bin_file.comp.module.?; | |
| 5788 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 5827 | const pt = func.pt; | |
| 5828 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 5789 | 5829 | const dst_ptr_mcv: MCValue = switch (base) { |
| 5790 | 5830 | .reg => |base_reg| .{ .register_offset = .{ .reg = base_reg, .off = disp } }, |
| 5791 | 5831 | .frame => |base_frame_index| .{ .lea_frame = .{ .index = base_frame_index, .off = disp } }, |
| ... | ... | @@ -5883,7 +5923,7 @@ fn genSetMem( |
| 5883 | 5923 | var part_disp: i32 = disp; |
| 5884 | 5924 | for (try func.splitType(ty), src_regs) |src_ty, src_reg| { |
| 5885 | 5925 | try func.genSetMem(base, part_disp, src_ty, .{ .register = src_reg }); |
| 5886 | part_disp += @intCast(src_ty.abiSize(mod)); | |
| 5926 | part_disp += @intCast(src_ty.abiSize(pt)); | |
| 5887 | 5927 | } |
| 5888 | 5928 | }, |
| 5889 | 5929 | .immediate => { |
| ... | ... | @@ -5914,7 +5954,8 @@ fn airIntFromPtr(func: *Func, inst: Air.Inst.Index) !void { |
| 5914 | 5954 | } |
| 5915 | 5955 | |
| 5916 | 5956 | fn airBitCast(func: *Func, inst: Air.Inst.Index) !void { |
| 5917 | const zcu = func.bin_file.comp.module.?; | |
| 5957 | const pt = func.pt; | |
| 5958 | const zcu = pt.zcu; | |
| 5918 | 5959 | |
| 5919 | 5960 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5920 | 5961 | const result = if (func.liveness.isUnused(inst)) .unreach else result: { |
| ... | ... | @@ -5926,10 +5967,10 @@ fn airBitCast(func: *Func, inst: Air.Inst.Index) !void { |
| 5926 | 5967 | const src_lock = if (src_mcv.getReg()) |reg| func.register_manager.lockReg(reg) else null; |
| 5927 | 5968 | defer if (src_lock) |lock| func.register_manager.unlockReg(lock); |
| 5928 | 5969 | |
| 5929 | const dst_mcv = if (dst_ty.abiSize(zcu) <= src_ty.abiSize(zcu) and | |
| 5970 | const dst_mcv = if (dst_ty.abiSize(pt) <= src_ty.abiSize(pt) and | |
| 5930 | 5971 | func.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: { |
| 5931 | 5972 | const dst_mcv = try func.allocRegOrMem(dst_ty, inst, true); |
| 5932 | try func.genCopy(switch (math.order(dst_ty.abiSize(zcu), src_ty.abiSize(zcu))) { | |
| 5973 | try func.genCopy(switch (math.order(dst_ty.abiSize(pt), src_ty.abiSize(pt))) { | |
| 5933 | 5974 | .lt => dst_ty, |
| 5934 | 5975 | .eq => if (!dst_mcv.isMemory() or src_mcv.isMemory()) dst_ty else src_ty, |
| 5935 | 5976 | .gt => src_ty, |
| ... | ... | @@ -5940,17 +5981,18 @@ fn airBitCast(func: *Func, inst: Air.Inst.Index) !void { |
| 5940 | 5981 | if (dst_ty.isAbiInt(zcu) and src_ty.isAbiInt(zcu) and |
| 5941 | 5982 | dst_ty.intInfo(zcu).signedness == src_ty.intInfo(zcu).signedness) break :result dst_mcv; |
| 5942 | 5983 | |
| 5943 | const abi_size = dst_ty.abiSize(zcu); | |
| 5944 | const bit_size = dst_ty.bitSize(zcu); | |
| 5984 | const abi_size = dst_ty.abiSize(pt); | |
| 5985 | const bit_size = dst_ty.bitSize(pt); | |
| 5945 | 5986 | if (abi_size * 8 <= bit_size) break :result dst_mcv; |
| 5946 | 5987 | |
| 5947 | return func.fail("TODO: airBitCast {} to {}", .{ src_ty.fmt(zcu), dst_ty.fmt(zcu) }); | |
| 5988 | return func.fail("TODO: airBitCast {} to {}", .{ src_ty.fmt(pt), dst_ty.fmt(pt) }); | |
| 5948 | 5989 | }; |
| 5949 | 5990 | return func.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 5950 | 5991 | } |
| 5951 | 5992 | |
| 5952 | 5993 | fn airArrayToSlice(func: *Func, inst: Air.Inst.Index) !void { |
| 5953 | const zcu = func.bin_file.comp.module.?; | |
| 5994 | const pt = func.pt; | |
| 5995 | const zcu = pt.zcu; | |
| 5954 | 5996 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5955 | 5997 | |
| 5956 | 5998 | const slice_ty = func.typeOfIndex(inst); |
| ... | ... | @@ -5959,11 +6001,11 @@ fn airArrayToSlice(func: *Func, inst: Air.Inst.Index) !void { |
| 5959 | 6001 | const array_ty = ptr_ty.childType(zcu); |
| 5960 | 6002 | const array_len = array_ty.arrayLen(zcu); |
| 5961 | 6003 | |
| 5962 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(slice_ty, zcu)); | |
| 6004 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(slice_ty, pt)); | |
| 5963 | 6005 | try func.genSetMem(.{ .frame = frame_index }, 0, ptr_ty, ptr); |
| 5964 | 6006 | try func.genSetMem( |
| 5965 | 6007 | .{ .frame = frame_index }, |
| 5966 | @intCast(ptr_ty.abiSize(zcu)), | |
| 6008 | @intCast(ptr_ty.abiSize(pt)), | |
| 5967 | 6009 | Type.usize, |
| 5968 | 6010 | .{ .immediate = array_len }, |
| 5969 | 6011 | ); |
| ... | ... | @@ -6015,7 +6057,8 @@ fn airAtomicStore(func: *Func, inst: Air.Inst.Index, order: std.builtin.AtomicOr |
| 6015 | 6057 | } |
| 6016 | 6058 | |
| 6017 | 6059 | fn airMemset(func: *Func, inst: Air.Inst.Index, safety: bool) !void { |
| 6018 | const zcu = func.bin_file.comp.module.?; | |
| 6060 | const pt = func.pt; | |
| 6061 | const zcu = pt.zcu; | |
| 6019 | 6062 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6020 | 6063 | |
| 6021 | 6064 | result: { |
| ... | ... | @@ -6037,7 +6080,7 @@ fn airMemset(func: *Func, inst: Air.Inst.Index, safety: bool) !void { |
| 6037 | 6080 | }; |
| 6038 | 6081 | defer if (src_val_lock) |lock| func.register_manager.unlockReg(lock); |
| 6039 | 6082 | |
| 6040 | const elem_abi_size: u31 = @intCast(elem_ty.abiSize(zcu)); | |
| 6083 | const elem_abi_size: u31 = @intCast(elem_ty.abiSize(pt)); | |
| 6041 | 6084 | |
| 6042 | 6085 | if (elem_abi_size == 1) { |
| 6043 | 6086 | const ptr: MCValue = switch (dst_ptr_ty.ptrSize(zcu)) { |
| ... | ... | @@ -6068,7 +6111,7 @@ fn airMemset(func: *Func, inst: Air.Inst.Index, safety: bool) !void { |
| 6068 | 6111 | switch (dst_ptr_ty.ptrSize(zcu)) { |
| 6069 | 6112 | .Slice => return func.fail("TODO: airMemset Slices", .{}), |
| 6070 | 6113 | .One => { |
| 6071 | const elem_ptr_ty = try zcu.singleMutPtrType(elem_ty); | |
| 6114 | const elem_ptr_ty = try pt.singleMutPtrType(elem_ty); | |
| 6072 | 6115 | |
| 6073 | 6116 | const len = dst_ptr_ty.childType(zcu).arrayLen(zcu); |
| 6074 | 6117 | |
| ... | ... | @@ -6110,7 +6153,8 @@ fn airTagName(func: *Func, inst: Air.Inst.Index) !void { |
| 6110 | 6153 | } |
| 6111 | 6154 | |
| 6112 | 6155 | fn airErrorName(func: *Func, inst: Air.Inst.Index) !void { |
| 6113 | const zcu = func.bin_file.comp.module.?; | |
| 6156 | const pt = func.pt; | |
| 6157 | const zcu = pt.zcu; | |
| 6114 | 6158 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 6115 | 6159 | |
| 6116 | 6160 | const err_ty = func.typeOf(un_op); |
| ... | ... | @@ -6126,7 +6170,7 @@ fn airErrorName(func: *Func, inst: Air.Inst.Index) !void { |
| 6126 | 6170 | // this is now the base address of the error name table |
| 6127 | 6171 | const lazy_sym = link.File.LazySymbol.initDecl(.const_data, null, zcu); |
| 6128 | 6172 | if (func.bin_file.cast(link.File.Elf)) |elf_file| { |
| 6129 | const sym_index = elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, lazy_sym) catch |err| | |
| 6173 | const sym_index = elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, pt, lazy_sym) catch |err| | |
| 6130 | 6174 | return func.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 6131 | 6175 | const sym = elf_file.symbol(sym_index); |
| 6132 | 6176 | try func.genSetReg(Type.usize, addr_reg, .{ .load_symbol = .{ .sym = sym.esym_index } }); |
| ... | ... | @@ -6239,7 +6283,8 @@ fn airReduce(func: *Func, inst: Air.Inst.Index) !void { |
| 6239 | 6283 | } |
| 6240 | 6284 | |
| 6241 | 6285 | fn airAggregateInit(func: *Func, inst: Air.Inst.Index) !void { |
| 6242 | const zcu = func.bin_file.comp.module.?; | |
| 6286 | const pt = func.pt; | |
| 6287 | const zcu = pt.zcu; | |
| 6243 | 6288 | const result_ty = func.typeOfIndex(inst); |
| 6244 | 6289 | const len: usize = @intCast(result_ty.arrayLen(zcu)); |
| 6245 | 6290 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| ... | ... | @@ -6248,21 +6293,21 @@ fn airAggregateInit(func: *Func, inst: Air.Inst.Index) !void { |
| 6248 | 6293 | const result: MCValue = result: { |
| 6249 | 6294 | switch (result_ty.zigTypeTag(zcu)) { |
| 6250 | 6295 | .Struct => { |
| 6251 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(result_ty, zcu)); | |
| 6296 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(result_ty, pt)); | |
| 6252 | 6297 | if (result_ty.containerLayout(zcu) == .@"packed") { |
| 6253 | 6298 | const struct_obj = zcu.typeToStruct(result_ty).?; |
| 6254 | 6299 | try func.genInlineMemset( |
| 6255 | 6300 | .{ .lea_frame = .{ .index = frame_index } }, |
| 6256 | 6301 | .{ .immediate = 0 }, |
| 6257 | .{ .immediate = result_ty.abiSize(zcu) }, | |
| 6302 | .{ .immediate = result_ty.abiSize(pt) }, | |
| 6258 | 6303 | ); |
| 6259 | 6304 | |
| 6260 | 6305 | for (elements, 0..) |elem, elem_i_usize| { |
| 6261 | 6306 | const elem_i: u32 = @intCast(elem_i_usize); |
| 6262 | if ((try result_ty.structFieldValueComptime(zcu, elem_i)) != null) continue; | |
| 6307 | if ((try result_ty.structFieldValueComptime(pt, elem_i)) != null) continue; | |
| 6263 | 6308 | |
| 6264 | 6309 | const elem_ty = result_ty.structFieldType(elem_i, zcu); |
| 6265 | const elem_bit_size: u32 = @intCast(elem_ty.bitSize(zcu)); | |
| 6310 | const elem_bit_size: u32 = @intCast(elem_ty.bitSize(pt)); | |
| 6266 | 6311 | if (elem_bit_size > 64) { |
| 6267 | 6312 | return func.fail( |
| 6268 | 6313 | "TODO airAggregateInit implement packed structs with large fields", |
| ... | ... | @@ -6270,9 +6315,9 @@ fn airAggregateInit(func: *Func, inst: Air.Inst.Index) !void { |
| 6270 | 6315 | ); |
| 6271 | 6316 | } |
| 6272 | 6317 | |
| 6273 | const elem_abi_size: u32 = @intCast(elem_ty.abiSize(zcu)); | |
| 6318 | const elem_abi_size: u32 = @intCast(elem_ty.abiSize(pt)); | |
| 6274 | 6319 | const elem_abi_bits = elem_abi_size * 8; |
| 6275 | const elem_off = zcu.structPackedFieldBitOffset(struct_obj, elem_i); | |
| 6320 | const elem_off = pt.structPackedFieldBitOffset(struct_obj, elem_i); | |
| 6276 | 6321 | const elem_byte_off: i32 = @intCast(elem_off / elem_abi_bits * elem_abi_size); |
| 6277 | 6322 | const elem_bit_off = elem_off % elem_abi_bits; |
| 6278 | 6323 | const elem_mcv = try func.resolveInst(elem); |
| ... | ... | @@ -6293,10 +6338,10 @@ fn airAggregateInit(func: *Func, inst: Air.Inst.Index) !void { |
| 6293 | 6338 | return func.fail("TODO: airAggregateInit packed structs", .{}); |
| 6294 | 6339 | } |
| 6295 | 6340 | } else for (elements, 0..) |elem, elem_i| { |
| 6296 | if ((try result_ty.structFieldValueComptime(zcu, elem_i)) != null) continue; | |
| 6341 | if ((try result_ty.structFieldValueComptime(pt, elem_i)) != null) continue; | |
| 6297 | 6342 | |
| 6298 | 6343 | const elem_ty = result_ty.structFieldType(elem_i, zcu); |
| 6299 | const elem_off: i32 = @intCast(result_ty.structFieldOffset(elem_i, zcu)); | |
| 6344 | const elem_off: i32 = @intCast(result_ty.structFieldOffset(elem_i, pt)); | |
| 6300 | 6345 | const elem_mcv = try func.resolveInst(elem); |
| 6301 | 6346 | try func.genSetMem(.{ .frame = frame_index }, elem_off, elem_ty, elem_mcv); |
| 6302 | 6347 | } |
| ... | ... | @@ -6304,8 +6349,8 @@ fn airAggregateInit(func: *Func, inst: Air.Inst.Index) !void { |
| 6304 | 6349 | }, |
| 6305 | 6350 | .Array => { |
| 6306 | 6351 | const elem_ty = result_ty.childType(zcu); |
| 6307 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(result_ty, zcu)); | |
| 6308 | const elem_size: u32 = @intCast(elem_ty.abiSize(zcu)); | |
| 6352 | const frame_index = try func.allocFrameIndex(FrameAlloc.initSpill(result_ty, pt)); | |
| 6353 | const elem_size: u32 = @intCast(elem_ty.abiSize(pt)); | |
| 6309 | 6354 | |
| 6310 | 6355 | for (elements, 0..) |elem, elem_i| { |
| 6311 | 6356 | const elem_mcv = try func.resolveInst(elem); |
| ... | ... | @@ -6325,7 +6370,7 @@ fn airAggregateInit(func: *Func, inst: Air.Inst.Index) !void { |
| 6325 | 6370 | ); |
| 6326 | 6371 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| 6327 | 6372 | }, |
| 6328 | else => return func.fail("TODO: airAggregate {}", .{result_ty.fmt(zcu)}), | |
| 6373 | else => return func.fail("TODO: airAggregate {}", .{result_ty.fmt(pt)}), | |
| 6329 | 6374 | } |
| 6330 | 6375 | }; |
| 6331 | 6376 | |
| ... | ... | @@ -6364,11 +6409,11 @@ fn airMulAdd(func: *Func, inst: Air.Inst.Index) !void { |
| 6364 | 6409 | } |
| 6365 | 6410 | |
| 6366 | 6411 | fn resolveInst(func: *Func, ref: Air.Inst.Ref) InnerError!MCValue { |
| 6367 | const zcu = func.bin_file.comp.module.?; | |
| 6412 | const pt = func.pt; | |
| 6368 | 6413 | |
| 6369 | 6414 | // If the type has no codegen bits, no need to store it. |
| 6370 | 6415 | const inst_ty = func.typeOf(ref); |
| 6371 | if (!inst_ty.hasRuntimeBits(zcu)) | |
| 6416 | if (!inst_ty.hasRuntimeBits(pt)) | |
| 6372 | 6417 | return .none; |
| 6373 | 6418 | |
| 6374 | 6419 | const mcv = if (ref.toIndex()) |inst| mcv: { |
| ... | ... | @@ -6394,9 +6439,11 @@ fn getResolvedInstValue(func: *Func, inst: Air.Inst.Index) *InstTracking { |
| 6394 | 6439 | } |
| 6395 | 6440 | |
| 6396 | 6441 | fn genTypedValue(func: *Func, val: Value) InnerError!MCValue { |
| 6397 | const zcu = func.bin_file.comp.module.?; | |
| 6442 | const pt = func.pt; | |
| 6443 | const zcu = pt.zcu; | |
| 6398 | 6444 | const result = try codegen.genTypedValue( |
| 6399 | 6445 | func.bin_file, |
| 6446 | pt, | |
| 6400 | 6447 | func.src_loc, |
| 6401 | 6448 | val, |
| 6402 | 6449 | zcu.funcOwnerDeclIndex(func.func_index), |
| ... | ... | @@ -6438,7 +6485,8 @@ fn resolveCallingConventionValues( |
| 6438 | 6485 | fn_info: InternPool.Key.FuncType, |
| 6439 | 6486 | var_args: []const Type, |
| 6440 | 6487 | ) !CallMCValues { |
| 6441 | const zcu = func.bin_file.comp.module.?; | |
| 6488 | const pt = func.pt; | |
| 6489 | const zcu = pt.zcu; | |
| 6442 | 6490 | const ip = &zcu.intern_pool; |
| 6443 | 6491 | |
| 6444 | 6492 | const param_types = try func.gpa.alloc(Type, fn_info.param_types.len + var_args.len); |
| ... | ... | @@ -6481,14 +6529,14 @@ fn resolveCallingConventionValues( |
| 6481 | 6529 | // Return values |
| 6482 | 6530 | if (ret_ty.zigTypeTag(zcu) == .NoReturn) { |
| 6483 | 6531 | result.return_value = InstTracking.init(.unreach); |
| 6484 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 6532 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 6485 | 6533 | result.return_value = InstTracking.init(.none); |
| 6486 | 6534 | } else { |
| 6487 | 6535 | var ret_tracking: [2]InstTracking = undefined; |
| 6488 | 6536 | var ret_tracking_i: usize = 0; |
| 6489 | 6537 | var ret_float_reg_i: usize = 0; |
| 6490 | 6538 | |
| 6491 | const classes = mem.sliceTo(&abi.classifySystem(ret_ty, zcu), .none); | |
| 6539 | const classes = mem.sliceTo(&abi.classifySystem(ret_ty, pt), .none); | |
| 6492 | 6540 | |
| 6493 | 6541 | for (classes) |class| switch (class) { |
| 6494 | 6542 | .integer => { |
| ... | ... | @@ -6521,7 +6569,7 @@ fn resolveCallingConventionValues( |
| 6521 | 6569 | }; |
| 6522 | 6570 | |
| 6523 | 6571 | result.return_value = switch (ret_tracking_i) { |
| 6524 | else => return func.fail("ty {} took {} tracking return indices", .{ ret_ty.fmt(zcu), ret_tracking_i }), | |
| 6572 | else => return func.fail("ty {} took {} tracking return indices", .{ ret_ty.fmt(pt), ret_tracking_i }), | |
| 6525 | 6573 | 1 => ret_tracking[0], |
| 6526 | 6574 | 2 => InstTracking.init(.{ .register_pair = .{ |
| 6527 | 6575 | ret_tracking[0].short.register, ret_tracking[1].short.register, |
| ... | ... | @@ -6532,7 +6580,7 @@ fn resolveCallingConventionValues( |
| 6532 | 6580 | var param_float_reg_i: usize = 0; |
| 6533 | 6581 | |
| 6534 | 6582 | for (param_types, result.args) |ty, *arg| { |
| 6535 | if (!ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 6583 | if (!ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 6536 | 6584 | assert(cc == .Unspecified); |
| 6537 | 6585 | arg.* = .none; |
| 6538 | 6586 | continue; |
| ... | ... | @@ -6541,7 +6589,7 @@ fn resolveCallingConventionValues( |
| 6541 | 6589 | var arg_mcv: [2]MCValue = undefined; |
| 6542 | 6590 | var arg_mcv_i: usize = 0; |
| 6543 | 6591 | |
| 6544 | const classes = mem.sliceTo(&abi.classifySystem(ty, zcu), .none); | |
| 6592 | const classes = mem.sliceTo(&abi.classifySystem(ty, pt), .none); | |
| 6545 | 6593 | |
| 6546 | 6594 | for (classes) |class| switch (class) { |
| 6547 | 6595 | .integer => { |
| ... | ... | @@ -6576,7 +6624,7 @@ fn resolveCallingConventionValues( |
| 6576 | 6624 | else => return func.fail("TODO: C calling convention arg class {}", .{class}), |
| 6577 | 6625 | } else { |
| 6578 | 6626 | arg.* = switch (arg_mcv_i) { |
| 6579 | else => return func.fail("ty {} took {} tracking arg indices", .{ ty.fmt(zcu), arg_mcv_i }), | |
| 6627 | else => return func.fail("ty {} took {} tracking arg indices", .{ ty.fmt(pt), arg_mcv_i }), | |
| 6580 | 6628 | 1 => arg_mcv[0], |
| 6581 | 6629 | 2 => .{ .register_pair = .{ arg_mcv[0].register, arg_mcv[1].register } }, |
| 6582 | 6630 | }; |
| ... | ... | @@ -6621,12 +6669,14 @@ fn parseRegName(name: []const u8) ?Register { |
| 6621 | 6669 | } |
| 6622 | 6670 | |
| 6623 | 6671 | fn typeOf(func: *Func, inst: Air.Inst.Ref) Type { |
| 6624 | const zcu = func.bin_file.comp.module.?; | |
| 6672 | const pt = func.pt; | |
| 6673 | const zcu = pt.zcu; | |
| 6625 | 6674 | return func.air.typeOf(inst, &zcu.intern_pool); |
| 6626 | 6675 | } |
| 6627 | 6676 | |
| 6628 | 6677 | fn typeOfIndex(func: *Func, inst: Air.Inst.Index) Type { |
| 6629 | const zcu = func.bin_file.comp.module.?; | |
| 6678 | const pt = func.pt; | |
| 6679 | const zcu = pt.zcu; | |
| 6630 | 6680 | return func.air.typeOfIndex(inst, &zcu.intern_pool); |
| 6631 | 6681 | } |
| 6632 | 6682 | |
| ... | ... | @@ -6634,40 +6684,41 @@ fn hasFeature(func: *Func, feature: Target.riscv.Feature) bool { |
| 6634 | 6684 | return Target.riscv.featureSetHas(func.target.cpu.features, feature); |
| 6635 | 6685 | } |
| 6636 | 6686 | |
| 6637 | pub fn errUnionPayloadOffset(payload_ty: Type, zcu: *Zcu) u64 { | |
| 6638 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) return 0; | |
| 6639 | const payload_align = payload_ty.abiAlignment(zcu); | |
| 6640 | const error_align = Type.anyerror.abiAlignment(zcu); | |
| 6641 | if (payload_align.compare(.gte, error_align) or !payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 6687 | pub fn errUnionPayloadOffset(payload_ty: Type, pt: Zcu.PerThread) u64 { | |
| 6688 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) return 0; | |
| 6689 | const payload_align = payload_ty.abiAlignment(pt); | |
| 6690 | const error_align = Type.anyerror.abiAlignment(pt); | |
| 6691 | if (payload_align.compare(.gte, error_align) or !payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 6642 | 6692 | return 0; |
| 6643 | 6693 | } else { |
| 6644 | return payload_align.forward(Type.anyerror.abiSize(zcu)); | |
| 6694 | return payload_align.forward(Type.anyerror.abiSize(pt)); | |
| 6645 | 6695 | } |
| 6646 | 6696 | } |
| 6647 | 6697 | |
| 6648 | pub fn errUnionErrorOffset(payload_ty: Type, zcu: *Zcu) u64 { | |
| 6649 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) return 0; | |
| 6650 | const payload_align = payload_ty.abiAlignment(zcu); | |
| 6651 | const error_align = Type.anyerror.abiAlignment(zcu); | |
| 6652 | if (payload_align.compare(.gte, error_align) and payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 6653 | return error_align.forward(payload_ty.abiSize(zcu)); | |
| 6698 | pub fn errUnionErrorOffset(payload_ty: Type, pt: Zcu.PerThread) u64 { | |
| 6699 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) return 0; | |
| 6700 | const payload_align = payload_ty.abiAlignment(pt); | |
| 6701 | const error_align = Type.anyerror.abiAlignment(pt); | |
| 6702 | if (payload_align.compare(.gte, error_align) and payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 6703 | return error_align.forward(payload_ty.abiSize(pt)); | |
| 6654 | 6704 | } else { |
| 6655 | 6705 | return 0; |
| 6656 | 6706 | } |
| 6657 | 6707 | } |
| 6658 | 6708 | |
| 6659 | 6709 | fn promoteInt(func: *Func, ty: Type) Type { |
| 6660 | const mod = func.bin_file.comp.module.?; | |
| 6710 | const pt = func.pt; | |
| 6711 | const zcu = pt.zcu; | |
| 6661 | 6712 | const int_info: InternPool.Key.IntType = switch (ty.toIntern()) { |
| 6662 | 6713 | .bool_type => .{ .signedness = .unsigned, .bits = 1 }, |
| 6663 | else => if (ty.isAbiInt(mod)) ty.intInfo(mod) else return ty, | |
| 6714 | else => if (ty.isAbiInt(zcu)) ty.intInfo(zcu) else return ty, | |
| 6664 | 6715 | }; |
| 6665 | 6716 | for ([_]Type{ |
| 6666 | 6717 | Type.c_int, Type.c_uint, |
| 6667 | 6718 | Type.c_long, Type.c_ulong, |
| 6668 | 6719 | Type.c_longlong, Type.c_ulonglong, |
| 6669 | 6720 | }) |promote_ty| { |
| 6670 | const promote_info = promote_ty.intInfo(mod); | |
| 6721 | const promote_info = promote_ty.intInfo(zcu); | |
| 6671 | 6722 | if (int_info.signedness == .signed and promote_info.signedness == .unsigned) continue; |
| 6672 | 6723 | if (int_info.bits + @intFromBool(int_info.signedness == .unsigned and |
| 6673 | 6724 | promote_info.signedness == .signed) <= promote_info.bits) return promote_ty; |
src/arch/riscv64/Emit.zig+3-2| ... | ... | @@ -1,5 +1,6 @@ |
| 1 | 1 | //! This file contains the functionality for emitting RISC-V MIR as machine code |
| 2 | 2 | |
| 3 | bin_file: *link.File, | |
| 3 | 4 | lower: Lower, |
| 4 | 5 | debug_output: DebugInfoOutput, |
| 5 | 6 | code: *std.ArrayList(u8), |
| ... | ... | @@ -48,7 +49,7 @@ pub fn emitMir(emit: *Emit) Error!void { |
| 48 | 49 | .Lib => emit.lower.link_mode == .static, |
| 49 | 50 | }; |
| 50 | 51 | |
| 51 | if (emit.lower.bin_file.cast(link.File.Elf)) |elf_file| { | |
| 52 | if (emit.bin_file.cast(link.File.Elf)) |elf_file| { | |
| 52 | 53 | const atom_ptr = elf_file.symbol(symbol.atom_index).atom(elf_file).?; |
| 53 | 54 | const sym_index = elf_file.zigObjectPtr().?.symbol(symbol.sym_index); |
| 54 | 55 | const sym = elf_file.symbol(sym_index); |
| ... | ... | @@ -77,7 +78,7 @@ pub fn emitMir(emit: *Emit) Error!void { |
| 77 | 78 | } else return emit.fail("TODO: load_symbol_reloc non-ELF", .{}); |
| 78 | 79 | }, |
| 79 | 80 | .call_extern_fn_reloc => |symbol| { |
| 80 | if (emit.lower.bin_file.cast(link.File.Elf)) |elf_file| { | |
| 81 | if (emit.bin_file.cast(link.File.Elf)) |elf_file| { | |
| 81 | 82 | const atom_ptr = elf_file.symbol(symbol.atom_index).atom(elf_file).?; |
| 82 | 83 | |
| 83 | 84 | const r_type: u32 = @intFromEnum(std.elf.R_RISCV.CALL_PLT); |
src/arch/riscv64/Lower.zig+6-6| ... | ... | @@ -1,6 +1,6 @@ |
| 1 | 1 | //! This file contains the functionality for lowering RISC-V MIR to Instructions |
| 2 | 2 | |
| 3 | bin_file: *link.File, | |
| 3 | pt: Zcu.PerThread, | |
| 4 | 4 | output_mode: std.builtin.OutputMode, |
| 5 | 5 | link_mode: std.builtin.LinkMode, |
| 6 | 6 | pic: bool, |
| ... | ... | @@ -44,7 +44,7 @@ pub fn lowerMir(lower: *Lower, index: Mir.Inst.Index) Error!struct { |
| 44 | 44 | insts: []const Instruction, |
| 45 | 45 | relocs: []const Reloc, |
| 46 | 46 | } { |
| 47 | const zcu = lower.bin_file.comp.module.?; | |
| 47 | const pt = lower.pt; | |
| 48 | 48 | |
| 49 | 49 | lower.result_insts = undefined; |
| 50 | 50 | lower.result_relocs = undefined; |
| ... | ... | @@ -243,11 +243,11 @@ pub fn lowerMir(lower: *Lower, index: Mir.Inst.Index) Error!struct { |
| 243 | 243 | |
| 244 | 244 | const class = rs1.class(); |
| 245 | 245 | const ty = compare.ty; |
| 246 | const size = std.math.ceilPowerOfTwo(u64, ty.bitSize(zcu)) catch { | |
| 247 | return lower.fail("pseudo_compare size {}", .{ty.bitSize(zcu)}); | |
| 246 | const size = std.math.ceilPowerOfTwo(u64, ty.bitSize(pt)) catch { | |
| 247 | return lower.fail("pseudo_compare size {}", .{ty.bitSize(pt)}); | |
| 248 | 248 | }; |
| 249 | 249 | |
| 250 | const is_unsigned = ty.isUnsignedInt(zcu); | |
| 250 | const is_unsigned = ty.isUnsignedInt(pt.zcu); | |
| 251 | 251 | |
| 252 | 252 | const less_than: Encoding.Mnemonic = if (is_unsigned) .sltu else .slt; |
| 253 | 253 | |
| ... | ... | @@ -502,7 +502,7 @@ pub fn fail(lower: *Lower, comptime format: []const u8, args: anytype) Error { |
| 502 | 502 | } |
| 503 | 503 | |
| 504 | 504 | fn hasFeature(lower: *Lower, feature: std.Target.riscv.Feature) bool { |
| 505 | const target = lower.bin_file.comp.module.?.getTarget(); | |
| 505 | const target = lower.pt.zcu.getTarget(); | |
| 506 | 506 | const features = target.cpu.features; |
| 507 | 507 | return std.Target.riscv.featureSetHas(features, feature); |
| 508 | 508 | } |
src/arch/riscv64/abi.zig+28-27| ... | ... | @@ -9,15 +9,15 @@ const assert = std.debug.assert; |
| 9 | 9 | |
| 10 | 10 | pub const Class = enum { memory, byval, integer, double_integer, fields }; |
| 11 | 11 | |
| 12 | pub fn classifyType(ty: Type, mod: *Zcu) Class { | |
| 13 | const target = mod.getTarget(); | |
| 14 | std.debug.assert(ty.hasRuntimeBitsIgnoreComptime(mod)); | |
| 12 | pub fn classifyType(ty: Type, pt: Zcu.PerThread) Class { | |
| 13 | const target = pt.zcu.getTarget(); | |
| 14 | std.debug.assert(ty.hasRuntimeBitsIgnoreComptime(pt)); | |
| 15 | 15 | |
| 16 | 16 | const max_byval_size = target.ptrBitWidth() * 2; |
| 17 | switch (ty.zigTypeTag(mod)) { | |
| 17 | switch (ty.zigTypeTag(pt.zcu)) { | |
| 18 | 18 | .Struct => { |
| 19 | const bit_size = ty.bitSize(mod); | |
| 20 | if (ty.containerLayout(mod) == .@"packed") { | |
| 19 | const bit_size = ty.bitSize(pt); | |
| 20 | if (ty.containerLayout(pt.zcu) == .@"packed") { | |
| 21 | 21 | if (bit_size > max_byval_size) return .memory; |
| 22 | 22 | return .byval; |
| 23 | 23 | } |
| ... | ... | @@ -25,12 +25,12 @@ pub fn classifyType(ty: Type, mod: *Zcu) Class { |
| 25 | 25 | if (std.Target.riscv.featureSetHas(target.cpu.features, .d)) fields: { |
| 26 | 26 | var any_fp = false; |
| 27 | 27 | var field_count: usize = 0; |
| 28 | for (0..ty.structFieldCount(mod)) |field_index| { | |
| 29 | const field_ty = ty.structFieldType(field_index, mod); | |
| 30 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; | |
| 28 | for (0..ty.structFieldCount(pt.zcu)) |field_index| { | |
| 29 | const field_ty = ty.structFieldType(field_index, pt.zcu); | |
| 30 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 31 | 31 | if (field_ty.isRuntimeFloat()) |
| 32 | 32 | any_fp = true |
| 33 | else if (!field_ty.isAbiInt(mod)) | |
| 33 | else if (!field_ty.isAbiInt(pt.zcu)) | |
| 34 | 34 | break :fields; |
| 35 | 35 | field_count += 1; |
| 36 | 36 | if (field_count > 2) break :fields; |
| ... | ... | @@ -45,8 +45,8 @@ pub fn classifyType(ty: Type, mod: *Zcu) Class { |
| 45 | 45 | return .integer; |
| 46 | 46 | }, |
| 47 | 47 | .Union => { |
| 48 | const bit_size = ty.bitSize(mod); | |
| 49 | if (ty.containerLayout(mod) == .@"packed") { | |
| 48 | const bit_size = ty.bitSize(pt); | |
| 49 | if (ty.containerLayout(pt.zcu) == .@"packed") { | |
| 50 | 50 | if (bit_size > max_byval_size) return .memory; |
| 51 | 51 | return .byval; |
| 52 | 52 | } |
| ... | ... | @@ -58,21 +58,21 @@ pub fn classifyType(ty: Type, mod: *Zcu) Class { |
| 58 | 58 | .Bool => return .integer, |
| 59 | 59 | .Float => return .byval, |
| 60 | 60 | .Int, .Enum, .ErrorSet => { |
| 61 | const bit_size = ty.bitSize(mod); | |
| 61 | const bit_size = ty.bitSize(pt); | |
| 62 | 62 | if (bit_size > max_byval_size) return .memory; |
| 63 | 63 | return .byval; |
| 64 | 64 | }, |
| 65 | 65 | .Vector => { |
| 66 | const bit_size = ty.bitSize(mod); | |
| 66 | const bit_size = ty.bitSize(pt); | |
| 67 | 67 | if (bit_size > max_byval_size) return .memory; |
| 68 | 68 | return .integer; |
| 69 | 69 | }, |
| 70 | 70 | .Optional => { |
| 71 | std.debug.assert(ty.isPtrLikeOptional(mod)); | |
| 71 | std.debug.assert(ty.isPtrLikeOptional(pt.zcu)); | |
| 72 | 72 | return .byval; |
| 73 | 73 | }, |
| 74 | 74 | .Pointer => { |
| 75 | std.debug.assert(!ty.isSlice(mod)); | |
| 75 | std.debug.assert(!ty.isSlice(pt.zcu)); | |
| 76 | 76 | return .byval; |
| 77 | 77 | }, |
| 78 | 78 | .ErrorUnion, |
| ... | ... | @@ -97,18 +97,19 @@ pub const SystemClass = enum { integer, float, memory, none }; |
| 97 | 97 | |
| 98 | 98 | /// There are a maximum of 8 possible return slots. Returned values are in |
| 99 | 99 | /// the beginning of the array; unused slots are filled with .none. |
| 100 | pub fn classifySystem(ty: Type, zcu: *Zcu) [8]SystemClass { | |
| 100 | pub fn classifySystem(ty: Type, pt: Zcu.PerThread) [8]SystemClass { | |
| 101 | const zcu = pt.zcu; | |
| 101 | 102 | var result = [1]SystemClass{.none} ** 8; |
| 102 | 103 | const memory_class = [_]SystemClass{ |
| 103 | 104 | .memory, .none, .none, .none, |
| 104 | 105 | .none, .none, .none, .none, |
| 105 | 106 | }; |
| 106 | switch (ty.zigTypeTag(zcu)) { | |
| 107 | switch (ty.zigTypeTag(pt.zcu)) { | |
| 107 | 108 | .Bool, .Void, .NoReturn => { |
| 108 | 109 | result[0] = .integer; |
| 109 | 110 | return result; |
| 110 | 111 | }, |
| 111 | .Pointer => switch (ty.ptrSize(zcu)) { | |
| 112 | .Pointer => switch (ty.ptrSize(pt.zcu)) { | |
| 112 | 113 | .Slice => { |
| 113 | 114 | result[0] = .integer; |
| 114 | 115 | result[1] = .integer; |
| ... | ... | @@ -120,17 +121,17 @@ pub fn classifySystem(ty: Type, zcu: *Zcu) [8]SystemClass { |
| 120 | 121 | }, |
| 121 | 122 | }, |
| 122 | 123 | .Optional => { |
| 123 | if (ty.isPtrLikeOptional(zcu)) { | |
| 124 | if (ty.isPtrLikeOptional(pt.zcu)) { | |
| 124 | 125 | result[0] = .integer; |
| 125 | 126 | return result; |
| 126 | 127 | } |
| 127 | 128 | result[0] = .integer; |
| 128 | if (ty.optionalChild(zcu).abiSize(zcu) == 0) return result; | |
| 129 | if (ty.optionalChild(zcu).abiSize(pt) == 0) return result; | |
| 129 | 130 | result[1] = .integer; |
| 130 | 131 | return result; |
| 131 | 132 | }, |
| 132 | 133 | .Int, .Enum, .ErrorSet => { |
| 133 | const int_bits = ty.intInfo(zcu).bits; | |
| 134 | const int_bits = ty.intInfo(pt.zcu).bits; | |
| 134 | 135 | if (int_bits <= 64) { |
| 135 | 136 | result[0] = .integer; |
| 136 | 137 | return result; |
| ... | ... | @@ -155,8 +156,8 @@ pub fn classifySystem(ty: Type, zcu: *Zcu) [8]SystemClass { |
| 155 | 156 | unreachable; // support split float args |
| 156 | 157 | }, |
| 157 | 158 | .ErrorUnion => { |
| 158 | const payload_ty = ty.errorUnionPayload(zcu); | |
| 159 | const payload_bits = payload_ty.bitSize(zcu); | |
| 159 | const payload_ty = ty.errorUnionPayload(pt.zcu); | |
| 160 | const payload_bits = payload_ty.bitSize(pt); | |
| 160 | 161 | |
| 161 | 162 | // the error union itself |
| 162 | 163 | result[0] = .integer; |
| ... | ... | @@ -167,8 +168,8 @@ pub fn classifySystem(ty: Type, zcu: *Zcu) [8]SystemClass { |
| 167 | 168 | return memory_class; |
| 168 | 169 | }, |
| 169 | 170 | .Struct => { |
| 170 | const layout = ty.containerLayout(zcu); | |
| 171 | const ty_size = ty.abiSize(zcu); | |
| 171 | const layout = ty.containerLayout(pt.zcu); | |
| 172 | const ty_size = ty.abiSize(pt); | |
| 172 | 173 | |
| 173 | 174 | if (layout == .@"packed") { |
| 174 | 175 | assert(ty_size <= 16); |
| ... | ... | @@ -180,7 +181,7 @@ pub fn classifySystem(ty: Type, zcu: *Zcu) [8]SystemClass { |
| 180 | 181 | return memory_class; |
| 181 | 182 | }, |
| 182 | 183 | .Array => { |
| 183 | const ty_size = ty.abiSize(zcu); | |
| 184 | const ty_size = ty.abiSize(pt); | |
| 184 | 185 | if (ty_size <= 8) { |
| 185 | 186 | result[0] = .integer; |
| 186 | 187 | return result; |
src/arch/sparc64/CodeGen.zig+125-95| ... | ... | @@ -11,11 +11,9 @@ const Allocator = mem.Allocator; |
| 11 | 11 | const builtin = @import("builtin"); |
| 12 | 12 | const link = @import("../../link.zig"); |
| 13 | 13 | const Zcu = @import("../../Zcu.zig"); |
| 14 | /// Deprecated. | |
| 15 | const Module = Zcu; | |
| 16 | 14 | const InternPool = @import("../../InternPool.zig"); |
| 17 | 15 | const Value = @import("../../Value.zig"); |
| 18 | const ErrorMsg = Module.ErrorMsg; | |
| 16 | const ErrorMsg = Zcu.ErrorMsg; | |
| 19 | 17 | const codegen = @import("../../codegen.zig"); |
| 20 | 18 | const Air = @import("../../Air.zig"); |
| 21 | 19 | const Mir = @import("Mir.zig"); |
| ... | ... | @@ -52,6 +50,7 @@ const RegisterView = enum(u1) { |
| 52 | 50 | }; |
| 53 | 51 | |
| 54 | 52 | gpa: Allocator, |
| 53 | pt: Zcu.PerThread, | |
| 55 | 54 | air: Air, |
| 56 | 55 | liveness: Liveness, |
| 57 | 56 | bin_file: *link.File, |
| ... | ... | @@ -64,7 +63,7 @@ args: []MCValue, |
| 64 | 63 | ret_mcv: MCValue, |
| 65 | 64 | fn_type: Type, |
| 66 | 65 | arg_index: usize, |
| 67 | src_loc: Module.LazySrcLoc, | |
| 66 | src_loc: Zcu.LazySrcLoc, | |
| 68 | 67 | stack_align: Alignment, |
| 69 | 68 | |
| 70 | 69 | /// MIR Instructions |
| ... | ... | @@ -263,15 +262,16 @@ const BigTomb = struct { |
| 263 | 262 | |
| 264 | 263 | pub fn generate( |
| 265 | 264 | lf: *link.File, |
| 266 | src_loc: Module.LazySrcLoc, | |
| 265 | pt: Zcu.PerThread, | |
| 266 | src_loc: Zcu.LazySrcLoc, | |
| 267 | 267 | func_index: InternPool.Index, |
| 268 | 268 | air: Air, |
| 269 | 269 | liveness: Liveness, |
| 270 | 270 | code: *std.ArrayList(u8), |
| 271 | 271 | debug_output: DebugInfoOutput, |
| 272 | 272 | ) CodeGenError!Result { |
| 273 | const gpa = lf.comp.gpa; | |
| 274 | const zcu = lf.comp.module.?; | |
| 273 | const zcu = pt.zcu; | |
| 274 | const gpa = zcu.gpa; | |
| 275 | 275 | const func = zcu.funcInfo(func_index); |
| 276 | 276 | const fn_owner_decl = zcu.declPtr(func.owner_decl); |
| 277 | 277 | assert(fn_owner_decl.has_tv); |
| ... | ... | @@ -289,11 +289,12 @@ pub fn generate( |
| 289 | 289 | |
| 290 | 290 | var function = Self{ |
| 291 | 291 | .gpa = gpa, |
| 292 | .pt = pt, | |
| 292 | 293 | .air = air, |
| 293 | 294 | .liveness = liveness, |
| 294 | 295 | .target = target, |
| 295 | .func_index = func_index, | |
| 296 | 296 | .bin_file = lf, |
| 297 | .func_index = func_index, | |
| 297 | 298 | .code = code, |
| 298 | 299 | .debug_output = debug_output, |
| 299 | 300 | .err_msg = null, |
| ... | ... | @@ -365,7 +366,8 @@ pub fn generate( |
| 365 | 366 | } |
| 366 | 367 | |
| 367 | 368 | fn gen(self: *Self) !void { |
| 368 | const mod = self.bin_file.comp.module.?; | |
| 369 | const pt = self.pt; | |
| 370 | const mod = pt.zcu; | |
| 369 | 371 | const cc = self.fn_type.fnCallingConvention(mod); |
| 370 | 372 | if (cc != .Naked) { |
| 371 | 373 | // TODO Finish function prologue and epilogue for sparc64. |
| ... | ... | @@ -493,7 +495,8 @@ fn gen(self: *Self) !void { |
| 493 | 495 | } |
| 494 | 496 | |
| 495 | 497 | fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 496 | const mod = self.bin_file.comp.module.?; | |
| 498 | const pt = self.pt; | |
| 499 | const mod = pt.zcu; | |
| 497 | 500 | const ip = &mod.intern_pool; |
| 498 | 501 | const air_tags = self.air.instructions.items(.tag); |
| 499 | 502 | |
| ... | ... | @@ -757,7 +760,8 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 757 | 760 | const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 758 | 761 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 759 | 762 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 760 | const mod = self.bin_file.comp.module.?; | |
| 763 | const pt = self.pt; | |
| 764 | const mod = pt.zcu; | |
| 761 | 765 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 762 | 766 | const lhs = try self.resolveInst(extra.lhs); |
| 763 | 767 | const rhs = try self.resolveInst(extra.rhs); |
| ... | ... | @@ -835,7 +839,8 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 835 | 839 | } |
| 836 | 840 | |
| 837 | 841 | fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| 838 | const mod = self.bin_file.comp.module.?; | |
| 842 | const pt = self.pt; | |
| 843 | const mod = pt.zcu; | |
| 839 | 844 | const vector_ty = self.typeOfIndex(inst); |
| 840 | 845 | const len = vector_ty.vectorLen(mod); |
| 841 | 846 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| ... | ... | @@ -869,7 +874,8 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 869 | 874 | } |
| 870 | 875 | |
| 871 | 876 | fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 872 | const mod = self.bin_file.comp.module.?; | |
| 877 | const pt = self.pt; | |
| 878 | const mod = pt.zcu; | |
| 873 | 879 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 874 | 880 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 875 | 881 | const ptr_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -1006,7 +1012,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 1006 | 1012 | } |
| 1007 | 1013 | |
| 1008 | 1014 | fn airArg(self: *Self, inst: Air.Inst.Index) !void { |
| 1009 | const mod = self.bin_file.comp.module.?; | |
| 1015 | const pt = self.pt; | |
| 1010 | 1016 | const arg_index = self.arg_index; |
| 1011 | 1017 | self.arg_index += 1; |
| 1012 | 1018 | |
| ... | ... | @@ -1016,8 +1022,8 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void { |
| 1016 | 1022 | const mcv = blk: { |
| 1017 | 1023 | switch (arg) { |
| 1018 | 1024 | .stack_offset => |off| { |
| 1019 | const abi_size = math.cast(u32, ty.abiSize(mod)) orelse { | |
| 1020 | return self.fail("type '{}' too big to fit into stack frame", .{ty.fmt(mod)}); | |
| 1025 | const abi_size = math.cast(u32, ty.abiSize(pt)) orelse { | |
| 1026 | return self.fail("type '{}' too big to fit into stack frame", .{ty.fmt(pt)}); | |
| 1021 | 1027 | }; |
| 1022 | 1028 | const offset = off + abi_size; |
| 1023 | 1029 | break :blk MCValue{ .stack_offset = offset }; |
| ... | ... | @@ -1205,7 +1211,8 @@ fn airBreakpoint(self: *Self) !void { |
| 1205 | 1211 | } |
| 1206 | 1212 | |
| 1207 | 1213 | fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void { |
| 1208 | const mod = self.bin_file.comp.module.?; | |
| 1214 | const pt = self.pt; | |
| 1215 | const mod = pt.zcu; | |
| 1209 | 1216 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1210 | 1217 | |
| 1211 | 1218 | // We have hardware byteswapper in SPARCv9, don't let mainstream compilers mislead you. |
| ... | ... | @@ -1228,7 +1235,7 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void { |
| 1228 | 1235 | if (int_info.bits == 8) break :result operand; |
| 1229 | 1236 | |
| 1230 | 1237 | const abi_size = int_info.bits >> 3; |
| 1231 | const abi_align = operand_ty.abiAlignment(mod); | |
| 1238 | const abi_align = operand_ty.abiAlignment(pt); | |
| 1232 | 1239 | const opposite_endian_asi = switch (self.target.cpu.arch.endian()) { |
| 1233 | 1240 | Endian.big => ASI.asi_primary_little, |
| 1234 | 1241 | Endian.little => ASI.asi_primary, |
| ... | ... | @@ -1297,7 +1304,8 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier |
| 1297 | 1304 | const extra = self.air.extraData(Air.Call, pl_op.payload); |
| 1298 | 1305 | const args = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[extra.end .. extra.end + extra.data.args_len])); |
| 1299 | 1306 | const ty = self.typeOf(callee); |
| 1300 | const mod = self.bin_file.comp.module.?; | |
| 1307 | const pt = self.pt; | |
| 1308 | const mod = pt.zcu; | |
| 1301 | 1309 | const fn_ty = switch (ty.zigTypeTag(mod)) { |
| 1302 | 1310 | .Fn => ty, |
| 1303 | 1311 | .Pointer => ty.childType(mod), |
| ... | ... | @@ -1341,7 +1349,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallModifier |
| 1341 | 1349 | |
| 1342 | 1350 | // Due to incremental compilation, how function calls are generated depends |
| 1343 | 1351 | // on linking. |
| 1344 | if (try self.air.value(callee, mod)) |func_value| { | |
| 1352 | if (try self.air.value(callee, pt)) |func_value| { | |
| 1345 | 1353 | if (self.bin_file.tag == link.File.Elf.base_tag) { |
| 1346 | 1354 | switch (mod.intern_pool.indexToKey(func_value.ip_index)) { |
| 1347 | 1355 | .func => |func| { |
| ... | ... | @@ -1429,7 +1437,8 @@ fn airClz(self: *Self, inst: Air.Inst.Index) !void { |
| 1429 | 1437 | |
| 1430 | 1438 | fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { |
| 1431 | 1439 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 1432 | const mod = self.bin_file.comp.module.?; | |
| 1440 | const pt = self.pt; | |
| 1441 | const mod = pt.zcu; | |
| 1433 | 1442 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 1434 | 1443 | const lhs = try self.resolveInst(bin_op.lhs); |
| 1435 | 1444 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -1444,7 +1453,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { |
| 1444 | 1453 | .ErrorSet => Type.u16, |
| 1445 | 1454 | .Optional => blk: { |
| 1446 | 1455 | const payload_ty = lhs_ty.optionalChild(mod); |
| 1447 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 1456 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1448 | 1457 | break :blk Type.u1; |
| 1449 | 1458 | } else if (lhs_ty.isPtrLikeOptional(mod)) { |
| 1450 | 1459 | break :blk Type.usize; |
| ... | ... | @@ -1655,7 +1664,8 @@ fn airCtz(self: *Self, inst: Air.Inst.Index) !void { |
| 1655 | 1664 | } |
| 1656 | 1665 | |
| 1657 | 1666 | fn airDbgInlineBlock(self: *Self, inst: Air.Inst.Index) !void { |
| 1658 | const mod = self.bin_file.comp.module.?; | |
| 1667 | const pt = self.pt; | |
| 1668 | const mod = pt.zcu; | |
| 1659 | 1669 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 1660 | 1670 | const extra = self.air.extraData(Air.DbgInlineBlock, ty_pl.payload); |
| 1661 | 1671 | const func = mod.funcInfo(extra.data.func); |
| ... | ... | @@ -1753,7 +1763,8 @@ fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 1753 | 1763 | if (self.liveness.isUnused(inst)) |
| 1754 | 1764 | return self.finishAir(inst, .dead, .{ ty_op.operand, .none, .none }); |
| 1755 | 1765 | |
| 1756 | const mod = self.bin_file.comp.module.?; | |
| 1766 | const pt = self.pt; | |
| 1767 | const mod = pt.zcu; | |
| 1757 | 1768 | const operand_ty = self.typeOf(ty_op.operand); |
| 1758 | 1769 | const operand = try self.resolveInst(ty_op.operand); |
| 1759 | 1770 | const info_a = operand_ty.intInfo(mod); |
| ... | ... | @@ -1814,12 +1825,13 @@ fn airIsNonNull(self: *Self, inst: Air.Inst.Index) !void { |
| 1814 | 1825 | } |
| 1815 | 1826 | |
| 1816 | 1827 | fn airLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 1817 | const mod = self.bin_file.comp.module.?; | |
| 1828 | const pt = self.pt; | |
| 1829 | const mod = pt.zcu; | |
| 1818 | 1830 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 1819 | 1831 | const elem_ty = self.typeOfIndex(inst); |
| 1820 | const elem_size = elem_ty.abiSize(mod); | |
| 1832 | const elem_size = elem_ty.abiSize(pt); | |
| 1821 | 1833 | const result: MCValue = result: { |
| 1822 | if (!elem_ty.hasRuntimeBits(mod)) | |
| 1834 | if (!elem_ty.hasRuntimeBits(pt)) | |
| 1823 | 1835 | break :result MCValue.none; |
| 1824 | 1836 | |
| 1825 | 1837 | const ptr = try self.resolveInst(ty_op.operand); |
| ... | ... | @@ -1898,7 +1910,7 @@ fn airMod(self: *Self, inst: Air.Inst.Index) !void { |
| 1898 | 1910 | const rhs = try self.resolveInst(bin_op.rhs); |
| 1899 | 1911 | const lhs_ty = self.typeOf(bin_op.lhs); |
| 1900 | 1912 | const rhs_ty = self.typeOf(bin_op.rhs); |
| 1901 | assert(lhs_ty.eql(rhs_ty, self.bin_file.comp.module.?)); | |
| 1913 | assert(lhs_ty.eql(rhs_ty, self.pt.zcu)); | |
| 1902 | 1914 | |
| 1903 | 1915 | if (self.liveness.isUnused(inst)) |
| 1904 | 1916 | return self.finishAir(inst, .dead, .{ bin_op.lhs, bin_op.rhs, .none }); |
| ... | ... | @@ -2040,7 +2052,8 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2040 | 2052 | //const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 2041 | 2053 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2042 | 2054 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2043 | const mod = self.bin_file.comp.module.?; | |
| 2055 | const pt = self.pt; | |
| 2056 | const mod = pt.zcu; | |
| 2044 | 2057 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 2045 | 2058 | const lhs = try self.resolveInst(extra.lhs); |
| 2046 | 2059 | const rhs = try self.resolveInst(extra.rhs); |
| ... | ... | @@ -2104,7 +2117,8 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2104 | 2117 | |
| 2105 | 2118 | fn airNot(self: *Self, inst: Air.Inst.Index) !void { |
| 2106 | 2119 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2107 | const mod = self.bin_file.comp.module.?; | |
| 2120 | const pt = self.pt; | |
| 2121 | const mod = pt.zcu; | |
| 2108 | 2122 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 2109 | 2123 | const operand = try self.resolveInst(ty_op.operand); |
| 2110 | 2124 | const operand_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -2336,7 +2350,8 @@ fn airShlSat(self: *Self, inst: Air.Inst.Index) !void { |
| 2336 | 2350 | fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 2337 | 2351 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 2338 | 2352 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 2339 | const mod = self.bin_file.comp.module.?; | |
| 2353 | const pt = self.pt; | |
| 2354 | const mod = pt.zcu; | |
| 2340 | 2355 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 2341 | 2356 | const lhs = try self.resolveInst(extra.lhs); |
| 2342 | 2357 | const rhs = try self.resolveInst(extra.rhs); |
| ... | ... | @@ -2441,7 +2456,8 @@ fn airSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 2441 | 2456 | } |
| 2442 | 2457 | |
| 2443 | 2458 | fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 2444 | const mod = self.bin_file.comp.module.?; | |
| 2459 | const pt = self.pt; | |
| 2460 | const mod = pt.zcu; | |
| 2445 | 2461 | const is_volatile = false; // TODO |
| 2446 | 2462 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2447 | 2463 | |
| ... | ... | @@ -2452,7 +2468,7 @@ fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 2452 | 2468 | |
| 2453 | 2469 | const slice_ty = self.typeOf(bin_op.lhs); |
| 2454 | 2470 | const elem_ty = slice_ty.childType(mod); |
| 2455 | const elem_size = elem_ty.abiSize(mod); | |
| 2471 | const elem_size = elem_ty.abiSize(pt); | |
| 2456 | 2472 | |
| 2457 | 2473 | const slice_ptr_field_type = slice_ty.slicePtrFieldType(mod); |
| 2458 | 2474 | |
| ... | ... | @@ -2566,10 +2582,10 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 2566 | 2582 | const operand = extra.struct_operand; |
| 2567 | 2583 | const index = extra.field_index; |
| 2568 | 2584 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 2569 | const mod = self.bin_file.comp.module.?; | |
| 2585 | const pt = self.pt; | |
| 2570 | 2586 | const mcv = try self.resolveInst(operand); |
| 2571 | 2587 | const struct_ty = self.typeOf(operand); |
| 2572 | const struct_field_offset = @as(u32, @intCast(struct_ty.structFieldOffset(index, mod))); | |
| 2588 | const struct_field_offset = @as(u32, @intCast(struct_ty.structFieldOffset(index, pt))); | |
| 2573 | 2589 | |
| 2574 | 2590 | switch (mcv) { |
| 2575 | 2591 | .dead, .unreach => unreachable, |
| ... | ... | @@ -2699,13 +2715,14 @@ fn airUnionInit(self: *Self, inst: Air.Inst.Index) !void { |
| 2699 | 2715 | } |
| 2700 | 2716 | |
| 2701 | 2717 | fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void { |
| 2702 | const mod = self.bin_file.comp.module.?; | |
| 2718 | const pt = self.pt; | |
| 2719 | const mod = pt.zcu; | |
| 2703 | 2720 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2704 | 2721 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 2705 | 2722 | const error_union_ty = self.typeOf(ty_op.operand); |
| 2706 | 2723 | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 2707 | 2724 | const mcv = try self.resolveInst(ty_op.operand); |
| 2708 | if (!payload_ty.hasRuntimeBits(mod)) break :result mcv; | |
| 2725 | if (!payload_ty.hasRuntimeBits(pt)) break :result mcv; | |
| 2709 | 2726 | |
| 2710 | 2727 | return self.fail("TODO implement unwrap error union error for non-empty payloads", .{}); |
| 2711 | 2728 | }; |
| ... | ... | @@ -2713,12 +2730,13 @@ fn airUnwrapErrErr(self: *Self, inst: Air.Inst.Index) !void { |
| 2713 | 2730 | } |
| 2714 | 2731 | |
| 2715 | 2732 | fn airUnwrapErrPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 2716 | const mod = self.bin_file.comp.module.?; | |
| 2733 | const pt = self.pt; | |
| 2734 | const mod = pt.zcu; | |
| 2717 | 2735 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2718 | 2736 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 2719 | 2737 | const error_union_ty = self.typeOf(ty_op.operand); |
| 2720 | 2738 | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 2721 | if (!payload_ty.hasRuntimeBits(mod)) break :result MCValue.none; | |
| 2739 | if (!payload_ty.hasRuntimeBits(pt)) break :result MCValue.none; | |
| 2722 | 2740 | |
| 2723 | 2741 | return self.fail("TODO implement unwrap error union payload for non-empty payloads", .{}); |
| 2724 | 2742 | }; |
| ... | ... | @@ -2727,13 +2745,14 @@ fn airUnwrapErrPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 2727 | 2745 | |
| 2728 | 2746 | /// E to E!T |
| 2729 | 2747 | fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void { |
| 2730 | const mod = self.bin_file.comp.module.?; | |
| 2748 | const pt = self.pt; | |
| 2749 | const mod = pt.zcu; | |
| 2731 | 2750 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2732 | 2751 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 2733 | 2752 | const error_union_ty = ty_op.ty.toType(); |
| 2734 | 2753 | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 2735 | 2754 | const mcv = try self.resolveInst(ty_op.operand); |
| 2736 | if (!payload_ty.hasRuntimeBits(mod)) break :result mcv; | |
| 2755 | if (!payload_ty.hasRuntimeBits(pt)) break :result mcv; | |
| 2737 | 2756 | |
| 2738 | 2757 | return self.fail("TODO implement wrap errunion error for non-empty payloads", .{}); |
| 2739 | 2758 | }; |
| ... | ... | @@ -2748,13 +2767,13 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 2748 | 2767 | } |
| 2749 | 2768 | |
| 2750 | 2769 | fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { |
| 2751 | const mod = self.bin_file.comp.module.?; | |
| 2770 | const pt = self.pt; | |
| 2752 | 2771 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2753 | 2772 | const result: MCValue = if (self.liveness.isUnused(inst)) .dead else result: { |
| 2754 | 2773 | const optional_ty = self.typeOfIndex(inst); |
| 2755 | 2774 | |
| 2756 | 2775 | // Optional with a zero-bit payload type is just a boolean true |
| 2757 | if (optional_ty.abiSize(mod) == 1) | |
| 2776 | if (optional_ty.abiSize(pt) == 1) | |
| 2758 | 2777 | break :result MCValue{ .immediate = 1 }; |
| 2759 | 2778 | |
| 2760 | 2779 | return self.fail("TODO implement wrap optional for {}", .{self.target.cpu.arch}); |
| ... | ... | @@ -2788,10 +2807,11 @@ fn allocMem(self: *Self, inst: Air.Inst.Index, abi_size: u32, abi_align: Alignme |
| 2788 | 2807 | |
| 2789 | 2808 | /// Use a pointer instruction as the basis for allocating stack memory. |
| 2790 | 2809 | fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 2791 | const mod = self.bin_file.comp.module.?; | |
| 2810 | const pt = self.pt; | |
| 2811 | const mod = pt.zcu; | |
| 2792 | 2812 | const elem_ty = self.typeOfIndex(inst).childType(mod); |
| 2793 | 2813 | |
| 2794 | if (!elem_ty.hasRuntimeBits(mod)) { | |
| 2814 | if (!elem_ty.hasRuntimeBits(pt)) { | |
| 2795 | 2815 | // As this stack item will never be dereferenced at runtime, |
| 2796 | 2816 | // return the stack offset 0. Stack offset 0 will be where all |
| 2797 | 2817 | // zero-sized stack allocations live as non-zero-sized |
| ... | ... | @@ -2799,21 +2819,21 @@ fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 2799 | 2819 | return @as(u32, 0); |
| 2800 | 2820 | } |
| 2801 | 2821 | |
| 2802 | const abi_size = math.cast(u32, elem_ty.abiSize(mod)) orelse { | |
| 2803 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(mod)}); | |
| 2822 | const abi_size = math.cast(u32, elem_ty.abiSize(pt)) orelse { | |
| 2823 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(pt)}); | |
| 2804 | 2824 | }; |
| 2805 | 2825 | // TODO swap this for inst.ty.ptrAlign |
| 2806 | const abi_align = elem_ty.abiAlignment(mod); | |
| 2826 | const abi_align = elem_ty.abiAlignment(pt); | |
| 2807 | 2827 | return self.allocMem(inst, abi_size, abi_align); |
| 2808 | 2828 | } |
| 2809 | 2829 | |
| 2810 | 2830 | fn allocRegOrMem(self: *Self, inst: Air.Inst.Index, reg_ok: bool) !MCValue { |
| 2811 | const mod = self.bin_file.comp.module.?; | |
| 2831 | const pt = self.pt; | |
| 2812 | 2832 | const elem_ty = self.typeOfIndex(inst); |
| 2813 | const abi_size = math.cast(u32, elem_ty.abiSize(mod)) orelse { | |
| 2814 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(mod)}); | |
| 2833 | const abi_size = math.cast(u32, elem_ty.abiSize(pt)) orelse { | |
| 2834 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(pt)}); | |
| 2815 | 2835 | }; |
| 2816 | const abi_align = elem_ty.abiAlignment(mod); | |
| 2836 | const abi_align = elem_ty.abiAlignment(pt); | |
| 2817 | 2837 | self.stack_align = self.stack_align.max(abi_align); |
| 2818 | 2838 | |
| 2819 | 2839 | if (reg_ok) { |
| ... | ... | @@ -2855,7 +2875,8 @@ fn binOp( |
| 2855 | 2875 | rhs_ty: Type, |
| 2856 | 2876 | metadata: ?BinOpMetadata, |
| 2857 | 2877 | ) InnerError!MCValue { |
| 2858 | const mod = self.bin_file.comp.module.?; | |
| 2878 | const pt = self.pt; | |
| 2879 | const mod = pt.zcu; | |
| 2859 | 2880 | switch (tag) { |
| 2860 | 2881 | .add, |
| 2861 | 2882 | .sub, |
| ... | ... | @@ -2996,7 +3017,7 @@ fn binOp( |
| 2996 | 3017 | .One => ptr_ty.childType(mod).childType(mod), // ptr to array, so get array element type |
| 2997 | 3018 | else => ptr_ty.childType(mod), |
| 2998 | 3019 | }; |
| 2999 | const elem_size = elem_ty.abiSize(mod); | |
| 3020 | const elem_size = elem_ty.abiSize(pt); | |
| 3000 | 3021 | |
| 3001 | 3022 | if (elem_size == 1) { |
| 3002 | 3023 | const base_tag: Mir.Inst.Tag = switch (tag) { |
| ... | ... | @@ -3396,8 +3417,8 @@ fn binOpRegister( |
| 3396 | 3417 | fn br(self: *Self, block: Air.Inst.Index, operand: Air.Inst.Ref) !void { |
| 3397 | 3418 | const block_data = self.blocks.getPtr(block).?; |
| 3398 | 3419 | |
| 3399 | const mod = self.bin_file.comp.module.?; | |
| 3400 | if (self.typeOf(operand).hasRuntimeBits(mod)) { | |
| 3420 | const pt = self.pt; | |
| 3421 | if (self.typeOf(operand).hasRuntimeBits(pt)) { | |
| 3401 | 3422 | const operand_mcv = try self.resolveInst(operand); |
| 3402 | 3423 | const block_mcv = block_data.mcv; |
| 3403 | 3424 | if (block_mcv == .none) { |
| ... | ... | @@ -3516,17 +3537,18 @@ fn ensureProcessDeathCapacity(self: *Self, additional_count: usize) !void { |
| 3516 | 3537 | |
| 3517 | 3538 | /// Given an error union, returns the payload |
| 3518 | 3539 | fn errUnionPayload(self: *Self, error_union_mcv: MCValue, error_union_ty: Type) !MCValue { |
| 3519 | const mod = self.bin_file.comp.module.?; | |
| 3540 | const pt = self.pt; | |
| 3541 | const mod = pt.zcu; | |
| 3520 | 3542 | const err_ty = error_union_ty.errorUnionSet(mod); |
| 3521 | 3543 | const payload_ty = error_union_ty.errorUnionPayload(mod); |
| 3522 | 3544 | if (err_ty.errorSetIsEmpty(mod)) { |
| 3523 | 3545 | return error_union_mcv; |
| 3524 | 3546 | } |
| 3525 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 3547 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3526 | 3548 | return MCValue.none; |
| 3527 | 3549 | } |
| 3528 | 3550 | |
| 3529 | const payload_offset = @as(u32, @intCast(errUnionPayloadOffset(payload_ty, mod))); | |
| 3551 | const payload_offset = @as(u32, @intCast(errUnionPayloadOffset(payload_ty, pt))); | |
| 3530 | 3552 | switch (error_union_mcv) { |
| 3531 | 3553 | .register => return self.fail("TODO errUnionPayload for registers", .{}), |
| 3532 | 3554 | .stack_offset => |off| { |
| ... | ... | @@ -3587,7 +3609,8 @@ fn finishAir(self: *Self, inst: Air.Inst.Index, result: MCValue, operands: [Live |
| 3587 | 3609 | } |
| 3588 | 3610 | |
| 3589 | 3611 | fn genArgDbgInfo(self: Self, inst: Air.Inst.Index, mcv: MCValue) !void { |
| 3590 | const mod = self.bin_file.comp.module.?; | |
| 3612 | const pt = self.pt; | |
| 3613 | const mod = pt.zcu; | |
| 3591 | 3614 | const arg = self.air.instructions.items(.data)[@intFromEnum(inst)].arg; |
| 3592 | 3615 | const ty = arg.ty.toType(); |
| 3593 | 3616 | const owner_decl = mod.funcOwnerDeclIndex(self.func_index); |
| ... | ... | @@ -3736,7 +3759,7 @@ fn genLoadASI(self: *Self, value_reg: Register, addr_reg: Register, off_reg: Reg |
| 3736 | 3759 | } |
| 3737 | 3760 | |
| 3738 | 3761 | fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void { |
| 3739 | const mod = self.bin_file.comp.module.?; | |
| 3762 | const pt = self.pt; | |
| 3740 | 3763 | switch (mcv) { |
| 3741 | 3764 | .dead => unreachable, |
| 3742 | 3765 | .unreach, .none => return, // Nothing to do. |
| ... | ... | @@ -3935,20 +3958,21 @@ fn genSetReg(self: *Self, ty: Type, reg: Register, mcv: MCValue) InnerError!void |
| 3935 | 3958 | // The value is in memory at a hard-coded address. |
| 3936 | 3959 | // If the type is a pointer, it means the pointer address is at this memory location. |
| 3937 | 3960 | try self.genSetReg(ty, reg, .{ .immediate = addr }); |
| 3938 | try self.genLoad(reg, reg, i13, 0, ty.abiSize(mod)); | |
| 3961 | try self.genLoad(reg, reg, i13, 0, ty.abiSize(pt)); | |
| 3939 | 3962 | }, |
| 3940 | 3963 | .stack_offset => |off| { |
| 3941 | 3964 | const real_offset = realStackOffset(off); |
| 3942 | 3965 | const simm13 = math.cast(i13, real_offset) orelse |
| 3943 | 3966 | return self.fail("TODO larger stack offsets: {}", .{real_offset}); |
| 3944 | try self.genLoad(reg, .sp, i13, simm13, ty.abiSize(mod)); | |
| 3967 | try self.genLoad(reg, .sp, i13, simm13, ty.abiSize(pt)); | |
| 3945 | 3968 | }, |
| 3946 | 3969 | } |
| 3947 | 3970 | } |
| 3948 | 3971 | |
| 3949 | 3972 | fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerError!void { |
| 3950 | const mod = self.bin_file.comp.module.?; | |
| 3951 | const abi_size = ty.abiSize(mod); | |
| 3973 | const pt = self.pt; | |
| 3974 | const mod = pt.zcu; | |
| 3975 | const abi_size = ty.abiSize(pt); | |
| 3952 | 3976 | switch (mcv) { |
| 3953 | 3977 | .dead => unreachable, |
| 3954 | 3978 | .unreach, .none => return, // Nothing to do. |
| ... | ... | @@ -3956,7 +3980,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro |
| 3956 | 3980 | if (!self.wantSafety()) |
| 3957 | 3981 | return; // The already existing value will do just fine. |
| 3958 | 3982 | // TODO Upgrade this to a memset call when we have that available. |
| 3959 | switch (ty.abiSize(mod)) { | |
| 3983 | switch (ty.abiSize(pt)) { | |
| 3960 | 3984 | 1 => return self.genSetStack(ty, stack_offset, .{ .immediate = 0xaa }), |
| 3961 | 3985 | 2 => return self.genSetStack(ty, stack_offset, .{ .immediate = 0xaaaa }), |
| 3962 | 3986 | 4 => return self.genSetStack(ty, stack_offset, .{ .immediate = 0xaaaaaaaa }), |
| ... | ... | @@ -3986,7 +4010,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro |
| 3986 | 4010 | try self.genSetStack(wrapped_ty, stack_offset, .{ .register = rwo.reg }); |
| 3987 | 4011 | |
| 3988 | 4012 | const overflow_bit_ty = ty.structFieldType(1, mod); |
| 3989 | const overflow_bit_offset = @as(u32, @intCast(ty.structFieldOffset(1, mod))); | |
| 4013 | const overflow_bit_offset = @as(u32, @intCast(ty.structFieldOffset(1, pt))); | |
| 3990 | 4014 | const cond_reg = try self.register_manager.allocReg(null, gp); |
| 3991 | 4015 | |
| 3992 | 4016 | // TODO handle floating point CCRs |
| ... | ... | @@ -4032,7 +4056,7 @@ fn genSetStack(self: *Self, ty: Type, stack_offset: u32, mcv: MCValue) InnerErro |
| 4032 | 4056 | const reg = try self.copyToTmpRegister(ty, mcv); |
| 4033 | 4057 | return self.genSetStack(ty, stack_offset, MCValue{ .register = reg }); |
| 4034 | 4058 | } else { |
| 4035 | const ptr_ty = try mod.singleMutPtrType(ty); | |
| 4059 | const ptr_ty = try pt.singleMutPtrType(ty); | |
| 4036 | 4060 | |
| 4037 | 4061 | const regs = try self.register_manager.allocRegs(4, .{ null, null, null, null }, gp); |
| 4038 | 4062 | const regs_locks = self.register_manager.lockRegsAssumeUnused(4, regs); |
| ... | ... | @@ -4121,12 +4145,13 @@ fn genStoreASI(self: *Self, value_reg: Register, addr_reg: Register, off_reg: Re |
| 4121 | 4145 | } |
| 4122 | 4146 | |
| 4123 | 4147 | fn genTypedValue(self: *Self, val: Value) InnerError!MCValue { |
| 4124 | const mod = self.bin_file.comp.module.?; | |
| 4148 | const pt = self.pt; | |
| 4125 | 4149 | const mcv: MCValue = switch (try codegen.genTypedValue( |
| 4126 | 4150 | self.bin_file, |
| 4151 | pt, | |
| 4127 | 4152 | self.src_loc, |
| 4128 | 4153 | val, |
| 4129 | mod.funcOwnerDeclIndex(self.func_index), | |
| 4154 | pt.zcu.funcOwnerDeclIndex(self.func_index), | |
| 4130 | 4155 | )) { |
| 4131 | 4156 | .mcv => |mcv| switch (mcv) { |
| 4132 | 4157 | .none => .none, |
| ... | ... | @@ -4157,14 +4182,15 @@ fn getResolvedInstValue(self: *Self, inst: Air.Inst.Index) MCValue { |
| 4157 | 4182 | } |
| 4158 | 4183 | |
| 4159 | 4184 | fn isErr(self: *Self, ty: Type, operand: MCValue) !MCValue { |
| 4160 | const mod = self.bin_file.comp.module.?; | |
| 4185 | const pt = self.pt; | |
| 4186 | const mod = pt.zcu; | |
| 4161 | 4187 | const error_type = ty.errorUnionSet(mod); |
| 4162 | 4188 | const payload_type = ty.errorUnionPayload(mod); |
| 4163 | 4189 | |
| 4164 | if (!error_type.hasRuntimeBits(mod)) { | |
| 4190 | if (!error_type.hasRuntimeBits(pt)) { | |
| 4165 | 4191 | return MCValue{ .immediate = 0 }; // always false |
| 4166 | } else if (!payload_type.hasRuntimeBits(mod)) { | |
| 4167 | if (error_type.abiSize(mod) <= 8) { | |
| 4192 | } else if (!payload_type.hasRuntimeBits(pt)) { | |
| 4193 | if (error_type.abiSize(pt) <= 8) { | |
| 4168 | 4194 | const reg_mcv: MCValue = switch (operand) { |
| 4169 | 4195 | .register => operand, |
| 4170 | 4196 | else => .{ .register = try self.copyToTmpRegister(error_type, operand) }, |
| ... | ... | @@ -4255,9 +4281,10 @@ fn jump(self: *Self, inst: Mir.Inst.Index) !void { |
| 4255 | 4281 | } |
| 4256 | 4282 | |
| 4257 | 4283 | fn load(self: *Self, dst_mcv: MCValue, ptr: MCValue, ptr_ty: Type) InnerError!void { |
| 4258 | const mod = self.bin_file.comp.module.?; | |
| 4284 | const pt = self.pt; | |
| 4285 | const mod = pt.zcu; | |
| 4259 | 4286 | const elem_ty = ptr_ty.childType(mod); |
| 4260 | const elem_size = elem_ty.abiSize(mod); | |
| 4287 | const elem_size = elem_ty.abiSize(pt); | |
| 4261 | 4288 | |
| 4262 | 4289 | switch (ptr) { |
| 4263 | 4290 | .none => unreachable, |
| ... | ... | @@ -4326,7 +4353,8 @@ fn minMax( |
| 4326 | 4353 | lhs_ty: Type, |
| 4327 | 4354 | rhs_ty: Type, |
| 4328 | 4355 | ) InnerError!MCValue { |
| 4329 | const mod = self.bin_file.comp.module.?; | |
| 4356 | const pt = self.pt; | |
| 4357 | const mod = pt.zcu; | |
| 4330 | 4358 | assert(lhs_ty.eql(rhs_ty, mod)); |
| 4331 | 4359 | switch (lhs_ty.zigTypeTag(mod)) { |
| 4332 | 4360 | .Float => return self.fail("TODO min/max on floats", .{}), |
| ... | ... | @@ -4446,7 +4474,8 @@ fn realStackOffset(off: u32) u32 { |
| 4446 | 4474 | |
| 4447 | 4475 | /// Caller must call `CallMCValues.deinit`. |
| 4448 | 4476 | fn resolveCallingConventionValues(self: *Self, fn_ty: Type, role: RegisterView) !CallMCValues { |
| 4449 | const mod = self.bin_file.comp.module.?; | |
| 4477 | const pt = self.pt; | |
| 4478 | const mod = pt.zcu; | |
| 4450 | 4479 | const ip = &mod.intern_pool; |
| 4451 | 4480 | const fn_info = mod.typeToFunc(fn_ty).?; |
| 4452 | 4481 | const cc = fn_info.cc; |
| ... | ... | @@ -4487,7 +4516,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type, role: RegisterView) |
| 4487 | 4516 | }; |
| 4488 | 4517 | |
| 4489 | 4518 | for (fn_info.param_types.get(ip), result.args) |ty, *result_arg| { |
| 4490 | const param_size = @as(u32, @intCast(Type.fromInterned(ty).abiSize(mod))); | |
| 4519 | const param_size = @as(u32, @intCast(Type.fromInterned(ty).abiSize(pt))); | |
| 4491 | 4520 | if (param_size <= 8) { |
| 4492 | 4521 | if (next_register < argument_registers.len) { |
| 4493 | 4522 | result_arg.* = .{ .register = argument_registers[next_register] }; |
| ... | ... | @@ -4516,10 +4545,10 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type, role: RegisterView) |
| 4516 | 4545 | |
| 4517 | 4546 | if (ret_ty.zigTypeTag(mod) == .NoReturn) { |
| 4518 | 4547 | result.return_value = .{ .unreach = {} }; |
| 4519 | } else if (!ret_ty.hasRuntimeBits(mod)) { | |
| 4548 | } else if (!ret_ty.hasRuntimeBits(pt)) { | |
| 4520 | 4549 | result.return_value = .{ .none = {} }; |
| 4521 | 4550 | } else { |
| 4522 | const ret_ty_size = @as(u32, @intCast(ret_ty.abiSize(mod))); | |
| 4551 | const ret_ty_size: u32 = @intCast(ret_ty.abiSize(pt)); | |
| 4523 | 4552 | // The callee puts the return values in %i0-%i3, which becomes %o0-%o3 inside the caller. |
| 4524 | 4553 | if (ret_ty_size <= 8) { |
| 4525 | 4554 | result.return_value = switch (role) { |
| ... | ... | @@ -4538,21 +4567,22 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type, role: RegisterView) |
| 4538 | 4567 | } |
| 4539 | 4568 | |
| 4540 | 4569 | fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue { |
| 4541 | const mod = self.bin_file.comp.module.?; | |
| 4570 | const pt = self.pt; | |
| 4542 | 4571 | const ty = self.typeOf(ref); |
| 4543 | 4572 | |
| 4544 | 4573 | // If the type has no codegen bits, no need to store it. |
| 4545 | if (!ty.hasRuntimeBitsIgnoreComptime(mod)) return .none; | |
| 4574 | if (!ty.hasRuntimeBitsIgnoreComptime(pt)) return .none; | |
| 4546 | 4575 | |
| 4547 | 4576 | if (ref.toIndex()) |inst| { |
| 4548 | 4577 | return self.getResolvedInstValue(inst); |
| 4549 | 4578 | } |
| 4550 | 4579 | |
| 4551 | return self.genTypedValue((try self.air.value(ref, mod)).?); | |
| 4580 | return self.genTypedValue((try self.air.value(ref, pt)).?); | |
| 4552 | 4581 | } |
| 4553 | 4582 | |
| 4554 | 4583 | fn ret(self: *Self, mcv: MCValue) !void { |
| 4555 | const mod = self.bin_file.comp.module.?; | |
| 4584 | const pt = self.pt; | |
| 4585 | const mod = pt.zcu; | |
| 4556 | 4586 | const ret_ty = self.fn_type.fnReturnType(mod); |
| 4557 | 4587 | try self.setRegOrMem(ret_ty, self.ret_mcv, mcv); |
| 4558 | 4588 | |
| ... | ... | @@ -4654,8 +4684,8 @@ pub fn spillInstruction(self: *Self, reg: Register, inst: Air.Inst.Index) !void |
| 4654 | 4684 | } |
| 4655 | 4685 | |
| 4656 | 4686 | fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type) InnerError!void { |
| 4657 | const mod = self.bin_file.comp.module.?; | |
| 4658 | const abi_size = value_ty.abiSize(mod); | |
| 4687 | const pt = self.pt; | |
| 4688 | const abi_size = value_ty.abiSize(pt); | |
| 4659 | 4689 | |
| 4660 | 4690 | switch (ptr) { |
| 4661 | 4691 | .none => unreachable, |
| ... | ... | @@ -4696,11 +4726,12 @@ fn store(self: *Self, ptr: MCValue, value: MCValue, ptr_ty: Type, value_ty: Type |
| 4696 | 4726 | |
| 4697 | 4727 | fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32) !MCValue { |
| 4698 | 4728 | return if (self.liveness.isUnused(inst)) .dead else result: { |
| 4699 | const mod = self.bin_file.comp.module.?; | |
| 4729 | const pt = self.pt; | |
| 4730 | const mod = pt.zcu; | |
| 4700 | 4731 | const mcv = try self.resolveInst(operand); |
| 4701 | 4732 | const ptr_ty = self.typeOf(operand); |
| 4702 | 4733 | const struct_ty = ptr_ty.childType(mod); |
| 4703 | const struct_field_offset = @as(u32, @intCast(struct_ty.structFieldOffset(index, mod))); | |
| 4734 | const struct_field_offset = @as(u32, @intCast(struct_ty.structFieldOffset(index, pt))); | |
| 4704 | 4735 | switch (mcv) { |
| 4705 | 4736 | .ptr_stack_offset => |off| { |
| 4706 | 4737 | break :result MCValue{ .ptr_stack_offset = off - struct_field_offset }; |
| ... | ... | @@ -4738,7 +4769,8 @@ fn trunc( |
| 4738 | 4769 | operand_ty: Type, |
| 4739 | 4770 | dest_ty: Type, |
| 4740 | 4771 | ) !MCValue { |
| 4741 | const mod = self.bin_file.comp.module.?; | |
| 4772 | const pt = self.pt; | |
| 4773 | const mod = pt.zcu; | |
| 4742 | 4774 | const info_a = operand_ty.intInfo(mod); |
| 4743 | 4775 | const info_b = dest_ty.intInfo(mod); |
| 4744 | 4776 | |
| ... | ... | @@ -4848,7 +4880,7 @@ fn truncRegister( |
| 4848 | 4880 | } |
| 4849 | 4881 | } |
| 4850 | 4882 | |
| 4851 | /// TODO support scope overrides. Also note this logic is duplicated with `Module.wantSafety`. | |
| 4883 | /// TODO support scope overrides. Also note this logic is duplicated with `Zcu.wantSafety`. | |
| 4852 | 4884 | fn wantSafety(self: *Self) bool { |
| 4853 | 4885 | return switch (self.bin_file.comp.root_mod.optimize_mode) { |
| 4854 | 4886 | .Debug => true, |
| ... | ... | @@ -4859,11 +4891,9 @@ fn wantSafety(self: *Self) bool { |
| 4859 | 4891 | } |
| 4860 | 4892 | |
| 4861 | 4893 | fn typeOf(self: *Self, inst: Air.Inst.Ref) Type { |
| 4862 | const mod = self.bin_file.comp.module.?; | |
| 4863 | return self.air.typeOf(inst, &mod.intern_pool); | |
| 4894 | return self.air.typeOf(inst, &self.pt.zcu.intern_pool); | |
| 4864 | 4895 | } |
| 4865 | 4896 | |
| 4866 | 4897 | fn typeOfIndex(self: *Self, inst: Air.Inst.Index) Type { |
| 4867 | const mod = self.bin_file.comp.module.?; | |
| 4868 | return self.air.typeOfIndex(inst, &mod.intern_pool); | |
| 4898 | return self.air.typeOfIndex(inst, &self.pt.zcu.intern_pool); | |
| 4869 | 4899 | } |
src/arch/sparc64/Emit.zig+2-4| ... | ... | @@ -6,9 +6,7 @@ const Endian = std.builtin.Endian; |
| 6 | 6 | const assert = std.debug.assert; |
| 7 | 7 | const link = @import("../../link.zig"); |
| 8 | 8 | const Zcu = @import("../../Zcu.zig"); |
| 9 | /// Deprecated. | |
| 10 | const Module = Zcu; | |
| 11 | const ErrorMsg = Module.ErrorMsg; | |
| 9 | const ErrorMsg = Zcu.ErrorMsg; | |
| 12 | 10 | const Liveness = @import("../../Liveness.zig"); |
| 13 | 11 | const log = std.log.scoped(.sparcv9_emit); |
| 14 | 12 | const DebugInfoOutput = @import("../../codegen.zig").DebugInfoOutput; |
| ... | ... | @@ -24,7 +22,7 @@ bin_file: *link.File, |
| 24 | 22 | debug_output: DebugInfoOutput, |
| 25 | 23 | target: *const std.Target, |
| 26 | 24 | err_msg: ?*ErrorMsg = null, |
| 27 | src_loc: Module.LazySrcLoc, | |
| 25 | src_loc: Zcu.LazySrcLoc, | |
| 28 | 26 | code: *std.ArrayList(u8), |
| 29 | 27 | |
| 30 | 28 | prev_di_line: u32, |
src/arch/wasm/CodeGen.zig+532-429| ... | ... | @@ -684,6 +684,7 @@ simd_immediates: std.ArrayListUnmanaged([16]u8) = .{}, |
| 684 | 684 | target: std.Target, |
| 685 | 685 | /// Represents the wasm binary file that is being linked. |
| 686 | 686 | bin_file: *link.File.Wasm, |
| 687 | pt: Zcu.PerThread, | |
| 687 | 688 | /// List of MIR Instructions |
| 688 | 689 | mir_instructions: std.MultiArrayList(Mir.Inst) = .{}, |
| 689 | 690 | /// Contains extra data for MIR |
| ... | ... | @@ -764,8 +765,7 @@ pub fn deinit(func: *CodeGen) void { |
| 764 | 765 | |
| 765 | 766 | /// Sets `err_msg` on `CodeGen` and returns `error.CodegenFail` which is caught in link/Wasm.zig |
| 766 | 767 | fn fail(func: *CodeGen, comptime fmt: []const u8, args: anytype) InnerError { |
| 767 | const mod = func.bin_file.base.comp.module.?; | |
| 768 | const src_loc = func.decl.navSrcLoc(mod); | |
| 768 | const src_loc = func.decl.navSrcLoc(func.pt.zcu); | |
| 769 | 769 | func.err_msg = try Zcu.ErrorMsg.create(func.gpa, src_loc, fmt, args); |
| 770 | 770 | return error.CodegenFail; |
| 771 | 771 | } |
| ... | ... | @@ -788,10 +788,11 @@ fn resolveInst(func: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue { |
| 788 | 788 | const gop = try func.branches.items[0].values.getOrPut(func.gpa, ref); |
| 789 | 789 | assert(!gop.found_existing); |
| 790 | 790 | |
| 791 | const mod = func.bin_file.base.comp.module.?; | |
| 792 | const val = (try func.air.value(ref, mod)).?; | |
| 791 | const pt = func.pt; | |
| 792 | const mod = pt.zcu; | |
| 793 | const val = (try func.air.value(ref, pt)).?; | |
| 793 | 794 | const ty = func.typeOf(ref); |
| 794 | if (!ty.hasRuntimeBitsIgnoreComptime(mod) and !ty.isInt(mod) and !ty.isError(mod)) { | |
| 795 | if (!ty.hasRuntimeBitsIgnoreComptime(pt) and !ty.isInt(mod) and !ty.isError(mod)) { | |
| 795 | 796 | gop.value_ptr.* = WValue{ .none = {} }; |
| 796 | 797 | return gop.value_ptr.*; |
| 797 | 798 | } |
| ... | ... | @@ -802,8 +803,8 @@ fn resolveInst(func: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue { |
| 802 | 803 | // |
| 803 | 804 | // In the other cases, we will simply lower the constant to a value that fits |
| 804 | 805 | // into a single local (such as a pointer, integer, bool, etc). |
| 805 | const result = if (isByRef(ty, mod)) blk: { | |
| 806 | const sym_index = try func.bin_file.lowerUnnamedConst(val, func.decl_index); | |
| 806 | const result = if (isByRef(ty, pt)) blk: { | |
| 807 | const sym_index = try func.bin_file.lowerUnnamedConst(pt, val, func.decl_index); | |
| 807 | 808 | break :blk WValue{ .memory = sym_index }; |
| 808 | 809 | } else try func.lowerConstant(val, ty); |
| 809 | 810 | |
| ... | ... | @@ -990,7 +991,8 @@ fn addExtraAssumeCapacity(func: *CodeGen, extra: anytype) error{OutOfMemory}!u32 |
| 990 | 991 | } |
| 991 | 992 | |
| 992 | 993 | /// Using a given `Type`, returns the corresponding type |
| 993 | fn typeToValtype(ty: Type, mod: *Zcu) wasm.Valtype { | |
| 994 | fn typeToValtype(ty: Type, pt: Zcu.PerThread) wasm.Valtype { | |
| 995 | const mod = pt.zcu; | |
| 994 | 996 | const target = mod.getTarget(); |
| 995 | 997 | const ip = &mod.intern_pool; |
| 996 | 998 | return switch (ty.zigTypeTag(mod)) { |
| ... | ... | @@ -1002,26 +1004,26 @@ fn typeToValtype(ty: Type, mod: *Zcu) wasm.Valtype { |
| 1002 | 1004 | else => unreachable, |
| 1003 | 1005 | }, |
| 1004 | 1006 | .Int, .Enum => blk: { |
| 1005 | const info = ty.intInfo(mod); | |
| 1007 | const info = ty.intInfo(pt.zcu); | |
| 1006 | 1008 | if (info.bits <= 32) break :blk wasm.Valtype.i32; |
| 1007 | 1009 | if (info.bits > 32 and info.bits <= 128) break :blk wasm.Valtype.i64; |
| 1008 | 1010 | break :blk wasm.Valtype.i32; // represented as pointer to stack |
| 1009 | 1011 | }, |
| 1010 | 1012 | .Struct => { |
| 1011 | if (mod.typeToPackedStruct(ty)) |packed_struct| { | |
| 1012 | return typeToValtype(Type.fromInterned(packed_struct.backingIntType(ip).*), mod); | |
| 1013 | if (pt.zcu.typeToPackedStruct(ty)) |packed_struct| { | |
| 1014 | return typeToValtype(Type.fromInterned(packed_struct.backingIntType(ip).*), pt); | |
| 1013 | 1015 | } else { |
| 1014 | 1016 | return wasm.Valtype.i32; |
| 1015 | 1017 | } |
| 1016 | 1018 | }, |
| 1017 | .Vector => switch (determineSimdStoreStrategy(ty, mod)) { | |
| 1019 | .Vector => switch (determineSimdStoreStrategy(ty, pt)) { | |
| 1018 | 1020 | .direct => wasm.Valtype.v128, |
| 1019 | 1021 | .unrolled => wasm.Valtype.i32, |
| 1020 | 1022 | }, |
| 1021 | .Union => switch (ty.containerLayout(mod)) { | |
| 1023 | .Union => switch (ty.containerLayout(pt.zcu)) { | |
| 1022 | 1024 | .@"packed" => { |
| 1023 | const int_ty = mod.intType(.unsigned, @as(u16, @intCast(ty.bitSize(mod)))) catch @panic("out of memory"); | |
| 1024 | return typeToValtype(int_ty, mod); | |
| 1025 | const int_ty = pt.intType(.unsigned, @as(u16, @intCast(ty.bitSize(pt)))) catch @panic("out of memory"); | |
| 1026 | return typeToValtype(int_ty, pt); | |
| 1025 | 1027 | }, |
| 1026 | 1028 | else => wasm.Valtype.i32, |
| 1027 | 1029 | }, |
| ... | ... | @@ -1030,17 +1032,17 @@ fn typeToValtype(ty: Type, mod: *Zcu) wasm.Valtype { |
| 1030 | 1032 | } |
| 1031 | 1033 | |
| 1032 | 1034 | /// Using a given `Type`, returns the byte representation of its wasm value type |
| 1033 | fn genValtype(ty: Type, mod: *Zcu) u8 { | |
| 1034 | return wasm.valtype(typeToValtype(ty, mod)); | |
| 1035 | fn genValtype(ty: Type, pt: Zcu.PerThread) u8 { | |
| 1036 | return wasm.valtype(typeToValtype(ty, pt)); | |
| 1035 | 1037 | } |
| 1036 | 1038 | |
| 1037 | 1039 | /// Using a given `Type`, returns the corresponding wasm value type |
| 1038 | 1040 | /// Differently from `genValtype` this also allows `void` to create a block |
| 1039 | 1041 | /// with no return type |
| 1040 | fn genBlockType(ty: Type, mod: *Zcu) u8 { | |
| 1042 | fn genBlockType(ty: Type, pt: Zcu.PerThread) u8 { | |
| 1041 | 1043 | return switch (ty.ip_index) { |
| 1042 | 1044 | .void_type, .noreturn_type => wasm.block_empty, |
| 1043 | else => genValtype(ty, mod), | |
| 1045 | else => genValtype(ty, pt), | |
| 1044 | 1046 | }; |
| 1045 | 1047 | } |
| 1046 | 1048 | |
| ... | ... | @@ -1101,8 +1103,8 @@ fn getResolvedInst(func: *CodeGen, ref: Air.Inst.Ref) *WValue { |
| 1101 | 1103 | /// Creates one locals for a given `Type`. |
| 1102 | 1104 | /// Returns a corresponding `Wvalue` with `local` as active tag |
| 1103 | 1105 | fn allocLocal(func: *CodeGen, ty: Type) InnerError!WValue { |
| 1104 | const mod = func.bin_file.base.comp.module.?; | |
| 1105 | const valtype = typeToValtype(ty, mod); | |
| 1106 | const pt = func.pt; | |
| 1107 | const valtype = typeToValtype(ty, pt); | |
| 1106 | 1108 | switch (valtype) { |
| 1107 | 1109 | .i32 => if (func.free_locals_i32.popOrNull()) |index| { |
| 1108 | 1110 | log.debug("reusing local ({d}) of type {}", .{ index, valtype }); |
| ... | ... | @@ -1133,8 +1135,8 @@ fn allocLocal(func: *CodeGen, ty: Type) InnerError!WValue { |
| 1133 | 1135 | /// Ensures a new local will be created. This is useful when it's useful |
| 1134 | 1136 | /// to use a zero-initialized local. |
| 1135 | 1137 | fn ensureAllocLocal(func: *CodeGen, ty: Type) InnerError!WValue { |
| 1136 | const mod = func.bin_file.base.comp.module.?; | |
| 1137 | try func.locals.append(func.gpa, genValtype(ty, mod)); | |
| 1138 | const pt = func.pt; | |
| 1139 | try func.locals.append(func.gpa, genValtype(ty, pt)); | |
| 1138 | 1140 | const initial_index = func.local_index; |
| 1139 | 1141 | func.local_index += 1; |
| 1140 | 1142 | return WValue{ .local = .{ .value = initial_index, .references = 1 } }; |
| ... | ... | @@ -1147,23 +1149,24 @@ fn genFunctype( |
| 1147 | 1149 | cc: std.builtin.CallingConvention, |
| 1148 | 1150 | params: []const InternPool.Index, |
| 1149 | 1151 | return_type: Type, |
| 1150 | mod: *Zcu, | |
| 1152 | pt: Zcu.PerThread, | |
| 1151 | 1153 | ) !wasm.Type { |
| 1154 | const mod = pt.zcu; | |
| 1152 | 1155 | var temp_params = std.ArrayList(wasm.Valtype).init(gpa); |
| 1153 | 1156 | defer temp_params.deinit(); |
| 1154 | 1157 | var returns = std.ArrayList(wasm.Valtype).init(gpa); |
| 1155 | 1158 | defer returns.deinit(); |
| 1156 | 1159 | |
| 1157 | if (firstParamSRet(cc, return_type, mod)) { | |
| 1160 | if (firstParamSRet(cc, return_type, pt)) { | |
| 1158 | 1161 | try temp_params.append(.i32); // memory address is always a 32-bit handle |
| 1159 | } else if (return_type.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 1162 | } else if (return_type.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1160 | 1163 | if (cc == .C) { |
| 1161 | const res_classes = abi.classifyType(return_type, mod); | |
| 1164 | const res_classes = abi.classifyType(return_type, pt); | |
| 1162 | 1165 | assert(res_classes[0] == .direct and res_classes[1] == .none); |
| 1163 | const scalar_type = abi.scalarType(return_type, mod); | |
| 1164 | try returns.append(typeToValtype(scalar_type, mod)); | |
| 1166 | const scalar_type = abi.scalarType(return_type, pt); | |
| 1167 | try returns.append(typeToValtype(scalar_type, pt)); | |
| 1165 | 1168 | } else { |
| 1166 | try returns.append(typeToValtype(return_type, mod)); | |
| 1169 | try returns.append(typeToValtype(return_type, pt)); | |
| 1167 | 1170 | } |
| 1168 | 1171 | } else if (return_type.isError(mod)) { |
| 1169 | 1172 | try returns.append(.i32); |
| ... | ... | @@ -1172,25 +1175,25 @@ fn genFunctype( |
| 1172 | 1175 | // param types |
| 1173 | 1176 | for (params) |param_type_ip| { |
| 1174 | 1177 | const param_type = Type.fromInterned(param_type_ip); |
| 1175 | if (!param_type.hasRuntimeBitsIgnoreComptime(mod)) continue; | |
| 1178 | if (!param_type.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 1176 | 1179 | |
| 1177 | 1180 | switch (cc) { |
| 1178 | 1181 | .C => { |
| 1179 | const param_classes = abi.classifyType(param_type, mod); | |
| 1182 | const param_classes = abi.classifyType(param_type, pt); | |
| 1180 | 1183 | for (param_classes) |class| { |
| 1181 | 1184 | if (class == .none) continue; |
| 1182 | 1185 | if (class == .direct) { |
| 1183 | const scalar_type = abi.scalarType(param_type, mod); | |
| 1184 | try temp_params.append(typeToValtype(scalar_type, mod)); | |
| 1186 | const scalar_type = abi.scalarType(param_type, pt); | |
| 1187 | try temp_params.append(typeToValtype(scalar_type, pt)); | |
| 1185 | 1188 | } else { |
| 1186 | try temp_params.append(typeToValtype(param_type, mod)); | |
| 1189 | try temp_params.append(typeToValtype(param_type, pt)); | |
| 1187 | 1190 | } |
| 1188 | 1191 | } |
| 1189 | 1192 | }, |
| 1190 | else => if (isByRef(param_type, mod)) | |
| 1193 | else => if (isByRef(param_type, pt)) | |
| 1191 | 1194 | try temp_params.append(.i32) |
| 1192 | 1195 | else |
| 1193 | try temp_params.append(typeToValtype(param_type, mod)), | |
| 1196 | try temp_params.append(typeToValtype(param_type, pt)), | |
| 1194 | 1197 | } |
| 1195 | 1198 | } |
| 1196 | 1199 | |
| ... | ... | @@ -1202,6 +1205,7 @@ fn genFunctype( |
| 1202 | 1205 | |
| 1203 | 1206 | pub fn generate( |
| 1204 | 1207 | bin_file: *link.File, |
| 1208 | pt: Zcu.PerThread, | |
| 1205 | 1209 | src_loc: Zcu.LazySrcLoc, |
| 1206 | 1210 | func_index: InternPool.Index, |
| 1207 | 1211 | air: Air, |
| ... | ... | @@ -1210,15 +1214,15 @@ pub fn generate( |
| 1210 | 1214 | debug_output: codegen.DebugInfoOutput, |
| 1211 | 1215 | ) codegen.CodeGenError!codegen.Result { |
| 1212 | 1216 | _ = src_loc; |
| 1213 | const comp = bin_file.comp; | |
| 1214 | const gpa = comp.gpa; | |
| 1215 | const zcu = comp.module.?; | |
| 1217 | const zcu = pt.zcu; | |
| 1218 | const gpa = zcu.gpa; | |
| 1216 | 1219 | const func = zcu.funcInfo(func_index); |
| 1217 | 1220 | const decl = zcu.declPtr(func.owner_decl); |
| 1218 | 1221 | const namespace = zcu.namespacePtr(decl.src_namespace); |
| 1219 | 1222 | const target = namespace.fileScope(zcu).mod.resolved_target.result; |
| 1220 | 1223 | var code_gen: CodeGen = .{ |
| 1221 | 1224 | .gpa = gpa, |
| 1225 | .pt = pt, | |
| 1222 | 1226 | .air = air, |
| 1223 | 1227 | .liveness = liveness, |
| 1224 | 1228 | .code = code, |
| ... | ... | @@ -1242,10 +1246,11 @@ pub fn generate( |
| 1242 | 1246 | } |
| 1243 | 1247 | |
| 1244 | 1248 | fn genFunc(func: *CodeGen) InnerError!void { |
| 1245 | const mod = func.bin_file.base.comp.module.?; | |
| 1249 | const pt = func.pt; | |
| 1250 | const mod = pt.zcu; | |
| 1246 | 1251 | const ip = &mod.intern_pool; |
| 1247 | 1252 | const fn_info = mod.typeToFunc(func.decl.typeOf(mod)).?; |
| 1248 | var func_type = try genFunctype(func.gpa, fn_info.cc, fn_info.param_types.get(ip), Type.fromInterned(fn_info.return_type), mod); | |
| 1253 | var func_type = try genFunctype(func.gpa, fn_info.cc, fn_info.param_types.get(ip), Type.fromInterned(fn_info.return_type), pt); | |
| 1249 | 1254 | defer func_type.deinit(func.gpa); |
| 1250 | 1255 | _ = try func.bin_file.storeDeclType(func.decl_index, func_type); |
| 1251 | 1256 | |
| ... | ... | @@ -1272,7 +1277,7 @@ fn genFunc(func: *CodeGen) InnerError!void { |
| 1272 | 1277 | if (func_type.returns.len != 0 and func.air.instructions.len > 0) { |
| 1273 | 1278 | const inst: Air.Inst.Index = @enumFromInt(func.air.instructions.len - 1); |
| 1274 | 1279 | const last_inst_ty = func.typeOfIndex(inst); |
| 1275 | if (!last_inst_ty.hasRuntimeBitsIgnoreComptime(mod) or last_inst_ty.isNoReturn(mod)) { | |
| 1280 | if (!last_inst_ty.hasRuntimeBitsIgnoreComptime(pt) or last_inst_ty.isNoReturn(mod)) { | |
| 1276 | 1281 | try func.addTag(.@"unreachable"); |
| 1277 | 1282 | } |
| 1278 | 1283 | } |
| ... | ... | @@ -1354,7 +1359,8 @@ const CallWValues = struct { |
| 1354 | 1359 | }; |
| 1355 | 1360 | |
| 1356 | 1361 | fn resolveCallingConventionValues(func: *CodeGen, fn_ty: Type) InnerError!CallWValues { |
| 1357 | const mod = func.bin_file.base.comp.module.?; | |
| 1362 | const pt = func.pt; | |
| 1363 | const mod = pt.zcu; | |
| 1358 | 1364 | const ip = &mod.intern_pool; |
| 1359 | 1365 | const fn_info = mod.typeToFunc(fn_ty).?; |
| 1360 | 1366 | const cc = fn_info.cc; |
| ... | ... | @@ -1369,7 +1375,7 @@ fn resolveCallingConventionValues(func: *CodeGen, fn_ty: Type) InnerError!CallWV |
| 1369 | 1375 | |
| 1370 | 1376 | // Check if we store the result as a pointer to the stack rather than |
| 1371 | 1377 | // by value |
| 1372 | if (firstParamSRet(fn_info.cc, Type.fromInterned(fn_info.return_type), mod)) { | |
| 1378 | if (firstParamSRet(fn_info.cc, Type.fromInterned(fn_info.return_type), pt)) { | |
| 1373 | 1379 | // the sret arg will be passed as first argument, therefore we |
| 1374 | 1380 | // set the `return_value` before allocating locals for regular args. |
| 1375 | 1381 | result.return_value = .{ .local = .{ .value = func.local_index, .references = 1 } }; |
| ... | ... | @@ -1379,7 +1385,7 @@ fn resolveCallingConventionValues(func: *CodeGen, fn_ty: Type) InnerError!CallWV |
| 1379 | 1385 | switch (cc) { |
| 1380 | 1386 | .Unspecified => { |
| 1381 | 1387 | for (fn_info.param_types.get(ip)) |ty| { |
| 1382 | if (!Type.fromInterned(ty).hasRuntimeBitsIgnoreComptime(mod)) { | |
| 1388 | if (!Type.fromInterned(ty).hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1383 | 1389 | continue; |
| 1384 | 1390 | } |
| 1385 | 1391 | |
| ... | ... | @@ -1389,7 +1395,7 @@ fn resolveCallingConventionValues(func: *CodeGen, fn_ty: Type) InnerError!CallWV |
| 1389 | 1395 | }, |
| 1390 | 1396 | .C => { |
| 1391 | 1397 | for (fn_info.param_types.get(ip)) |ty| { |
| 1392 | const ty_classes = abi.classifyType(Type.fromInterned(ty), mod); | |
| 1398 | const ty_classes = abi.classifyType(Type.fromInterned(ty), pt); | |
| 1393 | 1399 | for (ty_classes) |class| { |
| 1394 | 1400 | if (class == .none) continue; |
| 1395 | 1401 | try args.append(.{ .local = .{ .value = func.local_index, .references = 1 } }); |
| ... | ... | @@ -1403,11 +1409,11 @@ fn resolveCallingConventionValues(func: *CodeGen, fn_ty: Type) InnerError!CallWV |
| 1403 | 1409 | return result; |
| 1404 | 1410 | } |
| 1405 | 1411 | |
| 1406 | fn firstParamSRet(cc: std.builtin.CallingConvention, return_type: Type, mod: *Zcu) bool { | |
| 1412 | fn firstParamSRet(cc: std.builtin.CallingConvention, return_type: Type, pt: Zcu.PerThread) bool { | |
| 1407 | 1413 | switch (cc) { |
| 1408 | .Unspecified, .Inline => return isByRef(return_type, mod), | |
| 1414 | .Unspecified, .Inline => return isByRef(return_type, pt), | |
| 1409 | 1415 | .C => { |
| 1410 | const ty_classes = abi.classifyType(return_type, mod); | |
| 1416 | const ty_classes = abi.classifyType(return_type, pt); | |
| 1411 | 1417 | if (ty_classes[0] == .indirect) return true; |
| 1412 | 1418 | if (ty_classes[0] == .direct and ty_classes[1] == .direct) return true; |
| 1413 | 1419 | return false; |
| ... | ... | @@ -1423,8 +1429,9 @@ fn lowerArg(func: *CodeGen, cc: std.builtin.CallingConvention, ty: Type, value: |
| 1423 | 1429 | return func.lowerToStack(value); |
| 1424 | 1430 | } |
| 1425 | 1431 | |
| 1426 | const mod = func.bin_file.base.comp.module.?; | |
| 1427 | const ty_classes = abi.classifyType(ty, mod); | |
| 1432 | const pt = func.pt; | |
| 1433 | const mod = pt.zcu; | |
| 1434 | const ty_classes = abi.classifyType(ty, pt); | |
| 1428 | 1435 | assert(ty_classes[0] != .none); |
| 1429 | 1436 | switch (ty.zigTypeTag(mod)) { |
| 1430 | 1437 | .Struct, .Union => { |
| ... | ... | @@ -1432,7 +1439,7 @@ fn lowerArg(func: *CodeGen, cc: std.builtin.CallingConvention, ty: Type, value: |
| 1432 | 1439 | return func.lowerToStack(value); |
| 1433 | 1440 | } |
| 1434 | 1441 | assert(ty_classes[0] == .direct); |
| 1435 | const scalar_type = abi.scalarType(ty, mod); | |
| 1442 | const scalar_type = abi.scalarType(ty, pt); | |
| 1436 | 1443 | switch (value) { |
| 1437 | 1444 | .memory, |
| 1438 | 1445 | .memory_offset, |
| ... | ... | @@ -1447,7 +1454,7 @@ fn lowerArg(func: *CodeGen, cc: std.builtin.CallingConvention, ty: Type, value: |
| 1447 | 1454 | return func.lowerToStack(value); |
| 1448 | 1455 | } |
| 1449 | 1456 | assert(ty_classes[0] == .direct and ty_classes[1] == .direct); |
| 1450 | assert(ty.abiSize(mod) == 16); | |
| 1457 | assert(ty.abiSize(pt) == 16); | |
| 1451 | 1458 | // in this case we have an integer or float that must be lowered as 2 i64's. |
| 1452 | 1459 | try func.emitWValue(value); |
| 1453 | 1460 | try func.addMemArg(.i64_load, .{ .offset = value.offset(), .alignment = 8 }); |
| ... | ... | @@ -1514,18 +1521,18 @@ fn restoreStackPointer(func: *CodeGen) !void { |
| 1514 | 1521 | /// |
| 1515 | 1522 | /// Asserts Type has codegenbits |
| 1516 | 1523 | fn allocStack(func: *CodeGen, ty: Type) !WValue { |
| 1517 | const mod = func.bin_file.base.comp.module.?; | |
| 1518 | assert(ty.hasRuntimeBitsIgnoreComptime(mod)); | |
| 1524 | const pt = func.pt; | |
| 1525 | assert(ty.hasRuntimeBitsIgnoreComptime(pt)); | |
| 1519 | 1526 | if (func.initial_stack_value == .none) { |
| 1520 | 1527 | try func.initializeStack(); |
| 1521 | 1528 | } |
| 1522 | 1529 | |
| 1523 | const abi_size = std.math.cast(u32, ty.abiSize(mod)) orelse { | |
| 1530 | const abi_size = std.math.cast(u32, ty.abiSize(pt)) orelse { | |
| 1524 | 1531 | return func.fail("Type {} with ABI size of {d} exceeds stack frame size", .{ |
| 1525 | ty.fmt(mod), ty.abiSize(mod), | |
| 1532 | ty.fmt(pt), ty.abiSize(pt), | |
| 1526 | 1533 | }); |
| 1527 | 1534 | }; |
| 1528 | const abi_align = ty.abiAlignment(mod); | |
| 1535 | const abi_align = ty.abiAlignment(pt); | |
| 1529 | 1536 | |
| 1530 | 1537 | func.stack_alignment = func.stack_alignment.max(abi_align); |
| 1531 | 1538 | |
| ... | ... | @@ -1540,7 +1547,8 @@ fn allocStack(func: *CodeGen, ty: Type) !WValue { |
| 1540 | 1547 | /// This is different from allocStack where this will use the pointer's alignment |
| 1541 | 1548 | /// if it is set, to ensure the stack alignment will be set correctly. |
| 1542 | 1549 | fn allocStackPtr(func: *CodeGen, inst: Air.Inst.Index) !WValue { |
| 1543 | const mod = func.bin_file.base.comp.module.?; | |
| 1550 | const pt = func.pt; | |
| 1551 | const mod = pt.zcu; | |
| 1544 | 1552 | const ptr_ty = func.typeOfIndex(inst); |
| 1545 | 1553 | const pointee_ty = ptr_ty.childType(mod); |
| 1546 | 1554 | |
| ... | ... | @@ -1548,14 +1556,14 @@ fn allocStackPtr(func: *CodeGen, inst: Air.Inst.Index) !WValue { |
| 1548 | 1556 | try func.initializeStack(); |
| 1549 | 1557 | } |
| 1550 | 1558 | |
| 1551 | if (!pointee_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 1559 | if (!pointee_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1552 | 1560 | return func.allocStack(Type.usize); // create a value containing just the stack pointer. |
| 1553 | 1561 | } |
| 1554 | 1562 | |
| 1555 | const abi_alignment = ptr_ty.ptrAlignment(mod); | |
| 1556 | const abi_size = std.math.cast(u32, pointee_ty.abiSize(mod)) orelse { | |
| 1563 | const abi_alignment = ptr_ty.ptrAlignment(pt); | |
| 1564 | const abi_size = std.math.cast(u32, pointee_ty.abiSize(pt)) orelse { | |
| 1557 | 1565 | return func.fail("Type {} with ABI size of {d} exceeds stack frame size", .{ |
| 1558 | pointee_ty.fmt(mod), pointee_ty.abiSize(mod), | |
| 1566 | pointee_ty.fmt(pt), pointee_ty.abiSize(pt), | |
| 1559 | 1567 | }); |
| 1560 | 1568 | }; |
| 1561 | 1569 | func.stack_alignment = func.stack_alignment.max(abi_alignment); |
| ... | ... | @@ -1711,7 +1719,8 @@ fn arch(func: *const CodeGen) std.Target.Cpu.Arch { |
| 1711 | 1719 | |
| 1712 | 1720 | /// For a given `Type`, will return true when the type will be passed |
| 1713 | 1721 | /// by reference, rather than by value |
| 1714 | fn isByRef(ty: Type, mod: *Zcu) bool { | |
| 1722 | fn isByRef(ty: Type, pt: Zcu.PerThread) bool { | |
| 1723 | const mod = pt.zcu; | |
| 1715 | 1724 | const ip = &mod.intern_pool; |
| 1716 | 1725 | const target = mod.getTarget(); |
| 1717 | 1726 | switch (ty.zigTypeTag(mod)) { |
| ... | ... | @@ -1734,28 +1743,28 @@ fn isByRef(ty: Type, mod: *Zcu) bool { |
| 1734 | 1743 | |
| 1735 | 1744 | .Array, |
| 1736 | 1745 | .Frame, |
| 1737 | => return ty.hasRuntimeBitsIgnoreComptime(mod), | |
| 1746 | => return ty.hasRuntimeBitsIgnoreComptime(pt), | |
| 1738 | 1747 | .Union => { |
| 1739 | 1748 | if (mod.typeToUnion(ty)) |union_obj| { |
| 1740 | 1749 | if (union_obj.getLayout(ip) == .@"packed") { |
| 1741 | return ty.abiSize(mod) > 8; | |
| 1750 | return ty.abiSize(pt) > 8; | |
| 1742 | 1751 | } |
| 1743 | 1752 | } |
| 1744 | return ty.hasRuntimeBitsIgnoreComptime(mod); | |
| 1753 | return ty.hasRuntimeBitsIgnoreComptime(pt); | |
| 1745 | 1754 | }, |
| 1746 | 1755 | .Struct => { |
| 1747 | 1756 | if (mod.typeToPackedStruct(ty)) |packed_struct| { |
| 1748 | return isByRef(Type.fromInterned(packed_struct.backingIntType(ip).*), mod); | |
| 1757 | return isByRef(Type.fromInterned(packed_struct.backingIntType(ip).*), pt); | |
| 1749 | 1758 | } |
| 1750 | return ty.hasRuntimeBitsIgnoreComptime(mod); | |
| 1759 | return ty.hasRuntimeBitsIgnoreComptime(pt); | |
| 1751 | 1760 | }, |
| 1752 | .Vector => return determineSimdStoreStrategy(ty, mod) == .unrolled, | |
| 1761 | .Vector => return determineSimdStoreStrategy(ty, pt) == .unrolled, | |
| 1753 | 1762 | .Int => return ty.intInfo(mod).bits > 64, |
| 1754 | 1763 | .Enum => return ty.intInfo(mod).bits > 64, |
| 1755 | 1764 | .Float => return ty.floatBits(target) > 64, |
| 1756 | 1765 | .ErrorUnion => { |
| 1757 | 1766 | const pl_ty = ty.errorUnionPayload(mod); |
| 1758 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 1767 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1759 | 1768 | return false; |
| 1760 | 1769 | } |
| 1761 | 1770 | return true; |
| ... | ... | @@ -1764,7 +1773,7 @@ fn isByRef(ty: Type, mod: *Zcu) bool { |
| 1764 | 1773 | if (ty.isPtrLikeOptional(mod)) return false; |
| 1765 | 1774 | const pl_type = ty.optionalChild(mod); |
| 1766 | 1775 | if (pl_type.zigTypeTag(mod) == .ErrorSet) return false; |
| 1767 | return pl_type.hasRuntimeBitsIgnoreComptime(mod); | |
| 1776 | return pl_type.hasRuntimeBitsIgnoreComptime(pt); | |
| 1768 | 1777 | }, |
| 1769 | 1778 | .Pointer => { |
| 1770 | 1779 | // Slices act like struct and will be passed by reference |
| ... | ... | @@ -1783,11 +1792,11 @@ const SimdStoreStrategy = enum { |
| 1783 | 1792 | /// This means when a given type is 128 bits and either the simd128 or relaxed-simd |
| 1784 | 1793 | /// features are enabled, the function will return `.direct`. This would allow to store |
| 1785 | 1794 | /// it using a instruction, rather than an unrolled version. |
| 1786 | fn determineSimdStoreStrategy(ty: Type, mod: *Zcu) SimdStoreStrategy { | |
| 1787 | std.debug.assert(ty.zigTypeTag(mod) == .Vector); | |
| 1788 | if (ty.bitSize(mod) != 128) return .unrolled; | |
| 1795 | fn determineSimdStoreStrategy(ty: Type, pt: Zcu.PerThread) SimdStoreStrategy { | |
| 1796 | std.debug.assert(ty.zigTypeTag(pt.zcu) == .Vector); | |
| 1797 | if (ty.bitSize(pt) != 128) return .unrolled; | |
| 1789 | 1798 | const hasFeature = std.Target.wasm.featureSetHas; |
| 1790 | const target = mod.getTarget(); | |
| 1799 | const target = pt.zcu.getTarget(); | |
| 1791 | 1800 | const features = target.cpu.features; |
| 1792 | 1801 | if (hasFeature(features, .relaxed_simd) or hasFeature(features, .simd128)) { |
| 1793 | 1802 | return .direct; |
| ... | ... | @@ -2064,7 +2073,8 @@ fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2064 | 2073 | } |
| 2065 | 2074 | |
| 2066 | 2075 | fn genBody(func: *CodeGen, body: []const Air.Inst.Index) InnerError!void { |
| 2067 | const mod = func.bin_file.base.comp.module.?; | |
| 2076 | const pt = func.pt; | |
| 2077 | const mod = pt.zcu; | |
| 2068 | 2078 | const ip = &mod.intern_pool; |
| 2069 | 2079 | |
| 2070 | 2080 | for (body) |inst| { |
| ... | ... | @@ -2085,7 +2095,8 @@ fn genBody(func: *CodeGen, body: []const Air.Inst.Index) InnerError!void { |
| 2085 | 2095 | } |
| 2086 | 2096 | |
| 2087 | 2097 | fn airRet(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2088 | const mod = func.bin_file.base.comp.module.?; | |
| 2098 | const pt = func.pt; | |
| 2099 | const mod = pt.zcu; | |
| 2089 | 2100 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 2090 | 2101 | const operand = try func.resolveInst(un_op); |
| 2091 | 2102 | const fn_info = mod.typeToFunc(func.decl.typeOf(mod)).?; |
| ... | ... | @@ -2095,27 +2106,27 @@ fn airRet(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2095 | 2106 | // to the stack instead |
| 2096 | 2107 | if (func.return_value != .none) { |
| 2097 | 2108 | try func.store(func.return_value, operand, ret_ty, 0); |
| 2098 | } else if (fn_info.cc == .C and ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 2109 | } else if (fn_info.cc == .C and ret_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 2099 | 2110 | switch (ret_ty.zigTypeTag(mod)) { |
| 2100 | 2111 | // Aggregate types can be lowered as a singular value |
| 2101 | 2112 | .Struct, .Union => { |
| 2102 | const scalar_type = abi.scalarType(ret_ty, mod); | |
| 2113 | const scalar_type = abi.scalarType(ret_ty, pt); | |
| 2103 | 2114 | try func.emitWValue(operand); |
| 2104 | 2115 | const opcode = buildOpcode(.{ |
| 2105 | 2116 | .op = .load, |
| 2106 | .width = @as(u8, @intCast(scalar_type.abiSize(mod) * 8)), | |
| 2117 | .width = @as(u8, @intCast(scalar_type.abiSize(pt) * 8)), | |
| 2107 | 2118 | .signedness = if (scalar_type.isSignedInt(mod)) .signed else .unsigned, |
| 2108 | .valtype1 = typeToValtype(scalar_type, mod), | |
| 2119 | .valtype1 = typeToValtype(scalar_type, pt), | |
| 2109 | 2120 | }); |
| 2110 | 2121 | try func.addMemArg(Mir.Inst.Tag.fromOpcode(opcode), .{ |
| 2111 | 2122 | .offset = operand.offset(), |
| 2112 | .alignment = @intCast(scalar_type.abiAlignment(mod).toByteUnits().?), | |
| 2123 | .alignment = @intCast(scalar_type.abiAlignment(pt).toByteUnits().?), | |
| 2113 | 2124 | }); |
| 2114 | 2125 | }, |
| 2115 | 2126 | else => try func.emitWValue(operand), |
| 2116 | 2127 | } |
| 2117 | 2128 | } else { |
| 2118 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod) and ret_ty.isError(mod)) { | |
| 2129 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt) and ret_ty.isError(mod)) { | |
| 2119 | 2130 | try func.addImm32(0); |
| 2120 | 2131 | } else { |
| 2121 | 2132 | try func.emitWValue(operand); |
| ... | ... | @@ -2128,16 +2139,17 @@ fn airRet(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2128 | 2139 | } |
| 2129 | 2140 | |
| 2130 | 2141 | fn airRetPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2131 | const mod = func.bin_file.base.comp.module.?; | |
| 2142 | const pt = func.pt; | |
| 2143 | const mod = pt.zcu; | |
| 2132 | 2144 | const child_type = func.typeOfIndex(inst).childType(mod); |
| 2133 | 2145 | |
| 2134 | 2146 | const result = result: { |
| 2135 | if (!child_type.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { | |
| 2147 | if (!child_type.isFnOrHasRuntimeBitsIgnoreComptime(pt)) { | |
| 2136 | 2148 | break :result try func.allocStack(Type.usize); // create pointer to void |
| 2137 | 2149 | } |
| 2138 | 2150 | |
| 2139 | 2151 | const fn_info = mod.typeToFunc(func.decl.typeOf(mod)).?; |
| 2140 | if (firstParamSRet(fn_info.cc, Type.fromInterned(fn_info.return_type), mod)) { | |
| 2152 | if (firstParamSRet(fn_info.cc, Type.fromInterned(fn_info.return_type), pt)) { | |
| 2141 | 2153 | break :result func.return_value; |
| 2142 | 2154 | } |
| 2143 | 2155 | |
| ... | ... | @@ -2148,17 +2160,18 @@ fn airRetPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2148 | 2160 | } |
| 2149 | 2161 | |
| 2150 | 2162 | fn airRetLoad(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2151 | const mod = func.bin_file.base.comp.module.?; | |
| 2163 | const pt = func.pt; | |
| 2164 | const mod = pt.zcu; | |
| 2152 | 2165 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 2153 | 2166 | const operand = try func.resolveInst(un_op); |
| 2154 | 2167 | const ret_ty = func.typeOf(un_op).childType(mod); |
| 2155 | 2168 | |
| 2156 | 2169 | const fn_info = mod.typeToFunc(func.decl.typeOf(mod)).?; |
| 2157 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 2170 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 2158 | 2171 | if (ret_ty.isError(mod)) { |
| 2159 | 2172 | try func.addImm32(0); |
| 2160 | 2173 | } |
| 2161 | } else if (!firstParamSRet(fn_info.cc, Type.fromInterned(fn_info.return_type), mod)) { | |
| 2174 | } else if (!firstParamSRet(fn_info.cc, Type.fromInterned(fn_info.return_type), pt)) { | |
| 2162 | 2175 | // leave on the stack |
| 2163 | 2176 | _ = try func.load(operand, ret_ty, 0); |
| 2164 | 2177 | } |
| ... | ... | @@ -2175,7 +2188,8 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif |
| 2175 | 2188 | const args = @as([]const Air.Inst.Ref, @ptrCast(func.air.extra[extra.end..][0..extra.data.args_len])); |
| 2176 | 2189 | const ty = func.typeOf(pl_op.operand); |
| 2177 | 2190 | |
| 2178 | const mod = func.bin_file.base.comp.module.?; | |
| 2191 | const pt = func.pt; | |
| 2192 | const mod = pt.zcu; | |
| 2179 | 2193 | const ip = &mod.intern_pool; |
| 2180 | 2194 | const fn_ty = switch (ty.zigTypeTag(mod)) { |
| 2181 | 2195 | .Fn => ty, |
| ... | ... | @@ -2184,10 +2198,10 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif |
| 2184 | 2198 | }; |
| 2185 | 2199 | const ret_ty = fn_ty.fnReturnType(mod); |
| 2186 | 2200 | const fn_info = mod.typeToFunc(fn_ty).?; |
| 2187 | const first_param_sret = firstParamSRet(fn_info.cc, Type.fromInterned(fn_info.return_type), mod); | |
| 2201 | const first_param_sret = firstParamSRet(fn_info.cc, Type.fromInterned(fn_info.return_type), pt); | |
| 2188 | 2202 | |
| 2189 | 2203 | const callee: ?InternPool.DeclIndex = blk: { |
| 2190 | const func_val = (try func.air.value(pl_op.operand, mod)) orelse break :blk null; | |
| 2204 | const func_val = (try func.air.value(pl_op.operand, pt)) orelse break :blk null; | |
| 2191 | 2205 | |
| 2192 | 2206 | if (func_val.getFunction(mod)) |function| { |
| 2193 | 2207 | _ = try func.bin_file.getOrCreateAtomForDecl(function.owner_decl); |
| ... | ... | @@ -2195,7 +2209,7 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif |
| 2195 | 2209 | } else if (func_val.getExternFunc(mod)) |extern_func| { |
| 2196 | 2210 | const ext_decl = mod.declPtr(extern_func.decl); |
| 2197 | 2211 | const ext_info = mod.typeToFunc(ext_decl.typeOf(mod)).?; |
| 2198 | var func_type = try genFunctype(func.gpa, ext_info.cc, ext_info.param_types.get(ip), Type.fromInterned(ext_info.return_type), mod); | |
| 2212 | var func_type = try genFunctype(func.gpa, ext_info.cc, ext_info.param_types.get(ip), Type.fromInterned(ext_info.return_type), pt); | |
| 2199 | 2213 | defer func_type.deinit(func.gpa); |
| 2200 | 2214 | const atom_index = try func.bin_file.getOrCreateAtomForDecl(extern_func.decl); |
| 2201 | 2215 | const atom = func.bin_file.getAtomPtr(atom_index); |
| ... | ... | @@ -2230,7 +2244,7 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif |
| 2230 | 2244 | const arg_val = try func.resolveInst(arg); |
| 2231 | 2245 | |
| 2232 | 2246 | const arg_ty = func.typeOf(arg); |
| 2233 | if (!arg_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; | |
| 2247 | if (!arg_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 2234 | 2248 | |
| 2235 | 2249 | try func.lowerArg(mod.typeToFunc(fn_ty).?.cc, arg_ty, arg_val); |
| 2236 | 2250 | } |
| ... | ... | @@ -2245,7 +2259,7 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif |
| 2245 | 2259 | const operand = try func.resolveInst(pl_op.operand); |
| 2246 | 2260 | try func.emitWValue(operand); |
| 2247 | 2261 | |
| 2248 | var fn_type = try genFunctype(func.gpa, fn_info.cc, fn_info.param_types.get(ip), Type.fromInterned(fn_info.return_type), mod); | |
| 2262 | var fn_type = try genFunctype(func.gpa, fn_info.cc, fn_info.param_types.get(ip), Type.fromInterned(fn_info.return_type), pt); | |
| 2249 | 2263 | defer fn_type.deinit(func.gpa); |
| 2250 | 2264 | |
| 2251 | 2265 | const fn_type_index = try func.bin_file.zigObjectPtr().?.putOrGetFuncType(func.gpa, fn_type); |
| ... | ... | @@ -2253,7 +2267,7 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif |
| 2253 | 2267 | } |
| 2254 | 2268 | |
| 2255 | 2269 | const result_value = result_value: { |
| 2256 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod) and !ret_ty.isError(mod)) { | |
| 2270 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt) and !ret_ty.isError(mod)) { | |
| 2257 | 2271 | break :result_value WValue{ .none = {} }; |
| 2258 | 2272 | } else if (ret_ty.isNoReturn(mod)) { |
| 2259 | 2273 | try func.addTag(.@"unreachable"); |
| ... | ... | @@ -2264,7 +2278,7 @@ fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallModif |
| 2264 | 2278 | } else if (mod.typeToFunc(fn_ty).?.cc == .C and ret_ty.zigTypeTag(mod) == .Struct or ret_ty.zigTypeTag(mod) == .Union) { |
| 2265 | 2279 | const result_local = try func.allocLocal(ret_ty); |
| 2266 | 2280 | try func.addLabel(.local_set, result_local.local.value); |
| 2267 | const scalar_type = abi.scalarType(ret_ty, mod); | |
| 2281 | const scalar_type = abi.scalarType(ret_ty, pt); | |
| 2268 | 2282 | const result = try func.allocStack(scalar_type); |
| 2269 | 2283 | try func.store(result, result_local, scalar_type, 0); |
| 2270 | 2284 | break :result_value result; |
| ... | ... | @@ -2287,7 +2301,8 @@ fn airAlloc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2287 | 2301 | } |
| 2288 | 2302 | |
| 2289 | 2303 | fn airStore(func: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void { |
| 2290 | const mod = func.bin_file.base.comp.module.?; | |
| 2304 | const pt = func.pt; | |
| 2305 | const mod = pt.zcu; | |
| 2291 | 2306 | if (safety) { |
| 2292 | 2307 | // TODO if the value is undef, write 0xaa bytes to dest |
| 2293 | 2308 | } else { |
| ... | ... | @@ -2306,13 +2321,13 @@ fn airStore(func: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void |
| 2306 | 2321 | } else { |
| 2307 | 2322 | // at this point we have a non-natural alignment, we must |
| 2308 | 2323 | // load the value, and then shift+or the rhs into the result location. |
| 2309 | const int_elem_ty = try mod.intType(.unsigned, ptr_info.packed_offset.host_size * 8); | |
| 2324 | const int_elem_ty = try pt.intType(.unsigned, ptr_info.packed_offset.host_size * 8); | |
| 2310 | 2325 | |
| 2311 | if (isByRef(int_elem_ty, mod)) { | |
| 2326 | if (isByRef(int_elem_ty, pt)) { | |
| 2312 | 2327 | return func.fail("TODO: airStore for pointers to bitfields with backing type larger than 64bits", .{}); |
| 2313 | 2328 | } |
| 2314 | 2329 | |
| 2315 | var mask = @as(u64, @intCast((@as(u65, 1) << @as(u7, @intCast(ty.bitSize(mod)))) - 1)); | |
| 2330 | var mask = @as(u64, @intCast((@as(u65, 1) << @as(u7, @intCast(ty.bitSize(pt)))) - 1)); | |
| 2316 | 2331 | mask <<= @as(u6, @intCast(ptr_info.packed_offset.bit_offset)); |
| 2317 | 2332 | mask ^= ~@as(u64, 0); |
| 2318 | 2333 | const shift_val = if (ptr_info.packed_offset.host_size <= 4) |
| ... | ... | @@ -2324,9 +2339,9 @@ fn airStore(func: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void |
| 2324 | 2339 | else |
| 2325 | 2340 | WValue{ .imm64 = mask }; |
| 2326 | 2341 | const wrap_mask_val = if (ptr_info.packed_offset.host_size <= 4) |
| 2327 | WValue{ .imm32 = @truncate(~@as(u64, 0) >> @intCast(64 - ty.bitSize(mod))) } | |
| 2342 | WValue{ .imm32 = @truncate(~@as(u64, 0) >> @intCast(64 - ty.bitSize(pt))) } | |
| 2328 | 2343 | else |
| 2329 | WValue{ .imm64 = ~@as(u64, 0) >> @intCast(64 - ty.bitSize(mod)) }; | |
| 2344 | WValue{ .imm64 = ~@as(u64, 0) >> @intCast(64 - ty.bitSize(pt)) }; | |
| 2330 | 2345 | |
| 2331 | 2346 | try func.emitWValue(lhs); |
| 2332 | 2347 | const loaded = try func.load(lhs, int_elem_ty, 0); |
| ... | ... | @@ -2346,12 +2361,13 @@ fn airStore(func: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void |
| 2346 | 2361 | |
| 2347 | 2362 | fn store(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerError!void { |
| 2348 | 2363 | assert(!(lhs != .stack and rhs == .stack)); |
| 2349 | const mod = func.bin_file.base.comp.module.?; | |
| 2350 | const abi_size = ty.abiSize(mod); | |
| 2364 | const pt = func.pt; | |
| 2365 | const mod = pt.zcu; | |
| 2366 | const abi_size = ty.abiSize(pt); | |
| 2351 | 2367 | switch (ty.zigTypeTag(mod)) { |
| 2352 | 2368 | .ErrorUnion => { |
| 2353 | 2369 | const pl_ty = ty.errorUnionPayload(mod); |
| 2354 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 2370 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 2355 | 2371 | return func.store(lhs, rhs, Type.anyerror, 0); |
| 2356 | 2372 | } |
| 2357 | 2373 | |
| ... | ... | @@ -2363,7 +2379,7 @@ fn store(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerE |
| 2363 | 2379 | return func.store(lhs, rhs, Type.usize, 0); |
| 2364 | 2380 | } |
| 2365 | 2381 | const pl_ty = ty.optionalChild(mod); |
| 2366 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 2382 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 2367 | 2383 | return func.store(lhs, rhs, Type.u8, 0); |
| 2368 | 2384 | } |
| 2369 | 2385 | if (pl_ty.zigTypeTag(mod) == .ErrorSet) { |
| ... | ... | @@ -2373,11 +2389,11 @@ fn store(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerE |
| 2373 | 2389 | const len = @as(u32, @intCast(abi_size)); |
| 2374 | 2390 | return func.memcpy(lhs, rhs, .{ .imm32 = len }); |
| 2375 | 2391 | }, |
| 2376 | .Struct, .Array, .Union => if (isByRef(ty, mod)) { | |
| 2392 | .Struct, .Array, .Union => if (isByRef(ty, pt)) { | |
| 2377 | 2393 | const len = @as(u32, @intCast(abi_size)); |
| 2378 | 2394 | return func.memcpy(lhs, rhs, .{ .imm32 = len }); |
| 2379 | 2395 | }, |
| 2380 | .Vector => switch (determineSimdStoreStrategy(ty, mod)) { | |
| 2396 | .Vector => switch (determineSimdStoreStrategy(ty, pt)) { | |
| 2381 | 2397 | .unrolled => { |
| 2382 | 2398 | const len: u32 = @intCast(abi_size); |
| 2383 | 2399 | return func.memcpy(lhs, rhs, .{ .imm32 = len }); |
| ... | ... | @@ -2391,7 +2407,7 @@ fn store(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerE |
| 2391 | 2407 | try func.mir_extra.appendSlice(func.gpa, &[_]u32{ |
| 2392 | 2408 | std.wasm.simdOpcode(.v128_store), |
| 2393 | 2409 | offset + lhs.offset(), |
| 2394 | @intCast(ty.abiAlignment(mod).toByteUnits() orelse 0), | |
| 2410 | @intCast(ty.abiAlignment(pt).toByteUnits() orelse 0), | |
| 2395 | 2411 | }); |
| 2396 | 2412 | return func.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } }); |
| 2397 | 2413 | }, |
| ... | ... | @@ -2421,11 +2437,11 @@ fn store(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerE |
| 2421 | 2437 | try func.store(.{ .stack = {} }, msb, Type.u64, 8 + lhs.offset()); |
| 2422 | 2438 | return; |
| 2423 | 2439 | } else if (abi_size > 16) { |
| 2424 | try func.memcpy(lhs, rhs, .{ .imm32 = @as(u32, @intCast(ty.abiSize(mod))) }); | |
| 2440 | try func.memcpy(lhs, rhs, .{ .imm32 = @as(u32, @intCast(ty.abiSize(pt))) }); | |
| 2425 | 2441 | }, |
| 2426 | 2442 | else => if (abi_size > 8) { |
| 2427 | 2443 | return func.fail("TODO: `store` for type `{}` with abisize `{d}`", .{ |
| 2428 | ty.fmt(func.bin_file.base.comp.module.?), | |
| 2444 | ty.fmt(pt), | |
| 2429 | 2445 | abi_size, |
| 2430 | 2446 | }); |
| 2431 | 2447 | }, |
| ... | ... | @@ -2435,7 +2451,7 @@ fn store(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerE |
| 2435 | 2451 | // into lhs, so we calculate that and emit that instead |
| 2436 | 2452 | try func.lowerToStack(rhs); |
| 2437 | 2453 | |
| 2438 | const valtype = typeToValtype(ty, mod); | |
| 2454 | const valtype = typeToValtype(ty, pt); | |
| 2439 | 2455 | const opcode = buildOpcode(.{ |
| 2440 | 2456 | .valtype1 = valtype, |
| 2441 | 2457 | .width = @as(u8, @intCast(abi_size * 8)), |
| ... | ... | @@ -2447,23 +2463,24 @@ fn store(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerE |
| 2447 | 2463 | Mir.Inst.Tag.fromOpcode(opcode), |
| 2448 | 2464 | .{ |
| 2449 | 2465 | .offset = offset + lhs.offset(), |
| 2450 | .alignment = @intCast(ty.abiAlignment(mod).toByteUnits().?), | |
| 2466 | .alignment = @intCast(ty.abiAlignment(pt).toByteUnits().?), | |
| 2451 | 2467 | }, |
| 2452 | 2468 | ); |
| 2453 | 2469 | } |
| 2454 | 2470 | |
| 2455 | 2471 | fn airLoad(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2456 | const mod = func.bin_file.base.comp.module.?; | |
| 2472 | const pt = func.pt; | |
| 2473 | const mod = pt.zcu; | |
| 2457 | 2474 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2458 | 2475 | const operand = try func.resolveInst(ty_op.operand); |
| 2459 | 2476 | const ty = ty_op.ty.toType(); |
| 2460 | 2477 | const ptr_ty = func.typeOf(ty_op.operand); |
| 2461 | 2478 | const ptr_info = ptr_ty.ptrInfo(mod); |
| 2462 | 2479 | |
| 2463 | if (!ty.hasRuntimeBitsIgnoreComptime(mod)) return func.finishAir(inst, .none, &.{ty_op.operand}); | |
| 2480 | if (!ty.hasRuntimeBitsIgnoreComptime(pt)) return func.finishAir(inst, .none, &.{ty_op.operand}); | |
| 2464 | 2481 | |
| 2465 | 2482 | const result = result: { |
| 2466 | if (isByRef(ty, mod)) { | |
| 2483 | if (isByRef(ty, pt)) { | |
| 2467 | 2484 | const new_local = try func.allocStack(ty); |
| 2468 | 2485 | try func.store(new_local, operand, ty, 0); |
| 2469 | 2486 | break :result new_local; |
| ... | ... | @@ -2476,7 +2493,7 @@ fn airLoad(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2476 | 2493 | |
| 2477 | 2494 | // at this point we have a non-natural alignment, we must |
| 2478 | 2495 | // shift the value to obtain the correct bit. |
| 2479 | const int_elem_ty = try mod.intType(.unsigned, ptr_info.packed_offset.host_size * 8); | |
| 2496 | const int_elem_ty = try pt.intType(.unsigned, ptr_info.packed_offset.host_size * 8); | |
| 2480 | 2497 | const shift_val = if (ptr_info.packed_offset.host_size <= 4) |
| 2481 | 2498 | WValue{ .imm32 = ptr_info.packed_offset.bit_offset } |
| 2482 | 2499 | else if (ptr_info.packed_offset.host_size <= 8) |
| ... | ... | @@ -2496,7 +2513,8 @@ fn airLoad(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2496 | 2513 | /// Loads an operand from the linear memory section. |
| 2497 | 2514 | /// NOTE: Leaves the value on the stack. |
| 2498 | 2515 | fn load(func: *CodeGen, operand: WValue, ty: Type, offset: u32) InnerError!WValue { |
| 2499 | const mod = func.bin_file.base.comp.module.?; | |
| 2516 | const pt = func.pt; | |
| 2517 | const mod = pt.zcu; | |
| 2500 | 2518 | // load local's value from memory by its stack position |
| 2501 | 2519 | try func.emitWValue(operand); |
| 2502 | 2520 | |
| ... | ... | @@ -2507,15 +2525,15 @@ fn load(func: *CodeGen, operand: WValue, ty: Type, offset: u32) InnerError!WValu |
| 2507 | 2525 | try func.mir_extra.appendSlice(func.gpa, &[_]u32{ |
| 2508 | 2526 | std.wasm.simdOpcode(.v128_load), |
| 2509 | 2527 | offset + operand.offset(), |
| 2510 | @intCast(ty.abiAlignment(mod).toByteUnits().?), | |
| 2528 | @intCast(ty.abiAlignment(pt).toByteUnits().?), | |
| 2511 | 2529 | }); |
| 2512 | 2530 | try func.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } }); |
| 2513 | 2531 | return WValue{ .stack = {} }; |
| 2514 | 2532 | } |
| 2515 | 2533 | |
| 2516 | const abi_size: u8 = @intCast(ty.abiSize(mod)); | |
| 2534 | const abi_size: u8 = @intCast(ty.abiSize(pt)); | |
| 2517 | 2535 | const opcode = buildOpcode(.{ |
| 2518 | .valtype1 = typeToValtype(ty, mod), | |
| 2536 | .valtype1 = typeToValtype(ty, pt), | |
| 2519 | 2537 | .width = abi_size * 8, |
| 2520 | 2538 | .op = .load, |
| 2521 | 2539 | .signedness = if (ty.isSignedInt(mod)) .signed else .unsigned, |
| ... | ... | @@ -2525,7 +2543,7 @@ fn load(func: *CodeGen, operand: WValue, ty: Type, offset: u32) InnerError!WValu |
| 2525 | 2543 | Mir.Inst.Tag.fromOpcode(opcode), |
| 2526 | 2544 | .{ |
| 2527 | 2545 | .offset = offset + operand.offset(), |
| 2528 | .alignment = @intCast(ty.abiAlignment(mod).toByteUnits().?), | |
| 2546 | .alignment = @intCast(ty.abiAlignment(pt).toByteUnits().?), | |
| 2529 | 2547 | }, |
| 2530 | 2548 | ); |
| 2531 | 2549 | |
| ... | ... | @@ -2533,13 +2551,14 @@ fn load(func: *CodeGen, operand: WValue, ty: Type, offset: u32) InnerError!WValu |
| 2533 | 2551 | } |
| 2534 | 2552 | |
| 2535 | 2553 | fn airArg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2536 | const mod = func.bin_file.base.comp.module.?; | |
| 2554 | const pt = func.pt; | |
| 2555 | const mod = pt.zcu; | |
| 2537 | 2556 | const arg_index = func.arg_index; |
| 2538 | 2557 | const arg = func.args[arg_index]; |
| 2539 | 2558 | const cc = mod.typeToFunc(func.decl.typeOf(mod)).?.cc; |
| 2540 | 2559 | const arg_ty = func.typeOfIndex(inst); |
| 2541 | 2560 | if (cc == .C) { |
| 2542 | const arg_classes = abi.classifyType(arg_ty, mod); | |
| 2561 | const arg_classes = abi.classifyType(arg_ty, pt); | |
| 2543 | 2562 | for (arg_classes) |class| { |
| 2544 | 2563 | if (class != .none) { |
| 2545 | 2564 | func.arg_index += 1; |
| ... | ... | @@ -2552,7 +2571,7 @@ fn airArg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2552 | 2571 | if (arg_ty.zigTypeTag(mod) != .Int and arg_ty.zigTypeTag(mod) != .Float) { |
| 2553 | 2572 | return func.fail( |
| 2554 | 2573 | "TODO: Implement C-ABI argument for type '{}'", |
| 2555 | .{arg_ty.fmt(func.bin_file.base.comp.module.?)}, | |
| 2574 | .{arg_ty.fmt(pt)}, | |
| 2556 | 2575 | ); |
| 2557 | 2576 | } |
| 2558 | 2577 | const result = try func.allocStack(arg_ty); |
| ... | ... | @@ -2579,7 +2598,7 @@ fn airArg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2579 | 2598 | } |
| 2580 | 2599 | |
| 2581 | 2600 | fn airBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 2582 | const mod = func.bin_file.base.comp.module.?; | |
| 2601 | const pt = func.pt; | |
| 2583 | 2602 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2584 | 2603 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2585 | 2604 | const rhs = try func.resolveInst(bin_op.rhs); |
| ... | ... | @@ -2593,10 +2612,10 @@ fn airBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 2593 | 2612 | // For big integers we can ignore this as we will call into compiler-rt which handles this. |
| 2594 | 2613 | const result = switch (op) { |
| 2595 | 2614 | .shr, .shl => res: { |
| 2596 | const lhs_wasm_bits = toWasmBits(@as(u16, @intCast(lhs_ty.bitSize(mod)))) orelse { | |
| 2615 | const lhs_wasm_bits = toWasmBits(@intCast(lhs_ty.bitSize(pt))) orelse { | |
| 2597 | 2616 | return func.fail("TODO: implement '{s}' for types larger than 128 bits", .{@tagName(op)}); |
| 2598 | 2617 | }; |
| 2599 | const rhs_wasm_bits = toWasmBits(@as(u16, @intCast(rhs_ty.bitSize(mod)))).?; | |
| 2618 | const rhs_wasm_bits = toWasmBits(@intCast(rhs_ty.bitSize(pt))).?; | |
| 2600 | 2619 | const new_rhs = if (lhs_wasm_bits != rhs_wasm_bits and lhs_wasm_bits != 128) blk: { |
| 2601 | 2620 | const tmp = try func.intcast(rhs, rhs_ty, lhs_ty); |
| 2602 | 2621 | break :blk try tmp.toLocal(func, lhs_ty); |
| ... | ... | @@ -2616,7 +2635,8 @@ fn airBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 2616 | 2635 | /// Performs a binary operation on the given `WValue`'s |
| 2617 | 2636 | /// NOTE: THis leaves the value on top of the stack. |
| 2618 | 2637 | fn binOp(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError!WValue { |
| 2619 | const mod = func.bin_file.base.comp.module.?; | |
| 2638 | const pt = func.pt; | |
| 2639 | const mod = pt.zcu; | |
| 2620 | 2640 | assert(!(lhs != .stack and rhs == .stack)); |
| 2621 | 2641 | |
| 2622 | 2642 | if (ty.isAnyFloat()) { |
| ... | ... | @@ -2624,20 +2644,20 @@ fn binOp(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError! |
| 2624 | 2644 | return func.floatOp(float_op, ty, &.{ lhs, rhs }); |
| 2625 | 2645 | } |
| 2626 | 2646 | |
| 2627 | if (isByRef(ty, mod)) { | |
| 2647 | if (isByRef(ty, pt)) { | |
| 2628 | 2648 | if (ty.zigTypeTag(mod) == .Int) { |
| 2629 | 2649 | return func.binOpBigInt(lhs, rhs, ty, op); |
| 2630 | 2650 | } else { |
| 2631 | 2651 | return func.fail( |
| 2632 | 2652 | "TODO: Implement binary operation for type: {}", |
| 2633 | .{ty.fmt(func.bin_file.base.comp.module.?)}, | |
| 2653 | .{ty.fmt(pt)}, | |
| 2634 | 2654 | ); |
| 2635 | 2655 | } |
| 2636 | 2656 | } |
| 2637 | 2657 | |
| 2638 | 2658 | const opcode: wasm.Opcode = buildOpcode(.{ |
| 2639 | 2659 | .op = op, |
| 2640 | .valtype1 = typeToValtype(ty, mod), | |
| 2660 | .valtype1 = typeToValtype(ty, pt), | |
| 2641 | 2661 | .signedness = if (ty.isSignedInt(mod)) .signed else .unsigned, |
| 2642 | 2662 | }); |
| 2643 | 2663 | try func.emitWValue(lhs); |
| ... | ... | @@ -2649,7 +2669,8 @@ fn binOp(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError! |
| 2649 | 2669 | } |
| 2650 | 2670 | |
| 2651 | 2671 | fn binOpBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError!WValue { |
| 2652 | const mod = func.bin_file.base.comp.module.?; | |
| 2672 | const pt = func.pt; | |
| 2673 | const mod = pt.zcu; | |
| 2653 | 2674 | const int_info = ty.intInfo(mod); |
| 2654 | 2675 | if (int_info.bits > 128) { |
| 2655 | 2676 | return func.fail("TODO: Implement binary operation for big integers larger than 128 bits", .{}); |
| ... | ... | @@ -2785,7 +2806,8 @@ const FloatOp = enum { |
| 2785 | 2806 | }; |
| 2786 | 2807 | |
| 2787 | 2808 | fn airAbs(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2788 | const mod = func.bin_file.base.comp.module.?; | |
| 2809 | const pt = func.pt; | |
| 2810 | const mod = pt.zcu; | |
| 2789 | 2811 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2790 | 2812 | const operand = try func.resolveInst(ty_op.operand); |
| 2791 | 2813 | const ty = func.typeOf(ty_op.operand); |
| ... | ... | @@ -2793,7 +2815,7 @@ fn airAbs(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2793 | 2815 | |
| 2794 | 2816 | switch (scalar_ty.zigTypeTag(mod)) { |
| 2795 | 2817 | .Int => if (ty.zigTypeTag(mod) == .Vector) { |
| 2796 | return func.fail("TODO implement airAbs for {}", .{ty.fmt(mod)}); | |
| 2818 | return func.fail("TODO implement airAbs for {}", .{ty.fmt(pt)}); | |
| 2797 | 2819 | } else { |
| 2798 | 2820 | const int_bits = ty.intInfo(mod).bits; |
| 2799 | 2821 | const wasm_bits = toWasmBits(int_bits) orelse { |
| ... | ... | @@ -2877,7 +2899,8 @@ fn airUnaryFloatOp(func: *CodeGen, inst: Air.Inst.Index, op: FloatOp) InnerError |
| 2877 | 2899 | } |
| 2878 | 2900 | |
| 2879 | 2901 | fn floatOp(func: *CodeGen, float_op: FloatOp, ty: Type, args: []const WValue) InnerError!WValue { |
| 2880 | const mod = func.bin_file.base.comp.module.?; | |
| 2902 | const pt = func.pt; | |
| 2903 | const mod = pt.zcu; | |
| 2881 | 2904 | if (ty.zigTypeTag(mod) == .Vector) { |
| 2882 | 2905 | return func.fail("TODO: Implement floatOps for vectors", .{}); |
| 2883 | 2906 | } |
| ... | ... | @@ -2893,7 +2916,7 @@ fn floatOp(func: *CodeGen, float_op: FloatOp, ty: Type, args: []const WValue) In |
| 2893 | 2916 | for (args) |operand| { |
| 2894 | 2917 | try func.emitWValue(operand); |
| 2895 | 2918 | } |
| 2896 | const opcode = buildOpcode(.{ .op = op, .valtype1 = typeToValtype(ty, mod) }); | |
| 2919 | const opcode = buildOpcode(.{ .op = op, .valtype1 = typeToValtype(ty, pt) }); | |
| 2897 | 2920 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 2898 | 2921 | return .stack; |
| 2899 | 2922 | } |
| ... | ... | @@ -2983,7 +3006,8 @@ fn floatNeg(func: *CodeGen, ty: Type, arg: WValue) InnerError!WValue { |
| 2983 | 3006 | } |
| 2984 | 3007 | |
| 2985 | 3008 | fn airWrapBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 2986 | const mod = func.bin_file.base.comp.module.?; | |
| 3009 | const pt = func.pt; | |
| 3010 | const mod = pt.zcu; | |
| 2987 | 3011 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 2988 | 3012 | |
| 2989 | 3013 | const lhs = try func.resolveInst(bin_op.lhs); |
| ... | ... | @@ -3002,10 +3026,10 @@ fn airWrapBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 3002 | 3026 | // For big integers we can ignore this as we will call into compiler-rt which handles this. |
| 3003 | 3027 | const result = switch (op) { |
| 3004 | 3028 | .shr, .shl => res: { |
| 3005 | const lhs_wasm_bits = toWasmBits(@as(u16, @intCast(lhs_ty.bitSize(mod)))) orelse { | |
| 3029 | const lhs_wasm_bits = toWasmBits(@intCast(lhs_ty.bitSize(pt))) orelse { | |
| 3006 | 3030 | return func.fail("TODO: implement '{s}' for types larger than 128 bits", .{@tagName(op)}); |
| 3007 | 3031 | }; |
| 3008 | const rhs_wasm_bits = toWasmBits(@as(u16, @intCast(rhs_ty.bitSize(mod)))).?; | |
| 3032 | const rhs_wasm_bits = toWasmBits(@intCast(rhs_ty.bitSize(pt))).?; | |
| 3009 | 3033 | const new_rhs = if (lhs_wasm_bits != rhs_wasm_bits and lhs_wasm_bits != 128) blk: { |
| 3010 | 3034 | const tmp = try func.intcast(rhs, rhs_ty, lhs_ty); |
| 3011 | 3035 | break :blk try tmp.toLocal(func, lhs_ty); |
| ... | ... | @@ -3034,9 +3058,10 @@ fn wrapBinOp(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerEr |
| 3034 | 3058 | /// Asserts `Type` is <= 128 bits. |
| 3035 | 3059 | /// NOTE: When the Type is <= 64 bits, leaves the value on top of the stack, if wrapping was needed. |
| 3036 | 3060 | fn wrapOperand(func: *CodeGen, operand: WValue, ty: Type) InnerError!WValue { |
| 3037 | const mod = func.bin_file.base.comp.module.?; | |
| 3038 | assert(ty.abiSize(mod) <= 16); | |
| 3039 | const int_bits = @as(u16, @intCast(ty.bitSize(mod))); // TODO use ty.intInfo(mod).bits | |
| 3061 | const pt = func.pt; | |
| 3062 | const mod = pt.zcu; | |
| 3063 | assert(ty.abiSize(pt) <= 16); | |
| 3064 | const int_bits: u16 = @intCast(ty.bitSize(pt)); // TODO use ty.intInfo(mod).bits | |
| 3040 | 3065 | const wasm_bits = toWasmBits(int_bits) orelse { |
| 3041 | 3066 | return func.fail("TODO: Implement wrapOperand for bitsize '{d}'", .{int_bits}); |
| 3042 | 3067 | }; |
| ... | ... | @@ -3098,13 +3123,14 @@ fn wrapOperand(func: *CodeGen, operand: WValue, ty: Type) InnerError!WValue { |
| 3098 | 3123 | } |
| 3099 | 3124 | |
| 3100 | 3125 | fn lowerPtr(func: *CodeGen, ptr_val: InternPool.Index, prev_offset: u64) InnerError!WValue { |
| 3101 | const zcu = func.bin_file.base.comp.module.?; | |
| 3126 | const pt = func.pt; | |
| 3127 | const zcu = pt.zcu; | |
| 3102 | 3128 | const ptr = zcu.intern_pool.indexToKey(ptr_val).ptr; |
| 3103 | 3129 | const offset: u64 = prev_offset + ptr.byte_offset; |
| 3104 | 3130 | return switch (ptr.base_addr) { |
| 3105 | 3131 | .decl => |decl| return func.lowerDeclRefValue(decl, @intCast(offset)), |
| 3106 | 3132 | .anon_decl => |ad| return func.lowerAnonDeclRef(ad, @intCast(offset)), |
| 3107 | .int => return func.lowerConstant(try zcu.intValue(Type.usize, offset), Type.usize), | |
| 3133 | .int => return func.lowerConstant(try pt.intValue(Type.usize, offset), Type.usize), | |
| 3108 | 3134 | .eu_payload => return func.fail("Wasm TODO: lower error union payload pointer", .{}), |
| 3109 | 3135 | .opt_payload => |opt_ptr| return func.lowerPtr(opt_ptr, offset), |
| 3110 | 3136 | .field => |field| { |
| ... | ... | @@ -3120,13 +3146,13 @@ fn lowerPtr(func: *CodeGen, ptr_val: InternPool.Index, prev_offset: u64) InnerEr |
| 3120 | 3146 | }; |
| 3121 | 3147 | }, |
| 3122 | 3148 | .Struct => switch (base_ty.containerLayout(zcu)) { |
| 3123 | .auto => base_ty.structFieldOffset(@intCast(field.index), zcu), | |
| 3149 | .auto => base_ty.structFieldOffset(@intCast(field.index), pt), | |
| 3124 | 3150 | .@"extern", .@"packed" => unreachable, |
| 3125 | 3151 | }, |
| 3126 | 3152 | .Union => switch (base_ty.containerLayout(zcu)) { |
| 3127 | 3153 | .auto => off: { |
| 3128 | 3154 | // Keep in sync with the `un` case of `generateSymbol`. |
| 3129 | const layout = base_ty.unionGetLayout(zcu); | |
| 3155 | const layout = base_ty.unionGetLayout(pt); | |
| 3130 | 3156 | if (layout.payload_size == 0) break :off 0; |
| 3131 | 3157 | if (layout.tag_size == 0) break :off 0; |
| 3132 | 3158 | if (layout.tag_align.compare(.gte, layout.payload_align)) { |
| ... | ... | @@ -3152,17 +3178,18 @@ fn lowerAnonDeclRef( |
| 3152 | 3178 | anon_decl: InternPool.Key.Ptr.BaseAddr.AnonDecl, |
| 3153 | 3179 | offset: u32, |
| 3154 | 3180 | ) InnerError!WValue { |
| 3155 | const mod = func.bin_file.base.comp.module.?; | |
| 3181 | const pt = func.pt; | |
| 3182 | const mod = pt.zcu; | |
| 3156 | 3183 | const decl_val = anon_decl.val; |
| 3157 | 3184 | const ty = Type.fromInterned(mod.intern_pool.typeOf(decl_val)); |
| 3158 | 3185 | |
| 3159 | 3186 | const is_fn_body = ty.zigTypeTag(mod) == .Fn; |
| 3160 | if (!is_fn_body and !ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 3187 | if (!is_fn_body and !ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3161 | 3188 | return WValue{ .imm32 = 0xaaaaaaaa }; |
| 3162 | 3189 | } |
| 3163 | 3190 | |
| 3164 | 3191 | const decl_align = mod.intern_pool.indexToKey(anon_decl.orig_ty).ptr_type.flags.alignment; |
| 3165 | const res = try func.bin_file.lowerAnonDecl(decl_val, decl_align, func.decl.navSrcLoc(mod)); | |
| 3192 | const res = try func.bin_file.lowerAnonDecl(pt, decl_val, decl_align, func.decl.navSrcLoc(mod)); | |
| 3166 | 3193 | switch (res) { |
| 3167 | 3194 | .ok => {}, |
| 3168 | 3195 | .fail => |em| { |
| ... | ... | @@ -3180,7 +3207,8 @@ fn lowerAnonDeclRef( |
| 3180 | 3207 | } |
| 3181 | 3208 | |
| 3182 | 3209 | fn lowerDeclRefValue(func: *CodeGen, decl_index: InternPool.DeclIndex, offset: u32) InnerError!WValue { |
| 3183 | const mod = func.bin_file.base.comp.module.?; | |
| 3210 | const pt = func.pt; | |
| 3211 | const mod = pt.zcu; | |
| 3184 | 3212 | |
| 3185 | 3213 | const decl = mod.declPtr(decl_index); |
| 3186 | 3214 | // check if decl is an alias to a function, in which case we |
| ... | ... | @@ -3195,7 +3223,7 @@ fn lowerDeclRefValue(func: *CodeGen, decl_index: InternPool.DeclIndex, offset: u |
| 3195 | 3223 | } |
| 3196 | 3224 | } |
| 3197 | 3225 | const decl_ty = decl.typeOf(mod); |
| 3198 | if (decl_ty.zigTypeTag(mod) != .Fn and !decl_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 3226 | if (decl_ty.zigTypeTag(mod) != .Fn and !decl_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3199 | 3227 | return WValue{ .imm32 = 0xaaaaaaaa }; |
| 3200 | 3228 | } |
| 3201 | 3229 | |
| ... | ... | @@ -3212,8 +3240,9 @@ fn lowerDeclRefValue(func: *CodeGen, decl_index: InternPool.DeclIndex, offset: u |
| 3212 | 3240 | |
| 3213 | 3241 | /// Asserts that `isByRef` returns `false` for `ty`. |
| 3214 | 3242 | fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 3215 | const mod = func.bin_file.base.comp.module.?; | |
| 3216 | assert(!isByRef(ty, mod)); | |
| 3243 | const pt = func.pt; | |
| 3244 | const mod = pt.zcu; | |
| 3245 | assert(!isByRef(ty, pt)); | |
| 3217 | 3246 | const ip = &mod.intern_pool; |
| 3218 | 3247 | if (val.isUndefDeep(mod)) return func.emitUndefined(ty); |
| 3219 | 3248 | |
| ... | ... | @@ -3261,13 +3290,13 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 3261 | 3290 | const int_info = ty.intInfo(mod); |
| 3262 | 3291 | switch (int_info.signedness) { |
| 3263 | 3292 | .signed => switch (int_info.bits) { |
| 3264 | 0...32 => return WValue{ .imm32 = @bitCast(@as(i32, @intCast(val.toSignedInt(mod)))) }, | |
| 3265 | 33...64 => return WValue{ .imm64 = @bitCast(val.toSignedInt(mod)) }, | |
| 3293 | 0...32 => return WValue{ .imm32 = @bitCast(@as(i32, @intCast(val.toSignedInt(pt)))) }, | |
| 3294 | 33...64 => return WValue{ .imm64 = @bitCast(val.toSignedInt(pt)) }, | |
| 3266 | 3295 | else => unreachable, |
| 3267 | 3296 | }, |
| 3268 | 3297 | .unsigned => switch (int_info.bits) { |
| 3269 | 0...32 => return WValue{ .imm32 = @intCast(val.toUnsignedInt(mod)) }, | |
| 3270 | 33...64 => return WValue{ .imm64 = val.toUnsignedInt(mod) }, | |
| 3298 | 0...32 => return WValue{ .imm32 = @intCast(val.toUnsignedInt(pt)) }, | |
| 3299 | 33...64 => return WValue{ .imm64 = val.toUnsignedInt(pt) }, | |
| 3271 | 3300 | else => unreachable, |
| 3272 | 3301 | }, |
| 3273 | 3302 | } |
| ... | ... | @@ -3277,22 +3306,22 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 3277 | 3306 | return WValue{ .imm32 = int }; |
| 3278 | 3307 | }, |
| 3279 | 3308 | .error_union => |error_union| { |
| 3280 | const err_int_ty = try mod.errorIntType(); | |
| 3309 | const err_int_ty = try pt.errorIntType(); | |
| 3281 | 3310 | const err_ty, const err_val = switch (error_union.val) { |
| 3282 | 3311 | .err_name => |err_name| .{ |
| 3283 | 3312 | ty.errorUnionSet(mod), |
| 3284 | Value.fromInterned((try mod.intern(.{ .err = .{ | |
| 3313 | Value.fromInterned(try pt.intern(.{ .err = .{ | |
| 3285 | 3314 | .ty = ty.errorUnionSet(mod).toIntern(), |
| 3286 | 3315 | .name = err_name, |
| 3287 | } }))), | |
| 3316 | } })), | |
| 3288 | 3317 | }, |
| 3289 | 3318 | .payload => .{ |
| 3290 | 3319 | err_int_ty, |
| 3291 | try mod.intValue(err_int_ty, 0), | |
| 3320 | try pt.intValue(err_int_ty, 0), | |
| 3292 | 3321 | }, |
| 3293 | 3322 | }; |
| 3294 | 3323 | const payload_type = ty.errorUnionPayload(mod); |
| 3295 | if (!payload_type.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 3324 | if (!payload_type.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3296 | 3325 | // We use the error type directly as the type. |
| 3297 | 3326 | return func.lowerConstant(err_val, err_ty); |
| 3298 | 3327 | } |
| ... | ... | @@ -3318,7 +3347,7 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 3318 | 3347 | .field => |base_index| ptr = ip.indexToKey(base_index.base).ptr, |
| 3319 | 3348 | .arr_elem, .comptime_field, .comptime_alloc => unreachable, |
| 3320 | 3349 | }; |
| 3321 | return .{ .memory = try func.bin_file.lowerUnnamedConst(val, owner_decl) }; | |
| 3350 | return .{ .memory = try func.bin_file.lowerUnnamedConst(pt, val, owner_decl) }; | |
| 3322 | 3351 | }, |
| 3323 | 3352 | .ptr => return func.lowerPtr(val.toIntern(), 0), |
| 3324 | 3353 | .opt => if (ty.optionalReprIsPayload(mod)) { |
| ... | ... | @@ -3332,11 +3361,11 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 3332 | 3361 | return WValue{ .imm32 = @intFromBool(!val.isNull(mod)) }; |
| 3333 | 3362 | }, |
| 3334 | 3363 | .aggregate => switch (ip.indexToKey(ty.ip_index)) { |
| 3335 | .array_type => return func.fail("Wasm TODO: LowerConstant for {}", .{ty.fmt(mod)}), | |
| 3364 | .array_type => return func.fail("Wasm TODO: LowerConstant for {}", .{ty.fmt(pt)}), | |
| 3336 | 3365 | .vector_type => { |
| 3337 | assert(determineSimdStoreStrategy(ty, mod) == .direct); | |
| 3366 | assert(determineSimdStoreStrategy(ty, pt) == .direct); | |
| 3338 | 3367 | var buf: [16]u8 = undefined; |
| 3339 | val.writeToMemory(ty, mod, &buf) catch unreachable; | |
| 3368 | val.writeToMemory(ty, pt, &buf) catch unreachable; | |
| 3340 | 3369 | return func.storeSimdImmd(buf); |
| 3341 | 3370 | }, |
| 3342 | 3371 | .struct_type => { |
| ... | ... | @@ -3345,9 +3374,9 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 3345 | 3374 | // are by-ref types. |
| 3346 | 3375 | assert(struct_type.layout == .@"packed"); |
| 3347 | 3376 | var buf: [8]u8 = .{0} ** 8; // zero the buffer so we do not read 0xaa as integer |
| 3348 | val.writeToPackedMemory(ty, mod, &buf, 0) catch unreachable; | |
| 3377 | val.writeToPackedMemory(ty, pt, &buf, 0) catch unreachable; | |
| 3349 | 3378 | const backing_int_ty = Type.fromInterned(struct_type.backingIntType(ip).*); |
| 3350 | const int_val = try mod.intValue( | |
| 3379 | const int_val = try pt.intValue( | |
| 3351 | 3380 | backing_int_ty, |
| 3352 | 3381 | mem.readInt(u64, &buf, .little), |
| 3353 | 3382 | ); |
| ... | ... | @@ -3358,7 +3387,7 @@ fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 3358 | 3387 | .un => |un| { |
| 3359 | 3388 | // in this case we have a packed union which will not be passed by reference. |
| 3360 | 3389 | const constant_ty = if (un.tag == .none) |
| 3361 | try ty.unionBackingType(mod) | |
| 3390 | try ty.unionBackingType(pt) | |
| 3362 | 3391 | else field_ty: { |
| 3363 | 3392 | const union_obj = mod.typeToUnion(ty).?; |
| 3364 | 3393 | const field_index = mod.unionTagFieldIndex(union_obj, Value.fromInterned(un.tag)).?; |
| ... | ... | @@ -3379,7 +3408,8 @@ fn storeSimdImmd(func: *CodeGen, value: [16]u8) !WValue { |
| 3379 | 3408 | } |
| 3380 | 3409 | |
| 3381 | 3410 | fn emitUndefined(func: *CodeGen, ty: Type) InnerError!WValue { |
| 3382 | const mod = func.bin_file.base.comp.module.?; | |
| 3411 | const pt = func.pt; | |
| 3412 | const mod = pt.zcu; | |
| 3383 | 3413 | const ip = &mod.intern_pool; |
| 3384 | 3414 | switch (ty.zigTypeTag(mod)) { |
| 3385 | 3415 | .Bool, .ErrorSet => return WValue{ .imm32 = 0xaaaaaaaa }, |
| ... | ... | @@ -3421,15 +3451,16 @@ fn emitUndefined(func: *CodeGen, ty: Type) InnerError!WValue { |
| 3421 | 3451 | /// It's illegal to provide a value with a type that cannot be represented |
| 3422 | 3452 | /// as an integer value. |
| 3423 | 3453 | fn valueAsI32(func: *const CodeGen, val: Value, ty: Type) i32 { |
| 3424 | const mod = func.bin_file.base.comp.module.?; | |
| 3454 | const pt = func.pt; | |
| 3455 | const mod = pt.zcu; | |
| 3425 | 3456 | |
| 3426 | 3457 | switch (val.ip_index) { |
| 3427 | 3458 | .none => {}, |
| 3428 | 3459 | .bool_true => return 1, |
| 3429 | 3460 | .bool_false => return 0, |
| 3430 | 3461 | else => return switch (mod.intern_pool.indexToKey(val.ip_index)) { |
| 3431 | .enum_tag => |enum_tag| intIndexAsI32(&mod.intern_pool, enum_tag.int, mod), | |
| 3432 | .int => |int| intStorageAsI32(int.storage, mod), | |
| 3462 | .enum_tag => |enum_tag| intIndexAsI32(&mod.intern_pool, enum_tag.int, pt), | |
| 3463 | .int => |int| intStorageAsI32(int.storage, pt), | |
| 3433 | 3464 | .ptr => |ptr| { |
| 3434 | 3465 | assert(ptr.base_addr == .int); |
| 3435 | 3466 | return @intCast(ptr.byte_offset); |
| ... | ... | @@ -3445,17 +3476,17 @@ fn valueAsI32(func: *const CodeGen, val: Value, ty: Type) i32 { |
| 3445 | 3476 | }; |
| 3446 | 3477 | } |
| 3447 | 3478 | |
| 3448 | fn intIndexAsI32(ip: *const InternPool, int: InternPool.Index, mod: *Zcu) i32 { | |
| 3449 | return intStorageAsI32(ip.indexToKey(int).int.storage, mod); | |
| 3479 | fn intIndexAsI32(ip: *const InternPool, int: InternPool.Index, pt: Zcu.PerThread) i32 { | |
| 3480 | return intStorageAsI32(ip.indexToKey(int).int.storage, pt); | |
| 3450 | 3481 | } |
| 3451 | 3482 | |
| 3452 | fn intStorageAsI32(storage: InternPool.Key.Int.Storage, mod: *Zcu) i32 { | |
| 3483 | fn intStorageAsI32(storage: InternPool.Key.Int.Storage, pt: Zcu.PerThread) i32 { | |
| 3453 | 3484 | return switch (storage) { |
| 3454 | 3485 | .i64 => |x| @as(i32, @intCast(x)), |
| 3455 | 3486 | .u64 => |x| @as(i32, @bitCast(@as(u32, @intCast(x)))), |
| 3456 | 3487 | .big_int => unreachable, |
| 3457 | .lazy_align => |ty| @as(i32, @bitCast(@as(u32, @intCast(Type.fromInterned(ty).abiAlignment(mod).toByteUnits() orelse 0)))), | |
| 3458 | .lazy_size => |ty| @as(i32, @bitCast(@as(u32, @intCast(Type.fromInterned(ty).abiSize(mod))))), | |
| 3488 | .lazy_align => |ty| @as(i32, @bitCast(@as(u32, @intCast(Type.fromInterned(ty).abiAlignment(pt).toByteUnits() orelse 0)))), | |
| 3489 | .lazy_size => |ty| @as(i32, @bitCast(@as(u32, @intCast(Type.fromInterned(ty).abiSize(pt))))), | |
| 3459 | 3490 | }; |
| 3460 | 3491 | } |
| 3461 | 3492 | |
| ... | ... | @@ -3466,12 +3497,12 @@ fn airBlock(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3466 | 3497 | } |
| 3467 | 3498 | |
| 3468 | 3499 | fn lowerBlock(func: *CodeGen, inst: Air.Inst.Index, block_ty: Type, body: []const Air.Inst.Index) InnerError!void { |
| 3469 | const mod = func.bin_file.base.comp.module.?; | |
| 3470 | const wasm_block_ty = genBlockType(block_ty, mod); | |
| 3500 | const pt = func.pt; | |
| 3501 | const wasm_block_ty = genBlockType(block_ty, pt); | |
| 3471 | 3502 | |
| 3472 | 3503 | // if wasm_block_ty is non-empty, we create a register to store the temporary value |
| 3473 | 3504 | const block_result: WValue = if (wasm_block_ty != wasm.block_empty) blk: { |
| 3474 | const ty: Type = if (isByRef(block_ty, mod)) Type.u32 else block_ty; | |
| 3505 | const ty: Type = if (isByRef(block_ty, pt)) Type.u32 else block_ty; | |
| 3475 | 3506 | break :blk try func.ensureAllocLocal(ty); // make sure it's a clean local as it may never get overwritten |
| 3476 | 3507 | } else WValue.none; |
| 3477 | 3508 | |
| ... | ... | @@ -3583,10 +3614,11 @@ fn airCmp(func: *CodeGen, inst: Air.Inst.Index, op: std.math.CompareOperator) In |
| 3583 | 3614 | /// NOTE: This leaves the result on top of the stack, rather than a new local. |
| 3584 | 3615 | fn cmp(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: std.math.CompareOperator) InnerError!WValue { |
| 3585 | 3616 | assert(!(lhs != .stack and rhs == .stack)); |
| 3586 | const mod = func.bin_file.base.comp.module.?; | |
| 3617 | const pt = func.pt; | |
| 3618 | const mod = pt.zcu; | |
| 3587 | 3619 | if (ty.zigTypeTag(mod) == .Optional and !ty.optionalReprIsPayload(mod)) { |
| 3588 | 3620 | const payload_ty = ty.optionalChild(mod); |
| 3589 | if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 3621 | if (payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3590 | 3622 | // When we hit this case, we must check the value of optionals |
| 3591 | 3623 | // that are not pointers. This means first checking against non-null for |
| 3592 | 3624 | // both lhs and rhs, as well as checking the payload are matching of lhs and rhs |
| ... | ... | @@ -3594,7 +3626,7 @@ fn cmp(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: std.math.CompareO |
| 3594 | 3626 | } |
| 3595 | 3627 | } else if (ty.isAnyFloat()) { |
| 3596 | 3628 | return func.cmpFloat(ty, lhs, rhs, op); |
| 3597 | } else if (isByRef(ty, mod)) { | |
| 3629 | } else if (isByRef(ty, pt)) { | |
| 3598 | 3630 | return func.cmpBigInt(lhs, rhs, ty, op); |
| 3599 | 3631 | } |
| 3600 | 3632 | |
| ... | ... | @@ -3612,7 +3644,7 @@ fn cmp(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: std.math.CompareO |
| 3612 | 3644 | try func.lowerToStack(rhs); |
| 3613 | 3645 | |
| 3614 | 3646 | const opcode: wasm.Opcode = buildOpcode(.{ |
| 3615 | .valtype1 = typeToValtype(ty, mod), | |
| 3647 | .valtype1 = typeToValtype(ty, pt), | |
| 3616 | 3648 | .op = switch (op) { |
| 3617 | 3649 | .lt => .lt, |
| 3618 | 3650 | .lte => .le, |
| ... | ... | @@ -3683,8 +3715,8 @@ fn airCmpLtErrorsLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3683 | 3715 | const errors_len = WValue{ .memory = @intFromEnum(sym_index) }; |
| 3684 | 3716 | |
| 3685 | 3717 | try func.emitWValue(operand); |
| 3686 | const mod = func.bin_file.base.comp.module.?; | |
| 3687 | const err_int_ty = try mod.errorIntType(); | |
| 3718 | const pt = func.pt; | |
| 3719 | const err_int_ty = try pt.errorIntType(); | |
| 3688 | 3720 | const errors_len_val = try func.load(errors_len, err_int_ty, 0); |
| 3689 | 3721 | const result = try func.cmp(.stack, errors_len_val, err_int_ty, .lt); |
| 3690 | 3722 | |
| ... | ... | @@ -3692,12 +3724,12 @@ fn airCmpLtErrorsLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3692 | 3724 | } |
| 3693 | 3725 | |
| 3694 | 3726 | fn airBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3695 | const mod = func.bin_file.base.comp.module.?; | |
| 3727 | const pt = func.pt; | |
| 3696 | 3728 | const br = func.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 3697 | 3729 | const block = func.blocks.get(br.block_inst).?; |
| 3698 | 3730 | |
| 3699 | 3731 | // if operand has codegen bits we should break with a value |
| 3700 | if (func.typeOf(br.operand).hasRuntimeBitsIgnoreComptime(mod)) { | |
| 3732 | if (func.typeOf(br.operand).hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3701 | 3733 | const operand = try func.resolveInst(br.operand); |
| 3702 | 3734 | try func.lowerToStack(operand); |
| 3703 | 3735 | |
| ... | ... | @@ -3719,7 +3751,8 @@ fn airNot(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3719 | 3751 | |
| 3720 | 3752 | const operand = try func.resolveInst(ty_op.operand); |
| 3721 | 3753 | const operand_ty = func.typeOf(ty_op.operand); |
| 3722 | const mod = func.bin_file.base.comp.module.?; | |
| 3754 | const pt = func.pt; | |
| 3755 | const mod = pt.zcu; | |
| 3723 | 3756 | |
| 3724 | 3757 | const result = result: { |
| 3725 | 3758 | if (operand_ty.zigTypeTag(mod) == .Bool) { |
| ... | ... | @@ -3731,7 +3764,7 @@ fn airNot(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3731 | 3764 | } else { |
| 3732 | 3765 | const int_info = operand_ty.intInfo(mod); |
| 3733 | 3766 | const wasm_bits = toWasmBits(int_info.bits) orelse { |
| 3734 | return func.fail("TODO: Implement binary NOT for {}", .{operand_ty.fmt(mod)}); | |
| 3767 | return func.fail("TODO: Implement binary NOT for {}", .{operand_ty.fmt(pt)}); | |
| 3735 | 3768 | }; |
| 3736 | 3769 | |
| 3737 | 3770 | switch (wasm_bits) { |
| ... | ... | @@ -3798,13 +3831,14 @@ fn airUnreachable(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3798 | 3831 | } |
| 3799 | 3832 | |
| 3800 | 3833 | fn airBitcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3801 | const mod = func.bin_file.base.comp.module.?; | |
| 3834 | const pt = func.pt; | |
| 3835 | const mod = pt.zcu; | |
| 3802 | 3836 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3803 | 3837 | const operand = try func.resolveInst(ty_op.operand); |
| 3804 | 3838 | const wanted_ty = func.typeOfIndex(inst); |
| 3805 | 3839 | const given_ty = func.typeOf(ty_op.operand); |
| 3806 | 3840 | |
| 3807 | const bit_size = given_ty.bitSize(mod); | |
| 3841 | const bit_size = given_ty.bitSize(pt); | |
| 3808 | 3842 | const needs_wrapping = (given_ty.isSignedInt(mod) != wanted_ty.isSignedInt(mod)) and |
| 3809 | 3843 | bit_size != 32 and bit_size != 64 and bit_size != 128; |
| 3810 | 3844 | |
| ... | ... | @@ -3814,7 +3848,7 @@ fn airBitcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3814 | 3848 | break :result try bitcast_result.toLocal(func, wanted_ty); |
| 3815 | 3849 | } |
| 3816 | 3850 | |
| 3817 | if (isByRef(given_ty, mod) and !isByRef(wanted_ty, mod)) { | |
| 3851 | if (isByRef(given_ty, pt) and !isByRef(wanted_ty, pt)) { | |
| 3818 | 3852 | const loaded_memory = try func.load(operand, wanted_ty, 0); |
| 3819 | 3853 | if (needs_wrapping) { |
| 3820 | 3854 | break :result try (try func.wrapOperand(loaded_memory, wanted_ty)).toLocal(func, wanted_ty); |
| ... | ... | @@ -3822,7 +3856,7 @@ fn airBitcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3822 | 3856 | break :result try loaded_memory.toLocal(func, wanted_ty); |
| 3823 | 3857 | } |
| 3824 | 3858 | } |
| 3825 | if (!isByRef(given_ty, mod) and isByRef(wanted_ty, mod)) { | |
| 3859 | if (!isByRef(given_ty, pt) and isByRef(wanted_ty, pt)) { | |
| 3826 | 3860 | const stack_memory = try func.allocStack(wanted_ty); |
| 3827 | 3861 | try func.store(stack_memory, operand, given_ty, 0); |
| 3828 | 3862 | if (needs_wrapping) { |
| ... | ... | @@ -3842,17 +3876,18 @@ fn airBitcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3842 | 3876 | } |
| 3843 | 3877 | |
| 3844 | 3878 | fn bitcast(func: *CodeGen, wanted_ty: Type, given_ty: Type, operand: WValue) InnerError!WValue { |
| 3845 | const mod = func.bin_file.base.comp.module.?; | |
| 3879 | const pt = func.pt; | |
| 3880 | const mod = pt.zcu; | |
| 3846 | 3881 | // if we bitcast a float to or from an integer we must use the 'reinterpret' instruction |
| 3847 | 3882 | if (!(wanted_ty.isAnyFloat() or given_ty.isAnyFloat())) return operand; |
| 3848 | 3883 | if (wanted_ty.ip_index == .f16_type or given_ty.ip_index == .f16_type) return operand; |
| 3849 | if (wanted_ty.bitSize(mod) > 64) return operand; | |
| 3884 | if (wanted_ty.bitSize(pt) > 64) return operand; | |
| 3850 | 3885 | assert((wanted_ty.isInt(mod) and given_ty.isAnyFloat()) or (wanted_ty.isAnyFloat() and given_ty.isInt(mod))); |
| 3851 | 3886 | |
| 3852 | 3887 | const opcode = buildOpcode(.{ |
| 3853 | 3888 | .op = .reinterpret, |
| 3854 | .valtype1 = typeToValtype(wanted_ty, mod), | |
| 3855 | .valtype2 = typeToValtype(given_ty, mod), | |
| 3889 | .valtype1 = typeToValtype(wanted_ty, pt), | |
| 3890 | .valtype2 = typeToValtype(given_ty, pt), | |
| 3856 | 3891 | }); |
| 3857 | 3892 | try func.emitWValue(operand); |
| 3858 | 3893 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| ... | ... | @@ -3860,7 +3895,8 @@ fn bitcast(func: *CodeGen, wanted_ty: Type, given_ty: Type, operand: WValue) Inn |
| 3860 | 3895 | } |
| 3861 | 3896 | |
| 3862 | 3897 | fn airStructFieldPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3863 | const mod = func.bin_file.base.comp.module.?; | |
| 3898 | const pt = func.pt; | |
| 3899 | const mod = pt.zcu; | |
| 3864 | 3900 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3865 | 3901 | const extra = func.air.extraData(Air.StructField, ty_pl.payload); |
| 3866 | 3902 | |
| ... | ... | @@ -3872,7 +3908,8 @@ fn airStructFieldPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3872 | 3908 | } |
| 3873 | 3909 | |
| 3874 | 3910 | fn airStructFieldPtrIndex(func: *CodeGen, inst: Air.Inst.Index, index: u32) InnerError!void { |
| 3875 | const mod = func.bin_file.base.comp.module.?; | |
| 3911 | const pt = func.pt; | |
| 3912 | const mod = pt.zcu; | |
| 3876 | 3913 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3877 | 3914 | const struct_ptr = try func.resolveInst(ty_op.operand); |
| 3878 | 3915 | const struct_ptr_ty = func.typeOf(ty_op.operand); |
| ... | ... | @@ -3891,7 +3928,8 @@ fn structFieldPtr( |
| 3891 | 3928 | struct_ty: Type, |
| 3892 | 3929 | index: u32, |
| 3893 | 3930 | ) InnerError!WValue { |
| 3894 | const mod = func.bin_file.base.comp.module.?; | |
| 3931 | const pt = func.pt; | |
| 3932 | const mod = pt.zcu; | |
| 3895 | 3933 | const result_ty = func.typeOfIndex(inst); |
| 3896 | 3934 | const struct_ptr_ty_info = struct_ptr_ty.ptrInfo(mod); |
| 3897 | 3935 | |
| ... | ... | @@ -3902,12 +3940,12 @@ fn structFieldPtr( |
| 3902 | 3940 | break :offset @as(u32, 0); |
| 3903 | 3941 | } |
| 3904 | 3942 | const struct_type = mod.typeToStruct(struct_ty).?; |
| 3905 | break :offset @divExact(mod.structPackedFieldBitOffset(struct_type, index) + struct_ptr_ty_info.packed_offset.bit_offset, 8); | |
| 3943 | break :offset @divExact(pt.structPackedFieldBitOffset(struct_type, index) + struct_ptr_ty_info.packed_offset.bit_offset, 8); | |
| 3906 | 3944 | }, |
| 3907 | 3945 | .Union => 0, |
| 3908 | 3946 | else => unreachable, |
| 3909 | 3947 | }, |
| 3910 | else => struct_ty.structFieldOffset(index, mod), | |
| 3948 | else => struct_ty.structFieldOffset(index, pt), | |
| 3911 | 3949 | }; |
| 3912 | 3950 | // save a load and store when we can simply reuse the operand |
| 3913 | 3951 | if (offset == 0) { |
| ... | ... | @@ -3922,7 +3960,8 @@ fn structFieldPtr( |
| 3922 | 3960 | } |
| 3923 | 3961 | |
| 3924 | 3962 | fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3925 | const mod = func.bin_file.base.comp.module.?; | |
| 3963 | const pt = func.pt; | |
| 3964 | const mod = pt.zcu; | |
| 3926 | 3965 | const ip = &mod.intern_pool; |
| 3927 | 3966 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3928 | 3967 | const struct_field = func.air.extraData(Air.StructField, ty_pl.payload).data; |
| ... | ... | @@ -3931,13 +3970,13 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3931 | 3970 | const operand = try func.resolveInst(struct_field.struct_operand); |
| 3932 | 3971 | const field_index = struct_field.field_index; |
| 3933 | 3972 | const field_ty = struct_ty.structFieldType(field_index, mod); |
| 3934 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) return func.finishAir(inst, .none, &.{struct_field.struct_operand}); | |
| 3973 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) return func.finishAir(inst, .none, &.{struct_field.struct_operand}); | |
| 3935 | 3974 | |
| 3936 | 3975 | const result = switch (struct_ty.containerLayout(mod)) { |
| 3937 | 3976 | .@"packed" => switch (struct_ty.zigTypeTag(mod)) { |
| 3938 | 3977 | .Struct => result: { |
| 3939 | 3978 | const packed_struct = mod.typeToPackedStruct(struct_ty).?; |
| 3940 | const offset = mod.structPackedFieldBitOffset(packed_struct, field_index); | |
| 3979 | const offset = pt.structPackedFieldBitOffset(packed_struct, field_index); | |
| 3941 | 3980 | const backing_ty = Type.fromInterned(packed_struct.backingIntType(ip).*); |
| 3942 | 3981 | const wasm_bits = toWasmBits(backing_ty.intInfo(mod).bits) orelse { |
| 3943 | 3982 | return func.fail("TODO: airStructFieldVal for packed structs larger than 128 bits", .{}); |
| ... | ... | @@ -3956,7 +3995,7 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3956 | 3995 | try func.binOp(operand, const_wvalue, backing_ty, .shr); |
| 3957 | 3996 | |
| 3958 | 3997 | if (field_ty.zigTypeTag(mod) == .Float) { |
| 3959 | const int_type = try mod.intType(.unsigned, @as(u16, @intCast(field_ty.bitSize(mod)))); | |
| 3998 | const int_type = try pt.intType(.unsigned, @as(u16, @intCast(field_ty.bitSize(pt)))); | |
| 3960 | 3999 | const truncated = try func.trunc(shifted_value, int_type, backing_ty); |
| 3961 | 4000 | const bitcasted = try func.bitcast(field_ty, int_type, truncated); |
| 3962 | 4001 | break :result try bitcasted.toLocal(func, field_ty); |
| ... | ... | @@ -3965,7 +4004,7 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3965 | 4004 | // we can simply reuse the operand. |
| 3966 | 4005 | break :result func.reuseOperand(struct_field.struct_operand, operand); |
| 3967 | 4006 | } else if (field_ty.isPtrAtRuntime(mod)) { |
| 3968 | const int_type = try mod.intType(.unsigned, @as(u16, @intCast(field_ty.bitSize(mod)))); | |
| 4007 | const int_type = try pt.intType(.unsigned, @as(u16, @intCast(field_ty.bitSize(pt)))); | |
| 3969 | 4008 | const truncated = try func.trunc(shifted_value, int_type, backing_ty); |
| 3970 | 4009 | break :result try truncated.toLocal(func, field_ty); |
| 3971 | 4010 | } |
| ... | ... | @@ -3973,8 +4012,8 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3973 | 4012 | break :result try truncated.toLocal(func, field_ty); |
| 3974 | 4013 | }, |
| 3975 | 4014 | .Union => result: { |
| 3976 | if (isByRef(struct_ty, mod)) { | |
| 3977 | if (!isByRef(field_ty, mod)) { | |
| 4015 | if (isByRef(struct_ty, pt)) { | |
| 4016 | if (!isByRef(field_ty, pt)) { | |
| 3978 | 4017 | const val = try func.load(operand, field_ty, 0); |
| 3979 | 4018 | break :result try val.toLocal(func, field_ty); |
| 3980 | 4019 | } else { |
| ... | ... | @@ -3984,14 +4023,14 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3984 | 4023 | } |
| 3985 | 4024 | } |
| 3986 | 4025 | |
| 3987 | const union_int_type = try mod.intType(.unsigned, @as(u16, @intCast(struct_ty.bitSize(mod)))); | |
| 4026 | const union_int_type = try pt.intType(.unsigned, @as(u16, @intCast(struct_ty.bitSize(pt)))); | |
| 3988 | 4027 | if (field_ty.zigTypeTag(mod) == .Float) { |
| 3989 | const int_type = try mod.intType(.unsigned, @as(u16, @intCast(field_ty.bitSize(mod)))); | |
| 4028 | const int_type = try pt.intType(.unsigned, @as(u16, @intCast(field_ty.bitSize(pt)))); | |
| 3990 | 4029 | const truncated = try func.trunc(operand, int_type, union_int_type); |
| 3991 | 4030 | const bitcasted = try func.bitcast(field_ty, int_type, truncated); |
| 3992 | 4031 | break :result try bitcasted.toLocal(func, field_ty); |
| 3993 | 4032 | } else if (field_ty.isPtrAtRuntime(mod)) { |
| 3994 | const int_type = try mod.intType(.unsigned, @as(u16, @intCast(field_ty.bitSize(mod)))); | |
| 4033 | const int_type = try pt.intType(.unsigned, @as(u16, @intCast(field_ty.bitSize(pt)))); | |
| 3995 | 4034 | const truncated = try func.trunc(operand, int_type, union_int_type); |
| 3996 | 4035 | break :result try truncated.toLocal(func, field_ty); |
| 3997 | 4036 | } |
| ... | ... | @@ -4001,10 +4040,10 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4001 | 4040 | else => unreachable, |
| 4002 | 4041 | }, |
| 4003 | 4042 | else => result: { |
| 4004 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(field_index, mod)) orelse { | |
| 4005 | return func.fail("Field type '{}' too big to fit into stack frame", .{field_ty.fmt(mod)}); | |
| 4043 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(field_index, pt)) orelse { | |
| 4044 | return func.fail("Field type '{}' too big to fit into stack frame", .{field_ty.fmt(pt)}); | |
| 4006 | 4045 | }; |
| 4007 | if (isByRef(field_ty, mod)) { | |
| 4046 | if (isByRef(field_ty, pt)) { | |
| 4008 | 4047 | switch (operand) { |
| 4009 | 4048 | .stack_offset => |stack_offset| { |
| 4010 | 4049 | break :result WValue{ .stack_offset = .{ .value = stack_offset.value + offset, .references = 1 } }; |
| ... | ... | @@ -4021,7 +4060,8 @@ fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4021 | 4060 | } |
| 4022 | 4061 | |
| 4023 | 4062 | fn airSwitchBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4024 | const mod = func.bin_file.base.comp.module.?; | |
| 4063 | const pt = func.pt; | |
| 4064 | const mod = pt.zcu; | |
| 4025 | 4065 | // result type is always 'noreturn' |
| 4026 | 4066 | const blocktype = wasm.block_empty; |
| 4027 | 4067 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| ... | ... | @@ -4055,7 +4095,7 @@ fn airSwitchBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4055 | 4095 | errdefer func.gpa.free(values); |
| 4056 | 4096 | |
| 4057 | 4097 | for (items, 0..) |ref, i| { |
| 4058 | const item_val = (try func.air.value(ref, mod)).?; | |
| 4098 | const item_val = (try func.air.value(ref, pt)).?; | |
| 4059 | 4099 | const int_val = func.valueAsI32(item_val, target_ty); |
| 4060 | 4100 | if (lowest_maybe == null or int_val < lowest_maybe.?) { |
| 4061 | 4101 | lowest_maybe = int_val; |
| ... | ... | @@ -4078,7 +4118,7 @@ fn airSwitchBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4078 | 4118 | // When the target is an integer size larger than u32, we have no way to use the value |
| 4079 | 4119 | // as an index, therefore we also use an if/else-chain for those cases. |
| 4080 | 4120 | // TODO: Benchmark this to find a proper value, LLVM seems to draw the line at '40~45'. |
| 4081 | const is_sparse = highest - lowest > 50 or target_ty.bitSize(mod) > 32; | |
| 4121 | const is_sparse = highest - lowest > 50 or target_ty.bitSize(pt) > 32; | |
| 4082 | 4122 | |
| 4083 | 4123 | const else_body: []const Air.Inst.Index = @ptrCast(func.air.extra[extra_index..][0..switch_br.data.else_body_len]); |
| 4084 | 4124 | const has_else_body = else_body.len != 0; |
| ... | ... | @@ -4150,7 +4190,7 @@ fn airSwitchBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4150 | 4190 | const val = try func.lowerConstant(case.values[0].value, target_ty); |
| 4151 | 4191 | try func.emitWValue(val); |
| 4152 | 4192 | const opcode = buildOpcode(.{ |
| 4153 | .valtype1 = typeToValtype(target_ty, mod), | |
| 4193 | .valtype1 = typeToValtype(target_ty, pt), | |
| 4154 | 4194 | .op = .ne, // not equal, because we want to jump out of this block if it does not match the condition. |
| 4155 | 4195 | .signedness = signedness, |
| 4156 | 4196 | }); |
| ... | ... | @@ -4164,7 +4204,7 @@ fn airSwitchBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4164 | 4204 | const val = try func.lowerConstant(value.value, target_ty); |
| 4165 | 4205 | try func.emitWValue(val); |
| 4166 | 4206 | const opcode = buildOpcode(.{ |
| 4167 | .valtype1 = typeToValtype(target_ty, mod), | |
| 4207 | .valtype1 = typeToValtype(target_ty, pt), | |
| 4168 | 4208 | .op = .eq, |
| 4169 | 4209 | .signedness = signedness, |
| 4170 | 4210 | }); |
| ... | ... | @@ -4201,7 +4241,8 @@ fn airSwitchBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4201 | 4241 | } |
| 4202 | 4242 | |
| 4203 | 4243 | fn airIsErr(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!void { |
| 4204 | const mod = func.bin_file.base.comp.module.?; | |
| 4244 | const pt = func.pt; | |
| 4245 | const mod = pt.zcu; | |
| 4205 | 4246 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4206 | 4247 | const operand = try func.resolveInst(un_op); |
| 4207 | 4248 | const err_union_ty = func.typeOf(un_op); |
| ... | ... | @@ -4217,10 +4258,10 @@ fn airIsErr(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerErro |
| 4217 | 4258 | } |
| 4218 | 4259 | |
| 4219 | 4260 | try func.emitWValue(operand); |
| 4220 | if (pl_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4261 | if (pl_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4221 | 4262 | try func.addMemArg(.i32_load16_u, .{ |
| 4222 | .offset = operand.offset() + @as(u32, @intCast(errUnionErrorOffset(pl_ty, mod))), | |
| 4223 | .alignment = @intCast(Type.anyerror.abiAlignment(mod).toByteUnits().?), | |
| 4263 | .offset = operand.offset() + @as(u32, @intCast(errUnionErrorOffset(pl_ty, pt))), | |
| 4264 | .alignment = @intCast(Type.anyerror.abiAlignment(pt).toByteUnits().?), | |
| 4224 | 4265 | }); |
| 4225 | 4266 | } |
| 4226 | 4267 | |
| ... | ... | @@ -4236,7 +4277,8 @@ fn airIsErr(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerErro |
| 4236 | 4277 | } |
| 4237 | 4278 | |
| 4238 | 4279 | fn airUnwrapErrUnionPayload(func: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void { |
| 4239 | const mod = func.bin_file.base.comp.module.?; | |
| 4280 | const pt = func.pt; | |
| 4281 | const mod = pt.zcu; | |
| 4240 | 4282 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4241 | 4283 | |
| 4242 | 4284 | const operand = try func.resolveInst(ty_op.operand); |
| ... | ... | @@ -4245,15 +4287,15 @@ fn airUnwrapErrUnionPayload(func: *CodeGen, inst: Air.Inst.Index, op_is_ptr: boo |
| 4245 | 4287 | const payload_ty = err_ty.errorUnionPayload(mod); |
| 4246 | 4288 | |
| 4247 | 4289 | const result = result: { |
| 4248 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4290 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4249 | 4291 | if (op_is_ptr) { |
| 4250 | 4292 | break :result func.reuseOperand(ty_op.operand, operand); |
| 4251 | 4293 | } |
| 4252 | 4294 | break :result WValue{ .none = {} }; |
| 4253 | 4295 | } |
| 4254 | 4296 | |
| 4255 | const pl_offset = @as(u32, @intCast(errUnionPayloadOffset(payload_ty, mod))); | |
| 4256 | if (op_is_ptr or isByRef(payload_ty, mod)) { | |
| 4297 | const pl_offset = @as(u32, @intCast(errUnionPayloadOffset(payload_ty, pt))); | |
| 4298 | if (op_is_ptr or isByRef(payload_ty, pt)) { | |
| 4257 | 4299 | break :result try func.buildPointerOffset(operand, pl_offset, .new); |
| 4258 | 4300 | } |
| 4259 | 4301 | |
| ... | ... | @@ -4264,7 +4306,8 @@ fn airUnwrapErrUnionPayload(func: *CodeGen, inst: Air.Inst.Index, op_is_ptr: boo |
| 4264 | 4306 | } |
| 4265 | 4307 | |
| 4266 | 4308 | fn airUnwrapErrUnionError(func: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void { |
| 4267 | const mod = func.bin_file.base.comp.module.?; | |
| 4309 | const pt = func.pt; | |
| 4310 | const mod = pt.zcu; | |
| 4268 | 4311 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4269 | 4312 | |
| 4270 | 4313 | const operand = try func.resolveInst(ty_op.operand); |
| ... | ... | @@ -4277,18 +4320,18 @@ fn airUnwrapErrUnionError(func: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) |
| 4277 | 4320 | break :result WValue{ .imm32 = 0 }; |
| 4278 | 4321 | } |
| 4279 | 4322 | |
| 4280 | if (op_is_ptr or !payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4323 | if (op_is_ptr or !payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4281 | 4324 | break :result func.reuseOperand(ty_op.operand, operand); |
| 4282 | 4325 | } |
| 4283 | 4326 | |
| 4284 | const error_val = try func.load(operand, Type.anyerror, @as(u32, @intCast(errUnionErrorOffset(payload_ty, mod)))); | |
| 4327 | const error_val = try func.load(operand, Type.anyerror, @intCast(errUnionErrorOffset(payload_ty, pt))); | |
| 4285 | 4328 | break :result try error_val.toLocal(func, Type.anyerror); |
| 4286 | 4329 | }; |
| 4287 | 4330 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4288 | 4331 | } |
| 4289 | 4332 | |
| 4290 | 4333 | fn airWrapErrUnionPayload(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4291 | const mod = func.bin_file.base.comp.module.?; | |
| 4334 | const pt = func.pt; | |
| 4292 | 4335 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4293 | 4336 | |
| 4294 | 4337 | const operand = try func.resolveInst(ty_op.operand); |
| ... | ... | @@ -4296,18 +4339,18 @@ fn airWrapErrUnionPayload(func: *CodeGen, inst: Air.Inst.Index) InnerError!void |
| 4296 | 4339 | |
| 4297 | 4340 | const pl_ty = func.typeOf(ty_op.operand); |
| 4298 | 4341 | const result = result: { |
| 4299 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4342 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4300 | 4343 | break :result func.reuseOperand(ty_op.operand, operand); |
| 4301 | 4344 | } |
| 4302 | 4345 | |
| 4303 | 4346 | const err_union = try func.allocStack(err_ty); |
| 4304 | const payload_ptr = try func.buildPointerOffset(err_union, @as(u32, @intCast(errUnionPayloadOffset(pl_ty, mod))), .new); | |
| 4347 | const payload_ptr = try func.buildPointerOffset(err_union, @as(u32, @intCast(errUnionPayloadOffset(pl_ty, pt))), .new); | |
| 4305 | 4348 | try func.store(payload_ptr, operand, pl_ty, 0); |
| 4306 | 4349 | |
| 4307 | 4350 | // ensure we also write '0' to the error part, so any present stack value gets overwritten by it. |
| 4308 | 4351 | try func.emitWValue(err_union); |
| 4309 | 4352 | try func.addImm32(0); |
| 4310 | const err_val_offset = @as(u32, @intCast(errUnionErrorOffset(pl_ty, mod))); | |
| 4353 | const err_val_offset: u32 = @intCast(errUnionErrorOffset(pl_ty, pt)); | |
| 4311 | 4354 | try func.addMemArg(.i32_store16, .{ |
| 4312 | 4355 | .offset = err_union.offset() + err_val_offset, |
| 4313 | 4356 | .alignment = 2, |
| ... | ... | @@ -4318,7 +4361,8 @@ fn airWrapErrUnionPayload(func: *CodeGen, inst: Air.Inst.Index) InnerError!void |
| 4318 | 4361 | } |
| 4319 | 4362 | |
| 4320 | 4363 | fn airWrapErrUnionErr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4321 | const mod = func.bin_file.base.comp.module.?; | |
| 4364 | const pt = func.pt; | |
| 4365 | const mod = pt.zcu; | |
| 4322 | 4366 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4323 | 4367 | |
| 4324 | 4368 | const operand = try func.resolveInst(ty_op.operand); |
| ... | ... | @@ -4326,17 +4370,17 @@ fn airWrapErrUnionErr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4326 | 4370 | const pl_ty = err_ty.errorUnionPayload(mod); |
| 4327 | 4371 | |
| 4328 | 4372 | const result = result: { |
| 4329 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4373 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4330 | 4374 | break :result func.reuseOperand(ty_op.operand, operand); |
| 4331 | 4375 | } |
| 4332 | 4376 | |
| 4333 | 4377 | const err_union = try func.allocStack(err_ty); |
| 4334 | 4378 | // store error value |
| 4335 | try func.store(err_union, operand, Type.anyerror, @as(u32, @intCast(errUnionErrorOffset(pl_ty, mod)))); | |
| 4379 | try func.store(err_union, operand, Type.anyerror, @intCast(errUnionErrorOffset(pl_ty, pt))); | |
| 4336 | 4380 | |
| 4337 | 4381 | // write 'undefined' to the payload |
| 4338 | const payload_ptr = try func.buildPointerOffset(err_union, @as(u32, @intCast(errUnionPayloadOffset(pl_ty, mod))), .new); | |
| 4339 | const len = @as(u32, @intCast(err_ty.errorUnionPayload(mod).abiSize(mod))); | |
| 4382 | const payload_ptr = try func.buildPointerOffset(err_union, @as(u32, @intCast(errUnionPayloadOffset(pl_ty, pt))), .new); | |
| 4383 | const len = @as(u32, @intCast(err_ty.errorUnionPayload(mod).abiSize(pt))); | |
| 4340 | 4384 | try func.memset(Type.u8, payload_ptr, .{ .imm32 = len }, .{ .imm32 = 0xaa }); |
| 4341 | 4385 | |
| 4342 | 4386 | break :result err_union; |
| ... | ... | @@ -4350,16 +4394,17 @@ fn airIntcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4350 | 4394 | const ty = ty_op.ty.toType(); |
| 4351 | 4395 | const operand = try func.resolveInst(ty_op.operand); |
| 4352 | 4396 | const operand_ty = func.typeOf(ty_op.operand); |
| 4353 | const mod = func.bin_file.base.comp.module.?; | |
| 4397 | const pt = func.pt; | |
| 4398 | const mod = pt.zcu; | |
| 4354 | 4399 | if (ty.zigTypeTag(mod) == .Vector or operand_ty.zigTypeTag(mod) == .Vector) { |
| 4355 | 4400 | return func.fail("todo Wasm intcast for vectors", .{}); |
| 4356 | 4401 | } |
| 4357 | if (ty.abiSize(mod) > 16 or operand_ty.abiSize(mod) > 16) { | |
| 4402 | if (ty.abiSize(pt) > 16 or operand_ty.abiSize(pt) > 16) { | |
| 4358 | 4403 | return func.fail("todo Wasm intcast for bitsize > 128", .{}); |
| 4359 | 4404 | } |
| 4360 | 4405 | |
| 4361 | const op_bits = toWasmBits(@as(u16, @intCast(operand_ty.bitSize(mod)))).?; | |
| 4362 | const wanted_bits = toWasmBits(@as(u16, @intCast(ty.bitSize(mod)))).?; | |
| 4406 | const op_bits = toWasmBits(@intCast(operand_ty.bitSize(pt))).?; | |
| 4407 | const wanted_bits = toWasmBits(@intCast(ty.bitSize(pt))).?; | |
| 4363 | 4408 | const result = if (op_bits == wanted_bits) |
| 4364 | 4409 | func.reuseOperand(ty_op.operand, operand) |
| 4365 | 4410 | else |
| ... | ... | @@ -4373,9 +4418,10 @@ fn airIntcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4373 | 4418 | /// Asserts type's bitsize <= 128 |
| 4374 | 4419 | /// NOTE: May leave the result on the top of the stack. |
| 4375 | 4420 | fn intcast(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerError!WValue { |
| 4376 | const mod = func.bin_file.base.comp.module.?; | |
| 4377 | const given_bitsize = @as(u16, @intCast(given.bitSize(mod))); | |
| 4378 | const wanted_bitsize = @as(u16, @intCast(wanted.bitSize(mod))); | |
| 4421 | const pt = func.pt; | |
| 4422 | const mod = pt.zcu; | |
| 4423 | const given_bitsize = @as(u16, @intCast(given.bitSize(pt))); | |
| 4424 | const wanted_bitsize = @as(u16, @intCast(wanted.bitSize(pt))); | |
| 4379 | 4425 | assert(given_bitsize <= 128); |
| 4380 | 4426 | assert(wanted_bitsize <= 128); |
| 4381 | 4427 | |
| ... | ... | @@ -4422,7 +4468,8 @@ fn intcast(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerErro |
| 4422 | 4468 | } |
| 4423 | 4469 | |
| 4424 | 4470 | fn airIsNull(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: enum { value, ptr }) InnerError!void { |
| 4425 | const mod = func.bin_file.base.comp.module.?; | |
| 4471 | const pt = func.pt; | |
| 4472 | const mod = pt.zcu; | |
| 4426 | 4473 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4427 | 4474 | const operand = try func.resolveInst(un_op); |
| 4428 | 4475 | |
| ... | ... | @@ -4436,15 +4483,16 @@ fn airIsNull(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: |
| 4436 | 4483 | /// For a given type and operand, checks if it's considered `null`. |
| 4437 | 4484 | /// NOTE: Leaves the result on the stack |
| 4438 | 4485 | fn isNull(func: *CodeGen, operand: WValue, optional_ty: Type, opcode: wasm.Opcode) InnerError!WValue { |
| 4439 | const mod = func.bin_file.base.comp.module.?; | |
| 4486 | const pt = func.pt; | |
| 4487 | const mod = pt.zcu; | |
| 4440 | 4488 | try func.emitWValue(operand); |
| 4441 | 4489 | const payload_ty = optional_ty.optionalChild(mod); |
| 4442 | 4490 | if (!optional_ty.optionalReprIsPayload(mod)) { |
| 4443 | 4491 | // When payload is zero-bits, we can treat operand as a value, rather than |
| 4444 | 4492 | // a pointer to the stack value |
| 4445 | if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4446 | const offset = std.math.cast(u32, payload_ty.abiSize(mod)) orelse { | |
| 4447 | return func.fail("Optional type {} too big to fit into stack frame", .{optional_ty.fmt(mod)}); | |
| 4493 | if (payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4494 | const offset = std.math.cast(u32, payload_ty.abiSize(pt)) orelse { | |
| 4495 | return func.fail("Optional type {} too big to fit into stack frame", .{optional_ty.fmt(pt)}); | |
| 4448 | 4496 | }; |
| 4449 | 4497 | try func.addMemArg(.i32_load8_u, .{ .offset = operand.offset() + offset, .alignment = 1 }); |
| 4450 | 4498 | } |
| ... | ... | @@ -4464,11 +4512,12 @@ fn isNull(func: *CodeGen, operand: WValue, optional_ty: Type, opcode: wasm.Opcod |
| 4464 | 4512 | } |
| 4465 | 4513 | |
| 4466 | 4514 | fn airOptionalPayload(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4467 | const mod = func.bin_file.base.comp.module.?; | |
| 4515 | const pt = func.pt; | |
| 4516 | const mod = pt.zcu; | |
| 4468 | 4517 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4469 | 4518 | const opt_ty = func.typeOf(ty_op.operand); |
| 4470 | 4519 | const payload_ty = func.typeOfIndex(inst); |
| 4471 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4520 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4472 | 4521 | return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 4473 | 4522 | } |
| 4474 | 4523 | |
| ... | ... | @@ -4476,7 +4525,7 @@ fn airOptionalPayload(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4476 | 4525 | const operand = try func.resolveInst(ty_op.operand); |
| 4477 | 4526 | if (opt_ty.optionalReprIsPayload(mod)) break :result func.reuseOperand(ty_op.operand, operand); |
| 4478 | 4527 | |
| 4479 | if (isByRef(payload_ty, mod)) { | |
| 4528 | if (isByRef(payload_ty, pt)) { | |
| 4480 | 4529 | break :result try func.buildPointerOffset(operand, 0, .new); |
| 4481 | 4530 | } |
| 4482 | 4531 | |
| ... | ... | @@ -4487,14 +4536,15 @@ fn airOptionalPayload(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4487 | 4536 | } |
| 4488 | 4537 | |
| 4489 | 4538 | fn airOptionalPayloadPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4490 | const mod = func.bin_file.base.comp.module.?; | |
| 4539 | const pt = func.pt; | |
| 4540 | const mod = pt.zcu; | |
| 4491 | 4541 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4492 | 4542 | const operand = try func.resolveInst(ty_op.operand); |
| 4493 | 4543 | const opt_ty = func.typeOf(ty_op.operand).childType(mod); |
| 4494 | 4544 | |
| 4495 | 4545 | const result = result: { |
| 4496 | 4546 | const payload_ty = opt_ty.optionalChild(mod); |
| 4497 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod) or opt_ty.optionalReprIsPayload(mod)) { | |
| 4547 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt) or opt_ty.optionalReprIsPayload(mod)) { | |
| 4498 | 4548 | break :result func.reuseOperand(ty_op.operand, operand); |
| 4499 | 4549 | } |
| 4500 | 4550 | |
| ... | ... | @@ -4504,12 +4554,13 @@ fn airOptionalPayloadPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4504 | 4554 | } |
| 4505 | 4555 | |
| 4506 | 4556 | fn airOptionalPayloadPtrSet(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4507 | const mod = func.bin_file.base.comp.module.?; | |
| 4557 | const pt = func.pt; | |
| 4558 | const mod = pt.zcu; | |
| 4508 | 4559 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4509 | 4560 | const operand = try func.resolveInst(ty_op.operand); |
| 4510 | 4561 | const opt_ty = func.typeOf(ty_op.operand).childType(mod); |
| 4511 | 4562 | const payload_ty = opt_ty.optionalChild(mod); |
| 4512 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4563 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4513 | 4564 | return func.fail("TODO: Implement OptionalPayloadPtrSet for optional with zero-sized type {}", .{payload_ty.fmtDebug()}); |
| 4514 | 4565 | } |
| 4515 | 4566 | |
| ... | ... | @@ -4517,8 +4568,8 @@ fn airOptionalPayloadPtrSet(func: *CodeGen, inst: Air.Inst.Index) InnerError!voi |
| 4517 | 4568 | return func.finishAir(inst, operand, &.{ty_op.operand}); |
| 4518 | 4569 | } |
| 4519 | 4570 | |
| 4520 | const offset = std.math.cast(u32, payload_ty.abiSize(mod)) orelse { | |
| 4521 | return func.fail("Optional type {} too big to fit into stack frame", .{opt_ty.fmt(mod)}); | |
| 4571 | const offset = std.math.cast(u32, payload_ty.abiSize(pt)) orelse { | |
| 4572 | return func.fail("Optional type {} too big to fit into stack frame", .{opt_ty.fmt(pt)}); | |
| 4522 | 4573 | }; |
| 4523 | 4574 | |
| 4524 | 4575 | try func.emitWValue(operand); |
| ... | ... | @@ -4532,10 +4583,11 @@ fn airOptionalPayloadPtrSet(func: *CodeGen, inst: Air.Inst.Index) InnerError!voi |
| 4532 | 4583 | fn airWrapOptional(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4533 | 4584 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4534 | 4585 | const payload_ty = func.typeOf(ty_op.operand); |
| 4535 | const mod = func.bin_file.base.comp.module.?; | |
| 4586 | const pt = func.pt; | |
| 4587 | const mod = pt.zcu; | |
| 4536 | 4588 | |
| 4537 | 4589 | const result = result: { |
| 4538 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4590 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4539 | 4591 | const non_null_bit = try func.allocStack(Type.u1); |
| 4540 | 4592 | try func.emitWValue(non_null_bit); |
| 4541 | 4593 | try func.addImm32(1); |
| ... | ... | @@ -4548,8 +4600,8 @@ fn airWrapOptional(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4548 | 4600 | if (op_ty.optionalReprIsPayload(mod)) { |
| 4549 | 4601 | break :result func.reuseOperand(ty_op.operand, operand); |
| 4550 | 4602 | } |
| 4551 | const offset = std.math.cast(u32, payload_ty.abiSize(mod)) orelse { | |
| 4552 | return func.fail("Optional type {} too big to fit into stack frame", .{op_ty.fmt(mod)}); | |
| 4603 | const offset = std.math.cast(u32, payload_ty.abiSize(pt)) orelse { | |
| 4604 | return func.fail("Optional type {} too big to fit into stack frame", .{op_ty.fmt(pt)}); | |
| 4553 | 4605 | }; |
| 4554 | 4606 | |
| 4555 | 4607 | // Create optional type, set the non-null bit, and store the operand inside the optional type |
| ... | ... | @@ -4589,14 +4641,15 @@ fn airSliceLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4589 | 4641 | } |
| 4590 | 4642 | |
| 4591 | 4643 | fn airSliceElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4592 | const mod = func.bin_file.base.comp.module.?; | |
| 4644 | const pt = func.pt; | |
| 4645 | const mod = pt.zcu; | |
| 4593 | 4646 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4594 | 4647 | |
| 4595 | 4648 | const slice_ty = func.typeOf(bin_op.lhs); |
| 4596 | 4649 | const slice = try func.resolveInst(bin_op.lhs); |
| 4597 | 4650 | const index = try func.resolveInst(bin_op.rhs); |
| 4598 | 4651 | const elem_ty = slice_ty.childType(mod); |
| 4599 | const elem_size = elem_ty.abiSize(mod); | |
| 4652 | const elem_size = elem_ty.abiSize(pt); | |
| 4600 | 4653 | |
| 4601 | 4654 | // load pointer onto stack |
| 4602 | 4655 | _ = try func.load(slice, Type.usize, 0); |
| ... | ... | @@ -4610,7 +4663,7 @@ fn airSliceElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4610 | 4663 | const result_ptr = try func.allocLocal(Type.usize); |
| 4611 | 4664 | try func.addLabel(.local_set, result_ptr.local.value); |
| 4612 | 4665 | |
| 4613 | const result = if (!isByRef(elem_ty, mod)) result: { | |
| 4666 | const result = if (!isByRef(elem_ty, pt)) result: { | |
| 4614 | 4667 | const elem_val = try func.load(result_ptr, elem_ty, 0); |
| 4615 | 4668 | break :result try elem_val.toLocal(func, elem_ty); |
| 4616 | 4669 | } else result_ptr; |
| ... | ... | @@ -4619,12 +4672,13 @@ fn airSliceElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4619 | 4672 | } |
| 4620 | 4673 | |
| 4621 | 4674 | fn airSliceElemPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4622 | const mod = func.bin_file.base.comp.module.?; | |
| 4675 | const pt = func.pt; | |
| 4676 | const mod = pt.zcu; | |
| 4623 | 4677 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4624 | 4678 | const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4625 | 4679 | |
| 4626 | 4680 | const elem_ty = ty_pl.ty.toType().childType(mod); |
| 4627 | const elem_size = elem_ty.abiSize(mod); | |
| 4681 | const elem_size = elem_ty.abiSize(pt); | |
| 4628 | 4682 | |
| 4629 | 4683 | const slice = try func.resolveInst(bin_op.lhs); |
| 4630 | 4684 | const index = try func.resolveInst(bin_op.rhs); |
| ... | ... | @@ -4672,14 +4726,14 @@ fn airTrunc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4672 | 4726 | /// Truncates a given operand to a given type, discarding any overflown bits. |
| 4673 | 4727 | /// NOTE: Resulting value is left on the stack. |
| 4674 | 4728 | fn trunc(func: *CodeGen, operand: WValue, wanted_ty: Type, given_ty: Type) InnerError!WValue { |
| 4675 | const mod = func.bin_file.base.comp.module.?; | |
| 4676 | const given_bits = @as(u16, @intCast(given_ty.bitSize(mod))); | |
| 4729 | const pt = func.pt; | |
| 4730 | const given_bits = @as(u16, @intCast(given_ty.bitSize(pt))); | |
| 4677 | 4731 | if (toWasmBits(given_bits) == null) { |
| 4678 | 4732 | return func.fail("TODO: Implement wasm integer truncation for integer bitsize: {d}", .{given_bits}); |
| 4679 | 4733 | } |
| 4680 | 4734 | |
| 4681 | 4735 | var result = try func.intcast(operand, given_ty, wanted_ty); |
| 4682 | const wanted_bits = @as(u16, @intCast(wanted_ty.bitSize(mod))); | |
| 4736 | const wanted_bits = @as(u16, @intCast(wanted_ty.bitSize(pt))); | |
| 4683 | 4737 | const wasm_bits = toWasmBits(wanted_bits).?; |
| 4684 | 4738 | if (wasm_bits != wanted_bits) { |
| 4685 | 4739 | result = try func.wrapOperand(result, wanted_ty); |
| ... | ... | @@ -4696,7 +4750,8 @@ fn airIntFromBool(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4696 | 4750 | } |
| 4697 | 4751 | |
| 4698 | 4752 | fn airArrayToSlice(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4699 | const mod = func.bin_file.base.comp.module.?; | |
| 4753 | const pt = func.pt; | |
| 4754 | const mod = pt.zcu; | |
| 4700 | 4755 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4701 | 4756 | |
| 4702 | 4757 | const operand = try func.resolveInst(ty_op.operand); |
| ... | ... | @@ -4707,7 +4762,7 @@ fn airArrayToSlice(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4707 | 4762 | const slice_local = try func.allocStack(slice_ty); |
| 4708 | 4763 | |
| 4709 | 4764 | // store the array ptr in the slice |
| 4710 | if (array_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4765 | if (array_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4711 | 4766 | try func.store(slice_local, operand, Type.usize, 0); |
| 4712 | 4767 | } |
| 4713 | 4768 | |
| ... | ... | @@ -4719,7 +4774,8 @@ fn airArrayToSlice(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4719 | 4774 | } |
| 4720 | 4775 | |
| 4721 | 4776 | fn airIntFromPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4722 | const mod = func.bin_file.base.comp.module.?; | |
| 4777 | const pt = func.pt; | |
| 4778 | const mod = pt.zcu; | |
| 4723 | 4779 | const un_op = func.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 4724 | 4780 | const operand = try func.resolveInst(un_op); |
| 4725 | 4781 | const ptr_ty = func.typeOf(un_op); |
| ... | ... | @@ -4734,14 +4790,15 @@ fn airIntFromPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4734 | 4790 | } |
| 4735 | 4791 | |
| 4736 | 4792 | fn airPtrElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4737 | const mod = func.bin_file.base.comp.module.?; | |
| 4793 | const pt = func.pt; | |
| 4794 | const mod = pt.zcu; | |
| 4738 | 4795 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4739 | 4796 | |
| 4740 | 4797 | const ptr_ty = func.typeOf(bin_op.lhs); |
| 4741 | 4798 | const ptr = try func.resolveInst(bin_op.lhs); |
| 4742 | 4799 | const index = try func.resolveInst(bin_op.rhs); |
| 4743 | 4800 | const elem_ty = ptr_ty.childType(mod); |
| 4744 | const elem_size = elem_ty.abiSize(mod); | |
| 4801 | const elem_size = elem_ty.abiSize(pt); | |
| 4745 | 4802 | |
| 4746 | 4803 | // load pointer onto the stack |
| 4747 | 4804 | if (ptr_ty.isSlice(mod)) { |
| ... | ... | @@ -4759,7 +4816,7 @@ fn airPtrElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4759 | 4816 | const elem_result = val: { |
| 4760 | 4817 | var result = try func.allocLocal(Type.usize); |
| 4761 | 4818 | try func.addLabel(.local_set, result.local.value); |
| 4762 | if (isByRef(elem_ty, mod)) { | |
| 4819 | if (isByRef(elem_ty, pt)) { | |
| 4763 | 4820 | break :val result; |
| 4764 | 4821 | } |
| 4765 | 4822 | defer result.free(func); // only free if it's not returned like above |
| ... | ... | @@ -4771,13 +4828,14 @@ fn airPtrElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4771 | 4828 | } |
| 4772 | 4829 | |
| 4773 | 4830 | fn airPtrElemPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4774 | const mod = func.bin_file.base.comp.module.?; | |
| 4831 | const pt = func.pt; | |
| 4832 | const mod = pt.zcu; | |
| 4775 | 4833 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4776 | 4834 | const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4777 | 4835 | |
| 4778 | 4836 | const ptr_ty = func.typeOf(bin_op.lhs); |
| 4779 | 4837 | const elem_ty = ty_pl.ty.toType().childType(mod); |
| 4780 | const elem_size = elem_ty.abiSize(mod); | |
| 4838 | const elem_size = elem_ty.abiSize(pt); | |
| 4781 | 4839 | |
| 4782 | 4840 | const ptr = try func.resolveInst(bin_op.lhs); |
| 4783 | 4841 | const index = try func.resolveInst(bin_op.rhs); |
| ... | ... | @@ -4801,7 +4859,8 @@ fn airPtrElemPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4801 | 4859 | } |
| 4802 | 4860 | |
| 4803 | 4861 | fn airPtrBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 4804 | const mod = func.bin_file.base.comp.module.?; | |
| 4862 | const pt = func.pt; | |
| 4863 | const mod = pt.zcu; | |
| 4805 | 4864 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4806 | 4865 | const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4807 | 4866 | |
| ... | ... | @@ -4813,13 +4872,13 @@ fn airPtrBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 4813 | 4872 | else => ptr_ty.childType(mod), |
| 4814 | 4873 | }; |
| 4815 | 4874 | |
| 4816 | const valtype = typeToValtype(Type.usize, mod); | |
| 4875 | const valtype = typeToValtype(Type.usize, pt); | |
| 4817 | 4876 | const mul_opcode = buildOpcode(.{ .valtype1 = valtype, .op = .mul }); |
| 4818 | 4877 | const bin_opcode = buildOpcode(.{ .valtype1 = valtype, .op = op }); |
| 4819 | 4878 | |
| 4820 | 4879 | try func.lowerToStack(ptr); |
| 4821 | 4880 | try func.emitWValue(offset); |
| 4822 | try func.addImm32(@intCast(pointee_ty.abiSize(mod))); | |
| 4881 | try func.addImm32(@intCast(pointee_ty.abiSize(pt))); | |
| 4823 | 4882 | try func.addTag(Mir.Inst.Tag.fromOpcode(mul_opcode)); |
| 4824 | 4883 | try func.addTag(Mir.Inst.Tag.fromOpcode(bin_opcode)); |
| 4825 | 4884 | |
| ... | ... | @@ -4829,7 +4888,8 @@ fn airPtrBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 4829 | 4888 | } |
| 4830 | 4889 | |
| 4831 | 4890 | fn airMemset(func: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void { |
| 4832 | const mod = func.bin_file.base.comp.module.?; | |
| 4891 | const pt = func.pt; | |
| 4892 | const mod = pt.zcu; | |
| 4833 | 4893 | if (safety) { |
| 4834 | 4894 | // TODO if the value is undef, write 0xaa bytes to dest |
| 4835 | 4895 | } else { |
| ... | ... | @@ -4862,8 +4922,8 @@ fn airMemset(func: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void |
| 4862 | 4922 | /// this to wasm's memset instruction. When the feature is not present, |
| 4863 | 4923 | /// we implement it manually. |
| 4864 | 4924 | fn memset(func: *CodeGen, elem_ty: Type, ptr: WValue, len: WValue, value: WValue) InnerError!void { |
| 4865 | const mod = func.bin_file.base.comp.module.?; | |
| 4866 | const abi_size = @as(u32, @intCast(elem_ty.abiSize(mod))); | |
| 4925 | const pt = func.pt; | |
| 4926 | const abi_size = @as(u32, @intCast(elem_ty.abiSize(pt))); | |
| 4867 | 4927 | |
| 4868 | 4928 | // When bulk_memory is enabled, we lower it to wasm's memset instruction. |
| 4869 | 4929 | // If not, we lower it ourselves. |
| ... | ... | @@ -4951,16 +5011,17 @@ fn memset(func: *CodeGen, elem_ty: Type, ptr: WValue, len: WValue, value: WValue |
| 4951 | 5011 | } |
| 4952 | 5012 | |
| 4953 | 5013 | fn airArrayElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4954 | const mod = func.bin_file.base.comp.module.?; | |
| 5014 | const pt = func.pt; | |
| 5015 | const mod = pt.zcu; | |
| 4955 | 5016 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4956 | 5017 | |
| 4957 | 5018 | const array_ty = func.typeOf(bin_op.lhs); |
| 4958 | 5019 | const array = try func.resolveInst(bin_op.lhs); |
| 4959 | 5020 | const index = try func.resolveInst(bin_op.rhs); |
| 4960 | 5021 | const elem_ty = array_ty.childType(mod); |
| 4961 | const elem_size = elem_ty.abiSize(mod); | |
| 5022 | const elem_size = elem_ty.abiSize(pt); | |
| 4962 | 5023 | |
| 4963 | if (isByRef(array_ty, mod)) { | |
| 5024 | if (isByRef(array_ty, pt)) { | |
| 4964 | 5025 | try func.lowerToStack(array); |
| 4965 | 5026 | try func.emitWValue(index); |
| 4966 | 5027 | try func.addImm32(@intCast(elem_size)); |
| ... | ... | @@ -4971,7 +5032,7 @@ fn airArrayElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4971 | 5032 | |
| 4972 | 5033 | switch (index) { |
| 4973 | 5034 | inline .imm32, .imm64 => |lane| { |
| 4974 | const opcode: wasm.SimdOpcode = switch (elem_ty.bitSize(mod)) { | |
| 5035 | const opcode: wasm.SimdOpcode = switch (elem_ty.bitSize(pt)) { | |
| 4975 | 5036 | 8 => if (elem_ty.isSignedInt(mod)) .i8x16_extract_lane_s else .i8x16_extract_lane_u, |
| 4976 | 5037 | 16 => if (elem_ty.isSignedInt(mod)) .i16x8_extract_lane_s else .i16x8_extract_lane_u, |
| 4977 | 5038 | 32 => if (elem_ty.isInt(mod)) .i32x4_extract_lane else .f32x4_extract_lane, |
| ... | ... | @@ -5007,7 +5068,7 @@ fn airArrayElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5007 | 5068 | var result = try func.allocLocal(Type.usize); |
| 5008 | 5069 | try func.addLabel(.local_set, result.local.value); |
| 5009 | 5070 | |
| 5010 | if (isByRef(elem_ty, mod)) { | |
| 5071 | if (isByRef(elem_ty, pt)) { | |
| 5011 | 5072 | break :val result; |
| 5012 | 5073 | } |
| 5013 | 5074 | defer result.free(func); // only free if no longer needed and not returned like above |
| ... | ... | @@ -5020,7 +5081,8 @@ fn airArrayElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5020 | 5081 | } |
| 5021 | 5082 | |
| 5022 | 5083 | fn airIntFromFloat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5023 | const mod = func.bin_file.base.comp.module.?; | |
| 5084 | const pt = func.pt; | |
| 5085 | const mod = pt.zcu; | |
| 5024 | 5086 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5025 | 5087 | |
| 5026 | 5088 | const operand = try func.resolveInst(ty_op.operand); |
| ... | ... | @@ -5054,8 +5116,8 @@ fn airIntFromFloat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5054 | 5116 | try func.emitWValue(operand); |
| 5055 | 5117 | const op = buildOpcode(.{ |
| 5056 | 5118 | .op = .trunc, |
| 5057 | .valtype1 = typeToValtype(dest_ty, mod), | |
| 5058 | .valtype2 = typeToValtype(op_ty, mod), | |
| 5119 | .valtype1 = typeToValtype(dest_ty, pt), | |
| 5120 | .valtype2 = typeToValtype(op_ty, pt), | |
| 5059 | 5121 | .signedness = dest_info.signedness, |
| 5060 | 5122 | }); |
| 5061 | 5123 | try func.addTag(Mir.Inst.Tag.fromOpcode(op)); |
| ... | ... | @@ -5065,7 +5127,8 @@ fn airIntFromFloat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5065 | 5127 | } |
| 5066 | 5128 | |
| 5067 | 5129 | fn airFloatFromInt(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5068 | const mod = func.bin_file.base.comp.module.?; | |
| 5130 | const pt = func.pt; | |
| 5131 | const mod = pt.zcu; | |
| 5069 | 5132 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5070 | 5133 | |
| 5071 | 5134 | const operand = try func.resolveInst(ty_op.operand); |
| ... | ... | @@ -5099,8 +5162,8 @@ fn airFloatFromInt(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5099 | 5162 | try func.emitWValue(operand); |
| 5100 | 5163 | const op = buildOpcode(.{ |
| 5101 | 5164 | .op = .convert, |
| 5102 | .valtype1 = typeToValtype(dest_ty, mod), | |
| 5103 | .valtype2 = typeToValtype(op_ty, mod), | |
| 5165 | .valtype1 = typeToValtype(dest_ty, pt), | |
| 5166 | .valtype2 = typeToValtype(op_ty, pt), | |
| 5104 | 5167 | .signedness = op_info.signedness, |
| 5105 | 5168 | }); |
| 5106 | 5169 | try func.addTag(Mir.Inst.Tag.fromOpcode(op)); |
| ... | ... | @@ -5111,19 +5174,20 @@ fn airFloatFromInt(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5111 | 5174 | } |
| 5112 | 5175 | |
| 5113 | 5176 | fn airSplat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5114 | const mod = func.bin_file.base.comp.module.?; | |
| 5177 | const pt = func.pt; | |
| 5178 | const mod = pt.zcu; | |
| 5115 | 5179 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5116 | 5180 | const operand = try func.resolveInst(ty_op.operand); |
| 5117 | 5181 | const ty = func.typeOfIndex(inst); |
| 5118 | 5182 | const elem_ty = ty.childType(mod); |
| 5119 | 5183 | |
| 5120 | if (determineSimdStoreStrategy(ty, mod) == .direct) blk: { | |
| 5184 | if (determineSimdStoreStrategy(ty, pt) == .direct) blk: { | |
| 5121 | 5185 | switch (operand) { |
| 5122 | 5186 | // when the operand lives in the linear memory section, we can directly |
| 5123 | 5187 | // load and splat the value at once. Meaning we do not first have to load |
| 5124 | 5188 | // the scalar value onto the stack. |
| 5125 | 5189 | .stack_offset, .memory, .memory_offset => { |
| 5126 | const opcode = switch (elem_ty.bitSize(mod)) { | |
| 5190 | const opcode = switch (elem_ty.bitSize(pt)) { | |
| 5127 | 5191 | 8 => std.wasm.simdOpcode(.v128_load8_splat), |
| 5128 | 5192 | 16 => std.wasm.simdOpcode(.v128_load16_splat), |
| 5129 | 5193 | 32 => std.wasm.simdOpcode(.v128_load32_splat), |
| ... | ... | @@ -5138,14 +5202,14 @@ fn airSplat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5138 | 5202 | try func.mir_extra.appendSlice(func.gpa, &[_]u32{ |
| 5139 | 5203 | opcode, |
| 5140 | 5204 | operand.offset(), |
| 5141 | @intCast(elem_ty.abiAlignment(mod).toByteUnits().?), | |
| 5205 | @intCast(elem_ty.abiAlignment(pt).toByteUnits().?), | |
| 5142 | 5206 | }); |
| 5143 | 5207 | try func.addInst(.{ .tag = .simd_prefix, .data = .{ .payload = extra_index } }); |
| 5144 | 5208 | try func.addLabel(.local_set, result.local.value); |
| 5145 | 5209 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 5146 | 5210 | }, |
| 5147 | 5211 | .local => { |
| 5148 | const opcode = switch (elem_ty.bitSize(mod)) { | |
| 5212 | const opcode = switch (elem_ty.bitSize(pt)) { | |
| 5149 | 5213 | 8 => std.wasm.simdOpcode(.i8x16_splat), |
| 5150 | 5214 | 16 => std.wasm.simdOpcode(.i16x8_splat), |
| 5151 | 5215 | 32 => if (elem_ty.isInt(mod)) std.wasm.simdOpcode(.i32x4_splat) else std.wasm.simdOpcode(.f32x4_splat), |
| ... | ... | @@ -5163,14 +5227,14 @@ fn airSplat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5163 | 5227 | else => unreachable, |
| 5164 | 5228 | } |
| 5165 | 5229 | } |
| 5166 | const elem_size = elem_ty.bitSize(mod); | |
| 5230 | const elem_size = elem_ty.bitSize(pt); | |
| 5167 | 5231 | const vector_len = @as(usize, @intCast(ty.vectorLen(mod))); |
| 5168 | 5232 | if ((!std.math.isPowerOfTwo(elem_size) or elem_size % 8 != 0) and vector_len > 1) { |
| 5169 | 5233 | return func.fail("TODO: WebAssembly `@splat` for arbitrary element bitsize {d}", .{elem_size}); |
| 5170 | 5234 | } |
| 5171 | 5235 | |
| 5172 | 5236 | const result = try func.allocStack(ty); |
| 5173 | const elem_byte_size = @as(u32, @intCast(elem_ty.abiSize(mod))); | |
| 5237 | const elem_byte_size = @as(u32, @intCast(elem_ty.abiSize(pt))); | |
| 5174 | 5238 | var index: usize = 0; |
| 5175 | 5239 | var offset: u32 = 0; |
| 5176 | 5240 | while (index < vector_len) : (index += 1) { |
| ... | ... | @@ -5190,7 +5254,8 @@ fn airSelect(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5190 | 5254 | } |
| 5191 | 5255 | |
| 5192 | 5256 | fn airShuffle(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5193 | const mod = func.bin_file.base.comp.module.?; | |
| 5257 | const pt = func.pt; | |
| 5258 | const mod = pt.zcu; | |
| 5194 | 5259 | const inst_ty = func.typeOfIndex(inst); |
| 5195 | 5260 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5196 | 5261 | const extra = func.air.extraData(Air.Shuffle, ty_pl.payload).data; |
| ... | ... | @@ -5201,14 +5266,14 @@ fn airShuffle(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5201 | 5266 | const mask_len = extra.mask_len; |
| 5202 | 5267 | |
| 5203 | 5268 | const child_ty = inst_ty.childType(mod); |
| 5204 | const elem_size = child_ty.abiSize(mod); | |
| 5269 | const elem_size = child_ty.abiSize(pt); | |
| 5205 | 5270 | |
| 5206 | 5271 | // TODO: One of them could be by ref; handle in loop |
| 5207 | if (isByRef(func.typeOf(extra.a), mod) or isByRef(inst_ty, mod)) { | |
| 5272 | if (isByRef(func.typeOf(extra.a), pt) or isByRef(inst_ty, pt)) { | |
| 5208 | 5273 | const result = try func.allocStack(inst_ty); |
| 5209 | 5274 | |
| 5210 | 5275 | for (0..mask_len) |index| { |
| 5211 | const value = (try mask.elemValue(mod, index)).toSignedInt(mod); | |
| 5276 | const value = (try mask.elemValue(pt, index)).toSignedInt(pt); | |
| 5212 | 5277 | |
| 5213 | 5278 | try func.emitWValue(result); |
| 5214 | 5279 | |
| ... | ... | @@ -5228,7 +5293,7 @@ fn airShuffle(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5228 | 5293 | |
| 5229 | 5294 | var lanes = mem.asBytes(operands[1..]); |
| 5230 | 5295 | for (0..@as(usize, @intCast(mask_len))) |index| { |
| 5231 | const mask_elem = (try mask.elemValue(mod, index)).toSignedInt(mod); | |
| 5296 | const mask_elem = (try mask.elemValue(pt, index)).toSignedInt(pt); | |
| 5232 | 5297 | const base_index = if (mask_elem >= 0) |
| 5233 | 5298 | @as(u8, @intCast(@as(i64, @intCast(elem_size)) * mask_elem)) |
| 5234 | 5299 | else |
| ... | ... | @@ -5259,7 +5324,8 @@ fn airReduce(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5259 | 5324 | } |
| 5260 | 5325 | |
| 5261 | 5326 | fn airAggregateInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5262 | const mod = func.bin_file.base.comp.module.?; | |
| 5327 | const pt = func.pt; | |
| 5328 | const mod = pt.zcu; | |
| 5263 | 5329 | const ip = &mod.intern_pool; |
| 5264 | 5330 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5265 | 5331 | const result_ty = func.typeOfIndex(inst); |
| ... | ... | @@ -5271,7 +5337,7 @@ fn airAggregateInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5271 | 5337 | .Array => { |
| 5272 | 5338 | const result = try func.allocStack(result_ty); |
| 5273 | 5339 | const elem_ty = result_ty.childType(mod); |
| 5274 | const elem_size = @as(u32, @intCast(elem_ty.abiSize(mod))); | |
| 5340 | const elem_size = @as(u32, @intCast(elem_ty.abiSize(pt))); | |
| 5275 | 5341 | const sentinel = if (result_ty.sentinel(mod)) |sent| blk: { |
| 5276 | 5342 | break :blk try func.lowerConstant(sent, elem_ty); |
| 5277 | 5343 | } else null; |
| ... | ... | @@ -5279,7 +5345,7 @@ fn airAggregateInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5279 | 5345 | // When the element type is by reference, we must copy the entire |
| 5280 | 5346 | // value. It is therefore safer to move the offset pointer and store |
| 5281 | 5347 | // each value individually, instead of using store offsets. |
| 5282 | if (isByRef(elem_ty, mod)) { | |
| 5348 | if (isByRef(elem_ty, pt)) { | |
| 5283 | 5349 | // copy stack pointer into a temporary local, which is |
| 5284 | 5350 | // moved for each element to store each value in the right position. |
| 5285 | 5351 | const offset = try func.buildPointerOffset(result, 0, .new); |
| ... | ... | @@ -5309,7 +5375,7 @@ fn airAggregateInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5309 | 5375 | }, |
| 5310 | 5376 | .Struct => switch (result_ty.containerLayout(mod)) { |
| 5311 | 5377 | .@"packed" => { |
| 5312 | if (isByRef(result_ty, mod)) { | |
| 5378 | if (isByRef(result_ty, pt)) { | |
| 5313 | 5379 | return func.fail("TODO: airAggregateInit for packed structs larger than 64 bits", .{}); |
| 5314 | 5380 | } |
| 5315 | 5381 | const packed_struct = mod.typeToPackedStruct(result_ty).?; |
| ... | ... | @@ -5318,7 +5384,7 @@ fn airAggregateInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5318 | 5384 | |
| 5319 | 5385 | // ensure the result is zero'd |
| 5320 | 5386 | const result = try func.allocLocal(backing_type); |
| 5321 | if (backing_type.bitSize(mod) <= 32) | |
| 5387 | if (backing_type.bitSize(pt) <= 32) | |
| 5322 | 5388 | try func.addImm32(0) |
| 5323 | 5389 | else |
| 5324 | 5390 | try func.addImm64(0); |
| ... | ... | @@ -5327,16 +5393,16 @@ fn airAggregateInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5327 | 5393 | var current_bit: u16 = 0; |
| 5328 | 5394 | for (elements, 0..) |elem, elem_index| { |
| 5329 | 5395 | const field_ty = Type.fromInterned(field_types.get(ip)[elem_index]); |
| 5330 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; | |
| 5396 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 5331 | 5397 | |
| 5332 | const shift_val = if (backing_type.bitSize(mod) <= 32) | |
| 5398 | const shift_val = if (backing_type.bitSize(pt) <= 32) | |
| 5333 | 5399 | WValue{ .imm32 = current_bit } |
| 5334 | 5400 | else |
| 5335 | 5401 | WValue{ .imm64 = current_bit }; |
| 5336 | 5402 | |
| 5337 | 5403 | const value = try func.resolveInst(elem); |
| 5338 | const value_bit_size: u16 = @intCast(field_ty.bitSize(mod)); | |
| 5339 | const int_ty = try mod.intType(.unsigned, value_bit_size); | |
| 5404 | const value_bit_size: u16 = @intCast(field_ty.bitSize(pt)); | |
| 5405 | const int_ty = try pt.intType(.unsigned, value_bit_size); | |
| 5340 | 5406 | |
| 5341 | 5407 | // load our current result on stack so we can perform all transformations |
| 5342 | 5408 | // using only stack values. Saving the cost of loads and stores. |
| ... | ... | @@ -5359,10 +5425,10 @@ fn airAggregateInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5359 | 5425 | const offset = try func.buildPointerOffset(result, 0, .new); // pointer to offset |
| 5360 | 5426 | var prev_field_offset: u64 = 0; |
| 5361 | 5427 | for (elements, 0..) |elem, elem_index| { |
| 5362 | if ((try result_ty.structFieldValueComptime(mod, elem_index)) != null) continue; | |
| 5428 | if (try result_ty.structFieldValueComptime(pt, elem_index) != null) continue; | |
| 5363 | 5429 | |
| 5364 | 5430 | const elem_ty = result_ty.structFieldType(elem_index, mod); |
| 5365 | const field_offset = result_ty.structFieldOffset(elem_index, mod); | |
| 5431 | const field_offset = result_ty.structFieldOffset(elem_index, pt); | |
| 5366 | 5432 | _ = try func.buildPointerOffset(offset, @intCast(field_offset - prev_field_offset), .modify); |
| 5367 | 5433 | prev_field_offset = field_offset; |
| 5368 | 5434 | |
| ... | ... | @@ -5389,14 +5455,15 @@ fn airAggregateInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5389 | 5455 | } |
| 5390 | 5456 | |
| 5391 | 5457 | fn airUnionInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5392 | const mod = func.bin_file.base.comp.module.?; | |
| 5458 | const pt = func.pt; | |
| 5459 | const mod = pt.zcu; | |
| 5393 | 5460 | const ip = &mod.intern_pool; |
| 5394 | 5461 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5395 | 5462 | const extra = func.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 5396 | 5463 | |
| 5397 | 5464 | const result = result: { |
| 5398 | 5465 | const union_ty = func.typeOfIndex(inst); |
| 5399 | const layout = union_ty.unionGetLayout(mod); | |
| 5466 | const layout = union_ty.unionGetLayout(pt); | |
| 5400 | 5467 | const union_obj = mod.typeToUnion(union_ty).?; |
| 5401 | 5468 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[extra.field_index]); |
| 5402 | 5469 | const field_name = union_obj.loadTagType(ip).names.get(ip)[extra.field_index]; |
| ... | ... | @@ -5404,22 +5471,22 @@ fn airUnionInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5404 | 5471 | const tag_int = blk: { |
| 5405 | 5472 | const tag_ty = union_ty.unionTagTypeHypothetical(mod); |
| 5406 | 5473 | const enum_field_index = tag_ty.enumFieldIndex(field_name, mod).?; |
| 5407 | const tag_val = try mod.enumValueFieldIndex(tag_ty, enum_field_index); | |
| 5474 | const tag_val = try pt.enumValueFieldIndex(tag_ty, enum_field_index); | |
| 5408 | 5475 | break :blk try func.lowerConstant(tag_val, tag_ty); |
| 5409 | 5476 | }; |
| 5410 | 5477 | if (layout.payload_size == 0) { |
| 5411 | 5478 | if (layout.tag_size == 0) { |
| 5412 | 5479 | break :result WValue{ .none = {} }; |
| 5413 | 5480 | } |
| 5414 | assert(!isByRef(union_ty, mod)); | |
| 5481 | assert(!isByRef(union_ty, pt)); | |
| 5415 | 5482 | break :result tag_int; |
| 5416 | 5483 | } |
| 5417 | 5484 | |
| 5418 | if (isByRef(union_ty, mod)) { | |
| 5485 | if (isByRef(union_ty, pt)) { | |
| 5419 | 5486 | const result_ptr = try func.allocStack(union_ty); |
| 5420 | 5487 | const payload = try func.resolveInst(extra.init); |
| 5421 | 5488 | if (layout.tag_align.compare(.gte, layout.payload_align)) { |
| 5422 | if (isByRef(field_ty, mod)) { | |
| 5489 | if (isByRef(field_ty, pt)) { | |
| 5423 | 5490 | const payload_ptr = try func.buildPointerOffset(result_ptr, layout.tag_size, .new); |
| 5424 | 5491 | try func.store(payload_ptr, payload, field_ty, 0); |
| 5425 | 5492 | } else { |
| ... | ... | @@ -5443,14 +5510,14 @@ fn airUnionInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5443 | 5510 | break :result result_ptr; |
| 5444 | 5511 | } else { |
| 5445 | 5512 | const operand = try func.resolveInst(extra.init); |
| 5446 | const union_int_type = try mod.intType(.unsigned, @as(u16, @intCast(union_ty.bitSize(mod)))); | |
| 5513 | const union_int_type = try pt.intType(.unsigned, @as(u16, @intCast(union_ty.bitSize(pt)))); | |
| 5447 | 5514 | if (field_ty.zigTypeTag(mod) == .Float) { |
| 5448 | const int_type = try mod.intType(.unsigned, @intCast(field_ty.bitSize(mod))); | |
| 5515 | const int_type = try pt.intType(.unsigned, @intCast(field_ty.bitSize(pt))); | |
| 5449 | 5516 | const bitcasted = try func.bitcast(field_ty, int_type, operand); |
| 5450 | 5517 | const casted = try func.trunc(bitcasted, int_type, union_int_type); |
| 5451 | 5518 | break :result try casted.toLocal(func, field_ty); |
| 5452 | 5519 | } else if (field_ty.isPtrAtRuntime(mod)) { |
| 5453 | const int_type = try mod.intType(.unsigned, @intCast(field_ty.bitSize(mod))); | |
| 5520 | const int_type = try pt.intType(.unsigned, @intCast(field_ty.bitSize(pt))); | |
| 5454 | 5521 | const casted = try func.intcast(operand, int_type, union_int_type); |
| 5455 | 5522 | break :result try casted.toLocal(func, field_ty); |
| 5456 | 5523 | } |
| ... | ... | @@ -5488,8 +5555,9 @@ fn airWasmMemoryGrow(func: *CodeGen, inst: Air.Inst.Index) !void { |
| 5488 | 5555 | } |
| 5489 | 5556 | |
| 5490 | 5557 | fn cmpOptionals(func: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.math.CompareOperator) InnerError!WValue { |
| 5491 | const mod = func.bin_file.base.comp.module.?; | |
| 5492 | assert(operand_ty.hasRuntimeBitsIgnoreComptime(mod)); | |
| 5558 | const pt = func.pt; | |
| 5559 | const mod = pt.zcu; | |
| 5560 | assert(operand_ty.hasRuntimeBitsIgnoreComptime(pt)); | |
| 5493 | 5561 | assert(op == .eq or op == .neq); |
| 5494 | 5562 | const payload_ty = operand_ty.optionalChild(mod); |
| 5495 | 5563 | |
| ... | ... | @@ -5506,7 +5574,7 @@ fn cmpOptionals(func: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op: |
| 5506 | 5574 | |
| 5507 | 5575 | _ = try func.load(lhs, payload_ty, 0); |
| 5508 | 5576 | _ = try func.load(rhs, payload_ty, 0); |
| 5509 | const opcode = buildOpcode(.{ .op = .ne, .valtype1 = typeToValtype(payload_ty, mod) }); | |
| 5577 | const opcode = buildOpcode(.{ .op = .ne, .valtype1 = typeToValtype(payload_ty, pt) }); | |
| 5510 | 5578 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 5511 | 5579 | try func.addLabel(.br_if, 0); |
| 5512 | 5580 | |
| ... | ... | @@ -5524,11 +5592,12 @@ fn cmpOptionals(func: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op: |
| 5524 | 5592 | /// NOTE: Leaves the result of the comparison on top of the stack. |
| 5525 | 5593 | /// TODO: Lower this to compiler_rt call when bitsize > 128 |
| 5526 | 5594 | fn cmpBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.math.CompareOperator) InnerError!WValue { |
| 5527 | const mod = func.bin_file.base.comp.module.?; | |
| 5528 | assert(operand_ty.abiSize(mod) >= 16); | |
| 5595 | const pt = func.pt; | |
| 5596 | const mod = pt.zcu; | |
| 5597 | assert(operand_ty.abiSize(pt) >= 16); | |
| 5529 | 5598 | assert(!(lhs != .stack and rhs == .stack)); |
| 5530 | if (operand_ty.bitSize(mod) > 128) { | |
| 5531 | return func.fail("TODO: Support cmpBigInt for integer bitsize: '{d}'", .{operand_ty.bitSize(mod)}); | |
| 5599 | if (operand_ty.bitSize(pt) > 128) { | |
| 5600 | return func.fail("TODO: Support cmpBigInt for integer bitsize: '{d}'", .{operand_ty.bitSize(pt)}); | |
| 5532 | 5601 | } |
| 5533 | 5602 | |
| 5534 | 5603 | var lhs_high_bit = try (try func.load(lhs, Type.u64, 0)).toLocal(func, Type.u64); |
| ... | ... | @@ -5566,11 +5635,12 @@ fn cmpBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op: std |
| 5566 | 5635 | } |
| 5567 | 5636 | |
| 5568 | 5637 | fn airSetUnionTag(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5569 | const mod = func.bin_file.base.comp.module.?; | |
| 5638 | const pt = func.pt; | |
| 5639 | const mod = pt.zcu; | |
| 5570 | 5640 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 5571 | 5641 | const un_ty = func.typeOf(bin_op.lhs).childType(mod); |
| 5572 | 5642 | const tag_ty = func.typeOf(bin_op.rhs); |
| 5573 | const layout = un_ty.unionGetLayout(mod); | |
| 5643 | const layout = un_ty.unionGetLayout(pt); | |
| 5574 | 5644 | if (layout.tag_size == 0) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 5575 | 5645 | |
| 5576 | 5646 | const union_ptr = try func.resolveInst(bin_op.lhs); |
| ... | ... | @@ -5590,12 +5660,12 @@ fn airSetUnionTag(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5590 | 5660 | } |
| 5591 | 5661 | |
| 5592 | 5662 | fn airGetUnionTag(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5593 | const mod = func.bin_file.base.comp.module.?; | |
| 5663 | const pt = func.pt; | |
| 5594 | 5664 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5595 | 5665 | |
| 5596 | 5666 | const un_ty = func.typeOf(ty_op.operand); |
| 5597 | 5667 | const tag_ty = func.typeOfIndex(inst); |
| 5598 | const layout = un_ty.unionGetLayout(mod); | |
| 5668 | const layout = un_ty.unionGetLayout(pt); | |
| 5599 | 5669 | if (layout.tag_size == 0) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 5600 | 5670 | |
| 5601 | 5671 | const operand = try func.resolveInst(ty_op.operand); |
| ... | ... | @@ -5695,7 +5765,8 @@ fn fptrunc(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerErro |
| 5695 | 5765 | } |
| 5696 | 5766 | |
| 5697 | 5767 | fn airErrUnionPayloadPtrSet(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5698 | const mod = func.bin_file.base.comp.module.?; | |
| 5768 | const pt = func.pt; | |
| 5769 | const mod = pt.zcu; | |
| 5699 | 5770 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5700 | 5771 | |
| 5701 | 5772 | const err_set_ty = func.typeOf(ty_op.operand).childType(mod); |
| ... | ... | @@ -5707,27 +5778,28 @@ fn airErrUnionPayloadPtrSet(func: *CodeGen, inst: Air.Inst.Index) InnerError!voi |
| 5707 | 5778 | operand, |
| 5708 | 5779 | .{ .imm32 = 0 }, |
| 5709 | 5780 | Type.anyerror, |
| 5710 | @as(u32, @intCast(errUnionErrorOffset(payload_ty, mod))), | |
| 5781 | @intCast(errUnionErrorOffset(payload_ty, pt)), | |
| 5711 | 5782 | ); |
| 5712 | 5783 | |
| 5713 | 5784 | const result = result: { |
| 5714 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 5785 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 5715 | 5786 | break :result func.reuseOperand(ty_op.operand, operand); |
| 5716 | 5787 | } |
| 5717 | 5788 | |
| 5718 | break :result try func.buildPointerOffset(operand, @as(u32, @intCast(errUnionPayloadOffset(payload_ty, mod))), .new); | |
| 5789 | break :result try func.buildPointerOffset(operand, @as(u32, @intCast(errUnionPayloadOffset(payload_ty, pt))), .new); | |
| 5719 | 5790 | }; |
| 5720 | 5791 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5721 | 5792 | } |
| 5722 | 5793 | |
| 5723 | 5794 | fn airFieldParentPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5724 | const mod = func.bin_file.base.comp.module.?; | |
| 5795 | const pt = func.pt; | |
| 5796 | const mod = pt.zcu; | |
| 5725 | 5797 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5726 | 5798 | const extra = func.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| 5727 | 5799 | |
| 5728 | 5800 | const field_ptr = try func.resolveInst(extra.field_ptr); |
| 5729 | 5801 | const parent_ty = ty_pl.ty.toType().childType(mod); |
| 5730 | const field_offset = parent_ty.structFieldOffset(extra.field_index, mod); | |
| 5802 | const field_offset = parent_ty.structFieldOffset(extra.field_index, pt); | |
| 5731 | 5803 | |
| 5732 | 5804 | const result = if (field_offset != 0) result: { |
| 5733 | 5805 | const base = try func.buildPointerOffset(field_ptr, 0, .new); |
| ... | ... | @@ -5742,7 +5814,8 @@ fn airFieldParentPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5742 | 5814 | } |
| 5743 | 5815 | |
| 5744 | 5816 | fn sliceOrArrayPtr(func: *CodeGen, ptr: WValue, ptr_ty: Type) InnerError!WValue { |
| 5745 | const mod = func.bin_file.base.comp.module.?; | |
| 5817 | const pt = func.pt; | |
| 5818 | const mod = pt.zcu; | |
| 5746 | 5819 | if (ptr_ty.isSlice(mod)) { |
| 5747 | 5820 | return func.slicePtr(ptr); |
| 5748 | 5821 | } else { |
| ... | ... | @@ -5751,7 +5824,8 @@ fn sliceOrArrayPtr(func: *CodeGen, ptr: WValue, ptr_ty: Type) InnerError!WValue |
| 5751 | 5824 | } |
| 5752 | 5825 | |
| 5753 | 5826 | fn airMemcpy(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5754 | const mod = func.bin_file.base.comp.module.?; | |
| 5827 | const pt = func.pt; | |
| 5828 | const mod = pt.zcu; | |
| 5755 | 5829 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 5756 | 5830 | const dst = try func.resolveInst(bin_op.lhs); |
| 5757 | 5831 | const dst_ty = func.typeOf(bin_op.lhs); |
| ... | ... | @@ -5761,16 +5835,16 @@ fn airMemcpy(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5761 | 5835 | const len = switch (dst_ty.ptrSize(mod)) { |
| 5762 | 5836 | .Slice => blk: { |
| 5763 | 5837 | const slice_len = try func.sliceLen(dst); |
| 5764 | if (ptr_elem_ty.abiSize(mod) != 1) { | |
| 5838 | if (ptr_elem_ty.abiSize(pt) != 1) { | |
| 5765 | 5839 | try func.emitWValue(slice_len); |
| 5766 | try func.emitWValue(.{ .imm32 = @as(u32, @intCast(ptr_elem_ty.abiSize(mod))) }); | |
| 5840 | try func.emitWValue(.{ .imm32 = @as(u32, @intCast(ptr_elem_ty.abiSize(pt))) }); | |
| 5767 | 5841 | try func.addTag(.i32_mul); |
| 5768 | 5842 | try func.addLabel(.local_set, slice_len.local.value); |
| 5769 | 5843 | } |
| 5770 | 5844 | break :blk slice_len; |
| 5771 | 5845 | }, |
| 5772 | 5846 | .One => @as(WValue, .{ |
| 5773 | .imm32 = @as(u32, @intCast(ptr_elem_ty.arrayLen(mod) * ptr_elem_ty.childType(mod).abiSize(mod))), | |
| 5847 | .imm32 = @as(u32, @intCast(ptr_elem_ty.arrayLen(mod) * ptr_elem_ty.childType(mod).abiSize(pt))), | |
| 5774 | 5848 | }), |
| 5775 | 5849 | .C, .Many => unreachable, |
| 5776 | 5850 | }; |
| ... | ... | @@ -5791,7 +5865,8 @@ fn airRetAddr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5791 | 5865 | } |
| 5792 | 5866 | |
| 5793 | 5867 | fn airPopcount(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5794 | const mod = func.bin_file.base.comp.module.?; | |
| 5868 | const pt = func.pt; | |
| 5869 | const mod = pt.zcu; | |
| 5795 | 5870 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5796 | 5871 | |
| 5797 | 5872 | const operand = try func.resolveInst(ty_op.operand); |
| ... | ... | @@ -5812,14 +5887,14 @@ fn airPopcount(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5812 | 5887 | 32 => { |
| 5813 | 5888 | try func.emitWValue(operand); |
| 5814 | 5889 | if (op_ty.isSignedInt(mod) and bits != wasm_bits) { |
| 5815 | _ = try func.wrapOperand(.stack, try mod.intType(.unsigned, bits)); | |
| 5890 | _ = try func.wrapOperand(.stack, try pt.intType(.unsigned, bits)); | |
| 5816 | 5891 | } |
| 5817 | 5892 | try func.addTag(.i32_popcnt); |
| 5818 | 5893 | }, |
| 5819 | 5894 | 64 => { |
| 5820 | 5895 | try func.emitWValue(operand); |
| 5821 | 5896 | if (op_ty.isSignedInt(mod) and bits != wasm_bits) { |
| 5822 | _ = try func.wrapOperand(.stack, try mod.intType(.unsigned, bits)); | |
| 5897 | _ = try func.wrapOperand(.stack, try pt.intType(.unsigned, bits)); | |
| 5823 | 5898 | } |
| 5824 | 5899 | try func.addTag(.i64_popcnt); |
| 5825 | 5900 | try func.addTag(.i32_wrap_i64); |
| ... | ... | @@ -5830,7 +5905,7 @@ fn airPopcount(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5830 | 5905 | try func.addTag(.i64_popcnt); |
| 5831 | 5906 | _ = try func.load(operand, Type.u64, 8); |
| 5832 | 5907 | if (op_ty.isSignedInt(mod) and bits != wasm_bits) { |
| 5833 | _ = try func.wrapOperand(.stack, try mod.intType(.unsigned, bits - 64)); | |
| 5908 | _ = try func.wrapOperand(.stack, try pt.intType(.unsigned, bits - 64)); | |
| 5834 | 5909 | } |
| 5835 | 5910 | try func.addTag(.i64_popcnt); |
| 5836 | 5911 | try func.addTag(.i64_add); |
| ... | ... | @@ -5845,7 +5920,8 @@ fn airPopcount(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5845 | 5920 | } |
| 5846 | 5921 | |
| 5847 | 5922 | fn airBitReverse(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5848 | const mod = func.bin_file.base.comp.module.?; | |
| 5923 | const pt = func.pt; | |
| 5924 | const mod = pt.zcu; | |
| 5849 | 5925 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5850 | 5926 | |
| 5851 | 5927 | const operand = try func.resolveInst(ty_op.operand); |
| ... | ... | @@ -5956,10 +6032,10 @@ fn airErrorName(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5956 | 6032 | // |
| 5957 | 6033 | // As the names are global and the slice elements are constant, we do not have |
| 5958 | 6034 | // to make a copy of the ptr+value but can point towards them directly. |
| 5959 | const error_table_symbol = try func.bin_file.getErrorTableSymbol(); | |
| 6035 | const pt = func.pt; | |
| 6036 | const error_table_symbol = try func.bin_file.getErrorTableSymbol(pt); | |
| 5960 | 6037 | const name_ty = Type.slice_const_u8_sentinel_0; |
| 5961 | const mod = func.bin_file.base.comp.module.?; | |
| 5962 | const abi_size = name_ty.abiSize(mod); | |
| 6038 | const abi_size = name_ty.abiSize(pt); | |
| 5963 | 6039 | |
| 5964 | 6040 | const error_name_value: WValue = .{ .memory = error_table_symbol }; // emitting this will create a relocation |
| 5965 | 6041 | try func.emitWValue(error_name_value); |
| ... | ... | @@ -5998,7 +6074,8 @@ fn airAddSubWithOverflow(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerErro |
| 5998 | 6074 | const lhs = try func.resolveInst(extra.lhs); |
| 5999 | 6075 | const rhs = try func.resolveInst(extra.rhs); |
| 6000 | 6076 | const lhs_ty = func.typeOf(extra.lhs); |
| 6001 | const mod = func.bin_file.base.comp.module.?; | |
| 6077 | const pt = func.pt; | |
| 6078 | const mod = pt.zcu; | |
| 6002 | 6079 | |
| 6003 | 6080 | if (lhs_ty.zigTypeTag(mod) == .Vector) { |
| 6004 | 6081 | return func.fail("TODO: Implement overflow arithmetic for vectors", .{}); |
| ... | ... | @@ -6044,14 +6121,15 @@ fn airAddSubWithOverflow(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerErro |
| 6044 | 6121 | |
| 6045 | 6122 | const result_ptr = try func.allocStack(func.typeOfIndex(inst)); |
| 6046 | 6123 | try func.store(result_ptr, result, lhs_ty, 0); |
| 6047 | const offset = @as(u32, @intCast(lhs_ty.abiSize(mod))); | |
| 6124 | const offset = @as(u32, @intCast(lhs_ty.abiSize(pt))); | |
| 6048 | 6125 | try func.store(result_ptr, overflow_local, Type.u1, offset); |
| 6049 | 6126 | |
| 6050 | 6127 | func.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| 6051 | 6128 | } |
| 6052 | 6129 | |
| 6053 | 6130 | fn addSubWithOverflowBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, result_ty: Type, op: Op) InnerError!WValue { |
| 6054 | const mod = func.bin_file.base.comp.module.?; | |
| 6131 | const pt = func.pt; | |
| 6132 | const mod = pt.zcu; | |
| 6055 | 6133 | assert(op == .add or op == .sub); |
| 6056 | 6134 | const int_info = ty.intInfo(mod); |
| 6057 | 6135 | const is_signed = int_info.signedness == .signed; |
| ... | ... | @@ -6116,7 +6194,8 @@ fn addSubWithOverflowBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, |
| 6116 | 6194 | } |
| 6117 | 6195 | |
| 6118 | 6196 | fn airShlWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6119 | const mod = func.bin_file.base.comp.module.?; | |
| 6197 | const pt = func.pt; | |
| 6198 | const mod = pt.zcu; | |
| 6120 | 6199 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6121 | 6200 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 6122 | 6201 | |
| ... | ... | @@ -6159,7 +6238,7 @@ fn airShlWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6159 | 6238 | |
| 6160 | 6239 | const result_ptr = try func.allocStack(func.typeOfIndex(inst)); |
| 6161 | 6240 | try func.store(result_ptr, result, lhs_ty, 0); |
| 6162 | const offset = @as(u32, @intCast(lhs_ty.abiSize(mod))); | |
| 6241 | const offset = @as(u32, @intCast(lhs_ty.abiSize(pt))); | |
| 6163 | 6242 | try func.store(result_ptr, overflow_local, Type.u1, offset); |
| 6164 | 6243 | |
| 6165 | 6244 | func.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| ... | ... | @@ -6172,7 +6251,8 @@ fn airMulWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6172 | 6251 | const lhs = try func.resolveInst(extra.lhs); |
| 6173 | 6252 | const rhs = try func.resolveInst(extra.rhs); |
| 6174 | 6253 | const lhs_ty = func.typeOf(extra.lhs); |
| 6175 | const mod = func.bin_file.base.comp.module.?; | |
| 6254 | const pt = func.pt; | |
| 6255 | const mod = pt.zcu; | |
| 6176 | 6256 | |
| 6177 | 6257 | if (lhs_ty.zigTypeTag(mod) == .Vector) { |
| 6178 | 6258 | return func.fail("TODO: Implement overflow arithmetic for vectors", .{}); |
| ... | ... | @@ -6332,7 +6412,7 @@ fn airMulWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6332 | 6412 | |
| 6333 | 6413 | const result_ptr = try func.allocStack(func.typeOfIndex(inst)); |
| 6334 | 6414 | try func.store(result_ptr, bin_op_local, lhs_ty, 0); |
| 6335 | const offset = @as(u32, @intCast(lhs_ty.abiSize(mod))); | |
| 6415 | const offset = @as(u32, @intCast(lhs_ty.abiSize(pt))); | |
| 6336 | 6416 | try func.store(result_ptr, overflow_bit, Type.u1, offset); |
| 6337 | 6417 | |
| 6338 | 6418 | func.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| ... | ... | @@ -6340,7 +6420,8 @@ fn airMulWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6340 | 6420 | |
| 6341 | 6421 | fn airMaxMin(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 6342 | 6422 | assert(op == .max or op == .min); |
| 6343 | const mod = func.bin_file.base.comp.module.?; | |
| 6423 | const pt = func.pt; | |
| 6424 | const mod = pt.zcu; | |
| 6344 | 6425 | const target = mod.getTarget(); |
| 6345 | 6426 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6346 | 6427 | |
| ... | ... | @@ -6349,7 +6430,7 @@ fn airMaxMin(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 6349 | 6430 | return func.fail("TODO: `@maximum` and `@minimum` for vectors", .{}); |
| 6350 | 6431 | } |
| 6351 | 6432 | |
| 6352 | if (ty.abiSize(mod) > 16) { | |
| 6433 | if (ty.abiSize(pt) > 16) { | |
| 6353 | 6434 | return func.fail("TODO: `@maximum` and `@minimum` for types larger than 16 bytes", .{}); |
| 6354 | 6435 | } |
| 6355 | 6436 | |
| ... | ... | @@ -6377,14 +6458,15 @@ fn airMaxMin(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 6377 | 6458 | } |
| 6378 | 6459 | |
| 6379 | 6460 | // store result in local |
| 6380 | const result_ty = if (isByRef(ty, mod)) Type.u32 else ty; | |
| 6461 | const result_ty = if (isByRef(ty, pt)) Type.u32 else ty; | |
| 6381 | 6462 | const result = try func.allocLocal(result_ty); |
| 6382 | 6463 | try func.addLabel(.local_set, result.local.value); |
| 6383 | 6464 | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 6384 | 6465 | } |
| 6385 | 6466 | |
| 6386 | 6467 | fn airMulAdd(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6387 | const mod = func.bin_file.base.comp.module.?; | |
| 6468 | const pt = func.pt; | |
| 6469 | const mod = pt.zcu; | |
| 6388 | 6470 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6389 | 6471 | const bin_op = func.air.extraData(Air.Bin, pl_op.payload).data; |
| 6390 | 6472 | |
| ... | ... | @@ -6418,7 +6500,8 @@ fn airMulAdd(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6418 | 6500 | } |
| 6419 | 6501 | |
| 6420 | 6502 | fn airClz(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6421 | const mod = func.bin_file.base.comp.module.?; | |
| 6503 | const pt = func.pt; | |
| 6504 | const mod = pt.zcu; | |
| 6422 | 6505 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6423 | 6506 | |
| 6424 | 6507 | const ty = func.typeOf(ty_op.operand); |
| ... | ... | @@ -6471,7 +6554,8 @@ fn airClz(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6471 | 6554 | } |
| 6472 | 6555 | |
| 6473 | 6556 | fn airCtz(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6474 | const mod = func.bin_file.base.comp.module.?; | |
| 6557 | const pt = func.pt; | |
| 6558 | const mod = pt.zcu; | |
| 6475 | 6559 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6476 | 6560 | |
| 6477 | 6561 | const ty = func.typeOf(ty_op.operand); |
| ... | ... | @@ -6558,7 +6642,8 @@ fn airDbgInlineBlock(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6558 | 6642 | fn airDbgVar(func: *CodeGen, inst: Air.Inst.Index, is_ptr: bool) InnerError!void { |
| 6559 | 6643 | if (func.debug_output != .dwarf) return func.finishAir(inst, .none, &.{}); |
| 6560 | 6644 | |
| 6561 | const mod = func.bin_file.base.comp.module.?; | |
| 6645 | const pt = func.pt; | |
| 6646 | const mod = pt.zcu; | |
| 6562 | 6647 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6563 | 6648 | const ty = func.typeOf(pl_op.operand); |
| 6564 | 6649 | const operand = try func.resolveInst(pl_op.operand); |
| ... | ... | @@ -6591,7 +6676,8 @@ fn airTry(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6591 | 6676 | } |
| 6592 | 6677 | |
| 6593 | 6678 | fn airTryPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6594 | const mod = func.bin_file.base.comp.module.?; | |
| 6679 | const pt = func.pt; | |
| 6680 | const mod = pt.zcu; | |
| 6595 | 6681 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6596 | 6682 | const extra = func.air.extraData(Air.TryPtr, ty_pl.payload); |
| 6597 | 6683 | const err_union_ptr = try func.resolveInst(extra.data.ptr); |
| ... | ... | @@ -6609,13 +6695,14 @@ fn lowerTry( |
| 6609 | 6695 | err_union_ty: Type, |
| 6610 | 6696 | operand_is_ptr: bool, |
| 6611 | 6697 | ) InnerError!WValue { |
| 6612 | const mod = func.bin_file.base.comp.module.?; | |
| 6698 | const pt = func.pt; | |
| 6699 | const mod = pt.zcu; | |
| 6613 | 6700 | if (operand_is_ptr) { |
| 6614 | 6701 | return func.fail("TODO: lowerTry for pointers", .{}); |
| 6615 | 6702 | } |
| 6616 | 6703 | |
| 6617 | 6704 | const pl_ty = err_union_ty.errorUnionPayload(mod); |
| 6618 | const pl_has_bits = pl_ty.hasRuntimeBitsIgnoreComptime(mod); | |
| 6705 | const pl_has_bits = pl_ty.hasRuntimeBitsIgnoreComptime(pt); | |
| 6619 | 6706 | |
| 6620 | 6707 | if (!err_union_ty.errorUnionSet(mod).errorSetIsEmpty(mod)) { |
| 6621 | 6708 | // Block we can jump out of when error is not set |
| ... | ... | @@ -6624,10 +6711,10 @@ fn lowerTry( |
| 6624 | 6711 | // check if the error tag is set for the error union. |
| 6625 | 6712 | try func.emitWValue(err_union); |
| 6626 | 6713 | if (pl_has_bits) { |
| 6627 | const err_offset = @as(u32, @intCast(errUnionErrorOffset(pl_ty, mod))); | |
| 6714 | const err_offset: u32 = @intCast(errUnionErrorOffset(pl_ty, pt)); | |
| 6628 | 6715 | try func.addMemArg(.i32_load16_u, .{ |
| 6629 | 6716 | .offset = err_union.offset() + err_offset, |
| 6630 | .alignment = @intCast(Type.anyerror.abiAlignment(mod).toByteUnits().?), | |
| 6717 | .alignment = @intCast(Type.anyerror.abiAlignment(pt).toByteUnits().?), | |
| 6631 | 6718 | }); |
| 6632 | 6719 | } |
| 6633 | 6720 | try func.addTag(.i32_eqz); |
| ... | ... | @@ -6649,8 +6736,8 @@ fn lowerTry( |
| 6649 | 6736 | return WValue{ .none = {} }; |
| 6650 | 6737 | } |
| 6651 | 6738 | |
| 6652 | const pl_offset = @as(u32, @intCast(errUnionPayloadOffset(pl_ty, mod))); | |
| 6653 | if (isByRef(pl_ty, mod)) { | |
| 6739 | const pl_offset: u32 = @intCast(errUnionPayloadOffset(pl_ty, pt)); | |
| 6740 | if (isByRef(pl_ty, pt)) { | |
| 6654 | 6741 | return buildPointerOffset(func, err_union, pl_offset, .new); |
| 6655 | 6742 | } |
| 6656 | 6743 | const payload = try func.load(err_union, pl_ty, pl_offset); |
| ... | ... | @@ -6658,7 +6745,8 @@ fn lowerTry( |
| 6658 | 6745 | } |
| 6659 | 6746 | |
| 6660 | 6747 | fn airByteSwap(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6661 | const mod = func.bin_file.base.comp.module.?; | |
| 6748 | const pt = func.pt; | |
| 6749 | const mod = pt.zcu; | |
| 6662 | 6750 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6663 | 6751 | |
| 6664 | 6752 | const ty = func.typeOfIndex(inst); |
| ... | ... | @@ -6744,7 +6832,8 @@ fn airDivTrunc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6744 | 6832 | fn airDivFloor(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6745 | 6833 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6746 | 6834 | |
| 6747 | const mod = func.bin_file.base.comp.module.?; | |
| 6835 | const pt = func.pt; | |
| 6836 | const mod = pt.zcu; | |
| 6748 | 6837 | const ty = func.typeOfIndex(inst); |
| 6749 | 6838 | const lhs = try func.resolveInst(bin_op.lhs); |
| 6750 | 6839 | const rhs = try func.resolveInst(bin_op.rhs); |
| ... | ... | @@ -6864,7 +6953,8 @@ fn airRem(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6864 | 6953 | fn airMod(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 6865 | 6954 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6866 | 6955 | |
| 6867 | const mod = func.bin_file.base.comp.module.?; | |
| 6956 | const pt = func.pt; | |
| 6957 | const mod = pt.zcu; | |
| 6868 | 6958 | const ty = func.typeOfIndex(inst); |
| 6869 | 6959 | const lhs = try func.resolveInst(bin_op.lhs); |
| 6870 | 6960 | const rhs = try func.resolveInst(bin_op.rhs); |
| ... | ... | @@ -6901,7 +6991,8 @@ fn airSatBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 6901 | 6991 | assert(op == .add or op == .sub); |
| 6902 | 6992 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6903 | 6993 | |
| 6904 | const mod = func.bin_file.base.comp.module.?; | |
| 6994 | const pt = func.pt; | |
| 6995 | const mod = pt.zcu; | |
| 6905 | 6996 | const ty = func.typeOfIndex(inst); |
| 6906 | 6997 | const lhs = try func.resolveInst(bin_op.lhs); |
| 6907 | 6998 | const rhs = try func.resolveInst(bin_op.rhs); |
| ... | ... | @@ -6949,11 +7040,12 @@ fn airSatBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 6949 | 7040 | } |
| 6950 | 7041 | |
| 6951 | 7042 | fn signedSat(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError!WValue { |
| 6952 | const mod = func.bin_file.base.comp.module.?; | |
| 7043 | const pt = func.pt; | |
| 7044 | const mod = pt.zcu; | |
| 6953 | 7045 | const int_info = ty.intInfo(mod); |
| 6954 | 7046 | const wasm_bits = toWasmBits(int_info.bits).?; |
| 6955 | 7047 | const is_wasm_bits = wasm_bits == int_info.bits; |
| 6956 | const ext_ty = if (!is_wasm_bits) try mod.intType(int_info.signedness, wasm_bits) else ty; | |
| 7048 | const ext_ty = if (!is_wasm_bits) try pt.intType(int_info.signedness, wasm_bits) else ty; | |
| 6957 | 7049 | |
| 6958 | 7050 | const max_val: u64 = @as(u64, @intCast((@as(u65, 1) << @as(u7, @intCast(int_info.bits - 1))) - 1)); |
| 6959 | 7051 | const min_val: i64 = (-@as(i64, @intCast(@as(u63, @intCast(max_val))))) - 1; |
| ... | ... | @@ -7007,7 +7099,8 @@ fn signedSat(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerEr |
| 7007 | 7099 | fn airShlSat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7008 | 7100 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7009 | 7101 | |
| 7010 | const mod = func.bin_file.base.comp.module.?; | |
| 7102 | const pt = func.pt; | |
| 7103 | const mod = pt.zcu; | |
| 7011 | 7104 | const ty = func.typeOfIndex(inst); |
| 7012 | 7105 | const int_info = ty.intInfo(mod); |
| 7013 | 7106 | const is_signed = int_info.signedness == .signed; |
| ... | ... | @@ -7061,7 +7154,7 @@ fn airShlSat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7061 | 7154 | 64 => WValue{ .imm64 = shift_size }, |
| 7062 | 7155 | else => unreachable, |
| 7063 | 7156 | }; |
| 7064 | const ext_ty = try mod.intType(int_info.signedness, wasm_bits); | |
| 7157 | const ext_ty = try pt.intType(int_info.signedness, wasm_bits); | |
| 7065 | 7158 | |
| 7066 | 7159 | var shl_res = try (try func.binOp(lhs, shift_value, ext_ty, .shl)).toLocal(func, ext_ty); |
| 7067 | 7160 | defer shl_res.free(func); |
| ... | ... | @@ -7128,13 +7221,14 @@ fn callIntrinsic( |
| 7128 | 7221 | }; |
| 7129 | 7222 | |
| 7130 | 7223 | // Always pass over C-ABI |
| 7131 | const mod = func.bin_file.base.comp.module.?; | |
| 7132 | var func_type = try genFunctype(func.gpa, .C, param_types, return_type, mod); | |
| 7224 | const pt = func.pt; | |
| 7225 | const mod = pt.zcu; | |
| 7226 | var func_type = try genFunctype(func.gpa, .C, param_types, return_type, pt); | |
| 7133 | 7227 | defer func_type.deinit(func.gpa); |
| 7134 | 7228 | const func_type_index = try func.bin_file.zigObjectPtr().?.putOrGetFuncType(func.gpa, func_type); |
| 7135 | 7229 | try func.bin_file.addOrUpdateImport(name, symbol_index, null, func_type_index); |
| 7136 | 7230 | |
| 7137 | const want_sret_param = firstParamSRet(.C, return_type, mod); | |
| 7231 | const want_sret_param = firstParamSRet(.C, return_type, pt); | |
| 7138 | 7232 | // if we want return as first param, we allocate a pointer to stack, |
| 7139 | 7233 | // and emit it as our first argument |
| 7140 | 7234 | const sret = if (want_sret_param) blk: { |
| ... | ... | @@ -7146,14 +7240,14 @@ fn callIntrinsic( |
| 7146 | 7240 | // Lower all arguments to the stack before we call our function |
| 7147 | 7241 | for (args, 0..) |arg, arg_i| { |
| 7148 | 7242 | assert(!(want_sret_param and arg == .stack)); |
| 7149 | assert(Type.fromInterned(param_types[arg_i]).hasRuntimeBitsIgnoreComptime(mod)); | |
| 7243 | assert(Type.fromInterned(param_types[arg_i]).hasRuntimeBitsIgnoreComptime(pt)); | |
| 7150 | 7244 | try func.lowerArg(.C, Type.fromInterned(param_types[arg_i]), arg); |
| 7151 | 7245 | } |
| 7152 | 7246 | |
| 7153 | 7247 | // Actually call our intrinsic |
| 7154 | 7248 | try func.addLabel(.call, @intFromEnum(symbol_index)); |
| 7155 | 7249 | |
| 7156 | if (!return_type.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 7250 | if (!return_type.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 7157 | 7251 | return WValue.none; |
| 7158 | 7252 | } else if (return_type.isNoReturn(mod)) { |
| 7159 | 7253 | try func.addTag(.@"unreachable"); |
| ... | ... | @@ -7181,7 +7275,8 @@ fn airTagName(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7181 | 7275 | } |
| 7182 | 7276 | |
| 7183 | 7277 | fn getTagNameFunction(func: *CodeGen, enum_ty: Type) InnerError!u32 { |
| 7184 | const mod = func.bin_file.base.comp.module.?; | |
| 7278 | const pt = func.pt; | |
| 7279 | const mod = pt.zcu; | |
| 7185 | 7280 | const ip = &mod.intern_pool; |
| 7186 | 7281 | const enum_decl_index = enum_ty.getOwnerDecl(mod); |
| 7187 | 7282 | |
| ... | ... | @@ -7199,7 +7294,7 @@ fn getTagNameFunction(func: *CodeGen, enum_ty: Type) InnerError!u32 { |
| 7199 | 7294 | |
| 7200 | 7295 | const int_tag_ty = enum_ty.intTagType(mod); |
| 7201 | 7296 | |
| 7202 | if (int_tag_ty.bitSize(mod) > 64) { | |
| 7297 | if (int_tag_ty.bitSize(pt) > 64) { | |
| 7203 | 7298 | return func.fail("TODO: Implement @tagName for enums with tag size larger than 64 bits", .{}); |
| 7204 | 7299 | } |
| 7205 | 7300 | |
| ... | ... | @@ -7225,16 +7320,17 @@ fn getTagNameFunction(func: *CodeGen, enum_ty: Type) InnerError!u32 { |
| 7225 | 7320 | const tag_name_len = tag_name.length(ip); |
| 7226 | 7321 | // for each tag name, create an unnamed const, |
| 7227 | 7322 | // and then get a pointer to its value. |
| 7228 | const name_ty = try mod.arrayType(.{ | |
| 7323 | const name_ty = try pt.arrayType(.{ | |
| 7229 | 7324 | .len = tag_name_len, |
| 7230 | 7325 | .child = .u8_type, |
| 7231 | 7326 | .sentinel = .zero_u8, |
| 7232 | 7327 | }); |
| 7233 | const name_val = try mod.intern(.{ .aggregate = .{ | |
| 7328 | const name_val = try pt.intern(.{ .aggregate = .{ | |
| 7234 | 7329 | .ty = name_ty.toIntern(), |
| 7235 | 7330 | .storage = .{ .bytes = tag_name.toString() }, |
| 7236 | 7331 | } }); |
| 7237 | 7332 | const tag_sym_index = try func.bin_file.lowerUnnamedConst( |
| 7333 | pt, | |
| 7238 | 7334 | Value.fromInterned(name_val), |
| 7239 | 7335 | enum_decl_index, |
| 7240 | 7336 | ); |
| ... | ... | @@ -7247,7 +7343,7 @@ fn getTagNameFunction(func: *CodeGen, enum_ty: Type) InnerError!u32 { |
| 7247 | 7343 | try writer.writeByte(std.wasm.opcode(.local_get)); |
| 7248 | 7344 | try leb.writeUleb128(writer, @as(u32, 1)); |
| 7249 | 7345 | |
| 7250 | const tag_val = try mod.enumValueFieldIndex(enum_ty, @intCast(tag_index)); | |
| 7346 | const tag_val = try pt.enumValueFieldIndex(enum_ty, @intCast(tag_index)); | |
| 7251 | 7347 | const tag_value = try func.lowerConstant(tag_val, enum_ty); |
| 7252 | 7348 | |
| 7253 | 7349 | switch (tag_value) { |
| ... | ... | @@ -7334,13 +7430,14 @@ fn getTagNameFunction(func: *CodeGen, enum_ty: Type) InnerError!u32 { |
| 7334 | 7430 | try writer.writeByte(std.wasm.opcode(.end)); |
| 7335 | 7431 | |
| 7336 | 7432 | const slice_ty = Type.slice_const_u8_sentinel_0; |
| 7337 | const func_type = try genFunctype(arena, .Unspecified, &.{int_tag_ty.ip_index}, slice_ty, mod); | |
| 7433 | const func_type = try genFunctype(arena, .Unspecified, &.{int_tag_ty.ip_index}, slice_ty, pt); | |
| 7338 | 7434 | const sym_index = try func.bin_file.createFunction(func_name, func_type, &body_list, &relocs); |
| 7339 | 7435 | return @intFromEnum(sym_index); |
| 7340 | 7436 | } |
| 7341 | 7437 | |
| 7342 | 7438 | fn airErrorSetHasValue(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7343 | const mod = func.bin_file.base.comp.module.?; | |
| 7439 | const pt = func.pt; | |
| 7440 | const mod = pt.zcu; | |
| 7344 | 7441 | const ip = &mod.intern_pool; |
| 7345 | 7442 | const ty_op = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7346 | 7443 | |
| ... | ... | @@ -7426,7 +7523,8 @@ inline fn useAtomicFeature(func: *const CodeGen) bool { |
| 7426 | 7523 | } |
| 7427 | 7524 | |
| 7428 | 7525 | fn airCmpxchg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7429 | const mod = func.bin_file.base.comp.module.?; | |
| 7526 | const pt = func.pt; | |
| 7527 | const mod = pt.zcu; | |
| 7430 | 7528 | const ty_pl = func.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 7431 | 7529 | const extra = func.air.extraData(Air.Cmpxchg, ty_pl.payload).data; |
| 7432 | 7530 | |
| ... | ... | @@ -7445,7 +7543,7 @@ fn airCmpxchg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7445 | 7543 | try func.emitWValue(ptr_operand); |
| 7446 | 7544 | try func.lowerToStack(expected_val); |
| 7447 | 7545 | try func.lowerToStack(new_val); |
| 7448 | try func.addAtomicMemArg(switch (ty.abiSize(mod)) { | |
| 7546 | try func.addAtomicMemArg(switch (ty.abiSize(pt)) { | |
| 7449 | 7547 | 1 => .i32_atomic_rmw8_cmpxchg_u, |
| 7450 | 7548 | 2 => .i32_atomic_rmw16_cmpxchg_u, |
| 7451 | 7549 | 4 => .i32_atomic_rmw_cmpxchg, |
| ... | ... | @@ -7453,14 +7551,14 @@ fn airCmpxchg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7453 | 7551 | else => |size| return func.fail("TODO: implement `@cmpxchg` for types with abi size '{d}'", .{size}), |
| 7454 | 7552 | }, .{ |
| 7455 | 7553 | .offset = ptr_operand.offset(), |
| 7456 | .alignment = @intCast(ty.abiAlignment(mod).toByteUnits().?), | |
| 7554 | .alignment = @intCast(ty.abiAlignment(pt).toByteUnits().?), | |
| 7457 | 7555 | }); |
| 7458 | 7556 | try func.addLabel(.local_tee, val_local.local.value); |
| 7459 | 7557 | _ = try func.cmp(.stack, expected_val, ty, .eq); |
| 7460 | 7558 | try func.addLabel(.local_set, cmp_result.local.value); |
| 7461 | 7559 | break :val val_local; |
| 7462 | 7560 | } else val: { |
| 7463 | if (ty.abiSize(mod) > 8) { | |
| 7561 | if (ty.abiSize(pt) > 8) { | |
| 7464 | 7562 | return func.fail("TODO: Implement `@cmpxchg` for types larger than abi size of 8 bytes", .{}); |
| 7465 | 7563 | } |
| 7466 | 7564 | const ptr_val = try WValue.toLocal(try func.load(ptr_operand, ty, 0), func, ty); |
| ... | ... | @@ -7476,7 +7574,7 @@ fn airCmpxchg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7476 | 7574 | break :val ptr_val; |
| 7477 | 7575 | }; |
| 7478 | 7576 | |
| 7479 | const result_ptr = if (isByRef(result_ty, mod)) val: { | |
| 7577 | const result_ptr = if (isByRef(result_ty, pt)) val: { | |
| 7480 | 7578 | try func.emitWValue(cmp_result); |
| 7481 | 7579 | try func.addImm32(~@as(u32, 0)); |
| 7482 | 7580 | try func.addTag(.i32_xor); |
| ... | ... | @@ -7484,7 +7582,7 @@ fn airCmpxchg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7484 | 7582 | try func.addTag(.i32_and); |
| 7485 | 7583 | const and_result = try WValue.toLocal(.stack, func, Type.bool); |
| 7486 | 7584 | const result_ptr = try func.allocStack(result_ty); |
| 7487 | try func.store(result_ptr, and_result, Type.bool, @as(u32, @intCast(ty.abiSize(mod)))); | |
| 7585 | try func.store(result_ptr, and_result, Type.bool, @as(u32, @intCast(ty.abiSize(pt)))); | |
| 7488 | 7586 | try func.store(result_ptr, ptr_val, ty, 0); |
| 7489 | 7587 | break :val result_ptr; |
| 7490 | 7588 | } else val: { |
| ... | ... | @@ -7499,13 +7597,13 @@ fn airCmpxchg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7499 | 7597 | } |
| 7500 | 7598 | |
| 7501 | 7599 | fn airAtomicLoad(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7502 | const mod = func.bin_file.base.comp.module.?; | |
| 7600 | const pt = func.pt; | |
| 7503 | 7601 | const atomic_load = func.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load; |
| 7504 | 7602 | const ptr = try func.resolveInst(atomic_load.ptr); |
| 7505 | 7603 | const ty = func.typeOfIndex(inst); |
| 7506 | 7604 | |
| 7507 | 7605 | if (func.useAtomicFeature()) { |
| 7508 | const tag: wasm.AtomicsOpcode = switch (ty.abiSize(mod)) { | |
| 7606 | const tag: wasm.AtomicsOpcode = switch (ty.abiSize(pt)) { | |
| 7509 | 7607 | 1 => .i32_atomic_load8_u, |
| 7510 | 7608 | 2 => .i32_atomic_load16_u, |
| 7511 | 7609 | 4 => .i32_atomic_load, |
| ... | ... | @@ -7515,7 +7613,7 @@ fn airAtomicLoad(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7515 | 7613 | try func.emitWValue(ptr); |
| 7516 | 7614 | try func.addAtomicMemArg(tag, .{ |
| 7517 | 7615 | .offset = ptr.offset(), |
| 7518 | .alignment = @intCast(ty.abiAlignment(mod).toByteUnits().?), | |
| 7616 | .alignment = @intCast(ty.abiAlignment(pt).toByteUnits().?), | |
| 7519 | 7617 | }); |
| 7520 | 7618 | } else { |
| 7521 | 7619 | _ = try func.load(ptr, ty, 0); |
| ... | ... | @@ -7526,7 +7624,8 @@ fn airAtomicLoad(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7526 | 7624 | } |
| 7527 | 7625 | |
| 7528 | 7626 | fn airAtomicRmw(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7529 | const mod = func.bin_file.base.comp.module.?; | |
| 7627 | const pt = func.pt; | |
| 7628 | const mod = pt.zcu; | |
| 7530 | 7629 | const pl_op = func.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 7531 | 7630 | const extra = func.air.extraData(Air.AtomicRmw, pl_op.payload).data; |
| 7532 | 7631 | |
| ... | ... | @@ -7550,7 +7649,7 @@ fn airAtomicRmw(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7550 | 7649 | try func.emitWValue(ptr); |
| 7551 | 7650 | try func.emitWValue(value); |
| 7552 | 7651 | if (op == .Nand) { |
| 7553 | const wasm_bits = toWasmBits(@as(u16, @intCast(ty.bitSize(mod)))).?; | |
| 7652 | const wasm_bits = toWasmBits(@intCast(ty.bitSize(pt))).?; | |
| 7554 | 7653 | |
| 7555 | 7654 | const and_res = try func.binOp(value, operand, ty, .@"and"); |
| 7556 | 7655 | if (wasm_bits == 32) |
| ... | ... | @@ -7567,7 +7666,7 @@ fn airAtomicRmw(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7567 | 7666 | try func.addTag(.select); |
| 7568 | 7667 | } |
| 7569 | 7668 | try func.addAtomicMemArg( |
| 7570 | switch (ty.abiSize(mod)) { | |
| 7669 | switch (ty.abiSize(pt)) { | |
| 7571 | 7670 | 1 => .i32_atomic_rmw8_cmpxchg_u, |
| 7572 | 7671 | 2 => .i32_atomic_rmw16_cmpxchg_u, |
| 7573 | 7672 | 4 => .i32_atomic_rmw_cmpxchg, |
| ... | ... | @@ -7576,7 +7675,7 @@ fn airAtomicRmw(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7576 | 7675 | }, |
| 7577 | 7676 | .{ |
| 7578 | 7677 | .offset = ptr.offset(), |
| 7579 | .alignment = @intCast(ty.abiAlignment(mod).toByteUnits().?), | |
| 7678 | .alignment = @intCast(ty.abiAlignment(pt).toByteUnits().?), | |
| 7580 | 7679 | }, |
| 7581 | 7680 | ); |
| 7582 | 7681 | const select_res = try func.allocLocal(ty); |
| ... | ... | @@ -7595,7 +7694,7 @@ fn airAtomicRmw(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7595 | 7694 | else => { |
| 7596 | 7695 | try func.emitWValue(ptr); |
| 7597 | 7696 | try func.emitWValue(operand); |
| 7598 | const tag: wasm.AtomicsOpcode = switch (ty.abiSize(mod)) { | |
| 7697 | const tag: wasm.AtomicsOpcode = switch (ty.abiSize(pt)) { | |
| 7599 | 7698 | 1 => switch (op) { |
| 7600 | 7699 | .Xchg => .i32_atomic_rmw8_xchg_u, |
| 7601 | 7700 | .Add => .i32_atomic_rmw8_add_u, |
| ... | ... | @@ -7636,7 +7735,7 @@ fn airAtomicRmw(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7636 | 7735 | }; |
| 7637 | 7736 | try func.addAtomicMemArg(tag, .{ |
| 7638 | 7737 | .offset = ptr.offset(), |
| 7639 | .alignment = @intCast(ty.abiAlignment(mod).toByteUnits().?), | |
| 7738 | .alignment = @intCast(ty.abiAlignment(pt).toByteUnits().?), | |
| 7640 | 7739 | }); |
| 7641 | 7740 | const result = try WValue.toLocal(.stack, func, ty); |
| 7642 | 7741 | return func.finishAir(inst, result, &.{ pl_op.operand, extra.operand }); |
| ... | ... | @@ -7681,7 +7780,7 @@ fn airAtomicRmw(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7681 | 7780 | try func.store(.stack, .stack, ty, ptr.offset()); |
| 7682 | 7781 | }, |
| 7683 | 7782 | .Nand => { |
| 7684 | const wasm_bits = toWasmBits(@as(u16, @intCast(ty.bitSize(mod)))).?; | |
| 7783 | const wasm_bits = toWasmBits(@intCast(ty.bitSize(pt))).?; | |
| 7685 | 7784 | |
| 7686 | 7785 | try func.emitWValue(ptr); |
| 7687 | 7786 | const and_res = try func.binOp(result, operand, ty, .@"and"); |
| ... | ... | @@ -7701,7 +7800,8 @@ fn airAtomicRmw(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7701 | 7800 | } |
| 7702 | 7801 | |
| 7703 | 7802 | fn airFence(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7704 | const zcu = func.bin_file.base.comp.module.?; | |
| 7803 | const pt = func.pt; | |
| 7804 | const zcu = pt.zcu; | |
| 7705 | 7805 | // Only when the atomic feature is enabled, and we're not building |
| 7706 | 7806 | // for a single-threaded build, can we emit the `fence` instruction. |
| 7707 | 7807 | // In all other cases, we emit no instructions for a fence. |
| ... | ... | @@ -7715,7 +7815,8 @@ fn airFence(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7715 | 7815 | } |
| 7716 | 7816 | |
| 7717 | 7817 | fn airAtomicStore(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7718 | const mod = func.bin_file.base.comp.module.?; | |
| 7818 | const pt = func.pt; | |
| 7819 | const mod = pt.zcu; | |
| 7719 | 7820 | const bin_op = func.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7720 | 7821 | |
| 7721 | 7822 | const ptr = try func.resolveInst(bin_op.lhs); |
| ... | ... | @@ -7724,7 +7825,7 @@ fn airAtomicStore(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7724 | 7825 | const ty = ptr_ty.childType(mod); |
| 7725 | 7826 | |
| 7726 | 7827 | if (func.useAtomicFeature()) { |
| 7727 | const tag: wasm.AtomicsOpcode = switch (ty.abiSize(mod)) { | |
| 7828 | const tag: wasm.AtomicsOpcode = switch (ty.abiSize(pt)) { | |
| 7728 | 7829 | 1 => .i32_atomic_store8, |
| 7729 | 7830 | 2 => .i32_atomic_store16, |
| 7730 | 7831 | 4 => .i32_atomic_store, |
| ... | ... | @@ -7735,7 +7836,7 @@ fn airAtomicStore(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7735 | 7836 | try func.lowerToStack(operand); |
| 7736 | 7837 | try func.addAtomicMemArg(tag, .{ |
| 7737 | 7838 | .offset = ptr.offset(), |
| 7738 | .alignment = @intCast(ty.abiAlignment(mod).toByteUnits().?), | |
| 7839 | .alignment = @intCast(ty.abiAlignment(pt).toByteUnits().?), | |
| 7739 | 7840 | }); |
| 7740 | 7841 | } else { |
| 7741 | 7842 | try func.store(ptr, operand, ty, 0); |
| ... | ... | @@ -7754,11 +7855,13 @@ fn airFrameAddress(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 7754 | 7855 | } |
| 7755 | 7856 | |
| 7756 | 7857 | fn typeOf(func: *CodeGen, inst: Air.Inst.Ref) Type { |
| 7757 | const mod = func.bin_file.base.comp.module.?; | |
| 7858 | const pt = func.pt; | |
| 7859 | const mod = pt.zcu; | |
| 7758 | 7860 | return func.air.typeOf(inst, &mod.intern_pool); |
| 7759 | 7861 | } |
| 7760 | 7862 | |
| 7761 | 7863 | fn typeOfIndex(func: *CodeGen, inst: Air.Inst.Index) Type { |
| 7762 | const mod = func.bin_file.base.comp.module.?; | |
| 7864 | const pt = func.pt; | |
| 7865 | const mod = pt.zcu; | |
| 7763 | 7866 | return func.air.typeOfIndex(inst, &mod.intern_pool); |
| 7764 | 7867 | } |
src/arch/wasm/abi.zig+21-19| ... | ... | @@ -22,15 +22,16 @@ const direct: [2]Class = .{ .direct, .none }; |
| 22 | 22 | /// Classifies a given Zig type to determine how they must be passed |
| 23 | 23 | /// or returned as value within a wasm function. |
| 24 | 24 | /// When all elements result in `.none`, no value must be passed in or returned. |
| 25 | pub fn classifyType(ty: Type, mod: *Zcu) [2]Class { | |
| 25 | pub fn classifyType(ty: Type, pt: Zcu.PerThread) [2]Class { | |
| 26 | const mod = pt.zcu; | |
| 26 | 27 | const ip = &mod.intern_pool; |
| 27 | 28 | const target = mod.getTarget(); |
| 28 | if (!ty.hasRuntimeBitsIgnoreComptime(mod)) return none; | |
| 29 | if (!ty.hasRuntimeBitsIgnoreComptime(pt)) return none; | |
| 29 | 30 | switch (ty.zigTypeTag(mod)) { |
| 30 | 31 | .Struct => { |
| 31 | const struct_type = mod.typeToStruct(ty).?; | |
| 32 | const struct_type = pt.zcu.typeToStruct(ty).?; | |
| 32 | 33 | if (struct_type.layout == .@"packed") { |
| 33 | if (ty.bitSize(mod) <= 64) return direct; | |
| 34 | if (ty.bitSize(pt) <= 64) return direct; | |
| 34 | 35 | return .{ .direct, .direct }; |
| 35 | 36 | } |
| 36 | 37 | if (struct_type.field_types.len > 1) { |
| ... | ... | @@ -40,13 +41,13 @@ pub fn classifyType(ty: Type, mod: *Zcu) [2]Class { |
| 40 | 41 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[0]); |
| 41 | 42 | const explicit_align = struct_type.fieldAlign(ip, 0); |
| 42 | 43 | if (explicit_align != .none) { |
| 43 | if (explicit_align.compareStrict(.gt, field_ty.abiAlignment(mod))) | |
| 44 | if (explicit_align.compareStrict(.gt, field_ty.abiAlignment(pt))) | |
| 44 | 45 | return memory; |
| 45 | 46 | } |
| 46 | return classifyType(field_ty, mod); | |
| 47 | return classifyType(field_ty, pt); | |
| 47 | 48 | }, |
| 48 | 49 | .Int, .Enum, .ErrorSet => { |
| 49 | const int_bits = ty.intInfo(mod).bits; | |
| 50 | const int_bits = ty.intInfo(pt.zcu).bits; | |
| 50 | 51 | if (int_bits <= 64) return direct; |
| 51 | 52 | if (int_bits <= 128) return .{ .direct, .direct }; |
| 52 | 53 | return memory; |
| ... | ... | @@ -61,24 +62,24 @@ pub fn classifyType(ty: Type, mod: *Zcu) [2]Class { |
| 61 | 62 | .Vector => return direct, |
| 62 | 63 | .Array => return memory, |
| 63 | 64 | .Optional => { |
| 64 | assert(ty.isPtrLikeOptional(mod)); | |
| 65 | assert(ty.isPtrLikeOptional(pt.zcu)); | |
| 65 | 66 | return direct; |
| 66 | 67 | }, |
| 67 | 68 | .Pointer => { |
| 68 | assert(!ty.isSlice(mod)); | |
| 69 | assert(!ty.isSlice(pt.zcu)); | |
| 69 | 70 | return direct; |
| 70 | 71 | }, |
| 71 | 72 | .Union => { |
| 72 | const union_obj = mod.typeToUnion(ty).?; | |
| 73 | const union_obj = pt.zcu.typeToUnion(ty).?; | |
| 73 | 74 | if (union_obj.getLayout(ip) == .@"packed") { |
| 74 | if (ty.bitSize(mod) <= 64) return direct; | |
| 75 | if (ty.bitSize(pt) <= 64) return direct; | |
| 75 | 76 | return .{ .direct, .direct }; |
| 76 | 77 | } |
| 77 | const layout = ty.unionGetLayout(mod); | |
| 78 | const layout = ty.unionGetLayout(pt); | |
| 78 | 79 | assert(layout.tag_size == 0); |
| 79 | 80 | if (union_obj.field_types.len > 1) return memory; |
| 80 | 81 | const first_field_ty = Type.fromInterned(union_obj.field_types.get(ip)[0]); |
| 81 | return classifyType(first_field_ty, mod); | |
| 82 | return classifyType(first_field_ty, pt); | |
| 82 | 83 | }, |
| 83 | 84 | .ErrorUnion, |
| 84 | 85 | .Frame, |
| ... | ... | @@ -100,28 +101,29 @@ pub fn classifyType(ty: Type, mod: *Zcu) [2]Class { |
| 100 | 101 | /// Returns the scalar type a given type can represent. |
| 101 | 102 | /// Asserts given type can be represented as scalar, such as |
| 102 | 103 | /// a struct with a single scalar field. |
| 103 | pub fn scalarType(ty: Type, mod: *Zcu) Type { | |
| 104 | pub fn scalarType(ty: Type, pt: Zcu.PerThread) Type { | |
| 105 | const mod = pt.zcu; | |
| 104 | 106 | const ip = &mod.intern_pool; |
| 105 | 107 | switch (ty.zigTypeTag(mod)) { |
| 106 | 108 | .Struct => { |
| 107 | 109 | if (mod.typeToPackedStruct(ty)) |packed_struct| { |
| 108 | return scalarType(Type.fromInterned(packed_struct.backingIntType(ip).*), mod); | |
| 110 | return scalarType(Type.fromInterned(packed_struct.backingIntType(ip).*), pt); | |
| 109 | 111 | } else { |
| 110 | 112 | assert(ty.structFieldCount(mod) == 1); |
| 111 | return scalarType(ty.structFieldType(0, mod), mod); | |
| 113 | return scalarType(ty.structFieldType(0, mod), pt); | |
| 112 | 114 | } |
| 113 | 115 | }, |
| 114 | 116 | .Union => { |
| 115 | 117 | const union_obj = mod.typeToUnion(ty).?; |
| 116 | 118 | if (union_obj.getLayout(ip) != .@"packed") { |
| 117 | const layout = mod.getUnionLayout(union_obj); | |
| 119 | const layout = pt.getUnionLayout(union_obj); | |
| 118 | 120 | if (layout.payload_size == 0 and layout.tag_size != 0) { |
| 119 | return scalarType(ty.unionTagTypeSafety(mod).?, mod); | |
| 121 | return scalarType(ty.unionTagTypeSafety(mod).?, pt); | |
| 120 | 122 | } |
| 121 | 123 | assert(union_obj.field_types.len == 1); |
| 122 | 124 | } |
| 123 | 125 | const first_field_ty = Type.fromInterned(union_obj.field_types.get(ip)[0]); |
| 124 | return scalarType(first_field_ty, mod); | |
| 126 | return scalarType(first_field_ty, pt); | |
| 125 | 127 | }, |
| 126 | 128 | else => return ty, |
| 127 | 129 | } |
src/arch/x86_64/CodeGen.zig+655-551| ... | ... | @@ -19,7 +19,7 @@ const CodeGenError = codegen.CodeGenError; |
| 19 | 19 | const Compilation = @import("../../Compilation.zig"); |
| 20 | 20 | const DebugInfoOutput = codegen.DebugInfoOutput; |
| 21 | 21 | const DW = std.dwarf; |
| 22 | const ErrorMsg = Module.ErrorMsg; | |
| 22 | const ErrorMsg = Zcu.ErrorMsg; | |
| 23 | 23 | const Result = codegen.Result; |
| 24 | 24 | const Emit = @import("Emit.zig"); |
| 25 | 25 | const Liveness = @import("../../Liveness.zig"); |
| ... | ... | @@ -27,8 +27,6 @@ const Lower = @import("Lower.zig"); |
| 27 | 27 | const Mir = @import("Mir.zig"); |
| 28 | 28 | const Package = @import("../../Package.zig"); |
| 29 | 29 | const Zcu = @import("../../Zcu.zig"); |
| 30 | /// Deprecated. | |
| 31 | const Module = Zcu; | |
| 32 | 30 | const InternPool = @import("../../InternPool.zig"); |
| 33 | 31 | const Alignment = InternPool.Alignment; |
| 34 | 32 | const Target = std.Target; |
| ... | ... | @@ -52,6 +50,7 @@ const FrameIndex = bits.FrameIndex; |
| 52 | 50 | const InnerError = CodeGenError || error{OutOfRegisters}; |
| 53 | 51 | |
| 54 | 52 | gpa: Allocator, |
| 53 | pt: Zcu.PerThread, | |
| 55 | 54 | air: Air, |
| 56 | 55 | liveness: Liveness, |
| 57 | 56 | bin_file: *link.File, |
| ... | ... | @@ -74,7 +73,7 @@ va_info: union { |
| 74 | 73 | ret_mcv: InstTracking, |
| 75 | 74 | fn_type: Type, |
| 76 | 75 | arg_index: u32, |
| 77 | src_loc: Module.LazySrcLoc, | |
| 76 | src_loc: Zcu.LazySrcLoc, | |
| 78 | 77 | |
| 79 | 78 | eflags_inst: ?Air.Inst.Index = null, |
| 80 | 79 | |
| ... | ... | @@ -120,18 +119,18 @@ const Owner = union(enum) { |
| 120 | 119 | func_index: InternPool.Index, |
| 121 | 120 | lazy_sym: link.File.LazySymbol, |
| 122 | 121 | |
| 123 | fn getDecl(owner: Owner, mod: *Module) InternPool.DeclIndex { | |
| 122 | fn getDecl(owner: Owner, zcu: *Zcu) InternPool.DeclIndex { | |
| 124 | 123 | return switch (owner) { |
| 125 | .func_index => |func_index| mod.funcOwnerDeclIndex(func_index), | |
| 126 | .lazy_sym => |lazy_sym| lazy_sym.ty.getOwnerDecl(mod), | |
| 124 | .func_index => |func_index| zcu.funcOwnerDeclIndex(func_index), | |
| 125 | .lazy_sym => |lazy_sym| lazy_sym.ty.getOwnerDecl(zcu), | |
| 127 | 126 | }; |
| 128 | 127 | } |
| 129 | 128 | |
| 130 | 129 | fn getSymbolIndex(owner: Owner, ctx: *Self) !u32 { |
| 130 | const pt = ctx.pt; | |
| 131 | 131 | switch (owner) { |
| 132 | 132 | .func_index => |func_index| { |
| 133 | const mod = ctx.bin_file.comp.module.?; | |
| 134 | const decl_index = mod.funcOwnerDeclIndex(func_index); | |
| 133 | const decl_index = ctx.pt.zcu.funcOwnerDeclIndex(func_index); | |
| 135 | 134 | if (ctx.bin_file.cast(link.File.Elf)) |elf_file| { |
| 136 | 135 | return elf_file.zigObjectPtr().?.getOrCreateMetadataForDecl(elf_file, decl_index); |
| 137 | 136 | } else if (ctx.bin_file.cast(link.File.MachO)) |macho_file| { |
| ... | ... | @@ -145,17 +144,17 @@ const Owner = union(enum) { |
| 145 | 144 | }, |
| 146 | 145 | .lazy_sym => |lazy_sym| { |
| 147 | 146 | if (ctx.bin_file.cast(link.File.Elf)) |elf_file| { |
| 148 | return elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, lazy_sym) catch |err| | |
| 147 | return elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, pt, lazy_sym) catch |err| | |
| 149 | 148 | ctx.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 150 | 149 | } else if (ctx.bin_file.cast(link.File.MachO)) |macho_file| { |
| 151 | return macho_file.getZigObject().?.getOrCreateMetadataForLazySymbol(macho_file, lazy_sym) catch |err| | |
| 150 | return macho_file.getZigObject().?.getOrCreateMetadataForLazySymbol(macho_file, pt, lazy_sym) catch |err| | |
| 152 | 151 | ctx.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 153 | 152 | } else if (ctx.bin_file.cast(link.File.Coff)) |coff_file| { |
| 154 | const atom = coff_file.getOrCreateAtomForLazySymbol(lazy_sym) catch |err| | |
| 153 | const atom = coff_file.getOrCreateAtomForLazySymbol(pt, lazy_sym) catch |err| | |
| 155 | 154 | return ctx.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 156 | 155 | return coff_file.getAtom(atom).getSymbolIndex().?; |
| 157 | 156 | } else if (ctx.bin_file.cast(link.File.Plan9)) |p9_file| { |
| 158 | return p9_file.getOrCreateAtomForLazySymbol(lazy_sym) catch |err| | |
| 157 | return p9_file.getOrCreateAtomForLazySymbol(pt, lazy_sym) catch |err| | |
| 159 | 158 | return ctx.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 160 | 159 | } else unreachable; |
| 161 | 160 | }, |
| ... | ... | @@ -753,14 +752,14 @@ const FrameAlloc = struct { |
| 753 | 752 | .ref_count = 0, |
| 754 | 753 | }; |
| 755 | 754 | } |
| 756 | fn initType(ty: Type, mod: *Module) FrameAlloc { | |
| 755 | fn initType(ty: Type, pt: Zcu.PerThread) FrameAlloc { | |
| 757 | 756 | return init(.{ |
| 758 | .size = ty.abiSize(mod), | |
| 759 | .alignment = ty.abiAlignment(mod), | |
| 757 | .size = ty.abiSize(pt), | |
| 758 | .alignment = ty.abiAlignment(pt), | |
| 760 | 759 | }); |
| 761 | 760 | } |
| 762 | fn initSpill(ty: Type, mod: *Module) FrameAlloc { | |
| 763 | const abi_size = ty.abiSize(mod); | |
| 761 | fn initSpill(ty: Type, pt: Zcu.PerThread) FrameAlloc { | |
| 762 | const abi_size = ty.abiSize(pt); | |
| 764 | 763 | const spill_size = if (abi_size < 8) |
| 765 | 764 | math.ceilPowerOfTwoAssert(u64, abi_size) |
| 766 | 765 | else |
| ... | ... | @@ -768,7 +767,7 @@ const FrameAlloc = struct { |
| 768 | 767 | return init(.{ |
| 769 | 768 | .size = spill_size, |
| 770 | 769 | .pad = @intCast(spill_size - abi_size), |
| 771 | .alignment = ty.abiAlignment(mod).maxStrict( | |
| 770 | .alignment = ty.abiAlignment(pt).maxStrict( | |
| 772 | 771 | Alignment.fromNonzeroByteUnits(@min(spill_size, 8)), |
| 773 | 772 | ), |
| 774 | 773 | }); |
| ... | ... | @@ -777,7 +776,7 @@ const FrameAlloc = struct { |
| 777 | 776 | |
| 778 | 777 | const StackAllocation = struct { |
| 779 | 778 | inst: ?Air.Inst.Index, |
| 780 | /// TODO do we need size? should be determined by inst.ty.abiSize(mod) | |
| 779 | /// TODO do we need size? should be determined by inst.ty.abiSize(pt) | |
| 781 | 780 | size: u32, |
| 782 | 781 | }; |
| 783 | 782 | |
| ... | ... | @@ -795,16 +794,17 @@ const Self = @This(); |
| 795 | 794 | |
| 796 | 795 | pub fn generate( |
| 797 | 796 | bin_file: *link.File, |
| 798 | src_loc: Module.LazySrcLoc, | |
| 797 | pt: Zcu.PerThread, | |
| 798 | src_loc: Zcu.LazySrcLoc, | |
| 799 | 799 | func_index: InternPool.Index, |
| 800 | 800 | air: Air, |
| 801 | 801 | liveness: Liveness, |
| 802 | 802 | code: *std.ArrayList(u8), |
| 803 | 803 | debug_output: DebugInfoOutput, |
| 804 | 804 | ) CodeGenError!Result { |
| 805 | const comp = bin_file.comp; | |
| 806 | const gpa = comp.gpa; | |
| 807 | const zcu = comp.module.?; | |
| 805 | const zcu = pt.zcu; | |
| 806 | const gpa = zcu.gpa; | |
| 807 | const comp = zcu.comp; | |
| 808 | 808 | const func = zcu.funcInfo(func_index); |
| 809 | 809 | const fn_owner_decl = zcu.declPtr(func.owner_decl); |
| 810 | 810 | assert(fn_owner_decl.has_tv); |
| ... | ... | @@ -812,8 +812,9 @@ pub fn generate( |
| 812 | 812 | const namespace = zcu.namespacePtr(fn_owner_decl.src_namespace); |
| 813 | 813 | const mod = namespace.fileScope(zcu).mod; |
| 814 | 814 | |
| 815 | var function = Self{ | |
| 815 | var function: Self = .{ | |
| 816 | 816 | .gpa = gpa, |
| 817 | .pt = pt, | |
| 817 | 818 | .air = air, |
| 818 | 819 | .liveness = liveness, |
| 819 | 820 | .target = &mod.resolved_target.result, |
| ... | ... | @@ -882,11 +883,11 @@ pub fn generate( |
| 882 | 883 | function.args = call_info.args; |
| 883 | 884 | function.ret_mcv = call_info.return_value; |
| 884 | 885 | function.frame_allocs.set(@intFromEnum(FrameIndex.ret_addr), FrameAlloc.init(.{ |
| 885 | .size = Type.usize.abiSize(zcu), | |
| 886 | .alignment = Type.usize.abiAlignment(zcu).min(call_info.stack_align), | |
| 886 | .size = Type.usize.abiSize(pt), | |
| 887 | .alignment = Type.usize.abiAlignment(pt).min(call_info.stack_align), | |
| 887 | 888 | })); |
| 888 | 889 | function.frame_allocs.set(@intFromEnum(FrameIndex.base_ptr), FrameAlloc.init(.{ |
| 889 | .size = Type.usize.abiSize(zcu), | |
| 890 | .size = Type.usize.abiSize(pt), | |
| 890 | 891 | .alignment = Alignment.min( |
| 891 | 892 | call_info.stack_align, |
| 892 | 893 | Alignment.fromNonzeroByteUnits(function.target.stackAlignment()), |
| ... | ... | @@ -971,7 +972,8 @@ pub fn generate( |
| 971 | 972 | |
| 972 | 973 | pub fn generateLazy( |
| 973 | 974 | bin_file: *link.File, |
| 974 | src_loc: Module.LazySrcLoc, | |
| 975 | pt: Zcu.PerThread, | |
| 976 | src_loc: Zcu.LazySrcLoc, | |
| 975 | 977 | lazy_sym: link.File.LazySymbol, |
| 976 | 978 | code: *std.ArrayList(u8), |
| 977 | 979 | debug_output: DebugInfoOutput, |
| ... | ... | @@ -980,8 +982,9 @@ pub fn generateLazy( |
| 980 | 982 | const gpa = comp.gpa; |
| 981 | 983 | // This function is for generating global code, so we use the root module. |
| 982 | 984 | const mod = comp.root_mod; |
| 983 | var function = Self{ | |
| 985 | var function: Self = .{ | |
| 984 | 986 | .gpa = gpa, |
| 987 | .pt = pt, | |
| 985 | 988 | .air = undefined, |
| 986 | 989 | .liveness = undefined, |
| 987 | 990 | .target = &mod.resolved_target.result, |
| ... | ... | @@ -1065,7 +1068,7 @@ pub fn generateLazy( |
| 1065 | 1068 | } |
| 1066 | 1069 | |
| 1067 | 1070 | const FormatDeclData = struct { |
| 1068 | mod: *Module, | |
| 1071 | zcu: *Zcu, | |
| 1069 | 1072 | decl_index: InternPool.DeclIndex, |
| 1070 | 1073 | }; |
| 1071 | 1074 | fn formatDecl( |
| ... | ... | @@ -1074,11 +1077,11 @@ fn formatDecl( |
| 1074 | 1077 | _: std.fmt.FormatOptions, |
| 1075 | 1078 | writer: anytype, |
| 1076 | 1079 | ) @TypeOf(writer).Error!void { |
| 1077 | try data.mod.declPtr(data.decl_index).renderFullyQualifiedName(data.mod, writer); | |
| 1080 | try data.zcu.declPtr(data.decl_index).renderFullyQualifiedName(data.zcu, writer); | |
| 1078 | 1081 | } |
| 1079 | 1082 | fn fmtDecl(self: *Self, decl_index: InternPool.DeclIndex) std.fmt.Formatter(formatDecl) { |
| 1080 | 1083 | return .{ .data = .{ |
| 1081 | .mod = self.bin_file.comp.module.?, | |
| 1084 | .zcu = self.pt.zcu, | |
| 1082 | 1085 | .decl_index = decl_index, |
| 1083 | 1086 | } }; |
| 1084 | 1087 | } |
| ... | ... | @@ -1095,7 +1098,7 @@ fn formatAir( |
| 1095 | 1098 | ) @TypeOf(writer).Error!void { |
| 1096 | 1099 | @import("../../print_air.zig").dumpInst( |
| 1097 | 1100 | data.inst, |
| 1098 | data.self.bin_file.comp.module.?, | |
| 1101 | data.self.pt, | |
| 1099 | 1102 | data.self.air, |
| 1100 | 1103 | data.self.liveness, |
| 1101 | 1104 | ); |
| ... | ... | @@ -1746,7 +1749,8 @@ fn asmMemoryRegisterImmediate( |
| 1746 | 1749 | } |
| 1747 | 1750 | |
| 1748 | 1751 | fn gen(self: *Self) InnerError!void { |
| 1749 | const mod = self.bin_file.comp.module.?; | |
| 1752 | const pt = self.pt; | |
| 1753 | const mod = pt.zcu; | |
| 1750 | 1754 | const fn_info = mod.typeToFunc(self.fn_type).?; |
| 1751 | 1755 | const cc = abi.resolveCallingConvention(fn_info.cc, self.target.*); |
| 1752 | 1756 | if (cc != .Naked) { |
| ... | ... | @@ -1764,7 +1768,7 @@ fn gen(self: *Self) InnerError!void { |
| 1764 | 1768 | // The address where to store the return value for the caller is in a |
| 1765 | 1769 | // register which the callee is free to clobber. Therefore, we purposely |
| 1766 | 1770 | // spill it to stack immediately. |
| 1767 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(Type.usize, mod)); | |
| 1771 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(Type.usize, pt)); | |
| 1768 | 1772 | try self.genSetMem( |
| 1769 | 1773 | .{ .frame = frame_index }, |
| 1770 | 1774 | 0, |
| ... | ... | @@ -1800,7 +1804,7 @@ fn gen(self: *Self) InnerError!void { |
| 1800 | 1804 | try self.asmRegisterImmediate(.{ ._, .cmp }, .al, Immediate.u(info.fp_count)); |
| 1801 | 1805 | const skip_sse_reloc = try self.asmJccReloc(.na, undefined); |
| 1802 | 1806 | |
| 1803 | const vec_2_f64 = try mod.vectorType(.{ .len = 2, .child = .f64_type }); | |
| 1807 | const vec_2_f64 = try pt.vectorType(.{ .len = 2, .child = .f64_type }); | |
| 1804 | 1808 | for (abi.SysV.c_abi_sse_param_regs[info.fp_count..], info.fp_count..) |reg, reg_i| |
| 1805 | 1809 | try self.genSetMem( |
| 1806 | 1810 | .{ .frame = reg_save_area_fi }, |
| ... | ... | @@ -1951,7 +1955,8 @@ fn gen(self: *Self) InnerError!void { |
| 1951 | 1955 | } |
| 1952 | 1956 | |
| 1953 | 1957 | fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 1954 | const mod = self.bin_file.comp.module.?; | |
| 1958 | const pt = self.pt; | |
| 1959 | const mod = pt.zcu; | |
| 1955 | 1960 | const ip = &mod.intern_pool; |
| 1956 | 1961 | const air_tags = self.air.instructions.items(.tag); |
| 1957 | 1962 | |
| ... | ... | @@ -2222,12 +2227,13 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 2222 | 2227 | } |
| 2223 | 2228 | |
| 2224 | 2229 | fn genLazy(self: *Self, lazy_sym: link.File.LazySymbol) InnerError!void { |
| 2225 | const mod = self.bin_file.comp.module.?; | |
| 2230 | const pt = self.pt; | |
| 2231 | const mod = pt.zcu; | |
| 2226 | 2232 | const ip = &mod.intern_pool; |
| 2227 | 2233 | switch (lazy_sym.ty.zigTypeTag(mod)) { |
| 2228 | 2234 | .Enum => { |
| 2229 | 2235 | const enum_ty = lazy_sym.ty; |
| 2230 | wip_mir_log.debug("{}.@tagName:", .{enum_ty.fmt(mod)}); | |
| 2236 | wip_mir_log.debug("{}.@tagName:", .{enum_ty.fmt(pt)}); | |
| 2231 | 2237 | |
| 2232 | 2238 | const resolved_cc = abi.resolveCallingConvention(.Unspecified, self.target.*); |
| 2233 | 2239 | const param_regs = abi.getCAbiIntParamRegs(resolved_cc); |
| ... | ... | @@ -2249,7 +2255,7 @@ fn genLazy(self: *Self, lazy_sym: link.File.LazySymbol) InnerError!void { |
| 2249 | 2255 | const tag_names = enum_ty.enumFields(mod); |
| 2250 | 2256 | for (exitlude_jump_relocs, 0..) |*exitlude_jump_reloc, tag_index| { |
| 2251 | 2257 | const tag_name_len = tag_names.get(ip)[tag_index].length(ip); |
| 2252 | const tag_val = try mod.enumValueFieldIndex(enum_ty, @intCast(tag_index)); | |
| 2258 | const tag_val = try pt.enumValueFieldIndex(enum_ty, @intCast(tag_index)); | |
| 2253 | 2259 | const tag_mcv = try self.genTypedValue(tag_val); |
| 2254 | 2260 | try self.genBinOpMir(.{ ._, .cmp }, enum_ty, enum_mcv, tag_mcv); |
| 2255 | 2261 | const skip_reloc = try self.asmJccReloc(.ne, undefined); |
| ... | ... | @@ -2282,7 +2288,7 @@ fn genLazy(self: *Self, lazy_sym: link.File.LazySymbol) InnerError!void { |
| 2282 | 2288 | }, |
| 2283 | 2289 | else => return self.fail( |
| 2284 | 2290 | "TODO implement {s} for {}", |
| 2285 | .{ @tagName(lazy_sym.kind), lazy_sym.ty.fmt(mod) }, | |
| 2291 | .{ @tagName(lazy_sym.kind), lazy_sym.ty.fmt(pt) }, | |
| 2286 | 2292 | ), |
| 2287 | 2293 | } |
| 2288 | 2294 | } |
| ... | ... | @@ -2481,14 +2487,15 @@ fn allocFrameIndex(self: *Self, alloc: FrameAlloc) !FrameIndex { |
| 2481 | 2487 | |
| 2482 | 2488 | /// Use a pointer instruction as the basis for allocating stack memory. |
| 2483 | 2489 | fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !FrameIndex { |
| 2484 | const mod = self.bin_file.comp.module.?; | |
| 2490 | const pt = self.pt; | |
| 2491 | const mod = pt.zcu; | |
| 2485 | 2492 | const ptr_ty = self.typeOfIndex(inst); |
| 2486 | 2493 | const val_ty = ptr_ty.childType(mod); |
| 2487 | 2494 | return self.allocFrameIndex(FrameAlloc.init(.{ |
| 2488 | .size = math.cast(u32, val_ty.abiSize(mod)) orelse { | |
| 2489 | return self.fail("type '{}' too big to fit into stack frame", .{val_ty.fmt(mod)}); | |
| 2495 | .size = math.cast(u32, val_ty.abiSize(pt)) orelse { | |
| 2496 | return self.fail("type '{}' too big to fit into stack frame", .{val_ty.fmt(pt)}); | |
| 2490 | 2497 | }, |
| 2491 | .alignment = ptr_ty.ptrAlignment(mod).max(.@"1"), | |
| 2498 | .alignment = ptr_ty.ptrAlignment(pt).max(.@"1"), | |
| 2492 | 2499 | })); |
| 2493 | 2500 | } |
| 2494 | 2501 | |
| ... | ... | @@ -2501,9 +2508,10 @@ fn allocTempRegOrMem(self: *Self, elem_ty: Type, reg_ok: bool) !MCValue { |
| 2501 | 2508 | } |
| 2502 | 2509 | |
| 2503 | 2510 | fn allocRegOrMemAdvanced(self: *Self, ty: Type, inst: ?Air.Inst.Index, reg_ok: bool) !MCValue { |
| 2504 | const mod = self.bin_file.comp.module.?; | |
| 2505 | const abi_size = math.cast(u32, ty.abiSize(mod)) orelse { | |
| 2506 | return self.fail("type '{}' too big to fit into stack frame", .{ty.fmt(mod)}); | |
| 2511 | const pt = self.pt; | |
| 2512 | const mod = pt.zcu; | |
| 2513 | const abi_size = math.cast(u32, ty.abiSize(pt)) orelse { | |
| 2514 | return self.fail("type '{}' too big to fit into stack frame", .{ty.fmt(pt)}); | |
| 2507 | 2515 | }; |
| 2508 | 2516 | |
| 2509 | 2517 | if (reg_ok) need_mem: { |
| ... | ... | @@ -2529,12 +2537,13 @@ fn allocRegOrMemAdvanced(self: *Self, ty: Type, inst: ?Air.Inst.Index, reg_ok: b |
| 2529 | 2537 | } |
| 2530 | 2538 | } |
| 2531 | 2539 | |
| 2532 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(ty, mod)); | |
| 2540 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(ty, pt)); | |
| 2533 | 2541 | return .{ .load_frame = .{ .index = frame_index } }; |
| 2534 | 2542 | } |
| 2535 | 2543 | |
| 2536 | 2544 | fn regClassForType(self: *Self, ty: Type) RegisterManager.RegisterBitSet { |
| 2537 | const mod = self.bin_file.comp.module.?; | |
| 2545 | const pt = self.pt; | |
| 2546 | const mod = pt.zcu; | |
| 2538 | 2547 | return switch (ty.zigTypeTag(mod)) { |
| 2539 | 2548 | .Float => switch (ty.floatBits(self.target.*)) { |
| 2540 | 2549 | 80 => abi.RegisterClass.x87, |
| ... | ... | @@ -2849,7 +2858,8 @@ fn airFptrunc(self: *Self, inst: Air.Inst.Index) !void { |
| 2849 | 2858 | } |
| 2850 | 2859 | |
| 2851 | 2860 | fn airFpext(self: *Self, inst: Air.Inst.Index) !void { |
| 2852 | const mod = self.bin_file.comp.module.?; | |
| 2861 | const pt = self.pt; | |
| 2862 | const mod = pt.zcu; | |
| 2853 | 2863 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2854 | 2864 | const dst_ty = self.typeOfIndex(inst); |
| 2855 | 2865 | const dst_scalar_ty = dst_ty.scalarType(mod); |
| ... | ... | @@ -2892,14 +2902,14 @@ fn airFpext(self: *Self, inst: Air.Inst.Index) !void { |
| 2892 | 2902 | } }, &.{src_scalar_ty}, &.{.{ .air_ref = ty_op.operand }}); |
| 2893 | 2903 | } |
| 2894 | 2904 | |
| 2895 | const src_abi_size: u32 = @intCast(src_ty.abiSize(mod)); | |
| 2905 | const src_abi_size: u32 = @intCast(src_ty.abiSize(pt)); | |
| 2896 | 2906 | const src_mcv = try self.resolveInst(ty_op.operand); |
| 2897 | 2907 | const dst_mcv = if (src_mcv.isRegister() and self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) |
| 2898 | 2908 | src_mcv |
| 2899 | 2909 | else |
| 2900 | 2910 | try self.copyToRegisterWithInstTracking(inst, dst_ty, src_mcv); |
| 2901 | 2911 | const dst_reg = dst_mcv.getReg().?; |
| 2902 | const dst_alias = registerAlias(dst_reg, @intCast(@max(dst_ty.abiSize(mod), 16))); | |
| 2912 | const dst_alias = registerAlias(dst_reg, @intCast(@max(dst_ty.abiSize(pt), 16))); | |
| 2903 | 2913 | const dst_lock = self.register_manager.lockReg(dst_reg); |
| 2904 | 2914 | defer if (dst_lock) |lock| self.register_manager.unlockReg(lock); |
| 2905 | 2915 | |
| ... | ... | @@ -2978,19 +2988,20 @@ fn airFpext(self: *Self, inst: Air.Inst.Index) !void { |
| 2978 | 2988 | } |
| 2979 | 2989 | break :result dst_mcv; |
| 2980 | 2990 | } orelse return self.fail("TODO implement airFpext from {} to {}", .{ |
| 2981 | src_ty.fmt(mod), dst_ty.fmt(mod), | |
| 2991 | src_ty.fmt(pt), dst_ty.fmt(pt), | |
| 2982 | 2992 | }); |
| 2983 | 2993 | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 2984 | 2994 | } |
| 2985 | 2995 | |
| 2986 | 2996 | fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 2987 | const mod = self.bin_file.comp.module.?; | |
| 2997 | const pt = self.pt; | |
| 2998 | const mod = pt.zcu; | |
| 2988 | 2999 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 2989 | 3000 | const src_ty = self.typeOf(ty_op.operand); |
| 2990 | 3001 | const dst_ty = self.typeOfIndex(inst); |
| 2991 | 3002 | |
| 2992 | 3003 | const result = @as(?MCValue, result: { |
| 2993 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(mod)); | |
| 3004 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(pt)); | |
| 2994 | 3005 | |
| 2995 | 3006 | const src_int_info = src_ty.intInfo(mod); |
| 2996 | 3007 | const dst_int_info = dst_ty.intInfo(mod); |
| ... | ... | @@ -3001,13 +3012,13 @@ fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 3001 | 3012 | |
| 3002 | 3013 | const src_mcv = try self.resolveInst(ty_op.operand); |
| 3003 | 3014 | if (dst_ty.isVector(mod)) { |
| 3004 | const src_abi_size: u32 = @intCast(src_ty.abiSize(mod)); | |
| 3015 | const src_abi_size: u32 = @intCast(src_ty.abiSize(pt)); | |
| 3005 | 3016 | const max_abi_size = @max(dst_abi_size, src_abi_size); |
| 3006 | 3017 | if (max_abi_size > @as(u32, if (self.hasFeature(.avx2)) 32 else 16)) break :result null; |
| 3007 | 3018 | const has_avx = self.hasFeature(.avx); |
| 3008 | 3019 | |
| 3009 | const dst_elem_abi_size = dst_ty.childType(mod).abiSize(mod); | |
| 3010 | const src_elem_abi_size = src_ty.childType(mod).abiSize(mod); | |
| 3020 | const dst_elem_abi_size = dst_ty.childType(mod).abiSize(pt); | |
| 3021 | const src_elem_abi_size = src_ty.childType(mod).abiSize(pt); | |
| 3011 | 3022 | switch (math.order(dst_elem_abi_size, src_elem_abi_size)) { |
| 3012 | 3023 | .lt => { |
| 3013 | 3024 | const mir_tag: Mir.Inst.FixedTag = switch (dst_elem_abi_size) { |
| ... | ... | @@ -3236,19 +3247,20 @@ fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 3236 | 3247 | |
| 3237 | 3248 | break :result dst_mcv; |
| 3238 | 3249 | }) orelse return self.fail("TODO implement airIntCast from {} to {}", .{ |
| 3239 | src_ty.fmt(mod), dst_ty.fmt(mod), | |
| 3250 | src_ty.fmt(pt), dst_ty.fmt(pt), | |
| 3240 | 3251 | }); |
| 3241 | 3252 | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 3242 | 3253 | } |
| 3243 | 3254 | |
| 3244 | 3255 | fn airTrunc(self: *Self, inst: Air.Inst.Index) !void { |
| 3245 | const mod = self.bin_file.comp.module.?; | |
| 3256 | const pt = self.pt; | |
| 3257 | const mod = pt.zcu; | |
| 3246 | 3258 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3247 | 3259 | |
| 3248 | 3260 | const dst_ty = self.typeOfIndex(inst); |
| 3249 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(mod)); | |
| 3261 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(pt)); | |
| 3250 | 3262 | const src_ty = self.typeOf(ty_op.operand); |
| 3251 | const src_abi_size: u32 = @intCast(src_ty.abiSize(mod)); | |
| 3263 | const src_abi_size: u32 = @intCast(src_ty.abiSize(pt)); | |
| 3252 | 3264 | |
| 3253 | 3265 | const result = result: { |
| 3254 | 3266 | const src_mcv = try self.resolveInst(ty_op.operand); |
| ... | ... | @@ -3278,9 +3290,9 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) !void { |
| 3278 | 3290 | if (dst_ty.zigTypeTag(mod) == .Vector) { |
| 3279 | 3291 | assert(src_ty.zigTypeTag(mod) == .Vector and dst_ty.vectorLen(mod) == src_ty.vectorLen(mod)); |
| 3280 | 3292 | const dst_elem_ty = dst_ty.childType(mod); |
| 3281 | const dst_elem_abi_size: u32 = @intCast(dst_elem_ty.abiSize(mod)); | |
| 3293 | const dst_elem_abi_size: u32 = @intCast(dst_elem_ty.abiSize(pt)); | |
| 3282 | 3294 | const src_elem_ty = src_ty.childType(mod); |
| 3283 | const src_elem_abi_size: u32 = @intCast(src_elem_ty.abiSize(mod)); | |
| 3295 | const src_elem_abi_size: u32 = @intCast(src_elem_ty.abiSize(pt)); | |
| 3284 | 3296 | |
| 3285 | 3297 | const mir_tag = @as(?Mir.Inst.FixedTag, switch (dst_elem_abi_size) { |
| 3286 | 3298 | 1 => switch (src_elem_abi_size) { |
| ... | ... | @@ -3305,20 +3317,20 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) !void { |
| 3305 | 3317 | else => null, |
| 3306 | 3318 | }, |
| 3307 | 3319 | else => null, |
| 3308 | }) orelse return self.fail("TODO implement airTrunc for {}", .{dst_ty.fmt(mod)}); | |
| 3320 | }) orelse return self.fail("TODO implement airTrunc for {}", .{dst_ty.fmt(pt)}); | |
| 3309 | 3321 | |
| 3310 | 3322 | const dst_info = dst_elem_ty.intInfo(mod); |
| 3311 | 3323 | const src_info = src_elem_ty.intInfo(mod); |
| 3312 | 3324 | |
| 3313 | const mask_val = try mod.intValue(src_elem_ty, @as(u64, math.maxInt(u64)) >> @intCast(64 - dst_info.bits)); | |
| 3325 | const mask_val = try pt.intValue(src_elem_ty, @as(u64, math.maxInt(u64)) >> @intCast(64 - dst_info.bits)); | |
| 3314 | 3326 | |
| 3315 | const splat_ty = try mod.vectorType(.{ | |
| 3327 | const splat_ty = try pt.vectorType(.{ | |
| 3316 | 3328 | .len = @intCast(@divExact(@as(u64, if (src_abi_size > 16) 256 else 128), src_info.bits)), |
| 3317 | 3329 | .child = src_elem_ty.ip_index, |
| 3318 | 3330 | }); |
| 3319 | const splat_abi_size: u32 = @intCast(splat_ty.abiSize(mod)); | |
| 3331 | const splat_abi_size: u32 = @intCast(splat_ty.abiSize(pt)); | |
| 3320 | 3332 | |
| 3321 | const splat_val = try mod.intern(.{ .aggregate = .{ | |
| 3333 | const splat_val = try pt.intern(.{ .aggregate = .{ | |
| 3322 | 3334 | .ty = splat_ty.ip_index, |
| 3323 | 3335 | .storage = .{ .repeated_elem = mask_val.ip_index }, |
| 3324 | 3336 | } }); |
| ... | ... | @@ -3375,7 +3387,7 @@ fn airTrunc(self: *Self, inst: Air.Inst.Index) !void { |
| 3375 | 3387 | } |
| 3376 | 3388 | } else if (dst_abi_size <= 16) { |
| 3377 | 3389 | const dst_info = dst_ty.intInfo(mod); |
| 3378 | const high_ty = try mod.intType(dst_info.signedness, dst_info.bits - 64); | |
| 3390 | const high_ty = try pt.intType(dst_info.signedness, dst_info.bits - 64); | |
| 3379 | 3391 | if (self.regExtraBits(high_ty) > 0) { |
| 3380 | 3392 | try self.truncateRegister(high_ty, dst_mcv.register_pair[1].to64()); |
| 3381 | 3393 | } |
| ... | ... | @@ -3400,12 +3412,12 @@ fn airIntFromBool(self: *Self, inst: Air.Inst.Index) !void { |
| 3400 | 3412 | } |
| 3401 | 3413 | |
| 3402 | 3414 | fn airSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 3403 | const mod = self.bin_file.comp.module.?; | |
| 3415 | const pt = self.pt; | |
| 3404 | 3416 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3405 | 3417 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3406 | 3418 | |
| 3407 | 3419 | const slice_ty = self.typeOfIndex(inst); |
| 3408 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(slice_ty, mod)); | |
| 3420 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(slice_ty, pt)); | |
| 3409 | 3421 | |
| 3410 | 3422 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 3411 | 3423 | try self.genSetMem(.{ .frame = frame_index }, 0, ptr_ty, .{ .air_ref = bin_op.lhs }, .{}); |
| ... | ... | @@ -3413,7 +3425,7 @@ fn airSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 3413 | 3425 | const len_ty = self.typeOf(bin_op.rhs); |
| 3414 | 3426 | try self.genSetMem( |
| 3415 | 3427 | .{ .frame = frame_index }, |
| 3416 | @intCast(ptr_ty.abiSize(mod)), | |
| 3428 | @intCast(ptr_ty.abiSize(pt)), | |
| 3417 | 3429 | len_ty, |
| 3418 | 3430 | .{ .air_ref = bin_op.rhs }, |
| 3419 | 3431 | .{}, |
| ... | ... | @@ -3430,14 +3442,15 @@ fn airUnOp(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 3430 | 3442 | } |
| 3431 | 3443 | |
| 3432 | 3444 | fn airBinOp(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 3433 | const mod = self.bin_file.comp.module.?; | |
| 3445 | const pt = self.pt; | |
| 3446 | const mod = pt.zcu; | |
| 3434 | 3447 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3435 | 3448 | const dst_mcv = try self.genBinOp(inst, tag, bin_op.lhs, bin_op.rhs); |
| 3436 | 3449 | |
| 3437 | 3450 | const dst_ty = self.typeOfIndex(inst); |
| 3438 | 3451 | if (dst_ty.isAbiInt(mod)) { |
| 3439 | const abi_size: u32 = @intCast(dst_ty.abiSize(mod)); | |
| 3440 | const bit_size: u32 = @intCast(dst_ty.bitSize(mod)); | |
| 3452 | const abi_size: u32 = @intCast(dst_ty.abiSize(pt)); | |
| 3453 | const bit_size: u32 = @intCast(dst_ty.bitSize(pt)); | |
| 3441 | 3454 | if (abi_size * 8 > bit_size) { |
| 3442 | 3455 | const dst_lock = switch (dst_mcv) { |
| 3443 | 3456 | .register => |dst_reg| self.register_manager.lockRegAssumeUnused(dst_reg), |
| ... | ... | @@ -3452,7 +3465,7 @@ fn airBinOp(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void { |
| 3452 | 3465 | const tmp_lock = self.register_manager.lockRegAssumeUnused(tmp_reg); |
| 3453 | 3466 | defer self.register_manager.unlockReg(tmp_lock); |
| 3454 | 3467 | |
| 3455 | const hi_ty = try mod.intType(.unsigned, @intCast((dst_ty.bitSize(mod) - 1) % 64 + 1)); | |
| 3468 | const hi_ty = try pt.intType(.unsigned, @intCast((dst_ty.bitSize(pt) - 1) % 64 + 1)); | |
| 3456 | 3469 | const hi_mcv = dst_mcv.address().offset(@intCast(bit_size / 64 * 8)).deref(); |
| 3457 | 3470 | try self.genSetReg(tmp_reg, hi_ty, hi_mcv, .{}); |
| 3458 | 3471 | try self.truncateRegister(dst_ty, tmp_reg); |
| ... | ... | @@ -3471,7 +3484,8 @@ fn airPtrArithmetic(self: *Self, inst: Air.Inst.Index, tag: Air.Inst.Tag) !void |
| 3471 | 3484 | } |
| 3472 | 3485 | |
| 3473 | 3486 | fn activeIntBits(self: *Self, dst_air: Air.Inst.Ref) u16 { |
| 3474 | const mod = self.bin_file.comp.module.?; | |
| 3487 | const pt = self.pt; | |
| 3488 | const mod = pt.zcu; | |
| 3475 | 3489 | const air_tag = self.air.instructions.items(.tag); |
| 3476 | 3490 | const air_data = self.air.instructions.items(.data); |
| 3477 | 3491 | |
| ... | ... | @@ -3497,7 +3511,7 @@ fn activeIntBits(self: *Self, dst_air: Air.Inst.Ref) u16 { |
| 3497 | 3511 | } |
| 3498 | 3512 | } else if (dst_air.toInterned()) |ip_index| { |
| 3499 | 3513 | var space: Value.BigIntSpace = undefined; |
| 3500 | const src_int = Value.fromInterned(ip_index).toBigInt(&space, mod); | |
| 3514 | const src_int = Value.fromInterned(ip_index).toBigInt(&space, pt); | |
| 3501 | 3515 | return @as(u16, @intCast(src_int.bitCountTwosComp())) + |
| 3502 | 3516 | @intFromBool(src_int.positive and dst_info.signedness == .signed); |
| 3503 | 3517 | } |
| ... | ... | @@ -3505,7 +3519,8 @@ fn activeIntBits(self: *Self, dst_air: Air.Inst.Ref) u16 { |
| 3505 | 3519 | } |
| 3506 | 3520 | |
| 3507 | 3521 | fn airMulDivBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 3508 | const mod = self.bin_file.comp.module.?; | |
| 3522 | const pt = self.pt; | |
| 3523 | const mod = pt.zcu; | |
| 3509 | 3524 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3510 | 3525 | const result = result: { |
| 3511 | 3526 | const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| ... | ... | @@ -3514,10 +3529,10 @@ fn airMulDivBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 3514 | 3529 | .Float, .Vector => break :result try self.genBinOp(inst, tag, bin_op.lhs, bin_op.rhs), |
| 3515 | 3530 | else => {}, |
| 3516 | 3531 | } |
| 3517 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(mod)); | |
| 3532 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(pt)); | |
| 3518 | 3533 | |
| 3519 | 3534 | const dst_info = dst_ty.intInfo(mod); |
| 3520 | const src_ty = try mod.intType(dst_info.signedness, switch (tag) { | |
| 3535 | const src_ty = try pt.intType(dst_info.signedness, switch (tag) { | |
| 3521 | 3536 | else => unreachable, |
| 3522 | 3537 | .mul, .mul_wrap => @max( |
| 3523 | 3538 | self.activeIntBits(bin_op.lhs), |
| ... | ... | @@ -3526,7 +3541,7 @@ fn airMulDivBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 3526 | 3541 | ), |
| 3527 | 3542 | .div_trunc, .div_floor, .div_exact, .rem, .mod => dst_info.bits, |
| 3528 | 3543 | }); |
| 3529 | const src_abi_size: u32 = @intCast(src_ty.abiSize(mod)); | |
| 3544 | const src_abi_size: u32 = @intCast(src_ty.abiSize(pt)); | |
| 3530 | 3545 | |
| 3531 | 3546 | if (dst_abi_size == 16 and src_abi_size == 16) switch (tag) { |
| 3532 | 3547 | else => unreachable, |
| ... | ... | @@ -3539,7 +3554,7 @@ fn airMulDivBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 3539 | 3554 | state: State, |
| 3540 | 3555 | reloc: Mir.Inst.Index, |
| 3541 | 3556 | } = if (signed and tag == .div_floor) state: { |
| 3542 | const frame_index = try self.allocFrameIndex(FrameAlloc.initType(Type.usize, mod)); | |
| 3557 | const frame_index = try self.allocFrameIndex(FrameAlloc.initType(Type.usize, pt)); | |
| 3543 | 3558 | try self.asmMemoryImmediate( |
| 3544 | 3559 | .{ ._, .mov }, |
| 3545 | 3560 | .{ .base = .{ .frame = frame_index }, .mod = .{ .rm = .{ .size = .qword } } }, |
| ... | ... | @@ -3614,7 +3629,7 @@ fn airMulDivBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 3614 | 3629 | .rem, .mod => "mod", |
| 3615 | 3630 | else => unreachable, |
| 3616 | 3631 | }, |
| 3617 | intCompilerRtAbiName(@intCast(dst_ty.bitSize(mod))), | |
| 3632 | intCompilerRtAbiName(@intCast(dst_ty.bitSize(pt))), | |
| 3618 | 3633 | }) catch unreachable, |
| 3619 | 3634 | } }, |
| 3620 | 3635 | &.{ src_ty, src_ty }, |
| ... | ... | @@ -3643,7 +3658,7 @@ fn airMulDivBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 3643 | 3658 | .return_type = dst_ty.toIntern(), |
| 3644 | 3659 | .param_types = &.{ src_ty.toIntern(), src_ty.toIntern() }, |
| 3645 | 3660 | .callee = std.fmt.bufPrint(&callee_buf, "__div{c}i3", .{ |
| 3646 | intCompilerRtAbiName(@intCast(dst_ty.bitSize(mod))), | |
| 3661 | intCompilerRtAbiName(@intCast(dst_ty.bitSize(pt))), | |
| 3647 | 3662 | }) catch unreachable, |
| 3648 | 3663 | } }, |
| 3649 | 3664 | &.{ src_ty, src_ty }, |
| ... | ... | @@ -3734,12 +3749,13 @@ fn airMulDivBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 3734 | 3749 | } |
| 3735 | 3750 | |
| 3736 | 3751 | fn airAddSat(self: *Self, inst: Air.Inst.Index) !void { |
| 3737 | const mod = self.bin_file.comp.module.?; | |
| 3752 | const pt = self.pt; | |
| 3753 | const mod = pt.zcu; | |
| 3738 | 3754 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3739 | 3755 | const ty = self.typeOf(bin_op.lhs); |
| 3740 | if (ty.zigTypeTag(mod) == .Vector or ty.abiSize(mod) > 8) return self.fail( | |
| 3756 | if (ty.zigTypeTag(mod) == .Vector or ty.abiSize(pt) > 8) return self.fail( | |
| 3741 | 3757 | "TODO implement airAddSat for {}", |
| 3742 | .{ty.fmt(mod)}, | |
| 3758 | .{ty.fmt(pt)}, | |
| 3743 | 3759 | ); |
| 3744 | 3760 | |
| 3745 | 3761 | const lhs_mcv = try self.resolveInst(bin_op.lhs); |
| ... | ... | @@ -3804,7 +3820,7 @@ fn airAddSat(self: *Self, inst: Air.Inst.Index) !void { |
| 3804 | 3820 | break :cc .o; |
| 3805 | 3821 | } else cc: { |
| 3806 | 3822 | try self.genSetReg(limit_reg, ty, .{ |
| 3807 | .immediate = @as(u64, math.maxInt(u64)) >> @intCast(64 - ty.bitSize(mod)), | |
| 3823 | .immediate = @as(u64, math.maxInt(u64)) >> @intCast(64 - ty.bitSize(pt)), | |
| 3808 | 3824 | }, .{}); |
| 3809 | 3825 | |
| 3810 | 3826 | try self.genBinOpMir(.{ ._, .add }, ty, dst_mcv, rhs_mcv); |
| ... | ... | @@ -3815,7 +3831,7 @@ fn airAddSat(self: *Self, inst: Air.Inst.Index) !void { |
| 3815 | 3831 | break :cc .c; |
| 3816 | 3832 | }; |
| 3817 | 3833 | |
| 3818 | const cmov_abi_size = @max(@as(u32, @intCast(ty.abiSize(mod))), 2); | |
| 3834 | const cmov_abi_size = @max(@as(u32, @intCast(ty.abiSize(pt))), 2); | |
| 3819 | 3835 | try self.asmCmovccRegisterRegister( |
| 3820 | 3836 | cc, |
| 3821 | 3837 | registerAlias(dst_reg, cmov_abi_size), |
| ... | ... | @@ -3834,12 +3850,13 @@ fn airAddSat(self: *Self, inst: Air.Inst.Index) !void { |
| 3834 | 3850 | } |
| 3835 | 3851 | |
| 3836 | 3852 | fn airSubSat(self: *Self, inst: Air.Inst.Index) !void { |
| 3837 | const mod = self.bin_file.comp.module.?; | |
| 3853 | const pt = self.pt; | |
| 3854 | const mod = pt.zcu; | |
| 3838 | 3855 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3839 | 3856 | const ty = self.typeOf(bin_op.lhs); |
| 3840 | if (ty.zigTypeTag(mod) == .Vector or ty.abiSize(mod) > 8) return self.fail( | |
| 3857 | if (ty.zigTypeTag(mod) == .Vector or ty.abiSize(pt) > 8) return self.fail( | |
| 3841 | 3858 | "TODO implement airSubSat for {}", |
| 3842 | .{ty.fmt(mod)}, | |
| 3859 | .{ty.fmt(pt)}, | |
| 3843 | 3860 | ); |
| 3844 | 3861 | |
| 3845 | 3862 | const lhs_mcv = try self.resolveInst(bin_op.lhs); |
| ... | ... | @@ -3908,7 +3925,7 @@ fn airSubSat(self: *Self, inst: Air.Inst.Index) !void { |
| 3908 | 3925 | break :cc .c; |
| 3909 | 3926 | }; |
| 3910 | 3927 | |
| 3911 | const cmov_abi_size = @max(@as(u32, @intCast(ty.abiSize(mod))), 2); | |
| 3928 | const cmov_abi_size = @max(@as(u32, @intCast(ty.abiSize(pt))), 2); | |
| 3912 | 3929 | try self.asmCmovccRegisterRegister( |
| 3913 | 3930 | cc, |
| 3914 | 3931 | registerAlias(dst_reg, cmov_abi_size), |
| ... | ... | @@ -3927,13 +3944,14 @@ fn airSubSat(self: *Self, inst: Air.Inst.Index) !void { |
| 3927 | 3944 | } |
| 3928 | 3945 | |
| 3929 | 3946 | fn airMulSat(self: *Self, inst: Air.Inst.Index) !void { |
| 3930 | const mod = self.bin_file.comp.module.?; | |
| 3947 | const pt = self.pt; | |
| 3948 | const mod = pt.zcu; | |
| 3931 | 3949 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3932 | 3950 | const ty = self.typeOf(bin_op.lhs); |
| 3933 | 3951 | |
| 3934 | 3952 | const result = result: { |
| 3935 | 3953 | if (ty.toIntern() == .i128_type) { |
| 3936 | const ptr_c_int = try mod.singleMutPtrType(Type.c_int); | |
| 3954 | const ptr_c_int = try pt.singleMutPtrType(Type.c_int); | |
| 3937 | 3955 | const overflow = try self.allocTempRegOrMem(Type.c_int, false); |
| 3938 | 3956 | |
| 3939 | 3957 | const dst_mcv = try self.genCall(.{ .lib = .{ |
| ... | ... | @@ -4010,9 +4028,9 @@ fn airMulSat(self: *Self, inst: Air.Inst.Index) !void { |
| 4010 | 4028 | break :result dst_mcv; |
| 4011 | 4029 | } |
| 4012 | 4030 | |
| 4013 | if (ty.zigTypeTag(mod) == .Vector or ty.abiSize(mod) > 8) return self.fail( | |
| 4031 | if (ty.zigTypeTag(mod) == .Vector or ty.abiSize(pt) > 8) return self.fail( | |
| 4014 | 4032 | "TODO implement airMulSat for {}", |
| 4015 | .{ty.fmt(mod)}, | |
| 4033 | .{ty.fmt(pt)}, | |
| 4016 | 4034 | ); |
| 4017 | 4035 | |
| 4018 | 4036 | try self.spillRegisters(&.{ .rax, .rcx, .rdx }); |
| ... | ... | @@ -4061,7 +4079,7 @@ fn airMulSat(self: *Self, inst: Air.Inst.Index) !void { |
| 4061 | 4079 | }; |
| 4062 | 4080 | |
| 4063 | 4081 | const dst_mcv = try self.genMulDivBinOp(.mul, inst, ty, ty, lhs_mcv, rhs_mcv); |
| 4064 | const cmov_abi_size = @max(@as(u32, @intCast(ty.abiSize(mod))), 2); | |
| 4082 | const cmov_abi_size = @max(@as(u32, @intCast(ty.abiSize(pt))), 2); | |
| 4065 | 4083 | try self.asmCmovccRegisterRegister( |
| 4066 | 4084 | cc, |
| 4067 | 4085 | registerAlias(dst_mcv.register, cmov_abi_size), |
| ... | ... | @@ -4073,7 +4091,8 @@ fn airMulSat(self: *Self, inst: Air.Inst.Index) !void { |
| 4073 | 4091 | } |
| 4074 | 4092 | |
| 4075 | 4093 | fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4076 | const mod = self.bin_file.comp.module.?; | |
| 4094 | const pt = self.pt; | |
| 4095 | const mod = pt.zcu; | |
| 4077 | 4096 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4078 | 4097 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4079 | 4098 | const result: MCValue = result: { |
| ... | ... | @@ -4109,17 +4128,17 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4109 | 4128 | } |
| 4110 | 4129 | |
| 4111 | 4130 | const frame_index = |
| 4112 | try self.allocFrameIndex(FrameAlloc.initSpill(tuple_ty, mod)); | |
| 4131 | try self.allocFrameIndex(FrameAlloc.initSpill(tuple_ty, pt)); | |
| 4113 | 4132 | try self.genSetMem( |
| 4114 | 4133 | .{ .frame = frame_index }, |
| 4115 | @intCast(tuple_ty.structFieldOffset(1, mod)), | |
| 4134 | @intCast(tuple_ty.structFieldOffset(1, pt)), | |
| 4116 | 4135 | Type.u1, |
| 4117 | 4136 | .{ .eflags = cc }, |
| 4118 | 4137 | .{}, |
| 4119 | 4138 | ); |
| 4120 | 4139 | try self.genSetMem( |
| 4121 | 4140 | .{ .frame = frame_index }, |
| 4122 | @intCast(tuple_ty.structFieldOffset(0, mod)), | |
| 4141 | @intCast(tuple_ty.structFieldOffset(0, pt)), | |
| 4123 | 4142 | ty, |
| 4124 | 4143 | partial_mcv, |
| 4125 | 4144 | .{}, |
| ... | ... | @@ -4128,7 +4147,7 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4128 | 4147 | } |
| 4129 | 4148 | |
| 4130 | 4149 | const frame_index = |
| 4131 | try self.allocFrameIndex(FrameAlloc.initSpill(tuple_ty, mod)); | |
| 4150 | try self.allocFrameIndex(FrameAlloc.initSpill(tuple_ty, pt)); | |
| 4132 | 4151 | try self.genSetFrameTruncatedOverflowCompare(tuple_ty, frame_index, partial_mcv, cc); |
| 4133 | 4152 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| 4134 | 4153 | }, |
| ... | ... | @@ -4139,7 +4158,8 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4139 | 4158 | } |
| 4140 | 4159 | |
| 4141 | 4160 | fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4142 | const mod = self.bin_file.comp.module.?; | |
| 4161 | const pt = self.pt; | |
| 4162 | const mod = pt.zcu; | |
| 4143 | 4163 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4144 | 4164 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4145 | 4165 | const result: MCValue = result: { |
| ... | ... | @@ -4186,17 +4206,17 @@ fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4186 | 4206 | } |
| 4187 | 4207 | |
| 4188 | 4208 | const frame_index = |
| 4189 | try self.allocFrameIndex(FrameAlloc.initSpill(tuple_ty, mod)); | |
| 4209 | try self.allocFrameIndex(FrameAlloc.initSpill(tuple_ty, pt)); | |
| 4190 | 4210 | try self.genSetMem( |
| 4191 | 4211 | .{ .frame = frame_index }, |
| 4192 | @intCast(tuple_ty.structFieldOffset(1, mod)), | |
| 4212 | @intCast(tuple_ty.structFieldOffset(1, pt)), | |
| 4193 | 4213 | tuple_ty.structFieldType(1, mod), |
| 4194 | 4214 | .{ .eflags = cc }, |
| 4195 | 4215 | .{}, |
| 4196 | 4216 | ); |
| 4197 | 4217 | try self.genSetMem( |
| 4198 | 4218 | .{ .frame = frame_index }, |
| 4199 | @intCast(tuple_ty.structFieldOffset(0, mod)), | |
| 4219 | @intCast(tuple_ty.structFieldOffset(0, pt)), | |
| 4200 | 4220 | tuple_ty.structFieldType(0, mod), |
| 4201 | 4221 | partial_mcv, |
| 4202 | 4222 | .{}, |
| ... | ... | @@ -4205,7 +4225,7 @@ fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4205 | 4225 | } |
| 4206 | 4226 | |
| 4207 | 4227 | const frame_index = |
| 4208 | try self.allocFrameIndex(FrameAlloc.initSpill(tuple_ty, mod)); | |
| 4228 | try self.allocFrameIndex(FrameAlloc.initSpill(tuple_ty, pt)); | |
| 4209 | 4229 | try self.genSetFrameTruncatedOverflowCompare(tuple_ty, frame_index, partial_mcv, cc); |
| 4210 | 4230 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| 4211 | 4231 | }, |
| ... | ... | @@ -4222,7 +4242,8 @@ fn genSetFrameTruncatedOverflowCompare( |
| 4222 | 4242 | src_mcv: MCValue, |
| 4223 | 4243 | overflow_cc: ?Condition, |
| 4224 | 4244 | ) !void { |
| 4225 | const mod = self.bin_file.comp.module.?; | |
| 4245 | const pt = self.pt; | |
| 4246 | const mod = pt.zcu; | |
| 4226 | 4247 | const src_lock = switch (src_mcv) { |
| 4227 | 4248 | .register => |reg| self.register_manager.lockReg(reg), |
| 4228 | 4249 | else => null, |
| ... | ... | @@ -4233,12 +4254,12 @@ fn genSetFrameTruncatedOverflowCompare( |
| 4233 | 4254 | const int_info = ty.intInfo(mod); |
| 4234 | 4255 | |
| 4235 | 4256 | const hi_bits = (int_info.bits - 1) % 64 + 1; |
| 4236 | const hi_ty = try mod.intType(int_info.signedness, hi_bits); | |
| 4257 | const hi_ty = try pt.intType(int_info.signedness, hi_bits); | |
| 4237 | 4258 | |
| 4238 | 4259 | const limb_bits: u16 = @intCast(if (int_info.bits <= 64) self.regBitSize(ty) else 64); |
| 4239 | const limb_ty = try mod.intType(int_info.signedness, limb_bits); | |
| 4260 | const limb_ty = try pt.intType(int_info.signedness, limb_bits); | |
| 4240 | 4261 | |
| 4241 | const rest_ty = try mod.intType(.unsigned, int_info.bits - hi_bits); | |
| 4262 | const rest_ty = try pt.intType(.unsigned, int_info.bits - hi_bits); | |
| 4242 | 4263 | |
| 4243 | 4264 | const temp_regs = |
| 4244 | 4265 | try self.register_manager.allocRegs(3, .{null} ** 3, abi.RegisterClass.gp); |
| ... | ... | @@ -4269,7 +4290,7 @@ fn genSetFrameTruncatedOverflowCompare( |
| 4269 | 4290 | ); |
| 4270 | 4291 | } |
| 4271 | 4292 | |
| 4272 | const payload_off: i32 = @intCast(tuple_ty.structFieldOffset(0, mod)); | |
| 4293 | const payload_off: i32 = @intCast(tuple_ty.structFieldOffset(0, pt)); | |
| 4273 | 4294 | if (hi_limb_off > 0) try self.genSetMem( |
| 4274 | 4295 | .{ .frame = frame_index }, |
| 4275 | 4296 | payload_off, |
| ... | ... | @@ -4286,7 +4307,7 @@ fn genSetFrameTruncatedOverflowCompare( |
| 4286 | 4307 | ); |
| 4287 | 4308 | try self.genSetMem( |
| 4288 | 4309 | .{ .frame = frame_index }, |
| 4289 | @intCast(tuple_ty.structFieldOffset(1, mod)), | |
| 4310 | @intCast(tuple_ty.structFieldOffset(1, pt)), | |
| 4290 | 4311 | tuple_ty.structFieldType(1, mod), |
| 4291 | 4312 | if (overflow_cc) |_| .{ .register = overflow_reg.to8() } else .{ .eflags = .ne }, |
| 4292 | 4313 | .{}, |
| ... | ... | @@ -4294,18 +4315,19 @@ fn genSetFrameTruncatedOverflowCompare( |
| 4294 | 4315 | } |
| 4295 | 4316 | |
| 4296 | 4317 | fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4297 | const mod = self.bin_file.comp.module.?; | |
| 4318 | const pt = self.pt; | |
| 4319 | const mod = pt.zcu; | |
| 4298 | 4320 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4299 | 4321 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4300 | 4322 | const tuple_ty = self.typeOfIndex(inst); |
| 4301 | 4323 | const dst_ty = self.typeOf(bin_op.lhs); |
| 4302 | 4324 | const result: MCValue = switch (dst_ty.zigTypeTag(mod)) { |
| 4303 | .Vector => return self.fail("TODO implement airMulWithOverflow for {}", .{dst_ty.fmt(mod)}), | |
| 4325 | .Vector => return self.fail("TODO implement airMulWithOverflow for {}", .{dst_ty.fmt(pt)}), | |
| 4304 | 4326 | .Int => result: { |
| 4305 | 4327 | const dst_info = dst_ty.intInfo(mod); |
| 4306 | 4328 | if (dst_info.bits > 128 and dst_info.signedness == .unsigned) { |
| 4307 | 4329 | const slow_inc = self.hasFeature(.slow_incdec); |
| 4308 | const abi_size: u32 = @intCast(dst_ty.abiSize(mod)); | |
| 4330 | const abi_size: u32 = @intCast(dst_ty.abiSize(pt)); | |
| 4309 | 4331 | const limb_len = math.divCeil(u32, abi_size, 8) catch unreachable; |
| 4310 | 4332 | |
| 4311 | 4333 | try self.spillRegisters(&.{ .rax, .rcx, .rdx }); |
| ... | ... | @@ -4316,7 +4338,7 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4316 | 4338 | try self.genInlineMemset( |
| 4317 | 4339 | dst_mcv.address(), |
| 4318 | 4340 | .{ .immediate = 0 }, |
| 4319 | .{ .immediate = tuple_ty.abiSize(mod) }, | |
| 4341 | .{ .immediate = tuple_ty.abiSize(pt) }, | |
| 4320 | 4342 | .{}, |
| 4321 | 4343 | ); |
| 4322 | 4344 | const lhs_mcv = try self.resolveInst(bin_op.lhs); |
| ... | ... | @@ -4356,7 +4378,7 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4356 | 4378 | .index = temp_regs[3].to64(), |
| 4357 | 4379 | .scale = .@"8", |
| 4358 | 4380 | .disp = dst_mcv.load_frame.off + |
| 4359 | @as(i32, @intCast(tuple_ty.structFieldOffset(0, mod))), | |
| 4381 | @as(i32, @intCast(tuple_ty.structFieldOffset(0, pt))), | |
| 4360 | 4382 | } }, |
| 4361 | 4383 | }, .rdx); |
| 4362 | 4384 | try self.asmSetccRegister(.c, .cl); |
| ... | ... | @@ -4380,7 +4402,7 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4380 | 4402 | .index = temp_regs[3].to64(), |
| 4381 | 4403 | .scale = .@"8", |
| 4382 | 4404 | .disp = dst_mcv.load_frame.off + |
| 4383 | @as(i32, @intCast(tuple_ty.structFieldOffset(0, mod))), | |
| 4405 | @as(i32, @intCast(tuple_ty.structFieldOffset(0, pt))), | |
| 4384 | 4406 | } }, |
| 4385 | 4407 | }, .rax); |
| 4386 | 4408 | try self.asmSetccRegister(.c, .ch); |
| ... | ... | @@ -4429,7 +4451,7 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4429 | 4451 | .mod = .{ .rm = .{ |
| 4430 | 4452 | .size = .byte, |
| 4431 | 4453 | .disp = dst_mcv.load_frame.off + |
| 4432 | @as(i32, @intCast(tuple_ty.structFieldOffset(1, mod))), | |
| 4454 | @as(i32, @intCast(tuple_ty.structFieldOffset(1, pt))), | |
| 4433 | 4455 | } }, |
| 4434 | 4456 | }, Immediate.u(1)); |
| 4435 | 4457 | self.performReloc(no_overflow); |
| ... | ... | @@ -4453,11 +4475,11 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4453 | 4475 | const lhs_active_bits = self.activeIntBits(bin_op.lhs); |
| 4454 | 4476 | const rhs_active_bits = self.activeIntBits(bin_op.rhs); |
| 4455 | 4477 | const src_bits = @max(lhs_active_bits, rhs_active_bits, dst_info.bits / 2); |
| 4456 | const src_ty = try mod.intType(dst_info.signedness, src_bits); | |
| 4478 | const src_ty = try pt.intType(dst_info.signedness, src_bits); | |
| 4457 | 4479 | if (src_bits > 64 and src_bits <= 128 and |
| 4458 | 4480 | dst_info.bits > 64 and dst_info.bits <= 128) switch (dst_info.signedness) { |
| 4459 | 4481 | .signed => { |
| 4460 | const ptr_c_int = try mod.singleMutPtrType(Type.c_int); | |
| 4482 | const ptr_c_int = try pt.singleMutPtrType(Type.c_int); | |
| 4461 | 4483 | const overflow = try self.allocTempRegOrMem(Type.c_int, false); |
| 4462 | 4484 | const result = try self.genCall(.{ .lib = .{ |
| 4463 | 4485 | .return_type = .i128_type, |
| ... | ... | @@ -4472,7 +4494,7 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4472 | 4494 | const dst_mcv = try self.allocRegOrMem(inst, false); |
| 4473 | 4495 | try self.genSetMem( |
| 4474 | 4496 | .{ .frame = dst_mcv.load_frame.index }, |
| 4475 | @intCast(tuple_ty.structFieldOffset(0, mod)), | |
| 4497 | @intCast(tuple_ty.structFieldOffset(0, pt)), | |
| 4476 | 4498 | tuple_ty.structFieldType(0, mod), |
| 4477 | 4499 | result, |
| 4478 | 4500 | .{}, |
| ... | ... | @@ -4484,7 +4506,7 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4484 | 4506 | ); |
| 4485 | 4507 | try self.genSetMem( |
| 4486 | 4508 | .{ .frame = dst_mcv.load_frame.index }, |
| 4487 | @intCast(tuple_ty.structFieldOffset(1, mod)), | |
| 4509 | @intCast(tuple_ty.structFieldOffset(1, pt)), | |
| 4488 | 4510 | tuple_ty.structFieldType(1, mod), |
| 4489 | 4511 | .{ .eflags = .ne }, |
| 4490 | 4512 | .{}, |
| ... | ... | @@ -4596,14 +4618,14 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4596 | 4618 | const dst_mcv = try self.allocRegOrMem(inst, false); |
| 4597 | 4619 | try self.genSetMem( |
| 4598 | 4620 | .{ .frame = dst_mcv.load_frame.index }, |
| 4599 | @intCast(tuple_ty.structFieldOffset(0, mod)), | |
| 4621 | @intCast(tuple_ty.structFieldOffset(0, pt)), | |
| 4600 | 4622 | tuple_ty.structFieldType(0, mod), |
| 4601 | 4623 | .{ .register_pair = .{ .rax, .rdx } }, |
| 4602 | 4624 | .{}, |
| 4603 | 4625 | ); |
| 4604 | 4626 | try self.genSetMem( |
| 4605 | 4627 | .{ .frame = dst_mcv.load_frame.index }, |
| 4606 | @intCast(tuple_ty.structFieldOffset(1, mod)), | |
| 4628 | @intCast(tuple_ty.structFieldOffset(1, pt)), | |
| 4607 | 4629 | tuple_ty.structFieldType(1, mod), |
| 4608 | 4630 | .{ .register = tmp_regs[1] }, |
| 4609 | 4631 | .{}, |
| ... | ... | @@ -4636,7 +4658,7 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4636 | 4658 | self.eflags_inst = inst; |
| 4637 | 4659 | break :result .{ .register_overflow = .{ .reg = reg, .eflags = cc } }; |
| 4638 | 4660 | } else { |
| 4639 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(tuple_ty, mod)); | |
| 4661 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(tuple_ty, pt)); | |
| 4640 | 4662 | try self.genSetFrameTruncatedOverflowCompare(tuple_ty, frame_index, partial_mcv, cc); |
| 4641 | 4663 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| 4642 | 4664 | }, |
| ... | ... | @@ -4644,21 +4666,21 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4644 | 4666 | // For now, this is the only supported multiply that doesn't fit in a register. |
| 4645 | 4667 | if (dst_info.bits > 128 or src_bits != 64) |
| 4646 | 4668 | return self.fail("TODO implement airWithOverflow from {} to {}", .{ |
| 4647 | src_ty.fmt(mod), dst_ty.fmt(mod), | |
| 4669 | src_ty.fmt(pt), dst_ty.fmt(pt), | |
| 4648 | 4670 | }); |
| 4649 | 4671 | |
| 4650 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(tuple_ty, mod)); | |
| 4672 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(tuple_ty, pt)); | |
| 4651 | 4673 | if (dst_info.bits >= lhs_active_bits + rhs_active_bits) { |
| 4652 | 4674 | try self.genSetMem( |
| 4653 | 4675 | .{ .frame = frame_index }, |
| 4654 | @intCast(tuple_ty.structFieldOffset(0, mod)), | |
| 4676 | @intCast(tuple_ty.structFieldOffset(0, pt)), | |
| 4655 | 4677 | tuple_ty.structFieldType(0, mod), |
| 4656 | 4678 | partial_mcv, |
| 4657 | 4679 | .{}, |
| 4658 | 4680 | ); |
| 4659 | 4681 | try self.genSetMem( |
| 4660 | 4682 | .{ .frame = frame_index }, |
| 4661 | @intCast(tuple_ty.structFieldOffset(1, mod)), | |
| 4683 | @intCast(tuple_ty.structFieldOffset(1, pt)), | |
| 4662 | 4684 | tuple_ty.structFieldType(1, mod), |
| 4663 | 4685 | .{ .immediate = 0 }, // cc being set is impossible |
| 4664 | 4686 | .{}, |
| ... | ... | @@ -4682,8 +4704,8 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) !void { |
| 4682 | 4704 | /// Clobbers .rax and .rdx registers. |
| 4683 | 4705 | /// Quotient is saved in .rax and remainder in .rdx. |
| 4684 | 4706 | fn genIntMulDivOpMir(self: *Self, tag: Mir.Inst.FixedTag, ty: Type, lhs: MCValue, rhs: MCValue) !void { |
| 4685 | const mod = self.bin_file.comp.module.?; | |
| 4686 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 4707 | const pt = self.pt; | |
| 4708 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 4687 | 4709 | const bit_size: u32 = @intCast(self.regBitSize(ty)); |
| 4688 | 4710 | if (abi_size > 8) { |
| 4689 | 4711 | return self.fail("TODO implement genIntMulDivOpMir for ABI size larger than 8", .{}); |
| ... | ... | @@ -4732,8 +4754,9 @@ fn genIntMulDivOpMir(self: *Self, tag: Mir.Inst.FixedTag, ty: Type, lhs: MCValue |
| 4732 | 4754 | /// Always returns a register. |
| 4733 | 4755 | /// Clobbers .rax and .rdx registers. |
| 4734 | 4756 | fn genInlineIntDivFloor(self: *Self, ty: Type, lhs: MCValue, rhs: MCValue) !MCValue { |
| 4735 | const mod = self.bin_file.comp.module.?; | |
| 4736 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 4757 | const pt = self.pt; | |
| 4758 | const mod = pt.zcu; | |
| 4759 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 4737 | 4760 | const int_info = ty.intInfo(mod); |
| 4738 | 4761 | const dividend = switch (lhs) { |
| 4739 | 4762 | .register => |reg| reg, |
| ... | ... | @@ -4784,7 +4807,8 @@ fn genInlineIntDivFloor(self: *Self, ty: Type, lhs: MCValue, rhs: MCValue) !MCVa |
| 4784 | 4807 | } |
| 4785 | 4808 | |
| 4786 | 4809 | fn airShlShrBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 4787 | const mod = self.bin_file.comp.module.?; | |
| 4810 | const pt = self.pt; | |
| 4811 | const mod = pt.zcu; | |
| 4788 | 4812 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4789 | 4813 | |
| 4790 | 4814 | const air_tags = self.air.instructions.items(.tag); |
| ... | ... | @@ -4811,7 +4835,7 @@ fn airShlShrBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 4811 | 4835 | const tmp_lock = self.register_manager.lockRegAssumeUnused(tmp_reg); |
| 4812 | 4836 | defer self.register_manager.unlockReg(tmp_lock); |
| 4813 | 4837 | |
| 4814 | const lhs_bits: u31 = @intCast(lhs_ty.bitSize(mod)); | |
| 4838 | const lhs_bits: u31 = @intCast(lhs_ty.bitSize(pt)); | |
| 4815 | 4839 | const tmp_ty = if (lhs_bits > 64) Type.usize else lhs_ty; |
| 4816 | 4840 | const off = frame_addr.off + (lhs_bits - 1) / 64 * 8; |
| 4817 | 4841 | try self.genSetReg( |
| ... | ... | @@ -4922,11 +4946,11 @@ fn airShlShrBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 4922 | 4946 | .shl, .shl_exact => if (self.hasFeature(.avx2)) .{ .vp_q, .sll } else null, |
| 4923 | 4947 | }, |
| 4924 | 4948 | }, |
| 4925 | })) |mir_tag| if (try self.air.value(bin_op.rhs, mod)) |rhs_val| { | |
| 4949 | })) |mir_tag| if (try self.air.value(bin_op.rhs, pt)) |rhs_val| { | |
| 4926 | 4950 | switch (mod.intern_pool.indexToKey(rhs_val.toIntern())) { |
| 4927 | 4951 | .aggregate => |rhs_aggregate| switch (rhs_aggregate.storage) { |
| 4928 | 4952 | .repeated_elem => |rhs_elem| { |
| 4929 | const abi_size: u32 = @intCast(lhs_ty.abiSize(mod)); | |
| 4953 | const abi_size: u32 = @intCast(lhs_ty.abiSize(pt)); | |
| 4930 | 4954 | |
| 4931 | 4955 | const lhs_mcv = try self.resolveInst(bin_op.lhs); |
| 4932 | 4956 | const dst_reg, const lhs_reg = if (lhs_mcv.isRegister() and |
| ... | ... | @@ -4946,7 +4970,7 @@ fn airShlShrBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 4946 | 4970 | self.register_manager.unlockReg(lock); |
| 4947 | 4971 | |
| 4948 | 4972 | const shift_imm = |
| 4949 | Immediate.u(@intCast(Value.fromInterned(rhs_elem).toUnsignedInt(mod))); | |
| 4973 | Immediate.u(@intCast(Value.fromInterned(rhs_elem).toUnsignedInt(pt))); | |
| 4950 | 4974 | if (self.hasFeature(.avx)) try self.asmRegisterRegisterImmediate( |
| 4951 | 4975 | mir_tag, |
| 4952 | 4976 | registerAlias(dst_reg, abi_size), |
| ... | ... | @@ -4968,7 +4992,7 @@ fn airShlShrBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 4968 | 4992 | } |
| 4969 | 4993 | } else if (bin_op.rhs.toIndex()) |rhs_inst| switch (air_tags[@intFromEnum(rhs_inst)]) { |
| 4970 | 4994 | .splat => { |
| 4971 | const abi_size: u32 = @intCast(lhs_ty.abiSize(mod)); | |
| 4995 | const abi_size: u32 = @intCast(lhs_ty.abiSize(pt)); | |
| 4972 | 4996 | |
| 4973 | 4997 | const lhs_mcv = try self.resolveInst(bin_op.lhs); |
| 4974 | 4998 | const dst_reg, const lhs_reg = if (lhs_mcv.isRegister() and |
| ... | ... | @@ -4991,13 +5015,13 @@ fn airShlShrBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 4991 | 5015 | const shift_lock = self.register_manager.lockRegAssumeUnused(shift_reg); |
| 4992 | 5016 | defer self.register_manager.unlockReg(shift_lock); |
| 4993 | 5017 | |
| 4994 | const mask_ty = try mod.vectorType(.{ .len = 16, .child = .u8_type }); | |
| 4995 | const mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{ | |
| 5018 | const mask_ty = try pt.vectorType(.{ .len = 16, .child = .u8_type }); | |
| 5019 | const mask_mcv = try self.genTypedValue(Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 4996 | 5020 | .ty = mask_ty.toIntern(), |
| 4997 | 5021 | .storage = .{ .elems = &([1]InternPool.Index{ |
| 4998 | (try rhs_ty.childType(mod).maxIntScalar(mod, Type.u8)).toIntern(), | |
| 5022 | (try rhs_ty.childType(mod).maxIntScalar(pt, Type.u8)).toIntern(), | |
| 4999 | 5023 | } ++ [1]InternPool.Index{ |
| 5000 | (try mod.intValue(Type.u8, 0)).toIntern(), | |
| 5024 | (try pt.intValue(Type.u8, 0)).toIntern(), | |
| 5001 | 5025 | } ** 15) }, |
| 5002 | 5026 | } }))); |
| 5003 | 5027 | const mask_addr_reg = |
| ... | ... | @@ -5045,7 +5069,7 @@ fn airShlShrBinOp(self: *Self, inst: Air.Inst.Index) !void { |
| 5045 | 5069 | }, |
| 5046 | 5070 | else => {}, |
| 5047 | 5071 | } |
| 5048 | return self.fail("TODO implement airShlShrBinOp for {}", .{lhs_ty.fmt(mod)}); | |
| 5072 | return self.fail("TODO implement airShlShrBinOp for {}", .{lhs_ty.fmt(pt)}); | |
| 5049 | 5073 | }; |
| 5050 | 5074 | return self.finishAir(inst, result, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 5051 | 5075 | } |
| ... | ... | @@ -5058,11 +5082,11 @@ fn airShlSat(self: *Self, inst: Air.Inst.Index) !void { |
| 5058 | 5082 | } |
| 5059 | 5083 | |
| 5060 | 5084 | fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 5061 | const mod = self.bin_file.comp.module.?; | |
| 5085 | const pt = self.pt; | |
| 5062 | 5086 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5063 | 5087 | const result: MCValue = result: { |
| 5064 | 5088 | const pl_ty = self.typeOfIndex(inst); |
| 5065 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result .none; | |
| 5089 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result .none; | |
| 5066 | 5090 | |
| 5067 | 5091 | const opt_mcv = try self.resolveInst(ty_op.operand); |
| 5068 | 5092 | if (self.reuseOperand(inst, ty_op.operand, 0, opt_mcv)) { |
| ... | ... | @@ -5104,7 +5128,8 @@ fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 5104 | 5128 | } |
| 5105 | 5129 | |
| 5106 | 5130 | fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void { |
| 5107 | const mod = self.bin_file.comp.module.?; | |
| 5131 | const pt = self.pt; | |
| 5132 | const mod = pt.zcu; | |
| 5108 | 5133 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5109 | 5134 | const result = result: { |
| 5110 | 5135 | const dst_ty = self.typeOfIndex(inst); |
| ... | ... | @@ -5130,7 +5155,7 @@ fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void { |
| 5130 | 5155 | try self.copyToRegisterWithInstTracking(inst, dst_ty, src_mcv); |
| 5131 | 5156 | |
| 5132 | 5157 | const pl_ty = dst_ty.childType(mod); |
| 5133 | const pl_abi_size: i32 = @intCast(pl_ty.abiSize(mod)); | |
| 5158 | const pl_abi_size: i32 = @intCast(pl_ty.abiSize(pt)); | |
| 5134 | 5159 | try self.genSetMem( |
| 5135 | 5160 | .{ .reg = dst_mcv.getReg().? }, |
| 5136 | 5161 | pl_abi_size, |
| ... | ... | @@ -5144,7 +5169,8 @@ fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void { |
| 5144 | 5169 | } |
| 5145 | 5170 | |
| 5146 | 5171 | fn airUnwrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void { |
| 5147 | const mod = self.bin_file.comp.module.?; | |
| 5172 | const pt = self.pt; | |
| 5173 | const mod = pt.zcu; | |
| 5148 | 5174 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5149 | 5175 | const err_union_ty = self.typeOf(ty_op.operand); |
| 5150 | 5176 | const err_ty = err_union_ty.errorUnionSet(mod); |
| ... | ... | @@ -5156,11 +5182,11 @@ fn airUnwrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void { |
| 5156 | 5182 | break :result MCValue{ .immediate = 0 }; |
| 5157 | 5183 | } |
| 5158 | 5184 | |
| 5159 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 5185 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 5160 | 5186 | break :result operand; |
| 5161 | 5187 | } |
| 5162 | 5188 | |
| 5163 | const err_off = errUnionErrorOffset(payload_ty, mod); | |
| 5189 | const err_off = errUnionErrorOffset(payload_ty, pt); | |
| 5164 | 5190 | switch (operand) { |
| 5165 | 5191 | .register => |reg| { |
| 5166 | 5192 | // TODO reuse operand |
| ... | ... | @@ -5197,7 +5223,8 @@ fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 5197 | 5223 | |
| 5198 | 5224 | // *(E!T) -> E |
| 5199 | 5225 | fn airUnwrapErrUnionErrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 5200 | const mod = self.bin_file.comp.module.?; | |
| 5226 | const pt = self.pt; | |
| 5227 | const mod = pt.zcu; | |
| 5201 | 5228 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5202 | 5229 | |
| 5203 | 5230 | const src_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -5217,8 +5244,8 @@ fn airUnwrapErrUnionErrPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 5217 | 5244 | const eu_ty = src_ty.childType(mod); |
| 5218 | 5245 | const pl_ty = eu_ty.errorUnionPayload(mod); |
| 5219 | 5246 | const err_ty = eu_ty.errorUnionSet(mod); |
| 5220 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, mod)); | |
| 5221 | const err_abi_size: u32 = @intCast(err_ty.abiSize(mod)); | |
| 5247 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, pt)); | |
| 5248 | const err_abi_size: u32 = @intCast(err_ty.abiSize(pt)); | |
| 5222 | 5249 | try self.asmRegisterMemory( |
| 5223 | 5250 | .{ ._, .mov }, |
| 5224 | 5251 | registerAlias(dst_reg, err_abi_size), |
| ... | ... | @@ -5244,7 +5271,8 @@ fn airUnwrapErrUnionPayloadPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 5244 | 5271 | } |
| 5245 | 5272 | |
| 5246 | 5273 | fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void { |
| 5247 | const mod = self.bin_file.comp.module.?; | |
| 5274 | const pt = self.pt; | |
| 5275 | const mod = pt.zcu; | |
| 5248 | 5276 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5249 | 5277 | const result: MCValue = result: { |
| 5250 | 5278 | const src_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -5259,8 +5287,8 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void { |
| 5259 | 5287 | const eu_ty = src_ty.childType(mod); |
| 5260 | 5288 | const pl_ty = eu_ty.errorUnionPayload(mod); |
| 5261 | 5289 | const err_ty = eu_ty.errorUnionSet(mod); |
| 5262 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, mod)); | |
| 5263 | const err_abi_size: u32 = @intCast(err_ty.abiSize(mod)); | |
| 5290 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, pt)); | |
| 5291 | const err_abi_size: u32 = @intCast(err_ty.abiSize(pt)); | |
| 5264 | 5292 | try self.asmMemoryImmediate( |
| 5265 | 5293 | .{ ._, .mov }, |
| 5266 | 5294 | .{ |
| ... | ... | @@ -5283,8 +5311,8 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) !void { |
| 5283 | 5311 | const dst_lock = self.register_manager.lockReg(dst_reg); |
| 5284 | 5312 | defer if (dst_lock) |lock| self.register_manager.unlockReg(lock); |
| 5285 | 5313 | |
| 5286 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, mod)); | |
| 5287 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(mod)); | |
| 5314 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, pt)); | |
| 5315 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(pt)); | |
| 5288 | 5316 | try self.asmRegisterMemory( |
| 5289 | 5317 | .{ ._, .lea }, |
| 5290 | 5318 | registerAlias(dst_reg, dst_abi_size), |
| ... | ... | @@ -5304,13 +5332,14 @@ fn genUnwrapErrUnionPayloadMir( |
| 5304 | 5332 | err_union_ty: Type, |
| 5305 | 5333 | err_union: MCValue, |
| 5306 | 5334 | ) !MCValue { |
| 5307 | const mod = self.bin_file.comp.module.?; | |
| 5335 | const pt = self.pt; | |
| 5336 | const mod = pt.zcu; | |
| 5308 | 5337 | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| 5309 | 5338 | |
| 5310 | 5339 | const result: MCValue = result: { |
| 5311 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result .none; | |
| 5340 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result .none; | |
| 5312 | 5341 | |
| 5313 | const payload_off: u31 = @intCast(errUnionPayloadOffset(payload_ty, mod)); | |
| 5342 | const payload_off: u31 = @intCast(errUnionPayloadOffset(payload_ty, pt)); | |
| 5314 | 5343 | switch (err_union) { |
| 5315 | 5344 | .load_frame => |frame_addr| break :result .{ .load_frame = .{ |
| 5316 | 5345 | .index = frame_addr.index, |
| ... | ... | @@ -5353,12 +5382,13 @@ fn genUnwrapErrUnionPayloadPtrMir( |
| 5353 | 5382 | ptr_ty: Type, |
| 5354 | 5383 | ptr_mcv: MCValue, |
| 5355 | 5384 | ) !MCValue { |
| 5356 | const mod = self.bin_file.comp.module.?; | |
| 5385 | const pt = self.pt; | |
| 5386 | const mod = pt.zcu; | |
| 5357 | 5387 | const err_union_ty = ptr_ty.childType(mod); |
| 5358 | 5388 | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| 5359 | 5389 | |
| 5360 | 5390 | const result: MCValue = result: { |
| 5361 | const payload_off = errUnionPayloadOffset(payload_ty, mod); | |
| 5391 | const payload_off = errUnionPayloadOffset(payload_ty, pt); | |
| 5362 | 5392 | const result_mcv: MCValue = if (maybe_inst) |inst| |
| 5363 | 5393 | try self.copyToRegisterWithInstTracking(inst, ptr_ty, ptr_mcv) |
| 5364 | 5394 | else |
| ... | ... | @@ -5387,11 +5417,12 @@ fn airSaveErrReturnTraceIndex(self: *Self, inst: Air.Inst.Index) !void { |
| 5387 | 5417 | } |
| 5388 | 5418 | |
| 5389 | 5419 | fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { |
| 5390 | const mod = self.bin_file.comp.module.?; | |
| 5420 | const pt = self.pt; | |
| 5421 | const mod = pt.zcu; | |
| 5391 | 5422 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5392 | 5423 | const result: MCValue = result: { |
| 5393 | 5424 | const pl_ty = self.typeOf(ty_op.operand); |
| 5394 | if (!pl_ty.hasRuntimeBits(mod)) break :result .{ .immediate = 1 }; | |
| 5425 | if (!pl_ty.hasRuntimeBits(pt)) break :result .{ .immediate = 1 }; | |
| 5395 | 5426 | |
| 5396 | 5427 | const opt_ty = self.typeOfIndex(inst); |
| 5397 | 5428 | const pl_mcv = try self.resolveInst(ty_op.operand); |
| ... | ... | @@ -5408,7 +5439,7 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { |
| 5408 | 5439 | try self.genCopy(pl_ty, opt_mcv, pl_mcv, .{}); |
| 5409 | 5440 | |
| 5410 | 5441 | if (!same_repr) { |
| 5411 | const pl_abi_size: i32 = @intCast(pl_ty.abiSize(mod)); | |
| 5442 | const pl_abi_size: i32 = @intCast(pl_ty.abiSize(pt)); | |
| 5412 | 5443 | switch (opt_mcv) { |
| 5413 | 5444 | else => unreachable, |
| 5414 | 5445 | |
| ... | ... | @@ -5441,7 +5472,8 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void { |
| 5441 | 5472 | |
| 5442 | 5473 | /// T to E!T |
| 5443 | 5474 | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 5444 | const mod = self.bin_file.comp.module.?; | |
| 5475 | const pt = self.pt; | |
| 5476 | const mod = pt.zcu; | |
| 5445 | 5477 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5446 | 5478 | |
| 5447 | 5479 | const eu_ty = ty_op.ty.toType(); |
| ... | ... | @@ -5450,11 +5482,11 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 5450 | 5482 | const operand = try self.resolveInst(ty_op.operand); |
| 5451 | 5483 | |
| 5452 | 5484 | const result: MCValue = result: { |
| 5453 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result .{ .immediate = 0 }; | |
| 5485 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result .{ .immediate = 0 }; | |
| 5454 | 5486 | |
| 5455 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(eu_ty, mod)); | |
| 5456 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, mod)); | |
| 5457 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, mod)); | |
| 5487 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(eu_ty, pt)); | |
| 5488 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, pt)); | |
| 5489 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, pt)); | |
| 5458 | 5490 | try self.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, operand, .{}); |
| 5459 | 5491 | try self.genSetMem(.{ .frame = frame_index }, err_off, err_ty, .{ .immediate = 0 }, .{}); |
| 5460 | 5492 | break :result .{ .load_frame = .{ .index = frame_index } }; |
| ... | ... | @@ -5464,7 +5496,8 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 5464 | 5496 | |
| 5465 | 5497 | /// E to E!T |
| 5466 | 5498 | fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void { |
| 5467 | const mod = self.bin_file.comp.module.?; | |
| 5499 | const pt = self.pt; | |
| 5500 | const mod = pt.zcu; | |
| 5468 | 5501 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5469 | 5502 | |
| 5470 | 5503 | const eu_ty = ty_op.ty.toType(); |
| ... | ... | @@ -5472,11 +5505,11 @@ fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) !void { |
| 5472 | 5505 | const err_ty = eu_ty.errorUnionSet(mod); |
| 5473 | 5506 | |
| 5474 | 5507 | const result: MCValue = result: { |
| 5475 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result try self.resolveInst(ty_op.operand); | |
| 5508 | if (!pl_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result try self.resolveInst(ty_op.operand); | |
| 5476 | 5509 | |
| 5477 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(eu_ty, mod)); | |
| 5478 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, mod)); | |
| 5479 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, mod)); | |
| 5510 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(eu_ty, pt)); | |
| 5511 | const pl_off: i32 = @intCast(errUnionPayloadOffset(pl_ty, pt)); | |
| 5512 | const err_off: i32 = @intCast(errUnionErrorOffset(pl_ty, pt)); | |
| 5480 | 5513 | try self.genSetMem(.{ .frame = frame_index }, pl_off, pl_ty, .undef, .{}); |
| 5481 | 5514 | const operand = try self.resolveInst(ty_op.operand); |
| 5482 | 5515 | try self.genSetMem(.{ .frame = frame_index }, err_off, err_ty, operand, .{}); |
| ... | ... | @@ -5523,7 +5556,7 @@ fn airSliceLen(self: *Self, inst: Air.Inst.Index) !void { |
| 5523 | 5556 | } |
| 5524 | 5557 | |
| 5525 | 5558 | fn airPtrSliceLenPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 5526 | const mod = self.bin_file.comp.module.?; | |
| 5559 | const pt = self.pt; | |
| 5527 | 5560 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5528 | 5561 | |
| 5529 | 5562 | const src_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -5544,7 +5577,7 @@ fn airPtrSliceLenPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 5544 | 5577 | const dst_lock = self.register_manager.lockReg(dst_reg); |
| 5545 | 5578 | defer if (dst_lock) |lock| self.register_manager.unlockReg(lock); |
| 5546 | 5579 | |
| 5547 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(mod)); | |
| 5580 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(pt)); | |
| 5548 | 5581 | try self.asmRegisterMemory( |
| 5549 | 5582 | .{ ._, .lea }, |
| 5550 | 5583 | registerAlias(dst_reg, dst_abi_size), |
| ... | ... | @@ -5591,7 +5624,8 @@ fn elemOffset(self: *Self, index_ty: Type, index: MCValue, elem_size: u64) !Regi |
| 5591 | 5624 | } |
| 5592 | 5625 | |
| 5593 | 5626 | fn genSliceElemPtr(self: *Self, lhs: Air.Inst.Ref, rhs: Air.Inst.Ref) !MCValue { |
| 5594 | const mod = self.bin_file.comp.module.?; | |
| 5627 | const pt = self.pt; | |
| 5628 | const mod = pt.zcu; | |
| 5595 | 5629 | const slice_ty = self.typeOf(lhs); |
| 5596 | 5630 | const slice_mcv = try self.resolveInst(lhs); |
| 5597 | 5631 | const slice_mcv_lock: ?RegisterLock = switch (slice_mcv) { |
| ... | ... | @@ -5601,7 +5635,7 @@ fn genSliceElemPtr(self: *Self, lhs: Air.Inst.Ref, rhs: Air.Inst.Ref) !MCValue { |
| 5601 | 5635 | defer if (slice_mcv_lock) |lock| self.register_manager.unlockReg(lock); |
| 5602 | 5636 | |
| 5603 | 5637 | const elem_ty = slice_ty.childType(mod); |
| 5604 | const elem_size = elem_ty.abiSize(mod); | |
| 5638 | const elem_size = elem_ty.abiSize(pt); | |
| 5605 | 5639 | const slice_ptr_field_type = slice_ty.slicePtrFieldType(mod); |
| 5606 | 5640 | |
| 5607 | 5641 | const index_ty = self.typeOf(rhs); |
| ... | ... | @@ -5627,12 +5661,13 @@ fn genSliceElemPtr(self: *Self, lhs: Air.Inst.Ref, rhs: Air.Inst.Ref) !MCValue { |
| 5627 | 5661 | } |
| 5628 | 5662 | |
| 5629 | 5663 | fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 5630 | const mod = self.bin_file.comp.module.?; | |
| 5664 | const pt = self.pt; | |
| 5665 | const mod = pt.zcu; | |
| 5631 | 5666 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 5632 | 5667 | |
| 5633 | 5668 | const result: MCValue = result: { |
| 5634 | 5669 | const elem_ty = self.typeOfIndex(inst); |
| 5635 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result .none; | |
| 5670 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result .none; | |
| 5636 | 5671 | |
| 5637 | 5672 | const slice_ty = self.typeOf(bin_op.lhs); |
| 5638 | 5673 | const slice_ptr_field_type = slice_ty.slicePtrFieldType(mod); |
| ... | ... | @@ -5652,7 +5687,8 @@ fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 5652 | 5687 | } |
| 5653 | 5688 | |
| 5654 | 5689 | fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 5655 | const mod = self.bin_file.comp.module.?; | |
| 5690 | const pt = self.pt; | |
| 5691 | const mod = pt.zcu; | |
| 5656 | 5692 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 5657 | 5693 | |
| 5658 | 5694 | const result: MCValue = result: { |
| ... | ... | @@ -5675,7 +5711,7 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 5675 | 5711 | defer if (index_lock) |lock| self.register_manager.unlockReg(lock); |
| 5676 | 5712 | |
| 5677 | 5713 | try self.spillEflagsIfOccupied(); |
| 5678 | if (array_ty.isVector(mod) and elem_ty.bitSize(mod) == 1) { | |
| 5714 | if (array_ty.isVector(mod) and elem_ty.bitSize(pt) == 1) { | |
| 5679 | 5715 | const index_reg = switch (index_mcv) { |
| 5680 | 5716 | .register => |reg| reg, |
| 5681 | 5717 | else => try self.copyToTmpRegister(index_ty, index_mcv), |
| ... | ... | @@ -5688,7 +5724,7 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 5688 | 5724 | index_reg.to64(), |
| 5689 | 5725 | ), |
| 5690 | 5726 | .sse => { |
| 5691 | const frame_index = try self.allocFrameIndex(FrameAlloc.initType(array_ty, mod)); | |
| 5727 | const frame_index = try self.allocFrameIndex(FrameAlloc.initType(array_ty, pt)); | |
| 5692 | 5728 | try self.genSetMem(.{ .frame = frame_index }, 0, array_ty, array_mcv, .{}); |
| 5693 | 5729 | try self.asmMemoryRegister( |
| 5694 | 5730 | .{ ._, .bt }, |
| ... | ... | @@ -5717,7 +5753,7 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 5717 | 5753 | index_reg.to64(), |
| 5718 | 5754 | ), |
| 5719 | 5755 | else => return self.fail("TODO airArrayElemVal for {s} of {}", .{ |
| 5720 | @tagName(array_mcv), array_ty.fmt(mod), | |
| 5756 | @tagName(array_mcv), array_ty.fmt(pt), | |
| 5721 | 5757 | }), |
| 5722 | 5758 | } |
| 5723 | 5759 | |
| ... | ... | @@ -5726,14 +5762,14 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 5726 | 5762 | break :result .{ .register = dst_reg }; |
| 5727 | 5763 | } |
| 5728 | 5764 | |
| 5729 | const elem_abi_size = elem_ty.abiSize(mod); | |
| 5765 | const elem_abi_size = elem_ty.abiSize(pt); | |
| 5730 | 5766 | const addr_reg = try self.register_manager.allocReg(null, abi.RegisterClass.gp); |
| 5731 | 5767 | const addr_lock = self.register_manager.lockRegAssumeUnused(addr_reg); |
| 5732 | 5768 | defer self.register_manager.unlockReg(addr_lock); |
| 5733 | 5769 | |
| 5734 | 5770 | switch (array_mcv) { |
| 5735 | 5771 | .register => { |
| 5736 | const frame_index = try self.allocFrameIndex(FrameAlloc.initType(array_ty, mod)); | |
| 5772 | const frame_index = try self.allocFrameIndex(FrameAlloc.initType(array_ty, pt)); | |
| 5737 | 5773 | try self.genSetMem(.{ .frame = frame_index }, 0, array_ty, array_mcv, .{}); |
| 5738 | 5774 | try self.asmRegisterMemory( |
| 5739 | 5775 | .{ ._, .lea }, |
| ... | ... | @@ -5757,7 +5793,7 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 5757 | 5793 | => try self.genSetReg(addr_reg, Type.usize, array_mcv.address(), .{}), |
| 5758 | 5794 | .lea_symbol, .lea_direct, .lea_tlv => unreachable, |
| 5759 | 5795 | else => return self.fail("TODO airArrayElemVal_val for {s} of {}", .{ |
| 5760 | @tagName(array_mcv), array_ty.fmt(mod), | |
| 5796 | @tagName(array_mcv), array_ty.fmt(pt), | |
| 5761 | 5797 | }), |
| 5762 | 5798 | } |
| 5763 | 5799 | |
| ... | ... | @@ -5781,7 +5817,8 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 5781 | 5817 | } |
| 5782 | 5818 | |
| 5783 | 5819 | fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 5784 | const mod = self.bin_file.comp.module.?; | |
| 5820 | const pt = self.pt; | |
| 5821 | const mod = pt.zcu; | |
| 5785 | 5822 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 5786 | 5823 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 5787 | 5824 | |
| ... | ... | @@ -5790,9 +5827,9 @@ fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 5790 | 5827 | |
| 5791 | 5828 | const result = result: { |
| 5792 | 5829 | const elem_ty = ptr_ty.elemType2(mod); |
| 5793 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result .none; | |
| 5830 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result .none; | |
| 5794 | 5831 | |
| 5795 | const elem_abi_size: u32 = @intCast(elem_ty.abiSize(mod)); | |
| 5832 | const elem_abi_size: u32 = @intCast(elem_ty.abiSize(pt)); | |
| 5796 | 5833 | const index_ty = self.typeOf(bin_op.rhs); |
| 5797 | 5834 | const index_mcv = try self.resolveInst(bin_op.rhs); |
| 5798 | 5835 | const index_lock = switch (index_mcv) { |
| ... | ... | @@ -5831,7 +5868,8 @@ fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) !void { |
| 5831 | 5868 | } |
| 5832 | 5869 | |
| 5833 | 5870 | fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 5834 | const mod = self.bin_file.comp.module.?; | |
| 5871 | const pt = self.pt; | |
| 5872 | const mod = pt.zcu; | |
| 5835 | 5873 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5836 | 5874 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 5837 | 5875 | |
| ... | ... | @@ -5854,7 +5892,7 @@ fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 5854 | 5892 | } |
| 5855 | 5893 | |
| 5856 | 5894 | const elem_ty = base_ptr_ty.elemType2(mod); |
| 5857 | const elem_abi_size = elem_ty.abiSize(mod); | |
| 5895 | const elem_abi_size = elem_ty.abiSize(pt); | |
| 5858 | 5896 | const index_ty = self.typeOf(extra.rhs); |
| 5859 | 5897 | const index_mcv = try self.resolveInst(extra.rhs); |
| 5860 | 5898 | const index_lock: ?RegisterLock = switch (index_mcv) { |
| ... | ... | @@ -5876,12 +5914,13 @@ fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 5876 | 5914 | } |
| 5877 | 5915 | |
| 5878 | 5916 | fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) !void { |
| 5879 | const mod = self.bin_file.comp.module.?; | |
| 5917 | const pt = self.pt; | |
| 5918 | const mod = pt.zcu; | |
| 5880 | 5919 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 5881 | 5920 | const ptr_union_ty = self.typeOf(bin_op.lhs); |
| 5882 | 5921 | const union_ty = ptr_union_ty.childType(mod); |
| 5883 | 5922 | const tag_ty = self.typeOf(bin_op.rhs); |
| 5884 | const layout = union_ty.unionGetLayout(mod); | |
| 5923 | const layout = union_ty.unionGetLayout(pt); | |
| 5885 | 5924 | |
| 5886 | 5925 | if (layout.tag_size == 0) { |
| 5887 | 5926 | return self.finishAir(inst, .none, .{ bin_op.lhs, bin_op.rhs, .none }); |
| ... | ... | @@ -5913,19 +5952,19 @@ fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) !void { |
| 5913 | 5952 | break :blk MCValue{ .register = reg }; |
| 5914 | 5953 | } else ptr; |
| 5915 | 5954 | |
| 5916 | const ptr_tag_ty = try mod.adjustPtrTypeChild(ptr_union_ty, tag_ty); | |
| 5955 | const ptr_tag_ty = try pt.adjustPtrTypeChild(ptr_union_ty, tag_ty); | |
| 5917 | 5956 | try self.store(ptr_tag_ty, adjusted_ptr, tag, .{}); |
| 5918 | 5957 | |
| 5919 | 5958 | return self.finishAir(inst, .none, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 5920 | 5959 | } |
| 5921 | 5960 | |
| 5922 | 5961 | fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) !void { |
| 5923 | const mod = self.bin_file.comp.module.?; | |
| 5962 | const pt = self.pt; | |
| 5924 | 5963 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5925 | 5964 | |
| 5926 | 5965 | const tag_ty = self.typeOfIndex(inst); |
| 5927 | 5966 | const union_ty = self.typeOf(ty_op.operand); |
| 5928 | const layout = union_ty.unionGetLayout(mod); | |
| 5967 | const layout = union_ty.unionGetLayout(pt); | |
| 5929 | 5968 | |
| 5930 | 5969 | if (layout.tag_size == 0) { |
| 5931 | 5970 | return self.finishAir(inst, .none, .{ ty_op.operand, .none, .none }); |
| ... | ... | @@ -5939,7 +5978,7 @@ fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) !void { |
| 5939 | 5978 | }; |
| 5940 | 5979 | defer if (operand_lock) |lock| self.register_manager.unlockReg(lock); |
| 5941 | 5980 | |
| 5942 | const tag_abi_size = tag_ty.abiSize(mod); | |
| 5981 | const tag_abi_size = tag_ty.abiSize(pt); | |
| 5943 | 5982 | const dst_mcv: MCValue = blk: { |
| 5944 | 5983 | switch (operand) { |
| 5945 | 5984 | .load_frame => |frame_addr| { |
| ... | ... | @@ -5983,7 +6022,8 @@ fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) !void { |
| 5983 | 6022 | } |
| 5984 | 6023 | |
| 5985 | 6024 | fn airClz(self: *Self, inst: Air.Inst.Index) !void { |
| 5986 | const mod = self.bin_file.comp.module.?; | |
| 6025 | const pt = self.pt; | |
| 6026 | const mod = pt.zcu; | |
| 5987 | 6027 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5988 | 6028 | const result = result: { |
| 5989 | 6029 | try self.spillEflagsIfOccupied(); |
| ... | ... | @@ -5991,7 +6031,7 @@ fn airClz(self: *Self, inst: Air.Inst.Index) !void { |
| 5991 | 6031 | const dst_ty = self.typeOfIndex(inst); |
| 5992 | 6032 | const src_ty = self.typeOf(ty_op.operand); |
| 5993 | 6033 | if (src_ty.zigTypeTag(mod) == .Vector) return self.fail("TODO implement airClz for {}", .{ |
| 5994 | src_ty.fmt(mod), | |
| 6034 | src_ty.fmt(pt), | |
| 5995 | 6035 | }); |
| 5996 | 6036 | |
| 5997 | 6037 | const src_mcv = try self.resolveInst(ty_op.operand); |
| ... | ... | @@ -6010,8 +6050,8 @@ fn airClz(self: *Self, inst: Air.Inst.Index) !void { |
| 6010 | 6050 | const dst_lock = self.register_manager.lockRegAssumeUnused(dst_reg); |
| 6011 | 6051 | defer self.register_manager.unlockReg(dst_lock); |
| 6012 | 6052 | |
| 6013 | const abi_size: u31 = @intCast(src_ty.abiSize(mod)); | |
| 6014 | const src_bits: u31 = @intCast(src_ty.bitSize(mod)); | |
| 6053 | const abi_size: u31 = @intCast(src_ty.abiSize(pt)); | |
| 6054 | const src_bits: u31 = @intCast(src_ty.bitSize(pt)); | |
| 6015 | 6055 | const has_lzcnt = self.hasFeature(.lzcnt); |
| 6016 | 6056 | if (src_bits > @as(u32, if (has_lzcnt) 128 else 64)) { |
| 6017 | 6057 | const limbs_len = math.divCeil(u32, abi_size, 8) catch unreachable; |
| ... | ... | @@ -6121,7 +6161,7 @@ fn airClz(self: *Self, inst: Air.Inst.Index) !void { |
| 6121 | 6161 | } |
| 6122 | 6162 | |
| 6123 | 6163 | assert(src_bits <= 64); |
| 6124 | const cmov_abi_size = @max(@as(u32, @intCast(dst_ty.abiSize(mod))), 2); | |
| 6164 | const cmov_abi_size = @max(@as(u32, @intCast(dst_ty.abiSize(pt))), 2); | |
| 6125 | 6165 | if (math.isPowerOfTwo(src_bits)) { |
| 6126 | 6166 | const imm_reg = try self.copyToTmpRegister(dst_ty, .{ |
| 6127 | 6167 | .immediate = src_bits ^ (src_bits - 1), |
| ... | ... | @@ -6179,7 +6219,8 @@ fn airClz(self: *Self, inst: Air.Inst.Index) !void { |
| 6179 | 6219 | } |
| 6180 | 6220 | |
| 6181 | 6221 | fn airCtz(self: *Self, inst: Air.Inst.Index) !void { |
| 6182 | const mod = self.bin_file.comp.module.?; | |
| 6222 | const pt = self.pt; | |
| 6223 | const mod = pt.zcu; | |
| 6183 | 6224 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6184 | 6225 | const result = result: { |
| 6185 | 6226 | try self.spillEflagsIfOccupied(); |
| ... | ... | @@ -6187,7 +6228,7 @@ fn airCtz(self: *Self, inst: Air.Inst.Index) !void { |
| 6187 | 6228 | const dst_ty = self.typeOfIndex(inst); |
| 6188 | 6229 | const src_ty = self.typeOf(ty_op.operand); |
| 6189 | 6230 | if (src_ty.zigTypeTag(mod) == .Vector) return self.fail("TODO implement airCtz for {}", .{ |
| 6190 | src_ty.fmt(mod), | |
| 6231 | src_ty.fmt(pt), | |
| 6191 | 6232 | }); |
| 6192 | 6233 | |
| 6193 | 6234 | const src_mcv = try self.resolveInst(ty_op.operand); |
| ... | ... | @@ -6206,8 +6247,8 @@ fn airCtz(self: *Self, inst: Air.Inst.Index) !void { |
| 6206 | 6247 | const dst_lock = self.register_manager.lockReg(dst_reg); |
| 6207 | 6248 | defer if (dst_lock) |lock| self.register_manager.unlockReg(lock); |
| 6208 | 6249 | |
| 6209 | const abi_size: u31 = @intCast(src_ty.abiSize(mod)); | |
| 6210 | const src_bits: u31 = @intCast(src_ty.bitSize(mod)); | |
| 6250 | const abi_size: u31 = @intCast(src_ty.abiSize(pt)); | |
| 6251 | const src_bits: u31 = @intCast(src_ty.bitSize(pt)); | |
| 6211 | 6252 | const has_bmi = self.hasFeature(.bmi); |
| 6212 | 6253 | if (src_bits > @as(u32, if (has_bmi) 128 else 64)) { |
| 6213 | 6254 | const limbs_len = math.divCeil(u32, abi_size, 8) catch unreachable; |
| ... | ... | @@ -6328,7 +6369,7 @@ fn airCtz(self: *Self, inst: Air.Inst.Index) !void { |
| 6328 | 6369 | try self.genBinOpMir(.{ ._, .bsf }, wide_ty, dst_mcv, .{ .register = wide_reg }); |
| 6329 | 6370 | } else try self.genBinOpMir(.{ ._, .bsf }, src_ty, dst_mcv, mat_src_mcv); |
| 6330 | 6371 | |
| 6331 | const cmov_abi_size = @max(@as(u32, @intCast(dst_ty.abiSize(mod))), 2); | |
| 6372 | const cmov_abi_size = @max(@as(u32, @intCast(dst_ty.abiSize(pt))), 2); | |
| 6332 | 6373 | try self.asmCmovccRegisterRegister( |
| 6333 | 6374 | .z, |
| 6334 | 6375 | registerAlias(dst_reg, cmov_abi_size), |
| ... | ... | @@ -6340,15 +6381,16 @@ fn airCtz(self: *Self, inst: Air.Inst.Index) !void { |
| 6340 | 6381 | } |
| 6341 | 6382 | |
| 6342 | 6383 | fn airPopCount(self: *Self, inst: Air.Inst.Index) !void { |
| 6343 | const mod = self.bin_file.comp.module.?; | |
| 6384 | const pt = self.pt; | |
| 6385 | const mod = pt.zcu; | |
| 6344 | 6386 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6345 | 6387 | const result: MCValue = result: { |
| 6346 | 6388 | try self.spillEflagsIfOccupied(); |
| 6347 | 6389 | |
| 6348 | 6390 | const src_ty = self.typeOf(ty_op.operand); |
| 6349 | const src_abi_size: u32 = @intCast(src_ty.abiSize(mod)); | |
| 6391 | const src_abi_size: u32 = @intCast(src_ty.abiSize(pt)); | |
| 6350 | 6392 | if (src_ty.zigTypeTag(mod) == .Vector or src_abi_size > 16) |
| 6351 | return self.fail("TODO implement airPopCount for {}", .{src_ty.fmt(mod)}); | |
| 6393 | return self.fail("TODO implement airPopCount for {}", .{src_ty.fmt(pt)}); | |
| 6352 | 6394 | const src_mcv = try self.resolveInst(ty_op.operand); |
| 6353 | 6395 | |
| 6354 | 6396 | const mat_src_mcv = switch (src_mcv) { |
| ... | ... | @@ -6385,7 +6427,7 @@ fn airPopCount(self: *Self, inst: Air.Inst.Index) !void { |
| 6385 | 6427 | else |
| 6386 | 6428 | .{ .register = mat_src_mcv.register_pair[0] }, false); |
| 6387 | 6429 | const src_info = src_ty.intInfo(mod); |
| 6388 | const hi_ty = try mod.intType(src_info.signedness, (src_info.bits - 1) % 64 + 1); | |
| 6430 | const hi_ty = try pt.intType(src_info.signedness, (src_info.bits - 1) % 64 + 1); | |
| 6389 | 6431 | try self.genPopCount(tmp_regs[1], hi_ty, if (mat_src_mcv.isMemory()) |
| 6390 | 6432 | mat_src_mcv.address().offset(8).deref() |
| 6391 | 6433 | else |
| ... | ... | @@ -6403,16 +6445,16 @@ fn genPopCount( |
| 6403 | 6445 | src_mcv: MCValue, |
| 6404 | 6446 | dst_contains_src: bool, |
| 6405 | 6447 | ) !void { |
| 6406 | const mod = self.bin_file.comp.module.?; | |
| 6448 | const pt = self.pt; | |
| 6407 | 6449 | |
| 6408 | const src_abi_size: u32 = @intCast(src_ty.abiSize(mod)); | |
| 6450 | const src_abi_size: u32 = @intCast(src_ty.abiSize(pt)); | |
| 6409 | 6451 | if (self.hasFeature(.popcnt)) return self.genBinOpMir( |
| 6410 | 6452 | .{ ._, .popcnt }, |
| 6411 | 6453 | if (src_abi_size > 1) src_ty else Type.u32, |
| 6412 | 6454 | .{ .register = dst_reg }, |
| 6413 | 6455 | if (src_abi_size > 1) src_mcv else src: { |
| 6414 | 6456 | if (!dst_contains_src) try self.genSetReg(dst_reg, src_ty, src_mcv, .{}); |
| 6415 | try self.truncateRegister(try src_ty.toUnsigned(mod), dst_reg); | |
| 6457 | try self.truncateRegister(try src_ty.toUnsigned(pt), dst_reg); | |
| 6416 | 6458 | break :src .{ .register = dst_reg }; |
| 6417 | 6459 | }, |
| 6418 | 6460 | ); |
| ... | ... | @@ -6495,13 +6537,14 @@ fn genByteSwap( |
| 6495 | 6537 | src_mcv: MCValue, |
| 6496 | 6538 | mem_ok: bool, |
| 6497 | 6539 | ) !MCValue { |
| 6498 | const mod = self.bin_file.comp.module.?; | |
| 6540 | const pt = self.pt; | |
| 6541 | const mod = pt.zcu; | |
| 6499 | 6542 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6500 | 6543 | const has_movbe = self.hasFeature(.movbe); |
| 6501 | 6544 | |
| 6502 | 6545 | if (src_ty.zigTypeTag(mod) == .Vector) return self.fail( |
| 6503 | 6546 | "TODO implement genByteSwap for {}", |
| 6504 | .{src_ty.fmt(mod)}, | |
| 6547 | .{src_ty.fmt(pt)}, | |
| 6505 | 6548 | ); |
| 6506 | 6549 | |
| 6507 | 6550 | const src_lock = switch (src_mcv) { |
| ... | ... | @@ -6510,7 +6553,7 @@ fn genByteSwap( |
| 6510 | 6553 | }; |
| 6511 | 6554 | defer if (src_lock) |lock| self.register_manager.unlockReg(lock); |
| 6512 | 6555 | |
| 6513 | const abi_size: u32 = @intCast(src_ty.abiSize(mod)); | |
| 6556 | const abi_size: u32 = @intCast(src_ty.abiSize(pt)); | |
| 6514 | 6557 | switch (abi_size) { |
| 6515 | 6558 | 0 => unreachable, |
| 6516 | 6559 | 1 => return if ((mem_ok or src_mcv.isRegister()) and |
| ... | ... | @@ -6658,11 +6701,12 @@ fn genByteSwap( |
| 6658 | 6701 | } |
| 6659 | 6702 | |
| 6660 | 6703 | fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void { |
| 6661 | const mod = self.bin_file.comp.module.?; | |
| 6704 | const pt = self.pt; | |
| 6705 | const mod = pt.zcu; | |
| 6662 | 6706 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6663 | 6707 | |
| 6664 | 6708 | const src_ty = self.typeOf(ty_op.operand); |
| 6665 | const src_bits: u32 = @intCast(src_ty.bitSize(mod)); | |
| 6709 | const src_bits: u32 = @intCast(src_ty.bitSize(pt)); | |
| 6666 | 6710 | const src_mcv = try self.resolveInst(ty_op.operand); |
| 6667 | 6711 | |
| 6668 | 6712 | const dst_mcv = try self.genByteSwap(inst, src_ty, src_mcv, true); |
| ... | ... | @@ -6674,18 +6718,19 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) !void { |
| 6674 | 6718 | src_ty, |
| 6675 | 6719 | dst_mcv, |
| 6676 | 6720 | if (src_bits > 256) Type.u16 else Type.u8, |
| 6677 | .{ .immediate = src_ty.abiSize(mod) * 8 - src_bits }, | |
| 6721 | .{ .immediate = src_ty.abiSize(pt) * 8 - src_bits }, | |
| 6678 | 6722 | ); |
| 6679 | 6723 | return self.finishAir(inst, dst_mcv, .{ ty_op.operand, .none, .none }); |
| 6680 | 6724 | } |
| 6681 | 6725 | |
| 6682 | 6726 | fn airBitReverse(self: *Self, inst: Air.Inst.Index) !void { |
| 6683 | const mod = self.bin_file.comp.module.?; | |
| 6727 | const pt = self.pt; | |
| 6728 | const mod = pt.zcu; | |
| 6684 | 6729 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6685 | 6730 | |
| 6686 | 6731 | const src_ty = self.typeOf(ty_op.operand); |
| 6687 | const abi_size: u32 = @intCast(src_ty.abiSize(mod)); | |
| 6688 | const bit_size: u32 = @intCast(src_ty.bitSize(mod)); | |
| 6732 | const abi_size: u32 = @intCast(src_ty.abiSize(pt)); | |
| 6733 | const bit_size: u32 = @intCast(src_ty.bitSize(pt)); | |
| 6689 | 6734 | const src_mcv = try self.resolveInst(ty_op.operand); |
| 6690 | 6735 | |
| 6691 | 6736 | const dst_mcv = try self.genByteSwap(inst, src_ty, src_mcv, false); |
| ... | ... | @@ -6802,14 +6847,15 @@ fn airBitReverse(self: *Self, inst: Air.Inst.Index) !void { |
| 6802 | 6847 | } |
| 6803 | 6848 | |
| 6804 | 6849 | fn floatSign(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, ty: Type) !void { |
| 6805 | const mod = self.bin_file.comp.module.?; | |
| 6850 | const pt = self.pt; | |
| 6851 | const mod = pt.zcu; | |
| 6806 | 6852 | const tag = self.air.instructions.items(.tag)[@intFromEnum(inst)]; |
| 6807 | 6853 | |
| 6808 | 6854 | const result = result: { |
| 6809 | 6855 | const scalar_bits = ty.scalarType(mod).floatBits(self.target.*); |
| 6810 | 6856 | if (scalar_bits == 80) { |
| 6811 | 6857 | if (ty.zigTypeTag(mod) != .Float) return self.fail("TODO implement floatSign for {}", .{ |
| 6812 | ty.fmt(mod), | |
| 6858 | ty.fmt(pt), | |
| 6813 | 6859 | }); |
| 6814 | 6860 | |
| 6815 | 6861 | const src_mcv = try self.resolveInst(operand); |
| ... | ... | @@ -6829,11 +6875,11 @@ fn floatSign(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, ty: Type) |
| 6829 | 6875 | break :result dst_mcv; |
| 6830 | 6876 | } |
| 6831 | 6877 | |
| 6832 | const abi_size: u32 = switch (ty.abiSize(mod)) { | |
| 6878 | const abi_size: u32 = switch (ty.abiSize(pt)) { | |
| 6833 | 6879 | 1...16 => 16, |
| 6834 | 6880 | 17...32 => 32, |
| 6835 | 6881 | else => return self.fail("TODO implement floatSign for {}", .{ |
| 6836 | ty.fmt(mod), | |
| 6882 | ty.fmt(pt), | |
| 6837 | 6883 | }), |
| 6838 | 6884 | }; |
| 6839 | 6885 | |
| ... | ... | @@ -6852,14 +6898,14 @@ fn floatSign(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, ty: Type) |
| 6852 | 6898 | const dst_lock = self.register_manager.lockReg(dst_reg); |
| 6853 | 6899 | defer if (dst_lock) |lock| self.register_manager.unlockReg(lock); |
| 6854 | 6900 | |
| 6855 | const vec_ty = try mod.vectorType(.{ | |
| 6901 | const vec_ty = try pt.vectorType(.{ | |
| 6856 | 6902 | .len = @divExact(abi_size * 8, scalar_bits), |
| 6857 | .child = (try mod.intType(.signed, scalar_bits)).ip_index, | |
| 6903 | .child = (try pt.intType(.signed, scalar_bits)).ip_index, | |
| 6858 | 6904 | }); |
| 6859 | 6905 | |
| 6860 | 6906 | const sign_mcv = try self.genTypedValue(switch (tag) { |
| 6861 | .neg => try vec_ty.minInt(mod, vec_ty), | |
| 6862 | .abs => try vec_ty.maxInt(mod, vec_ty), | |
| 6907 | .neg => try vec_ty.minInt(pt, vec_ty), | |
| 6908 | .abs => try vec_ty.maxInt(pt, vec_ty), | |
| 6863 | 6909 | else => unreachable, |
| 6864 | 6910 | }); |
| 6865 | 6911 | const sign_mem: Memory = if (sign_mcv.isMemory()) |
| ... | ... | @@ -6891,7 +6937,7 @@ fn floatSign(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, ty: Type) |
| 6891 | 6937 | .abs => .{ .v_pd, .@"and" }, |
| 6892 | 6938 | else => unreachable, |
| 6893 | 6939 | }, |
| 6894 | 80 => return self.fail("TODO implement floatSign for {}", .{ty.fmt(mod)}), | |
| 6940 | 80 => return self.fail("TODO implement floatSign for {}", .{ty.fmt(pt)}), | |
| 6895 | 6941 | else => unreachable, |
| 6896 | 6942 | }, |
| 6897 | 6943 | registerAlias(dst_reg, abi_size), |
| ... | ... | @@ -6917,7 +6963,7 @@ fn floatSign(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, ty: Type) |
| 6917 | 6963 | .abs => .{ ._pd, .@"and" }, |
| 6918 | 6964 | else => unreachable, |
| 6919 | 6965 | }, |
| 6920 | 80 => return self.fail("TODO implement floatSign for {}", .{ty.fmt(mod)}), | |
| 6966 | 80 => return self.fail("TODO implement floatSign for {}", .{ty.fmt(pt)}), | |
| 6921 | 6967 | else => unreachable, |
| 6922 | 6968 | }, |
| 6923 | 6969 | registerAlias(dst_reg, abi_size), |
| ... | ... | @@ -6978,7 +7024,8 @@ fn airRound(self: *Self, inst: Air.Inst.Index, mode: RoundMode) !void { |
| 6978 | 7024 | } |
| 6979 | 7025 | |
| 6980 | 7026 | fn getRoundTag(self: *Self, ty: Type) ?Mir.Inst.FixedTag { |
| 6981 | const mod = self.bin_file.comp.module.?; | |
| 7027 | const pt = self.pt; | |
| 7028 | const mod = pt.zcu; | |
| 6982 | 7029 | return if (self.hasFeature(.sse4_1)) switch (ty.zigTypeTag(mod)) { |
| 6983 | 7030 | .Float => switch (ty.floatBits(self.target.*)) { |
| 6984 | 7031 | 32 => if (self.hasFeature(.avx)) .{ .v_ss, .round } else .{ ._ss, .round }, |
| ... | ... | @@ -7010,11 +7057,12 @@ fn getRoundTag(self: *Self, ty: Type) ?Mir.Inst.FixedTag { |
| 7010 | 7057 | } |
| 7011 | 7058 | |
| 7012 | 7059 | fn genRoundLibcall(self: *Self, ty: Type, src_mcv: MCValue, mode: RoundMode) !MCValue { |
| 7013 | const mod = self.bin_file.comp.module.?; | |
| 7060 | const pt = self.pt; | |
| 7061 | const mod = pt.zcu; | |
| 7014 | 7062 | if (self.getRoundTag(ty)) |_| return .none; |
| 7015 | 7063 | |
| 7016 | 7064 | if (ty.zigTypeTag(mod) != .Float) |
| 7017 | return self.fail("TODO implement genRound for {}", .{ty.fmt(mod)}); | |
| 7065 | return self.fail("TODO implement genRound for {}", .{ty.fmt(pt)}); | |
| 7018 | 7066 | |
| 7019 | 7067 | var callee_buf: ["__trunc?".len]u8 = undefined; |
| 7020 | 7068 | return try self.genCall(.{ .lib = .{ |
| ... | ... | @@ -7034,12 +7082,12 @@ fn genRoundLibcall(self: *Self, ty: Type, src_mcv: MCValue, mode: RoundMode) !MC |
| 7034 | 7082 | } |
| 7035 | 7083 | |
| 7036 | 7084 | fn genRound(self: *Self, ty: Type, dst_reg: Register, src_mcv: MCValue, mode: RoundMode) !void { |
| 7037 | const mod = self.bin_file.comp.module.?; | |
| 7085 | const pt = self.pt; | |
| 7038 | 7086 | const mir_tag = self.getRoundTag(ty) orelse { |
| 7039 | 7087 | const result = try self.genRoundLibcall(ty, src_mcv, mode); |
| 7040 | 7088 | return self.genSetReg(dst_reg, ty, result, .{}); |
| 7041 | 7089 | }; |
| 7042 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 7090 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 7043 | 7091 | const dst_alias = registerAlias(dst_reg, abi_size); |
| 7044 | 7092 | switch (mir_tag[0]) { |
| 7045 | 7093 | .v_ss, .v_sd => if (src_mcv.isMemory()) try self.asmRegisterRegisterMemoryImmediate( |
| ... | ... | @@ -7076,14 +7124,15 @@ fn genRound(self: *Self, ty: Type, dst_reg: Register, src_mcv: MCValue, mode: Ro |
| 7076 | 7124 | } |
| 7077 | 7125 | |
| 7078 | 7126 | fn airAbs(self: *Self, inst: Air.Inst.Index) !void { |
| 7079 | const mod = self.bin_file.comp.module.?; | |
| 7127 | const pt = self.pt; | |
| 7128 | const mod = pt.zcu; | |
| 7080 | 7129 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7081 | 7130 | const ty = self.typeOf(ty_op.operand); |
| 7082 | 7131 | |
| 7083 | 7132 | const result: MCValue = result: { |
| 7084 | 7133 | const mir_tag = @as(?Mir.Inst.FixedTag, switch (ty.zigTypeTag(mod)) { |
| 7085 | 7134 | else => null, |
| 7086 | .Int => switch (ty.abiSize(mod)) { | |
| 7135 | .Int => switch (ty.abiSize(pt)) { | |
| 7087 | 7136 | 0 => unreachable, |
| 7088 | 7137 | 1...8 => { |
| 7089 | 7138 | try self.spillEflagsIfOccupied(); |
| ... | ... | @@ -7092,7 +7141,7 @@ fn airAbs(self: *Self, inst: Air.Inst.Index) !void { |
| 7092 | 7141 | |
| 7093 | 7142 | try self.genUnOpMir(.{ ._, .neg }, ty, dst_mcv); |
| 7094 | 7143 | |
| 7095 | const cmov_abi_size = @max(@as(u32, @intCast(ty.abiSize(mod))), 2); | |
| 7144 | const cmov_abi_size = @max(@as(u32, @intCast(ty.abiSize(pt))), 2); | |
| 7096 | 7145 | switch (src_mcv) { |
| 7097 | 7146 | .register => |val_reg| try self.asmCmovccRegisterRegister( |
| 7098 | 7147 | .l, |
| ... | ... | @@ -7151,7 +7200,7 @@ fn airAbs(self: *Self, inst: Air.Inst.Index) !void { |
| 7151 | 7200 | break :result dst_mcv; |
| 7152 | 7201 | }, |
| 7153 | 7202 | else => { |
| 7154 | const abi_size: u31 = @intCast(ty.abiSize(mod)); | |
| 7203 | const abi_size: u31 = @intCast(ty.abiSize(pt)); | |
| 7155 | 7204 | const limb_len = math.divCeil(u31, abi_size, 8) catch unreachable; |
| 7156 | 7205 | |
| 7157 | 7206 | const tmp_regs = |
| ... | ... | @@ -7249,9 +7298,9 @@ fn airAbs(self: *Self, inst: Air.Inst.Index) !void { |
| 7249 | 7298 | }, |
| 7250 | 7299 | .Float => return self.floatSign(inst, ty_op.operand, ty), |
| 7251 | 7300 | }, |
| 7252 | }) orelse return self.fail("TODO implement airAbs for {}", .{ty.fmt(mod)}); | |
| 7301 | }) orelse return self.fail("TODO implement airAbs for {}", .{ty.fmt(pt)}); | |
| 7253 | 7302 | |
| 7254 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 7303 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 7255 | 7304 | const src_mcv = try self.resolveInst(ty_op.operand); |
| 7256 | 7305 | const dst_reg = if (src_mcv.isRegister() and self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) |
| 7257 | 7306 | src_mcv.getReg().? |
| ... | ... | @@ -7276,10 +7325,11 @@ fn airAbs(self: *Self, inst: Air.Inst.Index) !void { |
| 7276 | 7325 | } |
| 7277 | 7326 | |
| 7278 | 7327 | fn airSqrt(self: *Self, inst: Air.Inst.Index) !void { |
| 7279 | const mod = self.bin_file.comp.module.?; | |
| 7328 | const pt = self.pt; | |
| 7329 | const mod = pt.zcu; | |
| 7280 | 7330 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 7281 | 7331 | const ty = self.typeOf(un_op); |
| 7282 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 7332 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 7283 | 7333 | |
| 7284 | 7334 | const result: MCValue = result: { |
| 7285 | 7335 | switch (ty.zigTypeTag(mod)) { |
| ... | ... | @@ -7408,7 +7458,7 @@ fn airSqrt(self: *Self, inst: Air.Inst.Index) !void { |
| 7408 | 7458 | }, |
| 7409 | 7459 | else => unreachable, |
| 7410 | 7460 | }) orelse return self.fail("TODO implement airSqrt for {}", .{ |
| 7411 | ty.fmt(mod), | |
| 7461 | ty.fmt(pt), | |
| 7412 | 7462 | }); |
| 7413 | 7463 | switch (mir_tag[0]) { |
| 7414 | 7464 | .v_ss, .v_sd => if (src_mcv.isMemory()) try self.asmRegisterRegisterMemory( |
| ... | ... | @@ -7521,14 +7571,15 @@ fn reuseOperandAdvanced( |
| 7521 | 7571 | } |
| 7522 | 7572 | |
| 7523 | 7573 | fn packedLoad(self: *Self, dst_mcv: MCValue, ptr_ty: Type, ptr_mcv: MCValue) InnerError!void { |
| 7524 | const mod = self.bin_file.comp.module.?; | |
| 7574 | const pt = self.pt; | |
| 7575 | const mod = pt.zcu; | |
| 7525 | 7576 | |
| 7526 | 7577 | const ptr_info = ptr_ty.ptrInfo(mod); |
| 7527 | 7578 | const val_ty = Type.fromInterned(ptr_info.child); |
| 7528 | if (!val_ty.hasRuntimeBitsIgnoreComptime(mod)) return; | |
| 7529 | const val_abi_size: u32 = @intCast(val_ty.abiSize(mod)); | |
| 7579 | if (!val_ty.hasRuntimeBitsIgnoreComptime(pt)) return; | |
| 7580 | const val_abi_size: u32 = @intCast(val_ty.abiSize(pt)); | |
| 7530 | 7581 | |
| 7531 | const val_bit_size: u32 = @intCast(val_ty.bitSize(mod)); | |
| 7582 | const val_bit_size: u32 = @intCast(val_ty.bitSize(pt)); | |
| 7532 | 7583 | const ptr_bit_off = ptr_info.packed_offset.bit_offset + switch (ptr_info.flags.vector_index) { |
| 7533 | 7584 | .none => 0, |
| 7534 | 7585 | .runtime => unreachable, |
| ... | ... | @@ -7566,7 +7617,7 @@ fn packedLoad(self: *Self, dst_mcv: MCValue, ptr_ty: Type, ptr_mcv: MCValue) Inn |
| 7566 | 7617 | return; |
| 7567 | 7618 | } |
| 7568 | 7619 | |
| 7569 | if (val_abi_size > 8) return self.fail("TODO implement packed load of {}", .{val_ty.fmt(mod)}); | |
| 7620 | if (val_abi_size > 8) return self.fail("TODO implement packed load of {}", .{val_ty.fmt(pt)}); | |
| 7570 | 7621 | |
| 7571 | 7622 | const limb_abi_size: u31 = @min(val_abi_size, 8); |
| 7572 | 7623 | const limb_abi_bits = limb_abi_size * 8; |
| ... | ... | @@ -7633,9 +7684,10 @@ fn packedLoad(self: *Self, dst_mcv: MCValue, ptr_ty: Type, ptr_mcv: MCValue) Inn |
| 7633 | 7684 | } |
| 7634 | 7685 | |
| 7635 | 7686 | fn load(self: *Self, dst_mcv: MCValue, ptr_ty: Type, ptr_mcv: MCValue) InnerError!void { |
| 7636 | const mod = self.bin_file.comp.module.?; | |
| 7687 | const pt = self.pt; | |
| 7688 | const mod = pt.zcu; | |
| 7637 | 7689 | const dst_ty = ptr_ty.childType(mod); |
| 7638 | if (!dst_ty.hasRuntimeBitsIgnoreComptime(mod)) return; | |
| 7690 | if (!dst_ty.hasRuntimeBitsIgnoreComptime(pt)) return; | |
| 7639 | 7691 | switch (ptr_mcv) { |
| 7640 | 7692 | .none, |
| 7641 | 7693 | .unreach, |
| ... | ... | @@ -7675,18 +7727,19 @@ fn load(self: *Self, dst_mcv: MCValue, ptr_ty: Type, ptr_mcv: MCValue) InnerErro |
| 7675 | 7727 | } |
| 7676 | 7728 | |
| 7677 | 7729 | fn airLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 7678 | const mod = self.bin_file.comp.module.?; | |
| 7730 | const pt = self.pt; | |
| 7731 | const mod = pt.zcu; | |
| 7679 | 7732 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7680 | 7733 | const elem_ty = self.typeOfIndex(inst); |
| 7681 | 7734 | const result: MCValue = result: { |
| 7682 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result .none; | |
| 7735 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result .none; | |
| 7683 | 7736 | |
| 7684 | 7737 | try self.spillRegisters(&.{ .rdi, .rsi, .rcx }); |
| 7685 | 7738 | const reg_locks = self.register_manager.lockRegsAssumeUnused(3, .{ .rdi, .rsi, .rcx }); |
| 7686 | 7739 | defer for (reg_locks) |lock| self.register_manager.unlockReg(lock); |
| 7687 | 7740 | |
| 7688 | 7741 | const ptr_ty = self.typeOf(ty_op.operand); |
| 7689 | const elem_size = elem_ty.abiSize(mod); | |
| 7742 | const elem_size = elem_ty.abiSize(pt); | |
| 7690 | 7743 | |
| 7691 | 7744 | const elem_rc = self.regClassForType(elem_ty); |
| 7692 | 7745 | const ptr_rc = self.regClassForType(ptr_ty); |
| ... | ... | @@ -7706,7 +7759,7 @@ fn airLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 7706 | 7759 | try self.load(dst_mcv, ptr_ty, ptr_mcv); |
| 7707 | 7760 | } |
| 7708 | 7761 | |
| 7709 | if (elem_ty.isAbiInt(mod) and elem_size * 8 > elem_ty.bitSize(mod)) { | |
| 7762 | if (elem_ty.isAbiInt(mod) and elem_size * 8 > elem_ty.bitSize(pt)) { | |
| 7710 | 7763 | const high_mcv: MCValue = switch (dst_mcv) { |
| 7711 | 7764 | .register => |dst_reg| .{ .register = dst_reg }, |
| 7712 | 7765 | .register_pair => |dst_regs| .{ .register = dst_regs[1] }, |
| ... | ... | @@ -7733,16 +7786,17 @@ fn airLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 7733 | 7786 | } |
| 7734 | 7787 | |
| 7735 | 7788 | fn packedStore(self: *Self, ptr_ty: Type, ptr_mcv: MCValue, src_mcv: MCValue) InnerError!void { |
| 7736 | const mod = self.bin_file.comp.module.?; | |
| 7789 | const pt = self.pt; | |
| 7790 | const mod = pt.zcu; | |
| 7737 | 7791 | const ptr_info = ptr_ty.ptrInfo(mod); |
| 7738 | 7792 | const src_ty = Type.fromInterned(ptr_info.child); |
| 7739 | if (!src_ty.hasRuntimeBitsIgnoreComptime(mod)) return; | |
| 7793 | if (!src_ty.hasRuntimeBitsIgnoreComptime(pt)) return; | |
| 7740 | 7794 | |
| 7741 | 7795 | const limb_abi_size: u16 = @min(ptr_info.packed_offset.host_size, 8); |
| 7742 | 7796 | const limb_abi_bits = limb_abi_size * 8; |
| 7743 | const limb_ty = try mod.intType(.unsigned, limb_abi_bits); | |
| 7797 | const limb_ty = try pt.intType(.unsigned, limb_abi_bits); | |
| 7744 | 7798 | |
| 7745 | const src_bit_size = src_ty.bitSize(mod); | |
| 7799 | const src_bit_size = src_ty.bitSize(pt); | |
| 7746 | 7800 | const ptr_bit_off = ptr_info.packed_offset.bit_offset + switch (ptr_info.flags.vector_index) { |
| 7747 | 7801 | .none => 0, |
| 7748 | 7802 | .runtime => unreachable, |
| ... | ... | @@ -7827,7 +7881,7 @@ fn packedStore(self: *Self, ptr_ty: Type, ptr_mcv: MCValue, src_mcv: MCValue) In |
| 7827 | 7881 | limb_mem, |
| 7828 | 7882 | registerAlias(tmp_reg, limb_abi_size), |
| 7829 | 7883 | ); |
| 7830 | } else return self.fail("TODO: implement packed store of {}", .{src_ty.fmt(mod)}); | |
| 7884 | } else return self.fail("TODO: implement packed store of {}", .{src_ty.fmt(pt)}); | |
| 7831 | 7885 | } |
| 7832 | 7886 | } |
| 7833 | 7887 | |
| ... | ... | @@ -7838,9 +7892,10 @@ fn store( |
| 7838 | 7892 | src_mcv: MCValue, |
| 7839 | 7893 | opts: CopyOptions, |
| 7840 | 7894 | ) InnerError!void { |
| 7841 | const mod = self.bin_file.comp.module.?; | |
| 7895 | const pt = self.pt; | |
| 7896 | const mod = pt.zcu; | |
| 7842 | 7897 | const src_ty = ptr_ty.childType(mod); |
| 7843 | if (!src_ty.hasRuntimeBitsIgnoreComptime(mod)) return; | |
| 7898 | if (!src_ty.hasRuntimeBitsIgnoreComptime(pt)) return; | |
| 7844 | 7899 | switch (ptr_mcv) { |
| 7845 | 7900 | .none, |
| 7846 | 7901 | .unreach, |
| ... | ... | @@ -7880,7 +7935,8 @@ fn store( |
| 7880 | 7935 | } |
| 7881 | 7936 | |
| 7882 | 7937 | fn airStore(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 7883 | const mod = self.bin_file.comp.module.?; | |
| 7938 | const pt = self.pt; | |
| 7939 | const mod = pt.zcu; | |
| 7884 | 7940 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7885 | 7941 | |
| 7886 | 7942 | result: { |
| ... | ... | @@ -7918,15 +7974,16 @@ fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u8) !void { |
| 7918 | 7974 | } |
| 7919 | 7975 | |
| 7920 | 7976 | fn fieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32) !MCValue { |
| 7921 | const mod = self.bin_file.comp.module.?; | |
| 7977 | const pt = self.pt; | |
| 7978 | const mod = pt.zcu; | |
| 7922 | 7979 | const ptr_field_ty = self.typeOfIndex(inst); |
| 7923 | 7980 | const ptr_container_ty = self.typeOf(operand); |
| 7924 | 7981 | const container_ty = ptr_container_ty.childType(mod); |
| 7925 | 7982 | |
| 7926 | 7983 | const field_off: i32 = switch (container_ty.containerLayout(mod)) { |
| 7927 | .auto, .@"extern" => @intCast(container_ty.structFieldOffset(index, mod)), | |
| 7984 | .auto, .@"extern" => @intCast(container_ty.structFieldOffset(index, pt)), | |
| 7928 | 7985 | .@"packed" => @divExact(@as(i32, ptr_container_ty.ptrInfo(mod).packed_offset.bit_offset) + |
| 7929 | (if (mod.typeToStruct(container_ty)) |struct_obj| mod.structPackedFieldBitOffset(struct_obj, index) else 0) - | |
| 7986 | (if (mod.typeToStruct(container_ty)) |struct_obj| pt.structPackedFieldBitOffset(struct_obj, index) else 0) - | |
| 7930 | 7987 | ptr_field_ty.ptrInfo(mod).packed_offset.bit_offset, 8), |
| 7931 | 7988 | }; |
| 7932 | 7989 | |
| ... | ... | @@ -7940,7 +7997,8 @@ fn fieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32 |
| 7940 | 7997 | } |
| 7941 | 7998 | |
| 7942 | 7999 | fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 7943 | const mod = self.bin_file.comp.module.?; | |
| 8000 | const pt = self.pt; | |
| 8001 | const mod = pt.zcu; | |
| 7944 | 8002 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 7945 | 8003 | const extra = self.air.extraData(Air.StructField, ty_pl.payload).data; |
| 7946 | 8004 | const result: MCValue = result: { |
| ... | ... | @@ -7950,14 +8008,14 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 7950 | 8008 | const container_ty = self.typeOf(operand); |
| 7951 | 8009 | const container_rc = self.regClassForType(container_ty); |
| 7952 | 8010 | const field_ty = container_ty.structFieldType(index, mod); |
| 7953 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) break :result .none; | |
| 8011 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) break :result .none; | |
| 7954 | 8012 | const field_rc = self.regClassForType(field_ty); |
| 7955 | 8013 | const field_is_gp = field_rc.supersetOf(abi.RegisterClass.gp); |
| 7956 | 8014 | |
| 7957 | 8015 | const src_mcv = try self.resolveInst(operand); |
| 7958 | 8016 | const field_off: u32 = switch (container_ty.containerLayout(mod)) { |
| 7959 | .auto, .@"extern" => @intCast(container_ty.structFieldOffset(extra.field_index, mod) * 8), | |
| 7960 | .@"packed" => if (mod.typeToStruct(container_ty)) |struct_obj| mod.structPackedFieldBitOffset(struct_obj, extra.field_index) else 0, | |
| 8017 | .auto, .@"extern" => @intCast(container_ty.structFieldOffset(extra.field_index, pt) * 8), | |
| 8018 | .@"packed" => if (mod.typeToStruct(container_ty)) |struct_obj| pt.structPackedFieldBitOffset(struct_obj, extra.field_index) else 0, | |
| 7961 | 8019 | }; |
| 7962 | 8020 | |
| 7963 | 8021 | switch (src_mcv) { |
| ... | ... | @@ -7988,7 +8046,7 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 7988 | 8046 | ); |
| 7989 | 8047 | } |
| 7990 | 8048 | if (abi.RegisterClass.gp.isSet(RegisterManager.indexOfRegIntoTracked(dst_reg).?) and |
| 7991 | container_ty.abiSize(mod) * 8 > field_ty.bitSize(mod)) | |
| 8049 | container_ty.abiSize(pt) * 8 > field_ty.bitSize(pt)) | |
| 7992 | 8050 | try self.truncateRegister(field_ty, dst_reg); |
| 7993 | 8051 | |
| 7994 | 8052 | break :result if (field_off == 0 or field_rc.supersetOf(abi.RegisterClass.gp)) |
| ... | ... | @@ -8000,7 +8058,7 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 8000 | 8058 | const src_regs_lock = self.register_manager.lockRegsAssumeUnused(2, src_regs); |
| 8001 | 8059 | defer for (src_regs_lock) |lock| self.register_manager.unlockReg(lock); |
| 8002 | 8060 | |
| 8003 | const field_bit_size: u32 = @intCast(field_ty.bitSize(mod)); | |
| 8061 | const field_bit_size: u32 = @intCast(field_ty.bitSize(pt)); | |
| 8004 | 8062 | const src_reg = if (field_off + field_bit_size <= 64) |
| 8005 | 8063 | src_regs[0] |
| 8006 | 8064 | else if (field_off >= 64) |
| ... | ... | @@ -8044,7 +8102,7 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 8044 | 8102 | } |
| 8045 | 8103 | |
| 8046 | 8104 | if (field_bit_size < 128) try self.truncateRegister( |
| 8047 | try mod.intType(.unsigned, @intCast(field_bit_size - 64)), | |
| 8105 | try pt.intType(.unsigned, @intCast(field_bit_size - 64)), | |
| 8048 | 8106 | dst_regs[1], |
| 8049 | 8107 | ); |
| 8050 | 8108 | break :result if (field_rc.supersetOf(abi.RegisterClass.gp)) |
| ... | ... | @@ -8099,14 +8157,14 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 8099 | 8157 | } |
| 8100 | 8158 | }, |
| 8101 | 8159 | .load_frame => |frame_addr| { |
| 8102 | const field_abi_size: u32 = @intCast(field_ty.abiSize(mod)); | |
| 8160 | const field_abi_size: u32 = @intCast(field_ty.abiSize(pt)); | |
| 8103 | 8161 | if (field_off % 8 == 0) { |
| 8104 | 8162 | const field_byte_off = @divExact(field_off, 8); |
| 8105 | 8163 | const off_mcv = src_mcv.address().offset(@intCast(field_byte_off)).deref(); |
| 8106 | const field_bit_size = field_ty.bitSize(mod); | |
| 8164 | const field_bit_size = field_ty.bitSize(pt); | |
| 8107 | 8165 | |
| 8108 | 8166 | if (field_abi_size <= 8) { |
| 8109 | const int_ty = try mod.intType( | |
| 8167 | const int_ty = try pt.intType( | |
| 8110 | 8168 | if (field_ty.isAbiInt(mod)) field_ty.intInfo(mod).signedness else .unsigned, |
| 8111 | 8169 | @intCast(field_bit_size), |
| 8112 | 8170 | ); |
| ... | ... | @@ -8127,7 +8185,7 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 8127 | 8185 | try self.copyToRegisterWithInstTracking(inst, field_ty, dst_mcv); |
| 8128 | 8186 | } |
| 8129 | 8187 | |
| 8130 | const container_abi_size: u32 = @intCast(container_ty.abiSize(mod)); | |
| 8188 | const container_abi_size: u32 = @intCast(container_ty.abiSize(pt)); | |
| 8131 | 8189 | const dst_mcv = if (field_byte_off + field_abi_size <= container_abi_size and |
| 8132 | 8190 | self.reuseOperand(inst, operand, 0, src_mcv)) |
| 8133 | 8191 | off_mcv |
| ... | ... | @@ -8228,16 +8286,17 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| 8228 | 8286 | } |
| 8229 | 8287 | |
| 8230 | 8288 | fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 8231 | const mod = self.bin_file.comp.module.?; | |
| 8289 | const pt = self.pt; | |
| 8290 | const mod = pt.zcu; | |
| 8232 | 8291 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 8233 | 8292 | const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| 8234 | 8293 | |
| 8235 | 8294 | const inst_ty = self.typeOfIndex(inst); |
| 8236 | 8295 | const parent_ty = inst_ty.childType(mod); |
| 8237 | 8296 | const field_off: i32 = switch (parent_ty.containerLayout(mod)) { |
| 8238 | .auto, .@"extern" => @intCast(parent_ty.structFieldOffset(extra.field_index, mod)), | |
| 8297 | .auto, .@"extern" => @intCast(parent_ty.structFieldOffset(extra.field_index, pt)), | |
| 8239 | 8298 | .@"packed" => @divExact(@as(i32, inst_ty.ptrInfo(mod).packed_offset.bit_offset) + |
| 8240 | (if (mod.typeToStruct(parent_ty)) |struct_obj| mod.structPackedFieldBitOffset(struct_obj, extra.field_index) else 0) - | |
| 8299 | (if (mod.typeToStruct(parent_ty)) |struct_obj| pt.structPackedFieldBitOffset(struct_obj, extra.field_index) else 0) - | |
| 8241 | 8300 | self.typeOf(extra.field_ptr).ptrInfo(mod).packed_offset.bit_offset, 8), |
| 8242 | 8301 | }; |
| 8243 | 8302 | |
| ... | ... | @@ -8252,10 +8311,11 @@ fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 8252 | 8311 | } |
| 8253 | 8312 | |
| 8254 | 8313 | fn genUnOp(self: *Self, maybe_inst: ?Air.Inst.Index, tag: Air.Inst.Tag, src_air: Air.Inst.Ref) !MCValue { |
| 8255 | const mod = self.bin_file.comp.module.?; | |
| 8314 | const pt = self.pt; | |
| 8315 | const mod = pt.zcu; | |
| 8256 | 8316 | const src_ty = self.typeOf(src_air); |
| 8257 | 8317 | if (src_ty.zigTypeTag(mod) == .Vector) |
| 8258 | return self.fail("TODO implement genUnOp for {}", .{src_ty.fmt(mod)}); | |
| 8318 | return self.fail("TODO implement genUnOp for {}", .{src_ty.fmt(pt)}); | |
| 8259 | 8319 | |
| 8260 | 8320 | var src_mcv = try self.resolveInst(src_air); |
| 8261 | 8321 | switch (src_mcv) { |
| ... | ... | @@ -8290,7 +8350,7 @@ fn genUnOp(self: *Self, maybe_inst: ?Air.Inst.Index, tag: Air.Inst.Tag, src_air: |
| 8290 | 8350 | }; |
| 8291 | 8351 | defer if (dst_lock) |lock| self.register_manager.unlockReg(lock); |
| 8292 | 8352 | |
| 8293 | const abi_size: u16 = @intCast(src_ty.abiSize(mod)); | |
| 8353 | const abi_size: u16 = @intCast(src_ty.abiSize(pt)); | |
| 8294 | 8354 | switch (tag) { |
| 8295 | 8355 | .not => { |
| 8296 | 8356 | const limb_abi_size: u16 = @min(abi_size, 8); |
| ... | ... | @@ -8304,7 +8364,7 @@ fn genUnOp(self: *Self, maybe_inst: ?Air.Inst.Index, tag: Air.Inst.Tag, src_air: |
| 8304 | 8364 | .signed => abi_size * 8, |
| 8305 | 8365 | .unsigned => int_info.bits, |
| 8306 | 8366 | } - byte_off * 8, limb_abi_size * 8)); |
| 8307 | const limb_ty = try mod.intType(int_info.signedness, limb_bits); | |
| 8367 | const limb_ty = try pt.intType(int_info.signedness, limb_bits); | |
| 8308 | 8368 | const limb_mcv = switch (byte_off) { |
| 8309 | 8369 | 0 => dst_mcv, |
| 8310 | 8370 | else => dst_mcv.address().offset(byte_off).deref(), |
| ... | ... | @@ -8340,9 +8400,9 @@ fn genUnOp(self: *Self, maybe_inst: ?Air.Inst.Index, tag: Air.Inst.Tag, src_air: |
| 8340 | 8400 | } |
| 8341 | 8401 | |
| 8342 | 8402 | fn genUnOpMir(self: *Self, mir_tag: Mir.Inst.FixedTag, dst_ty: Type, dst_mcv: MCValue) !void { |
| 8343 | const mod = self.bin_file.comp.module.?; | |
| 8344 | const abi_size: u32 = @intCast(dst_ty.abiSize(mod)); | |
| 8345 | if (abi_size > 8) return self.fail("TODO implement {} for {}", .{ mir_tag, dst_ty.fmt(mod) }); | |
| 8403 | const pt = self.pt; | |
| 8404 | const abi_size: u32 = @intCast(dst_ty.abiSize(pt)); | |
| 8405 | if (abi_size > 8) return self.fail("TODO implement {} for {}", .{ mir_tag, dst_ty.fmt(pt) }); | |
| 8346 | 8406 | switch (dst_mcv) { |
| 8347 | 8407 | .none, |
| 8348 | 8408 | .unreach, |
| ... | ... | @@ -8389,9 +8449,9 @@ fn genShiftBinOpMir( |
| 8389 | 8449 | rhs_ty: Type, |
| 8390 | 8450 | rhs_mcv: MCValue, |
| 8391 | 8451 | ) !void { |
| 8392 | const mod = self.bin_file.comp.module.?; | |
| 8393 | const abi_size: u32 = @intCast(lhs_ty.abiSize(mod)); | |
| 8394 | const shift_abi_size: u32 = @intCast(rhs_ty.abiSize(mod)); | |
| 8452 | const pt = self.pt; | |
| 8453 | const abi_size: u32 = @intCast(lhs_ty.abiSize(pt)); | |
| 8454 | const shift_abi_size: u32 = @intCast(rhs_ty.abiSize(pt)); | |
| 8395 | 8455 | try self.spillEflagsIfOccupied(); |
| 8396 | 8456 | |
| 8397 | 8457 | if (abi_size > 16) { |
| ... | ... | @@ -9046,9 +9106,10 @@ fn genShiftBinOp( |
| 9046 | 9106 | lhs_ty: Type, |
| 9047 | 9107 | rhs_ty: Type, |
| 9048 | 9108 | ) !MCValue { |
| 9049 | const mod = self.bin_file.comp.module.?; | |
| 9109 | const pt = self.pt; | |
| 9110 | const mod = pt.zcu; | |
| 9050 | 9111 | if (lhs_ty.zigTypeTag(mod) == .Vector) return self.fail("TODO implement genShiftBinOp for {}", .{ |
| 9051 | lhs_ty.fmt(mod), | |
| 9112 | lhs_ty.fmt(pt), | |
| 9052 | 9113 | }); |
| 9053 | 9114 | |
| 9054 | 9115 | try self.register_manager.getKnownReg(.rcx, null); |
| ... | ... | @@ -9104,13 +9165,14 @@ fn genMulDivBinOp( |
| 9104 | 9165 | lhs_mcv: MCValue, |
| 9105 | 9166 | rhs_mcv: MCValue, |
| 9106 | 9167 | ) !MCValue { |
| 9107 | const mod = self.bin_file.comp.module.?; | |
| 9168 | const pt = self.pt; | |
| 9169 | const mod = pt.zcu; | |
| 9108 | 9170 | if (dst_ty.zigTypeTag(mod) == .Vector or dst_ty.zigTypeTag(mod) == .Float) return self.fail( |
| 9109 | 9171 | "TODO implement genMulDivBinOp for {s} from {} to {}", |
| 9110 | .{ @tagName(tag), src_ty.fmt(mod), dst_ty.fmt(mod) }, | |
| 9172 | .{ @tagName(tag), src_ty.fmt(pt), dst_ty.fmt(pt) }, | |
| 9111 | 9173 | ); |
| 9112 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(mod)); | |
| 9113 | const src_abi_size: u32 = @intCast(src_ty.abiSize(mod)); | |
| 9174 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(pt)); | |
| 9175 | const src_abi_size: u32 = @intCast(src_ty.abiSize(pt)); | |
| 9114 | 9176 | |
| 9115 | 9177 | assert(self.register_manager.isRegFree(.rax)); |
| 9116 | 9178 | assert(self.register_manager.isRegFree(.rcx)); |
| ... | ... | @@ -9299,13 +9361,13 @@ fn genMulDivBinOp( |
| 9299 | 9361 | .signed => {}, |
| 9300 | 9362 | .unsigned => { |
| 9301 | 9363 | const dst_mcv = try self.allocRegOrMemAdvanced(dst_ty, maybe_inst, false); |
| 9302 | const manyptr_u32_ty = try mod.ptrType(.{ | |
| 9364 | const manyptr_u32_ty = try pt.ptrType(.{ | |
| 9303 | 9365 | .child = .u32_type, |
| 9304 | 9366 | .flags = .{ |
| 9305 | 9367 | .size = .Many, |
| 9306 | 9368 | }, |
| 9307 | 9369 | }); |
| 9308 | const manyptr_const_u32_ty = try mod.ptrType(.{ | |
| 9370 | const manyptr_const_u32_ty = try pt.ptrType(.{ | |
| 9309 | 9371 | .child = .u32_type, |
| 9310 | 9372 | .flags = .{ |
| 9311 | 9373 | .size = .Many, |
| ... | ... | @@ -9348,7 +9410,7 @@ fn genMulDivBinOp( |
| 9348 | 9410 | } |
| 9349 | 9411 | return self.fail( |
| 9350 | 9412 | "TODO implement genMulDivBinOp for {s} from {} to {}", |
| 9351 | .{ @tagName(tag), src_ty.fmt(mod), dst_ty.fmt(mod) }, | |
| 9413 | .{ @tagName(tag), src_ty.fmt(pt), dst_ty.fmt(pt) }, | |
| 9352 | 9414 | ); |
| 9353 | 9415 | } |
| 9354 | 9416 | const ty = if (dst_abi_size <= 8) dst_ty else src_ty; |
| ... | ... | @@ -9515,10 +9577,11 @@ fn genBinOp( |
| 9515 | 9577 | lhs_air: Air.Inst.Ref, |
| 9516 | 9578 | rhs_air: Air.Inst.Ref, |
| 9517 | 9579 | ) !MCValue { |
| 9518 | const mod = self.bin_file.comp.module.?; | |
| 9580 | const pt = self.pt; | |
| 9581 | const mod = pt.zcu; | |
| 9519 | 9582 | const lhs_ty = self.typeOf(lhs_air); |
| 9520 | 9583 | const rhs_ty = self.typeOf(rhs_air); |
| 9521 | const abi_size: u32 = @intCast(lhs_ty.abiSize(mod)); | |
| 9584 | const abi_size: u32 = @intCast(lhs_ty.abiSize(pt)); | |
| 9522 | 9585 | |
| 9523 | 9586 | if (lhs_ty.isRuntimeFloat()) libcall: { |
| 9524 | 9587 | const float_bits = lhs_ty.floatBits(self.target.*); |
| ... | ... | @@ -9556,7 +9619,7 @@ fn genBinOp( |
| 9556 | 9619 | floatLibcAbiSuffix(lhs_ty), |
| 9557 | 9620 | }), |
| 9558 | 9621 | else => return self.fail("TODO implement genBinOp for {s} {}", .{ |
| 9559 | @tagName(air_tag), lhs_ty.fmt(mod), | |
| 9622 | @tagName(air_tag), lhs_ty.fmt(pt), | |
| 9560 | 9623 | }), |
| 9561 | 9624 | } catch unreachable; |
| 9562 | 9625 | const result = try self.genCall(.{ .lib = .{ |
| ... | ... | @@ -9668,7 +9731,7 @@ fn genBinOp( |
| 9668 | 9731 | break :adjusted .{ .register = dst_reg }; |
| 9669 | 9732 | }, |
| 9670 | 9733 | 80, 128 => return self.fail("TODO implement genBinOp for {s} of {}", .{ |
| 9671 | @tagName(air_tag), lhs_ty.fmt(mod), | |
| 9734 | @tagName(air_tag), lhs_ty.fmt(pt), | |
| 9672 | 9735 | }), |
| 9673 | 9736 | else => unreachable, |
| 9674 | 9737 | }; |
| ... | ... | @@ -9700,8 +9763,8 @@ fn genBinOp( |
| 9700 | 9763 | }; |
| 9701 | 9764 | if (sse_op and ((lhs_ty.scalarType(mod).isRuntimeFloat() and |
| 9702 | 9765 | lhs_ty.scalarType(mod).floatBits(self.target.*) == 80) or |
| 9703 | lhs_ty.abiSize(mod) > @as(u6, if (self.hasFeature(.avx)) 32 else 16))) | |
| 9704 | return self.fail("TODO implement genBinOp for {s} {}", .{ @tagName(air_tag), lhs_ty.fmt(mod) }); | |
| 9766 | lhs_ty.abiSize(pt) > @as(u6, if (self.hasFeature(.avx)) 32 else 16))) | |
| 9767 | return self.fail("TODO implement genBinOp for {s} {}", .{ @tagName(air_tag), lhs_ty.fmt(pt) }); | |
| 9705 | 9768 | |
| 9706 | 9769 | const maybe_mask_reg = switch (air_tag) { |
| 9707 | 9770 | else => null, |
| ... | ... | @@ -9857,7 +9920,7 @@ fn genBinOp( |
| 9857 | 9920 | const tmp_lock = self.register_manager.lockRegAssumeUnused(tmp_reg); |
| 9858 | 9921 | defer self.register_manager.unlockReg(tmp_lock); |
| 9859 | 9922 | |
| 9860 | const elem_size = lhs_ty.elemType2(mod).abiSize(mod); | |
| 9923 | const elem_size = lhs_ty.elemType2(mod).abiSize(pt); | |
| 9861 | 9924 | try self.genIntMulComplexOpMir(rhs_ty, tmp_mcv, .{ .immediate = elem_size }); |
| 9862 | 9925 | try self.genBinOpMir( |
| 9863 | 9926 | switch (air_tag) { |
| ... | ... | @@ -10003,7 +10066,7 @@ fn genBinOp( |
| 10003 | 10066 | }, |
| 10004 | 10067 | }; |
| 10005 | 10068 | |
| 10006 | const cmov_abi_size = @max(@as(u32, @intCast(lhs_ty.abiSize(mod))), 2); | |
| 10069 | const cmov_abi_size = @max(@as(u32, @intCast(lhs_ty.abiSize(pt))), 2); | |
| 10007 | 10070 | const tmp_reg = switch (dst_mcv) { |
| 10008 | 10071 | .register => |reg| reg, |
| 10009 | 10072 | else => try self.copyToTmpRegister(lhs_ty, dst_mcv), |
| ... | ... | @@ -10082,7 +10145,7 @@ fn genBinOp( |
| 10082 | 10145 | }, |
| 10083 | 10146 | |
| 10084 | 10147 | else => return self.fail("TODO implement genBinOp for {s} {}", .{ |
| 10085 | @tagName(air_tag), lhs_ty.fmt(mod), | |
| 10148 | @tagName(air_tag), lhs_ty.fmt(pt), | |
| 10086 | 10149 | }), |
| 10087 | 10150 | } |
| 10088 | 10151 | return dst_mcv; |
| ... | ... | @@ -10835,7 +10898,7 @@ fn genBinOp( |
| 10835 | 10898 | }, |
| 10836 | 10899 | }, |
| 10837 | 10900 | }) orelse return self.fail("TODO implement genBinOp for {s} {}", .{ |
| 10838 | @tagName(air_tag), lhs_ty.fmt(mod), | |
| 10901 | @tagName(air_tag), lhs_ty.fmt(pt), | |
| 10839 | 10902 | }); |
| 10840 | 10903 | |
| 10841 | 10904 | const lhs_copy_reg = if (maybe_mask_reg) |_| registerAlias( |
| ... | ... | @@ -10978,7 +11041,7 @@ fn genBinOp( |
| 10978 | 11041 | }, |
| 10979 | 11042 | else => unreachable, |
| 10980 | 11043 | }) orelse return self.fail("TODO implement genBinOp for {s} {}", .{ |
| 10981 | @tagName(air_tag), lhs_ty.fmt(mod), | |
| 11044 | @tagName(air_tag), lhs_ty.fmt(pt), | |
| 10982 | 11045 | }), |
| 10983 | 11046 | mask_reg, |
| 10984 | 11047 | rhs_copy_reg, |
| ... | ... | @@ -11010,7 +11073,7 @@ fn genBinOp( |
| 11010 | 11073 | }, |
| 11011 | 11074 | else => unreachable, |
| 11012 | 11075 | }) orelse return self.fail("TODO implement genBinOp for {s} {}", .{ |
| 11013 | @tagName(air_tag), lhs_ty.fmt(mod), | |
| 11076 | @tagName(air_tag), lhs_ty.fmt(pt), | |
| 11014 | 11077 | }), |
| 11015 | 11078 | dst_reg, |
| 11016 | 11079 | dst_reg, |
| ... | ... | @@ -11046,7 +11109,7 @@ fn genBinOp( |
| 11046 | 11109 | }, |
| 11047 | 11110 | else => unreachable, |
| 11048 | 11111 | }) orelse return self.fail("TODO implement genBinOp for {s} {}", .{ |
| 11049 | @tagName(air_tag), lhs_ty.fmt(mod), | |
| 11112 | @tagName(air_tag), lhs_ty.fmt(pt), | |
| 11050 | 11113 | }), |
| 11051 | 11114 | mask_reg, |
| 11052 | 11115 | mask_reg, |
| ... | ... | @@ -11077,7 +11140,7 @@ fn genBinOp( |
| 11077 | 11140 | }, |
| 11078 | 11141 | else => unreachable, |
| 11079 | 11142 | }) orelse return self.fail("TODO implement genBinOp for {s} {}", .{ |
| 11080 | @tagName(air_tag), lhs_ty.fmt(mod), | |
| 11143 | @tagName(air_tag), lhs_ty.fmt(pt), | |
| 11081 | 11144 | }), |
| 11082 | 11145 | dst_reg, |
| 11083 | 11146 | lhs_copy_reg.?, |
| ... | ... | @@ -11107,7 +11170,7 @@ fn genBinOp( |
| 11107 | 11170 | }, |
| 11108 | 11171 | else => unreachable, |
| 11109 | 11172 | }) orelse return self.fail("TODO implement genBinOp for {s} {}", .{ |
| 11110 | @tagName(air_tag), lhs_ty.fmt(mod), | |
| 11173 | @tagName(air_tag), lhs_ty.fmt(pt), | |
| 11111 | 11174 | }); |
| 11112 | 11175 | try self.asmRegisterRegister(.{ mir_fixes, .@"and" }, dst_reg, mask_reg); |
| 11113 | 11176 | try self.asmRegisterRegister(.{ mir_fixes, .andn }, mask_reg, lhs_copy_reg.?); |
| ... | ... | @@ -11125,8 +11188,8 @@ fn genBinOp( |
| 11125 | 11188 | .cmp_gte, |
| 11126 | 11189 | .cmp_neq, |
| 11127 | 11190 | => { |
| 11128 | const unsigned_ty = try lhs_ty.toUnsigned(mod); | |
| 11129 | const not_mcv = try self.genTypedValue(try unsigned_ty.maxInt(mod, unsigned_ty)); | |
| 11191 | const unsigned_ty = try lhs_ty.toUnsigned(pt); | |
| 11192 | const not_mcv = try self.genTypedValue(try unsigned_ty.maxInt(pt, unsigned_ty)); | |
| 11130 | 11193 | const not_mem: Memory = if (not_mcv.isMemory()) |
| 11131 | 11194 | try not_mcv.mem(self, Memory.Size.fromSize(abi_size)) |
| 11132 | 11195 | else |
| ... | ... | @@ -11195,8 +11258,9 @@ fn genBinOpMir( |
| 11195 | 11258 | dst_mcv: MCValue, |
| 11196 | 11259 | src_mcv: MCValue, |
| 11197 | 11260 | ) !void { |
| 11198 | const mod = self.bin_file.comp.module.?; | |
| 11199 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 11261 | const pt = self.pt; | |
| 11262 | const mod = pt.zcu; | |
| 11263 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 11200 | 11264 | try self.spillEflagsIfOccupied(); |
| 11201 | 11265 | switch (dst_mcv) { |
| 11202 | 11266 | .none, |
| ... | ... | @@ -11358,7 +11422,7 @@ fn genBinOpMir( |
| 11358 | 11422 | .load_got, |
| 11359 | 11423 | .load_tlv, |
| 11360 | 11424 | => { |
| 11361 | const ptr_ty = try mod.singleConstPtrType(ty); | |
| 11425 | const ptr_ty = try pt.singleConstPtrType(ty); | |
| 11362 | 11426 | const addr_reg = try self.copyToTmpRegister(ptr_ty, src_mcv.address()); |
| 11363 | 11427 | return self.genBinOpMir(mir_limb_tag, ty, dst_mcv, .{ |
| 11364 | 11428 | .indirect = .{ .reg = addr_reg, .off = off }, |
| ... | ... | @@ -11619,8 +11683,8 @@ fn genBinOpMir( |
| 11619 | 11683 | /// Performs multi-operand integer multiplication between dst_mcv and src_mcv, storing the result in dst_mcv. |
| 11620 | 11684 | /// Does not support byte-size operands. |
| 11621 | 11685 | fn genIntMulComplexOpMir(self: *Self, dst_ty: Type, dst_mcv: MCValue, src_mcv: MCValue) InnerError!void { |
| 11622 | const mod = self.bin_file.comp.module.?; | |
| 11623 | const abi_size: u32 = @intCast(dst_ty.abiSize(mod)); | |
| 11686 | const pt = self.pt; | |
| 11687 | const abi_size: u32 = @intCast(dst_ty.abiSize(pt)); | |
| 11624 | 11688 | try self.spillEflagsIfOccupied(); |
| 11625 | 11689 | switch (dst_mcv) { |
| 11626 | 11690 | .none, |
| ... | ... | @@ -11746,7 +11810,8 @@ fn genIntMulComplexOpMir(self: *Self, dst_ty: Type, dst_mcv: MCValue, src_mcv: M |
| 11746 | 11810 | } |
| 11747 | 11811 | |
| 11748 | 11812 | fn airArg(self: *Self, inst: Air.Inst.Index) !void { |
| 11749 | const mod = self.bin_file.comp.module.?; | |
| 11813 | const pt = self.pt; | |
| 11814 | const mod = pt.zcu; | |
| 11750 | 11815 | // skip zero-bit arguments as they don't have a corresponding arg instruction |
| 11751 | 11816 | var arg_index = self.arg_index; |
| 11752 | 11817 | while (self.args[arg_index] == .none) arg_index += 1; |
| ... | ... | @@ -11808,7 +11873,7 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void { |
| 11808 | 11873 | try self.genInlineMemset( |
| 11809 | 11874 | dst_mcv.address().offset(@intFromBool(regs_frame_addr.regs > 0)), |
| 11810 | 11875 | .{ .immediate = 0 }, |
| 11811 | .{ .immediate = arg_ty.abiSize(mod) - @intFromBool(regs_frame_addr.regs > 0) }, | |
| 11876 | .{ .immediate = arg_ty.abiSize(pt) - @intFromBool(regs_frame_addr.regs > 0) }, | |
| 11812 | 11877 | .{}, |
| 11813 | 11878 | ); |
| 11814 | 11879 | |
| ... | ... | @@ -11865,7 +11930,8 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void { |
| 11865 | 11930 | } |
| 11866 | 11931 | |
| 11867 | 11932 | fn genArgDbgInfo(self: Self, ty: Type, name: [:0]const u8, mcv: MCValue) !void { |
| 11868 | const mod = self.bin_file.comp.module.?; | |
| 11933 | const pt = self.pt; | |
| 11934 | const mod = pt.zcu; | |
| 11869 | 11935 | switch (self.debug_output) { |
| 11870 | 11936 | .dwarf => |dw| { |
| 11871 | 11937 | const loc: link.File.Dwarf.DeclState.DbgInfoLoc = switch (mcv) { |
| ... | ... | @@ -11901,7 +11967,8 @@ fn genVarDbgInfo( |
| 11901 | 11967 | mcv: MCValue, |
| 11902 | 11968 | name: [:0]const u8, |
| 11903 | 11969 | ) !void { |
| 11904 | const mod = self.bin_file.comp.module.?; | |
| 11970 | const pt = self.pt; | |
| 11971 | const mod = pt.zcu; | |
| 11905 | 11972 | const is_ptr = switch (tag) { |
| 11906 | 11973 | .dbg_var_ptr => true, |
| 11907 | 11974 | .dbg_var_val => false, |
| ... | ... | @@ -12020,7 +12087,8 @@ fn genCall(self: *Self, info: union(enum) { |
| 12020 | 12087 | callee: []const u8, |
| 12021 | 12088 | }, |
| 12022 | 12089 | }, arg_types: []const Type, args: []const MCValue) !MCValue { |
| 12023 | const mod = self.bin_file.comp.module.?; | |
| 12090 | const pt = self.pt; | |
| 12091 | const mod = pt.zcu; | |
| 12024 | 12092 | |
| 12025 | 12093 | const fn_ty = switch (info) { |
| 12026 | 12094 | .air => |callee| fn_info: { |
| ... | ... | @@ -12031,7 +12099,7 @@ fn genCall(self: *Self, info: union(enum) { |
| 12031 | 12099 | else => unreachable, |
| 12032 | 12100 | }; |
| 12033 | 12101 | }, |
| 12034 | .lib => |lib| try mod.funcType(.{ | |
| 12102 | .lib => |lib| try pt.funcType(.{ | |
| 12035 | 12103 | .param_types = lib.param_types, |
| 12036 | 12104 | .return_type = lib.return_type, |
| 12037 | 12105 | .cc = .C, |
| ... | ... | @@ -12101,7 +12169,7 @@ fn genCall(self: *Self, info: union(enum) { |
| 12101 | 12169 | try reg_locks.appendSlice(&self.register_manager.lockRegs(2, regs)); |
| 12102 | 12170 | }, |
| 12103 | 12171 | .indirect => |reg_off| { |
| 12104 | frame_index.* = try self.allocFrameIndex(FrameAlloc.initType(arg_ty, mod)); | |
| 12172 | frame_index.* = try self.allocFrameIndex(FrameAlloc.initType(arg_ty, pt)); | |
| 12105 | 12173 | try self.genSetMem(.{ .frame = frame_index.* }, 0, arg_ty, src_arg, .{}); |
| 12106 | 12174 | try self.register_manager.getReg(reg_off.reg, null); |
| 12107 | 12175 | try reg_locks.append(self.register_manager.lockReg(reg_off.reg)); |
| ... | ... | @@ -12173,7 +12241,7 @@ fn genCall(self: *Self, info: union(enum) { |
| 12173 | 12241 | .none, .unreach => {}, |
| 12174 | 12242 | .indirect => |reg_off| { |
| 12175 | 12243 | const ret_ty = Type.fromInterned(fn_info.return_type); |
| 12176 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(ret_ty, mod)); | |
| 12244 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(ret_ty, pt)); | |
| 12177 | 12245 | try self.genSetReg(reg_off.reg, Type.usize, .{ |
| 12178 | 12246 | .lea_frame = .{ .index = frame_index, .off = -reg_off.off }, |
| 12179 | 12247 | }, .{}); |
| ... | ... | @@ -12188,14 +12256,14 @@ fn genCall(self: *Self, info: union(enum) { |
| 12188 | 12256 | .none, .load_frame => {}, |
| 12189 | 12257 | .register => |dst_reg| switch (fn_info.cc) { |
| 12190 | 12258 | else => try self.genSetReg( |
| 12191 | registerAlias(dst_reg, @intCast(arg_ty.abiSize(mod))), | |
| 12259 | registerAlias(dst_reg, @intCast(arg_ty.abiSize(pt))), | |
| 12192 | 12260 | arg_ty, |
| 12193 | 12261 | src_arg, |
| 12194 | 12262 | .{}, |
| 12195 | 12263 | ), |
| 12196 | 12264 | .C, .SysV, .Win64 => { |
| 12197 | 12265 | const promoted_ty = self.promoteInt(arg_ty); |
| 12198 | const promoted_abi_size: u32 = @intCast(promoted_ty.abiSize(mod)); | |
| 12266 | const promoted_abi_size: u32 = @intCast(promoted_ty.abiSize(pt)); | |
| 12199 | 12267 | const dst_alias = registerAlias(dst_reg, promoted_abi_size); |
| 12200 | 12268 | try self.genSetReg(dst_alias, promoted_ty, src_arg, .{}); |
| 12201 | 12269 | if (promoted_ty.toIntern() != arg_ty.toIntern()) |
| ... | ... | @@ -12246,7 +12314,7 @@ fn genCall(self: *Self, info: union(enum) { |
| 12246 | 12314 | // Due to incremental compilation, how function calls are generated depends |
| 12247 | 12315 | // on linking. |
| 12248 | 12316 | switch (info) { |
| 12249 | .air => |callee| if (try self.air.value(callee, mod)) |func_value| { | |
| 12317 | .air => |callee| if (try self.air.value(callee, pt)) |func_value| { | |
| 12250 | 12318 | const func_key = mod.intern_pool.indexToKey(func_value.ip_index); |
| 12251 | 12319 | switch (switch (func_key) { |
| 12252 | 12320 | else => func_key, |
| ... | ... | @@ -12332,7 +12400,8 @@ fn genCall(self: *Self, info: union(enum) { |
| 12332 | 12400 | } |
| 12333 | 12401 | |
| 12334 | 12402 | fn airRet(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 12335 | const mod = self.bin_file.comp.module.?; | |
| 12403 | const pt = self.pt; | |
| 12404 | const mod = pt.zcu; | |
| 12336 | 12405 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 12337 | 12406 | |
| 12338 | 12407 | const ret_ty = self.fn_type.fnReturnType(mod); |
| ... | ... | @@ -12387,7 +12456,8 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) !void { |
| 12387 | 12456 | } |
| 12388 | 12457 | |
| 12389 | 12458 | fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { |
| 12390 | const mod = self.bin_file.comp.module.?; | |
| 12459 | const pt = self.pt; | |
| 12460 | const mod = pt.zcu; | |
| 12391 | 12461 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 12392 | 12462 | var ty = self.typeOf(bin_op.lhs); |
| 12393 | 12463 | var null_compare: ?Mir.Inst.Index = null; |
| ... | ... | @@ -12457,9 +12527,9 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { |
| 12457 | 12527 | }, |
| 12458 | 12528 | .Optional => if (!ty.optionalReprIsPayload(mod)) { |
| 12459 | 12529 | const opt_ty = ty; |
| 12460 | const opt_abi_size: u31 = @intCast(opt_ty.abiSize(mod)); | |
| 12530 | const opt_abi_size: u31 = @intCast(opt_ty.abiSize(pt)); | |
| 12461 | 12531 | ty = opt_ty.optionalChild(mod); |
| 12462 | const payload_abi_size: u31 = @intCast(ty.abiSize(mod)); | |
| 12532 | const payload_abi_size: u31 = @intCast(ty.abiSize(pt)); | |
| 12463 | 12533 | |
| 12464 | 12534 | const temp_lhs_reg = try self.register_manager.allocReg(null, abi.RegisterClass.gp); |
| 12465 | 12535 | const temp_lhs_lock = self.register_manager.lockRegAssumeUnused(temp_lhs_reg); |
| ... | ... | @@ -12518,7 +12588,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { |
| 12518 | 12588 | |
| 12519 | 12589 | switch (ty.zigTypeTag(mod)) { |
| 12520 | 12590 | else => { |
| 12521 | const abi_size: u16 = @intCast(ty.abiSize(mod)); | |
| 12591 | const abi_size: u16 = @intCast(ty.abiSize(pt)); | |
| 12522 | 12592 | const may_flip: enum { |
| 12523 | 12593 | may_flip, |
| 12524 | 12594 | must_flip, |
| ... | ... | @@ -12845,7 +12915,8 @@ fn airCmpVector(self: *Self, inst: Air.Inst.Index) !void { |
| 12845 | 12915 | } |
| 12846 | 12916 | |
| 12847 | 12917 | fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) !void { |
| 12848 | const mod = self.bin_file.comp.module.?; | |
| 12918 | const pt = self.pt; | |
| 12919 | const mod = pt.zcu; | |
| 12849 | 12920 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 12850 | 12921 | |
| 12851 | 12922 | const addr_reg = try self.register_manager.allocReg(null, abi.RegisterClass.gp); |
| ... | ... | @@ -12856,7 +12927,7 @@ fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) !void { |
| 12856 | 12927 | try self.spillEflagsIfOccupied(); |
| 12857 | 12928 | |
| 12858 | 12929 | const op_ty = self.typeOf(un_op); |
| 12859 | const op_abi_size: u32 = @intCast(op_ty.abiSize(mod)); | |
| 12930 | const op_abi_size: u32 = @intCast(op_ty.abiSize(pt)); | |
| 12860 | 12931 | const op_mcv = try self.resolveInst(un_op); |
| 12861 | 12932 | const dst_reg = switch (op_mcv) { |
| 12862 | 12933 | .register => |reg| reg, |
| ... | ... | @@ -12987,8 +13058,8 @@ fn airDbgVar(self: *Self, inst: Air.Inst.Index) !void { |
| 12987 | 13058 | } |
| 12988 | 13059 | |
| 12989 | 13060 | fn genCondBrMir(self: *Self, ty: Type, mcv: MCValue) !Mir.Inst.Index { |
| 12990 | const mod = self.bin_file.comp.module.?; | |
| 12991 | const abi_size = ty.abiSize(mod); | |
| 13061 | const pt = self.pt; | |
| 13062 | const abi_size = ty.abiSize(pt); | |
| 12992 | 13063 | switch (mcv) { |
| 12993 | 13064 | .eflags => |cc| { |
| 12994 | 13065 | // Here we map the opposites since the jump is to the false branch. |
| ... | ... | @@ -13060,7 +13131,8 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void { |
| 13060 | 13131 | } |
| 13061 | 13132 | |
| 13062 | 13133 | fn isNull(self: *Self, inst: Air.Inst.Index, opt_ty: Type, opt_mcv: MCValue) !MCValue { |
| 13063 | const mod = self.bin_file.comp.module.?; | |
| 13134 | const pt = self.pt; | |
| 13135 | const mod = pt.zcu; | |
| 13064 | 13136 | switch (opt_mcv) { |
| 13065 | 13137 | .register_overflow => |ro| return .{ .eflags = ro.eflags.negate() }, |
| 13066 | 13138 | else => {}, |
| ... | ... | @@ -13073,7 +13145,7 @@ fn isNull(self: *Self, inst: Air.Inst.Index, opt_ty: Type, opt_mcv: MCValue) !MC |
| 13073 | 13145 | const some_info: struct { off: i32, ty: Type } = if (opt_ty.optionalReprIsPayload(mod)) |
| 13074 | 13146 | .{ .off = 0, .ty = if (pl_ty.isSlice(mod)) pl_ty.slicePtrFieldType(mod) else pl_ty } |
| 13075 | 13147 | else |
| 13076 | .{ .off = @intCast(pl_ty.abiSize(mod)), .ty = Type.bool }; | |
| 13148 | .{ .off = @intCast(pl_ty.abiSize(pt)), .ty = Type.bool }; | |
| 13077 | 13149 | |
| 13078 | 13150 | self.eflags_inst = inst; |
| 13079 | 13151 | switch (opt_mcv) { |
| ... | ... | @@ -13098,14 +13170,14 @@ fn isNull(self: *Self, inst: Air.Inst.Index, opt_ty: Type, opt_mcv: MCValue) !MC |
| 13098 | 13170 | |
| 13099 | 13171 | .register => |opt_reg| { |
| 13100 | 13172 | if (some_info.off == 0) { |
| 13101 | const some_abi_size: u32 = @intCast(some_info.ty.abiSize(mod)); | |
| 13173 | const some_abi_size: u32 = @intCast(some_info.ty.abiSize(pt)); | |
| 13102 | 13174 | const alias_reg = registerAlias(opt_reg, some_abi_size); |
| 13103 | 13175 | assert(some_abi_size * 8 == alias_reg.bitSize()); |
| 13104 | 13176 | try self.asmRegisterRegister(.{ ._, .@"test" }, alias_reg, alias_reg); |
| 13105 | 13177 | return .{ .eflags = .z }; |
| 13106 | 13178 | } |
| 13107 | 13179 | assert(some_info.ty.ip_index == .bool_type); |
| 13108 | const opt_abi_size: u32 = @intCast(opt_ty.abiSize(mod)); | |
| 13180 | const opt_abi_size: u32 = @intCast(opt_ty.abiSize(pt)); | |
| 13109 | 13181 | try self.asmRegisterImmediate( |
| 13110 | 13182 | .{ ._, .bt }, |
| 13111 | 13183 | registerAlias(opt_reg, opt_abi_size), |
| ... | ... | @@ -13125,7 +13197,7 @@ fn isNull(self: *Self, inst: Air.Inst.Index, opt_ty: Type, opt_mcv: MCValue) !MC |
| 13125 | 13197 | defer self.register_manager.unlockReg(addr_reg_lock); |
| 13126 | 13198 | |
| 13127 | 13199 | try self.genSetReg(addr_reg, Type.usize, opt_mcv.address(), .{}); |
| 13128 | const some_abi_size: u32 = @intCast(some_info.ty.abiSize(mod)); | |
| 13200 | const some_abi_size: u32 = @intCast(some_info.ty.abiSize(pt)); | |
| 13129 | 13201 | try self.asmMemoryImmediate( |
| 13130 | 13202 | .{ ._, .cmp }, |
| 13131 | 13203 | .{ |
| ... | ... | @@ -13141,7 +13213,7 @@ fn isNull(self: *Self, inst: Air.Inst.Index, opt_ty: Type, opt_mcv: MCValue) !MC |
| 13141 | 13213 | }, |
| 13142 | 13214 | |
| 13143 | 13215 | .indirect, .load_frame => { |
| 13144 | const some_abi_size: u32 = @intCast(some_info.ty.abiSize(mod)); | |
| 13216 | const some_abi_size: u32 = @intCast(some_info.ty.abiSize(pt)); | |
| 13145 | 13217 | try self.asmMemoryImmediate( |
| 13146 | 13218 | .{ ._, .cmp }, |
| 13147 | 13219 | switch (opt_mcv) { |
| ... | ... | @@ -13169,7 +13241,8 @@ fn isNull(self: *Self, inst: Air.Inst.Index, opt_ty: Type, opt_mcv: MCValue) !MC |
| 13169 | 13241 | } |
| 13170 | 13242 | |
| 13171 | 13243 | fn isNullPtr(self: *Self, inst: Air.Inst.Index, ptr_ty: Type, ptr_mcv: MCValue) !MCValue { |
| 13172 | const mod = self.bin_file.comp.module.?; | |
| 13244 | const pt = self.pt; | |
| 13245 | const mod = pt.zcu; | |
| 13173 | 13246 | const opt_ty = ptr_ty.childType(mod); |
| 13174 | 13247 | const pl_ty = opt_ty.optionalChild(mod); |
| 13175 | 13248 | |
| ... | ... | @@ -13178,7 +13251,7 @@ fn isNullPtr(self: *Self, inst: Air.Inst.Index, ptr_ty: Type, ptr_mcv: MCValue) |
| 13178 | 13251 | const some_info: struct { off: i32, ty: Type } = if (opt_ty.optionalReprIsPayload(mod)) |
| 13179 | 13252 | .{ .off = 0, .ty = if (pl_ty.isSlice(mod)) pl_ty.slicePtrFieldType(mod) else pl_ty } |
| 13180 | 13253 | else |
| 13181 | .{ .off = @intCast(pl_ty.abiSize(mod)), .ty = Type.bool }; | |
| 13254 | .{ .off = @intCast(pl_ty.abiSize(pt)), .ty = Type.bool }; | |
| 13182 | 13255 | |
| 13183 | 13256 | const ptr_reg = switch (ptr_mcv) { |
| 13184 | 13257 | .register => |reg| reg, |
| ... | ... | @@ -13187,7 +13260,7 @@ fn isNullPtr(self: *Self, inst: Air.Inst.Index, ptr_ty: Type, ptr_mcv: MCValue) |
| 13187 | 13260 | const ptr_lock = self.register_manager.lockReg(ptr_reg); |
| 13188 | 13261 | defer if (ptr_lock) |lock| self.register_manager.unlockReg(lock); |
| 13189 | 13262 | |
| 13190 | const some_abi_size: u32 = @intCast(some_info.ty.abiSize(mod)); | |
| 13263 | const some_abi_size: u32 = @intCast(some_info.ty.abiSize(pt)); | |
| 13191 | 13264 | try self.asmMemoryImmediate( |
| 13192 | 13265 | .{ ._, .cmp }, |
| 13193 | 13266 | .{ |
| ... | ... | @@ -13205,13 +13278,14 @@ fn isNullPtr(self: *Self, inst: Air.Inst.Index, ptr_ty: Type, ptr_mcv: MCValue) |
| 13205 | 13278 | } |
| 13206 | 13279 | |
| 13207 | 13280 | fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) !MCValue { |
| 13208 | const mod = self.bin_file.comp.module.?; | |
| 13281 | const pt = self.pt; | |
| 13282 | const mod = pt.zcu; | |
| 13209 | 13283 | const err_ty = eu_ty.errorUnionSet(mod); |
| 13210 | 13284 | if (err_ty.errorSetIsEmpty(mod)) return MCValue{ .immediate = 0 }; // always false |
| 13211 | 13285 | |
| 13212 | 13286 | try self.spillEflagsIfOccupied(); |
| 13213 | 13287 | |
| 13214 | const err_off: u31 = @intCast(errUnionErrorOffset(eu_ty.errorUnionPayload(mod), mod)); | |
| 13288 | const err_off: u31 = @intCast(errUnionErrorOffset(eu_ty.errorUnionPayload(mod), pt)); | |
| 13215 | 13289 | switch (eu_mcv) { |
| 13216 | 13290 | .register => |reg| { |
| 13217 | 13291 | const eu_lock = self.register_manager.lockReg(reg); |
| ... | ... | @@ -13253,7 +13327,8 @@ fn isErr(self: *Self, maybe_inst: ?Air.Inst.Index, eu_ty: Type, eu_mcv: MCValue) |
| 13253 | 13327 | } |
| 13254 | 13328 | |
| 13255 | 13329 | fn isErrPtr(self: *Self, maybe_inst: ?Air.Inst.Index, ptr_ty: Type, ptr_mcv: MCValue) !MCValue { |
| 13256 | const mod = self.bin_file.comp.module.?; | |
| 13330 | const pt = self.pt; | |
| 13331 | const mod = pt.zcu; | |
| 13257 | 13332 | const eu_ty = ptr_ty.childType(mod); |
| 13258 | 13333 | const err_ty = eu_ty.errorUnionSet(mod); |
| 13259 | 13334 | if (err_ty.errorSetIsEmpty(mod)) return MCValue{ .immediate = 0 }; // always false |
| ... | ... | @@ -13267,7 +13342,7 @@ fn isErrPtr(self: *Self, maybe_inst: ?Air.Inst.Index, ptr_ty: Type, ptr_mcv: MCV |
| 13267 | 13342 | const ptr_lock = self.register_manager.lockReg(ptr_reg); |
| 13268 | 13343 | defer if (ptr_lock) |lock| self.register_manager.unlockReg(lock); |
| 13269 | 13344 | |
| 13270 | const err_off: u31 = @intCast(errUnionErrorOffset(eu_ty.errorUnionPayload(mod), mod)); | |
| 13345 | const err_off: u31 = @intCast(errUnionErrorOffset(eu_ty.errorUnionPayload(mod), pt)); | |
| 13271 | 13346 | try self.asmMemoryImmediate( |
| 13272 | 13347 | .{ ._, .cmp }, |
| 13273 | 13348 | .{ |
| ... | ... | @@ -13539,12 +13614,12 @@ fn performReloc(self: *Self, reloc: Mir.Inst.Index) void { |
| 13539 | 13614 | } |
| 13540 | 13615 | |
| 13541 | 13616 | fn airBr(self: *Self, inst: Air.Inst.Index) !void { |
| 13542 | const mod = self.bin_file.comp.module.?; | |
| 13617 | const pt = self.pt; | |
| 13543 | 13618 | const br = self.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 13544 | 13619 | |
| 13545 | 13620 | const block_ty = self.typeOfIndex(br.block_inst); |
| 13546 | 13621 | const block_unused = |
| 13547 | !block_ty.hasRuntimeBitsIgnoreComptime(mod) or self.liveness.isUnused(br.block_inst); | |
| 13622 | !block_ty.hasRuntimeBitsIgnoreComptime(pt) or self.liveness.isUnused(br.block_inst); | |
| 13548 | 13623 | const block_tracking = self.inst_tracking.getPtr(br.block_inst).?; |
| 13549 | 13624 | const block_data = self.blocks.getPtr(br.block_inst).?; |
| 13550 | 13625 | const first_br = block_data.relocs.items.len == 0; |
| ... | ... | @@ -13600,7 +13675,8 @@ fn airBr(self: *Self, inst: Air.Inst.Index) !void { |
| 13600 | 13675 | } |
| 13601 | 13676 | |
| 13602 | 13677 | fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 13603 | const mod = self.bin_file.comp.module.?; | |
| 13678 | const pt = self.pt; | |
| 13679 | const mod = pt.zcu; | |
| 13604 | 13680 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 13605 | 13681 | const extra = self.air.extraData(Air.Asm, ty_pl.payload); |
| 13606 | 13682 | const clobbers_len: u31 = @truncate(extra.data.flags); |
| ... | ... | @@ -13664,7 +13740,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 13664 | 13740 | 'x' => abi.RegisterClass.sse, |
| 13665 | 13741 | else => unreachable, |
| 13666 | 13742 | }) orelse return self.fail("ran out of registers lowering inline asm", .{}), |
| 13667 | @intCast(ty.abiSize(mod)), | |
| 13743 | @intCast(ty.abiSize(pt)), | |
| 13668 | 13744 | ) |
| 13669 | 13745 | else if (mem.eql(u8, rest, "m")) |
| 13670 | 13746 | if (output != .none) null else return self.fail( |
| ... | ... | @@ -13734,7 +13810,7 @@ fn airAsm(self: *Self, inst: Air.Inst.Index) !void { |
| 13734 | 13810 | break :arg input_mcv; |
| 13735 | 13811 | const reg = try self.register_manager.allocReg(null, rc); |
| 13736 | 13812 | try self.genSetReg(reg, ty, input_mcv, .{}); |
| 13737 | break :arg .{ .register = registerAlias(reg, @intCast(ty.abiSize(mod))) }; | |
| 13813 | break :arg .{ .register = registerAlias(reg, @intCast(ty.abiSize(pt))) }; | |
| 13738 | 13814 | } else if (mem.eql(u8, constraint, "i") or mem.eql(u8, constraint, "n")) |
| 13739 | 13815 | switch (input_mcv) { |
| 13740 | 13816 | .immediate => |imm| .{ .immediate = imm }, |
| ... | ... | @@ -14310,18 +14386,19 @@ const MoveStrategy = union(enum) { |
| 14310 | 14386 | } |
| 14311 | 14387 | }; |
| 14312 | 14388 | fn moveStrategy(self: *Self, ty: Type, class: Register.Class, aligned: bool) !MoveStrategy { |
| 14313 | const mod = self.bin_file.comp.module.?; | |
| 14389 | const pt = self.pt; | |
| 14390 | const mod = pt.zcu; | |
| 14314 | 14391 | switch (class) { |
| 14315 | 14392 | .general_purpose, .segment => return .{ .move = .{ ._, .mov } }, |
| 14316 | 14393 | .x87 => return .x87_load_store, |
| 14317 | 14394 | .mmx => {}, |
| 14318 | 14395 | .sse => switch (ty.zigTypeTag(mod)) { |
| 14319 | 14396 | else => { |
| 14320 | const classes = mem.sliceTo(&abi.classifySystemV(ty, mod, self.target.*, .other), .none); | |
| 14397 | const classes = mem.sliceTo(&abi.classifySystemV(ty, pt, self.target.*, .other), .none); | |
| 14321 | 14398 | assert(std.mem.indexOfNone(abi.Class, classes, &.{ |
| 14322 | 14399 | .integer, .sse, .sseup, .memory, .float, .float_combine, |
| 14323 | 14400 | }) == null); |
| 14324 | const abi_size = ty.abiSize(mod); | |
| 14401 | const abi_size = ty.abiSize(pt); | |
| 14325 | 14402 | if (abi_size < 4 or |
| 14326 | 14403 | std.mem.indexOfScalar(abi.Class, classes, .integer) != null) switch (abi_size) { |
| 14327 | 14404 | 1 => if (self.hasFeature(.avx)) return .{ .vex_insert_extract = .{ |
| ... | ... | @@ -14532,7 +14609,7 @@ fn moveStrategy(self: *Self, ty: Type, class: Register.Class, aligned: bool) !Mo |
| 14532 | 14609 | }, |
| 14533 | 14610 | .ip => {}, |
| 14534 | 14611 | } |
| 14535 | return self.fail("TODO moveStrategy for {}", .{ty.fmt(mod)}); | |
| 14612 | return self.fail("TODO moveStrategy for {}", .{ty.fmt(pt)}); | |
| 14536 | 14613 | } |
| 14537 | 14614 | |
| 14538 | 14615 | const CopyOptions = struct { |
| ... | ... | @@ -14540,7 +14617,7 @@ const CopyOptions = struct { |
| 14540 | 14617 | }; |
| 14541 | 14618 | |
| 14542 | 14619 | fn genCopy(self: *Self, ty: Type, dst_mcv: MCValue, src_mcv: MCValue, opts: CopyOptions) InnerError!void { |
| 14543 | const mod = self.bin_file.comp.module.?; | |
| 14620 | const pt = self.pt; | |
| 14544 | 14621 | |
| 14545 | 14622 | const src_lock = if (src_mcv.getReg()) |reg| self.register_manager.lockReg(reg) else null; |
| 14546 | 14623 | defer if (src_lock) |lock| self.register_manager.unlockReg(lock); |
| ... | ... | @@ -14601,7 +14678,7 @@ fn genCopy(self: *Self, ty: Type, dst_mcv: MCValue, src_mcv: MCValue, opts: Copy |
| 14601 | 14678 | opts, |
| 14602 | 14679 | ), |
| 14603 | 14680 | else => return self.fail("TODO implement genCopy for {s} of {}", .{ |
| 14604 | @tagName(src_mcv), ty.fmt(mod), | |
| 14681 | @tagName(src_mcv), ty.fmt(pt), | |
| 14605 | 14682 | }), |
| 14606 | 14683 | }; |
| 14607 | 14684 | defer if (src_info) |info| self.register_manager.unlockReg(info.addr_lock); |
| ... | ... | @@ -14617,7 +14694,7 @@ fn genCopy(self: *Self, ty: Type, dst_mcv: MCValue, src_mcv: MCValue, opts: Copy |
| 14617 | 14694 | } }, |
| 14618 | 14695 | else => unreachable, |
| 14619 | 14696 | }, opts); |
| 14620 | part_disp += @intCast(dst_ty.abiSize(mod)); | |
| 14697 | part_disp += @intCast(dst_ty.abiSize(pt)); | |
| 14621 | 14698 | } |
| 14622 | 14699 | }, |
| 14623 | 14700 | .indirect => |reg_off| try self.genSetMem( |
| ... | ... | @@ -14658,9 +14735,10 @@ fn genSetReg( |
| 14658 | 14735 | src_mcv: MCValue, |
| 14659 | 14736 | opts: CopyOptions, |
| 14660 | 14737 | ) InnerError!void { |
| 14661 | const mod = self.bin_file.comp.module.?; | |
| 14662 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 14663 | if (ty.bitSize(mod) > dst_reg.bitSize()) | |
| 14738 | const pt = self.pt; | |
| 14739 | const mod = pt.zcu; | |
| 14740 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 14741 | if (ty.bitSize(pt) > dst_reg.bitSize()) | |
| 14664 | 14742 | return self.fail("genSetReg called with a value larger than dst_reg", .{}); |
| 14665 | 14743 | switch (src_mcv) { |
| 14666 | 14744 | .none, |
| ... | ... | @@ -14686,7 +14764,7 @@ fn genSetReg( |
| 14686 | 14764 | ), |
| 14687 | 14765 | else => unreachable, |
| 14688 | 14766 | }, |
| 14689 | .segment, .x87, .mmx, .sse => try self.genSetReg(dst_reg, ty, try self.genTypedValue(try mod.undefValue(ty)), opts), | |
| 14767 | .segment, .x87, .mmx, .sse => try self.genSetReg(dst_reg, ty, try self.genTypedValue(try pt.undefValue(ty)), opts), | |
| 14690 | 14768 | .ip => unreachable, |
| 14691 | 14769 | }, |
| 14692 | 14770 | .eflags => |cc| try self.asmSetccRegister(cc, dst_reg.to8()), |
| ... | ... | @@ -14797,7 +14875,7 @@ fn genSetReg( |
| 14797 | 14875 | 80 => null, |
| 14798 | 14876 | else => unreachable, |
| 14799 | 14877 | }, |
| 14800 | }) orelse return self.fail("TODO implement genSetReg for {}", .{ty.fmt(mod)}), | |
| 14878 | }) orelse return self.fail("TODO implement genSetReg for {}", .{ty.fmt(pt)}), | |
| 14801 | 14879 | registerAlias(dst_reg, abi_size), |
| 14802 | 14880 | registerAlias(src_reg, abi_size), |
| 14803 | 14881 | ), |
| ... | ... | @@ -14847,7 +14925,7 @@ fn genSetReg( |
| 14847 | 14925 | return (try self.moveStrategy( |
| 14848 | 14926 | ty, |
| 14849 | 14927 | dst_reg.class(), |
| 14850 | ty.abiAlignment(mod).check(@as(u32, @bitCast(small_addr))), | |
| 14928 | ty.abiAlignment(pt).check(@as(u32, @bitCast(small_addr))), | |
| 14851 | 14929 | )).read(self, registerAlias(dst_reg, abi_size), .{ |
| 14852 | 14930 | .base = .{ .reg = .ds }, |
| 14853 | 14931 | .mod = .{ .rm = .{ |
| ... | ... | @@ -14967,8 +15045,9 @@ fn genSetMem( |
| 14967 | 15045 | src_mcv: MCValue, |
| 14968 | 15046 | opts: CopyOptions, |
| 14969 | 15047 | ) InnerError!void { |
| 14970 | const mod = self.bin_file.comp.module.?; | |
| 14971 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 15048 | const pt = self.pt; | |
| 15049 | const mod = pt.zcu; | |
| 15050 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 14972 | 15051 | const dst_ptr_mcv: MCValue = switch (base) { |
| 14973 | 15052 | .none => .{ .immediate = @bitCast(@as(i64, disp)) }, |
| 14974 | 15053 | .reg => |base_reg| .{ .register_offset = .{ .reg = base_reg, .off = disp } }, |
| ... | ... | @@ -15094,21 +15173,21 @@ fn genSetMem( |
| 15094 | 15173 | var part_disp: i32 = disp; |
| 15095 | 15174 | for (try self.splitType(ty), src_regs) |src_ty, src_reg| { |
| 15096 | 15175 | try self.genSetMem(base, part_disp, src_ty, .{ .register = src_reg }, opts); |
| 15097 | part_disp += @intCast(src_ty.abiSize(mod)); | |
| 15176 | part_disp += @intCast(src_ty.abiSize(pt)); | |
| 15098 | 15177 | } |
| 15099 | 15178 | }, |
| 15100 | 15179 | .register_overflow => |ro| switch (ty.zigTypeTag(mod)) { |
| 15101 | 15180 | .Struct => { |
| 15102 | 15181 | try self.genSetMem( |
| 15103 | 15182 | base, |
| 15104 | disp + @as(i32, @intCast(ty.structFieldOffset(0, mod))), | |
| 15183 | disp + @as(i32, @intCast(ty.structFieldOffset(0, pt))), | |
| 15105 | 15184 | ty.structFieldType(0, mod), |
| 15106 | 15185 | .{ .register = ro.reg }, |
| 15107 | 15186 | opts, |
| 15108 | 15187 | ); |
| 15109 | 15188 | try self.genSetMem( |
| 15110 | 15189 | base, |
| 15111 | disp + @as(i32, @intCast(ty.structFieldOffset(1, mod))), | |
| 15190 | disp + @as(i32, @intCast(ty.structFieldOffset(1, pt))), | |
| 15112 | 15191 | ty.structFieldType(1, mod), |
| 15113 | 15192 | .{ .eflags = ro.eflags }, |
| 15114 | 15193 | opts, |
| ... | ... | @@ -15120,14 +15199,14 @@ fn genSetMem( |
| 15120 | 15199 | try self.genSetMem(base, disp, child_ty, .{ .register = ro.reg }, opts); |
| 15121 | 15200 | try self.genSetMem( |
| 15122 | 15201 | base, |
| 15123 | disp + @as(i32, @intCast(child_ty.abiSize(mod))), | |
| 15202 | disp + @as(i32, @intCast(child_ty.abiSize(pt))), | |
| 15124 | 15203 | Type.bool, |
| 15125 | 15204 | .{ .eflags = ro.eflags }, |
| 15126 | 15205 | opts, |
| 15127 | 15206 | ); |
| 15128 | 15207 | }, |
| 15129 | 15208 | else => return self.fail("TODO implement genSetMem for {s} of {}", .{ |
| 15130 | @tagName(src_mcv), ty.fmt(mod), | |
| 15209 | @tagName(src_mcv), ty.fmt(pt), | |
| 15131 | 15210 | }), |
| 15132 | 15211 | }, |
| 15133 | 15212 | .register_offset, |
| ... | ... | @@ -15236,8 +15315,9 @@ fn genLazySymbolRef( |
| 15236 | 15315 | reg: Register, |
| 15237 | 15316 | lazy_sym: link.File.LazySymbol, |
| 15238 | 15317 | ) InnerError!void { |
| 15318 | const pt = self.pt; | |
| 15239 | 15319 | if (self.bin_file.cast(link.File.Elf)) |elf_file| { |
| 15240 | const sym_index = elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, lazy_sym) catch |err| | |
| 15320 | const sym_index = elf_file.zigObjectPtr().?.getOrCreateMetadataForLazySymbol(elf_file, pt, lazy_sym) catch |err| | |
| 15241 | 15321 | return self.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 15242 | 15322 | const sym = elf_file.symbol(sym_index); |
| 15243 | 15323 | if (self.mod.pic) { |
| ... | ... | @@ -15273,7 +15353,7 @@ fn genLazySymbolRef( |
| 15273 | 15353 | } |
| 15274 | 15354 | } |
| 15275 | 15355 | } else if (self.bin_file.cast(link.File.Plan9)) |p9_file| { |
| 15276 | const atom_index = p9_file.getOrCreateAtomForLazySymbol(lazy_sym) catch |err| | |
| 15356 | const atom_index = p9_file.getOrCreateAtomForLazySymbol(pt, lazy_sym) catch |err| | |
| 15277 | 15357 | return self.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 15278 | 15358 | var atom = p9_file.getAtom(atom_index); |
| 15279 | 15359 | _ = atom.getOrCreateOffsetTableEntry(p9_file); |
| ... | ... | @@ -15300,7 +15380,7 @@ fn genLazySymbolRef( |
| 15300 | 15380 | else => unreachable, |
| 15301 | 15381 | } |
| 15302 | 15382 | } else if (self.bin_file.cast(link.File.Coff)) |coff_file| { |
| 15303 | const atom_index = coff_file.getOrCreateAtomForLazySymbol(lazy_sym) catch |err| | |
| 15383 | const atom_index = coff_file.getOrCreateAtomForLazySymbol(pt, lazy_sym) catch |err| | |
| 15304 | 15384 | return self.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 15305 | 15385 | const sym_index = coff_file.getAtom(atom_index).getSymbolIndex().?; |
| 15306 | 15386 | switch (tag) { |
| ... | ... | @@ -15314,7 +15394,7 @@ fn genLazySymbolRef( |
| 15314 | 15394 | else => unreachable, |
| 15315 | 15395 | } |
| 15316 | 15396 | } else if (self.bin_file.cast(link.File.MachO)) |macho_file| { |
| 15317 | const sym_index = macho_file.getZigObject().?.getOrCreateMetadataForLazySymbol(macho_file, lazy_sym) catch |err| | |
| 15397 | const sym_index = macho_file.getZigObject().?.getOrCreateMetadataForLazySymbol(macho_file, pt, lazy_sym) catch |err| | |
| 15318 | 15398 | return self.fail("{s} creating lazy symbol", .{@errorName(err)}); |
| 15319 | 15399 | const sym = macho_file.getSymbol(sym_index); |
| 15320 | 15400 | switch (tag) { |
| ... | ... | @@ -15353,7 +15433,8 @@ fn airIntFromPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 15353 | 15433 | } |
| 15354 | 15434 | |
| 15355 | 15435 | fn airBitCast(self: *Self, inst: Air.Inst.Index) !void { |
| 15356 | const mod = self.bin_file.comp.module.?; | |
| 15436 | const pt = self.pt; | |
| 15437 | const mod = pt.zcu; | |
| 15357 | 15438 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 15358 | 15439 | const dst_ty = self.typeOfIndex(inst); |
| 15359 | 15440 | const src_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -15366,10 +15447,10 @@ fn airBitCast(self: *Self, inst: Air.Inst.Index) !void { |
| 15366 | 15447 | const src_lock = if (src_mcv.getReg()) |reg| self.register_manager.lockReg(reg) else null; |
| 15367 | 15448 | defer if (src_lock) |lock| self.register_manager.unlockReg(lock); |
| 15368 | 15449 | |
| 15369 | const dst_mcv = if (dst_rc.supersetOf(src_rc) and dst_ty.abiSize(mod) <= src_ty.abiSize(mod) and | |
| 15450 | const dst_mcv = if (dst_rc.supersetOf(src_rc) and dst_ty.abiSize(pt) <= src_ty.abiSize(pt) and | |
| 15370 | 15451 | self.reuseOperand(inst, ty_op.operand, 0, src_mcv)) src_mcv else dst: { |
| 15371 | 15452 | const dst_mcv = try self.allocRegOrMem(inst, true); |
| 15372 | try self.genCopy(switch (math.order(dst_ty.abiSize(mod), src_ty.abiSize(mod))) { | |
| 15453 | try self.genCopy(switch (math.order(dst_ty.abiSize(pt), src_ty.abiSize(pt))) { | |
| 15373 | 15454 | .lt => dst_ty, |
| 15374 | 15455 | .eq => if (!dst_mcv.isMemory() or src_mcv.isMemory()) dst_ty else src_ty, |
| 15375 | 15456 | .gt => src_ty, |
| ... | ... | @@ -15382,8 +15463,8 @@ fn airBitCast(self: *Self, inst: Air.Inst.Index) !void { |
| 15382 | 15463 | if (dst_ty.isAbiInt(mod) and src_ty.isAbiInt(mod) and |
| 15383 | 15464 | dst_ty.intInfo(mod).signedness == src_ty.intInfo(mod).signedness) break :result dst_mcv; |
| 15384 | 15465 | |
| 15385 | const abi_size = dst_ty.abiSize(mod); | |
| 15386 | const bit_size = dst_ty.bitSize(mod); | |
| 15466 | const abi_size = dst_ty.abiSize(pt); | |
| 15467 | const bit_size = dst_ty.bitSize(pt); | |
| 15387 | 15468 | if (abi_size * 8 <= bit_size or dst_ty.isVector(mod)) break :result dst_mcv; |
| 15388 | 15469 | |
| 15389 | 15470 | const dst_limbs_len = math.divCeil(i32, @intCast(bit_size), 64) catch unreachable; |
| ... | ... | @@ -15412,7 +15493,8 @@ fn airBitCast(self: *Self, inst: Air.Inst.Index) !void { |
| 15412 | 15493 | } |
| 15413 | 15494 | |
| 15414 | 15495 | fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 15415 | const mod = self.bin_file.comp.module.?; | |
| 15496 | const pt = self.pt; | |
| 15497 | const mod = pt.zcu; | |
| 15416 | 15498 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 15417 | 15499 | |
| 15418 | 15500 | const slice_ty = self.typeOfIndex(inst); |
| ... | ... | @@ -15421,11 +15503,11 @@ fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 15421 | 15503 | const array_ty = ptr_ty.childType(mod); |
| 15422 | 15504 | const array_len = array_ty.arrayLen(mod); |
| 15423 | 15505 | |
| 15424 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(slice_ty, mod)); | |
| 15506 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(slice_ty, pt)); | |
| 15425 | 15507 | try self.genSetMem(.{ .frame = frame_index }, 0, ptr_ty, ptr, .{}); |
| 15426 | 15508 | try self.genSetMem( |
| 15427 | 15509 | .{ .frame = frame_index }, |
| 15428 | @intCast(ptr_ty.abiSize(mod)), | |
| 15510 | @intCast(ptr_ty.abiSize(pt)), | |
| 15429 | 15511 | Type.usize, |
| 15430 | 15512 | .{ .immediate = array_len }, |
| 15431 | 15513 | .{}, |
| ... | ... | @@ -15436,14 +15518,15 @@ fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) !void { |
| 15436 | 15518 | } |
| 15437 | 15519 | |
| 15438 | 15520 | fn airFloatFromInt(self: *Self, inst: Air.Inst.Index) !void { |
| 15439 | const mod = self.bin_file.comp.module.?; | |
| 15521 | const pt = self.pt; | |
| 15522 | const mod = pt.zcu; | |
| 15440 | 15523 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 15441 | 15524 | |
| 15442 | 15525 | const dst_ty = self.typeOfIndex(inst); |
| 15443 | 15526 | const dst_bits = dst_ty.floatBits(self.target.*); |
| 15444 | 15527 | |
| 15445 | 15528 | const src_ty = self.typeOf(ty_op.operand); |
| 15446 | const src_bits: u32 = @intCast(src_ty.bitSize(mod)); | |
| 15529 | const src_bits: u32 = @intCast(src_ty.bitSize(pt)); | |
| 15447 | 15530 | const src_signedness = |
| 15448 | 15531 | if (src_ty.isAbiInt(mod)) src_ty.intInfo(mod).signedness else .unsigned; |
| 15449 | 15532 | const src_size = math.divCeil(u32, @max(switch (src_signedness) { |
| ... | ... | @@ -15458,7 +15541,7 @@ fn airFloatFromInt(self: *Self, inst: Air.Inst.Index) !void { |
| 15458 | 15541 | else => unreachable, |
| 15459 | 15542 | }) { |
| 15460 | 15543 | if (src_bits > 128) return self.fail("TODO implement airFloatFromInt from {} to {}", .{ |
| 15461 | src_ty.fmt(mod), dst_ty.fmt(mod), | |
| 15544 | src_ty.fmt(pt), dst_ty.fmt(pt), | |
| 15462 | 15545 | }); |
| 15463 | 15546 | |
| 15464 | 15547 | var callee_buf: ["__floatun?i?f".len]u8 = undefined; |
| ... | ... | @@ -15500,7 +15583,7 @@ fn airFloatFromInt(self: *Self, inst: Air.Inst.Index) !void { |
| 15500 | 15583 | }, |
| 15501 | 15584 | else => null, |
| 15502 | 15585 | }) orelse return self.fail("TODO implement airFloatFromInt from {} to {}", .{ |
| 15503 | src_ty.fmt(mod), dst_ty.fmt(mod), | |
| 15586 | src_ty.fmt(pt), dst_ty.fmt(pt), | |
| 15504 | 15587 | }); |
| 15505 | 15588 | const dst_alias = dst_reg.to128(); |
| 15506 | 15589 | const src_alias = registerAlias(src_reg, src_size); |
| ... | ... | @@ -15515,11 +15598,12 @@ fn airFloatFromInt(self: *Self, inst: Air.Inst.Index) !void { |
| 15515 | 15598 | } |
| 15516 | 15599 | |
| 15517 | 15600 | fn airIntFromFloat(self: *Self, inst: Air.Inst.Index) !void { |
| 15518 | const mod = self.bin_file.comp.module.?; | |
| 15601 | const pt = self.pt; | |
| 15602 | const mod = pt.zcu; | |
| 15519 | 15603 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 15520 | 15604 | |
| 15521 | 15605 | const dst_ty = self.typeOfIndex(inst); |
| 15522 | const dst_bits: u32 = @intCast(dst_ty.bitSize(mod)); | |
| 15606 | const dst_bits: u32 = @intCast(dst_ty.bitSize(pt)); | |
| 15523 | 15607 | const dst_signedness = |
| 15524 | 15608 | if (dst_ty.isAbiInt(mod)) dst_ty.intInfo(mod).signedness else .unsigned; |
| 15525 | 15609 | const dst_size = math.divCeil(u32, @max(switch (dst_signedness) { |
| ... | ... | @@ -15537,7 +15621,7 @@ fn airIntFromFloat(self: *Self, inst: Air.Inst.Index) !void { |
| 15537 | 15621 | else => unreachable, |
| 15538 | 15622 | }) { |
| 15539 | 15623 | if (dst_bits > 128) return self.fail("TODO implement airIntFromFloat from {} to {}", .{ |
| 15540 | src_ty.fmt(mod), dst_ty.fmt(mod), | |
| 15624 | src_ty.fmt(pt), dst_ty.fmt(pt), | |
| 15541 | 15625 | }); |
| 15542 | 15626 | |
| 15543 | 15627 | var callee_buf: ["__fixuns?f?i".len]u8 = undefined; |
| ... | ... | @@ -15586,13 +15670,13 @@ fn airIntFromFloat(self: *Self, inst: Air.Inst.Index) !void { |
| 15586 | 15670 | } |
| 15587 | 15671 | |
| 15588 | 15672 | fn airCmpxchg(self: *Self, inst: Air.Inst.Index) !void { |
| 15589 | const mod = self.bin_file.comp.module.?; | |
| 15673 | const pt = self.pt; | |
| 15590 | 15674 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 15591 | 15675 | const extra = self.air.extraData(Air.Cmpxchg, ty_pl.payload).data; |
| 15592 | 15676 | |
| 15593 | 15677 | const ptr_ty = self.typeOf(extra.ptr); |
| 15594 | 15678 | const val_ty = self.typeOf(extra.expected_value); |
| 15595 | const val_abi_size: u32 = @intCast(val_ty.abiSize(mod)); | |
| 15679 | const val_abi_size: u32 = @intCast(val_ty.abiSize(pt)); | |
| 15596 | 15680 | |
| 15597 | 15681 | try self.spillRegisters(&.{ .rax, .rdx, .rbx, .rcx }); |
| 15598 | 15682 | const regs_lock = self.register_manager.lockRegsAssumeUnused(4, .{ .rax, .rdx, .rbx, .rcx }); |
| ... | ... | @@ -15682,7 +15766,8 @@ fn atomicOp( |
| 15682 | 15766 | rmw_op: ?std.builtin.AtomicRmwOp, |
| 15683 | 15767 | order: std.builtin.AtomicOrder, |
| 15684 | 15768 | ) InnerError!MCValue { |
| 15685 | const mod = self.bin_file.comp.module.?; | |
| 15769 | const pt = self.pt; | |
| 15770 | const mod = pt.zcu; | |
| 15686 | 15771 | const ptr_lock = switch (ptr_mcv) { |
| 15687 | 15772 | .register => |reg| self.register_manager.lockReg(reg), |
| 15688 | 15773 | else => null, |
| ... | ... | @@ -15695,7 +15780,7 @@ fn atomicOp( |
| 15695 | 15780 | }; |
| 15696 | 15781 | defer if (val_lock) |lock| self.register_manager.unlockReg(lock); |
| 15697 | 15782 | |
| 15698 | const val_abi_size: u32 = @intCast(val_ty.abiSize(mod)); | |
| 15783 | const val_abi_size: u32 = @intCast(val_ty.abiSize(pt)); | |
| 15699 | 15784 | const mem_size = Memory.Size.fromSize(val_abi_size); |
| 15700 | 15785 | const ptr_mem: Memory = switch (ptr_mcv) { |
| 15701 | 15786 | .immediate, .register, .register_offset, .lea_frame => try ptr_mcv.deref().mem(self, mem_size), |
| ... | ... | @@ -15809,7 +15894,7 @@ fn atomicOp( |
| 15809 | 15894 | }, |
| 15810 | 15895 | else => unreachable, |
| 15811 | 15896 | }) orelse return self.fail("TODO implement atomicOp of {s} for {}", .{ |
| 15812 | @tagName(op), val_ty.fmt(mod), | |
| 15897 | @tagName(op), val_ty.fmt(pt), | |
| 15813 | 15898 | }); |
| 15814 | 15899 | try self.genSetReg(sse_reg, val_ty, .{ .register = .rax }, .{}); |
| 15815 | 15900 | switch (mir_tag[0]) { |
| ... | ... | @@ -16086,7 +16171,8 @@ fn airAtomicStore(self: *Self, inst: Air.Inst.Index, order: std.builtin.AtomicOr |
| 16086 | 16171 | } |
| 16087 | 16172 | |
| 16088 | 16173 | fn airMemset(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 16089 | const mod = self.bin_file.comp.module.?; | |
| 16174 | const pt = self.pt; | |
| 16175 | const mod = pt.zcu; | |
| 16090 | 16176 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 16091 | 16177 | |
| 16092 | 16178 | result: { |
| ... | ... | @@ -16112,7 +16198,7 @@ fn airMemset(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 16112 | 16198 | }; |
| 16113 | 16199 | defer if (src_val_lock) |lock| self.register_manager.unlockReg(lock); |
| 16114 | 16200 | |
| 16115 | const elem_abi_size: u31 = @intCast(elem_ty.abiSize(mod)); | |
| 16201 | const elem_abi_size: u31 = @intCast(elem_ty.abiSize(pt)); | |
| 16116 | 16202 | |
| 16117 | 16203 | if (elem_abi_size == 1) { |
| 16118 | 16204 | const ptr: MCValue = switch (dst_ptr_ty.ptrSize(mod)) { |
| ... | ... | @@ -16185,7 +16271,7 @@ fn airMemset(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 16185 | 16271 | self.performReloc(skip_reloc); |
| 16186 | 16272 | }, |
| 16187 | 16273 | .One => { |
| 16188 | const elem_ptr_ty = try mod.singleMutPtrType(elem_ty); | |
| 16274 | const elem_ptr_ty = try pt.singleMutPtrType(elem_ty); | |
| 16189 | 16275 | |
| 16190 | 16276 | const len = dst_ptr_ty.childType(mod).arrayLen(mod); |
| 16191 | 16277 | |
| ... | ... | @@ -16214,7 +16300,8 @@ fn airMemset(self: *Self, inst: Air.Inst.Index, safety: bool) !void { |
| 16214 | 16300 | } |
| 16215 | 16301 | |
| 16216 | 16302 | fn airMemcpy(self: *Self, inst: Air.Inst.Index) !void { |
| 16217 | const mod = self.bin_file.comp.module.?; | |
| 16303 | const pt = self.pt; | |
| 16304 | const mod = pt.zcu; | |
| 16218 | 16305 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 16219 | 16306 | |
| 16220 | 16307 | try self.spillRegisters(&.{ .rdi, .rsi, .rcx }); |
| ... | ... | @@ -16246,13 +16333,13 @@ fn airMemcpy(self: *Self, inst: Air.Inst.Index) !void { |
| 16246 | 16333 | .{ .i_, .mul }, |
| 16247 | 16334 | len_reg, |
| 16248 | 16335 | try dst_ptr.address().offset(8).deref().mem(self, .qword), |
| 16249 | Immediate.s(@intCast(dst_ptr_ty.childType(mod).abiSize(mod))), | |
| 16336 | Immediate.s(@intCast(dst_ptr_ty.childType(mod).abiSize(pt))), | |
| 16250 | 16337 | ); |
| 16251 | 16338 | break :len .{ .register = len_reg }; |
| 16252 | 16339 | }, |
| 16253 | 16340 | .One => len: { |
| 16254 | 16341 | const array_ty = dst_ptr_ty.childType(mod); |
| 16255 | break :len .{ .immediate = array_ty.arrayLen(mod) * array_ty.childType(mod).abiSize(mod) }; | |
| 16342 | break :len .{ .immediate = array_ty.arrayLen(mod) * array_ty.childType(mod).abiSize(pt) }; | |
| 16256 | 16343 | }, |
| 16257 | 16344 | .C, .Many => unreachable, |
| 16258 | 16345 | }; |
| ... | ... | @@ -16269,7 +16356,8 @@ fn airMemcpy(self: *Self, inst: Air.Inst.Index) !void { |
| 16269 | 16356 | } |
| 16270 | 16357 | |
| 16271 | 16358 | fn airTagName(self: *Self, inst: Air.Inst.Index) !void { |
| 16272 | const mod = self.bin_file.comp.module.?; | |
| 16359 | const pt = self.pt; | |
| 16360 | const mod = pt.zcu; | |
| 16273 | 16361 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 16274 | 16362 | const inst_ty = self.typeOfIndex(inst); |
| 16275 | 16363 | const enum_ty = self.typeOf(un_op); |
| ... | ... | @@ -16278,8 +16366,8 @@ fn airTagName(self: *Self, inst: Air.Inst.Index) !void { |
| 16278 | 16366 | // We need a properly aligned and sized call frame to be able to call this function. |
| 16279 | 16367 | { |
| 16280 | 16368 | const needed_call_frame = FrameAlloc.init(.{ |
| 16281 | .size = inst_ty.abiSize(mod), | |
| 16282 | .alignment = inst_ty.abiAlignment(mod), | |
| 16369 | .size = inst_ty.abiSize(pt), | |
| 16370 | .alignment = inst_ty.abiAlignment(pt), | |
| 16283 | 16371 | }); |
| 16284 | 16372 | const frame_allocs_slice = self.frame_allocs.slice(); |
| 16285 | 16373 | const stack_frame_size = |
| ... | ... | @@ -16311,7 +16399,8 @@ fn airTagName(self: *Self, inst: Air.Inst.Index) !void { |
| 16311 | 16399 | } |
| 16312 | 16400 | |
| 16313 | 16401 | fn airErrorName(self: *Self, inst: Air.Inst.Index) !void { |
| 16314 | const mod = self.bin_file.comp.module.?; | |
| 16402 | const pt = self.pt; | |
| 16403 | const mod = pt.zcu; | |
| 16315 | 16404 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 16316 | 16405 | |
| 16317 | 16406 | const err_ty = self.typeOf(un_op); |
| ... | ... | @@ -16413,7 +16502,8 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) !void { |
| 16413 | 16502 | } |
| 16414 | 16503 | |
| 16415 | 16504 | fn airSplat(self: *Self, inst: Air.Inst.Index) !void { |
| 16416 | const mod = self.bin_file.comp.module.?; | |
| 16505 | const pt = self.pt; | |
| 16506 | const mod = pt.zcu; | |
| 16417 | 16507 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 16418 | 16508 | const vector_ty = self.typeOfIndex(inst); |
| 16419 | 16509 | const vector_len = vector_ty.vectorLen(mod); |
| ... | ... | @@ -16495,15 +16585,15 @@ fn airSplat(self: *Self, inst: Air.Inst.Index) !void { |
| 16495 | 16585 | const src_mcv = try self.resolveInst(ty_op.operand); |
| 16496 | 16586 | if (src_mcv.isMemory()) try self.asmRegisterMemory( |
| 16497 | 16587 | mir_tag, |
| 16498 | registerAlias(dst_reg, @intCast(vector_ty.abiSize(mod))), | |
| 16588 | registerAlias(dst_reg, @intCast(vector_ty.abiSize(pt))), | |
| 16499 | 16589 | try src_mcv.mem(self, self.memSize(scalar_ty)), |
| 16500 | 16590 | ) else { |
| 16501 | 16591 | if (mir_tag[0] == .v_i128) break :avx2; |
| 16502 | 16592 | try self.genSetReg(dst_reg, scalar_ty, src_mcv, .{}); |
| 16503 | 16593 | try self.asmRegisterRegister( |
| 16504 | 16594 | mir_tag, |
| 16505 | registerAlias(dst_reg, @intCast(vector_ty.abiSize(mod))), | |
| 16506 | registerAlias(dst_reg, @intCast(scalar_ty.abiSize(mod))), | |
| 16595 | registerAlias(dst_reg, @intCast(vector_ty.abiSize(pt))), | |
| 16596 | registerAlias(dst_reg, @intCast(scalar_ty.abiSize(pt))), | |
| 16507 | 16597 | ); |
| 16508 | 16598 | } |
| 16509 | 16599 | break :result .{ .register = dst_reg }; |
| ... | ... | @@ -16515,7 +16605,7 @@ fn airSplat(self: *Self, inst: Air.Inst.Index) !void { |
| 16515 | 16605 | try self.genSetReg(dst_reg, scalar_ty, .{ .air_ref = ty_op.operand }, .{}); |
| 16516 | 16606 | if (vector_len == 1) break :result .{ .register = dst_reg }; |
| 16517 | 16607 | |
| 16518 | const dst_alias = registerAlias(dst_reg, @intCast(vector_ty.abiSize(mod))); | |
| 16608 | const dst_alias = registerAlias(dst_reg, @intCast(vector_ty.abiSize(pt))); | |
| 16519 | 16609 | const scalar_bits = scalar_ty.intInfo(mod).bits; |
| 16520 | 16610 | if (switch (scalar_bits) { |
| 16521 | 16611 | 1...8 => true, |
| ... | ... | @@ -16745,20 +16835,21 @@ fn airSplat(self: *Self, inst: Air.Inst.Index) !void { |
| 16745 | 16835 | else => unreachable, |
| 16746 | 16836 | }, |
| 16747 | 16837 | } |
| 16748 | return self.fail("TODO implement airSplat for {}", .{vector_ty.fmt(mod)}); | |
| 16838 | return self.fail("TODO implement airSplat for {}", .{vector_ty.fmt(pt)}); | |
| 16749 | 16839 | }; |
| 16750 | 16840 | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 16751 | 16841 | } |
| 16752 | 16842 | |
| 16753 | 16843 | fn airSelect(self: *Self, inst: Air.Inst.Index) !void { |
| 16754 | const mod = self.bin_file.comp.module.?; | |
| 16844 | const pt = self.pt; | |
| 16845 | const mod = pt.zcu; | |
| 16755 | 16846 | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 16756 | 16847 | const extra = self.air.extraData(Air.Bin, pl_op.payload).data; |
| 16757 | 16848 | const ty = self.typeOfIndex(inst); |
| 16758 | 16849 | const vec_len = ty.vectorLen(mod); |
| 16759 | 16850 | const elem_ty = ty.childType(mod); |
| 16760 | const elem_abi_size: u32 = @intCast(elem_ty.abiSize(mod)); | |
| 16761 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 16851 | const elem_abi_size: u32 = @intCast(elem_ty.abiSize(pt)); | |
| 16852 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 16762 | 16853 | const pred_ty = self.typeOf(pl_op.operand); |
| 16763 | 16854 | |
| 16764 | 16855 | const result = result: { |
| ... | ... | @@ -16878,17 +16969,17 @@ fn airSelect(self: *Self, inst: Air.Inst.Index) !void { |
| 16878 | 16969 | else => unreachable, |
| 16879 | 16970 | }), |
| 16880 | 16971 | ); |
| 16881 | } else return self.fail("TODO implement airSelect for {}", .{ty.fmt(mod)}); | |
| 16972 | } else return self.fail("TODO implement airSelect for {}", .{ty.fmt(pt)}); | |
| 16882 | 16973 | const elem_bits: u16 = @intCast(elem_abi_size * 8); |
| 16883 | const mask_elem_ty = try mod.intType(.unsigned, elem_bits); | |
| 16884 | const mask_ty = try mod.vectorType(.{ .len = vec_len, .child = mask_elem_ty.toIntern() }); | |
| 16974 | const mask_elem_ty = try pt.intType(.unsigned, elem_bits); | |
| 16975 | const mask_ty = try pt.vectorType(.{ .len = vec_len, .child = mask_elem_ty.toIntern() }); | |
| 16885 | 16976 | if (!pred_fits_in_elem) if (self.hasFeature(.ssse3)) { |
| 16886 | 16977 | var mask_elems: [32]InternPool.Index = undefined; |
| 16887 | for (mask_elems[0..vec_len], 0..) |*elem, bit| elem.* = try mod.intern(.{ .int = .{ | |
| 16978 | for (mask_elems[0..vec_len], 0..) |*elem, bit| elem.* = try pt.intern(.{ .int = .{ | |
| 16888 | 16979 | .ty = mask_elem_ty.toIntern(), |
| 16889 | 16980 | .storage = .{ .u64 = bit / elem_bits }, |
| 16890 | 16981 | } }); |
| 16891 | const mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{ | |
| 16982 | const mask_mcv = try self.genTypedValue(Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 16892 | 16983 | .ty = mask_ty.toIntern(), |
| 16893 | 16984 | .storage = .{ .elems = mask_elems[0..vec_len] }, |
| 16894 | 16985 | } }))); |
| ... | ... | @@ -16906,14 +16997,14 @@ fn airSelect(self: *Self, inst: Air.Inst.Index) !void { |
| 16906 | 16997 | mask_alias, |
| 16907 | 16998 | mask_mem, |
| 16908 | 16999 | ); |
| 16909 | } else return self.fail("TODO implement airSelect for {}", .{ty.fmt(mod)}); | |
| 17000 | } else return self.fail("TODO implement airSelect for {}", .{ty.fmt(pt)}); | |
| 16910 | 17001 | { |
| 16911 | 17002 | var mask_elems: [32]InternPool.Index = undefined; |
| 16912 | for (mask_elems[0..vec_len], 0..) |*elem, bit| elem.* = try mod.intern(.{ .int = .{ | |
| 17003 | for (mask_elems[0..vec_len], 0..) |*elem, bit| elem.* = try pt.intern(.{ .int = .{ | |
| 16913 | 17004 | .ty = mask_elem_ty.toIntern(), |
| 16914 | 17005 | .storage = .{ .u64 = @as(u32, 1) << @intCast(bit & (elem_bits - 1)) }, |
| 16915 | 17006 | } }); |
| 16916 | const mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{ | |
| 17007 | const mask_mcv = try self.genTypedValue(Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 16917 | 17008 | .ty = mask_ty.toIntern(), |
| 16918 | 17009 | .storage = .{ .elems = mask_elems[0..vec_len] }, |
| 16919 | 17010 | } }))); |
| ... | ... | @@ -17014,7 +17105,7 @@ fn airSelect(self: *Self, inst: Air.Inst.Index) !void { |
| 17014 | 17105 | else => null, |
| 17015 | 17106 | }, |
| 17016 | 17107 | }, |
| 17017 | }) orelse return self.fail("TODO implement airSelect for {}", .{ty.fmt(mod)}); | |
| 17108 | }) orelse return self.fail("TODO implement airSelect for {}", .{ty.fmt(pt)}); | |
| 17018 | 17109 | if (has_avx) { |
| 17019 | 17110 | const rhs_alias = if (rhs_mcv.isRegister()) |
| 17020 | 17111 | registerAlias(rhs_mcv.getReg().?, abi_size) |
| ... | ... | @@ -17061,7 +17152,7 @@ fn airSelect(self: *Self, inst: Air.Inst.Index) !void { |
| 17061 | 17152 | 16, 80, 128 => null, |
| 17062 | 17153 | else => unreachable, |
| 17063 | 17154 | }, |
| 17064 | }) orelse return self.fail("TODO implement airSelect for {}", .{ty.fmt(mod)}); | |
| 17155 | }) orelse return self.fail("TODO implement airSelect for {}", .{ty.fmt(pt)}); | |
| 17065 | 17156 | try self.asmRegisterRegister(.{ mir_fixes, .@"and" }, dst_alias, mask_alias); |
| 17066 | 17157 | if (rhs_mcv.isMemory()) try self.asmRegisterMemory( |
| 17067 | 17158 | .{ mir_fixes, .andn }, |
| ... | ... | @@ -17083,18 +17174,19 @@ fn airSelect(self: *Self, inst: Air.Inst.Index) !void { |
| 17083 | 17174 | } |
| 17084 | 17175 | |
| 17085 | 17176 | fn airShuffle(self: *Self, inst: Air.Inst.Index) !void { |
| 17086 | const mod = self.bin_file.comp.module.?; | |
| 17177 | const pt = self.pt; | |
| 17178 | const mod = pt.zcu; | |
| 17087 | 17179 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 17088 | 17180 | const extra = self.air.extraData(Air.Shuffle, ty_pl.payload).data; |
| 17089 | 17181 | |
| 17090 | 17182 | const dst_ty = self.typeOfIndex(inst); |
| 17091 | 17183 | const elem_ty = dst_ty.childType(mod); |
| 17092 | const elem_abi_size: u16 = @intCast(elem_ty.abiSize(mod)); | |
| 17093 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(mod)); | |
| 17184 | const elem_abi_size: u16 = @intCast(elem_ty.abiSize(pt)); | |
| 17185 | const dst_abi_size: u32 = @intCast(dst_ty.abiSize(pt)); | |
| 17094 | 17186 | const lhs_ty = self.typeOf(extra.a); |
| 17095 | const lhs_abi_size: u32 = @intCast(lhs_ty.abiSize(mod)); | |
| 17187 | const lhs_abi_size: u32 = @intCast(lhs_ty.abiSize(pt)); | |
| 17096 | 17188 | const rhs_ty = self.typeOf(extra.b); |
| 17097 | const rhs_abi_size: u32 = @intCast(rhs_ty.abiSize(mod)); | |
| 17189 | const rhs_abi_size: u32 = @intCast(rhs_ty.abiSize(pt)); | |
| 17098 | 17190 | const max_abi_size = @max(dst_abi_size, lhs_abi_size, rhs_abi_size); |
| 17099 | 17191 | |
| 17100 | 17192 | const ExpectedContents = [32]?i32; |
| ... | ... | @@ -17106,11 +17198,11 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void { |
| 17106 | 17198 | defer allocator.free(mask_elems); |
| 17107 | 17199 | for (mask_elems, 0..) |*mask_elem, elem_index| { |
| 17108 | 17200 | const mask_elem_val = |
| 17109 | Value.fromInterned(extra.mask).elemValue(mod, elem_index) catch unreachable; | |
| 17201 | Value.fromInterned(extra.mask).elemValue(pt, elem_index) catch unreachable; | |
| 17110 | 17202 | mask_elem.* = if (mask_elem_val.isUndef(mod)) |
| 17111 | 17203 | null |
| 17112 | 17204 | else |
| 17113 | @intCast(mask_elem_val.toSignedInt(mod)); | |
| 17205 | @intCast(mask_elem_val.toSignedInt(pt)); | |
| 17114 | 17206 | } |
| 17115 | 17207 | |
| 17116 | 17208 | const has_avx = self.hasFeature(.avx); |
| ... | ... | @@ -17626,8 +17718,8 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void { |
| 17626 | 17718 | else |
| 17627 | 17719 | self.hasFeature(.avx2)) 32 else 16)) break :blendv; |
| 17628 | 17720 | |
| 17629 | const select_mask_elem_ty = try mod.intType(.unsigned, elem_abi_size * 8); | |
| 17630 | const select_mask_ty = try mod.vectorType(.{ | |
| 17721 | const select_mask_elem_ty = try pt.intType(.unsigned, elem_abi_size * 8); | |
| 17722 | const select_mask_ty = try pt.vectorType(.{ | |
| 17631 | 17723 | .len = @intCast(mask_elems.len), |
| 17632 | 17724 | .child = select_mask_elem_ty.toIntern(), |
| 17633 | 17725 | }); |
| ... | ... | @@ -17643,11 +17735,11 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void { |
| 17643 | 17735 | if (mask_elem_index != elem_index) break :blendv; |
| 17644 | 17736 | |
| 17645 | 17737 | select_mask_elem.* = (if (mask_elem < 0) |
| 17646 | try select_mask_elem_ty.maxIntScalar(mod, select_mask_elem_ty) | |
| 17738 | try select_mask_elem_ty.maxIntScalar(pt, select_mask_elem_ty) | |
| 17647 | 17739 | else |
| 17648 | try select_mask_elem_ty.minIntScalar(mod, select_mask_elem_ty)).toIntern(); | |
| 17740 | try select_mask_elem_ty.minIntScalar(pt, select_mask_elem_ty)).toIntern(); | |
| 17649 | 17741 | } |
| 17650 | const select_mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{ | |
| 17742 | const select_mask_mcv = try self.genTypedValue(Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 17651 | 17743 | .ty = select_mask_ty.toIntern(), |
| 17652 | 17744 | .storage = .{ .elems = select_mask_elems[0..mask_elems.len] }, |
| 17653 | 17745 | } }))); |
| ... | ... | @@ -17783,7 +17875,7 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void { |
| 17783 | 17875 | var lhs_mask_elems: [16]InternPool.Index = undefined; |
| 17784 | 17876 | for (lhs_mask_elems[0..max_abi_size], 0..) |*lhs_mask_elem, byte_index| { |
| 17785 | 17877 | const elem_index = byte_index / elem_abi_size; |
| 17786 | lhs_mask_elem.* = try mod.intern(.{ .int = .{ | |
| 17878 | lhs_mask_elem.* = try pt.intern(.{ .int = .{ | |
| 17787 | 17879 | .ty = .u8_type, |
| 17788 | 17880 | .storage = .{ .u64 = if (elem_index >= mask_elems.len) 0b1_00_00000 else elem: { |
| 17789 | 17881 | const mask_elem = mask_elems[elem_index] orelse break :elem 0b1_00_00000; |
| ... | ... | @@ -17794,8 +17886,8 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void { |
| 17794 | 17886 | } }, |
| 17795 | 17887 | } }); |
| 17796 | 17888 | } |
| 17797 | const lhs_mask_ty = try mod.vectorType(.{ .len = max_abi_size, .child = .u8_type }); | |
| 17798 | const lhs_mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{ | |
| 17889 | const lhs_mask_ty = try pt.vectorType(.{ .len = max_abi_size, .child = .u8_type }); | |
| 17890 | const lhs_mask_mcv = try self.genTypedValue(Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 17799 | 17891 | .ty = lhs_mask_ty.toIntern(), |
| 17800 | 17892 | .storage = .{ .elems = lhs_mask_elems[0..max_abi_size] }, |
| 17801 | 17893 | } }))); |
| ... | ... | @@ -17817,7 +17909,7 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void { |
| 17817 | 17909 | var rhs_mask_elems: [16]InternPool.Index = undefined; |
| 17818 | 17910 | for (rhs_mask_elems[0..max_abi_size], 0..) |*rhs_mask_elem, byte_index| { |
| 17819 | 17911 | const elem_index = byte_index / elem_abi_size; |
| 17820 | rhs_mask_elem.* = try mod.intern(.{ .int = .{ | |
| 17912 | rhs_mask_elem.* = try pt.intern(.{ .int = .{ | |
| 17821 | 17913 | .ty = .u8_type, |
| 17822 | 17914 | .storage = .{ .u64 = if (elem_index >= mask_elems.len) 0b1_00_00000 else elem: { |
| 17823 | 17915 | const mask_elem = mask_elems[elem_index] orelse break :elem 0b1_00_00000; |
| ... | ... | @@ -17828,8 +17920,8 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void { |
| 17828 | 17920 | } }, |
| 17829 | 17921 | } }); |
| 17830 | 17922 | } |
| 17831 | const rhs_mask_ty = try mod.vectorType(.{ .len = max_abi_size, .child = .u8_type }); | |
| 17832 | const rhs_mask_mcv = try self.genTypedValue(Value.fromInterned(try mod.intern(.{ .aggregate = .{ | |
| 17923 | const rhs_mask_ty = try pt.vectorType(.{ .len = max_abi_size, .child = .u8_type }); | |
| 17924 | const rhs_mask_mcv = try self.genTypedValue(Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 17833 | 17925 | .ty = rhs_mask_ty.toIntern(), |
| 17834 | 17926 | .storage = .{ .elems = rhs_mask_elems[0..max_abi_size] }, |
| 17835 | 17927 | } }))); |
| ... | ... | @@ -17881,14 +17973,15 @@ fn airShuffle(self: *Self, inst: Air.Inst.Index) !void { |
| 17881 | 17973 | |
| 17882 | 17974 | break :result null; |
| 17883 | 17975 | }) orelse return self.fail("TODO implement airShuffle from {} and {} to {} with {}", .{ |
| 17884 | lhs_ty.fmt(mod), rhs_ty.fmt(mod), dst_ty.fmt(mod), | |
| 17885 | Value.fromInterned(extra.mask).fmtValue(mod, null), | |
| 17976 | lhs_ty.fmt(pt), rhs_ty.fmt(pt), dst_ty.fmt(pt), | |
| 17977 | Value.fromInterned(extra.mask).fmtValue(pt, null), | |
| 17886 | 17978 | }); |
| 17887 | 17979 | return self.finishAir(inst, result, .{ extra.a, extra.b, .none }); |
| 17888 | 17980 | } |
| 17889 | 17981 | |
| 17890 | 17982 | fn airReduce(self: *Self, inst: Air.Inst.Index) !void { |
| 17891 | const mod = self.bin_file.comp.module.?; | |
| 17983 | const pt = self.pt; | |
| 17984 | const mod = pt.zcu; | |
| 17892 | 17985 | const reduce = self.air.instructions.items(.data)[@intFromEnum(inst)].reduce; |
| 17893 | 17986 | |
| 17894 | 17987 | const result: MCValue = result: { |
| ... | ... | @@ -17898,9 +17991,9 @@ fn airReduce(self: *Self, inst: Air.Inst.Index) !void { |
| 17898 | 17991 | |
| 17899 | 17992 | const operand_mcv = try self.resolveInst(reduce.operand); |
| 17900 | 17993 | const mask_len = (math.cast(u6, operand_ty.vectorLen(mod)) orelse |
| 17901 | return self.fail("TODO implement airReduce for {}", .{operand_ty.fmt(mod)})); | |
| 17994 | return self.fail("TODO implement airReduce for {}", .{operand_ty.fmt(pt)})); | |
| 17902 | 17995 | const mask = (@as(u64, 1) << mask_len) - 1; |
| 17903 | const abi_size: u32 = @intCast(operand_ty.abiSize(mod)); | |
| 17996 | const abi_size: u32 = @intCast(operand_ty.abiSize(pt)); | |
| 17904 | 17997 | switch (reduce.operation) { |
| 17905 | 17998 | .Or => { |
| 17906 | 17999 | if (operand_mcv.isMemory()) try self.asmMemoryImmediate( |
| ... | ... | @@ -17936,16 +18029,17 @@ fn airReduce(self: *Self, inst: Air.Inst.Index) !void { |
| 17936 | 18029 | try self.asmRegisterRegister(.{ ._, .@"test" }, tmp_reg, tmp_reg); |
| 17937 | 18030 | break :result .{ .eflags = .z }; |
| 17938 | 18031 | }, |
| 17939 | else => return self.fail("TODO implement airReduce for {}", .{operand_ty.fmt(mod)}), | |
| 18032 | else => return self.fail("TODO implement airReduce for {}", .{operand_ty.fmt(pt)}), | |
| 17940 | 18033 | } |
| 17941 | 18034 | } |
| 17942 | return self.fail("TODO implement airReduce for {}", .{operand_ty.fmt(mod)}); | |
| 18035 | return self.fail("TODO implement airReduce for {}", .{operand_ty.fmt(pt)}); | |
| 17943 | 18036 | }; |
| 17944 | 18037 | return self.finishAir(inst, result, .{ reduce.operand, .none, .none }); |
| 17945 | 18038 | } |
| 17946 | 18039 | |
| 17947 | 18040 | fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| 17948 | const mod = self.bin_file.comp.module.?; | |
| 18041 | const pt = self.pt; | |
| 18042 | const mod = pt.zcu; | |
| 17949 | 18043 | const result_ty = self.typeOfIndex(inst); |
| 17950 | 18044 | const len: usize = @intCast(result_ty.arrayLen(mod)); |
| 17951 | 18045 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| ... | ... | @@ -17953,30 +18047,30 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| 17953 | 18047 | const result: MCValue = result: { |
| 17954 | 18048 | switch (result_ty.zigTypeTag(mod)) { |
| 17955 | 18049 | .Struct => { |
| 17956 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(result_ty, mod)); | |
| 18050 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(result_ty, pt)); | |
| 17957 | 18051 | if (result_ty.containerLayout(mod) == .@"packed") { |
| 17958 | 18052 | const struct_obj = mod.typeToStruct(result_ty).?; |
| 17959 | 18053 | try self.genInlineMemset( |
| 17960 | 18054 | .{ .lea_frame = .{ .index = frame_index } }, |
| 17961 | 18055 | .{ .immediate = 0 }, |
| 17962 | .{ .immediate = result_ty.abiSize(mod) }, | |
| 18056 | .{ .immediate = result_ty.abiSize(pt) }, | |
| 17963 | 18057 | .{}, |
| 17964 | 18058 | ); |
| 17965 | 18059 | for (elements, 0..) |elem, elem_i_usize| { |
| 17966 | 18060 | const elem_i: u32 = @intCast(elem_i_usize); |
| 17967 | if ((try result_ty.structFieldValueComptime(mod, elem_i)) != null) continue; | |
| 18061 | if ((try result_ty.structFieldValueComptime(pt, elem_i)) != null) continue; | |
| 17968 | 18062 | |
| 17969 | 18063 | const elem_ty = result_ty.structFieldType(elem_i, mod); |
| 17970 | const elem_bit_size: u32 = @intCast(elem_ty.bitSize(mod)); | |
| 18064 | const elem_bit_size: u32 = @intCast(elem_ty.bitSize(pt)); | |
| 17971 | 18065 | if (elem_bit_size > 64) { |
| 17972 | 18066 | return self.fail( |
| 17973 | 18067 | "TODO airAggregateInit implement packed structs with large fields", |
| 17974 | 18068 | .{}, |
| 17975 | 18069 | ); |
| 17976 | 18070 | } |
| 17977 | const elem_abi_size: u32 = @intCast(elem_ty.abiSize(mod)); | |
| 18071 | const elem_abi_size: u32 = @intCast(elem_ty.abiSize(pt)); | |
| 17978 | 18072 | const elem_abi_bits = elem_abi_size * 8; |
| 17979 | const elem_off = mod.structPackedFieldBitOffset(struct_obj, elem_i); | |
| 18073 | const elem_off = pt.structPackedFieldBitOffset(struct_obj, elem_i); | |
| 17980 | 18074 | const elem_byte_off: i32 = @intCast(elem_off / elem_abi_bits * elem_abi_size); |
| 17981 | 18075 | const elem_bit_off = elem_off % elem_abi_bits; |
| 17982 | 18076 | const elem_mcv = try self.resolveInst(elem); |
| ... | ... | @@ -18046,10 +18140,10 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| 18046 | 18140 | } |
| 18047 | 18141 | } |
| 18048 | 18142 | } else for (elements, 0..) |elem, elem_i| { |
| 18049 | if ((try result_ty.structFieldValueComptime(mod, elem_i)) != null) continue; | |
| 18143 | if ((try result_ty.structFieldValueComptime(pt, elem_i)) != null) continue; | |
| 18050 | 18144 | |
| 18051 | 18145 | const elem_ty = result_ty.structFieldType(elem_i, mod); |
| 18052 | const elem_off: i32 = @intCast(result_ty.structFieldOffset(elem_i, mod)); | |
| 18146 | const elem_off: i32 = @intCast(result_ty.structFieldOffset(elem_i, pt)); | |
| 18053 | 18147 | const elem_mcv = try self.resolveInst(elem); |
| 18054 | 18148 | const mat_elem_mcv = switch (elem_mcv) { |
| 18055 | 18149 | .load_tlv => |sym_index| MCValue{ .lea_tlv = sym_index }, |
| ... | ... | @@ -18062,7 +18156,7 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| 18062 | 18156 | .Array, .Vector => { |
| 18063 | 18157 | const elem_ty = result_ty.childType(mod); |
| 18064 | 18158 | if (result_ty.isVector(mod) and elem_ty.toIntern() == .bool_type) { |
| 18065 | const result_size: u32 = @intCast(result_ty.abiSize(mod)); | |
| 18159 | const result_size: u32 = @intCast(result_ty.abiSize(pt)); | |
| 18066 | 18160 | const dst_reg = try self.register_manager.allocReg(inst, abi.RegisterClass.gp); |
| 18067 | 18161 | try self.asmRegisterRegister( |
| 18068 | 18162 | .{ ._, .xor }, |
| ... | ... | @@ -18093,8 +18187,8 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| 18093 | 18187 | } |
| 18094 | 18188 | break :result .{ .register = dst_reg }; |
| 18095 | 18189 | } else { |
| 18096 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(result_ty, mod)); | |
| 18097 | const elem_size: u32 = @intCast(elem_ty.abiSize(mod)); | |
| 18190 | const frame_index = try self.allocFrameIndex(FrameAlloc.initSpill(result_ty, pt)); | |
| 18191 | const elem_size: u32 = @intCast(elem_ty.abiSize(pt)); | |
| 18098 | 18192 | |
| 18099 | 18193 | for (elements, 0..) |elem, elem_i| { |
| 18100 | 18194 | const elem_mcv = try self.resolveInst(elem); |
| ... | ... | @@ -18136,18 +18230,19 @@ fn airAggregateInit(self: *Self, inst: Air.Inst.Index) !void { |
| 18136 | 18230 | } |
| 18137 | 18231 | |
| 18138 | 18232 | fn airUnionInit(self: *Self, inst: Air.Inst.Index) !void { |
| 18139 | const mod = self.bin_file.comp.module.?; | |
| 18233 | const pt = self.pt; | |
| 18234 | const mod = pt.zcu; | |
| 18140 | 18235 | const ip = &mod.intern_pool; |
| 18141 | 18236 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 18142 | 18237 | const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 18143 | 18238 | const result: MCValue = result: { |
| 18144 | 18239 | const union_ty = self.typeOfIndex(inst); |
| 18145 | const layout = union_ty.unionGetLayout(mod); | |
| 18240 | const layout = union_ty.unionGetLayout(pt); | |
| 18146 | 18241 | |
| 18147 | 18242 | const src_ty = self.typeOf(extra.init); |
| 18148 | 18243 | const src_mcv = try self.resolveInst(extra.init); |
| 18149 | 18244 | if (layout.tag_size == 0) { |
| 18150 | if (layout.abi_size <= src_ty.abiSize(mod) and | |
| 18245 | if (layout.abi_size <= src_ty.abiSize(pt) and | |
| 18151 | 18246 | self.reuseOperand(inst, extra.init, 0, src_mcv)) break :result src_mcv; |
| 18152 | 18247 | |
| 18153 | 18248 | const dst_mcv = try self.allocRegOrMem(inst, true); |
| ... | ... | @@ -18161,9 +18256,9 @@ fn airUnionInit(self: *Self, inst: Air.Inst.Index) !void { |
| 18161 | 18256 | const field_name = union_obj.loadTagType(ip).names.get(ip)[extra.field_index]; |
| 18162 | 18257 | const tag_ty = Type.fromInterned(union_obj.enum_tag_ty); |
| 18163 | 18258 | const field_index = tag_ty.enumFieldIndex(field_name, mod).?; |
| 18164 | const tag_val = try mod.enumValueFieldIndex(tag_ty, field_index); | |
| 18165 | const tag_int_val = try tag_val.intFromEnum(tag_ty, mod); | |
| 18166 | const tag_int = tag_int_val.toUnsignedInt(mod); | |
| 18259 | const tag_val = try pt.enumValueFieldIndex(tag_ty, field_index); | |
| 18260 | const tag_int_val = try tag_val.intFromEnum(tag_ty, pt); | |
| 18261 | const tag_int = tag_int_val.toUnsignedInt(pt); | |
| 18167 | 18262 | const tag_off: i32 = if (layout.tag_align.compare(.lt, layout.payload_align)) |
| 18168 | 18263 | @intCast(layout.payload_size) |
| 18169 | 18264 | else |
| ... | ... | @@ -18192,7 +18287,8 @@ fn airPrefetch(self: *Self, inst: Air.Inst.Index) !void { |
| 18192 | 18287 | } |
| 18193 | 18288 | |
| 18194 | 18289 | fn airMulAdd(self: *Self, inst: Air.Inst.Index) !void { |
| 18195 | const mod = self.bin_file.comp.module.?; | |
| 18290 | const pt = self.pt; | |
| 18291 | const mod = pt.zcu; | |
| 18196 | 18292 | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 18197 | 18293 | const extra = self.air.extraData(Air.Bin, pl_op.payload).data; |
| 18198 | 18294 | const ty = self.typeOfIndex(inst); |
| ... | ... | @@ -18205,7 +18301,7 @@ fn airMulAdd(self: *Self, inst: Air.Inst.Index) !void { |
| 18205 | 18301 | else => unreachable, |
| 18206 | 18302 | }) { |
| 18207 | 18303 | if (ty.zigTypeTag(mod) != .Float) return self.fail("TODO implement airMulAdd for {}", .{ |
| 18208 | ty.fmt(mod), | |
| 18304 | ty.fmt(pt), | |
| 18209 | 18305 | }); |
| 18210 | 18306 | |
| 18211 | 18307 | var callee_buf: ["__fma?".len]u8 = undefined; |
| ... | ... | @@ -18334,12 +18430,12 @@ fn airMulAdd(self: *Self, inst: Air.Inst.Index) !void { |
| 18334 | 18430 | else => unreachable, |
| 18335 | 18431 | } |
| 18336 | 18432 | else |
| 18337 | unreachable) orelse return self.fail("TODO implement airMulAdd for {}", .{ty.fmt(mod)}); | |
| 18433 | unreachable) orelse return self.fail("TODO implement airMulAdd for {}", .{ty.fmt(pt)}); | |
| 18338 | 18434 | |
| 18339 | 18435 | var mops: [3]MCValue = undefined; |
| 18340 | 18436 | for (order, mcvs) |mop_index, mcv| mops[mop_index - 1] = mcv; |
| 18341 | 18437 | |
| 18342 | const abi_size: u32 = @intCast(ty.abiSize(mod)); | |
| 18438 | const abi_size: u32 = @intCast(ty.abiSize(pt)); | |
| 18343 | 18439 | const mop1_reg = registerAlias(mops[0].getReg().?, abi_size); |
| 18344 | 18440 | const mop2_reg = registerAlias(mops[1].getReg().?, abi_size); |
| 18345 | 18441 | if (mops[2].isRegister()) try self.asmRegisterRegisterRegister( |
| ... | ... | @@ -18359,9 +18455,10 @@ fn airMulAdd(self: *Self, inst: Air.Inst.Index) !void { |
| 18359 | 18455 | } |
| 18360 | 18456 | |
| 18361 | 18457 | fn airVaStart(self: *Self, inst: Air.Inst.Index) !void { |
| 18362 | const mod = self.bin_file.comp.module.?; | |
| 18458 | const pt = self.pt; | |
| 18459 | const mod = pt.zcu; | |
| 18363 | 18460 | const va_list_ty = self.air.instructions.items(.data)[@intFromEnum(inst)].ty; |
| 18364 | const ptr_anyopaque_ty = try mod.singleMutPtrType(Type.anyopaque); | |
| 18461 | const ptr_anyopaque_ty = try pt.singleMutPtrType(Type.anyopaque); | |
| 18365 | 18462 | |
| 18366 | 18463 | const result: MCValue = switch (abi.resolveCallingConvention( |
| 18367 | 18464 | self.fn_type.fnCallingConvention(mod), |
| ... | ... | @@ -18369,7 +18466,7 @@ fn airVaStart(self: *Self, inst: Air.Inst.Index) !void { |
| 18369 | 18466 | )) { |
| 18370 | 18467 | .SysV => result: { |
| 18371 | 18468 | const info = self.va_info.sysv; |
| 18372 | const dst_fi = try self.allocFrameIndex(FrameAlloc.initSpill(va_list_ty, mod)); | |
| 18469 | const dst_fi = try self.allocFrameIndex(FrameAlloc.initSpill(va_list_ty, pt)); | |
| 18373 | 18470 | var field_off: u31 = 0; |
| 18374 | 18471 | // gp_offset: c_uint, |
| 18375 | 18472 | try self.genSetMem( |
| ... | ... | @@ -18379,7 +18476,7 @@ fn airVaStart(self: *Self, inst: Air.Inst.Index) !void { |
| 18379 | 18476 | .{ .immediate = info.gp_count * 8 }, |
| 18380 | 18477 | .{}, |
| 18381 | 18478 | ); |
| 18382 | field_off += @intCast(Type.c_uint.abiSize(mod)); | |
| 18479 | field_off += @intCast(Type.c_uint.abiSize(pt)); | |
| 18383 | 18480 | // fp_offset: c_uint, |
| 18384 | 18481 | try self.genSetMem( |
| 18385 | 18482 | .{ .frame = dst_fi }, |
| ... | ... | @@ -18388,7 +18485,7 @@ fn airVaStart(self: *Self, inst: Air.Inst.Index) !void { |
| 18388 | 18485 | .{ .immediate = abi.SysV.c_abi_int_param_regs.len * 8 + info.fp_count * 16 }, |
| 18389 | 18486 | .{}, |
| 18390 | 18487 | ); |
| 18391 | field_off += @intCast(Type.c_uint.abiSize(mod)); | |
| 18488 | field_off += @intCast(Type.c_uint.abiSize(pt)); | |
| 18392 | 18489 | // overflow_arg_area: *anyopaque, |
| 18393 | 18490 | try self.genSetMem( |
| 18394 | 18491 | .{ .frame = dst_fi }, |
| ... | ... | @@ -18397,7 +18494,7 @@ fn airVaStart(self: *Self, inst: Air.Inst.Index) !void { |
| 18397 | 18494 | .{ .lea_frame = info.overflow_arg_area }, |
| 18398 | 18495 | .{}, |
| 18399 | 18496 | ); |
| 18400 | field_off += @intCast(ptr_anyopaque_ty.abiSize(mod)); | |
| 18497 | field_off += @intCast(ptr_anyopaque_ty.abiSize(pt)); | |
| 18401 | 18498 | // reg_save_area: *anyopaque, |
| 18402 | 18499 | try self.genSetMem( |
| 18403 | 18500 | .{ .frame = dst_fi }, |
| ... | ... | @@ -18406,7 +18503,7 @@ fn airVaStart(self: *Self, inst: Air.Inst.Index) !void { |
| 18406 | 18503 | .{ .lea_frame = info.reg_save_area }, |
| 18407 | 18504 | .{}, |
| 18408 | 18505 | ); |
| 18409 | field_off += @intCast(ptr_anyopaque_ty.abiSize(mod)); | |
| 18506 | field_off += @intCast(ptr_anyopaque_ty.abiSize(pt)); | |
| 18410 | 18507 | break :result .{ .load_frame = .{ .index = dst_fi } }; |
| 18411 | 18508 | }, |
| 18412 | 18509 | .Win64 => return self.fail("TODO implement c_va_start for Win64", .{}), |
| ... | ... | @@ -18416,11 +18513,12 @@ fn airVaStart(self: *Self, inst: Air.Inst.Index) !void { |
| 18416 | 18513 | } |
| 18417 | 18514 | |
| 18418 | 18515 | fn airVaArg(self: *Self, inst: Air.Inst.Index) !void { |
| 18419 | const mod = self.bin_file.comp.module.?; | |
| 18516 | const pt = self.pt; | |
| 18517 | const mod = pt.zcu; | |
| 18420 | 18518 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 18421 | 18519 | const ty = self.typeOfIndex(inst); |
| 18422 | 18520 | const promote_ty = self.promoteVarArg(ty); |
| 18423 | const ptr_anyopaque_ty = try mod.singleMutPtrType(Type.anyopaque); | |
| 18521 | const ptr_anyopaque_ty = try pt.singleMutPtrType(Type.anyopaque); | |
| 18424 | 18522 | const unused = self.liveness.isUnused(inst); |
| 18425 | 18523 | |
| 18426 | 18524 | const result: MCValue = switch (abi.resolveCallingConvention( |
| ... | ... | @@ -18454,7 +18552,7 @@ fn airVaArg(self: *Self, inst: Air.Inst.Index) !void { |
| 18454 | 18552 | const overflow_arg_area: MCValue = .{ .indirect = .{ .reg = ptr_arg_list_reg, .off = 8 } }; |
| 18455 | 18553 | const reg_save_area: MCValue = .{ .indirect = .{ .reg = ptr_arg_list_reg, .off = 16 } }; |
| 18456 | 18554 | |
| 18457 | const classes = mem.sliceTo(&abi.classifySystemV(promote_ty, mod, self.target.*, .arg), .none); | |
| 18555 | const classes = mem.sliceTo(&abi.classifySystemV(promote_ty, pt, self.target.*, .arg), .none); | |
| 18458 | 18556 | switch (classes[0]) { |
| 18459 | 18557 | .integer => { |
| 18460 | 18558 | assert(classes.len == 1); |
| ... | ... | @@ -18489,7 +18587,7 @@ fn airVaArg(self: *Self, inst: Air.Inst.Index) !void { |
| 18489 | 18587 | .base = .{ .reg = addr_reg }, |
| 18490 | 18588 | .mod = .{ .rm = .{ |
| 18491 | 18589 | .size = .qword, |
| 18492 | .disp = @intCast(@max(promote_ty.abiSize(mod), 8)), | |
| 18590 | .disp = @intCast(@max(promote_ty.abiSize(pt), 8)), | |
| 18493 | 18591 | } }, |
| 18494 | 18592 | }); |
| 18495 | 18593 | try self.genCopy( |
| ... | ... | @@ -18537,7 +18635,7 @@ fn airVaArg(self: *Self, inst: Air.Inst.Index) !void { |
| 18537 | 18635 | .base = .{ .reg = addr_reg }, |
| 18538 | 18636 | .mod = .{ .rm = .{ |
| 18539 | 18637 | .size = .qword, |
| 18540 | .disp = @intCast(@max(promote_ty.abiSize(mod), 8)), | |
| 18638 | .disp = @intCast(@max(promote_ty.abiSize(pt), 8)), | |
| 18541 | 18639 | } }, |
| 18542 | 18640 | }); |
| 18543 | 18641 | try self.genCopy( |
| ... | ... | @@ -18557,7 +18655,7 @@ fn airVaArg(self: *Self, inst: Air.Inst.Index) !void { |
| 18557 | 18655 | unreachable; |
| 18558 | 18656 | }, |
| 18559 | 18657 | else => return self.fail("TODO implement c_va_arg for {} on SysV", .{ |
| 18560 | promote_ty.fmt(mod), | |
| 18658 | promote_ty.fmt(pt), | |
| 18561 | 18659 | }), |
| 18562 | 18660 | } |
| 18563 | 18661 | |
| ... | ... | @@ -18627,11 +18725,11 @@ fn airVaEnd(self: *Self, inst: Air.Inst.Index) !void { |
| 18627 | 18725 | } |
| 18628 | 18726 | |
| 18629 | 18727 | fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!MCValue { |
| 18630 | const mod = self.bin_file.comp.module.?; | |
| 18728 | const pt = self.pt; | |
| 18631 | 18729 | const ty = self.typeOf(ref); |
| 18632 | 18730 | |
| 18633 | 18731 | // If the type has no codegen bits, no need to store it. |
| 18634 | if (!ty.hasRuntimeBitsIgnoreComptime(mod)) return .none; | |
| 18732 | if (!ty.hasRuntimeBitsIgnoreComptime(pt)) return .none; | |
| 18635 | 18733 | |
| 18636 | 18734 | const mcv = if (ref.toIndex()) |inst| mcv: { |
| 18637 | 18735 | break :mcv self.inst_tracking.getPtr(inst).?.short; |
| ... | ... | @@ -18705,8 +18803,8 @@ fn limitImmediateType(self: *Self, operand: Air.Inst.Ref, comptime T: type) !MCV |
| 18705 | 18803 | } |
| 18706 | 18804 | |
| 18707 | 18805 | fn genTypedValue(self: *Self, val: Value) InnerError!MCValue { |
| 18708 | const mod = self.bin_file.comp.module.?; | |
| 18709 | return switch (try codegen.genTypedValue(self.bin_file, self.src_loc, val, self.owner.getDecl(mod))) { | |
| 18806 | const pt = self.pt; | |
| 18807 | return switch (try codegen.genTypedValue(self.bin_file, pt, self.src_loc, val, self.owner.getDecl(pt.zcu))) { | |
| 18710 | 18808 | .mcv => |mcv| switch (mcv) { |
| 18711 | 18809 | .none => .none, |
| 18712 | 18810 | .undef => .undef, |
| ... | ... | @@ -18745,7 +18843,8 @@ fn resolveCallingConventionValues( |
| 18745 | 18843 | var_args: []const Type, |
| 18746 | 18844 | stack_frame_base: FrameIndex, |
| 18747 | 18845 | ) !CallMCValues { |
| 18748 | const mod = self.bin_file.comp.module.?; | |
| 18846 | const pt = self.pt; | |
| 18847 | const mod = pt.zcu; | |
| 18749 | 18848 | const ip = &mod.intern_pool; |
| 18750 | 18849 | const cc = fn_info.cc; |
| 18751 | 18850 | const param_types = try self.gpa.alloc(Type, fn_info.param_types.len + var_args.len); |
| ... | ... | @@ -18788,7 +18887,7 @@ fn resolveCallingConventionValues( |
| 18788 | 18887 | .SysV => {}, |
| 18789 | 18888 | .Win64 => { |
| 18790 | 18889 | // Align the stack to 16bytes before allocating shadow stack space (if any). |
| 18791 | result.stack_byte_count += @intCast(4 * Type.usize.abiSize(mod)); | |
| 18890 | result.stack_byte_count += @intCast(4 * Type.usize.abiSize(pt)); | |
| 18792 | 18891 | }, |
| 18793 | 18892 | else => unreachable, |
| 18794 | 18893 | } |
| ... | ... | @@ -18796,7 +18895,7 @@ fn resolveCallingConventionValues( |
| 18796 | 18895 | // Return values |
| 18797 | 18896 | if (ret_ty.zigTypeTag(mod) == .NoReturn) { |
| 18798 | 18897 | result.return_value = InstTracking.init(.unreach); |
| 18799 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 18898 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 18800 | 18899 | // TODO: is this even possible for C calling convention? |
| 18801 | 18900 | result.return_value = InstTracking.init(.none); |
| 18802 | 18901 | } else { |
| ... | ... | @@ -18804,15 +18903,15 @@ fn resolveCallingConventionValues( |
| 18804 | 18903 | var ret_tracking_i: usize = 0; |
| 18805 | 18904 | |
| 18806 | 18905 | const classes = switch (resolved_cc) { |
| 18807 | .SysV => mem.sliceTo(&abi.classifySystemV(ret_ty, mod, self.target.*, .ret), .none), | |
| 18808 | .Win64 => &.{abi.classifyWindows(ret_ty, mod)}, | |
| 18906 | .SysV => mem.sliceTo(&abi.classifySystemV(ret_ty, pt, self.target.*, .ret), .none), | |
| 18907 | .Win64 => &.{abi.classifyWindows(ret_ty, pt)}, | |
| 18809 | 18908 | else => unreachable, |
| 18810 | 18909 | }; |
| 18811 | 18910 | for (classes) |class| switch (class) { |
| 18812 | 18911 | .integer => { |
| 18813 | 18912 | const ret_int_reg = registerAlias( |
| 18814 | 18913 | abi.getCAbiIntReturnRegs(resolved_cc)[ret_int_reg_i], |
| 18815 | @intCast(@min(ret_ty.abiSize(mod), 8)), | |
| 18914 | @intCast(@min(ret_ty.abiSize(pt), 8)), | |
| 18816 | 18915 | ); |
| 18817 | 18916 | ret_int_reg_i += 1; |
| 18818 | 18917 | |
| ... | ... | @@ -18822,7 +18921,7 @@ fn resolveCallingConventionValues( |
| 18822 | 18921 | .sse, .float, .float_combine, .win_i128 => { |
| 18823 | 18922 | const ret_sse_reg = registerAlias( |
| 18824 | 18923 | abi.getCAbiSseReturnRegs(resolved_cc)[ret_sse_reg_i], |
| 18825 | @intCast(ret_ty.abiSize(mod)), | |
| 18924 | @intCast(ret_ty.abiSize(pt)), | |
| 18826 | 18925 | ); |
| 18827 | 18926 | ret_sse_reg_i += 1; |
| 18828 | 18927 | |
| ... | ... | @@ -18865,7 +18964,7 @@ fn resolveCallingConventionValues( |
| 18865 | 18964 | |
| 18866 | 18965 | // Input params |
| 18867 | 18966 | for (param_types, result.args) |ty, *arg| { |
| 18868 | assert(ty.hasRuntimeBitsIgnoreComptime(mod)); | |
| 18967 | assert(ty.hasRuntimeBitsIgnoreComptime(pt)); | |
| 18869 | 18968 | switch (resolved_cc) { |
| 18870 | 18969 | .SysV => {}, |
| 18871 | 18970 | .Win64 => { |
| ... | ... | @@ -18879,8 +18978,8 @@ fn resolveCallingConventionValues( |
| 18879 | 18978 | var arg_mcv_i: usize = 0; |
| 18880 | 18979 | |
| 18881 | 18980 | const classes = switch (resolved_cc) { |
| 18882 | .SysV => mem.sliceTo(&abi.classifySystemV(ty, mod, self.target.*, .arg), .none), | |
| 18883 | .Win64 => &.{abi.classifyWindows(ty, mod)}, | |
| 18981 | .SysV => mem.sliceTo(&abi.classifySystemV(ty, pt, self.target.*, .arg), .none), | |
| 18982 | .Win64 => &.{abi.classifyWindows(ty, pt)}, | |
| 18884 | 18983 | else => unreachable, |
| 18885 | 18984 | }; |
| 18886 | 18985 | for (classes) |class| switch (class) { |
| ... | ... | @@ -18890,7 +18989,7 @@ fn resolveCallingConventionValues( |
| 18890 | 18989 | |
| 18891 | 18990 | const param_int_reg = registerAlias( |
| 18892 | 18991 | abi.getCAbiIntParamRegs(resolved_cc)[param_int_reg_i], |
| 18893 | @intCast(@min(ty.abiSize(mod), 8)), | |
| 18992 | @intCast(@min(ty.abiSize(pt), 8)), | |
| 18894 | 18993 | ); |
| 18895 | 18994 | param_int_reg_i += 1; |
| 18896 | 18995 | |
| ... | ... | @@ -18903,7 +19002,7 @@ fn resolveCallingConventionValues( |
| 18903 | 19002 | |
| 18904 | 19003 | const param_sse_reg = registerAlias( |
| 18905 | 19004 | abi.getCAbiSseParamRegs(resolved_cc)[param_sse_reg_i], |
| 18906 | @intCast(ty.abiSize(mod)), | |
| 19005 | @intCast(ty.abiSize(pt)), | |
| 18907 | 19006 | ); |
| 18908 | 19007 | param_sse_reg_i += 1; |
| 18909 | 19008 | |
| ... | ... | @@ -18916,7 +19015,7 @@ fn resolveCallingConventionValues( |
| 18916 | 19015 | .x87, .x87up, .complex_x87, .memory => break, |
| 18917 | 19016 | else => unreachable, |
| 18918 | 19017 | }, |
| 18919 | .Win64 => if (ty.abiSize(mod) > 8) { | |
| 19018 | .Win64 => if (ty.abiSize(pt) > 8) { | |
| 18920 | 19019 | const param_int_reg = |
| 18921 | 19020 | abi.getCAbiIntParamRegs(resolved_cc)[param_int_reg_i].to64(); |
| 18922 | 19021 | param_int_reg_i += 1; |
| ... | ... | @@ -18938,7 +19037,7 @@ fn resolveCallingConventionValues( |
| 18938 | 19037 | const frame_elems_len = ty.vectorLen(mod) - remaining_param_int_regs; |
| 18939 | 19038 | const frame_elem_size = mem.alignForward( |
| 18940 | 19039 | u64, |
| 18941 | ty.childType(mod).abiSize(mod), | |
| 19040 | ty.childType(mod).abiSize(pt), | |
| 18942 | 19041 | frame_elem_align, |
| 18943 | 19042 | ); |
| 18944 | 19043 | const frame_size: u31 = @intCast(frame_elems_len * frame_elem_size); |
| ... | ... | @@ -18962,9 +19061,9 @@ fn resolveCallingConventionValues( |
| 18962 | 19061 | continue; |
| 18963 | 19062 | } |
| 18964 | 19063 | |
| 18965 | const param_size: u31 = @intCast(ty.abiSize(mod)); | |
| 19064 | const param_size: u31 = @intCast(ty.abiSize(pt)); | |
| 18966 | 19065 | const param_align: u31 = |
| 18967 | @intCast(@max(ty.abiAlignment(mod).toByteUnits().?, 8)); | |
| 19066 | @intCast(@max(ty.abiAlignment(pt).toByteUnits().?, 8)); | |
| 18968 | 19067 | result.stack_byte_count = |
| 18969 | 19068 | mem.alignForward(u31, result.stack_byte_count, param_align); |
| 18970 | 19069 | arg.* = .{ .load_frame = .{ |
| ... | ... | @@ -18984,11 +19083,11 @@ fn resolveCallingConventionValues( |
| 18984 | 19083 | // Return values |
| 18985 | 19084 | if (ret_ty.zigTypeTag(mod) == .NoReturn) { |
| 18986 | 19085 | result.return_value = InstTracking.init(.unreach); |
| 18987 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 19086 | } else if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 18988 | 19087 | result.return_value = InstTracking.init(.none); |
| 18989 | 19088 | } else { |
| 18990 | 19089 | const ret_reg = abi.getCAbiIntReturnRegs(resolved_cc)[0]; |
| 18991 | const ret_ty_size: u31 = @intCast(ret_ty.abiSize(mod)); | |
| 19090 | const ret_ty_size: u31 = @intCast(ret_ty.abiSize(pt)); | |
| 18992 | 19091 | if (ret_ty_size <= 8 and !ret_ty.isRuntimeFloat()) { |
| 18993 | 19092 | const aliased_reg = registerAlias(ret_reg, ret_ty_size); |
| 18994 | 19093 | result.return_value = .{ .short = .{ .register = aliased_reg }, .long = .none }; |
| ... | ... | @@ -19003,12 +19102,12 @@ fn resolveCallingConventionValues( |
| 19003 | 19102 | |
| 19004 | 19103 | // Input params |
| 19005 | 19104 | for (param_types, result.args) |ty, *arg| { |
| 19006 | if (!ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 19105 | if (!ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 19007 | 19106 | arg.* = .none; |
| 19008 | 19107 | continue; |
| 19009 | 19108 | } |
| 19010 | const param_size: u31 = @intCast(ty.abiSize(mod)); | |
| 19011 | const param_align: u31 = @intCast(ty.abiAlignment(mod).toByteUnits().?); | |
| 19109 | const param_size: u31 = @intCast(ty.abiSize(pt)); | |
| 19110 | const param_align: u31 = @intCast(ty.abiAlignment(pt).toByteUnits().?); | |
| 19012 | 19111 | result.stack_byte_count = |
| 19013 | 19112 | mem.alignForward(u31, result.stack_byte_count, param_align); |
| 19014 | 19113 | arg.* = .{ .load_frame = .{ |
| ... | ... | @@ -19093,47 +19192,49 @@ fn registerAlias(reg: Register, size_bytes: u32) Register { |
| 19093 | 19192 | } |
| 19094 | 19193 | |
| 19095 | 19194 | fn memSize(self: *Self, ty: Type) Memory.Size { |
| 19096 | const mod = self.bin_file.comp.module.?; | |
| 19195 | const pt = self.pt; | |
| 19196 | const mod = pt.zcu; | |
| 19097 | 19197 | return switch (ty.zigTypeTag(mod)) { |
| 19098 | 19198 | .Float => Memory.Size.fromBitSize(ty.floatBits(self.target.*)), |
| 19099 | else => Memory.Size.fromSize(@intCast(ty.abiSize(mod))), | |
| 19199 | else => Memory.Size.fromSize(@intCast(ty.abiSize(pt))), | |
| 19100 | 19200 | }; |
| 19101 | 19201 | } |
| 19102 | 19202 | |
| 19103 | 19203 | fn splitType(self: *Self, ty: Type) ![2]Type { |
| 19104 | const mod = self.bin_file.comp.module.?; | |
| 19105 | const classes = mem.sliceTo(&abi.classifySystemV(ty, mod, self.target.*, .other), .none); | |
| 19204 | const pt = self.pt; | |
| 19205 | const classes = mem.sliceTo(&abi.classifySystemV(ty, pt, self.target.*, .other), .none); | |
| 19106 | 19206 | var parts: [2]Type = undefined; |
| 19107 | 19207 | if (classes.len == 2) for (&parts, classes, 0..) |*part, class, part_i| { |
| 19108 | 19208 | part.* = switch (class) { |
| 19109 | 19209 | .integer => switch (part_i) { |
| 19110 | 19210 | 0 => Type.u64, |
| 19111 | 19211 | 1 => part: { |
| 19112 | const elem_size = ty.abiAlignment(mod).minStrict(.@"8").toByteUnits().?; | |
| 19113 | const elem_ty = try mod.intType(.unsigned, @intCast(elem_size * 8)); | |
| 19114 | break :part switch (@divExact(ty.abiSize(mod) - 8, elem_size)) { | |
| 19212 | const elem_size = ty.abiAlignment(pt).minStrict(.@"8").toByteUnits().?; | |
| 19213 | const elem_ty = try pt.intType(.unsigned, @intCast(elem_size * 8)); | |
| 19214 | break :part switch (@divExact(ty.abiSize(pt) - 8, elem_size)) { | |
| 19115 | 19215 | 1 => elem_ty, |
| 19116 | else => |len| try mod.arrayType(.{ .len = len, .child = elem_ty.toIntern() }), | |
| 19216 | else => |len| try pt.arrayType(.{ .len = len, .child = elem_ty.toIntern() }), | |
| 19117 | 19217 | }; |
| 19118 | 19218 | }, |
| 19119 | 19219 | else => unreachable, |
| 19120 | 19220 | }, |
| 19121 | 19221 | .float => Type.f32, |
| 19122 | .float_combine => try mod.arrayType(.{ .len = 2, .child = .f32_type }), | |
| 19222 | .float_combine => try pt.arrayType(.{ .len = 2, .child = .f32_type }), | |
| 19123 | 19223 | .sse => Type.f64, |
| 19124 | 19224 | else => break, |
| 19125 | 19225 | }; |
| 19126 | } else if (parts[0].abiSize(mod) + parts[1].abiSize(mod) == ty.abiSize(mod)) return parts; | |
| 19127 | return self.fail("TODO implement splitType for {}", .{ty.fmt(mod)}); | |
| 19226 | } else if (parts[0].abiSize(pt) + parts[1].abiSize(pt) == ty.abiSize(pt)) return parts; | |
| 19227 | return self.fail("TODO implement splitType for {}", .{ty.fmt(pt)}); | |
| 19128 | 19228 | } |
| 19129 | 19229 | |
| 19130 | 19230 | /// Truncates the value in the register in place. |
| 19131 | 19231 | /// Clobbers any remaining bits. |
| 19132 | 19232 | fn truncateRegister(self: *Self, ty: Type, reg: Register) !void { |
| 19133 | const mod = self.bin_file.comp.module.?; | |
| 19233 | const pt = self.pt; | |
| 19234 | const mod = pt.zcu; | |
| 19134 | 19235 | const int_info = if (ty.isAbiInt(mod)) ty.intInfo(mod) else std.builtin.Type.Int{ |
| 19135 | 19236 | .signedness = .unsigned, |
| 19136 | .bits = @intCast(ty.bitSize(mod)), | |
| 19237 | .bits = @intCast(ty.bitSize(pt)), | |
| 19137 | 19238 | }; |
| 19138 | 19239 | const shift = math.cast(u6, 64 - int_info.bits % 64) orelse return; |
| 19139 | 19240 | try self.spillEflagsIfOccupied(); |
| ... | ... | @@ -19177,8 +19278,9 @@ fn truncateRegister(self: *Self, ty: Type, reg: Register) !void { |
| 19177 | 19278 | } |
| 19178 | 19279 | |
| 19179 | 19280 | fn regBitSize(self: *Self, ty: Type) u64 { |
| 19180 | const mod = self.bin_file.comp.module.?; | |
| 19181 | const abi_size = ty.abiSize(mod); | |
| 19281 | const pt = self.pt; | |
| 19282 | const mod = pt.zcu; | |
| 19283 | const abi_size = ty.abiSize(pt); | |
| 19182 | 19284 | return switch (ty.zigTypeTag(mod)) { |
| 19183 | 19285 | else => switch (abi_size) { |
| 19184 | 19286 | 1 => 8, |
| ... | ... | @@ -19196,8 +19298,7 @@ fn regBitSize(self: *Self, ty: Type) u64 { |
| 19196 | 19298 | } |
| 19197 | 19299 | |
| 19198 | 19300 | fn regExtraBits(self: *Self, ty: Type) u64 { |
| 19199 | const mod = self.bin_file.comp.module.?; | |
| 19200 | return self.regBitSize(ty) - ty.bitSize(mod); | |
| 19301 | return self.regBitSize(ty) - ty.bitSize(self.pt); | |
| 19201 | 19302 | } |
| 19202 | 19303 | |
| 19203 | 19304 | fn hasFeature(self: *Self, feature: Target.x86.Feature) bool { |
| ... | ... | @@ -19211,12 +19312,14 @@ fn hasAllFeatures(self: *Self, features: anytype) bool { |
| 19211 | 19312 | } |
| 19212 | 19313 | |
| 19213 | 19314 | fn typeOf(self: *Self, inst: Air.Inst.Ref) Type { |
| 19214 | const mod = self.bin_file.comp.module.?; | |
| 19315 | const pt = self.pt; | |
| 19316 | const mod = pt.zcu; | |
| 19215 | 19317 | return self.air.typeOf(inst, &mod.intern_pool); |
| 19216 | 19318 | } |
| 19217 | 19319 | |
| 19218 | 19320 | fn typeOfIndex(self: *Self, inst: Air.Inst.Index) Type { |
| 19219 | const mod = self.bin_file.comp.module.?; | |
| 19321 | const pt = self.pt; | |
| 19322 | const mod = pt.zcu; | |
| 19220 | 19323 | return self.air.typeOfIndex(inst, &mod.intern_pool); |
| 19221 | 19324 | } |
| 19222 | 19325 | |
| ... | ... | @@ -19268,7 +19371,8 @@ fn floatLibcAbiSuffix(ty: Type) []const u8 { |
| 19268 | 19371 | } |
| 19269 | 19372 | |
| 19270 | 19373 | fn promoteInt(self: *Self, ty: Type) Type { |
| 19271 | const mod = self.bin_file.comp.module.?; | |
| 19374 | const pt = self.pt; | |
| 19375 | const mod = pt.zcu; | |
| 19272 | 19376 | const int_info: InternPool.Key.IntType = switch (ty.toIntern()) { |
| 19273 | 19377 | .bool_type => .{ .signedness = .unsigned, .bits = 1 }, |
| 19274 | 19378 | else => if (ty.isAbiInt(mod)) ty.intInfo(mod) else return ty, |
src/arch/x86_64/Lower.zig+2-4| ... | ... | @@ -8,7 +8,7 @@ allocator: Allocator, |
| 8 | 8 | mir: Mir, |
| 9 | 9 | cc: std.builtin.CallingConvention, |
| 10 | 10 | err_msg: ?*ErrorMsg = null, |
| 11 | src_loc: Module.LazySrcLoc, | |
| 11 | src_loc: Zcu.LazySrcLoc, | |
| 12 | 12 | result_insts_len: u8 = undefined, |
| 13 | 13 | result_relocs_len: u8 = undefined, |
| 14 | 14 | result_insts: [ |
| ... | ... | @@ -657,7 +657,7 @@ const std = @import("std"); |
| 657 | 657 | |
| 658 | 658 | const Air = @import("../../Air.zig"); |
| 659 | 659 | const Allocator = std.mem.Allocator; |
| 660 | const ErrorMsg = Module.ErrorMsg; | |
| 660 | const ErrorMsg = Zcu.ErrorMsg; | |
| 661 | 661 | const Immediate = bits.Immediate; |
| 662 | 662 | const Instruction = encoder.Instruction; |
| 663 | 663 | const Lower = @This(); |
| ... | ... | @@ -665,8 +665,6 @@ const Memory = Instruction.Memory; |
| 665 | 665 | const Mir = @import("Mir.zig"); |
| 666 | 666 | const Mnemonic = Instruction.Mnemonic; |
| 667 | 667 | const Zcu = @import("../../Zcu.zig"); |
| 668 | /// Deprecated. | |
| 669 | const Module = Zcu; | |
| 670 | 668 | const Operand = Instruction.Operand; |
| 671 | 669 | const Prefix = Instruction.Prefix; |
| 672 | 670 | const Register = bits.Register; |
src/arch/x86_64/abi.zig+35-35| ... | ... | @@ -44,7 +44,7 @@ pub const Class = enum { |
| 44 | 44 | } |
| 45 | 45 | }; |
| 46 | 46 | |
| 47 | pub fn classifyWindows(ty: Type, zcu: *Zcu) Class { | |
| 47 | pub fn classifyWindows(ty: Type, pt: Zcu.PerThread) Class { | |
| 48 | 48 | // https://docs.microsoft.com/en-gb/cpp/build/x64-calling-convention?view=vs-2017 |
| 49 | 49 | // "There's a strict one-to-one correspondence between a function call's arguments |
| 50 | 50 | // and the registers used for those arguments. Any argument that doesn't fit in 8 |
| ... | ... | @@ -53,7 +53,7 @@ pub fn classifyWindows(ty: Type, zcu: *Zcu) Class { |
| 53 | 53 | // "All floating point operations are done using the 16 XMM registers." |
| 54 | 54 | // "Structs and unions of size 8, 16, 32, or 64 bits, and __m64 types, are passed |
| 55 | 55 | // as if they were integers of the same size." |
| 56 | switch (ty.zigTypeTag(zcu)) { | |
| 56 | switch (ty.zigTypeTag(pt.zcu)) { | |
| 57 | 57 | .Pointer, |
| 58 | 58 | .Int, |
| 59 | 59 | .Bool, |
| ... | ... | @@ -68,12 +68,12 @@ pub fn classifyWindows(ty: Type, zcu: *Zcu) Class { |
| 68 | 68 | .ErrorUnion, |
| 69 | 69 | .AnyFrame, |
| 70 | 70 | .Frame, |
| 71 | => switch (ty.abiSize(zcu)) { | |
| 71 | => switch (ty.abiSize(pt)) { | |
| 72 | 72 | 0 => unreachable, |
| 73 | 73 | 1, 2, 4, 8 => return .integer, |
| 74 | else => switch (ty.zigTypeTag(zcu)) { | |
| 74 | else => switch (ty.zigTypeTag(pt.zcu)) { | |
| 75 | 75 | .Int => return .win_i128, |
| 76 | .Struct, .Union => if (ty.containerLayout(zcu) == .@"packed") { | |
| 76 | .Struct, .Union => if (ty.containerLayout(pt.zcu) == .@"packed") { | |
| 77 | 77 | return .win_i128; |
| 78 | 78 | } else { |
| 79 | 79 | return .memory; |
| ... | ... | @@ -100,14 +100,14 @@ pub const Context = enum { ret, arg, field, other }; |
| 100 | 100 | |
| 101 | 101 | /// There are a maximum of 8 possible return slots. Returned values are in |
| 102 | 102 | /// the beginning of the array; unused slots are filled with .none. |
| 103 | pub fn classifySystemV(ty: Type, zcu: *Zcu, target: std.Target, ctx: Context) [8]Class { | |
| 103 | pub fn classifySystemV(ty: Type, pt: Zcu.PerThread, target: std.Target, ctx: Context) [8]Class { | |
| 104 | 104 | const memory_class = [_]Class{ |
| 105 | 105 | .memory, .none, .none, .none, |
| 106 | 106 | .none, .none, .none, .none, |
| 107 | 107 | }; |
| 108 | 108 | var result = [1]Class{.none} ** 8; |
| 109 | switch (ty.zigTypeTag(zcu)) { | |
| 110 | .Pointer => switch (ty.ptrSize(zcu)) { | |
| 109 | switch (ty.zigTypeTag(pt.zcu)) { | |
| 110 | .Pointer => switch (ty.ptrSize(pt.zcu)) { | |
| 111 | 111 | .Slice => { |
| 112 | 112 | result[0] = .integer; |
| 113 | 113 | result[1] = .integer; |
| ... | ... | @@ -119,7 +119,7 @@ pub fn classifySystemV(ty: Type, zcu: *Zcu, target: std.Target, ctx: Context) [8 |
| 119 | 119 | }, |
| 120 | 120 | }, |
| 121 | 121 | .Int, .Enum, .ErrorSet => { |
| 122 | const bits = ty.intInfo(zcu).bits; | |
| 122 | const bits = ty.intInfo(pt.zcu).bits; | |
| 123 | 123 | if (bits <= 64) { |
| 124 | 124 | result[0] = .integer; |
| 125 | 125 | return result; |
| ... | ... | @@ -185,8 +185,8 @@ pub fn classifySystemV(ty: Type, zcu: *Zcu, target: std.Target, ctx: Context) [8 |
| 185 | 185 | else => unreachable, |
| 186 | 186 | }, |
| 187 | 187 | .Vector => { |
| 188 | const elem_ty = ty.childType(zcu); | |
| 189 | const bits = elem_ty.bitSize(zcu) * ty.arrayLen(zcu); | |
| 188 | const elem_ty = ty.childType(pt.zcu); | |
| 189 | const bits = elem_ty.bitSize(pt) * ty.arrayLen(pt.zcu); | |
| 190 | 190 | if (elem_ty.toIntern() == .bool_type) { |
| 191 | 191 | if (bits <= 32) return .{ |
| 192 | 192 | .integer, .none, .none, .none, |
| ... | ... | @@ -250,7 +250,7 @@ pub fn classifySystemV(ty: Type, zcu: *Zcu, target: std.Target, ctx: Context) [8 |
| 250 | 250 | return memory_class; |
| 251 | 251 | }, |
| 252 | 252 | .Optional => { |
| 253 | if (ty.isPtrLikeOptional(zcu)) { | |
| 253 | if (ty.isPtrLikeOptional(pt.zcu)) { | |
| 254 | 254 | result[0] = .integer; |
| 255 | 255 | return result; |
| 256 | 256 | } |
| ... | ... | @@ -261,8 +261,8 @@ pub fn classifySystemV(ty: Type, zcu: *Zcu, target: std.Target, ctx: Context) [8 |
| 261 | 261 | // it contains unaligned fields, it has class MEMORY" |
| 262 | 262 | // "If the size of the aggregate exceeds a single eightbyte, each is classified |
| 263 | 263 | // separately.". |
| 264 | const ty_size = ty.abiSize(zcu); | |
| 265 | switch (ty.containerLayout(zcu)) { | |
| 264 | const ty_size = ty.abiSize(pt); | |
| 265 | switch (ty.containerLayout(pt.zcu)) { | |
| 266 | 266 | .auto, .@"extern" => {}, |
| 267 | 267 | .@"packed" => { |
| 268 | 268 | assert(ty_size <= 16); |
| ... | ... | @@ -274,10 +274,10 @@ pub fn classifySystemV(ty: Type, zcu: *Zcu, target: std.Target, ctx: Context) [8 |
| 274 | 274 | if (ty_size > 64) |
| 275 | 275 | return memory_class; |
| 276 | 276 | |
| 277 | _ = if (zcu.typeToStruct(ty)) |loaded_struct| | |
| 278 | classifySystemVStruct(&result, 0, loaded_struct, zcu, target) | |
| 279 | else if (zcu.typeToUnion(ty)) |loaded_union| | |
| 280 | classifySystemVUnion(&result, 0, loaded_union, zcu, target) | |
| 277 | _ = if (pt.zcu.typeToStruct(ty)) |loaded_struct| | |
| 278 | classifySystemVStruct(&result, 0, loaded_struct, pt, target) | |
| 279 | else if (pt.zcu.typeToUnion(ty)) |loaded_union| | |
| 280 | classifySystemVUnion(&result, 0, loaded_union, pt, target) | |
| 281 | 281 | else |
| 282 | 282 | unreachable; |
| 283 | 283 | |
| ... | ... | @@ -306,7 +306,7 @@ pub fn classifySystemV(ty: Type, zcu: *Zcu, target: std.Target, ctx: Context) [8 |
| 306 | 306 | return result; |
| 307 | 307 | }, |
| 308 | 308 | .Array => { |
| 309 | const ty_size = ty.abiSize(zcu); | |
| 309 | const ty_size = ty.abiSize(pt); | |
| 310 | 310 | if (ty_size <= 8) { |
| 311 | 311 | result[0] = .integer; |
| 312 | 312 | return result; |
| ... | ... | @@ -326,10 +326,10 @@ fn classifySystemVStruct( |
| 326 | 326 | result: *[8]Class, |
| 327 | 327 | starting_byte_offset: u64, |
| 328 | 328 | loaded_struct: InternPool.LoadedStructType, |
| 329 | zcu: *Zcu, | |
| 329 | pt: Zcu.PerThread, | |
| 330 | 330 | target: std.Target, |
| 331 | 331 | ) u64 { |
| 332 | const ip = &zcu.intern_pool; | |
| 332 | const ip = &pt.zcu.intern_pool; | |
| 333 | 333 | var byte_offset = starting_byte_offset; |
| 334 | 334 | var field_it = loaded_struct.iterateRuntimeOrder(ip); |
| 335 | 335 | while (field_it.next()) |field_index| { |
| ... | ... | @@ -338,29 +338,29 @@ fn classifySystemVStruct( |
| 338 | 338 | byte_offset = std.mem.alignForward( |
| 339 | 339 | u64, |
| 340 | 340 | byte_offset, |
| 341 | field_align.toByteUnits() orelse field_ty.abiAlignment(zcu).toByteUnits().?, | |
| 341 | field_align.toByteUnits() orelse field_ty.abiAlignment(pt).toByteUnits().?, | |
| 342 | 342 | ); |
| 343 | if (zcu.typeToStruct(field_ty)) |field_loaded_struct| { | |
| 343 | if (pt.zcu.typeToStruct(field_ty)) |field_loaded_struct| { | |
| 344 | 344 | switch (field_loaded_struct.layout) { |
| 345 | 345 | .auto, .@"extern" => { |
| 346 | byte_offset = classifySystemVStruct(result, byte_offset, field_loaded_struct, zcu, target); | |
| 346 | byte_offset = classifySystemVStruct(result, byte_offset, field_loaded_struct, pt, target); | |
| 347 | 347 | continue; |
| 348 | 348 | }, |
| 349 | 349 | .@"packed" => {}, |
| 350 | 350 | } |
| 351 | } else if (zcu.typeToUnion(field_ty)) |field_loaded_union| { | |
| 351 | } else if (pt.zcu.typeToUnion(field_ty)) |field_loaded_union| { | |
| 352 | 352 | switch (field_loaded_union.getLayout(ip)) { |
| 353 | 353 | .auto, .@"extern" => { |
| 354 | byte_offset = classifySystemVUnion(result, byte_offset, field_loaded_union, zcu, target); | |
| 354 | byte_offset = classifySystemVUnion(result, byte_offset, field_loaded_union, pt, target); | |
| 355 | 355 | continue; |
| 356 | 356 | }, |
| 357 | 357 | .@"packed" => {}, |
| 358 | 358 | } |
| 359 | 359 | } |
| 360 | const field_classes = std.mem.sliceTo(&classifySystemV(field_ty, zcu, target, .field), .none); | |
| 360 | const field_classes = std.mem.sliceTo(&classifySystemV(field_ty, pt, target, .field), .none); | |
| 361 | 361 | for (result[@intCast(byte_offset / 8)..][0..field_classes.len], field_classes) |*result_class, field_class| |
| 362 | 362 | result_class.* = result_class.combineSystemV(field_class); |
| 363 | byte_offset += field_ty.abiSize(zcu); | |
| 363 | byte_offset += field_ty.abiSize(pt); | |
| 364 | 364 | } |
| 365 | 365 | const final_byte_offset = starting_byte_offset + loaded_struct.size(ip).*; |
| 366 | 366 | std.debug.assert(final_byte_offset == std.mem.alignForward( |
| ... | ... | @@ -375,30 +375,30 @@ fn classifySystemVUnion( |
| 375 | 375 | result: *[8]Class, |
| 376 | 376 | starting_byte_offset: u64, |
| 377 | 377 | loaded_union: InternPool.LoadedUnionType, |
| 378 | zcu: *Zcu, | |
| 378 | pt: Zcu.PerThread, | |
| 379 | 379 | target: std.Target, |
| 380 | 380 | ) u64 { |
| 381 | const ip = &zcu.intern_pool; | |
| 381 | const ip = &pt.zcu.intern_pool; | |
| 382 | 382 | for (0..loaded_union.field_types.len) |field_index| { |
| 383 | 383 | const field_ty = Type.fromInterned(loaded_union.field_types.get(ip)[field_index]); |
| 384 | if (zcu.typeToStruct(field_ty)) |field_loaded_struct| { | |
| 384 | if (pt.zcu.typeToStruct(field_ty)) |field_loaded_struct| { | |
| 385 | 385 | switch (field_loaded_struct.layout) { |
| 386 | 386 | .auto, .@"extern" => { |
| 387 | _ = classifySystemVStruct(result, starting_byte_offset, field_loaded_struct, zcu, target); | |
| 387 | _ = classifySystemVStruct(result, starting_byte_offset, field_loaded_struct, pt, target); | |
| 388 | 388 | continue; |
| 389 | 389 | }, |
| 390 | 390 | .@"packed" => {}, |
| 391 | 391 | } |
| 392 | } else if (zcu.typeToUnion(field_ty)) |field_loaded_union| { | |
| 392 | } else if (pt.zcu.typeToUnion(field_ty)) |field_loaded_union| { | |
| 393 | 393 | switch (field_loaded_union.getLayout(ip)) { |
| 394 | 394 | .auto, .@"extern" => { |
| 395 | _ = classifySystemVUnion(result, starting_byte_offset, field_loaded_union, zcu, target); | |
| 395 | _ = classifySystemVUnion(result, starting_byte_offset, field_loaded_union, pt, target); | |
| 396 | 396 | continue; |
| 397 | 397 | }, |
| 398 | 398 | .@"packed" => {}, |
| 399 | 399 | } |
| 400 | 400 | } |
| 401 | const field_classes = std.mem.sliceTo(&classifySystemV(field_ty, zcu, target, .field), .none); | |
| 401 | const field_classes = std.mem.sliceTo(&classifySystemV(field_ty, pt, target, .field), .none); | |
| 402 | 402 | for (result[@intCast(starting_byte_offset / 8)..][0..field_classes.len], field_classes) |*result_class, field_class| |
| 403 | 403 | result_class.* = result_class.combineSystemV(field_class); |
| 404 | 404 | } |
src/codegen.zig+140-129| ... | ... | @@ -13,12 +13,10 @@ const trace = @import("tracy.zig").trace; |
| 13 | 13 | const Air = @import("Air.zig"); |
| 14 | 14 | const Allocator = mem.Allocator; |
| 15 | 15 | const Compilation = @import("Compilation.zig"); |
| 16 | const ErrorMsg = Module.ErrorMsg; | |
| 16 | const ErrorMsg = Zcu.ErrorMsg; | |
| 17 | 17 | const InternPool = @import("InternPool.zig"); |
| 18 | 18 | const Liveness = @import("Liveness.zig"); |
| 19 | 19 | const Zcu = @import("Zcu.zig"); |
| 20 | /// Deprecated. | |
| 21 | const Module = Zcu; | |
| 22 | 20 | const Target = std.Target; |
| 23 | 21 | const Type = @import("Type.zig"); |
| 24 | 22 | const Value = @import("Value.zig"); |
| ... | ... | @@ -47,14 +45,15 @@ pub const DebugInfoOutput = union(enum) { |
| 47 | 45 | |
| 48 | 46 | pub fn generateFunction( |
| 49 | 47 | lf: *link.File, |
| 50 | src_loc: Module.LazySrcLoc, | |
| 48 | pt: Zcu.PerThread, | |
| 49 | src_loc: Zcu.LazySrcLoc, | |
| 51 | 50 | func_index: InternPool.Index, |
| 52 | 51 | air: Air, |
| 53 | 52 | liveness: Liveness, |
| 54 | 53 | code: *std.ArrayList(u8), |
| 55 | 54 | debug_output: DebugInfoOutput, |
| 56 | 55 | ) CodeGenError!Result { |
| 57 | const zcu = lf.comp.module.?; | |
| 56 | const zcu = pt.zcu; | |
| 58 | 57 | const func = zcu.funcInfo(func_index); |
| 59 | 58 | const decl = zcu.declPtr(func.owner_decl); |
| 60 | 59 | const namespace = zcu.namespacePtr(decl.src_namespace); |
| ... | ... | @@ -62,35 +61,36 @@ pub fn generateFunction( |
| 62 | 61 | switch (target.cpu.arch) { |
| 63 | 62 | .arm, |
| 64 | 63 | .armeb, |
| 65 | => return @import("arch/arm/CodeGen.zig").generate(lf, src_loc, func_index, air, liveness, code, debug_output), | |
| 64 | => return @import("arch/arm/CodeGen.zig").generate(lf, pt, src_loc, func_index, air, liveness, code, debug_output), | |
| 66 | 65 | .aarch64, |
| 67 | 66 | .aarch64_be, |
| 68 | 67 | .aarch64_32, |
| 69 | => return @import("arch/aarch64/CodeGen.zig").generate(lf, src_loc, func_index, air, liveness, code, debug_output), | |
| 70 | .riscv64 => return @import("arch/riscv64/CodeGen.zig").generate(lf, src_loc, func_index, air, liveness, code, debug_output), | |
| 71 | .sparc64 => return @import("arch/sparc64/CodeGen.zig").generate(lf, src_loc, func_index, air, liveness, code, debug_output), | |
| 72 | .x86_64 => return @import("arch/x86_64/CodeGen.zig").generate(lf, src_loc, func_index, air, liveness, code, debug_output), | |
| 68 | => return @import("arch/aarch64/CodeGen.zig").generate(lf, pt, src_loc, func_index, air, liveness, code, debug_output), | |
| 69 | .riscv64 => return @import("arch/riscv64/CodeGen.zig").generate(lf, pt, src_loc, func_index, air, liveness, code, debug_output), | |
| 70 | .sparc64 => return @import("arch/sparc64/CodeGen.zig").generate(lf, pt, src_loc, func_index, air, liveness, code, debug_output), | |
| 71 | .x86_64 => return @import("arch/x86_64/CodeGen.zig").generate(lf, pt, src_loc, func_index, air, liveness, code, debug_output), | |
| 73 | 72 | .wasm32, |
| 74 | 73 | .wasm64, |
| 75 | => return @import("arch/wasm/CodeGen.zig").generate(lf, src_loc, func_index, air, liveness, code, debug_output), | |
| 74 | => return @import("arch/wasm/CodeGen.zig").generate(lf, pt, src_loc, func_index, air, liveness, code, debug_output), | |
| 76 | 75 | else => unreachable, |
| 77 | 76 | } |
| 78 | 77 | } |
| 79 | 78 | |
| 80 | 79 | pub fn generateLazyFunction( |
| 81 | 80 | lf: *link.File, |
| 82 | src_loc: Module.LazySrcLoc, | |
| 81 | pt: Zcu.PerThread, | |
| 82 | src_loc: Zcu.LazySrcLoc, | |
| 83 | 83 | lazy_sym: link.File.LazySymbol, |
| 84 | 84 | code: *std.ArrayList(u8), |
| 85 | 85 | debug_output: DebugInfoOutput, |
| 86 | 86 | ) CodeGenError!Result { |
| 87 | const zcu = lf.comp.module.?; | |
| 87 | const zcu = pt.zcu; | |
| 88 | 88 | const decl_index = lazy_sym.ty.getOwnerDecl(zcu); |
| 89 | 89 | const decl = zcu.declPtr(decl_index); |
| 90 | 90 | const namespace = zcu.namespacePtr(decl.src_namespace); |
| 91 | 91 | const target = namespace.fileScope(zcu).mod.resolved_target.result; |
| 92 | 92 | switch (target.cpu.arch) { |
| 93 | .x86_64 => return @import("arch/x86_64/CodeGen.zig").generateLazy(lf, src_loc, lazy_sym, code, debug_output), | |
| 93 | .x86_64 => return @import("arch/x86_64/CodeGen.zig").generateLazy(lf, pt, src_loc, lazy_sym, code, debug_output), | |
| 94 | 94 | else => unreachable, |
| 95 | 95 | } |
| 96 | 96 | } |
| ... | ... | @@ -105,7 +105,8 @@ fn writeFloat(comptime F: type, f: F, target: Target, endian: std.builtin.Endian |
| 105 | 105 | |
| 106 | 106 | pub fn generateLazySymbol( |
| 107 | 107 | bin_file: *link.File, |
| 108 | src_loc: Module.LazySrcLoc, | |
| 108 | pt: Zcu.PerThread, | |
| 109 | src_loc: Zcu.LazySrcLoc, | |
| 109 | 110 | lazy_sym: link.File.LazySymbol, |
| 110 | 111 | // TODO don't use an "out" parameter like this; put it in the result instead |
| 111 | 112 | alignment: *Alignment, |
| ... | ... | @@ -119,25 +120,24 @@ pub fn generateLazySymbol( |
| 119 | 120 | defer tracy.end(); |
| 120 | 121 | |
| 121 | 122 | const comp = bin_file.comp; |
| 122 | const zcu = comp.module.?; | |
| 123 | const ip = &zcu.intern_pool; | |
| 123 | const ip = &pt.zcu.intern_pool; | |
| 124 | 124 | const target = comp.root_mod.resolved_target.result; |
| 125 | 125 | const endian = target.cpu.arch.endian(); |
| 126 | 126 | const gpa = comp.gpa; |
| 127 | 127 | |
| 128 | 128 | log.debug("generateLazySymbol: kind = {s}, ty = {}", .{ |
| 129 | 129 | @tagName(lazy_sym.kind), |
| 130 | lazy_sym.ty.fmt(zcu), | |
| 130 | lazy_sym.ty.fmt(pt), | |
| 131 | 131 | }); |
| 132 | 132 | |
| 133 | 133 | if (lazy_sym.kind == .code) { |
| 134 | 134 | alignment.* = target_util.defaultFunctionAlignment(target); |
| 135 | return generateLazyFunction(bin_file, src_loc, lazy_sym, code, debug_output); | |
| 135 | return generateLazyFunction(bin_file, pt, src_loc, lazy_sym, code, debug_output); | |
| 136 | 136 | } |
| 137 | 137 | |
| 138 | if (lazy_sym.ty.isAnyError(zcu)) { | |
| 138 | if (lazy_sym.ty.isAnyError(pt.zcu)) { | |
| 139 | 139 | alignment.* = .@"4"; |
| 140 | const err_names = zcu.global_error_set.keys(); | |
| 140 | const err_names = pt.zcu.global_error_set.keys(); | |
| 141 | 141 | mem.writeInt(u32, try code.addManyAsArray(4), @intCast(err_names.len), endian); |
| 142 | 142 | var offset = code.items.len; |
| 143 | 143 | try code.resize((1 + err_names.len + 1) * 4); |
| ... | ... | @@ -151,9 +151,9 @@ pub fn generateLazySymbol( |
| 151 | 151 | } |
| 152 | 152 | mem.writeInt(u32, code.items[offset..][0..4], @intCast(code.items.len), endian); |
| 153 | 153 | return Result.ok; |
| 154 | } else if (lazy_sym.ty.zigTypeTag(zcu) == .Enum) { | |
| 154 | } else if (lazy_sym.ty.zigTypeTag(pt.zcu) == .Enum) { | |
| 155 | 155 | alignment.* = .@"1"; |
| 156 | const tag_names = lazy_sym.ty.enumFields(zcu); | |
| 156 | const tag_names = lazy_sym.ty.enumFields(pt.zcu); | |
| 157 | 157 | for (0..tag_names.len) |tag_index| { |
| 158 | 158 | const tag_name = tag_names.get(ip)[tag_index].toSlice(ip); |
| 159 | 159 | try code.ensureUnusedCapacity(tag_name.len + 1); |
| ... | ... | @@ -165,13 +165,14 @@ pub fn generateLazySymbol( |
| 165 | 165 | gpa, |
| 166 | 166 | src_loc, |
| 167 | 167 | "TODO implement generateLazySymbol for {s} {}", |
| 168 | .{ @tagName(lazy_sym.kind), lazy_sym.ty.fmt(zcu) }, | |
| 168 | .{ @tagName(lazy_sym.kind), lazy_sym.ty.fmt(pt) }, | |
| 169 | 169 | ) }; |
| 170 | 170 | } |
| 171 | 171 | |
| 172 | 172 | pub fn generateSymbol( |
| 173 | 173 | bin_file: *link.File, |
| 174 | src_loc: Module.LazySrcLoc, | |
| 174 | pt: Zcu.PerThread, | |
| 175 | src_loc: Zcu.LazySrcLoc, | |
| 175 | 176 | val: Value, |
| 176 | 177 | code: *std.ArrayList(u8), |
| 177 | 178 | debug_output: DebugInfoOutput, |
| ... | ... | @@ -180,17 +181,17 @@ pub fn generateSymbol( |
| 180 | 181 | const tracy = trace(@src()); |
| 181 | 182 | defer tracy.end(); |
| 182 | 183 | |
| 183 | const mod = bin_file.comp.module.?; | |
| 184 | const mod = pt.zcu; | |
| 184 | 185 | const ip = &mod.intern_pool; |
| 185 | 186 | const ty = val.typeOf(mod); |
| 186 | 187 | |
| 187 | 188 | const target = mod.getTarget(); |
| 188 | 189 | const endian = target.cpu.arch.endian(); |
| 189 | 190 | |
| 190 | log.debug("generateSymbol: val = {}", .{val.fmtValue(mod, null)}); | |
| 191 | log.debug("generateSymbol: val = {}", .{val.fmtValue(pt, null)}); | |
| 191 | 192 | |
| 192 | 193 | if (val.isUndefDeep(mod)) { |
| 193 | const abi_size = math.cast(usize, ty.abiSize(mod)) orelse return error.Overflow; | |
| 194 | const abi_size = math.cast(usize, ty.abiSize(pt)) orelse return error.Overflow; | |
| 194 | 195 | try code.appendNTimes(0xaa, abi_size); |
| 195 | 196 | return .ok; |
| 196 | 197 | } |
| ... | ... | @@ -236,9 +237,9 @@ pub fn generateSymbol( |
| 236 | 237 | .empty_enum_value, |
| 237 | 238 | => unreachable, // non-runtime values |
| 238 | 239 | .int => { |
| 239 | const abi_size = math.cast(usize, ty.abiSize(mod)) orelse return error.Overflow; | |
| 240 | const abi_size = math.cast(usize, ty.abiSize(pt)) orelse return error.Overflow; | |
| 240 | 241 | var space: Value.BigIntSpace = undefined; |
| 241 | const int_val = val.toBigInt(&space, mod); | |
| 242 | const int_val = val.toBigInt(&space, pt); | |
| 242 | 243 | int_val.writeTwosComplement(try code.addManyAsSlice(abi_size), endian); |
| 243 | 244 | }, |
| 244 | 245 | .err => |err| { |
| ... | ... | @@ -252,14 +253,14 @@ pub fn generateSymbol( |
| 252 | 253 | .payload => 0, |
| 253 | 254 | }; |
| 254 | 255 | |
| 255 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 256 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 256 | 257 | try code.writer().writeInt(u16, err_val, endian); |
| 257 | 258 | return .ok; |
| 258 | 259 | } |
| 259 | 260 | |
| 260 | const payload_align = payload_ty.abiAlignment(mod); | |
| 261 | const error_align = Type.anyerror.abiAlignment(mod); | |
| 262 | const abi_align = ty.abiAlignment(mod); | |
| 261 | const payload_align = payload_ty.abiAlignment(pt); | |
| 262 | const error_align = Type.anyerror.abiAlignment(pt); | |
| 263 | const abi_align = ty.abiAlignment(pt); | |
| 263 | 264 | |
| 264 | 265 | // error value first when its type is larger than the error union's payload |
| 265 | 266 | if (error_align.order(payload_align) == .gt) { |
| ... | ... | @@ -269,8 +270,8 @@ pub fn generateSymbol( |
| 269 | 270 | // emit payload part of the error union |
| 270 | 271 | { |
| 271 | 272 | const begin = code.items.len; |
| 272 | switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(switch (error_union.val) { | |
| 273 | .err_name => try mod.intern(.{ .undef = payload_ty.toIntern() }), | |
| 273 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(switch (error_union.val) { | |
| 274 | .err_name => try pt.intern(.{ .undef = payload_ty.toIntern() }), | |
| 274 | 275 | .payload => |payload| payload, |
| 275 | 276 | }), code, debug_output, reloc_info)) { |
| 276 | 277 | .ok => {}, |
| ... | ... | @@ -300,7 +301,7 @@ pub fn generateSymbol( |
| 300 | 301 | }, |
| 301 | 302 | .enum_tag => |enum_tag| { |
| 302 | 303 | const int_tag_ty = ty.intTagType(mod); |
| 303 | switch (try generateSymbol(bin_file, src_loc, try mod.getCoerced(Value.fromInterned(enum_tag.int), int_tag_ty), code, debug_output, reloc_info)) { | |
| 304 | switch (try generateSymbol(bin_file, pt, src_loc, try pt.getCoerced(Value.fromInterned(enum_tag.int), int_tag_ty), code, debug_output, reloc_info)) { | |
| 304 | 305 | .ok => {}, |
| 305 | 306 | .fail => |em| return .{ .fail = em }, |
| 306 | 307 | } |
| ... | ... | @@ -311,21 +312,21 @@ pub fn generateSymbol( |
| 311 | 312 | .f64 => |f64_val| writeFloat(f64, f64_val, target, endian, try code.addManyAsArray(8)), |
| 312 | 313 | .f80 => |f80_val| { |
| 313 | 314 | writeFloat(f80, f80_val, target, endian, try code.addManyAsArray(10)); |
| 314 | const abi_size = math.cast(usize, ty.abiSize(mod)) orelse return error.Overflow; | |
| 315 | const abi_size = math.cast(usize, ty.abiSize(pt)) orelse return error.Overflow; | |
| 315 | 316 | try code.appendNTimes(0, abi_size - 10); |
| 316 | 317 | }, |
| 317 | 318 | .f128 => |f128_val| writeFloat(f128, f128_val, target, endian, try code.addManyAsArray(16)), |
| 318 | 319 | }, |
| 319 | .ptr => switch (try lowerPtr(bin_file, src_loc, val.toIntern(), code, debug_output, reloc_info, 0)) { | |
| 320 | .ptr => switch (try lowerPtr(bin_file, pt, src_loc, val.toIntern(), code, debug_output, reloc_info, 0)) { | |
| 320 | 321 | .ok => {}, |
| 321 | 322 | .fail => |em| return .{ .fail = em }, |
| 322 | 323 | }, |
| 323 | 324 | .slice => |slice| { |
| 324 | switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(slice.ptr), code, debug_output, reloc_info)) { | |
| 325 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(slice.ptr), code, debug_output, reloc_info)) { | |
| 325 | 326 | .ok => {}, |
| 326 | 327 | .fail => |em| return .{ .fail = em }, |
| 327 | 328 | } |
| 328 | switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(slice.len), code, debug_output, reloc_info)) { | |
| 329 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(slice.len), code, debug_output, reloc_info)) { | |
| 329 | 330 | .ok => {}, |
| 330 | 331 | .fail => |em| return .{ .fail = em }, |
| 331 | 332 | } |
| ... | ... | @@ -333,11 +334,11 @@ pub fn generateSymbol( |
| 333 | 334 | .opt => { |
| 334 | 335 | const payload_type = ty.optionalChild(mod); |
| 335 | 336 | const payload_val = val.optionalValue(mod); |
| 336 | const abi_size = math.cast(usize, ty.abiSize(mod)) orelse return error.Overflow; | |
| 337 | const abi_size = math.cast(usize, ty.abiSize(pt)) orelse return error.Overflow; | |
| 337 | 338 | |
| 338 | 339 | if (ty.optionalReprIsPayload(mod)) { |
| 339 | 340 | if (payload_val) |value| { |
| 340 | switch (try generateSymbol(bin_file, src_loc, value, code, debug_output, reloc_info)) { | |
| 341 | switch (try generateSymbol(bin_file, pt, src_loc, value, code, debug_output, reloc_info)) { | |
| 341 | 342 | .ok => {}, |
| 342 | 343 | .fail => |em| return Result{ .fail = em }, |
| 343 | 344 | } |
| ... | ... | @@ -345,10 +346,12 @@ pub fn generateSymbol( |
| 345 | 346 | try code.appendNTimes(0, abi_size); |
| 346 | 347 | } |
| 347 | 348 | } else { |
| 348 | const padding = abi_size - (math.cast(usize, payload_type.abiSize(mod)) orelse return error.Overflow) - 1; | |
| 349 | if (payload_type.hasRuntimeBits(mod)) { | |
| 350 | const value = payload_val orelse Value.fromInterned((try mod.intern(.{ .undef = payload_type.toIntern() }))); | |
| 351 | switch (try generateSymbol(bin_file, src_loc, value, code, debug_output, reloc_info)) { | |
| 349 | const padding = abi_size - (math.cast(usize, payload_type.abiSize(pt)) orelse return error.Overflow) - 1; | |
| 350 | if (payload_type.hasRuntimeBits(pt)) { | |
| 351 | const value = payload_val orelse Value.fromInterned(try pt.intern(.{ | |
| 352 | .undef = payload_type.toIntern(), | |
| 353 | })); | |
| 354 | switch (try generateSymbol(bin_file, pt, src_loc, value, code, debug_output, reloc_info)) { | |
| 352 | 355 | .ok => {}, |
| 353 | 356 | .fail => |em| return Result{ .fail = em }, |
| 354 | 357 | } |
| ... | ... | @@ -363,7 +366,7 @@ pub fn generateSymbol( |
| 363 | 366 | .elems, .repeated_elem => { |
| 364 | 367 | var index: u64 = 0; |
| 365 | 368 | while (index < array_type.lenIncludingSentinel()) : (index += 1) { |
| 366 | switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(switch (aggregate.storage) { | |
| 369 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(switch (aggregate.storage) { | |
| 367 | 370 | .bytes => unreachable, |
| 368 | 371 | .elems => |elems| elems[@intCast(index)], |
| 369 | 372 | .repeated_elem => |elem| if (index < array_type.len) |
| ... | ... | @@ -378,8 +381,7 @@ pub fn generateSymbol( |
| 378 | 381 | }, |
| 379 | 382 | }, |
| 380 | 383 | .vector_type => |vector_type| { |
| 381 | const abi_size = math.cast(usize, ty.abiSize(mod)) orelse | |
| 382 | return error.Overflow; | |
| 384 | const abi_size = math.cast(usize, ty.abiSize(pt)) orelse return error.Overflow; | |
| 383 | 385 | if (vector_type.child == .bool_type) { |
| 384 | 386 | const bytes = try code.addManyAsSlice(abi_size); |
| 385 | 387 | @memset(bytes, 0xaa); |
| ... | ... | @@ -424,7 +426,7 @@ pub fn generateSymbol( |
| 424 | 426 | .elems, .repeated_elem => { |
| 425 | 427 | var index: u64 = 0; |
| 426 | 428 | while (index < vector_type.len) : (index += 1) { |
| 427 | switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(switch (aggregate.storage) { | |
| 429 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(switch (aggregate.storage) { | |
| 428 | 430 | .bytes => unreachable, |
| 429 | 431 | .elems => |elems| elems[ |
| 430 | 432 | math.cast(usize, index) orelse return error.Overflow |
| ... | ... | @@ -439,7 +441,7 @@ pub fn generateSymbol( |
| 439 | 441 | } |
| 440 | 442 | |
| 441 | 443 | const padding = abi_size - |
| 442 | (math.cast(usize, Type.fromInterned(vector_type.child).abiSize(mod) * vector_type.len) orelse | |
| 444 | (math.cast(usize, Type.fromInterned(vector_type.child).abiSize(pt) * vector_type.len) orelse | |
| 443 | 445 | return error.Overflow); |
| 444 | 446 | if (padding > 0) try code.appendNTimes(0, padding); |
| 445 | 447 | } |
| ... | ... | @@ -452,10 +454,10 @@ pub fn generateSymbol( |
| 452 | 454 | 0.., |
| 453 | 455 | ) |field_ty, comptime_val, index| { |
| 454 | 456 | if (comptime_val != .none) continue; |
| 455 | if (!Type.fromInterned(field_ty).hasRuntimeBits(mod)) continue; | |
| 457 | if (!Type.fromInterned(field_ty).hasRuntimeBits(pt)) continue; | |
| 456 | 458 | |
| 457 | 459 | const field_val = switch (aggregate.storage) { |
| 458 | .bytes => |bytes| try ip.get(mod.gpa, .{ .int = .{ | |
| 460 | .bytes => |bytes| try pt.intern(.{ .int = .{ | |
| 459 | 461 | .ty = field_ty, |
| 460 | 462 | .storage = .{ .u64 = bytes.at(index, ip) }, |
| 461 | 463 | } }), |
| ... | ... | @@ -463,14 +465,14 @@ pub fn generateSymbol( |
| 463 | 465 | .repeated_elem => |elem| elem, |
| 464 | 466 | }; |
| 465 | 467 | |
| 466 | switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(field_val), code, debug_output, reloc_info)) { | |
| 468 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), code, debug_output, reloc_info)) { | |
| 467 | 469 | .ok => {}, |
| 468 | 470 | .fail => |em| return Result{ .fail = em }, |
| 469 | 471 | } |
| 470 | 472 | const unpadded_field_end = code.items.len - struct_begin; |
| 471 | 473 | |
| 472 | 474 | // Pad struct members if required |
| 473 | const padded_field_end = ty.structFieldOffset(index + 1, mod); | |
| 475 | const padded_field_end = ty.structFieldOffset(index + 1, pt); | |
| 474 | 476 | const padding = math.cast(usize, padded_field_end - unpadded_field_end) orelse |
| 475 | 477 | return error.Overflow; |
| 476 | 478 | |
| ... | ... | @@ -483,15 +485,14 @@ pub fn generateSymbol( |
| 483 | 485 | const struct_type = ip.loadStructType(ty.toIntern()); |
| 484 | 486 | switch (struct_type.layout) { |
| 485 | 487 | .@"packed" => { |
| 486 | const abi_size = math.cast(usize, ty.abiSize(mod)) orelse | |
| 487 | return error.Overflow; | |
| 488 | const abi_size = math.cast(usize, ty.abiSize(pt)) orelse return error.Overflow; | |
| 488 | 489 | const current_pos = code.items.len; |
| 489 | 490 | try code.appendNTimes(0, abi_size); |
| 490 | 491 | var bits: u16 = 0; |
| 491 | 492 | |
| 492 | 493 | for (struct_type.field_types.get(ip), 0..) |field_ty, index| { |
| 493 | 494 | const field_val = switch (aggregate.storage) { |
| 494 | .bytes => |bytes| try ip.get(mod.gpa, .{ .int = .{ | |
| 495 | .bytes => |bytes| try pt.intern(.{ .int = .{ | |
| 495 | 496 | .ty = field_ty, |
| 496 | 497 | .storage = .{ .u64 = bytes.at(index, ip) }, |
| 497 | 498 | } }), |
| ... | ... | @@ -502,18 +503,18 @@ pub fn generateSymbol( |
| 502 | 503 | // pointer may point to a decl which must be marked used |
| 503 | 504 | // but can also result in a relocation. Therefore we handle those separately. |
| 504 | 505 | if (Type.fromInterned(field_ty).zigTypeTag(mod) == .Pointer) { |
| 505 | const field_size = math.cast(usize, Type.fromInterned(field_ty).abiSize(mod)) orelse | |
| 506 | const field_size = math.cast(usize, Type.fromInterned(field_ty).abiSize(pt)) orelse | |
| 506 | 507 | return error.Overflow; |
| 507 | 508 | var tmp_list = try std.ArrayList(u8).initCapacity(code.allocator, field_size); |
| 508 | 509 | defer tmp_list.deinit(); |
| 509 | switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(field_val), &tmp_list, debug_output, reloc_info)) { | |
| 510 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), &tmp_list, debug_output, reloc_info)) { | |
| 510 | 511 | .ok => @memcpy(code.items[current_pos..][0..tmp_list.items.len], tmp_list.items), |
| 511 | 512 | .fail => |em| return Result{ .fail = em }, |
| 512 | 513 | } |
| 513 | 514 | } else { |
| 514 | Value.fromInterned(field_val).writeToPackedMemory(Type.fromInterned(field_ty), mod, code.items[current_pos..], bits) catch unreachable; | |
| 515 | Value.fromInterned(field_val).writeToPackedMemory(Type.fromInterned(field_ty), pt, code.items[current_pos..], bits) catch unreachable; | |
| 515 | 516 | } |
| 516 | bits += @intCast(Type.fromInterned(field_ty).bitSize(mod)); | |
| 517 | bits += @intCast(Type.fromInterned(field_ty).bitSize(pt)); | |
| 517 | 518 | } |
| 518 | 519 | }, |
| 519 | 520 | .auto, .@"extern" => { |
| ... | ... | @@ -524,10 +525,10 @@ pub fn generateSymbol( |
| 524 | 525 | var it = struct_type.iterateRuntimeOrder(ip); |
| 525 | 526 | while (it.next()) |field_index| { |
| 526 | 527 | const field_ty = field_types[field_index]; |
| 527 | if (!Type.fromInterned(field_ty).hasRuntimeBits(mod)) continue; | |
| 528 | if (!Type.fromInterned(field_ty).hasRuntimeBits(pt)) continue; | |
| 528 | 529 | |
| 529 | 530 | const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { |
| 530 | .bytes => |bytes| try ip.get(mod.gpa, .{ .int = .{ | |
| 531 | .bytes => |bytes| try pt.intern(.{ .int = .{ | |
| 531 | 532 | .ty = field_ty, |
| 532 | 533 | .storage = .{ .u64 = bytes.at(field_index, ip) }, |
| 533 | 534 | } }), |
| ... | ... | @@ -541,7 +542,7 @@ pub fn generateSymbol( |
| 541 | 542 | ) orelse return error.Overflow; |
| 542 | 543 | if (padding > 0) try code.appendNTimes(0, padding); |
| 543 | 544 | |
| 544 | switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(field_val), code, debug_output, reloc_info)) { | |
| 545 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(field_val), code, debug_output, reloc_info)) { | |
| 545 | 546 | .ok => {}, |
| 546 | 547 | .fail => |em| return Result{ .fail = em }, |
| 547 | 548 | } |
| ... | ... | @@ -562,15 +563,15 @@ pub fn generateSymbol( |
| 562 | 563 | else => unreachable, |
| 563 | 564 | }, |
| 564 | 565 | .un => |un| { |
| 565 | const layout = ty.unionGetLayout(mod); | |
| 566 | const layout = ty.unionGetLayout(pt); | |
| 566 | 567 | |
| 567 | 568 | if (layout.payload_size == 0) { |
| 568 | return generateSymbol(bin_file, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info); | |
| 569 | return generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info); | |
| 569 | 570 | } |
| 570 | 571 | |
| 571 | 572 | // Check if we should store the tag first. |
| 572 | 573 | if (layout.tag_size > 0 and layout.tag_align.compare(.gte, layout.payload_align)) { |
| 573 | switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info)) { | |
| 574 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info)) { | |
| 574 | 575 | .ok => {}, |
| 575 | 576 | .fail => |em| return Result{ .fail = em }, |
| 576 | 577 | } |
| ... | ... | @@ -580,28 +581,28 @@ pub fn generateSymbol( |
| 580 | 581 | if (un.tag != .none) { |
| 581 | 582 | const field_index = ty.unionTagFieldIndex(Value.fromInterned(un.tag), mod).?; |
| 582 | 583 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 583 | if (!field_ty.hasRuntimeBits(mod)) { | |
| 584 | if (!field_ty.hasRuntimeBits(pt)) { | |
| 584 | 585 | try code.appendNTimes(0xaa, math.cast(usize, layout.payload_size) orelse return error.Overflow); |
| 585 | 586 | } else { |
| 586 | switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(un.val), code, debug_output, reloc_info)) { | |
| 587 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.val), code, debug_output, reloc_info)) { | |
| 587 | 588 | .ok => {}, |
| 588 | 589 | .fail => |em| return Result{ .fail = em }, |
| 589 | 590 | } |
| 590 | 591 | |
| 591 | const padding = math.cast(usize, layout.payload_size - field_ty.abiSize(mod)) orelse return error.Overflow; | |
| 592 | const padding = math.cast(usize, layout.payload_size - field_ty.abiSize(pt)) orelse return error.Overflow; | |
| 592 | 593 | if (padding > 0) { |
| 593 | 594 | try code.appendNTimes(0, padding); |
| 594 | 595 | } |
| 595 | 596 | } |
| 596 | 597 | } else { |
| 597 | switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(un.val), code, debug_output, reloc_info)) { | |
| 598 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.val), code, debug_output, reloc_info)) { | |
| 598 | 599 | .ok => {}, |
| 599 | 600 | .fail => |em| return Result{ .fail = em }, |
| 600 | 601 | } |
| 601 | 602 | } |
| 602 | 603 | |
| 603 | 604 | if (layout.tag_size > 0 and layout.tag_align.compare(.lt, layout.payload_align)) { |
| 604 | switch (try generateSymbol(bin_file, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info)) { | |
| 605 | switch (try generateSymbol(bin_file, pt, src_loc, Value.fromInterned(un.tag), code, debug_output, reloc_info)) { | |
| 605 | 606 | .ok => {}, |
| 606 | 607 | .fail => |em| return Result{ .fail = em }, |
| 607 | 608 | } |
| ... | ... | @@ -618,22 +619,24 @@ pub fn generateSymbol( |
| 618 | 619 | |
| 619 | 620 | fn lowerPtr( |
| 620 | 621 | bin_file: *link.File, |
| 621 | src_loc: Module.LazySrcLoc, | |
| 622 | pt: Zcu.PerThread, | |
| 623 | src_loc: Zcu.LazySrcLoc, | |
| 622 | 624 | ptr_val: InternPool.Index, |
| 623 | 625 | code: *std.ArrayList(u8), |
| 624 | 626 | debug_output: DebugInfoOutput, |
| 625 | 627 | reloc_info: RelocInfo, |
| 626 | 628 | prev_offset: u64, |
| 627 | 629 | ) CodeGenError!Result { |
| 628 | const zcu = bin_file.comp.module.?; | |
| 630 | const zcu = pt.zcu; | |
| 629 | 631 | const ptr = zcu.intern_pool.indexToKey(ptr_val).ptr; |
| 630 | 632 | const offset: u64 = prev_offset + ptr.byte_offset; |
| 631 | 633 | return switch (ptr.base_addr) { |
| 632 | .decl => |decl| try lowerDeclRef(bin_file, src_loc, decl, code, debug_output, reloc_info, offset), | |
| 633 | .anon_decl => |ad| try lowerAnonDeclRef(bin_file, src_loc, ad, code, debug_output, reloc_info, offset), | |
| 634 | .int => try generateSymbol(bin_file, src_loc, try zcu.intValue(Type.usize, offset), code, debug_output, reloc_info), | |
| 634 | .decl => |decl| try lowerDeclRef(bin_file, pt, src_loc, decl, code, debug_output, reloc_info, offset), | |
| 635 | .anon_decl => |ad| try lowerAnonDeclRef(bin_file, pt, src_loc, ad, code, debug_output, reloc_info, offset), | |
| 636 | .int => try generateSymbol(bin_file, pt, src_loc, try pt.intValue(Type.usize, offset), code, debug_output, reloc_info), | |
| 635 | 637 | .eu_payload => |eu_ptr| try lowerPtr( |
| 636 | 638 | bin_file, |
| 639 | pt, | |
| 637 | 640 | src_loc, |
| 638 | 641 | eu_ptr, |
| 639 | 642 | code, |
| ... | ... | @@ -641,11 +644,12 @@ fn lowerPtr( |
| 641 | 644 | reloc_info, |
| 642 | 645 | offset + errUnionPayloadOffset( |
| 643 | 646 | Value.fromInterned(eu_ptr).typeOf(zcu).childType(zcu).errorUnionPayload(zcu), |
| 644 | zcu, | |
| 647 | pt, | |
| 645 | 648 | ), |
| 646 | 649 | ), |
| 647 | 650 | .opt_payload => |opt_ptr| try lowerPtr( |
| 648 | 651 | bin_file, |
| 652 | pt, | |
| 649 | 653 | src_loc, |
| 650 | 654 | opt_ptr, |
| 651 | 655 | code, |
| ... | ... | @@ -666,12 +670,12 @@ fn lowerPtr( |
| 666 | 670 | }; |
| 667 | 671 | }, |
| 668 | 672 | .Struct, .Union => switch (base_ty.containerLayout(zcu)) { |
| 669 | .auto => base_ty.structFieldOffset(@intCast(field.index), zcu), | |
| 673 | .auto => base_ty.structFieldOffset(@intCast(field.index), pt), | |
| 670 | 674 | .@"extern", .@"packed" => unreachable, |
| 671 | 675 | }, |
| 672 | 676 | else => unreachable, |
| 673 | 677 | }; |
| 674 | return lowerPtr(bin_file, src_loc, field.base, code, debug_output, reloc_info, offset + field_off); | |
| 678 | return lowerPtr(bin_file, pt, src_loc, field.base, code, debug_output, reloc_info, offset + field_off); | |
| 675 | 679 | }, |
| 676 | 680 | .arr_elem, .comptime_field, .comptime_alloc => unreachable, |
| 677 | 681 | }; |
| ... | ... | @@ -683,7 +687,8 @@ const RelocInfo = struct { |
| 683 | 687 | |
| 684 | 688 | fn lowerAnonDeclRef( |
| 685 | 689 | lf: *link.File, |
| 686 | src_loc: Module.LazySrcLoc, | |
| 690 | pt: Zcu.PerThread, | |
| 691 | src_loc: Zcu.LazySrcLoc, | |
| 687 | 692 | anon_decl: InternPool.Key.Ptr.BaseAddr.AnonDecl, |
| 688 | 693 | code: *std.ArrayList(u8), |
| 689 | 694 | debug_output: DebugInfoOutput, |
| ... | ... | @@ -691,22 +696,21 @@ fn lowerAnonDeclRef( |
| 691 | 696 | offset: u64, |
| 692 | 697 | ) CodeGenError!Result { |
| 693 | 698 | _ = debug_output; |
| 694 | const zcu = lf.comp.module.?; | |
| 695 | const ip = &zcu.intern_pool; | |
| 699 | const ip = &pt.zcu.intern_pool; | |
| 696 | 700 | const target = lf.comp.root_mod.resolved_target.result; |
| 697 | 701 | |
| 698 | 702 | const ptr_width_bytes = @divExact(target.ptrBitWidth(), 8); |
| 699 | 703 | const decl_val = anon_decl.val; |
| 700 | 704 | const decl_ty = Type.fromInterned(ip.typeOf(decl_val)); |
| 701 | log.debug("lowerAnonDecl: ty = {}", .{decl_ty.fmt(zcu)}); | |
| 702 | const is_fn_body = decl_ty.zigTypeTag(zcu) == .Fn; | |
| 703 | if (!is_fn_body and !decl_ty.hasRuntimeBits(zcu)) { | |
| 705 | log.debug("lowerAnonDecl: ty = {}", .{decl_ty.fmt(pt)}); | |
| 706 | const is_fn_body = decl_ty.zigTypeTag(pt.zcu) == .Fn; | |
| 707 | if (!is_fn_body and !decl_ty.hasRuntimeBits(pt)) { | |
| 704 | 708 | try code.appendNTimes(0xaa, ptr_width_bytes); |
| 705 | 709 | return Result.ok; |
| 706 | 710 | } |
| 707 | 711 | |
| 708 | 712 | const decl_align = ip.indexToKey(anon_decl.orig_ty).ptr_type.flags.alignment; |
| 709 | const res = try lf.lowerAnonDecl(decl_val, decl_align, src_loc); | |
| 713 | const res = try lf.lowerAnonDecl(pt, decl_val, decl_align, src_loc); | |
| 710 | 714 | switch (res) { |
| 711 | 715 | .ok => {}, |
| 712 | 716 | .fail => |em| return .{ .fail = em }, |
| ... | ... | @@ -730,7 +734,8 @@ fn lowerAnonDeclRef( |
| 730 | 734 | |
| 731 | 735 | fn lowerDeclRef( |
| 732 | 736 | lf: *link.File, |
| 733 | src_loc: Module.LazySrcLoc, | |
| 737 | pt: Zcu.PerThread, | |
| 738 | src_loc: Zcu.LazySrcLoc, | |
| 734 | 739 | decl_index: InternPool.DeclIndex, |
| 735 | 740 | code: *std.ArrayList(u8), |
| 736 | 741 | debug_output: DebugInfoOutput, |
| ... | ... | @@ -739,14 +744,14 @@ fn lowerDeclRef( |
| 739 | 744 | ) CodeGenError!Result { |
| 740 | 745 | _ = src_loc; |
| 741 | 746 | _ = debug_output; |
| 742 | const zcu = lf.comp.module.?; | |
| 747 | const zcu = pt.zcu; | |
| 743 | 748 | const decl = zcu.declPtr(decl_index); |
| 744 | 749 | const namespace = zcu.namespacePtr(decl.src_namespace); |
| 745 | 750 | const target = namespace.fileScope(zcu).mod.resolved_target.result; |
| 746 | 751 | |
| 747 | 752 | const ptr_width = target.ptrBitWidth(); |
| 748 | 753 | const is_fn_body = decl.typeOf(zcu).zigTypeTag(zcu) == .Fn; |
| 749 | if (!is_fn_body and !decl.typeOf(zcu).hasRuntimeBits(zcu)) { | |
| 754 | if (!is_fn_body and !decl.typeOf(zcu).hasRuntimeBits(pt)) { | |
| 750 | 755 | try code.appendNTimes(0xaa, @divExact(ptr_width, 8)); |
| 751 | 756 | return Result.ok; |
| 752 | 757 | } |
| ... | ... | @@ -814,7 +819,7 @@ pub const GenResult = union(enum) { |
| 814 | 819 | |
| 815 | 820 | fn fail( |
| 816 | 821 | gpa: Allocator, |
| 817 | src_loc: Module.LazySrcLoc, | |
| 822 | src_loc: Zcu.LazySrcLoc, | |
| 818 | 823 | comptime format: []const u8, |
| 819 | 824 | args: anytype, |
| 820 | 825 | ) Allocator.Error!GenResult { |
| ... | ... | @@ -825,14 +830,15 @@ pub const GenResult = union(enum) { |
| 825 | 830 | |
| 826 | 831 | fn genDeclRef( |
| 827 | 832 | lf: *link.File, |
| 828 | src_loc: Module.LazySrcLoc, | |
| 833 | pt: Zcu.PerThread, | |
| 834 | src_loc: Zcu.LazySrcLoc, | |
| 829 | 835 | val: Value, |
| 830 | 836 | ptr_decl_index: InternPool.DeclIndex, |
| 831 | 837 | ) CodeGenError!GenResult { |
| 832 | const zcu = lf.comp.module.?; | |
| 838 | const zcu = pt.zcu; | |
| 833 | 839 | const ip = &zcu.intern_pool; |
| 834 | 840 | const ty = val.typeOf(zcu); |
| 835 | log.debug("genDeclRef: val = {}", .{val.fmtValue(zcu, null)}); | |
| 841 | log.debug("genDeclRef: val = {}", .{val.fmtValue(pt, null)}); | |
| 836 | 842 | |
| 837 | 843 | const ptr_decl = zcu.declPtr(ptr_decl_index); |
| 838 | 844 | const namespace = zcu.namespacePtr(ptr_decl.src_namespace); |
| ... | ... | @@ -848,7 +854,7 @@ fn genDeclRef( |
| 848 | 854 | }; |
| 849 | 855 | const decl = zcu.declPtr(decl_index); |
| 850 | 856 | |
| 851 | if (!decl.typeOf(zcu).isFnOrHasRuntimeBitsIgnoreComptime(zcu)) { | |
| 857 | if (!decl.typeOf(zcu).isFnOrHasRuntimeBitsIgnoreComptime(pt)) { | |
| 852 | 858 | const imm: u64 = switch (ptr_bytes) { |
| 853 | 859 | 1 => 0xaa, |
| 854 | 860 | 2 => 0xaaaa, |
| ... | ... | @@ -865,12 +871,12 @@ fn genDeclRef( |
| 865 | 871 | // TODO this feels clunky. Perhaps we should check for it in `genTypedValue`? |
| 866 | 872 | if (ty.castPtrToFn(zcu)) |fn_ty| { |
| 867 | 873 | if (zcu.typeToFunc(fn_ty).?.is_generic) { |
| 868 | return GenResult.mcv(.{ .immediate = fn_ty.abiAlignment(zcu).toByteUnits().? }); | |
| 874 | return GenResult.mcv(.{ .immediate = fn_ty.abiAlignment(pt).toByteUnits().? }); | |
| 869 | 875 | } |
| 870 | 876 | } else if (ty.zigTypeTag(zcu) == .Pointer) { |
| 871 | 877 | const elem_ty = ty.elemType2(zcu); |
| 872 | if (!elem_ty.hasRuntimeBits(zcu)) { | |
| 873 | return GenResult.mcv(.{ .immediate = elem_ty.abiAlignment(zcu).toByteUnits().? }); | |
| 878 | if (!elem_ty.hasRuntimeBits(pt)) { | |
| 879 | return GenResult.mcv(.{ .immediate = elem_ty.abiAlignment(pt).toByteUnits().? }); | |
| 874 | 880 | } |
| 875 | 881 | } |
| 876 | 882 | |
| ... | ... | @@ -931,15 +937,15 @@ fn genDeclRef( |
| 931 | 937 | |
| 932 | 938 | fn genUnnamedConst( |
| 933 | 939 | lf: *link.File, |
| 934 | src_loc: Module.LazySrcLoc, | |
| 940 | pt: Zcu.PerThread, | |
| 941 | src_loc: Zcu.LazySrcLoc, | |
| 935 | 942 | val: Value, |
| 936 | 943 | owner_decl_index: InternPool.DeclIndex, |
| 937 | 944 | ) CodeGenError!GenResult { |
| 938 | const zcu = lf.comp.module.?; | |
| 939 | 945 | const gpa = lf.comp.gpa; |
| 940 | log.debug("genUnnamedConst: val = {}", .{val.fmtValue(zcu, null)}); | |
| 946 | log.debug("genUnnamedConst: val = {}", .{val.fmtValue(pt, null)}); | |
| 941 | 947 | |
| 942 | const local_sym_index = lf.lowerUnnamedConst(val, owner_decl_index) catch |err| { | |
| 948 | const local_sym_index = lf.lowerUnnamedConst(pt, val, owner_decl_index) catch |err| { | |
| 943 | 949 | return GenResult.fail(gpa, src_loc, "lowering unnamed constant failed: {s}", .{@errorName(err)}); |
| 944 | 950 | }; |
| 945 | 951 | switch (lf.tag) { |
| ... | ... | @@ -970,15 +976,16 @@ fn genUnnamedConst( |
| 970 | 976 | |
| 971 | 977 | pub fn genTypedValue( |
| 972 | 978 | lf: *link.File, |
| 973 | src_loc: Module.LazySrcLoc, | |
| 979 | pt: Zcu.PerThread, | |
| 980 | src_loc: Zcu.LazySrcLoc, | |
| 974 | 981 | val: Value, |
| 975 | 982 | owner_decl_index: InternPool.DeclIndex, |
| 976 | 983 | ) CodeGenError!GenResult { |
| 977 | const zcu = lf.comp.module.?; | |
| 984 | const zcu = pt.zcu; | |
| 978 | 985 | const ip = &zcu.intern_pool; |
| 979 | 986 | const ty = val.typeOf(zcu); |
| 980 | 987 | |
| 981 | log.debug("genTypedValue: val = {}", .{val.fmtValue(zcu, null)}); | |
| 988 | log.debug("genTypedValue: val = {}", .{val.fmtValue(pt, null)}); | |
| 982 | 989 | |
| 983 | 990 | if (val.isUndef(zcu)) |
| 984 | 991 | return GenResult.mcv(.undef); |
| ... | ... | @@ -990,7 +997,7 @@ pub fn genTypedValue( |
| 990 | 997 | |
| 991 | 998 | if (!ty.isSlice(zcu)) switch (ip.indexToKey(val.toIntern())) { |
| 992 | 999 | .ptr => |ptr| if (ptr.byte_offset == 0) switch (ptr.base_addr) { |
| 993 | .decl => |decl| return genDeclRef(lf, src_loc, val, decl), | |
| 1000 | .decl => |decl| return genDeclRef(lf, pt, src_loc, val, decl), | |
| 994 | 1001 | else => {}, |
| 995 | 1002 | }, |
| 996 | 1003 | else => {}, |
| ... | ... | @@ -1007,7 +1014,7 @@ pub fn genTypedValue( |
| 1007 | 1014 | .none => {}, |
| 1008 | 1015 | else => switch (ip.indexToKey(val.toIntern())) { |
| 1009 | 1016 | .int => { |
| 1010 | return GenResult.mcv(.{ .immediate = val.toUnsignedInt(zcu) }); | |
| 1017 | return GenResult.mcv(.{ .immediate = val.toUnsignedInt(pt) }); | |
| 1011 | 1018 | }, |
| 1012 | 1019 | else => {}, |
| 1013 | 1020 | }, |
| ... | ... | @@ -1017,8 +1024,8 @@ pub fn genTypedValue( |
| 1017 | 1024 | const info = ty.intInfo(zcu); |
| 1018 | 1025 | if (info.bits <= ptr_bits) { |
| 1019 | 1026 | const unsigned: u64 = switch (info.signedness) { |
| 1020 | .signed => @bitCast(val.toSignedInt(zcu)), | |
| 1021 | .unsigned => val.toUnsignedInt(zcu), | |
| 1027 | .signed => @bitCast(val.toSignedInt(pt)), | |
| 1028 | .unsigned => val.toUnsignedInt(pt), | |
| 1022 | 1029 | }; |
| 1023 | 1030 | return GenResult.mcv(.{ .immediate = unsigned }); |
| 1024 | 1031 | } |
| ... | ... | @@ -1030,11 +1037,12 @@ pub fn genTypedValue( |
| 1030 | 1037 | if (ty.isPtrLikeOptional(zcu)) { |
| 1031 | 1038 | return genTypedValue( |
| 1032 | 1039 | lf, |
| 1040 | pt, | |
| 1033 | 1041 | src_loc, |
| 1034 | 1042 | val.optionalValue(zcu) orelse return GenResult.mcv(.{ .immediate = 0 }), |
| 1035 | 1043 | owner_decl_index, |
| 1036 | 1044 | ); |
| 1037 | } else if (ty.abiSize(zcu) == 1) { | |
| 1045 | } else if (ty.abiSize(pt) == 1) { | |
| 1038 | 1046 | return GenResult.mcv(.{ .immediate = @intFromBool(!val.isNull(zcu)) }); |
| 1039 | 1047 | } |
| 1040 | 1048 | }, |
| ... | ... | @@ -1042,6 +1050,7 @@ pub fn genTypedValue( |
| 1042 | 1050 | const enum_tag = ip.indexToKey(val.toIntern()).enum_tag; |
| 1043 | 1051 | return genTypedValue( |
| 1044 | 1052 | lf, |
| 1053 | pt, | |
| 1045 | 1054 | src_loc, |
| 1046 | 1055 | Value.fromInterned(enum_tag.int), |
| 1047 | 1056 | owner_decl_index, |
| ... | ... | @@ -1055,14 +1064,15 @@ pub fn genTypedValue( |
| 1055 | 1064 | .ErrorUnion => { |
| 1056 | 1065 | const err_type = ty.errorUnionSet(zcu); |
| 1057 | 1066 | const payload_type = ty.errorUnionPayload(zcu); |
| 1058 | if (!payload_type.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 1067 | if (!payload_type.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1059 | 1068 | // We use the error type directly as the type. |
| 1060 | const err_int_ty = try zcu.errorIntType(); | |
| 1069 | const err_int_ty = try pt.errorIntType(); | |
| 1061 | 1070 | switch (ip.indexToKey(val.toIntern()).error_union.val) { |
| 1062 | 1071 | .err_name => |err_name| return genTypedValue( |
| 1063 | 1072 | lf, |
| 1073 | pt, | |
| 1064 | 1074 | src_loc, |
| 1065 | Value.fromInterned(try zcu.intern(.{ .err = .{ | |
| 1075 | Value.fromInterned(try pt.intern(.{ .err = .{ | |
| 1066 | 1076 | .ty = err_type.toIntern(), |
| 1067 | 1077 | .name = err_name, |
| 1068 | 1078 | } })), |
| ... | ... | @@ -1070,8 +1080,9 @@ pub fn genTypedValue( |
| 1070 | 1080 | ), |
| 1071 | 1081 | .payload => return genTypedValue( |
| 1072 | 1082 | lf, |
| 1083 | pt, | |
| 1073 | 1084 | src_loc, |
| 1074 | try zcu.intValue(err_int_ty, 0), | |
| 1085 | try pt.intValue(err_int_ty, 0), | |
| 1075 | 1086 | owner_decl_index, |
| 1076 | 1087 | ), |
| 1077 | 1088 | } |
| ... | ... | @@ -1090,26 +1101,26 @@ pub fn genTypedValue( |
| 1090 | 1101 | else => {}, |
| 1091 | 1102 | } |
| 1092 | 1103 | |
| 1093 | return genUnnamedConst(lf, src_loc, val, owner_decl_index); | |
| 1104 | return genUnnamedConst(lf, pt, src_loc, val, owner_decl_index); | |
| 1094 | 1105 | } |
| 1095 | 1106 | |
| 1096 | pub fn errUnionPayloadOffset(payload_ty: Type, mod: *Module) u64 { | |
| 1097 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) return 0; | |
| 1098 | const payload_align = payload_ty.abiAlignment(mod); | |
| 1099 | const error_align = Type.anyerror.abiAlignment(mod); | |
| 1100 | if (payload_align.compare(.gte, error_align) or !payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 1107 | pub fn errUnionPayloadOffset(payload_ty: Type, pt: Zcu.PerThread) u64 { | |
| 1108 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) return 0; | |
| 1109 | const payload_align = payload_ty.abiAlignment(pt); | |
| 1110 | const error_align = Type.anyerror.abiAlignment(pt); | |
| 1111 | if (payload_align.compare(.gte, error_align) or !payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1101 | 1112 | return 0; |
| 1102 | 1113 | } else { |
| 1103 | return payload_align.forward(Type.anyerror.abiSize(mod)); | |
| 1114 | return payload_align.forward(Type.anyerror.abiSize(pt)); | |
| 1104 | 1115 | } |
| 1105 | 1116 | } |
| 1106 | 1117 | |
| 1107 | pub fn errUnionErrorOffset(payload_ty: Type, mod: *Module) u64 { | |
| 1108 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) return 0; | |
| 1109 | const payload_align = payload_ty.abiAlignment(mod); | |
| 1110 | const error_align = Type.anyerror.abiAlignment(mod); | |
| 1111 | if (payload_align.compare(.gte, error_align) and payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 1112 | return error_align.forward(payload_ty.abiSize(mod)); | |
| 1118 | pub fn errUnionErrorOffset(payload_ty: Type, pt: Zcu.PerThread) u64 { | |
| 1119 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) return 0; | |
| 1120 | const payload_align = payload_ty.abiAlignment(pt); | |
| 1121 | const error_align = Type.anyerror.abiAlignment(pt); | |
| 1122 | if (payload_align.compare(.gte, error_align) and payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1123 | return error_align.forward(payload_ty.abiSize(pt)); | |
| 1113 | 1124 | } else { |
| 1114 | 1125 | return 0; |
| 1115 | 1126 | } |
src/codegen/c.zig+408-337| ... | ... | @@ -333,15 +333,15 @@ pub const Function = struct { |
| 333 | 333 | const gop = try f.value_map.getOrPut(ref); |
| 334 | 334 | if (gop.found_existing) return gop.value_ptr.*; |
| 335 | 335 | |
| 336 | const zcu = f.object.dg.zcu; | |
| 337 | const val = (try f.air.value(ref, zcu)).?; | |
| 336 | const pt = f.object.dg.pt; | |
| 337 | const val = (try f.air.value(ref, pt)).?; | |
| 338 | 338 | const ty = f.typeOf(ref); |
| 339 | 339 | |
| 340 | const result: CValue = if (lowersToArray(ty, zcu)) result: { | |
| 340 | const result: CValue = if (lowersToArray(ty, pt)) result: { | |
| 341 | 341 | const writer = f.object.codeHeaderWriter(); |
| 342 | 342 | const decl_c_value = try f.allocLocalValue(.{ |
| 343 | 343 | .ctype = try f.ctypeFromType(ty, .complete), |
| 344 | .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(zcu)), | |
| 344 | .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(pt)), | |
| 345 | 345 | }); |
| 346 | 346 | const gpa = f.object.dg.gpa; |
| 347 | 347 | try f.allocs.put(gpa, decl_c_value.new_local, false); |
| ... | ... | @@ -358,7 +358,7 @@ pub const Function = struct { |
| 358 | 358 | } |
| 359 | 359 | |
| 360 | 360 | fn wantSafety(f: *Function) bool { |
| 361 | return switch (f.object.dg.zcu.optimizeMode()) { | |
| 361 | return switch (f.object.dg.pt.zcu.optimizeMode()) { | |
| 362 | 362 | .Debug, .ReleaseSafe => true, |
| 363 | 363 | .ReleaseFast, .ReleaseSmall => false, |
| 364 | 364 | }; |
| ... | ... | @@ -379,7 +379,7 @@ pub const Function = struct { |
| 379 | 379 | fn allocLocal(f: *Function, inst: ?Air.Inst.Index, ty: Type) !CValue { |
| 380 | 380 | return f.allocAlignedLocal(inst, .{ |
| 381 | 381 | .ctype = try f.ctypeFromType(ty, .complete), |
| 382 | .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(f.object.dg.zcu)), | |
| 382 | .alignas = CType.AlignAs.fromAbiAlignment(ty.abiAlignment(f.object.dg.pt)), | |
| 383 | 383 | }); |
| 384 | 384 | } |
| 385 | 385 | |
| ... | ... | @@ -500,7 +500,8 @@ pub const Function = struct { |
| 500 | 500 | |
| 501 | 501 | fn getLazyFnName(f: *Function, key: LazyFnKey, data: LazyFnValue.Data) ![]const u8 { |
| 502 | 502 | const gpa = f.object.dg.gpa; |
| 503 | const zcu = f.object.dg.zcu; | |
| 503 | const pt = f.object.dg.pt; | |
| 504 | const zcu = pt.zcu; | |
| 504 | 505 | const ctype_pool = &f.object.dg.ctype_pool; |
| 505 | 506 | |
| 506 | 507 | const gop = try f.lazy_fns.getOrPut(gpa, key); |
| ... | ... | @@ -539,13 +540,11 @@ pub const Function = struct { |
| 539 | 540 | } |
| 540 | 541 | |
| 541 | 542 | fn typeOf(f: *Function, inst: Air.Inst.Ref) Type { |
| 542 | const zcu = f.object.dg.zcu; | |
| 543 | return f.air.typeOf(inst, &zcu.intern_pool); | |
| 543 | return f.air.typeOf(inst, &f.object.dg.pt.zcu.intern_pool); | |
| 544 | 544 | } |
| 545 | 545 | |
| 546 | 546 | fn typeOfIndex(f: *Function, inst: Air.Inst.Index) Type { |
| 547 | const zcu = f.object.dg.zcu; | |
| 548 | return f.air.typeOfIndex(inst, &zcu.intern_pool); | |
| 547 | return f.air.typeOfIndex(inst, &f.object.dg.pt.zcu.intern_pool); | |
| 549 | 548 | } |
| 550 | 549 | |
| 551 | 550 | fn copyCValue(f: *Function, ctype: CType, dst: CValue, src: CValue) !void { |
| ... | ... | @@ -608,7 +607,7 @@ pub const Object = struct { |
| 608 | 607 | /// This data is available both when outputting .c code and when outputting an .h file. |
| 609 | 608 | pub const DeclGen = struct { |
| 610 | 609 | gpa: mem.Allocator, |
| 611 | zcu: *Zcu, | |
| 610 | pt: Zcu.PerThread, | |
| 612 | 611 | mod: *Module, |
| 613 | 612 | pass: Pass, |
| 614 | 613 | is_naked_fn: bool, |
| ... | ... | @@ -634,7 +633,7 @@ pub const DeclGen = struct { |
| 634 | 633 | |
| 635 | 634 | fn fail(dg: *DeclGen, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } { |
| 636 | 635 | @setCold(true); |
| 637 | const zcu = dg.zcu; | |
| 636 | const zcu = dg.pt.zcu; | |
| 638 | 637 | const decl_index = dg.pass.decl; |
| 639 | 638 | const decl = zcu.declPtr(decl_index); |
| 640 | 639 | const src_loc = decl.navSrcLoc(zcu); |
| ... | ... | @@ -648,7 +647,8 @@ pub const DeclGen = struct { |
| 648 | 647 | anon_decl: InternPool.Key.Ptr.BaseAddr.AnonDecl, |
| 649 | 648 | location: ValueRenderLocation, |
| 650 | 649 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 651 | const zcu = dg.zcu; | |
| 650 | const pt = dg.pt; | |
| 651 | const zcu = pt.zcu; | |
| 652 | 652 | const ip = &zcu.intern_pool; |
| 653 | 653 | const ctype_pool = &dg.ctype_pool; |
| 654 | 654 | const decl_val = Value.fromInterned(anon_decl.val); |
| ... | ... | @@ -656,7 +656,7 @@ pub const DeclGen = struct { |
| 656 | 656 | |
| 657 | 657 | // Render an undefined pointer if we have a pointer to a zero-bit or comptime type. |
| 658 | 658 | const ptr_ty = Type.fromInterned(anon_decl.orig_ty); |
| 659 | if (ptr_ty.isPtrAtRuntime(zcu) and !decl_ty.isFnOrHasRuntimeBits(zcu)) { | |
| 659 | if (ptr_ty.isPtrAtRuntime(zcu) and !decl_ty.isFnOrHasRuntimeBits(pt)) { | |
| 660 | 660 | return dg.writeCValue(writer, .{ .undef = ptr_ty }); |
| 661 | 661 | } |
| 662 | 662 | |
| ... | ... | @@ -696,7 +696,7 @@ pub const DeclGen = struct { |
| 696 | 696 | // alignment. If there is already an entry, keep the greater alignment. |
| 697 | 697 | const explicit_alignment = ptr_type.flags.alignment; |
| 698 | 698 | if (explicit_alignment != .none) { |
| 699 | const abi_alignment = Type.fromInterned(ptr_type.child).abiAlignment(zcu); | |
| 699 | const abi_alignment = Type.fromInterned(ptr_type.child).abiAlignment(pt); | |
| 700 | 700 | if (explicit_alignment.order(abi_alignment).compare(.gt)) { |
| 701 | 701 | const aligned_gop = try dg.aligned_anon_decls.getOrPut(dg.gpa, anon_decl.val); |
| 702 | 702 | aligned_gop.value_ptr.* = if (aligned_gop.found_existing) |
| ... | ... | @@ -713,15 +713,16 @@ pub const DeclGen = struct { |
| 713 | 713 | decl_index: InternPool.DeclIndex, |
| 714 | 714 | location: ValueRenderLocation, |
| 715 | 715 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 716 | const zcu = dg.zcu; | |
| 716 | const pt = dg.pt; | |
| 717 | const zcu = pt.zcu; | |
| 717 | 718 | const ctype_pool = &dg.ctype_pool; |
| 718 | 719 | const decl = zcu.declPtr(decl_index); |
| 719 | 720 | assert(decl.has_tv); |
| 720 | 721 | |
| 721 | 722 | // Render an undefined pointer if we have a pointer to a zero-bit or comptime type. |
| 722 | 723 | const decl_ty = decl.typeOf(zcu); |
| 723 | const ptr_ty = try decl.declPtrType(zcu); | |
| 724 | if (!decl_ty.isFnOrHasRuntimeBits(zcu)) { | |
| 724 | const ptr_ty = try decl.declPtrType(pt); | |
| 725 | if (!decl_ty.isFnOrHasRuntimeBits(pt)) { | |
| 725 | 726 | return dg.writeCValue(writer, .{ .undef = ptr_ty }); |
| 726 | 727 | } |
| 727 | 728 | |
| ... | ... | @@ -756,12 +757,13 @@ pub const DeclGen = struct { |
| 756 | 757 | derivation: Value.PointerDeriveStep, |
| 757 | 758 | location: ValueRenderLocation, |
| 758 | 759 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 759 | const zcu = dg.zcu; | |
| 760 | const pt = dg.pt; | |
| 761 | const zcu = pt.zcu; | |
| 760 | 762 | switch (derivation) { |
| 761 | 763 | .comptime_alloc_ptr, .comptime_field_ptr => unreachable, |
| 762 | 764 | .int => |int| { |
| 763 | 765 | const ptr_ctype = try dg.ctypeFromType(int.ptr_ty, .complete); |
| 764 | const addr_val = try zcu.intValue(Type.usize, int.addr); | |
| 766 | const addr_val = try pt.intValue(Type.usize, int.addr); | |
| 765 | 767 | try writer.writeByte('('); |
| 766 | 768 | try dg.renderCType(writer, ptr_ctype); |
| 767 | 769 | try writer.print("){x}", .{try dg.fmtIntLiteral(addr_val, .Other)}); |
| ... | ... | @@ -777,12 +779,12 @@ pub const DeclGen = struct { |
| 777 | 779 | }, |
| 778 | 780 | |
| 779 | 781 | .field_ptr => |field| { |
| 780 | const parent_ptr_ty = try field.parent.ptrType(zcu); | |
| 782 | const parent_ptr_ty = try field.parent.ptrType(pt); | |
| 781 | 783 | |
| 782 | 784 | // Ensure complete type definition is available before accessing fields. |
| 783 | 785 | _ = try dg.ctypeFromType(parent_ptr_ty.childType(zcu), .complete); |
| 784 | 786 | |
| 785 | switch (fieldLocation(parent_ptr_ty, field.result_ptr_ty, field.field_idx, zcu)) { | |
| 787 | switch (fieldLocation(parent_ptr_ty, field.result_ptr_ty, field.field_idx, pt)) { | |
| 786 | 788 | .begin => { |
| 787 | 789 | const ptr_ctype = try dg.ctypeFromType(field.result_ptr_ty, .complete); |
| 788 | 790 | try writer.writeByte('('); |
| ... | ... | @@ -801,7 +803,7 @@ pub const DeclGen = struct { |
| 801 | 803 | try writer.writeByte('('); |
| 802 | 804 | try dg.renderCType(writer, ptr_ctype); |
| 803 | 805 | try writer.writeByte(')'); |
| 804 | const offset_val = try zcu.intValue(Type.usize, byte_offset); | |
| 806 | const offset_val = try pt.intValue(Type.usize, byte_offset); | |
| 805 | 807 | try writer.writeAll("((char *)"); |
| 806 | 808 | try dg.renderPointer(writer, field.parent.*, location); |
| 807 | 809 | try writer.print(" + {})", .{try dg.fmtIntLiteral(offset_val, .Other)}); |
| ... | ... | @@ -809,7 +811,7 @@ pub const DeclGen = struct { |
| 809 | 811 | } |
| 810 | 812 | }, |
| 811 | 813 | |
| 812 | .elem_ptr => |elem| if (!(try elem.parent.ptrType(zcu)).childType(zcu).hasRuntimeBits(zcu)) { | |
| 814 | .elem_ptr => |elem| if (!(try elem.parent.ptrType(pt)).childType(zcu).hasRuntimeBits(pt)) { | |
| 813 | 815 | // Element type is zero-bit, so lowers to `void`. The index is irrelevant; just cast the pointer. |
| 814 | 816 | const ptr_ctype = try dg.ctypeFromType(elem.result_ptr_ty, .complete); |
| 815 | 817 | try writer.writeByte('('); |
| ... | ... | @@ -817,11 +819,11 @@ pub const DeclGen = struct { |
| 817 | 819 | try writer.writeByte(')'); |
| 818 | 820 | try dg.renderPointer(writer, elem.parent.*, location); |
| 819 | 821 | } else { |
| 820 | const index_val = try zcu.intValue(Type.usize, elem.elem_idx); | |
| 822 | const index_val = try pt.intValue(Type.usize, elem.elem_idx); | |
| 821 | 823 | // We want to do pointer arithmetic on a pointer to the element type. |
| 822 | 824 | // We might have a pointer-to-array. In this case, we must cast first. |
| 823 | 825 | const result_ctype = try dg.ctypeFromType(elem.result_ptr_ty, .complete); |
| 824 | const parent_ctype = try dg.ctypeFromType(try elem.parent.ptrType(zcu), .complete); | |
| 826 | const parent_ctype = try dg.ctypeFromType(try elem.parent.ptrType(pt), .complete); | |
| 825 | 827 | if (result_ctype.eql(parent_ctype)) { |
| 826 | 828 | // The pointer already has an appropriate type - just do the arithmetic. |
| 827 | 829 | try writer.writeByte('('); |
| ... | ... | @@ -846,7 +848,7 @@ pub const DeclGen = struct { |
| 846 | 848 | if (oac.byte_offset == 0) { |
| 847 | 849 | try dg.renderPointer(writer, oac.parent.*, location); |
| 848 | 850 | } else { |
| 849 | const offset_val = try zcu.intValue(Type.usize, oac.byte_offset); | |
| 851 | const offset_val = try pt.intValue(Type.usize, oac.byte_offset); | |
| 850 | 852 | try writer.writeAll("((char *)"); |
| 851 | 853 | try dg.renderPointer(writer, oac.parent.*, location); |
| 852 | 854 | try writer.print(" + {})", .{try dg.fmtIntLiteral(offset_val, .Other)}); |
| ... | ... | @@ -856,8 +858,7 @@ pub const DeclGen = struct { |
| 856 | 858 | } |
| 857 | 859 | |
| 858 | 860 | fn renderErrorName(dg: *DeclGen, writer: anytype, err_name: InternPool.NullTerminatedString) !void { |
| 859 | const zcu = dg.zcu; | |
| 860 | const ip = &zcu.intern_pool; | |
| 861 | const ip = &dg.pt.zcu.intern_pool; | |
| 861 | 862 | try writer.print("zig_error_{}", .{fmtIdent(err_name.toSlice(ip))}); |
| 862 | 863 | } |
| 863 | 864 | |
| ... | ... | @@ -867,7 +868,8 @@ pub const DeclGen = struct { |
| 867 | 868 | val: Value, |
| 868 | 869 | location: ValueRenderLocation, |
| 869 | 870 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 870 | const zcu = dg.zcu; | |
| 871 | const pt = dg.pt; | |
| 872 | const zcu = pt.zcu; | |
| 871 | 873 | const ip = &zcu.intern_pool; |
| 872 | 874 | const target = &dg.mod.resolved_target.result; |
| 873 | 875 | const ctype_pool = &dg.ctype_pool; |
| ... | ... | @@ -927,7 +929,7 @@ pub const DeclGen = struct { |
| 927 | 929 | try writer.writeAll("(("); |
| 928 | 930 | try dg.renderCType(writer, ctype); |
| 929 | 931 | try writer.print("){x})", .{try dg.fmtIntLiteral( |
| 930 | try zcu.intValue(Type.usize, val.toUnsignedInt(zcu)), | |
| 932 | try pt.intValue(Type.usize, val.toUnsignedInt(pt)), | |
| 931 | 933 | .Other, |
| 932 | 934 | )}); |
| 933 | 935 | }, |
| ... | ... | @@ -974,10 +976,10 @@ pub const DeclGen = struct { |
| 974 | 976 | .enum_tag => |enum_tag| try dg.renderValue(writer, Value.fromInterned(enum_tag.int), location), |
| 975 | 977 | .float => { |
| 976 | 978 | const bits = ty.floatBits(target.*); |
| 977 | const f128_val = val.toFloat(f128, zcu); | |
| 979 | const f128_val = val.toFloat(f128, pt); | |
| 978 | 980 | |
| 979 | 981 | // All unsigned ints matching float types are pre-allocated. |
| 980 | const repr_ty = zcu.intType(.unsigned, bits) catch unreachable; | |
| 982 | const repr_ty = pt.intType(.unsigned, bits) catch unreachable; | |
| 981 | 983 | |
| 982 | 984 | assert(bits <= 128); |
| 983 | 985 | var repr_val_limbs: [BigInt.calcTwosCompLimbCount(128)]BigIntLimb = undefined; |
| ... | ... | @@ -988,10 +990,10 @@ pub const DeclGen = struct { |
| 988 | 990 | }; |
| 989 | 991 | |
| 990 | 992 | switch (bits) { |
| 991 | 16 => repr_val_big.set(@as(u16, @bitCast(val.toFloat(f16, zcu)))), | |
| 992 | 32 => repr_val_big.set(@as(u32, @bitCast(val.toFloat(f32, zcu)))), | |
| 993 | 64 => repr_val_big.set(@as(u64, @bitCast(val.toFloat(f64, zcu)))), | |
| 994 | 80 => repr_val_big.set(@as(u80, @bitCast(val.toFloat(f80, zcu)))), | |
| 993 | 16 => repr_val_big.set(@as(u16, @bitCast(val.toFloat(f16, pt)))), | |
| 994 | 32 => repr_val_big.set(@as(u32, @bitCast(val.toFloat(f32, pt)))), | |
| 995 | 64 => repr_val_big.set(@as(u64, @bitCast(val.toFloat(f64, pt)))), | |
| 996 | 80 => repr_val_big.set(@as(u80, @bitCast(val.toFloat(f80, pt)))), | |
| 995 | 997 | 128 => repr_val_big.set(@as(u128, @bitCast(f128_val))), |
| 996 | 998 | else => unreachable, |
| 997 | 999 | } |
| ... | ... | @@ -1002,10 +1004,10 @@ pub const DeclGen = struct { |
| 1002 | 1004 | try dg.renderTypeForBuiltinFnName(writer, ty); |
| 1003 | 1005 | try writer.writeByte('('); |
| 1004 | 1006 | switch (bits) { |
| 1005 | 16 => try writer.print("{x}", .{val.toFloat(f16, zcu)}), | |
| 1006 | 32 => try writer.print("{x}", .{val.toFloat(f32, zcu)}), | |
| 1007 | 64 => try writer.print("{x}", .{val.toFloat(f64, zcu)}), | |
| 1008 | 80 => try writer.print("{x}", .{val.toFloat(f80, zcu)}), | |
| 1007 | 16 => try writer.print("{x}", .{val.toFloat(f16, pt)}), | |
| 1008 | 32 => try writer.print("{x}", .{val.toFloat(f32, pt)}), | |
| 1009 | 64 => try writer.print("{x}", .{val.toFloat(f64, pt)}), | |
| 1010 | 80 => try writer.print("{x}", .{val.toFloat(f80, pt)}), | |
| 1009 | 1011 | 128 => try writer.print("{x}", .{f128_val}), |
| 1010 | 1012 | else => unreachable, |
| 1011 | 1013 | } |
| ... | ... | @@ -1045,10 +1047,10 @@ pub const DeclGen = struct { |
| 1045 | 1047 | if (std.math.isNan(f128_val)) switch (bits) { |
| 1046 | 1048 | // We only actually need to pass the significand, but it will get |
| 1047 | 1049 | // properly masked anyway, so just pass the whole value. |
| 1048 | 16 => try writer.print("\"0x{x}\"", .{@as(u16, @bitCast(val.toFloat(f16, zcu)))}), | |
| 1049 | 32 => try writer.print("\"0x{x}\"", .{@as(u32, @bitCast(val.toFloat(f32, zcu)))}), | |
| 1050 | 64 => try writer.print("\"0x{x}\"", .{@as(u64, @bitCast(val.toFloat(f64, zcu)))}), | |
| 1051 | 80 => try writer.print("\"0x{x}\"", .{@as(u80, @bitCast(val.toFloat(f80, zcu)))}), | |
| 1050 | 16 => try writer.print("\"0x{x}\"", .{@as(u16, @bitCast(val.toFloat(f16, pt)))}), | |
| 1051 | 32 => try writer.print("\"0x{x}\"", .{@as(u32, @bitCast(val.toFloat(f32, pt)))}), | |
| 1052 | 64 => try writer.print("\"0x{x}\"", .{@as(u64, @bitCast(val.toFloat(f64, pt)))}), | |
| 1053 | 80 => try writer.print("\"0x{x}\"", .{@as(u80, @bitCast(val.toFloat(f80, pt)))}), | |
| 1052 | 1054 | 128 => try writer.print("\"0x{x}\"", .{@as(u128, @bitCast(f128_val))}), |
| 1053 | 1055 | else => unreachable, |
| 1054 | 1056 | }; |
| ... | ... | @@ -1056,7 +1058,7 @@ pub const DeclGen = struct { |
| 1056 | 1058 | empty = false; |
| 1057 | 1059 | } |
| 1058 | 1060 | try writer.print("{x}", .{try dg.fmtIntLiteral( |
| 1059 | try zcu.intValue_big(repr_ty, repr_val_big.toConst()), | |
| 1061 | try pt.intValue_big(repr_ty, repr_val_big.toConst()), | |
| 1060 | 1062 | location, |
| 1061 | 1063 | )}); |
| 1062 | 1064 | if (!empty) try writer.writeByte(')'); |
| ... | ... | @@ -1084,7 +1086,7 @@ pub const DeclGen = struct { |
| 1084 | 1086 | .ptr => { |
| 1085 | 1087 | var arena = std.heap.ArenaAllocator.init(zcu.gpa); |
| 1086 | 1088 | defer arena.deinit(); |
| 1087 | const derivation = try val.pointerDerivation(arena.allocator(), zcu); | |
| 1089 | const derivation = try val.pointerDerivation(arena.allocator(), pt); | |
| 1088 | 1090 | try dg.renderPointer(writer, derivation, location); |
| 1089 | 1091 | }, |
| 1090 | 1092 | .opt => |opt| switch (ctype.info(ctype_pool)) { |
| ... | ... | @@ -1167,15 +1169,15 @@ pub const DeclGen = struct { |
| 1167 | 1169 | try literal.start(); |
| 1168 | 1170 | var index: usize = 0; |
| 1169 | 1171 | while (index < ai.len) : (index += 1) { |
| 1170 | const elem_val = try val.elemValue(zcu, index); | |
| 1172 | const elem_val = try val.elemValue(pt, index); | |
| 1171 | 1173 | const elem_val_u8: u8 = if (elem_val.isUndef(zcu)) |
| 1172 | 1174 | undefPattern(u8) |
| 1173 | 1175 | else |
| 1174 | @intCast(elem_val.toUnsignedInt(zcu)); | |
| 1176 | @intCast(elem_val.toUnsignedInt(pt)); | |
| 1175 | 1177 | try literal.writeChar(elem_val_u8); |
| 1176 | 1178 | } |
| 1177 | 1179 | if (ai.sentinel) |s| { |
| 1178 | const s_u8: u8 = @intCast(s.toUnsignedInt(zcu)); | |
| 1180 | const s_u8: u8 = @intCast(s.toUnsignedInt(pt)); | |
| 1179 | 1181 | if (s_u8 != 0) try literal.writeChar(s_u8); |
| 1180 | 1182 | } |
| 1181 | 1183 | try literal.end(); |
| ... | ... | @@ -1184,7 +1186,7 @@ pub const DeclGen = struct { |
| 1184 | 1186 | var index: usize = 0; |
| 1185 | 1187 | while (index < ai.len) : (index += 1) { |
| 1186 | 1188 | if (index != 0) try writer.writeByte(','); |
| 1187 | const elem_val = try val.elemValue(zcu, index); | |
| 1189 | const elem_val = try val.elemValue(pt, index); | |
| 1188 | 1190 | try dg.renderValue(writer, elem_val, initializer_type); |
| 1189 | 1191 | } |
| 1190 | 1192 | if (ai.sentinel) |s| { |
| ... | ... | @@ -1207,13 +1209,13 @@ pub const DeclGen = struct { |
| 1207 | 1209 | const comptime_val = tuple.values.get(ip)[field_index]; |
| 1208 | 1210 | if (comptime_val != .none) continue; |
| 1209 | 1211 | const field_ty = Type.fromInterned(tuple.types.get(ip)[field_index]); |
| 1210 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 1212 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 1211 | 1213 | |
| 1212 | 1214 | if (!empty) try writer.writeByte(','); |
| 1213 | 1215 | |
| 1214 | 1216 | const field_val = Value.fromInterned( |
| 1215 | 1217 | switch (ip.indexToKey(val.toIntern()).aggregate.storage) { |
| 1216 | .bytes => |bytes| try ip.get(zcu.gpa, .{ .int = .{ | |
| 1218 | .bytes => |bytes| try pt.intern(.{ .int = .{ | |
| 1217 | 1219 | .ty = field_ty.toIntern(), |
| 1218 | 1220 | .storage = .{ .u64 = bytes.at(field_index, ip) }, |
| 1219 | 1221 | } }), |
| ... | ... | @@ -1242,12 +1244,12 @@ pub const DeclGen = struct { |
| 1242 | 1244 | var need_comma = false; |
| 1243 | 1245 | while (field_it.next()) |field_index| { |
| 1244 | 1246 | const field_ty = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]); |
| 1245 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 1247 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 1246 | 1248 | |
| 1247 | 1249 | if (need_comma) try writer.writeByte(','); |
| 1248 | 1250 | need_comma = true; |
| 1249 | 1251 | const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { |
| 1250 | .bytes => |bytes| try ip.get(zcu.gpa, .{ .int = .{ | |
| 1252 | .bytes => |bytes| try pt.intern(.{ .int = .{ | |
| 1251 | 1253 | .ty = field_ty.toIntern(), |
| 1252 | 1254 | .storage = .{ .u64 = bytes.at(field_index, ip) }, |
| 1253 | 1255 | } }), |
| ... | ... | @@ -1262,14 +1264,14 @@ pub const DeclGen = struct { |
| 1262 | 1264 | const int_info = ty.intInfo(zcu); |
| 1263 | 1265 | |
| 1264 | 1266 | const bits = Type.smallestUnsignedBits(int_info.bits - 1); |
| 1265 | const bit_offset_ty = try zcu.intType(.unsigned, bits); | |
| 1267 | const bit_offset_ty = try pt.intType(.unsigned, bits); | |
| 1266 | 1268 | |
| 1267 | 1269 | var bit_offset: u64 = 0; |
| 1268 | 1270 | var eff_num_fields: usize = 0; |
| 1269 | 1271 | |
| 1270 | 1272 | for (0..loaded_struct.field_types.len) |field_index| { |
| 1271 | 1273 | const field_ty = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]); |
| 1272 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 1274 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 1273 | 1275 | eff_num_fields += 1; |
| 1274 | 1276 | } |
| 1275 | 1277 | |
| ... | ... | @@ -1277,7 +1279,7 @@ pub const DeclGen = struct { |
| 1277 | 1279 | try writer.writeByte('('); |
| 1278 | 1280 | try dg.renderUndefValue(writer, ty, location); |
| 1279 | 1281 | try writer.writeByte(')'); |
| 1280 | } else if (ty.bitSize(zcu) > 64) { | |
| 1282 | } else if (ty.bitSize(pt) > 64) { | |
| 1281 | 1283 | // zig_or_u128(zig_or_u128(zig_shl_u128(a, a_off), zig_shl_u128(b, b_off)), zig_shl_u128(c, c_off)) |
| 1282 | 1284 | var num_or = eff_num_fields - 1; |
| 1283 | 1285 | while (num_or > 0) : (num_or -= 1) { |
| ... | ... | @@ -1290,10 +1292,10 @@ pub const DeclGen = struct { |
| 1290 | 1292 | var needs_closing_paren = false; |
| 1291 | 1293 | for (0..loaded_struct.field_types.len) |field_index| { |
| 1292 | 1294 | const field_ty = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]); |
| 1293 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 1295 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 1294 | 1296 | |
| 1295 | 1297 | const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { |
| 1296 | .bytes => |bytes| try ip.get(zcu.gpa, .{ .int = .{ | |
| 1298 | .bytes => |bytes| try pt.intern(.{ .int = .{ | |
| 1297 | 1299 | .ty = field_ty.toIntern(), |
| 1298 | 1300 | .storage = .{ .u64 = bytes.at(field_index, ip) }, |
| 1299 | 1301 | } }), |
| ... | ... | @@ -1307,7 +1309,7 @@ pub const DeclGen = struct { |
| 1307 | 1309 | try writer.writeByte('('); |
| 1308 | 1310 | try dg.renderIntCast(writer, ty, cast_context, field_ty, .FunctionArgument); |
| 1309 | 1311 | try writer.writeAll(", "); |
| 1310 | try dg.renderValue(writer, try zcu.intValue(bit_offset_ty, bit_offset), .FunctionArgument); | |
| 1312 | try dg.renderValue(writer, try pt.intValue(bit_offset_ty, bit_offset), .FunctionArgument); | |
| 1311 | 1313 | try writer.writeByte(')'); |
| 1312 | 1314 | } else { |
| 1313 | 1315 | try dg.renderIntCast(writer, ty, cast_context, field_ty, .FunctionArgument); |
| ... | ... | @@ -1316,7 +1318,7 @@ pub const DeclGen = struct { |
| 1316 | 1318 | if (needs_closing_paren) try writer.writeByte(')'); |
| 1317 | 1319 | if (eff_index != eff_num_fields - 1) try writer.writeAll(", "); |
| 1318 | 1320 | |
| 1319 | bit_offset += field_ty.bitSize(zcu); | |
| 1321 | bit_offset += field_ty.bitSize(pt); | |
| 1320 | 1322 | needs_closing_paren = true; |
| 1321 | 1323 | eff_index += 1; |
| 1322 | 1324 | } |
| ... | ... | @@ -1326,7 +1328,7 @@ pub const DeclGen = struct { |
| 1326 | 1328 | var empty = true; |
| 1327 | 1329 | for (0..loaded_struct.field_types.len) |field_index| { |
| 1328 | 1330 | const field_ty = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]); |
| 1329 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 1331 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 1330 | 1332 | |
| 1331 | 1333 | if (!empty) try writer.writeAll(" | "); |
| 1332 | 1334 | try writer.writeByte('('); |
| ... | ... | @@ -1334,7 +1336,7 @@ pub const DeclGen = struct { |
| 1334 | 1336 | try writer.writeByte(')'); |
| 1335 | 1337 | |
| 1336 | 1338 | const field_val = switch (ip.indexToKey(val.toIntern()).aggregate.storage) { |
| 1337 | .bytes => |bytes| try ip.get(zcu.gpa, .{ .int = .{ | |
| 1339 | .bytes => |bytes| try pt.intern(.{ .int = .{ | |
| 1338 | 1340 | .ty = field_ty.toIntern(), |
| 1339 | 1341 | .storage = .{ .u64 = bytes.at(field_index, ip) }, |
| 1340 | 1342 | } }), |
| ... | ... | @@ -1345,12 +1347,12 @@ pub const DeclGen = struct { |
| 1345 | 1347 | if (bit_offset != 0) { |
| 1346 | 1348 | try dg.renderValue(writer, Value.fromInterned(field_val), .Other); |
| 1347 | 1349 | try writer.writeAll(" << "); |
| 1348 | try dg.renderValue(writer, try zcu.intValue(bit_offset_ty, bit_offset), .FunctionArgument); | |
| 1350 | try dg.renderValue(writer, try pt.intValue(bit_offset_ty, bit_offset), .FunctionArgument); | |
| 1349 | 1351 | } else { |
| 1350 | 1352 | try dg.renderValue(writer, Value.fromInterned(field_val), .Other); |
| 1351 | 1353 | } |
| 1352 | 1354 | |
| 1353 | bit_offset += field_ty.bitSize(zcu); | |
| 1355 | bit_offset += field_ty.bitSize(pt); | |
| 1354 | 1356 | empty = false; |
| 1355 | 1357 | } |
| 1356 | 1358 | try writer.writeByte(')'); |
| ... | ... | @@ -1363,7 +1365,7 @@ pub const DeclGen = struct { |
| 1363 | 1365 | .un => |un| { |
| 1364 | 1366 | const loaded_union = ip.loadUnionType(ty.toIntern()); |
| 1365 | 1367 | if (un.tag == .none) { |
| 1366 | const backing_ty = try ty.unionBackingType(zcu); | |
| 1368 | const backing_ty = try ty.unionBackingType(pt); | |
| 1367 | 1369 | switch (loaded_union.getLayout(ip)) { |
| 1368 | 1370 | .@"packed" => { |
| 1369 | 1371 | if (!location.isInitializer()) { |
| ... | ... | @@ -1378,7 +1380,7 @@ pub const DeclGen = struct { |
| 1378 | 1380 | return dg.fail("TODO: C backend: implement extern union backing type rendering in static initializers", .{}); |
| 1379 | 1381 | } |
| 1380 | 1382 | |
| 1381 | const ptr_ty = try zcu.singleConstPtrType(ty); | |
| 1383 | const ptr_ty = try pt.singleConstPtrType(ty); | |
| 1382 | 1384 | try writer.writeAll("*(("); |
| 1383 | 1385 | try dg.renderType(writer, ptr_ty); |
| 1384 | 1386 | try writer.writeAll(")("); |
| ... | ... | @@ -1400,7 +1402,7 @@ pub const DeclGen = struct { |
| 1400 | 1402 | const field_ty = Type.fromInterned(loaded_union.field_types.get(ip)[field_index]); |
| 1401 | 1403 | const field_name = loaded_union.loadTagType(ip).names.get(ip)[field_index]; |
| 1402 | 1404 | if (loaded_union.getLayout(ip) == .@"packed") { |
| 1403 | if (field_ty.hasRuntimeBits(zcu)) { | |
| 1405 | if (field_ty.hasRuntimeBits(pt)) { | |
| 1404 | 1406 | if (field_ty.isPtrAtRuntime(zcu)) { |
| 1405 | 1407 | try writer.writeByte('('); |
| 1406 | 1408 | try dg.renderCType(writer, ctype); |
| ... | ... | @@ -1431,7 +1433,7 @@ pub const DeclGen = struct { |
| 1431 | 1433 | ), |
| 1432 | 1434 | .payload => { |
| 1433 | 1435 | try writer.writeByte('{'); |
| 1434 | if (field_ty.hasRuntimeBits(zcu)) { | |
| 1436 | if (field_ty.hasRuntimeBits(pt)) { | |
| 1435 | 1437 | try writer.print(" .{ } = ", .{fmtIdent(field_name.toSlice(ip))}); |
| 1436 | 1438 | try dg.renderValue( |
| 1437 | 1439 | writer, |
| ... | ... | @@ -1443,7 +1445,7 @@ pub const DeclGen = struct { |
| 1443 | 1445 | const inner_field_ty = Type.fromInterned( |
| 1444 | 1446 | loaded_union.field_types.get(ip)[inner_field_index], |
| 1445 | 1447 | ); |
| 1446 | if (!inner_field_ty.hasRuntimeBits(zcu)) continue; | |
| 1448 | if (!inner_field_ty.hasRuntimeBits(pt)) continue; | |
| 1447 | 1449 | try dg.renderUndefValue(writer, inner_field_ty, initializer_type); |
| 1448 | 1450 | break; |
| 1449 | 1451 | } |
| ... | ... | @@ -1464,7 +1466,8 @@ pub const DeclGen = struct { |
| 1464 | 1466 | ty: Type, |
| 1465 | 1467 | location: ValueRenderLocation, |
| 1466 | 1468 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 1467 | const zcu = dg.zcu; | |
| 1469 | const pt = dg.pt; | |
| 1470 | const zcu = pt.zcu; | |
| 1468 | 1471 | const ip = &zcu.intern_pool; |
| 1469 | 1472 | const target = &dg.mod.resolved_target.result; |
| 1470 | 1473 | const ctype_pool = &dg.ctype_pool; |
| ... | ... | @@ -1490,7 +1493,7 @@ pub const DeclGen = struct { |
| 1490 | 1493 | => { |
| 1491 | 1494 | const bits = ty.floatBits(target.*); |
| 1492 | 1495 | // All unsigned ints matching float types are pre-allocated. |
| 1493 | const repr_ty = zcu.intType(.unsigned, bits) catch unreachable; | |
| 1496 | const repr_ty = dg.pt.intType(.unsigned, bits) catch unreachable; | |
| 1494 | 1497 | |
| 1495 | 1498 | try writer.writeAll("zig_make_"); |
| 1496 | 1499 | try dg.renderTypeForBuiltinFnName(writer, ty); |
| ... | ... | @@ -1515,14 +1518,14 @@ pub const DeclGen = struct { |
| 1515 | 1518 | .error_set_type, |
| 1516 | 1519 | .inferred_error_set_type, |
| 1517 | 1520 | => return writer.print("{x}", .{ |
| 1518 | try dg.fmtIntLiteral(try zcu.undefValue(ty), location), | |
| 1521 | try dg.fmtIntLiteral(try pt.undefValue(ty), location), | |
| 1519 | 1522 | }), |
| 1520 | 1523 | .ptr_type => |ptr_type| switch (ptr_type.flags.size) { |
| 1521 | 1524 | .One, .Many, .C => { |
| 1522 | 1525 | try writer.writeAll("(("); |
| 1523 | 1526 | try dg.renderCType(writer, ctype); |
| 1524 | 1527 | return writer.print("){x})", .{ |
| 1525 | try dg.fmtIntLiteral(try zcu.undefValue(Type.usize), .Other), | |
| 1528 | try dg.fmtIntLiteral(try pt.undefValue(Type.usize), .Other), | |
| 1526 | 1529 | }); |
| 1527 | 1530 | }, |
| 1528 | 1531 | .Slice => { |
| ... | ... | @@ -1536,7 +1539,7 @@ pub const DeclGen = struct { |
| 1536 | 1539 | const ptr_ty = ty.slicePtrFieldType(zcu); |
| 1537 | 1540 | try dg.renderType(writer, ptr_ty); |
| 1538 | 1541 | return writer.print("){x}, {0x}}}", .{ |
| 1539 | try dg.fmtIntLiteral(try zcu.undefValue(Type.usize), .Other), | |
| 1542 | try dg.fmtIntLiteral(try dg.pt.undefValue(Type.usize), .Other), | |
| 1540 | 1543 | }); |
| 1541 | 1544 | }, |
| 1542 | 1545 | }, |
| ... | ... | @@ -1591,7 +1594,7 @@ pub const DeclGen = struct { |
| 1591 | 1594 | var need_comma = false; |
| 1592 | 1595 | while (field_it.next()) |field_index| { |
| 1593 | 1596 | const field_ty = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]); |
| 1594 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 1597 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 1595 | 1598 | |
| 1596 | 1599 | if (need_comma) try writer.writeByte(','); |
| 1597 | 1600 | need_comma = true; |
| ... | ... | @@ -1600,7 +1603,7 @@ pub const DeclGen = struct { |
| 1600 | 1603 | return writer.writeByte('}'); |
| 1601 | 1604 | }, |
| 1602 | 1605 | .@"packed" => return writer.print("{x}", .{ |
| 1603 | try dg.fmtIntLiteral(try zcu.undefValue(ty), .Other), | |
| 1606 | try dg.fmtIntLiteral(try pt.undefValue(ty), .Other), | |
| 1604 | 1607 | }), |
| 1605 | 1608 | } |
| 1606 | 1609 | }, |
| ... | ... | @@ -1616,7 +1619,7 @@ pub const DeclGen = struct { |
| 1616 | 1619 | for (0..anon_struct_info.types.len) |field_index| { |
| 1617 | 1620 | if (anon_struct_info.values.get(ip)[field_index] != .none) continue; |
| 1618 | 1621 | const field_ty = Type.fromInterned(anon_struct_info.types.get(ip)[field_index]); |
| 1619 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 1622 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 1620 | 1623 | |
| 1621 | 1624 | if (need_comma) try writer.writeByte(','); |
| 1622 | 1625 | need_comma = true; |
| ... | ... | @@ -1654,7 +1657,7 @@ pub const DeclGen = struct { |
| 1654 | 1657 | const inner_field_ty = Type.fromInterned( |
| 1655 | 1658 | loaded_union.field_types.get(ip)[inner_field_index], |
| 1656 | 1659 | ); |
| 1657 | if (!inner_field_ty.hasRuntimeBits(zcu)) continue; | |
| 1660 | if (!inner_field_ty.hasRuntimeBits(pt)) continue; | |
| 1658 | 1661 | try dg.renderUndefValue( |
| 1659 | 1662 | writer, |
| 1660 | 1663 | inner_field_ty, |
| ... | ... | @@ -1670,7 +1673,7 @@ pub const DeclGen = struct { |
| 1670 | 1673 | if (has_tag) try writer.writeByte('}'); |
| 1671 | 1674 | }, |
| 1672 | 1675 | .@"packed" => return writer.print("{x}", .{ |
| 1673 | try dg.fmtIntLiteral(try zcu.undefValue(ty), .Other), | |
| 1676 | try dg.fmtIntLiteral(try pt.undefValue(ty), .Other), | |
| 1674 | 1677 | }), |
| 1675 | 1678 | } |
| 1676 | 1679 | }, |
| ... | ... | @@ -1775,7 +1778,7 @@ pub const DeclGen = struct { |
| 1775 | 1778 | }, |
| 1776 | 1779 | }, |
| 1777 | 1780 | ) !void { |
| 1778 | const zcu = dg.zcu; | |
| 1781 | const zcu = dg.pt.zcu; | |
| 1779 | 1782 | const ip = &zcu.intern_pool; |
| 1780 | 1783 | |
| 1781 | 1784 | const fn_ty = fn_val.typeOf(zcu); |
| ... | ... | @@ -1856,7 +1859,7 @@ pub const DeclGen = struct { |
| 1856 | 1859 | |
| 1857 | 1860 | fn ctypeFromType(dg: *DeclGen, ty: Type, kind: CType.Kind) !CType { |
| 1858 | 1861 | defer std.debug.assert(dg.scratch.items.len == 0); |
| 1859 | return dg.ctype_pool.fromType(dg.gpa, &dg.scratch, ty, dg.zcu, dg.mod, kind); | |
| 1862 | return dg.ctype_pool.fromType(dg.gpa, &dg.scratch, ty, dg.pt, dg.mod, kind); | |
| 1860 | 1863 | } |
| 1861 | 1864 | |
| 1862 | 1865 | fn byteSize(dg: *DeclGen, ctype: CType) u64 { |
| ... | ... | @@ -1879,8 +1882,8 @@ pub const DeclGen = struct { |
| 1879 | 1882 | } |
| 1880 | 1883 | |
| 1881 | 1884 | fn renderCType(dg: *DeclGen, w: anytype, ctype: CType) error{OutOfMemory}!void { |
| 1882 | _ = try renderTypePrefix(dg.pass, &dg.ctype_pool, dg.zcu, w, ctype, .suffix, .{}); | |
| 1883 | try renderTypeSuffix(dg.pass, &dg.ctype_pool, dg.zcu, w, ctype, .suffix, .{}); | |
| 1885 | _ = try renderTypePrefix(dg.pass, &dg.ctype_pool, dg.pt.zcu, w, ctype, .suffix, .{}); | |
| 1886 | try renderTypeSuffix(dg.pass, &dg.ctype_pool, dg.pt.zcu, w, ctype, .suffix, .{}); | |
| 1884 | 1887 | } |
| 1885 | 1888 | |
| 1886 | 1889 | const IntCastContext = union(enum) { |
| ... | ... | @@ -1904,18 +1907,18 @@ pub const DeclGen = struct { |
| 1904 | 1907 | } |
| 1905 | 1908 | }; |
| 1906 | 1909 | fn intCastIsNoop(dg: *DeclGen, dest_ty: Type, src_ty: Type) bool { |
| 1907 | const zcu = dg.zcu; | |
| 1908 | const dest_bits = dest_ty.bitSize(zcu); | |
| 1909 | const dest_int_info = dest_ty.intInfo(zcu); | |
| 1910 | const pt = dg.pt; | |
| 1911 | const dest_bits = dest_ty.bitSize(pt); | |
| 1912 | const dest_int_info = dest_ty.intInfo(pt.zcu); | |
| 1910 | 1913 | |
| 1911 | const src_is_ptr = src_ty.isPtrAtRuntime(zcu); | |
| 1914 | const src_is_ptr = src_ty.isPtrAtRuntime(pt.zcu); | |
| 1912 | 1915 | const src_eff_ty: Type = if (src_is_ptr) switch (dest_int_info.signedness) { |
| 1913 | 1916 | .unsigned => Type.usize, |
| 1914 | 1917 | .signed => Type.isize, |
| 1915 | 1918 | } else src_ty; |
| 1916 | 1919 | |
| 1917 | const src_bits = src_eff_ty.bitSize(zcu); | |
| 1918 | const src_int_info = if (src_eff_ty.isAbiInt(zcu)) src_eff_ty.intInfo(zcu) else null; | |
| 1920 | const src_bits = src_eff_ty.bitSize(pt); | |
| 1921 | const src_int_info = if (src_eff_ty.isAbiInt(pt.zcu)) src_eff_ty.intInfo(pt.zcu) else null; | |
| 1919 | 1922 | if (dest_bits <= 64 and src_bits <= 64) { |
| 1920 | 1923 | const needs_cast = src_int_info == null or |
| 1921 | 1924 | (toCIntBits(dest_int_info.bits) != toCIntBits(src_int_info.?.bits) or |
| ... | ... | @@ -1944,8 +1947,9 @@ pub const DeclGen = struct { |
| 1944 | 1947 | src_ty: Type, |
| 1945 | 1948 | location: ValueRenderLocation, |
| 1946 | 1949 | ) !void { |
| 1947 | const zcu = dg.zcu; | |
| 1948 | const dest_bits = dest_ty.bitSize(zcu); | |
| 1950 | const pt = dg.pt; | |
| 1951 | const zcu = pt.zcu; | |
| 1952 | const dest_bits = dest_ty.bitSize(pt); | |
| 1949 | 1953 | const dest_int_info = dest_ty.intInfo(zcu); |
| 1950 | 1954 | |
| 1951 | 1955 | const src_is_ptr = src_ty.isPtrAtRuntime(zcu); |
| ... | ... | @@ -1954,7 +1958,7 @@ pub const DeclGen = struct { |
| 1954 | 1958 | .signed => Type.isize, |
| 1955 | 1959 | } else src_ty; |
| 1956 | 1960 | |
| 1957 | const src_bits = src_eff_ty.bitSize(zcu); | |
| 1961 | const src_bits = src_eff_ty.bitSize(pt); | |
| 1958 | 1962 | const src_int_info = if (src_eff_ty.isAbiInt(zcu)) src_eff_ty.intInfo(zcu) else null; |
| 1959 | 1963 | if (dest_bits <= 64 and src_bits <= 64) { |
| 1960 | 1964 | const needs_cast = src_int_info == null or |
| ... | ... | @@ -2035,7 +2039,7 @@ pub const DeclGen = struct { |
| 2035 | 2039 | qualifiers, |
| 2036 | 2040 | CType.AlignAs.fromAlignment(.{ |
| 2037 | 2041 | .@"align" = alignment, |
| 2038 | .abi = ty.abiAlignment(dg.zcu), | |
| 2042 | .abi = ty.abiAlignment(dg.pt), | |
| 2039 | 2043 | }), |
| 2040 | 2044 | ); |
| 2041 | 2045 | } |
| ... | ... | @@ -2048,6 +2052,7 @@ pub const DeclGen = struct { |
| 2048 | 2052 | qualifiers: CQualifiers, |
| 2049 | 2053 | alignas: CType.AlignAs, |
| 2050 | 2054 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 2055 | const zcu = dg.pt.zcu; | |
| 2051 | 2056 | switch (alignas.abiOrder()) { |
| 2052 | 2057 | .lt => try w.print("zig_under_align({}) ", .{alignas.toByteUnits()}), |
| 2053 | 2058 | .eq => {}, |
| ... | ... | @@ -2055,10 +2060,10 @@ pub const DeclGen = struct { |
| 2055 | 2060 | } |
| 2056 | 2061 | |
| 2057 | 2062 | try w.print("{}", .{ |
| 2058 | try renderTypePrefix(dg.pass, &dg.ctype_pool, dg.zcu, w, ctype, .suffix, qualifiers), | |
| 2063 | try renderTypePrefix(dg.pass, &dg.ctype_pool, zcu, w, ctype, .suffix, qualifiers), | |
| 2059 | 2064 | }); |
| 2060 | 2065 | try dg.writeName(w, name); |
| 2061 | try renderTypeSuffix(dg.pass, &dg.ctype_pool, dg.zcu, w, ctype, .suffix, .{}); | |
| 2066 | try renderTypeSuffix(dg.pass, &dg.ctype_pool, zcu, w, ctype, .suffix, .{}); | |
| 2062 | 2067 | } |
| 2063 | 2068 | |
| 2064 | 2069 | fn writeName(dg: *DeclGen, w: anytype, c_value: CValue) !void { |
| ... | ... | @@ -2162,7 +2167,7 @@ pub const DeclGen = struct { |
| 2162 | 2167 | decl_index: InternPool.DeclIndex, |
| 2163 | 2168 | variable: InternPool.Key.Variable, |
| 2164 | 2169 | ) !void { |
| 2165 | const zcu = dg.zcu; | |
| 2170 | const zcu = dg.pt.zcu; | |
| 2166 | 2171 | const decl = zcu.declPtr(decl_index); |
| 2167 | 2172 | const fwd = dg.fwdDeclWriter(); |
| 2168 | 2173 | try fwd.writeAll(if (variable.is_extern) "zig_extern " else "static "); |
| ... | ... | @@ -2180,7 +2185,7 @@ pub const DeclGen = struct { |
| 2180 | 2185 | } |
| 2181 | 2186 | |
| 2182 | 2187 | fn renderDeclName(dg: *DeclGen, writer: anytype, decl_index: InternPool.DeclIndex) !void { |
| 2183 | const zcu = dg.zcu; | |
| 2188 | const zcu = dg.pt.zcu; | |
| 2184 | 2189 | const ip = &zcu.intern_pool; |
| 2185 | 2190 | const decl = zcu.declPtr(decl_index); |
| 2186 | 2191 | |
| ... | ... | @@ -2236,15 +2241,15 @@ pub const DeclGen = struct { |
| 2236 | 2241 | .bits => {}, |
| 2237 | 2242 | } |
| 2238 | 2243 | |
| 2239 | const zcu = dg.zcu; | |
| 2240 | const int_info = if (ty.isAbiInt(zcu)) ty.intInfo(zcu) else std.builtin.Type.Int{ | |
| 2244 | const pt = dg.pt; | |
| 2245 | const int_info = if (ty.isAbiInt(pt.zcu)) ty.intInfo(pt.zcu) else std.builtin.Type.Int{ | |
| 2241 | 2246 | .signedness = .unsigned, |
| 2242 | .bits = @as(u16, @intCast(ty.bitSize(zcu))), | |
| 2247 | .bits = @as(u16, @intCast(ty.bitSize(pt))), | |
| 2243 | 2248 | }; |
| 2244 | 2249 | |
| 2245 | 2250 | if (is_big) try writer.print(", {}", .{int_info.signedness == .signed}); |
| 2246 | 2251 | try writer.print(", {}", .{try dg.fmtIntLiteral( |
| 2247 | try zcu.intValue(if (is_big) Type.u16 else Type.u8, int_info.bits), | |
| 2252 | try pt.intValue(if (is_big) Type.u16 else Type.u8, int_info.bits), | |
| 2248 | 2253 | .FunctionArgument, |
| 2249 | 2254 | )}); |
| 2250 | 2255 | } |
| ... | ... | @@ -2254,7 +2259,7 @@ pub const DeclGen = struct { |
| 2254 | 2259 | val: Value, |
| 2255 | 2260 | loc: ValueRenderLocation, |
| 2256 | 2261 | ) !std.fmt.Formatter(formatIntLiteral) { |
| 2257 | const zcu = dg.zcu; | |
| 2262 | const zcu = dg.pt.zcu; | |
| 2258 | 2263 | const kind = loc.toCTypeKind(); |
| 2259 | 2264 | const ty = val.typeOf(zcu); |
| 2260 | 2265 | return std.fmt.Formatter(formatIntLiteral){ .data = .{ |
| ... | ... | @@ -2616,7 +2621,8 @@ pub fn genGlobalAsm(zcu: *Zcu, writer: anytype) !void { |
| 2616 | 2621 | } |
| 2617 | 2622 | |
| 2618 | 2623 | pub fn genErrDecls(o: *Object) !void { |
| 2619 | const zcu = o.dg.zcu; | |
| 2624 | const pt = o.dg.pt; | |
| 2625 | const zcu = pt.zcu; | |
| 2620 | 2626 | const ip = &zcu.intern_pool; |
| 2621 | 2627 | const writer = o.writer(); |
| 2622 | 2628 | |
| ... | ... | @@ -2628,7 +2634,7 @@ pub fn genErrDecls(o: *Object) !void { |
| 2628 | 2634 | for (zcu.global_error_set.keys()[1..], 1..) |name_nts, value| { |
| 2629 | 2635 | const name = name_nts.toSlice(ip); |
| 2630 | 2636 | max_name_len = @max(name.len, max_name_len); |
| 2631 | const err_val = try zcu.intern(.{ .err = .{ | |
| 2637 | const err_val = try pt.intern(.{ .err = .{ | |
| 2632 | 2638 | .ty = .anyerror_type, |
| 2633 | 2639 | .name = name_nts, |
| 2634 | 2640 | } }); |
| ... | ... | @@ -2649,12 +2655,12 @@ pub fn genErrDecls(o: *Object) !void { |
| 2649 | 2655 | @memcpy(name_buf[name_prefix.len..][0..name_slice.len], name_slice); |
| 2650 | 2656 | const identifier = name_buf[0 .. name_prefix.len + name_slice.len]; |
| 2651 | 2657 | |
| 2652 | const name_ty = try zcu.arrayType(.{ | |
| 2658 | const name_ty = try pt.arrayType(.{ | |
| 2653 | 2659 | .len = name_slice.len, |
| 2654 | 2660 | .child = .u8_type, |
| 2655 | 2661 | .sentinel = .zero_u8, |
| 2656 | 2662 | }); |
| 2657 | const name_val = try zcu.intern(.{ .aggregate = .{ | |
| 2663 | const name_val = try pt.intern(.{ .aggregate = .{ | |
| 2658 | 2664 | .ty = name_ty.toIntern(), |
| 2659 | 2665 | .storage = .{ .bytes = name.toString() }, |
| 2660 | 2666 | } }); |
| ... | ... | @@ -2673,7 +2679,7 @@ pub fn genErrDecls(o: *Object) !void { |
| 2673 | 2679 | try writer.writeAll(";\n"); |
| 2674 | 2680 | } |
| 2675 | 2681 | |
| 2676 | const name_array_ty = try zcu.arrayType(.{ | |
| 2682 | const name_array_ty = try pt.arrayType(.{ | |
| 2677 | 2683 | .len = zcu.global_error_set.count(), |
| 2678 | 2684 | .child = .slice_const_u8_sentinel_0_type, |
| 2679 | 2685 | }); |
| ... | ... | @@ -2693,14 +2699,15 @@ pub fn genErrDecls(o: *Object) !void { |
| 2693 | 2699 | if (value != 0) try writer.writeByte(','); |
| 2694 | 2700 | try writer.print("{{" ++ name_prefix ++ "{}, {}}}", .{ |
| 2695 | 2701 | fmtIdent(name), |
| 2696 | try o.dg.fmtIntLiteral(try zcu.intValue(Type.usize, name.len), .StaticInitializer), | |
| 2702 | try o.dg.fmtIntLiteral(try pt.intValue(Type.usize, name.len), .StaticInitializer), | |
| 2697 | 2703 | }); |
| 2698 | 2704 | } |
| 2699 | 2705 | try writer.writeAll("};\n"); |
| 2700 | 2706 | } |
| 2701 | 2707 | |
| 2702 | 2708 | pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFnMap.Entry) !void { |
| 2703 | const zcu = o.dg.zcu; | |
| 2709 | const pt = o.dg.pt; | |
| 2710 | const zcu = pt.zcu; | |
| 2704 | 2711 | const ip = &zcu.intern_pool; |
| 2705 | 2712 | const ctype_pool = &o.dg.ctype_pool; |
| 2706 | 2713 | const w = o.writer(); |
| ... | ... | @@ -2721,20 +2728,20 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn |
| 2721 | 2728 | for (0..tag_names.len) |tag_index| { |
| 2722 | 2729 | const tag_name = tag_names.get(ip)[tag_index]; |
| 2723 | 2730 | const tag_name_len = tag_name.length(ip); |
| 2724 | const tag_val = try zcu.enumValueFieldIndex(enum_ty, @intCast(tag_index)); | |
| 2731 | const tag_val = try pt.enumValueFieldIndex(enum_ty, @intCast(tag_index)); | |
| 2725 | 2732 | |
| 2726 | const name_ty = try zcu.arrayType(.{ | |
| 2733 | const name_ty = try pt.arrayType(.{ | |
| 2727 | 2734 | .len = tag_name_len, |
| 2728 | 2735 | .child = .u8_type, |
| 2729 | 2736 | .sentinel = .zero_u8, |
| 2730 | 2737 | }); |
| 2731 | const name_val = try zcu.intern(.{ .aggregate = .{ | |
| 2738 | const name_val = try pt.intern(.{ .aggregate = .{ | |
| 2732 | 2739 | .ty = name_ty.toIntern(), |
| 2733 | 2740 | .storage = .{ .bytes = tag_name.toString() }, |
| 2734 | 2741 | } }); |
| 2735 | 2742 | |
| 2736 | 2743 | try w.print(" case {}: {{\n static ", .{ |
| 2737 | try o.dg.fmtIntLiteral(try tag_val.intFromEnum(enum_ty, zcu), .Other), | |
| 2744 | try o.dg.fmtIntLiteral(try tag_val.intFromEnum(enum_ty, pt), .Other), | |
| 2738 | 2745 | }); |
| 2739 | 2746 | try o.dg.renderTypeAndName(w, name_ty, .{ .identifier = "name" }, Const, .none, .complete); |
| 2740 | 2747 | try w.writeAll(" = "); |
| ... | ... | @@ -2743,7 +2750,7 @@ pub fn genLazyFn(o: *Object, lazy_ctype_pool: *const CType.Pool, lazy_fn: LazyFn |
| 2743 | 2750 | try o.dg.renderType(w, name_slice_ty); |
| 2744 | 2751 | try w.print("){{{}, {}}};\n", .{ |
| 2745 | 2752 | fmtIdent("name"), |
| 2746 | try o.dg.fmtIntLiteral(try zcu.intValue(Type.usize, tag_name_len), .Other), | |
| 2753 | try o.dg.fmtIntLiteral(try pt.intValue(Type.usize, tag_name_len), .Other), | |
| 2747 | 2754 | }); |
| 2748 | 2755 | |
| 2749 | 2756 | try w.writeAll(" }\n"); |
| ... | ... | @@ -2788,7 +2795,7 @@ pub fn genFunc(f: *Function) !void { |
| 2788 | 2795 | defer tracy.end(); |
| 2789 | 2796 | |
| 2790 | 2797 | const o = &f.object; |
| 2791 | const zcu = o.dg.zcu; | |
| 2798 | const zcu = o.dg.pt.zcu; | |
| 2792 | 2799 | const gpa = o.dg.gpa; |
| 2793 | 2800 | const decl_index = o.dg.pass.decl; |
| 2794 | 2801 | const decl = zcu.declPtr(decl_index); |
| ... | ... | @@ -2879,12 +2886,13 @@ pub fn genDecl(o: *Object) !void { |
| 2879 | 2886 | const tracy = trace(@src()); |
| 2880 | 2887 | defer tracy.end(); |
| 2881 | 2888 | |
| 2882 | const zcu = o.dg.zcu; | |
| 2889 | const pt = o.dg.pt; | |
| 2890 | const zcu = pt.zcu; | |
| 2883 | 2891 | const decl_index = o.dg.pass.decl; |
| 2884 | 2892 | const decl = zcu.declPtr(decl_index); |
| 2885 | 2893 | const decl_ty = decl.typeOf(zcu); |
| 2886 | 2894 | |
| 2887 | if (!decl_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) return; | |
| 2895 | if (!decl_ty.isFnOrHasRuntimeBitsIgnoreComptime(pt)) return; | |
| 2888 | 2896 | if (decl.val.getExternFunc(zcu)) |_| { |
| 2889 | 2897 | const fwd = o.dg.fwdDeclWriter(); |
| 2890 | 2898 | try fwd.writeAll("zig_extern "); |
| ... | ... | @@ -2928,7 +2936,7 @@ pub fn genDeclValue( |
| 2928 | 2936 | alignment: Alignment, |
| 2929 | 2937 | @"linksection": InternPool.OptionalNullTerminatedString, |
| 2930 | 2938 | ) !void { |
| 2931 | const zcu = o.dg.zcu; | |
| 2939 | const zcu = o.dg.pt.zcu; | |
| 2932 | 2940 | const ty = val.typeOf(zcu); |
| 2933 | 2941 | |
| 2934 | 2942 | const fwd = o.dg.fwdDeclWriter(); |
| ... | ... | @@ -2946,7 +2954,7 @@ pub fn genDeclValue( |
| 2946 | 2954 | } |
| 2947 | 2955 | |
| 2948 | 2956 | pub fn genExports(dg: *DeclGen, exported: Zcu.Exported, export_indices: []const u32) !void { |
| 2949 | const zcu = dg.zcu; | |
| 2957 | const zcu = dg.pt.zcu; | |
| 2950 | 2958 | const ip = &zcu.intern_pool; |
| 2951 | 2959 | const fwd = dg.fwdDeclWriter(); |
| 2952 | 2960 | |
| ... | ... | @@ -3088,7 +3096,7 @@ fn genBodyResolveState(f: *Function, inst: Air.Inst.Index, leading_deaths: []con |
| 3088 | 3096 | } |
| 3089 | 3097 | |
| 3090 | 3098 | fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutOfMemory }!void { |
| 3091 | const zcu = f.object.dg.zcu; | |
| 3099 | const zcu = f.object.dg.pt.zcu; | |
| 3092 | 3100 | const ip = &zcu.intern_pool; |
| 3093 | 3101 | const air_tags = f.air.instructions.items(.tag); |
| 3094 | 3102 | const air_datas = f.air.instructions.items(.data); |
| ... | ... | @@ -3388,10 +3396,10 @@ fn airSliceField(f: *Function, inst: Air.Inst.Index, is_ptr: bool, field_name: [ |
| 3388 | 3396 | } |
| 3389 | 3397 | |
| 3390 | 3398 | fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3391 | const zcu = f.object.dg.zcu; | |
| 3399 | const pt = f.object.dg.pt; | |
| 3392 | 3400 | const inst_ty = f.typeOfIndex(inst); |
| 3393 | 3401 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3394 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 3402 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3395 | 3403 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3396 | 3404 | return .none; |
| 3397 | 3405 | } |
| ... | ... | @@ -3414,13 +3422,14 @@ fn airPtrElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3414 | 3422 | } |
| 3415 | 3423 | |
| 3416 | 3424 | fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3417 | const zcu = f.object.dg.zcu; | |
| 3425 | const pt = f.object.dg.pt; | |
| 3426 | const zcu = pt.zcu; | |
| 3418 | 3427 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3419 | 3428 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3420 | 3429 | |
| 3421 | 3430 | const inst_ty = f.typeOfIndex(inst); |
| 3422 | 3431 | const ptr_ty = f.typeOf(bin_op.lhs); |
| 3423 | const elem_has_bits = ptr_ty.elemType2(zcu).hasRuntimeBitsIgnoreComptime(zcu); | |
| 3432 | const elem_has_bits = ptr_ty.elemType2(zcu).hasRuntimeBitsIgnoreComptime(pt); | |
| 3424 | 3433 | |
| 3425 | 3434 | const ptr = try f.resolveInst(bin_op.lhs); |
| 3426 | 3435 | const index = try f.resolveInst(bin_op.rhs); |
| ... | ... | @@ -3449,10 +3458,10 @@ fn airPtrElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3449 | 3458 | } |
| 3450 | 3459 | |
| 3451 | 3460 | fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3452 | const zcu = f.object.dg.zcu; | |
| 3461 | const pt = f.object.dg.pt; | |
| 3453 | 3462 | const inst_ty = f.typeOfIndex(inst); |
| 3454 | 3463 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3455 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 3464 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3456 | 3465 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3457 | 3466 | return .none; |
| 3458 | 3467 | } |
| ... | ... | @@ -3475,14 +3484,15 @@ fn airSliceElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3475 | 3484 | } |
| 3476 | 3485 | |
| 3477 | 3486 | fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3478 | const zcu = f.object.dg.zcu; | |
| 3487 | const pt = f.object.dg.pt; | |
| 3488 | const zcu = pt.zcu; | |
| 3479 | 3489 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3480 | 3490 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3481 | 3491 | |
| 3482 | 3492 | const inst_ty = f.typeOfIndex(inst); |
| 3483 | 3493 | const slice_ty = f.typeOf(bin_op.lhs); |
| 3484 | 3494 | const elem_ty = slice_ty.elemType2(zcu); |
| 3485 | const elem_has_bits = elem_ty.hasRuntimeBitsIgnoreComptime(zcu); | |
| 3495 | const elem_has_bits = elem_ty.hasRuntimeBitsIgnoreComptime(pt); | |
| 3486 | 3496 | |
| 3487 | 3497 | const slice = try f.resolveInst(bin_op.lhs); |
| 3488 | 3498 | const index = try f.resolveInst(bin_op.rhs); |
| ... | ... | @@ -3505,10 +3515,10 @@ fn airSliceElemPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3505 | 3515 | } |
| 3506 | 3516 | |
| 3507 | 3517 | fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3508 | const zcu = f.object.dg.zcu; | |
| 3518 | const pt = f.object.dg.pt; | |
| 3509 | 3519 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3510 | 3520 | const inst_ty = f.typeOfIndex(inst); |
| 3511 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 3521 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3512 | 3522 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 3513 | 3523 | return .none; |
| 3514 | 3524 | } |
| ... | ... | @@ -3531,40 +3541,40 @@ fn airArrayElemVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3531 | 3541 | } |
| 3532 | 3542 | |
| 3533 | 3543 | fn airAlloc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3534 | const zcu = f.object.dg.zcu; | |
| 3544 | const pt = f.object.dg.pt; | |
| 3545 | const zcu = pt.zcu; | |
| 3535 | 3546 | const inst_ty = f.typeOfIndex(inst); |
| 3536 | 3547 | const elem_ty = inst_ty.childType(zcu); |
| 3537 | if (!elem_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) return .{ .undef = inst_ty }; | |
| 3548 | if (!elem_ty.isFnOrHasRuntimeBitsIgnoreComptime(pt)) return .{ .undef = inst_ty }; | |
| 3538 | 3549 | |
| 3539 | 3550 | const local = try f.allocLocalValue(.{ |
| 3540 | 3551 | .ctype = try f.ctypeFromType(elem_ty, .complete), |
| 3541 | 3552 | .alignas = CType.AlignAs.fromAlignment(.{ |
| 3542 | 3553 | .@"align" = inst_ty.ptrInfo(zcu).flags.alignment, |
| 3543 | .abi = elem_ty.abiAlignment(zcu), | |
| 3554 | .abi = elem_ty.abiAlignment(pt), | |
| 3544 | 3555 | }), |
| 3545 | 3556 | }); |
| 3546 | 3557 | log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.new_local }); |
| 3547 | const gpa = f.object.dg.zcu.gpa; | |
| 3548 | try f.allocs.put(gpa, local.new_local, true); | |
| 3558 | try f.allocs.put(zcu.gpa, local.new_local, true); | |
| 3549 | 3559 | return .{ .local_ref = local.new_local }; |
| 3550 | 3560 | } |
| 3551 | 3561 | |
| 3552 | 3562 | fn airRetPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3553 | const zcu = f.object.dg.zcu; | |
| 3563 | const pt = f.object.dg.pt; | |
| 3564 | const zcu = pt.zcu; | |
| 3554 | 3565 | const inst_ty = f.typeOfIndex(inst); |
| 3555 | 3566 | const elem_ty = inst_ty.childType(zcu); |
| 3556 | if (!elem_ty.isFnOrHasRuntimeBitsIgnoreComptime(zcu)) return .{ .undef = inst_ty }; | |
| 3567 | if (!elem_ty.isFnOrHasRuntimeBitsIgnoreComptime(pt)) return .{ .undef = inst_ty }; | |
| 3557 | 3568 | |
| 3558 | 3569 | const local = try f.allocLocalValue(.{ |
| 3559 | 3570 | .ctype = try f.ctypeFromType(elem_ty, .complete), |
| 3560 | 3571 | .alignas = CType.AlignAs.fromAlignment(.{ |
| 3561 | 3572 | .@"align" = inst_ty.ptrInfo(zcu).flags.alignment, |
| 3562 | .abi = elem_ty.abiAlignment(zcu), | |
| 3573 | .abi = elem_ty.abiAlignment(pt), | |
| 3563 | 3574 | }), |
| 3564 | 3575 | }); |
| 3565 | 3576 | log.debug("%{d}: allocated unfreeable t{d}", .{ inst, local.new_local }); |
| 3566 | const gpa = f.object.dg.zcu.gpa; | |
| 3567 | try f.allocs.put(gpa, local.new_local, true); | |
| 3577 | try f.allocs.put(zcu.gpa, local.new_local, true); | |
| 3568 | 3578 | return .{ .local_ref = local.new_local }; |
| 3569 | 3579 | } |
| 3570 | 3580 | |
| ... | ... | @@ -3593,7 +3603,8 @@ fn airArg(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3593 | 3603 | } |
| 3594 | 3604 | |
| 3595 | 3605 | fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3596 | const zcu = f.object.dg.zcu; | |
| 3606 | const pt = f.object.dg.pt; | |
| 3607 | const zcu = pt.zcu; | |
| 3597 | 3608 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3598 | 3609 | |
| 3599 | 3610 | const ptr_ty = f.typeOf(ty_op.operand); |
| ... | ... | @@ -3601,7 +3612,7 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3601 | 3612 | const ptr_info = ptr_scalar_ty.ptrInfo(zcu); |
| 3602 | 3613 | const src_ty = Type.fromInterned(ptr_info.child); |
| 3603 | 3614 | |
| 3604 | if (!src_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 3615 | if (!src_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3605 | 3616 | try reap(f, inst, &.{ty_op.operand}); |
| 3606 | 3617 | return .none; |
| 3607 | 3618 | } |
| ... | ... | @@ -3611,10 +3622,10 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3611 | 3622 | try reap(f, inst, &.{ty_op.operand}); |
| 3612 | 3623 | |
| 3613 | 3624 | const is_aligned = if (ptr_info.flags.alignment != .none) |
| 3614 | ptr_info.flags.alignment.order(src_ty.abiAlignment(zcu)).compare(.gte) | |
| 3625 | ptr_info.flags.alignment.order(src_ty.abiAlignment(pt)).compare(.gte) | |
| 3615 | 3626 | else |
| 3616 | 3627 | true; |
| 3617 | const is_array = lowersToArray(src_ty, zcu); | |
| 3628 | const is_array = lowersToArray(src_ty, pt); | |
| 3618 | 3629 | const need_memcpy = !is_aligned or is_array; |
| 3619 | 3630 | |
| 3620 | 3631 | const writer = f.object.writer(); |
| ... | ... | @@ -3634,12 +3645,12 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3634 | 3645 | try writer.writeAll("))"); |
| 3635 | 3646 | } else if (ptr_info.packed_offset.host_size > 0 and ptr_info.flags.vector_index == .none) { |
| 3636 | 3647 | const host_bits: u16 = ptr_info.packed_offset.host_size * 8; |
| 3637 | const host_ty = try zcu.intType(.unsigned, host_bits); | |
| 3648 | const host_ty = try pt.intType(.unsigned, host_bits); | |
| 3638 | 3649 | |
| 3639 | const bit_offset_ty = try zcu.intType(.unsigned, Type.smallestUnsignedBits(host_bits - 1)); | |
| 3640 | const bit_offset_val = try zcu.intValue(bit_offset_ty, ptr_info.packed_offset.bit_offset); | |
| 3650 | const bit_offset_ty = try pt.intType(.unsigned, Type.smallestUnsignedBits(host_bits - 1)); | |
| 3651 | const bit_offset_val = try pt.intValue(bit_offset_ty, ptr_info.packed_offset.bit_offset); | |
| 3641 | 3652 | |
| 3642 | const field_ty = try zcu.intType(.unsigned, @as(u16, @intCast(src_ty.bitSize(zcu)))); | |
| 3653 | const field_ty = try pt.intType(.unsigned, @as(u16, @intCast(src_ty.bitSize(pt)))); | |
| 3643 | 3654 | |
| 3644 | 3655 | try f.writeCValue(writer, local, .Other); |
| 3645 | 3656 | try v.elem(f, writer); |
| ... | ... | @@ -3650,9 +3661,9 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3650 | 3661 | try writer.writeAll("(("); |
| 3651 | 3662 | try f.renderType(writer, field_ty); |
| 3652 | 3663 | try writer.writeByte(')'); |
| 3653 | const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64; | |
| 3664 | const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(pt) > 64; | |
| 3654 | 3665 | if (cant_cast) { |
| 3655 | if (field_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{}); | |
| 3666 | if (field_ty.bitSize(pt) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{}); | |
| 3656 | 3667 | try writer.writeAll("zig_lo_"); |
| 3657 | 3668 | try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty); |
| 3658 | 3669 | try writer.writeByte('('); |
| ... | ... | @@ -3680,7 +3691,8 @@ fn airLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3680 | 3691 | } |
| 3681 | 3692 | |
| 3682 | 3693 | fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { |
| 3683 | const zcu = f.object.dg.zcu; | |
| 3694 | const pt = f.object.dg.pt; | |
| 3695 | const zcu = pt.zcu; | |
| 3684 | 3696 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 3685 | 3697 | const writer = f.object.writer(); |
| 3686 | 3698 | const op_inst = un_op.toIndex(); |
| ... | ... | @@ -3695,11 +3707,11 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { |
| 3695 | 3707 | const operand = try f.resolveInst(un_op); |
| 3696 | 3708 | try reap(f, inst, &.{un_op}); |
| 3697 | 3709 | var deref = is_ptr; |
| 3698 | const is_array = lowersToArray(ret_ty, zcu); | |
| 3710 | const is_array = lowersToArray(ret_ty, pt); | |
| 3699 | 3711 | const ret_val = if (is_array) ret_val: { |
| 3700 | 3712 | const array_local = try f.allocAlignedLocal(inst, .{ |
| 3701 | 3713 | .ctype = ret_ctype, |
| 3702 | .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(f.object.dg.zcu)), | |
| 3714 | .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(pt)), | |
| 3703 | 3715 | }); |
| 3704 | 3716 | try writer.writeAll("memcpy("); |
| 3705 | 3717 | try f.writeCValueMember(writer, array_local, .{ .identifier = "array" }); |
| ... | ... | @@ -3733,7 +3745,8 @@ fn airRet(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { |
| 3733 | 3745 | } |
| 3734 | 3746 | |
| 3735 | 3747 | fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3736 | const zcu = f.object.dg.zcu; | |
| 3748 | const pt = f.object.dg.pt; | |
| 3749 | const zcu = pt.zcu; | |
| 3737 | 3750 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3738 | 3751 | |
| 3739 | 3752 | const operand = try f.resolveInst(ty_op.operand); |
| ... | ... | @@ -3760,7 +3773,8 @@ fn airIntCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3760 | 3773 | } |
| 3761 | 3774 | |
| 3762 | 3775 | fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3763 | const zcu = f.object.dg.zcu; | |
| 3776 | const pt = f.object.dg.pt; | |
| 3777 | const zcu = pt.zcu; | |
| 3764 | 3778 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3765 | 3779 | |
| 3766 | 3780 | const operand = try f.resolveInst(ty_op.operand); |
| ... | ... | @@ -3809,13 +3823,13 @@ fn airTrunc(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3809 | 3823 | try f.writeCValue(writer, operand, .FunctionArgument); |
| 3810 | 3824 | try v.elem(f, writer); |
| 3811 | 3825 | try writer.print(", {x})", .{ |
| 3812 | try f.fmtIntLiteral(try inst_scalar_ty.maxIntScalar(zcu, scalar_ty)), | |
| 3826 | try f.fmtIntLiteral(try inst_scalar_ty.maxIntScalar(pt, scalar_ty)), | |
| 3813 | 3827 | }); |
| 3814 | 3828 | }, |
| 3815 | 3829 | .signed => { |
| 3816 | 3830 | const c_bits = toCIntBits(scalar_int_info.bits) orelse |
| 3817 | 3831 | return f.fail("TODO: C backend: implement integer types larger than 128 bits", .{}); |
| 3818 | const shift_val = try zcu.intValue(Type.u8, c_bits - dest_bits); | |
| 3832 | const shift_val = try pt.intValue(Type.u8, c_bits - dest_bits); | |
| 3819 | 3833 | |
| 3820 | 3834 | try writer.writeAll("zig_shr_"); |
| 3821 | 3835 | try f.object.dg.renderTypeForBuiltinFnName(writer, scalar_ty); |
| ... | ... | @@ -3860,7 +3874,8 @@ fn airIntFromBool(f: *Function, inst: Air.Inst.Index) !CValue { |
| 3860 | 3874 | } |
| 3861 | 3875 | |
| 3862 | 3876 | fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3863 | const zcu = f.object.dg.zcu; | |
| 3877 | const pt = f.object.dg.pt; | |
| 3878 | const zcu = pt.zcu; | |
| 3864 | 3879 | // *a = b; |
| 3865 | 3880 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3866 | 3881 | |
| ... | ... | @@ -3871,7 +3886,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3871 | 3886 | const ptr_val = try f.resolveInst(bin_op.lhs); |
| 3872 | 3887 | const src_ty = f.typeOf(bin_op.rhs); |
| 3873 | 3888 | |
| 3874 | const val_is_undef = if (try f.air.value(bin_op.rhs, zcu)) |v| v.isUndefDeep(zcu) else false; | |
| 3889 | const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |v| v.isUndefDeep(zcu) else false; | |
| 3875 | 3890 | |
| 3876 | 3891 | if (val_is_undef) { |
| 3877 | 3892 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| ... | ... | @@ -3887,10 +3902,10 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3887 | 3902 | } |
| 3888 | 3903 | |
| 3889 | 3904 | const is_aligned = if (ptr_info.flags.alignment != .none) |
| 3890 | ptr_info.flags.alignment.order(src_ty.abiAlignment(zcu)).compare(.gte) | |
| 3905 | ptr_info.flags.alignment.order(src_ty.abiAlignment(pt)).compare(.gte) | |
| 3891 | 3906 | else |
| 3892 | 3907 | true; |
| 3893 | const is_array = lowersToArray(Type.fromInterned(ptr_info.child), zcu); | |
| 3908 | const is_array = lowersToArray(Type.fromInterned(ptr_info.child), pt); | |
| 3894 | 3909 | const need_memcpy = !is_aligned or is_array; |
| 3895 | 3910 | |
| 3896 | 3911 | const src_val = try f.resolveInst(bin_op.rhs); |
| ... | ... | @@ -3901,7 +3916,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3901 | 3916 | if (need_memcpy) { |
| 3902 | 3917 | // For this memcpy to safely work we need the rhs to have the same |
| 3903 | 3918 | // underlying type as the lhs (i.e. they must both be arrays of the same underlying type). |
| 3904 | assert(src_ty.eql(Type.fromInterned(ptr_info.child), f.object.dg.zcu)); | |
| 3919 | assert(src_ty.eql(Type.fromInterned(ptr_info.child), zcu)); | |
| 3905 | 3920 | |
| 3906 | 3921 | // If the source is a constant, writeCValue will emit a brace initialization |
| 3907 | 3922 | // so work around this by initializing into new local. |
| ... | ... | @@ -3932,12 +3947,12 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3932 | 3947 | try v.end(f, inst, writer); |
| 3933 | 3948 | } else if (ptr_info.packed_offset.host_size > 0 and ptr_info.flags.vector_index == .none) { |
| 3934 | 3949 | const host_bits = ptr_info.packed_offset.host_size * 8; |
| 3935 | const host_ty = try zcu.intType(.unsigned, host_bits); | |
| 3950 | const host_ty = try pt.intType(.unsigned, host_bits); | |
| 3936 | 3951 | |
| 3937 | const bit_offset_ty = try zcu.intType(.unsigned, Type.smallestUnsignedBits(host_bits - 1)); | |
| 3938 | const bit_offset_val = try zcu.intValue(bit_offset_ty, ptr_info.packed_offset.bit_offset); | |
| 3952 | const bit_offset_ty = try pt.intType(.unsigned, Type.smallestUnsignedBits(host_bits - 1)); | |
| 3953 | const bit_offset_val = try pt.intValue(bit_offset_ty, ptr_info.packed_offset.bit_offset); | |
| 3939 | 3954 | |
| 3940 | const src_bits = src_ty.bitSize(zcu); | |
| 3955 | const src_bits = src_ty.bitSize(pt); | |
| 3941 | 3956 | |
| 3942 | 3957 | const ExpectedContents = [BigInt.Managed.default_capacity]BigIntLimb; |
| 3943 | 3958 | var stack align(@alignOf(ExpectedContents)) = |
| ... | ... | @@ -3950,7 +3965,7 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3950 | 3965 | try mask.shiftLeft(&mask, ptr_info.packed_offset.bit_offset); |
| 3951 | 3966 | try mask.bitNotWrap(&mask, .unsigned, host_bits); |
| 3952 | 3967 | |
| 3953 | const mask_val = try zcu.intValue_big(host_ty, mask.toConst()); | |
| 3968 | const mask_val = try pt.intValue_big(host_ty, mask.toConst()); | |
| 3954 | 3969 | |
| 3955 | 3970 | const v = try Vectorize.start(f, inst, writer, ptr_ty); |
| 3956 | 3971 | const a = try Assignment.start(f, writer, src_scalar_ctype); |
| ... | ... | @@ -3967,9 +3982,9 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 3967 | 3982 | try writer.print(", {x}), zig_shl_", .{try f.fmtIntLiteral(mask_val)}); |
| 3968 | 3983 | try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty); |
| 3969 | 3984 | try writer.writeByte('('); |
| 3970 | const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(zcu) > 64; | |
| 3985 | const cant_cast = host_ty.isInt(zcu) and host_ty.bitSize(pt) > 64; | |
| 3971 | 3986 | if (cant_cast) { |
| 3972 | if (src_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{}); | |
| 3987 | if (src_ty.bitSize(pt) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{}); | |
| 3973 | 3988 | try writer.writeAll("zig_make_"); |
| 3974 | 3989 | try f.object.dg.renderTypeForBuiltinFnName(writer, host_ty); |
| 3975 | 3990 | try writer.writeAll("(0, "); |
| ... | ... | @@ -4013,7 +4028,8 @@ fn airStore(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 4013 | 4028 | } |
| 4014 | 4029 | |
| 4015 | 4030 | fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: BuiltinInfo) !CValue { |
| 4016 | const zcu = f.object.dg.zcu; | |
| 4031 | const pt = f.object.dg.pt; | |
| 4032 | const zcu = pt.zcu; | |
| 4017 | 4033 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4018 | 4034 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4019 | 4035 | |
| ... | ... | @@ -4051,7 +4067,8 @@ fn airOverflow(f: *Function, inst: Air.Inst.Index, operation: []const u8, info: |
| 4051 | 4067 | } |
| 4052 | 4068 | |
| 4053 | 4069 | fn airNot(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4054 | const zcu = f.object.dg.zcu; | |
| 4070 | const pt = f.object.dg.pt; | |
| 4071 | const zcu = pt.zcu; | |
| 4055 | 4072 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4056 | 4073 | const operand_ty = f.typeOf(ty_op.operand); |
| 4057 | 4074 | const scalar_ty = operand_ty.scalarType(zcu); |
| ... | ... | @@ -4084,11 +4101,12 @@ fn airBinOp( |
| 4084 | 4101 | operation: []const u8, |
| 4085 | 4102 | info: BuiltinInfo, |
| 4086 | 4103 | ) !CValue { |
| 4087 | const zcu = f.object.dg.zcu; | |
| 4104 | const pt = f.object.dg.pt; | |
| 4105 | const zcu = pt.zcu; | |
| 4088 | 4106 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4089 | 4107 | const operand_ty = f.typeOf(bin_op.lhs); |
| 4090 | 4108 | const scalar_ty = operand_ty.scalarType(zcu); |
| 4091 | if ((scalar_ty.isInt(zcu) and scalar_ty.bitSize(zcu) > 64) or scalar_ty.isRuntimeFloat()) | |
| 4109 | if ((scalar_ty.isInt(zcu) and scalar_ty.bitSize(pt) > 64) or scalar_ty.isRuntimeFloat()) | |
| 4092 | 4110 | return try airBinBuiltinCall(f, inst, operation, info); |
| 4093 | 4111 | |
| 4094 | 4112 | const lhs = try f.resolveInst(bin_op.lhs); |
| ... | ... | @@ -4122,11 +4140,12 @@ fn airCmpOp( |
| 4122 | 4140 | data: anytype, |
| 4123 | 4141 | operator: std.math.CompareOperator, |
| 4124 | 4142 | ) !CValue { |
| 4125 | const zcu = f.object.dg.zcu; | |
| 4143 | const pt = f.object.dg.pt; | |
| 4144 | const zcu = pt.zcu; | |
| 4126 | 4145 | const lhs_ty = f.typeOf(data.lhs); |
| 4127 | 4146 | const scalar_ty = lhs_ty.scalarType(zcu); |
| 4128 | 4147 | |
| 4129 | const scalar_bits = scalar_ty.bitSize(zcu); | |
| 4148 | const scalar_bits = scalar_ty.bitSize(pt); | |
| 4130 | 4149 | if (scalar_ty.isInt(zcu) and scalar_bits > 64) |
| 4131 | 4150 | return airCmpBuiltinCall( |
| 4132 | 4151 | f, |
| ... | ... | @@ -4170,12 +4189,13 @@ fn airEquality( |
| 4170 | 4189 | inst: Air.Inst.Index, |
| 4171 | 4190 | operator: std.math.CompareOperator, |
| 4172 | 4191 | ) !CValue { |
| 4173 | const zcu = f.object.dg.zcu; | |
| 4192 | const pt = f.object.dg.pt; | |
| 4193 | const zcu = pt.zcu; | |
| 4174 | 4194 | const ctype_pool = &f.object.dg.ctype_pool; |
| 4175 | 4195 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4176 | 4196 | |
| 4177 | 4197 | const operand_ty = f.typeOf(bin_op.lhs); |
| 4178 | const operand_bits = operand_ty.bitSize(zcu); | |
| 4198 | const operand_bits = operand_ty.bitSize(pt); | |
| 4179 | 4199 | if (operand_ty.isAbiInt(zcu) and operand_bits > 64) |
| 4180 | 4200 | return airCmpBuiltinCall( |
| 4181 | 4201 | f, |
| ... | ... | @@ -4256,7 +4276,8 @@ fn airCmpLtErrorsLen(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4256 | 4276 | } |
| 4257 | 4277 | |
| 4258 | 4278 | fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 4259 | const zcu = f.object.dg.zcu; | |
| 4279 | const pt = f.object.dg.pt; | |
| 4280 | const zcu = pt.zcu; | |
| 4260 | 4281 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4261 | 4282 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4262 | 4283 | |
| ... | ... | @@ -4267,7 +4288,7 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 4267 | 4288 | const inst_ty = f.typeOfIndex(inst); |
| 4268 | 4289 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 4269 | 4290 | const elem_ty = inst_scalar_ty.elemType2(zcu); |
| 4270 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(zcu)) return f.moveCValue(inst, inst_ty, lhs); | |
| 4291 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(pt)) return f.moveCValue(inst, inst_ty, lhs); | |
| 4271 | 4292 | const inst_scalar_ctype = try f.ctypeFromType(inst_scalar_ty, .complete); |
| 4272 | 4293 | |
| 4273 | 4294 | const local = try f.allocLocal(inst, inst_ty); |
| ... | ... | @@ -4299,13 +4320,14 @@ fn airPtrAddSub(f: *Function, inst: Air.Inst.Index, operator: u8) !CValue { |
| 4299 | 4320 | } |
| 4300 | 4321 | |
| 4301 | 4322 | fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []const u8) !CValue { |
| 4302 | const zcu = f.object.dg.zcu; | |
| 4323 | const pt = f.object.dg.pt; | |
| 4324 | const zcu = pt.zcu; | |
| 4303 | 4325 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4304 | 4326 | |
| 4305 | 4327 | const inst_ty = f.typeOfIndex(inst); |
| 4306 | 4328 | const inst_scalar_ty = inst_ty.scalarType(zcu); |
| 4307 | 4329 | |
| 4308 | if ((inst_scalar_ty.isInt(zcu) and inst_scalar_ty.bitSize(zcu) > 64) or inst_scalar_ty.isRuntimeFloat()) | |
| 4330 | if ((inst_scalar_ty.isInt(zcu) and inst_scalar_ty.bitSize(pt) > 64) or inst_scalar_ty.isRuntimeFloat()) | |
| 4309 | 4331 | return try airBinBuiltinCall(f, inst, operation, .none); |
| 4310 | 4332 | |
| 4311 | 4333 | const lhs = try f.resolveInst(bin_op.lhs); |
| ... | ... | @@ -4339,7 +4361,8 @@ fn airMinMax(f: *Function, inst: Air.Inst.Index, operator: u8, operation: []cons |
| 4339 | 4361 | } |
| 4340 | 4362 | |
| 4341 | 4363 | fn airSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4342 | const zcu = f.object.dg.zcu; | |
| 4364 | const pt = f.object.dg.pt; | |
| 4365 | const zcu = pt.zcu; | |
| 4343 | 4366 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4344 | 4367 | const bin_op = f.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4345 | 4368 | |
| ... | ... | @@ -4374,7 +4397,8 @@ fn airCall( |
| 4374 | 4397 | inst: Air.Inst.Index, |
| 4375 | 4398 | modifier: std.builtin.CallModifier, |
| 4376 | 4399 | ) !CValue { |
| 4377 | const zcu = f.object.dg.zcu; | |
| 4400 | const pt = f.object.dg.pt; | |
| 4401 | const zcu = pt.zcu; | |
| 4378 | 4402 | // Not even allowed to call panic in a naked function. |
| 4379 | 4403 | if (f.object.dg.is_naked_fn) return .none; |
| 4380 | 4404 | |
| ... | ... | @@ -4398,7 +4422,7 @@ fn airCall( |
| 4398 | 4422 | if (!arg_ctype.eql(try f.ctypeFromType(arg_ty, .complete))) { |
| 4399 | 4423 | const array_local = try f.allocAlignedLocal(inst, .{ |
| 4400 | 4424 | .ctype = arg_ctype, |
| 4401 | .alignas = CType.AlignAs.fromAbiAlignment(arg_ty.abiAlignment(zcu)), | |
| 4425 | .alignas = CType.AlignAs.fromAbiAlignment(arg_ty.abiAlignment(pt)), | |
| 4402 | 4426 | }); |
| 4403 | 4427 | try writer.writeAll("memcpy("); |
| 4404 | 4428 | try f.writeCValueMember(writer, array_local, .{ .identifier = "array" }); |
| ... | ... | @@ -4445,7 +4469,7 @@ fn airCall( |
| 4445 | 4469 | } else { |
| 4446 | 4470 | const local = try f.allocAlignedLocal(inst, .{ |
| 4447 | 4471 | .ctype = ret_ctype, |
| 4448 | .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(zcu)), | |
| 4472 | .alignas = CType.AlignAs.fromAbiAlignment(ret_ty.abiAlignment(pt)), | |
| 4449 | 4473 | }); |
| 4450 | 4474 | try f.writeCValue(writer, local, .Other); |
| 4451 | 4475 | try writer.writeAll(" = "); |
| ... | ... | @@ -4456,7 +4480,7 @@ fn airCall( |
| 4456 | 4480 | callee: { |
| 4457 | 4481 | known: { |
| 4458 | 4482 | const fn_decl = fn_decl: { |
| 4459 | const callee_val = (try f.air.value(pl_op.operand, zcu)) orelse break :known; | |
| 4483 | const callee_val = (try f.air.value(pl_op.operand, pt)) orelse break :known; | |
| 4460 | 4484 | break :fn_decl switch (zcu.intern_pool.indexToKey(callee_val.toIntern())) { |
| 4461 | 4485 | .extern_func => |extern_func| extern_func.decl, |
| 4462 | 4486 | .func => |func| func.owner_decl, |
| ... | ... | @@ -4499,7 +4523,7 @@ fn airCall( |
| 4499 | 4523 | try writer.writeAll(");\n"); |
| 4500 | 4524 | |
| 4501 | 4525 | const result = result: { |
| 4502 | if (result_local == .none or !lowersToArray(ret_ty, zcu)) | |
| 4526 | if (result_local == .none or !lowersToArray(ret_ty, pt)) | |
| 4503 | 4527 | break :result result_local; |
| 4504 | 4528 | |
| 4505 | 4529 | const array_local = try f.allocLocal(inst, ret_ty); |
| ... | ... | @@ -4533,7 +4557,8 @@ fn airDbgStmt(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4533 | 4557 | } |
| 4534 | 4558 | |
| 4535 | 4559 | fn airDbgInlineBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4536 | const zcu = f.object.dg.zcu; | |
| 4560 | const pt = f.object.dg.pt; | |
| 4561 | const zcu = pt.zcu; | |
| 4537 | 4562 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4538 | 4563 | const extra = f.air.extraData(Air.DbgInlineBlock, ty_pl.payload); |
| 4539 | 4564 | const owner_decl = zcu.funcOwnerDeclPtr(extra.data.func); |
| ... | ... | @@ -4545,10 +4570,11 @@ fn airDbgInlineBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4545 | 4570 | } |
| 4546 | 4571 | |
| 4547 | 4572 | fn airDbgVar(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4548 | const zcu = f.object.dg.zcu; | |
| 4573 | const pt = f.object.dg.pt; | |
| 4574 | const zcu = pt.zcu; | |
| 4549 | 4575 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4550 | 4576 | const name = f.air.nullTerminatedString(pl_op.payload); |
| 4551 | const operand_is_undef = if (try f.air.value(pl_op.operand, zcu)) |v| v.isUndefDeep(zcu) else false; | |
| 4577 | const operand_is_undef = if (try f.air.value(pl_op.operand, pt)) |v| v.isUndefDeep(zcu) else false; | |
| 4552 | 4578 | if (!operand_is_undef) _ = try f.resolveInst(pl_op.operand); |
| 4553 | 4579 | |
| 4554 | 4580 | try reap(f, inst, &.{pl_op.operand}); |
| ... | ... | @@ -4564,7 +4590,8 @@ fn airBlock(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4564 | 4590 | } |
| 4565 | 4591 | |
| 4566 | 4592 | fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) !CValue { |
| 4567 | const zcu = f.object.dg.zcu; | |
| 4593 | const pt = f.object.dg.pt; | |
| 4594 | const zcu = pt.zcu; | |
| 4568 | 4595 | const liveness_block = f.liveness.getBlock(inst); |
| 4569 | 4596 | |
| 4570 | 4597 | const block_id: usize = f.next_block_index; |
| ... | ... | @@ -4572,7 +4599,7 @@ fn lowerBlock(f: *Function, inst: Air.Inst.Index, body: []const Air.Inst.Index) |
| 4572 | 4599 | const writer = f.object.writer(); |
| 4573 | 4600 | |
| 4574 | 4601 | const inst_ty = f.typeOfIndex(inst); |
| 4575 | const result = if (inst_ty.hasRuntimeBitsIgnoreComptime(zcu) and !f.liveness.isUnused(inst)) | |
| 4602 | const result = if (inst_ty.hasRuntimeBitsIgnoreComptime(pt) and !f.liveness.isUnused(inst)) | |
| 4576 | 4603 | try f.allocLocal(inst, inst_ty) |
| 4577 | 4604 | else |
| 4578 | 4605 | .none; |
| ... | ... | @@ -4611,7 +4638,8 @@ fn airTry(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4611 | 4638 | } |
| 4612 | 4639 | |
| 4613 | 4640 | fn airTryPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4614 | const zcu = f.object.dg.zcu; | |
| 4641 | const pt = f.object.dg.pt; | |
| 4642 | const zcu = pt.zcu; | |
| 4615 | 4643 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4616 | 4644 | const extra = f.air.extraData(Air.TryPtr, ty_pl.payload); |
| 4617 | 4645 | const body: []const Air.Inst.Index = @ptrCast(f.air.extra[extra.end..][0..extra.data.body_len]); |
| ... | ... | @@ -4627,13 +4655,14 @@ fn lowerTry( |
| 4627 | 4655 | err_union_ty: Type, |
| 4628 | 4656 | is_ptr: bool, |
| 4629 | 4657 | ) !CValue { |
| 4630 | const zcu = f.object.dg.zcu; | |
| 4658 | const pt = f.object.dg.pt; | |
| 4659 | const zcu = pt.zcu; | |
| 4631 | 4660 | const err_union = try f.resolveInst(operand); |
| 4632 | 4661 | const inst_ty = f.typeOfIndex(inst); |
| 4633 | 4662 | const liveness_condbr = f.liveness.getCondBr(inst); |
| 4634 | 4663 | const writer = f.object.writer(); |
| 4635 | 4664 | const payload_ty = err_union_ty.errorUnionPayload(zcu); |
| 4636 | const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(zcu); | |
| 4665 | const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(pt); | |
| 4637 | 4666 | |
| 4638 | 4667 | if (!err_union_ty.errorUnionSet(zcu).errorSetIsEmpty(zcu)) { |
| 4639 | 4668 | try writer.writeAll("if ("); |
| ... | ... | @@ -4725,7 +4754,8 @@ fn airBitcast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4725 | 4754 | } |
| 4726 | 4755 | |
| 4727 | 4756 | fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CValue { |
| 4728 | const zcu = f.object.dg.zcu; | |
| 4757 | const pt = f.object.dg.pt; | |
| 4758 | const zcu = pt.zcu; | |
| 4729 | 4759 | const target = &f.object.dg.mod.resolved_target.result; |
| 4730 | 4760 | const ctype_pool = &f.object.dg.ctype_pool; |
| 4731 | 4761 | const writer = f.object.writer(); |
| ... | ... | @@ -4771,7 +4801,7 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal |
| 4771 | 4801 | try writer.writeAll(", sizeof("); |
| 4772 | 4802 | try f.renderType( |
| 4773 | 4803 | writer, |
| 4774 | if (dest_ty.abiSize(zcu) <= operand_ty.abiSize(zcu)) dest_ty else operand_ty, | |
| 4804 | if (dest_ty.abiSize(pt) <= operand_ty.abiSize(pt)) dest_ty else operand_ty, | |
| 4775 | 4805 | ); |
| 4776 | 4806 | try writer.writeAll("));\n"); |
| 4777 | 4807 | |
| ... | ... | @@ -4805,7 +4835,7 @@ fn bitcast(f: *Function, dest_ty: Type, operand: CValue, operand_ty: Type) !CVal |
| 4805 | 4835 | try writer.writeByte('('); |
| 4806 | 4836 | } |
| 4807 | 4837 | try writer.writeAll("zig_wrap_"); |
| 4808 | const info_ty = try zcu.intType(dest_info.signedness, bits); | |
| 4838 | const info_ty = try pt.intType(dest_info.signedness, bits); | |
| 4809 | 4839 | if (wrap_ctype) |ctype| |
| 4810 | 4840 | try f.object.dg.renderCTypeForBuiltinFnName(writer, ctype) |
| 4811 | 4841 | else |
| ... | ... | @@ -4935,7 +4965,8 @@ fn airCondBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4935 | 4965 | } |
| 4936 | 4966 | |
| 4937 | 4967 | fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4938 | const zcu = f.object.dg.zcu; | |
| 4968 | const pt = f.object.dg.pt; | |
| 4969 | const zcu = pt.zcu; | |
| 4939 | 4970 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 4940 | 4971 | const condition = try f.resolveInst(pl_op.operand); |
| 4941 | 4972 | try reap(f, inst, &.{pl_op.operand}); |
| ... | ... | @@ -4979,16 +5010,16 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 4979 | 5010 | for (items) |item| { |
| 4980 | 5011 | try f.object.indent_writer.insertNewline(); |
| 4981 | 5012 | try writer.writeAll("case "); |
| 4982 | const item_value = try f.air.value(item, zcu); | |
| 4983 | if (item_value.?.getUnsignedInt(zcu)) |item_int| try writer.print("{}\n", .{ | |
| 4984 | try f.fmtIntLiteral(try zcu.intValue(lowered_condition_ty, item_int)), | |
| 5013 | const item_value = try f.air.value(item, pt); | |
| 5014 | if (item_value.?.getUnsignedInt(pt)) |item_int| try writer.print("{}\n", .{ | |
| 5015 | try f.fmtIntLiteral(try pt.intValue(lowered_condition_ty, item_int)), | |
| 4985 | 5016 | }) else { |
| 4986 | 5017 | if (condition_ty.isPtrAtRuntime(zcu)) { |
| 4987 | 5018 | try writer.writeByte('('); |
| 4988 | 5019 | try f.renderType(writer, Type.usize); |
| 4989 | 5020 | try writer.writeByte(')'); |
| 4990 | 5021 | } |
| 4991 | try f.object.dg.renderValue(writer, (try f.air.value(item, zcu)).?, .Other); | |
| 5022 | try f.object.dg.renderValue(writer, (try f.air.value(item, pt)).?, .Other); | |
| 4992 | 5023 | } |
| 4993 | 5024 | try writer.writeByte(':'); |
| 4994 | 5025 | } |
| ... | ... | @@ -5026,13 +5057,14 @@ fn airSwitchBr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5026 | 5057 | } |
| 5027 | 5058 | |
| 5028 | 5059 | fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool { |
| 5029 | const target = &f.object.dg.mod.resolved_target.result; | |
| 5060 | const dg = f.object.dg; | |
| 5061 | const target = &dg.mod.resolved_target.result; | |
| 5030 | 5062 | return switch (constraint[0]) { |
| 5031 | 5063 | '{' => true, |
| 5032 | 5064 | 'i', 'r' => false, |
| 5033 | 5065 | 'I' => !target.cpu.arch.isArmOrThumb(), |
| 5034 | 5066 | else => switch (value) { |
| 5035 | .constant => |val| switch (f.object.dg.zcu.intern_pool.indexToKey(val.toIntern())) { | |
| 5067 | .constant => |val| switch (dg.pt.zcu.intern_pool.indexToKey(val.toIntern())) { | |
| 5036 | 5068 | .ptr => |ptr| if (ptr.byte_offset == 0) switch (ptr.base_addr) { |
| 5037 | 5069 | .decl => false, |
| 5038 | 5070 | else => true, |
| ... | ... | @@ -5045,7 +5077,8 @@ fn asmInputNeedsLocal(f: *Function, constraint: []const u8, value: CValue) bool |
| 5045 | 5077 | } |
| 5046 | 5078 | |
| 5047 | 5079 | fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5048 | const zcu = f.object.dg.zcu; | |
| 5080 | const pt = f.object.dg.pt; | |
| 5081 | const zcu = pt.zcu; | |
| 5049 | 5082 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5050 | 5083 | const extra = f.air.extraData(Air.Asm, ty_pl.payload); |
| 5051 | 5084 | const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0; |
| ... | ... | @@ -5060,10 +5093,10 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5060 | 5093 | const result = result: { |
| 5061 | 5094 | const writer = f.object.writer(); |
| 5062 | 5095 | const inst_ty = f.typeOfIndex(inst); |
| 5063 | const inst_local = if (inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) local: { | |
| 5096 | const inst_local = if (inst_ty.hasRuntimeBitsIgnoreComptime(pt)) local: { | |
| 5064 | 5097 | const inst_local = try f.allocLocalValue(.{ |
| 5065 | 5098 | .ctype = try f.ctypeFromType(inst_ty, .complete), |
| 5066 | .alignas = CType.AlignAs.fromAbiAlignment(inst_ty.abiAlignment(zcu)), | |
| 5099 | .alignas = CType.AlignAs.fromAbiAlignment(inst_ty.abiAlignment(pt)), | |
| 5067 | 5100 | }); |
| 5068 | 5101 | if (f.wantSafety()) { |
| 5069 | 5102 | try f.writeCValue(writer, inst_local, .Other); |
| ... | ... | @@ -5096,7 +5129,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5096 | 5129 | try writer.writeAll("register "); |
| 5097 | 5130 | const output_local = try f.allocLocalValue(.{ |
| 5098 | 5131 | .ctype = try f.ctypeFromType(output_ty, .complete), |
| 5099 | .alignas = CType.AlignAs.fromAbiAlignment(output_ty.abiAlignment(zcu)), | |
| 5132 | .alignas = CType.AlignAs.fromAbiAlignment(output_ty.abiAlignment(pt)), | |
| 5100 | 5133 | }); |
| 5101 | 5134 | try f.allocs.put(gpa, output_local.new_local, false); |
| 5102 | 5135 | try f.object.dg.renderTypeAndName(writer, output_ty, output_local, .{}, .none, .complete); |
| ... | ... | @@ -5131,7 +5164,7 @@ fn airAsm(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5131 | 5164 | if (is_reg) try writer.writeAll("register "); |
| 5132 | 5165 | const input_local = try f.allocLocalValue(.{ |
| 5133 | 5166 | .ctype = try f.ctypeFromType(input_ty, .complete), |
| 5134 | .alignas = CType.AlignAs.fromAbiAlignment(input_ty.abiAlignment(zcu)), | |
| 5167 | .alignas = CType.AlignAs.fromAbiAlignment(input_ty.abiAlignment(pt)), | |
| 5135 | 5168 | }); |
| 5136 | 5169 | try f.allocs.put(gpa, input_local.new_local, false); |
| 5137 | 5170 | try f.object.dg.renderTypeAndName(writer, input_ty, input_local, Const, .none, .complete); |
| ... | ... | @@ -5314,7 +5347,8 @@ fn airIsNull( |
| 5314 | 5347 | operator: std.math.CompareOperator, |
| 5315 | 5348 | is_ptr: bool, |
| 5316 | 5349 | ) !CValue { |
| 5317 | const zcu = f.object.dg.zcu; | |
| 5350 | const pt = f.object.dg.pt; | |
| 5351 | const zcu = pt.zcu; | |
| 5318 | 5352 | const ctype_pool = &f.object.dg.ctype_pool; |
| 5319 | 5353 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5320 | 5354 | |
| ... | ... | @@ -5369,7 +5403,8 @@ fn airIsNull( |
| 5369 | 5403 | } |
| 5370 | 5404 | |
| 5371 | 5405 | fn airOptionalPayload(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { |
| 5372 | const zcu = f.object.dg.zcu; | |
| 5406 | const pt = f.object.dg.pt; | |
| 5407 | const zcu = pt.zcu; | |
| 5373 | 5408 | const ctype_pool = &f.object.dg.ctype_pool; |
| 5374 | 5409 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5375 | 5410 | |
| ... | ... | @@ -5404,7 +5439,8 @@ fn airOptionalPayload(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue |
| 5404 | 5439 | } |
| 5405 | 5440 | |
| 5406 | 5441 | fn airOptionalPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5407 | const zcu = f.object.dg.zcu; | |
| 5442 | const pt = f.object.dg.pt; | |
| 5443 | const zcu = pt.zcu; | |
| 5408 | 5444 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5409 | 5445 | const writer = f.object.writer(); |
| 5410 | 5446 | const operand = try f.resolveInst(ty_op.operand); |
| ... | ... | @@ -5458,21 +5494,22 @@ fn fieldLocation( |
| 5458 | 5494 | container_ptr_ty: Type, |
| 5459 | 5495 | field_ptr_ty: Type, |
| 5460 | 5496 | field_index: u32, |
| 5461 | zcu: *Zcu, | |
| 5497 | pt: Zcu.PerThread, | |
| 5462 | 5498 | ) union(enum) { |
| 5463 | 5499 | begin: void, |
| 5464 | 5500 | field: CValue, |
| 5465 | 5501 | byte_offset: u64, |
| 5466 | 5502 | } { |
| 5503 | const zcu = pt.zcu; | |
| 5467 | 5504 | const ip = &zcu.intern_pool; |
| 5468 | 5505 | const container_ty = Type.fromInterned(ip.indexToKey(container_ptr_ty.toIntern()).ptr_type.child); |
| 5469 | 5506 | switch (ip.indexToKey(container_ty.toIntern())) { |
| 5470 | 5507 | .struct_type => { |
| 5471 | 5508 | const loaded_struct = ip.loadStructType(container_ty.toIntern()); |
| 5472 | 5509 | return switch (loaded_struct.layout) { |
| 5473 | .auto, .@"extern" => if (!container_ty.hasRuntimeBitsIgnoreComptime(zcu)) | |
| 5510 | .auto, .@"extern" => if (!container_ty.hasRuntimeBitsIgnoreComptime(pt)) | |
| 5474 | 5511 | .begin |
| 5475 | else if (!field_ptr_ty.childType(zcu).hasRuntimeBitsIgnoreComptime(zcu)) | |
| 5512 | else if (!field_ptr_ty.childType(zcu).hasRuntimeBitsIgnoreComptime(pt)) | |
| 5476 | 5513 | .{ .byte_offset = loaded_struct.offsets.get(ip)[field_index] } |
| 5477 | 5514 | else |
| 5478 | 5515 | .{ .field = if (loaded_struct.fieldName(ip, field_index).unwrap()) |field_name| |
| ... | ... | @@ -5480,16 +5517,16 @@ fn fieldLocation( |
| 5480 | 5517 | else |
| 5481 | 5518 | .{ .field = field_index } }, |
| 5482 | 5519 | .@"packed" => if (field_ptr_ty.ptrInfo(zcu).packed_offset.host_size == 0) |
| 5483 | .{ .byte_offset = @divExact(zcu.structPackedFieldBitOffset(loaded_struct, field_index) + | |
| 5520 | .{ .byte_offset = @divExact(pt.structPackedFieldBitOffset(loaded_struct, field_index) + | |
| 5484 | 5521 | container_ptr_ty.ptrInfo(zcu).packed_offset.bit_offset, 8) } |
| 5485 | 5522 | else |
| 5486 | 5523 | .begin, |
| 5487 | 5524 | }; |
| 5488 | 5525 | }, |
| 5489 | .anon_struct_type => |anon_struct_info| return if (!container_ty.hasRuntimeBitsIgnoreComptime(zcu)) | |
| 5526 | .anon_struct_type => |anon_struct_info| return if (!container_ty.hasRuntimeBitsIgnoreComptime(pt)) | |
| 5490 | 5527 | .begin |
| 5491 | else if (!field_ptr_ty.childType(zcu).hasRuntimeBitsIgnoreComptime(zcu)) | |
| 5492 | .{ .byte_offset = container_ty.structFieldOffset(field_index, zcu) } | |
| 5528 | else if (!field_ptr_ty.childType(zcu).hasRuntimeBitsIgnoreComptime(pt)) | |
| 5529 | .{ .byte_offset = container_ty.structFieldOffset(field_index, pt) } | |
| 5493 | 5530 | else |
| 5494 | 5531 | .{ .field = if (anon_struct_info.fieldName(ip, field_index).unwrap()) |field_name| |
| 5495 | 5532 | .{ .identifier = field_name.toSlice(ip) } |
| ... | ... | @@ -5500,8 +5537,8 @@ fn fieldLocation( |
| 5500 | 5537 | switch (loaded_union.getLayout(ip)) { |
| 5501 | 5538 | .auto, .@"extern" => { |
| 5502 | 5539 | const field_ty = Type.fromInterned(loaded_union.field_types.get(ip)[field_index]); |
| 5503 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) | |
| 5504 | return if (loaded_union.hasTag(ip) and !container_ty.unionHasAllZeroBitFieldTypes(zcu)) | |
| 5540 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) | |
| 5541 | return if (loaded_union.hasTag(ip) and !container_ty.unionHasAllZeroBitFieldTypes(pt)) | |
| 5505 | 5542 | .{ .field = .{ .identifier = "payload" } } |
| 5506 | 5543 | else |
| 5507 | 5544 | .begin; |
| ... | ... | @@ -5546,7 +5583,8 @@ fn airStructFieldPtrIndex(f: *Function, inst: Air.Inst.Index, index: u8) !CValue |
| 5546 | 5583 | } |
| 5547 | 5584 | |
| 5548 | 5585 | fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5549 | const zcu = f.object.dg.zcu; | |
| 5586 | const pt = f.object.dg.pt; | |
| 5587 | const zcu = pt.zcu; | |
| 5550 | 5588 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5551 | 5589 | const extra = f.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| 5552 | 5590 | |
| ... | ... | @@ -5564,10 +5602,10 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5564 | 5602 | try f.renderType(writer, container_ptr_ty); |
| 5565 | 5603 | try writer.writeByte(')'); |
| 5566 | 5604 | |
| 5567 | switch (fieldLocation(container_ptr_ty, field_ptr_ty, extra.field_index, zcu)) { | |
| 5605 | switch (fieldLocation(container_ptr_ty, field_ptr_ty, extra.field_index, pt)) { | |
| 5568 | 5606 | .begin => try f.writeCValue(writer, field_ptr_val, .Initializer), |
| 5569 | 5607 | .field => |field| { |
| 5570 | const u8_ptr_ty = try zcu.adjustPtrTypeChild(field_ptr_ty, Type.u8); | |
| 5608 | const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, Type.u8); | |
| 5571 | 5609 | |
| 5572 | 5610 | try writer.writeAll("(("); |
| 5573 | 5611 | try f.renderType(writer, u8_ptr_ty); |
| ... | ... | @@ -5580,14 +5618,14 @@ fn airFieldParentPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5580 | 5618 | try writer.writeAll("))"); |
| 5581 | 5619 | }, |
| 5582 | 5620 | .byte_offset => |byte_offset| { |
| 5583 | const u8_ptr_ty = try zcu.adjustPtrTypeChild(field_ptr_ty, Type.u8); | |
| 5621 | const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, Type.u8); | |
| 5584 | 5622 | |
| 5585 | 5623 | try writer.writeAll("(("); |
| 5586 | 5624 | try f.renderType(writer, u8_ptr_ty); |
| 5587 | 5625 | try writer.writeByte(')'); |
| 5588 | 5626 | try f.writeCValue(writer, field_ptr_val, .Other); |
| 5589 | 5627 | try writer.print(" - {})", .{ |
| 5590 | try f.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset)), | |
| 5628 | try f.fmtIntLiteral(try pt.intValue(Type.usize, byte_offset)), | |
| 5591 | 5629 | }); |
| 5592 | 5630 | }, |
| 5593 | 5631 | } |
| ... | ... | @@ -5603,7 +5641,8 @@ fn fieldPtr( |
| 5603 | 5641 | container_ptr_val: CValue, |
| 5604 | 5642 | field_index: u32, |
| 5605 | 5643 | ) !CValue { |
| 5606 | const zcu = f.object.dg.zcu; | |
| 5644 | const pt = f.object.dg.pt; | |
| 5645 | const zcu = pt.zcu; | |
| 5607 | 5646 | const container_ty = container_ptr_ty.childType(zcu); |
| 5608 | 5647 | const field_ptr_ty = f.typeOfIndex(inst); |
| 5609 | 5648 | |
| ... | ... | @@ -5617,21 +5656,21 @@ fn fieldPtr( |
| 5617 | 5656 | try f.renderType(writer, field_ptr_ty); |
| 5618 | 5657 | try writer.writeByte(')'); |
| 5619 | 5658 | |
| 5620 | switch (fieldLocation(container_ptr_ty, field_ptr_ty, field_index, zcu)) { | |
| 5659 | switch (fieldLocation(container_ptr_ty, field_ptr_ty, field_index, pt)) { | |
| 5621 | 5660 | .begin => try f.writeCValue(writer, container_ptr_val, .Initializer), |
| 5622 | 5661 | .field => |field| { |
| 5623 | 5662 | try writer.writeByte('&'); |
| 5624 | 5663 | try f.writeCValueDerefMember(writer, container_ptr_val, field); |
| 5625 | 5664 | }, |
| 5626 | 5665 | .byte_offset => |byte_offset| { |
| 5627 | const u8_ptr_ty = try zcu.adjustPtrTypeChild(field_ptr_ty, Type.u8); | |
| 5666 | const u8_ptr_ty = try pt.adjustPtrTypeChild(field_ptr_ty, Type.u8); | |
| 5628 | 5667 | |
| 5629 | 5668 | try writer.writeAll("(("); |
| 5630 | 5669 | try f.renderType(writer, u8_ptr_ty); |
| 5631 | 5670 | try writer.writeByte(')'); |
| 5632 | 5671 | try f.writeCValue(writer, container_ptr_val, .Other); |
| 5633 | 5672 | try writer.print(" + {})", .{ |
| 5634 | try f.fmtIntLiteral(try zcu.intValue(Type.usize, byte_offset)), | |
| 5673 | try f.fmtIntLiteral(try pt.intValue(Type.usize, byte_offset)), | |
| 5635 | 5674 | }); |
| 5636 | 5675 | }, |
| 5637 | 5676 | } |
| ... | ... | @@ -5641,13 +5680,14 @@ fn fieldPtr( |
| 5641 | 5680 | } |
| 5642 | 5681 | |
| 5643 | 5682 | fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5644 | const zcu = f.object.dg.zcu; | |
| 5683 | const pt = f.object.dg.pt; | |
| 5684 | const zcu = pt.zcu; | |
| 5645 | 5685 | const ip = &zcu.intern_pool; |
| 5646 | 5686 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5647 | 5687 | const extra = f.air.extraData(Air.StructField, ty_pl.payload).data; |
| 5648 | 5688 | |
| 5649 | 5689 | const inst_ty = f.typeOfIndex(inst); |
| 5650 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 5690 | if (!inst_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 5651 | 5691 | try reap(f, inst, &.{extra.struct_operand}); |
| 5652 | 5692 | return .none; |
| 5653 | 5693 | } |
| ... | ... | @@ -5671,15 +5711,15 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5671 | 5711 | .@"packed" => { |
| 5672 | 5712 | const int_info = struct_ty.intInfo(zcu); |
| 5673 | 5713 | |
| 5674 | const bit_offset_ty = try zcu.intType(.unsigned, Type.smallestUnsignedBits(int_info.bits - 1)); | |
| 5714 | const bit_offset_ty = try pt.intType(.unsigned, Type.smallestUnsignedBits(int_info.bits - 1)); | |
| 5675 | 5715 | |
| 5676 | const bit_offset = zcu.structPackedFieldBitOffset(loaded_struct, extra.field_index); | |
| 5716 | const bit_offset = pt.structPackedFieldBitOffset(loaded_struct, extra.field_index); | |
| 5677 | 5717 | |
| 5678 | 5718 | const field_int_signedness = if (inst_ty.isAbiInt(zcu)) |
| 5679 | 5719 | inst_ty.intInfo(zcu).signedness |
| 5680 | 5720 | else |
| 5681 | 5721 | .unsigned; |
| 5682 | const field_int_ty = try zcu.intType(field_int_signedness, @as(u16, @intCast(inst_ty.bitSize(zcu)))); | |
| 5722 | const field_int_ty = try pt.intType(field_int_signedness, @as(u16, @intCast(inst_ty.bitSize(pt)))); | |
| 5683 | 5723 | |
| 5684 | 5724 | const temp_local = try f.allocLocal(inst, field_int_ty); |
| 5685 | 5725 | try f.writeCValue(writer, temp_local, .Other); |
| ... | ... | @@ -5690,7 +5730,7 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5690 | 5730 | try writer.writeByte(')'); |
| 5691 | 5731 | const cant_cast = int_info.bits > 64; |
| 5692 | 5732 | if (cant_cast) { |
| 5693 | if (field_int_ty.bitSize(zcu) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{}); | |
| 5733 | if (field_int_ty.bitSize(pt) > 64) return f.fail("TODO: C backend: implement casting between types > 64 bits", .{}); | |
| 5694 | 5734 | try writer.writeAll("zig_lo_"); |
| 5695 | 5735 | try f.object.dg.renderTypeForBuiltinFnName(writer, struct_ty); |
| 5696 | 5736 | try writer.writeByte('('); |
| ... | ... | @@ -5702,12 +5742,12 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5702 | 5742 | } |
| 5703 | 5743 | try f.writeCValue(writer, struct_byval, .Other); |
| 5704 | 5744 | if (bit_offset > 0) try writer.print(", {})", .{ |
| 5705 | try f.fmtIntLiteral(try zcu.intValue(bit_offset_ty, bit_offset)), | |
| 5745 | try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)), | |
| 5706 | 5746 | }); |
| 5707 | 5747 | if (cant_cast) try writer.writeByte(')'); |
| 5708 | 5748 | try f.object.dg.renderBuiltinInfo(writer, field_int_ty, .bits); |
| 5709 | 5749 | try writer.writeAll(");\n"); |
| 5710 | if (inst_ty.eql(field_int_ty, f.object.dg.zcu)) return temp_local; | |
| 5750 | if (inst_ty.eql(field_int_ty, zcu)) return temp_local; | |
| 5711 | 5751 | |
| 5712 | 5752 | const local = try f.allocLocal(inst, inst_ty); |
| 5713 | 5753 | if (local.new_local != temp_local.new_local) { |
| ... | ... | @@ -5783,7 +5823,8 @@ fn airStructFieldVal(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5783 | 5823 | /// *(E!T) -> E |
| 5784 | 5824 | /// Note that the result is never a pointer. |
| 5785 | 5825 | fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5786 | const zcu = f.object.dg.zcu; | |
| 5826 | const pt = f.object.dg.pt; | |
| 5827 | const zcu = pt.zcu; | |
| 5787 | 5828 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5788 | 5829 | |
| 5789 | 5830 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -5797,7 +5838,7 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5797 | 5838 | const payload_ty = error_union_ty.errorUnionPayload(zcu); |
| 5798 | 5839 | const local = try f.allocLocal(inst, inst_ty); |
| 5799 | 5840 | |
| 5800 | if (!payload_ty.hasRuntimeBits(zcu) and operand == .local and operand.local == local.new_local) { | |
| 5841 | if (!payload_ty.hasRuntimeBits(pt) and operand == .local and operand.local == local.new_local) { | |
| 5801 | 5842 | // The store will be 'x = x'; elide it. |
| 5802 | 5843 | return local; |
| 5803 | 5844 | } |
| ... | ... | @@ -5806,11 +5847,11 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5806 | 5847 | try f.writeCValue(writer, local, .Other); |
| 5807 | 5848 | try writer.writeAll(" = "); |
| 5808 | 5849 | |
| 5809 | if (!payload_ty.hasRuntimeBits(zcu)) | |
| 5850 | if (!payload_ty.hasRuntimeBits(pt)) | |
| 5810 | 5851 | try f.writeCValue(writer, operand, .Other) |
| 5811 | 5852 | else if (error_ty.errorSetIsEmpty(zcu)) |
| 5812 | 5853 | try writer.print("{}", .{ |
| 5813 | try f.fmtIntLiteral(try zcu.intValue(try zcu.errorIntType(), 0)), | |
| 5854 | try f.fmtIntLiteral(try pt.intValue(try pt.errorIntType(), 0)), | |
| 5814 | 5855 | }) |
| 5815 | 5856 | else if (operand_is_ptr) |
| 5816 | 5857 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" }) |
| ... | ... | @@ -5821,7 +5862,8 @@ fn airUnwrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5821 | 5862 | } |
| 5822 | 5863 | |
| 5823 | 5864 | fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValue { |
| 5824 | const zcu = f.object.dg.zcu; | |
| 5865 | const pt = f.object.dg.pt; | |
| 5866 | const zcu = pt.zcu; | |
| 5825 | 5867 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5826 | 5868 | |
| 5827 | 5869 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -5831,7 +5873,7 @@ fn airUnwrapErrUnionPay(f: *Function, inst: Air.Inst.Index, is_ptr: bool) !CValu |
| 5831 | 5873 | const error_union_ty = if (is_ptr) operand_ty.childType(zcu) else operand_ty; |
| 5832 | 5874 | |
| 5833 | 5875 | const writer = f.object.writer(); |
| 5834 | if (!error_union_ty.errorUnionPayload(zcu).hasRuntimeBits(zcu)) { | |
| 5876 | if (!error_union_ty.errorUnionPayload(zcu).hasRuntimeBits(pt)) { | |
| 5835 | 5877 | if (!is_ptr) return .none; |
| 5836 | 5878 | |
| 5837 | 5879 | const local = try f.allocLocal(inst, inst_ty); |
| ... | ... | @@ -5896,12 +5938,13 @@ fn airWrapOptional(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5896 | 5938 | } |
| 5897 | 5939 | |
| 5898 | 5940 | fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5899 | const zcu = f.object.dg.zcu; | |
| 5941 | const pt = f.object.dg.pt; | |
| 5942 | const zcu = pt.zcu; | |
| 5900 | 5943 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5901 | 5944 | |
| 5902 | 5945 | const inst_ty = f.typeOfIndex(inst); |
| 5903 | 5946 | const payload_ty = inst_ty.errorUnionPayload(zcu); |
| 5904 | const repr_is_err = !payload_ty.hasRuntimeBitsIgnoreComptime(zcu); | |
| 5947 | const repr_is_err = !payload_ty.hasRuntimeBitsIgnoreComptime(pt); | |
| 5905 | 5948 | const err_ty = inst_ty.errorUnionSet(zcu); |
| 5906 | 5949 | const err = try f.resolveInst(ty_op.operand); |
| 5907 | 5950 | try reap(f, inst, &.{ty_op.operand}); |
| ... | ... | @@ -5935,7 +5978,8 @@ fn airWrapErrUnionErr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5935 | 5978 | } |
| 5936 | 5979 | |
| 5937 | 5980 | fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5938 | const zcu = f.object.dg.zcu; | |
| 5981 | const pt = f.object.dg.pt; | |
| 5982 | const zcu = pt.zcu; | |
| 5939 | 5983 | const writer = f.object.writer(); |
| 5940 | 5984 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5941 | 5985 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -5944,12 +5988,12 @@ fn airErrUnionPayloadPtrSet(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5944 | 5988 | const error_union_ty = operand_ty.childType(zcu); |
| 5945 | 5989 | |
| 5946 | 5990 | const payload_ty = error_union_ty.errorUnionPayload(zcu); |
| 5947 | const err_int_ty = try zcu.errorIntType(); | |
| 5948 | const no_err = try zcu.intValue(err_int_ty, 0); | |
| 5991 | const err_int_ty = try pt.errorIntType(); | |
| 5992 | const no_err = try pt.intValue(err_int_ty, 0); | |
| 5949 | 5993 | try reap(f, inst, &.{ty_op.operand}); |
| 5950 | 5994 | |
| 5951 | 5995 | // First, set the non-error value. |
| 5952 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 5996 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 5953 | 5997 | const a = try Assignment.start(f, writer, try f.ctypeFromType(operand_ty, .complete)); |
| 5954 | 5998 | try f.writeCValueDeref(writer, operand); |
| 5955 | 5999 | try a.assign(f, writer); |
| ... | ... | @@ -5994,13 +6038,14 @@ fn airSaveErrReturnTraceIndex(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5994 | 6038 | } |
| 5995 | 6039 | |
| 5996 | 6040 | fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 5997 | const zcu = f.object.dg.zcu; | |
| 6041 | const pt = f.object.dg.pt; | |
| 6042 | const zcu = pt.zcu; | |
| 5998 | 6043 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5999 | 6044 | |
| 6000 | 6045 | const inst_ty = f.typeOfIndex(inst); |
| 6001 | 6046 | const payload_ty = inst_ty.errorUnionPayload(zcu); |
| 6002 | 6047 | const payload = try f.resolveInst(ty_op.operand); |
| 6003 | const repr_is_err = !payload_ty.hasRuntimeBitsIgnoreComptime(zcu); | |
| 6048 | const repr_is_err = !payload_ty.hasRuntimeBitsIgnoreComptime(pt); | |
| 6004 | 6049 | const err_ty = inst_ty.errorUnionSet(zcu); |
| 6005 | 6050 | try reap(f, inst, &.{ty_op.operand}); |
| 6006 | 6051 | |
| ... | ... | @@ -6020,14 +6065,15 @@ fn airWrapErrUnionPay(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6020 | 6065 | else |
| 6021 | 6066 | try f.writeCValueMember(writer, local, .{ .identifier = "error" }); |
| 6022 | 6067 | try a.assign(f, writer); |
| 6023 | try f.object.dg.renderValue(writer, try zcu.intValue(try zcu.errorIntType(), 0), .Other); | |
| 6068 | try f.object.dg.renderValue(writer, try pt.intValue(try pt.errorIntType(), 0), .Other); | |
| 6024 | 6069 | try a.end(f, writer); |
| 6025 | 6070 | } |
| 6026 | 6071 | return local; |
| 6027 | 6072 | } |
| 6028 | 6073 | |
| 6029 | 6074 | fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const u8) !CValue { |
| 6030 | const zcu = f.object.dg.zcu; | |
| 6075 | const pt = f.object.dg.pt; | |
| 6076 | const zcu = pt.zcu; | |
| 6031 | 6077 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 6032 | 6078 | |
| 6033 | 6079 | const writer = f.object.writer(); |
| ... | ... | @@ -6042,9 +6088,9 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const |
| 6042 | 6088 | const a = try Assignment.start(f, writer, CType.bool); |
| 6043 | 6089 | try f.writeCValue(writer, local, .Other); |
| 6044 | 6090 | try a.assign(f, writer); |
| 6045 | const err_int_ty = try zcu.errorIntType(); | |
| 6091 | const err_int_ty = try pt.errorIntType(); | |
| 6046 | 6092 | if (!error_ty.errorSetIsEmpty(zcu)) |
| 6047 | if (payload_ty.hasRuntimeBits(zcu)) | |
| 6093 | if (payload_ty.hasRuntimeBits(pt)) | |
| 6048 | 6094 | if (is_ptr) |
| 6049 | 6095 | try f.writeCValueDerefMember(writer, operand, .{ .identifier = "error" }) |
| 6050 | 6096 | else |
| ... | ... | @@ -6052,17 +6098,18 @@ fn airIsErr(f: *Function, inst: Air.Inst.Index, is_ptr: bool, operator: []const |
| 6052 | 6098 | else |
| 6053 | 6099 | try f.writeCValue(writer, operand, .Other) |
| 6054 | 6100 | else |
| 6055 | try f.object.dg.renderValue(writer, try zcu.intValue(err_int_ty, 0), .Other); | |
| 6101 | try f.object.dg.renderValue(writer, try pt.intValue(err_int_ty, 0), .Other); | |
| 6056 | 6102 | try writer.writeByte(' '); |
| 6057 | 6103 | try writer.writeAll(operator); |
| 6058 | 6104 | try writer.writeByte(' '); |
| 6059 | try f.object.dg.renderValue(writer, try zcu.intValue(err_int_ty, 0), .Other); | |
| 6105 | try f.object.dg.renderValue(writer, try pt.intValue(err_int_ty, 0), .Other); | |
| 6060 | 6106 | try a.end(f, writer); |
| 6061 | 6107 | return local; |
| 6062 | 6108 | } |
| 6063 | 6109 | |
| 6064 | 6110 | fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6065 | const zcu = f.object.dg.zcu; | |
| 6111 | const pt = f.object.dg.pt; | |
| 6112 | const zcu = pt.zcu; | |
| 6066 | 6113 | const ctype_pool = &f.object.dg.ctype_pool; |
| 6067 | 6114 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6068 | 6115 | |
| ... | ... | @@ -6096,7 +6143,7 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6096 | 6143 | if (operand_child_ctype.info(ctype_pool) == .array) { |
| 6097 | 6144 | try writer.writeByte('&'); |
| 6098 | 6145 | try f.writeCValueDeref(writer, operand); |
| 6099 | try writer.print("[{}]", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, 0))}); | |
| 6146 | try writer.print("[{}]", .{try f.fmtIntLiteral(try pt.intValue(Type.usize, 0))}); | |
| 6100 | 6147 | } else try f.writeCValue(writer, operand, .Initializer); |
| 6101 | 6148 | } |
| 6102 | 6149 | try a.end(f, writer); |
| ... | ... | @@ -6106,7 +6153,7 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6106 | 6153 | try f.writeCValueMember(writer, local, .{ .identifier = "len" }); |
| 6107 | 6154 | try a.assign(f, writer); |
| 6108 | 6155 | try writer.print("{}", .{ |
| 6109 | try f.fmtIntLiteral(try zcu.intValue(Type.usize, array_ty.arrayLen(zcu))), | |
| 6156 | try f.fmtIntLiteral(try pt.intValue(Type.usize, array_ty.arrayLen(zcu))), | |
| 6110 | 6157 | }); |
| 6111 | 6158 | try a.end(f, writer); |
| 6112 | 6159 | } |
| ... | ... | @@ -6115,7 +6162,8 @@ fn airArrayToSlice(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6115 | 6162 | } |
| 6116 | 6163 | |
| 6117 | 6164 | fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6118 | const zcu = f.object.dg.zcu; | |
| 6165 | const pt = f.object.dg.pt; | |
| 6166 | const zcu = pt.zcu; | |
| 6119 | 6167 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6120 | 6168 | |
| 6121 | 6169 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -6165,7 +6213,8 @@ fn airFloatCast(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6165 | 6213 | } |
| 6166 | 6214 | |
| 6167 | 6215 | fn airIntFromPtr(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6168 | const zcu = f.object.dg.zcu; | |
| 6216 | const pt = f.object.dg.pt; | |
| 6217 | const zcu = pt.zcu; | |
| 6169 | 6218 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 6170 | 6219 | |
| 6171 | 6220 | const operand = try f.resolveInst(un_op); |
| ... | ... | @@ -6194,7 +6243,8 @@ fn airUnBuiltinCall( |
| 6194 | 6243 | operation: []const u8, |
| 6195 | 6244 | info: BuiltinInfo, |
| 6196 | 6245 | ) !CValue { |
| 6197 | const zcu = f.object.dg.zcu; | |
| 6246 | const pt = f.object.dg.pt; | |
| 6247 | const zcu = pt.zcu; | |
| 6198 | 6248 | |
| 6199 | 6249 | const operand = try f.resolveInst(operand_ref); |
| 6200 | 6250 | try reap(f, inst, &.{operand_ref}); |
| ... | ... | @@ -6237,7 +6287,8 @@ fn airBinBuiltinCall( |
| 6237 | 6287 | operation: []const u8, |
| 6238 | 6288 | info: BuiltinInfo, |
| 6239 | 6289 | ) !CValue { |
| 6240 | const zcu = f.object.dg.zcu; | |
| 6290 | const pt = f.object.dg.pt; | |
| 6291 | const zcu = pt.zcu; | |
| 6241 | 6292 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6242 | 6293 | |
| 6243 | 6294 | const operand_ty = f.typeOf(bin_op.lhs); |
| ... | ... | @@ -6292,7 +6343,8 @@ fn airCmpBuiltinCall( |
| 6292 | 6343 | operation: enum { cmp, operator }, |
| 6293 | 6344 | info: BuiltinInfo, |
| 6294 | 6345 | ) !CValue { |
| 6295 | const zcu = f.object.dg.zcu; | |
| 6346 | const pt = f.object.dg.pt; | |
| 6347 | const zcu = pt.zcu; | |
| 6296 | 6348 | const lhs = try f.resolveInst(data.lhs); |
| 6297 | 6349 | const rhs = try f.resolveInst(data.rhs); |
| 6298 | 6350 | try reap(f, inst, &.{ data.lhs, data.rhs }); |
| ... | ... | @@ -6333,7 +6385,7 @@ fn airCmpBuiltinCall( |
| 6333 | 6385 | try writer.writeByte(')'); |
| 6334 | 6386 | if (!ref_ret) try writer.print("{s}{}", .{ |
| 6335 | 6387 | compareOperatorC(operator), |
| 6336 | try f.fmtIntLiteral(try zcu.intValue(Type.i32, 0)), | |
| 6388 | try f.fmtIntLiteral(try pt.intValue(Type.i32, 0)), | |
| 6337 | 6389 | }); |
| 6338 | 6390 | try writer.writeAll(";\n"); |
| 6339 | 6391 | try v.end(f, inst, writer); |
| ... | ... | @@ -6342,7 +6394,8 @@ fn airCmpBuiltinCall( |
| 6342 | 6394 | } |
| 6343 | 6395 | |
| 6344 | 6396 | fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue { |
| 6345 | const zcu = f.object.dg.zcu; | |
| 6397 | const pt = f.object.dg.pt; | |
| 6398 | const zcu = pt.zcu; | |
| 6346 | 6399 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6347 | 6400 | const extra = f.air.extraData(Air.Cmpxchg, ty_pl.payload).data; |
| 6348 | 6401 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -6358,7 +6411,7 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6358 | 6411 | try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value }); |
| 6359 | 6412 | |
| 6360 | 6413 | const repr_ty = if (ty.isRuntimeFloat()) |
| 6361 | zcu.intType(.unsigned, @as(u16, @intCast(ty.abiSize(zcu) * 8))) catch unreachable | |
| 6414 | pt.intType(.unsigned, @as(u16, @intCast(ty.abiSize(pt) * 8))) catch unreachable | |
| 6362 | 6415 | else |
| 6363 | 6416 | ty; |
| 6364 | 6417 | |
| ... | ... | @@ -6448,7 +6501,8 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue |
| 6448 | 6501 | } |
| 6449 | 6502 | |
| 6450 | 6503 | fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6451 | const zcu = f.object.dg.zcu; | |
| 6504 | const pt = f.object.dg.pt; | |
| 6505 | const zcu = pt.zcu; | |
| 6452 | 6506 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6453 | 6507 | const extra = f.air.extraData(Air.AtomicRmw, pl_op.payload).data; |
| 6454 | 6508 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -6461,10 +6515,10 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6461 | 6515 | const operand_mat = try Materialize.start(f, inst, ty, operand); |
| 6462 | 6516 | try reap(f, inst, &.{ pl_op.operand, extra.operand }); |
| 6463 | 6517 | |
| 6464 | const repr_bits = @as(u16, @intCast(ty.abiSize(zcu) * 8)); | |
| 6518 | const repr_bits = @as(u16, @intCast(ty.abiSize(pt) * 8)); | |
| 6465 | 6519 | const is_float = ty.isRuntimeFloat(); |
| 6466 | 6520 | const is_128 = repr_bits == 128; |
| 6467 | const repr_ty = if (is_float) zcu.intType(.unsigned, repr_bits) catch unreachable else ty; | |
| 6521 | const repr_ty = if (is_float) pt.intType(.unsigned, repr_bits) catch unreachable else ty; | |
| 6468 | 6522 | |
| 6469 | 6523 | const local = try f.allocLocal(inst, inst_ty); |
| 6470 | 6524 | try writer.print("zig_atomicrmw_{s}", .{toAtomicRmwSuffix(extra.op())}); |
| ... | ... | @@ -6503,7 +6557,8 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6503 | 6557 | } |
| 6504 | 6558 | |
| 6505 | 6559 | fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6506 | const zcu = f.object.dg.zcu; | |
| 6560 | const pt = f.object.dg.pt; | |
| 6561 | const zcu = pt.zcu; | |
| 6507 | 6562 | const atomic_load = f.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load; |
| 6508 | 6563 | const ptr = try f.resolveInst(atomic_load.ptr); |
| 6509 | 6564 | try reap(f, inst, &.{atomic_load.ptr}); |
| ... | ... | @@ -6511,7 +6566,7 @@ fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6511 | 6566 | const ty = ptr_ty.childType(zcu); |
| 6512 | 6567 | |
| 6513 | 6568 | const repr_ty = if (ty.isRuntimeFloat()) |
| 6514 | zcu.intType(.unsigned, @as(u16, @intCast(ty.abiSize(zcu) * 8))) catch unreachable | |
| 6569 | pt.intType(.unsigned, @as(u16, @intCast(ty.abiSize(pt) * 8))) catch unreachable | |
| 6515 | 6570 | else |
| 6516 | 6571 | ty; |
| 6517 | 6572 | |
| ... | ... | @@ -6539,7 +6594,8 @@ fn airAtomicLoad(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6539 | 6594 | } |
| 6540 | 6595 | |
| 6541 | 6596 | fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CValue { |
| 6542 | const zcu = f.object.dg.zcu; | |
| 6597 | const pt = f.object.dg.pt; | |
| 6598 | const zcu = pt.zcu; | |
| 6543 | 6599 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6544 | 6600 | const ptr_ty = f.typeOf(bin_op.lhs); |
| 6545 | 6601 | const ty = ptr_ty.childType(zcu); |
| ... | ... | @@ -6551,7 +6607,7 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa |
| 6551 | 6607 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6552 | 6608 | |
| 6553 | 6609 | const repr_ty = if (ty.isRuntimeFloat()) |
| 6554 | zcu.intType(.unsigned, @as(u16, @intCast(ty.abiSize(zcu) * 8))) catch unreachable | |
| 6610 | pt.intType(.unsigned, @as(u16, @intCast(ty.abiSize(pt) * 8))) catch unreachable | |
| 6555 | 6611 | else |
| 6556 | 6612 | ty; |
| 6557 | 6613 | |
| ... | ... | @@ -6574,7 +6630,8 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa |
| 6574 | 6630 | } |
| 6575 | 6631 | |
| 6576 | 6632 | fn writeSliceOrPtr(f: *Function, writer: anytype, ptr: CValue, ptr_ty: Type) !void { |
| 6577 | const zcu = f.object.dg.zcu; | |
| 6633 | const pt = f.object.dg.pt; | |
| 6634 | const zcu = pt.zcu; | |
| 6578 | 6635 | if (ptr_ty.isSlice(zcu)) { |
| 6579 | 6636 | try f.writeCValueMember(writer, ptr, .{ .identifier = "ptr" }); |
| 6580 | 6637 | } else { |
| ... | ... | @@ -6583,14 +6640,15 @@ fn writeSliceOrPtr(f: *Function, writer: anytype, ptr: CValue, ptr_ty: Type) !vo |
| 6583 | 6640 | } |
| 6584 | 6641 | |
| 6585 | 6642 | fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6586 | const zcu = f.object.dg.zcu; | |
| 6643 | const pt = f.object.dg.pt; | |
| 6644 | const zcu = pt.zcu; | |
| 6587 | 6645 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6588 | 6646 | const dest_ty = f.typeOf(bin_op.lhs); |
| 6589 | 6647 | const dest_slice = try f.resolveInst(bin_op.lhs); |
| 6590 | 6648 | const value = try f.resolveInst(bin_op.rhs); |
| 6591 | 6649 | const elem_ty = f.typeOf(bin_op.rhs); |
| 6592 | const elem_abi_size = elem_ty.abiSize(zcu); | |
| 6593 | const val_is_undef = if (try f.air.value(bin_op.rhs, zcu)) |val| val.isUndefDeep(zcu) else false; | |
| 6650 | const elem_abi_size = elem_ty.abiSize(pt); | |
| 6651 | const val_is_undef = if (try f.air.value(bin_op.rhs, pt)) |val| val.isUndefDeep(zcu) else false; | |
| 6594 | 6652 | const writer = f.object.writer(); |
| 6595 | 6653 | |
| 6596 | 6654 | if (val_is_undef) { |
| ... | ... | @@ -6628,7 +6686,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6628 | 6686 | // For the assignment in this loop, the array pointer needs to get |
| 6629 | 6687 | // casted to a regular pointer, otherwise an error like this occurs: |
| 6630 | 6688 | // error: array type 'uint32_t[20]' (aka 'unsigned int[20]') is not assignable |
| 6631 | const elem_ptr_ty = try zcu.ptrType(.{ | |
| 6689 | const elem_ptr_ty = try pt.ptrType(.{ | |
| 6632 | 6690 | .child = elem_ty.toIntern(), |
| 6633 | 6691 | .flags = .{ |
| 6634 | 6692 | .size = .C, |
| ... | ... | @@ -6640,7 +6698,7 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6640 | 6698 | try writer.writeAll("for ("); |
| 6641 | 6699 | try f.writeCValue(writer, index, .Other); |
| 6642 | 6700 | try writer.writeAll(" = "); |
| 6643 | try f.object.dg.renderValue(writer, try zcu.intValue(Type.usize, 0), .Initializer); | |
| 6701 | try f.object.dg.renderValue(writer, try pt.intValue(Type.usize, 0), .Initializer); | |
| 6644 | 6702 | try writer.writeAll("; "); |
| 6645 | 6703 | try f.writeCValue(writer, index, .Other); |
| 6646 | 6704 | try writer.writeAll(" != "); |
| ... | ... | @@ -6705,7 +6763,8 @@ fn airMemset(f: *Function, inst: Air.Inst.Index, safety: bool) !CValue { |
| 6705 | 6763 | } |
| 6706 | 6764 | |
| 6707 | 6765 | fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6708 | const zcu = f.object.dg.zcu; | |
| 6766 | const pt = f.object.dg.pt; | |
| 6767 | const zcu = pt.zcu; | |
| 6709 | 6768 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6710 | 6769 | const dest_ptr = try f.resolveInst(bin_op.lhs); |
| 6711 | 6770 | const src_ptr = try f.resolveInst(bin_op.rhs); |
| ... | ... | @@ -6733,10 +6792,11 @@ fn airMemcpy(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6733 | 6792 | } |
| 6734 | 6793 | |
| 6735 | 6794 | fn writeArrayLen(f: *Function, writer: ArrayListWriter, dest_ptr: CValue, dest_ty: Type) !void { |
| 6736 | const zcu = f.object.dg.zcu; | |
| 6795 | const pt = f.object.dg.pt; | |
| 6796 | const zcu = pt.zcu; | |
| 6737 | 6797 | switch (dest_ty.ptrSize(zcu)) { |
| 6738 | 6798 | .One => try writer.print("{}", .{ |
| 6739 | try f.fmtIntLiteral(try zcu.intValue(Type.usize, dest_ty.childType(zcu).arrayLen(zcu))), | |
| 6799 | try f.fmtIntLiteral(try pt.intValue(Type.usize, dest_ty.childType(zcu).arrayLen(zcu))), | |
| 6740 | 6800 | }), |
| 6741 | 6801 | .Many, .C => unreachable, |
| 6742 | 6802 | .Slice => try f.writeCValueMember(writer, dest_ptr, .{ .identifier = "len" }), |
| ... | ... | @@ -6744,14 +6804,15 @@ fn writeArrayLen(f: *Function, writer: ArrayListWriter, dest_ptr: CValue, dest_t |
| 6744 | 6804 | } |
| 6745 | 6805 | |
| 6746 | 6806 | fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6747 | const zcu = f.object.dg.zcu; | |
| 6807 | const pt = f.object.dg.pt; | |
| 6808 | const zcu = pt.zcu; | |
| 6748 | 6809 | const bin_op = f.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6749 | 6810 | const union_ptr = try f.resolveInst(bin_op.lhs); |
| 6750 | 6811 | const new_tag = try f.resolveInst(bin_op.rhs); |
| 6751 | 6812 | try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); |
| 6752 | 6813 | |
| 6753 | 6814 | const union_ty = f.typeOf(bin_op.lhs).childType(zcu); |
| 6754 | const layout = union_ty.unionGetLayout(zcu); | |
| 6815 | const layout = union_ty.unionGetLayout(pt); | |
| 6755 | 6816 | if (layout.tag_size == 0) return .none; |
| 6756 | 6817 | const tag_ty = union_ty.unionTagTypeSafety(zcu).?; |
| 6757 | 6818 | |
| ... | ... | @@ -6765,14 +6826,14 @@ fn airSetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6765 | 6826 | } |
| 6766 | 6827 | |
| 6767 | 6828 | fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6768 | const zcu = f.object.dg.zcu; | |
| 6829 | const pt = f.object.dg.pt; | |
| 6769 | 6830 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6770 | 6831 | |
| 6771 | 6832 | const operand = try f.resolveInst(ty_op.operand); |
| 6772 | 6833 | try reap(f, inst, &.{ty_op.operand}); |
| 6773 | 6834 | |
| 6774 | 6835 | const union_ty = f.typeOf(ty_op.operand); |
| 6775 | const layout = union_ty.unionGetLayout(zcu); | |
| 6836 | const layout = union_ty.unionGetLayout(pt); | |
| 6776 | 6837 | if (layout.tag_size == 0) return .none; |
| 6777 | 6838 | |
| 6778 | 6839 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -6787,7 +6848,8 @@ fn airGetUnionTag(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6787 | 6848 | } |
| 6788 | 6849 | |
| 6789 | 6850 | fn airTagName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6790 | const zcu = f.object.dg.zcu; | |
| 6851 | const pt = f.object.dg.pt; | |
| 6852 | const zcu = pt.zcu; | |
| 6791 | 6853 | const un_op = f.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 6792 | 6854 | |
| 6793 | 6855 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -6824,7 +6886,8 @@ fn airErrorName(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6824 | 6886 | } |
| 6825 | 6887 | |
| 6826 | 6888 | fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6827 | const zcu = f.object.dg.zcu; | |
| 6889 | const pt = f.object.dg.pt; | |
| 6890 | const zcu = pt.zcu; | |
| 6828 | 6891 | const ty_op = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6829 | 6892 | |
| 6830 | 6893 | const operand = try f.resolveInst(ty_op.operand); |
| ... | ... | @@ -6879,7 +6942,7 @@ fn airSelect(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6879 | 6942 | } |
| 6880 | 6943 | |
| 6881 | 6944 | fn airShuffle(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6882 | const zcu = f.object.dg.zcu; | |
| 6945 | const pt = f.object.dg.pt; | |
| 6883 | 6946 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6884 | 6947 | const extra = f.air.extraData(Air.Shuffle, ty_pl.payload).data; |
| 6885 | 6948 | |
| ... | ... | @@ -6895,11 +6958,11 @@ fn airShuffle(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6895 | 6958 | for (0..extra.mask_len) |index| { |
| 6896 | 6959 | try f.writeCValue(writer, local, .Other); |
| 6897 | 6960 | try writer.writeByte('['); |
| 6898 | try f.object.dg.renderValue(writer, try zcu.intValue(Type.usize, index), .Other); | |
| 6961 | try f.object.dg.renderValue(writer, try pt.intValue(Type.usize, index), .Other); | |
| 6899 | 6962 | try writer.writeAll("] = "); |
| 6900 | 6963 | |
| 6901 | const mask_elem = (try mask.elemValue(zcu, index)).toSignedInt(zcu); | |
| 6902 | const src_val = try zcu.intValue(Type.usize, @as(u64, @intCast(mask_elem ^ mask_elem >> 63))); | |
| 6964 | const mask_elem = (try mask.elemValue(pt, index)).toSignedInt(pt); | |
| 6965 | const src_val = try pt.intValue(Type.usize, @as(u64, @intCast(mask_elem ^ mask_elem >> 63))); | |
| 6903 | 6966 | |
| 6904 | 6967 | try f.writeCValue(writer, if (mask_elem >= 0) lhs else rhs, .Other); |
| 6905 | 6968 | try writer.writeByte('['); |
| ... | ... | @@ -6911,7 +6974,8 @@ fn airShuffle(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6911 | 6974 | } |
| 6912 | 6975 | |
| 6913 | 6976 | fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6914 | const zcu = f.object.dg.zcu; | |
| 6977 | const pt = f.object.dg.pt; | |
| 6978 | const zcu = pt.zcu; | |
| 6915 | 6979 | const reduce = f.air.instructions.items(.data)[@intFromEnum(inst)].reduce; |
| 6916 | 6980 | |
| 6917 | 6981 | const scalar_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -6920,7 +6984,7 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6920 | 6984 | const operand_ty = f.typeOf(reduce.operand); |
| 6921 | 6985 | const writer = f.object.writer(); |
| 6922 | 6986 | |
| 6923 | const use_operator = scalar_ty.bitSize(zcu) <= 64; | |
| 6987 | const use_operator = scalar_ty.bitSize(pt) <= 64; | |
| 6924 | 6988 | const op: union(enum) { |
| 6925 | 6989 | const Func = struct { operation: []const u8, info: BuiltinInfo = .none }; |
| 6926 | 6990 | builtin: Func, |
| ... | ... | @@ -6971,37 +7035,37 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 6971 | 7035 | try f.object.dg.renderValue(writer, switch (reduce.operation) { |
| 6972 | 7036 | .Or, .Xor => switch (scalar_ty.zigTypeTag(zcu)) { |
| 6973 | 7037 | .Bool => Value.false, |
| 6974 | .Int => try zcu.intValue(scalar_ty, 0), | |
| 7038 | .Int => try pt.intValue(scalar_ty, 0), | |
| 6975 | 7039 | else => unreachable, |
| 6976 | 7040 | }, |
| 6977 | 7041 | .And => switch (scalar_ty.zigTypeTag(zcu)) { |
| 6978 | 7042 | .Bool => Value.true, |
| 6979 | 7043 | .Int => switch (scalar_ty.intInfo(zcu).signedness) { |
| 6980 | .unsigned => try scalar_ty.maxIntScalar(zcu, scalar_ty), | |
| 6981 | .signed => try zcu.intValue(scalar_ty, -1), | |
| 7044 | .unsigned => try scalar_ty.maxIntScalar(pt, scalar_ty), | |
| 7045 | .signed => try pt.intValue(scalar_ty, -1), | |
| 6982 | 7046 | }, |
| 6983 | 7047 | else => unreachable, |
| 6984 | 7048 | }, |
| 6985 | 7049 | .Add => switch (scalar_ty.zigTypeTag(zcu)) { |
| 6986 | .Int => try zcu.intValue(scalar_ty, 0), | |
| 6987 | .Float => try zcu.floatValue(scalar_ty, 0.0), | |
| 7050 | .Int => try pt.intValue(scalar_ty, 0), | |
| 7051 | .Float => try pt.floatValue(scalar_ty, 0.0), | |
| 6988 | 7052 | else => unreachable, |
| 6989 | 7053 | }, |
| 6990 | 7054 | .Mul => switch (scalar_ty.zigTypeTag(zcu)) { |
| 6991 | .Int => try zcu.intValue(scalar_ty, 1), | |
| 6992 | .Float => try zcu.floatValue(scalar_ty, 1.0), | |
| 7055 | .Int => try pt.intValue(scalar_ty, 1), | |
| 7056 | .Float => try pt.floatValue(scalar_ty, 1.0), | |
| 6993 | 7057 | else => unreachable, |
| 6994 | 7058 | }, |
| 6995 | 7059 | .Min => switch (scalar_ty.zigTypeTag(zcu)) { |
| 6996 | 7060 | .Bool => Value.true, |
| 6997 | .Int => try scalar_ty.maxIntScalar(zcu, scalar_ty), | |
| 6998 | .Float => try zcu.floatValue(scalar_ty, std.math.nan(f128)), | |
| 7061 | .Int => try scalar_ty.maxIntScalar(pt, scalar_ty), | |
| 7062 | .Float => try pt.floatValue(scalar_ty, std.math.nan(f128)), | |
| 6999 | 7063 | else => unreachable, |
| 7000 | 7064 | }, |
| 7001 | 7065 | .Max => switch (scalar_ty.zigTypeTag(zcu)) { |
| 7002 | 7066 | .Bool => Value.false, |
| 7003 | .Int => try scalar_ty.minIntScalar(zcu, scalar_ty), | |
| 7004 | .Float => try zcu.floatValue(scalar_ty, std.math.nan(f128)), | |
| 7067 | .Int => try scalar_ty.minIntScalar(pt, scalar_ty), | |
| 7068 | .Float => try pt.floatValue(scalar_ty, std.math.nan(f128)), | |
| 7005 | 7069 | else => unreachable, |
| 7006 | 7070 | }, |
| 7007 | 7071 | }, .Initializer); |
| ... | ... | @@ -7046,7 +7110,8 @@ fn airReduce(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7046 | 7110 | } |
| 7047 | 7111 | |
| 7048 | 7112 | fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7049 | const zcu = f.object.dg.zcu; | |
| 7113 | const pt = f.object.dg.pt; | |
| 7114 | const zcu = pt.zcu; | |
| 7050 | 7115 | const ip = &zcu.intern_pool; |
| 7051 | 7116 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 7052 | 7117 | const inst_ty = f.typeOfIndex(inst); |
| ... | ... | @@ -7096,7 +7161,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7096 | 7161 | var field_it = loaded_struct.iterateRuntimeOrder(ip); |
| 7097 | 7162 | while (field_it.next()) |field_index| { |
| 7098 | 7163 | const field_ty = Type.fromInterned(loaded_struct.field_types.get(ip)[field_index]); |
| 7099 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 7164 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 7100 | 7165 | |
| 7101 | 7166 | const a = try Assignment.start(f, writer, try f.ctypeFromType(field_ty, .complete)); |
| 7102 | 7167 | try f.writeCValueMember(writer, local, if (loaded_struct.fieldName(ip, field_index).unwrap()) |field_name| |
| ... | ... | @@ -7113,7 +7178,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7113 | 7178 | try writer.writeAll(" = "); |
| 7114 | 7179 | const int_info = inst_ty.intInfo(zcu); |
| 7115 | 7180 | |
| 7116 | const bit_offset_ty = try zcu.intType(.unsigned, Type.smallestUnsignedBits(int_info.bits - 1)); | |
| 7181 | const bit_offset_ty = try pt.intType(.unsigned, Type.smallestUnsignedBits(int_info.bits - 1)); | |
| 7117 | 7182 | |
| 7118 | 7183 | var bit_offset: u64 = 0; |
| 7119 | 7184 | |
| ... | ... | @@ -7121,7 +7186,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7121 | 7186 | for (0..elements.len) |field_index| { |
| 7122 | 7187 | if (inst_ty.structFieldIsComptime(field_index, zcu)) continue; |
| 7123 | 7188 | const field_ty = inst_ty.structFieldType(field_index, zcu); |
| 7124 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 7189 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 7125 | 7190 | |
| 7126 | 7191 | if (!empty) { |
| 7127 | 7192 | try writer.writeAll("zig_or_"); |
| ... | ... | @@ -7134,7 +7199,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7134 | 7199 | for (resolved_elements, 0..) |element, field_index| { |
| 7135 | 7200 | if (inst_ty.structFieldIsComptime(field_index, zcu)) continue; |
| 7136 | 7201 | const field_ty = inst_ty.structFieldType(field_index, zcu); |
| 7137 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 7202 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 7138 | 7203 | |
| 7139 | 7204 | if (!empty) try writer.writeAll(", "); |
| 7140 | 7205 | // TODO: Skip this entire shift if val is 0? |
| ... | ... | @@ -7160,13 +7225,13 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7160 | 7225 | } |
| 7161 | 7226 | |
| 7162 | 7227 | try writer.print(", {}", .{ |
| 7163 | try f.fmtIntLiteral(try zcu.intValue(bit_offset_ty, bit_offset)), | |
| 7228 | try f.fmtIntLiteral(try pt.intValue(bit_offset_ty, bit_offset)), | |
| 7164 | 7229 | }); |
| 7165 | 7230 | try f.object.dg.renderBuiltinInfo(writer, inst_ty, .bits); |
| 7166 | 7231 | try writer.writeByte(')'); |
| 7167 | 7232 | if (!empty) try writer.writeByte(')'); |
| 7168 | 7233 | |
| 7169 | bit_offset += field_ty.bitSize(zcu); | |
| 7234 | bit_offset += field_ty.bitSize(pt); | |
| 7170 | 7235 | empty = false; |
| 7171 | 7236 | } |
| 7172 | 7237 | try writer.writeAll(";\n"); |
| ... | ... | @@ -7176,7 +7241,7 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7176 | 7241 | .anon_struct_type => |anon_struct_info| for (0..anon_struct_info.types.len) |field_index| { |
| 7177 | 7242 | if (anon_struct_info.values.get(ip)[field_index] != .none) continue; |
| 7178 | 7243 | const field_ty = Type.fromInterned(anon_struct_info.types.get(ip)[field_index]); |
| 7179 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 7244 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 7180 | 7245 | |
| 7181 | 7246 | const a = try Assignment.start(f, writer, try f.ctypeFromType(field_ty, .complete)); |
| 7182 | 7247 | try f.writeCValueMember(writer, local, if (anon_struct_info.fieldName(ip, field_index).unwrap()) |field_name| |
| ... | ... | @@ -7194,7 +7259,8 @@ fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7194 | 7259 | } |
| 7195 | 7260 | |
| 7196 | 7261 | fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7197 | const zcu = f.object.dg.zcu; | |
| 7262 | const pt = f.object.dg.pt; | |
| 7263 | const zcu = pt.zcu; | |
| 7198 | 7264 | const ip = &zcu.intern_pool; |
| 7199 | 7265 | const ty_pl = f.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 7200 | 7266 | const extra = f.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| ... | ... | @@ -7211,15 +7277,15 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7211 | 7277 | if (loaded_union.getLayout(ip) == .@"packed") return f.moveCValue(inst, union_ty, payload); |
| 7212 | 7278 | |
| 7213 | 7279 | const field: CValue = if (union_ty.unionTagTypeSafety(zcu)) |tag_ty| field: { |
| 7214 | const layout = union_ty.unionGetLayout(zcu); | |
| 7280 | const layout = union_ty.unionGetLayout(pt); | |
| 7215 | 7281 | if (layout.tag_size != 0) { |
| 7216 | 7282 | const field_index = tag_ty.enumFieldIndex(field_name, zcu).?; |
| 7217 | const tag_val = try zcu.enumValueFieldIndex(tag_ty, field_index); | |
| 7283 | const tag_val = try pt.enumValueFieldIndex(tag_ty, field_index); | |
| 7218 | 7284 | |
| 7219 | 7285 | const a = try Assignment.start(f, writer, try f.ctypeFromType(tag_ty, .complete)); |
| 7220 | 7286 | try f.writeCValueMember(writer, local, .{ .identifier = "tag" }); |
| 7221 | 7287 | try a.assign(f, writer); |
| 7222 | try writer.print("{}", .{try f.fmtIntLiteral(try tag_val.intFromEnum(tag_ty, zcu))}); | |
| 7288 | try writer.print("{}", .{try f.fmtIntLiteral(try tag_val.intFromEnum(tag_ty, pt))}); | |
| 7223 | 7289 | try a.end(f, writer); |
| 7224 | 7290 | } |
| 7225 | 7291 | break :field .{ .payload_identifier = field_name.toSlice(ip) }; |
| ... | ... | @@ -7234,7 +7300,8 @@ fn airUnionInit(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7234 | 7300 | } |
| 7235 | 7301 | |
| 7236 | 7302 | fn airPrefetch(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7237 | const zcu = f.object.dg.zcu; | |
| 7303 | const pt = f.object.dg.pt; | |
| 7304 | const zcu = pt.zcu; | |
| 7238 | 7305 | const prefetch = f.air.instructions.items(.data)[@intFromEnum(inst)].prefetch; |
| 7239 | 7306 | |
| 7240 | 7307 | const ptr_ty = f.typeOf(prefetch.ptr); |
| ... | ... | @@ -7291,7 +7358,8 @@ fn airWasmMemoryGrow(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7291 | 7358 | } |
| 7292 | 7359 | |
| 7293 | 7360 | fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7294 | const zcu = f.object.dg.zcu; | |
| 7361 | const pt = f.object.dg.pt; | |
| 7362 | const zcu = pt.zcu; | |
| 7295 | 7363 | const pl_op = f.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 7296 | 7364 | const bin_op = f.air.extraData(Air.Bin, pl_op.payload).data; |
| 7297 | 7365 | |
| ... | ... | @@ -7326,7 +7394,8 @@ fn airMulAdd(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7326 | 7394 | } |
| 7327 | 7395 | |
| 7328 | 7396 | fn airCVaStart(f: *Function, inst: Air.Inst.Index) !CValue { |
| 7329 | const zcu = f.object.dg.zcu; | |
| 7397 | const pt = f.object.dg.pt; | |
| 7398 | const zcu = pt.zcu; | |
| 7330 | 7399 | const inst_ty = f.typeOfIndex(inst); |
| 7331 | 7400 | const decl_index = f.object.dg.pass.decl; |
| 7332 | 7401 | const decl = zcu.declPtr(decl_index); |
| ... | ... | @@ -7699,7 +7768,8 @@ fn formatIntLiteral( |
| 7699 | 7768 | options: std.fmt.FormatOptions, |
| 7700 | 7769 | writer: anytype, |
| 7701 | 7770 | ) @TypeOf(writer).Error!void { |
| 7702 | const zcu = data.dg.zcu; | |
| 7771 | const pt = data.dg.pt; | |
| 7772 | const zcu = pt.zcu; | |
| 7703 | 7773 | const target = &data.dg.mod.resolved_target.result; |
| 7704 | 7774 | const ctype_pool = &data.dg.ctype_pool; |
| 7705 | 7775 | |
| ... | ... | @@ -7732,7 +7802,7 @@ fn formatIntLiteral( |
| 7732 | 7802 | }; |
| 7733 | 7803 | undef_int.truncate(undef_int.toConst(), data.int_info.signedness, data.int_info.bits); |
| 7734 | 7804 | break :blk undef_int.toConst(); |
| 7735 | } else data.val.toBigInt(&int_buf, zcu); | |
| 7805 | } else data.val.toBigInt(&int_buf, pt); | |
| 7736 | 7806 | assert(int.fitsInTwosComp(data.int_info.signedness, data.int_info.bits)); |
| 7737 | 7807 | |
| 7738 | 7808 | const c_bits: usize = @intCast(data.ctype.byteSize(ctype_pool, data.dg.mod) * 8); |
| ... | ... | @@ -7866,7 +7936,7 @@ fn formatIntLiteral( |
| 7866 | 7936 | .int_info = c_limb_int_info, |
| 7867 | 7937 | .kind = data.kind, |
| 7868 | 7938 | .ctype = c_limb_ctype, |
| 7869 | .val = try zcu.intValue_big(Type.comptime_int, c_limb_mut.toConst()), | |
| 7939 | .val = try pt.intValue_big(Type.comptime_int, c_limb_mut.toConst()), | |
| 7870 | 7940 | }, fmt, options, writer); |
| 7871 | 7941 | } |
| 7872 | 7942 | } |
| ... | ... | @@ -7940,17 +8010,18 @@ const Vectorize = struct { |
| 7940 | 8010 | index: CValue = .none, |
| 7941 | 8011 | |
| 7942 | 8012 | pub fn start(f: *Function, inst: Air.Inst.Index, writer: anytype, ty: Type) !Vectorize { |
| 7943 | const zcu = f.object.dg.zcu; | |
| 8013 | const pt = f.object.dg.pt; | |
| 8014 | const zcu = pt.zcu; | |
| 7944 | 8015 | return if (ty.zigTypeTag(zcu) == .Vector) index: { |
| 7945 | 8016 | const local = try f.allocLocal(inst, Type.usize); |
| 7946 | 8017 | |
| 7947 | 8018 | try writer.writeAll("for ("); |
| 7948 | 8019 | try f.writeCValue(writer, local, .Other); |
| 7949 | try writer.print(" = {d}; ", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, 0))}); | |
| 8020 | try writer.print(" = {d}; ", .{try f.fmtIntLiteral(try pt.intValue(Type.usize, 0))}); | |
| 7950 | 8021 | try f.writeCValue(writer, local, .Other); |
| 7951 | try writer.print(" < {d}; ", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, ty.vectorLen(zcu)))}); | |
| 8022 | try writer.print(" < {d}; ", .{try f.fmtIntLiteral(try pt.intValue(Type.usize, ty.vectorLen(zcu)))}); | |
| 7952 | 8023 | try f.writeCValue(writer, local, .Other); |
| 7953 | try writer.print(" += {d}) {{\n", .{try f.fmtIntLiteral(try zcu.intValue(Type.usize, 1))}); | |
| 8024 | try writer.print(" += {d}) {{\n", .{try f.fmtIntLiteral(try pt.intValue(Type.usize, 1))}); | |
| 7954 | 8025 | f.object.indent_writer.pushIndent(); |
| 7955 | 8026 | |
| 7956 | 8027 | break :index .{ .index = local }; |
| ... | ... | @@ -7974,10 +8045,10 @@ const Vectorize = struct { |
| 7974 | 8045 | } |
| 7975 | 8046 | }; |
| 7976 | 8047 | |
| 7977 | fn lowersToArray(ty: Type, zcu: *Zcu) bool { | |
| 7978 | return switch (ty.zigTypeTag(zcu)) { | |
| 8048 | fn lowersToArray(ty: Type, pt: Zcu.PerThread) bool { | |
| 8049 | return switch (ty.zigTypeTag(pt.zcu)) { | |
| 7979 | 8050 | .Array, .Vector => return true, |
| 7980 | else => return ty.isAbiInt(zcu) and toCIntBits(@as(u32, @intCast(ty.bitSize(zcu)))) == null, | |
| 8051 | else => return ty.isAbiInt(pt.zcu) and toCIntBits(@as(u32, @intCast(ty.bitSize(pt)))) == null, | |
| 7981 | 8052 | }; |
| 7982 | 8053 | } |
| 7983 | 8054 |
src/codegen/c/Type.zig+33-33| ... | ... | @@ -1339,11 +1339,11 @@ pub const Pool = struct { |
| 1339 | 1339 | allocator: std.mem.Allocator, |
| 1340 | 1340 | scratch: *std.ArrayListUnmanaged(u32), |
| 1341 | 1341 | ty: Type, |
| 1342 | zcu: *Zcu, | |
| 1342 | pt: Zcu.PerThread, | |
| 1343 | 1343 | mod: *Module, |
| 1344 | 1344 | kind: Kind, |
| 1345 | 1345 | ) !CType { |
| 1346 | const ip = &zcu.intern_pool; | |
| 1346 | const ip = &pt.zcu.intern_pool; | |
| 1347 | 1347 | switch (ty.toIntern()) { |
| 1348 | 1348 | .u0_type, |
| 1349 | 1349 | .i0_type, |
| ... | ... | @@ -1400,7 +1400,7 @@ pub const Pool = struct { |
| 1400 | 1400 | allocator, |
| 1401 | 1401 | scratch, |
| 1402 | 1402 | Type.fromInterned(ip.loadEnumType(ip_index).tag_ty), |
| 1403 | zcu, | |
| 1403 | pt, | |
| 1404 | 1404 | mod, |
| 1405 | 1405 | kind, |
| 1406 | 1406 | ), |
| ... | ... | @@ -1409,7 +1409,7 @@ pub const Pool = struct { |
| 1409 | 1409 | .adhoc_inferred_error_set_type, |
| 1410 | 1410 | => return pool.fromIntInfo(allocator, .{ |
| 1411 | 1411 | .signedness = .unsigned, |
| 1412 | .bits = zcu.errorSetBits(), | |
| 1412 | .bits = pt.zcu.errorSetBits(), | |
| 1413 | 1413 | }, mod, kind), |
| 1414 | 1414 | .manyptr_u8_type, |
| 1415 | 1415 | => return pool.getPointer(allocator, .{ |
| ... | ... | @@ -1492,13 +1492,13 @@ pub const Pool = struct { |
| 1492 | 1492 | allocator, |
| 1493 | 1493 | scratch, |
| 1494 | 1494 | Type.fromInterned(ptr_info.child), |
| 1495 | zcu, | |
| 1495 | pt, | |
| 1496 | 1496 | mod, |
| 1497 | 1497 | .forward, |
| 1498 | 1498 | ), |
| 1499 | 1499 | .alignas = AlignAs.fromAlignment(.{ |
| 1500 | 1500 | .@"align" = ptr_info.flags.alignment, |
| 1501 | .abi = Type.fromInterned(ptr_info.child).abiAlignment(zcu), | |
| 1501 | .abi = Type.fromInterned(ptr_info.child).abiAlignment(pt), | |
| 1502 | 1502 | }), |
| 1503 | 1503 | }; |
| 1504 | 1504 | break :elem_ctype if (elem.alignas.abiOrder().compare(.gte)) |
| ... | ... | @@ -1535,7 +1535,7 @@ pub const Pool = struct { |
| 1535 | 1535 | allocator, |
| 1536 | 1536 | scratch, |
| 1537 | 1537 | Type.fromInterned(ip.slicePtrType(ip_index)), |
| 1538 | zcu, | |
| 1538 | pt, | |
| 1539 | 1539 | mod, |
| 1540 | 1540 | kind, |
| 1541 | 1541 | ), |
| ... | ... | @@ -1560,7 +1560,7 @@ pub const Pool = struct { |
| 1560 | 1560 | allocator, |
| 1561 | 1561 | scratch, |
| 1562 | 1562 | elem_type, |
| 1563 | zcu, | |
| 1563 | pt, | |
| 1564 | 1564 | mod, |
| 1565 | 1565 | kind.noParameter(), |
| 1566 | 1566 | ); |
| ... | ... | @@ -1574,7 +1574,7 @@ pub const Pool = struct { |
| 1574 | 1574 | .{ |
| 1575 | 1575 | .name = .{ .index = .array }, |
| 1576 | 1576 | .ctype = array_ctype, |
| 1577 | .alignas = AlignAs.fromAbiAlignment(elem_type.abiAlignment(zcu)), | |
| 1577 | .alignas = AlignAs.fromAbiAlignment(elem_type.abiAlignment(pt)), | |
| 1578 | 1578 | }, |
| 1579 | 1579 | }; |
| 1580 | 1580 | return pool.fromFields(allocator, .@"struct", &fields, kind); |
| ... | ... | @@ -1586,7 +1586,7 @@ pub const Pool = struct { |
| 1586 | 1586 | allocator, |
| 1587 | 1587 | scratch, |
| 1588 | 1588 | elem_type, |
| 1589 | zcu, | |
| 1589 | pt, | |
| 1590 | 1590 | mod, |
| 1591 | 1591 | kind.noParameter(), |
| 1592 | 1592 | ); |
| ... | ... | @@ -1600,7 +1600,7 @@ pub const Pool = struct { |
| 1600 | 1600 | .{ |
| 1601 | 1601 | .name = .{ .index = .array }, |
| 1602 | 1602 | .ctype = vector_ctype, |
| 1603 | .alignas = AlignAs.fromAbiAlignment(elem_type.abiAlignment(zcu)), | |
| 1603 | .alignas = AlignAs.fromAbiAlignment(elem_type.abiAlignment(pt)), | |
| 1604 | 1604 | }, |
| 1605 | 1605 | }; |
| 1606 | 1606 | return pool.fromFields(allocator, .@"struct", &fields, kind); |
| ... | ... | @@ -1611,7 +1611,7 @@ pub const Pool = struct { |
| 1611 | 1611 | allocator, |
| 1612 | 1612 | scratch, |
| 1613 | 1613 | Type.fromInterned(payload_type), |
| 1614 | zcu, | |
| 1614 | pt, | |
| 1615 | 1615 | mod, |
| 1616 | 1616 | kind.noParameter(), |
| 1617 | 1617 | ); |
| ... | ... | @@ -1635,7 +1635,7 @@ pub const Pool = struct { |
| 1635 | 1635 | .name = .{ .index = .payload }, |
| 1636 | 1636 | .ctype = payload_ctype, |
| 1637 | 1637 | .alignas = AlignAs.fromAbiAlignment( |
| 1638 | Type.fromInterned(payload_type).abiAlignment(zcu), | |
| 1638 | Type.fromInterned(payload_type).abiAlignment(pt), | |
| 1639 | 1639 | ), |
| 1640 | 1640 | }, |
| 1641 | 1641 | }; |
| ... | ... | @@ -1643,7 +1643,7 @@ pub const Pool = struct { |
| 1643 | 1643 | }, |
| 1644 | 1644 | .anyframe_type => unreachable, |
| 1645 | 1645 | .error_union_type => |error_union_info| { |
| 1646 | const error_set_bits = zcu.errorSetBits(); | |
| 1646 | const error_set_bits = pt.zcu.errorSetBits(); | |
| 1647 | 1647 | const error_set_ctype = try pool.fromIntInfo(allocator, .{ |
| 1648 | 1648 | .signedness = .unsigned, |
| 1649 | 1649 | .bits = error_set_bits, |
| ... | ... | @@ -1654,7 +1654,7 @@ pub const Pool = struct { |
| 1654 | 1654 | allocator, |
| 1655 | 1655 | scratch, |
| 1656 | 1656 | payload_type, |
| 1657 | zcu, | |
| 1657 | pt, | |
| 1658 | 1658 | mod, |
| 1659 | 1659 | kind.noParameter(), |
| 1660 | 1660 | ); |
| ... | ... | @@ -1671,7 +1671,7 @@ pub const Pool = struct { |
| 1671 | 1671 | .{ |
| 1672 | 1672 | .name = .{ .index = .payload }, |
| 1673 | 1673 | .ctype = payload_ctype, |
| 1674 | .alignas = AlignAs.fromAbiAlignment(payload_type.abiAlignment(zcu)), | |
| 1674 | .alignas = AlignAs.fromAbiAlignment(payload_type.abiAlignment(pt)), | |
| 1675 | 1675 | }, |
| 1676 | 1676 | }; |
| 1677 | 1677 | return pool.fromFields(allocator, .@"struct", &fields, kind); |
| ... | ... | @@ -1685,7 +1685,7 @@ pub const Pool = struct { |
| 1685 | 1685 | .tag = .@"struct", |
| 1686 | 1686 | .name = .{ .owner_decl = loaded_struct.decl.unwrap().? }, |
| 1687 | 1687 | }); |
| 1688 | if (kind.isForward()) return if (ty.hasRuntimeBitsIgnoreComptime(zcu)) | |
| 1688 | if (kind.isForward()) return if (ty.hasRuntimeBitsIgnoreComptime(pt)) | |
| 1689 | 1689 | fwd_decl |
| 1690 | 1690 | else |
| 1691 | 1691 | CType.void; |
| ... | ... | @@ -1706,7 +1706,7 @@ pub const Pool = struct { |
| 1706 | 1706 | allocator, |
| 1707 | 1707 | scratch, |
| 1708 | 1708 | field_type, |
| 1709 | zcu, | |
| 1709 | pt, | |
| 1710 | 1710 | mod, |
| 1711 | 1711 | kind.noParameter(), |
| 1712 | 1712 | ); |
| ... | ... | @@ -1718,7 +1718,7 @@ pub const Pool = struct { |
| 1718 | 1718 | String.fromUnnamed(@intCast(field_index)); |
| 1719 | 1719 | const field_alignas = AlignAs.fromAlignment(.{ |
| 1720 | 1720 | .@"align" = loaded_struct.fieldAlign(ip, field_index), |
| 1721 | .abi = field_type.abiAlignment(zcu), | |
| 1721 | .abi = field_type.abiAlignment(pt), | |
| 1722 | 1722 | }); |
| 1723 | 1723 | pool.addHashedExtraAssumeCapacityTo(scratch, &hasher, Field, .{ |
| 1724 | 1724 | .name = field_name.index, |
| ... | ... | @@ -1745,7 +1745,7 @@ pub const Pool = struct { |
| 1745 | 1745 | allocator, |
| 1746 | 1746 | scratch, |
| 1747 | 1747 | Type.fromInterned(loaded_struct.backingIntType(ip).*), |
| 1748 | zcu, | |
| 1748 | pt, | |
| 1749 | 1749 | mod, |
| 1750 | 1750 | kind, |
| 1751 | 1751 | ), |
| ... | ... | @@ -1766,7 +1766,7 @@ pub const Pool = struct { |
| 1766 | 1766 | allocator, |
| 1767 | 1767 | scratch, |
| 1768 | 1768 | field_type, |
| 1769 | zcu, | |
| 1769 | pt, | |
| 1770 | 1770 | mod, |
| 1771 | 1771 | kind.noParameter(), |
| 1772 | 1772 | ); |
| ... | ... | @@ -1780,7 +1780,7 @@ pub const Pool = struct { |
| 1780 | 1780 | .name = field_name.index, |
| 1781 | 1781 | .ctype = field_ctype.index, |
| 1782 | 1782 | .flags = .{ .alignas = AlignAs.fromAbiAlignment( |
| 1783 | field_type.abiAlignment(zcu), | |
| 1783 | field_type.abiAlignment(pt), | |
| 1784 | 1784 | ) }, |
| 1785 | 1785 | }); |
| 1786 | 1786 | } |
| ... | ... | @@ -1806,7 +1806,7 @@ pub const Pool = struct { |
| 1806 | 1806 | extra_index, |
| 1807 | 1807 | ); |
| 1808 | 1808 | } |
| 1809 | const fwd_decl = try pool.fromType(allocator, scratch, ty, zcu, mod, .forward); | |
| 1809 | const fwd_decl = try pool.fromType(allocator, scratch, ty, pt, mod, .forward); | |
| 1810 | 1810 | try pool.ensureUnusedCapacity(allocator, 1); |
| 1811 | 1811 | const extra_index = try pool.addHashedExtra(allocator, &hasher, Aggregate, .{ |
| 1812 | 1812 | .fwd_decl = fwd_decl.index, |
| ... | ... | @@ -1824,7 +1824,7 @@ pub const Pool = struct { |
| 1824 | 1824 | .tag = if (has_tag) .@"struct" else .@"union", |
| 1825 | 1825 | .name = .{ .owner_decl = loaded_union.decl }, |
| 1826 | 1826 | }); |
| 1827 | if (kind.isForward()) return if (ty.hasRuntimeBitsIgnoreComptime(zcu)) | |
| 1827 | if (kind.isForward()) return if (ty.hasRuntimeBitsIgnoreComptime(pt)) | |
| 1828 | 1828 | fwd_decl |
| 1829 | 1829 | else |
| 1830 | 1830 | CType.void; |
| ... | ... | @@ -1847,7 +1847,7 @@ pub const Pool = struct { |
| 1847 | 1847 | allocator, |
| 1848 | 1848 | scratch, |
| 1849 | 1849 | field_type, |
| 1850 | zcu, | |
| 1850 | pt, | |
| 1851 | 1851 | mod, |
| 1852 | 1852 | kind.noParameter(), |
| 1853 | 1853 | ); |
| ... | ... | @@ -1858,7 +1858,7 @@ pub const Pool = struct { |
| 1858 | 1858 | ); |
| 1859 | 1859 | const field_alignas = AlignAs.fromAlignment(.{ |
| 1860 | 1860 | .@"align" = loaded_union.fieldAlign(ip, field_index), |
| 1861 | .abi = field_type.abiAlignment(zcu), | |
| 1861 | .abi = field_type.abiAlignment(pt), | |
| 1862 | 1862 | }); |
| 1863 | 1863 | pool.addHashedExtraAssumeCapacityTo(scratch, &hasher, Field, .{ |
| 1864 | 1864 | .name = field_name.index, |
| ... | ... | @@ -1895,7 +1895,7 @@ pub const Pool = struct { |
| 1895 | 1895 | allocator, |
| 1896 | 1896 | scratch, |
| 1897 | 1897 | tag_type, |
| 1898 | zcu, | |
| 1898 | pt, | |
| 1899 | 1899 | mod, |
| 1900 | 1900 | kind.noParameter(), |
| 1901 | 1901 | ); |
| ... | ... | @@ -1903,7 +1903,7 @@ pub const Pool = struct { |
| 1903 | 1903 | struct_fields[struct_fields_len] = .{ |
| 1904 | 1904 | .name = .{ .index = .tag }, |
| 1905 | 1905 | .ctype = tag_ctype, |
| 1906 | .alignas = AlignAs.fromAbiAlignment(tag_type.abiAlignment(zcu)), | |
| 1906 | .alignas = AlignAs.fromAbiAlignment(tag_type.abiAlignment(pt)), | |
| 1907 | 1907 | }; |
| 1908 | 1908 | struct_fields_len += 1; |
| 1909 | 1909 | } |
| ... | ... | @@ -1951,7 +1951,7 @@ pub const Pool = struct { |
| 1951 | 1951 | }, |
| 1952 | 1952 | .@"packed" => return pool.fromIntInfo(allocator, .{ |
| 1953 | 1953 | .signedness = .unsigned, |
| 1954 | .bits = @intCast(ty.bitSize(zcu)), | |
| 1954 | .bits = @intCast(ty.bitSize(pt)), | |
| 1955 | 1955 | }, mod, kind), |
| 1956 | 1956 | } |
| 1957 | 1957 | }, |
| ... | ... | @@ -1960,7 +1960,7 @@ pub const Pool = struct { |
| 1960 | 1960 | allocator, |
| 1961 | 1961 | scratch, |
| 1962 | 1962 | Type.fromInterned(ip.loadEnumType(ip_index).tag_ty), |
| 1963 | zcu, | |
| 1963 | pt, | |
| 1964 | 1964 | mod, |
| 1965 | 1965 | kind, |
| 1966 | 1966 | ), |
| ... | ... | @@ -1975,7 +1975,7 @@ pub const Pool = struct { |
| 1975 | 1975 | allocator, |
| 1976 | 1976 | scratch, |
| 1977 | 1977 | return_type, |
| 1978 | zcu, | |
| 1978 | pt, | |
| 1979 | 1979 | mod, |
| 1980 | 1980 | kind.asParameter(), |
| 1981 | 1981 | ) else CType.void; |
| ... | ... | @@ -1987,7 +1987,7 @@ pub const Pool = struct { |
| 1987 | 1987 | allocator, |
| 1988 | 1988 | scratch, |
| 1989 | 1989 | param_type, |
| 1990 | zcu, | |
| 1990 | pt, | |
| 1991 | 1991 | mod, |
| 1992 | 1992 | kind.asParameter(), |
| 1993 | 1993 | ); |
| ... | ... | @@ -2011,7 +2011,7 @@ pub const Pool = struct { |
| 2011 | 2011 | .inferred_error_set_type, |
| 2012 | 2012 | => return pool.fromIntInfo(allocator, .{ |
| 2013 | 2013 | .signedness = .unsigned, |
| 2014 | .bits = zcu.errorSetBits(), | |
| 2014 | .bits = pt.zcu.errorSetBits(), | |
| 2015 | 2015 | }, mod, kind), |
| 2016 | 2016 | |
| 2017 | 2017 | .undef, |
src/codegen/llvm.zig+751-680| ... | ... | @@ -15,8 +15,6 @@ const link = @import("../link.zig"); |
| 15 | 15 | const Compilation = @import("../Compilation.zig"); |
| 16 | 16 | const build_options = @import("build_options"); |
| 17 | 17 | const Zcu = @import("../Zcu.zig"); |
| 18 | /// Deprecated. | |
| 19 | const Module = Zcu; | |
| 20 | 18 | const InternPool = @import("../InternPool.zig"); |
| 21 | 19 | const Package = @import("../Package.zig"); |
| 22 | 20 | const Air = @import("../Air.zig"); |
| ... | ... | @@ -810,7 +808,7 @@ pub const Object = struct { |
| 810 | 808 | gpa: Allocator, |
| 811 | 809 | builder: Builder, |
| 812 | 810 | |
| 813 | module: *Module, | |
| 811 | pt: Zcu.PerThread, | |
| 814 | 812 | |
| 815 | 813 | debug_compile_unit: Builder.Metadata, |
| 816 | 814 | |
| ... | ... | @@ -820,7 +818,7 @@ pub const Object = struct { |
| 820 | 818 | debug_enums: std.ArrayListUnmanaged(Builder.Metadata), |
| 821 | 819 | debug_globals: std.ArrayListUnmanaged(Builder.Metadata), |
| 822 | 820 | |
| 823 | debug_file_map: std.AutoHashMapUnmanaged(*const Module.File, Builder.Metadata), | |
| 821 | debug_file_map: std.AutoHashMapUnmanaged(*const Zcu.File, Builder.Metadata), | |
| 824 | 822 | debug_type_map: std.AutoHashMapUnmanaged(Type, Builder.Metadata), |
| 825 | 823 | |
| 826 | 824 | debug_unresolved_namespace_scopes: std.AutoArrayHashMapUnmanaged(InternPool.NamespaceIndex, Builder.Metadata), |
| ... | ... | @@ -992,7 +990,10 @@ pub const Object = struct { |
| 992 | 990 | obj.* = .{ |
| 993 | 991 | .gpa = gpa, |
| 994 | 992 | .builder = builder, |
| 995 | .module = comp.module.?, | |
| 993 | .pt = .{ | |
| 994 | .zcu = comp.module.?, | |
| 995 | .tid = .main, | |
| 996 | }, | |
| 996 | 997 | .debug_compile_unit = debug_compile_unit, |
| 997 | 998 | .debug_enums_fwd_ref = debug_enums_fwd_ref, |
| 998 | 999 | .debug_globals_fwd_ref = debug_globals_fwd_ref, |
| ... | ... | @@ -1033,7 +1034,8 @@ pub const Object = struct { |
| 1033 | 1034 | // If o.error_name_table is null, then it was not referenced by any instructions. |
| 1034 | 1035 | if (o.error_name_table == .none) return; |
| 1035 | 1036 | |
| 1036 | const mod = o.module; | |
| 1037 | const pt = o.pt; | |
| 1038 | const mod = pt.zcu; | |
| 1037 | 1039 | |
| 1038 | 1040 | const error_name_list = mod.global_error_set.keys(); |
| 1039 | 1041 | const llvm_errors = try mod.gpa.alloc(Builder.Constant, error_name_list.len); |
| ... | ... | @@ -1072,7 +1074,7 @@ pub const Object = struct { |
| 1072 | 1074 | table_variable_index.setMutability(.constant, &o.builder); |
| 1073 | 1075 | table_variable_index.setUnnamedAddr(.unnamed_addr, &o.builder); |
| 1074 | 1076 | table_variable_index.setAlignment( |
| 1075 | slice_ty.abiAlignment(mod).toLlvm(), | |
| 1077 | slice_ty.abiAlignment(pt).toLlvm(), | |
| 1076 | 1078 | &o.builder, |
| 1077 | 1079 | ); |
| 1078 | 1080 | |
| ... | ... | @@ -1083,8 +1085,7 @@ pub const Object = struct { |
| 1083 | 1085 | // If there is no such function in the module, it means the source code does not need it. |
| 1084 | 1086 | const name = o.builder.strtabStringIfExists(lt_errors_fn_name) orelse return; |
| 1085 | 1087 | const llvm_fn = o.builder.getGlobal(name) orelse return; |
| 1086 | const mod = o.module; | |
| 1087 | const errors_len = mod.global_error_set.count(); | |
| 1088 | const errors_len = o.pt.zcu.global_error_set.count(); | |
| 1088 | 1089 | |
| 1089 | 1090 | var wip = try Builder.WipFunction.init(&o.builder, .{ |
| 1090 | 1091 | .function = llvm_fn.ptrConst(&o.builder).kind.function, |
| ... | ... | @@ -1106,10 +1107,8 @@ pub const Object = struct { |
| 1106 | 1107 | } |
| 1107 | 1108 | |
| 1108 | 1109 | fn genModuleLevelAssembly(object: *Object) !void { |
| 1109 | const mod = object.module; | |
| 1110 | ||
| 1111 | 1110 | const writer = object.builder.setModuleAsm(); |
| 1112 | for (mod.global_assembly.values()) |assembly| { | |
| 1111 | for (object.pt.zcu.global_assembly.values()) |assembly| { | |
| 1113 | 1112 | try writer.print("{s}\n", .{assembly}); |
| 1114 | 1113 | } |
| 1115 | 1114 | try object.builder.finishModuleAsm(); |
| ... | ... | @@ -1131,6 +1130,9 @@ pub const Object = struct { |
| 1131 | 1130 | }; |
| 1132 | 1131 | |
| 1133 | 1132 | pub fn emit(self: *Object, options: EmitOptions) !void { |
| 1133 | const zcu = self.pt.zcu; | |
| 1134 | const comp = zcu.comp; | |
| 1135 | ||
| 1134 | 1136 | { |
| 1135 | 1137 | try self.genErrorNameTable(); |
| 1136 | 1138 | try self.genCmpLtErrorsLenFunction(); |
| ... | ... | @@ -1143,8 +1145,8 @@ pub const Object = struct { |
| 1143 | 1145 | const namespace_index = self.debug_unresolved_namespace_scopes.keys()[i]; |
| 1144 | 1146 | const fwd_ref = self.debug_unresolved_namespace_scopes.values()[i]; |
| 1145 | 1147 | |
| 1146 | const namespace = self.module.namespacePtr(namespace_index); | |
| 1147 | const debug_type = try self.lowerDebugType(namespace.getType(self.module)); | |
| 1148 | const namespace = zcu.namespacePtr(namespace_index); | |
| 1149 | const debug_type = try self.lowerDebugType(namespace.getType(zcu)); | |
| 1148 | 1150 | |
| 1149 | 1151 | self.builder.debugForwardReferenceSetType(fwd_ref, debug_type); |
| 1150 | 1152 | } |
| ... | ... | @@ -1206,12 +1208,12 @@ pub const Object = struct { |
| 1206 | 1208 | try file.writeAll(ptr[0..(bitcode.len * 4)]); |
| 1207 | 1209 | } |
| 1208 | 1210 | |
| 1209 | if (!build_options.have_llvm or !self.module.comp.config.use_lib_llvm) { | |
| 1211 | if (!build_options.have_llvm or !comp.config.use_lib_llvm) { | |
| 1210 | 1212 | log.err("emitting without libllvm not implemented", .{}); |
| 1211 | 1213 | return error.FailedToEmit; |
| 1212 | 1214 | } |
| 1213 | 1215 | |
| 1214 | initializeLLVMTarget(self.module.comp.root_mod.resolved_target.result.cpu.arch); | |
| 1216 | initializeLLVMTarget(comp.root_mod.resolved_target.result.cpu.arch); | |
| 1215 | 1217 | |
| 1216 | 1218 | const context: *llvm.Context = llvm.Context.create(); |
| 1217 | 1219 | errdefer context.dispose(); |
| ... | ... | @@ -1247,8 +1249,8 @@ pub const Object = struct { |
| 1247 | 1249 | @panic("Invalid LLVM triple"); |
| 1248 | 1250 | } |
| 1249 | 1251 | |
| 1250 | const optimize_mode = self.module.comp.root_mod.optimize_mode; | |
| 1251 | const pic = self.module.comp.root_mod.pic; | |
| 1252 | const optimize_mode = comp.root_mod.optimize_mode; | |
| 1253 | const pic = comp.root_mod.pic; | |
| 1252 | 1254 | |
| 1253 | 1255 | const opt_level: llvm.CodeGenOptLevel = if (optimize_mode == .Debug) |
| 1254 | 1256 | .None |
| ... | ... | @@ -1257,12 +1259,12 @@ pub const Object = struct { |
| 1257 | 1259 | |
| 1258 | 1260 | const reloc_mode: llvm.RelocMode = if (pic) |
| 1259 | 1261 | .PIC |
| 1260 | else if (self.module.comp.config.link_mode == .dynamic) | |
| 1262 | else if (comp.config.link_mode == .dynamic) | |
| 1261 | 1263 | llvm.RelocMode.DynamicNoPIC |
| 1262 | 1264 | else |
| 1263 | 1265 | .Static; |
| 1264 | 1266 | |
| 1265 | const code_model: llvm.CodeModel = switch (self.module.comp.root_mod.code_model) { | |
| 1267 | const code_model: llvm.CodeModel = switch (comp.root_mod.code_model) { | |
| 1266 | 1268 | .default => .Default, |
| 1267 | 1269 | .tiny => .Tiny, |
| 1268 | 1270 | .small => .Small, |
| ... | ... | @@ -1277,24 +1279,24 @@ pub const Object = struct { |
| 1277 | 1279 | var target_machine = llvm.TargetMachine.create( |
| 1278 | 1280 | target, |
| 1279 | 1281 | target_triple_sentinel, |
| 1280 | if (self.module.comp.root_mod.resolved_target.result.cpu.model.llvm_name) |s| s.ptr else null, | |
| 1281 | self.module.comp.root_mod.resolved_target.llvm_cpu_features.?, | |
| 1282 | if (comp.root_mod.resolved_target.result.cpu.model.llvm_name) |s| s.ptr else null, | |
| 1283 | comp.root_mod.resolved_target.llvm_cpu_features.?, | |
| 1282 | 1284 | opt_level, |
| 1283 | 1285 | reloc_mode, |
| 1284 | 1286 | code_model, |
| 1285 | self.module.comp.function_sections, | |
| 1286 | self.module.comp.data_sections, | |
| 1287 | comp.function_sections, | |
| 1288 | comp.data_sections, | |
| 1287 | 1289 | float_abi, |
| 1288 | if (target_util.llvmMachineAbi(self.module.comp.root_mod.resolved_target.result)) |s| s.ptr else null, | |
| 1290 | if (target_util.llvmMachineAbi(comp.root_mod.resolved_target.result)) |s| s.ptr else null, | |
| 1289 | 1291 | ); |
| 1290 | 1292 | errdefer target_machine.dispose(); |
| 1291 | 1293 | |
| 1292 | 1294 | if (pic) module.setModulePICLevel(); |
| 1293 | if (self.module.comp.config.pie) module.setModulePIELevel(); | |
| 1295 | if (comp.config.pie) module.setModulePIELevel(); | |
| 1294 | 1296 | if (code_model != .Default) module.setModuleCodeModel(code_model); |
| 1295 | 1297 | |
| 1296 | if (self.module.comp.llvm_opt_bisect_limit >= 0) { | |
| 1297 | context.setOptBisectLimit(self.module.comp.llvm_opt_bisect_limit); | |
| 1298 | if (comp.llvm_opt_bisect_limit >= 0) { | |
| 1299 | context.setOptBisectLimit(comp.llvm_opt_bisect_limit); | |
| 1298 | 1300 | } |
| 1299 | 1301 | |
| 1300 | 1302 | // Unfortunately, LLVM shits the bed when we ask for both binary and assembly. |
| ... | ... | @@ -1352,11 +1354,13 @@ pub const Object = struct { |
| 1352 | 1354 | |
| 1353 | 1355 | pub fn updateFunc( |
| 1354 | 1356 | o: *Object, |
| 1355 | zcu: *Module, | |
| 1357 | pt: Zcu.PerThread, | |
| 1356 | 1358 | func_index: InternPool.Index, |
| 1357 | 1359 | air: Air, |
| 1358 | 1360 | liveness: Liveness, |
| 1359 | 1361 | ) !void { |
| 1362 | assert(std.meta.eql(pt, o.pt)); | |
| 1363 | const zcu = pt.zcu; | |
| 1360 | 1364 | const comp = zcu.comp; |
| 1361 | 1365 | const func = zcu.funcInfo(func_index); |
| 1362 | 1366 | const decl_index = func.owner_decl; |
| ... | ... | @@ -1437,7 +1441,7 @@ pub const Object = struct { |
| 1437 | 1441 | var llvm_arg_i: u32 = 0; |
| 1438 | 1442 | |
| 1439 | 1443 | // This gets the LLVM values from the function and stores them in `dg.args`. |
| 1440 | const sret = firstParamSRet(fn_info, zcu, target); | |
| 1444 | const sret = firstParamSRet(fn_info, pt, target); | |
| 1441 | 1445 | const ret_ptr: Builder.Value = if (sret) param: { |
| 1442 | 1446 | const param = wip.arg(llvm_arg_i); |
| 1443 | 1447 | llvm_arg_i += 1; |
| ... | ... | @@ -1478,8 +1482,8 @@ pub const Object = struct { |
| 1478 | 1482 | const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[param_index]); |
| 1479 | 1483 | const param = wip.arg(llvm_arg_i); |
| 1480 | 1484 | |
| 1481 | if (isByRef(param_ty, zcu)) { | |
| 1482 | const alignment = param_ty.abiAlignment(zcu).toLlvm(); | |
| 1485 | if (isByRef(param_ty, pt)) { | |
| 1486 | const alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 1483 | 1487 | const param_llvm_ty = param.typeOfWip(&wip); |
| 1484 | 1488 | const arg_ptr = try buildAllocaInner(&wip, param_llvm_ty, alignment, target); |
| 1485 | 1489 | _ = try wip.store(.normal, param, arg_ptr, alignment); |
| ... | ... | @@ -1495,12 +1499,12 @@ pub const Object = struct { |
| 1495 | 1499 | const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]); |
| 1496 | 1500 | const param_llvm_ty = try o.lowerType(param_ty); |
| 1497 | 1501 | const param = wip.arg(llvm_arg_i); |
| 1498 | const alignment = param_ty.abiAlignment(zcu).toLlvm(); | |
| 1502 | const alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 1499 | 1503 | |
| 1500 | 1504 | try o.addByRefParamAttrs(&attributes, llvm_arg_i, alignment, it.byval_attr, param_llvm_ty); |
| 1501 | 1505 | llvm_arg_i += 1; |
| 1502 | 1506 | |
| 1503 | if (isByRef(param_ty, zcu)) { | |
| 1507 | if (isByRef(param_ty, pt)) { | |
| 1504 | 1508 | args.appendAssumeCapacity(param); |
| 1505 | 1509 | } else { |
| 1506 | 1510 | args.appendAssumeCapacity(try wip.load(.normal, param_llvm_ty, param, alignment, "")); |
| ... | ... | @@ -1510,12 +1514,12 @@ pub const Object = struct { |
| 1510 | 1514 | const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]); |
| 1511 | 1515 | const param_llvm_ty = try o.lowerType(param_ty); |
| 1512 | 1516 | const param = wip.arg(llvm_arg_i); |
| 1513 | const alignment = param_ty.abiAlignment(zcu).toLlvm(); | |
| 1517 | const alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 1514 | 1518 | |
| 1515 | 1519 | try attributes.addParamAttr(llvm_arg_i, .noundef, &o.builder); |
| 1516 | 1520 | llvm_arg_i += 1; |
| 1517 | 1521 | |
| 1518 | if (isByRef(param_ty, zcu)) { | |
| 1522 | if (isByRef(param_ty, pt)) { | |
| 1519 | 1523 | args.appendAssumeCapacity(param); |
| 1520 | 1524 | } else { |
| 1521 | 1525 | args.appendAssumeCapacity(try wip.load(.normal, param_llvm_ty, param, alignment, "")); |
| ... | ... | @@ -1528,11 +1532,11 @@ pub const Object = struct { |
| 1528 | 1532 | llvm_arg_i += 1; |
| 1529 | 1533 | |
| 1530 | 1534 | const param_llvm_ty = try o.lowerType(param_ty); |
| 1531 | const alignment = param_ty.abiAlignment(zcu).toLlvm(); | |
| 1535 | const alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 1532 | 1536 | const arg_ptr = try buildAllocaInner(&wip, param_llvm_ty, alignment, target); |
| 1533 | 1537 | _ = try wip.store(.normal, param, arg_ptr, alignment); |
| 1534 | 1538 | |
| 1535 | args.appendAssumeCapacity(if (isByRef(param_ty, zcu)) | |
| 1539 | args.appendAssumeCapacity(if (isByRef(param_ty, pt)) | |
| 1536 | 1540 | arg_ptr |
| 1537 | 1541 | else |
| 1538 | 1542 | try wip.load(.normal, param_llvm_ty, arg_ptr, alignment, "")); |
| ... | ... | @@ -1556,7 +1560,7 @@ pub const Object = struct { |
| 1556 | 1560 | const elem_align = (if (ptr_info.flags.alignment != .none) |
| 1557 | 1561 | @as(InternPool.Alignment, ptr_info.flags.alignment) |
| 1558 | 1562 | else |
| 1559 | Type.fromInterned(ptr_info.child).abiAlignment(zcu).max(.@"1")).toLlvm(); | |
| 1563 | Type.fromInterned(ptr_info.child).abiAlignment(pt).max(.@"1")).toLlvm(); | |
| 1560 | 1564 | try attributes.addParamAttr(llvm_arg_i, .{ .@"align" = elem_align }, &o.builder); |
| 1561 | 1565 | const ptr_param = wip.arg(llvm_arg_i); |
| 1562 | 1566 | llvm_arg_i += 1; |
| ... | ... | @@ -1573,7 +1577,7 @@ pub const Object = struct { |
| 1573 | 1577 | const field_types = it.types_buffer[0..it.types_len]; |
| 1574 | 1578 | const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]); |
| 1575 | 1579 | const param_llvm_ty = try o.lowerType(param_ty); |
| 1576 | const param_alignment = param_ty.abiAlignment(zcu).toLlvm(); | |
| 1580 | const param_alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 1577 | 1581 | const arg_ptr = try buildAllocaInner(&wip, param_llvm_ty, param_alignment, target); |
| 1578 | 1582 | const llvm_ty = try o.builder.structType(.normal, field_types); |
| 1579 | 1583 | for (0..field_types.len) |field_i| { |
| ... | ... | @@ -1585,7 +1589,7 @@ pub const Object = struct { |
| 1585 | 1589 | _ = try wip.store(.normal, param, field_ptr, alignment); |
| 1586 | 1590 | } |
| 1587 | 1591 | |
| 1588 | const is_by_ref = isByRef(param_ty, zcu); | |
| 1592 | const is_by_ref = isByRef(param_ty, pt); | |
| 1589 | 1593 | args.appendAssumeCapacity(if (is_by_ref) |
| 1590 | 1594 | arg_ptr |
| 1591 | 1595 | else |
| ... | ... | @@ -1603,11 +1607,11 @@ pub const Object = struct { |
| 1603 | 1607 | const param = wip.arg(llvm_arg_i); |
| 1604 | 1608 | llvm_arg_i += 1; |
| 1605 | 1609 | |
| 1606 | const alignment = param_ty.abiAlignment(zcu).toLlvm(); | |
| 1610 | const alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 1607 | 1611 | const arg_ptr = try buildAllocaInner(&wip, param_llvm_ty, alignment, target); |
| 1608 | 1612 | _ = try wip.store(.normal, param, arg_ptr, alignment); |
| 1609 | 1613 | |
| 1610 | args.appendAssumeCapacity(if (isByRef(param_ty, zcu)) | |
| 1614 | args.appendAssumeCapacity(if (isByRef(param_ty, pt)) | |
| 1611 | 1615 | arg_ptr |
| 1612 | 1616 | else |
| 1613 | 1617 | try wip.load(.normal, param_llvm_ty, arg_ptr, alignment, "")); |
| ... | ... | @@ -1618,11 +1622,11 @@ pub const Object = struct { |
| 1618 | 1622 | const param = wip.arg(llvm_arg_i); |
| 1619 | 1623 | llvm_arg_i += 1; |
| 1620 | 1624 | |
| 1621 | const alignment = param_ty.abiAlignment(zcu).toLlvm(); | |
| 1625 | const alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 1622 | 1626 | const arg_ptr = try buildAllocaInner(&wip, param_llvm_ty, alignment, target); |
| 1623 | 1627 | _ = try wip.store(.normal, param, arg_ptr, alignment); |
| 1624 | 1628 | |
| 1625 | args.appendAssumeCapacity(if (isByRef(param_ty, zcu)) | |
| 1629 | args.appendAssumeCapacity(if (isByRef(param_ty, pt)) | |
| 1626 | 1630 | arg_ptr |
| 1627 | 1631 | else |
| 1628 | 1632 | try wip.load(.normal, param_llvm_ty, arg_ptr, alignment, "")); |
| ... | ... | @@ -1700,8 +1704,9 @@ pub const Object = struct { |
| 1700 | 1704 | try fg.wip.finish(); |
| 1701 | 1705 | } |
| 1702 | 1706 | |
| 1703 | pub fn updateDecl(self: *Object, module: *Module, decl_index: InternPool.DeclIndex) !void { | |
| 1704 | const decl = module.declPtr(decl_index); | |
| 1707 | pub fn updateDecl(self: *Object, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex) !void { | |
| 1708 | assert(std.meta.eql(pt, self.pt)); | |
| 1709 | const decl = pt.zcu.declPtr(decl_index); | |
| 1705 | 1710 | var dg: DeclGen = .{ |
| 1706 | 1711 | .object = self, |
| 1707 | 1712 | .decl = decl, |
| ... | ... | @@ -1711,7 +1716,7 @@ pub const Object = struct { |
| 1711 | 1716 | dg.genDecl() catch |err| switch (err) { |
| 1712 | 1717 | error.CodegenFail => { |
| 1713 | 1718 | decl.analysis = .codegen_failure; |
| 1714 | try module.failed_analysis.put(module.gpa, InternPool.AnalUnit.wrap(.{ .decl = decl_index }), dg.err_msg.?); | |
| 1719 | try pt.zcu.failed_analysis.put(pt.zcu.gpa, InternPool.AnalUnit.wrap(.{ .decl = decl_index }), dg.err_msg.?); | |
| 1715 | 1720 | dg.err_msg = null; |
| 1716 | 1721 | return; |
| 1717 | 1722 | }, |
| ... | ... | @@ -1721,10 +1726,12 @@ pub const Object = struct { |
| 1721 | 1726 | |
| 1722 | 1727 | pub fn updateExports( |
| 1723 | 1728 | self: *Object, |
| 1724 | zcu: *Zcu, | |
| 1725 | exported: Module.Exported, | |
| 1729 | pt: Zcu.PerThread, | |
| 1730 | exported: Zcu.Exported, | |
| 1726 | 1731 | export_indices: []const u32, |
| 1727 | 1732 | ) link.File.UpdateExportsError!void { |
| 1733 | assert(std.meta.eql(pt, self.pt)); | |
| 1734 | const zcu = pt.zcu; | |
| 1728 | 1735 | const decl_index = switch (exported) { |
| 1729 | 1736 | .decl_index => |i| i, |
| 1730 | 1737 | .value => |val| return updateExportedValue(self, zcu, val, export_indices), |
| ... | ... | @@ -1748,7 +1755,7 @@ pub const Object = struct { |
| 1748 | 1755 | |
| 1749 | 1756 | fn updateExportedValue( |
| 1750 | 1757 | o: *Object, |
| 1751 | mod: *Module, | |
| 1758 | mod: *Zcu, | |
| 1752 | 1759 | exported_value: InternPool.Index, |
| 1753 | 1760 | export_indices: []const u32, |
| 1754 | 1761 | ) link.File.UpdateExportsError!void { |
| ... | ... | @@ -1783,7 +1790,7 @@ pub const Object = struct { |
| 1783 | 1790 | |
| 1784 | 1791 | fn updateExportedGlobal( |
| 1785 | 1792 | o: *Object, |
| 1786 | mod: *Module, | |
| 1793 | mod: *Zcu, | |
| 1787 | 1794 | global_index: Builder.Global.Index, |
| 1788 | 1795 | export_indices: []const u32, |
| 1789 | 1796 | ) link.File.UpdateExportsError!void { |
| ... | ... | @@ -1879,7 +1886,7 @@ pub const Object = struct { |
| 1879 | 1886 | global.delete(&self.builder); |
| 1880 | 1887 | } |
| 1881 | 1888 | |
| 1882 | fn getDebugFile(o: *Object, file: *const Module.File) Allocator.Error!Builder.Metadata { | |
| 1889 | fn getDebugFile(o: *Object, file: *const Zcu.File) Allocator.Error!Builder.Metadata { | |
| 1883 | 1890 | const gpa = o.gpa; |
| 1884 | 1891 | const gop = try o.debug_file_map.getOrPut(gpa, file); |
| 1885 | 1892 | errdefer assert(o.debug_file_map.remove(file)); |
| ... | ... | @@ -1909,7 +1916,8 @@ pub const Object = struct { |
| 1909 | 1916 | |
| 1910 | 1917 | const gpa = o.gpa; |
| 1911 | 1918 | const target = o.target; |
| 1912 | const zcu = o.module; | |
| 1919 | const pt = o.pt; | |
| 1920 | const zcu = pt.zcu; | |
| 1913 | 1921 | const ip = &zcu.intern_pool; |
| 1914 | 1922 | |
| 1915 | 1923 | if (o.debug_type_map.get(ty)) |debug_type| return debug_type; |
| ... | ... | @@ -1931,7 +1939,7 @@ pub const Object = struct { |
| 1931 | 1939 | const name = try o.allocTypeName(ty); |
| 1932 | 1940 | defer gpa.free(name); |
| 1933 | 1941 | const builder_name = try o.builder.metadataString(name); |
| 1934 | const debug_bits = ty.abiSize(zcu) * 8; // lldb cannot handle non-byte sized types | |
| 1942 | const debug_bits = ty.abiSize(pt) * 8; // lldb cannot handle non-byte sized types | |
| 1935 | 1943 | const debug_int_type = switch (info.signedness) { |
| 1936 | 1944 | .signed => try o.builder.debugSignedType(builder_name, debug_bits), |
| 1937 | 1945 | .unsigned => try o.builder.debugUnsignedType(builder_name, debug_bits), |
| ... | ... | @@ -1941,9 +1949,9 @@ pub const Object = struct { |
| 1941 | 1949 | }, |
| 1942 | 1950 | .Enum => { |
| 1943 | 1951 | const owner_decl_index = ty.getOwnerDecl(zcu); |
| 1944 | const owner_decl = o.module.declPtr(owner_decl_index); | |
| 1952 | const owner_decl = zcu.declPtr(owner_decl_index); | |
| 1945 | 1953 | |
| 1946 | if (!ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 1954 | if (!ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1947 | 1955 | const debug_enum_type = try o.makeEmptyNamespaceDebugType(owner_decl_index); |
| 1948 | 1956 | try o.debug_type_map.put(gpa, ty, debug_enum_type); |
| 1949 | 1957 | return debug_enum_type; |
| ... | ... | @@ -1961,7 +1969,7 @@ pub const Object = struct { |
| 1961 | 1969 | for (enum_type.names.get(ip), 0..) |field_name_ip, i| { |
| 1962 | 1970 | var bigint_space: Value.BigIntSpace = undefined; |
| 1963 | 1971 | const bigint = if (enum_type.values.len != 0) |
| 1964 | Value.fromInterned(enum_type.values.get(ip)[i]).toBigInt(&bigint_space, zcu) | |
| 1972 | Value.fromInterned(enum_type.values.get(ip)[i]).toBigInt(&bigint_space, pt) | |
| 1965 | 1973 | else |
| 1966 | 1974 | std.math.big.int.Mutable.init(&bigint_space.limbs, i).toConst(); |
| 1967 | 1975 | |
| ... | ... | @@ -1986,8 +1994,8 @@ pub const Object = struct { |
| 1986 | 1994 | scope, |
| 1987 | 1995 | owner_decl.typeSrcLine(zcu) + 1, // Line |
| 1988 | 1996 | try o.lowerDebugType(int_ty), |
| 1989 | ty.abiSize(zcu) * 8, | |
| 1990 | (ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8, | |
| 1997 | ty.abiSize(pt) * 8, | |
| 1998 | (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8, | |
| 1991 | 1999 | try o.builder.debugTuple(enumerators), |
| 1992 | 2000 | ); |
| 1993 | 2001 | |
| ... | ... | @@ -2027,10 +2035,10 @@ pub const Object = struct { |
| 2027 | 2035 | ptr_info.flags.is_const or |
| 2028 | 2036 | ptr_info.flags.is_volatile or |
| 2029 | 2037 | ptr_info.flags.size == .Many or ptr_info.flags.size == .C or |
| 2030 | !Type.fromInterned(ptr_info.child).hasRuntimeBitsIgnoreComptime(zcu)) | |
| 2038 | !Type.fromInterned(ptr_info.child).hasRuntimeBitsIgnoreComptime(pt)) | |
| 2031 | 2039 | { |
| 2032 | const bland_ptr_ty = try zcu.ptrType(.{ | |
| 2033 | .child = if (!Type.fromInterned(ptr_info.child).hasRuntimeBitsIgnoreComptime(zcu)) | |
| 2040 | const bland_ptr_ty = try pt.ptrType(.{ | |
| 2041 | .child = if (!Type.fromInterned(ptr_info.child).hasRuntimeBitsIgnoreComptime(pt)) | |
| 2034 | 2042 | .anyopaque_type |
| 2035 | 2043 | else |
| 2036 | 2044 | ptr_info.child, |
| ... | ... | @@ -2060,10 +2068,10 @@ pub const Object = struct { |
| 2060 | 2068 | defer gpa.free(name); |
| 2061 | 2069 | const line = 0; |
| 2062 | 2070 | |
| 2063 | const ptr_size = ptr_ty.abiSize(zcu); | |
| 2064 | const ptr_align = ptr_ty.abiAlignment(zcu); | |
| 2065 | const len_size = len_ty.abiSize(zcu); | |
| 2066 | const len_align = len_ty.abiAlignment(zcu); | |
| 2071 | const ptr_size = ptr_ty.abiSize(pt); | |
| 2072 | const ptr_align = ptr_ty.abiAlignment(pt); | |
| 2073 | const len_size = len_ty.abiSize(pt); | |
| 2074 | const len_align = len_ty.abiAlignment(pt); | |
| 2067 | 2075 | |
| 2068 | 2076 | const len_offset = len_align.forward(ptr_size); |
| 2069 | 2077 | |
| ... | ... | @@ -2095,8 +2103,8 @@ pub const Object = struct { |
| 2095 | 2103 | o.debug_compile_unit, // Scope |
| 2096 | 2104 | line, |
| 2097 | 2105 | .none, // Underlying type |
| 2098 | ty.abiSize(zcu) * 8, | |
| 2099 | (ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8, | |
| 2106 | ty.abiSize(pt) * 8, | |
| 2107 | (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8, | |
| 2100 | 2108 | try o.builder.debugTuple(&.{ |
| 2101 | 2109 | debug_ptr_type, |
| 2102 | 2110 | debug_len_type, |
| ... | ... | @@ -2124,7 +2132,7 @@ pub const Object = struct { |
| 2124 | 2132 | 0, // Line |
| 2125 | 2133 | debug_elem_ty, |
| 2126 | 2134 | target.ptrBitWidth(), |
| 2127 | (ty.ptrAlignment(zcu).toByteUnits() orelse 0) * 8, | |
| 2135 | (ty.ptrAlignment(pt).toByteUnits() orelse 0) * 8, | |
| 2128 | 2136 | 0, // Offset |
| 2129 | 2137 | ); |
| 2130 | 2138 | |
| ... | ... | @@ -2149,7 +2157,7 @@ pub const Object = struct { |
| 2149 | 2157 | const name = try o.allocTypeName(ty); |
| 2150 | 2158 | defer gpa.free(name); |
| 2151 | 2159 | const owner_decl_index = ty.getOwnerDecl(zcu); |
| 2152 | const owner_decl = o.module.declPtr(owner_decl_index); | |
| 2160 | const owner_decl = zcu.declPtr(owner_decl_index); | |
| 2153 | 2161 | const file_scope = zcu.namespacePtr(owner_decl.src_namespace).fileScope(zcu); |
| 2154 | 2162 | const debug_opaque_type = try o.builder.debugStructType( |
| 2155 | 2163 | try o.builder.metadataString(name), |
| ... | ... | @@ -2171,8 +2179,8 @@ pub const Object = struct { |
| 2171 | 2179 | .none, // Scope |
| 2172 | 2180 | 0, // Line |
| 2173 | 2181 | try o.lowerDebugType(ty.childType(zcu)), |
| 2174 | ty.abiSize(zcu) * 8, | |
| 2175 | (ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8, | |
| 2182 | ty.abiSize(pt) * 8, | |
| 2183 | (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8, | |
| 2176 | 2184 | try o.builder.debugTuple(&.{ |
| 2177 | 2185 | try o.builder.debugSubrange( |
| 2178 | 2186 | try o.builder.debugConstant(try o.builder.intConst(.i64, 0)), |
| ... | ... | @@ -2214,8 +2222,8 @@ pub const Object = struct { |
| 2214 | 2222 | .none, // Scope |
| 2215 | 2223 | 0, // Line |
| 2216 | 2224 | debug_elem_type, |
| 2217 | ty.abiSize(zcu) * 8, | |
| 2218 | (ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8, | |
| 2225 | ty.abiSize(pt) * 8, | |
| 2226 | (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8, | |
| 2219 | 2227 | try o.builder.debugTuple(&.{ |
| 2220 | 2228 | try o.builder.debugSubrange( |
| 2221 | 2229 | try o.builder.debugConstant(try o.builder.intConst(.i64, 0)), |
| ... | ... | @@ -2231,7 +2239,7 @@ pub const Object = struct { |
| 2231 | 2239 | const name = try o.allocTypeName(ty); |
| 2232 | 2240 | defer gpa.free(name); |
| 2233 | 2241 | const child_ty = ty.optionalChild(zcu); |
| 2234 | if (!child_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 2242 | if (!child_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 2235 | 2243 | const debug_bool_type = try o.builder.debugBoolType( |
| 2236 | 2244 | try o.builder.metadataString(name), |
| 2237 | 2245 | 8, |
| ... | ... | @@ -2258,10 +2266,10 @@ pub const Object = struct { |
| 2258 | 2266 | } |
| 2259 | 2267 | |
| 2260 | 2268 | const non_null_ty = Type.u8; |
| 2261 | const payload_size = child_ty.abiSize(zcu); | |
| 2262 | const payload_align = child_ty.abiAlignment(zcu); | |
| 2263 | const non_null_size = non_null_ty.abiSize(zcu); | |
| 2264 | const non_null_align = non_null_ty.abiAlignment(zcu); | |
| 2269 | const payload_size = child_ty.abiSize(pt); | |
| 2270 | const payload_align = child_ty.abiAlignment(pt); | |
| 2271 | const non_null_size = non_null_ty.abiSize(pt); | |
| 2272 | const non_null_align = non_null_ty.abiAlignment(pt); | |
| 2265 | 2273 | const non_null_offset = non_null_align.forward(payload_size); |
| 2266 | 2274 | |
| 2267 | 2275 | const debug_data_type = try o.builder.debugMemberType( |
| ... | ... | @@ -2292,8 +2300,8 @@ pub const Object = struct { |
| 2292 | 2300 | o.debug_compile_unit, // Scope |
| 2293 | 2301 | 0, // Line |
| 2294 | 2302 | .none, // Underlying type |
| 2295 | ty.abiSize(zcu) * 8, | |
| 2296 | (ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8, | |
| 2303 | ty.abiSize(pt) * 8, | |
| 2304 | (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8, | |
| 2297 | 2305 | try o.builder.debugTuple(&.{ |
| 2298 | 2306 | debug_data_type, |
| 2299 | 2307 | debug_some_type, |
| ... | ... | @@ -2310,7 +2318,7 @@ pub const Object = struct { |
| 2310 | 2318 | }, |
| 2311 | 2319 | .ErrorUnion => { |
| 2312 | 2320 | const payload_ty = ty.errorUnionPayload(zcu); |
| 2313 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 2321 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 2314 | 2322 | // TODO: Maybe remove? |
| 2315 | 2323 | const debug_error_union_type = try o.lowerDebugType(Type.anyerror); |
| 2316 | 2324 | try o.debug_type_map.put(gpa, ty, debug_error_union_type); |
| ... | ... | @@ -2320,10 +2328,10 @@ pub const Object = struct { |
| 2320 | 2328 | const name = try o.allocTypeName(ty); |
| 2321 | 2329 | defer gpa.free(name); |
| 2322 | 2330 | |
| 2323 | const error_size = Type.anyerror.abiSize(zcu); | |
| 2324 | const error_align = Type.anyerror.abiAlignment(zcu); | |
| 2325 | const payload_size = payload_ty.abiSize(zcu); | |
| 2326 | const payload_align = payload_ty.abiAlignment(zcu); | |
| 2331 | const error_size = Type.anyerror.abiSize(pt); | |
| 2332 | const error_align = Type.anyerror.abiAlignment(pt); | |
| 2333 | const payload_size = payload_ty.abiSize(pt); | |
| 2334 | const payload_align = payload_ty.abiAlignment(pt); | |
| 2327 | 2335 | |
| 2328 | 2336 | var error_index: u32 = undefined; |
| 2329 | 2337 | var payload_index: u32 = undefined; |
| ... | ... | @@ -2371,8 +2379,8 @@ pub const Object = struct { |
| 2371 | 2379 | o.debug_compile_unit, // Sope |
| 2372 | 2380 | 0, // Line |
| 2373 | 2381 | .none, // Underlying type |
| 2374 | ty.abiSize(zcu) * 8, | |
| 2375 | (ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8, | |
| 2382 | ty.abiSize(pt) * 8, | |
| 2383 | (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8, | |
| 2376 | 2384 | try o.builder.debugTuple(&fields), |
| 2377 | 2385 | ); |
| 2378 | 2386 | |
| ... | ... | @@ -2399,8 +2407,8 @@ pub const Object = struct { |
| 2399 | 2407 | const info = Type.fromInterned(backing_int_ty).intInfo(zcu); |
| 2400 | 2408 | const builder_name = try o.builder.metadataString(name); |
| 2401 | 2409 | const debug_int_type = switch (info.signedness) { |
| 2402 | .signed => try o.builder.debugSignedType(builder_name, ty.abiSize(zcu) * 8), | |
| 2403 | .unsigned => try o.builder.debugUnsignedType(builder_name, ty.abiSize(zcu) * 8), | |
| 2410 | .signed => try o.builder.debugSignedType(builder_name, ty.abiSize(pt) * 8), | |
| 2411 | .unsigned => try o.builder.debugUnsignedType(builder_name, ty.abiSize(pt) * 8), | |
| 2404 | 2412 | }; |
| 2405 | 2413 | try o.debug_type_map.put(gpa, ty, debug_int_type); |
| 2406 | 2414 | return debug_int_type; |
| ... | ... | @@ -2420,10 +2428,10 @@ pub const Object = struct { |
| 2420 | 2428 | const debug_fwd_ref = try o.builder.debugForwardReference(); |
| 2421 | 2429 | |
| 2422 | 2430 | for (tuple.types.get(ip), tuple.values.get(ip), 0..) |field_ty, field_val, i| { |
| 2423 | if (field_val != .none or !Type.fromInterned(field_ty).hasRuntimeBits(zcu)) continue; | |
| 2431 | if (field_val != .none or !Type.fromInterned(field_ty).hasRuntimeBits(pt)) continue; | |
| 2424 | 2432 | |
| 2425 | const field_size = Type.fromInterned(field_ty).abiSize(zcu); | |
| 2426 | const field_align = Type.fromInterned(field_ty).abiAlignment(zcu); | |
| 2433 | const field_size = Type.fromInterned(field_ty).abiSize(pt); | |
| 2434 | const field_align = Type.fromInterned(field_ty).abiAlignment(pt); | |
| 2427 | 2435 | const field_offset = field_align.forward(offset); |
| 2428 | 2436 | offset = field_offset + field_size; |
| 2429 | 2437 | |
| ... | ... | @@ -2451,8 +2459,8 @@ pub const Object = struct { |
| 2451 | 2459 | o.debug_compile_unit, // Scope |
| 2452 | 2460 | 0, // Line |
| 2453 | 2461 | .none, // Underlying type |
| 2454 | ty.abiSize(zcu) * 8, | |
| 2455 | (ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8, | |
| 2462 | ty.abiSize(pt) * 8, | |
| 2463 | (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8, | |
| 2456 | 2464 | try o.builder.debugTuple(fields.items), |
| 2457 | 2465 | ); |
| 2458 | 2466 | |
| ... | ... | @@ -2479,7 +2487,7 @@ pub const Object = struct { |
| 2479 | 2487 | else => {}, |
| 2480 | 2488 | } |
| 2481 | 2489 | |
| 2482 | if (!ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 2490 | if (!ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 2483 | 2491 | const owner_decl_index = ty.getOwnerDecl(zcu); |
| 2484 | 2492 | const debug_struct_type = try o.makeEmptyNamespaceDebugType(owner_decl_index); |
| 2485 | 2493 | try o.debug_type_map.put(gpa, ty, debug_struct_type); |
| ... | ... | @@ -2502,14 +2510,14 @@ pub const Object = struct { |
| 2502 | 2510 | var it = struct_type.iterateRuntimeOrder(ip); |
| 2503 | 2511 | while (it.next()) |field_index| { |
| 2504 | 2512 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 2505 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 2506 | const field_size = field_ty.abiSize(zcu); | |
| 2507 | const field_align = zcu.structFieldAlignment( | |
| 2513 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 2514 | const field_size = field_ty.abiSize(pt); | |
| 2515 | const field_align = pt.structFieldAlignment( | |
| 2508 | 2516 | struct_type.fieldAlign(ip, field_index), |
| 2509 | 2517 | field_ty, |
| 2510 | 2518 | struct_type.layout, |
| 2511 | 2519 | ); |
| 2512 | const field_offset = ty.structFieldOffset(field_index, zcu); | |
| 2520 | const field_offset = ty.structFieldOffset(field_index, pt); | |
| 2513 | 2521 | |
| 2514 | 2522 | const field_name = struct_type.fieldName(ip, field_index).unwrap() orelse |
| 2515 | 2523 | try ip.getOrPutStringFmt(gpa, "{d}", .{field_index}, .no_embedded_nulls); |
| ... | ... | @@ -2532,8 +2540,8 @@ pub const Object = struct { |
| 2532 | 2540 | o.debug_compile_unit, // Scope |
| 2533 | 2541 | 0, // Line |
| 2534 | 2542 | .none, // Underlying type |
| 2535 | ty.abiSize(zcu) * 8, | |
| 2536 | (ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8, | |
| 2543 | ty.abiSize(pt) * 8, | |
| 2544 | (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8, | |
| 2537 | 2545 | try o.builder.debugTuple(fields.items), |
| 2538 | 2546 | ); |
| 2539 | 2547 | |
| ... | ... | @@ -2553,7 +2561,7 @@ pub const Object = struct { |
| 2553 | 2561 | |
| 2554 | 2562 | const union_type = ip.loadUnionType(ty.toIntern()); |
| 2555 | 2563 | if (!union_type.haveFieldTypes(ip) or |
| 2556 | !ty.hasRuntimeBitsIgnoreComptime(zcu) or | |
| 2564 | !ty.hasRuntimeBitsIgnoreComptime(pt) or | |
| 2557 | 2565 | !union_type.haveLayout(ip)) |
| 2558 | 2566 | { |
| 2559 | 2567 | const debug_union_type = try o.makeEmptyNamespaceDebugType(owner_decl_index); |
| ... | ... | @@ -2561,7 +2569,7 @@ pub const Object = struct { |
| 2561 | 2569 | return debug_union_type; |
| 2562 | 2570 | } |
| 2563 | 2571 | |
| 2564 | const layout = zcu.getUnionLayout(union_type); | |
| 2572 | const layout = pt.getUnionLayout(union_type); | |
| 2565 | 2573 | |
| 2566 | 2574 | const debug_fwd_ref = try o.builder.debugForwardReference(); |
| 2567 | 2575 | |
| ... | ... | @@ -2575,8 +2583,8 @@ pub const Object = struct { |
| 2575 | 2583 | o.debug_compile_unit, // Scope |
| 2576 | 2584 | 0, // Line |
| 2577 | 2585 | .none, // Underlying type |
| 2578 | ty.abiSize(zcu) * 8, | |
| 2579 | (ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8, | |
| 2586 | ty.abiSize(pt) * 8, | |
| 2587 | (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8, | |
| 2580 | 2588 | try o.builder.debugTuple( |
| 2581 | 2589 | &.{try o.lowerDebugType(Type.fromInterned(union_type.enum_tag_ty))}, |
| 2582 | 2590 | ), |
| ... | ... | @@ -2603,12 +2611,12 @@ pub const Object = struct { |
| 2603 | 2611 | |
| 2604 | 2612 | for (0..tag_type.names.len) |field_index| { |
| 2605 | 2613 | const field_ty = union_type.field_types.get(ip)[field_index]; |
| 2606 | if (!Type.fromInterned(field_ty).hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 2614 | if (!Type.fromInterned(field_ty).hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 2607 | 2615 | |
| 2608 | const field_size = Type.fromInterned(field_ty).abiSize(zcu); | |
| 2616 | const field_size = Type.fromInterned(field_ty).abiSize(pt); | |
| 2609 | 2617 | const field_align: InternPool.Alignment = switch (union_type.flagsPtr(ip).layout) { |
| 2610 | 2618 | .@"packed" => .none, |
| 2611 | .auto, .@"extern" => zcu.unionFieldNormalAlignment(union_type, @intCast(field_index)), | |
| 2619 | .auto, .@"extern" => pt.unionFieldNormalAlignment(union_type, @intCast(field_index)), | |
| 2612 | 2620 | }; |
| 2613 | 2621 | |
| 2614 | 2622 | const field_name = tag_type.names.get(ip)[field_index]; |
| ... | ... | @@ -2637,8 +2645,8 @@ pub const Object = struct { |
| 2637 | 2645 | o.debug_compile_unit, // Scope |
| 2638 | 2646 | 0, // Line |
| 2639 | 2647 | .none, // Underlying type |
| 2640 | ty.abiSize(zcu) * 8, | |
| 2641 | (ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8, | |
| 2648 | ty.abiSize(pt) * 8, | |
| 2649 | (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8, | |
| 2642 | 2650 | try o.builder.debugTuple(fields.items), |
| 2643 | 2651 | ); |
| 2644 | 2652 | |
| ... | ... | @@ -2696,8 +2704,8 @@ pub const Object = struct { |
| 2696 | 2704 | o.debug_compile_unit, // Scope |
| 2697 | 2705 | 0, // Line |
| 2698 | 2706 | .none, // Underlying type |
| 2699 | ty.abiSize(zcu) * 8, | |
| 2700 | (ty.abiAlignment(zcu).toByteUnits() orelse 0) * 8, | |
| 2707 | ty.abiSize(pt) * 8, | |
| 2708 | (ty.abiAlignment(pt).toByteUnits() orelse 0) * 8, | |
| 2701 | 2709 | try o.builder.debugTuple(&full_fields), |
| 2702 | 2710 | ); |
| 2703 | 2711 | |
| ... | ... | @@ -2718,13 +2726,13 @@ pub const Object = struct { |
| 2718 | 2726 | try debug_param_types.ensureUnusedCapacity(3 + fn_info.param_types.len); |
| 2719 | 2727 | |
| 2720 | 2728 | // Return type goes first. |
| 2721 | if (Type.fromInterned(fn_info.return_type).hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 2722 | const sret = firstParamSRet(fn_info, zcu, target); | |
| 2729 | if (Type.fromInterned(fn_info.return_type).hasRuntimeBitsIgnoreComptime(pt)) { | |
| 2730 | const sret = firstParamSRet(fn_info, pt, target); | |
| 2723 | 2731 | const ret_ty = if (sret) Type.void else Type.fromInterned(fn_info.return_type); |
| 2724 | 2732 | debug_param_types.appendAssumeCapacity(try o.lowerDebugType(ret_ty)); |
| 2725 | 2733 | |
| 2726 | 2734 | if (sret) { |
| 2727 | const ptr_ty = try zcu.singleMutPtrType(Type.fromInterned(fn_info.return_type)); | |
| 2735 | const ptr_ty = try pt.singleMutPtrType(Type.fromInterned(fn_info.return_type)); | |
| 2728 | 2736 | debug_param_types.appendAssumeCapacity(try o.lowerDebugType(ptr_ty)); |
| 2729 | 2737 | } |
| 2730 | 2738 | } else { |
| ... | ... | @@ -2732,18 +2740,18 @@ pub const Object = struct { |
| 2732 | 2740 | } |
| 2733 | 2741 | |
| 2734 | 2742 | if (Type.fromInterned(fn_info.return_type).isError(zcu) and |
| 2735 | o.module.comp.config.any_error_tracing) | |
| 2743 | zcu.comp.config.any_error_tracing) | |
| 2736 | 2744 | { |
| 2737 | const ptr_ty = try zcu.singleMutPtrType(try o.getStackTraceType()); | |
| 2745 | const ptr_ty = try pt.singleMutPtrType(try o.getStackTraceType()); | |
| 2738 | 2746 | debug_param_types.appendAssumeCapacity(try o.lowerDebugType(ptr_ty)); |
| 2739 | 2747 | } |
| 2740 | 2748 | |
| 2741 | 2749 | for (0..fn_info.param_types.len) |i| { |
| 2742 | 2750 | const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[i]); |
| 2743 | if (!param_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 2751 | if (!param_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 2744 | 2752 | |
| 2745 | if (isByRef(param_ty, zcu)) { | |
| 2746 | const ptr_ty = try zcu.singleMutPtrType(param_ty); | |
| 2753 | if (isByRef(param_ty, pt)) { | |
| 2754 | const ptr_ty = try pt.singleMutPtrType(param_ty); | |
| 2747 | 2755 | debug_param_types.appendAssumeCapacity(try o.lowerDebugType(ptr_ty)); |
| 2748 | 2756 | } else { |
| 2749 | 2757 | debug_param_types.appendAssumeCapacity(try o.lowerDebugType(param_ty)); |
| ... | ... | @@ -2770,7 +2778,7 @@ pub const Object = struct { |
| 2770 | 2778 | } |
| 2771 | 2779 | |
| 2772 | 2780 | fn namespaceToDebugScope(o: *Object, namespace_index: InternPool.NamespaceIndex) !Builder.Metadata { |
| 2773 | const zcu = o.module; | |
| 2781 | const zcu = o.pt.zcu; | |
| 2774 | 2782 | const namespace = zcu.namespacePtr(namespace_index); |
| 2775 | 2783 | const file_scope = namespace.fileScope(zcu); |
| 2776 | 2784 | if (namespace.parent == .none) return try o.getDebugFile(file_scope); |
| ... | ... | @@ -2783,7 +2791,7 @@ pub const Object = struct { |
| 2783 | 2791 | } |
| 2784 | 2792 | |
| 2785 | 2793 | fn makeEmptyNamespaceDebugType(o: *Object, decl_index: InternPool.DeclIndex) !Builder.Metadata { |
| 2786 | const zcu = o.module; | |
| 2794 | const zcu = o.pt.zcu; | |
| 2787 | 2795 | const decl = zcu.declPtr(decl_index); |
| 2788 | 2796 | const file_scope = zcu.namespacePtr(decl.src_namespace).fileScope(zcu); |
| 2789 | 2797 | return o.builder.debugStructType( |
| ... | ... | @@ -2799,7 +2807,7 @@ pub const Object = struct { |
| 2799 | 2807 | } |
| 2800 | 2808 | |
| 2801 | 2809 | fn getStackTraceType(o: *Object) Allocator.Error!Type { |
| 2802 | const zcu = o.module; | |
| 2810 | const zcu = o.pt.zcu; | |
| 2803 | 2811 | |
| 2804 | 2812 | const std_mod = zcu.std_mod; |
| 2805 | 2813 | const std_file_imported = zcu.importPkg(std_mod) catch unreachable; |
| ... | ... | @@ -2807,13 +2815,13 @@ pub const Object = struct { |
| 2807 | 2815 | const builtin_str = try zcu.intern_pool.getOrPutString(zcu.gpa, "builtin", .no_embedded_nulls); |
| 2808 | 2816 | const std_file_root_decl = zcu.fileRootDecl(std_file_imported.file_index); |
| 2809 | 2817 | const std_namespace = zcu.namespacePtr(zcu.declPtr(std_file_root_decl.unwrap().?).src_namespace); |
| 2810 | const builtin_decl = std_namespace.decls.getKeyAdapted(builtin_str, Module.DeclAdapter{ .zcu = zcu }).?; | |
| 2818 | const builtin_decl = std_namespace.decls.getKeyAdapted(builtin_str, Zcu.DeclAdapter{ .zcu = zcu }).?; | |
| 2811 | 2819 | |
| 2812 | 2820 | const stack_trace_str = try zcu.intern_pool.getOrPutString(zcu.gpa, "StackTrace", .no_embedded_nulls); |
| 2813 | 2821 | // buffer is only used for int_type, `builtin` is a struct. |
| 2814 | 2822 | const builtin_ty = zcu.declPtr(builtin_decl).val.toType(); |
| 2815 | 2823 | const builtin_namespace = zcu.namespacePtrUnwrap(builtin_ty.getNamespaceIndex(zcu)).?; |
| 2816 | const stack_trace_decl_index = builtin_namespace.decls.getKeyAdapted(stack_trace_str, Module.DeclAdapter{ .zcu = zcu }).?; | |
| 2824 | const stack_trace_decl_index = builtin_namespace.decls.getKeyAdapted(stack_trace_str, Zcu.DeclAdapter{ .zcu = zcu }).?; | |
| 2817 | 2825 | const stack_trace_decl = zcu.declPtr(stack_trace_decl_index); |
| 2818 | 2826 | |
| 2819 | 2827 | // Sema should have ensured that StackTrace was analyzed. |
| ... | ... | @@ -2824,7 +2832,7 @@ pub const Object = struct { |
| 2824 | 2832 | fn allocTypeName(o: *Object, ty: Type) Allocator.Error![:0]const u8 { |
| 2825 | 2833 | var buffer = std.ArrayList(u8).init(o.gpa); |
| 2826 | 2834 | errdefer buffer.deinit(); |
| 2827 | try ty.print(buffer.writer(), o.module); | |
| 2835 | try ty.print(buffer.writer(), o.pt); | |
| 2828 | 2836 | return buffer.toOwnedSliceSentinel(0); |
| 2829 | 2837 | } |
| 2830 | 2838 | |
| ... | ... | @@ -2835,7 +2843,8 @@ pub const Object = struct { |
| 2835 | 2843 | o: *Object, |
| 2836 | 2844 | decl_index: InternPool.DeclIndex, |
| 2837 | 2845 | ) Allocator.Error!Builder.Function.Index { |
| 2838 | const zcu = o.module; | |
| 2846 | const pt = o.pt; | |
| 2847 | const zcu = pt.zcu; | |
| 2839 | 2848 | const ip = &zcu.intern_pool; |
| 2840 | 2849 | const gpa = o.gpa; |
| 2841 | 2850 | const decl = zcu.declPtr(decl_index); |
| ... | ... | @@ -2848,7 +2857,7 @@ pub const Object = struct { |
| 2848 | 2857 | assert(decl.has_tv); |
| 2849 | 2858 | const fn_info = zcu.typeToFunc(zig_fn_type).?; |
| 2850 | 2859 | const target = owner_mod.resolved_target.result; |
| 2851 | const sret = firstParamSRet(fn_info, zcu, target); | |
| 2860 | const sret = firstParamSRet(fn_info, pt, target); | |
| 2852 | 2861 | |
| 2853 | 2862 | const is_extern = decl.isExtern(zcu); |
| 2854 | 2863 | const function_index = try o.builder.addFunction( |
| ... | ... | @@ -2929,14 +2938,14 @@ pub const Object = struct { |
| 2929 | 2938 | .byval => { |
| 2930 | 2939 | const param_index = it.zig_index - 1; |
| 2931 | 2940 | const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[param_index]); |
| 2932 | if (!isByRef(param_ty, zcu)) { | |
| 2941 | if (!isByRef(param_ty, pt)) { | |
| 2933 | 2942 | try o.addByValParamAttrs(&attributes, param_ty, param_index, fn_info, it.llvm_index - 1); |
| 2934 | 2943 | } |
| 2935 | 2944 | }, |
| 2936 | 2945 | .byref => { |
| 2937 | 2946 | const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]); |
| 2938 | 2947 | const param_llvm_ty = try o.lowerType(param_ty); |
| 2939 | const alignment = param_ty.abiAlignment(zcu); | |
| 2948 | const alignment = param_ty.abiAlignment(pt); | |
| 2940 | 2949 | try o.addByRefParamAttrs(&attributes, it.llvm_index - 1, alignment.toLlvm(), it.byval_attr, param_llvm_ty); |
| 2941 | 2950 | }, |
| 2942 | 2951 | .byref_mut => try attributes.addParamAttr(it.llvm_index - 1, .noundef, &o.builder), |
| ... | ... | @@ -2964,7 +2973,7 @@ pub const Object = struct { |
| 2964 | 2973 | attributes: *Builder.FunctionAttributes.Wip, |
| 2965 | 2974 | owner_mod: *Package.Module, |
| 2966 | 2975 | ) Allocator.Error!void { |
| 2967 | const comp = o.module.comp; | |
| 2976 | const comp = o.pt.zcu.comp; | |
| 2968 | 2977 | |
| 2969 | 2978 | if (!owner_mod.red_zone) { |
| 2970 | 2979 | try attributes.addFnAttr(.noredzone, &o.builder); |
| ... | ... | @@ -3039,7 +3048,7 @@ pub const Object = struct { |
| 3039 | 3048 | } |
| 3040 | 3049 | errdefer assert(o.anon_decl_map.remove(decl_val)); |
| 3041 | 3050 | |
| 3042 | const mod = o.module; | |
| 3051 | const mod = o.pt.zcu; | |
| 3043 | 3052 | const decl_ty = mod.intern_pool.typeOf(decl_val); |
| 3044 | 3053 | |
| 3045 | 3054 | const variable_index = try o.builder.addVariable( |
| ... | ... | @@ -3065,7 +3074,7 @@ pub const Object = struct { |
| 3065 | 3074 | if (gop.found_existing) return gop.value_ptr.ptr(&o.builder).kind.variable; |
| 3066 | 3075 | errdefer assert(o.decl_map.remove(decl_index)); |
| 3067 | 3076 | |
| 3068 | const zcu = o.module; | |
| 3077 | const zcu = o.pt.zcu; | |
| 3069 | 3078 | const decl = zcu.declPtr(decl_index); |
| 3070 | 3079 | const is_extern = decl.isExtern(zcu); |
| 3071 | 3080 | |
| ... | ... | @@ -3100,11 +3109,12 @@ pub const Object = struct { |
| 3100 | 3109 | } |
| 3101 | 3110 | |
| 3102 | 3111 | fn errorIntType(o: *Object) Allocator.Error!Builder.Type { |
| 3103 | return o.builder.intType(o.module.errorSetBits()); | |
| 3112 | return o.builder.intType(o.pt.zcu.errorSetBits()); | |
| 3104 | 3113 | } |
| 3105 | 3114 | |
| 3106 | 3115 | fn lowerType(o: *Object, t: Type) Allocator.Error!Builder.Type { |
| 3107 | const mod = o.module; | |
| 3116 | const pt = o.pt; | |
| 3117 | const mod = pt.zcu; | |
| 3108 | 3118 | const target = mod.getTarget(); |
| 3109 | 3119 | const ip = &mod.intern_pool; |
| 3110 | 3120 | return switch (t.toIntern()) { |
| ... | ... | @@ -3230,7 +3240,7 @@ pub const Object = struct { |
| 3230 | 3240 | ), |
| 3231 | 3241 | .opt_type => |child_ty| { |
| 3232 | 3242 | // Must stay in sync with `opt_payload` logic in `lowerPtr`. |
| 3233 | if (!Type.fromInterned(child_ty).hasRuntimeBitsIgnoreComptime(mod)) return .i8; | |
| 3243 | if (!Type.fromInterned(child_ty).hasRuntimeBitsIgnoreComptime(pt)) return .i8; | |
| 3234 | 3244 | |
| 3235 | 3245 | const payload_ty = try o.lowerType(Type.fromInterned(child_ty)); |
| 3236 | 3246 | if (t.optionalReprIsPayload(mod)) return payload_ty; |
| ... | ... | @@ -3238,8 +3248,8 @@ pub const Object = struct { |
| 3238 | 3248 | comptime assert(optional_layout_version == 3); |
| 3239 | 3249 | var fields: [3]Builder.Type = .{ payload_ty, .i8, undefined }; |
| 3240 | 3250 | var fields_len: usize = 2; |
| 3241 | const offset = Type.fromInterned(child_ty).abiSize(mod) + 1; | |
| 3242 | const abi_size = t.abiSize(mod); | |
| 3251 | const offset = Type.fromInterned(child_ty).abiSize(pt) + 1; | |
| 3252 | const abi_size = t.abiSize(pt); | |
| 3243 | 3253 | const padding_len = abi_size - offset; |
| 3244 | 3254 | if (padding_len > 0) { |
| 3245 | 3255 | fields[2] = try o.builder.arrayType(padding_len, .i8); |
| ... | ... | @@ -3252,16 +3262,16 @@ pub const Object = struct { |
| 3252 | 3262 | // Must stay in sync with `codegen.errUnionPayloadOffset`. |
| 3253 | 3263 | // See logic in `lowerPtr`. |
| 3254 | 3264 | const error_type = try o.errorIntType(); |
| 3255 | if (!Type.fromInterned(error_union_type.payload_type).hasRuntimeBitsIgnoreComptime(mod)) | |
| 3265 | if (!Type.fromInterned(error_union_type.payload_type).hasRuntimeBitsIgnoreComptime(pt)) | |
| 3256 | 3266 | return error_type; |
| 3257 | 3267 | const payload_type = try o.lowerType(Type.fromInterned(error_union_type.payload_type)); |
| 3258 | const err_int_ty = try mod.errorIntType(); | |
| 3268 | const err_int_ty = try o.pt.errorIntType(); | |
| 3259 | 3269 | |
| 3260 | const payload_align = Type.fromInterned(error_union_type.payload_type).abiAlignment(mod); | |
| 3261 | const error_align = err_int_ty.abiAlignment(mod); | |
| 3270 | const payload_align = Type.fromInterned(error_union_type.payload_type).abiAlignment(pt); | |
| 3271 | const error_align = err_int_ty.abiAlignment(pt); | |
| 3262 | 3272 | |
| 3263 | const payload_size = Type.fromInterned(error_union_type.payload_type).abiSize(mod); | |
| 3264 | const error_size = err_int_ty.abiSize(mod); | |
| 3273 | const payload_size = Type.fromInterned(error_union_type.payload_type).abiSize(pt); | |
| 3274 | const error_size = err_int_ty.abiSize(pt); | |
| 3265 | 3275 | |
| 3266 | 3276 | var fields: [3]Builder.Type = undefined; |
| 3267 | 3277 | var fields_len: usize = 2; |
| ... | ... | @@ -3317,12 +3327,12 @@ pub const Object = struct { |
| 3317 | 3327 | var it = struct_type.iterateRuntimeOrder(ip); |
| 3318 | 3328 | while (it.next()) |field_index| { |
| 3319 | 3329 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 3320 | const field_align = mod.structFieldAlignment( | |
| 3330 | const field_align = pt.structFieldAlignment( | |
| 3321 | 3331 | struct_type.fieldAlign(ip, field_index), |
| 3322 | 3332 | field_ty, |
| 3323 | 3333 | struct_type.layout, |
| 3324 | 3334 | ); |
| 3325 | const field_ty_align = field_ty.abiAlignment(mod); | |
| 3335 | const field_ty_align = field_ty.abiAlignment(pt); | |
| 3326 | 3336 | if (field_align.compare(.lt, field_ty_align)) struct_kind = .@"packed"; |
| 3327 | 3337 | big_align = big_align.max(field_align); |
| 3328 | 3338 | const prev_offset = offset; |
| ... | ... | @@ -3334,7 +3344,7 @@ pub const Object = struct { |
| 3334 | 3344 | try o.builder.arrayType(padding_len, .i8), |
| 3335 | 3345 | ); |
| 3336 | 3346 | |
| 3337 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 3347 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3338 | 3348 | // This is a zero-bit field. If there are runtime bits after this field, |
| 3339 | 3349 | // map to the next LLVM field (which we know exists): otherwise, don't |
| 3340 | 3350 | // map the field, indicating it's at the end of the struct. |
| ... | ... | @@ -3353,7 +3363,7 @@ pub const Object = struct { |
| 3353 | 3363 | }, @intCast(llvm_field_types.items.len)); |
| 3354 | 3364 | try llvm_field_types.append(o.gpa, try o.lowerType(field_ty)); |
| 3355 | 3365 | |
| 3356 | offset += field_ty.abiSize(mod); | |
| 3366 | offset += field_ty.abiSize(pt); | |
| 3357 | 3367 | } |
| 3358 | 3368 | { |
| 3359 | 3369 | const prev_offset = offset; |
| ... | ... | @@ -3386,7 +3396,7 @@ pub const Object = struct { |
| 3386 | 3396 | var offset: u64 = 0; |
| 3387 | 3397 | var big_align: InternPool.Alignment = .none; |
| 3388 | 3398 | |
| 3389 | const struct_size = t.abiSize(mod); | |
| 3399 | const struct_size = t.abiSize(pt); | |
| 3390 | 3400 | |
| 3391 | 3401 | for ( |
| 3392 | 3402 | anon_struct_type.types.get(ip), |
| ... | ... | @@ -3395,7 +3405,7 @@ pub const Object = struct { |
| 3395 | 3405 | ) |field_ty, field_val, field_index| { |
| 3396 | 3406 | if (field_val != .none) continue; |
| 3397 | 3407 | |
| 3398 | const field_align = Type.fromInterned(field_ty).abiAlignment(mod); | |
| 3408 | const field_align = Type.fromInterned(field_ty).abiAlignment(pt); | |
| 3399 | 3409 | big_align = big_align.max(field_align); |
| 3400 | 3410 | const prev_offset = offset; |
| 3401 | 3411 | offset = field_align.forward(offset); |
| ... | ... | @@ -3405,7 +3415,7 @@ pub const Object = struct { |
| 3405 | 3415 | o.gpa, |
| 3406 | 3416 | try o.builder.arrayType(padding_len, .i8), |
| 3407 | 3417 | ); |
| 3408 | if (!Type.fromInterned(field_ty).hasRuntimeBitsIgnoreComptime(mod)) { | |
| 3418 | if (!Type.fromInterned(field_ty).hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3409 | 3419 | // This is a zero-bit field. If there are runtime bits after this field, |
| 3410 | 3420 | // map to the next LLVM field (which we know exists): otherwise, don't |
| 3411 | 3421 | // map the field, indicating it's at the end of the struct. |
| ... | ... | @@ -3423,7 +3433,7 @@ pub const Object = struct { |
| 3423 | 3433 | }, @intCast(llvm_field_types.items.len)); |
| 3424 | 3434 | try llvm_field_types.append(o.gpa, try o.lowerType(Type.fromInterned(field_ty))); |
| 3425 | 3435 | |
| 3426 | offset += Type.fromInterned(field_ty).abiSize(mod); | |
| 3436 | offset += Type.fromInterned(field_ty).abiSize(pt); | |
| 3427 | 3437 | } |
| 3428 | 3438 | { |
| 3429 | 3439 | const prev_offset = offset; |
| ... | ... | @@ -3440,10 +3450,10 @@ pub const Object = struct { |
| 3440 | 3450 | if (o.type_map.get(t.toIntern())) |value| return value; |
| 3441 | 3451 | |
| 3442 | 3452 | const union_obj = ip.loadUnionType(t.toIntern()); |
| 3443 | const layout = mod.getUnionLayout(union_obj); | |
| 3453 | const layout = pt.getUnionLayout(union_obj); | |
| 3444 | 3454 | |
| 3445 | 3455 | if (union_obj.flagsPtr(ip).layout == .@"packed") { |
| 3446 | const int_ty = try o.builder.intType(@intCast(t.bitSize(mod))); | |
| 3456 | const int_ty = try o.builder.intType(@intCast(t.bitSize(pt))); | |
| 3447 | 3457 | try o.type_map.put(o.gpa, t.toIntern(), int_ty); |
| 3448 | 3458 | return int_ty; |
| 3449 | 3459 | } |
| ... | ... | @@ -3552,18 +3562,20 @@ pub const Object = struct { |
| 3552 | 3562 | /// being a zero bit type, but it should still be lowered as an i8 in such case. |
| 3553 | 3563 | /// There are other similar cases handled here as well. |
| 3554 | 3564 | fn lowerPtrElemTy(o: *Object, elem_ty: Type) Allocator.Error!Builder.Type { |
| 3555 | const mod = o.module; | |
| 3565 | const pt = o.pt; | |
| 3566 | const mod = pt.zcu; | |
| 3556 | 3567 | const lower_elem_ty = switch (elem_ty.zigTypeTag(mod)) { |
| 3557 | 3568 | .Opaque => true, |
| 3558 | 3569 | .Fn => !mod.typeToFunc(elem_ty).?.is_generic, |
| 3559 | .Array => elem_ty.childType(mod).hasRuntimeBitsIgnoreComptime(mod), | |
| 3560 | else => elem_ty.hasRuntimeBitsIgnoreComptime(mod), | |
| 3570 | .Array => elem_ty.childType(mod).hasRuntimeBitsIgnoreComptime(pt), | |
| 3571 | else => elem_ty.hasRuntimeBitsIgnoreComptime(pt), | |
| 3561 | 3572 | }; |
| 3562 | 3573 | return if (lower_elem_ty) try o.lowerType(elem_ty) else .i8; |
| 3563 | 3574 | } |
| 3564 | 3575 | |
| 3565 | 3576 | fn lowerTypeFn(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type { |
| 3566 | const mod = o.module; | |
| 3577 | const pt = o.pt; | |
| 3578 | const mod = pt.zcu; | |
| 3567 | 3579 | const ip = &mod.intern_pool; |
| 3568 | 3580 | const target = mod.getTarget(); |
| 3569 | 3581 | const ret_ty = try lowerFnRetTy(o, fn_info); |
| ... | ... | @@ -3571,14 +3583,14 @@ pub const Object = struct { |
| 3571 | 3583 | var llvm_params = std.ArrayListUnmanaged(Builder.Type){}; |
| 3572 | 3584 | defer llvm_params.deinit(o.gpa); |
| 3573 | 3585 | |
| 3574 | if (firstParamSRet(fn_info, mod, target)) { | |
| 3586 | if (firstParamSRet(fn_info, pt, target)) { | |
| 3575 | 3587 | try llvm_params.append(o.gpa, .ptr); |
| 3576 | 3588 | } |
| 3577 | 3589 | |
| 3578 | 3590 | if (Type.fromInterned(fn_info.return_type).isError(mod) and |
| 3579 | 3591 | mod.comp.config.any_error_tracing) |
| 3580 | 3592 | { |
| 3581 | const ptr_ty = try mod.singleMutPtrType(try o.getStackTraceType()); | |
| 3593 | const ptr_ty = try pt.singleMutPtrType(try o.getStackTraceType()); | |
| 3582 | 3594 | try llvm_params.append(o.gpa, try o.lowerType(ptr_ty)); |
| 3583 | 3595 | } |
| 3584 | 3596 | |
| ... | ... | @@ -3595,7 +3607,7 @@ pub const Object = struct { |
| 3595 | 3607 | .abi_sized_int => { |
| 3596 | 3608 | const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[it.zig_index - 1]); |
| 3597 | 3609 | try llvm_params.append(o.gpa, try o.builder.intType( |
| 3598 | @intCast(param_ty.abiSize(mod) * 8), | |
| 3610 | @intCast(param_ty.abiSize(pt) * 8), | |
| 3599 | 3611 | )); |
| 3600 | 3612 | }, |
| 3601 | 3613 | .slice => { |
| ... | ... | @@ -3633,7 +3645,8 @@ pub const Object = struct { |
| 3633 | 3645 | } |
| 3634 | 3646 | |
| 3635 | 3647 | fn lowerValueToInt(o: *Object, llvm_int_ty: Builder.Type, arg_val: InternPool.Index) Error!Builder.Constant { |
| 3636 | const mod = o.module; | |
| 3648 | const pt = o.pt; | |
| 3649 | const mod = pt.zcu; | |
| 3637 | 3650 | const ip = &mod.intern_pool; |
| 3638 | 3651 | const target = mod.getTarget(); |
| 3639 | 3652 | |
| ... | ... | @@ -3666,15 +3679,15 @@ pub const Object = struct { |
| 3666 | 3679 | var running_int = try o.builder.intConst(llvm_int_ty, 0); |
| 3667 | 3680 | var running_bits: u16 = 0; |
| 3668 | 3681 | for (struct_type.field_types.get(ip), 0..) |field_ty, field_index| { |
| 3669 | if (!Type.fromInterned(field_ty).hasRuntimeBitsIgnoreComptime(mod)) continue; | |
| 3682 | if (!Type.fromInterned(field_ty).hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 3670 | 3683 | |
| 3671 | 3684 | const shift_rhs = try o.builder.intConst(llvm_int_ty, running_bits); |
| 3672 | const field_val = try o.lowerValueToInt(llvm_int_ty, (try val.fieldValue(mod, field_index)).toIntern()); | |
| 3685 | const field_val = try o.lowerValueToInt(llvm_int_ty, (try val.fieldValue(pt, field_index)).toIntern()); | |
| 3673 | 3686 | const shifted = try o.builder.binConst(.shl, field_val, shift_rhs); |
| 3674 | 3687 | |
| 3675 | 3688 | running_int = try o.builder.binConst(.xor, running_int, shifted); |
| 3676 | 3689 | |
| 3677 | const ty_bit_size: u16 = @intCast(Type.fromInterned(field_ty).bitSize(mod)); | |
| 3690 | const ty_bit_size: u16 = @intCast(Type.fromInterned(field_ty).bitSize(pt)); | |
| 3678 | 3691 | running_bits += ty_bit_size; |
| 3679 | 3692 | } |
| 3680 | 3693 | return running_int; |
| ... | ... | @@ -3683,7 +3696,7 @@ pub const Object = struct { |
| 3683 | 3696 | else => unreachable, |
| 3684 | 3697 | }, |
| 3685 | 3698 | .un => |un| { |
| 3686 | const layout = ty.unionGetLayout(mod); | |
| 3699 | const layout = ty.unionGetLayout(pt); | |
| 3687 | 3700 | if (layout.payload_size == 0) return o.lowerValue(un.tag); |
| 3688 | 3701 | |
| 3689 | 3702 | const union_obj = mod.typeToUnion(ty).?; |
| ... | ... | @@ -3701,7 +3714,7 @@ pub const Object = struct { |
| 3701 | 3714 | } |
| 3702 | 3715 | const field_index = mod.unionTagFieldIndex(union_obj, Value.fromInterned(un.tag)).?; |
| 3703 | 3716 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 3704 | if (!field_ty.hasRuntimeBits(mod)) return o.builder.intConst(llvm_int_ty, 0); | |
| 3717 | if (!field_ty.hasRuntimeBits(pt)) return o.builder.intConst(llvm_int_ty, 0); | |
| 3705 | 3718 | return o.lowerValueToInt(llvm_int_ty, un.val); |
| 3706 | 3719 | }, |
| 3707 | 3720 | .simple_value => |simple_value| switch (simple_value) { |
| ... | ... | @@ -3715,7 +3728,7 @@ pub const Object = struct { |
| 3715 | 3728 | .opt => {}, // pointer like optional expected |
| 3716 | 3729 | else => unreachable, |
| 3717 | 3730 | } |
| 3718 | const bits = ty.bitSize(mod); | |
| 3731 | const bits = ty.bitSize(pt); | |
| 3719 | 3732 | const bytes: usize = @intCast(std.mem.alignForward(u64, bits, 8) / 8); |
| 3720 | 3733 | |
| 3721 | 3734 | var stack = std.heap.stackFallback(32, o.gpa); |
| ... | ... | @@ -3729,12 +3742,7 @@ pub const Object = struct { |
| 3729 | 3742 | defer allocator.free(limbs); |
| 3730 | 3743 | @memset(limbs, 0); |
| 3731 | 3744 | |
| 3732 | val.writeToPackedMemory( | |
| 3733 | ty, | |
| 3734 | mod, | |
| 3735 | std.mem.sliceAsBytes(limbs)[0..bytes], | |
| 3736 | 0, | |
| 3737 | ) catch unreachable; | |
| 3745 | val.writeToPackedMemory(ty, pt, std.mem.sliceAsBytes(limbs)[0..bytes], 0) catch unreachable; | |
| 3738 | 3746 | |
| 3739 | 3747 | if (builtin.target.cpu.arch.endian() == .little) { |
| 3740 | 3748 | if (target.cpu.arch.endian() == .big) |
| ... | ... | @@ -3752,7 +3760,8 @@ pub const Object = struct { |
| 3752 | 3760 | } |
| 3753 | 3761 | |
| 3754 | 3762 | fn lowerValue(o: *Object, arg_val: InternPool.Index) Error!Builder.Constant { |
| 3755 | const mod = o.module; | |
| 3763 | const pt = o.pt; | |
| 3764 | const mod = pt.zcu; | |
| 3756 | 3765 | const ip = &mod.intern_pool; |
| 3757 | 3766 | const target = mod.getTarget(); |
| 3758 | 3767 | |
| ... | ... | @@ -3811,7 +3820,7 @@ pub const Object = struct { |
| 3811 | 3820 | }, |
| 3812 | 3821 | .int => { |
| 3813 | 3822 | var bigint_space: Value.BigIntSpace = undefined; |
| 3814 | const bigint = val.toBigInt(&bigint_space, mod); | |
| 3823 | const bigint = val.toBigInt(&bigint_space, pt); | |
| 3815 | 3824 | return lowerBigInt(o, ty, bigint); |
| 3816 | 3825 | }, |
| 3817 | 3826 | .err => |err| { |
| ... | ... | @@ -3821,24 +3830,24 @@ pub const Object = struct { |
| 3821 | 3830 | }, |
| 3822 | 3831 | .error_union => |error_union| { |
| 3823 | 3832 | const err_val = switch (error_union.val) { |
| 3824 | .err_name => |err_name| try mod.intern(.{ .err = .{ | |
| 3833 | .err_name => |err_name| try pt.intern(.{ .err = .{ | |
| 3825 | 3834 | .ty = ty.errorUnionSet(mod).toIntern(), |
| 3826 | 3835 | .name = err_name, |
| 3827 | 3836 | } }), |
| 3828 | .payload => (try mod.intValue(try mod.errorIntType(), 0)).toIntern(), | |
| 3837 | .payload => (try pt.intValue(try pt.errorIntType(), 0)).toIntern(), | |
| 3829 | 3838 | }; |
| 3830 | const err_int_ty = try mod.errorIntType(); | |
| 3839 | const err_int_ty = try pt.errorIntType(); | |
| 3831 | 3840 | const payload_type = ty.errorUnionPayload(mod); |
| 3832 | if (!payload_type.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 3841 | if (!payload_type.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3833 | 3842 | // We use the error type directly as the type. |
| 3834 | 3843 | return o.lowerValue(err_val); |
| 3835 | 3844 | } |
| 3836 | 3845 | |
| 3837 | const payload_align = payload_type.abiAlignment(mod); | |
| 3838 | const error_align = err_int_ty.abiAlignment(mod); | |
| 3846 | const payload_align = payload_type.abiAlignment(pt); | |
| 3847 | const error_align = err_int_ty.abiAlignment(pt); | |
| 3839 | 3848 | const llvm_error_value = try o.lowerValue(err_val); |
| 3840 | 3849 | const llvm_payload_value = try o.lowerValue(switch (error_union.val) { |
| 3841 | .err_name => try mod.intern(.{ .undef = payload_type.toIntern() }), | |
| 3850 | .err_name => try pt.intern(.{ .undef = payload_type.toIntern() }), | |
| 3842 | 3851 | .payload => |payload| payload, |
| 3843 | 3852 | }); |
| 3844 | 3853 | |
| ... | ... | @@ -3869,16 +3878,16 @@ pub const Object = struct { |
| 3869 | 3878 | .enum_tag => |enum_tag| o.lowerValue(enum_tag.int), |
| 3870 | 3879 | .float => switch (ty.floatBits(target)) { |
| 3871 | 3880 | 16 => if (backendSupportsF16(target)) |
| 3872 | try o.builder.halfConst(val.toFloat(f16, mod)) | |
| 3881 | try o.builder.halfConst(val.toFloat(f16, pt)) | |
| 3873 | 3882 | else |
| 3874 | try o.builder.intConst(.i16, @as(i16, @bitCast(val.toFloat(f16, mod)))), | |
| 3875 | 32 => try o.builder.floatConst(val.toFloat(f32, mod)), | |
| 3876 | 64 => try o.builder.doubleConst(val.toFloat(f64, mod)), | |
| 3883 | try o.builder.intConst(.i16, @as(i16, @bitCast(val.toFloat(f16, pt)))), | |
| 3884 | 32 => try o.builder.floatConst(val.toFloat(f32, pt)), | |
| 3885 | 64 => try o.builder.doubleConst(val.toFloat(f64, pt)), | |
| 3877 | 3886 | 80 => if (backendSupportsF80(target)) |
| 3878 | try o.builder.x86_fp80Const(val.toFloat(f80, mod)) | |
| 3887 | try o.builder.x86_fp80Const(val.toFloat(f80, pt)) | |
| 3879 | 3888 | else |
| 3880 | try o.builder.intConst(.i80, @as(i80, @bitCast(val.toFloat(f80, mod)))), | |
| 3881 | 128 => try o.builder.fp128Const(val.toFloat(f128, mod)), | |
| 3889 | try o.builder.intConst(.i80, @as(i80, @bitCast(val.toFloat(f80, pt)))), | |
| 3890 | 128 => try o.builder.fp128Const(val.toFloat(f128, pt)), | |
| 3882 | 3891 | else => unreachable, |
| 3883 | 3892 | }, |
| 3884 | 3893 | .ptr => try o.lowerPtr(arg_val, 0), |
| ... | ... | @@ -3891,7 +3900,7 @@ pub const Object = struct { |
| 3891 | 3900 | const payload_ty = ty.optionalChild(mod); |
| 3892 | 3901 | |
| 3893 | 3902 | const non_null_bit = try o.builder.intConst(.i8, @intFromBool(opt.val != .none)); |
| 3894 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 3903 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 3895 | 3904 | return non_null_bit; |
| 3896 | 3905 | } |
| 3897 | 3906 | const llvm_ty = try o.lowerType(ty); |
| ... | ... | @@ -3909,7 +3918,7 @@ pub const Object = struct { |
| 3909 | 3918 | var fields: [3]Builder.Type = undefined; |
| 3910 | 3919 | var vals: [3]Builder.Constant = undefined; |
| 3911 | 3920 | vals[0] = try o.lowerValue(switch (opt.val) { |
| 3912 | .none => try mod.intern(.{ .undef = payload_ty.toIntern() }), | |
| 3921 | .none => try pt.intern(.{ .undef = payload_ty.toIntern() }), | |
| 3913 | 3922 | else => |payload| payload, |
| 3914 | 3923 | }); |
| 3915 | 3924 | vals[1] = non_null_bit; |
| ... | ... | @@ -4058,9 +4067,9 @@ pub const Object = struct { |
| 4058 | 4067 | 0.., |
| 4059 | 4068 | ) |field_ty, field_val, field_index| { |
| 4060 | 4069 | if (field_val != .none) continue; |
| 4061 | if (!Type.fromInterned(field_ty).hasRuntimeBitsIgnoreComptime(mod)) continue; | |
| 4070 | if (!Type.fromInterned(field_ty).hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 4062 | 4071 | |
| 4063 | const field_align = Type.fromInterned(field_ty).abiAlignment(mod); | |
| 4072 | const field_align = Type.fromInterned(field_ty).abiAlignment(pt); | |
| 4064 | 4073 | big_align = big_align.max(field_align); |
| 4065 | 4074 | const prev_offset = offset; |
| 4066 | 4075 | offset = field_align.forward(offset); |
| ... | ... | @@ -4076,13 +4085,13 @@ pub const Object = struct { |
| 4076 | 4085 | } |
| 4077 | 4086 | |
| 4078 | 4087 | vals[llvm_index] = |
| 4079 | try o.lowerValue((try val.fieldValue(mod, field_index)).toIntern()); | |
| 4088 | try o.lowerValue((try val.fieldValue(pt, field_index)).toIntern()); | |
| 4080 | 4089 | fields[llvm_index] = vals[llvm_index].typeOf(&o.builder); |
| 4081 | 4090 | if (fields[llvm_index] != struct_ty.structFields(&o.builder)[llvm_index]) |
| 4082 | 4091 | need_unnamed = true; |
| 4083 | 4092 | llvm_index += 1; |
| 4084 | 4093 | |
| 4085 | offset += Type.fromInterned(field_ty).abiSize(mod); | |
| 4094 | offset += Type.fromInterned(field_ty).abiSize(pt); | |
| 4086 | 4095 | } |
| 4087 | 4096 | { |
| 4088 | 4097 | const prev_offset = offset; |
| ... | ... | @@ -4109,7 +4118,7 @@ pub const Object = struct { |
| 4109 | 4118 | if (struct_type.layout == .@"packed") { |
| 4110 | 4119 | comptime assert(Type.packed_struct_layout_version == 2); |
| 4111 | 4120 | |
| 4112 | const bits = ty.bitSize(mod); | |
| 4121 | const bits = ty.bitSize(pt); | |
| 4113 | 4122 | const llvm_int_ty = try o.builder.intType(@intCast(bits)); |
| 4114 | 4123 | |
| 4115 | 4124 | return o.lowerValueToInt(llvm_int_ty, arg_val); |
| ... | ... | @@ -4138,7 +4147,7 @@ pub const Object = struct { |
| 4138 | 4147 | var field_it = struct_type.iterateRuntimeOrder(ip); |
| 4139 | 4148 | while (field_it.next()) |field_index| { |
| 4140 | 4149 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 4141 | const field_align = mod.structFieldAlignment( | |
| 4150 | const field_align = pt.structFieldAlignment( | |
| 4142 | 4151 | struct_type.fieldAlign(ip, field_index), |
| 4143 | 4152 | field_ty, |
| 4144 | 4153 | struct_type.layout, |
| ... | ... | @@ -4158,20 +4167,20 @@ pub const Object = struct { |
| 4158 | 4167 | llvm_index += 1; |
| 4159 | 4168 | } |
| 4160 | 4169 | |
| 4161 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4170 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4162 | 4171 | // This is a zero-bit field - we only needed it for the alignment. |
| 4163 | 4172 | continue; |
| 4164 | 4173 | } |
| 4165 | 4174 | |
| 4166 | 4175 | vals[llvm_index] = try o.lowerValue( |
| 4167 | (try val.fieldValue(mod, field_index)).toIntern(), | |
| 4176 | (try val.fieldValue(pt, field_index)).toIntern(), | |
| 4168 | 4177 | ); |
| 4169 | 4178 | fields[llvm_index] = vals[llvm_index].typeOf(&o.builder); |
| 4170 | 4179 | if (fields[llvm_index] != struct_ty.structFields(&o.builder)[llvm_index]) |
| 4171 | 4180 | need_unnamed = true; |
| 4172 | 4181 | llvm_index += 1; |
| 4173 | 4182 | |
| 4174 | offset += field_ty.abiSize(mod); | |
| 4183 | offset += field_ty.abiSize(pt); | |
| 4175 | 4184 | } |
| 4176 | 4185 | { |
| 4177 | 4186 | const prev_offset = offset; |
| ... | ... | @@ -4195,7 +4204,7 @@ pub const Object = struct { |
| 4195 | 4204 | }, |
| 4196 | 4205 | .un => |un| { |
| 4197 | 4206 | const union_ty = try o.lowerType(ty); |
| 4198 | const layout = ty.unionGetLayout(mod); | |
| 4207 | const layout = ty.unionGetLayout(pt); | |
| 4199 | 4208 | if (layout.payload_size == 0) return o.lowerValue(un.tag); |
| 4200 | 4209 | |
| 4201 | 4210 | const union_obj = mod.typeToUnion(ty).?; |
| ... | ... | @@ -4206,8 +4215,8 @@ pub const Object = struct { |
| 4206 | 4215 | const field_index = mod.unionTagFieldIndex(union_obj, Value.fromInterned(un.tag)).?; |
| 4207 | 4216 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[field_index]); |
| 4208 | 4217 | if (container_layout == .@"packed") { |
| 4209 | if (!field_ty.hasRuntimeBits(mod)) return o.builder.intConst(union_ty, 0); | |
| 4210 | const bits = ty.bitSize(mod); | |
| 4218 | if (!field_ty.hasRuntimeBits(pt)) return o.builder.intConst(union_ty, 0); | |
| 4219 | const bits = ty.bitSize(pt); | |
| 4211 | 4220 | const llvm_int_ty = try o.builder.intType(@intCast(bits)); |
| 4212 | 4221 | |
| 4213 | 4222 | return o.lowerValueToInt(llvm_int_ty, arg_val); |
| ... | ... | @@ -4219,7 +4228,7 @@ pub const Object = struct { |
| 4219 | 4228 | // must pointer cast to the expected type before accessing the union. |
| 4220 | 4229 | need_unnamed = layout.most_aligned_field != field_index; |
| 4221 | 4230 | |
| 4222 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4231 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4223 | 4232 | const padding_len = layout.payload_size; |
| 4224 | 4233 | break :p try o.builder.undefConst(try o.builder.arrayType(padding_len, .i8)); |
| 4225 | 4234 | } |
| ... | ... | @@ -4228,7 +4237,7 @@ pub const Object = struct { |
| 4228 | 4237 | if (payload_ty != union_ty.structFields(&o.builder)[ |
| 4229 | 4238 | @intFromBool(layout.tag_align.compare(.gte, layout.payload_align)) |
| 4230 | 4239 | ]) need_unnamed = true; |
| 4231 | const field_size = field_ty.abiSize(mod); | |
| 4240 | const field_size = field_ty.abiSize(pt); | |
| 4232 | 4241 | if (field_size == layout.payload_size) break :p payload; |
| 4233 | 4242 | const padding_len = layout.payload_size - field_size; |
| 4234 | 4243 | const padding_ty = try o.builder.arrayType(padding_len, .i8); |
| ... | ... | @@ -4239,7 +4248,7 @@ pub const Object = struct { |
| 4239 | 4248 | } else p: { |
| 4240 | 4249 | assert(layout.tag_size == 0); |
| 4241 | 4250 | if (container_layout == .@"packed") { |
| 4242 | const bits = ty.bitSize(mod); | |
| 4251 | const bits = ty.bitSize(pt); | |
| 4243 | 4252 | const llvm_int_ty = try o.builder.intType(@intCast(bits)); |
| 4244 | 4253 | |
| 4245 | 4254 | return o.lowerValueToInt(llvm_int_ty, arg_val); |
| ... | ... | @@ -4286,7 +4295,7 @@ pub const Object = struct { |
| 4286 | 4295 | ty: Type, |
| 4287 | 4296 | bigint: std.math.big.int.Const, |
| 4288 | 4297 | ) Allocator.Error!Builder.Constant { |
| 4289 | const mod = o.module; | |
| 4298 | const mod = o.pt.zcu; | |
| 4290 | 4299 | return o.builder.bigIntConst(try o.builder.intType(ty.intInfo(mod).bits), bigint); |
| 4291 | 4300 | } |
| 4292 | 4301 | |
| ... | ... | @@ -4295,7 +4304,8 @@ pub const Object = struct { |
| 4295 | 4304 | ptr_val: InternPool.Index, |
| 4296 | 4305 | prev_offset: u64, |
| 4297 | 4306 | ) Error!Builder.Constant { |
| 4298 | const zcu = o.module; | |
| 4307 | const pt = o.pt; | |
| 4308 | const zcu = pt.zcu; | |
| 4299 | 4309 | const ptr = zcu.intern_pool.indexToKey(ptr_val).ptr; |
| 4300 | 4310 | const offset: u64 = prev_offset + ptr.byte_offset; |
| 4301 | 4311 | return switch (ptr.base_addr) { |
| ... | ... | @@ -4320,7 +4330,7 @@ pub const Object = struct { |
| 4320 | 4330 | eu_ptr, |
| 4321 | 4331 | offset + @import("../codegen.zig").errUnionPayloadOffset( |
| 4322 | 4332 | Value.fromInterned(eu_ptr).typeOf(zcu).childType(zcu), |
| 4323 | zcu, | |
| 4333 | pt, | |
| 4324 | 4334 | ), |
| 4325 | 4335 | ), |
| 4326 | 4336 | .opt_payload => |opt_ptr| try o.lowerPtr(opt_ptr, offset), |
| ... | ... | @@ -4336,7 +4346,7 @@ pub const Object = struct { |
| 4336 | 4346 | }; |
| 4337 | 4347 | }, |
| 4338 | 4348 | .Struct, .Union => switch (agg_ty.containerLayout(zcu)) { |
| 4339 | .auto => agg_ty.structFieldOffset(@intCast(field.index), zcu), | |
| 4349 | .auto => agg_ty.structFieldOffset(@intCast(field.index), pt), | |
| 4340 | 4350 | .@"extern", .@"packed" => unreachable, |
| 4341 | 4351 | }, |
| 4342 | 4352 | else => unreachable, |
| ... | ... | @@ -4353,7 +4363,8 @@ pub const Object = struct { |
| 4353 | 4363 | o: *Object, |
| 4354 | 4364 | anon_decl: InternPool.Key.Ptr.BaseAddr.AnonDecl, |
| 4355 | 4365 | ) Error!Builder.Constant { |
| 4356 | const mod = o.module; | |
| 4366 | const pt = o.pt; | |
| 4367 | const mod = pt.zcu; | |
| 4357 | 4368 | const ip = &mod.intern_pool; |
| 4358 | 4369 | const decl_val = anon_decl.val; |
| 4359 | 4370 | const decl_ty = Type.fromInterned(ip.typeOf(decl_val)); |
| ... | ... | @@ -4370,14 +4381,14 @@ pub const Object = struct { |
| 4370 | 4381 | const ptr_ty = Type.fromInterned(anon_decl.orig_ty); |
| 4371 | 4382 | |
| 4372 | 4383 | const is_fn_body = decl_ty.zigTypeTag(mod) == .Fn; |
| 4373 | if ((!is_fn_body and !decl_ty.hasRuntimeBits(mod)) or | |
| 4384 | if ((!is_fn_body and !decl_ty.hasRuntimeBits(pt)) or | |
| 4374 | 4385 | (is_fn_body and mod.typeToFunc(decl_ty).?.is_generic)) return o.lowerPtrToVoid(ptr_ty); |
| 4375 | 4386 | |
| 4376 | 4387 | if (is_fn_body) |
| 4377 | 4388 | @panic("TODO"); |
| 4378 | 4389 | |
| 4379 | 4390 | const llvm_addr_space = toLlvmAddressSpace(ptr_ty.ptrAddressSpace(mod), target); |
| 4380 | const alignment = ptr_ty.ptrAlignment(mod); | |
| 4391 | const alignment = ptr_ty.ptrAlignment(pt); | |
| 4381 | 4392 | const llvm_global = (try o.resolveGlobalAnonDecl(decl_val, llvm_addr_space, alignment)).ptrConst(&o.builder).global; |
| 4382 | 4393 | |
| 4383 | 4394 | const llvm_val = try o.builder.convConst( |
| ... | ... | @@ -4389,7 +4400,8 @@ pub const Object = struct { |
| 4389 | 4400 | } |
| 4390 | 4401 | |
| 4391 | 4402 | fn lowerDeclRefValue(o: *Object, decl_index: InternPool.DeclIndex) Allocator.Error!Builder.Constant { |
| 4392 | const mod = o.module; | |
| 4403 | const pt = o.pt; | |
| 4404 | const mod = pt.zcu; | |
| 4393 | 4405 | |
| 4394 | 4406 | // In the case of something like: |
| 4395 | 4407 | // fn foo() void {} |
| ... | ... | @@ -4408,10 +4420,10 @@ pub const Object = struct { |
| 4408 | 4420 | } |
| 4409 | 4421 | |
| 4410 | 4422 | const decl_ty = decl.typeOf(mod); |
| 4411 | const ptr_ty = try decl.declPtrType(mod); | |
| 4423 | const ptr_ty = try decl.declPtrType(pt); | |
| 4412 | 4424 | |
| 4413 | 4425 | const is_fn_body = decl_ty.zigTypeTag(mod) == .Fn; |
| 4414 | if ((!is_fn_body and !decl_ty.hasRuntimeBits(mod)) or | |
| 4426 | if ((!is_fn_body and !decl_ty.hasRuntimeBits(pt)) or | |
| 4415 | 4427 | (is_fn_body and mod.typeToFunc(decl_ty).?.is_generic)) |
| 4416 | 4428 | { |
| 4417 | 4429 | return o.lowerPtrToVoid(ptr_ty); |
| ... | ... | @@ -4431,7 +4443,7 @@ pub const Object = struct { |
| 4431 | 4443 | } |
| 4432 | 4444 | |
| 4433 | 4445 | fn lowerPtrToVoid(o: *Object, ptr_ty: Type) Allocator.Error!Builder.Constant { |
| 4434 | const mod = o.module; | |
| 4446 | const mod = o.pt.zcu; | |
| 4435 | 4447 | // Even though we are pointing at something which has zero bits (e.g. `void`), |
| 4436 | 4448 | // Pointers are defined to have bits. So we must return something here. |
| 4437 | 4449 | // The value cannot be undefined, because we use the `nonnull` annotation |
| ... | ... | @@ -4459,20 +4471,21 @@ pub const Object = struct { |
| 4459 | 4471 | /// RMW exchange of floating-point values is bitcasted to same-sized integer |
| 4460 | 4472 | /// types to work around a LLVM deficiency when targeting ARM/AArch64. |
| 4461 | 4473 | fn getAtomicAbiType(o: *Object, ty: Type, is_rmw_xchg: bool) Allocator.Error!Builder.Type { |
| 4462 | const mod = o.module; | |
| 4474 | const pt = o.pt; | |
| 4475 | const mod = pt.zcu; | |
| 4463 | 4476 | const int_ty = switch (ty.zigTypeTag(mod)) { |
| 4464 | 4477 | .Int => ty, |
| 4465 | 4478 | .Enum => ty.intTagType(mod), |
| 4466 | 4479 | .Float => { |
| 4467 | 4480 | if (!is_rmw_xchg) return .none; |
| 4468 | return o.builder.intType(@intCast(ty.abiSize(mod) * 8)); | |
| 4481 | return o.builder.intType(@intCast(ty.abiSize(pt) * 8)); | |
| 4469 | 4482 | }, |
| 4470 | 4483 | .Bool => return .i8, |
| 4471 | 4484 | else => return .none, |
| 4472 | 4485 | }; |
| 4473 | 4486 | const bit_count = int_ty.intInfo(mod).bits; |
| 4474 | 4487 | if (!std.math.isPowerOfTwo(bit_count) or (bit_count % 8) != 0) { |
| 4475 | return o.builder.intType(@intCast(int_ty.abiSize(mod) * 8)); | |
| 4488 | return o.builder.intType(@intCast(int_ty.abiSize(pt) * 8)); | |
| 4476 | 4489 | } else { |
| 4477 | 4490 | return .none; |
| 4478 | 4491 | } |
| ... | ... | @@ -4486,7 +4499,8 @@ pub const Object = struct { |
| 4486 | 4499 | fn_info: InternPool.Key.FuncType, |
| 4487 | 4500 | llvm_arg_i: u32, |
| 4488 | 4501 | ) Allocator.Error!void { |
| 4489 | const mod = o.module; | |
| 4502 | const pt = o.pt; | |
| 4503 | const mod = pt.zcu; | |
| 4490 | 4504 | if (param_ty.isPtrAtRuntime(mod)) { |
| 4491 | 4505 | const ptr_info = param_ty.ptrInfo(mod); |
| 4492 | 4506 | if (math.cast(u5, param_index)) |i| { |
| ... | ... | @@ -4507,7 +4521,7 @@ pub const Object = struct { |
| 4507 | 4521 | const elem_align = if (ptr_info.flags.alignment != .none) |
| 4508 | 4522 | ptr_info.flags.alignment |
| 4509 | 4523 | else |
| 4510 | Type.fromInterned(ptr_info.child).abiAlignment(mod).max(.@"1"); | |
| 4524 | Type.fromInterned(ptr_info.child).abiAlignment(pt).max(.@"1"); | |
| 4511 | 4525 | try attributes.addParamAttr(llvm_arg_i, .{ .@"align" = elem_align.toLlvm() }, &o.builder); |
| 4512 | 4526 | } else if (ccAbiPromoteInt(fn_info.cc, mod, param_ty)) |s| switch (s) { |
| 4513 | 4527 | .signed => try attributes.addParamAttr(llvm_arg_i, .signext, &o.builder), |
| ... | ... | @@ -4540,7 +4554,7 @@ pub const Object = struct { |
| 4540 | 4554 | const name = try o.builder.strtabString(lt_errors_fn_name); |
| 4541 | 4555 | if (o.builder.getGlobal(name)) |llvm_fn| return llvm_fn.ptrConst(&o.builder).kind.function; |
| 4542 | 4556 | |
| 4543 | const zcu = o.module; | |
| 4557 | const zcu = o.pt.zcu; | |
| 4544 | 4558 | const target = zcu.root_mod.resolved_target.result; |
| 4545 | 4559 | const function_index = try o.builder.addFunction( |
| 4546 | 4560 | try o.builder.fnType(.i1, &.{try o.errorIntType()}, .normal), |
| ... | ... | @@ -4559,7 +4573,8 @@ pub const Object = struct { |
| 4559 | 4573 | } |
| 4560 | 4574 | |
| 4561 | 4575 | fn getEnumTagNameFunction(o: *Object, enum_ty: Type) !Builder.Function.Index { |
| 4562 | const zcu = o.module; | |
| 4576 | const pt = o.pt; | |
| 4577 | const zcu = pt.zcu; | |
| 4563 | 4578 | const ip = &zcu.intern_pool; |
| 4564 | 4579 | const enum_type = ip.loadEnumType(enum_ty.toIntern()); |
| 4565 | 4580 | |
| ... | ... | @@ -4618,7 +4633,7 @@ pub const Object = struct { |
| 4618 | 4633 | |
| 4619 | 4634 | const return_block = try wip.block(1, "Name"); |
| 4620 | 4635 | const this_tag_int_value = try o.lowerValue( |
| 4621 | (try zcu.enumValueFieldIndex(enum_ty, @intCast(field_index))).toIntern(), | |
| 4636 | (try pt.enumValueFieldIndex(enum_ty, @intCast(field_index))).toIntern(), | |
| 4622 | 4637 | ); |
| 4623 | 4638 | try wip_switch.addCase(this_tag_int_value, return_block, &wip); |
| 4624 | 4639 | |
| ... | ... | @@ -4636,13 +4651,13 @@ pub const Object = struct { |
| 4636 | 4651 | |
| 4637 | 4652 | pub const DeclGen = struct { |
| 4638 | 4653 | object: *Object, |
| 4639 | decl: *Module.Decl, | |
| 4654 | decl: *Zcu.Decl, | |
| 4640 | 4655 | decl_index: InternPool.DeclIndex, |
| 4641 | err_msg: ?*Module.ErrorMsg, | |
| 4656 | err_msg: ?*Zcu.ErrorMsg, | |
| 4642 | 4657 | |
| 4643 | 4658 | fn ownerModule(dg: DeclGen) *Package.Module { |
| 4644 | 4659 | const o = dg.object; |
| 4645 | const zcu = o.module; | |
| 4660 | const zcu = o.pt.zcu; | |
| 4646 | 4661 | const namespace = zcu.namespacePtr(dg.decl.src_namespace); |
| 4647 | 4662 | const file_scope = namespace.fileScope(zcu); |
| 4648 | 4663 | return file_scope.mod; |
| ... | ... | @@ -4653,15 +4668,15 @@ pub const DeclGen = struct { |
| 4653 | 4668 | assert(dg.err_msg == null); |
| 4654 | 4669 | const o = dg.object; |
| 4655 | 4670 | const gpa = o.gpa; |
| 4656 | const mod = o.module; | |
| 4657 | const src_loc = dg.decl.navSrcLoc(mod); | |
| 4658 | dg.err_msg = try Module.ErrorMsg.create(gpa, src_loc, "TODO (LLVM): " ++ format, args); | |
| 4671 | const src_loc = dg.decl.navSrcLoc(o.pt.zcu); | |
| 4672 | dg.err_msg = try Zcu.ErrorMsg.create(gpa, src_loc, "TODO (LLVM): " ++ format, args); | |
| 4659 | 4673 | return error.CodegenFail; |
| 4660 | 4674 | } |
| 4661 | 4675 | |
| 4662 | 4676 | fn genDecl(dg: *DeclGen) !void { |
| 4663 | 4677 | const o = dg.object; |
| 4664 | const zcu = o.module; | |
| 4678 | const pt = o.pt; | |
| 4679 | const zcu = pt.zcu; | |
| 4665 | 4680 | const ip = &zcu.intern_pool; |
| 4666 | 4681 | const decl = dg.decl; |
| 4667 | 4682 | const decl_index = dg.decl_index; |
| ... | ... | @@ -4672,7 +4687,7 @@ pub const DeclGen = struct { |
| 4672 | 4687 | } else { |
| 4673 | 4688 | const variable_index = try o.resolveGlobalDecl(decl_index); |
| 4674 | 4689 | variable_index.setAlignment( |
| 4675 | decl.getAlignment(zcu).toLlvm(), | |
| 4690 | decl.getAlignment(pt).toLlvm(), | |
| 4676 | 4691 | &o.builder, |
| 4677 | 4692 | ); |
| 4678 | 4693 | if (decl.@"linksection".toSlice(ip)) |section| |
| ... | ... | @@ -4833,23 +4848,21 @@ pub const FuncGen = struct { |
| 4833 | 4848 | const gop = try self.func_inst_table.getOrPut(gpa, inst); |
| 4834 | 4849 | if (gop.found_existing) return gop.value_ptr.*; |
| 4835 | 4850 | |
| 4836 | const o = self.dg.object; | |
| 4837 | const mod = o.module; | |
| 4838 | const llvm_val = try self.resolveValue((try self.air.value(inst, mod)).?); | |
| 4851 | const llvm_val = try self.resolveValue((try self.air.value(inst, self.dg.object.pt)).?); | |
| 4839 | 4852 | gop.value_ptr.* = llvm_val.toValue(); |
| 4840 | 4853 | return llvm_val.toValue(); |
| 4841 | 4854 | } |
| 4842 | 4855 | |
| 4843 | 4856 | fn resolveValue(self: *FuncGen, val: Value) Error!Builder.Constant { |
| 4844 | 4857 | const o = self.dg.object; |
| 4845 | const mod = o.module; | |
| 4846 | const ty = val.typeOf(mod); | |
| 4858 | const pt = o.pt; | |
| 4859 | const ty = val.typeOf(pt.zcu); | |
| 4847 | 4860 | const llvm_val = try o.lowerValue(val.toIntern()); |
| 4848 | if (!isByRef(ty, mod)) return llvm_val; | |
| 4861 | if (!isByRef(ty, pt)) return llvm_val; | |
| 4849 | 4862 | |
| 4850 | 4863 | // We have an LLVM value but we need to create a global constant and |
| 4851 | 4864 | // set the value as its initializer, and then return a pointer to the global. |
| 4852 | const target = mod.getTarget(); | |
| 4865 | const target = pt.zcu.getTarget(); | |
| 4853 | 4866 | const variable_index = try o.builder.addVariable( |
| 4854 | 4867 | .empty, |
| 4855 | 4868 | llvm_val.typeOf(&o.builder), |
| ... | ... | @@ -4859,7 +4872,7 @@ pub const FuncGen = struct { |
| 4859 | 4872 | variable_index.setLinkage(.private, &o.builder); |
| 4860 | 4873 | variable_index.setMutability(.constant, &o.builder); |
| 4861 | 4874 | variable_index.setUnnamedAddr(.unnamed_addr, &o.builder); |
| 4862 | variable_index.setAlignment(ty.abiAlignment(mod).toLlvm(), &o.builder); | |
| 4875 | variable_index.setAlignment(ty.abiAlignment(pt).toLlvm(), &o.builder); | |
| 4863 | 4876 | return o.builder.convConst( |
| 4864 | 4877 | variable_index.toConst(&o.builder), |
| 4865 | 4878 | try o.builder.ptrType(toLlvmAddressSpace(.generic, target)), |
| ... | ... | @@ -4868,10 +4881,10 @@ pub const FuncGen = struct { |
| 4868 | 4881 | |
| 4869 | 4882 | fn resolveNullOptUsize(self: *FuncGen) Error!Builder.Constant { |
| 4870 | 4883 | const o = self.dg.object; |
| 4871 | const mod = o.module; | |
| 4884 | const pt = o.pt; | |
| 4872 | 4885 | if (o.null_opt_usize == .no_init) { |
| 4873 | o.null_opt_usize = try self.resolveValue(Value.fromInterned(try mod.intern(.{ .opt = .{ | |
| 4874 | .ty = try mod.intern(.{ .opt_type = .usize_type }), | |
| 4886 | o.null_opt_usize = try self.resolveValue(Value.fromInterned(try pt.intern(.{ .opt = .{ | |
| 4887 | .ty = try pt.intern(.{ .opt_type = .usize_type }), | |
| 4875 | 4888 | .val = .none, |
| 4876 | 4889 | } }))); |
| 4877 | 4890 | } |
| ... | ... | @@ -4880,7 +4893,7 @@ pub const FuncGen = struct { |
| 4880 | 4893 | |
| 4881 | 4894 | fn genBody(self: *FuncGen, body: []const Air.Inst.Index) Error!void { |
| 4882 | 4895 | const o = self.dg.object; |
| 4883 | const mod = o.module; | |
| 4896 | const mod = o.pt.zcu; | |
| 4884 | 4897 | const ip = &mod.intern_pool; |
| 4885 | 4898 | const air_tags = self.air.instructions.items(.tag); |
| 4886 | 4899 | for (body, 0..) |inst, i| { |
| ... | ... | @@ -5145,7 +5158,8 @@ pub const FuncGen = struct { |
| 5145 | 5158 | |
| 5146 | 5159 | if (maybe_inline_func) |inline_func| { |
| 5147 | 5160 | const o = self.dg.object; |
| 5148 | const zcu = o.module; | |
| 5161 | const pt = o.pt; | |
| 5162 | const zcu = pt.zcu; | |
| 5149 | 5163 | |
| 5150 | 5164 | const func = zcu.funcInfo(inline_func); |
| 5151 | 5165 | const decl_index = func.owner_decl; |
| ... | ... | @@ -5161,7 +5175,7 @@ pub const FuncGen = struct { |
| 5161 | 5175 | |
| 5162 | 5176 | const fqn = try decl.fullyQualifiedName(zcu); |
| 5163 | 5177 | |
| 5164 | const fn_ty = try zcu.funcType(.{ | |
| 5178 | const fn_ty = try pt.funcType(.{ | |
| 5165 | 5179 | .param_types = &.{}, |
| 5166 | 5180 | .return_type = .void_type, |
| 5167 | 5181 | }); |
| ... | ... | @@ -5228,7 +5242,8 @@ pub const FuncGen = struct { |
| 5228 | 5242 | const extra = self.air.extraData(Air.Call, pl_op.payload); |
| 5229 | 5243 | const args: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra.end..][0..extra.data.args_len]); |
| 5230 | 5244 | const o = self.dg.object; |
| 5231 | const mod = o.module; | |
| 5245 | const pt = o.pt; | |
| 5246 | const mod = pt.zcu; | |
| 5232 | 5247 | const ip = &mod.intern_pool; |
| 5233 | 5248 | const callee_ty = self.typeOf(pl_op.operand); |
| 5234 | 5249 | const zig_fn_ty = switch (callee_ty.zigTypeTag(mod)) { |
| ... | ... | @@ -5240,7 +5255,7 @@ pub const FuncGen = struct { |
| 5240 | 5255 | const return_type = Type.fromInterned(fn_info.return_type); |
| 5241 | 5256 | const llvm_fn = try self.resolveInst(pl_op.operand); |
| 5242 | 5257 | const target = mod.getTarget(); |
| 5243 | const sret = firstParamSRet(fn_info, mod, target); | |
| 5258 | const sret = firstParamSRet(fn_info, pt, target); | |
| 5244 | 5259 | |
| 5245 | 5260 | var llvm_args = std.ArrayList(Builder.Value).init(self.gpa); |
| 5246 | 5261 | defer llvm_args.deinit(); |
| ... | ... | @@ -5258,14 +5273,13 @@ pub const FuncGen = struct { |
| 5258 | 5273 | const llvm_ret_ty = try o.lowerType(return_type); |
| 5259 | 5274 | try attributes.addParamAttr(0, .{ .sret = llvm_ret_ty }, &o.builder); |
| 5260 | 5275 | |
| 5261 | const alignment = return_type.abiAlignment(mod).toLlvm(); | |
| 5276 | const alignment = return_type.abiAlignment(pt).toLlvm(); | |
| 5262 | 5277 | const ret_ptr = try self.buildAllocaWorkaround(return_type, alignment); |
| 5263 | 5278 | try llvm_args.append(ret_ptr); |
| 5264 | 5279 | break :blk ret_ptr; |
| 5265 | 5280 | }; |
| 5266 | 5281 | |
| 5267 | const err_return_tracing = return_type.isError(mod) and | |
| 5268 | o.module.comp.config.any_error_tracing; | |
| 5282 | const err_return_tracing = return_type.isError(mod) and mod.comp.config.any_error_tracing; | |
| 5269 | 5283 | if (err_return_tracing) { |
| 5270 | 5284 | assert(self.err_ret_trace != .none); |
| 5271 | 5285 | try llvm_args.append(self.err_ret_trace); |
| ... | ... | @@ -5279,8 +5293,8 @@ pub const FuncGen = struct { |
| 5279 | 5293 | const param_ty = self.typeOf(arg); |
| 5280 | 5294 | const llvm_arg = try self.resolveInst(arg); |
| 5281 | 5295 | const llvm_param_ty = try o.lowerType(param_ty); |
| 5282 | if (isByRef(param_ty, mod)) { | |
| 5283 | const alignment = param_ty.abiAlignment(mod).toLlvm(); | |
| 5296 | if (isByRef(param_ty, pt)) { | |
| 5297 | const alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 5284 | 5298 | const loaded = try self.wip.load(.normal, llvm_param_ty, llvm_arg, alignment, ""); |
| 5285 | 5299 | try llvm_args.append(loaded); |
| 5286 | 5300 | } else { |
| ... | ... | @@ -5291,10 +5305,10 @@ pub const FuncGen = struct { |
| 5291 | 5305 | const arg = args[it.zig_index - 1]; |
| 5292 | 5306 | const param_ty = self.typeOf(arg); |
| 5293 | 5307 | const llvm_arg = try self.resolveInst(arg); |
| 5294 | if (isByRef(param_ty, mod)) { | |
| 5308 | if (isByRef(param_ty, pt)) { | |
| 5295 | 5309 | try llvm_args.append(llvm_arg); |
| 5296 | 5310 | } else { |
| 5297 | const alignment = param_ty.abiAlignment(mod).toLlvm(); | |
| 5311 | const alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 5298 | 5312 | const param_llvm_ty = llvm_arg.typeOfWip(&self.wip); |
| 5299 | 5313 | const arg_ptr = try self.buildAlloca(param_llvm_ty, alignment); |
| 5300 | 5314 | _ = try self.wip.store(.normal, llvm_arg, arg_ptr, alignment); |
| ... | ... | @@ -5306,10 +5320,10 @@ pub const FuncGen = struct { |
| 5306 | 5320 | const param_ty = self.typeOf(arg); |
| 5307 | 5321 | const llvm_arg = try self.resolveInst(arg); |
| 5308 | 5322 | |
| 5309 | const alignment = param_ty.abiAlignment(mod).toLlvm(); | |
| 5323 | const alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 5310 | 5324 | const param_llvm_ty = try o.lowerType(param_ty); |
| 5311 | 5325 | const arg_ptr = try self.buildAllocaWorkaround(param_ty, alignment); |
| 5312 | if (isByRef(param_ty, mod)) { | |
| 5326 | if (isByRef(param_ty, pt)) { | |
| 5313 | 5327 | const loaded = try self.wip.load(.normal, param_llvm_ty, llvm_arg, alignment, ""); |
| 5314 | 5328 | _ = try self.wip.store(.normal, loaded, arg_ptr, alignment); |
| 5315 | 5329 | } else { |
| ... | ... | @@ -5321,16 +5335,16 @@ pub const FuncGen = struct { |
| 5321 | 5335 | const arg = args[it.zig_index - 1]; |
| 5322 | 5336 | const param_ty = self.typeOf(arg); |
| 5323 | 5337 | const llvm_arg = try self.resolveInst(arg); |
| 5324 | const int_llvm_ty = try o.builder.intType(@intCast(param_ty.abiSize(mod) * 8)); | |
| 5338 | const int_llvm_ty = try o.builder.intType(@intCast(param_ty.abiSize(pt) * 8)); | |
| 5325 | 5339 | |
| 5326 | if (isByRef(param_ty, mod)) { | |
| 5327 | const alignment = param_ty.abiAlignment(mod).toLlvm(); | |
| 5340 | if (isByRef(param_ty, pt)) { | |
| 5341 | const alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 5328 | 5342 | const loaded = try self.wip.load(.normal, int_llvm_ty, llvm_arg, alignment, ""); |
| 5329 | 5343 | try llvm_args.append(loaded); |
| 5330 | 5344 | } else { |
| 5331 | 5345 | // LLVM does not allow bitcasting structs so we must allocate |
| 5332 | 5346 | // a local, store as one type, and then load as another type. |
| 5333 | const alignment = param_ty.abiAlignment(mod).toLlvm(); | |
| 5347 | const alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 5334 | 5348 | const int_ptr = try self.buildAllocaWorkaround(param_ty, alignment); |
| 5335 | 5349 | _ = try self.wip.store(.normal, llvm_arg, int_ptr, alignment); |
| 5336 | 5350 | const loaded = try self.wip.load(.normal, int_llvm_ty, int_ptr, alignment, ""); |
| ... | ... | @@ -5349,9 +5363,9 @@ pub const FuncGen = struct { |
| 5349 | 5363 | const param_ty = self.typeOf(arg); |
| 5350 | 5364 | const llvm_types = it.types_buffer[0..it.types_len]; |
| 5351 | 5365 | const llvm_arg = try self.resolveInst(arg); |
| 5352 | const is_by_ref = isByRef(param_ty, mod); | |
| 5366 | const is_by_ref = isByRef(param_ty, pt); | |
| 5353 | 5367 | const arg_ptr = if (is_by_ref) llvm_arg else ptr: { |
| 5354 | const alignment = param_ty.abiAlignment(mod).toLlvm(); | |
| 5368 | const alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 5355 | 5369 | const ptr = try self.buildAlloca(llvm_arg.typeOfWip(&self.wip), alignment); |
| 5356 | 5370 | _ = try self.wip.store(.normal, llvm_arg, ptr, alignment); |
| 5357 | 5371 | break :ptr ptr; |
| ... | ... | @@ -5377,8 +5391,8 @@ pub const FuncGen = struct { |
| 5377 | 5391 | const arg = args[it.zig_index - 1]; |
| 5378 | 5392 | const arg_ty = self.typeOf(arg); |
| 5379 | 5393 | var llvm_arg = try self.resolveInst(arg); |
| 5380 | const alignment = arg_ty.abiAlignment(mod).toLlvm(); | |
| 5381 | if (!isByRef(arg_ty, mod)) { | |
| 5394 | const alignment = arg_ty.abiAlignment(pt).toLlvm(); | |
| 5395 | if (!isByRef(arg_ty, pt)) { | |
| 5382 | 5396 | const ptr = try self.buildAlloca(llvm_arg.typeOfWip(&self.wip), alignment); |
| 5383 | 5397 | _ = try self.wip.store(.normal, llvm_arg, ptr, alignment); |
| 5384 | 5398 | llvm_arg = ptr; |
| ... | ... | @@ -5395,8 +5409,8 @@ pub const FuncGen = struct { |
| 5395 | 5409 | const arg = args[it.zig_index - 1]; |
| 5396 | 5410 | const arg_ty = self.typeOf(arg); |
| 5397 | 5411 | var llvm_arg = try self.resolveInst(arg); |
| 5398 | const alignment = arg_ty.abiAlignment(mod).toLlvm(); | |
| 5399 | if (!isByRef(arg_ty, mod)) { | |
| 5412 | const alignment = arg_ty.abiAlignment(pt).toLlvm(); | |
| 5413 | if (!isByRef(arg_ty, pt)) { | |
| 5400 | 5414 | const ptr = try self.buildAlloca(llvm_arg.typeOfWip(&self.wip), alignment); |
| 5401 | 5415 | _ = try self.wip.store(.normal, llvm_arg, ptr, alignment); |
| 5402 | 5416 | llvm_arg = ptr; |
| ... | ... | @@ -5418,7 +5432,7 @@ pub const FuncGen = struct { |
| 5418 | 5432 | .byval => { |
| 5419 | 5433 | const param_index = it.zig_index - 1; |
| 5420 | 5434 | const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[param_index]); |
| 5421 | if (!isByRef(param_ty, mod)) { | |
| 5435 | if (!isByRef(param_ty, pt)) { | |
| 5422 | 5436 | try o.addByValParamAttrs(&attributes, param_ty, param_index, fn_info, it.llvm_index - 1); |
| 5423 | 5437 | } |
| 5424 | 5438 | }, |
| ... | ... | @@ -5426,7 +5440,7 @@ pub const FuncGen = struct { |
| 5426 | 5440 | const param_index = it.zig_index - 1; |
| 5427 | 5441 | const param_ty = Type.fromInterned(fn_info.param_types.get(ip)[param_index]); |
| 5428 | 5442 | const param_llvm_ty = try o.lowerType(param_ty); |
| 5429 | const alignment = param_ty.abiAlignment(mod).toLlvm(); | |
| 5443 | const alignment = param_ty.abiAlignment(pt).toLlvm(); | |
| 5430 | 5444 | try o.addByRefParamAttrs(&attributes, it.llvm_index - 1, alignment, it.byval_attr, param_llvm_ty); |
| 5431 | 5445 | }, |
| 5432 | 5446 | .byref_mut => try attributes.addParamAttr(it.llvm_index - 1, .noundef, &o.builder), |
| ... | ... | @@ -5460,7 +5474,7 @@ pub const FuncGen = struct { |
| 5460 | 5474 | const elem_align = (if (ptr_info.flags.alignment != .none) |
| 5461 | 5475 | @as(InternPool.Alignment, ptr_info.flags.alignment) |
| 5462 | 5476 | else |
| 5463 | Type.fromInterned(ptr_info.child).abiAlignment(mod).max(.@"1")).toLlvm(); | |
| 5477 | Type.fromInterned(ptr_info.child).abiAlignment(pt).max(.@"1")).toLlvm(); | |
| 5464 | 5478 | try attributes.addParamAttr(llvm_arg_i, .{ .@"align" = elem_align }, &o.builder); |
| 5465 | 5479 | }, |
| 5466 | 5480 | }; |
| ... | ... | @@ -5485,17 +5499,17 @@ pub const FuncGen = struct { |
| 5485 | 5499 | return .none; |
| 5486 | 5500 | } |
| 5487 | 5501 | |
| 5488 | if (self.liveness.isUnused(inst) or !return_type.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 5502 | if (self.liveness.isUnused(inst) or !return_type.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 5489 | 5503 | return .none; |
| 5490 | 5504 | } |
| 5491 | 5505 | |
| 5492 | 5506 | const llvm_ret_ty = try o.lowerType(return_type); |
| 5493 | 5507 | if (ret_ptr) |rp| { |
| 5494 | if (isByRef(return_type, mod)) { | |
| 5508 | if (isByRef(return_type, pt)) { | |
| 5495 | 5509 | return rp; |
| 5496 | 5510 | } else { |
| 5497 | 5511 | // our by-ref status disagrees with sret so we must load. |
| 5498 | const return_alignment = return_type.abiAlignment(mod).toLlvm(); | |
| 5512 | const return_alignment = return_type.abiAlignment(pt).toLlvm(); | |
| 5499 | 5513 | return self.wip.load(.normal, llvm_ret_ty, rp, return_alignment, ""); |
| 5500 | 5514 | } |
| 5501 | 5515 | } |
| ... | ... | @@ -5506,19 +5520,19 @@ pub const FuncGen = struct { |
| 5506 | 5520 | // In this case the function return type is honoring the calling convention by having |
| 5507 | 5521 | // a different LLVM type than the usual one. We solve this here at the callsite |
| 5508 | 5522 | // by using our canonical type, then loading it if necessary. |
| 5509 | const alignment = return_type.abiAlignment(mod).toLlvm(); | |
| 5523 | const alignment = return_type.abiAlignment(pt).toLlvm(); | |
| 5510 | 5524 | const rp = try self.buildAlloca(abi_ret_ty, alignment); |
| 5511 | 5525 | _ = try self.wip.store(.normal, call, rp, alignment); |
| 5512 | return if (isByRef(return_type, mod)) | |
| 5526 | return if (isByRef(return_type, pt)) | |
| 5513 | 5527 | rp |
| 5514 | 5528 | else |
| 5515 | 5529 | try self.wip.load(.normal, llvm_ret_ty, rp, alignment, ""); |
| 5516 | 5530 | } |
| 5517 | 5531 | |
| 5518 | if (isByRef(return_type, mod)) { | |
| 5532 | if (isByRef(return_type, pt)) { | |
| 5519 | 5533 | // our by-ref status disagrees with sret so we must allocate, store, |
| 5520 | 5534 | // and return the allocation pointer. |
| 5521 | const alignment = return_type.abiAlignment(mod).toLlvm(); | |
| 5535 | const alignment = return_type.abiAlignment(pt).toLlvm(); | |
| 5522 | 5536 | const rp = try self.buildAlloca(llvm_ret_ty, alignment); |
| 5523 | 5537 | _ = try self.wip.store(.normal, call, rp, alignment); |
| 5524 | 5538 | return rp; |
| ... | ... | @@ -5527,9 +5541,9 @@ pub const FuncGen = struct { |
| 5527 | 5541 | } |
| 5528 | 5542 | } |
| 5529 | 5543 | |
| 5530 | fn buildSimplePanic(fg: *FuncGen, panic_id: Module.PanicId) !void { | |
| 5544 | fn buildSimplePanic(fg: *FuncGen, panic_id: Zcu.PanicId) !void { | |
| 5531 | 5545 | const o = fg.dg.object; |
| 5532 | const mod = o.module; | |
| 5546 | const mod = o.pt.zcu; | |
| 5533 | 5547 | const msg_decl_index = mod.panic_messages[@intFromEnum(panic_id)].unwrap().?; |
| 5534 | 5548 | const msg_decl = mod.declPtr(msg_decl_index); |
| 5535 | 5549 | const msg_len = msg_decl.typeOf(mod).childType(mod).arrayLen(mod); |
| ... | ... | @@ -5567,15 +5581,16 @@ pub const FuncGen = struct { |
| 5567 | 5581 | |
| 5568 | 5582 | fn airRet(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value { |
| 5569 | 5583 | const o = self.dg.object; |
| 5570 | const mod = o.module; | |
| 5584 | const pt = o.pt; | |
| 5585 | const mod = pt.zcu; | |
| 5571 | 5586 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5572 | 5587 | const ret_ty = self.typeOf(un_op); |
| 5573 | 5588 | |
| 5574 | 5589 | if (self.ret_ptr != .none) { |
| 5575 | const ptr_ty = try mod.singleMutPtrType(ret_ty); | |
| 5590 | const ptr_ty = try pt.singleMutPtrType(ret_ty); | |
| 5576 | 5591 | |
| 5577 | 5592 | const operand = try self.resolveInst(un_op); |
| 5578 | const val_is_undef = if (try self.air.value(un_op, mod)) |val| val.isUndefDeep(mod) else false; | |
| 5593 | const val_is_undef = if (try self.air.value(un_op, pt)) |val| val.isUndefDeep(mod) else false; | |
| 5579 | 5594 | if (val_is_undef and safety) undef: { |
| 5580 | 5595 | const ptr_info = ptr_ty.ptrInfo(mod); |
| 5581 | 5596 | const needs_bitmask = (ptr_info.packed_offset.host_size != 0); |
| ... | ... | @@ -5585,10 +5600,10 @@ pub const FuncGen = struct { |
| 5585 | 5600 | // https://github.com/ziglang/zig/issues/15337 |
| 5586 | 5601 | break :undef; |
| 5587 | 5602 | } |
| 5588 | const len = try o.builder.intValue(try o.lowerType(Type.usize), ret_ty.abiSize(mod)); | |
| 5603 | const len = try o.builder.intValue(try o.lowerType(Type.usize), ret_ty.abiSize(pt)); | |
| 5589 | 5604 | _ = try self.wip.callMemSet( |
| 5590 | 5605 | self.ret_ptr, |
| 5591 | ptr_ty.ptrAlignment(mod).toLlvm(), | |
| 5606 | ptr_ty.ptrAlignment(pt).toLlvm(), | |
| 5592 | 5607 | try o.builder.intValue(.i8, 0xaa), |
| 5593 | 5608 | len, |
| 5594 | 5609 | if (ptr_ty.isVolatilePtr(mod)) .@"volatile" else .normal, |
| ... | ... | @@ -5615,7 +5630,7 @@ pub const FuncGen = struct { |
| 5615 | 5630 | return .none; |
| 5616 | 5631 | } |
| 5617 | 5632 | const fn_info = mod.typeToFunc(self.dg.decl.typeOf(mod)).?; |
| 5618 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 5633 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 5619 | 5634 | if (Type.fromInterned(fn_info.return_type).isError(mod)) { |
| 5620 | 5635 | // Functions with an empty error set are emitted with an error code |
| 5621 | 5636 | // return type and return zero so they can be function pointers coerced |
| ... | ... | @@ -5629,13 +5644,13 @@ pub const FuncGen = struct { |
| 5629 | 5644 | |
| 5630 | 5645 | const abi_ret_ty = try lowerFnRetTy(o, fn_info); |
| 5631 | 5646 | const operand = try self.resolveInst(un_op); |
| 5632 | const val_is_undef = if (try self.air.value(un_op, mod)) |val| val.isUndefDeep(mod) else false; | |
| 5633 | const alignment = ret_ty.abiAlignment(mod).toLlvm(); | |
| 5647 | const val_is_undef = if (try self.air.value(un_op, pt)) |val| val.isUndefDeep(mod) else false; | |
| 5648 | const alignment = ret_ty.abiAlignment(pt).toLlvm(); | |
| 5634 | 5649 | |
| 5635 | 5650 | if (val_is_undef and safety) { |
| 5636 | 5651 | const llvm_ret_ty = operand.typeOfWip(&self.wip); |
| 5637 | 5652 | const rp = try self.buildAlloca(llvm_ret_ty, alignment); |
| 5638 | const len = try o.builder.intValue(try o.lowerType(Type.usize), ret_ty.abiSize(mod)); | |
| 5653 | const len = try o.builder.intValue(try o.lowerType(Type.usize), ret_ty.abiSize(pt)); | |
| 5639 | 5654 | _ = try self.wip.callMemSet( |
| 5640 | 5655 | rp, |
| 5641 | 5656 | alignment, |
| ... | ... | @@ -5651,7 +5666,7 @@ pub const FuncGen = struct { |
| 5651 | 5666 | return .none; |
| 5652 | 5667 | } |
| 5653 | 5668 | |
| 5654 | if (isByRef(ret_ty, mod)) { | |
| 5669 | if (isByRef(ret_ty, pt)) { | |
| 5655 | 5670 | // operand is a pointer however self.ret_ptr is null so that means |
| 5656 | 5671 | // we need to return a value. |
| 5657 | 5672 | _ = try self.wip.ret(try self.wip.load(.normal, abi_ret_ty, operand, alignment, "")); |
| ... | ... | @@ -5672,12 +5687,13 @@ pub const FuncGen = struct { |
| 5672 | 5687 | |
| 5673 | 5688 | fn airRetLoad(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5674 | 5689 | const o = self.dg.object; |
| 5675 | const mod = o.module; | |
| 5690 | const pt = o.pt; | |
| 5691 | const mod = pt.zcu; | |
| 5676 | 5692 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5677 | 5693 | const ptr_ty = self.typeOf(un_op); |
| 5678 | 5694 | const ret_ty = ptr_ty.childType(mod); |
| 5679 | 5695 | const fn_info = mod.typeToFunc(self.dg.decl.typeOf(mod)).?; |
| 5680 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 5696 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 5681 | 5697 | if (Type.fromInterned(fn_info.return_type).isError(mod)) { |
| 5682 | 5698 | // Functions with an empty error set are emitted with an error code |
| 5683 | 5699 | // return type and return zero so they can be function pointers coerced |
| ... | ... | @@ -5694,7 +5710,7 @@ pub const FuncGen = struct { |
| 5694 | 5710 | } |
| 5695 | 5711 | const ptr = try self.resolveInst(un_op); |
| 5696 | 5712 | const abi_ret_ty = try lowerFnRetTy(o, fn_info); |
| 5697 | const alignment = ret_ty.abiAlignment(mod).toLlvm(); | |
| 5713 | const alignment = ret_ty.abiAlignment(pt).toLlvm(); | |
| 5698 | 5714 | _ = try self.wip.ret(try self.wip.load(.normal, abi_ret_ty, ptr, alignment, "")); |
| 5699 | 5715 | return .none; |
| 5700 | 5716 | } |
| ... | ... | @@ -5711,17 +5727,17 @@ pub const FuncGen = struct { |
| 5711 | 5727 | |
| 5712 | 5728 | fn airCVaCopy(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5713 | 5729 | const o = self.dg.object; |
| 5730 | const pt = o.pt; | |
| 5714 | 5731 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5715 | 5732 | const src_list = try self.resolveInst(ty_op.operand); |
| 5716 | 5733 | const va_list_ty = ty_op.ty.toType(); |
| 5717 | 5734 | const llvm_va_list_ty = try o.lowerType(va_list_ty); |
| 5718 | const mod = o.module; | |
| 5719 | 5735 | |
| 5720 | const result_alignment = va_list_ty.abiAlignment(mod).toLlvm(); | |
| 5736 | const result_alignment = va_list_ty.abiAlignment(pt).toLlvm(); | |
| 5721 | 5737 | const dest_list = try self.buildAllocaWorkaround(va_list_ty, result_alignment); |
| 5722 | 5738 | |
| 5723 | 5739 | _ = try self.wip.callIntrinsic(.normal, .none, .va_copy, &.{}, &.{ dest_list, src_list }, ""); |
| 5724 | return if (isByRef(va_list_ty, mod)) | |
| 5740 | return if (isByRef(va_list_ty, pt)) | |
| 5725 | 5741 | dest_list |
| 5726 | 5742 | else |
| 5727 | 5743 | try self.wip.load(.normal, llvm_va_list_ty, dest_list, result_alignment, ""); |
| ... | ... | @@ -5737,15 +5753,15 @@ pub const FuncGen = struct { |
| 5737 | 5753 | |
| 5738 | 5754 | fn airCVaStart(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5739 | 5755 | const o = self.dg.object; |
| 5740 | const mod = o.module; | |
| 5756 | const pt = o.pt; | |
| 5741 | 5757 | const va_list_ty = self.typeOfIndex(inst); |
| 5742 | 5758 | const llvm_va_list_ty = try o.lowerType(va_list_ty); |
| 5743 | 5759 | |
| 5744 | const result_alignment = va_list_ty.abiAlignment(mod).toLlvm(); | |
| 5760 | const result_alignment = va_list_ty.abiAlignment(pt).toLlvm(); | |
| 5745 | 5761 | const dest_list = try self.buildAllocaWorkaround(va_list_ty, result_alignment); |
| 5746 | 5762 | |
| 5747 | 5763 | _ = try self.wip.callIntrinsic(.normal, .none, .va_start, &.{}, &.{dest_list}, ""); |
| 5748 | return if (isByRef(va_list_ty, mod)) | |
| 5764 | return if (isByRef(va_list_ty, pt)) | |
| 5749 | 5765 | dest_list |
| 5750 | 5766 | else |
| 5751 | 5767 | try self.wip.load(.normal, llvm_va_list_ty, dest_list, result_alignment, ""); |
| ... | ... | @@ -5802,21 +5818,22 @@ pub const FuncGen = struct { |
| 5802 | 5818 | rhs: Builder.Value, |
| 5803 | 5819 | ) Allocator.Error!Builder.Value { |
| 5804 | 5820 | const o = self.dg.object; |
| 5805 | const mod = o.module; | |
| 5821 | const pt = o.pt; | |
| 5822 | const mod = pt.zcu; | |
| 5806 | 5823 | const scalar_ty = operand_ty.scalarType(mod); |
| 5807 | 5824 | const int_ty = switch (scalar_ty.zigTypeTag(mod)) { |
| 5808 | 5825 | .Enum => scalar_ty.intTagType(mod), |
| 5809 | 5826 | .Int, .Bool, .Pointer, .ErrorSet => scalar_ty, |
| 5810 | 5827 | .Optional => blk: { |
| 5811 | 5828 | const payload_ty = operand_ty.optionalChild(mod); |
| 5812 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod) or | |
| 5829 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt) or | |
| 5813 | 5830 | operand_ty.optionalReprIsPayload(mod)) |
| 5814 | 5831 | { |
| 5815 | 5832 | break :blk operand_ty; |
| 5816 | 5833 | } |
| 5817 | 5834 | // We need to emit instructions to check for equality/inequality |
| 5818 | 5835 | // of optionals that are not pointers. |
| 5819 | const is_by_ref = isByRef(scalar_ty, mod); | |
| 5836 | const is_by_ref = isByRef(scalar_ty, pt); | |
| 5820 | 5837 | const opt_llvm_ty = try o.lowerType(scalar_ty); |
| 5821 | 5838 | const lhs_non_null = try self.optCmpNull(.ne, opt_llvm_ty, lhs, is_by_ref); |
| 5822 | 5839 | const rhs_non_null = try self.optCmpNull(.ne, opt_llvm_ty, rhs, is_by_ref); |
| ... | ... | @@ -5908,7 +5925,8 @@ pub const FuncGen = struct { |
| 5908 | 5925 | body: []const Air.Inst.Index, |
| 5909 | 5926 | ) !Builder.Value { |
| 5910 | 5927 | const o = self.dg.object; |
| 5911 | const mod = o.module; | |
| 5928 | const pt = o.pt; | |
| 5929 | const mod = pt.zcu; | |
| 5912 | 5930 | const inst_ty = self.typeOfIndex(inst); |
| 5913 | 5931 | |
| 5914 | 5932 | if (inst_ty.isNoReturn(mod)) { |
| ... | ... | @@ -5916,7 +5934,7 @@ pub const FuncGen = struct { |
| 5916 | 5934 | return .none; |
| 5917 | 5935 | } |
| 5918 | 5936 | |
| 5919 | const have_block_result = inst_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod); | |
| 5937 | const have_block_result = inst_ty.isFnOrHasRuntimeBitsIgnoreComptime(pt); | |
| 5920 | 5938 | |
| 5921 | 5939 | var breaks: BreakList = if (have_block_result) .{ .list = .{} } else .{ .len = 0 }; |
| 5922 | 5940 | defer if (have_block_result) breaks.list.deinit(self.gpa); |
| ... | ... | @@ -5940,7 +5958,7 @@ pub const FuncGen = struct { |
| 5940 | 5958 | // a pointer to it. LLVM IR allows the call instruction to use function bodies instead |
| 5941 | 5959 | // of function pointers, however the phi makes it a runtime value and therefore |
| 5942 | 5960 | // the LLVM type has to be wrapped in a pointer. |
| 5943 | if (inst_ty.zigTypeTag(mod) == .Fn or isByRef(inst_ty, mod)) { | |
| 5961 | if (inst_ty.zigTypeTag(mod) == .Fn or isByRef(inst_ty, pt)) { | |
| 5944 | 5962 | break :ty .ptr; |
| 5945 | 5963 | } |
| 5946 | 5964 | break :ty raw_llvm_ty; |
| ... | ... | @@ -5958,13 +5976,13 @@ pub const FuncGen = struct { |
| 5958 | 5976 | |
| 5959 | 5977 | fn airBr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 5960 | 5978 | const o = self.dg.object; |
| 5979 | const pt = o.pt; | |
| 5961 | 5980 | const branch = self.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 5962 | 5981 | const block = self.blocks.get(branch.block_inst).?; |
| 5963 | 5982 | |
| 5964 | 5983 | // Add the values to the lists only if the break provides a value. |
| 5965 | 5984 | const operand_ty = self.typeOf(branch.operand); |
| 5966 | const mod = o.module; | |
| 5967 | if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { | |
| 5985 | if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(pt)) { | |
| 5968 | 5986 | const val = try self.resolveInst(branch.operand); |
| 5969 | 5987 | |
| 5970 | 5988 | // For the phi node, we need the basic blocks and the values of the |
| ... | ... | @@ -5998,7 +6016,7 @@ pub const FuncGen = struct { |
| 5998 | 6016 | |
| 5999 | 6017 | fn airTry(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 6000 | 6018 | const o = self.dg.object; |
| 6001 | const mod = o.module; | |
| 6019 | const pt = o.pt; | |
| 6002 | 6020 | const inst = body_tail[0]; |
| 6003 | 6021 | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6004 | 6022 | const err_union = try self.resolveInst(pl_op.operand); |
| ... | ... | @@ -6006,14 +6024,14 @@ pub const FuncGen = struct { |
| 6006 | 6024 | const body: []const Air.Inst.Index = @ptrCast(self.air.extra[extra.end..][0..extra.data.body_len]); |
| 6007 | 6025 | const err_union_ty = self.typeOf(pl_op.operand); |
| 6008 | 6026 | const payload_ty = self.typeOfIndex(inst); |
| 6009 | const can_elide_load = if (isByRef(payload_ty, mod)) self.canElideLoad(body_tail) else false; | |
| 6027 | const can_elide_load = if (isByRef(payload_ty, pt)) self.canElideLoad(body_tail) else false; | |
| 6010 | 6028 | const is_unused = self.liveness.isUnused(inst); |
| 6011 | 6029 | return lowerTry(self, err_union, body, err_union_ty, false, can_elide_load, is_unused); |
| 6012 | 6030 | } |
| 6013 | 6031 | |
| 6014 | 6032 | fn airTryPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6015 | 6033 | const o = self.dg.object; |
| 6016 | const mod = o.module; | |
| 6034 | const mod = o.pt.zcu; | |
| 6017 | 6035 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6018 | 6036 | const extra = self.air.extraData(Air.TryPtr, ty_pl.payload); |
| 6019 | 6037 | const err_union_ptr = try self.resolveInst(extra.data.ptr); |
| ... | ... | @@ -6033,9 +6051,10 @@ pub const FuncGen = struct { |
| 6033 | 6051 | is_unused: bool, |
| 6034 | 6052 | ) !Builder.Value { |
| 6035 | 6053 | const o = fg.dg.object; |
| 6036 | const mod = o.module; | |
| 6054 | const pt = o.pt; | |
| 6055 | const mod = pt.zcu; | |
| 6037 | 6056 | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| 6038 | const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(mod); | |
| 6057 | const payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(pt); | |
| 6039 | 6058 | const err_union_llvm_ty = try o.lowerType(err_union_ty); |
| 6040 | 6059 | const error_type = try o.errorIntType(); |
| 6041 | 6060 | |
| ... | ... | @@ -6048,8 +6067,8 @@ pub const FuncGen = struct { |
| 6048 | 6067 | else |
| 6049 | 6068 | err_union; |
| 6050 | 6069 | } |
| 6051 | const err_field_index = try errUnionErrorOffset(payload_ty, mod); | |
| 6052 | if (operand_is_ptr or isByRef(err_union_ty, mod)) { | |
| 6070 | const err_field_index = try errUnionErrorOffset(payload_ty, pt); | |
| 6071 | if (operand_is_ptr or isByRef(err_union_ty, pt)) { | |
| 6053 | 6072 | const err_field_ptr = |
| 6054 | 6073 | try fg.wip.gepStruct(err_union_llvm_ty, err_union, err_field_index, ""); |
| 6055 | 6074 | // TODO add alignment to this load |
| ... | ... | @@ -6077,13 +6096,13 @@ pub const FuncGen = struct { |
| 6077 | 6096 | } |
| 6078 | 6097 | if (is_unused) return .none; |
| 6079 | 6098 | if (!payload_has_bits) return if (operand_is_ptr) err_union else .none; |
| 6080 | const offset = try errUnionPayloadOffset(payload_ty, mod); | |
| 6099 | const offset = try errUnionPayloadOffset(payload_ty, pt); | |
| 6081 | 6100 | if (operand_is_ptr) { |
| 6082 | 6101 | return fg.wip.gepStruct(err_union_llvm_ty, err_union, offset, ""); |
| 6083 | } else if (isByRef(err_union_ty, mod)) { | |
| 6102 | } else if (isByRef(err_union_ty, pt)) { | |
| 6084 | 6103 | const payload_ptr = try fg.wip.gepStruct(err_union_llvm_ty, err_union, offset, ""); |
| 6085 | const payload_alignment = payload_ty.abiAlignment(mod).toLlvm(); | |
| 6086 | if (isByRef(payload_ty, mod)) { | |
| 6104 | const payload_alignment = payload_ty.abiAlignment(pt).toLlvm(); | |
| 6105 | if (isByRef(payload_ty, pt)) { | |
| 6087 | 6106 | if (can_elide_load) |
| 6088 | 6107 | return payload_ptr; |
| 6089 | 6108 | |
| ... | ... | @@ -6161,7 +6180,7 @@ pub const FuncGen = struct { |
| 6161 | 6180 | |
| 6162 | 6181 | fn airLoop(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6163 | 6182 | const o = self.dg.object; |
| 6164 | const mod = o.module; | |
| 6183 | const mod = o.pt.zcu; | |
| 6165 | 6184 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6166 | 6185 | const loop = self.air.extraData(Air.Block, ty_pl.payload); |
| 6167 | 6186 | const body: []const Air.Inst.Index = @ptrCast(self.air.extra[loop.end..][0..loop.data.body_len]); |
| ... | ... | @@ -6185,7 +6204,8 @@ pub const FuncGen = struct { |
| 6185 | 6204 | |
| 6186 | 6205 | fn airArrayToSlice(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6187 | 6206 | const o = self.dg.object; |
| 6188 | const mod = o.module; | |
| 6207 | const pt = o.pt; | |
| 6208 | const mod = pt.zcu; | |
| 6189 | 6209 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6190 | 6210 | const operand_ty = self.typeOf(ty_op.operand); |
| 6191 | 6211 | const array_ty = operand_ty.childType(mod); |
| ... | ... | @@ -6193,7 +6213,7 @@ pub const FuncGen = struct { |
| 6193 | 6213 | const len = try o.builder.intValue(llvm_usize, array_ty.arrayLen(mod)); |
| 6194 | 6214 | const slice_llvm_ty = try o.lowerType(self.typeOfIndex(inst)); |
| 6195 | 6215 | const operand = try self.resolveInst(ty_op.operand); |
| 6196 | if (!array_ty.hasRuntimeBitsIgnoreComptime(mod)) | |
| 6216 | if (!array_ty.hasRuntimeBitsIgnoreComptime(pt)) | |
| 6197 | 6217 | return self.wip.buildAggregate(slice_llvm_ty, &.{ operand, len }, ""); |
| 6198 | 6218 | const ptr = try self.wip.gep(.inbounds, try o.lowerType(array_ty), operand, &.{ |
| 6199 | 6219 | try o.builder.intValue(llvm_usize, 0), try o.builder.intValue(llvm_usize, 0), |
| ... | ... | @@ -6203,7 +6223,8 @@ pub const FuncGen = struct { |
| 6203 | 6223 | |
| 6204 | 6224 | fn airFloatFromInt(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6205 | 6225 | const o = self.dg.object; |
| 6206 | const mod = o.module; | |
| 6226 | const pt = o.pt; | |
| 6227 | const mod = pt.zcu; | |
| 6207 | 6228 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6208 | 6229 | |
| 6209 | 6230 | const workaround_operand = try self.resolveInst(ty_op.operand); |
| ... | ... | @@ -6213,7 +6234,7 @@ pub const FuncGen = struct { |
| 6213 | 6234 | |
| 6214 | 6235 | const operand = o: { |
| 6215 | 6236 | // Work around LLVM bug. See https://github.com/ziglang/zig/issues/17381. |
| 6216 | const bit_size = operand_scalar_ty.bitSize(mod); | |
| 6237 | const bit_size = operand_scalar_ty.bitSize(pt); | |
| 6217 | 6238 | for ([_]u8{ 8, 16, 32, 64, 128 }) |b| { |
| 6218 | 6239 | if (bit_size < b) { |
| 6219 | 6240 | break :o try self.wip.cast( |
| ... | ... | @@ -6241,7 +6262,7 @@ pub const FuncGen = struct { |
| 6241 | 6262 | "", |
| 6242 | 6263 | ); |
| 6243 | 6264 | |
| 6244 | const rt_int_bits = compilerRtIntBits(@intCast(operand_scalar_ty.bitSize(mod))); | |
| 6265 | const rt_int_bits = compilerRtIntBits(@intCast(operand_scalar_ty.bitSize(pt))); | |
| 6245 | 6266 | const rt_int_ty = try o.builder.intType(rt_int_bits); |
| 6246 | 6267 | var extended = try self.wip.conv( |
| 6247 | 6268 | if (is_signed_int) .signed else .unsigned, |
| ... | ... | @@ -6287,7 +6308,8 @@ pub const FuncGen = struct { |
| 6287 | 6308 | _ = fast; |
| 6288 | 6309 | |
| 6289 | 6310 | const o = self.dg.object; |
| 6290 | const mod = o.module; | |
| 6311 | const pt = o.pt; | |
| 6312 | const mod = pt.zcu; | |
| 6291 | 6313 | const target = mod.getTarget(); |
| 6292 | 6314 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6293 | 6315 | |
| ... | ... | @@ -6309,7 +6331,7 @@ pub const FuncGen = struct { |
| 6309 | 6331 | ); |
| 6310 | 6332 | } |
| 6311 | 6333 | |
| 6312 | const rt_int_bits = compilerRtIntBits(@intCast(dest_scalar_ty.bitSize(mod))); | |
| 6334 | const rt_int_bits = compilerRtIntBits(@intCast(dest_scalar_ty.bitSize(pt))); | |
| 6313 | 6335 | const ret_ty = try o.builder.intType(rt_int_bits); |
| 6314 | 6336 | const libc_ret_ty = if (rt_int_bits == 128 and (target.os.tag == .windows and target.cpu.arch == .x86_64)) b: { |
| 6315 | 6337 | // On Windows x86-64, "ti" functions must use Vector(2, u64) instead of the standard |
| ... | ... | @@ -6348,19 +6370,20 @@ pub const FuncGen = struct { |
| 6348 | 6370 | |
| 6349 | 6371 | fn sliceOrArrayPtr(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.Error!Builder.Value { |
| 6350 | 6372 | const o = fg.dg.object; |
| 6351 | const mod = o.module; | |
| 6373 | const mod = o.pt.zcu; | |
| 6352 | 6374 | return if (ty.isSlice(mod)) fg.wip.extractValue(ptr, &.{0}, "") else ptr; |
| 6353 | 6375 | } |
| 6354 | 6376 | |
| 6355 | 6377 | fn sliceOrArrayLenInBytes(fg: *FuncGen, ptr: Builder.Value, ty: Type) Allocator.Error!Builder.Value { |
| 6356 | 6378 | const o = fg.dg.object; |
| 6357 | const mod = o.module; | |
| 6379 | const pt = o.pt; | |
| 6380 | const mod = pt.zcu; | |
| 6358 | 6381 | const llvm_usize = try o.lowerType(Type.usize); |
| 6359 | 6382 | switch (ty.ptrSize(mod)) { |
| 6360 | 6383 | .Slice => { |
| 6361 | 6384 | const len = try fg.wip.extractValue(ptr, &.{1}, ""); |
| 6362 | 6385 | const elem_ty = ty.childType(mod); |
| 6363 | const abi_size = elem_ty.abiSize(mod); | |
| 6386 | const abi_size = elem_ty.abiSize(pt); | |
| 6364 | 6387 | if (abi_size == 1) return len; |
| 6365 | 6388 | const abi_size_llvm_val = try o.builder.intValue(llvm_usize, abi_size); |
| 6366 | 6389 | return fg.wip.bin(.@"mul nuw", len, abi_size_llvm_val, ""); |
| ... | ... | @@ -6368,7 +6391,7 @@ pub const FuncGen = struct { |
| 6368 | 6391 | .One => { |
| 6369 | 6392 | const array_ty = ty.childType(mod); |
| 6370 | 6393 | const elem_ty = array_ty.childType(mod); |
| 6371 | const abi_size = elem_ty.abiSize(mod); | |
| 6394 | const abi_size = elem_ty.abiSize(pt); | |
| 6372 | 6395 | return o.builder.intValue(llvm_usize, array_ty.arrayLen(mod) * abi_size); |
| 6373 | 6396 | }, |
| 6374 | 6397 | .Many, .C => unreachable, |
| ... | ... | @@ -6383,7 +6406,7 @@ pub const FuncGen = struct { |
| 6383 | 6406 | |
| 6384 | 6407 | fn airPtrSliceFieldPtr(self: *FuncGen, inst: Air.Inst.Index, index: c_uint) !Builder.Value { |
| 6385 | 6408 | const o = self.dg.object; |
| 6386 | const mod = o.module; | |
| 6409 | const mod = o.pt.zcu; | |
| 6387 | 6410 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6388 | 6411 | const slice_ptr = try self.resolveInst(ty_op.operand); |
| 6389 | 6412 | const slice_ptr_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -6394,7 +6417,8 @@ pub const FuncGen = struct { |
| 6394 | 6417 | |
| 6395 | 6418 | fn airSliceElemVal(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 6396 | 6419 | const o = self.dg.object; |
| 6397 | const mod = o.module; | |
| 6420 | const pt = o.pt; | |
| 6421 | const mod = pt.zcu; | |
| 6398 | 6422 | const inst = body_tail[0]; |
| 6399 | 6423 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6400 | 6424 | const slice_ty = self.typeOf(bin_op.lhs); |
| ... | ... | @@ -6404,11 +6428,11 @@ pub const FuncGen = struct { |
| 6404 | 6428 | const llvm_elem_ty = try o.lowerPtrElemTy(elem_ty); |
| 6405 | 6429 | const base_ptr = try self.wip.extractValue(slice, &.{0}, ""); |
| 6406 | 6430 | const ptr = try self.wip.gep(.inbounds, llvm_elem_ty, base_ptr, &.{index}, ""); |
| 6407 | if (isByRef(elem_ty, mod)) { | |
| 6431 | if (isByRef(elem_ty, pt)) { | |
| 6408 | 6432 | if (self.canElideLoad(body_tail)) |
| 6409 | 6433 | return ptr; |
| 6410 | 6434 | |
| 6411 | const elem_alignment = elem_ty.abiAlignment(mod).toLlvm(); | |
| 6435 | const elem_alignment = elem_ty.abiAlignment(pt).toLlvm(); | |
| 6412 | 6436 | return self.loadByRef(ptr, elem_ty, elem_alignment, .normal); |
| 6413 | 6437 | } |
| 6414 | 6438 | |
| ... | ... | @@ -6417,7 +6441,7 @@ pub const FuncGen = struct { |
| 6417 | 6441 | |
| 6418 | 6442 | fn airSliceElemPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6419 | 6443 | const o = self.dg.object; |
| 6420 | const mod = o.module; | |
| 6444 | const mod = o.pt.zcu; | |
| 6421 | 6445 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6422 | 6446 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 6423 | 6447 | const slice_ty = self.typeOf(bin_op.lhs); |
| ... | ... | @@ -6431,7 +6455,8 @@ pub const FuncGen = struct { |
| 6431 | 6455 | |
| 6432 | 6456 | fn airArrayElemVal(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 6433 | 6457 | const o = self.dg.object; |
| 6434 | const mod = o.module; | |
| 6458 | const pt = o.pt; | |
| 6459 | const mod = pt.zcu; | |
| 6435 | 6460 | const inst = body_tail[0]; |
| 6436 | 6461 | |
| 6437 | 6462 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| ... | ... | @@ -6440,15 +6465,15 @@ pub const FuncGen = struct { |
| 6440 | 6465 | const rhs = try self.resolveInst(bin_op.rhs); |
| 6441 | 6466 | const array_llvm_ty = try o.lowerType(array_ty); |
| 6442 | 6467 | const elem_ty = array_ty.childType(mod); |
| 6443 | if (isByRef(array_ty, mod)) { | |
| 6468 | if (isByRef(array_ty, pt)) { | |
| 6444 | 6469 | const indices: [2]Builder.Value = .{ |
| 6445 | 6470 | try o.builder.intValue(try o.lowerType(Type.usize), 0), rhs, |
| 6446 | 6471 | }; |
| 6447 | if (isByRef(elem_ty, mod)) { | |
| 6472 | if (isByRef(elem_ty, pt)) { | |
| 6448 | 6473 | const elem_ptr = |
| 6449 | 6474 | try self.wip.gep(.inbounds, array_llvm_ty, array_llvm_val, &indices, ""); |
| 6450 | 6475 | if (canElideLoad(self, body_tail)) return elem_ptr; |
| 6451 | const elem_alignment = elem_ty.abiAlignment(mod).toLlvm(); | |
| 6476 | const elem_alignment = elem_ty.abiAlignment(pt).toLlvm(); | |
| 6452 | 6477 | return self.loadByRef(elem_ptr, elem_ty, elem_alignment, .normal); |
| 6453 | 6478 | } else { |
| 6454 | 6479 | const elem_ptr = |
| ... | ... | @@ -6463,7 +6488,8 @@ pub const FuncGen = struct { |
| 6463 | 6488 | |
| 6464 | 6489 | fn airPtrElemVal(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 6465 | 6490 | const o = self.dg.object; |
| 6466 | const mod = o.module; | |
| 6491 | const pt = o.pt; | |
| 6492 | const mod = pt.zcu; | |
| 6467 | 6493 | const inst = body_tail[0]; |
| 6468 | 6494 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 6469 | 6495 | const ptr_ty = self.typeOf(bin_op.lhs); |
| ... | ... | @@ -6477,9 +6503,9 @@ pub const FuncGen = struct { |
| 6477 | 6503 | &.{ try o.builder.intValue(try o.lowerType(Type.usize), 0), rhs } |
| 6478 | 6504 | else |
| 6479 | 6505 | &.{rhs}, ""); |
| 6480 | if (isByRef(elem_ty, mod)) { | |
| 6506 | if (isByRef(elem_ty, pt)) { | |
| 6481 | 6507 | if (self.canElideLoad(body_tail)) return ptr; |
| 6482 | const elem_alignment = elem_ty.abiAlignment(mod).toLlvm(); | |
| 6508 | const elem_alignment = elem_ty.abiAlignment(pt).toLlvm(); | |
| 6483 | 6509 | return self.loadByRef(ptr, elem_ty, elem_alignment, .normal); |
| 6484 | 6510 | } |
| 6485 | 6511 | |
| ... | ... | @@ -6488,12 +6514,13 @@ pub const FuncGen = struct { |
| 6488 | 6514 | |
| 6489 | 6515 | fn airPtrElemPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6490 | 6516 | const o = self.dg.object; |
| 6491 | const mod = o.module; | |
| 6517 | const pt = o.pt; | |
| 6518 | const mod = pt.zcu; | |
| 6492 | 6519 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6493 | 6520 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 6494 | 6521 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 6495 | 6522 | const elem_ty = ptr_ty.childType(mod); |
| 6496 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) return self.resolveInst(bin_op.lhs); | |
| 6523 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(pt)) return self.resolveInst(bin_op.lhs); | |
| 6497 | 6524 | |
| 6498 | 6525 | const base_ptr = try self.resolveInst(bin_op.lhs); |
| 6499 | 6526 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -6530,7 +6557,8 @@ pub const FuncGen = struct { |
| 6530 | 6557 | |
| 6531 | 6558 | fn airStructFieldVal(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 6532 | 6559 | const o = self.dg.object; |
| 6533 | const mod = o.module; | |
| 6560 | const pt = o.pt; | |
| 6561 | const mod = pt.zcu; | |
| 6534 | 6562 | const inst = body_tail[0]; |
| 6535 | 6563 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6536 | 6564 | const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data; |
| ... | ... | @@ -6538,27 +6566,27 @@ pub const FuncGen = struct { |
| 6538 | 6566 | const struct_llvm_val = try self.resolveInst(struct_field.struct_operand); |
| 6539 | 6567 | const field_index = struct_field.field_index; |
| 6540 | 6568 | const field_ty = struct_ty.structFieldType(field_index, mod); |
| 6541 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) return .none; | |
| 6569 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) return .none; | |
| 6542 | 6570 | |
| 6543 | if (!isByRef(struct_ty, mod)) { | |
| 6544 | assert(!isByRef(field_ty, mod)); | |
| 6571 | if (!isByRef(struct_ty, pt)) { | |
| 6572 | assert(!isByRef(field_ty, pt)); | |
| 6545 | 6573 | switch (struct_ty.zigTypeTag(mod)) { |
| 6546 | 6574 | .Struct => switch (struct_ty.containerLayout(mod)) { |
| 6547 | 6575 | .@"packed" => { |
| 6548 | 6576 | const struct_type = mod.typeToStruct(struct_ty).?; |
| 6549 | const bit_offset = mod.structPackedFieldBitOffset(struct_type, field_index); | |
| 6577 | const bit_offset = pt.structPackedFieldBitOffset(struct_type, field_index); | |
| 6550 | 6578 | const containing_int = struct_llvm_val; |
| 6551 | 6579 | const shift_amt = |
| 6552 | 6580 | try o.builder.intValue(containing_int.typeOfWip(&self.wip), bit_offset); |
| 6553 | 6581 | const shifted_value = try self.wip.bin(.lshr, containing_int, shift_amt, ""); |
| 6554 | 6582 | const elem_llvm_ty = try o.lowerType(field_ty); |
| 6555 | 6583 | if (field_ty.zigTypeTag(mod) == .Float or field_ty.zigTypeTag(mod) == .Vector) { |
| 6556 | const same_size_int = try o.builder.intType(@intCast(field_ty.bitSize(mod))); | |
| 6584 | const same_size_int = try o.builder.intType(@intCast(field_ty.bitSize(pt))); | |
| 6557 | 6585 | const truncated_int = |
| 6558 | 6586 | try self.wip.cast(.trunc, shifted_value, same_size_int, ""); |
| 6559 | 6587 | return self.wip.cast(.bitcast, truncated_int, elem_llvm_ty, ""); |
| 6560 | 6588 | } else if (field_ty.isPtrAtRuntime(mod)) { |
| 6561 | const same_size_int = try o.builder.intType(@intCast(field_ty.bitSize(mod))); | |
| 6589 | const same_size_int = try o.builder.intType(@intCast(field_ty.bitSize(pt))); | |
| 6562 | 6590 | const truncated_int = |
| 6563 | 6591 | try self.wip.cast(.trunc, shifted_value, same_size_int, ""); |
| 6564 | 6592 | return self.wip.cast(.inttoptr, truncated_int, elem_llvm_ty, ""); |
| ... | ... | @@ -6575,12 +6603,12 @@ pub const FuncGen = struct { |
| 6575 | 6603 | const containing_int = struct_llvm_val; |
| 6576 | 6604 | const elem_llvm_ty = try o.lowerType(field_ty); |
| 6577 | 6605 | if (field_ty.zigTypeTag(mod) == .Float or field_ty.zigTypeTag(mod) == .Vector) { |
| 6578 | const same_size_int = try o.builder.intType(@intCast(field_ty.bitSize(mod))); | |
| 6606 | const same_size_int = try o.builder.intType(@intCast(field_ty.bitSize(pt))); | |
| 6579 | 6607 | const truncated_int = |
| 6580 | 6608 | try self.wip.cast(.trunc, containing_int, same_size_int, ""); |
| 6581 | 6609 | return self.wip.cast(.bitcast, truncated_int, elem_llvm_ty, ""); |
| 6582 | 6610 | } else if (field_ty.isPtrAtRuntime(mod)) { |
| 6583 | const same_size_int = try o.builder.intType(@intCast(field_ty.bitSize(mod))); | |
| 6611 | const same_size_int = try o.builder.intType(@intCast(field_ty.bitSize(pt))); | |
| 6584 | 6612 | const truncated_int = |
| 6585 | 6613 | try self.wip.cast(.trunc, containing_int, same_size_int, ""); |
| 6586 | 6614 | return self.wip.cast(.inttoptr, truncated_int, elem_llvm_ty, ""); |
| ... | ... | @@ -6599,12 +6627,12 @@ pub const FuncGen = struct { |
| 6599 | 6627 | const llvm_field_index = o.llvmFieldIndex(struct_ty, field_index).?; |
| 6600 | 6628 | const field_ptr = |
| 6601 | 6629 | try self.wip.gepStruct(struct_llvm_ty, struct_llvm_val, llvm_field_index, ""); |
| 6602 | const alignment = struct_ty.structFieldAlign(field_index, mod); | |
| 6603 | const field_ptr_ty = try mod.ptrType(.{ | |
| 6630 | const alignment = struct_ty.structFieldAlign(field_index, pt); | |
| 6631 | const field_ptr_ty = try pt.ptrType(.{ | |
| 6604 | 6632 | .child = field_ty.toIntern(), |
| 6605 | 6633 | .flags = .{ .alignment = alignment }, |
| 6606 | 6634 | }); |
| 6607 | if (isByRef(field_ty, mod)) { | |
| 6635 | if (isByRef(field_ty, pt)) { | |
| 6608 | 6636 | if (canElideLoad(self, body_tail)) |
| 6609 | 6637 | return field_ptr; |
| 6610 | 6638 | |
| ... | ... | @@ -6617,12 +6645,12 @@ pub const FuncGen = struct { |
| 6617 | 6645 | }, |
| 6618 | 6646 | .Union => { |
| 6619 | 6647 | const union_llvm_ty = try o.lowerType(struct_ty); |
| 6620 | const layout = struct_ty.unionGetLayout(mod); | |
| 6648 | const layout = struct_ty.unionGetLayout(pt); | |
| 6621 | 6649 | const payload_index = @intFromBool(layout.tag_align.compare(.gte, layout.payload_align)); |
| 6622 | 6650 | const field_ptr = |
| 6623 | 6651 | try self.wip.gepStruct(union_llvm_ty, struct_llvm_val, payload_index, ""); |
| 6624 | 6652 | const payload_alignment = layout.payload_align.toLlvm(); |
| 6625 | if (isByRef(field_ty, mod)) { | |
| 6653 | if (isByRef(field_ty, pt)) { | |
| 6626 | 6654 | if (canElideLoad(self, body_tail)) return field_ptr; |
| 6627 | 6655 | return self.loadByRef(field_ptr, field_ty, payload_alignment, .normal); |
| 6628 | 6656 | } else { |
| ... | ... | @@ -6635,14 +6663,15 @@ pub const FuncGen = struct { |
| 6635 | 6663 | |
| 6636 | 6664 | fn airFieldParentPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6637 | 6665 | const o = self.dg.object; |
| 6638 | const mod = o.module; | |
| 6666 | const pt = o.pt; | |
| 6667 | const mod = pt.zcu; | |
| 6639 | 6668 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6640 | 6669 | const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| 6641 | 6670 | |
| 6642 | 6671 | const field_ptr = try self.resolveInst(extra.field_ptr); |
| 6643 | 6672 | |
| 6644 | 6673 | const parent_ty = ty_pl.ty.toType().childType(mod); |
| 6645 | const field_offset = parent_ty.structFieldOffset(extra.field_index, mod); | |
| 6674 | const field_offset = parent_ty.structFieldOffset(extra.field_index, pt); | |
| 6646 | 6675 | if (field_offset == 0) return field_ptr; |
| 6647 | 6676 | |
| 6648 | 6677 | const res_ty = try o.lowerType(ty_pl.ty.toType()); |
| ... | ... | @@ -6696,7 +6725,7 @@ pub const FuncGen = struct { |
| 6696 | 6725 | |
| 6697 | 6726 | fn airDbgVarPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 6698 | 6727 | const o = self.dg.object; |
| 6699 | const mod = o.module; | |
| 6728 | const mod = o.pt.zcu; | |
| 6700 | 6729 | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6701 | 6730 | const operand = try self.resolveInst(pl_op.operand); |
| 6702 | 6731 | const name = self.air.nullTerminatedString(pl_op.payload); |
| ... | ... | @@ -6743,9 +6772,9 @@ pub const FuncGen = struct { |
| 6743 | 6772 | try o.lowerDebugType(operand_ty), |
| 6744 | 6773 | ); |
| 6745 | 6774 | |
| 6746 | const zcu = o.module; | |
| 6775 | const pt = o.pt; | |
| 6747 | 6776 | const owner_mod = self.dg.ownerModule(); |
| 6748 | if (isByRef(operand_ty, zcu)) { | |
| 6777 | if (isByRef(operand_ty, pt)) { | |
| 6749 | 6778 | _ = try self.wip.callIntrinsic( |
| 6750 | 6779 | .normal, |
| 6751 | 6780 | .none, |
| ... | ... | @@ -6759,7 +6788,7 @@ pub const FuncGen = struct { |
| 6759 | 6788 | "", |
| 6760 | 6789 | ); |
| 6761 | 6790 | } else if (owner_mod.optimize_mode == .Debug) { |
| 6762 | const alignment = operand_ty.abiAlignment(zcu).toLlvm(); | |
| 6791 | const alignment = operand_ty.abiAlignment(pt).toLlvm(); | |
| 6763 | 6792 | const alloca = try self.buildAlloca(operand.typeOfWip(&self.wip), alignment); |
| 6764 | 6793 | _ = try self.wip.store(.normal, operand, alloca, alignment); |
| 6765 | 6794 | _ = try self.wip.callIntrinsic( |
| ... | ... | @@ -6830,7 +6859,8 @@ pub const FuncGen = struct { |
| 6830 | 6859 | // This stores whether we need to add an elementtype attribute and |
| 6831 | 6860 | // if so, the element type itself. |
| 6832 | 6861 | const llvm_param_attrs = try arena.alloc(Builder.Type, max_param_count); |
| 6833 | const mod = o.module; | |
| 6862 | const pt = o.pt; | |
| 6863 | const mod = pt.zcu; | |
| 6834 | 6864 | const target = mod.getTarget(); |
| 6835 | 6865 | |
| 6836 | 6866 | var llvm_ret_i: usize = 0; |
| ... | ... | @@ -6930,13 +6960,13 @@ pub const FuncGen = struct { |
| 6930 | 6960 | |
| 6931 | 6961 | const arg_llvm_value = try self.resolveInst(input); |
| 6932 | 6962 | const arg_ty = self.typeOf(input); |
| 6933 | const is_by_ref = isByRef(arg_ty, mod); | |
| 6963 | const is_by_ref = isByRef(arg_ty, pt); | |
| 6934 | 6964 | if (is_by_ref) { |
| 6935 | 6965 | if (constraintAllowsMemory(constraint)) { |
| 6936 | 6966 | llvm_param_values[llvm_param_i] = arg_llvm_value; |
| 6937 | 6967 | llvm_param_types[llvm_param_i] = arg_llvm_value.typeOfWip(&self.wip); |
| 6938 | 6968 | } else { |
| 6939 | const alignment = arg_ty.abiAlignment(mod).toLlvm(); | |
| 6969 | const alignment = arg_ty.abiAlignment(pt).toLlvm(); | |
| 6940 | 6970 | const arg_llvm_ty = try o.lowerType(arg_ty); |
| 6941 | 6971 | const load_inst = |
| 6942 | 6972 | try self.wip.load(.normal, arg_llvm_ty, arg_llvm_value, alignment, ""); |
| ... | ... | @@ -6948,7 +6978,7 @@ pub const FuncGen = struct { |
| 6948 | 6978 | llvm_param_values[llvm_param_i] = arg_llvm_value; |
| 6949 | 6979 | llvm_param_types[llvm_param_i] = arg_llvm_value.typeOfWip(&self.wip); |
| 6950 | 6980 | } else { |
| 6951 | const alignment = arg_ty.abiAlignment(mod).toLlvm(); | |
| 6981 | const alignment = arg_ty.abiAlignment(pt).toLlvm(); | |
| 6952 | 6982 | const arg_ptr = try self.buildAlloca(arg_llvm_value.typeOfWip(&self.wip), alignment); |
| 6953 | 6983 | _ = try self.wip.store(.normal, arg_llvm_value, arg_ptr, alignment); |
| 6954 | 6984 | llvm_param_values[llvm_param_i] = arg_ptr; |
| ... | ... | @@ -7000,7 +7030,7 @@ pub const FuncGen = struct { |
| 7000 | 7030 | llvm_param_values[llvm_param_i] = llvm_rw_val; |
| 7001 | 7031 | llvm_param_types[llvm_param_i] = llvm_rw_val.typeOfWip(&self.wip); |
| 7002 | 7032 | } else { |
| 7003 | const alignment = rw_ty.abiAlignment(mod).toLlvm(); | |
| 7033 | const alignment = rw_ty.abiAlignment(pt).toLlvm(); | |
| 7004 | 7034 | const loaded = try self.wip.load(.normal, llvm_elem_ty, llvm_rw_val, alignment, ""); |
| 7005 | 7035 | llvm_param_values[llvm_param_i] = loaded; |
| 7006 | 7036 | llvm_param_types[llvm_param_i] = llvm_elem_ty; |
| ... | ... | @@ -7161,7 +7191,7 @@ pub const FuncGen = struct { |
| 7161 | 7191 | const output_ptr = try self.resolveInst(output); |
| 7162 | 7192 | const output_ptr_ty = self.typeOf(output); |
| 7163 | 7193 | |
| 7164 | const alignment = output_ptr_ty.ptrAlignment(mod).toLlvm(); | |
| 7194 | const alignment = output_ptr_ty.ptrAlignment(pt).toLlvm(); | |
| 7165 | 7195 | _ = try self.wip.store(.normal, output_value, output_ptr, alignment); |
| 7166 | 7196 | } else { |
| 7167 | 7197 | ret_val = output_value; |
| ... | ... | @@ -7179,7 +7209,8 @@ pub const FuncGen = struct { |
| 7179 | 7209 | cond: Builder.IntegerCondition, |
| 7180 | 7210 | ) !Builder.Value { |
| 7181 | 7211 | const o = self.dg.object; |
| 7182 | const mod = o.module; | |
| 7212 | const pt = o.pt; | |
| 7213 | const mod = pt.zcu; | |
| 7183 | 7214 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 7184 | 7215 | const operand = try self.resolveInst(un_op); |
| 7185 | 7216 | const operand_ty = self.typeOf(un_op); |
| ... | ... | @@ -7204,7 +7235,7 @@ pub const FuncGen = struct { |
| 7204 | 7235 | |
| 7205 | 7236 | comptime assert(optional_layout_version == 3); |
| 7206 | 7237 | |
| 7207 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 7238 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 7208 | 7239 | const loaded = if (operand_is_ptr) |
| 7209 | 7240 | try self.wip.load(.normal, optional_llvm_ty, operand, .default, "") |
| 7210 | 7241 | else |
| ... | ... | @@ -7212,7 +7243,7 @@ pub const FuncGen = struct { |
| 7212 | 7243 | return self.wip.icmp(cond, loaded, try o.builder.intValue(.i8, 0), ""); |
| 7213 | 7244 | } |
| 7214 | 7245 | |
| 7215 | const is_by_ref = operand_is_ptr or isByRef(optional_ty, mod); | |
| 7246 | const is_by_ref = operand_is_ptr or isByRef(optional_ty, pt); | |
| 7216 | 7247 | return self.optCmpNull(cond, optional_llvm_ty, operand, is_by_ref); |
| 7217 | 7248 | } |
| 7218 | 7249 | |
| ... | ... | @@ -7223,7 +7254,8 @@ pub const FuncGen = struct { |
| 7223 | 7254 | operand_is_ptr: bool, |
| 7224 | 7255 | ) !Builder.Value { |
| 7225 | 7256 | const o = self.dg.object; |
| 7226 | const mod = o.module; | |
| 7257 | const pt = o.pt; | |
| 7258 | const mod = pt.zcu; | |
| 7227 | 7259 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 7228 | 7260 | const operand = try self.resolveInst(un_op); |
| 7229 | 7261 | const operand_ty = self.typeOf(un_op); |
| ... | ... | @@ -7241,7 +7273,7 @@ pub const FuncGen = struct { |
| 7241 | 7273 | return val.toValue(); |
| 7242 | 7274 | } |
| 7243 | 7275 | |
| 7244 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 7276 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 7245 | 7277 | const loaded = if (operand_is_ptr) |
| 7246 | 7278 | try self.wip.load(.normal, try o.lowerType(err_union_ty), operand, .default, "") |
| 7247 | 7279 | else |
| ... | ... | @@ -7249,9 +7281,9 @@ pub const FuncGen = struct { |
| 7249 | 7281 | return self.wip.icmp(cond, loaded, zero, ""); |
| 7250 | 7282 | } |
| 7251 | 7283 | |
| 7252 | const err_field_index = try errUnionErrorOffset(payload_ty, mod); | |
| 7284 | const err_field_index = try errUnionErrorOffset(payload_ty, pt); | |
| 7253 | 7285 | |
| 7254 | const loaded = if (operand_is_ptr or isByRef(err_union_ty, mod)) loaded: { | |
| 7286 | const loaded = if (operand_is_ptr or isByRef(err_union_ty, pt)) loaded: { | |
| 7255 | 7287 | const err_union_llvm_ty = try o.lowerType(err_union_ty); |
| 7256 | 7288 | const err_field_ptr = |
| 7257 | 7289 | try self.wip.gepStruct(err_union_llvm_ty, operand, err_field_index, ""); |
| ... | ... | @@ -7262,12 +7294,13 @@ pub const FuncGen = struct { |
| 7262 | 7294 | |
| 7263 | 7295 | fn airOptionalPayloadPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7264 | 7296 | const o = self.dg.object; |
| 7265 | const mod = o.module; | |
| 7297 | const pt = o.pt; | |
| 7298 | const mod = pt.zcu; | |
| 7266 | 7299 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7267 | 7300 | const operand = try self.resolveInst(ty_op.operand); |
| 7268 | 7301 | const optional_ty = self.typeOf(ty_op.operand).childType(mod); |
| 7269 | 7302 | const payload_ty = optional_ty.optionalChild(mod); |
| 7270 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 7303 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 7271 | 7304 | // We have a pointer to a zero-bit value and we need to return |
| 7272 | 7305 | // a pointer to a zero-bit value. |
| 7273 | 7306 | return operand; |
| ... | ... | @@ -7283,13 +7316,14 @@ pub const FuncGen = struct { |
| 7283 | 7316 | comptime assert(optional_layout_version == 3); |
| 7284 | 7317 | |
| 7285 | 7318 | const o = self.dg.object; |
| 7286 | const mod = o.module; | |
| 7319 | const pt = o.pt; | |
| 7320 | const mod = pt.zcu; | |
| 7287 | 7321 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7288 | 7322 | const operand = try self.resolveInst(ty_op.operand); |
| 7289 | 7323 | const optional_ty = self.typeOf(ty_op.operand).childType(mod); |
| 7290 | 7324 | const payload_ty = optional_ty.optionalChild(mod); |
| 7291 | 7325 | const non_null_bit = try o.builder.intValue(.i8, 1); |
| 7292 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 7326 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 7293 | 7327 | // We have a pointer to a i8. We need to set it to 1 and then return the same pointer. |
| 7294 | 7328 | _ = try self.wip.store(.normal, non_null_bit, operand, .default); |
| 7295 | 7329 | return operand; |
| ... | ... | @@ -7314,13 +7348,14 @@ pub const FuncGen = struct { |
| 7314 | 7348 | |
| 7315 | 7349 | fn airOptionalPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 7316 | 7350 | const o = self.dg.object; |
| 7317 | const mod = o.module; | |
| 7351 | const pt = o.pt; | |
| 7352 | const mod = pt.zcu; | |
| 7318 | 7353 | const inst = body_tail[0]; |
| 7319 | 7354 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7320 | 7355 | const operand = try self.resolveInst(ty_op.operand); |
| 7321 | 7356 | const optional_ty = self.typeOf(ty_op.operand); |
| 7322 | 7357 | const payload_ty = self.typeOfIndex(inst); |
| 7323 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) return .none; | |
| 7358 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) return .none; | |
| 7324 | 7359 | |
| 7325 | 7360 | if (optional_ty.optionalReprIsPayload(mod)) { |
| 7326 | 7361 | // Payload value is the same as the optional value. |
| ... | ... | @@ -7328,7 +7363,7 @@ pub const FuncGen = struct { |
| 7328 | 7363 | } |
| 7329 | 7364 | |
| 7330 | 7365 | const opt_llvm_ty = try o.lowerType(optional_ty); |
| 7331 | const can_elide_load = if (isByRef(payload_ty, mod)) self.canElideLoad(body_tail) else false; | |
| 7366 | const can_elide_load = if (isByRef(payload_ty, pt)) self.canElideLoad(body_tail) else false; | |
| 7332 | 7367 | return self.optPayloadHandle(opt_llvm_ty, operand, optional_ty, can_elide_load); |
| 7333 | 7368 | } |
| 7334 | 7369 | |
| ... | ... | @@ -7338,7 +7373,8 @@ pub const FuncGen = struct { |
| 7338 | 7373 | operand_is_ptr: bool, |
| 7339 | 7374 | ) !Builder.Value { |
| 7340 | 7375 | const o = self.dg.object; |
| 7341 | const mod = o.module; | |
| 7376 | const pt = o.pt; | |
| 7377 | const mod = pt.zcu; | |
| 7342 | 7378 | const inst = body_tail[0]; |
| 7343 | 7379 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7344 | 7380 | const operand = try self.resolveInst(ty_op.operand); |
| ... | ... | @@ -7347,17 +7383,17 @@ pub const FuncGen = struct { |
| 7347 | 7383 | const result_ty = self.typeOfIndex(inst); |
| 7348 | 7384 | const payload_ty = if (operand_is_ptr) result_ty.childType(mod) else result_ty; |
| 7349 | 7385 | |
| 7350 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 7386 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 7351 | 7387 | return if (operand_is_ptr) operand else .none; |
| 7352 | 7388 | } |
| 7353 | const offset = try errUnionPayloadOffset(payload_ty, mod); | |
| 7389 | const offset = try errUnionPayloadOffset(payload_ty, pt); | |
| 7354 | 7390 | const err_union_llvm_ty = try o.lowerType(err_union_ty); |
| 7355 | 7391 | if (operand_is_ptr) { |
| 7356 | 7392 | return self.wip.gepStruct(err_union_llvm_ty, operand, offset, ""); |
| 7357 | } else if (isByRef(err_union_ty, mod)) { | |
| 7358 | const payload_alignment = payload_ty.abiAlignment(mod).toLlvm(); | |
| 7393 | } else if (isByRef(err_union_ty, pt)) { | |
| 7394 | const payload_alignment = payload_ty.abiAlignment(pt).toLlvm(); | |
| 7359 | 7395 | const payload_ptr = try self.wip.gepStruct(err_union_llvm_ty, operand, offset, ""); |
| 7360 | if (isByRef(payload_ty, mod)) { | |
| 7396 | if (isByRef(payload_ty, pt)) { | |
| 7361 | 7397 | if (self.canElideLoad(body_tail)) return payload_ptr; |
| 7362 | 7398 | return self.loadByRef(payload_ptr, payload_ty, payload_alignment, .normal); |
| 7363 | 7399 | } |
| ... | ... | @@ -7373,7 +7409,8 @@ pub const FuncGen = struct { |
| 7373 | 7409 | operand_is_ptr: bool, |
| 7374 | 7410 | ) !Builder.Value { |
| 7375 | 7411 | const o = self.dg.object; |
| 7376 | const mod = o.module; | |
| 7412 | const pt = o.pt; | |
| 7413 | const mod = pt.zcu; | |
| 7377 | 7414 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7378 | 7415 | const operand = try self.resolveInst(ty_op.operand); |
| 7379 | 7416 | const operand_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -7388,14 +7425,14 @@ pub const FuncGen = struct { |
| 7388 | 7425 | } |
| 7389 | 7426 | |
| 7390 | 7427 | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| 7391 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 7428 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 7392 | 7429 | if (!operand_is_ptr) return operand; |
| 7393 | 7430 | return self.wip.load(.normal, error_type, operand, .default, ""); |
| 7394 | 7431 | } |
| 7395 | 7432 | |
| 7396 | const offset = try errUnionErrorOffset(payload_ty, mod); | |
| 7433 | const offset = try errUnionErrorOffset(payload_ty, pt); | |
| 7397 | 7434 | |
| 7398 | if (operand_is_ptr or isByRef(err_union_ty, mod)) { | |
| 7435 | if (operand_is_ptr or isByRef(err_union_ty, pt)) { | |
| 7399 | 7436 | const err_union_llvm_ty = try o.lowerType(err_union_ty); |
| 7400 | 7437 | const err_field_ptr = try self.wip.gepStruct(err_union_llvm_ty, operand, offset, ""); |
| 7401 | 7438 | return self.wip.load(.normal, error_type, err_field_ptr, .default, ""); |
| ... | ... | @@ -7406,22 +7443,23 @@ pub const FuncGen = struct { |
| 7406 | 7443 | |
| 7407 | 7444 | fn airErrUnionPayloadPtrSet(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7408 | 7445 | const o = self.dg.object; |
| 7409 | const mod = o.module; | |
| 7446 | const pt = o.pt; | |
| 7447 | const mod = pt.zcu; | |
| 7410 | 7448 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7411 | 7449 | const operand = try self.resolveInst(ty_op.operand); |
| 7412 | 7450 | const err_union_ty = self.typeOf(ty_op.operand).childType(mod); |
| 7413 | 7451 | |
| 7414 | 7452 | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| 7415 | 7453 | const non_error_val = try o.builder.intValue(try o.errorIntType(), 0); |
| 7416 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 7454 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 7417 | 7455 | _ = try self.wip.store(.normal, non_error_val, operand, .default); |
| 7418 | 7456 | return operand; |
| 7419 | 7457 | } |
| 7420 | 7458 | const err_union_llvm_ty = try o.lowerType(err_union_ty); |
| 7421 | 7459 | { |
| 7422 | const err_int_ty = try mod.errorIntType(); | |
| 7423 | const error_alignment = err_int_ty.abiAlignment(mod).toLlvm(); | |
| 7424 | const error_offset = try errUnionErrorOffset(payload_ty, mod); | |
| 7460 | const err_int_ty = try pt.errorIntType(); | |
| 7461 | const error_alignment = err_int_ty.abiAlignment(pt).toLlvm(); | |
| 7462 | const error_offset = try errUnionErrorOffset(payload_ty, pt); | |
| 7425 | 7463 | // First set the non-error value. |
| 7426 | 7464 | const non_null_ptr = try self.wip.gepStruct(err_union_llvm_ty, operand, error_offset, ""); |
| 7427 | 7465 | _ = try self.wip.store(.normal, non_error_val, non_null_ptr, error_alignment); |
| ... | ... | @@ -7429,7 +7467,7 @@ pub const FuncGen = struct { |
| 7429 | 7467 | // Then return the payload pointer (only if it is used). |
| 7430 | 7468 | if (self.liveness.isUnused(inst)) return .none; |
| 7431 | 7469 | |
| 7432 | const payload_offset = try errUnionPayloadOffset(payload_ty, mod); | |
| 7470 | const payload_offset = try errUnionPayloadOffset(payload_ty, pt); | |
| 7433 | 7471 | return self.wip.gepStruct(err_union_llvm_ty, operand, payload_offset, ""); |
| 7434 | 7472 | } |
| 7435 | 7473 | |
| ... | ... | @@ -7446,19 +7484,21 @@ pub const FuncGen = struct { |
| 7446 | 7484 | |
| 7447 | 7485 | fn airSaveErrReturnTraceIndex(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7448 | 7486 | const o = self.dg.object; |
| 7487 | const pt = o.pt; | |
| 7488 | const mod = pt.zcu; | |
| 7489 | ||
| 7449 | 7490 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 7450 | 7491 | const struct_ty = ty_pl.ty.toType(); |
| 7451 | 7492 | const field_index = ty_pl.payload; |
| 7452 | 7493 | |
| 7453 | const mod = o.module; | |
| 7454 | 7494 | const struct_llvm_ty = try o.lowerType(struct_ty); |
| 7455 | 7495 | const llvm_field_index = o.llvmFieldIndex(struct_ty, field_index).?; |
| 7456 | 7496 | assert(self.err_ret_trace != .none); |
| 7457 | 7497 | const field_ptr = |
| 7458 | 7498 | try self.wip.gepStruct(struct_llvm_ty, self.err_ret_trace, llvm_field_index, ""); |
| 7459 | const field_alignment = struct_ty.structFieldAlign(field_index, mod); | |
| 7499 | const field_alignment = struct_ty.structFieldAlign(field_index, pt); | |
| 7460 | 7500 | const field_ty = struct_ty.structFieldType(field_index, mod); |
| 7461 | const field_ptr_ty = try mod.ptrType(.{ | |
| 7501 | const field_ptr_ty = try pt.ptrType(.{ | |
| 7462 | 7502 | .child = field_ty.toIntern(), |
| 7463 | 7503 | .flags = .{ .alignment = field_alignment }, |
| 7464 | 7504 | }); |
| ... | ... | @@ -7490,29 +7530,30 @@ pub const FuncGen = struct { |
| 7490 | 7530 | |
| 7491 | 7531 | fn airWrapOptional(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 7492 | 7532 | const o = self.dg.object; |
| 7493 | const mod = o.module; | |
| 7533 | const pt = o.pt; | |
| 7534 | const mod = pt.zcu; | |
| 7494 | 7535 | const inst = body_tail[0]; |
| 7495 | 7536 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7496 | 7537 | const payload_ty = self.typeOf(ty_op.operand); |
| 7497 | 7538 | const non_null_bit = try o.builder.intValue(.i8, 1); |
| 7498 | 7539 | comptime assert(optional_layout_version == 3); |
| 7499 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) return non_null_bit; | |
| 7540 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) return non_null_bit; | |
| 7500 | 7541 | const operand = try self.resolveInst(ty_op.operand); |
| 7501 | 7542 | const optional_ty = self.typeOfIndex(inst); |
| 7502 | 7543 | if (optional_ty.optionalReprIsPayload(mod)) return operand; |
| 7503 | 7544 | const llvm_optional_ty = try o.lowerType(optional_ty); |
| 7504 | if (isByRef(optional_ty, mod)) { | |
| 7545 | if (isByRef(optional_ty, pt)) { | |
| 7505 | 7546 | const directReturn = self.isNextRet(body_tail); |
| 7506 | 7547 | const optional_ptr = if (directReturn) |
| 7507 | 7548 | self.ret_ptr |
| 7508 | 7549 | else brk: { |
| 7509 | const alignment = optional_ty.abiAlignment(mod).toLlvm(); | |
| 7550 | const alignment = optional_ty.abiAlignment(pt).toLlvm(); | |
| 7510 | 7551 | const optional_ptr = try self.buildAllocaWorkaround(optional_ty, alignment); |
| 7511 | 7552 | break :brk optional_ptr; |
| 7512 | 7553 | }; |
| 7513 | 7554 | |
| 7514 | 7555 | const payload_ptr = try self.wip.gepStruct(llvm_optional_ty, optional_ptr, 0, ""); |
| 7515 | const payload_ptr_ty = try mod.singleMutPtrType(payload_ty); | |
| 7556 | const payload_ptr_ty = try pt.singleMutPtrType(payload_ty); | |
| 7516 | 7557 | try self.store(payload_ptr, payload_ptr_ty, operand, .none); |
| 7517 | 7558 | const non_null_ptr = try self.wip.gepStruct(llvm_optional_ty, optional_ptr, 1, ""); |
| 7518 | 7559 | _ = try self.wip.store(.normal, non_null_bit, non_null_ptr, .default); |
| ... | ... | @@ -7523,36 +7564,36 @@ pub const FuncGen = struct { |
| 7523 | 7564 | |
| 7524 | 7565 | fn airWrapErrUnionPayload(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 7525 | 7566 | const o = self.dg.object; |
| 7526 | const mod = o.module; | |
| 7567 | const pt = o.pt; | |
| 7527 | 7568 | const inst = body_tail[0]; |
| 7528 | 7569 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7529 | 7570 | const err_un_ty = self.typeOfIndex(inst); |
| 7530 | 7571 | const operand = try self.resolveInst(ty_op.operand); |
| 7531 | 7572 | const payload_ty = self.typeOf(ty_op.operand); |
| 7532 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 7573 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 7533 | 7574 | return operand; |
| 7534 | 7575 | } |
| 7535 | 7576 | const ok_err_code = try o.builder.intValue(try o.errorIntType(), 0); |
| 7536 | 7577 | const err_un_llvm_ty = try o.lowerType(err_un_ty); |
| 7537 | 7578 | |
| 7538 | const payload_offset = try errUnionPayloadOffset(payload_ty, mod); | |
| 7539 | const error_offset = try errUnionErrorOffset(payload_ty, mod); | |
| 7540 | if (isByRef(err_un_ty, mod)) { | |
| 7579 | const payload_offset = try errUnionPayloadOffset(payload_ty, pt); | |
| 7580 | const error_offset = try errUnionErrorOffset(payload_ty, pt); | |
| 7581 | if (isByRef(err_un_ty, pt)) { | |
| 7541 | 7582 | const directReturn = self.isNextRet(body_tail); |
| 7542 | 7583 | const result_ptr = if (directReturn) |
| 7543 | 7584 | self.ret_ptr |
| 7544 | 7585 | else brk: { |
| 7545 | const alignment = err_un_ty.abiAlignment(mod).toLlvm(); | |
| 7586 | const alignment = err_un_ty.abiAlignment(pt).toLlvm(); | |
| 7546 | 7587 | const result_ptr = try self.buildAllocaWorkaround(err_un_ty, alignment); |
| 7547 | 7588 | break :brk result_ptr; |
| 7548 | 7589 | }; |
| 7549 | 7590 | |
| 7550 | 7591 | const err_ptr = try self.wip.gepStruct(err_un_llvm_ty, result_ptr, error_offset, ""); |
| 7551 | const err_int_ty = try mod.errorIntType(); | |
| 7552 | const error_alignment = err_int_ty.abiAlignment(mod).toLlvm(); | |
| 7592 | const err_int_ty = try pt.errorIntType(); | |
| 7593 | const error_alignment = err_int_ty.abiAlignment(pt).toLlvm(); | |
| 7553 | 7594 | _ = try self.wip.store(.normal, ok_err_code, err_ptr, error_alignment); |
| 7554 | 7595 | const payload_ptr = try self.wip.gepStruct(err_un_llvm_ty, result_ptr, payload_offset, ""); |
| 7555 | const payload_ptr_ty = try mod.singleMutPtrType(payload_ty); | |
| 7596 | const payload_ptr_ty = try pt.singleMutPtrType(payload_ty); | |
| 7556 | 7597 | try self.store(payload_ptr, payload_ptr_ty, operand, .none); |
| 7557 | 7598 | return result_ptr; |
| 7558 | 7599 | } |
| ... | ... | @@ -7564,33 +7605,34 @@ pub const FuncGen = struct { |
| 7564 | 7605 | |
| 7565 | 7606 | fn airWrapErrUnionErr(self: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 7566 | 7607 | const o = self.dg.object; |
| 7567 | const mod = o.module; | |
| 7608 | const pt = o.pt; | |
| 7609 | const mod = pt.zcu; | |
| 7568 | 7610 | const inst = body_tail[0]; |
| 7569 | 7611 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 7570 | 7612 | const err_un_ty = self.typeOfIndex(inst); |
| 7571 | 7613 | const payload_ty = err_un_ty.errorUnionPayload(mod); |
| 7572 | 7614 | const operand = try self.resolveInst(ty_op.operand); |
| 7573 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) return operand; | |
| 7615 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) return operand; | |
| 7574 | 7616 | const err_un_llvm_ty = try o.lowerType(err_un_ty); |
| 7575 | 7617 | |
| 7576 | const payload_offset = try errUnionPayloadOffset(payload_ty, mod); | |
| 7577 | const error_offset = try errUnionErrorOffset(payload_ty, mod); | |
| 7578 | if (isByRef(err_un_ty, mod)) { | |
| 7618 | const payload_offset = try errUnionPayloadOffset(payload_ty, pt); | |
| 7619 | const error_offset = try errUnionErrorOffset(payload_ty, pt); | |
| 7620 | if (isByRef(err_un_ty, pt)) { | |
| 7579 | 7621 | const directReturn = self.isNextRet(body_tail); |
| 7580 | 7622 | const result_ptr = if (directReturn) |
| 7581 | 7623 | self.ret_ptr |
| 7582 | 7624 | else brk: { |
| 7583 | const alignment = err_un_ty.abiAlignment(mod).toLlvm(); | |
| 7625 | const alignment = err_un_ty.abiAlignment(pt).toLlvm(); | |
| 7584 | 7626 | const result_ptr = try self.buildAllocaWorkaround(err_un_ty, alignment); |
| 7585 | 7627 | break :brk result_ptr; |
| 7586 | 7628 | }; |
| 7587 | 7629 | |
| 7588 | 7630 | const err_ptr = try self.wip.gepStruct(err_un_llvm_ty, result_ptr, error_offset, ""); |
| 7589 | const err_int_ty = try mod.errorIntType(); | |
| 7590 | const error_alignment = err_int_ty.abiAlignment(mod).toLlvm(); | |
| 7631 | const err_int_ty = try pt.errorIntType(); | |
| 7632 | const error_alignment = err_int_ty.abiAlignment(pt).toLlvm(); | |
| 7591 | 7633 | _ = try self.wip.store(.normal, operand, err_ptr, error_alignment); |
| 7592 | 7634 | const payload_ptr = try self.wip.gepStruct(err_un_llvm_ty, result_ptr, payload_offset, ""); |
| 7593 | const payload_ptr_ty = try mod.singleMutPtrType(payload_ty); | |
| 7635 | const payload_ptr_ty = try pt.singleMutPtrType(payload_ty); | |
| 7594 | 7636 | // TODO store undef to payload_ptr |
| 7595 | 7637 | _ = payload_ptr; |
| 7596 | 7638 | _ = payload_ptr_ty; |
| ... | ... | @@ -7624,7 +7666,8 @@ pub const FuncGen = struct { |
| 7624 | 7666 | |
| 7625 | 7667 | fn airVectorStoreElem(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7626 | 7668 | const o = self.dg.object; |
| 7627 | const mod = o.module; | |
| 7669 | const pt = o.pt; | |
| 7670 | const mod = pt.zcu; | |
| 7628 | 7671 | const data = self.air.instructions.items(.data)[@intFromEnum(inst)].vector_store_elem; |
| 7629 | 7672 | const extra = self.air.extraData(Air.Bin, data.payload).data; |
| 7630 | 7673 | |
| ... | ... | @@ -7636,7 +7679,7 @@ pub const FuncGen = struct { |
| 7636 | 7679 | const access_kind: Builder.MemoryAccessKind = |
| 7637 | 7680 | if (vector_ptr_ty.isVolatilePtr(mod)) .@"volatile" else .normal; |
| 7638 | 7681 | const elem_llvm_ty = try o.lowerType(vector_ptr_ty.childType(mod)); |
| 7639 | const alignment = vector_ptr_ty.ptrAlignment(mod).toLlvm(); | |
| 7682 | const alignment = vector_ptr_ty.ptrAlignment(pt).toLlvm(); | |
| 7640 | 7683 | const loaded = try self.wip.load(access_kind, elem_llvm_ty, vector_ptr, alignment, ""); |
| 7641 | 7684 | |
| 7642 | 7685 | const new_vector = try self.wip.insertElement(loaded, operand, index, ""); |
| ... | ... | @@ -7646,7 +7689,7 @@ pub const FuncGen = struct { |
| 7646 | 7689 | |
| 7647 | 7690 | fn airMin(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7648 | 7691 | const o = self.dg.object; |
| 7649 | const mod = o.module; | |
| 7692 | const mod = o.pt.zcu; | |
| 7650 | 7693 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7651 | 7694 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7652 | 7695 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -7666,7 +7709,7 @@ pub const FuncGen = struct { |
| 7666 | 7709 | |
| 7667 | 7710 | fn airMax(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7668 | 7711 | const o = self.dg.object; |
| 7669 | const mod = o.module; | |
| 7712 | const mod = o.pt.zcu; | |
| 7670 | 7713 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7671 | 7714 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7672 | 7715 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -7696,7 +7739,7 @@ pub const FuncGen = struct { |
| 7696 | 7739 | |
| 7697 | 7740 | fn airAdd(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value { |
| 7698 | 7741 | const o = self.dg.object; |
| 7699 | const mod = o.module; | |
| 7742 | const mod = o.pt.zcu; | |
| 7700 | 7743 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7701 | 7744 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7702 | 7745 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -7714,7 +7757,7 @@ pub const FuncGen = struct { |
| 7714 | 7757 | unsigned_intrinsic: Builder.Intrinsic, |
| 7715 | 7758 | ) !Builder.Value { |
| 7716 | 7759 | const o = fg.dg.object; |
| 7717 | const mod = o.module; | |
| 7760 | const mod = o.pt.zcu; | |
| 7718 | 7761 | |
| 7719 | 7762 | const bin_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7720 | 7763 | const lhs = try fg.resolveInst(bin_op.lhs); |
| ... | ... | @@ -7762,7 +7805,7 @@ pub const FuncGen = struct { |
| 7762 | 7805 | |
| 7763 | 7806 | fn airAddSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7764 | 7807 | const o = self.dg.object; |
| 7765 | const mod = o.module; | |
| 7808 | const mod = o.pt.zcu; | |
| 7766 | 7809 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7767 | 7810 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7768 | 7811 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -7782,7 +7825,7 @@ pub const FuncGen = struct { |
| 7782 | 7825 | |
| 7783 | 7826 | fn airSub(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value { |
| 7784 | 7827 | const o = self.dg.object; |
| 7785 | const mod = o.module; | |
| 7828 | const mod = o.pt.zcu; | |
| 7786 | 7829 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7787 | 7830 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7788 | 7831 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -7803,7 +7846,7 @@ pub const FuncGen = struct { |
| 7803 | 7846 | |
| 7804 | 7847 | fn airSubSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7805 | 7848 | const o = self.dg.object; |
| 7806 | const mod = o.module; | |
| 7849 | const mod = o.pt.zcu; | |
| 7807 | 7850 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7808 | 7851 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7809 | 7852 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -7823,7 +7866,7 @@ pub const FuncGen = struct { |
| 7823 | 7866 | |
| 7824 | 7867 | fn airMul(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value { |
| 7825 | 7868 | const o = self.dg.object; |
| 7826 | const mod = o.module; | |
| 7869 | const mod = o.pt.zcu; | |
| 7827 | 7870 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7828 | 7871 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7829 | 7872 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -7844,7 +7887,7 @@ pub const FuncGen = struct { |
| 7844 | 7887 | |
| 7845 | 7888 | fn airMulSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7846 | 7889 | const o = self.dg.object; |
| 7847 | const mod = o.module; | |
| 7890 | const mod = o.pt.zcu; | |
| 7848 | 7891 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7849 | 7892 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7850 | 7893 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -7873,7 +7916,7 @@ pub const FuncGen = struct { |
| 7873 | 7916 | |
| 7874 | 7917 | fn airDivTrunc(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value { |
| 7875 | 7918 | const o = self.dg.object; |
| 7876 | const mod = o.module; | |
| 7919 | const mod = o.pt.zcu; | |
| 7877 | 7920 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7878 | 7921 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7879 | 7922 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -7889,7 +7932,7 @@ pub const FuncGen = struct { |
| 7889 | 7932 | |
| 7890 | 7933 | fn airDivFloor(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value { |
| 7891 | 7934 | const o = self.dg.object; |
| 7892 | const mod = o.module; | |
| 7935 | const mod = o.pt.zcu; | |
| 7893 | 7936 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7894 | 7937 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7895 | 7938 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -7921,7 +7964,7 @@ pub const FuncGen = struct { |
| 7921 | 7964 | |
| 7922 | 7965 | fn airDivExact(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value { |
| 7923 | 7966 | const o = self.dg.object; |
| 7924 | const mod = o.module; | |
| 7967 | const mod = o.pt.zcu; | |
| 7925 | 7968 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7926 | 7969 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7927 | 7970 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -7939,7 +7982,7 @@ pub const FuncGen = struct { |
| 7939 | 7982 | |
| 7940 | 7983 | fn airRem(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value { |
| 7941 | 7984 | const o = self.dg.object; |
| 7942 | const mod = o.module; | |
| 7985 | const mod = o.pt.zcu; | |
| 7943 | 7986 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7944 | 7987 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7945 | 7988 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -7956,7 +7999,7 @@ pub const FuncGen = struct { |
| 7956 | 7999 | |
| 7957 | 8000 | fn airMod(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value { |
| 7958 | 8001 | const o = self.dg.object; |
| 7959 | const mod = o.module; | |
| 8002 | const mod = o.pt.zcu; | |
| 7960 | 8003 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 7961 | 8004 | const lhs = try self.resolveInst(bin_op.lhs); |
| 7962 | 8005 | const rhs = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -7992,7 +8035,7 @@ pub const FuncGen = struct { |
| 7992 | 8035 | |
| 7993 | 8036 | fn airPtrAdd(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 7994 | 8037 | const o = self.dg.object; |
| 7995 | const mod = o.module; | |
| 8038 | const mod = o.pt.zcu; | |
| 7996 | 8039 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 7997 | 8040 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 7998 | 8041 | const ptr = try self.resolveInst(bin_op.lhs); |
| ... | ... | @@ -8014,7 +8057,7 @@ pub const FuncGen = struct { |
| 8014 | 8057 | |
| 8015 | 8058 | fn airPtrSub(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8016 | 8059 | const o = self.dg.object; |
| 8017 | const mod = o.module; | |
| 8060 | const mod = o.pt.zcu; | |
| 8018 | 8061 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 8019 | 8062 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 8020 | 8063 | const ptr = try self.resolveInst(bin_op.lhs); |
| ... | ... | @@ -8042,7 +8085,8 @@ pub const FuncGen = struct { |
| 8042 | 8085 | unsigned_intrinsic: Builder.Intrinsic, |
| 8043 | 8086 | ) !Builder.Value { |
| 8044 | 8087 | const o = self.dg.object; |
| 8045 | const mod = o.module; | |
| 8088 | const pt = o.pt; | |
| 8089 | const mod = pt.zcu; | |
| 8046 | 8090 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 8047 | 8091 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 8048 | 8092 | |
| ... | ... | @@ -8065,8 +8109,8 @@ pub const FuncGen = struct { |
| 8065 | 8109 | const result_index = o.llvmFieldIndex(inst_ty, 0).?; |
| 8066 | 8110 | const overflow_index = o.llvmFieldIndex(inst_ty, 1).?; |
| 8067 | 8111 | |
| 8068 | if (isByRef(inst_ty, mod)) { | |
| 8069 | const result_alignment = inst_ty.abiAlignment(mod).toLlvm(); | |
| 8112 | if (isByRef(inst_ty, pt)) { | |
| 8113 | const result_alignment = inst_ty.abiAlignment(pt).toLlvm(); | |
| 8070 | 8114 | const alloca_inst = try self.buildAllocaWorkaround(inst_ty, result_alignment); |
| 8071 | 8115 | { |
| 8072 | 8116 | const field_ptr = try self.wip.gepStruct(llvm_inst_ty, alloca_inst, result_index, ""); |
| ... | ... | @@ -8135,7 +8179,7 @@ pub const FuncGen = struct { |
| 8135 | 8179 | return o.builder.addFunction( |
| 8136 | 8180 | try o.builder.fnType(return_type, param_types, .normal), |
| 8137 | 8181 | fn_name, |
| 8138 | toLlvmAddressSpace(.generic, o.module.getTarget()), | |
| 8182 | toLlvmAddressSpace(.generic, o.pt.zcu.getTarget()), | |
| 8139 | 8183 | ); |
| 8140 | 8184 | } |
| 8141 | 8185 | |
| ... | ... | @@ -8149,8 +8193,8 @@ pub const FuncGen = struct { |
| 8149 | 8193 | params: [2]Builder.Value, |
| 8150 | 8194 | ) !Builder.Value { |
| 8151 | 8195 | const o = self.dg.object; |
| 8152 | const mod = o.module; | |
| 8153 | const target = o.module.getTarget(); | |
| 8196 | const mod = o.pt.zcu; | |
| 8197 | const target = mod.getTarget(); | |
| 8154 | 8198 | const scalar_ty = ty.scalarType(mod); |
| 8155 | 8199 | const scalar_llvm_ty = try o.lowerType(scalar_ty); |
| 8156 | 8200 | |
| ... | ... | @@ -8255,7 +8299,7 @@ pub const FuncGen = struct { |
| 8255 | 8299 | params: [params_len]Builder.Value, |
| 8256 | 8300 | ) !Builder.Value { |
| 8257 | 8301 | const o = self.dg.object; |
| 8258 | const mod = o.module; | |
| 8302 | const mod = o.pt.zcu; | |
| 8259 | 8303 | const target = mod.getTarget(); |
| 8260 | 8304 | const scalar_ty = ty.scalarType(mod); |
| 8261 | 8305 | const llvm_ty = try o.lowerType(ty); |
| ... | ... | @@ -8396,7 +8440,8 @@ pub const FuncGen = struct { |
| 8396 | 8440 | |
| 8397 | 8441 | fn airShlWithOverflow(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8398 | 8442 | const o = self.dg.object; |
| 8399 | const mod = o.module; | |
| 8443 | const pt = o.pt; | |
| 8444 | const mod = pt.zcu; | |
| 8400 | 8445 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 8401 | 8446 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 8402 | 8447 | |
| ... | ... | @@ -8422,8 +8467,8 @@ pub const FuncGen = struct { |
| 8422 | 8467 | const result_index = o.llvmFieldIndex(dest_ty, 0).?; |
| 8423 | 8468 | const overflow_index = o.llvmFieldIndex(dest_ty, 1).?; |
| 8424 | 8469 | |
| 8425 | if (isByRef(dest_ty, mod)) { | |
| 8426 | const result_alignment = dest_ty.abiAlignment(mod).toLlvm(); | |
| 8470 | if (isByRef(dest_ty, pt)) { | |
| 8471 | const result_alignment = dest_ty.abiAlignment(pt).toLlvm(); | |
| 8427 | 8472 | const alloca_inst = try self.buildAllocaWorkaround(dest_ty, result_alignment); |
| 8428 | 8473 | { |
| 8429 | 8474 | const field_ptr = try self.wip.gepStruct(llvm_dest_ty, alloca_inst, result_index, ""); |
| ... | ... | @@ -8466,7 +8511,7 @@ pub const FuncGen = struct { |
| 8466 | 8511 | |
| 8467 | 8512 | fn airShlExact(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8468 | 8513 | const o = self.dg.object; |
| 8469 | const mod = o.module; | |
| 8514 | const mod = o.pt.zcu; | |
| 8470 | 8515 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 8471 | 8516 | |
| 8472 | 8517 | const lhs = try self.resolveInst(bin_op.lhs); |
| ... | ... | @@ -8497,7 +8542,8 @@ pub const FuncGen = struct { |
| 8497 | 8542 | |
| 8498 | 8543 | fn airShlSat(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8499 | 8544 | const o = self.dg.object; |
| 8500 | const mod = o.module; | |
| 8545 | const pt = o.pt; | |
| 8546 | const mod = pt.zcu; | |
| 8501 | 8547 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 8502 | 8548 | |
| 8503 | 8549 | const lhs = try self.resolveInst(bin_op.lhs); |
| ... | ... | @@ -8505,7 +8551,7 @@ pub const FuncGen = struct { |
| 8505 | 8551 | |
| 8506 | 8552 | const lhs_ty = self.typeOf(bin_op.lhs); |
| 8507 | 8553 | const lhs_scalar_ty = lhs_ty.scalarType(mod); |
| 8508 | const lhs_bits = lhs_scalar_ty.bitSize(mod); | |
| 8554 | const lhs_bits = lhs_scalar_ty.bitSize(pt); | |
| 8509 | 8555 | |
| 8510 | 8556 | const casted_rhs = try self.wip.conv(.unsigned, rhs, try o.lowerType(lhs_ty), ""); |
| 8511 | 8557 | |
| ... | ... | @@ -8539,7 +8585,7 @@ pub const FuncGen = struct { |
| 8539 | 8585 | |
| 8540 | 8586 | fn airShr(self: *FuncGen, inst: Air.Inst.Index, is_exact: bool) !Builder.Value { |
| 8541 | 8587 | const o = self.dg.object; |
| 8542 | const mod = o.module; | |
| 8588 | const mod = o.pt.zcu; | |
| 8543 | 8589 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 8544 | 8590 | |
| 8545 | 8591 | const lhs = try self.resolveInst(bin_op.lhs); |
| ... | ... | @@ -8558,7 +8604,7 @@ pub const FuncGen = struct { |
| 8558 | 8604 | |
| 8559 | 8605 | fn airAbs(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8560 | 8606 | const o = self.dg.object; |
| 8561 | const mod = o.module; | |
| 8607 | const mod = o.pt.zcu; | |
| 8562 | 8608 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 8563 | 8609 | const operand = try self.resolveInst(ty_op.operand); |
| 8564 | 8610 | const operand_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -8580,7 +8626,7 @@ pub const FuncGen = struct { |
| 8580 | 8626 | |
| 8581 | 8627 | fn airIntCast(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8582 | 8628 | const o = self.dg.object; |
| 8583 | const mod = o.module; | |
| 8629 | const mod = o.pt.zcu; | |
| 8584 | 8630 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 8585 | 8631 | const dest_ty = self.typeOfIndex(inst); |
| 8586 | 8632 | const dest_llvm_ty = try o.lowerType(dest_ty); |
| ... | ... | @@ -8604,7 +8650,7 @@ pub const FuncGen = struct { |
| 8604 | 8650 | |
| 8605 | 8651 | fn airFptrunc(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8606 | 8652 | const o = self.dg.object; |
| 8607 | const mod = o.module; | |
| 8653 | const mod = o.pt.zcu; | |
| 8608 | 8654 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 8609 | 8655 | const operand = try self.resolveInst(ty_op.operand); |
| 8610 | 8656 | const operand_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -8638,7 +8684,7 @@ pub const FuncGen = struct { |
| 8638 | 8684 | |
| 8639 | 8685 | fn airFpext(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8640 | 8686 | const o = self.dg.object; |
| 8641 | const mod = o.module; | |
| 8687 | const mod = o.pt.zcu; | |
| 8642 | 8688 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 8643 | 8689 | const operand = try self.resolveInst(ty_op.operand); |
| 8644 | 8690 | const operand_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -8696,9 +8742,10 @@ pub const FuncGen = struct { |
| 8696 | 8742 | |
| 8697 | 8743 | fn bitCast(self: *FuncGen, operand: Builder.Value, operand_ty: Type, inst_ty: Type) !Builder.Value { |
| 8698 | 8744 | const o = self.dg.object; |
| 8699 | const mod = o.module; | |
| 8700 | const operand_is_ref = isByRef(operand_ty, mod); | |
| 8701 | const result_is_ref = isByRef(inst_ty, mod); | |
| 8745 | const pt = o.pt; | |
| 8746 | const mod = pt.zcu; | |
| 8747 | const operand_is_ref = isByRef(operand_ty, pt); | |
| 8748 | const result_is_ref = isByRef(inst_ty, pt); | |
| 8702 | 8749 | const llvm_dest_ty = try o.lowerType(inst_ty); |
| 8703 | 8750 | |
| 8704 | 8751 | if (operand_is_ref and result_is_ref) { |
| ... | ... | @@ -8721,9 +8768,9 @@ pub const FuncGen = struct { |
| 8721 | 8768 | if (!result_is_ref) { |
| 8722 | 8769 | return self.dg.todo("implement bitcast vector to non-ref array", .{}); |
| 8723 | 8770 | } |
| 8724 | const alignment = inst_ty.abiAlignment(mod).toLlvm(); | |
| 8771 | const alignment = inst_ty.abiAlignment(pt).toLlvm(); | |
| 8725 | 8772 | const array_ptr = try self.buildAllocaWorkaround(inst_ty, alignment); |
| 8726 | const bitcast_ok = elem_ty.bitSize(mod) == elem_ty.abiSize(mod) * 8; | |
| 8773 | const bitcast_ok = elem_ty.bitSize(pt) == elem_ty.abiSize(pt) * 8; | |
| 8727 | 8774 | if (bitcast_ok) { |
| 8728 | 8775 | _ = try self.wip.store(.normal, operand, array_ptr, alignment); |
| 8729 | 8776 | } else { |
| ... | ... | @@ -8748,11 +8795,11 @@ pub const FuncGen = struct { |
| 8748 | 8795 | const llvm_vector_ty = try o.lowerType(inst_ty); |
| 8749 | 8796 | if (!operand_is_ref) return self.dg.todo("implement bitcast non-ref array to vector", .{}); |
| 8750 | 8797 | |
| 8751 | const bitcast_ok = elem_ty.bitSize(mod) == elem_ty.abiSize(mod) * 8; | |
| 8798 | const bitcast_ok = elem_ty.bitSize(pt) == elem_ty.abiSize(pt) * 8; | |
| 8752 | 8799 | if (bitcast_ok) { |
| 8753 | 8800 | // The array is aligned to the element's alignment, while the vector might have a completely |
| 8754 | 8801 | // different alignment. This means we need to enforce the alignment of this load. |
| 8755 | const alignment = elem_ty.abiAlignment(mod).toLlvm(); | |
| 8802 | const alignment = elem_ty.abiAlignment(pt).toLlvm(); | |
| 8756 | 8803 | return self.wip.load(.normal, llvm_vector_ty, operand, alignment, ""); |
| 8757 | 8804 | } else { |
| 8758 | 8805 | // If the ABI size of the element type is not evenly divisible by size in bits; |
| ... | ... | @@ -8777,24 +8824,25 @@ pub const FuncGen = struct { |
| 8777 | 8824 | } |
| 8778 | 8825 | |
| 8779 | 8826 | if (operand_is_ref) { |
| 8780 | const alignment = operand_ty.abiAlignment(mod).toLlvm(); | |
| 8827 | const alignment = operand_ty.abiAlignment(pt).toLlvm(); | |
| 8781 | 8828 | return self.wip.load(.normal, llvm_dest_ty, operand, alignment, ""); |
| 8782 | 8829 | } |
| 8783 | 8830 | |
| 8784 | 8831 | if (result_is_ref) { |
| 8785 | const alignment = operand_ty.abiAlignment(mod).max(inst_ty.abiAlignment(mod)).toLlvm(); | |
| 8832 | const alignment = operand_ty.abiAlignment(pt).max(inst_ty.abiAlignment(pt)).toLlvm(); | |
| 8786 | 8833 | const result_ptr = try self.buildAllocaWorkaround(inst_ty, alignment); |
| 8787 | 8834 | _ = try self.wip.store(.normal, operand, result_ptr, alignment); |
| 8788 | 8835 | return result_ptr; |
| 8789 | 8836 | } |
| 8790 | 8837 | |
| 8791 | 8838 | if (llvm_dest_ty.isStruct(&o.builder) or |
| 8792 | ((operand_ty.zigTypeTag(mod) == .Vector or inst_ty.zigTypeTag(mod) == .Vector) and operand_ty.bitSize(mod) != inst_ty.bitSize(mod))) | |
| 8839 | ((operand_ty.zigTypeTag(mod) == .Vector or inst_ty.zigTypeTag(mod) == .Vector) and | |
| 8840 | operand_ty.bitSize(pt) != inst_ty.bitSize(pt))) | |
| 8793 | 8841 | { |
| 8794 | 8842 | // Both our operand and our result are values, not pointers, |
| 8795 | 8843 | // but LLVM won't let us bitcast struct values or vectors with padding bits. |
| 8796 | 8844 | // Therefore, we store operand to alloca, then load for result. |
| 8797 | const alignment = operand_ty.abiAlignment(mod).max(inst_ty.abiAlignment(mod)).toLlvm(); | |
| 8845 | const alignment = operand_ty.abiAlignment(pt).max(inst_ty.abiAlignment(pt)).toLlvm(); | |
| 8798 | 8846 | const result_ptr = try self.buildAllocaWorkaround(inst_ty, alignment); |
| 8799 | 8847 | _ = try self.wip.store(.normal, operand, result_ptr, alignment); |
| 8800 | 8848 | return self.wip.load(.normal, llvm_dest_ty, result_ptr, alignment, ""); |
| ... | ... | @@ -8811,7 +8859,8 @@ pub const FuncGen = struct { |
| 8811 | 8859 | |
| 8812 | 8860 | fn airArg(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8813 | 8861 | const o = self.dg.object; |
| 8814 | const mod = o.module; | |
| 8862 | const pt = o.pt; | |
| 8863 | const mod = pt.zcu; | |
| 8815 | 8864 | const arg_val = self.args[self.arg_index]; |
| 8816 | 8865 | self.arg_index += 1; |
| 8817 | 8866 | |
| ... | ... | @@ -8847,7 +8896,7 @@ pub const FuncGen = struct { |
| 8847 | 8896 | }; |
| 8848 | 8897 | |
| 8849 | 8898 | const owner_mod = self.dg.ownerModule(); |
| 8850 | if (isByRef(inst_ty, mod)) { | |
| 8899 | if (isByRef(inst_ty, pt)) { | |
| 8851 | 8900 | _ = try self.wip.callIntrinsic( |
| 8852 | 8901 | .normal, |
| 8853 | 8902 | .none, |
| ... | ... | @@ -8861,7 +8910,7 @@ pub const FuncGen = struct { |
| 8861 | 8910 | "", |
| 8862 | 8911 | ); |
| 8863 | 8912 | } else if (owner_mod.optimize_mode == .Debug) { |
| 8864 | const alignment = inst_ty.abiAlignment(mod).toLlvm(); | |
| 8913 | const alignment = inst_ty.abiAlignment(pt).toLlvm(); | |
| 8865 | 8914 | const alloca = try self.buildAlloca(arg_val.typeOfWip(&self.wip), alignment); |
| 8866 | 8915 | _ = try self.wip.store(.normal, arg_val, alloca, alignment); |
| 8867 | 8916 | _ = try self.wip.callIntrinsic( |
| ... | ... | @@ -8897,27 +8946,29 @@ pub const FuncGen = struct { |
| 8897 | 8946 | |
| 8898 | 8947 | fn airAlloc(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8899 | 8948 | const o = self.dg.object; |
| 8900 | const mod = o.module; | |
| 8949 | const pt = o.pt; | |
| 8950 | const mod = pt.zcu; | |
| 8901 | 8951 | const ptr_ty = self.typeOfIndex(inst); |
| 8902 | 8952 | const pointee_type = ptr_ty.childType(mod); |
| 8903 | if (!pointee_type.isFnOrHasRuntimeBitsIgnoreComptime(mod)) | |
| 8953 | if (!pointee_type.isFnOrHasRuntimeBitsIgnoreComptime(pt)) | |
| 8904 | 8954 | return (try o.lowerPtrToVoid(ptr_ty)).toValue(); |
| 8905 | 8955 | |
| 8906 | 8956 | //const pointee_llvm_ty = try o.lowerType(pointee_type); |
| 8907 | const alignment = ptr_ty.ptrAlignment(mod).toLlvm(); | |
| 8957 | const alignment = ptr_ty.ptrAlignment(pt).toLlvm(); | |
| 8908 | 8958 | return self.buildAllocaWorkaround(pointee_type, alignment); |
| 8909 | 8959 | } |
| 8910 | 8960 | |
| 8911 | 8961 | fn airRetPtr(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 8912 | 8962 | const o = self.dg.object; |
| 8913 | const mod = o.module; | |
| 8963 | const pt = o.pt; | |
| 8964 | const mod = pt.zcu; | |
| 8914 | 8965 | const ptr_ty = self.typeOfIndex(inst); |
| 8915 | 8966 | const ret_ty = ptr_ty.childType(mod); |
| 8916 | if (!ret_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) | |
| 8967 | if (!ret_ty.isFnOrHasRuntimeBitsIgnoreComptime(pt)) | |
| 8917 | 8968 | return (try o.lowerPtrToVoid(ptr_ty)).toValue(); |
| 8918 | 8969 | if (self.ret_ptr != .none) return self.ret_ptr; |
| 8919 | 8970 | //const ret_llvm_ty = try o.lowerType(ret_ty); |
| 8920 | const alignment = ptr_ty.ptrAlignment(mod).toLlvm(); | |
| 8971 | const alignment = ptr_ty.ptrAlignment(pt).toLlvm(); | |
| 8921 | 8972 | return self.buildAllocaWorkaround(ret_ty, alignment); |
| 8922 | 8973 | } |
| 8923 | 8974 | |
| ... | ... | @@ -8928,7 +8979,7 @@ pub const FuncGen = struct { |
| 8928 | 8979 | llvm_ty: Builder.Type, |
| 8929 | 8980 | alignment: Builder.Alignment, |
| 8930 | 8981 | ) Allocator.Error!Builder.Value { |
| 8931 | const target = self.dg.object.module.getTarget(); | |
| 8982 | const target = self.dg.object.pt.zcu.getTarget(); | |
| 8932 | 8983 | return buildAllocaInner(&self.wip, llvm_ty, alignment, target); |
| 8933 | 8984 | } |
| 8934 | 8985 | |
| ... | ... | @@ -8939,18 +8990,19 @@ pub const FuncGen = struct { |
| 8939 | 8990 | alignment: Builder.Alignment, |
| 8940 | 8991 | ) Allocator.Error!Builder.Value { |
| 8941 | 8992 | const o = self.dg.object; |
| 8942 | return self.buildAlloca(try o.builder.arrayType(ty.abiSize(o.module), .i8), alignment); | |
| 8993 | return self.buildAlloca(try o.builder.arrayType(ty.abiSize(o.pt), .i8), alignment); | |
| 8943 | 8994 | } |
| 8944 | 8995 | |
| 8945 | 8996 | fn airStore(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value { |
| 8946 | 8997 | const o = self.dg.object; |
| 8947 | const mod = o.module; | |
| 8998 | const pt = o.pt; | |
| 8999 | const mod = pt.zcu; | |
| 8948 | 9000 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 8949 | 9001 | const dest_ptr = try self.resolveInst(bin_op.lhs); |
| 8950 | 9002 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 8951 | 9003 | const operand_ty = ptr_ty.childType(mod); |
| 8952 | 9004 | |
| 8953 | const val_is_undef = if (try self.air.value(bin_op.rhs, mod)) |val| val.isUndefDeep(mod) else false; | |
| 9005 | const val_is_undef = if (try self.air.value(bin_op.rhs, pt)) |val| val.isUndefDeep(mod) else false; | |
| 8954 | 9006 | if (val_is_undef) { |
| 8955 | 9007 | const ptr_info = ptr_ty.ptrInfo(mod); |
| 8956 | 9008 | const needs_bitmask = (ptr_info.packed_offset.host_size != 0); |
| ... | ... | @@ -8964,10 +9016,10 @@ pub const FuncGen = struct { |
| 8964 | 9016 | // Even if safety is disabled, we still emit a memset to undefined since it conveys |
| 8965 | 9017 | // extra information to LLVM. However, safety makes the difference between using |
| 8966 | 9018 | // 0xaa or actual undefined for the fill byte. |
| 8967 | const len = try o.builder.intValue(try o.lowerType(Type.usize), operand_ty.abiSize(mod)); | |
| 9019 | const len = try o.builder.intValue(try o.lowerType(Type.usize), operand_ty.abiSize(pt)); | |
| 8968 | 9020 | _ = try self.wip.callMemSet( |
| 8969 | 9021 | dest_ptr, |
| 8970 | ptr_ty.ptrAlignment(mod).toLlvm(), | |
| 9022 | ptr_ty.ptrAlignment(pt).toLlvm(), | |
| 8971 | 9023 | if (safety) try o.builder.intValue(.i8, 0xaa) else try o.builder.undefValue(.i8), |
| 8972 | 9024 | len, |
| 8973 | 9025 | if (ptr_ty.isVolatilePtr(mod)) .@"volatile" else .normal, |
| ... | ... | @@ -8992,7 +9044,7 @@ pub const FuncGen = struct { |
| 8992 | 9044 | /// The first instruction of `body_tail` is the one whose copy we want to elide. |
| 8993 | 9045 | fn canElideLoad(fg: *FuncGen, body_tail: []const Air.Inst.Index) bool { |
| 8994 | 9046 | const o = fg.dg.object; |
| 8995 | const mod = o.module; | |
| 9047 | const mod = o.pt.zcu; | |
| 8996 | 9048 | const ip = &mod.intern_pool; |
| 8997 | 9049 | for (body_tail[1..]) |body_inst| { |
| 8998 | 9050 | switch (fg.liveness.categorizeOperand(fg.air, body_inst, body_tail[0], ip)) { |
| ... | ... | @@ -9008,7 +9060,8 @@ pub const FuncGen = struct { |
| 9008 | 9060 | |
| 9009 | 9061 | fn airLoad(fg: *FuncGen, body_tail: []const Air.Inst.Index) !Builder.Value { |
| 9010 | 9062 | const o = fg.dg.object; |
| 9011 | const mod = o.module; | |
| 9063 | const pt = o.pt; | |
| 9064 | const mod = pt.zcu; | |
| 9012 | 9065 | const inst = body_tail[0]; |
| 9013 | 9066 | const ty_op = fg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 9014 | 9067 | const ptr_ty = fg.typeOf(ty_op.operand); |
| ... | ... | @@ -9016,7 +9069,7 @@ pub const FuncGen = struct { |
| 9016 | 9069 | const ptr = try fg.resolveInst(ty_op.operand); |
| 9017 | 9070 | |
| 9018 | 9071 | elide: { |
| 9019 | if (!isByRef(Type.fromInterned(ptr_info.child), mod)) break :elide; | |
| 9072 | if (!isByRef(Type.fromInterned(ptr_info.child), pt)) break :elide; | |
| 9020 | 9073 | if (!canElideLoad(fg, body_tail)) break :elide; |
| 9021 | 9074 | return ptr; |
| 9022 | 9075 | } |
| ... | ... | @@ -9040,7 +9093,7 @@ pub const FuncGen = struct { |
| 9040 | 9093 | _ = inst; |
| 9041 | 9094 | const o = self.dg.object; |
| 9042 | 9095 | const llvm_usize = try o.lowerType(Type.usize); |
| 9043 | if (!target_util.supportsReturnAddress(o.module.getTarget())) { | |
| 9096 | if (!target_util.supportsReturnAddress(o.pt.zcu.getTarget())) { | |
| 9044 | 9097 | // https://github.com/ziglang/zig/issues/11946 |
| 9045 | 9098 | return o.builder.intValue(llvm_usize, 0); |
| 9046 | 9099 | } |
| ... | ... | @@ -9068,7 +9121,8 @@ pub const FuncGen = struct { |
| 9068 | 9121 | kind: Builder.Function.Instruction.CmpXchg.Kind, |
| 9069 | 9122 | ) !Builder.Value { |
| 9070 | 9123 | const o = self.dg.object; |
| 9071 | const mod = o.module; | |
| 9124 | const pt = o.pt; | |
| 9125 | const mod = pt.zcu; | |
| 9072 | 9126 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 9073 | 9127 | const extra = self.air.extraData(Air.Cmpxchg, ty_pl.payload).data; |
| 9074 | 9128 | const ptr = try self.resolveInst(extra.ptr); |
| ... | ... | @@ -9095,7 +9149,7 @@ pub const FuncGen = struct { |
| 9095 | 9149 | self.sync_scope, |
| 9096 | 9150 | toLlvmAtomicOrdering(extra.successOrder()), |
| 9097 | 9151 | toLlvmAtomicOrdering(extra.failureOrder()), |
| 9098 | ptr_ty.ptrAlignment(mod).toLlvm(), | |
| 9152 | ptr_ty.ptrAlignment(pt).toLlvm(), | |
| 9099 | 9153 | "", |
| 9100 | 9154 | ); |
| 9101 | 9155 | |
| ... | ... | @@ -9118,7 +9172,8 @@ pub const FuncGen = struct { |
| 9118 | 9172 | |
| 9119 | 9173 | fn airAtomicRmw(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9120 | 9174 | const o = self.dg.object; |
| 9121 | const mod = o.module; | |
| 9175 | const pt = o.pt; | |
| 9176 | const mod = pt.zcu; | |
| 9122 | 9177 | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 9123 | 9178 | const extra = self.air.extraData(Air.AtomicRmw, pl_op.payload).data; |
| 9124 | 9179 | const ptr = try self.resolveInst(pl_op.operand); |
| ... | ... | @@ -9134,7 +9189,7 @@ pub const FuncGen = struct { |
| 9134 | 9189 | |
| 9135 | 9190 | const access_kind: Builder.MemoryAccessKind = |
| 9136 | 9191 | if (ptr_ty.isVolatilePtr(mod)) .@"volatile" else .normal; |
| 9137 | const ptr_alignment = ptr_ty.ptrAlignment(mod).toLlvm(); | |
| 9192 | const ptr_alignment = ptr_ty.ptrAlignment(pt).toLlvm(); | |
| 9138 | 9193 | |
| 9139 | 9194 | if (llvm_abi_ty != .none) { |
| 9140 | 9195 | // operand needs widening and truncating or bitcasting. |
| ... | ... | @@ -9181,19 +9236,20 @@ pub const FuncGen = struct { |
| 9181 | 9236 | |
| 9182 | 9237 | fn airAtomicLoad(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9183 | 9238 | const o = self.dg.object; |
| 9184 | const mod = o.module; | |
| 9239 | const pt = o.pt; | |
| 9240 | const mod = pt.zcu; | |
| 9185 | 9241 | const atomic_load = self.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load; |
| 9186 | 9242 | const ptr = try self.resolveInst(atomic_load.ptr); |
| 9187 | 9243 | const ptr_ty = self.typeOf(atomic_load.ptr); |
| 9188 | 9244 | const info = ptr_ty.ptrInfo(mod); |
| 9189 | 9245 | const elem_ty = Type.fromInterned(info.child); |
| 9190 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) return .none; | |
| 9246 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(pt)) return .none; | |
| 9191 | 9247 | const ordering = toLlvmAtomicOrdering(atomic_load.order); |
| 9192 | 9248 | const llvm_abi_ty = try o.getAtomicAbiType(elem_ty, false); |
| 9193 | 9249 | const ptr_alignment = (if (info.flags.alignment != .none) |
| 9194 | 9250 | @as(InternPool.Alignment, info.flags.alignment) |
| 9195 | 9251 | else |
| 9196 | Type.fromInterned(info.child).abiAlignment(mod)).toLlvm(); | |
| 9252 | Type.fromInterned(info.child).abiAlignment(pt)).toLlvm(); | |
| 9197 | 9253 | const access_kind: Builder.MemoryAccessKind = |
| 9198 | 9254 | if (info.flags.is_volatile) .@"volatile" else .normal; |
| 9199 | 9255 | const elem_llvm_ty = try o.lowerType(elem_ty); |
| ... | ... | @@ -9228,11 +9284,12 @@ pub const FuncGen = struct { |
| 9228 | 9284 | ordering: Builder.AtomicOrdering, |
| 9229 | 9285 | ) !Builder.Value { |
| 9230 | 9286 | const o = self.dg.object; |
| 9231 | const mod = o.module; | |
| 9287 | const pt = o.pt; | |
| 9288 | const mod = pt.zcu; | |
| 9232 | 9289 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 9233 | 9290 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 9234 | 9291 | const operand_ty = ptr_ty.childType(mod); |
| 9235 | if (!operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) return .none; | |
| 9292 | if (!operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(pt)) return .none; | |
| 9236 | 9293 | const ptr = try self.resolveInst(bin_op.lhs); |
| 9237 | 9294 | var element = try self.resolveInst(bin_op.rhs); |
| 9238 | 9295 | const llvm_abi_ty = try o.getAtomicAbiType(operand_ty, false); |
| ... | ... | @@ -9252,12 +9309,13 @@ pub const FuncGen = struct { |
| 9252 | 9309 | |
| 9253 | 9310 | fn airMemset(self: *FuncGen, inst: Air.Inst.Index, safety: bool) !Builder.Value { |
| 9254 | 9311 | const o = self.dg.object; |
| 9255 | const mod = o.module; | |
| 9312 | const pt = o.pt; | |
| 9313 | const mod = pt.zcu; | |
| 9256 | 9314 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 9257 | 9315 | const dest_slice = try self.resolveInst(bin_op.lhs); |
| 9258 | 9316 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 9259 | 9317 | const elem_ty = self.typeOf(bin_op.rhs); |
| 9260 | const dest_ptr_align = ptr_ty.ptrAlignment(mod).toLlvm(); | |
| 9318 | const dest_ptr_align = ptr_ty.ptrAlignment(pt).toLlvm(); | |
| 9261 | 9319 | const dest_ptr = try self.sliceOrArrayPtr(dest_slice, ptr_ty); |
| 9262 | 9320 | const access_kind: Builder.MemoryAccessKind = |
| 9263 | 9321 | if (ptr_ty.isVolatilePtr(mod)) .@"volatile" else .normal; |
| ... | ... | @@ -9270,7 +9328,7 @@ pub const FuncGen = struct { |
| 9270 | 9328 | ptr_ty.isSlice(mod) and |
| 9271 | 9329 | std.Target.wasm.featureSetHas(o.target.cpu.features, .bulk_memory); |
| 9272 | 9330 | |
| 9273 | if (try self.air.value(bin_op.rhs, mod)) |elem_val| { | |
| 9331 | if (try self.air.value(bin_op.rhs, pt)) |elem_val| { | |
| 9274 | 9332 | if (elem_val.isUndefDeep(mod)) { |
| 9275 | 9333 | // Even if safety is disabled, we still emit a memset to undefined since it conveys |
| 9276 | 9334 | // extra information to LLVM. However, safety makes the difference between using |
| ... | ... | @@ -9296,7 +9354,7 @@ pub const FuncGen = struct { |
| 9296 | 9354 | // repeating byte pattern, for example, `@as(u64, 0)` has a |
| 9297 | 9355 | // repeating byte pattern of 0 bytes. In such case, the memset |
| 9298 | 9356 | // intrinsic can be used. |
| 9299 | if (try elem_val.hasRepeatedByteRepr(elem_ty, mod)) |byte_val| { | |
| 9357 | if (try elem_val.hasRepeatedByteRepr(elem_ty, pt)) |byte_val| { | |
| 9300 | 9358 | const fill_byte = try o.builder.intValue(.i8, byte_val); |
| 9301 | 9359 | const len = try self.sliceOrArrayLenInBytes(dest_slice, ptr_ty); |
| 9302 | 9360 | if (intrinsic_len0_traps) { |
| ... | ... | @@ -9309,7 +9367,7 @@ pub const FuncGen = struct { |
| 9309 | 9367 | } |
| 9310 | 9368 | |
| 9311 | 9369 | const value = try self.resolveInst(bin_op.rhs); |
| 9312 | const elem_abi_size = elem_ty.abiSize(mod); | |
| 9370 | const elem_abi_size = elem_ty.abiSize(pt); | |
| 9313 | 9371 | |
| 9314 | 9372 | if (elem_abi_size == 1) { |
| 9315 | 9373 | // In this case we can take advantage of LLVM's intrinsic. |
| ... | ... | @@ -9361,9 +9419,9 @@ pub const FuncGen = struct { |
| 9361 | 9419 | _ = try self.wip.brCond(end, body_block, end_block); |
| 9362 | 9420 | |
| 9363 | 9421 | self.wip.cursor = .{ .block = body_block }; |
| 9364 | const elem_abi_align = elem_ty.abiAlignment(mod); | |
| 9422 | const elem_abi_align = elem_ty.abiAlignment(pt); | |
| 9365 | 9423 | const it_ptr_align = InternPool.Alignment.fromLlvm(dest_ptr_align).min(elem_abi_align).toLlvm(); |
| 9366 | if (isByRef(elem_ty, mod)) { | |
| 9424 | if (isByRef(elem_ty, pt)) { | |
| 9367 | 9425 | _ = try self.wip.callMemCpy( |
| 9368 | 9426 | it_ptr.toValue(), |
| 9369 | 9427 | it_ptr_align, |
| ... | ... | @@ -9405,7 +9463,8 @@ pub const FuncGen = struct { |
| 9405 | 9463 | |
| 9406 | 9464 | fn airMemcpy(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9407 | 9465 | const o = self.dg.object; |
| 9408 | const mod = o.module; | |
| 9466 | const pt = o.pt; | |
| 9467 | const mod = pt.zcu; | |
| 9409 | 9468 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 9410 | 9469 | const dest_slice = try self.resolveInst(bin_op.lhs); |
| 9411 | 9470 | const dest_ptr_ty = self.typeOf(bin_op.lhs); |
| ... | ... | @@ -9434,9 +9493,9 @@ pub const FuncGen = struct { |
| 9434 | 9493 | self.wip.cursor = .{ .block = memcpy_block }; |
| 9435 | 9494 | _ = try self.wip.callMemCpy( |
| 9436 | 9495 | dest_ptr, |
| 9437 | dest_ptr_ty.ptrAlignment(mod).toLlvm(), | |
| 9496 | dest_ptr_ty.ptrAlignment(pt).toLlvm(), | |
| 9438 | 9497 | src_ptr, |
| 9439 | src_ptr_ty.ptrAlignment(mod).toLlvm(), | |
| 9498 | src_ptr_ty.ptrAlignment(pt).toLlvm(), | |
| 9440 | 9499 | len, |
| 9441 | 9500 | access_kind, |
| 9442 | 9501 | ); |
| ... | ... | @@ -9447,9 +9506,9 @@ pub const FuncGen = struct { |
| 9447 | 9506 | |
| 9448 | 9507 | _ = try self.wip.callMemCpy( |
| 9449 | 9508 | dest_ptr, |
| 9450 | dest_ptr_ty.ptrAlignment(mod).toLlvm(), | |
| 9509 | dest_ptr_ty.ptrAlignment(pt).toLlvm(), | |
| 9451 | 9510 | src_ptr, |
| 9452 | src_ptr_ty.ptrAlignment(mod).toLlvm(), | |
| 9511 | src_ptr_ty.ptrAlignment(pt).toLlvm(), | |
| 9453 | 9512 | len, |
| 9454 | 9513 | access_kind, |
| 9455 | 9514 | ); |
| ... | ... | @@ -9458,10 +9517,11 @@ pub const FuncGen = struct { |
| 9458 | 9517 | |
| 9459 | 9518 | fn airSetUnionTag(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9460 | 9519 | const o = self.dg.object; |
| 9461 | const mod = o.module; | |
| 9520 | const pt = o.pt; | |
| 9521 | const mod = pt.zcu; | |
| 9462 | 9522 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 9463 | 9523 | const un_ty = self.typeOf(bin_op.lhs).childType(mod); |
| 9464 | const layout = un_ty.unionGetLayout(mod); | |
| 9524 | const layout = un_ty.unionGetLayout(pt); | |
| 9465 | 9525 | if (layout.tag_size == 0) return .none; |
| 9466 | 9526 | const union_ptr = try self.resolveInst(bin_op.lhs); |
| 9467 | 9527 | const new_tag = try self.resolveInst(bin_op.rhs); |
| ... | ... | @@ -9479,13 +9539,13 @@ pub const FuncGen = struct { |
| 9479 | 9539 | |
| 9480 | 9540 | fn airGetUnionTag(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9481 | 9541 | const o = self.dg.object; |
| 9482 | const mod = o.module; | |
| 9542 | const pt = o.pt; | |
| 9483 | 9543 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 9484 | 9544 | const un_ty = self.typeOf(ty_op.operand); |
| 9485 | const layout = un_ty.unionGetLayout(mod); | |
| 9545 | const layout = un_ty.unionGetLayout(pt); | |
| 9486 | 9546 | if (layout.tag_size == 0) return .none; |
| 9487 | 9547 | const union_handle = try self.resolveInst(ty_op.operand); |
| 9488 | if (isByRef(un_ty, mod)) { | |
| 9548 | if (isByRef(un_ty, pt)) { | |
| 9489 | 9549 | const llvm_un_ty = try o.lowerType(un_ty); |
| 9490 | 9550 | if (layout.payload_size == 0) |
| 9491 | 9551 | return self.wip.load(.normal, llvm_un_ty, union_handle, .default, ""); |
| ... | ... | @@ -9554,7 +9614,7 @@ pub const FuncGen = struct { |
| 9554 | 9614 | |
| 9555 | 9615 | fn airByteSwap(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9556 | 9616 | const o = self.dg.object; |
| 9557 | const mod = o.module; | |
| 9617 | const mod = o.pt.zcu; | |
| 9558 | 9618 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 9559 | 9619 | const operand_ty = self.typeOf(ty_op.operand); |
| 9560 | 9620 | var bits = operand_ty.intInfo(mod).bits; |
| ... | ... | @@ -9588,7 +9648,7 @@ pub const FuncGen = struct { |
| 9588 | 9648 | |
| 9589 | 9649 | fn airErrorSetHasValue(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9590 | 9650 | const o = self.dg.object; |
| 9591 | const mod = o.module; | |
| 9651 | const mod = o.pt.zcu; | |
| 9592 | 9652 | const ip = &mod.intern_pool; |
| 9593 | 9653 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 9594 | 9654 | const operand = try self.resolveInst(ty_op.operand); |
| ... | ... | @@ -9638,7 +9698,8 @@ pub const FuncGen = struct { |
| 9638 | 9698 | |
| 9639 | 9699 | fn getIsNamedEnumValueFunction(self: *FuncGen, enum_ty: Type) !Builder.Function.Index { |
| 9640 | 9700 | const o = self.dg.object; |
| 9641 | const zcu = o.module; | |
| 9701 | const pt = o.pt; | |
| 9702 | const zcu = pt.zcu; | |
| 9642 | 9703 | const enum_type = zcu.intern_pool.loadEnumType(enum_ty.toIntern()); |
| 9643 | 9704 | |
| 9644 | 9705 | // TODO: detect when the type changes and re-emit this function. |
| ... | ... | @@ -9678,7 +9739,7 @@ pub const FuncGen = struct { |
| 9678 | 9739 | |
| 9679 | 9740 | for (0..enum_type.names.len) |field_index| { |
| 9680 | 9741 | const this_tag_int_value = try o.lowerValue( |
| 9681 | (try zcu.enumValueFieldIndex(enum_ty, @intCast(field_index))).toIntern(), | |
| 9742 | (try pt.enumValueFieldIndex(enum_ty, @intCast(field_index))).toIntern(), | |
| 9682 | 9743 | ); |
| 9683 | 9744 | try wip_switch.addCase(this_tag_int_value, named_block, &wip); |
| 9684 | 9745 | } |
| ... | ... | @@ -9745,7 +9806,8 @@ pub const FuncGen = struct { |
| 9745 | 9806 | |
| 9746 | 9807 | fn airShuffle(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9747 | 9808 | const o = self.dg.object; |
| 9748 | const mod = o.module; | |
| 9809 | const pt = o.pt; | |
| 9810 | const mod = pt.zcu; | |
| 9749 | 9811 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 9750 | 9812 | const extra = self.air.extraData(Air.Shuffle, ty_pl.payload).data; |
| 9751 | 9813 | const a = try self.resolveInst(extra.a); |
| ... | ... | @@ -9763,11 +9825,11 @@ pub const FuncGen = struct { |
| 9763 | 9825 | defer self.gpa.free(values); |
| 9764 | 9826 | |
| 9765 | 9827 | for (values, 0..) |*val, i| { |
| 9766 | const elem = try mask.elemValue(mod, i); | |
| 9828 | const elem = try mask.elemValue(pt, i); | |
| 9767 | 9829 | if (elem.isUndef(mod)) { |
| 9768 | 9830 | val.* = try o.builder.undefConst(.i32); |
| 9769 | 9831 | } else { |
| 9770 | const int = elem.toSignedInt(mod); | |
| 9832 | const int = elem.toSignedInt(pt); | |
| 9771 | 9833 | const unsigned: u32 = @intCast(if (int >= 0) int else ~int + a_len); |
| 9772 | 9834 | val.* = try o.builder.intConst(.i32, unsigned); |
| 9773 | 9835 | } |
| ... | ... | @@ -9854,7 +9916,7 @@ pub const FuncGen = struct { |
| 9854 | 9916 | |
| 9855 | 9917 | fn airReduce(self: *FuncGen, inst: Air.Inst.Index, fast: Builder.FastMathKind) !Builder.Value { |
| 9856 | 9918 | const o = self.dg.object; |
| 9857 | const mod = o.module; | |
| 9919 | const mod = o.pt.zcu; | |
| 9858 | 9920 | const target = mod.getTarget(); |
| 9859 | 9921 | |
| 9860 | 9922 | const reduce = self.air.instructions.items(.data)[@intFromEnum(inst)].reduce; |
| ... | ... | @@ -9964,7 +10026,8 @@ pub const FuncGen = struct { |
| 9964 | 10026 | |
| 9965 | 10027 | fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 9966 | 10028 | const o = self.dg.object; |
| 9967 | const mod = o.module; | |
| 10029 | const pt = o.pt; | |
| 10030 | const mod = pt.zcu; | |
| 9968 | 10031 | const ip = &mod.intern_pool; |
| 9969 | 10032 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 9970 | 10033 | const result_ty = self.typeOfIndex(inst); |
| ... | ... | @@ -9986,16 +10049,16 @@ pub const FuncGen = struct { |
| 9986 | 10049 | if (mod.typeToPackedStruct(result_ty)) |struct_type| { |
| 9987 | 10050 | const backing_int_ty = struct_type.backingIntType(ip).*; |
| 9988 | 10051 | assert(backing_int_ty != .none); |
| 9989 | const big_bits = Type.fromInterned(backing_int_ty).bitSize(mod); | |
| 10052 | const big_bits = Type.fromInterned(backing_int_ty).bitSize(pt); | |
| 9990 | 10053 | const int_ty = try o.builder.intType(@intCast(big_bits)); |
| 9991 | 10054 | comptime assert(Type.packed_struct_layout_version == 2); |
| 9992 | 10055 | var running_int = try o.builder.intValue(int_ty, 0); |
| 9993 | 10056 | var running_bits: u16 = 0; |
| 9994 | 10057 | for (elements, struct_type.field_types.get(ip)) |elem, field_ty| { |
| 9995 | if (!Type.fromInterned(field_ty).hasRuntimeBitsIgnoreComptime(mod)) continue; | |
| 10058 | if (!Type.fromInterned(field_ty).hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 9996 | 10059 | |
| 9997 | 10060 | const non_int_val = try self.resolveInst(elem); |
| 9998 | const ty_bit_size: u16 = @intCast(Type.fromInterned(field_ty).bitSize(mod)); | |
| 10061 | const ty_bit_size: u16 = @intCast(Type.fromInterned(field_ty).bitSize(pt)); | |
| 9999 | 10062 | const small_int_ty = try o.builder.intType(ty_bit_size); |
| 10000 | 10063 | const small_int_val = if (Type.fromInterned(field_ty).isPtrAtRuntime(mod)) |
| 10001 | 10064 | try self.wip.cast(.ptrtoint, non_int_val, small_int_ty, "") |
| ... | ... | @@ -10013,23 +10076,23 @@ pub const FuncGen = struct { |
| 10013 | 10076 | |
| 10014 | 10077 | assert(result_ty.containerLayout(mod) != .@"packed"); |
| 10015 | 10078 | |
| 10016 | if (isByRef(result_ty, mod)) { | |
| 10079 | if (isByRef(result_ty, pt)) { | |
| 10017 | 10080 | // TODO in debug builds init to undef so that the padding will be 0xaa |
| 10018 | 10081 | // even if we fully populate the fields. |
| 10019 | const alignment = result_ty.abiAlignment(mod).toLlvm(); | |
| 10082 | const alignment = result_ty.abiAlignment(pt).toLlvm(); | |
| 10020 | 10083 | const alloca_inst = try self.buildAllocaWorkaround(result_ty, alignment); |
| 10021 | 10084 | |
| 10022 | 10085 | for (elements, 0..) |elem, i| { |
| 10023 | if ((try result_ty.structFieldValueComptime(mod, i)) != null) continue; | |
| 10086 | if ((try result_ty.structFieldValueComptime(pt, i)) != null) continue; | |
| 10024 | 10087 | |
| 10025 | 10088 | const llvm_elem = try self.resolveInst(elem); |
| 10026 | 10089 | const llvm_i = o.llvmFieldIndex(result_ty, i).?; |
| 10027 | 10090 | const field_ptr = |
| 10028 | 10091 | try self.wip.gepStruct(llvm_result_ty, alloca_inst, llvm_i, ""); |
| 10029 | const field_ptr_ty = try mod.ptrType(.{ | |
| 10092 | const field_ptr_ty = try pt.ptrType(.{ | |
| 10030 | 10093 | .child = self.typeOf(elem).toIntern(), |
| 10031 | 10094 | .flags = .{ |
| 10032 | .alignment = result_ty.structFieldAlign(i, mod), | |
| 10095 | .alignment = result_ty.structFieldAlign(i, pt), | |
| 10033 | 10096 | }, |
| 10034 | 10097 | }); |
| 10035 | 10098 | try self.store(field_ptr, field_ptr_ty, llvm_elem, .none); |
| ... | ... | @@ -10039,7 +10102,7 @@ pub const FuncGen = struct { |
| 10039 | 10102 | } else { |
| 10040 | 10103 | var result = try o.builder.poisonValue(llvm_result_ty); |
| 10041 | 10104 | for (elements, 0..) |elem, i| { |
| 10042 | if ((try result_ty.structFieldValueComptime(mod, i)) != null) continue; | |
| 10105 | if ((try result_ty.structFieldValueComptime(pt, i)) != null) continue; | |
| 10043 | 10106 | |
| 10044 | 10107 | const llvm_elem = try self.resolveInst(elem); |
| 10045 | 10108 | const llvm_i = o.llvmFieldIndex(result_ty, i).?; |
| ... | ... | @@ -10049,15 +10112,15 @@ pub const FuncGen = struct { |
| 10049 | 10112 | } |
| 10050 | 10113 | }, |
| 10051 | 10114 | .Array => { |
| 10052 | assert(isByRef(result_ty, mod)); | |
| 10115 | assert(isByRef(result_ty, pt)); | |
| 10053 | 10116 | |
| 10054 | 10117 | const llvm_usize = try o.lowerType(Type.usize); |
| 10055 | 10118 | const usize_zero = try o.builder.intValue(llvm_usize, 0); |
| 10056 | const alignment = result_ty.abiAlignment(mod).toLlvm(); | |
| 10119 | const alignment = result_ty.abiAlignment(pt).toLlvm(); | |
| 10057 | 10120 | const alloca_inst = try self.buildAllocaWorkaround(result_ty, alignment); |
| 10058 | 10121 | |
| 10059 | 10122 | const array_info = result_ty.arrayInfo(mod); |
| 10060 | const elem_ptr_ty = try mod.ptrType(.{ | |
| 10123 | const elem_ptr_ty = try pt.ptrType(.{ | |
| 10061 | 10124 | .child = array_info.elem_type.toIntern(), |
| 10062 | 10125 | }); |
| 10063 | 10126 | |
| ... | ... | @@ -10084,21 +10147,22 @@ pub const FuncGen = struct { |
| 10084 | 10147 | |
| 10085 | 10148 | fn airUnionInit(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 10086 | 10149 | const o = self.dg.object; |
| 10087 | const mod = o.module; | |
| 10150 | const pt = o.pt; | |
| 10151 | const mod = pt.zcu; | |
| 10088 | 10152 | const ip = &mod.intern_pool; |
| 10089 | 10153 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 10090 | 10154 | const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 10091 | 10155 | const union_ty = self.typeOfIndex(inst); |
| 10092 | 10156 | const union_llvm_ty = try o.lowerType(union_ty); |
| 10093 | const layout = union_ty.unionGetLayout(mod); | |
| 10157 | const layout = union_ty.unionGetLayout(pt); | |
| 10094 | 10158 | const union_obj = mod.typeToUnion(union_ty).?; |
| 10095 | 10159 | |
| 10096 | 10160 | if (union_obj.getLayout(ip) == .@"packed") { |
| 10097 | const big_bits = union_ty.bitSize(mod); | |
| 10161 | const big_bits = union_ty.bitSize(pt); | |
| 10098 | 10162 | const int_llvm_ty = try o.builder.intType(@intCast(big_bits)); |
| 10099 | 10163 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[extra.field_index]); |
| 10100 | 10164 | const non_int_val = try self.resolveInst(extra.init); |
| 10101 | const small_int_ty = try o.builder.intType(@intCast(field_ty.bitSize(mod))); | |
| 10165 | const small_int_ty = try o.builder.intType(@intCast(field_ty.bitSize(pt))); | |
| 10102 | 10166 | const small_int_val = if (field_ty.isPtrAtRuntime(mod)) |
| 10103 | 10167 | try self.wip.cast(.ptrtoint, non_int_val, small_int_ty, "") |
| 10104 | 10168 | else |
| ... | ... | @@ -10110,19 +10174,19 @@ pub const FuncGen = struct { |
| 10110 | 10174 | const tag_ty = union_ty.unionTagTypeHypothetical(mod); |
| 10111 | 10175 | const union_field_name = union_obj.loadTagType(ip).names.get(ip)[extra.field_index]; |
| 10112 | 10176 | const enum_field_index = tag_ty.enumFieldIndex(union_field_name, mod).?; |
| 10113 | const tag_val = try mod.enumValueFieldIndex(tag_ty, enum_field_index); | |
| 10114 | break :blk try tag_val.intFromEnum(tag_ty, mod); | |
| 10177 | const tag_val = try pt.enumValueFieldIndex(tag_ty, enum_field_index); | |
| 10178 | break :blk try tag_val.intFromEnum(tag_ty, pt); | |
| 10115 | 10179 | }; |
| 10116 | 10180 | if (layout.payload_size == 0) { |
| 10117 | 10181 | if (layout.tag_size == 0) { |
| 10118 | 10182 | return .none; |
| 10119 | 10183 | } |
| 10120 | assert(!isByRef(union_ty, mod)); | |
| 10184 | assert(!isByRef(union_ty, pt)); | |
| 10121 | 10185 | var big_int_space: Value.BigIntSpace = undefined; |
| 10122 | const tag_big_int = tag_int_val.toBigInt(&big_int_space, mod); | |
| 10186 | const tag_big_int = tag_int_val.toBigInt(&big_int_space, pt); | |
| 10123 | 10187 | return try o.builder.bigIntValue(union_llvm_ty, tag_big_int); |
| 10124 | 10188 | } |
| 10125 | assert(isByRef(union_ty, mod)); | |
| 10189 | assert(isByRef(union_ty, pt)); | |
| 10126 | 10190 | // The llvm type of the alloca will be the named LLVM union type, and will not |
| 10127 | 10191 | // necessarily match the format that we need, depending on which tag is active. |
| 10128 | 10192 | // We must construct the correct unnamed struct type here, in order to then set |
| ... | ... | @@ -10132,14 +10196,14 @@ pub const FuncGen = struct { |
| 10132 | 10196 | const llvm_payload = try self.resolveInst(extra.init); |
| 10133 | 10197 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[extra.field_index]); |
| 10134 | 10198 | const field_llvm_ty = try o.lowerType(field_ty); |
| 10135 | const field_size = field_ty.abiSize(mod); | |
| 10136 | const field_align = mod.unionFieldNormalAlignment(union_obj, extra.field_index); | |
| 10199 | const field_size = field_ty.abiSize(pt); | |
| 10200 | const field_align = pt.unionFieldNormalAlignment(union_obj, extra.field_index); | |
| 10137 | 10201 | const llvm_usize = try o.lowerType(Type.usize); |
| 10138 | 10202 | const usize_zero = try o.builder.intValue(llvm_usize, 0); |
| 10139 | 10203 | |
| 10140 | 10204 | const llvm_union_ty = t: { |
| 10141 | 10205 | const payload_ty = p: { |
| 10142 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 10206 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 10143 | 10207 | const padding_len = layout.payload_size; |
| 10144 | 10208 | break :p try o.builder.arrayType(padding_len, .i8); |
| 10145 | 10209 | } |
| ... | ... | @@ -10169,7 +10233,7 @@ pub const FuncGen = struct { |
| 10169 | 10233 | |
| 10170 | 10234 | // Now we follow the layout as expressed above with GEP instructions to set the |
| 10171 | 10235 | // tag and the payload. |
| 10172 | const field_ptr_ty = try mod.ptrType(.{ | |
| 10236 | const field_ptr_ty = try pt.ptrType(.{ | |
| 10173 | 10237 | .child = field_ty.toIntern(), |
| 10174 | 10238 | .flags = .{ .alignment = field_align }, |
| 10175 | 10239 | }); |
| ... | ... | @@ -10195,9 +10259,9 @@ pub const FuncGen = struct { |
| 10195 | 10259 | const field_ptr = try self.wip.gep(.inbounds, llvm_union_ty, result_ptr, &indices, ""); |
| 10196 | 10260 | const tag_ty = try o.lowerType(Type.fromInterned(union_obj.enum_tag_ty)); |
| 10197 | 10261 | var big_int_space: Value.BigIntSpace = undefined; |
| 10198 | const tag_big_int = tag_int_val.toBigInt(&big_int_space, mod); | |
| 10262 | const tag_big_int = tag_int_val.toBigInt(&big_int_space, pt); | |
| 10199 | 10263 | const llvm_tag = try o.builder.bigIntValue(tag_ty, tag_big_int); |
| 10200 | const tag_alignment = Type.fromInterned(union_obj.enum_tag_ty).abiAlignment(mod).toLlvm(); | |
| 10264 | const tag_alignment = Type.fromInterned(union_obj.enum_tag_ty).abiAlignment(pt).toLlvm(); | |
| 10201 | 10265 | _ = try self.wip.store(.normal, llvm_tag, field_ptr, tag_alignment); |
| 10202 | 10266 | } |
| 10203 | 10267 | |
| ... | ... | @@ -10223,7 +10287,7 @@ pub const FuncGen = struct { |
| 10223 | 10287 | // by the target. |
| 10224 | 10288 | // To work around this, don't emit llvm.prefetch in this case. |
| 10225 | 10289 | // See https://bugs.llvm.org/show_bug.cgi?id=21037 |
| 10226 | const mod = o.module; | |
| 10290 | const mod = o.pt.zcu; | |
| 10227 | 10291 | const target = mod.getTarget(); |
| 10228 | 10292 | switch (prefetch.cache) { |
| 10229 | 10293 | .instruction => switch (target.cpu.arch) { |
| ... | ... | @@ -10279,7 +10343,7 @@ pub const FuncGen = struct { |
| 10279 | 10343 | |
| 10280 | 10344 | fn airWorkItemId(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 10281 | 10345 | const o = self.dg.object; |
| 10282 | const target = o.module.getTarget(); | |
| 10346 | const target = o.pt.zcu.getTarget(); | |
| 10283 | 10347 | assert(target.cpu.arch == .amdgcn); // TODO is to port this function to other GPU architectures |
| 10284 | 10348 | |
| 10285 | 10349 | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| ... | ... | @@ -10289,7 +10353,7 @@ pub const FuncGen = struct { |
| 10289 | 10353 | |
| 10290 | 10354 | fn airWorkGroupSize(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 10291 | 10355 | const o = self.dg.object; |
| 10292 | const target = o.module.getTarget(); | |
| 10356 | const target = o.pt.zcu.getTarget(); | |
| 10293 | 10357 | assert(target.cpu.arch == .amdgcn); // TODO is to port this function to other GPU architectures |
| 10294 | 10358 | |
| 10295 | 10359 | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| ... | ... | @@ -10312,7 +10376,7 @@ pub const FuncGen = struct { |
| 10312 | 10376 | |
| 10313 | 10377 | fn airWorkGroupId(self: *FuncGen, inst: Air.Inst.Index) !Builder.Value { |
| 10314 | 10378 | const o = self.dg.object; |
| 10315 | const target = o.module.getTarget(); | |
| 10379 | const target = o.pt.zcu.getTarget(); | |
| 10316 | 10380 | assert(target.cpu.arch == .amdgcn); // TODO is to port this function to other GPU architectures |
| 10317 | 10381 | |
| 10318 | 10382 | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| ... | ... | @@ -10322,7 +10386,7 @@ pub const FuncGen = struct { |
| 10322 | 10386 | |
| 10323 | 10387 | fn getErrorNameTable(self: *FuncGen) Allocator.Error!Builder.Variable.Index { |
| 10324 | 10388 | const o = self.dg.object; |
| 10325 | const mod = o.module; | |
| 10389 | const pt = o.pt; | |
| 10326 | 10390 | |
| 10327 | 10391 | const table = o.error_name_table; |
| 10328 | 10392 | if (table != .none) return table; |
| ... | ... | @@ -10334,7 +10398,7 @@ pub const FuncGen = struct { |
| 10334 | 10398 | variable_index.setMutability(.constant, &o.builder); |
| 10335 | 10399 | variable_index.setUnnamedAddr(.unnamed_addr, &o.builder); |
| 10336 | 10400 | variable_index.setAlignment( |
| 10337 | Type.slice_const_u8_sentinel_0.abiAlignment(mod).toLlvm(), | |
| 10401 | Type.slice_const_u8_sentinel_0.abiAlignment(pt).toLlvm(), | |
| 10338 | 10402 | &o.builder, |
| 10339 | 10403 | ); |
| 10340 | 10404 | |
| ... | ... | @@ -10372,15 +10436,16 @@ pub const FuncGen = struct { |
| 10372 | 10436 | can_elide_load: bool, |
| 10373 | 10437 | ) !Builder.Value { |
| 10374 | 10438 | const o = fg.dg.object; |
| 10375 | const mod = o.module; | |
| 10439 | const pt = o.pt; | |
| 10440 | const mod = pt.zcu; | |
| 10376 | 10441 | const payload_ty = opt_ty.optionalChild(mod); |
| 10377 | 10442 | |
| 10378 | if (isByRef(opt_ty, mod)) { | |
| 10443 | if (isByRef(opt_ty, pt)) { | |
| 10379 | 10444 | // We have a pointer and we need to return a pointer to the first field. |
| 10380 | 10445 | const payload_ptr = try fg.wip.gepStruct(opt_llvm_ty, opt_handle, 0, ""); |
| 10381 | 10446 | |
| 10382 | const payload_alignment = payload_ty.abiAlignment(mod).toLlvm(); | |
| 10383 | if (isByRef(payload_ty, mod)) { | |
| 10447 | const payload_alignment = payload_ty.abiAlignment(pt).toLlvm(); | |
| 10448 | if (isByRef(payload_ty, pt)) { | |
| 10384 | 10449 | if (can_elide_load) |
| 10385 | 10450 | return payload_ptr; |
| 10386 | 10451 | |
| ... | ... | @@ -10389,7 +10454,7 @@ pub const FuncGen = struct { |
| 10389 | 10454 | return fg.loadTruncate(.normal, payload_ty, payload_ptr, payload_alignment); |
| 10390 | 10455 | } |
| 10391 | 10456 | |
| 10392 | assert(!isByRef(payload_ty, mod)); | |
| 10457 | assert(!isByRef(payload_ty, pt)); | |
| 10393 | 10458 | return fg.wip.extractValue(opt_handle, &.{0}, ""); |
| 10394 | 10459 | } |
| 10395 | 10460 | |
| ... | ... | @@ -10400,12 +10465,12 @@ pub const FuncGen = struct { |
| 10400 | 10465 | non_null_bit: Builder.Value, |
| 10401 | 10466 | ) !Builder.Value { |
| 10402 | 10467 | const o = self.dg.object; |
| 10468 | const pt = o.pt; | |
| 10403 | 10469 | const optional_llvm_ty = try o.lowerType(optional_ty); |
| 10404 | 10470 | const non_null_field = try self.wip.cast(.zext, non_null_bit, .i8, ""); |
| 10405 | const mod = o.module; | |
| 10406 | 10471 | |
| 10407 | if (isByRef(optional_ty, mod)) { | |
| 10408 | const payload_alignment = optional_ty.abiAlignment(mod).toLlvm(); | |
| 10472 | if (isByRef(optional_ty, pt)) { | |
| 10473 | const payload_alignment = optional_ty.abiAlignment(pt).toLlvm(); | |
| 10409 | 10474 | const alloca_inst = try self.buildAllocaWorkaround(optional_ty, payload_alignment); |
| 10410 | 10475 | |
| 10411 | 10476 | { |
| ... | ... | @@ -10432,7 +10497,8 @@ pub const FuncGen = struct { |
| 10432 | 10497 | field_index: u32, |
| 10433 | 10498 | ) !Builder.Value { |
| 10434 | 10499 | const o = self.dg.object; |
| 10435 | const mod = o.module; | |
| 10500 | const pt = o.pt; | |
| 10501 | const mod = pt.zcu; | |
| 10436 | 10502 | const struct_ty = struct_ptr_ty.childType(mod); |
| 10437 | 10503 | switch (struct_ty.zigTypeTag(mod)) { |
| 10438 | 10504 | .Struct => switch (struct_ty.containerLayout(mod)) { |
| ... | ... | @@ -10452,7 +10518,7 @@ pub const FuncGen = struct { |
| 10452 | 10518 | |
| 10453 | 10519 | // We have a pointer to a packed struct field that happens to be byte-aligned. |
| 10454 | 10520 | // Offset our operand pointer by the correct number of bytes. |
| 10455 | const byte_offset = @divExact(mod.structPackedFieldBitOffset(struct_type, field_index) + struct_ptr_ty_info.packed_offset.bit_offset, 8); | |
| 10521 | const byte_offset = @divExact(pt.structPackedFieldBitOffset(struct_type, field_index) + struct_ptr_ty_info.packed_offset.bit_offset, 8); | |
| 10456 | 10522 | if (byte_offset == 0) return struct_ptr; |
| 10457 | 10523 | const usize_ty = try o.lowerType(Type.usize); |
| 10458 | 10524 | const llvm_index = try o.builder.intValue(usize_ty, byte_offset); |
| ... | ... | @@ -10470,14 +10536,14 @@ pub const FuncGen = struct { |
| 10470 | 10536 | // the struct. |
| 10471 | 10537 | const llvm_index = try o.builder.intValue( |
| 10472 | 10538 | try o.lowerType(Type.usize), |
| 10473 | @intFromBool(struct_ty.hasRuntimeBitsIgnoreComptime(mod)), | |
| 10539 | @intFromBool(struct_ty.hasRuntimeBitsIgnoreComptime(pt)), | |
| 10474 | 10540 | ); |
| 10475 | 10541 | return self.wip.gep(.inbounds, struct_llvm_ty, struct_ptr, &.{llvm_index}, ""); |
| 10476 | 10542 | } |
| 10477 | 10543 | }, |
| 10478 | 10544 | }, |
| 10479 | 10545 | .Union => { |
| 10480 | const layout = struct_ty.unionGetLayout(mod); | |
| 10546 | const layout = struct_ty.unionGetLayout(pt); | |
| 10481 | 10547 | if (layout.payload_size == 0 or struct_ty.containerLayout(mod) == .@"packed") return struct_ptr; |
| 10482 | 10548 | const payload_index = @intFromBool(layout.tag_align.compare(.gte, layout.payload_align)); |
| 10483 | 10549 | const union_llvm_ty = try o.lowerType(struct_ty); |
| ... | ... | @@ -10500,9 +10566,10 @@ pub const FuncGen = struct { |
| 10500 | 10566 | // => so load the byte aligned value and trunc the unwanted bits. |
| 10501 | 10567 | |
| 10502 | 10568 | const o = fg.dg.object; |
| 10503 | const mod = o.module; | |
| 10569 | const pt = o.pt; | |
| 10570 | const mod = pt.zcu; | |
| 10504 | 10571 | const payload_llvm_ty = try o.lowerType(payload_ty); |
| 10505 | const abi_size = payload_ty.abiSize(mod); | |
| 10572 | const abi_size = payload_ty.abiSize(pt); | |
| 10506 | 10573 | |
| 10507 | 10574 | // llvm bug workarounds: |
| 10508 | 10575 | const workaround_explicit_mask = o.target.cpu.arch == .powerpc and abi_size >= 4; |
| ... | ... | @@ -10522,7 +10589,7 @@ pub const FuncGen = struct { |
| 10522 | 10589 | const shifted = if (payload_llvm_ty != load_llvm_ty and o.target.cpu.arch.endian() == .big) |
| 10523 | 10590 | try fg.wip.bin(.lshr, loaded, try o.builder.intValue( |
| 10524 | 10591 | load_llvm_ty, |
| 10525 | (payload_ty.abiSize(mod) - (std.math.divCeil(u64, payload_ty.bitSize(mod), 8) catch unreachable)) * 8, | |
| 10592 | (payload_ty.abiSize(pt) - (std.math.divCeil(u64, payload_ty.bitSize(pt), 8) catch unreachable)) * 8, | |
| 10526 | 10593 | ), "") |
| 10527 | 10594 | else |
| 10528 | 10595 | loaded; |
| ... | ... | @@ -10546,11 +10613,11 @@ pub const FuncGen = struct { |
| 10546 | 10613 | access_kind: Builder.MemoryAccessKind, |
| 10547 | 10614 | ) !Builder.Value { |
| 10548 | 10615 | const o = fg.dg.object; |
| 10549 | const mod = o.module; | |
| 10616 | const pt = o.pt; | |
| 10550 | 10617 | //const pointee_llvm_ty = try o.lowerType(pointee_type); |
| 10551 | const result_align = InternPool.Alignment.fromLlvm(ptr_alignment).max(pointee_type.abiAlignment(mod)).toLlvm(); | |
| 10618 | const result_align = InternPool.Alignment.fromLlvm(ptr_alignment).max(pointee_type.abiAlignment(pt)).toLlvm(); | |
| 10552 | 10619 | const result_ptr = try fg.buildAllocaWorkaround(pointee_type, result_align); |
| 10553 | const size_bytes = pointee_type.abiSize(mod); | |
| 10620 | const size_bytes = pointee_type.abiSize(pt); | |
| 10554 | 10621 | _ = try fg.wip.callMemCpy( |
| 10555 | 10622 | result_ptr, |
| 10556 | 10623 | result_align, |
| ... | ... | @@ -10567,15 +10634,16 @@ pub const FuncGen = struct { |
| 10567 | 10634 | /// For isByRef=false types, it creates a load instruction and returns it. |
| 10568 | 10635 | fn load(self: *FuncGen, ptr: Builder.Value, ptr_ty: Type) !Builder.Value { |
| 10569 | 10636 | const o = self.dg.object; |
| 10570 | const mod = o.module; | |
| 10637 | const pt = o.pt; | |
| 10638 | const mod = pt.zcu; | |
| 10571 | 10639 | const info = ptr_ty.ptrInfo(mod); |
| 10572 | 10640 | const elem_ty = Type.fromInterned(info.child); |
| 10573 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) return .none; | |
| 10641 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(pt)) return .none; | |
| 10574 | 10642 | |
| 10575 | 10643 | const ptr_alignment = (if (info.flags.alignment != .none) |
| 10576 | 10644 | @as(InternPool.Alignment, info.flags.alignment) |
| 10577 | 10645 | else |
| 10578 | elem_ty.abiAlignment(mod)).toLlvm(); | |
| 10646 | elem_ty.abiAlignment(pt)).toLlvm(); | |
| 10579 | 10647 | |
| 10580 | 10648 | const access_kind: Builder.MemoryAccessKind = |
| 10581 | 10649 | if (info.flags.is_volatile) .@"volatile" else .normal; |
| ... | ... | @@ -10591,7 +10659,7 @@ pub const FuncGen = struct { |
| 10591 | 10659 | } |
| 10592 | 10660 | |
| 10593 | 10661 | if (info.packed_offset.host_size == 0) { |
| 10594 | if (isByRef(elem_ty, mod)) { | |
| 10662 | if (isByRef(elem_ty, pt)) { | |
| 10595 | 10663 | return self.loadByRef(ptr, elem_ty, ptr_alignment, access_kind); |
| 10596 | 10664 | } |
| 10597 | 10665 | return self.loadTruncate(access_kind, elem_ty, ptr, ptr_alignment); |
| ... | ... | @@ -10601,13 +10669,13 @@ pub const FuncGen = struct { |
| 10601 | 10669 | const containing_int = |
| 10602 | 10670 | try self.wip.load(access_kind, containing_int_ty, ptr, ptr_alignment, ""); |
| 10603 | 10671 | |
| 10604 | const elem_bits = ptr_ty.childType(mod).bitSize(mod); | |
| 10672 | const elem_bits = ptr_ty.childType(mod).bitSize(pt); | |
| 10605 | 10673 | const shift_amt = try o.builder.intValue(containing_int_ty, info.packed_offset.bit_offset); |
| 10606 | 10674 | const shifted_value = try self.wip.bin(.lshr, containing_int, shift_amt, ""); |
| 10607 | 10675 | const elem_llvm_ty = try o.lowerType(elem_ty); |
| 10608 | 10676 | |
| 10609 | if (isByRef(elem_ty, mod)) { | |
| 10610 | const result_align = elem_ty.abiAlignment(mod).toLlvm(); | |
| 10677 | if (isByRef(elem_ty, pt)) { | |
| 10678 | const result_align = elem_ty.abiAlignment(pt).toLlvm(); | |
| 10611 | 10679 | const result_ptr = try self.buildAllocaWorkaround(elem_ty, result_align); |
| 10612 | 10680 | |
| 10613 | 10681 | const same_size_int = try o.builder.intType(@intCast(elem_bits)); |
| ... | ... | @@ -10639,13 +10707,14 @@ pub const FuncGen = struct { |
| 10639 | 10707 | ordering: Builder.AtomicOrdering, |
| 10640 | 10708 | ) !void { |
| 10641 | 10709 | const o = self.dg.object; |
| 10642 | const mod = o.module; | |
| 10710 | const pt = o.pt; | |
| 10711 | const mod = pt.zcu; | |
| 10643 | 10712 | const info = ptr_ty.ptrInfo(mod); |
| 10644 | 10713 | const elem_ty = Type.fromInterned(info.child); |
| 10645 | if (!elem_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { | |
| 10714 | if (!elem_ty.isFnOrHasRuntimeBitsIgnoreComptime(pt)) { | |
| 10646 | 10715 | return; |
| 10647 | 10716 | } |
| 10648 | const ptr_alignment = ptr_ty.ptrAlignment(mod).toLlvm(); | |
| 10717 | const ptr_alignment = ptr_ty.ptrAlignment(pt).toLlvm(); | |
| 10649 | 10718 | const access_kind: Builder.MemoryAccessKind = |
| 10650 | 10719 | if (info.flags.is_volatile) .@"volatile" else .normal; |
| 10651 | 10720 | |
| ... | ... | @@ -10669,7 +10738,7 @@ pub const FuncGen = struct { |
| 10669 | 10738 | assert(ordering == .none); |
| 10670 | 10739 | const containing_int = |
| 10671 | 10740 | try self.wip.load(access_kind, containing_int_ty, ptr, ptr_alignment, ""); |
| 10672 | const elem_bits = ptr_ty.childType(mod).bitSize(mod); | |
| 10741 | const elem_bits = ptr_ty.childType(mod).bitSize(pt); | |
| 10673 | 10742 | const shift_amt = try o.builder.intConst(containing_int_ty, info.packed_offset.bit_offset); |
| 10674 | 10743 | // Convert to equally-sized integer type in order to perform the bit |
| 10675 | 10744 | // operations on the value to store |
| ... | ... | @@ -10704,7 +10773,7 @@ pub const FuncGen = struct { |
| 10704 | 10773 | _ = try self.wip.store(access_kind, ored_value, ptr, ptr_alignment); |
| 10705 | 10774 | return; |
| 10706 | 10775 | } |
| 10707 | if (!isByRef(elem_ty, mod)) { | |
| 10776 | if (!isByRef(elem_ty, pt)) { | |
| 10708 | 10777 | _ = try self.wip.storeAtomic( |
| 10709 | 10778 | access_kind, |
| 10710 | 10779 | elem, |
| ... | ... | @@ -10720,8 +10789,8 @@ pub const FuncGen = struct { |
| 10720 | 10789 | ptr, |
| 10721 | 10790 | ptr_alignment, |
| 10722 | 10791 | elem, |
| 10723 | elem_ty.abiAlignment(mod).toLlvm(), | |
| 10724 | try o.builder.intValue(try o.lowerType(Type.usize), elem_ty.abiSize(mod)), | |
| 10792 | elem_ty.abiAlignment(pt).toLlvm(), | |
| 10793 | try o.builder.intValue(try o.lowerType(Type.usize), elem_ty.abiSize(pt)), | |
| 10725 | 10794 | access_kind, |
| 10726 | 10795 | ); |
| 10727 | 10796 | } |
| ... | ... | @@ -10747,12 +10816,13 @@ pub const FuncGen = struct { |
| 10747 | 10816 | a5: Builder.Value, |
| 10748 | 10817 | ) Allocator.Error!Builder.Value { |
| 10749 | 10818 | const o = fg.dg.object; |
| 10750 | const mod = o.module; | |
| 10819 | const pt = o.pt; | |
| 10820 | const mod = pt.zcu; | |
| 10751 | 10821 | const target = mod.getTarget(); |
| 10752 | 10822 | if (!target_util.hasValgrindSupport(target)) return default_value; |
| 10753 | 10823 | |
| 10754 | 10824 | const llvm_usize = try o.lowerType(Type.usize); |
| 10755 | const usize_alignment = Type.usize.abiAlignment(mod).toLlvm(); | |
| 10825 | const usize_alignment = Type.usize.abiAlignment(pt).toLlvm(); | |
| 10756 | 10826 | |
| 10757 | 10827 | const array_llvm_ty = try o.builder.arrayType(6, llvm_usize); |
| 10758 | 10828 | const array_ptr = if (fg.valgrind_client_request_array == .none) a: { |
| ... | ... | @@ -10813,13 +10883,13 @@ pub const FuncGen = struct { |
| 10813 | 10883 | |
| 10814 | 10884 | fn typeOf(fg: *FuncGen, inst: Air.Inst.Ref) Type { |
| 10815 | 10885 | const o = fg.dg.object; |
| 10816 | const mod = o.module; | |
| 10886 | const mod = o.pt.zcu; | |
| 10817 | 10887 | return fg.air.typeOf(inst, &mod.intern_pool); |
| 10818 | 10888 | } |
| 10819 | 10889 | |
| 10820 | 10890 | fn typeOfIndex(fg: *FuncGen, inst: Air.Inst.Index) Type { |
| 10821 | 10891 | const o = fg.dg.object; |
| 10822 | const mod = o.module; | |
| 10892 | const mod = o.pt.zcu; | |
| 10823 | 10893 | return fg.air.typeOfIndex(inst, &mod.intern_pool); |
| 10824 | 10894 | } |
| 10825 | 10895 | }; |
| ... | ... | @@ -10990,12 +11060,12 @@ fn toLlvmGlobalAddressSpace(wanted_address_space: std.builtin.AddressSpace, targ |
| 10990 | 11060 | }; |
| 10991 | 11061 | } |
| 10992 | 11062 | |
| 10993 | fn returnTypeByRef(zcu: *Zcu, target: std.Target, ty: Type) bool { | |
| 10994 | if (isByRef(ty, zcu)) { | |
| 11063 | fn returnTypeByRef(pt: Zcu.PerThread, target: std.Target, ty: Type) bool { | |
| 11064 | if (isByRef(ty, pt)) { | |
| 10995 | 11065 | return true; |
| 10996 | 11066 | } else if (target.cpu.arch.isX86() and |
| 10997 | 11067 | !std.Target.x86.featureSetHas(target.cpu.features, .evex512) and |
| 10998 | ty.totalVectorBits(zcu) >= 512) | |
| 11068 | ty.totalVectorBits(pt) >= 512) | |
| 10999 | 11069 | { |
| 11000 | 11070 | // As of LLVM 18, passing a vector byval with fastcc that is 512 bits or more returns |
| 11001 | 11071 | // "512-bit vector arguments require 'evex512' for AVX512" |
| ... | ... | @@ -11005,38 +11075,38 @@ fn returnTypeByRef(zcu: *Zcu, target: std.Target, ty: Type) bool { |
| 11005 | 11075 | } |
| 11006 | 11076 | } |
| 11007 | 11077 | |
| 11008 | fn firstParamSRet(fn_info: InternPool.Key.FuncType, zcu: *Zcu, target: std.Target) bool { | |
| 11078 | fn firstParamSRet(fn_info: InternPool.Key.FuncType, pt: Zcu.PerThread, target: std.Target) bool { | |
| 11009 | 11079 | const return_type = Type.fromInterned(fn_info.return_type); |
| 11010 | if (!return_type.hasRuntimeBitsIgnoreComptime(zcu)) return false; | |
| 11080 | if (!return_type.hasRuntimeBitsIgnoreComptime(pt)) return false; | |
| 11011 | 11081 | |
| 11012 | 11082 | return switch (fn_info.cc) { |
| 11013 | .Unspecified, .Inline => returnTypeByRef(zcu, target, return_type), | |
| 11083 | .Unspecified, .Inline => returnTypeByRef(pt, target, return_type), | |
| 11014 | 11084 | .C => switch (target.cpu.arch) { |
| 11015 | 11085 | .mips, .mipsel => false, |
| 11016 | .x86 => isByRef(return_type, zcu), | |
| 11086 | .x86 => isByRef(return_type, pt), | |
| 11017 | 11087 | .x86_64 => switch (target.os.tag) { |
| 11018 | .windows => x86_64_abi.classifyWindows(return_type, zcu) == .memory, | |
| 11019 | else => firstParamSRetSystemV(return_type, zcu, target), | |
| 11088 | .windows => x86_64_abi.classifyWindows(return_type, pt) == .memory, | |
| 11089 | else => firstParamSRetSystemV(return_type, pt, target), | |
| 11020 | 11090 | }, |
| 11021 | .wasm32 => wasm_c_abi.classifyType(return_type, zcu)[0] == .indirect, | |
| 11022 | .aarch64, .aarch64_be => aarch64_c_abi.classifyType(return_type, zcu) == .memory, | |
| 11023 | .arm, .armeb => switch (arm_c_abi.classifyType(return_type, zcu, .ret)) { | |
| 11091 | .wasm32 => wasm_c_abi.classifyType(return_type, pt)[0] == .indirect, | |
| 11092 | .aarch64, .aarch64_be => aarch64_c_abi.classifyType(return_type, pt) == .memory, | |
| 11093 | .arm, .armeb => switch (arm_c_abi.classifyType(return_type, pt, .ret)) { | |
| 11024 | 11094 | .memory, .i64_array => true, |
| 11025 | 11095 | .i32_array => |size| size != 1, |
| 11026 | 11096 | .byval => false, |
| 11027 | 11097 | }, |
| 11028 | .riscv32, .riscv64 => riscv_c_abi.classifyType(return_type, zcu) == .memory, | |
| 11098 | .riscv32, .riscv64 => riscv_c_abi.classifyType(return_type, pt) == .memory, | |
| 11029 | 11099 | else => false, // TODO investigate C ABI for other architectures |
| 11030 | 11100 | }, |
| 11031 | .SysV => firstParamSRetSystemV(return_type, zcu, target), | |
| 11032 | .Win64 => x86_64_abi.classifyWindows(return_type, zcu) == .memory, | |
| 11033 | .Stdcall => !isScalar(zcu, return_type), | |
| 11101 | .SysV => firstParamSRetSystemV(return_type, pt, target), | |
| 11102 | .Win64 => x86_64_abi.classifyWindows(return_type, pt) == .memory, | |
| 11103 | .Stdcall => !isScalar(pt.zcu, return_type), | |
| 11034 | 11104 | else => false, |
| 11035 | 11105 | }; |
| 11036 | 11106 | } |
| 11037 | 11107 | |
| 11038 | fn firstParamSRetSystemV(ty: Type, zcu: *Zcu, target: std.Target) bool { | |
| 11039 | const class = x86_64_abi.classifySystemV(ty, zcu, target, .ret); | |
| 11108 | fn firstParamSRetSystemV(ty: Type, pt: Zcu.PerThread, target: std.Target) bool { | |
| 11109 | const class = x86_64_abi.classifySystemV(ty, pt, target, .ret); | |
| 11040 | 11110 | if (class[0] == .memory) return true; |
| 11041 | 11111 | if (class[0] == .x87 and class[2] != .none) return true; |
| 11042 | 11112 | return false; |
| ... | ... | @@ -11046,9 +11116,10 @@ fn firstParamSRetSystemV(ty: Type, zcu: *Zcu, target: std.Target) bool { |
| 11046 | 11116 | /// completely differently in the function prototype to honor the C ABI, and then |
| 11047 | 11117 | /// be effectively bitcasted to the actual return type. |
| 11048 | 11118 | fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type { |
| 11049 | const mod = o.module; | |
| 11119 | const pt = o.pt; | |
| 11120 | const mod = pt.zcu; | |
| 11050 | 11121 | const return_type = Type.fromInterned(fn_info.return_type); |
| 11051 | if (!return_type.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 11122 | if (!return_type.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 11052 | 11123 | // If the return type is an error set or an error union, then we make this |
| 11053 | 11124 | // anyerror return type instead, so that it can be coerced into a function |
| 11054 | 11125 | // pointer type which has anyerror as the return type. |
| ... | ... | @@ -11058,12 +11129,12 @@ fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Bu |
| 11058 | 11129 | switch (fn_info.cc) { |
| 11059 | 11130 | .Unspecified, |
| 11060 | 11131 | .Inline, |
| 11061 | => return if (returnTypeByRef(mod, target, return_type)) .void else o.lowerType(return_type), | |
| 11132 | => return if (returnTypeByRef(pt, target, return_type)) .void else o.lowerType(return_type), | |
| 11062 | 11133 | |
| 11063 | 11134 | .C => { |
| 11064 | 11135 | switch (target.cpu.arch) { |
| 11065 | 11136 | .mips, .mipsel => return o.lowerType(return_type), |
| 11066 | .x86 => return if (isByRef(return_type, mod)) .void else o.lowerType(return_type), | |
| 11137 | .x86 => return if (isByRef(return_type, pt)) .void else o.lowerType(return_type), | |
| 11067 | 11138 | .x86_64 => switch (target.os.tag) { |
| 11068 | 11139 | .windows => return lowerWin64FnRetTy(o, fn_info), |
| 11069 | 11140 | else => return lowerSystemVFnRetTy(o, fn_info), |
| ... | ... | @@ -11072,36 +11143,36 @@ fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Bu |
| 11072 | 11143 | if (isScalar(mod, return_type)) { |
| 11073 | 11144 | return o.lowerType(return_type); |
| 11074 | 11145 | } |
| 11075 | const classes = wasm_c_abi.classifyType(return_type, mod); | |
| 11146 | const classes = wasm_c_abi.classifyType(return_type, pt); | |
| 11076 | 11147 | if (classes[0] == .indirect or classes[0] == .none) { |
| 11077 | 11148 | return .void; |
| 11078 | 11149 | } |
| 11079 | 11150 | |
| 11080 | 11151 | assert(classes[0] == .direct and classes[1] == .none); |
| 11081 | const scalar_type = wasm_c_abi.scalarType(return_type, mod); | |
| 11082 | return o.builder.intType(@intCast(scalar_type.abiSize(mod) * 8)); | |
| 11152 | const scalar_type = wasm_c_abi.scalarType(return_type, pt); | |
| 11153 | return o.builder.intType(@intCast(scalar_type.abiSize(pt) * 8)); | |
| 11083 | 11154 | }, |
| 11084 | 11155 | .aarch64, .aarch64_be => { |
| 11085 | switch (aarch64_c_abi.classifyType(return_type, mod)) { | |
| 11156 | switch (aarch64_c_abi.classifyType(return_type, pt)) { | |
| 11086 | 11157 | .memory => return .void, |
| 11087 | 11158 | .float_array => return o.lowerType(return_type), |
| 11088 | 11159 | .byval => return o.lowerType(return_type), |
| 11089 | .integer => return o.builder.intType(@intCast(return_type.bitSize(mod))), | |
| 11160 | .integer => return o.builder.intType(@intCast(return_type.bitSize(pt))), | |
| 11090 | 11161 | .double_integer => return o.builder.arrayType(2, .i64), |
| 11091 | 11162 | } |
| 11092 | 11163 | }, |
| 11093 | 11164 | .arm, .armeb => { |
| 11094 | switch (arm_c_abi.classifyType(return_type, mod, .ret)) { | |
| 11165 | switch (arm_c_abi.classifyType(return_type, pt, .ret)) { | |
| 11095 | 11166 | .memory, .i64_array => return .void, |
| 11096 | 11167 | .i32_array => |len| return if (len == 1) .i32 else .void, |
| 11097 | 11168 | .byval => return o.lowerType(return_type), |
| 11098 | 11169 | } |
| 11099 | 11170 | }, |
| 11100 | 11171 | .riscv32, .riscv64 => { |
| 11101 | switch (riscv_c_abi.classifyType(return_type, mod)) { | |
| 11172 | switch (riscv_c_abi.classifyType(return_type, pt)) { | |
| 11102 | 11173 | .memory => return .void, |
| 11103 | 11174 | .integer => { |
| 11104 | return o.builder.intType(@intCast(return_type.bitSize(mod))); | |
| 11175 | return o.builder.intType(@intCast(return_type.bitSize(pt))); | |
| 11105 | 11176 | }, |
| 11106 | 11177 | .double_integer => { |
| 11107 | 11178 | return o.builder.structType(.normal, &.{ .i64, .i64 }); |
| ... | ... | @@ -11112,7 +11183,7 @@ fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Bu |
| 11112 | 11183 | var types: [8]Builder.Type = undefined; |
| 11113 | 11184 | for (0..return_type.structFieldCount(mod)) |field_index| { |
| 11114 | 11185 | const field_ty = return_type.structFieldType(field_index, mod); |
| 11115 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; | |
| 11186 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 11116 | 11187 | types[types_len] = try o.lowerType(field_ty); |
| 11117 | 11188 | types_len += 1; |
| 11118 | 11189 | } |
| ... | ... | @@ -11132,14 +11203,14 @@ fn lowerFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Bu |
| 11132 | 11203 | } |
| 11133 | 11204 | |
| 11134 | 11205 | fn lowerWin64FnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type { |
| 11135 | const mod = o.module; | |
| 11206 | const pt = o.pt; | |
| 11136 | 11207 | const return_type = Type.fromInterned(fn_info.return_type); |
| 11137 | switch (x86_64_abi.classifyWindows(return_type, mod)) { | |
| 11208 | switch (x86_64_abi.classifyWindows(return_type, pt)) { | |
| 11138 | 11209 | .integer => { |
| 11139 | if (isScalar(mod, return_type)) { | |
| 11210 | if (isScalar(pt.zcu, return_type)) { | |
| 11140 | 11211 | return o.lowerType(return_type); |
| 11141 | 11212 | } else { |
| 11142 | return o.builder.intType(@intCast(return_type.abiSize(mod) * 8)); | |
| 11213 | return o.builder.intType(@intCast(return_type.abiSize(pt) * 8)); | |
| 11143 | 11214 | } |
| 11144 | 11215 | }, |
| 11145 | 11216 | .win_i128 => return o.builder.vectorType(.normal, 2, .i64), |
| ... | ... | @@ -11150,14 +11221,15 @@ fn lowerWin64FnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Err |
| 11150 | 11221 | } |
| 11151 | 11222 | |
| 11152 | 11223 | fn lowerSystemVFnRetTy(o: *Object, fn_info: InternPool.Key.FuncType) Allocator.Error!Builder.Type { |
| 11153 | const mod = o.module; | |
| 11224 | const pt = o.pt; | |
| 11225 | const mod = pt.zcu; | |
| 11154 | 11226 | const ip = &mod.intern_pool; |
| 11155 | 11227 | const return_type = Type.fromInterned(fn_info.return_type); |
| 11156 | 11228 | if (isScalar(mod, return_type)) { |
| 11157 | 11229 | return o.lowerType(return_type); |
| 11158 | 11230 | } |
| 11159 | 11231 | const target = mod.getTarget(); |
| 11160 | const classes = x86_64_abi.classifySystemV(return_type, mod, target, .ret); | |
| 11232 | const classes = x86_64_abi.classifySystemV(return_type, pt, target, .ret); | |
| 11161 | 11233 | if (classes[0] == .memory) return .void; |
| 11162 | 11234 | var types_index: u32 = 0; |
| 11163 | 11235 | var types_buffer: [8]Builder.Type = undefined; |
| ... | ... | @@ -11249,8 +11321,7 @@ const ParamTypeIterator = struct { |
| 11249 | 11321 | |
| 11250 | 11322 | pub fn next(it: *ParamTypeIterator) Allocator.Error!?Lowering { |
| 11251 | 11323 | if (it.zig_index >= it.fn_info.param_types.len) return null; |
| 11252 | const zcu = it.object.module; | |
| 11253 | const ip = &zcu.intern_pool; | |
| 11324 | const ip = &it.object.pt.zcu.intern_pool; | |
| 11254 | 11325 | const ty = it.fn_info.param_types.get(ip)[it.zig_index]; |
| 11255 | 11326 | it.byval_attr = false; |
| 11256 | 11327 | return nextInner(it, Type.fromInterned(ty)); |
| ... | ... | @@ -11258,8 +11329,7 @@ const ParamTypeIterator = struct { |
| 11258 | 11329 | |
| 11259 | 11330 | /// `airCall` uses this instead of `next` so that it can take into account variadic functions. |
| 11260 | 11331 | pub fn nextCall(it: *ParamTypeIterator, fg: *FuncGen, args: []const Air.Inst.Ref) Allocator.Error!?Lowering { |
| 11261 | const zcu = it.object.module; | |
| 11262 | const ip = &zcu.intern_pool; | |
| 11332 | const ip = &it.object.pt.zcu.intern_pool; | |
| 11263 | 11333 | if (it.zig_index >= it.fn_info.param_types.len) { |
| 11264 | 11334 | if (it.zig_index >= args.len) { |
| 11265 | 11335 | return null; |
| ... | ... | @@ -11272,10 +11342,11 @@ const ParamTypeIterator = struct { |
| 11272 | 11342 | } |
| 11273 | 11343 | |
| 11274 | 11344 | fn nextInner(it: *ParamTypeIterator, ty: Type) Allocator.Error!?Lowering { |
| 11275 | const zcu = it.object.module; | |
| 11345 | const pt = it.object.pt; | |
| 11346 | const zcu = pt.zcu; | |
| 11276 | 11347 | const target = zcu.getTarget(); |
| 11277 | 11348 | |
| 11278 | if (!ty.hasRuntimeBitsIgnoreComptime(zcu)) { | |
| 11349 | if (!ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 11279 | 11350 | it.zig_index += 1; |
| 11280 | 11351 | return .no_bits; |
| 11281 | 11352 | } |
| ... | ... | @@ -11288,11 +11359,11 @@ const ParamTypeIterator = struct { |
| 11288 | 11359 | { |
| 11289 | 11360 | it.llvm_index += 1; |
| 11290 | 11361 | return .slice; |
| 11291 | } else if (isByRef(ty, zcu)) { | |
| 11362 | } else if (isByRef(ty, pt)) { | |
| 11292 | 11363 | return .byref; |
| 11293 | 11364 | } else if (target.cpu.arch.isX86() and |
| 11294 | 11365 | !std.Target.x86.featureSetHas(target.cpu.features, .evex512) and |
| 11295 | ty.totalVectorBits(zcu) >= 512) | |
| 11366 | ty.totalVectorBits(pt) >= 512) | |
| 11296 | 11367 | { |
| 11297 | 11368 | // As of LLVM 18, passing a vector byval with fastcc that is 512 bits or more returns |
| 11298 | 11369 | // "512-bit vector arguments require 'evex512' for AVX512" |
| ... | ... | @@ -11320,7 +11391,7 @@ const ParamTypeIterator = struct { |
| 11320 | 11391 | if (isScalar(zcu, ty)) { |
| 11321 | 11392 | return .byval; |
| 11322 | 11393 | } |
| 11323 | const classes = wasm_c_abi.classifyType(ty, zcu); | |
| 11394 | const classes = wasm_c_abi.classifyType(ty, pt); | |
| 11324 | 11395 | if (classes[0] == .indirect) { |
| 11325 | 11396 | return .byref; |
| 11326 | 11397 | } |
| ... | ... | @@ -11329,7 +11400,7 @@ const ParamTypeIterator = struct { |
| 11329 | 11400 | .aarch64, .aarch64_be => { |
| 11330 | 11401 | it.zig_index += 1; |
| 11331 | 11402 | it.llvm_index += 1; |
| 11332 | switch (aarch64_c_abi.classifyType(ty, zcu)) { | |
| 11403 | switch (aarch64_c_abi.classifyType(ty, pt)) { | |
| 11333 | 11404 | .memory => return .byref_mut, |
| 11334 | 11405 | .float_array => |len| return Lowering{ .float_array = len }, |
| 11335 | 11406 | .byval => return .byval, |
| ... | ... | @@ -11344,7 +11415,7 @@ const ParamTypeIterator = struct { |
| 11344 | 11415 | .arm, .armeb => { |
| 11345 | 11416 | it.zig_index += 1; |
| 11346 | 11417 | it.llvm_index += 1; |
| 11347 | switch (arm_c_abi.classifyType(ty, zcu, .arg)) { | |
| 11418 | switch (arm_c_abi.classifyType(ty, pt, .arg)) { | |
| 11348 | 11419 | .memory => { |
| 11349 | 11420 | it.byval_attr = true; |
| 11350 | 11421 | return .byref; |
| ... | ... | @@ -11359,7 +11430,7 @@ const ParamTypeIterator = struct { |
| 11359 | 11430 | it.llvm_index += 1; |
| 11360 | 11431 | if (ty.toIntern() == .f16_type and |
| 11361 | 11432 | !std.Target.riscv.featureSetHas(target.cpu.features, .d)) return .as_u16; |
| 11362 | switch (riscv_c_abi.classifyType(ty, zcu)) { | |
| 11433 | switch (riscv_c_abi.classifyType(ty, pt)) { | |
| 11363 | 11434 | .memory => return .byref_mut, |
| 11364 | 11435 | .byval => return .byval, |
| 11365 | 11436 | .integer => return .abi_sized_int, |
| ... | ... | @@ -11368,7 +11439,7 @@ const ParamTypeIterator = struct { |
| 11368 | 11439 | it.types_len = 0; |
| 11369 | 11440 | for (0..ty.structFieldCount(zcu)) |field_index| { |
| 11370 | 11441 | const field_ty = ty.structFieldType(field_index, zcu); |
| 11371 | if (!field_ty.hasRuntimeBitsIgnoreComptime(zcu)) continue; | |
| 11442 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 11372 | 11443 | it.types_buffer[it.types_len] = try it.object.lowerType(field_ty); |
| 11373 | 11444 | it.types_len += 1; |
| 11374 | 11445 | } |
| ... | ... | @@ -11406,10 +11477,10 @@ const ParamTypeIterator = struct { |
| 11406 | 11477 | } |
| 11407 | 11478 | |
| 11408 | 11479 | fn nextWin64(it: *ParamTypeIterator, ty: Type) ?Lowering { |
| 11409 | const zcu = it.object.module; | |
| 11410 | switch (x86_64_abi.classifyWindows(ty, zcu)) { | |
| 11480 | const pt = it.object.pt; | |
| 11481 | switch (x86_64_abi.classifyWindows(ty, pt)) { | |
| 11411 | 11482 | .integer => { |
| 11412 | if (isScalar(zcu, ty)) { | |
| 11483 | if (isScalar(pt.zcu, ty)) { | |
| 11413 | 11484 | it.zig_index += 1; |
| 11414 | 11485 | it.llvm_index += 1; |
| 11415 | 11486 | return .byval; |
| ... | ... | @@ -11439,17 +11510,17 @@ const ParamTypeIterator = struct { |
| 11439 | 11510 | } |
| 11440 | 11511 | |
| 11441 | 11512 | fn nextSystemV(it: *ParamTypeIterator, ty: Type) Allocator.Error!?Lowering { |
| 11442 | const zcu = it.object.module; | |
| 11443 | const ip = &zcu.intern_pool; | |
| 11444 | const target = zcu.getTarget(); | |
| 11445 | const classes = x86_64_abi.classifySystemV(ty, zcu, target, .arg); | |
| 11513 | const pt = it.object.pt; | |
| 11514 | const ip = &pt.zcu.intern_pool; | |
| 11515 | const target = pt.zcu.getTarget(); | |
| 11516 | const classes = x86_64_abi.classifySystemV(ty, pt, target, .arg); | |
| 11446 | 11517 | if (classes[0] == .memory) { |
| 11447 | 11518 | it.zig_index += 1; |
| 11448 | 11519 | it.llvm_index += 1; |
| 11449 | 11520 | it.byval_attr = true; |
| 11450 | 11521 | return .byref; |
| 11451 | 11522 | } |
| 11452 | if (isScalar(zcu, ty)) { | |
| 11523 | if (isScalar(pt.zcu, ty)) { | |
| 11453 | 11524 | it.zig_index += 1; |
| 11454 | 11525 | it.llvm_index += 1; |
| 11455 | 11526 | return .byval; |
| ... | ... | @@ -11550,7 +11621,7 @@ fn iterateParamTypes(object: *Object, fn_info: InternPool.Key.FuncType) ParamTyp |
| 11550 | 11621 | |
| 11551 | 11622 | fn ccAbiPromoteInt( |
| 11552 | 11623 | cc: std.builtin.CallingConvention, |
| 11553 | mod: *Module, | |
| 11624 | mod: *Zcu, | |
| 11554 | 11625 | ty: Type, |
| 11555 | 11626 | ) ?std.builtin.Signedness { |
| 11556 | 11627 | const target = mod.getTarget(); |
| ... | ... | @@ -11598,13 +11669,13 @@ fn ccAbiPromoteInt( |
| 11598 | 11669 | |
| 11599 | 11670 | /// This is the one source of truth for whether a type is passed around as an LLVM pointer, |
| 11600 | 11671 | /// or as an LLVM value. |
| 11601 | fn isByRef(ty: Type, mod: *Module) bool { | |
| 11672 | fn isByRef(ty: Type, pt: Zcu.PerThread) bool { | |
| 11602 | 11673 | // For tuples and structs, if there are more than this many non-void |
| 11603 | 11674 | // fields, then we make it byref, otherwise byval. |
| 11604 | 11675 | const max_fields_byval = 0; |
| 11605 | const ip = &mod.intern_pool; | |
| 11676 | const ip = &pt.zcu.intern_pool; | |
| 11606 | 11677 | |
| 11607 | switch (ty.zigTypeTag(mod)) { | |
| 11678 | switch (ty.zigTypeTag(pt.zcu)) { | |
| 11608 | 11679 | .Type, |
| 11609 | 11680 | .ComptimeInt, |
| 11610 | 11681 | .ComptimeFloat, |
| ... | ... | @@ -11627,17 +11698,17 @@ fn isByRef(ty: Type, mod: *Module) bool { |
| 11627 | 11698 | .AnyFrame, |
| 11628 | 11699 | => return false, |
| 11629 | 11700 | |
| 11630 | .Array, .Frame => return ty.hasRuntimeBits(mod), | |
| 11701 | .Array, .Frame => return ty.hasRuntimeBits(pt), | |
| 11631 | 11702 | .Struct => { |
| 11632 | 11703 | const struct_type = switch (ip.indexToKey(ty.toIntern())) { |
| 11633 | 11704 | .anon_struct_type => |tuple| { |
| 11634 | 11705 | var count: usize = 0; |
| 11635 | 11706 | for (tuple.types.get(ip), tuple.values.get(ip)) |field_ty, field_val| { |
| 11636 | if (field_val != .none or !Type.fromInterned(field_ty).hasRuntimeBits(mod)) continue; | |
| 11707 | if (field_val != .none or !Type.fromInterned(field_ty).hasRuntimeBits(pt)) continue; | |
| 11637 | 11708 | |
| 11638 | 11709 | count += 1; |
| 11639 | 11710 | if (count > max_fields_byval) return true; |
| 11640 | if (isByRef(Type.fromInterned(field_ty), mod)) return true; | |
| 11711 | if (isByRef(Type.fromInterned(field_ty), pt)) return true; | |
| 11641 | 11712 | } |
| 11642 | 11713 | return false; |
| 11643 | 11714 | }, |
| ... | ... | @@ -11655,27 +11726,27 @@ fn isByRef(ty: Type, mod: *Module) bool { |
| 11655 | 11726 | count += 1; |
| 11656 | 11727 | if (count > max_fields_byval) return true; |
| 11657 | 11728 | const field_ty = Type.fromInterned(field_types[field_index]); |
| 11658 | if (isByRef(field_ty, mod)) return true; | |
| 11729 | if (isByRef(field_ty, pt)) return true; | |
| 11659 | 11730 | } |
| 11660 | 11731 | return false; |
| 11661 | 11732 | }, |
| 11662 | .Union => switch (ty.containerLayout(mod)) { | |
| 11733 | .Union => switch (ty.containerLayout(pt.zcu)) { | |
| 11663 | 11734 | .@"packed" => return false, |
| 11664 | else => return ty.hasRuntimeBits(mod), | |
| 11735 | else => return ty.hasRuntimeBits(pt), | |
| 11665 | 11736 | }, |
| 11666 | 11737 | .ErrorUnion => { |
| 11667 | const payload_ty = ty.errorUnionPayload(mod); | |
| 11668 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 11738 | const payload_ty = ty.errorUnionPayload(pt.zcu); | |
| 11739 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 11669 | 11740 | return false; |
| 11670 | 11741 | } |
| 11671 | 11742 | return true; |
| 11672 | 11743 | }, |
| 11673 | 11744 | .Optional => { |
| 11674 | const payload_ty = ty.optionalChild(mod); | |
| 11675 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 11745 | const payload_ty = ty.optionalChild(pt.zcu); | |
| 11746 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 11676 | 11747 | return false; |
| 11677 | 11748 | } |
| 11678 | if (ty.optionalReprIsPayload(mod)) { | |
| 11749 | if (ty.optionalReprIsPayload(pt.zcu)) { | |
| 11679 | 11750 | return false; |
| 11680 | 11751 | } |
| 11681 | 11752 | return true; |
| ... | ... | @@ -11683,7 +11754,7 @@ fn isByRef(ty: Type, mod: *Module) bool { |
| 11683 | 11754 | } |
| 11684 | 11755 | } |
| 11685 | 11756 | |
| 11686 | fn isScalar(mod: *Module, ty: Type) bool { | |
| 11757 | fn isScalar(mod: *Zcu, ty: Type) bool { | |
| 11687 | 11758 | return switch (ty.zigTypeTag(mod)) { |
| 11688 | 11759 | .Void, |
| 11689 | 11760 | .Bool, |
| ... | ... | @@ -11774,7 +11845,7 @@ const lt_errors_fn_name = "__zig_lt_errors_len"; |
| 11774 | 11845 | /// Without this workaround, LLVM crashes with "unknown codeview register H1" |
| 11775 | 11846 | /// https://github.com/llvm/llvm-project/issues/56484 |
| 11776 | 11847 | fn needDbgVarWorkaround(o: *Object) bool { |
| 11777 | const target = o.module.getTarget(); | |
| 11848 | const target = o.pt.zcu.getTarget(); | |
| 11778 | 11849 | if (target.os.tag == .windows and target.cpu.arch == .aarch64) { |
| 11779 | 11850 | return true; |
| 11780 | 11851 | } |
| ... | ... | @@ -11817,14 +11888,14 @@ fn buildAllocaInner( |
| 11817 | 11888 | return wip.conv(.unneeded, alloca, .ptr, ""); |
| 11818 | 11889 | } |
| 11819 | 11890 | |
| 11820 | fn errUnionPayloadOffset(payload_ty: Type, mod: *Module) !u1 { | |
| 11821 | const err_int_ty = try mod.errorIntType(); | |
| 11822 | return @intFromBool(err_int_ty.abiAlignment(mod).compare(.gt, payload_ty.abiAlignment(mod))); | |
| 11891 | fn errUnionPayloadOffset(payload_ty: Type, pt: Zcu.PerThread) !u1 { | |
| 11892 | const err_int_ty = try pt.errorIntType(); | |
| 11893 | return @intFromBool(err_int_ty.abiAlignment(pt).compare(.gt, payload_ty.abiAlignment(pt))); | |
| 11823 | 11894 | } |
| 11824 | 11895 | |
| 11825 | fn errUnionErrorOffset(payload_ty: Type, mod: *Module) !u1 { | |
| 11826 | const err_int_ty = try mod.errorIntType(); | |
| 11827 | return @intFromBool(err_int_ty.abiAlignment(mod).compare(.lte, payload_ty.abiAlignment(mod))); | |
| 11896 | fn errUnionErrorOffset(payload_ty: Type, pt: Zcu.PerThread) !u1 { | |
| 11897 | const err_int_ty = try pt.errorIntType(); | |
| 11898 | return @intFromBool(err_int_ty.abiAlignment(pt).compare(.lte, payload_ty.abiAlignment(pt))); | |
| 11828 | 11899 | } |
| 11829 | 11900 | |
| 11830 | 11901 | /// Returns true for asm constraint (e.g. "=*m", "=r") if it accepts a memory location |
src/codegen/spirv.zig+239-211| ... | ... | @@ -6,9 +6,7 @@ const assert = std.debug.assert; |
| 6 | 6 | const Signedness = std.builtin.Signedness; |
| 7 | 7 | |
| 8 | 8 | const Zcu = @import("../Zcu.zig"); |
| 9 | /// Deprecated. | |
| 10 | const Module = Zcu; | |
| 11 | const Decl = Module.Decl; | |
| 9 | const Decl = Zcu.Decl; | |
| 12 | 10 | const Type = @import("../Type.zig"); |
| 13 | 11 | const Value = @import("../Value.zig"); |
| 14 | 12 | const Air = @import("../Air.zig"); |
| ... | ... | @@ -188,12 +186,13 @@ pub const Object = struct { |
| 188 | 186 | |
| 189 | 187 | fn genDecl( |
| 190 | 188 | self: *Object, |
| 191 | zcu: *Zcu, | |
| 189 | pt: Zcu.PerThread, | |
| 192 | 190 | decl_index: InternPool.DeclIndex, |
| 193 | 191 | air: Air, |
| 194 | 192 | liveness: Liveness, |
| 195 | 193 | ) !void { |
| 196 | const gpa = self.gpa; | |
| 194 | const zcu = pt.zcu; | |
| 195 | const gpa = zcu.gpa; | |
| 197 | 196 | const decl = zcu.declPtr(decl_index); |
| 198 | 197 | const namespace = zcu.namespacePtr(decl.src_namespace); |
| 199 | 198 | const structured_cfg = namespace.fileScope(zcu).mod.structured_cfg; |
| ... | ... | @@ -201,7 +200,7 @@ pub const Object = struct { |
| 201 | 200 | var decl_gen = DeclGen{ |
| 202 | 201 | .gpa = gpa, |
| 203 | 202 | .object = self, |
| 204 | .module = zcu, | |
| 203 | .pt = pt, | |
| 205 | 204 | .spv = &self.spv, |
| 206 | 205 | .decl_index = decl_index, |
| 207 | 206 | .air = air, |
| ... | ... | @@ -235,34 +234,34 @@ pub const Object = struct { |
| 235 | 234 | |
| 236 | 235 | pub fn updateFunc( |
| 237 | 236 | self: *Object, |
| 238 | mod: *Module, | |
| 237 | pt: Zcu.PerThread, | |
| 239 | 238 | func_index: InternPool.Index, |
| 240 | 239 | air: Air, |
| 241 | 240 | liveness: Liveness, |
| 242 | 241 | ) !void { |
| 243 | const decl_index = mod.funcInfo(func_index).owner_decl; | |
| 242 | const decl_index = pt.zcu.funcInfo(func_index).owner_decl; | |
| 244 | 243 | // TODO: Separate types for generating decls and functions? |
| 245 | try self.genDecl(mod, decl_index, air, liveness); | |
| 244 | try self.genDecl(pt, decl_index, air, liveness); | |
| 246 | 245 | } |
| 247 | 246 | |
| 248 | 247 | pub fn updateDecl( |
| 249 | 248 | self: *Object, |
| 250 | mod: *Module, | |
| 249 | pt: Zcu.PerThread, | |
| 251 | 250 | decl_index: InternPool.DeclIndex, |
| 252 | 251 | ) !void { |
| 253 | try self.genDecl(mod, decl_index, undefined, undefined); | |
| 252 | try self.genDecl(pt, decl_index, undefined, undefined); | |
| 254 | 253 | } |
| 255 | 254 | |
| 256 | 255 | /// Fetch or allocate a result id for decl index. This function also marks the decl as alive. |
| 257 | 256 | /// Note: Function does not actually generate the decl, it just allocates an index. |
| 258 | pub fn resolveDecl(self: *Object, mod: *Module, decl_index: InternPool.DeclIndex) !SpvModule.Decl.Index { | |
| 259 | const decl = mod.declPtr(decl_index); | |
| 257 | pub fn resolveDecl(self: *Object, zcu: *Zcu, decl_index: InternPool.DeclIndex) !SpvModule.Decl.Index { | |
| 258 | const decl = zcu.declPtr(decl_index); | |
| 260 | 259 | assert(decl.has_tv); // TODO: Do we need to handle a situation where this is false? |
| 261 | 260 | |
| 262 | 261 | const entry = try self.decl_link.getOrPut(self.gpa, decl_index); |
| 263 | 262 | if (!entry.found_existing) { |
| 264 | 263 | // TODO: Extern fn? |
| 265 | const kind: SpvModule.Decl.Kind = if (decl.val.isFuncBody(mod)) | |
| 264 | const kind: SpvModule.Decl.Kind = if (decl.val.isFuncBody(zcu)) | |
| 266 | 265 | .func |
| 267 | 266 | else switch (decl.@"addrspace") { |
| 268 | 267 | .generic => .invocation_global, |
| ... | ... | @@ -285,7 +284,7 @@ const DeclGen = struct { |
| 285 | 284 | object: *Object, |
| 286 | 285 | |
| 287 | 286 | /// The Zig module that we are generating decls for. |
| 288 | module: *Module, | |
| 287 | pt: Zcu.PerThread, | |
| 289 | 288 | |
| 290 | 289 | /// The SPIR-V module that instructions should be emitted into. |
| 291 | 290 | /// This is the same as `self.object.spv`, repeated here for brevity. |
| ... | ... | @@ -333,7 +332,7 @@ const DeclGen = struct { |
| 333 | 332 | |
| 334 | 333 | /// If `gen` returned `Error.CodegenFail`, this contains an explanatory message. |
| 335 | 334 | /// Memory is owned by `module.gpa`. |
| 336 | error_msg: ?*Module.ErrorMsg = null, | |
| 335 | error_msg: ?*Zcu.ErrorMsg = null, | |
| 337 | 336 | |
| 338 | 337 | /// Possible errors the `genDecl` function may return. |
| 339 | 338 | const Error = error{ CodegenFail, OutOfMemory }; |
| ... | ... | @@ -410,15 +409,15 @@ const DeclGen = struct { |
| 410 | 409 | |
| 411 | 410 | /// Return the target which we are currently compiling for. |
| 412 | 411 | pub fn getTarget(self: *DeclGen) std.Target { |
| 413 | return self.module.getTarget(); | |
| 412 | return self.pt.zcu.getTarget(); | |
| 414 | 413 | } |
| 415 | 414 | |
| 416 | 415 | pub fn fail(self: *DeclGen, comptime format: []const u8, args: anytype) Error { |
| 417 | 416 | @setCold(true); |
| 418 | const mod = self.module; | |
| 419 | const src_loc = self.module.declPtr(self.decl_index).navSrcLoc(mod); | |
| 417 | const zcu = self.pt.zcu; | |
| 418 | const src_loc = zcu.declPtr(self.decl_index).navSrcLoc(zcu); | |
| 420 | 419 | assert(self.error_msg == null); |
| 421 | self.error_msg = try Module.ErrorMsg.create(self.module.gpa, src_loc, format, args); | |
| 420 | self.error_msg = try Zcu.ErrorMsg.create(zcu.gpa, src_loc, format, args); | |
| 422 | 421 | return error.CodegenFail; |
| 423 | 422 | } |
| 424 | 423 | |
| ... | ... | @@ -439,8 +438,9 @@ const DeclGen = struct { |
| 439 | 438 | |
| 440 | 439 | /// Fetch the result-id for a previously generated instruction or constant. |
| 441 | 440 | fn resolve(self: *DeclGen, inst: Air.Inst.Ref) !IdRef { |
| 442 | const mod = self.module; | |
| 443 | if (try self.air.value(inst, mod)) |val| { | |
| 441 | const pt = self.pt; | |
| 442 | const mod = pt.zcu; | |
| 443 | if (try self.air.value(inst, pt)) |val| { | |
| 444 | 444 | const ty = self.typeOf(inst); |
| 445 | 445 | if (ty.zigTypeTag(mod) == .Fn) { |
| 446 | 446 | const fn_decl_index = switch (mod.intern_pool.indexToKey(val.ip_index)) { |
| ... | ... | @@ -462,7 +462,7 @@ const DeclGen = struct { |
| 462 | 462 | fn resolveAnonDecl(self: *DeclGen, val: InternPool.Index) !IdRef { |
| 463 | 463 | // TODO: This cannot be a function at this point, but it should probably be handled anyway. |
| 464 | 464 | |
| 465 | const mod = self.module; | |
| 465 | const mod = self.pt.zcu; | |
| 466 | 466 | const ty = Type.fromInterned(mod.intern_pool.typeOf(val)); |
| 467 | 467 | const decl_ptr_ty_id = try self.ptrType(ty, .Generic); |
| 468 | 468 | |
| ... | ... | @@ -642,7 +642,7 @@ const DeclGen = struct { |
| 642 | 642 | |
| 643 | 643 | /// Checks whether the type can be directly translated to SPIR-V vectors |
| 644 | 644 | fn isSpvVector(self: *DeclGen, ty: Type) bool { |
| 645 | const mod = self.module; | |
| 645 | const mod = self.pt.zcu; | |
| 646 | 646 | const target = self.getTarget(); |
| 647 | 647 | if (ty.zigTypeTag(mod) != .Vector) return false; |
| 648 | 648 | |
| ... | ... | @@ -668,7 +668,7 @@ const DeclGen = struct { |
| 668 | 668 | } |
| 669 | 669 | |
| 670 | 670 | fn arithmeticTypeInfo(self: *DeclGen, ty: Type) ArithmeticTypeInfo { |
| 671 | const mod = self.module; | |
| 671 | const mod = self.pt.zcu; | |
| 672 | 672 | const target = self.getTarget(); |
| 673 | 673 | var scalar_ty = ty.scalarType(mod); |
| 674 | 674 | if (scalar_ty.zigTypeTag(mod) == .Enum) { |
| ... | ... | @@ -744,7 +744,7 @@ const DeclGen = struct { |
| 744 | 744 | /// the value to an unsigned int first for Kernels. |
| 745 | 745 | fn constInt(self: *DeclGen, ty: Type, value: anytype, repr: Repr) !IdRef { |
| 746 | 746 | // TODO: Cache? |
| 747 | const mod = self.module; | |
| 747 | const mod = self.pt.zcu; | |
| 748 | 748 | const scalar_ty = ty.scalarType(mod); |
| 749 | 749 | const int_info = scalar_ty.intInfo(mod); |
| 750 | 750 | // Use backing bits so that negatives are sign extended |
| ... | ... | @@ -824,7 +824,7 @@ const DeclGen = struct { |
| 824 | 824 | /// Construct a vector at runtime. |
| 825 | 825 | /// ty must be an vector type. |
| 826 | 826 | fn constructVector(self: *DeclGen, ty: Type, constituents: []const IdRef) !IdRef { |
| 827 | const mod = self.module; | |
| 827 | const mod = self.pt.zcu; | |
| 828 | 828 | assert(ty.vectorLen(mod) == constituents.len); |
| 829 | 829 | |
| 830 | 830 | // Note: older versions of the Khronos SPRIV-LLVM translator crash on this instruction |
| ... | ... | @@ -848,7 +848,7 @@ const DeclGen = struct { |
| 848 | 848 | /// Construct a vector at runtime with all lanes set to the same value. |
| 849 | 849 | /// ty must be an vector type. |
| 850 | 850 | fn constructVectorSplat(self: *DeclGen, ty: Type, constituent: IdRef) !IdRef { |
| 851 | const mod = self.module; | |
| 851 | const mod = self.pt.zcu; | |
| 852 | 852 | const n = ty.vectorLen(mod); |
| 853 | 853 | |
| 854 | 854 | const constituents = try self.gpa.alloc(IdRef, n); |
| ... | ... | @@ -886,12 +886,13 @@ const DeclGen = struct { |
| 886 | 886 | return id; |
| 887 | 887 | } |
| 888 | 888 | |
| 889 | const mod = self.module; | |
| 889 | const pt = self.pt; | |
| 890 | const mod = pt.zcu; | |
| 890 | 891 | const target = self.getTarget(); |
| 891 | 892 | const result_ty_id = try self.resolveType(ty, repr); |
| 892 | 893 | const ip = &mod.intern_pool; |
| 893 | 894 | |
| 894 | log.debug("lowering constant: ty = {}, val = {}", .{ ty.fmt(mod), val.fmtValue(mod, null) }); | |
| 895 | log.debug("lowering constant: ty = {}, val = {}", .{ ty.fmt(pt), val.fmtValue(pt, null) }); | |
| 895 | 896 | if (val.isUndefDeep(mod)) { |
| 896 | 897 | return self.spv.constUndef(result_ty_id); |
| 897 | 898 | } |
| ... | ... | @@ -940,16 +941,16 @@ const DeclGen = struct { |
| 940 | 941 | }, |
| 941 | 942 | .int => { |
| 942 | 943 | if (ty.isSignedInt(mod)) { |
| 943 | break :cache try self.constInt(ty, val.toSignedInt(mod), repr); | |
| 944 | break :cache try self.constInt(ty, val.toSignedInt(pt), repr); | |
| 944 | 945 | } else { |
| 945 | break :cache try self.constInt(ty, val.toUnsignedInt(mod), repr); | |
| 946 | break :cache try self.constInt(ty, val.toUnsignedInt(pt), repr); | |
| 946 | 947 | } |
| 947 | 948 | }, |
| 948 | 949 | .float => { |
| 949 | 950 | const lit: spec.LiteralContextDependentNumber = switch (ty.floatBits(target)) { |
| 950 | 16 => .{ .uint32 = @as(u16, @bitCast(val.toFloat(f16, mod))) }, | |
| 951 | 32 => .{ .float32 = val.toFloat(f32, mod) }, | |
| 952 | 64 => .{ .float64 = val.toFloat(f64, mod) }, | |
| 951 | 16 => .{ .uint32 = @as(u16, @bitCast(val.toFloat(f16, pt))) }, | |
| 952 | 32 => .{ .float32 = val.toFloat(f32, pt) }, | |
| 953 | 64 => .{ .float64 = val.toFloat(f64, pt) }, | |
| 953 | 954 | 80, 128 => unreachable, // TODO |
| 954 | 955 | else => unreachable, |
| 955 | 956 | }; |
| ... | ... | @@ -968,17 +969,17 @@ const DeclGen = struct { |
| 968 | 969 | .error_union => |error_union| { |
| 969 | 970 | // TODO: Error unions may be constructed with constant instructions if the payload type |
| 970 | 971 | // allows it. For now, just generate it here regardless. |
| 971 | const err_int_ty = try mod.errorIntType(); | |
| 972 | const err_int_ty = try pt.errorIntType(); | |
| 972 | 973 | const err_ty = switch (error_union.val) { |
| 973 | 974 | .err_name => ty.errorUnionSet(mod), |
| 974 | 975 | .payload => err_int_ty, |
| 975 | 976 | }; |
| 976 | 977 | const err_val = switch (error_union.val) { |
| 977 | .err_name => |err_name| Value.fromInterned((try mod.intern(.{ .err = .{ | |
| 978 | .err_name => |err_name| Value.fromInterned(try pt.intern(.{ .err = .{ | |
| 978 | 979 | .ty = ty.errorUnionSet(mod).toIntern(), |
| 979 | 980 | .name = err_name, |
| 980 | } }))), | |
| 981 | .payload => try mod.intValue(err_int_ty, 0), | |
| 981 | } })), | |
| 982 | .payload => try pt.intValue(err_int_ty, 0), | |
| 982 | 983 | }; |
| 983 | 984 | const payload_ty = ty.errorUnionPayload(mod); |
| 984 | 985 | const eu_layout = self.errorUnionLayout(payload_ty); |
| ... | ... | @@ -988,7 +989,7 @@ const DeclGen = struct { |
| 988 | 989 | } |
| 989 | 990 | |
| 990 | 991 | const payload_val = Value.fromInterned(switch (error_union.val) { |
| 991 | .err_name => try mod.intern(.{ .undef = payload_ty.toIntern() }), | |
| 992 | .err_name => try pt.intern(.{ .undef = payload_ty.toIntern() }), | |
| 992 | 993 | .payload => |payload| payload, |
| 993 | 994 | }); |
| 994 | 995 | |
| ... | ... | @@ -1007,7 +1008,7 @@ const DeclGen = struct { |
| 1007 | 1008 | return try self.constructStruct(ty, &types, &constituents); |
| 1008 | 1009 | }, |
| 1009 | 1010 | .enum_tag => { |
| 1010 | const int_val = try val.intFromEnum(ty, mod); | |
| 1011 | const int_val = try val.intFromEnum(ty, pt); | |
| 1011 | 1012 | const int_ty = ty.intTagType(mod); |
| 1012 | 1013 | break :cache try self.constant(int_ty, int_val, repr); |
| 1013 | 1014 | }, |
| ... | ... | @@ -1026,7 +1027,7 @@ const DeclGen = struct { |
| 1026 | 1027 | const payload_ty = ty.optionalChild(mod); |
| 1027 | 1028 | const maybe_payload_val = val.optionalValue(mod); |
| 1028 | 1029 | |
| 1029 | if (!payload_ty.hasRuntimeBits(mod)) { | |
| 1030 | if (!payload_ty.hasRuntimeBits(pt)) { | |
| 1030 | 1031 | break :cache try self.constBool(maybe_payload_val != null, .indirect); |
| 1031 | 1032 | } else if (ty.optionalReprIsPayload(mod)) { |
| 1032 | 1033 | // Optional representation is a nullable pointer or slice. |
| ... | ... | @@ -1104,13 +1105,13 @@ const DeclGen = struct { |
| 1104 | 1105 | var it = struct_type.iterateRuntimeOrder(ip); |
| 1105 | 1106 | while (it.next()) |field_index| { |
| 1106 | 1107 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 1107 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 1108 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1108 | 1109 | // This is a zero-bit field - we only needed it for the alignment. |
| 1109 | 1110 | continue; |
| 1110 | 1111 | } |
| 1111 | 1112 | |
| 1112 | 1113 | // TODO: Padding? |
| 1113 | const field_val = try val.fieldValue(mod, field_index); | |
| 1114 | const field_val = try val.fieldValue(pt, field_index); | |
| 1114 | 1115 | const field_id = try self.constant(field_ty, field_val, .indirect); |
| 1115 | 1116 | |
| 1116 | 1117 | try types.append(field_ty); |
| ... | ... | @@ -1126,7 +1127,7 @@ const DeclGen = struct { |
| 1126 | 1127 | const active_field = ty.unionTagFieldIndex(Value.fromInterned(un.tag), mod).?; |
| 1127 | 1128 | const union_obj = mod.typeToUnion(ty).?; |
| 1128 | 1129 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[active_field]); |
| 1129 | const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(mod)) | |
| 1130 | const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(pt)) | |
| 1130 | 1131 | try self.constant(field_ty, Value.fromInterned(un.val), .direct) |
| 1131 | 1132 | else |
| 1132 | 1133 | null; |
| ... | ... | @@ -1144,10 +1145,10 @@ const DeclGen = struct { |
| 1144 | 1145 | fn constantPtr(self: *DeclGen, ptr_val: Value) Error!IdRef { |
| 1145 | 1146 | // TODO: Caching?? |
| 1146 | 1147 | |
| 1147 | const zcu = self.module; | |
| 1148 | const pt = self.pt; | |
| 1148 | 1149 | |
| 1149 | if (ptr_val.isUndef(zcu)) { | |
| 1150 | const result_ty = ptr_val.typeOf(zcu); | |
| 1150 | if (ptr_val.isUndef(pt.zcu)) { | |
| 1151 | const result_ty = ptr_val.typeOf(pt.zcu); | |
| 1151 | 1152 | const result_ty_id = try self.resolveType(result_ty, .direct); |
| 1152 | 1153 | return self.spv.constUndef(result_ty_id); |
| 1153 | 1154 | } |
| ... | ... | @@ -1155,12 +1156,13 @@ const DeclGen = struct { |
| 1155 | 1156 | var arena = std.heap.ArenaAllocator.init(self.gpa); |
| 1156 | 1157 | defer arena.deinit(); |
| 1157 | 1158 | |
| 1158 | const derivation = try ptr_val.pointerDerivation(arena.allocator(), zcu); | |
| 1159 | const derivation = try ptr_val.pointerDerivation(arena.allocator(), pt); | |
| 1159 | 1160 | return self.derivePtr(derivation); |
| 1160 | 1161 | } |
| 1161 | 1162 | |
| 1162 | 1163 | fn derivePtr(self: *DeclGen, derivation: Value.PointerDeriveStep) Error!IdRef { |
| 1163 | const zcu = self.module; | |
| 1164 | const pt = self.pt; | |
| 1165 | const zcu = pt.zcu; | |
| 1164 | 1166 | switch (derivation) { |
| 1165 | 1167 | .comptime_alloc_ptr, .comptime_field_ptr => unreachable, |
| 1166 | 1168 | .int => |int| { |
| ... | ... | @@ -1172,12 +1174,12 @@ const DeclGen = struct { |
| 1172 | 1174 | try self.func.body.emit(self.spv.gpa, .OpConvertUToPtr, .{ |
| 1173 | 1175 | .id_result_type = result_ty_id, |
| 1174 | 1176 | .id_result = result_ptr_id, |
| 1175 | .integer_value = try self.constant(Type.usize, try zcu.intValue(Type.usize, int.addr), .direct), | |
| 1177 | .integer_value = try self.constant(Type.usize, try pt.intValue(Type.usize, int.addr), .direct), | |
| 1176 | 1178 | }); |
| 1177 | 1179 | return result_ptr_id; |
| 1178 | 1180 | }, |
| 1179 | 1181 | .decl_ptr => |decl| { |
| 1180 | const result_ptr_ty = try zcu.declPtr(decl).declPtrType(zcu); | |
| 1182 | const result_ptr_ty = try zcu.declPtr(decl).declPtrType(pt); | |
| 1181 | 1183 | return self.constantDeclRef(result_ptr_ty, decl); |
| 1182 | 1184 | }, |
| 1183 | 1185 | .anon_decl_ptr => |ad| { |
| ... | ... | @@ -1188,18 +1190,18 @@ const DeclGen = struct { |
| 1188 | 1190 | .opt_payload_ptr => @panic("TODO"), |
| 1189 | 1191 | .field_ptr => |field| { |
| 1190 | 1192 | const parent_ptr_id = try self.derivePtr(field.parent.*); |
| 1191 | const parent_ptr_ty = try field.parent.ptrType(zcu); | |
| 1193 | const parent_ptr_ty = try field.parent.ptrType(pt); | |
| 1192 | 1194 | return self.structFieldPtr(field.result_ptr_ty, parent_ptr_ty, parent_ptr_id, field.field_idx); |
| 1193 | 1195 | }, |
| 1194 | 1196 | .elem_ptr => |elem| { |
| 1195 | 1197 | const parent_ptr_id = try self.derivePtr(elem.parent.*); |
| 1196 | const parent_ptr_ty = try elem.parent.ptrType(zcu); | |
| 1198 | const parent_ptr_ty = try elem.parent.ptrType(pt); | |
| 1197 | 1199 | const index_id = try self.constInt(Type.usize, elem.elem_idx, .direct); |
| 1198 | 1200 | return self.ptrElemPtr(parent_ptr_ty, parent_ptr_id, index_id); |
| 1199 | 1201 | }, |
| 1200 | 1202 | .offset_and_cast => |oac| { |
| 1201 | 1203 | const parent_ptr_id = try self.derivePtr(oac.parent.*); |
| 1202 | const parent_ptr_ty = try oac.parent.ptrType(zcu); | |
| 1204 | const parent_ptr_ty = try oac.parent.ptrType(pt); | |
| 1203 | 1205 | disallow: { |
| 1204 | 1206 | if (oac.byte_offset != 0) break :disallow; |
| 1205 | 1207 | // Allow changing the pointer type child only to restructure arrays. |
| ... | ... | @@ -1218,8 +1220,8 @@ const DeclGen = struct { |
| 1218 | 1220 | return result_ptr_id; |
| 1219 | 1221 | } |
| 1220 | 1222 | return self.fail("Cannot perform pointer cast: '{}' to '{}'", .{ |
| 1221 | parent_ptr_ty.fmt(zcu), | |
| 1222 | oac.new_ptr_ty.fmt(zcu), | |
| 1223 | parent_ptr_ty.fmt(pt), | |
| 1224 | oac.new_ptr_ty.fmt(pt), | |
| 1223 | 1225 | }); |
| 1224 | 1226 | }, |
| 1225 | 1227 | } |
| ... | ... | @@ -1232,7 +1234,8 @@ const DeclGen = struct { |
| 1232 | 1234 | ) !IdRef { |
| 1233 | 1235 | // TODO: Merge this function with constantDeclRef. |
| 1234 | 1236 | |
| 1235 | const mod = self.module; | |
| 1237 | const pt = self.pt; | |
| 1238 | const mod = pt.zcu; | |
| 1236 | 1239 | const ip = &mod.intern_pool; |
| 1237 | 1240 | const ty_id = try self.resolveType(ty, .direct); |
| 1238 | 1241 | const decl_val = anon_decl.val; |
| ... | ... | @@ -1247,7 +1250,7 @@ const DeclGen = struct { |
| 1247 | 1250 | } |
| 1248 | 1251 | |
| 1249 | 1252 | // const is_fn_body = decl_ty.zigTypeTag(mod) == .Fn; |
| 1250 | if (!decl_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { | |
| 1253 | if (!decl_ty.isFnOrHasRuntimeBitsIgnoreComptime(pt)) { | |
| 1251 | 1254 | // Pointer to nothing - return undefoined |
| 1252 | 1255 | return self.spv.constUndef(ty_id); |
| 1253 | 1256 | } |
| ... | ... | @@ -1276,7 +1279,8 @@ const DeclGen = struct { |
| 1276 | 1279 | } |
| 1277 | 1280 | |
| 1278 | 1281 | fn constantDeclRef(self: *DeclGen, ty: Type, decl_index: InternPool.DeclIndex) !IdRef { |
| 1279 | const mod = self.module; | |
| 1282 | const pt = self.pt; | |
| 1283 | const mod = pt.zcu; | |
| 1280 | 1284 | const ty_id = try self.resolveType(ty, .direct); |
| 1281 | 1285 | const decl = mod.declPtr(decl_index); |
| 1282 | 1286 | |
| ... | ... | @@ -1290,7 +1294,7 @@ const DeclGen = struct { |
| 1290 | 1294 | else => {}, |
| 1291 | 1295 | } |
| 1292 | 1296 | |
| 1293 | if (!decl.typeOf(mod).isFnOrHasRuntimeBitsIgnoreComptime(mod)) { | |
| 1297 | if (!decl.typeOf(mod).isFnOrHasRuntimeBitsIgnoreComptime(pt)) { | |
| 1294 | 1298 | // Pointer to nothing - return undefined. |
| 1295 | 1299 | return self.spv.constUndef(ty_id); |
| 1296 | 1300 | } |
| ... | ... | @@ -1331,7 +1335,7 @@ const DeclGen = struct { |
| 1331 | 1335 | fn resolveTypeName(self: *DeclGen, ty: Type) ![]const u8 { |
| 1332 | 1336 | var name = std.ArrayList(u8).init(self.gpa); |
| 1333 | 1337 | defer name.deinit(); |
| 1334 | try ty.print(name.writer(), self.module); | |
| 1338 | try ty.print(name.writer(), self.pt); | |
| 1335 | 1339 | return try name.toOwnedSlice(); |
| 1336 | 1340 | } |
| 1337 | 1341 | |
| ... | ... | @@ -1424,14 +1428,14 @@ const DeclGen = struct { |
| 1424 | 1428 | } |
| 1425 | 1429 | |
| 1426 | 1430 | fn zigScalarOrVectorTypeLike(self: *DeclGen, new_ty: Type, base_ty: Type) !Type { |
| 1427 | const mod = self.module; | |
| 1428 | const new_scalar_ty = new_ty.scalarType(mod); | |
| 1429 | if (!base_ty.isVector(mod)) { | |
| 1431 | const pt = self.pt; | |
| 1432 | const new_scalar_ty = new_ty.scalarType(pt.zcu); | |
| 1433 | if (!base_ty.isVector(pt.zcu)) { | |
| 1430 | 1434 | return new_scalar_ty; |
| 1431 | 1435 | } |
| 1432 | 1436 | |
| 1433 | return try mod.vectorType(.{ | |
| 1434 | .len = base_ty.vectorLen(mod), | |
| 1437 | return try pt.vectorType(.{ | |
| 1438 | .len = base_ty.vectorLen(pt.zcu), | |
| 1435 | 1439 | .child = new_scalar_ty.toIntern(), |
| 1436 | 1440 | }); |
| 1437 | 1441 | } |
| ... | ... | @@ -1455,7 +1459,7 @@ const DeclGen = struct { |
| 1455 | 1459 | /// } |
| 1456 | 1460 | /// If any of the fields' size is 0, it will be omitted. |
| 1457 | 1461 | fn resolveUnionType(self: *DeclGen, ty: Type) !IdRef { |
| 1458 | const mod = self.module; | |
| 1462 | const mod = self.pt.zcu; | |
| 1459 | 1463 | const ip = &mod.intern_pool; |
| 1460 | 1464 | const union_obj = mod.typeToUnion(ty).?; |
| 1461 | 1465 | |
| ... | ... | @@ -1506,12 +1510,12 @@ const DeclGen = struct { |
| 1506 | 1510 | } |
| 1507 | 1511 | |
| 1508 | 1512 | fn resolveFnReturnType(self: *DeclGen, ret_ty: Type) !IdRef { |
| 1509 | const mod = self.module; | |
| 1510 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 1513 | const pt = self.pt; | |
| 1514 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1511 | 1515 | // If the return type is an error set or an error union, then we make this |
| 1512 | 1516 | // anyerror return type instead, so that it can be coerced into a function |
| 1513 | 1517 | // pointer type which has anyerror as the return type. |
| 1514 | if (ret_ty.isError(mod)) { | |
| 1518 | if (ret_ty.isError(pt.zcu)) { | |
| 1515 | 1519 | return self.resolveType(Type.anyerror, .direct); |
| 1516 | 1520 | } else { |
| 1517 | 1521 | return self.resolveType(Type.void, .direct); |
| ... | ... | @@ -1533,9 +1537,10 @@ const DeclGen = struct { |
| 1533 | 1537 | } |
| 1534 | 1538 | |
| 1535 | 1539 | fn resolveTypeInner(self: *DeclGen, ty: Type, repr: Repr) Error!IdRef { |
| 1536 | const mod = self.module; | |
| 1540 | const pt = self.pt; | |
| 1541 | const mod = pt.zcu; | |
| 1537 | 1542 | const ip = &mod.intern_pool; |
| 1538 | log.debug("resolveType: ty = {}", .{ty.fmt(mod)}); | |
| 1543 | log.debug("resolveType: ty = {}", .{ty.fmt(pt)}); | |
| 1539 | 1544 | const target = self.getTarget(); |
| 1540 | 1545 | |
| 1541 | 1546 | const section = &self.spv.sections.types_globals_constants; |
| ... | ... | @@ -1607,7 +1612,7 @@ const DeclGen = struct { |
| 1607 | 1612 | return self.fail("array type of {} elements is too large", .{ty.arrayLenIncludingSentinel(mod)}); |
| 1608 | 1613 | }; |
| 1609 | 1614 | |
| 1610 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 1615 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1611 | 1616 | // The size of the array would be 0, but that is not allowed in SPIR-V. |
| 1612 | 1617 | // This path can be reached when the backend is asked to generate a pointer to |
| 1613 | 1618 | // an array of some zero-bit type. This should always be an indirect path. |
| ... | ... | @@ -1655,7 +1660,7 @@ const DeclGen = struct { |
| 1655 | 1660 | var param_index: usize = 0; |
| 1656 | 1661 | for (fn_info.param_types.get(ip)) |param_ty_index| { |
| 1657 | 1662 | const param_ty = Type.fromInterned(param_ty_index); |
| 1658 | if (!param_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; | |
| 1663 | if (!param_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 1659 | 1664 | |
| 1660 | 1665 | param_ty_ids[param_index] = try self.resolveType(param_ty, .direct); |
| 1661 | 1666 | param_index += 1; |
| ... | ... | @@ -1713,7 +1718,7 @@ const DeclGen = struct { |
| 1713 | 1718 | |
| 1714 | 1719 | var member_index: usize = 0; |
| 1715 | 1720 | for (tuple.types.get(ip), tuple.values.get(ip)) |field_ty, field_val| { |
| 1716 | if (field_val != .none or !Type.fromInterned(field_ty).hasRuntimeBits(mod)) continue; | |
| 1721 | if (field_val != .none or !Type.fromInterned(field_ty).hasRuntimeBits(pt)) continue; | |
| 1717 | 1722 | |
| 1718 | 1723 | member_types[member_index] = try self.resolveType(Type.fromInterned(field_ty), .indirect); |
| 1719 | 1724 | member_index += 1; |
| ... | ... | @@ -1742,7 +1747,7 @@ const DeclGen = struct { |
| 1742 | 1747 | var it = struct_type.iterateRuntimeOrder(ip); |
| 1743 | 1748 | while (it.next()) |field_index| { |
| 1744 | 1749 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 1745 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 1750 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1746 | 1751 | // This is a zero-bit field - we only needed it for the alignment. |
| 1747 | 1752 | continue; |
| 1748 | 1753 | } |
| ... | ... | @@ -1761,7 +1766,7 @@ const DeclGen = struct { |
| 1761 | 1766 | }, |
| 1762 | 1767 | .Optional => { |
| 1763 | 1768 | const payload_ty = ty.optionalChild(mod); |
| 1764 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 1769 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 1765 | 1770 | // Just use a bool. |
| 1766 | 1771 | // Note: Always generate the bool with indirect format, to save on some sanity |
| 1767 | 1772 | // Perform the conversion to a direct bool when the field is extracted. |
| ... | ... | @@ -1878,14 +1883,14 @@ const DeclGen = struct { |
| 1878 | 1883 | }; |
| 1879 | 1884 | |
| 1880 | 1885 | fn errorUnionLayout(self: *DeclGen, payload_ty: Type) ErrorUnionLayout { |
| 1881 | const mod = self.module; | |
| 1886 | const pt = self.pt; | |
| 1882 | 1887 | |
| 1883 | const error_align = Type.anyerror.abiAlignment(mod); | |
| 1884 | const payload_align = payload_ty.abiAlignment(mod); | |
| 1888 | const error_align = Type.anyerror.abiAlignment(pt); | |
| 1889 | const payload_align = payload_ty.abiAlignment(pt); | |
| 1885 | 1890 | |
| 1886 | 1891 | const error_first = error_align.compare(.gt, payload_align); |
| 1887 | 1892 | return .{ |
| 1888 | .payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(mod), | |
| 1893 | .payload_has_bits = payload_ty.hasRuntimeBitsIgnoreComptime(pt), | |
| 1889 | 1894 | .error_first = error_first, |
| 1890 | 1895 | }; |
| 1891 | 1896 | } |
| ... | ... | @@ -1909,9 +1914,10 @@ const DeclGen = struct { |
| 1909 | 1914 | }; |
| 1910 | 1915 | |
| 1911 | 1916 | fn unionLayout(self: *DeclGen, ty: Type) UnionLayout { |
| 1912 | const mod = self.module; | |
| 1917 | const pt = self.pt; | |
| 1918 | const mod = pt.zcu; | |
| 1913 | 1919 | const ip = &mod.intern_pool; |
| 1914 | const layout = ty.unionGetLayout(self.module); | |
| 1920 | const layout = ty.unionGetLayout(pt); | |
| 1915 | 1921 | const union_obj = mod.typeToUnion(ty).?; |
| 1916 | 1922 | |
| 1917 | 1923 | var union_layout = UnionLayout{ |
| ... | ... | @@ -1932,7 +1938,7 @@ const DeclGen = struct { |
| 1932 | 1938 | const most_aligned_field = layout.most_aligned_field; |
| 1933 | 1939 | const most_aligned_field_ty = Type.fromInterned(union_obj.field_types.get(ip)[most_aligned_field]); |
| 1934 | 1940 | union_layout.payload_ty = most_aligned_field_ty; |
| 1935 | union_layout.payload_size = @intCast(most_aligned_field_ty.abiSize(mod)); | |
| 1941 | union_layout.payload_size = @intCast(most_aligned_field_ty.abiSize(pt)); | |
| 1936 | 1942 | } else { |
| 1937 | 1943 | union_layout.payload_size = 0; |
| 1938 | 1944 | } |
| ... | ... | @@ -1999,7 +2005,7 @@ const DeclGen = struct { |
| 1999 | 2005 | } |
| 2000 | 2006 | |
| 2001 | 2007 | fn materialize(self: Temporary, dg: *DeclGen) !IdResult { |
| 2002 | const mod = dg.module; | |
| 2008 | const mod = dg.pt.zcu; | |
| 2003 | 2009 | switch (self.value) { |
| 2004 | 2010 | .singleton => |id| return id, |
| 2005 | 2011 | .exploded_vector => |range| { |
| ... | ... | @@ -2029,12 +2035,12 @@ const DeclGen = struct { |
| 2029 | 2035 | /// 'Explode' a temporary into separate elements. This turns a vector |
| 2030 | 2036 | /// into a bag of elements. |
| 2031 | 2037 | fn explode(self: Temporary, dg: *DeclGen) !IdRange { |
| 2032 | const mod = dg.module; | |
| 2038 | const mod = dg.pt.zcu; | |
| 2033 | 2039 | |
| 2034 | 2040 | // If the value is a scalar, then this is a no-op. |
| 2035 | 2041 | if (!self.ty.isVector(mod)) { |
| 2036 | 2042 | return switch (self.value) { |
| 2037 | .singleton => |id| IdRange{ .base = @intFromEnum(id), .len = 1 }, | |
| 2043 | .singleton => |id| .{ .base = @intFromEnum(id), .len = 1 }, | |
| 2038 | 2044 | .exploded_vector => |range| range, |
| 2039 | 2045 | }; |
| 2040 | 2046 | } |
| ... | ... | @@ -2088,7 +2094,7 @@ const DeclGen = struct { |
| 2088 | 2094 | /// only checks the size, but the source-of-truth is implemented |
| 2089 | 2095 | /// by `isSpvVector()`. |
| 2090 | 2096 | fn fromType(ty: Type, dg: *DeclGen) Vectorization { |
| 2091 | const mod = dg.module; | |
| 2097 | const mod = dg.pt.zcu; | |
| 2092 | 2098 | if (!ty.isVector(mod)) { |
| 2093 | 2099 | return .scalar; |
| 2094 | 2100 | } else if (dg.isSpvVector(ty)) { |
| ... | ... | @@ -2164,11 +2170,11 @@ const DeclGen = struct { |
| 2164 | 2170 | /// Turns `ty` into the result-type of an individual vector operation. |
| 2165 | 2171 | /// `ty` may be a scalar or vector, it doesn't matter. |
| 2166 | 2172 | fn operationType(self: Vectorization, dg: *DeclGen, ty: Type) !Type { |
| 2167 | const mod = dg.module; | |
| 2168 | const scalar_ty = ty.scalarType(mod); | |
| 2173 | const pt = dg.pt; | |
| 2174 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 2169 | 2175 | return switch (self) { |
| 2170 | 2176 | .scalar, .unrolled => scalar_ty, |
| 2171 | .spv_vectorized => |n| try mod.vectorType(.{ | |
| 2177 | .spv_vectorized => |n| try pt.vectorType(.{ | |
| 2172 | 2178 | .len = n, |
| 2173 | 2179 | .child = scalar_ty.toIntern(), |
| 2174 | 2180 | }), |
| ... | ... | @@ -2178,11 +2184,11 @@ const DeclGen = struct { |
| 2178 | 2184 | /// Turns `ty` into the result-type of the entire operation. |
| 2179 | 2185 | /// `ty` may be a scalar or vector, it doesn't matter. |
| 2180 | 2186 | fn resultType(self: Vectorization, dg: *DeclGen, ty: Type) !Type { |
| 2181 | const mod = dg.module; | |
| 2182 | const scalar_ty = ty.scalarType(mod); | |
| 2187 | const pt = dg.pt; | |
| 2188 | const scalar_ty = ty.scalarType(pt.zcu); | |
| 2183 | 2189 | return switch (self) { |
| 2184 | 2190 | .scalar => scalar_ty, |
| 2185 | .unrolled, .spv_vectorized => |n| try mod.vectorType(.{ | |
| 2191 | .unrolled, .spv_vectorized => |n| try pt.vectorType(.{ | |
| 2186 | 2192 | .len = n, |
| 2187 | 2193 | .child = scalar_ty.toIntern(), |
| 2188 | 2194 | }), |
| ... | ... | @@ -2193,8 +2199,8 @@ const DeclGen = struct { |
| 2193 | 2199 | /// this setup, and returns a new type that holds the relevant information on how to access |
| 2194 | 2200 | /// elements of the input. |
| 2195 | 2201 | fn prepare(self: Vectorization, dg: *DeclGen, tmp: Temporary) !PreparedOperand { |
| 2196 | const mod = dg.module; | |
| 2197 | const is_vector = tmp.ty.isVector(mod); | |
| 2202 | const pt = dg.pt; | |
| 2203 | const is_vector = tmp.ty.isVector(pt.zcu); | |
| 2198 | 2204 | const is_spv_vector = dg.isSpvVector(tmp.ty); |
| 2199 | 2205 | const value: PreparedOperand.Value = switch (tmp.value) { |
| 2200 | 2206 | .singleton => |id| switch (self) { |
| ... | ... | @@ -2209,7 +2215,7 @@ const DeclGen = struct { |
| 2209 | 2215 | } |
| 2210 | 2216 | |
| 2211 | 2217 | // Broadcast scalar into vector. |
| 2212 | const vector_ty = try mod.vectorType(.{ | |
| 2218 | const vector_ty = try pt.vectorType(.{ | |
| 2213 | 2219 | .len = self.components(), |
| 2214 | 2220 | .child = tmp.ty.toIntern(), |
| 2215 | 2221 | }); |
| ... | ... | @@ -2340,7 +2346,7 @@ const DeclGen = struct { |
| 2340 | 2346 | /// This function builds an OpSConvert of OpUConvert depending on the |
| 2341 | 2347 | /// signedness of the types. |
| 2342 | 2348 | fn buildIntConvert(self: *DeclGen, dst_ty: Type, src: Temporary) !Temporary { |
| 2343 | const mod = self.module; | |
| 2349 | const mod = self.pt.zcu; | |
| 2344 | 2350 | |
| 2345 | 2351 | const dst_ty_id = try self.resolveType(dst_ty.scalarType(mod), .direct); |
| 2346 | 2352 | const src_ty_id = try self.resolveType(src.ty.scalarType(mod), .direct); |
| ... | ... | @@ -2419,7 +2425,7 @@ const DeclGen = struct { |
| 2419 | 2425 | } |
| 2420 | 2426 | |
| 2421 | 2427 | fn buildSelect(self: *DeclGen, condition: Temporary, lhs: Temporary, rhs: Temporary) !Temporary { |
| 2422 | const mod = self.module; | |
| 2428 | const mod = self.pt.zcu; | |
| 2423 | 2429 | |
| 2424 | 2430 | const v = self.vectorization(.{ condition, lhs, rhs }); |
| 2425 | 2431 | const ops = v.operations(); |
| ... | ... | @@ -2764,7 +2770,8 @@ const DeclGen = struct { |
| 2764 | 2770 | lhs: Temporary, |
| 2765 | 2771 | rhs: Temporary, |
| 2766 | 2772 | ) !struct { Temporary, Temporary } { |
| 2767 | const mod = self.module; | |
| 2773 | const pt = self.pt; | |
| 2774 | const mod = pt.zcu; | |
| 2768 | 2775 | const target = self.getTarget(); |
| 2769 | 2776 | const ip = &mod.intern_pool; |
| 2770 | 2777 | |
| ... | ... | @@ -2814,7 +2821,7 @@ const DeclGen = struct { |
| 2814 | 2821 | // where T is maybe vectorized. |
| 2815 | 2822 | const types = [2]InternPool.Index{ arith_op_ty.toIntern(), arith_op_ty.toIntern() }; |
| 2816 | 2823 | const values = [2]InternPool.Index{ .none, .none }; |
| 2817 | const index = try ip.getAnonStructType(mod.gpa, .{ | |
| 2824 | const index = try ip.getAnonStructType(mod.gpa, pt.tid, .{ | |
| 2818 | 2825 | .types = &types, |
| 2819 | 2826 | .values = &values, |
| 2820 | 2827 | .names = &.{}, |
| ... | ... | @@ -2888,7 +2895,7 @@ const DeclGen = struct { |
| 2888 | 2895 | /// the name of an error in the text executor. |
| 2889 | 2896 | fn generateTestEntryPoint(self: *DeclGen, name: []const u8, spv_test_decl_index: SpvModule.Decl.Index) !void { |
| 2890 | 2897 | const anyerror_ty_id = try self.resolveType(Type.anyerror, .direct); |
| 2891 | const ptr_anyerror_ty = try self.module.ptrType(.{ | |
| 2898 | const ptr_anyerror_ty = try self.pt.ptrType(.{ | |
| 2892 | 2899 | .child = Type.anyerror.toIntern(), |
| 2893 | 2900 | .flags = .{ .address_space = .global }, |
| 2894 | 2901 | }); |
| ... | ... | @@ -2940,7 +2947,8 @@ const DeclGen = struct { |
| 2940 | 2947 | } |
| 2941 | 2948 | |
| 2942 | 2949 | fn genDecl(self: *DeclGen) !void { |
| 2943 | const mod = self.module; | |
| 2950 | const pt = self.pt; | |
| 2951 | const mod = pt.zcu; | |
| 2944 | 2952 | const ip = &mod.intern_pool; |
| 2945 | 2953 | const decl = mod.declPtr(self.decl_index); |
| 2946 | 2954 | const spv_decl_index = try self.object.resolveDecl(mod, self.decl_index); |
| ... | ... | @@ -2967,7 +2975,7 @@ const DeclGen = struct { |
| 2967 | 2975 | try self.args.ensureUnusedCapacity(self.gpa, fn_info.param_types.len); |
| 2968 | 2976 | for (fn_info.param_types.get(ip)) |param_ty_index| { |
| 2969 | 2977 | const param_ty = Type.fromInterned(param_ty_index); |
| 2970 | if (!param_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; | |
| 2978 | if (!param_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 2971 | 2979 | |
| 2972 | 2980 | const param_type_id = try self.resolveType(param_ty, .direct); |
| 2973 | 2981 | const arg_result_id = self.spv.allocId(); |
| ... | ... | @@ -3004,11 +3012,11 @@ const DeclGen = struct { |
| 3004 | 3012 | // Append the actual code into the functions section. |
| 3005 | 3013 | try self.spv.addFunction(spv_decl_index, self.func); |
| 3006 | 3014 | |
| 3007 | const fqn = try decl.fullyQualifiedName(self.module); | |
| 3015 | const fqn = try decl.fullyQualifiedName(self.pt.zcu); | |
| 3008 | 3016 | try self.spv.debugName(result_id, fqn.toSlice(ip)); |
| 3009 | 3017 | |
| 3010 | 3018 | // Temporarily generate a test kernel declaration if this is a test function. |
| 3011 | if (self.module.test_functions.contains(self.decl_index)) { | |
| 3019 | if (self.pt.zcu.test_functions.contains(self.decl_index)) { | |
| 3012 | 3020 | try self.generateTestEntryPoint(fqn.toSlice(ip), spv_decl_index); |
| 3013 | 3021 | } |
| 3014 | 3022 | }, |
| ... | ... | @@ -3033,7 +3041,7 @@ const DeclGen = struct { |
| 3033 | 3041 | .storage_class = final_storage_class, |
| 3034 | 3042 | }); |
| 3035 | 3043 | |
| 3036 | const fqn = try decl.fullyQualifiedName(self.module); | |
| 3044 | const fqn = try decl.fullyQualifiedName(self.pt.zcu); | |
| 3037 | 3045 | try self.spv.debugName(result_id, fqn.toSlice(ip)); |
| 3038 | 3046 | try self.spv.declareDeclDeps(spv_decl_index, &.{}); |
| 3039 | 3047 | }, |
| ... | ... | @@ -3078,7 +3086,7 @@ const DeclGen = struct { |
| 3078 | 3086 | try self.func.body.emit(self.spv.gpa, .OpFunctionEnd, {}); |
| 3079 | 3087 | try self.spv.addFunction(spv_decl_index, self.func); |
| 3080 | 3088 | |
| 3081 | const fqn = try decl.fullyQualifiedName(self.module); | |
| 3089 | const fqn = try decl.fullyQualifiedName(self.pt.zcu); | |
| 3082 | 3090 | try self.spv.debugNameFmt(initializer_id, "initializer of {}", .{fqn.fmt(ip)}); |
| 3083 | 3091 | |
| 3084 | 3092 | try self.spv.sections.types_globals_constants.emit(self.spv.gpa, .OpExtInst, .{ |
| ... | ... | @@ -3119,7 +3127,7 @@ const DeclGen = struct { |
| 3119 | 3127 | /// Convert representation from indirect (in memory) to direct (in 'register') |
| 3120 | 3128 | /// This converts the argument type from resolveType(ty, .indirect) to resolveType(ty, .direct). |
| 3121 | 3129 | fn convertToDirect(self: *DeclGen, ty: Type, operand_id: IdRef) !IdRef { |
| 3122 | const mod = self.module; | |
| 3130 | const mod = self.pt.zcu; | |
| 3123 | 3131 | switch (ty.scalarType(mod).zigTypeTag(mod)) { |
| 3124 | 3132 | .Bool => { |
| 3125 | 3133 | const false_id = try self.constBool(false, .indirect); |
| ... | ... | @@ -3145,7 +3153,7 @@ const DeclGen = struct { |
| 3145 | 3153 | /// Convert representation from direct (in 'register) to direct (in memory) |
| 3146 | 3154 | /// This converts the argument type from resolveType(ty, .direct) to resolveType(ty, .indirect). |
| 3147 | 3155 | fn convertToIndirect(self: *DeclGen, ty: Type, operand_id: IdRef) !IdRef { |
| 3148 | const mod = self.module; | |
| 3156 | const mod = self.pt.zcu; | |
| 3149 | 3157 | switch (ty.scalarType(mod).zigTypeTag(mod)) { |
| 3150 | 3158 | .Bool => { |
| 3151 | 3159 | const result = try self.intFromBool(Temporary.init(ty, operand_id)); |
| ... | ... | @@ -3222,7 +3230,7 @@ const DeclGen = struct { |
| 3222 | 3230 | } |
| 3223 | 3231 | |
| 3224 | 3232 | fn genInst(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 3225 | const mod = self.module; | |
| 3233 | const mod = self.pt.zcu; | |
| 3226 | 3234 | const ip = &mod.intern_pool; |
| 3227 | 3235 | if (self.liveness.isUnused(inst) and !self.air.mustLower(inst, ip)) |
| 3228 | 3236 | return; |
| ... | ... | @@ -3402,7 +3410,7 @@ const DeclGen = struct { |
| 3402 | 3410 | } |
| 3403 | 3411 | |
| 3404 | 3412 | fn airShift(self: *DeclGen, inst: Air.Inst.Index, unsigned: BinaryOp, signed: BinaryOp) !?IdRef { |
| 3405 | const mod = self.module; | |
| 3413 | const mod = self.pt.zcu; | |
| 3406 | 3414 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 3407 | 3415 | |
| 3408 | 3416 | const base = try self.temporary(bin_op.lhs); |
| ... | ... | @@ -3480,7 +3488,7 @@ const DeclGen = struct { |
| 3480 | 3488 | /// All other values are returned unmodified (this makes strange integer |
| 3481 | 3489 | /// wrapping easier to use in generic operations). |
| 3482 | 3490 | fn normalize(self: *DeclGen, value: Temporary, info: ArithmeticTypeInfo) !Temporary { |
| 3483 | const mod = self.module; | |
| 3491 | const mod = self.pt.zcu; | |
| 3484 | 3492 | const ty = value.ty; |
| 3485 | 3493 | switch (info.class) { |
| 3486 | 3494 | .integer, .bool, .float => return value, |
| ... | ... | @@ -3721,7 +3729,7 @@ const DeclGen = struct { |
| 3721 | 3729 | |
| 3722 | 3730 | fn airMulOverflow(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 3723 | 3731 | const target = self.getTarget(); |
| 3724 | const mod = self.module; | |
| 3732 | const pt = self.pt; | |
| 3725 | 3733 | |
| 3726 | 3734 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3727 | 3735 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| ... | ... | @@ -3758,7 +3766,7 @@ const DeclGen = struct { |
| 3758 | 3766 | const result, const overflowed = switch (info.signedness) { |
| 3759 | 3767 | .unsigned => blk: { |
| 3760 | 3768 | if (maybe_op_ty_bits) |op_ty_bits| { |
| 3761 | const op_ty = try mod.intType(.unsigned, op_ty_bits); | |
| 3769 | const op_ty = try pt.intType(.unsigned, op_ty_bits); | |
| 3762 | 3770 | const casted_lhs = try self.buildIntConvert(op_ty, lhs); |
| 3763 | 3771 | const casted_rhs = try self.buildIntConvert(op_ty, rhs); |
| 3764 | 3772 | |
| ... | ... | @@ -3828,7 +3836,7 @@ const DeclGen = struct { |
| 3828 | 3836 | ); |
| 3829 | 3837 | |
| 3830 | 3838 | if (maybe_op_ty_bits) |op_ty_bits| { |
| 3831 | const op_ty = try mod.intType(.signed, op_ty_bits); | |
| 3839 | const op_ty = try pt.intType(.signed, op_ty_bits); | |
| 3832 | 3840 | // Assume normalized; sign bit is set. We want a sign extend. |
| 3833 | 3841 | const casted_lhs = try self.buildIntConvert(op_ty, lhs); |
| 3834 | 3842 | const casted_rhs = try self.buildIntConvert(op_ty, rhs); |
| ... | ... | @@ -3900,7 +3908,7 @@ const DeclGen = struct { |
| 3900 | 3908 | } |
| 3901 | 3909 | |
| 3902 | 3910 | fn airShlOverflow(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 3903 | const mod = self.module; | |
| 3911 | const mod = self.pt.zcu; | |
| 3904 | 3912 | |
| 3905 | 3913 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 3906 | 3914 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| ... | ... | @@ -3958,7 +3966,7 @@ const DeclGen = struct { |
| 3958 | 3966 | fn airClzCtz(self: *DeclGen, inst: Air.Inst.Index, op: UnaryOp) !?IdRef { |
| 3959 | 3967 | if (self.liveness.isUnused(inst)) return null; |
| 3960 | 3968 | |
| 3961 | const mod = self.module; | |
| 3969 | const mod = self.pt.zcu; | |
| 3962 | 3970 | const target = self.getTarget(); |
| 3963 | 3971 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 3964 | 3972 | const operand = try self.temporary(ty_op.operand); |
| ... | ... | @@ -4007,7 +4015,7 @@ const DeclGen = struct { |
| 4007 | 4015 | } |
| 4008 | 4016 | |
| 4009 | 4017 | fn airReduce(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 4010 | const mod = self.module; | |
| 4018 | const mod = self.pt.zcu; | |
| 4011 | 4019 | const reduce = self.air.instructions.items(.data)[@intFromEnum(inst)].reduce; |
| 4012 | 4020 | const operand = try self.resolve(reduce.operand); |
| 4013 | 4021 | const operand_ty = self.typeOf(reduce.operand); |
| ... | ... | @@ -4082,7 +4090,8 @@ const DeclGen = struct { |
| 4082 | 4090 | } |
| 4083 | 4091 | |
| 4084 | 4092 | fn airShuffle(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 4085 | const mod = self.module; | |
| 4093 | const pt = self.pt; | |
| 4094 | const mod = pt.zcu; | |
| 4086 | 4095 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4087 | 4096 | const extra = self.air.extraData(Air.Shuffle, ty_pl.payload).data; |
| 4088 | 4097 | const a = try self.resolve(extra.a); |
| ... | ... | @@ -4108,14 +4117,14 @@ const DeclGen = struct { |
| 4108 | 4117 | const a_len = a_ty.vectorLen(mod); |
| 4109 | 4118 | |
| 4110 | 4119 | for (components, 0..) |*component, i| { |
| 4111 | const elem = try mask.elemValue(mod, i); | |
| 4120 | const elem = try mask.elemValue(pt, i); | |
| 4112 | 4121 | if (elem.isUndef(mod)) { |
| 4113 | 4122 | // This is explicitly valid for OpVectorShuffle, it indicates undefined. |
| 4114 | 4123 | component.* = 0xFFFF_FFFF; |
| 4115 | 4124 | continue; |
| 4116 | 4125 | } |
| 4117 | 4126 | |
| 4118 | const index = elem.toSignedInt(mod); | |
| 4127 | const index = elem.toSignedInt(pt); | |
| 4119 | 4128 | if (index >= 0) { |
| 4120 | 4129 | component.* = @intCast(index); |
| 4121 | 4130 | } else { |
| ... | ... | @@ -4140,13 +4149,13 @@ const DeclGen = struct { |
| 4140 | 4149 | defer self.gpa.free(components); |
| 4141 | 4150 | |
| 4142 | 4151 | for (components, 0..) |*id, i| { |
| 4143 | const elem = try mask.elemValue(mod, i); | |
| 4152 | const elem = try mask.elemValue(pt, i); | |
| 4144 | 4153 | if (elem.isUndef(mod)) { |
| 4145 | 4154 | id.* = try self.spv.constUndef(scalar_ty_id); |
| 4146 | 4155 | continue; |
| 4147 | 4156 | } |
| 4148 | 4157 | |
| 4149 | const index = elem.toSignedInt(mod); | |
| 4158 | const index = elem.toSignedInt(pt); | |
| 4150 | 4159 | if (index >= 0) { |
| 4151 | 4160 | id.* = try self.extractVectorComponent(scalar_ty, a, @intCast(index)); |
| 4152 | 4161 | } else { |
| ... | ... | @@ -4220,7 +4229,7 @@ const DeclGen = struct { |
| 4220 | 4229 | } |
| 4221 | 4230 | |
| 4222 | 4231 | fn ptrAdd(self: *DeclGen, result_ty: Type, ptr_ty: Type, ptr_id: IdRef, offset_id: IdRef) !IdRef { |
| 4223 | const mod = self.module; | |
| 4232 | const mod = self.pt.zcu; | |
| 4224 | 4233 | const result_ty_id = try self.resolveType(result_ty, .direct); |
| 4225 | 4234 | |
| 4226 | 4235 | switch (ptr_ty.ptrSize(mod)) { |
| ... | ... | @@ -4276,7 +4285,8 @@ const DeclGen = struct { |
| 4276 | 4285 | lhs: Temporary, |
| 4277 | 4286 | rhs: Temporary, |
| 4278 | 4287 | ) !Temporary { |
| 4279 | const mod = self.module; | |
| 4288 | const pt = self.pt; | |
| 4289 | const mod = pt.zcu; | |
| 4280 | 4290 | const scalar_ty = lhs.ty.scalarType(mod); |
| 4281 | 4291 | const is_vector = lhs.ty.isVector(mod); |
| 4282 | 4292 | |
| ... | ... | @@ -4324,7 +4334,7 @@ const DeclGen = struct { |
| 4324 | 4334 | |
| 4325 | 4335 | const payload_ty = ty.optionalChild(mod); |
| 4326 | 4336 | if (ty.optionalReprIsPayload(mod)) { |
| 4327 | assert(payload_ty.hasRuntimeBitsIgnoreComptime(mod)); | |
| 4337 | assert(payload_ty.hasRuntimeBitsIgnoreComptime(pt)); | |
| 4328 | 4338 | assert(!payload_ty.isSlice(mod)); |
| 4329 | 4339 | |
| 4330 | 4340 | return try self.cmp(op, lhs.pun(payload_ty), rhs.pun(payload_ty)); |
| ... | ... | @@ -4333,12 +4343,12 @@ const DeclGen = struct { |
| 4333 | 4343 | const lhs_id = try lhs.materialize(self); |
| 4334 | 4344 | const rhs_id = try rhs.materialize(self); |
| 4335 | 4345 | |
| 4336 | const lhs_valid_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) | |
| 4346 | const lhs_valid_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(pt)) | |
| 4337 | 4347 | try self.extractField(Type.bool, lhs_id, 1) |
| 4338 | 4348 | else |
| 4339 | 4349 | try self.convertToDirect(Type.bool, lhs_id); |
| 4340 | 4350 | |
| 4341 | const rhs_valid_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) | |
| 4351 | const rhs_valid_id = if (payload_ty.hasRuntimeBitsIgnoreComptime(pt)) | |
| 4342 | 4352 | try self.extractField(Type.bool, rhs_id, 1) |
| 4343 | 4353 | else |
| 4344 | 4354 | try self.convertToDirect(Type.bool, rhs_id); |
| ... | ... | @@ -4346,7 +4356,7 @@ const DeclGen = struct { |
| 4346 | 4356 | const lhs_valid = Temporary.init(Type.bool, lhs_valid_id); |
| 4347 | 4357 | const rhs_valid = Temporary.init(Type.bool, rhs_valid_id); |
| 4348 | 4358 | |
| 4349 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4359 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4350 | 4360 | return try self.cmp(op, lhs_valid, rhs_valid); |
| 4351 | 4361 | } |
| 4352 | 4362 | |
| ... | ... | @@ -4466,7 +4476,7 @@ const DeclGen = struct { |
| 4466 | 4476 | src_ty: Type, |
| 4467 | 4477 | src_id: IdRef, |
| 4468 | 4478 | ) !IdRef { |
| 4469 | const mod = self.module; | |
| 4479 | const mod = self.pt.zcu; | |
| 4470 | 4480 | const src_ty_id = try self.resolveType(src_ty, .direct); |
| 4471 | 4481 | const dst_ty_id = try self.resolveType(dst_ty, .direct); |
| 4472 | 4482 | |
| ... | ... | @@ -4675,7 +4685,8 @@ const DeclGen = struct { |
| 4675 | 4685 | } |
| 4676 | 4686 | |
| 4677 | 4687 | fn airArrayToSlice(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 4678 | const mod = self.module; | |
| 4688 | const pt = self.pt; | |
| 4689 | const mod = pt.zcu; | |
| 4679 | 4690 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 4680 | 4691 | const array_ptr_ty = self.typeOf(ty_op.operand); |
| 4681 | 4692 | const array_ty = array_ptr_ty.childType(mod); |
| ... | ... | @@ -4687,7 +4698,7 @@ const DeclGen = struct { |
| 4687 | 4698 | const array_ptr_id = try self.resolve(ty_op.operand); |
| 4688 | 4699 | const len_id = try self.constInt(Type.usize, array_ty.arrayLen(mod), .direct); |
| 4689 | 4700 | |
| 4690 | const elem_ptr_id = if (!array_ty.hasRuntimeBitsIgnoreComptime(mod)) | |
| 4701 | const elem_ptr_id = if (!array_ty.hasRuntimeBitsIgnoreComptime(pt)) | |
| 4691 | 4702 | // Note: The pointer is something like *opaque{}, so we need to bitcast it to the element type. |
| 4692 | 4703 | try self.bitCast(elem_ptr_ty, array_ptr_ty, array_ptr_id) |
| 4693 | 4704 | else |
| ... | ... | @@ -4719,7 +4730,8 @@ const DeclGen = struct { |
| 4719 | 4730 | } |
| 4720 | 4731 | |
| 4721 | 4732 | fn airAggregateInit(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 4722 | const mod = self.module; | |
| 4733 | const pt = self.pt; | |
| 4734 | const mod = pt.zcu; | |
| 4723 | 4735 | const ip = &mod.intern_pool; |
| 4724 | 4736 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4725 | 4737 | const result_ty = self.typeOfIndex(inst); |
| ... | ... | @@ -4742,8 +4754,8 @@ const DeclGen = struct { |
| 4742 | 4754 | switch (ip.indexToKey(result_ty.toIntern())) { |
| 4743 | 4755 | .anon_struct_type => |tuple| { |
| 4744 | 4756 | for (tuple.types.get(ip), elements, 0..) |field_ty, element, i| { |
| 4745 | if ((try result_ty.structFieldValueComptime(mod, i)) != null) continue; | |
| 4746 | assert(Type.fromInterned(field_ty).hasRuntimeBits(mod)); | |
| 4757 | if ((try result_ty.structFieldValueComptime(pt, i)) != null) continue; | |
| 4758 | assert(Type.fromInterned(field_ty).hasRuntimeBits(pt)); | |
| 4747 | 4759 | |
| 4748 | 4760 | const id = try self.resolve(element); |
| 4749 | 4761 | types[index] = Type.fromInterned(field_ty); |
| ... | ... | @@ -4756,9 +4768,9 @@ const DeclGen = struct { |
| 4756 | 4768 | var it = struct_type.iterateRuntimeOrder(ip); |
| 4757 | 4769 | for (elements, 0..) |element, i| { |
| 4758 | 4770 | const field_index = it.next().?; |
| 4759 | if ((try result_ty.structFieldValueComptime(mod, i)) != null) continue; | |
| 4771 | if ((try result_ty.structFieldValueComptime(pt, i)) != null) continue; | |
| 4760 | 4772 | const field_ty = Type.fromInterned(struct_type.field_types.get(ip)[field_index]); |
| 4761 | assert(field_ty.hasRuntimeBitsIgnoreComptime(mod)); | |
| 4773 | assert(field_ty.hasRuntimeBitsIgnoreComptime(pt)); | |
| 4762 | 4774 | |
| 4763 | 4775 | const id = try self.resolve(element); |
| 4764 | 4776 | types[index] = field_ty; |
| ... | ... | @@ -4808,13 +4820,14 @@ const DeclGen = struct { |
| 4808 | 4820 | } |
| 4809 | 4821 | |
| 4810 | 4822 | fn sliceOrArrayLen(self: *DeclGen, operand_id: IdRef, ty: Type) !IdRef { |
| 4811 | const mod = self.module; | |
| 4823 | const pt = self.pt; | |
| 4824 | const mod = pt.zcu; | |
| 4812 | 4825 | switch (ty.ptrSize(mod)) { |
| 4813 | 4826 | .Slice => return self.extractField(Type.usize, operand_id, 1), |
| 4814 | 4827 | .One => { |
| 4815 | 4828 | const array_ty = ty.childType(mod); |
| 4816 | 4829 | const elem_ty = array_ty.childType(mod); |
| 4817 | const abi_size = elem_ty.abiSize(mod); | |
| 4830 | const abi_size = elem_ty.abiSize(pt); | |
| 4818 | 4831 | const size = array_ty.arrayLenIncludingSentinel(mod) * abi_size; |
| 4819 | 4832 | return try self.constInt(Type.usize, size, .direct); |
| 4820 | 4833 | }, |
| ... | ... | @@ -4823,7 +4836,7 @@ const DeclGen = struct { |
| 4823 | 4836 | } |
| 4824 | 4837 | |
| 4825 | 4838 | fn sliceOrArrayPtr(self: *DeclGen, operand_id: IdRef, ty: Type) !IdRef { |
| 4826 | const mod = self.module; | |
| 4839 | const mod = self.pt.zcu; | |
| 4827 | 4840 | if (ty.isSlice(mod)) { |
| 4828 | 4841 | const ptr_ty = ty.slicePtrFieldType(mod); |
| 4829 | 4842 | return self.extractField(ptr_ty, operand_id, 0); |
| ... | ... | @@ -4855,7 +4868,7 @@ const DeclGen = struct { |
| 4855 | 4868 | } |
| 4856 | 4869 | |
| 4857 | 4870 | fn airSliceElemPtr(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 4858 | const mod = self.module; | |
| 4871 | const mod = self.pt.zcu; | |
| 4859 | 4872 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4860 | 4873 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4861 | 4874 | const slice_ty = self.typeOf(bin_op.lhs); |
| ... | ... | @@ -4872,7 +4885,7 @@ const DeclGen = struct { |
| 4872 | 4885 | } |
| 4873 | 4886 | |
| 4874 | 4887 | fn airSliceElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 4875 | const mod = self.module; | |
| 4888 | const mod = self.pt.zcu; | |
| 4876 | 4889 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4877 | 4890 | const slice_ty = self.typeOf(bin_op.lhs); |
| 4878 | 4891 | if (!slice_ty.isVolatilePtr(mod) and self.liveness.isUnused(inst)) return null; |
| ... | ... | @@ -4889,7 +4902,7 @@ const DeclGen = struct { |
| 4889 | 4902 | } |
| 4890 | 4903 | |
| 4891 | 4904 | fn ptrElemPtr(self: *DeclGen, ptr_ty: Type, ptr_id: IdRef, index_id: IdRef) !IdRef { |
| 4892 | const mod = self.module; | |
| 4905 | const mod = self.pt.zcu; | |
| 4893 | 4906 | // Construct new pointer type for the resulting pointer |
| 4894 | 4907 | const elem_ty = ptr_ty.elemType2(mod); // use elemType() so that we get T for *[N]T. |
| 4895 | 4908 | const elem_ptr_ty_id = try self.ptrType(elem_ty, self.spvStorageClass(ptr_ty.ptrAddressSpace(mod))); |
| ... | ... | @@ -4904,14 +4917,15 @@ const DeclGen = struct { |
| 4904 | 4917 | } |
| 4905 | 4918 | |
| 4906 | 4919 | fn airPtrElemPtr(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 4907 | const mod = self.module; | |
| 4920 | const pt = self.pt; | |
| 4921 | const mod = pt.zcu; | |
| 4908 | 4922 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 4909 | 4923 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4910 | 4924 | const src_ptr_ty = self.typeOf(bin_op.lhs); |
| 4911 | 4925 | const elem_ty = src_ptr_ty.childType(mod); |
| 4912 | 4926 | const ptr_id = try self.resolve(bin_op.lhs); |
| 4913 | 4927 | |
| 4914 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 4928 | if (!elem_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 4915 | 4929 | const dst_ptr_ty = self.typeOfIndex(inst); |
| 4916 | 4930 | return try self.bitCast(dst_ptr_ty, src_ptr_ty, ptr_id); |
| 4917 | 4931 | } |
| ... | ... | @@ -4921,7 +4935,7 @@ const DeclGen = struct { |
| 4921 | 4935 | } |
| 4922 | 4936 | |
| 4923 | 4937 | fn airArrayElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 4924 | const mod = self.module; | |
| 4938 | const mod = self.pt.zcu; | |
| 4925 | 4939 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4926 | 4940 | const array_ty = self.typeOf(bin_op.lhs); |
| 4927 | 4941 | const elem_ty = array_ty.childType(mod); |
| ... | ... | @@ -4982,7 +4996,7 @@ const DeclGen = struct { |
| 4982 | 4996 | } |
| 4983 | 4997 | |
| 4984 | 4998 | fn airPtrElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 4985 | const mod = self.module; | |
| 4999 | const mod = self.pt.zcu; | |
| 4986 | 5000 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 4987 | 5001 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 4988 | 5002 | const elem_ty = self.typeOfIndex(inst); |
| ... | ... | @@ -4993,7 +5007,7 @@ const DeclGen = struct { |
| 4993 | 5007 | } |
| 4994 | 5008 | |
| 4995 | 5009 | fn airVectorStoreElem(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 4996 | const mod = self.module; | |
| 5010 | const mod = self.pt.zcu; | |
| 4997 | 5011 | const data = self.air.instructions.items(.data)[@intFromEnum(inst)].vector_store_elem; |
| 4998 | 5012 | const extra = self.air.extraData(Air.Bin, data.payload).data; |
| 4999 | 5013 | |
| ... | ... | @@ -5015,7 +5029,7 @@ const DeclGen = struct { |
| 5015 | 5029 | } |
| 5016 | 5030 | |
| 5017 | 5031 | fn airSetUnionTag(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 5018 | const mod = self.module; | |
| 5032 | const mod = self.pt.zcu; | |
| 5019 | 5033 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 5020 | 5034 | const un_ptr_ty = self.typeOf(bin_op.lhs); |
| 5021 | 5035 | const un_ty = un_ptr_ty.childType(mod); |
| ... | ... | @@ -5041,7 +5055,7 @@ const DeclGen = struct { |
| 5041 | 5055 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5042 | 5056 | const un_ty = self.typeOf(ty_op.operand); |
| 5043 | 5057 | |
| 5044 | const mod = self.module; | |
| 5058 | const mod = self.pt.zcu; | |
| 5045 | 5059 | const layout = self.unionLayout(un_ty); |
| 5046 | 5060 | if (layout.tag_size == 0) return null; |
| 5047 | 5061 | |
| ... | ... | @@ -5064,7 +5078,8 @@ const DeclGen = struct { |
| 5064 | 5078 | |
| 5065 | 5079 | // Note: The result here is not cached, because it generates runtime code. |
| 5066 | 5080 | |
| 5067 | const mod = self.module; | |
| 5081 | const pt = self.pt; | |
| 5082 | const mod = pt.zcu; | |
| 5068 | 5083 | const ip = &mod.intern_pool; |
| 5069 | 5084 | const union_ty = mod.typeToUnion(ty).?; |
| 5070 | 5085 | const tag_ty = Type.fromInterned(union_ty.enum_tag_ty); |
| ... | ... | @@ -5076,9 +5091,9 @@ const DeclGen = struct { |
| 5076 | 5091 | const layout = self.unionLayout(ty); |
| 5077 | 5092 | |
| 5078 | 5093 | const tag_int = if (layout.tag_size != 0) blk: { |
| 5079 | const tag_val = try mod.enumValueFieldIndex(tag_ty, active_field); | |
| 5080 | const tag_int_val = try tag_val.intFromEnum(tag_ty, mod); | |
| 5081 | break :blk tag_int_val.toUnsignedInt(mod); | |
| 5094 | const tag_val = try pt.enumValueFieldIndex(tag_ty, active_field); | |
| 5095 | const tag_int_val = try tag_val.intFromEnum(tag_ty, pt); | |
| 5096 | break :blk tag_int_val.toUnsignedInt(pt); | |
| 5082 | 5097 | } else 0; |
| 5083 | 5098 | |
| 5084 | 5099 | if (!layout.has_payload) { |
| ... | ... | @@ -5095,7 +5110,7 @@ const DeclGen = struct { |
| 5095 | 5110 | } |
| 5096 | 5111 | |
| 5097 | 5112 | const payload_ty = Type.fromInterned(union_ty.field_types.get(ip)[active_field]); |
| 5098 | if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 5113 | if (payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 5099 | 5114 | const pl_ptr_ty_id = try self.ptrType(layout.payload_ty, .Function); |
| 5100 | 5115 | const pl_ptr_id = try self.accessChain(pl_ptr_ty_id, tmp_id, &.{layout.payload_index}); |
| 5101 | 5116 | const active_pl_ptr_ty_id = try self.ptrType(payload_ty, .Function); |
| ... | ... | @@ -5118,7 +5133,8 @@ const DeclGen = struct { |
| 5118 | 5133 | } |
| 5119 | 5134 | |
| 5120 | 5135 | fn airUnionInit(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 5121 | const mod = self.module; | |
| 5136 | const pt = self.pt; | |
| 5137 | const mod = pt.zcu; | |
| 5122 | 5138 | const ip = &mod.intern_pool; |
| 5123 | 5139 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5124 | 5140 | const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| ... | ... | @@ -5126,7 +5142,7 @@ const DeclGen = struct { |
| 5126 | 5142 | |
| 5127 | 5143 | const union_obj = mod.typeToUnion(ty).?; |
| 5128 | 5144 | const field_ty = Type.fromInterned(union_obj.field_types.get(ip)[extra.field_index]); |
| 5129 | const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(mod)) | |
| 5145 | const payload = if (field_ty.hasRuntimeBitsIgnoreComptime(pt)) | |
| 5130 | 5146 | try self.resolve(extra.init) |
| 5131 | 5147 | else |
| 5132 | 5148 | null; |
| ... | ... | @@ -5134,7 +5150,8 @@ const DeclGen = struct { |
| 5134 | 5150 | } |
| 5135 | 5151 | |
| 5136 | 5152 | fn airStructFieldVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 5137 | const mod = self.module; | |
| 5153 | const pt = self.pt; | |
| 5154 | const mod = pt.zcu; | |
| 5138 | 5155 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5139 | 5156 | const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data; |
| 5140 | 5157 | |
| ... | ... | @@ -5143,7 +5160,7 @@ const DeclGen = struct { |
| 5143 | 5160 | const field_index = struct_field.field_index; |
| 5144 | 5161 | const field_ty = object_ty.structFieldType(field_index, mod); |
| 5145 | 5162 | |
| 5146 | if (!field_ty.hasRuntimeBitsIgnoreComptime(mod)) return null; | |
| 5163 | if (!field_ty.hasRuntimeBitsIgnoreComptime(pt)) return null; | |
| 5147 | 5164 | |
| 5148 | 5165 | switch (object_ty.zigTypeTag(mod)) { |
| 5149 | 5166 | .Struct => switch (object_ty.containerLayout(mod)) { |
| ... | ... | @@ -5178,7 +5195,8 @@ const DeclGen = struct { |
| 5178 | 5195 | } |
| 5179 | 5196 | |
| 5180 | 5197 | fn airFieldParentPtr(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 5181 | const mod = self.module; | |
| 5198 | const pt = self.pt; | |
| 5199 | const mod = pt.zcu; | |
| 5182 | 5200 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 5183 | 5201 | const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| 5184 | 5202 | |
| ... | ... | @@ -5187,7 +5205,7 @@ const DeclGen = struct { |
| 5187 | 5205 | |
| 5188 | 5206 | const field_ptr = try self.resolve(extra.field_ptr); |
| 5189 | 5207 | const field_ptr_int = try self.intFromPtr(field_ptr); |
| 5190 | const field_offset = parent_ty.structFieldOffset(extra.field_index, mod); | |
| 5208 | const field_offset = parent_ty.structFieldOffset(extra.field_index, pt); | |
| 5191 | 5209 | |
| 5192 | 5210 | const base_ptr_int = base_ptr_int: { |
| 5193 | 5211 | if (field_offset == 0) break :base_ptr_int field_ptr_int; |
| ... | ... | @@ -5218,7 +5236,7 @@ const DeclGen = struct { |
| 5218 | 5236 | ) !IdRef { |
| 5219 | 5237 | const result_ty_id = try self.resolveType(result_ptr_ty, .direct); |
| 5220 | 5238 | |
| 5221 | const zcu = self.module; | |
| 5239 | const zcu = self.pt.zcu; | |
| 5222 | 5240 | const object_ty = object_ptr_ty.childType(zcu); |
| 5223 | 5241 | switch (object_ty.zigTypeTag(zcu)) { |
| 5224 | 5242 | .Pointer => { |
| ... | ... | @@ -5312,7 +5330,7 @@ const DeclGen = struct { |
| 5312 | 5330 | } |
| 5313 | 5331 | |
| 5314 | 5332 | fn airAlloc(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 5315 | const mod = self.module; | |
| 5333 | const mod = self.pt.zcu; | |
| 5316 | 5334 | const ptr_ty = self.typeOfIndex(inst); |
| 5317 | 5335 | assert(ptr_ty.ptrAddressSpace(mod) == .generic); |
| 5318 | 5336 | const child_ty = ptr_ty.childType(mod); |
| ... | ... | @@ -5486,9 +5504,10 @@ const DeclGen = struct { |
| 5486 | 5504 | // of the block, then a label, and then generate the rest of the current |
| 5487 | 5505 | // ir.Block in a different SPIR-V block. |
| 5488 | 5506 | |
| 5489 | const mod = self.module; | |
| 5507 | const pt = self.pt; | |
| 5508 | const mod = pt.zcu; | |
| 5490 | 5509 | const ty = self.typeOfIndex(inst); |
| 5491 | const have_block_result = ty.isFnOrHasRuntimeBitsIgnoreComptime(mod); | |
| 5510 | const have_block_result = ty.isFnOrHasRuntimeBitsIgnoreComptime(pt); | |
| 5492 | 5511 | |
| 5493 | 5512 | const cf = switch (self.control_flow) { |
| 5494 | 5513 | .structured => |*cf| cf, |
| ... | ... | @@ -5618,13 +5637,13 @@ const DeclGen = struct { |
| 5618 | 5637 | } |
| 5619 | 5638 | |
| 5620 | 5639 | fn airBr(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 5621 | const mod = self.module; | |
| 5640 | const pt = self.pt; | |
| 5622 | 5641 | const br = self.air.instructions.items(.data)[@intFromEnum(inst)].br; |
| 5623 | 5642 | const operand_ty = self.typeOf(br.operand); |
| 5624 | 5643 | |
| 5625 | 5644 | switch (self.control_flow) { |
| 5626 | 5645 | .structured => |*cf| { |
| 5627 | if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { | |
| 5646 | if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(pt)) { | |
| 5628 | 5647 | const operand_id = try self.resolve(br.operand); |
| 5629 | 5648 | const block_result_var_id = cf.block_results.get(br.block_inst).?; |
| 5630 | 5649 | try self.store(operand_ty, block_result_var_id, operand_id, .{}); |
| ... | ... | @@ -5635,7 +5654,7 @@ const DeclGen = struct { |
| 5635 | 5654 | }, |
| 5636 | 5655 | .unstructured => |cf| { |
| 5637 | 5656 | const block = cf.blocks.get(br.block_inst).?; |
| 5638 | if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(mod)) { | |
| 5657 | if (operand_ty.isFnOrHasRuntimeBitsIgnoreComptime(pt)) { | |
| 5639 | 5658 | const operand_id = try self.resolve(br.operand); |
| 5640 | 5659 | // current_block_label should not be undefined here, lest there |
| 5641 | 5660 | // is a br or br_void in the function's body. |
| ... | ... | @@ -5762,7 +5781,7 @@ const DeclGen = struct { |
| 5762 | 5781 | } |
| 5763 | 5782 | |
| 5764 | 5783 | fn airLoad(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 5765 | const mod = self.module; | |
| 5784 | const mod = self.pt.zcu; | |
| 5766 | 5785 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5767 | 5786 | const ptr_ty = self.typeOf(ty_op.operand); |
| 5768 | 5787 | const elem_ty = self.typeOfIndex(inst); |
| ... | ... | @@ -5773,20 +5792,22 @@ const DeclGen = struct { |
| 5773 | 5792 | } |
| 5774 | 5793 | |
| 5775 | 5794 | fn airStore(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 5795 | const mod = self.pt.zcu; | |
| 5776 | 5796 | const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op; |
| 5777 | 5797 | const ptr_ty = self.typeOf(bin_op.lhs); |
| 5778 | const elem_ty = ptr_ty.childType(self.module); | |
| 5798 | const elem_ty = ptr_ty.childType(mod); | |
| 5779 | 5799 | const ptr = try self.resolve(bin_op.lhs); |
| 5780 | 5800 | const value = try self.resolve(bin_op.rhs); |
| 5781 | 5801 | |
| 5782 | try self.store(elem_ty, ptr, value, .{ .is_volatile = ptr_ty.isVolatilePtr(self.module) }); | |
| 5802 | try self.store(elem_ty, ptr, value, .{ .is_volatile = ptr_ty.isVolatilePtr(mod) }); | |
| 5783 | 5803 | } |
| 5784 | 5804 | |
| 5785 | 5805 | fn airRet(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 5806 | const pt = self.pt; | |
| 5807 | const mod = pt.zcu; | |
| 5786 | 5808 | const operand = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5787 | 5809 | const ret_ty = self.typeOf(operand); |
| 5788 | const mod = self.module; | |
| 5789 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 5810 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 5790 | 5811 | const decl = mod.declPtr(self.decl_index); |
| 5791 | 5812 | const fn_info = mod.typeToFunc(decl.typeOf(mod)).?; |
| 5792 | 5813 | if (Type.fromInterned(fn_info.return_type).isError(mod)) { |
| ... | ... | @@ -5805,12 +5826,13 @@ const DeclGen = struct { |
| 5805 | 5826 | } |
| 5806 | 5827 | |
| 5807 | 5828 | fn airRetLoad(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 5808 | const mod = self.module; | |
| 5829 | const pt = self.pt; | |
| 5830 | const mod = pt.zcu; | |
| 5809 | 5831 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5810 | 5832 | const ptr_ty = self.typeOf(un_op); |
| 5811 | 5833 | const ret_ty = ptr_ty.childType(mod); |
| 5812 | 5834 | |
| 5813 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 5835 | if (!ret_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 5814 | 5836 | const decl = mod.declPtr(self.decl_index); |
| 5815 | 5837 | const fn_info = mod.typeToFunc(decl.typeOf(mod)).?; |
| 5816 | 5838 | if (Type.fromInterned(fn_info.return_type).isError(mod)) { |
| ... | ... | @@ -5832,7 +5854,7 @@ const DeclGen = struct { |
| 5832 | 5854 | } |
| 5833 | 5855 | |
| 5834 | 5856 | fn airTry(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 5835 | const mod = self.module; | |
| 5857 | const mod = self.pt.zcu; | |
| 5836 | 5858 | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 5837 | 5859 | const err_union_id = try self.resolve(pl_op.operand); |
| 5838 | 5860 | const extra = self.air.extraData(Air.Try, pl_op.payload); |
| ... | ... | @@ -5902,7 +5924,7 @@ const DeclGen = struct { |
| 5902 | 5924 | } |
| 5903 | 5925 | |
| 5904 | 5926 | fn airErrUnionErr(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 5905 | const mod = self.module; | |
| 5927 | const mod = self.pt.zcu; | |
| 5906 | 5928 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5907 | 5929 | const operand_id = try self.resolve(ty_op.operand); |
| 5908 | 5930 | const err_union_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -5938,7 +5960,7 @@ const DeclGen = struct { |
| 5938 | 5960 | } |
| 5939 | 5961 | |
| 5940 | 5962 | fn airWrapErrUnionErr(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 5941 | const mod = self.module; | |
| 5963 | const mod = self.pt.zcu; | |
| 5942 | 5964 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 5943 | 5965 | const err_union_ty = self.typeOfIndex(inst); |
| 5944 | 5966 | const payload_ty = err_union_ty.errorUnionPayload(mod); |
| ... | ... | @@ -5985,7 +6007,8 @@ const DeclGen = struct { |
| 5985 | 6007 | } |
| 5986 | 6008 | |
| 5987 | 6009 | fn airIsNull(self: *DeclGen, inst: Air.Inst.Index, is_pointer: bool, pred: enum { is_null, is_non_null }) !?IdRef { |
| 5988 | const mod = self.module; | |
| 6010 | const pt = self.pt; | |
| 6011 | const mod = pt.zcu; | |
| 5989 | 6012 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 5990 | 6013 | const operand_id = try self.resolve(un_op); |
| 5991 | 6014 | const operand_ty = self.typeOf(un_op); |
| ... | ... | @@ -6026,7 +6049,7 @@ const DeclGen = struct { |
| 6026 | 6049 | |
| 6027 | 6050 | const is_non_null_id = blk: { |
| 6028 | 6051 | if (is_pointer) { |
| 6029 | if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 6052 | if (payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 6030 | 6053 | const storage_class = self.spvStorageClass(operand_ty.ptrAddressSpace(mod)); |
| 6031 | 6054 | const bool_ptr_ty_id = try self.ptrType(Type.bool, storage_class); |
| 6032 | 6055 | const tag_ptr_id = try self.accessChain(bool_ptr_ty_id, operand_id, &.{1}); |
| ... | ... | @@ -6036,7 +6059,7 @@ const DeclGen = struct { |
| 6036 | 6059 | break :blk try self.load(Type.bool, operand_id, .{}); |
| 6037 | 6060 | } |
| 6038 | 6061 | |
| 6039 | break :blk if (payload_ty.hasRuntimeBitsIgnoreComptime(mod)) | |
| 6062 | break :blk if (payload_ty.hasRuntimeBitsIgnoreComptime(pt)) | |
| 6040 | 6063 | try self.extractField(Type.bool, operand_id, 1) |
| 6041 | 6064 | else |
| 6042 | 6065 | // Optional representation is bool indicating whether the optional is set |
| ... | ... | @@ -6061,7 +6084,7 @@ const DeclGen = struct { |
| 6061 | 6084 | } |
| 6062 | 6085 | |
| 6063 | 6086 | fn airIsErr(self: *DeclGen, inst: Air.Inst.Index, pred: enum { is_err, is_non_err }) !?IdRef { |
| 6064 | const mod = self.module; | |
| 6087 | const mod = self.pt.zcu; | |
| 6065 | 6088 | const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op; |
| 6066 | 6089 | const operand_id = try self.resolve(un_op); |
| 6067 | 6090 | const err_union_ty = self.typeOf(un_op); |
| ... | ... | @@ -6094,13 +6117,14 @@ const DeclGen = struct { |
| 6094 | 6117 | } |
| 6095 | 6118 | |
| 6096 | 6119 | fn airUnwrapOptional(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 6097 | const mod = self.module; | |
| 6120 | const pt = self.pt; | |
| 6121 | const mod = pt.zcu; | |
| 6098 | 6122 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6099 | 6123 | const operand_id = try self.resolve(ty_op.operand); |
| 6100 | 6124 | const optional_ty = self.typeOf(ty_op.operand); |
| 6101 | 6125 | const payload_ty = self.typeOfIndex(inst); |
| 6102 | 6126 | |
| 6103 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) return null; | |
| 6127 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) return null; | |
| 6104 | 6128 | |
| 6105 | 6129 | if (optional_ty.optionalReprIsPayload(mod)) { |
| 6106 | 6130 | return operand_id; |
| ... | ... | @@ -6110,7 +6134,8 @@ const DeclGen = struct { |
| 6110 | 6134 | } |
| 6111 | 6135 | |
| 6112 | 6136 | fn airUnwrapOptionalPtr(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 6113 | const mod = self.module; | |
| 6137 | const pt = self.pt; | |
| 6138 | const mod = pt.zcu; | |
| 6114 | 6139 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6115 | 6140 | const operand_id = try self.resolve(ty_op.operand); |
| 6116 | 6141 | const operand_ty = self.typeOf(ty_op.operand); |
| ... | ... | @@ -6119,7 +6144,7 @@ const DeclGen = struct { |
| 6119 | 6144 | const result_ty = self.typeOfIndex(inst); |
| 6120 | 6145 | const result_ty_id = try self.resolveType(result_ty, .direct); |
| 6121 | 6146 | |
| 6122 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 6147 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 6123 | 6148 | // There is no payload, but we still need to return a valid pointer. |
| 6124 | 6149 | // We can just return anything here, so just return a pointer to the operand. |
| 6125 | 6150 | return try self.bitCast(result_ty, operand_ty, operand_id); |
| ... | ... | @@ -6134,11 +6159,12 @@ const DeclGen = struct { |
| 6134 | 6159 | } |
| 6135 | 6160 | |
| 6136 | 6161 | fn airWrapOptional(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 6137 | const mod = self.module; | |
| 6162 | const pt = self.pt; | |
| 6163 | const mod = pt.zcu; | |
| 6138 | 6164 | const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op; |
| 6139 | 6165 | const payload_ty = self.typeOf(ty_op.operand); |
| 6140 | 6166 | |
| 6141 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(mod)) { | |
| 6167 | if (!payload_ty.hasRuntimeBitsIgnoreComptime(pt)) { | |
| 6142 | 6168 | return try self.constBool(true, .indirect); |
| 6143 | 6169 | } |
| 6144 | 6170 | |
| ... | ... | @@ -6156,7 +6182,8 @@ const DeclGen = struct { |
| 6156 | 6182 | } |
| 6157 | 6183 | |
| 6158 | 6184 | fn airSwitchBr(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 6159 | const mod = self.module; | |
| 6185 | const pt = self.pt; | |
| 6186 | const mod = pt.zcu; | |
| 6160 | 6187 | const target = self.getTarget(); |
| 6161 | 6188 | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6162 | 6189 | const cond_ty = self.typeOf(pl_op.operand); |
| ... | ... | @@ -6240,15 +6267,15 @@ const DeclGen = struct { |
| 6240 | 6267 | const label = case_labels.at(case_i); |
| 6241 | 6268 | |
| 6242 | 6269 | for (items) |item| { |
| 6243 | const value = (try self.air.value(item, mod)) orelse unreachable; | |
| 6270 | const value = (try self.air.value(item, pt)) orelse unreachable; | |
| 6244 | 6271 | const int_val: u64 = switch (cond_ty.zigTypeTag(mod)) { |
| 6245 | .Bool, .Int => if (cond_ty.isSignedInt(mod)) @bitCast(value.toSignedInt(mod)) else value.toUnsignedInt(mod), | |
| 6272 | .Bool, .Int => if (cond_ty.isSignedInt(mod)) @bitCast(value.toSignedInt(pt)) else value.toUnsignedInt(pt), | |
| 6246 | 6273 | .Enum => blk: { |
| 6247 | 6274 | // TODO: figure out of cond_ty is correct (something with enum literals) |
| 6248 | break :blk (try value.intFromEnum(cond_ty, mod)).toUnsignedInt(mod); // TODO: composite integer constants | |
| 6275 | break :blk (try value.intFromEnum(cond_ty, pt)).toUnsignedInt(pt); // TODO: composite integer constants | |
| 6249 | 6276 | }, |
| 6250 | 6277 | .ErrorSet => value.getErrorInt(mod), |
| 6251 | .Pointer => value.toUnsignedInt(mod), | |
| 6278 | .Pointer => value.toUnsignedInt(pt), | |
| 6252 | 6279 | else => unreachable, |
| 6253 | 6280 | }; |
| 6254 | 6281 | const int_lit: spec.LiteralContextDependentNumber = switch (cond_words) { |
| ... | ... | @@ -6328,8 +6355,9 @@ const DeclGen = struct { |
| 6328 | 6355 | } |
| 6329 | 6356 | |
| 6330 | 6357 | fn airDbgStmt(self: *DeclGen, inst: Air.Inst.Index) !void { |
| 6358 | const pt = self.pt; | |
| 6359 | const mod = pt.zcu; | |
| 6331 | 6360 | const dbg_stmt = self.air.instructions.items(.data)[@intFromEnum(inst)].dbg_stmt; |
| 6332 | const mod = self.module; | |
| 6333 | 6361 | const decl = mod.declPtr(self.decl_index); |
| 6334 | 6362 | const path = decl.getFileScope(mod).sub_file_path; |
| 6335 | 6363 | try self.func.body.emit(self.spv.gpa, .OpLine, .{ |
| ... | ... | @@ -6340,7 +6368,7 @@ const DeclGen = struct { |
| 6340 | 6368 | } |
| 6341 | 6369 | |
| 6342 | 6370 | fn airDbgInlineBlock(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 6343 | const mod = self.module; | |
| 6371 | const mod = self.pt.zcu; | |
| 6344 | 6372 | const inst_datas = self.air.instructions.items(.data); |
| 6345 | 6373 | const extra = self.air.extraData(Air.DbgInlineBlock, inst_datas[@intFromEnum(inst)].ty_pl.payload); |
| 6346 | 6374 | const decl = mod.funcOwnerDeclPtr(extra.data.func); |
| ... | ... | @@ -6358,7 +6386,7 @@ const DeclGen = struct { |
| 6358 | 6386 | } |
| 6359 | 6387 | |
| 6360 | 6388 | fn airAssembly(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { |
| 6361 | const mod = self.module; | |
| 6389 | const mod = self.pt.zcu; | |
| 6362 | 6390 | const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 6363 | 6391 | const extra = self.air.extraData(Air.Asm, ty_pl.payload); |
| 6364 | 6392 | |
| ... | ... | @@ -6440,20 +6468,20 @@ const DeclGen = struct { |
| 6440 | 6468 | // TODO: Translate proper error locations. |
| 6441 | 6469 | assert(as.errors.items.len != 0); |
| 6442 | 6470 | assert(self.error_msg == null); |
| 6443 | const src_loc = self.module.declPtr(self.decl_index).navSrcLoc(mod); | |
| 6444 | self.error_msg = try Module.ErrorMsg.create(self.module.gpa, src_loc, "failed to assemble SPIR-V inline assembly", .{}); | |
| 6445 | const notes = try self.module.gpa.alloc(Module.ErrorMsg, as.errors.items.len); | |
| 6471 | const src_loc = mod.declPtr(self.decl_index).navSrcLoc(mod); | |
| 6472 | self.error_msg = try Zcu.ErrorMsg.create(mod.gpa, src_loc, "failed to assemble SPIR-V inline assembly", .{}); | |
| 6473 | const notes = try mod.gpa.alloc(Zcu.ErrorMsg, as.errors.items.len); | |
| 6446 | 6474 | |
| 6447 | 6475 | // Sub-scope to prevent `return error.CodegenFail` from running the errdefers. |
| 6448 | 6476 | { |
| 6449 | errdefer self.module.gpa.free(notes); | |
| 6477 | errdefer mod.gpa.free(notes); | |
| 6450 | 6478 | var i: usize = 0; |
| 6451 | 6479 | errdefer for (notes[0..i]) |*note| { |
| 6452 | note.deinit(self.module.gpa); | |
| 6480 | note.deinit(mod.gpa); | |
| 6453 | 6481 | }; |
| 6454 | 6482 | |
| 6455 | 6483 | while (i < as.errors.items.len) : (i += 1) { |
| 6456 | notes[i] = try Module.ErrorMsg.init(self.module.gpa, src_loc, "{s}", .{as.errors.items[i].msg}); | |
| 6484 | notes[i] = try Zcu.ErrorMsg.init(mod.gpa, src_loc, "{s}", .{as.errors.items[i].msg}); | |
| 6457 | 6485 | } |
| 6458 | 6486 | } |
| 6459 | 6487 | self.error_msg.?.notes = notes; |
| ... | ... | @@ -6489,7 +6517,8 @@ const DeclGen = struct { |
| 6489 | 6517 | fn airCall(self: *DeclGen, inst: Air.Inst.Index, modifier: std.builtin.CallModifier) !?IdRef { |
| 6490 | 6518 | _ = modifier; |
| 6491 | 6519 | |
| 6492 | const mod = self.module; | |
| 6520 | const pt = self.pt; | |
| 6521 | const mod = pt.zcu; | |
| 6493 | 6522 | const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 6494 | 6523 | const extra = self.air.extraData(Air.Call, pl_op.payload); |
| 6495 | 6524 | const args: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra.end..][0..extra.data.args_len]); |
| ... | ... | @@ -6515,7 +6544,7 @@ const DeclGen = struct { |
| 6515 | 6544 | // before starting to emit OpFunctionCall instructions. Hence the |
| 6516 | 6545 | // temporary params buffer. |
| 6517 | 6546 | const arg_ty = self.typeOf(arg); |
| 6518 | if (!arg_ty.hasRuntimeBitsIgnoreComptime(mod)) continue; | |
| 6547 | if (!arg_ty.hasRuntimeBitsIgnoreComptime(pt)) continue; | |
| 6519 | 6548 | const arg_id = try self.resolve(arg); |
| 6520 | 6549 | |
| 6521 | 6550 | params[n_params] = arg_id; |
| ... | ... | @@ -6533,7 +6562,7 @@ const DeclGen = struct { |
| 6533 | 6562 | try self.func.body.emit(self.spv.gpa, .OpUnreachable, {}); |
| 6534 | 6563 | } |
| 6535 | 6564 | |
| 6536 | if (self.liveness.isUnused(inst) or !Type.fromInterned(return_type).hasRuntimeBitsIgnoreComptime(mod)) { | |
| 6565 | if (self.liveness.isUnused(inst) or !Type.fromInterned(return_type).hasRuntimeBitsIgnoreComptime(pt)) { | |
| 6537 | 6566 | return null; |
| 6538 | 6567 | } |
| 6539 | 6568 | |
| ... | ... | @@ -6541,11 +6570,10 @@ const DeclGen = struct { |
| 6541 | 6570 | } |
| 6542 | 6571 | |
| 6543 | 6572 | fn builtin3D(self: *DeclGen, result_ty: Type, builtin: spec.BuiltIn, dimension: u32, out_of_range_value: anytype) !IdRef { |
| 6544 | const mod = self.module; | |
| 6545 | 6573 | if (dimension >= 3) { |
| 6546 | 6574 | return try self.constInt(result_ty, out_of_range_value, .direct); |
| 6547 | 6575 | } |
| 6548 | const vec_ty = try mod.vectorType(.{ | |
| 6576 | const vec_ty = try self.pt.vectorType(.{ | |
| 6549 | 6577 | .len = 3, |
| 6550 | 6578 | .child = result_ty.toIntern(), |
| 6551 | 6579 | }); |
| ... | ... | @@ -6591,12 +6619,12 @@ const DeclGen = struct { |
| 6591 | 6619 | } |
| 6592 | 6620 | |
| 6593 | 6621 | fn typeOf(self: *DeclGen, inst: Air.Inst.Ref) Type { |
| 6594 | const mod = self.module; | |
| 6622 | const mod = self.pt.zcu; | |
| 6595 | 6623 | return self.air.typeOf(inst, &mod.intern_pool); |
| 6596 | 6624 | } |
| 6597 | 6625 | |
| 6598 | 6626 | fn typeOfIndex(self: *DeclGen, inst: Air.Inst.Index) Type { |
| 6599 | const mod = self.module; | |
| 6627 | const mod = self.pt.zcu; | |
| 6600 | 6628 | return self.air.typeOfIndex(inst, &mod.intern_pool); |
| 6601 | 6629 | } |
| 6602 | 6630 | }; |
src/crash_report.zig+3-3| ... | ... | @@ -76,9 +76,9 @@ fn dumpStatusReport() !void { |
| 76 | 76 | |
| 77 | 77 | const stderr = io.getStdErr().writer(); |
| 78 | 78 | const block: *Sema.Block = anal.block; |
| 79 | const mod = anal.sema.mod; | |
| 79 | const zcu = anal.sema.pt.zcu; | |
| 80 | 80 | |
| 81 | const file, const src_base_node = Zcu.LazySrcLoc.resolveBaseNode(block.src_base_inst, mod); | |
| 81 | const file, const src_base_node = Zcu.LazySrcLoc.resolveBaseNode(block.src_base_inst, zcu); | |
| 82 | 82 | |
| 83 | 83 | try stderr.writeAll("Analyzing "); |
| 84 | 84 | try writeFilePath(file, stderr); |
| ... | ... | @@ -104,7 +104,7 @@ fn dumpStatusReport() !void { |
| 104 | 104 | while (parent) |curr| { |
| 105 | 105 | fba.reset(); |
| 106 | 106 | try stderr.writeAll(" in "); |
| 107 | const cur_block_file, const cur_block_src_base_node = Zcu.LazySrcLoc.resolveBaseNode(curr.block.src_base_inst, mod); | |
| 107 | const cur_block_file, const cur_block_src_base_node = Zcu.LazySrcLoc.resolveBaseNode(curr.block.src_base_inst, zcu); | |
| 108 | 108 | try writeFilePath(cur_block_file, stderr); |
| 109 | 109 | try stderr.writeAll("\n > "); |
| 110 | 110 | print_zir.renderSingleInstruction( |
src/link.zig+24-25| ... | ... | @@ -15,8 +15,6 @@ const Compilation = @import("Compilation.zig"); |
| 15 | 15 | const LibCInstallation = std.zig.LibCInstallation; |
| 16 | 16 | const Liveness = @import("Liveness.zig"); |
| 17 | 17 | const Zcu = @import("Zcu.zig"); |
| 18 | /// Deprecated. | |
| 19 | const Module = Zcu; | |
| 20 | 18 | const InternPool = @import("InternPool.zig"); |
| 21 | 19 | const Type = @import("Type.zig"); |
| 22 | 20 | const Value = @import("Value.zig"); |
| ... | ... | @@ -367,14 +365,14 @@ pub const File = struct { |
| 367 | 365 | /// Called from within the CodeGen to lower a local variable instantion as an unnamed |
| 368 | 366 | /// constant. Returns the symbol index of the lowered constant in the read-only section |
| 369 | 367 | /// of the final binary. |
| 370 | pub fn lowerUnnamedConst(base: *File, val: Value, decl_index: InternPool.DeclIndex) UpdateDeclError!u32 { | |
| 368 | pub fn lowerUnnamedConst(base: *File, pt: Zcu.PerThread, val: Value, decl_index: InternPool.DeclIndex) UpdateDeclError!u32 { | |
| 371 | 369 | if (build_options.only_c) @compileError("unreachable"); |
| 372 | 370 | switch (base.tag) { |
| 373 | 371 | .spirv => unreachable, |
| 374 | 372 | .c => unreachable, |
| 375 | 373 | .nvptx => unreachable, |
| 376 | 374 | inline else => |t| { |
| 377 | return @as(*t.Type(), @fieldParentPtr("base", base)).lowerUnnamedConst(val, decl_index); | |
| 375 | return @as(*t.Type(), @fieldParentPtr("base", base)).lowerUnnamedConst(pt, val, decl_index); | |
| 378 | 376 | }, |
| 379 | 377 | } |
| 380 | 378 | } |
| ... | ... | @@ -399,13 +397,13 @@ pub const File = struct { |
| 399 | 397 | } |
| 400 | 398 | |
| 401 | 399 | /// May be called before or after updateExports for any given Decl. |
| 402 | pub fn updateDecl(base: *File, module: *Module, decl_index: InternPool.DeclIndex) UpdateDeclError!void { | |
| 403 | const decl = module.declPtr(decl_index); | |
| 400 | pub fn updateDecl(base: *File, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex) UpdateDeclError!void { | |
| 401 | const decl = pt.zcu.declPtr(decl_index); | |
| 404 | 402 | assert(decl.has_tv); |
| 405 | 403 | switch (base.tag) { |
| 406 | 404 | inline else => |tag| { |
| 407 | 405 | if (tag != .c and build_options.only_c) unreachable; |
| 408 | return @as(*tag.Type(), @fieldParentPtr("base", base)).updateDecl(module, decl_index); | |
| 406 | return @as(*tag.Type(), @fieldParentPtr("base", base)).updateDecl(pt, decl_index); | |
| 409 | 407 | }, |
| 410 | 408 | } |
| 411 | 409 | } |
| ... | ... | @@ -413,7 +411,7 @@ pub const File = struct { |
| 413 | 411 | /// May be called before or after updateExports for any given Decl. |
| 414 | 412 | pub fn updateFunc( |
| 415 | 413 | base: *File, |
| 416 | module: *Module, | |
| 414 | pt: Zcu.PerThread, | |
| 417 | 415 | func_index: InternPool.Index, |
| 418 | 416 | air: Air, |
| 419 | 417 | liveness: Liveness, |
| ... | ... | @@ -421,12 +419,12 @@ pub const File = struct { |
| 421 | 419 | switch (base.tag) { |
| 422 | 420 | inline else => |tag| { |
| 423 | 421 | if (tag != .c and build_options.only_c) unreachable; |
| 424 | return @as(*tag.Type(), @fieldParentPtr("base", base)).updateFunc(module, func_index, air, liveness); | |
| 422 | return @as(*tag.Type(), @fieldParentPtr("base", base)).updateFunc(pt, func_index, air, liveness); | |
| 425 | 423 | }, |
| 426 | 424 | } |
| 427 | 425 | } |
| 428 | 426 | |
| 429 | pub fn updateDeclLineNumber(base: *File, module: *Module, decl_index: InternPool.DeclIndex) UpdateDeclError!void { | |
| 427 | pub fn updateDeclLineNumber(base: *File, module: *Zcu, decl_index: InternPool.DeclIndex) UpdateDeclError!void { | |
| 430 | 428 | const decl = module.declPtr(decl_index); |
| 431 | 429 | assert(decl.has_tv); |
| 432 | 430 | switch (base.tag) { |
| ... | ... | @@ -537,7 +535,7 @@ pub const File = struct { |
| 537 | 535 | /// Commit pending changes and write headers. Takes into account final output mode |
| 538 | 536 | /// and `use_lld`, not only `effectiveOutputMode`. |
| 539 | 537 | /// `arena` has the lifetime of the call to `Compilation.update`. |
| 540 | pub fn flush(base: *File, arena: Allocator, prog_node: std.Progress.Node) FlushError!void { | |
| 538 | pub fn flush(base: *File, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) FlushError!void { | |
| 541 | 539 | if (build_options.only_c) { |
| 542 | 540 | assert(base.tag == .c); |
| 543 | 541 | return @as(*C, @fieldParentPtr("base", base)).flush(arena, prog_node); |
| ... | ... | @@ -563,27 +561,27 @@ pub const File = struct { |
| 563 | 561 | const output_mode = comp.config.output_mode; |
| 564 | 562 | const link_mode = comp.config.link_mode; |
| 565 | 563 | if (use_lld and output_mode == .Lib and link_mode == .static) { |
| 566 | return base.linkAsArchive(arena, prog_node); | |
| 564 | return base.linkAsArchive(arena, tid, prog_node); | |
| 567 | 565 | } |
| 568 | 566 | switch (base.tag) { |
| 569 | 567 | inline else => |tag| { |
| 570 | return @as(*tag.Type(), @fieldParentPtr("base", base)).flush(arena, prog_node); | |
| 568 | return @as(*tag.Type(), @fieldParentPtr("base", base)).flush(arena, tid, prog_node); | |
| 571 | 569 | }, |
| 572 | 570 | } |
| 573 | 571 | } |
| 574 | 572 | |
| 575 | 573 | /// Commit pending changes and write headers. Works based on `effectiveOutputMode` |
| 576 | 574 | /// rather than final output mode. |
| 577 | pub fn flushModule(base: *File, arena: Allocator, prog_node: std.Progress.Node) FlushError!void { | |
| 575 | pub fn flushModule(base: *File, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) FlushError!void { | |
| 578 | 576 | switch (base.tag) { |
| 579 | 577 | inline else => |tag| { |
| 580 | 578 | if (tag != .c and build_options.only_c) unreachable; |
| 581 | return @as(*tag.Type(), @fieldParentPtr("base", base)).flushModule(arena, prog_node); | |
| 579 | return @as(*tag.Type(), @fieldParentPtr("base", base)).flushModule(arena, tid, prog_node); | |
| 582 | 580 | }, |
| 583 | 581 | } |
| 584 | 582 | } |
| 585 | 583 | |
| 586 | /// Called when a Decl is deleted from the Module. | |
| 584 | /// Called when a Decl is deleted from the Zcu. | |
| 587 | 585 | pub fn freeDecl(base: *File, decl_index: InternPool.DeclIndex) void { |
| 588 | 586 | switch (base.tag) { |
| 589 | 587 | inline else => |tag| { |
| ... | ... | @@ -604,14 +602,14 @@ pub const File = struct { |
| 604 | 602 | /// May be called before or after updateDecl for any given Decl. |
| 605 | 603 | pub fn updateExports( |
| 606 | 604 | base: *File, |
| 607 | module: *Module, | |
| 608 | exported: Module.Exported, | |
| 605 | pt: Zcu.PerThread, | |
| 606 | exported: Zcu.Exported, | |
| 609 | 607 | export_indices: []const u32, |
| 610 | 608 | ) UpdateExportsError!void { |
| 611 | 609 | switch (base.tag) { |
| 612 | 610 | inline else => |tag| { |
| 613 | 611 | if (tag != .c and build_options.only_c) unreachable; |
| 614 | return @as(*tag.Type(), @fieldParentPtr("base", base)).updateExports(module, exported, export_indices); | |
| 612 | return @as(*tag.Type(), @fieldParentPtr("base", base)).updateExports(pt, exported, export_indices); | |
| 615 | 613 | }, |
| 616 | 614 | } |
| 617 | 615 | } |
| ... | ... | @@ -644,9 +642,10 @@ pub const File = struct { |
| 644 | 642 | |
| 645 | 643 | pub fn lowerAnonDecl( |
| 646 | 644 | base: *File, |
| 645 | pt: Zcu.PerThread, | |
| 647 | 646 | decl_val: InternPool.Index, |
| 648 | 647 | decl_align: InternPool.Alignment, |
| 649 | src_loc: Module.LazySrcLoc, | |
| 648 | src_loc: Zcu.LazySrcLoc, | |
| 650 | 649 | ) !LowerResult { |
| 651 | 650 | if (build_options.only_c) @compileError("unreachable"); |
| 652 | 651 | switch (base.tag) { |
| ... | ... | @@ -654,7 +653,7 @@ pub const File = struct { |
| 654 | 653 | .spirv => unreachable, |
| 655 | 654 | .nvptx => unreachable, |
| 656 | 655 | inline else => |tag| { |
| 657 | return @as(*tag.Type(), @fieldParentPtr("base", base)).lowerAnonDecl(decl_val, decl_align, src_loc); | |
| 656 | return @as(*tag.Type(), @fieldParentPtr("base", base)).lowerAnonDecl(pt, decl_val, decl_align, src_loc); | |
| 658 | 657 | }, |
| 659 | 658 | } |
| 660 | 659 | } |
| ... | ... | @@ -689,7 +688,7 @@ pub const File = struct { |
| 689 | 688 | } |
| 690 | 689 | } |
| 691 | 690 | |
| 692 | pub fn linkAsArchive(base: *File, arena: Allocator, prog_node: std.Progress.Node) FlushError!void { | |
| 691 | pub fn linkAsArchive(base: *File, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) FlushError!void { | |
| 693 | 692 | const tracy = trace(@src()); |
| 694 | 693 | defer tracy.end(); |
| 695 | 694 | |
| ... | ... | @@ -704,7 +703,7 @@ pub const File = struct { |
| 704 | 703 | // If there is no Zig code to compile, then we should skip flushing the output file |
| 705 | 704 | // because it will not be part of the linker line anyway. |
| 706 | 705 | const zcu_obj_path: ?[]const u8 = if (opt_zcu != null) blk: { |
| 707 | try base.flushModule(arena, prog_node); | |
| 706 | try base.flushModule(arena, tid, prog_node); | |
| 708 | 707 | |
| 709 | 708 | const dirname = fs.path.dirname(full_out_path_z) orelse "."; |
| 710 | 709 | break :blk try fs.path.join(arena, &.{ dirname, base.zcu_object_sub_path.? }); |
| ... | ... | @@ -896,14 +895,14 @@ pub const File = struct { |
| 896 | 895 | kind: Kind, |
| 897 | 896 | ty: Type, |
| 898 | 897 | |
| 899 | pub fn initDecl(kind: Kind, decl: ?InternPool.DeclIndex, mod: *Module) LazySymbol { | |
| 898 | pub fn initDecl(kind: Kind, decl: ?InternPool.DeclIndex, mod: *Zcu) LazySymbol { | |
| 900 | 899 | return .{ .kind = kind, .ty = if (decl) |decl_index| |
| 901 | 900 | mod.declPtr(decl_index).val.toType() |
| 902 | 901 | else |
| 903 | 902 | Type.anyerror }; |
| 904 | 903 | } |
| 905 | 904 | |
| 906 | pub fn getDecl(self: LazySymbol, mod: *Module) InternPool.OptionalDeclIndex { | |
| 905 | pub fn getDecl(self: LazySymbol, mod: *Zcu) InternPool.OptionalDeclIndex { | |
| 907 | 906 | return InternPool.OptionalDeclIndex.init(self.ty.getOwnerDeclOrNull(mod)); |
| 908 | 907 | } |
| 909 | 908 | }; |
src/link/C.zig+31-28| ... | ... | @@ -186,13 +186,13 @@ pub fn freeDecl(self: *C, decl_index: InternPool.DeclIndex) void { |
| 186 | 186 | |
| 187 | 187 | pub fn updateFunc( |
| 188 | 188 | self: *C, |
| 189 | zcu: *Zcu, | |
| 189 | pt: Zcu.PerThread, | |
| 190 | 190 | func_index: InternPool.Index, |
| 191 | 191 | air: Air, |
| 192 | 192 | liveness: Liveness, |
| 193 | 193 | ) !void { |
| 194 | const gpa = self.base.comp.gpa; | |
| 195 | ||
| 194 | const zcu = pt.zcu; | |
| 195 | const gpa = zcu.gpa; | |
| 196 | 196 | const func = zcu.funcInfo(func_index); |
| 197 | 197 | const decl_index = func.owner_decl; |
| 198 | 198 | const decl = zcu.declPtr(decl_index); |
| ... | ... | @@ -218,7 +218,7 @@ pub fn updateFunc( |
| 218 | 218 | .object = .{ |
| 219 | 219 | .dg = .{ |
| 220 | 220 | .gpa = gpa, |
| 221 | .zcu = zcu, | |
| 221 | .pt = pt, | |
| 222 | 222 | .mod = file_scope.mod, |
| 223 | 223 | .error_msg = null, |
| 224 | 224 | .pass = .{ .decl = decl_index }, |
| ... | ... | @@ -263,7 +263,7 @@ pub fn updateFunc( |
| 263 | 263 | gop.value_ptr.code = try self.addString(function.object.code.items); |
| 264 | 264 | } |
| 265 | 265 | |
| 266 | fn updateAnonDecl(self: *C, zcu: *Zcu, i: usize) !void { | |
| 266 | fn updateAnonDecl(self: *C, pt: Zcu.PerThread, i: usize) !void { | |
| 267 | 267 | const gpa = self.base.comp.gpa; |
| 268 | 268 | const anon_decl = self.anon_decls.keys()[i]; |
| 269 | 269 | |
| ... | ... | @@ -275,8 +275,8 @@ fn updateAnonDecl(self: *C, zcu: *Zcu, i: usize) !void { |
| 275 | 275 | var object: codegen.Object = .{ |
| 276 | 276 | .dg = .{ |
| 277 | 277 | .gpa = gpa, |
| 278 | .zcu = zcu, | |
| 279 | .mod = zcu.root_mod, | |
| 278 | .pt = pt, | |
| 279 | .mod = pt.zcu.root_mod, | |
| 280 | 280 | .error_msg = null, |
| 281 | 281 | .pass = .{ .anon = anon_decl }, |
| 282 | 282 | .is_naked_fn = false, |
| ... | ... | @@ -319,12 +319,13 @@ fn updateAnonDecl(self: *C, zcu: *Zcu, i: usize) !void { |
| 319 | 319 | }; |
| 320 | 320 | } |
| 321 | 321 | |
| 322 | pub fn updateDecl(self: *C, zcu: *Zcu, decl_index: InternPool.DeclIndex) !void { | |
| 322 | pub fn updateDecl(self: *C, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex) !void { | |
| 323 | 323 | const tracy = trace(@src()); |
| 324 | 324 | defer tracy.end(); |
| 325 | 325 | |
| 326 | 326 | const gpa = self.base.comp.gpa; |
| 327 | 327 | |
| 328 | const zcu = pt.zcu; | |
| 328 | 329 | const decl = zcu.declPtr(decl_index); |
| 329 | 330 | const gop = try self.decl_table.getOrPut(gpa, decl_index); |
| 330 | 331 | errdefer _ = self.decl_table.pop(); |
| ... | ... | @@ -342,7 +343,7 @@ pub fn updateDecl(self: *C, zcu: *Zcu, decl_index: InternPool.DeclIndex) !void { |
| 342 | 343 | var object: codegen.Object = .{ |
| 343 | 344 | .dg = .{ |
| 344 | 345 | .gpa = gpa, |
| 345 | .zcu = zcu, | |
| 346 | .pt = pt, | |
| 346 | 347 | .mod = file_scope.mod, |
| 347 | 348 | .error_msg = null, |
| 348 | 349 | .pass = .{ .decl = decl_index }, |
| ... | ... | @@ -390,8 +391,8 @@ pub fn updateDeclLineNumber(self: *C, zcu: *Zcu, decl_index: InternPool.DeclInde |
| 390 | 391 | _ = decl_index; |
| 391 | 392 | } |
| 392 | 393 | |
| 393 | pub fn flush(self: *C, arena: Allocator, prog_node: std.Progress.Node) !void { | |
| 394 | return self.flushModule(arena, prog_node); | |
| 394 | pub fn flush(self: *C, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) !void { | |
| 395 | return self.flushModule(arena, tid, prog_node); | |
| 395 | 396 | } |
| 396 | 397 | |
| 397 | 398 | fn abiDefines(self: *C, target: std.Target) !std.ArrayList(u8) { |
| ... | ... | @@ -409,7 +410,7 @@ fn abiDefines(self: *C, target: std.Target) !std.ArrayList(u8) { |
| 409 | 410 | return defines; |
| 410 | 411 | } |
| 411 | 412 | |
| 412 | pub fn flushModule(self: *C, arena: Allocator, prog_node: std.Progress.Node) !void { | |
| 413 | pub fn flushModule(self: *C, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) !void { | |
| 413 | 414 | _ = arena; // Has the same lifetime as the call to Compilation.update. |
| 414 | 415 | |
| 415 | 416 | const tracy = trace(@src()); |
| ... | ... | @@ -421,11 +422,12 @@ pub fn flushModule(self: *C, arena: Allocator, prog_node: std.Progress.Node) !vo |
| 421 | 422 | const comp = self.base.comp; |
| 422 | 423 | const gpa = comp.gpa; |
| 423 | 424 | const zcu = self.base.comp.module.?; |
| 425 | const pt: Zcu.PerThread = .{ .zcu = zcu, .tid = tid }; | |
| 424 | 426 | |
| 425 | 427 | { |
| 426 | 428 | var i: usize = 0; |
| 427 | 429 | while (i < self.anon_decls.count()) : (i += 1) { |
| 428 | try updateAnonDecl(self, zcu, i); | |
| 430 | try updateAnonDecl(self, pt, i); | |
| 429 | 431 | } |
| 430 | 432 | } |
| 431 | 433 | |
| ... | ... | @@ -463,7 +465,7 @@ pub fn flushModule(self: *C, arena: Allocator, prog_node: std.Progress.Node) !vo |
| 463 | 465 | self.lazy_fwd_decl_buf.clearRetainingCapacity(); |
| 464 | 466 | self.lazy_code_buf.clearRetainingCapacity(); |
| 465 | 467 | try f.lazy_ctype_pool.init(gpa); |
| 466 | try self.flushErrDecls(zcu, &f.lazy_ctype_pool); | |
| 468 | try self.flushErrDecls(pt, &f.lazy_ctype_pool); | |
| 467 | 469 | |
| 468 | 470 | // Unlike other backends, the .c code we are emitting has order-dependent decls. |
| 469 | 471 | // `CType`s, forward decls, and non-functions first. |
| ... | ... | @@ -483,7 +485,7 @@ pub fn flushModule(self: *C, arena: Allocator, prog_node: std.Progress.Node) !vo |
| 483 | 485 | } |
| 484 | 486 | |
| 485 | 487 | for (self.anon_decls.keys(), self.anon_decls.values()) |value, *decl_block| try self.flushDeclBlock( |
| 486 | zcu, | |
| 488 | pt, | |
| 487 | 489 | zcu.root_mod, |
| 488 | 490 | &f, |
| 489 | 491 | decl_block, |
| ... | ... | @@ -497,7 +499,7 @@ pub fn flushModule(self: *C, arena: Allocator, prog_node: std.Progress.Node) !vo |
| 497 | 499 | const extern_name = if (decl.isExtern(zcu)) decl.name.toOptional() else .none; |
| 498 | 500 | const mod = zcu.namespacePtr(decl.src_namespace).fileScope(zcu).mod; |
| 499 | 501 | try self.flushDeclBlock( |
| 500 | zcu, | |
| 502 | pt, | |
| 501 | 503 | mod, |
| 502 | 504 | &f, |
| 503 | 505 | decl_block, |
| ... | ... | @@ -670,7 +672,7 @@ fn flushCTypes( |
| 670 | 672 | } |
| 671 | 673 | } |
| 672 | 674 | |
| 673 | fn flushErrDecls(self: *C, zcu: *Zcu, ctype_pool: *codegen.CType.Pool) FlushDeclError!void { | |
| 675 | fn flushErrDecls(self: *C, pt: Zcu.PerThread, ctype_pool: *codegen.CType.Pool) FlushDeclError!void { | |
| 674 | 676 | const gpa = self.base.comp.gpa; |
| 675 | 677 | |
| 676 | 678 | const fwd_decl = &self.lazy_fwd_decl_buf; |
| ... | ... | @@ -679,8 +681,8 @@ fn flushErrDecls(self: *C, zcu: *Zcu, ctype_pool: *codegen.CType.Pool) FlushDecl |
| 679 | 681 | var object = codegen.Object{ |
| 680 | 682 | .dg = .{ |
| 681 | 683 | .gpa = gpa, |
| 682 | .zcu = zcu, | |
| 683 | .mod = zcu.root_mod, | |
| 684 | .pt = pt, | |
| 685 | .mod = pt.zcu.root_mod, | |
| 684 | 686 | .error_msg = null, |
| 685 | 687 | .pass = .flush, |
| 686 | 688 | .is_naked_fn = false, |
| ... | ... | @@ -712,7 +714,7 @@ fn flushErrDecls(self: *C, zcu: *Zcu, ctype_pool: *codegen.CType.Pool) FlushDecl |
| 712 | 714 | |
| 713 | 715 | fn flushLazyFn( |
| 714 | 716 | self: *C, |
| 715 | zcu: *Zcu, | |
| 717 | pt: Zcu.PerThread, | |
| 716 | 718 | mod: *Module, |
| 717 | 719 | ctype_pool: *codegen.CType.Pool, |
| 718 | 720 | lazy_ctype_pool: *const codegen.CType.Pool, |
| ... | ... | @@ -726,7 +728,7 @@ fn flushLazyFn( |
| 726 | 728 | var object = codegen.Object{ |
| 727 | 729 | .dg = .{ |
| 728 | 730 | .gpa = gpa, |
| 729 | .zcu = zcu, | |
| 731 | .pt = pt, | |
| 730 | 732 | .mod = mod, |
| 731 | 733 | .error_msg = null, |
| 732 | 734 | .pass = .flush, |
| ... | ... | @@ -761,7 +763,7 @@ fn flushLazyFn( |
| 761 | 763 | |
| 762 | 764 | fn flushLazyFns( |
| 763 | 765 | self: *C, |
| 764 | zcu: *Zcu, | |
| 766 | pt: Zcu.PerThread, | |
| 765 | 767 | mod: *Module, |
| 766 | 768 | f: *Flush, |
| 767 | 769 | lazy_ctype_pool: *const codegen.CType.Pool, |
| ... | ... | @@ -775,13 +777,13 @@ fn flushLazyFns( |
| 775 | 777 | const gop = f.lazy_fns.getOrPutAssumeCapacity(entry.key_ptr.*); |
| 776 | 778 | if (gop.found_existing) continue; |
| 777 | 779 | gop.value_ptr.* = {}; |
| 778 | try self.flushLazyFn(zcu, mod, &f.lazy_ctype_pool, lazy_ctype_pool, entry); | |
| 780 | try self.flushLazyFn(pt, mod, &f.lazy_ctype_pool, lazy_ctype_pool, entry); | |
| 779 | 781 | } |
| 780 | 782 | } |
| 781 | 783 | |
| 782 | 784 | fn flushDeclBlock( |
| 783 | 785 | self: *C, |
| 784 | zcu: *Zcu, | |
| 786 | pt: Zcu.PerThread, | |
| 785 | 787 | mod: *Module, |
| 786 | 788 | f: *Flush, |
| 787 | 789 | decl_block: *const DeclBlock, |
| ... | ... | @@ -790,7 +792,7 @@ fn flushDeclBlock( |
| 790 | 792 | extern_name: InternPool.OptionalNullTerminatedString, |
| 791 | 793 | ) FlushDeclError!void { |
| 792 | 794 | const gpa = self.base.comp.gpa; |
| 793 | try self.flushLazyFns(zcu, mod, f, &decl_block.ctype_pool, decl_block.lazy_fns); | |
| 795 | try self.flushLazyFns(pt, mod, f, &decl_block.ctype_pool, decl_block.lazy_fns); | |
| 794 | 796 | try f.all_buffers.ensureUnusedCapacity(gpa, 1); |
| 795 | 797 | // avoid emitting extern decls that are already exported |
| 796 | 798 | if (extern_name.unwrap()) |name| if (export_names.contains(name)) return; |
| ... | ... | @@ -845,11 +847,12 @@ pub fn flushEmitH(zcu: *Zcu) !void { |
| 845 | 847 | |
| 846 | 848 | pub fn updateExports( |
| 847 | 849 | self: *C, |
| 848 | zcu: *Zcu, | |
| 850 | pt: Zcu.PerThread, | |
| 849 | 851 | exported: Zcu.Exported, |
| 850 | 852 | export_indices: []const u32, |
| 851 | 853 | ) !void { |
| 852 | const gpa = self.base.comp.gpa; | |
| 854 | const zcu = pt.zcu; | |
| 855 | const gpa = zcu.gpa; | |
| 853 | 856 | const mod, const pass: codegen.DeclGen.Pass, const decl_block, const exported_block = switch (exported) { |
| 854 | 857 | .decl_index => |decl_index| .{ |
| 855 | 858 | zcu.namespacePtr(zcu.declPtr(decl_index).src_namespace).fileScope(zcu).mod, |
| ... | ... | @@ -869,7 +872,7 @@ pub fn updateExports( |
| 869 | 872 | fwd_decl.clearRetainingCapacity(); |
| 870 | 873 | var dg: codegen.DeclGen = .{ |
| 871 | 874 | .gpa = gpa, |
| 872 | .zcu = zcu, | |
| 875 | .pt = pt, | |
| 873 | 876 | .mod = mod, |
| 874 | 877 | .error_msg = null, |
| 875 | 878 | .pass = pass, |
src/link/Coff.zig+53-32| ... | ... | @@ -1120,16 +1120,17 @@ fn freeAtom(self: *Coff, atom_index: Atom.Index) void { |
| 1120 | 1120 | self.getAtomPtr(atom_index).sym_index = 0; |
| 1121 | 1121 | } |
| 1122 | 1122 | |
| 1123 | pub fn updateFunc(self: *Coff, mod: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 1123 | pub fn updateFunc(self: *Coff, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 1124 | 1124 | if (build_options.skip_non_native and builtin.object_format != .coff) { |
| 1125 | 1125 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 1126 | 1126 | } |
| 1127 | 1127 | if (self.llvm_object) |llvm_object| { |
| 1128 | return llvm_object.updateFunc(mod, func_index, air, liveness); | |
| 1128 | return llvm_object.updateFunc(pt, func_index, air, liveness); | |
| 1129 | 1129 | } |
| 1130 | 1130 | const tracy = trace(@src()); |
| 1131 | 1131 | defer tracy.end(); |
| 1132 | 1132 | |
| 1133 | const mod = pt.zcu; | |
| 1133 | 1134 | const func = mod.funcInfo(func_index); |
| 1134 | 1135 | const decl_index = func.owner_decl; |
| 1135 | 1136 | const decl = mod.declPtr(decl_index); |
| ... | ... | @@ -1144,6 +1145,7 @@ pub fn updateFunc(self: *Coff, mod: *Module, func_index: InternPool.Index, air: |
| 1144 | 1145 | |
| 1145 | 1146 | const res = try codegen.generateFunction( |
| 1146 | 1147 | &self.base, |
| 1148 | pt, | |
| 1147 | 1149 | decl.navSrcLoc(mod), |
| 1148 | 1150 | func_index, |
| 1149 | 1151 | air, |
| ... | ... | @@ -1160,14 +1162,14 @@ pub fn updateFunc(self: *Coff, mod: *Module, func_index: InternPool.Index, air: |
| 1160 | 1162 | }, |
| 1161 | 1163 | }; |
| 1162 | 1164 | |
| 1163 | try self.updateDeclCode(decl_index, code, .FUNCTION); | |
| 1165 | try self.updateDeclCode(pt, decl_index, code, .FUNCTION); | |
| 1164 | 1166 | |
| 1165 | 1167 | // Exports will be updated by `Zcu.processExports` after the update. |
| 1166 | 1168 | } |
| 1167 | 1169 | |
| 1168 | pub fn lowerUnnamedConst(self: *Coff, val: Value, decl_index: InternPool.DeclIndex) !u32 { | |
| 1169 | const gpa = self.base.comp.gpa; | |
| 1170 | const mod = self.base.comp.module.?; | |
| 1170 | pub fn lowerUnnamedConst(self: *Coff, pt: Zcu.PerThread, val: Value, decl_index: InternPool.DeclIndex) !u32 { | |
| 1171 | const mod = pt.zcu; | |
| 1172 | const gpa = mod.gpa; | |
| 1171 | 1173 | const decl = mod.declPtr(decl_index); |
| 1172 | 1174 | const gop = try self.unnamed_const_atoms.getOrPut(gpa, decl_index); |
| 1173 | 1175 | if (!gop.found_existing) { |
| ... | ... | @@ -1179,7 +1181,7 @@ pub fn lowerUnnamedConst(self: *Coff, val: Value, decl_index: InternPool.DeclInd |
| 1179 | 1181 | const sym_name = try std.fmt.allocPrint(gpa, "__unnamed_{}_{d}", .{ decl_name.fmt(&mod.intern_pool), index }); |
| 1180 | 1182 | defer gpa.free(sym_name); |
| 1181 | 1183 | const ty = val.typeOf(mod); |
| 1182 | const atom_index = switch (try self.lowerConst(sym_name, val, ty.abiAlignment(mod), self.rdata_section_index.?, decl.navSrcLoc(mod))) { | |
| 1184 | const atom_index = switch (try self.lowerConst(pt, sym_name, val, ty.abiAlignment(pt), self.rdata_section_index.?, decl.navSrcLoc(mod))) { | |
| 1183 | 1185 | .ok => |atom_index| atom_index, |
| 1184 | 1186 | .fail => |em| { |
| 1185 | 1187 | decl.analysis = .codegen_failure; |
| ... | ... | @@ -1197,7 +1199,15 @@ const LowerConstResult = union(enum) { |
| 1197 | 1199 | fail: *Module.ErrorMsg, |
| 1198 | 1200 | }; |
| 1199 | 1201 | |
| 1200 | fn lowerConst(self: *Coff, name: []const u8, val: Value, required_alignment: InternPool.Alignment, sect_id: u16, src_loc: Module.LazySrcLoc) !LowerConstResult { | |
| 1202 | fn lowerConst( | |
| 1203 | self: *Coff, | |
| 1204 | pt: Zcu.PerThread, | |
| 1205 | name: []const u8, | |
| 1206 | val: Value, | |
| 1207 | required_alignment: InternPool.Alignment, | |
| 1208 | sect_id: u16, | |
| 1209 | src_loc: Module.LazySrcLoc, | |
| 1210 | ) !LowerConstResult { | |
| 1201 | 1211 | const gpa = self.base.comp.gpa; |
| 1202 | 1212 | |
| 1203 | 1213 | var code_buffer = std.ArrayList(u8).init(gpa); |
| ... | ... | @@ -1208,7 +1218,7 @@ fn lowerConst(self: *Coff, name: []const u8, val: Value, required_alignment: Int |
| 1208 | 1218 | try self.setSymbolName(sym, name); |
| 1209 | 1219 | sym.section_number = @as(coff.SectionNumber, @enumFromInt(sect_id + 1)); |
| 1210 | 1220 | |
| 1211 | const res = try codegen.generateSymbol(&self.base, src_loc, val, &code_buffer, .none, .{ | |
| 1221 | const res = try codegen.generateSymbol(&self.base, pt, src_loc, val, &code_buffer, .none, .{ | |
| 1212 | 1222 | .parent_atom_index = self.getAtom(atom_index).getSymbolIndex().?, |
| 1213 | 1223 | }); |
| 1214 | 1224 | const code = switch (res) { |
| ... | ... | @@ -1235,13 +1245,14 @@ fn lowerConst(self: *Coff, name: []const u8, val: Value, required_alignment: Int |
| 1235 | 1245 | |
| 1236 | 1246 | pub fn updateDecl( |
| 1237 | 1247 | self: *Coff, |
| 1238 | mod: *Module, | |
| 1248 | pt: Zcu.PerThread, | |
| 1239 | 1249 | decl_index: InternPool.DeclIndex, |
| 1240 | 1250 | ) link.File.UpdateDeclError!void { |
| 1251 | const mod = pt.zcu; | |
| 1241 | 1252 | if (build_options.skip_non_native and builtin.object_format != .coff) { |
| 1242 | 1253 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 1243 | 1254 | } |
| 1244 | if (self.llvm_object) |llvm_object| return llvm_object.updateDecl(mod, decl_index); | |
| 1255 | if (self.llvm_object) |llvm_object| return llvm_object.updateDecl(pt, decl_index); | |
| 1245 | 1256 | const tracy = trace(@src()); |
| 1246 | 1257 | defer tracy.end(); |
| 1247 | 1258 | |
| ... | ... | @@ -1270,7 +1281,7 @@ pub fn updateDecl( |
| 1270 | 1281 | defer code_buffer.deinit(); |
| 1271 | 1282 | |
| 1272 | 1283 | const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val; |
| 1273 | const res = try codegen.generateSymbol(&self.base, decl.navSrcLoc(mod), decl_val, &code_buffer, .none, .{ | |
| 1284 | const res = try codegen.generateSymbol(&self.base, pt, decl.navSrcLoc(mod), decl_val, &code_buffer, .none, .{ | |
| 1274 | 1285 | .parent_atom_index = atom.getSymbolIndex().?, |
| 1275 | 1286 | }); |
| 1276 | 1287 | const code = switch (res) { |
| ... | ... | @@ -1282,19 +1293,20 @@ pub fn updateDecl( |
| 1282 | 1293 | }, |
| 1283 | 1294 | }; |
| 1284 | 1295 | |
| 1285 | try self.updateDeclCode(decl_index, code, .NULL); | |
| 1296 | try self.updateDeclCode(pt, decl_index, code, .NULL); | |
| 1286 | 1297 | |
| 1287 | 1298 | // Exports will be updated by `Zcu.processExports` after the update. |
| 1288 | 1299 | } |
| 1289 | 1300 | |
| 1290 | 1301 | fn updateLazySymbolAtom( |
| 1291 | 1302 | self: *Coff, |
| 1303 | pt: Zcu.PerThread, | |
| 1292 | 1304 | sym: link.File.LazySymbol, |
| 1293 | 1305 | atom_index: Atom.Index, |
| 1294 | 1306 | section_index: u16, |
| 1295 | 1307 | ) !void { |
| 1296 | const gpa = self.base.comp.gpa; | |
| 1297 | const mod = self.base.comp.module.?; | |
| 1308 | const mod = pt.zcu; | |
| 1309 | const gpa = mod.gpa; | |
| 1298 | 1310 | |
| 1299 | 1311 | var required_alignment: InternPool.Alignment = .none; |
| 1300 | 1312 | var code_buffer = std.ArrayList(u8).init(gpa); |
| ... | ... | @@ -1302,7 +1314,7 @@ fn updateLazySymbolAtom( |
| 1302 | 1314 | |
| 1303 | 1315 | const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{}", .{ |
| 1304 | 1316 | @tagName(sym.kind), |
| 1305 | sym.ty.fmt(mod), | |
| 1317 | sym.ty.fmt(pt), | |
| 1306 | 1318 | }); |
| 1307 | 1319 | defer gpa.free(name); |
| 1308 | 1320 | |
| ... | ... | @@ -1312,6 +1324,7 @@ fn updateLazySymbolAtom( |
| 1312 | 1324 | const src = sym.ty.srcLocOrNull(mod) orelse Module.LazySrcLoc.unneeded; |
| 1313 | 1325 | const res = try codegen.generateLazySymbol( |
| 1314 | 1326 | &self.base, |
| 1327 | pt, | |
| 1315 | 1328 | src, |
| 1316 | 1329 | sym, |
| 1317 | 1330 | &required_alignment, |
| ... | ... | @@ -1346,7 +1359,7 @@ fn updateLazySymbolAtom( |
| 1346 | 1359 | try self.writeAtom(atom_index, code); |
| 1347 | 1360 | } |
| 1348 | 1361 | |
| 1349 | pub fn getOrCreateAtomForLazySymbol(self: *Coff, sym: link.File.LazySymbol) !Atom.Index { | |
| 1362 | pub fn getOrCreateAtomForLazySymbol(self: *Coff, pt: Zcu.PerThread, sym: link.File.LazySymbol) !Atom.Index { | |
| 1350 | 1363 | const gpa = self.base.comp.gpa; |
| 1351 | 1364 | const mod = self.base.comp.module.?; |
| 1352 | 1365 | const gop = try self.lazy_syms.getOrPut(gpa, sym.getDecl(mod)); |
| ... | ... | @@ -1364,7 +1377,7 @@ pub fn getOrCreateAtomForLazySymbol(self: *Coff, sym: link.File.LazySymbol) !Ato |
| 1364 | 1377 | metadata.state.* = .pending_flush; |
| 1365 | 1378 | const atom = metadata.atom.*; |
| 1366 | 1379 | // anyerror needs to be deferred until flushModule |
| 1367 | if (sym.getDecl(mod) != .none) try self.updateLazySymbolAtom(sym, atom, switch (sym.kind) { | |
| 1380 | if (sym.getDecl(mod) != .none) try self.updateLazySymbolAtom(pt, sym, atom, switch (sym.kind) { | |
| 1368 | 1381 | .code => self.text_section_index.?, |
| 1369 | 1382 | .const_data => self.rdata_section_index.?, |
| 1370 | 1383 | }); |
| ... | ... | @@ -1410,14 +1423,14 @@ fn getDeclOutputSection(self: *Coff, decl_index: InternPool.DeclIndex) u16 { |
| 1410 | 1423 | return index; |
| 1411 | 1424 | } |
| 1412 | 1425 | |
| 1413 | fn updateDeclCode(self: *Coff, decl_index: InternPool.DeclIndex, code: []u8, complex_type: coff.ComplexType) !void { | |
| 1414 | const mod = self.base.comp.module.?; | |
| 1426 | fn updateDeclCode(self: *Coff, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex, code: []u8, complex_type: coff.ComplexType) !void { | |
| 1427 | const mod = pt.zcu; | |
| 1415 | 1428 | const decl = mod.declPtr(decl_index); |
| 1416 | 1429 | |
| 1417 | 1430 | const decl_name = try decl.fullyQualifiedName(mod); |
| 1418 | 1431 | |
| 1419 | 1432 | log.debug("updateDeclCode {}{*}", .{ decl_name.fmt(&mod.intern_pool), decl }); |
| 1420 | const required_alignment: u32 = @intCast(decl.getAlignment(mod).toByteUnits() orelse 0); | |
| 1433 | const required_alignment: u32 = @intCast(decl.getAlignment(pt).toByteUnits() orelse 0); | |
| 1421 | 1434 | |
| 1422 | 1435 | const decl_metadata = self.decls.get(decl_index).?; |
| 1423 | 1436 | const atom_index = decl_metadata.atom; |
| ... | ... | @@ -1496,7 +1509,7 @@ pub fn freeDecl(self: *Coff, decl_index: InternPool.DeclIndex) void { |
| 1496 | 1509 | |
| 1497 | 1510 | pub fn updateExports( |
| 1498 | 1511 | self: *Coff, |
| 1499 | mod: *Module, | |
| 1512 | pt: Zcu.PerThread, | |
| 1500 | 1513 | exported: Module.Exported, |
| 1501 | 1514 | export_indices: []const u32, |
| 1502 | 1515 | ) link.File.UpdateExportsError!void { |
| ... | ... | @@ -1504,6 +1517,7 @@ pub fn updateExports( |
| 1504 | 1517 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 1505 | 1518 | } |
| 1506 | 1519 | |
| 1520 | const mod = pt.zcu; | |
| 1507 | 1521 | const ip = &mod.intern_pool; |
| 1508 | 1522 | const comp = self.base.comp; |
| 1509 | 1523 | const target = comp.root_mod.resolved_target.result; |
| ... | ... | @@ -1542,7 +1556,7 @@ pub fn updateExports( |
| 1542 | 1556 | } |
| 1543 | 1557 | } |
| 1544 | 1558 | |
| 1545 | if (self.llvm_object) |llvm_object| return llvm_object.updateExports(mod, exported, export_indices); | |
| 1559 | if (self.llvm_object) |llvm_object| return llvm_object.updateExports(pt, exported, export_indices); | |
| 1546 | 1560 | |
| 1547 | 1561 | const gpa = comp.gpa; |
| 1548 | 1562 | |
| ... | ... | @@ -1553,7 +1567,7 @@ pub fn updateExports( |
| 1553 | 1567 | }, |
| 1554 | 1568 | .value => |value| self.anon_decls.getPtr(value) orelse blk: { |
| 1555 | 1569 | const first_exp = mod.all_exports.items[export_indices[0]]; |
| 1556 | const res = try self.lowerAnonDecl(value, .none, first_exp.src); | |
| 1570 | const res = try self.lowerAnonDecl(pt, value, .none, first_exp.src); | |
| 1557 | 1571 | switch (res) { |
| 1558 | 1572 | .ok => {}, |
| 1559 | 1573 | .fail => |em| { |
| ... | ... | @@ -1696,19 +1710,19 @@ fn resolveGlobalSymbol(self: *Coff, current: SymbolWithLoc) !void { |
| 1696 | 1710 | gop.value_ptr.* = current; |
| 1697 | 1711 | } |
| 1698 | 1712 | |
| 1699 | pub fn flush(self: *Coff, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 1713 | pub fn flush(self: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 1700 | 1714 | const comp = self.base.comp; |
| 1701 | 1715 | const use_lld = build_options.have_llvm and comp.config.use_lld; |
| 1702 | 1716 | if (use_lld) { |
| 1703 | return lld.linkWithLLD(self, arena, prog_node); | |
| 1717 | return lld.linkWithLLD(self, arena, tid, prog_node); | |
| 1704 | 1718 | } |
| 1705 | 1719 | switch (comp.config.output_mode) { |
| 1706 | .Exe, .Obj => return self.flushModule(arena, prog_node), | |
| 1720 | .Exe, .Obj => return self.flushModule(arena, tid, prog_node), | |
| 1707 | 1721 | .Lib => return error.TODOImplementWritingLibFiles, |
| 1708 | 1722 | } |
| 1709 | 1723 | } |
| 1710 | 1724 | |
| 1711 | pub fn flushModule(self: *Coff, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 1725 | pub fn flushModule(self: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 1712 | 1726 | const tracy = trace(@src()); |
| 1713 | 1727 | defer tracy.end(); |
| 1714 | 1728 | |
| ... | ... | @@ -1723,13 +1737,17 @@ pub fn flushModule(self: *Coff, arena: Allocator, prog_node: std.Progress.Node) |
| 1723 | 1737 | const sub_prog_node = prog_node.start("COFF Flush", 0); |
| 1724 | 1738 | defer sub_prog_node.end(); |
| 1725 | 1739 | |
| 1726 | const module = comp.module orelse return error.LinkingWithoutZigSourceUnimplemented; | |
| 1740 | const pt: Zcu.PerThread = .{ | |
| 1741 | .zcu = comp.module orelse return error.LinkingWithoutZigSourceUnimplemented, | |
| 1742 | .tid = tid, | |
| 1743 | }; | |
| 1727 | 1744 | |
| 1728 | 1745 | if (self.lazy_syms.getPtr(.none)) |metadata| { |
| 1729 | 1746 | // Most lazy symbols can be updated on first use, but |
| 1730 | 1747 | // anyerror needs to wait for everything to be flushed. |
| 1731 | 1748 | if (metadata.text_state != .unused) self.updateLazySymbolAtom( |
| 1732 | link.File.LazySymbol.initDecl(.code, null, module), | |
| 1749 | pt, | |
| 1750 | link.File.LazySymbol.initDecl(.code, null, pt.zcu), | |
| 1733 | 1751 | metadata.text_atom, |
| 1734 | 1752 | self.text_section_index.?, |
| 1735 | 1753 | ) catch |err| return switch (err) { |
| ... | ... | @@ -1737,7 +1755,8 @@ pub fn flushModule(self: *Coff, arena: Allocator, prog_node: std.Progress.Node) |
| 1737 | 1755 | else => |e| e, |
| 1738 | 1756 | }; |
| 1739 | 1757 | if (metadata.rdata_state != .unused) self.updateLazySymbolAtom( |
| 1740 | link.File.LazySymbol.initDecl(.const_data, null, module), | |
| 1758 | pt, | |
| 1759 | link.File.LazySymbol.initDecl(.const_data, null, pt.zcu), | |
| 1741 | 1760 | metadata.rdata_atom, |
| 1742 | 1761 | self.rdata_section_index.?, |
| 1743 | 1762 | ) catch |err| return switch (err) { |
| ... | ... | @@ -1858,6 +1877,7 @@ pub fn getDeclVAddr(self: *Coff, decl_index: InternPool.DeclIndex, reloc_info: l |
| 1858 | 1877 | |
| 1859 | 1878 | pub fn lowerAnonDecl( |
| 1860 | 1879 | self: *Coff, |
| 1880 | pt: Zcu.PerThread, | |
| 1861 | 1881 | decl_val: InternPool.Index, |
| 1862 | 1882 | explicit_alignment: InternPool.Alignment, |
| 1863 | 1883 | src_loc: Module.LazySrcLoc, |
| ... | ... | @@ -1866,7 +1886,7 @@ pub fn lowerAnonDecl( |
| 1866 | 1886 | const mod = self.base.comp.module.?; |
| 1867 | 1887 | const ty = Type.fromInterned(mod.intern_pool.typeOf(decl_val)); |
| 1868 | 1888 | const decl_alignment = switch (explicit_alignment) { |
| 1869 | .none => ty.abiAlignment(mod), | |
| 1889 | .none => ty.abiAlignment(pt), | |
| 1870 | 1890 | else => explicit_alignment, |
| 1871 | 1891 | }; |
| 1872 | 1892 | if (self.anon_decls.get(decl_val)) |metadata| { |
| ... | ... | @@ -1881,6 +1901,7 @@ pub fn lowerAnonDecl( |
| 1881 | 1901 | @intFromEnum(decl_val), |
| 1882 | 1902 | }) catch unreachable; |
| 1883 | 1903 | const res = self.lowerConst( |
| 1904 | pt, | |
| 1884 | 1905 | name, |
| 1885 | 1906 | val, |
| 1886 | 1907 | decl_alignment, |
src/link/Coff/lld.zig+3-2| ... | ... | @@ -15,8 +15,9 @@ const Allocator = mem.Allocator; |
| 15 | 15 | |
| 16 | 16 | const Coff = @import("../Coff.zig"); |
| 17 | 17 | const Compilation = @import("../../Compilation.zig"); |
| 18 | const Zcu = @import("../../Zcu.zig"); | |
| 18 | 19 | |
| 19 | pub fn linkWithLLD(self: *Coff, arena: Allocator, prog_node: std.Progress.Node) !void { | |
| 20 | pub fn linkWithLLD(self: *Coff, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) !void { | |
| 20 | 21 | const tracy = trace(@src()); |
| 21 | 22 | defer tracy.end(); |
| 22 | 23 | |
| ... | ... | @@ -29,7 +30,7 @@ pub fn linkWithLLD(self: *Coff, arena: Allocator, prog_node: std.Progress.Node) |
| 29 | 30 | // If there is no Zig code to compile, then we should skip flushing the output file because it |
| 30 | 31 | // will not be part of the linker line anyway. |
| 31 | 32 | const module_obj_path: ?[]const u8 = if (comp.module != null) blk: { |
| 32 | try self.flushModule(arena, prog_node); | |
| 33 | try self.flushModule(arena, tid, prog_node); | |
| 33 | 34 | |
| 34 | 35 | if (fs.path.dirname(full_out_path)) |dirname| { |
| 35 | 36 | break :blk try fs.path.join(arena, &.{ dirname, self.base.zcu_object_sub_path.? }); |
src/link/Dwarf.zig+112-110| ... | ... | @@ -31,7 +31,7 @@ strtab: StringTable = .{}, |
| 31 | 31 | /// They will end up in the DWARF debug_line header as two lists: |
| 32 | 32 | /// * []include_directory |
| 33 | 33 | /// * []file_names |
| 34 | di_files: std.AutoArrayHashMapUnmanaged(*const Module.File, void) = .{}, | |
| 34 | di_files: std.AutoArrayHashMapUnmanaged(*const Zcu.File, void) = .{}, | |
| 35 | 35 | |
| 36 | 36 | global_abbrev_relocs: std.ArrayListUnmanaged(AbbrevRelocation) = .{}, |
| 37 | 37 | |
| ... | ... | @@ -67,7 +67,7 @@ const DbgLineHeader = struct { |
| 67 | 67 | /// Decl's inner Atom is assigned an offset within the DWARF section. |
| 68 | 68 | pub const DeclState = struct { |
| 69 | 69 | dwarf: *Dwarf, |
| 70 | mod: *Module, | |
| 70 | pt: Zcu.PerThread, | |
| 71 | 71 | di_atom_decls: *const AtomTable, |
| 72 | 72 | dbg_line_func: InternPool.Index, |
| 73 | 73 | dbg_line: std.ArrayList(u8), |
| ... | ... | @@ -113,7 +113,7 @@ pub const DeclState = struct { |
| 113 | 113 | .type = ty, |
| 114 | 114 | .offset = undefined, |
| 115 | 115 | }); |
| 116 | log.debug("%{d}: {}", .{ sym_index, ty.fmt(self.mod) }); | |
| 116 | log.debug("%{d}: {}", .{ sym_index, ty.fmt(self.pt) }); | |
| 117 | 117 | try self.abbrev_resolver.putNoClobber(gpa, ty.toIntern(), sym_index); |
| 118 | 118 | break :blk sym_index; |
| 119 | 119 | }; |
| ... | ... | @@ -128,16 +128,17 @@ pub const DeclState = struct { |
| 128 | 128 | |
| 129 | 129 | fn addDbgInfoType( |
| 130 | 130 | self: *DeclState, |
| 131 | mod: *Module, | |
| 131 | pt: Zcu.PerThread, | |
| 132 | 132 | atom_index: Atom.Index, |
| 133 | 133 | ty: Type, |
| 134 | 134 | ) error{OutOfMemory}!void { |
| 135 | const zcu = pt.zcu; | |
| 135 | 136 | const dbg_info_buffer = &self.dbg_info; |
| 136 | const target = mod.getTarget(); | |
| 137 | const target = zcu.getTarget(); | |
| 137 | 138 | const target_endian = target.cpu.arch.endian(); |
| 138 | const ip = &mod.intern_pool; | |
| 139 | const ip = &zcu.intern_pool; | |
| 139 | 140 | |
| 140 | switch (ty.zigTypeTag(mod)) { | |
| 141 | switch (ty.zigTypeTag(zcu)) { | |
| 141 | 142 | .NoReturn => unreachable, |
| 142 | 143 | .Void => { |
| 143 | 144 | try dbg_info_buffer.append(@intFromEnum(AbbrevCode.zero_bit_type)); |
| ... | ... | @@ -148,12 +149,12 @@ pub const DeclState = struct { |
| 148 | 149 | // DW.AT.encoding, DW.FORM.data1 |
| 149 | 150 | dbg_info_buffer.appendAssumeCapacity(DW.ATE.boolean); |
| 150 | 151 | // DW.AT.byte_size, DW.FORM.udata |
| 151 | try leb128.writeUleb128(dbg_info_buffer.writer(), ty.abiSize(mod)); | |
| 152 | try leb128.writeUleb128(dbg_info_buffer.writer(), ty.abiSize(pt)); | |
| 152 | 153 | // DW.AT.name, DW.FORM.string |
| 153 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(mod)}); | |
| 154 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(pt)}); | |
| 154 | 155 | }, |
| 155 | 156 | .Int => { |
| 156 | const info = ty.intInfo(mod); | |
| 157 | const info = ty.intInfo(zcu); | |
| 157 | 158 | try dbg_info_buffer.ensureUnusedCapacity(12); |
| 158 | 159 | dbg_info_buffer.appendAssumeCapacity(@intFromEnum(AbbrevCode.base_type)); |
| 159 | 160 | // DW.AT.encoding, DW.FORM.data1 |
| ... | ... | @@ -162,30 +163,30 @@ pub const DeclState = struct { |
| 162 | 163 | .unsigned => DW.ATE.unsigned, |
| 163 | 164 | }); |
| 164 | 165 | // DW.AT.byte_size, DW.FORM.udata |
| 165 | try leb128.writeUleb128(dbg_info_buffer.writer(), ty.abiSize(mod)); | |
| 166 | try leb128.writeUleb128(dbg_info_buffer.writer(), ty.abiSize(pt)); | |
| 166 | 167 | // DW.AT.name, DW.FORM.string |
| 167 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(mod)}); | |
| 168 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(pt)}); | |
| 168 | 169 | }, |
| 169 | 170 | .Optional => { |
| 170 | if (ty.isPtrLikeOptional(mod)) { | |
| 171 | if (ty.isPtrLikeOptional(zcu)) { | |
| 171 | 172 | try dbg_info_buffer.ensureUnusedCapacity(12); |
| 172 | 173 | dbg_info_buffer.appendAssumeCapacity(@intFromEnum(AbbrevCode.base_type)); |
| 173 | 174 | // DW.AT.encoding, DW.FORM.data1 |
| 174 | 175 | dbg_info_buffer.appendAssumeCapacity(DW.ATE.address); |
| 175 | 176 | // DW.AT.byte_size, DW.FORM.udata |
| 176 | try leb128.writeUleb128(dbg_info_buffer.writer(), ty.abiSize(mod)); | |
| 177 | try leb128.writeUleb128(dbg_info_buffer.writer(), ty.abiSize(pt)); | |
| 177 | 178 | // DW.AT.name, DW.FORM.string |
| 178 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(mod)}); | |
| 179 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(pt)}); | |
| 179 | 180 | } else { |
| 180 | 181 | // Non-pointer optionals are structs: struct { .maybe = *, .val = * } |
| 181 | const payload_ty = ty.optionalChild(mod); | |
| 182 | const payload_ty = ty.optionalChild(zcu); | |
| 182 | 183 | // DW.AT.structure_type |
| 183 | 184 | try dbg_info_buffer.append(@intFromEnum(AbbrevCode.struct_type)); |
| 184 | 185 | // DW.AT.byte_size, DW.FORM.udata |
| 185 | const abi_size = ty.abiSize(mod); | |
| 186 | const abi_size = ty.abiSize(pt); | |
| 186 | 187 | try leb128.writeUleb128(dbg_info_buffer.writer(), abi_size); |
| 187 | 188 | // DW.AT.name, DW.FORM.string |
| 188 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(mod)}); | |
| 189 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(pt)}); | |
| 189 | 190 | // DW.AT.member |
| 190 | 191 | try dbg_info_buffer.ensureUnusedCapacity(21); |
| 191 | 192 | dbg_info_buffer.appendAssumeCapacity(@intFromEnum(AbbrevCode.struct_member)); |
| ... | ... | @@ -208,14 +209,14 @@ pub const DeclState = struct { |
| 208 | 209 | dbg_info_buffer.appendNTimesAssumeCapacity(0, 4); |
| 209 | 210 | try self.addTypeRelocGlobal(atom_index, payload_ty, @intCast(index)); |
| 210 | 211 | // DW.AT.data_member_location, DW.FORM.udata |
| 211 | const offset = abi_size - payload_ty.abiSize(mod); | |
| 212 | const offset = abi_size - payload_ty.abiSize(pt); | |
| 212 | 213 | try leb128.writeUleb128(dbg_info_buffer.writer(), offset); |
| 213 | 214 | // DW.AT.structure_type delimit children |
| 214 | 215 | try dbg_info_buffer.append(0); |
| 215 | 216 | } |
| 216 | 217 | }, |
| 217 | 218 | .Pointer => { |
| 218 | if (ty.isSlice(mod)) { | |
| 219 | if (ty.isSlice(zcu)) { | |
| 219 | 220 | // Slices are structs: struct { .ptr = *, .len = N } |
| 220 | 221 | const ptr_bits = target.ptrBitWidth(); |
| 221 | 222 | const ptr_bytes: u8 = @intCast(@divExact(ptr_bits, 8)); |
| ... | ... | @@ -223,9 +224,9 @@ pub const DeclState = struct { |
| 223 | 224 | try dbg_info_buffer.ensureUnusedCapacity(2); |
| 224 | 225 | dbg_info_buffer.appendAssumeCapacity(@intFromEnum(AbbrevCode.struct_type)); |
| 225 | 226 | // DW.AT.byte_size, DW.FORM.udata |
| 226 | try leb128.writeUleb128(dbg_info_buffer.writer(), ty.abiSize(mod)); | |
| 227 | try leb128.writeUleb128(dbg_info_buffer.writer(), ty.abiSize(pt)); | |
| 227 | 228 | // DW.AT.name, DW.FORM.string |
| 228 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(mod)}); | |
| 229 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(pt)}); | |
| 229 | 230 | // DW.AT.member |
| 230 | 231 | try dbg_info_buffer.ensureUnusedCapacity(21); |
| 231 | 232 | dbg_info_buffer.appendAssumeCapacity(@intFromEnum(AbbrevCode.struct_member)); |
| ... | ... | @@ -235,7 +236,7 @@ pub const DeclState = struct { |
| 235 | 236 | // DW.AT.type, DW.FORM.ref4 |
| 236 | 237 | var index = dbg_info_buffer.items.len; |
| 237 | 238 | dbg_info_buffer.appendNTimesAssumeCapacity(0, 4); |
| 238 | const ptr_ty = ty.slicePtrFieldType(mod); | |
| 239 | const ptr_ty = ty.slicePtrFieldType(zcu); | |
| 239 | 240 | try self.addTypeRelocGlobal(atom_index, ptr_ty, @intCast(index)); |
| 240 | 241 | // DW.AT.data_member_location, DW.FORM.udata |
| 241 | 242 | dbg_info_buffer.appendAssumeCapacity(0); |
| ... | ... | @@ -258,19 +259,19 @@ pub const DeclState = struct { |
| 258 | 259 | // DW.AT.type, DW.FORM.ref4 |
| 259 | 260 | const index = dbg_info_buffer.items.len; |
| 260 | 261 | dbg_info_buffer.appendNTimesAssumeCapacity(0, 4); |
| 261 | try self.addTypeRelocGlobal(atom_index, ty.childType(mod), @intCast(index)); | |
| 262 | try self.addTypeRelocGlobal(atom_index, ty.childType(zcu), @intCast(index)); | |
| 262 | 263 | } |
| 263 | 264 | }, |
| 264 | 265 | .Array => { |
| 265 | 266 | // DW.AT.array_type |
| 266 | 267 | try dbg_info_buffer.append(@intFromEnum(AbbrevCode.array_type)); |
| 267 | 268 | // DW.AT.name, DW.FORM.string |
| 268 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(mod)}); | |
| 269 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(pt)}); | |
| 269 | 270 | // DW.AT.type, DW.FORM.ref4 |
| 270 | 271 | var index = dbg_info_buffer.items.len; |
| 271 | 272 | try dbg_info_buffer.ensureUnusedCapacity(9); |
| 272 | 273 | dbg_info_buffer.appendNTimesAssumeCapacity(0, 4); |
| 273 | try self.addTypeRelocGlobal(atom_index, ty.childType(mod), @intCast(index)); | |
| 274 | try self.addTypeRelocGlobal(atom_index, ty.childType(zcu), @intCast(index)); | |
| 274 | 275 | // DW.AT.subrange_type |
| 275 | 276 | dbg_info_buffer.appendAssumeCapacity(@intFromEnum(AbbrevCode.array_dim)); |
| 276 | 277 | // DW.AT.type, DW.FORM.ref4 |
| ... | ... | @@ -278,7 +279,7 @@ pub const DeclState = struct { |
| 278 | 279 | dbg_info_buffer.appendNTimesAssumeCapacity(0, 4); |
| 279 | 280 | try self.addTypeRelocGlobal(atom_index, Type.usize, @intCast(index)); |
| 280 | 281 | // DW.AT.count, DW.FORM.udata |
| 281 | const len = ty.arrayLenIncludingSentinel(mod); | |
| 282 | const len = ty.arrayLenIncludingSentinel(pt.zcu); | |
| 282 | 283 | try leb128.writeUleb128(dbg_info_buffer.writer(), len); |
| 283 | 284 | // DW.AT.array_type delimit children |
| 284 | 285 | try dbg_info_buffer.append(0); |
| ... | ... | @@ -287,13 +288,13 @@ pub const DeclState = struct { |
| 287 | 288 | // DW.AT.structure_type |
| 288 | 289 | try dbg_info_buffer.append(@intFromEnum(AbbrevCode.struct_type)); |
| 289 | 290 | // DW.AT.byte_size, DW.FORM.udata |
| 290 | try leb128.writeUleb128(dbg_info_buffer.writer(), ty.abiSize(mod)); | |
| 291 | try leb128.writeUleb128(dbg_info_buffer.writer(), ty.abiSize(pt)); | |
| 291 | 292 | |
| 292 | 293 | blk: { |
| 293 | 294 | switch (ip.indexToKey(ty.ip_index)) { |
| 294 | 295 | .anon_struct_type => |fields| { |
| 295 | 296 | // DW.AT.name, DW.FORM.string |
| 296 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(mod)}); | |
| 297 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(pt)}); | |
| 297 | 298 | |
| 298 | 299 | for (fields.types.get(ip), 0..) |field_ty, field_index| { |
| 299 | 300 | // DW.AT.member |
| ... | ... | @@ -305,14 +306,14 @@ pub const DeclState = struct { |
| 305 | 306 | try dbg_info_buffer.appendNTimes(0, 4); |
| 306 | 307 | try self.addTypeRelocGlobal(atom_index, Type.fromInterned(field_ty), @intCast(index)); |
| 307 | 308 | // DW.AT.data_member_location, DW.FORM.udata |
| 308 | const field_off = ty.structFieldOffset(field_index, mod); | |
| 309 | const field_off = ty.structFieldOffset(field_index, pt); | |
| 309 | 310 | try leb128.writeUleb128(dbg_info_buffer.writer(), field_off); |
| 310 | 311 | } |
| 311 | 312 | }, |
| 312 | 313 | .struct_type => { |
| 313 | 314 | const struct_type = ip.loadStructType(ty.toIntern()); |
| 314 | 315 | // DW.AT.name, DW.FORM.string |
| 315 | try ty.print(dbg_info_buffer.writer(), mod); | |
| 316 | try ty.print(dbg_info_buffer.writer(), pt); | |
| 316 | 317 | try dbg_info_buffer.append(0); |
| 317 | 318 | |
| 318 | 319 | if (struct_type.layout == .@"packed") { |
| ... | ... | @@ -322,7 +323,7 @@ pub const DeclState = struct { |
| 322 | 323 | |
| 323 | 324 | if (struct_type.isTuple(ip)) { |
| 324 | 325 | for (struct_type.field_types.get(ip), struct_type.offsets.get(ip), 0..) |field_ty, field_off, field_index| { |
| 325 | if (!Type.fromInterned(field_ty).hasRuntimeBits(mod)) continue; | |
| 326 | if (!Type.fromInterned(field_ty).hasRuntimeBits(pt)) continue; | |
| 326 | 327 | // DW.AT.member |
| 327 | 328 | try dbg_info_buffer.append(@intFromEnum(AbbrevCode.struct_member)); |
| 328 | 329 | // DW.AT.name, DW.FORM.string |
| ... | ... | @@ -340,7 +341,7 @@ pub const DeclState = struct { |
| 340 | 341 | struct_type.field_types.get(ip), |
| 341 | 342 | struct_type.offsets.get(ip), |
| 342 | 343 | ) |field_name, field_ty, field_off| { |
| 343 | if (!Type.fromInterned(field_ty).hasRuntimeBits(mod)) continue; | |
| 344 | if (!Type.fromInterned(field_ty).hasRuntimeBits(pt)) continue; | |
| 344 | 345 | const field_name_slice = field_name.toSlice(ip); |
| 345 | 346 | // DW.AT.member |
| 346 | 347 | try dbg_info_buffer.ensureUnusedCapacity(field_name_slice.len + 2); |
| ... | ... | @@ -367,9 +368,9 @@ pub const DeclState = struct { |
| 367 | 368 | // DW.AT.enumeration_type |
| 368 | 369 | try dbg_info_buffer.append(@intFromEnum(AbbrevCode.enum_type)); |
| 369 | 370 | // DW.AT.byte_size, DW.FORM.udata |
| 370 | try leb128.writeUleb128(dbg_info_buffer.writer(), ty.abiSize(mod)); | |
| 371 | try leb128.writeUleb128(dbg_info_buffer.writer(), ty.abiSize(pt)); | |
| 371 | 372 | // DW.AT.name, DW.FORM.string |
| 372 | try ty.print(dbg_info_buffer.writer(), mod); | |
| 373 | try ty.print(dbg_info_buffer.writer(), pt); | |
| 373 | 374 | try dbg_info_buffer.append(0); |
| 374 | 375 | |
| 375 | 376 | const enum_type = ip.loadEnumType(ty.ip_index); |
| ... | ... | @@ -386,8 +387,8 @@ pub const DeclState = struct { |
| 386 | 387 | const value = enum_type.values.get(ip)[field_i]; |
| 387 | 388 | // TODO do not assume a 64bit enum value - could be bigger. |
| 388 | 389 | // See https://github.com/ziglang/zig/issues/645 |
| 389 | const field_int_val = try Value.fromInterned(value).intFromEnum(ty, mod); | |
| 390 | break :value @bitCast(field_int_val.toSignedInt(mod)); | |
| 390 | const field_int_val = try Value.fromInterned(value).intFromEnum(ty, pt); | |
| 391 | break :value @bitCast(field_int_val.toSignedInt(pt)); | |
| 391 | 392 | }; |
| 392 | 393 | mem.writeInt(u64, dbg_info_buffer.addManyAsArrayAssumeCapacity(8), value, target_endian); |
| 393 | 394 | } |
| ... | ... | @@ -396,8 +397,8 @@ pub const DeclState = struct { |
| 396 | 397 | try dbg_info_buffer.append(0); |
| 397 | 398 | }, |
| 398 | 399 | .Union => { |
| 399 | const union_obj = mod.typeToUnion(ty).?; | |
| 400 | const layout = mod.getUnionLayout(union_obj); | |
| 400 | const union_obj = zcu.typeToUnion(ty).?; | |
| 401 | const layout = pt.getUnionLayout(union_obj); | |
| 401 | 402 | const payload_offset = if (layout.tag_align.compare(.gte, layout.payload_align)) layout.tag_size else 0; |
| 402 | 403 | const tag_offset = if (layout.tag_align.compare(.gte, layout.payload_align)) 0 else layout.payload_size; |
| 403 | 404 | // TODO this is temporary to match current state of unions in Zig - we don't yet have |
| ... | ... | @@ -410,7 +411,7 @@ pub const DeclState = struct { |
| 410 | 411 | // DW.AT.byte_size, DW.FORM.udata |
| 411 | 412 | try leb128.writeUleb128(dbg_info_buffer.writer(), layout.abi_size); |
| 412 | 413 | // DW.AT.name, DW.FORM.string |
| 413 | try ty.print(dbg_info_buffer.writer(), mod); | |
| 414 | try ty.print(dbg_info_buffer.writer(), pt); | |
| 414 | 415 | try dbg_info_buffer.append(0); |
| 415 | 416 | |
| 416 | 417 | // DW.AT.member |
| ... | ... | @@ -435,12 +436,12 @@ pub const DeclState = struct { |
| 435 | 436 | if (is_tagged) { |
| 436 | 437 | try dbg_info_buffer.writer().print("AnonUnion\x00", .{}); |
| 437 | 438 | } else { |
| 438 | try ty.print(dbg_info_buffer.writer(), mod); | |
| 439 | try ty.print(dbg_info_buffer.writer(), pt); | |
| 439 | 440 | try dbg_info_buffer.append(0); |
| 440 | 441 | } |
| 441 | 442 | |
| 442 | 443 | for (union_obj.field_types.get(ip), union_obj.loadTagType(ip).names.get(ip)) |field_ty, field_name| { |
| 443 | if (!Type.fromInterned(field_ty).hasRuntimeBits(mod)) continue; | |
| 444 | if (!Type.fromInterned(field_ty).hasRuntimeBits(pt)) continue; | |
| 444 | 445 | const field_name_slice = field_name.toSlice(ip); |
| 445 | 446 | // DW.AT.member |
| 446 | 447 | try dbg_info_buffer.append(@intFromEnum(AbbrevCode.struct_member)); |
| ... | ... | @@ -474,25 +475,25 @@ pub const DeclState = struct { |
| 474 | 475 | try dbg_info_buffer.append(0); |
| 475 | 476 | } |
| 476 | 477 | }, |
| 477 | .ErrorSet => try addDbgInfoErrorSet(mod, ty, target, &self.dbg_info), | |
| 478 | .ErrorSet => try addDbgInfoErrorSet(pt, ty, target, &self.dbg_info), | |
| 478 | 479 | .ErrorUnion => { |
| 479 | const error_ty = ty.errorUnionSet(mod); | |
| 480 | const payload_ty = ty.errorUnionPayload(mod); | |
| 481 | const payload_align = if (payload_ty.isNoReturn(mod)) .none else payload_ty.abiAlignment(mod); | |
| 482 | const error_align = Type.anyerror.abiAlignment(mod); | |
| 483 | const abi_size = ty.abiSize(mod); | |
| 484 | const payload_off = if (error_align.compare(.gte, payload_align)) Type.anyerror.abiSize(mod) else 0; | |
| 485 | const error_off = if (error_align.compare(.gte, payload_align)) 0 else payload_ty.abiSize(mod); | |
| 480 | const error_ty = ty.errorUnionSet(zcu); | |
| 481 | const payload_ty = ty.errorUnionPayload(zcu); | |
| 482 | const payload_align = if (payload_ty.isNoReturn(zcu)) .none else payload_ty.abiAlignment(pt); | |
| 483 | const error_align = Type.anyerror.abiAlignment(pt); | |
| 484 | const abi_size = ty.abiSize(pt); | |
| 485 | const payload_off = if (error_align.compare(.gte, payload_align)) Type.anyerror.abiSize(pt) else 0; | |
| 486 | const error_off = if (error_align.compare(.gte, payload_align)) 0 else payload_ty.abiSize(pt); | |
| 486 | 487 | |
| 487 | 488 | // DW.AT.structure_type |
| 488 | 489 | try dbg_info_buffer.append(@intFromEnum(AbbrevCode.struct_type)); |
| 489 | 490 | // DW.AT.byte_size, DW.FORM.udata |
| 490 | 491 | try leb128.writeUleb128(dbg_info_buffer.writer(), abi_size); |
| 491 | 492 | // DW.AT.name, DW.FORM.string |
| 492 | try ty.print(dbg_info_buffer.writer(), mod); | |
| 493 | try ty.print(dbg_info_buffer.writer(), pt); | |
| 493 | 494 | try dbg_info_buffer.append(0); |
| 494 | 495 | |
| 495 | if (!payload_ty.isNoReturn(mod)) { | |
| 496 | if (!payload_ty.isNoReturn(zcu)) { | |
| 496 | 497 | // DW.AT.member |
| 497 | 498 | try dbg_info_buffer.ensureUnusedCapacity(11); |
| 498 | 499 | dbg_info_buffer.appendAssumeCapacity(@intFromEnum(AbbrevCode.struct_member)); |
| ... | ... | @@ -526,7 +527,7 @@ pub const DeclState = struct { |
| 526 | 527 | try dbg_info_buffer.append(0); |
| 527 | 528 | }, |
| 528 | 529 | else => { |
| 529 | log.debug("TODO implement .debug_info for type '{}'", .{ty.fmt(self.mod)}); | |
| 530 | log.debug("TODO implement .debug_info for type '{}'", .{ty.fmt(pt)}); | |
| 530 | 531 | try dbg_info_buffer.append(@intFromEnum(AbbrevCode.zero_bit_type)); |
| 531 | 532 | }, |
| 532 | 533 | } |
| ... | ... | @@ -555,6 +556,7 @@ pub const DeclState = struct { |
| 555 | 556 | owner_decl: InternPool.DeclIndex, |
| 556 | 557 | loc: DbgInfoLoc, |
| 557 | 558 | ) error{OutOfMemory}!void { |
| 559 | const pt = self.pt; | |
| 558 | 560 | const dbg_info = &self.dbg_info; |
| 559 | 561 | const atom_index = self.di_atom_decls.get(owner_decl).?; |
| 560 | 562 | const name_with_null = name.ptr[0 .. name.len + 1]; |
| ... | ... | @@ -580,9 +582,9 @@ pub const DeclState = struct { |
| 580 | 582 | } |
| 581 | 583 | }, |
| 582 | 584 | .register_pair => |regs| { |
| 583 | const reg_bits = self.mod.getTarget().ptrBitWidth(); | |
| 585 | const reg_bits = pt.zcu.getTarget().ptrBitWidth(); | |
| 584 | 586 | const reg_bytes: u8 = @intCast(@divExact(reg_bits, 8)); |
| 585 | const abi_size = ty.abiSize(self.mod); | |
| 587 | const abi_size = ty.abiSize(pt); | |
| 586 | 588 | try dbg_info.ensureUnusedCapacity(10); |
| 587 | 589 | dbg_info.appendAssumeCapacity(@intFromEnum(AbbrevCode.parameter)); |
| 588 | 590 | // DW.AT.location, DW.FORM.exprloc |
| ... | ... | @@ -675,10 +677,10 @@ pub const DeclState = struct { |
| 675 | 677 | const name_with_null = name.ptr[0 .. name.len + 1]; |
| 676 | 678 | try dbg_info.append(@intFromEnum(AbbrevCode.variable)); |
| 677 | 679 | const gpa = self.dwarf.allocator; |
| 678 | const mod = self.mod; | |
| 679 | const target = mod.getTarget(); | |
| 680 | const pt = self.pt; | |
| 681 | const target = pt.zcu.getTarget(); | |
| 680 | 682 | const endian = target.cpu.arch.endian(); |
| 681 | const child_ty = if (is_ptr) ty.childType(mod) else ty; | |
| 683 | const child_ty = if (is_ptr) ty.childType(pt.zcu) else ty; | |
| 682 | 684 | |
| 683 | 685 | switch (loc) { |
| 684 | 686 | .register => |reg| { |
| ... | ... | @@ -701,9 +703,9 @@ pub const DeclState = struct { |
| 701 | 703 | }, |
| 702 | 704 | |
| 703 | 705 | .register_pair => |regs| { |
| 704 | const reg_bits = self.mod.getTarget().ptrBitWidth(); | |
| 706 | const reg_bits = pt.zcu.getTarget().ptrBitWidth(); | |
| 705 | 707 | const reg_bytes: u8 = @intCast(@divExact(reg_bits, 8)); |
| 706 | const abi_size = child_ty.abiSize(self.mod); | |
| 708 | const abi_size = child_ty.abiSize(pt); | |
| 707 | 709 | try dbg_info.ensureUnusedCapacity(9); |
| 708 | 710 | // DW.AT.location, DW.FORM.exprloc |
| 709 | 711 | var expr_len = std.io.countingWriter(std.io.null_writer); |
| ... | ... | @@ -829,9 +831,9 @@ pub const DeclState = struct { |
| 829 | 831 | const fixup = dbg_info.items.len; |
| 830 | 832 | dbg_info.appendSliceAssumeCapacity(&[2]u8{ // DW.AT.location, DW.FORM.exprloc |
| 831 | 833 | 1, |
| 832 | if (child_ty.isSignedInt(mod)) DW.OP.consts else DW.OP.constu, | |
| 834 | if (child_ty.isSignedInt(pt.zcu)) DW.OP.consts else DW.OP.constu, | |
| 833 | 835 | }); |
| 834 | if (child_ty.isSignedInt(mod)) { | |
| 836 | if (child_ty.isSignedInt(pt.zcu)) { | |
| 835 | 837 | try leb128.writeIleb128(dbg_info.writer(), @as(i64, @bitCast(x))); |
| 836 | 838 | } else { |
| 837 | 839 | try leb128.writeUleb128(dbg_info.writer(), x); |
| ... | ... | @@ -844,7 +846,7 @@ pub const DeclState = struct { |
| 844 | 846 | // DW.AT.location, DW.FORM.exprloc |
| 845 | 847 | // uleb128(exprloc_len) |
| 846 | 848 | // DW.OP.implicit_value uleb128(len_of_bytes) bytes |
| 847 | const abi_size: u32 = @intCast(child_ty.abiSize(mod)); | |
| 849 | const abi_size: u32 = @intCast(child_ty.abiSize(self.pt)); | |
| 848 | 850 | var implicit_value_len = std.ArrayList(u8).init(gpa); |
| 849 | 851 | defer implicit_value_len.deinit(); |
| 850 | 852 | try leb128.writeUleb128(implicit_value_len.writer(), abi_size); |
| ... | ... | @@ -934,22 +936,23 @@ pub const DeclState = struct { |
| 934 | 936 | } |
| 935 | 937 | |
| 936 | 938 | pub fn setInlineFunc(self: *DeclState, func: InternPool.Index) error{OutOfMemory}!void { |
| 939 | const zcu = self.pt.zcu; | |
| 937 | 940 | if (self.dbg_line_func == func) return; |
| 938 | 941 | |
| 939 | 942 | try self.dbg_line.ensureUnusedCapacity((1 + 4) + (1 + 5)); |
| 940 | 943 | |
| 941 | const old_func_info = self.mod.funcInfo(self.dbg_line_func); | |
| 942 | const new_func_info = self.mod.funcInfo(func); | |
| 944 | const old_func_info = zcu.funcInfo(self.dbg_line_func); | |
| 945 | const new_func_info = zcu.funcInfo(func); | |
| 943 | 946 | |
| 944 | const old_file = try self.dwarf.addDIFile(self.mod, old_func_info.owner_decl); | |
| 945 | const new_file = try self.dwarf.addDIFile(self.mod, new_func_info.owner_decl); | |
| 947 | const old_file = try self.dwarf.addDIFile(zcu, old_func_info.owner_decl); | |
| 948 | const new_file = try self.dwarf.addDIFile(zcu, new_func_info.owner_decl); | |
| 946 | 949 | if (old_file != new_file) { |
| 947 | 950 | self.dbg_line.appendAssumeCapacity(DW.LNS.set_file); |
| 948 | 951 | leb128.writeUnsignedFixed(4, self.dbg_line.addManyAsArrayAssumeCapacity(4), new_file); |
| 949 | 952 | } |
| 950 | 953 | |
| 951 | const old_src_line: i33 = self.mod.declPtr(old_func_info.owner_decl).navSrcLine(self.mod); | |
| 952 | const new_src_line: i33 = self.mod.declPtr(new_func_info.owner_decl).navSrcLine(self.mod); | |
| 954 | const old_src_line: i33 = zcu.declPtr(old_func_info.owner_decl).navSrcLine(zcu); | |
| 955 | const new_src_line: i33 = zcu.declPtr(new_func_info.owner_decl).navSrcLine(zcu); | |
| 953 | 956 | if (new_src_line != old_src_line) { |
| 954 | 957 | self.dbg_line.appendAssumeCapacity(DW.LNS.advance_line); |
| 955 | 958 | leb128.writeSignedFixed(5, self.dbg_line.addManyAsArrayAssumeCapacity(5), new_src_line - old_src_line); |
| ... | ... | @@ -1074,19 +1077,19 @@ pub fn deinit(self: *Dwarf) void { |
| 1074 | 1077 | |
| 1075 | 1078 | /// Initializes Decl's state and its matching output buffers. |
| 1076 | 1079 | /// Call this before `commitDeclState`. |
| 1077 | pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: InternPool.DeclIndex) !DeclState { | |
| 1080 | pub fn initDeclState(self: *Dwarf, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex) !DeclState { | |
| 1078 | 1081 | const tracy = trace(@src()); |
| 1079 | 1082 | defer tracy.end(); |
| 1080 | 1083 | |
| 1081 | const decl = mod.declPtr(decl_index); | |
| 1082 | const decl_linkage_name = try decl.fullyQualifiedName(mod); | |
| 1084 | const decl = pt.zcu.declPtr(decl_index); | |
| 1085 | const decl_linkage_name = try decl.fullyQualifiedName(pt.zcu); | |
| 1083 | 1086 | |
| 1084 | log.debug("initDeclState {}{*}", .{ decl_linkage_name.fmt(&mod.intern_pool), decl }); | |
| 1087 | log.debug("initDeclState {}{*}", .{ decl_linkage_name.fmt(&pt.zcu.intern_pool), decl }); | |
| 1085 | 1088 | |
| 1086 | 1089 | const gpa = self.allocator; |
| 1087 | 1090 | var decl_state: DeclState = .{ |
| 1088 | 1091 | .dwarf = self, |
| 1089 | .mod = mod, | |
| 1092 | .pt = pt, | |
| 1090 | 1093 | .di_atom_decls = &self.di_atom_decls, |
| 1091 | 1094 | .dbg_line_func = undefined, |
| 1092 | 1095 | .dbg_line = std.ArrayList(u8).init(gpa), |
| ... | ... | @@ -1105,7 +1108,7 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: InternPool.DeclInde |
| 1105 | 1108 | |
| 1106 | 1109 | assert(decl.has_tv); |
| 1107 | 1110 | |
| 1108 | switch (decl.typeOf(mod).zigTypeTag(mod)) { | |
| 1111 | switch (decl.typeOf(pt.zcu).zigTypeTag(pt.zcu)) { | |
| 1109 | 1112 | .Fn => { |
| 1110 | 1113 | _ = try self.getOrCreateAtomForDecl(.src_fn, decl_index); |
| 1111 | 1114 | |
| ... | ... | @@ -1114,13 +1117,13 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: InternPool.DeclInde |
| 1114 | 1117 | try dbg_line_buffer.ensureTotalCapacity((3 + ptr_width_bytes) + (1 + 4) + (1 + 4) + (1 + 5) + 1); |
| 1115 | 1118 | |
| 1116 | 1119 | decl_state.dbg_line_func = decl.val.toIntern(); |
| 1117 | const func = decl.val.getFunction(mod).?; | |
| 1120 | const func = decl.val.getFunction(pt.zcu).?; | |
| 1118 | 1121 | log.debug("decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d}", .{ |
| 1119 | decl.navSrcLine(mod), | |
| 1122 | decl.navSrcLine(pt.zcu), | |
| 1120 | 1123 | func.lbrace_line, |
| 1121 | 1124 | func.rbrace_line, |
| 1122 | 1125 | }); |
| 1123 | const line: u28 = @intCast(decl.navSrcLine(mod) + func.lbrace_line); | |
| 1126 | const line: u28 = @intCast(decl.navSrcLine(pt.zcu) + func.lbrace_line); | |
| 1124 | 1127 | |
| 1125 | 1128 | dbg_line_buffer.appendSliceAssumeCapacity(&.{ |
| 1126 | 1129 | DW.LNS.extended_op, |
| ... | ... | @@ -1142,7 +1145,7 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: InternPool.DeclInde |
| 1142 | 1145 | assert(self.getRelocDbgFileIndex() == dbg_line_buffer.items.len); |
| 1143 | 1146 | // Once we support more than one source file, this will have the ability to be more |
| 1144 | 1147 | // than one possible value. |
| 1145 | const file_index = try self.addDIFile(mod, decl_index); | |
| 1148 | const file_index = try self.addDIFile(pt.zcu, decl_index); | |
| 1146 | 1149 | leb128.writeUnsignedFixed(4, dbg_line_buffer.addManyAsArrayAssumeCapacity(4), file_index); |
| 1147 | 1150 | |
| 1148 | 1151 | dbg_line_buffer.appendAssumeCapacity(DW.LNS.set_column); |
| ... | ... | @@ -1153,13 +1156,13 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: InternPool.DeclInde |
| 1153 | 1156 | dbg_line_buffer.appendAssumeCapacity(DW.LNS.copy); |
| 1154 | 1157 | |
| 1155 | 1158 | // .debug_info subprogram |
| 1156 | const decl_name_slice = decl.name.toSlice(&mod.intern_pool); | |
| 1157 | const decl_linkage_name_slice = decl_linkage_name.toSlice(&mod.intern_pool); | |
| 1159 | const decl_name_slice = decl.name.toSlice(&pt.zcu.intern_pool); | |
| 1160 | const decl_linkage_name_slice = decl_linkage_name.toSlice(&pt.zcu.intern_pool); | |
| 1158 | 1161 | try dbg_info_buffer.ensureUnusedCapacity(1 + ptr_width_bytes + 4 + 4 + |
| 1159 | 1162 | (decl_name_slice.len + 1) + (decl_linkage_name_slice.len + 1)); |
| 1160 | 1163 | |
| 1161 | const fn_ret_type = decl.typeOf(mod).fnReturnType(mod); | |
| 1162 | const fn_ret_has_bits = fn_ret_type.hasRuntimeBits(mod); | |
| 1164 | const fn_ret_type = decl.typeOf(pt.zcu).fnReturnType(pt.zcu); | |
| 1165 | const fn_ret_has_bits = fn_ret_type.hasRuntimeBits(pt); | |
| 1163 | 1166 | dbg_info_buffer.appendAssumeCapacity(@intFromEnum( |
| 1164 | 1167 | @as(AbbrevCode, if (fn_ret_has_bits) .subprogram else .subprogram_retvoid), |
| 1165 | 1168 | )); |
| ... | ... | @@ -1191,7 +1194,7 @@ pub fn initDeclState(self: *Dwarf, mod: *Module, decl_index: InternPool.DeclInde |
| 1191 | 1194 | |
| 1192 | 1195 | pub fn commitDeclState( |
| 1193 | 1196 | self: *Dwarf, |
| 1194 | zcu: *Module, | |
| 1197 | pt: Zcu.PerThread, | |
| 1195 | 1198 | decl_index: InternPool.DeclIndex, |
| 1196 | 1199 | sym_addr: u64, |
| 1197 | 1200 | sym_size: u64, |
| ... | ... | @@ -1201,6 +1204,7 @@ pub fn commitDeclState( |
| 1201 | 1204 | defer tracy.end(); |
| 1202 | 1205 | |
| 1203 | 1206 | const gpa = self.allocator; |
| 1207 | const zcu = pt.zcu; | |
| 1204 | 1208 | const decl = zcu.declPtr(decl_index); |
| 1205 | 1209 | const ip = &zcu.intern_pool; |
| 1206 | 1210 | const namespace = zcu.namespacePtr(decl.src_namespace); |
| ... | ... | @@ -1432,7 +1436,7 @@ pub fn commitDeclState( |
| 1432 | 1436 | if (ip.isErrorSetType(ty.toIntern())) continue; |
| 1433 | 1437 | |
| 1434 | 1438 | symbol.offset = @intCast(dbg_info_buffer.items.len); |
| 1435 | try decl_state.addDbgInfoType(zcu, di_atom_index, ty); | |
| 1439 | try decl_state.addDbgInfoType(pt, di_atom_index, ty); | |
| 1436 | 1440 | } |
| 1437 | 1441 | } |
| 1438 | 1442 | |
| ... | ... | @@ -1457,7 +1461,7 @@ pub fn commitDeclState( |
| 1457 | 1461 | reloc.offset, |
| 1458 | 1462 | value, |
| 1459 | 1463 | reloc_target, |
| 1460 | ty.fmt(zcu), | |
| 1464 | ty.fmt(pt), | |
| 1461 | 1465 | }); |
| 1462 | 1466 | mem.writeInt( |
| 1463 | 1467 | u32, |
| ... | ... | @@ -1691,7 +1695,7 @@ fn writeDeclDebugInfo(self: *Dwarf, atom_index: Atom.Index, dbg_info_buf: []cons |
| 1691 | 1695 | } |
| 1692 | 1696 | } |
| 1693 | 1697 | |
| 1694 | pub fn updateDeclLineNumber(self: *Dwarf, mod: *Module, decl_index: InternPool.DeclIndex) !void { | |
| 1698 | pub fn updateDeclLineNumber(self: *Dwarf, zcu: *Zcu, decl_index: InternPool.DeclIndex) !void { | |
| 1695 | 1699 | const tracy = trace(@src()); |
| 1696 | 1700 | defer tracy.end(); |
| 1697 | 1701 | |
| ... | ... | @@ -1699,14 +1703,14 @@ pub fn updateDeclLineNumber(self: *Dwarf, mod: *Module, decl_index: InternPool.D |
| 1699 | 1703 | const atom = self.getAtom(.src_fn, atom_index); |
| 1700 | 1704 | if (atom.len == 0) return; |
| 1701 | 1705 | |
| 1702 | const decl = mod.declPtr(decl_index); | |
| 1703 | const func = decl.val.getFunction(mod).?; | |
| 1706 | const decl = zcu.declPtr(decl_index); | |
| 1707 | const func = decl.val.getFunction(zcu).?; | |
| 1704 | 1708 | log.debug("decl.src_line={d}, func.lbrace_line={d}, func.rbrace_line={d}", .{ |
| 1705 | decl.navSrcLine(mod), | |
| 1709 | decl.navSrcLine(zcu), | |
| 1706 | 1710 | func.lbrace_line, |
| 1707 | 1711 | func.rbrace_line, |
| 1708 | 1712 | }); |
| 1709 | const line: u28 = @intCast(decl.navSrcLine(mod) + func.lbrace_line); | |
| 1713 | const line: u28 = @intCast(decl.navSrcLine(zcu) + func.lbrace_line); | |
| 1710 | 1714 | var data: [4]u8 = undefined; |
| 1711 | 1715 | leb128.writeUnsignedFixed(4, &data, line); |
| 1712 | 1716 | |
| ... | ... | @@ -1969,7 +1973,7 @@ fn dbgInfoHeaderBytes(self: *Dwarf) usize { |
| 1969 | 1973 | return 120; |
| 1970 | 1974 | } |
| 1971 | 1975 | |
| 1972 | pub fn writeDbgInfoHeader(self: *Dwarf, zcu: *Module, low_pc: u64, high_pc: u64) !void { | |
| 1976 | pub fn writeDbgInfoHeader(self: *Dwarf, zcu: *Zcu, low_pc: u64, high_pc: u64) !void { | |
| 1973 | 1977 | // If this value is null it means there is an error in the module; |
| 1974 | 1978 | // leave debug_info_header_dirty=true. |
| 1975 | 1979 | const first_dbg_info_off = self.getDebugInfoOff() orelse return; |
| ... | ... | @@ -2058,14 +2062,14 @@ pub fn writeDbgInfoHeader(self: *Dwarf, zcu: *Module, low_pc: u64, high_pc: u64) |
| 2058 | 2062 | } |
| 2059 | 2063 | } |
| 2060 | 2064 | |
| 2061 | fn resolveCompilationDir(module: *Module, buffer: *[std.fs.max_path_bytes]u8) []const u8 { | |
| 2065 | fn resolveCompilationDir(zcu: *Zcu, buffer: *[std.fs.max_path_bytes]u8) []const u8 { | |
| 2062 | 2066 | // We fully resolve all paths at this point to avoid lack of source line info in stack |
| 2063 | 2067 | // traces or lack of debugging information which, if relative paths were used, would |
| 2064 | 2068 | // be very location dependent. |
| 2065 | 2069 | // TODO: the only concern I have with this is WASI as either host or target, should |
| 2066 | 2070 | // we leave the paths as relative then? |
| 2067 | const root_dir_path = module.root_mod.root.root_dir.path orelse "."; | |
| 2068 | const sub_path = module.root_mod.root.sub_path; | |
| 2071 | const root_dir_path = zcu.root_mod.root.root_dir.path orelse "."; | |
| 2072 | const sub_path = zcu.root_mod.root.sub_path; | |
| 2069 | 2073 | const realpath = if (std.fs.path.isAbsolute(root_dir_path)) r: { |
| 2070 | 2074 | @memcpy(buffer[0..root_dir_path.len], root_dir_path); |
| 2071 | 2075 | break :r root_dir_path; |
| ... | ... | @@ -2682,7 +2686,7 @@ fn padToIdeal(actual_size: anytype) @TypeOf(actual_size) { |
| 2682 | 2686 | return actual_size +| (actual_size / ideal_factor); |
| 2683 | 2687 | } |
| 2684 | 2688 | |
| 2685 | pub fn flushModule(self: *Dwarf, module: *Module) !void { | |
| 2689 | pub fn flushModule(self: *Dwarf, pt: Zcu.PerThread) !void { | |
| 2686 | 2690 | const comp = self.bin_file.comp; |
| 2687 | 2691 | const target = comp.root_mod.resolved_target.result; |
| 2688 | 2692 | |
| ... | ... | @@ -2694,9 +2698,9 @@ pub fn flushModule(self: *Dwarf, module: *Module) !void { |
| 2694 | 2698 | |
| 2695 | 2699 | var dbg_info_buffer = std.ArrayList(u8).init(arena); |
| 2696 | 2700 | try addDbgInfoErrorSetNames( |
| 2697 | module, | |
| 2701 | pt, | |
| 2698 | 2702 | Type.anyerror, |
| 2699 | module.global_error_set.keys(), | |
| 2703 | pt.zcu.global_error_set.keys(), | |
| 2700 | 2704 | target, |
| 2701 | 2705 | &dbg_info_buffer, |
| 2702 | 2706 | ); |
| ... | ... | @@ -2759,9 +2763,9 @@ pub fn flushModule(self: *Dwarf, module: *Module) !void { |
| 2759 | 2763 | } |
| 2760 | 2764 | } |
| 2761 | 2765 | |
| 2762 | fn addDIFile(self: *Dwarf, mod: *Module, decl_index: InternPool.DeclIndex) !u28 { | |
| 2763 | const decl = mod.declPtr(decl_index); | |
| 2764 | const file_scope = decl.getFileScope(mod); | |
| 2766 | fn addDIFile(self: *Dwarf, zcu: *Zcu, decl_index: InternPool.DeclIndex) !u28 { | |
| 2767 | const decl = zcu.declPtr(decl_index); | |
| 2768 | const file_scope = decl.getFileScope(zcu); | |
| 2765 | 2769 | const gop = try self.di_files.getOrPut(self.allocator, file_scope); |
| 2766 | 2770 | if (!gop.found_existing) { |
| 2767 | 2771 | switch (self.bin_file.tag) { |
| ... | ... | @@ -2827,16 +2831,16 @@ fn genIncludeDirsAndFileNames(self: *Dwarf, arena: Allocator) !struct { |
| 2827 | 2831 | } |
| 2828 | 2832 | |
| 2829 | 2833 | fn addDbgInfoErrorSet( |
| 2830 | mod: *Module, | |
| 2834 | pt: Zcu.PerThread, | |
| 2831 | 2835 | ty: Type, |
| 2832 | 2836 | target: std.Target, |
| 2833 | 2837 | dbg_info_buffer: *std.ArrayList(u8), |
| 2834 | 2838 | ) !void { |
| 2835 | return addDbgInfoErrorSetNames(mod, ty, ty.errorSetNames(mod).get(&mod.intern_pool), target, dbg_info_buffer); | |
| 2839 | return addDbgInfoErrorSetNames(pt, ty, ty.errorSetNames(pt.zcu).get(&pt.zcu.intern_pool), target, dbg_info_buffer); | |
| 2836 | 2840 | } |
| 2837 | 2841 | |
| 2838 | 2842 | fn addDbgInfoErrorSetNames( |
| 2839 | mod: *Module, | |
| 2843 | pt: Zcu.PerThread, | |
| 2840 | 2844 | /// Used for printing the type name only. |
| 2841 | 2845 | ty: Type, |
| 2842 | 2846 | error_names: []const InternPool.NullTerminatedString, |
| ... | ... | @@ -2848,10 +2852,10 @@ fn addDbgInfoErrorSetNames( |
| 2848 | 2852 | // DW.AT.enumeration_type |
| 2849 | 2853 | try dbg_info_buffer.append(@intFromEnum(AbbrevCode.enum_type)); |
| 2850 | 2854 | // DW.AT.byte_size, DW.FORM.udata |
| 2851 | const abi_size = Type.anyerror.abiSize(mod); | |
| 2855 | const abi_size = Type.anyerror.abiSize(pt); | |
| 2852 | 2856 | try leb128.writeUleb128(dbg_info_buffer.writer(), abi_size); |
| 2853 | 2857 | // DW.AT.name, DW.FORM.string |
| 2854 | try ty.print(dbg_info_buffer.writer(), mod); | |
| 2858 | try ty.print(dbg_info_buffer.writer(), pt); | |
| 2855 | 2859 | try dbg_info_buffer.append(0); |
| 2856 | 2860 | |
| 2857 | 2861 | // DW.AT.enumerator |
| ... | ... | @@ -2865,8 +2869,8 @@ fn addDbgInfoErrorSetNames( |
| 2865 | 2869 | mem.writeInt(u64, dbg_info_buffer.addManyAsArrayAssumeCapacity(8), 0, target_endian); |
| 2866 | 2870 | |
| 2867 | 2871 | for (error_names) |error_name| { |
| 2868 | const int = try mod.getErrorValue(error_name); | |
| 2869 | const error_name_slice = error_name.toSlice(&mod.intern_pool); | |
| 2872 | const int = try pt.zcu.getErrorValue(error_name); | |
| 2873 | const error_name_slice = error_name.toSlice(&pt.zcu.intern_pool); | |
| 2870 | 2874 | // DW.AT.enumerator |
| 2871 | 2875 | try dbg_info_buffer.ensureUnusedCapacity(error_name_slice.len + 2 + @sizeOf(u64)); |
| 2872 | 2876 | dbg_info_buffer.appendAssumeCapacity(@intFromEnum(AbbrevCode.enum_variant)); |
| ... | ... | @@ -2965,8 +2969,6 @@ const LinkBlock = File.LinkBlock; |
| 2965 | 2969 | const LinkFn = File.LinkFn; |
| 2966 | 2970 | const LinkerLoad = @import("../codegen.zig").LinkerLoad; |
| 2967 | 2971 | const Zcu = @import("../Zcu.zig"); |
| 2968 | /// Deprecated. | |
| 2969 | const Module = Zcu; | |
| 2970 | 2972 | const InternPool = @import("../InternPool.zig"); |
| 2971 | 2973 | const StringTable = @import("StringTable.zig"); |
| 2972 | 2974 | const Type = @import("../Type.zig"); |
src/link/Elf.zig+20-19| ... | ... | @@ -550,11 +550,12 @@ pub fn getDeclVAddr(self: *Elf, decl_index: InternPool.DeclIndex, reloc_info: li |
| 550 | 550 | |
| 551 | 551 | pub fn lowerAnonDecl( |
| 552 | 552 | self: *Elf, |
| 553 | pt: Zcu.PerThread, | |
| 553 | 554 | decl_val: InternPool.Index, |
| 554 | 555 | explicit_alignment: InternPool.Alignment, |
| 555 | 556 | src_loc: Module.LazySrcLoc, |
| 556 | 557 | ) !codegen.Result { |
| 557 | return self.zigObjectPtr().?.lowerAnonDecl(self, decl_val, explicit_alignment, src_loc); | |
| 558 | return self.zigObjectPtr().?.lowerAnonDecl(self, pt, decl_val, explicit_alignment, src_loc); | |
| 558 | 559 | } |
| 559 | 560 | |
| 560 | 561 | pub fn getAnonDeclVAddr(self: *Elf, decl_val: InternPool.Index, reloc_info: link.File.RelocInfo) !u64 { |
| ... | ... | @@ -1064,15 +1065,15 @@ pub fn markDirty(self: *Elf, shdr_index: u32) void { |
| 1064 | 1065 | } |
| 1065 | 1066 | } |
| 1066 | 1067 | |
| 1067 | pub fn flush(self: *Elf, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 1068 | pub fn flush(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 1068 | 1069 | const use_lld = build_options.have_llvm and self.base.comp.config.use_lld; |
| 1069 | 1070 | if (use_lld) { |
| 1070 | return self.linkWithLLD(arena, prog_node); | |
| 1071 | return self.linkWithLLD(arena, tid, prog_node); | |
| 1071 | 1072 | } |
| 1072 | try self.flushModule(arena, prog_node); | |
| 1073 | try self.flushModule(arena, tid, prog_node); | |
| 1073 | 1074 | } |
| 1074 | 1075 | |
| 1075 | pub fn flushModule(self: *Elf, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 1076 | pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 1076 | 1077 | const tracy = trace(@src()); |
| 1077 | 1078 | defer tracy.end(); |
| 1078 | 1079 | |
| ... | ... | @@ -1103,7 +1104,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, prog_node: std.Progress.Node) l |
| 1103 | 1104 | // --verbose-link |
| 1104 | 1105 | if (comp.verbose_link) try self.dumpArgv(comp); |
| 1105 | 1106 | |
| 1106 | if (self.zigObjectPtr()) |zig_object| try zig_object.flushModule(self); | |
| 1107 | if (self.zigObjectPtr()) |zig_object| try zig_object.flushModule(self, tid); | |
| 1107 | 1108 | if (self.base.isStaticLib()) return relocatable.flushStaticLib(self, comp, module_obj_path); |
| 1108 | 1109 | if (self.base.isObject()) return relocatable.flushObject(self, comp, module_obj_path); |
| 1109 | 1110 | |
| ... | ... | @@ -2146,7 +2147,7 @@ fn scanRelocs(self: *Elf) !void { |
| 2146 | 2147 | } |
| 2147 | 2148 | } |
| 2148 | 2149 | |
| 2149 | fn linkWithLLD(self: *Elf, arena: Allocator, prog_node: std.Progress.Node) !void { | |
| 2150 | fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) !void { | |
| 2150 | 2151 | const tracy = trace(@src()); |
| 2151 | 2152 | defer tracy.end(); |
| 2152 | 2153 | |
| ... | ... | @@ -2159,7 +2160,7 @@ fn linkWithLLD(self: *Elf, arena: Allocator, prog_node: std.Progress.Node) !void |
| 2159 | 2160 | // If there is no Zig code to compile, then we should skip flushing the output file because it |
| 2160 | 2161 | // will not be part of the linker line anyway. |
| 2161 | 2162 | const module_obj_path: ?[]const u8 = if (comp.module != null) blk: { |
| 2162 | try self.flushModule(arena, prog_node); | |
| 2163 | try self.flushModule(arena, tid, prog_node); | |
| 2163 | 2164 | |
| 2164 | 2165 | if (fs.path.dirname(full_out_path)) |dirname| { |
| 2165 | 2166 | break :blk try fs.path.join(arena, &.{ dirname, self.base.zcu_object_sub_path.? }); |
| ... | ... | @@ -2983,41 +2984,41 @@ pub fn freeDecl(self: *Elf, decl_index: InternPool.DeclIndex) void { |
| 2983 | 2984 | return self.zigObjectPtr().?.freeDecl(self, decl_index); |
| 2984 | 2985 | } |
| 2985 | 2986 | |
| 2986 | pub fn updateFunc(self: *Elf, mod: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 2987 | pub fn updateFunc(self: *Elf, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 2987 | 2988 | if (build_options.skip_non_native and builtin.object_format != .elf) { |
| 2988 | 2989 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 2989 | 2990 | } |
| 2990 | if (self.llvm_object) |llvm_object| return llvm_object.updateFunc(mod, func_index, air, liveness); | |
| 2991 | return self.zigObjectPtr().?.updateFunc(self, mod, func_index, air, liveness); | |
| 2991 | if (self.llvm_object) |llvm_object| return llvm_object.updateFunc(pt, func_index, air, liveness); | |
| 2992 | return self.zigObjectPtr().?.updateFunc(self, pt, func_index, air, liveness); | |
| 2992 | 2993 | } |
| 2993 | 2994 | |
| 2994 | 2995 | pub fn updateDecl( |
| 2995 | 2996 | self: *Elf, |
| 2996 | mod: *Module, | |
| 2997 | pt: Zcu.PerThread, | |
| 2997 | 2998 | decl_index: InternPool.DeclIndex, |
| 2998 | 2999 | ) link.File.UpdateDeclError!void { |
| 2999 | 3000 | if (build_options.skip_non_native and builtin.object_format != .elf) { |
| 3000 | 3001 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 3001 | 3002 | } |
| 3002 | if (self.llvm_object) |llvm_object| return llvm_object.updateDecl(mod, decl_index); | |
| 3003 | return self.zigObjectPtr().?.updateDecl(self, mod, decl_index); | |
| 3003 | if (self.llvm_object) |llvm_object| return llvm_object.updateDecl(pt, decl_index); | |
| 3004 | return self.zigObjectPtr().?.updateDecl(self, pt, decl_index); | |
| 3004 | 3005 | } |
| 3005 | 3006 | |
| 3006 | pub fn lowerUnnamedConst(self: *Elf, val: Value, decl_index: InternPool.DeclIndex) !u32 { | |
| 3007 | return self.zigObjectPtr().?.lowerUnnamedConst(self, val, decl_index); | |
| 3007 | pub fn lowerUnnamedConst(self: *Elf, pt: Zcu.PerThread, val: Value, decl_index: InternPool.DeclIndex) !u32 { | |
| 3008 | return self.zigObjectPtr().?.lowerUnnamedConst(self, pt, val, decl_index); | |
| 3008 | 3009 | } |
| 3009 | 3010 | |
| 3010 | 3011 | pub fn updateExports( |
| 3011 | 3012 | self: *Elf, |
| 3012 | mod: *Module, | |
| 3013 | pt: Zcu.PerThread, | |
| 3013 | 3014 | exported: Module.Exported, |
| 3014 | 3015 | export_indices: []const u32, |
| 3015 | 3016 | ) link.File.UpdateExportsError!void { |
| 3016 | 3017 | if (build_options.skip_non_native and builtin.object_format != .elf) { |
| 3017 | 3018 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 3018 | 3019 | } |
| 3019 | if (self.llvm_object) |llvm_object| return llvm_object.updateExports(mod, exported, export_indices); | |
| 3020 | return self.zigObjectPtr().?.updateExports(self, mod, exported, export_indices); | |
| 3020 | if (self.llvm_object) |llvm_object| return llvm_object.updateExports(pt, exported, export_indices); | |
| 3021 | return self.zigObjectPtr().?.updateExports(self, pt, exported, export_indices); | |
| 3021 | 3022 | } |
| 3022 | 3023 | |
| 3023 | 3024 | pub fn updateDeclLineNumber(self: *Elf, mod: *Module, decl_index: InternPool.DeclIndex) !void { |
src/link/Elf/ZigObject.zig+60-55| ... | ... | @@ -158,16 +158,17 @@ pub fn deinit(self: *ZigObject, allocator: Allocator) void { |
| 158 | 158 | } |
| 159 | 159 | } |
| 160 | 160 | |
| 161 | pub fn flushModule(self: *ZigObject, elf_file: *Elf) !void { | |
| 161 | pub fn flushModule(self: *ZigObject, elf_file: *Elf, tid: Zcu.PerThread.Id) !void { | |
| 162 | 162 | // Handle any lazy symbols that were emitted by incremental compilation. |
| 163 | 163 | if (self.lazy_syms.getPtr(.none)) |metadata| { |
| 164 | const zcu = elf_file.base.comp.module.?; | |
| 164 | const pt: Zcu.PerThread = .{ .zcu = elf_file.base.comp.module.?, .tid = tid }; | |
| 165 | 165 | |
| 166 | 166 | // Most lazy symbols can be updated on first use, but |
| 167 | 167 | // anyerror needs to wait for everything to be flushed. |
| 168 | 168 | if (metadata.text_state != .unused) self.updateLazySymbol( |
| 169 | 169 | elf_file, |
| 170 | link.File.LazySymbol.initDecl(.code, null, zcu), | |
| 170 | pt, | |
| 171 | link.File.LazySymbol.initDecl(.code, null, pt.zcu), | |
| 171 | 172 | metadata.text_symbol_index, |
| 172 | 173 | ) catch |err| return switch (err) { |
| 173 | 174 | error.CodegenFail => error.FlushFailure, |
| ... | ... | @@ -175,7 +176,8 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf) !void { |
| 175 | 176 | }; |
| 176 | 177 | if (metadata.rodata_state != .unused) self.updateLazySymbol( |
| 177 | 178 | elf_file, |
| 178 | link.File.LazySymbol.initDecl(.const_data, null, zcu), | |
| 179 | pt, | |
| 180 | link.File.LazySymbol.initDecl(.const_data, null, pt.zcu), | |
| 179 | 181 | metadata.rodata_symbol_index, |
| 180 | 182 | ) catch |err| return switch (err) { |
| 181 | 183 | error.CodegenFail => error.FlushFailure, |
| ... | ... | @@ -188,8 +190,8 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf) !void { |
| 188 | 190 | } |
| 189 | 191 | |
| 190 | 192 | if (self.dwarf) |*dw| { |
| 191 | const zcu = elf_file.base.comp.module.?; | |
| 192 | try dw.flushModule(zcu); | |
| 193 | const pt: Zcu.PerThread = .{ .zcu = elf_file.base.comp.module.?, .tid = tid }; | |
| 194 | try dw.flushModule(pt); | |
| 193 | 195 | |
| 194 | 196 | // TODO I need to re-think how to handle ZigObject's debug sections AND debug sections |
| 195 | 197 | // extracted from input object files correctly. |
| ... | ... | @@ -202,7 +204,7 @@ pub fn flushModule(self: *ZigObject, elf_file: *Elf) !void { |
| 202 | 204 | const text_shdr = elf_file.shdrs.items[elf_file.zig_text_section_index.?]; |
| 203 | 205 | const low_pc = text_shdr.sh_addr; |
| 204 | 206 | const high_pc = text_shdr.sh_addr + text_shdr.sh_size; |
| 205 | try dw.writeDbgInfoHeader(zcu, low_pc, high_pc); | |
| 207 | try dw.writeDbgInfoHeader(pt.zcu, low_pc, high_pc); | |
| 206 | 208 | self.debug_info_header_dirty = false; |
| 207 | 209 | } |
| 208 | 210 | |
| ... | ... | @@ -684,6 +686,7 @@ pub fn getAnonDeclVAddr( |
| 684 | 686 | pub fn lowerAnonDecl( |
| 685 | 687 | self: *ZigObject, |
| 686 | 688 | elf_file: *Elf, |
| 689 | pt: Zcu.PerThread, | |
| 687 | 690 | decl_val: InternPool.Index, |
| 688 | 691 | explicit_alignment: InternPool.Alignment, |
| 689 | 692 | src_loc: Module.LazySrcLoc, |
| ... | ... | @@ -692,7 +695,7 @@ pub fn lowerAnonDecl( |
| 692 | 695 | const mod = elf_file.base.comp.module.?; |
| 693 | 696 | const ty = Type.fromInterned(mod.intern_pool.typeOf(decl_val)); |
| 694 | 697 | const decl_alignment = switch (explicit_alignment) { |
| 695 | .none => ty.abiAlignment(mod), | |
| 698 | .none => ty.abiAlignment(pt), | |
| 696 | 699 | else => explicit_alignment, |
| 697 | 700 | }; |
| 698 | 701 | if (self.anon_decls.get(decl_val)) |metadata| { |
| ... | ... | @@ -708,6 +711,7 @@ pub fn lowerAnonDecl( |
| 708 | 711 | }) catch unreachable; |
| 709 | 712 | const res = self.lowerConst( |
| 710 | 713 | elf_file, |
| 714 | pt, | |
| 711 | 715 | name, |
| 712 | 716 | val, |
| 713 | 717 | decl_alignment, |
| ... | ... | @@ -733,10 +737,11 @@ pub fn lowerAnonDecl( |
| 733 | 737 | pub fn getOrCreateMetadataForLazySymbol( |
| 734 | 738 | self: *ZigObject, |
| 735 | 739 | elf_file: *Elf, |
| 740 | pt: Zcu.PerThread, | |
| 736 | 741 | lazy_sym: link.File.LazySymbol, |
| 737 | 742 | ) !Symbol.Index { |
| 738 | const gpa = elf_file.base.comp.gpa; | |
| 739 | const mod = elf_file.base.comp.module.?; | |
| 743 | const mod = pt.zcu; | |
| 744 | const gpa = mod.gpa; | |
| 740 | 745 | const gop = try self.lazy_syms.getOrPut(gpa, lazy_sym.getDecl(mod)); |
| 741 | 746 | errdefer _ = if (!gop.found_existing) self.lazy_syms.pop(); |
| 742 | 747 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| ... | ... | @@ -766,7 +771,7 @@ pub fn getOrCreateMetadataForLazySymbol( |
| 766 | 771 | metadata.state.* = .pending_flush; |
| 767 | 772 | const symbol_index = metadata.symbol_index.*; |
| 768 | 773 | // anyerror needs to be deferred until flushModule |
| 769 | if (lazy_sym.getDecl(mod) != .none) try self.updateLazySymbol(elf_file, lazy_sym, symbol_index); | |
| 774 | if (lazy_sym.getDecl(mod) != .none) try self.updateLazySymbol(elf_file, pt, lazy_sym, symbol_index); | |
| 770 | 775 | return symbol_index; |
| 771 | 776 | } |
| 772 | 777 | |
| ... | ... | @@ -893,6 +898,7 @@ fn getDeclShdrIndex( |
| 893 | 898 | fn updateDeclCode( |
| 894 | 899 | self: *ZigObject, |
| 895 | 900 | elf_file: *Elf, |
| 901 | pt: Zcu.PerThread, | |
| 896 | 902 | decl_index: InternPool.DeclIndex, |
| 897 | 903 | sym_index: Symbol.Index, |
| 898 | 904 | shdr_index: u32, |
| ... | ... | @@ -900,13 +906,13 @@ fn updateDeclCode( |
| 900 | 906 | stt_bits: u8, |
| 901 | 907 | ) !void { |
| 902 | 908 | const gpa = elf_file.base.comp.gpa; |
| 903 | const mod = elf_file.base.comp.module.?; | |
| 909 | const mod = pt.zcu; | |
| 904 | 910 | const decl = mod.declPtr(decl_index); |
| 905 | 911 | const decl_name = try decl.fullyQualifiedName(mod); |
| 906 | 912 | |
| 907 | 913 | log.debug("updateDeclCode {}{*}", .{ decl_name.fmt(&mod.intern_pool), decl }); |
| 908 | 914 | |
| 909 | const required_alignment = decl.getAlignment(mod).max( | |
| 915 | const required_alignment = decl.getAlignment(pt).max( | |
| 910 | 916 | target_util.minFunctionAlignment(mod.getTarget()), |
| 911 | 917 | ); |
| 912 | 918 | |
| ... | ... | @@ -994,19 +1000,20 @@ fn updateDeclCode( |
| 994 | 1000 | fn updateTlv( |
| 995 | 1001 | self: *ZigObject, |
| 996 | 1002 | elf_file: *Elf, |
| 1003 | pt: Zcu.PerThread, | |
| 997 | 1004 | decl_index: InternPool.DeclIndex, |
| 998 | 1005 | sym_index: Symbol.Index, |
| 999 | 1006 | shndx: u32, |
| 1000 | 1007 | code: []const u8, |
| 1001 | 1008 | ) !void { |
| 1002 | const gpa = elf_file.base.comp.gpa; | |
| 1003 | const mod = elf_file.base.comp.module.?; | |
| 1009 | const mod = pt.zcu; | |
| 1010 | const gpa = mod.gpa; | |
| 1004 | 1011 | const decl = mod.declPtr(decl_index); |
| 1005 | 1012 | const decl_name = try decl.fullyQualifiedName(mod); |
| 1006 | 1013 | |
| 1007 | 1014 | log.debug("updateTlv {} ({*})", .{ decl_name.fmt(&mod.intern_pool), decl }); |
| 1008 | 1015 | |
| 1009 | const required_alignment = decl.getAlignment(mod); | |
| 1016 | const required_alignment = decl.getAlignment(pt); | |
| 1010 | 1017 | |
| 1011 | 1018 | const sym = elf_file.symbol(sym_index); |
| 1012 | 1019 | const esym = &self.local_esyms.items(.elf_sym)[sym.esym_index]; |
| ... | ... | @@ -1048,7 +1055,7 @@ fn updateTlv( |
| 1048 | 1055 | pub fn updateFunc( |
| 1049 | 1056 | self: *ZigObject, |
| 1050 | 1057 | elf_file: *Elf, |
| 1051 | mod: *Module, | |
| 1058 | pt: Zcu.PerThread, | |
| 1052 | 1059 | func_index: InternPool.Index, |
| 1053 | 1060 | air: Air, |
| 1054 | 1061 | liveness: Liveness, |
| ... | ... | @@ -1056,6 +1063,7 @@ pub fn updateFunc( |
| 1056 | 1063 | const tracy = trace(@src()); |
| 1057 | 1064 | defer tracy.end(); |
| 1058 | 1065 | |
| 1066 | const mod = pt.zcu; | |
| 1059 | 1067 | const gpa = elf_file.base.comp.gpa; |
| 1060 | 1068 | const func = mod.funcInfo(func_index); |
| 1061 | 1069 | const decl_index = func.owner_decl; |
| ... | ... | @@ -1068,29 +1076,19 @@ pub fn updateFunc( |
| 1068 | 1076 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 1069 | 1077 | defer code_buffer.deinit(); |
| 1070 | 1078 | |
| 1071 | var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(mod, decl_index) else null; | |
| 1079 | var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(pt, decl_index) else null; | |
| 1072 | 1080 | defer if (decl_state) |*ds| ds.deinit(); |
| 1073 | 1081 | |
| 1074 | const res = if (decl_state) |*ds| | |
| 1075 | try codegen.generateFunction( | |
| 1076 | &elf_file.base, | |
| 1077 | decl.navSrcLoc(mod), | |
| 1078 | func_index, | |
| 1079 | air, | |
| 1080 | liveness, | |
| 1081 | &code_buffer, | |
| 1082 | .{ .dwarf = ds }, | |
| 1083 | ) | |
| 1084 | else | |
| 1085 | try codegen.generateFunction( | |
| 1086 | &elf_file.base, | |
| 1087 | decl.navSrcLoc(mod), | |
| 1088 | func_index, | |
| 1089 | air, | |
| 1090 | liveness, | |
| 1091 | &code_buffer, | |
| 1092 | .none, | |
| 1093 | ); | |
| 1082 | const res = try codegen.generateFunction( | |
| 1083 | &elf_file.base, | |
| 1084 | pt, | |
| 1085 | decl.navSrcLoc(mod), | |
| 1086 | func_index, | |
| 1087 | air, | |
| 1088 | liveness, | |
| 1089 | &code_buffer, | |
| 1090 | if (decl_state) |*ds| .{ .dwarf = ds } else .none, | |
| 1091 | ); | |
| 1094 | 1092 | |
| 1095 | 1093 | const code = switch (res) { |
| 1096 | 1094 | .ok => code_buffer.items, |
| ... | ... | @@ -1102,12 +1100,12 @@ pub fn updateFunc( |
| 1102 | 1100 | }; |
| 1103 | 1101 | |
| 1104 | 1102 | const shndx = try self.getDeclShdrIndex(elf_file, decl, code); |
| 1105 | try self.updateDeclCode(elf_file, decl_index, sym_index, shndx, code, elf.STT_FUNC); | |
| 1103 | try self.updateDeclCode(elf_file, pt, decl_index, sym_index, shndx, code, elf.STT_FUNC); | |
| 1106 | 1104 | |
| 1107 | 1105 | if (decl_state) |*ds| { |
| 1108 | 1106 | const sym = elf_file.symbol(sym_index); |
| 1109 | 1107 | try self.dwarf.?.commitDeclState( |
| 1110 | mod, | |
| 1108 | pt, | |
| 1111 | 1109 | decl_index, |
| 1112 | 1110 | @intCast(sym.address(.{}, elf_file)), |
| 1113 | 1111 | sym.atom(elf_file).?.size, |
| ... | ... | @@ -1121,12 +1119,13 @@ pub fn updateFunc( |
| 1121 | 1119 | pub fn updateDecl( |
| 1122 | 1120 | self: *ZigObject, |
| 1123 | 1121 | elf_file: *Elf, |
| 1124 | mod: *Module, | |
| 1122 | pt: Zcu.PerThread, | |
| 1125 | 1123 | decl_index: InternPool.DeclIndex, |
| 1126 | 1124 | ) link.File.UpdateDeclError!void { |
| 1127 | 1125 | const tracy = trace(@src()); |
| 1128 | 1126 | defer tracy.end(); |
| 1129 | 1127 | |
| 1128 | const mod = pt.zcu; | |
| 1130 | 1129 | const decl = mod.declPtr(decl_index); |
| 1131 | 1130 | |
| 1132 | 1131 | if (decl.val.getExternFunc(mod)) |_| { |
| ... | ... | @@ -1150,19 +1149,19 @@ pub fn updateDecl( |
| 1150 | 1149 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 1151 | 1150 | defer code_buffer.deinit(); |
| 1152 | 1151 | |
| 1153 | var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(mod, decl_index) else null; | |
| 1152 | var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(pt, decl_index) else null; | |
| 1154 | 1153 | defer if (decl_state) |*ds| ds.deinit(); |
| 1155 | 1154 | |
| 1156 | 1155 | // TODO implement .debug_info for global variables |
| 1157 | 1156 | const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val; |
| 1158 | 1157 | const res = if (decl_state) |*ds| |
| 1159 | try codegen.generateSymbol(&elf_file.base, decl.navSrcLoc(mod), decl_val, &code_buffer, .{ | |
| 1158 | try codegen.generateSymbol(&elf_file.base, pt, decl.navSrcLoc(mod), decl_val, &code_buffer, .{ | |
| 1160 | 1159 | .dwarf = ds, |
| 1161 | 1160 | }, .{ |
| 1162 | 1161 | .parent_atom_index = sym_index, |
| 1163 | 1162 | }) |
| 1164 | 1163 | else |
| 1165 | try codegen.generateSymbol(&elf_file.base, decl.navSrcLoc(mod), decl_val, &code_buffer, .none, .{ | |
| 1164 | try codegen.generateSymbol(&elf_file.base, pt, decl.navSrcLoc(mod), decl_val, &code_buffer, .none, .{ | |
| 1166 | 1165 | .parent_atom_index = sym_index, |
| 1167 | 1166 | }); |
| 1168 | 1167 | |
| ... | ... | @@ -1177,14 +1176,14 @@ pub fn updateDecl( |
| 1177 | 1176 | |
| 1178 | 1177 | const shndx = try self.getDeclShdrIndex(elf_file, decl, code); |
| 1179 | 1178 | if (elf_file.shdrs.items[shndx].sh_flags & elf.SHF_TLS != 0) |
| 1180 | try self.updateTlv(elf_file, decl_index, sym_index, shndx, code) | |
| 1179 | try self.updateTlv(elf_file, pt, decl_index, sym_index, shndx, code) | |
| 1181 | 1180 | else |
| 1182 | try self.updateDeclCode(elf_file, decl_index, sym_index, shndx, code, elf.STT_OBJECT); | |
| 1181 | try self.updateDeclCode(elf_file, pt, decl_index, sym_index, shndx, code, elf.STT_OBJECT); | |
| 1183 | 1182 | |
| 1184 | 1183 | if (decl_state) |*ds| { |
| 1185 | 1184 | const sym = elf_file.symbol(sym_index); |
| 1186 | 1185 | try self.dwarf.?.commitDeclState( |
| 1187 | mod, | |
| 1186 | pt, | |
| 1188 | 1187 | decl_index, |
| 1189 | 1188 | @intCast(sym.address(.{}, elf_file)), |
| 1190 | 1189 | sym.atom(elf_file).?.size, |
| ... | ... | @@ -1198,11 +1197,12 @@ pub fn updateDecl( |
| 1198 | 1197 | fn updateLazySymbol( |
| 1199 | 1198 | self: *ZigObject, |
| 1200 | 1199 | elf_file: *Elf, |
| 1200 | pt: Zcu.PerThread, | |
| 1201 | 1201 | sym: link.File.LazySymbol, |
| 1202 | 1202 | symbol_index: Symbol.Index, |
| 1203 | 1203 | ) !void { |
| 1204 | const gpa = elf_file.base.comp.gpa; | |
| 1205 | const mod = elf_file.base.comp.module.?; | |
| 1204 | const mod = pt.zcu; | |
| 1205 | const gpa = mod.gpa; | |
| 1206 | 1206 | |
| 1207 | 1207 | var required_alignment: InternPool.Alignment = .none; |
| 1208 | 1208 | var code_buffer = std.ArrayList(u8).init(gpa); |
| ... | ... | @@ -1211,7 +1211,7 @@ fn updateLazySymbol( |
| 1211 | 1211 | const name_str_index = blk: { |
| 1212 | 1212 | const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{}", .{ |
| 1213 | 1213 | @tagName(sym.kind), |
| 1214 | sym.ty.fmt(mod), | |
| 1214 | sym.ty.fmt(pt), | |
| 1215 | 1215 | }); |
| 1216 | 1216 | defer gpa.free(name); |
| 1217 | 1217 | break :blk try self.strtab.insert(gpa, name); |
| ... | ... | @@ -1220,6 +1220,7 @@ fn updateLazySymbol( |
| 1220 | 1220 | const src = sym.ty.srcLocOrNull(mod) orelse Module.LazySrcLoc.unneeded; |
| 1221 | 1221 | const res = try codegen.generateLazySymbol( |
| 1222 | 1222 | &elf_file.base, |
| 1223 | pt, | |
| 1223 | 1224 | src, |
| 1224 | 1225 | sym, |
| 1225 | 1226 | &required_alignment, |
| ... | ... | @@ -1273,6 +1274,7 @@ fn updateLazySymbol( |
| 1273 | 1274 | pub fn lowerUnnamedConst( |
| 1274 | 1275 | self: *ZigObject, |
| 1275 | 1276 | elf_file: *Elf, |
| 1277 | pt: Zcu.PerThread, | |
| 1276 | 1278 | val: Value, |
| 1277 | 1279 | decl_index: InternPool.DeclIndex, |
| 1278 | 1280 | ) !u32 { |
| ... | ... | @@ -1291,9 +1293,10 @@ pub fn lowerUnnamedConst( |
| 1291 | 1293 | const ty = val.typeOf(mod); |
| 1292 | 1294 | const sym_index = switch (try self.lowerConst( |
| 1293 | 1295 | elf_file, |
| 1296 | pt, | |
| 1294 | 1297 | name, |
| 1295 | 1298 | val, |
| 1296 | ty.abiAlignment(mod), | |
| 1299 | ty.abiAlignment(pt), | |
| 1297 | 1300 | elf_file.zig_data_rel_ro_section_index.?, |
| 1298 | 1301 | decl.navSrcLoc(mod), |
| 1299 | 1302 | )) { |
| ... | ... | @@ -1318,20 +1321,21 @@ const LowerConstResult = union(enum) { |
| 1318 | 1321 | fn lowerConst( |
| 1319 | 1322 | self: *ZigObject, |
| 1320 | 1323 | elf_file: *Elf, |
| 1324 | pt: Zcu.PerThread, | |
| 1321 | 1325 | name: []const u8, |
| 1322 | 1326 | val: Value, |
| 1323 | 1327 | required_alignment: InternPool.Alignment, |
| 1324 | 1328 | output_section_index: u32, |
| 1325 | 1329 | src_loc: Module.LazySrcLoc, |
| 1326 | 1330 | ) !LowerConstResult { |
| 1327 | const gpa = elf_file.base.comp.gpa; | |
| 1331 | const gpa = pt.zcu.gpa; | |
| 1328 | 1332 | |
| 1329 | 1333 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 1330 | 1334 | defer code_buffer.deinit(); |
| 1331 | 1335 | |
| 1332 | 1336 | const sym_index = try self.addAtom(elf_file); |
| 1333 | 1337 | |
| 1334 | const res = try codegen.generateSymbol(&elf_file.base, src_loc, val, &code_buffer, .{ | |
| 1338 | const res = try codegen.generateSymbol(&elf_file.base, pt, src_loc, val, &code_buffer, .{ | |
| 1335 | 1339 | .none = {}, |
| 1336 | 1340 | }, .{ |
| 1337 | 1341 | .parent_atom_index = sym_index, |
| ... | ... | @@ -1373,13 +1377,14 @@ fn lowerConst( |
| 1373 | 1377 | pub fn updateExports( |
| 1374 | 1378 | self: *ZigObject, |
| 1375 | 1379 | elf_file: *Elf, |
| 1376 | mod: *Module, | |
| 1380 | pt: Zcu.PerThread, | |
| 1377 | 1381 | exported: Module.Exported, |
| 1378 | 1382 | export_indices: []const u32, |
| 1379 | 1383 | ) link.File.UpdateExportsError!void { |
| 1380 | 1384 | const tracy = trace(@src()); |
| 1381 | 1385 | defer tracy.end(); |
| 1382 | 1386 | |
| 1387 | const mod = pt.zcu; | |
| 1383 | 1388 | const gpa = elf_file.base.comp.gpa; |
| 1384 | 1389 | const metadata = switch (exported) { |
| 1385 | 1390 | .decl_index => |decl_index| blk: { |
| ... | ... | @@ -1388,7 +1393,7 @@ pub fn updateExports( |
| 1388 | 1393 | }, |
| 1389 | 1394 | .value => |value| self.anon_decls.getPtr(value) orelse blk: { |
| 1390 | 1395 | const first_exp = mod.all_exports.items[export_indices[0]]; |
| 1391 | const res = try self.lowerAnonDecl(elf_file, value, .none, first_exp.src); | |
| 1396 | const res = try self.lowerAnonDecl(elf_file, pt, value, .none, first_exp.src); | |
| 1392 | 1397 | switch (res) { |
| 1393 | 1398 | .ok => {}, |
| 1394 | 1399 | .fail => |em| { |
src/link/MachO.zig+17-16| ... | ... | @@ -360,11 +360,11 @@ pub fn deinit(self: *MachO) void { |
| 360 | 360 | self.unwind_records.deinit(gpa); |
| 361 | 361 | } |
| 362 | 362 | |
| 363 | pub fn flush(self: *MachO, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 364 | try self.flushModule(arena, prog_node); | |
| 363 | pub fn flush(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 364 | try self.flushModule(arena, tid, prog_node); | |
| 365 | 365 | } |
| 366 | 366 | |
| 367 | pub fn flushModule(self: *MachO, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 367 | pub fn flushModule(self: *MachO, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 368 | 368 | const tracy = trace(@src()); |
| 369 | 369 | defer tracy.end(); |
| 370 | 370 | |
| ... | ... | @@ -391,7 +391,7 @@ pub fn flushModule(self: *MachO, arena: Allocator, prog_node: std.Progress.Node) |
| 391 | 391 | // --verbose-link |
| 392 | 392 | if (comp.verbose_link) try self.dumpArgv(comp); |
| 393 | 393 | |
| 394 | if (self.getZigObject()) |zo| try zo.flushModule(self); | |
| 394 | if (self.getZigObject()) |zo| try zo.flushModule(self, tid); | |
| 395 | 395 | if (self.base.isStaticLib()) return relocatable.flushStaticLib(self, comp, module_obj_path); |
| 396 | 396 | if (self.base.isObject()) return relocatable.flushObject(self, comp, module_obj_path); |
| 397 | 397 | |
| ... | ... | @@ -3178,24 +3178,24 @@ pub fn writeCodeSignature(self: *MachO, code_sig: *CodeSignature) !void { |
| 3178 | 3178 | try self.base.file.?.pwriteAll(buffer.items, offset); |
| 3179 | 3179 | } |
| 3180 | 3180 | |
| 3181 | pub fn updateFunc(self: *MachO, mod: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 3181 | pub fn updateFunc(self: *MachO, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 3182 | 3182 | if (build_options.skip_non_native and builtin.object_format != .macho) { |
| 3183 | 3183 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 3184 | 3184 | } |
| 3185 | if (self.llvm_object) |llvm_object| return llvm_object.updateFunc(mod, func_index, air, liveness); | |
| 3186 | return self.getZigObject().?.updateFunc(self, mod, func_index, air, liveness); | |
| 3185 | if (self.llvm_object) |llvm_object| return llvm_object.updateFunc(pt, func_index, air, liveness); | |
| 3186 | return self.getZigObject().?.updateFunc(self, pt, func_index, air, liveness); | |
| 3187 | 3187 | } |
| 3188 | 3188 | |
| 3189 | pub fn lowerUnnamedConst(self: *MachO, val: Value, decl_index: InternPool.DeclIndex) !u32 { | |
| 3190 | return self.getZigObject().?.lowerUnnamedConst(self, val, decl_index); | |
| 3189 | pub fn lowerUnnamedConst(self: *MachO, pt: Zcu.PerThread, val: Value, decl_index: InternPool.DeclIndex) !u32 { | |
| 3190 | return self.getZigObject().?.lowerUnnamedConst(self, pt, val, decl_index); | |
| 3191 | 3191 | } |
| 3192 | 3192 | |
| 3193 | pub fn updateDecl(self: *MachO, mod: *Module, decl_index: InternPool.DeclIndex) !void { | |
| 3193 | pub fn updateDecl(self: *MachO, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex) !void { | |
| 3194 | 3194 | if (build_options.skip_non_native and builtin.object_format != .macho) { |
| 3195 | 3195 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 3196 | 3196 | } |
| 3197 | if (self.llvm_object) |llvm_object| return llvm_object.updateDecl(mod, decl_index); | |
| 3198 | return self.getZigObject().?.updateDecl(self, mod, decl_index); | |
| 3197 | if (self.llvm_object) |llvm_object| return llvm_object.updateDecl(pt, decl_index); | |
| 3198 | return self.getZigObject().?.updateDecl(self, pt, decl_index); | |
| 3199 | 3199 | } |
| 3200 | 3200 | |
| 3201 | 3201 | pub fn updateDeclLineNumber(self: *MachO, module: *Module, decl_index: InternPool.DeclIndex) !void { |
| ... | ... | @@ -3205,15 +3205,15 @@ pub fn updateDeclLineNumber(self: *MachO, module: *Module, decl_index: InternPoo |
| 3205 | 3205 | |
| 3206 | 3206 | pub fn updateExports( |
| 3207 | 3207 | self: *MachO, |
| 3208 | mod: *Module, | |
| 3208 | pt: Zcu.PerThread, | |
| 3209 | 3209 | exported: Module.Exported, |
| 3210 | 3210 | export_indices: []const u32, |
| 3211 | 3211 | ) link.File.UpdateExportsError!void { |
| 3212 | 3212 | if (build_options.skip_non_native and builtin.object_format != .macho) { |
| 3213 | 3213 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 3214 | 3214 | } |
| 3215 | if (self.llvm_object) |llvm_object| return llvm_object.updateExports(mod, exported, export_indices); | |
| 3216 | return self.getZigObject().?.updateExports(self, mod, exported, export_indices); | |
| 3215 | if (self.llvm_object) |llvm_object| return llvm_object.updateExports(pt, exported, export_indices); | |
| 3216 | return self.getZigObject().?.updateExports(self, pt, exported, export_indices); | |
| 3217 | 3217 | } |
| 3218 | 3218 | |
| 3219 | 3219 | pub fn deleteExport( |
| ... | ... | @@ -3237,11 +3237,12 @@ pub fn getDeclVAddr(self: *MachO, decl_index: InternPool.DeclIndex, reloc_info: |
| 3237 | 3237 | |
| 3238 | 3238 | pub fn lowerAnonDecl( |
| 3239 | 3239 | self: *MachO, |
| 3240 | pt: Zcu.PerThread, | |
| 3240 | 3241 | decl_val: InternPool.Index, |
| 3241 | 3242 | explicit_alignment: InternPool.Alignment, |
| 3242 | 3243 | src_loc: Module.LazySrcLoc, |
| 3243 | 3244 | ) !codegen.Result { |
| 3244 | return self.getZigObject().?.lowerAnonDecl(self, decl_val, explicit_alignment, src_loc); | |
| 3245 | return self.getZigObject().?.lowerAnonDecl(self, pt, decl_val, explicit_alignment, src_loc); | |
| 3245 | 3246 | } |
| 3246 | 3247 | |
| 3247 | 3248 | pub fn getAnonDeclVAddr(self: *MachO, decl_val: InternPool.Index, reloc_info: link.File.RelocInfo) !u64 { |
src/link/MachO/ZigObject.zig+49-33| ... | ... | @@ -425,16 +425,17 @@ pub fn getInputSection(self: ZigObject, atom: Atom, macho_file: *MachO) macho.se |
| 425 | 425 | return sect; |
| 426 | 426 | } |
| 427 | 427 | |
| 428 | pub fn flushModule(self: *ZigObject, macho_file: *MachO) !void { | |
| 428 | pub fn flushModule(self: *ZigObject, macho_file: *MachO, tid: Zcu.PerThread.Id) !void { | |
| 429 | 429 | // Handle any lazy symbols that were emitted by incremental compilation. |
| 430 | 430 | if (self.lazy_syms.getPtr(.none)) |metadata| { |
| 431 | const zcu = macho_file.base.comp.module.?; | |
| 431 | const pt: Zcu.PerThread = .{ .zcu = macho_file.base.comp.module.?, .tid = tid }; | |
| 432 | 432 | |
| 433 | 433 | // Most lazy symbols can be updated on first use, but |
| 434 | 434 | // anyerror needs to wait for everything to be flushed. |
| 435 | 435 | if (metadata.text_state != .unused) self.updateLazySymbol( |
| 436 | 436 | macho_file, |
| 437 | link.File.LazySymbol.initDecl(.code, null, zcu), | |
| 437 | pt, | |
| 438 | link.File.LazySymbol.initDecl(.code, null, pt.zcu), | |
| 438 | 439 | metadata.text_symbol_index, |
| 439 | 440 | ) catch |err| return switch (err) { |
| 440 | 441 | error.CodegenFail => error.FlushFailure, |
| ... | ... | @@ -442,7 +443,8 @@ pub fn flushModule(self: *ZigObject, macho_file: *MachO) !void { |
| 442 | 443 | }; |
| 443 | 444 | if (metadata.const_state != .unused) self.updateLazySymbol( |
| 444 | 445 | macho_file, |
| 445 | link.File.LazySymbol.initDecl(.const_data, null, zcu), | |
| 446 | pt, | |
| 447 | link.File.LazySymbol.initDecl(.const_data, null, pt.zcu), | |
| 446 | 448 | metadata.const_symbol_index, |
| 447 | 449 | ) catch |err| return switch (err) { |
| 448 | 450 | error.CodegenFail => error.FlushFailure, |
| ... | ... | @@ -455,8 +457,8 @@ pub fn flushModule(self: *ZigObject, macho_file: *MachO) !void { |
| 455 | 457 | } |
| 456 | 458 | |
| 457 | 459 | if (self.dwarf) |*dw| { |
| 458 | const zcu = macho_file.base.comp.module.?; | |
| 459 | try dw.flushModule(zcu); | |
| 460 | const pt: Zcu.PerThread = .{ .zcu = macho_file.base.comp.module.?, .tid = tid }; | |
| 461 | try dw.flushModule(pt); | |
| 460 | 462 | |
| 461 | 463 | if (self.debug_abbrev_dirty) { |
| 462 | 464 | try dw.writeDbgAbbrev(); |
| ... | ... | @@ -469,7 +471,7 @@ pub fn flushModule(self: *ZigObject, macho_file: *MachO) !void { |
| 469 | 471 | const text_section = macho_file.sections.items(.header)[macho_file.zig_text_sect_index.?]; |
| 470 | 472 | const low_pc = text_section.addr; |
| 471 | 473 | const high_pc = text_section.addr + text_section.size; |
| 472 | try dw.writeDbgInfoHeader(zcu, low_pc, high_pc); | |
| 474 | try dw.writeDbgInfoHeader(pt.zcu, low_pc, high_pc); | |
| 473 | 475 | self.debug_info_header_dirty = false; |
| 474 | 476 | } |
| 475 | 477 | |
| ... | ... | @@ -570,6 +572,7 @@ pub fn getAnonDeclVAddr( |
| 570 | 572 | pub fn lowerAnonDecl( |
| 571 | 573 | self: *ZigObject, |
| 572 | 574 | macho_file: *MachO, |
| 575 | pt: Zcu.PerThread, | |
| 573 | 576 | decl_val: InternPool.Index, |
| 574 | 577 | explicit_alignment: Atom.Alignment, |
| 575 | 578 | src_loc: Module.LazySrcLoc, |
| ... | ... | @@ -578,7 +581,7 @@ pub fn lowerAnonDecl( |
| 578 | 581 | const mod = macho_file.base.comp.module.?; |
| 579 | 582 | const ty = Type.fromInterned(mod.intern_pool.typeOf(decl_val)); |
| 580 | 583 | const decl_alignment = switch (explicit_alignment) { |
| 581 | .none => ty.abiAlignment(mod), | |
| 584 | .none => ty.abiAlignment(pt), | |
| 582 | 585 | else => explicit_alignment, |
| 583 | 586 | }; |
| 584 | 587 | if (self.anon_decls.get(decl_val)) |metadata| { |
| ... | ... | @@ -593,6 +596,7 @@ pub fn lowerAnonDecl( |
| 593 | 596 | }) catch unreachable; |
| 594 | 597 | const res = self.lowerConst( |
| 595 | 598 | macho_file, |
| 599 | pt, | |
| 596 | 600 | name, |
| 597 | 601 | Value.fromInterned(decl_val), |
| 598 | 602 | decl_alignment, |
| ... | ... | @@ -656,7 +660,7 @@ pub fn freeDecl(self: *ZigObject, macho_file: *MachO, decl_index: InternPool.Dec |
| 656 | 660 | pub fn updateFunc( |
| 657 | 661 | self: *ZigObject, |
| 658 | 662 | macho_file: *MachO, |
| 659 | mod: *Module, | |
| 663 | pt: Zcu.PerThread, | |
| 660 | 664 | func_index: InternPool.Index, |
| 661 | 665 | air: Air, |
| 662 | 666 | liveness: Liveness, |
| ... | ... | @@ -664,7 +668,8 @@ pub fn updateFunc( |
| 664 | 668 | const tracy = trace(@src()); |
| 665 | 669 | defer tracy.end(); |
| 666 | 670 | |
| 667 | const gpa = macho_file.base.comp.gpa; | |
| 671 | const mod = pt.zcu; | |
| 672 | const gpa = mod.gpa; | |
| 668 | 673 | const func = mod.funcInfo(func_index); |
| 669 | 674 | const decl_index = func.owner_decl; |
| 670 | 675 | const decl = mod.declPtr(decl_index); |
| ... | ... | @@ -676,12 +681,13 @@ pub fn updateFunc( |
| 676 | 681 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 677 | 682 | defer code_buffer.deinit(); |
| 678 | 683 | |
| 679 | var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(mod, decl_index) else null; | |
| 684 | var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(pt, decl_index) else null; | |
| 680 | 685 | defer if (decl_state) |*ds| ds.deinit(); |
| 681 | 686 | |
| 682 | 687 | const dio: codegen.DebugInfoOutput = if (decl_state) |*ds| .{ .dwarf = ds } else .none; |
| 683 | 688 | const res = try codegen.generateFunction( |
| 684 | 689 | &macho_file.base, |
| 690 | pt, | |
| 685 | 691 | decl.navSrcLoc(mod), |
| 686 | 692 | func_index, |
| 687 | 693 | air, |
| ... | ... | @@ -700,12 +706,12 @@ pub fn updateFunc( |
| 700 | 706 | }; |
| 701 | 707 | |
| 702 | 708 | const sect_index = try self.getDeclOutputSection(macho_file, decl, code); |
| 703 | try self.updateDeclCode(macho_file, decl_index, sym_index, sect_index, code); | |
| 709 | try self.updateDeclCode(macho_file, pt, decl_index, sym_index, sect_index, code); | |
| 704 | 710 | |
| 705 | 711 | if (decl_state) |*ds| { |
| 706 | 712 | const sym = macho_file.getSymbol(sym_index); |
| 707 | 713 | try self.dwarf.?.commitDeclState( |
| 708 | mod, | |
| 714 | pt, | |
| 709 | 715 | decl_index, |
| 710 | 716 | sym.getAddress(.{}, macho_file), |
| 711 | 717 | sym.getAtom(macho_file).?.size, |
| ... | ... | @@ -719,12 +725,13 @@ pub fn updateFunc( |
| 719 | 725 | pub fn updateDecl( |
| 720 | 726 | self: *ZigObject, |
| 721 | 727 | macho_file: *MachO, |
| 722 | mod: *Module, | |
| 728 | pt: Zcu.PerThread, | |
| 723 | 729 | decl_index: InternPool.DeclIndex, |
| 724 | 730 | ) link.File.UpdateDeclError!void { |
| 725 | 731 | const tracy = trace(@src()); |
| 726 | 732 | defer tracy.end(); |
| 727 | 733 | |
| 734 | const mod = pt.zcu; | |
| 728 | 735 | const decl = mod.declPtr(decl_index); |
| 729 | 736 | |
| 730 | 737 | if (decl.val.getExternFunc(mod)) |_| { |
| ... | ... | @@ -749,12 +756,12 @@ pub fn updateDecl( |
| 749 | 756 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 750 | 757 | defer code_buffer.deinit(); |
| 751 | 758 | |
| 752 | var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(mod, decl_index) else null; | |
| 759 | var decl_state: ?Dwarf.DeclState = if (self.dwarf) |*dw| try dw.initDeclState(pt, decl_index) else null; | |
| 753 | 760 | defer if (decl_state) |*ds| ds.deinit(); |
| 754 | 761 | |
| 755 | 762 | const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val; |
| 756 | 763 | const dio: codegen.DebugInfoOutput = if (decl_state) |*ds| .{ .dwarf = ds } else .none; |
| 757 | const res = try codegen.generateSymbol(&macho_file.base, decl.navSrcLoc(mod), decl_val, &code_buffer, dio, .{ | |
| 764 | const res = try codegen.generateSymbol(&macho_file.base, pt, decl.navSrcLoc(mod), decl_val, &code_buffer, dio, .{ | |
| 758 | 765 | .parent_atom_index = sym_index, |
| 759 | 766 | }); |
| 760 | 767 | |
| ... | ... | @@ -772,15 +779,15 @@ pub fn updateDecl( |
| 772 | 779 | else => false, |
| 773 | 780 | }; |
| 774 | 781 | if (is_threadlocal) { |
| 775 | try self.updateTlv(macho_file, decl_index, sym_index, sect_index, code); | |
| 782 | try self.updateTlv(macho_file, pt, decl_index, sym_index, sect_index, code); | |
| 776 | 783 | } else { |
| 777 | try self.updateDeclCode(macho_file, decl_index, sym_index, sect_index, code); | |
| 784 | try self.updateDeclCode(macho_file, pt, decl_index, sym_index, sect_index, code); | |
| 778 | 785 | } |
| 779 | 786 | |
| 780 | 787 | if (decl_state) |*ds| { |
| 781 | 788 | const sym = macho_file.getSymbol(sym_index); |
| 782 | 789 | try self.dwarf.?.commitDeclState( |
| 783 | mod, | |
| 790 | pt, | |
| 784 | 791 | decl_index, |
| 785 | 792 | sym.getAddress(.{}, macho_file), |
| 786 | 793 | sym.getAtom(macho_file).?.size, |
| ... | ... | @@ -794,19 +801,20 @@ pub fn updateDecl( |
| 794 | 801 | fn updateDeclCode( |
| 795 | 802 | self: *ZigObject, |
| 796 | 803 | macho_file: *MachO, |
| 804 | pt: Zcu.PerThread, | |
| 797 | 805 | decl_index: InternPool.DeclIndex, |
| 798 | 806 | sym_index: Symbol.Index, |
| 799 | 807 | sect_index: u8, |
| 800 | 808 | code: []const u8, |
| 801 | 809 | ) !void { |
| 802 | 810 | const gpa = macho_file.base.comp.gpa; |
| 803 | const mod = macho_file.base.comp.module.?; | |
| 811 | const mod = pt.zcu; | |
| 804 | 812 | const decl = mod.declPtr(decl_index); |
| 805 | 813 | const decl_name = try decl.fullyQualifiedName(mod); |
| 806 | 814 | |
| 807 | 815 | log.debug("updateDeclCode {}{*}", .{ decl_name.fmt(&mod.intern_pool), decl }); |
| 808 | 816 | |
| 809 | const required_alignment = decl.getAlignment(mod); | |
| 817 | const required_alignment = decl.getAlignment(pt); | |
| 810 | 818 | |
| 811 | 819 | const sect = &macho_file.sections.items(.header)[sect_index]; |
| 812 | 820 | const sym = macho_file.getSymbol(sym_index); |
| ... | ... | @@ -879,19 +887,20 @@ fn updateDeclCode( |
| 879 | 887 | fn updateTlv( |
| 880 | 888 | self: *ZigObject, |
| 881 | 889 | macho_file: *MachO, |
| 890 | pt: Zcu.PerThread, | |
| 882 | 891 | decl_index: InternPool.DeclIndex, |
| 883 | 892 | sym_index: Symbol.Index, |
| 884 | 893 | sect_index: u8, |
| 885 | 894 | code: []const u8, |
| 886 | 895 | ) !void { |
| 887 | const mod = macho_file.base.comp.module.?; | |
| 896 | const mod = pt.zcu; | |
| 888 | 897 | const decl = mod.declPtr(decl_index); |
| 889 | 898 | const decl_name = try decl.fullyQualifiedName(mod); |
| 890 | 899 | |
| 891 | 900 | log.debug("updateTlv {} ({*})", .{ decl_name.fmt(&mod.intern_pool), decl }); |
| 892 | 901 | |
| 893 | 902 | const decl_name_slice = decl_name.toSlice(&mod.intern_pool); |
| 894 | const required_alignment = decl.getAlignment(mod); | |
| 903 | const required_alignment = decl.getAlignment(pt); | |
| 895 | 904 | |
| 896 | 905 | // 1. Lower TLV initializer |
| 897 | 906 | const init_sym_index = try self.createTlvInitializer( |
| ... | ... | @@ -1079,11 +1088,12 @@ fn getDeclOutputSection( |
| 1079 | 1088 | pub fn lowerUnnamedConst( |
| 1080 | 1089 | self: *ZigObject, |
| 1081 | 1090 | macho_file: *MachO, |
| 1091 | pt: Zcu.PerThread, | |
| 1082 | 1092 | val: Value, |
| 1083 | 1093 | decl_index: InternPool.DeclIndex, |
| 1084 | 1094 | ) !u32 { |
| 1085 | const gpa = macho_file.base.comp.gpa; | |
| 1086 | const mod = macho_file.base.comp.module.?; | |
| 1095 | const mod = pt.zcu; | |
| 1096 | const gpa = mod.gpa; | |
| 1087 | 1097 | const gop = try self.unnamed_consts.getOrPut(gpa, decl_index); |
| 1088 | 1098 | if (!gop.found_existing) { |
| 1089 | 1099 | gop.value_ptr.* = .{}; |
| ... | ... | @@ -1096,9 +1106,10 @@ pub fn lowerUnnamedConst( |
| 1096 | 1106 | defer gpa.free(name); |
| 1097 | 1107 | const sym_index = switch (try self.lowerConst( |
| 1098 | 1108 | macho_file, |
| 1109 | pt, | |
| 1099 | 1110 | name, |
| 1100 | 1111 | val, |
| 1101 | val.typeOf(mod).abiAlignment(mod), | |
| 1112 | val.typeOf(mod).abiAlignment(pt), | |
| 1102 | 1113 | macho_file.zig_const_sect_index.?, |
| 1103 | 1114 | decl.navSrcLoc(mod), |
| 1104 | 1115 | )) { |
| ... | ... | @@ -1123,6 +1134,7 @@ const LowerConstResult = union(enum) { |
| 1123 | 1134 | fn lowerConst( |
| 1124 | 1135 | self: *ZigObject, |
| 1125 | 1136 | macho_file: *MachO, |
| 1137 | pt: Zcu.PerThread, | |
| 1126 | 1138 | name: []const u8, |
| 1127 | 1139 | val: Value, |
| 1128 | 1140 | required_alignment: Atom.Alignment, |
| ... | ... | @@ -1136,7 +1148,7 @@ fn lowerConst( |
| 1136 | 1148 | |
| 1137 | 1149 | const sym_index = try self.addAtom(macho_file); |
| 1138 | 1150 | |
| 1139 | const res = try codegen.generateSymbol(&macho_file.base, src_loc, val, &code_buffer, .{ | |
| 1151 | const res = try codegen.generateSymbol(&macho_file.base, pt, src_loc, val, &code_buffer, .{ | |
| 1140 | 1152 | .none = {}, |
| 1141 | 1153 | }, .{ |
| 1142 | 1154 | .parent_atom_index = sym_index, |
| ... | ... | @@ -1181,13 +1193,14 @@ fn lowerConst( |
| 1181 | 1193 | pub fn updateExports( |
| 1182 | 1194 | self: *ZigObject, |
| 1183 | 1195 | macho_file: *MachO, |
| 1184 | mod: *Module, | |
| 1196 | pt: Zcu.PerThread, | |
| 1185 | 1197 | exported: Module.Exported, |
| 1186 | 1198 | export_indices: []const u32, |
| 1187 | 1199 | ) link.File.UpdateExportsError!void { |
| 1188 | 1200 | const tracy = trace(@src()); |
| 1189 | 1201 | defer tracy.end(); |
| 1190 | 1202 | |
| 1203 | const mod = pt.zcu; | |
| 1191 | 1204 | const gpa = macho_file.base.comp.gpa; |
| 1192 | 1205 | const metadata = switch (exported) { |
| 1193 | 1206 | .decl_index => |decl_index| blk: { |
| ... | ... | @@ -1196,7 +1209,7 @@ pub fn updateExports( |
| 1196 | 1209 | }, |
| 1197 | 1210 | .value => |value| self.anon_decls.getPtr(value) orelse blk: { |
| 1198 | 1211 | const first_exp = mod.all_exports.items[export_indices[0]]; |
| 1199 | const res = try self.lowerAnonDecl(macho_file, value, .none, first_exp.src); | |
| 1212 | const res = try self.lowerAnonDecl(macho_file, pt, value, .none, first_exp.src); | |
| 1200 | 1213 | switch (res) { |
| 1201 | 1214 | .ok => {}, |
| 1202 | 1215 | .fail => |em| { |
| ... | ... | @@ -1272,6 +1285,7 @@ pub fn updateExports( |
| 1272 | 1285 | fn updateLazySymbol( |
| 1273 | 1286 | self: *ZigObject, |
| 1274 | 1287 | macho_file: *MachO, |
| 1288 | pt: Zcu.PerThread, | |
| 1275 | 1289 | lazy_sym: link.File.LazySymbol, |
| 1276 | 1290 | symbol_index: Symbol.Index, |
| 1277 | 1291 | ) !void { |
| ... | ... | @@ -1285,7 +1299,7 @@ fn updateLazySymbol( |
| 1285 | 1299 | const name_str_index = blk: { |
| 1286 | 1300 | const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{}", .{ |
| 1287 | 1301 | @tagName(lazy_sym.kind), |
| 1288 | lazy_sym.ty.fmt(mod), | |
| 1302 | lazy_sym.ty.fmt(pt), | |
| 1289 | 1303 | }); |
| 1290 | 1304 | defer gpa.free(name); |
| 1291 | 1305 | break :blk try self.strtab.insert(gpa, name); |
| ... | ... | @@ -1294,6 +1308,7 @@ fn updateLazySymbol( |
| 1294 | 1308 | const src = lazy_sym.ty.srcLocOrNull(mod) orelse Module.LazySrcLoc.unneeded; |
| 1295 | 1309 | const res = try codegen.generateLazySymbol( |
| 1296 | 1310 | &macho_file.base, |
| 1311 | pt, | |
| 1297 | 1312 | src, |
| 1298 | 1313 | lazy_sym, |
| 1299 | 1314 | &required_alignment, |
| ... | ... | @@ -1431,10 +1446,11 @@ pub fn getOrCreateMetadataForDecl( |
| 1431 | 1446 | pub fn getOrCreateMetadataForLazySymbol( |
| 1432 | 1447 | self: *ZigObject, |
| 1433 | 1448 | macho_file: *MachO, |
| 1449 | pt: Zcu.PerThread, | |
| 1434 | 1450 | lazy_sym: link.File.LazySymbol, |
| 1435 | 1451 | ) !Symbol.Index { |
| 1436 | const gpa = macho_file.base.comp.gpa; | |
| 1437 | const mod = macho_file.base.comp.module.?; | |
| 1452 | const mod = pt.zcu; | |
| 1453 | const gpa = mod.gpa; | |
| 1438 | 1454 | const gop = try self.lazy_syms.getOrPut(gpa, lazy_sym.getDecl(mod)); |
| 1439 | 1455 | errdefer _ = if (!gop.found_existing) self.lazy_syms.pop(); |
| 1440 | 1456 | if (!gop.found_existing) gop.value_ptr.* = .{}; |
| ... | ... | @@ -1464,7 +1480,7 @@ pub fn getOrCreateMetadataForLazySymbol( |
| 1464 | 1480 | metadata.state.* = .pending_flush; |
| 1465 | 1481 | const symbol_index = metadata.symbol_index.*; |
| 1466 | 1482 | // anyerror needs to be deferred until flushModule |
| 1467 | if (lazy_sym.getDecl(mod) != .none) try self.updateLazySymbol(macho_file, lazy_sym, symbol_index); | |
| 1483 | if (lazy_sym.getDecl(mod) != .none) try self.updateLazySymbol(macho_file, pt, lazy_sym, symbol_index); | |
| 1468 | 1484 | return symbol_index; |
| 1469 | 1485 | } |
| 1470 | 1486 |
src/link/NvPtx.zig+11-12| ... | ... | @@ -13,8 +13,6 @@ const assert = std.debug.assert; |
| 13 | 13 | const log = std.log.scoped(.link); |
| 14 | 14 | |
| 15 | 15 | const Zcu = @import("../Zcu.zig"); |
| 16 | /// Deprecated. | |
| 17 | const Module = Zcu; | |
| 18 | 16 | const InternPool = @import("../InternPool.zig"); |
| 19 | 17 | const Compilation = @import("../Compilation.zig"); |
| 20 | 18 | const link = @import("../link.zig"); |
| ... | ... | @@ -84,35 +82,35 @@ pub fn deinit(self: *NvPtx) void { |
| 84 | 82 | self.llvm_object.deinit(); |
| 85 | 83 | } |
| 86 | 84 | |
| 87 | pub fn updateFunc(self: *NvPtx, module: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 88 | try self.llvm_object.updateFunc(module, func_index, air, liveness); | |
| 85 | pub fn updateFunc(self: *NvPtx, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 86 | try self.llvm_object.updateFunc(pt, func_index, air, liveness); | |
| 89 | 87 | } |
| 90 | 88 | |
| 91 | pub fn updateDecl(self: *NvPtx, module: *Module, decl_index: InternPool.DeclIndex) !void { | |
| 92 | return self.llvm_object.updateDecl(module, decl_index); | |
| 89 | pub fn updateDecl(self: *NvPtx, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex) !void { | |
| 90 | return self.llvm_object.updateDecl(pt, decl_index); | |
| 93 | 91 | } |
| 94 | 92 | |
| 95 | 93 | pub fn updateExports( |
| 96 | 94 | self: *NvPtx, |
| 97 | module: *Module, | |
| 98 | exported: Module.Exported, | |
| 95 | pt: Zcu.PerThread, | |
| 96 | exported: Zcu.Exported, | |
| 99 | 97 | export_indices: []const u32, |
| 100 | 98 | ) !void { |
| 101 | 99 | if (build_options.skip_non_native and builtin.object_format != .nvptx) |
| 102 | 100 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 103 | 101 | |
| 104 | return self.llvm_object.updateExports(module, exported, export_indices); | |
| 102 | return self.llvm_object.updateExports(pt, exported, export_indices); | |
| 105 | 103 | } |
| 106 | 104 | |
| 107 | 105 | pub fn freeDecl(self: *NvPtx, decl_index: InternPool.DeclIndex) void { |
| 108 | 106 | return self.llvm_object.freeDecl(decl_index); |
| 109 | 107 | } |
| 110 | 108 | |
| 111 | pub fn flush(self: *NvPtx, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 112 | return self.flushModule(arena, prog_node); | |
| 109 | pub fn flush(self: *NvPtx, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 110 | return self.flushModule(arena, tid, prog_node); | |
| 113 | 111 | } |
| 114 | 112 | |
| 115 | pub fn flushModule(self: *NvPtx, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 113 | pub fn flushModule(self: *NvPtx, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 116 | 114 | if (build_options.skip_non_native) |
| 117 | 115 | @panic("Attempted to compile for architecture that was disabled by build configuration"); |
| 118 | 116 | |
| ... | ... | @@ -121,5 +119,6 @@ pub fn flushModule(self: *NvPtx, arena: Allocator, prog_node: std.Progress.Node) |
| 121 | 119 | _ = arena; |
| 122 | 120 | _ = self; |
| 123 | 121 | _ = prog_node; |
| 122 | _ = tid; | |
| 124 | 123 | @panic("TODO: rewrite the NvPtx.flushModule function"); |
| 125 | 124 | } |
src/link/Plan9.zig+47-40| ... | ... | @@ -4,8 +4,6 @@ |
| 4 | 4 | const Plan9 = @This(); |
| 5 | 5 | const link = @import("../link.zig"); |
| 6 | 6 | const Zcu = @import("../Zcu.zig"); |
| 7 | /// Deprecated. | |
| 8 | const Module = Zcu; | |
| 9 | 7 | const InternPool = @import("../InternPool.zig"); |
| 10 | 8 | const Compilation = @import("../Compilation.zig"); |
| 11 | 9 | const aout = @import("Plan9/aout.zig"); |
| ... | ... | @@ -56,7 +54,7 @@ path_arena: std.heap.ArenaAllocator, |
| 56 | 54 | /// of the function to know what file it came from. |
| 57 | 55 | /// If we group the decls by file, it makes it really easy to do this (put the symbol in the correct place) |
| 58 | 56 | fn_decl_table: std.AutoArrayHashMapUnmanaged( |
| 59 | *Module.File, | |
| 57 | *Zcu.File, | |
| 60 | 58 | struct { sym_index: u32, functions: std.AutoArrayHashMapUnmanaged(InternPool.DeclIndex, FnDeclOutput) = .{} }, |
| 61 | 59 | ) = .{}, |
| 62 | 60 | /// the code is modified when relocated, so that is why it is mutable |
| ... | ... | @@ -411,12 +409,13 @@ fn addPathComponents(self: *Plan9, path: []const u8, a: *std.ArrayList(u8)) !voi |
| 411 | 409 | } |
| 412 | 410 | } |
| 413 | 411 | |
| 414 | pub fn updateFunc(self: *Plan9, mod: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 412 | pub fn updateFunc(self: *Plan9, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 415 | 413 | if (build_options.skip_non_native and builtin.object_format != .plan9) { |
| 416 | 414 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 417 | 415 | } |
| 418 | 416 | |
| 419 | const gpa = self.base.comp.gpa; | |
| 417 | const mod = pt.zcu; | |
| 418 | const gpa = mod.gpa; | |
| 420 | 419 | const target = self.base.comp.root_mod.resolved_target.result; |
| 421 | 420 | const func = mod.funcInfo(func_index); |
| 422 | 421 | const decl_index = func.owner_decl; |
| ... | ... | @@ -439,6 +438,7 @@ pub fn updateFunc(self: *Plan9, mod: *Module, func_index: InternPool.Index, air: |
| 439 | 438 | |
| 440 | 439 | const res = try codegen.generateFunction( |
| 441 | 440 | &self.base, |
| 441 | pt, | |
| 442 | 442 | decl.navSrcLoc(mod), |
| 443 | 443 | func_index, |
| 444 | 444 | air, |
| ... | ... | @@ -468,13 +468,13 @@ pub fn updateFunc(self: *Plan9, mod: *Module, func_index: InternPool.Index, air: |
| 468 | 468 | return self.updateFinish(decl_index); |
| 469 | 469 | } |
| 470 | 470 | |
| 471 | pub fn lowerUnnamedConst(self: *Plan9, val: Value, decl_index: InternPool.DeclIndex) !u32 { | |
| 472 | const gpa = self.base.comp.gpa; | |
| 471 | pub fn lowerUnnamedConst(self: *Plan9, pt: Zcu.PerThread, val: Value, decl_index: InternPool.DeclIndex) !u32 { | |
| 472 | const mod = pt.zcu; | |
| 473 | const gpa = mod.gpa; | |
| 473 | 474 | _ = try self.seeDecl(decl_index); |
| 474 | 475 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 475 | 476 | defer code_buffer.deinit(); |
| 476 | 477 | |
| 477 | const mod = self.base.comp.module.?; | |
| 478 | 478 | const decl = mod.declPtr(decl_index); |
| 479 | 479 | |
| 480 | 480 | const gop = try self.unnamed_const_atoms.getOrPut(gpa, decl_index); |
| ... | ... | @@ -505,7 +505,7 @@ pub fn lowerUnnamedConst(self: *Plan9, val: Value, decl_index: InternPool.DeclIn |
| 505 | 505 | }; |
| 506 | 506 | self.syms.items[info.sym_index.?] = sym; |
| 507 | 507 | |
| 508 | const res = try codegen.generateSymbol(&self.base, decl.navSrcLoc(mod), val, &code_buffer, .{ | |
| 508 | const res = try codegen.generateSymbol(&self.base, pt, decl.navSrcLoc(mod), val, &code_buffer, .{ | |
| 509 | 509 | .none = {}, |
| 510 | 510 | }, .{ |
| 511 | 511 | .parent_atom_index = new_atom_idx, |
| ... | ... | @@ -530,8 +530,9 @@ pub fn lowerUnnamedConst(self: *Plan9, val: Value, decl_index: InternPool.DeclIn |
| 530 | 530 | return new_atom_idx; |
| 531 | 531 | } |
| 532 | 532 | |
| 533 | pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: InternPool.DeclIndex) !void { | |
| 533 | pub fn updateDecl(self: *Plan9, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex) !void { | |
| 534 | 534 | const gpa = self.base.comp.gpa; |
| 535 | const mod = pt.zcu; | |
| 535 | 536 | const decl = mod.declPtr(decl_index); |
| 536 | 537 | |
| 537 | 538 | if (decl.isExtern(mod)) { |
| ... | ... | @@ -544,7 +545,7 @@ pub fn updateDecl(self: *Plan9, mod: *Module, decl_index: InternPool.DeclIndex) |
| 544 | 545 | defer code_buffer.deinit(); |
| 545 | 546 | const decl_val = if (decl.val.getVariable(mod)) |variable| Value.fromInterned(variable.init) else decl.val; |
| 546 | 547 | // TODO we need the symbol index for symbol in the table of locals for the containing atom |
| 547 | const res = try codegen.generateSymbol(&self.base, decl.navSrcLoc(mod), decl_val, &code_buffer, .{ .none = {} }, .{ | |
| 548 | const res = try codegen.generateSymbol(&self.base, pt, decl.navSrcLoc(mod), decl_val, &code_buffer, .{ .none = {} }, .{ | |
| 548 | 549 | .parent_atom_index = @as(Atom.Index, @intCast(atom_idx)), |
| 549 | 550 | }); |
| 550 | 551 | const code = switch (res) { |
| ... | ... | @@ -610,7 +611,7 @@ fn allocateGotIndex(self: *Plan9) usize { |
| 610 | 611 | } |
| 611 | 612 | } |
| 612 | 613 | |
| 613 | pub fn flush(self: *Plan9, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 614 | pub fn flush(self: *Plan9, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 614 | 615 | const comp = self.base.comp; |
| 615 | 616 | const use_lld = build_options.have_llvm and comp.config.use_lld; |
| 616 | 617 | assert(!use_lld); |
| ... | ... | @@ -621,7 +622,7 @@ pub fn flush(self: *Plan9, arena: Allocator, prog_node: std.Progress.Node) link. |
| 621 | 622 | .Obj => return error.TODOImplementPlan9Objs, |
| 622 | 623 | .Lib => return error.TODOImplementWritingLibFiles, |
| 623 | 624 | } |
| 624 | return self.flushModule(arena, prog_node); | |
| 625 | return self.flushModule(arena, tid, prog_node); | |
| 625 | 626 | } |
| 626 | 627 | |
| 627 | 628 | pub fn changeLine(l: *std.ArrayList(u8), delta_line: i32) !void { |
| ... | ... | @@ -669,20 +670,20 @@ fn atomCount(self: *Plan9) usize { |
| 669 | 670 | return data_decl_count + fn_decl_count + unnamed_const_count + lazy_atom_count + extern_atom_count + anon_atom_count; |
| 670 | 671 | } |
| 671 | 672 | |
| 672 | pub fn flushModule(self: *Plan9, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 673 | pub fn flushModule(self: *Plan9, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 673 | 674 | if (build_options.skip_non_native and builtin.object_format != .plan9) { |
| 674 | 675 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 675 | 676 | } |
| 676 | 677 | |
| 678 | const tracy = trace(@src()); | |
| 679 | defer tracy.end(); | |
| 680 | ||
| 677 | 681 | _ = arena; // Has the same lifetime as the call to Compilation.update. |
| 678 | 682 | |
| 679 | 683 | const comp = self.base.comp; |
| 680 | 684 | const gpa = comp.gpa; |
| 681 | 685 | const target = comp.root_mod.resolved_target.result; |
| 682 | 686 | |
| 683 | const tracy = trace(@src()); | |
| 684 | defer tracy.end(); | |
| 685 | ||
| 686 | 687 | const sub_prog_node = prog_node.start("Flush Module", 0); |
| 687 | 688 | defer sub_prog_node.end(); |
| 688 | 689 | |
| ... | ... | @@ -690,21 +691,26 @@ pub fn flushModule(self: *Plan9, arena: Allocator, prog_node: std.Progress.Node) |
| 690 | 691 | |
| 691 | 692 | defer assert(self.hdr.entry != 0x0); |
| 692 | 693 | |
| 693 | const mod = self.base.comp.module orelse return error.LinkingWithoutZigSourceUnimplemented; | |
| 694 | const pt: Zcu.PerThread = .{ | |
| 695 | .zcu = self.base.comp.module orelse return error.LinkingWithoutZigSourceUnimplemented, | |
| 696 | .tid = tid, | |
| 697 | }; | |
| 694 | 698 | |
| 695 | 699 | // finish up the lazy syms |
| 696 | 700 | if (self.lazy_syms.getPtr(.none)) |metadata| { |
| 697 | 701 | // Most lazy symbols can be updated on first use, but |
| 698 | 702 | // anyerror needs to wait for everything to be flushed. |
| 699 | 703 | if (metadata.text_state != .unused) self.updateLazySymbolAtom( |
| 700 | File.LazySymbol.initDecl(.code, null, mod), | |
| 704 | pt, | |
| 705 | File.LazySymbol.initDecl(.code, null, pt.zcu), | |
| 701 | 706 | metadata.text_atom, |
| 702 | 707 | ) catch |err| return switch (err) { |
| 703 | 708 | error.CodegenFail => error.FlushFailure, |
| 704 | 709 | else => |e| e, |
| 705 | 710 | }; |
| 706 | 711 | if (metadata.rodata_state != .unused) self.updateLazySymbolAtom( |
| 707 | File.LazySymbol.initDecl(.const_data, null, mod), | |
| 712 | pt, | |
| 713 | File.LazySymbol.initDecl(.const_data, null, pt.zcu), | |
| 708 | 714 | metadata.rodata_atom, |
| 709 | 715 | ) catch |err| return switch (err) { |
| 710 | 716 | error.CodegenFail => error.FlushFailure, |
| ... | ... | @@ -747,7 +753,7 @@ pub fn flushModule(self: *Plan9, arena: Allocator, prog_node: std.Progress.Node) |
| 747 | 753 | var it = fentry.value_ptr.functions.iterator(); |
| 748 | 754 | while (it.next()) |entry| { |
| 749 | 755 | const decl_index = entry.key_ptr.*; |
| 750 | const decl = mod.declPtr(decl_index); | |
| 756 | const decl = pt.zcu.declPtr(decl_index); | |
| 751 | 757 | const atom = self.getAtomPtr(self.decls.get(decl_index).?.index); |
| 752 | 758 | const out = entry.value_ptr.*; |
| 753 | 759 | { |
| ... | ... | @@ -767,7 +773,7 @@ pub fn flushModule(self: *Plan9, arena: Allocator, prog_node: std.Progress.Node) |
| 767 | 773 | const off = self.getAddr(text_i, .t); |
| 768 | 774 | text_i += out.code.len; |
| 769 | 775 | atom.offset = off; |
| 770 | log.debug("write text decl {*} ({}), lines {d} to {d}.;__GOT+0x{x} vaddr: 0x{x}", .{ decl, decl.name.fmt(&mod.intern_pool), out.start_line + 1, out.end_line, atom.got_index.? * 8, off }); | |
| 776 | log.debug("write text decl {*} ({}), lines {d} to {d}.;__GOT+0x{x} vaddr: 0x{x}", .{ decl, decl.name.fmt(&pt.zcu.intern_pool), out.start_line + 1, out.end_line, atom.got_index.? * 8, off }); | |
| 771 | 777 | if (!self.sixtyfour_bit) { |
| 772 | 778 | mem.writeInt(u32, got_table[atom.got_index.? * 4 ..][0..4], @as(u32, @intCast(off)), target.cpu.arch.endian()); |
| 773 | 779 | } else { |
| ... | ... | @@ -775,7 +781,7 @@ pub fn flushModule(self: *Plan9, arena: Allocator, prog_node: std.Progress.Node) |
| 775 | 781 | } |
| 776 | 782 | self.syms.items[atom.sym_index.?].value = off; |
| 777 | 783 | if (self.decl_exports.get(decl_index)) |export_indices| { |
| 778 | try self.addDeclExports(mod, decl_index, export_indices); | |
| 784 | try self.addDeclExports(pt.zcu, decl_index, export_indices); | |
| 779 | 785 | } |
| 780 | 786 | } |
| 781 | 787 | } |
| ... | ... | @@ -841,7 +847,7 @@ pub fn flushModule(self: *Plan9, arena: Allocator, prog_node: std.Progress.Node) |
| 841 | 847 | } |
| 842 | 848 | self.syms.items[atom.sym_index.?].value = off; |
| 843 | 849 | if (self.decl_exports.get(decl_index)) |export_indices| { |
| 844 | try self.addDeclExports(mod, decl_index, export_indices); | |
| 850 | try self.addDeclExports(pt.zcu, decl_index, export_indices); | |
| 845 | 851 | } |
| 846 | 852 | } |
| 847 | 853 | // write the unnamed constants after the other data decls |
| ... | ... | @@ -1009,7 +1015,7 @@ pub fn flushModule(self: *Plan9, arena: Allocator, prog_node: std.Progress.Node) |
| 1009 | 1015 | } |
| 1010 | 1016 | fn addDeclExports( |
| 1011 | 1017 | self: *Plan9, |
| 1012 | mod: *Module, | |
| 1018 | mod: *Zcu, | |
| 1013 | 1019 | decl_index: InternPool.DeclIndex, |
| 1014 | 1020 | export_indices: []const u32, |
| 1015 | 1021 | ) !void { |
| ... | ... | @@ -1025,7 +1031,7 @@ fn addDeclExports( |
| 1025 | 1031 | if (!section_name.eqlSlice(".text", &mod.intern_pool) and |
| 1026 | 1032 | !section_name.eqlSlice(".data", &mod.intern_pool)) |
| 1027 | 1033 | { |
| 1028 | try mod.failed_exports.put(mod.gpa, export_idx, try Module.ErrorMsg.create( | |
| 1034 | try mod.failed_exports.put(mod.gpa, export_idx, try Zcu.ErrorMsg.create( | |
| 1029 | 1035 | gpa, |
| 1030 | 1036 | mod.declPtr(decl_index).navSrcLoc(mod), |
| 1031 | 1037 | "plan9 does not support extra sections", |
| ... | ... | @@ -1155,8 +1161,8 @@ pub fn seeDecl(self: *Plan9, decl_index: InternPool.DeclIndex) !Atom.Index { |
| 1155 | 1161 | |
| 1156 | 1162 | pub fn updateExports( |
| 1157 | 1163 | self: *Plan9, |
| 1158 | module: *Module, | |
| 1159 | exported: Module.Exported, | |
| 1164 | pt: Zcu.PerThread, | |
| 1165 | exported: Zcu.Exported, | |
| 1160 | 1166 | export_indices: []const u32, |
| 1161 | 1167 | ) !void { |
| 1162 | 1168 | const gpa = self.base.comp.gpa; |
| ... | ... | @@ -1173,11 +1179,11 @@ pub fn updateExports( |
| 1173 | 1179 | }, |
| 1174 | 1180 | } |
| 1175 | 1181 | // all proper work is done in flush |
| 1176 | _ = module; | |
| 1182 | _ = pt; | |
| 1177 | 1183 | } |
| 1178 | 1184 | |
| 1179 | pub fn getOrCreateAtomForLazySymbol(self: *Plan9, sym: File.LazySymbol) !Atom.Index { | |
| 1180 | const gpa = self.base.comp.gpa; | |
| 1185 | pub fn getOrCreateAtomForLazySymbol(self: *Plan9, pt: Zcu.PerThread, sym: File.LazySymbol) !Atom.Index { | |
| 1186 | const gpa = pt.zcu.gpa; | |
| 1181 | 1187 | const gop = try self.lazy_syms.getOrPut(gpa, sym.getDecl(self.base.comp.module.?)); |
| 1182 | 1188 | errdefer _ = if (!gop.found_existing) self.lazy_syms.pop(); |
| 1183 | 1189 | |
| ... | ... | @@ -1198,14 +1204,13 @@ pub fn getOrCreateAtomForLazySymbol(self: *Plan9, sym: File.LazySymbol) !Atom.In |
| 1198 | 1204 | _ = self.getAtomPtr(atom).getOrCreateOffsetTableEntry(self); |
| 1199 | 1205 | // anyerror needs to be deferred until flushModule |
| 1200 | 1206 | if (sym.getDecl(self.base.comp.module.?) != .none) { |
| 1201 | try self.updateLazySymbolAtom(sym, atom); | |
| 1207 | try self.updateLazySymbolAtom(pt, sym, atom); | |
| 1202 | 1208 | } |
| 1203 | 1209 | return atom; |
| 1204 | 1210 | } |
| 1205 | 1211 | |
| 1206 | fn updateLazySymbolAtom(self: *Plan9, sym: File.LazySymbol, atom_index: Atom.Index) !void { | |
| 1207 | const gpa = self.base.comp.gpa; | |
| 1208 | const mod = self.base.comp.module.?; | |
| 1212 | fn updateLazySymbolAtom(self: *Plan9, pt: Zcu.PerThread, sym: File.LazySymbol, atom_index: Atom.Index) !void { | |
| 1213 | const gpa = pt.zcu.gpa; | |
| 1209 | 1214 | |
| 1210 | 1215 | var required_alignment: InternPool.Alignment = .none; |
| 1211 | 1216 | var code_buffer = std.ArrayList(u8).init(gpa); |
| ... | ... | @@ -1214,7 +1219,7 @@ fn updateLazySymbolAtom(self: *Plan9, sym: File.LazySymbol, atom_index: Atom.Ind |
| 1214 | 1219 | // create the symbol for the name |
| 1215 | 1220 | const name = try std.fmt.allocPrint(gpa, "__lazy_{s}_{}", .{ |
| 1216 | 1221 | @tagName(sym.kind), |
| 1217 | sym.ty.fmt(mod), | |
| 1222 | sym.ty.fmt(pt), | |
| 1218 | 1223 | }); |
| 1219 | 1224 | |
| 1220 | 1225 | const symbol: aout.Sym = .{ |
| ... | ... | @@ -1225,9 +1230,10 @@ fn updateLazySymbolAtom(self: *Plan9, sym: File.LazySymbol, atom_index: Atom.Ind |
| 1225 | 1230 | self.syms.items[self.getAtomPtr(atom_index).sym_index.?] = symbol; |
| 1226 | 1231 | |
| 1227 | 1232 | // generate the code |
| 1228 | const src = sym.ty.srcLocOrNull(mod) orelse Module.LazySrcLoc.unneeded; | |
| 1233 | const src = sym.ty.srcLocOrNull(pt.zcu) orelse Zcu.LazySrcLoc.unneeded; | |
| 1229 | 1234 | const res = try codegen.generateLazySymbol( |
| 1230 | 1235 | &self.base, |
| 1236 | pt, | |
| 1231 | 1237 | src, |
| 1232 | 1238 | sym, |
| 1233 | 1239 | &required_alignment, |
| ... | ... | @@ -1490,7 +1496,7 @@ pub fn writeSyms(self: *Plan9, buf: *std.ArrayList(u8)) !void { |
| 1490 | 1496 | } |
| 1491 | 1497 | |
| 1492 | 1498 | /// Must be called only after a successful call to `updateDecl`. |
| 1493 | pub fn updateDeclLineNumber(self: *Plan9, mod: *Module, decl_index: InternPool.DeclIndex) !void { | |
| 1499 | pub fn updateDeclLineNumber(self: *Plan9, mod: *Zcu, decl_index: InternPool.DeclIndex) !void { | |
| 1494 | 1500 | _ = self; |
| 1495 | 1501 | _ = mod; |
| 1496 | 1502 | _ = decl_index; |
| ... | ... | @@ -1544,9 +1550,10 @@ pub fn getDeclVAddr( |
| 1544 | 1550 | |
| 1545 | 1551 | pub fn lowerAnonDecl( |
| 1546 | 1552 | self: *Plan9, |
| 1553 | pt: Zcu.PerThread, | |
| 1547 | 1554 | decl_val: InternPool.Index, |
| 1548 | 1555 | explicit_alignment: InternPool.Alignment, |
| 1549 | src_loc: Module.LazySrcLoc, | |
| 1556 | src_loc: Zcu.LazySrcLoc, | |
| 1550 | 1557 | ) !codegen.Result { |
| 1551 | 1558 | _ = explicit_alignment; |
| 1552 | 1559 | // This is basically the same as lowerUnnamedConst. |
| ... | ... | @@ -1569,7 +1576,7 @@ pub fn lowerAnonDecl( |
| 1569 | 1576 | gop.value_ptr.* = index; |
| 1570 | 1577 | // we need to free name latex |
| 1571 | 1578 | var code_buffer = std.ArrayList(u8).init(gpa); |
| 1572 | const res = try codegen.generateSymbol(&self.base, src_loc, val, &code_buffer, .{ .none = {} }, .{ .parent_atom_index = index }); | |
| 1579 | const res = try codegen.generateSymbol(&self.base, pt, src_loc, val, &code_buffer, .{ .none = {} }, .{ .parent_atom_index = index }); | |
| 1573 | 1580 | const code = switch (res) { |
| 1574 | 1581 | .ok => code_buffer.items, |
| 1575 | 1582 | .fail => |em| return .{ .fail = em }, |
src/link/SpirV.zig+16-16| ... | ... | @@ -28,8 +28,6 @@ const assert = std.debug.assert; |
| 28 | 28 | const log = std.log.scoped(.link); |
| 29 | 29 | |
| 30 | 30 | const Zcu = @import("../Zcu.zig"); |
| 31 | /// Deprecated. | |
| 32 | const Module = Zcu; | |
| 33 | 31 | const InternPool = @import("../InternPool.zig"); |
| 34 | 32 | const Compilation = @import("../Compilation.zig"); |
| 35 | 33 | const link = @import("../link.zig"); |
| ... | ... | @@ -125,35 +123,36 @@ pub fn deinit(self: *SpirV) void { |
| 125 | 123 | self.object.deinit(); |
| 126 | 124 | } |
| 127 | 125 | |
| 128 | pub fn updateFunc(self: *SpirV, module: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 126 | pub fn updateFunc(self: *SpirV, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 129 | 127 | if (build_options.skip_non_native) { |
| 130 | 128 | @panic("Attempted to compile for architecture that was disabled by build configuration"); |
| 131 | 129 | } |
| 132 | 130 | |
| 133 | const func = module.funcInfo(func_index); | |
| 134 | const decl = module.declPtr(func.owner_decl); | |
| 135 | log.debug("lowering function {}", .{decl.name.fmt(&module.intern_pool)}); | |
| 131 | const func = pt.zcu.funcInfo(func_index); | |
| 132 | const decl = pt.zcu.declPtr(func.owner_decl); | |
| 133 | log.debug("lowering function {}", .{decl.name.fmt(&pt.zcu.intern_pool)}); | |
| 136 | 134 | |
| 137 | try self.object.updateFunc(module, func_index, air, liveness); | |
| 135 | try self.object.updateFunc(pt, func_index, air, liveness); | |
| 138 | 136 | } |
| 139 | 137 | |
| 140 | pub fn updateDecl(self: *SpirV, module: *Module, decl_index: InternPool.DeclIndex) !void { | |
| 138 | pub fn updateDecl(self: *SpirV, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex) !void { | |
| 141 | 139 | if (build_options.skip_non_native) { |
| 142 | 140 | @panic("Attempted to compile for architecture that was disabled by build configuration"); |
| 143 | 141 | } |
| 144 | 142 | |
| 145 | const decl = module.declPtr(decl_index); | |
| 146 | log.debug("lowering declaration {}", .{decl.name.fmt(&module.intern_pool)}); | |
| 143 | const decl = pt.zcu.declPtr(decl_index); | |
| 144 | log.debug("lowering declaration {}", .{decl.name.fmt(&pt.zcu.intern_pool)}); | |
| 147 | 145 | |
| 148 | try self.object.updateDecl(module, decl_index); | |
| 146 | try self.object.updateDecl(pt, decl_index); | |
| 149 | 147 | } |
| 150 | 148 | |
| 151 | 149 | pub fn updateExports( |
| 152 | 150 | self: *SpirV, |
| 153 | mod: *Module, | |
| 154 | exported: Module.Exported, | |
| 151 | pt: Zcu.PerThread, | |
| 152 | exported: Zcu.Exported, | |
| 155 | 153 | export_indices: []const u32, |
| 156 | 154 | ) !void { |
| 155 | const mod = pt.zcu; | |
| 157 | 156 | const decl_index = switch (exported) { |
| 158 | 157 | .decl_index => |i| i, |
| 159 | 158 | .value => |val| { |
| ... | ... | @@ -196,11 +195,11 @@ pub fn freeDecl(self: *SpirV, decl_index: InternPool.DeclIndex) void { |
| 196 | 195 | _ = decl_index; |
| 197 | 196 | } |
| 198 | 197 | |
| 199 | pub fn flush(self: *SpirV, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 200 | return self.flushModule(arena, prog_node); | |
| 198 | pub fn flush(self: *SpirV, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 199 | return self.flushModule(arena, tid, prog_node); | |
| 201 | 200 | } |
| 202 | 201 | |
| 203 | pub fn flushModule(self: *SpirV, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 202 | pub fn flushModule(self: *SpirV, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 204 | 203 | if (build_options.skip_non_native) { |
| 205 | 204 | @panic("Attempted to compile for architecture that was disabled by build configuration"); |
| 206 | 205 | } |
| ... | ... | @@ -216,6 +215,7 @@ pub fn flushModule(self: *SpirV, arena: Allocator, prog_node: std.Progress.Node) |
| 216 | 215 | const comp = self.base.comp; |
| 217 | 216 | const gpa = comp.gpa; |
| 218 | 217 | const target = comp.getTarget(); |
| 218 | _ = tid; | |
| 219 | 219 | |
| 220 | 220 | try writeCapabilities(spv, target); |
| 221 | 221 | try writeMemoryModel(spv, target); |
src/link/Wasm.zig+25-26| ... | ... | @@ -29,8 +29,6 @@ const InternPool = @import("../InternPool.zig"); |
| 29 | 29 | const Liveness = @import("../Liveness.zig"); |
| 30 | 30 | const LlvmObject = @import("../codegen/llvm.zig").Object; |
| 31 | 31 | const Zcu = @import("../Zcu.zig"); |
| 32 | /// Deprecated. | |
| 33 | const Module = Zcu; | |
| 34 | 32 | const Object = @import("Wasm/Object.zig"); |
| 35 | 33 | const Symbol = @import("Wasm/Symbol.zig"); |
| 36 | 34 | const Type = @import("../Type.zig"); |
| ... | ... | @@ -1441,25 +1439,25 @@ pub fn deinit(wasm: *Wasm) void { |
| 1441 | 1439 | wasm.files.deinit(gpa); |
| 1442 | 1440 | } |
| 1443 | 1441 | |
| 1444 | pub fn updateFunc(wasm: *Wasm, mod: *Module, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 1442 | pub fn updateFunc(wasm: *Wasm, pt: Zcu.PerThread, func_index: InternPool.Index, air: Air, liveness: Liveness) !void { | |
| 1445 | 1443 | if (build_options.skip_non_native and builtin.object_format != .wasm) { |
| 1446 | 1444 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 1447 | 1445 | } |
| 1448 | if (wasm.llvm_object) |llvm_object| return llvm_object.updateFunc(mod, func_index, air, liveness); | |
| 1449 | try wasm.zigObjectPtr().?.updateFunc(wasm, mod, func_index, air, liveness); | |
| 1446 | if (wasm.llvm_object) |llvm_object| return llvm_object.updateFunc(pt, func_index, air, liveness); | |
| 1447 | try wasm.zigObjectPtr().?.updateFunc(wasm, pt, func_index, air, liveness); | |
| 1450 | 1448 | } |
| 1451 | 1449 | |
| 1452 | 1450 | // Generate code for the Decl, storing it in memory to be later written to |
| 1453 | 1451 | // the file on flush(). |
| 1454 | pub fn updateDecl(wasm: *Wasm, mod: *Module, decl_index: InternPool.DeclIndex) !void { | |
| 1452 | pub fn updateDecl(wasm: *Wasm, pt: Zcu.PerThread, decl_index: InternPool.DeclIndex) !void { | |
| 1455 | 1453 | if (build_options.skip_non_native and builtin.object_format != .wasm) { |
| 1456 | 1454 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 1457 | 1455 | } |
| 1458 | if (wasm.llvm_object) |llvm_object| return llvm_object.updateDecl(mod, decl_index); | |
| 1459 | try wasm.zigObjectPtr().?.updateDecl(wasm, mod, decl_index); | |
| 1456 | if (wasm.llvm_object) |llvm_object| return llvm_object.updateDecl(pt, decl_index); | |
| 1457 | try wasm.zigObjectPtr().?.updateDecl(wasm, pt, decl_index); | |
| 1460 | 1458 | } |
| 1461 | 1459 | |
| 1462 | pub fn updateDeclLineNumber(wasm: *Wasm, mod: *Module, decl_index: InternPool.DeclIndex) !void { | |
| 1460 | pub fn updateDeclLineNumber(wasm: *Wasm, mod: *Zcu, decl_index: InternPool.DeclIndex) !void { | |
| 1463 | 1461 | if (wasm.llvm_object) |_| return; |
| 1464 | 1462 | try wasm.zigObjectPtr().?.updateDeclLineNumber(mod, decl_index); |
| 1465 | 1463 | } |
| ... | ... | @@ -1506,8 +1504,8 @@ fn getFunctionSignature(wasm: *const Wasm, loc: SymbolLoc) std.wasm.Type { |
| 1506 | 1504 | /// Lowers a constant typed value to a local symbol and atom. |
| 1507 | 1505 | /// Returns the symbol index of the local |
| 1508 | 1506 | /// The given `decl` is the parent decl whom owns the constant. |
| 1509 | pub fn lowerUnnamedConst(wasm: *Wasm, val: Value, decl_index: InternPool.DeclIndex) !u32 { | |
| 1510 | return wasm.zigObjectPtr().?.lowerUnnamedConst(wasm, val, decl_index); | |
| 1507 | pub fn lowerUnnamedConst(wasm: *Wasm, pt: Zcu.PerThread, val: Value, decl_index: InternPool.DeclIndex) !u32 { | |
| 1508 | return wasm.zigObjectPtr().?.lowerUnnamedConst(wasm, pt, val, decl_index); | |
| 1511 | 1509 | } |
| 1512 | 1510 | |
| 1513 | 1511 | /// Returns the symbol index from a symbol of which its flag is set global, |
| ... | ... | @@ -1531,11 +1529,12 @@ pub fn getDeclVAddr( |
| 1531 | 1529 | |
| 1532 | 1530 | pub fn lowerAnonDecl( |
| 1533 | 1531 | wasm: *Wasm, |
| 1532 | pt: Zcu.PerThread, | |
| 1534 | 1533 | decl_val: InternPool.Index, |
| 1535 | 1534 | explicit_alignment: Alignment, |
| 1536 | src_loc: Module.LazySrcLoc, | |
| 1535 | src_loc: Zcu.LazySrcLoc, | |
| 1537 | 1536 | ) !codegen.Result { |
| 1538 | return wasm.zigObjectPtr().?.lowerAnonDecl(wasm, decl_val, explicit_alignment, src_loc); | |
| 1537 | return wasm.zigObjectPtr().?.lowerAnonDecl(wasm, pt, decl_val, explicit_alignment, src_loc); | |
| 1539 | 1538 | } |
| 1540 | 1539 | |
| 1541 | 1540 | pub fn getAnonDeclVAddr(wasm: *Wasm, decl_val: InternPool.Index, reloc_info: link.File.RelocInfo) !u64 { |
| ... | ... | @@ -1553,15 +1552,15 @@ pub fn deleteExport( |
| 1553 | 1552 | |
| 1554 | 1553 | pub fn updateExports( |
| 1555 | 1554 | wasm: *Wasm, |
| 1556 | mod: *Module, | |
| 1557 | exported: Module.Exported, | |
| 1555 | pt: Zcu.PerThread, | |
| 1556 | exported: Zcu.Exported, | |
| 1558 | 1557 | export_indices: []const u32, |
| 1559 | 1558 | ) !void { |
| 1560 | 1559 | if (build_options.skip_non_native and builtin.object_format != .wasm) { |
| 1561 | 1560 | @panic("Attempted to compile for object format that was disabled by build configuration"); |
| 1562 | 1561 | } |
| 1563 | if (wasm.llvm_object) |llvm_object| return llvm_object.updateExports(mod, exported, export_indices); | |
| 1564 | return wasm.zigObjectPtr().?.updateExports(wasm, mod, exported, export_indices); | |
| 1562 | if (wasm.llvm_object) |llvm_object| return llvm_object.updateExports(pt, exported, export_indices); | |
| 1563 | return wasm.zigObjectPtr().?.updateExports(wasm, pt, exported, export_indices); | |
| 1565 | 1564 | } |
| 1566 | 1565 | |
| 1567 | 1566 | pub fn freeDecl(wasm: *Wasm, decl_index: InternPool.DeclIndex) void { |
| ... | ... | @@ -2466,18 +2465,18 @@ fn appendDummySegment(wasm: *Wasm) !void { |
| 2466 | 2465 | }); |
| 2467 | 2466 | } |
| 2468 | 2467 | |
| 2469 | pub fn flush(wasm: *Wasm, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 2468 | pub fn flush(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 2470 | 2469 | const comp = wasm.base.comp; |
| 2471 | 2470 | const use_lld = build_options.have_llvm and comp.config.use_lld; |
| 2472 | 2471 | |
| 2473 | 2472 | if (use_lld) { |
| 2474 | return wasm.linkWithLLD(arena, prog_node); | |
| 2473 | return wasm.linkWithLLD(arena, tid, prog_node); | |
| 2475 | 2474 | } |
| 2476 | return wasm.flushModule(arena, prog_node); | |
| 2475 | return wasm.flushModule(arena, tid, prog_node); | |
| 2477 | 2476 | } |
| 2478 | 2477 | |
| 2479 | 2478 | /// Uses the in-house linker to link one or multiple object -and archive files into a WebAssembly binary. |
| 2480 | pub fn flushModule(wasm: *Wasm, arena: Allocator, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 2479 | pub fn flushModule(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) link.File.FlushError!void { | |
| 2481 | 2480 | const tracy = trace(@src()); |
| 2482 | 2481 | defer tracy.end(); |
| 2483 | 2482 | |
| ... | ... | @@ -2513,7 +2512,7 @@ pub fn flushModule(wasm: *Wasm, arena: Allocator, prog_node: std.Progress.Node) |
| 2513 | 2512 | const wasi_exec_model = comp.config.wasi_exec_model; |
| 2514 | 2513 | |
| 2515 | 2514 | if (wasm.zigObjectPtr()) |zig_object| { |
| 2516 | try zig_object.flushModule(wasm); | |
| 2515 | try zig_object.flushModule(wasm, tid); | |
| 2517 | 2516 | } |
| 2518 | 2517 | |
| 2519 | 2518 | // When the target os is WASI, we allow linking with WASI-LIBC |
| ... | ... | @@ -3324,7 +3323,7 @@ fn emitImport(wasm: *Wasm, writer: anytype, import: types.Import) !void { |
| 3324 | 3323 | } |
| 3325 | 3324 | } |
| 3326 | 3325 | |
| 3327 | fn linkWithLLD(wasm: *Wasm, arena: Allocator, prog_node: std.Progress.Node) !void { | |
| 3326 | fn linkWithLLD(wasm: *Wasm, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: std.Progress.Node) !void { | |
| 3328 | 3327 | const tracy = trace(@src()); |
| 3329 | 3328 | defer tracy.end(); |
| 3330 | 3329 | |
| ... | ... | @@ -3342,7 +3341,7 @@ fn linkWithLLD(wasm: *Wasm, arena: Allocator, prog_node: std.Progress.Node) !voi |
| 3342 | 3341 | // If there is no Zig code to compile, then we should skip flushing the output file because it |
| 3343 | 3342 | // will not be part of the linker line anyway. |
| 3344 | 3343 | const module_obj_path: ?[]const u8 = if (comp.module != null) blk: { |
| 3345 | try wasm.flushModule(arena, prog_node); | |
| 3344 | try wasm.flushModule(arena, tid, prog_node); | |
| 3346 | 3345 | |
| 3347 | 3346 | if (fs.path.dirname(full_out_path)) |dirname| { |
| 3348 | 3347 | break :blk try fs.path.join(arena, &.{ dirname, wasm.base.zcu_object_sub_path.? }); |
| ... | ... | @@ -4009,8 +4008,8 @@ pub fn storeDeclType(wasm: *Wasm, decl_index: InternPool.DeclIndex, func_type: s |
| 4009 | 4008 | /// Returns the symbol index of the error name table. |
| 4010 | 4009 | /// |
| 4011 | 4010 | /// When the symbol does not yet exist, it will create a new one instead. |
| 4012 | pub fn getErrorTableSymbol(wasm_file: *Wasm) !u32 { | |
| 4013 | const sym_index = try wasm_file.zigObjectPtr().?.getErrorTableSymbol(wasm_file); | |
| 4011 | pub fn getErrorTableSymbol(wasm_file: *Wasm, pt: Zcu.PerThread) !u32 { | |
| 4012 | const sym_index = try wasm_file.zigObjectPtr().?.getErrorTableSymbol(wasm_file, pt); | |
| 4014 | 4013 | return @intFromEnum(sym_index); |
| 4015 | 4014 | } |
| 4016 | 4015 |
src/link/Wasm/ZigObject.zig+59-41| ... | ... | @@ -241,9 +241,10 @@ pub fn allocateSymbol(zig_object: *ZigObject, gpa: std.mem.Allocator) !Symbol.In |
| 241 | 241 | pub fn updateDecl( |
| 242 | 242 | zig_object: *ZigObject, |
| 243 | 243 | wasm_file: *Wasm, |
| 244 | mod: *Module, | |
| 244 | pt: Zcu.PerThread, | |
| 245 | 245 | decl_index: InternPool.DeclIndex, |
| 246 | 246 | ) !void { |
| 247 | const mod = pt.zcu; | |
| 247 | 248 | const decl = mod.declPtr(decl_index); |
| 248 | 249 | if (decl.val.getFunction(mod)) |_| { |
| 249 | 250 | return; |
| ... | ... | @@ -269,6 +270,7 @@ pub fn updateDecl( |
| 269 | 270 | |
| 270 | 271 | const res = try codegen.generateSymbol( |
| 271 | 272 | &wasm_file.base, |
| 273 | pt, | |
| 272 | 274 | decl.navSrcLoc(mod), |
| 273 | 275 | val, |
| 274 | 276 | &code_writer, |
| ... | ... | @@ -285,21 +287,21 @@ pub fn updateDecl( |
| 285 | 287 | }, |
| 286 | 288 | }; |
| 287 | 289 | |
| 288 | return zig_object.finishUpdateDecl(wasm_file, decl_index, code); | |
| 290 | return zig_object.finishUpdateDecl(wasm_file, pt, decl_index, code); | |
| 289 | 291 | } |
| 290 | 292 | |
| 291 | 293 | pub fn updateFunc( |
| 292 | 294 | zig_object: *ZigObject, |
| 293 | 295 | wasm_file: *Wasm, |
| 294 | mod: *Module, | |
| 296 | pt: Zcu.PerThread, | |
| 295 | 297 | func_index: InternPool.Index, |
| 296 | 298 | air: Air, |
| 297 | 299 | liveness: Liveness, |
| 298 | 300 | ) !void { |
| 299 | 301 | const gpa = wasm_file.base.comp.gpa; |
| 300 | const func = mod.funcInfo(func_index); | |
| 302 | const func = pt.zcu.funcInfo(func_index); | |
| 301 | 303 | const decl_index = func.owner_decl; |
| 302 | const decl = mod.declPtr(decl_index); | |
| 304 | const decl = pt.zcu.declPtr(decl_index); | |
| 303 | 305 | const atom_index = try zig_object.getOrCreateAtomForDecl(wasm_file, decl_index); |
| 304 | 306 | const atom = wasm_file.getAtomPtr(atom_index); |
| 305 | 307 | atom.clear(); |
| ... | ... | @@ -308,7 +310,8 @@ pub fn updateFunc( |
| 308 | 310 | defer code_writer.deinit(); |
| 309 | 311 | const result = try codegen.generateFunction( |
| 310 | 312 | &wasm_file.base, |
| 311 | decl.navSrcLoc(mod), | |
| 313 | pt, | |
| 314 | decl.navSrcLoc(pt.zcu), | |
| 312 | 315 | func_index, |
| 313 | 316 | air, |
| 314 | 317 | liveness, |
| ... | ... | @@ -320,29 +323,31 @@ pub fn updateFunc( |
| 320 | 323 | .ok => code_writer.items, |
| 321 | 324 | .fail => |em| { |
| 322 | 325 | decl.analysis = .codegen_failure; |
| 323 | try mod.failed_analysis.put(mod.gpa, AnalUnit.wrap(.{ .decl = decl_index }), em); | |
| 326 | try pt.zcu.failed_analysis.put(gpa, AnalUnit.wrap(.{ .decl = decl_index }), em); | |
| 324 | 327 | return; |
| 325 | 328 | }, |
| 326 | 329 | }; |
| 327 | 330 | |
| 328 | return zig_object.finishUpdateDecl(wasm_file, decl_index, code); | |
| 331 | return zig_object.finishUpdateDecl(wasm_file, pt, decl_index, code); | |
| 329 | 332 | } |
| 330 | 333 | |
| 331 | 334 | fn finishUpdateDecl( |
| 332 | 335 | zig_object: *ZigObject, |
| 333 | 336 | wasm_file: *Wasm, |
| 337 | pt: Zcu.PerThread, | |
| 334 | 338 | decl_index: InternPool.DeclIndex, |
| 335 | 339 | code: []const u8, |
| 336 | 340 | ) !void { |
| 337 | const gpa = wasm_file.base.comp.gpa; | |
| 338 | const zcu = wasm_file.base.comp.module.?; | |
| 341 | const zcu = pt.zcu; | |
| 342 | const ip = &zcu.intern_pool; | |
| 343 | const gpa = zcu.gpa; | |
| 339 | 344 | const decl = zcu.declPtr(decl_index); |
| 340 | 345 | const decl_info = zig_object.decls_map.get(decl_index).?; |
| 341 | 346 | const atom_index = decl_info.atom; |
| 342 | 347 | const atom = wasm_file.getAtomPtr(atom_index); |
| 343 | 348 | const sym = zig_object.symbol(atom.sym_index); |
| 344 | 349 | const full_name = try decl.fullyQualifiedName(zcu); |
| 345 | sym.name = try zig_object.string_table.insert(gpa, full_name.toSlice(&zcu.intern_pool)); | |
| 350 | sym.name = try zig_object.string_table.insert(gpa, full_name.toSlice(ip)); | |
| 346 | 351 | try atom.code.appendSlice(gpa, code); |
| 347 | 352 | atom.size = @intCast(code.len); |
| 348 | 353 | |
| ... | ... | @@ -382,7 +387,7 @@ fn finishUpdateDecl( |
| 382 | 387 | // Will be freed upon freeing of decl or after cleanup of Wasm binary. |
| 383 | 388 | const full_segment_name = try std.mem.concat(gpa, u8, &.{ |
| 384 | 389 | segment_name, |
| 385 | full_name.toSlice(&zcu.intern_pool), | |
| 390 | full_name.toSlice(ip), | |
| 386 | 391 | }); |
| 387 | 392 | errdefer gpa.free(full_segment_name); |
| 388 | 393 | sym.tag = .data; |
| ... | ... | @@ -390,7 +395,7 @@ fn finishUpdateDecl( |
| 390 | 395 | }, |
| 391 | 396 | } |
| 392 | 397 | if (code.len == 0) return; |
| 393 | atom.alignment = decl.getAlignment(zcu); | |
| 398 | atom.alignment = decl.getAlignment(pt); | |
| 394 | 399 | } |
| 395 | 400 | |
| 396 | 401 | /// Creates and initializes a new segment in the 'Data' section. |
| ... | ... | @@ -437,9 +442,10 @@ pub fn getOrCreateAtomForDecl(zig_object: *ZigObject, wasm_file: *Wasm, decl_ind |
| 437 | 442 | pub fn lowerAnonDecl( |
| 438 | 443 | zig_object: *ZigObject, |
| 439 | 444 | wasm_file: *Wasm, |
| 445 | pt: Zcu.PerThread, | |
| 440 | 446 | decl_val: InternPool.Index, |
| 441 | 447 | explicit_alignment: InternPool.Alignment, |
| 442 | src_loc: Module.LazySrcLoc, | |
| 448 | src_loc: Zcu.LazySrcLoc, | |
| 443 | 449 | ) !codegen.Result { |
| 444 | 450 | const gpa = wasm_file.base.comp.gpa; |
| 445 | 451 | const gop = try zig_object.anon_decls.getOrPut(gpa, decl_val); |
| ... | ... | @@ -449,7 +455,7 @@ pub fn lowerAnonDecl( |
| 449 | 455 | @intFromEnum(decl_val), |
| 450 | 456 | }) catch unreachable; |
| 451 | 457 | |
| 452 | switch (try zig_object.lowerConst(wasm_file, name, Value.fromInterned(decl_val), src_loc)) { | |
| 458 | switch (try zig_object.lowerConst(wasm_file, pt, name, Value.fromInterned(decl_val), src_loc)) { | |
| 453 | 459 | .ok => |atom_index| zig_object.anon_decls.values()[gop.index] = atom_index, |
| 454 | 460 | .fail => |em| return .{ .fail = em }, |
| 455 | 461 | } |
| ... | ... | @@ -469,9 +475,15 @@ pub fn lowerAnonDecl( |
| 469 | 475 | /// Lowers a constant typed value to a local symbol and atom. |
| 470 | 476 | /// Returns the symbol index of the local |
| 471 | 477 | /// The given `decl` is the parent decl whom owns the constant. |
| 472 | pub fn lowerUnnamedConst(zig_object: *ZigObject, wasm_file: *Wasm, val: Value, decl_index: InternPool.DeclIndex) !u32 { | |
| 473 | const gpa = wasm_file.base.comp.gpa; | |
| 474 | const mod = wasm_file.base.comp.module.?; | |
| 478 | pub fn lowerUnnamedConst( | |
| 479 | zig_object: *ZigObject, | |
| 480 | wasm_file: *Wasm, | |
| 481 | pt: Zcu.PerThread, | |
| 482 | val: Value, | |
| 483 | decl_index: InternPool.DeclIndex, | |
| 484 | ) !u32 { | |
| 485 | const mod = pt.zcu; | |
| 486 | const gpa = mod.gpa; | |
| 475 | 487 | std.debug.assert(val.typeOf(mod).zigTypeTag(mod) != .Fn); // cannot create local symbols for functions |
| 476 | 488 | const decl = mod.declPtr(decl_index); |
| 477 | 489 | |
| ... | ... | @@ -494,7 +506,7 @@ pub fn lowerUnnamedConst(zig_object: *ZigObject, wasm_file: *Wasm, val: Value, d |
| 494 | 506 | else |
| 495 | 507 | decl.navSrcLoc(mod); |
| 496 | 508 | |
| 497 | switch (try zig_object.lowerConst(wasm_file, name, val, decl_src)) { | |
| 509 | switch (try zig_object.lowerConst(wasm_file, pt, name, val, decl_src)) { | |
| 498 | 510 | .ok => |atom_index| { |
| 499 | 511 | try wasm_file.getAtomPtr(parent_atom_index).locals.append(gpa, atom_index); |
| 500 | 512 | return @intFromEnum(wasm_file.getAtom(atom_index).sym_index); |
| ... | ... | @@ -509,10 +521,17 @@ pub fn lowerUnnamedConst(zig_object: *ZigObject, wasm_file: *Wasm, val: Value, d |
| 509 | 521 | |
| 510 | 522 | const LowerConstResult = union(enum) { |
| 511 | 523 | ok: Atom.Index, |
| 512 | fail: *Module.ErrorMsg, | |
| 524 | fail: *Zcu.ErrorMsg, | |
| 513 | 525 | }; |
| 514 | 526 | |
| 515 | fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, val: Value, src_loc: Module.LazySrcLoc) !LowerConstResult { | |
| 527 | fn lowerConst( | |
| 528 | zig_object: *ZigObject, | |
| 529 | wasm_file: *Wasm, | |
| 530 | pt: Zcu.PerThread, | |
| 531 | name: []const u8, | |
| 532 | val: Value, | |
| 533 | src_loc: Zcu.LazySrcLoc, | |
| 534 | ) !LowerConstResult { | |
| 516 | 535 | const gpa = wasm_file.base.comp.gpa; |
| 517 | 536 | const mod = wasm_file.base.comp.module.?; |
| 518 | 537 | |
| ... | ... | @@ -526,7 +545,7 @@ fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, val: V |
| 526 | 545 | |
| 527 | 546 | const code = code: { |
| 528 | 547 | const atom = wasm_file.getAtomPtr(atom_index); |
| 529 | atom.alignment = ty.abiAlignment(mod); | |
| 548 | atom.alignment = ty.abiAlignment(pt); | |
| 530 | 549 | const segment_name = try std.mem.concat(gpa, u8, &.{ ".rodata.", name }); |
| 531 | 550 | errdefer gpa.free(segment_name); |
| 532 | 551 | zig_object.symbol(sym_index).* = .{ |
| ... | ... | @@ -536,13 +555,14 @@ fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, val: V |
| 536 | 555 | .index = try zig_object.createDataSegment( |
| 537 | 556 | gpa, |
| 538 | 557 | segment_name, |
| 539 | ty.abiAlignment(mod), | |
| 558 | ty.abiAlignment(pt), | |
| 540 | 559 | ), |
| 541 | 560 | .virtual_address = undefined, |
| 542 | 561 | }; |
| 543 | 562 | |
| 544 | 563 | const result = try codegen.generateSymbol( |
| 545 | 564 | &wasm_file.base, |
| 565 | pt, | |
| 546 | 566 | src_loc, |
| 547 | 567 | val, |
| 548 | 568 | &value_bytes, |
| ... | ... | @@ -568,7 +588,7 @@ fn lowerConst(zig_object: *ZigObject, wasm_file: *Wasm, name: []const u8, val: V |
| 568 | 588 | /// Returns the symbol index of the error name table. |
| 569 | 589 | /// |
| 570 | 590 | /// When the symbol does not yet exist, it will create a new one instead. |
| 571 | pub fn getErrorTableSymbol(zig_object: *ZigObject, wasm_file: *Wasm) !Symbol.Index { | |
| 591 | pub fn getErrorTableSymbol(zig_object: *ZigObject, wasm_file: *Wasm, pt: Zcu.PerThread) !Symbol.Index { | |
| 572 | 592 | if (zig_object.error_table_symbol != .null) { |
| 573 | 593 | return zig_object.error_table_symbol; |
| 574 | 594 | } |
| ... | ... | @@ -581,8 +601,7 @@ pub fn getErrorTableSymbol(zig_object: *ZigObject, wasm_file: *Wasm) !Symbol.Ind |
| 581 | 601 | const atom_index = try wasm_file.createAtom(sym_index, zig_object.index); |
| 582 | 602 | const atom = wasm_file.getAtomPtr(atom_index); |
| 583 | 603 | const slice_ty = Type.slice_const_u8_sentinel_0; |
| 584 | const mod = wasm_file.base.comp.module.?; | |
| 585 | atom.alignment = slice_ty.abiAlignment(mod); | |
| 604 | atom.alignment = slice_ty.abiAlignment(pt); | |
| 586 | 605 | |
| 587 | 606 | const sym_name = try zig_object.string_table.insert(gpa, "__zig_err_name_table"); |
| 588 | 607 | const segment_name = try gpa.dupe(u8, ".rodata.__zig_err_name_table"); |
| ... | ... | @@ -604,7 +623,7 @@ pub fn getErrorTableSymbol(zig_object: *ZigObject, wasm_file: *Wasm) !Symbol.Ind |
| 604 | 623 | /// |
| 605 | 624 | /// This creates a table that consists of pointers and length to each error name. |
| 606 | 625 | /// The table is what is being pointed to within the runtime bodies that are generated. |
| 607 | fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { | |
| 626 | fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm, tid: Zcu.PerThread.Id) !void { | |
| 608 | 627 | if (zig_object.error_table_symbol == .null) return; |
| 609 | 628 | const gpa = wasm_file.base.comp.gpa; |
| 610 | 629 | const atom_index = wasm_file.symbol_atom.get(.{ .file = zig_object.index, .index = zig_object.error_table_symbol }).?; |
| ... | ... | @@ -631,11 +650,11 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { |
| 631 | 650 | |
| 632 | 651 | // Addend for each relocation to the table |
| 633 | 652 | var addend: u32 = 0; |
| 634 | const mod = wasm_file.base.comp.module.?; | |
| 635 | for (mod.global_error_set.keys()) |error_name| { | |
| 653 | const pt: Zcu.PerThread = .{ .zcu = wasm_file.base.comp.module.?, .tid = tid }; | |
| 654 | for (pt.zcu.global_error_set.keys()) |error_name| { | |
| 636 | 655 | const atom = wasm_file.getAtomPtr(atom_index); |
| 637 | 656 | |
| 638 | const error_name_slice = error_name.toSlice(&mod.intern_pool); | |
| 657 | const error_name_slice = error_name.toSlice(&pt.zcu.intern_pool); | |
| 639 | 658 | const len: u32 = @intCast(error_name_slice.len + 1); // names are 0-terminated |
| 640 | 659 | |
| 641 | 660 | const slice_ty = Type.slice_const_u8_sentinel_0; |
| ... | ... | @@ -650,14 +669,14 @@ fn populateErrorNameTable(zig_object: *ZigObject, wasm_file: *Wasm) !void { |
| 650 | 669 | .offset = offset, |
| 651 | 670 | .addend = @intCast(addend), |
| 652 | 671 | }); |
| 653 | atom.size += @intCast(slice_ty.abiSize(mod)); | |
| 672 | atom.size += @intCast(slice_ty.abiSize(pt)); | |
| 654 | 673 | addend += len; |
| 655 | 674 | |
| 656 | 675 | // as we updated the error name table, we now store the actual name within the names atom |
| 657 | 676 | try names_atom.code.ensureUnusedCapacity(gpa, len); |
| 658 | 677 | names_atom.code.appendSliceAssumeCapacity(error_name_slice[0..len]); |
| 659 | 678 | |
| 660 | log.debug("Populated error name: '{}'", .{error_name.fmt(&mod.intern_pool)}); | |
| 679 | log.debug("Populated error name: '{}'", .{error_name.fmt(&pt.zcu.intern_pool)}); | |
| 661 | 680 | } |
| 662 | 681 | names_atom.size = addend; |
| 663 | 682 | zig_object.error_names_atom = names_atom_index; |
| ... | ... | @@ -858,10 +877,11 @@ pub fn deleteExport( |
| 858 | 877 | pub fn updateExports( |
| 859 | 878 | zig_object: *ZigObject, |
| 860 | 879 | wasm_file: *Wasm, |
| 861 | mod: *Module, | |
| 862 | exported: Module.Exported, | |
| 880 | pt: Zcu.PerThread, | |
| 881 | exported: Zcu.Exported, | |
| 863 | 882 | export_indices: []const u32, |
| 864 | 883 | ) !void { |
| 884 | const mod = pt.zcu; | |
| 865 | 885 | const decl_index = switch (exported) { |
| 866 | 886 | .decl_index => |i| i, |
| 867 | 887 | .value => |val| { |
| ... | ... | @@ -880,7 +900,7 @@ pub fn updateExports( |
| 880 | 900 | for (export_indices) |export_idx| { |
| 881 | 901 | const exp = mod.all_exports.items[export_idx]; |
| 882 | 902 | if (exp.opts.section.toSlice(&mod.intern_pool)) |section| { |
| 883 | try mod.failed_exports.putNoClobber(gpa, export_idx, try Module.ErrorMsg.create( | |
| 903 | try mod.failed_exports.putNoClobber(gpa, export_idx, try Zcu.ErrorMsg.create( | |
| 884 | 904 | gpa, |
| 885 | 905 | decl.navSrcLoc(mod), |
| 886 | 906 | "Unimplemented: ExportOptions.section '{s}'", |
| ... | ... | @@ -913,7 +933,7 @@ pub fn updateExports( |
| 913 | 933 | }, |
| 914 | 934 | .strong => {}, // symbols are strong by default |
| 915 | 935 | .link_once => { |
| 916 | try mod.failed_exports.putNoClobber(gpa, export_idx, try Module.ErrorMsg.create( | |
| 936 | try mod.failed_exports.putNoClobber(gpa, export_idx, try Zcu.ErrorMsg.create( | |
| 917 | 937 | gpa, |
| 918 | 938 | decl.navSrcLoc(mod), |
| 919 | 939 | "Unimplemented: LinkOnce", |
| ... | ... | @@ -1096,7 +1116,7 @@ pub fn createDebugSectionForIndex(zig_object: *ZigObject, wasm_file: *Wasm, inde |
| 1096 | 1116 | return atom_index; |
| 1097 | 1117 | } |
| 1098 | 1118 | |
| 1099 | pub fn updateDeclLineNumber(zig_object: *ZigObject, mod: *Module, decl_index: InternPool.DeclIndex) !void { | |
| 1119 | pub fn updateDeclLineNumber(zig_object: *ZigObject, mod: *Zcu, decl_index: InternPool.DeclIndex) !void { | |
| 1100 | 1120 | if (zig_object.dwarf) |*dw| { |
| 1101 | 1121 | const decl = mod.declPtr(decl_index); |
| 1102 | 1122 | const decl_name = try decl.fullyQualifiedName(mod); |
| ... | ... | @@ -1228,8 +1248,8 @@ fn appendFunction(zig_object: *ZigObject, gpa: std.mem.Allocator, func: std.wasm |
| 1228 | 1248 | return index; |
| 1229 | 1249 | } |
| 1230 | 1250 | |
| 1231 | pub fn flushModule(zig_object: *ZigObject, wasm_file: *Wasm) !void { | |
| 1232 | try zig_object.populateErrorNameTable(wasm_file); | |
| 1251 | pub fn flushModule(zig_object: *ZigObject, wasm_file: *Wasm, tid: Zcu.PerThread.Id) !void { | |
| 1252 | try zig_object.populateErrorNameTable(wasm_file, tid); | |
| 1233 | 1253 | try zig_object.setupErrorsLen(wasm_file); |
| 1234 | 1254 | } |
| 1235 | 1255 | |
| ... | ... | @@ -1248,8 +1268,6 @@ const File = @import("file.zig").File; |
| 1248 | 1268 | const InternPool = @import("../../InternPool.zig"); |
| 1249 | 1269 | const Liveness = @import("../../Liveness.zig"); |
| 1250 | 1270 | const Zcu = @import("../../Zcu.zig"); |
| 1251 | /// Deprecated. | |
| 1252 | const Module = Zcu; | |
| 1253 | 1271 | const StringTable = @import("../StringTable.zig"); |
| 1254 | 1272 | const Symbol = @import("Symbol.zig"); |
| 1255 | 1273 | const Type = @import("../../Type.zig"); |
src/main.zig+4-4| ... | ... | @@ -172,7 +172,7 @@ pub fn main() anyerror!void { |
| 172 | 172 | } |
| 173 | 173 | // We would prefer to use raw libc allocator here, but cannot |
| 174 | 174 | // use it if it won't support the alignment we need. |
| 175 | if (@alignOf(std.c.max_align_t) < @alignOf(i128)) { | |
| 175 | if (@alignOf(std.c.max_align_t) < @max(@alignOf(i128), std.atomic.cache_line)) { | |
| 176 | 176 | break :gpa std.heap.c_allocator; |
| 177 | 177 | } |
| 178 | 178 | break :gpa std.heap.raw_c_allocator; |
| ... | ... | @@ -3092,7 +3092,7 @@ fn buildOutputType( |
| 3092 | 3092 | defer emit_implib_resolved.deinit(); |
| 3093 | 3093 | |
| 3094 | 3094 | var thread_pool: ThreadPool = undefined; |
| 3095 | try thread_pool.init(.{ .allocator = gpa }); | |
| 3095 | try thread_pool.init(.{ .allocator = gpa, .track_ids = true }); | |
| 3096 | 3096 | defer thread_pool.deinit(); |
| 3097 | 3097 | |
| 3098 | 3098 | var cleanup_local_cache_dir: ?fs.Dir = null; |
| ... | ... | @@ -4895,7 +4895,7 @@ fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !void { |
| 4895 | 4895 | child_argv.items[argv_index_cache_dir] = local_cache_directory.path orelse cwd_path; |
| 4896 | 4896 | |
| 4897 | 4897 | var thread_pool: ThreadPool = undefined; |
| 4898 | try thread_pool.init(.{ .allocator = gpa }); | |
| 4898 | try thread_pool.init(.{ .allocator = gpa, .track_ids = true }); | |
| 4899 | 4899 | defer thread_pool.deinit(); |
| 4900 | 4900 | |
| 4901 | 4901 | // Dummy http client that is not actually used when only_core_functionality is enabled. |
| ... | ... | @@ -5329,7 +5329,7 @@ fn jitCmd( |
| 5329 | 5329 | defer global_cache_directory.handle.close(); |
| 5330 | 5330 | |
| 5331 | 5331 | var thread_pool: ThreadPool = undefined; |
| 5332 | try thread_pool.init(.{ .allocator = gpa }); | |
| 5332 | try thread_pool.init(.{ .allocator = gpa, .track_ids = true }); | |
| 5333 | 5333 | defer thread_pool.deinit(); |
| 5334 | 5334 | |
| 5335 | 5335 | var child_argv: std.ArrayListUnmanaged([]const u8) = .{}; |
src/mutable_value.zig+48-52| ... | ... | @@ -54,46 +54,44 @@ pub const MutableValue = union(enum) { |
| 54 | 54 | payload: *MutableValue, |
| 55 | 55 | }; |
| 56 | 56 | |
| 57 | pub fn intern(mv: MutableValue, zcu: *Zcu, arena: Allocator) Allocator.Error!Value { | |
| 58 | const ip = &zcu.intern_pool; | |
| 59 | const gpa = zcu.gpa; | |
| 57 | pub fn intern(mv: MutableValue, pt: Zcu.PerThread, arena: Allocator) Allocator.Error!Value { | |
| 60 | 58 | return Value.fromInterned(switch (mv) { |
| 61 | 59 | .interned => |ip_index| ip_index, |
| 62 | .eu_payload => |sv| try ip.get(gpa, .{ .error_union = .{ | |
| 60 | .eu_payload => |sv| try pt.intern(.{ .error_union = .{ | |
| 63 | 61 | .ty = sv.ty, |
| 64 | .val = .{ .payload = (try sv.child.intern(zcu, arena)).toIntern() }, | |
| 62 | .val = .{ .payload = (try sv.child.intern(pt, arena)).toIntern() }, | |
| 65 | 63 | } }), |
| 66 | .opt_payload => |sv| try ip.get(gpa, .{ .opt = .{ | |
| 64 | .opt_payload => |sv| try pt.intern(.{ .opt = .{ | |
| 67 | 65 | .ty = sv.ty, |
| 68 | .val = (try sv.child.intern(zcu, arena)).toIntern(), | |
| 66 | .val = (try sv.child.intern(pt, arena)).toIntern(), | |
| 69 | 67 | } }), |
| 70 | .repeated => |sv| try ip.get(gpa, .{ .aggregate = .{ | |
| 68 | .repeated => |sv| try pt.intern(.{ .aggregate = .{ | |
| 71 | 69 | .ty = sv.ty, |
| 72 | .storage = .{ .repeated_elem = (try sv.child.intern(zcu, arena)).toIntern() }, | |
| 70 | .storage = .{ .repeated_elem = (try sv.child.intern(pt, arena)).toIntern() }, | |
| 73 | 71 | } }), |
| 74 | .bytes => |b| try ip.get(gpa, .{ .aggregate = .{ | |
| 72 | .bytes => |b| try pt.intern(.{ .aggregate = .{ | |
| 75 | 73 | .ty = b.ty, |
| 76 | .storage = .{ .bytes = try ip.getOrPutString(gpa, b.data, .maybe_embedded_nulls) }, | |
| 74 | .storage = .{ .bytes = try pt.zcu.intern_pool.getOrPutString(pt.zcu.gpa, b.data, .maybe_embedded_nulls) }, | |
| 77 | 75 | } }), |
| 78 | 76 | .aggregate => |a| { |
| 79 | 77 | const elems = try arena.alloc(InternPool.Index, a.elems.len); |
| 80 | 78 | for (a.elems, elems) |mut_elem, *interned_elem| { |
| 81 | interned_elem.* = (try mut_elem.intern(zcu, arena)).toIntern(); | |
| 79 | interned_elem.* = (try mut_elem.intern(pt, arena)).toIntern(); | |
| 82 | 80 | } |
| 83 | return Value.fromInterned(try ip.get(gpa, .{ .aggregate = .{ | |
| 81 | return Value.fromInterned(try pt.intern(.{ .aggregate = .{ | |
| 84 | 82 | .ty = a.ty, |
| 85 | 83 | .storage = .{ .elems = elems }, |
| 86 | 84 | } })); |
| 87 | 85 | }, |
| 88 | .slice => |s| try ip.get(gpa, .{ .slice = .{ | |
| 86 | .slice => |s| try pt.intern(.{ .slice = .{ | |
| 89 | 87 | .ty = s.ty, |
| 90 | .ptr = (try s.ptr.intern(zcu, arena)).toIntern(), | |
| 91 | .len = (try s.len.intern(zcu, arena)).toIntern(), | |
| 88 | .ptr = (try s.ptr.intern(pt, arena)).toIntern(), | |
| 89 | .len = (try s.len.intern(pt, arena)).toIntern(), | |
| 92 | 90 | } }), |
| 93 | .un => |u| try ip.get(gpa, .{ .un = .{ | |
| 91 | .un => |u| try pt.intern(.{ .un = .{ | |
| 94 | 92 | .ty = u.ty, |
| 95 | 93 | .tag = u.tag, |
| 96 | .val = (try u.payload.intern(zcu, arena)).toIntern(), | |
| 94 | .val = (try u.payload.intern(pt, arena)).toIntern(), | |
| 97 | 95 | } }), |
| 98 | 96 | }); |
| 99 | 97 | } |
| ... | ... | @@ -108,13 +106,13 @@ pub const MutableValue = union(enum) { |
| 108 | 106 | /// If `!allow_repeated`, the `repeated` representation will not be used. |
| 109 | 107 | pub fn unintern( |
| 110 | 108 | mv: *MutableValue, |
| 111 | zcu: *Zcu, | |
| 109 | pt: Zcu.PerThread, | |
| 112 | 110 | arena: Allocator, |
| 113 | 111 | allow_bytes: bool, |
| 114 | 112 | allow_repeated: bool, |
| 115 | 113 | ) Allocator.Error!void { |
| 114 | const zcu = pt.zcu; | |
| 116 | 115 | const ip = &zcu.intern_pool; |
| 117 | const gpa = zcu.gpa; | |
| 118 | 116 | switch (mv.*) { |
| 119 | 117 | .interned => |ip_index| switch (ip.indexToKey(ip_index)) { |
| 120 | 118 | .opt => |opt| if (opt.val != .none) { |
| ... | ... | @@ -170,7 +168,7 @@ pub const MutableValue = union(enum) { |
| 170 | 168 | } else { |
| 171 | 169 | const mut_elems = try arena.alloc(MutableValue, len); |
| 172 | 170 | for (bytes.toSlice(len, ip), mut_elems) |b, *mut_elem| { |
| 173 | mut_elem.* = .{ .interned = try ip.get(gpa, .{ .int = .{ | |
| 171 | mut_elem.* = .{ .interned = try pt.intern(.{ .int = .{ | |
| 174 | 172 | .ty = .u8_type, |
| 175 | 173 | .storage = .{ .u64 = b }, |
| 176 | 174 | } }) }; |
| ... | ... | @@ -221,12 +219,12 @@ pub const MutableValue = union(enum) { |
| 221 | 219 | switch (type_tag) { |
| 222 | 220 | .Array, .Vector => { |
| 223 | 221 | const elem_ty = ip.childType(ty_ip); |
| 224 | const undef_elem = try ip.get(gpa, .{ .undef = elem_ty }); | |
| 222 | const undef_elem = try pt.intern(.{ .undef = elem_ty }); | |
| 225 | 223 | @memset(elems[0..@intCast(len_no_sent)], .{ .interned = undef_elem }); |
| 226 | 224 | }, |
| 227 | 225 | .Struct => for (elems[0..@intCast(len_no_sent)], 0..) |*mut_elem, i| { |
| 228 | 226 | const field_ty = ty.structFieldType(i, zcu).toIntern(); |
| 229 | mut_elem.* = .{ .interned = try ip.get(gpa, .{ .undef = field_ty }) }; | |
| 227 | mut_elem.* = .{ .interned = try pt.intern(.{ .undef = field_ty }) }; | |
| 230 | 228 | }, |
| 231 | 229 | else => unreachable, |
| 232 | 230 | } |
| ... | ... | @@ -238,7 +236,7 @@ pub const MutableValue = union(enum) { |
| 238 | 236 | } else { |
| 239 | 237 | const repeated_val = try arena.create(MutableValue); |
| 240 | 238 | repeated_val.* = .{ |
| 241 | .interned = try ip.get(gpa, .{ .undef = ip.childType(ty_ip) }), | |
| 239 | .interned = try pt.intern(.{ .undef = ip.childType(ty_ip) }), | |
| 242 | 240 | }; |
| 243 | 241 | mv.* = .{ .repeated = .{ |
| 244 | 242 | .ty = ty_ip, |
| ... | ... | @@ -248,11 +246,8 @@ pub const MutableValue = union(enum) { |
| 248 | 246 | }, |
| 249 | 247 | .Union => { |
| 250 | 248 | const payload = try arena.create(MutableValue); |
| 251 | const backing_ty = try Type.fromInterned(ty_ip).unionBackingType(zcu); | |
| 252 | payload.* = .{ .interned = try ip.get( | |
| 253 | gpa, | |
| 254 | .{ .undef = backing_ty.toIntern() }, | |
| 255 | ) }; | |
| 249 | const backing_ty = try Type.fromInterned(ty_ip).unionBackingType(pt); | |
| 250 | payload.* = .{ .interned = try pt.intern(.{ .undef = backing_ty.toIntern() }) }; | |
| 256 | 251 | mv.* = .{ .un = .{ |
| 257 | 252 | .ty = ty_ip, |
| 258 | 253 | .tag = .none, |
| ... | ... | @@ -264,8 +259,8 @@ pub const MutableValue = union(enum) { |
| 264 | 259 | if (ptr_ty.flags.size != .Slice) return; |
| 265 | 260 | const ptr = try arena.create(MutableValue); |
| 266 | 261 | const len = try arena.create(MutableValue); |
| 267 | ptr.* = .{ .interned = try ip.get(gpa, .{ .undef = ip.slicePtrType(ty_ip) }) }; | |
| 268 | len.* = .{ .interned = try ip.get(gpa, .{ .undef = .usize_type }) }; | |
| 262 | ptr.* = .{ .interned = try pt.intern(.{ .undef = ip.slicePtrType(ty_ip) }) }; | |
| 263 | len.* = .{ .interned = try pt.intern(.{ .undef = .usize_type }) }; | |
| 269 | 264 | mv.* = .{ .slice = .{ |
| 270 | 265 | .ty = ty_ip, |
| 271 | 266 | .ptr = ptr, |
| ... | ... | @@ -279,7 +274,7 @@ pub const MutableValue = union(enum) { |
| 279 | 274 | .bytes => |bytes| if (!allow_bytes) { |
| 280 | 275 | const elems = try arena.alloc(MutableValue, bytes.data.len); |
| 281 | 276 | for (bytes.data, elems) |byte, *interned_byte| { |
| 282 | interned_byte.* = .{ .interned = try ip.get(gpa, .{ .int = .{ | |
| 277 | interned_byte.* = .{ .interned = try pt.intern(.{ .int = .{ | |
| 283 | 278 | .ty = .u8_type, |
| 284 | 279 | .storage = .{ .u64 = byte }, |
| 285 | 280 | } }) }; |
| ... | ... | @@ -298,22 +293,22 @@ pub const MutableValue = union(enum) { |
| 298 | 293 | /// The returned pointer is valid until the representation of `mv` changes. |
| 299 | 294 | pub fn elem( |
| 300 | 295 | mv: *MutableValue, |
| 301 | zcu: *Zcu, | |
| 296 | pt: Zcu.PerThread, | |
| 302 | 297 | arena: Allocator, |
| 303 | 298 | field_idx: usize, |
| 304 | 299 | ) Allocator.Error!*MutableValue { |
| 300 | const zcu = pt.zcu; | |
| 305 | 301 | const ip = &zcu.intern_pool; |
| 306 | const gpa = zcu.gpa; | |
| 307 | 302 | // Convert to the `aggregate` representation. |
| 308 | 303 | switch (mv.*) { |
| 309 | 304 | .eu_payload, .opt_payload, .un => unreachable, |
| 310 | 305 | .interned => { |
| 311 | try mv.unintern(zcu, arena, false, false); | |
| 306 | try mv.unintern(pt, arena, false, false); | |
| 312 | 307 | }, |
| 313 | 308 | .bytes => |bytes| { |
| 314 | 309 | const elems = try arena.alloc(MutableValue, bytes.data.len); |
| 315 | 310 | for (bytes.data, elems) |byte, *interned_byte| { |
| 316 | interned_byte.* = .{ .interned = try ip.get(gpa, .{ .int = .{ | |
| 311 | interned_byte.* = .{ .interned = try pt.intern(.{ .int = .{ | |
| 317 | 312 | .ty = .u8_type, |
| 318 | 313 | .storage = .{ .u64 = byte }, |
| 319 | 314 | } }) }; |
| ... | ... | @@ -351,14 +346,15 @@ pub const MutableValue = union(enum) { |
| 351 | 346 | /// For slices, uses `Value.slice_ptr_index` and `Value.slice_len_index`. |
| 352 | 347 | pub fn setElem( |
| 353 | 348 | mv: *MutableValue, |
| 354 | zcu: *Zcu, | |
| 349 | pt: Zcu.PerThread, | |
| 355 | 350 | arena: Allocator, |
| 356 | 351 | field_idx: usize, |
| 357 | 352 | field_val: MutableValue, |
| 358 | 353 | ) Allocator.Error!void { |
| 354 | const zcu = pt.zcu; | |
| 359 | 355 | const ip = &zcu.intern_pool; |
| 360 | 356 | const is_trivial_int = field_val.isTrivialInt(zcu); |
| 361 | try mv.unintern(zcu, arena, is_trivial_int, true); | |
| 357 | try mv.unintern(pt, arena, is_trivial_int, true); | |
| 362 | 358 | switch (mv.*) { |
| 363 | 359 | .interned, |
| 364 | 360 | .eu_payload, |
| ... | ... | @@ -373,7 +369,7 @@ pub const MutableValue = union(enum) { |
| 373 | 369 | .bytes => |b| { |
| 374 | 370 | assert(is_trivial_int); |
| 375 | 371 | assert(field_val.typeOf(zcu).toIntern() == .u8_type); |
| 376 | b.data[field_idx] = @intCast(Value.fromInterned(field_val.interned).toUnsignedInt(zcu)); | |
| 372 | b.data[field_idx] = @intCast(Value.fromInterned(field_val.interned).toUnsignedInt(pt)); | |
| 377 | 373 | }, |
| 378 | 374 | .repeated => |r| { |
| 379 | 375 | if (field_val.eqlTrivial(r.child.*)) return; |
| ... | ... | @@ -386,9 +382,9 @@ pub const MutableValue = union(enum) { |
| 386 | 382 | { |
| 387 | 383 | // We can use the `bytes` representation. |
| 388 | 384 | const bytes = try arena.alloc(u8, @intCast(len_inc_sent)); |
| 389 | const repeated_byte = Value.fromInterned(r.child.interned).toUnsignedInt(zcu); | |
| 385 | const repeated_byte = Value.fromInterned(r.child.interned).toUnsignedInt(pt); | |
| 390 | 386 | @memset(bytes, @intCast(repeated_byte)); |
| 391 | bytes[field_idx] = @intCast(Value.fromInterned(field_val.interned).toUnsignedInt(zcu)); | |
| 387 | bytes[field_idx] = @intCast(Value.fromInterned(field_val.interned).toUnsignedInt(pt)); | |
| 392 | 388 | mv.* = .{ .bytes = .{ |
| 393 | 389 | .ty = r.ty, |
| 394 | 390 | .data = bytes, |
| ... | ... | @@ -435,7 +431,7 @@ pub const MutableValue = union(enum) { |
| 435 | 431 | } else { |
| 436 | 432 | const bytes = try arena.alloc(u8, a.elems.len); |
| 437 | 433 | for (a.elems, bytes) |elem_val, *b| { |
| 438 | b.* = @intCast(Value.fromInterned(elem_val.interned).toUnsignedInt(zcu)); | |
| 434 | b.* = @intCast(Value.fromInterned(elem_val.interned).toUnsignedInt(pt)); | |
| 439 | 435 | } |
| 440 | 436 | mv.* = .{ .bytes = .{ |
| 441 | 437 | .ty = a.ty, |
| ... | ... | @@ -451,7 +447,7 @@ pub const MutableValue = union(enum) { |
| 451 | 447 | /// For slices, uses `Value.slice_ptr_index` and `Value.slice_len_index`. |
| 452 | 448 | pub fn getElem( |
| 453 | 449 | mv: MutableValue, |
| 454 | zcu: *Zcu, | |
| 450 | pt: Zcu.PerThread, | |
| 455 | 451 | field_idx: usize, |
| 456 | 452 | ) Allocator.Error!MutableValue { |
| 457 | 453 | return switch (mv) { |
| ... | ... | @@ -459,16 +455,16 @@ pub const MutableValue = union(enum) { |
| 459 | 455 | .opt_payload, |
| 460 | 456 | => unreachable, |
| 461 | 457 | .interned => |ip_index| { |
| 462 | const ty = Type.fromInterned(zcu.intern_pool.typeOf(ip_index)); | |
| 463 | switch (ty.zigTypeTag(zcu)) { | |
| 464 | .Array, .Vector => return .{ .interned = (try Value.fromInterned(ip_index).elemValue(zcu, field_idx)).toIntern() }, | |
| 465 | .Struct, .Union => return .{ .interned = (try Value.fromInterned(ip_index).fieldValue(zcu, field_idx)).toIntern() }, | |
| 458 | const ty = Type.fromInterned(pt.zcu.intern_pool.typeOf(ip_index)); | |
| 459 | switch (ty.zigTypeTag(pt.zcu)) { | |
| 460 | .Array, .Vector => return .{ .interned = (try Value.fromInterned(ip_index).elemValue(pt, field_idx)).toIntern() }, | |
| 461 | .Struct, .Union => return .{ .interned = (try Value.fromInterned(ip_index).fieldValue(pt, field_idx)).toIntern() }, | |
| 466 | 462 | .Pointer => { |
| 467 | assert(ty.isSlice(zcu)); | |
| 463 | assert(ty.isSlice(pt.zcu)); | |
| 468 | 464 | return switch (field_idx) { |
| 469 | Value.slice_ptr_index => .{ .interned = Value.fromInterned(ip_index).slicePtr(zcu).toIntern() }, | |
| 470 | Value.slice_len_index => .{ .interned = switch (zcu.intern_pool.indexToKey(ip_index)) { | |
| 471 | .undef => try zcu.intern(.{ .undef = .usize_type }), | |
| 465 | Value.slice_ptr_index => .{ .interned = Value.fromInterned(ip_index).slicePtr(pt.zcu).toIntern() }, | |
| 466 | Value.slice_len_index => .{ .interned = switch (pt.zcu.intern_pool.indexToKey(ip_index)) { | |
| 467 | .undef => try pt.intern(.{ .undef = .usize_type }), | |
| 472 | 468 | .slice => |s| s.len, |
| 473 | 469 | else => unreachable, |
| 474 | 470 | } }, |
| ... | ... | @@ -487,7 +483,7 @@ pub const MutableValue = union(enum) { |
| 487 | 483 | Value.slice_len_index => s.len.*, |
| 488 | 484 | else => unreachable, |
| 489 | 485 | }, |
| 490 | .bytes => |b| .{ .interned = try zcu.intern(.{ .int = .{ | |
| 486 | .bytes => |b| .{ .interned = try pt.intern(.{ .int = .{ | |
| 491 | 487 | .ty = .u8_type, |
| 492 | 488 | .storage = .{ .u64 = b.data[field_idx] }, |
| 493 | 489 | } }) }, |
src/print_air.zig+19-19| ... | ... | @@ -9,7 +9,7 @@ const Air = @import("Air.zig"); |
| 9 | 9 | const Liveness = @import("Liveness.zig"); |
| 10 | 10 | const InternPool = @import("InternPool.zig"); |
| 11 | 11 | |
| 12 | pub fn write(stream: anytype, module: *Zcu, air: Air, liveness: ?Liveness) void { | |
| 12 | pub fn write(stream: anytype, pt: Zcu.PerThread, air: Air, liveness: ?Liveness) void { | |
| 13 | 13 | const instruction_bytes = air.instructions.len * |
| 14 | 14 | // Here we don't use @sizeOf(Air.Inst.Data) because it would include |
| 15 | 15 | // the debug safety tag but we want to measure release size. |
| ... | ... | @@ -42,8 +42,8 @@ pub fn write(stream: anytype, module: *Zcu, air: Air, liveness: ?Liveness) void |
| 42 | 42 | // zig fmt: on |
| 43 | 43 | |
| 44 | 44 | var writer: Writer = .{ |
| 45 | .module = module, | |
| 46 | .gpa = module.gpa, | |
| 45 | .pt = pt, | |
| 46 | .gpa = pt.zcu.gpa, | |
| 47 | 47 | .air = air, |
| 48 | 48 | .liveness = liveness, |
| 49 | 49 | .indent = 2, |
| ... | ... | @@ -55,13 +55,13 @@ pub fn write(stream: anytype, module: *Zcu, air: Air, liveness: ?Liveness) void |
| 55 | 55 | pub fn writeInst( |
| 56 | 56 | stream: anytype, |
| 57 | 57 | inst: Air.Inst.Index, |
| 58 | module: *Zcu, | |
| 58 | pt: Zcu.PerThread, | |
| 59 | 59 | air: Air, |
| 60 | 60 | liveness: ?Liveness, |
| 61 | 61 | ) void { |
| 62 | 62 | var writer: Writer = .{ |
| 63 | .module = module, | |
| 64 | .gpa = module.gpa, | |
| 63 | .pt = pt, | |
| 64 | .gpa = pt.zcu.gpa, | |
| 65 | 65 | .air = air, |
| 66 | 66 | .liveness = liveness, |
| 67 | 67 | .indent = 2, |
| ... | ... | @@ -70,16 +70,16 @@ pub fn writeInst( |
| 70 | 70 | writer.writeInst(stream, inst) catch return; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | pub fn dump(module: *Zcu, air: Air, liveness: ?Liveness) void { | |
| 74 | write(std.io.getStdErr().writer(), module, air, liveness); | |
| 73 | pub fn dump(pt: Zcu.PerThread, air: Air, liveness: ?Liveness) void { | |
| 74 | write(std.io.getStdErr().writer(), pt, air, liveness); | |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | pub fn dumpInst(inst: Air.Inst.Index, module: *Zcu, air: Air, liveness: ?Liveness) void { | |
| 78 | writeInst(std.io.getStdErr().writer(), inst, module, air, liveness); | |
| 77 | pub fn dumpInst(inst: Air.Inst.Index, pt: Zcu.PerThread, air: Air, liveness: ?Liveness) void { | |
| 78 | writeInst(std.io.getStdErr().writer(), inst, pt, air, liveness); | |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | 81 | const Writer = struct { |
| 82 | module: *Zcu, | |
| 82 | pt: Zcu.PerThread, | |
| 83 | 83 | gpa: Allocator, |
| 84 | 84 | air: Air, |
| 85 | 85 | liveness: ?Liveness, |
| ... | ... | @@ -345,7 +345,7 @@ const Writer = struct { |
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | fn writeType(w: *Writer, s: anytype, ty: Type) !void { |
| 348 | return ty.print(s, w.module); | |
| 348 | return ty.print(s, w.pt); | |
| 349 | 349 | } |
| 350 | 350 | |
| 351 | 351 | fn writeTy(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { |
| ... | ... | @@ -424,7 +424,7 @@ const Writer = struct { |
| 424 | 424 | } |
| 425 | 425 | |
| 426 | 426 | fn writeAggregateInit(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { |
| 427 | const mod = w.module; | |
| 427 | const mod = w.pt.zcu; | |
| 428 | 428 | const ty_pl = w.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl; |
| 429 | 429 | const vector_ty = ty_pl.ty.toType(); |
| 430 | 430 | const len = @as(usize, @intCast(vector_ty.arrayLen(mod))); |
| ... | ... | @@ -504,7 +504,7 @@ const Writer = struct { |
| 504 | 504 | } |
| 505 | 505 | |
| 506 | 506 | fn writeSelect(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { |
| 507 | const mod = w.module; | |
| 507 | const mod = w.pt.zcu; | |
| 508 | 508 | const pl_op = w.air.instructions.items(.data)[@intFromEnum(inst)].pl_op; |
| 509 | 509 | const extra = w.air.extraData(Air.Bin, pl_op.payload).data; |
| 510 | 510 | |
| ... | ... | @@ -947,11 +947,11 @@ const Writer = struct { |
| 947 | 947 | if (@intFromEnum(operand) < InternPool.static_len) { |
| 948 | 948 | return s.print("@{}", .{operand}); |
| 949 | 949 | } else if (operand.toInterned()) |ip_index| { |
| 950 | const mod = w.module; | |
| 951 | const ty = Type.fromInterned(mod.intern_pool.indexToKey(ip_index).typeOf()); | |
| 950 | const pt = w.pt; | |
| 951 | const ty = Type.fromInterned(pt.zcu.intern_pool.indexToKey(ip_index).typeOf()); | |
| 952 | 952 | try s.print("<{}, {}>", .{ |
| 953 | ty.fmt(mod), | |
| 954 | Value.fromInterned(ip_index).fmtValue(mod, null), | |
| 953 | ty.fmt(pt), | |
| 954 | Value.fromInterned(ip_index).fmtValue(pt, null), | |
| 955 | 955 | }); |
| 956 | 956 | } else { |
| 957 | 957 | return w.writeInstIndex(s, operand.toIndex().?, dies); |
| ... | ... | @@ -970,7 +970,7 @@ const Writer = struct { |
| 970 | 970 | } |
| 971 | 971 | |
| 972 | 972 | fn typeOfIndex(w: *Writer, inst: Air.Inst.Index) Type { |
| 973 | const mod = w.module; | |
| 973 | const mod = w.pt.zcu; | |
| 974 | 974 | return w.air.typeOfIndex(inst, &mod.intern_pool); |
| 975 | 975 | } |
| 976 | 976 | }; |
src/print_value.zig+53-52| ... | ... | @@ -5,8 +5,6 @@ const std = @import("std"); |
| 5 | 5 | const Type = @import("Type.zig"); |
| 6 | 6 | const Value = @import("Value.zig"); |
| 7 | 7 | const Zcu = @import("Zcu.zig"); |
| 8 | /// Deprecated. | |
| 9 | const Module = Zcu; | |
| 10 | 8 | const Sema = @import("Sema.zig"); |
| 11 | 9 | const InternPool = @import("InternPool.zig"); |
| 12 | 10 | const Allocator = std.mem.Allocator; |
| ... | ... | @@ -17,7 +15,7 @@ const max_string_len = 256; |
| 17 | 15 | |
| 18 | 16 | pub const FormatContext = struct { |
| 19 | 17 | val: Value, |
| 20 | mod: *Module, | |
| 18 | pt: Zcu.PerThread, | |
| 21 | 19 | opt_sema: ?*Sema, |
| 22 | 20 | depth: u8, |
| 23 | 21 | }; |
| ... | ... | @@ -30,7 +28,7 @@ pub fn format( |
| 30 | 28 | ) !void { |
| 31 | 29 | _ = options; |
| 32 | 30 | comptime std.debug.assert(fmt.len == 0); |
| 33 | return print(ctx.val, writer, ctx.depth, ctx.mod, ctx.opt_sema) catch |err| switch (err) { | |
| 31 | return print(ctx.val, writer, ctx.depth, ctx.pt, ctx.opt_sema) catch |err| switch (err) { | |
| 34 | 32 | error.OutOfMemory => @panic("OOM"), // We're not allowed to return this from a format function |
| 35 | 33 | error.ComptimeBreak, error.ComptimeReturn => unreachable, |
| 36 | 34 | error.AnalysisFail => unreachable, // TODO: re-evaluate when we use `opt_sema` more fully |
| ... | ... | @@ -42,10 +40,11 @@ pub fn print( |
| 42 | 40 | val: Value, |
| 43 | 41 | writer: anytype, |
| 44 | 42 | level: u8, |
| 45 | mod: *Module, | |
| 43 | pt: Zcu.PerThread, | |
| 46 | 44 | /// If this `Sema` is provided, we will recurse through pointers where possible to provide friendly output. |
| 47 | 45 | opt_sema: ?*Sema, |
| 48 | ) (@TypeOf(writer).Error || Module.CompileError)!void { | |
| 46 | ) (@TypeOf(writer).Error || Zcu.CompileError)!void { | |
| 47 | const mod = pt.zcu; | |
| 49 | 48 | const ip = &mod.intern_pool; |
| 50 | 49 | switch (ip.indexToKey(val.toIntern())) { |
| 51 | 50 | .int_type, |
| ... | ... | @@ -64,7 +63,7 @@ pub fn print( |
| 64 | 63 | .func_type, |
| 65 | 64 | .error_set_type, |
| 66 | 65 | .inferred_error_set_type, |
| 67 | => try Type.print(val.toType(), writer, mod), | |
| 66 | => try Type.print(val.toType(), writer, pt), | |
| 68 | 67 | .undef => try writer.writeAll("undefined"), |
| 69 | 68 | .simple_value => |simple_value| switch (simple_value) { |
| 70 | 69 | .void => try writer.writeAll("{}"), |
| ... | ... | @@ -82,13 +81,13 @@ pub fn print( |
| 82 | 81 | .int => |int| switch (int.storage) { |
| 83 | 82 | inline .u64, .i64, .big_int => |x| try writer.print("{}", .{x}), |
| 84 | 83 | .lazy_align => |ty| if (opt_sema != null) { |
| 85 | const a = (try Type.fromInterned(ty).abiAlignmentAdvanced(mod, .sema)).scalar; | |
| 84 | const a = (try Type.fromInterned(ty).abiAlignmentAdvanced(pt, .sema)).scalar; | |
| 86 | 85 | try writer.print("{}", .{a.toByteUnits() orelse 0}); |
| 87 | } else try writer.print("@alignOf({})", .{Type.fromInterned(ty).fmt(mod)}), | |
| 86 | } else try writer.print("@alignOf({})", .{Type.fromInterned(ty).fmt(pt)}), | |
| 88 | 87 | .lazy_size => |ty| if (opt_sema != null) { |
| 89 | const s = (try Type.fromInterned(ty).abiSizeAdvanced(mod, .sema)).scalar; | |
| 88 | const s = (try Type.fromInterned(ty).abiSizeAdvanced(pt, .sema)).scalar; | |
| 90 | 89 | try writer.print("{}", .{s}); |
| 91 | } else try writer.print("@sizeOf({})", .{Type.fromInterned(ty).fmt(mod)}), | |
| 90 | } else try writer.print("@sizeOf({})", .{Type.fromInterned(ty).fmt(pt)}), | |
| 92 | 91 | }, |
| 93 | 92 | .err => |err| try writer.print("error.{}", .{ |
| 94 | 93 | err.name.fmt(ip), |
| ... | ... | @@ -97,7 +96,7 @@ pub fn print( |
| 97 | 96 | .err_name => |err_name| try writer.print("error.{}", .{ |
| 98 | 97 | err_name.fmt(ip), |
| 99 | 98 | }), |
| 100 | .payload => |payload| try print(Value.fromInterned(payload), writer, level, mod, opt_sema), | |
| 99 | .payload => |payload| try print(Value.fromInterned(payload), writer, level, pt, opt_sema), | |
| 101 | 100 | }, |
| 102 | 101 | .enum_literal => |enum_literal| try writer.print(".{}", .{ |
| 103 | 102 | enum_literal.fmt(ip), |
| ... | ... | @@ -111,7 +110,7 @@ pub fn print( |
| 111 | 110 | return writer.writeAll("@enumFromInt(...)"); |
| 112 | 111 | } |
| 113 | 112 | try writer.writeAll("@enumFromInt("); |
| 114 | try print(Value.fromInterned(enum_tag.int), writer, level - 1, mod, opt_sema); | |
| 113 | try print(Value.fromInterned(enum_tag.int), writer, level - 1, pt, opt_sema); | |
| 115 | 114 | try writer.writeAll(")"); |
| 116 | 115 | }, |
| 117 | 116 | .empty_enum_value => try writer.writeAll("(empty enum value)"), |
| ... | ... | @@ -128,12 +127,12 @@ pub fn print( |
| 128 | 127 | // TODO: eventually we want to load the slice as an array with `opt_sema`, but that's |
| 129 | 128 | // currently not possible without e.g. triggering compile errors. |
| 130 | 129 | } |
| 131 | try printPtr(Value.fromInterned(slice.ptr), writer, level, mod, opt_sema); | |
| 130 | try printPtr(Value.fromInterned(slice.ptr), writer, level, pt, opt_sema); | |
| 132 | 131 | try writer.writeAll("[0.."); |
| 133 | 132 | if (level == 0) { |
| 134 | 133 | try writer.writeAll("(...)"); |
| 135 | 134 | } else { |
| 136 | try print(Value.fromInterned(slice.len), writer, level - 1, mod, opt_sema); | |
| 135 | try print(Value.fromInterned(slice.len), writer, level - 1, pt, opt_sema); | |
| 137 | 136 | } |
| 138 | 137 | try writer.writeAll("]"); |
| 139 | 138 | }, |
| ... | ... | @@ -147,28 +146,28 @@ pub fn print( |
| 147 | 146 | // TODO: eventually we want to load the pointer with `opt_sema`, but that's |
| 148 | 147 | // currently not possible without e.g. triggering compile errors. |
| 149 | 148 | } |
| 150 | try printPtr(val, writer, level, mod, opt_sema); | |
| 149 | try printPtr(val, writer, level, pt, opt_sema); | |
| 151 | 150 | }, |
| 152 | 151 | .opt => |opt| switch (opt.val) { |
| 153 | 152 | .none => try writer.writeAll("null"), |
| 154 | else => |payload| try print(Value.fromInterned(payload), writer, level, mod, opt_sema), | |
| 153 | else => |payload| try print(Value.fromInterned(payload), writer, level, pt, opt_sema), | |
| 155 | 154 | }, |
| 156 | .aggregate => |aggregate| try printAggregate(val, aggregate, false, writer, level, mod, opt_sema), | |
| 155 | .aggregate => |aggregate| try printAggregate(val, aggregate, false, writer, level, pt, opt_sema), | |
| 157 | 156 | .un => |un| { |
| 158 | 157 | if (level == 0) { |
| 159 | 158 | try writer.writeAll(".{ ... }"); |
| 160 | 159 | return; |
| 161 | 160 | } |
| 162 | 161 | if (un.tag == .none) { |
| 163 | const backing_ty = try val.typeOf(mod).unionBackingType(mod); | |
| 164 | try writer.print("@bitCast(@as({}, ", .{backing_ty.fmt(mod)}); | |
| 165 | try print(Value.fromInterned(un.val), writer, level - 1, mod, opt_sema); | |
| 162 | const backing_ty = try val.typeOf(mod).unionBackingType(pt); | |
| 163 | try writer.print("@bitCast(@as({}, ", .{backing_ty.fmt(pt)}); | |
| 164 | try print(Value.fromInterned(un.val), writer, level - 1, pt, opt_sema); | |
| 166 | 165 | try writer.writeAll("))"); |
| 167 | 166 | } else { |
| 168 | 167 | try writer.writeAll(".{ "); |
| 169 | try print(Value.fromInterned(un.tag), writer, level - 1, mod, opt_sema); | |
| 168 | try print(Value.fromInterned(un.tag), writer, level - 1, pt, opt_sema); | |
| 170 | 169 | try writer.writeAll(" = "); |
| 171 | try print(Value.fromInterned(un.val), writer, level - 1, mod, opt_sema); | |
| 170 | try print(Value.fromInterned(un.val), writer, level - 1, pt, opt_sema); | |
| 172 | 171 | try writer.writeAll(" }"); |
| 173 | 172 | } |
| 174 | 173 | }, |
| ... | ... | @@ -182,13 +181,14 @@ fn printAggregate( |
| 182 | 181 | is_ref: bool, |
| 183 | 182 | writer: anytype, |
| 184 | 183 | level: u8, |
| 185 | zcu: *Zcu, | |
| 184 | pt: Zcu.PerThread, | |
| 186 | 185 | opt_sema: ?*Sema, |
| 187 | ) (@TypeOf(writer).Error || Module.CompileError)!void { | |
| 186 | ) (@TypeOf(writer).Error || Zcu.CompileError)!void { | |
| 188 | 187 | if (level == 0) { |
| 189 | 188 | if (is_ref) try writer.writeByte('&'); |
| 190 | 189 | return writer.writeAll(".{ ... }"); |
| 191 | 190 | } |
| 191 | const zcu = pt.zcu; | |
| 192 | 192 | const ip = &zcu.intern_pool; |
| 193 | 193 | const ty = Type.fromInterned(aggregate.ty); |
| 194 | 194 | switch (ty.zigTypeTag(zcu)) { |
| ... | ... | @@ -203,7 +203,7 @@ fn printAggregate( |
| 203 | 203 | if (i != 0) try writer.writeAll(", "); |
| 204 | 204 | const field_name = ty.structFieldName(@intCast(i), zcu).unwrap().?; |
| 205 | 205 | try writer.print(".{i} = ", .{field_name.fmt(ip)}); |
| 206 | try print(try val.fieldValue(zcu, i), writer, level - 1, zcu, opt_sema); | |
| 206 | try print(try val.fieldValue(pt, i), writer, level - 1, pt, opt_sema); | |
| 207 | 207 | } |
| 208 | 208 | try writer.writeAll(" }"); |
| 209 | 209 | return; |
| ... | ... | @@ -230,7 +230,7 @@ fn printAggregate( |
| 230 | 230 | if (ty.childType(zcu).toIntern() != .u8_type) break :one_byte_str; |
| 231 | 231 | const elem_val = Value.fromInterned(aggregate.storage.values()[0]); |
| 232 | 232 | if (elem_val.isUndef(zcu)) break :one_byte_str; |
| 233 | const byte = elem_val.toUnsignedInt(zcu); | |
| 233 | const byte = elem_val.toUnsignedInt(pt); | |
| 234 | 234 | try writer.print("\"{}\"", .{std.zig.fmtEscapes(&.{@intCast(byte)})}); |
| 235 | 235 | if (!is_ref) try writer.writeAll(".*"); |
| 236 | 236 | return; |
| ... | ... | @@ -253,7 +253,7 @@ fn printAggregate( |
| 253 | 253 | const max_len = @min(len, max_aggregate_items); |
| 254 | 254 | for (0..max_len) |i| { |
| 255 | 255 | if (i != 0) try writer.writeAll(", "); |
| 256 | try print(try val.fieldValue(zcu, i), writer, level - 1, zcu, opt_sema); | |
| 256 | try print(try val.fieldValue(pt, i), writer, level - 1, pt, opt_sema); | |
| 257 | 257 | } |
| 258 | 258 | if (len > max_aggregate_items) { |
| 259 | 259 | try writer.writeAll(", ..."); |
| ... | ... | @@ -261,8 +261,8 @@ fn printAggregate( |
| 261 | 261 | return writer.writeAll(" }"); |
| 262 | 262 | } |
| 263 | 263 | |
| 264 | fn printPtr(ptr_val: Value, writer: anytype, level: u8, zcu: *Zcu, opt_sema: ?*Sema) (@TypeOf(writer).Error || Module.CompileError)!void { | |
| 265 | const ptr = switch (zcu.intern_pool.indexToKey(ptr_val.toIntern())) { | |
| 264 | fn printPtr(ptr_val: Value, writer: anytype, level: u8, pt: Zcu.PerThread, opt_sema: ?*Sema) (@TypeOf(writer).Error || Zcu.CompileError)!void { | |
| 265 | const ptr = switch (pt.zcu.intern_pool.indexToKey(ptr_val.toIntern())) { | |
| 266 | 266 | .undef => return writer.writeAll("undefined"), |
| 267 | 267 | .ptr => |ptr| ptr, |
| 268 | 268 | else => unreachable, |
| ... | ... | @@ -270,32 +270,33 @@ fn printPtr(ptr_val: Value, writer: anytype, level: u8, zcu: *Zcu, opt_sema: ?*S |
| 270 | 270 | |
| 271 | 271 | if (ptr.base_addr == .anon_decl) { |
| 272 | 272 | // If the value is an aggregate, we can potentially print it more nicely. |
| 273 | switch (zcu.intern_pool.indexToKey(ptr.base_addr.anon_decl.val)) { | |
| 273 | switch (pt.zcu.intern_pool.indexToKey(ptr.base_addr.anon_decl.val)) { | |
| 274 | 274 | .aggregate => |agg| return printAggregate( |
| 275 | 275 | Value.fromInterned(ptr.base_addr.anon_decl.val), |
| 276 | 276 | agg, |
| 277 | 277 | true, |
| 278 | 278 | writer, |
| 279 | 279 | level, |
| 280 | zcu, | |
| 280 | pt, | |
| 281 | 281 | opt_sema, |
| 282 | 282 | ), |
| 283 | 283 | else => {}, |
| 284 | 284 | } |
| 285 | 285 | } |
| 286 | 286 | |
| 287 | var arena = std.heap.ArenaAllocator.init(zcu.gpa); | |
| 287 | var arena = std.heap.ArenaAllocator.init(pt.zcu.gpa); | |
| 288 | 288 | defer arena.deinit(); |
| 289 | const derivation = try ptr_val.pointerDerivationAdvanced(arena.allocator(), zcu, opt_sema); | |
| 290 | try printPtrDerivation(derivation, writer, level, zcu, opt_sema); | |
| 289 | const derivation = try ptr_val.pointerDerivationAdvanced(arena.allocator(), pt, opt_sema); | |
| 290 | try printPtrDerivation(derivation, writer, level, pt, opt_sema); | |
| 291 | 291 | } |
| 292 | 292 | |
| 293 | 293 | /// Print `derivation` as an lvalue, i.e. such that writing `&` before this gives the pointer value. |
| 294 | fn printPtrDerivation(derivation: Value.PointerDeriveStep, writer: anytype, level: u8, zcu: *Zcu, opt_sema: ?*Sema) (@TypeOf(writer).Error || Module.CompileError)!void { | |
| 294 | fn printPtrDerivation(derivation: Value.PointerDeriveStep, writer: anytype, level: u8, pt: Zcu.PerThread, opt_sema: ?*Sema) (@TypeOf(writer).Error || Zcu.CompileError)!void { | |
| 295 | const zcu = pt.zcu; | |
| 295 | 296 | const ip = &zcu.intern_pool; |
| 296 | 297 | switch (derivation) { |
| 297 | 298 | .int => |int| try writer.print("@as({}, @ptrFromInt({x})).*", .{ |
| 298 | int.ptr_ty.fmt(zcu), | |
| 299 | int.ptr_ty.fmt(pt), | |
| 299 | 300 | int.addr, |
| 300 | 301 | }), |
| 301 | 302 | .decl_ptr => |decl| { |
| ... | ... | @@ -303,33 +304,33 @@ fn printPtrDerivation(derivation: Value.PointerDeriveStep, writer: anytype, leve |
| 303 | 304 | }, |
| 304 | 305 | .anon_decl_ptr => |anon| { |
| 305 | 306 | const ty = Value.fromInterned(anon.val).typeOf(zcu); |
| 306 | try writer.print("@as({}, ", .{ty.fmt(zcu)}); | |
| 307 | try print(Value.fromInterned(anon.val), writer, level - 1, zcu, opt_sema); | |
| 307 | try writer.print("@as({}, ", .{ty.fmt(pt)}); | |
| 308 | try print(Value.fromInterned(anon.val), writer, level - 1, pt, opt_sema); | |
| 308 | 309 | try writer.writeByte(')'); |
| 309 | 310 | }, |
| 310 | 311 | .comptime_alloc_ptr => |info| { |
| 311 | try writer.print("@as({}, ", .{info.val.typeOf(zcu).fmt(zcu)}); | |
| 312 | try print(info.val, writer, level - 1, zcu, opt_sema); | |
| 312 | try writer.print("@as({}, ", .{info.val.typeOf(zcu).fmt(pt)}); | |
| 313 | try print(info.val, writer, level - 1, pt, opt_sema); | |
| 313 | 314 | try writer.writeByte(')'); |
| 314 | 315 | }, |
| 315 | 316 | .comptime_field_ptr => |val| { |
| 316 | 317 | const ty = val.typeOf(zcu); |
| 317 | try writer.print("@as({}, ", .{ty.fmt(zcu)}); | |
| 318 | try print(val, writer, level - 1, zcu, opt_sema); | |
| 318 | try writer.print("@as({}, ", .{ty.fmt(pt)}); | |
| 319 | try print(val, writer, level - 1, pt, opt_sema); | |
| 319 | 320 | try writer.writeByte(')'); |
| 320 | 321 | }, |
| 321 | 322 | .eu_payload_ptr => |info| { |
| 322 | 323 | try writer.writeByte('('); |
| 323 | try printPtrDerivation(info.parent.*, writer, level, zcu, opt_sema); | |
| 324 | try printPtrDerivation(info.parent.*, writer, level, pt, opt_sema); | |
| 324 | 325 | try writer.writeAll(" catch unreachable)"); |
| 325 | 326 | }, |
| 326 | 327 | .opt_payload_ptr => |info| { |
| 327 | try printPtrDerivation(info.parent.*, writer, level, zcu, opt_sema); | |
| 328 | try printPtrDerivation(info.parent.*, writer, level, pt, opt_sema); | |
| 328 | 329 | try writer.writeAll(".?"); |
| 329 | 330 | }, |
| 330 | 331 | .field_ptr => |field| { |
| 331 | try printPtrDerivation(field.parent.*, writer, level, zcu, opt_sema); | |
| 332 | const agg_ty = (try field.parent.ptrType(zcu)).childType(zcu); | |
| 332 | try printPtrDerivation(field.parent.*, writer, level, pt, opt_sema); | |
| 333 | const agg_ty = (try field.parent.ptrType(pt)).childType(zcu); | |
| 333 | 334 | switch (agg_ty.zigTypeTag(zcu)) { |
| 334 | 335 | .Struct => if (agg_ty.structFieldName(field.field_idx, zcu).unwrap()) |field_name| { |
| 335 | 336 | try writer.print(".{i}", .{field_name.fmt(ip)}); |
| ... | ... | @@ -350,16 +351,16 @@ fn printPtrDerivation(derivation: Value.PointerDeriveStep, writer: anytype, leve |
| 350 | 351 | } |
| 351 | 352 | }, |
| 352 | 353 | .elem_ptr => |elem| { |
| 353 | try printPtrDerivation(elem.parent.*, writer, level, zcu, opt_sema); | |
| 354 | try printPtrDerivation(elem.parent.*, writer, level, pt, opt_sema); | |
| 354 | 355 | try writer.print("[{d}]", .{elem.elem_idx}); |
| 355 | 356 | }, |
| 356 | 357 | .offset_and_cast => |oac| if (oac.byte_offset == 0) { |
| 357 | try writer.print("@as({}, @ptrCast(", .{oac.new_ptr_ty.fmt(zcu)}); | |
| 358 | try printPtrDerivation(oac.parent.*, writer, level, zcu, opt_sema); | |
| 358 | try writer.print("@as({}, @ptrCast(", .{oac.new_ptr_ty.fmt(pt)}); | |
| 359 | try printPtrDerivation(oac.parent.*, writer, level, pt, opt_sema); | |
| 359 | 360 | try writer.writeAll("))"); |
| 360 | 361 | } else { |
| 361 | try writer.print("@as({}, @ptrFromInt(@intFromPtr(", .{oac.new_ptr_ty.fmt(zcu)}); | |
| 362 | try printPtrDerivation(oac.parent.*, writer, level, zcu, opt_sema); | |
| 362 | try writer.print("@as({}, @ptrFromInt(@intFromPtr(", .{oac.new_ptr_ty.fmt(pt)}); | |
| 363 | try printPtrDerivation(oac.parent.*, writer, level, pt, opt_sema); | |
| 363 | 364 | try writer.print(") + {d}))", .{oac.byte_offset}); |
| 364 | 365 | }, |
| 365 | 366 | } |
src/print_zir.zig+4-5| ... | ... | @@ -7,13 +7,12 @@ const InternPool = @import("InternPool.zig"); |
| 7 | 7 | |
| 8 | 8 | const Zir = std.zig.Zir; |
| 9 | 9 | const Zcu = @import("Zcu.zig"); |
| 10 | const Module = Zcu; | |
| 11 | 10 | const LazySrcLoc = Zcu.LazySrcLoc; |
| 12 | 11 | |
| 13 | 12 | /// Write human-readable, debug formatted ZIR code to a file. |
| 14 | 13 | pub fn renderAsTextToFile( |
| 15 | 14 | gpa: Allocator, |
| 16 | scope_file: *Module.File, | |
| 15 | scope_file: *Zcu.File, | |
| 17 | 16 | fs_file: std.fs.File, |
| 18 | 17 | ) !void { |
| 19 | 18 | var arena = std.heap.ArenaAllocator.init(gpa); |
| ... | ... | @@ -64,7 +63,7 @@ pub fn renderInstructionContext( |
| 64 | 63 | gpa: Allocator, |
| 65 | 64 | block: []const Zir.Inst.Index, |
| 66 | 65 | block_index: usize, |
| 67 | scope_file: *Module.File, | |
| 66 | scope_file: *Zcu.File, | |
| 68 | 67 | parent_decl_node: Ast.Node.Index, |
| 69 | 68 | indent: u32, |
| 70 | 69 | stream: anytype, |
| ... | ... | @@ -96,7 +95,7 @@ pub fn renderInstructionContext( |
| 96 | 95 | pub fn renderSingleInstruction( |
| 97 | 96 | gpa: Allocator, |
| 98 | 97 | inst: Zir.Inst.Index, |
| 99 | scope_file: *Module.File, | |
| 98 | scope_file: *Zcu.File, | |
| 100 | 99 | parent_decl_node: Ast.Node.Index, |
| 101 | 100 | indent: u32, |
| 102 | 101 | stream: anytype, |
| ... | ... | @@ -122,7 +121,7 @@ pub fn renderSingleInstruction( |
| 122 | 121 | const Writer = struct { |
| 123 | 122 | gpa: Allocator, |
| 124 | 123 | arena: Allocator, |
| 125 | file: *Module.File, | |
| 124 | file: *Zcu.File, | |
| 126 | 125 | code: Zir, |
| 127 | 126 | indent: u32, |
| 128 | 127 | parent_decl_node: Ast.Node.Index, |
src/register_manager.zig-2| ... | ... | @@ -7,8 +7,6 @@ const Air = @import("Air.zig"); |
| 7 | 7 | const StaticBitSet = std.bit_set.StaticBitSet; |
| 8 | 8 | const Type = @import("Type.zig"); |
| 9 | 9 | const Zcu = @import("Zcu.zig"); |
| 10 | /// Deprecated. | |
| 11 | const Module = Zcu; | |
| 12 | 10 | const expect = std.testing.expect; |
| 13 | 11 | const expectEqual = std.testing.expectEqual; |
| 14 | 12 | const expectEqualSlices = std.testing.expectEqualSlices; |