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