OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #ifndef VM_REGEXP_ASSEMBLER_H_ | 5 #ifndef VM_REGEXP_ASSEMBLER_H_ |
6 #define VM_REGEXP_ASSEMBLER_H_ | 6 #define VM_REGEXP_ASSEMBLER_H_ |
7 | 7 |
8 // SNIP | 8 #include "vm/assembler.h" |
| 9 #include "vm/intermediate_language.h" |
| 10 #include "vm/object.h" |
9 | 11 |
10 namespace dart { | 12 namespace dart { |
11 | 13 |
12 // SNIP | 14 // Utility function for the DotPrinter |
| 15 void PrintUtf16(uint16_t c); |
13 | 16 |
14 class RegExpMacroAssembler { | 17 |
| 18 /// Convenience wrapper around a BlockEntryInstr pointer. |
| 19 class BlockLabel : public ValueObject { |
| 20 public: |
| 21 BlockLabel() |
| 22 : block_(new JoinEntryInstr(-1, -1)), |
| 23 is_bound_(false), |
| 24 is_linked_(false) { } |
| 25 |
| 26 BlockLabel(const BlockLabel& that) |
| 27 : ValueObject(), |
| 28 block_(that.block_), |
| 29 is_bound_(that.is_bound_), |
| 30 is_linked_(that.is_linked_) { } |
| 31 |
| 32 BlockLabel& operator=(const BlockLabel& that) { |
| 33 block_ = that.block_; |
| 34 is_bound_ = that.is_bound_; |
| 35 is_linked_ = that.is_linked_; |
| 36 return *this; |
| 37 } |
| 38 |
| 39 JoinEntryInstr* block() const { return block_; } |
| 40 |
| 41 bool IsBound() const { return is_bound_; } |
| 42 void SetBound(intptr_t block_id) { |
| 43 ASSERT(!is_bound_); |
| 44 block_->set_block_id(block_id); |
| 45 is_bound_ = true; |
| 46 } |
| 47 |
| 48 bool IsLinked() const { return !is_bound_ && is_linked_; } |
| 49 void SetLinked() { |
| 50 is_linked_ = true; |
| 51 } |
| 52 |
| 53 intptr_t Position() const { |
| 54 ASSERT(IsBound()); |
| 55 return block_->block_id(); |
| 56 } |
| 57 |
| 58 private: |
| 59 JoinEntryInstr* block_; |
| 60 |
| 61 bool is_bound_; |
| 62 bool is_linked_; |
| 63 }; |
| 64 |
| 65 |
| 66 class RegExpMacroAssembler : public ZoneAllocated { |
15 public: | 67 public: |
16 // The implementation must be able to handle at least: | 68 // The implementation must be able to handle at least: |
17 static const int kMaxRegister = (1 << 16) - 1; | 69 static const intptr_t kMaxRegister = (1 << 16) - 1; |
18 static const int kMaxCPOffset = (1 << 15) - 1; | 70 static const intptr_t kMaxCPOffset = (1 << 15) - 1; |
19 static const int kMinCPOffset = -(1 << 15); | 71 static const intptr_t kMinCPOffset = -(1 << 15); |
20 | 72 |
21 static const int kTableSizeBits = 7; | 73 static const intptr_t kTableSizeBits = 7; |
22 static const int kTableSize = 1 << kTableSizeBits; | 74 static const intptr_t kTableSize = 1 << kTableSizeBits; |
23 static const int kTableMask = kTableSize - 1; | 75 static const intptr_t kTableMask = kTableSize - 1; |
| 76 |
| 77 enum { |
| 78 kParamStringIndex = 0, |
| 79 kParamStartOffsetIndex, |
| 80 kParamCount |
| 81 }; |
24 | 82 |
25 enum IrregexpImplementation { | 83 enum IrregexpImplementation { |
26 kIA32Implementation, | 84 kIRImplementation |
27 kARMImplementation, | |
28 kARM64Implementation, | |
29 kMIPSImplementation, | |
30 kX64Implementation, | |
31 kX87Implementation, | |
32 kBytecodeImplementation | |
33 }; | 85 }; |
34 | 86 |
35 enum StackCheckFlag { | 87 explicit RegExpMacroAssembler(Isolate* isolate); |
36 kNoStackLimitCheck = false, | |
37 kCheckStackLimit = true | |
38 }; | |
39 | |
40 explicit RegExpMacroAssembler(Zone* zone); | |
41 virtual ~RegExpMacroAssembler(); | 88 virtual ~RegExpMacroAssembler(); |
42 // The maximal number of pushes between stack checks. Users must supply | 89 // The maximal number of pushes between stack checks. Users must supply |
43 // kCheckStackLimit flag to push operations (instead of kNoStackLimitCheck) | 90 // kCheckStackLimit flag to push operations (instead of kNoStackLimitCheck) |
44 // at least once for every stack_limit() pushes that are executed. | 91 // at least once for every stack_limit() pushes that are executed. |
45 virtual int stack_limit_slack() = 0; | 92 virtual intptr_t stack_limit_slack() = 0; |
46 virtual bool CanReadUnaligned() = 0; | 93 virtual bool CanReadUnaligned() = 0; |
47 virtual void AdvanceCurrentPosition(int by) = 0; // Signed cp change. | 94 virtual void AdvanceCurrentPosition(intptr_t by) = 0; // Signed cp change. |
48 virtual void AdvanceRegister(int reg, int by) = 0; // r[reg] += by. | 95 virtual void AdvanceRegister(intptr_t reg, intptr_t by) = 0; // r[reg] += by. |
49 // Continues execution from the position pushed on the top of the backtrack | 96 // Continues execution from the position pushed on the top of the backtrack |
50 // stack by an earlier PushBacktrack(Label*). | 97 // stack by an earlier PushBacktrack(BlockLabel*). |
51 virtual void Backtrack() = 0; | 98 virtual void Backtrack() = 0; |
52 virtual void Bind(Label* label) = 0; | 99 virtual void BindBlock(BlockLabel* label) = 0; |
53 virtual void CheckAtStart(Label* on_at_start) = 0; | 100 virtual void CheckAtStart(BlockLabel* on_at_start) = 0; |
54 // Dispatch after looking the current character up in a 2-bits-per-entry | 101 // Dispatch after looking the current character up in a 2-bits-per-entry |
55 // map. The destinations vector has up to 4 labels. | 102 // map. The destinations vector has up to 4 labels. |
56 virtual void CheckCharacter(unsigned c, Label* on_equal) = 0; | 103 virtual void CheckCharacter(unsigned c, BlockLabel* on_equal) = 0; |
57 // Bitwise and the current character with the given constant and then | 104 // Bitwise and the current character with the given constant and then |
58 // check for a match with c. | 105 // check for a match with c. |
59 virtual void CheckCharacterAfterAnd(unsigned c, | 106 virtual void CheckCharacterAfterAnd(unsigned c, |
60 unsigned and_with, | 107 unsigned and_with, |
61 Label* on_equal) = 0; | 108 BlockLabel* on_equal) = 0; |
62 virtual void CheckCharacterGT(uc16 limit, Label* on_greater) = 0; | 109 virtual void CheckCharacterGT(uint16_t limit, BlockLabel* on_greater) = 0; |
63 virtual void CheckCharacterLT(uc16 limit, Label* on_less) = 0; | 110 virtual void CheckCharacterLT(uint16_t limit, BlockLabel* on_less) = 0; |
64 virtual void CheckGreedyLoop(Label* on_tos_equals_current_position) = 0; | 111 virtual void CheckGreedyLoop(BlockLabel* on_tos_equals_current_position) = 0; |
65 virtual void CheckNotAtStart(Label* on_not_at_start) = 0; | 112 virtual void CheckNotAtStart(BlockLabel* on_not_at_start) = 0; |
66 virtual void CheckNotBackReference(int start_reg, Label* on_no_match) = 0; | 113 virtual void CheckNotBackReference( |
67 virtual void CheckNotBackReferenceIgnoreCase(int start_reg, | 114 intptr_t start_reg, BlockLabel* on_no_match) = 0; |
68 Label* on_no_match) = 0; | 115 virtual void CheckNotBackReferenceIgnoreCase(intptr_t start_reg, |
| 116 BlockLabel* on_no_match) = 0; |
69 // Check the current character for a match with a literal character. If we | 117 // Check the current character for a match with a literal character. If we |
70 // fail to match then goto the on_failure label. End of input always | 118 // fail to match then goto the on_failure label. End of input always |
71 // matches. If the label is NULL then we should pop a backtrack address off | 119 // matches. If the label is NULL then we should pop a backtrack address off |
72 // the stack and go to that. | 120 // the stack and go to that. |
73 virtual void CheckNotCharacter(unsigned c, Label* on_not_equal) = 0; | 121 virtual void CheckNotCharacter(unsigned c, BlockLabel* on_not_equal) = 0; |
74 virtual void CheckNotCharacterAfterAnd(unsigned c, | 122 virtual void CheckNotCharacterAfterAnd(unsigned c, |
75 unsigned and_with, | 123 unsigned and_with, |
76 Label* on_not_equal) = 0; | 124 BlockLabel* on_not_equal) = 0; |
77 // Subtract a constant from the current character, then and with the given | 125 // Subtract a constant from the current character, then and with the given |
78 // constant and then check for a match with c. | 126 // constant and then check for a match with c. |
79 virtual void CheckNotCharacterAfterMinusAnd(uc16 c, | 127 virtual void CheckNotCharacterAfterMinusAnd(uint16_t c, |
80 uc16 minus, | 128 uint16_t minus, |
81 uc16 and_with, | 129 uint16_t and_with, |
82 Label* on_not_equal) = 0; | 130 BlockLabel* on_not_equal) = 0; |
83 virtual void CheckCharacterInRange(uc16 from, | 131 virtual void CheckCharacterInRange(uint16_t from, |
84 uc16 to, // Both inclusive. | 132 uint16_t to, // Both inclusive. |
85 Label* on_in_range) = 0; | 133 BlockLabel* on_in_range) = 0; |
86 virtual void CheckCharacterNotInRange(uc16 from, | 134 virtual void CheckCharacterNotInRange(uint16_t from, |
87 uc16 to, // Both inclusive. | 135 uint16_t to, // Both inclusive. |
88 Label* on_not_in_range) = 0; | 136 BlockLabel* on_not_in_range) = 0; |
89 | 137 |
90 // The current character (modulus the kTableSize) is looked up in the byte | 138 // The current character (modulus the kTableSize) is looked up in the byte |
91 // array, and if the found byte is non-zero, we jump to the on_bit_set label. | 139 // array, and if the found byte is non-zero, we jump to the on_bit_set label. |
92 virtual void CheckBitInTable(Handle<ByteArray> table, Label* on_bit_set) = 0; | 140 virtual void CheckBitInTable(const TypedData& table, |
| 141 BlockLabel* on_bit_set) = 0; |
93 | 142 |
94 // Checks whether the given offset from the current position is before | 143 // Checks whether the given offset from the current position is before |
95 // the end of the string. May overwrite the current character. | 144 // the end of the string. May overwrite the current character. |
96 virtual void CheckPosition(int cp_offset, Label* on_outside_input) { | 145 virtual void CheckPosition(intptr_t cp_offset, BlockLabel* on_outside_input) { |
97 LoadCurrentCharacter(cp_offset, on_outside_input, true); | 146 LoadCurrentCharacter(cp_offset, on_outside_input, true); |
98 } | 147 } |
99 // Check whether a standard/default character class matches the current | 148 // Check whether a standard/default character class matches the current |
100 // character. Returns false if the type of special character class does | 149 // character. Returns false if the type of special character class does |
101 // not have custom support. | 150 // not have custom support. |
102 // May clobber the current loaded character. | 151 // May clobber the current loaded character. |
103 virtual bool CheckSpecialCharacterClass(uc16 type, | 152 virtual bool CheckSpecialCharacterClass(uint16_t type, |
104 Label* on_no_match) { | 153 BlockLabel* on_no_match) { |
105 return false; | 154 return false; |
106 } | 155 } |
107 virtual void Fail() = 0; | 156 virtual void Fail() = 0; |
108 virtual Handle<HeapObject> GetCode(Handle<String> source) = 0; | |
109 virtual void GoTo(Label* label) = 0; | |
110 // Check whether a register is >= a given constant and go to a label if it | 157 // Check whether a register is >= a given constant and go to a label if it |
111 // is. Backtracks instead if the label is NULL. | 158 // is. Backtracks instead if the label is NULL. |
112 virtual void IfRegisterGE(int reg, int comparand, Label* if_ge) = 0; | 159 virtual void IfRegisterGE( |
| 160 intptr_t reg, intptr_t comparand, BlockLabel* if_ge) = 0; |
113 // Check whether a register is < a given constant and go to a label if it is. | 161 // Check whether a register is < a given constant and go to a label if it is. |
114 // Backtracks instead if the label is NULL. | 162 // Backtracks instead if the label is NULL. |
115 virtual void IfRegisterLT(int reg, int comparand, Label* if_lt) = 0; | 163 virtual void IfRegisterLT( |
| 164 intptr_t reg, intptr_t comparand, BlockLabel* if_lt) = 0; |
116 // Check whether a register is == to the current position and go to a | 165 // Check whether a register is == to the current position and go to a |
117 // label if it is. | 166 // label if it is. |
118 virtual void IfRegisterEqPos(int reg, Label* if_eq) = 0; | 167 virtual void IfRegisterEqPos(intptr_t reg, BlockLabel* if_eq) = 0; |
119 virtual IrregexpImplementation Implementation() = 0; | 168 virtual IrregexpImplementation Implementation() = 0; |
120 virtual void LoadCurrentCharacter(int cp_offset, | 169 // The assembler is closed, iff there is no current instruction assigned. |
121 Label* on_end_of_input, | 170 virtual bool IsClosed() const = 0; |
| 171 // Jump to the target label without setting it as the current instruction. |
| 172 virtual void GoTo(BlockLabel* to) = 0; |
| 173 virtual void LoadCurrentCharacter(intptr_t cp_offset, |
| 174 BlockLabel* on_end_of_input, |
122 bool check_bounds = true, | 175 bool check_bounds = true, |
123 int characters = 1) = 0; | 176 intptr_t characters = 1) = 0; |
124 virtual void PopCurrentPosition() = 0; | 177 virtual void PopCurrentPosition() = 0; |
125 virtual void PopRegister(int register_index) = 0; | 178 virtual void PopRegister(intptr_t register_index) = 0; |
| 179 // Prints string within the generated code. Used for debugging. |
| 180 virtual void Print(const char* str) = 0; |
| 181 // Prints all emitted blocks. |
| 182 virtual void PrintBlocks() = 0; |
126 // Pushes the label on the backtrack stack, so that a following Backtrack | 183 // Pushes the label on the backtrack stack, so that a following Backtrack |
127 // will go to this label. Always checks the backtrack stack limit. | 184 // will go to this label. Always checks the backtrack stack limit. |
128 virtual void PushBacktrack(Label* label) = 0; | 185 virtual void PushBacktrack(BlockLabel* label) = 0; |
129 virtual void PushCurrentPosition() = 0; | 186 virtual void PushCurrentPosition() = 0; |
130 virtual void PushRegister(int register_index, | 187 virtual void PushRegister(intptr_t register_index) = 0; |
131 StackCheckFlag check_stack_limit) = 0; | 188 virtual void ReadCurrentPositionFromRegister(intptr_t reg) = 0; |
132 virtual void ReadCurrentPositionFromRegister(int reg) = 0; | 189 virtual void ReadStackPointerFromRegister(intptr_t reg) = 0; |
133 virtual void ReadStackPointerFromRegister(int reg) = 0; | 190 virtual void SetCurrentPositionFromEnd(intptr_t by) = 0; |
134 virtual void SetCurrentPositionFromEnd(int by) = 0; | 191 virtual void SetRegister(intptr_t register_index, intptr_t to) = 0; |
135 virtual void SetRegister(int register_index, int to) = 0; | |
136 // Return whether the matching (with a global regexp) will be restarted. | 192 // Return whether the matching (with a global regexp) will be restarted. |
137 virtual bool Succeed() = 0; | 193 virtual bool Succeed() = 0; |
138 virtual void WriteCurrentPositionToRegister(int reg, int cp_offset) = 0; | 194 virtual void WriteCurrentPositionToRegister( |
139 virtual void ClearRegisters(int reg_from, int reg_to) = 0; | 195 intptr_t reg, intptr_t cp_offset) = 0; |
140 virtual void WriteStackPointerToRegister(int reg) = 0; | 196 virtual void ClearRegisters(intptr_t reg_from, intptr_t reg_to) = 0; |
| 197 virtual void WriteStackPointerToRegister(intptr_t reg) = 0; |
141 | 198 |
142 // Controls the generation of large inlined constants in the code. | 199 // Controls the generation of large inlined constants in the code. |
143 void set_slow_safe(bool ssc) { slow_safe_compiler_ = ssc; } | 200 void set_slow_safe(bool ssc) { slow_safe_compiler_ = ssc; } |
144 bool slow_safe() { return slow_safe_compiler_; } | 201 bool slow_safe() { return slow_safe_compiler_; } |
145 | 202 |
146 enum GlobalMode { NOT_GLOBAL, GLOBAL, GLOBAL_NO_ZERO_LENGTH_CHECK }; | 203 enum GlobalMode { NOT_GLOBAL, GLOBAL, GLOBAL_NO_ZERO_LENGTH_CHECK }; |
147 // Set whether the regular expression has the global flag. Exiting due to | 204 // Set whether the regular expression has the global flag. Exiting due to |
148 // a failure in a global regexp may still mean success overall. | 205 // a failure in a global regexp may still mean success overall. |
149 inline void set_global_mode(GlobalMode mode) { global_mode_ = mode; } | 206 inline void set_global_mode(GlobalMode mode) { global_mode_ = mode; } |
150 inline bool global() { return global_mode_ != NOT_GLOBAL; } | 207 inline bool global() { return global_mode_ != NOT_GLOBAL; } |
151 inline bool global_with_zero_length_check() { | 208 inline bool global_with_zero_length_check() { |
152 return global_mode_ == GLOBAL; | 209 return global_mode_ == GLOBAL; |
153 } | 210 } |
154 | 211 |
155 Zone* zone() const { return zone_; } | 212 Isolate* isolate() const { return isolate_; } |
156 | 213 |
157 private: | 214 private: |
158 bool slow_safe_compiler_; | 215 bool slow_safe_compiler_; |
159 bool global_mode_; | 216 bool global_mode_; |
160 Zone* zone_; | 217 Isolate* isolate_; |
161 }; | 218 }; |
162 | 219 |
163 // SNIP | 220 |
| 221 class IRRegExpMacroAssembler : public RegExpMacroAssembler { |
| 222 public: |
| 223 // Type of input string to generate code for. |
| 224 enum Mode { ASCII = 1, UC16 = 2 }; |
| 225 |
| 226 // Result of calling generated native RegExp code. |
| 227 // RETRY: Something significant changed during execution, and the matching |
| 228 // should be retried from scratch. |
| 229 // EXCEPTION: Something failed during execution. If no exception has been |
| 230 // thrown, it's an internal out-of-memory, and the caller should |
| 231 // throw the exception. |
| 232 // FAILURE: Matching failed. |
| 233 // SUCCESS: Matching succeeded, and the output array has been filled with |
| 234 // capture positions. |
| 235 enum Result { RETRY = -2, EXCEPTION = -1, FAILURE = 0, SUCCESS = 1 }; |
| 236 |
| 237 IRRegExpMacroAssembler(intptr_t specialization_cid, |
| 238 intptr_t capture_count, |
| 239 const ParsedFunction* parsed_function, |
| 240 const ZoneGrowableArray<const ICData*>& ic_data_array, |
| 241 Isolate* isolate); |
| 242 virtual ~IRRegExpMacroAssembler(); |
| 243 |
| 244 virtual bool CanReadUnaligned(); |
| 245 |
| 246 // Compares two-byte strings case insensitively. |
| 247 // Called from generated RegExp code. |
| 248 static RawBool* CaseInsensitiveCompareUC16( |
| 249 RawString* str_raw, |
| 250 RawSmi* lhs_index_raw, |
| 251 RawSmi* rhs_index_raw, |
| 252 RawSmi* length_raw); |
| 253 |
| 254 static RawArray* Execute(const Function& function, |
| 255 const String& input, |
| 256 const Smi& start_offset, |
| 257 Isolate* isolate); |
| 258 |
| 259 virtual bool IsClosed() const { return (current_instruction_ == NULL); } |
| 260 |
| 261 virtual intptr_t stack_limit_slack(); |
| 262 virtual void AdvanceCurrentPosition(intptr_t by); |
| 263 virtual void AdvanceRegister(intptr_t reg, intptr_t by); |
| 264 virtual void Backtrack(); |
| 265 virtual void BindBlock(BlockLabel* label); |
| 266 virtual void CheckAtStart(BlockLabel* on_at_start); |
| 267 virtual void CheckCharacter(uint32_t c, BlockLabel* on_equal); |
| 268 virtual void CheckCharacterAfterAnd(uint32_t c, |
| 269 uint32_t mask, |
| 270 BlockLabel* on_equal); |
| 271 virtual void CheckCharacterGT(uint16_t limit, BlockLabel* on_greater); |
| 272 virtual void CheckCharacterLT(uint16_t limit, BlockLabel* on_less); |
| 273 // A "greedy loop" is a loop that is both greedy and with a simple |
| 274 // body. It has a particularly simple implementation. |
| 275 virtual void CheckGreedyLoop(BlockLabel* on_tos_equals_current_position); |
| 276 virtual void CheckNotAtStart(BlockLabel* on_not_at_start); |
| 277 virtual void CheckNotBackReference(intptr_t start_reg, |
| 278 BlockLabel* on_no_match); |
| 279 virtual void CheckNotBackReferenceIgnoreCase(intptr_t start_reg, |
| 280 BlockLabel* on_no_match); |
| 281 virtual void CheckNotCharacter(uint32_t c, BlockLabel* on_not_equal); |
| 282 virtual void CheckNotCharacterAfterAnd(uint32_t c, |
| 283 uint32_t mask, |
| 284 BlockLabel* on_not_equal); |
| 285 virtual void CheckNotCharacterAfterMinusAnd(uint16_t c, |
| 286 uint16_t minus, |
| 287 uint16_t mask, |
| 288 BlockLabel* on_not_equal); |
| 289 virtual void CheckCharacterInRange(uint16_t from, |
| 290 uint16_t to, |
| 291 BlockLabel* on_in_range); |
| 292 virtual void CheckCharacterNotInRange(uint16_t from, |
| 293 uint16_t to, |
| 294 BlockLabel* on_not_in_range); |
| 295 virtual void CheckBitInTable(const TypedData& table, BlockLabel* on_bit_set); |
| 296 |
| 297 // Checks whether the given offset from the current position is before |
| 298 // the end of the string. |
| 299 virtual void CheckPosition(intptr_t cp_offset, BlockLabel* on_outside_input); |
| 300 virtual bool CheckSpecialCharacterClass( |
| 301 uint16_t type, BlockLabel* on_no_match); |
| 302 virtual void Fail(); |
| 303 virtual void IfRegisterGE(intptr_t reg, |
| 304 intptr_t comparand, BlockLabel* if_ge); |
| 305 virtual void IfRegisterLT(intptr_t reg, |
| 306 intptr_t comparand, BlockLabel* if_lt); |
| 307 virtual void IfRegisterEqPos(intptr_t reg, BlockLabel* if_eq); |
| 308 virtual IrregexpImplementation Implementation(); |
| 309 virtual void GoTo(BlockLabel* to); |
| 310 virtual void LoadCurrentCharacter(intptr_t cp_offset, |
| 311 BlockLabel* on_end_of_input, |
| 312 bool check_bounds = true, |
| 313 intptr_t characters = 1); |
| 314 virtual void PopCurrentPosition(); |
| 315 virtual void PopRegister(intptr_t register_index); |
| 316 virtual void Print(const char* str); |
| 317 virtual void PushBacktrack(BlockLabel* label); |
| 318 virtual void PushCurrentPosition(); |
| 319 virtual void PushRegister(intptr_t register_index); |
| 320 virtual void ReadCurrentPositionFromRegister(intptr_t reg); |
| 321 virtual void ReadStackPointerFromRegister(intptr_t reg); |
| 322 virtual void SetCurrentPositionFromEnd(intptr_t by); |
| 323 virtual void SetRegister(intptr_t register_index, intptr_t to); |
| 324 virtual bool Succeed(); |
| 325 virtual void WriteCurrentPositionToRegister(intptr_t reg, intptr_t cp_offset); |
| 326 virtual void ClearRegisters(intptr_t reg_from, intptr_t reg_to); |
| 327 virtual void WriteStackPointerToRegister(intptr_t reg); |
| 328 |
| 329 virtual void PrintBlocks(); |
| 330 |
| 331 GraphEntryInstr* graph_entry() const { return entry_block_; } |
| 332 |
| 333 intptr_t num_stack_locals() const { return local_id_.Count(); } |
| 334 intptr_t num_blocks() const { return block_id_.Count(); } |
| 335 |
| 336 // A table mapping block ids to block offsets, used to look up offsets |
| 337 // for indirect goto instructions. |
| 338 void FinalizeBlockOffsetTable(); |
| 339 |
| 340 // Fill in indirect goto successors. |
| 341 void FinalizeIndirectGotos(); |
| 342 |
| 343 private: |
| 344 // Generate the contents of preset blocks. The entry block is the entry point |
| 345 // of the generated code. |
| 346 void GenerateEntryBlock(); |
| 347 // Performs backtracking, i.e. popping an offset from the stack and doing |
| 348 // an indirect goto. |
| 349 void GenerateBacktrackBlock(); |
| 350 // Copies capture indices into the result area and returns true. |
| 351 void GenerateSuccessBlock(); |
| 352 // Returns false. |
| 353 void GenerateExitBlock(); |
| 354 |
| 355 enum ComparisonKind { |
| 356 kEQ, |
| 357 kNE, |
| 358 kLT, |
| 359 kGT, |
| 360 kLTE, |
| 361 kGTE, |
| 362 }; |
| 363 |
| 364 struct InstanceCallDescriptor { |
| 365 // Standard (i.e. most non-Smi) functions. |
| 366 explicit InstanceCallDescriptor(const String& name) |
| 367 : name(name), |
| 368 token_kind(Token::kILLEGAL), |
| 369 checked_argument_count(1) { } |
| 370 |
| 371 InstanceCallDescriptor(const String& name, |
| 372 Token::Kind token_kind, |
| 373 intptr_t checked_argument_count) |
| 374 : name(name), |
| 375 token_kind(token_kind), |
| 376 checked_argument_count(checked_argument_count) { } |
| 377 |
| 378 // Special cases for Smi and indexing functions. |
| 379 static InstanceCallDescriptor FromToken(Token::Kind token_kind) { |
| 380 switch (token_kind) { |
| 381 case Token::kEQ: return InstanceCallDescriptor( |
| 382 Symbols::EqualOperator(), token_kind, 2); |
| 383 case Token::kADD: return InstanceCallDescriptor( |
| 384 Symbols::Plus(), token_kind, 2); |
| 385 case Token::kSUB: return InstanceCallDescriptor( |
| 386 Symbols::Minus(), token_kind, 2); |
| 387 case Token::kBIT_OR: return InstanceCallDescriptor( |
| 388 Symbols::BitOr(), token_kind, 2); |
| 389 case Token::kBIT_AND: return InstanceCallDescriptor( |
| 390 Symbols::BitAnd(), token_kind, 2); |
| 391 case Token::kLT: return InstanceCallDescriptor( |
| 392 Symbols::LAngleBracket(), token_kind, 2); |
| 393 case Token::kLTE: return InstanceCallDescriptor( |
| 394 Symbols::LessEqualOperator(), token_kind, 2); |
| 395 case Token::kGT: return InstanceCallDescriptor( |
| 396 Symbols::RAngleBracket(), token_kind, 2); |
| 397 case Token::kGTE: return InstanceCallDescriptor( |
| 398 Symbols::GreaterEqualOperator(), token_kind, 2); |
| 399 case Token::kNEGATE: return InstanceCallDescriptor( |
| 400 Symbols::UnaryMinus(), token_kind, 1); |
| 401 case Token::kINDEX: return InstanceCallDescriptor( |
| 402 Symbols::IndexToken(), token_kind, 2); |
| 403 case Token::kASSIGN_INDEX: return InstanceCallDescriptor( |
| 404 Symbols::AssignIndexToken(), token_kind, 3); |
| 405 default: |
| 406 UNREACHABLE(); |
| 407 } |
| 408 UNREACHABLE(); |
| 409 return InstanceCallDescriptor(Symbols::Empty()); |
| 410 } |
| 411 |
| 412 const String& name; |
| 413 Token::Kind token_kind; |
| 414 intptr_t checked_argument_count; |
| 415 }; |
| 416 |
| 417 LocalVariable* Local(const String& name); |
| 418 LocalVariable* Parameter(const String& name, intptr_t index) const; |
| 419 |
| 420 ConstantInstr* Int64Constant(int64_t value) const; |
| 421 ConstantInstr* Uint64Constant(uint64_t value) const; |
| 422 ConstantInstr* BoolConstant(bool value) const; |
| 423 ConstantInstr* StringConstant(const char* value) const; |
| 424 |
| 425 // The word character map static member of the RegExp class. |
| 426 // Byte map of one byte characters with a 0xff if the character is a word |
| 427 // character (digit, letter or underscore) and 0x00 otherwise. |
| 428 // Used by generated RegExp code. |
| 429 ConstantInstr* WordCharacterMapConstant() const; |
| 430 |
| 431 ComparisonInstr* Comparison(ComparisonKind kind, |
| 432 Definition* lhs, Definition* rhs); |
| 433 |
| 434 InstanceCallInstr* InstanceCall(const InstanceCallDescriptor& desc, |
| 435 PushArgumentInstr* arg1) const; |
| 436 InstanceCallInstr* InstanceCall(const InstanceCallDescriptor& desc, |
| 437 PushArgumentInstr* arg1, |
| 438 PushArgumentInstr* arg2) const; |
| 439 InstanceCallInstr* InstanceCall(const InstanceCallDescriptor& desc, |
| 440 PushArgumentInstr* arg1, |
| 441 PushArgumentInstr* arg2, |
| 442 PushArgumentInstr* arg3) const; |
| 443 InstanceCallInstr* InstanceCall( |
| 444 const InstanceCallDescriptor& desc, |
| 445 ZoneGrowableArray<PushArgumentInstr*>* arguments) const; |
| 446 |
| 447 StaticCallInstr* StaticCall(const Function& function) const; |
| 448 StaticCallInstr* StaticCall(const Function& function, |
| 449 PushArgumentInstr* arg1) const; |
| 450 StaticCallInstr* StaticCall(const Function& function, |
| 451 PushArgumentInstr* arg1, |
| 452 PushArgumentInstr* arg2) const; |
| 453 StaticCallInstr* StaticCall( |
| 454 const Function& function, |
| 455 ZoneGrowableArray<PushArgumentInstr*>* arguments) const; |
| 456 |
| 457 // Creates a new block consisting simply of a goto to dst. |
| 458 TargetEntryInstr* TargetWithJoinGoto(JoinEntryInstr* dst); |
| 459 IndirectEntryInstr* IndirectWithJoinGoto(JoinEntryInstr* dst); |
| 460 |
| 461 // Adds, respectively subtracts lhs and rhs and returns the result. |
| 462 Definition* Add(PushArgumentInstr* lhs, PushArgumentInstr* rhs); |
| 463 Definition* Sub(PushArgumentInstr* lhs, PushArgumentInstr* rhs); |
| 464 |
| 465 LoadLocalInstr* LoadLocal(LocalVariable* local) const; |
| 466 void StoreLocal(LocalVariable* local, Value* value); |
| 467 |
| 468 PushArgumentInstr* PushArgument(Value* value); |
| 469 PushArgumentInstr* PushLocal(LocalVariable* local); |
| 470 |
| 471 // Load a number of characters at the given offset from the |
| 472 // current position, into the current-character register. |
| 473 void LoadCurrentCharacterUnchecked(intptr_t cp_offset, |
| 474 intptr_t character_count); |
| 475 |
| 476 // Returns the character within the passed string at the specified index. |
| 477 Value* CharacterAt(Definition* index); |
| 478 |
| 479 // Load a number of characters starting from index in the pattern string. |
| 480 Value* LoadCodeUnitsAt(Value* pattern, |
| 481 Value* index, |
| 482 intptr_t character_count); |
| 483 |
| 484 // Check whether preemption has been requested. |
| 485 void CheckPreemption(); |
| 486 |
| 487 // Byte size of chars in the string to match (decided by the Mode argument) |
| 488 inline intptr_t char_size() { return static_cast<int>(mode_); } |
| 489 |
| 490 // Equivalent to a conditional branch to the label, unless the label |
| 491 // is NULL, in which case it is a conditional Backtrack. |
| 492 void BranchOrBacktrack(ComparisonInstr* comparison, |
| 493 BlockLabel* true_successor); |
| 494 |
| 495 // Set up all local variables and parameters. |
| 496 void InitializeLocals(); |
| 497 |
| 498 // Allocates a new local, and returns the appropriate id for placing it |
| 499 // on the stack. |
| 500 intptr_t GetNextLocalIndex(); |
| 501 |
| 502 // We never have any copied parameters. |
| 503 intptr_t num_copied_params() const { |
| 504 return 0; |
| 505 } |
| 506 |
| 507 // Return the position register at the specified index, creating it if |
| 508 // necessary. Note that the number of such registers can exceed the amount |
| 509 // required by the number of output captures. |
| 510 LocalVariable* position_register(intptr_t index); |
| 511 |
| 512 void set_current_instruction(Instruction* instruction); |
| 513 |
| 514 // The following functions are responsible for appending instructions |
| 515 // to the current instruction in various ways. The most simple one |
| 516 // is AppendInstruction, which simply appends an instruction and performs |
| 517 // bookkeeping. |
| 518 void AppendInstruction(Instruction* instruction); |
| 519 // Similar to AppendInstruction, but closes the current block by |
| 520 // setting current_instruction_ to NULL. |
| 521 void CloseBlockWith(Instruction* instruction); |
| 522 // Appends definition and allocates a temp index for the result. |
| 523 Value* Bind(Definition* definition); |
| 524 // Loads and binds a local variable. |
| 525 Value* BindLoadLocal(const LocalVariable& local); |
| 526 |
| 527 // Appends the definition. |
| 528 void Do(Definition* definition); |
| 529 // Closes the current block with a jump to the specified block. |
| 530 void GoTo(JoinEntryInstr* to); |
| 531 |
| 532 // Accessors for our local stack_. |
| 533 void PushStack(Definition* definition); |
| 534 Value* PopStack(); |
| 535 |
| 536 // Prints the specified argument. Used for debugging. |
| 537 void Print(PushArgumentInstr* argument); |
| 538 |
| 539 // A utility class tracking ids of various objects such as blocks, temps, etc. |
| 540 class IdAllocator : public ValueObject { |
| 541 public: |
| 542 IdAllocator() : next_id(0) { } |
| 543 |
| 544 intptr_t Count() const { return next_id; } |
| 545 intptr_t Alloc(intptr_t count = 1) { |
| 546 ASSERT(count >= 0); |
| 547 intptr_t current_id = next_id; |
| 548 next_id += count; |
| 549 return current_id; |
| 550 } |
| 551 void Dealloc(intptr_t count = 1) { |
| 552 ASSERT(count <= next_id); |
| 553 next_id -= count; |
| 554 } |
| 555 |
| 556 private: |
| 557 intptr_t next_id; |
| 558 }; |
| 559 |
| 560 // Which mode to generate code for (ASCII or UC16). |
| 561 Mode mode_; |
| 562 |
| 563 // Which specific string class to generate code for. |
| 564 intptr_t specialization_cid_; |
| 565 |
| 566 // Block entries used internally. |
| 567 GraphEntryInstr* entry_block_; |
| 568 JoinEntryInstr* start_block_; |
| 569 JoinEntryInstr* success_block_; |
| 570 JoinEntryInstr* backtrack_block_; |
| 571 JoinEntryInstr* exit_block_; |
| 572 |
| 573 const ParsedFunction* parsed_function_; |
| 574 const ZoneGrowableArray<const ICData*>& ic_data_array_; |
| 575 |
| 576 // All created blocks are contained within this set. Used for printing |
| 577 // the generated code. |
| 578 GrowableArray<BlockEntryInstr*> blocks_; |
| 579 |
| 580 // The current instruction to link to when new code is emitted. |
| 581 Instruction* current_instruction_; |
| 582 |
| 583 // A list, acting as the runtime stack for both backtrack locations and |
| 584 // stored positions within the string. |
| 585 LocalVariable* stack_; |
| 586 |
| 587 // Stores the current character within the string. |
| 588 LocalVariable* current_character_; |
| 589 |
| 590 // Stores the current location within the string as a negative offset |
| 591 // from the end of the string. |
| 592 LocalVariable* current_position_; |
| 593 |
| 594 // The string being processed, passed as a function parameter. |
| 595 LocalVariable* string_param_; |
| 596 |
| 597 // Stores the length of string_param_. |
| 598 LocalVariable* string_param_length_; |
| 599 |
| 600 // The start index within the string, passed as a function parameter. |
| 601 LocalVariable* start_index_param_; |
| 602 |
| 603 // An assortment of utility variables. |
| 604 LocalVariable* capture_length_; |
| 605 LocalVariable* match_start_index_; |
| 606 LocalVariable* capture_start_index_; |
| 607 LocalVariable* match_end_index_; |
| 608 LocalVariable* char_in_capture_; |
| 609 LocalVariable* char_in_match_; |
| 610 |
| 611 LocalVariable* result_; |
| 612 |
| 613 // Stored positions containing group bounds. Generated as needed. |
| 614 const intptr_t position_registers_count_; |
| 615 GrowableArray<LocalVariable*> position_registers_; |
| 616 |
| 617 // The actual array object used as the stack. |
| 618 GrowableObjectArray& stack_array_; |
| 619 |
| 620 GrowableArray<IndirectGotoInstr*> igotos_; |
| 621 |
| 622 IdAllocator block_id_; |
| 623 IdAllocator temp_id_; |
| 624 IdAllocator arg_id_; |
| 625 IdAllocator local_id_; |
| 626 IdAllocator indirect_id_; |
| 627 }; |
| 628 |
164 | 629 |
165 } // namespace dart | 630 } // namespace dart |
166 | 631 |
167 #endif // VM_REGEXP_ASSEMBLER_H_ | 632 #endif // VM_REGEXP_ASSEMBLER_H_ |
OLD | NEW |