authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-01-04 14:54:50-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-01-04 14:54:50-05:00
log5087ec6f41ba928e14596e00822dc117aeb90a12
tree95d629f527158751b0828b274f96b5e32c924d12
parent5c228765f1094d30e64d13c0077c67b2867ecd6a
parent89b1fdc4437531776b46ed7133c44b7250c122f8
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10508 from Luukdegram/wasm-behavior-tests

Stage2: wasm - Pass more behavior tests

5 files changed, 400 insertions(+), 137 deletions(-)

lib/std/wasm.zig+23-1
......@@ -212,6 +212,28 @@ test "Wasm - opcodes" {
212212 try testing.expectEqual(@as(u16, 0xC4), i64_extend32_s);
213213}
214214
215/// Opcodes that require a prefix `0xFC`
216pub const PrefixedOpcode = enum(u8) {
217 i32_trunc_sat_f32_s = 0x00,
218 i32_trunc_sat_f32_u = 0x01,
219 i32_trunc_sat_f64_s = 0x02,
220 i32_trunc_sat_f64_u = 0x03,
221 i64_trunc_sat_f32_s = 0x04,
222 i64_trunc_sat_f32_u = 0x05,
223 i64_trunc_sat_f64_s = 0x06,
224 i64_trunc_sat_f64_u = 0x07,
225 memory_init = 0x08,
226 data_drop = 0x09,
227 memory_copy = 0x0A,
228 memory_fill = 0x0B,
229 table_init = 0x0C,
230 elem_drop = 0x0D,
231 table_copy = 0x0E,
232 table_grow = 0x0F,
233 table_size = 0x10,
234 table_fill = 0x11,
235};
236
215237/// Enum representing all Wasm value types as per spec:
216238/// https://webassembly.github.io/spec/core/binary/types.html
217239pub const Valtype = enum(u8) {
......@@ -266,7 +288,7 @@ pub const InitExpression = union(enum) {
266288 global_get: u32,
267289};
268290
269///
291/// Represents a function entry, holding the index to its type
270292pub const Func = struct {
271293 type_index: u32,
272294};
src/arch/wasm/CodeGen.zig+342-128
......@@ -623,6 +623,10 @@ fn addTag(self: *Self, tag: Mir.Inst.Tag) error{OutOfMemory}!void {
623623 try self.addInst(.{ .tag = tag, .data = .{ .tag = {} } });
624624}
625625
626fn addExtended(self: *Self, opcode: wasm.PrefixedOpcode) error{OutOfMemory}!void {
627 try self.addInst(.{ .tag = .extended, .secondary = @enumToInt(opcode), .data = .{ .tag = {} } });
628}
629
626630fn addLabel(self: *Self, tag: Mir.Inst.Tag, label: u32) error{OutOfMemory}!void {
627631 try self.addInst(.{ .tag = tag, .data = .{ .label = label } });
628632}
......@@ -746,6 +750,13 @@ fn genFunctype(self: *Self, fn_ty: Type) !wasm.Type {
746750 defer params.deinit();
747751 var returns = std.ArrayList(wasm.Valtype).init(self.gpa);
748752 defer returns.deinit();
753 const return_type = fn_ty.fnReturnType();
754
755 const want_sret = isByRef(return_type);
756
757 if (want_sret) {
758 try params.append(try self.typeToValtype(Type.usize));
759 }
749760
750761 // param types
751762 if (fn_ty.fnParamLen() != 0) {
......@@ -759,11 +770,8 @@ fn genFunctype(self: *Self, fn_ty: Type) !wasm.Type {
759770 }
760771
761772 // return type
762 const return_type = fn_ty.fnReturnType();
763 switch (return_type.zigTypeTag()) {
764 .Void, .NoReturn => {},
765 .Struct => return self.fail("TODO: Implement struct as return type for wasm", .{}),
766 else => try returns.append(try self.typeToValtype(return_type)),
773 if (!want_sret and return_type.hasCodeGenBits()) {
774 try returns.append(try self.typeToValtype(return_type));
767775 }
768776
769777 return wasm.Type{
......@@ -785,6 +793,15 @@ pub fn genFunc(self: *Self) InnerError!Result {
785793
786794 // Generate MIR for function body
787795 try self.genBody(self.air.getMainBody());
796 // In case we have a return value, but the last instruction is a noreturn (such as a while loop)
797 // we emit an unreachable instruction to tell the stack validator that part will never be reached.
798 if (func_type.returns.len != 0 and self.air.instructions.len > 0) {
799 const inst = @intCast(u32, self.air.instructions.len - 1);
800 if (self.air.typeOfIndex(inst).isNoReturn()) {
801 try self.addTag(.@"unreachable");
802 }
803 }
804
788805 // End of function body
789806 try self.addTag(.end);
790807
......@@ -1074,6 +1091,15 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) InnerError!CallWValu
10741091 .return_value = .none,
10751092 };
10761093 errdefer self.gpa.free(result.args);
1094 const ret_ty = fn_ty.fnReturnType();
1095 // Check if we store the result as a pointer to the stack rather than
1096 // by value
1097 if (isByRef(ret_ty)) {
1098 // the sret arg will be passed as first argument, therefore we
1099 // set the `return_value` before allocating locals for regular args.
1100 result.return_value = .{ .local = self.local_index };
1101 self.local_index += 1;
1102 }
10771103 switch (cc) {
10781104 .Naked => return result,
10791105 .Unspecified, .C => {
......@@ -1086,27 +1112,6 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) InnerError!CallWValu
10861112 result.args[ty_index] = .{ .local = self.local_index };
10871113 self.local_index += 1;
10881114 }
1089
1090 const ret_ty = fn_ty.fnReturnType();
1091 if (isByRef(ret_ty)) {
1092 result.return_value = try self.allocLocal(Type.initTag(.i32));
1093 }
1094
1095 // Check if we store the result as a pointer to the stack rather than
1096 // by value
1097 if (result.return_value != .none) {
1098 if (self.initial_stack_value == .none) try self.initializeStack();
1099 const offset = std.math.cast(u32, ret_ty.abiSize(self.target)) catch {
1100 return self.fail("Return type '{}' too big for stack frame", .{ret_ty});
1101 };
1102
1103 try self.moveStack(offset, result.return_value.local);
1104
1105 // We want to make sure the return value's stack value doesn't get overwritten,
1106 // so set initial stack value to current's position instead.
1107 try self.addLabel(.global_get, 0);
1108 try self.addLabel(.local_set, self.initial_stack_value.local);
1109 }
11101115 },
11111116 else => return self.fail("TODO implement function parameters for cc '{}' on wasm", .{cc}),
11121117 }
......@@ -1165,10 +1170,16 @@ fn allocStack(self: *Self, ty: Type) !WValue {
11651170 assert(ty.hasCodeGenBits());
11661171
11671172 // calculate needed stack space
1168 const abi_size = std.math.cast(u32, ty.abiSize(self.target)) catch {
1173 var abi_size = std.math.cast(u32, ty.abiSize(self.target)) catch {
11691174 return self.fail("Given type '{}' too big to fit into stack frame", .{ty});
11701175 };
11711176
1177 // We store slices as a struct with a pointer field and a length field
1178 // both being 'usize' size.
1179 if (ty.isSlice()) {
1180 abi_size = self.ptrSize() * 2;
1181 }
1182
11721183 // allocate a local using wasm's pointer size
11731184 const local = try self.allocLocal(Type.@"usize");
11741185 try self.moveStack(abi_size, local.local);
......@@ -1256,6 +1267,28 @@ fn isByRef(ty: Type) bool {
12561267 }
12571268}
12581269
1270/// Creates a new local for a pointer that points to memory with given offset.
1271/// This can be used to get a pointer to a struct field, error payload, etc.
1272fn buildPointerOffset(self: *Self, ptr_value: WValue, offset: u64) InnerError!WValue {
1273 // do not perform arithmetic when offset is 0.
1274 if (offset == 0) return ptr_value;
1275 const result_ptr = try self.allocLocal(Type.usize);
1276 try self.emitWValue(ptr_value);
1277 switch (self.target.cpu.arch.ptrBitWidth()) {
1278 32 => {
1279 try self.addImm32(@bitCast(i32, @intCast(u32, offset)));
1280 try self.addTag(.i32_add);
1281 },
1282 64 => {
1283 try self.addImm64(offset);
1284 try self.addTag(.i64_add);
1285 },
1286 else => unreachable,
1287 }
1288 try self.addLabel(.local_set, result_ptr.local);
1289 return result_ptr;
1290}
1291
12591292fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
12601293 const air_tags = self.air.instructions.items(.tag);
12611294 return switch (air_tags[inst]) {
......@@ -1296,16 +1329,17 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
12961329 .is_err => self.airIsErr(inst, .i32_ne),
12971330 .is_non_err => self.airIsErr(inst, .i32_eq),
12981331
1299 .is_null => self.airIsNull(inst, .i32_ne),
1300 .is_non_null => self.airIsNull(inst, .i32_eq),
1301 .is_null_ptr => self.airIsNull(inst, .i32_ne),
1302 .is_non_null_ptr => self.airIsNull(inst, .i32_eq),
1332 .is_null => self.airIsNull(inst, .i32_eq, .value),
1333 .is_non_null => self.airIsNull(inst, .i32_ne, .value),
1334 .is_null_ptr => self.airIsNull(inst, .i32_eq, .ptr),
1335 .is_non_null_ptr => self.airIsNull(inst, .i32_ne, .ptr),
13031336
13041337 .load => self.airLoad(inst),
13051338 .loop => self.airLoop(inst),
1339 .memset => self.airMemset(inst),
13061340 .not => self.airNot(inst),
13071341 .optional_payload => self.airOptionalPayload(inst),
1308 .optional_payload_ptr => self.airOptionalPayload(inst),
1342 .optional_payload_ptr => self.airOptionalPayloadPtr(inst),
13091343 .optional_payload_ptr_set => self.airOptionalPayloadPtrSet(inst),
13101344 .ptr_add => self.airPtrBinOp(inst, .add),
13111345 .ptr_sub => self.airPtrBinOp(inst, .sub),
......@@ -1315,17 +1349,21 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
13151349 .ret => self.airRet(inst),
13161350 .ret_ptr => self.airRetPtr(inst),
13171351 .ret_load => self.airRetLoad(inst),
1352
1353 .slice => self.airSlice(inst),
13181354 .slice_len => self.airSliceLen(inst),
13191355 .slice_elem_val => self.airSliceElemVal(inst),
13201356 .slice_elem_ptr => self.airSliceElemPtr(inst),
13211357 .slice_ptr => self.airSlicePtr(inst),
13221358 .store => self.airStore(inst),
1359
13231360 .struct_field_ptr => self.airStructFieldPtr(inst),
13241361 .struct_field_ptr_index_0 => self.airStructFieldPtrIndex(inst, 0),
13251362 .struct_field_ptr_index_1 => self.airStructFieldPtrIndex(inst, 1),
13261363 .struct_field_ptr_index_2 => self.airStructFieldPtrIndex(inst, 2),
13271364 .struct_field_ptr_index_3 => self.airStructFieldPtrIndex(inst, 3),
13281365 .struct_field_val => self.airStructFieldVal(inst),
1366
13291367 .switch_br => self.airSwitchBr(inst),
13301368 .trunc => self.airTrunc(inst),
13311369 .unreach => self.airUnreachable(inst),
......@@ -1353,7 +1391,6 @@ fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
13531391 // to the stack instead
13541392 if (self.return_value != .none) {
13551393 try self.store(self.return_value, operand, self.decl.ty.fnReturnType(), 0);
1356 try self.emitWValue(self.return_value);
13571394 } else {
13581395 try self.emitWValue(operand);
13591396 }
......@@ -1372,6 +1409,9 @@ fn airRetPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
13721409
13731410 if (child_type.abiSize(self.target) == 0) return WValue{ .none = {} };
13741411
1412 if (isByRef(child_type)) {
1413 return self.return_value;
1414 }
13751415 return self.allocStack(child_type);
13761416}
13771417
......@@ -1381,9 +1421,7 @@ fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
13811421 const ret_ty = self.air.typeOf(un_op).childType();
13821422 if (!ret_ty.hasCodeGenBits()) return WValue.none;
13831423
1384 if (isByRef(ret_ty)) {
1385 try self.emitWValue(operand);
1386 } else {
1424 if (!isByRef(ret_ty)) {
13871425 const result = try self.load(operand, ret_ty, 0);
13881426 try self.emitWValue(result);
13891427 }
......@@ -1404,6 +1442,8 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
14041442 .Pointer => ty.childType(),
14051443 else => unreachable,
14061444 };
1445 const ret_ty = fn_ty.fnReturnType();
1446 const first_param_sret = isByRef(ret_ty);
14071447
14081448 const target: ?*Decl = blk: {
14091449 const func_val = self.air.value(pl_op.operand) orelse break :blk null;
......@@ -1416,6 +1456,12 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
14161456 return self.fail("Expected a function, but instead found type '{s}'", .{func_val.tag()});
14171457 };
14181458
1459 const sret = if (first_param_sret) blk: {
1460 const sret_local = try self.allocStack(ret_ty);
1461 try self.emitWValue(sret_local);
1462 break :blk sret_local;
1463 } else WValue{ .none = {} };
1464
14191465 for (args) |arg| {
14201466 const arg_ref = @intToEnum(Air.Inst.Ref, arg);
14211467 const arg_val = self.resolveInst(arg_ref);
......@@ -1454,42 +1500,35 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
14541500 try self.addLabel(.call_indirect, fn_type_index);
14551501 }
14561502
1457 const ret_ty = fn_ty.fnReturnType();
1458 if (!ret_ty.hasCodeGenBits()) return WValue.none;
1459
1460 // TODO: Implement this for all aggregate types
1461 if (ret_ty.isSlice()) {
1462 // first load the values onto the regular stack, before we move the stack pointer
1463 // to prevent overwriting the return value.
1464 const tmp = try self.allocLocal(ret_ty);
1465 try self.addLabel(.local_set, tmp.local);
1466 const field_ty = Type.@"usize";
1467 const offset = @intCast(u32, field_ty.abiSize(self.target));
1468 const ptr_local = try self.load(tmp, field_ty, 0);
1469 const len_local = try self.load(tmp, field_ty, offset);
1470
1471 // As our values are now safe, we reserve space on the virtual stack and
1472 // store the values there.
1473 const result = try self.allocStack(ret_ty);
1474 try self.store(result, ptr_local, field_ty, 0);
1475 try self.store(result, len_local, field_ty, offset);
1476 return result;
1503 if (self.liveness.isUnused(inst) or !ret_ty.hasCodeGenBits()) {
1504 return WValue.none;
1505 } else if (ret_ty.isNoReturn()) {
1506 try self.addTag(.@"unreachable");
1507 return WValue.none;
1508 } else if (first_param_sret) {
1509 return sret;
1510 } else {
1511 const result_local = try self.allocLocal(ret_ty);
1512 try self.addLabel(.local_set, result_local.local);
1513 return result_local;
14771514 }
1478
1479 const result_local = try self.allocLocal(ret_ty);
1480 try self.addLabel(.local_set, result_local.local);
1481 return result_local;
14821515}
14831516
14841517fn airAlloc(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
1485 const child_type = self.air.typeOfIndex(inst).childType();
1518 const pointee_type = self.air.typeOfIndex(inst).childType();
14861519
14871520 // Initialize the stack
14881521 if (self.initial_stack_value == .none) {
14891522 try self.initializeStack();
14901523 }
1491 if (child_type.abiSize(self.target) == 0) return WValue{ .none = {} };
1492 return self.allocStack(child_type);
1524
1525 if (!pointee_type.hasCodeGenBits()) {
1526 // when the pointee is zero-sized, we still want to create a pointer.
1527 // but instead use a default pointer type as storage.
1528 const zero_ptr = try self.allocStack(Type.usize);
1529 return zero_ptr;
1530 }
1531 return self.allocStack(pointee_type);
14931532}
14941533
14951534fn airStore(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
......@@ -1516,6 +1555,8 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro
15161555 const tag_ty = if (ty.zigTypeTag() == .ErrorUnion) ty.errorUnionSet() else Type.initTag(.u8);
15171556 const payload_offset = if (ty.zigTypeTag() == .ErrorUnion)
15181557 @intCast(u32, tag_ty.abiSize(self.target))
1558 else if (ty.isPtrLikeOptional())
1559 @as(u32, 0)
15191560 else
15201561 @intCast(u32, ty.abiSize(self.target) - payload_ty.abiSize(self.target));
15211562
......@@ -1528,6 +1569,10 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro
15281569 try self.addLabel(.local_set, mem_local.local);
15291570 try self.store(lhs, mem_local, ty, 0);
15301571 return;
1572 } else if (ty.isPtrLikeOptional()) {
1573 // set the address of rhs to lhs
1574 try self.store(lhs, rhs, Type.usize, 0);
1575 return;
15311576 }
15321577 // constant will contain both tag and payload,
15331578 // so save those in 2 temporary locals before storing them
......@@ -1546,11 +1591,23 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro
15461591 return;
15471592 },
15481593 .local => {
1594 // When the optional is pointer-like, we simply store the pointer
1595 // instead.
1596 if (ty.isPtrLikeOptional()) {
1597 try self.store(lhs, rhs, Type.usize, 0);
1598 return;
1599 }
15491600 // Load values from `rhs` stack position and store in `lhs` instead
15501601 const tag_local = try self.load(rhs, tag_ty, 0);
15511602 if (payload_ty.hasCodeGenBits()) {
1552 const payload_local = try self.load(rhs, payload_ty, payload_offset);
1553 try self.store(lhs, payload_local, payload_ty, payload_offset);
1603 if (isByRef(payload_ty)) {
1604 const payload_ptr = try self.buildPointerOffset(rhs, payload_offset);
1605 const lhs_payload_ptr = try self.buildPointerOffset(lhs, payload_offset);
1606 try self.store(lhs_payload_ptr, payload_ptr, payload_ty, 0);
1607 } else {
1608 const payload_local = try self.load(rhs, payload_ty, payload_offset);
1609 try self.store(lhs, payload_local, payload_ty, payload_offset);
1610 }
15541611 }
15551612 return try self.store(lhs, tag_local, tag_ty, 0);
15561613 },
......@@ -1593,12 +1650,9 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro
15931650 const len_offset = self.ptrSize();
15941651 if (val.castTag(.decl_ref)) |decl| {
15951652 // for decl references we also need to retrieve the length and the original decl's pointer
1596 try self.addMemArg(.i32_load, .{ .offset = 0, .alignment = Type.@"usize".abiAlignment(self.target) });
1653 try self.addMemArg(.i32_load, .{ .offset = 0, .alignment = self.ptrSize() });
15971654 try self.addLabel(.memory_address, decl.data.link.wasm.sym_index);
1598 try self.addMemArg(
1599 .i32_load,
1600 .{ .offset = len_offset, .alignment = Type.@"usize".abiAlignment(self.target) },
1601 );
1655 try self.addMemArg(.i32_load, .{ .offset = len_offset, .alignment = self.ptrSize() });
16021656 }
16031657 try self.addLabel(.local_set, len_local.local);
16041658 try self.addLabel(.local_set, ptr_local.local);
......@@ -1630,7 +1684,6 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro
16301684 .ErrorSet,
16311685 .Enum,
16321686 .Bool,
1633 .ErrorUnion,
16341687 => @intCast(u8, ty.abiSize(self.target)),
16351688 else => @as(u8, 4),
16361689 };
......@@ -1670,7 +1723,9 @@ fn load(self: *Self, operand: WValue, ty: Type, offset: u32) InnerError!WValue {
16701723 // load local's value from memory by its stack position
16711724 try self.emitWValue(operand);
16721725 // Build the opcode with the right bitsize
1673 const signedness: std.builtin.Signedness = if (ty.isUnsignedInt() or ty.zigTypeTag() == .ErrorSet)
1726 const signedness: std.builtin.Signedness = if (ty.isUnsignedInt() or
1727 ty.zigTypeTag() == .ErrorSet or
1728 ty.zigTypeTag() == .Bool)
16741729 .unsigned
16751730 else
16761731 .signed;
......@@ -1684,6 +1739,10 @@ fn load(self: *Self, operand: WValue, ty: Type, offset: u32) InnerError!WValue {
16841739 .Bool,
16851740 .ErrorUnion,
16861741 => @intCast(u8, ty.abiSize(self.target)),
1742 .Optional => blk: {
1743 if (ty.isPtrLikeOptional()) break :blk @intCast(u8, self.ptrSize());
1744 break :blk @intCast(u8, ty.abiSize(self.target));
1745 },
16871746 else => @as(u8, 4),
16881747 };
16891748
......@@ -1828,7 +1887,7 @@ fn emitConstant(self: *Self, val: Value, ty: Type) InnerError!void {
18281887 }
18291888 } else if (val.castTag(.int_u64)) |int_ptr| {
18301889 try self.addImm32(@bitCast(i32, @intCast(u32, int_ptr.data)));
1831 } else if (val.tag() == .zero) {
1890 } else if (val.tag() == .zero or val.tag() == .null_value) {
18321891 try self.addImm32(0);
18331892 } else if (val.tag() == .one) {
18341893 try self.addImm32(1);
......@@ -1886,18 +1945,19 @@ fn emitConstant(self: *Self, val: Value, ty: Type) InnerError!void {
18861945 var buf: Type.Payload.ElemType = undefined;
18871946 const payload_type = ty.optionalChild(&buf);
18881947 if (ty.isPtrLikeOptional()) {
1889 return self.fail("Wasm TODO: emitConstant for optional pointer", .{});
1948 try self.emitConstant(val, payload_type);
1949 return;
18901950 }
18911951
18921952 // When constant has value 'null', set is_null local to '1'
18931953 // and payload to '0'
18941954 if (val.castTag(.opt_payload)) |payload| {
1895 try self.addImm32(0);
1955 try self.addImm32(1);
18961956 if (payload_type.hasCodeGenBits())
18971957 try self.emitConstant(payload.data, payload_type);
18981958 } else {
18991959 // set null-tag
1900 try self.addImm32(1);
1960 try self.addImm32(0);
19011961 // null-tag is set, so write a '0' const
19021962 try self.addImm32(0);
19031963 }
......@@ -1908,11 +1968,16 @@ fn emitConstant(self: *Self, val: Value, ty: Type) InnerError!void {
19081968 const result = try self.allocStack(ty);
19091969
19101970 const fields = ty.structFields();
1971 var offset: u32 = 0;
19111972 for (fields.values()) |field, index| {
1973 if (isByRef(field.ty)) {
1974 return self.fail("TODO: emitConstant for struct field type {}\n", .{field.ty});
1975 }
19121976 const tmp = try self.allocLocal(field.ty);
19131977 try self.emitConstant(struct_data.data[index], field.ty);
19141978 try self.addLabel(.local_set, tmp.local);
1915 try self.store(result, tmp, field.ty, field.offset);
1979 try self.store(result, tmp, field.ty, offset);
1980 offset += @intCast(u32, field.ty.abiSize(self.target));
19161981 }
19171982 try self.addLabel(.local_get, result.local);
19181983 },
......@@ -1936,10 +2001,15 @@ fn emitUndefined(self: *Self, ty: Type) InnerError!void {
19362001 // validator will not accept it due to out-of-bounds memory access);
19372002 .Array => try self.addImm32(@bitCast(i32, @as(u32, 0xaa))),
19382003 .Struct => {
1939 // TODO: Write 0xaa to each field
2004 // TODO: Write 0xaa struct's memory
19402005 const result = try self.allocStack(ty);
19412006 try self.addLabel(.local_get, result.local);
19422007 },
2008 .Pointer => switch (self.ptrSize()) {
2009 4 => try self.addImm32(@bitCast(i32, @as(u32, 0xaaaaaaaa))),
2010 8 => try self.addImm64(0xaaaaaaaaaaaaaaaa),
2011 else => unreachable,
2012 },
19432013 else => return self.fail("Wasm TODO: emitUndefined for type: {}\n", .{ty}),
19442014 }
19452015}
......@@ -2065,23 +2135,34 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
20652135}
20662136
20672137fn airCmp(self: *Self, inst: Air.Inst.Index, op: std.math.CompareOperator) InnerError!WValue {
2068 const data: Air.Inst.Data = self.air.instructions.items(.data)[inst];
2069 const lhs = self.resolveInst(data.bin_op.lhs);
2070 const rhs = self.resolveInst(data.bin_op.rhs);
2071 const lhs_ty = self.air.typeOf(data.bin_op.lhs);
2138 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
2139 const lhs = self.resolveInst(bin_op.lhs);
2140 const rhs = self.resolveInst(bin_op.rhs);
2141 const operand_ty = self.air.typeOf(bin_op.lhs);
20722142
20732143 try self.emitWValue(lhs);
20742144 try self.emitWValue(rhs);
20752145
2146 if (operand_ty.zigTypeTag() == .Optional and !operand_ty.isPtrLikeOptional()) {
2147 var buf: Type.Payload.ElemType = undefined;
2148 const payload_ty = operand_ty.optionalChild(&buf);
2149 if (payload_ty.hasCodeGenBits()) {
2150 // When we hit this case, we must check the value of optionals
2151 // that are not pointers. This means first checking against non-null for
2152 // both lhs and rhs, as well as checking the payload are matching of lhs and rhs
2153 return self.fail("TODO: Implement airCmp for comparing optionals", .{});
2154 }
2155 }
2156
20762157 const signedness: std.builtin.Signedness = blk: {
20772158 // by default we tell the operand type is unsigned (i.e. bools and enum values)
2078 if (lhs_ty.zigTypeTag() != .Int) break :blk .unsigned;
2159 if (operand_ty.zigTypeTag() != .Int) break :blk .unsigned;
20792160
20802161 // incase of an actual integer, we emit the correct signedness
2081 break :blk lhs_ty.intInfo(self.target).signedness;
2162 break :blk operand_ty.intInfo(self.target).signedness;
20822163 };
20832164 const opcode: wasm.Opcode = buildOpcode(.{
2084 .valtype1 = try self.typeToValtype(lhs_ty),
2165 .valtype1 = try self.typeToValtype(operand_ty),
20852166 .op = switch (op) {
20862167 .lt => .lt,
20872168 .lte => .le,
......@@ -2132,7 +2213,7 @@ fn airNot(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
21322213 try self.addTag(.i32_eq);
21332214
21342215 // save the result in the local
2135 const not_tmp = try self.allocLocal(self.air.getRefType(ty_op.ty));
2216 const not_tmp = try self.allocLocal(Type.initTag(.i32));
21362217 try self.addLabel(.local_set, not_tmp.local);
21372218 return not_tmp;
21382219}
......@@ -2173,7 +2254,7 @@ fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
21732254 struct_ty.structFieldType(extra.data.field_index),
21742255 });
21752256 };
2176 return structFieldPtr(struct_ptr, offset);
2257 return self.structFieldPtr(struct_ptr, offset);
21772258}
21782259
21792260fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u32) InnerError!WValue {
......@@ -2186,10 +2267,10 @@ fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u32) InnerEr
21862267 field_ty,
21872268 });
21882269 };
2189 return structFieldPtr(struct_ptr, offset);
2270 return self.structFieldPtr(struct_ptr, offset);
21902271}
21912272
2192fn structFieldPtr(struct_ptr: WValue, offset: u32) InnerError!WValue {
2273fn structFieldPtr(self: *Self, struct_ptr: WValue, offset: u32) InnerError!WValue {
21932274 var final_offset = offset;
21942275 const local = switch (struct_ptr) {
21952276 .local => |local| local,
......@@ -2199,7 +2280,7 @@ fn structFieldPtr(struct_ptr: WValue, offset: u32) InnerError!WValue {
21992280 },
22002281 else => unreachable,
22012282 };
2202 return WValue{ .local_with_offset = .{ .local = local, .offset = final_offset } };
2283 return self.buildPointerOffset(.{ .local = local }, final_offset);
22032284}
22042285
22052286fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
......@@ -2434,24 +2515,13 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
24342515 const offset = err_ty.errorUnionSet().abiSize(self.target);
24352516
24362517 const err_union = try self.allocStack(err_ty);
2437 const to_store = switch (op_ty.zigTypeTag()) {
2438 // for those types we must load the pointer and then store
2439 // its value
2440 .Pointer, .Optional => blk: {
2441 if (!op_ty.isPtrLikeOptional()) {
2442 return self.fail("TODO: airWrapErrUnionPayload for optional type {}", .{op_ty});
2443 }
2444 break :blk try self.load(operand, op_ty, 0);
2445 },
2446 .Int => operand,
2447 else => return self.fail("TODO: airWrapErrUnionPayload for type {}", .{op_ty}),
2448 };
2449
2450 try self.store(err_union, to_store, op_ty, @intCast(u32, offset));
2518 const payload_ptr = try self.buildPointerOffset(err_union, offset);
2519 try self.store(payload_ptr, operand, op_ty, 0);
24512520
24522521 // ensure we also write '0' to the error part, so any present stack value gets overwritten by it.
2453 const tmp_local = try self.allocLocal(err_ty.errorUnionSet()); // locals are '0' by default.
2454 try self.store(err_union, tmp_local, err_ty.errorUnionSet(), 0);
2522 try self.addLabel(.local_get, err_union.local);
2523 try self.addImm32(0);
2524 try self.addMemArg(.i32_store16, .{ .offset = 0, .alignment = 2 });
24552525
24562526 return err_union;
24572527}
......@@ -2499,64 +2569,140 @@ fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
24992569 return result;
25002570}
25012571
2502fn airIsNull(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!WValue {
2572fn airIsNull(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: enum { value, ptr }) InnerError!WValue {
25032573 const un_op = self.air.instructions.items(.data)[inst].un_op;
25042574 const operand = self.resolveInst(un_op);
25052575
25062576 const op_ty = self.air.typeOf(un_op);
2577 const optional_ty = if (op_kind == .ptr) op_ty.childType() else op_ty;
25072578 try self.emitWValue(operand);
2508 if (!op_ty.isPtrLikeOptional()) {
2509 try self.addMemArg(.i32_load8_u, .{ .offset = 0, .alignment = 1 });
2579 if (!optional_ty.isPtrLikeOptional()) {
2580 var buf: Type.Payload.ElemType = undefined;
2581 const payload_ty = optional_ty.optionalChild(&buf);
2582 // When payload is zero-bits, we can treat operand as a value, rather than a
2583 // stack value
2584 if (payload_ty.hasCodeGenBits()) {
2585 try self.addMemArg(.i32_load8_u, .{ .offset = 0, .alignment = 1 });
2586 }
25102587 }
25112588
2512 // Compare the error value with '0'
2589 // Compare the null value with '0'
25132590 try self.addImm32(0);
25142591 try self.addTag(Mir.Inst.Tag.fromOpcode(opcode));
25152592
2516 const is_null_tmp = try self.allocLocal(Type.initTag(.u8));
2593 const is_null_tmp = try self.allocLocal(Type.initTag(.i32));
25172594 try self.addLabel(.local_set, is_null_tmp.local);
25182595 return is_null_tmp;
25192596}
25202597
25212598fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2599 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
25222600 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
25232601 const operand = self.resolveInst(ty_op.operand);
25242602 const opt_ty = self.air.typeOf(ty_op.operand);
2603 const payload_ty = self.air.typeOfIndex(inst);
2604 if (!payload_ty.hasCodeGenBits()) return WValue{ .none = {} };
2605 if (opt_ty.isPtrLikeOptional()) return operand;
2606
2607 const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target);
25252608
2526 // For pointers we simply return its stack address, rather than
2527 // loading its value
2528 if (opt_ty.zigTypeTag() == .Pointer) {
2529 return WValue{ .local_with_offset = .{ .local = operand.local, .offset = 1 } };
2609 if (isByRef(payload_ty)) {
2610 return self.buildPointerOffset(operand, offset);
25302611 }
25312612
2532 if (opt_ty.isPtrLikeOptional()) return operand;
2613 return self.load(operand, payload_ty, @intCast(u32, offset));
2614}
2615
2616fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2617 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
2618
2619 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
2620 const operand = self.resolveInst(ty_op.operand);
2621 const opt_ty = self.air.typeOf(ty_op.operand).childType();
25332622
25342623 var buf: Type.Payload.ElemType = undefined;
2535 const child_ty = opt_ty.optionalChild(&buf);
2536 const offset = opt_ty.abiSize(self.target) - child_ty.abiSize(self.target);
2624 const payload_ty = opt_ty.optionalChild(&buf);
2625 if (!payload_ty.hasCodeGenBits() or opt_ty.isPtrLikeOptional()) {
2626 return operand;
2627 }
25372628
2538 return self.load(operand, child_ty, @intCast(u32, offset));
2629 const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target);
2630 return self.buildPointerOffset(operand, offset);
25392631}
25402632
25412633fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
25422634 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
25432635 const operand = self.resolveInst(ty_op.operand);
2544 _ = operand;
2545 return self.fail("TODO - wasm codegen for optional_payload_ptr_set", .{});
2636 const opt_ty = self.air.typeOf(ty_op.operand).childType();
2637 var buf: Type.Payload.ElemType = undefined;
2638 const payload_ty = opt_ty.optionalChild(&buf);
2639 if (!payload_ty.hasCodeGenBits()) {
2640 return self.fail("TODO: Implement OptionalPayloadPtrSet for optional with zero-sized type {}", .{payload_ty});
2641 }
2642
2643 if (opt_ty.isPtrLikeOptional()) {
2644 return operand;
2645 }
2646
2647 const offset = std.math.cast(u32, opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target)) catch {
2648 return self.fail("Optional type {} too big to fit into stack frame", .{opt_ty});
2649 };
2650
2651 try self.emitWValue(operand);
2652 try self.addImm32(1);
2653 try self.addMemArg(.i32_store8, .{ .offset = 0, .alignment = 1 });
2654
2655 return self.buildPointerOffset(operand, offset);
25462656}
25472657
25482658fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2659 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
2660
25492661 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
2662 const payload_ty = self.air.typeOf(ty_op.operand);
2663 if (!payload_ty.hasCodeGenBits()) {
2664 const non_null_bit = try self.allocStack(Type.initTag(.u1));
2665 try self.addLabel(.local_get, non_null_bit.local);
2666 try self.addImm32(1);
2667 try self.addMemArg(.i32_store8, .{ .offset = 0, .alignment = 1 });
2668 return non_null_bit;
2669 }
2670
25502671 const operand = self.resolveInst(ty_op.operand);
2672 const op_ty = self.air.typeOfIndex(inst);
2673 if (op_ty.isPtrLikeOptional()) {
2674 return operand;
2675 }
2676 const offset = std.math.cast(u32, op_ty.abiSize(self.target) - payload_ty.abiSize(self.target)) catch {
2677 return self.fail("Optional type {} too big to fit into stack frame", .{op_ty});
2678 };
25512679
2552 const op_ty = self.air.typeOf(ty_op.operand);
2553 const optional_ty = self.air.getRefType(ty_op.ty);
2554 const offset = optional_ty.abiSize(self.target) - op_ty.abiSize(self.target);
2680 // Create optional type, set the non-null bit, and store the operand inside the optional type
2681 const result = try self.allocStack(op_ty);
2682 try self.addLabel(.local_get, result.local);
2683 try self.addImm32(1);
2684 try self.addMemArg(.i32_store8, .{ .offset = 0, .alignment = 1 });
2685
2686 const payload_ptr = try self.buildPointerOffset(result, offset);
2687 try self.store(payload_ptr, operand, payload_ty, 0);
25552688
2556 return WValue{ .local_with_offset = .{
2557 .local = operand.local,
2558 .offset = @intCast(u32, offset),
2559 } };
2689 return result;
2690}
2691
2692fn airSlice(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2693 if (self.liveness.isUnused(inst)) return WValue{ .none = {} };
2694
2695 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
2696 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
2697 const lhs = self.resolveInst(bin_op.lhs);
2698 const rhs = self.resolveInst(bin_op.rhs);
2699 const slice_ty = self.air.typeOfIndex(inst);
2700
2701 const slice = try self.allocStack(slice_ty);
2702 try self.store(slice, lhs, Type.usize, 0);
2703 try self.store(slice, rhs, Type.usize, self.ptrSize());
2704
2705 return slice;
25602706}
25612707
25622708fn airSliceLen(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
......@@ -2809,3 +2955,71 @@ fn airPtrBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue {
28092955 try self.addLabel(.local_set, result.local);
28102956 return result;
28112957}
2958
2959fn airMemset(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2960 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
2961 const bin_op = self.air.extraData(Air.Bin, pl_op.payload).data;
2962
2963 const ptr = self.resolveInst(pl_op.operand);
2964 const value = self.resolveInst(bin_op.lhs);
2965 const len = self.resolveInst(bin_op.rhs);
2966 try self.memSet(ptr, len, value);
2967
2968 return WValue.none;
2969}
2970
2971/// Sets a region of memory at `ptr` to the value of `value`
2972/// When the user has enabled the bulk_memory feature, we lower
2973/// this to wasm's memset instruction. When the feature is not present,
2974/// we implement it manually.
2975fn memSet(self: *Self, ptr: WValue, len: WValue, value: WValue) InnerError!void {
2976 // When bulk_memory is enabled, we lower it to wasm's memset instruction.
2977 // If not, we lower it ourselves
2978 if (std.Target.wasm.featureSetHas(self.target.cpu.features, .bulk_memory)) {
2979 try self.emitWValue(ptr);
2980 try self.emitWValue(value);
2981 try self.emitWValue(len);
2982 try self.addExtended(.memory_fill);
2983 return;
2984 }
2985
2986 // TODO: We should probably lower this to a call to compiler_rt
2987 // But for now, we implement it manually
2988 const offset = try self.allocLocal(Type.usize); // local for counter
2989 // outer block to jump to when loop is done
2990 try self.startBlock(.block, wasm.block_empty);
2991 try self.startBlock(.loop, wasm.block_empty);
2992 try self.emitWValue(offset);
2993 try self.emitWValue(len);
2994 switch (self.ptrSize()) {
2995 4 => try self.addTag(.i32_eq),
2996 8 => try self.addTag(.i64_eq),
2997 else => unreachable,
2998 }
2999 try self.addLabel(.br_if, 1); // jump out of loop into outer block (finished)
3000 try self.emitWValue(ptr);
3001 try self.emitWValue(offset);
3002 switch (self.ptrSize()) {
3003 4 => try self.addTag(.i32_add),
3004 8 => try self.addTag(.i64_add),
3005 else => unreachable,
3006 }
3007 try self.emitWValue(value);
3008 const mem_store_op: Mir.Inst.Tag = switch (self.ptrSize()) {
3009 4 => .i32_store8,
3010 8 => .i64_store8,
3011 else => unreachable,
3012 };
3013 try self.addMemArg(mem_store_op, .{ .offset = 0, .alignment = 1 });
3014 try self.emitWValue(offset);
3015 try self.addImm32(1);
3016 switch (self.ptrSize()) {
3017 4 => try self.addTag(.i32_add),
3018 8 => try self.addTag(.i64_add),
3019 else => unreachable,
3020 }
3021 try self.addLabel(.local_set, offset.local);
3022 try self.addLabel(.br, 0); // jump to start of loop
3023 try self.endBlock();
3024 try self.endBlock();
3025}
src/arch/wasm/Emit.zig+19
......@@ -161,6 +161,8 @@ pub fn emitMir(emit: *Emit) InnerError!void {
161161 .i64_extend8_s => try emit.emitTag(tag),
162162 .i64_extend16_s => try emit.emitTag(tag),
163163 .i64_extend32_s => try emit.emitTag(tag),
164
165 .extended => try emit.emitExtended(inst),
164166 }
165167 }
166168}
......@@ -321,3 +323,20 @@ fn emitMemAddress(emit: *Emit, inst: Mir.Inst.Index) !void {
321323 .relocation_type = .R_WASM_MEMORY_ADDR_LEB,
322324 });
323325}
326
327fn emitExtended(emit: *Emit, inst: Mir.Inst.Index) !void {
328 const opcode = emit.mir.instructions.items(.secondary)[inst];
329 switch (@intToEnum(std.wasm.PrefixedOpcode, opcode)) {
330 .memory_fill => try emit.emitMemFill(),
331 else => |tag| return emit.fail("TODO: Implement extension instruction: {s}\n", .{@tagName(tag)}),
332 }
333}
334
335fn emitMemFill(emit: *Emit) !void {
336 try emit.code.append(0xFC);
337 try emit.code.append(0x0B);
338 // When multi-memory proposal reaches phase 4, we
339 // can emit a different memory index here.
340 // For now we will always emit index 0.
341 try leb128.writeULEB128(emit.code.writer(), @as(u32, 0));
342}
src/arch/wasm/Mir.zig+8
......@@ -19,6 +19,9 @@ extra: []const u32,
1919pub const Inst = struct {
2020 /// The opcode that represents this instruction
2121 tag: Tag,
22 /// This opcode will be set when `tag` represents an extended
23 /// instruction with prefix 0xFC, or a simd instruction with prefix 0xFD.
24 secondary: u8 = 0,
2225 /// Data is determined by the set `tag`.
2326 /// For example, `data` will be an i32 for when `tag` is 'i32_const'.
2427 data: Data,
......@@ -373,6 +376,11 @@ pub const Inst = struct {
373376 i64_extend16_s = 0xC3,
374377 /// Uses `tag`
375378 i64_extend32_s = 0xC4,
379 /// The instruction consists of an extension opcode
380 /// set in `secondary`
381 ///
382 /// The `data` field depends on the extension instruction
383 extended = 0xFC,
376384 /// Contains a symbol to a function pointer
377385 /// uses `label`
378386 ///
test/behavior.zig+8-8
......@@ -39,16 +39,23 @@ test {
3939 _ = @import("behavior/defer.zig");
4040 _ = @import("behavior/enum.zig");
4141 _ = @import("behavior/error.zig");
42 _ = @import("behavior/generics.zig");
4243 _ = @import("behavior/if.zig");
4344 _ = @import("behavior/import.zig");
4445 _ = @import("behavior/incomplete_struct_param_tld.zig");
4546 _ = @import("behavior/inttoptr.zig");
47 _ = @import("behavior/member_func.zig");
48 _ = @import("behavior/null.zig");
4649 _ = @import("behavior/pointers.zig");
4750 _ = @import("behavior/ptrcast.zig");
4851 _ = @import("behavior/ref_var_in_if_after_if_2nd_switch_prong.zig");
52 _ = @import("behavior/struct.zig");
53 _ = @import("behavior/this.zig");
4954 _ = @import("behavior/truncate.zig");
50 _ = @import("behavior/usingnamespace.zig");
5155 _ = @import("behavior/underscore.zig");
56 _ = @import("behavior/usingnamespace.zig");
57 _ = @import("behavior/void.zig");
58 _ = @import("behavior/while.zig");
5259
5360 if (!builtin.zig_is_stage2 or builtin.stage2_arch != .wasm32) {
5461 // Tests that pass for stage1, llvm backend, C backend
......@@ -56,16 +63,9 @@ test {
5663 _ = @import("behavior/array.zig");
5764 _ = @import("behavior/cast.zig");
5865 _ = @import("behavior/for.zig");
59 _ = @import("behavior/generics.zig");
6066 _ = @import("behavior/int128.zig");
61 _ = @import("behavior/member_func.zig");
62 _ = @import("behavior/null.zig");
6367 _ = @import("behavior/optional.zig");
64 _ = @import("behavior/struct.zig");
65 _ = @import("behavior/this.zig");
6668 _ = @import("behavior/translate_c_macros.zig");
67 _ = @import("behavior/while.zig");
68 _ = @import("behavior/void.zig");
6969
7070 if (builtin.object_format != .c) {
7171 // Tests that pass for stage1 and the llvm backend.