Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: This wasn't needed until try/finally but here it is. I add a visitor that walks the CFG and extracts all named locals and unnamed local keys and then can be used to provide numeric IDs for the unnamed locals Reviewed By: mofarrell Differential Revision: D5577679 fbshipit-source-id: 9cc02d1cc0d2b7accd8c67113b91c17547ae59ef
- Loading branch information
Showing
10 changed files
with
217 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
+----------------------------------------------------------------------+ | ||
| HipHop for PHP | | ||
+----------------------------------------------------------------------+ | ||
| Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) | | ||
+----------------------------------------------------------------------+ | ||
| This source file is subject to version 3.01 of the PHP license, | | ||
| that is bundled with this package in the file LICENSE, and is | | ||
| available through the world-wide-web at the following url: | | ||
| http://www.php.net/license/3_01.txt | | ||
| If you did not receive a copy of the PHP license and are unable to | | ||
| obtain it through the world-wide-web, please send a note to | | ||
| license@php.net so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
*/ | ||
|
||
#include "hphp/php7/analysis.h" | ||
|
||
#include "hphp/util/match.h" | ||
|
||
namespace HPHP { namespace php7 { | ||
|
||
namespace { | ||
|
||
using bc::Local; | ||
using bc::NamedLocal; | ||
using bc::UniqueLocal; | ||
|
||
struct LocalSet { | ||
void add(const Local& local) { | ||
match<void>(local, | ||
[&](const NamedLocal& named) { | ||
names.insert(named.name); | ||
}, | ||
[&](const UniqueLocal& unique) { | ||
uniqueIds.insert(unique.id); | ||
} | ||
); | ||
} | ||
|
||
void allocateUniqueIds() { | ||
uint32_t id = names.size(); | ||
for (auto& idPtr : uniqueIds) { | ||
*idPtr = id++; | ||
} | ||
} | ||
|
||
std::unordered_set<std::string> names; | ||
std::unordered_set<std::shared_ptr<uint32_t>> uniqueIds; | ||
}; | ||
|
||
struct LocalsVisitor : CFGVisitor { | ||
explicit LocalsVisitor(LocalSet& locals) | ||
: locals(locals) {} | ||
|
||
void beginTry() override {} | ||
|
||
void beginCatch() override {} | ||
|
||
void endRegion() override {} | ||
|
||
void block(Block* blk) override { | ||
for (const auto& bc : blk->code) { | ||
bc.visit(*this); | ||
} | ||
} | ||
|
||
template <class T> | ||
void bytecode(const T& bc) { | ||
bc.visit_imms(*this); | ||
} | ||
|
||
void imm(Local local) { | ||
locals.add(local); | ||
} | ||
|
||
template <class T> | ||
void imm(const T&) {} | ||
|
||
LocalSet& locals; | ||
}; | ||
|
||
} // namespace | ||
|
||
std::unordered_set<std::string> analyzeLocals(const Function& func) { | ||
LocalSet locals; | ||
for (const auto& param : func.params) { | ||
locals.add(bc::NamedLocal{param.name}); | ||
} | ||
func.cfg.visit(LocalsVisitor(locals)); | ||
locals.allocateUniqueIds(); | ||
return locals.names; | ||
} | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
|
||
}} // namespace HPHP::php7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
+----------------------------------------------------------------------+ | ||
| HipHop for PHP | | ||
+----------------------------------------------------------------------+ | ||
| Copyright (c) 2010- Facebook, Inc. (http://www.facebook.com) | | ||
+----------------------------------------------------------------------+ | ||
| This source file is subject to version 3.01 of the PHP license, | | ||
| that is bundled with this package in the file LICENSE, and is | | ||
| available through the world-wide-web at the following url: | | ||
| http://www.php.net/license/3_01.txt | | ||
| If you did not receive a copy of the PHP license and are unable to | | ||
| obtain it through the world-wide-web, please send a note to | | ||
| license@php.net so we can mail you a copy immediately. | | ||
+----------------------------------------------------------------------+ | ||
*/ | ||
|
||
#ifndef incl_HPHP_PHP7_ANALYSIS_H | ||
#define incl_HPHP_PHP7_ANALYSIS_H | ||
|
||
#include "hphp/php7/unit.h" | ||
|
||
#include <unordered_set> | ||
|
||
namespace HPHP { namespace php7 { | ||
|
||
/* Find all named locals and allocate IDs for unnamed locals */ | ||
std::unordered_set<std::string> analyzeLocals(const Function& func); | ||
|
||
}} // namespace HPHP::php7 | ||
|
||
#endif // incl_HPHP_PHP7_ANALYSIS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.