• 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 2006-2009 the V8 project authors. All rights reserved.
34 
35 #ifndef V8_ASSEMBLER_H_
36 #define V8_ASSEMBLER_H_
37 
38 #include "runtime.h"
39 #include "top.h"
40 #include "zone-inl.h"
41 #include "token.h"
42 
43 namespace v8 {
44 namespace internal {
45 
46 
47 // -----------------------------------------------------------------------------
48 // Labels represent pc locations; they are typically jump or call targets.
49 // After declaration, a label can be freely used to denote known or (yet)
50 // unknown pc location. Assembler::bind() is used to bind a label to the
51 // current pc. A label can be bound only once.
52 
53 class Label BASE_EMBEDDED {
54  public:
INLINE(Label ())55   INLINE(Label())                 { Unuse(); }
INLINE(~Label ())56   INLINE(~Label())                { ASSERT(!is_linked()); }
57 
INLINE(void Unuse ())58   INLINE(void Unuse())            { pos_ = 0; }
59 
INLINE(bool is_bound ()const)60   INLINE(bool is_bound()  const)  { return pos_ <  0; }
INLINE(bool is_unused ()const)61   INLINE(bool is_unused() const)  { return pos_ == 0; }
INLINE(bool is_linked ()const)62   INLINE(bool is_linked() const)  { return pos_ >  0; }
63 
64   // Returns the position of bound or linked labels. Cannot be used
65   // for unused labels.
66   int pos() const;
67 
68  private:
69   // pos_ encodes both the binding state (via its sign)
70   // and the binding position (via its value) of a label.
71   //
72   // pos_ <  0  bound label, pos() returns the jump target position
73   // pos_ == 0  unused label
74   // pos_ >  0  linked label, pos() returns the last reference position
75   int pos_;
76 
bind_to(int pos)77   void bind_to(int pos)  {
78     pos_ = -pos - 1;
79     ASSERT(is_bound());
80   }
link_to(int pos)81   void link_to(int pos)  {
82     pos_ =  pos + 1;
83     ASSERT(is_linked());
84   }
85 
86   friend class Assembler;
87   friend class RegexpAssembler;
88   friend class Displacement;
89   friend class ShadowTarget;
90   friend class RegExpMacroAssemblerIrregexp;
91 };
92 
93 
94 // -----------------------------------------------------------------------------
95 // Relocation information
96 
97 
98 // Relocation information consists of the address (pc) of the datum
99 // to which the relocation information applies, the relocation mode
100 // (rmode), and an optional data field. The relocation mode may be
101 // "descriptive" and not indicate a need for relocation, but simply
102 // describe a property of the datum. Such rmodes are useful for GC
103 // and nice disassembly output.
104 
105 class RelocInfo BASE_EMBEDDED {
106  public:
107   // The constant kNoPosition is used with the collecting of source positions
108   // in the relocation information. Two types of source positions are collected
109   // "position" (RelocMode position) and "statement position" (RelocMode
110   // statement_position). The "position" is collected at places in the source
111   // code which are of interest when making stack traces to pin-point the source
112   // location of a stack frame as close as possible. The "statement position" is
113   // collected at the beginning at each statement, and is used to indicate
114   // possible break locations. kNoPosition is used to indicate an
115   // invalid/uninitialized position value.
116   static const int kNoPosition = -1;
117 
118   enum Mode {
119     // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
120     CONSTRUCT_CALL,  // code target that is a call to a JavaScript constructor.
121     CODE_TARGET_CONTEXT,  // code target used for contextual loads.
122     DEBUG_BREAK,
123     CODE_TARGET,         // code target which is not any of the above.
124     EMBEDDED_OBJECT,
125     EMBEDDED_STRING,
126 
127     // Everything after runtime_entry (inclusive) is not GC'ed.
128     RUNTIME_ENTRY,
129     JS_RETURN,  // Marks start of the ExitJSFrame code.
130     COMMENT,
131     POSITION,  // See comment for kNoPosition above.
132     STATEMENT_POSITION,  // See comment for kNoPosition above.
133     EXTERNAL_REFERENCE,  // The address of an external C++ function.
134     INTERNAL_REFERENCE,  // An address inside the same function.
135 
136     // add more as needed
137     // Pseudo-types
138     NUMBER_OF_MODES,  // must be no greater than 14 - see RelocInfoWriter
139     NONE,  // never recorded
140     LAST_CODE_ENUM = CODE_TARGET,
141     LAST_GCED_ENUM = EMBEDDED_STRING
142   };
143 
144 
RelocInfo()145   RelocInfo() {}
RelocInfo(byte * pc,Mode rmode,intptr_t data)146   RelocInfo(byte* pc, Mode rmode, intptr_t data)
147       : pc_(pc), rmode_(rmode), data_(data) {
148   }
149 
IsConstructCall(Mode mode)150   static inline bool IsConstructCall(Mode mode) {
151     return mode == CONSTRUCT_CALL;
152   }
IsCodeTarget(Mode mode)153   static inline bool IsCodeTarget(Mode mode) {
154     return mode <= LAST_CODE_ENUM;
155   }
156   // Is the relocation mode affected by GC?
IsGCRelocMode(Mode mode)157   static inline bool IsGCRelocMode(Mode mode) {
158     return mode <= LAST_GCED_ENUM;
159   }
IsJSReturn(Mode mode)160   static inline bool IsJSReturn(Mode mode) {
161     return mode == JS_RETURN;
162   }
IsComment(Mode mode)163   static inline bool IsComment(Mode mode) {
164     return mode == COMMENT;
165   }
IsPosition(Mode mode)166   static inline bool IsPosition(Mode mode) {
167     return mode == POSITION || mode == STATEMENT_POSITION;
168   }
IsStatementPosition(Mode mode)169   static inline bool IsStatementPosition(Mode mode) {
170     return mode == STATEMENT_POSITION;
171   }
IsExternalReference(Mode mode)172   static inline bool IsExternalReference(Mode mode) {
173     return mode == EXTERNAL_REFERENCE;
174   }
IsInternalReference(Mode mode)175   static inline bool IsInternalReference(Mode mode) {
176     return mode == INTERNAL_REFERENCE;
177   }
ModeMask(Mode mode)178   static inline int ModeMask(Mode mode) { return 1 << mode; }
179 
180   // Accessors
pc()181   byte* pc() const  { return pc_; }
set_pc(byte * pc)182   void set_pc(byte* pc) { pc_ = pc; }
rmode()183   Mode rmode() const {  return rmode_; }
data()184   intptr_t data() const  { return data_; }
185 
186   // Apply a relocation by delta bytes
187   INLINE(void apply(intptr_t delta));
188 
189   // Read/modify the code target in the branch/call instruction
190   // this relocation applies to;
191   // can only be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY
192   INLINE(Address target_address());
193   INLINE(void set_target_address(Address target));
194   INLINE(Object* target_object());
195   INLINE(Handle<Object> target_object_handle(Assembler* origin));
196   INLINE(Object** target_object_address());
197   INLINE(void set_target_object(Object* target));
198 
199   // Read the address of the word containing the target_address. Can only
200   // be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY.
201   INLINE(Address target_address_address());
202 
203   // Read/modify the reference in the instruction this relocation
204   // applies to; can only be called if rmode_ is external_reference
205   INLINE(Address* target_reference_address());
206 
207   // Read/modify the address of a call instruction. This is used to relocate
208   // the break points where straight-line code is patched with a call
209   // instruction.
210   INLINE(Address call_address());
211   INLINE(void set_call_address(Address target));
212   INLINE(Object* call_object());
213   INLINE(Object** call_object_address());
214   INLINE(void set_call_object(Object* target));
215 
216   // Patch the code with some other code.
217   void PatchCode(byte* instructions, int instruction_count);
218 
219   // Patch the code with a call.
220   void PatchCodeWithCall(Address target, int guard_bytes);
221 
222   // Check whether this return sequence has been patched
223   // with a call to the debugger.
224   INLINE(bool IsPatchedReturnSequence());
225 
226 #ifdef ENABLE_DISASSEMBLER
227   // Printing
228   static const char* RelocModeName(Mode rmode);
229   void Print();
230 #endif  // ENABLE_DISASSEMBLER
231 #ifdef DEBUG
232   // Debugging
233   void Verify();
234 #endif
235 
236   static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
237   static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
238   static const int kDebugMask = kPositionMask | 1 << COMMENT;
239   static const int kApplyMask;  // Modes affected by apply. Depends on arch.
240 
241  private:
242   // On ARM, note that pc_ is the address of the constant pool entry
243   // to be relocated and not the address of the instruction
244   // referencing the constant pool entry (except when rmode_ ==
245   // comment).
246   byte* pc_;
247   Mode rmode_;
248   intptr_t data_;
249   friend class RelocIterator;
250 };
251 
252 
253 // RelocInfoWriter serializes a stream of relocation info. It writes towards
254 // lower addresses.
255 class RelocInfoWriter BASE_EMBEDDED {
256  public:
RelocInfoWriter()257   RelocInfoWriter() : pos_(NULL), last_pc_(NULL), last_data_(0) {}
RelocInfoWriter(byte * pos,byte * pc)258   RelocInfoWriter(byte* pos, byte* pc) : pos_(pos), last_pc_(pc),
259                                          last_data_(0) {}
260 
pos()261   byte* pos() const { return pos_; }
last_pc()262   byte* last_pc() const { return last_pc_; }
263 
264   void Write(const RelocInfo* rinfo);
265 
266   // Update the state of the stream after reloc info buffer
267   // and/or code is moved while the stream is active.
Reposition(byte * pos,byte * pc)268   void Reposition(byte* pos, byte* pc) {
269     pos_ = pos;
270     last_pc_ = pc;
271   }
272 
273   // Max size (bytes) of a written RelocInfo. Longest encoding is
274   // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
275   // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
276   // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
277   // Here we use the maximum of the two.
278   static const int kMaxSize = 16;
279 
280  private:
281   inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
282   inline void WriteTaggedPC(uint32_t pc_delta, int tag);
283   inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
284   inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
285   inline void WriteTaggedData(intptr_t data_delta, int tag);
286   inline void WriteExtraTag(int extra_tag, int top_tag);
287 
288   byte* pos_;
289   byte* last_pc_;
290   intptr_t last_data_;
291   DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
292 };
293 
294 
295 // A RelocIterator iterates over relocation information.
296 // Typical use:
297 //
298 //   for (RelocIterator it(code); !it.done(); it.next()) {
299 //     // do something with it.rinfo() here
300 //   }
301 //
302 // A mask can be specified to skip unwanted modes.
303 class RelocIterator: public Malloced {
304  public:
305   // Create a new iterator positioned at
306   // the beginning of the reloc info.
307   // Relocation information with mode k is included in the
308   // iteration iff bit k of mode_mask is set.
309   explicit RelocIterator(Code* code, int mode_mask = -1);
310   explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
311 
312   // Iteration
done()313   bool done() const  { return done_; }
314   void next();
315 
316   // Return pointer valid until next next().
rinfo()317   RelocInfo* rinfo() {
318     ASSERT(!done());
319     return &rinfo_;
320   }
321 
322  private:
323   // Advance* moves the position before/after reading.
324   // *Read* reads from current byte(s) into rinfo_.
325   // *Get* just reads and returns info on current byte.
326   void Advance(int bytes = 1) { pos_ -= bytes; }
327   int AdvanceGetTag();
328   int GetExtraTag();
329   int GetTopTag();
330   void ReadTaggedPC();
331   void AdvanceReadPC();
332   void AdvanceReadData();
333   void AdvanceReadVariableLengthPCJump();
334   int GetPositionTypeTag();
335   void ReadTaggedData();
336 
337   static RelocInfo::Mode DebugInfoModeFromTag(int tag);
338 
339   // If the given mode is wanted, set it in rinfo_ and return true.
340   // Else return false. Used for efficiently skipping unwanted modes.
SetMode(RelocInfo::Mode mode)341   bool SetMode(RelocInfo::Mode mode) {
342     return (mode_mask_ & 1 << mode) ? (rinfo_.rmode_ = mode, true) : false;
343   }
344 
345   byte* pos_;
346   byte* end_;
347   RelocInfo rinfo_;
348   bool done_;
349   int mode_mask_;
350   DISALLOW_COPY_AND_ASSIGN(RelocIterator);
351 };
352 
353 
354 //------------------------------------------------------------------------------
355 // External function
356 
357 //----------------------------------------------------------------------------
358 class IC_Utility;
359 class SCTableReference;
360 #ifdef ENABLE_DEBUGGER_SUPPORT
361 class Debug_Address;
362 #endif
363 
364 
365 typedef void* ExternalReferenceRedirector(void* original, bool fp_return);
366 
367 
368 // An ExternalReference represents a C++ address used in the generated
369 // code. All references to C++ functions and variables must be encapsulated in
370 // an ExternalReference instance. This is done in order to track the origin of
371 // all external references in the code so that they can be bound to the correct
372 // addresses when deserializing a heap.
373 class ExternalReference BASE_EMBEDDED {
374  public:
375   explicit ExternalReference(Builtins::CFunctionId id);
376 
377   explicit ExternalReference(ApiFunction* ptr);
378 
379   explicit ExternalReference(Builtins::Name name);
380 
381   explicit ExternalReference(Runtime::FunctionId id);
382 
383   explicit ExternalReference(Runtime::Function* f);
384 
385   explicit ExternalReference(const IC_Utility& ic_utility);
386 
387 #ifdef ENABLE_DEBUGGER_SUPPORT
388   explicit ExternalReference(const Debug_Address& debug_address);
389 #endif
390 
391   explicit ExternalReference(StatsCounter* counter);
392 
393   explicit ExternalReference(Top::AddressId id);
394 
395   explicit ExternalReference(const SCTableReference& table_ref);
396 
397   // One-of-a-kind references. These references are not part of a general
398   // pattern. This means that they have to be added to the
399   // ExternalReferenceTable in serialize.cc manually.
400 
401   static ExternalReference perform_gc_function();
402   static ExternalReference random_positive_smi_function();
403   static ExternalReference transcendental_cache_array_address();
404 
405   // Static data in the keyed lookup cache.
406   static ExternalReference keyed_lookup_cache_keys();
407   static ExternalReference keyed_lookup_cache_field_offsets();
408 
409   // Static variable Factory::the_hole_value.location()
410   static ExternalReference the_hole_value_location();
411 
412   // Static variable Heap::roots_address()
413   static ExternalReference roots_address();
414 
415   // Static variable StackGuard::address_of_jslimit()
416   static ExternalReference address_of_stack_limit();
417 
418   // Static variable StackGuard::address_of_real_jslimit()
419   static ExternalReference address_of_real_stack_limit();
420 
421   // Static variable RegExpStack::limit_address()
422   static ExternalReference address_of_regexp_stack_limit();
423 
424   // Static variables for RegExp.
425   static ExternalReference address_of_static_offsets_vector();
426   static ExternalReference address_of_regexp_stack_memory_address();
427   static ExternalReference address_of_regexp_stack_memory_size();
428 
429   // Static variable Heap::NewSpaceStart()
430   static ExternalReference new_space_start();
431   static ExternalReference new_space_mask();
432   static ExternalReference heap_always_allocate_scope_depth();
433 
434   // Used for fast allocation in generated code.
435   static ExternalReference new_space_allocation_top_address();
436   static ExternalReference new_space_allocation_limit_address();
437 
438   static ExternalReference double_fp_operation(Token::Value operation);
439   static ExternalReference compare_doubles();
440 
441   static ExternalReference handle_scope_extensions_address();
442   static ExternalReference handle_scope_next_address();
443   static ExternalReference handle_scope_limit_address();
444 
445   static ExternalReference scheduled_exception_address();
446 
address()447   Address address() const {return reinterpret_cast<Address>(address_);}
448 
449 #ifdef ENABLE_DEBUGGER_SUPPORT
450   // Function Debug::Break()
451   static ExternalReference debug_break();
452 
453   // Used to check if single stepping is enabled in generated code.
454   static ExternalReference debug_step_in_fp_address();
455 #endif
456 
457 #ifdef V8_NATIVE_REGEXP
458   // C functions called from RegExp generated code.
459 
460   // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
461   static ExternalReference re_case_insensitive_compare_uc16();
462 
463   // Function RegExpMacroAssembler*::CheckStackGuardState()
464   static ExternalReference re_check_stack_guard_state();
465 
466   // Function NativeRegExpMacroAssembler::GrowStack()
467   static ExternalReference re_grow_stack();
468 
469   // byte NativeRegExpMacroAssembler::word_character_bitmap
470   static ExternalReference re_word_character_map();
471 
472 #endif
473 
474   // This lets you register a function that rewrites all external references.
475   // Used by the ARM simulator to catch calls to external references.
set_redirector(ExternalReferenceRedirector * redirector)476   static void set_redirector(ExternalReferenceRedirector* redirector) {
477     ASSERT(redirector_ == NULL);  // We can't stack them.
478     redirector_ = redirector;
479   }
480 
481  private:
ExternalReference(void * address)482   explicit ExternalReference(void* address)
483       : address_(address) {}
484 
485   static ExternalReferenceRedirector* redirector_;
486 
487   static void* Redirect(void* address, bool fp_return = false) {
488     if (redirector_ == NULL) return address;
489     void* answer = (*redirector_)(address, fp_return);
490     return answer;
491   }
492 
493   static void* Redirect(Address address_arg, bool fp_return = false) {
494     void* address = reinterpret_cast<void*>(address_arg);
495     void* answer = (redirector_ == NULL) ?
496                    address :
497                    (*redirector_)(address, fp_return);
498     return answer;
499   }
500 
501   void* address_;
502 };
503 
504 
505 // -----------------------------------------------------------------------------
506 // Utility functions
507 
is_intn(int x,int n)508 static inline bool is_intn(int x, int n)  {
509   return -(1 << (n-1)) <= x && x < (1 << (n-1));
510 }
511 
is_int8(int x)512 static inline bool is_int8(int x)  { return is_intn(x, 8); }
is_int16(int x)513 static inline bool is_int16(int x)  { return is_intn(x, 16); }
is_int18(int x)514 static inline bool is_int18(int x)  { return is_intn(x, 18); }
is_int24(int x)515 static inline bool is_int24(int x)  { return is_intn(x, 24); }
516 
is_uintn(int x,int n)517 static inline bool is_uintn(int x, int n) {
518   return (x & -(1 << n)) == 0;
519 }
520 
is_uint2(int x)521 static inline bool is_uint2(int x)  { return is_uintn(x, 2); }
is_uint3(int x)522 static inline bool is_uint3(int x)  { return is_uintn(x, 3); }
is_uint4(int x)523 static inline bool is_uint4(int x)  { return is_uintn(x, 4); }
is_uint5(int x)524 static inline bool is_uint5(int x)  { return is_uintn(x, 5); }
is_uint6(int x)525 static inline bool is_uint6(int x)  { return is_uintn(x, 6); }
is_uint8(int x)526 static inline bool is_uint8(int x)  { return is_uintn(x, 8); }
is_uint10(int x)527 static inline bool is_uint10(int x)  { return is_uintn(x, 10); }
is_uint12(int x)528 static inline bool is_uint12(int x)  { return is_uintn(x, 12); }
is_uint16(int x)529 static inline bool is_uint16(int x)  { return is_uintn(x, 16); }
is_uint24(int x)530 static inline bool is_uint24(int x)  { return is_uintn(x, 24); }
is_uint26(int x)531 static inline bool is_uint26(int x)  { return is_uintn(x, 26); }
is_uint28(int x)532 static inline bool is_uint28(int x)  { return is_uintn(x, 28); }
533 
NumberOfBitsSet(uint32_t x)534 static inline int NumberOfBitsSet(uint32_t x) {
535   unsigned int num_bits_set;
536   for (num_bits_set = 0; x; x >>= 1) {
537     num_bits_set += x & 1;
538   }
539   return num_bits_set;
540 }
541 
542 } }  // namespace v8::internal
543 
544 #endif  // V8_ASSEMBLER_H_
545