Deshim VirtualExecutor in folly
[hiphop-php.git] / hphp / hhbbc / interp.h
blobdae4a83cdedf5241b61d482a2f243a00f176622e
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 +----------------------------------------------------------------------+
16 #pragma once
18 #include <functional>
19 #include <vector>
20 #include <bitset>
22 #include "hphp/hhbbc/bc.h"
23 #include "hphp/hhbbc/context.h"
24 #include "hphp/hhbbc/index.h"
25 #include "hphp/hhbbc/misc.h"
26 #include "hphp/hhbbc/type-system.h"
28 namespace HPHP::HHBBC {
30 struct PropertiesInfo;
31 struct CollectedInfo;
32 struct State;
33 struct StateMutationUndo;
34 struct StepFlags;
35 struct Bytecode;
36 struct ISS;
37 namespace php { struct Block; }
38 namespace res { struct Func; }
40 //////////////////////////////////////////////////////////////////////
42 struct BlockUpdateInfo {
43 BlockId fallthrough{NoBlockId};
44 uint32_t unchangedBcs{0};
45 CompactVector<Bytecode> replacedBcs;
49 * RunFlags are information about running an entire block in the
50 * interpreter.
52 struct RunFlags {
54 * If this is not none, the interpreter executed a return in this
55 * block, with this type.
57 Optional<Type> returned;
60 * If returned is set, and the returned value was a parameter,
61 * retParam will be set to the parameter's id; otherwise it will be
62 * NoLocalId.
64 LocalId retParam{NoLocalId};
65 BlockUpdateInfo updateInfo;
68 * See FuncAnalysisResult for details.
70 std::bitset<64> usedParams;
72 bool noThrow{true};
75 //////////////////////////////////////////////////////////////////////
77 constexpr int kMaxTrackedLocals = 512;
80 * StepFlags are information about the effects of a single opcode.
81 * Each single-instruction step of the interpreter sends various
82 * effects information back to the caller in this structure.
84 struct StepFlags {
86 * Potentially Exception-throwing Instruction.
88 * Instructions are assumed to be PEIs unless the abstract
89 * interpreter says they aren't. A PEI must propagate the state
90 * from before the instruction across all throw exit edges.
92 * Some instructions that can throw with mid-opcode states need to
93 * handle those cases specially.
95 bool wasPEI = true;
98 * If an instruction sets this flag, it means that if it pushed a
99 * type with a constant value, it had no side effects other than
100 * computing the value which was pushed. This means the instruction
101 * can be replaced with pops of its inputs followed by a push of the
102 * constant.
104 bool canConstProp = false;
107 * If an instruction sets this flag, it means that this
108 * instruction doesn't prevent a call to the containing function
109 * from being discarded if its result is unneeded.
111 * Instructions that are marked canConstProp that also produce a
112 * constant result automatically set this flag.
114 bool effectFree = false;
117 * Set by impl_vec to indicate that this instruction was already
118 * dealt with via reduce.
120 bool reduced = false;
123 * If set to something other than NoBlockId, then this block
124 * unconditionally falls through to that block.
126 BlockId jmpDest = NoBlockId;
129 * If an instruction may read or write to locals, these flags
130 * indicate which ones. We don't track this information for local
131 * ids past kMaxTrackedLocals, which are assumed to always be in
132 * this set.
134 * This is currently used to try to leave out unnecessary type
135 * assertions on locals (for options.FilterAssertions), and as a
136 * conservative list of variables that should be added to the gen
137 * set for global dce.
139 * The latter use means these flags must be conservative in the
140 * direction of which locals are read. That is: an instruction may
141 * not read a local that isn't mentioned in this set.
143 std::bitset<kMaxTrackedLocals> mayReadLocalSet;
146 * See FuncAnalysisResult for details.
148 std::bitset<64> usedParams;
151 * If this is not none, the interpreter executed a return on this
152 * step, with this type.
154 Optional<Type> returned;
157 * If returned is set, and the returned value was a parameter,
158 * retParam will be set to the parameter's id; otherwise it will be
159 * NoLocalId.
161 LocalId retParam{NoLocalId};
164 //////////////////////////////////////////////////////////////////////
167 * Context for running the block interpreter (either on a single
168 * instruction, or for a whole block).
170 struct Interp {
171 const IIndex& index;
172 AnalysisContext ctx;
173 CollectedInfo& collect;
174 const BlockId bid;
175 const php::Block* blk;
176 State& state;
177 StateMutationUndo* undo = nullptr;
181 * Step a single instruction in the block, and hand back flags
183 * This entry point is used to propagate block entry states to
184 * mid-block positions after the global analysis has already finished.
186 StepFlags step(Interp&, const Bytecode& op);
189 * Run a whole block. Returns a type that should be merged into the
190 * function return value, or TBottom.
192 * If a branch is taken or an exception is thrown, the supplied
193 * callback is used to indicate when/where the state referenced in the
194 * Interp structure should be propagated.
196 * If the PropagateFn is called with a nullptr State, it means that
197 * the given block should be re-processed.
199 * If the block needs to be reprocessed, RollbackFn will be called to
200 * "roll-back" any states propagated to other blocks.
202 using PropagateFn = std::function<void (BlockId, const State*)>;
203 using RollbackFn = std::function<void()>;
204 RunFlags run(Interp&, const State& in, const PropagateFn&, const RollbackFn&);
207 * Dispatch a bytecode to the default interpreter.
209 * This entry point is used by custom interpreters that need to add
210 * some logic to the default interpreter but want to run it otherwise.
211 * Calling step() does not give control over the state (ISS instance)
212 * which a custom interpreter may need to specialize.
214 void default_dispatch(ISS&, const Bytecode&);
217 * Can this call be converted to an FCallBuiltin
219 bool optimize_builtin(ISS& env, const php::Func* func, const FCallArgs& fca);
222 * Static list of all builtins which can be potentially optimized
223 * specially.
225 const std::vector<SString>& special_builtins();
227 Optional<Type>
228 const_fold(ISS& env, uint32_t nArgs, uint32_t numExtraInputs,
229 const php::Func& phpFunc, bool variadicsPacked);
232 * Extracts name from the type either by using a reified name specialization or
233 * by looking at the typed value
235 SString getNameFromType(const Type& t);
237 //////////////////////////////////////////////////////////////////////