index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. 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); }
  2. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  3. 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."); }
  4. 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); }
  5. 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; }
  6. 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; }
  7. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  8. import { isBlock, isFunc, isIdentifier, numberLiteralFromRaw, traverse } from "../../index";
  9. import { moduleContextFromModuleAST } from "../ast-module-to-module-context"; // FIXME(sven): do the same with all block instructions, must be more generic here
  10. function newUnexpectedFunction(i) {
  11. return new Error("unknown function at offset: " + i);
  12. }
  13. export function transform(ast) {
  14. var module = null;
  15. traverse(ast, {
  16. Module: function (_Module) {
  17. function Module(_x) {
  18. return _Module.apply(this, arguments);
  19. }
  20. Module.toString = function () {
  21. return _Module.toString();
  22. };
  23. return Module;
  24. }(function (path) {
  25. module = path.node;
  26. })
  27. });
  28. if (module == null) {
  29. throw new Error("Module not foudn in program");
  30. }
  31. var moduleContext = moduleContextFromModuleAST(module); // Transform the actual instruction in function bodies
  32. traverse(ast, {
  33. Func: function (_Func) {
  34. function Func(_x2) {
  35. return _Func.apply(this, arguments);
  36. }
  37. Func.toString = function () {
  38. return _Func.toString();
  39. };
  40. return Func;
  41. }(function (path) {
  42. transformFuncPath(path, moduleContext);
  43. }),
  44. Start: function (_Start) {
  45. function Start(_x3) {
  46. return _Start.apply(this, arguments);
  47. }
  48. Start.toString = function () {
  49. return _Start.toString();
  50. };
  51. return Start;
  52. }(function (path) {
  53. var index = path.node.index;
  54. if (isIdentifier(index) === true) {
  55. var offsetInModule = moduleContext.getFunctionOffsetByIdentifier(index.value);
  56. if (typeof offsetInModule === "undefined") {
  57. throw newUnexpectedFunction(index.value);
  58. } // Replace the index Identifier
  59. // $FlowIgnore: reference?
  60. path.node.index = numberLiteralFromRaw(offsetInModule);
  61. }
  62. })
  63. });
  64. }
  65. function transformFuncPath(funcPath, moduleContext) {
  66. var funcNode = funcPath.node;
  67. var signature = funcNode.signature;
  68. if (signature.type !== "Signature") {
  69. throw new Error("Function signatures must be denormalised before execution");
  70. }
  71. var params = signature.params; // Add func locals in the context
  72. params.forEach(function (p) {
  73. return moduleContext.addLocal(p.valtype);
  74. });
  75. traverse(funcNode, {
  76. Instr: function (_Instr) {
  77. function Instr(_x4) {
  78. return _Instr.apply(this, arguments);
  79. }
  80. Instr.toString = function () {
  81. return _Instr.toString();
  82. };
  83. return Instr;
  84. }(function (instrPath) {
  85. var instrNode = instrPath.node;
  86. /**
  87. * Local access
  88. */
  89. if (instrNode.id === "get_local" || instrNode.id === "set_local" || instrNode.id === "tee_local") {
  90. var _instrNode$args = _slicedToArray(instrNode.args, 1),
  91. firstArg = _instrNode$args[0];
  92. if (firstArg.type === "Identifier") {
  93. var offsetInParams = params.findIndex(function (_ref) {
  94. var id = _ref.id;
  95. return id === firstArg.value;
  96. });
  97. if (offsetInParams === -1) {
  98. throw new Error("".concat(firstArg.value, " not found in ").concat(instrNode.id, ": not declared in func params"));
  99. } // Replace the Identifer node by our new NumberLiteral node
  100. instrNode.args[0] = numberLiteralFromRaw(offsetInParams);
  101. }
  102. }
  103. /**
  104. * Global access
  105. */
  106. if (instrNode.id === "get_global" || instrNode.id === "set_global") {
  107. var _instrNode$args2 = _slicedToArray(instrNode.args, 1),
  108. _firstArg = _instrNode$args2[0];
  109. if (isIdentifier(_firstArg) === true) {
  110. var globalOffset = moduleContext.getGlobalOffsetByIdentifier( // $FlowIgnore: reference?
  111. _firstArg.value);
  112. if (typeof globalOffset === "undefined") {
  113. // $FlowIgnore: reference?
  114. throw new Error("global ".concat(_firstArg.value, " not found in module"));
  115. } // Replace the Identifer node by our new NumberLiteral node
  116. instrNode.args[0] = numberLiteralFromRaw(globalOffset);
  117. }
  118. }
  119. /**
  120. * Labels lookup
  121. */
  122. if (instrNode.id === "br") {
  123. var _instrNode$args3 = _slicedToArray(instrNode.args, 1),
  124. _firstArg2 = _instrNode$args3[0];
  125. if (isIdentifier(_firstArg2) === true) {
  126. // if the labels is not found it is going to be replaced with -1
  127. // which is invalid.
  128. var relativeBlockCount = -1; // $FlowIgnore: reference?
  129. instrPath.findParent(function (_ref2) {
  130. var node = _ref2.node;
  131. if (isBlock(node)) {
  132. relativeBlockCount++; // $FlowIgnore: reference?
  133. var name = node.label || node.name;
  134. if (_typeof(name) === "object") {
  135. // $FlowIgnore: isIdentifier ensures that
  136. if (name.value === _firstArg2.value) {
  137. // Found it
  138. return false;
  139. }
  140. }
  141. }
  142. if (isFunc(node)) {
  143. return false;
  144. }
  145. }); // Replace the Identifer node by our new NumberLiteral node
  146. instrNode.args[0] = numberLiteralFromRaw(relativeBlockCount);
  147. }
  148. }
  149. }),
  150. /**
  151. * Func lookup
  152. */
  153. CallInstruction: function (_CallInstruction) {
  154. function CallInstruction(_x5) {
  155. return _CallInstruction.apply(this, arguments);
  156. }
  157. CallInstruction.toString = function () {
  158. return _CallInstruction.toString();
  159. };
  160. return CallInstruction;
  161. }(function (_ref3) {
  162. var node = _ref3.node;
  163. var index = node.index;
  164. if (isIdentifier(index) === true) {
  165. var offsetInModule = moduleContext.getFunctionOffsetByIdentifier(index.value);
  166. if (typeof offsetInModule === "undefined") {
  167. throw newUnexpectedFunction(index.value);
  168. } // Replace the index Identifier
  169. // $FlowIgnore: reference?
  170. node.index = numberLiteralFromRaw(offsetInModule);
  171. }
  172. })
  173. });
  174. }