Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: test/mjsunit/wasm/wasm-module-builder.js

Issue 2345593003: [wasm] Master CL for Binary 0xC changes. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: [wasm] Master CL for Binary 0xC changes. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 class Binary extends Array { 5 class Binary extends Array {
6 emit_u8(val) { 6 emit_u8(val) {
7 this.push(val); 7 this.push(val);
8 } 8 }
9 9
10 emit_u16(val) { 10 emit_u16(val) {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 } 54 }
55 } 55 }
56 56
57 emit_header() { 57 emit_header() {
58 this.push(kWasmH0, kWasmH1, kWasmH2, kWasmH3, 58 this.push(kWasmH0, kWasmH1, kWasmH2, kWasmH3,
59 kWasmV0, kWasmV1, kWasmV2, kWasmV3); 59 kWasmV0, kWasmV1, kWasmV2, kWasmV3);
60 } 60 }
61 61
62 emit_section(section_code, content_generator) { 62 emit_section(section_code, content_generator) {
63 // Emit section name. 63 // Emit section name.
64 this.emit_string(section_names[section_code]); 64 if (section_code == kNameSectionCode) {
65 this.emit_u8(kUnknownSectionCode);
66 this.emit_string("name");
67 } else {
68 this.emit_u8(section_code);
69 }
65 // Emit the section to a temporary buffer: its full length isn't know yet. 70 // Emit the section to a temporary buffer: its full length isn't know yet.
66 let section = new Binary; 71 let section = new Binary;
67 content_generator(section); 72 content_generator(section);
68 // Emit section length. 73 // Emit section length.
69 this.emit_varint(section.length); 74 this.emit_varint(section.length);
70 // Copy the temporary buffer. 75 // Copy the temporary buffer.
71 this.push(...section); 76 this.push(...section);
72 } 77 }
73 } 78 }
74 79
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 } 145 }
141 146
142 addGlobal(local_type) { 147 addGlobal(local_type) {
143 this.globals.push(local_type); 148 this.globals.push(local_type);
144 return this.globals.length - 1; 149 return this.globals.length - 1;
145 } 150 }
146 151
147 addFunction(name, type) { 152 addFunction(name, type) {
148 let type_index = (typeof type) == "number" ? type : this.addType(type); 153 let type_index = (typeof type) == "number" ? type : this.addType(type);
149 let func = new WasmFunctionBuilder(name, type_index); 154 let func = new WasmFunctionBuilder(name, type_index);
150 func.index = this.functions.length; 155 func.index = this.functions.length + this.imports.length;
151 this.functions.push(func); 156 this.functions.push(func);
152 return func; 157 return func;
153 } 158 }
154 159
155 addImportWithModule(module, name, type) { 160 addImportWithModule(module, name, type) {
156 let type_index = (typeof type) == "number" ? type : this.addType(type); 161 let type_index = (typeof type) == "number" ? type : this.addType(type);
157 this.imports.push({module: module, name: name, type: type_index}); 162 this.imports.push({module: module, name: name, type: type_index});
158 return this.imports.length - 1; 163 return this.imports.length - 1;
159 } 164 }
160 165
(...skipping 14 matching lines...) Expand all
175 toArray(debug) { 180 toArray(debug) {
176 let binary = new Binary; 181 let binary = new Binary;
177 let wasm = this; 182 let wasm = this;
178 183
179 // Add header 184 // Add header
180 binary.emit_header(); 185 binary.emit_header();
181 186
182 // Add type section 187 // Add type section
183 if (wasm.types.length > 0) { 188 if (wasm.types.length > 0) {
184 if (debug) print("emitting types @ " + binary.length); 189 if (debug) print("emitting types @ " + binary.length);
185 binary.emit_section(kDeclTypes, section => { 190 binary.emit_section(kTypeSectionCode, section => {
186 section.emit_varint(wasm.types.length); 191 section.emit_varint(wasm.types.length);
187 for (let type of wasm.types) { 192 for (let type of wasm.types) {
188 section.emit_u8(kWasmFunctionTypeForm); 193 section.emit_u8(kWasmFunctionTypeForm);
189 section.emit_varint(type.params.length); 194 section.emit_varint(type.params.length);
190 for (let param of type.params) { 195 for (let param of type.params) {
191 section.emit_u8(param); 196 section.emit_u8(param);
192 } 197 }
193 section.emit_varint(type.results.length); 198 section.emit_varint(type.results.length);
194 for (let result of type.results) { 199 for (let result of type.results) {
195 section.emit_u8(result); 200 section.emit_u8(result);
196 } 201 }
197 } 202 }
198 }); 203 });
199 } 204 }
200 205
201 if (wasm.globals.length > 0) {
202 if (debug) print ("emitting globals @ " + binary.length);
203 binary.emit_section(kDeclGlobals, section => {
204 section.emit_varint(wasm.globals.length);
205 for (let global_type of wasm.globals) {
206 section.emit_varint(0); // length of global name
207 section.emit_u8(global_type);
208 section.emit_u8(false); // we do not support exported globals
209 }
210 });
211 }
212
213 // Add imports section 206 // Add imports section
214 if (wasm.imports.length > 0) { 207 if (wasm.imports.length > 0) {
215 if (debug) print("emitting imports @ " + binary.length); 208 if (debug) print("emitting imports @ " + binary.length);
216 binary.emit_section(kDeclImports, section => { 209 binary.emit_section(kImportSectionCode, section => {
217 section.emit_varint(wasm.imports.length); 210 section.emit_varint(wasm.imports.length);
218 for (let imp of wasm.imports) { 211 for (let imp of wasm.imports) {
219 section.emit_varint(imp.type);
220 section.emit_string(imp.module); 212 section.emit_string(imp.module);
221 section.emit_string(imp.name || ''); 213 section.emit_string(imp.name || '');
214 section.emit_u8(kExternalFunction);
215 section.emit_varint(imp.type);
222 } 216 }
223 }); 217 });
224 } 218 }
225 219
226 // Add functions declarations 220 // Add functions declarations
227 let has_names = false; 221 let has_names = false;
228 let names = false; 222 let names = false;
229 let exports = 0; 223 let exports = 0;
230 if (wasm.functions.length > 0) { 224 if (wasm.functions.length > 0) {
231 if (debug) print("emitting function decls @ " + binary.length); 225 if (debug) print("emitting function decls @ " + binary.length);
232 binary.emit_section(kDeclFunctions, section => { 226 binary.emit_section(kFunctionSectionCode, section => {
233 section.emit_varint(wasm.functions.length); 227 section.emit_varint(wasm.functions.length);
234 for (let func of wasm.functions) { 228 for (let func of wasm.functions) {
235 has_names = has_names || (func.name != undefined && 229 has_names = has_names || (func.name != undefined &&
236 func.name.length > 0); 230 func.name.length > 0);
237 exports += func.exports.length; 231 exports += func.exports.length;
238 section.emit_varint(func.type_index); 232 section.emit_varint(func.type_index);
239 } 233 }
240 }); 234 });
241 } 235 }
242 236
243 // Add table. 237 // Add table.
244 if (wasm.table.length > 0) { 238 if (wasm.table.length > 0) {
245 if (debug) print("emitting table @ " + binary.length); 239 if (debug) print("emitting table @ " + binary.length);
246 binary.emit_section(kDeclTable, section => { 240 binary.emit_section(kTableSectionCode, section => {
241 section.emit_u8(1); // one table entry
242 section.emit_u8(kWasmAnyFunctionTypeForm);
243 section.emit_u8(1);
247 section.emit_varint(wasm.table.length); 244 section.emit_varint(wasm.table.length);
248 if (wasm.pad !== null) { 245 section.emit_varint(wasm.table.length);
249 if (debug) print("emitting table padding @ " + binary.length);
250 section.emit_varint(wasm.pad);
251 }
252 for (let index of wasm.table) {
253 section.emit_varint(index);
254 }
255 }); 246 });
256 } 247 }
257 248
258 // Add memory section 249 // Add memory section
259 if (wasm.memory != undefined) { 250 if (wasm.memory != undefined) {
260 if (debug) print("emitting memory @ " + binary.length); 251 if (debug) print("emitting memory @ " + binary.length);
261 binary.emit_section(kDeclMemory, section => { 252 binary.emit_section(kMemorySectionCode, section => {
253 section.emit_u8(1); // one memory entry
254 section.emit_varint(kResizableMaximumFlag);
262 section.emit_varint(wasm.memory.min); 255 section.emit_varint(wasm.memory.min);
263 section.emit_varint(wasm.memory.max); 256 section.emit_varint(wasm.memory.max);
264 section.emit_u8(wasm.memory.exp ? 1 : 0);
265 }); 257 });
266 } 258 }
267 259
260 // Add global section.
261 if (wasm.globals.length > 0) {
262 if (debug) print ("emitting globals @ " + binary.length);
263 binary.emit_section(kGlobalSectionCode, section => {
264 section.emit_varint(wasm.globals.length);
265 for (let global_type of wasm.globals) {
266 section.emit_u8(global_type);
267 section.emit_u8(true); // mutable
268 switch (global_type) {
ahaas 2016/09/19 11:02:38 What about kGlobalIndex? Isn't that also a potenti
titzer 2016/09/19 11:37:03 Ah, kGlobalIndex is a member of the WasmExternalKi
269 case kAstI32:
270 section.emit_u8(kExprI32Const);
271 section.emit_u8(0);
272 break;
273 case kAstI64:
274 section.emit_u8(kExprI64Const);
275 section.emit_u8(0);
276 break;
277 case kAstF32:
278 section.emit_u8(kExprF32Const);
279 section.emit_u32(0);
280 break;
281 case kAstF64:
282 section.emit_u8(kExprI32Const);
283 section.emit_u32(0);
284 section.emit_u32(0);
285 break;
286 }
287 section.emit_u8(kExprEnd); // end of init expression
288 }
289 });
290 }
268 291
269 // Add export table. 292 // Add export table.
270 if (exports > 0) { 293 var mem_export = (wasm.memory != undefined && wasm.memory.exp);
294 if (exports > 0 || mem_export) {
271 if (debug) print("emitting exports @ " + binary.length); 295 if (debug) print("emitting exports @ " + binary.length);
272 binary.emit_section(kDeclExports, section => { 296 binary.emit_section(kExportSectionCode, section => {
273 section.emit_varint(exports); 297 section.emit_varint(exports + (mem_export ? 1 : 0));
274 for (let func of wasm.functions) { 298 for (let func of wasm.functions) {
275 for (let exp of func.exports) { 299 for (let exp of func.exports) {
300 section.emit_string(exp);
301 section.emit_u8(kExternalFunction);
276 section.emit_varint(func.index); 302 section.emit_varint(func.index);
277 section.emit_string(exp);
278 } 303 }
279 } 304 }
305 if (mem_export) {
306 section.emit_string("memory");
307 section.emit_u8(kExternalMemory);
308 section.emit_u8(0);
309 }
280 }); 310 });
281 } 311 }
282 312
283 // Add start function section. 313 // Add start function section.
284 if (wasm.start_index != undefined) { 314 if (wasm.start_index != undefined) {
285 if (debug) print("emitting start function @ " + binary.length); 315 if (debug) print("emitting start function @ " + binary.length);
286 binary.emit_section(kDeclStart, section => { 316 binary.emit_section(kStartSectionCode, section => {
287 section.emit_varint(wasm.start_index); 317 section.emit_varint(wasm.start_index);
288 }); 318 });
289 } 319 }
290 320
321 // Add table elements.
322 if (wasm.table.length > 0) {
323 if (debug) print("emitting table @ " + binary.length);
324 binary.emit_section(kElementSectionCode, section => {
325 section.emit_u8(1);
326 section.emit_u8(0); // table index
327 section.emit_u8(kExprI32Const);
328 section.emit_u8(0);
329 section.emit_u8(kExprEnd);
330 section.emit_varint(wasm.table.length);
331 for (let index of wasm.table) {
332 section.emit_varint(index);
333 }
334 });
335 }
336
291 // Add function bodies. 337 // Add function bodies.
292 if (wasm.functions.length > 0) { 338 if (wasm.functions.length > 0) {
293 // emit function bodies 339 // emit function bodies
294 if (debug) print("emitting code @ " + binary.length); 340 if (debug) print("emitting code @ " + binary.length);
295 binary.emit_section(kDeclCode, section => { 341 binary.emit_section(kCodeSectionCode, section => {
296 section.emit_varint(wasm.functions.length); 342 section.emit_varint(wasm.functions.length);
297 for (let func of wasm.functions) { 343 for (let func of wasm.functions) {
298 // Function body length will be patched later. 344 // Function body length will be patched later.
299 let local_decls = []; 345 let local_decls = [];
300 let l = func.locals; 346 let l = func.locals;
301 if (l != undefined) { 347 if (l != undefined) {
302 let local_decls_count = 0; 348 let local_decls_count = 0;
303 if (l.i32_count > 0) { 349 if (l.i32_count > 0) {
304 local_decls.push({count: l.i32_count, type: kAstI32}); 350 local_decls.push({count: l.i32_count, type: kAstI32});
305 } 351 }
(...skipping 18 matching lines...) Expand all
324 section.emit_varint(header.length + func.body.length); 370 section.emit_varint(header.length + func.body.length);
325 section.emit_bytes(header); 371 section.emit_bytes(header);
326 section.emit_bytes(func.body); 372 section.emit_bytes(func.body);
327 } 373 }
328 }); 374 });
329 } 375 }
330 376
331 // Add data segments. 377 // Add data segments.
332 if (wasm.segments.length > 0) { 378 if (wasm.segments.length > 0) {
333 if (debug) print("emitting data segments @ " + binary.length); 379 if (debug) print("emitting data segments @ " + binary.length);
334 binary.emit_section(kDeclData, section => { 380 binary.emit_section(kDataSectionCode, section => {
335 section.emit_varint(wasm.segments.length); 381 section.emit_varint(wasm.segments.length);
336 for (let seg of wasm.segments) { 382 for (let seg of wasm.segments) {
383 section.emit_u8(0); // linear memory index 0
384 section.emit_u8(kExprI32Const);
337 section.emit_varint(seg.addr); 385 section.emit_varint(seg.addr);
386 section.emit_u8(kExprEnd);
338 section.emit_varint(seg.data.length); 387 section.emit_varint(seg.data.length);
339 section.emit_bytes(seg.data); 388 section.emit_bytes(seg.data);
340 } 389 }
341 }); 390 });
342 } 391 }
343 392
344 // Add any explicitly added sections 393 // Add any explicitly added sections
345 for (let exp of wasm.explicit) { 394 for (let exp of wasm.explicit) {
346 if (debug) print("emitting explicit @ " + binary.length); 395 if (debug) print("emitting explicit @ " + binary.length);
347 binary.emit_bytes(exp); 396 binary.emit_bytes(exp);
348 } 397 }
349 398
350 // Add function names. 399 // Add function names.
351 if (has_names) { 400 if (has_names) {
352 if (debug) print("emitting names @ " + binary.length); 401 if (debug) print("emitting names @ " + binary.length);
353 binary.emit_section(kDeclNames, section => { 402 binary.emit_section(kNameSectionCode, section => {
354 section.emit_varint(wasm.functions.length); 403 section.emit_varint(wasm.functions.length);
355 for (let func of wasm.functions) { 404 for (let func of wasm.functions) {
356 var name = func.name == undefined ? "" : func.name; 405 var name = func.name == undefined ? "" : func.name;
357 section.emit_string(name); 406 section.emit_string(name);
358 section.emit_u8(0); // local names count == 0 407 section.emit_u8(0); // local names count == 0
359 } 408 }
360 }); 409 });
361 } 410 }
362 411
363 return binary; 412 return binary;
(...skipping 10 matching lines...) Expand all
374 } 423 }
375 return buffer; 424 return buffer;
376 } 425 }
377 426
378 instantiate(...args) { 427 instantiate(...args) {
379 let module = new WebAssembly.Module(this.toBuffer()); 428 let module = new WebAssembly.Module(this.toBuffer());
380 let instance = new WebAssembly.Instance(module, ...args); 429 let instance = new WebAssembly.Instance(module, ...args);
381 return instance; 430 return instance;
382 } 431 }
383 } 432 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698