From 3f5ee87d262080265b65a3789392b399c91f30ad Mon Sep 17 00:00:00 2001 From: Daniel Wirtz Date: Wed, 21 Feb 2018 23:22:11 +0100 Subject: Improve name mangling of asm.js identifiers (#1433) Also refactors mangling to its own file so it can be reused by generators and consumers, i.e., where it is important to know that an import must be named 'switch_' where it otherwise would be 'switch'. * Update tests and JS dist files --- src/wasm2asm.h | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) (limited to 'src/wasm2asm.h') diff --git a/src/wasm2asm.h b/src/wasm2asm.h index eded082ad..59f2d6ab8 100644 --- a/src/wasm2asm.h +++ b/src/wasm2asm.h @@ -26,6 +26,7 @@ #include #include "asmjs/shared-constants.h" +#include "asmjs/asmangle.h" #include "wasm.h" #include "wasm-builder.h" #include "emscripten-optimizer/optimizer.h" @@ -156,27 +157,16 @@ public: frees[type].push_back(temp); } - static IString fromName(Name name) { - // TODO: more clever name fixing, including checking we do not collide - const char* str = name.str; - // check the various issues, and recurse so we check the others - if (strchr(str, '-')) { - char* mod = strdup(str); - str = mod; - while (*mod) { - if (*mod == '-') *mod = '_'; - mod++; - } - IString result = fromName(IString(str, false)); - free((void*)str); - return result; - } - if (isdigit(str[0]) || strcmp(str, "if") == 0) { - std::string prefixed = "$$"; - prefixed += name.str; - return fromName(IString(prefixed.c_str(), false)); + IString fromName(Name name) { + // TODO: checking names do not collide after mangling + auto it = mangledNames.find(name.c_str()); + if (it != mangledNames.end()) { + return it->second; } - return name; + auto mangled = asmangle(std::string(name.c_str())); + IString ret(mangled.c_str(), false); + mangledNames[name.c_str()] = ret; + return ret; } void setStatement(Expression* curr) { @@ -202,6 +192,10 @@ private: // Expressions that will be a statement. std::set willBeStatement; + // Mangled names cache by interned names. + // Utilizes the usually reused underlying cstring's pointer as the key. + std::unordered_map mangledNames; + // All our function tables have the same size TODO: optimize? size_t tableSize; -- cgit v1.2.3