authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-12-15 16:12:53-08:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-15 15:11:35-08:00
log4ecc4addc432792514a86f6a8fde0285de4468b2
treebc177b477d5acdfb4b255595210b905558ddae90
parent098e0b1906479e6aa3c4afb3aeff5c85504521c2

wasm codegen: remove dependency on PerThread where possible


1 files changed, 81 insertions(+), 148 deletions(-)

src/arch/wasm/CodeGen.zig+81-148
......@@ -1233,7 +1233,7 @@ pub fn function(
12331233 const returns = fn_ty_index.ptr(wasm).returns.slice(wasm);
12341234 const any_returns = returns.len != 0;
12351235
1236 var cc_result = try resolveCallingConventionValues(pt, fn_ty, target);
1236 var cc_result = try resolveCallingConventionValues(zcu, fn_ty, target);
12371237 defer cc_result.deinit(gpa);
12381238
12391239 var code_gen: CodeGen = .{
......@@ -1267,8 +1267,7 @@ pub fn function(
12671267
12681268fn functionInner(cg: *CodeGen, any_returns: bool) InnerError!Function {
12691269 const wasm = cg.wasm;
1270 const pt = cg.pt;
1271 const zcu = pt.zcu;
1270 const zcu = cg.pt.zcu;
12721271
12731272 const start_mir_off: u32 = @intCast(wasm.mir_instructions.len);
12741273 const start_mir_extra_off: u32 = @intCast(wasm.mir_extra.items.len);
......@@ -1325,11 +1324,10 @@ const CallWValues = struct {
13251324};
13261325
13271326fn resolveCallingConventionValues(
1328 pt: Zcu.PerThread,
1327 zcu: *const Zcu,
13291328 fn_ty: Type,
13301329 target: *const std.Target,
13311330) Allocator.Error!CallWValues {
1332 const zcu = pt.zcu;
13331331 const gpa = zcu.gpa;
13341332 const ip = &zcu.intern_pool;
13351333 const fn_info = zcu.typeToFunc(fn_ty).?;
......@@ -1407,8 +1405,7 @@ fn lowerArg(cg: *CodeGen, cc: std.builtin.CallingConvention, ty: Type, value: WV
14071405 return cg.lowerToStack(value);
14081406 }
14091407
1410 const pt = cg.pt;
1411 const zcu = pt.zcu;
1408 const zcu = cg.pt.zcu;
14121409 const ty_classes = abi.classifyType(ty, zcu);
14131410 assert(ty_classes[0] != .none);
14141411 switch (ty.zigTypeTag(zcu)) {
......@@ -1494,7 +1491,8 @@ fn restoreStackPointer(cg: *CodeGen) !void {
14941491///
14951492/// Asserts Type has codegenbits
14961493fn allocStack(cg: *CodeGen, ty: Type) !WValue {
1497 const zcu = cg.pt.zcu;
1494 const pt = cg.pt;
1495 const zcu = pt.zcu;
14981496 assert(ty.hasRuntimeBitsIgnoreComptime(zcu));
14991497 if (cg.initial_stack_value == .none) {
15001498 try cg.initializeStack();
......@@ -1502,7 +1500,7 @@ fn allocStack(cg: *CodeGen, ty: Type) !WValue {
15021500
15031501 const abi_size = std.math.cast(u32, ty.abiSize(zcu)) orelse {
15041502 return cg.fail("Type {} with ABI size of {d} exceeds stack frame size", .{
1505 ty.fmt(cg.pt), ty.abiSize(zcu),
1503 ty.fmt(pt), ty.abiSize(zcu),
15061504 });
15071505 };
15081506 const abi_align = ty.abiAlignment(zcu);
......@@ -2038,8 +2036,7 @@ fn genInst(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
20382036}
20392037
20402038fn genBody(cg: *CodeGen, body: []const Air.Inst.Index) InnerError!void {
2041 const pt = cg.pt;
2042 const zcu = pt.zcu;
2039 const zcu = cg.pt.zcu;
20432040 const ip = &zcu.intern_pool;
20442041
20452042 for (body) |inst| {
......@@ -2060,8 +2057,7 @@ fn genBody(cg: *CodeGen, body: []const Air.Inst.Index) InnerError!void {
20602057}
20612058
20622059fn airRet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2063 const pt = cg.pt;
2064 const zcu = pt.zcu;
2060 const zcu = cg.pt.zcu;
20652061 const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
20662062 const operand = try cg.resolveInst(un_op);
20672063 const fn_info = zcu.typeToFunc(zcu.navValue(cg.owner_nav).typeOf(zcu)).?;
......@@ -2104,8 +2100,7 @@ fn airRet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
21042100}
21052101
21062102fn airRetPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2107 const pt = cg.pt;
2108 const zcu = pt.zcu;
2103 const zcu = cg.pt.zcu;
21092104 const child_type = cg.typeOfIndex(inst).childType(zcu);
21102105
21112106 const result = result: {
......@@ -2125,8 +2120,7 @@ fn airRetPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
21252120}
21262121
21272122fn airRetLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
2128 const pt = cg.pt;
2129 const zcu = pt.zcu;
2123 const zcu = cg.pt.zcu;
21302124 const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
21312125 const operand = try cg.resolveInst(un_op);
21322126 const ret_ty = cg.typeOf(un_op).childType(zcu);
......@@ -2452,8 +2446,7 @@ fn airLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
24522446/// Loads an operand from the linear memory section.
24532447/// NOTE: Leaves the value on the stack.
24542448fn load(cg: *CodeGen, operand: WValue, ty: Type, offset: u32) InnerError!WValue {
2455 const pt = cg.pt;
2456 const zcu = pt.zcu;
2449 const zcu = cg.pt.zcu;
24572450 // load local's value from memory by its stack position
24582451 try cg.emitWValue(operand);
24592452
......@@ -2526,8 +2519,7 @@ fn airArg(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
25262519}
25272520
25282521fn airBinOp(cg: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void {
2529 const pt = cg.pt;
2530 const zcu = pt.zcu;
2522 const zcu = cg.pt.zcu;
25312523 const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
25322524 const lhs = try cg.resolveInst(bin_op.lhs);
25332525 const rhs = try cg.resolveInst(bin_op.rhs);
......@@ -2594,8 +2586,7 @@ fn binOp(cg: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError!WV
25942586}
25952587
25962588fn binOpBigInt(cg: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError!WValue {
2597 const pt = cg.pt;
2598 const zcu = pt.zcu;
2589 const zcu = cg.pt.zcu;
25992590 const int_info = ty.intInfo(zcu);
26002591 if (int_info.bits > 128) {
26012592 return cg.fail("TODO: Implement binary operation for big integers larger than 128 bits", .{});
......@@ -2877,8 +2868,7 @@ fn airUnaryFloatOp(cg: *CodeGen, inst: Air.Inst.Index, op: FloatOp) InnerError!v
28772868}
28782869
28792870fn floatOp(cg: *CodeGen, float_op: FloatOp, ty: Type, args: []const WValue) InnerError!WValue {
2880 const pt = cg.pt;
2881 const zcu = pt.zcu;
2871 const zcu = cg.pt.zcu;
28822872 if (ty.zigTypeTag(zcu) == .vector) {
28832873 return cg.fail("TODO: Implement floatOps for vectors", .{});
28842874 }
......@@ -2952,8 +2942,7 @@ fn floatNeg(cg: *CodeGen, ty: Type, arg: WValue) InnerError!WValue {
29522942}
29532943
29542944fn airWrapBinOp(cg: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void {
2955 const pt = cg.pt;
2956 const zcu = pt.zcu;
2945 const zcu = cg.pt.zcu;
29572946 const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
29582947
29592948 const lhs = try cg.resolveInst(bin_op.lhs);
......@@ -3000,8 +2989,7 @@ fn wrapBinOp(cg: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerErro
30002989/// Asserts `Type` is <= 128 bits.
30012990/// NOTE: When the Type is <= 64 bits, leaves the value on top of the stack, if wrapping was needed.
30022991fn wrapOperand(cg: *CodeGen, operand: WValue, ty: Type) InnerError!WValue {
3003 const pt = cg.pt;
3004 const zcu = pt.zcu;
2992 const zcu = cg.pt.zcu;
30052993 assert(ty.abiSize(zcu) <= 16);
30062994 const int_bits: u16 = @intCast(ty.bitSize(zcu)); // TODO use ty.intInfo(zcu).bits
30072995 const wasm_bits = toWasmBits(int_bits) orelse {
......@@ -3275,8 +3263,7 @@ fn storeSimdImmd(cg: *CodeGen, value: [16]u8) !WValue {
32753263}
32763264
32773265fn emitUndefined(cg: *CodeGen, ty: Type) InnerError!WValue {
3278 const pt = cg.pt;
3279 const zcu = pt.zcu;
3266 const zcu = cg.pt.zcu;
32803267 const ip = &zcu.intern_pool;
32813268 switch (ty.zigTypeTag(zcu)) {
32823269 .bool, .error_set => return .{ .imm32 = 0xaaaaaaaa },
......@@ -3317,16 +3304,15 @@ fn emitUndefined(cg: *CodeGen, ty: Type) InnerError!WValue {
33173304/// It's illegal to provide a value with a type that cannot be represented
33183305/// as an integer value.
33193306fn valueAsI32(cg: *const CodeGen, val: Value) i32 {
3320 const pt = cg.pt;
3321 const zcu = pt.zcu;
3307 const zcu = cg.pt.zcu;
33223308 const ip = &zcu.intern_pool;
33233309
33243310 switch (val.toIntern()) {
33253311 .bool_true => return 1,
33263312 .bool_false => return 0,
33273313 else => return switch (ip.indexToKey(val.ip_index)) {
3328 .enum_tag => |enum_tag| intIndexAsI32(ip, enum_tag.int, pt),
3329 .int => |int| intStorageAsI32(int.storage, pt),
3314 .enum_tag => |enum_tag| intIndexAsI32(ip, enum_tag.int, zcu),
3315 .int => |int| intStorageAsI32(int.storage, zcu),
33303316 .ptr => |ptr| {
33313317 assert(ptr.base_addr == .int);
33323318 return @intCast(ptr.byte_offset);
......@@ -3337,12 +3323,11 @@ fn valueAsI32(cg: *const CodeGen, val: Value) i32 {
33373323 }
33383324}
33393325
3340fn intIndexAsI32(ip: *const InternPool, int: InternPool.Index, pt: Zcu.PerThread) i32 {
3341 return intStorageAsI32(ip.indexToKey(int).int.storage, pt);
3326fn intIndexAsI32(ip: *const InternPool, int: InternPool.Index, zcu: *const Zcu) i32 {
3327 return intStorageAsI32(ip.indexToKey(int).int.storage, zcu);
33423328}
33433329
3344fn intStorageAsI32(storage: InternPool.Key.Int.Storage, pt: Zcu.PerThread) i32 {
3345 const zcu = pt.zcu;
3330fn intStorageAsI32(storage: InternPool.Key.Int.Storage, zcu: *const Zcu) i32 {
33463331 return switch (storage) {
33473332 .i64 => |x| @as(i32, @intCast(x)),
33483333 .u64 => |x| @as(i32, @bitCast(@as(u32, @intCast(x)))),
......@@ -3359,8 +3344,7 @@ fn airBlock(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
33593344}
33603345
33613346fn lowerBlock(cg: *CodeGen, inst: Air.Inst.Index, block_ty: Type, body: []const Air.Inst.Index) InnerError!void {
3362 const pt = cg.pt;
3363 const zcu = pt.zcu;
3347 const zcu = cg.pt.zcu;
33643348 const wasm_block_ty = genBlockType(block_ty, zcu, cg.target);
33653349
33663350 // if wasm_block_ty is non-empty, we create a register to store the temporary value
......@@ -3478,8 +3462,7 @@ fn airCmp(cg: *CodeGen, inst: Air.Inst.Index, op: std.math.CompareOperator) Inne
34783462/// NOTE: This leaves the result on top of the stack, rather than a new local.
34793463fn cmp(cg: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: std.math.CompareOperator) InnerError!WValue {
34803464 assert(!(lhs != .stack and rhs == .stack));
3481 const pt = cg.pt;
3482 const zcu = pt.zcu;
3465 const zcu = cg.pt.zcu;
34833466 if (ty.zigTypeTag(zcu) == .optional and !ty.optionalReprIsPayload(zcu)) {
34843467 const payload_ty = ty.optionalChild(zcu);
34853468 if (payload_ty.hasRuntimeBitsIgnoreComptime(zcu)) {
......@@ -3699,8 +3682,7 @@ fn airUnreachable(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
36993682}
37003683
37013684fn airBitcast(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
3702 const pt = cg.pt;
3703 const zcu = pt.zcu;
3685 const zcu = cg.pt.zcu;
37043686 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
37053687 const operand = try cg.resolveInst(ty_op.operand);
37063688 const wanted_ty = cg.typeOfIndex(inst);
......@@ -3743,8 +3725,7 @@ fn airBitcast(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
37433725}
37443726
37453727fn bitcast(cg: *CodeGen, wanted_ty: Type, given_ty: Type, operand: WValue) InnerError!WValue {
3746 const pt = cg.pt;
3747 const zcu = pt.zcu;
3728 const zcu = cg.pt.zcu;
37483729 // if we bitcast a float to or from an integer we must use the 'reinterpret' instruction
37493730 if (!(wanted_ty.isAnyFloat() or given_ty.isAnyFloat())) return operand;
37503731 if (wanted_ty.ip_index == .f16_type or given_ty.ip_index == .f16_type) return operand;
......@@ -3762,8 +3743,7 @@ fn bitcast(cg: *CodeGen, wanted_ty: Type, given_ty: Type, operand: WValue) Inner
37623743}
37633744
37643745fn airStructFieldPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
3765 const pt = cg.pt;
3766 const zcu = pt.zcu;
3746 const zcu = cg.pt.zcu;
37673747 const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
37683748 const extra = cg.air.extraData(Air.StructField, ty_pl.payload);
37693749
......@@ -3775,8 +3755,7 @@ fn airStructFieldPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
37753755}
37763756
37773757fn airStructFieldPtrIndex(cg: *CodeGen, inst: Air.Inst.Index, index: u32) InnerError!void {
3778 const pt = cg.pt;
3779 const zcu = pt.zcu;
3758 const zcu = cg.pt.zcu;
37803759 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
37813760 const struct_ptr = try cg.resolveInst(ty_op.operand);
37823761 const struct_ptr_ty = cg.typeOf(ty_op.operand);
......@@ -4115,8 +4094,7 @@ fn airSwitchBr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
41154094}
41164095
41174096fn airIsErr(cg: *CodeGen, inst: Air.Inst.Index, opcode: std.wasm.Opcode) InnerError!void {
4118 const pt = cg.pt;
4119 const zcu = pt.zcu;
4097 const zcu = cg.pt.zcu;
41204098 const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
41214099 const operand = try cg.resolveInst(un_op);
41224100 const err_union_ty = cg.typeOf(un_op);
......@@ -4148,8 +4126,7 @@ fn airIsErr(cg: *CodeGen, inst: Air.Inst.Index, opcode: std.wasm.Opcode) InnerEr
41484126}
41494127
41504128fn airUnwrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void {
4151 const pt = cg.pt;
4152 const zcu = pt.zcu;
4129 const zcu = cg.pt.zcu;
41534130 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
41544131
41554132 const operand = try cg.resolveInst(ty_op.operand);
......@@ -4176,8 +4153,7 @@ fn airUnwrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool)
41764153}
41774154
41784155fn airUnwrapErrUnionError(cg: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void {
4179 const pt = cg.pt;
4180 const zcu = pt.zcu;
4156 const zcu = cg.pt.zcu;
41814157 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
41824158
41834159 const operand = try cg.resolveInst(ty_op.operand);
......@@ -4230,8 +4206,7 @@ fn airWrapErrUnionPayload(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
42304206}
42314207
42324208fn airWrapErrUnionErr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4233 const pt = cg.pt;
4234 const zcu = pt.zcu;
4209 const zcu = cg.pt.zcu;
42354210 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
42364211
42374212 const operand = try cg.resolveInst(ty_op.operand);
......@@ -4263,8 +4238,7 @@ fn airIntcast(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
42634238 const ty = ty_op.ty.toType();
42644239 const operand = try cg.resolveInst(ty_op.operand);
42654240 const operand_ty = cg.typeOf(ty_op.operand);
4266 const pt = cg.pt;
4267 const zcu = pt.zcu;
4241 const zcu = cg.pt.zcu;
42684242 if (ty.zigTypeTag(zcu) == .vector or operand_ty.zigTypeTag(zcu) == .vector) {
42694243 return cg.fail("todo Wasm intcast for vectors", .{});
42704244 }
......@@ -4287,8 +4261,7 @@ fn airIntcast(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
42874261/// Asserts type's bitsize <= 128
42884262/// NOTE: May leave the result on the top of the stack.
42894263fn intcast(cg: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerError!WValue {
4290 const pt = cg.pt;
4291 const zcu = pt.zcu;
4264 const zcu = cg.pt.zcu;
42924265 const given_bitsize = @as(u16, @intCast(given.bitSize(zcu)));
42934266 const wanted_bitsize = @as(u16, @intCast(wanted.bitSize(zcu)));
42944267 assert(given_bitsize <= 128);
......@@ -4337,8 +4310,7 @@ fn intcast(cg: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerError!
43374310}
43384311
43394312fn airIsNull(cg: *CodeGen, inst: Air.Inst.Index, opcode: std.wasm.Opcode, op_kind: enum { value, ptr }) InnerError!void {
4340 const pt = cg.pt;
4341 const zcu = pt.zcu;
4313 const zcu = cg.pt.zcu;
43424314 const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
43434315 const operand = try cg.resolveInst(un_op);
43444316
......@@ -4379,8 +4351,7 @@ fn isNull(cg: *CodeGen, operand: WValue, optional_ty: Type, opcode: std.wasm.Opc
43794351}
43804352
43814353fn airOptionalPayload(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4382 const pt = cg.pt;
4383 const zcu = pt.zcu;
4354 const zcu = cg.pt.zcu;
43844355 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
43854356 const opt_ty = cg.typeOf(ty_op.operand);
43864357 const payload_ty = cg.typeOfIndex(inst);
......@@ -4402,8 +4373,7 @@ fn airOptionalPayload(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
44024373}
44034374
44044375fn airOptionalPayloadPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4405 const pt = cg.pt;
4406 const zcu = pt.zcu;
4376 const zcu = cg.pt.zcu;
44074377 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
44084378 const operand = try cg.resolveInst(ty_op.operand);
44094379 const opt_ty = cg.typeOf(ty_op.operand).childType(zcu);
......@@ -4507,8 +4477,7 @@ fn airSliceLen(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
45074477}
45084478
45094479fn airSliceElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4510 const pt = cg.pt;
4511 const zcu = pt.zcu;
4480 const zcu = cg.pt.zcu;
45124481 const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
45134482
45144483 const slice_ty = cg.typeOf(bin_op.lhs);
......@@ -4535,8 +4504,7 @@ fn airSliceElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
45354504}
45364505
45374506fn airSliceElemPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4538 const pt = cg.pt;
4539 const zcu = pt.zcu;
4507 const zcu = cg.pt.zcu;
45404508 const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
45414509 const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data;
45424510
......@@ -4579,8 +4547,7 @@ fn airTrunc(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
45794547 const operand = try cg.resolveInst(ty_op.operand);
45804548 const wanted_ty: Type = ty_op.ty.toType();
45814549 const op_ty = cg.typeOf(ty_op.operand);
4582 const pt = cg.pt;
4583 const zcu = pt.zcu;
4550 const zcu = cg.pt.zcu;
45844551
45854552 if (wanted_ty.zigTypeTag(zcu) == .vector or op_ty.zigTypeTag(zcu) == .vector) {
45864553 return cg.fail("TODO: trunc for vectors", .{});
......@@ -4597,8 +4564,7 @@ fn airTrunc(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
45974564/// Truncates a given operand to a given type, discarding any overflown bits.
45984565/// NOTE: Resulting value is left on the stack.
45994566fn trunc(cg: *CodeGen, operand: WValue, wanted_ty: Type, given_ty: Type) InnerError!WValue {
4600 const pt = cg.pt;
4601 const zcu = pt.zcu;
4567 const zcu = cg.pt.zcu;
46024568 const given_bits = @as(u16, @intCast(given_ty.bitSize(zcu)));
46034569 if (toWasmBits(given_bits) == null) {
46044570 return cg.fail("TODO: Implement wasm integer truncation for integer bitsize: {d}", .{given_bits});
......@@ -4622,8 +4588,7 @@ fn airIntFromBool(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
46224588}
46234589
46244590fn airArrayToSlice(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4625 const pt = cg.pt;
4626 const zcu = pt.zcu;
4591 const zcu = cg.pt.zcu;
46274592 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
46284593
46294594 const operand = try cg.resolveInst(ty_op.operand);
......@@ -4646,8 +4611,7 @@ fn airArrayToSlice(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
46464611}
46474612
46484613fn airIntFromPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4649 const pt = cg.pt;
4650 const zcu = pt.zcu;
4614 const zcu = cg.pt.zcu;
46514615 const un_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
46524616 const operand = try cg.resolveInst(un_op);
46534617 const ptr_ty = cg.typeOf(un_op);
......@@ -4662,8 +4626,7 @@ fn airIntFromPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
46624626}
46634627
46644628fn airPtrElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4665 const pt = cg.pt;
4666 const zcu = pt.zcu;
4629 const zcu = cg.pt.zcu;
46674630 const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
46684631
46694632 const ptr_ty = cg.typeOf(bin_op.lhs);
......@@ -4694,8 +4657,7 @@ fn airPtrElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
46944657}
46954658
46964659fn airPtrElemPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4697 const pt = cg.pt;
4698 const zcu = pt.zcu;
4660 const zcu = cg.pt.zcu;
46994661 const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
47004662 const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data;
47014663
......@@ -4723,8 +4685,7 @@ fn airPtrElemPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
47234685}
47244686
47254687fn airPtrBinOp(cg: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void {
4726 const pt = cg.pt;
4727 const zcu = pt.zcu;
4688 const zcu = cg.pt.zcu;
47284689 const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
47294690 const bin_op = cg.air.extraData(Air.Bin, ty_pl.payload).data;
47304691
......@@ -4750,8 +4711,7 @@ fn airPtrBinOp(cg: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void {
47504711}
47514712
47524713fn airMemset(cg: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void {
4753 const pt = cg.pt;
4754 const zcu = pt.zcu;
4714 const zcu = cg.pt.zcu;
47554715 if (safety) {
47564716 // TODO if the value is undef, write 0xaa bytes to dest
47574717 } else {
......@@ -4784,8 +4744,8 @@ fn airMemset(cg: *CodeGen, inst: Air.Inst.Index, safety: bool) InnerError!void {
47844744/// this to wasm's memset instruction. When the feature is not present,
47854745/// we implement it manually.
47864746fn memset(cg: *CodeGen, elem_ty: Type, ptr: WValue, len: WValue, value: WValue) InnerError!void {
4787 const pt = cg.pt;
4788 const abi_size = @as(u32, @intCast(elem_ty.abiSize(pt.zcu)));
4747 const zcu = cg.pt.zcu;
4748 const abi_size = @as(u32, @intCast(elem_ty.abiSize(zcu)));
47894749
47904750 // When bulk_memory is enabled, we lower it to wasm's memset instruction.
47914751 // If not, we lower it ourselves.
......@@ -4869,8 +4829,7 @@ fn memset(cg: *CodeGen, elem_ty: Type, ptr: WValue, len: WValue, value: WValue)
48694829}
48704830
48714831fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4872 const pt = cg.pt;
4873 const zcu = pt.zcu;
4832 const zcu = cg.pt.zcu;
48744833 const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
48754834
48764835 const array_ty = cg.typeOf(bin_op.lhs);
......@@ -4931,8 +4890,7 @@ fn airArrayElemVal(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
49314890}
49324891
49334892fn airIntFromFloat(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4934 const pt = cg.pt;
4935 const zcu = pt.zcu;
4893 const zcu = cg.pt.zcu;
49364894 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
49374895
49384896 const operand = try cg.resolveInst(ty_op.operand);
......@@ -4983,8 +4941,7 @@ fn airIntFromFloat(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
49834941}
49844942
49854943fn airFloatFromInt(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
4986 const pt = cg.pt;
4987 const zcu = pt.zcu;
4944 const zcu = cg.pt.zcu;
49884945 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
49894946
49904947 const operand = try cg.resolveInst(ty_op.operand);
......@@ -5036,8 +4993,7 @@ fn airFloatFromInt(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
50364993}
50374994
50384995fn airSplat(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5039 const pt = cg.pt;
5040 const zcu = pt.zcu;
4996 const zcu = cg.pt.zcu;
50414997 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
50424998 const operand = try cg.resolveInst(ty_op.operand);
50434999 const ty = cg.typeOfIndex(inst);
......@@ -5405,8 +5361,7 @@ fn airWasmMemoryGrow(cg: *CodeGen, inst: Air.Inst.Index) !void {
54055361}
54065362
54075363fn cmpOptionals(cg: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.math.CompareOperator) InnerError!WValue {
5408 const pt = cg.pt;
5409 const zcu = pt.zcu;
5364 const zcu = cg.pt.zcu;
54105365 assert(operand_ty.hasRuntimeBitsIgnoreComptime(zcu));
54115366 assert(op == .eq or op == .neq);
54125367 const payload_ty = operand_ty.optionalChild(zcu);
......@@ -5442,8 +5397,7 @@ fn cmpOptionals(cg: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op: st
54425397/// NOTE: Leaves the result of the comparison on top of the stack.
54435398/// TODO: Lower this to compiler_rt call when bitsize > 128
54445399fn cmpBigInt(cg: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.math.CompareOperator) InnerError!WValue {
5445 const pt = cg.pt;
5446 const zcu = pt.zcu;
5400 const zcu = cg.pt.zcu;
54475401 assert(operand_ty.abiSize(zcu) >= 16);
54485402 assert(!(lhs != .stack and rhs == .stack));
54495403 if (operand_ty.bitSize(zcu) > 128) {
......@@ -5637,8 +5591,7 @@ fn fptrunc(cg: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerError!
56375591}
56385592
56395593fn airErrUnionPayloadPtrSet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5640 const pt = cg.pt;
5641 const zcu = pt.zcu;
5594 const zcu = cg.pt.zcu;
56425595 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
56435596
56445597 const err_set_ty = cg.typeOf(ty_op.operand).childType(zcu);
......@@ -5664,8 +5617,7 @@ fn airErrUnionPayloadPtrSet(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void
56645617}
56655618
56665619fn airFieldParentPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5667 const pt = cg.pt;
5668 const zcu = pt.zcu;
5620 const zcu = cg.pt.zcu;
56695621 const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
56705622 const extra = cg.air.extraData(Air.FieldParentPtr, ty_pl.payload).data;
56715623
......@@ -5686,8 +5638,7 @@ fn airFieldParentPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
56865638}
56875639
56885640fn sliceOrArrayPtr(cg: *CodeGen, ptr: WValue, ptr_ty: Type) InnerError!WValue {
5689 const pt = cg.pt;
5690 const zcu = pt.zcu;
5641 const zcu = cg.pt.zcu;
56915642 if (ptr_ty.isSlice(zcu)) {
56925643 return cg.slicePtr(ptr);
56935644 } else {
......@@ -5696,8 +5647,7 @@ fn sliceOrArrayPtr(cg: *CodeGen, ptr: WValue, ptr_ty: Type) InnerError!WValue {
56965647}
56975648
56985649fn airMemcpy(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5699 const pt = cg.pt;
5700 const zcu = pt.zcu;
5650 const zcu = cg.pt.zcu;
57015651 const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
57025652 const dst = try cg.resolveInst(bin_op.lhs);
57035653 const dst_ty = cg.typeOf(bin_op.lhs);
......@@ -5788,8 +5738,7 @@ fn airPopcount(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
57885738}
57895739
57905740fn airBitReverse(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
5791 const pt = cg.pt;
5792 const zcu = pt.zcu;
5741 const zcu = cg.pt.zcu;
57935742 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
57945743
57955744 const operand = try cg.resolveInst(ty_op.operand);
......@@ -6176,8 +6125,7 @@ fn airMaxMin(
61766125 op: enum { fmax, fmin },
61776126 cmp_op: std.math.CompareOperator,
61786127) InnerError!void {
6179 const pt = cg.pt;
6180 const zcu = pt.zcu;
6128 const zcu = cg.pt.zcu;
61816129 const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
61826130
61836131 const ty = cg.typeOfIndex(inst);
......@@ -6218,8 +6166,7 @@ fn airMaxMin(
62186166}
62196167
62206168fn airMulAdd(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6221 const pt = cg.pt;
6222 const zcu = pt.zcu;
6169 const zcu = cg.pt.zcu;
62236170 const pl_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
62246171 const bin_op = cg.air.extraData(Air.Bin, pl_op.payload).data;
62256172
......@@ -6253,8 +6200,7 @@ fn airMulAdd(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
62536200}
62546201
62556202fn airClz(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6256 const pt = cg.pt;
6257 const zcu = pt.zcu;
6203 const zcu = cg.pt.zcu;
62586204 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
62596205
62606206 const ty = cg.typeOf(ty_op.operand);
......@@ -6304,8 +6250,7 @@ fn airClz(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
63046250}
63056251
63066252fn airCtz(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6307 const pt = cg.pt;
6308 const zcu = pt.zcu;
6253 const zcu = cg.pt.zcu;
63096254 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
63106255
63116256 const ty = cg.typeOf(ty_op.operand);
......@@ -6406,8 +6351,7 @@ fn airTry(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
64066351}
64076352
64086353fn airTryPtr(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6409 const pt = cg.pt;
6410 const zcu = pt.zcu;
6354 const zcu = cg.pt.zcu;
64116355 const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
64126356 const extra = cg.air.extraData(Air.TryPtr, ty_pl.payload);
64136357 const err_union_ptr = try cg.resolveInst(extra.data.ptr);
......@@ -6425,8 +6369,7 @@ fn lowerTry(
64256369 err_union_ty: Type,
64266370 operand_is_ptr: bool,
64276371) InnerError!WValue {
6428 const pt = cg.pt;
6429 const zcu = pt.zcu;
6372 const zcu = cg.pt.zcu;
64306373 if (operand_is_ptr) {
64316374 return cg.fail("TODO: lowerTry for pointers", .{});
64326375 }
......@@ -6475,8 +6418,7 @@ fn lowerTry(
64756418}
64766419
64776420fn airByteSwap(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
6478 const pt = cg.pt;
6479 const zcu = pt.zcu;
6421 const zcu = cg.pt.zcu;
64806422 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
64816423
64826424 const ty = cg.typeOfIndex(inst);
......@@ -6558,8 +6500,7 @@ fn airDivTrunc(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
65586500fn airDivFloor(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
65596501 const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
65606502
6561 const pt = cg.pt;
6562 const zcu = pt.zcu;
6503 const zcu = cg.pt.zcu;
65636504 const ty = cg.typeOfIndex(inst);
65646505 const lhs = try cg.resolveInst(bin_op.lhs);
65656506 const rhs = try cg.resolveInst(bin_op.rhs);
......@@ -6819,8 +6760,7 @@ fn airSatBinOp(cg: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void {
68196760 assert(op == .add or op == .sub);
68206761 const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
68216762
6822 const pt = cg.pt;
6823 const zcu = pt.zcu;
6763 const zcu = cg.pt.zcu;
68246764 const ty = cg.typeOfIndex(inst);
68256765 const lhs = try cg.resolveInst(bin_op.lhs);
68266766 const rhs = try cg.resolveInst(bin_op.rhs);
......@@ -7041,8 +6981,7 @@ fn callIntrinsic(
70416981 args: []const WValue,
70426982) InnerError!WValue {
70436983 assert(param_types.len == args.len);
7044 const pt = cg.pt;
7045 const zcu = pt.zcu;
6984 const zcu = cg.pt.zcu;
70466985
70476986 // Always pass over C-ABI
70486987
......@@ -7090,8 +7029,7 @@ fn airTagName(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
70907029}
70917030
70927031fn airErrorSetHasValue(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7093 const pt = cg.pt;
7094 const zcu = pt.zcu;
7032 const zcu = cg.pt.zcu;
70957033 const ip = &zcu.intern_pool;
70967034 const ty_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
70977035
......@@ -7177,8 +7115,7 @@ inline fn useAtomicFeature(cg: *const CodeGen) bool {
71777115}
71787116
71797117fn airCmpxchg(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7180 const pt = cg.pt;
7181 const zcu = pt.zcu;
7118 const zcu = cg.pt.zcu;
71827119 const ty_pl = cg.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
71837120 const extra = cg.air.extraData(Air.Cmpxchg, ty_pl.payload).data;
71847121
......@@ -7251,13 +7188,13 @@ fn airCmpxchg(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
72517188}
72527189
72537190fn airAtomicLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7254 const pt = cg.pt;
7191 const zcu = cg.pt.zcu;
72557192 const atomic_load = cg.air.instructions.items(.data)[@intFromEnum(inst)].atomic_load;
72567193 const ptr = try cg.resolveInst(atomic_load.ptr);
72577194 const ty = cg.typeOfIndex(inst);
72587195
72597196 if (cg.useAtomicFeature()) {
7260 const tag: std.wasm.AtomicsOpcode = switch (ty.abiSize(pt.zcu)) {
7197 const tag: std.wasm.AtomicsOpcode = switch (ty.abiSize(zcu)) {
72617198 1 => .i32_atomic_load8_u,
72627199 2 => .i32_atomic_load16_u,
72637200 4 => .i32_atomic_load,
......@@ -7267,7 +7204,7 @@ fn airAtomicLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
72677204 try cg.emitWValue(ptr);
72687205 try cg.addAtomicMemArg(tag, .{
72697206 .offset = ptr.offset(),
7270 .alignment = @intCast(ty.abiAlignment(pt.zcu).toByteUnits().?),
7207 .alignment = @intCast(ty.abiAlignment(zcu).toByteUnits().?),
72717208 });
72727209 } else {
72737210 _ = try cg.load(ptr, ty, 0);
......@@ -7277,8 +7214,7 @@ fn airAtomicLoad(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
72777214}
72787215
72797216fn airAtomicRmw(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7280 const pt = cg.pt;
7281 const zcu = pt.zcu;
7217 const zcu = cg.pt.zcu;
72827218 const pl_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
72837219 const extra = cg.air.extraData(Air.AtomicRmw, pl_op.payload).data;
72847220
......@@ -7452,8 +7388,7 @@ fn airAtomicRmw(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
74527388}
74537389
74547390fn airAtomicStore(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
7455 const pt = cg.pt;
7456 const zcu = pt.zcu;
7391 const zcu = cg.pt.zcu;
74577392 const bin_op = cg.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
74587393
74597394 const ptr = try cg.resolveInst(bin_op.lhs);
......@@ -7491,14 +7426,12 @@ fn airFrameAddress(cg: *CodeGen, inst: Air.Inst.Index) InnerError!void {
74917426}
74927427
74937428fn typeOf(cg: *CodeGen, inst: Air.Inst.Ref) Type {
7494 const pt = cg.pt;
7495 const zcu = pt.zcu;
7429 const zcu = cg.pt.zcu;
74967430 return cg.air.typeOf(inst, &zcu.intern_pool);
74977431}
74987432
74997433fn typeOfIndex(cg: *CodeGen, inst: Air.Inst.Index) Type {
7500 const pt = cg.pt;
7501 const zcu = pt.zcu;
7434 const zcu = cg.pt.zcu;
75027435 return cg.air.typeOfIndex(inst, &zcu.intern_pool);
75037436}
75047437