summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax Graey <maxgraey@gmail.com>2022-07-29 07:15:35 +0300
committerGitHub <noreply@github.com>2022-07-29 04:15:35 +0000
commitd02c260619e5d068b6893d4948de0487d0f1f66d (patch)
treebeba5a15fb0ace5bd34a4135efecc54deb754bc6 /src
parent0cd9fb599fc5a44df7774d5f180d912ccab8c941 (diff)
downloadbinaryen-d02c260619e5d068b6893d4948de0487d0f1f66d.tar.gz
binaryen-d02c260619e5d068b6893d4948de0487d0f1f66d.tar.bz2
binaryen-d02c260619e5d068b6893d4948de0487d0f1f66d.zip
[JS Api] Reuse C-Api for emitText and emitStackIR (#4832)
Make the C API match the JS API and fix an old bug where extra newlines were emitted.
Diffstat (limited to 'src')
-rw-r--r--src/binaryen-c.cpp45
-rw-r--r--src/binaryen-c.h8
-rw-r--r--src/js/binaryen.js-post.js22
-rw-r--r--src/passes/Print.cpp7
-rw-r--r--src/wasm-stack.h2
5 files changed, 43 insertions, 41 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index 682272201..68158cdf7 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -4009,8 +4009,8 @@ void BinaryenModulePrint(BinaryenModuleRef module) {
std::cout << *(Module*)module;
}
-void BinaryenModulePrintStackIR(BinaryenModuleRef module) {
- wasm::printStackIR(std::cout, (Module*)module);
+void BinaryenModulePrintStackIR(BinaryenModuleRef module, bool optimize) {
+ wasm::printStackIR(std::cout, (Module*)module, optimize);
}
void BinaryenModulePrintAsmjs(BinaryenModuleRef module) {
@@ -4197,11 +4197,12 @@ size_t BinaryenModuleWriteText(BinaryenModuleRef module,
size_t BinaryenModuleWriteStackIR(BinaryenModuleRef module,
char* output,
- size_t outputSize) {
+ size_t outputSize,
+ bool optimize) {
// use a stringstream as an std::ostream. Extract the std::string
// representation, and then store in the output.
std::stringstream ss;
- wasm::printStackIR(ss, (Module*)module);
+ wasm::printStackIR(ss, (Module*)module, optimize);
const auto temp = ss.str();
const auto ctemp = temp.c_str();
@@ -4238,40 +4239,42 @@ BinaryenModuleAllocateAndWrite(BinaryenModuleRef module,
char* sourceMap = nullptr;
if (sourceMapUrl) {
auto str = os.str();
- sourceMap = (char*)malloc(str.length() + 1);
- std::copy_n(str.c_str(), str.length() + 1, sourceMap);
+ const size_t len = str.length() + 1;
+ sourceMap = (char*)malloc(len);
+ std::copy_n(str.c_str(), len, sourceMap);
}
return {binary, buffer.size(), sourceMap};
}
char* BinaryenModuleAllocateAndWriteText(BinaryenModuleRef module) {
- std::stringstream ss;
+ std::ostringstream os;
bool colors = Colors::isEnabled();
Colors::setEnabled(false); // do not use colors for writing
- ss << *(Module*)module;
+ os << *(Module*)module;
Colors::setEnabled(colors); // restore colors state
- const std::string out = ss.str();
- const int len = out.length() + 1;
- char* cout = (char*)malloc(len);
- strncpy(cout, out.c_str(), len);
- return cout;
+ auto str = os.str();
+ const size_t len = str.length() + 1;
+ char* output = (char*)malloc(len);
+ std::copy_n(str.c_str(), len, output);
+ return output;
}
-char* BinaryenModuleAllocateAndWriteStackIR(BinaryenModuleRef module) {
- std::stringstream ss;
+char* BinaryenModuleAllocateAndWriteStackIR(BinaryenModuleRef module,
+ bool optimize) {
+ std::ostringstream os;
bool colors = Colors::isEnabled();
Colors::setEnabled(false); // do not use colors for writing
- wasm::printStackIR(ss, (Module*)module);
+ wasm::printStackIR(os, (Module*)module, optimize);
Colors::setEnabled(colors); // restore colors state
- const std::string out = ss.str();
- const int len = out.length() + 1;
- char* cout = (char*)malloc(len);
- strncpy(cout, out.c_str(), len);
- return cout;
+ auto str = os.str();
+ const size_t len = str.length() + 1;
+ char* output = (char*)malloc(len);
+ std::copy_n(str.c_str(), len, output);
+ return output;
}
BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize) {
diff --git a/src/binaryen-c.h b/src/binaryen-c.h
index f7825b080..4f74eef4e 100644
--- a/src/binaryen-c.h
+++ b/src/binaryen-c.h
@@ -2363,7 +2363,8 @@ BINARYEN_API BinaryenModuleRef BinaryenModuleParse(const char* text);
BINARYEN_API void BinaryenModulePrint(BinaryenModuleRef module);
// Print a module to stdout in stack IR text format. Useful for debugging.
-BINARYEN_API void BinaryenModulePrintStackIR(BinaryenModuleRef module);
+BINARYEN_API void BinaryenModulePrintStackIR(BinaryenModuleRef module,
+ bool optimize);
// Print a module to stdout in asm.js syntax.
BINARYEN_API void BinaryenModulePrintAsmjs(BinaryenModuleRef module);
@@ -2504,7 +2505,8 @@ BINARYEN_API size_t BinaryenModuleWriteText(BinaryenModuleRef module,
// outputSize
BINARYEN_API size_t BinaryenModuleWriteStackIR(BinaryenModuleRef module,
char* output,
- size_t outputSize);
+ size_t outputSize,
+ bool optimize);
typedef struct BinaryenBufferSizes {
size_t outputBytes;
@@ -2550,7 +2552,7 @@ BINARYEN_API char* BinaryenModuleAllocateAndWriteText(BinaryenModuleRef module);
// char* with malloc(), and expects the user to free() them manually
// once not needed anymore.
BINARYEN_API char*
-BinaryenModuleAllocateAndWriteStackIR(BinaryenModuleRef module);
+BinaryenModuleAllocateAndWriteStackIR(BinaryenModuleRef module, bool optimize);
// Deserialize a module from binary form.
BINARYEN_API BinaryenModuleRef BinaryenModuleRead(char* input,
diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js
index 9e1534070..d2a8eb7f5 100644
--- a/src/js/binaryen.js-post.js
+++ b/src/js/binaryen.js-post.js
@@ -2616,22 +2616,16 @@ function wrapModule(module, self = {}) {
return Module['_BinaryenGetElementSegmentByIndex'](module, index);
};
self['emitText'] = function() {
- const old = out;
- let ret = '';
- out = x => { ret += x + '\n' };
- Module['_BinaryenModulePrint'](module);
- out = old;
- return ret;
+ let textPtr = Module['_BinaryenModuleAllocateAndWriteText'](module);
+ let text = UTF8ToString(textPtr);
+ if (textPtr) _free(textPtr);
+ return text;
};
self['emitStackIR'] = function(optimize) {
- self['runPasses'](['generate-stack-ir']);
- if (optimize) self['runPasses'](['optimize-stack-ir']);
- const old = out;
- let ret = '';
- out = x => { ret += x + '\n' };
- self['runPasses'](['print-stack-ir']);
- out = old;
- return ret;
+ let textPtr = Module['_BinaryenModuleAllocateAndWriteStackIR'](module, optimize);
+ let text = UTF8ToString(textPtr);
+ if (textPtr) _free(textPtr);
+ return text;
};
self['emitAsmjs'] = function() {
const old = out;
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index a6a0f7919..797ce46b1 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -3627,15 +3627,18 @@ printStackIR(StackIR* ir, std::ostream& o, Function* func) {
default:
WASM_UNREACHABLE("unexpeted op");
}
- std::cout << '\n';
+ o << '\n';
}
assert(controlFlowDepth == 0);
return o;
}
-std::ostream& printStackIR(std::ostream& o, Module* module) {
+std::ostream& printStackIR(std::ostream& o, Module* module, bool optimize) {
wasm::PassRunner runner(module);
runner.add("generate-stack-ir");
+ if (optimize) {
+ runner.add("optimize-stack-ir");
+ }
runner.add(std::make_unique<PrintStackIR>(&o));
runner.run();
return o;
diff --git a/src/wasm-stack.h b/src/wasm-stack.h
index 6e39f45c6..2a007739d 100644
--- a/src/wasm-stack.h
+++ b/src/wasm-stack.h
@@ -472,7 +472,7 @@ private:
Function* func;
};
-std::ostream& printStackIR(std::ostream& o, Module* module);
+std::ostream& printStackIR(std::ostream& o, Module* module, bool optimize);
} // namespace wasm