diff options
author | Alon Zakai <azakai@google.com> | 2021-03-30 16:50:55 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-30 16:50:55 -0700 |
commit | a680b716efe08afd9920269253058a849d6c4286 (patch) | |
tree | 9268b2a903bc60a675c6db537ffa4a3c0cb75df6 /src/passes/SetGlobals.cpp | |
parent | 330f722a4f4ae5288b21954aefa496af5cbc71e7 (diff) | |
download | binaryen-a680b716efe08afd9920269253058a849d6c4286.tar.gz binaryen-a680b716efe08afd9920269253058a849d6c4286.tar.bz2 binaryen-a680b716efe08afd9920269253058a849d6c4286.zip |
SetGlobals pass (#3750)
This allows changing a global's value on the commandline in an easy way.
In some toolchains this is useful as the build can contain a global that
indicates something like a logging level, which this can customize.
Diffstat (limited to 'src/passes/SetGlobals.cpp')
-rw-r--r-- | src/passes/SetGlobals.cpp | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/passes/SetGlobals.cpp b/src/passes/SetGlobals.cpp new file mode 100644 index 000000000..8a147665c --- /dev/null +++ b/src/passes/SetGlobals.cpp @@ -0,0 +1,63 @@ +/* + * Copyright 2021 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// Assigns values to specified globals. This can be useful to perform a minor +// customization of an existing wasm file. + +#include "pass.h" +#include "support/string.h" +#include "wasm-builder.h" +#include "wasm.h" + +namespace wasm { + +struct SetGlobals : public Pass { + void run(PassRunner* runner, Module* module) override { + Name input = runner->options.getArgument( + "set-globals", + "SetGlobals usage: wasm-opt --pass-arg=set-globals@x=y,z=w"); + + // The input is a set of X=Y pairs separated by commas. + String::Split pairs(input.str, ","); + for (auto& pair : pairs) { + String::Split nameAndValue(pair, "="); + auto name = nameAndValue[0]; + auto value = nameAndValue[1]; + auto* glob = module->getGlobalOrNull(name); + if (!glob) { + std::cerr << "warning: could not find global: " << name << '\n'; + } + // Parse the input. + Literal lit; + if (glob->type == Type::i32) { + lit = Literal(int32_t(stoi(value))); + } else if (glob->type == Type::i64) { + lit = Literal(int64_t(stoll(value))); + } else { + Fatal() << "global's type is not supported: " << name; + } + // The global now has a value, and is not imported. + glob->init = Builder(*module).makeConst(lit); + glob->module = glob->base = Name(); + } + } +}; + +// declare pass + +Pass* createSetGlobalsPass() { return new SetGlobals(); } + +} // namespace wasm |