blob: 0314c707c6dddd9c2d7bcd504f63b86c29cd0464 [file] [log] [blame]
ager@chromium.orga74f0da2008-12-03 16:05:52 +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
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +000031#include "ast.h"
32
kasperl@chromium.org71affb52009-05-26 05:44:31 +000033namespace v8 {
34namespace internal {
ager@chromium.orga74f0da2008-12-03 16:05:52 +000035
ager@chromium.orga74f0da2008-12-03 16:05:52 +000036struct DisjunctDecisionRow {
37 RegExpCharacterClass cc;
38 Label* on_match;
39};
40
41
42class RegExpMacroAssembler {
43 public:
ager@chromium.orgddb913d2009-01-27 10:01:48 +000044 // The implementation must be able to handle at least:
45 static const int kMaxRegister = (1 << 16) - 1;
46 static const int kMaxCPOffset = (1 << 15) - 1;
47 static const int kMinCPOffset = -(1 << 15);
ager@chromium.orga74f0da2008-12-03 16:05:52 +000048 enum IrregexpImplementation {
49 kIA32Implementation,
50 kARMImplementation,
lrn@chromium.org7516f052011-03-30 08:52:27 +000051 kMIPSImplementation,
sgjesse@chromium.org911335c2009-08-19 12:59:44 +000052 kX64Implementation,
ager@chromium.org32912102009-01-16 10:38:43 +000053 kBytecodeImplementation
54 };
55
56 enum StackCheckFlag {
57 kNoStackLimitCheck = false,
58 kCheckStackLimit = true
59 };
ager@chromium.orga74f0da2008-12-03 16:05:52 +000060
61 RegExpMacroAssembler();
62 virtual ~RegExpMacroAssembler();
ager@chromium.org32912102009-01-16 10:38:43 +000063 // The maximal number of pushes between stack checks. Users must supply
64 // kCheckStackLimit flag to push operations (instead of kNoStackLimitCheck)
65 // at least once for every stack_limit() pushes that are executed.
66 virtual int stack_limit_slack() = 0;
ager@chromium.org18ad94b2009-09-02 08:22:29 +000067 virtual bool CanReadUnaligned();
ager@chromium.orga74f0da2008-12-03 16:05:52 +000068 virtual void AdvanceCurrentPosition(int by) = 0; // Signed cp change.
69 virtual void AdvanceRegister(int reg, int by) = 0; // r[reg] += by.
ager@chromium.org32912102009-01-16 10:38:43 +000070 // Continues execution from the position pushed on the top of the backtrack
71 // stack by an earlier PushBacktrack(Label*).
ager@chromium.orga74f0da2008-12-03 16:05:52 +000072 virtual void Backtrack() = 0;
73 virtual void Bind(Label* label) = 0;
ager@chromium.orgddb913d2009-01-27 10:01:48 +000074 virtual void CheckAtStart(Label* on_at_start) = 0;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000075 // Dispatch after looking the current character up in a 2-bits-per-entry
76 // map. The destinations vector has up to 4 labels.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000077 virtual void CheckCharacter(unsigned c, Label* on_equal) = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +000078 // Bitwise and the current character with the given constant and then
79 // check for a match with c.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +000080 virtual void CheckCharacterAfterAnd(unsigned c,
81 unsigned and_with,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +000082 Label* on_equal) = 0;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000083 virtual void CheckCharacterGT(uc16 limit, Label* on_greater) = 0;
84 virtual void CheckCharacterLT(uc16 limit, Label* on_less) = 0;
85 // Check the current character for a match with a literal string. If we
ager@chromium.org8bb60582008-12-11 12:02:20 +000086 // fail to match then goto the on_failure label. If check_eos is set then
87 // the end of input always fails. If check_eos is clear then it is the
88 // caller's responsibility to ensure that the end of string is not hit.
89 // If the label is NULL then we should pop a backtrack address off
90 // the stack and go to that.
ager@chromium.orga74f0da2008-12-03 16:05:52 +000091 virtual void CheckCharacters(
92 Vector<const uc16> str,
93 int cp_offset,
ager@chromium.org8bb60582008-12-11 12:02:20 +000094 Label* on_failure,
95 bool check_eos) = 0;
96 virtual void CheckGreedyLoop(Label* on_tos_equals_current_position) = 0;
ager@chromium.orga74f0da2008-12-03 16:05:52 +000097 virtual void CheckNotAtStart(Label* on_not_at_start) = 0;
98 virtual void CheckNotBackReference(int start_reg, Label* on_no_match) = 0;
99 virtual void CheckNotBackReferenceIgnoreCase(int start_reg,
100 Label* on_no_match) = 0;
101 // Check the current character for a match with a literal character. If we
102 // fail to match then goto the on_failure label. End of input always
103 // matches. If the label is NULL then we should pop a backtrack address off
104 // the stack and go to that.
sgjesse@chromium.orgc6c57182011-01-17 12:24:25 +0000105 virtual void CheckNotCharacter(unsigned c, Label* on_not_equal) = 0;
106 virtual void CheckNotCharacterAfterAnd(unsigned c,
107 unsigned and_with,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000108 Label* on_not_equal) = 0;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000109 // Subtract a constant from the current character, then or with the given
110 // constant and then check for a match with c.
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000111 virtual void CheckNotCharacterAfterMinusAnd(uc16 c,
112 uc16 minus,
113 uc16 and_with,
114 Label* on_not_equal) = 0;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000115 virtual void CheckNotRegistersEqual(int reg1,
116 int reg2,
117 Label* on_not_equal) = 0;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000118
119 // Checks whether the given offset from the current position is before
120 // the end of the string. May overwrite the current character.
121 virtual void CheckPosition(int cp_offset, Label* on_outside_input) {
122 LoadCurrentCharacter(cp_offset, on_outside_input, true);
123 }
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000124 // Check whether a standard/default character class matches the current
125 // character. Returns false if the type of special character class does
126 // not have custom support.
127 // May clobber the current loaded character.
128 virtual bool CheckSpecialCharacterClass(uc16 type,
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000129 Label* on_no_match) {
130 return false;
131 }
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000132 virtual void Fail() = 0;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000133 virtual Handle<HeapObject> GetCode(Handle<String> source) = 0;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000134 virtual void GoTo(Label* label) = 0;
135 // Check whether a register is >= a given constant and go to a label if it
136 // is. Backtracks instead if the label is NULL.
137 virtual void IfRegisterGE(int reg, int comparand, Label* if_ge) = 0;
138 // Check whether a register is < a given constant and go to a label if it is.
139 // Backtracks instead if the label is NULL.
140 virtual void IfRegisterLT(int reg, int comparand, Label* if_lt) = 0;
ager@chromium.org32912102009-01-16 10:38:43 +0000141 // Check whether a register is == to the current position and go to a
142 // label if it is.
143 virtual void IfRegisterEqPos(int reg, Label* if_eq) = 0;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000144 virtual IrregexpImplementation Implementation() = 0;
christian.plesner.hansen@gmail.com37abdec2009-01-06 14:43:28 +0000145 virtual void LoadCurrentCharacter(int cp_offset,
146 Label* on_end_of_input,
147 bool check_bounds = true,
148 int characters = 1) = 0;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000149 virtual void PopCurrentPosition() = 0;
150 virtual void PopRegister(int register_index) = 0;
ager@chromium.org32912102009-01-16 10:38:43 +0000151 // Pushes the label on the backtrack stack, so that a following Backtrack
152 // will go to this label. Always checks the backtrack stack limit.
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000153 virtual void PushBacktrack(Label* label) = 0;
154 virtual void PushCurrentPosition() = 0;
ager@chromium.org32912102009-01-16 10:38:43 +0000155 virtual void PushRegister(int register_index,
156 StackCheckFlag check_stack_limit) = 0;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000157 virtual void ReadCurrentPositionFromRegister(int reg) = 0;
158 virtual void ReadStackPointerFromRegister(int reg) = 0;
whesse@chromium.org4a5224e2010-10-20 12:37:07 +0000159 virtual void SetCurrentPositionFromEnd(int by) = 0;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000160 virtual void SetRegister(int register_index, int to) = 0;
161 virtual void Succeed() = 0;
ager@chromium.org8bb60582008-12-11 12:02:20 +0000162 virtual void WriteCurrentPositionToRegister(int reg, int cp_offset) = 0;
ager@chromium.orgddb913d2009-01-27 10:01:48 +0000163 virtual void ClearRegisters(int reg_from, int reg_to) = 0;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000164 virtual void WriteStackPointerToRegister(int reg) = 0;
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000165
166 // Controls the generation of large inlined constants in the code.
167 void set_slow_safe(bool ssc) { slow_safe_compiler_ = ssc; }
168 bool slow_safe() { return slow_safe_compiler_; }
169
170 private:
171 bool slow_safe_compiler_;
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000172};
173
174
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000175#ifndef V8_INTERPRETED_REGEXP // Avoid compiling unused code.
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000176
177class NativeRegExpMacroAssembler: public RegExpMacroAssembler {
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000178 public:
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000179 // Type of input string to generate code for.
180 enum Mode { ASCII = 1, UC16 = 2 };
181
182 // Result of calling generated native RegExp code.
183 // RETRY: Something significant changed during execution, and the matching
184 // should be retried from scratch.
185 // EXCEPTION: Something failed during execution. If no exception has been
186 // thrown, it's an internal out-of-memory, and the caller should
187 // throw the exception.
188 // FAILURE: Matching failed.
189 // SUCCESS: Matching succeeded, and the output array has been filled with
190 // capture positions.
191 enum Result { RETRY = -2, EXCEPTION = -1, FAILURE = 0, SUCCESS = 1 };
192
193 NativeRegExpMacroAssembler();
194 virtual ~NativeRegExpMacroAssembler();
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000195 virtual bool CanReadUnaligned();
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000196
197 static Result Match(Handle<Code> regexp,
198 Handle<String> subject,
199 int* offsets_vector,
200 int offsets_vector_length,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000201 int previous_index,
202 Isolate* isolate);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000203
204 // Compares two-byte strings case insensitively.
205 // Called from generated RegExp code.
206 static int CaseInsensitiveCompareUC16(Address byte_offset1,
207 Address byte_offset2,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000208 size_t byte_length,
209 Isolate* isolate);
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000210
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000211 // Called from RegExp if the backtrack stack limit is hit.
212 // Tries to expand the stack. Returns the new stack-pointer if
213 // successful, and updates the stack_top address, or returns 0 if unable
214 // to grow the stack.
215 // This function must not trigger a garbage collection.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000216 static Address GrowStack(Address stack_pointer, Address* stack_top,
217 Isolate* isolate);
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000218
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000219 static const byte* StringCharacterPosition(String* subject, int start_index);
220
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000221 // Byte map of ASCII characters with a 0xff if the character is a word
222 // character (digit, letter or underscore) and 0x00 otherwise.
223 // Used by generated RegExp code.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000224 static const byte word_character_map[128];
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000225
226 static Address word_character_map_address() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000227 return const_cast<Address>(&word_character_map[0]);
sgjesse@chromium.orgb302e562010-02-03 11:26:59 +0000228 }
229
sgjesse@chromium.org911335c2009-08-19 12:59:44 +0000230 static Result Execute(Code* code,
231 String* input,
232 int start_offset,
233 const byte* input_start,
234 const byte* input_end,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000235 int* output,
236 Isolate* isolate);
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000237};
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000238
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000239#endif // V8_INTERPRETED_REGEXP
ager@chromium.org18ad94b2009-09-02 08:22:29 +0000240
ager@chromium.orga74f0da2008-12-03 16:05:52 +0000241} } // namespace v8::internal
242
243#endif // V8_REGEXP_MACRO_ASSEMBLER_H_