1// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef V8_REGEXP_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_
6#define V8_REGEXP_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_
7
8#include "src/ia32/assembler-ia32.h"
9#include "src/macro-assembler.h"
10#include "src/regexp/regexp-macro-assembler.h"
11
12namespace v8 {
13namespace internal {
14
15#ifndef V8_INTERPRETED_REGEXP
16class RegExpMacroAssemblerIA32: public NativeRegExpMacroAssembler {
17 public:
18  RegExpMacroAssemblerIA32(Isolate* isolate, Zone* zone, Mode mode,
19                           int registers_to_save);
20  virtual ~RegExpMacroAssemblerIA32();
21  virtual int stack_limit_slack();
22  virtual void AdvanceCurrentPosition(int by);
23  virtual void AdvanceRegister(int reg, int by);
24  virtual void Backtrack();
25  virtual void Bind(Label* label);
26  virtual void CheckAtStart(Label* on_at_start);
27  virtual void CheckCharacter(uint32_t c, Label* on_equal);
28  virtual void CheckCharacterAfterAnd(uint32_t c,
29                                      uint32_t mask,
30                                      Label* on_equal);
31  virtual void CheckCharacterGT(uc16 limit, Label* on_greater);
32  virtual void CheckCharacterLT(uc16 limit, Label* on_less);
33  // A "greedy loop" is a loop that is both greedy and with a simple
34  // body. It has a particularly simple implementation.
35  virtual void CheckGreedyLoop(Label* on_tos_equals_current_position);
36  virtual void CheckNotAtStart(int cp_offset, Label* on_not_at_start);
37  virtual void CheckNotBackReference(int start_reg, bool read_backward,
38                                     Label* on_no_match);
39  virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
40                                               bool read_backward,
41                                               Label* on_no_match);
42  virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal);
43  virtual void CheckNotCharacterAfterAnd(uint32_t c,
44                                         uint32_t mask,
45                                         Label* on_not_equal);
46  virtual void CheckNotCharacterAfterMinusAnd(uc16 c,
47                                              uc16 minus,
48                                              uc16 mask,
49                                              Label* on_not_equal);
50  virtual void CheckCharacterInRange(uc16 from,
51                                     uc16 to,
52                                     Label* on_in_range);
53  virtual void CheckCharacterNotInRange(uc16 from,
54                                        uc16 to,
55                                        Label* on_not_in_range);
56  virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set);
57
58  // Checks whether the given offset from the current position is before
59  // the end of the string.
60  virtual void CheckPosition(int cp_offset, Label* on_outside_input);
61  virtual bool CheckSpecialCharacterClass(uc16 type, Label* on_no_match);
62  virtual void Fail();
63  virtual Handle<HeapObject> GetCode(Handle<String> source);
64  virtual void GoTo(Label* label);
65  virtual void IfRegisterGE(int reg, int comparand, Label* if_ge);
66  virtual void IfRegisterLT(int reg, int comparand, Label* if_lt);
67  virtual void IfRegisterEqPos(int reg, Label* if_eq);
68  virtual IrregexpImplementation Implementation();
69  virtual void LoadCurrentCharacter(int cp_offset,
70                                    Label* on_end_of_input,
71                                    bool check_bounds = true,
72                                    int characters = 1);
73  virtual void PopCurrentPosition();
74  virtual void PopRegister(int register_index);
75  virtual void PushBacktrack(Label* label);
76  virtual void PushCurrentPosition();
77  virtual void PushRegister(int register_index,
78                            StackCheckFlag check_stack_limit);
79  virtual void ReadCurrentPositionFromRegister(int reg);
80  virtual void ReadStackPointerFromRegister(int reg);
81  virtual void SetCurrentPositionFromEnd(int by);
82  virtual void SetRegister(int register_index, int to);
83  virtual bool Succeed();
84  virtual void WriteCurrentPositionToRegister(int reg, int cp_offset);
85  virtual void ClearRegisters(int reg_from, int reg_to);
86  virtual void WriteStackPointerToRegister(int reg);
87
88  // Called from RegExp if the stack-guard is triggered.
89  // If the code object is relocated, the return address is fixed before
90  // returning.
91  static int CheckStackGuardState(Address* return_address,
92                                  Code* re_code,
93                                  Address re_frame);
94
95 private:
96  // Offsets from ebp of function parameters and stored registers.
97  static const int kFramePointer = 0;
98  // Above the frame pointer - function parameters and return address.
99  static const int kReturn_eip = kFramePointer + kPointerSize;
100  static const int kFrameAlign = kReturn_eip + kPointerSize;
101  // Parameters.
102  static const int kInputString = kFrameAlign;
103  static const int kStartIndex = kInputString + kPointerSize;
104  static const int kInputStart = kStartIndex + kPointerSize;
105  static const int kInputEnd = kInputStart + kPointerSize;
106  static const int kRegisterOutput = kInputEnd + kPointerSize;
107  // For the case of global regular expression, we have room to store at least
108  // one set of capture results.  For the case of non-global regexp, we ignore
109  // this value.
110  static const int kNumOutputRegisters = kRegisterOutput + kPointerSize;
111  static const int kStackHighEnd = kNumOutputRegisters + kPointerSize;
112  static const int kDirectCall = kStackHighEnd + kPointerSize;
113  static const int kIsolate = kDirectCall + kPointerSize;
114  // Below the frame pointer - local stack variables.
115  // When adding local variables remember to push space for them in
116  // the frame in GetCode.
117  static const int kBackup_esi = kFramePointer - kPointerSize;
118  static const int kBackup_edi = kBackup_esi - kPointerSize;
119  static const int kBackup_ebx = kBackup_edi - kPointerSize;
120  static const int kSuccessfulCaptures = kBackup_ebx - kPointerSize;
121  static const int kStringStartMinusOne = kSuccessfulCaptures - kPointerSize;
122  // First register address. Following registers are below it on the stack.
123  static const int kRegisterZero = kStringStartMinusOne - kPointerSize;
124
125  // Initial size of code buffer.
126  static const size_t kRegExpCodeSize = 1024;
127
128  // Load a number of characters at the given offset from the
129  // current position, into the current-character register.
130  void LoadCurrentCharacterUnchecked(int cp_offset, int character_count);
131
132  // Check whether preemption has been requested.
133  void CheckPreemption();
134
135  // Check whether we are exceeding the stack limit on the backtrack stack.
136  void CheckStackLimit();
137
138  // Generate a call to CheckStackGuardState.
139  void CallCheckStackGuardState(Register scratch);
140
141  // The ebp-relative location of a regexp register.
142  Operand register_location(int register_index);
143
144  // The register containing the current character after LoadCurrentCharacter.
145  inline Register current_character() { return edx; }
146
147  // The register containing the backtrack stack top. Provides a meaningful
148  // name to the register.
149  inline Register backtrack_stackpointer() { return ecx; }
150
151  // Byte size of chars in the string to match (decided by the Mode argument)
152  inline int char_size() { return static_cast<int>(mode_); }
153
154  // Equivalent to a conditional branch to the label, unless the label
155  // is NULL, in which case it is a conditional Backtrack.
156  void BranchOrBacktrack(Condition condition, Label* to);
157
158  // Call and return internally in the generated code in a way that
159  // is GC-safe (i.e., doesn't leave absolute code addresses on the stack)
160  inline void SafeCall(Label* to);
161  inline void SafeReturn();
162  inline void SafeCallTarget(Label* name);
163
164  // Pushes the value of a register on the backtrack stack. Decrements the
165  // stack pointer (ecx) by a word size and stores the register's value there.
166  inline void Push(Register source);
167
168  // Pushes a value on the backtrack stack. Decrements the stack pointer (ecx)
169  // by a word size and stores the value there.
170  inline void Push(Immediate value);
171
172  // Pops a value from the backtrack stack. Reads the word at the stack pointer
173  // (ecx) and increments it by a word size.
174  inline void Pop(Register target);
175
176  Isolate* isolate() const { return masm_->isolate(); }
177
178  MacroAssembler* masm_;
179
180  // Which mode to generate code for (LATIN1 or UC16).
181  Mode mode_;
182
183  // One greater than maximal register index actually used.
184  int num_registers_;
185
186  // Number of registers to output at the end (the saved registers
187  // are always 0..num_saved_registers_-1)
188  int num_saved_registers_;
189
190  // Labels used internally.
191  Label entry_label_;
192  Label start_label_;
193  Label success_label_;
194  Label backtrack_label_;
195  Label exit_label_;
196  Label check_preempt_label_;
197  Label stack_overflow_label_;
198};
199#endif  // V8_INTERPRETED_REGEXP
200
201}  // namespace internal
202}  // namespace v8
203
204#endif  // V8_REGEXP_IA32_REGEXP_MACRO_ASSEMBLER_IA32_H_
205