Home | History | Annotate | Download | only in wrec
      1 /*
      2  * Copyright (C) 2008 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      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.
     12  *
     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.
     24  */
     25 
     26 #ifndef WRECGenerator_h
     27 #define WRECGenerator_h
     28 
     29 #include <wtf/Platform.h>
     30 
     31 #if ENABLE(WREC)
     32 
     33 #include "Quantifier.h"
     34 #include "MacroAssembler.h"
     35 #include <wtf/ASCIICType.h>
     36 #include <wtf/unicode/Unicode.h>
     37 #include "WREC.h"
     38 
     39 namespace JSC {
     40 
     41     class JSGlobalData;
     42 
     43     namespace WREC {
     44 
     45     class CharacterRange;
     46     class GenerateAtomFunctor;
     47     class Parser;
     48     struct CharacterClass;
     49 
     50     class Generator : private MacroAssembler {
     51     public:
     52         using MacroAssembler::Jump;
     53         using MacroAssembler::JumpList;
     54         using MacroAssembler::Label;
     55 
     56         enum ParenthesesType { Capturing, NonCapturing, Assertion, InvertedAssertion, Error };
     57 
     58         static CompiledRegExp compileRegExp(JSGlobalData*, const UString& pattern, unsigned* numSubpatterns_ptr, const char** error_ptr, RefPtr<ExecutablePool>& pool, bool ignoreCase = false, bool multiline = false);
     59 
     60         Generator(Parser& parser)
     61             : m_parser(parser)
     62         {
     63         }
     64 
     65 #if CPU(X86)
     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;
     70 
     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.
     73 
     74         static const RegisterID returnRegister = X86Registers::eax;
     75 #endif
     76 #if CPU(X86_64)
     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;
     81 
     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.
     84 
     85         static const RegisterID returnRegister = X86Registers::eax;
     86 #endif
     87 
     88         void generateEnter();
     89         void generateSaveIndex();
     90         void generateIncrementIndex(Jump* failure = 0);
     91         void generateLoadCharacter(JumpList& failures);
     92         void generateJumpIfNotEndOfInput(Label);
     93         void generateReturnSuccess();
     94         void generateReturnFailure();
     95 
     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);
    114 
    115         void terminateAlternative(JumpList& successes, JumpList& failures);
    116         void terminateDisjunction(JumpList& successes);
    117 
    118     private:
    119         bool generatePatternCharacterPair(JumpList& failures, int ch1, int ch2);
    120 
    121         Parser& m_parser;
    122     };
    123 
    124 } } // namespace JSC::WREC
    125 
    126 #endif // ENABLE(WREC)
    127 
    128 #endif // WRECGenerator_h
    129