summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/binaryen-c.cpp18
-rw-r--r--src/binaryen-c.h5
-rw-r--r--src/js/binaryen.js-post.js8
-rw-r--r--src/wasm.h1
-rw-r--r--src/wasm/wasm.cpp7
-rw-r--r--src/wasm2asm.h10
6 files changed, 42 insertions, 7 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index dfdca516f..2aa0633af 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -29,6 +29,7 @@
#include "wasm-printing.h"
#include "wasm-s-parser.h"
#include "wasm-validator.h"
+#include "wasm2asm.h"
#include "cfg/Relooper.h"
#include "ast_utils.h"
#include "shell-interface.h"
@@ -651,7 +652,7 @@ BinaryenExpressionRef BinaryenDrop(BinaryenModuleRef module, BinaryenExpressionR
}
BinaryenExpressionRef BinaryenReturn(BinaryenModuleRef module, BinaryenExpressionRef value) {
auto* ret = Builder(*((Module*)module)).makeReturn((Expression*)value);
-
+
if (tracing) {
auto id = noteExpression(ret);
std::cout << " expressions[" << id << "] = BinaryenReturn(the_module, expressions[" << expressions[value] << "]);\n";
@@ -932,6 +933,21 @@ void BinaryenModulePrint(BinaryenModuleRef module) {
WasmPrinter::printModule((Module*)module);
}
+void BinaryenModulePrintAsmjs(BinaryenModuleRef module) {
+ if (tracing) {
+ std::cout << " BinaryenModulePrintAsmjs(the_module);\n";
+ }
+
+ Module* wasm = (Module*)module;
+ Wasm2AsmBuilder::Flags builderFlags;
+ Wasm2AsmBuilder wasm2asm(builderFlags);
+ Ref asmjs = wasm2asm.processWasm(wasm);
+ JSPrinter jser(true, true, asmjs);
+ jser.printAst();
+
+ std::cout << jser.buffer;
+}
+
int BinaryenModuleValidate(BinaryenModuleRef module) {
if (tracing) {
std::cout << " BinaryenModuleValidate(the_module);\n";
diff --git a/src/binaryen-c.h b/src/binaryen-c.h
index f37bcf085..9427584fd 100644
--- a/src/binaryen-c.h
+++ b/src/binaryen-c.h
@@ -372,9 +372,12 @@ void BinaryenSetStart(BinaryenModuleRef module, BinaryenFunctionRef start);
// Parse a module in s-expression text format
BinaryenModuleRef BinaryenModuleParse(const char* text);
-// Print a module to stdout. Useful for debugging.
+// Print a module to stdout in s-expression text format. Useful for debugging.
void BinaryenModulePrint(BinaryenModuleRef module);
+// Print a module to stdout in asm.js syntax.
+void BinaryenModulePrintAsmjs(BinaryenModuleRef module);
+
// Validate a module, showing errors on problems.
// @return 0 if an error occurred, 1 if validated succesfully
int BinaryenModuleValidate(BinaryenModuleRef module);
diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js
index cda00742c..985691494 100644
--- a/src/js/binaryen.js-post.js
+++ b/src/js/binaryen.js-post.js
@@ -833,6 +833,14 @@
Module['print'] = old;
return ret;
};
+ this['emitAsmjs'] = function() {
+ var old = Module['print'];
+ var ret = '';
+ Module['print'] = function(x) { ret += x + '\n' };
+ Module['_BinaryenModulePrintAsmjs'](module);
+ Module['print'] = old;
+ return ret;
+ };
this['validate'] = function() {
return Module['_BinaryenModuleValidate'](module);
};
diff --git a/src/wasm.h b/src/wasm.h
index e5b1eaea3..974bf1730 100644
--- a/src/wasm.h
+++ b/src/wasm.h
@@ -586,6 +586,7 @@ public:
WasmType getLocalType(Index index);
Name getLocalNameOrDefault(Index index);
+ Name getLocalNameOrGeneric(Index index);
private:
bool hasLocalName(Index index) const;
diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp
index 635d14354..19ba5be21 100644
--- a/src/wasm/wasm.cpp
+++ b/src/wasm/wasm.cpp
@@ -560,6 +560,13 @@ Name Function::getLocalNameOrDefault(Index index) {
return Name();
}
+Name Function::getLocalNameOrGeneric(Index index) {
+ if (hasLocalName(index)) {
+ return localNames[index];
+ }
+ return Name::fromInt(index);
+}
+
Index Function::getLocalIndex(Name name) {
assert(localIndices.count(name) > 0);
return localIndices[name];
diff --git a/src/wasm2asm.h b/src/wasm2asm.h
index 7ba9d5175..7a760da26 100644
--- a/src/wasm2asm.h
+++ b/src/wasm2asm.h
@@ -523,7 +523,7 @@ Ref Wasm2AsmBuilder::processFunction(Function* func) {
temps[i32] = temps[f32] = temps[f64] = 0;
// arguments
for (Index i = 0; i < func->getNumParams(); i++) {
- IString name = fromName(func->getLocalName(i));
+ IString name = fromName(func->getLocalNameOrGeneric(i));
ValueBuilder::appendArgumentToFunction(ret, name);
ret[3]->push_back(
ValueBuilder::makeStatement(
@@ -559,7 +559,7 @@ Ref Wasm2AsmBuilder::processFunction(Function* func) {
}
// vars, including new temp vars
for (Index i = func->getVarIndexBase(); i < func->getNumLocals(); i++) {
- ValueBuilder::appendToVar(theVar, fromName(func->getLocalName(i)), makeAsmCoercedZero(wasmToAsmType(func->getLocalType(i))));
+ ValueBuilder::appendToVar(theVar, fromName(func->getLocalNameOrGeneric(i)), makeAsmCoercedZero(wasmToAsmType(func->getLocalType(i))));
}
if (theVar[1]->size() == 0) {
ret[3]->splice(theVarIndex, 1);
@@ -928,12 +928,12 @@ Ref Wasm2AsmBuilder::processFunctionBody(Function* func, IString result) {
return makeStatementizedCall(curr->operands, ret, theCall, result, curr->type);
}
Ref visitGetLocal(GetLocal *curr) {
- return ValueBuilder::makeName(fromName(func->getLocalName(curr->index)));
+ return ValueBuilder::makeName(fromName(func->getLocalNameOrGeneric(curr->index)));
}
Ref visitSetLocal(SetLocal *curr) {
if (!isStatement(curr)) {
return ValueBuilder::makeBinary(
- ValueBuilder::makeName(fromName(func->getLocalName(curr->index))),
+ ValueBuilder::makeName(fromName(func->getLocalNameOrGeneric(curr->index))),
SET, visit(curr->value, EXPRESSION_RESULT));
}
ScopedTemp temp(curr->type, parent, func, result); // if result was provided, our child can just assign there. otherwise, allocate a temp for it to assign to.
@@ -941,7 +941,7 @@ Ref Wasm2AsmBuilder::processFunctionBody(Function* func, IString result) {
// the output was assigned to result, so we can just assign it to our target
ret[1]->push_back(ValueBuilder::makeStatement(
ValueBuilder::makeBinary(
- ValueBuilder::makeName(fromName(func->getLocalName(curr->index))),
+ ValueBuilder::makeName(fromName(func->getLocalNameOrGeneric(curr->index))),
SET, temp.getAstName())));
return ret;
}