2011-02-02 13:55:29 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
2010-12-07 11:31:57 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
#include "v8.h"
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "safepoint-table.h"
|
2011-01-12 14:14:14 +00:00
|
|
|
|
2011-02-02 13:55:29 +00:00
|
|
|
#include "deoptimizer.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
#include "disasm.h"
|
2011-01-12 14:14:14 +00:00
|
|
|
#include "macro-assembler.h"
|
2011-03-18 20:35:07 +00:00
|
|
|
#include "zone-inl.h"
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2011-01-12 14:14:14 +00:00
|
|
|
|
|
|
|
bool SafepointEntry::HasRegisters() const {
|
|
|
|
ASSERT(is_valid());
|
|
|
|
ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte));
|
|
|
|
const int num_reg_bytes = kNumSafepointRegisters >> kBitsPerByteLog2;
|
|
|
|
for (int i = 0; i < num_reg_bytes; i++) {
|
|
|
|
if (bits_[i] != SafepointTable::kNoRegisters) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SafepointEntry::HasRegisterAt(int reg_index) const {
|
|
|
|
ASSERT(is_valid());
|
|
|
|
ASSERT(reg_index >= 0 && reg_index < kNumSafepointRegisters);
|
|
|
|
int byte_index = reg_index >> kBitsPerByteLog2;
|
|
|
|
int bit_index = reg_index & (kBitsPerByte - 1);
|
|
|
|
return (bits_[byte_index] & (1 << bit_index)) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
SafepointTable::SafepointTable(Code* code) {
|
|
|
|
ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION);
|
|
|
|
code_ = code;
|
2011-02-03 10:07:22 +00:00
|
|
|
Address header = code->instruction_start() + code->safepoint_table_offset();
|
2010-12-07 11:31:57 +00:00
|
|
|
length_ = Memory::uint32_at(header + kLengthOffset);
|
|
|
|
entry_size_ = Memory::uint32_at(header + kEntrySizeOffset);
|
|
|
|
pc_and_deoptimization_indexes_ = header + kHeaderSize;
|
|
|
|
entries_ = pc_and_deoptimization_indexes_ +
|
|
|
|
(length_ * kPcAndDeoptimizationIndexSize);
|
|
|
|
ASSERT(entry_size_ > 0);
|
2011-09-12 10:50:50 +00:00
|
|
|
STATIC_ASSERT(SafepointEntry::DeoptimizationIndexField::kMax ==
|
|
|
|
Safepoint::kNoDeoptimizationIndex);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-12 14:14:14 +00:00
|
|
|
SafepointEntry SafepointTable::FindEntry(Address pc) const {
|
|
|
|
unsigned pc_offset = static_cast<unsigned>(pc - code_->instruction_start());
|
|
|
|
for (unsigned i = 0; i < length(); i++) {
|
|
|
|
// TODO(kasperl): Replace the linear search with binary search.
|
|
|
|
if (GetPcOffset(i) == pc_offset) return GetEntry(i);
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
2011-01-12 14:14:14 +00:00
|
|
|
return SafepointEntry();
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SafepointTable::PrintEntry(unsigned index) const {
|
|
|
|
disasm::NameConverter converter;
|
2011-01-12 14:14:14 +00:00
|
|
|
SafepointEntry entry = GetEntry(index);
|
|
|
|
uint8_t* bits = entry.bits();
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
// Print the stack slot bits.
|
|
|
|
if (entry_size_ > 0) {
|
|
|
|
ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte));
|
|
|
|
const int first = kNumSafepointRegisters >> kBitsPerByteLog2;
|
|
|
|
int last = entry_size_ - 1;
|
2011-01-12 14:14:14 +00:00
|
|
|
for (int i = first; i < last; i++) PrintBits(bits[i], kBitsPerByte);
|
2010-12-07 11:31:57 +00:00
|
|
|
int last_bits = code_->stack_slots() - ((last - first) * kBitsPerByte);
|
2011-01-12 14:14:14 +00:00
|
|
|
PrintBits(bits[last], last_bits);
|
2010-12-07 11:31:57 +00:00
|
|
|
|
|
|
|
// Print the registers (if any).
|
2011-01-12 14:14:14 +00:00
|
|
|
if (!entry.HasRegisters()) return;
|
2010-12-07 11:31:57 +00:00
|
|
|
for (int j = 0; j < kNumSafepointRegisters; j++) {
|
2011-01-12 14:14:14 +00:00
|
|
|
if (entry.HasRegisterAt(j)) {
|
2010-12-07 11:31:57 +00:00
|
|
|
PrintF(" | %s", converter.NameOfCPURegister(j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SafepointTable::PrintBits(uint8_t byte, int digits) {
|
|
|
|
ASSERT(digits >= 0 && digits <= kBitsPerByte);
|
|
|
|
for (int i = 0; i < digits; i++) {
|
|
|
|
PrintF("%c", ((byte & (1 << i)) == 0) ? '0' : '1');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-12 14:14:14 +00:00
|
|
|
void Safepoint::DefinePointerRegister(Register reg) {
|
|
|
|
registers_->Add(reg.code());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-26 20:48:48 +00:00
|
|
|
Safepoint SafepointTableBuilder::DefineSafepoint(
|
2011-11-16 08:44:30 +00:00
|
|
|
Assembler* assembler,
|
|
|
|
Safepoint::Kind kind,
|
|
|
|
int arguments,
|
|
|
|
Safepoint::DeoptMode deopt_mode) {
|
2011-01-12 14:14:14 +00:00
|
|
|
ASSERT(arguments >= 0);
|
2011-11-16 08:44:30 +00:00
|
|
|
DeoptimizationInfo info;
|
|
|
|
info.pc = assembler->pc_offset();
|
|
|
|
info.arguments = arguments;
|
|
|
|
info.has_doubles = (kind & Safepoint::kWithDoubles);
|
|
|
|
deoptimization_info_.Add(info);
|
|
|
|
deopt_index_list_.Add(Safepoint::kNoDeoptimizationIndex);
|
|
|
|
if (deopt_mode == Safepoint::kNoLazyDeopt) {
|
|
|
|
last_lazy_safepoint_ = deopt_index_list_.length();
|
|
|
|
}
|
2010-12-07 11:31:57 +00:00
|
|
|
indexes_.Add(new ZoneList<int>(8));
|
2011-01-26 20:48:48 +00:00
|
|
|
registers_.Add((kind & Safepoint::kWithRegisters)
|
|
|
|
? new ZoneList<int>(4)
|
|
|
|
: NULL);
|
2010-12-07 11:31:57 +00:00
|
|
|
return Safepoint(indexes_.last(), registers_.last());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-16 08:44:30 +00:00
|
|
|
void SafepointTableBuilder::RecordLazyDeoptimizationIndex(int index) {
|
|
|
|
while (last_lazy_safepoint_ < deopt_index_list_.length()) {
|
|
|
|
deopt_index_list_[last_lazy_safepoint_++] = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
unsigned SafepointTableBuilder::GetCodeOffset() const {
|
|
|
|
ASSERT(emitted_);
|
|
|
|
return offset_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void SafepointTableBuilder::Emit(Assembler* assembler, int bits_per_entry) {
|
2011-02-02 13:55:29 +00:00
|
|
|
// For lazy deoptimization we need space to patch a call after every call.
|
|
|
|
// Ensure there is always space for such patching, even if the code ends
|
|
|
|
// in a call.
|
|
|
|
int target_offset = assembler->pc_offset() + Deoptimizer::patch_size();
|
|
|
|
while (assembler->pc_offset() < target_offset) {
|
|
|
|
assembler->nop();
|
|
|
|
}
|
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
// Make sure the safepoint table is properly aligned. Pad with nops.
|
|
|
|
assembler->Align(kIntSize);
|
|
|
|
assembler->RecordComment(";;; Safepoint table.");
|
|
|
|
offset_ = assembler->pc_offset();
|
|
|
|
|
|
|
|
// Take the register bits into account.
|
|
|
|
bits_per_entry += kNumSafepointRegisters;
|
|
|
|
|
|
|
|
// Compute the number of bytes per safepoint entry.
|
|
|
|
int bytes_per_entry =
|
|
|
|
RoundUp(bits_per_entry, kBitsPerByte) >> kBitsPerByteLog2;
|
|
|
|
|
|
|
|
// Emit the table header.
|
|
|
|
int length = deoptimization_info_.length();
|
|
|
|
assembler->dd(length);
|
|
|
|
assembler->dd(bytes_per_entry);
|
|
|
|
|
2011-11-16 08:44:30 +00:00
|
|
|
// Emit sorted table of pc offsets together with deoptimization indexes.
|
2010-12-07 11:31:57 +00:00
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
assembler->dd(deoptimization_info_[i].pc);
|
2011-11-16 08:44:30 +00:00
|
|
|
assembler->dd(EncodeExceptPC(deoptimization_info_[i],
|
|
|
|
deopt_index_list_[i]));
|
2010-12-07 11:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Emit table of bitmaps.
|
|
|
|
ZoneList<uint8_t> bits(bytes_per_entry);
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
ZoneList<int>* indexes = indexes_[i];
|
|
|
|
ZoneList<int>* registers = registers_[i];
|
|
|
|
bits.Clear();
|
|
|
|
bits.AddBlock(0, bytes_per_entry);
|
|
|
|
|
|
|
|
// Run through the registers (if any).
|
|
|
|
ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte));
|
|
|
|
if (registers == NULL) {
|
|
|
|
const int num_reg_bytes = kNumSafepointRegisters >> kBitsPerByteLog2;
|
|
|
|
for (int j = 0; j < num_reg_bytes; j++) {
|
|
|
|
bits[j] = SafepointTable::kNoRegisters;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int j = 0; j < registers->length(); j++) {
|
|
|
|
int index = registers->at(j);
|
|
|
|
ASSERT(index >= 0 && index < kNumSafepointRegisters);
|
|
|
|
int byte_index = index >> kBitsPerByteLog2;
|
|
|
|
int bit_index = index & (kBitsPerByte - 1);
|
|
|
|
bits[byte_index] |= (1 << bit_index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run through the indexes and build a bitmap.
|
|
|
|
for (int j = 0; j < indexes->length(); j++) {
|
|
|
|
int index = bits_per_entry - 1 - indexes->at(j);
|
|
|
|
int byte_index = index >> kBitsPerByteLog2;
|
|
|
|
int bit_index = index & (kBitsPerByte - 1);
|
|
|
|
bits[byte_index] |= (1U << bit_index);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Emit the bitmap for the current entry.
|
|
|
|
for (int k = 0; k < bytes_per_entry; k++) {
|
|
|
|
assembler->db(bits[k]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
emitted_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-16 08:44:30 +00:00
|
|
|
uint32_t SafepointTableBuilder::EncodeExceptPC(const DeoptimizationInfo& info,
|
|
|
|
unsigned index) {
|
2011-01-12 14:14:14 +00:00
|
|
|
uint32_t encoding = SafepointEntry::DeoptimizationIndexField::encode(index);
|
|
|
|
encoding |= SafepointEntry::ArgumentsField::encode(info.arguments);
|
2011-01-14 08:49:52 +00:00
|
|
|
encoding |= SafepointEntry::SaveDoublesField::encode(info.has_doubles);
|
2010-12-07 11:31:57 +00:00
|
|
|
return encoding;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-04 14:09:03 +00:00
|
|
|
|
2010-12-07 11:31:57 +00:00
|
|
|
} } // namespace v8::internal
|