authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-01-10 20:28:55+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-01-10 21:03:37+01:00
logbf46aee878aa3ac6824047038ed887744da3e259
treec02f06b4462f84bfc318881ecb3caa7c3bf02a05
parent6a9ddf244a1a6641991ed45b7ff7cacf7c789bfb
signaturelock-open Commit is signed but in an unrecognized format.

wasm: Basic 128bit integer support

This implements storing, loading and comparing 128bit integers. TODO: Make all >64 bit integers make a call to compiler-rt for binary operations.

2 files changed, 78 insertions(+), 31 deletions(-)

src/arch/wasm/CodeGen.zig+71-25
......@@ -690,7 +690,7 @@ fn typeToValtype(self: *Self, ty: Type) InnerError!wasm.Valtype {
690690 const info = ty.intInfo(self.target);
691691 if (info.bits <= 32) break :blk wasm.Valtype.i32;
692692 if (info.bits > 32 and info.bits <= 64) break :blk wasm.Valtype.i64;
693 return self.fail("TODO: Support integer bitsize: '{d}'", .{info.bits});
693 break :blk wasm.Valtype.i32; // represented as pointer to stack
694694 },
695695 .Enum => switch (ty.tag()) {
696696 .enum_simple => wasm.Valtype.i32,
......@@ -753,7 +753,7 @@ fn genFunctype(self: *Self, fn_ty: Type) !wasm.Type {
753753 defer returns.deinit();
754754 const return_type = fn_ty.fnReturnType();
755755
756 const want_sret = isByRef(return_type);
756 const want_sret = self.isByRef(return_type);
757757
758758 if (want_sret) {
759759 try params.append(try self.typeToValtype(Type.usize));
......@@ -1084,7 +1084,7 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) InnerError!CallWValu
10841084 const ret_ty = fn_ty.fnReturnType();
10851085 // Check if we store the result as a pointer to the stack rather than
10861086 // by value
1087 if (isByRef(ret_ty)) {
1087 if (self.isByRef(ret_ty)) {
10881088 // the sret arg will be passed as first argument, therefore we
10891089 // set the `return_value` before allocating locals for regular args.
10901090 result.return_value = .{ .local = self.local_index };
......@@ -1209,7 +1209,7 @@ fn ptrSize(self: *const Self) u16 {
12091209
12101210/// For a given `Type`, will return true when the type will be passed
12111211/// by reference, rather than by value.
1212fn isByRef(ty: Type) bool {
1212fn isByRef(self: Self, ty: Type) bool {
12131213 switch (ty.zigTypeTag()) {
12141214 .Type,
12151215 .ComptimeInt,
......@@ -1224,7 +1224,6 @@ fn isByRef(ty: Type) bool {
12241224 .NoReturn,
12251225 .Void,
12261226 .Bool,
1227 .Int,
12281227 .Float,
12291228 .ErrorSet,
12301229 .Fn,
......@@ -1238,6 +1237,7 @@ fn isByRef(ty: Type) bool {
12381237 .Frame,
12391238 .Union,
12401239 => return ty.hasCodeGenBits(),
1240 .Int => return if (ty.intInfo(self.target).bits > 64) true else false,
12411241 .ErrorUnion => {
12421242 const has_tag = ty.errorUnionSet().hasCodeGenBits();
12431243 const has_pl = ty.errorUnionPayload().hasCodeGenBits();
......@@ -1412,7 +1412,7 @@ fn airRetPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
14121412 const child_type = self.air.typeOfIndex(inst).childType();
14131413 if (child_type.abiSize(self.target) == 0) return WValue{ .none = {} };
14141414
1415 if (isByRef(child_type)) {
1415 if (self.isByRef(child_type)) {
14161416 return self.return_value;
14171417 }
14181418
......@@ -1429,7 +1429,7 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
14291429 const ret_ty = self.air.typeOf(un_op).childType();
14301430 if (!ret_ty.hasCodeGenBits()) return WValue.none;
14311431
1432 if (!isByRef(ret_ty)) {
1432 if (!self.isByRef(ret_ty)) {
14331433 const result = try self.load(operand, ret_ty, 0);
14341434 try self.emitWValue(result);
14351435 }
......@@ -1451,7 +1451,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
14511451 else => unreachable,
14521452 };
14531453 const ret_ty = fn_ty.fnReturnType();
1454 const first_param_sret = isByRef(ret_ty);
1454 const first_param_sret = self.isByRef(ret_ty);
14551455
14561456 const target: ?*Decl = blk: {
14571457 const func_val = self.air.value(pl_op.operand) orelse break :blk null;
......@@ -1479,7 +1479,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
14791479
14801480 // If we need to pass by reference, but the argument is a constant,
14811481 // we must first lower it before passing it.
1482 if (isByRef(arg_ty) and arg_val == .constant) {
1482 if (self.isByRef(arg_ty) and arg_val == .constant) {
14831483 const arg_local = try self.allocStack(arg_ty);
14841484 try self.store(arg_local, arg_val, arg_ty, 0);
14851485 try self.emitWValue(arg_local);
......@@ -1591,7 +1591,12 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro
15911591 if (payload_ty.hasCodeGenBits()) {
15921592 const payload_local = try self.allocLocal(payload_ty);
15931593 try self.addLabel(.local_set, payload_local.local);
1594 try self.store(lhs, payload_local, payload_ty, payload_offset);
1594 if (self.isByRef(payload_ty)) {
1595 const ptr = try self.buildPointerOffset(lhs, payload_offset, .new);
1596 try self.store(ptr, payload_local, payload_ty, 0);
1597 } else {
1598 try self.store(lhs, payload_local, payload_ty, payload_offset);
1599 }
15951600 }
15961601 try self.addLabel(.local_set, tag_local.local);
15971602
......@@ -1608,7 +1613,7 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro
16081613 // Load values from `rhs` stack position and store in `lhs` instead
16091614 const tag_local = try self.load(rhs, tag_ty, 0);
16101615 if (payload_ty.hasCodeGenBits()) {
1611 if (isByRef(payload_ty)) {
1616 if (self.isByRef(payload_ty)) {
16121617 const payload_ptr = try self.buildPointerOffset(rhs, payload_offset, .new);
16131618 const lhs_payload_ptr = try self.buildPointerOffset(lhs, payload_offset, .new);
16141619 try self.store(lhs_payload_ptr, payload_ptr, payload_ty, 0);
......@@ -1734,7 +1739,7 @@ fn airLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
17341739
17351740 if (!ty.hasCodeGenBits()) return WValue{ .none = {} };
17361741
1737 if (isByRef(ty)) {
1742 if (self.isByRef(ty)) {
17381743 const new_local = try self.allocStack(ty);
17391744 try self.store(new_local, operand, ty, 0);
17401745 return new_local;
......@@ -1803,6 +1808,11 @@ fn airBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue {
18031808 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
18041809 const lhs = self.resolveInst(bin_op.lhs);
18051810 const rhs = self.resolveInst(bin_op.rhs);
1811 const operand_ty = self.air.typeOfIndex(inst);
1812
1813 if (self.isByRef(operand_ty)) {
1814 return self.fail("TODO: Implement binary operation for type: {}", .{operand_ty});
1815 }
18061816
18071817 try self.emitWValue(lhs);
18081818 try self.emitWValue(rhs);
......@@ -1893,9 +1903,7 @@ fn emitConstant(self: *Self, val: Value, ty: Type) InnerError!void {
18931903 const result = try self.allocStack(ty);
18941904 var space: Value.BigIntSpace = undefined;
18951905 const bigint = val.toBigInt(&space);
1896 if (bigint.limbs.len == 1) {
1897 // value is '0'. As wasm's values are zeroed by default,
1898 // just return and pop its stack pointer value.
1906 if (bigint.limbs.len == 1 and bigint.limbs[0] == 0) {
18991907 try self.addLabel(.local_get, result.local);
19001908 return;
19011909 }
......@@ -2277,6 +2285,8 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: std.math.CompareOperator) Inner
22772285 // both lhs and rhs, as well as checking the payload are matching of lhs and rhs
22782286 return self.cmpOptionals(lhs, rhs, operand_ty, op);
22792287 }
2288 } else if (self.isByRef(operand_ty)) {
2289 return self.cmpBigInt(lhs, rhs, operand_ty, op);
22802290 }
22812291
22822292 try self.emitWValue(lhs);
......@@ -2425,7 +2435,7 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
24252435 return self.fail("Field type '{}' too big to fit into stack frame", .{field_ty});
24262436 };
24272437
2428 if (isByRef(field_ty)) {
2438 if (self.isByRef(field_ty)) {
24292439 return WValue{ .local_with_offset = .{ .local = operand.local, .offset = offset } };
24302440 }
24312441
......@@ -2608,13 +2618,16 @@ fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!W
26082618}
26092619
26102620fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2611 if (self.liveness.isUnused(inst)) return WValue.none;
2621 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
26122622 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
26132623 const operand = self.resolveInst(ty_op.operand);
26142624 const err_ty = self.air.typeOf(ty_op.operand);
26152625 const payload_ty = err_ty.errorUnionPayload();
2616 if (!payload_ty.hasCodeGenBits()) return WValue.none;
2626 if (!payload_ty.hasCodeGenBits()) return WValue{ .none = {} };
26172627 const offset = @intCast(u32, err_ty.errorUnionSet().abiSize(self.target));
2628 if (self.isByRef(payload_ty)) {
2629 return self.buildPointerOffset(operand, offset, .new);
2630 }
26182631 return try self.load(operand, payload_ty, offset);
26192632}
26202633
......@@ -2741,7 +2754,7 @@ fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
27412754
27422755 const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target);
27432756
2744 if (isByRef(payload_ty)) {
2757 if (self.isByRef(payload_ty)) {
27452758 return self.buildPointerOffset(operand, offset, .new);
27462759 }
27472760
......@@ -2872,7 +2885,7 @@ fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
28722885 const result = try self.allocLocal(elem_ty);
28732886 try self.addLabel(.local_set, result.local);
28742887
2875 if (isByRef(elem_ty)) {
2888 if (self.isByRef(elem_ty)) {
28762889 return result;
28772890 }
28782891 return try self.load(result, elem_ty, 0);
......@@ -3033,7 +3046,7 @@ fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
30333046
30343047 const result = try self.allocLocal(elem_ty);
30353048 try self.addLabel(.local_set, result.local);
3036 if (isByRef(elem_ty)) {
3049 if (self.isByRef(elem_ty)) {
30373050 return result;
30383051 }
30393052 return try self.load(result, elem_ty, 0);
......@@ -3183,7 +3196,7 @@ fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
31833196 const result = try self.allocLocal(elem_ty);
31843197 try self.addLabel(.local_set, result.local);
31853198
3186 if (isByRef(elem_ty)) {
3199 if (self.isByRef(elem_ty)) {
31873200 return result;
31883201 }
31893202 return try self.load(result, elem_ty, 0);
......@@ -3244,10 +3257,43 @@ fn cmpOptionals(self: *Self, lhs: WValue, rhs: WValue, operand_ty: Type, op: std
32443257 try self.addLabel(.local_set, result.local);
32453258 try self.endBlock();
32463259
3247 const is_equal = try self.allocLocal(Type.initTag(.i32));
32483260 try self.emitWValue(result);
32493261 try self.addImm32(0);
32503262 try self.addTag(if (op == .eq) .i32_ne else .i32_eq);
3251 try self.addLabel(.local_set, is_equal.local);
3252 return is_equal;
3263 try self.addLabel(.local_set, result.local);
3264 return result;
3265}
3266
3267/// Compares big integers by checking both its high bits and low bits.
3268/// TODO: Lower this to compiler_rt call
3269fn cmpBigInt(self: *Self, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.math.CompareOperator) InnerError!WValue {
3270 if (operand_ty.intInfo(self.target).bits > 128) {
3271 return self.fail("TODO: Support cmpBigInt for integer bitsize: '{d}'", .{operand_ty.intInfo(self.target).bits});
3272 }
3273
3274 const result = try self.allocLocal(Type.initTag(.i32));
3275 {
3276 try self.startBlock(.block, wasm.block_empty);
3277 const lhs_high_bit = try self.load(lhs, Type.initTag(.u64), 0);
3278 const lhs_low_bit = try self.load(lhs, Type.initTag(.u64), 8);
3279 const rhs_high_bit = try self.load(rhs, Type.initTag(.u64), 0);
3280 const rhs_low_bit = try self.load(rhs, Type.initTag(.u64), 8);
3281 try self.emitWValue(lhs_high_bit);
3282 try self.emitWValue(rhs_high_bit);
3283 try self.addTag(.i64_ne);
3284 try self.addLabel(.br_if, 0);
3285 try self.emitWValue(lhs_low_bit);
3286 try self.emitWValue(rhs_low_bit);
3287 try self.addTag(.i64_ne);
3288 try self.addLabel(.br_if, 0);
3289 try self.addImm32(1);
3290 try self.addLabel(.local_set, result.local);
3291 try self.endBlock();
3292 }
3293
3294 try self.emitWValue(result);
3295 try self.addImm32(0);
3296 try self.addTag(if (op == .eq) .i32_ne else .i32_eq);
3297 try self.addLabel(.local_set, result.local);
3298 return result;
32533299}
test/behavior.zig+7-6
......@@ -20,8 +20,8 @@ test {
2020
2121 if (builtin.zig_backend != .stage2_arm and builtin.zig_backend != .stage2_x86_64) {
2222 // Tests that pass for stage1, llvm backend, C backend, wasm backend.
23 _ = @import("behavior/align.zig");
2324 _ = @import("behavior/array.zig");
24 _ = @import("behavior/bugs/3586.zig");
2525 _ = @import("behavior/basic.zig");
2626 _ = @import("behavior/bitcast.zig");
2727 _ = @import("behavior/bugs/624.zig");
......@@ -31,12 +31,14 @@ test {
3131 _ = @import("behavior/bugs/2692.zig");
3232 _ = @import("behavior/bugs/2889.zig");
3333 _ = @import("behavior/bugs/3046.zig");
34 _ = @import("behavior/bugs/3586.zig");
3435 _ = @import("behavior/bugs/4560.zig");
3536 _ = @import("behavior/bugs/4769_a.zig");
3637 _ = @import("behavior/bugs/4769_b.zig");
3738 _ = @import("behavior/bugs/4954.zig");
3839 _ = @import("behavior/byval_arg_var.zig");
3940 _ = @import("behavior/call.zig");
41 _ = @import("behavior/cast.zig");
4042 _ = @import("behavior/defer.zig");
4143 _ = @import("behavior/enum.zig");
4244 _ = @import("behavior/error.zig");
......@@ -48,12 +50,15 @@ test {
4850 _ = @import("behavior/inttoptr.zig");
4951 _ = @import("behavior/member_func.zig");
5052 _ = @import("behavior/null.zig");
53 _ = @import("behavior/optional.zig");
5154 _ = @import("behavior/pointers.zig");
5255 _ = @import("behavior/ptrcast.zig");
5356 _ = @import("behavior/ref_var_in_if_after_if_2nd_switch_prong.zig");
57 _ = @import("behavior/src.zig");
5458 _ = @import("behavior/struct.zig");
5559 _ = @import("behavior/this.zig");
5660 _ = @import("behavior/truncate.zig");
61 _ = @import("behavior/try.zig");
5762 _ = @import("behavior/undefined.zig");
5863 _ = @import("behavior/underscore.zig");
5964 _ = @import("behavior/usingnamespace.zig");
......@@ -62,13 +67,9 @@ test {
6267
6368 if (builtin.zig_backend != .stage2_wasm) {
6469 // Tests that pass for stage1, llvm backend, C backend
65 _ = @import("behavior/align.zig");
66 _ = @import("behavior/cast.zig");
70 _ = @import("behavior/cast_int.zig");
6771 _ = @import("behavior/int128.zig");
68 _ = @import("behavior/optional.zig");
6972 _ = @import("behavior/translate_c_macros.zig");
70 _ = @import("behavior/try.zig");
71 _ = @import("behavior/src.zig");
7273
7374 if (builtin.zig_backend != .stage2_c) {
7475 // Tests that pass for stage1 and the llvm backend.