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