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 "allocation.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 predictable_code_size() const { return predictable_code_size_; }
69 void set_predictable_code_size(bool value) { predictable_code_size_ = value; }
71 uint64_t enabled_cpu_features() const { return enabled_cpu_features_; }
72 void set_enabled_cpu_features(uint64_t features) {
73 enabled_cpu_features_ = features;
75 bool IsEnabled(CpuFeature f) {
76 return (enabled_cpu_features_ & (static_cast<uint64_t>(1) << f)) != 0;
79 // Overwrite a host NaN with a quiet target NaN. Used by mksnapshot for
80 // cross-snapshotting.
81 static void QuietNaN(HeapObject* nan) { }
83 int pc_offset() const { return static_cast<int>(pc_ - buffer_); }
85 // This function is called when code generation is aborted, so that
86 // the assembler could clean up internal data structures.
87 virtual void AbortedCodeGeneration() { }
89 static const int kMinimalBufferSize = 4*KB;
92 // The buffer into which code and relocation info are generated. It could
93 // either be owned by the assembler or be provided externally.
98 // The program counter, which points into the buffer above and moves forward.
104 uint64_t enabled_cpu_features_;
105 bool emit_debug_code_;
106 bool predictable_code_size_;
110 // Avoids using instructions that vary in size in unpredictable ways between the
111 // snapshot and the running VM.
112 class PredictableCodeSizeScope {
114 PredictableCodeSizeScope(AssemblerBase* assembler, int expected_size);
115 ~PredictableCodeSizeScope();
118 AssemblerBase* assembler_;
125 // Enable a specified feature within a scope.
126 class CpuFeatureScope BASE_EMBEDDED {
129 CpuFeatureScope(AssemblerBase* assembler, CpuFeature f);
133 AssemblerBase* assembler_;
134 uint64_t old_enabled_;
136 CpuFeatureScope(AssemblerBase* assembler, CpuFeature f) {}
141 // Enable a unsupported feature within a scope for cross-compiling for a
143 class PlatformFeatureScope BASE_EMBEDDED {
145 explicit PlatformFeatureScope(CpuFeature f);
146 ~PlatformFeatureScope();
149 uint64_t old_cross_compile_;
153 // -----------------------------------------------------------------------------
154 // Labels represent pc locations; they are typically jump or call targets.
155 // After declaration, a label can be freely used to denote known or (yet)
156 // unknown pc location. Assembler::bind() is used to bind a label to the
157 // current pc. A label can be bound only once.
159 class Label BASE_EMBEDDED {
171 ASSERT(!is_linked());
172 ASSERT(!is_near_linked());
175 INLINE(void Unuse()) { pos_ = 0; }
176 INLINE(void UnuseNear()) { near_link_pos_ = 0; }
178 INLINE(bool is_bound() const) { return pos_ < 0; }
179 INLINE(bool is_unused() const) { return pos_ == 0 && near_link_pos_ == 0; }
180 INLINE(bool is_linked() const) { return pos_ > 0; }
181 INLINE(bool is_near_linked() const) { return near_link_pos_ > 0; }
183 // Returns the position of bound or linked labels. Cannot be used
184 // for unused labels.
186 int near_link_pos() const { return near_link_pos_ - 1; }
189 // pos_ encodes both the binding state (via its sign)
190 // and the binding position (via its value) of a label.
192 // pos_ < 0 bound label, pos() returns the jump target position
193 // pos_ == 0 unused label
194 // pos_ > 0 linked label, pos() returns the last reference position
197 // Behaves like |pos_| in the "> 0" case, but for near jumps to this label.
200 void bind_to(int pos) {
204 void link_to(int pos, Distance distance = kFar) {
205 if (distance == kNear) {
206 near_link_pos_ = pos + 1;
207 ASSERT(is_near_linked());
214 friend class Assembler;
215 friend class Displacement;
216 friend class RegExpMacroAssemblerIrregexp;
218 #if V8_TARGET_ARCH_ARM64
219 // On ARM64, the Assembler keeps track of pointers to Labels to resolve
220 // branches to distant targets. Copying labels would confuse the Assembler.
221 DISALLOW_COPY_AND_ASSIGN(Label); // NOLINT
226 enum SaveFPRegsMode { kDontSaveFPRegs, kSaveFPRegs };
229 // -----------------------------------------------------------------------------
230 // Relocation information
233 // Relocation information consists of the address (pc) of the datum
234 // to which the relocation information applies, the relocation mode
235 // (rmode), and an optional data field. The relocation mode may be
236 // "descriptive" and not indicate a need for relocation, but simply
237 // describe a property of the datum. Such rmodes are useful for GC
238 // and nice disassembly output.
240 class RelocInfo BASE_EMBEDDED {
242 // The constant kNoPosition is used with the collecting of source positions
243 // in the relocation information. Two types of source positions are collected
244 // "position" (RelocMode position) and "statement position" (RelocMode
245 // statement_position). The "position" is collected at places in the source
246 // code which are of interest when making stack traces to pin-point the source
247 // location of a stack frame as close as possible. The "statement position" is
248 // collected at the beginning at each statement, and is used to indicate
249 // possible break locations. kNoPosition is used to indicate an
250 // invalid/uninitialized position value.
251 static const int kNoPosition = -1;
253 // This string is used to add padding comments to the reloc info in cases
254 // where we are not sure to have enough space for patching in during
255 // lazy deoptimization. This is the case if we have indirect calls for which
256 // we do not normally record relocation info.
257 static const char* const kFillerCommentString;
259 // The minimum size of a comment is equal to three bytes for the extra tagged
260 // pc + the tag for the data, and kPointerSize for the actual pointer to the
262 static const int kMinRelocCommentSize = 3 + kPointerSize;
264 // The maximum size for a call instruction including pc-jump.
265 static const int kMaxCallSize = 6;
267 // The maximum pc delta that will use the short encoding.
268 static const int kMaxSmallPCDelta;
271 // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
272 CODE_TARGET, // Code target which is not any of the above.
274 CONSTRUCT_CALL, // code target that is a call to a JavaScript constructor.
275 DEBUG_BREAK, // Code target for the debugger statement.
279 // Everything after runtime_entry (inclusive) is not GC'ed.
281 JS_RETURN, // Marks start of the ExitJSFrame code.
283 POSITION, // See comment for kNoPosition above.
284 STATEMENT_POSITION, // See comment for kNoPosition above.
285 DEBUG_BREAK_SLOT, // Additional code inserted for debug break slot.
286 EXTERNAL_REFERENCE, // The address of an external C++ function.
287 INTERNAL_REFERENCE, // An address inside the same function.
289 // Marks constant and veneer pools. Only used on ARM and ARM64.
290 // They use a custom noncompact encoding.
294 // add more as needed
296 NUMBER_OF_MODES, // There are at most 15 modes with noncompact encoding.
297 NONE32, // never recorded 32-bit value
298 NONE64, // never recorded 64-bit value
299 CODE_AGE_SEQUENCE, // Not stored in RelocInfo array, used explictly by
301 FIRST_REAL_RELOC_MODE = CODE_TARGET,
302 LAST_REAL_RELOC_MODE = VENEER_POOL,
303 FIRST_PSEUDO_RELOC_MODE = CODE_AGE_SEQUENCE,
304 LAST_PSEUDO_RELOC_MODE = CODE_AGE_SEQUENCE,
305 LAST_CODE_ENUM = DEBUG_BREAK,
306 LAST_GCED_ENUM = CELL,
307 // Modes <= LAST_COMPACT_ENUM are guaranteed to have compact encoding.
308 LAST_COMPACT_ENUM = CODE_TARGET_WITH_ID,
309 LAST_STANDARD_NONCOMPACT_ENUM = INTERNAL_REFERENCE
315 RelocInfo(byte* pc, Mode rmode, intptr_t data, Code* host)
316 : pc_(pc), rmode_(rmode), data_(data), host_(host) {
318 RelocInfo(byte* pc, double data64)
319 : pc_(pc), rmode_(NONE64), data64_(data64), host_(NULL) {
322 static inline bool IsRealRelocMode(Mode mode) {
323 return mode >= FIRST_REAL_RELOC_MODE &&
324 mode <= LAST_REAL_RELOC_MODE;
326 static inline bool IsPseudoRelocMode(Mode mode) {
327 ASSERT(!IsRealRelocMode(mode));
328 return mode >= FIRST_PSEUDO_RELOC_MODE &&
329 mode <= LAST_PSEUDO_RELOC_MODE;
331 static inline bool IsConstructCall(Mode mode) {
332 return mode == CONSTRUCT_CALL;
334 static inline bool IsCodeTarget(Mode mode) {
335 return mode <= LAST_CODE_ENUM;
337 static inline bool IsEmbeddedObject(Mode mode) {
338 return mode == EMBEDDED_OBJECT;
340 static inline bool IsRuntimeEntry(Mode mode) {
341 return mode == RUNTIME_ENTRY;
343 // Is the relocation mode affected by GC?
344 static inline bool IsGCRelocMode(Mode mode) {
345 return mode <= LAST_GCED_ENUM;
347 static inline bool IsJSReturn(Mode mode) {
348 return mode == JS_RETURN;
350 static inline bool IsComment(Mode mode) {
351 return mode == COMMENT;
353 static inline bool IsConstPool(Mode mode) {
354 return mode == CONST_POOL;
356 static inline bool IsVeneerPool(Mode mode) {
357 return mode == VENEER_POOL;
359 static inline bool IsPosition(Mode mode) {
360 return mode == POSITION || mode == STATEMENT_POSITION;
362 static inline bool IsStatementPosition(Mode mode) {
363 return mode == STATEMENT_POSITION;
365 static inline bool IsExternalReference(Mode mode) {
366 return mode == EXTERNAL_REFERENCE;
368 static inline bool IsInternalReference(Mode mode) {
369 return mode == INTERNAL_REFERENCE;
371 static inline bool IsDebugBreakSlot(Mode mode) {
372 return mode == DEBUG_BREAK_SLOT;
374 static inline bool IsNone(Mode mode) {
375 return mode == NONE32 || mode == NONE64;
377 static inline bool IsCodeAgeSequence(Mode mode) {
378 return mode == CODE_AGE_SEQUENCE;
380 static inline int ModeMask(Mode mode) { return 1 << mode; }
382 // Returns true if the first RelocInfo has the same mode and raw data as the
384 static inline bool IsEqual(RelocInfo first, RelocInfo second) {
385 return first.rmode() == second.rmode() &&
386 (first.rmode() == RelocInfo::NONE64 ?
387 first.raw_data64() == second.raw_data64() :
388 first.data() == second.data());
392 byte* pc() const { return pc_; }
393 void set_pc(byte* pc) { pc_ = pc; }
394 Mode rmode() const { return rmode_; }
395 intptr_t data() const { return data_; }
396 double data64() const { return data64_; }
397 uint64_t raw_data64() {
398 return BitCast<uint64_t>(data64_);
400 Code* host() const { return host_; }
401 void set_host(Code* host) { host_ = host; }
403 // Apply a relocation by delta bytes
404 INLINE(void apply(intptr_t delta));
406 // Is the pointer this relocation info refers to coded like a plain pointer
407 // or is it strange in some way (e.g. relative or patched into a series of
409 bool IsCodedSpecially();
411 // If true, the pointer this relocation info refers to is an entry in the
412 // constant pool, otherwise the pointer is embedded in the instruction stream.
413 bool IsInConstantPool();
415 // Read/modify the code target in the branch/call instruction
416 // this relocation applies to;
417 // can only be called if IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_)
418 INLINE(Address target_address());
419 INLINE(void set_target_address(Address target,
420 WriteBarrierMode mode = UPDATE_WRITE_BARRIER));
421 INLINE(Object* target_object());
422 INLINE(Handle<Object> target_object_handle(Assembler* origin));
423 INLINE(void set_target_object(Object* target,
424 WriteBarrierMode mode = UPDATE_WRITE_BARRIER));
425 INLINE(Address target_runtime_entry(Assembler* origin));
426 INLINE(void set_target_runtime_entry(Address target,
427 WriteBarrierMode mode =
428 UPDATE_WRITE_BARRIER));
429 INLINE(Cell* target_cell());
430 INLINE(Handle<Cell> target_cell_handle());
431 INLINE(void set_target_cell(Cell* cell,
432 WriteBarrierMode mode = UPDATE_WRITE_BARRIER));
433 INLINE(Handle<Object> code_age_stub_handle(Assembler* origin));
434 INLINE(Code* code_age_stub());
435 INLINE(void set_code_age_stub(Code* stub));
437 // Returns the address of the constant pool entry where the target address
438 // is held. This should only be called if IsInConstantPool returns true.
439 INLINE(Address constant_pool_entry_address());
441 // Read the address of the word containing the target_address in an
442 // instruction stream. What this means exactly is architecture-independent.
443 // The only architecture-independent user of this function is the serializer.
444 // The serializer uses it to find out how many raw bytes of instruction to
445 // output before the next target. Architecture-independent code shouldn't
446 // dereference the pointer it gets back from this.
447 INLINE(Address target_address_address());
449 // This indicates how much space a target takes up when deserializing a code
450 // stream. For most architectures this is just the size of a pointer. For
451 // an instruction like movw/movt where the target bits are mixed into the
452 // instruction bits the size of the target will be zero, indicating that the
453 // serializer should not step forwards in memory after a target is resolved
454 // and written. In this case the target_address_address function above
455 // should return the end of the instructions to be patched, allowing the
456 // deserializer to deserialize the instructions as raw bytes and put them in
457 // place, ready to be patched with the target.
458 INLINE(int target_address_size());
460 // Read/modify the reference in the instruction this relocation
461 // applies to; can only be called if rmode_ is external_reference
462 INLINE(Address target_reference());
464 // Read/modify the address of a call instruction. This is used to relocate
465 // the break points where straight-line code is patched with a call
467 INLINE(Address call_address());
468 INLINE(void set_call_address(Address target));
469 INLINE(Object* call_object());
470 INLINE(void set_call_object(Object* target));
471 INLINE(Object** call_object_address());
473 // Wipe out a relocation to a fixed value, used for making snapshots
475 INLINE(void WipeOut());
477 template<typename StaticVisitor> inline void Visit(Heap* heap);
478 inline void Visit(Isolate* isolate, ObjectVisitor* v);
480 // Patch the code with some other code.
481 void PatchCode(byte* instructions, int instruction_count);
483 // Patch the code with a call.
484 void PatchCodeWithCall(Address target, int guard_bytes);
486 // Check whether this return sequence has been patched
487 // with a call to the debugger.
488 INLINE(bool IsPatchedReturnSequence());
490 // Check whether this debug break slot has been patched with a call to the
492 INLINE(bool IsPatchedDebugBreakSlotSequence());
495 // Check whether the given code contains relocation information that
496 // either is position-relative or movable by the garbage collector.
497 static bool RequiresRelocation(const CodeDesc& desc);
500 #ifdef ENABLE_DISASSEMBLER
502 static const char* RelocModeName(Mode rmode);
503 void Print(Isolate* isolate, FILE* out);
504 #endif // ENABLE_DISASSEMBLER
509 static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
510 static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
511 static const int kDataMask =
512 (1 << CODE_TARGET_WITH_ID) | kPositionMask | (1 << COMMENT);
513 static const int kApplyMask; // Modes affected by apply. Depends on arch.
516 // On ARM, note that pc_ is the address of the constant pool entry
517 // to be relocated and not the address of the instruction
518 // referencing the constant pool entry (except when rmode_ ==
527 // External-reference pointers are also split across instruction-pairs
528 // on some platforms, but are accessed via indirect pointers. This location
529 // provides a place for that pointer to exist naturally. Its address
530 // is returned by RelocInfo::target_reference_address().
531 Address reconstructed_adr_ptr_;
532 friend class RelocIterator;
536 // RelocInfoWriter serializes a stream of relocation info. It writes towards
538 class RelocInfoWriter BASE_EMBEDDED {
540 RelocInfoWriter() : pos_(NULL),
544 RelocInfoWriter(byte* pos, byte* pc) : pos_(pos),
549 byte* pos() const { return pos_; }
550 byte* last_pc() const { return last_pc_; }
552 void Write(const RelocInfo* rinfo);
554 // Update the state of the stream after reloc info buffer
555 // and/or code is moved while the stream is active.
556 void Reposition(byte* pos, byte* pc) {
561 // Max size (bytes) of a written RelocInfo. Longest encoding is
562 // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
563 // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
564 // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
565 // Here we use the maximum of the two.
566 static const int kMaxSize = 16;
569 inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
570 inline void WriteTaggedPC(uint32_t pc_delta, int tag);
571 inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
572 inline void WriteExtraTaggedIntData(int data_delta, int top_tag);
573 inline void WriteExtraTaggedPoolData(int data, int pool_type);
574 inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
575 inline void WriteTaggedData(intptr_t data_delta, int tag);
576 inline void WriteExtraTag(int extra_tag, int top_tag);
582 DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
586 // A RelocIterator iterates over relocation information.
589 // for (RelocIterator it(code); !it.done(); it.next()) {
590 // // do something with it.rinfo() here
593 // A mask can be specified to skip unwanted modes.
594 class RelocIterator: public Malloced {
596 // Create a new iterator positioned at
597 // the beginning of the reloc info.
598 // Relocation information with mode k is included in the
599 // iteration iff bit k of mode_mask is set.
600 explicit RelocIterator(Code* code, int mode_mask = -1);
601 explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
604 bool done() const { return done_; }
607 // Return pointer valid until next next().
614 // Advance* moves the position before/after reading.
615 // *Read* reads from current byte(s) into rinfo_.
616 // *Get* just reads and returns info on current byte.
617 void Advance(int bytes = 1) { pos_ -= bytes; }
622 void AdvanceReadPC();
623 void AdvanceReadId();
624 void AdvanceReadPoolData();
625 void AdvanceReadPosition();
626 void AdvanceReadData();
627 void AdvanceReadVariableLengthPCJump();
628 int GetLocatableTypeTag();
630 void ReadTaggedPosition();
632 // If the given mode is wanted, set it in rinfo_ and return true.
633 // Else return false. Used for efficiently skipping unwanted modes.
634 bool SetMode(RelocInfo::Mode mode) {
635 return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
640 byte* code_age_sequence_;
646 DISALLOW_COPY_AND_ASSIGN(RelocIterator);
650 //------------------------------------------------------------------------------
653 //----------------------------------------------------------------------------
655 class SCTableReference;
656 #ifdef ENABLE_DEBUGGER_SUPPORT
661 // An ExternalReference represents a C++ address used in the generated
662 // code. All references to C++ functions and variables must be encapsulated in
663 // an ExternalReference instance. This is done in order to track the origin of
664 // all external references in the code so that they can be bound to the correct
665 // addresses when deserializing a heap.
666 class ExternalReference BASE_EMBEDDED {
668 // Used in the simulator to support different native api calls.
671 // MaybeObject* f(v8::internal::Arguments).
672 BUILTIN_CALL, // default
674 // Builtin that takes float arguments and returns an int.
675 // int f(double, double).
676 BUILTIN_COMPARE_CALL,
678 // Builtin call that returns floating point.
679 // double f(double, double).
682 // Builtin call that returns floating point.
686 // Builtin call that returns floating point.
687 // double f(double, int).
690 // Direct call to API function callback.
691 // void f(v8::FunctionCallbackInfo&)
694 // Call to function callback via InvokeFunctionCallback.
695 // void f(v8::FunctionCallbackInfo&, v8::FunctionCallback)
698 // Direct call to accessor getter callback.
699 // void f(Local<String> property, PropertyCallbackInfo& info)
702 // Call to accessor getter callback via InvokeAccessorGetterCallback.
703 // void f(Local<String> property, PropertyCallbackInfo& info,
704 // AccessorGetterCallback callback)
705 PROFILING_GETTER_CALL
709 static void InitializeMathExpData();
710 static void TearDownMathExpData();
712 typedef void* ExternalReferenceRedirector(void* original, Type type);
714 ExternalReference() : address_(NULL) {}
716 ExternalReference(Builtins::CFunctionId id, Isolate* isolate);
718 ExternalReference(ApiFunction* ptr, Type type, Isolate* isolate);
720 ExternalReference(Builtins::Name name, Isolate* isolate);
722 ExternalReference(Runtime::FunctionId id, Isolate* isolate);
724 ExternalReference(const Runtime::Function* f, Isolate* isolate);
726 ExternalReference(const IC_Utility& ic_utility, Isolate* isolate);
728 #ifdef ENABLE_DEBUGGER_SUPPORT
729 ExternalReference(const Debug_Address& debug_address, Isolate* isolate);
732 explicit ExternalReference(StatsCounter* counter);
734 ExternalReference(Isolate::AddressId id, Isolate* isolate);
736 explicit ExternalReference(const SCTableReference& table_ref);
738 // Isolate as an external reference.
739 static ExternalReference isolate_address(Isolate* isolate);
741 // One-of-a-kind references. These references are not part of a general
742 // pattern. This means that they have to be added to the
743 // ExternalReferenceTable in serialize.cc manually.
745 static ExternalReference incremental_marking_record_write_function(
747 static ExternalReference store_buffer_overflow_function(
749 static ExternalReference flush_icache_function(Isolate* isolate);
750 static ExternalReference perform_gc_function(Isolate* isolate);
751 static ExternalReference delete_handle_scope_extensions(Isolate* isolate);
753 static ExternalReference get_date_field_function(Isolate* isolate);
754 static ExternalReference date_cache_stamp(Isolate* isolate);
756 static ExternalReference get_make_code_young_function(Isolate* isolate);
757 static ExternalReference get_mark_code_as_executed_function(Isolate* isolate);
759 // Deoptimization support.
760 static ExternalReference new_deoptimizer_function(Isolate* isolate);
761 static ExternalReference compute_output_frames_function(Isolate* isolate);
764 static ExternalReference log_enter_external_function(Isolate* isolate);
765 static ExternalReference log_leave_external_function(Isolate* isolate);
767 // Static data in the keyed lookup cache.
768 static ExternalReference keyed_lookup_cache_keys(Isolate* isolate);
769 static ExternalReference keyed_lookup_cache_field_offsets(Isolate* isolate);
771 // Static variable Heap::roots_array_start()
772 static ExternalReference roots_array_start(Isolate* isolate);
774 // Static variable Heap::allocation_sites_list_address()
775 static ExternalReference allocation_sites_list_address(Isolate* isolate);
777 // Static variable StackGuard::address_of_jslimit()
778 static ExternalReference address_of_stack_limit(Isolate* isolate);
780 // Static variable StackGuard::address_of_real_jslimit()
781 static ExternalReference address_of_real_stack_limit(Isolate* isolate);
783 // Static variable RegExpStack::limit_address()
784 static ExternalReference address_of_regexp_stack_limit(Isolate* isolate);
786 // Static variables for RegExp.
787 static ExternalReference address_of_static_offsets_vector(Isolate* isolate);
788 static ExternalReference address_of_regexp_stack_memory_address(
790 static ExternalReference address_of_regexp_stack_memory_size(
793 // Static variable Heap::NewSpaceStart()
794 static ExternalReference new_space_start(Isolate* isolate);
795 static ExternalReference new_space_mask(Isolate* isolate);
796 static ExternalReference heap_always_allocate_scope_depth(Isolate* isolate);
797 static ExternalReference new_space_mark_bits(Isolate* isolate);
800 static ExternalReference store_buffer_top(Isolate* isolate);
802 // Used for fast allocation in generated code.
803 static ExternalReference new_space_allocation_top_address(Isolate* isolate);
804 static ExternalReference new_space_allocation_limit_address(Isolate* isolate);
805 static ExternalReference old_pointer_space_allocation_top_address(
807 static ExternalReference old_pointer_space_allocation_limit_address(
809 static ExternalReference old_data_space_allocation_top_address(
811 static ExternalReference old_data_space_allocation_limit_address(
813 static ExternalReference new_space_high_promotion_mode_active_address(
816 static ExternalReference mod_two_doubles_operation(Isolate* isolate);
817 static ExternalReference power_double_double_function(Isolate* isolate);
818 static ExternalReference power_double_int_function(Isolate* isolate);
820 static ExternalReference handle_scope_next_address(Isolate* isolate);
821 static ExternalReference handle_scope_limit_address(Isolate* isolate);
822 static ExternalReference handle_scope_level_address(Isolate* isolate);
824 static ExternalReference scheduled_exception_address(Isolate* isolate);
825 static ExternalReference address_of_pending_message_obj(Isolate* isolate);
826 static ExternalReference address_of_has_pending_message(Isolate* isolate);
827 static ExternalReference address_of_pending_message_script(Isolate* isolate);
829 // Static variables containing common double constants.
830 static ExternalReference address_of_min_int();
831 static ExternalReference address_of_one_half();
832 static ExternalReference address_of_minus_one_half();
833 static ExternalReference address_of_minus_zero();
834 static ExternalReference address_of_zero();
835 static ExternalReference address_of_uint8_max_value();
836 static ExternalReference address_of_negative_infinity();
837 static ExternalReference address_of_canonical_non_hole_nan();
838 static ExternalReference address_of_the_hole_nan();
839 static ExternalReference address_of_uint32_bias();
841 static ExternalReference math_log_double_function(Isolate* isolate);
843 static ExternalReference math_exp_constants(int constant_index);
844 static ExternalReference math_exp_log_table();
846 static ExternalReference page_flags(Page* page);
848 static ExternalReference ForDeoptEntry(Address entry);
850 static ExternalReference cpu_features();
852 Address address() const { return reinterpret_cast<Address>(address_); }
854 #ifdef ENABLE_DEBUGGER_SUPPORT
855 // Function Debug::Break()
856 static ExternalReference debug_break(Isolate* isolate);
858 // Used to check if single stepping is enabled in generated code.
859 static ExternalReference debug_step_in_fp_address(Isolate* isolate);
862 #ifndef V8_INTERPRETED_REGEXP
863 // C functions called from RegExp generated code.
865 // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
866 static ExternalReference re_case_insensitive_compare_uc16(Isolate* isolate);
868 // Function RegExpMacroAssembler*::CheckStackGuardState()
869 static ExternalReference re_check_stack_guard_state(Isolate* isolate);
871 // Function NativeRegExpMacroAssembler::GrowStack()
872 static ExternalReference re_grow_stack(Isolate* isolate);
874 // byte NativeRegExpMacroAssembler::word_character_bitmap
875 static ExternalReference re_word_character_map();
879 // This lets you register a function that rewrites all external references.
880 // Used by the ARM simulator to catch calls to external references.
881 static void set_redirector(Isolate* isolate,
882 ExternalReferenceRedirector* redirector) {
883 // We can't stack them.
884 ASSERT(isolate->external_reference_redirector() == NULL);
885 isolate->set_external_reference_redirector(
886 reinterpret_cast<ExternalReferenceRedirectorPointer*>(redirector));
889 static ExternalReference stress_deopt_count(Isolate* isolate);
891 bool operator==(const ExternalReference& other) const {
892 return address_ == other.address_;
895 bool operator!=(const ExternalReference& other) const {
896 return !(*this == other);
900 explicit ExternalReference(void* address)
901 : address_(address) {}
903 static void* Redirect(Isolate* isolate,
905 Type type = ExternalReference::BUILTIN_CALL) {
906 ExternalReferenceRedirector* redirector =
907 reinterpret_cast<ExternalReferenceRedirector*>(
908 isolate->external_reference_redirector());
909 if (redirector == NULL) return address;
910 void* answer = (*redirector)(address, type);
914 static void* Redirect(Isolate* isolate,
916 Type type = ExternalReference::BUILTIN_CALL) {
917 ExternalReferenceRedirector* redirector =
918 reinterpret_cast<ExternalReferenceRedirector*>(
919 isolate->external_reference_redirector());
920 void* address = reinterpret_cast<void*>(address_arg);
921 void* answer = (redirector == NULL) ?
923 (*redirector)(address, type);
931 // -----------------------------------------------------------------------------
932 // Position recording support
934 struct PositionState {
935 PositionState() : current_position(RelocInfo::kNoPosition),
936 written_position(RelocInfo::kNoPosition),
937 current_statement_position(RelocInfo::kNoPosition),
938 written_statement_position(RelocInfo::kNoPosition) {}
940 int current_position;
941 int written_position;
943 int current_statement_position;
944 int written_statement_position;
948 class PositionsRecorder BASE_EMBEDDED {
950 explicit PositionsRecorder(Assembler* assembler)
951 : assembler_(assembler) {
952 #ifdef ENABLE_GDB_JIT_INTERFACE
953 gdbjit_lineinfo_ = NULL;
955 jit_handler_data_ = NULL;
958 #ifdef ENABLE_GDB_JIT_INTERFACE
959 ~PositionsRecorder() {
960 delete gdbjit_lineinfo_;
963 void StartGDBJITLineInfoRecording() {
965 gdbjit_lineinfo_ = new GDBJITLineInfo();
969 GDBJITLineInfo* DetachGDBJITLineInfo() {
970 GDBJITLineInfo* lineinfo = gdbjit_lineinfo_;
971 gdbjit_lineinfo_ = NULL; // To prevent deallocation in destructor.
975 void AttachJITHandlerData(void* user_data) {
976 jit_handler_data_ = user_data;
979 void* DetachJITHandlerData() {
980 void* old_data = jit_handler_data_;
981 jit_handler_data_ = NULL;
984 // Set current position to pos.
985 void RecordPosition(int pos);
987 // Set current statement position to pos.
988 void RecordStatementPosition(int pos);
990 // Write recorded positions to relocation information.
991 bool WriteRecordedPositions();
993 int current_position() const { return state_.current_position; }
995 int current_statement_position() const {
996 return state_.current_statement_position;
1000 Assembler* assembler_;
1001 PositionState state_;
1002 #ifdef ENABLE_GDB_JIT_INTERFACE
1003 GDBJITLineInfo* gdbjit_lineinfo_;
1006 // Currently jit_handler_data_ is used to store JITHandler-specific data
1007 // over the lifetime of a PositionsRecorder
1008 void* jit_handler_data_;
1009 friend class PreservePositionScope;
1011 DISALLOW_COPY_AND_ASSIGN(PositionsRecorder);
1015 class PreservePositionScope BASE_EMBEDDED {
1017 explicit PreservePositionScope(PositionsRecorder* positions_recorder)
1018 : positions_recorder_(positions_recorder),
1019 saved_state_(positions_recorder->state_) {}
1021 ~PreservePositionScope() {
1022 positions_recorder_->state_ = saved_state_;
1026 PositionsRecorder* positions_recorder_;
1027 const PositionState saved_state_;
1029 DISALLOW_COPY_AND_ASSIGN(PreservePositionScope);
1033 // -----------------------------------------------------------------------------
1034 // Utility functions
1036 inline int NumberOfBitsSet(uint32_t x) {
1037 unsigned int num_bits_set;
1038 for (num_bits_set = 0; x; x >>= 1) {
1039 num_bits_set += x & 1;
1041 return num_bits_set;
1044 bool EvalComparison(Token::Value op, double op1, double op2);
1046 // Computes pow(x, y) with the special cases in the spec for Math.pow.
1047 double power_helper(double x, double y);
1048 double power_double_int(double x, int y);
1049 double power_double_double(double x, double y);
1051 // Helper class for generating code or data associated with the code
1052 // right after a call instruction. As an example this can be used to
1053 // generate safepoint data after calls for crankshaft.
1057 virtual ~CallWrapper() { }
1058 // Called just before emitting a call. Argument is the size of the generated
1060 virtual void BeforeCall(int call_size) const = 0;
1061 // Called just after emitting a call, i.e., at the return site for the call.
1062 virtual void AfterCall() const = 0;
1065 class NullCallWrapper : public CallWrapper {
1067 NullCallWrapper() { }
1068 virtual ~NullCallWrapper() { }
1069 virtual void BeforeCall(int call_size) const { }
1070 virtual void AfterCall() const { }
1074 // The multiplier and shift for signed division via multiplication, see Warren's
1075 // "Hacker's Delight", chapter 10.
1076 class MultiplierAndShift {
1078 explicit MultiplierAndShift(int32_t d);
1079 int32_t multiplier() const { return multiplier_; }
1080 int32_t shift() const { return shift_; }
1083 int32_t multiplier_;
1088 } } // namespace v8::internal
1090 #endif // V8_ASSEMBLER_H_