1 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); }
3 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; }
5 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"); } }
7 import { isBlock, isFunc, isIdentifier, numberLiteralFromRaw, traverse } from "../../index";
8 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);
14 export function transform(ast) {
17 Module: function (_Module) {
19 return _Module.apply(this, arguments);
22 Module.toString = function () {
23 return _Module.toString();
31 var moduleContext = moduleContextFromModuleAST(module); // Transform the actual instruction in function bodies
34 Func: function (_Func) {
36 return _Func.apply(this, arguments);
39 Func.toString = function () {
40 return _Func.toString();
45 transformFuncPath(path, moduleContext);
47 Start: function (_Start) {
49 return _Start.apply(this, arguments);
52 Start.toString = function () {
53 return _Start.toString();
58 var index = path.node.index;
60 if (isIdentifier(index) === true) {
61 var offsetInModule = moduleContext.getFunctionOffsetByIdentifier(index.value);
63 if (typeof offsetInModule === "undefined") {
64 throw newUnexpectedFunction(index.value);
65 } // Replace the index Identifier
66 // $FlowIgnore: reference?
69 path.node.index = numberLiteralFromRaw(offsetInModule);
75 function transformFuncPath(funcPath, moduleContext) {
76 var funcNode = funcPath.node;
77 var signature = funcNode.signature;
79 if (signature.type !== "Signature") {
80 throw new Error("Function signatures must be denormalised before execution");
83 var params = signature.params; // Add func locals in the context
85 params.forEach(function (p) {
86 return moduleContext.addLocal(p.valtype);
89 Instr: function (_Instr) {
91 return _Instr.apply(this, arguments);
94 Instr.toString = function () {
95 return _Instr.toString();
99 }(function (instrPath) {
100 var instrNode = instrPath.node;
105 if (instrNode.id === "get_local" || instrNode.id === "set_local" || instrNode.id === "tee_local") {
106 var _instrNode$args = _slicedToArray(instrNode.args, 1),
107 firstArg = _instrNode$args[0];
109 if (firstArg.type === "Identifier") {
110 var offsetInParams = params.findIndex(function (_ref) {
112 return id === firstArg.value;
115 if (offsetInParams === -1) {
116 throw new Error("".concat(firstArg.value, " not found in ").concat(instrNode.id, ": not declared in func params"));
117 } // Replace the Identifer node by our new NumberLiteral node
120 instrNode.args[0] = numberLiteralFromRaw(offsetInParams);
128 if (instrNode.id === "get_global" || instrNode.id === "set_global") {
129 var _instrNode$args2 = _slicedToArray(instrNode.args, 1),
130 _firstArg = _instrNode$args2[0];
132 if (isIdentifier(_firstArg) === true) {
133 var globalOffset = moduleContext.getGlobalOffsetByIdentifier( // $FlowIgnore: reference?
136 if (typeof globalOffset === "undefined") {
137 // $FlowIgnore: reference?
138 throw new Error("global ".concat(_firstArg.value, " not found in module"));
139 } // Replace the Identifer node by our new NumberLiteral node
142 instrNode.args[0] = numberLiteralFromRaw(globalOffset);
150 if (instrNode.id === "br") {
151 var _instrNode$args3 = _slicedToArray(instrNode.args, 1),
152 _firstArg2 = _instrNode$args3[0];
154 if (isIdentifier(_firstArg2) === true) {
155 // if the labels is not found it is going to be replaced with -1
157 var relativeBlockCount = -1; // $FlowIgnore: reference?
159 instrPath.findParent(function (_ref2) {
160 var node = _ref2.node;
163 relativeBlockCount++; // $FlowIgnore: reference?
165 var name = node.label || node.name;
167 if (_typeof(name) === "object") {
168 // $FlowIgnore: isIdentifier ensures that
169 if (name.value === _firstArg2.value) {
179 }); // Replace the Identifer node by our new NumberLiteral node
181 instrNode.args[0] = numberLiteralFromRaw(relativeBlockCount);
189 CallInstruction: function (_CallInstruction) {
190 function CallInstruction(_x5) {
191 return _CallInstruction.apply(this, arguments);
194 CallInstruction.toString = function () {
195 return _CallInstruction.toString();
198 return CallInstruction;
200 var node = _ref3.node;
201 var index = node.index;
203 if (isIdentifier(index) === true) {
204 var offsetInModule = moduleContext.getFunctionOffsetByIdentifier(index.value);
206 if (typeof offsetInModule === "undefined") {
207 throw newUnexpectedFunction(index.value);
208 } // Replace the index Identifier
209 // $FlowIgnore: reference?
212 node.index = numberLiteralFromRaw(offsetInModule);