2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef WRECGenerator_h
27 #define WRECGenerator_h
29 #include <wtf/Platform.h>
33 #include "Quantifier.h"
34 #include "MacroAssembler.h"
35 #include <wtf/ASCIICType.h>
36 #include <wtf/unicode/Unicode.h>
46 class GenerateAtomFunctor
;
48 struct CharacterClass
;
50 class Generator
: private MacroAssembler
{
52 using MacroAssembler::Jump
;
53 using MacroAssembler::JumpList
;
54 using MacroAssembler::Label
;
56 enum ParenthesesType
{ Capturing
, NonCapturing
, Assertion
, InvertedAssertion
, Error
};
58 static CompiledRegExp
compileRegExp(JSGlobalData
*, const UString
& pattern
, unsigned* numSubpatterns_ptr
, const char** error_ptr
, RefPtr
<ExecutablePool
>& pool
, bool ignoreCase
= false, bool multiline
= false);
60 Generator(Parser
& parser
)
66 static const RegisterID input
= X86Registers::eax
;
67 static const RegisterID index
= X86Registers::edx
;
68 static const RegisterID length
= X86Registers::ecx
;
69 static const RegisterID output
= X86Registers::edi
;
71 static const RegisterID character
= X86Registers::esi
;
72 static const RegisterID repeatCount
= X86Registers::ebx
; // How many times the current atom repeats in the current match.
74 static const RegisterID returnRegister
= X86Registers::eax
;
77 static const RegisterID input
= X86Registers::edi
;
78 static const RegisterID index
= X86Registers::esi
;
79 static const RegisterID length
= X86Registers::edx
;
80 static const RegisterID output
= X86Registers::ecx
;
82 static const RegisterID character
= X86Registers::eax
;
83 static const RegisterID repeatCount
= X86Registers::ebx
; // How many times the current atom repeats in the current match.
85 static const RegisterID returnRegister
= X86Registers::eax
;
89 void generateSaveIndex();
90 void generateIncrementIndex(Jump
* failure
= 0);
91 void generateLoadCharacter(JumpList
& failures
);
92 void generateJumpIfNotEndOfInput(Label
);
93 void generateReturnSuccess();
94 void generateReturnFailure();
96 void generateGreedyQuantifier(JumpList
& failures
, GenerateAtomFunctor
& functor
, unsigned min
, unsigned max
);
97 void generateNonGreedyQuantifier(JumpList
& failures
, GenerateAtomFunctor
& functor
, unsigned min
, unsigned max
);
98 void generateBacktrack1();
99 void generateBacktrackBackreference(unsigned subpatternId
);
100 void generateCharacterClass(JumpList
& failures
, const CharacterClass
& charClass
, bool invert
);
101 void generateCharacterClassInverted(JumpList
& failures
, const CharacterClass
& charClass
);
102 void generateCharacterClassInvertedRange(JumpList
& failures
, JumpList
& matchDest
, const CharacterRange
* ranges
, unsigned count
, unsigned* matchIndex
, const UChar
* matches
, unsigned matchCount
);
103 void generatePatternCharacter(JumpList
& failures
, int ch
);
104 void generatePatternCharacterSequence(JumpList
& failures
, int* sequence
, size_t count
);
105 void generateAssertionWordBoundary(JumpList
& failures
, bool invert
);
106 void generateAssertionBOL(JumpList
& failures
);
107 void generateAssertionEOL(JumpList
& failures
);
108 void generateBackreference(JumpList
& failures
, unsigned subpatternID
);
109 void generateBackreferenceQuantifier(JumpList
& failures
, Quantifier::Type quantifierType
, unsigned subpatternId
, unsigned min
, unsigned max
);
110 void generateParenthesesAssertion(JumpList
& failures
);
111 void generateParenthesesInvertedAssertion(JumpList
& failures
);
112 Jump
generateParenthesesResetTrampoline(JumpList
& newFailures
, unsigned subpatternIdBefore
, unsigned subpatternIdAfter
);
113 void generateParenthesesNonGreedy(JumpList
& failures
, Label start
, Jump success
, Jump fail
);
115 void terminateAlternative(JumpList
& successes
, JumpList
& failures
);
116 void terminateDisjunction(JumpList
& successes
);
119 bool generatePatternCharacterPair(JumpList
& failures
, int ch1
, int ch2
);
124 } } // namespace JSC::WREC
126 #endif // ENABLE(WREC)
128 #endif // WRECGenerator_h