blob: 3c7fc1c6313557b17494907794151eccce614f35 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright (c) 1994-2006 Sun Microsystems Inc.
2// 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 Murdoch257744e2011-11-30 15:57:28 +000033// Copyright 2011 the V8 project authors. All rights reserved.
Steve Blocka7e24c12009-10-30 11:49:00 +000034
35#include "v8.h"
36
37#include "arguments.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010038#include "deoptimizer.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000039#include "execution.h"
40#include "ic-inl.h"
41#include "factory.h"
42#include "runtime.h"
Ben Murdochb0fe1622011-05-05 13:52:32 +010043#include "runtime-profiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000044#include "serialize.h"
45#include "stub-cache.h"
46#include "regexp-stack.h"
47#include "ast.h"
48#include "regexp-macro-assembler.h"
Leon Clarkee46be812010-01-19 14:06:41 +000049#include "platform.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000050// Include native regexp-macro-assembler.
Steve Block6ded16b2010-05-10 14:33:55 +010051#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +000052#if V8_TARGET_ARCH_IA32
53#include "ia32/regexp-macro-assembler-ia32.h"
54#elif V8_TARGET_ARCH_X64
55#include "x64/regexp-macro-assembler-x64.h"
56#elif V8_TARGET_ARCH_ARM
57#include "arm/regexp-macro-assembler-arm.h"
Steve Block44f0eee2011-05-26 01:26:41 +010058#elif V8_TARGET_ARCH_MIPS
59#include "mips/regexp-macro-assembler-mips.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000060#else // Unknown architecture.
61#error "Unknown architecture."
62#endif // Target architecture.
Steve Block6ded16b2010-05-10 14:33:55 +010063#endif // V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +000064
65namespace v8 {
66namespace internal {
67
68
Ben Murdochb0fe1622011-05-05 13:52:32 +010069const double DoubleConstant::min_int = kMinInt;
70const double DoubleConstant::one_half = 0.5;
Ben Murdochb8e0da22011-05-16 14:20:40 +010071const double DoubleConstant::minus_zero = -0.0;
Ben Murdoch257744e2011-11-30 15:57:28 +000072const double DoubleConstant::uint8_max_value = 255;
73const double DoubleConstant::zero = 0.0;
Steve Block44f0eee2011-05-26 01:26:41 +010074const double DoubleConstant::nan = OS::nan_value();
Ben Murdochb0fe1622011-05-05 13:52:32 +010075const double DoubleConstant::negative_infinity = -V8_INFINITY;
Ben Murdoche0cee9b2011-05-25 10:26:03 +010076const char* RelocInfo::kFillerCommentString = "DEOPTIMIZATION PADDING";
Ben Murdochb0fe1622011-05-05 13:52:32 +010077
Steve Blocka7e24c12009-10-30 11:49:00 +000078// -----------------------------------------------------------------------------
Steve Block053d10c2011-06-13 19:13:29 +010079// Implementation of AssemblerBase
80
81AssemblerBase::AssemblerBase(Isolate* isolate)
82 : isolate_(isolate),
83 jit_cookie_(0) {
84 if (FLAG_mask_constants_with_cookie && isolate != NULL) {
85 jit_cookie_ = V8::RandomPrivate(isolate);
86 }
87}
88
89
90// -----------------------------------------------------------------------------
Steve Blocka7e24c12009-10-30 11:49:00 +000091// Implementation of Label
92
93int Label::pos() const {
94 if (pos_ < 0) return -pos_ - 1;
95 if (pos_ > 0) return pos_ - 1;
96 UNREACHABLE();
97 return 0;
98}
99
100
101// -----------------------------------------------------------------------------
102// Implementation of RelocInfoWriter and RelocIterator
103//
Ben Murdoch257744e2011-11-30 15:57:28 +0000104// Relocation information is written backwards in memory, from high addresses
105// towards low addresses, byte by byte. Therefore, in the encodings listed
106// below, the first byte listed it at the highest address, and successive
107// bytes in the record are at progressively lower addresses.
108//
Steve Blocka7e24c12009-10-30 11:49:00 +0000109// Encoding
110//
111// The most common modes are given single-byte encodings. Also, it is
112// easy to identify the type of reloc info and skip unwanted modes in
113// an iteration.
114//
Ben Murdoch257744e2011-11-30 15:57:28 +0000115// The encoding relies on the fact that there are fewer than 14
116// different non-compactly encoded relocation modes.
Steve Blocka7e24c12009-10-30 11:49:00 +0000117//
Ben Murdoch257744e2011-11-30 15:57:28 +0000118// The first byte of a relocation record has a tag in its low 2 bits:
119// Here are the record schemes, depending on the low tag and optional higher
120// tags.
Steve Blocka7e24c12009-10-30 11:49:00 +0000121//
Ben Murdoch257744e2011-11-30 15:57:28 +0000122// Low tag:
123// 00: embedded_object: [6-bit pc delta] 00
Steve Blocka7e24c12009-10-30 11:49:00 +0000124//
Ben Murdoch257744e2011-11-30 15:57:28 +0000125// 01: code_target: [6-bit pc delta] 01
Steve Blocka7e24c12009-10-30 11:49:00 +0000126//
Ben Murdoch257744e2011-11-30 15:57:28 +0000127// 10: short_data_record: [6-bit pc delta] 10 followed by
128// [6-bit data delta] [2-bit data type tag]
Steve Blocka7e24c12009-10-30 11:49:00 +0000129//
Ben Murdoch257744e2011-11-30 15:57:28 +0000130// 11: long_record [2-bit high tag][4 bit middle_tag] 11
131// followed by variable data depending on type.
Steve Blocka7e24c12009-10-30 11:49:00 +0000132//
Ben Murdoch257744e2011-11-30 15:57:28 +0000133// 2-bit data type tags, used in short_data_record and data_jump long_record:
134// code_target_with_id: 00
135// position: 01
136// statement_position: 10
137// comment: 11 (not used in short_data_record)
Steve Blocka7e24c12009-10-30 11:49:00 +0000138//
Ben Murdoch257744e2011-11-30 15:57:28 +0000139// Long record format:
140// 4-bit middle_tag:
141// 0000 - 1100 : Short record for RelocInfo::Mode middle_tag + 2
142// (The middle_tag encodes rmode - RelocInfo::LAST_COMPACT_ENUM,
143// and is between 0000 and 1100)
144// The format is:
145// 00 [4 bit middle_tag] 11 followed by
146// 00 [6 bit pc delta]
Steve Blocka7e24c12009-10-30 11:49:00 +0000147//
Ben Murdoch257744e2011-11-30 15:57:28 +0000148// 1101: not used (would allow one more relocation mode to be added)
149// 1110: long_data_record
150// The format is: [2-bit data_type_tag] 1110 11
151// signed intptr_t, lowest byte written first
152// (except data_type code_target_with_id, which
153// is followed by a signed int, not intptr_t.)
Steve Blocka7e24c12009-10-30 11:49:00 +0000154//
Ben Murdoch257744e2011-11-30 15:57:28 +0000155// 1111: long_pc_jump
156// The format is:
157// pc-jump: 00 1111 11,
158// 00 [6 bits pc delta]
159// or
160// pc-jump (variable length):
161// 01 1111 11,
162// [7 bits data] 0
163// ...
164// [7 bits data] 1
165// (Bits 6..31 of pc delta, with leading zeroes
166// dropped, and last non-zero chunk tagged with 1.)
167
168
Steve Blocka7e24c12009-10-30 11:49:00 +0000169const int kMaxRelocModes = 14;
170
171const int kTagBits = 2;
172const int kTagMask = (1 << kTagBits) - 1;
173const int kExtraTagBits = 4;
Ben Murdoch257744e2011-11-30 15:57:28 +0000174const int kLocatableTypeTagBits = 2;
175const int kSmallDataBits = kBitsPerByte - kLocatableTypeTagBits;
Steve Blocka7e24c12009-10-30 11:49:00 +0000176
177const int kEmbeddedObjectTag = 0;
178const int kCodeTargetTag = 1;
Ben Murdoch257744e2011-11-30 15:57:28 +0000179const int kLocatableTag = 2;
Steve Blocka7e24c12009-10-30 11:49:00 +0000180const int kDefaultTag = 3;
181
Ben Murdoch257744e2011-11-30 15:57:28 +0000182const int kPCJumpExtraTag = (1 << kExtraTagBits) - 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000183
184const int kSmallPCDeltaBits = kBitsPerByte - kTagBits;
185const int kSmallPCDeltaMask = (1 << kSmallPCDeltaBits) - 1;
Steve Block44f0eee2011-05-26 01:26:41 +0100186const int RelocInfo::kMaxSmallPCDelta = kSmallPCDeltaMask;
Steve Blocka7e24c12009-10-30 11:49:00 +0000187
188const int kVariableLengthPCJumpTopTag = 1;
189const int kChunkBits = 7;
190const int kChunkMask = (1 << kChunkBits) - 1;
191const int kLastChunkTagBits = 1;
192const int kLastChunkTagMask = 1;
193const int kLastChunkTag = 1;
194
195
Ben Murdoch257744e2011-11-30 15:57:28 +0000196const int kDataJumpExtraTag = kPCJumpExtraTag - 1;
Steve Blocka7e24c12009-10-30 11:49:00 +0000197
Ben Murdoch257744e2011-11-30 15:57:28 +0000198const int kCodeWithIdTag = 0;
199const int kNonstatementPositionTag = 1;
200const int kStatementPositionTag = 2;
201const int kCommentTag = 3;
Steve Blocka7e24c12009-10-30 11:49:00 +0000202
203
204uint32_t RelocInfoWriter::WriteVariableLengthPCJump(uint32_t pc_delta) {
205 // Return if the pc_delta can fit in kSmallPCDeltaBits bits.
206 // Otherwise write a variable length PC jump for the bits that do
207 // not fit in the kSmallPCDeltaBits bits.
208 if (is_uintn(pc_delta, kSmallPCDeltaBits)) return pc_delta;
Ben Murdoch257744e2011-11-30 15:57:28 +0000209 WriteExtraTag(kPCJumpExtraTag, kVariableLengthPCJumpTopTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000210 uint32_t pc_jump = pc_delta >> kSmallPCDeltaBits;
211 ASSERT(pc_jump > 0);
212 // Write kChunkBits size chunks of the pc_jump.
213 for (; pc_jump > 0; pc_jump = pc_jump >> kChunkBits) {
214 byte b = pc_jump & kChunkMask;
215 *--pos_ = b << kLastChunkTagBits;
216 }
217 // Tag the last chunk so it can be identified.
218 *pos_ = *pos_ | kLastChunkTag;
219 // Return the remaining kSmallPCDeltaBits of the pc_delta.
220 return pc_delta & kSmallPCDeltaMask;
221}
222
223
224void RelocInfoWriter::WriteTaggedPC(uint32_t pc_delta, int tag) {
225 // Write a byte of tagged pc-delta, possibly preceded by var. length pc-jump.
226 pc_delta = WriteVariableLengthPCJump(pc_delta);
227 *--pos_ = pc_delta << kTagBits | tag;
228}
229
230
231void RelocInfoWriter::WriteTaggedData(intptr_t data_delta, int tag) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000232 *--pos_ = static_cast<byte>(data_delta << kLocatableTypeTagBits | tag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000233}
234
235
236void RelocInfoWriter::WriteExtraTag(int extra_tag, int top_tag) {
Steve Blockd0582a62009-12-15 09:54:21 +0000237 *--pos_ = static_cast<int>(top_tag << (kTagBits + kExtraTagBits) |
238 extra_tag << kTagBits |
239 kDefaultTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000240}
241
242
243void RelocInfoWriter::WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag) {
244 // Write two-byte tagged pc-delta, possibly preceded by var. length pc-jump.
245 pc_delta = WriteVariableLengthPCJump(pc_delta);
246 WriteExtraTag(extra_tag, 0);
247 *--pos_ = pc_delta;
248}
249
250
Ben Murdoch257744e2011-11-30 15:57:28 +0000251void RelocInfoWriter::WriteExtraTaggedIntData(int data_delta, int top_tag) {
252 WriteExtraTag(kDataJumpExtraTag, top_tag);
253 for (int i = 0; i < kIntSize; i++) {
254 *--pos_ = static_cast<byte>(data_delta);
255 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
256 data_delta = data_delta >> kBitsPerByte;
257 }
258}
259
Steve Blocka7e24c12009-10-30 11:49:00 +0000260void RelocInfoWriter::WriteExtraTaggedData(intptr_t data_delta, int top_tag) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000261 WriteExtraTag(kDataJumpExtraTag, top_tag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000262 for (int i = 0; i < kIntptrSize; i++) {
Steve Blockd0582a62009-12-15 09:54:21 +0000263 *--pos_ = static_cast<byte>(data_delta);
Ben Murdoch257744e2011-11-30 15:57:28 +0000264 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
Steve Blocka7e24c12009-10-30 11:49:00 +0000265 data_delta = data_delta >> kBitsPerByte;
266 }
267}
268
269
270void RelocInfoWriter::Write(const RelocInfo* rinfo) {
271#ifdef DEBUG
272 byte* begin_pos = pos_;
273#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000274 ASSERT(rinfo->pc() - last_pc_ >= 0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000275 ASSERT(RelocInfo::NUMBER_OF_MODES - RelocInfo::LAST_COMPACT_ENUM <=
276 kMaxRelocModes);
Steve Blocka7e24c12009-10-30 11:49:00 +0000277 // Use unsigned delta-encoding for pc.
Steve Blockd0582a62009-12-15 09:54:21 +0000278 uint32_t pc_delta = static_cast<uint32_t>(rinfo->pc() - last_pc_);
Steve Blocka7e24c12009-10-30 11:49:00 +0000279 RelocInfo::Mode rmode = rinfo->rmode();
280
281 // The two most common modes are given small tags, and usually fit in a byte.
282 if (rmode == RelocInfo::EMBEDDED_OBJECT) {
283 WriteTaggedPC(pc_delta, kEmbeddedObjectTag);
284 } else if (rmode == RelocInfo::CODE_TARGET) {
285 WriteTaggedPC(pc_delta, kCodeTargetTag);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100286 ASSERT(begin_pos - pos_ <= RelocInfo::kMaxCallSize);
Ben Murdoch257744e2011-11-30 15:57:28 +0000287 } else if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
288 // Use signed delta-encoding for id.
289 ASSERT(static_cast<int>(rinfo->data()) == rinfo->data());
290 int id_delta = static_cast<int>(rinfo->data()) - last_id_;
291 // Check if delta is small enough to fit in a tagged byte.
292 if (is_intn(id_delta, kSmallDataBits)) {
293 WriteTaggedPC(pc_delta, kLocatableTag);
294 WriteTaggedData(id_delta, kCodeWithIdTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000295 } else {
296 // Otherwise, use costly encoding.
Ben Murdoch257744e2011-11-30 15:57:28 +0000297 WriteExtraTaggedPC(pc_delta, kPCJumpExtraTag);
298 WriteExtraTaggedIntData(id_delta, kCodeWithIdTag);
Steve Blocka7e24c12009-10-30 11:49:00 +0000299 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000300 last_id_ = static_cast<int>(rinfo->data());
301 } else if (RelocInfo::IsPosition(rmode)) {
302 // Use signed delta-encoding for position.
303 ASSERT(static_cast<int>(rinfo->data()) == rinfo->data());
304 int pos_delta = static_cast<int>(rinfo->data()) - last_position_;
305 int pos_type_tag = (rmode == RelocInfo::POSITION) ? kNonstatementPositionTag
306 : kStatementPositionTag;
307 // Check if delta is small enough to fit in a tagged byte.
308 if (is_intn(pos_delta, kSmallDataBits)) {
309 WriteTaggedPC(pc_delta, kLocatableTag);
310 WriteTaggedData(pos_delta, pos_type_tag);
311 } else {
312 // Otherwise, use costly encoding.
313 WriteExtraTaggedPC(pc_delta, kPCJumpExtraTag);
314 WriteExtraTaggedIntData(pos_delta, pos_type_tag);
315 }
316 last_position_ = static_cast<int>(rinfo->data());
Steve Blocka7e24c12009-10-30 11:49:00 +0000317 } else if (RelocInfo::IsComment(rmode)) {
318 // Comments are normally not generated, so we use the costly encoding.
Ben Murdoch257744e2011-11-30 15:57:28 +0000319 WriteExtraTaggedPC(pc_delta, kPCJumpExtraTag);
320 WriteExtraTaggedData(rinfo->data(), kCommentTag);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100321 ASSERT(begin_pos - pos_ >= RelocInfo::kMinRelocCommentSize);
Steve Blocka7e24c12009-10-30 11:49:00 +0000322 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +0000323 ASSERT(rmode > RelocInfo::LAST_COMPACT_ENUM);
324 int saved_mode = rmode - RelocInfo::LAST_COMPACT_ENUM;
Steve Blocka7e24c12009-10-30 11:49:00 +0000325 // For all other modes we simply use the mode as the extra tag.
326 // None of these modes need a data component.
Ben Murdoch257744e2011-11-30 15:57:28 +0000327 ASSERT(saved_mode < kPCJumpExtraTag && saved_mode < kDataJumpExtraTag);
328 WriteExtraTaggedPC(pc_delta, saved_mode);
Steve Blocka7e24c12009-10-30 11:49:00 +0000329 }
330 last_pc_ = rinfo->pc();
331#ifdef DEBUG
332 ASSERT(begin_pos - pos_ <= kMaxSize);
333#endif
334}
335
336
337inline int RelocIterator::AdvanceGetTag() {
338 return *--pos_ & kTagMask;
339}
340
341
342inline int RelocIterator::GetExtraTag() {
343 return (*pos_ >> kTagBits) & ((1 << kExtraTagBits) - 1);
344}
345
346
347inline int RelocIterator::GetTopTag() {
348 return *pos_ >> (kTagBits + kExtraTagBits);
349}
350
351
352inline void RelocIterator::ReadTaggedPC() {
353 rinfo_.pc_ += *pos_ >> kTagBits;
354}
355
356
357inline void RelocIterator::AdvanceReadPC() {
358 rinfo_.pc_ += *--pos_;
359}
360
361
Ben Murdoch257744e2011-11-30 15:57:28 +0000362void RelocIterator::AdvanceReadId() {
363 int x = 0;
364 for (int i = 0; i < kIntSize; i++) {
365 x |= static_cast<int>(*--pos_) << i * kBitsPerByte;
366 }
367 last_id_ += x;
368 rinfo_.data_ = last_id_;
369}
370
371
372void RelocIterator::AdvanceReadPosition() {
373 int x = 0;
374 for (int i = 0; i < kIntSize; i++) {
375 x |= static_cast<int>(*--pos_) << i * kBitsPerByte;
376 }
377 last_position_ += x;
378 rinfo_.data_ = last_position_;
379}
380
381
Steve Blocka7e24c12009-10-30 11:49:00 +0000382void RelocIterator::AdvanceReadData() {
383 intptr_t x = 0;
384 for (int i = 0; i < kIntptrSize; i++) {
385 x |= static_cast<intptr_t>(*--pos_) << i * kBitsPerByte;
386 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000387 rinfo_.data_ = x;
Steve Blocka7e24c12009-10-30 11:49:00 +0000388}
389
390
391void RelocIterator::AdvanceReadVariableLengthPCJump() {
392 // Read the 32-kSmallPCDeltaBits most significant bits of the
393 // pc jump in kChunkBits bit chunks and shift them into place.
394 // Stop when the last chunk is encountered.
395 uint32_t pc_jump = 0;
396 for (int i = 0; i < kIntSize; i++) {
397 byte pc_jump_part = *--pos_;
398 pc_jump |= (pc_jump_part >> kLastChunkTagBits) << i * kChunkBits;
399 if ((pc_jump_part & kLastChunkTagMask) == 1) break;
400 }
401 // The least significant kSmallPCDeltaBits bits will be added
402 // later.
403 rinfo_.pc_ += pc_jump << kSmallPCDeltaBits;
404}
405
406
Ben Murdoch257744e2011-11-30 15:57:28 +0000407inline int RelocIterator::GetLocatableTypeTag() {
408 return *pos_ & ((1 << kLocatableTypeTagBits) - 1);
Steve Blocka7e24c12009-10-30 11:49:00 +0000409}
410
411
Ben Murdoch257744e2011-11-30 15:57:28 +0000412inline void RelocIterator::ReadTaggedId() {
Steve Blocka7e24c12009-10-30 11:49:00 +0000413 int8_t signed_b = *pos_;
414 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
Ben Murdoch257744e2011-11-30 15:57:28 +0000415 last_id_ += signed_b >> kLocatableTypeTagBits;
416 rinfo_.data_ = last_id_;
Steve Blocka7e24c12009-10-30 11:49:00 +0000417}
418
419
Ben Murdoch257744e2011-11-30 15:57:28 +0000420inline void RelocIterator::ReadTaggedPosition() {
421 int8_t signed_b = *pos_;
422 // Signed right shift is arithmetic shift. Tested in test-utils.cc.
423 last_position_ += signed_b >> kLocatableTypeTagBits;
424 rinfo_.data_ = last_position_;
425}
426
427
428static inline RelocInfo::Mode GetPositionModeFromTag(int tag) {
429 ASSERT(tag == kNonstatementPositionTag ||
430 tag == kStatementPositionTag);
431 return (tag == kNonstatementPositionTag) ?
432 RelocInfo::POSITION :
433 RelocInfo::STATEMENT_POSITION;
Steve Blocka7e24c12009-10-30 11:49:00 +0000434}
435
436
437void RelocIterator::next() {
438 ASSERT(!done());
439 // Basically, do the opposite of RelocInfoWriter::Write.
440 // Reading of data is as far as possible avoided for unwanted modes,
441 // but we must always update the pc.
442 //
443 // We exit this loop by returning when we find a mode we want.
444 while (pos_ > end_) {
445 int tag = AdvanceGetTag();
446 if (tag == kEmbeddedObjectTag) {
447 ReadTaggedPC();
448 if (SetMode(RelocInfo::EMBEDDED_OBJECT)) return;
449 } else if (tag == kCodeTargetTag) {
450 ReadTaggedPC();
Steve Blocka7e24c12009-10-30 11:49:00 +0000451 if (SetMode(RelocInfo::CODE_TARGET)) return;
Ben Murdoch257744e2011-11-30 15:57:28 +0000452 } else if (tag == kLocatableTag) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000453 ReadTaggedPC();
454 Advance();
Ben Murdoch257744e2011-11-30 15:57:28 +0000455 int locatable_tag = GetLocatableTypeTag();
456 if (locatable_tag == kCodeWithIdTag) {
457 if (SetMode(RelocInfo::CODE_TARGET_WITH_ID)) {
458 ReadTaggedId();
459 return;
460 }
461 } else {
462 // Compact encoding is never used for comments,
463 // so it must be a position.
464 ASSERT(locatable_tag == kNonstatementPositionTag ||
465 locatable_tag == kStatementPositionTag);
466 if (mode_mask_ & RelocInfo::kPositionMask) {
467 ReadTaggedPosition();
468 if (SetMode(GetPositionModeFromTag(locatable_tag))) return;
469 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000470 }
471 } else {
472 ASSERT(tag == kDefaultTag);
473 int extra_tag = GetExtraTag();
Ben Murdoch257744e2011-11-30 15:57:28 +0000474 if (extra_tag == kPCJumpExtraTag) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000475 int top_tag = GetTopTag();
476 if (top_tag == kVariableLengthPCJumpTopTag) {
477 AdvanceReadVariableLengthPCJump();
478 } else {
479 AdvanceReadPC();
480 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000481 } else if (extra_tag == kDataJumpExtraTag) {
482 int locatable_tag = GetTopTag();
483 if (locatable_tag == kCodeWithIdTag) {
484 if (SetMode(RelocInfo::CODE_TARGET_WITH_ID)) {
485 AdvanceReadId();
486 return;
487 }
488 Advance(kIntSize);
489 } else if (locatable_tag != kCommentTag) {
490 ASSERT(locatable_tag == kNonstatementPositionTag ||
491 locatable_tag == kStatementPositionTag);
492 if (mode_mask_ & RelocInfo::kPositionMask) {
493 AdvanceReadPosition();
494 if (SetMode(GetPositionModeFromTag(locatable_tag))) return;
495 } else {
496 Advance(kIntSize);
497 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000498 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +0000499 ASSERT(locatable_tag == kCommentTag);
500 if (SetMode(RelocInfo::COMMENT)) {
501 AdvanceReadData();
502 return;
503 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000504 Advance(kIntptrSize);
505 }
506 } else {
507 AdvanceReadPC();
Ben Murdoch257744e2011-11-30 15:57:28 +0000508 int rmode = extra_tag + RelocInfo::LAST_COMPACT_ENUM;
509 if (SetMode(static_cast<RelocInfo::Mode>(rmode))) return;
Steve Blocka7e24c12009-10-30 11:49:00 +0000510 }
511 }
512 }
513 done_ = true;
514}
515
516
517RelocIterator::RelocIterator(Code* code, int mode_mask) {
518 rinfo_.pc_ = code->instruction_start();
519 rinfo_.data_ = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100520 // Relocation info is read backwards.
Steve Blocka7e24c12009-10-30 11:49:00 +0000521 pos_ = code->relocation_start() + code->relocation_size();
522 end_ = code->relocation_start();
523 done_ = false;
524 mode_mask_ = mode_mask;
Ben Murdoch257744e2011-11-30 15:57:28 +0000525 last_id_ = 0;
526 last_position_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000527 if (mode_mask_ == 0) pos_ = end_;
528 next();
529}
530
531
532RelocIterator::RelocIterator(const CodeDesc& desc, int mode_mask) {
533 rinfo_.pc_ = desc.buffer;
534 rinfo_.data_ = 0;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100535 // Relocation info is read backwards.
Steve Blocka7e24c12009-10-30 11:49:00 +0000536 pos_ = desc.buffer + desc.buffer_size;
537 end_ = pos_ - desc.reloc_size;
538 done_ = false;
539 mode_mask_ = mode_mask;
Ben Murdoch257744e2011-11-30 15:57:28 +0000540 last_id_ = 0;
541 last_position_ = 0;
Steve Blocka7e24c12009-10-30 11:49:00 +0000542 if (mode_mask_ == 0) pos_ = end_;
543 next();
544}
545
546
547// -----------------------------------------------------------------------------
548// Implementation of RelocInfo
549
550
551#ifdef ENABLE_DISASSEMBLER
552const char* RelocInfo::RelocModeName(RelocInfo::Mode rmode) {
553 switch (rmode) {
554 case RelocInfo::NONE:
555 return "no reloc";
556 case RelocInfo::EMBEDDED_OBJECT:
557 return "embedded object";
Steve Blocka7e24c12009-10-30 11:49:00 +0000558 case RelocInfo::CONSTRUCT_CALL:
559 return "code target (js construct call)";
560 case RelocInfo::CODE_TARGET_CONTEXT:
561 return "code target (context)";
Andrei Popescu402d9372010-02-26 13:31:12 +0000562 case RelocInfo::DEBUG_BREAK:
563#ifndef ENABLE_DEBUGGER_SUPPORT
564 UNREACHABLE();
565#endif
566 return "debug break";
Steve Blocka7e24c12009-10-30 11:49:00 +0000567 case RelocInfo::CODE_TARGET:
568 return "code target";
Ben Murdoch257744e2011-11-30 15:57:28 +0000569 case RelocInfo::CODE_TARGET_WITH_ID:
570 return "code target with id";
Ben Murdochb0fe1622011-05-05 13:52:32 +0100571 case RelocInfo::GLOBAL_PROPERTY_CELL:
572 return "global property cell";
Steve Blocka7e24c12009-10-30 11:49:00 +0000573 case RelocInfo::RUNTIME_ENTRY:
574 return "runtime entry";
575 case RelocInfo::JS_RETURN:
576 return "js return";
577 case RelocInfo::COMMENT:
578 return "comment";
579 case RelocInfo::POSITION:
580 return "position";
581 case RelocInfo::STATEMENT_POSITION:
582 return "statement position";
583 case RelocInfo::EXTERNAL_REFERENCE:
584 return "external reference";
585 case RelocInfo::INTERNAL_REFERENCE:
586 return "internal reference";
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100587 case RelocInfo::DEBUG_BREAK_SLOT:
588#ifndef ENABLE_DEBUGGER_SUPPORT
589 UNREACHABLE();
590#endif
591 return "debug break slot";
Steve Blocka7e24c12009-10-30 11:49:00 +0000592 case RelocInfo::NUMBER_OF_MODES:
593 UNREACHABLE();
594 return "number_of_modes";
595 }
596 return "unknown relocation type";
597}
598
599
Ben Murdochb0fe1622011-05-05 13:52:32 +0100600void RelocInfo::Print(FILE* out) {
601 PrintF(out, "%p %s", pc_, RelocModeName(rmode_));
Steve Blocka7e24c12009-10-30 11:49:00 +0000602 if (IsComment(rmode_)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100603 PrintF(out, " (%s)", reinterpret_cast<char*>(data_));
Steve Blocka7e24c12009-10-30 11:49:00 +0000604 } else if (rmode_ == EMBEDDED_OBJECT) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100605 PrintF(out, " (");
606 target_object()->ShortPrint(out);
607 PrintF(out, ")");
Steve Blocka7e24c12009-10-30 11:49:00 +0000608 } else if (rmode_ == EXTERNAL_REFERENCE) {
609 ExternalReferenceEncoder ref_encoder;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100610 PrintF(out, " (%s) (%p)",
Steve Blocka7e24c12009-10-30 11:49:00 +0000611 ref_encoder.NameOfAddress(*target_reference_address()),
612 *target_reference_address());
613 } else if (IsCodeTarget(rmode_)) {
614 Code* code = Code::GetCodeFromTargetAddress(target_address());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100615 PrintF(out, " (%s) (%p)", Code::Kind2String(code->kind()),
616 target_address());
Ben Murdoch257744e2011-11-30 15:57:28 +0000617 if (rmode_ == CODE_TARGET_WITH_ID) {
618 PrintF(" (id=%d)", static_cast<int>(data_));
619 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000620 } else if (IsPosition(rmode_)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100621 PrintF(out, " (%" V8_PTR_PREFIX "d)", data());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100622 } else if (rmode_ == RelocInfo::RUNTIME_ENTRY &&
623 Isolate::Current()->deoptimizer_data() != NULL) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100624 // Depotimization bailouts are stored as runtime entries.
625 int id = Deoptimizer::GetDeoptimizationId(
626 target_address(), Deoptimizer::EAGER);
627 if (id != Deoptimizer::kNotDeoptimizationEntry) {
628 PrintF(out, " (deoptimization bailout %d)", id);
629 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000630 }
631
Ben Murdochb0fe1622011-05-05 13:52:32 +0100632 PrintF(out, "\n");
Steve Blocka7e24c12009-10-30 11:49:00 +0000633}
634#endif // ENABLE_DISASSEMBLER
635
636
637#ifdef DEBUG
638void RelocInfo::Verify() {
639 switch (rmode_) {
640 case EMBEDDED_OBJECT:
641 Object::VerifyPointer(target_object());
642 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100643 case GLOBAL_PROPERTY_CELL:
644 Object::VerifyPointer(target_cell());
645 break;
Andrei Popescu402d9372010-02-26 13:31:12 +0000646 case DEBUG_BREAK:
647#ifndef ENABLE_DEBUGGER_SUPPORT
648 UNREACHABLE();
649 break;
650#endif
Steve Blocka7e24c12009-10-30 11:49:00 +0000651 case CONSTRUCT_CALL:
652 case CODE_TARGET_CONTEXT:
Ben Murdoch257744e2011-11-30 15:57:28 +0000653 case CODE_TARGET_WITH_ID:
Steve Blocka7e24c12009-10-30 11:49:00 +0000654 case CODE_TARGET: {
655 // convert inline target address to code object
656 Address addr = target_address();
657 ASSERT(addr != NULL);
658 // Check that we can find the right code object.
659 Code* code = Code::GetCodeFromTargetAddress(addr);
Steve Block44f0eee2011-05-26 01:26:41 +0100660 Object* found = HEAP->FindCodeObject(addr);
Steve Blocka7e24c12009-10-30 11:49:00 +0000661 ASSERT(found->IsCode());
662 ASSERT(code->address() == HeapObject::cast(found)->address());
663 break;
664 }
Steve Blocka7e24c12009-10-30 11:49:00 +0000665 case RUNTIME_ENTRY:
666 case JS_RETURN:
667 case COMMENT:
668 case POSITION:
669 case STATEMENT_POSITION:
670 case EXTERNAL_REFERENCE:
671 case INTERNAL_REFERENCE:
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100672 case DEBUG_BREAK_SLOT:
Steve Blocka7e24c12009-10-30 11:49:00 +0000673 case NONE:
674 break;
675 case NUMBER_OF_MODES:
676 UNREACHABLE();
677 break;
678 }
679}
680#endif // DEBUG
681
682
683// -----------------------------------------------------------------------------
684// Implementation of ExternalReference
685
Steve Block44f0eee2011-05-26 01:26:41 +0100686ExternalReference::ExternalReference(Builtins::CFunctionId id, Isolate* isolate)
687 : address_(Redirect(isolate, Builtins::c_function_address(id))) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000688
689
Steve Block1e0659c2011-05-24 12:43:12 +0100690ExternalReference::ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +0100691 ApiFunction* fun,
692 Type type = ExternalReference::BUILTIN_CALL,
693 Isolate* isolate = NULL)
694 : address_(Redirect(isolate, fun->address(), type)) {}
Steve Blockd0582a62009-12-15 09:54:21 +0000695
696
Steve Block44f0eee2011-05-26 01:26:41 +0100697ExternalReference::ExternalReference(Builtins::Name name, Isolate* isolate)
698 : address_(isolate->builtins()->builtin_address(name)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000699
700
Steve Block44f0eee2011-05-26 01:26:41 +0100701ExternalReference::ExternalReference(Runtime::FunctionId id,
702 Isolate* isolate)
703 : address_(Redirect(isolate, Runtime::FunctionForId(id)->entry)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000704
705
Steve Block44f0eee2011-05-26 01:26:41 +0100706ExternalReference::ExternalReference(const Runtime::Function* f,
707 Isolate* isolate)
708 : address_(Redirect(isolate, f->entry)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000709
710
Steve Block44f0eee2011-05-26 01:26:41 +0100711ExternalReference ExternalReference::isolate_address() {
712 return ExternalReference(Isolate::Current());
713}
714
715
716ExternalReference::ExternalReference(const IC_Utility& ic_utility,
717 Isolate* isolate)
718 : address_(Redirect(isolate, ic_utility.address())) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000719
720#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +0100721ExternalReference::ExternalReference(const Debug_Address& debug_address,
722 Isolate* isolate)
723 : address_(debug_address.address(isolate)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000724#endif
725
726ExternalReference::ExternalReference(StatsCounter* counter)
727 : address_(reinterpret_cast<Address>(counter->GetInternalPointer())) {}
728
729
Steve Block44f0eee2011-05-26 01:26:41 +0100730ExternalReference::ExternalReference(Isolate::AddressId id, Isolate* isolate)
731 : address_(isolate->get_address_from_id(id)) {}
Steve Blocka7e24c12009-10-30 11:49:00 +0000732
733
734ExternalReference::ExternalReference(const SCTableReference& table_ref)
735 : address_(table_ref.address()) {}
736
737
Steve Block44f0eee2011-05-26 01:26:41 +0100738ExternalReference ExternalReference::perform_gc_function(Isolate* isolate) {
739 return ExternalReference(Redirect(isolate,
740 FUNCTION_ADDR(Runtime::PerformGC)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000741}
742
743
Steve Block44f0eee2011-05-26 01:26:41 +0100744ExternalReference ExternalReference::fill_heap_number_with_random_function(
745 Isolate* isolate) {
746 return ExternalReference(Redirect(
747 isolate,
748 FUNCTION_ADDR(V8::FillHeapNumberWithRandom)));
Steve Block6ded16b2010-05-10 14:33:55 +0100749}
750
751
Steve Block44f0eee2011-05-26 01:26:41 +0100752ExternalReference ExternalReference::delete_handle_scope_extensions(
753 Isolate* isolate) {
754 return ExternalReference(Redirect(
755 isolate,
756 FUNCTION_ADDR(HandleScope::DeleteExtensions)));
John Reck59135872010-11-02 12:39:01 -0700757}
758
759
Steve Block44f0eee2011-05-26 01:26:41 +0100760ExternalReference ExternalReference::random_uint32_function(
761 Isolate* isolate) {
762 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(V8::Random)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000763}
764
765
Steve Block44f0eee2011-05-26 01:26:41 +0100766ExternalReference ExternalReference::transcendental_cache_array_address(
767 Isolate* isolate) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100768 return ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +0100769 isolate->transcendental_cache()->cache_array_address());
Ben Murdochb0fe1622011-05-05 13:52:32 +0100770}
771
772
Steve Block44f0eee2011-05-26 01:26:41 +0100773ExternalReference ExternalReference::new_deoptimizer_function(
774 Isolate* isolate) {
Ben Murdochb0fe1622011-05-05 13:52:32 +0100775 return ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +0100776 Redirect(isolate, FUNCTION_ADDR(Deoptimizer::New)));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100777}
778
779
Steve Block44f0eee2011-05-26 01:26:41 +0100780ExternalReference ExternalReference::compute_output_frames_function(
781 Isolate* isolate) {
782 return ExternalReference(
783 Redirect(isolate, FUNCTION_ADDR(Deoptimizer::ComputeOutputFrames)));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100784}
785
786
Steve Block44f0eee2011-05-26 01:26:41 +0100787ExternalReference ExternalReference::global_contexts_list(Isolate* isolate) {
788 return ExternalReference(isolate->heap()->global_contexts_list_address());
Leon Clarkee46be812010-01-19 14:06:41 +0000789}
790
791
Steve Block44f0eee2011-05-26 01:26:41 +0100792ExternalReference ExternalReference::keyed_lookup_cache_keys(Isolate* isolate) {
793 return ExternalReference(isolate->keyed_lookup_cache()->keys_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000794}
795
796
Steve Block44f0eee2011-05-26 01:26:41 +0100797ExternalReference ExternalReference::keyed_lookup_cache_field_offsets(
798 Isolate* isolate) {
799 return ExternalReference(
800 isolate->keyed_lookup_cache()->field_offsets_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000801}
802
803
Steve Block44f0eee2011-05-26 01:26:41 +0100804ExternalReference ExternalReference::the_hole_value_location(Isolate* isolate) {
805 return ExternalReference(isolate->factory()->the_hole_value().location());
Ben Murdoch086aeea2011-05-13 15:57:08 +0100806}
807
808
Steve Block44f0eee2011-05-26 01:26:41 +0100809ExternalReference ExternalReference::arguments_marker_location(
810 Isolate* isolate) {
811 return ExternalReference(isolate->factory()->arguments_marker().location());
Steve Blocka7e24c12009-10-30 11:49:00 +0000812}
813
814
Steve Block44f0eee2011-05-26 01:26:41 +0100815ExternalReference ExternalReference::roots_address(Isolate* isolate) {
816 return ExternalReference(isolate->heap()->roots_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000817}
818
819
Steve Block44f0eee2011-05-26 01:26:41 +0100820ExternalReference ExternalReference::address_of_stack_limit(Isolate* isolate) {
821 return ExternalReference(isolate->stack_guard()->address_of_jslimit());
Steve Blockd0582a62009-12-15 09:54:21 +0000822}
823
824
Steve Block44f0eee2011-05-26 01:26:41 +0100825ExternalReference ExternalReference::address_of_real_stack_limit(
826 Isolate* isolate) {
827 return ExternalReference(isolate->stack_guard()->address_of_real_jslimit());
Steve Blocka7e24c12009-10-30 11:49:00 +0000828}
829
830
Steve Block44f0eee2011-05-26 01:26:41 +0100831ExternalReference ExternalReference::address_of_regexp_stack_limit(
832 Isolate* isolate) {
833 return ExternalReference(isolate->regexp_stack()->limit_address());
Steve Blocka7e24c12009-10-30 11:49:00 +0000834}
835
836
Steve Block44f0eee2011-05-26 01:26:41 +0100837ExternalReference ExternalReference::new_space_start(Isolate* isolate) {
838 return ExternalReference(isolate->heap()->NewSpaceStart());
Andrei Popescu402d9372010-02-26 13:31:12 +0000839}
840
841
Steve Block44f0eee2011-05-26 01:26:41 +0100842ExternalReference ExternalReference::new_space_mask(Isolate* isolate) {
843 Address mask = reinterpret_cast<Address>(isolate->heap()->NewSpaceMask());
844 return ExternalReference(mask);
Steve Blocka7e24c12009-10-30 11:49:00 +0000845}
846
847
Steve Block44f0eee2011-05-26 01:26:41 +0100848ExternalReference ExternalReference::new_space_allocation_top_address(
849 Isolate* isolate) {
850 return ExternalReference(isolate->heap()->NewSpaceAllocationTopAddress());
Steve Blocka7e24c12009-10-30 11:49:00 +0000851}
852
853
Steve Block44f0eee2011-05-26 01:26:41 +0100854ExternalReference ExternalReference::heap_always_allocate_scope_depth(
855 Isolate* isolate) {
856 Heap* heap = isolate->heap();
857 return ExternalReference(heap->always_allocate_scope_depth_address());
858}
859
860
861ExternalReference ExternalReference::new_space_allocation_limit_address(
862 Isolate* isolate) {
863 return ExternalReference(isolate->heap()->NewSpaceAllocationLimitAddress());
Steve Blocka7e24c12009-10-30 11:49:00 +0000864}
865
Steve Blockd0582a62009-12-15 09:54:21 +0000866
John Reck59135872010-11-02 12:39:01 -0700867ExternalReference ExternalReference::handle_scope_level_address() {
868 return ExternalReference(HandleScope::current_level_address());
Steve Blockd0582a62009-12-15 09:54:21 +0000869}
870
871
872ExternalReference ExternalReference::handle_scope_next_address() {
873 return ExternalReference(HandleScope::current_next_address());
874}
875
876
877ExternalReference ExternalReference::handle_scope_limit_address() {
878 return ExternalReference(HandleScope::current_limit_address());
879}
880
881
Steve Block44f0eee2011-05-26 01:26:41 +0100882ExternalReference ExternalReference::scheduled_exception_address(
883 Isolate* isolate) {
884 return ExternalReference(isolate->scheduled_exception_address());
Steve Blockd0582a62009-12-15 09:54:21 +0000885}
886
887
Ben Murdochb0fe1622011-05-05 13:52:32 +0100888ExternalReference ExternalReference::address_of_min_int() {
889 return ExternalReference(reinterpret_cast<void*>(
890 const_cast<double*>(&DoubleConstant::min_int)));
891}
892
893
894ExternalReference ExternalReference::address_of_one_half() {
895 return ExternalReference(reinterpret_cast<void*>(
896 const_cast<double*>(&DoubleConstant::one_half)));
897}
898
899
Ben Murdochb8e0da22011-05-16 14:20:40 +0100900ExternalReference ExternalReference::address_of_minus_zero() {
901 return ExternalReference(reinterpret_cast<void*>(
902 const_cast<double*>(&DoubleConstant::minus_zero)));
903}
904
905
Ben Murdoch257744e2011-11-30 15:57:28 +0000906ExternalReference ExternalReference::address_of_zero() {
907 return ExternalReference(reinterpret_cast<void*>(
908 const_cast<double*>(&DoubleConstant::zero)));
909}
910
911
912ExternalReference ExternalReference::address_of_uint8_max_value() {
913 return ExternalReference(reinterpret_cast<void*>(
914 const_cast<double*>(&DoubleConstant::uint8_max_value)));
915}
916
917
Ben Murdochb0fe1622011-05-05 13:52:32 +0100918ExternalReference ExternalReference::address_of_negative_infinity() {
919 return ExternalReference(reinterpret_cast<void*>(
920 const_cast<double*>(&DoubleConstant::negative_infinity)));
921}
922
923
Steve Block44f0eee2011-05-26 01:26:41 +0100924ExternalReference ExternalReference::address_of_nan() {
925 return ExternalReference(reinterpret_cast<void*>(
926 const_cast<double*>(&DoubleConstant::nan)));
927}
928
929
Steve Block6ded16b2010-05-10 14:33:55 +0100930#ifndef V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +0000931
Steve Block44f0eee2011-05-26 01:26:41 +0100932ExternalReference ExternalReference::re_check_stack_guard_state(
933 Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000934 Address function;
935#ifdef V8_TARGET_ARCH_X64
936 function = FUNCTION_ADDR(RegExpMacroAssemblerX64::CheckStackGuardState);
937#elif V8_TARGET_ARCH_IA32
938 function = FUNCTION_ADDR(RegExpMacroAssemblerIA32::CheckStackGuardState);
939#elif V8_TARGET_ARCH_ARM
940 function = FUNCTION_ADDR(RegExpMacroAssemblerARM::CheckStackGuardState);
Steve Block44f0eee2011-05-26 01:26:41 +0100941#elif V8_TARGET_ARCH_MIPS
942 function = FUNCTION_ADDR(RegExpMacroAssemblerMIPS::CheckStackGuardState);
Steve Blocka7e24c12009-10-30 11:49:00 +0000943#else
Leon Clarke4515c472010-02-03 11:58:03 +0000944 UNREACHABLE();
Steve Blocka7e24c12009-10-30 11:49:00 +0000945#endif
Steve Block44f0eee2011-05-26 01:26:41 +0100946 return ExternalReference(Redirect(isolate, function));
Steve Blocka7e24c12009-10-30 11:49:00 +0000947}
948
Steve Block44f0eee2011-05-26 01:26:41 +0100949ExternalReference ExternalReference::re_grow_stack(Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000950 return ExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +0100951 Redirect(isolate, FUNCTION_ADDR(NativeRegExpMacroAssembler::GrowStack)));
Steve Blocka7e24c12009-10-30 11:49:00 +0000952}
953
Steve Block44f0eee2011-05-26 01:26:41 +0100954ExternalReference ExternalReference::re_case_insensitive_compare_uc16(
955 Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +0000956 return ExternalReference(Redirect(
Steve Block44f0eee2011-05-26 01:26:41 +0100957 isolate,
Steve Blocka7e24c12009-10-30 11:49:00 +0000958 FUNCTION_ADDR(NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16)));
959}
960
Leon Clarkee46be812010-01-19 14:06:41 +0000961ExternalReference ExternalReference::re_word_character_map() {
962 return ExternalReference(
963 NativeRegExpMacroAssembler::word_character_map_address());
964}
965
Steve Block44f0eee2011-05-26 01:26:41 +0100966ExternalReference ExternalReference::address_of_static_offsets_vector(
967 Isolate* isolate) {
968 return ExternalReference(
969 OffsetsVector::static_offsets_vector_address(isolate));
Leon Clarkee46be812010-01-19 14:06:41 +0000970}
971
Steve Block44f0eee2011-05-26 01:26:41 +0100972ExternalReference ExternalReference::address_of_regexp_stack_memory_address(
973 Isolate* isolate) {
974 return ExternalReference(
975 isolate->regexp_stack()->memory_address());
Leon Clarkee46be812010-01-19 14:06:41 +0000976}
977
Steve Block44f0eee2011-05-26 01:26:41 +0100978ExternalReference ExternalReference::address_of_regexp_stack_memory_size(
979 Isolate* isolate) {
980 return ExternalReference(isolate->regexp_stack()->memory_size_address());
Leon Clarkee46be812010-01-19 14:06:41 +0000981}
982
Steve Block6ded16b2010-05-10 14:33:55 +0100983#endif // V8_INTERPRETED_REGEXP
Steve Blocka7e24c12009-10-30 11:49:00 +0000984
985
986static double add_two_doubles(double x, double y) {
987 return x + y;
988}
989
990
991static double sub_two_doubles(double x, double y) {
992 return x - y;
993}
994
995
996static double mul_two_doubles(double x, double y) {
997 return x * y;
998}
999
1000
1001static double div_two_doubles(double x, double y) {
1002 return x / y;
1003}
1004
1005
1006static double mod_two_doubles(double x, double y) {
Leon Clarkee46be812010-01-19 14:06:41 +00001007 return modulo(x, y);
Steve Blocka7e24c12009-10-30 11:49:00 +00001008}
1009
1010
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001011static double math_sin_double(double x) {
1012 return sin(x);
1013}
1014
1015
1016static double math_cos_double(double x) {
1017 return cos(x);
1018}
1019
1020
1021static double math_log_double(double x) {
1022 return log(x);
1023}
1024
1025
Steve Block44f0eee2011-05-26 01:26:41 +01001026ExternalReference ExternalReference::math_sin_double_function(
1027 Isolate* isolate) {
1028 return ExternalReference(Redirect(isolate,
1029 FUNCTION_ADDR(math_sin_double),
Ben Murdoch257744e2011-11-30 15:57:28 +00001030 BUILTIN_FP_CALL));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001031}
1032
1033
Steve Block44f0eee2011-05-26 01:26:41 +01001034ExternalReference ExternalReference::math_cos_double_function(
1035 Isolate* isolate) {
1036 return ExternalReference(Redirect(isolate,
1037 FUNCTION_ADDR(math_cos_double),
Ben Murdoch257744e2011-11-30 15:57:28 +00001038 BUILTIN_FP_CALL));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001039}
1040
1041
Steve Block44f0eee2011-05-26 01:26:41 +01001042ExternalReference ExternalReference::math_log_double_function(
1043 Isolate* isolate) {
1044 return ExternalReference(Redirect(isolate,
1045 FUNCTION_ADDR(math_log_double),
Ben Murdoch257744e2011-11-30 15:57:28 +00001046 BUILTIN_FP_CALL));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001047}
1048
1049
Ben Murdochb0fe1622011-05-05 13:52:32 +01001050// Helper function to compute x^y, where y is known to be an
1051// integer. Uses binary decomposition to limit the number of
1052// multiplications; see the discussion in "Hacker's Delight" by Henry
1053// S. Warren, Jr., figure 11-6, page 213.
1054double power_double_int(double x, int y) {
1055 double m = (y < 0) ? 1 / x : x;
1056 unsigned n = (y < 0) ? -y : y;
1057 double p = 1;
1058 while (n != 0) {
1059 if ((n & 1) != 0) p *= m;
1060 m *= m;
1061 if ((n & 2) != 0) p *= m;
1062 m *= m;
1063 n >>= 2;
1064 }
1065 return p;
1066}
1067
1068
1069double power_double_double(double x, double y) {
1070 int y_int = static_cast<int>(y);
1071 if (y == y_int) {
1072 return power_double_int(x, y_int); // Returns 1.0 for exponent 0.
1073 }
1074 if (!isinf(x)) {
Steve Block1e0659c2011-05-24 12:43:12 +01001075 if (y == 0.5) return sqrt(x + 0.0); // -0 must be converted to +0.
1076 if (y == -0.5) return 1.0 / sqrt(x + 0.0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001077 }
1078 if (isnan(y) || ((x == 1 || x == -1) && isinf(y))) {
1079 return OS::nan_value();
1080 }
1081 return pow(x, y);
1082}
1083
1084
Steve Block44f0eee2011-05-26 01:26:41 +01001085ExternalReference ExternalReference::power_double_double_function(
1086 Isolate* isolate) {
1087 return ExternalReference(Redirect(isolate,
1088 FUNCTION_ADDR(power_double_double),
Ben Murdoch257744e2011-11-30 15:57:28 +00001089 BUILTIN_FP_FP_CALL));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001090}
1091
1092
Steve Block44f0eee2011-05-26 01:26:41 +01001093ExternalReference ExternalReference::power_double_int_function(
1094 Isolate* isolate) {
1095 return ExternalReference(Redirect(isolate,
1096 FUNCTION_ADDR(power_double_int),
Ben Murdoch257744e2011-11-30 15:57:28 +00001097 BUILTIN_FP_INT_CALL));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001098}
1099
1100
Leon Clarkee46be812010-01-19 14:06:41 +00001101static int native_compare_doubles(double y, double x) {
1102 if (x == y) return EQUAL;
1103 return x < y ? LESS : GREATER;
Steve Blocka7e24c12009-10-30 11:49:00 +00001104}
1105
1106
1107ExternalReference ExternalReference::double_fp_operation(
Steve Block44f0eee2011-05-26 01:26:41 +01001108 Token::Value operation, Isolate* isolate) {
Steve Blocka7e24c12009-10-30 11:49:00 +00001109 typedef double BinaryFPOperation(double x, double y);
1110 BinaryFPOperation* function = NULL;
1111 switch (operation) {
1112 case Token::ADD:
1113 function = &add_two_doubles;
1114 break;
1115 case Token::SUB:
1116 function = &sub_two_doubles;
1117 break;
1118 case Token::MUL:
1119 function = &mul_two_doubles;
1120 break;
1121 case Token::DIV:
1122 function = &div_two_doubles;
1123 break;
1124 case Token::MOD:
1125 function = &mod_two_doubles;
1126 break;
1127 default:
1128 UNREACHABLE();
1129 }
Steve Block44f0eee2011-05-26 01:26:41 +01001130 return ExternalReference(Redirect(isolate,
1131 FUNCTION_ADDR(function),
Ben Murdoch257744e2011-11-30 15:57:28 +00001132 BUILTIN_FP_FP_CALL));
Steve Blocka7e24c12009-10-30 11:49:00 +00001133}
1134
1135
Steve Block44f0eee2011-05-26 01:26:41 +01001136ExternalReference ExternalReference::compare_doubles(Isolate* isolate) {
1137 return ExternalReference(Redirect(isolate,
1138 FUNCTION_ADDR(native_compare_doubles),
Ben Murdoch257744e2011-11-30 15:57:28 +00001139 BUILTIN_COMPARE_CALL));
Steve Blocka7e24c12009-10-30 11:49:00 +00001140}
1141
1142
Steve Blocka7e24c12009-10-30 11:49:00 +00001143#ifdef ENABLE_DEBUGGER_SUPPORT
Steve Block44f0eee2011-05-26 01:26:41 +01001144ExternalReference ExternalReference::debug_break(Isolate* isolate) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001145 return ExternalReference(Redirect(isolate, FUNCTION_ADDR(Debug_Break)));
Steve Blocka7e24c12009-10-30 11:49:00 +00001146}
1147
1148
Steve Block44f0eee2011-05-26 01:26:41 +01001149ExternalReference ExternalReference::debug_step_in_fp_address(
1150 Isolate* isolate) {
1151 return ExternalReference(isolate->debug()->step_in_fp_addr());
Steve Blocka7e24c12009-10-30 11:49:00 +00001152}
1153#endif
1154
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001155
Ben Murdochb0fe1622011-05-05 13:52:32 +01001156void PositionsRecorder::RecordPosition(int pos) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001157 ASSERT(pos != RelocInfo::kNoPosition);
1158 ASSERT(pos >= 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001159 state_.current_position = pos;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001160#ifdef ENABLE_GDB_JIT_INTERFACE
1161 if (gdbjit_lineinfo_ != NULL) {
1162 gdbjit_lineinfo_->SetPosition(assembler_->pc_offset(), pos, false);
1163 }
1164#endif
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001165}
1166
1167
1168void PositionsRecorder::RecordStatementPosition(int pos) {
1169 ASSERT(pos != RelocInfo::kNoPosition);
1170 ASSERT(pos >= 0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001171 state_.current_statement_position = pos;
Ben Murdochb8e0da22011-05-16 14:20:40 +01001172#ifdef ENABLE_GDB_JIT_INTERFACE
1173 if (gdbjit_lineinfo_ != NULL) {
1174 gdbjit_lineinfo_->SetPosition(assembler_->pc_offset(), pos, true);
1175 }
1176#endif
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001177}
1178
1179
1180bool PositionsRecorder::WriteRecordedPositions() {
1181 bool written = false;
1182
1183 // Write the statement position if it is different from what was written last
1184 // time.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001185 if (state_.current_statement_position != state_.written_statement_position) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001186 EnsureSpace ensure_space(assembler_);
1187 assembler_->RecordRelocInfo(RelocInfo::STATEMENT_POSITION,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001188 state_.current_statement_position);
1189 state_.written_statement_position = state_.current_statement_position;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001190 written = true;
1191 }
1192
1193 // Write the position if it is different from what was written last time and
Ben Murdochb0fe1622011-05-05 13:52:32 +01001194 // also different from the written statement position.
1195 if (state_.current_position != state_.written_position &&
1196 state_.current_position != state_.written_statement_position) {
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001197 EnsureSpace ensure_space(assembler_);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001198 assembler_->RecordRelocInfo(RelocInfo::POSITION, state_.current_position);
1199 state_.written_position = state_.current_position;
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001200 written = true;
1201 }
1202
Teng-Hui Zhu3e5fa292010-11-09 16:16:48 -08001203 // Return whether something was written.
1204 return written;
1205}
1206
Steve Blocka7e24c12009-10-30 11:49:00 +00001207} } // namespace v8::internal