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