1 | "use strict";
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, "__esModule", {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.print = print;
|
---|
7 |
|
---|
8 | var _ast = require("@webassemblyjs/ast");
|
---|
9 |
|
---|
10 | var _long = _interopRequireDefault(require("@xtuc/long"));
|
---|
11 |
|
---|
12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
---|
13 |
|
---|
14 | function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
|
---|
15 |
|
---|
16 | function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
---|
17 |
|
---|
18 | function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
---|
19 |
|
---|
20 | function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
---|
21 |
|
---|
22 | function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
---|
23 |
|
---|
24 | function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
---|
25 |
|
---|
26 | function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
---|
27 |
|
---|
28 | var compact = false;
|
---|
29 | var space = " ";
|
---|
30 |
|
---|
31 | var quote = function quote(str) {
|
---|
32 | return "\"".concat(str, "\"");
|
---|
33 | };
|
---|
34 |
|
---|
35 | function indent(nb) {
|
---|
36 | return Array(nb).fill(space + space).join("");
|
---|
37 | } // TODO(sven): allow arbitrary ast nodes
|
---|
38 |
|
---|
39 |
|
---|
40 | function print(n) {
|
---|
41 | if (n.type === "Program") {
|
---|
42 | return printProgram(n, 0);
|
---|
43 | } else {
|
---|
44 | throw new Error("Unsupported node in print of type: " + String(n.type));
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | function printProgram(n, depth) {
|
---|
49 | return n.body.reduce(function (acc, child) {
|
---|
50 | if (child.type === "Module") {
|
---|
51 | acc += printModule(child, depth + 1);
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (child.type === "Func") {
|
---|
55 | acc += printFunc(child, depth + 1);
|
---|
56 | }
|
---|
57 |
|
---|
58 | if (child.type === "BlockComment") {
|
---|
59 | acc += printBlockComment(child);
|
---|
60 | }
|
---|
61 |
|
---|
62 | if (child.type === "LeadingComment") {
|
---|
63 | acc += printLeadingComment(child);
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (compact === false) {
|
---|
67 | acc += "\n";
|
---|
68 | }
|
---|
69 |
|
---|
70 | return acc;
|
---|
71 | }, "");
|
---|
72 | }
|
---|
73 |
|
---|
74 | function printTypeInstruction(n) {
|
---|
75 | var out = "";
|
---|
76 | out += "(";
|
---|
77 | out += "type";
|
---|
78 | out += space;
|
---|
79 |
|
---|
80 | if (n.id != null) {
|
---|
81 | out += printIndex(n.id);
|
---|
82 | out += space;
|
---|
83 | }
|
---|
84 |
|
---|
85 | out += "(";
|
---|
86 | out += "func";
|
---|
87 | n.functype.params.forEach(function (param) {
|
---|
88 | out += space;
|
---|
89 | out += "(";
|
---|
90 | out += "param";
|
---|
91 | out += space;
|
---|
92 | out += printFuncParam(param);
|
---|
93 | out += ")";
|
---|
94 | });
|
---|
95 | n.functype.results.forEach(function (result) {
|
---|
96 | out += space;
|
---|
97 | out += "(";
|
---|
98 | out += "result";
|
---|
99 | out += space;
|
---|
100 | out += result;
|
---|
101 | out += ")";
|
---|
102 | });
|
---|
103 | out += ")"; // func
|
---|
104 |
|
---|
105 | out += ")";
|
---|
106 | return out;
|
---|
107 | }
|
---|
108 |
|
---|
109 | function printModule(n, depth) {
|
---|
110 | var out = "(";
|
---|
111 | out += "module";
|
---|
112 |
|
---|
113 | if (typeof n.id === "string") {
|
---|
114 | out += space;
|
---|
115 | out += n.id;
|
---|
116 | }
|
---|
117 |
|
---|
118 | if (compact === false) {
|
---|
119 | out += "\n";
|
---|
120 | } else {
|
---|
121 | out += space;
|
---|
122 | }
|
---|
123 |
|
---|
124 | n.fields.forEach(function (field) {
|
---|
125 | if (compact === false) {
|
---|
126 | out += indent(depth);
|
---|
127 | }
|
---|
128 |
|
---|
129 | switch (field.type) {
|
---|
130 | case "Func":
|
---|
131 | {
|
---|
132 | out += printFunc(field, depth + 1);
|
---|
133 | break;
|
---|
134 | }
|
---|
135 |
|
---|
136 | case "TypeInstruction":
|
---|
137 | {
|
---|
138 | out += printTypeInstruction(field);
|
---|
139 | break;
|
---|
140 | }
|
---|
141 |
|
---|
142 | case "Table":
|
---|
143 | {
|
---|
144 | out += printTable(field);
|
---|
145 | break;
|
---|
146 | }
|
---|
147 |
|
---|
148 | case "Global":
|
---|
149 | {
|
---|
150 | out += printGlobal(field, depth + 1);
|
---|
151 | break;
|
---|
152 | }
|
---|
153 |
|
---|
154 | case "ModuleExport":
|
---|
155 | {
|
---|
156 | out += printModuleExport(field);
|
---|
157 | break;
|
---|
158 | }
|
---|
159 |
|
---|
160 | case "ModuleImport":
|
---|
161 | {
|
---|
162 | out += printModuleImport(field);
|
---|
163 | break;
|
---|
164 | }
|
---|
165 |
|
---|
166 | case "Memory":
|
---|
167 | {
|
---|
168 | out += printMemory(field);
|
---|
169 | break;
|
---|
170 | }
|
---|
171 |
|
---|
172 | case "BlockComment":
|
---|
173 | {
|
---|
174 | out += printBlockComment(field);
|
---|
175 | break;
|
---|
176 | }
|
---|
177 |
|
---|
178 | case "LeadingComment":
|
---|
179 | {
|
---|
180 | out += printLeadingComment(field);
|
---|
181 | break;
|
---|
182 | }
|
---|
183 |
|
---|
184 | case "Start":
|
---|
185 | {
|
---|
186 | out += printStart(field);
|
---|
187 | break;
|
---|
188 | }
|
---|
189 |
|
---|
190 | case "Elem":
|
---|
191 | {
|
---|
192 | out += printElem(field, depth);
|
---|
193 | break;
|
---|
194 | }
|
---|
195 |
|
---|
196 | case "Data":
|
---|
197 | {
|
---|
198 | out += printData(field, depth);
|
---|
199 | break;
|
---|
200 | }
|
---|
201 |
|
---|
202 | default:
|
---|
203 | throw new Error("Unsupported node in printModule: " + String(field.type));
|
---|
204 | }
|
---|
205 |
|
---|
206 | if (compact === false) {
|
---|
207 | out += "\n";
|
---|
208 | }
|
---|
209 | });
|
---|
210 | out += ")";
|
---|
211 | return out;
|
---|
212 | }
|
---|
213 |
|
---|
214 | function printData(n, depth) {
|
---|
215 | var out = "";
|
---|
216 | out += "(";
|
---|
217 | out += "data";
|
---|
218 | out += space;
|
---|
219 | out += printIndex(n.memoryIndex);
|
---|
220 | out += space;
|
---|
221 | out += printInstruction(n.offset, depth);
|
---|
222 | out += space;
|
---|
223 | out += '"';
|
---|
224 | n.init.values.forEach(function (_byte) {
|
---|
225 | // Avoid non-displayable characters
|
---|
226 | if (_byte <= 31 || _byte == 34 || _byte == 92 || _byte >= 127) {
|
---|
227 | out += "\\";
|
---|
228 | out += ("00" + _byte.toString(16)).substr(-2);
|
---|
229 | } else if (_byte > 255) {
|
---|
230 | throw new Error("Unsupported byte in data segment: " + _byte);
|
---|
231 | } else {
|
---|
232 | out += String.fromCharCode(_byte);
|
---|
233 | }
|
---|
234 | });
|
---|
235 | out += '"';
|
---|
236 | out += ")";
|
---|
237 | return out;
|
---|
238 | }
|
---|
239 |
|
---|
240 | function printElem(n, depth) {
|
---|
241 | var out = "";
|
---|
242 | out += "(";
|
---|
243 | out += "elem";
|
---|
244 | out += space;
|
---|
245 | out += printIndex(n.table);
|
---|
246 |
|
---|
247 | var _n$offset = _slicedToArray(n.offset, 1),
|
---|
248 | firstOffset = _n$offset[0];
|
---|
249 |
|
---|
250 | out += space;
|
---|
251 | out += "(";
|
---|
252 | out += "offset";
|
---|
253 | out += space;
|
---|
254 | out += printInstruction(firstOffset, depth);
|
---|
255 | out += ")";
|
---|
256 | n.funcs.forEach(function (func) {
|
---|
257 | out += space;
|
---|
258 | out += printIndex(func);
|
---|
259 | });
|
---|
260 | out += ")";
|
---|
261 | return out;
|
---|
262 | }
|
---|
263 |
|
---|
264 | function printStart(n) {
|
---|
265 | var out = "";
|
---|
266 | out += "(";
|
---|
267 | out += "start";
|
---|
268 | out += space;
|
---|
269 | out += printIndex(n.index);
|
---|
270 | out += ")";
|
---|
271 | return out;
|
---|
272 | }
|
---|
273 |
|
---|
274 | function printLeadingComment(n) {
|
---|
275 | // Don't print leading comments in compact mode
|
---|
276 | if (compact === true) {
|
---|
277 | return "";
|
---|
278 | }
|
---|
279 |
|
---|
280 | var out = "";
|
---|
281 | out += ";;";
|
---|
282 | out += n.value;
|
---|
283 | out += "\n";
|
---|
284 | return out;
|
---|
285 | }
|
---|
286 |
|
---|
287 | function printBlockComment(n) {
|
---|
288 | // Don't print block comments in compact mode
|
---|
289 | if (compact === true) {
|
---|
290 | return "";
|
---|
291 | }
|
---|
292 |
|
---|
293 | var out = "";
|
---|
294 | out += "(;";
|
---|
295 | out += n.value;
|
---|
296 | out += ";)";
|
---|
297 | out += "\n";
|
---|
298 | return out;
|
---|
299 | }
|
---|
300 |
|
---|
301 | function printSignature(n) {
|
---|
302 | var out = "";
|
---|
303 | n.params.forEach(function (param) {
|
---|
304 | out += space;
|
---|
305 | out += "(";
|
---|
306 | out += "param";
|
---|
307 | out += space;
|
---|
308 | out += printFuncParam(param);
|
---|
309 | out += ")";
|
---|
310 | });
|
---|
311 | n.results.forEach(function (result) {
|
---|
312 | out += space;
|
---|
313 | out += "(";
|
---|
314 | out += "result";
|
---|
315 | out += space;
|
---|
316 | out += result;
|
---|
317 | out += ")";
|
---|
318 | });
|
---|
319 | return out;
|
---|
320 | }
|
---|
321 |
|
---|
322 | function printModuleImportDescr(n) {
|
---|
323 | var out = "";
|
---|
324 |
|
---|
325 | if (n.type === "FuncImportDescr") {
|
---|
326 | out += "(";
|
---|
327 | out += "func";
|
---|
328 |
|
---|
329 | if ((0, _ast.isAnonymous)(n.id) === false) {
|
---|
330 | out += space;
|
---|
331 | out += printIdentifier(n.id);
|
---|
332 | }
|
---|
333 |
|
---|
334 | out += printSignature(n.signature);
|
---|
335 | out += ")";
|
---|
336 | }
|
---|
337 |
|
---|
338 | if (n.type === "GlobalType") {
|
---|
339 | out += "(";
|
---|
340 | out += "global";
|
---|
341 | out += space;
|
---|
342 | out += printGlobalType(n);
|
---|
343 | out += ")";
|
---|
344 | }
|
---|
345 |
|
---|
346 | if (n.type === "Table") {
|
---|
347 | out += printTable(n);
|
---|
348 | }
|
---|
349 |
|
---|
350 | return out;
|
---|
351 | }
|
---|
352 |
|
---|
353 | function printModuleImport(n) {
|
---|
354 | var out = "";
|
---|
355 | out += "(";
|
---|
356 | out += "import";
|
---|
357 | out += space;
|
---|
358 | out += quote(n.module);
|
---|
359 | out += space;
|
---|
360 | out += quote(n.name);
|
---|
361 | out += space;
|
---|
362 | out += printModuleImportDescr(n.descr);
|
---|
363 | out += ")";
|
---|
364 | return out;
|
---|
365 | }
|
---|
366 |
|
---|
367 | function printGlobalType(n) {
|
---|
368 | var out = "";
|
---|
369 |
|
---|
370 | if (n.mutability === "var") {
|
---|
371 | out += "(";
|
---|
372 | out += "mut";
|
---|
373 | out += space;
|
---|
374 | out += n.valtype;
|
---|
375 | out += ")";
|
---|
376 | } else {
|
---|
377 | out += n.valtype;
|
---|
378 | }
|
---|
379 |
|
---|
380 | return out;
|
---|
381 | }
|
---|
382 |
|
---|
383 | function printGlobal(n, depth) {
|
---|
384 | var out = "";
|
---|
385 | out += "(";
|
---|
386 | out += "global";
|
---|
387 | out += space;
|
---|
388 |
|
---|
389 | if (n.name != null && (0, _ast.isAnonymous)(n.name) === false) {
|
---|
390 | out += printIdentifier(n.name);
|
---|
391 | out += space;
|
---|
392 | }
|
---|
393 |
|
---|
394 | out += printGlobalType(n.globalType);
|
---|
395 | out += space;
|
---|
396 | n.init.forEach(function (i) {
|
---|
397 | out += printInstruction(i, depth + 1);
|
---|
398 | });
|
---|
399 | out += ")";
|
---|
400 | return out;
|
---|
401 | }
|
---|
402 |
|
---|
403 | function printTable(n) {
|
---|
404 | var out = "";
|
---|
405 | out += "(";
|
---|
406 | out += "table";
|
---|
407 | out += space;
|
---|
408 |
|
---|
409 | if (n.name != null && (0, _ast.isAnonymous)(n.name) === false) {
|
---|
410 | out += printIdentifier(n.name);
|
---|
411 | out += space;
|
---|
412 | }
|
---|
413 |
|
---|
414 | out += printLimit(n.limits);
|
---|
415 | out += space;
|
---|
416 | out += n.elementType;
|
---|
417 | out += ")";
|
---|
418 | return out;
|
---|
419 | }
|
---|
420 |
|
---|
421 | function printFuncParam(n) {
|
---|
422 | var out = "";
|
---|
423 |
|
---|
424 | if (typeof n.id === "string") {
|
---|
425 | out += "$" + n.id;
|
---|
426 | out += space;
|
---|
427 | }
|
---|
428 |
|
---|
429 | out += n.valtype;
|
---|
430 | return out;
|
---|
431 | }
|
---|
432 |
|
---|
433 | function printFunc(n, depth) {
|
---|
434 | var out = "";
|
---|
435 | out += "(";
|
---|
436 | out += "func";
|
---|
437 |
|
---|
438 | if (n.name != null) {
|
---|
439 | if (n.name.type === "Identifier" && (0, _ast.isAnonymous)(n.name) === false) {
|
---|
440 | out += space;
|
---|
441 | out += printIdentifier(n.name);
|
---|
442 | }
|
---|
443 | }
|
---|
444 |
|
---|
445 | if (n.signature.type === "Signature") {
|
---|
446 | out += printSignature(n.signature);
|
---|
447 | } else {
|
---|
448 | var index = n.signature;
|
---|
449 | out += space;
|
---|
450 | out += "(";
|
---|
451 | out += "type";
|
---|
452 | out += space;
|
---|
453 | out += printIndex(index);
|
---|
454 | out += ")";
|
---|
455 | }
|
---|
456 |
|
---|
457 | if (n.body.length > 0) {
|
---|
458 | // func is empty since we ignore the default end instruction
|
---|
459 | if (n.body.length === 1 && n.body[0].id === "end") {
|
---|
460 | out += ")";
|
---|
461 | return out;
|
---|
462 | }
|
---|
463 |
|
---|
464 | if (compact === false) {
|
---|
465 | out += "\n";
|
---|
466 | }
|
---|
467 |
|
---|
468 | n.body.forEach(function (i) {
|
---|
469 | if (i.id !== "end") {
|
---|
470 | out += indent(depth);
|
---|
471 | out += printInstruction(i, depth);
|
---|
472 |
|
---|
473 | if (compact === false) {
|
---|
474 | out += "\n";
|
---|
475 | }
|
---|
476 | }
|
---|
477 | });
|
---|
478 | out += indent(depth - 1) + ")";
|
---|
479 | } else {
|
---|
480 | out += ")";
|
---|
481 | }
|
---|
482 |
|
---|
483 | return out;
|
---|
484 | }
|
---|
485 |
|
---|
486 | function printInstruction(n, depth) {
|
---|
487 | switch (n.type) {
|
---|
488 | case "Instr":
|
---|
489 | // $FlowIgnore
|
---|
490 | return printGenericInstruction(n, depth + 1);
|
---|
491 |
|
---|
492 | case "BlockInstruction":
|
---|
493 | // $FlowIgnore
|
---|
494 | return printBlockInstruction(n, depth + 1);
|
---|
495 |
|
---|
496 | case "IfInstruction":
|
---|
497 | // $FlowIgnore
|
---|
498 | return printIfInstruction(n, depth + 1);
|
---|
499 |
|
---|
500 | case "CallInstruction":
|
---|
501 | // $FlowIgnore
|
---|
502 | return printCallInstruction(n, depth + 1);
|
---|
503 |
|
---|
504 | case "CallIndirectInstruction":
|
---|
505 | // $FlowIgnore
|
---|
506 | return printCallIndirectIntruction(n, depth + 1);
|
---|
507 |
|
---|
508 | case "LoopInstruction":
|
---|
509 | // $FlowIgnore
|
---|
510 | return printLoopInstruction(n, depth + 1);
|
---|
511 |
|
---|
512 | default:
|
---|
513 | throw new Error("Unsupported instruction: " + JSON.stringify(n.type));
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 | function printCallIndirectIntruction(n, depth) {
|
---|
518 | var out = "";
|
---|
519 | out += "(";
|
---|
520 | out += "call_indirect";
|
---|
521 |
|
---|
522 | if (n.signature.type === "Signature") {
|
---|
523 | out += printSignature(n.signature);
|
---|
524 | } else if (n.signature.type === "Identifier") {
|
---|
525 | out += space;
|
---|
526 | out += "(";
|
---|
527 | out += "type";
|
---|
528 | out += space;
|
---|
529 | out += printIdentifier(n.signature);
|
---|
530 | out += ")";
|
---|
531 | } else {
|
---|
532 | throw new Error("CallIndirectInstruction: unsupported signature " + JSON.stringify(n.signature.type));
|
---|
533 | }
|
---|
534 |
|
---|
535 | out += space;
|
---|
536 |
|
---|
537 | if (n.intrs != null) {
|
---|
538 | // $FlowIgnore
|
---|
539 | n.intrs.forEach(function (i, index) {
|
---|
540 | // $FlowIgnore
|
---|
541 | out += printInstruction(i, depth + 1); // $FlowIgnore
|
---|
542 |
|
---|
543 | if (index !== n.intrs.length - 1) {
|
---|
544 | out += space;
|
---|
545 | }
|
---|
546 | });
|
---|
547 | }
|
---|
548 |
|
---|
549 | out += ")";
|
---|
550 | return out;
|
---|
551 | }
|
---|
552 |
|
---|
553 | function printLoopInstruction(n, depth) {
|
---|
554 | var out = "";
|
---|
555 | out += "(";
|
---|
556 | out += "loop";
|
---|
557 |
|
---|
558 | if (n.label != null && (0, _ast.isAnonymous)(n.label) === false) {
|
---|
559 | out += space;
|
---|
560 | out += printIdentifier(n.label);
|
---|
561 | }
|
---|
562 |
|
---|
563 | if (typeof n.resulttype === "string") {
|
---|
564 | out += space;
|
---|
565 | out += "(";
|
---|
566 | out += "result";
|
---|
567 | out += space;
|
---|
568 | out += n.resulttype;
|
---|
569 | out += ")";
|
---|
570 | }
|
---|
571 |
|
---|
572 | if (n.instr.length > 0) {
|
---|
573 | n.instr.forEach(function (e) {
|
---|
574 | if (compact === false) {
|
---|
575 | out += "\n";
|
---|
576 | }
|
---|
577 |
|
---|
578 | out += indent(depth);
|
---|
579 | out += printInstruction(e, depth + 1);
|
---|
580 | });
|
---|
581 |
|
---|
582 | if (compact === false) {
|
---|
583 | out += "\n";
|
---|
584 | out += indent(depth - 1);
|
---|
585 | }
|
---|
586 | }
|
---|
587 |
|
---|
588 | out += ")";
|
---|
589 | return out;
|
---|
590 | }
|
---|
591 |
|
---|
592 | function printCallInstruction(n, depth) {
|
---|
593 | var out = "";
|
---|
594 | out += "(";
|
---|
595 | out += "call";
|
---|
596 | out += space;
|
---|
597 | out += printIndex(n.index);
|
---|
598 |
|
---|
599 | if (_typeof(n.instrArgs) === "object") {
|
---|
600 | // $FlowIgnore
|
---|
601 | n.instrArgs.forEach(function (arg) {
|
---|
602 | out += space;
|
---|
603 | out += printFuncInstructionArg(arg, depth + 1);
|
---|
604 | });
|
---|
605 | }
|
---|
606 |
|
---|
607 | out += ")";
|
---|
608 | return out;
|
---|
609 | }
|
---|
610 |
|
---|
611 | function printIfInstruction(n, depth) {
|
---|
612 | var out = "";
|
---|
613 | out += "(";
|
---|
614 | out += "if";
|
---|
615 |
|
---|
616 | if (n.testLabel != null && (0, _ast.isAnonymous)(n.testLabel) === false) {
|
---|
617 | out += space;
|
---|
618 | out += printIdentifier(n.testLabel);
|
---|
619 | }
|
---|
620 |
|
---|
621 | if (typeof n.result === "string") {
|
---|
622 | out += space;
|
---|
623 | out += "(";
|
---|
624 | out += "result";
|
---|
625 | out += space;
|
---|
626 | out += n.result;
|
---|
627 | out += ")";
|
---|
628 | }
|
---|
629 |
|
---|
630 | if (n.test.length > 0) {
|
---|
631 | out += space;
|
---|
632 | n.test.forEach(function (i) {
|
---|
633 | out += printInstruction(i, depth + 1);
|
---|
634 | });
|
---|
635 | }
|
---|
636 |
|
---|
637 | if (n.consequent.length > 0) {
|
---|
638 | if (compact === false) {
|
---|
639 | out += "\n";
|
---|
640 | }
|
---|
641 |
|
---|
642 | out += indent(depth);
|
---|
643 | out += "(";
|
---|
644 | out += "then";
|
---|
645 | depth++;
|
---|
646 | n.consequent.forEach(function (i) {
|
---|
647 | if (compact === false) {
|
---|
648 | out += "\n";
|
---|
649 | }
|
---|
650 |
|
---|
651 | out += indent(depth);
|
---|
652 | out += printInstruction(i, depth + 1);
|
---|
653 | });
|
---|
654 | depth--;
|
---|
655 |
|
---|
656 | if (compact === false) {
|
---|
657 | out += "\n";
|
---|
658 | out += indent(depth);
|
---|
659 | }
|
---|
660 |
|
---|
661 | out += ")";
|
---|
662 | } else {
|
---|
663 | if (compact === false) {
|
---|
664 | out += "\n";
|
---|
665 | out += indent(depth);
|
---|
666 | }
|
---|
667 |
|
---|
668 | out += "(";
|
---|
669 | out += "then";
|
---|
670 | out += ")";
|
---|
671 | }
|
---|
672 |
|
---|
673 | if (n.alternate.length > 0) {
|
---|
674 | if (compact === false) {
|
---|
675 | out += "\n";
|
---|
676 | }
|
---|
677 |
|
---|
678 | out += indent(depth);
|
---|
679 | out += "(";
|
---|
680 | out += "else";
|
---|
681 | depth++;
|
---|
682 | n.alternate.forEach(function (i) {
|
---|
683 | if (compact === false) {
|
---|
684 | out += "\n";
|
---|
685 | }
|
---|
686 |
|
---|
687 | out += indent(depth);
|
---|
688 | out += printInstruction(i, depth + 1);
|
---|
689 | });
|
---|
690 | depth--;
|
---|
691 |
|
---|
692 | if (compact === false) {
|
---|
693 | out += "\n";
|
---|
694 | out += indent(depth);
|
---|
695 | }
|
---|
696 |
|
---|
697 | out += ")";
|
---|
698 | } else {
|
---|
699 | if (compact === false) {
|
---|
700 | out += "\n";
|
---|
701 | out += indent(depth);
|
---|
702 | }
|
---|
703 |
|
---|
704 | out += "(";
|
---|
705 | out += "else";
|
---|
706 | out += ")";
|
---|
707 | }
|
---|
708 |
|
---|
709 | if (compact === false) {
|
---|
710 | out += "\n";
|
---|
711 | out += indent(depth - 1);
|
---|
712 | }
|
---|
713 |
|
---|
714 | out += ")";
|
---|
715 | return out;
|
---|
716 | }
|
---|
717 |
|
---|
718 | function printBlockInstruction(n, depth) {
|
---|
719 | var out = "";
|
---|
720 | out += "(";
|
---|
721 | out += "block";
|
---|
722 |
|
---|
723 | if (n.label != null && (0, _ast.isAnonymous)(n.label) === false) {
|
---|
724 | out += space;
|
---|
725 | out += printIdentifier(n.label);
|
---|
726 | }
|
---|
727 |
|
---|
728 | if (typeof n.result === "string") {
|
---|
729 | out += space;
|
---|
730 | out += "(";
|
---|
731 | out += "result";
|
---|
732 | out += space;
|
---|
733 | out += n.result;
|
---|
734 | out += ")";
|
---|
735 | }
|
---|
736 |
|
---|
737 | if (n.instr.length > 0) {
|
---|
738 | n.instr.forEach(function (i) {
|
---|
739 | if (compact === false) {
|
---|
740 | out += "\n";
|
---|
741 | }
|
---|
742 |
|
---|
743 | out += indent(depth);
|
---|
744 | out += printInstruction(i, depth + 1);
|
---|
745 | });
|
---|
746 |
|
---|
747 | if (compact === false) {
|
---|
748 | out += "\n";
|
---|
749 | }
|
---|
750 |
|
---|
751 | out += indent(depth - 1);
|
---|
752 | out += ")";
|
---|
753 | } else {
|
---|
754 | out += ")";
|
---|
755 | }
|
---|
756 |
|
---|
757 | return out;
|
---|
758 | }
|
---|
759 |
|
---|
760 | function printGenericInstruction(n, depth) {
|
---|
761 | var out = "";
|
---|
762 | out += "(";
|
---|
763 |
|
---|
764 | if (typeof n.object === "string") {
|
---|
765 | out += n.object;
|
---|
766 | out += ".";
|
---|
767 | }
|
---|
768 |
|
---|
769 | out += n.id;
|
---|
770 | n.args.forEach(function (arg) {
|
---|
771 | out += space;
|
---|
772 | out += printFuncInstructionArg(arg, depth + 1);
|
---|
773 | });
|
---|
774 |
|
---|
775 | if (n.namedArgs !== undefined) {
|
---|
776 | for (var key in n.namedArgs) {
|
---|
777 | out += space + key + "=";
|
---|
778 | out += printFuncInstructionArg(n.namedArgs[key], depth + 1);
|
---|
779 | }
|
---|
780 | }
|
---|
781 |
|
---|
782 | out += ")";
|
---|
783 | return out;
|
---|
784 | }
|
---|
785 |
|
---|
786 | function printLongNumberLiteral(n) {
|
---|
787 | if (typeof n.raw === "string") {
|
---|
788 | return n.raw;
|
---|
789 | }
|
---|
790 |
|
---|
791 | var _n$value = n.value,
|
---|
792 | low = _n$value.low,
|
---|
793 | high = _n$value.high;
|
---|
794 | var v = new _long["default"](low, high);
|
---|
795 | return v.toString();
|
---|
796 | }
|
---|
797 |
|
---|
798 | function printFloatLiteral(n) {
|
---|
799 | if (typeof n.raw === "string") {
|
---|
800 | return n.raw;
|
---|
801 | }
|
---|
802 |
|
---|
803 | return String(n.value);
|
---|
804 | }
|
---|
805 |
|
---|
806 | function printFuncInstructionArg(n, depth) {
|
---|
807 | var out = "";
|
---|
808 |
|
---|
809 | if (n.type === "NumberLiteral") {
|
---|
810 | out += printNumberLiteral(n);
|
---|
811 | }
|
---|
812 |
|
---|
813 | if (n.type === "LongNumberLiteral") {
|
---|
814 | out += printLongNumberLiteral(n);
|
---|
815 | }
|
---|
816 |
|
---|
817 | if (n.type === "Identifier" && (0, _ast.isAnonymous)(n) === false) {
|
---|
818 | out += printIdentifier(n);
|
---|
819 | }
|
---|
820 |
|
---|
821 | if (n.type === "ValtypeLiteral") {
|
---|
822 | out += n.name;
|
---|
823 | }
|
---|
824 |
|
---|
825 | if (n.type === "FloatLiteral") {
|
---|
826 | out += printFloatLiteral(n);
|
---|
827 | }
|
---|
828 |
|
---|
829 | if ((0, _ast.isInstruction)(n)) {
|
---|
830 | out += printInstruction(n, depth + 1);
|
---|
831 | }
|
---|
832 |
|
---|
833 | return out;
|
---|
834 | }
|
---|
835 |
|
---|
836 | function printNumberLiteral(n) {
|
---|
837 | if (typeof n.raw === "string") {
|
---|
838 | return n.raw;
|
---|
839 | }
|
---|
840 |
|
---|
841 | return String(n.value);
|
---|
842 | }
|
---|
843 |
|
---|
844 | function printModuleExport(n) {
|
---|
845 | var out = "";
|
---|
846 | out += "(";
|
---|
847 | out += "export";
|
---|
848 | out += space;
|
---|
849 | out += quote(n.name);
|
---|
850 |
|
---|
851 | if (n.descr.exportType === "Func") {
|
---|
852 | out += space;
|
---|
853 | out += "(";
|
---|
854 | out += "func";
|
---|
855 | out += space;
|
---|
856 | out += printIndex(n.descr.id);
|
---|
857 | out += ")";
|
---|
858 | } else if (n.descr.exportType === "Global") {
|
---|
859 | out += space;
|
---|
860 | out += "(";
|
---|
861 | out += "global";
|
---|
862 | out += space;
|
---|
863 | out += printIndex(n.descr.id);
|
---|
864 | out += ")";
|
---|
865 | } else if (n.descr.exportType === "Memory") {
|
---|
866 | out += space;
|
---|
867 | out += "(";
|
---|
868 | out += "memory";
|
---|
869 | out += space;
|
---|
870 | out += printIndex(n.descr.id);
|
---|
871 | out += ")";
|
---|
872 | } else if (n.descr.exportType === "Table") {
|
---|
873 | out += space;
|
---|
874 | out += "(";
|
---|
875 | out += "table";
|
---|
876 | out += space;
|
---|
877 | out += printIndex(n.descr.id);
|
---|
878 | out += ")";
|
---|
879 | } else {
|
---|
880 | throw new Error("printModuleExport: unknown type: " + n.descr.exportType);
|
---|
881 | }
|
---|
882 |
|
---|
883 | out += ")";
|
---|
884 | return out;
|
---|
885 | }
|
---|
886 |
|
---|
887 | function printIdentifier(n) {
|
---|
888 | return "$" + n.value;
|
---|
889 | }
|
---|
890 |
|
---|
891 | function printIndex(n) {
|
---|
892 | if (n.type === "Identifier") {
|
---|
893 | return printIdentifier(n);
|
---|
894 | } else if (n.type === "NumberLiteral") {
|
---|
895 | return printNumberLiteral(n);
|
---|
896 | } else {
|
---|
897 | throw new Error("Unsupported index: " + n.type);
|
---|
898 | }
|
---|
899 | }
|
---|
900 |
|
---|
901 | function printMemory(n) {
|
---|
902 | var out = "";
|
---|
903 | out += "(";
|
---|
904 | out += "memory";
|
---|
905 |
|
---|
906 | if (n.id != null) {
|
---|
907 | out += space;
|
---|
908 | out += printIndex(n.id);
|
---|
909 | out += space;
|
---|
910 | }
|
---|
911 |
|
---|
912 | out += printLimit(n.limits);
|
---|
913 | out += ")";
|
---|
914 | return out;
|
---|
915 | }
|
---|
916 |
|
---|
917 | function printLimit(n) {
|
---|
918 | var out = "";
|
---|
919 | out += n.min + "";
|
---|
920 |
|
---|
921 | if (n.max != null) {
|
---|
922 | out += space;
|
---|
923 | out += String(n.max);
|
---|
924 |
|
---|
925 | if (n.shared === true) {
|
---|
926 | out += " shared";
|
---|
927 | }
|
---|
928 | }
|
---|
929 |
|
---|
930 | return out;
|
---|
931 | } |
---|