js/src/yarr/YarrJIT.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 *
michael@0 4 * Copyright (C) 2009 Apple Inc. All rights reserved.
michael@0 5 *
michael@0 6 * Redistribution and use in source and binary forms, with or without
michael@0 7 * modification, are permitted provided that the following conditions
michael@0 8 * are met:
michael@0 9 * 1. Redistributions of source code must retain the above copyright
michael@0 10 * notice, this list of conditions and the following disclaimer.
michael@0 11 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 12 * notice, this list of conditions and the following disclaimer in the
michael@0 13 * documentation and/or other materials provided with the distribution.
michael@0 14 *
michael@0 15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
michael@0 16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
michael@0 18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
michael@0 19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0 20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0 22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
michael@0 23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 26 */
michael@0 27
michael@0 28 #include "yarr/YarrJIT.h"
michael@0 29
michael@0 30 #include "assembler/assembler/LinkBuffer.h"
michael@0 31 #include "yarr/Yarr.h"
michael@0 32 #include "yarr/YarrCanonicalizeUCS2.h"
michael@0 33
michael@0 34 #if ENABLE_YARR_JIT
michael@0 35
michael@0 36 using namespace WTF;
michael@0 37
michael@0 38 namespace JSC { namespace Yarr {
michael@0 39
michael@0 40 template<YarrJITCompileMode compileMode>
michael@0 41 class YarrGenerator : private MacroAssembler {
michael@0 42 friend void jitCompile(JSGlobalData*, YarrCodeBlock& jitObject, const String& pattern, unsigned& numSubpatterns, const char*& error, bool ignoreCase, bool multiline);
michael@0 43
michael@0 44 #if WTF_CPU_ARM
michael@0 45 static const RegisterID input = ARMRegisters::r0;
michael@0 46 static const RegisterID index = ARMRegisters::r1;
michael@0 47 static const RegisterID length = ARMRegisters::r2;
michael@0 48 static const RegisterID output = ARMRegisters::r4;
michael@0 49
michael@0 50 static const RegisterID regT0 = ARMRegisters::r5;
michael@0 51 static const RegisterID regT1 = ARMRegisters::r6;
michael@0 52
michael@0 53 static const RegisterID returnRegister = ARMRegisters::r0;
michael@0 54 static const RegisterID returnRegister2 = ARMRegisters::r1;
michael@0 55 #elif WTF_CPU_MIPS
michael@0 56 static const RegisterID input = MIPSRegisters::a0;
michael@0 57 static const RegisterID index = MIPSRegisters::a1;
michael@0 58 static const RegisterID length = MIPSRegisters::a2;
michael@0 59 static const RegisterID output = MIPSRegisters::a3;
michael@0 60
michael@0 61 static const RegisterID regT0 = MIPSRegisters::t4;
michael@0 62 static const RegisterID regT1 = MIPSRegisters::t5;
michael@0 63
michael@0 64 static const RegisterID returnRegister = MIPSRegisters::v0;
michael@0 65 static const RegisterID returnRegister2 = MIPSRegisters::v1;
michael@0 66 #elif WTF_CPU_SH4
michael@0 67 static const RegisterID input = SH4Registers::r4;
michael@0 68 static const RegisterID index = SH4Registers::r5;
michael@0 69 static const RegisterID length = SH4Registers::r6;
michael@0 70 static const RegisterID output = SH4Registers::r7;
michael@0 71
michael@0 72 static const RegisterID regT0 = SH4Registers::r0;
michael@0 73 static const RegisterID regT1 = SH4Registers::r1;
michael@0 74
michael@0 75 static const RegisterID returnRegister = SH4Registers::r0;
michael@0 76 static const RegisterID returnRegister2 = SH4Registers::r1;
michael@0 77 #elif WTF_CPU_SPARC
michael@0 78 static const RegisterID input = SparcRegisters::i0;
michael@0 79 static const RegisterID index = SparcRegisters::i1;
michael@0 80 static const RegisterID length = SparcRegisters::i2;
michael@0 81 static const RegisterID output = SparcRegisters::i3;
michael@0 82
michael@0 83 static const RegisterID regT0 = SparcRegisters::i4;
michael@0 84 static const RegisterID regT1 = SparcRegisters::i5;
michael@0 85
michael@0 86 static const RegisterID returnRegister = SparcRegisters::i0;
michael@0 87 #elif WTF_CPU_X86
michael@0 88 static const RegisterID input = X86Registers::eax;
michael@0 89 static const RegisterID index = X86Registers::edx;
michael@0 90 static const RegisterID length = X86Registers::ecx;
michael@0 91 static const RegisterID output = X86Registers::edi;
michael@0 92
michael@0 93 static const RegisterID regT0 = X86Registers::ebx;
michael@0 94 static const RegisterID regT1 = X86Registers::esi;
michael@0 95
michael@0 96 static const RegisterID returnRegister = X86Registers::eax;
michael@0 97 static const RegisterID returnRegister2 = X86Registers::edx;
michael@0 98 #elif WTF_CPU_X86_64
michael@0 99 # if WTF_PLATFORM_WIN
michael@0 100 static const RegisterID input = X86Registers::ecx;
michael@0 101 static const RegisterID index = X86Registers::edx;
michael@0 102 static const RegisterID length = X86Registers::r8;
michael@0 103 static const RegisterID output = X86Registers::r9;
michael@0 104 # else
michael@0 105 static const RegisterID input = X86Registers::edi;
michael@0 106 static const RegisterID index = X86Registers::esi;
michael@0 107 static const RegisterID length = X86Registers::edx;
michael@0 108 static const RegisterID output = X86Registers::ecx;
michael@0 109 # endif
michael@0 110
michael@0 111 static const RegisterID regT0 = X86Registers::eax;
michael@0 112 static const RegisterID regT1 = X86Registers::ebx;
michael@0 113
michael@0 114 static const RegisterID returnRegister = X86Registers::eax;
michael@0 115
michael@0 116 # if !WTF_PLATFORM_WIN
michael@0 117 // no way to use int128_t as return value on Win64 ABI
michael@0 118 static const RegisterID returnRegister2 = X86Registers::edx;
michael@0 119 # endif
michael@0 120 #endif
michael@0 121
michael@0 122 void optimizeAlternative(PatternAlternative* alternative)
michael@0 123 {
michael@0 124 if (!alternative->m_terms.size())
michael@0 125 return;
michael@0 126
michael@0 127 for (unsigned i = 0; i < alternative->m_terms.size() - 1; ++i) {
michael@0 128 PatternTerm& term = alternative->m_terms[i];
michael@0 129 PatternTerm& nextTerm = alternative->m_terms[i + 1];
michael@0 130
michael@0 131 if ((term.type == PatternTerm::TypeCharacterClass)
michael@0 132 && (term.quantityType == QuantifierFixedCount)
michael@0 133 && (nextTerm.type == PatternTerm::TypePatternCharacter)
michael@0 134 && (nextTerm.quantityType == QuantifierFixedCount)) {
michael@0 135 PatternTerm termCopy = term;
michael@0 136 alternative->m_terms[i] = nextTerm;
michael@0 137 alternative->m_terms[i + 1] = termCopy;
michael@0 138 }
michael@0 139 }
michael@0 140 }
michael@0 141
michael@0 142 void matchCharacterClassRange(RegisterID character, JumpList& failures, JumpList& matchDest, const CharacterRange* ranges, unsigned count, unsigned* matchIndex, const UChar* matches, unsigned matchCount)
michael@0 143 {
michael@0 144 do {
michael@0 145 // pick which range we're going to generate
michael@0 146 int which = count >> 1;
michael@0 147 char lo = ranges[which].begin;
michael@0 148 char hi = ranges[which].end;
michael@0 149
michael@0 150 // check if there are any ranges or matches below lo. If not, just jl to failure -
michael@0 151 // if there is anything else to check, check that first, if it falls through jmp to failure.
michael@0 152 if ((*matchIndex < matchCount) && (matches[*matchIndex] < lo)) {
michael@0 153 Jump loOrAbove = branch32(GreaterThanOrEqual, character, Imm32((unsigned short)lo));
michael@0 154
michael@0 155 // generate code for all ranges before this one
michael@0 156 if (which)
michael@0 157 matchCharacterClassRange(character, failures, matchDest, ranges, which, matchIndex, matches, matchCount);
michael@0 158
michael@0 159 while ((*matchIndex < matchCount) && (matches[*matchIndex] < lo)) {
michael@0 160 matchDest.append(branch32(Equal, character, Imm32((unsigned short)matches[*matchIndex])));
michael@0 161 ++*matchIndex;
michael@0 162 }
michael@0 163 failures.append(jump());
michael@0 164
michael@0 165 loOrAbove.link(this);
michael@0 166 } else if (which) {
michael@0 167 Jump loOrAbove = branch32(GreaterThanOrEqual, character, Imm32((unsigned short)lo));
michael@0 168
michael@0 169 matchCharacterClassRange(character, failures, matchDest, ranges, which, matchIndex, matches, matchCount);
michael@0 170 failures.append(jump());
michael@0 171
michael@0 172 loOrAbove.link(this);
michael@0 173 } else
michael@0 174 failures.append(branch32(LessThan, character, Imm32((unsigned short)lo)));
michael@0 175
michael@0 176 while ((*matchIndex < matchCount) && (matches[*matchIndex] <= hi))
michael@0 177 ++*matchIndex;
michael@0 178
michael@0 179 matchDest.append(branch32(LessThanOrEqual, character, Imm32((unsigned short)hi)));
michael@0 180 // fall through to here, the value is above hi.
michael@0 181
michael@0 182 // shuffle along & loop around if there are any more matches to handle.
michael@0 183 unsigned next = which + 1;
michael@0 184 ranges += next;
michael@0 185 count -= next;
michael@0 186 } while (count);
michael@0 187 }
michael@0 188
michael@0 189 void matchCharacterClass(RegisterID character, JumpList& matchDest, const CharacterClass* charClass)
michael@0 190 {
michael@0 191 if (charClass->m_table) {
michael@0 192 ExtendedAddress tableEntry(character, reinterpret_cast<intptr_t>(charClass->m_table));
michael@0 193 matchDest.append(branchTest8(charClass->m_tableInverted ? Zero : NonZero, tableEntry));
michael@0 194 return;
michael@0 195 }
michael@0 196 Jump unicodeFail;
michael@0 197 if (charClass->m_matchesUnicode.size() || charClass->m_rangesUnicode.size()) {
michael@0 198 Jump isAscii = branch32(LessThanOrEqual, character, TrustedImm32(0x7f));
michael@0 199
michael@0 200 if (charClass->m_matchesUnicode.size()) {
michael@0 201 for (unsigned i = 0; i < charClass->m_matchesUnicode.size(); ++i) {
michael@0 202 UChar ch = charClass->m_matchesUnicode[i];
michael@0 203 matchDest.append(branch32(Equal, character, Imm32(ch)));
michael@0 204 }
michael@0 205 }
michael@0 206
michael@0 207 if (charClass->m_rangesUnicode.size()) {
michael@0 208 for (unsigned i = 0; i < charClass->m_rangesUnicode.size(); ++i) {
michael@0 209 UChar lo = charClass->m_rangesUnicode[i].begin;
michael@0 210 UChar hi = charClass->m_rangesUnicode[i].end;
michael@0 211
michael@0 212 Jump below = branch32(LessThan, character, Imm32(lo));
michael@0 213 matchDest.append(branch32(LessThanOrEqual, character, Imm32(hi)));
michael@0 214 below.link(this);
michael@0 215 }
michael@0 216 }
michael@0 217
michael@0 218 unicodeFail = jump();
michael@0 219 isAscii.link(this);
michael@0 220 }
michael@0 221
michael@0 222 if (charClass->m_ranges.size()) {
michael@0 223 unsigned matchIndex = 0;
michael@0 224 JumpList failures;
michael@0 225 matchCharacterClassRange(character, failures, matchDest, charClass->m_ranges.begin(), charClass->m_ranges.size(), &matchIndex, charClass->m_matches.begin(), charClass->m_matches.size());
michael@0 226 while (matchIndex < charClass->m_matches.size())
michael@0 227 matchDest.append(branch32(Equal, character, Imm32((unsigned short)charClass->m_matches[matchIndex++])));
michael@0 228
michael@0 229 failures.link(this);
michael@0 230 } else if (charClass->m_matches.size()) {
michael@0 231 // optimization: gather 'a','A' etc back together, can mask & test once.
michael@0 232 Vector<char> matchesAZaz;
michael@0 233
michael@0 234 for (unsigned i = 0; i < charClass->m_matches.size(); ++i) {
michael@0 235 char ch = charClass->m_matches[i];
michael@0 236 if (m_pattern.m_ignoreCase) {
michael@0 237 if (isASCIILower(ch)) {
michael@0 238 matchesAZaz.append(ch);
michael@0 239 continue;
michael@0 240 }
michael@0 241 if (isASCIIUpper(ch))
michael@0 242 continue;
michael@0 243 }
michael@0 244 matchDest.append(branch32(Equal, character, Imm32((unsigned short)ch)));
michael@0 245 }
michael@0 246
michael@0 247 if (unsigned countAZaz = matchesAZaz.size()) {
michael@0 248 or32(TrustedImm32(32), character);
michael@0 249 for (unsigned i = 0; i < countAZaz; ++i)
michael@0 250 matchDest.append(branch32(Equal, character, TrustedImm32(matchesAZaz[i])));
michael@0 251 }
michael@0 252 }
michael@0 253
michael@0 254 if (charClass->m_matchesUnicode.size() || charClass->m_rangesUnicode.size())
michael@0 255 unicodeFail.link(this);
michael@0 256 }
michael@0 257
michael@0 258 // Jumps if input not available; will have (incorrectly) incremented already!
michael@0 259 Jump jumpIfNoAvailableInput(unsigned countToCheck = 0)
michael@0 260 {
michael@0 261 if (countToCheck)
michael@0 262 add32(Imm32(countToCheck), index);
michael@0 263 return branch32(Above, index, length);
michael@0 264 }
michael@0 265
michael@0 266 Jump jumpIfAvailableInput(unsigned countToCheck)
michael@0 267 {
michael@0 268 add32(Imm32(countToCheck), index);
michael@0 269 return branch32(BelowOrEqual, index, length);
michael@0 270 }
michael@0 271
michael@0 272 Jump checkInput()
michael@0 273 {
michael@0 274 return branch32(BelowOrEqual, index, length);
michael@0 275 }
michael@0 276
michael@0 277 Jump atEndOfInput()
michael@0 278 {
michael@0 279 return branch32(Equal, index, length);
michael@0 280 }
michael@0 281
michael@0 282 Jump notAtEndOfInput()
michael@0 283 {
michael@0 284 return branch32(NotEqual, index, length);
michael@0 285 }
michael@0 286
michael@0 287 Jump jumpIfCharNotEquals(UChar ch, int inputPosition, RegisterID character)
michael@0 288 {
michael@0 289 readCharacter(inputPosition, character);
michael@0 290
michael@0 291 // For case-insesitive compares, non-ascii characters that have different
michael@0 292 // upper & lower case representations are converted to a character class.
michael@0 293 ASSERT(!m_pattern.m_ignoreCase || isASCIIAlpha(ch) || isCanonicallyUnique(ch));
michael@0 294 if (m_pattern.m_ignoreCase && isASCIIAlpha(ch)) {
michael@0 295 or32(TrustedImm32(0x20), character);
michael@0 296 ch |= 0x20;
michael@0 297 }
michael@0 298
michael@0 299 return branch32(NotEqual, character, Imm32(ch));
michael@0 300 }
michael@0 301
michael@0 302 void readCharacter(int inputPosition, RegisterID reg)
michael@0 303 {
michael@0 304 if (m_charSize == Char8)
michael@0 305 load8(BaseIndex(input, index, TimesOne, inputPosition * sizeof(char)), reg);
michael@0 306 else
michael@0 307 load16(BaseIndex(input, index, TimesTwo, inputPosition * sizeof(UChar)), reg);
michael@0 308 }
michael@0 309
michael@0 310 void storeToFrame(RegisterID reg, unsigned frameLocation)
michael@0 311 {
michael@0 312 poke(reg, frameLocation);
michael@0 313 }
michael@0 314
michael@0 315 void storeToFrame(TrustedImm32 imm, unsigned frameLocation)
michael@0 316 {
michael@0 317 poke(imm, frameLocation);
michael@0 318 }
michael@0 319
michael@0 320 DataLabelPtr storeToFrameWithPatch(unsigned frameLocation)
michael@0 321 {
michael@0 322 return storePtrWithPatch(TrustedImmPtr(0), Address(stackPointerRegister, frameLocation * sizeof(void*)));
michael@0 323 }
michael@0 324
michael@0 325 void loadFromFrame(unsigned frameLocation, RegisterID reg)
michael@0 326 {
michael@0 327 peek(reg, frameLocation);
michael@0 328 }
michael@0 329
michael@0 330 void loadFromFrameAndJump(unsigned frameLocation)
michael@0 331 {
michael@0 332 jump(Address(stackPointerRegister, frameLocation * sizeof(void*)));
michael@0 333 }
michael@0 334
michael@0 335 void initCallFrame()
michael@0 336 {
michael@0 337 unsigned callFrameSize = m_pattern.m_body->m_callFrameSize;
michael@0 338 if (callFrameSize)
michael@0 339 subPtr(Imm32(callFrameSize * sizeof(void*)), stackPointerRegister);
michael@0 340 }
michael@0 341 void removeCallFrame()
michael@0 342 {
michael@0 343 unsigned callFrameSize = m_pattern.m_body->m_callFrameSize;
michael@0 344 if (callFrameSize)
michael@0 345 addPtr(Imm32(callFrameSize * sizeof(void*)), stackPointerRegister);
michael@0 346 }
michael@0 347
michael@0 348 // Used to record subpatters, should only be called if compileMode is IncludeSubpatterns.
michael@0 349 void setSubpatternStart(RegisterID reg, unsigned subpattern)
michael@0 350 {
michael@0 351 ASSERT(subpattern);
michael@0 352 // FIXME: should be able to ASSERT(compileMode == IncludeSubpatterns), but then this function is conditionally NORETURN. :-(
michael@0 353 store32(reg, Address(output, (subpattern << 1) * sizeof(int)));
michael@0 354 }
michael@0 355 void setSubpatternEnd(RegisterID reg, unsigned subpattern)
michael@0 356 {
michael@0 357 ASSERT(subpattern);
michael@0 358 // FIXME: should be able to ASSERT(compileMode == IncludeSubpatterns), but then this function is conditionally NORETURN. :-(
michael@0 359 store32(reg, Address(output, ((subpattern << 1) + 1) * sizeof(int)));
michael@0 360 }
michael@0 361 void clearSubpatternStart(unsigned subpattern)
michael@0 362 {
michael@0 363 ASSERT(subpattern);
michael@0 364 // FIXME: should be able to ASSERT(compileMode == IncludeSubpatterns), but then this function is conditionally NORETURN. :-(
michael@0 365 store32(TrustedImm32(-1), Address(output, (subpattern << 1) * sizeof(int)));
michael@0 366 }
michael@0 367
michael@0 368 // We use one of three different strategies to track the start of the current match,
michael@0 369 // while matching.
michael@0 370 // 1) If the pattern has a fixed size, do nothing! - we calculate the value lazily
michael@0 371 // at the end of matching. This is irrespective of compileMode, and in this case
michael@0 372 // these methods should never be called.
michael@0 373 // 2) If we're compiling IncludeSubpatterns, 'output' contains a pointer to an output
michael@0 374 // vector, store the match start in the output vector.
michael@0 375 // 3) If we're compiling MatchOnly, 'output' is unused, store the match start directly
michael@0 376 // in this register.
michael@0 377 void setMatchStart(RegisterID reg)
michael@0 378 {
michael@0 379 ASSERT(!m_pattern.m_body->m_hasFixedSize);
michael@0 380 if (compileMode == IncludeSubpatterns)
michael@0 381 store32(reg, output);
michael@0 382 else
michael@0 383 move(reg, output);
michael@0 384 }
michael@0 385 void getMatchStart(RegisterID reg)
michael@0 386 {
michael@0 387 ASSERT(!m_pattern.m_body->m_hasFixedSize);
michael@0 388 if (compileMode == IncludeSubpatterns)
michael@0 389 load32(output, reg);
michael@0 390 else
michael@0 391 move(output, reg);
michael@0 392 }
michael@0 393
michael@0 394 enum YarrOpCode {
michael@0 395 // These nodes wrap body alternatives - those in the main disjunction,
michael@0 396 // rather than subpatterns or assertions. These are chained together in
michael@0 397 // a doubly linked list, with a 'begin' node for the first alternative,
michael@0 398 // a 'next' node for each subsequent alternative, and an 'end' node at
michael@0 399 // the end. In the case of repeating alternatives, the 'end' node also
michael@0 400 // has a reference back to 'begin'.
michael@0 401 OpBodyAlternativeBegin,
michael@0 402 OpBodyAlternativeNext,
michael@0 403 OpBodyAlternativeEnd,
michael@0 404 // Similar to the body alternatives, but used for subpatterns with two
michael@0 405 // or more alternatives.
michael@0 406 OpNestedAlternativeBegin,
michael@0 407 OpNestedAlternativeNext,
michael@0 408 OpNestedAlternativeEnd,
michael@0 409 // Used for alternatives in subpatterns where there is only a single
michael@0 410 // alternative (backtrackingis easier in these cases), or for alternatives
michael@0 411 // which never need to be backtracked (those in parenthetical assertions,
michael@0 412 // terminal subpatterns).
michael@0 413 OpSimpleNestedAlternativeBegin,
michael@0 414 OpSimpleNestedAlternativeNext,
michael@0 415 OpSimpleNestedAlternativeEnd,
michael@0 416 // Used to wrap 'Once' subpattern matches (quantityCount == 1).
michael@0 417 OpParenthesesSubpatternOnceBegin,
michael@0 418 OpParenthesesSubpatternOnceEnd,
michael@0 419 // Used to wrap 'Terminal' subpattern matches (at the end of the regexp).
michael@0 420 OpParenthesesSubpatternTerminalBegin,
michael@0 421 OpParenthesesSubpatternTerminalEnd,
michael@0 422 // Used to wrap parenthetical assertions.
michael@0 423 OpParentheticalAssertionBegin,
michael@0 424 OpParentheticalAssertionEnd,
michael@0 425 // Wraps all simple terms (pattern characters, character classes).
michael@0 426 OpTerm,
michael@0 427 // Where an expression contains only 'once through' body alternatives
michael@0 428 // and no repeating ones, this op is used to return match failure.
michael@0 429 OpMatchFailed
michael@0 430 };
michael@0 431
michael@0 432 // This structure is used to hold the compiled opcode information,
michael@0 433 // including reference back to the original PatternTerm/PatternAlternatives,
michael@0 434 // and JIT compilation data structures.
michael@0 435 struct YarrOp {
michael@0 436 explicit YarrOp(PatternTerm* term)
michael@0 437 : m_op(OpTerm)
michael@0 438 , m_term(term)
michael@0 439 , m_isDeadCode(false)
michael@0 440 {
michael@0 441 }
michael@0 442
michael@0 443 explicit YarrOp(YarrOpCode op)
michael@0 444 : m_op(op)
michael@0 445 , m_isDeadCode(false)
michael@0 446 {
michael@0 447 }
michael@0 448
michael@0 449 // The operation, as a YarrOpCode, and also a reference to the PatternTerm.
michael@0 450 YarrOpCode m_op;
michael@0 451 PatternTerm* m_term;
michael@0 452
michael@0 453 // For alternatives, this holds the PatternAlternative and doubly linked
michael@0 454 // references to this alternative's siblings. In the case of the
michael@0 455 // OpBodyAlternativeEnd node at the end of a section of repeating nodes,
michael@0 456 // m_nextOp will reference the OpBodyAlternativeBegin node of the first
michael@0 457 // repeating alternative.
michael@0 458 PatternAlternative* m_alternative;
michael@0 459 size_t m_previousOp;
michael@0 460 size_t m_nextOp;
michael@0 461
michael@0 462 // Used to record a set of Jumps out of the generated code, typically
michael@0 463 // used for jumps out to backtracking code, and a single reentry back
michael@0 464 // into the code for a node (likely where a backtrack will trigger
michael@0 465 // rematching).
michael@0 466 Label m_reentry;
michael@0 467 JumpList m_jumps;
michael@0 468
michael@0 469 // Used for backtracking when the prior alternative did not consume any
michael@0 470 // characters but matched.
michael@0 471 Jump m_zeroLengthMatch;
michael@0 472
michael@0 473 // This flag is used to null out the second pattern character, when
michael@0 474 // two are fused to match a pair together.
michael@0 475 bool m_isDeadCode;
michael@0 476
michael@0 477 // Currently used in the case of some of the more complex management of
michael@0 478 // 'm_checked', to cache the offset used in this alternative, to avoid
michael@0 479 // recalculating it.
michael@0 480 int m_checkAdjust;
michael@0 481
michael@0 482 // Used by OpNestedAlternativeNext/End to hold the pointer to the
michael@0 483 // value that will be pushed into the pattern's frame to return to,
michael@0 484 // upon backtracking back into the disjunction.
michael@0 485 DataLabelPtr m_returnAddress;
michael@0 486 };
michael@0 487
michael@0 488 // BacktrackingState
michael@0 489 // This class encapsulates information about the state of code generation
michael@0 490 // whilst generating the code for backtracking, when a term fails to match.
michael@0 491 // Upon entry to code generation of the backtracking code for a given node,
michael@0 492 // the Backtracking state will hold references to all control flow sources
michael@0 493 // that are outputs in need of further backtracking from the prior node
michael@0 494 // generated (which is the subsequent operation in the regular expression,
michael@0 495 // and in the m_ops Vector, since we generated backtracking backwards).
michael@0 496 // These references to control flow take the form of:
michael@0 497 // - A jump list of jumps, to be linked to code that will backtrack them
michael@0 498 // further.
michael@0 499 // - A set of DataLabelPtr values, to be populated with values to be
michael@0 500 // treated effectively as return addresses backtracking into complex
michael@0 501 // subpatterns.
michael@0 502 // - A flag indicating that the current sequence of generated code up to
michael@0 503 // this point requires backtracking.
michael@0 504 class BacktrackingState {
michael@0 505 public:
michael@0 506 BacktrackingState()
michael@0 507 : m_pendingFallthrough(false)
michael@0 508 {
michael@0 509 }
michael@0 510
michael@0 511 // Add a jump or jumps, a return address, or set the flag indicating
michael@0 512 // that the current 'fallthrough' control flow requires backtracking.
michael@0 513 void append(const Jump& jump)
michael@0 514 {
michael@0 515 m_laterFailures.append(jump);
michael@0 516 }
michael@0 517 void append(JumpList& jumpList)
michael@0 518 {
michael@0 519 m_laterFailures.append(jumpList);
michael@0 520 }
michael@0 521 void append(const DataLabelPtr& returnAddress)
michael@0 522 {
michael@0 523 m_pendingReturns.append(returnAddress);
michael@0 524 }
michael@0 525 void fallthrough()
michael@0 526 {
michael@0 527 ASSERT(!m_pendingFallthrough);
michael@0 528 m_pendingFallthrough = true;
michael@0 529 }
michael@0 530
michael@0 531 // These methods clear the backtracking state, either linking to the
michael@0 532 // current location, a provided label, or copying the backtracking out
michael@0 533 // to a JumpList. All actions may require code generation to take place,
michael@0 534 // and as such are passed a pointer to the assembler.
michael@0 535 void link(MacroAssembler* assembler)
michael@0 536 {
michael@0 537 if (m_pendingReturns.size()) {
michael@0 538 Label here(assembler);
michael@0 539 for (unsigned i = 0; i < m_pendingReturns.size(); ++i)
michael@0 540 m_backtrackRecords.append(ReturnAddressRecord(m_pendingReturns[i], here));
michael@0 541 m_pendingReturns.clear();
michael@0 542 }
michael@0 543 m_laterFailures.link(assembler);
michael@0 544 m_laterFailures.clear();
michael@0 545 m_pendingFallthrough = false;
michael@0 546 }
michael@0 547 void linkTo(Label label, MacroAssembler* assembler)
michael@0 548 {
michael@0 549 if (m_pendingReturns.size()) {
michael@0 550 for (unsigned i = 0; i < m_pendingReturns.size(); ++i)
michael@0 551 m_backtrackRecords.append(ReturnAddressRecord(m_pendingReturns[i], label));
michael@0 552 m_pendingReturns.clear();
michael@0 553 }
michael@0 554 if (m_pendingFallthrough)
michael@0 555 assembler->jump(label);
michael@0 556 m_laterFailures.linkTo(label, assembler);
michael@0 557 m_laterFailures.clear();
michael@0 558 m_pendingFallthrough = false;
michael@0 559 }
michael@0 560 void takeBacktracksToJumpList(JumpList& jumpList, MacroAssembler* assembler)
michael@0 561 {
michael@0 562 if (m_pendingReturns.size()) {
michael@0 563 Label here(assembler);
michael@0 564 for (unsigned i = 0; i < m_pendingReturns.size(); ++i)
michael@0 565 m_backtrackRecords.append(ReturnAddressRecord(m_pendingReturns[i], here));
michael@0 566 m_pendingReturns.clear();
michael@0 567 m_pendingFallthrough = true;
michael@0 568 }
michael@0 569 if (m_pendingFallthrough)
michael@0 570 jumpList.append(assembler->jump());
michael@0 571 jumpList.append(m_laterFailures);
michael@0 572 m_laterFailures.clear();
michael@0 573 m_pendingFallthrough = false;
michael@0 574 }
michael@0 575
michael@0 576 bool isEmpty()
michael@0 577 {
michael@0 578 return m_laterFailures.empty() && m_pendingReturns.isEmpty() && !m_pendingFallthrough;
michael@0 579 }
michael@0 580
michael@0 581 // Called at the end of code generation to link all return addresses.
michael@0 582 void linkDataLabels(LinkBuffer& linkBuffer)
michael@0 583 {
michael@0 584 ASSERT(isEmpty());
michael@0 585 for (unsigned i = 0; i < m_backtrackRecords.size(); ++i)
michael@0 586 linkBuffer.patch(m_backtrackRecords[i].m_dataLabel, linkBuffer.locationOf(m_backtrackRecords[i].m_backtrackLocation));
michael@0 587 }
michael@0 588
michael@0 589 private:
michael@0 590 struct ReturnAddressRecord {
michael@0 591 ReturnAddressRecord(DataLabelPtr dataLabel, Label backtrackLocation)
michael@0 592 : m_dataLabel(dataLabel)
michael@0 593 , m_backtrackLocation(backtrackLocation)
michael@0 594 {
michael@0 595 }
michael@0 596
michael@0 597 DataLabelPtr m_dataLabel;
michael@0 598 Label m_backtrackLocation;
michael@0 599 };
michael@0 600
michael@0 601 JumpList m_laterFailures;
michael@0 602 bool m_pendingFallthrough;
michael@0 603 Vector<DataLabelPtr, 4> m_pendingReturns;
michael@0 604 Vector<ReturnAddressRecord, 4> m_backtrackRecords;
michael@0 605 };
michael@0 606
michael@0 607 // Generation methods:
michael@0 608 // ===================
michael@0 609
michael@0 610 // This method provides a default implementation of backtracking common
michael@0 611 // to many terms; terms commonly jump out of the forwards matching path
michael@0 612 // on any failed conditions, and add these jumps to the m_jumps list. If
michael@0 613 // no special handling is required we can often just backtrack to m_jumps.
michael@0 614 bool backtrackTermDefault(size_t opIndex)
michael@0 615 {
michael@0 616 YarrOp& op = m_ops[opIndex];
michael@0 617 m_backtrackingState.append(op.m_jumps);
michael@0 618 return true;
michael@0 619 }
michael@0 620
michael@0 621 bool generateAssertionBOL(size_t opIndex)
michael@0 622 {
michael@0 623 YarrOp& op = m_ops[opIndex];
michael@0 624 PatternTerm* term = op.m_term;
michael@0 625
michael@0 626 if (m_pattern.m_multiline) {
michael@0 627 const RegisterID character = regT0;
michael@0 628
michael@0 629 JumpList matchDest;
michael@0 630 if (!term->inputPosition)
michael@0 631 matchDest.append(branch32(Equal, index, Imm32(m_checked)));
michael@0 632
michael@0 633 readCharacter((term->inputPosition - m_checked) - 1, character);
michael@0 634 matchCharacterClass(character, matchDest, m_pattern.newlineCharacterClass());
michael@0 635 op.m_jumps.append(jump());
michael@0 636
michael@0 637 matchDest.link(this);
michael@0 638 } else {
michael@0 639 // Erk, really should poison out these alternatives early. :-/
michael@0 640 if (term->inputPosition)
michael@0 641 op.m_jumps.append(jump());
michael@0 642 else
michael@0 643 op.m_jumps.append(branch32(NotEqual, index, Imm32(m_checked)));
michael@0 644 }
michael@0 645 return true;
michael@0 646 }
michael@0 647 bool backtrackAssertionBOL(size_t opIndex)
michael@0 648 {
michael@0 649 return backtrackTermDefault(opIndex);
michael@0 650 }
michael@0 651
michael@0 652 bool generateAssertionEOL(size_t opIndex)
michael@0 653 {
michael@0 654 YarrOp& op = m_ops[opIndex];
michael@0 655 PatternTerm* term = op.m_term;
michael@0 656
michael@0 657 if (m_pattern.m_multiline) {
michael@0 658 const RegisterID character = regT0;
michael@0 659
michael@0 660 JumpList matchDest;
michael@0 661 if (term->inputPosition == m_checked)
michael@0 662 matchDest.append(atEndOfInput());
michael@0 663
michael@0 664 readCharacter(term->inputPosition - m_checked, character);
michael@0 665 matchCharacterClass(character, matchDest, m_pattern.newlineCharacterClass());
michael@0 666 op.m_jumps.append(jump());
michael@0 667
michael@0 668 matchDest.link(this);
michael@0 669 } else {
michael@0 670 if (term->inputPosition == m_checked)
michael@0 671 op.m_jumps.append(notAtEndOfInput());
michael@0 672 // Erk, really should poison out these alternatives early. :-/
michael@0 673 else
michael@0 674 op.m_jumps.append(jump());
michael@0 675 }
michael@0 676 return true;
michael@0 677 }
michael@0 678 bool backtrackAssertionEOL(size_t opIndex)
michael@0 679 {
michael@0 680 return backtrackTermDefault(opIndex);
michael@0 681 }
michael@0 682
michael@0 683 // Also falls though on nextIsNotWordChar.
michael@0 684 void matchAssertionWordchar(size_t opIndex, JumpList& nextIsWordChar, JumpList& nextIsNotWordChar)
michael@0 685 {
michael@0 686 YarrOp& op = m_ops[opIndex];
michael@0 687 PatternTerm* term = op.m_term;
michael@0 688
michael@0 689 const RegisterID character = regT0;
michael@0 690
michael@0 691 if (term->inputPosition == m_checked)
michael@0 692 nextIsNotWordChar.append(atEndOfInput());
michael@0 693
michael@0 694 readCharacter((term->inputPosition - m_checked), character);
michael@0 695 matchCharacterClass(character, nextIsWordChar, m_pattern.wordcharCharacterClass());
michael@0 696 }
michael@0 697
michael@0 698 bool generateAssertionWordBoundary(size_t opIndex)
michael@0 699 {
michael@0 700 YarrOp& op = m_ops[opIndex];
michael@0 701 PatternTerm* term = op.m_term;
michael@0 702
michael@0 703 const RegisterID character = regT0;
michael@0 704
michael@0 705 Jump atBegin;
michael@0 706 JumpList matchDest;
michael@0 707 if (!term->inputPosition)
michael@0 708 atBegin = branch32(Equal, index, Imm32(m_checked));
michael@0 709 readCharacter((term->inputPosition - m_checked) - 1, character);
michael@0 710 matchCharacterClass(character, matchDest, m_pattern.wordcharCharacterClass());
michael@0 711 if (!term->inputPosition)
michael@0 712 atBegin.link(this);
michael@0 713
michael@0 714 // We fall through to here if the last character was not a wordchar.
michael@0 715 JumpList nonWordCharThenWordChar;
michael@0 716 JumpList nonWordCharThenNonWordChar;
michael@0 717 if (term->invert()) {
michael@0 718 matchAssertionWordchar(opIndex, nonWordCharThenNonWordChar, nonWordCharThenWordChar);
michael@0 719 nonWordCharThenWordChar.append(jump());
michael@0 720 } else {
michael@0 721 matchAssertionWordchar(opIndex, nonWordCharThenWordChar, nonWordCharThenNonWordChar);
michael@0 722 nonWordCharThenNonWordChar.append(jump());
michael@0 723 }
michael@0 724 op.m_jumps.append(nonWordCharThenNonWordChar);
michael@0 725
michael@0 726 // We jump here if the last character was a wordchar.
michael@0 727 matchDest.link(this);
michael@0 728 JumpList wordCharThenWordChar;
michael@0 729 JumpList wordCharThenNonWordChar;
michael@0 730 if (term->invert()) {
michael@0 731 matchAssertionWordchar(opIndex, wordCharThenNonWordChar, wordCharThenWordChar);
michael@0 732 wordCharThenWordChar.append(jump());
michael@0 733 } else {
michael@0 734 matchAssertionWordchar(opIndex, wordCharThenWordChar, wordCharThenNonWordChar);
michael@0 735 // This can fall-though!
michael@0 736 }
michael@0 737
michael@0 738 op.m_jumps.append(wordCharThenWordChar);
michael@0 739
michael@0 740 nonWordCharThenWordChar.link(this);
michael@0 741 wordCharThenNonWordChar.link(this);
michael@0 742 return true;
michael@0 743 }
michael@0 744 bool backtrackAssertionWordBoundary(size_t opIndex)
michael@0 745 {
michael@0 746 return backtrackTermDefault(opIndex);
michael@0 747 }
michael@0 748
michael@0 749 bool generatePatternCharacterOnce(size_t opIndex)
michael@0 750 {
michael@0 751 YarrOp& op = m_ops[opIndex];
michael@0 752
michael@0 753 if (op.m_isDeadCode)
michael@0 754 return true;
michael@0 755
michael@0 756 // m_ops always ends with a OpBodyAlternativeEnd or OpMatchFailed
michael@0 757 // node, so there must always be at least one more node.
michael@0 758 ASSERT(opIndex + 1 < m_ops.size());
michael@0 759 YarrOp* nextOp = &m_ops[opIndex + 1];
michael@0 760
michael@0 761 PatternTerm* term = op.m_term;
michael@0 762 UChar ch = term->patternCharacter;
michael@0 763
michael@0 764 if ((ch > 0xff) && (m_charSize == Char8)) {
michael@0 765 // Have a 16 bit pattern character and an 8 bit string - short circuit
michael@0 766 op.m_jumps.append(jump());
michael@0 767 return true;
michael@0 768 }
michael@0 769
michael@0 770 const RegisterID character = regT0;
michael@0 771 int maxCharactersAtOnce = m_charSize == Char8 ? 4 : 2;
michael@0 772 unsigned ignoreCaseMask = 0;
michael@0 773 #if CPU(BIG_ENDIAN)
michael@0 774 int allCharacters = ch << (m_charSize == Char8 ? 24 : 16);
michael@0 775 #else
michael@0 776 int allCharacters = ch;
michael@0 777 #endif
michael@0 778 int numberCharacters;
michael@0 779 int startTermPosition = term->inputPosition;
michael@0 780
michael@0 781 // For case-insesitive compares, non-ascii characters that have different
michael@0 782 // upper & lower case representations are converted to a character class.
michael@0 783 ASSERT(!m_pattern.m_ignoreCase || isASCIIAlpha(ch) || isCanonicallyUnique(ch));
michael@0 784
michael@0 785 if (m_pattern.m_ignoreCase && isASCIIAlpha(ch))
michael@0 786 #if CPU(BIG_ENDIAN)
michael@0 787 ignoreCaseMask |= 32 << (m_charSize == Char8 ? 24 : 16);
michael@0 788 #else
michael@0 789 ignoreCaseMask |= 32;
michael@0 790 #endif
michael@0 791
michael@0 792 for (numberCharacters = 1; numberCharacters < maxCharactersAtOnce && nextOp->m_op == OpTerm; ++numberCharacters, nextOp = &m_ops[opIndex + numberCharacters]) {
michael@0 793 PatternTerm* nextTerm = nextOp->m_term;
michael@0 794
michael@0 795 if (nextTerm->type != PatternTerm::TypePatternCharacter
michael@0 796 || nextTerm->quantityType != QuantifierFixedCount
michael@0 797 || nextTerm->quantityCount != 1
michael@0 798 || nextTerm->inputPosition != (startTermPosition + numberCharacters))
michael@0 799 break;
michael@0 800
michael@0 801 nextOp->m_isDeadCode = true;
michael@0 802
michael@0 803 #if CPU(BIG_ENDIAN)
michael@0 804 int shiftAmount = (m_charSize == Char8 ? 24 : 16) - ((m_charSize == Char8 ? 8 : 16) * numberCharacters);
michael@0 805 #else
michael@0 806 int shiftAmount = (m_charSize == Char8 ? 8 : 16) * numberCharacters;
michael@0 807 #endif
michael@0 808
michael@0 809 UChar currentCharacter = nextTerm->patternCharacter;
michael@0 810
michael@0 811 if ((currentCharacter > 0xff) && (m_charSize == Char8)) {
michael@0 812 // Have a 16 bit pattern character and an 8 bit string - short circuit
michael@0 813 op.m_jumps.append(jump());
michael@0 814 return true;
michael@0 815 }
michael@0 816
michael@0 817 // For case-insesitive compares, non-ascii characters that have different
michael@0 818 // upper & lower case representations are converted to a character class.
michael@0 819 ASSERT(!m_pattern.m_ignoreCase || isASCIIAlpha(currentCharacter) || isCanonicallyUnique(currentCharacter));
michael@0 820
michael@0 821 allCharacters |= (currentCharacter << shiftAmount);
michael@0 822
michael@0 823 if ((m_pattern.m_ignoreCase) && (isASCIIAlpha(currentCharacter)))
michael@0 824 ignoreCaseMask |= 32 << shiftAmount;
michael@0 825 }
michael@0 826
michael@0 827 if (m_charSize == Char8) {
michael@0 828 switch (numberCharacters) {
michael@0 829 case 1:
michael@0 830 op.m_jumps.append(jumpIfCharNotEquals(ch, startTermPosition - m_checked, character));
michael@0 831 return true;
michael@0 832 case 2: {
michael@0 833 BaseIndex address(input, index, TimesOne, (startTermPosition - m_checked) * sizeof(LChar));
michael@0 834 load16Unaligned(address, character);
michael@0 835 break;
michael@0 836 }
michael@0 837 case 3: {
michael@0 838 BaseIndex highAddress(input, index, TimesOne, (startTermPosition - m_checked) * sizeof(LChar));
michael@0 839 load16Unaligned(highAddress, character);
michael@0 840 if (ignoreCaseMask)
michael@0 841 or32(Imm32(ignoreCaseMask), character);
michael@0 842 op.m_jumps.append(branch32(NotEqual, character, Imm32((allCharacters & 0xffff) | ignoreCaseMask)));
michael@0 843 op.m_jumps.append(jumpIfCharNotEquals(allCharacters >> 16, startTermPosition + 2 - m_checked, character));
michael@0 844 return true;
michael@0 845 }
michael@0 846 case 4: {
michael@0 847 BaseIndex address(input, index, TimesOne, (startTermPosition - m_checked) * sizeof(LChar));
michael@0 848 load32WithUnalignedHalfWords(address, character);
michael@0 849 break;
michael@0 850 }
michael@0 851 }
michael@0 852 } else {
michael@0 853 switch (numberCharacters) {
michael@0 854 case 1:
michael@0 855 op.m_jumps.append(jumpIfCharNotEquals(ch, term->inputPosition - m_checked, character));
michael@0 856 return true;
michael@0 857 case 2:
michael@0 858 BaseIndex address(input, index, TimesTwo, (term->inputPosition - m_checked) * sizeof(UChar));
michael@0 859 load32WithUnalignedHalfWords(address, character);
michael@0 860 break;
michael@0 861 }
michael@0 862 }
michael@0 863
michael@0 864 if (ignoreCaseMask)
michael@0 865 or32(Imm32(ignoreCaseMask), character);
michael@0 866 op.m_jumps.append(branch32(NotEqual, character, Imm32(allCharacters | ignoreCaseMask)));
michael@0 867 return true;
michael@0 868 }
michael@0 869 bool backtrackPatternCharacterOnce(size_t opIndex)
michael@0 870 {
michael@0 871 return backtrackTermDefault(opIndex);
michael@0 872 }
michael@0 873
michael@0 874 bool generatePatternCharacterFixed(size_t opIndex)
michael@0 875 {
michael@0 876 YarrOp& op = m_ops[opIndex];
michael@0 877 PatternTerm* term = op.m_term;
michael@0 878 UChar ch = term->patternCharacter;
michael@0 879
michael@0 880 const RegisterID character = regT0;
michael@0 881 const RegisterID countRegister = regT1;
michael@0 882
michael@0 883 move(index, countRegister);
michael@0 884 if (term->quantityCount.hasOverflowed())
michael@0 885 return false;
michael@0 886 sub32(Imm32(term->quantityCount.unsafeGet()), countRegister);
michael@0 887
michael@0 888 Label loop(this);
michael@0 889 int offset;
michael@0 890 if ((Checked<int>(term->inputPosition - m_checked + Checked<int64_t>(term->quantityCount)) * static_cast<int>(m_charSize == Char8 ? sizeof(char) : sizeof(UChar))).safeGet(offset))
michael@0 891 return false;
michael@0 892 BaseIndex address(input, countRegister, m_charScale, offset);
michael@0 893
michael@0 894 if (m_charSize == Char8)
michael@0 895 load8(address, character);
michael@0 896 else
michael@0 897 load16(address, character);
michael@0 898
michael@0 899 // For case-insesitive compares, non-ascii characters that have different
michael@0 900 // upper & lower case representations are converted to a character class.
michael@0 901 ASSERT(!m_pattern.m_ignoreCase || isASCIIAlpha(ch) || isCanonicallyUnique(ch));
michael@0 902 if (m_pattern.m_ignoreCase && isASCIIAlpha(ch)) {
michael@0 903 or32(TrustedImm32(0x20), character);
michael@0 904 ch |= 0x20;
michael@0 905 }
michael@0 906
michael@0 907 op.m_jumps.append(branch32(NotEqual, character, Imm32(ch)));
michael@0 908 add32(TrustedImm32(1), countRegister);
michael@0 909 branch32(NotEqual, countRegister, index).linkTo(loop, this);
michael@0 910
michael@0 911 return true;
michael@0 912 }
michael@0 913 bool backtrackPatternCharacterFixed(size_t opIndex)
michael@0 914 {
michael@0 915 return backtrackTermDefault(opIndex);
michael@0 916 }
michael@0 917
michael@0 918 bool generatePatternCharacterGreedy(size_t opIndex)
michael@0 919 {
michael@0 920 YarrOp& op = m_ops[opIndex];
michael@0 921 PatternTerm* term = op.m_term;
michael@0 922 UChar ch = term->patternCharacter;
michael@0 923
michael@0 924 const RegisterID character = regT0;
michael@0 925 const RegisterID countRegister = regT1;
michael@0 926
michael@0 927 move(TrustedImm32(0), countRegister);
michael@0 928
michael@0 929 // Unless have a 16 bit pattern character and an 8 bit string - short circuit
michael@0 930 if (!((ch > 0xff) && (m_charSize == Char8))) {
michael@0 931 JumpList failures;
michael@0 932 Label loop(this);
michael@0 933 failures.append(atEndOfInput());
michael@0 934 failures.append(jumpIfCharNotEquals(ch, term->inputPosition - m_checked, character));
michael@0 935
michael@0 936 add32(TrustedImm32(1), countRegister);
michael@0 937 add32(TrustedImm32(1), index);
michael@0 938 if (term->quantityCount == quantifyInfinite) {
michael@0 939 jump(loop);
michael@0 940 } else {
michael@0 941 if (term->quantityCount.hasOverflowed())
michael@0 942 return false;
michael@0 943 branch32(NotEqual, countRegister, Imm32(term->quantityCount.unsafeGet())).linkTo(loop, this);
michael@0 944 }
michael@0 945
michael@0 946 failures.link(this);
michael@0 947 }
michael@0 948 op.m_reentry = label();
michael@0 949
michael@0 950 storeToFrame(countRegister, term->frameLocation);
michael@0 951 return true;
michael@0 952 }
michael@0 953 bool backtrackPatternCharacterGreedy(size_t opIndex)
michael@0 954 {
michael@0 955 YarrOp& op = m_ops[opIndex];
michael@0 956 PatternTerm* term = op.m_term;
michael@0 957
michael@0 958 const RegisterID countRegister = regT1;
michael@0 959
michael@0 960 m_backtrackingState.link(this);
michael@0 961
michael@0 962 loadFromFrame(term->frameLocation, countRegister);
michael@0 963 m_backtrackingState.append(branchTest32(Zero, countRegister));
michael@0 964 sub32(TrustedImm32(1), countRegister);
michael@0 965 sub32(TrustedImm32(1), index);
michael@0 966 jump(op.m_reentry);
michael@0 967
michael@0 968 return true;
michael@0 969 }
michael@0 970
michael@0 971 bool generatePatternCharacterNonGreedy(size_t opIndex)
michael@0 972 {
michael@0 973 YarrOp& op = m_ops[opIndex];
michael@0 974 PatternTerm* term = op.m_term;
michael@0 975
michael@0 976 const RegisterID countRegister = regT1;
michael@0 977
michael@0 978 move(TrustedImm32(0), countRegister);
michael@0 979 op.m_reentry = label();
michael@0 980 storeToFrame(countRegister, term->frameLocation);
michael@0 981 return true;
michael@0 982 }
michael@0 983 bool backtrackPatternCharacterNonGreedy(size_t opIndex)
michael@0 984 {
michael@0 985 YarrOp& op = m_ops[opIndex];
michael@0 986 PatternTerm* term = op.m_term;
michael@0 987 UChar ch = term->patternCharacter;
michael@0 988
michael@0 989 const RegisterID character = regT0;
michael@0 990 const RegisterID countRegister = regT1;
michael@0 991
michael@0 992 m_backtrackingState.link(this);
michael@0 993
michael@0 994 loadFromFrame(term->frameLocation, countRegister);
michael@0 995
michael@0 996 // Unless have a 16 bit pattern character and an 8 bit string - short circuit
michael@0 997 if (!((ch > 0xff) && (m_charSize == Char8))) {
michael@0 998 JumpList nonGreedyFailures;
michael@0 999 nonGreedyFailures.append(atEndOfInput());
michael@0 1000 if (term->quantityCount != quantifyInfinite) {
michael@0 1001 if (term->quantityCount.hasOverflowed())
michael@0 1002 return false;
michael@0 1003 nonGreedyFailures.append(branch32(Equal, countRegister, Imm32(term->quantityCount.unsafeGet())));
michael@0 1004 }
michael@0 1005 nonGreedyFailures.append(jumpIfCharNotEquals(ch, term->inputPosition - m_checked, character));
michael@0 1006
michael@0 1007 add32(TrustedImm32(1), countRegister);
michael@0 1008 add32(TrustedImm32(1), index);
michael@0 1009
michael@0 1010 jump(op.m_reentry);
michael@0 1011 nonGreedyFailures.link(this);
michael@0 1012 }
michael@0 1013
michael@0 1014 sub32(countRegister, index);
michael@0 1015 m_backtrackingState.fallthrough();
michael@0 1016
michael@0 1017 return true;
michael@0 1018 }
michael@0 1019
michael@0 1020 bool generateCharacterClassOnce(size_t opIndex)
michael@0 1021 {
michael@0 1022 YarrOp& op = m_ops[opIndex];
michael@0 1023 PatternTerm* term = op.m_term;
michael@0 1024
michael@0 1025 const RegisterID character = regT0;
michael@0 1026
michael@0 1027 JumpList matchDest;
michael@0 1028 readCharacter(term->inputPosition - m_checked, character);
michael@0 1029 matchCharacterClass(character, matchDest, term->characterClass);
michael@0 1030
michael@0 1031 if (term->invert())
michael@0 1032 op.m_jumps.append(matchDest);
michael@0 1033 else {
michael@0 1034 op.m_jumps.append(jump());
michael@0 1035 matchDest.link(this);
michael@0 1036 }
michael@0 1037 return true;
michael@0 1038 }
michael@0 1039 bool backtrackCharacterClassOnce(size_t opIndex)
michael@0 1040 {
michael@0 1041 return backtrackTermDefault(opIndex);
michael@0 1042 }
michael@0 1043
michael@0 1044 bool generateCharacterClassFixed(size_t opIndex)
michael@0 1045 {
michael@0 1046 YarrOp& op = m_ops[opIndex];
michael@0 1047 PatternTerm* term = op.m_term;
michael@0 1048
michael@0 1049 const RegisterID character = regT0;
michael@0 1050 const RegisterID countRegister = regT1;
michael@0 1051
michael@0 1052 move(index, countRegister);
michael@0 1053 if (term->quantityCount.hasOverflowed())
michael@0 1054 return false;
michael@0 1055 sub32(Imm32(term->quantityCount.unsafeGet()), countRegister);
michael@0 1056
michael@0 1057 Label loop(this);
michael@0 1058 JumpList matchDest;
michael@0 1059
michael@0 1060 int offset;
michael@0 1061 Checked<int64_t> checkedOffset(term->inputPosition - m_checked + Checked<int64_t>(term->quantityCount));
michael@0 1062
michael@0 1063 if (m_charSize == Char8) {
michael@0 1064 if ((Checked<int>(checkedOffset) * static_cast<int>(sizeof(char))).safeGet(offset))
michael@0 1065 return false;
michael@0 1066 load8(BaseIndex(input, countRegister, TimesOne, offset), character);
michael@0 1067 } else {
michael@0 1068 if ((Checked<int>(checkedOffset) * static_cast<int>(sizeof(UChar))).safeGet(offset))
michael@0 1069 return false;
michael@0 1070 load16(BaseIndex(input, countRegister, TimesTwo, offset), character);
michael@0 1071 }
michael@0 1072 matchCharacterClass(character, matchDest, term->characterClass);
michael@0 1073
michael@0 1074 if (term->invert())
michael@0 1075 op.m_jumps.append(matchDest);
michael@0 1076 else {
michael@0 1077 op.m_jumps.append(jump());
michael@0 1078 matchDest.link(this);
michael@0 1079 }
michael@0 1080
michael@0 1081 add32(TrustedImm32(1), countRegister);
michael@0 1082 branch32(NotEqual, countRegister, index).linkTo(loop, this);
michael@0 1083 return true;
michael@0 1084 }
michael@0 1085 bool backtrackCharacterClassFixed(size_t opIndex)
michael@0 1086 {
michael@0 1087 return backtrackTermDefault(opIndex);
michael@0 1088 }
michael@0 1089
michael@0 1090 bool generateCharacterClassGreedy(size_t opIndex)
michael@0 1091 {
michael@0 1092 YarrOp& op = m_ops[opIndex];
michael@0 1093 PatternTerm* term = op.m_term;
michael@0 1094
michael@0 1095 const RegisterID character = regT0;
michael@0 1096 const RegisterID countRegister = regT1;
michael@0 1097
michael@0 1098 move(TrustedImm32(0), countRegister);
michael@0 1099
michael@0 1100 JumpList failures;
michael@0 1101 Label loop(this);
michael@0 1102 failures.append(atEndOfInput());
michael@0 1103
michael@0 1104 if (term->invert()) {
michael@0 1105 readCharacter(term->inputPosition - m_checked, character);
michael@0 1106 matchCharacterClass(character, failures, term->characterClass);
michael@0 1107 } else {
michael@0 1108 JumpList matchDest;
michael@0 1109 readCharacter(term->inputPosition - m_checked, character);
michael@0 1110 matchCharacterClass(character, matchDest, term->characterClass);
michael@0 1111 failures.append(jump());
michael@0 1112 matchDest.link(this);
michael@0 1113 }
michael@0 1114
michael@0 1115 add32(TrustedImm32(1), countRegister);
michael@0 1116 add32(TrustedImm32(1), index);
michael@0 1117 if (term->quantityCount != quantifyInfinite) {
michael@0 1118 unsigned quantityCount;
michael@0 1119 if (term->quantityCount.safeGet(quantityCount))
michael@0 1120 return false;
michael@0 1121 branch32(NotEqual, countRegister, Imm32(quantityCount)).linkTo(loop, this);
michael@0 1122 failures.append(jump());
michael@0 1123 } else
michael@0 1124 jump(loop);
michael@0 1125
michael@0 1126 failures.link(this);
michael@0 1127 op.m_reentry = label();
michael@0 1128
michael@0 1129 storeToFrame(countRegister, term->frameLocation);
michael@0 1130 return true;
michael@0 1131 }
michael@0 1132 bool backtrackCharacterClassGreedy(size_t opIndex)
michael@0 1133 {
michael@0 1134 YarrOp& op = m_ops[opIndex];
michael@0 1135 PatternTerm* term = op.m_term;
michael@0 1136
michael@0 1137 const RegisterID countRegister = regT1;
michael@0 1138
michael@0 1139 m_backtrackingState.link(this);
michael@0 1140
michael@0 1141 loadFromFrame(term->frameLocation, countRegister);
michael@0 1142 m_backtrackingState.append(branchTest32(Zero, countRegister));
michael@0 1143 sub32(TrustedImm32(1), countRegister);
michael@0 1144 sub32(TrustedImm32(1), index);
michael@0 1145 jump(op.m_reentry);
michael@0 1146
michael@0 1147 return true;
michael@0 1148 }
michael@0 1149
michael@0 1150 bool generateCharacterClassNonGreedy(size_t opIndex)
michael@0 1151 {
michael@0 1152 YarrOp& op = m_ops[opIndex];
michael@0 1153 PatternTerm* term = op.m_term;
michael@0 1154
michael@0 1155 const RegisterID countRegister = regT1;
michael@0 1156
michael@0 1157 move(TrustedImm32(0), countRegister);
michael@0 1158 op.m_reentry = label();
michael@0 1159 storeToFrame(countRegister, term->frameLocation);
michael@0 1160 return true;
michael@0 1161 }
michael@0 1162 bool backtrackCharacterClassNonGreedy(size_t opIndex)
michael@0 1163 {
michael@0 1164 YarrOp& op = m_ops[opIndex];
michael@0 1165 PatternTerm* term = op.m_term;
michael@0 1166
michael@0 1167 const RegisterID character = regT0;
michael@0 1168 const RegisterID countRegister = regT1;
michael@0 1169
michael@0 1170 JumpList nonGreedyFailures;
michael@0 1171
michael@0 1172 m_backtrackingState.link(this);
michael@0 1173
michael@0 1174 loadFromFrame(term->frameLocation, countRegister);
michael@0 1175
michael@0 1176 nonGreedyFailures.append(atEndOfInput());
michael@0 1177 if (term->quantityCount.hasOverflowed())
michael@0 1178 return false;
michael@0 1179 nonGreedyFailures.append(branch32(Equal, countRegister, Imm32(term->quantityCount.unsafeGet())));
michael@0 1180
michael@0 1181 JumpList matchDest;
michael@0 1182 readCharacter(term->inputPosition - m_checked, character);
michael@0 1183 matchCharacterClass(character, matchDest, term->characterClass);
michael@0 1184
michael@0 1185 if (term->invert())
michael@0 1186 nonGreedyFailures.append(matchDest);
michael@0 1187 else {
michael@0 1188 nonGreedyFailures.append(jump());
michael@0 1189 matchDest.link(this);
michael@0 1190 }
michael@0 1191
michael@0 1192 add32(TrustedImm32(1), countRegister);
michael@0 1193 add32(TrustedImm32(1), index);
michael@0 1194
michael@0 1195 jump(op.m_reentry);
michael@0 1196
michael@0 1197 nonGreedyFailures.link(this);
michael@0 1198 sub32(countRegister, index);
michael@0 1199 m_backtrackingState.fallthrough();
michael@0 1200
michael@0 1201 return true;
michael@0 1202 }
michael@0 1203
michael@0 1204 bool generateDotStarEnclosure(size_t opIndex)
michael@0 1205 {
michael@0 1206 YarrOp& op = m_ops[opIndex];
michael@0 1207 PatternTerm* term = op.m_term;
michael@0 1208
michael@0 1209 const RegisterID character = regT0;
michael@0 1210 const RegisterID matchPos = regT1;
michael@0 1211
michael@0 1212 JumpList foundBeginningNewLine;
michael@0 1213 JumpList saveStartIndex;
michael@0 1214 JumpList foundEndingNewLine;
michael@0 1215
michael@0 1216 ASSERT(!m_pattern.m_body->m_hasFixedSize);
michael@0 1217 getMatchStart(matchPos);
michael@0 1218
michael@0 1219 saveStartIndex.append(branchTest32(Zero, matchPos));
michael@0 1220 Label findBOLLoop(this);
michael@0 1221 sub32(TrustedImm32(1), matchPos);
michael@0 1222 if (m_charSize == Char8)
michael@0 1223 load8(BaseIndex(input, matchPos, TimesOne, 0), character);
michael@0 1224 else
michael@0 1225 load16(BaseIndex(input, matchPos, TimesTwo, 0), character);
michael@0 1226 matchCharacterClass(character, foundBeginningNewLine, m_pattern.newlineCharacterClass());
michael@0 1227 branchTest32(NonZero, matchPos).linkTo(findBOLLoop, this);
michael@0 1228 saveStartIndex.append(jump());
michael@0 1229
michael@0 1230 foundBeginningNewLine.link(this);
michael@0 1231 add32(TrustedImm32(1), matchPos); // Advance past newline
michael@0 1232 saveStartIndex.link(this);
michael@0 1233
michael@0 1234 if (!m_pattern.m_multiline && term->anchors.bolAnchor)
michael@0 1235 op.m_jumps.append(branchTest32(NonZero, matchPos));
michael@0 1236
michael@0 1237 ASSERT(!m_pattern.m_body->m_hasFixedSize);
michael@0 1238 setMatchStart(matchPos);
michael@0 1239
michael@0 1240 move(index, matchPos);
michael@0 1241
michael@0 1242 Label findEOLLoop(this);
michael@0 1243 foundEndingNewLine.append(branch32(Equal, matchPos, length));
michael@0 1244 if (m_charSize == Char8)
michael@0 1245 load8(BaseIndex(input, matchPos, TimesOne, 0), character);
michael@0 1246 else
michael@0 1247 load16(BaseIndex(input, matchPos, TimesTwo, 0), character);
michael@0 1248 matchCharacterClass(character, foundEndingNewLine, m_pattern.newlineCharacterClass());
michael@0 1249 add32(TrustedImm32(1), matchPos);
michael@0 1250 jump(findEOLLoop);
michael@0 1251
michael@0 1252 foundEndingNewLine.link(this);
michael@0 1253
michael@0 1254 if (!m_pattern.m_multiline && term->anchors.eolAnchor)
michael@0 1255 op.m_jumps.append(branch32(NotEqual, matchPos, length));
michael@0 1256
michael@0 1257 move(matchPos, index);
michael@0 1258 return true;
michael@0 1259 }
michael@0 1260
michael@0 1261 bool backtrackDotStarEnclosure(size_t opIndex)
michael@0 1262 {
michael@0 1263 return backtrackTermDefault(opIndex);
michael@0 1264 }
michael@0 1265
michael@0 1266 // Code generation/backtracking for simple terms
michael@0 1267 // (pattern characters, character classes, and assertions).
michael@0 1268 // These methods farm out work to the set of functions above.
michael@0 1269 bool generateTerm(size_t opIndex)
michael@0 1270 {
michael@0 1271 YarrOp& op = m_ops[opIndex];
michael@0 1272 PatternTerm* term = op.m_term;
michael@0 1273
michael@0 1274 switch (term->type) {
michael@0 1275 case PatternTerm::TypePatternCharacter:
michael@0 1276 switch (term->quantityType) {
michael@0 1277 case QuantifierFixedCount:
michael@0 1278 if (term->quantityCount == 1)
michael@0 1279 return generatePatternCharacterOnce(opIndex);
michael@0 1280 else
michael@0 1281 return generatePatternCharacterFixed(opIndex);
michael@0 1282 break;
michael@0 1283 case QuantifierGreedy:
michael@0 1284 return generatePatternCharacterGreedy(opIndex);
michael@0 1285 case QuantifierNonGreedy:
michael@0 1286 return generatePatternCharacterNonGreedy(opIndex);
michael@0 1287 }
michael@0 1288 break;
michael@0 1289
michael@0 1290 case PatternTerm::TypeCharacterClass:
michael@0 1291 switch (term->quantityType) {
michael@0 1292 case QuantifierFixedCount:
michael@0 1293 if (term->quantityCount == 1)
michael@0 1294 return generateCharacterClassOnce(opIndex);
michael@0 1295 else
michael@0 1296 return generateCharacterClassFixed(opIndex);
michael@0 1297 break;
michael@0 1298 case QuantifierGreedy:
michael@0 1299 return generateCharacterClassGreedy(opIndex);
michael@0 1300 case QuantifierNonGreedy:
michael@0 1301 return generateCharacterClassNonGreedy(opIndex);
michael@0 1302 }
michael@0 1303 break;
michael@0 1304
michael@0 1305 case PatternTerm::TypeAssertionBOL:
michael@0 1306 return generateAssertionBOL(opIndex);
michael@0 1307
michael@0 1308 case PatternTerm::TypeAssertionEOL:
michael@0 1309 return generateAssertionEOL(opIndex);
michael@0 1310
michael@0 1311 case PatternTerm::TypeAssertionWordBoundary:
michael@0 1312 return generateAssertionWordBoundary(opIndex);
michael@0 1313
michael@0 1314 case PatternTerm::TypeForwardReference:
michael@0 1315 return true;
michael@0 1316
michael@0 1317 case PatternTerm::TypeParenthesesSubpattern:
michael@0 1318 case PatternTerm::TypeParentheticalAssertion:
michael@0 1319 ASSERT_NOT_REACHED();
michael@0 1320 return false;
michael@0 1321 case PatternTerm::TypeBackReference:
michael@0 1322 return false;
michael@0 1323 case PatternTerm::TypeDotStarEnclosure:
michael@0 1324 return generateDotStarEnclosure(opIndex);
michael@0 1325 }
michael@0 1326
michael@0 1327 return false;
michael@0 1328 }
michael@0 1329 bool backtrackTerm(size_t opIndex)
michael@0 1330 {
michael@0 1331 YarrOp& op = m_ops[opIndex];
michael@0 1332 PatternTerm* term = op.m_term;
michael@0 1333
michael@0 1334 switch (term->type) {
michael@0 1335 case PatternTerm::TypePatternCharacter:
michael@0 1336 switch (term->quantityType) {
michael@0 1337 case QuantifierFixedCount:
michael@0 1338 if (term->quantityCount == 1)
michael@0 1339 return backtrackPatternCharacterOnce(opIndex);
michael@0 1340 else
michael@0 1341 return backtrackPatternCharacterFixed(opIndex);
michael@0 1342 case QuantifierGreedy:
michael@0 1343 return backtrackPatternCharacterGreedy(opIndex);
michael@0 1344 case QuantifierNonGreedy:
michael@0 1345 return backtrackPatternCharacterNonGreedy(opIndex);
michael@0 1346 }
michael@0 1347 break;
michael@0 1348
michael@0 1349 case PatternTerm::TypeCharacterClass:
michael@0 1350 switch (term->quantityType) {
michael@0 1351 case QuantifierFixedCount:
michael@0 1352 if (term->quantityCount == 1)
michael@0 1353 return backtrackCharacterClassOnce(opIndex);
michael@0 1354 else
michael@0 1355 return backtrackCharacterClassFixed(opIndex);
michael@0 1356 case QuantifierGreedy:
michael@0 1357 return backtrackCharacterClassGreedy(opIndex);
michael@0 1358 case QuantifierNonGreedy:
michael@0 1359 return backtrackCharacterClassNonGreedy(opIndex);
michael@0 1360 }
michael@0 1361 break;
michael@0 1362
michael@0 1363 case PatternTerm::TypeAssertionBOL:
michael@0 1364 return backtrackAssertionBOL(opIndex);
michael@0 1365
michael@0 1366 case PatternTerm::TypeAssertionEOL:
michael@0 1367 return backtrackAssertionEOL(opIndex);
michael@0 1368
michael@0 1369 case PatternTerm::TypeAssertionWordBoundary:
michael@0 1370 return backtrackAssertionWordBoundary(opIndex);
michael@0 1371
michael@0 1372 case PatternTerm::TypeForwardReference:
michael@0 1373 return true;
michael@0 1374
michael@0 1375 case PatternTerm::TypeParenthesesSubpattern:
michael@0 1376 case PatternTerm::TypeParentheticalAssertion:
michael@0 1377 ASSERT_NOT_REACHED();
michael@0 1378 return false;
michael@0 1379
michael@0 1380 case PatternTerm::TypeDotStarEnclosure:
michael@0 1381 return backtrackDotStarEnclosure(opIndex);
michael@0 1382
michael@0 1383 case PatternTerm::TypeBackReference:
michael@0 1384 return false;
michael@0 1385 }
michael@0 1386 return true;
michael@0 1387 }
michael@0 1388
michael@0 1389 bool generate()
michael@0 1390 {
michael@0 1391 // Forwards generate the matching code.
michael@0 1392 ASSERT(m_ops.size());
michael@0 1393 size_t opIndex = 0;
michael@0 1394
michael@0 1395 do {
michael@0 1396 YarrOp& op = m_ops[opIndex];
michael@0 1397 switch (op.m_op) {
michael@0 1398
michael@0 1399 case OpTerm:
michael@0 1400 if (!generateTerm(opIndex))
michael@0 1401 return false;
michael@0 1402 break;
michael@0 1403
michael@0 1404 // OpBodyAlternativeBegin/Next/End
michael@0 1405 //
michael@0 1406 // These nodes wrap the set of alternatives in the body of the regular expression.
michael@0 1407 // There may be either one or two chains of OpBodyAlternative nodes, one representing
michael@0 1408 // the 'once through' sequence of alternatives (if any exist), and one representing
michael@0 1409 // the repeating alternatives (again, if any exist).
michael@0 1410 //
michael@0 1411 // Upon normal entry to the Begin alternative, we will check that input is available.
michael@0 1412 // Reentry to the Begin alternative will take place after the check has taken place,
michael@0 1413 // and will assume that the input position has already been progressed as appropriate.
michael@0 1414 //
michael@0 1415 // Entry to subsequent Next/End alternatives occurs when the prior alternative has
michael@0 1416 // successfully completed a match - return a success state from JIT code.
michael@0 1417 //
michael@0 1418 // Next alternatives allow for reentry optimized to suit backtracking from its
michael@0 1419 // preceding alternative. It expects the input position to still be set to a position
michael@0 1420 // appropriate to its predecessor, and it will only perform an input check if the
michael@0 1421 // predecessor had a minimum size less than its own.
michael@0 1422 //
michael@0 1423 // In the case 'once through' expressions, the End node will also have a reentry
michael@0 1424 // point to jump to when the last alternative fails. Again, this expects the input
michael@0 1425 // position to still reflect that expected by the prior alternative.
michael@0 1426 case OpBodyAlternativeBegin: {
michael@0 1427 PatternAlternative* alternative = op.m_alternative;
michael@0 1428
michael@0 1429 // Upon entry at the head of the set of alternatives, check if input is available
michael@0 1430 // to run the first alternative. (This progresses the input position).
michael@0 1431 op.m_jumps.append(jumpIfNoAvailableInput(alternative->m_minimumSize));
michael@0 1432 // We will reenter after the check, and assume the input position to have been
michael@0 1433 // set as appropriate to this alternative.
michael@0 1434 op.m_reentry = label();
michael@0 1435
michael@0 1436 if (alternative->m_minimumSize > INT_MAX)
michael@0 1437 return false;
michael@0 1438 m_checked = alternative->m_minimumSize;
michael@0 1439 break;
michael@0 1440 }
michael@0 1441 case OpBodyAlternativeNext:
michael@0 1442 case OpBodyAlternativeEnd: {
michael@0 1443 PatternAlternative* priorAlternative = m_ops[op.m_previousOp].m_alternative;
michael@0 1444 PatternAlternative* alternative = op.m_alternative;
michael@0 1445
michael@0 1446 // If we get here, the prior alternative matched - return success.
michael@0 1447
michael@0 1448 // Adjust the stack pointer to remove the pattern's frame.
michael@0 1449 #if !WTF_CPU_SPARC
michael@0 1450 removeCallFrame();
michael@0 1451 #endif
michael@0 1452
michael@0 1453 // Load appropriate values into the return register and the first output
michael@0 1454 // slot, and return. In the case of pattern with a fixed size, we will
michael@0 1455 // not have yet set the value in the first
michael@0 1456 ASSERT(index != returnRegister);
michael@0 1457 if (m_pattern.m_body->m_hasFixedSize) {
michael@0 1458 move(index, returnRegister);
michael@0 1459 if (priorAlternative->m_minimumSize)
michael@0 1460 sub32(Imm32(priorAlternative->m_minimumSize), returnRegister);
michael@0 1461 if (compileMode == IncludeSubpatterns)
michael@0 1462 store32(returnRegister, output);
michael@0 1463 } else
michael@0 1464 getMatchStart(returnRegister);
michael@0 1465 if (compileMode == IncludeSubpatterns)
michael@0 1466 store32(index, Address(output, 4));
michael@0 1467 #if WTF_CPU_X86_64
michael@0 1468 // upper 32bit to 0
michael@0 1469 move32(returnRegister, returnRegister);
michael@0 1470 lshiftPtr(Imm32(32), index);
michael@0 1471 orPtr(index, returnRegister);
michael@0 1472 #else
michael@0 1473 move(index, returnRegister2);
michael@0 1474 #endif
michael@0 1475
michael@0 1476 generateReturn();
michael@0 1477
michael@0 1478 // This is the divide between the tail of the prior alternative, above, and
michael@0 1479 // the head of the subsequent alternative, below.
michael@0 1480
michael@0 1481 if (op.m_op == OpBodyAlternativeNext) {
michael@0 1482 // This is the reentry point for the Next alternative. We expect any code
michael@0 1483 // that jumps here to do so with the input position matching that of the
michael@0 1484 // PRIOR alteranative, and we will only check input availability if we
michael@0 1485 // need to progress it forwards.
michael@0 1486 op.m_reentry = label();
michael@0 1487 if (alternative->m_minimumSize > priorAlternative->m_minimumSize) {
michael@0 1488 add32(Imm32(alternative->m_minimumSize - priorAlternative->m_minimumSize), index);
michael@0 1489 op.m_jumps.append(jumpIfNoAvailableInput());
michael@0 1490 } else if (priorAlternative->m_minimumSize > alternative->m_minimumSize)
michael@0 1491 sub32(Imm32(priorAlternative->m_minimumSize - alternative->m_minimumSize), index);
michael@0 1492 } else if (op.m_nextOp == notFound) {
michael@0 1493 // This is the reentry point for the End of 'once through' alternatives,
michael@0 1494 // jumped to when the last alternative fails to match.
michael@0 1495 op.m_reentry = label();
michael@0 1496 sub32(Imm32(priorAlternative->m_minimumSize), index);
michael@0 1497 }
michael@0 1498
michael@0 1499 if (op.m_op == OpBodyAlternativeNext)
michael@0 1500 m_checked += alternative->m_minimumSize;
michael@0 1501 m_checked -= priorAlternative->m_minimumSize;
michael@0 1502 break;
michael@0 1503 }
michael@0 1504
michael@0 1505 // OpSimpleNestedAlternativeBegin/Next/End
michael@0 1506 // OpNestedAlternativeBegin/Next/End
michael@0 1507 //
michael@0 1508 // These nodes are used to handle sets of alternatives that are nested within
michael@0 1509 // subpatterns and parenthetical assertions. The 'simple' forms are used where
michael@0 1510 // we do not need to be able to backtrack back into any alternative other than
michael@0 1511 // the last, the normal forms allow backtracking into any alternative.
michael@0 1512 //
michael@0 1513 // Each Begin/Next node is responsible for planting an input check to ensure
michael@0 1514 // sufficient input is available on entry. Next nodes additionally need to
michael@0 1515 // jump to the end - Next nodes use the End node's m_jumps list to hold this
michael@0 1516 // set of jumps.
michael@0 1517 //
michael@0 1518 // In the non-simple forms, successful alternative matches must store a
michael@0 1519 // 'return address' using a DataLabelPtr, used to store the address to jump
michael@0 1520 // to when backtracking, to get to the code for the appropriate alternative.
michael@0 1521 case OpSimpleNestedAlternativeBegin:
michael@0 1522 case OpNestedAlternativeBegin: {
michael@0 1523 PatternTerm* term = op.m_term;
michael@0 1524 PatternAlternative* alternative = op.m_alternative;
michael@0 1525 PatternDisjunction* disjunction = term->parentheses.disjunction;
michael@0 1526
michael@0 1527 // Calculate how much input we need to check for, and if non-zero check.
michael@0 1528 op.m_checkAdjust = alternative->m_minimumSize;
michael@0 1529 if ((term->quantityType == QuantifierFixedCount) && (term->type != PatternTerm::TypeParentheticalAssertion))
michael@0 1530 op.m_checkAdjust -= disjunction->m_minimumSize;
michael@0 1531 if (op.m_checkAdjust)
michael@0 1532 op.m_jumps.append(jumpIfNoAvailableInput(op.m_checkAdjust));
michael@0 1533
michael@0 1534 m_checked += op.m_checkAdjust;
michael@0 1535 break;
michael@0 1536 }
michael@0 1537 case OpSimpleNestedAlternativeNext:
michael@0 1538 case OpNestedAlternativeNext: {
michael@0 1539 PatternTerm* term = op.m_term;
michael@0 1540 PatternAlternative* alternative = op.m_alternative;
michael@0 1541 PatternDisjunction* disjunction = term->parentheses.disjunction;
michael@0 1542
michael@0 1543 // In the non-simple case, store a 'return address' so we can backtrack correctly.
michael@0 1544 if (op.m_op == OpNestedAlternativeNext) {
michael@0 1545 unsigned parenthesesFrameLocation = term->frameLocation;
michael@0 1546 unsigned alternativeFrameLocation = parenthesesFrameLocation;
michael@0 1547 if (term->quantityType != QuantifierFixedCount)
michael@0 1548 alternativeFrameLocation += YarrStackSpaceForBackTrackInfoParenthesesOnce;
michael@0 1549 op.m_returnAddress = storeToFrameWithPatch(alternativeFrameLocation);
michael@0 1550 }
michael@0 1551
michael@0 1552 if (term->quantityType != QuantifierFixedCount && !m_ops[op.m_previousOp].m_alternative->m_minimumSize) {
michael@0 1553 // If the previous alternative matched without consuming characters then
michael@0 1554 // backtrack to try to match while consumming some input.
michael@0 1555 op.m_zeroLengthMatch = branch32(Equal, index, Address(stackPointerRegister, term->frameLocation * sizeof(void*)));
michael@0 1556 }
michael@0 1557
michael@0 1558 // If we reach here then the last alternative has matched - jump to the
michael@0 1559 // End node, to skip over any further alternatives.
michael@0 1560 //
michael@0 1561 // FIXME: this is logically O(N^2) (though N can be expected to be very
michael@0 1562 // small). We could avoid this either by adding an extra jump to the JIT
michael@0 1563 // data structures, or by making backtracking code that jumps to Next
michael@0 1564 // alternatives are responsible for checking that input is available (if
michael@0 1565 // we didn't need to plant the input checks, then m_jumps would be free).
michael@0 1566 YarrOp* endOp = &m_ops[op.m_nextOp];
michael@0 1567 while (endOp->m_nextOp != notFound) {
michael@0 1568 ASSERT(endOp->m_op == OpSimpleNestedAlternativeNext || endOp->m_op == OpNestedAlternativeNext);
michael@0 1569 endOp = &m_ops[endOp->m_nextOp];
michael@0 1570 }
michael@0 1571 ASSERT(endOp->m_op == OpSimpleNestedAlternativeEnd || endOp->m_op == OpNestedAlternativeEnd);
michael@0 1572 endOp->m_jumps.append(jump());
michael@0 1573
michael@0 1574 // This is the entry point for the next alternative.
michael@0 1575 op.m_reentry = label();
michael@0 1576
michael@0 1577 // Calculate how much input we need to check for, and if non-zero check.
michael@0 1578 op.m_checkAdjust = alternative->m_minimumSize;
michael@0 1579 if ((term->quantityType == QuantifierFixedCount) && (term->type != PatternTerm::TypeParentheticalAssertion))
michael@0 1580 op.m_checkAdjust -= disjunction->m_minimumSize;
michael@0 1581 if (op.m_checkAdjust)
michael@0 1582 op.m_jumps.append(jumpIfNoAvailableInput(op.m_checkAdjust));
michael@0 1583
michael@0 1584 YarrOp& lastOp = m_ops[op.m_previousOp];
michael@0 1585 m_checked -= lastOp.m_checkAdjust;
michael@0 1586 m_checked += op.m_checkAdjust;
michael@0 1587 break;
michael@0 1588 }
michael@0 1589 case OpSimpleNestedAlternativeEnd:
michael@0 1590 case OpNestedAlternativeEnd: {
michael@0 1591 PatternTerm* term = op.m_term;
michael@0 1592
michael@0 1593 // In the non-simple case, store a 'return address' so we can backtrack correctly.
michael@0 1594 if (op.m_op == OpNestedAlternativeEnd) {
michael@0 1595 unsigned parenthesesFrameLocation = term->frameLocation;
michael@0 1596 unsigned alternativeFrameLocation = parenthesesFrameLocation;
michael@0 1597 if (term->quantityType != QuantifierFixedCount)
michael@0 1598 alternativeFrameLocation += YarrStackSpaceForBackTrackInfoParenthesesOnce;
michael@0 1599 op.m_returnAddress = storeToFrameWithPatch(alternativeFrameLocation);
michael@0 1600 }
michael@0 1601
michael@0 1602 if (term->quantityType != QuantifierFixedCount && !m_ops[op.m_previousOp].m_alternative->m_minimumSize) {
michael@0 1603 // If the previous alternative matched without consuming characters then
michael@0 1604 // backtrack to try to match while consumming some input.
michael@0 1605 op.m_zeroLengthMatch = branch32(Equal, index, Address(stackPointerRegister, term->frameLocation * sizeof(void*)));
michael@0 1606 }
michael@0 1607
michael@0 1608 // If this set of alternatives contains more than one alternative,
michael@0 1609 // then the Next nodes will have planted jumps to the End, and added
michael@0 1610 // them to this node's m_jumps list.
michael@0 1611 op.m_jumps.link(this);
michael@0 1612 op.m_jumps.clear();
michael@0 1613
michael@0 1614 YarrOp& lastOp = m_ops[op.m_previousOp];
michael@0 1615 m_checked -= lastOp.m_checkAdjust;
michael@0 1616 break;
michael@0 1617 }
michael@0 1618
michael@0 1619 // OpParenthesesSubpatternOnceBegin/End
michael@0 1620 //
michael@0 1621 // These nodes support (optionally) capturing subpatterns, that have a
michael@0 1622 // quantity count of 1 (this covers fixed once, and ?/?? quantifiers).
michael@0 1623 case OpParenthesesSubpatternOnceBegin: {
michael@0 1624 PatternTerm* term = op.m_term;
michael@0 1625 unsigned parenthesesFrameLocation = term->frameLocation;
michael@0 1626 const RegisterID indexTemporary = regT0;
michael@0 1627 ASSERT(term->quantityCount == 1);
michael@0 1628
michael@0 1629 // Upon entry to a Greedy quantified set of parenthese store the index.
michael@0 1630 // We'll use this for two purposes:
michael@0 1631 // - To indicate which iteration we are on of mathing the remainder of
michael@0 1632 // the expression after the parentheses - the first, including the
michael@0 1633 // match within the parentheses, or the second having skipped over them.
michael@0 1634 // - To check for empty matches, which must be rejected.
michael@0 1635 //
michael@0 1636 // At the head of a NonGreedy set of parentheses we'll immediately set the
michael@0 1637 // value on the stack to -1 (indicating a match skipping the subpattern),
michael@0 1638 // and plant a jump to the end. We'll also plant a label to backtrack to
michael@0 1639 // to reenter the subpattern later, with a store to set up index on the
michael@0 1640 // second iteration.
michael@0 1641 //
michael@0 1642 // FIXME: for capturing parens, could use the index in the capture array?
michael@0 1643 if (term->quantityType == QuantifierGreedy)
michael@0 1644 storeToFrame(index, parenthesesFrameLocation);
michael@0 1645 else if (term->quantityType == QuantifierNonGreedy) {
michael@0 1646 storeToFrame(TrustedImm32(-1), parenthesesFrameLocation);
michael@0 1647 op.m_jumps.append(jump());
michael@0 1648 op.m_reentry = label();
michael@0 1649 storeToFrame(index, parenthesesFrameLocation);
michael@0 1650 }
michael@0 1651
michael@0 1652 // If the parenthese are capturing, store the starting index value to the
michael@0 1653 // captures array, offsetting as necessary.
michael@0 1654 //
michael@0 1655 // FIXME: could avoid offsetting this value in JIT code, apply
michael@0 1656 // offsets only afterwards, at the point the results array is
michael@0 1657 // being accessed.
michael@0 1658 if (term->capture() && compileMode == IncludeSubpatterns) {
michael@0 1659 int inputOffset = term->inputPosition - m_checked;
michael@0 1660 if (term->quantityType == QuantifierFixedCount)
michael@0 1661 inputOffset -= term->parentheses.disjunction->m_minimumSize;
michael@0 1662 if (inputOffset) {
michael@0 1663 move(index, indexTemporary);
michael@0 1664 add32(Imm32(inputOffset), indexTemporary);
michael@0 1665 setSubpatternStart(indexTemporary, term->parentheses.subpatternId);
michael@0 1666 } else
michael@0 1667 setSubpatternStart(index, term->parentheses.subpatternId);
michael@0 1668 }
michael@0 1669 break;
michael@0 1670 }
michael@0 1671 case OpParenthesesSubpatternOnceEnd: {
michael@0 1672 PatternTerm* term = op.m_term;
michael@0 1673 const RegisterID indexTemporary = regT0;
michael@0 1674 ASSERT(term->quantityCount == 1);
michael@0 1675
michael@0 1676 #ifndef NDEBUG
michael@0 1677 // Runtime ASSERT to make sure that the nested alternative handled the
michael@0 1678 // "no input consumed" check.
michael@0 1679 if (term->quantityType != QuantifierFixedCount && !term->parentheses.disjunction->m_minimumSize) {
michael@0 1680 Jump pastBreakpoint;
michael@0 1681 pastBreakpoint = branch32(NotEqual, index, Address(stackPointerRegister, term->frameLocation * sizeof(void*)));
michael@0 1682 breakpoint();
michael@0 1683 pastBreakpoint.link(this);
michael@0 1684 }
michael@0 1685 #endif
michael@0 1686
michael@0 1687 // If the parenthese are capturing, store the ending index value to the
michael@0 1688 // captures array, offsetting as necessary.
michael@0 1689 //
michael@0 1690 // FIXME: could avoid offsetting this value in JIT code, apply
michael@0 1691 // offsets only afterwards, at the point the results array is
michael@0 1692 // being accessed.
michael@0 1693 if (term->capture() && compileMode == IncludeSubpatterns) {
michael@0 1694 int inputOffset = term->inputPosition - m_checked;
michael@0 1695 if (inputOffset) {
michael@0 1696 move(index, indexTemporary);
michael@0 1697 add32(Imm32(inputOffset), indexTemporary);
michael@0 1698 setSubpatternEnd(indexTemporary, term->parentheses.subpatternId);
michael@0 1699 } else
michael@0 1700 setSubpatternEnd(index, term->parentheses.subpatternId);
michael@0 1701 }
michael@0 1702
michael@0 1703 // If the parentheses are quantified Greedy then add a label to jump back
michael@0 1704 // to if get a failed match from after the parentheses. For NonGreedy
michael@0 1705 // parentheses, link the jump from before the subpattern to here.
michael@0 1706 if (term->quantityType == QuantifierGreedy)
michael@0 1707 op.m_reentry = label();
michael@0 1708 else if (term->quantityType == QuantifierNonGreedy) {
michael@0 1709 YarrOp& beginOp = m_ops[op.m_previousOp];
michael@0 1710 beginOp.m_jumps.link(this);
michael@0 1711 }
michael@0 1712 break;
michael@0 1713 }
michael@0 1714
michael@0 1715 // OpParenthesesSubpatternTerminalBegin/End
michael@0 1716 case OpParenthesesSubpatternTerminalBegin: {
michael@0 1717 PatternTerm* term = op.m_term;
michael@0 1718 ASSERT(term->quantityType == QuantifierGreedy);
michael@0 1719 ASSERT(term->quantityCount == quantifyInfinite);
michael@0 1720 ASSERT(!term->capture());
michael@0 1721
michael@0 1722 // Upon entry set a label to loop back to.
michael@0 1723 op.m_reentry = label();
michael@0 1724
michael@0 1725 // Store the start index of the current match; we need to reject zero
michael@0 1726 // length matches.
michael@0 1727 storeToFrame(index, term->frameLocation);
michael@0 1728 break;
michael@0 1729 }
michael@0 1730 case OpParenthesesSubpatternTerminalEnd: {
michael@0 1731 YarrOp& beginOp = m_ops[op.m_previousOp];
michael@0 1732 #ifndef NDEBUG
michael@0 1733 PatternTerm* term = op.m_term;
michael@0 1734
michael@0 1735 // Runtime ASSERT to make sure that the nested alternative handled the
michael@0 1736 // "no input consumed" check.
michael@0 1737 Jump pastBreakpoint;
michael@0 1738 pastBreakpoint = branch32(NotEqual, index, Address(stackPointerRegister, term->frameLocation * sizeof(void*)));
michael@0 1739 breakpoint();
michael@0 1740 pastBreakpoint.link(this);
michael@0 1741 #endif
michael@0 1742
michael@0 1743 // We know that the match is non-zero, we can accept it and
michael@0 1744 // loop back up to the head of the subpattern.
michael@0 1745 jump(beginOp.m_reentry);
michael@0 1746
michael@0 1747 // This is the entry point to jump to when we stop matching - we will
michael@0 1748 // do so once the subpattern cannot match any more.
michael@0 1749 op.m_reentry = label();
michael@0 1750 break;
michael@0 1751 }
michael@0 1752
michael@0 1753 // OpParentheticalAssertionBegin/End
michael@0 1754 case OpParentheticalAssertionBegin: {
michael@0 1755 PatternTerm* term = op.m_term;
michael@0 1756
michael@0 1757 // Store the current index - assertions should not update index, so
michael@0 1758 // we will need to restore it upon a successful match.
michael@0 1759 unsigned parenthesesFrameLocation = term->frameLocation;
michael@0 1760 storeToFrame(index, parenthesesFrameLocation);
michael@0 1761
michael@0 1762 // Check
michael@0 1763 op.m_checkAdjust = m_checked - term->inputPosition;
michael@0 1764 if (op.m_checkAdjust)
michael@0 1765 sub32(Imm32(op.m_checkAdjust), index);
michael@0 1766
michael@0 1767 m_checked -= op.m_checkAdjust;
michael@0 1768 break;
michael@0 1769 }
michael@0 1770 case OpParentheticalAssertionEnd: {
michael@0 1771 PatternTerm* term = op.m_term;
michael@0 1772
michael@0 1773 // Restore the input index value.
michael@0 1774 unsigned parenthesesFrameLocation = term->frameLocation;
michael@0 1775 loadFromFrame(parenthesesFrameLocation, index);
michael@0 1776
michael@0 1777 // If inverted, a successful match of the assertion must be treated
michael@0 1778 // as a failure, so jump to backtracking.
michael@0 1779 if (term->invert()) {
michael@0 1780 op.m_jumps.append(jump());
michael@0 1781 op.m_reentry = label();
michael@0 1782 }
michael@0 1783
michael@0 1784 YarrOp& lastOp = m_ops[op.m_previousOp];
michael@0 1785 m_checked += lastOp.m_checkAdjust;
michael@0 1786 break;
michael@0 1787 }
michael@0 1788
michael@0 1789 case OpMatchFailed:
michael@0 1790 #if !WTF_CPU_SPARC
michael@0 1791 removeCallFrame();
michael@0 1792 #endif
michael@0 1793 #if WTF_CPU_X86_64
michael@0 1794 move(TrustedImm32(int(WTF::notFound)), returnRegister);
michael@0 1795 #else
michael@0 1796 move(TrustedImmPtr((void*)WTF::notFound), returnRegister);
michael@0 1797 move(TrustedImm32(0), returnRegister2);
michael@0 1798 #endif
michael@0 1799 generateReturn();
michael@0 1800 break;
michael@0 1801 }
michael@0 1802
michael@0 1803 ++opIndex;
michael@0 1804 } while (opIndex < m_ops.size());
michael@0 1805
michael@0 1806 return true;
michael@0 1807 }
michael@0 1808
michael@0 1809 bool backtrack()
michael@0 1810 {
michael@0 1811 // Backwards generate the backtracking code.
michael@0 1812 size_t opIndex = m_ops.size();
michael@0 1813 ASSERT(opIndex);
michael@0 1814
michael@0 1815 do {
michael@0 1816 --opIndex;
michael@0 1817 YarrOp& op = m_ops[opIndex];
michael@0 1818 switch (op.m_op) {
michael@0 1819
michael@0 1820 case OpTerm:
michael@0 1821 if (!backtrackTerm(opIndex))
michael@0 1822 return false;
michael@0 1823 break;
michael@0 1824
michael@0 1825 // OpBodyAlternativeBegin/Next/End
michael@0 1826 //
michael@0 1827 // For each Begin/Next node representing an alternative, we need to decide what to do
michael@0 1828 // in two circumstances:
michael@0 1829 // - If we backtrack back into this node, from within the alternative.
michael@0 1830 // - If the input check at the head of the alternative fails (if this exists).
michael@0 1831 //
michael@0 1832 // We treat these two cases differently since in the former case we have slightly
michael@0 1833 // more information - since we are backtracking out of a prior alternative we know
michael@0 1834 // that at least enough input was available to run it. For example, given the regular
michael@0 1835 // expression /a|b/, if we backtrack out of the first alternative (a failed pattern
michael@0 1836 // character match of 'a'), then we need not perform an additional input availability
michael@0 1837 // check before running the second alternative.
michael@0 1838 //
michael@0 1839 // Backtracking required differs for the last alternative, which in the case of the
michael@0 1840 // repeating set of alternatives must loop. The code generated for the last alternative
michael@0 1841 // will also be used to handle all input check failures from any prior alternatives -
michael@0 1842 // these require similar functionality, in seeking the next available alternative for
michael@0 1843 // which there is sufficient input.
michael@0 1844 //
michael@0 1845 // Since backtracking of all other alternatives simply requires us to link backtracks
michael@0 1846 // to the reentry point for the subsequent alternative, we will only be generating any
michael@0 1847 // code when backtracking the last alternative.
michael@0 1848 case OpBodyAlternativeBegin:
michael@0 1849 case OpBodyAlternativeNext: {
michael@0 1850 PatternAlternative* alternative = op.m_alternative;
michael@0 1851
michael@0 1852 if (op.m_op == OpBodyAlternativeNext) {
michael@0 1853 PatternAlternative* priorAlternative = m_ops[op.m_previousOp].m_alternative;
michael@0 1854 m_checked += priorAlternative->m_minimumSize;
michael@0 1855 }
michael@0 1856 m_checked -= alternative->m_minimumSize;
michael@0 1857
michael@0 1858 // Is this the last alternative? If not, then if we backtrack to this point we just
michael@0 1859 // need to jump to try to match the next alternative.
michael@0 1860 if (m_ops[op.m_nextOp].m_op != OpBodyAlternativeEnd) {
michael@0 1861 m_backtrackingState.linkTo(m_ops[op.m_nextOp].m_reentry, this);
michael@0 1862 break;
michael@0 1863 }
michael@0 1864 YarrOp& endOp = m_ops[op.m_nextOp];
michael@0 1865
michael@0 1866 YarrOp* beginOp = &op;
michael@0 1867 while (beginOp->m_op != OpBodyAlternativeBegin) {
michael@0 1868 ASSERT(beginOp->m_op == OpBodyAlternativeNext);
michael@0 1869 beginOp = &m_ops[beginOp->m_previousOp];
michael@0 1870 }
michael@0 1871
michael@0 1872 bool onceThrough = endOp.m_nextOp == notFound;
michael@0 1873
michael@0 1874 // First, generate code to handle cases where we backtrack out of an attempted match
michael@0 1875 // of the last alternative. If this is a 'once through' set of alternatives then we
michael@0 1876 // have nothing to do - link this straight through to the End.
michael@0 1877 if (onceThrough)
michael@0 1878 m_backtrackingState.linkTo(endOp.m_reentry, this);
michael@0 1879 else {
michael@0 1880 // If we don't need to move the input poistion, and the pattern has a fixed size
michael@0 1881 // (in which case we omit the store of the start index until the pattern has matched)
michael@0 1882 // then we can just link the backtrack out of the last alternative straight to the
michael@0 1883 // head of the first alternative.
michael@0 1884 if (m_pattern.m_body->m_hasFixedSize
michael@0 1885 && (alternative->m_minimumSize > beginOp->m_alternative->m_minimumSize)
michael@0 1886 && (alternative->m_minimumSize - beginOp->m_alternative->m_minimumSize == 1))
michael@0 1887 m_backtrackingState.linkTo(beginOp->m_reentry, this);
michael@0 1888 else {
michael@0 1889 // We need to generate a trampoline of code to execute before looping back
michael@0 1890 // around to the first alternative.
michael@0 1891 m_backtrackingState.link(this);
michael@0 1892
michael@0 1893 // If the pattern size is not fixed, then store the start index, for use if we match.
michael@0 1894 if (!m_pattern.m_body->m_hasFixedSize) {
michael@0 1895 if (alternative->m_minimumSize == 1)
michael@0 1896 setMatchStart(index);
michael@0 1897 else {
michael@0 1898 move(index, regT0);
michael@0 1899 if (alternative->m_minimumSize)
michael@0 1900 sub32(Imm32(alternative->m_minimumSize - 1), regT0);
michael@0 1901 else
michael@0 1902 add32(TrustedImm32(1), regT0);
michael@0 1903 setMatchStart(regT0);
michael@0 1904 }
michael@0 1905 }
michael@0 1906
michael@0 1907 // Generate code to loop. Check whether the last alternative is longer than the
michael@0 1908 // first (e.g. /a|xy/ or /a|xyz/).
michael@0 1909 if (alternative->m_minimumSize > beginOp->m_alternative->m_minimumSize) {
michael@0 1910 // We want to loop, and increment input position. If the delta is 1, it is
michael@0 1911 // already correctly incremented, if more than one then decrement as appropriate.
michael@0 1912 unsigned delta = alternative->m_minimumSize - beginOp->m_alternative->m_minimumSize;
michael@0 1913 ASSERT(delta);
michael@0 1914 if (delta != 1)
michael@0 1915 sub32(Imm32(delta - 1), index);
michael@0 1916 jump(beginOp->m_reentry);
michael@0 1917 } else {
michael@0 1918 // If the first alternative has minimum size 0xFFFFFFFFu, then there cannot
michael@0 1919 // be sufficent input available to handle this, so just fall through.
michael@0 1920 unsigned delta = beginOp->m_alternative->m_minimumSize - alternative->m_minimumSize;
michael@0 1921 if (delta != 0xFFFFFFFFu) {
michael@0 1922 // We need to check input because we are incrementing the input.
michael@0 1923 add32(Imm32(delta + 1), index);
michael@0 1924 checkInput().linkTo(beginOp->m_reentry, this);
michael@0 1925 }
michael@0 1926 }
michael@0 1927 }
michael@0 1928 }
michael@0 1929
michael@0 1930 // We can reach this point in the code in two ways:
michael@0 1931 // - Fallthrough from the code above (a repeating alternative backtracked out of its
michael@0 1932 // last alternative, and did not have sufficent input to run the first).
michael@0 1933 // - We will loop back up to the following label when a releating alternative loops,
michael@0 1934 // following a failed input check.
michael@0 1935 //
michael@0 1936 // Either way, we have just failed the input check for the first alternative.
michael@0 1937 Label firstInputCheckFailed(this);
michael@0 1938
michael@0 1939 // Generate code to handle input check failures from alternatives except the last.
michael@0 1940 // prevOp is the alternative we're handling a bail out from (initially Begin), and
michael@0 1941 // nextOp is the alternative we will be attempting to reenter into.
michael@0 1942 //
michael@0 1943 // We will link input check failures from the forwards matching path back to the code
michael@0 1944 // that can handle them.
michael@0 1945 YarrOp* prevOp = beginOp;
michael@0 1946 YarrOp* nextOp = &m_ops[beginOp->m_nextOp];
michael@0 1947 while (nextOp->m_op != OpBodyAlternativeEnd) {
michael@0 1948 prevOp->m_jumps.link(this);
michael@0 1949
michael@0 1950 // We only get here if an input check fails, it is only worth checking again
michael@0 1951 // if the next alternative has a minimum size less than the last.
michael@0 1952 if (prevOp->m_alternative->m_minimumSize > nextOp->m_alternative->m_minimumSize) {
michael@0 1953 // FIXME: if we added an extra label to YarrOp, we could avoid needing to
michael@0 1954 // subtract delta back out, and reduce this code. Should performance test
michael@0 1955 // the benefit of this.
michael@0 1956 unsigned delta = prevOp->m_alternative->m_minimumSize - nextOp->m_alternative->m_minimumSize;
michael@0 1957 sub32(Imm32(delta), index);
michael@0 1958 Jump fail = jumpIfNoAvailableInput();
michael@0 1959 add32(Imm32(delta), index);
michael@0 1960 jump(nextOp->m_reentry);
michael@0 1961 fail.link(this);
michael@0 1962 } else if (prevOp->m_alternative->m_minimumSize < nextOp->m_alternative->m_minimumSize)
michael@0 1963 add32(Imm32(nextOp->m_alternative->m_minimumSize - prevOp->m_alternative->m_minimumSize), index);
michael@0 1964 prevOp = nextOp;
michael@0 1965 nextOp = &m_ops[nextOp->m_nextOp];
michael@0 1966 }
michael@0 1967
michael@0 1968 // We fall through to here if there is insufficient input to run the last alternative.
michael@0 1969
michael@0 1970 // If there is insufficient input to run the last alternative, then for 'once through'
michael@0 1971 // alternatives we are done - just jump back up into the forwards matching path at the End.
michael@0 1972 if (onceThrough) {
michael@0 1973 op.m_jumps.linkTo(endOp.m_reentry, this);
michael@0 1974 jump(endOp.m_reentry);
michael@0 1975 break;
michael@0 1976 }
michael@0 1977
michael@0 1978 // For repeating alternatives, link any input check failure from the last alternative to
michael@0 1979 // this point.
michael@0 1980 op.m_jumps.link(this);
michael@0 1981
michael@0 1982 bool needsToUpdateMatchStart = !m_pattern.m_body->m_hasFixedSize;
michael@0 1983
michael@0 1984 // Check for cases where input position is already incremented by 1 for the last
michael@0 1985 // alternative (this is particularly useful where the minimum size of the body
michael@0 1986 // disjunction is 0, e.g. /a*|b/).
michael@0 1987 if (needsToUpdateMatchStart && alternative->m_minimumSize == 1) {
michael@0 1988 // index is already incremented by 1, so just store it now!
michael@0 1989 setMatchStart(index);
michael@0 1990 needsToUpdateMatchStart = false;
michael@0 1991 }
michael@0 1992
michael@0 1993 // Check whether there is sufficient input to loop. Increment the input position by
michael@0 1994 // one, and check. Also add in the minimum disjunction size before checking - there
michael@0 1995 // is no point in looping if we're just going to fail all the input checks around
michael@0 1996 // the next iteration.
michael@0 1997 ASSERT(alternative->m_minimumSize >= m_pattern.m_body->m_minimumSize);
michael@0 1998 if (alternative->m_minimumSize == m_pattern.m_body->m_minimumSize) {
michael@0 1999 // If the last alternative had the same minimum size as the disjunction,
michael@0 2000 // just simply increment input pos by 1, no adjustment based on minimum size.
michael@0 2001 add32(TrustedImm32(1), index);
michael@0 2002 } else {
michael@0 2003 // If the minumum for the last alternative was one greater than than that
michael@0 2004 // for the disjunction, we're already progressed by 1, nothing to do!
michael@0 2005 unsigned delta = (alternative->m_minimumSize - m_pattern.m_body->m_minimumSize) - 1;
michael@0 2006 if (delta)
michael@0 2007 sub32(Imm32(delta), index);
michael@0 2008 }
michael@0 2009 Jump matchFailed = jumpIfNoAvailableInput();
michael@0 2010
michael@0 2011 if (needsToUpdateMatchStart) {
michael@0 2012 if (!m_pattern.m_body->m_minimumSize)
michael@0 2013 setMatchStart(index);
michael@0 2014 else {
michael@0 2015 move(index, regT0);
michael@0 2016 sub32(Imm32(m_pattern.m_body->m_minimumSize), regT0);
michael@0 2017 setMatchStart(regT0);
michael@0 2018 }
michael@0 2019 }
michael@0 2020
michael@0 2021 // Calculate how much more input the first alternative requires than the minimum
michael@0 2022 // for the body as a whole. If no more is needed then we dont need an additional
michael@0 2023 // input check here - jump straight back up to the start of the first alternative.
michael@0 2024 if (beginOp->m_alternative->m_minimumSize == m_pattern.m_body->m_minimumSize)
michael@0 2025 jump(beginOp->m_reentry);
michael@0 2026 else {
michael@0 2027 if (beginOp->m_alternative->m_minimumSize > m_pattern.m_body->m_minimumSize)
michael@0 2028 add32(Imm32(beginOp->m_alternative->m_minimumSize - m_pattern.m_body->m_minimumSize), index);
michael@0 2029 else
michael@0 2030 sub32(Imm32(m_pattern.m_body->m_minimumSize - beginOp->m_alternative->m_minimumSize), index);
michael@0 2031 checkInput().linkTo(beginOp->m_reentry, this);
michael@0 2032 jump(firstInputCheckFailed);
michael@0 2033 }
michael@0 2034
michael@0 2035 // We jump to here if we iterate to the point that there is insufficient input to
michael@0 2036 // run any matches, and need to return a failure state from JIT code.
michael@0 2037 matchFailed.link(this);
michael@0 2038
michael@0 2039 #if !WTF_CPU_SPARC
michael@0 2040 removeCallFrame();
michael@0 2041 #endif
michael@0 2042 #if WTF_CPU_X86_64
michael@0 2043 move(TrustedImm32(int(WTF::notFound)), returnRegister);
michael@0 2044 #else
michael@0 2045 move(TrustedImmPtr((void*)WTF::notFound), returnRegister);
michael@0 2046 move(TrustedImm32(0), returnRegister2);
michael@0 2047 #endif
michael@0 2048 generateReturn();
michael@0 2049 break;
michael@0 2050 }
michael@0 2051 case OpBodyAlternativeEnd: {
michael@0 2052 // We should never backtrack back into a body disjunction.
michael@0 2053 ASSERT(m_backtrackingState.isEmpty());
michael@0 2054
michael@0 2055 PatternAlternative* priorAlternative = m_ops[op.m_previousOp].m_alternative;
michael@0 2056 m_checked += priorAlternative->m_minimumSize;
michael@0 2057 break;
michael@0 2058 }
michael@0 2059
michael@0 2060 // OpSimpleNestedAlternativeBegin/Next/End
michael@0 2061 // OpNestedAlternativeBegin/Next/End
michael@0 2062 //
michael@0 2063 // Generate code for when we backtrack back out of an alternative into
michael@0 2064 // a Begin or Next node, or when the entry input count check fails. If
michael@0 2065 // there are more alternatives we need to jump to the next alternative,
michael@0 2066 // if not we backtrack back out of the current set of parentheses.
michael@0 2067 //
michael@0 2068 // In the case of non-simple nested assertions we need to also link the
michael@0 2069 // 'return address' appropriately to backtrack back out into the correct
michael@0 2070 // alternative.
michael@0 2071 case OpSimpleNestedAlternativeBegin:
michael@0 2072 case OpSimpleNestedAlternativeNext:
michael@0 2073 case OpNestedAlternativeBegin:
michael@0 2074 case OpNestedAlternativeNext: {
michael@0 2075 YarrOp& nextOp = m_ops[op.m_nextOp];
michael@0 2076 bool isBegin = op.m_previousOp == notFound;
michael@0 2077 bool isLastAlternative = nextOp.m_nextOp == notFound;
michael@0 2078 ASSERT(isBegin == (op.m_op == OpSimpleNestedAlternativeBegin || op.m_op == OpNestedAlternativeBegin));
michael@0 2079 ASSERT(isLastAlternative == (nextOp.m_op == OpSimpleNestedAlternativeEnd || nextOp.m_op == OpNestedAlternativeEnd));
michael@0 2080
michael@0 2081 // Treat an input check failure the same as a failed match.
michael@0 2082 m_backtrackingState.append(op.m_jumps);
michael@0 2083
michael@0 2084 // Set the backtracks to jump to the appropriate place. We may need
michael@0 2085 // to link the backtracks in one of three different way depending on
michael@0 2086 // the type of alternative we are dealing with:
michael@0 2087 // - A single alternative, with no simplings.
michael@0 2088 // - The last alternative of a set of two or more.
michael@0 2089 // - An alternative other than the last of a set of two or more.
michael@0 2090 //
michael@0 2091 // In the case of a single alternative on its own, we don't need to
michael@0 2092 // jump anywhere - if the alternative fails to match we can just
michael@0 2093 // continue to backtrack out of the parentheses without jumping.
michael@0 2094 //
michael@0 2095 // In the case of the last alternative in a set of more than one, we
michael@0 2096 // need to jump to return back out to the beginning. We'll do so by
michael@0 2097 // adding a jump to the End node's m_jumps list, and linking this
michael@0 2098 // when we come to generate the Begin node. For alternatives other
michael@0 2099 // than the last, we need to jump to the next alternative.
michael@0 2100 //
michael@0 2101 // If the alternative had adjusted the input position we must link
michael@0 2102 // backtracking to here, correct, and then jump on. If not we can
michael@0 2103 // link the backtracks directly to their destination.
michael@0 2104 if (op.m_checkAdjust) {
michael@0 2105 // Handle the cases where we need to link the backtracks here.
michael@0 2106 m_backtrackingState.link(this);
michael@0 2107 sub32(Imm32(op.m_checkAdjust), index);
michael@0 2108 if (!isLastAlternative) {
michael@0 2109 // An alternative that is not the last should jump to its successor.
michael@0 2110 jump(nextOp.m_reentry);
michael@0 2111 } else if (!isBegin) {
michael@0 2112 // The last of more than one alternatives must jump back to the beginning.
michael@0 2113 nextOp.m_jumps.append(jump());
michael@0 2114 } else {
michael@0 2115 // A single alternative on its own can fall through.
michael@0 2116 m_backtrackingState.fallthrough();
michael@0 2117 }
michael@0 2118 } else {
michael@0 2119 // Handle the cases where we can link the backtracks directly to their destinations.
michael@0 2120 if (!isLastAlternative) {
michael@0 2121 // An alternative that is not the last should jump to its successor.
michael@0 2122 m_backtrackingState.linkTo(nextOp.m_reentry, this);
michael@0 2123 } else if (!isBegin) {
michael@0 2124 // The last of more than one alternatives must jump back to the beginning.
michael@0 2125 m_backtrackingState.takeBacktracksToJumpList(nextOp.m_jumps, this);
michael@0 2126 }
michael@0 2127 // In the case of a single alternative on its own do nothing - it can fall through.
michael@0 2128 }
michael@0 2129
michael@0 2130 // If there is a backtrack jump from a zero length match link it here.
michael@0 2131 if (op.m_zeroLengthMatch.isSet())
michael@0 2132 m_backtrackingState.append(op.m_zeroLengthMatch);
michael@0 2133
michael@0 2134 // At this point we've handled the backtracking back into this node.
michael@0 2135 // Now link any backtracks that need to jump to here.
michael@0 2136
michael@0 2137 // For non-simple alternatives, link the alternative's 'return address'
michael@0 2138 // so that we backtrack back out into the previous alternative.
michael@0 2139 if (op.m_op == OpNestedAlternativeNext)
michael@0 2140 m_backtrackingState.append(op.m_returnAddress);
michael@0 2141
michael@0 2142 // If there is more than one alternative, then the last alternative will
michael@0 2143 // have planted a jump to be linked to the end. This jump was added to the
michael@0 2144 // End node's m_jumps list. If we are back at the beginning, link it here.
michael@0 2145 if (isBegin) {
michael@0 2146 YarrOp* endOp = &m_ops[op.m_nextOp];
michael@0 2147 while (endOp->m_nextOp != notFound) {
michael@0 2148 ASSERT(endOp->m_op == OpSimpleNestedAlternativeNext || endOp->m_op == OpNestedAlternativeNext);
michael@0 2149 endOp = &m_ops[endOp->m_nextOp];
michael@0 2150 }
michael@0 2151 ASSERT(endOp->m_op == OpSimpleNestedAlternativeEnd || endOp->m_op == OpNestedAlternativeEnd);
michael@0 2152 m_backtrackingState.append(endOp->m_jumps);
michael@0 2153 }
michael@0 2154
michael@0 2155 if (!isBegin) {
michael@0 2156 YarrOp& lastOp = m_ops[op.m_previousOp];
michael@0 2157 m_checked += lastOp.m_checkAdjust;
michael@0 2158 }
michael@0 2159 m_checked -= op.m_checkAdjust;
michael@0 2160 break;
michael@0 2161 }
michael@0 2162 case OpSimpleNestedAlternativeEnd:
michael@0 2163 case OpNestedAlternativeEnd: {
michael@0 2164 PatternTerm* term = op.m_term;
michael@0 2165
michael@0 2166 // If there is a backtrack jump from a zero length match link it here.
michael@0 2167 if (op.m_zeroLengthMatch.isSet())
michael@0 2168 m_backtrackingState.append(op.m_zeroLengthMatch);
michael@0 2169
michael@0 2170 // If we backtrack into the end of a simple subpattern do nothing;
michael@0 2171 // just continue through into the last alternative. If we backtrack
michael@0 2172 // into the end of a non-simple set of alterntives we need to jump
michael@0 2173 // to the backtracking return address set up during generation.
michael@0 2174 if (op.m_op == OpNestedAlternativeEnd) {
michael@0 2175 m_backtrackingState.link(this);
michael@0 2176
michael@0 2177 // Plant a jump to the return address.
michael@0 2178 unsigned parenthesesFrameLocation = term->frameLocation;
michael@0 2179 unsigned alternativeFrameLocation = parenthesesFrameLocation;
michael@0 2180 if (term->quantityType != QuantifierFixedCount)
michael@0 2181 alternativeFrameLocation += YarrStackSpaceForBackTrackInfoParenthesesOnce;
michael@0 2182 loadFromFrameAndJump(alternativeFrameLocation);
michael@0 2183
michael@0 2184 // Link the DataLabelPtr associated with the end of the last
michael@0 2185 // alternative to this point.
michael@0 2186 m_backtrackingState.append(op.m_returnAddress);
michael@0 2187 }
michael@0 2188
michael@0 2189 YarrOp& lastOp = m_ops[op.m_previousOp];
michael@0 2190 m_checked += lastOp.m_checkAdjust;
michael@0 2191 break;
michael@0 2192 }
michael@0 2193
michael@0 2194 // OpParenthesesSubpatternOnceBegin/End
michael@0 2195 //
michael@0 2196 // When we are backtracking back out of a capturing subpattern we need
michael@0 2197 // to clear the start index in the matches output array, to record that
michael@0 2198 // this subpattern has not been captured.
michael@0 2199 //
michael@0 2200 // When backtracking back out of a Greedy quantified subpattern we need
michael@0 2201 // to catch this, and try running the remainder of the alternative after
michael@0 2202 // the subpattern again, skipping the parentheses.
michael@0 2203 //
michael@0 2204 // Upon backtracking back into a quantified set of parentheses we need to
michael@0 2205 // check whether we were currently skipping the subpattern. If not, we
michael@0 2206 // can backtrack into them, if we were we need to either backtrack back
michael@0 2207 // out of the start of the parentheses, or jump back to the forwards
michael@0 2208 // matching start, depending of whether the match is Greedy or NonGreedy.
michael@0 2209 case OpParenthesesSubpatternOnceBegin: {
michael@0 2210 PatternTerm* term = op.m_term;
michael@0 2211 ASSERT(term->quantityCount == 1);
michael@0 2212
michael@0 2213 // We only need to backtrack to thispoint if capturing or greedy.
michael@0 2214 if ((term->capture() && compileMode == IncludeSubpatterns) || term->quantityType == QuantifierGreedy) {
michael@0 2215 m_backtrackingState.link(this);
michael@0 2216
michael@0 2217 // If capturing, clear the capture (we only need to reset start).
michael@0 2218 if (term->capture() && compileMode == IncludeSubpatterns)
michael@0 2219 clearSubpatternStart(term->parentheses.subpatternId);
michael@0 2220
michael@0 2221 // If Greedy, jump to the end.
michael@0 2222 if (term->quantityType == QuantifierGreedy) {
michael@0 2223 // Clear the flag in the stackframe indicating we ran through the subpattern.
michael@0 2224 unsigned parenthesesFrameLocation = term->frameLocation;
michael@0 2225 storeToFrame(TrustedImm32(-1), parenthesesFrameLocation);
michael@0 2226 // Jump to after the parentheses, skipping the subpattern.
michael@0 2227 jump(m_ops[op.m_nextOp].m_reentry);
michael@0 2228 // A backtrack from after the parentheses, when skipping the subpattern,
michael@0 2229 // will jump back to here.
michael@0 2230 op.m_jumps.link(this);
michael@0 2231 }
michael@0 2232
michael@0 2233 m_backtrackingState.fallthrough();
michael@0 2234 }
michael@0 2235 break;
michael@0 2236 }
michael@0 2237 case OpParenthesesSubpatternOnceEnd: {
michael@0 2238 PatternTerm* term = op.m_term;
michael@0 2239
michael@0 2240 if (term->quantityType != QuantifierFixedCount) {
michael@0 2241 m_backtrackingState.link(this);
michael@0 2242
michael@0 2243 // Check whether we should backtrack back into the parentheses, or if we
michael@0 2244 // are currently in a state where we had skipped over the subpattern
michael@0 2245 // (in which case the flag value on the stack will be -1).
michael@0 2246 unsigned parenthesesFrameLocation = term->frameLocation;
michael@0 2247 Jump hadSkipped = branch32(Equal, Address(stackPointerRegister, parenthesesFrameLocation * sizeof(void*)), TrustedImm32(-1));
michael@0 2248
michael@0 2249 if (term->quantityType == QuantifierGreedy) {
michael@0 2250 // For Greedy parentheses, we skip after having already tried going
michael@0 2251 // through the subpattern, so if we get here we're done.
michael@0 2252 YarrOp& beginOp = m_ops[op.m_previousOp];
michael@0 2253 beginOp.m_jumps.append(hadSkipped);
michael@0 2254 } else {
michael@0 2255 // For NonGreedy parentheses, we try skipping the subpattern first,
michael@0 2256 // so if we get here we need to try running through the subpattern
michael@0 2257 // next. Jump back to the start of the parentheses in the forwards
michael@0 2258 // matching path.
michael@0 2259 ASSERT(term->quantityType == QuantifierNonGreedy);
michael@0 2260 YarrOp& beginOp = m_ops[op.m_previousOp];
michael@0 2261 hadSkipped.linkTo(beginOp.m_reentry, this);
michael@0 2262 }
michael@0 2263
michael@0 2264 m_backtrackingState.fallthrough();
michael@0 2265 }
michael@0 2266
michael@0 2267 m_backtrackingState.append(op.m_jumps);
michael@0 2268 break;
michael@0 2269 }
michael@0 2270
michael@0 2271 // OpParenthesesSubpatternTerminalBegin/End
michael@0 2272 //
michael@0 2273 // Terminal subpatterns will always match - there is nothing after them to
michael@0 2274 // force a backtrack, and they have a minimum count of 0, and as such will
michael@0 2275 // always produce an acceptable result.
michael@0 2276 case OpParenthesesSubpatternTerminalBegin: {
michael@0 2277 // We will backtrack to this point once the subpattern cannot match any
michael@0 2278 // more. Since no match is accepted as a successful match (we are Greedy
michael@0 2279 // quantified with a minimum of zero) jump back to the forwards matching
michael@0 2280 // path at the end.
michael@0 2281 YarrOp& endOp = m_ops[op.m_nextOp];
michael@0 2282 m_backtrackingState.linkTo(endOp.m_reentry, this);
michael@0 2283 break;
michael@0 2284 }
michael@0 2285 case OpParenthesesSubpatternTerminalEnd:
michael@0 2286 // We should never be backtracking to here (hence the 'terminal' in the name).
michael@0 2287 ASSERT(m_backtrackingState.isEmpty());
michael@0 2288 m_backtrackingState.append(op.m_jumps);
michael@0 2289 break;
michael@0 2290
michael@0 2291 // OpParentheticalAssertionBegin/End
michael@0 2292 case OpParentheticalAssertionBegin: {
michael@0 2293 PatternTerm* term = op.m_term;
michael@0 2294 YarrOp& endOp = m_ops[op.m_nextOp];
michael@0 2295
michael@0 2296 // We need to handle the backtracks upon backtracking back out
michael@0 2297 // of a parenthetical assertion if either we need to correct
michael@0 2298 // the input index, or the assertion was inverted.
michael@0 2299 if (op.m_checkAdjust || term->invert()) {
michael@0 2300 m_backtrackingState.link(this);
michael@0 2301
michael@0 2302 if (op.m_checkAdjust)
michael@0 2303 add32(Imm32(op.m_checkAdjust), index);
michael@0 2304
michael@0 2305 // In an inverted assertion failure to match the subpattern
michael@0 2306 // is treated as a successful match - jump to the end of the
michael@0 2307 // subpattern. We already have adjusted the input position
michael@0 2308 // back to that before the assertion, which is correct.
michael@0 2309 if (term->invert())
michael@0 2310 jump(endOp.m_reentry);
michael@0 2311
michael@0 2312 m_backtrackingState.fallthrough();
michael@0 2313 }
michael@0 2314
michael@0 2315 // The End node's jump list will contain any backtracks into
michael@0 2316 // the end of the assertion. Also, if inverted, we will have
michael@0 2317 // added the failure caused by a successful match to this.
michael@0 2318 m_backtrackingState.append(endOp.m_jumps);
michael@0 2319
michael@0 2320 m_checked += op.m_checkAdjust;
michael@0 2321 break;
michael@0 2322 }
michael@0 2323 case OpParentheticalAssertionEnd: {
michael@0 2324 // FIXME: We should really be clearing any nested subpattern
michael@0 2325 // matches on bailing out from after the pattern. Firefox has
michael@0 2326 // this bug too (presumably because they use YARR!)
michael@0 2327
michael@0 2328 // Never backtrack into an assertion; later failures bail to before the begin.
michael@0 2329 m_backtrackingState.takeBacktracksToJumpList(op.m_jumps, this);
michael@0 2330
michael@0 2331 YarrOp& lastOp = m_ops[op.m_previousOp];
michael@0 2332 m_checked -= lastOp.m_checkAdjust;
michael@0 2333 break;
michael@0 2334 }
michael@0 2335
michael@0 2336 case OpMatchFailed:
michael@0 2337 break;
michael@0 2338 }
michael@0 2339
michael@0 2340 } while (opIndex);
michael@0 2341
michael@0 2342 return true;
michael@0 2343 }
michael@0 2344
michael@0 2345 // Compilation methods:
michael@0 2346 // ====================
michael@0 2347
michael@0 2348 // opCompileParenthesesSubpattern
michael@0 2349 // Emits ops for a subpattern (set of parentheses). These consist
michael@0 2350 // of a set of alternatives wrapped in an outer set of nodes for
michael@0 2351 // the parentheses.
michael@0 2352 // Supported types of parentheses are 'Once' (quantityCount == 1)
michael@0 2353 // and 'Terminal' (non-capturing parentheses quantified as greedy
michael@0 2354 // and infinite).
michael@0 2355 // Alternatives will use the 'Simple' set of ops if either the
michael@0 2356 // subpattern is terminal (in which case we will never need to
michael@0 2357 // backtrack), or if the subpattern only contains one alternative.
michael@0 2358 void opCompileParenthesesSubpattern(PatternTerm* term)
michael@0 2359 {
michael@0 2360 YarrOpCode parenthesesBeginOpCode;
michael@0 2361 YarrOpCode parenthesesEndOpCode;
michael@0 2362 YarrOpCode alternativeBeginOpCode = OpSimpleNestedAlternativeBegin;
michael@0 2363 YarrOpCode alternativeNextOpCode = OpSimpleNestedAlternativeNext;
michael@0 2364 YarrOpCode alternativeEndOpCode = OpSimpleNestedAlternativeEnd;
michael@0 2365
michael@0 2366 // We can currently only compile quantity 1 subpatterns that are
michael@0 2367 // not copies. We generate a copy in the case of a range quantifier,
michael@0 2368 // e.g. /(?:x){3,9}/, or /(?:x)+/ (These are effectively expanded to
michael@0 2369 // /(?:x){3,3}(?:x){0,6}/ and /(?:x)(?:x)*/ repectively). The problem
michael@0 2370 // comes where the subpattern is capturing, in which case we would
michael@0 2371 // need to restore the capture from the first subpattern upon a
michael@0 2372 // failure in the second.
michael@0 2373 if (term->quantityCount == 1 && !term->parentheses.isCopy) {
michael@0 2374 // Select the 'Once' nodes.
michael@0 2375 parenthesesBeginOpCode = OpParenthesesSubpatternOnceBegin;
michael@0 2376 parenthesesEndOpCode = OpParenthesesSubpatternOnceEnd;
michael@0 2377
michael@0 2378 // If there is more than one alternative we cannot use the 'simple' nodes.
michael@0 2379 if (term->parentheses.disjunction->m_alternatives.size() != 1) {
michael@0 2380 alternativeBeginOpCode = OpNestedAlternativeBegin;
michael@0 2381 alternativeNextOpCode = OpNestedAlternativeNext;
michael@0 2382 alternativeEndOpCode = OpNestedAlternativeEnd;
michael@0 2383 }
michael@0 2384 } else if (term->parentheses.isTerminal) {
michael@0 2385 // Terminal groups are optimized on the assumption that matching will never
michael@0 2386 // backtrack into the terminal group. But this is false if there is more
michael@0 2387 // than one alternative and one of the alternatives can match empty. In that
michael@0 2388 // case, the empty match is counted as a failure, so we would need to backtrack.
michael@0 2389 // The backtracking code doesn't handle this case correctly, so we fall back
michael@0 2390 // to the interpreter.
michael@0 2391 Vector<PatternAlternative*>& alternatives = term->parentheses.disjunction->m_alternatives;
michael@0 2392 if (alternatives.size() != 1) {
michael@0 2393 for (unsigned i = 0; i < alternatives.size(); ++i) {
michael@0 2394 if (alternatives[i]->m_minimumSize == 0) {
michael@0 2395 m_shouldFallBack = true;
michael@0 2396 return;
michael@0 2397 }
michael@0 2398 }
michael@0 2399 }
michael@0 2400
michael@0 2401 // Select the 'Terminal' nodes.
michael@0 2402 parenthesesBeginOpCode = OpParenthesesSubpatternTerminalBegin;
michael@0 2403 parenthesesEndOpCode = OpParenthesesSubpatternTerminalEnd;
michael@0 2404 } else {
michael@0 2405 // This subpattern is not supported by the JIT.
michael@0 2406 m_shouldFallBack = true;
michael@0 2407 return;
michael@0 2408 }
michael@0 2409
michael@0 2410 size_t parenBegin = m_ops.size();
michael@0 2411 m_ops.append(parenthesesBeginOpCode);
michael@0 2412
michael@0 2413 m_ops.append(alternativeBeginOpCode);
michael@0 2414 m_ops.last().m_previousOp = notFound;
michael@0 2415 m_ops.last().m_term = term;
michael@0 2416 Vector<PatternAlternative*>& alternatives = term->parentheses.disjunction->m_alternatives;
michael@0 2417 for (unsigned i = 0; i < alternatives.size(); ++i) {
michael@0 2418 size_t lastOpIndex = m_ops.size() - 1;
michael@0 2419
michael@0 2420 PatternAlternative* nestedAlternative = alternatives[i];
michael@0 2421 opCompileAlternative(nestedAlternative);
michael@0 2422
michael@0 2423 size_t thisOpIndex = m_ops.size();
michael@0 2424 m_ops.append(YarrOp(alternativeNextOpCode));
michael@0 2425
michael@0 2426 YarrOp& lastOp = m_ops[lastOpIndex];
michael@0 2427 YarrOp& thisOp = m_ops[thisOpIndex];
michael@0 2428
michael@0 2429 lastOp.m_alternative = nestedAlternative;
michael@0 2430 lastOp.m_nextOp = thisOpIndex;
michael@0 2431 thisOp.m_previousOp = lastOpIndex;
michael@0 2432 thisOp.m_term = term;
michael@0 2433 }
michael@0 2434 YarrOp& lastOp = m_ops.last();
michael@0 2435 ASSERT(lastOp.m_op == alternativeNextOpCode);
michael@0 2436 lastOp.m_op = alternativeEndOpCode;
michael@0 2437 lastOp.m_alternative = 0;
michael@0 2438 lastOp.m_nextOp = notFound;
michael@0 2439
michael@0 2440 size_t parenEnd = m_ops.size();
michael@0 2441 m_ops.append(parenthesesEndOpCode);
michael@0 2442
michael@0 2443 m_ops[parenBegin].m_term = term;
michael@0 2444 m_ops[parenBegin].m_previousOp = notFound;
michael@0 2445 m_ops[parenBegin].m_nextOp = parenEnd;
michael@0 2446 m_ops[parenEnd].m_term = term;
michael@0 2447 m_ops[parenEnd].m_previousOp = parenBegin;
michael@0 2448 m_ops[parenEnd].m_nextOp = notFound;
michael@0 2449 }
michael@0 2450
michael@0 2451 // opCompileParentheticalAssertion
michael@0 2452 // Emits ops for a parenthetical assertion. These consist of an
michael@0 2453 // OpSimpleNestedAlternativeBegin/Next/End set of nodes wrapping
michael@0 2454 // the alternatives, with these wrapped by an outer pair of
michael@0 2455 // OpParentheticalAssertionBegin/End nodes.
michael@0 2456 // We can always use the OpSimpleNestedAlternative nodes in the
michael@0 2457 // case of parenthetical assertions since these only ever match
michael@0 2458 // once, and will never backtrack back into the assertion.
michael@0 2459 void opCompileParentheticalAssertion(PatternTerm* term)
michael@0 2460 {
michael@0 2461 size_t parenBegin = m_ops.size();
michael@0 2462 m_ops.append(OpParentheticalAssertionBegin);
michael@0 2463
michael@0 2464 m_ops.append(OpSimpleNestedAlternativeBegin);
michael@0 2465 m_ops.last().m_previousOp = notFound;
michael@0 2466 m_ops.last().m_term = term;
michael@0 2467 Vector<PatternAlternative*>& alternatives = term->parentheses.disjunction->m_alternatives;
michael@0 2468 for (unsigned i = 0; i < alternatives.size(); ++i) {
michael@0 2469 size_t lastOpIndex = m_ops.size() - 1;
michael@0 2470
michael@0 2471 PatternAlternative* nestedAlternative = alternatives[i];
michael@0 2472 opCompileAlternative(nestedAlternative);
michael@0 2473
michael@0 2474 size_t thisOpIndex = m_ops.size();
michael@0 2475 m_ops.append(YarrOp(OpSimpleNestedAlternativeNext));
michael@0 2476
michael@0 2477 YarrOp& lastOp = m_ops[lastOpIndex];
michael@0 2478 YarrOp& thisOp = m_ops[thisOpIndex];
michael@0 2479
michael@0 2480 lastOp.m_alternative = nestedAlternative;
michael@0 2481 lastOp.m_nextOp = thisOpIndex;
michael@0 2482 thisOp.m_previousOp = lastOpIndex;
michael@0 2483 thisOp.m_term = term;
michael@0 2484 }
michael@0 2485 YarrOp& lastOp = m_ops.last();
michael@0 2486 ASSERT(lastOp.m_op == OpSimpleNestedAlternativeNext);
michael@0 2487 lastOp.m_op = OpSimpleNestedAlternativeEnd;
michael@0 2488 lastOp.m_alternative = 0;
michael@0 2489 lastOp.m_nextOp = notFound;
michael@0 2490
michael@0 2491 size_t parenEnd = m_ops.size();
michael@0 2492 m_ops.append(OpParentheticalAssertionEnd);
michael@0 2493
michael@0 2494 m_ops[parenBegin].m_term = term;
michael@0 2495 m_ops[parenBegin].m_previousOp = notFound;
michael@0 2496 m_ops[parenBegin].m_nextOp = parenEnd;
michael@0 2497 m_ops[parenEnd].m_term = term;
michael@0 2498 m_ops[parenEnd].m_previousOp = parenBegin;
michael@0 2499 m_ops[parenEnd].m_nextOp = notFound;
michael@0 2500 }
michael@0 2501
michael@0 2502 // opCompileAlternative
michael@0 2503 // Called to emit nodes for all terms in an alternative.
michael@0 2504 void opCompileAlternative(PatternAlternative* alternative)
michael@0 2505 {
michael@0 2506 optimizeAlternative(alternative);
michael@0 2507
michael@0 2508 for (unsigned i = 0; i < alternative->m_terms.size(); ++i) {
michael@0 2509 PatternTerm* term = &alternative->m_terms[i];
michael@0 2510
michael@0 2511 switch (term->type) {
michael@0 2512 case PatternTerm::TypeParenthesesSubpattern:
michael@0 2513 opCompileParenthesesSubpattern(term);
michael@0 2514 break;
michael@0 2515
michael@0 2516 case PatternTerm::TypeParentheticalAssertion:
michael@0 2517 opCompileParentheticalAssertion(term);
michael@0 2518 break;
michael@0 2519
michael@0 2520 default:
michael@0 2521 m_ops.append(term);
michael@0 2522 }
michael@0 2523 }
michael@0 2524 }
michael@0 2525
michael@0 2526 // opCompileBody
michael@0 2527 // This method compiles the body disjunction of the regular expression.
michael@0 2528 // The body consists of two sets of alternatives - zero or more 'once
michael@0 2529 // through' (BOL anchored) alternatives, followed by zero or more
michael@0 2530 // repeated alternatives.
michael@0 2531 // For each of these two sets of alteratives, if not empty they will be
michael@0 2532 // wrapped in a set of OpBodyAlternativeBegin/Next/End nodes (with the
michael@0 2533 // 'begin' node referencing the first alternative, and 'next' nodes
michael@0 2534 // referencing any further alternatives. The begin/next/end nodes are
michael@0 2535 // linked together in a doubly linked list. In the case of repeating
michael@0 2536 // alternatives, the end node is also linked back to the beginning.
michael@0 2537 // If no repeating alternatives exist, then a OpMatchFailed node exists
michael@0 2538 // to return the failing result.
michael@0 2539 void opCompileBody(PatternDisjunction* disjunction)
michael@0 2540 {
michael@0 2541 Vector<PatternAlternative*>& alternatives = disjunction->m_alternatives;
michael@0 2542 size_t currentAlternativeIndex = 0;
michael@0 2543
michael@0 2544 // Emit the 'once through' alternatives.
michael@0 2545 if (alternatives.size() && alternatives[0]->onceThrough()) {
michael@0 2546 m_ops.append(YarrOp(OpBodyAlternativeBegin));
michael@0 2547 m_ops.last().m_previousOp = notFound;
michael@0 2548
michael@0 2549 do {
michael@0 2550 size_t lastOpIndex = m_ops.size() - 1;
michael@0 2551 PatternAlternative* alternative = alternatives[currentAlternativeIndex];
michael@0 2552 opCompileAlternative(alternative);
michael@0 2553
michael@0 2554 size_t thisOpIndex = m_ops.size();
michael@0 2555 m_ops.append(YarrOp(OpBodyAlternativeNext));
michael@0 2556
michael@0 2557 YarrOp& lastOp = m_ops[lastOpIndex];
michael@0 2558 YarrOp& thisOp = m_ops[thisOpIndex];
michael@0 2559
michael@0 2560 lastOp.m_alternative = alternative;
michael@0 2561 lastOp.m_nextOp = thisOpIndex;
michael@0 2562 thisOp.m_previousOp = lastOpIndex;
michael@0 2563
michael@0 2564 ++currentAlternativeIndex;
michael@0 2565 } while (currentAlternativeIndex < alternatives.size() && alternatives[currentAlternativeIndex]->onceThrough());
michael@0 2566
michael@0 2567 YarrOp& lastOp = m_ops.last();
michael@0 2568
michael@0 2569 ASSERT(lastOp.m_op == OpBodyAlternativeNext);
michael@0 2570 lastOp.m_op = OpBodyAlternativeEnd;
michael@0 2571 lastOp.m_alternative = 0;
michael@0 2572 lastOp.m_nextOp = notFound;
michael@0 2573 }
michael@0 2574
michael@0 2575 if (currentAlternativeIndex == alternatives.size()) {
michael@0 2576 m_ops.append(YarrOp(OpMatchFailed));
michael@0 2577 return;
michael@0 2578 }
michael@0 2579
michael@0 2580 // Emit the repeated alternatives.
michael@0 2581 size_t repeatLoop = m_ops.size();
michael@0 2582 m_ops.append(YarrOp(OpBodyAlternativeBegin));
michael@0 2583 m_ops.last().m_previousOp = notFound;
michael@0 2584 do {
michael@0 2585 size_t lastOpIndex = m_ops.size() - 1;
michael@0 2586 PatternAlternative* alternative = alternatives[currentAlternativeIndex];
michael@0 2587 ASSERT(!alternative->onceThrough());
michael@0 2588 opCompileAlternative(alternative);
michael@0 2589
michael@0 2590 size_t thisOpIndex = m_ops.size();
michael@0 2591 m_ops.append(YarrOp(OpBodyAlternativeNext));
michael@0 2592
michael@0 2593 YarrOp& lastOp = m_ops[lastOpIndex];
michael@0 2594 YarrOp& thisOp = m_ops[thisOpIndex];
michael@0 2595
michael@0 2596 lastOp.m_alternative = alternative;
michael@0 2597 lastOp.m_nextOp = thisOpIndex;
michael@0 2598 thisOp.m_previousOp = lastOpIndex;
michael@0 2599
michael@0 2600 ++currentAlternativeIndex;
michael@0 2601 } while (currentAlternativeIndex < alternatives.size());
michael@0 2602 YarrOp& lastOp = m_ops.last();
michael@0 2603 ASSERT(lastOp.m_op == OpBodyAlternativeNext);
michael@0 2604 lastOp.m_op = OpBodyAlternativeEnd;
michael@0 2605 lastOp.m_alternative = 0;
michael@0 2606 lastOp.m_nextOp = repeatLoop;
michael@0 2607 }
michael@0 2608
michael@0 2609 void generateEnter()
michael@0 2610 {
michael@0 2611 #if WTF_CPU_X86_64
michael@0 2612 push(X86Registers::ebp);
michael@0 2613 move(stackPointerRegister, X86Registers::ebp);
michael@0 2614 push(X86Registers::ebx);
michael@0 2615 // The ABI doesn't guarantee the upper bits are zero on unsigned arguments, so clear them ourselves.
michael@0 2616 zeroExtend32ToPtr(index, index);
michael@0 2617 zeroExtend32ToPtr(length, length);
michael@0 2618 #elif WTF_CPU_X86
michael@0 2619 push(X86Registers::ebp);
michael@0 2620 move(stackPointerRegister, X86Registers::ebp);
michael@0 2621 // TODO: do we need spill registers to fill the output pointer if there are no sub captures?
michael@0 2622 push(X86Registers::ebx);
michael@0 2623 push(X86Registers::edi);
michael@0 2624 push(X86Registers::esi);
michael@0 2625 // load output into edi (2 = saved ebp + return address).
michael@0 2626 # if WTF_COMPILER_MSVC || WTF_COMPILER_SUNCC
michael@0 2627 loadPtr(Address(X86Registers::ebp, 2 * sizeof(void*)), input);
michael@0 2628 loadPtr(Address(X86Registers::ebp, 3 * sizeof(void*)), index);
michael@0 2629 loadPtr(Address(X86Registers::ebp, 4 * sizeof(void*)), length);
michael@0 2630 if (compileMode == IncludeSubpatterns)
michael@0 2631 loadPtr(Address(X86Registers::ebp, 5 * sizeof(void*)), output);
michael@0 2632 # else
michael@0 2633 if (compileMode == IncludeSubpatterns)
michael@0 2634 loadPtr(Address(X86Registers::ebp, 2 * sizeof(void*)), output);
michael@0 2635 # endif
michael@0 2636 #elif WTF_CPU_ARM
michael@0 2637 push(ARMRegisters::r4);
michael@0 2638 push(ARMRegisters::r5);
michael@0 2639 push(ARMRegisters::r6);
michael@0 2640 # if WTF_CPU_ARM_TRADITIONAL
michael@0 2641 push(ARMRegisters::r8); // scratch register
michael@0 2642 # endif
michael@0 2643 if (compileMode == IncludeSubpatterns)
michael@0 2644 move(ARMRegisters::r3, output);
michael@0 2645 #elif WTF_CPU_SH4
michael@0 2646 push(SH4Registers::r11);
michael@0 2647 push(SH4Registers::r13);
michael@0 2648 #elif WTF_CPU_SPARC
michael@0 2649 save(Imm32(-m_pattern.m_body->m_callFrameSize * sizeof(void*)));
michael@0 2650 #elif WTF_CPU_MIPS
michael@0 2651 // Do nothing.
michael@0 2652 #endif
michael@0 2653 }
michael@0 2654
michael@0 2655 void generateReturn()
michael@0 2656 {
michael@0 2657 #if WTF_CPU_X86_64
michael@0 2658 pop(X86Registers::ebx);
michael@0 2659 pop(X86Registers::ebp);
michael@0 2660 #elif WTF_CPU_X86
michael@0 2661 pop(X86Registers::esi);
michael@0 2662 pop(X86Registers::edi);
michael@0 2663 pop(X86Registers::ebx);
michael@0 2664 pop(X86Registers::ebp);
michael@0 2665 #elif WTF_CPU_ARM
michael@0 2666 # if WTF_CPU_ARM_TRADITIONAL
michael@0 2667 pop(ARMRegisters::r8); // scratch register
michael@0 2668 # endif
michael@0 2669 pop(ARMRegisters::r6);
michael@0 2670 pop(ARMRegisters::r5);
michael@0 2671 pop(ARMRegisters::r4);
michael@0 2672 #elif WTF_CPU_SH4
michael@0 2673 pop(SH4Registers::r13);
michael@0 2674 pop(SH4Registers::r11);
michael@0 2675 #elif WTF_CPU_SPARC
michael@0 2676 ret_and_restore();
michael@0 2677 return;
michael@0 2678 #elif WTF_CPU_MIPS
michael@0 2679 // Do nothing
michael@0 2680 #endif
michael@0 2681 ret();
michael@0 2682 }
michael@0 2683
michael@0 2684 public:
michael@0 2685 YarrGenerator(YarrPattern& pattern, YarrCharSize charSize)
michael@0 2686 : m_pattern(pattern)
michael@0 2687 , m_charSize(charSize)
michael@0 2688 , m_charScale(m_charSize == Char8 ? TimesOne: TimesTwo)
michael@0 2689 , m_shouldFallBack(false)
michael@0 2690 , m_checked(0)
michael@0 2691 {
michael@0 2692 }
michael@0 2693
michael@0 2694 void compile(JSGlobalData* globalData, YarrCodeBlock& jitObject)
michael@0 2695 {
michael@0 2696 generateEnter();
michael@0 2697
michael@0 2698 Jump hasInput = checkInput();
michael@0 2699 #if WTF_CPU_X86_64
michael@0 2700 move(TrustedImm32(int(WTF::notFound)), returnRegister);
michael@0 2701 #else
michael@0 2702 move(TrustedImmPtr((void*)WTF::notFound), returnRegister);
michael@0 2703 move(TrustedImm32(0), returnRegister2);
michael@0 2704 #endif
michael@0 2705 generateReturn();
michael@0 2706 hasInput.link(this);
michael@0 2707
michael@0 2708 if (compileMode == IncludeSubpatterns) {
michael@0 2709 for (unsigned i = 0; i < m_pattern.m_numSubpatterns + 1; ++i)
michael@0 2710 store32(TrustedImm32(-1), Address(output, (i << 1) * sizeof(int)));
michael@0 2711 }
michael@0 2712
michael@0 2713 if (!m_pattern.m_body->m_hasFixedSize)
michael@0 2714 setMatchStart(index);
michael@0 2715
michael@0 2716 initCallFrame();
michael@0 2717
michael@0 2718 // Compile the pattern to the internal 'YarrOp' representation.
michael@0 2719 opCompileBody(m_pattern.m_body);
michael@0 2720
michael@0 2721 // If we encountered anything we can't handle in the JIT code
michael@0 2722 // (e.g. backreferences) then return early.
michael@0 2723 if (m_shouldFallBack) {
michael@0 2724 jitObject.setFallBack(true);
michael@0 2725 return;
michael@0 2726 }
michael@0 2727
michael@0 2728 if (!generate() || !backtrack()) {
michael@0 2729 jitObject.setFallBack(true);
michael@0 2730 return;
michael@0 2731 }
michael@0 2732
michael@0 2733 // Link & finalize the code.
michael@0 2734 ExecutablePool *pool;
michael@0 2735 bool ok;
michael@0 2736 LinkBuffer linkBuffer(this, globalData->regexAllocator, &pool, &ok, REGEXP_CODE);
michael@0 2737
michael@0 2738 // Attempt to detect OOM during linkBuffer creation.
michael@0 2739 if (linkBuffer.unsafeCode() == nullptr) {
michael@0 2740 jitObject.setFallBack(true);
michael@0 2741 return;
michael@0 2742 }
michael@0 2743
michael@0 2744 m_backtrackingState.linkDataLabels(linkBuffer);
michael@0 2745
michael@0 2746 if (compileMode == MatchOnly) {
michael@0 2747 #if YARR_8BIT_CHAR_SUPPORT
michael@0 2748 if (m_charSize == Char8)
michael@0 2749 jitObject.set8BitCodeMatchOnly(linkBuffer.finalizeCode());
michael@0 2750 else
michael@0 2751 #endif
michael@0 2752 jitObject.set16BitCodeMatchOnly(linkBuffer.finalizeCode());
michael@0 2753 } else {
michael@0 2754 #if YARR_8BIT_CHAR_SUPPORT
michael@0 2755 if (m_charSize == Char8)
michael@0 2756 jitObject.set8BitCode(linkBuffer.finalizeCode());
michael@0 2757 else
michael@0 2758 #endif
michael@0 2759 jitObject.set16BitCode(linkBuffer.finalizeCode());
michael@0 2760 }
michael@0 2761 jitObject.setFallBack(m_shouldFallBack);
michael@0 2762 }
michael@0 2763
michael@0 2764 private:
michael@0 2765 YarrPattern& m_pattern;
michael@0 2766
michael@0 2767 YarrCharSize m_charSize;
michael@0 2768
michael@0 2769 Scale m_charScale;
michael@0 2770
michael@0 2771 // Used to detect regular expression constructs that are not currently
michael@0 2772 // supported in the JIT; fall back to the interpreter when this is detected.
michael@0 2773 bool m_shouldFallBack;
michael@0 2774
michael@0 2775 // The regular expression expressed as a linear sequence of operations.
michael@0 2776 Vector<YarrOp, 128> m_ops;
michael@0 2777
michael@0 2778 // This records the current input offset being applied due to the current
michael@0 2779 // set of alternatives we are nested within. E.g. when matching the
michael@0 2780 // character 'b' within the regular expression /abc/, we will know that
michael@0 2781 // the minimum size for the alternative is 3, checked upon entry to the
michael@0 2782 // alternative, and that 'b' is at offset 1 from the start, and as such
michael@0 2783 // when matching 'b' we need to apply an offset of -2 to the load.
michael@0 2784 //
michael@0 2785 // FIXME: This should go away. Rather than tracking this value throughout
michael@0 2786 // code generation, we should gather this information up front & store it
michael@0 2787 // on the YarrOp structure.
michael@0 2788 int m_checked;
michael@0 2789
michael@0 2790 // This class records state whilst generating the backtracking path of code.
michael@0 2791 BacktrackingState m_backtrackingState;
michael@0 2792 };
michael@0 2793
michael@0 2794 void jitCompile(YarrPattern& pattern, YarrCharSize charSize, JSGlobalData* globalData, YarrCodeBlock& jitObject, YarrJITCompileMode mode)
michael@0 2795 {
michael@0 2796 if (mode == MatchOnly)
michael@0 2797 YarrGenerator<MatchOnly>(pattern, charSize).compile(globalData, jitObject);
michael@0 2798 else
michael@0 2799 YarrGenerator<IncludeSubpatterns>(pattern, charSize).compile(globalData, jitObject);
michael@0 2800 }
michael@0 2801
michael@0 2802 }}
michael@0 2803
michael@0 2804 #endif

mercurial