Use shape values at runtime for position data
[hiphop-php.git] / hphp / hhbbc / misc.h
blob04832d4428d91d3ca3fca5f705ad56cd3f3df138
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 <chrono>
19 #include <cassert>
21 #include <boost/variant.hpp>
22 #include <memory>
24 #include "hphp/util/compact-vector.h"
25 #include "hphp/util/low-ptr.h"
26 #include "hphp/util/match.h"
27 #include "hphp/util/trace.h"
29 namespace HPHP {
30 struct StringData;
31 struct ArrayData;
34 namespace HPHP { namespace HHBBC {
36 //////////////////////////////////////////////////////////////////////
39 * String that must be a static string, and Array that must be a
40 * static array.
42 using SString = const StringData*;
43 using LSString = LowPtr<const StringData>;
44 using SArray = const ArrayData*;
46 struct Bytecode;
47 using BytecodeVec = CompactVector<Bytecode>;
50 * HHBC evaluation stack flavors.
52 enum class Flavor { C, U, CU };
55 * Types of parameter preparation (or unknown).
57 enum class PrepKind { InOut, Val, Unknown };
59 using LocalId = uint32_t;
60 constexpr const LocalId NoLocalId = -1;
62 * Special value used by StackElem::equivLoc to indicate that
63 * this element is a dup of the one below.
65 constexpr const LocalId StackDupId = -2;
66 constexpr const LocalId StackThisId = -3;
67 constexpr const LocalId MaxLocalId = StackThisId - 1;
69 using IterId = uint32_t;
70 using BlockId = uint32_t;
71 constexpr const BlockId NoBlockId = -1;
72 using ExnNodeId = uint32_t;
73 constexpr const ExnNodeId NoExnNodeId = -1;
75 //////////////////////////////////////////////////////////////////////
78 * Many places in the code want to bump tracing levels by some amount
79 * for systemlib-related processing. This is the amount they all bump
80 * by.
82 constexpr int kSystemLibBump = 10;
85 * Functions listed in the --trace functions list get trace level bumped by
86 * this amount.
88 constexpr int kTraceFuncBump = -10;
91 * We may run the interpreter collecting stats and when trace is on
92 * the amount of noise is unbearable. This is to keep tracing out
93 * of stats collection.
95 constexpr int kStatsBump = 50;
97 //////////////////////////////////////////////////////////////////////
99 void profile_memory(const char* what, const char* when, const std::string&);
100 void summarize_memory();
102 struct trace_time {
103 using clock = std::chrono::system_clock;
104 using time_point = clock::time_point;
106 explicit trace_time(const char* what,
107 const std::string& extra = std::string{})
108 : what(what)
109 , start(clock::now())
110 , extra(extra)
112 profile_memory(what, "start", extra);
113 if (Trace::moduleEnabledRelease(Trace::hhbbc_time, 1)) {
114 Trace::traceRelease(
115 "%s",
116 folly::sformat(
117 "{}: {}: start{}\n",
118 ts(start),
119 what,
120 !extra.empty() ? folly::format(" ({})", extra).str() : extra
121 ).c_str()
126 ~trace_time() {
127 namespace C = std::chrono;
128 auto const end = clock::now();
129 auto const elapsed = C::duration_cast<C::milliseconds>(
130 end - start
132 profile_memory(what, "end", extra);
133 if (Trace::moduleEnabledRelease(Trace::hhbbc_time, 1)) {
134 Trace::traceRelease(
135 "%s",
136 folly::sformat(
137 "{}: {}: {}ms elapsed\n",
138 ts(end), what, elapsed.count())
139 .c_str()
144 trace_time(const trace_time&) = delete;
145 trace_time& operator=(const trace_time&) = delete;
147 private:
148 std::string ts(time_point t) {
149 char snow[64];
150 auto tm = std::chrono::system_clock::to_time_t(t);
151 ctime_r(&tm, snow);
152 // Eliminate trailing newline from ctime_r.
153 snow[24] = '\0';
154 return snow;
156 const char* what;
157 time_point start;
158 std::string extra;
161 //////////////////////////////////////////////////////////////////////