diff options
author | Alon Zakai <alonzakai@gmail.com> | 2015-11-12 21:49:04 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2015-11-13 18:11:24 -0800 |
commit | 97b943c832dd3389b81c0896826977e0c447de28 (patch) | |
tree | 466affed0a003bdf5d787d17326c09c5edda7c99 /src/pass.cpp | |
parent | 06a9c12ea045dfb9b1dd4f4ccf959ac715d4df8a (diff) | |
download | binaryen-97b943c832dd3389b81c0896826977e0c447de28.tar.gz binaryen-97b943c832dd3389b81c0896826977e0c447de28.tar.bz2 binaryen-97b943c832dd3389b81c0896826977e0c447de28.zip |
pass support
Diffstat (limited to 'src/pass.cpp')
-rw-r--r-- | src/pass.cpp | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/pass.cpp b/src/pass.cpp new file mode 100644 index 000000000..25e7a41e5 --- /dev/null +++ b/src/pass.cpp @@ -0,0 +1,75 @@ + +#include <pass.h> + +namespace wasm { + +// PassRegistry + +PassRegistry* PassRegistry::get() { + static PassRegistry* manager = nullptr; + if (!manager) { + manager = new PassRegistry(); + } + return manager; +} + +void PassRegistry::registerPass(const char* name, Creator create) { + assert(passCreatorMap.find(name) == passCreatorMap.end()); + passCreatorMap[name] = create; +} + +Pass* PassRegistry::createPass(std::string name) { + Creator creator = passCreatorMap[name]; + if (!creator) return nullptr; + return creator(); +} + +std::vector<std::string> PassRegistry::getRegisteredNames() { + std::vector<std::string> ret; + for (auto pair : passCreatorMap) { + ret.push_back(pair.first); + } + return ret; +} + +// PassRunner + +void PassRunner::add(std::string passName) { + passes.push_back(PassRegistry::get()->createPass(passName)); +} + +template<class P> +void PassRunner::add() { + passes.push_back(new P()); +} + +void PassRunner::run(Module* module) { + for (auto pass : passes) { + currPass = pass; + pass->run(this, module); + } +} + +template<class P> +P* PassRunner::getLast() { + bool found = false; + P* ret; + for (int i = passes.size() - 1; i >= 0; i--) { + if (found && (ret = dynamic_cast<P*>(passes[i]))) { + return ret; + } + if (passes[i] == currPass) { + found = true; + } + } + return nullptr; +} + +PassRunner::~PassRunner() { + for (auto pass : passes) { + delete(pass); + } +} + +} // namespace wasm + |