authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2021-11-29 10:52:04+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-11-29 10:52:04+01:00
log7a7df392d146b8f77c74b0578fc48f6c927efc93
tree248d909fcbb7866124aafdf9e01b7d2cb6646da9
parent2ca5a859e9f5148a09928803cc2d109440a8067c
parentadf059f272dfd3c1652bce774c0b6c204d5d6b8b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10240 from Luukdegram/stage2-wasm-behaviour

Stage2: wasm - Implement 'zig test'

9 files changed, 623 insertions(+), 244 deletions(-)

lib/std/start.zig+18
...@@ -30,6 +30,8 @@ comptime {...@@ -30,6 +30,8 @@ comptime {
30 }30 }
31 } else if (builtin.os.tag == .windows) {31 } else if (builtin.os.tag == .windows) {
32 @export(wWinMainCRTStartup2, .{ .name = "wWinMainCRTStartup" });32 @export(wWinMainCRTStartup2, .{ .name = "wWinMainCRTStartup" });
33 } else if (builtin.os.tag == .wasi and @hasDecl(root, "main")) {
34 @export(wasmMain2, .{ .name = "_start" });
33 } else {35 } else {
34 if (!@hasDecl(root, "_start")) {36 if (!@hasDecl(root, "_start")) {
35 @export(_start2, .{ .name = "_start" });37 @export(_start2, .{ .name = "_start" });
...@@ -98,6 +100,22 @@ fn callMain2() noreturn {...@@ -98,6 +100,22 @@ fn callMain2() noreturn {
98 exit2(0);100 exit2(0);
99}101}
100102
103fn wasmMain2() u8 {
104 switch (@typeInfo(@typeInfo(@TypeOf(root.main)).Fn.return_type.?)) {
105 .Void => {
106 root.main();
107 return 0;
108 },
109 .Int => |info| {
110 if (info.bits != 8 or info.signedness == .signed) {
111 @compileError(bad_main_ret);
112 }
113 return root.main();
114 },
115 else => @compileError("Bad return type main"),
116 }
117}
118
101fn wWinMainCRTStartup2() callconv(.C) noreturn {119fn wWinMainCRTStartup2() callconv(.C) noreturn {
102 root.main();120 root.main();
103 exit2(0);121 exit2(0);
src/arch/wasm/CodeGen.zig+315-40
...@@ -692,6 +692,7 @@ fn typeToValtype(self: *Self, ty: Type) InnerError!wasm.Valtype {...@@ -692,6 +692,7 @@ fn typeToValtype(self: *Self, ty: Type) InnerError!wasm.Valtype {
692 .Struct,692 .Struct,
693 .ErrorUnion,693 .ErrorUnion,
694 .Optional,694 .Optional,
695 .Fn,
695 => wasm.Valtype.i32,696 => wasm.Valtype.i32,
696 else => self.fail("TODO - Wasm valtype for type '{}'", .{ty}),697 else => self.fail("TODO - Wasm valtype for type '{}'", .{ty}),
697 };698 };
...@@ -809,23 +810,52 @@ pub fn genFunc(self: *Self) InnerError!Result {...@@ -809,23 +810,52 @@ pub fn genFunc(self: *Self) InnerError!Result {
809}810}
810811
811/// Generates the wasm bytecode for the declaration belonging to `Context`812/// Generates the wasm bytecode for the declaration belonging to `Context`
812pub fn gen(self: *Self, ty: Type, val: Value) InnerError!Result {813pub fn genDecl(self: *Self, ty: Type, val: Value) InnerError!Result {
814 if (val.isUndef()) {
815 try self.code.appendNTimes(0xaa, @intCast(usize, ty.abiSize(self.target)));
816 return Result.appended;
817 }
813 switch (ty.zigTypeTag()) {818 switch (ty.zigTypeTag()) {
814 .Fn => {819 .Fn => {
815 if (val.tag() == .extern_fn) {820 const fn_decl = switch (val.tag()) {
816 var func_type = try self.genFunctype(self.decl.ty);821 .extern_fn => val.castTag(.extern_fn).?.data,
817 defer func_type.deinit(self.gpa);822 .function => val.castTag(.function).?.data.owner_decl,
818 self.decl.fn_link.wasm.type_index = try self.bin_file.putOrGetFuncType(func_type);823 else => unreachable,
819 return Result.appended; // don't need code body for extern functions824 };
825 return try self.lowerDeclRef(fn_decl);
826 },
827 .Optional => {
828 var opt_buf: Type.Payload.ElemType = undefined;
829 const payload_type = ty.optionalChild(&opt_buf);
830 if (ty.isPtrLikeOptional()) {
831 if (val.castTag(.opt_payload)) |payload| {
832 return try self.genDecl(payload_type, payload.data);
833 } else if (!val.isNull()) {
834 return try self.genDecl(payload_type, val);
835 } else {
836 try self.code.appendNTimes(0, @intCast(usize, ty.abiSize(self.target)));
837 return Result.appended;
838 }
820 }839 }
821 return self.fail("TODO implement wasm codegen for function pointers", .{});840 // `null-tag` byte
841 try self.code.appendNTimes(@boolToInt(!val.isNull()), 4);
842 const pl_result = try self.genDecl(
843 payload_type,
844 if (val.castTag(.opt_payload)) |pl| pl.data else Value.initTag(.undef),
845 );
846 switch (pl_result) {
847 .appended => {},
848 .externally_managed => |payload| try self.code.appendSlice(payload),
849 }
850 return Result.appended;
822 },851 },
823 .Array => {852 .Array => switch (val.tag()) {
824 if (val.castTag(.bytes)) |payload| {853 .bytes => {
854 const payload = val.castTag(.bytes).?;
825 if (ty.sentinel()) |sentinel| {855 if (ty.sentinel()) |sentinel| {
826 try self.code.appendSlice(payload.data);856 try self.code.appendSlice(payload.data);
827857
828 switch (try self.gen(ty.childType(), sentinel)) {858 switch (try self.genDecl(ty.childType(), sentinel)) {
829 .appended => return Result.appended,859 .appended => return Result.appended,
830 .externally_managed => |data| {860 .externally_managed => |data| {
831 try self.code.appendSlice(data);861 try self.code.appendSlice(data);
...@@ -834,16 +864,33 @@ pub fn gen(self: *Self, ty: Type, val: Value) InnerError!Result {...@@ -834,16 +864,33 @@ pub fn gen(self: *Self, ty: Type, val: Value) InnerError!Result {
834 }864 }
835 }865 }
836 return Result{ .externally_managed = payload.data };866 return Result{ .externally_managed = payload.data };
837 } else return self.fail("TODO implement gen for more kinds of arrays", .{});867 },
868 .array => {
869 const elem_vals = val.castTag(.array).?.data;
870 const elem_ty = ty.elemType();
871 for (elem_vals) |elem_val| {
872 switch (try self.genDecl(elem_ty, elem_val)) {
873 .appended => {},
874 .externally_managed => |data| {
875 try self.code.appendSlice(data);
876 },
877 }
878 }
879 return Result.appended;
880 },
881 else => return self.fail("TODO implement genDecl for array type value: {s}", .{@tagName(val.tag())}),
838 },882 },
839 .Int => {883 .Int => {
840 const info = ty.intInfo(self.target);884 const info = ty.intInfo(self.target);
841 if (info.bits == 8 and info.signedness == .unsigned) {885 const abi_size = @intCast(usize, ty.abiSize(self.target));
842 const int_byte = val.toUnsignedInt();886 // todo: Implement integer sizes larger than 64bits
843 try self.code.append(@intCast(u8, int_byte));887 if (info.bits > 64) return self.fail("TODO: Implement genDecl for integer bit size: {d}", .{info.bits});
844 return Result.appended;888 var buf: [8]u8 = undefined;
845 }889 if (info.signedness == .unsigned) {
846 return self.fail("TODO: Implement codegen for int type: '{}'", .{ty});890 std.mem.writeIntLittle(u64, &buf, val.toUnsignedInt());
891 } else std.mem.writeIntLittle(i64, &buf, val.toSignedInt());
892 try self.code.appendSlice(buf[0..abi_size]);
893 return Result.appended;
847 },894 },
848 .Enum => {895 .Enum => {
849 try self.emitConstant(val, ty);896 try self.emitConstant(val, ty);
...@@ -855,15 +902,83 @@ pub fn gen(self: *Self, ty: Type, val: Value) InnerError!Result {...@@ -855,15 +902,83 @@ pub fn gen(self: *Self, ty: Type, val: Value) InnerError!Result {
855 return Result.appended;902 return Result.appended;
856 },903 },
857 .Struct => {904 .Struct => {
858 // TODO write the fields for real905 const field_vals = val.castTag(.@"struct").?.data;
859 const abi_size = try std.math.cast(usize, ty.abiSize(self.target));906 for (field_vals) |field_val, index| {
860 try self.code.writer().writeByteNTimes(0xaa, abi_size);907 const field_ty = ty.structFieldType(index);
861 return Result{ .appended = {} };908 if (!field_ty.hasCodeGenBits()) continue;
909
910 switch (try self.genDecl(field_ty, field_val)) {
911 .appended => {},
912 .externally_managed => |payload| try self.code.appendSlice(payload),
913 }
914 }
915 return Result.appended;
916 },
917 .Union => {
918 // TODO: Implement Union declarations
919 const abi_size = @intCast(usize, ty.abiSize(self.target));
920 try self.code.appendNTimes(0xaa, abi_size);
921 return Result.appended;
922 },
923 .Pointer => switch (val.tag()) {
924 .variable => {
925 const decl = val.castTag(.variable).?.data.owner_decl;
926 return try self.lowerDeclRef(decl);
927 },
928 .decl_ref => {
929 const decl = val.castTag(.decl_ref).?.data;
930 return try self.lowerDeclRef(decl);
931 },
932 .slice => {
933 const slice = val.castTag(.slice).?.data;
934 var buf: Type.SlicePtrFieldTypeBuffer = undefined;
935 const ptr_ty = ty.slicePtrFieldType(&buf);
936 switch (try self.genDecl(ptr_ty, slice.ptr)) {
937 .externally_managed => |data| try self.code.appendSlice(data),
938 .appended => {},
939 }
940 switch (try self.genDecl(Type.usize, slice.len)) {
941 .externally_managed => |data| try self.code.appendSlice(data),
942 .appended => {},
943 }
944 return Result.appended;
945 },
946 else => return self.fail("TODO: Implement zig decl gen for pointer type value: '{s}'", .{@tagName(val.tag())}),
862 },947 },
863 else => |tag| return self.fail("TODO: Implement zig type codegen for type: '{s}'", .{tag}),948 else => |tag| return self.fail("TODO: Implement zig type codegen for type: '{s}'", .{tag}),
864 }949 }
865}950}
866951
952fn lowerDeclRef(self: *Self, decl: *Module.Decl) InnerError!Result {
953 decl.alive = true;
954
955 const offset = @intCast(u32, self.code.items.len);
956 const atom = &self.decl.link.wasm;
957 const target_sym_index = decl.link.wasm.sym_index;
958
959 if (decl.ty.zigTypeTag() == .Fn) {
960 // We found a function pointer, so add it to our table,
961 // as function pointers are not allowed to be stored inside the data section,
962 // but rather in a function table which are called by index
963 try self.bin_file.addTableFunction(target_sym_index);
964 try atom.relocs.append(self.gpa, .{
965 .index = target_sym_index,
966 .offset = offset,
967 .relocation_type = .R_WASM_TABLE_INDEX_I32,
968 });
969 } else {
970 try atom.relocs.append(self.gpa, .{
971 .index = target_sym_index,
972 .offset = offset,
973 .relocation_type = .R_WASM_MEMORY_ADDR_I32,
974 });
975 }
976 const ptr_width = @intCast(usize, self.target.cpu.arch.ptrBitWidth() / 8);
977 try self.code.appendNTimes(0xaa, ptr_width);
978
979 return Result.appended;
980}
981
867const CallWValues = struct {982const CallWValues = struct {
868 args: []WValue,983 args: []WValue,
869 return_value: WValue,984 return_value: WValue,
...@@ -1015,6 +1130,10 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {...@@ -1015,6 +1130,10 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
1015 .loop => self.airLoop(inst),1130 .loop => self.airLoop(inst),
1016 .not => self.airNot(inst),1131 .not => self.airNot(inst),
1017 .ret => self.airRet(inst),1132 .ret => self.airRet(inst),
1133 .ret_ptr => self.airRetPtr(inst),
1134 .ret_load => self.airRetLoad(inst),
1135 .slice_len => self.airSliceLen(inst),
1136 .slice_elem_val => self.airSliceElemVal(inst),
1018 .store => self.airStore(inst),1137 .store => self.airStore(inst),
1019 .struct_field_ptr => self.airStructFieldPtr(inst),1138 .struct_field_ptr => self.airStructFieldPtr(inst),
1020 .struct_field_ptr_index_0 => self.airStructFieldPtrIndex(inst, 0),1139 .struct_field_ptr_index_0 => self.airStructFieldPtrIndex(inst, 0),
...@@ -1027,6 +1146,7 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {...@@ -1027,6 +1146,7 @@ fn genInst(self: *Self, inst: Air.Inst.Index) !WValue {
1027 .wrap_optional => self.airWrapOptional(inst),1146 .wrap_optional => self.airWrapOptional(inst),
10281147
1029 .unwrap_errunion_payload => self.airUnwrapErrUnionPayload(inst),1148 .unwrap_errunion_payload => self.airUnwrapErrUnionPayload(inst),
1149 .unwrap_errunion_err => self.airUnwrapErrUnionError(inst),
1030 .wrap_errunion_payload => self.airWrapErrUnionPayload(inst),1150 .wrap_errunion_payload => self.airWrapErrUnionPayload(inst),
10311151
1032 .optional_payload => self.airOptionalPayload(inst),1152 .optional_payload => self.airOptionalPayload(inst),
...@@ -1059,13 +1179,48 @@ fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!WValue {...@@ -1059,13 +1179,48 @@ fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
1059 return .none;1179 return .none;
1060}1180}
10611181
1182fn airRetPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
1183 const child_type = self.air.typeOfIndex(inst).childType();
1184
1185 // Initialize the stack
1186 if (self.initial_stack_value == .none) {
1187 try self.initializeStack();
1188 }
1189
1190 const abi_size = child_type.abiSize(self.target);
1191 if (abi_size == 0) return WValue{ .none = {} };
1192
1193 // local, containing the offset to the stack position
1194 const local = try self.allocLocal(Type.initTag(.i32)); // always pointer therefore i32
1195 try self.moveStack(@intCast(u32, abi_size), local.local);
1196
1197 return local;
1198}
1199
1200fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
1201 const un_op = self.air.instructions.items(.data)[inst].un_op;
1202 const operand = self.resolveInst(un_op);
1203 const result = try self.load(operand, self.air.typeOf(un_op), 0);
1204 try self.addLabel(.local_get, result.local);
1205 try self.restoreStackPointer();
1206 try self.addTag(.@"return");
1207 return .none;
1208}
1209
1062fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue {1210fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
1063 const pl_op = self.air.instructions.items(.data)[inst].pl_op;1211 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
1064 const extra = self.air.extraData(Air.Call, pl_op.payload);1212 const extra = self.air.extraData(Air.Call, pl_op.payload);
1065 const args = self.air.extra[extra.end..][0..extra.data.args_len];1213 const args = self.air.extra[extra.end..][0..extra.data.args_len];
1214 const ty = self.air.typeOf(pl_op.operand);
10661215
1067 const target: *Decl = blk: {1216 const fn_ty = switch (ty.zigTypeTag()) {
1068 const func_val = self.air.value(pl_op.operand).?;1217 .Fn => ty,
1218 .Pointer => ty.childType(),
1219 else => unreachable,
1220 };
1221
1222 const target: ?*Decl = blk: {
1223 const func_val = self.air.value(pl_op.operand) orelse break :blk null;
10691224
1070 if (func_val.castTag(.function)) |func| {1225 if (func_val.castTag(.function)) |func| {
1071 break :blk func.data.owner_decl;1226 break :blk func.data.owner_decl;
...@@ -1080,9 +1235,25 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue {...@@ -1080,9 +1235,25 @@ fn airCall(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
1080 try self.emitWValue(arg_val);1235 try self.emitWValue(arg_val);
1081 }1236 }
10821237
1083 try self.addLabel(.call, target.link.wasm.sym_index);1238 if (target) |direct| {
1239 try self.addLabel(.call, direct.link.wasm.sym_index);
1240 } else {
1241 // in this case we call a function pointer
1242 // so load its value onto the stack
1243 std.debug.assert(ty.zigTypeTag() == .Pointer);
1244 const operand = self.resolveInst(pl_op.operand);
1245 try self.emitWValue(operand);
1246 const result = try self.load(operand, fn_ty, operand.local_with_offset.offset);
1247 try self.addLabel(.local_get, result.local);
1248
1249 var fn_type = try self.genFunctype(fn_ty);
1250 defer fn_type.deinit(self.gpa);
10841251
1085 const ret_ty = target.ty.fnReturnType();1252 const fn_type_index = try self.bin_file.putOrGetFuncType(fn_type);
1253 try self.addLabel(.call_indirect, fn_type_index);
1254 }
1255
1256 const ret_ty = fn_ty.fnReturnType();
1086 switch (ret_ty.zigTypeTag()) {1257 switch (ret_ty.zigTypeTag()) {
1087 .Void, .NoReturn => return WValue.none,1258 .Void, .NoReturn => return WValue.none,
1088 else => {1259 else => {
...@@ -1145,13 +1316,16 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro...@@ -1145,13 +1316,16 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro
1145 // in memory1316 // in memory
1146 try self.emitWValue(rhs);1317 try self.emitWValue(rhs);
1147 const tag_local = try self.allocLocal(tag_ty);1318 const tag_local = try self.allocLocal(tag_ty);
1148 const payload_local = try self.allocLocal(payload_ty);
11491319
1150 try self.addLabel(.local_set, payload_local.local);1320 if (payload_ty.hasCodeGenBits()) {
1321 const payload_local = try self.allocLocal(payload_ty);
1322 try self.addLabel(.local_set, payload_local.local);
1323 try self.store(lhs, payload_local, payload_ty, payload_offset);
1324 }
1151 try self.addLabel(.local_set, tag_local.local);1325 try self.addLabel(.local_set, tag_local.local);
11521326
1153 try self.store(lhs, tag_local, tag_ty, 0);1327 try self.store(lhs, tag_local, tag_ty, 0);
1154 return try self.store(lhs, payload_local, payload_ty, payload_offset);1328 return;
1155 },1329 },
1156 .local => {1330 .local => {
1157 // Load values from `rhs` stack position and store in `lhs` instead1331 // Load values from `rhs` stack position and store in `lhs` instead
...@@ -1197,9 +1371,18 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro...@@ -1197,9 +1371,18 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro
1197 try self.emitWValue(lhs);1371 try self.emitWValue(lhs);
1198 try self.emitWValue(rhs);1372 try self.emitWValue(rhs);
1199 const valtype = try self.typeToValtype(ty);1373 const valtype = try self.typeToValtype(ty);
1374 // check if we should pass by pointer or value based on ABI size
1375 // TODO: Implement a way to get ABI values from a given type,
1376 // that is portable across the backend, rather than copying logic.
1377 const abi_size = if ((ty.isInt() or ty.isAnyFloat()) and ty.abiSize(self.target) <= 8)
1378 @intCast(u8, ty.abiSize(self.target))
1379 else if (ty.zigTypeTag() == .ErrorSet or ty.zigTypeTag() == .Enum)
1380 @intCast(u8, ty.abiSize(self.target))
1381 else
1382 @as(u8, 4);
1200 const opcode = buildOpcode(.{1383 const opcode = buildOpcode(.{
1201 .valtype1 = valtype,1384 .valtype1 = valtype,
1202 .width = @intCast(u8, Type.abiSize(ty, self.target) * 8), // use bitsize instead of byte size1385 .width = abi_size * 8, // use bitsize instead of byte size
1203 .op = .store,1386 .op = .store,
1204 });1387 });
12051388
...@@ -1220,7 +1403,7 @@ fn airLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue {...@@ -1220,7 +1403,7 @@ fn airLoad(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
1220 const ty = self.air.getRefType(ty_op.ty);1403 const ty = self.air.getRefType(ty_op.ty);
12211404
1222 return switch (ty.zigTypeTag()) {1405 return switch (ty.zigTypeTag()) {
1223 .Struct, .ErrorUnion, .Optional => operand, // pass as pointer1406 .Struct, .ErrorUnion, .Optional, .Pointer => operand, // pass as pointer
1224 else => switch (operand) {1407 else => switch (operand) {
1225 .local_with_offset => |with_offset| try self.load(operand, ty, with_offset.offset),1408 .local_with_offset => |with_offset| try self.load(operand, ty, with_offset.offset),
1226 else => try self.load(operand, ty, 0),1409 else => try self.load(operand, ty, 0),
...@@ -1233,9 +1416,19 @@ fn load(self: *Self, operand: WValue, ty: Type, offset: u32) InnerError!WValue {...@@ -1233,9 +1416,19 @@ fn load(self: *Self, operand: WValue, ty: Type, offset: u32) InnerError!WValue {
1233 try self.emitWValue(operand);1416 try self.emitWValue(operand);
1234 // Build the opcode with the right bitsize1417 // Build the opcode with the right bitsize
1235 const signedness: std.builtin.Signedness = if (ty.isUnsignedInt()) .unsigned else .signed;1418 const signedness: std.builtin.Signedness = if (ty.isUnsignedInt()) .unsigned else .signed;
1419 // check if we should pass by pointer or value based on ABI size
1420 // TODO: Implement a way to get ABI values from a given type,
1421 // that is portable across the backend, rather than copying logic.
1422 const abi_size = if ((ty.isInt() or ty.isAnyFloat()) and ty.abiSize(self.target) <= 8)
1423 @intCast(u8, ty.abiSize(self.target))
1424 else if (ty.zigTypeTag() == .ErrorSet or ty.zigTypeTag() == .Enum)
1425 @intCast(u8, ty.abiSize(self.target))
1426 else
1427 @as(u8, 4);
1428
1236 const opcode = buildOpcode(.{1429 const opcode = buildOpcode(.{
1237 .valtype1 = try self.typeToValtype(ty),1430 .valtype1 = try self.typeToValtype(ty),
1238 .width = @intCast(u8, Type.abiSize(ty, self.target) * 8), // use bitsize instead of byte size1431 .width = abi_size * 8, // use bitsize instead of byte size
1239 .op = .load,1432 .op = .load,
1240 .signedness = signedness,1433 .signedness = signedness,
1241 });1434 });
...@@ -1399,14 +1592,19 @@ fn emitConstant(self: *Self, val: Value, ty: Type) InnerError!void {...@@ -1399,14 +1592,19 @@ fn emitConstant(self: *Self, val: Value, ty: Type) InnerError!void {
1399 const payload_val = pl.data;1592 const payload_val = pl.data;
1400 // no error, so write a '0' const1593 // no error, so write a '0' const
1401 try self.addImm32(0);1594 try self.addImm32(0);
1402 // after the error code, we emit the payload1595
1403 try self.emitConstant(payload_val, payload_type);1596 if (payload_type.hasCodeGenBits()) {
1597 // after the error code, we emit the payload
1598 try self.emitConstant(payload_val, payload_type);
1599 }
1404 } else {1600 } else {
1405 // write the error val1601 // write the error val
1406 try self.emitConstant(val, error_type);1602 try self.emitConstant(val, error_type);
14071603
1408 // no payload, so write a '0' const1604 if (payload_type.hasCodeGenBits()) {
1409 try self.addImm32(0);1605 // no payload, so write a '0' const
1606 try self.addImm32(0);
1607 }
1410 }1608 }
1411 },1609 },
1412 .Optional => {1610 .Optional => {
...@@ -1867,8 +2065,19 @@ fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue...@@ -1867,8 +2065,19 @@ fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue
1867 const ty_op = self.air.instructions.items(.data)[inst].ty_op;2065 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
1868 const operand = self.resolveInst(ty_op.operand);2066 const operand = self.resolveInst(ty_op.operand);
1869 const err_ty = self.air.typeOf(ty_op.operand);2067 const err_ty = self.air.typeOf(ty_op.operand);
2068 const payload_ty = err_ty.errorUnionPayload();
2069 if (!payload_ty.hasCodeGenBits()) return WValue.none;
1870 const offset = @intCast(u32, err_ty.errorUnionSet().abiSize(self.target));2070 const offset = @intCast(u32, err_ty.errorUnionSet().abiSize(self.target));
1871 return self.load(operand, err_ty.errorUnionPayload(), offset);2071 return try self.load(operand, payload_ty, offset);
2072}
2073
2074fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2075 if (self.liveness.isUnused(inst)) return WValue.none;
2076
2077 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
2078 const operand = self.resolveInst(ty_op.operand);
2079 const err_ty = self.air.typeOf(ty_op.operand);
2080 return try self.load(operand, err_ty.errorUnionSet(), 0);
1872}2081}
18732082
1874fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {2083fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
...@@ -1886,18 +2095,20 @@ fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue {...@@ -1886,18 +2095,20 @@ fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
1886 const op_bits = ref_info.bits;2095 const op_bits = ref_info.bits;
1887 const wanted_bits = ty.intInfo(self.target).bits;2096 const wanted_bits = ty.intInfo(self.target).bits;
18882097
1889 try self.emitWValue(operand);
1890 if (op_bits > 32 and wanted_bits <= 32) {2098 if (op_bits > 32 and wanted_bits <= 32) {
2099 try self.emitWValue(operand);
1891 try self.addTag(.i32_wrap_i64);2100 try self.addTag(.i32_wrap_i64);
1892 } else if (op_bits <= 32 and wanted_bits > 32) {2101 } else if (op_bits <= 32 and wanted_bits > 32) {
2102 try self.emitWValue(operand);
1893 try self.addTag(switch (ref_info.signedness) {2103 try self.addTag(switch (ref_info.signedness) {
1894 .signed => .i64_extend_i32_s,2104 .signed => .i64_extend_i32_s,
1895 .unsigned => .i64_extend_i32_u,2105 .unsigned => .i64_extend_i32_u,
1896 });2106 });
1897 }2107 } else return operand;
18982108
1899 // other cases are no-op2109 const result = try self.allocLocal(ty);
1900 return .none;2110 try self.addLabel(.local_set, result.local);
2111 return result;
1901}2112}
19022113
1903fn airIsNull(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!WValue {2114fn airIsNull(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!WValue {
...@@ -1961,3 +2172,67 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!WValue {...@@ -1961,3 +2172,67 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
1961 .offset = @intCast(u32, offset),2172 .offset = @intCast(u32, offset),
1962 } };2173 } };
1963}2174}
2175
2176fn airSliceLen(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2177 if (self.liveness.isUnused(inst)) return WValue.none;
2178
2179 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
2180 const operand = self.resolveInst(ty_op.operand);
2181 const pointer_width = self.target.cpu.arch.ptrBitWidth() / 8;
2182
2183 // Get pointer to slice
2184 try self.emitWValue(operand);
2185 // length of slice is stored after the pointer of the slice
2186 const extra_index = try self.addExtra(Mir.MemArg{
2187 .offset = pointer_width,
2188 .alignment = pointer_width,
2189 });
2190 try self.addInst(.{ .tag = .i32_load, .data = .{ .payload = extra_index } });
2191
2192 const result = try self.allocLocal(Type.initTag(.i32)); // pointer is always i32
2193 // store slice length in local
2194 try self.addLabel(.local_set, result.local);
2195 return result;
2196}
2197
2198fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue {
2199 if (self.liveness.isUnused(inst)) return WValue.none;
2200
2201 const bin_op = self.air.instructions.items(.data)[inst].bin_op;
2202 const slice_ty = self.air.typeOf(bin_op.lhs);
2203 const slice = self.resolveInst(bin_op.lhs);
2204 const index = self.resolveInst(bin_op.rhs);
2205 const elem_ty = slice_ty.childType();
2206 const elem_size = elem_ty.abiSize(self.target);
2207
2208 // load pointer onto stack
2209 try self.emitWValue(slice);
2210
2211 // calculate index into slice
2212 try self.emitWValue(index);
2213 try self.addImm32(@bitCast(i32, @intCast(u32, elem_size)));
2214 try self.addTag(.i32_mul);
2215 try self.addTag(.i32_add);
2216
2217 const abi_size = if (elem_size < 8)
2218 @intCast(u8, elem_size)
2219 else
2220 @as(u8, 4); // elements larger than 8 bytes will be passed by pointer
2221
2222 const extra_index = try self.addExtra(Mir.MemArg{
2223 .offset = 0,
2224 .alignment = elem_ty.abiAlignment(self.target),
2225 });
2226 const signedness: std.builtin.Signedness = if (elem_ty.isUnsignedInt()) .unsigned else .signed;
2227 const opcode = buildOpcode(.{
2228 .valtype1 = try self.typeToValtype(elem_ty),
2229 .width = abi_size * 8,
2230 .op = .load,
2231 .signedness = signedness,
2232 });
2233 try self.addInst(.{ .tag = Mir.Inst.Tag.fromOpcode(opcode), .data = .{ .payload = extra_index } });
2234
2235 const result = try self.allocLocal(elem_ty);
2236 try self.addLabel(.local_set, result.local);
2237 return result;
2238}
src/arch/wasm/Emit.zig+9-1
...@@ -47,6 +47,7 @@ pub fn emitMir(emit: *Emit) InnerError!void {...@@ -47,6 +47,7 @@ pub fn emitMir(emit: *Emit) InnerError!void {
4747
48 // relocatables48 // relocatables
49 .call => try emit.emitCall(inst),49 .call => try emit.emitCall(inst),
50 .call_indirect => try emit.emitCallIndirect(inst),
50 .global_get => try emit.emitGlobal(tag, inst),51 .global_get => try emit.emitGlobal(tag, inst),
51 .global_set => try emit.emitGlobal(tag, inst),52 .global_set => try emit.emitGlobal(tag, inst),
52 .memory_address => try emit.emitMemAddress(inst),53 .memory_address => try emit.emitMemAddress(inst),
...@@ -256,7 +257,7 @@ fn emitMemArg(emit: *Emit, tag: Mir.Inst.Tag, inst: Mir.Inst.Index) !void {...@@ -256,7 +257,7 @@ fn emitMemArg(emit: *Emit, tag: Mir.Inst.Tag, inst: Mir.Inst.Index) !void {
256 try emit.code.append(@enumToInt(tag));257 try emit.code.append(@enumToInt(tag));
257258
258 // wasm encodes alignment as power of 2, rather than natural alignment259 // wasm encodes alignment as power of 2, rather than natural alignment
259 const encoded_alignment = mem_arg.alignment >> 1;260 const encoded_alignment = @ctz(u32, mem_arg.alignment);
260 try leb128.writeULEB128(emit.code.writer(), encoded_alignment);261 try leb128.writeULEB128(emit.code.writer(), encoded_alignment);
261 try leb128.writeULEB128(emit.code.writer(), mem_arg.offset);262 try leb128.writeULEB128(emit.code.writer(), mem_arg.offset);
262}263}
...@@ -276,6 +277,13 @@ fn emitCall(emit: *Emit, inst: Mir.Inst.Index) !void {...@@ -276,6 +277,13 @@ fn emitCall(emit: *Emit, inst: Mir.Inst.Index) !void {
276 });277 });
277}278}
278279
280fn emitCallIndirect(emit: *Emit, inst: Mir.Inst.Index) !void {
281 const label = emit.mir.instructions.items(.data)[inst].label;
282 try emit.code.append(std.wasm.opcode(.call_indirect));
283 try leb128.writeULEB128(emit.code.writer(), @as(u32, 0)); // TODO: Emit relocation for table index
284 try leb128.writeULEB128(emit.code.writer(), label);
285}
286
279fn emitMemAddress(emit: *Emit, inst: Mir.Inst.Index) !void {287fn emitMemAddress(emit: *Emit, inst: Mir.Inst.Index) !void {
280 const symbol_index = emit.mir.instructions.items(.data)[inst].label;288 const symbol_index = emit.mir.instructions.items(.data)[inst].label;
281 try emit.code.append(std.wasm.opcode(.i32_const));289 try emit.code.append(std.wasm.opcode(.i32_const));
src/arch/wasm/Mir.zig+5
...@@ -69,6 +69,11 @@ pub const Inst = struct {...@@ -69,6 +69,11 @@ pub const Inst = struct {
69 ///69 ///
70 /// Uses `label`70 /// Uses `label`
71 call = 0x10,71 call = 0x10,
72 /// Calls a function pointer by its function signature
73 /// and index into the function table.
74 ///
75 /// Uses `label`
76 call_indirect = 0x11,
72 /// Loads a local at given index onto the stack.77 /// Loads a local at given index onto the stack.
73 ///78 ///
74 /// Uses `label`79 /// Uses `label`
src/link/Wasm.zig+65-15
...@@ -79,7 +79,9 @@ memories: wasm.Memory = .{ .limits = .{ .min = 0, .max = null } },...@@ -79,7 +79,9 @@ memories: wasm.Memory = .{ .limits = .{ .min = 0, .max = null } },
79/// Indirect function table, used to call function pointers79/// Indirect function table, used to call function pointers
80/// When this is non-zero, we must emit a table entry,80/// When this is non-zero, we must emit a table entry,
81/// as well as an 'elements' section.81/// as well as an 'elements' section.
82function_table: std.ArrayListUnmanaged(Symbol) = .{},82///
83/// Note: Key is symbol index, value represents the index into the table
84function_table: std.AutoHashMapUnmanaged(u32, u32) = .{},
8385
84pub const Segment = struct {86pub const Segment = struct {
85 alignment: u32,87 alignment: u32,
...@@ -276,7 +278,7 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void {...@@ -276,7 +278,7 @@ pub fn updateDecl(self: *Wasm, module: *Module, decl: *Module.Decl) !void {
276 defer codegen.deinit();278 defer codegen.deinit();
277279
278 // generate the 'code' section for the function declaration280 // generate the 'code' section for the function declaration
279 const result = codegen.gen(decl.ty, decl.val) catch |err| switch (err) {281 const result = codegen.genDecl(decl.ty, decl.val) catch |err| switch (err) {
280 error.CodegenFail => {282 error.CodegenFail => {
281 decl.analysis = .codegen_failure;283 decl.analysis = .codegen_failure;
282 try module.failed_decls.put(module.gpa, decl, codegen.err_msg);284 try module.failed_decls.put(module.gpa, decl, codegen.err_msg);
...@@ -334,6 +336,25 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void {...@@ -334,6 +336,25 @@ pub fn freeDecl(self: *Wasm, decl: *Module.Decl) void {
334 else => unreachable,336 else => unreachable,
335 }337 }
336 }338 }
339
340 // maybe remove from function table if needed
341 if (decl.ty.zigTypeTag() == .Fn) {
342 _ = self.function_table.remove(atom.sym_index);
343 }
344}
345
346/// Appends a new entry to the indirect function table
347pub fn addTableFunction(self: *Wasm, symbol_index: u32) !void {
348 const index = @intCast(u32, self.function_table.count());
349 try self.function_table.put(self.base.allocator, symbol_index, index);
350}
351
352fn mapFunctionTable(self: *Wasm) void {
353 var it = self.function_table.valueIterator();
354 var index: u32 = 0;
355 while (it.next()) |value_ptr| : (index += 1) {
356 value_ptr.* = index;
357 }
337}358}
338359
339fn addOrUpdateImport(self: *Wasm, decl: *Module.Decl) !void {360fn addOrUpdateImport(self: *Wasm, decl: *Module.Decl) !void {
...@@ -583,6 +604,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {...@@ -583,6 +604,7 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
583604
584 try self.setupMemory();605 try self.setupMemory();
585 try self.allocateAtoms();606 try self.allocateAtoms();
607 self.mapFunctionTable();
586608
587 const file = self.base.file.?;609 const file = self.base.file.?;
588 const header_size = 5 + 1;610 const header_size = 5 + 1;
...@@ -662,6 +684,22 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {...@@ -662,6 +684,22 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
662 );684 );
663 }685 }
664686
687 if (self.function_table.count() > 0) {
688 const header_offset = try reserveVecSectionHeader(file);
689 const writer = file.writer();
690
691 try leb.writeULEB128(writer, wasm.reftype(.funcref));
692 try emitLimits(writer, .{ .min = 1, .max = null });
693
694 try writeVecSectionHeader(
695 file,
696 header_offset,
697 .table,
698 @intCast(u32, (try file.getPos()) - header_offset - header_size),
699 @as(u32, 1),
700 );
701 }
702
665 // Memory section703 // Memory section
666 if (!self.base.options.import_memory) {704 if (!self.base.options.import_memory) {
667 const header_offset = try reserveVecSectionHeader(file);705 const header_offset = try reserveVecSectionHeader(file);
...@@ -743,6 +781,31 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {...@@ -743,6 +781,31 @@ pub fn flushModule(self: *Wasm, comp: *Compilation) !void {
743 );781 );
744 }782 }
745783
784 // element section (function table)
785 if (self.function_table.count() > 0) {
786 const header_offset = try reserveVecSectionHeader(file);
787 const writer = file.writer();
788
789 var flags: u32 = 0x2; // Yes we have a table
790 try leb.writeULEB128(writer, flags);
791 try leb.writeULEB128(writer, @as(u32, 0)); // index of that table. TODO: Store synthetic symbols
792 try emitInit(writer, .{ .i32_const = 0 });
793 try leb.writeULEB128(writer, @as(u8, 0));
794 try leb.writeULEB128(writer, @intCast(u32, self.function_table.count()));
795 var symbol_it = self.function_table.keyIterator();
796 while (symbol_it.next()) |symbol_index_ptr| {
797 try leb.writeULEB128(writer, self.symbols.items[symbol_index_ptr.*].index);
798 }
799
800 try writeVecSectionHeader(
801 file,
802 header_offset,
803 .element,
804 @intCast(u32, (try file.getPos()) - header_offset - header_size),
805 @as(u32, 1),
806 );
807 }
808
746 // Code section809 // Code section
747 if (self.code_section_index) |code_index| {810 if (self.code_section_index) |code_index| {
748 const header_offset = try reserveVecSectionHeader(file);811 const header_offset = try reserveVecSectionHeader(file);
...@@ -1233,16 +1296,3 @@ pub fn putOrGetFuncType(self: *Wasm, func_type: wasm.Type) !u32 {...@@ -1233,16 +1296,3 @@ pub fn putOrGetFuncType(self: *Wasm, func_type: wasm.Type) !u32 {
1233 });1296 });
1234 return index;1297 return index;
1235}1298}
1236
1237/// From a given index and an `ExternalKind`, finds the corresponding Import.
1238/// This is due to indexes for imports being unique per type, rather than across all imports.
1239fn findImport(self: Wasm, index: u32, external_type: wasm.ExternalKind) ?*wasm.Import {
1240 var current_index: u32 = 0;
1241 for (self.imports.items) |*import| {
1242 if (import.kind == external_type) {
1243 if (current_index == index) return import;
1244 current_index += 1;
1245 }
1246 }
1247 return null;
1248}
src/link/Wasm/Atom.zig+3-5
...@@ -83,7 +83,7 @@ pub fn resolveRelocs(self: *Atom, wasm_bin: *const Wasm) !void {...@@ -83,7 +83,7 @@ pub fn resolveRelocs(self: *Atom, wasm_bin: *const Wasm) !void {
8383
84 for (self.relocs.items) |reloc| {84 for (self.relocs.items) |reloc| {
85 const value = try relocationValue(reloc, wasm_bin);85 const value = try relocationValue(reloc, wasm_bin);
86 log.debug("Relocating '{s}' referenced in '{s}' offset=0x{x:0>8} value={d}\n", .{86 log.debug("Relocating '{s}' referenced in '{s}' offset=0x{x:0>8} value={d}", .{
87 wasm_bin.symbols.items[reloc.index].name,87 wasm_bin.symbols.items[reloc.index].name,
88 symbol.name,88 symbol.name,
89 reloc.offset,89 reloc.offset,
...@@ -129,7 +129,7 @@ fn relocationValue(relocation: types.Relocation, wasm_bin: *const Wasm) !u64 {...@@ -129,7 +129,7 @@ fn relocationValue(relocation: types.Relocation, wasm_bin: *const Wasm) !u64 {
129 .R_WASM_TABLE_INDEX_I64,129 .R_WASM_TABLE_INDEX_I64,
130 .R_WASM_TABLE_INDEX_SLEB,130 .R_WASM_TABLE_INDEX_SLEB,
131 .R_WASM_TABLE_INDEX_SLEB64,131 .R_WASM_TABLE_INDEX_SLEB64,
132 => return error.TodoImplementTableIndex, // find table index from a function symbol132 => return wasm_bin.function_table.get(relocation.index) orelse 0,
133 .R_WASM_TYPE_INDEX_LEB => wasm_bin.functions.items[symbol.index].type_index,133 .R_WASM_TYPE_INDEX_LEB => wasm_bin.functions.items[symbol.index].type_index,
134 .R_WASM_GLOBAL_INDEX_I32,134 .R_WASM_GLOBAL_INDEX_I32,
135 .R_WASM_GLOBAL_INDEX_LEB,135 .R_WASM_GLOBAL_INDEX_LEB,
...@@ -152,9 +152,7 @@ fn relocationValue(relocation: types.Relocation, wasm_bin: *const Wasm) !u64 {...@@ -152,9 +152,7 @@ fn relocationValue(relocation: types.Relocation, wasm_bin: *const Wasm) !u64 {
152 target_atom = target_atom.next orelse break;152 target_atom = target_atom.next orelse break;
153 }153 }
154 const segment = wasm_bin.segments.items[atom_index];154 const segment = wasm_bin.segments.items[atom_index];
155 const base = wasm_bin.base.options.global_base orelse 1024;155 break :blk target_atom.offset + segment.offset + (relocation.addend orelse 0);
156 const offset = target_atom.offset + segment.offset;
157 break :blk offset + base + (relocation.addend orelse 0);
158 },156 },
159 .R_WASM_EVENT_INDEX_LEB => symbol.index,157 .R_WASM_EVENT_INDEX_LEB => symbol.index,
160 .R_WASM_SECTION_OFFSET_I32,158 .R_WASM_SECTION_OFFSET_I32,
test/cases.zig+1-1
...@@ -26,7 +26,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -26,7 +26,7 @@ pub fn addCases(ctx: *TestContext) !void {
26 var case = ctx.exe("hello world with updates", linux_x64);26 var case = ctx.exe("hello world with updates", linux_x64);
2727
28 case.addError("", &[_][]const u8{28 case.addError("", &[_][]const u8{
29 ":97:9: error: struct 'tmp.tmp' has no member named 'main'",29 ":99:9: error: struct 'tmp.tmp' has no member named 'main'",
30 });30 });
3131
32 // Incorrect return type32 // Incorrect return type
test/stage2/darwin.zig+1-1
...@@ -14,7 +14,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -14,7 +14,7 @@ pub fn addCases(ctx: *TestContext) !void {
14 {14 {
15 var case = ctx.exe("darwin hello world with updates", target);15 var case = ctx.exe("darwin hello world with updates", target);
16 case.addError("", &[_][]const u8{16 case.addError("", &[_][]const u8{
17 ":97:9: error: struct 'tmp.tmp' has no member named 'main'",17 ":99:9: error: struct 'tmp.tmp' has no member named 'main'",
18 });18 });
1919
20 // Incorrect return type20 // Incorrect return type
test/stage2/wasm.zig+206-181
...@@ -11,7 +11,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -11,7 +11,7 @@ pub fn addCases(ctx: *TestContext) !void {
11 var case = ctx.exe("wasm function calls", wasi);11 var case = ctx.exe("wasm function calls", wasi);
1212
13 case.addCompareOutput(13 case.addCompareOutput(
14 \\pub export fn _start() u32 {14 \\pub fn main() u8 {
15 \\ foo();15 \\ foo();
16 \\ bar();16 \\ bar();
17 \\ return 42;17 \\ return 42;
...@@ -26,7 +26,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -26,7 +26,7 @@ pub fn addCases(ctx: *TestContext) !void {
26 );26 );
2727
28 case.addCompareOutput(28 case.addCompareOutput(
29 \\pub export fn _start() i64 {29 \\pub fn main() u8 {
30 \\ bar();30 \\ bar();
31 \\ foo();31 \\ foo();
32 \\ foo();32 \\ foo();
...@@ -44,10 +44,10 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -44,10 +44,10 @@ pub fn addCases(ctx: *TestContext) !void {
44 );44 );
4545
46 case.addCompareOutput(46 case.addCompareOutput(
47 \\pub export fn _start() f32 {47 \\pub fn main() void {
48 \\ bar();48 \\ bar();
49 \\ foo();49 \\ foo();
50 \\ return 42.0;50 \\ return;
51 \\}51 \\}
52 \\fn foo() void {52 \\fn foo() void {
53 \\ bar();53 \\ bar();
...@@ -56,15 +56,15 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -56,15 +56,15 @@ pub fn addCases(ctx: *TestContext) !void {
56 \\}56 \\}
57 \\fn bar() void {}57 \\fn bar() void {}
58 ,58 ,
59 "42\n",59 "0\n",
60 );60 );
6161
62 case.addCompareOutput(62 case.addCompareOutput(
63 \\pub export fn _start() u32 {63 \\pub fn main() u8 {
64 \\ foo(10, 20);64 \\ foo(10, 20);
65 \\ return 5;65 \\ return 5;
66 \\}66 \\}
67 \\fn foo(x: u32, y: u32) void { _ = x; _ = y; }67 \\fn foo(x: u8, y: u8) void { _ = x; _ = y; }
68 , "5\n");68 , "5\n");
69 }69 }
7070
...@@ -72,10 +72,10 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -72,10 +72,10 @@ pub fn addCases(ctx: *TestContext) !void {
72 var case = ctx.exe("wasm locals", wasi);72 var case = ctx.exe("wasm locals", wasi);
7373
74 case.addCompareOutput(74 case.addCompareOutput(
75 \\pub export fn _start() u32 {75 \\pub fn main() u8 {
76 \\ var i: u32 = 5;76 \\ var i: u8 = 5;
77 \\ var y: f32 = 42.0;77 \\ var y: f32 = 42.0;
78 \\ var x: u32 = 10;78 \\ var x: u8 = 10;
79 \\ if (false) {79 \\ if (false) {
80 \\ y;80 \\ y;
81 \\ x;81 \\ x;
...@@ -85,18 +85,18 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -85,18 +85,18 @@ pub fn addCases(ctx: *TestContext) !void {
85 , "5\n");85 , "5\n");
8686
87 case.addCompareOutput(87 case.addCompareOutput(
88 \\pub export fn _start() u32 {88 \\pub fn main() u8 {
89 \\ var i: u32 = 5;89 \\ var i: u8 = 5;
90 \\ var y: f32 = 42.0;90 \\ var y: f32 = 42.0;
91 \\ _ = y;91 \\ _ = y;
92 \\ var x: u32 = 10;92 \\ var x: u8 = 10;
93 \\ foo(i, x);93 \\ foo(i, x);
94 \\ i = x;94 \\ i = x;
95 \\ return i;95 \\ return i;
96 \\}96 \\}
97 \\fn foo(x: u32, y: u32) void {97 \\fn foo(x: u8, y: u8) void {
98 \\ _ = y;98 \\ _ = y;
99 \\ var i: u32 = 10;99 \\ var i: u8 = 10;
100 \\ i = x;100 \\ i = x;
101 \\}101 \\}
102 , "10\n");102 , "10\n");
...@@ -106,228 +106,246 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -106,228 +106,246 @@ pub fn addCases(ctx: *TestContext) !void {
106 var case = ctx.exe("wasm binary operands", wasi);106 var case = ctx.exe("wasm binary operands", wasi);
107107
108 case.addCompareOutput(108 case.addCompareOutput(
109 \\pub export fn _start() u32 {109 \\pub fn main() u8 {
110 \\ var i: u32 = 5;110 \\ var i: u8 = 5;
111 \\ i += 20;111 \\ i += 20;
112 \\ return i;112 \\ return i;
113 \\}113 \\}
114 , "25\n");114 , "25\n");
115115
116 case.addCompareOutput(116 case.addCompareOutput(
117 \\pub export fn _start() i32 {117 \\pub fn main() void {
118 \\ var i: i32 = 2147483647;118 \\ var i: i32 = 2147483647;
119 \\ return i +% 1;119 \\ if (i +% 1 != -2147483648) unreachable;
120 \\ return;
120 \\}121 \\}
121 , "-2147483648\n");122 , "0\n");
122123
123 case.addCompareOutput(124 case.addCompareOutput(
124 \\pub export fn _start() i32 {125 \\pub fn main() void {
125 \\ var i: i4 = 7;126 \\ var i: i4 = 7;
126 \\ return i +% 1;127 \\ if (i +% 1 != 0) unreachable;
128 \\ return;
127 \\}129 \\}
128 , "0\n");130 , "0\n");
129131
130 case.addCompareOutput(132 case.addCompareOutput(
131 \\pub export fn _start() u32 {133 \\pub fn main() u8 {
132 \\ var i: u8 = 255;134 \\ var i: u8 = 255;
133 \\ return i +% 1;135 \\ return i +% 1;
134 \\}136 \\}
135 , "0\n");137 , "0\n");
136138
137 case.addCompareOutput(139 case.addCompareOutput(
138 \\pub export fn _start() u32 {140 \\pub fn main() u8 {
139 \\ var i: u32 = 5;141 \\ var i: u8 = 5;
140 \\ i += 20;142 \\ i += 20;
141 \\ var result: u32 = foo(i, 10);143 \\ var result: u8 = foo(i, 10);
142 \\ return result;144 \\ return result;
143 \\}145 \\}
144 \\fn foo(x: u32, y: u32) u32 {146 \\fn foo(x: u8, y: u8) u8 {
145 \\ return x + y;147 \\ return x + y;
146 \\}148 \\}
147 , "35\n");149 , "35\n");
148150
149 case.addCompareOutput(151 case.addCompareOutput(
150 \\pub export fn _start() u32 {152 \\pub fn main() u8 {
151 \\ var i: u32 = 20;153 \\ var i: u8 = 20;
152 \\ i -= 5;154 \\ i -= 5;
153 \\ return i;155 \\ return i;
154 \\}156 \\}
155 , "15\n");157 , "15\n");
156158
157 case.addCompareOutput(159 case.addCompareOutput(
158 \\pub export fn _start() i32 {160 \\pub fn main() void {
159 \\ var i: i32 = -2147483648;161 \\ var i: i32 = -2147483648;
160 \\ return i -% 1;162 \\ if (i -% 1 != 2147483647) unreachable;
163 \\ return;
161 \\}164 \\}
162 , "2147483647\n");165 , "0\n");
163166
164 case.addCompareOutput(167 case.addCompareOutput(
165 \\pub export fn _start() i32 {168 \\pub fn main() void {
166 \\ var i: i7 = -64;169 \\ var i: i7 = -64;
167 \\ return i -% 1;170 \\ if (i -% 1 != 63) unreachable;
171 \\ return;
168 \\}172 \\}
169 , "63\n");173 , "0\n");
170174
171 case.addCompareOutput(175 case.addCompareOutput(
172 \\pub export fn _start() u32 {176 \\pub fn main() u8 {
173 \\ var i: u4 = 0;177 \\ var i: u4 = 0;
174 \\ return i -% 1;178 \\ return i -% 1;
175 \\}179 \\}
176 , "15\n");180 , "15\n");
177181
178 case.addCompareOutput(182 case.addCompareOutput(
179 \\pub export fn _start() u32 {183 \\pub fn main() u8 {
180 \\ var i: u32 = 5;184 \\ var i: u8 = 5;
181 \\ i -= 3;185 \\ i -= 3;
182 \\ var result: u32 = foo(i, 10);186 \\ var result: u8 = foo(i, 10);
183 \\ return result;187 \\ return result;
184 \\}188 \\}
185 \\fn foo(x: u32, y: u32) u32 {189 \\fn foo(x: u8, y: u8) u8 {
186 \\ return y - x;190 \\ return y - x;
187 \\}191 \\}
188 , "8\n");192 , "8\n");
189193
190 case.addCompareOutput(194 case.addCompareOutput(
191 \\pub export fn _start() u32 {195 \\pub fn main() void {
192 \\ var i: u32 = 5;196 \\ var i: u32 = 5;
193 \\ i *= 7;197 \\ i *= 7;
194 \\ var result: u32 = foo(i, 10);198 \\ var result: u32 = foo(i, 10);
195 \\ return result;199 \\ if (result != 350) unreachable;
200 \\ return;
196 \\}201 \\}
197 \\fn foo(x: u32, y: u32) u32 {202 \\fn foo(x: u32, y: u32) u32 {
198 \\ return x * y;203 \\ return x * y;
199 \\}204 \\}
200 , "350\n");205 , "0\n");
201206
202 case.addCompareOutput(207 case.addCompareOutput(
203 \\pub export fn _start() i32 {208 \\pub fn main() void {
204 \\ var i: i32 = 2147483647;209 \\ var i: i32 = 2147483647;
205 \\ return i *% 2;210 \\ const result = i *% 2;
211 \\ if (result != -2) unreachable;
212 \\ return;
206 \\}213 \\}
207 , "-2\n");214 , "0\n");
208215
209 case.addCompareOutput(216 case.addCompareOutput(
210 \\pub export fn _start() u32 {217 \\pub fn main() void {
211 \\ var i: u3 = 3;218 \\ var i: u3 = 3;
212 \\ return i *% 3;219 \\ if (i *% 3 != 1) unreachable;
220 \\ return;
213 \\}221 \\}
214 , "1\n");222 , "0\n");
215223
216 case.addCompareOutput(224 case.addCompareOutput(
217 \\pub export fn _start() i32 {225 \\pub fn main() void {
218 \\ var i: i4 = 3;226 \\ var i: i4 = 3;
219 \\ return i *% 3;227 \\ if (i *% 3 != 1) unreachable;
228 \\ return;
220 \\}229 \\}
221 , "1\n");230 , "0\n");
222231
223 case.addCompareOutput(232 case.addCompareOutput(
224 \\pub export fn _start() u32 {233 \\pub fn main() void {
225 \\ var i: u32 = 352;234 \\ var i: u32 = 352;
226 \\ i /= 7; // i = 50235 \\ i /= 7; // i = 50
227 \\ var result: u32 = foo(i, 7);236 \\ var result: u32 = foo(i, 7);
228 \\ return result;237 \\ if (result != 7) unreachable;
238 \\ return;
229 \\}239 \\}
230 \\fn foo(x: u32, y: u32) u32 {240 \\fn foo(x: u32, y: u32) u32 {
231 \\ return x / y;241 \\ return x / y;
232 \\}242 \\}
233 , "7\n");243 , "0\n");
234244
235 case.addCompareOutput(245 case.addCompareOutput(
236 \\pub export fn _start() u32 {246 \\pub fn main() u8 {
237 \\ var i: u32 = 5;247 \\ var i: u8 = 5;
238 \\ i &= 6;248 \\ i &= 6;
239 \\ return i;249 \\ return i;
240 \\}250 \\}
241 , "4\n");251 , "4\n");
242252
243 case.addCompareOutput(253 case.addCompareOutput(
244 \\pub export fn _start() u32 {254 \\pub fn main() u8 {
245 \\ var i: u32 = 5;255 \\ var i: u8 = 5;
246 \\ i |= 6;256 \\ i |= 6;
247 \\ return i;257 \\ return i;
248 \\}258 \\}
249 , "7\n");259 , "7\n");
250260
251 case.addCompareOutput(261 case.addCompareOutput(
252 \\pub export fn _start() u32 {262 \\pub fn main() u8 {
253 \\ var i: u32 = 5;263 \\ var i: u8 = 5;
254 \\ i ^= 6;264 \\ i ^= 6;
255 \\ return i;265 \\ return i;
256 \\}266 \\}
257 , "3\n");267 , "3\n");
258268
259 case.addCompareOutput(269 case.addCompareOutput(
260 \\pub export fn _start() bool {270 \\pub fn main() void {
261 \\ var b: bool = false;271 \\ var b: bool = false;
262 \\ b = b or false;272 \\ b = b or false;
263 \\ return b;273 \\ if (b) unreachable;
274 \\ return;
264 \\}275 \\}
265 , "0\n");276 , "0\n");
266277
267 case.addCompareOutput(278 case.addCompareOutput(
268 \\pub export fn _start() bool {279 \\pub fn main() void {
269 \\ var b: bool = true;280 \\ var b: bool = true;
270 \\ b = b or false;281 \\ b = b or false;
271 \\ return b;282 \\ if (!b) unreachable;
283 \\ return;
272 \\}284 \\}
273 , "1\n");285 , "0\n");
274286
275 case.addCompareOutput(287 case.addCompareOutput(
276 \\pub export fn _start() bool {288 \\pub fn main() void {
277 \\ var b: bool = false;289 \\ var b: bool = false;
278 \\ b = b or true;290 \\ b = b or true;
279 \\ return b;291 \\ if (!b) unreachable;
292 \\ return;
280 \\}293 \\}
281 , "1\n");294 , "0\n");
282295
283 case.addCompareOutput(296 case.addCompareOutput(
284 \\pub export fn _start() bool {297 \\pub fn main() void {
285 \\ var b: bool = true;298 \\ var b: bool = true;
286 \\ b = b or true;299 \\ b = b or true;
287 \\ return b;300 \\ if (!b) unreachable;
301 \\ return;
288 \\}302 \\}
289 , "1\n");303 , "0\n");
290304
291 case.addCompareOutput(305 case.addCompareOutput(
292 \\pub export fn _start() bool {306 \\pub fn main() void {
293 \\ var b: bool = false;307 \\ var b: bool = false;
294 \\ b = b and false;308 \\ b = b and false;
295 \\ return b;309 \\ if (b) unreachable;
310 \\ return;
296 \\}311 \\}
297 , "0\n");312 , "0\n");
298313
299 case.addCompareOutput(314 case.addCompareOutput(
300 \\pub export fn _start() bool {315 \\pub fn main() void {
301 \\ var b: bool = true;316 \\ var b: bool = true;
302 \\ b = b and false;317 \\ b = b and false;
303 \\ return b;318 \\ if (b) unreachable;
319 \\ return;
304 \\}320 \\}
305 , "0\n");321 , "0\n");
306322
307 case.addCompareOutput(323 case.addCompareOutput(
308 \\pub export fn _start() bool {324 \\pub fn main() void {
309 \\ var b: bool = false;325 \\ var b: bool = false;
310 \\ b = b and true;326 \\ b = b and true;
311 \\ return b;327 \\ if (b) unreachable;
328 \\ return;
312 \\}329 \\}
313 , "0\n");330 , "0\n");
314331
315 case.addCompareOutput(332 case.addCompareOutput(
316 \\pub export fn _start() bool {333 \\pub fn main() void {
317 \\ var b: bool = true;334 \\ var b: bool = true;
318 \\ b = b and true;335 \\ b = b and true;
319 \\ return b;336 \\ if (!b) unreachable;
337 \\ return;
320 \\}338 \\}
321 , "1\n");339 , "0\n");
322 }340 }
323341
324 {342 {
325 var case = ctx.exe("wasm conditions", wasi);343 var case = ctx.exe("wasm conditions", wasi);
326344
327 case.addCompareOutput(345 case.addCompareOutput(
328 \\pub export fn _start() u32 {346 \\pub fn main() u8 {
329 \\ var i: u32 = 5;347 \\ var i: u8 = 5;
330 \\ if (i > @as(u32, 4)) {348 \\ if (i > @as(u8, 4)) {
331 \\ i += 10;349 \\ i += 10;
332 \\ }350 \\ }
333 \\ return i;351 \\ return i;
...@@ -335,9 +353,9 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -335,9 +353,9 @@ pub fn addCases(ctx: *TestContext) !void {
335 , "15\n");353 , "15\n");
336354
337 case.addCompareOutput(355 case.addCompareOutput(
338 \\pub export fn _start() u32 {356 \\pub fn main() u8 {
339 \\ var i: u32 = 5;357 \\ var i: u8 = 5;
340 \\ if (i < @as(u32, 4)) {358 \\ if (i < @as(u8, 4)) {
341 \\ i += 10;359 \\ i += 10;
342 \\ } else {360 \\ } else {
343 \\ i = 2;361 \\ i = 2;
...@@ -347,11 +365,11 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -347,11 +365,11 @@ pub fn addCases(ctx: *TestContext) !void {
347 , "2\n");365 , "2\n");
348366
349 case.addCompareOutput(367 case.addCompareOutput(
350 \\pub export fn _start() u32 {368 \\pub fn main() u8 {
351 \\ var i: u32 = 5;369 \\ var i: u8 = 5;
352 \\ if (i < @as(u32, 4)) {370 \\ if (i < @as(u8, 4)) {
353 \\ i += 10;371 \\ i += 10;
354 \\ } else if(i == @as(u32, 5)) {372 \\ } else if(i == @as(u8, 5)) {
355 \\ i = 20;373 \\ i = 20;
356 \\ }374 \\ }
357 \\ return i;375 \\ return i;
...@@ -359,12 +377,12 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -359,12 +377,12 @@ pub fn addCases(ctx: *TestContext) !void {
359 , "20\n");377 , "20\n");
360378
361 case.addCompareOutput(379 case.addCompareOutput(
362 \\pub export fn _start() u32 {380 \\pub fn main() u8 {
363 \\ var i: u32 = 11;381 \\ var i: u8 = 11;
364 \\ if (i < @as(u32, 4)) {382 \\ if (i < @as(u8, 4)) {
365 \\ i += 10;383 \\ i += 10;
366 \\ } else {384 \\ } else {
367 \\ if (i > @as(u32, 10)) {385 \\ if (i > @as(u8, 10)) {
368 \\ i += 20;386 \\ i += 20;
369 \\ } else {387 \\ } else {
370 \\ i = 20;388 \\ i = 20;
...@@ -375,7 +393,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -375,7 +393,7 @@ pub fn addCases(ctx: *TestContext) !void {
375 , "31\n");393 , "31\n");
376394
377 case.addCompareOutput(395 case.addCompareOutput(
378 \\pub export fn _start() void {396 \\pub fn main() void {
379 \\ assert(foo(true) != @as(i32, 30));397 \\ assert(foo(true) != @as(i32, 30));
380 \\}398 \\}
381 \\399 \\
...@@ -387,10 +405,10 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -387,10 +405,10 @@ pub fn addCases(ctx: *TestContext) !void {
387 \\ const x = if(ok) @as(i32, 20) else @as(i32, 10);405 \\ const x = if(ok) @as(i32, 20) else @as(i32, 10);
388 \\ return x;406 \\ return x;
389 \\}407 \\}
390 , "");408 , "0\n");
391409
392 case.addCompareOutput(410 case.addCompareOutput(
393 \\pub export fn _start() void {411 \\pub fn main() void {
394 \\ assert(foo(false) == @as(i32, 20));412 \\ assert(foo(false) == @as(i32, 20));
395 \\ assert(foo(true) == @as(i32, 30));413 \\ assert(foo(true) == @as(i32, 30));
396 \\}414 \\}
...@@ -407,16 +425,16 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -407,16 +425,16 @@ pub fn addCases(ctx: *TestContext) !void {
407 \\ };425 \\ };
408 \\ return val + 10;426 \\ return val + 10;
409 \\}427 \\}
410 , "");428 , "0\n");
411 }429 }
412430
413 {431 {
414 var case = ctx.exe("wasm while loops", wasi);432 var case = ctx.exe("wasm while loops", wasi);
415433
416 case.addCompareOutput(434 case.addCompareOutput(
417 \\pub export fn _start() u32 {435 \\pub fn main() u8 {
418 \\ var i: u32 = 0;436 \\ var i: u8 = 0;
419 \\ while(i < @as(u32, 5)){437 \\ while(i < @as(u8, 5)){
420 \\ i += 1;438 \\ i += 1;
421 \\ }439 \\ }
422 \\440 \\
...@@ -425,10 +443,10 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -425,10 +443,10 @@ pub fn addCases(ctx: *TestContext) !void {
425 , "5\n");443 , "5\n");
426444
427 case.addCompareOutput(445 case.addCompareOutput(
428 \\pub export fn _start() u32 {446 \\pub fn main() u8 {
429 \\ var i: u32 = 0;447 \\ var i: u8 = 0;
430 \\ while(i < @as(u32, 10)){448 \\ while(i < @as(u8, 10)){
431 \\ var x: u32 = 1;449 \\ var x: u8 = 1;
432 \\ i += x;450 \\ i += x;
433 \\ }451 \\ }
434 \\ return i;452 \\ return i;
...@@ -436,12 +454,12 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -436,12 +454,12 @@ pub fn addCases(ctx: *TestContext) !void {
436 , "10\n");454 , "10\n");
437455
438 case.addCompareOutput(456 case.addCompareOutput(
439 \\pub export fn _start() u32 {457 \\pub fn main() u8 {
440 \\ var i: u32 = 0;458 \\ var i: u8 = 0;
441 \\ while(i < @as(u32, 10)){459 \\ while(i < @as(u8, 10)){
442 \\ var x: u32 = 1;460 \\ var x: u8 = 1;
443 \\ i += x;461 \\ i += x;
444 \\ if (i == @as(u32, 5)) break;462 \\ if (i == @as(u8, 5)) break;
445 \\ }463 \\ }
446 \\ return i;464 \\ return i;
447 \\}465 \\}
...@@ -454,7 +472,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -454,7 +472,7 @@ pub fn addCases(ctx: *TestContext) !void {
454 case.addCompareOutput(472 case.addCompareOutput(
455 \\const Number = enum { One, Two, Three };473 \\const Number = enum { One, Two, Three };
456 \\474 \\
457 \\pub export fn _start() i32 {475 \\pub fn main() void {
458 \\ var number1 = Number.One;476 \\ var number1 = Number.One;
459 \\ var number2: Number = .Two;477 \\ var number2: Number = .Two;
460 \\ if (false) {478 \\ if (false) {
...@@ -462,47 +480,52 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -462,47 +480,52 @@ pub fn addCases(ctx: *TestContext) !void {
462 \\ number2;480 \\ number2;
463 \\ }481 \\ }
464 \\ const number3 = @intToEnum(Number, 2);482 \\ const number3 = @intToEnum(Number, 2);
465 \\483 \\ if (@enumToInt(number3) != 2) {
466 \\ return @enumToInt(number3);484 \\ unreachable;
485 \\ }
486 \\ return;
467 \\}487 \\}
468 , "2\n");488 , "0\n");
469489
470 case.addCompareOutput(490 case.addCompareOutput(
471 \\const Number = enum { One, Two, Three };491 \\const Number = enum { One, Two, Three };
472 \\492 \\
473 \\pub export fn _start() i32 {493 \\pub fn main() void {
474 \\ var number1 = Number.One;494 \\ var number1 = Number.One;
475 \\ var number2: Number = .Two;495 \\ var number2: Number = .Two;
476 \\ const number3 = @intToEnum(Number, 2);496 \\ const number3 = @intToEnum(Number, 2);
477 \\ if (number1 == number2) return 1;497 \\ assert(number1 != number2);
478 \\ if (number2 == number3) return 1;498 \\ assert(number2 != number3);
479 \\ if (@enumToInt(number1) != 0) return 1;499 \\ assert(@enumToInt(number1) == 0);
480 \\ if (@enumToInt(number2) != 1) return 1;500 \\ assert(@enumToInt(number2) == 1);
481 \\ if (@enumToInt(number3) != 2) return 1;501 \\ assert(@enumToInt(number3) == 2);
482 \\ var x: Number = .Two;502 \\ var x: Number = .Two;
483 \\ if (number2 != x) return 1;503 \\ assert(number2 == x);
484 \\504 \\
485 \\ return @enumToInt(number3);505 \\ return;
486 \\}506 \\}
487 , "2\n");507 \\fn assert(val: bool) void {
508 \\ if(!val) unreachable;
509 \\}
510 , "0\n");
488 }511 }
489512
490 {513 {
491 var case = ctx.exe("wasm structs", wasi);514 var case = ctx.exe("wasm structs", wasi);
492515
493 case.addCompareOutput(516 case.addCompareOutput(
494 \\const Example = struct { x: u32 };517 \\const Example = struct { x: u8 };
495 \\518 \\
496 \\pub export fn _start() u32 {519 \\pub fn main() u8 {
497 \\ var example: Example = .{ .x = 5 };520 \\ var example: Example = .{ .x = 5 };
498 \\ return example.x;521 \\ return example.x;
499 \\}522 \\}
500 , "5\n");523 , "5\n");
501524
502 case.addCompareOutput(525 case.addCompareOutput(
503 \\const Example = struct { x: u32 };526 \\const Example = struct { x: u8 };
504 \\527 \\
505 \\pub export fn _start() u32 {528 \\pub fn main() u8 {
506 \\ var example: Example = .{ .x = 5 };529 \\ var example: Example = .{ .x = 5 };
507 \\ example.x = 10;530 \\ example.x = 10;
508 \\ return example.x;531 \\ return example.x;
...@@ -510,18 +533,18 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -510,18 +533,18 @@ pub fn addCases(ctx: *TestContext) !void {
510 , "10\n");533 , "10\n");
511534
512 case.addCompareOutput(535 case.addCompareOutput(
513 \\const Example = struct { x: u32, y: u32 };536 \\const Example = struct { x: u8, y: u8 };
514 \\537 \\
515 \\pub export fn _start() u32 {538 \\pub fn main() u8 {
516 \\ var example: Example = .{ .x = 5, .y = 10 };539 \\ var example: Example = .{ .x = 5, .y = 10 };
517 \\ return example.y + example.x;540 \\ return example.y + example.x;
518 \\}541 \\}
519 , "15\n");542 , "15\n");
520543
521 case.addCompareOutput(544 case.addCompareOutput(
522 \\const Example = struct { x: u32, y: u32 };545 \\const Example = struct { x: u8, y: u8 };
523 \\546 \\
524 \\pub export fn _start() u32 {547 \\pub fn main() u8 {
525 \\ var example: Example = .{ .x = 5, .y = 10 };548 \\ var example: Example = .{ .x = 5, .y = 10 };
526 \\ var example2: Example = .{ .x = 10, .y = 20 };549 \\ var example2: Example = .{ .x = 10, .y = 20 };
527 \\550 \\
...@@ -531,9 +554,9 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -531,9 +554,9 @@ pub fn addCases(ctx: *TestContext) !void {
531 , "30\n");554 , "30\n");
532555
533 case.addCompareOutput(556 case.addCompareOutput(
534 \\const Example = struct { x: u32, y: u32 };557 \\const Example = struct { x: u8, y: u8 };
535 \\558 \\
536 \\pub export fn _start() u32 {559 \\pub fn main() u8 {
537 \\ var example: Example = .{ .x = 5, .y = 10 };560 \\ var example: Example = .{ .x = 5, .y = 10 };
538 \\561 \\
539 \\ example = .{ .x = 10, .y = 20 };562 \\ example = .{ .x = 10, .y = 20 };
...@@ -546,9 +569,9 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -546,9 +569,9 @@ pub fn addCases(ctx: *TestContext) !void {
546 var case = ctx.exe("wasm switch", wasi);569 var case = ctx.exe("wasm switch", wasi);
547570
548 case.addCompareOutput(571 case.addCompareOutput(
549 \\pub export fn _start() u32 {572 \\pub fn main() u8 {
550 \\ var val: u32 = 1;573 \\ var val: u8 = 1;
551 \\ var a: u32 = switch (val) {574 \\ var a: u8 = switch (val) {
552 \\ 0, 1 => 2,575 \\ 0, 1 => 2,
553 \\ 2 => 3,576 \\ 2 => 3,
554 \\ 3 => 4,577 \\ 3 => 4,
...@@ -560,9 +583,9 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -560,9 +583,9 @@ pub fn addCases(ctx: *TestContext) !void {
560 , "2\n");583 , "2\n");
561584
562 case.addCompareOutput(585 case.addCompareOutput(
563 \\pub export fn _start() u32 {586 \\pub fn main() u8 {
564 \\ var val: u32 = 2;587 \\ var val: u8 = 2;
565 \\ var a: u32 = switch (val) {588 \\ var a: u8 = switch (val) {
566 \\ 0, 1 => 2,589 \\ 0, 1 => 2,
567 \\ 2 => 3,590 \\ 2 => 3,
568 \\ 3 => 4,591 \\ 3 => 4,
...@@ -574,9 +597,9 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -574,9 +597,9 @@ pub fn addCases(ctx: *TestContext) !void {
574 , "3\n");597 , "3\n");
575598
576 case.addCompareOutput(599 case.addCompareOutput(
577 \\pub export fn _start() u32 {600 \\pub fn main() u8 {
578 \\ var val: u32 = 10;601 \\ var val: u8 = 10;
579 \\ var a: u32 = switch (val) {602 \\ var a: u8 = switch (val) {
580 \\ 0, 1 => 2,603 \\ 0, 1 => 2,
581 \\ 2 => 3,604 \\ 2 => 3,
582 \\ 3 => 4,605 \\ 3 => 4,
...@@ -590,9 +613,9 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -590,9 +613,9 @@ pub fn addCases(ctx: *TestContext) !void {
590 case.addCompareOutput(613 case.addCompareOutput(
591 \\const MyEnum = enum { One, Two, Three };614 \\const MyEnum = enum { One, Two, Three };
592 \\615 \\
593 \\pub export fn _start() u32 {616 \\pub fn main() u8 {
594 \\ var val: MyEnum = .Two;617 \\ var val: MyEnum = .Two;
595 \\ var a: u32 = switch (val) {618 \\ var a: u8 = switch (val) {
596 \\ .One => 1,619 \\ .One => 1,
597 \\ .Two => 2,620 \\ .Two => 2,
598 \\ .Three => 3,621 \\ .Three => 3,
...@@ -607,7 +630,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -607,7 +630,7 @@ pub fn addCases(ctx: *TestContext) !void {
607 var case = ctx.exe("wasm error unions", wasi);630 var case = ctx.exe("wasm error unions", wasi);
608631
609 case.addCompareOutput(632 case.addCompareOutput(
610 \\pub export fn _start() void {633 \\pub fn main() void {
611 \\ var e1 = error.Foo;634 \\ var e1 = error.Foo;
612 \\ var e2 = error.Bar;635 \\ var e2 = error.Bar;
613 \\ assert(e1 != e2);636 \\ assert(e1 != e2);
...@@ -618,32 +641,32 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -618,32 +641,32 @@ pub fn addCases(ctx: *TestContext) !void {
618 \\fn assert(b: bool) void {641 \\fn assert(b: bool) void {
619 \\ if (!b) unreachable;642 \\ if (!b) unreachable;
620 \\}643 \\}
621 , "");644 , "0\n");
622645
623 case.addCompareOutput(646 case.addCompareOutput(
624 \\pub export fn _start() u32 {647 \\pub fn main() u8 {
625 \\ var e: anyerror!u32 = 5;648 \\ var e: anyerror!u8 = 5;
626 \\ const i = e catch 10;649 \\ const i = e catch 10;
627 \\ return i;650 \\ return i;
628 \\}651 \\}
629 , "5\n");652 , "5\n");
630653
631 case.addCompareOutput(654 case.addCompareOutput(
632 \\pub export fn _start() u32 {655 \\pub fn main() u8 {
633 \\ var e: anyerror!u32 = error.Foo;656 \\ var e: anyerror!u8 = error.Foo;
634 \\ const i = e catch 10;657 \\ const i = e catch 10;
635 \\ return i;658 \\ return i;
636 \\}659 \\}
637 , "10\n");660 , "10\n");
638661
639 case.addCompareOutput(662 case.addCompareOutput(
640 \\pub export fn _start() u32 {663 \\pub fn main() u8 {
641 \\ var e = foo();664 \\ var e = foo();
642 \\ const i = e catch 69;665 \\ const i = e catch 69;
643 \\ return i;666 \\ return i;
644 \\}667 \\}
645 \\668 \\
646 \\fn foo() anyerror!u32 {669 \\fn foo() anyerror!u8 {
647 \\ return 5;670 \\ return 5;
648 \\}671 \\}
649 , "5\n");672 , "5\n");
...@@ -653,24 +676,24 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -653,24 +676,24 @@ pub fn addCases(ctx: *TestContext) !void {
653 var case = ctx.exe("wasm error union part 2", wasi);676 var case = ctx.exe("wasm error union part 2", wasi);
654677
655 case.addCompareOutput(678 case.addCompareOutput(
656 \\pub export fn _start() u32 {679 \\pub fn main() u8 {
657 \\ var e = foo();680 \\ var e = foo();
658 \\ const i = e catch 69;681 \\ const i = e catch 69;
659 \\ return i;682 \\ return i;
660 \\}683 \\}
661 \\684 \\
662 \\fn foo() anyerror!u32 {685 \\fn foo() anyerror!u8 {
663 \\ return error.Bruh;686 \\ return error.Bruh;
664 \\}687 \\}
665 , "69\n");688 , "69\n");
666 case.addCompareOutput(689 case.addCompareOutput(
667 \\pub export fn _start() u32 {690 \\pub fn main() u8 {
668 \\ var e = foo();691 \\ var e = foo();
669 \\ const i = e catch 42;692 \\ const i = e catch 42;
670 \\ return i;693 \\ return i;
671 \\}694 \\}
672 \\695 \\
673 \\fn foo() anyerror!u32 {696 \\fn foo() anyerror!u8 {
674 \\ return error.Dab;697 \\ return error.Dab;
675 \\}698 \\}
676 , "42\n");699 , "42\n");
...@@ -680,20 +703,22 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -680,20 +703,22 @@ pub fn addCases(ctx: *TestContext) !void {
680 var case = ctx.exe("wasm integer widening", wasi);703 var case = ctx.exe("wasm integer widening", wasi);
681704
682 case.addCompareOutput(705 case.addCompareOutput(
683 \\pub export fn _start() u64 {706 \\pub fn main() void{
684 \\ var x: u32 = 5;707 \\ var x: u8 = 5;
685 \\ return x;708 \\ var y: u64 = x;
709 \\ _ = y;
710 \\ return;
686 \\}711 \\}
687 , "5\n");712 , "0\n");
688 }713 }
689714
690 {715 {
691 var case = ctx.exe("wasm optionals", wasi);716 var case = ctx.exe("wasm optionals", wasi);
692717
693 case.addCompareOutput(718 case.addCompareOutput(
694 \\pub export fn _start() u32 {719 \\pub fn main() u8 {
695 \\ var x: ?u32 = 5;720 \\ var x: ?u8 = 5;
696 \\ var y: u32 = 0;721 \\ var y: u8 = 0;
697 \\ if (x) |val| {722 \\ if (x) |val| {
698 \\ y = val;723 \\ y = val;
699 \\ }724 \\ }
...@@ -702,9 +727,9 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -702,9 +727,9 @@ pub fn addCases(ctx: *TestContext) !void {
702 , "5\n");727 , "5\n");
703728
704 case.addCompareOutput(729 case.addCompareOutput(
705 \\pub export fn _start() u32 {730 \\pub fn main() u8 {
706 \\ var x: ?u32 = null;731 \\ var x: ?u8 = null;
707 \\ var y: u32 = 0;732 \\ var y: u8 = 0;
708 \\ if (x) |val| {733 \\ if (x) |val| {
709 \\ y = val;734 \\ y = val;
710 \\ }735 \\ }
...@@ -713,23 +738,23 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -713,23 +738,23 @@ pub fn addCases(ctx: *TestContext) !void {
713 , "0\n");738 , "0\n");
714739
715 case.addCompareOutput(740 case.addCompareOutput(
716 \\pub export fn _start() u32 {741 \\pub fn main() u8 {
717 \\ var x: ?u32 = 5;742 \\ var x: ?u8 = 5;
718 \\ return x.?;743 \\ return x.?;
719 \\}744 \\}
720 , "5\n");745 , "5\n");
721746
722 case.addCompareOutput(747 case.addCompareOutput(
723 \\pub export fn _start() u32 {748 \\pub fn main() u8 {
724 \\ var x: u32 = 5;749 \\ var x: u8 = 5;
725 \\ var y: ?u32 = x;750 \\ var y: ?u8 = x;
726 \\ return y.?;751 \\ return y.?;
727 \\}752 \\}
728 , "5\n");753 , "5\n");
729754
730 case.addCompareOutput(755 case.addCompareOutput(
731 \\pub export fn _start() u32 {756 \\pub fn main() u8 {
732 \\ var val: ?u32 = 5;757 \\ var val: ?u8 = 5;
733 \\ while (val) |*v| {758 \\ while (val) |*v| {
734 \\ v.* -= 1;759 \\ v.* -= 1;
735 \\ if (v.* == 2) {760 \\ if (v.* == 2) {
...@@ -745,32 +770,32 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -745,32 +770,32 @@ pub fn addCases(ctx: *TestContext) !void {
745 var case = ctx.exe("wasm pointers", wasi);770 var case = ctx.exe("wasm pointers", wasi);
746771
747 case.addCompareOutput(772 case.addCompareOutput(
748 \\pub export fn _start() u32 {773 \\pub fn main() u8 {
749 \\ var x: u32 = 0;774 \\ var x: u8 = 0;
750 \\775 \\
751 \\ foo(&x);776 \\ foo(&x);
752 \\ return x;777 \\ return x;
753 \\}778 \\}
754 \\779 \\
755 \\fn foo(x: *u32)void {780 \\fn foo(x: *u8)void {
756 \\ x.* = 2;781 \\ x.* = 2;
757 \\}782 \\}
758 , "2\n");783 , "2\n");
759784
760 case.addCompareOutput(785 case.addCompareOutput(
761 \\pub export fn _start() u32 {786 \\pub fn main() u8 {
762 \\ var x: u32 = 0;787 \\ var x: u8 = 0;
763 \\788 \\
764 \\ foo(&x);789 \\ foo(&x);
765 \\ bar(&x);790 \\ bar(&x);
766 \\ return x;791 \\ return x;
767 \\}792 \\}
768 \\793 \\
769 \\fn foo(x: *u32)void {794 \\fn foo(x: *u8)void {
770 \\ x.* = 2;795 \\ x.* = 2;
771 \\}796 \\}
772 \\797 \\
773 \\fn bar(x: *u32) void {798 \\fn bar(x: *u8) void {
774 \\ x.* += 2;799 \\ x.* += 2;
775 \\}800 \\}
776 , "4\n");801 , "4\n");