/usr/include/hphp/hhbbc/representation.h is in hhvm-dev 3.21.0+dfsg-2ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 | /*
+----------------------------------------------------------------------+
| HipHop for PHP |
+----------------------------------------------------------------------+
| Copyright (c) 2010-present 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_HHBBC_REPRESENTATION_H_
#define incl_HHBBC_REPRESENTATION_H_
#include <list>
#include <memory>
#include <string>
#include <tuple>
#include <unordered_map>
#include <utility>
#include <vector>
#include <boost/variant.hpp>
#include "hphp/util/atomic-vector.h"
#include "hphp/util/compact-vector.h"
#include "hphp/util/md5.h"
#include "hphp/runtime/base/user-attributes.h"
#include "hphp/runtime/vm/preclass.h"
#include "hphp/runtime/vm/type-alias.h"
#include "hphp/runtime/vm/type-constraint.h"
#include "hphp/hhbbc/bc.h"
#include "hphp/hhbbc/misc.h"
#include "hphp/hhbbc/src-loc.h"
namespace HPHP { namespace HHBBC {
namespace php {
//////////////////////////////////////////////////////////////////////
struct Func;
struct ExnNode;
struct Unit;
//////////////////////////////////////////////////////////////////////
struct SrcInfo {
LineRange loc;
LSString docComment;
};
//////////////////////////////////////////////////////////////////////
/*
* A basic block in our factored control flow graph.
*
* Blocks terminate on control flow, except exceptional control flow.
* We keep a set of "factored edges" representing all possible early
* exits due to exceptional control flow.
*/
struct Block {
/*
* Blocks in HHBC are each part of a bytecode "section". The section
* is either the "primary function body", or a fault funclet. We
* represent fault funclet sections with unique ids.
*
* Each section must be a contiguous region of bytecode, with the
* primary function body first. These ids are tracked just to
* maintain this invariant at emit time.
*/
enum class Section : uint32_t { Main = 0 };
Section section;
/*
* Blocks have unique ids within a given function.
*/
uint32_t id;
/*
* Instructions in the block. Never empty guarantee.
*/
std::vector<Bytecode> hhbcs;
/*
* The pointer for this block's exception region, or nullptr if
* there is none.
*/
borrowed_ptr<ExnNode> exnNode;
/*
* Edges coming out of blocks are repesented in three ways:
*
* - fallthrough edges (the end of the block unconditionally jumps
* to the named block). If fallthroughNS is true, this edge
* represents a no-surprise jump.
*
* - Taken edges (these are encoded in the last instruction in hhbcs).
*
* - factoredExits (these represent edges traversed for exceptions
* mid-block)
*
* For the idea behind the factored exit edge thing, see "Efficient
* and Precise Modeling of Exceptions for the Analysis of Java
* Programs" (http://dl.acm.org/citation.cfm?id=316171).
*/
BlockId fallthrough{NoBlockId};
bool fallthroughNS = false;
CompactVector<BlockId> factoredExits;
};
//////////////////////////////////////////////////////////////////////
/*
* Exception regions.
*
* Each block in the program body can have a pointer to a node in the
* exception handler tree. This means they are in all the "exception
* regions" for each node in the tree down to that node. This
* information is used to construct exception handling regions at emit
* time.
*
* There are two types of regions; CatchRegions and FaultRegions.
* These correspond to the two types of regions described in
* bytecode.specification. Note though that although it's not
* specified there, in addition to an entry offset, these regions
* optionally list some information about iterators if the reason the
* region is there is to free iterator variables.
*
* Exceptional control flow is also represented more explicitly with
* factored exit edges (see php::Block). This tree structure just
* exists to get the EHEnts right.
*
* Note: blocks in fault funclets will have factored edges to the
* blocks listed as handlers in any ExnNode that contained the
* fault-protected region, since those control flow paths are
* possible. Generally they will have nullptr for their exnNode
* pointers, however, although they may also have other EH-protected
* regions inside of them (this currently occurs in the case of
* php-level finally blocks cloned into fault funclets).
*/
struct FaultRegion { BlockId faultEntry;
Id iterId;
bool itRef; };
struct CatchRegion { BlockId catchEntry;
Id iterId;
bool itRef; };
struct ExnNode {
uint32_t id;
borrowed_ptr<ExnNode> parent;
CompactVector<std::unique_ptr<ExnNode>> children;
boost::variant<FaultRegion,CatchRegion> info;
};
//////////////////////////////////////////////////////////////////////
/*
* Metadata about a parameter to a php function.
*/
struct Param {
/*
* Default value for this parameter, or KindOfUninit if it has no
* default value.
*/
Cell defaultValue;
/*
* Pointer to the block we'll enter for default-value initialization
* of this parameter, or nullptr if this parameter had no default
* value initializer.
*/
BlockId dvEntryPoint;
/*
* Information about the parameter's typehint, if any.
*
* NOTE: this is represented in the repo as a string type name and
* some TypeConstraint::Flags.
*/
TypeConstraint typeConstraint;
/*
* User-visible version of the type constraint as a string.
* Propagated for reflection.
*/
LSString userTypeConstraint;
/*
* Evalable php code that will give the default argument. This is
* redundant with the dv initializer, but gets propagated through
* for reflection.
*/
LSString phpCode;
/*
* Each parameter of a func can have arbitrary user attributes.
*/
UserAttributeMap userAttributes;
/*
* The type of the arguments for builtin functions, or for HNI
* functions with a native implementation. folly::none for
* non-builtins.
*/
folly::Optional<DataType> builtinType;
/*
* Whether this parameter is passed by reference.
*/
bool byRef: 1;
/*
* Whether this parameter must be passed by reference.
* The FPassCE and FPassCW opcodes will only produce
* an error/warnging if this is set.
*/
bool mustBeRef: 1;
/*
* Whether this parameter is a variadic capture.
*/
bool isVariadic: 1;
};
/*
* Metadata about a local variable in a function. Name may be
* nullptr, for unnamed locals.
*/
struct Local {
LSString name;
uint32_t id : 31;
uint32_t killed : 1;
};
/*
* Static local information. For each static local, we need to keep
* the php code around for reflection.
*/
struct StaticLocalInfo {
LSString name;
};
/*
* Extra information for function with a HNI native implementation.
*/
struct NativeInfo {
/*
* Return type from the C++ implementation function, as an optional DataType;
* folly::none stands for a Variant return.
*/
folly::Optional<DataType> returnType;
/*
* Associated dynamic call wrapper function. Used to catch dynamic calls to
* caller frame affecting functions.
*/
Id dynCallWrapperId;
};
/*
* Representation of a function, class method, or pseudomain function.
*/
struct Func {
/*
* Basic information about the function.
*/
LSString name;
SrcInfo srcInfo;
Attr attrs;
/*
* Parameters, locals, class-refs, and iterators.
*
* There are at least as many locals as parameters (parameters are
* also locals---the names of parameters are stored in the locals
* vector).
*/
IterId numIters;
ClsRefSlotId numClsRefSlots;
CompactVector<Param> params;
CompactVector<Local> locals;
CompactVector<StaticLocalInfo> staticLocals;
/*
* Which unit defined this function. If it is a method, the cls
* field will be set to the class that contains it.
*/
borrowed_ptr<Unit> unit;
borrowed_ptr<Class> cls;
/*
* All owning pointers to blocks are in this vector, which has the
* blocks in an unspecified order. Blocks have borrowed pointers to
* each other to represent control flow arcs.
*/
CompactVector<std::unique_ptr<Block>> blocks;
/*
* Try and fault regions form a tree structure. The tree is hanging
* off the func here, with children pointers. Each block that is
* within a try or fault region has a pointer to the inner-most
* ExnNode protecting it.
*
* Note that this, together with the Blocks' factoredExits are updated
* during the concurrent analyze pass.
*/
CompactVector<std::unique_ptr<ExnNode>> exnNodes;
/*
* Entry point blocks for default value initializers.
*
* Note that in PHP you can declare functions where some of the
* earlier parameters have default values, and later ones don't. In
* this case we'll have NoBlockIds after the first valid entry here.
*/
CompactVector<BlockId> dvEntries;
/*
* Entry point to the function when the number of passed args is
* equal to the number of parameters.
*/
BlockId mainEntry;
/*
* User-visible return type specification as a string. This is only
* passed through to expose it to reflection.
*/
LSString returnUserType;
/*
* If traits are being flattened by hphpc, we keep the original
* filename of a function (the file that defined the trait) so
* backtraces and things work correctly. Otherwise this is nullptr.
*/
LSString originalFilename;
/*
* Whether or not this function is a top-level function. (Defined
* outside of any other function body.)
*/
bool top : 1;
/*
* This is the generated function for a closure body. I.e. this
* function contains the code that should run when the closure is
* invoked.
*/
bool isClosureBody : 1;
/*
* This is an async function.
*/
bool isAsync : 1;
/*
* This is a generator.
*/
bool isGenerator : 1;
/*
* This generator yields key value pairs.
*/
bool isPairGenerator : 1;
bool isMemoizeWrapper : 1;
bool isMemoizeImpl : 1;
/*
* Return type specified in the source code (ex. "function foo(): Bar").
* HHVM checks if the a function's return value matches it's return type
* constraint via the VerifyRetType* instructions.
*/
TypeConstraint retTypeConstraint;
/*
* User attribute list.
*/
UserAttributeMap userAttributes;
/*
* For HNI-based extensions, additional information for functions
* with a native-implementation is here. If this isn't a function
* with an HNI-based native implementation, this will be nullptr.
*/
std::unique_ptr<NativeInfo> nativeInfo;
};
//////////////////////////////////////////////////////////////////////
/*
* A class property.
*
* Both static and instance properties use this structure.
*/
struct Prop {
LSString name;
Attr attrs;
LSString docComment;
/*
* Properties can have string type constraints, which we need to
* propagate through just for reflection purposes.
*/
LSString typeConstraint;
/*
* The default value of the property, for properties with scalar
* initializers. May be KindOfUninit in some cases where the
* property should not have an initial value (i.e. not even null).
*/
Cell val;
};
/*
* A class constant.
*/
struct Const {
LSString name;
// The class that defined this constant.
borrowed_ptr<php::Class> cls;
/*
* The value will be KindOfUninit if the class constant is defined
* using an 86cinit method.
*
* The lack of a value represents an abstract class constant.
*/
folly::Optional<Cell> val;
/*
* We pass through eval'able php code and a string type constraint,
* only for exposure to reflection.
*/
LSString phpCode;
LSString typeConstraint;
bool isTypeconst;
};
/*
* Representation of a php class declaration.
*/
struct Class {
/*
* Basic information about the class.
*/
LSString name;
SrcInfo srcInfo;
Attr attrs;
/*
* The id used to reference the class within its unit
*/
int32_t id;
/*
* Which unit defined this class.
*/
borrowed_ptr<Unit> unit;
/*
* Hoistability of this class. See the description in class.h
* formation on hoistability.
*/
PreClass::Hoistable hoistability;
/*
* Name of the parent class.
*/
LSString parentName;
/*
* If this class represents a closure, this points to the class that
* lexically contains the closure, if there was one. If this class
* doesn't represent a closure, this will be nullptr.
*
* The significance of this is that closures created lexically
* inside of a class run as if they were part of that class context
* (with regard to access checks, etc).
*/
borrowed_ptr<php::Class> closureContextCls;
/*
* Names of inherited interfaces.
*/
CompactVector<LowStringPtr> interfaceNames;
/*
* Names of used traits, number of declared (i.e., non-trait, non-inherited)
* methods, trait alias/precedence rules (if any).
*
* This is using the exact structures from the runtime PreClass. In
* WholeProgram mode, we won't see these because traits will already be
* flattened.
*/
CompactVector<LowStringPtr> usedTraitNames;
CompactVector<PreClass::ClassRequirement> requirements;
CompactVector<PreClass::TraitPrecRule> traitPrecRules;
CompactVector<PreClass::TraitAliasRule> traitAliasRules;
int32_t numDeclMethods;
/*
* Methods on the class. If there's an 86cinit, it must be last.
*/
CompactVector<std::unique_ptr<php::Func>> methods;
/*
* Properties defined on this class.
*/
CompactVector<Prop> properties;
/*
* Constants defined on this class.
*/
CompactVector<Const> constants;
/*
* User attributes for this class declaration.
*/
UserAttributeMap userAttributes;
/*
* The underlying base type, if this is an enum
*/
TypeConstraint enumBaseTy;
};
//////////////////////////////////////////////////////////////////////
using TypeAlias = ::HPHP::TypeAlias;
//////////////////////////////////////////////////////////////////////
/*
* Representation of a php file (normal compilation unit).
*/
struct Unit {
MD5 md5;
LSString filename;
bool isHHFile{false};
bool useStrictTypes{false};
bool useStrictTypesForBuiltins{false};
std::atomic<bool> persistent{true};
int preloadPriority{0};
std::unique_ptr<Func> pseudomain;
CompactVector<std::unique_ptr<Func>> funcs;
CompactVector<std::unique_ptr<Class>> classes;
CompactVector<std::unique_ptr<TypeAlias>> typeAliases;
CompactVector<std::pair<SString,SString>> classAliases;
CompactVector<SrcLoc> srcLocs;
};
/*
* A php Program is a set of compilation units.
*/
struct Program {
enum CInit {
ForAnalyze = 1,
ForOptimize = 2,
ForAll = ForAnalyze | ForOptimize,
};
explicit Program(size_t numUnitsGuess) :
nextConstInit(0),
constInits(100 + (numUnitsGuess / 4), 0) {
}
static uintptr_t tagged_func(Func* f, CInit ci) {
return reinterpret_cast<uintptr_t>(f) | ci;
}
std::vector<std::unique_ptr<Unit>> units;
std::atomic<size_t> nextConstInit;
AtomicVector<uintptr_t> constInits;
};
//////////////////////////////////////////////////////////////////////
std::string show(const Func&);
std::string show(const Class&);
std::string show(const Unit&);
std::string show(const Program&);
std::string local_string(const Func&, LocalId);
//////////////////////////////////////////////////////////////////////
bool check(const Func&);
bool check(const Class&);
bool check(const Unit&);
bool check(const Program&);
//////////////////////////////////////////////////////////////////////
}}}
#endif
|