blob: 5c8c2ce16d97cf646fd774a4b8c1bcc960c75702 [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 Murdoch097c5b22016-05-18 11:27:45 +010037#include <math.h>
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038#include <cmath>
39#include "src/api.h"
40#include "src/base/cpu.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040041#include "src/base/functional.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042#include "src/base/lazy-instance.h"
43#include "src/base/platform/platform.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044#include "src/base/utils/random-number-generator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000045#include "src/builtins.h"
46#include "src/codegen.h"
47#include "src/counters.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000048#include "src/debug/debug.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000049#include "src/deoptimizer.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000050#include "src/disassembler.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000051#include "src/execution.h"
52#include "src/ic/ic.h"
53#include "src/ic/stub-cache.h"
Ben Murdoch097c5b22016-05-18 11:27:45 +010054#include "src/interpreter/interpreter.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000055#include "src/ostreams.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000056#include "src/profiler/cpu-profiler.h"
57#include "src/regexp/jsregexp.h"
58#include "src/regexp/regexp-macro-assembler.h"
59#include "src/regexp/regexp-stack.h"
60#include "src/register-configuration.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040061#include "src/runtime/runtime.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000062#include "src/simulator.h" // For flushing instruction cache.
63#include "src/snapshot/serialize.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010064
65#if V8_TARGET_ARCH_IA32
Ben Murdochb8a8cc12014-11-26 15:28:44 +000066#include "src/ia32/assembler-ia32-inl.h" // NOLINT
Ben Murdoch3ef787d2012-04-12 10:51:47 +010067#elif V8_TARGET_ARCH_X64
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068#include "src/x64/assembler-x64-inl.h" // NOLINT
69#elif V8_TARGET_ARCH_ARM64
70#include "src/arm64/assembler-arm64-inl.h" // NOLINT
Ben Murdoch3ef787d2012-04-12 10:51:47 +010071#elif V8_TARGET_ARCH_ARM
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072#include "src/arm/assembler-arm-inl.h" // NOLINT
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000073#elif V8_TARGET_ARCH_PPC
74#include "src/ppc/assembler-ppc-inl.h" // NOLINT
Ben Murdoch3ef787d2012-04-12 10:51:47 +010075#elif V8_TARGET_ARCH_MIPS
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076#include "src/mips/assembler-mips-inl.h" // NOLINT
77#elif V8_TARGET_ARCH_MIPS64
78#include "src/mips64/assembler-mips64-inl.h" // NOLINT
79#elif V8_TARGET_ARCH_X87
80#include "src/x87/assembler-x87-inl.h" // NOLINT
Ben Murdoch3ef787d2012-04-12 10:51:47 +010081#else
82#error "Unknown architecture."
83#endif
84
Steve Blocka7e24c12009-10-30 11:49:00 +000085// Include native regexp-macro-assembler.
Steve Block6ded16b2010-05-10 14:33:55 +010086#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +000087#if V8_TARGET_ARCH_IA32
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000088#include "src/regexp/ia32/regexp-macro-assembler-ia32.h" // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +000089#elif V8_TARGET_ARCH_X64
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000090#include "src/regexp/x64/regexp-macro-assembler-x64.h" // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +000091#elif V8_TARGET_ARCH_ARM64
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092#include "src/regexp/arm64/regexp-macro-assembler-arm64.h" // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +000093#elif V8_TARGET_ARCH_ARM
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094#include "src/regexp/arm/regexp-macro-assembler-arm.h" // NOLINT
95#elif V8_TARGET_ARCH_PPC
96#include "src/regexp/ppc/regexp-macro-assembler-ppc.h" // NOLINT
Steve Block44f0eee2011-05-26 01:26:41 +010097#elif V8_TARGET_ARCH_MIPS
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000098#include "src/regexp/mips/regexp-macro-assembler-mips.h" // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099#elif V8_TARGET_ARCH_MIPS64
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000100#include "src/regexp/mips64/regexp-macro-assembler-mips64.h" // NOLINT
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101#elif V8_TARGET_ARCH_X87
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000102#include "src/regexp/x87/regexp-macro-assembler-x87.h" // NOLINT
Steve Blocka7e24c12009-10-30 11:49:00 +0000103#else // Unknown architecture.
104#error "Unknown architecture."
105#endif // Target architecture.
Steve Block6ded16b2010-05-10 14:33:55 +0100106#endif // V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +0000107
108namespace v8 {
109namespace internal {
110
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100111// -----------------------------------------------------------------------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112// Common register code.
113
114const char* Register::ToString() {
115 // This is the mapping of allocation indices to registers.
116 DCHECK(reg_code >= 0 && reg_code < kNumRegisters);
117 return RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT)
118 ->GetGeneralRegisterName(reg_code);
119}
120
121
122bool Register::IsAllocatable() const {
123 return ((1 << reg_code) &
124 RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT)
125 ->allocatable_general_codes_mask()) != 0;
126}
127
128
129const char* DoubleRegister::ToString() {
130 // This is the mapping of allocation indices to registers.
131 DCHECK(reg_code >= 0 && reg_code < kMaxNumRegisters);
132 return RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT)
133 ->GetDoubleRegisterName(reg_code);
134}
135
136
137bool DoubleRegister::IsAllocatable() const {
138 return ((1 << reg_code) &
139 RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT)
140 ->allocatable_double_codes_mask()) != 0;
141}
142
143
144// -----------------------------------------------------------------------------
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100145// Common double constants.
Steve Blocka7e24c12009-10-30 11:49:00 +0000146
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100147struct DoubleConstant BASE_EMBEDDED {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148double min_int;
149double one_half;
150double minus_one_half;
151double negative_infinity;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152double the_hole_nan;
153double uint32_bias;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100154};
155
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156static DoubleConstant double_constants;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100157
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000158const char* const RelocInfo::kFillerCommentString = "DEOPTIMIZATION PADDING";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100159
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160static bool math_exp_data_initialized = false;
161static base::Mutex* math_exp_data_mutex = NULL;
162static double* math_exp_constants_array = NULL;
163static double* math_exp_log_table_array = NULL;
164
Steve Blocka7e24c12009-10-30 11:49:00 +0000165// -----------------------------------------------------------------------------
Steve Block053d10c2011-06-13 19:13:29 +0100166// Implementation of AssemblerBase
167
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168AssemblerBase::AssemblerBase(Isolate* isolate, void* buffer, int buffer_size)
Steve Block053d10c2011-06-13 19:13:29 +0100169 : isolate_(isolate),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170 jit_cookie_(0),
171 enabled_cpu_features_(0),
172 emit_debug_code_(FLAG_debug_code),
173 predictable_code_size_(false),
174 // We may use the assembler without an isolate.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400175 serializer_enabled_(isolate && isolate->serializer_enabled()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000176 constant_pool_available_(false) {
177 DCHECK_NOT_NULL(isolate);
178 if (FLAG_mask_constants_with_cookie) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 jit_cookie_ = isolate->random_number_generator()->NextInt();
Steve Block053d10c2011-06-13 19:13:29 +0100180 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 own_buffer_ = buffer == NULL;
182 if (buffer_size == 0) buffer_size = kMinimalBufferSize;
183 DCHECK(buffer_size > 0);
184 if (own_buffer_) buffer = NewArray<byte>(buffer_size);
185 buffer_ = static_cast<byte*>(buffer);
186 buffer_size_ = buffer_size;
187
188 pc_ = buffer_;
Steve Block053d10c2011-06-13 19:13:29 +0100189}
190
191
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192AssemblerBase::~AssemblerBase() {
193 if (own_buffer_) DeleteArray(buffer_);
194}
195
196
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000197void AssemblerBase::FlushICache(Isolate* isolate, void* start, size_t size) {
198 if (size == 0) return;
199 if (CpuFeatures::IsSupported(COHERENT_CACHE)) return;
200
201#if defined(USE_SIMULATOR)
202 Simulator::FlushICache(isolate->simulator_i_cache(), start, size);
203#else
204 CpuFeatures::FlushICache(start, size);
205#endif // USE_SIMULATOR
206}
207
208
209void AssemblerBase::Print() {
210 OFStream os(stdout);
211 v8::internal::Disassembler::Decode(isolate(), &os, buffer_, pc_, nullptr);
212}
213
214
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215// -----------------------------------------------------------------------------
216// Implementation of PredictableCodeSizeScope
217
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000218PredictableCodeSizeScope::PredictableCodeSizeScope(AssemblerBase* assembler)
219 : PredictableCodeSizeScope(assembler, -1) {}
220
221
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000222PredictableCodeSizeScope::PredictableCodeSizeScope(AssemblerBase* assembler,
223 int expected_size)
224 : assembler_(assembler),
225 expected_size_(expected_size),
226 start_offset_(assembler->pc_offset()),
227 old_value_(assembler->predictable_code_size()) {
228 assembler_->set_predictable_code_size(true);
229}
230
231
232PredictableCodeSizeScope::~PredictableCodeSizeScope() {
233 // TODO(svenpanne) Remove the 'if' when everything works.
234 if (expected_size_ >= 0) {
235 CHECK_EQ(expected_size_, assembler_->pc_offset() - start_offset_);
236 }
237 assembler_->set_predictable_code_size(old_value_);
238}
239
240
241// -----------------------------------------------------------------------------
242// Implementation of CpuFeatureScope
243
244#ifdef DEBUG
245CpuFeatureScope::CpuFeatureScope(AssemblerBase* assembler, CpuFeature f)
246 : assembler_(assembler) {
247 DCHECK(CpuFeatures::IsSupported(f));
248 old_enabled_ = assembler_->enabled_cpu_features();
249 uint64_t mask = static_cast<uint64_t>(1) << f;
250 // TODO(svenpanne) This special case below doesn't belong here!
251#if V8_TARGET_ARCH_ARM
252 // ARMv7 is implied by VFP3.
253 if (f == VFP3) {
254 mask |= static_cast<uint64_t>(1) << ARMv7;
255 }
256#endif
257 assembler_->set_enabled_cpu_features(old_enabled_ | mask);
258}
259
260
261CpuFeatureScope::~CpuFeatureScope() {
262 assembler_->set_enabled_cpu_features(old_enabled_);
263}
264#endif
265
266
267bool CpuFeatures::initialized_ = false;
268unsigned CpuFeatures::supported_ = 0;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100269unsigned CpuFeatures::icache_line_size_ = 0;
270unsigned CpuFeatures::dcache_line_size_ = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000271
Steve Block053d10c2011-06-13 19:13:29 +0100272// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000273// Implementation of Label
274
275int Label::pos() const {
276 if (pos_ < 0) return -pos_ - 1;
277 if (pos_ > 0) return pos_ - 1;
278 UNREACHABLE();
279 return 0;
280}
281
282
283// -----------------------------------------------------------------------------
284// Implementation of RelocInfoWriter and RelocIterator
285//
Ben Murdoch257744e2011-11-30 15:57:28 +0000286// Relocation information is written backwards in memory, from high addresses
287// towards low addresses, byte by byte. Therefore, in the encodings listed
288// below, the first byte listed it at the highest address, and successive
289// bytes in the record are at progressively lower addresses.
290//
Steve Blocka7e24c12009-10-30 11:49:00 +0000291// Encoding
292//
293// The most common modes are given single-byte encodings. Also, it is
294// easy to identify the type of reloc info and skip unwanted modes in
295// an iteration.
296//
Ben Murdoch257744e2011-11-30 15:57:28 +0000297// The encoding relies on the fact that there are fewer than 14
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298// different relocation modes using standard non-compact encoding.
Steve Blocka7e24c12009-10-30 11:49:00 +0000299//
Ben Murdoch257744e2011-11-30 15:57:28 +0000300// The first byte of a relocation record has a tag in its low 2 bits:
301// Here are the record schemes, depending on the low tag and optional higher
302// tags.
Steve Blocka7e24c12009-10-30 11:49:00 +0000303//
Ben Murdoch257744e2011-11-30 15:57:28 +0000304// Low tag:
305// 00: embedded_object: [6-bit pc delta] 00
Steve Blocka7e24c12009-10-30 11:49:00 +0000306//
Ben Murdoch257744e2011-11-30 15:57:28 +0000307// 01: code_target: [6-bit pc delta] 01
Steve Blocka7e24c12009-10-30 11:49:00 +0000308//
Ben Murdoch257744e2011-11-30 15:57:28 +0000309// 10: short_data_record: [6-bit pc delta] 10 followed by
310// [6-bit data delta] [2-bit data type tag]
Steve Blocka7e24c12009-10-30 11:49:00 +0000311//
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000312// 11: long_record [6 bit reloc mode] 11
313// followed by pc delta
314// followed by optional data depending on type.
Steve Blocka7e24c12009-10-30 11:49:00 +0000315//
Ben Murdoch257744e2011-11-30 15:57:28 +0000316// 2-bit data type tags, used in short_data_record and data_jump long_record:
317// code_target_with_id: 00
318// position: 01
319// statement_position: 10
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000320// deopt_reason: 11
Steve Blocka7e24c12009-10-30 11:49:00 +0000321//
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000322// If a pc delta exceeds 6 bits, it is split into a remainder that fits into
323// 6 bits and a part that does not. The latter is encoded as a long record
324// with PC_JUMP as pseudo reloc info mode. The former is encoded as part of
325// the following record in the usual way. The long pc jump record has variable
326// length:
327// pc-jump: [PC_JUMP] 11
Ben Murdoch257744e2011-11-30 15:57:28 +0000328// [7 bits data] 0
329// ...
330// [7 bits data] 1
331// (Bits 6..31 of pc delta, with leading zeroes
332// dropped, and last non-zero chunk tagged with 1.)
333
Steve Blocka7e24c12009-10-30 11:49:00 +0000334const int kTagBits = 2;
335const int kTagMask = (1 << kTagBits) - 1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000336const int kLongTagBits = 6;
337const int kShortDataTypeTagBits = 2;
338const int kShortDataBits = kBitsPerByte - kShortDataTypeTagBits;
Steve Blocka7e24c12009-10-30 11:49:00 +0000339
340const int kEmbeddedObjectTag = 0;
341const int kCodeTargetTag = 1;
Ben Murdoch257744e2011-11-30 15:57:28 +0000342const int kLocatableTag = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +0000343const int kDefaultTag = 3;
344
Steve Blocka7e24c12009-10-30 11:49:00 +0000345const int kSmallPCDeltaBits = kBitsPerByte - kTagBits;
346const int kSmallPCDeltaMask = (1 << kSmallPCDeltaBits) - 1;
Steve Block44f0eee2011-05-26 01:26:41 +0100347const int RelocInfo::kMaxSmallPCDelta = kSmallPCDeltaMask;
Steve Blocka7e24c12009-10-30 11:49:00 +0000348
Steve Blocka7e24c12009-10-30 11:49:00 +0000349const int kChunkBits = 7;
350const int kChunkMask = (1 << kChunkBits) - 1;
351const int kLastChunkTagBits = 1;
352const int kLastChunkTagMask = 1;
353const int kLastChunkTag = 1;
354
Ben Murdoch257744e2011-11-30 15:57:28 +0000355const int kCodeWithIdTag = 0;
356const int kNonstatementPositionTag = 1;
357const int kStatementPositionTag = 2;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000358const int kDeoptReasonTag = 3;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000359
Steve Blocka7e24c12009-10-30 11:49:00 +0000360
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000361uint32_t RelocInfoWriter::WriteLongPCJump(uint32_t pc_delta) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000362 // Return if the pc_delta can fit in kSmallPCDeltaBits bits.
363 // Otherwise write a variable length PC jump for the bits that do
364 // not fit in the kSmallPCDeltaBits bits.
365 if (is_uintn(pc_delta, kSmallPCDeltaBits)) return pc_delta;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000366 WriteMode(RelocInfo::PC_JUMP);
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 uint32_t pc_jump = pc_delta >> kSmallPCDeltaBits;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000368 DCHECK(pc_jump > 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000369 // Write kChunkBits size chunks of the pc_jump.
370 for (; pc_jump > 0; pc_jump = pc_jump >> kChunkBits) {
371 byte b = pc_jump & kChunkMask;
372 *--pos_ = b << kLastChunkTagBits;
373 }
374 // Tag the last chunk so it can be identified.
375 *pos_ = *pos_ | kLastChunkTag;
376 // Return the remaining kSmallPCDeltaBits of the pc_delta.
377 return pc_delta & kSmallPCDeltaMask;
378}
379
380
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000381void RelocInfoWriter::WriteShortTaggedPC(uint32_t pc_delta, int tag) {
382 // Write a byte of tagged pc-delta, possibly preceded by an explicit pc-jump.
383 pc_delta = WriteLongPCJump(pc_delta);
Steve Blocka7e24c12009-10-30 11:49:00 +0000384 *--pos_ = pc_delta << kTagBits | tag;
385}
386
387
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000388void RelocInfoWriter::WriteShortTaggedData(intptr_t data_delta, int tag) {
389 *--pos_ = static_cast<byte>(data_delta << kShortDataTypeTagBits | tag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000390}
391
392
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000393void RelocInfoWriter::WriteMode(RelocInfo::Mode rmode) {
394 STATIC_ASSERT(RelocInfo::NUMBER_OF_MODES <= (1 << kLongTagBits));
395 *--pos_ = static_cast<int>((rmode << kTagBits) | kDefaultTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000396}
397
398
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000399void RelocInfoWriter::WriteModeAndPC(uint32_t pc_delta, RelocInfo::Mode rmode) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000400 // Write two-byte tagged pc-delta, possibly preceded by var. length pc-jump.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000401 pc_delta = WriteLongPCJump(pc_delta);
402 WriteMode(rmode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000403 *--pos_ = pc_delta;
404}
405
406
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000407void RelocInfoWriter::WriteIntData(int number) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000408 for (int i = 0; i < kIntSize; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000409 *--pos_ = static_cast<byte>(number);
Ben Murdoch257744e2011-11-30 15:57:28 +0000410 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000411 number = number >> kBitsPerByte;
Ben Murdoch257744e2011-11-30 15:57:28 +0000412 }
413}
414
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000415
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000416void RelocInfoWriter::WriteData(intptr_t data_delta) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000417 for (int i = 0; i < kIntptrSize; i++) {
Steve Blockd0582a62009-12-15 09:54:21 +0000418 *--pos_ = static_cast<byte>(data_delta);
Ben Murdoch257744e2011-11-30 15:57:28 +0000419 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
Steve Blocka7e24c12009-10-30 11:49:00 +0000420 data_delta = data_delta >> kBitsPerByte;
421 }
422}
423
424
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000425void RelocInfoWriter::WritePosition(int pc_delta, int pos_delta,
426 RelocInfo::Mode rmode) {
427 int pos_type_tag = (rmode == RelocInfo::POSITION) ? kNonstatementPositionTag
428 : kStatementPositionTag;
429 // Check if delta is small enough to fit in a tagged byte.
430 if (is_intn(pos_delta, kShortDataBits)) {
431 WriteShortTaggedPC(pc_delta, kLocatableTag);
432 WriteShortTaggedData(pos_delta, pos_type_tag);
433 } else {
434 // Otherwise, use costly encoding.
435 WriteModeAndPC(pc_delta, rmode);
436 WriteIntData(pos_delta);
437 }
438}
439
440
441void RelocInfoWriter::FlushPosition() {
442 if (!next_position_candidate_flushed_) {
443 WritePosition(next_position_candidate_pc_delta_,
444 next_position_candidate_pos_delta_, RelocInfo::POSITION);
445 next_position_candidate_pos_delta_ = 0;
446 next_position_candidate_pc_delta_ = 0;
447 next_position_candidate_flushed_ = true;
448 }
449}
450
451
Steve Blocka7e24c12009-10-30 11:49:00 +0000452void RelocInfoWriter::Write(const RelocInfo* rinfo) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000453 RelocInfo::Mode rmode = rinfo->rmode();
454 if (rmode != RelocInfo::POSITION) {
455 FlushPosition();
456 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000457#ifdef DEBUG
458 byte* begin_pos = pos_;
459#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000460 DCHECK(rinfo->rmode() < RelocInfo::NUMBER_OF_MODES);
461 DCHECK(rinfo->pc() - last_pc_ >= 0);
Steve Blocka7e24c12009-10-30 11:49:00 +0000462 // Use unsigned delta-encoding for pc.
Steve Blockd0582a62009-12-15 09:54:21 +0000463 uint32_t pc_delta = static_cast<uint32_t>(rinfo->pc() - last_pc_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000464
465 // The two most common modes are given small tags, and usually fit in a byte.
466 if (rmode == RelocInfo::EMBEDDED_OBJECT) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000467 WriteShortTaggedPC(pc_delta, kEmbeddedObjectTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000468 } else if (rmode == RelocInfo::CODE_TARGET) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000469 WriteShortTaggedPC(pc_delta, kCodeTargetTag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000470 DCHECK(begin_pos - pos_ <= RelocInfo::kMaxCallSize);
Ben Murdoch257744e2011-11-30 15:57:28 +0000471 } else if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
472 // Use signed delta-encoding for id.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000473 DCHECK_EQ(static_cast<int>(rinfo->data()), rinfo->data());
Ben Murdoch257744e2011-11-30 15:57:28 +0000474 int id_delta = static_cast<int>(rinfo->data()) - last_id_;
475 // Check if delta is small enough to fit in a tagged byte.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000476 if (is_intn(id_delta, kShortDataBits)) {
477 WriteShortTaggedPC(pc_delta, kLocatableTag);
478 WriteShortTaggedData(id_delta, kCodeWithIdTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000479 } else {
480 // Otherwise, use costly encoding.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000481 WriteModeAndPC(pc_delta, rmode);
482 WriteIntData(id_delta);
Steve Blocka7e24c12009-10-30 11:49:00 +0000483 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000484 last_id_ = static_cast<int>(rinfo->data());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000485 } else if (rmode == RelocInfo::DEOPT_REASON) {
486 DCHECK(rinfo->data() < (1 << kShortDataBits));
487 WriteShortTaggedPC(pc_delta, kLocatableTag);
488 WriteShortTaggedData(rinfo->data(), kDeoptReasonTag);
Ben Murdoch257744e2011-11-30 15:57:28 +0000489 } else if (RelocInfo::IsPosition(rmode)) {
490 // Use signed delta-encoding for position.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000491 DCHECK_EQ(static_cast<int>(rinfo->data()), rinfo->data());
Ben Murdoch257744e2011-11-30 15:57:28 +0000492 int pos_delta = static_cast<int>(rinfo->data()) - last_position_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000493 if (rmode == RelocInfo::STATEMENT_POSITION) {
494 WritePosition(pc_delta, pos_delta, rmode);
Ben Murdoch257744e2011-11-30 15:57:28 +0000495 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000496 DCHECK_EQ(rmode, RelocInfo::POSITION);
497 if (pc_delta != 0 || last_mode_ != RelocInfo::POSITION) {
498 FlushPosition();
499 next_position_candidate_pc_delta_ = pc_delta;
500 next_position_candidate_pos_delta_ = pos_delta;
501 } else {
502 next_position_candidate_pos_delta_ += pos_delta;
503 }
504 next_position_candidate_flushed_ = false;
Ben Murdoch257744e2011-11-30 15:57:28 +0000505 }
506 last_position_ = static_cast<int>(rinfo->data());
Steve Blocka7e24c12009-10-30 11:49:00 +0000507 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000508 WriteModeAndPC(pc_delta, rmode);
509 if (RelocInfo::IsComment(rmode)) {
510 WriteData(rinfo->data());
511 } else if (RelocInfo::IsConstPool(rmode) ||
512 RelocInfo::IsVeneerPool(rmode)) {
513 WriteIntData(static_cast<int>(rinfo->data()));
514 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000515 }
516 last_pc_ = rinfo->pc();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000517 last_mode_ = rmode;
Steve Blocka7e24c12009-10-30 11:49:00 +0000518#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000519 DCHECK(begin_pos - pos_ <= kMaxSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000520#endif
521}
522
523
524inline int RelocIterator::AdvanceGetTag() {
525 return *--pos_ & kTagMask;
526}
527
528
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000529inline RelocInfo::Mode RelocIterator::GetMode() {
530 return static_cast<RelocInfo::Mode>((*pos_ >> kTagBits) &
531 ((1 << kLongTagBits) - 1));
Steve Blocka7e24c12009-10-30 11:49:00 +0000532}
533
534
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000535inline void RelocIterator::ReadShortTaggedPC() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 rinfo_.pc_ += *pos_ >> kTagBits;
537}
538
539
540inline void RelocIterator::AdvanceReadPC() {
541 rinfo_.pc_ += *--pos_;
542}
543
544
Ben Murdoch257744e2011-11-30 15:57:28 +0000545void RelocIterator::AdvanceReadId() {
546 int x = 0;
547 for (int i = 0; i < kIntSize; i++) {
548 x |= static_cast<int>(*--pos_) << i * kBitsPerByte;
549 }
550 last_id_ += x;
551 rinfo_.data_ = last_id_;
552}
553
554
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000555void RelocIterator::AdvanceReadInt() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000556 int x = 0;
557 for (int i = 0; i < kIntSize; i++) {
558 x |= static_cast<int>(*--pos_) << i * kBitsPerByte;
559 }
560 rinfo_.data_ = x;
561}
562
563
Ben Murdoch257744e2011-11-30 15:57:28 +0000564void RelocIterator::AdvanceReadPosition() {
565 int x = 0;
566 for (int i = 0; i < kIntSize; i++) {
567 x |= static_cast<int>(*--pos_) << i * kBitsPerByte;
568 }
569 last_position_ += x;
570 rinfo_.data_ = last_position_;
571}
572
573
Steve Blocka7e24c12009-10-30 11:49:00 +0000574void RelocIterator::AdvanceReadData() {
575 intptr_t x = 0;
576 for (int i = 0; i < kIntptrSize; i++) {
577 x |= static_cast<intptr_t>(*--pos_) << i * kBitsPerByte;
578 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000579 rinfo_.data_ = x;
Steve Blocka7e24c12009-10-30 11:49:00 +0000580}
581
582
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000583void RelocIterator::AdvanceReadLongPCJump() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000584 // Read the 32-kSmallPCDeltaBits most significant bits of the
585 // pc jump in kChunkBits bit chunks and shift them into place.
586 // Stop when the last chunk is encountered.
587 uint32_t pc_jump = 0;
588 for (int i = 0; i < kIntSize; i++) {
589 byte pc_jump_part = *--pos_;
590 pc_jump |= (pc_jump_part >> kLastChunkTagBits) << i * kChunkBits;
591 if ((pc_jump_part & kLastChunkTagMask) == 1) break;
592 }
593 // The least significant kSmallPCDeltaBits bits will be added
594 // later.
595 rinfo_.pc_ += pc_jump << kSmallPCDeltaBits;
596}
597
598
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000599inline int RelocIterator::GetShortDataTypeTag() {
600 return *pos_ & ((1 << kShortDataTypeTagBits) - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000601}
602
603
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000604inline void RelocIterator::ReadShortTaggedId() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000605 int8_t signed_b = *pos_;
606 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000607 last_id_ += signed_b >> kShortDataTypeTagBits;
Ben Murdoch257744e2011-11-30 15:57:28 +0000608 rinfo_.data_ = last_id_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000609}
610
611
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000612inline void RelocIterator::ReadShortTaggedPosition() {
Ben Murdoch257744e2011-11-30 15:57:28 +0000613 int8_t signed_b = *pos_;
614 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000615 last_position_ += signed_b >> kShortDataTypeTagBits;
Ben Murdoch257744e2011-11-30 15:57:28 +0000616 rinfo_.data_ = last_position_;
617}
618
619
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000620inline void RelocIterator::ReadShortTaggedData() {
621 uint8_t unsigned_b = *pos_;
622 rinfo_.data_ = unsigned_b >> kTagBits;
623}
624
625
Ben Murdoch257744e2011-11-30 15:57:28 +0000626static inline RelocInfo::Mode GetPositionModeFromTag(int tag) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000627 DCHECK(tag == kNonstatementPositionTag ||
Ben Murdoch257744e2011-11-30 15:57:28 +0000628 tag == kStatementPositionTag);
629 return (tag == kNonstatementPositionTag) ?
630 RelocInfo::POSITION :
631 RelocInfo::STATEMENT_POSITION;
Steve Blocka7e24c12009-10-30 11:49:00 +0000632}
633
634
635void RelocIterator::next() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000636 DCHECK(!done());
Steve Blocka7e24c12009-10-30 11:49:00 +0000637 // Basically, do the opposite of RelocInfoWriter::Write.
638 // Reading of data is as far as possible avoided for unwanted modes,
639 // but we must always update the pc.
640 //
641 // We exit this loop by returning when we find a mode we want.
642 while (pos_ > end_) {
643 int tag = AdvanceGetTag();
644 if (tag == kEmbeddedObjectTag) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000645 ReadShortTaggedPC();
Steve Blocka7e24c12009-10-30 11:49:00 +0000646 if (SetMode(RelocInfo::EMBEDDED_OBJECT)) return;
647 } else if (tag == kCodeTargetTag) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000648 ReadShortTaggedPC();
Steve Blocka7e24c12009-10-30 11:49:00 +0000649 if (SetMode(RelocInfo::CODE_TARGET)) return;
Ben Murdoch257744e2011-11-30 15:57:28 +0000650 } else if (tag == kLocatableTag) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000651 ReadShortTaggedPC();
Steve Blocka7e24c12009-10-30 11:49:00 +0000652 Advance();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000653 int data_type_tag = GetShortDataTypeTag();
654 if (data_type_tag == kCodeWithIdTag) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000655 if (SetMode(RelocInfo::CODE_TARGET_WITH_ID)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000656 ReadShortTaggedId();
657 return;
658 }
659 } else if (data_type_tag == kDeoptReasonTag) {
660 if (SetMode(RelocInfo::DEOPT_REASON)) {
661 ReadShortTaggedData();
Ben Murdoch257744e2011-11-30 15:57:28 +0000662 return;
663 }
664 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000665 DCHECK(data_type_tag == kNonstatementPositionTag ||
666 data_type_tag == kStatementPositionTag);
Ben Murdoch257744e2011-11-30 15:57:28 +0000667 if (mode_mask_ & RelocInfo::kPositionMask) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000668 // Always update the position if we are interested in either
669 // statement positions or non-statement positions.
670 ReadShortTaggedPosition();
671 if (SetMode(GetPositionModeFromTag(data_type_tag))) return;
Ben Murdoch257744e2011-11-30 15:57:28 +0000672 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000673 }
674 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000675 DCHECK(tag == kDefaultTag);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000676 RelocInfo::Mode rmode = GetMode();
677 if (rmode == RelocInfo::PC_JUMP) {
678 AdvanceReadLongPCJump();
679 } else {
680 AdvanceReadPC();
681 if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
682 if (SetMode(rmode)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000683 AdvanceReadId();
684 return;
685 }
686 Advance(kIntSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000687 } else if (RelocInfo::IsComment(rmode)) {
688 if (SetMode(rmode)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000689 AdvanceReadData();
690 return;
691 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000692 Advance(kIntptrSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000693 } else if (RelocInfo::IsPosition(rmode)) {
694 if (mode_mask_ & RelocInfo::kPositionMask) {
695 // Always update the position if we are interested in either
696 // statement positions or non-statement positions.
697 AdvanceReadPosition();
698 if (SetMode(rmode)) return;
699 } else {
700 Advance(kIntSize);
701 }
702 } else if (RelocInfo::IsConstPool(rmode) ||
703 RelocInfo::IsVeneerPool(rmode)) {
704 if (SetMode(rmode)) {
705 AdvanceReadInt();
706 return;
707 }
708 Advance(kIntSize);
709 } else if (SetMode(static_cast<RelocInfo::Mode>(rmode))) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000710 return;
711 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000712 }
713 }
714 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000715 if (code_age_sequence_ != NULL) {
716 byte* old_code_age_sequence = code_age_sequence_;
717 code_age_sequence_ = NULL;
718 if (SetMode(RelocInfo::CODE_AGE_SEQUENCE)) {
719 rinfo_.data_ = 0;
720 rinfo_.pc_ = old_code_age_sequence;
721 return;
722 }
723 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000724 done_ = true;
725}
726
727
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000728RelocIterator::RelocIterator(Code* code, int mode_mask)
729 : rinfo_(code->map()->GetIsolate()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100730 rinfo_.host_ = code;
Steve Blocka7e24c12009-10-30 11:49:00 +0000731 rinfo_.pc_ = code->instruction_start();
732 rinfo_.data_ = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100733 // Relocation info is read backwards.
Steve Blocka7e24c12009-10-30 11:49:00 +0000734 pos_ = code->relocation_start() + code->relocation_size();
735 end_ = code->relocation_start();
736 done_ = false;
737 mode_mask_ = mode_mask;
Ben Murdoch257744e2011-11-30 15:57:28 +0000738 last_id_ = 0;
739 last_position_ = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000740 byte* sequence = code->FindCodeAgeSequence();
741 // We get the isolate from the map, because at serialization time
742 // the code pointer has been cloned and isn't really in heap space.
743 Isolate* isolate = code->map()->GetIsolate();
744 if (sequence != NULL && !Code::IsYoungSequence(isolate, sequence)) {
745 code_age_sequence_ = sequence;
746 } else {
747 code_age_sequence_ = NULL;
748 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000749 if (mode_mask_ == 0) pos_ = end_;
750 next();
751}
752
753
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000754RelocIterator::RelocIterator(const CodeDesc& desc, int mode_mask)
755 : rinfo_(desc.origin->isolate()) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000756 rinfo_.pc_ = desc.buffer;
757 rinfo_.data_ = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100758 // Relocation info is read backwards.
Steve Blocka7e24c12009-10-30 11:49:00 +0000759 pos_ = desc.buffer + desc.buffer_size;
760 end_ = pos_ - desc.reloc_size;
761 done_ = false;
762 mode_mask_ = mode_mask;
Ben Murdoch257744e2011-11-30 15:57:28 +0000763 last_id_ = 0;
764 last_position_ = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000765 code_age_sequence_ = NULL;
Steve Blocka7e24c12009-10-30 11:49:00 +0000766 if (mode_mask_ == 0) pos_ = end_;
767 next();
768}
769
770
771// -----------------------------------------------------------------------------
772// Implementation of RelocInfo
773
Ben Murdoch097c5b22016-05-18 11:27:45 +0100774bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
775 return DebugCodegen::DebugBreakSlotIsPatched(pc_);
776}
Steve Blocka7e24c12009-10-30 11:49:00 +0000777
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000778#ifdef DEBUG
779bool RelocInfo::RequiresRelocation(const CodeDesc& desc) {
780 // Ensure there are no code targets or embedded objects present in the
781 // deoptimization entries, they would require relocation after code
782 // generation.
783 int mode_mask = RelocInfo::kCodeTargetMask |
784 RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
785 RelocInfo::ModeMask(RelocInfo::CELL) |
786 RelocInfo::kApplyMask;
787 RelocIterator it(desc, mode_mask);
788 return !it.done();
789}
790#endif
791
792
Steve Blocka7e24c12009-10-30 11:49:00 +0000793#ifdef ENABLE_DISASSEMBLER
794const char* RelocInfo::RelocModeName(RelocInfo::Mode rmode) {
795 switch (rmode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000796 case NONE32:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000797 return "no reloc 32";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000798 case NONE64:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000799 return "no reloc 64";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000800 case EMBEDDED_OBJECT:
Steve Blocka7e24c12009-10-30 11:49:00 +0000801 return "embedded object";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000802 case DEBUGGER_STATEMENT:
803 return "debugger statement";
804 case CODE_TARGET:
Steve Blocka7e24c12009-10-30 11:49:00 +0000805 return "code target";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000806 case CODE_TARGET_WITH_ID:
Ben Murdoch257744e2011-11-30 15:57:28 +0000807 return "code target with id";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000808 case CELL:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000809 return "property cell";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000810 case RUNTIME_ENTRY:
Steve Blocka7e24c12009-10-30 11:49:00 +0000811 return "runtime entry";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000812 case COMMENT:
Steve Blocka7e24c12009-10-30 11:49:00 +0000813 return "comment";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000814 case POSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +0000815 return "position";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000816 case STATEMENT_POSITION:
Steve Blocka7e24c12009-10-30 11:49:00 +0000817 return "statement position";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000818 case EXTERNAL_REFERENCE:
Steve Blocka7e24c12009-10-30 11:49:00 +0000819 return "external reference";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000820 case INTERNAL_REFERENCE:
Steve Blocka7e24c12009-10-30 11:49:00 +0000821 return "internal reference";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000822 case INTERNAL_REFERENCE_ENCODED:
823 return "encoded internal reference";
824 case DEOPT_REASON:
825 return "deopt reason";
826 case CONST_POOL:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000827 return "constant pool";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000828 case VENEER_POOL:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000829 return "veneer pool";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000830 case DEBUG_BREAK_SLOT_AT_POSITION:
831 return "debug break slot at position";
832 case DEBUG_BREAK_SLOT_AT_RETURN:
833 return "debug break slot at return";
834 case DEBUG_BREAK_SLOT_AT_CALL:
835 return "debug break slot at call";
836 case CODE_AGE_SEQUENCE:
837 return "code age sequence";
838 case GENERATOR_CONTINUATION:
839 return "generator continuation";
840 case NUMBER_OF_MODES:
841 case PC_JUMP:
Steve Blocka7e24c12009-10-30 11:49:00 +0000842 UNREACHABLE();
843 return "number_of_modes";
844 }
845 return "unknown relocation type";
846}
847
848
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400849void RelocInfo::Print(Isolate* isolate, std::ostream& os) { // NOLINT
850 os << static_cast<const void*>(pc_) << " " << RelocModeName(rmode_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000851 if (IsComment(rmode_)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000852 os << " (" << reinterpret_cast<char*>(data_) << ")";
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000853 } else if (rmode_ == DEOPT_REASON) {
854 os << " (" << Deoptimizer::GetDeoptReason(
855 static_cast<Deoptimizer::DeoptReason>(data_)) << ")";
Steve Blocka7e24c12009-10-30 11:49:00 +0000856 } else if (rmode_ == EMBEDDED_OBJECT) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000857 os << " (" << Brief(target_object()) << ")";
Steve Blocka7e24c12009-10-30 11:49:00 +0000858 } else if (rmode_ == EXTERNAL_REFERENCE) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000859 ExternalReferenceEncoder ref_encoder(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000860 os << " ("
861 << ref_encoder.NameOfAddress(isolate, target_external_reference())
862 << ") (" << static_cast<const void*>(target_external_reference())
863 << ")";
Steve Blocka7e24c12009-10-30 11:49:00 +0000864 } else if (IsCodeTarget(rmode_)) {
865 Code* code = Code::GetCodeFromTargetAddress(target_address());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400866 os << " (" << Code::Kind2String(code->kind()) << ") ("
867 << static_cast<const void*>(target_address()) << ")";
Ben Murdoch257744e2011-11-30 15:57:28 +0000868 if (rmode_ == CODE_TARGET_WITH_ID) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000869 os << " (id=" << static_cast<int>(data_) << ")";
Ben Murdoch257744e2011-11-30 15:57:28 +0000870 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000871 } else if (IsPosition(rmode_)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000872 os << " (" << data() << ")";
873 } else if (IsRuntimeEntry(rmode_) &&
874 isolate->deoptimizer_data() != NULL) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100875 // Depotimization bailouts are stored as runtime entries.
876 int id = Deoptimizer::GetDeoptimizationId(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000877 isolate, target_address(), Deoptimizer::EAGER);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100878 if (id != Deoptimizer::kNotDeoptimizationEntry) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000879 os << " (deoptimization bailout " << id << ")";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100880 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000881 } else if (IsConstPool(rmode_)) {
882 os << " (size " << static_cast<int>(data_) << ")";
Steve Blocka7e24c12009-10-30 11:49:00 +0000883 }
884
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000885 os << "\n";
Steve Blocka7e24c12009-10-30 11:49:00 +0000886}
887#endif // ENABLE_DISASSEMBLER
888
889
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000890#ifdef VERIFY_HEAP
891void RelocInfo::Verify(Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000892 switch (rmode_) {
893 case EMBEDDED_OBJECT:
894 Object::VerifyPointer(target_object());
895 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000896 case CELL:
Ben Murdochb0fe1622011-05-05 13:52:32 +0100897 Object::VerifyPointer(target_cell());
898 break;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000899 case DEBUGGER_STATEMENT:
Ben Murdoch257744e2011-11-30 15:57:28 +0000900 case CODE_TARGET_WITH_ID:
Steve Blocka7e24c12009-10-30 11:49:00 +0000901 case CODE_TARGET: {
902 // convert inline target address to code object
903 Address addr = target_address();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000904 CHECK(addr != NULL);
Steve Blocka7e24c12009-10-30 11:49:00 +0000905 // Check that we can find the right code object.
906 Code* code = Code::GetCodeFromTargetAddress(addr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000907 Object* found = isolate->FindCodeObject(addr);
908 CHECK(found->IsCode());
909 CHECK(code->address() == HeapObject::cast(found)->address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000910 break;
911 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000912 case INTERNAL_REFERENCE:
913 case INTERNAL_REFERENCE_ENCODED: {
914 Address target = target_internal_reference();
915 Address pc = target_internal_reference_address();
916 Code* code = Code::cast(isolate->FindCodeObject(pc));
917 CHECK(target >= code->instruction_start());
918 CHECK(target <= code->instruction_end());
919 break;
920 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000921 case RUNTIME_ENTRY:
Steve Blocka7e24c12009-10-30 11:49:00 +0000922 case COMMENT:
923 case POSITION:
924 case STATEMENT_POSITION:
925 case EXTERNAL_REFERENCE:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000926 case DEOPT_REASON:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000927 case CONST_POOL:
928 case VENEER_POOL:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000929 case DEBUG_BREAK_SLOT_AT_POSITION:
930 case DEBUG_BREAK_SLOT_AT_RETURN:
931 case DEBUG_BREAK_SLOT_AT_CALL:
932 case GENERATOR_CONTINUATION:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000933 case NONE32:
934 case NONE64:
Steve Blocka7e24c12009-10-30 11:49:00 +0000935 break;
936 case NUMBER_OF_MODES:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000937 case PC_JUMP:
Steve Blocka7e24c12009-10-30 11:49:00 +0000938 UNREACHABLE();
939 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000940 case CODE_AGE_SEQUENCE:
941 DCHECK(Code::IsYoungSequence(isolate, pc_) || code_age_stub()->IsCode());
942 break;
Steve Blocka7e24c12009-10-30 11:49:00 +0000943 }
944}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000945#endif // VERIFY_HEAP
Steve Blocka7e24c12009-10-30 11:49:00 +0000946
947
Steve Blocka7e24c12009-10-30 11:49:00 +0000948// Implementation of ExternalReference
949
Ben Murdoch097c5b22016-05-18 11:27:45 +0100950static ExternalReference::Type BuiltinCallTypeForResultSize(int result_size) {
951 switch (result_size) {
952 case 1:
953 return ExternalReference::BUILTIN_CALL;
954 case 2:
955 return ExternalReference::BUILTIN_CALL_PAIR;
956 case 3:
957 return ExternalReference::BUILTIN_CALL_TRIPLE;
958 }
959 UNREACHABLE();
960 return ExternalReference::BUILTIN_CALL;
961}
962
963
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000964void ExternalReference::SetUp() {
965 double_constants.min_int = kMinInt;
966 double_constants.one_half = 0.5;
967 double_constants.minus_one_half = -0.5;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000968 double_constants.the_hole_nan = bit_cast<double>(kHoleNanInt64);
969 double_constants.negative_infinity = -V8_INFINITY;
970 double_constants.uint32_bias =
971 static_cast<double>(static_cast<uint32_t>(0xFFFFFFFF)) + 1;
972
973 math_exp_data_mutex = new base::Mutex();
974}
975
976
977void ExternalReference::InitializeMathExpData() {
978 // Early return?
979 if (math_exp_data_initialized) return;
980
981 base::LockGuard<base::Mutex> lock_guard(math_exp_data_mutex);
982 if (!math_exp_data_initialized) {
983 // If this is changed, generated code must be adapted too.
984 const int kTableSizeBits = 11;
985 const int kTableSize = 1 << kTableSizeBits;
986 const double kTableSizeDouble = static_cast<double>(kTableSize);
987
988 math_exp_constants_array = new double[9];
989 // Input values smaller than this always return 0.
990 math_exp_constants_array[0] = -708.39641853226408;
991 // Input values larger than this always return +Infinity.
992 math_exp_constants_array[1] = 709.78271289338397;
993 math_exp_constants_array[2] = V8_INFINITY;
994 // The rest is black magic. Do not attempt to understand it. It is
995 // loosely based on the "expd" function published at:
996 // http://herumi.blogspot.com/2011/08/fast-double-precision-exponential.html
997 const double constant3 = (1 << kTableSizeBits) / std::log(2.0);
998 math_exp_constants_array[3] = constant3;
999 math_exp_constants_array[4] =
1000 static_cast<double>(static_cast<int64_t>(3) << 51);
1001 math_exp_constants_array[5] = 1 / constant3;
1002 math_exp_constants_array[6] = 3.0000000027955394;
1003 math_exp_constants_array[7] = 0.16666666685227835;
1004 math_exp_constants_array[8] = 1;
1005
1006 math_exp_log_table_array = new double[kTableSize];
1007 for (int i = 0; i < kTableSize; i++) {
1008 double value = std::pow(2, i / kTableSizeDouble);
1009 uint64_t bits = bit_cast<uint64_t, double>(value);
1010 bits &= (static_cast<uint64_t>(1) << 52) - 1;
1011 double mantissa = bit_cast<double, uint64_t>(bits);
1012 math_exp_log_table_array[i] = mantissa;
1013 }
1014
1015 math_exp_data_initialized = true;
1016 }
1017}
1018
1019
1020void ExternalReference::TearDownMathExpData() {
1021 delete[] math_exp_constants_array;
1022 math_exp_constants_array = NULL;
1023 delete[] math_exp_log_table_array;
1024 math_exp_log_table_array = NULL;
1025 delete math_exp_data_mutex;
1026 math_exp_data_mutex = NULL;
1027}
1028
1029
Steve Block44f0eee2011-05-26 01:26:41 +01001030ExternalReference::ExternalReference(Builtins::CFunctionId id, Isolate* isolate)
1031 : address_(Redirect(isolate, Builtins::c_function_address(id))) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001032
1033
Steve Block1e0659c2011-05-24 12:43:12 +01001034ExternalReference::ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01001035 ApiFunction* fun,
1036 Type type = ExternalReference::BUILTIN_CALL,
1037 Isolate* isolate = NULL)
1038 : address_(Redirect(isolate, fun->address(), type)) {}
Steve Blockd0582a62009-12-15 09:54:21 +00001039
1040
Steve Block44f0eee2011-05-26 01:26:41 +01001041ExternalReference::ExternalReference(Builtins::Name name, Isolate* isolate)
1042 : address_(isolate->builtins()->builtin_address(name)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001043
1044
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001045ExternalReference::ExternalReference(Runtime::FunctionId id, Isolate* isolate)
Ben Murdoch097c5b22016-05-18 11:27:45 +01001046 : ExternalReference(Runtime::FunctionForId(id), isolate) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001047
1048
Steve Block44f0eee2011-05-26 01:26:41 +01001049ExternalReference::ExternalReference(const Runtime::Function* f,
1050 Isolate* isolate)
Ben Murdoch097c5b22016-05-18 11:27:45 +01001051 : address_(Redirect(isolate, f->entry,
1052 BuiltinCallTypeForResultSize(f->result_size))) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001053
1054
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001055ExternalReference ExternalReference::isolate_address(Isolate* isolate) {
1056 return ExternalReference(isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001057}
1058
Ben Murdoch097c5b22016-05-18 11:27:45 +01001059ExternalReference ExternalReference::interpreter_dispatch_table_address(
1060 Isolate* isolate) {
1061 return ExternalReference(isolate->interpreter()->dispatch_table_address());
1062}
Steve Block44f0eee2011-05-26 01:26:41 +01001063
Steve Blocka7e24c12009-10-30 11:49:00 +00001064ExternalReference::ExternalReference(StatsCounter* counter)
1065 : address_(reinterpret_cast<Address>(counter->GetInternalPointer())) {}
1066
1067
Steve Block44f0eee2011-05-26 01:26:41 +01001068ExternalReference::ExternalReference(Isolate::AddressId id, Isolate* isolate)
1069 : address_(isolate->get_address_from_id(id)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +00001070
1071
1072ExternalReference::ExternalReference(const SCTableReference& table_ref)
1073 : address_(table_ref.address()) {}
1074
1075
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001076ExternalReference ExternalReference::
1077 incremental_marking_record_write_function(Isolate* isolate) {
1078 return ExternalReference(Redirect(
1079 isolate,
1080 FUNCTION_ADDR(IncrementalMarking::RecordWriteFromCode)));
1081}
1082
Ben Murdoch097c5b22016-05-18 11:27:45 +01001083ExternalReference
1084ExternalReference::incremental_marking_record_write_code_entry_function(
1085 Isolate* isolate) {
1086 return ExternalReference(Redirect(
1087 isolate,
1088 FUNCTION_ADDR(IncrementalMarking::RecordWriteOfCodeEntryFromCode)));
1089}
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001090
Ben Murdoch097c5b22016-05-18 11:27:45 +01001091ExternalReference ExternalReference::store_buffer_overflow_function(
1092 Isolate* isolate) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001093 return ExternalReference(Redirect(
1094 isolate,
1095 FUNCTION_ADDR(StoreBuffer::StoreBufferOverflow)));
1096}
1097
1098
Steve Block44f0eee2011-05-26 01:26:41 +01001099ExternalReference ExternalReference::delete_handle_scope_extensions(
1100 Isolate* isolate) {
1101 return ExternalReference(Redirect(
1102 isolate,
1103 FUNCTION_ADDR(HandleScope::DeleteExtensions)));
John Reck59135872010-11-02 12:39:01 -07001104}
1105
1106
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001107ExternalReference ExternalReference::get_date_field_function(
1108 Isolate* isolate) {
1109 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(JSDate::GetField)));
1110}
1111
1112
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001113ExternalReference ExternalReference::get_make_code_young_function(
1114 Isolate* isolate) {
1115 return ExternalReference(Redirect(
1116 isolate, FUNCTION_ADDR(Code::MakeCodeAgeSequenceYoung)));
1117}
1118
1119
1120ExternalReference ExternalReference::get_mark_code_as_executed_function(
1121 Isolate* isolate) {
1122 return ExternalReference(Redirect(
1123 isolate, FUNCTION_ADDR(Code::MarkCodeAsExecuted)));
1124}
1125
1126
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001127ExternalReference ExternalReference::date_cache_stamp(Isolate* isolate) {
1128 return ExternalReference(isolate->date_cache()->stamp_address());
1129}
1130
1131
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001132ExternalReference ExternalReference::stress_deopt_count(Isolate* isolate) {
1133 return ExternalReference(isolate->stress_deopt_count_address());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001134}
1135
1136
Steve Block44f0eee2011-05-26 01:26:41 +01001137ExternalReference ExternalReference::new_deoptimizer_function(
1138 Isolate* isolate) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001139 return ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01001140 Redirect(isolate, FUNCTION_ADDR(Deoptimizer::New)));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001141}
1142
1143
Steve Block44f0eee2011-05-26 01:26:41 +01001144ExternalReference ExternalReference::compute_output_frames_function(
1145 Isolate* isolate) {
1146 return ExternalReference(
1147 Redirect(isolate, FUNCTION_ADDR(Deoptimizer::ComputeOutputFrames)));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001148}
1149
Ben Murdoch097c5b22016-05-18 11:27:45 +01001150static void f32_trunc_wrapper(float* param) { *param = truncf(*param); }
1151
1152ExternalReference ExternalReference::f32_trunc_wrapper_function(
1153 Isolate* isolate) {
1154 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(f32_trunc_wrapper)));
1155}
1156
1157static void f32_floor_wrapper(float* param) { *param = floorf(*param); }
1158
1159ExternalReference ExternalReference::f32_floor_wrapper_function(
1160 Isolate* isolate) {
1161 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(f32_floor_wrapper)));
1162}
1163
1164static void f32_ceil_wrapper(float* param) { *param = ceilf(*param); }
1165
1166ExternalReference ExternalReference::f32_ceil_wrapper_function(
1167 Isolate* isolate) {
1168 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(f32_ceil_wrapper)));
1169}
1170
1171static void f32_nearest_int_wrapper(float* param) {
1172 *param = nearbyintf(*param);
1173}
1174
1175ExternalReference ExternalReference::f32_nearest_int_wrapper_function(
1176 Isolate* isolate) {
1177 return ExternalReference(
1178 Redirect(isolate, FUNCTION_ADDR(f32_nearest_int_wrapper)));
1179}
1180
1181static void f64_trunc_wrapper(double* param) { *param = trunc(*param); }
1182
1183ExternalReference ExternalReference::f64_trunc_wrapper_function(
1184 Isolate* isolate) {
1185 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(f64_trunc_wrapper)));
1186}
1187
1188static void f64_floor_wrapper(double* param) { *param = floor(*param); }
1189
1190ExternalReference ExternalReference::f64_floor_wrapper_function(
1191 Isolate* isolate) {
1192 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(f64_floor_wrapper)));
1193}
1194
1195static void f64_ceil_wrapper(double* param) { *param = ceil(*param); }
1196
1197ExternalReference ExternalReference::f64_ceil_wrapper_function(
1198 Isolate* isolate) {
1199 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(f64_ceil_wrapper)));
1200}
1201
1202static void f64_nearest_int_wrapper(double* param) {
1203 *param = nearbyint(*param);
1204}
1205
1206ExternalReference ExternalReference::f64_nearest_int_wrapper_function(
1207 Isolate* isolate) {
1208 return ExternalReference(
1209 Redirect(isolate, FUNCTION_ADDR(f64_nearest_int_wrapper)));
1210}
Ben Murdochb0fe1622011-05-05 13:52:32 +01001211
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001212ExternalReference ExternalReference::log_enter_external_function(
1213 Isolate* isolate) {
1214 return ExternalReference(
1215 Redirect(isolate, FUNCTION_ADDR(Logger::EnterExternal)));
1216}
1217
1218
1219ExternalReference ExternalReference::log_leave_external_function(
1220 Isolate* isolate) {
1221 return ExternalReference(
1222 Redirect(isolate, FUNCTION_ADDR(Logger::LeaveExternal)));
1223}
1224
1225
Steve Block44f0eee2011-05-26 01:26:41 +01001226ExternalReference ExternalReference::keyed_lookup_cache_keys(Isolate* isolate) {
1227 return ExternalReference(isolate->keyed_lookup_cache()->keys_address());
Steve Blocka7e24c12009-10-30 11:49:00 +00001228}
1229
1230
Steve Block44f0eee2011-05-26 01:26:41 +01001231ExternalReference ExternalReference::keyed_lookup_cache_field_offsets(
1232 Isolate* isolate) {
1233 return ExternalReference(
1234 isolate->keyed_lookup_cache()->field_offsets_address());
Steve Blocka7e24c12009-10-30 11:49:00 +00001235}
1236
1237
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001238ExternalReference ExternalReference::roots_array_start(Isolate* isolate) {
1239 return ExternalReference(isolate->heap()->roots_array_start());
Steve Blocka7e24c12009-10-30 11:49:00 +00001240}
1241
1242
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001243ExternalReference ExternalReference::allocation_sites_list_address(
1244 Isolate* isolate) {
1245 return ExternalReference(isolate->heap()->allocation_sites_list_address());
1246}
1247
1248
Steve Block44f0eee2011-05-26 01:26:41 +01001249ExternalReference ExternalReference::address_of_stack_limit(Isolate* isolate) {
1250 return ExternalReference(isolate->stack_guard()->address_of_jslimit());
Steve Blockd0582a62009-12-15 09:54:21 +00001251}
1252
1253
Steve Block44f0eee2011-05-26 01:26:41 +01001254ExternalReference ExternalReference::address_of_real_stack_limit(
1255 Isolate* isolate) {
1256 return ExternalReference(isolate->stack_guard()->address_of_real_jslimit());
Steve Blocka7e24c12009-10-30 11:49:00 +00001257}
1258
1259
Steve Block44f0eee2011-05-26 01:26:41 +01001260ExternalReference ExternalReference::address_of_regexp_stack_limit(
1261 Isolate* isolate) {
1262 return ExternalReference(isolate->regexp_stack()->limit_address());
Steve Blocka7e24c12009-10-30 11:49:00 +00001263}
1264
1265
Steve Block44f0eee2011-05-26 01:26:41 +01001266ExternalReference ExternalReference::new_space_start(Isolate* isolate) {
1267 return ExternalReference(isolate->heap()->NewSpaceStart());
Andrei Popescu402d9372010-02-26 13:31:12 +00001268}
1269
1270
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001271ExternalReference ExternalReference::store_buffer_top(Isolate* isolate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001272 return ExternalReference(isolate->heap()->store_buffer_top_address());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001273}
1274
1275
Steve Block44f0eee2011-05-26 01:26:41 +01001276ExternalReference ExternalReference::new_space_allocation_top_address(
1277 Isolate* isolate) {
1278 return ExternalReference(isolate->heap()->NewSpaceAllocationTopAddress());
Steve Blocka7e24c12009-10-30 11:49:00 +00001279}
1280
1281
Steve Block44f0eee2011-05-26 01:26:41 +01001282ExternalReference ExternalReference::new_space_allocation_limit_address(
1283 Isolate* isolate) {
1284 return ExternalReference(isolate->heap()->NewSpaceAllocationLimitAddress());
Steve Blocka7e24c12009-10-30 11:49:00 +00001285}
1286
Steve Blockd0582a62009-12-15 09:54:21 +00001287
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001288ExternalReference ExternalReference::old_space_allocation_top_address(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001289 Isolate* isolate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001290 return ExternalReference(isolate->heap()->OldSpaceAllocationTopAddress());
Steve Blockd0582a62009-12-15 09:54:21 +00001291}
1292
1293
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001294ExternalReference ExternalReference::old_space_allocation_limit_address(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001295 Isolate* isolate) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001296 return ExternalReference(isolate->heap()->OldSpaceAllocationLimitAddress());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001297}
1298
1299
1300ExternalReference ExternalReference::handle_scope_level_address(
1301 Isolate* isolate) {
1302 return ExternalReference(HandleScope::current_level_address(isolate));
1303}
1304
1305
1306ExternalReference ExternalReference::handle_scope_next_address(
1307 Isolate* isolate) {
1308 return ExternalReference(HandleScope::current_next_address(isolate));
1309}
1310
1311
1312ExternalReference ExternalReference::handle_scope_limit_address(
1313 Isolate* isolate) {
1314 return ExternalReference(HandleScope::current_limit_address(isolate));
Steve Blockd0582a62009-12-15 09:54:21 +00001315}
1316
1317
Steve Block44f0eee2011-05-26 01:26:41 +01001318ExternalReference ExternalReference::scheduled_exception_address(
1319 Isolate* isolate) {
1320 return ExternalReference(isolate->scheduled_exception_address());
Steve Blockd0582a62009-12-15 09:54:21 +00001321}
1322
1323
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001324ExternalReference ExternalReference::address_of_pending_message_obj(
1325 Isolate* isolate) {
1326 return ExternalReference(isolate->pending_message_obj_address());
1327}
1328
1329
Ben Murdochb0fe1622011-05-05 13:52:32 +01001330ExternalReference ExternalReference::address_of_min_int() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001331 return ExternalReference(reinterpret_cast<void*>(&double_constants.min_int));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001332}
1333
1334
1335ExternalReference ExternalReference::address_of_one_half() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001336 return ExternalReference(reinterpret_cast<void*>(&double_constants.one_half));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001337}
1338
1339
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001340ExternalReference ExternalReference::address_of_minus_one_half() {
1341 return ExternalReference(
1342 reinterpret_cast<void*>(&double_constants.minus_one_half));
Ben Murdoch257744e2011-11-30 15:57:28 +00001343}
1344
1345
Ben Murdochb0fe1622011-05-05 13:52:32 +01001346ExternalReference ExternalReference::address_of_negative_infinity() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001347 return ExternalReference(
1348 reinterpret_cast<void*>(&double_constants.negative_infinity));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001349}
1350
1351
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001352ExternalReference ExternalReference::address_of_the_hole_nan() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001353 return ExternalReference(
1354 reinterpret_cast<void*>(&double_constants.the_hole_nan));
1355}
1356
1357
1358ExternalReference ExternalReference::address_of_uint32_bias() {
1359 return ExternalReference(
1360 reinterpret_cast<void*>(&double_constants.uint32_bias));
1361}
1362
1363
1364ExternalReference ExternalReference::is_profiling_address(Isolate* isolate) {
1365 return ExternalReference(isolate->cpu_profiler()->is_profiling_address());
1366}
1367
1368
1369ExternalReference ExternalReference::invoke_function_callback(
1370 Isolate* isolate) {
1371 Address thunk_address = FUNCTION_ADDR(&InvokeFunctionCallback);
1372 ExternalReference::Type thunk_type = ExternalReference::PROFILING_API_CALL;
1373 ApiFunction thunk_fun(thunk_address);
1374 return ExternalReference(&thunk_fun, thunk_type, isolate);
1375}
1376
1377
1378ExternalReference ExternalReference::invoke_accessor_getter_callback(
1379 Isolate* isolate) {
1380 Address thunk_address = FUNCTION_ADDR(&InvokeAccessorGetterCallback);
1381 ExternalReference::Type thunk_type =
1382 ExternalReference::PROFILING_GETTER_CALL;
1383 ApiFunction thunk_fun(thunk_address);
1384 return ExternalReference(&thunk_fun, thunk_type, isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01001385}
1386
1387
Steve Block6ded16b2010-05-10 14:33:55 +01001388#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +00001389
Steve Block44f0eee2011-05-26 01:26:41 +01001390ExternalReference ExternalReference::re_check_stack_guard_state(
1391 Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001392 Address function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001393#if V8_TARGET_ARCH_X64
Steve Blocka7e24c12009-10-30 11:49:00 +00001394 function = FUNCTION_ADDR(RegExpMacroAssemblerX64::CheckStackGuardState);
1395#elif V8_TARGET_ARCH_IA32
1396 function = FUNCTION_ADDR(RegExpMacroAssemblerIA32::CheckStackGuardState);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001397#elif V8_TARGET_ARCH_ARM64
1398 function = FUNCTION_ADDR(RegExpMacroAssemblerARM64::CheckStackGuardState);
Steve Blocka7e24c12009-10-30 11:49:00 +00001399#elif V8_TARGET_ARCH_ARM
1400 function = FUNCTION_ADDR(RegExpMacroAssemblerARM::CheckStackGuardState);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001401#elif V8_TARGET_ARCH_PPC
1402 function = FUNCTION_ADDR(RegExpMacroAssemblerPPC::CheckStackGuardState);
Steve Block44f0eee2011-05-26 01:26:41 +01001403#elif V8_TARGET_ARCH_MIPS
1404 function = FUNCTION_ADDR(RegExpMacroAssemblerMIPS::CheckStackGuardState);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001405#elif V8_TARGET_ARCH_MIPS64
1406 function = FUNCTION_ADDR(RegExpMacroAssemblerMIPS::CheckStackGuardState);
1407#elif V8_TARGET_ARCH_X87
1408 function = FUNCTION_ADDR(RegExpMacroAssemblerX87::CheckStackGuardState);
Steve Blocka7e24c12009-10-30 11:49:00 +00001409#else
Leon Clarke4515c472010-02-03 11:58:03 +00001410 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +00001411#endif
Steve Block44f0eee2011-05-26 01:26:41 +01001412 return ExternalReference(Redirect(isolate, function));
Steve Blocka7e24c12009-10-30 11:49:00 +00001413}
1414
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001415
Steve Block44f0eee2011-05-26 01:26:41 +01001416ExternalReference ExternalReference::re_grow_stack(Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001417 return ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01001418 Redirect(isolate, FUNCTION_ADDR(NativeRegExpMacroAssembler::GrowStack)));
Steve Blocka7e24c12009-10-30 11:49:00 +00001419}
1420
Steve Block44f0eee2011-05-26 01:26:41 +01001421ExternalReference ExternalReference::re_case_insensitive_compare_uc16(
1422 Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001423 return ExternalReference(Redirect(
Steve Block44f0eee2011-05-26 01:26:41 +01001424 isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +00001425 FUNCTION_ADDR(NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16)));
1426}
1427
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001428
Leon Clarkee46be812010-01-19 14:06:41 +00001429ExternalReference ExternalReference::re_word_character_map() {
1430 return ExternalReference(
1431 NativeRegExpMacroAssembler::word_character_map_address());
1432}
1433
Steve Block44f0eee2011-05-26 01:26:41 +01001434ExternalReference ExternalReference::address_of_static_offsets_vector(
1435 Isolate* isolate) {
1436 return ExternalReference(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001437 reinterpret_cast<Address>(isolate->jsregexp_static_offsets_vector()));
Leon Clarkee46be812010-01-19 14:06:41 +00001438}
1439
Steve Block44f0eee2011-05-26 01:26:41 +01001440ExternalReference ExternalReference::address_of_regexp_stack_memory_address(
1441 Isolate* isolate) {
1442 return ExternalReference(
1443 isolate->regexp_stack()->memory_address());
Leon Clarkee46be812010-01-19 14:06:41 +00001444}
1445
Steve Block44f0eee2011-05-26 01:26:41 +01001446ExternalReference ExternalReference::address_of_regexp_stack_memory_size(
1447 Isolate* isolate) {
1448 return ExternalReference(isolate->regexp_stack()->memory_size_address());
Leon Clarkee46be812010-01-19 14:06:41 +00001449}
1450
Steve Block6ded16b2010-05-10 14:33:55 +01001451#endif // V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +00001452
1453
Steve Block44f0eee2011-05-26 01:26:41 +01001454ExternalReference ExternalReference::math_log_double_function(
1455 Isolate* isolate) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001456 typedef double (*d2d)(double x);
Steve Block44f0eee2011-05-26 01:26:41 +01001457 return ExternalReference(Redirect(isolate,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001458 FUNCTION_ADDR(static_cast<d2d>(std::log)),
Ben Murdoch257744e2011-11-30 15:57:28 +00001459 BUILTIN_FP_CALL));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001460}
1461
1462
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001463ExternalReference ExternalReference::math_exp_constants(int constant_index) {
1464 DCHECK(math_exp_data_initialized);
1465 return ExternalReference(
1466 reinterpret_cast<void*>(math_exp_constants_array + constant_index));
1467}
1468
1469
1470ExternalReference ExternalReference::math_exp_log_table() {
1471 DCHECK(math_exp_data_initialized);
1472 return ExternalReference(reinterpret_cast<void*>(math_exp_log_table_array));
1473}
1474
1475
1476ExternalReference ExternalReference::page_flags(Page* page) {
1477 return ExternalReference(reinterpret_cast<Address>(page) +
1478 MemoryChunk::kFlagsOffset);
1479}
1480
1481
1482ExternalReference ExternalReference::ForDeoptEntry(Address entry) {
1483 return ExternalReference(entry);
1484}
1485
1486
1487ExternalReference ExternalReference::cpu_features() {
1488 DCHECK(CpuFeatures::initialized_);
1489 return ExternalReference(&CpuFeatures::supported_);
1490}
1491
1492
1493ExternalReference ExternalReference::debug_is_active_address(
1494 Isolate* isolate) {
1495 return ExternalReference(isolate->debug()->is_active_address());
1496}
1497
1498
1499ExternalReference ExternalReference::debug_after_break_target_address(
1500 Isolate* isolate) {
1501 return ExternalReference(isolate->debug()->after_break_target_address());
1502}
1503
1504
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001505ExternalReference ExternalReference::virtual_handler_register(
1506 Isolate* isolate) {
1507 return ExternalReference(isolate->virtual_handler_register_address());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001508}
1509
1510
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001511ExternalReference ExternalReference::virtual_slot_register(Isolate* isolate) {
1512 return ExternalReference(isolate->virtual_slot_register_address());
1513}
1514
1515
1516ExternalReference ExternalReference::runtime_function_table_address(
1517 Isolate* isolate) {
1518 return ExternalReference(
1519 const_cast<Runtime::Function*>(Runtime::RuntimeFunctionTable(isolate)));
1520}
1521
1522
1523double power_helper(Isolate* isolate, double x, double y) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001524 int y_int = static_cast<int>(y);
1525 if (y == y_int) {
1526 return power_double_int(x, y_int); // Returns 1 if exponent is 0.
1527 }
1528 if (y == 0.5) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001529 lazily_initialize_fast_sqrt(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001530 return (std::isinf(x)) ? V8_INFINITY
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001531 : fast_sqrt(x + 0.0, isolate); // Convert -0 to +0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001532 }
1533 if (y == -0.5) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001534 lazily_initialize_fast_sqrt(isolate);
1535 return (std::isinf(x)) ? 0 : 1.0 / fast_sqrt(x + 0.0,
1536 isolate); // Convert -0 to +0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001537 }
1538 return power_double_double(x, y);
1539}
1540
1541
Ben Murdochb0fe1622011-05-05 13:52:32 +01001542// Helper function to compute x^y, where y is known to be an
1543// integer. Uses binary decomposition to limit the number of
1544// multiplications; see the discussion in "Hacker's Delight" by Henry
1545// S. Warren, Jr., figure 11-6, page 213.
1546double power_double_int(double x, int y) {
1547 double m = (y < 0) ? 1 / x : x;
1548 unsigned n = (y < 0) ? -y : y;
1549 double p = 1;
1550 while (n != 0) {
1551 if ((n & 1) != 0) p *= m;
1552 m *= m;
1553 if ((n & 2) != 0) p *= m;
1554 m *= m;
1555 n >>= 2;
1556 }
1557 return p;
1558}
1559
1560
1561double power_double_double(double x, double y) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001562#if (defined(__MINGW64_VERSION_MAJOR) && \
1563 (!defined(__MINGW64_VERSION_RC) || __MINGW64_VERSION_RC < 1)) || \
1564 defined(V8_OS_AIX)
1565 // MinGW64 and AIX have a custom implementation for pow. This handles certain
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001566 // special cases that are different.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001567 if ((x == 0.0 || std::isinf(x)) && y != 0.0 && std::isfinite(y)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001568 double f;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001569 double result = ((x == 0.0) ^ (y > 0)) ? V8_INFINITY : 0;
1570 /* retain sign if odd integer exponent */
1571 return ((std::modf(y, &f) == 0.0) && (static_cast<int64_t>(y) & 1))
1572 ? copysign(result, x)
1573 : result;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001574 }
1575
1576 if (x == 2.0) {
1577 int y_int = static_cast<int>(y);
1578 if (y == y_int) {
1579 return std::ldexp(1.0, y_int);
1580 }
1581 }
1582#endif
1583
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001584 // The checks for special cases can be dropped in ia32 because it has already
1585 // been done in generated code before bailing out here.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001586 if (std::isnan(y) || ((x == 1 || x == -1) && std::isinf(y))) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001587 return std::numeric_limits<double>::quiet_NaN();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001588 }
1589 return std::pow(x, y);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001590}
1591
1592
Steve Block44f0eee2011-05-26 01:26:41 +01001593ExternalReference ExternalReference::power_double_double_function(
1594 Isolate* isolate) {
1595 return ExternalReference(Redirect(isolate,
1596 FUNCTION_ADDR(power_double_double),
Ben Murdoch257744e2011-11-30 15:57:28 +00001597 BUILTIN_FP_FP_CALL));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001598}
1599
1600
Steve Block44f0eee2011-05-26 01:26:41 +01001601ExternalReference ExternalReference::power_double_int_function(
1602 Isolate* isolate) {
1603 return ExternalReference(Redirect(isolate,
1604 FUNCTION_ADDR(power_double_int),
Ben Murdoch257744e2011-11-30 15:57:28 +00001605 BUILTIN_FP_INT_CALL));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001606}
1607
1608
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001609ExternalReference ExternalReference::mod_two_doubles_operation(
1610 Isolate* isolate) {
Steve Block44f0eee2011-05-26 01:26:41 +01001611 return ExternalReference(Redirect(isolate,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001612 FUNCTION_ADDR(modulo),
Ben Murdoch257744e2011-11-30 15:57:28 +00001613 BUILTIN_FP_FP_CALL));
Steve Blocka7e24c12009-10-30 11:49:00 +00001614}
1615
1616
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001617ExternalReference ExternalReference::debug_step_in_enabled_address(
1618 Isolate* isolate) {
1619 return ExternalReference(isolate->debug()->step_in_enabled_address());
Steve Blocka7e24c12009-10-30 11:49:00 +00001620}
1621
1622
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001623ExternalReference ExternalReference::fixed_typed_array_base_data_offset() {
1624 return ExternalReference(reinterpret_cast<void*>(
1625 FixedTypedArrayBase::kDataOffset - kHeapObjectTag));
Steve Blocka7e24c12009-10-30 11:49:00 +00001626}
Steve Blocka7e24c12009-10-30 11:49:00 +00001627
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001628
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001629bool operator==(ExternalReference lhs, ExternalReference rhs) {
1630 return lhs.address() == rhs.address();
1631}
1632
1633
1634bool operator!=(ExternalReference lhs, ExternalReference rhs) {
1635 return !(lhs == rhs);
1636}
1637
1638
1639size_t hash_value(ExternalReference reference) {
1640 return base::hash<Address>()(reference.address());
1641}
1642
1643
1644std::ostream& operator<<(std::ostream& os, ExternalReference reference) {
1645 os << static_cast<const void*>(reference.address());
1646 const Runtime::Function* fn = Runtime::FunctionForEntry(reference.address());
1647 if (fn) os << "<" << fn->name << ".entry>";
1648 return os;
1649}
1650
1651
Ben Murdochb0fe1622011-05-05 13:52:32 +01001652void PositionsRecorder::RecordPosition(int pos) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001653 DCHECK(pos != RelocInfo::kNoPosition);
1654 DCHECK(pos >= 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001655 state_.current_position = pos;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001656 LOG_CODE_EVENT(assembler_->isolate(),
1657 CodeLinePosInfoAddPositionEvent(jit_handler_data_,
1658 assembler_->pc_offset(),
1659 pos));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001660}
1661
1662
1663void PositionsRecorder::RecordStatementPosition(int pos) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001664 DCHECK(pos != RelocInfo::kNoPosition);
1665 DCHECK(pos >= 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001666 state_.current_statement_position = pos;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001667 LOG_CODE_EVENT(assembler_->isolate(),
1668 CodeLinePosInfoAddStatementPositionEvent(
1669 jit_handler_data_,
1670 assembler_->pc_offset(),
1671 pos));
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001672}
1673
1674
1675bool PositionsRecorder::WriteRecordedPositions() {
1676 bool written = false;
1677
1678 // Write the statement position if it is different from what was written last
1679 // time.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001680 if (state_.current_statement_position != state_.written_statement_position) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001681 EnsureSpace ensure_space(assembler_);
1682 assembler_->RecordRelocInfo(RelocInfo::STATEMENT_POSITION,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001683 state_.current_statement_position);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001684 state_.written_position = state_.current_statement_position;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001685 state_.written_statement_position = state_.current_statement_position;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001686 written = true;
1687 }
1688
1689 // Write the position if it is different from what was written last time and
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001690 // also different from the statement position that was just written.
1691 if (state_.current_position != state_.written_position) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001692 EnsureSpace ensure_space(assembler_);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001693 assembler_->RecordRelocInfo(RelocInfo::POSITION, state_.current_position);
1694 state_.written_position = state_.current_position;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001695 written = true;
1696 }
1697
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001698 // Return whether something was written.
1699 return written;
1700}
1701
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001702
1703ConstantPoolBuilder::ConstantPoolBuilder(int ptr_reach_bits,
1704 int double_reach_bits) {
1705 info_[ConstantPoolEntry::INTPTR].entries.reserve(64);
1706 info_[ConstantPoolEntry::INTPTR].regular_reach_bits = ptr_reach_bits;
1707 info_[ConstantPoolEntry::DOUBLE].regular_reach_bits = double_reach_bits;
1708}
1709
1710
1711ConstantPoolEntry::Access ConstantPoolBuilder::NextAccess(
1712 ConstantPoolEntry::Type type) const {
1713 const PerTypeEntryInfo& info = info_[type];
1714
1715 if (info.overflow()) return ConstantPoolEntry::OVERFLOWED;
1716
1717 int dbl_count = info_[ConstantPoolEntry::DOUBLE].regular_count;
1718 int dbl_offset = dbl_count * kDoubleSize;
1719 int ptr_count = info_[ConstantPoolEntry::INTPTR].regular_count;
1720 int ptr_offset = ptr_count * kPointerSize + dbl_offset;
1721
1722 if (type == ConstantPoolEntry::DOUBLE) {
1723 // Double overflow detection must take into account the reach for both types
1724 int ptr_reach_bits = info_[ConstantPoolEntry::INTPTR].regular_reach_bits;
1725 if (!is_uintn(dbl_offset, info.regular_reach_bits) ||
1726 (ptr_count > 0 &&
1727 !is_uintn(ptr_offset + kDoubleSize - kPointerSize, ptr_reach_bits))) {
1728 return ConstantPoolEntry::OVERFLOWED;
1729 }
1730 } else {
1731 DCHECK(type == ConstantPoolEntry::INTPTR);
1732 if (!is_uintn(ptr_offset, info.regular_reach_bits)) {
1733 return ConstantPoolEntry::OVERFLOWED;
1734 }
1735 }
1736
1737 return ConstantPoolEntry::REGULAR;
1738}
1739
1740
1741ConstantPoolEntry::Access ConstantPoolBuilder::AddEntry(
1742 ConstantPoolEntry& entry, ConstantPoolEntry::Type type) {
1743 DCHECK(!emitted_label_.is_bound());
1744 PerTypeEntryInfo& info = info_[type];
1745 const int entry_size = ConstantPoolEntry::size(type);
1746 bool merged = false;
1747
1748 if (entry.sharing_ok()) {
1749 // Try to merge entries
1750 std::vector<ConstantPoolEntry>::iterator it = info.shared_entries.begin();
1751 int end = static_cast<int>(info.shared_entries.size());
1752 for (int i = 0; i < end; i++, it++) {
1753 if ((entry_size == kPointerSize) ? entry.value() == it->value()
1754 : entry.value64() == it->value64()) {
1755 // Merge with found entry.
1756 entry.set_merged_index(i);
1757 merged = true;
1758 break;
1759 }
1760 }
1761 }
1762
1763 // By definition, merged entries have regular access.
1764 DCHECK(!merged || entry.merged_index() < info.regular_count);
1765 ConstantPoolEntry::Access access =
1766 (merged ? ConstantPoolEntry::REGULAR : NextAccess(type));
1767
1768 // Enforce an upper bound on search time by limiting the search to
1769 // unique sharable entries which fit in the regular section.
1770 if (entry.sharing_ok() && !merged && access == ConstantPoolEntry::REGULAR) {
1771 info.shared_entries.push_back(entry);
1772 } else {
1773 info.entries.push_back(entry);
1774 }
1775
1776 // We're done if we found a match or have already triggered the
1777 // overflow state.
1778 if (merged || info.overflow()) return access;
1779
1780 if (access == ConstantPoolEntry::REGULAR) {
1781 info.regular_count++;
1782 } else {
1783 info.overflow_start = static_cast<int>(info.entries.size()) - 1;
1784 }
1785
1786 return access;
1787}
1788
1789
1790void ConstantPoolBuilder::EmitSharedEntries(Assembler* assm,
1791 ConstantPoolEntry::Type type) {
1792 PerTypeEntryInfo& info = info_[type];
1793 std::vector<ConstantPoolEntry>& shared_entries = info.shared_entries;
1794 const int entry_size = ConstantPoolEntry::size(type);
1795 int base = emitted_label_.pos();
1796 DCHECK(base > 0);
1797 int shared_end = static_cast<int>(shared_entries.size());
1798 std::vector<ConstantPoolEntry>::iterator shared_it = shared_entries.begin();
1799 for (int i = 0; i < shared_end; i++, shared_it++) {
1800 int offset = assm->pc_offset() - base;
1801 shared_it->set_offset(offset); // Save offset for merged entries.
1802 if (entry_size == kPointerSize) {
1803 assm->dp(shared_it->value());
1804 } else {
1805 assm->dq(shared_it->value64());
1806 }
1807 DCHECK(is_uintn(offset, info.regular_reach_bits));
1808
1809 // Patch load sequence with correct offset.
1810 assm->PatchConstantPoolAccessInstruction(shared_it->position(), offset,
1811 ConstantPoolEntry::REGULAR, type);
1812 }
1813}
1814
1815
1816void ConstantPoolBuilder::EmitGroup(Assembler* assm,
1817 ConstantPoolEntry::Access access,
1818 ConstantPoolEntry::Type type) {
1819 PerTypeEntryInfo& info = info_[type];
1820 const bool overflow = info.overflow();
1821 std::vector<ConstantPoolEntry>& entries = info.entries;
1822 std::vector<ConstantPoolEntry>& shared_entries = info.shared_entries;
1823 const int entry_size = ConstantPoolEntry::size(type);
1824 int base = emitted_label_.pos();
1825 DCHECK(base > 0);
1826 int begin;
1827 int end;
1828
1829 if (access == ConstantPoolEntry::REGULAR) {
1830 // Emit any shared entries first
1831 EmitSharedEntries(assm, type);
1832 }
1833
1834 if (access == ConstantPoolEntry::REGULAR) {
1835 begin = 0;
1836 end = overflow ? info.overflow_start : static_cast<int>(entries.size());
1837 } else {
1838 DCHECK(access == ConstantPoolEntry::OVERFLOWED);
1839 if (!overflow) return;
1840 begin = info.overflow_start;
1841 end = static_cast<int>(entries.size());
1842 }
1843
1844 std::vector<ConstantPoolEntry>::iterator it = entries.begin();
1845 if (begin > 0) std::advance(it, begin);
1846 for (int i = begin; i < end; i++, it++) {
1847 // Update constant pool if necessary and get the entry's offset.
1848 int offset;
1849 ConstantPoolEntry::Access entry_access;
1850 if (!it->is_merged()) {
1851 // Emit new entry
1852 offset = assm->pc_offset() - base;
1853 entry_access = access;
1854 if (entry_size == kPointerSize) {
1855 assm->dp(it->value());
1856 } else {
1857 assm->dq(it->value64());
1858 }
1859 } else {
1860 // Retrieve offset from shared entry.
1861 offset = shared_entries[it->merged_index()].offset();
1862 entry_access = ConstantPoolEntry::REGULAR;
1863 }
1864
1865 DCHECK(entry_access == ConstantPoolEntry::OVERFLOWED ||
1866 is_uintn(offset, info.regular_reach_bits));
1867
1868 // Patch load sequence with correct offset.
1869 assm->PatchConstantPoolAccessInstruction(it->position(), offset,
1870 entry_access, type);
1871 }
1872}
1873
1874
1875// Emit and return position of pool. Zero implies no constant pool.
1876int ConstantPoolBuilder::Emit(Assembler* assm) {
1877 bool emitted = emitted_label_.is_bound();
1878 bool empty = IsEmpty();
1879
1880 if (!emitted) {
1881 // Mark start of constant pool. Align if necessary.
1882 if (!empty) assm->DataAlign(kDoubleSize);
1883 assm->bind(&emitted_label_);
1884 if (!empty) {
1885 // Emit in groups based on access and type.
1886 // Emit doubles first for alignment purposes.
1887 EmitGroup(assm, ConstantPoolEntry::REGULAR, ConstantPoolEntry::DOUBLE);
1888 EmitGroup(assm, ConstantPoolEntry::REGULAR, ConstantPoolEntry::INTPTR);
1889 if (info_[ConstantPoolEntry::DOUBLE].overflow()) {
1890 assm->DataAlign(kDoubleSize);
1891 EmitGroup(assm, ConstantPoolEntry::OVERFLOWED,
1892 ConstantPoolEntry::DOUBLE);
1893 }
1894 if (info_[ConstantPoolEntry::INTPTR].overflow()) {
1895 EmitGroup(assm, ConstantPoolEntry::OVERFLOWED,
1896 ConstantPoolEntry::INTPTR);
1897 }
1898 }
1899 }
1900
1901 return !empty ? emitted_label_.pos() : 0;
1902}
1903
1904
1905// Platform specific but identical code for all the platforms.
1906
1907
Ben Murdoch097c5b22016-05-18 11:27:45 +01001908void Assembler::RecordDeoptReason(const int reason, int raw_position) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001909 if (FLAG_trace_deopt || isolate()->cpu_profiler()->is_profiling()) {
1910 EnsureSpace ensure_space(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001911 RecordRelocInfo(RelocInfo::POSITION, raw_position);
1912 RecordRelocInfo(RelocInfo::DEOPT_REASON, reason);
1913 }
1914}
1915
1916
1917void Assembler::RecordComment(const char* msg) {
1918 if (FLAG_code_comments) {
1919 EnsureSpace ensure_space(this);
1920 RecordRelocInfo(RelocInfo::COMMENT, reinterpret_cast<intptr_t>(msg));
1921 }
1922}
1923
1924
1925void Assembler::RecordGeneratorContinuation() {
1926 EnsureSpace ensure_space(this);
1927 RecordRelocInfo(RelocInfo::GENERATOR_CONTINUATION);
1928}
1929
1930
1931void Assembler::RecordDebugBreakSlot(RelocInfo::Mode mode) {
1932 EnsureSpace ensure_space(this);
1933 DCHECK(RelocInfo::IsDebugBreakSlot(mode));
1934 RecordRelocInfo(mode);
1935}
1936
1937
1938void Assembler::DataAlign(int m) {
1939 DCHECK(m >= 2 && base::bits::IsPowerOfTwo32(m));
1940 while ((pc_offset() & (m - 1)) != 0) {
1941 db(0);
1942 }
1943}
1944} // namespace internal
1945} // namespace v8