2 +----------------------------------------------------------------------+
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"
36 ///////////////////////////////////////////////////////////////////////////////
41 struct PreClassEmitter
;
44 ///////////////////////////////////////////////////////////////////////////////
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.
66 /////////////////////////////////////////////////////////////////////////////
69 struct ParamInfo
: public Func::ParamInfo
{
75 void serde(SerDe
& sd
) {
76 Func::ParamInfo
* parent
= this;
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.
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
);
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 /////////////////////////////////////////////////////////////////////////////
124 * Get the associated Unit and PreClass emitters.
126 UnitEmitter
& ue() const;
127 PreClassEmitter
* pce() const;
130 * XXX: What are these for?
136 * XXX: Set the whatever these things are.
138 void setIds(int sn
, Id id
);
141 /////////////////////////////////////////////////////////////////////////////
142 // Locals, iterators, and parameters.
147 Id
numLocals() const;
148 Id
numIterators() const;
149 Id
numLiveIterators() const;
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.
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 /////////////////////////////////////////////////////////////////////////////
191 * Add entries to the EH and FPI tables, and return them by reference.
193 EHEntEmitter
& addEHEnt();
198 * Private table sort routines; called at finish()-time.
201 void sortFPITab(bool load
);
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 /////////////////////////////////////////////////////////////////////////////
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
,
254 * Set some fields for builtin functions.
256 void setBuiltinFunc(BuiltinFunction bif
, BuiltinFunction nif
,
257 Attr attrs_
, Offset base_
);
260 /////////////////////////////////////////////////////////////////////////////
268 PreClassEmitter
* m_pce
;
286 SVInfoVec staticVars
;
289 MaybeDataType returnType
;
290 TypeConstraint retTypeConstraint
;
291 LowStringPtr retUserType
;
299 bool isPairGenerator
;
301 bool isMemoizeWrapper
;
302 bool hasMemoizeSharedProp
;
305 LowStringPtr docComment
;
306 LowStringPtr originalFilename
;
308 UserAttributeMap userAttributes
;
310 StringData
*memoizePropName
;
311 int memoizeSharedPropIndex
;
315 * FuncEmitter-managed state.
317 Func::NamedLocalsMap::Builder m_localNames
;
319 int m_numUnnamedLocals
;
320 int m_activeUnnamedLocals
;
322 Id m_nextFreeIterator
;
324 const ClassInfo::MethodInfo
* m_info
;
325 BuiltinFunction m_builtinFuncPtr
;
326 BuiltinFunction m_nativeFuncPtr
;
331 ///////////////////////////////////////////////////////////////////////////////
334 * Proxy for converting in-repo function representations into FuncEmitters.
336 struct FuncRepoProxy
: public RepoProxy
{
338 friend class FuncEmitter
;
340 explicit FuncRepoProxy(Repo
& repo
);
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_