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:
80 public BitField<int, kGapCodeSizeBits, kDeoptIndexBits> {};
82 static bool HasRegisters(uint8_t* entry);
83 static bool HasRegisterAt(uint8_t* entry, int reg_index);
85 void PrintEntry(unsigned index) const;
88 static const uint8_t kNoRegisters = 0xFF;
90 static const int kLengthOffset = 0;
91 static const int kEntrySizeOffset = kLengthOffset + kIntSize;
92 static const int kHeaderSize = kEntrySizeOffset + kIntSize;
94 static const int kPcSize = kIntSize;
95 static const int kDeoptimizationIndexSize = kIntSize;
96 static const int kPcAndDeoptimizationIndexSize =
97 kPcSize + kDeoptimizationIndexSize;
99 Address GetPcOffsetLocation(unsigned index) const {
100 return pc_and_deoptimization_indexes_ +
101 (index * kPcAndDeoptimizationIndexSize);
104 Address GetDeoptimizationLocation(unsigned index) const {
105 return GetPcOffsetLocation(index) + kPcSize;
108 static void PrintBits(uint8_t byte, int digits);
110 AssertNoAllocation no_allocation_;
113 unsigned entry_size_;
115 Address pc_and_deoptimization_indexes_;
118 friend class SafepointTableBuilder;
122 class Safepoint BASE_EMBEDDED {
124 static const int kNoDeoptimizationIndex =
125 (1 << (SafepointTable::kDeoptIndexBits)) - 1;
127 void DefinePointerSlot(int index) { indexes_->Add(index); }
128 void DefinePointerRegister(Register reg) { registers_->Add(reg.code()); }
131 Safepoint(ZoneList<int>* indexes, ZoneList<int>* registers) :
132 indexes_(indexes), registers_(registers) { }
133 ZoneList<int>* indexes_;
134 ZoneList<int>* registers_;
136 friend class SafepointTableBuilder;
140 class SafepointTableBuilder BASE_EMBEDDED {
142 SafepointTableBuilder()
143 : deoptimization_info_(32),
148 // Get the offset of the emitted safepoint table in the code.
149 unsigned GetCodeOffset() const;
151 // Define a new safepoint for the current position in the body.
152 Safepoint DefineSafepoint(
153 Assembler* assembler,
154 int deoptimization_index = Safepoint::kNoDeoptimizationIndex);
156 // Define a new safepoint with registers on the stack for the
157 // current position in the body and take the number of arguments on
158 // top of the registers into account.
159 Safepoint DefineSafepointWithRegisters(
160 Assembler* assembler,
162 int deoptimization_index = Safepoint::kNoDeoptimizationIndex);
164 // Update the last safepoint with the size of the code generated for the gap
166 void SetPcAfterGap(int pc) {
167 ASSERT(!deoptimization_info_.is_empty());
168 int index = deoptimization_info_.length() - 1;
169 deoptimization_info_[index].pc_after_gap = pc;
172 // Emit the safepoint table after the body. The number of bits per
173 // entry must be enough to hold all the pointer indexes.
174 void Emit(Assembler* assembler, int bits_per_entry);
177 struct DeoptimizationInfo {
179 unsigned deoptimization_index;
180 unsigned pc_after_gap;
183 uint32_t EncodeDeoptimizationIndexAndGap(DeoptimizationInfo info);
185 ZoneList<DeoptimizationInfo> deoptimization_info_;
186 ZoneList<ZoneList<int>*> indexes_;
187 ZoneList<ZoneList<int>*> registers_;
192 DISALLOW_COPY_AND_ASSIGN(SafepointTableBuilder);
195 } } // namespace v8::internal
197 #endif // V8_SAFEPOINT_TABLE_H_