1 // Copyright (c) 1994-2006 Sun Microsystems Inc.
2 // All Rights Reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // - Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
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.
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.
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.
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.
35 #ifndef V8_ASSEMBLER_H_
36 #define V8_ASSEMBLER_H_
40 #include "src/allocation.h"
41 #include "src/builtins.h"
42 #include "src/gdb-jit.h"
43 #include "src/isolate.h"
44 #include "src/runtime.h"
45 #include "src/token.h"
54 // -----------------------------------------------------------------------------
55 // Platform independent assembler base class.
57 class AssemblerBase: public Malloced {
59 AssemblerBase(Isolate* isolate, void* buffer, int buffer_size);
60 virtual ~AssemblerBase();
62 Isolate* isolate() const { return isolate_; }
63 int jit_cookie() const { return jit_cookie_; }
65 bool emit_debug_code() const { return emit_debug_code_; }
66 void set_emit_debug_code(bool value) { emit_debug_code_ = value; }
68 bool serializer_enabled() const { return serializer_enabled_; }
69 void enable_serializer() { serializer_enabled_ = true; }
71 bool predictable_code_size() const { return predictable_code_size_; }
72 void set_predictable_code_size(bool value) { predictable_code_size_ = value; }
74 uint64_t enabled_cpu_features() const { return enabled_cpu_features_; }
75 void set_enabled_cpu_features(uint64_t features) {
76 enabled_cpu_features_ = features;
78 bool IsEnabled(CpuFeature f) {
79 return (enabled_cpu_features_ & (static_cast<uint64_t>(1) << f)) != 0;
82 // Overwrite a host NaN with a quiet target NaN. Used by mksnapshot for
83 // cross-snapshotting.
84 static void QuietNaN(HeapObject* nan) { }
86 int pc_offset() const { return static_cast<int>(pc_ - buffer_); }
88 // This function is called when code generation is aborted, so that
89 // the assembler could clean up internal data structures.
90 virtual void AbortedCodeGeneration() { }
92 static const int kMinimalBufferSize = 4*KB;
95 // The buffer into which code and relocation info are generated. It could
96 // either be owned by the assembler or be provided externally.
101 // The program counter, which points into the buffer above and moves forward.
107 uint64_t enabled_cpu_features_;
108 bool emit_debug_code_;
109 bool predictable_code_size_;
110 bool serializer_enabled_;
114 // Avoids emitting debug code during the lifetime of this scope object.
115 class DontEmitDebugCodeScope BASE_EMBEDDED {
117 explicit DontEmitDebugCodeScope(AssemblerBase* assembler)
118 : assembler_(assembler), old_value_(assembler->emit_debug_code()) {
119 assembler_->set_emit_debug_code(false);
121 ~DontEmitDebugCodeScope() {
122 assembler_->set_emit_debug_code(old_value_);
125 AssemblerBase* assembler_;
130 // Avoids using instructions that vary in size in unpredictable ways between the
131 // snapshot and the running VM.
132 class PredictableCodeSizeScope {
134 PredictableCodeSizeScope(AssemblerBase* assembler, int expected_size);
135 ~PredictableCodeSizeScope();
138 AssemblerBase* assembler_;
145 // Enable a specified feature within a scope.
146 class CpuFeatureScope BASE_EMBEDDED {
149 CpuFeatureScope(AssemblerBase* assembler, CpuFeature f);
153 AssemblerBase* assembler_;
154 uint64_t old_enabled_;
156 CpuFeatureScope(AssemblerBase* assembler, CpuFeature f) {}
161 // CpuFeatures keeps track of which features are supported by the target CPU.
162 // Supported features must be enabled by a CpuFeatureScope before use.
164 // if (assembler->IsSupported(SSE3)) {
165 // CpuFeatureScope fscope(assembler, SSE3);
166 // // Generate code containing SSE3 instructions.
168 // // Generate alternative code.
170 class CpuFeatures : public AllStatic {
172 static void Probe(bool cross_compile) {
173 STATIC_ASSERT(NUMBER_OF_CPU_FEATURES <= kBitsPerInt);
174 if (initialized_) return;
176 ProbeImpl(cross_compile);
179 static unsigned SupportedFeatures() {
184 static bool IsSupported(CpuFeature f) {
185 return (supported_ & (1u << f)) != 0;
188 static inline bool SupportsCrankshaft();
190 static inline unsigned cache_line_size() {
191 DCHECK(cache_line_size_ != 0);
192 return cache_line_size_;
195 static void PrintTarget();
196 static void PrintFeatures();
198 // Flush instruction cache.
199 static void FlushICache(void* start, size_t size);
202 // Platform-dependent implementation.
203 static void ProbeImpl(bool cross_compile);
205 static unsigned supported_;
206 static unsigned cache_line_size_;
207 static bool initialized_;
208 friend class ExternalReference;
209 DISALLOW_COPY_AND_ASSIGN(CpuFeatures);
213 // -----------------------------------------------------------------------------
214 // Labels represent pc locations; they are typically jump or call targets.
215 // After declaration, a label can be freely used to denote known or (yet)
216 // unknown pc location. Assembler::bind() is used to bind a label to the
217 // current pc. A label can be bound only once.
219 class Label BASE_EMBEDDED {
231 DCHECK(!is_linked());
232 DCHECK(!is_near_linked());
235 INLINE(void Unuse()) { pos_ = 0; }
236 INLINE(void UnuseNear()) { near_link_pos_ = 0; }
238 INLINE(bool is_bound() const) { return pos_ < 0; }
239 INLINE(bool is_unused() const) { return pos_ == 0 && near_link_pos_ == 0; }
240 INLINE(bool is_linked() const) { return pos_ > 0; }
241 INLINE(bool is_near_linked() const) { return near_link_pos_ > 0; }
243 // Returns the position of bound or linked labels. Cannot be used
244 // for unused labels.
246 int near_link_pos() const { return near_link_pos_ - 1; }
249 // pos_ encodes both the binding state (via its sign)
250 // and the binding position (via its value) of a label.
252 // pos_ < 0 bound label, pos() returns the jump target position
253 // pos_ == 0 unused label
254 // pos_ > 0 linked label, pos() returns the last reference position
257 // Behaves like |pos_| in the "> 0" case, but for near jumps to this label.
260 void bind_to(int pos) {
264 void link_to(int pos, Distance distance = kFar) {
265 if (distance == kNear) {
266 near_link_pos_ = pos + 1;
267 DCHECK(is_near_linked());
274 friend class Assembler;
275 friend class Displacement;
276 friend class RegExpMacroAssemblerIrregexp;
278 #if V8_TARGET_ARCH_ARM64
279 // On ARM64, the Assembler keeps track of pointers to Labels to resolve
280 // branches to distant targets. Copying labels would confuse the Assembler.
281 DISALLOW_COPY_AND_ASSIGN(Label); // NOLINT
286 enum SaveFPRegsMode { kDontSaveFPRegs, kSaveFPRegs };
288 // Specifies whether to perform icache flush operations on RelocInfo updates.
289 // If FLUSH_ICACHE_IF_NEEDED, the icache will always be flushed if an
290 // instruction was modified. If SKIP_ICACHE_FLUSH the flush will always be
291 // skipped (only use this if you will flush the icache manually before it is
293 enum ICacheFlushMode { FLUSH_ICACHE_IF_NEEDED, SKIP_ICACHE_FLUSH };
295 // -----------------------------------------------------------------------------
296 // Relocation information
299 // Relocation information consists of the address (pc) of the datum
300 // to which the relocation information applies, the relocation mode
301 // (rmode), and an optional data field. The relocation mode may be
302 // "descriptive" and not indicate a need for relocation, but simply
303 // describe a property of the datum. Such rmodes are useful for GC
304 // and nice disassembly output.
308 // The constant kNoPosition is used with the collecting of source positions
309 // in the relocation information. Two types of source positions are collected
310 // "position" (RelocMode position) and "statement position" (RelocMode
311 // statement_position). The "position" is collected at places in the source
312 // code which are of interest when making stack traces to pin-point the source
313 // location of a stack frame as close as possible. The "statement position" is
314 // collected at the beginning at each statement, and is used to indicate
315 // possible break locations. kNoPosition is used to indicate an
316 // invalid/uninitialized position value.
317 static const int kNoPosition = -1;
319 // This string is used to add padding comments to the reloc info in cases
320 // where we are not sure to have enough space for patching in during
321 // lazy deoptimization. This is the case if we have indirect calls for which
322 // we do not normally record relocation info.
323 static const char* const kFillerCommentString;
325 // The minimum size of a comment is equal to three bytes for the extra tagged
326 // pc + the tag for the data, and kPointerSize for the actual pointer to the
328 static const int kMinRelocCommentSize = 3 + kPointerSize;
330 // The maximum size for a call instruction including pc-jump.
331 static const int kMaxCallSize = 6;
333 // The maximum pc delta that will use the short encoding.
334 static const int kMaxSmallPCDelta;
337 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
338 CODE_TARGET, // Code target which is not any of the above.
340 CONSTRUCT_CALL, // code target that is a call to a JavaScript constructor.
341 DEBUG_BREAK, // Code target for the debugger statement.
345 // Everything after runtime_entry (inclusive) is not GC'ed.
347 JS_RETURN, // Marks start of the ExitJSFrame code.
349 POSITION, // See comment for kNoPosition above.
350 STATEMENT_POSITION, // See comment for kNoPosition above.
351 DEBUG_BREAK_SLOT, // Additional code inserted for debug break slot.
352 EXTERNAL_REFERENCE, // The address of an external C++ function.
353 INTERNAL_REFERENCE, // An address inside the same function.
355 // Marks constant and veneer pools. Only used on ARM and ARM64.
356 // They use a custom noncompact encoding.
360 // add more as needed
362 NUMBER_OF_MODES, // There are at most 15 modes with noncompact encoding.
363 NONE32, // never recorded 32-bit value
364 NONE64, // never recorded 64-bit value
365 CODE_AGE_SEQUENCE, // Not stored in RelocInfo array, used explictly by
367 FIRST_REAL_RELOC_MODE = CODE_TARGET,
368 LAST_REAL_RELOC_MODE = VENEER_POOL,
369 FIRST_PSEUDO_RELOC_MODE = CODE_AGE_SEQUENCE,
370 LAST_PSEUDO_RELOC_MODE = CODE_AGE_SEQUENCE,
371 LAST_CODE_ENUM = DEBUG_BREAK,
372 LAST_GCED_ENUM = CELL,
373 // Modes <= LAST_COMPACT_ENUM are guaranteed to have compact encoding.
374 LAST_COMPACT_ENUM = CODE_TARGET_WITH_ID,
375 LAST_STANDARD_NONCOMPACT_ENUM = INTERNAL_REFERENCE
380 RelocInfo(byte* pc, Mode rmode, intptr_t data, Code* host)
381 : pc_(pc), rmode_(rmode), data_(data), host_(host) {
383 RelocInfo(byte* pc, double data64)
384 : pc_(pc), rmode_(NONE64), data64_(data64), host_(NULL) {
387 static inline bool IsRealRelocMode(Mode mode) {
388 return mode >= FIRST_REAL_RELOC_MODE &&
389 mode <= LAST_REAL_RELOC_MODE;
391 static inline bool IsPseudoRelocMode(Mode mode) {
392 DCHECK(!IsRealRelocMode(mode));
393 return mode >= FIRST_PSEUDO_RELOC_MODE &&
394 mode <= LAST_PSEUDO_RELOC_MODE;
396 static inline bool IsConstructCall(Mode mode) {
397 return mode == CONSTRUCT_CALL;
399 static inline bool IsCodeTarget(Mode mode) {
400 return mode <= LAST_CODE_ENUM;
402 static inline bool IsEmbeddedObject(Mode mode) {
403 return mode == EMBEDDED_OBJECT;
405 static inline bool IsRuntimeEntry(Mode mode) {
406 return mode == RUNTIME_ENTRY;
408 // Is the relocation mode affected by GC?
409 static inline bool IsGCRelocMode(Mode mode) {
410 return mode <= LAST_GCED_ENUM;
412 static inline bool IsJSReturn(Mode mode) {
413 return mode == JS_RETURN;
415 static inline bool IsComment(Mode mode) {
416 return mode == COMMENT;
418 static inline bool IsConstPool(Mode mode) {
419 return mode == CONST_POOL;
421 static inline bool IsVeneerPool(Mode mode) {
422 return mode == VENEER_POOL;
424 static inline bool IsPosition(Mode mode) {
425 return mode == POSITION || mode == STATEMENT_POSITION;
427 static inline bool IsStatementPosition(Mode mode) {
428 return mode == STATEMENT_POSITION;
430 static inline bool IsExternalReference(Mode mode) {
431 return mode == EXTERNAL_REFERENCE;
433 static inline bool IsInternalReference(Mode mode) {
434 return mode == INTERNAL_REFERENCE;
436 static inline bool IsDebugBreakSlot(Mode mode) {
437 return mode == DEBUG_BREAK_SLOT;
439 static inline bool IsNone(Mode mode) {
440 return mode == NONE32 || mode == NONE64;
442 static inline bool IsCodeAgeSequence(Mode mode) {
443 return mode == CODE_AGE_SEQUENCE;
445 static inline int ModeMask(Mode mode) { return 1 << mode; }
447 // Returns true if the first RelocInfo has the same mode and raw data as the
449 static inline bool IsEqual(RelocInfo first, RelocInfo second) {
450 return first.rmode() == second.rmode() &&
451 (first.rmode() == RelocInfo::NONE64 ?
452 first.raw_data64() == second.raw_data64() :
453 first.data() == second.data());
457 byte* pc() const { return pc_; }
458 void set_pc(byte* pc) { pc_ = pc; }
459 Mode rmode() const { return rmode_; }
460 intptr_t data() const { return data_; }
461 double data64() const { return data64_; }
462 uint64_t raw_data64() {
463 return BitCast<uint64_t>(data64_);
465 Code* host() const { return host_; }
466 void set_host(Code* host) { host_ = host; }
468 // Apply a relocation by delta bytes
469 INLINE(void apply(intptr_t delta,
470 ICacheFlushMode icache_flush_mode =
471 FLUSH_ICACHE_IF_NEEDED));
473 // Is the pointer this relocation info refers to coded like a plain pointer
474 // or is it strange in some way (e.g. relative or patched into a series of
476 bool IsCodedSpecially();
478 // If true, the pointer this relocation info refers to is an entry in the
479 // constant pool, otherwise the pointer is embedded in the instruction stream.
480 bool IsInConstantPool();
482 // Read/modify the code target in the branch/call instruction
483 // this relocation applies to;
484 // can only be called if IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)
485 INLINE(Address target_address());
486 INLINE(void set_target_address(Address target,
487 WriteBarrierMode write_barrier_mode =
488 UPDATE_WRITE_BARRIER,
489 ICacheFlushMode icache_flush_mode =
490 FLUSH_ICACHE_IF_NEEDED));
491 INLINE(Object* target_object());
492 INLINE(Handle<Object> target_object_handle(Assembler* origin));
493 INLINE(void set_target_object(Object* target,
494 WriteBarrierMode write_barrier_mode =
495 UPDATE_WRITE_BARRIER,
496 ICacheFlushMode icache_flush_mode =
497 FLUSH_ICACHE_IF_NEEDED));
498 INLINE(Address target_runtime_entry(Assembler* origin));
499 INLINE(void set_target_runtime_entry(Address target,
500 WriteBarrierMode write_barrier_mode =
501 UPDATE_WRITE_BARRIER,
502 ICacheFlushMode icache_flush_mode =
503 FLUSH_ICACHE_IF_NEEDED));
504 INLINE(Cell* target_cell());
505 INLINE(Handle<Cell> target_cell_handle());
506 INLINE(void set_target_cell(Cell* cell,
507 WriteBarrierMode write_barrier_mode =
508 UPDATE_WRITE_BARRIER,
509 ICacheFlushMode icache_flush_mode =
510 FLUSH_ICACHE_IF_NEEDED));
511 INLINE(Handle<Object> code_age_stub_handle(Assembler* origin));
512 INLINE(Code* code_age_stub());
513 INLINE(void set_code_age_stub(Code* stub,
514 ICacheFlushMode icache_flush_mode =
515 FLUSH_ICACHE_IF_NEEDED));
517 // Returns the address of the constant pool entry where the target address
518 // is held. This should only be called if IsInConstantPool returns true.
519 INLINE(Address constant_pool_entry_address());
521 // Read the address of the word containing the target_address in an
522 // instruction stream. What this means exactly is architecture-independent.
523 // The only architecture-independent user of this function is the serializer.
524 // The serializer uses it to find out how many raw bytes of instruction to
525 // output before the next target. Architecture-independent code shouldn't
526 // dereference the pointer it gets back from this.
527 INLINE(Address target_address_address());
529 // This indicates how much space a target takes up when deserializing a code
530 // stream. For most architectures this is just the size of a pointer. For
531 // an instruction like movw/movt where the target bits are mixed into the
532 // instruction bits the size of the target will be zero, indicating that the
533 // serializer should not step forwards in memory after a target is resolved
534 // and written. In this case the target_address_address function above
535 // should return the end of the instructions to be patched, allowing the
536 // deserializer to deserialize the instructions as raw bytes and put them in
537 // place, ready to be patched with the target.
538 INLINE(int target_address_size());
540 // Read/modify the reference in the instruction this relocation
541 // applies to; can only be called if rmode_ is external_reference
542 INLINE(Address target_reference());
544 // Read/modify the address of a call instruction. This is used to relocate
545 // the break points where straight-line code is patched with a call
547 INLINE(Address call_address());
548 INLINE(void set_call_address(Address target));
549 INLINE(Object* call_object());
550 INLINE(void set_call_object(Object* target));
551 INLINE(Object** call_object_address());
553 // Wipe out a relocation to a fixed value, used for making snapshots
555 INLINE(void WipeOut());
557 template<typename StaticVisitor> inline void Visit(Heap* heap);
558 inline void Visit(Isolate* isolate, ObjectVisitor* v);
560 // Patch the code with some other code.
561 void PatchCode(byte* instructions, int instruction_count);
563 // Patch the code with a call.
564 void PatchCodeWithCall(Address target, int guard_bytes);
566 // Check whether this return sequence has been patched
567 // with a call to the debugger.
568 INLINE(bool IsPatchedReturnSequence());
570 // Check whether this debug break slot has been patched with a call to the
572 INLINE(bool IsPatchedDebugBreakSlotSequence());
575 // Check whether the given code contains relocation information that
576 // either is position-relative or movable by the garbage collector.
577 static bool RequiresRelocation(const CodeDesc& desc);
580 #ifdef ENABLE_DISASSEMBLER
582 static const char* RelocModeName(Mode rmode);
583 void Print(Isolate* isolate, OStream& os); // NOLINT
584 #endif // ENABLE_DISASSEMBLER
586 void Verify(Isolate* isolate);
589 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
590 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
591 static const int kDataMask =
592 (1 << CODE_TARGET_WITH_ID) | kPositionMask | (1 << COMMENT);
593 static const int kApplyMask; // Modes affected by apply. Depends on arch.
596 // On ARM, note that pc_ is the address of the constant pool entry
597 // to be relocated and not the address of the instruction
598 // referencing the constant pool entry (except when rmode_ ==
607 // External-reference pointers are also split across instruction-pairs
608 // on some platforms, but are accessed via indirect pointers. This location
609 // provides a place for that pointer to exist naturally. Its address
610 // is returned by RelocInfo::target_reference_address().
611 Address reconstructed_adr_ptr_;
612 friend class RelocIterator;
616 // RelocInfoWriter serializes a stream of relocation info. It writes towards
618 class RelocInfoWriter BASE_EMBEDDED {
620 RelocInfoWriter() : pos_(NULL),
624 RelocInfoWriter(byte* pos, byte* pc) : pos_(pos),
629 byte* pos() const { return pos_; }
630 byte* last_pc() const { return last_pc_; }
632 void Write(const RelocInfo* rinfo);
634 // Update the state of the stream after reloc info buffer
635 // and/or code is moved while the stream is active.
636 void Reposition(byte* pos, byte* pc) {
641 // Max size (bytes) of a written RelocInfo. Longest encoding is
642 // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
643 // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
644 // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
645 // Here we use the maximum of the two.
646 static const int kMaxSize = 16;
649 inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
650 inline void WriteTaggedPC(uint32_t pc_delta, int tag);
651 inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
652 inline void WriteExtraTaggedIntData(int data_delta, int top_tag);
653 inline void WriteExtraTaggedPoolData(int data, int pool_type);
654 inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
655 inline void WriteTaggedData(intptr_t data_delta, int tag);
656 inline void WriteExtraTag(int extra_tag, int top_tag);
662 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
666 // A RelocIterator iterates over relocation information.
669 // for (RelocIterator it(code); !it.done(); it.next()) {
670 // // do something with it.rinfo() here
673 // A mask can be specified to skip unwanted modes.
674 class RelocIterator: public Malloced {
676 // Create a new iterator positioned at
677 // the beginning of the reloc info.
678 // Relocation information with mode k is included in the
679 // iteration iff bit k of mode_mask is set.
680 explicit RelocIterator(Code* code, int mode_mask = -1);
681 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
684 bool done() const { return done_; }
687 // Return pointer valid until next next().
694 // Advance* moves the position before/after reading.
695 // *Read* reads from current byte(s) into rinfo_.
696 // *Get* just reads and returns info on current byte.
697 void Advance(int bytes = 1) { pos_ -= bytes; }
702 void AdvanceReadPC();
703 void AdvanceReadId();
704 void AdvanceReadPoolData();
705 void AdvanceReadPosition();
706 void AdvanceReadData();
707 void AdvanceReadVariableLengthPCJump();
708 int GetLocatableTypeTag();
710 void ReadTaggedPosition();
712 // If the given mode is wanted, set it in rinfo_ and return true.
713 // Else return false. Used for efficiently skipping unwanted modes.
714 bool SetMode(RelocInfo::Mode mode) {
715 return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
720 byte* code_age_sequence_;
726 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
730 //------------------------------------------------------------------------------
733 //----------------------------------------------------------------------------
735 class SCTableReference;
739 // An ExternalReference represents a C++ address used in the generated
740 // code. All references to C++ functions and variables must be encapsulated in
741 // an ExternalReference instance. This is done in order to track the origin of
742 // all external references in the code so that they can be bound to the correct
743 // addresses when deserializing a heap.
744 class ExternalReference BASE_EMBEDDED {
746 // Used in the simulator to support different native api calls.
749 // Object* f(v8::internal::Arguments).
750 BUILTIN_CALL, // default
752 // Builtin that takes float arguments and returns an int.
753 // int f(double, double).
754 BUILTIN_COMPARE_CALL,
756 // Builtin call that returns floating point.
757 // double f(double, double).
760 // Builtin call that returns floating point.
764 // Builtin call that returns floating point.
765 // double f(double, int).
768 // Direct call to API function callback.
769 // void f(v8::FunctionCallbackInfo&)
772 // Call to function callback via InvokeFunctionCallback.
773 // void f(v8::FunctionCallbackInfo&, v8::FunctionCallback)
776 // Direct call to accessor getter callback.
777 // void f(Local<String> property, PropertyCallbackInfo& info)
780 // Call to accessor getter callback via InvokeAccessorGetterCallback.
781 // void f(Local<String> property, PropertyCallbackInfo& info,
782 // AccessorGetterCallback callback)
783 PROFILING_GETTER_CALL
787 static void InitializeMathExpData();
788 static void TearDownMathExpData();
790 typedef void* ExternalReferenceRedirector(void* original, Type type);
792 ExternalReference() : address_(NULL) {}
794 ExternalReference(Builtins::CFunctionId id, Isolate* isolate);
796 ExternalReference(ApiFunction* ptr, Type type, Isolate* isolate);
798 ExternalReference(Builtins::Name name, Isolate* isolate);
800 ExternalReference(Runtime::FunctionId id, Isolate* isolate);
802 ExternalReference(const Runtime::Function* f, Isolate* isolate);
804 ExternalReference(const IC_Utility& ic_utility, Isolate* isolate);
806 explicit ExternalReference(StatsCounter* counter);
808 ExternalReference(Isolate::AddressId id, Isolate* isolate);
810 explicit ExternalReference(const SCTableReference& table_ref);
812 // Isolate as an external reference.
813 static ExternalReference isolate_address(Isolate* isolate);
815 // One-of-a-kind references. These references are not part of a general
816 // pattern. This means that they have to be added to the
817 // ExternalReferenceTable in serialize.cc manually.
819 static ExternalReference incremental_marking_record_write_function(
821 static ExternalReference store_buffer_overflow_function(
823 static ExternalReference flush_icache_function(Isolate* isolate);
824 static ExternalReference delete_handle_scope_extensions(Isolate* isolate);
826 static ExternalReference get_date_field_function(Isolate* isolate);
827 static ExternalReference date_cache_stamp(Isolate* isolate);
829 static ExternalReference get_make_code_young_function(Isolate* isolate);
830 static ExternalReference get_mark_code_as_executed_function(Isolate* isolate);
832 // Deoptimization support.
833 static ExternalReference new_deoptimizer_function(Isolate* isolate);
834 static ExternalReference compute_output_frames_function(Isolate* isolate);
837 static ExternalReference log_enter_external_function(Isolate* isolate);
838 static ExternalReference log_leave_external_function(Isolate* isolate);
840 // Static data in the keyed lookup cache.
841 static ExternalReference keyed_lookup_cache_keys(Isolate* isolate);
842 static ExternalReference keyed_lookup_cache_field_offsets(Isolate* isolate);
844 // Static variable Heap::roots_array_start()
845 static ExternalReference roots_array_start(Isolate* isolate);
847 // Static variable Heap::allocation_sites_list_address()
848 static ExternalReference allocation_sites_list_address(Isolate* isolate);
850 // Static variable StackGuard::address_of_jslimit()
851 static ExternalReference address_of_stack_limit(Isolate* isolate);
853 // Static variable StackGuard::address_of_real_jslimit()
854 static ExternalReference address_of_real_stack_limit(Isolate* isolate);
856 // Static variable RegExpStack::limit_address()
857 static ExternalReference address_of_regexp_stack_limit(Isolate* isolate);
859 // Static variables for RegExp.
860 static ExternalReference address_of_static_offsets_vector(Isolate* isolate);
861 static ExternalReference address_of_regexp_stack_memory_address(
863 static ExternalReference address_of_regexp_stack_memory_size(
866 // Static variable Heap::NewSpaceStart()
867 static ExternalReference new_space_start(Isolate* isolate);
868 static ExternalReference new_space_mask(Isolate* isolate);
871 static ExternalReference store_buffer_top(Isolate* isolate);
873 // Used for fast allocation in generated code.
874 static ExternalReference new_space_allocation_top_address(Isolate* isolate);
875 static ExternalReference new_space_allocation_limit_address(Isolate* isolate);
876 static ExternalReference old_pointer_space_allocation_top_address(
878 static ExternalReference old_pointer_space_allocation_limit_address(
880 static ExternalReference old_data_space_allocation_top_address(
882 static ExternalReference old_data_space_allocation_limit_address(
885 static ExternalReference mod_two_doubles_operation(Isolate* isolate);
886 static ExternalReference power_double_double_function(Isolate* isolate);
887 static ExternalReference power_double_int_function(Isolate* isolate);
889 static ExternalReference handle_scope_next_address(Isolate* isolate);
890 static ExternalReference handle_scope_limit_address(Isolate* isolate);
891 static ExternalReference handle_scope_level_address(Isolate* isolate);
893 static ExternalReference scheduled_exception_address(Isolate* isolate);
894 static ExternalReference address_of_pending_message_obj(Isolate* isolate);
895 static ExternalReference address_of_has_pending_message(Isolate* isolate);
896 static ExternalReference address_of_pending_message_script(Isolate* isolate);
898 // Static variables containing common double constants.
899 static ExternalReference address_of_min_int();
900 static ExternalReference address_of_one_half();
901 static ExternalReference address_of_minus_one_half();
902 static ExternalReference address_of_negative_infinity();
903 static ExternalReference address_of_canonical_non_hole_nan();
904 static ExternalReference address_of_the_hole_nan();
905 static ExternalReference address_of_uint32_bias();
907 static ExternalReference math_log_double_function(Isolate* isolate);
909 static ExternalReference math_exp_constants(int constant_index);
910 static ExternalReference math_exp_log_table();
912 static ExternalReference page_flags(Page* page);
914 static ExternalReference ForDeoptEntry(Address entry);
916 static ExternalReference cpu_features();
918 static ExternalReference debug_is_active_address(Isolate* isolate);
919 static ExternalReference debug_after_break_target_address(Isolate* isolate);
920 static ExternalReference debug_restarter_frame_function_pointer_address(
923 static ExternalReference is_profiling_address(Isolate* isolate);
924 static ExternalReference invoke_function_callback(Isolate* isolate);
925 static ExternalReference invoke_accessor_getter_callback(Isolate* isolate);
927 Address address() const { return reinterpret_cast<Address>(address_); }
929 // Function Debug::Break()
930 static ExternalReference debug_break(Isolate* isolate);
932 // Used to check if single stepping is enabled in generated code.
933 static ExternalReference debug_step_in_fp_address(Isolate* isolate);
935 #ifndef V8_INTERPRETED_REGEXP
936 // C functions called from RegExp generated code.
938 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
939 static ExternalReference re_case_insensitive_compare_uc16(Isolate* isolate);
941 // Function RegExpMacroAssembler*::CheckStackGuardState()
942 static ExternalReference re_check_stack_guard_state(Isolate* isolate);
944 // Function NativeRegExpMacroAssembler::GrowStack()
945 static ExternalReference re_grow_stack(Isolate* isolate);
947 // byte NativeRegExpMacroAssembler::word_character_bitmap
948 static ExternalReference re_word_character_map();
952 // This lets you register a function that rewrites all external references.
953 // Used by the ARM simulator to catch calls to external references.
954 static void set_redirector(Isolate* isolate,
955 ExternalReferenceRedirector* redirector) {
956 // We can't stack them.
957 DCHECK(isolate->external_reference_redirector() == NULL);
958 isolate->set_external_reference_redirector(
959 reinterpret_cast<ExternalReferenceRedirectorPointer*>(redirector));
962 static ExternalReference stress_deopt_count(Isolate* isolate);
964 bool operator==(const ExternalReference& other) const {
965 return address_ == other.address_;
968 bool operator!=(const ExternalReference& other) const {
969 return !(*this == other);
973 explicit ExternalReference(void* address)
974 : address_(address) {}
976 static void* Redirect(Isolate* isolate,
978 Type type = ExternalReference::BUILTIN_CALL) {
979 ExternalReferenceRedirector* redirector =
980 reinterpret_cast<ExternalReferenceRedirector*>(
981 isolate->external_reference_redirector());
982 void* address = reinterpret_cast<void*>(address_arg);
983 void* answer = (redirector == NULL) ?
985 (*redirector)(address, type);
993 // -----------------------------------------------------------------------------
994 // Position recording support
996 struct PositionState {
997 PositionState() : current_position(RelocInfo::kNoPosition),
998 written_position(RelocInfo::kNoPosition),
999 current_statement_position(RelocInfo::kNoPosition),
1000 written_statement_position(RelocInfo::kNoPosition) {}
1002 int current_position;
1003 int written_position;
1005 int current_statement_position;
1006 int written_statement_position;
1010 class PositionsRecorder BASE_EMBEDDED {
1012 explicit PositionsRecorder(Assembler* assembler)
1013 : assembler_(assembler) {
1014 jit_handler_data_ = NULL;
1017 void AttachJITHandlerData(void* user_data) {
1018 jit_handler_data_ = user_data;
1021 void* DetachJITHandlerData() {
1022 void* old_data = jit_handler_data_;
1023 jit_handler_data_ = NULL;
1026 // Set current position to pos.
1027 void RecordPosition(int pos);
1029 // Set current statement position to pos.
1030 void RecordStatementPosition(int pos);
1032 // Write recorded positions to relocation information.
1033 bool WriteRecordedPositions();
1035 int current_position() const { return state_.current_position; }
1037 int current_statement_position() const {
1038 return state_.current_statement_position;
1042 Assembler* assembler_;
1043 PositionState state_;
1045 // Currently jit_handler_data_ is used to store JITHandler-specific data
1046 // over the lifetime of a PositionsRecorder
1047 void* jit_handler_data_;
1048 friend class PreservePositionScope;
1050 DISALLOW_COPY_AND_ASSIGN(PositionsRecorder);
1054 class PreservePositionScope BASE_EMBEDDED {
1056 explicit PreservePositionScope(PositionsRecorder* positions_recorder)
1057 : positions_recorder_(positions_recorder),
1058 saved_state_(positions_recorder->state_) {}
1060 ~PreservePositionScope() {
1061 positions_recorder_->state_ = saved_state_;
1065 PositionsRecorder* positions_recorder_;
1066 const PositionState saved_state_;
1068 DISALLOW_COPY_AND_ASSIGN(PreservePositionScope);
1072 // -----------------------------------------------------------------------------
1073 // Utility functions
1075 inline int NumberOfBitsSet(uint32_t x) {
1076 unsigned int num_bits_set;
1077 for (num_bits_set = 0; x; x >>= 1) {
1078 num_bits_set += x & 1;
1080 return num_bits_set;
1083 bool EvalComparison(Token::Value op, double op1, double op2);
1085 // Computes pow(x, y) with the special cases in the spec for Math.pow.
1086 double power_helper(double x, double y);
1087 double power_double_int(double x, int y);
1088 double power_double_double(double x, double y);
1090 // Helper class for generating code or data associated with the code
1091 // right after a call instruction. As an example this can be used to
1092 // generate safepoint data after calls for crankshaft.
1096 virtual ~CallWrapper() { }
1097 // Called just before emitting a call. Argument is the size of the generated
1099 virtual void BeforeCall(int call_size) const = 0;
1100 // Called just after emitting a call, i.e., at the return site for the call.
1101 virtual void AfterCall() const = 0;
1104 class NullCallWrapper : public CallWrapper {
1106 NullCallWrapper() { }
1107 virtual ~NullCallWrapper() { }
1108 virtual void BeforeCall(int call_size) const { }
1109 virtual void AfterCall() const { }
1113 // The multiplier and shift for signed division via multiplication, see Warren's
1114 // "Hacker's Delight", chapter 10.
1115 class MultiplierAndShift {
1117 explicit MultiplierAndShift(int32_t d);
1118 int32_t multiplier() const { return multiplier_; }
1119 int32_t shift() const { return shift_; }
1122 int32_t multiplier_;
1127 } } // namespace v8::internal
1129 #endif // V8_ASSEMBLER_H_