blob: 26aab2c72ea21f8ce14972c28c490fba652b1ff1 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 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_REGEXP_MACRO_ASSEMBLER_H_
29#define V8_REGEXP_MACRO_ASSEMBLER_H_
30
31namespace v8 {
32namespace internal {
33
34struct DisjunctDecisionRow {
35 RegExpCharacterClass cc;
36 Label* on_match;
37};
38
39
40class RegExpMacroAssembler {
41 public:
42 // The implementation must be able to handle at least:
43 static const int kMaxRegister = (1 << 16) - 1;
44 static const int kMaxCPOffset = (1 << 15) - 1;
45 static const int kMinCPOffset = -(1 << 15);
46 enum IrregexpImplementation {
47 kIA32Implementation,
48 kARMImplementation,
49 kX64Implementation,
50 kBytecodeImplementation
51 };
52
53 enum StackCheckFlag {
54 kNoStackLimitCheck = false,
55 kCheckStackLimit = true
56 };
57
58 RegExpMacroAssembler();
59 virtual ~RegExpMacroAssembler();
60 // The maximal number of pushes between stack checks. Users must supply
61 // kCheckStackLimit flag to push operations (instead of kNoStackLimitCheck)
62 // at least once for every stack_limit() pushes that are executed.
63 virtual int stack_limit_slack() = 0;
64 virtual bool CanReadUnaligned();
65 virtual void AdvanceCurrentPosition(int by) = 0; // Signed cp change.
66 virtual void AdvanceRegister(int reg, int by) = 0; // r[reg] += by.
67 // Continues execution from the position pushed on the top of the backtrack
68 // stack by an earlier PushBacktrack(Label*).
69 virtual void Backtrack() = 0;
70 virtual void Bind(Label* label) = 0;
71 virtual void CheckAtStart(Label* on_at_start) = 0;
72 // Dispatch after looking the current character up in a 2-bits-per-entry
73 // map. The destinations vector has up to 4 labels.
74 virtual void CheckCharacter(uint32_t c, Label* on_equal) = 0;
75 // Bitwise and the current character with the given constant and then
76 // check for a match with c.
77 virtual void CheckCharacterAfterAnd(uint32_t c,
78 uint32_t and_with,
79 Label* on_equal) = 0;
80 virtual void CheckCharacterGT(uc16 limit, Label* on_greater) = 0;
81 virtual void CheckCharacterLT(uc16 limit, Label* on_less) = 0;
82 // Check the current character for a match with a literal string. If we
83 // fail to match then goto the on_failure label. If check_eos is set then
84 // the end of input always fails. If check_eos is clear then it is the
85 // caller's responsibility to ensure that the end of string is not hit.
86 // If the label is NULL then we should pop a backtrack address off
87 // the stack and go to that.
88 virtual void CheckCharacters(
89 Vector<const uc16> str,
90 int cp_offset,
91 Label* on_failure,
92 bool check_eos) = 0;
93 virtual void CheckGreedyLoop(Label* on_tos_equals_current_position) = 0;
94 virtual void CheckNotAtStart(Label* on_not_at_start) = 0;
95 virtual void CheckNotBackReference(int start_reg, Label* on_no_match) = 0;
96 virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
97 Label* on_no_match) = 0;
98 // Check the current character for a match with a literal character. If we
99 // fail to match then goto the on_failure label. End of input always
100 // matches. If the label is NULL then we should pop a backtrack address off
101 // the stack and go to that.
102 virtual void CheckNotCharacter(uint32_t c, Label* on_not_equal) = 0;
103 virtual void CheckNotCharacterAfterAnd(uint32_t c,
104 uint32_t and_with,
105 Label* on_not_equal) = 0;
106 // Subtract a constant from the current character, then or with the given
107 // constant and then check for a match with c.
108 virtual void CheckNotCharacterAfterMinusAnd(uc16 c,
109 uc16 minus,
110 uc16 and_with,
111 Label* on_not_equal) = 0;
112 virtual void CheckNotRegistersEqual(int reg1,
113 int reg2,
114 Label* on_not_equal) = 0;
115
116 // Checks whether the given offset from the current position is before
117 // the end of the string. May overwrite the current character.
118 virtual void CheckPosition(int cp_offset, Label* on_outside_input) {
119 LoadCurrentCharacter(cp_offset, on_outside_input, true);
120 }
121 // Check whether a standard/default character class matches the current
122 // character. Returns false if the type of special character class does
123 // not have custom support.
124 // May clobber the current loaded character.
125 virtual bool CheckSpecialCharacterClass(uc16 type,
126 int cp_offset,
127 bool check_offset,
128 Label* on_no_match) {
129 return false;
130 }
131 virtual void Fail() = 0;
132 virtual Handle<Object> GetCode(Handle<String> source) = 0;
133 virtual void GoTo(Label* label) = 0;
134 // Check whether a register is >= a given constant and go to a label if it
135 // is. Backtracks instead if the label is NULL.
136 virtual void IfRegisterGE(int reg, int comparand, Label* if_ge) = 0;
137 // Check whether a register is < a given constant and go to a label if it is.
138 // Backtracks instead if the label is NULL.
139 virtual void IfRegisterLT(int reg, int comparand, Label* if_lt) = 0;
140 // Check whether a register is == to the current position and go to a
141 // label if it is.
142 virtual void IfRegisterEqPos(int reg, Label* if_eq) = 0;
143 virtual IrregexpImplementation Implementation() = 0;
144 virtual void LoadCurrentCharacter(int cp_offset,
145 Label* on_end_of_input,
146 bool check_bounds = true,
147 int characters = 1) = 0;
148 virtual void PopCurrentPosition() = 0;
149 virtual void PopRegister(int register_index) = 0;
150 // Pushes the label on the backtrack stack, so that a following Backtrack
151 // will go to this label. Always checks the backtrack stack limit.
152 virtual void PushBacktrack(Label* label) = 0;
153 virtual void PushCurrentPosition() = 0;
154 virtual void PushRegister(int register_index,
155 StackCheckFlag check_stack_limit) = 0;
156 virtual void ReadCurrentPositionFromRegister(int reg) = 0;
157 virtual void ReadStackPointerFromRegister(int reg) = 0;
158 virtual void SetRegister(int register_index, int to) = 0;
159 virtual void Succeed() = 0;
160 virtual void WriteCurrentPositionToRegister(int reg, int cp_offset) = 0;
161 virtual void ClearRegisters(int reg_from, int reg_to) = 0;
162 virtual void WriteStackPointerToRegister(int reg) = 0;
163};
164
165
166#ifdef V8_NATIVE_REGEXP // Avoid compiling unused code.
167
168class NativeRegExpMacroAssembler: public RegExpMacroAssembler {
169 public:
170 // Type of input string to generate code for.
171 enum Mode { ASCII = 1, UC16 = 2 };
172
173 // Result of calling generated native RegExp code.
174 // RETRY: Something significant changed during execution, and the matching
175 // should be retried from scratch.
176 // EXCEPTION: Something failed during execution. If no exception has been
177 // thrown, it's an internal out-of-memory, and the caller should
178 // throw the exception.
179 // FAILURE: Matching failed.
180 // SUCCESS: Matching succeeded, and the output array has been filled with
181 // capture positions.
182 enum Result { RETRY = -2, EXCEPTION = -1, FAILURE = 0, SUCCESS = 1 };
183
184 NativeRegExpMacroAssembler();
185 virtual ~NativeRegExpMacroAssembler();
186 virtual bool CanReadUnaligned();
187
188 static Result Match(Handle<Code> regexp,
189 Handle<String> subject,
190 int* offsets_vector,
191 int offsets_vector_length,
192 int previous_index);
193
194 // Compares two-byte strings case insensitively.
195 // Called from generated RegExp code.
196 static int CaseInsensitiveCompareUC16(Address byte_offset1,
197 Address byte_offset2,
198 size_t byte_length);
199
200 // Called from RegExp if the backtrack stack limit is hit.
201 // Tries to expand the stack. Returns the new stack-pointer if
202 // successful, and updates the stack_top address, or returns 0 if unable
203 // to grow the stack.
204 // This function must not trigger a garbage collection.
205 static Address GrowStack(Address stack_pointer, Address* stack_top);
206
207 static const byte* StringCharacterPosition(String* subject, int start_index);
208
209 static Result Execute(Code* code,
210 String* input,
211 int start_offset,
212 const byte* input_start,
213 const byte* input_end,
214 int* output,
215 bool at_start);
216};
217
218
219// Enter C code from generated RegExp code in a way that allows
220// the C code to fix the return address in case of a GC.
221// Currently only needed on ARM.
222class RegExpCEntryStub: public CodeStub {
223 public:
224 RegExpCEntryStub() {}
225 virtual ~RegExpCEntryStub() {}
226 void Generate(MacroAssembler* masm);
227
228 private:
229 Major MajorKey() { return RegExpCEntry; }
230 int MinorKey() { return 0; }
231 const char* GetName() { return "RegExpCEntryStub"; }
232};
233
234#endif // V8_NATIVE_REGEXP
235
236} } // namespace v8::internal
237
238#endif // V8_REGEXP_MACRO_ASSEMBLER_H_