index.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.print = print;
  6. var _ast = require("@webassemblyjs/ast");
  7. var _long = _interopRequireDefault(require("@xtuc/long"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. function _typeof(obj) { 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); }
  10. function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_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; }
  11. function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }
  12. var compact = false;
  13. var space = " ";
  14. var quote = function quote(str) {
  15. return "\"".concat(str, "\"");
  16. };
  17. function indent(nb) {
  18. return Array(nb).fill(space + space).join("");
  19. } // TODO(sven): allow arbitrary ast nodes
  20. function print(n) {
  21. if (n.type === "Program") {
  22. return printProgram(n, 0);
  23. } else {
  24. throw new Error("Unsupported node in print of type: " + String(n.type));
  25. }
  26. }
  27. function printProgram(n, depth) {
  28. return n.body.reduce(function (acc, child) {
  29. if (child.type === "Module") {
  30. acc += printModule(child, depth + 1);
  31. }
  32. if (child.type === "Func") {
  33. acc += printFunc(child, depth + 1);
  34. }
  35. if (child.type === "BlockComment") {
  36. acc += printBlockComment(child);
  37. }
  38. if (child.type === "LeadingComment") {
  39. acc += printLeadingComment(child);
  40. }
  41. if (compact === false) {
  42. acc += "\n";
  43. }
  44. return acc;
  45. }, "");
  46. }
  47. function printTypeInstruction(n) {
  48. var out = "";
  49. out += "(";
  50. out += "type";
  51. out += space;
  52. if (n.id != null) {
  53. out += printIndex(n.id);
  54. out += space;
  55. }
  56. out += "(";
  57. out += "func";
  58. n.functype.params.forEach(function (param) {
  59. out += space;
  60. out += "(";
  61. out += "param";
  62. out += space;
  63. out += printFuncParam(param);
  64. out += ")";
  65. });
  66. n.functype.results.forEach(function (result) {
  67. out += space;
  68. out += "(";
  69. out += "result";
  70. out += space;
  71. out += result;
  72. out += ")";
  73. });
  74. out += ")"; // func
  75. out += ")";
  76. return out;
  77. }
  78. function printModule(n, depth) {
  79. var out = "(";
  80. out += "module";
  81. if (typeof n.id === "string") {
  82. out += space;
  83. out += n.id;
  84. }
  85. if (compact === false) {
  86. out += "\n";
  87. } else {
  88. out += space;
  89. }
  90. n.fields.forEach(function (field) {
  91. if (compact === false) {
  92. out += indent(depth);
  93. }
  94. switch (field.type) {
  95. case "Func":
  96. {
  97. out += printFunc(field, depth + 1);
  98. break;
  99. }
  100. case "TypeInstruction":
  101. {
  102. out += printTypeInstruction(field);
  103. break;
  104. }
  105. case "Table":
  106. {
  107. out += printTable(field);
  108. break;
  109. }
  110. case "Global":
  111. {
  112. out += printGlobal(field, depth + 1);
  113. break;
  114. }
  115. case "ModuleExport":
  116. {
  117. out += printModuleExport(field);
  118. break;
  119. }
  120. case "ModuleImport":
  121. {
  122. out += printModuleImport(field);
  123. break;
  124. }
  125. case "Memory":
  126. {
  127. out += printMemory(field);
  128. break;
  129. }
  130. case "BlockComment":
  131. {
  132. out += printBlockComment(field);
  133. break;
  134. }
  135. case "LeadingComment":
  136. {
  137. out += printLeadingComment(field);
  138. break;
  139. }
  140. case "Start":
  141. {
  142. out += printStart(field);
  143. break;
  144. }
  145. case "Elem":
  146. {
  147. out += printElem(field, depth);
  148. break;
  149. }
  150. case "Data":
  151. {
  152. out += printData(field, depth);
  153. break;
  154. }
  155. default:
  156. throw new Error("Unsupported node in printModule: " + String(field.type));
  157. }
  158. if (compact === false) {
  159. out += "\n";
  160. }
  161. });
  162. out += ")";
  163. return out;
  164. }
  165. function printData(n, depth) {
  166. var out = "";
  167. out += "(";
  168. out += "data";
  169. out += space;
  170. out += printIndex(n.memoryIndex);
  171. out += space;
  172. out += printInstruction(n.offset, depth);
  173. out += space;
  174. var value = "";
  175. n.init.values.forEach(function (byte) {
  176. value += String.fromCharCode(byte);
  177. }); // Avoid non-displayable characters
  178. out += JSON.stringify(value);
  179. out += ")";
  180. return out;
  181. }
  182. function printElem(n, depth) {
  183. var out = "";
  184. out += "(";
  185. out += "elem";
  186. out += space;
  187. out += printIndex(n.table);
  188. var _n$offset = _slicedToArray(n.offset, 1),
  189. firstOffset = _n$offset[0];
  190. out += space;
  191. out += "(";
  192. out += "offset";
  193. out += space;
  194. out += printInstruction(firstOffset, depth);
  195. out += ")";
  196. n.funcs.forEach(function (func) {
  197. out += space;
  198. out += printIndex(func);
  199. });
  200. out += ")";
  201. return out;
  202. }
  203. function printStart(n) {
  204. var out = "";
  205. out += "(";
  206. out += "start";
  207. out += space;
  208. out += printIndex(n.index);
  209. out += ")";
  210. return out;
  211. }
  212. function printLeadingComment(n) {
  213. // Don't print leading comments in compact mode
  214. if (compact === true) {
  215. return "";
  216. }
  217. var out = "";
  218. out += ";;";
  219. out += n.value;
  220. out += "\n";
  221. return out;
  222. }
  223. function printBlockComment(n) {
  224. // Don't print block comments in compact mode
  225. if (compact === true) {
  226. return "";
  227. }
  228. var out = "";
  229. out += "(;";
  230. out += n.value;
  231. out += ";)";
  232. out += "\n";
  233. return out;
  234. }
  235. function printSignature(n) {
  236. var out = "";
  237. n.params.forEach(function (param) {
  238. out += space;
  239. out += "(";
  240. out += "param";
  241. out += space;
  242. out += printFuncParam(param);
  243. out += ")";
  244. });
  245. n.results.forEach(function (result) {
  246. out += space;
  247. out += "(";
  248. out += "result";
  249. out += space;
  250. out += result;
  251. out += ")";
  252. });
  253. return out;
  254. }
  255. function printModuleImportDescr(n) {
  256. var out = "";
  257. if (n.type === "FuncImportDescr") {
  258. out += "(";
  259. out += "func";
  260. if ((0, _ast.isAnonymous)(n.id) === false) {
  261. out += space;
  262. out += printIdentifier(n.id);
  263. }
  264. out += printSignature(n.signature);
  265. out += ")";
  266. }
  267. if (n.type === "GlobalType") {
  268. out += "(";
  269. out += "global";
  270. out += space;
  271. out += printGlobalType(n);
  272. out += ")";
  273. }
  274. if (n.type === "Table") {
  275. out += printTable(n);
  276. }
  277. return out;
  278. }
  279. function printModuleImport(n) {
  280. var out = "";
  281. out += "(";
  282. out += "import";
  283. out += space;
  284. out += quote(n.module);
  285. out += space;
  286. out += quote(n.name);
  287. out += space;
  288. out += printModuleImportDescr(n.descr);
  289. out += ")";
  290. return out;
  291. }
  292. function printGlobalType(n) {
  293. var out = "";
  294. if (n.mutability === "var") {
  295. out += "(";
  296. out += "mut";
  297. out += space;
  298. out += n.valtype;
  299. out += ")";
  300. } else {
  301. out += n.valtype;
  302. }
  303. return out;
  304. }
  305. function printGlobal(n, depth) {
  306. var out = "";
  307. out += "(";
  308. out += "global";
  309. out += space;
  310. if (n.name != null && (0, _ast.isAnonymous)(n.name) === false) {
  311. out += printIdentifier(n.name);
  312. out += space;
  313. }
  314. out += printGlobalType(n.globalType);
  315. out += space;
  316. n.init.forEach(function (i) {
  317. out += printInstruction(i, depth + 1);
  318. });
  319. out += ")";
  320. return out;
  321. }
  322. function printTable(n) {
  323. var out = "";
  324. out += "(";
  325. out += "table";
  326. out += space;
  327. if (n.name != null && (0, _ast.isAnonymous)(n.name) === false) {
  328. out += printIdentifier(n.name);
  329. out += space;
  330. }
  331. out += printLimit(n.limits);
  332. out += space;
  333. out += n.elementType;
  334. out += ")";
  335. return out;
  336. }
  337. function printFuncParam(n) {
  338. var out = "";
  339. if (typeof n.id === "string") {
  340. out += "$" + n.id;
  341. out += space;
  342. }
  343. out += n.valtype;
  344. return out;
  345. }
  346. function printFunc(n, depth) {
  347. var out = "";
  348. out += "(";
  349. out += "func";
  350. if (n.name != null) {
  351. if (n.name.type === "Identifier" && (0, _ast.isAnonymous)(n.name) === false) {
  352. out += space;
  353. out += printIdentifier(n.name);
  354. }
  355. }
  356. if (n.signature.type === "Signature") {
  357. out += printSignature(n.signature);
  358. } else {
  359. var index = n.signature;
  360. out += space;
  361. out += "(";
  362. out += "type";
  363. out += space;
  364. out += printIndex(index);
  365. out += ")";
  366. }
  367. if (n.body.length > 0) {
  368. if (compact === false) {
  369. out += "\n";
  370. }
  371. n.body.forEach(function (i) {
  372. out += indent(depth);
  373. out += printInstruction(i, depth);
  374. if (compact === false) {
  375. out += "\n";
  376. }
  377. });
  378. out += indent(depth - 1) + ")";
  379. } else {
  380. out += ")";
  381. }
  382. return out;
  383. }
  384. function printInstruction(n, depth) {
  385. switch (n.type) {
  386. case "Instr":
  387. // $FlowIgnore
  388. return printGenericInstruction(n, depth + 1);
  389. case "BlockInstruction":
  390. // $FlowIgnore
  391. return printBlockInstruction(n, depth + 1);
  392. case "IfInstruction":
  393. // $FlowIgnore
  394. return printIfInstruction(n, depth + 1);
  395. case "CallInstruction":
  396. // $FlowIgnore
  397. return printCallInstruction(n, depth + 1);
  398. case "CallIndirectInstruction":
  399. // $FlowIgnore
  400. return printCallIndirectIntruction(n, depth + 1);
  401. case "LoopInstruction":
  402. // $FlowIgnore
  403. return printLoopInstruction(n, depth + 1);
  404. default:
  405. throw new Error("Unsupported instruction: " + JSON.stringify(n.type));
  406. }
  407. }
  408. function printCallIndirectIntruction(n, depth) {
  409. var out = "";
  410. out += "(";
  411. out += "call_indirect";
  412. if (n.signature.type === "Signature") {
  413. out += printSignature(n.signature);
  414. } else if (n.signature.type === "Identifier") {
  415. out += space;
  416. out += "(";
  417. out += "type";
  418. out += space;
  419. out += printIdentifier(n.signature);
  420. out += ")";
  421. } else {
  422. throw new Error("CallIndirectInstruction: unsupported signature " + JSON.stringify(n.signature.type));
  423. }
  424. out += space;
  425. if (n.intrs != null) {
  426. // $FlowIgnore
  427. n.intrs.forEach(function (i, index) {
  428. // $FlowIgnore
  429. out += printInstruction(i, depth + 1); // $FlowIgnore
  430. if (index !== n.intrs.length - 1) {
  431. out += space;
  432. }
  433. });
  434. }
  435. out += ")";
  436. return out;
  437. }
  438. function printLoopInstruction(n, depth) {
  439. var out = "";
  440. out += "(";
  441. out += "loop";
  442. if (n.label != null && (0, _ast.isAnonymous)(n.label) === false) {
  443. out += space;
  444. out += printIdentifier(n.label);
  445. }
  446. if (typeof n.resulttype === "string") {
  447. out += space;
  448. out += "(";
  449. out += "result";
  450. out += space;
  451. out += n.resulttype;
  452. out += ")";
  453. }
  454. if (n.instr.length > 0) {
  455. n.instr.forEach(function (e) {
  456. if (compact === false) {
  457. out += "\n";
  458. }
  459. out += indent(depth);
  460. out += printInstruction(e, depth + 1);
  461. });
  462. if (compact === false) {
  463. out += "\n";
  464. out += indent(depth - 1);
  465. }
  466. }
  467. out += ")";
  468. return out;
  469. }
  470. function printCallInstruction(n, depth) {
  471. var out = "";
  472. out += "(";
  473. out += "call";
  474. out += space;
  475. out += printIndex(n.index);
  476. if (_typeof(n.instrArgs) === "object") {
  477. // $FlowIgnore
  478. n.instrArgs.forEach(function (arg) {
  479. out += space;
  480. out += printFuncInstructionArg(arg, depth + 1);
  481. });
  482. }
  483. out += ")";
  484. return out;
  485. }
  486. function printIfInstruction(n, depth) {
  487. var out = "";
  488. out += "(";
  489. out += "if";
  490. if (n.testLabel != null && (0, _ast.isAnonymous)(n.testLabel) === false) {
  491. out += space;
  492. out += printIdentifier(n.testLabel);
  493. }
  494. if (typeof n.result === "string") {
  495. out += space;
  496. out += "(";
  497. out += "result";
  498. out += space;
  499. out += n.result;
  500. out += ")";
  501. }
  502. if (n.test.length > 0) {
  503. out += space;
  504. n.test.forEach(function (i) {
  505. out += printInstruction(i, depth + 1);
  506. });
  507. }
  508. if (n.consequent.length > 0) {
  509. if (compact === false) {
  510. out += "\n";
  511. }
  512. out += indent(depth);
  513. out += "(";
  514. out += "then";
  515. depth++;
  516. n.consequent.forEach(function (i) {
  517. if (compact === false) {
  518. out += "\n";
  519. }
  520. out += indent(depth);
  521. out += printInstruction(i, depth + 1);
  522. });
  523. depth--;
  524. if (compact === false) {
  525. out += "\n";
  526. out += indent(depth);
  527. }
  528. out += ")";
  529. } else {
  530. if (compact === false) {
  531. out += "\n";
  532. out += indent(depth);
  533. }
  534. out += "(";
  535. out += "then";
  536. out += ")";
  537. }
  538. if (n.alternate.length > 0) {
  539. if (compact === false) {
  540. out += "\n";
  541. }
  542. out += indent(depth);
  543. out += "(";
  544. out += "else";
  545. depth++;
  546. n.alternate.forEach(function (i) {
  547. if (compact === false) {
  548. out += "\n";
  549. }
  550. out += indent(depth);
  551. out += printInstruction(i, depth + 1);
  552. });
  553. depth--;
  554. if (compact === false) {
  555. out += "\n";
  556. out += indent(depth);
  557. }
  558. out += ")";
  559. } else {
  560. if (compact === false) {
  561. out += "\n";
  562. out += indent(depth);
  563. }
  564. out += "(";
  565. out += "else";
  566. out += ")";
  567. }
  568. if (compact === false) {
  569. out += "\n";
  570. out += indent(depth - 1);
  571. }
  572. out += ")";
  573. return out;
  574. }
  575. function printBlockInstruction(n, depth) {
  576. var out = "";
  577. out += "(";
  578. out += "block";
  579. if (n.label != null && (0, _ast.isAnonymous)(n.label) === false) {
  580. out += space;
  581. out += printIdentifier(n.label);
  582. }
  583. if (typeof n.result === "string") {
  584. out += space;
  585. out += "(";
  586. out += "result";
  587. out += space;
  588. out += n.result;
  589. out += ")";
  590. }
  591. if (n.instr.length > 0) {
  592. n.instr.forEach(function (i) {
  593. if (compact === false) {
  594. out += "\n";
  595. }
  596. out += indent(depth);
  597. out += printInstruction(i, depth + 1);
  598. });
  599. if (compact === false) {
  600. out += "\n";
  601. }
  602. out += indent(depth - 1);
  603. out += ")";
  604. } else {
  605. out += ")";
  606. }
  607. return out;
  608. }
  609. function printGenericInstruction(n, depth) {
  610. var out = "";
  611. out += "(";
  612. if (typeof n.object === "string") {
  613. out += n.object;
  614. out += ".";
  615. }
  616. out += n.id;
  617. n.args.forEach(function (arg) {
  618. out += space;
  619. out += printFuncInstructionArg(arg, depth + 1);
  620. });
  621. out += ")";
  622. return out;
  623. }
  624. function printLongNumberLiteral(n) {
  625. if (typeof n.raw === "string") {
  626. return n.raw;
  627. }
  628. var _n$value = n.value,
  629. low = _n$value.low,
  630. high = _n$value.high;
  631. var v = new _long.default(low, high);
  632. return v.toString();
  633. }
  634. function printFloatLiteral(n) {
  635. if (typeof n.raw === "string") {
  636. return n.raw;
  637. }
  638. return String(n.value);
  639. }
  640. function printFuncInstructionArg(n, depth) {
  641. var out = "";
  642. if (n.type === "NumberLiteral") {
  643. out += printNumberLiteral(n);
  644. }
  645. if (n.type === "LongNumberLiteral") {
  646. out += printLongNumberLiteral(n);
  647. }
  648. if (n.type === "Identifier" && (0, _ast.isAnonymous)(n) === false) {
  649. out += printIdentifier(n);
  650. }
  651. if (n.type === "ValtypeLiteral") {
  652. out += n.name;
  653. }
  654. if (n.type === "FloatLiteral") {
  655. out += printFloatLiteral(n);
  656. }
  657. if ((0, _ast.isInstruction)(n)) {
  658. out += printInstruction(n, depth + 1);
  659. }
  660. return out;
  661. }
  662. function printNumberLiteral(n) {
  663. if (typeof n.raw === "string") {
  664. return n.raw;
  665. }
  666. return String(n.value);
  667. }
  668. function printModuleExport(n) {
  669. var out = "";
  670. out += "(";
  671. out += "export";
  672. out += space;
  673. out += quote(n.name);
  674. if (n.descr.exportType === "Func") {
  675. out += space;
  676. out += "(";
  677. out += "func";
  678. out += space;
  679. out += printIndex(n.descr.id);
  680. out += ")";
  681. } else if (n.descr.exportType === "Global") {
  682. out += space;
  683. out += "(";
  684. out += "global";
  685. out += space;
  686. out += printIndex(n.descr.id);
  687. out += ")";
  688. } else if (n.descr.exportType === "Memory" || n.descr.exportType === "Mem") {
  689. out += space;
  690. out += "(";
  691. out += "memory";
  692. out += space;
  693. out += printIndex(n.descr.id);
  694. out += ")";
  695. } else if (n.descr.exportType === "Table") {
  696. out += space;
  697. out += "(";
  698. out += "table";
  699. out += space;
  700. out += printIndex(n.descr.id);
  701. out += ")";
  702. } else {
  703. throw new Error("printModuleExport: unknown type: " + n.descr.exportType);
  704. }
  705. out += ")";
  706. return out;
  707. }
  708. function printIdentifier(n) {
  709. return "$" + n.value;
  710. }
  711. function printIndex(n) {
  712. if (n.type === "Identifier") {
  713. return printIdentifier(n);
  714. } else if (n.type === "NumberLiteral") {
  715. return printNumberLiteral(n);
  716. } else {
  717. throw new Error("Unsupported index: " + n.type);
  718. }
  719. }
  720. function printMemory(n) {
  721. var out = "";
  722. out += "(";
  723. out += "memory";
  724. if (n.id != null) {
  725. out += space;
  726. out += printIndex(n.id);
  727. out += space;
  728. }
  729. out += printLimit(n.limits);
  730. out += ")";
  731. return out;
  732. }
  733. function printLimit(n) {
  734. var out = "";
  735. out += n.min + "";
  736. if (n.max != null) {
  737. out += space;
  738. out += String(n.max);
  739. }
  740. return out;
  741. }