Use RATs from HHBBC in init_use_vars
[hiphop-php.git] / hphp / runtime / vm / func-emitter.h
blob1d45fde57c488a1babdb1fc07717cec631ea4536
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2015 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 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_VM_FUNC_EMITTER_H_
18 #define incl_HPHP_VM_FUNC_EMITTER_H_
20 #include "hphp/runtime/base/attr.h"
21 #include "hphp/runtime/base/class-info.h"
22 #include "hphp/runtime/base/datatype.h"
23 #include "hphp/runtime/base/type-string.h"
24 #include "hphp/runtime/base/typed-value.h"
25 #include "hphp/runtime/base/user-attributes.h"
27 #include "hphp/runtime/vm/func.h"
28 #include "hphp/runtime/vm/repo-helpers.h"
29 #include "hphp/runtime/vm/type-constraint.h"
30 #include "hphp/runtime/vm/unit.h"
32 #include <utility>
33 #include <vector>
35 namespace HPHP {
36 ///////////////////////////////////////////////////////////////////////////////
38 struct PreClass;
39 struct StringData;
41 struct PreClassEmitter;
42 struct UnitEmitter;
44 ///////////////////////////////////////////////////////////////////////////////
46 struct EHEntEmitter {
47 EHEnt::Type m_type;
48 bool m_itRef;
49 Offset m_base;
50 Offset m_past;
51 int m_iterId;
52 int m_parentIndex;
53 Offset m_fault;
54 std::vector<std::pair<Id,Offset>> m_catches;
56 template<class SerDe> void serde(SerDe& sd);
59 ///////////////////////////////////////////////////////////////////////////////
62 * Bag of Func's fields used to emit Funcs.
64 struct FuncEmitter {
66 /////////////////////////////////////////////////////////////////////////////
67 // Types.
69 struct ParamInfo : public Func::ParamInfo {
70 ParamInfo()
71 : byRef(false)
74 template<class SerDe>
75 void serde(SerDe& sd) {
76 Func::ParamInfo* parent = this;
77 parent->serde(sd);
78 sd(byRef);
81 // Whether the parameter is passed by reference. This field is absent from
82 // Func::ParamInfo because we store it in a bitfield on Func.
83 bool byRef;
86 typedef std::vector<ParamInfo> ParamInfoVec;
87 typedef std::vector<Func::SVInfo> SVInfoVec;
88 typedef std::vector<EHEntEmitter> EHEntVec;
89 typedef std::vector<FPIEnt> FPIEntVec;
92 /////////////////////////////////////////////////////////////////////////////
93 // Initialization and execution.
95 FuncEmitter(UnitEmitter& ue, int sn, Id id, const StringData* n);
96 FuncEmitter(UnitEmitter& ue, int sn, const StringData* n,
97 PreClassEmitter* pce);
98 ~FuncEmitter();
101 * Just set some fields when we start and stop emitting.
103 void init(int l1, int l2, Offset base_, Attr attrs_, bool top_,
104 const StringData* docComment_);
105 void finish(Offset past, bool load);
108 * Commit this function to a repo.
110 void commit(RepoTxn& txn) const;
113 * Instantiate a runtime Func*.
115 Func* create(Unit& unit, PreClass* preClass = nullptr) const;
117 template<class SerDe> void serdeMetaData(SerDe&);
120 /////////////////////////////////////////////////////////////////////////////
121 // Metadata.
124 * Get the associated Unit and PreClass emitters.
126 UnitEmitter& ue() const;
127 PreClassEmitter* pce() const;
130 * XXX: What are these for?
132 int sn() const;
133 Id id() const;
136 * XXX: Set the whatever these things are.
138 void setIds(int sn, Id id);
141 /////////////////////////////////////////////////////////////////////////////
142 // Locals, iterators, and parameters.
145 * Count things.
147 Id numLocals() const;
148 Id numIterators() const;
149 Id numLiveIterators() const;
152 * Set things.
154 void setNumIterators(Id numIterators);
155 void setNumLiveIterators(Id id);
158 * Check existence of, look up, and allocate named locals.
160 bool hasVar(const StringData* name) const;
161 Id lookupVarId(const StringData* name) const;
162 void allocVarId(const StringData* name);
165 * Allocate and free unnamed locals.
167 Id allocUnnamedLocal();
168 void freeUnnamedLocal(Id id);
171 * Allocate and free iterators.
173 Id allocIterator();
174 void freeIterator(Id id);
177 * Add a parameter and corresponding named local.
179 void appendParam(const StringData* name, const ParamInfo& info);
182 * Get the local variable name -> id map.
184 const Func::NamedLocalsMap::Builder& localNameMap() const;
187 /////////////////////////////////////////////////////////////////////////////
188 // Unit tables.
191 * Add entries to the EH and FPI tables, and return them by reference.
193 EHEntEmitter& addEHEnt();
194 FPIEnt& addFPIEnt();
196 private:
198 * Private table sort routines; called at finish()-time.
200 void sortEHTab();
201 void sortFPITab(bool load);
203 public:
205 * Declare that the EH table was created in sort-order and doesn't need to be
206 * resorted at finish() time.
208 void setEHTabIsSorted();
210 /////////////////////////////////////////////////////////////////////////////
211 // Helper accessors. [const]
214 * Is the function a pseudomain, a method, variadic (i.e., takes a `...'
215 * parameter), or an HNI function with a native implementation?
217 bool isPseudoMain() const;
218 bool isMethod() const;
219 bool isVariadic() const;
222 * @returns: std::make_pair(line1, line2)
224 std::pair<int,int> getLocation() const;
227 /////////////////////////////////////////////////////////////////////////////
228 // Complex setters.
230 // XXX: Some of these should be moved to the emitter (esp. the
231 // setBuiltinFunc() methods).
234 * Shorthand for setting `line1' and `line2' because typing is hard.
236 void setLocation(int l1, int l2);
239 * Pulls native and system attributes out of the user attributes map.
241 * System attributes are returned by reference through `attrs_', and native
242 * attributes are returned as an integer.
244 int parseNativeAttributes(Attr& attrs_) const;
247 * Pull fields for builtin functions out of a MethodInfo object.
249 void setBuiltinFunc(const ClassInfo::MethodInfo* info,
250 BuiltinFunction bif, BuiltinFunction nif,
251 Offset base_);
254 * Set some fields for builtin functions.
256 void setBuiltinFunc(BuiltinFunction bif, BuiltinFunction nif,
257 Attr attrs_, Offset base_);
260 /////////////////////////////////////////////////////////////////////////////
261 // Data members.
263 private:
265 * Metadata.
267 UnitEmitter& m_ue;
268 PreClassEmitter* m_pce;
270 int m_sn;
271 Id m_id;
273 public:
275 * Func fields.
277 Offset base;
278 Offset past;
279 int line1;
280 int line2;
281 LowStringPtr name;
282 bool top;
283 Attr attrs;
285 ParamInfoVec params;
286 SVInfoVec staticVars;
287 int maxStackCells;
289 MaybeDataType returnType;
290 TypeConstraint retTypeConstraint;
291 LowStringPtr retUserType;
293 EHEntVec ehtab;
294 FPIEntVec fpitab;
296 bool isClosureBody;
297 bool isAsync;
298 bool isGenerator;
299 bool isPairGenerator;
300 bool isMemoizeImpl;
301 bool isMemoizeWrapper;
302 bool hasMemoizeSharedProp;
303 bool containsCalls;
305 LowStringPtr docComment;
306 LowStringPtr originalFilename;
308 UserAttributeMap userAttributes;
310 StringData *memoizePropName;
311 int memoizeSharedPropIndex;
313 private:
315 * FuncEmitter-managed state.
317 Func::NamedLocalsMap::Builder m_localNames;
318 Id m_numLocals;
319 int m_numUnnamedLocals;
320 int m_activeUnnamedLocals;
321 Id m_numIterators;
322 Id m_nextFreeIterator;
324 const ClassInfo::MethodInfo* m_info;
325 BuiltinFunction m_builtinFuncPtr;
326 BuiltinFunction m_nativeFuncPtr;
328 bool m_ehTabSorted;
331 ///////////////////////////////////////////////////////////////////////////////
334 * Proxy for converting in-repo function representations into FuncEmitters.
336 struct FuncRepoProxy : public RepoProxy {
337 friend class Func;
338 friend class FuncEmitter;
340 explicit FuncRepoProxy(Repo& repo);
341 ~FuncRepoProxy();
342 void createSchema(int repoId, RepoTxn& txn);
344 struct InsertFuncStmt : public RepoProxy::Stmt {
345 InsertFuncStmt(Repo& repo, int repoId) : Stmt(repo, repoId) {}
346 void insert(const FuncEmitter& fe,
347 RepoTxn& txn, int64_t unitSn, int funcSn, Id preClassId,
348 const StringData* name, bool top);
351 struct GetFuncsStmt : public RepoProxy::Stmt {
352 GetFuncsStmt(Repo& repo, int repoId) : Stmt(repo, repoId) {}
353 void get(UnitEmitter& ue);
356 InsertFuncStmt insertFunc[RepoIdCount];
357 GetFuncsStmt getFuncs[RepoIdCount];
360 ///////////////////////////////////////////////////////////////////////////////
363 #define incl_HPHP_VM_FUNC_EMITTER_INL_H_
364 #include "hphp/runtime/vm/func-emitter-inl.h"
365 #undef incl_HPHP_VM_FUNC_EMITTER_INL_H_
367 #endif // incl_HPHP_VM_FUNC_EMITTER_H_