assembler.h 26.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
// Copyright (c) 1994-2006 Sun Microsystems Inc.
// All Rights Reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// - Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// - Redistribution in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// - Neither the name of Sun Microsystems or the names of contributors may
// be used to endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// The original source code covered by the above license above has been
// modified significantly by Google Inc.
33
// Copyright 2006-2009 the V8 project authors. All rights reserved.
34 35 36 37

#ifndef V8_ASSEMBLER_H_
#define V8_ASSEMBLER_H_

38
#include "gdb-jit.h"
39 40
#include "runtime.h"
#include "top.h"
41
#include "token.h"
42

43 44
namespace v8 {
namespace internal {
45 46


47 48 49 50 51 52 53
// -----------------------------------------------------------------------------
// Common double constants.

class DoubleConstant: public AllStatic {
 public:
  static const double min_int;
  static const double one_half;
54
  static const double minus_zero;
55
  static const double negative_infinity;
56 57 58
};


59 60 61 62 63 64
// -----------------------------------------------------------------------------
// Labels represent pc locations; they are typically jump or call targets.
// After declaration, a label can be freely used to denote known or (yet)
// unknown pc location. Assembler::bind() is used to bind a label to the
// current pc. A label can be bound only once.

65
class Label BASE_EMBEDDED {
66
 public:
67
  INLINE(Label())                 { Unuse(); }
68 69 70 71
  INLINE(~Label())                { ASSERT(!is_linked()); }

  INLINE(void Unuse())            { pos_ = 0; }

72
  INLINE(bool is_bound() const)  { return pos_ <  0; }
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  INLINE(bool is_unused() const)  { return pos_ == 0; }
  INLINE(bool is_linked() const)  { return pos_ >  0; }

  // Returns the position of bound or linked labels. Cannot be used
  // for unused labels.
  int pos() const;

 private:
  // pos_ encodes both the binding state (via its sign)
  // and the binding position (via its value) of a label.
  //
  // pos_ <  0  bound label, pos() returns the jump target position
  // pos_ == 0  unused label
  // pos_ >  0  linked label, pos() returns the last reference position
  int pos_;

  void bind_to(int pos)  {
    pos_ = -pos - 1;
    ASSERT(is_bound());
  }
  void link_to(int pos)  {
    pos_ =  pos + 1;
    ASSERT(is_linked());
  }

  friend class Assembler;
99
  friend class RegexpAssembler;
100
  friend class Displacement;
101
  friend class ShadowTarget;
102
  friend class RegExpMacroAssemblerIrregexp;
103 104 105
};


106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
// -----------------------------------------------------------------------------
// NearLabels are labels used for short jumps (in Intel jargon).
// NearLabels should be used if it can be guaranteed that the jump range is
// within -128 to +127. We already use short jumps when jumping backwards,
// so using a NearLabel will only have performance impact if used for forward
// jumps.
class NearLabel BASE_EMBEDDED {
 public:
  NearLabel() { Unuse(); }
  ~NearLabel() { ASSERT(!is_linked()); }

  void Unuse() {
    pos_ = -1;
    unresolved_branches_ = 0;
#ifdef DEBUG
    for (int i = 0; i < kMaxUnresolvedBranches; i++) {
      unresolved_positions_[i] = -1;
    }
#endif
  }

  int pos() {
    ASSERT(is_bound());
    return pos_;
  }

  bool is_bound() { return pos_ >= 0; }
  bool is_linked() { return !is_bound() && unresolved_branches_ > 0; }
  bool is_unused() { return !is_bound() && unresolved_branches_ == 0; }

  void bind_to(int position) {
    ASSERT(!is_bound());
    pos_ = position;
  }

  void link_to(int position) {
    ASSERT(!is_bound());
    ASSERT(unresolved_branches_ < kMaxUnresolvedBranches);
    unresolved_positions_[unresolved_branches_++] = position;
  }

 private:
  static const int kMaxUnresolvedBranches = 8;
  int pos_;
  int unresolved_branches_;
  int unresolved_positions_[kMaxUnresolvedBranches];

  friend class Assembler;
};


157 158 159
// -----------------------------------------------------------------------------
// Relocation information

kasperl@chromium.org's avatar
kasperl@chromium.org committed
160

161 162 163 164 165 166 167 168 169
// Relocation information consists of the address (pc) of the datum
// to which the relocation information applies, the relocation mode
// (rmode), and an optional data field. The relocation mode may be
// "descriptive" and not indicate a need for relocation, but simply
// describe a property of the datum. Such rmodes are useful for GC
// and nice disassembly output.

class RelocInfo BASE_EMBEDDED {
 public:
170 171 172 173 174 175 176 177 178 179 180
  // The constant kNoPosition is used with the collecting of source positions
  // in the relocation information. Two types of source positions are collected
  // "position" (RelocMode position) and "statement position" (RelocMode
  // statement_position). The "position" is collected at places in the source
  // code which are of interest when making stack traces to pin-point the source
  // location of a stack frame as close as possible. The "statement position" is
  // collected at the beginning at each statement, and is used to indicate
  // possible break locations. kNoPosition is used to indicate an
  // invalid/uninitialized position value.
  static const int kNoPosition = -1;

181 182 183 184 185 186
  // This string is used to add padding comments to the reloc info in cases
  // where we are not sure to have enough space for patching in during
  // lazy deoptimization. This is the case if we have indirect calls for which
  // we do not normally record relocation info.
  static const char* kFillerCommentString;

187 188
  // The minimum size of a comment is equal to three bytes for the extra tagged
  // pc + the tag for the data, and kPointerSize for the actual pointer to the
189
  // comment.
190
  static const int kMinRelocCommentSize = 3 + kPointerSize;
191 192 193 194

  // The maximum size for a call instruction including pc-jump.
  static const int kMaxCallSize = 6;

195 196 197
  enum Mode {
    // Please note the order is important (see IsCodeTarget, IsGCRelocMode).
    CONSTRUCT_CALL,  // code target that is a call to a JavaScript constructor.
198
    CODE_TARGET_CONTEXT,  // Code target used for contextual loads and stores.
199 200
    DEBUG_BREAK,  // Code target for the debugger statement.
    CODE_TARGET,  // Code target which is not any of the above.
201
    EMBEDDED_OBJECT,
202 203
    GLOBAL_PROPERTY_CELL,

204 205 206 207 208 209
    // Everything after runtime_entry (inclusive) is not GC'ed.
    RUNTIME_ENTRY,
    JS_RETURN,  // Marks start of the ExitJSFrame code.
    COMMENT,
    POSITION,  // See comment for kNoPosition above.
    STATEMENT_POSITION,  // See comment for kNoPosition above.
210
    DEBUG_BREAK_SLOT,  // Additional code inserted for debug break slot.
211 212 213 214 215 216 217 218
    EXTERNAL_REFERENCE,  // The address of an external C++ function.
    INTERNAL_REFERENCE,  // An address inside the same function.

    // add more as needed
    // Pseudo-types
    NUMBER_OF_MODES,  // must be no greater than 14 - see RelocInfoWriter
    NONE,  // never recorded
    LAST_CODE_ENUM = CODE_TARGET,
219
    LAST_GCED_ENUM = GLOBAL_PROPERTY_CELL
220 221 222
  };


223
  RelocInfo() {}
224
  RelocInfo(byte* pc, Mode rmode, intptr_t data)
225 226 227
      : pc_(pc), rmode_(rmode), data_(data) {
  }

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
  static inline bool IsConstructCall(Mode mode) {
    return mode == CONSTRUCT_CALL;
  }
  static inline bool IsCodeTarget(Mode mode) {
    return mode <= LAST_CODE_ENUM;
  }
  // Is the relocation mode affected by GC?
  static inline bool IsGCRelocMode(Mode mode) {
    return mode <= LAST_GCED_ENUM;
  }
  static inline bool IsJSReturn(Mode mode) {
    return mode == JS_RETURN;
  }
  static inline bool IsComment(Mode mode) {
    return mode == COMMENT;
  }
  static inline bool IsPosition(Mode mode) {
    return mode == POSITION || mode == STATEMENT_POSITION;
  }
  static inline bool IsStatementPosition(Mode mode) {
    return mode == STATEMENT_POSITION;
  }
  static inline bool IsExternalReference(Mode mode) {
    return mode == EXTERNAL_REFERENCE;
  }
  static inline bool IsInternalReference(Mode mode) {
    return mode == INTERNAL_REFERENCE;
  }
256 257 258
  static inline bool IsDebugBreakSlot(Mode mode) {
    return mode == DEBUG_BREAK_SLOT;
  }
259 260
  static inline int ModeMask(Mode mode) { return 1 << mode; }

261
  // Accessors
262
  byte* pc() const { return pc_; }
263
  void set_pc(byte* pc) { pc_ = pc; }
264
  Mode rmode() const {  return rmode_; }
265
  intptr_t data() const { return data_; }
266 267

  // Apply a relocation by delta bytes
268
  INLINE(void apply(intptr_t delta));
269

270 271 272 273 274
  // Is the pointer this relocation info refers to coded like a plain pointer
  // or is it strange in some way (eg relative or patched into a series of
  // instructions).
  bool IsCodedSpecially();

275 276 277
  // Read/modify the code target in the branch/call instruction
  // this relocation applies to;
  // can only be called if IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY
278 279 280
  INLINE(Address target_address());
  INLINE(void set_target_address(Address target));
  INLINE(Object* target_object());
281
  INLINE(Handle<Object> target_object_handle(Assembler* origin));
282 283
  INLINE(Object** target_object_address());
  INLINE(void set_target_object(Object* target));
284 285 286 287
  INLINE(JSGlobalPropertyCell* target_cell());
  INLINE(Handle<JSGlobalPropertyCell> target_cell_handle());
  INLINE(void set_target_cell(JSGlobalPropertyCell* cell));

288

289 290 291 292 293 294
  // Read the address of the word containing the target_address in an
  // instruction stream.  What this means exactly is architecture-independent.
  // The only architecture-independent user of this function is the serializer.
  // The serializer uses it to find out how many raw bytes of instruction to
  // output before the next target.  Architecture-independent code shouldn't
  // dereference the pointer it gets back from this.
295
  INLINE(Address target_address_address());
296 297 298 299 300 301 302 303 304 305
  // This indicates how much space a target takes up when deserializing a code
  // stream.  For most architectures this is just the size of a pointer.  For
  // an instruction like movw/movt where the target bits are mixed into the
  // instruction bits the size of the target will be zero, indicating that the
  // serializer should not step forwards in memory after a target is resolved
  // and written.  In this case the target_address_address function above
  // should return the end of the instructions to be patched, allowing the
  // deserializer to deserialize the instructions as raw bytes and put them in
  // place, ready to be patched with the target.
  INLINE(int target_address_size());
306

307 308 309 310 311 312 313 314 315 316 317
  // Read/modify the reference in the instruction this relocation
  // applies to; can only be called if rmode_ is external_reference
  INLINE(Address* target_reference_address());

  // Read/modify the address of a call instruction. This is used to relocate
  // the break points where straight-line code is patched with a call
  // instruction.
  INLINE(Address call_address());
  INLINE(void set_call_address(Address target));
  INLINE(Object* call_object());
  INLINE(void set_call_object(Object* target));
318
  INLINE(Object** call_object_address());
319

320
  template<typename StaticVisitor> inline void Visit();
321 322
  inline void Visit(ObjectVisitor* v);

323
  // Patch the code with some other code.
324
  void PatchCode(byte* instructions, int instruction_count);
325 326

  // Patch the code with a call.
327
  void PatchCodeWithCall(Address target, int guard_bytes);
328 329 330 331

  // Check whether this return sequence has been patched
  // with a call to the debugger.
  INLINE(bool IsPatchedReturnSequence());
332

333 334 335 336
  // Check whether this debug break slot has been patched with a call to the
  // debugger.
  INLINE(bool IsPatchedDebugBreakSlotSequence());

337 338
#ifdef ENABLE_DISASSEMBLER
  // Printing
339
  static const char* RelocModeName(Mode rmode);
340
  void Print(FILE* out);
341
#endif  // ENABLE_DISASSEMBLER
342 343 344 345 346
#ifdef DEBUG
  // Debugging
  void Verify();
#endif

347 348 349
  static const int kCodeTargetMask = (1 << (LAST_CODE_ENUM + 1)) - 1;
  static const int kPositionMask = 1 << POSITION | 1 << STATEMENT_POSITION;
  static const int kDebugMask = kPositionMask | 1 << COMMENT;
350 351 352 353 354 355 356 357
  static const int kApplyMask;  // Modes affected by apply. Depends on arch.

 private:
  // On ARM, note that pc_ is the address of the constant pool entry
  // to be relocated and not the address of the instruction
  // referencing the constant pool entry (except when rmode_ ==
  // comment).
  byte* pc_;
358
  Mode rmode_;
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
  intptr_t data_;
  friend class RelocIterator;
};


// RelocInfoWriter serializes a stream of relocation info. It writes towards
// lower addresses.
class RelocInfoWriter BASE_EMBEDDED {
 public:
  RelocInfoWriter() : pos_(NULL), last_pc_(NULL), last_data_(0) {}
  RelocInfoWriter(byte* pos, byte* pc) : pos_(pos), last_pc_(pc),
                                         last_data_(0) {}

  byte* pos() const { return pos_; }
  byte* last_pc() const { return last_pc_; }

  void Write(const RelocInfo* rinfo);

  // Update the state of the stream after reloc info buffer
  // and/or code is moved while the stream is active.
  void Reposition(byte* pos, byte* pc) {
    pos_ = pos;
    last_pc_ = pc;
  }

384 385 386 387 388 389
  // Max size (bytes) of a written RelocInfo. Longest encoding is
  // ExtraTag, VariableLengthPCJump, ExtraTag, pc_delta, ExtraTag, data_delta.
  // On ia32 and arm this is 1 + 4 + 1 + 1 + 1 + 4 = 12.
  // On x64 this is 1 + 4 + 1 + 1 + 1 + 8 == 16;
  // Here we use the maximum of the two.
  static const int kMaxSize = 16;
390 391 392 393 394

 private:
  inline uint32_t WriteVariableLengthPCJump(uint32_t pc_delta);
  inline void WriteTaggedPC(uint32_t pc_delta, int tag);
  inline void WriteExtraTaggedPC(uint32_t pc_delta, int extra_tag);
395 396
  inline void WriteExtraTaggedData(intptr_t data_delta, int top_tag);
  inline void WriteTaggedData(intptr_t data_delta, int tag);
397 398 399 400 401
  inline void WriteExtraTag(int extra_tag, int top_tag);

  byte* pos_;
  byte* last_pc_;
  intptr_t last_data_;
402
  DISALLOW_COPY_AND_ASSIGN(RelocInfoWriter);
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
};


// A RelocIterator iterates over relocation information.
// Typical use:
//
//   for (RelocIterator it(code); !it.done(); it.next()) {
//     // do something with it.rinfo() here
//   }
//
// A mask can be specified to skip unwanted modes.
class RelocIterator: public Malloced {
 public:
  // Create a new iterator positioned at
  // the beginning of the reloc info.
  // Relocation information with mode k is included in the
  // iteration iff bit k of mode_mask is set.
  explicit RelocIterator(Code* code, int mode_mask = -1);
  explicit RelocIterator(const CodeDesc& desc, int mode_mask = -1);

  // Iteration
424
  bool done() const { return done_; }
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
  void next();

  // Return pointer valid until next next().
  RelocInfo* rinfo() {
    ASSERT(!done());
    return &rinfo_;
  }

 private:
  // Advance* moves the position before/after reading.
  // *Read* reads from current byte(s) into rinfo_.
  // *Get* just reads and returns info on current byte.
  void Advance(int bytes = 1) { pos_ -= bytes; }
  int AdvanceGetTag();
  int GetExtraTag();
  int GetTopTag();
  void ReadTaggedPC();
  void AdvanceReadPC();
  void AdvanceReadData();
  void AdvanceReadVariableLengthPCJump();
  int GetPositionTypeTag();
  void ReadTaggedData();

448
  static RelocInfo::Mode DebugInfoModeFromTag(int tag);
449 450 451

  // If the given mode is wanted, set it in rinfo_ and return true.
  // Else return false. Used for efficiently skipping unwanted modes.
452
  bool SetMode(RelocInfo::Mode mode) {
453
    return (mode_mask_ & (1 << mode)) ? (rinfo_.rmode_ = mode, true) : false;
454 455 456 457 458 459 460
  }

  byte* pos_;
  byte* end_;
  RelocInfo rinfo_;
  bool done_;
  int mode_mask_;
461
  DISALLOW_COPY_AND_ASSIGN(RelocIterator);
462 463 464 465 466 467 468 469 470
};


//------------------------------------------------------------------------------
// External function

//----------------------------------------------------------------------------
class IC_Utility;
class SCTableReference;
471 472 473
#ifdef ENABLE_DEBUGGER_SUPPORT
class Debug_Address;
#endif
474

475 476 477 478 479 480

// An ExternalReference represents a C++ address used in the generated
// code. All references to C++ functions and variables must be encapsulated in
// an ExternalReference instance. This is done in order to track the origin of
// all external references in the code so that they can be bound to the correct
// addresses when deserializing a heap.
481 482
class ExternalReference BASE_EMBEDDED {
 public:
483 484
  // Used in the simulator to support different native api calls.
  enum Type {
485 486
    // Builtin call.
    // MaybeObject* f(v8::internal::Arguments).
487
    BUILTIN_CALL,  // default
488 489 490

    // Builtin call that returns floating point.
    // double f(double, double).
491
    FP_RETURN_CALL,
492 493 494 495 496 497 498 499

    // Direct call to API function callback.
    // Handle<Value> f(v8::Arguments&)
    DIRECT_API_CALL,

    // Direct call to accessor getter callback.
    // Handle<value> f(Local<String> property, AccessorInfo& info)
    DIRECT_GETTER_CALL
500 501 502 503
  };

  typedef void* ExternalReferenceRedirector(void* original, Type type);

504 505
  explicit ExternalReference(Builtins::CFunctionId id);

506
  explicit ExternalReference(ApiFunction* ptr, Type type);
507

508 509 510 511 512 513 514 515
  explicit ExternalReference(Builtins::Name name);

  explicit ExternalReference(Runtime::FunctionId id);

  explicit ExternalReference(Runtime::Function* f);

  explicit ExternalReference(const IC_Utility& ic_utility);

516
#ifdef ENABLE_DEBUGGER_SUPPORT
517
  explicit ExternalReference(const Debug_Address& debug_address);
518
#endif
519 520 521 522 523 524 525 526 527 528 529

  explicit ExternalReference(StatsCounter* counter);

  explicit ExternalReference(Top::AddressId id);

  explicit ExternalReference(const SCTableReference& table_ref);

  // One-of-a-kind references. These references are not part of a general
  // pattern. This means that they have to be added to the
  // ExternalReferenceTable in serialize.cc manually.

530
  static ExternalReference perform_gc_function();
531
  static ExternalReference fill_heap_number_with_random_function();
532
  static ExternalReference random_uint32_function();
533
  static ExternalReference transcendental_cache_array_address();
534
  static ExternalReference delete_handle_scope_extensions();
535

536 537 538 539 540
  // Deoptimization support.
  static ExternalReference new_deoptimizer_function();
  static ExternalReference compute_output_frames_function();
  static ExternalReference global_contexts_list();

541 542 543 544
  // Static data in the keyed lookup cache.
  static ExternalReference keyed_lookup_cache_keys();
  static ExternalReference keyed_lookup_cache_field_offsets();

545 546 547
  // Static variable Factory::the_hole_value.location()
  static ExternalReference the_hole_value_location();

548 549 550
  // Static variable Factory::arguments_marker.location()
  static ExternalReference arguments_marker_location();

551 552 553
  // Static variable Heap::roots_address()
  static ExternalReference roots_address();

554
  // Static variable StackGuard::address_of_jslimit()
555 556 557 558
  static ExternalReference address_of_stack_limit();

  // Static variable StackGuard::address_of_real_jslimit()
  static ExternalReference address_of_real_stack_limit();
559

560 561 562
  // Static variable RegExpStack::limit_address()
  static ExternalReference address_of_regexp_stack_limit();

563 564 565 566 567
  // Static variables for RegExp.
  static ExternalReference address_of_static_offsets_vector();
  static ExternalReference address_of_regexp_stack_memory_address();
  static ExternalReference address_of_regexp_stack_memory_size();

568 569
  // Static variable Heap::NewSpaceStart()
  static ExternalReference new_space_start();
570
  static ExternalReference new_space_mask();
571
  static ExternalReference heap_always_allocate_scope_depth();
572 573 574 575 576

  // Used for fast allocation in generated code.
  static ExternalReference new_space_allocation_top_address();
  static ExternalReference new_space_allocation_limit_address();

577
  static ExternalReference double_fp_operation(Token::Value operation);
578
  static ExternalReference compare_doubles();
579 580
  static ExternalReference power_double_double_function();
  static ExternalReference power_double_int_function();
581

582 583
  static ExternalReference handle_scope_next_address();
  static ExternalReference handle_scope_limit_address();
584
  static ExternalReference handle_scope_level_address();
585 586 587

  static ExternalReference scheduled_exception_address();

588 589 590
  // Static variables containing common double constants.
  static ExternalReference address_of_min_int();
  static ExternalReference address_of_one_half();
591
  static ExternalReference address_of_minus_zero();
592
  static ExternalReference address_of_negative_infinity();
593

594 595 596 597
  static ExternalReference math_sin_double_function();
  static ExternalReference math_cos_double_function();
  static ExternalReference math_log_double_function();

598
  Address address() const {return reinterpret_cast<Address>(address_);}
599

600 601 602 603 604 605 606 607
#ifdef ENABLE_DEBUGGER_SUPPORT
  // Function Debug::Break()
  static ExternalReference debug_break();

  // Used to check if single stepping is enabled in generated code.
  static ExternalReference debug_step_in_fp_address();
#endif

608
#ifndef V8_INTERPRETED_REGEXP
lrn@chromium.org's avatar
lrn@chromium.org committed
609 610 611 612 613 614 615 616 617 618
  // C functions called from RegExp generated code.

  // Function NativeRegExpMacroAssembler::CaseInsensitiveCompareUC16()
  static ExternalReference re_case_insensitive_compare_uc16();

  // Function RegExpMacroAssembler*::CheckStackGuardState()
  static ExternalReference re_check_stack_guard_state();

  // Function NativeRegExpMacroAssembler::GrowStack()
  static ExternalReference re_grow_stack();
619 620 621 622

  // byte NativeRegExpMacroAssembler::word_character_bitmap
  static ExternalReference re_word_character_map();

lrn@chromium.org's avatar
lrn@chromium.org committed
623 624
#endif

625 626 627 628 629 630 631
  // This lets you register a function that rewrites all external references.
  // Used by the ARM simulator to catch calls to external references.
  static void set_redirector(ExternalReferenceRedirector* redirector) {
    ASSERT(redirector_ == NULL);  // We can't stack them.
    redirector_ = redirector;
  }

632 633
 private:
  explicit ExternalReference(void* address)
634 635 636 637
      : address_(address) {}

  static ExternalReferenceRedirector* redirector_;

638 639
  static void* Redirect(void* address,
                        Type type = ExternalReference::BUILTIN_CALL) {
640
    if (redirector_ == NULL) return address;
641
    void* answer = (*redirector_)(address, type);
642
    return answer;
643 644
  }

645 646
  static void* Redirect(Address address_arg,
                        Type type = ExternalReference::BUILTIN_CALL) {
647
    void* address = reinterpret_cast<void*>(address_arg);
648 649
    void* answer = (redirector_ == NULL) ?
                   address :
650
                   (*redirector_)(address, type);
651
    return answer;
652
  }
653

654
  void* address_;
655 656 657
};


658 659 660
// -----------------------------------------------------------------------------
// Position recording support

661 662 663 664 665 666 667 668 669 670 671 672 673
struct PositionState {
  PositionState() : current_position(RelocInfo::kNoPosition),
                    written_position(RelocInfo::kNoPosition),
                    current_statement_position(RelocInfo::kNoPosition),
                    written_statement_position(RelocInfo::kNoPosition) {}

  int current_position;
  int written_position;

  int current_statement_position;
  int written_statement_position;
};

674 675 676 677

class PositionsRecorder BASE_EMBEDDED {
 public:
  explicit PositionsRecorder(Assembler* assembler)
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
      : assembler_(assembler) {
#ifdef ENABLE_GDB_JIT_INTERFACE
    gdbjit_lineinfo_ = NULL;
#endif
  }

#ifdef ENABLE_GDB_JIT_INTERFACE
  ~PositionsRecorder() {
    delete gdbjit_lineinfo_;
  }

  void StartGDBJITLineInfoRecording() {
    if (FLAG_gdbjit) {
      gdbjit_lineinfo_ = new GDBJITLineInfo();
    }
  }

  GDBJITLineInfo* DetachGDBJITLineInfo() {
    GDBJITLineInfo* lineinfo = gdbjit_lineinfo_;
    gdbjit_lineinfo_ = NULL;  // To prevent deallocation in destructor.
    return lineinfo;
  }
#endif
701 702 703

  // Set current position to pos.
  void RecordPosition(int pos);
704 705 706 707 708 709 710

  // Set current statement position to pos.
  void RecordStatementPosition(int pos);

  // Write recorded positions to relocation information.
  bool WriteRecordedPositions();

711
  int current_position() const { return state_.current_position; }
712

713 714 715
  int current_statement_position() const {
    return state_.current_statement_position;
  }
716 717 718

 private:
  Assembler* assembler_;
719
  PositionState state_;
720 721 722
#ifdef ENABLE_GDB_JIT_INTERFACE
  GDBJITLineInfo* gdbjit_lineinfo_;
#endif
723

724
  friend class PreservePositionScope;
725

726
  DISALLOW_COPY_AND_ASSIGN(PositionsRecorder);
727 728 729
};


730
class PreservePositionScope BASE_EMBEDDED {
731
 public:
732
  explicit PreservePositionScope(PositionsRecorder* positions_recorder)
733
      : positions_recorder_(positions_recorder),
734
        saved_state_(positions_recorder->state_) {}
735

736 737
  ~PreservePositionScope() {
    positions_recorder_->state_ = saved_state_;
738 739 740 741
  }

 private:
  PositionsRecorder* positions_recorder_;
742 743 744
  const PositionState saved_state_;

  DISALLOW_COPY_AND_ASSIGN(PreservePositionScope);
745 746 747
};


748 749 750 751 752 753 754 755
// -----------------------------------------------------------------------------
// Utility functions

static inline bool is_intn(int x, int n)  {
  return -(1 << (n-1)) <= x && x < (1 << (n-1));
}

static inline bool is_int8(int x)  { return is_intn(x, 8); }
756 757 758
static inline bool is_int16(int x)  { return is_intn(x, 16); }
static inline bool is_int18(int x)  { return is_intn(x, 18); }
static inline bool is_int24(int x)  { return is_intn(x, 24); }
759 760 761 762 763

static inline bool is_uintn(int x, int n) {
  return (x & -(1 << n)) == 0;
}

764
static inline bool is_uint2(int x)  { return is_uintn(x, 2); }
765 766 767
static inline bool is_uint3(int x)  { return is_uintn(x, 3); }
static inline bool is_uint4(int x)  { return is_uintn(x, 4); }
static inline bool is_uint5(int x)  { return is_uintn(x, 5); }
768
static inline bool is_uint6(int x)  { return is_uintn(x, 6); }
769
static inline bool is_uint8(int x)  { return is_uintn(x, 8); }
770
static inline bool is_uint10(int x)  { return is_uintn(x, 10); }
771 772 773
static inline bool is_uint12(int x)  { return is_uintn(x, 12); }
static inline bool is_uint16(int x)  { return is_uintn(x, 16); }
static inline bool is_uint24(int x)  { return is_uintn(x, 24); }
774 775 776 777 778 779 780 781 782 783
static inline bool is_uint26(int x)  { return is_uintn(x, 26); }
static inline bool is_uint28(int x)  { return is_uintn(x, 28); }

static inline int NumberOfBitsSet(uint32_t x) {
  unsigned int num_bits_set;
  for (num_bits_set = 0; x; x >>= 1) {
    num_bits_set += x & 1;
  }
  return num_bits_set;
}
784

785 786 787 788
// Computes pow(x, y) with the special cases in the spec for Math.pow.
double power_double_int(double x, int y);
double power_double_double(double x, double y);

789 790 791
} }  // namespace v8::internal

#endif  // V8_ASSEMBLER_H_