Home | History | Annotate | Download | only in x64
      1 // Copyright 2009 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 #ifndef V8_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
     29 #define V8_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
     30 
     31 namespace v8 {
     32 namespace internal {
     33 
     34 #ifdef V8_NATIVE_REGEXP
     35 
     36 class RegExpMacroAssemblerX64: public NativeRegExpMacroAssembler {
     37  public:
     38   RegExpMacroAssemblerX64(Mode mode, int registers_to_save);
     39   virtual ~RegExpMacroAssemblerX64();
     40   virtual int stack_limit_slack();
     41   virtual void AdvanceCurrentPosition(int by);
     42   virtual void AdvanceRegister(int reg, int by);
     43   virtual void Backtrack();
     44   virtual void Bind(Label* label);
     45   virtual void CheckAtStart(Label* on_at_start);
     46   virtual void CheckCharacter(uint32_t c, Label* on_equal);
     47   virtual void CheckCharacterAfterAnd(uint32_t c,
     48                                       uint32_t mask,
     49                                       Label* on_equal);
     50   virtual void CheckCharacterGT(uc16 limit, Label* on_greater);
     51   virtual void CheckCharacterLT(uc16 limit, Label* on_less);
     52   virtual void CheckCharacters(Vector<const uc16> str,
     53                                int cp_offset,
     54                                Label* on_failure,
     55                                bool check_end_of_string);
     56   // A "greedy loop" is a loop that is both greedy and with a simple
     57   // body. It has a particularly simple implementation.
     58   virtual void CheckGreedyLoop(Label* on_tos_equals_current_position);
     59   virtual void CheckNotAtStart(Label* on_not_at_start);
     60   virtual void CheckNotBackReference(int start_reg, Label* on_no_match);
     61   virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
     62                                                Label* on_no_match);
     63   virtual void CheckNotRegistersEqual(int reg1, int reg2, Label* on_not_equal);
     64   virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal);
     65   virtual void CheckNotCharacterAfterAnd(uint32_t c,
     66                                          uint32_t mask,
     67                                          Label* on_not_equal);
     68   virtual void CheckNotCharacterAfterMinusAnd(uc16 c,
     69                                               uc16 minus,
     70                                               uc16 mask,
     71                                               Label* on_not_equal);
     72   // Checks whether the given offset from the current position is before
     73   // the end of the string.
     74   virtual void CheckPosition(int cp_offset, Label* on_outside_input);
     75   virtual bool CheckSpecialCharacterClass(uc16 type,
     76                                           Label* on_no_match);
     77   virtual void Fail();
     78   virtual Handle<Object> GetCode(Handle<String> source);
     79   virtual void GoTo(Label* label);
     80   virtual void IfRegisterGE(int reg, int comparand, Label* if_ge);
     81   virtual void IfRegisterLT(int reg, int comparand, Label* if_lt);
     82   virtual void IfRegisterEqPos(int reg, Label* if_eq);
     83   virtual IrregexpImplementation Implementation();
     84   virtual void LoadCurrentCharacter(int cp_offset,
     85                                     Label* on_end_of_input,
     86                                     bool check_bounds = true,
     87                                     int characters = 1);
     88   virtual void PopCurrentPosition();
     89   virtual void PopRegister(int register_index);
     90   virtual void PushBacktrack(Label* label);
     91   virtual void PushCurrentPosition();
     92   virtual void PushRegister(int register_index,
     93                             StackCheckFlag check_stack_limit);
     94   virtual void ReadCurrentPositionFromRegister(int reg);
     95   virtual void ReadStackPointerFromRegister(int reg);
     96   virtual void SetRegister(int register_index, int to);
     97   virtual void Succeed();
     98   virtual void WriteCurrentPositionToRegister(int reg, int cp_offset);
     99   virtual void ClearRegisters(int reg_from, int reg_to);
    100   virtual void WriteStackPointerToRegister(int reg);
    101 
    102   static Result Match(Handle<Code> regexp,
    103                       Handle<String> subject,
    104                       int* offsets_vector,
    105                       int offsets_vector_length,
    106                       int previous_index);
    107 
    108   static Result Execute(Code* code,
    109                         String* input,
    110                         int start_offset,
    111                         const byte* input_start,
    112                         const byte* input_end,
    113                         int* output,
    114                         bool at_start);
    115 
    116   // Called from RegExp if the stack-guard is triggered.
    117   // If the code object is relocated, the return address is fixed before
    118   // returning.
    119   static int CheckStackGuardState(Address* return_address,
    120                                   Code* re_code,
    121                                   Address re_frame);
    122 
    123  private:
    124   // Offsets from rbp of function parameters and stored registers.
    125   static const int kFramePointer = 0;
    126   // Above the frame pointer - function parameters and return address.
    127   static const int kReturn_eip = kFramePointer + kPointerSize;
    128   static const int kFrameAlign = kReturn_eip + kPointerSize;
    129 
    130 #ifdef _WIN64
    131   // Parameters (first four passed as registers, but with room on stack).
    132   // In Microsoft 64-bit Calling Convention, there is room on the callers
    133   // stack (before the return address) to spill parameter registers. We
    134   // use this space to store the register passed parameters.
    135   static const int kInputString = kFrameAlign;
    136   // StartIndex is passed as 32 bit int.
    137   static const int kStartIndex = kInputString + kPointerSize;
    138   static const int kInputStart = kStartIndex + kPointerSize;
    139   static const int kInputEnd = kInputStart + kPointerSize;
    140   static const int kRegisterOutput = kInputEnd + kPointerSize;
    141   static const int kStackHighEnd = kRegisterOutput + kPointerSize;
    142   // DirectCall is passed as 32 bit int (values 0 or 1).
    143   static const int kDirectCall = kStackHighEnd + kPointerSize;
    144 #else
    145   // In AMD64 ABI Calling Convention, the first six integer parameters
    146   // are passed as registers, and caller must allocate space on the stack
    147   // if it wants them stored. We push the parameters after the frame pointer.
    148   static const int kInputString = kFramePointer - kPointerSize;
    149   static const int kStartIndex = kInputString - kPointerSize;
    150   static const int kInputStart = kStartIndex - kPointerSize;
    151   static const int kInputEnd = kInputStart - kPointerSize;
    152   static const int kRegisterOutput = kInputEnd - kPointerSize;
    153   static const int kStackHighEnd = kRegisterOutput - kPointerSize;
    154   static const int kDirectCall = kFrameAlign;
    155 #endif
    156 
    157 #ifdef _WIN64
    158   // Microsoft calling convention has three callee-saved registers
    159   // (that we are using). We push these after the frame pointer.
    160   static const int kBackup_rsi = kFramePointer - kPointerSize;
    161   static const int kBackup_rdi = kBackup_rsi - kPointerSize;
    162   static const int kBackup_rbx = kBackup_rdi - kPointerSize;
    163   static const int kLastCalleeSaveRegister = kBackup_rbx;
    164 #else
    165   // AMD64 Calling Convention has only one callee-save register that
    166   // we use. We push this after the frame pointer (and after the
    167   // parameters).
    168   static const int kBackup_rbx = kStackHighEnd - kPointerSize;
    169   static const int kLastCalleeSaveRegister = kBackup_rbx;
    170 #endif
    171 
    172   // When adding local variables remember to push space for them in
    173   // the frame in GetCode.
    174   static const int kInputStartMinusOne =
    175       kLastCalleeSaveRegister - kPointerSize;
    176   static const int kAtStart = kInputStartMinusOne - kPointerSize;
    177 
    178   // First register address. Following registers are below it on the stack.
    179   static const int kRegisterZero = kAtStart - kPointerSize;
    180 
    181   // Initial size of code buffer.
    182   static const size_t kRegExpCodeSize = 1024;
    183 
    184   // Load a number of characters at the given offset from the
    185   // current position, into the current-character register.
    186   void LoadCurrentCharacterUnchecked(int cp_offset, int character_count);
    187 
    188   // Check whether preemption has been requested.
    189   void CheckPreemption();
    190 
    191   // Check whether we are exceeding the stack limit on the backtrack stack.
    192   void CheckStackLimit();
    193 
    194   // Generate a call to CheckStackGuardState.
    195   void CallCheckStackGuardState();
    196 
    197   // The rbp-relative location of a regexp register.
    198   Operand register_location(int register_index);
    199 
    200   // The register containing the current character after LoadCurrentCharacter.
    201   inline Register current_character() { return rdx; }
    202 
    203   // The register containing the backtrack stack top. Provides a meaningful
    204   // name to the register.
    205   inline Register backtrack_stackpointer() { return rcx; }
    206 
    207   // The registers containing a self pointer to this code's Code object.
    208   inline Register code_object_pointer() { return r8; }
    209 
    210   // Byte size of chars in the string to match (decided by the Mode argument)
    211   inline int char_size() { return static_cast<int>(mode_); }
    212 
    213   // Equivalent to a conditional branch to the label, unless the label
    214   // is NULL, in which case it is a conditional Backtrack.
    215   void BranchOrBacktrack(Condition condition, Label* to);
    216 
    217   void MarkPositionForCodeRelativeFixup() {
    218     code_relative_fixup_positions_.Add(masm_->pc_offset());
    219   }
    220 
    221   void FixupCodeRelativePositions();
    222 
    223   // Call and return internally in the generated code in a way that
    224   // is GC-safe (i.e., doesn't leave absolute code addresses on the stack)
    225   inline void SafeCall(Label* to);
    226   inline void SafeCallTarget(Label* label);
    227   inline void SafeReturn();
    228 
    229   // Pushes the value of a register on the backtrack stack. Decrements the
    230   // stack pointer (rcx) by a word size and stores the register's value there.
    231   inline void Push(Register source);
    232 
    233   // Pushes a value on the backtrack stack. Decrements the stack pointer (rcx)
    234   // by a word size and stores the value there.
    235   inline void Push(Immediate value);
    236 
    237   // Pushes the Code object relative offset of a label on the backtrack stack
    238   // (i.e., a backtrack target). Decrements the stack pointer (rcx)
    239   // by a word size and stores the value there.
    240   inline void Push(Label* label);
    241 
    242   // Pops a value from the backtrack stack. Reads the word at the stack pointer
    243   // (rcx) and increments it by a word size.
    244   inline void Pop(Register target);
    245 
    246   // Drops the top value from the backtrack stack without reading it.
    247   // Increments the stack pointer (rcx) by a word size.
    248   inline void Drop();
    249 
    250   MacroAssembler* masm_;
    251 
    252   ZoneList<int> code_relative_fixup_positions_;
    253 
    254   // Which mode to generate code for (ASCII or UC16).
    255   Mode mode_;
    256 
    257   // One greater than maximal register index actually used.
    258   int num_registers_;
    259 
    260   // Number of registers to output at the end (the saved registers
    261   // are always 0..num_saved_registers_-1)
    262   int num_saved_registers_;
    263 
    264   // Labels used internally.
    265   Label entry_label_;
    266   Label start_label_;
    267   Label success_label_;
    268   Label backtrack_label_;
    269   Label exit_label_;
    270   Label check_preempt_label_;
    271   Label stack_overflow_label_;
    272 };
    273 
    274 #endif  // V8_NATIVE_REGEXP
    275 
    276 }}  // namespace v8::internal
    277 
    278 #endif  // V8_X64_REGEXP_MACRO_ASSEMBLER_X64_H_
    279