• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 2011 the V8 project authors. All rights reserved.
34 
35 #ifndef V8_ASSEMBLER_H_
36 #define V8_ASSEMBLER_H_
37 
38 #include "gdb-jit.h"
39 #include "runtime.h"
40 #include "token.h"
41 
42 namespace v8 {
43 namespace internal {
44 
45 
46 // -----------------------------------------------------------------------------
47 // Platform independent assembler base class.
48 
49 class AssemblerBase: public Malloced {
50  public:
51   explicit AssemblerBase(Isolate* isolate);
52 
isolate()53   Isolate* isolate() const { return isolate_; }
jit_cookie()54   int jit_cookie() { return jit_cookie_; }
55 
56  private:
57   Isolate* isolate_;
58   int jit_cookie_;
59 };
60 
61 // -----------------------------------------------------------------------------
62 // Common double constants.
63 
64 class DoubleConstant: public AllStatic {
65  public:
66   static const double min_int;
67   static const double one_half;
68   static const double minus_zero;
69   static const double negative_infinity;
70   static const double nan;
71 };
72 
73 
74 // -----------------------------------------------------------------------------
75 // Labels represent pc locations; they are typically jump or call targets.
76 // After declaration, a label can be freely used to denote known or (yet)
77 // unknown pc location. Assembler::bind() is used to bind a label to the
78 // current pc. A label can be bound only once.
79 
80 class Label BASE_EMBEDDED {
81  public:
INLINE(Label ())82   INLINE(Label())                 { Unuse(); }
INLINE(~Label ())83   INLINE(~Label())                { ASSERT(!is_linked()); }
84 
INLINE(void Unuse ())85   INLINE(void Unuse())            { pos_ = 0; }
86 
INLINE(bool is_bound ()const)87   INLINE(bool is_bound() const)  { return pos_ <  0; }
INLINE(bool is_unused ()const)88   INLINE(bool is_unused() const)  { return pos_ == 0; }
INLINE(bool is_linked ()const)89   INLINE(bool is_linked() const)  { return pos_ >  0; }
90 
91   // Returns the position of bound or linked labels. Cannot be used
92   // for unused labels.
93   int pos() const;
94 
95  private:
96   // pos_ encodes both the binding state (via its sign)
97   // and the binding position (via its value) of a label.
98   //
99   // pos_ <  0  bound label, pos() returns the jump target position
100   // pos_ == 0  unused label
101   // pos_ >  0  linked label, pos() returns the last reference position
102   int pos_;
103 
bind_to(int pos)104   void bind_to(int pos)  {
105     pos_ = -pos - 1;
106     ASSERT(is_bound());
107   }
link_to(int pos)108   void link_to(int pos)  {
109     pos_ =  pos + 1;
110     ASSERT(is_linked());
111   }
112 
113   friend class Assembler;
114   friend class RegexpAssembler;
115   friend class Displacement;
116   friend class RegExpMacroAssemblerIrregexp;
117 };
118 
119 
120 // -----------------------------------------------------------------------------
121 // NearLabels are labels used for short jumps (in Intel jargon).
122 // NearLabels should be used if it can be guaranteed that the jump range is
123 // within -128 to +127. We already use short jumps when jumping backwards,
124 // so using a NearLabel will only have performance impact if used for forward
125 // jumps.
126 class NearLabel BASE_EMBEDDED {
127  public:
NearLabel()128   NearLabel() { Unuse(); }
~NearLabel()129   ~NearLabel() { ASSERT(!is_linked()); }
130 
Unuse()131   void Unuse() {
132     pos_ = -1;
133     unresolved_branches_ = 0;
134 #ifdef DEBUG
135     for (int i = 0; i < kMaxUnresolvedBranches; i++) {
136       unresolved_positions_[i] = -1;
137     }
138 #endif
139   }
140 
pos()141   int pos() {
142     ASSERT(is_bound());
143     return pos_;
144   }
145 
is_bound()146   bool is_bound() { return pos_ >= 0; }
is_linked()147   bool is_linked() { return !is_bound() && unresolved_branches_ > 0; }
is_unused()148   bool is_unused() { return !is_bound() && unresolved_branches_ == 0; }
149 
bind_to(int position)150   void bind_to(int position) {
151     ASSERT(!is_bound());
152     pos_ = position;
153   }
154 
link_to(int position)155   void link_to(int position) {
156     ASSERT(!is_bound());
157     ASSERT(unresolved_branches_ < kMaxUnresolvedBranches);
158     unresolved_positions_[unresolved_branches_++] = position;
159   }
160 
161  private:
162   static const int kMaxUnresolvedBranches = 8;
163   int pos_;
164   int unresolved_branches_;
165   int unresolved_positions_[kMaxUnresolvedBranches];
166 
167   friend class Assembler;
168 };
169 
170 
171 // -----------------------------------------------------------------------------
172 // Relocation information
173 
174 
175 // Relocation information consists of the address (pc) of the datum
176 // to which the relocation information applies, the relocation mode
177 // (rmode), and an optional data field. The relocation mode may be
178 // "descriptive" and not indicate a need for relocation, but simply
179 // describe a property of the datum. Such rmodes are useful for GC
180 // and nice disassembly output.
181 
182 class RelocInfo BASE_EMBEDDED {
183  public:
184   // The constant kNoPosition is used with the collecting of source positions
185   // in the relocation information. Two types of source positions are collected
186   // "position" (RelocMode position) and "statement position" (RelocMode
187   // statement_position). The "position" is collected at places in the source
188   // code which are of interest when making stack traces to pin-point the source
189   // location of a stack frame as close as possible. The "statement position" is
190   // collected at the beginning at each statement, and is used to indicate
191   // possible break locations. kNoPosition is used to indicate an
192   // invalid/uninitialized position value.
193   static const int kNoPosition = -1;
194 
195   // This string is used to add padding comments to the reloc info in cases
196   // where we are not sure to have enough space for patching in during
197   // lazy deoptimization. This is the case if we have indirect calls for which
198   // we do not normally record relocation info.
199   static const char* kFillerCommentString;
200 
201   // The minimum size of a comment is equal to three bytes for the extra tagged
202   // pc + the tag for the data, and kPointerSize for the actual pointer to the
203   // comment.
204   static const int kMinRelocCommentSize = 3 + kPointerSize;
205 
206   // The maximum size for a call instruction including pc-jump.
207   static const int kMaxCallSize = 6;
208 
209   // The maximum pc delta that will use the short encoding.
210   static const int kMaxSmallPCDelta;
211 
212   enum Mode {
213     // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
214     CONSTRUCT_CALL,  // code target that is a call to a JavaScript constructor.
215     CODE_TARGET_CONTEXT,  // Code target used for contextual loads and stores.
216     DEBUG_BREAK,  // Code target for the debugger statement.
217     CODE_TARGET,  // Code target which is not any of the above.
218     EMBEDDED_OBJECT,
219     GLOBAL_PROPERTY_CELL,
220 
221     // Everything after runtime_entry (inclusive) is not GC'ed.
222     RUNTIME_ENTRY,
223     JS_RETURN,  // Marks start of the ExitJSFrame code.
224     COMMENT,
225     POSITION,  // See comment for kNoPosition above.
226     STATEMENT_POSITION,  // See comment for kNoPosition above.
227     DEBUG_BREAK_SLOT,  // Additional code inserted for debug break slot.
228     EXTERNAL_REFERENCE,  // The address of an external C++ function.
229     INTERNAL_REFERENCE,  // An address inside the same function.
230 
231     // add more as needed
232     // Pseudo-types
233     NUMBER_OF_MODES,  // must be no greater than 14 - see RelocInfoWriter
234     NONE,  // never recorded
235     LAST_CODE_ENUM = CODE_TARGET,
236     LAST_GCED_ENUM = GLOBAL_PROPERTY_CELL
237   };
238 
239 
RelocInfo()240   RelocInfo() {}
RelocInfo(byte * pc,Mode rmode,intptr_t data)241   RelocInfo(byte* pc, Mode rmode, intptr_t data)
242       : pc_(pc), rmode_(rmode), data_(data) {
243   }
244 
IsConstructCall(Mode mode)245   static inline bool IsConstructCall(Mode mode) {
246     return mode == CONSTRUCT_CALL;
247   }
IsCodeTarget(Mode mode)248   static inline bool IsCodeTarget(Mode mode) {
249     return mode <= LAST_CODE_ENUM;
250   }
251   // Is the relocation mode affected by GC?
IsGCRelocMode(Mode mode)252   static inline bool IsGCRelocMode(Mode mode) {
253     return mode <= LAST_GCED_ENUM;
254   }
IsJSReturn(Mode mode)255   static inline bool IsJSReturn(Mode mode) {
256     return mode == JS_RETURN;
257   }
IsComment(Mode mode)258   static inline bool IsComment(Mode mode) {
259     return mode == COMMENT;
260   }
IsPosition(Mode mode)261   static inline bool IsPosition(Mode mode) {
262     return mode == POSITION || mode == STATEMENT_POSITION;
263   }
IsStatementPosition(Mode mode)264   static inline bool IsStatementPosition(Mode mode) {
265     return mode == STATEMENT_POSITION;
266   }
IsExternalReference(Mode mode)267   static inline bool IsExternalReference(Mode mode) {
268     return mode == EXTERNAL_REFERENCE;
269   }
IsInternalReference(Mode mode)270   static inline bool IsInternalReference(Mode mode) {
271     return mode == INTERNAL_REFERENCE;
272   }
IsDebugBreakSlot(Mode mode)273   static inline bool IsDebugBreakSlot(Mode mode) {
274     return mode == DEBUG_BREAK_SLOT;
275   }
ModeMask(Mode mode)276   static inline int ModeMask(Mode mode) { return 1 << mode; }
277 
278   // Accessors
pc()279   byte* pc() const { return pc_; }
set_pc(byte * pc)280   void set_pc(byte* pc) { pc_ = pc; }
rmode()281   Mode rmode() const {  return rmode_; }
data()282   intptr_t data() const { return data_; }
283 
284   // Apply a relocation by delta bytes
285   INLINE(void apply(intptr_t delta));
286 
287   // Is the pointer this relocation info refers to coded like a plain pointer
288   // or is it strange in some way (eg relative or patched into a series of
289   // instructions).
290   bool IsCodedSpecially();
291 
292   // Read/modify the code target in the branch/call instruction
293   // this relocation applies to;
294   // can only be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY
295   INLINE(Address target_address());
296   INLINE(void set_target_address(Address target));
297   INLINE(Object* target_object());
298   INLINE(Handle<Object> target_object_handle(Assembler* origin));
299   INLINE(Object** target_object_address());
300   INLINE(void set_target_object(Object* target));
301   INLINE(JSGlobalPropertyCell* target_cell());
302   INLINE(Handle<JSGlobalPropertyCell> target_cell_handle());
303   INLINE(void set_target_cell(JSGlobalPropertyCell* cell));
304 
305 
306   // Read the address of the word containing the target_address in an
307   // instruction stream.  What this means exactly is architecture-independent.
308   // The only architecture-independent user of this function is the serializer.
309   // The serializer uses it to find out how many raw bytes of instruction to
310   // output before the next target.  Architecture-independent code shouldn't
311   // dereference the pointer it gets back from this.
312   INLINE(Address target_address_address());
313   // This indicates how much space a target takes up when deserializing a code
314   // stream.  For most architectures this is just the size of a pointer.  For
315   // an instruction like movw/movt where the target bits are mixed into the
316   // instruction bits the size of the target will be zero, indicating that the
317   // serializer should not step forwards in memory after a target is resolved
318   // and written.  In this case the target_address_address function above
319   // should return the end of the instructions to be patched, allowing the
320   // deserializer to deserialize the instructions as raw bytes and put them in
321   // place, ready to be patched with the target.
322   INLINE(int target_address_size());
323 
324   // Read/modify the reference in the instruction this relocation
325   // applies to; can only be called if rmode_ is external_reference
326   INLINE(Address* target_reference_address());
327 
328   // Read/modify the address of a call instruction. This is used to relocate
329   // the break points where straight-line code is patched with a call
330   // instruction.
331   INLINE(Address call_address());
332   INLINE(void set_call_address(Address target));
333   INLINE(Object* call_object());
334   INLINE(void set_call_object(Object* target));
335   INLINE(Object** call_object_address());
336 
337   template<typename StaticVisitor> inline void Visit(Heap* heap);
338   inline void Visit(ObjectVisitor* v);
339 
340   // Patch the code with some other code.
341   void PatchCode(byte* instructions, int instruction_count);
342 
343   // Patch the code with a call.
344   void PatchCodeWithCall(Address target, int guard_bytes);
345 
346   // Check whether this return sequence has been patched
347   // with a call to the debugger.
348   INLINE(bool IsPatchedReturnSequence());
349 
350   // Check whether this debug break slot has been patched with a call to the
351   // debugger.
352   INLINE(bool IsPatchedDebugBreakSlotSequence());
353 
354 #ifdef ENABLE_DISASSEMBLER
355   // Printing
356   static const char* RelocModeName(Mode rmode);
357   void Print(FILE* out);
358 #endif  // ENABLE_DISASSEMBLER
359 #ifdef DEBUG
360   // Debugging
361   void Verify();
362 #endif
363 
364   static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
365   static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
366   static const int kDebugMask = kPositionMask | 1 << COMMENT;
367   static const int kApplyMask;  // Modes affected by apply. Depends on arch.
368 
369  private:
370   // On ARM, note that pc_ is the address of the constant pool entry
371   // to be relocated and not the address of the instruction
372   // referencing the constant pool entry (except when rmode_ ==
373   // comment).
374   byte* pc_;
375   Mode rmode_;
376   intptr_t data_;
377   friend class RelocIterator;
378 };
379 
380 
381 // RelocInfoWriter serializes a stream of relocation info. It writes towards
382 // lower addresses.
383 class RelocInfoWriter BASE_EMBEDDED {
384  public:
RelocInfoWriter()385   RelocInfoWriter() : pos_(NULL), last_pc_(NULL), last_data_(0) {}
RelocInfoWriter(byte * pos,byte * pc)386   RelocInfoWriter(byte* pos, byte* pc) : pos_(pos), last_pc_(pc),
387                                          last_data_(0) {}
388 
pos()389   byte* pos() const { return pos_; }
last_pc()390   byte* last_pc() const { return last_pc_; }
391 
392   void Write(const RelocInfo* rinfo);
393 
394   // Update the state of the stream after reloc info buffer
395   // and/or code is moved while the stream is active.
Reposition(byte * pos,byte * pc)396   void Reposition(byte* pos, byte* pc) {
397     pos_ = pos;
398     last_pc_ = pc;
399   }
400 
401   // Max size (bytes) of a written RelocInfo. Longest encoding is
402   // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
403   // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
404   // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
405   // Here we use the maximum of the two.
406   static const int kMaxSize = 16;
407 
408  private:
409   inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
410   inline void WriteTaggedPC(uint32_t pc_delta, int tag);
411   inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
412   inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
413   inline void WriteTaggedData(intptr_t data_delta, int tag);
414   inline void WriteExtraTag(int extra_tag, int top_tag);
415 
416   byte* pos_;
417   byte* last_pc_;
418   intptr_t last_data_;
419   DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
420 };
421 
422 
423 // A RelocIterator iterates over relocation information.
424 // Typical use:
425 //
426 //   for (RelocIterator it(code); !it.done(); it.next()) {
427 //     // do something with it.rinfo() here
428 //   }
429 //
430 // A mask can be specified to skip unwanted modes.
431 class RelocIterator: public Malloced {
432  public:
433   // Create a new iterator positioned at
434   // the beginning of the reloc info.
435   // Relocation information with mode k is included in the
436   // iteration iff bit k of mode_mask is set.
437   explicit RelocIterator(Code* code, int mode_mask = -1);
438   explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
439 
440   // Iteration
done()441   bool done() const { return done_; }
442   void next();
443 
444   // Return pointer valid until next next().
rinfo()445   RelocInfo* rinfo() {
446     ASSERT(!done());
447     return &rinfo_;
448   }
449 
450  private:
451   // Advance* moves the position before/after reading.
452   // *Read* reads from current byte(s) into rinfo_.
453   // *Get* just reads and returns info on current byte.
454   void Advance(int bytes = 1) { pos_ -= bytes; }
455   int AdvanceGetTag();
456   int GetExtraTag();
457   int GetTopTag();
458   void ReadTaggedPC();
459   void AdvanceReadPC();
460   void AdvanceReadData();
461   void AdvanceReadVariableLengthPCJump();
462   int GetPositionTypeTag();
463   void ReadTaggedData();
464 
465   static RelocInfo::Mode DebugInfoModeFromTag(int tag);
466 
467   // If the given mode is wanted, set it in rinfo_ and return true.
468   // Else return false. Used for efficiently skipping unwanted modes.
SetMode(RelocInfo::Mode mode)469   bool SetMode(RelocInfo::Mode mode) {
470     return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
471   }
472 
473   byte* pos_;
474   byte* end_;
475   RelocInfo rinfo_;
476   bool done_;
477   int mode_mask_;
478   DISALLOW_COPY_AND_ASSIGN(RelocIterator);
479 };
480 
481 
482 //------------------------------------------------------------------------------
483 // External function
484 
485 //----------------------------------------------------------------------------
486 class IC_Utility;
487 class SCTableReference;
488 #ifdef ENABLE_DEBUGGER_SUPPORT
489 class Debug_Address;
490 #endif
491 
492 
493 // An ExternalReference represents a C++ address used in the generated
494 // code. All references to C++ functions and variables must be encapsulated in
495 // an ExternalReference instance. This is done in order to track the origin of
496 // all external references in the code so that they can be bound to the correct
497 // addresses when deserializing a heap.
498 class ExternalReference BASE_EMBEDDED {
499  public:
500   // Used in the simulator to support different native api calls.
501   enum Type {
502     // Builtin call.
503     // MaybeObject* f(v8::internal::Arguments).
504     BUILTIN_CALL,  // default
505 
506     // Builtin call that returns floating point.
507     // double f(double, double).
508     FP_RETURN_CALL,
509 
510     // Direct call to API function callback.
511     // Handle<Value> f(v8::Arguments&)
512     DIRECT_API_CALL,
513 
514     // Direct call to accessor getter callback.
515     // Handle<value> f(Local<String> property, AccessorInfo& info)
516     DIRECT_GETTER_CALL
517   };
518 
519   typedef void* ExternalReferenceRedirector(void* original, Type type);
520 
521   ExternalReference(Builtins::CFunctionId id, Isolate* isolate);
522 
523   ExternalReference(ApiFunction* ptr, Type type, Isolate* isolate);
524 
525   ExternalReference(Builtins::Name name, Isolate* isolate);
526 
527   ExternalReference(Runtime::FunctionId id, Isolate* isolate);
528 
529   ExternalReference(const Runtime::Function* f, Isolate* isolate);
530 
531   ExternalReference(const IC_Utility& ic_utility, Isolate* isolate);
532 
533 #ifdef ENABLE_DEBUGGER_SUPPORT
534   ExternalReference(const Debug_Address& debug_address, Isolate* isolate);
535 #endif
536 
537   explicit ExternalReference(StatsCounter* counter);
538 
539   ExternalReference(Isolate::AddressId id, Isolate* isolate);
540 
541   explicit ExternalReference(const SCTableReference& table_ref);
542 
543   // Isolate::Current() as an external reference.
544   static ExternalReference isolate_address();
545 
546   // One-of-a-kind references. These references are not part of a general
547   // pattern. This means that they have to be added to the
548   // ExternalReferenceTable in serialize.cc manually.
549 
550   static ExternalReference perform_gc_function(Isolate* isolate);
551   static ExternalReference fill_heap_number_with_random_function(
552       Isolate* isolate);
553   static ExternalReference random_uint32_function(Isolate* isolate);
554   static ExternalReference transcendental_cache_array_address(Isolate* isolate);
555   static ExternalReference delete_handle_scope_extensions(Isolate* isolate);
556 
557   // Deoptimization support.
558   static ExternalReference new_deoptimizer_function(Isolate* isolate);
559   static ExternalReference compute_output_frames_function(Isolate* isolate);
560   static ExternalReference global_contexts_list(Isolate* isolate);
561 
562   // Static data in the keyed lookup cache.
563   static ExternalReference keyed_lookup_cache_keys(Isolate* isolate);
564   static ExternalReference keyed_lookup_cache_field_offsets(Isolate* isolate);
565 
566   // Static variable Factory::the_hole_value.location()
567   static ExternalReference the_hole_value_location(Isolate* isolate);
568 
569   // Static variable Factory::arguments_marker.location()
570   static ExternalReference arguments_marker_location(Isolate* isolate);
571 
572   // Static variable Heap::roots_address()
573   static ExternalReference roots_address(Isolate* isolate);
574 
575   // Static variable StackGuard::address_of_jslimit()
576   static ExternalReference address_of_stack_limit(Isolate* isolate);
577 
578   // Static variable StackGuard::address_of_real_jslimit()
579   static ExternalReference address_of_real_stack_limit(Isolate* isolate);
580 
581   // Static variable RegExpStack::limit_address()
582   static ExternalReference address_of_regexp_stack_limit(Isolate* isolate);
583 
584   // Static variables for RegExp.
585   static ExternalReference address_of_static_offsets_vector(Isolate* isolate);
586   static ExternalReference address_of_regexp_stack_memory_address(
587       Isolate* isolate);
588   static ExternalReference address_of_regexp_stack_memory_size(
589       Isolate* isolate);
590 
591   // Static variable Heap::NewSpaceStart()
592   static ExternalReference new_space_start(Isolate* isolate);
593   static ExternalReference new_space_mask(Isolate* isolate);
594   static ExternalReference heap_always_allocate_scope_depth(Isolate* isolate);
595 
596   // Used for fast allocation in generated code.
597   static ExternalReference new_space_allocation_top_address(Isolate* isolate);
598   static ExternalReference new_space_allocation_limit_address(Isolate* isolate);
599 
600   static ExternalReference double_fp_operation(Token::Value operation,
601                                                Isolate* isolate);
602   static ExternalReference compare_doubles(Isolate* isolate);
603   static ExternalReference power_double_double_function(Isolate* isolate);
604   static ExternalReference power_double_int_function(Isolate* isolate);
605 
606   static ExternalReference handle_scope_next_address();
607   static ExternalReference handle_scope_limit_address();
608   static ExternalReference handle_scope_level_address();
609 
610   static ExternalReference scheduled_exception_address(Isolate* isolate);
611 
612   // Static variables containing common double constants.
613   static ExternalReference address_of_min_int();
614   static ExternalReference address_of_one_half();
615   static ExternalReference address_of_minus_zero();
616   static ExternalReference address_of_negative_infinity();
617   static ExternalReference address_of_nan();
618 
619   static ExternalReference math_sin_double_function(Isolate* isolate);
620   static ExternalReference math_cos_double_function(Isolate* isolate);
621   static ExternalReference math_log_double_function(Isolate* isolate);
622 
address()623   Address address() const {return reinterpret_cast<Address>(address_);}
624 
625 #ifdef ENABLE_DEBUGGER_SUPPORT
626   // Function Debug::Break()
627   static ExternalReference debug_break(Isolate* isolate);
628 
629   // Used to check if single stepping is enabled in generated code.
630   static ExternalReference debug_step_in_fp_address(Isolate* isolate);
631 #endif
632 
633 #ifndef V8_INTERPRETED_REGEXP
634   // C functions called from RegExp generated code.
635 
636   // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
637   static ExternalReference re_case_insensitive_compare_uc16(Isolate* isolate);
638 
639   // Function RegExpMacroAssembler*::CheckStackGuardState()
640   static ExternalReference re_check_stack_guard_state(Isolate* isolate);
641 
642   // Function NativeRegExpMacroAssembler::GrowStack()
643   static ExternalReference re_grow_stack(Isolate* isolate);
644 
645   // byte NativeRegExpMacroAssembler::word_character_bitmap
646   static ExternalReference re_word_character_map();
647 
648 #endif
649 
650   // This lets you register a function that rewrites all external references.
651   // Used by the ARM simulator to catch calls to external references.
set_redirector(ExternalReferenceRedirector * redirector)652   static void set_redirector(ExternalReferenceRedirector* redirector) {
653     // We can't stack them.
654     ASSERT(Isolate::Current()->external_reference_redirector() == NULL);
655     Isolate::Current()->set_external_reference_redirector(
656         reinterpret_cast<ExternalReferenceRedirectorPointer*>(redirector));
657   }
658 
659  private:
ExternalReference(void * address)660   explicit ExternalReference(void* address)
661       : address_(address) {}
662 
663   static void* Redirect(Isolate* isolate,
664                         void* address,
665                         Type type = ExternalReference::BUILTIN_CALL) {
666     ExternalReferenceRedirector* redirector =
667         reinterpret_cast<ExternalReferenceRedirector*>(
668             isolate->external_reference_redirector());
669     if (redirector == NULL) return address;
670     void* answer = (*redirector)(address, type);
671     return answer;
672   }
673 
674   static void* Redirect(Isolate* isolate,
675                         Address address_arg,
676                         Type type = ExternalReference::BUILTIN_CALL) {
677     ExternalReferenceRedirector* redirector =
678         reinterpret_cast<ExternalReferenceRedirector*>(
679             isolate->external_reference_redirector());
680     void* address = reinterpret_cast<void*>(address_arg);
681     void* answer = (redirector == NULL) ?
682                    address :
683                    (*redirector)(address, type);
684     return answer;
685   }
686 
687   void* address_;
688 };
689 
690 
691 // -----------------------------------------------------------------------------
692 // Position recording support
693 
694 struct PositionState {
PositionStatePositionState695   PositionState() : current_position(RelocInfo::kNoPosition),
696                     written_position(RelocInfo::kNoPosition),
697                     current_statement_position(RelocInfo::kNoPosition),
698                     written_statement_position(RelocInfo::kNoPosition) {}
699 
700   int current_position;
701   int written_position;
702 
703   int current_statement_position;
704   int written_statement_position;
705 };
706 
707 
708 class PositionsRecorder BASE_EMBEDDED {
709  public:
PositionsRecorder(Assembler * assembler)710   explicit PositionsRecorder(Assembler* assembler)
711       : assembler_(assembler) {
712 #ifdef ENABLE_GDB_JIT_INTERFACE
713     gdbjit_lineinfo_ = NULL;
714 #endif
715   }
716 
717 #ifdef ENABLE_GDB_JIT_INTERFACE
~PositionsRecorder()718   ~PositionsRecorder() {
719     delete gdbjit_lineinfo_;
720   }
721 
StartGDBJITLineInfoRecording()722   void StartGDBJITLineInfoRecording() {
723     if (FLAG_gdbjit) {
724       gdbjit_lineinfo_ = new GDBJITLineInfo();
725     }
726   }
727 
DetachGDBJITLineInfo()728   GDBJITLineInfo* DetachGDBJITLineInfo() {
729     GDBJITLineInfo* lineinfo = gdbjit_lineinfo_;
730     gdbjit_lineinfo_ = NULL;  // To prevent deallocation in destructor.
731     return lineinfo;
732   }
733 #endif
734 
735   // Set current position to pos.
736   void RecordPosition(int pos);
737 
738   // Set current statement position to pos.
739   void RecordStatementPosition(int pos);
740 
741   // Write recorded positions to relocation information.
742   bool WriteRecordedPositions();
743 
current_position()744   int current_position() const { return state_.current_position; }
745 
current_statement_position()746   int current_statement_position() const {
747     return state_.current_statement_position;
748   }
749 
750  private:
751   Assembler* assembler_;
752   PositionState state_;
753 #ifdef ENABLE_GDB_JIT_INTERFACE
754   GDBJITLineInfo* gdbjit_lineinfo_;
755 #endif
756 
757   friend class PreservePositionScope;
758 
759   DISALLOW_COPY_AND_ASSIGN(PositionsRecorder);
760 };
761 
762 
763 class PreservePositionScope BASE_EMBEDDED {
764  public:
PreservePositionScope(PositionsRecorder * positions_recorder)765   explicit PreservePositionScope(PositionsRecorder* positions_recorder)
766       : positions_recorder_(positions_recorder),
767         saved_state_(positions_recorder->state_) {}
768 
~PreservePositionScope()769   ~PreservePositionScope() {
770     positions_recorder_->state_ = saved_state_;
771   }
772 
773  private:
774   PositionsRecorder* positions_recorder_;
775   const PositionState saved_state_;
776 
777   DISALLOW_COPY_AND_ASSIGN(PreservePositionScope);
778 };
779 
780 
781 // -----------------------------------------------------------------------------
782 // Utility functions
783 
is_intn(int x,int n)784 static inline bool is_intn(int x, int n)  {
785   return -(1 << (n-1)) <= x && x < (1 << (n-1));
786 }
787 
is_int8(int x)788 static inline bool is_int8(int x)  { return is_intn(x, 8); }
is_int16(int x)789 static inline bool is_int16(int x)  { return is_intn(x, 16); }
is_int18(int x)790 static inline bool is_int18(int x)  { return is_intn(x, 18); }
is_int24(int x)791 static inline bool is_int24(int x)  { return is_intn(x, 24); }
792 
is_uintn(int x,int n)793 static inline bool is_uintn(int x, int n) {
794   return (x & -(1 << n)) == 0;
795 }
796 
is_uint2(int x)797 static inline bool is_uint2(int x)  { return is_uintn(x, 2); }
is_uint3(int x)798 static inline bool is_uint3(int x)  { return is_uintn(x, 3); }
is_uint4(int x)799 static inline bool is_uint4(int x)  { return is_uintn(x, 4); }
is_uint5(int x)800 static inline bool is_uint5(int x)  { return is_uintn(x, 5); }
is_uint6(int x)801 static inline bool is_uint6(int x)  { return is_uintn(x, 6); }
is_uint8(int x)802 static inline bool is_uint8(int x)  { return is_uintn(x, 8); }
is_uint10(int x)803 static inline bool is_uint10(int x)  { return is_uintn(x, 10); }
is_uint12(int x)804 static inline bool is_uint12(int x)  { return is_uintn(x, 12); }
is_uint16(int x)805 static inline bool is_uint16(int x)  { return is_uintn(x, 16); }
is_uint24(int x)806 static inline bool is_uint24(int x)  { return is_uintn(x, 24); }
is_uint26(int x)807 static inline bool is_uint26(int x)  { return is_uintn(x, 26); }
is_uint28(int x)808 static inline bool is_uint28(int x)  { return is_uintn(x, 28); }
809 
NumberOfBitsSet(uint32_t x)810 static inline int NumberOfBitsSet(uint32_t x) {
811   unsigned int num_bits_set;
812   for (num_bits_set = 0; x; x >>= 1) {
813     num_bits_set += x & 1;
814   }
815   return num_bits_set;
816 }
817 
818 // Computes pow(x, y) with the special cases in the spec for Math.pow.
819 double power_double_int(double x, int y);
820 double power_double_double(double x, double y);
821 
822 } }  // namespace v8::internal
823 
824 #endif  // V8_ASSEMBLER_H_
825