blob: 8dd52a48473a0d71d6219574924d3a12a1f3385b [file] [log] [blame]
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +00001// Copyright 2011 the V8 project authors. All rights reserved.
lrn@chromium.org7516f052011-03-30 08:52:27 +00002// 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
29#ifndef V8_MIPS_REGEXP_MACRO_ASSEMBLER_MIPS_H_
30#define V8_MIPS_REGEXP_MACRO_ASSEMBLER_MIPS_H_
31
sgjesse@chromium.org6db88712011-07-11 11:41:22 +000032#include "mips/assembler-mips.h"
33#include "mips/assembler-mips-inl.h"
34#include "macro-assembler.h"
35#include "code.h"
36#include "mips/macro-assembler-mips.h"
37
lrn@chromium.org7516f052011-03-30 08:52:27 +000038namespace v8 {
39namespace internal {
40
yangguo@chromium.org5a11aaf2012-06-20 11:29:00 +000041#ifndef V8_INTERPRETED_REGEXP
lrn@chromium.org7516f052011-03-30 08:52:27 +000042class RegExpMacroAssemblerMIPS: public NativeRegExpMacroAssembler {
43 public:
mmassi@chromium.org7028c052012-06-13 11:51:58 +000044 RegExpMacroAssemblerMIPS(Mode mode, int registers_to_save, Zone* zone);
lrn@chromium.org7516f052011-03-30 08:52:27 +000045 virtual ~RegExpMacroAssemblerMIPS();
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);
lrn@chromium.org7516f052011-03-30 08:52:27 +000069 virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal);
70 virtual void CheckNotCharacterAfterAnd(uint32_t c,
71 uint32_t mask,
72 Label* on_not_equal);
73 virtual void CheckNotCharacterAfterMinusAnd(uc16 c,
74 uc16 minus,
75 uc16 mask,
76 Label* on_not_equal);
jkummerow@chromium.org1456e702012-03-30 08:38:13 +000077 virtual void CheckCharacterInRange(uc16 from,
78 uc16 to,
79 Label* on_in_range);
80 virtual void CheckCharacterNotInRange(uc16 from,
81 uc16 to,
82 Label* on_not_in_range);
83 virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set);
84
lrn@chromium.org7516f052011-03-30 08:52:27 +000085 // Checks whether the given offset from the current position is before
86 // the end of the string.
87 virtual void CheckPosition(int cp_offset, Label* on_outside_input);
88 virtual bool CheckSpecialCharacterClass(uc16 type,
89 Label* on_no_match);
90 virtual void Fail();
karlklose@chromium.org83a47282011-05-11 11:54:09 +000091 virtual Handle<HeapObject> GetCode(Handle<String> source);
lrn@chromium.org7516f052011-03-30 08:52:27 +000092 virtual void GoTo(Label* label);
93 virtual void IfRegisterGE(int reg, int comparand, Label* if_ge);
94 virtual void IfRegisterLT(int reg, int comparand, Label* if_lt);
95 virtual void IfRegisterEqPos(int reg, Label* if_eq);
96 virtual IrregexpImplementation Implementation();
97 virtual void LoadCurrentCharacter(int cp_offset,
98 Label* on_end_of_input,
99 bool check_bounds = true,
100 int characters = 1);
101 virtual void PopCurrentPosition();
102 virtual void PopRegister(int register_index);
103 virtual void PushBacktrack(Label* label);
104 virtual void PushCurrentPosition();
105 virtual void PushRegister(int register_index,
106 StackCheckFlag check_stack_limit);
107 virtual void ReadCurrentPositionFromRegister(int reg);
108 virtual void ReadStackPointerFromRegister(int reg);
109 virtual void SetCurrentPositionFromEnd(int by);
110 virtual void SetRegister(int register_index, int to);
jkummerow@chromium.org777db6f2012-05-24 09:33:09 +0000111 virtual bool Succeed();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000112 virtual void WriteCurrentPositionToRegister(int reg, int cp_offset);
113 virtual void ClearRegisters(int reg_from, int reg_to);
114 virtual void WriteStackPointerToRegister(int reg);
rossberg@chromium.org89e18f52012-10-22 13:09:53 +0000115 virtual bool CanReadUnaligned();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000116
117 // Called from RegExp if the stack-guard is triggered.
118 // If the code object is relocated, the return address is fixed before
119 // returning.
120 static int CheckStackGuardState(Address* return_address,
121 Code* re_code,
122 Address re_frame);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +0000123
lrn@chromium.org7516f052011-03-30 08:52:27 +0000124 private:
125 // Offsets from frame_pointer() of function parameters and stored registers.
126 static const int kFramePointer = 0;
127
128 // Above the frame pointer - Stored registers and stack passed parameters.
129 // Registers s0 to s7, fp, and ra.
130 static const int kStoredRegisters = kFramePointer;
131 // Return address (stored from link register, read into pc on return).
132 static const int kReturnAddress = kStoredRegisters + 9 * kPointerSize;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000133 static const int kSecondaryReturnAddress = kReturnAddress + kPointerSize;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000134 // Stack frame header.
135 static const int kStackFrameHeader = kReturnAddress + kPointerSize;
136 // Stack parameters placed by caller.
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000137 static const int kRegisterOutput = kStackFrameHeader + 20;
jkummerow@chromium.org777db6f2012-05-24 09:33:09 +0000138 static const int kNumOutputRegisters = kRegisterOutput + kPointerSize;
139 static const int kStackHighEnd = kNumOutputRegisters + kPointerSize;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000140 static const int kDirectCall = kStackHighEnd + kPointerSize;
141 static const int kIsolate = kDirectCall + kPointerSize;
142
143 // Below the frame pointer.
144 // Register parameters stored by setup code.
145 static const int kInputEnd = kFramePointer - kPointerSize;
146 static const int kInputStart = kInputEnd - kPointerSize;
147 static const int kStartIndex = kInputStart - kPointerSize;
148 static const int kInputString = kStartIndex - kPointerSize;
149 // When adding local variables remember to push space for them in
150 // the frame in GetCode.
jkummerow@chromium.org777db6f2012-05-24 09:33:09 +0000151 static const int kSuccessfulCaptures = kInputString - kPointerSize;
152 static const int kInputStartMinusOne = kSuccessfulCaptures - kPointerSize;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000153 // First register address. Following registers are below it on the stack.
jkummerow@chromium.org777db6f2012-05-24 09:33:09 +0000154 static const int kRegisterZero = kInputStartMinusOne - kPointerSize;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000155
156 // Initial size of code buffer.
157 static const size_t kRegExpCodeSize = 1024;
158
159 // Load a number of characters at the given offset from the
160 // current position, into the current-character register.
161 void LoadCurrentCharacterUnchecked(int cp_offset, int character_count);
162
163 // Check whether preemption has been requested.
164 void CheckPreemption();
165
166 // Check whether we are exceeding the stack limit on the backtrack stack.
167 void CheckStackLimit();
168
169
170 // Generate a call to CheckStackGuardState.
171 void CallCheckStackGuardState(Register scratch);
172
173 // The ebp-relative location of a regexp register.
174 MemOperand register_location(int register_index);
175
176 // Register holding the current input position as negative offset from
177 // the end of the string.
178 inline Register current_input_offset() { return t2; }
179
180 // The register containing the current character after LoadCurrentCharacter.
181 inline Register current_character() { return t3; }
182
183 // Register holding address of the end of the input string.
184 inline Register end_of_input_address() { return t6; }
185
186 // Register holding the frame address. Local variables, parameters and
187 // regexp registers are addressed relative to this.
188 inline Register frame_pointer() { return fp; }
189
190 // The register containing the backtrack stack top. Provides a meaningful
191 // name to the register.
192 inline Register backtrack_stackpointer() { return t4; }
193
194 // Register holding pointer to the current code object.
195 inline Register code_pointer() { return t1; }
196
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000197 // Byte size of chars in the string to match (decided by the Mode argument).
lrn@chromium.org7516f052011-03-30 08:52:27 +0000198 inline int char_size() { return static_cast<int>(mode_); }
199
200 // Equivalent to a conditional branch to the label, unless the label
201 // is NULL, in which case it is a conditional Backtrack.
202 void BranchOrBacktrack(Label* to,
203 Condition condition,
204 Register rs,
205 const Operand& rt);
206
207 // Call and return internally in the generated code in a way that
208 // is GC-safe (i.e., doesn't leave absolute code addresses on the stack)
209 inline void SafeCall(Label* to,
210 Condition cond,
211 Register rs,
212 const Operand& rt);
213 inline void SafeReturn();
214 inline void SafeCallTarget(Label* name);
215
216 // Pushes the value of a register on the backtrack stack. Decrements the
217 // stack pointer by a word size and stores the register's value there.
218 inline void Push(Register source);
219
220 // Pops a value from the backtrack stack. Reads the word at the stack pointer
221 // and increments it by a word size.
222 inline void Pop(Register target);
223
224 // Calls a C function and cleans up the frame alignment done by
225 // by FrameAlign. The called function *is* allowed to trigger a garbage
226 // collection, but may not take more than four arguments (no arguments
227 // passed on the stack), and the first argument will be a pointer to the
228 // return address.
229 inline void CallCFunctionUsingStub(ExternalReference function,
230 int num_arguments);
231
232
233 MacroAssembler* masm_;
234
235 // Which mode to generate code for (ASCII or UC16).
236 Mode mode_;
237
238 // One greater than maximal register index actually used.
239 int num_registers_;
240
241 // Number of registers to output at the end (the saved registers
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000242 // are always 0..num_saved_registers_-1).
lrn@chromium.org7516f052011-03-30 08:52:27 +0000243 int num_saved_registers_;
244
245 // Labels used internally.
246 Label entry_label_;
247 Label start_label_;
248 Label success_label_;
249 Label backtrack_label_;
250 Label exit_label_;
251 Label check_preempt_label_;
252 Label stack_overflow_label_;
kmillikin@chromium.orgc53e10d2011-05-18 09:12:58 +0000253 Label internal_failure_label_;
lrn@chromium.org7516f052011-03-30 08:52:27 +0000254};
255
256#endif // V8_INTERPRETED_REGEXP
257
258
259}} // namespace v8::internal
260
261#endif // V8_MIPS_REGEXP_MACRO_ASSEMBLER_MIPS_H_