Hack codegen: static closures, ordering of inline functions and classes
[hiphop-php.git] / hphp / hack / src / hhbc / hhbc_ast.ml
blobef775f079f56eecb2dcdf0d757b2c1ca44c1b3d5
1 (**
2 * Copyright (c) 2017, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the "hack" directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
9 *)
11 type check_started =
12 | IgnoreStarted
13 | CheckStarted
14 type free_iterator =
15 | IgnoreIter
16 | FreeIter
17 type repo_auth_type = string (* see see runtime/base/repo-auth-type.h *)
18 type local_id = Local.t
20 type param_id =
21 | Param_unnamed of int
22 | Param_named of string
23 type param_num = int
24 type stack_index = int
25 type class_id = Hhbc_id.Class.t
26 type class_num = int
27 type function_num = int
28 type typedef_num = int
29 type function_id = Hhbc_id.Function.t
30 type method_id = Hhbc_id.Method.t
31 type const_id = Hhbc_id.Const.t
32 type prop_id = Hhbc_id.Prop.t
33 type num_params = int
34 type classref_id = int
35 type collection_type = int
36 (* Conventionally this is "A_" followed by an integer *)
37 type adata_id = string
39 (* These are the three flavors of value that can live on the stack:
40 * C = cell
41 * V = ref
42 * R = return value
43 * F = function argument
44 * U = uninit
45 * Note: Function argument and uninit are not added to the type
46 * as they are handled separately
48 module Flavor = struct
49 type t = Cell | Ref | ReturnVal
50 end
52 (* When using the PassX instructions we need to emit the right kind *)
53 module PassByRefKind = struct
54 type t = AllowCell | WarnOnCell | ErrorOnCell
55 end
57 module MemberOpMode = struct
59 type t =
60 | ModeNone
61 | Warn
62 | Define
63 | Unset
65 let to_string op =
66 match op with
67 | ModeNone -> "None"
68 | Warn -> "Warn"
69 | Define -> "Define"
70 | Unset -> "Unset"
72 end (* of MemberOpMode *)
74 module QueryOp = struct
75 type t =
76 | CGet
77 | CGetQuiet
78 | Isset
79 | Empty
81 let to_string op =
82 match op with
83 | CGet -> "CGet"
84 | CGetQuiet -> "CGetQuiet"
85 | Isset -> "Isset"
86 | Empty -> "Empty"
88 end (* of QueryOp *)
90 module FatalOp = struct
91 type t =
92 | Parse
93 | Runtime
94 | RuntimeOmitFrame
96 let to_string op =
97 match op with
98 | Parse -> "Parse"
99 | Runtime -> "Runtime"
100 | RuntimeOmitFrame -> "RuntimeOmitFrame"
102 end (* of FatalOp *)
104 module MemberKey = struct
105 type t =
106 | EC of stack_index
107 | EL of local_id
108 | ET of Litstr.id
109 | EI of int64
110 | PC of stack_index
111 | PL of local_id
112 | PT of prop_id
113 | QT of prop_id
115 end (* Of MemberKey *)
117 type instruct_basic =
118 | Nop
119 | EntryNop
120 | PopC
121 | PopV
122 | PopR
123 | PopU
124 | Dup
125 | Box
126 | Unbox
127 | BoxR
128 | UnboxR
129 | UnboxRNop
130 | RGetCNop
132 type instruct_lit_const =
133 | NYI of string
134 | Null
135 | True
136 | False
137 | NullUninit
138 | Int of int64
139 | Double of string
140 | String of string
141 (* Pseudo instruction that will get translated into appropraite literal
142 * bytecode, with possible reference to .adata *)
143 | TypedValue of Typed_value.t
144 | Array of adata_id
145 | Vec of adata_id
146 | Dict of adata_id
147 | Keyset of adata_id
148 | NewArray of int (* capacity hint *)
149 | NewMixedArray of int (* capacity hint *)
150 | NewDictArray of int (* capacity hint *)
151 | NewMIArray of int (* capacity hint *)
152 | NewMSArray of int (* capacity hint *)
153 | NewLikeArrayL of local_id * int (* capacity hint *)
154 | NewPackedArray of int
155 | NewStructArray of Litstr.id list
156 | NewVecArray of int
157 | NewKeysetArray of int
158 | NewPair
159 | AddElemC
160 | AddElemV
161 | AddNewElemC
162 | AddNewElemV
163 | NewCol of collection_type
164 | ColFromArray of collection_type
165 | MapAddElemC
166 | Cns of const_id
167 | CnsE of const_id
168 | CnsU of const_id * Litstr.id
169 | ClsCns of const_id * classref_id
170 | ClsCnsD of const_id * class_id
171 | File
172 | Dir
173 | Method
174 | NameA
176 type instruct_operator =
177 | Concat
178 | Abs
179 | Add
180 | Sub
181 | Mul
182 | AddO
183 | SubO
184 | MulO
185 | Div
186 | Mod
187 | Pow
188 | Sqrt
189 | Xor
190 | Not
191 | Same
192 | NSame
193 | Eq
194 | Neq
195 | Lt
196 | Lte
197 | Gt
198 | Gte
199 | Cmp
200 | BitAnd
201 | BitOr
202 | BitXor
203 | BitNot
204 | Shl
205 | Shr
206 | Floor
207 | Ceil
208 | CastBool
209 | CastInt
210 | CastDouble
211 | CastString
212 | CastArray
213 | CastObject
214 | CastVec
215 | CastDict
216 | CastKeyset
217 | InstanceOf
218 | InstanceOfD of class_id
219 | Print
220 | Clone
221 | Exit
222 | Fatal of FatalOp.t
224 type switchkind =
225 | Bounded
226 | Unbounded
228 type instruct_control_flow =
229 | Jmp of Label.t
230 | JmpNS of Label.t
231 | JmpZ of Label.t
232 | JmpNZ of Label.t
233 (* bounded, base, offset vector *)
234 | Switch of switchkind * int * Label.t list
235 (* litstr id / offset vector *)
236 | SSwitch of (Litstr.id * Label.t) list
237 | RetC
238 | RetV
239 | Unwind
240 | Throw
242 type instruct_special_flow =
243 | Continue of int * int (* This will be rewritten *)
244 | Break of int * int * (bool * Iterator.t) list (* This will be rewritten *)
246 type instruct_get =
247 | CGetL of local_id
248 | CGetQuietL of local_id
249 | CGetL2 of local_id
250 | CGetL3 of local_id
251 | CUGetL of local_id
252 | PushL of local_id
253 | CGetN
254 | CGetQuietN
255 | CGetG
256 | CGetQuietG
257 | CGetS of classref_id
258 | VGetL of local_id
259 | VGetN
260 | VGetG
261 | VGetS of classref_id
262 | ClsRefGetL of local_id * classref_id
263 | ClsRefGetC of classref_id
265 type istype_op =
266 | OpNull
267 | OpBool
268 | OpInt
269 | OpDbl
270 | OpStr
271 | OpArr
272 | OpObj
273 | OpScalar (* Int or Dbl or Str or Bool *)
274 | OpKeyset
275 | OpDict
276 | OpVec
278 type instruct_isset =
279 | IssetC
280 | IssetL of local_id
281 | IssetN
282 | IssetG
283 | IssetS of classref_id
284 | EmptyL of local_id
285 | EmptyN
286 | EmptyG
287 | EmptyS of classref_id
288 | IsTypeC of istype_op
289 | IsTypeL of local_id * istype_op
291 type eq_op =
292 | PlusEqual
293 | MinusEqual
294 | MulEqual
295 | ConcatEqual
296 | DivEqual
297 | PowEqual
298 | ModEqual
299 | AndEqual
300 | OrEqual
301 | XorEqual
302 | SlEqual
303 | SrEqual
304 | PlusEqualO
305 | MinusEqualO
306 | MulEqualO
308 type incdec_op =
309 | PreInc
310 | PostInc
311 | PreDec
312 | PostDec
313 | PreIncO
314 | PostIncO
315 | PreDecO
316 | PostDecO
318 type initprop_op =
319 | Static
320 | NonStatic
322 type instruct_mutator =
323 | SetL of local_id
324 | SetN
325 | SetG
326 | SetS of classref_id
327 | SetOpL of local_id * eq_op
328 | SetOpN of eq_op
329 | SetOpG of eq_op
330 | SetOpS of eq_op * classref_id
331 | IncDecL of local_id * incdec_op
332 | IncDecN of incdec_op
333 | IncDecG of incdec_op
334 | IncDecS of incdec_op * classref_id
335 | BindL of local_id
336 | BindN
337 | BindG
338 | BindS of classref_id
339 | UnsetL of local_id
340 | UnsetN
341 | UnsetG
342 | CheckProp of prop_id
343 | InitProp of prop_id * initprop_op
345 type instruct_call =
346 | FPushFunc of num_params
347 | FPushFuncD of num_params * function_id
348 | FPushFuncU of num_params * function_id * Litstr.id
349 | FPushObjMethod of num_params * Ast.og_null_flavor
350 | FPushObjMethodD of num_params * method_id * Ast.og_null_flavor
351 | FPushClsMethod of num_params * classref_id
352 | FPushClsMethodF of num_params * classref_id
353 | FPushClsMethodD of num_params * method_id * class_id
354 | FPushCtor of num_params * classref_id
355 | FPushCtorD of num_params * class_id
356 | FPushCtorI of num_params * classref_id
357 | DecodeCufIter of num_params * Label.t
358 | FPushCufIter of num_params * Iterator.t
359 | FPushCuf of num_params
360 | FPushCufF of num_params
361 | FPushCufSafe of num_params
362 | CufSafeArray
363 | CufSafeReturn
364 | FPassC of param_num
365 | FPassCW of param_num
366 | FPassCE of param_num
367 | FPassV of param_num
368 | FPassVNop of param_num
369 | FPassR of param_num
370 | FPassL of param_num * local_id
371 | FPassN of param_num
372 | FPassG of param_num
373 | FPassS of param_num * classref_id
374 | FCall of num_params
375 | FCallD of num_params * class_id * function_id
376 | FCallArray
377 | FCallAwait of num_params * class_id * function_id
378 | FCallUnpack of num_params
379 | FCallBuiltin of num_params * num_params * Litstr.id
381 type instruct_base =
382 | BaseNC of stack_index * MemberOpMode.t
383 | BaseNL of local_id * MemberOpMode.t
384 | FPassBaseNC of param_num * stack_index
385 | FPassBaseNL of param_num * local_id
386 | BaseGC of stack_index * MemberOpMode.t
387 | BaseGL of local_id * MemberOpMode.t
388 | FPassBaseGC of param_num * stack_index
389 | FPassBaseGL of param_num * local_id
390 | BaseSC of stack_index * classref_id
391 | BaseSL of local_id * stack_index
392 | BaseL of local_id * MemberOpMode.t
393 | FPassBaseL of param_num * local_id
394 | BaseC of stack_index
395 | BaseR of stack_index
396 | BaseH
397 | Dim of MemberOpMode.t * MemberKey.t
398 | FPassDim of param_num * MemberKey.t
400 type instruct_final =
401 | QueryM of num_params * QueryOp.t * MemberKey.t
402 | VGetM of num_params * MemberKey.t
403 | FPassM of param_num * num_params * MemberKey.t
404 | SetM of num_params * MemberKey.t
405 | IncDecM of num_params * incdec_op * MemberKey.t
406 | SetOpM of num_params * eq_op * MemberKey.t
407 | BindM of num_params * MemberKey.t
408 | UnsetM of num_params * MemberKey.t
409 | SetWithRefLML of local_id * local_id
410 | SetWithRefRML of local_id
412 type instruct_iterator =
413 | IterInit of Iterator.t * Label.t * local_id
414 | IterInitK of Iterator.t * Label.t * local_id * local_id
415 | WIterInit of Iterator.t * Label.t * local_id
416 | WIterInitK of Iterator.t * Label.t * local_id * local_id
417 | MIterInit of Iterator.t * Label.t * local_id
418 | MIterInitK of Iterator.t * Label.t * local_id * local_id
419 | IterNext of Iterator.t * Label.t * local_id
420 | IterNextK of Iterator.t * Label.t * local_id * local_id
421 | WIterNext of Iterator.t * Label.t * local_id
422 | WIterNextK of Iterator.t * Label.t * local_id * local_id
423 | MIterNext of Iterator.t * Label.t * local_id
424 | MIterNextK of Iterator.t * Label.t * local_id * local_id
425 | IterFree of Iterator.t
426 | MIterFree of Iterator.t
427 | CIterFree of Iterator.t
428 | IterBreak of Label.t * (bool * Iterator.t) list
430 type instruct_include_eval_define =
431 | Incl
432 | InclOnce
433 | Req
434 | ReqOnce
435 | ReqDoc
436 | Eval
437 | AliasCls of Litstr.id * Litstr.id
438 | DefFunc of function_num
439 | DefCls of class_num
440 | DefClsNop of class_num
441 | DefCns of const_id
442 | DefTypeAlias of typedef_num
444 type bare_this_op =
445 | Notice
446 | NoNotice
448 type class_kind =
449 | KClass
450 | KInterface
451 | KTrait
453 type op_silence =
454 | Start
455 | End
457 type instruct_misc =
458 | This
459 | BareThis of bare_this_op
460 | CheckThis
461 | InitThisLoc of local_id
462 | StaticLoc of local_id * Litstr.id
463 | StaticLocInit of local_id * Litstr.id
464 | Catch
465 | OODeclExists of class_kind
466 | VerifyParamType of param_id
467 | VerifyRetTypeC
468 | VerifyRetTypeV
469 | Self of classref_id
470 | Parent of classref_id
471 | LateBoundCls of classref_id
472 | ClsRefName of classref_id
473 | NativeImpl
474 | IncStat of int * int (* counter id, value *)
475 | AKExists
476 | CreateCl of num_params * class_num
477 | Idx
478 | ArrayIdx
479 | AssertRATL of local_id * repo_auth_type
480 | AssertRATStk of stack_index * repo_auth_type
481 | BreakTraceHint
482 | Silence of local_id * op_silence
483 | GetMemoKeyL of local_id
484 | VarEnvDynCall
485 | IsUninit
486 | CGetCUNop
487 | UGetCUNop
488 | MemoSet of int * local_id * int
489 | MemoGet of int * local_id * int
490 | IsMemoType
491 | MaybeMemoType
493 type gen_creation_execution =
494 | CreateCont
495 | ContEnter
496 | ContRaise
497 | Yield
498 | YieldK
499 | ContCheck of check_started
500 | ContValid
501 | ConStarted
502 | ContKey
503 | ContGetReturn
505 type gen_delegation =
506 | ContAssignDelegate
507 | ContEnterDelegate
508 | YieldFromDelegate
509 | ContUnsetDelegate of free_iterator
511 type async_functions =
512 | WHResult
513 | Await
515 type instruct_try =
516 | TryCatchBegin
517 | TryCatchMiddle
518 | TryCatchEnd
519 | TryCatchLegacyBegin of Label.t
520 | TryCatchLegacyEnd
521 | TryFaultBegin of Label.t
522 | TryFaultEnd
524 type instruct =
525 | IBasic of instruct_basic
526 | IIterator of instruct_iterator
527 | ILitConst of instruct_lit_const
528 | IOp of instruct_operator
529 | IContFlow of instruct_control_flow
530 | ISpecialFlow of instruct_special_flow
531 | ICall of instruct_call
532 | IMisc of instruct_misc
533 | IGet of instruct_get
534 | IMutator of instruct_mutator
535 | IIsset of instruct_isset
536 | IBase of instruct_base
537 | IFinal of instruct_final
538 | ILabel of Label.t
539 | ITry of instruct_try
540 | IComment of string
541 | IAsync of async_functions
542 | IGenerator of gen_creation_execution
543 | IIncludeEvalDefine of instruct_include_eval_define