Move the job of converting provided coeffects to ambient coeffects from callee to...
[hiphop-php.git] / hphp / runtime / vm / coeffects.h
blob60a65765787051575bfafe533258e69dbcfda8db
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present 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 #pragma once
19 #include "hphp/runtime/vm/class.h"
21 #include <folly/Optional.h>
23 namespace HPHP {
24 ///////////////////////////////////////////////////////////////////////////////
25 namespace jit {
26 struct SSATmp;
27 namespace irgen {
28 struct IRGS;
31 ///////////////////////////////////////////////////////////////////////////////
32 struct RuntimeCoeffects {
33 using storage_t = uint16_t;
35 static RuntimeCoeffects fromValue(uint16_t value) {
36 return RuntimeCoeffects{value};
39 static RuntimeCoeffects none() {
40 return RuntimeCoeffects::fromValue(0);
43 static RuntimeCoeffects full() {
44 return RuntimeCoeffects::fromValue(std::numeric_limits<storage_t>::max());
47 uint16_t value() const { return m_data; }
49 const std::string toString() const;
51 // Checks whether provided coeffects in `this` can call
52 // required coeffects in `o`
53 bool canCall(const RuntimeCoeffects o) const {
54 // a & b == a
55 // a & ~b == 0
56 return (m_data & (~o.m_data)) == 0;
59 bool canCallWithWarning(const RuntimeCoeffects) const;
61 // This operator is equivalent to | of [coeffectA | coeffectB]
62 RuntimeCoeffects& operator&=(const RuntimeCoeffects);
64 private:
65 explicit RuntimeCoeffects(uint16_t data) : m_data(data) {}
66 storage_t m_data;
69 struct StaticCoeffects {
70 using storage_t = RuntimeCoeffects::storage_t;
72 const folly::Optional<std::string> toString() const;
74 RuntimeCoeffects toAmbient() const;
75 RuntimeCoeffects toRequired() const;
77 // Returns the corresponding shallow bits for coeffects that are local
78 RuntimeCoeffects toShallowWithLocals() const;
80 static StaticCoeffects fromValue(uint16_t value) {
81 return StaticCoeffects{value};
84 uint16_t value() const { return m_data; }
86 static StaticCoeffects none() {
87 return StaticCoeffects::fromValue(0);
90 // This operator is equivalent to & of [coeffectA & coeffectB]
91 StaticCoeffects& operator|=(const StaticCoeffects);
93 template<class SerDe>
94 void serde(SerDe& sd) {
95 sd(m_data);
98 private:
99 // Returns the local bits
100 storage_t locals() const;
102 private:
103 explicit StaticCoeffects(uint16_t data) : m_data(data) {}
104 storage_t m_data;
107 static_assert(sizeof(StaticCoeffects) == sizeof(uint16_t), "");
108 static_assert(sizeof(StaticCoeffects) == sizeof(RuntimeCoeffects), "");
110 ///////////////////////////////////////////////////////////////////////////////
112 struct CoeffectRule final {
113 struct FunParam {};
114 struct CCParam {};
115 struct CCThis {};
117 CoeffectRule() = default;
119 /////////////////////////////////////////////////////////////////////////////
120 // Native coeffect rules ////////////////////////////////////////////////////
122 CoeffectRule(FunParam, uint32_t index)
123 : m_type(Type::FunParam)
124 , m_index(index)
127 CoeffectRule(CCParam, uint32_t index, const StringData* ctx_name)
128 : m_type(Type::CCParam)
129 , m_index(index)
130 , m_name(ctx_name)
131 { assertx(ctx_name); }
133 CoeffectRule(CCThis, const StringData* ctx_name)
134 : m_type(Type::CCThis)
135 , m_name(ctx_name)
136 { assertx(ctx_name); }
138 RuntimeCoeffects emit(const Func*, uint32_t, void*) const;
139 jit::SSATmp* emitJit(jit::irgen::IRGS&, const Func*,
140 uint32_t, jit::SSATmp*) const;
142 folly::Optional<std::string> toString(const Func*) const;
143 std::string getDirectiveString() const;
145 template<class SerDe>
146 void serde(SerDe&);
148 private:
149 enum class Type {
150 Invalid = 0,
152 FunParam,
153 CCParam,
154 CCThis,
157 Type m_type{Type::Invalid};
158 uint32_t m_index{0};
159 LowPtr<const StringData> m_name{nullptr};
162 ///////////////////////////////////////////////////////////////////////////////