blob: 4aac08d54167b02d1bfe8368e7c3213e67b56d79 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright (c) 1994-2006 Sun Microsystems Inc.
Steve Blocka7e24c12009-10-30 11:49:00 +00002// All Rights Reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// - Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10//
11// - Redistribution in binary form must reproduce the above copyright
12// notice, this list of conditions and the following disclaimer in the
13// documentation and/or other materials provided with the distribution.
14//
15// - Neither the name of Sun Microsystems or the names of contributors may
16// be used to endorse or promote products derived from this software without
17// specific prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31// The original source code covered by the above license above has been
32// modified significantly by Google Inc.
Ben Murdoch3ef787d2012-04-12 10:51:47 +010033// Copyright 2012 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +000034
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035#include "src/assembler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037#include <cmath>
38#include "src/api.h"
39#include "src/base/cpu.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040040#include "src/base/functional.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041#include "src/base/lazy-instance.h"
42#include "src/base/platform/platform.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043#include "src/base/utils/random-number-generator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044#include "src/builtins.h"
45#include "src/codegen.h"
46#include "src/counters.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047#include "src/debug/debug.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048#include "src/deoptimizer.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000049#include "src/disassembler.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000050#include "src/execution.h"
51#include "src/ic/ic.h"
52#include "src/ic/stub-cache.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053#include "src/ostreams.h"
54#include "src/parsing/token.h"
55#include "src/profiler/cpu-profiler.h"
56#include "src/regexp/jsregexp.h"
57#include "src/regexp/regexp-macro-assembler.h"
58#include "src/regexp/regexp-stack.h"
59#include "src/register-configuration.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040060#include "src/runtime/runtime.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000061#include "src/simulator.h" // For flushing instruction cache.
62#include "src/snapshot/serialize.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010063
64#if V8_TARGET_ARCH_IA32
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065#include "src/ia32/assembler-ia32-inl.h" // NOLINT
Ben Murdoch3ef787d2012-04-12 10:51:47 +010066#elif V8_TARGET_ARCH_X64
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067#include "src/x64/assembler-x64-inl.h" // NOLINT
68#elif V8_TARGET_ARCH_ARM64
69#include "src/arm64/assembler-arm64-inl.h" // NOLINT
Ben Murdoch3ef787d2012-04-12 10:51:47 +010070#elif V8_TARGET_ARCH_ARM
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071#include "src/arm/assembler-arm-inl.h" // NOLINT
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000072#elif V8_TARGET_ARCH_PPC
73#include "src/ppc/assembler-ppc-inl.h" // NOLINT
Ben Murdoch3ef787d2012-04-12 10:51:47 +010074#elif V8_TARGET_ARCH_MIPS
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075#include "src/mips/assembler-mips-inl.h" // NOLINT
76#elif V8_TARGET_ARCH_MIPS64
77#include "src/mips64/assembler-mips64-inl.h" // NOLINT
78#elif V8_TARGET_ARCH_X87
79#include "src/x87/assembler-x87-inl.h" // NOLINT
Ben Murdoch3ef787d2012-04-12 10:51:47 +010080#else
81#error "Unknown architecture."
82#endif
83
Steve Blocka7e24c12009-10-30 11:49:00 +000084// Include native regexp-macro-assembler.
Steve Block6ded16b2010-05-10 14:33:55 +010085#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +000086#if V8_TARGET_ARCH_IA32
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087#include "src/regexp/ia32/regexp-macro-assembler-ia32.h" // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +000088#elif V8_TARGET_ARCH_X64
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000089#include "src/regexp/x64/regexp-macro-assembler-x64.h" // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090#elif V8_TARGET_ARCH_ARM64
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091#include "src/regexp/arm64/regexp-macro-assembler-arm64.h" // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +000092#elif V8_TARGET_ARCH_ARM
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000093#include "src/regexp/arm/regexp-macro-assembler-arm.h" // NOLINT
94#elif V8_TARGET_ARCH_PPC
95#include "src/regexp/ppc/regexp-macro-assembler-ppc.h" // NOLINT
Steve Block44f0eee2011-05-26 01:26:41 +010096#elif V8_TARGET_ARCH_MIPS
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000097#include "src/regexp/mips/regexp-macro-assembler-mips.h" // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +000098#elif V8_TARGET_ARCH_MIPS64
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099#include "src/regexp/mips64/regexp-macro-assembler-mips64.h" // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000100#elif V8_TARGET_ARCH_X87
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000101#include "src/regexp/x87/regexp-macro-assembler-x87.h" // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +0000102#else // Unknown architecture.
103#error "Unknown architecture."
104#endif // Target architecture.
Steve Block6ded16b2010-05-10 14:33:55 +0100105#endif // V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +0000106
107namespace v8 {
108namespace internal {
109
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100110// -----------------------------------------------------------------------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111// Common register code.
112
113const char* Register::ToString() {
114 // This is the mapping of allocation indices to registers.
115 DCHECK(reg_code >= 0 && reg_code < kNumRegisters);
116 return RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT)
117 ->GetGeneralRegisterName(reg_code);
118}
119
120
121bool Register::IsAllocatable() const {
122 return ((1 << reg_code) &
123 RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT)
124 ->allocatable_general_codes_mask()) != 0;
125}
126
127
128const char* DoubleRegister::ToString() {
129 // This is the mapping of allocation indices to registers.
130 DCHECK(reg_code >= 0 && reg_code < kMaxNumRegisters);
131 return RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT)
132 ->GetDoubleRegisterName(reg_code);
133}
134
135
136bool DoubleRegister::IsAllocatable() const {
137 return ((1 << reg_code) &
138 RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT)
139 ->allocatable_double_codes_mask()) != 0;
140}
141
142
143// -----------------------------------------------------------------------------
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100144// Common double constants.
Steve Blocka7e24c12009-10-30 11:49:00 +0000145
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100146struct DoubleConstant BASE_EMBEDDED {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000147double min_int;
148double one_half;
149double minus_one_half;
150double negative_infinity;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000151double the_hole_nan;
152double uint32_bias;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100153};
154
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155static DoubleConstant double_constants;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100156
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000157const char* const RelocInfo::kFillerCommentString = "DEOPTIMIZATION PADDING";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100158
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159static bool math_exp_data_initialized = false;
160static base::Mutex* math_exp_data_mutex = NULL;
161static double* math_exp_constants_array = NULL;
162static double* math_exp_log_table_array = NULL;
163
Steve Blocka7e24c12009-10-30 11:49:00 +0000164// -----------------------------------------------------------------------------
Steve Block053d10c2011-06-13 19:13:29 +0100165// Implementation of AssemblerBase
166
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167AssemblerBase::AssemblerBase(Isolate* isolate, void* buffer, int buffer_size)
Steve Block053d10c2011-06-13 19:13:29 +0100168 : isolate_(isolate),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 jit_cookie_(0),
170 enabled_cpu_features_(0),
171 emit_debug_code_(FLAG_debug_code),
172 predictable_code_size_(false),
173 // We may use the assembler without an isolate.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400174 serializer_enabled_(isolate && isolate->serializer_enabled()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000175 constant_pool_available_(false) {
176 DCHECK_NOT_NULL(isolate);
177 if (FLAG_mask_constants_with_cookie) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178 jit_cookie_ = isolate->random_number_generator()->NextInt();
Steve Block053d10c2011-06-13 19:13:29 +0100179 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180 own_buffer_ = buffer == NULL;
181 if (buffer_size == 0) buffer_size = kMinimalBufferSize;
182 DCHECK(buffer_size > 0);
183 if (own_buffer_) buffer = NewArray<byte>(buffer_size);
184 buffer_ = static_cast<byte*>(buffer);
185 buffer_size_ = buffer_size;
186
187 pc_ = buffer_;
Steve Block053d10c2011-06-13 19:13:29 +0100188}
189
190
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000191AssemblerBase::~AssemblerBase() {
192 if (own_buffer_) DeleteArray(buffer_);
193}
194
195
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000196void AssemblerBase::FlushICache(Isolate* isolate, void* start, size_t size) {
197 if (size == 0) return;
198 if (CpuFeatures::IsSupported(COHERENT_CACHE)) return;
199
200#if defined(USE_SIMULATOR)
201 Simulator::FlushICache(isolate->simulator_i_cache(), start, size);
202#else
203 CpuFeatures::FlushICache(start, size);
204#endif // USE_SIMULATOR
205}
206
207
208void AssemblerBase::Print() {
209 OFStream os(stdout);
210 v8::internal::Disassembler::Decode(isolate(), &os, buffer_, pc_, nullptr);
211}
212
213
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214// -----------------------------------------------------------------------------
215// Implementation of PredictableCodeSizeScope
216
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000217PredictableCodeSizeScope::PredictableCodeSizeScope(AssemblerBase* assembler)
218 : PredictableCodeSizeScope(assembler, -1) {}
219
220
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221PredictableCodeSizeScope::PredictableCodeSizeScope(AssemblerBase* assembler,
222 int expected_size)
223 : assembler_(assembler),
224 expected_size_(expected_size),
225 start_offset_(assembler->pc_offset()),
226 old_value_(assembler->predictable_code_size()) {
227 assembler_->set_predictable_code_size(true);
228}
229
230
231PredictableCodeSizeScope::~PredictableCodeSizeScope() {
232 // TODO(svenpanne) Remove the 'if' when everything works.
233 if (expected_size_ >= 0) {
234 CHECK_EQ(expected_size_, assembler_->pc_offset() - start_offset_);
235 }
236 assembler_->set_predictable_code_size(old_value_);
237}
238
239
240// -----------------------------------------------------------------------------
241// Implementation of CpuFeatureScope
242
243#ifdef DEBUG
244CpuFeatureScope::CpuFeatureScope(AssemblerBase* assembler, CpuFeature f)
245 : assembler_(assembler) {
246 DCHECK(CpuFeatures::IsSupported(f));
247 old_enabled_ = assembler_->enabled_cpu_features();
248 uint64_t mask = static_cast<uint64_t>(1) << f;
249 // TODO(svenpanne) This special case below doesn't belong here!
250#if V8_TARGET_ARCH_ARM
251 // ARMv7 is implied by VFP3.
252 if (f == VFP3) {
253 mask |= static_cast<uint64_t>(1) << ARMv7;
254 }
255#endif
256 assembler_->set_enabled_cpu_features(old_enabled_ | mask);
257}
258
259
260CpuFeatureScope::~CpuFeatureScope() {
261 assembler_->set_enabled_cpu_features(old_enabled_);
262}
263#endif
264
265
266bool CpuFeatures::initialized_ = false;
267unsigned CpuFeatures::supported_ = 0;
268unsigned CpuFeatures::cache_line_size_ = 0;
269
270
Steve Block053d10c2011-06-13 19:13:29 +0100271// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000272// Implementation of Label
273
274int Label::pos() const {
275 if (pos_ < 0) return -pos_ - 1;
276 if (pos_ > 0) return pos_ - 1;
277 UNREACHABLE();
278 return 0;
279}
280
281
282// -----------------------------------------------------------------------------
283// Implementation of RelocInfoWriter and RelocIterator
284//
Ben Murdoch257744e2011-11-30 15:57:28 +0000285// Relocation information is written backwards in memory, from high addresses
286// towards low addresses, byte by byte. Therefore, in the encodings listed
287// below, the first byte listed it at the highest address, and successive
288// bytes in the record are at progressively lower addresses.
289//
Steve Blocka7e24c12009-10-30 11:49:00 +0000290// Encoding
291//
292// The most common modes are given single-byte encodings. Also, it is
293// easy to identify the type of reloc info and skip unwanted modes in
294// an iteration.
295//
Ben Murdoch257744e2011-11-30 15:57:28 +0000296// The encoding relies on the fact that there are fewer than 14
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000297// different relocation modes using standard non-compact encoding.
Steve Blocka7e24c12009-10-30 11:49:00 +0000298//
Ben Murdoch257744e2011-11-30 15:57:28 +0000299// The first byte of a relocation record has a tag in its low 2 bits:
300// Here are the record schemes, depending on the low tag and optional higher
301// tags.
Steve Blocka7e24c12009-10-30 11:49:00 +0000302//
Ben Murdoch257744e2011-11-30 15:57:28 +0000303// Low tag:
304// 00: embedded_object: [6-bit pc delta] 00
Steve Blocka7e24c12009-10-30 11:49:00 +0000305//
Ben Murdoch257744e2011-11-30 15:57:28 +0000306// 01: code_target: [6-bit pc delta] 01
Steve Blocka7e24c12009-10-30 11:49:00 +0000307//
Ben Murdoch257744e2011-11-30 15:57:28 +0000308// 10: short_data_record: [6-bit pc delta] 10 followed by
309// [6-bit data delta] [2-bit data type tag]
Steve Blocka7e24c12009-10-30 11:49:00 +0000310//
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000311// 11: long_record [6 bit reloc mode] 11
312// followed by pc delta
313// followed by optional data depending on type.
Steve Blocka7e24c12009-10-30 11:49:00 +0000314//
Ben Murdoch257744e2011-11-30 15:57:28 +0000315// 2-bit data type tags, used in short_data_record and data_jump long_record:
316// code_target_with_id: 00
317// position: 01
318// statement_position: 10
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000319// deopt_reason: 11
Steve Blocka7e24c12009-10-30 11:49:00 +0000320//
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000321// If a pc delta exceeds 6 bits, it is split into a remainder that fits into
322// 6 bits and a part that does not. The latter is encoded as a long record
323// with PC_JUMP as pseudo reloc info mode. The former is encoded as part of
324// the following record in the usual way. The long pc jump record has variable
325// length:
326// pc-jump: [PC_JUMP] 11
Ben Murdoch257744e2011-11-30 15:57:28 +0000327// [7 bits data] 0
328// ...
329// [7 bits data] 1
330// (Bits 6..31 of pc delta, with leading zeroes
331// dropped, and last non-zero chunk tagged with 1.)
332
Steve Blocka7e24c12009-10-30 11:49:00 +0000333const int kTagBits = 2;
334const int kTagMask = (1 << kTagBits) - 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000335const int kLongTagBits = 6;
336const int kShortDataTypeTagBits = 2;
337const int kShortDataBits = kBitsPerByte - kShortDataTypeTagBits;
Steve Blocka7e24c12009-10-30 11:49:00 +0000338
339const int kEmbeddedObjectTag = 0;
340const int kCodeTargetTag = 1;
Ben Murdoch257744e2011-11-30 15:57:28 +0000341const int kLocatableTag = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +0000342const int kDefaultTag = 3;
343
Steve Blocka7e24c12009-10-30 11:49:00 +0000344const int kSmallPCDeltaBits = kBitsPerByte - kTagBits;
345const int kSmallPCDeltaMask = (1 << kSmallPCDeltaBits) - 1;
Steve Block44f0eee2011-05-26 01:26:41 +0100346const int RelocInfo::kMaxSmallPCDelta = kSmallPCDeltaMask;
Steve Blocka7e24c12009-10-30 11:49:00 +0000347
Steve Blocka7e24c12009-10-30 11:49:00 +0000348const int kChunkBits = 7;
349const int kChunkMask = (1 << kChunkBits) - 1;
350const int kLastChunkTagBits = 1;
351const int kLastChunkTagMask = 1;
352const int kLastChunkTag = 1;
353
Ben Murdoch257744e2011-11-30 15:57:28 +0000354const int kCodeWithIdTag = 0;
355const int kNonstatementPositionTag = 1;
356const int kStatementPositionTag = 2;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000357const int kDeoptReasonTag = 3;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000358
Steve Blocka7e24c12009-10-30 11:49:00 +0000359
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000360uint32_t RelocInfoWriter::WriteLongPCJump(uint32_t pc_delta) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000361 // Return if the pc_delta can fit in kSmallPCDeltaBits bits.
362 // Otherwise write a variable length PC jump for the bits that do
363 // not fit in the kSmallPCDeltaBits bits.
364 if (is_uintn(pc_delta, kSmallPCDeltaBits)) return pc_delta;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000365 WriteMode(RelocInfo::PC_JUMP);
Steve Blocka7e24c12009-10-30 11:49:00 +0000366 uint32_t pc_jump = pc_delta >> kSmallPCDeltaBits;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000367 DCHECK(pc_jump > 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000368 // Write kChunkBits size chunks of the pc_jump.
369 for (; pc_jump > 0; pc_jump = pc_jump >> kChunkBits) {
370 byte b = pc_jump & kChunkMask;
371 *--pos_ = b << kLastChunkTagBits;
372 }
373 // Tag the last chunk so it can be identified.
374 *pos_ = *pos_ | kLastChunkTag;
375 // Return the remaining kSmallPCDeltaBits of the pc_delta.
376 return pc_delta & kSmallPCDeltaMask;
377}
378
379
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000380void RelocInfoWriter::WriteShortTaggedPC(uint32_t pc_delta, int tag) {
381 // Write a byte of tagged pc-delta, possibly preceded by an explicit pc-jump.
382 pc_delta = WriteLongPCJump(pc_delta);
Steve Blocka7e24c12009-10-30 11:49:00 +0000383 *--pos_ = pc_delta << kTagBits | tag;
384}
385
386
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000387void RelocInfoWriter::WriteShortTaggedData(intptr_t data_delta, int tag) {
388 *--pos_ = static_cast<byte>(data_delta << kShortDataTypeTagBits | tag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000389}
390
391
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000392void RelocInfoWriter::WriteMode(RelocInfo::Mode rmode) {
393 STATIC_ASSERT(RelocInfo::NUMBER_OF_MODES <= (1 << kLongTagBits));
394 *--pos_ = static_cast<int>((rmode << kTagBits) | kDefaultTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000395}
396
397
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000398void RelocInfoWriter::WriteModeAndPC(uint32_t pc_delta, RelocInfo::Mode rmode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000399 // Write two-byte tagged pc-delta, possibly preceded by var. length pc-jump.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000400 pc_delta = WriteLongPCJump(pc_delta);
401 WriteMode(rmode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000402 *--pos_ = pc_delta;
403}
404
405
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000406void RelocInfoWriter::WriteIntData(int number) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000407 for (int i = 0; i < kIntSize; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000408 *--pos_ = static_cast<byte>(number);
Ben Murdoch257744e2011-11-30 15:57:28 +0000409 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000410 number = number >> kBitsPerByte;
Ben Murdoch257744e2011-11-30 15:57:28 +0000411 }
412}
413
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000414
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000415void RelocInfoWriter::WriteData(intptr_t data_delta) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000416 for (int i = 0; i < kIntptrSize; i++) {
Steve Blockd0582a62009-12-15 09:54:21 +0000417 *--pos_ = static_cast<byte>(data_delta);
Ben Murdoch257744e2011-11-30 15:57:28 +0000418 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
Steve Blocka7e24c12009-10-30 11:49:00 +0000419 data_delta = data_delta >> kBitsPerByte;
420 }
421}
422
423
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000424void RelocInfoWriter::WritePosition(int pc_delta, int pos_delta,
425 RelocInfo::Mode rmode) {
426 int pos_type_tag = (rmode == RelocInfo::POSITION) ? kNonstatementPositionTag
427 : kStatementPositionTag;
428 // Check if delta is small enough to fit in a tagged byte.
429 if (is_intn(pos_delta, kShortDataBits)) {
430 WriteShortTaggedPC(pc_delta, kLocatableTag);
431 WriteShortTaggedData(pos_delta, pos_type_tag);
432 } else {
433 // Otherwise, use costly encoding.
434 WriteModeAndPC(pc_delta, rmode);
435 WriteIntData(pos_delta);
436 }
437}
438
439
440void RelocInfoWriter::FlushPosition() {
441 if (!next_position_candidate_flushed_) {
442 WritePosition(next_position_candidate_pc_delta_,
443 next_position_candidate_pos_delta_, RelocInfo::POSITION);
444 next_position_candidate_pos_delta_ = 0;
445 next_position_candidate_pc_delta_ = 0;
446 next_position_candidate_flushed_ = true;
447 }
448}
449
450
Steve Blocka7e24c12009-10-30 11:49:00 +0000451void RelocInfoWriter::Write(const RelocInfo* rinfo) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000452 RelocInfo::Mode rmode = rinfo->rmode();
453 if (rmode != RelocInfo::POSITION) {
454 FlushPosition();
455 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000456#ifdef DEBUG
457 byte* begin_pos = pos_;
458#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000459 DCHECK(rinfo->rmode() < RelocInfo::NUMBER_OF_MODES);
460 DCHECK(rinfo->pc() - last_pc_ >= 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000461 // Use unsigned delta-encoding for pc.
Steve Blockd0582a62009-12-15 09:54:21 +0000462 uint32_t pc_delta = static_cast<uint32_t>(rinfo->pc() - last_pc_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000463
464 // The two most common modes are given small tags, and usually fit in a byte.
465 if (rmode == RelocInfo::EMBEDDED_OBJECT) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000466 WriteShortTaggedPC(pc_delta, kEmbeddedObjectTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000467 } else if (rmode == RelocInfo::CODE_TARGET) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000468 WriteShortTaggedPC(pc_delta, kCodeTargetTag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000469 DCHECK(begin_pos - pos_ <= RelocInfo::kMaxCallSize);
Ben Murdoch257744e2011-11-30 15:57:28 +0000470 } else if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
471 // Use signed delta-encoding for id.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000472 DCHECK_EQ(static_cast<int>(rinfo->data()), rinfo->data());
Ben Murdoch257744e2011-11-30 15:57:28 +0000473 int id_delta = static_cast<int>(rinfo->data()) - last_id_;
474 // Check if delta is small enough to fit in a tagged byte.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000475 if (is_intn(id_delta, kShortDataBits)) {
476 WriteShortTaggedPC(pc_delta, kLocatableTag);
477 WriteShortTaggedData(id_delta, kCodeWithIdTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000478 } else {
479 // Otherwise, use costly encoding.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000480 WriteModeAndPC(pc_delta, rmode);
481 WriteIntData(id_delta);
Steve Blocka7e24c12009-10-30 11:49:00 +0000482 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000483 last_id_ = static_cast<int>(rinfo->data());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000484 } else if (rmode == RelocInfo::DEOPT_REASON) {
485 DCHECK(rinfo->data() < (1 << kShortDataBits));
486 WriteShortTaggedPC(pc_delta, kLocatableTag);
487 WriteShortTaggedData(rinfo->data(), kDeoptReasonTag);
Ben Murdoch257744e2011-11-30 15:57:28 +0000488 } else if (RelocInfo::IsPosition(rmode)) {
489 // Use signed delta-encoding for position.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000490 DCHECK_EQ(static_cast<int>(rinfo->data()), rinfo->data());
Ben Murdoch257744e2011-11-30 15:57:28 +0000491 int pos_delta = static_cast<int>(rinfo->data()) - last_position_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000492 if (rmode == RelocInfo::STATEMENT_POSITION) {
493 WritePosition(pc_delta, pos_delta, rmode);
Ben Murdoch257744e2011-11-30 15:57:28 +0000494 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000495 DCHECK_EQ(rmode, RelocInfo::POSITION);
496 if (pc_delta != 0 || last_mode_ != RelocInfo::POSITION) {
497 FlushPosition();
498 next_position_candidate_pc_delta_ = pc_delta;
499 next_position_candidate_pos_delta_ = pos_delta;
500 } else {
501 next_position_candidate_pos_delta_ += pos_delta;
502 }
503 next_position_candidate_flushed_ = false;
Ben Murdoch257744e2011-11-30 15:57:28 +0000504 }
505 last_position_ = static_cast<int>(rinfo->data());
Steve Blocka7e24c12009-10-30 11:49:00 +0000506 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000507 WriteModeAndPC(pc_delta, rmode);
508 if (RelocInfo::IsComment(rmode)) {
509 WriteData(rinfo->data());
510 } else if (RelocInfo::IsConstPool(rmode) ||
511 RelocInfo::IsVeneerPool(rmode)) {
512 WriteIntData(static_cast<int>(rinfo->data()));
513 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000514 }
515 last_pc_ = rinfo->pc();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000516 last_mode_ = rmode;
Steve Blocka7e24c12009-10-30 11:49:00 +0000517#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000518 DCHECK(begin_pos - pos_ <= kMaxSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000519#endif
520}
521
522
523inline int RelocIterator::AdvanceGetTag() {
524 return *--pos_ & kTagMask;
525}
526
527
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000528inline RelocInfo::Mode RelocIterator::GetMode() {
529 return static_cast<RelocInfo::Mode>((*pos_ >> kTagBits) &
530 ((1 << kLongTagBits) - 1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000531}
532
533
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000534inline void RelocIterator::ReadShortTaggedPC() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000535 rinfo_.pc_ += *pos_ >> kTagBits;
536}
537
538
539inline void RelocIterator::AdvanceReadPC() {
540 rinfo_.pc_ += *--pos_;
541}
542
543
Ben Murdoch257744e2011-11-30 15:57:28 +0000544void RelocIterator::AdvanceReadId() {
545 int x = 0;
546 for (int i = 0; i < kIntSize; i++) {
547 x |= static_cast<int>(*--pos_) << i * kBitsPerByte;
548 }
549 last_id_ += x;
550 rinfo_.data_ = last_id_;
551}
552
553
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000554void RelocIterator::AdvanceReadInt() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000555 int x = 0;
556 for (int i = 0; i < kIntSize; i++) {
557 x |= static_cast<int>(*--pos_) << i * kBitsPerByte;
558 }
559 rinfo_.data_ = x;
560}
561
562
Ben Murdoch257744e2011-11-30 15:57:28 +0000563void RelocIterator::AdvanceReadPosition() {
564 int x = 0;
565 for (int i = 0; i < kIntSize; i++) {
566 x |= static_cast<int>(*--pos_) << i * kBitsPerByte;
567 }
568 last_position_ += x;
569 rinfo_.data_ = last_position_;
570}
571
572
Steve Blocka7e24c12009-10-30 11:49:00 +0000573void RelocIterator::AdvanceReadData() {
574 intptr_t x = 0;
575 for (int i = 0; i < kIntptrSize; i++) {
576 x |= static_cast<intptr_t>(*--pos_) << i * kBitsPerByte;
577 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000578 rinfo_.data_ = x;
Steve Blocka7e24c12009-10-30 11:49:00 +0000579}
580
581
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000582void RelocIterator::AdvanceReadLongPCJump() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000583 // Read the 32-kSmallPCDeltaBits most significant bits of the
584 // pc jump in kChunkBits bit chunks and shift them into place.
585 // Stop when the last chunk is encountered.
586 uint32_t pc_jump = 0;
587 for (int i = 0; i < kIntSize; i++) {
588 byte pc_jump_part = *--pos_;
589 pc_jump |= (pc_jump_part >> kLastChunkTagBits) << i * kChunkBits;
590 if ((pc_jump_part & kLastChunkTagMask) == 1) break;
591 }
592 // The least significant kSmallPCDeltaBits bits will be added
593 // later.
594 rinfo_.pc_ += pc_jump << kSmallPCDeltaBits;
595}
596
597
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000598inline int RelocIterator::GetShortDataTypeTag() {
599 return *pos_ & ((1 << kShortDataTypeTagBits) - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000600}
601
602
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000603inline void RelocIterator::ReadShortTaggedId() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000604 int8_t signed_b = *pos_;
605 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000606 last_id_ += signed_b >> kShortDataTypeTagBits;
Ben Murdoch257744e2011-11-30 15:57:28 +0000607 rinfo_.data_ = last_id_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000608}
609
610
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000611inline void RelocIterator::ReadShortTaggedPosition() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000612 int8_t signed_b = *pos_;
613 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000614 last_position_ += signed_b >> kShortDataTypeTagBits;
Ben Murdoch257744e2011-11-30 15:57:28 +0000615 rinfo_.data_ = last_position_;
616}
617
618
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000619inline void RelocIterator::ReadShortTaggedData() {
620 uint8_t unsigned_b = *pos_;
621 rinfo_.data_ = unsigned_b >> kTagBits;
622}
623
624
Ben Murdoch257744e2011-11-30 15:57:28 +0000625static inline RelocInfo::Mode GetPositionModeFromTag(int tag) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000626 DCHECK(tag == kNonstatementPositionTag ||
Ben Murdoch257744e2011-11-30 15:57:28 +0000627 tag == kStatementPositionTag);
628 return (tag == kNonstatementPositionTag) ?
629 RelocInfo::POSITION :
630 RelocInfo::STATEMENT_POSITION;
Steve Blocka7e24c12009-10-30 11:49:00 +0000631}
632
633
634void RelocIterator::next() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000635 DCHECK(!done());
Steve Blocka7e24c12009-10-30 11:49:00 +0000636 // Basically, do the opposite of RelocInfoWriter::Write.
637 // Reading of data is as far as possible avoided for unwanted modes,
638 // but we must always update the pc.
639 //
640 // We exit this loop by returning when we find a mode we want.
641 while (pos_ > end_) {
642 int tag = AdvanceGetTag();
643 if (tag == kEmbeddedObjectTag) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000644 ReadShortTaggedPC();
Steve Blocka7e24c12009-10-30 11:49:00 +0000645 if (SetMode(RelocInfo::EMBEDDED_OBJECT)) return;
646 } else if (tag == kCodeTargetTag) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000647 ReadShortTaggedPC();
Steve Blocka7e24c12009-10-30 11:49:00 +0000648 if (SetMode(RelocInfo::CODE_TARGET)) return;
Ben Murdoch257744e2011-11-30 15:57:28 +0000649 } else if (tag == kLocatableTag) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000650 ReadShortTaggedPC();
Steve Blocka7e24c12009-10-30 11:49:00 +0000651 Advance();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000652 int data_type_tag = GetShortDataTypeTag();
653 if (data_type_tag == kCodeWithIdTag) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000654 if (SetMode(RelocInfo::CODE_TARGET_WITH_ID)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000655 ReadShortTaggedId();
656 return;
657 }
658 } else if (data_type_tag == kDeoptReasonTag) {
659 if (SetMode(RelocInfo::DEOPT_REASON)) {
660 ReadShortTaggedData();
Ben Murdoch257744e2011-11-30 15:57:28 +0000661 return;
662 }
663 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000664 DCHECK(data_type_tag == kNonstatementPositionTag ||
665 data_type_tag == kStatementPositionTag);
Ben Murdoch257744e2011-11-30 15:57:28 +0000666 if (mode_mask_ & RelocInfo::kPositionMask) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000667 // Always update the position if we are interested in either
668 // statement positions or non-statement positions.
669 ReadShortTaggedPosition();
670 if (SetMode(GetPositionModeFromTag(data_type_tag))) return;
Ben Murdoch257744e2011-11-30 15:57:28 +0000671 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000672 }
673 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000674 DCHECK(tag == kDefaultTag);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000675 RelocInfo::Mode rmode = GetMode();
676 if (rmode == RelocInfo::PC_JUMP) {
677 AdvanceReadLongPCJump();
678 } else {
679 AdvanceReadPC();
680 if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
681 if (SetMode(rmode)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000682 AdvanceReadId();
683 return;
684 }
685 Advance(kIntSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000686 } else if (RelocInfo::IsComment(rmode)) {
687 if (SetMode(rmode)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000688 AdvanceReadData();
689 return;
690 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000691 Advance(kIntptrSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000692 } else if (RelocInfo::IsPosition(rmode)) {
693 if (mode_mask_ & RelocInfo::kPositionMask) {
694 // Always update the position if we are interested in either
695 // statement positions or non-statement positions.
696 AdvanceReadPosition();
697 if (SetMode(rmode)) return;
698 } else {
699 Advance(kIntSize);
700 }
701 } else if (RelocInfo::IsConstPool(rmode) ||
702 RelocInfo::IsVeneerPool(rmode)) {
703 if (SetMode(rmode)) {
704 AdvanceReadInt();
705 return;
706 }
707 Advance(kIntSize);
708 } else if (SetMode(static_cast<RelocInfo::Mode>(rmode))) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000709 return;
710 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000711 }
712 }
713 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000714 if (code_age_sequence_ != NULL) {
715 byte* old_code_age_sequence = code_age_sequence_;
716 code_age_sequence_ = NULL;
717 if (SetMode(RelocInfo::CODE_AGE_SEQUENCE)) {
718 rinfo_.data_ = 0;
719 rinfo_.pc_ = old_code_age_sequence;
720 return;
721 }
722 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000723 done_ = true;
724}
725
726
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000727RelocIterator::RelocIterator(Code* code, int mode_mask)
728 : rinfo_(code->map()->GetIsolate()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100729 rinfo_.host_ = code;
Steve Blocka7e24c12009-10-30 11:49:00 +0000730 rinfo_.pc_ = code->instruction_start();
731 rinfo_.data_ = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100732 // Relocation info is read backwards.
Steve Blocka7e24c12009-10-30 11:49:00 +0000733 pos_ = code->relocation_start() + code->relocation_size();
734 end_ = code->relocation_start();
735 done_ = false;
736 mode_mask_ = mode_mask;
Ben Murdoch257744e2011-11-30 15:57:28 +0000737 last_id_ = 0;
738 last_position_ = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000739 byte* sequence = code->FindCodeAgeSequence();
740 // We get the isolate from the map, because at serialization time
741 // the code pointer has been cloned and isn't really in heap space.
742 Isolate* isolate = code->map()->GetIsolate();
743 if (sequence != NULL && !Code::IsYoungSequence(isolate, sequence)) {
744 code_age_sequence_ = sequence;
745 } else {
746 code_age_sequence_ = NULL;
747 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000748 if (mode_mask_ == 0) pos_ = end_;
749 next();
750}
751
752
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000753RelocIterator::RelocIterator(const CodeDesc& desc, int mode_mask)
754 : rinfo_(desc.origin->isolate()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000755 rinfo_.pc_ = desc.buffer;
756 rinfo_.data_ = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100757 // Relocation info is read backwards.
Steve Blocka7e24c12009-10-30 11:49:00 +0000758 pos_ = desc.buffer + desc.buffer_size;
759 end_ = pos_ - desc.reloc_size;
760 done_ = false;
761 mode_mask_ = mode_mask;
Ben Murdoch257744e2011-11-30 15:57:28 +0000762 last_id_ = 0;
763 last_position_ = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000764 code_age_sequence_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000765 if (mode_mask_ == 0) pos_ = end_;
766 next();
767}
768
769
770// -----------------------------------------------------------------------------
771// Implementation of RelocInfo
772
773
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000774#ifdef DEBUG
775bool RelocInfo::RequiresRelocation(const CodeDesc& desc) {
776 // Ensure there are no code targets or embedded objects present in the
777 // deoptimization entries, they would require relocation after code
778 // generation.
779 int mode_mask = RelocInfo::kCodeTargetMask |
780 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
781 RelocInfo::ModeMask(RelocInfo::CELL) |
782 RelocInfo::kApplyMask;
783 RelocIterator it(desc, mode_mask);
784 return !it.done();
785}
786#endif
787
788
Steve Blocka7e24c12009-10-30 11:49:00 +0000789#ifdef ENABLE_DISASSEMBLER
790const char* RelocInfo::RelocModeName(RelocInfo::Mode rmode) {
791 switch (rmode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000792 case NONE32:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000793 return "no reloc 32";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000794 case NONE64:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000795 return "no reloc 64";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000796 case EMBEDDED_OBJECT:
Steve Blocka7e24c12009-10-30 11:49:00 +0000797 return "embedded object";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000798 case DEBUGGER_STATEMENT:
799 return "debugger statement";
800 case CODE_TARGET:
Steve Blocka7e24c12009-10-30 11:49:00 +0000801 return "code target";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000802 case CODE_TARGET_WITH_ID:
Ben Murdoch257744e2011-11-30 15:57:28 +0000803 return "code target with id";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000804 case CELL:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000805 return "property cell";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000806 case RUNTIME_ENTRY:
Steve Blocka7e24c12009-10-30 11:49:00 +0000807 return "runtime entry";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000808 case COMMENT:
Steve Blocka7e24c12009-10-30 11:49:00 +0000809 return "comment";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000810 case POSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +0000811 return "position";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000812 case STATEMENT_POSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +0000813 return "statement position";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000814 case EXTERNAL_REFERENCE:
Steve Blocka7e24c12009-10-30 11:49:00 +0000815 return "external reference";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000816 case INTERNAL_REFERENCE:
Steve Blocka7e24c12009-10-30 11:49:00 +0000817 return "internal reference";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000818 case INTERNAL_REFERENCE_ENCODED:
819 return "encoded internal reference";
820 case DEOPT_REASON:
821 return "deopt reason";
822 case CONST_POOL:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000823 return "constant pool";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000824 case VENEER_POOL:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000825 return "veneer pool";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000826 case DEBUG_BREAK_SLOT_AT_POSITION:
827 return "debug break slot at position";
828 case DEBUG_BREAK_SLOT_AT_RETURN:
829 return "debug break slot at return";
830 case DEBUG_BREAK_SLOT_AT_CALL:
831 return "debug break slot at call";
832 case CODE_AGE_SEQUENCE:
833 return "code age sequence";
834 case GENERATOR_CONTINUATION:
835 return "generator continuation";
836 case NUMBER_OF_MODES:
837 case PC_JUMP:
Steve Blocka7e24c12009-10-30 11:49:00 +0000838 UNREACHABLE();
839 return "number_of_modes";
840 }
841 return "unknown relocation type";
842}
843
844
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400845void RelocInfo::Print(Isolate* isolate, std::ostream& os) { // NOLINT
846 os << static_cast<const void*>(pc_) << " " << RelocModeName(rmode_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000847 if (IsComment(rmode_)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000848 os << " (" << reinterpret_cast<char*>(data_) << ")";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000849 } else if (rmode_ == DEOPT_REASON) {
850 os << " (" << Deoptimizer::GetDeoptReason(
851 static_cast<Deoptimizer::DeoptReason>(data_)) << ")";
Steve Blocka7e24c12009-10-30 11:49:00 +0000852 } else if (rmode_ == EMBEDDED_OBJECT) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000853 os << " (" << Brief(target_object()) << ")";
Steve Blocka7e24c12009-10-30 11:49:00 +0000854 } else if (rmode_ == EXTERNAL_REFERENCE) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000855 ExternalReferenceEncoder ref_encoder(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000856 os << " ("
857 << ref_encoder.NameOfAddress(isolate, target_external_reference())
858 << ") (" << static_cast<const void*>(target_external_reference())
859 << ")";
Steve Blocka7e24c12009-10-30 11:49:00 +0000860 } else if (IsCodeTarget(rmode_)) {
861 Code* code = Code::GetCodeFromTargetAddress(target_address());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400862 os << " (" << Code::Kind2String(code->kind()) << ") ("
863 << static_cast<const void*>(target_address()) << ")";
Ben Murdoch257744e2011-11-30 15:57:28 +0000864 if (rmode_ == CODE_TARGET_WITH_ID) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000865 os << " (id=" << static_cast<int>(data_) << ")";
Ben Murdoch257744e2011-11-30 15:57:28 +0000866 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000867 } else if (IsPosition(rmode_)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000868 os << " (" << data() << ")";
869 } else if (IsRuntimeEntry(rmode_) &&
870 isolate->deoptimizer_data() != NULL) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100871 // Depotimization bailouts are stored as runtime entries.
872 int id = Deoptimizer::GetDeoptimizationId(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000873 isolate, target_address(), Deoptimizer::EAGER);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100874 if (id != Deoptimizer::kNotDeoptimizationEntry) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000875 os << " (deoptimization bailout " << id << ")";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100876 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000877 } else if (IsConstPool(rmode_)) {
878 os << " (size " << static_cast<int>(data_) << ")";
Steve Blocka7e24c12009-10-30 11:49:00 +0000879 }
880
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000881 os << "\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000882}
883#endif // ENABLE_DISASSEMBLER
884
885
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000886#ifdef VERIFY_HEAP
887void RelocInfo::Verify(Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000888 switch (rmode_) {
889 case EMBEDDED_OBJECT:
890 Object::VerifyPointer(target_object());
891 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000892 case CELL:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100893 Object::VerifyPointer(target_cell());
894 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000895 case DEBUGGER_STATEMENT:
Ben Murdoch257744e2011-11-30 15:57:28 +0000896 case CODE_TARGET_WITH_ID:
Steve Blocka7e24c12009-10-30 11:49:00 +0000897 case CODE_TARGET: {
898 // convert inline target address to code object
899 Address addr = target_address();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000900 CHECK(addr != NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000901 // Check that we can find the right code object.
902 Code* code = Code::GetCodeFromTargetAddress(addr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000903 Object* found = isolate->FindCodeObject(addr);
904 CHECK(found->IsCode());
905 CHECK(code->address() == HeapObject::cast(found)->address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000906 break;
907 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000908 case INTERNAL_REFERENCE:
909 case INTERNAL_REFERENCE_ENCODED: {
910 Address target = target_internal_reference();
911 Address pc = target_internal_reference_address();
912 Code* code = Code::cast(isolate->FindCodeObject(pc));
913 CHECK(target >= code->instruction_start());
914 CHECK(target <= code->instruction_end());
915 break;
916 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000917 case RUNTIME_ENTRY:
Steve Blocka7e24c12009-10-30 11:49:00 +0000918 case COMMENT:
919 case POSITION:
920 case STATEMENT_POSITION:
921 case EXTERNAL_REFERENCE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000922 case DEOPT_REASON:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000923 case CONST_POOL:
924 case VENEER_POOL:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000925 case DEBUG_BREAK_SLOT_AT_POSITION:
926 case DEBUG_BREAK_SLOT_AT_RETURN:
927 case DEBUG_BREAK_SLOT_AT_CALL:
928 case GENERATOR_CONTINUATION:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000929 case NONE32:
930 case NONE64:
Steve Blocka7e24c12009-10-30 11:49:00 +0000931 break;
932 case NUMBER_OF_MODES:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000933 case PC_JUMP:
Steve Blocka7e24c12009-10-30 11:49:00 +0000934 UNREACHABLE();
935 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000936 case CODE_AGE_SEQUENCE:
937 DCHECK(Code::IsYoungSequence(isolate, pc_) || code_age_stub()->IsCode());
938 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000939 }
940}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000941#endif // VERIFY_HEAP
Steve Blocka7e24c12009-10-30 11:49:00 +0000942
943
Steve Blocka7e24c12009-10-30 11:49:00 +0000944// Implementation of ExternalReference
945
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000946void ExternalReference::SetUp() {
947 double_constants.min_int = kMinInt;
948 double_constants.one_half = 0.5;
949 double_constants.minus_one_half = -0.5;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000950 double_constants.the_hole_nan = bit_cast<double>(kHoleNanInt64);
951 double_constants.negative_infinity = -V8_INFINITY;
952 double_constants.uint32_bias =
953 static_cast<double>(static_cast<uint32_t>(0xFFFFFFFF)) + 1;
954
955 math_exp_data_mutex = new base::Mutex();
956}
957
958
959void ExternalReference::InitializeMathExpData() {
960 // Early return?
961 if (math_exp_data_initialized) return;
962
963 base::LockGuard<base::Mutex> lock_guard(math_exp_data_mutex);
964 if (!math_exp_data_initialized) {
965 // If this is changed, generated code must be adapted too.
966 const int kTableSizeBits = 11;
967 const int kTableSize = 1 << kTableSizeBits;
968 const double kTableSizeDouble = static_cast<double>(kTableSize);
969
970 math_exp_constants_array = new double[9];
971 // Input values smaller than this always return 0.
972 math_exp_constants_array[0] = -708.39641853226408;
973 // Input values larger than this always return +Infinity.
974 math_exp_constants_array[1] = 709.78271289338397;
975 math_exp_constants_array[2] = V8_INFINITY;
976 // The rest is black magic. Do not attempt to understand it. It is
977 // loosely based on the "expd" function published at:
978 // http://herumi.blogspot.com/2011/08/fast-double-precision-exponential.html
979 const double constant3 = (1 << kTableSizeBits) / std::log(2.0);
980 math_exp_constants_array[3] = constant3;
981 math_exp_constants_array[4] =
982 static_cast<double>(static_cast<int64_t>(3) << 51);
983 math_exp_constants_array[5] = 1 / constant3;
984 math_exp_constants_array[6] = 3.0000000027955394;
985 math_exp_constants_array[7] = 0.16666666685227835;
986 math_exp_constants_array[8] = 1;
987
988 math_exp_log_table_array = new double[kTableSize];
989 for (int i = 0; i < kTableSize; i++) {
990 double value = std::pow(2, i / kTableSizeDouble);
991 uint64_t bits = bit_cast<uint64_t, double>(value);
992 bits &= (static_cast<uint64_t>(1) << 52) - 1;
993 double mantissa = bit_cast<double, uint64_t>(bits);
994 math_exp_log_table_array[i] = mantissa;
995 }
996
997 math_exp_data_initialized = true;
998 }
999}
1000
1001
1002void ExternalReference::TearDownMathExpData() {
1003 delete[] math_exp_constants_array;
1004 math_exp_constants_array = NULL;
1005 delete[] math_exp_log_table_array;
1006 math_exp_log_table_array = NULL;
1007 delete math_exp_data_mutex;
1008 math_exp_data_mutex = NULL;
1009}
1010
1011
Steve Block44f0eee2011-05-26 01:26:41 +01001012ExternalReference::ExternalReference(Builtins::CFunctionId id, Isolate* isolate)
1013 : address_(Redirect(isolate, Builtins::c_function_address(id))) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001014
1015
Steve Block1e0659c2011-05-24 12:43:12 +01001016ExternalReference::ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01001017 ApiFunction* fun,
1018 Type type = ExternalReference::BUILTIN_CALL,
1019 Isolate* isolate = NULL)
1020 : address_(Redirect(isolate, fun->address(), type)) {}
Steve Blockd0582a62009-12-15 09:54:21 +00001021
1022
Steve Block44f0eee2011-05-26 01:26:41 +01001023ExternalReference::ExternalReference(Builtins::Name name, Isolate* isolate)
1024 : address_(isolate->builtins()->builtin_address(name)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001025
1026
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001027ExternalReference::ExternalReference(Runtime::FunctionId id, Isolate* isolate)
1028 : address_(Redirect(isolate, Runtime::FunctionForId(id)->entry)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001029
1030
Steve Block44f0eee2011-05-26 01:26:41 +01001031ExternalReference::ExternalReference(const Runtime::Function* f,
1032 Isolate* isolate)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001033 : address_(Redirect(isolate, f->entry)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001034
1035
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001036ExternalReference ExternalReference::isolate_address(Isolate* isolate) {
1037 return ExternalReference(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001038}
1039
1040
Steve Blocka7e24c12009-10-30 11:49:00 +00001041ExternalReference::ExternalReference(StatsCounter* counter)
1042 : address_(reinterpret_cast<Address>(counter->GetInternalPointer())) {}
1043
1044
Steve Block44f0eee2011-05-26 01:26:41 +01001045ExternalReference::ExternalReference(Isolate::AddressId id, Isolate* isolate)
1046 : address_(isolate->get_address_from_id(id)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001047
1048
1049ExternalReference::ExternalReference(const SCTableReference& table_ref)
1050 : address_(table_ref.address()) {}
1051
1052
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001053ExternalReference ExternalReference::
1054 incremental_marking_record_write_function(Isolate* isolate) {
1055 return ExternalReference(Redirect(
1056 isolate,
1057 FUNCTION_ADDR(IncrementalMarking::RecordWriteFromCode)));
1058}
1059
1060
1061ExternalReference ExternalReference::
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001062 store_buffer_overflow_function(Isolate* isolate) {
1063 return ExternalReference(Redirect(
1064 isolate,
1065 FUNCTION_ADDR(StoreBuffer::StoreBufferOverflow)));
1066}
1067
1068
Steve Block44f0eee2011-05-26 01:26:41 +01001069ExternalReference ExternalReference::delete_handle_scope_extensions(
1070 Isolate* isolate) {
1071 return ExternalReference(Redirect(
1072 isolate,
1073 FUNCTION_ADDR(HandleScope::DeleteExtensions)));
John Reck59135872010-11-02 12:39:01 -07001074}
1075
1076
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001077ExternalReference ExternalReference::get_date_field_function(
1078 Isolate* isolate) {
1079 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(JSDate::GetField)));
1080}
1081
1082
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001083ExternalReference ExternalReference::get_make_code_young_function(
1084 Isolate* isolate) {
1085 return ExternalReference(Redirect(
1086 isolate, FUNCTION_ADDR(Code::MakeCodeAgeSequenceYoung)));
1087}
1088
1089
1090ExternalReference ExternalReference::get_mark_code_as_executed_function(
1091 Isolate* isolate) {
1092 return ExternalReference(Redirect(
1093 isolate, FUNCTION_ADDR(Code::MarkCodeAsExecuted)));
1094}
1095
1096
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001097ExternalReference ExternalReference::date_cache_stamp(Isolate* isolate) {
1098 return ExternalReference(isolate->date_cache()->stamp_address());
1099}
1100
1101
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001102ExternalReference ExternalReference::stress_deopt_count(Isolate* isolate) {
1103 return ExternalReference(isolate->stress_deopt_count_address());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001104}
1105
1106
Steve Block44f0eee2011-05-26 01:26:41 +01001107ExternalReference ExternalReference::new_deoptimizer_function(
1108 Isolate* isolate) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001109 return ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01001110 Redirect(isolate, FUNCTION_ADDR(Deoptimizer::New)));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001111}
1112
1113
Steve Block44f0eee2011-05-26 01:26:41 +01001114ExternalReference ExternalReference::compute_output_frames_function(
1115 Isolate* isolate) {
1116 return ExternalReference(
1117 Redirect(isolate, FUNCTION_ADDR(Deoptimizer::ComputeOutputFrames)));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001118}
1119
1120
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001121ExternalReference ExternalReference::log_enter_external_function(
1122 Isolate* isolate) {
1123 return ExternalReference(
1124 Redirect(isolate, FUNCTION_ADDR(Logger::EnterExternal)));
1125}
1126
1127
1128ExternalReference ExternalReference::log_leave_external_function(
1129 Isolate* isolate) {
1130 return ExternalReference(
1131 Redirect(isolate, FUNCTION_ADDR(Logger::LeaveExternal)));
1132}
1133
1134
Steve Block44f0eee2011-05-26 01:26:41 +01001135ExternalReference ExternalReference::keyed_lookup_cache_keys(Isolate* isolate) {
1136 return ExternalReference(isolate->keyed_lookup_cache()->keys_address());
Steve Blocka7e24c12009-10-30 11:49:00 +00001137}
1138
1139
Steve Block44f0eee2011-05-26 01:26:41 +01001140ExternalReference ExternalReference::keyed_lookup_cache_field_offsets(
1141 Isolate* isolate) {
1142 return ExternalReference(
1143 isolate->keyed_lookup_cache()->field_offsets_address());
Steve Blocka7e24c12009-10-30 11:49:00 +00001144}
1145
1146
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001147ExternalReference ExternalReference::roots_array_start(Isolate* isolate) {
1148 return ExternalReference(isolate->heap()->roots_array_start());
Steve Blocka7e24c12009-10-30 11:49:00 +00001149}
1150
1151
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001152ExternalReference ExternalReference::allocation_sites_list_address(
1153 Isolate* isolate) {
1154 return ExternalReference(isolate->heap()->allocation_sites_list_address());
1155}
1156
1157
Steve Block44f0eee2011-05-26 01:26:41 +01001158ExternalReference ExternalReference::address_of_stack_limit(Isolate* isolate) {
1159 return ExternalReference(isolate->stack_guard()->address_of_jslimit());
Steve Blockd0582a62009-12-15 09:54:21 +00001160}
1161
1162
Steve Block44f0eee2011-05-26 01:26:41 +01001163ExternalReference ExternalReference::address_of_real_stack_limit(
1164 Isolate* isolate) {
1165 return ExternalReference(isolate->stack_guard()->address_of_real_jslimit());
Steve Blocka7e24c12009-10-30 11:49:00 +00001166}
1167
1168
Steve Block44f0eee2011-05-26 01:26:41 +01001169ExternalReference ExternalReference::address_of_regexp_stack_limit(
1170 Isolate* isolate) {
1171 return ExternalReference(isolate->regexp_stack()->limit_address());
Steve Blocka7e24c12009-10-30 11:49:00 +00001172}
1173
1174
Steve Block44f0eee2011-05-26 01:26:41 +01001175ExternalReference ExternalReference::new_space_start(Isolate* isolate) {
1176 return ExternalReference(isolate->heap()->NewSpaceStart());
Andrei Popescu402d9372010-02-26 13:31:12 +00001177}
1178
1179
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001180ExternalReference ExternalReference::store_buffer_top(Isolate* isolate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001181 return ExternalReference(isolate->heap()->store_buffer_top_address());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001182}
1183
1184
Steve Block44f0eee2011-05-26 01:26:41 +01001185ExternalReference ExternalReference::new_space_mask(Isolate* isolate) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001186 return ExternalReference(reinterpret_cast<Address>(
1187 isolate->heap()->NewSpaceMask()));
Steve Blocka7e24c12009-10-30 11:49:00 +00001188}
1189
1190
Steve Block44f0eee2011-05-26 01:26:41 +01001191ExternalReference ExternalReference::new_space_allocation_top_address(
1192 Isolate* isolate) {
1193 return ExternalReference(isolate->heap()->NewSpaceAllocationTopAddress());
Steve Blocka7e24c12009-10-30 11:49:00 +00001194}
1195
1196
Steve Block44f0eee2011-05-26 01:26:41 +01001197ExternalReference ExternalReference::new_space_allocation_limit_address(
1198 Isolate* isolate) {
1199 return ExternalReference(isolate->heap()->NewSpaceAllocationLimitAddress());
Steve Blocka7e24c12009-10-30 11:49:00 +00001200}
1201
Steve Blockd0582a62009-12-15 09:54:21 +00001202
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001203ExternalReference ExternalReference::old_space_allocation_top_address(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001204 Isolate* isolate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001205 return ExternalReference(isolate->heap()->OldSpaceAllocationTopAddress());
Steve Blockd0582a62009-12-15 09:54:21 +00001206}
1207
1208
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001209ExternalReference ExternalReference::old_space_allocation_limit_address(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001210 Isolate* isolate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001211 return ExternalReference(isolate->heap()->OldSpaceAllocationLimitAddress());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001212}
1213
1214
1215ExternalReference ExternalReference::handle_scope_level_address(
1216 Isolate* isolate) {
1217 return ExternalReference(HandleScope::current_level_address(isolate));
1218}
1219
1220
1221ExternalReference ExternalReference::handle_scope_next_address(
1222 Isolate* isolate) {
1223 return ExternalReference(HandleScope::current_next_address(isolate));
1224}
1225
1226
1227ExternalReference ExternalReference::handle_scope_limit_address(
1228 Isolate* isolate) {
1229 return ExternalReference(HandleScope::current_limit_address(isolate));
Steve Blockd0582a62009-12-15 09:54:21 +00001230}
1231
1232
Steve Block44f0eee2011-05-26 01:26:41 +01001233ExternalReference ExternalReference::scheduled_exception_address(
1234 Isolate* isolate) {
1235 return ExternalReference(isolate->scheduled_exception_address());
Steve Blockd0582a62009-12-15 09:54:21 +00001236}
1237
1238
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001239ExternalReference ExternalReference::address_of_pending_message_obj(
1240 Isolate* isolate) {
1241 return ExternalReference(isolate->pending_message_obj_address());
1242}
1243
1244
Ben Murdochb0fe1622011-05-05 13:52:32 +01001245ExternalReference ExternalReference::address_of_min_int() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001246 return ExternalReference(reinterpret_cast<void*>(&double_constants.min_int));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001247}
1248
1249
1250ExternalReference ExternalReference::address_of_one_half() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001251 return ExternalReference(reinterpret_cast<void*>(&double_constants.one_half));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001252}
1253
1254
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001255ExternalReference ExternalReference::address_of_minus_one_half() {
1256 return ExternalReference(
1257 reinterpret_cast<void*>(&double_constants.minus_one_half));
Ben Murdoch257744e2011-11-30 15:57:28 +00001258}
1259
1260
Ben Murdochb0fe1622011-05-05 13:52:32 +01001261ExternalReference ExternalReference::address_of_negative_infinity() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001262 return ExternalReference(
1263 reinterpret_cast<void*>(&double_constants.negative_infinity));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001264}
1265
1266
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001267ExternalReference ExternalReference::address_of_the_hole_nan() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001268 return ExternalReference(
1269 reinterpret_cast<void*>(&double_constants.the_hole_nan));
1270}
1271
1272
1273ExternalReference ExternalReference::address_of_uint32_bias() {
1274 return ExternalReference(
1275 reinterpret_cast<void*>(&double_constants.uint32_bias));
1276}
1277
1278
1279ExternalReference ExternalReference::is_profiling_address(Isolate* isolate) {
1280 return ExternalReference(isolate->cpu_profiler()->is_profiling_address());
1281}
1282
1283
1284ExternalReference ExternalReference::invoke_function_callback(
1285 Isolate* isolate) {
1286 Address thunk_address = FUNCTION_ADDR(&InvokeFunctionCallback);
1287 ExternalReference::Type thunk_type = ExternalReference::PROFILING_API_CALL;
1288 ApiFunction thunk_fun(thunk_address);
1289 return ExternalReference(&thunk_fun, thunk_type, isolate);
1290}
1291
1292
1293ExternalReference ExternalReference::invoke_accessor_getter_callback(
1294 Isolate* isolate) {
1295 Address thunk_address = FUNCTION_ADDR(&InvokeAccessorGetterCallback);
1296 ExternalReference::Type thunk_type =
1297 ExternalReference::PROFILING_GETTER_CALL;
1298 ApiFunction thunk_fun(thunk_address);
1299 return ExternalReference(&thunk_fun, thunk_type, isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001300}
1301
1302
Steve Block6ded16b2010-05-10 14:33:55 +01001303#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +00001304
Steve Block44f0eee2011-05-26 01:26:41 +01001305ExternalReference ExternalReference::re_check_stack_guard_state(
1306 Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001307 Address function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001308#if V8_TARGET_ARCH_X64
Steve Blocka7e24c12009-10-30 11:49:00 +00001309 function = FUNCTION_ADDR(RegExpMacroAssemblerX64::CheckStackGuardState);
1310#elif V8_TARGET_ARCH_IA32
1311 function = FUNCTION_ADDR(RegExpMacroAssemblerIA32::CheckStackGuardState);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001312#elif V8_TARGET_ARCH_ARM64
1313 function = FUNCTION_ADDR(RegExpMacroAssemblerARM64::CheckStackGuardState);
Steve Blocka7e24c12009-10-30 11:49:00 +00001314#elif V8_TARGET_ARCH_ARM
1315 function = FUNCTION_ADDR(RegExpMacroAssemblerARM::CheckStackGuardState);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001316#elif V8_TARGET_ARCH_PPC
1317 function = FUNCTION_ADDR(RegExpMacroAssemblerPPC::CheckStackGuardState);
Steve Block44f0eee2011-05-26 01:26:41 +01001318#elif V8_TARGET_ARCH_MIPS
1319 function = FUNCTION_ADDR(RegExpMacroAssemblerMIPS::CheckStackGuardState);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001320#elif V8_TARGET_ARCH_MIPS64
1321 function = FUNCTION_ADDR(RegExpMacroAssemblerMIPS::CheckStackGuardState);
1322#elif V8_TARGET_ARCH_X87
1323 function = FUNCTION_ADDR(RegExpMacroAssemblerX87::CheckStackGuardState);
Steve Blocka7e24c12009-10-30 11:49:00 +00001324#else
Leon Clarke4515c472010-02-03 11:58:03 +00001325 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +00001326#endif
Steve Block44f0eee2011-05-26 01:26:41 +01001327 return ExternalReference(Redirect(isolate, function));
Steve Blocka7e24c12009-10-30 11:49:00 +00001328}
1329
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001330
Steve Block44f0eee2011-05-26 01:26:41 +01001331ExternalReference ExternalReference::re_grow_stack(Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001332 return ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01001333 Redirect(isolate, FUNCTION_ADDR(NativeRegExpMacroAssembler::GrowStack)));
Steve Blocka7e24c12009-10-30 11:49:00 +00001334}
1335
Steve Block44f0eee2011-05-26 01:26:41 +01001336ExternalReference ExternalReference::re_case_insensitive_compare_uc16(
1337 Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001338 return ExternalReference(Redirect(
Steve Block44f0eee2011-05-26 01:26:41 +01001339 isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +00001340 FUNCTION_ADDR(NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16)));
1341}
1342
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001343
Leon Clarkee46be812010-01-19 14:06:41 +00001344ExternalReference ExternalReference::re_word_character_map() {
1345 return ExternalReference(
1346 NativeRegExpMacroAssembler::word_character_map_address());
1347}
1348
Steve Block44f0eee2011-05-26 01:26:41 +01001349ExternalReference ExternalReference::address_of_static_offsets_vector(
1350 Isolate* isolate) {
1351 return ExternalReference(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001352 reinterpret_cast<Address>(isolate->jsregexp_static_offsets_vector()));
Leon Clarkee46be812010-01-19 14:06:41 +00001353}
1354
Steve Block44f0eee2011-05-26 01:26:41 +01001355ExternalReference ExternalReference::address_of_regexp_stack_memory_address(
1356 Isolate* isolate) {
1357 return ExternalReference(
1358 isolate->regexp_stack()->memory_address());
Leon Clarkee46be812010-01-19 14:06:41 +00001359}
1360
Steve Block44f0eee2011-05-26 01:26:41 +01001361ExternalReference ExternalReference::address_of_regexp_stack_memory_size(
1362 Isolate* isolate) {
1363 return ExternalReference(isolate->regexp_stack()->memory_size_address());
Leon Clarkee46be812010-01-19 14:06:41 +00001364}
1365
Steve Block6ded16b2010-05-10 14:33:55 +01001366#endif // V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +00001367
1368
Steve Block44f0eee2011-05-26 01:26:41 +01001369ExternalReference ExternalReference::math_log_double_function(
1370 Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001371 typedef double (*d2d)(double x);
Steve Block44f0eee2011-05-26 01:26:41 +01001372 return ExternalReference(Redirect(isolate,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001373 FUNCTION_ADDR(static_cast<d2d>(std::log)),
Ben Murdoch257744e2011-11-30 15:57:28 +00001374 BUILTIN_FP_CALL));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001375}
1376
1377
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001378ExternalReference ExternalReference::math_exp_constants(int constant_index) {
1379 DCHECK(math_exp_data_initialized);
1380 return ExternalReference(
1381 reinterpret_cast<void*>(math_exp_constants_array + constant_index));
1382}
1383
1384
1385ExternalReference ExternalReference::math_exp_log_table() {
1386 DCHECK(math_exp_data_initialized);
1387 return ExternalReference(reinterpret_cast<void*>(math_exp_log_table_array));
1388}
1389
1390
1391ExternalReference ExternalReference::page_flags(Page* page) {
1392 return ExternalReference(reinterpret_cast<Address>(page) +
1393 MemoryChunk::kFlagsOffset);
1394}
1395
1396
1397ExternalReference ExternalReference::ForDeoptEntry(Address entry) {
1398 return ExternalReference(entry);
1399}
1400
1401
1402ExternalReference ExternalReference::cpu_features() {
1403 DCHECK(CpuFeatures::initialized_);
1404 return ExternalReference(&CpuFeatures::supported_);
1405}
1406
1407
1408ExternalReference ExternalReference::debug_is_active_address(
1409 Isolate* isolate) {
1410 return ExternalReference(isolate->debug()->is_active_address());
1411}
1412
1413
1414ExternalReference ExternalReference::debug_after_break_target_address(
1415 Isolate* isolate) {
1416 return ExternalReference(isolate->debug()->after_break_target_address());
1417}
1418
1419
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001420ExternalReference ExternalReference::virtual_handler_register(
1421 Isolate* isolate) {
1422 return ExternalReference(isolate->virtual_handler_register_address());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001423}
1424
1425
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001426ExternalReference ExternalReference::virtual_slot_register(Isolate* isolate) {
1427 return ExternalReference(isolate->virtual_slot_register_address());
1428}
1429
1430
1431ExternalReference ExternalReference::runtime_function_table_address(
1432 Isolate* isolate) {
1433 return ExternalReference(
1434 const_cast<Runtime::Function*>(Runtime::RuntimeFunctionTable(isolate)));
1435}
1436
1437
1438double power_helper(Isolate* isolate, double x, double y) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001439 int y_int = static_cast<int>(y);
1440 if (y == y_int) {
1441 return power_double_int(x, y_int); // Returns 1 if exponent is 0.
1442 }
1443 if (y == 0.5) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001444 lazily_initialize_fast_sqrt(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001445 return (std::isinf(x)) ? V8_INFINITY
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001446 : fast_sqrt(x + 0.0, isolate); // Convert -0 to +0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001447 }
1448 if (y == -0.5) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001449 lazily_initialize_fast_sqrt(isolate);
1450 return (std::isinf(x)) ? 0 : 1.0 / fast_sqrt(x + 0.0,
1451 isolate); // Convert -0 to +0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001452 }
1453 return power_double_double(x, y);
1454}
1455
1456
Ben Murdochb0fe1622011-05-05 13:52:32 +01001457// Helper function to compute x^y, where y is known to be an
1458// integer. Uses binary decomposition to limit the number of
1459// multiplications; see the discussion in "Hacker's Delight" by Henry
1460// S. Warren, Jr., figure 11-6, page 213.
1461double power_double_int(double x, int y) {
1462 double m = (y < 0) ? 1 / x : x;
1463 unsigned n = (y < 0) ? -y : y;
1464 double p = 1;
1465 while (n != 0) {
1466 if ((n & 1) != 0) p *= m;
1467 m *= m;
1468 if ((n & 2) != 0) p *= m;
1469 m *= m;
1470 n >>= 2;
1471 }
1472 return p;
1473}
1474
1475
1476double power_double_double(double x, double y) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001477#if (defined(__MINGW64_VERSION_MAJOR) && \
1478 (!defined(__MINGW64_VERSION_RC) || __MINGW64_VERSION_RC < 1)) || \
1479 defined(V8_OS_AIX)
1480 // MinGW64 and AIX have a custom implementation for pow. This handles certain
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001481 // special cases that are different.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001482 if ((x == 0.0 || std::isinf(x)) && y != 0.0 && std::isfinite(y)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001483 double f;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001484 double result = ((x == 0.0) ^ (y > 0)) ? V8_INFINITY : 0;
1485 /* retain sign if odd integer exponent */
1486 return ((std::modf(y, &f) == 0.0) && (static_cast<int64_t>(y) & 1))
1487 ? copysign(result, x)
1488 : result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001489 }
1490
1491 if (x == 2.0) {
1492 int y_int = static_cast<int>(y);
1493 if (y == y_int) {
1494 return std::ldexp(1.0, y_int);
1495 }
1496 }
1497#endif
1498
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001499 // The checks for special cases can be dropped in ia32 because it has already
1500 // been done in generated code before bailing out here.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001501 if (std::isnan(y) || ((x == 1 || x == -1) && std::isinf(y))) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001502 return std::numeric_limits<double>::quiet_NaN();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001503 }
1504 return std::pow(x, y);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001505}
1506
1507
Steve Block44f0eee2011-05-26 01:26:41 +01001508ExternalReference ExternalReference::power_double_double_function(
1509 Isolate* isolate) {
1510 return ExternalReference(Redirect(isolate,
1511 FUNCTION_ADDR(power_double_double),
Ben Murdoch257744e2011-11-30 15:57:28 +00001512 BUILTIN_FP_FP_CALL));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001513}
1514
1515
Steve Block44f0eee2011-05-26 01:26:41 +01001516ExternalReference ExternalReference::power_double_int_function(
1517 Isolate* isolate) {
1518 return ExternalReference(Redirect(isolate,
1519 FUNCTION_ADDR(power_double_int),
Ben Murdoch257744e2011-11-30 15:57:28 +00001520 BUILTIN_FP_INT_CALL));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001521}
1522
1523
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001524bool EvalComparison(Token::Value op, double op1, double op2) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001525 DCHECK(Token::IsCompareOp(op));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001526 switch (op) {
1527 case Token::EQ:
1528 case Token::EQ_STRICT: return (op1 == op2);
1529 case Token::NE: return (op1 != op2);
1530 case Token::LT: return (op1 < op2);
1531 case Token::GT: return (op1 > op2);
1532 case Token::LTE: return (op1 <= op2);
1533 case Token::GTE: return (op1 >= op2);
1534 default:
1535 UNREACHABLE();
1536 return false;
1537 }
1538}
1539
1540
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001541ExternalReference ExternalReference::mod_two_doubles_operation(
1542 Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +01001543 return ExternalReference(Redirect(isolate,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001544 FUNCTION_ADDR(modulo),
Ben Murdoch257744e2011-11-30 15:57:28 +00001545 BUILTIN_FP_FP_CALL));
Steve Blocka7e24c12009-10-30 11:49:00 +00001546}
1547
1548
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001549ExternalReference ExternalReference::debug_step_in_enabled_address(
1550 Isolate* isolate) {
1551 return ExternalReference(isolate->debug()->step_in_enabled_address());
Steve Blocka7e24c12009-10-30 11:49:00 +00001552}
1553
1554
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001555ExternalReference ExternalReference::fixed_typed_array_base_data_offset() {
1556 return ExternalReference(reinterpret_cast<void*>(
1557 FixedTypedArrayBase::kDataOffset - kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001558}
Steve Blocka7e24c12009-10-30 11:49:00 +00001559
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001560
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001561bool operator==(ExternalReference lhs, ExternalReference rhs) {
1562 return lhs.address() == rhs.address();
1563}
1564
1565
1566bool operator!=(ExternalReference lhs, ExternalReference rhs) {
1567 return !(lhs == rhs);
1568}
1569
1570
1571size_t hash_value(ExternalReference reference) {
1572 return base::hash<Address>()(reference.address());
1573}
1574
1575
1576std::ostream& operator<<(std::ostream& os, ExternalReference reference) {
1577 os << static_cast<const void*>(reference.address());
1578 const Runtime::Function* fn = Runtime::FunctionForEntry(reference.address());
1579 if (fn) os << "<" << fn->name << ".entry>";
1580 return os;
1581}
1582
1583
Ben Murdochb0fe1622011-05-05 13:52:32 +01001584void PositionsRecorder::RecordPosition(int pos) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001585 DCHECK(pos != RelocInfo::kNoPosition);
1586 DCHECK(pos >= 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001587 state_.current_position = pos;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001588 LOG_CODE_EVENT(assembler_->isolate(),
1589 CodeLinePosInfoAddPositionEvent(jit_handler_data_,
1590 assembler_->pc_offset(),
1591 pos));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001592}
1593
1594
1595void PositionsRecorder::RecordStatementPosition(int pos) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001596 DCHECK(pos != RelocInfo::kNoPosition);
1597 DCHECK(pos >= 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001598 state_.current_statement_position = pos;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001599 LOG_CODE_EVENT(assembler_->isolate(),
1600 CodeLinePosInfoAddStatementPositionEvent(
1601 jit_handler_data_,
1602 assembler_->pc_offset(),
1603 pos));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001604}
1605
1606
1607bool PositionsRecorder::WriteRecordedPositions() {
1608 bool written = false;
1609
1610 // Write the statement position if it is different from what was written last
1611 // time.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001612 if (state_.current_statement_position != state_.written_statement_position) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001613 EnsureSpace ensure_space(assembler_);
1614 assembler_->RecordRelocInfo(RelocInfo::STATEMENT_POSITION,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001615 state_.current_statement_position);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001616 state_.written_position = state_.current_statement_position;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001617 state_.written_statement_position = state_.current_statement_position;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001618 written = true;
1619 }
1620
1621 // Write the position if it is different from what was written last time and
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001622 // also different from the statement position that was just written.
1623 if (state_.current_position != state_.written_position) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001624 EnsureSpace ensure_space(assembler_);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001625 assembler_->RecordRelocInfo(RelocInfo::POSITION, state_.current_position);
1626 state_.written_position = state_.current_position;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001627 written = true;
1628 }
1629
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001630 // Return whether something was written.
1631 return written;
1632}
1633
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001634
1635ConstantPoolBuilder::ConstantPoolBuilder(int ptr_reach_bits,
1636 int double_reach_bits) {
1637 info_[ConstantPoolEntry::INTPTR].entries.reserve(64);
1638 info_[ConstantPoolEntry::INTPTR].regular_reach_bits = ptr_reach_bits;
1639 info_[ConstantPoolEntry::DOUBLE].regular_reach_bits = double_reach_bits;
1640}
1641
1642
1643ConstantPoolEntry::Access ConstantPoolBuilder::NextAccess(
1644 ConstantPoolEntry::Type type) const {
1645 const PerTypeEntryInfo& info = info_[type];
1646
1647 if (info.overflow()) return ConstantPoolEntry::OVERFLOWED;
1648
1649 int dbl_count = info_[ConstantPoolEntry::DOUBLE].regular_count;
1650 int dbl_offset = dbl_count * kDoubleSize;
1651 int ptr_count = info_[ConstantPoolEntry::INTPTR].regular_count;
1652 int ptr_offset = ptr_count * kPointerSize + dbl_offset;
1653
1654 if (type == ConstantPoolEntry::DOUBLE) {
1655 // Double overflow detection must take into account the reach for both types
1656 int ptr_reach_bits = info_[ConstantPoolEntry::INTPTR].regular_reach_bits;
1657 if (!is_uintn(dbl_offset, info.regular_reach_bits) ||
1658 (ptr_count > 0 &&
1659 !is_uintn(ptr_offset + kDoubleSize - kPointerSize, ptr_reach_bits))) {
1660 return ConstantPoolEntry::OVERFLOWED;
1661 }
1662 } else {
1663 DCHECK(type == ConstantPoolEntry::INTPTR);
1664 if (!is_uintn(ptr_offset, info.regular_reach_bits)) {
1665 return ConstantPoolEntry::OVERFLOWED;
1666 }
1667 }
1668
1669 return ConstantPoolEntry::REGULAR;
1670}
1671
1672
1673ConstantPoolEntry::Access ConstantPoolBuilder::AddEntry(
1674 ConstantPoolEntry& entry, ConstantPoolEntry::Type type) {
1675 DCHECK(!emitted_label_.is_bound());
1676 PerTypeEntryInfo& info = info_[type];
1677 const int entry_size = ConstantPoolEntry::size(type);
1678 bool merged = false;
1679
1680 if (entry.sharing_ok()) {
1681 // Try to merge entries
1682 std::vector<ConstantPoolEntry>::iterator it = info.shared_entries.begin();
1683 int end = static_cast<int>(info.shared_entries.size());
1684 for (int i = 0; i < end; i++, it++) {
1685 if ((entry_size == kPointerSize) ? entry.value() == it->value()
1686 : entry.value64() == it->value64()) {
1687 // Merge with found entry.
1688 entry.set_merged_index(i);
1689 merged = true;
1690 break;
1691 }
1692 }
1693 }
1694
1695 // By definition, merged entries have regular access.
1696 DCHECK(!merged || entry.merged_index() < info.regular_count);
1697 ConstantPoolEntry::Access access =
1698 (merged ? ConstantPoolEntry::REGULAR : NextAccess(type));
1699
1700 // Enforce an upper bound on search time by limiting the search to
1701 // unique sharable entries which fit in the regular section.
1702 if (entry.sharing_ok() && !merged && access == ConstantPoolEntry::REGULAR) {
1703 info.shared_entries.push_back(entry);
1704 } else {
1705 info.entries.push_back(entry);
1706 }
1707
1708 // We're done if we found a match or have already triggered the
1709 // overflow state.
1710 if (merged || info.overflow()) return access;
1711
1712 if (access == ConstantPoolEntry::REGULAR) {
1713 info.regular_count++;
1714 } else {
1715 info.overflow_start = static_cast<int>(info.entries.size()) - 1;
1716 }
1717
1718 return access;
1719}
1720
1721
1722void ConstantPoolBuilder::EmitSharedEntries(Assembler* assm,
1723 ConstantPoolEntry::Type type) {
1724 PerTypeEntryInfo& info = info_[type];
1725 std::vector<ConstantPoolEntry>& shared_entries = info.shared_entries;
1726 const int entry_size = ConstantPoolEntry::size(type);
1727 int base = emitted_label_.pos();
1728 DCHECK(base > 0);
1729 int shared_end = static_cast<int>(shared_entries.size());
1730 std::vector<ConstantPoolEntry>::iterator shared_it = shared_entries.begin();
1731 for (int i = 0; i < shared_end; i++, shared_it++) {
1732 int offset = assm->pc_offset() - base;
1733 shared_it->set_offset(offset); // Save offset for merged entries.
1734 if (entry_size == kPointerSize) {
1735 assm->dp(shared_it->value());
1736 } else {
1737 assm->dq(shared_it->value64());
1738 }
1739 DCHECK(is_uintn(offset, info.regular_reach_bits));
1740
1741 // Patch load sequence with correct offset.
1742 assm->PatchConstantPoolAccessInstruction(shared_it->position(), offset,
1743 ConstantPoolEntry::REGULAR, type);
1744 }
1745}
1746
1747
1748void ConstantPoolBuilder::EmitGroup(Assembler* assm,
1749 ConstantPoolEntry::Access access,
1750 ConstantPoolEntry::Type type) {
1751 PerTypeEntryInfo& info = info_[type];
1752 const bool overflow = info.overflow();
1753 std::vector<ConstantPoolEntry>& entries = info.entries;
1754 std::vector<ConstantPoolEntry>& shared_entries = info.shared_entries;
1755 const int entry_size = ConstantPoolEntry::size(type);
1756 int base = emitted_label_.pos();
1757 DCHECK(base > 0);
1758 int begin;
1759 int end;
1760
1761 if (access == ConstantPoolEntry::REGULAR) {
1762 // Emit any shared entries first
1763 EmitSharedEntries(assm, type);
1764 }
1765
1766 if (access == ConstantPoolEntry::REGULAR) {
1767 begin = 0;
1768 end = overflow ? info.overflow_start : static_cast<int>(entries.size());
1769 } else {
1770 DCHECK(access == ConstantPoolEntry::OVERFLOWED);
1771 if (!overflow) return;
1772 begin = info.overflow_start;
1773 end = static_cast<int>(entries.size());
1774 }
1775
1776 std::vector<ConstantPoolEntry>::iterator it = entries.begin();
1777 if (begin > 0) std::advance(it, begin);
1778 for (int i = begin; i < end; i++, it++) {
1779 // Update constant pool if necessary and get the entry's offset.
1780 int offset;
1781 ConstantPoolEntry::Access entry_access;
1782 if (!it->is_merged()) {
1783 // Emit new entry
1784 offset = assm->pc_offset() - base;
1785 entry_access = access;
1786 if (entry_size == kPointerSize) {
1787 assm->dp(it->value());
1788 } else {
1789 assm->dq(it->value64());
1790 }
1791 } else {
1792 // Retrieve offset from shared entry.
1793 offset = shared_entries[it->merged_index()].offset();
1794 entry_access = ConstantPoolEntry::REGULAR;
1795 }
1796
1797 DCHECK(entry_access == ConstantPoolEntry::OVERFLOWED ||
1798 is_uintn(offset, info.regular_reach_bits));
1799
1800 // Patch load sequence with correct offset.
1801 assm->PatchConstantPoolAccessInstruction(it->position(), offset,
1802 entry_access, type);
1803 }
1804}
1805
1806
1807// Emit and return position of pool. Zero implies no constant pool.
1808int ConstantPoolBuilder::Emit(Assembler* assm) {
1809 bool emitted = emitted_label_.is_bound();
1810 bool empty = IsEmpty();
1811
1812 if (!emitted) {
1813 // Mark start of constant pool. Align if necessary.
1814 if (!empty) assm->DataAlign(kDoubleSize);
1815 assm->bind(&emitted_label_);
1816 if (!empty) {
1817 // Emit in groups based on access and type.
1818 // Emit doubles first for alignment purposes.
1819 EmitGroup(assm, ConstantPoolEntry::REGULAR, ConstantPoolEntry::DOUBLE);
1820 EmitGroup(assm, ConstantPoolEntry::REGULAR, ConstantPoolEntry::INTPTR);
1821 if (info_[ConstantPoolEntry::DOUBLE].overflow()) {
1822 assm->DataAlign(kDoubleSize);
1823 EmitGroup(assm, ConstantPoolEntry::OVERFLOWED,
1824 ConstantPoolEntry::DOUBLE);
1825 }
1826 if (info_[ConstantPoolEntry::INTPTR].overflow()) {
1827 EmitGroup(assm, ConstantPoolEntry::OVERFLOWED,
1828 ConstantPoolEntry::INTPTR);
1829 }
1830 }
1831 }
1832
1833 return !empty ? emitted_label_.pos() : 0;
1834}
1835
1836
1837// Platform specific but identical code for all the platforms.
1838
1839
1840void Assembler::RecordDeoptReason(const int reason,
1841 const SourcePosition position) {
1842 if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling()) {
1843 EnsureSpace ensure_space(this);
1844 int raw_position = position.IsUnknown() ? 0 : position.raw();
1845 RecordRelocInfo(RelocInfo::POSITION, raw_position);
1846 RecordRelocInfo(RelocInfo::DEOPT_REASON, reason);
1847 }
1848}
1849
1850
1851void Assembler::RecordComment(const char* msg) {
1852 if (FLAG_code_comments) {
1853 EnsureSpace ensure_space(this);
1854 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
1855 }
1856}
1857
1858
1859void Assembler::RecordGeneratorContinuation() {
1860 EnsureSpace ensure_space(this);
1861 RecordRelocInfo(RelocInfo::GENERATOR_CONTINUATION);
1862}
1863
1864
1865void Assembler::RecordDebugBreakSlot(RelocInfo::Mode mode) {
1866 EnsureSpace ensure_space(this);
1867 DCHECK(RelocInfo::IsDebugBreakSlot(mode));
1868 RecordRelocInfo(mode);
1869}
1870
1871
1872void Assembler::DataAlign(int m) {
1873 DCHECK(m >= 2 && base::bits::IsPowerOfTwo32(m));
1874 while ((pc_offset() & (m - 1)) != 0) {
1875 db(0);
1876 }
1877}
1878} // namespace internal
1879} // namespace v8