michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- michael@0: * vim: set ts=8 sts=4 et sw=4 tw=99: michael@0: * michael@0: * Copyright (C) 2009 Apple Inc. All rights reserved. michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY michael@0: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE michael@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR michael@0: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR michael@0: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, michael@0: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, michael@0: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR michael@0: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY michael@0: * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: #ifndef yarr_YarrJIT_h michael@0: #define yarr_YarrJIT_h michael@0: michael@0: #include "assembler/wtf/Platform.h" michael@0: michael@0: #if ENABLE_YARR_JIT michael@0: michael@0: #include "assembler/assembler/MacroAssemblerCodeRef.h" michael@0: michael@0: #include "yarr/MatchResult.h" michael@0: #include "yarr/Yarr.h" michael@0: michael@0: #if WTF_CPU_X86 && !WTF_COMPILER_MSVC && !WTF_COMPILER_SUNCC michael@0: #define YARR_CALL __attribute__ ((regparm (3))) michael@0: #else michael@0: #define YARR_CALL michael@0: #endif michael@0: michael@0: #include "jit/JitCommon.h" michael@0: michael@0: namespace JSC { michael@0: michael@0: class JSGlobalData; michael@0: class ExecutablePool; michael@0: michael@0: namespace Yarr { michael@0: michael@0: class YarrCodeBlock { michael@0: #if defined(WTF_CPU_X86_64) && !defined(_WIN64) michael@0: typedef MatchResult JITMatchResult; michael@0: #else michael@0: typedef EncodedMatchResult JITMatchResult; michael@0: #endif michael@0: michael@0: typedef JITMatchResult (*YarrJITCode8)(const LChar* input, unsigned start, unsigned length, int* output) YARR_CALL; michael@0: typedef JITMatchResult (*YarrJITCode16)(const UChar* input, unsigned start, unsigned length, int* output) YARR_CALL; michael@0: typedef JITMatchResult (*YarrJITCodeMatchOnly8)(const LChar* input, unsigned start, unsigned length) YARR_CALL; michael@0: typedef JITMatchResult (*YarrJITCodeMatchOnly16)(const UChar* input, unsigned start, unsigned length) YARR_CALL; michael@0: michael@0: public: michael@0: YarrCodeBlock() michael@0: : m_needFallBack(false) michael@0: { michael@0: } michael@0: michael@0: ~YarrCodeBlock() michael@0: { michael@0: } michael@0: michael@0: void setFallBack(bool fallback) { m_needFallBack = fallback; } michael@0: bool isFallBack() { return m_needFallBack; } michael@0: michael@0: #ifdef YARR_8BIT_CHAR_SUPPORT michael@0: bool has8BitCode() const { return m_ref8.allocSize(); } michael@0: void set8BitCode(MacroAssemblerCodeRef ref) { m_ref8 = ref; } michael@0: bool has8BitCodeMatchOnly() const { return m_matchOnly8.allocSize(); } michael@0: void set8BitCodeMatchOnly(MacroAssemblerCodeRef matchOnly) { m_matchOnly8 = matchOnly; } michael@0: #endif michael@0: michael@0: bool has16BitCode() const { return m_ref16.allocSize(); } michael@0: void set16BitCode(MacroAssemblerCodeRef ref) { m_ref16 = ref; } michael@0: michael@0: bool has16BitCodeMatchOnly() const { return m_matchOnly16.allocSize(); } michael@0: void set16BitCodeMatchOnly(MacroAssemblerCodeRef matchOnly) { m_matchOnly16 = matchOnly; } michael@0: michael@0: #if YARR_8BIT_CHAR_SUPPORT michael@0: MatchResult execute(const LChar* input, unsigned start, unsigned length, int* output) michael@0: { michael@0: ASSERT(has8BitCode()); michael@0: michael@0: return MatchResult(reinterpret_cast(m_ref8.code().executableAddress())(input, start, length, output)); michael@0: } michael@0: michael@0: MatchResult execute(const LChar* input, unsigned start, unsigned length) michael@0: { michael@0: ASSERT(has8BitCodeMatchOnly()); michael@0: michael@0: return MatchResult(reinterpret_cast(m_matchOnly8.code().executableAddress())(input, start, length)); michael@0: } michael@0: #endif michael@0: michael@0: MatchResult execute(const UChar* input, unsigned start, unsigned length, int* output) michael@0: { michael@0: ASSERT(has16BitCode()); michael@0: michael@0: YarrJITCode16 fn = JS_FUNC_TO_DATA_PTR(YarrJITCode16, m_ref16.code().executableAddress()); michael@0: return MatchResult(CALL_GENERATED_YARR_CODE4(fn, input, start, length, output)); michael@0: } michael@0: michael@0: MatchResult execute(const UChar* input, unsigned start, unsigned length) michael@0: { michael@0: ASSERT(has16BitCodeMatchOnly()); michael@0: michael@0: YarrJITCodeMatchOnly16 fn = JS_FUNC_TO_DATA_PTR(YarrJITCodeMatchOnly16, m_matchOnly16.code().executableAddress()); michael@0: return MatchResult(CALL_GENERATED_YARR_CODE3(fn, input, start, length)); michael@0: } michael@0: michael@0: #if ENABLE_REGEXP_TRACING michael@0: void *getAddr() { return m_ref.code().executableAddress(); } michael@0: #endif michael@0: michael@0: void clear() michael@0: { michael@0: #ifdef YARR_8BIT_CHAR_SUPPORT michael@0: m_ref8 = MacroAssemblerCodeRef(); michael@0: m_matchOnly8 = MacroAssemblerCodeRef(); michael@0: #endif michael@0: michael@0: m_ref16 = MacroAssemblerCodeRef(); michael@0: m_matchOnly16 = MacroAssemblerCodeRef(); michael@0: m_needFallBack = false; michael@0: } michael@0: michael@0: void release() { michael@0: #ifdef YARR_8BIT_CHAR_SUPPORT michael@0: m_ref8.release(); michael@0: m_matchOnly8.release(); michael@0: #endif michael@0: michael@0: m_ref16.release(); michael@0: m_matchOnly16.release(); michael@0: } michael@0: michael@0: private: michael@0: #ifdef YARR_8BIT_CHAR_SUPPORT michael@0: MacroAssemblerCodeRef m_ref8; michael@0: MacroAssemblerCodeRef m_matchOnly8; michael@0: #endif michael@0: michael@0: MacroAssemblerCodeRef m_ref16; michael@0: MacroAssemblerCodeRef m_matchOnly16; michael@0: bool m_needFallBack; michael@0: }; michael@0: michael@0: enum YarrJITCompileMode { michael@0: MatchOnly, michael@0: IncludeSubpatterns michael@0: }; michael@0: void jitCompile(YarrPattern&, YarrCharSize, JSGlobalData*, YarrCodeBlock& jitObject, YarrJITCompileMode = IncludeSubpatterns); michael@0: michael@0: } } // namespace JSC::Yarr michael@0: michael@0: #endif michael@0: michael@0: #endif /* yarr_YarrJIT_h */