Allow early initialization of classes if they don't have any [sp]init methods
[hiphop-php.git] / hphp / runtime / vm / srckey.h
blob8c724565fc7d94d561881fccd130dee0d9eac175
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2013 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_SRCKEY_H_
17 #define incl_HPHP_SRCKEY_H_
19 #include <tuple>
20 #include <boost/operators.hpp>
22 #include "hphp/runtime/vm/func.h"
23 #include "hphp/runtime/vm/core_types.h"
25 namespace HPHP {
27 //////////////////////////////////////////////////////////////////////
30 * A SrcKey is a logical source instruction---it's enough to identify
31 * these using a (Func id, hhbc instruction) pair.
33 struct SrcKey : private boost::totally_ordered<SrcKey> {
34 typedef uint64_t AtomicInt;
35 struct Hasher;
37 SrcKey()
38 : m_funcId(InvalidFuncId)
39 , m_offset(0)
42 SrcKey(const Func* f, Offset off)
43 : m_funcId(f->getFuncId())
44 , m_offset(off)
47 SrcKey(const Func* f, const Opcode* i)
48 : m_funcId(f->getFuncId())
49 , m_offset(f->unit()->offsetOf(i))
52 SrcKey(FuncId funcId, Offset off)
53 : m_funcId{funcId}
54 , m_offset{off}
57 // Packed representation of SrcKeys for use in contexts where we
58 // want atomicity. (SrcDB.)
59 AtomicInt toAtomicInt() const {
60 return uint64_t(getFuncId()) << 32 | uint64_t(offset());
62 static SrcKey fromAtomicInt(AtomicInt in) {
63 return SrcKey { uint32_t(in >> 32), (Offset) int32_t(in & 0xffffffff) };
66 void setFuncId(FuncId id) {
67 assert(id != InvalidFuncId);
68 m_funcId = id;
71 FuncId getFuncId() const {
72 assert(m_funcId != InvalidFuncId);
73 return m_funcId;
76 int offset() const {
77 return m_offset;
81 * Advance the SrcKey to the next instruction.
83 * If the SrcKey points to the last instruction in a function, this
84 * will advance past the end of the function, and potentially
85 * contain an invalid bytecode offset.
87 void advance(const Unit* u) {
88 m_offset += instrLen(u->at(offset()));
92 * Return a SrcKey representing the next instruction, without
93 * mutating this SrcKey.
95 SrcKey advanced(const Unit* u) const {
96 auto tmp = *this;
97 tmp.advance(u);
98 return tmp;
101 bool operator==(const SrcKey& r) const {
102 return offset() == r.offset() &&
103 getFuncId() == r.getFuncId();
106 bool operator<(const SrcKey& r) const {
107 return std::make_tuple(offset(), getFuncId()) <
108 std::make_tuple(r.offset(), r.getFuncId());
111 private:
112 FuncId m_funcId;
113 Offset m_offset;
116 struct SrcKey::Hasher {
117 size_t operator()(SrcKey sk) const {
118 return hash_int64_pair(sk.getFuncId(), uint64_t(sk.offset()));
122 //////////////////////////////////////////////////////////////////////
126 #endif