summaryrefslogtreecommitdiff
path: root/src/passes
diff options
context:
space:
mode:
Diffstat (limited to 'src/passes')
-rw-r--r--src/passes/Print.cpp13
-rw-r--r--src/passes/StringLowering.cpp35
-rw-r--r--src/passes/pass.cpp4
-rw-r--r--src/passes/passes.h1
4 files changed, 37 insertions, 16 deletions
diff --git a/src/passes/Print.cpp b/src/passes/Print.cpp
index 68a2e4cb6..a90ef4669 100644
--- a/src/passes/Print.cpp
+++ b/src/passes/Print.cpp
@@ -2838,8 +2838,9 @@ void PrintSExpression::handleSignature(HeapType curr, Name name) {
void PrintSExpression::visitExport(Export* curr) {
o << '(';
printMedium(o, "export ");
- // TODO: Escape the string properly.
- printText(o, curr->name.str.data()) << " (";
+ std::stringstream escaped;
+ String::printEscaped(escaped, curr->name.str);
+ printText(o, escaped.str(), false) << " (";
switch (curr->kind) {
case ExternalKind::Function:
o << "func";
@@ -2865,9 +2866,11 @@ void PrintSExpression::visitExport(Export* curr) {
void PrintSExpression::emitImportHeader(Importable* curr) {
printMedium(o, "import ");
- // TODO: Escape the strings properly and use std::string_view.
- printText(o, curr->module.str.data()) << ' ';
- printText(o, curr->base.str.data()) << ' ';
+ std::stringstream escapedModule, escapedBase;
+ String::printEscaped(escapedModule, curr->module.str);
+ String::printEscaped(escapedBase, curr->base.str);
+ printText(o, escapedModule.str(), false) << ' ';
+ printText(o, escapedBase.str(), false) << ' ';
}
void PrintSExpression::visitGlobal(Global* curr) {
diff --git a/src/passes/StringLowering.cpp b/src/passes/StringLowering.cpp
index df2d66860..dd7428546 100644
--- a/src/passes/StringLowering.cpp
+++ b/src/passes/StringLowering.cpp
@@ -189,6 +189,13 @@ struct StringGathering : public Pass {
};
struct StringLowering : public StringGathering {
+ // If true, then encode well-formed strings as (import "'" "string...")
+ // instead of emitting them into the JSON custom section.
+ bool useMagicImports;
+
+ StringLowering(bool useMagicImports = false)
+ : useMagicImports(useMagicImports) {}
+
void run(Module* module) override {
if (!module->features.has(FeatureSet::Strings)) {
return;
@@ -217,25 +224,30 @@ struct StringLowering : public StringGathering {
}
void makeImports(Module* module) {
- Index importIndex = 0;
+ Index jsonImportIndex = 0;
std::stringstream json;
json << '[';
bool first = true;
- std::vector<Name> importedStrings;
for (auto& global : module->globals) {
if (global->init) {
if (auto* c = global->init->dynCast<StringConst>()) {
- global->module = "string.const";
- global->base = std::to_string(importIndex);
- importIndex++;
- global->init = nullptr;
-
- if (first) {
- first = false;
+ std::stringstream utf8;
+ if (useMagicImports &&
+ String::convertUTF16ToUTF8(utf8, c->string.str)) {
+ global->module = "'";
+ global->base = Name(utf8.str());
} else {
- json << ',';
+ global->module = "string.const";
+ global->base = std::to_string(jsonImportIndex);
+ if (first) {
+ first = false;
+ } else {
+ json << ',';
+ }
+ String::printEscapedJSON(json, c->string.str);
+ jsonImportIndex++;
}
- String::printEscapedJSON(json, c->string.str);
+ global->init = nullptr;
}
}
}
@@ -516,5 +528,6 @@ struct StringLowering : public StringGathering {
Pass* createStringGatheringPass() { return new StringGathering(); }
Pass* createStringLoweringPass() { return new StringLowering(); }
+Pass* createStringLoweringMagicImportPass() { return new StringLowering(true); }
} // namespace wasm
diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp
index 0955082ac..19ddaf2d4 100644
--- a/src/passes/pass.cpp
+++ b/src/passes/pass.cpp
@@ -484,6 +484,10 @@ void PassRegistry::registerPasses() {
"lowers wasm strings and operations to imports",
createStringLoweringPass);
registerPass(
+ "string-lowering-magic-imports",
+ "same as string-lowering, but encodes well-formed strings as magic imports",
+ createStringLoweringMagicImportPass);
+ registerPass(
"strip", "deprecated; same as strip-debug", createStripDebugPass);
registerPass("stack-check",
"enforce limits on llvm's __stack_pointer global",
diff --git a/src/passes/passes.h b/src/passes/passes.h
index 1b1ca99c6..23a9ea70b 100644
--- a/src/passes/passes.h
+++ b/src/passes/passes.h
@@ -156,6 +156,7 @@ Pass* createSimplifyLocalsNoTeeNoStructurePass();
Pass* createStackCheckPass();
Pass* createStringGatheringPass();
Pass* createStringLoweringPass();
+Pass* createStringLoweringMagicImportPass();
Pass* createStripDebugPass();
Pass* createStripDWARFPass();
Pass* createStripProducersPass();