• 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     CODE_TARGET,         // code target which is not any of the above.
123     EMBEDDED_OBJECT,
124     EMBEDDED_STRING,
125 
126     // Everything after runtime_entry (inclusive) is not GC'ed.
127     RUNTIME_ENTRY,
128     JS_RETURN,  // Marks start of the ExitJSFrame code.
129     COMMENT,
130     POSITION,  // See comment for kNoPosition above.
131     STATEMENT_POSITION,  // See comment for kNoPosition above.
132     EXTERNAL_REFERENCE,  // The address of an external C++ function.
133     INTERNAL_REFERENCE,  // An address inside the same function.
134 
135     // add more as needed
136     // Pseudo-types
137     NUMBER_OF_MODES,  // must be no greater than 14 - see RelocInfoWriter
138     NONE,  // never recorded
139     LAST_CODE_ENUM = CODE_TARGET,
140     LAST_GCED_ENUM = EMBEDDED_STRING
141   };
142 
143 
RelocInfo()144   RelocInfo() {}
RelocInfo(byte * pc,Mode rmode,intptr_t data)145   RelocInfo(byte* pc, Mode rmode, intptr_t data)
146       : pc_(pc), rmode_(rmode), data_(data) {
147   }
148 
IsConstructCall(Mode mode)149   static inline bool IsConstructCall(Mode mode) {
150     return mode == CONSTRUCT_CALL;
151   }
IsCodeTarget(Mode mode)152   static inline bool IsCodeTarget(Mode mode) {
153     return mode <= LAST_CODE_ENUM;
154   }
155   // Is the relocation mode affected by GC?
IsGCRelocMode(Mode mode)156   static inline bool IsGCRelocMode(Mode mode) {
157     return mode <= LAST_GCED_ENUM;
158   }
IsJSReturn(Mode mode)159   static inline bool IsJSReturn(Mode mode) {
160     return mode == JS_RETURN;
161   }
IsComment(Mode mode)162   static inline bool IsComment(Mode mode) {
163     return mode == COMMENT;
164   }
IsPosition(Mode mode)165   static inline bool IsPosition(Mode mode) {
166     return mode == POSITION || mode == STATEMENT_POSITION;
167   }
IsStatementPosition(Mode mode)168   static inline bool IsStatementPosition(Mode mode) {
169     return mode == STATEMENT_POSITION;
170   }
IsExternalReference(Mode mode)171   static inline bool IsExternalReference(Mode mode) {
172     return mode == EXTERNAL_REFERENCE;
173   }
IsInternalReference(Mode mode)174   static inline bool IsInternalReference(Mode mode) {
175     return mode == INTERNAL_REFERENCE;
176   }
ModeMask(Mode mode)177   static inline int ModeMask(Mode mode) { return 1 << mode; }
178 
179   // Accessors
pc()180   byte* pc() const  { return pc_; }
set_pc(byte * pc)181   void set_pc(byte* pc) { pc_ = pc; }
rmode()182   Mode rmode() const {  return rmode_; }
data()183   intptr_t data() const  { return data_; }
184 
185   // Apply a relocation by delta bytes
186   INLINE(void apply(intptr_t delta));
187 
188   // Read/modify the code target in the branch/call instruction
189   // this relocation applies to;
190   // can only be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY
191   INLINE(Address target_address());
192   INLINE(void set_target_address(Address target));
193   INLINE(Object* target_object());
194   INLINE(Object** target_object_address());
195   INLINE(void set_target_object(Object* target));
196 
197   // Read the address of the word containing the target_address. Can only
198   // be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY.
199   INLINE(Address target_address_address());
200 
201   // Read/modify the reference in the instruction this relocation
202   // applies to; can only be called if rmode_ is external_reference
203   INLINE(Address* target_reference_address());
204 
205   // Read/modify the address of a call instruction. This is used to relocate
206   // the break points where straight-line code is patched with a call
207   // instruction.
208   INLINE(Address call_address());
209   INLINE(void set_call_address(Address target));
210   INLINE(Object* call_object());
211   INLINE(Object** call_object_address());
212   INLINE(void set_call_object(Object* target));
213 
214   // Patch the code with some other code.
215   void PatchCode(byte* instructions, int instruction_count);
216 
217   // Patch the code with a call.
218   void PatchCodeWithCall(Address target, int guard_bytes);
219   // Check whether the current instruction is currently a call
220   // sequence (whether naturally or a return sequence overwritten
221   // to enter the debugger).
222   INLINE(bool IsCallInstruction());
223 
224 #ifdef ENABLE_DISASSEMBLER
225   // Printing
226   static const char* RelocModeName(Mode rmode);
227   void Print();
228 #endif  // ENABLE_DISASSEMBLER
229 #ifdef DEBUG
230   // Debugging
231   void Verify();
232 #endif
233 
234   static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
235   static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
236   static const int kDebugMask = kPositionMask | 1 << COMMENT;
237   static const int kApplyMask;  // Modes affected by apply. Depends on arch.
238 
239  private:
240   // On ARM, note that pc_ is the address of the constant pool entry
241   // to be relocated and not the address of the instruction
242   // referencing the constant pool entry (except when rmode_ ==
243   // comment).
244   byte* pc_;
245   Mode rmode_;
246   intptr_t data_;
247   friend class RelocIterator;
248 };
249 
250 
251 // RelocInfoWriter serializes a stream of relocation info. It writes towards
252 // lower addresses.
253 class RelocInfoWriter BASE_EMBEDDED {
254  public:
RelocInfoWriter()255   RelocInfoWriter() : pos_(NULL), last_pc_(NULL), last_data_(0) {}
RelocInfoWriter(byte * pos,byte * pc)256   RelocInfoWriter(byte* pos, byte* pc) : pos_(pos), last_pc_(pc),
257                                          last_data_(0) {}
258 
pos()259   byte* pos() const { return pos_; }
last_pc()260   byte* last_pc() const { return last_pc_; }
261 
262   void Write(const RelocInfo* rinfo);
263 
264   // Update the state of the stream after reloc info buffer
265   // and/or code is moved while the stream is active.
Reposition(byte * pos,byte * pc)266   void Reposition(byte* pos, byte* pc) {
267     pos_ = pos;
268     last_pc_ = pc;
269   }
270 
271   // Max size (bytes) of a written RelocInfo. Longest encoding is
272   // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
273   // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
274   // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
275   // Here we use the maximum of the two.
276   static const int kMaxSize = 16;
277 
278  private:
279   inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
280   inline void WriteTaggedPC(uint32_t pc_delta, int tag);
281   inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
282   inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
283   inline void WriteTaggedData(intptr_t data_delta, int tag);
284   inline void WriteExtraTag(int extra_tag, int top_tag);
285 
286   byte* pos_;
287   byte* last_pc_;
288   intptr_t last_data_;
289   DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
290 };
291 
292 
293 // A RelocIterator iterates over relocation information.
294 // Typical use:
295 //
296 //   for (RelocIterator it(code); !it.done(); it.next()) {
297 //     // do something with it.rinfo() here
298 //   }
299 //
300 // A mask can be specified to skip unwanted modes.
301 class RelocIterator: public Malloced {
302  public:
303   // Create a new iterator positioned at
304   // the beginning of the reloc info.
305   // Relocation information with mode k is included in the
306   // iteration iff bit k of mode_mask is set.
307   explicit RelocIterator(Code* code, int mode_mask = -1);
308   explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);
309 
310   // Iteration
done()311   bool done() const  { return done_; }
312   void next();
313 
314   // Return pointer valid until next next().
rinfo()315   RelocInfo* rinfo() {
316     ASSERT(!done());
317     return &rinfo_;
318   }
319 
320  private:
321   // Advance* moves the position before/after reading.
322   // *Read* reads from current byte(s) into rinfo_.
323   // *Get* just reads and returns info on current byte.
324   void Advance(int bytes = 1) { pos_ -= bytes; }
325   int AdvanceGetTag();
326   int GetExtraTag();
327   int GetTopTag();
328   void ReadTaggedPC();
329   void AdvanceReadPC();
330   void AdvanceReadData();
331   void AdvanceReadVariableLengthPCJump();
332   int GetPositionTypeTag();
333   void ReadTaggedData();
334 
335   static RelocInfo::Mode DebugInfoModeFromTag(int tag);
336 
337   // If the given mode is wanted, set it in rinfo_ and return true.
338   // Else return false. Used for efficiently skipping unwanted modes.
SetMode(RelocInfo::Mode mode)339   bool SetMode(RelocInfo::Mode mode) {
340     return (mode_mask_ & 1 << mode) ? (rinfo_.rmode_ = mode, true) : false;
341   }
342 
343   byte* pos_;
344   byte* end_;
345   RelocInfo rinfo_;
346   bool done_;
347   int mode_mask_;
348   DISALLOW_COPY_AND_ASSIGN(RelocIterator);
349 };
350 
351 
352 //------------------------------------------------------------------------------
353 // External function
354 
355 //----------------------------------------------------------------------------
356 class IC_Utility;
357 class SCTableReference;
358 #ifdef ENABLE_DEBUGGER_SUPPORT
359 class Debug_Address;
360 #endif
361 
362 
363 typedef void* ExternalReferenceRedirector(void* original, bool fp_return);
364 
365 
366 // An ExternalReference represents a C++ address used in the generated
367 // code. All references to C++ functions and variables must be encapsulated in
368 // an ExternalReference instance. This is done in order to track the origin of
369 // all external references in the code so that they can be bound to the correct
370 // addresses when deserializing a heap.
371 class ExternalReference BASE_EMBEDDED {
372  public:
373   explicit ExternalReference(Builtins::CFunctionId id);
374 
375   explicit ExternalReference(Builtins::Name name);
376 
377   explicit ExternalReference(Runtime::FunctionId id);
378 
379   explicit ExternalReference(Runtime::Function* f);
380 
381   explicit ExternalReference(const IC_Utility& ic_utility);
382 
383 #ifdef ENABLE_DEBUGGER_SUPPORT
384   explicit ExternalReference(const Debug_Address& debug_address);
385 #endif
386 
387   explicit ExternalReference(StatsCounter* counter);
388 
389   explicit ExternalReference(Top::AddressId id);
390 
391   explicit ExternalReference(const SCTableReference& table_ref);
392 
393   // One-of-a-kind references. These references are not part of a general
394   // pattern. This means that they have to be added to the
395   // ExternalReferenceTable in serialize.cc manually.
396 
397   static ExternalReference perform_gc_function();
398   static ExternalReference builtin_passed_function();
399   static ExternalReference random_positive_smi_function();
400 
401   // Static variable Factory::the_hole_value.location()
402   static ExternalReference the_hole_value_location();
403 
404   // Static variable Heap::roots_address()
405   static ExternalReference roots_address();
406 
407   // Static variable StackGuard::address_of_jslimit()
408   static ExternalReference address_of_stack_guard_limit();
409 
410   // Static variable RegExpStack::limit_address()
411   static ExternalReference address_of_regexp_stack_limit();
412 
413   // Static variable Heap::NewSpaceStart()
414   static ExternalReference new_space_start();
415   static ExternalReference heap_always_allocate_scope_depth();
416 
417   // Used for fast allocation in generated code.
418   static ExternalReference new_space_allocation_top_address();
419   static ExternalReference new_space_allocation_limit_address();
420 
421   static ExternalReference double_fp_operation(Token::Value operation);
422   static ExternalReference compare_doubles();
423 
address()424   Address address() const {return reinterpret_cast<Address>(address_);}
425 
426 #ifdef ENABLE_DEBUGGER_SUPPORT
427   // Function Debug::Break()
428   static ExternalReference debug_break();
429 
430   // Used to check if single stepping is enabled in generated code.
431   static ExternalReference debug_step_in_fp_address();
432 #endif
433 
434 #ifdef V8_NATIVE_REGEXP
435   // C functions called from RegExp generated code.
436 
437   // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
438   static ExternalReference re_case_insensitive_compare_uc16();
439 
440   // Function RegExpMacroAssembler*::CheckStackGuardState()
441   static ExternalReference re_check_stack_guard_state();
442 
443   // Function NativeRegExpMacroAssembler::GrowStack()
444   static ExternalReference re_grow_stack();
445 #endif
446 
447   // This lets you register a function that rewrites all external references.
448   // Used by the ARM simulator to catch calls to external references.
set_redirector(ExternalReferenceRedirector * redirector)449   static void set_redirector(ExternalReferenceRedirector* redirector) {
450     ASSERT(redirector_ == NULL);  // We can't stack them.
451     redirector_ = redirector;
452   }
453 
454  private:
ExternalReference(void * address)455   explicit ExternalReference(void* address)
456       : address_(address) {}
457 
458   static ExternalReferenceRedirector* redirector_;
459 
460   static void* Redirect(void* address, bool fp_return = false) {
461     if (redirector_ == NULL) return address;
462     return (*redirector_)(address, fp_return);
463   }
464 
465   static void* Redirect(Address address_arg, bool fp_return = false) {
466     void* address = reinterpret_cast<void*>(address_arg);
467     return redirector_ == NULL ? address : (*redirector_)(address, fp_return);
468   }
469 
470   void* address_;
471 };
472 
473 
474 // -----------------------------------------------------------------------------
475 // Utility functions
476 
is_intn(int x,int n)477 static inline bool is_intn(int x, int n)  {
478   return -(1 << (n-1)) <= x && x < (1 << (n-1));
479 }
480 
is_int24(int x)481 static inline bool is_int24(int x)  { return is_intn(x, 24); }
is_int8(int x)482 static inline bool is_int8(int x)  { return is_intn(x, 8); }
483 
is_uintn(int x,int n)484 static inline bool is_uintn(int x, int n) {
485   return (x & -(1 << n)) == 0;
486 }
487 
is_uint2(int x)488 static inline bool is_uint2(int x)  { return is_uintn(x, 2); }
is_uint3(int x)489 static inline bool is_uint3(int x)  { return is_uintn(x, 3); }
is_uint4(int x)490 static inline bool is_uint4(int x)  { return is_uintn(x, 4); }
is_uint5(int x)491 static inline bool is_uint5(int x)  { return is_uintn(x, 5); }
is_uint6(int x)492 static inline bool is_uint6(int x)  { return is_uintn(x, 6); }
is_uint8(int x)493 static inline bool is_uint8(int x)  { return is_uintn(x, 8); }
is_uint12(int x)494 static inline bool is_uint12(int x)  { return is_uintn(x, 12); }
is_uint16(int x)495 static inline bool is_uint16(int x)  { return is_uintn(x, 16); }
is_uint24(int x)496 static inline bool is_uint24(int x)  { return is_uintn(x, 24); }
497 
498 } }  // namespace v8::internal
499 
500 #endif  // V8_ASSEMBLER_H_
501