1 // Copyright 2015 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 // Flags: --expose-wasm 6 7 function bytes() { 8 var buffer = new ArrayBuffer(arguments.length); 9 var view = new Uint8Array(buffer); 10 for (var i = 0; i < arguments.length; i++) { 11 var val = arguments[i]; 12 if ((typeof val) == "string") val = val.charCodeAt(0); 13 view[i] = val | 0; 14 } 15 return buffer; 16 } 17 18 // Header declaration constants 19 var kWasmH0 = 0; 20 var kWasmH1 = 0x61; 21 var kWasmH2 = 0x73; 22 var kWasmH3 = 0x6d; 23 24 var kWasmV0 = 11; 25 var kWasmV1 = 0; 26 var kWasmV2 = 0; 27 var kWasmV3 = 0; 28 29 var kHeaderSize = 8; 30 var kPageSize = 65536; 31 32 function bytesWithHeader() { 33 var buffer = new ArrayBuffer(kHeaderSize + arguments.length); 34 var view = new Uint8Array(buffer); 35 view[0] = kWasmH0; 36 view[1] = kWasmH1; 37 view[2] = kWasmH2; 38 view[3] = kWasmH3; 39 view[4] = kWasmV0; 40 view[5] = kWasmV1; 41 view[6] = kWasmV2; 42 view[7] = kWasmV3; 43 for (var i = 0; i < arguments.length; i++) { 44 var val = arguments[i]; 45 if ((typeof val) == "string") val = val.charCodeAt(0); 46 view[kHeaderSize + i] = val | 0; 47 } 48 return buffer; 49 } 50 51 var kDeclNoLocals = 0; 52 53 // Section declaration constants 54 var kDeclMemory = 0x00; 55 var kDeclTypes = 0x01; 56 var kDeclFunctions = 0x02; 57 var kDeclGlobals = 0x03; 58 var kDeclData = 0x04; 59 var kDeclTable = 0x05; 60 var kDeclEnd = 0x06; 61 var kDeclStart = 0x07; 62 var kDeclImports = 0x08; 63 var kDeclExports = 0x09; 64 var kDeclFunctions = 0x0a; 65 var kDeclCode = 0x0b; 66 var kDeclNames = 0x0c; 67 var kDeclFunctionTablePad = 0x0d; 68 69 var kArity0 = 0; 70 var kArity1 = 1; 71 var kArity2 = 2; 72 var kArity3 = 3; 73 var kWasmFunctionTypeForm = 0x40; 74 75 var section_names = [ 76 "memory", "type", "old_function", "global", "data", 77 "table", "end", "start", "import", "export", 78 "function", "code", "name", "table_pad"]; 79 80 // Function declaration flags 81 var kDeclFunctionName = 0x01; 82 var kDeclFunctionImport = 0x02; 83 var kDeclFunctionLocals = 0x04; 84 var kDeclFunctionExport = 0x08; 85 86 // Local types 87 var kAstStmt = 0; 88 var kAstI32 = 1; 89 var kAstI64 = 2; 90 var kAstF32 = 3; 91 var kAstF64 = 4; 92 93 // Useful signatures 94 var kSig_i = makeSig([], [kAstI32]); 95 var kSig_d = makeSig([], [kAstF64]); 96 var kSig_i_i = makeSig([kAstI32], [kAstI32]); 97 var kSig_i_ii = makeSig([kAstI32, kAstI32], [kAstI32]); 98 var kSig_i_iii = makeSig([kAstI32, kAstI32, kAstI32], [kAstI32]); 99 var kSig_d_dd = makeSig([kAstF64, kAstF64], [kAstF64]); 100 var kSig_l_ll = makeSig([kAstI64, kAstI64], [kAstI64]); 101 var kSig_i_dd = makeSig([kAstF64, kAstF64], [kAstI32]); 102 var kSig_v_v = makeSig([], []); 103 var kSig_i_v = makeSig([], [kAstI32]); 104 105 function makeSig(params, results) { 106 return {params: params, results: results}; 107 } 108 109 function makeSig_v_x(x) { 110 return makeSig([x], []); 111 } 112 113 function makeSig_v_xx(x) { 114 return makeSig([x, x], []); 115 } 116 117 function makeSig_r_v(r) { 118 return makeSig([], [r]); 119 } 120 121 function makeSig_r_x(r, x) { 122 return makeSig([x], [r]); 123 } 124 125 function makeSig_r_xx(r, x) { 126 return makeSig([x, x], [r]); 127 } 128 129 // Opcodes 130 var kExprNop = 0x00; 131 var kExprBlock = 0x01; 132 var kExprLoop = 0x02; 133 var kExprIf = 0x03; 134 var kExprElse = 0x04; 135 var kExprSelect = 0x05; 136 var kExprBr = 0x06; 137 var kExprBrIf = 0x07; 138 var kExprBrTable = 0x08; 139 var kExprReturn = 0x09; 140 var kExprUnreachable = 0x0a; 141 var kExprEnd = 0x0f; 142 143 var kExprI32Const = 0x10; 144 var kExprI64Const = 0x11; 145 var kExprF64Const = 0x12; 146 var kExprF32Const = 0x13; 147 var kExprGetLocal = 0x14; 148 var kExprSetLocal = 0x15; 149 var kExprCallFunction = 0x16; 150 var kExprCallIndirect = 0x17; 151 var kExprCallImport = 0x18; 152 var kExprI8Const = 0xcb; 153 var kExprLoadGlobal = 0xcc; 154 var kExprStoreGlobal = 0xcd; 155 156 var kExprI32LoadMem8S = 0x20; 157 var kExprI32LoadMem8U = 0x21; 158 var kExprI32LoadMem16S = 0x22; 159 var kExprI32LoadMem16U = 0x23; 160 var kExprI64LoadMem8S = 0x24; 161 var kExprI64LoadMem8U = 0x25; 162 var kExprI64LoadMem16S = 0x26; 163 var kExprI64LoadMem16U = 0x27; 164 var kExprI64LoadMem32S = 0x28; 165 var kExprI64LoadMem32U = 0x29; 166 var kExprI32LoadMem = 0x2a; 167 var kExprI64LoadMem = 0x2b; 168 var kExprF32LoadMem = 0x2c; 169 var kExprF64LoadMem = 0x2d; 170 171 var kExprI32StoreMem8 = 0x2e; 172 var kExprI32StoreMem16 = 0x2f; 173 var kExprI64StoreMem8 = 0x30; 174 var kExprI64StoreMem16 = 0x31; 175 var kExprI64StoreMem32 = 0x32; 176 var kExprI32StoreMem = 0x33; 177 var kExprI64StoreMem = 0x34; 178 var kExprF32StoreMem = 0x35; 179 var kExprF64StoreMem = 0x36; 180 181 var kExprMemorySize = 0x3b; 182 var kExprGrowMemory = 0x39; 183 184 var kExprI32Add = 0x40; 185 var kExprI32Sub = 0x41; 186 var kExprI32Mul = 0x42; 187 var kExprI32DivS = 0x43; 188 var kExprI32DivU = 0x44; 189 var kExprI32RemS = 0x45; 190 var kExprI32RemU = 0x46; 191 var kExprI32And = 0x47; 192 var kExprI32Ior = 0x48; 193 var kExprI32Xor = 0x49; 194 var kExprI32Shl = 0x4a; 195 var kExprI32ShrU = 0x4b; 196 var kExprI32ShrS = 0x4c; 197 var kExprI32Eq = 0x4d; 198 var kExprI32Ne = 0x4e; 199 var kExprI32LtS = 0x4f; 200 var kExprI32LeS = 0x50; 201 var kExprI32LtU = 0x51; 202 var kExprI32LeU = 0x52; 203 var kExprI32GtS = 0x53; 204 var kExprI32GeS = 0x54; 205 var kExprI32GtU = 0x55; 206 var kExprI32GeU = 0x56; 207 var kExprI32Clz = 0x57; 208 var kExprI32Ctz = 0x58; 209 var kExprI32Popcnt = 0x59; 210 var kExprI32Eqz = 0x5a; 211 var kExprI64Add = 0x5b; 212 var kExprI64Sub = 0x5c; 213 var kExprI64Mul = 0x5d; 214 var kExprI64DivS = 0x5e; 215 var kExprI64DivU = 0x5f; 216 var kExprI64RemS = 0x60; 217 var kExprI64RemU = 0x61; 218 var kExprI64And = 0x62; 219 var kExprI64Ior = 0x63; 220 var kExprI64Xor = 0x64; 221 var kExprI64Shl = 0x65; 222 var kExprI64ShrU = 0x66; 223 var kExprI64ShrS = 0x67; 224 var kExprI64Eq = 0x68; 225 var kExprI64Ne = 0x69; 226 var kExprI64LtS = 0x6a; 227 var kExprI64LeS = 0x6b; 228 var kExprI64LtU = 0x6c; 229 var kExprI64LeU = 0x6d; 230 var kExprI64GtS = 0x6e; 231 var kExprI64GeS = 0x6f; 232 var kExprI64GtU = 0x70; 233 var kExprI64GeU = 0x71; 234 var kExprI64Clz = 0x72; 235 var kExprI64Ctz = 0x73; 236 var kExprI64Popcnt = 0x74; 237 var kExprF32Add = 0x75; 238 var kExprF32Sub = 0x76; 239 var kExprF32Mul = 0x77; 240 var kExprF32Div = 0x78; 241 var kExprF32Min = 0x79; 242 var kExprF32Max = 0x7a; 243 var kExprF32Abs = 0x7b; 244 var kExprF32Neg = 0x7c; 245 var kExprF32CopySign = 0x7d; 246 var kExprF32Ceil = 0x7e; 247 var kExprF32Floor = 0x7f; 248 var kExprF32Trunc = 0x80; 249 var kExprF32NearestInt = 0x81; 250 var kExprF32Sqrt = 0x82; 251 var kExprF32Eq = 0x83; 252 var kExprF32Ne = 0x84; 253 var kExprF32Lt = 0x85; 254 var kExprF32Le = 0x86; 255 var kExprF32Gt = 0x87; 256 var kExprF32Ge = 0x88; 257 var kExprF64Add = 0x89; 258 var kExprF64Sub = 0x8a; 259 var kExprF64Mul = 0x8b; 260 var kExprF64Div = 0x8c; 261 var kExprF64Min = 0x8d; 262 var kExprF64Max = 0x8e; 263 var kExprF64Abs = 0x8f; 264 var kExprF64Neg = 0x90; 265 var kExprF64CopySign = 0x91; 266 var kExprF64Ceil = 0x92; 267 var kExprF64Floor = 0x93; 268 var kExprF64Trunc = 0x94; 269 var kExprF64NearestInt = 0x95; 270 var kExprF64Sqrt = 0x96; 271 var kExprF64Eq = 0x97; 272 var kExprF64Ne = 0x98; 273 var kExprF64Lt = 0x99; 274 var kExprF64Le = 0x9a; 275 var kExprF64Gt = 0x9b; 276 var kExprF64Ge = 0x9c; 277 var kExprI32SConvertF32 = 0x9d; 278 var kExprI32SConvertF64 = 0x9e; 279 var kExprI32UConvertF32 = 0x9f; 280 var kExprI32UConvertF64 = 0xa0; 281 var kExprI32ConvertI64 = 0xa1; 282 var kExprI64SConvertF32 = 0xa2; 283 var kExprI64SConvertF64 = 0xa3; 284 var kExprI64UConvertF32 = 0xa4; 285 var kExprI64UConvertF64 = 0xa5; 286 var kExprI64SConvertI32 = 0xa6; 287 var kExprI64UConvertI32 = 0xa7; 288 var kExprF32SConvertI32 = 0xa8; 289 var kExprF32UConvertI32 = 0xa9; 290 var kExprF32SConvertI64 = 0xaa; 291 var kExprF32UConvertI64 = 0xab; 292 var kExprF32ConvertF64 = 0xac; 293 var kExprF32ReinterpretI32 = 0xad; 294 var kExprF64SConvertI32 = 0xae; 295 var kExprF64UConvertI32 = 0xaf; 296 var kExprF64SConvertI64 = 0xb0; 297 var kExprF64UConvertI64 = 0xb1; 298 var kExprF64ConvertF32 = 0xb2; 299 var kExprF64ReinterpretI64 = 0xb3; 300 var kExprI32ReinterpretF32 = 0xb4; 301 var kExprI64ReinterpretF64 = 0xb5; 302 var kExprI32Ror = 0xb6; 303 var kExprI32Rol = 0xb7; 304 var kExprI64Ror = 0xb8; 305 var kExprI64Rol = 0xb9; 306 307 var kTrapUnreachable = 0; 308 var kTrapMemOutOfBounds = 1; 309 var kTrapDivByZero = 2; 310 var kTrapDivUnrepresentable = 3; 311 var kTrapRemByZero = 4; 312 var kTrapFloatUnrepresentable = 5; 313 var kTrapFuncInvalid = 6; 314 var kTrapFuncSigMismatch = 7; 315 316 var kTrapMsgs = [ 317 "unreachable", 318 "memory access out of bounds", 319 "divide by zero", 320 "divide result unrepresentable", 321 "remainder by zero", 322 "integer result unrepresentable", 323 "invalid function", 324 "function signature mismatch" 325 ]; 326 327 function assertTraps(trap, code) { 328 var threwException = true; 329 try { 330 if (typeof code === 'function') { 331 code(); 332 } else { 333 eval(code); 334 } 335 threwException = false; 336 } catch (e) { 337 assertEquals("object", typeof e); 338 assertEquals(kTrapMsgs[trap], e.message); 339 // Success. 340 return; 341 } 342 throw new MjsUnitAssertionError("Did not trap, expected: " + kTrapMsgs[trap]); 343 } 344