take &self
[hiphop-php.git] / hphp / hhbbc / hhbbc.h
blob81eeeca3042e600593e4fd73c7ef2da66b0e36b1
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #ifndef incl_HPHP_HHBBC_H_
17 #define incl_HPHP_HHBBC_H_
19 #include <atomic>
20 #include <deque>
21 #include <vector>
22 #include <memory>
23 #include <string>
24 #include <utility>
26 #include "hphp/runtime/base/repo-auth-type-array.h"
28 #include "hphp/util/hash-map.h"
29 #include "hphp/util/hash-set.h"
30 #include "hphp/util/hash.h"
31 #include "hphp/util/lock.h"
32 #include "hphp/util/synchronizable.h"
34 namespace HPHP {
36 struct UnitEmitter;
38 enum class Op : uint16_t;
39 struct OpHash {
40 size_t operator()(Op op) const {
41 return hash_int64(static_cast<uint16_t>(op));
45 namespace HHBBC {
47 using MethodMap = hphp_fast_string_imap<hphp_fast_string_iset>;
48 using OpcodeSet = hphp_fast_set<Op,OpHash>;
50 //////////////////////////////////////////////////////////////////////
53 * This is the public API to this subsystem.
56 //////////////////////////////////////////////////////////////////////
58 namespace php {
59 struct Program;
60 // basically a unique_ptr<Program>, but we want to be able to use it on a
61 // forward declared Program
62 struct ProgramPtr {
63 ProgramPtr() = default;
64 explicit ProgramPtr(Program* program) : m_program{program} {}
65 ~ProgramPtr() { if (m_program) clear(); }
66 ProgramPtr(ProgramPtr&& o) : m_program{o.m_program} {
67 o.m_program = nullptr;
69 ProgramPtr& operator=(ProgramPtr&& o) {
70 std::swap(m_program, o.m_program);
71 return *this;
73 Program* get() const { return m_program; }
74 Program& operator*() const { return *m_program; }
75 Program* operator->() const { return m_program; }
76 private:
77 void clear();
78 Program* m_program{};
82 // Create a method map for the options structure from a SinglePassReadableRange
83 // containing a list of Class::methodName strings.
84 template<class SinglePassReadableRange>
85 MethodMap make_method_map(SinglePassReadableRange&);
87 template<class SinglePassReadableRange>
88 OpcodeSet make_bytecode_map(SinglePassReadableRange& bcs);
90 //////////////////////////////////////////////////////////////////////
92 void hard_constprop(bool);
94 //////////////////////////////////////////////////////////////////////
96 struct UnitEmitterQueue : Synchronizable {
97 // Add another ue. Adding nullptr marks us done.
98 void push(std::unique_ptr<UnitEmitter> ue);
99 // Get the next ue, or nullptr to indicate we're done.
100 std::unique_ptr<UnitEmitter> pop();
101 // Fetch any remaining ues.
102 // Must be called in single threaded mode, after we've stopped adding ues.
103 void fetch(std::vector<std::unique_ptr<UnitEmitter>>& ues);
104 // Clear done flag, and get us ready for reuse.
105 void reset();
106 private:
107 std::deque<std::unique_ptr<UnitEmitter>> m_ues;
108 std::atomic<bool> m_done{};
112 * Create a php::Program, and wrap it in a ProgramPtr.
114 php::ProgramPtr make_program();
117 * Add the given unit to the program. May be called asynchronously.
119 void add_unit_to_program(const UnitEmitter* ue, php::Program& program);
122 * Perform whole-program optimization on a set of UnitEmitters.
124 * Currently this process relies on some information from HPHPc. It
125 * expects traits are already flattened (it might be wrong if they
126 * aren't).
128 void whole_program(php::ProgramPtr program,
129 UnitEmitterQueue& ueq,
130 std::unique_ptr<ArrayTypeTable::Builder>& arrTable,
131 int num_threads = 0);
133 //////////////////////////////////////////////////////////////////////
136 * Main entry point when the program should behave like hhbbc.
138 int main(int argc, char** argv);
140 //////////////////////////////////////////////////////////////////////
144 #endif