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