Move IC code into a subdir and move ic-compilation related code from stub-cache into...
[platform/upstream/v8.git] / src / assembler.cc
1 // 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.
33 // Copyright 2012 the V8 project authors. All rights reserved.
34
35 #include "src/assembler.h"
36
37 #include <cmath>
38 #include "src/api.h"
39 #include "src/base/cpu.h"
40 #include "src/base/lazy-instance.h"
41 #include "src/base/platform/platform.h"
42 #include "src/builtins.h"
43 #include "src/codegen.h"
44 #include "src/counters.h"
45 #include "src/cpu-profiler.h"
46 #include "src/debug.h"
47 #include "src/deoptimizer.h"
48 #include "src/execution.h"
49 #include "src/ic/ic.h"
50 #include "src/ic/stub-cache.h"
51 #include "src/isolate-inl.h"
52 #include "src/jsregexp.h"
53 #include "src/regexp-macro-assembler.h"
54 #include "src/regexp-stack.h"
55 #include "src/runtime.h"
56 #include "src/serialize.h"
57 #include "src/token.h"
58
59 #if V8_TARGET_ARCH_IA32
60 #include "src/ia32/assembler-ia32-inl.h"  // NOLINT
61 #elif V8_TARGET_ARCH_X64
62 #include "src/x64/assembler-x64-inl.h"  // NOLINT
63 #elif V8_TARGET_ARCH_ARM64
64 #include "src/arm64/assembler-arm64-inl.h"  // NOLINT
65 #elif V8_TARGET_ARCH_ARM
66 #include "src/arm/assembler-arm-inl.h"  // NOLINT
67 #elif V8_TARGET_ARCH_MIPS
68 #include "src/mips/assembler-mips-inl.h"  // NOLINT
69 #elif V8_TARGET_ARCH_MIPS64
70 #include "src/mips64/assembler-mips64-inl.h"  // NOLINT
71 #elif V8_TARGET_ARCH_X87
72 #include "src/x87/assembler-x87-inl.h"  // NOLINT
73 #else
74 #error "Unknown architecture."
75 #endif
76
77 // Include native regexp-macro-assembler.
78 #ifndef V8_INTERPRETED_REGEXP
79 #if V8_TARGET_ARCH_IA32
80 #include "src/ia32/regexp-macro-assembler-ia32.h"  // NOLINT
81 #elif V8_TARGET_ARCH_X64
82 #include "src/x64/regexp-macro-assembler-x64.h"  // NOLINT
83 #elif V8_TARGET_ARCH_ARM64
84 #include "src/arm64/regexp-macro-assembler-arm64.h"  // NOLINT
85 #elif V8_TARGET_ARCH_ARM
86 #include "src/arm/regexp-macro-assembler-arm.h"  // NOLINT
87 #elif V8_TARGET_ARCH_MIPS
88 #include "src/mips/regexp-macro-assembler-mips.h"  // NOLINT
89 #elif V8_TARGET_ARCH_MIPS64
90 #include "src/mips64/regexp-macro-assembler-mips64.h"  // NOLINT
91 #elif V8_TARGET_ARCH_X87
92 #include "src/x87/regexp-macro-assembler-x87.h"  // NOLINT
93 #else  // Unknown architecture.
94 #error "Unknown architecture."
95 #endif  // Target architecture.
96 #endif  // V8_INTERPRETED_REGEXP
97
98 namespace v8 {
99 namespace internal {
100
101 // -----------------------------------------------------------------------------
102 // Common double constants.
103
104 struct DoubleConstant BASE_EMBEDDED {
105 double min_int;
106 double one_half;
107 double minus_one_half;
108 double negative_infinity;
109 double canonical_non_hole_nan;
110 double the_hole_nan;
111 double uint32_bias;
112 };
113
114 static DoubleConstant double_constants;
115
116 const char* const RelocInfo::kFillerCommentString = "DEOPTIMIZATION PADDING";
117
118 static bool math_exp_data_initialized = false;
119 static base::Mutex* math_exp_data_mutex = NULL;
120 static double* math_exp_constants_array = NULL;
121 static double* math_exp_log_table_array = NULL;
122
123 // -----------------------------------------------------------------------------
124 // Implementation of AssemblerBase
125
126 AssemblerBase::AssemblerBase(Isolate* isolate, void* buffer, int buffer_size)
127     : isolate_(isolate),
128       jit_cookie_(0),
129       enabled_cpu_features_(0),
130       emit_debug_code_(FLAG_debug_code),
131       predictable_code_size_(false),
132       // We may use the assembler without an isolate.
133       serializer_enabled_(isolate && isolate->serializer_enabled()) {
134   if (FLAG_mask_constants_with_cookie && isolate != NULL)  {
135     jit_cookie_ = isolate->random_number_generator()->NextInt();
136   }
137   own_buffer_ = buffer == NULL;
138   if (buffer_size == 0) buffer_size = kMinimalBufferSize;
139   DCHECK(buffer_size > 0);
140   if (own_buffer_) buffer = NewArray<byte>(buffer_size);
141   buffer_ = static_cast<byte*>(buffer);
142   buffer_size_ = buffer_size;
143
144   pc_ = buffer_;
145 }
146
147
148 AssemblerBase::~AssemblerBase() {
149   if (own_buffer_) DeleteArray(buffer_);
150 }
151
152
153 // -----------------------------------------------------------------------------
154 // Implementation of PredictableCodeSizeScope
155
156 PredictableCodeSizeScope::PredictableCodeSizeScope(AssemblerBase* assembler,
157                                                    int expected_size)
158     : assembler_(assembler),
159       expected_size_(expected_size),
160       start_offset_(assembler->pc_offset()),
161       old_value_(assembler->predictable_code_size()) {
162   assembler_->set_predictable_code_size(true);
163 }
164
165
166 PredictableCodeSizeScope::~PredictableCodeSizeScope() {
167   // TODO(svenpanne) Remove the 'if' when everything works.
168   if (expected_size_ >= 0) {
169     CHECK_EQ(expected_size_, assembler_->pc_offset() - start_offset_);
170   }
171   assembler_->set_predictable_code_size(old_value_);
172 }
173
174
175 // -----------------------------------------------------------------------------
176 // Implementation of CpuFeatureScope
177
178 #ifdef DEBUG
179 CpuFeatureScope::CpuFeatureScope(AssemblerBase* assembler, CpuFeature f)
180     : assembler_(assembler) {
181   DCHECK(CpuFeatures::IsSupported(f));
182   old_enabled_ = assembler_->enabled_cpu_features();
183   uint64_t mask = static_cast<uint64_t>(1) << f;
184   // TODO(svenpanne) This special case below doesn't belong here!
185 #if V8_TARGET_ARCH_ARM
186   // ARMv7 is implied by VFP3.
187   if (f == VFP3) {
188     mask |= static_cast<uint64_t>(1) << ARMv7;
189   }
190 #endif
191   assembler_->set_enabled_cpu_features(old_enabled_ | mask);
192 }
193
194
195 CpuFeatureScope::~CpuFeatureScope() {
196   assembler_->set_enabled_cpu_features(old_enabled_);
197 }
198 #endif
199
200
201 bool CpuFeatures::initialized_ = false;
202 unsigned CpuFeatures::supported_ = 0;
203 unsigned CpuFeatures::cache_line_size_ = 0;
204
205
206 // -----------------------------------------------------------------------------
207 // Implementation of Label
208
209 int Label::pos() const {
210   if (pos_ < 0) return -pos_ - 1;
211   if (pos_ > 0) return  pos_ - 1;
212   UNREACHABLE();
213   return 0;
214 }
215
216
217 // -----------------------------------------------------------------------------
218 // Implementation of RelocInfoWriter and RelocIterator
219 //
220 // Relocation information is written backwards in memory, from high addresses
221 // towards low addresses, byte by byte.  Therefore, in the encodings listed
222 // below, the first byte listed it at the highest address, and successive
223 // bytes in the record are at progressively lower addresses.
224 //
225 // Encoding
226 //
227 // The most common modes are given single-byte encodings.  Also, it is
228 // easy to identify the type of reloc info and skip unwanted modes in
229 // an iteration.
230 //
231 // The encoding relies on the fact that there are fewer than 14
232 // different relocation modes using standard non-compact encoding.
233 //
234 // The first byte of a relocation record has a tag in its low 2 bits:
235 // Here are the record schemes, depending on the low tag and optional higher
236 // tags.
237 //
238 // Low tag:
239 //   00: embedded_object:      [6-bit pc delta] 00
240 //
241 //   01: code_target:          [6-bit pc delta] 01
242 //
243 //   10: short_data_record:    [6-bit pc delta] 10 followed by
244 //                             [6-bit data delta] [2-bit data type tag]
245 //
246 //   11: long_record           [2-bit high tag][4 bit middle_tag] 11
247 //                             followed by variable data depending on type.
248 //
249 //  2-bit data type tags, used in short_data_record and data_jump long_record:
250 //   code_target_with_id: 00
251 //   position:            01
252 //   statement_position:  10
253 //   comment:             11 (not used in short_data_record)
254 //
255 //  Long record format:
256 //    4-bit middle_tag:
257 //      0000 - 1100 : Short record for RelocInfo::Mode middle_tag + 2
258 //         (The middle_tag encodes rmode - RelocInfo::LAST_COMPACT_ENUM,
259 //          and is between 0000 and 1100)
260 //        The format is:
261 //                              00 [4 bit middle_tag] 11 followed by
262 //                              00 [6 bit pc delta]
263 //
264 //      1101: constant or veneer pool. Used only on ARM and ARM64 for now.
265 //        The format is:       [2-bit sub-type] 1101 11
266 //                             signed int (size of the pool).
267 //          The 2-bit sub-types are:
268 //            00: constant pool
269 //            01: veneer pool
270 //      1110: long_data_record
271 //        The format is:       [2-bit data_type_tag] 1110 11
272 //                             signed intptr_t, lowest byte written first
273 //                             (except data_type code_target_with_id, which
274 //                             is followed by a signed int, not intptr_t.)
275 //
276 //      1111: long_pc_jump
277 //        The format is:
278 //          pc-jump:             00 1111 11,
279 //                               00 [6 bits pc delta]
280 //        or
281 //          pc-jump (variable length):
282 //                               01 1111 11,
283 //                               [7 bits data] 0
284 //                                  ...
285 //                               [7 bits data] 1
286 //               (Bits 6..31 of pc delta, with leading zeroes
287 //                dropped, and last non-zero chunk tagged with 1.)
288
289
290 #ifdef DEBUG
291 const int kMaxStandardNonCompactModes = 14;
292 #endif
293
294 const int kTagBits = 2;
295 const int kTagMask = (1 << kTagBits) - 1;
296 const int kExtraTagBits = 4;
297 const int kLocatableTypeTagBits = 2;
298 const int kSmallDataBits = kBitsPerByte - kLocatableTypeTagBits;
299
300 const int kEmbeddedObjectTag = 0;
301 const int kCodeTargetTag = 1;
302 const int kLocatableTag = 2;
303 const int kDefaultTag = 3;
304
305 const int kPCJumpExtraTag = (1 << kExtraTagBits) - 1;
306
307 const int kSmallPCDeltaBits = kBitsPerByte - kTagBits;
308 const int kSmallPCDeltaMask = (1 << kSmallPCDeltaBits) - 1;
309 const int RelocInfo::kMaxSmallPCDelta = kSmallPCDeltaMask;
310
311 const int kVariableLengthPCJumpTopTag = 1;
312 const int kChunkBits = 7;
313 const int kChunkMask = (1 << kChunkBits) - 1;
314 const int kLastChunkTagBits = 1;
315 const int kLastChunkTagMask = 1;
316 const int kLastChunkTag = 1;
317
318
319 const int kDataJumpExtraTag = kPCJumpExtraTag - 1;
320
321 const int kCodeWithIdTag = 0;
322 const int kNonstatementPositionTag = 1;
323 const int kStatementPositionTag = 2;
324 const int kCommentTag = 3;
325
326 const int kPoolExtraTag = kPCJumpExtraTag - 2;
327 const int kConstPoolTag = 0;
328 const int kVeneerPoolTag = 1;
329
330
331 uint32_t RelocInfoWriter::WriteVariableLengthPCJump(uint32_t pc_delta) {
332   // Return if the pc_delta can fit in kSmallPCDeltaBits bits.
333   // Otherwise write a variable length PC jump for the bits that do
334   // not fit in the kSmallPCDeltaBits bits.
335   if (is_uintn(pc_delta, kSmallPCDeltaBits)) return pc_delta;
336   WriteExtraTag(kPCJumpExtraTag, kVariableLengthPCJumpTopTag);
337   uint32_t pc_jump = pc_delta >> kSmallPCDeltaBits;
338   DCHECK(pc_jump > 0);
339   // Write kChunkBits size chunks of the pc_jump.
340   for (; pc_jump > 0; pc_jump = pc_jump >> kChunkBits) {
341     byte b = pc_jump & kChunkMask;
342     *--pos_ = b << kLastChunkTagBits;
343   }
344   // Tag the last chunk so it can be identified.
345   *pos_ = *pos_ | kLastChunkTag;
346   // Return the remaining kSmallPCDeltaBits of the pc_delta.
347   return pc_delta & kSmallPCDeltaMask;
348 }
349
350
351 void RelocInfoWriter::WriteTaggedPC(uint32_t pc_delta, int tag) {
352   // Write a byte of tagged pc-delta, possibly preceded by var. length pc-jump.
353   pc_delta = WriteVariableLengthPCJump(pc_delta);
354   *--pos_ = pc_delta << kTagBits | tag;
355 }
356
357
358 void RelocInfoWriter::WriteTaggedData(intptr_t data_delta, int tag) {
359   *--pos_ = static_cast<byte>(data_delta << kLocatableTypeTagBits | tag);
360 }
361
362
363 void RelocInfoWriter::WriteExtraTag(int extra_tag, int top_tag) {
364   *--pos_ = static_cast<int>(top_tag << (kTagBits + kExtraTagBits) |
365                              extra_tag << kTagBits |
366                              kDefaultTag);
367 }
368
369
370 void RelocInfoWriter::WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag) {
371   // Write two-byte tagged pc-delta, possibly preceded by var. length pc-jump.
372   pc_delta = WriteVariableLengthPCJump(pc_delta);
373   WriteExtraTag(extra_tag, 0);
374   *--pos_ = pc_delta;
375 }
376
377
378 void RelocInfoWriter::WriteExtraTaggedIntData(int data_delta, int top_tag) {
379   WriteExtraTag(kDataJumpExtraTag, top_tag);
380   for (int i = 0; i < kIntSize; i++) {
381     *--pos_ = static_cast<byte>(data_delta);
382     // Signed right shift is arithmetic shift.  Tested in test-utils.cc.
383     data_delta = data_delta >> kBitsPerByte;
384   }
385 }
386
387
388 void RelocInfoWriter::WriteExtraTaggedPoolData(int data, int pool_type) {
389   WriteExtraTag(kPoolExtraTag, pool_type);
390   for (int i = 0; i < kIntSize; i++) {
391     *--pos_ = static_cast<byte>(data);
392     // Signed right shift is arithmetic shift.  Tested in test-utils.cc.
393     data = data >> kBitsPerByte;
394   }
395 }
396
397
398 void RelocInfoWriter::WriteExtraTaggedData(intptr_t data_delta, int top_tag) {
399   WriteExtraTag(kDataJumpExtraTag, top_tag);
400   for (int i = 0; i < kIntptrSize; i++) {
401     *--pos_ = static_cast<byte>(data_delta);
402     // Signed right shift is arithmetic shift.  Tested in test-utils.cc.
403     data_delta = data_delta >> kBitsPerByte;
404   }
405 }
406
407
408 void RelocInfoWriter::Write(const RelocInfo* rinfo) {
409 #ifdef DEBUG
410   byte* begin_pos = pos_;
411 #endif
412   DCHECK(rinfo->rmode() < RelocInfo::NUMBER_OF_MODES);
413   DCHECK(rinfo->pc() - last_pc_ >= 0);
414   DCHECK(RelocInfo::LAST_STANDARD_NONCOMPACT_ENUM - RelocInfo::LAST_COMPACT_ENUM
415          <= kMaxStandardNonCompactModes);
416   // Use unsigned delta-encoding for pc.
417   uint32_t pc_delta = static_cast<uint32_t>(rinfo->pc() - last_pc_);
418   RelocInfo::Mode rmode = rinfo->rmode();
419
420   // The two most common modes are given small tags, and usually fit in a byte.
421   if (rmode == RelocInfo::EMBEDDED_OBJECT) {
422     WriteTaggedPC(pc_delta, kEmbeddedObjectTag);
423   } else if (rmode == RelocInfo::CODE_TARGET) {
424     WriteTaggedPC(pc_delta, kCodeTargetTag);
425     DCHECK(begin_pos - pos_ <= RelocInfo::kMaxCallSize);
426   } else if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
427     // Use signed delta-encoding for id.
428     DCHECK(static_cast<int>(rinfo->data()) == rinfo->data());
429     int id_delta = static_cast<int>(rinfo->data()) - last_id_;
430     // Check if delta is small enough to fit in a tagged byte.
431     if (is_intn(id_delta, kSmallDataBits)) {
432       WriteTaggedPC(pc_delta, kLocatableTag);
433       WriteTaggedData(id_delta, kCodeWithIdTag);
434     } else {
435       // Otherwise, use costly encoding.
436       WriteExtraTaggedPC(pc_delta, kPCJumpExtraTag);
437       WriteExtraTaggedIntData(id_delta, kCodeWithIdTag);
438     }
439     last_id_ = static_cast<int>(rinfo->data());
440   } else if (RelocInfo::IsPosition(rmode)) {
441     // Use signed delta-encoding for position.
442     DCHECK(static_cast<int>(rinfo->data()) == rinfo->data());
443     int pos_delta = static_cast<int>(rinfo->data()) - last_position_;
444     int pos_type_tag = (rmode == RelocInfo::POSITION) ? kNonstatementPositionTag
445                                                       : kStatementPositionTag;
446     // Check if delta is small enough to fit in a tagged byte.
447     if (is_intn(pos_delta, kSmallDataBits)) {
448       WriteTaggedPC(pc_delta, kLocatableTag);
449       WriteTaggedData(pos_delta, pos_type_tag);
450     } else {
451       // Otherwise, use costly encoding.
452       WriteExtraTaggedPC(pc_delta, kPCJumpExtraTag);
453       WriteExtraTaggedIntData(pos_delta, pos_type_tag);
454     }
455     last_position_ = static_cast<int>(rinfo->data());
456   } else if (RelocInfo::IsComment(rmode)) {
457     // Comments are normally not generated, so we use the costly encoding.
458     WriteExtraTaggedPC(pc_delta, kPCJumpExtraTag);
459     WriteExtraTaggedData(rinfo->data(), kCommentTag);
460     DCHECK(begin_pos - pos_ >= RelocInfo::kMinRelocCommentSize);
461   } else if (RelocInfo::IsConstPool(rmode) || RelocInfo::IsVeneerPool(rmode)) {
462       WriteExtraTaggedPC(pc_delta, kPCJumpExtraTag);
463       WriteExtraTaggedPoolData(static_cast<int>(rinfo->data()),
464                                RelocInfo::IsConstPool(rmode) ? kConstPoolTag
465                                                              : kVeneerPoolTag);
466   } else {
467     DCHECK(rmode > RelocInfo::LAST_COMPACT_ENUM);
468     int saved_mode = rmode - RelocInfo::LAST_COMPACT_ENUM;
469     // For all other modes we simply use the mode as the extra tag.
470     // None of these modes need a data component.
471     DCHECK(saved_mode < kPCJumpExtraTag && saved_mode < kDataJumpExtraTag);
472     WriteExtraTaggedPC(pc_delta, saved_mode);
473   }
474   last_pc_ = rinfo->pc();
475 #ifdef DEBUG
476   DCHECK(begin_pos - pos_ <= kMaxSize);
477 #endif
478 }
479
480
481 inline int RelocIterator::AdvanceGetTag() {
482   return *--pos_ & kTagMask;
483 }
484
485
486 inline int RelocIterator::GetExtraTag() {
487   return (*pos_ >> kTagBits) & ((1 << kExtraTagBits) - 1);
488 }
489
490
491 inline int RelocIterator::GetTopTag() {
492   return *pos_ >> (kTagBits + kExtraTagBits);
493 }
494
495
496 inline void RelocIterator::ReadTaggedPC() {
497   rinfo_.pc_ += *pos_ >> kTagBits;
498 }
499
500
501 inline void RelocIterator::AdvanceReadPC() {
502   rinfo_.pc_ += *--pos_;
503 }
504
505
506 void RelocIterator::AdvanceReadId() {
507   int x = 0;
508   for (int i = 0; i < kIntSize; i++) {
509     x |= static_cast<int>(*--pos_) << i * kBitsPerByte;
510   }
511   last_id_ += x;
512   rinfo_.data_ = last_id_;
513 }
514
515
516 void RelocIterator::AdvanceReadPoolData() {
517   int x = 0;
518   for (int i = 0; i < kIntSize; i++) {
519     x |= static_cast<int>(*--pos_) << i * kBitsPerByte;
520   }
521   rinfo_.data_ = x;
522 }
523
524
525 void RelocIterator::AdvanceReadPosition() {
526   int x = 0;
527   for (int i = 0; i < kIntSize; i++) {
528     x |= static_cast<int>(*--pos_) << i * kBitsPerByte;
529   }
530   last_position_ += x;
531   rinfo_.data_ = last_position_;
532 }
533
534
535 void RelocIterator::AdvanceReadData() {
536   intptr_t x = 0;
537   for (int i = 0; i < kIntptrSize; i++) {
538     x |= static_cast<intptr_t>(*--pos_) << i * kBitsPerByte;
539   }
540   rinfo_.data_ = x;
541 }
542
543
544 void RelocIterator::AdvanceReadVariableLengthPCJump() {
545   // Read the 32-kSmallPCDeltaBits most significant bits of the
546   // pc jump in kChunkBits bit chunks and shift them into place.
547   // Stop when the last chunk is encountered.
548   uint32_t pc_jump = 0;
549   for (int i = 0; i < kIntSize; i++) {
550     byte pc_jump_part = *--pos_;
551     pc_jump |= (pc_jump_part >> kLastChunkTagBits) << i * kChunkBits;
552     if ((pc_jump_part & kLastChunkTagMask) == 1) break;
553   }
554   // The least significant kSmallPCDeltaBits bits will be added
555   // later.
556   rinfo_.pc_ += pc_jump << kSmallPCDeltaBits;
557 }
558
559
560 inline int RelocIterator::GetLocatableTypeTag() {
561   return *pos_ & ((1 << kLocatableTypeTagBits) - 1);
562 }
563
564
565 inline void RelocIterator::ReadTaggedId() {
566   int8_t signed_b = *pos_;
567   // Signed right shift is arithmetic shift.  Tested in test-utils.cc.
568   last_id_ += signed_b >> kLocatableTypeTagBits;
569   rinfo_.data_ = last_id_;
570 }
571
572
573 inline void RelocIterator::ReadTaggedPosition() {
574   int8_t signed_b = *pos_;
575   // Signed right shift is arithmetic shift.  Tested in test-utils.cc.
576   last_position_ += signed_b >> kLocatableTypeTagBits;
577   rinfo_.data_ = last_position_;
578 }
579
580
581 static inline RelocInfo::Mode GetPositionModeFromTag(int tag) {
582   DCHECK(tag == kNonstatementPositionTag ||
583          tag == kStatementPositionTag);
584   return (tag == kNonstatementPositionTag) ?
585          RelocInfo::POSITION :
586          RelocInfo::STATEMENT_POSITION;
587 }
588
589
590 void RelocIterator::next() {
591   DCHECK(!done());
592   // Basically, do the opposite of RelocInfoWriter::Write.
593   // Reading of data is as far as possible avoided for unwanted modes,
594   // but we must always update the pc.
595   //
596   // We exit this loop by returning when we find a mode we want.
597   while (pos_ > end_) {
598     int tag = AdvanceGetTag();
599     if (tag == kEmbeddedObjectTag) {
600       ReadTaggedPC();
601       if (SetMode(RelocInfo::EMBEDDED_OBJECT)) return;
602     } else if (tag == kCodeTargetTag) {
603       ReadTaggedPC();
604       if (SetMode(RelocInfo::CODE_TARGET)) return;
605     } else if (tag == kLocatableTag) {
606       ReadTaggedPC();
607       Advance();
608       int locatable_tag = GetLocatableTypeTag();
609       if (locatable_tag == kCodeWithIdTag) {
610         if (SetMode(RelocInfo::CODE_TARGET_WITH_ID)) {
611           ReadTaggedId();
612           return;
613         }
614       } else {
615         // Compact encoding is never used for comments,
616         // so it must be a position.
617         DCHECK(locatable_tag == kNonstatementPositionTag ||
618                locatable_tag == kStatementPositionTag);
619         if (mode_mask_ & RelocInfo::kPositionMask) {
620           ReadTaggedPosition();
621           if (SetMode(GetPositionModeFromTag(locatable_tag))) return;
622         }
623       }
624     } else {
625       DCHECK(tag == kDefaultTag);
626       int extra_tag = GetExtraTag();
627       if (extra_tag == kPCJumpExtraTag) {
628         if (GetTopTag() == kVariableLengthPCJumpTopTag) {
629           AdvanceReadVariableLengthPCJump();
630         } else {
631           AdvanceReadPC();
632         }
633       } else if (extra_tag == kDataJumpExtraTag) {
634         int locatable_tag = GetTopTag();
635         if (locatable_tag == kCodeWithIdTag) {
636           if (SetMode(RelocInfo::CODE_TARGET_WITH_ID)) {
637             AdvanceReadId();
638             return;
639           }
640           Advance(kIntSize);
641         } else if (locatable_tag != kCommentTag) {
642           DCHECK(locatable_tag == kNonstatementPositionTag ||
643                  locatable_tag == kStatementPositionTag);
644           if (mode_mask_ & RelocInfo::kPositionMask) {
645             AdvanceReadPosition();
646             if (SetMode(GetPositionModeFromTag(locatable_tag))) return;
647           } else {
648             Advance(kIntSize);
649           }
650         } else {
651           DCHECK(locatable_tag == kCommentTag);
652           if (SetMode(RelocInfo::COMMENT)) {
653             AdvanceReadData();
654             return;
655           }
656           Advance(kIntptrSize);
657         }
658       } else if (extra_tag == kPoolExtraTag) {
659         int pool_type = GetTopTag();
660         DCHECK(pool_type == kConstPoolTag || pool_type == kVeneerPoolTag);
661         RelocInfo::Mode rmode = (pool_type == kConstPoolTag) ?
662           RelocInfo::CONST_POOL : RelocInfo::VENEER_POOL;
663         if (SetMode(rmode)) {
664           AdvanceReadPoolData();
665           return;
666         }
667         Advance(kIntSize);
668       } else {
669         AdvanceReadPC();
670         int rmode = extra_tag + RelocInfo::LAST_COMPACT_ENUM;
671         if (SetMode(static_cast<RelocInfo::Mode>(rmode))) return;
672       }
673     }
674   }
675   if (code_age_sequence_ != NULL) {
676     byte* old_code_age_sequence = code_age_sequence_;
677     code_age_sequence_ = NULL;
678     if (SetMode(RelocInfo::CODE_AGE_SEQUENCE)) {
679       rinfo_.data_ = 0;
680       rinfo_.pc_ = old_code_age_sequence;
681       return;
682     }
683   }
684   done_ = true;
685 }
686
687
688 RelocIterator::RelocIterator(Code* code, int mode_mask) {
689   rinfo_.host_ = code;
690   rinfo_.pc_ = code->instruction_start();
691   rinfo_.data_ = 0;
692   // Relocation info is read backwards.
693   pos_ = code->relocation_start() + code->relocation_size();
694   end_ = code->relocation_start();
695   done_ = false;
696   mode_mask_ = mode_mask;
697   last_id_ = 0;
698   last_position_ = 0;
699   byte* sequence = code->FindCodeAgeSequence();
700   // We get the isolate from the map, because at serialization time
701   // the code pointer has been cloned and isn't really in heap space.
702   Isolate* isolate = code->map()->GetIsolate();
703   if (sequence != NULL && !Code::IsYoungSequence(isolate, sequence)) {
704     code_age_sequence_ = sequence;
705   } else {
706     code_age_sequence_ = NULL;
707   }
708   if (mode_mask_ == 0) pos_ = end_;
709   next();
710 }
711
712
713 RelocIterator::RelocIterator(const CodeDesc& desc, int mode_mask) {
714   rinfo_.pc_ = desc.buffer;
715   rinfo_.data_ = 0;
716   // Relocation info is read backwards.
717   pos_ = desc.buffer + desc.buffer_size;
718   end_ = pos_ - desc.reloc_size;
719   done_ = false;
720   mode_mask_ = mode_mask;
721   last_id_ = 0;
722   last_position_ = 0;
723   code_age_sequence_ = NULL;
724   if (mode_mask_ == 0) pos_ = end_;
725   next();
726 }
727
728
729 // -----------------------------------------------------------------------------
730 // Implementation of RelocInfo
731
732
733 #ifdef DEBUG
734 bool RelocInfo::RequiresRelocation(const CodeDesc& desc) {
735   // Ensure there are no code targets or embedded objects present in the
736   // deoptimization entries, they would require relocation after code
737   // generation.
738   int mode_mask = RelocInfo::kCodeTargetMask |
739                   RelocInfo::ModeMask(RelocInfo::EMBEDDED_OBJECT) |
740                   RelocInfo::ModeMask(RelocInfo::CELL) |
741                   RelocInfo::kApplyMask;
742   RelocIterator it(desc, mode_mask);
743   return !it.done();
744 }
745 #endif
746
747
748 #ifdef ENABLE_DISASSEMBLER
749 const char* RelocInfo::RelocModeName(RelocInfo::Mode rmode) {
750   switch (rmode) {
751     case RelocInfo::NONE32:
752       return "no reloc 32";
753     case RelocInfo::NONE64:
754       return "no reloc 64";
755     case RelocInfo::EMBEDDED_OBJECT:
756       return "embedded object";
757     case RelocInfo::CONSTRUCT_CALL:
758       return "code target (js construct call)";
759     case RelocInfo::DEBUG_BREAK:
760       return "debug break";
761     case RelocInfo::CODE_TARGET:
762       return "code target";
763     case RelocInfo::CODE_TARGET_WITH_ID:
764       return "code target with id";
765     case RelocInfo::CELL:
766       return "property cell";
767     case RelocInfo::RUNTIME_ENTRY:
768       return "runtime entry";
769     case RelocInfo::JS_RETURN:
770       return "js return";
771     case RelocInfo::COMMENT:
772       return "comment";
773     case RelocInfo::POSITION:
774       return "position";
775     case RelocInfo::STATEMENT_POSITION:
776       return "statement position";
777     case RelocInfo::EXTERNAL_REFERENCE:
778       return "external reference";
779     case RelocInfo::INTERNAL_REFERENCE:
780       return "internal reference";
781     case RelocInfo::CONST_POOL:
782       return "constant pool";
783     case RelocInfo::VENEER_POOL:
784       return "veneer pool";
785     case RelocInfo::DEBUG_BREAK_SLOT:
786       return "debug break slot";
787     case RelocInfo::CODE_AGE_SEQUENCE:
788       return "code_age_sequence";
789     case RelocInfo::NUMBER_OF_MODES:
790       UNREACHABLE();
791       return "number_of_modes";
792   }
793   return "unknown relocation type";
794 }
795
796
797 void RelocInfo::Print(Isolate* isolate, OStream& os) {  // NOLINT
798   os << pc_ << "  " << RelocModeName(rmode_);
799   if (IsComment(rmode_)) {
800     os << "  (" << reinterpret_cast<char*>(data_) << ")";
801   } else if (rmode_ == EMBEDDED_OBJECT) {
802     os << "  (" << Brief(target_object()) << ")";
803   } else if (rmode_ == EXTERNAL_REFERENCE) {
804     ExternalReferenceEncoder ref_encoder(isolate);
805     os << " (" << ref_encoder.NameOfAddress(target_reference()) << ")  ("
806        << target_reference() << ")";
807   } else if (IsCodeTarget(rmode_)) {
808     Code* code = Code::GetCodeFromTargetAddress(target_address());
809     os << " (" << Code::Kind2String(code->kind()) << ")  (" << target_address()
810        << ")";
811     if (rmode_ == CODE_TARGET_WITH_ID) {
812       os << " (id=" << static_cast<int>(data_) << ")";
813     }
814   } else if (IsPosition(rmode_)) {
815     os << "  (" << data() << ")";
816   } else if (IsRuntimeEntry(rmode_) &&
817              isolate->deoptimizer_data() != NULL) {
818     // Depotimization bailouts are stored as runtime entries.
819     int id = Deoptimizer::GetDeoptimizationId(
820         isolate, target_address(), Deoptimizer::EAGER);
821     if (id != Deoptimizer::kNotDeoptimizationEntry) {
822       os << "  (deoptimization bailout " << id << ")";
823     }
824   }
825
826   os << "\n";
827 }
828 #endif  // ENABLE_DISASSEMBLER
829
830
831 #ifdef VERIFY_HEAP
832 void RelocInfo::Verify(Isolate* isolate) {
833   switch (rmode_) {
834     case EMBEDDED_OBJECT:
835       Object::VerifyPointer(target_object());
836       break;
837     case CELL:
838       Object::VerifyPointer(target_cell());
839       break;
840     case DEBUG_BREAK:
841     case CONSTRUCT_CALL:
842     case CODE_TARGET_WITH_ID:
843     case CODE_TARGET: {
844       // convert inline target address to code object
845       Address addr = target_address();
846       CHECK(addr != NULL);
847       // Check that we can find the right code object.
848       Code* code = Code::GetCodeFromTargetAddress(addr);
849       Object* found = isolate->FindCodeObject(addr);
850       CHECK(found->IsCode());
851       CHECK(code->address() == HeapObject::cast(found)->address());
852       break;
853     }
854     case RUNTIME_ENTRY:
855     case JS_RETURN:
856     case COMMENT:
857     case POSITION:
858     case STATEMENT_POSITION:
859     case EXTERNAL_REFERENCE:
860     case INTERNAL_REFERENCE:
861     case CONST_POOL:
862     case VENEER_POOL:
863     case DEBUG_BREAK_SLOT:
864     case NONE32:
865     case NONE64:
866       break;
867     case NUMBER_OF_MODES:
868       UNREACHABLE();
869       break;
870     case CODE_AGE_SEQUENCE:
871       DCHECK(Code::IsYoungSequence(isolate, pc_) || code_age_stub()->IsCode());
872       break;
873   }
874 }
875 #endif  // VERIFY_HEAP
876
877
878 // -----------------------------------------------------------------------------
879 // Implementation of ExternalReference
880
881 void ExternalReference::SetUp() {
882   double_constants.min_int = kMinInt;
883   double_constants.one_half = 0.5;
884   double_constants.minus_one_half = -0.5;
885   double_constants.canonical_non_hole_nan = base::OS::nan_value();
886   double_constants.the_hole_nan = BitCast<double>(kHoleNanInt64);
887   double_constants.negative_infinity = -V8_INFINITY;
888   double_constants.uint32_bias =
889     static_cast<double>(static_cast<uint32_t>(0xFFFFFFFF)) + 1;
890
891   math_exp_data_mutex = new base::Mutex();
892 }
893
894
895 void ExternalReference::InitializeMathExpData() {
896   // Early return?
897   if (math_exp_data_initialized) return;
898
899   base::LockGuard<base::Mutex> lock_guard(math_exp_data_mutex);
900   if (!math_exp_data_initialized) {
901     // If this is changed, generated code must be adapted too.
902     const int kTableSizeBits = 11;
903     const int kTableSize = 1 << kTableSizeBits;
904     const double kTableSizeDouble = static_cast<double>(kTableSize);
905
906     math_exp_constants_array = new double[9];
907     // Input values smaller than this always return 0.
908     math_exp_constants_array[0] = -708.39641853226408;
909     // Input values larger than this always return +Infinity.
910     math_exp_constants_array[1] = 709.78271289338397;
911     math_exp_constants_array[2] = V8_INFINITY;
912     // The rest is black magic. Do not attempt to understand it. It is
913     // loosely based on the "expd" function published at:
914     // http://herumi.blogspot.com/2011/08/fast-double-precision-exponential.html
915     const double constant3 = (1 << kTableSizeBits) / std::log(2.0);
916     math_exp_constants_array[3] = constant3;
917     math_exp_constants_array[4] =
918         static_cast<double>(static_cast<int64_t>(3) << 51);
919     math_exp_constants_array[5] = 1 / constant3;
920     math_exp_constants_array[6] = 3.0000000027955394;
921     math_exp_constants_array[7] = 0.16666666685227835;
922     math_exp_constants_array[8] = 1;
923
924     math_exp_log_table_array = new double[kTableSize];
925     for (int i = 0; i < kTableSize; i++) {
926       double value = std::pow(2, i / kTableSizeDouble);
927       uint64_t bits = BitCast<uint64_t, double>(value);
928       bits &= (static_cast<uint64_t>(1) << 52) - 1;
929       double mantissa = BitCast<double, uint64_t>(bits);
930       math_exp_log_table_array[i] = mantissa;
931     }
932
933     math_exp_data_initialized = true;
934   }
935 }
936
937
938 void ExternalReference::TearDownMathExpData() {
939   delete[] math_exp_constants_array;
940   delete[] math_exp_log_table_array;
941   delete math_exp_data_mutex;
942 }
943
944
945 ExternalReference::ExternalReference(Builtins::CFunctionId id, Isolate* isolate)
946   : address_(Redirect(isolate, Builtins::c_function_address(id))) {}
947
948
949 ExternalReference::ExternalReference(
950     ApiFunction* fun,
951     Type type = ExternalReference::BUILTIN_CALL,
952     Isolate* isolate = NULL)
953   : address_(Redirect(isolate, fun->address(), type)) {}
954
955
956 ExternalReference::ExternalReference(Builtins::Name name, Isolate* isolate)
957   : address_(isolate->builtins()->builtin_address(name)) {}
958
959
960 ExternalReference::ExternalReference(Runtime::FunctionId id,
961                                      Isolate* isolate)
962   : address_(Redirect(isolate, Runtime::FunctionForId(id)->entry)) {}
963
964
965 ExternalReference::ExternalReference(const Runtime::Function* f,
966                                      Isolate* isolate)
967   : address_(Redirect(isolate, f->entry)) {}
968
969
970 ExternalReference ExternalReference::isolate_address(Isolate* isolate) {
971   return ExternalReference(isolate);
972 }
973
974
975 ExternalReference::ExternalReference(const IC_Utility& ic_utility,
976                                      Isolate* isolate)
977   : address_(Redirect(isolate, ic_utility.address())) {}
978
979
980 ExternalReference::ExternalReference(StatsCounter* counter)
981   : address_(reinterpret_cast<Address>(counter->GetInternalPointer())) {}
982
983
984 ExternalReference::ExternalReference(Isolate::AddressId id, Isolate* isolate)
985   : address_(isolate->get_address_from_id(id)) {}
986
987
988 ExternalReference::ExternalReference(const SCTableReference& table_ref)
989   : address_(table_ref.address()) {}
990
991
992 ExternalReference ExternalReference::
993     incremental_marking_record_write_function(Isolate* isolate) {
994   return ExternalReference(Redirect(
995       isolate,
996       FUNCTION_ADDR(IncrementalMarking::RecordWriteFromCode)));
997 }
998
999
1000 ExternalReference ExternalReference::
1001     store_buffer_overflow_function(Isolate* isolate) {
1002   return ExternalReference(Redirect(
1003       isolate,
1004       FUNCTION_ADDR(StoreBuffer::StoreBufferOverflow)));
1005 }
1006
1007
1008 ExternalReference ExternalReference::flush_icache_function(Isolate* isolate) {
1009   return ExternalReference(
1010       Redirect(isolate, FUNCTION_ADDR(CpuFeatures::FlushICache)));
1011 }
1012
1013
1014 ExternalReference ExternalReference::delete_handle_scope_extensions(
1015     Isolate* isolate) {
1016   return ExternalReference(Redirect(
1017       isolate,
1018       FUNCTION_ADDR(HandleScope::DeleteExtensions)));
1019 }
1020
1021
1022 ExternalReference ExternalReference::get_date_field_function(
1023     Isolate* isolate) {
1024   return ExternalReference(Redirect(isolate, FUNCTION_ADDR(JSDate::GetField)));
1025 }
1026
1027
1028 ExternalReference ExternalReference::get_make_code_young_function(
1029     Isolate* isolate) {
1030   return ExternalReference(Redirect(
1031       isolate, FUNCTION_ADDR(Code::MakeCodeAgeSequenceYoung)));
1032 }
1033
1034
1035 ExternalReference ExternalReference::get_mark_code_as_executed_function(
1036     Isolate* isolate) {
1037   return ExternalReference(Redirect(
1038       isolate, FUNCTION_ADDR(Code::MarkCodeAsExecuted)));
1039 }
1040
1041
1042 ExternalReference ExternalReference::date_cache_stamp(Isolate* isolate) {
1043   return ExternalReference(isolate->date_cache()->stamp_address());
1044 }
1045
1046
1047 ExternalReference ExternalReference::stress_deopt_count(Isolate* isolate) {
1048   return ExternalReference(isolate->stress_deopt_count_address());
1049 }
1050
1051
1052 ExternalReference ExternalReference::new_deoptimizer_function(
1053     Isolate* isolate) {
1054   return ExternalReference(
1055       Redirect(isolate, FUNCTION_ADDR(Deoptimizer::New)));
1056 }
1057
1058
1059 ExternalReference ExternalReference::compute_output_frames_function(
1060     Isolate* isolate) {
1061   return ExternalReference(
1062       Redirect(isolate, FUNCTION_ADDR(Deoptimizer::ComputeOutputFrames)));
1063 }
1064
1065
1066 ExternalReference ExternalReference::log_enter_external_function(
1067     Isolate* isolate) {
1068   return ExternalReference(
1069       Redirect(isolate, FUNCTION_ADDR(Logger::EnterExternal)));
1070 }
1071
1072
1073 ExternalReference ExternalReference::log_leave_external_function(
1074     Isolate* isolate) {
1075   return ExternalReference(
1076       Redirect(isolate, FUNCTION_ADDR(Logger::LeaveExternal)));
1077 }
1078
1079
1080 ExternalReference ExternalReference::keyed_lookup_cache_keys(Isolate* isolate) {
1081   return ExternalReference(isolate->keyed_lookup_cache()->keys_address());
1082 }
1083
1084
1085 ExternalReference ExternalReference::keyed_lookup_cache_field_offsets(
1086     Isolate* isolate) {
1087   return ExternalReference(
1088       isolate->keyed_lookup_cache()->field_offsets_address());
1089 }
1090
1091
1092 ExternalReference ExternalReference::roots_array_start(Isolate* isolate) {
1093   return ExternalReference(isolate->heap()->roots_array_start());
1094 }
1095
1096
1097 ExternalReference ExternalReference::allocation_sites_list_address(
1098     Isolate* isolate) {
1099   return ExternalReference(isolate->heap()->allocation_sites_list_address());
1100 }
1101
1102
1103 ExternalReference ExternalReference::address_of_stack_limit(Isolate* isolate) {
1104   return ExternalReference(isolate->stack_guard()->address_of_jslimit());
1105 }
1106
1107
1108 ExternalReference ExternalReference::address_of_real_stack_limit(
1109     Isolate* isolate) {
1110   return ExternalReference(isolate->stack_guard()->address_of_real_jslimit());
1111 }
1112
1113
1114 ExternalReference ExternalReference::address_of_regexp_stack_limit(
1115     Isolate* isolate) {
1116   return ExternalReference(isolate->regexp_stack()->limit_address());
1117 }
1118
1119
1120 ExternalReference ExternalReference::new_space_start(Isolate* isolate) {
1121   return ExternalReference(isolate->heap()->NewSpaceStart());
1122 }
1123
1124
1125 ExternalReference ExternalReference::store_buffer_top(Isolate* isolate) {
1126   return ExternalReference(isolate->heap()->store_buffer()->TopAddress());
1127 }
1128
1129
1130 ExternalReference ExternalReference::new_space_mask(Isolate* isolate) {
1131   return ExternalReference(reinterpret_cast<Address>(
1132       isolate->heap()->NewSpaceMask()));
1133 }
1134
1135
1136 ExternalReference ExternalReference::new_space_allocation_top_address(
1137     Isolate* isolate) {
1138   return ExternalReference(isolate->heap()->NewSpaceAllocationTopAddress());
1139 }
1140
1141
1142 ExternalReference ExternalReference::new_space_allocation_limit_address(
1143     Isolate* isolate) {
1144   return ExternalReference(isolate->heap()->NewSpaceAllocationLimitAddress());
1145 }
1146
1147
1148 ExternalReference ExternalReference::old_pointer_space_allocation_top_address(
1149     Isolate* isolate) {
1150   return ExternalReference(
1151       isolate->heap()->OldPointerSpaceAllocationTopAddress());
1152 }
1153
1154
1155 ExternalReference ExternalReference::old_pointer_space_allocation_limit_address(
1156     Isolate* isolate) {
1157   return ExternalReference(
1158       isolate->heap()->OldPointerSpaceAllocationLimitAddress());
1159 }
1160
1161
1162 ExternalReference ExternalReference::old_data_space_allocation_top_address(
1163     Isolate* isolate) {
1164   return ExternalReference(
1165       isolate->heap()->OldDataSpaceAllocationTopAddress());
1166 }
1167
1168
1169 ExternalReference ExternalReference::old_data_space_allocation_limit_address(
1170     Isolate* isolate) {
1171   return ExternalReference(
1172       isolate->heap()->OldDataSpaceAllocationLimitAddress());
1173 }
1174
1175
1176 ExternalReference ExternalReference::handle_scope_level_address(
1177     Isolate* isolate) {
1178   return ExternalReference(HandleScope::current_level_address(isolate));
1179 }
1180
1181
1182 ExternalReference ExternalReference::handle_scope_next_address(
1183     Isolate* isolate) {
1184   return ExternalReference(HandleScope::current_next_address(isolate));
1185 }
1186
1187
1188 ExternalReference ExternalReference::handle_scope_limit_address(
1189     Isolate* isolate) {
1190   return ExternalReference(HandleScope::current_limit_address(isolate));
1191 }
1192
1193
1194 ExternalReference ExternalReference::scheduled_exception_address(
1195     Isolate* isolate) {
1196   return ExternalReference(isolate->scheduled_exception_address());
1197 }
1198
1199
1200 ExternalReference ExternalReference::address_of_pending_message_obj(
1201     Isolate* isolate) {
1202   return ExternalReference(isolate->pending_message_obj_address());
1203 }
1204
1205
1206 ExternalReference ExternalReference::address_of_has_pending_message(
1207     Isolate* isolate) {
1208   return ExternalReference(isolate->has_pending_message_address());
1209 }
1210
1211
1212 ExternalReference ExternalReference::address_of_pending_message_script(
1213     Isolate* isolate) {
1214   return ExternalReference(isolate->pending_message_script_address());
1215 }
1216
1217
1218 ExternalReference ExternalReference::address_of_min_int() {
1219   return ExternalReference(reinterpret_cast<void*>(&double_constants.min_int));
1220 }
1221
1222
1223 ExternalReference ExternalReference::address_of_one_half() {
1224   return ExternalReference(reinterpret_cast<void*>(&double_constants.one_half));
1225 }
1226
1227
1228 ExternalReference ExternalReference::address_of_minus_one_half() {
1229   return ExternalReference(
1230       reinterpret_cast<void*>(&double_constants.minus_one_half));
1231 }
1232
1233
1234 ExternalReference ExternalReference::address_of_negative_infinity() {
1235   return ExternalReference(
1236       reinterpret_cast<void*>(&double_constants.negative_infinity));
1237 }
1238
1239
1240 ExternalReference ExternalReference::address_of_canonical_non_hole_nan() {
1241   return ExternalReference(
1242       reinterpret_cast<void*>(&double_constants.canonical_non_hole_nan));
1243 }
1244
1245
1246 ExternalReference ExternalReference::address_of_the_hole_nan() {
1247   return ExternalReference(
1248       reinterpret_cast<void*>(&double_constants.the_hole_nan));
1249 }
1250
1251
1252 ExternalReference ExternalReference::address_of_uint32_bias() {
1253   return ExternalReference(
1254       reinterpret_cast<void*>(&double_constants.uint32_bias));
1255 }
1256
1257
1258 ExternalReference ExternalReference::is_profiling_address(Isolate* isolate) {
1259   return ExternalReference(isolate->cpu_profiler()->is_profiling_address());
1260 }
1261
1262
1263 ExternalReference ExternalReference::invoke_function_callback(
1264     Isolate* isolate) {
1265   Address thunk_address = FUNCTION_ADDR(&InvokeFunctionCallback);
1266   ExternalReference::Type thunk_type = ExternalReference::PROFILING_API_CALL;
1267   ApiFunction thunk_fun(thunk_address);
1268   return ExternalReference(&thunk_fun, thunk_type, isolate);
1269 }
1270
1271
1272 ExternalReference ExternalReference::invoke_accessor_getter_callback(
1273     Isolate* isolate) {
1274   Address thunk_address = FUNCTION_ADDR(&InvokeAccessorGetterCallback);
1275   ExternalReference::Type thunk_type =
1276       ExternalReference::PROFILING_GETTER_CALL;
1277   ApiFunction thunk_fun(thunk_address);
1278   return ExternalReference(&thunk_fun, thunk_type, isolate);
1279 }
1280
1281
1282 #ifndef V8_INTERPRETED_REGEXP
1283
1284 ExternalReference ExternalReference::re_check_stack_guard_state(
1285     Isolate* isolate) {
1286   Address function;
1287 #if V8_TARGET_ARCH_X64
1288   function = FUNCTION_ADDR(RegExpMacroAssemblerX64::CheckStackGuardState);
1289 #elif V8_TARGET_ARCH_IA32
1290   function = FUNCTION_ADDR(RegExpMacroAssemblerIA32::CheckStackGuardState);
1291 #elif V8_TARGET_ARCH_ARM64
1292   function = FUNCTION_ADDR(RegExpMacroAssemblerARM64::CheckStackGuardState);
1293 #elif V8_TARGET_ARCH_ARM
1294   function = FUNCTION_ADDR(RegExpMacroAssemblerARM::CheckStackGuardState);
1295 #elif V8_TARGET_ARCH_MIPS
1296   function = FUNCTION_ADDR(RegExpMacroAssemblerMIPS::CheckStackGuardState);
1297 #elif V8_TARGET_ARCH_MIPS64
1298   function = FUNCTION_ADDR(RegExpMacroAssemblerMIPS::CheckStackGuardState);
1299 #elif V8_TARGET_ARCH_X87
1300   function = FUNCTION_ADDR(RegExpMacroAssemblerX87::CheckStackGuardState);
1301 #else
1302   UNREACHABLE();
1303 #endif
1304   return ExternalReference(Redirect(isolate, function));
1305 }
1306
1307
1308 ExternalReference ExternalReference::re_grow_stack(Isolate* isolate) {
1309   return ExternalReference(
1310       Redirect(isolate, FUNCTION_ADDR(NativeRegExpMacroAssembler::GrowStack)));
1311 }
1312
1313 ExternalReference ExternalReference::re_case_insensitive_compare_uc16(
1314     Isolate* isolate) {
1315   return ExternalReference(Redirect(
1316       isolate,
1317       FUNCTION_ADDR(NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16)));
1318 }
1319
1320
1321 ExternalReference ExternalReference::re_word_character_map() {
1322   return ExternalReference(
1323       NativeRegExpMacroAssembler::word_character_map_address());
1324 }
1325
1326 ExternalReference ExternalReference::address_of_static_offsets_vector(
1327     Isolate* isolate) {
1328   return ExternalReference(
1329       reinterpret_cast<Address>(isolate->jsregexp_static_offsets_vector()));
1330 }
1331
1332 ExternalReference ExternalReference::address_of_regexp_stack_memory_address(
1333     Isolate* isolate) {
1334   return ExternalReference(
1335       isolate->regexp_stack()->memory_address());
1336 }
1337
1338 ExternalReference ExternalReference::address_of_regexp_stack_memory_size(
1339     Isolate* isolate) {
1340   return ExternalReference(isolate->regexp_stack()->memory_size_address());
1341 }
1342
1343 #endif  // V8_INTERPRETED_REGEXP
1344
1345
1346 ExternalReference ExternalReference::math_log_double_function(
1347     Isolate* isolate) {
1348   typedef double (*d2d)(double x);
1349   return ExternalReference(Redirect(isolate,
1350                                     FUNCTION_ADDR(static_cast<d2d>(std::log)),
1351                                     BUILTIN_FP_CALL));
1352 }
1353
1354
1355 ExternalReference ExternalReference::math_exp_constants(int constant_index) {
1356   DCHECK(math_exp_data_initialized);
1357   return ExternalReference(
1358       reinterpret_cast<void*>(math_exp_constants_array + constant_index));
1359 }
1360
1361
1362 ExternalReference ExternalReference::math_exp_log_table() {
1363   DCHECK(math_exp_data_initialized);
1364   return ExternalReference(reinterpret_cast<void*>(math_exp_log_table_array));
1365 }
1366
1367
1368 ExternalReference ExternalReference::page_flags(Page* page) {
1369   return ExternalReference(reinterpret_cast<Address>(page) +
1370                            MemoryChunk::kFlagsOffset);
1371 }
1372
1373
1374 ExternalReference ExternalReference::ForDeoptEntry(Address entry) {
1375   return ExternalReference(entry);
1376 }
1377
1378
1379 ExternalReference ExternalReference::cpu_features() {
1380   DCHECK(CpuFeatures::initialized_);
1381   return ExternalReference(&CpuFeatures::supported_);
1382 }
1383
1384
1385 ExternalReference ExternalReference::debug_is_active_address(
1386     Isolate* isolate) {
1387   return ExternalReference(isolate->debug()->is_active_address());
1388 }
1389
1390
1391 ExternalReference ExternalReference::debug_after_break_target_address(
1392     Isolate* isolate) {
1393   return ExternalReference(isolate->debug()->after_break_target_address());
1394 }
1395
1396
1397 ExternalReference
1398     ExternalReference::debug_restarter_frame_function_pointer_address(
1399         Isolate* isolate) {
1400   return ExternalReference(
1401       isolate->debug()->restarter_frame_function_pointer_address());
1402 }
1403
1404
1405 double power_helper(double x, double y) {
1406   int y_int = static_cast<int>(y);
1407   if (y == y_int) {
1408     return power_double_int(x, y_int);  // Returns 1 if exponent is 0.
1409   }
1410   if (y == 0.5) {
1411     return (std::isinf(x)) ? V8_INFINITY
1412                            : fast_sqrt(x + 0.0);  // Convert -0 to +0.
1413   }
1414   if (y == -0.5) {
1415     return (std::isinf(x)) ? 0 : 1.0 / fast_sqrt(x + 0.0);  // Convert -0 to +0.
1416   }
1417   return power_double_double(x, y);
1418 }
1419
1420
1421 // Helper function to compute x^y, where y is known to be an
1422 // integer. Uses binary decomposition to limit the number of
1423 // multiplications; see the discussion in "Hacker's Delight" by Henry
1424 // S. Warren, Jr., figure 11-6, page 213.
1425 double power_double_int(double x, int y) {
1426   double m = (y < 0) ? 1 / x : x;
1427   unsigned n = (y < 0) ? -y : y;
1428   double p = 1;
1429   while (n != 0) {
1430     if ((n & 1) != 0) p *= m;
1431     m *= m;
1432     if ((n & 2) != 0) p *= m;
1433     m *= m;
1434     n >>= 2;
1435   }
1436   return p;
1437 }
1438
1439
1440 double power_double_double(double x, double y) {
1441 #if defined(__MINGW64_VERSION_MAJOR) && \
1442     (!defined(__MINGW64_VERSION_RC) || __MINGW64_VERSION_RC < 1)
1443   // MinGW64 has a custom implementation for pow.  This handles certain
1444   // special cases that are different.
1445   if ((x == 0.0 || std::isinf(x)) && std::isfinite(y)) {
1446     double f;
1447     if (std::modf(y, &f) != 0.0) {
1448       return ((x == 0.0) ^ (y > 0)) ? V8_INFINITY : 0;
1449     }
1450   }
1451
1452   if (x == 2.0) {
1453     int y_int = static_cast<int>(y);
1454     if (y == y_int) {
1455       return std::ldexp(1.0, y_int);
1456     }
1457   }
1458 #endif
1459
1460   // The checks for special cases can be dropped in ia32 because it has already
1461   // been done in generated code before bailing out here.
1462   if (std::isnan(y) || ((x == 1 || x == -1) && std::isinf(y))) {
1463     return base::OS::nan_value();
1464   }
1465   return std::pow(x, y);
1466 }
1467
1468
1469 ExternalReference ExternalReference::power_double_double_function(
1470     Isolate* isolate) {
1471   return ExternalReference(Redirect(isolate,
1472                                     FUNCTION_ADDR(power_double_double),
1473                                     BUILTIN_FP_FP_CALL));
1474 }
1475
1476
1477 ExternalReference ExternalReference::power_double_int_function(
1478     Isolate* isolate) {
1479   return ExternalReference(Redirect(isolate,
1480                                     FUNCTION_ADDR(power_double_int),
1481                                     BUILTIN_FP_INT_CALL));
1482 }
1483
1484
1485 bool EvalComparison(Token::Value op, double op1, double op2) {
1486   DCHECK(Token::IsCompareOp(op));
1487   switch (op) {
1488     case Token::EQ:
1489     case Token::EQ_STRICT: return (op1 == op2);
1490     case Token::NE: return (op1 != op2);
1491     case Token::LT: return (op1 < op2);
1492     case Token::GT: return (op1 > op2);
1493     case Token::LTE: return (op1 <= op2);
1494     case Token::GTE: return (op1 >= op2);
1495     default:
1496       UNREACHABLE();
1497       return false;
1498   }
1499 }
1500
1501
1502 ExternalReference ExternalReference::mod_two_doubles_operation(
1503     Isolate* isolate) {
1504   return ExternalReference(Redirect(isolate,
1505                                     FUNCTION_ADDR(modulo),
1506                                     BUILTIN_FP_FP_CALL));
1507 }
1508
1509
1510 ExternalReference ExternalReference::debug_break(Isolate* isolate) {
1511   return ExternalReference(Redirect(isolate, FUNCTION_ADDR(Debug_Break)));
1512 }
1513
1514
1515 ExternalReference ExternalReference::debug_step_in_fp_address(
1516     Isolate* isolate) {
1517   return ExternalReference(isolate->debug()->step_in_fp_addr());
1518 }
1519
1520
1521 void PositionsRecorder::RecordPosition(int pos) {
1522   DCHECK(pos != RelocInfo::kNoPosition);
1523   DCHECK(pos >= 0);
1524   state_.current_position = pos;
1525   LOG_CODE_EVENT(assembler_->isolate(),
1526                  CodeLinePosInfoAddPositionEvent(jit_handler_data_,
1527                                                  assembler_->pc_offset(),
1528                                                  pos));
1529 }
1530
1531
1532 void PositionsRecorder::RecordStatementPosition(int pos) {
1533   DCHECK(pos != RelocInfo::kNoPosition);
1534   DCHECK(pos >= 0);
1535   state_.current_statement_position = pos;
1536   LOG_CODE_EVENT(assembler_->isolate(),
1537                  CodeLinePosInfoAddStatementPositionEvent(
1538                      jit_handler_data_,
1539                      assembler_->pc_offset(),
1540                      pos));
1541 }
1542
1543
1544 bool PositionsRecorder::WriteRecordedPositions() {
1545   bool written = false;
1546
1547   // Write the statement position if it is different from what was written last
1548   // time.
1549   if (state_.current_statement_position != state_.written_statement_position) {
1550     EnsureSpace ensure_space(assembler_);
1551     assembler_->RecordRelocInfo(RelocInfo::STATEMENT_POSITION,
1552                                 state_.current_statement_position);
1553     state_.written_statement_position = state_.current_statement_position;
1554     written = true;
1555   }
1556
1557   // Write the position if it is different from what was written last time and
1558   // also different from the written statement position.
1559   if (state_.current_position != state_.written_position &&
1560       state_.current_position != state_.written_statement_position) {
1561     EnsureSpace ensure_space(assembler_);
1562     assembler_->RecordRelocInfo(RelocInfo::POSITION, state_.current_position);
1563     state_.written_position = state_.current_position;
1564     written = true;
1565   }
1566
1567   // Return whether something was written.
1568   return written;
1569 }
1570
1571
1572 MultiplierAndShift::MultiplierAndShift(int32_t d) {
1573   DCHECK(d <= -2 || 2 <= d);
1574   const uint32_t two31 = 0x80000000;
1575   uint32_t ad = Abs(d);
1576   uint32_t t = two31 + (uint32_t(d) >> 31);
1577   uint32_t anc = t - 1 - t % ad;   // Absolute value of nc.
1578   int32_t p = 31;                  // Init. p.
1579   uint32_t q1 = two31 / anc;       // Init. q1 = 2**p/|nc|.
1580   uint32_t r1 = two31 - q1 * anc;  // Init. r1 = rem(2**p, |nc|).
1581   uint32_t q2 = two31 / ad;        // Init. q2 = 2**p/|d|.
1582   uint32_t r2 = two31 - q2 * ad;   // Init. r2 = rem(2**p, |d|).
1583   uint32_t delta;
1584   do {
1585     p++;
1586     q1 *= 2;          // Update q1 = 2**p/|nc|.
1587     r1 *= 2;          // Update r1 = rem(2**p, |nc|).
1588     if (r1 >= anc) {  // Must be an unsigned comparison here.
1589       q1++;
1590       r1 = r1 - anc;
1591     }
1592     q2 *= 2;          // Update q2 = 2**p/|d|.
1593     r2 *= 2;          // Update r2 = rem(2**p, |d|).
1594     if (r2 >= ad) {   // Must be an unsigned comparison here.
1595       q2++;
1596       r2 = r2 - ad;
1597     }
1598     delta = ad - r2;
1599   } while (q1 < delta || (q1 == delta && r1 == 0));
1600   int32_t mul = static_cast<int32_t>(q2 + 1);
1601   multiplier_ = (d < 0) ? -mul : mul;
1602   shift_ = p - 32;
1603 }
1604
1605 } }  // namespace v8::internal