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