1// Copyright 2011 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
29#ifndef V8_MIPS_REGEXP_MACRO_ASSEMBLER_MIPS_H_
30#define V8_MIPS_REGEXP_MACRO_ASSEMBLER_MIPS_H_
31
32#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
38namespace v8 {
39namespace internal {
40
41#ifdef V8_INTERPRETED_REGEXP
42class RegExpMacroAssemblerMIPS: public RegExpMacroAssembler {
43 public:
44  RegExpMacroAssemblerMIPS();
45  virtual ~RegExpMacroAssemblerMIPS();
46};
47#else  // V8_INTERPRETED_REGEXP
48class RegExpMacroAssemblerMIPS: public NativeRegExpMacroAssembler {
49 public:
50  RegExpMacroAssemblerMIPS(Mode mode, int registers_to_save);
51  virtual ~RegExpMacroAssemblerMIPS();
52  virtual int stack_limit_slack();
53  virtual void AdvanceCurrentPosition(int by);
54  virtual void AdvanceRegister(int reg, int by);
55  virtual void Backtrack();
56  virtual void Bind(Label* label);
57  virtual void CheckAtStart(Label* on_at_start);
58  virtual void CheckCharacter(uint32_t c, Label* on_equal);
59  virtual void CheckCharacterAfterAnd(uint32_t c,
60                                      uint32_t mask,
61                                      Label* on_equal);
62  virtual void CheckCharacterGT(uc16 limit, Label* on_greater);
63  virtual void CheckCharacterLT(uc16 limit, Label* on_less);
64  virtual void CheckCharacters(Vector<const uc16> str,
65                               int cp_offset,
66                               Label* on_failure,
67                               bool check_end_of_string);
68  // A "greedy loop" is a loop that is both greedy and with a simple
69  // body. It has a particularly simple implementation.
70  virtual void CheckGreedyLoop(Label* on_tos_equals_current_position);
71  virtual void CheckNotAtStart(Label* on_not_at_start);
72  virtual void CheckNotBackReference(int start_reg, Label* on_no_match);
73  virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
74                                               Label* on_no_match);
75  virtual void CheckNotRegistersEqual(int reg1, int reg2, Label* on_not_equal);
76  virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal);
77  virtual void CheckNotCharacterAfterAnd(uint32_t c,
78                                         uint32_t mask,
79                                         Label* on_not_equal);
80  virtual void CheckNotCharacterAfterMinusAnd(uc16 c,
81                                              uc16 minus,
82                                              uc16 mask,
83                                              Label* on_not_equal);
84  // Checks whether the given offset from the current position is before
85  // the end of the string.
86  virtual void CheckPosition(int cp_offset, Label* on_outside_input);
87  virtual bool CheckSpecialCharacterClass(uc16 type,
88                                          Label* on_no_match);
89  virtual void Fail();
90  virtual Handle<HeapObject> GetCode(Handle<String> source);
91  virtual void GoTo(Label* label);
92  virtual void IfRegisterGE(int reg, int comparand, Label* if_ge);
93  virtual void IfRegisterLT(int reg, int comparand, Label* if_lt);
94  virtual void IfRegisterEqPos(int reg, Label* if_eq);
95  virtual IrregexpImplementation Implementation();
96  virtual void LoadCurrentCharacter(int cp_offset,
97                                    Label* on_end_of_input,
98                                    bool check_bounds = true,
99                                    int characters = 1);
100  virtual void PopCurrentPosition();
101  virtual void PopRegister(int register_index);
102  virtual void PushBacktrack(Label* label);
103  virtual void PushCurrentPosition();
104  virtual void PushRegister(int register_index,
105                            StackCheckFlag check_stack_limit);
106  virtual void ReadCurrentPositionFromRegister(int reg);
107  virtual void ReadStackPointerFromRegister(int reg);
108  virtual void SetCurrentPositionFromEnd(int by);
109  virtual void SetRegister(int register_index, int to);
110  virtual void Succeed();
111  virtual void WriteCurrentPositionToRegister(int reg, int cp_offset);
112  virtual void ClearRegisters(int reg_from, int reg_to);
113  virtual void WriteStackPointerToRegister(int reg);
114
115  // Called from RegExp if the stack-guard is triggered.
116  // If the code object is relocated, the return address is fixed before
117  // returning.
118  static int CheckStackGuardState(Address* return_address,
119                                  Code* re_code,
120                                  Address re_frame);
121
122 private:
123  // Offsets from frame_pointer() of function parameters and stored registers.
124  static const int kFramePointer = 0;
125
126  // Above the frame pointer - Stored registers and stack passed parameters.
127  // Registers s0 to s7, fp, and ra.
128  static const int kStoredRegisters = kFramePointer;
129  // Return address (stored from link register, read into pc on return).
130  static const int kReturnAddress = kStoredRegisters + 9 * kPointerSize;
131  static const int kSecondaryReturnAddress = kReturnAddress + kPointerSize;
132  // Stack frame header.
133  static const int kStackFrameHeader = kReturnAddress + kPointerSize;
134  // Stack parameters placed by caller.
135  static const int kRegisterOutput = kStackFrameHeader + 20;
136  static const int kStackHighEnd = kRegisterOutput + kPointerSize;
137  static const int kDirectCall = kStackHighEnd + kPointerSize;
138  static const int kIsolate = kDirectCall + kPointerSize;
139
140  // Below the frame pointer.
141  // Register parameters stored by setup code.
142  static const int kInputEnd = kFramePointer - kPointerSize;
143  static const int kInputStart = kInputEnd - kPointerSize;
144  static const int kStartIndex = kInputStart - kPointerSize;
145  static const int kInputString = kStartIndex - kPointerSize;
146  // When adding local variables remember to push space for them in
147  // the frame in GetCode.
148  static const int kInputStartMinusOne = kInputString - kPointerSize;
149  static const int kAtStart = kInputStartMinusOne - kPointerSize;
150  // First register address. Following registers are below it on the stack.
151  static const int kRegisterZero = kAtStart - kPointerSize;
152
153  // Initial size of code buffer.
154  static const size_t kRegExpCodeSize = 1024;
155
156  // Load a number of characters at the given offset from the
157  // current position, into the current-character register.
158  void LoadCurrentCharacterUnchecked(int cp_offset, int character_count);
159
160  // Check whether preemption has been requested.
161  void CheckPreemption();
162
163  // Check whether we are exceeding the stack limit on the backtrack stack.
164  void CheckStackLimit();
165
166
167  // Generate a call to CheckStackGuardState.
168  void CallCheckStackGuardState(Register scratch);
169
170  // The ebp-relative location of a regexp register.
171  MemOperand register_location(int register_index);
172
173  // Register holding the current input position as negative offset from
174  // the end of the string.
175  inline Register current_input_offset() { return t2; }
176
177  // The register containing the current character after LoadCurrentCharacter.
178  inline Register current_character() { return t3; }
179
180  // Register holding address of the end of the input string.
181  inline Register end_of_input_address() { return t6; }
182
183  // Register holding the frame address. Local variables, parameters and
184  // regexp registers are addressed relative to this.
185  inline Register frame_pointer() { return fp; }
186
187  // The register containing the backtrack stack top. Provides a meaningful
188  // name to the register.
189  inline Register backtrack_stackpointer() { return t4; }
190
191  // Register holding pointer to the current code object.
192  inline Register code_pointer() { return t1; }
193
194  // Byte size of chars in the string to match (decided by the Mode argument).
195  inline int char_size() { return static_cast<int>(mode_); }
196
197  // Equivalent to a conditional branch to the label, unless the label
198  // is NULL, in which case it is a conditional Backtrack.
199  void BranchOrBacktrack(Label* to,
200                         Condition condition,
201                         Register rs,
202                         const Operand& rt);
203
204  // Call and return internally in the generated code in a way that
205  // is GC-safe (i.e., doesn't leave absolute code addresses on the stack)
206  inline void SafeCall(Label* to,
207                       Condition cond,
208                       Register rs,
209                       const Operand& rt);
210  inline void SafeReturn();
211  inline void SafeCallTarget(Label* name);
212
213  // Pushes the value of a register on the backtrack stack. Decrements the
214  // stack pointer by a word size and stores the register's value there.
215  inline void Push(Register source);
216
217  // Pops a value from the backtrack stack. Reads the word at the stack pointer
218  // and increments it by a word size.
219  inline void Pop(Register target);
220
221  // Calls a C function and cleans up the frame alignment done by
222  // by FrameAlign. The called function *is* allowed to trigger a garbage
223  // collection, but may not take more than four arguments (no arguments
224  // passed on the stack), and the first argument will be a pointer to the
225  // return address.
226  inline void CallCFunctionUsingStub(ExternalReference function,
227                                     int num_arguments);
228
229
230  MacroAssembler* masm_;
231
232  // Which mode to generate code for (ASCII or UC16).
233  Mode mode_;
234
235  // One greater than maximal register index actually used.
236  int num_registers_;
237
238  // Number of registers to output at the end (the saved registers
239  // are always 0..num_saved_registers_-1).
240  int num_saved_registers_;
241
242  // Labels used internally.
243  Label entry_label_;
244  Label start_label_;
245  Label success_label_;
246  Label backtrack_label_;
247  Label exit_label_;
248  Label check_preempt_label_;
249  Label stack_overflow_label_;
250  Label internal_failure_label_;
251};
252
253#endif  // V8_INTERPRETED_REGEXP
254
255
256}}  // namespace v8::internal
257
258#endif  // V8_MIPS_REGEXP_MACRO_ASSEMBLER_MIPS_H_
259