authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-05-14 19:23:31+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-05-18 07:43:33+02:00
log502f5d824626675aecf9daaaf6af7548ef3e7f09
tree9c51f320b38c551a6d72f13be5e326e1c3dea34c
parent03a3ea2c1577208311d85482faadac6361442971

wasm: Fix C-ABI for 128 bit integers

We now pass the correct wasm type when the return type is a 128-bit integer. When a function accepts a 128-bit integer, we now allocate space on the virtual stack and store both arguments within that space as currently all following instructions assume the 128 bit integer doesn't live in a local, but the stack.

1 files changed, 46 insertions(+), 60 deletions(-)

src/arch/wasm/CodeGen.zig+46-60
......@@ -794,7 +794,7 @@ fn genFunctype(gpa: Allocator, fn_info: Type.Payload.Function.Data, target: std.
794794 defer returns.deinit();
795795
796796 if (firstParamSRet(fn_info, target)) {
797 try params.append(typeToValtype(fn_info.return_type, target));
797 try params.append(.i32); // memory address is always a 32-bit handle
798798 } else if (fn_info.return_type.hasRuntimeBitsIgnoreComptime()) {
799799 if (fn_info.cc == .C) {
800800 const res_classes = abi.classifyType(fn_info.return_type, target);
......@@ -824,7 +824,10 @@ fn genFunctype(gpa: Allocator, fn_info: Type.Payload.Function.Data, target: std.
824824 }
825825 }
826826 },
827 else => try params.append(typeToValtype(param_type, target)),
827 else => if (isByRef(param_type, target))
828 try params.append(.i32)
829 else
830 try params.append(typeToValtype(param_type, target)),
828831 }
829832 }
830833 }
......@@ -844,7 +847,6 @@ pub fn generate(
844847 code: *std.ArrayList(u8),
845848 debug_output: codegen.DebugInfoOutput,
846849) codegen.GenerateSymbolError!codegen.FnResult {
847 _ = debug_output; // TODO
848850 _ = src_loc;
849851 var code_gen: Self = .{
850852 .gpa = bin_file.allocator,
......@@ -1088,9 +1090,9 @@ fn lowerArg(self: *Self, cc: std.builtin.CallingConvention, ty: Type, value: WVa
10881090 assert(ty.abiSize(self.target) == 16);
10891091 // in this case we have an integer or float that must be lowered as 2 i64's.
10901092 try self.emitWValue(value);
1091 try self.addMemArg(.i64_load, .{ .offset = value.offset(), .alignment = 16 });
1093 try self.addMemArg(.i64_load, .{ .offset = value.offset(), .alignment = 8 });
10921094 try self.emitWValue(value);
1093 try self.addMemArg(.i64_load, .{ .offset = value.offset() + 8, .alignment = 16 });
1095 try self.addMemArg(.i64_load, .{ .offset = value.offset() + 8, .alignment = 8 });
10941096 },
10951097 else => return self.lowerToStack(value),
10961098 }
......@@ -1221,14 +1223,8 @@ fn memcpy(self: *Self, dst: WValue, src: WValue, len: WValue) !void {
12211223 // When bulk_memory is enabled, we lower it to wasm's memcpy instruction.
12221224 // If not, we lower it ourselves manually
12231225 if (std.Target.wasm.featureSetHas(self.target.cpu.features, .bulk_memory)) {
1224 switch (dst) {
1225 .stack_offset => try self.emitWValue(try self.buildPointerOffset(dst, 0, .new)),
1226 else => try self.emitWValue(dst),
1227 }
1228 switch (src) {
1229 .stack_offset => try self.emitWValue(try self.buildPointerOffset(src, 0, .new)),
1230 else => try self.emitWValue(src),
1231 }
1226 try self.lowerToStack(dst);
1227 try self.lowerToStack(src);
12321228 try self.emitWValue(len);
12331229 try self.addExtended(.memory_copy);
12341230 return;
......@@ -1792,7 +1788,7 @@ fn airStore(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
17921788 const ty = self.air.typeOf(bin_op.lhs).childType();
17931789
17941790 try self.store(lhs, rhs, ty, 0);
1795 return .none;
1791 return WValue{ .none = {} };
17961792}
17971793
17981794fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerError!void {
......@@ -1848,11 +1844,8 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro
18481844 try self.emitWValue(lhs);
18491845 // In this case we're actually interested in storing the stack position
18501846 // into lhs, so we calculate that and emit that instead
1851 if (rhs == .stack_offset) {
1852 try self.emitWValue(try self.buildPointerOffset(rhs, 0, .new));
1853 } else {
1854 try self.emitWValue(rhs);
1855 }
1847 try self.lowerToStack(rhs);
1848
18561849 const valtype = typeToValtype(ty, self.target);
18571850 const abi_size = @intCast(u8, ty.abiSize(self.target));
18581851
......@@ -1912,14 +1905,29 @@ fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
19121905 const arg_index = self.arg_index;
19131906 const arg = self.args[arg_index];
19141907 const cc = self.decl.ty.fnInfo().cc;
1908 const arg_ty = self.air.typeOfIndex(inst);
19151909 if (cc == .C) {
1916 const ty = self.air.typeOfIndex(inst);
1917 const arg_classes = abi.classifyType(ty, self.target);
1910 const arg_classes = abi.classifyType(arg_ty, self.target);
19181911 for (arg_classes) |class| {
19191912 if (class != .none) {
19201913 self.arg_index += 1;
19211914 }
19221915 }
1916
1917 // When we have an argument that's passed using more than a single parameter,
1918 // we combine them into a single stack value
1919 if (arg_classes[0] == .direct and arg_classes[1] == .direct) {
1920 if (arg_ty.zigTypeTag() != .Int) {
1921 return self.fail(
1922 "TODO: Implement C-ABI argument for type '{}'",
1923 .{arg_ty.fmt(self.bin_file.base.options.module.?)},
1924 );
1925 }
1926 const result = try self.allocStack(arg_ty);
1927 try self.store(result, arg, Type.u64, 0);
1928 try self.store(result, self.args[arg_index + 1], Type.u64, 8);
1929 return result;
1930 }
19231931 } else {
19241932 self.arg_index += 1;
19251933 }
......@@ -1943,7 +1951,7 @@ fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
19431951 std.dwarf.OP.WASM_local,
19441952 });
19451953 leb.writeULEB128(dbg_info.writer(), arg.local) catch unreachable;
1946 try self.addDbgInfoTypeReloc(self.air.typeOfIndex(inst));
1954 try self.addDbgInfoTypeReloc(arg_ty);
19471955 dbg_info.appendSliceAssumeCapacity(name);
19481956 dbg_info.appendAssumeCapacity(0);
19491957 },
......@@ -2503,14 +2511,8 @@ fn cmp(self: *Self, lhs: WValue, rhs: WValue, ty: Type, op: std.math.CompareOper
25032511
25042512 // ensure that when we compare pointers, we emit
25052513 // the true pointer of a stack value, rather than the stack pointer.
2506 switch (lhs) {
2507 .stack_offset => try self.emitWValue(try self.buildPointerOffset(lhs, 0, .new)),
2508 else => try self.emitWValue(lhs),
2509 }
2510 switch (rhs) {
2511 .stack_offset => try self.emitWValue(try self.buildPointerOffset(rhs, 0, .new)),
2512 else => try self.emitWValue(rhs),
2513 }
2514 try self.lowerToStack(lhs);
2515 try self.lowerToStack(rhs);
25142516
25152517 const signedness: std.builtin.Signedness = blk: {
25162518 // by default we tell the operand type is unsigned (i.e. bools and enum values)
......@@ -2560,11 +2562,7 @@ fn airBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
25602562 // if operand has codegen bits we should break with a value
25612563 if (self.air.typeOf(br.operand).hasRuntimeBitsIgnoreComptime()) {
25622564 const operand = try self.resolveInst(br.operand);
2563 const op = switch (operand) {
2564 .stack_offset => try self.buildPointerOffset(operand, 0, .new),
2565 else => operand,
2566 };
2567 try self.emitWValue(op);
2565 try self.lowerToStack(operand);
25682566
25692567 if (block.value != .none) {
25702568 try self.addLabel(.local_set, block.value.local);
......@@ -3295,11 +3293,7 @@ fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
32953293 const ptr_local = try self.load(ptr, Type.usize, 0);
32963294 try self.addLabel(.local_get, ptr_local.local);
32973295 } else {
3298 const pointer = switch (ptr) {
3299 .stack_offset => try self.buildPointerOffset(ptr, 0, .new),
3300 else => ptr,
3301 };
3302 try self.emitWValue(pointer);
3296 try self.lowerToStack(ptr);
33033297 }
33043298
33053299 // calculate index into slice
......@@ -3332,11 +3326,7 @@ fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
33323326 const ptr_local = try self.load(ptr, Type.usize, 0);
33333327 try self.addLabel(.local_get, ptr_local.local);
33343328 } else {
3335 const pointer = switch (ptr) {
3336 .stack_offset => try self.buildPointerOffset(ptr, 0, .new),
3337 else => ptr,
3338 };
3339 try self.emitWValue(pointer);
3329 try self.lowerToStack(ptr);
33403330 }
33413331
33423332 // calculate index into ptr
......@@ -3365,11 +3355,7 @@ fn airPtrBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue {
33653355 const mul_opcode = buildOpcode(.{ .valtype1 = valtype, .op = .mul });
33663356 const bin_opcode = buildOpcode(.{ .valtype1 = valtype, .op = op });
33673357
3368 const pointer = switch (ptr) {
3369 .stack_offset => try self.buildPointerOffset(ptr, 0, .new),
3370 else => ptr,
3371 };
3372 try self.emitWValue(pointer);
3358 try self.lowerToStack(ptr);
33733359 try self.emitWValue(offset);
33743360 try self.addImm32(@bitCast(i32, @intCast(u32, pointee_ty.abiSize(self.target))));
33753361 try self.addTag(Mir.Inst.Tag.fromOpcode(mul_opcode));
......@@ -3400,10 +3386,7 @@ fn memset(self: *Self, ptr: WValue, len: WValue, value: WValue) InnerError!void
34003386 // When bulk_memory is enabled, we lower it to wasm's memset instruction.
34013387 // If not, we lower it ourselves
34023388 if (std.Target.wasm.featureSetHas(self.target.cpu.features, .bulk_memory)) {
3403 switch (ptr) {
3404 .stack_offset => try self.emitWValue(try self.buildPointerOffset(ptr, 0, .new)),
3405 else => try self.emitWValue(ptr),
3406 }
3389 try self.lowerToStack(ptr);
34073390 try self.emitWValue(value);
34083391 try self.emitWValue(len);
34093392 try self.addExtended(.memory_fill);
......@@ -3490,12 +3473,7 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
34903473 const elem_ty = array_ty.childType();
34913474 const elem_size = elem_ty.abiSize(self.target);
34923475
3493 const array_ptr = switch (array) {
3494 .stack_offset => try self.buildPointerOffset(array, 0, .new),
3495 else => array,
3496 };
3497
3498 try self.emitWValue(array_ptr);
3476 try self.lowerToStack(array);
34993477 try self.emitWValue(index);
35003478 try self.addImm32(@bitCast(i32, @intCast(u32, elem_size)));
35013479 try self.addTag(.i32_mul);
......@@ -3518,6 +3496,10 @@ fn airFloatToInt(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
35183496 const dest_ty = self.air.typeOfIndex(inst);
35193497 const op_ty = self.air.typeOf(ty_op.operand);
35203498
3499 if (op_ty.abiSize(self.target) > 8) {
3500 return self.fail("TODO: floatToInt for integers/floats with bitsize larger than 64 bits", .{});
3501 }
3502
35213503 try self.emitWValue(operand);
35223504 const op = buildOpcode(.{
35233505 .op = .trunc,
......@@ -3541,6 +3523,10 @@ fn airIntToFloat(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
35413523 const dest_ty = self.air.typeOfIndex(inst);
35423524 const op_ty = self.air.typeOf(ty_op.operand);
35433525
3526 if (op_ty.abiSize(self.target) > 8) {
3527 return self.fail("TODO: intToFloat for integers/floats with bitsize larger than 64 bits", .{});
3528 }
3529
35443530 try self.emitWValue(operand);
35453531 const op = buildOpcode(.{
35463532 .op = .convert,