2 +----------------------------------------------------------------------+
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 #ifndef incl_HHBBC_EVAL_CELL_H_
17 #define incl_HHBBC_EVAL_CELL_H_
22 #include <folly/ScopeGuard.h>
23 #include <folly/Optional.h>
25 #include "hphp/runtime/base/array-data.h"
26 #include "hphp/runtime/base/execution-context.h"
27 #include "hphp/runtime/base/string-data.h"
28 #include "hphp/runtime/base/tv-refcount.h"
29 #include "hphp/runtime/vm/repo.h"
31 #include "hphp/hhbbc/hhbbc.h"
32 #include "hphp/hhbbc/type-system.h"
34 namespace HPHP
{ namespace HHBBC
{
36 //////////////////////////////////////////////////////////////////////
39 * When constant-evaluating certain operations, it's possible they
40 * will return non-static objects, or throw exceptions (e.g. cellAdd()
41 * with an array and an int).
43 * This routine converts these things back to types. In the case of
44 * an exception it returns TInitCell.
47 folly::Optional
<Type
> eval_cell(Pred p
) {
49 assert(!RuntimeOption::EvalJit
);
50 ThrowAllErrorsSetter taes
;
53 if (isRefcountedType(c
.m_type
)) {
57 if (c
.m_data
.pstr
->size() > Repo::get().stringLengthLimit()) {
58 tvDecRefCountable(&c
);
61 auto const sstr
= makeStaticString(c
.m_data
.pstr
);
62 tvDecRefCountable(&c
);
63 c
= make_tv
<KindOfPersistentString
>(sstr
);
68 auto const sarr
= ArrayData::GetScalarArray(c
.m_data
.parr
);
69 tvDecRefCountable(&c
);
70 c
= make_tv
<KindOfPersistentArray
>(sarr
);
75 auto const sarr
= ArrayData::GetScalarArray(c
.m_data
.parr
);
76 tvDecRefCountable(&c
);
77 c
= make_tv
<KindOfPersistentVec
>(sarr
);
82 auto const sarr
= ArrayData::GetScalarArray(c
.m_data
.parr
);
83 tvDecRefCountable(&c
);
84 c
= make_tv
<KindOfPersistentDict
>(sarr
);
89 auto const sarr
= ArrayData::GetScalarArray(c
.m_data
.parr
);
90 tvDecRefCountable(&c
);
91 c
= make_tv
<KindOfPersistentKeyset
>(sarr
);
99 case KindOfPersistentString
:
100 case KindOfPersistentArray
:
101 case KindOfPersistentVec
:
102 case KindOfPersistentDict
:
103 case KindOfPersistentKeyset
:
107 always_assert(0 && "Impossible constant evaluation occurred");
112 * We need to get rid of statics if we're not actually going to do
113 * constant propagation. When ConstantProp is on, the types we
114 * create here can reflect that we'll be changing bytecode later
115 * to actually make these into non-reference-counted SStr or
116 * SArrs. If we leave the bytecode alone, though, it generally
117 * won't actually be static at runtime.
119 * TODO(#3696042): loosen_statics here should ideally not give up
120 * on the array or string value, just its staticness.
122 auto const t
= from_cell(c
);
123 return options
.ConstantProp
? t
: loosen_statics(t
);
124 } catch (const Object
&) {
126 } catch (const std::exception
&) {
129 always_assert_flog(0, "a non-std::exception was thrown in eval_cell");
133 template<typename Pred
>
134 folly::Optional
<typename
std::result_of
<Pred()>::type
>
135 eval_cell_value(Pred p
) {
137 ThrowAllErrorsSetter taes
;
139 } catch (const Object
&) {
141 } catch (const std::exception
&) {
144 always_assert_flog(0, "a non-std::exception was thrown in eval_cell_value");
148 //////////////////////////////////////////////////////////////////////