Improve refineLocation a little
[hiphop-php.git] / hphp / hhbbc / misc.h
blobf9ab77386b86f40a385e2fb3e75da26c460376f8
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 #ifndef incl_HHBBC_MISC_H_
17 #define incl_HHBBC_MISC_H_
19 #include <chrono>
20 #include <cassert>
22 #include <boost/variant.hpp>
23 #include <memory>
25 #include "hphp/util/trace.h"
26 #include "hphp/util/match.h"
27 #include "hphp/util/low-ptr.h"
29 namespace HPHP {
30 struct StringData;
31 struct ArrayData;
34 namespace HPHP { namespace HHBBC {
36 //////////////////////////////////////////////////////////////////////
39 * Self-documenting type alias for pointers that aren't owned.
41 * This type is intended to imply that someone else has an owning
42 * pointer on this value which is guaranteed to live longer than this
43 * pointer.
45 template<class T> using borrowed_ptr = T*;
47 template<class T>
48 borrowed_ptr<T> borrow(const std::unique_ptr<T>& p) {
49 return p.get();
53 * String that must be a static string, and Array that must be a
54 * static array.
56 using SString = const StringData*;
57 using LSString = LowPtr<const StringData>;
58 using SArray = const ArrayData*;
61 * HHBC evaluation stack flavors.
63 enum class Flavor { C, V, R, F, U, CR, CU, CVU };
66 * Types of parameter preparation (or unknown).
68 enum class PrepKind { Ref, Val, Unknown };
70 using LocalId = uint32_t;
71 constexpr const LocalId NoLocalId = -1;
73 * Special value used by StackElem::equivLoc to indicate that
74 * this element is a dup of the one below.
76 constexpr const LocalId StackDupId = -2;
77 constexpr const LocalId MaxLocalId = StackDupId - 1;
79 using IterId = uint32_t;
80 using ClsRefSlotId = uint32_t;
81 constexpr const ClsRefSlotId NoClsRefSlotId = -1;
82 using BlockId = uint32_t;
83 constexpr const BlockId NoBlockId = -1;
85 //////////////////////////////////////////////////////////////////////
88 * Many places in the code want to bump tracing levels by some amount
89 * for systemlib-related processing. This is the amount they all bump
90 * by.
92 constexpr int kSystemLibBump = 10;
95 * Functions listed in the --trace functions list get trace level bumped by
96 * this amount.
98 constexpr int kTraceFuncBump = -10;
101 * We may run the interpreter collecting stats and when trace is on
102 * the amount of noise is unbearable. This is to keep tracing out
103 * of stats collection.
105 constexpr int kStatsBump = 50;
107 //////////////////////////////////////////////////////////////////////
109 struct trace_time {
110 using clock = std::chrono::high_resolution_clock;
111 using time_point = clock::time_point;
113 explicit trace_time(const char* what,
114 const std::string& extra = std::string{})
115 : what(what)
116 , start(clock::now())
118 if (Trace::moduleEnabledRelease(Trace::hhbbc_time, 1)) {
119 Trace::traceRelease(
120 "%s",
121 folly::format("{}: start{}\n",
122 what,
123 !extra.empty() ? folly::format(" ({})", extra).str() : extra
124 ).str().c_str()
129 ~trace_time() {
130 namespace C = std::chrono;
131 DEBUG_ONLY auto const elapsed = C::duration_cast<C::milliseconds>(
132 clock::now() - start
134 if (Trace::moduleEnabledRelease(Trace::hhbbc_time, 1)) {
135 Trace::traceRelease(
136 "%s",
137 folly::format("{}: {}ms elapsed\n", what, elapsed.count())
138 .str().c_str()
143 trace_time(const trace_time&) = delete;
144 trace_time& operator=(const trace_time&) = delete;
146 private:
147 const char* what;
148 time_point start;
151 //////////////////////////////////////////////////////////////////////
155 #endif