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