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