blob: e1d996338c2b5a7d0525a20b090f271f8a523a80 [file] [log] [blame]
Ben Murdochda12d292016-06-02 14:46:10 +01001// Copyright 2016 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
5function WasmFunctionBuilder(name, sig_index) {
6 this.name = name;
7 this.sig_index = sig_index;
8 this.exports = [];
9}
10
11WasmFunctionBuilder.prototype.exportAs = function(name) {
12 this.exports.push(name);
13 return this;
14}
15
16WasmFunctionBuilder.prototype.exportFunc = function() {
17 this.exports.push(this.name);
18 return this;
19}
20
21WasmFunctionBuilder.prototype.addBody = function(body) {
22 this.body = body;
23 return this;
24}
25
26WasmFunctionBuilder.prototype.addLocals = function(locals) {
27 this.locals = locals;
28 return this;
29}
30
31function WasmModuleBuilder() {
32 this.signatures = [];
33 this.imports = [];
34 this.functions = [];
35 this.exports = [];
36 this.function_table = [];
37 this.data_segments = [];
38 this.explicit = [];
39 return this;
40}
41
42WasmModuleBuilder.prototype.addStart = function(start_index) {
43 this.start_index = start_index;
44}
45
46WasmModuleBuilder.prototype.addMemory = function(min, max, exp) {
47 this.memory = {min: min, max: max, exp: exp};
48 return this;
49}
50
51WasmModuleBuilder.prototype.addExplicitSection = function(bytes) {
52 this.explicit.push(bytes);
53 return this;
54}
55
56// Add a signature; format is [rettype, param0, param1, ...]
57WasmModuleBuilder.prototype.addSignature = function(sig) {
58 // TODO: canonicalize signatures?
59 this.signatures.push(sig);
60 return this.signatures.length - 1;
61}
62
63WasmModuleBuilder.prototype.addFunction = function(name, sig) {
64 var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig);
65 var func = new WasmFunctionBuilder(name, sig_index);
66 func.index = this.functions.length;
67 this.functions.push(func);
68 return func;
69}
70
71WasmModuleBuilder.prototype.addImportWithModule = function(module, name, sig) {
72 var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig);
73 this.imports.push({module: module, name: name, sig_index: sig_index});
74 return this.imports.length - 1;
75}
76
77WasmModuleBuilder.prototype.addImport = function(name, sig) {
78 var sig_index = (typeof sig) == "number" ? sig : this.addSignature(sig);
79 this.imports.push({module: name, name: undefined, sig_index: sig_index});
80 return this.imports.length - 1;
81}
82
83WasmModuleBuilder.prototype.addDataSegment = function(addr, data, init) {
84 this.data_segments.push({addr: addr, data: data, init: init});
85 return this.data_segments.length - 1;
86}
87
88WasmModuleBuilder.prototype.appendToFunctionTable = function(array) {
89 this.function_table = this.function_table.concat(array);
90 return this;
91}
92
93function emit_u8(bytes, val) {
94 bytes.push(val & 0xff);
95}
96
97function emit_u16(bytes, val) {
98 bytes.push(val & 0xff);
99 bytes.push((val >> 8) & 0xff);
100}
101
102function emit_u32(bytes, val) {
103 bytes.push(val & 0xff);
104 bytes.push((val >> 8) & 0xff);
105 bytes.push((val >> 16) & 0xff);
106 bytes.push((val >> 24) & 0xff);
107}
108
109function emit_string(bytes, string) {
110 emit_varint(bytes, string.length);
111 for (var i = 0; i < string.length; i++) {
112 emit_u8(bytes, string.charCodeAt(i));
113 }
114}
115
116function emit_varint(bytes, val) {
117 while (true) {
118 var v = val & 0xff;
119 val = val >>> 7;
120 if (val == 0) {
121 bytes.push(v);
122 break;
123 }
124 bytes.push(v | 0x80);
125 }
126}
127
128function emit_bytes(bytes, data) {
129 for (var i = 0; i < data.length; i++) {
130 bytes.push(data[i] & 0xff);
131 }
132}
133
134function emit_section(bytes, section_code, content_generator) {
135 // Start the section in a temporary buffer: its full length isn't know yet.
136 var tmp_bytes = [];
137 emit_string(tmp_bytes, section_names[section_code]);
138 content_generator(tmp_bytes);
139 // Now that we know the section length, emit it and copy the section.
140 emit_varint(bytes, tmp_bytes.length);
141 Array.prototype.push.apply(bytes, tmp_bytes);
142}
143
144WasmModuleBuilder.prototype.toArray = function(debug) {
145 // Add header bytes
146 var bytes = [];
147 bytes = bytes.concat([kWasmH0, kWasmH1, kWasmH2, kWasmH3,
148 kWasmV0, kWasmV1, kWasmV2, kWasmV3]);
149
150 var wasm = this;
151
152 // Add memory section
153 if (wasm.memory != undefined) {
154 if (debug) print("emitting memory @ " + bytes.length);
155 emit_section(bytes, kDeclMemory, function(bytes) {
156 emit_varint(bytes, wasm.memory.min);
157 emit_varint(bytes, wasm.memory.max);
158 emit_u8(bytes, wasm.memory.exp ? 1 : 0);
159 });
160 }
161
162 // Add signatures section
163 if (wasm.signatures.length > 0) {
164 if (debug) print("emitting signatures @ " + bytes.length);
165 emit_section(bytes, kDeclSignatures, function(bytes) {
166 emit_varint(bytes, wasm.signatures.length);
167 for (sig of wasm.signatures) {
168 var params = sig.length - 1;
169 emit_varint(bytes, params);
170 for (var j = 0; j < sig.length; j++) {
171 emit_u8(bytes, sig[j]);
172 }
173 }
174 });
175 }
176
177 // Add imports section
178 if (wasm.imports.length > 0) {
179 if (debug) print("emitting imports @ " + bytes.length);
180 emit_section(bytes, kDeclImportTable, function(bytes) {
181 emit_varint(bytes, wasm.imports.length);
182 for (imp of wasm.imports) {
183 emit_varint(bytes, imp.sig_index);
184 emit_string(bytes, imp.module);
185 emit_string(bytes, imp.name || '');
186 }
187 });
188 }
189
190 // Add functions section
191 var names = false;
192 var exports = 0;
193 if (wasm.functions.length > 0) {
194 var has_names = false;
195
196 // emit function signatures
197 if (debug) print("emitting function sigs @ " + bytes.length);
198 emit_section(bytes, kDeclFunctionSignatures, function(bytes) {
199 emit_varint(bytes, wasm.functions.length);
200 for (func of wasm.functions) {
201 has_names = has_names || (func.name != undefined &&
202 func.name.length > 0);
203 exports += func.exports.length;
204
205 emit_varint(bytes, func.sig_index);
206 }
207 });
208
209 // emit function bodies
210 if (debug) print("emitting function bodies @ " + bytes.length);
211 emit_section(bytes, kDeclFunctionBodies, function(bytes) {
212 emit_varint(bytes, wasm.functions.length);
213 for (func of wasm.functions) {
214 // Function body length will be patched later.
215 var local_decls = [];
216 var l = func.locals;
217 if (l != undefined) {
218 var local_decls_count = 0;
219 if (l.i32_count > 0) {
220 local_decls.push({count: l.i32_count, type: kAstI32});
221 }
222 if (l.i64_count > 0) {
223 local_decls.push({count: l.i64_count, type: kAstI64});
224 }
225 if (l.f32_count > 0) {
226 local_decls.push({count: l.f32_count, type: kAstF32});
227 }
228 if (l.f64_count > 0) {
229 local_decls.push({count: l.f64_count, type: kAstF64});
230 }
231 }
232 var header = new Array();
233
234 emit_varint(header, local_decls.length);
235 for (decl of local_decls) {
236 emit_varint(header, decl.count);
237 emit_u8(header, decl.type);
238 }
239
240 emit_varint(bytes, header.length + func.body.length);
241 emit_bytes(bytes, header);
242 emit_bytes(bytes, func.body);
243 }
244 });
245 }
246
247 // emit function names
248 if (has_names) {
249 if (debug) print("emitting names @ " + bytes.length);
250 emit_section(bytes, kDeclNames, function(bytes) {
251 emit_varint(bytes, wasm.functions.length);
252 for (func of wasm.functions) {
253 var name = func.name == undefined ? "" : func.name;
254 emit_string(bytes, name);
255 emit_u8(bytes, 0); // local names count == 0
256 }
257 });
258 }
259
260 // Add start function section.
261 if (wasm.start_index != undefined) {
262 if (debug) print("emitting start function @ " + bytes.length);
263 emit_section(bytes, kDeclStartFunction, function(bytes) {
264 emit_varint(bytes, wasm.start_index);
265 });
266 }
267
268 if (wasm.function_table.length > 0) {
269 if (debug) print("emitting function table @ " + bytes.length);
270 emit_section(bytes, kDeclFunctionTable, function(bytes) {
271 emit_varint(bytes, wasm.function_table.length);
272 for (index of wasm.function_table) {
273 emit_varint(bytes, index);
274 }
275 });
276 }
277
278 if (exports > 0) {
279 if (debug) print("emitting exports @ " + bytes.length);
280 emit_section(bytes, kDeclExportTable, function(bytes) {
281 emit_varint(bytes, exports);
282 for (func of wasm.functions) {
283 for (exp of func.exports) {
284 emit_varint(bytes, func.index);
285 emit_string(bytes, exp);
286 }
287 }
288 });
289 }
290
291 if (wasm.data_segments.length > 0) {
292 if (debug) print("emitting data segments @ " + bytes.length);
293 emit_section(bytes, kDeclDataSegments, function(bytes) {
294 emit_varint(bytes, wasm.data_segments.length);
295 for (seg of wasm.data_segments) {
296 emit_varint(bytes, seg.addr);
297 emit_varint(bytes, seg.data.length);
298 emit_bytes(bytes, seg.data);
299 }
300 });
301 }
302
303 // Emit any explicitly added sections
304 for (exp of wasm.explicit) {
305 if (debug) print("emitting explicit @ " + bytes.length);
306 emit_bytes(bytes, exp);
307 }
308
309 // End the module.
310 if (debug) print("emitting end @ " + bytes.length);
311 emit_section(bytes, kDeclEnd, function(bytes) {});
312
313 return bytes;
314}
315
316WasmModuleBuilder.prototype.toBuffer = function(debug) {
317 var bytes = this.toArray(debug);
318 var buffer = new ArrayBuffer(bytes.length);
319 var view = new Uint8Array(buffer);
320 for (var i = 0; i < bytes.length; i++) {
321 var val = bytes[i];
322 if ((typeof val) == "string") val = val.charCodeAt(0);
323 view[i] = val | 0;
324 }
325 return buffer;
326}
327
328WasmModuleBuilder.prototype.instantiate = function(ffi, memory) {
329 var buffer = this.toBuffer();
330 if (memory != undefined) {
331 return Wasm.instantiateModule(buffer, ffi, memory);
332 } else {
333 return Wasm.instantiateModule(buffer, ffi);
334 }
335}