blob: 4944202f076d9817b510dc2aafc28cb7f72f054d [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 Murdoch3ef787d2012-04-12 10:51:47 +010035#include "assembler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000036
Ben Murdoch3ef787d2012-04-12 10:51:47 +010037#include <math.h> // For cos, log, pow, sin, tan, etc.
38#include "api.h"
39#include "builtins.h"
40#include "counters.h"
41#include "cpu.h"
42#include "debug.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010043#include "deoptimizer.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000044#include "execution.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010045#include "ic.h"
46#include "isolate.h"
47#include "jsregexp.h"
48#include "lazy-instance.h"
Ben Murdoch85b71792012-04-11 18:30:58 +010049#include "platform.h"
Ben Murdoch3ef787d2012-04-12 10:51:47 +010050#include "regexp-macro-assembler.h"
51#include "regexp-stack.h"
52#include "runtime.h"
53#include "serialize.h"
54#include "store-buffer-inl.h"
55#include "stub-cache.h"
56#include "token.h"
57
58#if V8_TARGET_ARCH_IA32
59#include "ia32/assembler-ia32-inl.h"
60#elif V8_TARGET_ARCH_X64
61#include "x64/assembler-x64-inl.h"
62#elif V8_TARGET_ARCH_ARM
63#include "arm/assembler-arm-inl.h"
64#elif V8_TARGET_ARCH_MIPS
65#include "mips/assembler-mips-inl.h"
66#else
67#error "Unknown architecture."
68#endif
69
Steve Blocka7e24c12009-10-30 11:49:00 +000070// Include native regexp-macro-assembler.
Steve Block6ded16b2010-05-10 14:33:55 +010071#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +000072#if V8_TARGET_ARCH_IA32
73#include "ia32/regexp-macro-assembler-ia32.h"
74#elif V8_TARGET_ARCH_X64
75#include "x64/regexp-macro-assembler-x64.h"
76#elif V8_TARGET_ARCH_ARM
77#include "arm/regexp-macro-assembler-arm.h"
Steve Block44f0eee2011-05-26 01:26:41 +010078#elif V8_TARGET_ARCH_MIPS
79#include "mips/regexp-macro-assembler-mips.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000080#else // Unknown architecture.
81#error "Unknown architecture."
82#endif // Target architecture.
Steve Block6ded16b2010-05-10 14:33:55 +010083#endif // V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +000084
85namespace v8 {
86namespace internal {
87
Ben Murdoch3ef787d2012-04-12 10:51:47 +010088// -----------------------------------------------------------------------------
89// Common double constants.
Steve Blocka7e24c12009-10-30 11:49:00 +000090
Ben Murdoch3ef787d2012-04-12 10:51:47 +010091struct DoubleConstant BASE_EMBEDDED {
92 double min_int;
93 double one_half;
94 double minus_zero;
95 double zero;
96 double uint8_max_value;
97 double negative_infinity;
98 double canonical_non_hole_nan;
99 double the_hole_nan;
100};
101
102struct InitializeDoubleConstants {
103 static void Construct(DoubleConstant* double_constants) {
104 double_constants->min_int = kMinInt;
105 double_constants->one_half = 0.5;
106 double_constants->minus_zero = -0.0;
107 double_constants->uint8_max_value = 255;
108 double_constants->zero = 0.0;
109 double_constants->canonical_non_hole_nan = OS::nan_value();
110 double_constants->the_hole_nan = BitCast<double>(kHoleNanInt64);
111 double_constants->negative_infinity = -V8_INFINITY;
112 }
113};
114
115static LazyInstance<DoubleConstant, InitializeDoubleConstants>::type
116 double_constants = LAZY_INSTANCE_INITIALIZER;
117
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000118const char* const RelocInfo::kFillerCommentString = "DEOPTIMIZATION PADDING";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100119
Steve Blocka7e24c12009-10-30 11:49:00 +0000120// -----------------------------------------------------------------------------
Steve Block053d10c2011-06-13 19:13:29 +0100121// Implementation of AssemblerBase
122
123AssemblerBase::AssemblerBase(Isolate* isolate)
124 : isolate_(isolate),
125 jit_cookie_(0) {
126 if (FLAG_mask_constants_with_cookie && isolate != NULL) {
127 jit_cookie_ = V8::RandomPrivate(isolate);
128 }
129}
130
131
132// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +0000133// Implementation of Label
134
135int Label::pos() const {
136 if (pos_ < 0) return -pos_ - 1;
137 if (pos_ > 0) return pos_ - 1;
138 UNREACHABLE();
139 return 0;
140}
141
142
143// -----------------------------------------------------------------------------
144// Implementation of RelocInfoWriter and RelocIterator
145//
Ben Murdoch257744e2011-11-30 15:57:28 +0000146// Relocation information is written backwards in memory, from high addresses
147// towards low addresses, byte by byte. Therefore, in the encodings listed
148// below, the first byte listed it at the highest address, and successive
149// bytes in the record are at progressively lower addresses.
150//
Steve Blocka7e24c12009-10-30 11:49:00 +0000151// Encoding
152//
153// The most common modes are given single-byte encodings. Also, it is
154// easy to identify the type of reloc info and skip unwanted modes in
155// an iteration.
156//
Ben Murdoch257744e2011-11-30 15:57:28 +0000157// The encoding relies on the fact that there are fewer than 14
158// different non-compactly encoded relocation modes.
Steve Blocka7e24c12009-10-30 11:49:00 +0000159//
Ben Murdoch257744e2011-11-30 15:57:28 +0000160// The first byte of a relocation record has a tag in its low 2 bits:
161// Here are the record schemes, depending on the low tag and optional higher
162// tags.
Steve Blocka7e24c12009-10-30 11:49:00 +0000163//
Ben Murdoch257744e2011-11-30 15:57:28 +0000164// Low tag:
165// 00: embedded_object: [6-bit pc delta] 00
Steve Blocka7e24c12009-10-30 11:49:00 +0000166//
Ben Murdoch257744e2011-11-30 15:57:28 +0000167// 01: code_target: [6-bit pc delta] 01
Steve Blocka7e24c12009-10-30 11:49:00 +0000168//
Ben Murdoch257744e2011-11-30 15:57:28 +0000169// 10: short_data_record: [6-bit pc delta] 10 followed by
170// [6-bit data delta] [2-bit data type tag]
Steve Blocka7e24c12009-10-30 11:49:00 +0000171//
Ben Murdoch257744e2011-11-30 15:57:28 +0000172// 11: long_record [2-bit high tag][4 bit middle_tag] 11
173// followed by variable data depending on type.
Steve Blocka7e24c12009-10-30 11:49:00 +0000174//
Ben Murdoch257744e2011-11-30 15:57:28 +0000175// 2-bit data type tags, used in short_data_record and data_jump long_record:
176// code_target_with_id: 00
177// position: 01
178// statement_position: 10
179// comment: 11 (not used in short_data_record)
Steve Blocka7e24c12009-10-30 11:49:00 +0000180//
Ben Murdoch257744e2011-11-30 15:57:28 +0000181// Long record format:
182// 4-bit middle_tag:
183// 0000 - 1100 : Short record for RelocInfo::Mode middle_tag + 2
184// (The middle_tag encodes rmode - RelocInfo::LAST_COMPACT_ENUM,
185// and is between 0000 and 1100)
186// The format is:
187// 00 [4 bit middle_tag] 11 followed by
188// 00 [6 bit pc delta]
Steve Blocka7e24c12009-10-30 11:49:00 +0000189//
Ben Murdoch257744e2011-11-30 15:57:28 +0000190// 1101: not used (would allow one more relocation mode to be added)
191// 1110: long_data_record
192// The format is: [2-bit data_type_tag] 1110 11
193// signed intptr_t, lowest byte written first
194// (except data_type code_target_with_id, which
195// is followed by a signed int, not intptr_t.)
Steve Blocka7e24c12009-10-30 11:49:00 +0000196//
Ben Murdoch257744e2011-11-30 15:57:28 +0000197// 1111: long_pc_jump
198// The format is:
199// pc-jump: 00 1111 11,
200// 00 [6 bits pc delta]
201// or
202// pc-jump (variable length):
203// 01 1111 11,
204// [7 bits data] 0
205// ...
206// [7 bits data] 1
207// (Bits 6..31 of pc delta, with leading zeroes
208// dropped, and last non-zero chunk tagged with 1.)
209
210
Steve Blocka7e24c12009-10-30 11:49:00 +0000211const int kMaxRelocModes = 14;
212
213const int kTagBits = 2;
214const int kTagMask = (1 << kTagBits) - 1;
215const int kExtraTagBits = 4;
Ben Murdoch257744e2011-11-30 15:57:28 +0000216const int kLocatableTypeTagBits = 2;
217const int kSmallDataBits = kBitsPerByte - kLocatableTypeTagBits;
Steve Blocka7e24c12009-10-30 11:49:00 +0000218
219const int kEmbeddedObjectTag = 0;
220const int kCodeTargetTag = 1;
Ben Murdoch257744e2011-11-30 15:57:28 +0000221const int kLocatableTag = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +0000222const int kDefaultTag = 3;
223
Ben Murdoch257744e2011-11-30 15:57:28 +0000224const int kPCJumpExtraTag = (1 << kExtraTagBits) - 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000225
226const int kSmallPCDeltaBits = kBitsPerByte - kTagBits;
227const int kSmallPCDeltaMask = (1 << kSmallPCDeltaBits) - 1;
Steve Block44f0eee2011-05-26 01:26:41 +0100228const int RelocInfo::kMaxSmallPCDelta = kSmallPCDeltaMask;
Steve Blocka7e24c12009-10-30 11:49:00 +0000229
230const int kVariableLengthPCJumpTopTag = 1;
231const int kChunkBits = 7;
232const int kChunkMask = (1 << kChunkBits) - 1;
233const int kLastChunkTagBits = 1;
234const int kLastChunkTagMask = 1;
235const int kLastChunkTag = 1;
236
237
Ben Murdoch257744e2011-11-30 15:57:28 +0000238const int kDataJumpExtraTag = kPCJumpExtraTag - 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000239
Ben Murdoch257744e2011-11-30 15:57:28 +0000240const int kCodeWithIdTag = 0;
241const int kNonstatementPositionTag = 1;
242const int kStatementPositionTag = 2;
243const int kCommentTag = 3;
Steve Blocka7e24c12009-10-30 11:49:00 +0000244
245
246uint32_t RelocInfoWriter::WriteVariableLengthPCJump(uint32_t pc_delta) {
247 // Return if the pc_delta can fit in kSmallPCDeltaBits bits.
248 // Otherwise write a variable length PC jump for the bits that do
249 // not fit in the kSmallPCDeltaBits bits.
250 if (is_uintn(pc_delta, kSmallPCDeltaBits)) return pc_delta;
Ben Murdoch257744e2011-11-30 15:57:28 +0000251 WriteExtraTag(kPCJumpExtraTag, kVariableLengthPCJumpTopTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000252 uint32_t pc_jump = pc_delta >> kSmallPCDeltaBits;
253 ASSERT(pc_jump > 0);
254 // Write kChunkBits size chunks of the pc_jump.
255 for (; pc_jump > 0; pc_jump = pc_jump >> kChunkBits) {
256 byte b = pc_jump & kChunkMask;
257 *--pos_ = b << kLastChunkTagBits;
258 }
259 // Tag the last chunk so it can be identified.
260 *pos_ = *pos_ | kLastChunkTag;
261 // Return the remaining kSmallPCDeltaBits of the pc_delta.
262 return pc_delta & kSmallPCDeltaMask;
263}
264
265
266void RelocInfoWriter::WriteTaggedPC(uint32_t pc_delta, int tag) {
267 // Write a byte of tagged pc-delta, possibly preceded by var. length pc-jump.
268 pc_delta = WriteVariableLengthPCJump(pc_delta);
269 *--pos_ = pc_delta << kTagBits | tag;
270}
271
272
273void RelocInfoWriter::WriteTaggedData(intptr_t data_delta, int tag) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000274 *--pos_ = static_cast<byte>(data_delta << kLocatableTypeTagBits | tag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000275}
276
277
278void RelocInfoWriter::WriteExtraTag(int extra_tag, int top_tag) {
Steve Blockd0582a62009-12-15 09:54:21 +0000279 *--pos_ = static_cast<int>(top_tag << (kTagBits + kExtraTagBits) |
280 extra_tag << kTagBits |
281 kDefaultTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000282}
283
284
285void RelocInfoWriter::WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag) {
286 // Write two-byte tagged pc-delta, possibly preceded by var. length pc-jump.
287 pc_delta = WriteVariableLengthPCJump(pc_delta);
288 WriteExtraTag(extra_tag, 0);
289 *--pos_ = pc_delta;
290}
291
292
Ben Murdoch257744e2011-11-30 15:57:28 +0000293void RelocInfoWriter::WriteExtraTaggedIntData(int data_delta, int top_tag) {
294 WriteExtraTag(kDataJumpExtraTag, top_tag);
295 for (int i = 0; i < kIntSize; i++) {
296 *--pos_ = static_cast<byte>(data_delta);
297 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
298 data_delta = data_delta >> kBitsPerByte;
299 }
300}
301
Steve Blocka7e24c12009-10-30 11:49:00 +0000302void RelocInfoWriter::WriteExtraTaggedData(intptr_t data_delta, int top_tag) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000303 WriteExtraTag(kDataJumpExtraTag, top_tag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000304 for (int i = 0; i < kIntptrSize; i++) {
Steve Blockd0582a62009-12-15 09:54:21 +0000305 *--pos_ = static_cast<byte>(data_delta);
Ben Murdoch257744e2011-11-30 15:57:28 +0000306 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
Steve Blocka7e24c12009-10-30 11:49:00 +0000307 data_delta = data_delta >> kBitsPerByte;
308 }
309}
310
311
312void RelocInfoWriter::Write(const RelocInfo* rinfo) {
313#ifdef DEBUG
314 byte* begin_pos = pos_;
315#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000316 ASSERT(rinfo->pc() - last_pc_ >= 0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000317 ASSERT(RelocInfo::NUMBER_OF_MODES - RelocInfo::LAST_COMPACT_ENUM <=
318 kMaxRelocModes);
Steve Blocka7e24c12009-10-30 11:49:00 +0000319 // Use unsigned delta-encoding for pc.
Steve Blockd0582a62009-12-15 09:54:21 +0000320 uint32_t pc_delta = static_cast<uint32_t>(rinfo->pc() - last_pc_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000321 RelocInfo::Mode rmode = rinfo->rmode();
322
323 // The two most common modes are given small tags, and usually fit in a byte.
324 if (rmode == RelocInfo::EMBEDDED_OBJECT) {
325 WriteTaggedPC(pc_delta, kEmbeddedObjectTag);
326 } else if (rmode == RelocInfo::CODE_TARGET) {
327 WriteTaggedPC(pc_delta, kCodeTargetTag);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100328 ASSERT(begin_pos - pos_ <= RelocInfo::kMaxCallSize);
Ben Murdoch257744e2011-11-30 15:57:28 +0000329 } else if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
330 // Use signed delta-encoding for id.
331 ASSERT(static_cast<int>(rinfo->data()) == rinfo->data());
332 int id_delta = static_cast<int>(rinfo->data()) - last_id_;
333 // Check if delta is small enough to fit in a tagged byte.
334 if (is_intn(id_delta, kSmallDataBits)) {
335 WriteTaggedPC(pc_delta, kLocatableTag);
336 WriteTaggedData(id_delta, kCodeWithIdTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000337 } else {
338 // Otherwise, use costly encoding.
Ben Murdoch257744e2011-11-30 15:57:28 +0000339 WriteExtraTaggedPC(pc_delta, kPCJumpExtraTag);
340 WriteExtraTaggedIntData(id_delta, kCodeWithIdTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000341 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000342 last_id_ = static_cast<int>(rinfo->data());
343 } else if (RelocInfo::IsPosition(rmode)) {
344 // Use signed delta-encoding for position.
345 ASSERT(static_cast<int>(rinfo->data()) == rinfo->data());
346 int pos_delta = static_cast<int>(rinfo->data()) - last_position_;
347 int pos_type_tag = (rmode == RelocInfo::POSITION) ? kNonstatementPositionTag
348 : kStatementPositionTag;
349 // Check if delta is small enough to fit in a tagged byte.
350 if (is_intn(pos_delta, kSmallDataBits)) {
351 WriteTaggedPC(pc_delta, kLocatableTag);
352 WriteTaggedData(pos_delta, pos_type_tag);
353 } else {
354 // Otherwise, use costly encoding.
355 WriteExtraTaggedPC(pc_delta, kPCJumpExtraTag);
356 WriteExtraTaggedIntData(pos_delta, pos_type_tag);
357 }
358 last_position_ = static_cast<int>(rinfo->data());
Steve Blocka7e24c12009-10-30 11:49:00 +0000359 } else if (RelocInfo::IsComment(rmode)) {
360 // Comments are normally not generated, so we use the costly encoding.
Ben Murdoch257744e2011-11-30 15:57:28 +0000361 WriteExtraTaggedPC(pc_delta, kPCJumpExtraTag);
362 WriteExtraTaggedData(rinfo->data(), kCommentTag);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100363 ASSERT(begin_pos - pos_ >= RelocInfo::kMinRelocCommentSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000364 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +0000365 ASSERT(rmode > RelocInfo::LAST_COMPACT_ENUM);
366 int saved_mode = rmode - RelocInfo::LAST_COMPACT_ENUM;
Steve Blocka7e24c12009-10-30 11:49:00 +0000367 // For all other modes we simply use the mode as the extra tag.
368 // None of these modes need a data component.
Ben Murdoch257744e2011-11-30 15:57:28 +0000369 ASSERT(saved_mode < kPCJumpExtraTag && saved_mode < kDataJumpExtraTag);
370 WriteExtraTaggedPC(pc_delta, saved_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000371 }
372 last_pc_ = rinfo->pc();
373#ifdef DEBUG
374 ASSERT(begin_pos - pos_ <= kMaxSize);
375#endif
376}
377
378
379inline int RelocIterator::AdvanceGetTag() {
380 return *--pos_ & kTagMask;
381}
382
383
384inline int RelocIterator::GetExtraTag() {
385 return (*pos_ >> kTagBits) & ((1 << kExtraTagBits) - 1);
386}
387
388
389inline int RelocIterator::GetTopTag() {
390 return *pos_ >> (kTagBits + kExtraTagBits);
391}
392
393
394inline void RelocIterator::ReadTaggedPC() {
395 rinfo_.pc_ += *pos_ >> kTagBits;
396}
397
398
399inline void RelocIterator::AdvanceReadPC() {
400 rinfo_.pc_ += *--pos_;
401}
402
403
Ben Murdoch257744e2011-11-30 15:57:28 +0000404void RelocIterator::AdvanceReadId() {
405 int x = 0;
406 for (int i = 0; i < kIntSize; i++) {
407 x |= static_cast<int>(*--pos_) << i * kBitsPerByte;
408 }
409 last_id_ += x;
410 rinfo_.data_ = last_id_;
411}
412
413
414void RelocIterator::AdvanceReadPosition() {
415 int x = 0;
416 for (int i = 0; i < kIntSize; i++) {
417 x |= static_cast<int>(*--pos_) << i * kBitsPerByte;
418 }
419 last_position_ += x;
420 rinfo_.data_ = last_position_;
421}
422
423
Steve Blocka7e24c12009-10-30 11:49:00 +0000424void RelocIterator::AdvanceReadData() {
425 intptr_t x = 0;
426 for (int i = 0; i < kIntptrSize; i++) {
427 x |= static_cast<intptr_t>(*--pos_) << i * kBitsPerByte;
428 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000429 rinfo_.data_ = x;
Steve Blocka7e24c12009-10-30 11:49:00 +0000430}
431
432
433void RelocIterator::AdvanceReadVariableLengthPCJump() {
434 // Read the 32-kSmallPCDeltaBits most significant bits of the
435 // pc jump in kChunkBits bit chunks and shift them into place.
436 // Stop when the last chunk is encountered.
437 uint32_t pc_jump = 0;
438 for (int i = 0; i < kIntSize; i++) {
439 byte pc_jump_part = *--pos_;
440 pc_jump |= (pc_jump_part >> kLastChunkTagBits) << i * kChunkBits;
441 if ((pc_jump_part & kLastChunkTagMask) == 1) break;
442 }
443 // The least significant kSmallPCDeltaBits bits will be added
444 // later.
445 rinfo_.pc_ += pc_jump << kSmallPCDeltaBits;
446}
447
448
Ben Murdoch257744e2011-11-30 15:57:28 +0000449inline int RelocIterator::GetLocatableTypeTag() {
450 return *pos_ & ((1 << kLocatableTypeTagBits) - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000451}
452
453
Ben Murdoch257744e2011-11-30 15:57:28 +0000454inline void RelocIterator::ReadTaggedId() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000455 int8_t signed_b = *pos_;
456 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
Ben Murdoch257744e2011-11-30 15:57:28 +0000457 last_id_ += signed_b >> kLocatableTypeTagBits;
458 rinfo_.data_ = last_id_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000459}
460
461
Ben Murdoch257744e2011-11-30 15:57:28 +0000462inline void RelocIterator::ReadTaggedPosition() {
463 int8_t signed_b = *pos_;
464 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
465 last_position_ += signed_b >> kLocatableTypeTagBits;
466 rinfo_.data_ = last_position_;
467}
468
469
470static inline RelocInfo::Mode GetPositionModeFromTag(int tag) {
471 ASSERT(tag == kNonstatementPositionTag ||
472 tag == kStatementPositionTag);
473 return (tag == kNonstatementPositionTag) ?
474 RelocInfo::POSITION :
475 RelocInfo::STATEMENT_POSITION;
Steve Blocka7e24c12009-10-30 11:49:00 +0000476}
477
478
479void RelocIterator::next() {
480 ASSERT(!done());
481 // Basically, do the opposite of RelocInfoWriter::Write.
482 // Reading of data is as far as possible avoided for unwanted modes,
483 // but we must always update the pc.
484 //
485 // We exit this loop by returning when we find a mode we want.
486 while (pos_ > end_) {
487 int tag = AdvanceGetTag();
488 if (tag == kEmbeddedObjectTag) {
489 ReadTaggedPC();
490 if (SetMode(RelocInfo::EMBEDDED_OBJECT)) return;
491 } else if (tag == kCodeTargetTag) {
492 ReadTaggedPC();
Steve Blocka7e24c12009-10-30 11:49:00 +0000493 if (SetMode(RelocInfo::CODE_TARGET)) return;
Ben Murdoch257744e2011-11-30 15:57:28 +0000494 } else if (tag == kLocatableTag) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000495 ReadTaggedPC();
496 Advance();
Ben Murdoch257744e2011-11-30 15:57:28 +0000497 int locatable_tag = GetLocatableTypeTag();
498 if (locatable_tag == kCodeWithIdTag) {
499 if (SetMode(RelocInfo::CODE_TARGET_WITH_ID)) {
500 ReadTaggedId();
501 return;
502 }
503 } else {
504 // Compact encoding is never used for comments,
505 // so it must be a position.
506 ASSERT(locatable_tag == kNonstatementPositionTag ||
507 locatable_tag == kStatementPositionTag);
508 if (mode_mask_ & RelocInfo::kPositionMask) {
509 ReadTaggedPosition();
510 if (SetMode(GetPositionModeFromTag(locatable_tag))) return;
511 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000512 }
513 } else {
514 ASSERT(tag == kDefaultTag);
515 int extra_tag = GetExtraTag();
Ben Murdoch257744e2011-11-30 15:57:28 +0000516 if (extra_tag == kPCJumpExtraTag) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000517 int top_tag = GetTopTag();
518 if (top_tag == kVariableLengthPCJumpTopTag) {
519 AdvanceReadVariableLengthPCJump();
520 } else {
521 AdvanceReadPC();
522 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000523 } else if (extra_tag == kDataJumpExtraTag) {
524 int locatable_tag = GetTopTag();
525 if (locatable_tag == kCodeWithIdTag) {
526 if (SetMode(RelocInfo::CODE_TARGET_WITH_ID)) {
527 AdvanceReadId();
528 return;
529 }
530 Advance(kIntSize);
531 } else if (locatable_tag != kCommentTag) {
532 ASSERT(locatable_tag == kNonstatementPositionTag ||
533 locatable_tag == kStatementPositionTag);
534 if (mode_mask_ & RelocInfo::kPositionMask) {
535 AdvanceReadPosition();
536 if (SetMode(GetPositionModeFromTag(locatable_tag))) return;
537 } else {
538 Advance(kIntSize);
539 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000540 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +0000541 ASSERT(locatable_tag == kCommentTag);
542 if (SetMode(RelocInfo::COMMENT)) {
543 AdvanceReadData();
544 return;
545 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000546 Advance(kIntptrSize);
547 }
548 } else {
549 AdvanceReadPC();
Ben Murdoch257744e2011-11-30 15:57:28 +0000550 int rmode = extra_tag + RelocInfo::LAST_COMPACT_ENUM;
551 if (SetMode(static_cast<RelocInfo::Mode>(rmode))) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000552 }
553 }
554 }
555 done_ = true;
556}
557
558
559RelocIterator::RelocIterator(Code* code, int mode_mask) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100560 rinfo_.host_ = code;
Steve Blocka7e24c12009-10-30 11:49:00 +0000561 rinfo_.pc_ = code->instruction_start();
562 rinfo_.data_ = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100563 // Relocation info is read backwards.
Steve Blocka7e24c12009-10-30 11:49:00 +0000564 pos_ = code->relocation_start() + code->relocation_size();
565 end_ = code->relocation_start();
566 done_ = false;
567 mode_mask_ = mode_mask;
Ben Murdoch257744e2011-11-30 15:57:28 +0000568 last_id_ = 0;
569 last_position_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000570 if (mode_mask_ == 0) pos_ = end_;
571 next();
572}
573
574
575RelocIterator::RelocIterator(const CodeDesc& desc, int mode_mask) {
576 rinfo_.pc_ = desc.buffer;
577 rinfo_.data_ = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100578 // Relocation info is read backwards.
Steve Blocka7e24c12009-10-30 11:49:00 +0000579 pos_ = desc.buffer + desc.buffer_size;
580 end_ = pos_ - desc.reloc_size;
581 done_ = false;
582 mode_mask_ = mode_mask;
Ben Murdoch257744e2011-11-30 15:57:28 +0000583 last_id_ = 0;
584 last_position_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000585 if (mode_mask_ == 0) pos_ = end_;
586 next();
587}
588
589
590// -----------------------------------------------------------------------------
591// Implementation of RelocInfo
592
593
594#ifdef ENABLE_DISASSEMBLER
595const char* RelocInfo::RelocModeName(RelocInfo::Mode rmode) {
596 switch (rmode) {
597 case RelocInfo::NONE:
598 return "no reloc";
599 case RelocInfo::EMBEDDED_OBJECT:
600 return "embedded object";
Steve Blocka7e24c12009-10-30 11:49:00 +0000601 case RelocInfo::CONSTRUCT_CALL:
602 return "code target (js construct call)";
603 case RelocInfo::CODE_TARGET_CONTEXT:
604 return "code target (context)";
Andrei Popescu402d9372010-02-26 13:31:12 +0000605 case RelocInfo::DEBUG_BREAK:
606#ifndef ENABLE_DEBUGGER_SUPPORT
607 UNREACHABLE();
608#endif
609 return "debug break";
Steve Blocka7e24c12009-10-30 11:49:00 +0000610 case RelocInfo::CODE_TARGET:
611 return "code target";
Ben Murdoch257744e2011-11-30 15:57:28 +0000612 case RelocInfo::CODE_TARGET_WITH_ID:
613 return "code target with id";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100614 case RelocInfo::GLOBAL_PROPERTY_CELL:
615 return "global property cell";
Steve Blocka7e24c12009-10-30 11:49:00 +0000616 case RelocInfo::RUNTIME_ENTRY:
617 return "runtime entry";
618 case RelocInfo::JS_RETURN:
619 return "js return";
620 case RelocInfo::COMMENT:
621 return "comment";
622 case RelocInfo::POSITION:
623 return "position";
624 case RelocInfo::STATEMENT_POSITION:
625 return "statement position";
626 case RelocInfo::EXTERNAL_REFERENCE:
627 return "external reference";
628 case RelocInfo::INTERNAL_REFERENCE:
629 return "internal reference";
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100630 case RelocInfo::DEBUG_BREAK_SLOT:
631#ifndef ENABLE_DEBUGGER_SUPPORT
632 UNREACHABLE();
633#endif
634 return "debug break slot";
Steve Blocka7e24c12009-10-30 11:49:00 +0000635 case RelocInfo::NUMBER_OF_MODES:
636 UNREACHABLE();
637 return "number_of_modes";
638 }
639 return "unknown relocation type";
640}
641
642
Ben Murdochb0fe1622011-05-05 13:52:32 +0100643void RelocInfo::Print(FILE* out) {
644 PrintF(out, "%p %s", pc_, RelocModeName(rmode_));
Steve Blocka7e24c12009-10-30 11:49:00 +0000645 if (IsComment(rmode_)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100646 PrintF(out, " (%s)", reinterpret_cast<char*>(data_));
Steve Blocka7e24c12009-10-30 11:49:00 +0000647 } else if (rmode_ == EMBEDDED_OBJECT) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100648 PrintF(out, " (");
649 target_object()->ShortPrint(out);
650 PrintF(out, ")");
Steve Blocka7e24c12009-10-30 11:49:00 +0000651 } else if (rmode_ == EXTERNAL_REFERENCE) {
652 ExternalReferenceEncoder ref_encoder;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100653 PrintF(out, " (%s) (%p)",
Steve Blocka7e24c12009-10-30 11:49:00 +0000654 ref_encoder.NameOfAddress(*target_reference_address()),
655 *target_reference_address());
656 } else if (IsCodeTarget(rmode_)) {
657 Code* code = Code::GetCodeFromTargetAddress(target_address());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100658 PrintF(out, " (%s) (%p)", Code::Kind2String(code->kind()),
659 target_address());
Ben Murdoch257744e2011-11-30 15:57:28 +0000660 if (rmode_ == CODE_TARGET_WITH_ID) {
661 PrintF(" (id=%d)", static_cast<int>(data_));
662 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000663 } else if (IsPosition(rmode_)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100664 PrintF(out, " (%" V8_PTR_PREFIX "d)", data());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100665 } else if (rmode_ == RelocInfo::RUNTIME_ENTRY &&
666 Isolate::Current()->deoptimizer_data() != NULL) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100667 // Depotimization bailouts are stored as runtime entries.
668 int id = Deoptimizer::GetDeoptimizationId(
669 target_address(), Deoptimizer::EAGER);
670 if (id != Deoptimizer::kNotDeoptimizationEntry) {
671 PrintF(out, " (deoptimization bailout %d)", id);
672 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000673 }
674
Ben Murdochb0fe1622011-05-05 13:52:32 +0100675 PrintF(out, "\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000676}
677#endif // ENABLE_DISASSEMBLER
678
679
680#ifdef DEBUG
681void RelocInfo::Verify() {
682 switch (rmode_) {
683 case EMBEDDED_OBJECT:
684 Object::VerifyPointer(target_object());
685 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100686 case GLOBAL_PROPERTY_CELL:
687 Object::VerifyPointer(target_cell());
688 break;
Andrei Popescu402d9372010-02-26 13:31:12 +0000689 case DEBUG_BREAK:
690#ifndef ENABLE_DEBUGGER_SUPPORT
691 UNREACHABLE();
692 break;
693#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000694 case CONSTRUCT_CALL:
695 case CODE_TARGET_CONTEXT:
Ben Murdoch257744e2011-11-30 15:57:28 +0000696 case CODE_TARGET_WITH_ID:
Steve Blocka7e24c12009-10-30 11:49:00 +0000697 case CODE_TARGET: {
698 // convert inline target address to code object
699 Address addr = target_address();
700 ASSERT(addr != NULL);
701 // Check that we can find the right code object.
702 Code* code = Code::GetCodeFromTargetAddress(addr);
Steve Block44f0eee2011-05-26 01:26:41 +0100703 Object* found = HEAP->FindCodeObject(addr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000704 ASSERT(found->IsCode());
705 ASSERT(code->address() == HeapObject::cast(found)->address());
706 break;
707 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000708 case RUNTIME_ENTRY:
709 case JS_RETURN:
710 case COMMENT:
711 case POSITION:
712 case STATEMENT_POSITION:
713 case EXTERNAL_REFERENCE:
714 case INTERNAL_REFERENCE:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100715 case DEBUG_BREAK_SLOT:
Steve Blocka7e24c12009-10-30 11:49:00 +0000716 case NONE:
717 break;
718 case NUMBER_OF_MODES:
719 UNREACHABLE();
720 break;
721 }
722}
723#endif // DEBUG
724
725
726// -----------------------------------------------------------------------------
727// Implementation of ExternalReference
728
Steve Block44f0eee2011-05-26 01:26:41 +0100729ExternalReference::ExternalReference(Builtins::CFunctionId id, Isolate* isolate)
730 : address_(Redirect(isolate, Builtins::c_function_address(id))) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000731
732
Steve Block1e0659c2011-05-24 12:43:12 +0100733ExternalReference::ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +0100734 ApiFunction* fun,
735 Type type = ExternalReference::BUILTIN_CALL,
736 Isolate* isolate = NULL)
737 : address_(Redirect(isolate, fun->address(), type)) {}
Steve Blockd0582a62009-12-15 09:54:21 +0000738
739
Steve Block44f0eee2011-05-26 01:26:41 +0100740ExternalReference::ExternalReference(Builtins::Name name, Isolate* isolate)
741 : address_(isolate->builtins()->builtin_address(name)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000742
743
Steve Block44f0eee2011-05-26 01:26:41 +0100744ExternalReference::ExternalReference(Runtime::FunctionId id,
745 Isolate* isolate)
746 : address_(Redirect(isolate, Runtime::FunctionForId(id)->entry)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000747
748
Steve Block44f0eee2011-05-26 01:26:41 +0100749ExternalReference::ExternalReference(const Runtime::Function* f,
750 Isolate* isolate)
751 : address_(Redirect(isolate, f->entry)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000752
753
Steve Block44f0eee2011-05-26 01:26:41 +0100754ExternalReference ExternalReference::isolate_address() {
755 return ExternalReference(Isolate::Current());
756}
757
758
759ExternalReference::ExternalReference(const IC_Utility& ic_utility,
760 Isolate* isolate)
761 : address_(Redirect(isolate, ic_utility.address())) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000762
763#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100764ExternalReference::ExternalReference(const Debug_Address& debug_address,
765 Isolate* isolate)
766 : address_(debug_address.address(isolate)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000767#endif
768
769ExternalReference::ExternalReference(StatsCounter* counter)
770 : address_(reinterpret_cast<Address>(counter->GetInternalPointer())) {}
771
772
Steve Block44f0eee2011-05-26 01:26:41 +0100773ExternalReference::ExternalReference(Isolate::AddressId id, Isolate* isolate)
774 : address_(isolate->get_address_from_id(id)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000775
776
777ExternalReference::ExternalReference(const SCTableReference& table_ref)
778 : address_(table_ref.address()) {}
779
780
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100781ExternalReference ExternalReference::
782 incremental_marking_record_write_function(Isolate* isolate) {
783 return ExternalReference(Redirect(
784 isolate,
785 FUNCTION_ADDR(IncrementalMarking::RecordWriteFromCode)));
786}
787
788
789ExternalReference ExternalReference::
790 incremental_evacuation_record_write_function(Isolate* isolate) {
791 return ExternalReference(Redirect(
792 isolate,
793 FUNCTION_ADDR(IncrementalMarking::RecordWriteForEvacuationFromCode)));
794}
795
796
797ExternalReference ExternalReference::
798 store_buffer_overflow_function(Isolate* isolate) {
799 return ExternalReference(Redirect(
800 isolate,
801 FUNCTION_ADDR(StoreBuffer::StoreBufferOverflow)));
802}
803
804
805ExternalReference ExternalReference::flush_icache_function(Isolate* isolate) {
806 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(CPU::FlushICache)));
807}
808
809
Steve Block44f0eee2011-05-26 01:26:41 +0100810ExternalReference ExternalReference::perform_gc_function(Isolate* isolate) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100811 return
812 ExternalReference(Redirect(isolate, FUNCTION_ADDR(Runtime::PerformGC)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000813}
814
815
Steve Block44f0eee2011-05-26 01:26:41 +0100816ExternalReference ExternalReference::fill_heap_number_with_random_function(
817 Isolate* isolate) {
818 return ExternalReference(Redirect(
819 isolate,
820 FUNCTION_ADDR(V8::FillHeapNumberWithRandom)));
Steve Block6ded16b2010-05-10 14:33:55 +0100821}
822
823
Steve Block44f0eee2011-05-26 01:26:41 +0100824ExternalReference ExternalReference::delete_handle_scope_extensions(
825 Isolate* isolate) {
826 return ExternalReference(Redirect(
827 isolate,
828 FUNCTION_ADDR(HandleScope::DeleteExtensions)));
John Reck59135872010-11-02 12:39:01 -0700829}
830
831
Steve Block44f0eee2011-05-26 01:26:41 +0100832ExternalReference ExternalReference::random_uint32_function(
833 Isolate* isolate) {
834 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(V8::Random)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000835}
836
837
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100838ExternalReference ExternalReference::get_date_field_function(
839 Isolate* isolate) {
840 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(JSDate::GetField)));
841}
842
843
844ExternalReference ExternalReference::date_cache_stamp(Isolate* isolate) {
845 return ExternalReference(isolate->date_cache()->stamp_address());
846}
847
848
Steve Block44f0eee2011-05-26 01:26:41 +0100849ExternalReference ExternalReference::transcendental_cache_array_address(
850 Isolate* isolate) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100851 return ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +0100852 isolate->transcendental_cache()->cache_array_address());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100853}
854
855
Steve Block44f0eee2011-05-26 01:26:41 +0100856ExternalReference ExternalReference::new_deoptimizer_function(
857 Isolate* isolate) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100858 return ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +0100859 Redirect(isolate, FUNCTION_ADDR(Deoptimizer::New)));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100860}
861
862
Steve Block44f0eee2011-05-26 01:26:41 +0100863ExternalReference ExternalReference::compute_output_frames_function(
864 Isolate* isolate) {
865 return ExternalReference(
866 Redirect(isolate, FUNCTION_ADDR(Deoptimizer::ComputeOutputFrames)));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100867}
868
869
Steve Block44f0eee2011-05-26 01:26:41 +0100870ExternalReference ExternalReference::keyed_lookup_cache_keys(Isolate* isolate) {
871 return ExternalReference(isolate->keyed_lookup_cache()->keys_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000872}
873
874
Steve Block44f0eee2011-05-26 01:26:41 +0100875ExternalReference ExternalReference::keyed_lookup_cache_field_offsets(
876 Isolate* isolate) {
877 return ExternalReference(
878 isolate->keyed_lookup_cache()->field_offsets_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000879}
880
881
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100882ExternalReference ExternalReference::roots_array_start(Isolate* isolate) {
883 return ExternalReference(isolate->heap()->roots_array_start());
Steve Blocka7e24c12009-10-30 11:49:00 +0000884}
885
886
Steve Block44f0eee2011-05-26 01:26:41 +0100887ExternalReference ExternalReference::address_of_stack_limit(Isolate* isolate) {
888 return ExternalReference(isolate->stack_guard()->address_of_jslimit());
Steve Blockd0582a62009-12-15 09:54:21 +0000889}
890
891
Steve Block44f0eee2011-05-26 01:26:41 +0100892ExternalReference ExternalReference::address_of_real_stack_limit(
893 Isolate* isolate) {
894 return ExternalReference(isolate->stack_guard()->address_of_real_jslimit());
Steve Blocka7e24c12009-10-30 11:49:00 +0000895}
896
897
Steve Block44f0eee2011-05-26 01:26:41 +0100898ExternalReference ExternalReference::address_of_regexp_stack_limit(
899 Isolate* isolate) {
900 return ExternalReference(isolate->regexp_stack()->limit_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000901}
902
903
Steve Block44f0eee2011-05-26 01:26:41 +0100904ExternalReference ExternalReference::new_space_start(Isolate* isolate) {
905 return ExternalReference(isolate->heap()->NewSpaceStart());
Andrei Popescu402d9372010-02-26 13:31:12 +0000906}
907
908
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100909ExternalReference ExternalReference::store_buffer_top(Isolate* isolate) {
910 return ExternalReference(isolate->heap()->store_buffer()->TopAddress());
911}
912
913
Steve Block44f0eee2011-05-26 01:26:41 +0100914ExternalReference ExternalReference::new_space_mask(Isolate* isolate) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100915 return ExternalReference(reinterpret_cast<Address>(
916 isolate->heap()->NewSpaceMask()));
Steve Blocka7e24c12009-10-30 11:49:00 +0000917}
918
919
Steve Block44f0eee2011-05-26 01:26:41 +0100920ExternalReference ExternalReference::new_space_allocation_top_address(
921 Isolate* isolate) {
922 return ExternalReference(isolate->heap()->NewSpaceAllocationTopAddress());
Steve Blocka7e24c12009-10-30 11:49:00 +0000923}
924
925
Steve Block44f0eee2011-05-26 01:26:41 +0100926ExternalReference ExternalReference::heap_always_allocate_scope_depth(
927 Isolate* isolate) {
928 Heap* heap = isolate->heap();
929 return ExternalReference(heap->always_allocate_scope_depth_address());
930}
931
932
933ExternalReference ExternalReference::new_space_allocation_limit_address(
934 Isolate* isolate) {
935 return ExternalReference(isolate->heap()->NewSpaceAllocationLimitAddress());
Steve Blocka7e24c12009-10-30 11:49:00 +0000936}
937
Steve Blockd0582a62009-12-15 09:54:21 +0000938
John Reck59135872010-11-02 12:39:01 -0700939ExternalReference ExternalReference::handle_scope_level_address() {
940 return ExternalReference(HandleScope::current_level_address());
Steve Blockd0582a62009-12-15 09:54:21 +0000941}
942
943
944ExternalReference ExternalReference::handle_scope_next_address() {
945 return ExternalReference(HandleScope::current_next_address());
946}
947
948
949ExternalReference ExternalReference::handle_scope_limit_address() {
950 return ExternalReference(HandleScope::current_limit_address());
951}
952
953
Steve Block44f0eee2011-05-26 01:26:41 +0100954ExternalReference ExternalReference::scheduled_exception_address(
955 Isolate* isolate) {
956 return ExternalReference(isolate->scheduled_exception_address());
Steve Blockd0582a62009-12-15 09:54:21 +0000957}
958
959
Ben Murdochb0fe1622011-05-05 13:52:32 +0100960ExternalReference ExternalReference::address_of_min_int() {
961 return ExternalReference(reinterpret_cast<void*>(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100962 &double_constants.Pointer()->min_int));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100963}
964
965
966ExternalReference ExternalReference::address_of_one_half() {
967 return ExternalReference(reinterpret_cast<void*>(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100968 &double_constants.Pointer()->one_half));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100969}
970
971
Ben Murdochb8e0da22011-05-16 14:20:40 +0100972ExternalReference ExternalReference::address_of_minus_zero() {
973 return ExternalReference(reinterpret_cast<void*>(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100974 &double_constants.Pointer()->minus_zero));
Ben Murdochb8e0da22011-05-16 14:20:40 +0100975}
976
977
Ben Murdoch257744e2011-11-30 15:57:28 +0000978ExternalReference ExternalReference::address_of_zero() {
979 return ExternalReference(reinterpret_cast<void*>(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100980 &double_constants.Pointer()->zero));
Ben Murdoch257744e2011-11-30 15:57:28 +0000981}
982
983
984ExternalReference ExternalReference::address_of_uint8_max_value() {
985 return ExternalReference(reinterpret_cast<void*>(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100986 &double_constants.Pointer()->uint8_max_value));
Ben Murdoch257744e2011-11-30 15:57:28 +0000987}
988
989
Ben Murdochb0fe1622011-05-05 13:52:32 +0100990ExternalReference ExternalReference::address_of_negative_infinity() {
991 return ExternalReference(reinterpret_cast<void*>(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100992 &double_constants.Pointer()->negative_infinity));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100993}
994
995
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000996ExternalReference ExternalReference::address_of_canonical_non_hole_nan() {
Steve Block44f0eee2011-05-26 01:26:41 +0100997 return ExternalReference(reinterpret_cast<void*>(
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100998 &double_constants.Pointer()->canonical_non_hole_nan));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000999}
1000
1001
1002ExternalReference ExternalReference::address_of_the_hole_nan() {
1003 return ExternalReference(reinterpret_cast<void*>(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001004 &double_constants.Pointer()->the_hole_nan));
Steve Block44f0eee2011-05-26 01:26:41 +01001005}
1006
1007
Steve Block6ded16b2010-05-10 14:33:55 +01001008#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +00001009
Steve Block44f0eee2011-05-26 01:26:41 +01001010ExternalReference ExternalReference::re_check_stack_guard_state(
1011 Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001012 Address function;
1013#ifdef V8_TARGET_ARCH_X64
1014 function = FUNCTION_ADDR(RegExpMacroAssemblerX64::CheckStackGuardState);
1015#elif V8_TARGET_ARCH_IA32
1016 function = FUNCTION_ADDR(RegExpMacroAssemblerIA32::CheckStackGuardState);
1017#elif V8_TARGET_ARCH_ARM
1018 function = FUNCTION_ADDR(RegExpMacroAssemblerARM::CheckStackGuardState);
Steve Block44f0eee2011-05-26 01:26:41 +01001019#elif V8_TARGET_ARCH_MIPS
1020 function = FUNCTION_ADDR(RegExpMacroAssemblerMIPS::CheckStackGuardState);
Steve Blocka7e24c12009-10-30 11:49:00 +00001021#else
Leon Clarke4515c472010-02-03 11:58:03 +00001022 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +00001023#endif
Steve Block44f0eee2011-05-26 01:26:41 +01001024 return ExternalReference(Redirect(isolate, function));
Steve Blocka7e24c12009-10-30 11:49:00 +00001025}
1026
Steve Block44f0eee2011-05-26 01:26:41 +01001027ExternalReference ExternalReference::re_grow_stack(Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001028 return ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01001029 Redirect(isolate, FUNCTION_ADDR(NativeRegExpMacroAssembler::GrowStack)));
Steve Blocka7e24c12009-10-30 11:49:00 +00001030}
1031
Steve Block44f0eee2011-05-26 01:26:41 +01001032ExternalReference ExternalReference::re_case_insensitive_compare_uc16(
1033 Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001034 return ExternalReference(Redirect(
Steve Block44f0eee2011-05-26 01:26:41 +01001035 isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +00001036 FUNCTION_ADDR(NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16)));
1037}
1038
Leon Clarkee46be812010-01-19 14:06:41 +00001039ExternalReference ExternalReference::re_word_character_map() {
1040 return ExternalReference(
1041 NativeRegExpMacroAssembler::word_character_map_address());
1042}
1043
Steve Block44f0eee2011-05-26 01:26:41 +01001044ExternalReference ExternalReference::address_of_static_offsets_vector(
1045 Isolate* isolate) {
1046 return ExternalReference(
1047 OffsetsVector::static_offsets_vector_address(isolate));
Leon Clarkee46be812010-01-19 14:06:41 +00001048}
1049
Steve Block44f0eee2011-05-26 01:26:41 +01001050ExternalReference ExternalReference::address_of_regexp_stack_memory_address(
1051 Isolate* isolate) {
1052 return ExternalReference(
1053 isolate->regexp_stack()->memory_address());
Leon Clarkee46be812010-01-19 14:06:41 +00001054}
1055
Steve Block44f0eee2011-05-26 01:26:41 +01001056ExternalReference ExternalReference::address_of_regexp_stack_memory_size(
1057 Isolate* isolate) {
1058 return ExternalReference(isolate->regexp_stack()->memory_size_address());
Leon Clarkee46be812010-01-19 14:06:41 +00001059}
1060
Steve Block6ded16b2010-05-10 14:33:55 +01001061#endif // V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +00001062
1063
1064static double add_two_doubles(double x, double y) {
1065 return x + y;
1066}
1067
1068
1069static double sub_two_doubles(double x, double y) {
1070 return x - y;
1071}
1072
1073
1074static double mul_two_doubles(double x, double y) {
1075 return x * y;
1076}
1077
1078
1079static double div_two_doubles(double x, double y) {
1080 return x / y;
1081}
1082
1083
1084static double mod_two_doubles(double x, double y) {
Leon Clarkee46be812010-01-19 14:06:41 +00001085 return modulo(x, y);
Steve Blocka7e24c12009-10-30 11:49:00 +00001086}
1087
1088
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001089static double math_sin_double(double x) {
1090 return sin(x);
1091}
1092
1093
1094static double math_cos_double(double x) {
1095 return cos(x);
1096}
1097
1098
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001099static double math_tan_double(double x) {
1100 return tan(x);
1101}
1102
1103
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001104static double math_log_double(double x) {
1105 return log(x);
1106}
1107
1108
Steve Block44f0eee2011-05-26 01:26:41 +01001109ExternalReference ExternalReference::math_sin_double_function(
1110 Isolate* isolate) {
1111 return ExternalReference(Redirect(isolate,
1112 FUNCTION_ADDR(math_sin_double),
Ben Murdoch257744e2011-11-30 15:57:28 +00001113 BUILTIN_FP_CALL));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001114}
1115
1116
Steve Block44f0eee2011-05-26 01:26:41 +01001117ExternalReference ExternalReference::math_cos_double_function(
1118 Isolate* isolate) {
1119 return ExternalReference(Redirect(isolate,
1120 FUNCTION_ADDR(math_cos_double),
Ben Murdoch257744e2011-11-30 15:57:28 +00001121 BUILTIN_FP_CALL));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001122}
1123
1124
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001125ExternalReference ExternalReference::math_tan_double_function(
1126 Isolate* isolate) {
1127 return ExternalReference(Redirect(isolate,
1128 FUNCTION_ADDR(math_tan_double),
1129 BUILTIN_FP_CALL));
1130}
1131
1132
Steve Block44f0eee2011-05-26 01:26:41 +01001133ExternalReference ExternalReference::math_log_double_function(
1134 Isolate* isolate) {
1135 return ExternalReference(Redirect(isolate,
1136 FUNCTION_ADDR(math_log_double),
Ben Murdoch257744e2011-11-30 15:57:28 +00001137 BUILTIN_FP_CALL));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001138}
1139
1140
Ben Murdochb0fe1622011-05-05 13:52:32 +01001141// Helper function to compute x^y, where y is known to be an
1142// integer. Uses binary decomposition to limit the number of
1143// multiplications; see the discussion in "Hacker's Delight" by Henry
1144// S. Warren, Jr., figure 11-6, page 213.
1145double power_double_int(double x, int y) {
1146 double m = (y < 0) ? 1 / x : x;
1147 unsigned n = (y < 0) ? -y : y;
1148 double p = 1;
1149 while (n != 0) {
1150 if ((n & 1) != 0) p *= m;
1151 m *= m;
1152 if ((n & 2) != 0) p *= m;
1153 m *= m;
1154 n >>= 2;
1155 }
1156 return p;
1157}
1158
1159
1160double power_double_double(double x, double y) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001161 // The checks for special cases can be dropped in ia32 because it has already
1162 // been done in generated code before bailing out here.
1163 if (isnan(y) || ((x == 1 || x == -1) && isinf(y))) return OS::nan_value();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001164 return pow(x, y);
1165}
1166
1167
Steve Block44f0eee2011-05-26 01:26:41 +01001168ExternalReference ExternalReference::power_double_double_function(
1169 Isolate* isolate) {
1170 return ExternalReference(Redirect(isolate,
1171 FUNCTION_ADDR(power_double_double),
Ben Murdoch257744e2011-11-30 15:57:28 +00001172 BUILTIN_FP_FP_CALL));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001173}
1174
1175
Steve Block44f0eee2011-05-26 01:26:41 +01001176ExternalReference ExternalReference::power_double_int_function(
1177 Isolate* isolate) {
1178 return ExternalReference(Redirect(isolate,
1179 FUNCTION_ADDR(power_double_int),
Ben Murdoch257744e2011-11-30 15:57:28 +00001180 BUILTIN_FP_INT_CALL));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001181}
1182
1183
Leon Clarkee46be812010-01-19 14:06:41 +00001184static int native_compare_doubles(double y, double x) {
1185 if (x == y) return EQUAL;
1186 return x < y ? LESS : GREATER;
Steve Blocka7e24c12009-10-30 11:49:00 +00001187}
1188
1189
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001190bool EvalComparison(Token::Value op, double op1, double op2) {
1191 ASSERT(Token::IsCompareOp(op));
1192 switch (op) {
1193 case Token::EQ:
1194 case Token::EQ_STRICT: return (op1 == op2);
1195 case Token::NE: return (op1 != op2);
1196 case Token::LT: return (op1 < op2);
1197 case Token::GT: return (op1 > op2);
1198 case Token::LTE: return (op1 <= op2);
1199 case Token::GTE: return (op1 >= op2);
1200 default:
1201 UNREACHABLE();
1202 return false;
1203 }
1204}
1205
1206
Steve Blocka7e24c12009-10-30 11:49:00 +00001207ExternalReference ExternalReference::double_fp_operation(
Steve Block44f0eee2011-05-26 01:26:41 +01001208 Token::Value operation, Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001209 typedef double BinaryFPOperation(double x, double y);
1210 BinaryFPOperation* function = NULL;
1211 switch (operation) {
1212 case Token::ADD:
1213 function = &add_two_doubles;
1214 break;
1215 case Token::SUB:
1216 function = &sub_two_doubles;
1217 break;
1218 case Token::MUL:
1219 function = &mul_two_doubles;
1220 break;
1221 case Token::DIV:
1222 function = &div_two_doubles;
1223 break;
1224 case Token::MOD:
1225 function = &mod_two_doubles;
1226 break;
1227 default:
1228 UNREACHABLE();
1229 }
Steve Block44f0eee2011-05-26 01:26:41 +01001230 return ExternalReference(Redirect(isolate,
1231 FUNCTION_ADDR(function),
Ben Murdoch257744e2011-11-30 15:57:28 +00001232 BUILTIN_FP_FP_CALL));
Steve Blocka7e24c12009-10-30 11:49:00 +00001233}
1234
1235
Steve Block44f0eee2011-05-26 01:26:41 +01001236ExternalReference ExternalReference::compare_doubles(Isolate* isolate) {
1237 return ExternalReference(Redirect(isolate,
1238 FUNCTION_ADDR(native_compare_doubles),
Ben Murdoch257744e2011-11-30 15:57:28 +00001239 BUILTIN_COMPARE_CALL));
Steve Blocka7e24c12009-10-30 11:49:00 +00001240}
1241
1242
Steve Blocka7e24c12009-10-30 11:49:00 +00001243#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001244ExternalReference ExternalReference::debug_break(Isolate* isolate) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001245 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(Debug_Break)));
Steve Blocka7e24c12009-10-30 11:49:00 +00001246}
1247
1248
Steve Block44f0eee2011-05-26 01:26:41 +01001249ExternalReference ExternalReference::debug_step_in_fp_address(
1250 Isolate* isolate) {
1251 return ExternalReference(isolate->debug()->step_in_fp_addr());
Steve Blocka7e24c12009-10-30 11:49:00 +00001252}
1253#endif
1254
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001255
Ben Murdochb0fe1622011-05-05 13:52:32 +01001256void PositionsRecorder::RecordPosition(int pos) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001257 ASSERT(pos != RelocInfo::kNoPosition);
1258 ASSERT(pos >= 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001259 state_.current_position = pos;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001260#ifdef ENABLE_GDB_JIT_INTERFACE
1261 if (gdbjit_lineinfo_ != NULL) {
1262 gdbjit_lineinfo_->SetPosition(assembler_->pc_offset(), pos, false);
1263 }
1264#endif
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001265}
1266
1267
1268void PositionsRecorder::RecordStatementPosition(int pos) {
1269 ASSERT(pos != RelocInfo::kNoPosition);
1270 ASSERT(pos >= 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001271 state_.current_statement_position = pos;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001272#ifdef ENABLE_GDB_JIT_INTERFACE
1273 if (gdbjit_lineinfo_ != NULL) {
1274 gdbjit_lineinfo_->SetPosition(assembler_->pc_offset(), pos, true);
1275 }
1276#endif
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001277}
1278
1279
1280bool PositionsRecorder::WriteRecordedPositions() {
1281 bool written = false;
1282
1283 // Write the statement position if it is different from what was written last
1284 // time.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001285 if (state_.current_statement_position != state_.written_statement_position) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001286 EnsureSpace ensure_space(assembler_);
1287 assembler_->RecordRelocInfo(RelocInfo::STATEMENT_POSITION,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001288 state_.current_statement_position);
1289 state_.written_statement_position = state_.current_statement_position;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001290 written = true;
1291 }
1292
1293 // Write the position if it is different from what was written last time and
Ben Murdochb0fe1622011-05-05 13:52:32 +01001294 // also different from the written statement position.
1295 if (state_.current_position != state_.written_position &&
1296 state_.current_position != state_.written_statement_position) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001297 EnsureSpace ensure_space(assembler_);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001298 assembler_->RecordRelocInfo(RelocInfo::POSITION, state_.current_position);
1299 state_.written_position = state_.current_position;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001300 written = true;
1301 }
1302
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001303 // Return whether something was written.
1304 return written;
1305}
1306
Steve Blocka7e24c12009-10-30 11:49:00 +00001307} } // namespace v8::internal