| 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 { isAnonymous, isInstruction } from "@webassemblyjs/ast";
|
|---|
| 16 | import Long from "@xtuc/long";
|
|---|
| 17 | var compact = false;
|
|---|
| 18 | var space = " ";
|
|---|
| 19 |
|
|---|
| 20 | var quote = function quote(str) {
|
|---|
| 21 | return "\"".concat(str, "\"");
|
|---|
| 22 | };
|
|---|
| 23 |
|
|---|
| 24 | function indent(nb) {
|
|---|
| 25 | return Array(nb).fill(space + space).join("");
|
|---|
| 26 | } // TODO(sven): allow arbitrary ast nodes
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 | export function print(n) {
|
|---|
| 30 | if (n.type === "Program") {
|
|---|
| 31 | return printProgram(n, 0);
|
|---|
| 32 | } else {
|
|---|
| 33 | throw new Error("Unsupported node in print of type: " + String(n.type));
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | function printProgram(n, depth) {
|
|---|
| 38 | return n.body.reduce(function (acc, child) {
|
|---|
| 39 | if (child.type === "Module") {
|
|---|
| 40 | acc += printModule(child, depth + 1);
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | if (child.type === "Func") {
|
|---|
| 44 | acc += printFunc(child, depth + 1);
|
|---|
| 45 | }
|
|---|
| 46 |
|
|---|
| 47 | if (child.type === "BlockComment") {
|
|---|
| 48 | acc += printBlockComment(child);
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | if (child.type === "LeadingComment") {
|
|---|
| 52 | acc += printLeadingComment(child);
|
|---|
| 53 | }
|
|---|
| 54 |
|
|---|
| 55 | if (compact === false) {
|
|---|
| 56 | acc += "\n";
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | return acc;
|
|---|
| 60 | }, "");
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | function printTypeInstruction(n) {
|
|---|
| 64 | var out = "";
|
|---|
| 65 | out += "(";
|
|---|
| 66 | out += "type";
|
|---|
| 67 | out += space;
|
|---|
| 68 |
|
|---|
| 69 | if (n.id != null) {
|
|---|
| 70 | out += printIndex(n.id);
|
|---|
| 71 | out += space;
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | out += "(";
|
|---|
| 75 | out += "func";
|
|---|
| 76 | n.functype.params.forEach(function (param) {
|
|---|
| 77 | out += space;
|
|---|
| 78 | out += "(";
|
|---|
| 79 | out += "param";
|
|---|
| 80 | out += space;
|
|---|
| 81 | out += printFuncParam(param);
|
|---|
| 82 | out += ")";
|
|---|
| 83 | });
|
|---|
| 84 | n.functype.results.forEach(function (result) {
|
|---|
| 85 | out += space;
|
|---|
| 86 | out += "(";
|
|---|
| 87 | out += "result";
|
|---|
| 88 | out += space;
|
|---|
| 89 | out += result;
|
|---|
| 90 | out += ")";
|
|---|
| 91 | });
|
|---|
| 92 | out += ")"; // func
|
|---|
| 93 |
|
|---|
| 94 | out += ")";
|
|---|
| 95 | return out;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | function printModule(n, depth) {
|
|---|
| 99 | var out = "(";
|
|---|
| 100 | out += "module";
|
|---|
| 101 |
|
|---|
| 102 | if (typeof n.id === "string") {
|
|---|
| 103 | out += space;
|
|---|
| 104 | out += n.id;
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | if (compact === false) {
|
|---|
| 108 | out += "\n";
|
|---|
| 109 | } else {
|
|---|
| 110 | out += space;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | n.fields.forEach(function (field) {
|
|---|
| 114 | if (compact === false) {
|
|---|
| 115 | out += indent(depth);
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | switch (field.type) {
|
|---|
| 119 | case "Func":
|
|---|
| 120 | {
|
|---|
| 121 | out += printFunc(field, depth + 1);
|
|---|
| 122 | break;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | case "TypeInstruction":
|
|---|
| 126 | {
|
|---|
| 127 | out += printTypeInstruction(field);
|
|---|
| 128 | break;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | case "Table":
|
|---|
| 132 | {
|
|---|
| 133 | out += printTable(field);
|
|---|
| 134 | break;
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | case "Global":
|
|---|
| 138 | {
|
|---|
| 139 | out += printGlobal(field, depth + 1);
|
|---|
| 140 | break;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | case "ModuleExport":
|
|---|
| 144 | {
|
|---|
| 145 | out += printModuleExport(field);
|
|---|
| 146 | break;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | case "ModuleImport":
|
|---|
| 150 | {
|
|---|
| 151 | out += printModuleImport(field);
|
|---|
| 152 | break;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | case "Memory":
|
|---|
| 156 | {
|
|---|
| 157 | out += printMemory(field);
|
|---|
| 158 | break;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | case "BlockComment":
|
|---|
| 162 | {
|
|---|
| 163 | out += printBlockComment(field);
|
|---|
| 164 | break;
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | case "LeadingComment":
|
|---|
| 168 | {
|
|---|
| 169 | out += printLeadingComment(field);
|
|---|
| 170 | break;
|
|---|
| 171 | }
|
|---|
| 172 |
|
|---|
| 173 | case "Start":
|
|---|
| 174 | {
|
|---|
| 175 | out += printStart(field);
|
|---|
| 176 | break;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | case "Elem":
|
|---|
| 180 | {
|
|---|
| 181 | out += printElem(field, depth);
|
|---|
| 182 | break;
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | case "Data":
|
|---|
| 186 | {
|
|---|
| 187 | out += printData(field, depth);
|
|---|
| 188 | break;
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | default:
|
|---|
| 192 | throw new Error("Unsupported node in printModule: " + String(field.type));
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | if (compact === false) {
|
|---|
| 196 | out += "\n";
|
|---|
| 197 | }
|
|---|
| 198 | });
|
|---|
| 199 | out += ")";
|
|---|
| 200 | return out;
|
|---|
| 201 | }
|
|---|
| 202 |
|
|---|
| 203 | function printData(n, depth) {
|
|---|
| 204 | var out = "";
|
|---|
| 205 | out += "(";
|
|---|
| 206 | out += "data";
|
|---|
| 207 | out += space;
|
|---|
| 208 | out += printIndex(n.memoryIndex);
|
|---|
| 209 | out += space;
|
|---|
| 210 | out += printInstruction(n.offset, depth);
|
|---|
| 211 | out += space;
|
|---|
| 212 | out += '"';
|
|---|
| 213 | n.init.values.forEach(function (_byte) {
|
|---|
| 214 | // Avoid non-displayable characters
|
|---|
| 215 | if (_byte <= 31 || _byte == 34 || _byte == 92 || _byte >= 127) {
|
|---|
| 216 | out += "\\";
|
|---|
| 217 | out += ("00" + _byte.toString(16)).substr(-2);
|
|---|
| 218 | } else if (_byte > 255) {
|
|---|
| 219 | throw new Error("Unsupported byte in data segment: " + _byte);
|
|---|
| 220 | } else {
|
|---|
| 221 | out += String.fromCharCode(_byte);
|
|---|
| 222 | }
|
|---|
| 223 | });
|
|---|
| 224 | out += '"';
|
|---|
| 225 | out += ")";
|
|---|
| 226 | return out;
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | function printElem(n, depth) {
|
|---|
| 230 | var out = "";
|
|---|
| 231 | out += "(";
|
|---|
| 232 | out += "elem";
|
|---|
| 233 | out += space;
|
|---|
| 234 | out += printIndex(n.table);
|
|---|
| 235 |
|
|---|
| 236 | var _n$offset = _slicedToArray(n.offset, 1),
|
|---|
| 237 | firstOffset = _n$offset[0];
|
|---|
| 238 |
|
|---|
| 239 | out += space;
|
|---|
| 240 | out += "(";
|
|---|
| 241 | out += "offset";
|
|---|
| 242 | out += space;
|
|---|
| 243 | out += printInstruction(firstOffset, depth);
|
|---|
| 244 | out += ")";
|
|---|
| 245 | n.funcs.forEach(function (func) {
|
|---|
| 246 | out += space;
|
|---|
| 247 | out += printIndex(func);
|
|---|
| 248 | });
|
|---|
| 249 | out += ")";
|
|---|
| 250 | return out;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | function printStart(n) {
|
|---|
| 254 | var out = "";
|
|---|
| 255 | out += "(";
|
|---|
| 256 | out += "start";
|
|---|
| 257 | out += space;
|
|---|
| 258 | out += printIndex(n.index);
|
|---|
| 259 | out += ")";
|
|---|
| 260 | return out;
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | function printLeadingComment(n) {
|
|---|
| 264 | // Don't print leading comments in compact mode
|
|---|
| 265 | if (compact === true) {
|
|---|
| 266 | return "";
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | var out = "";
|
|---|
| 270 | out += ";;";
|
|---|
| 271 | out += n.value;
|
|---|
| 272 | out += "\n";
|
|---|
| 273 | return out;
|
|---|
| 274 | }
|
|---|
| 275 |
|
|---|
| 276 | function printBlockComment(n) {
|
|---|
| 277 | // Don't print block comments in compact mode
|
|---|
| 278 | if (compact === true) {
|
|---|
| 279 | return "";
|
|---|
| 280 | }
|
|---|
| 281 |
|
|---|
| 282 | var out = "";
|
|---|
| 283 | out += "(;";
|
|---|
| 284 | out += n.value;
|
|---|
| 285 | out += ";)";
|
|---|
| 286 | out += "\n";
|
|---|
| 287 | return out;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | function printSignature(n) {
|
|---|
| 291 | var out = "";
|
|---|
| 292 | n.params.forEach(function (param) {
|
|---|
| 293 | out += space;
|
|---|
| 294 | out += "(";
|
|---|
| 295 | out += "param";
|
|---|
| 296 | out += space;
|
|---|
| 297 | out += printFuncParam(param);
|
|---|
| 298 | out += ")";
|
|---|
| 299 | });
|
|---|
| 300 | n.results.forEach(function (result) {
|
|---|
| 301 | out += space;
|
|---|
| 302 | out += "(";
|
|---|
| 303 | out += "result";
|
|---|
| 304 | out += space;
|
|---|
| 305 | out += result;
|
|---|
| 306 | out += ")";
|
|---|
| 307 | });
|
|---|
| 308 | return out;
|
|---|
| 309 | }
|
|---|
| 310 |
|
|---|
| 311 | function printModuleImportDescr(n) {
|
|---|
| 312 | var out = "";
|
|---|
| 313 |
|
|---|
| 314 | if (n.type === "FuncImportDescr") {
|
|---|
| 315 | out += "(";
|
|---|
| 316 | out += "func";
|
|---|
| 317 |
|
|---|
| 318 | if (isAnonymous(n.id) === false) {
|
|---|
| 319 | out += space;
|
|---|
| 320 | out += printIdentifier(n.id);
|
|---|
| 321 | }
|
|---|
| 322 |
|
|---|
| 323 | out += printSignature(n.signature);
|
|---|
| 324 | out += ")";
|
|---|
| 325 | }
|
|---|
| 326 |
|
|---|
| 327 | if (n.type === "GlobalType") {
|
|---|
| 328 | out += "(";
|
|---|
| 329 | out += "global";
|
|---|
| 330 | out += space;
|
|---|
| 331 | out += printGlobalType(n);
|
|---|
| 332 | out += ")";
|
|---|
| 333 | }
|
|---|
| 334 |
|
|---|
| 335 | if (n.type === "Table") {
|
|---|
| 336 | out += printTable(n);
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | return out;
|
|---|
| 340 | }
|
|---|
| 341 |
|
|---|
| 342 | function printModuleImport(n) {
|
|---|
| 343 | var out = "";
|
|---|
| 344 | out += "(";
|
|---|
| 345 | out += "import";
|
|---|
| 346 | out += space;
|
|---|
| 347 | out += quote(n.module);
|
|---|
| 348 | out += space;
|
|---|
| 349 | out += quote(n.name);
|
|---|
| 350 | out += space;
|
|---|
| 351 | out += printModuleImportDescr(n.descr);
|
|---|
| 352 | out += ")";
|
|---|
| 353 | return out;
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | function printGlobalType(n) {
|
|---|
| 357 | var out = "";
|
|---|
| 358 |
|
|---|
| 359 | if (n.mutability === "var") {
|
|---|
| 360 | out += "(";
|
|---|
| 361 | out += "mut";
|
|---|
| 362 | out += space;
|
|---|
| 363 | out += n.valtype;
|
|---|
| 364 | out += ")";
|
|---|
| 365 | } else {
|
|---|
| 366 | out += n.valtype;
|
|---|
| 367 | }
|
|---|
| 368 |
|
|---|
| 369 | return out;
|
|---|
| 370 | }
|
|---|
| 371 |
|
|---|
| 372 | function printGlobal(n, depth) {
|
|---|
| 373 | var out = "";
|
|---|
| 374 | out += "(";
|
|---|
| 375 | out += "global";
|
|---|
| 376 | out += space;
|
|---|
| 377 |
|
|---|
| 378 | if (n.name != null && isAnonymous(n.name) === false) {
|
|---|
| 379 | out += printIdentifier(n.name);
|
|---|
| 380 | out += space;
|
|---|
| 381 | }
|
|---|
| 382 |
|
|---|
| 383 | out += printGlobalType(n.globalType);
|
|---|
| 384 | out += space;
|
|---|
| 385 | n.init.forEach(function (i) {
|
|---|
| 386 | out += printInstruction(i, depth + 1);
|
|---|
| 387 | });
|
|---|
| 388 | out += ")";
|
|---|
| 389 | return out;
|
|---|
| 390 | }
|
|---|
| 391 |
|
|---|
| 392 | function printTable(n) {
|
|---|
| 393 | var out = "";
|
|---|
| 394 | out += "(";
|
|---|
| 395 | out += "table";
|
|---|
| 396 | out += space;
|
|---|
| 397 |
|
|---|
| 398 | if (n.name != null && isAnonymous(n.name) === false) {
|
|---|
| 399 | out += printIdentifier(n.name);
|
|---|
| 400 | out += space;
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | out += printLimit(n.limits);
|
|---|
| 404 | out += space;
|
|---|
| 405 | out += n.elementType;
|
|---|
| 406 | out += ")";
|
|---|
| 407 | return out;
|
|---|
| 408 | }
|
|---|
| 409 |
|
|---|
| 410 | function printFuncParam(n) {
|
|---|
| 411 | var out = "";
|
|---|
| 412 |
|
|---|
| 413 | if (typeof n.id === "string") {
|
|---|
| 414 | out += "$" + n.id;
|
|---|
| 415 | out += space;
|
|---|
| 416 | }
|
|---|
| 417 |
|
|---|
| 418 | out += n.valtype;
|
|---|
| 419 | return out;
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | function printFunc(n, depth) {
|
|---|
| 423 | var out = "";
|
|---|
| 424 | out += "(";
|
|---|
| 425 | out += "func";
|
|---|
| 426 |
|
|---|
| 427 | if (n.name != null) {
|
|---|
| 428 | if (n.name.type === "Identifier" && isAnonymous(n.name) === false) {
|
|---|
| 429 | out += space;
|
|---|
| 430 | out += printIdentifier(n.name);
|
|---|
| 431 | }
|
|---|
| 432 | }
|
|---|
| 433 |
|
|---|
| 434 | if (n.signature.type === "Signature") {
|
|---|
| 435 | out += printSignature(n.signature);
|
|---|
| 436 | } else {
|
|---|
| 437 | var index = n.signature;
|
|---|
| 438 | out += space;
|
|---|
| 439 | out += "(";
|
|---|
| 440 | out += "type";
|
|---|
| 441 | out += space;
|
|---|
| 442 | out += printIndex(index);
|
|---|
| 443 | out += ")";
|
|---|
| 444 | }
|
|---|
| 445 |
|
|---|
| 446 | if (n.body.length > 0) {
|
|---|
| 447 | // func is empty since we ignore the default end instruction
|
|---|
| 448 | if (n.body.length === 1 && n.body[0].id === "end") {
|
|---|
| 449 | out += ")";
|
|---|
| 450 | return out;
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | if (compact === false) {
|
|---|
| 454 | out += "\n";
|
|---|
| 455 | }
|
|---|
| 456 |
|
|---|
| 457 | n.body.forEach(function (i) {
|
|---|
| 458 | if (i.id !== "end") {
|
|---|
| 459 | out += indent(depth);
|
|---|
| 460 | out += printInstruction(i, depth);
|
|---|
| 461 |
|
|---|
| 462 | if (compact === false) {
|
|---|
| 463 | out += "\n";
|
|---|
| 464 | }
|
|---|
| 465 | }
|
|---|
| 466 | });
|
|---|
| 467 | out += indent(depth - 1) + ")";
|
|---|
| 468 | } else {
|
|---|
| 469 | out += ")";
|
|---|
| 470 | }
|
|---|
| 471 |
|
|---|
| 472 | return out;
|
|---|
| 473 | }
|
|---|
| 474 |
|
|---|
| 475 | function printInstruction(n, depth) {
|
|---|
| 476 | switch (n.type) {
|
|---|
| 477 | case "Instr":
|
|---|
| 478 | // $FlowIgnore
|
|---|
| 479 | return printGenericInstruction(n, depth + 1);
|
|---|
| 480 |
|
|---|
| 481 | case "BlockInstruction":
|
|---|
| 482 | // $FlowIgnore
|
|---|
| 483 | return printBlockInstruction(n, depth + 1);
|
|---|
| 484 |
|
|---|
| 485 | case "IfInstruction":
|
|---|
| 486 | // $FlowIgnore
|
|---|
| 487 | return printIfInstruction(n, depth + 1);
|
|---|
| 488 |
|
|---|
| 489 | case "CallInstruction":
|
|---|
| 490 | // $FlowIgnore
|
|---|
| 491 | return printCallInstruction(n, depth + 1);
|
|---|
| 492 |
|
|---|
| 493 | case "CallIndirectInstruction":
|
|---|
| 494 | // $FlowIgnore
|
|---|
| 495 | return printCallIndirectIntruction(n, depth + 1);
|
|---|
| 496 |
|
|---|
| 497 | case "LoopInstruction":
|
|---|
| 498 | // $FlowIgnore
|
|---|
| 499 | return printLoopInstruction(n, depth + 1);
|
|---|
| 500 |
|
|---|
| 501 | default:
|
|---|
| 502 | throw new Error("Unsupported instruction: " + JSON.stringify(n.type));
|
|---|
| 503 | }
|
|---|
| 504 | }
|
|---|
| 505 |
|
|---|
| 506 | function printCallIndirectIntruction(n, depth) {
|
|---|
| 507 | var out = "";
|
|---|
| 508 | out += "(";
|
|---|
| 509 | out += "call_indirect";
|
|---|
| 510 |
|
|---|
| 511 | if (n.signature.type === "Signature") {
|
|---|
| 512 | out += printSignature(n.signature);
|
|---|
| 513 | } else if (n.signature.type === "Identifier") {
|
|---|
| 514 | out += space;
|
|---|
| 515 | out += "(";
|
|---|
| 516 | out += "type";
|
|---|
| 517 | out += space;
|
|---|
| 518 | out += printIdentifier(n.signature);
|
|---|
| 519 | out += ")";
|
|---|
| 520 | } else {
|
|---|
| 521 | throw new Error("CallIndirectInstruction: unsupported signature " + JSON.stringify(n.signature.type));
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | out += space;
|
|---|
| 525 |
|
|---|
| 526 | if (n.intrs != null) {
|
|---|
| 527 | // $FlowIgnore
|
|---|
| 528 | n.intrs.forEach(function (i, index) {
|
|---|
| 529 | // $FlowIgnore
|
|---|
| 530 | out += printInstruction(i, depth + 1); // $FlowIgnore
|
|---|
| 531 |
|
|---|
| 532 | if (index !== n.intrs.length - 1) {
|
|---|
| 533 | out += space;
|
|---|
| 534 | }
|
|---|
| 535 | });
|
|---|
| 536 | }
|
|---|
| 537 |
|
|---|
| 538 | out += ")";
|
|---|
| 539 | return out;
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | function printLoopInstruction(n, depth) {
|
|---|
| 543 | var out = "";
|
|---|
| 544 | out += "(";
|
|---|
| 545 | out += "loop";
|
|---|
| 546 |
|
|---|
| 547 | if (n.label != null && isAnonymous(n.label) === false) {
|
|---|
| 548 | out += space;
|
|---|
| 549 | out += printIdentifier(n.label);
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | if (typeof n.resulttype === "string") {
|
|---|
| 553 | out += space;
|
|---|
| 554 | out += "(";
|
|---|
| 555 | out += "result";
|
|---|
| 556 | out += space;
|
|---|
| 557 | out += n.resulttype;
|
|---|
| 558 | out += ")";
|
|---|
| 559 | }
|
|---|
| 560 |
|
|---|
| 561 | if (n.instr.length > 0) {
|
|---|
| 562 | n.instr.forEach(function (e) {
|
|---|
| 563 | if (compact === false) {
|
|---|
| 564 | out += "\n";
|
|---|
| 565 | }
|
|---|
| 566 |
|
|---|
| 567 | out += indent(depth);
|
|---|
| 568 | out += printInstruction(e, depth + 1);
|
|---|
| 569 | });
|
|---|
| 570 |
|
|---|
| 571 | if (compact === false) {
|
|---|
| 572 | out += "\n";
|
|---|
| 573 | out += indent(depth - 1);
|
|---|
| 574 | }
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 | out += ")";
|
|---|
| 578 | return out;
|
|---|
| 579 | }
|
|---|
| 580 |
|
|---|
| 581 | function printCallInstruction(n, depth) {
|
|---|
| 582 | var out = "";
|
|---|
| 583 | out += "(";
|
|---|
| 584 | out += "call";
|
|---|
| 585 | out += space;
|
|---|
| 586 | out += printIndex(n.index);
|
|---|
| 587 |
|
|---|
| 588 | if (_typeof(n.instrArgs) === "object") {
|
|---|
| 589 | // $FlowIgnore
|
|---|
| 590 | n.instrArgs.forEach(function (arg) {
|
|---|
| 591 | out += space;
|
|---|
| 592 | out += printFuncInstructionArg(arg, depth + 1);
|
|---|
| 593 | });
|
|---|
| 594 | }
|
|---|
| 595 |
|
|---|
| 596 | out += ")";
|
|---|
| 597 | return out;
|
|---|
| 598 | }
|
|---|
| 599 |
|
|---|
| 600 | function printIfInstruction(n, depth) {
|
|---|
| 601 | var out = "";
|
|---|
| 602 | out += "(";
|
|---|
| 603 | out += "if";
|
|---|
| 604 |
|
|---|
| 605 | if (n.testLabel != null && isAnonymous(n.testLabel) === false) {
|
|---|
| 606 | out += space;
|
|---|
| 607 | out += printIdentifier(n.testLabel);
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | if (typeof n.result === "string") {
|
|---|
| 611 | out += space;
|
|---|
| 612 | out += "(";
|
|---|
| 613 | out += "result";
|
|---|
| 614 | out += space;
|
|---|
| 615 | out += n.result;
|
|---|
| 616 | out += ")";
|
|---|
| 617 | }
|
|---|
| 618 |
|
|---|
| 619 | if (n.test.length > 0) {
|
|---|
| 620 | out += space;
|
|---|
| 621 | n.test.forEach(function (i) {
|
|---|
| 622 | out += printInstruction(i, depth + 1);
|
|---|
| 623 | });
|
|---|
| 624 | }
|
|---|
| 625 |
|
|---|
| 626 | if (n.consequent.length > 0) {
|
|---|
| 627 | if (compact === false) {
|
|---|
| 628 | out += "\n";
|
|---|
| 629 | }
|
|---|
| 630 |
|
|---|
| 631 | out += indent(depth);
|
|---|
| 632 | out += "(";
|
|---|
| 633 | out += "then";
|
|---|
| 634 | depth++;
|
|---|
| 635 | n.consequent.forEach(function (i) {
|
|---|
| 636 | if (compact === false) {
|
|---|
| 637 | out += "\n";
|
|---|
| 638 | }
|
|---|
| 639 |
|
|---|
| 640 | out += indent(depth);
|
|---|
| 641 | out += printInstruction(i, depth + 1);
|
|---|
| 642 | });
|
|---|
| 643 | depth--;
|
|---|
| 644 |
|
|---|
| 645 | if (compact === false) {
|
|---|
| 646 | out += "\n";
|
|---|
| 647 | out += indent(depth);
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | out += ")";
|
|---|
| 651 | } else {
|
|---|
| 652 | if (compact === false) {
|
|---|
| 653 | out += "\n";
|
|---|
| 654 | out += indent(depth);
|
|---|
| 655 | }
|
|---|
| 656 |
|
|---|
| 657 | out += "(";
|
|---|
| 658 | out += "then";
|
|---|
| 659 | out += ")";
|
|---|
| 660 | }
|
|---|
| 661 |
|
|---|
| 662 | if (n.alternate.length > 0) {
|
|---|
| 663 | if (compact === false) {
|
|---|
| 664 | out += "\n";
|
|---|
| 665 | }
|
|---|
| 666 |
|
|---|
| 667 | out += indent(depth);
|
|---|
| 668 | out += "(";
|
|---|
| 669 | out += "else";
|
|---|
| 670 | depth++;
|
|---|
| 671 | n.alternate.forEach(function (i) {
|
|---|
| 672 | if (compact === false) {
|
|---|
| 673 | out += "\n";
|
|---|
| 674 | }
|
|---|
| 675 |
|
|---|
| 676 | out += indent(depth);
|
|---|
| 677 | out += printInstruction(i, depth + 1);
|
|---|
| 678 | });
|
|---|
| 679 | depth--;
|
|---|
| 680 |
|
|---|
| 681 | if (compact === false) {
|
|---|
| 682 | out += "\n";
|
|---|
| 683 | out += indent(depth);
|
|---|
| 684 | }
|
|---|
| 685 |
|
|---|
| 686 | out += ")";
|
|---|
| 687 | } else {
|
|---|
| 688 | if (compact === false) {
|
|---|
| 689 | out += "\n";
|
|---|
| 690 | out += indent(depth);
|
|---|
| 691 | }
|
|---|
| 692 |
|
|---|
| 693 | out += "(";
|
|---|
| 694 | out += "else";
|
|---|
| 695 | out += ")";
|
|---|
| 696 | }
|
|---|
| 697 |
|
|---|
| 698 | if (compact === false) {
|
|---|
| 699 | out += "\n";
|
|---|
| 700 | out += indent(depth - 1);
|
|---|
| 701 | }
|
|---|
| 702 |
|
|---|
| 703 | out += ")";
|
|---|
| 704 | return out;
|
|---|
| 705 | }
|
|---|
| 706 |
|
|---|
| 707 | function printBlockInstruction(n, depth) {
|
|---|
| 708 | var out = "";
|
|---|
| 709 | out += "(";
|
|---|
| 710 | out += "block";
|
|---|
| 711 |
|
|---|
| 712 | if (n.label != null && isAnonymous(n.label) === false) {
|
|---|
| 713 | out += space;
|
|---|
| 714 | out += printIdentifier(n.label);
|
|---|
| 715 | }
|
|---|
| 716 |
|
|---|
| 717 | if (typeof n.result === "string") {
|
|---|
| 718 | out += space;
|
|---|
| 719 | out += "(";
|
|---|
| 720 | out += "result";
|
|---|
| 721 | out += space;
|
|---|
| 722 | out += n.result;
|
|---|
| 723 | out += ")";
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | if (n.instr.length > 0) {
|
|---|
| 727 | n.instr.forEach(function (i) {
|
|---|
| 728 | if (compact === false) {
|
|---|
| 729 | out += "\n";
|
|---|
| 730 | }
|
|---|
| 731 |
|
|---|
| 732 | out += indent(depth);
|
|---|
| 733 | out += printInstruction(i, depth + 1);
|
|---|
| 734 | });
|
|---|
| 735 |
|
|---|
| 736 | if (compact === false) {
|
|---|
| 737 | out += "\n";
|
|---|
| 738 | }
|
|---|
| 739 |
|
|---|
| 740 | out += indent(depth - 1);
|
|---|
| 741 | out += ")";
|
|---|
| 742 | } else {
|
|---|
| 743 | out += ")";
|
|---|
| 744 | }
|
|---|
| 745 |
|
|---|
| 746 | return out;
|
|---|
| 747 | }
|
|---|
| 748 |
|
|---|
| 749 | function printGenericInstruction(n, depth) {
|
|---|
| 750 | var out = "";
|
|---|
| 751 | out += "(";
|
|---|
| 752 |
|
|---|
| 753 | if (typeof n.object === "string") {
|
|---|
| 754 | out += n.object;
|
|---|
| 755 | out += ".";
|
|---|
| 756 | }
|
|---|
| 757 |
|
|---|
| 758 | out += n.id;
|
|---|
| 759 | n.args.forEach(function (arg) {
|
|---|
| 760 | out += space;
|
|---|
| 761 | out += printFuncInstructionArg(arg, depth + 1);
|
|---|
| 762 | });
|
|---|
| 763 |
|
|---|
| 764 | if (n.namedArgs !== undefined) {
|
|---|
| 765 | for (var key in n.namedArgs) {
|
|---|
| 766 | out += space + key + "=";
|
|---|
| 767 | out += printFuncInstructionArg(n.namedArgs[key], depth + 1);
|
|---|
| 768 | }
|
|---|
| 769 | }
|
|---|
| 770 |
|
|---|
| 771 | out += ")";
|
|---|
| 772 | return out;
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | function printLongNumberLiteral(n) {
|
|---|
| 776 | if (typeof n.raw === "string") {
|
|---|
| 777 | return n.raw;
|
|---|
| 778 | }
|
|---|
| 779 |
|
|---|
| 780 | var _n$value = n.value,
|
|---|
| 781 | low = _n$value.low,
|
|---|
| 782 | high = _n$value.high;
|
|---|
| 783 | var v = new Long(low, high);
|
|---|
| 784 | return v.toString();
|
|---|
| 785 | }
|
|---|
| 786 |
|
|---|
| 787 | function printFloatLiteral(n) {
|
|---|
| 788 | if (typeof n.raw === "string") {
|
|---|
| 789 | return n.raw;
|
|---|
| 790 | }
|
|---|
| 791 |
|
|---|
| 792 | return String(n.value);
|
|---|
| 793 | }
|
|---|
| 794 |
|
|---|
| 795 | function printFuncInstructionArg(n, depth) {
|
|---|
| 796 | var out = "";
|
|---|
| 797 |
|
|---|
| 798 | if (n.type === "NumberLiteral") {
|
|---|
| 799 | out += printNumberLiteral(n);
|
|---|
| 800 | }
|
|---|
| 801 |
|
|---|
| 802 | if (n.type === "LongNumberLiteral") {
|
|---|
| 803 | out += printLongNumberLiteral(n);
|
|---|
| 804 | }
|
|---|
| 805 |
|
|---|
| 806 | if (n.type === "Identifier" && isAnonymous(n) === false) {
|
|---|
| 807 | out += printIdentifier(n);
|
|---|
| 808 | }
|
|---|
| 809 |
|
|---|
| 810 | if (n.type === "ValtypeLiteral") {
|
|---|
| 811 | out += n.name;
|
|---|
| 812 | }
|
|---|
| 813 |
|
|---|
| 814 | if (n.type === "FloatLiteral") {
|
|---|
| 815 | out += printFloatLiteral(n);
|
|---|
| 816 | }
|
|---|
| 817 |
|
|---|
| 818 | if (isInstruction(n)) {
|
|---|
| 819 | out += printInstruction(n, depth + 1);
|
|---|
| 820 | }
|
|---|
| 821 |
|
|---|
| 822 | return out;
|
|---|
| 823 | }
|
|---|
| 824 |
|
|---|
| 825 | function printNumberLiteral(n) {
|
|---|
| 826 | if (typeof n.raw === "string") {
|
|---|
| 827 | return n.raw;
|
|---|
| 828 | }
|
|---|
| 829 |
|
|---|
| 830 | return String(n.value);
|
|---|
| 831 | }
|
|---|
| 832 |
|
|---|
| 833 | function printModuleExport(n) {
|
|---|
| 834 | var out = "";
|
|---|
| 835 | out += "(";
|
|---|
| 836 | out += "export";
|
|---|
| 837 | out += space;
|
|---|
| 838 | out += quote(n.name);
|
|---|
| 839 |
|
|---|
| 840 | if (n.descr.exportType === "Func") {
|
|---|
| 841 | out += space;
|
|---|
| 842 | out += "(";
|
|---|
| 843 | out += "func";
|
|---|
| 844 | out += space;
|
|---|
| 845 | out += printIndex(n.descr.id);
|
|---|
| 846 | out += ")";
|
|---|
| 847 | } else if (n.descr.exportType === "Global") {
|
|---|
| 848 | out += space;
|
|---|
| 849 | out += "(";
|
|---|
| 850 | out += "global";
|
|---|
| 851 | out += space;
|
|---|
| 852 | out += printIndex(n.descr.id);
|
|---|
| 853 | out += ")";
|
|---|
| 854 | } else if (n.descr.exportType === "Memory") {
|
|---|
| 855 | out += space;
|
|---|
| 856 | out += "(";
|
|---|
| 857 | out += "memory";
|
|---|
| 858 | out += space;
|
|---|
| 859 | out += printIndex(n.descr.id);
|
|---|
| 860 | out += ")";
|
|---|
| 861 | } else if (n.descr.exportType === "Table") {
|
|---|
| 862 | out += space;
|
|---|
| 863 | out += "(";
|
|---|
| 864 | out += "table";
|
|---|
| 865 | out += space;
|
|---|
| 866 | out += printIndex(n.descr.id);
|
|---|
| 867 | out += ")";
|
|---|
| 868 | } else {
|
|---|
| 869 | throw new Error("printModuleExport: unknown type: " + n.descr.exportType);
|
|---|
| 870 | }
|
|---|
| 871 |
|
|---|
| 872 | out += ")";
|
|---|
| 873 | return out;
|
|---|
| 874 | }
|
|---|
| 875 |
|
|---|
| 876 | function printIdentifier(n) {
|
|---|
| 877 | return "$" + n.value;
|
|---|
| 878 | }
|
|---|
| 879 |
|
|---|
| 880 | function printIndex(n) {
|
|---|
| 881 | if (n.type === "Identifier") {
|
|---|
| 882 | return printIdentifier(n);
|
|---|
| 883 | } else if (n.type === "NumberLiteral") {
|
|---|
| 884 | return printNumberLiteral(n);
|
|---|
| 885 | } else {
|
|---|
| 886 | throw new Error("Unsupported index: " + n.type);
|
|---|
| 887 | }
|
|---|
| 888 | }
|
|---|
| 889 |
|
|---|
| 890 | function printMemory(n) {
|
|---|
| 891 | var out = "";
|
|---|
| 892 | out += "(";
|
|---|
| 893 | out += "memory";
|
|---|
| 894 |
|
|---|
| 895 | if (n.id != null) {
|
|---|
| 896 | out += space;
|
|---|
| 897 | out += printIndex(n.id);
|
|---|
| 898 | out += space;
|
|---|
| 899 | }
|
|---|
| 900 |
|
|---|
| 901 | out += printLimit(n.limits);
|
|---|
| 902 | out += ")";
|
|---|
| 903 | return out;
|
|---|
| 904 | }
|
|---|
| 905 |
|
|---|
| 906 | function printLimit(n) {
|
|---|
| 907 | var out = "";
|
|---|
| 908 | out += n.min + "";
|
|---|
| 909 |
|
|---|
| 910 | if (n.max != null) {
|
|---|
| 911 | out += space;
|
|---|
| 912 | out += String(n.max);
|
|---|
| 913 |
|
|---|
| 914 | if (n.shared === true) {
|
|---|
| 915 | out += " shared";
|
|---|
| 916 | }
|
|---|
| 917 | }
|
|---|
| 918 |
|
|---|
| 919 | return out;
|
|---|
| 920 | } |
|---|