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