1 // Copyright 2008-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_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_ 29 #define V8_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_ 30 31 namespace v8 { 32 namespace internal { 33 34 #ifndef V8_NATIVE_REGEXP 35 class RegExpMacroAssemblerIA32: public RegExpMacroAssembler { 36 public: RegExpMacroAssemblerIA32()37 RegExpMacroAssemblerIA32() { } ~RegExpMacroAssemblerIA32()38 virtual ~RegExpMacroAssemblerIA32() { } 39 }; 40 41 #else 42 class RegExpMacroAssemblerIA32: public NativeRegExpMacroAssembler { 43 public: 44 RegExpMacroAssemblerIA32(Mode mode, int registers_to_save); 45 virtual ~RegExpMacroAssemblerIA32(); 46 virtual int stack_limit_slack(); 47 virtual void AdvanceCurrentPosition(int by); 48 virtual void AdvanceRegister(int reg, int by); 49 virtual void Backtrack(); 50 virtual void Bind(Label* label); 51 virtual void CheckAtStart(Label* on_at_start); 52 virtual void CheckCharacter(uint32_t c, Label* on_equal); 53 virtual void CheckCharacterAfterAnd(uint32_t c, 54 uint32_t mask, 55 Label* on_equal); 56 virtual void CheckCharacterGT(uc16 limit, Label* on_greater); 57 virtual void CheckCharacterLT(uc16 limit, Label* on_less); 58 virtual void CheckCharacters(Vector<const uc16> str, 59 int cp_offset, 60 Label* on_failure, 61 bool check_end_of_string); 62 // A "greedy loop" is a loop that is both greedy and with a simple 63 // body. It has a particularly simple implementation. 64 virtual void CheckGreedyLoop(Label* on_tos_equals_current_position); 65 virtual void CheckNotAtStart(Label* on_not_at_start); 66 virtual void CheckNotBackReference(int start_reg, Label* on_no_match); 67 virtual void CheckNotBackReferenceIgnoreCase(int start_reg, 68 Label* on_no_match); 69 virtual void CheckNotRegistersEqual(int reg1, int reg2, Label* on_not_equal); 70 virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal); 71 virtual void CheckNotCharacterAfterAnd(uint32_t c, 72 uint32_t mask, 73 Label* on_not_equal); 74 virtual void CheckNotCharacterAfterMinusAnd(uc16 c, 75 uc16 minus, 76 uc16 mask, 77 Label* on_not_equal); 78 // Checks whether the given offset from the current position is before 79 // the end of the string. 80 virtual void CheckPosition(int cp_offset, Label* on_outside_input); 81 virtual bool CheckSpecialCharacterClass(uc16 type, 82 int cp_offset, 83 bool check_offset, 84 Label* on_no_match); 85 virtual void Fail(); 86 virtual Handle<Object> GetCode(Handle<String> source); 87 virtual void GoTo(Label* label); 88 virtual void IfRegisterGE(int reg, int comparand, Label* if_ge); 89 virtual void IfRegisterLT(int reg, int comparand, Label* if_lt); 90 virtual void IfRegisterEqPos(int reg, Label* if_eq); 91 virtual IrregexpImplementation Implementation(); 92 virtual void LoadCurrentCharacter(int cp_offset, 93 Label* on_end_of_input, 94 bool check_bounds = true, 95 int characters = 1); 96 virtual void PopCurrentPosition(); 97 virtual void PopRegister(int register_index); 98 virtual void PushBacktrack(Label* label); 99 virtual void PushCurrentPosition(); 100 virtual void PushRegister(int register_index, 101 StackCheckFlag check_stack_limit); 102 virtual void ReadCurrentPositionFromRegister(int reg); 103 virtual void ReadStackPointerFromRegister(int reg); 104 virtual void SetRegister(int register_index, int to); 105 virtual void Succeed(); 106 virtual void WriteCurrentPositionToRegister(int reg, int cp_offset); 107 virtual void ClearRegisters(int reg_from, int reg_to); 108 virtual void WriteStackPointerToRegister(int reg); 109 110 // Called from RegExp if the stack-guard is triggered. 111 // If the code object is relocated, the return address is fixed before 112 // returning. 113 static int CheckStackGuardState(Address* return_address, 114 Code* re_code, 115 Address re_frame); 116 117 private: 118 // Offsets from ebp of function parameters and stored registers. 119 static const int kFramePointer = 0; 120 // Above the frame pointer - function parameters and return address. 121 static const int kReturn_eip = kFramePointer + kPointerSize; 122 static const int kFrameAlign = kReturn_eip + kPointerSize; 123 // Parameters. 124 static const int kInputString = kFrameAlign; 125 static const int kStartIndex = kInputString + kPointerSize; 126 static const int kInputStart = kStartIndex + kPointerSize; 127 static const int kInputEnd = kInputStart + kPointerSize; 128 static const int kRegisterOutput = kInputEnd + kPointerSize; 129 static const int kAtStart = kRegisterOutput + kPointerSize; 130 static const int kStackHighEnd = kAtStart + kPointerSize; 131 // Below the frame pointer - local stack variables. 132 // When adding local variables remember to push space for them in 133 // the frame in GetCode. 134 static const int kBackup_esi = kFramePointer - kPointerSize; 135 static const int kBackup_edi = kBackup_esi - kPointerSize; 136 static const int kBackup_ebx = kBackup_edi - kPointerSize; 137 static const int kInputStartMinusOne = kBackup_ebx - kPointerSize; 138 // First register address. Following registers are below it on the stack. 139 static const int kRegisterZero = kInputStartMinusOne - kPointerSize; 140 141 // Initial size of code buffer. 142 static const size_t kRegExpCodeSize = 1024; 143 144 // Load a number of characters at the given offset from the 145 // current position, into the current-character register. 146 void LoadCurrentCharacterUnchecked(int cp_offset, int character_count); 147 148 // Check whether preemption has been requested. 149 void CheckPreemption(); 150 151 // Check whether we are exceeding the stack limit on the backtrack stack. 152 void CheckStackLimit(); 153 154 // Generate a call to CheckStackGuardState. 155 void CallCheckStackGuardState(Register scratch); 156 157 // The ebp-relative location of a regexp register. 158 Operand register_location(int register_index); 159 160 // The register containing the current character after LoadCurrentCharacter. 161 inline Register current_character() { return edx; } 162 163 // The register containing the backtrack stack top. Provides a meaningful 164 // name to the register. 165 inline Register backtrack_stackpointer() { return ecx; } 166 167 // Byte size of chars in the string to match (decided by the Mode argument) 168 inline int char_size() { return static_cast<int>(mode_); } 169 170 // Equivalent to a conditional branch to the label, unless the label 171 // is NULL, in which case it is a conditional Backtrack. 172 void BranchOrBacktrack(Condition condition, Label* to, Hint hint = no_hint); 173 174 // Call and return internally in the generated code in a way that 175 // is GC-safe (i.e., doesn't leave absolute code addresses on the stack) 176 inline void SafeCall(Label* to); 177 inline void SafeReturn(); 178 inline void SafeCallTarget(Label* name); 179 180 // Pushes the value of a register on the backtrack stack. Decrements the 181 // stack pointer (ecx) by a word size and stores the register's value there. 182 inline void Push(Register source); 183 184 // Pushes a value on the backtrack stack. Decrements the stack pointer (ecx) 185 // by a word size and stores the value there. 186 inline void Push(Immediate value); 187 188 // Pops a value from the backtrack stack. Reads the word at the stack pointer 189 // (ecx) and increments it by a word size. 190 inline void Pop(Register target); 191 192 // Before calling a C-function from generated code, align arguments on stack. 193 // After aligning the frame, arguments must be stored in esp[0], esp[4], 194 // etc., not pushed. The argument count assumes all arguments are word sized. 195 // Some compilers/platforms require the stack to be aligned when calling 196 // C++ code. 197 // Needs a scratch register to do some arithmetic. This register will be 198 // trashed. 199 inline void FrameAlign(int num_arguments, Register scratch); 200 201 // Calls a C function and cleans up the space for arguments allocated 202 // by FrameAlign. The called function is not allowed to trigger a garbage 203 // collection, since that might move the code and invalidate the return 204 // address (unless this is somehow accounted for). 205 inline void CallCFunction(ExternalReference function, int num_arguments); 206 207 MacroAssembler* masm_; 208 209 // Which mode to generate code for (ASCII or UC16). 210 Mode mode_; 211 212 // One greater than maximal register index actually used. 213 int num_registers_; 214 215 // Number of registers to output at the end (the saved registers 216 // are always 0..num_saved_registers_-1) 217 int num_saved_registers_; 218 219 // Labels used internally. 220 Label entry_label_; 221 Label start_label_; 222 Label success_label_; 223 Label backtrack_label_; 224 Label exit_label_; 225 Label check_preempt_label_; 226 Label stack_overflow_label_; 227 }; 228 #endif // V8_NATIVE_REGEXP 229 230 }} // namespace v8::internal 231 232 #endif // V8_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_ 233