wasm-module-builder.js 17.2 KB
Newer Older
1 2 3 4
// Copyright 2016 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6 7 8 9 10
// Used for encoding f32 and double constants to bits.
let __buffer = new ArrayBuffer(8);
let byte_view = new Int8Array(__buffer);
let f32_view = new Float32Array(__buffer);
let f64_view = new Float64Array(__buffer);

rossberg's avatar
rossberg committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
class Binary extends Array {
  emit_u8(val) {
    this.push(val);
  }

  emit_u16(val) {
    this.push(val & 0xff);
    this.push((val >> 8) & 0xff);
  }

  emit_u32(val) {
    this.push(val & 0xff);
    this.push((val >> 8) & 0xff);
    this.push((val >> 16) & 0xff);
    this.push((val >> 24) & 0xff);
  }

28
  emit_u32v(val) {
rossberg's avatar
rossberg committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    while (true) {
      let v = val & 0xff;
      val = val >>> 7;
      if (val == 0) {
        this.push(v);
        break;
      }
      this.push(v | 0x80);
    }
  }

  emit_bytes(data) {
    for (let i = 0; i < data.length; i++) {
      this.push(data[i] & 0xff);
    }
  }

  emit_string(string) {
    // When testing illegal names, we pass a byte array directly.
    if (string instanceof Array) {
49
      this.emit_u32v(string.length);
rossberg's avatar
rossberg committed
50 51 52 53 54 55 56
      this.emit_bytes(string);
      return;
    }

    // This is the hacky way to convert a JavaScript string to a UTF8 encoded
    // string only containing single-byte characters.
    let string_utf8 = unescape(encodeURIComponent(string));
57
    this.emit_u32v(string_utf8.length);
rossberg's avatar
rossberg committed
58 59 60 61 62 63 64 65 66 67 68 69
    for (let i = 0; i < string_utf8.length; i++) {
      this.emit_u8(string_utf8.charCodeAt(i));
    }
  }

  emit_header() {
    this.push(kWasmH0, kWasmH1, kWasmH2, kWasmH3,
              kWasmV0, kWasmV1, kWasmV2, kWasmV3);
  }

  emit_section(section_code, content_generator) {
    // Emit section name.
70
    this.emit_u8(section_code);
rossberg's avatar
rossberg committed
71 72 73 74
    // Emit the section to a temporary buffer: its full length isn't know yet.
    let section = new Binary;
    content_generator(section);
    // Emit section length.
75
    this.emit_u32v(section.length);
rossberg's avatar
rossberg committed
76 77 78 79 80 81
    // Copy the temporary buffer.
    this.push(...section);
  }
}

class WasmFunctionBuilder {
82 83
  constructor(module, name, type_index) {
    this.module = module;
84
    this.name = name;
rossberg's avatar
rossberg committed
85
    this.type_index = type_index;
86
    this.body = [];
rossberg's avatar
rossberg committed
87
  }
88

rossberg's avatar
rossberg committed
89
  exportAs(name) {
90
    this.module.addExport(name, this.index);
91
    return this;
rossberg's avatar
rossberg committed
92
  }
93

rossberg's avatar
rossberg committed
94
  exportFunc() {
95
    this.exportAs(this.name);
rossberg's avatar
rossberg committed
96 97
    return this;
  }
98

rossberg's avatar
rossberg committed
99
  addBody(body) {
100 101 102
    for (let b of body) {
      if (typeof b != 'number') throw new Error("invalid body");
    }
103
    this.body = body;
104 105
    // Automatically add the end for the function block to the body.
    body.push(kExprEnd);
106
    return this;
rossberg's avatar
rossberg committed
107
  }
108

109 110 111 112 113
  addBodyWithEnd(body) {
    this.body = body;
    return this;
  }

rossberg's avatar
rossberg committed
114
  addLocals(locals) {
115 116
    this.locals = locals;
    return this;
rossberg's avatar
rossberg committed
117
  }
118 119 120 121

  end() {
    return this.module;
  }
122 123
}

124 125 126 127 128 129 130 131 132
class WasmGlobalBuilder {
  constructor(module, type, mutable) {
    this.module = module;
    this.type = type;
    this.mutable = mutable;
    this.init = 0;
  }

  exportAs(name) {
133 134
    this.module.exports.push({name: name, kind: kExternalGlobal,
                              index: this.index});
135 136 137 138
    return this;
  }
}

rossberg's avatar
rossberg committed
139 140 141
class WasmModuleBuilder {
  constructor() {
    this.types = [];
142
    this.imports = [];
143
    this.exports = [];
144
    this.globals = [];
145
    this.functions = [];
146 147 148
    this.function_table = [];
    this.function_table_length = 0;
    this.function_table_inits = [];
rossberg's avatar
rossberg committed
149
    this.segments = [];
150
    this.explicit = [];
151 152
    this.num_imported_funcs = 0;
    this.num_imported_globals = 0;
153
    return this;
rossberg's avatar
rossberg committed
154
  }
155

rossberg's avatar
rossberg committed
156
  addStart(start_index) {
157
    this.start_index = start_index;
158
    return this;
rossberg's avatar
rossberg committed
159
  }
160

rossberg's avatar
rossberg committed
161
  addMemory(min, max, exp) {
162 163
    this.memory = {min: min, max: max, exp: exp};
    return this;
rossberg's avatar
rossberg committed
164
  }
165

rossberg's avatar
rossberg committed
166 167 168 169
  addExplicitSection(bytes) {
    this.explicit.push(bytes);
    return this;
  }
170

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
  stringToBytes(name) {
    var result = new Binary();
    result.emit_u32v(name.length);
    for (var i = 0; i < name.length; i++) {
      result.emit_u8(name.charCodeAt(i));
    }
    return result;
  }

  addCustomSection(name, bytes) {
    name = this.stringToBytes(name);
    var length = new Binary();
    length.emit_u32v(name.length + bytes.length);
    this.explicit.push([0, ...length, ...name, ...bytes]);
  }

rossberg's avatar
rossberg committed
187 188 189 190 191
  addType(type) {
    // TODO: canonicalize types?
    this.types.push(type);
    return this.types.length - 1;
  }
192

193 194 195 196 197
  addGlobal(local_type, mutable) {
    let glob = new WasmGlobalBuilder(this, local_type, mutable);
    glob.index = this.globals.length + this.num_imported_globals;
    this.globals.push(glob);
    return glob;
198 199
  }

rossberg's avatar
rossberg committed
200 201
  addFunction(name, type) {
    let type_index = (typeof type) == "number" ? type : this.addType(type);
202 203
    let func = new WasmFunctionBuilder(this, name, type_index);
    func.index = this.functions.length + this.num_imported_funcs;
204 205
    this.functions.push(func);
    return func;
rossberg's avatar
rossberg committed
206
  }
207

208
  addImport(module = "", name, type) {
rossberg's avatar
rossberg committed
209
    let type_index = (typeof type) == "number" ? type : this.addType(type);
210 211 212
    this.imports.push({module: module, name: name, kind: kExternalFunction,
                       type: type_index});
    return this.num_imported_funcs++;
rossberg's avatar
rossberg committed
213
  }
214

215
  addImportedGlobal(module = "", name, type) {
216 217 218 219 220 221
    let o = {module: module, name: name, kind: kExternalGlobal, type: type,
             mutable: false}
    this.imports.push(o);
    return this.num_imported_globals++;
  }

222
  addImportedMemory(module = "", name, initial = 0, maximum) {
223 224
    let o = {module: module, name: name, kind: kExternalMemory,
             initial: initial, maximum: maximum};
225
    this.imports.push(o);
226
    return this;
227 228
  }

229
  addImportedTable(module = "", name, initial, maximum) {
230 231 232 233 234
    let o = {module: module, name: name, kind: kExternalTable, initial: initial,
             maximum: maximum};
    this.imports.push(o);
  }

235 236 237 238 239
  addExport(name, index) {
    this.exports.push({name: name, kind: kExternalFunction, index: index});
    return this;
  }

240 241 242 243 244
  addExportOfKind(name, kind, index) {
    this.exports.push({name: name, kind: kind, index: index});
    return this;
  }

245 246
  addDataSegment(addr, data, is_global = false) {
    this.segments.push({addr: addr, data: data, is_global: is_global});
rossberg's avatar
rossberg committed
247 248
    return this.segments.length - 1;
  }
249

250 251 252 253
  exportMemoryAs(name) {
    this.exports.push({name: name, kind: kExternalMemory, index: 0});
  }

254
  addFunctionTableInit(base, is_global, array, is_import = false) {
255 256
    this.function_table_inits.push({base: base, is_global: is_global,
                                    array: array});
257 258
    if (!is_global) {
      var length = base + array.length;
259
      if (length > this.function_table_length && !is_import) {
260 261
        this.function_table_length = length;
      }
262 263 264 265
    }
    return this;
  }

rossberg's avatar
rossberg committed
266
  appendToTable(array) {
267 268 269 270 271
    return this.addFunctionTableInit(this.function_table.length, false, array);
  }

  setFunctionTableLength(length) {
    this.function_table_length = length;
272
    return this;
273 274
  }

275
  toArray(debug = false) {
rossberg's avatar
rossberg committed
276 277 278 279 280 281 282 283 284
    let binary = new Binary;
    let wasm = this;

    // Add header
    binary.emit_header();

    // Add type section
    if (wasm.types.length > 0) {
      if (debug) print("emitting types @ " + binary.length);
285
      binary.emit_section(kTypeSectionCode, section => {
286
        section.emit_u32v(wasm.types.length);
rossberg's avatar
rossberg committed
287 288
        for (let type of wasm.types) {
          section.emit_u8(kWasmFunctionTypeForm);
289
          section.emit_u32v(type.params.length);
rossberg's avatar
rossberg committed
290 291 292
          for (let param of type.params) {
            section.emit_u8(param);
          }
293
          section.emit_u32v(type.results.length);
rossberg's avatar
rossberg committed
294 295 296 297 298
          for (let result of type.results) {
            section.emit_u8(result);
          }
        }
      });
299 300 301
    }

    // Add imports section
302
    if (wasm.imports.length > 0) {
rossberg's avatar
rossberg committed
303
      if (debug) print("emitting imports @ " + binary.length);
304
      binary.emit_section(kImportSectionCode, section => {
305
        section.emit_u32v(wasm.imports.length);
rossberg's avatar
rossberg committed
306 307 308
        for (let imp of wasm.imports) {
          section.emit_string(imp.module);
          section.emit_string(imp.name || '');
309 310 311 312 313 314
          section.emit_u8(imp.kind);
          if (imp.kind == kExternalFunction) {
            section.emit_u32v(imp.type);
          } else if (imp.kind == kExternalGlobal) {
            section.emit_u32v(imp.type);
            section.emit_u8(imp.mutable);
315 316 317 318 319
          } else if (imp.kind == kExternalMemory) {
            var has_max = (typeof imp.maximum) != "undefined";
            section.emit_u8(has_max ? 1 : 0); // flags
            section.emit_u32v(imp.initial); // initial
            if (has_max) section.emit_u32v(imp.maximum); // maximum
320 321 322 323 324 325
          } else if (imp.kind == kExternalTable) {
            section.emit_u8(kWasmAnyFunctionTypeForm);
            var has_max = (typeof imp.maximum) != "undefined";
            section.emit_u8(has_max ? 1 : 0); // flags
            section.emit_u32v(imp.initial); // initial
            if (has_max) section.emit_u32v(imp.maximum); // maximum
326 327 328
          } else {
            throw new Error("unknown/unsupported import kind " + imp.kind);
          }
rossberg's avatar
rossberg committed
329 330
        }
      });
331 332
    }

333
    // Add functions declarations
rossberg's avatar
rossberg committed
334 335
    let has_names = false;
    let names = false;
336
    if (wasm.functions.length > 0) {
rossberg's avatar
rossberg committed
337
      if (debug) print("emitting function decls @ " + binary.length);
338
      binary.emit_section(kFunctionSectionCode, section => {
339
        section.emit_u32v(wasm.functions.length);
rossberg's avatar
rossberg committed
340 341 342
        for (let func of wasm.functions) {
          has_names = has_names || (func.name != undefined &&
                                   func.name.length > 0);
343
          section.emit_u32v(func.type_index);
rossberg's avatar
rossberg committed
344 345
        }
      });
346 347
    }

348 349
    // Add function_table.
    if (wasm.function_table_length > 0) {
rossberg's avatar
rossberg committed
350
      if (debug) print("emitting table @ " + binary.length);
351 352 353 354
      binary.emit_section(kTableSectionCode, section => {
        section.emit_u8(1);  // one table entry
        section.emit_u8(kWasmAnyFunctionTypeForm);
        section.emit_u8(1);
355 356
        section.emit_u32v(wasm.function_table_length);
        section.emit_u32v(wasm.function_table_length);
rossberg's avatar
rossberg committed
357
      });
358 359 360 361
    }

    // Add memory section
    if (wasm.memory != undefined) {
rossberg's avatar
rossberg committed
362
      if (debug) print("emitting memory @ " + binary.length);
363 364
      binary.emit_section(kMemorySectionCode, section => {
        section.emit_u8(1);  // one memory entry
365 366 367
        section.emit_u32v(kResizableMaximumFlag);
        section.emit_u32v(wasm.memory.min);
        section.emit_u32v(wasm.memory.max);
rossberg's avatar
rossberg committed
368
      });
369 370
    }

371 372 373 374
    // Add global section.
    if (wasm.globals.length > 0) {
      if (debug) print ("emitting globals @ " + binary.length);
      binary.emit_section(kGlobalSectionCode, section => {
375 376 377 378 379 380 381
        section.emit_u32v(wasm.globals.length);
        for (let global of wasm.globals) {
          section.emit_u8(global.type);
          section.emit_u8(global.mutable);
          if ((typeof global.init_index) == "undefined") {
            // Emit a constant initializer.
            switch (global.type) {
382
            case kWasmI32:
383
              section.emit_u8(kExprI32Const);
384
              section.emit_u32v(global.init);
385
              break;
386
            case kWasmI64:
387
              section.emit_u8(kExprI64Const);
388
              section.emit_u32v(global.init);
389
              break;
390
            case kWasmF32:
391
              section.emit_u8(kExprF32Const);
392 393 394 395 396
              f32_view[0] = global.init;
              section.emit_u8(byte_view[0]);
              section.emit_u8(byte_view[1]);
              section.emit_u8(byte_view[2]);
              section.emit_u8(byte_view[3]);
397
              break;
398
            case kWasmF64:
399 400 401 402 403 404 405 406 407 408
              section.emit_u8(kExprF64Const);
              f64_view[0] = global.init;
              section.emit_u8(byte_view[0]);
              section.emit_u8(byte_view[1]);
              section.emit_u8(byte_view[2]);
              section.emit_u8(byte_view[3]);
              section.emit_u8(byte_view[4]);
              section.emit_u8(byte_view[5]);
              section.emit_u8(byte_view[6]);
              section.emit_u8(byte_view[7]);
409
              break;
410 411 412 413 414
            }
          } else {
            // Emit a global-index initializer.
            section.emit_u8(kExprGetGlobal);
            section.emit_u32v(global.init_index);
415 416 417 418 419
          }
          section.emit_u8(kExprEnd);  // end of init expression
        }
      });
    }
420 421

    // Add export table.
422
    var mem_export = (wasm.memory != undefined && wasm.memory.exp);
423 424
    var exports_count = wasm.exports.length + (mem_export ? 1 : 0);
    if (exports_count > 0) {
rossberg's avatar
rossberg committed
425
      if (debug) print("emitting exports @ " + binary.length);
426
      binary.emit_section(kExportSectionCode, section => {
427 428 429 430 431
        section.emit_u32v(exports_count);
        for (let exp of wasm.exports) {
          section.emit_string(exp.name);
          section.emit_u8(exp.kind);
          section.emit_u32v(exp.index);
rossberg's avatar
rossberg committed
432
        }
433 434 435 436 437
        if (mem_export) {
          section.emit_string("memory");
          section.emit_u8(kExternalMemory);
          section.emit_u8(0);
        }
rossberg's avatar
rossberg committed
438
      });
439 440 441 442
    }

    // Add start function section.
    if (wasm.start_index != undefined) {
rossberg's avatar
rossberg committed
443
      if (debug) print("emitting start function @ " + binary.length);
444
      binary.emit_section(kStartSectionCode, section => {
445
        section.emit_u32v(wasm.start_index);
rossberg's avatar
rossberg committed
446
      });
447 448
    }

449
    // Add table elements.
450
    if (wasm.function_table_inits.length > 0) {
451 452
      if (debug) print("emitting table @ " + binary.length);
      binary.emit_section(kElementSectionCode, section => {
453 454
        var inits = wasm.function_table_inits;
        section.emit_u32v(inits.length);
455
        section.emit_u8(0); // table index
456 457 458 459 460 461 462 463 464 465 466 467 468

        for (let init of inits) {
          if (init.is_global) {
            section.emit_u8(kExprGetGlobal);
          } else {
            section.emit_u8(kExprI32Const);
          }
          section.emit_u32v(init.base);
          section.emit_u8(kExprEnd);
          section.emit_u32v(init.array.length);
          for (let index of init.array) {
            section.emit_u32v(index);
          }
469 470 471 472
        }
      });
    }

473 474
    // Add function bodies.
    if (wasm.functions.length > 0) {
rossberg's avatar
rossberg committed
475 476
      // emit function bodies
      if (debug) print("emitting code @ " + binary.length);
477
      binary.emit_section(kCodeSectionCode, section => {
478
        section.emit_u32v(wasm.functions.length);
rossberg's avatar
rossberg committed
479 480 481 482 483 484 485
        for (let func of wasm.functions) {
          // Function body length will be patched later.
          let local_decls = [];
          let l = func.locals;
          if (l != undefined) {
            let local_decls_count = 0;
            if (l.i32_count > 0) {
486
              local_decls.push({count: l.i32_count, type: kWasmI32});
487
            }
rossberg's avatar
rossberg committed
488
            if (l.i64_count > 0) {
489
              local_decls.push({count: l.i64_count, type: kWasmI64});
rossberg's avatar
rossberg committed
490 491
            }
            if (l.f32_count > 0) {
492
              local_decls.push({count: l.f32_count, type: kWasmF32});
rossberg's avatar
rossberg committed
493 494
            }
            if (l.f64_count > 0) {
495
              local_decls.push({count: l.f64_count, type: kWasmF64});
rossberg's avatar
rossberg committed
496 497 498 499
            }
          }

          let header = new Binary;
500
          header.emit_u32v(local_decls.length);
rossberg's avatar
rossberg committed
501
          for (let decl of local_decls) {
502
            header.emit_u32v(decl.count);
rossberg's avatar
rossberg committed
503 504 505
            header.emit_u8(decl.type);
          }

506
          section.emit_u32v(header.length + func.body.length);
rossberg's avatar
rossberg committed
507 508 509 510
          section.emit_bytes(header);
          section.emit_bytes(func.body);
        }
      });
511
    }
512

513
    // Add data segments.
rossberg's avatar
rossberg committed
514 515
    if (wasm.segments.length > 0) {
      if (debug) print("emitting data segments @ " + binary.length);
516
      binary.emit_section(kDataSectionCode, section => {
517
        section.emit_u32v(wasm.segments.length);
rossberg's avatar
rossberg committed
518
        for (let seg of wasm.segments) {
519
          section.emit_u8(0);  // linear memory index 0
520 521 522 523 524 525 526 527 528
          if (seg.is_global) {
            // initializer is a global variable
            section.emit_u8(kExprGetGlobal);
            section.emit_u32v(seg.addr);
          } else {
            // initializer is a constant
            section.emit_u8(kExprI32Const);
            section.emit_u32v(seg.addr);
          }
529
          section.emit_u8(kExprEnd);
530
          section.emit_u32v(seg.data.length);
rossberg's avatar
rossberg committed
531 532 533
          section.emit_bytes(seg.data);
        }
      });
534 535
    }

536
    // Add any explicitly added sections
rossberg's avatar
rossberg committed
537 538 539
    for (let exp of wasm.explicit) {
      if (debug) print("emitting explicit @ " + binary.length);
      binary.emit_bytes(exp);
540 541
    }

542 543
    // Add function names.
    if (has_names) {
rossberg's avatar
rossberg committed
544
      if (debug) print("emitting names @ " + binary.length);
545 546
      binary.emit_section(kUnknownSectionCode, section => {
        section.emit_string("name");
547 548 549 550 551 552
        var count = wasm.functions.length + wasm.num_imported_funcs;
        section.emit_u32v(count);
        for (var i = 0; i < wasm.num_imported_funcs; i++) {
          section.emit_u8(0); // empty string
          section.emit_u8(0); // local names count == 0
        }
rossberg's avatar
rossberg committed
553 554 555 556 557 558
        for (let func of wasm.functions) {
          var name = func.name == undefined ? "" : func.name;
          section.emit_string(name);
          section.emit_u8(0);  // local names count == 0
        }
      });
559 560
    }

rossberg's avatar
rossberg committed
561 562
    return binary;
  }
563

564
  toBuffer(debug = false) {
rossberg's avatar
rossberg committed
565 566 567 568 569 570 571
    let bytes = this.toArray(debug);
    let buffer = new ArrayBuffer(bytes.length);
    let view = new Uint8Array(buffer);
    for (let i = 0; i < bytes.length; i++) {
      let val = bytes[i];
      if ((typeof val) == "string") val = val.charCodeAt(0);
      view[i] = val | 0;
572 573
    }
    return buffer;
rossberg's avatar
rossberg committed
574
  }
575

mtrofin's avatar
mtrofin committed
576
  instantiate(ffi) {
rossberg's avatar
rossberg committed
577
    let module = new WebAssembly.Module(this.toBuffer());
mtrofin's avatar
mtrofin committed
578
    let instance = new WebAssembly.Instance(module, ffi);
rossberg's avatar
rossberg committed
579 580
    return instance;
  }
581
}