1 // Copyright 2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef V8_SAFEPOINT_TABLE_H_
29 #define V8_SAFEPOINT_TABLE_H_
33 #include "macro-assembler.h"
40 class SafepointTable BASE_EMBEDDED {
42 explicit SafepointTable(Code* code);
46 (length_ * (kPcAndDeoptimizationIndexSize + entry_size_)); }
47 unsigned length() const { return length_; }
48 unsigned entry_size() const { return entry_size_; }
50 unsigned GetPcOffset(unsigned index) const {
51 ASSERT(index < length_);
52 return Memory::uint32_at(GetPcOffsetLocation(index));
55 int GetDeoptimizationIndex(unsigned index) const {
56 ASSERT(index < length_);
57 unsigned value = Memory::uint32_at(GetDeoptimizationLocation(index));
58 return DeoptimizationIndexField::decode(value);
61 unsigned GetGapCodeSize(unsigned index) const {
62 ASSERT(index < length_);
63 unsigned value = Memory::uint32_at(GetDeoptimizationLocation(index));
64 return GapCodeSizeField::decode(value);
67 uint8_t* GetEntry(unsigned index) const {
68 ASSERT(index < length_);
69 return &Memory::uint8_at(entries_ + (index * entry_size_));
72 // Reserve 13 bits for the gap code size. On ARM a constant pool can be
73 // emitted when generating the gap code. The size of the const pool is less
74 // than what can be represented in 12 bits, so 13 bits gives room for having
75 // instructions before potentially emitting a constant pool.
76 static const int kGapCodeSizeBits = 13;
77 static const int kDeoptIndexBits = 32 - kGapCodeSizeBits;
78 class GapCodeSizeField: public BitField<unsigned, 0, kGapCodeSizeBits> {};
79 class DeoptimizationIndexField: public BitField<int, kGapCodeSizeBits, kDeoptIndexBits> {}; // NOLINT
81 static bool HasRegisters(uint8_t* entry);
82 static bool HasRegisterAt(uint8_t* entry, int reg_index);
84 void PrintEntry(unsigned index) const;
87 static const uint8_t kNoRegisters = 0xFF;
89 static const int kLengthOffset = 0;
90 static const int kEntrySizeOffset = kLengthOffset + kIntSize;
91 static const int kHeaderSize = kEntrySizeOffset + kIntSize;
93 static const int kPcSize = kIntSize;
94 static const int kDeoptimizationIndexSize = kIntSize;
95 static const int kPcAndDeoptimizationIndexSize =
96 kPcSize + kDeoptimizationIndexSize;
98 Address GetPcOffsetLocation(unsigned index) const {
99 return pc_and_deoptimization_indexes_ +
100 (index * kPcAndDeoptimizationIndexSize);
103 Address GetDeoptimizationLocation(unsigned index) const {
104 return GetPcOffsetLocation(index) + kPcSize;
107 static void PrintBits(uint8_t byte, int digits);
109 AssertNoAllocation no_allocation_;
112 unsigned entry_size_;
114 Address pc_and_deoptimization_indexes_;
117 friend class SafepointTableBuilder;
121 class Safepoint BASE_EMBEDDED {
123 static const int kNoDeoptimizationIndex =
124 (1 << (SafepointTable::kDeoptIndexBits)) - 1;
126 void DefinePointerSlot(int index) { indexes_->Add(index); }
127 void DefinePointerRegister(Register reg) { registers_->Add(reg.code()); }
130 Safepoint(ZoneList<int>* indexes, ZoneList<int>* registers) :
131 indexes_(indexes), registers_(registers) { }
132 ZoneList<int>* indexes_;
133 ZoneList<int>* registers_;
135 friend class SafepointTableBuilder;
139 class SafepointTableBuilder BASE_EMBEDDED {
141 SafepointTableBuilder()
142 : deoptimization_info_(32),
147 // Get the offset of the emitted safepoint table in the code.
148 unsigned GetCodeOffset() const;
150 // Define a new safepoint for the current position in the body.
151 Safepoint DefineSafepoint(
152 Assembler* assembler,
153 int deoptimization_index = Safepoint::kNoDeoptimizationIndex);
155 // Define a new safepoint with registers on the stack for the
156 // current position in the body and take the number of arguments on
157 // top of the registers into account.
158 Safepoint DefineSafepointWithRegisters(
159 Assembler* assembler,
161 int deoptimization_index = Safepoint::kNoDeoptimizationIndex);
163 // Update the last safepoint with the size of the code generated for the gap
165 void SetPcAfterGap(int pc) {
166 ASSERT(!deoptimization_info_.is_empty());
167 int index = deoptimization_info_.length() - 1;
168 deoptimization_info_[index].pc_after_gap = pc;
171 // Emit the safepoint table after the body. The number of bits per
172 // entry must be enough to hold all the pointer indexes.
173 void Emit(Assembler* assembler, int bits_per_entry);
176 struct DeoptimizationInfo {
178 unsigned deoptimization_index;
179 unsigned pc_after_gap;
182 uint32_t EncodeDeoptimizationIndexAndGap(DeoptimizationInfo info);
184 ZoneList<DeoptimizationInfo> deoptimization_info_;
185 ZoneList<ZoneList<int>*> indexes_;
186 ZoneList<ZoneList<int>*> registers_;
191 DISALLOW_COPY_AND_ASSIGN(SafepointTableBuilder);
194 } } // namespace v8::internal
196 #endif // V8_SAFEPOINT_TABLE_H_