YarrJIT.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2009 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. #ifndef YarrJIT_h
  26. #define YarrJIT_h
  27. #if ENABLE(YARR_JIT)
  28. #include "VM.h"
  29. #include "MacroAssemblerCodeRef.h"
  30. #include "MatchResult.h"
  31. #include "Yarr.h"
  32. #include "YarrPattern.h"
  33. #if CPU(X86) && !COMPILER(MSVC)
  34. #define YARR_CALL __attribute__ ((regparm (3)))
  35. #else
  36. #define YARR_CALL
  37. #endif
  38. namespace JSC {
  39. class VM;
  40. class ExecutablePool;
  41. namespace Yarr {
  42. class YarrCodeBlock {
  43. #if ENABLE(DETACHED_JIT)
  44. DETACHED_JIT_MAKE_SHARED_DATA_ALLOCATED;
  45. #endif
  46. #if CPU(X86_64)
  47. typedef MatchResult (*YarrJITCode8)(const LChar* input, unsigned start, unsigned length, int* output) YARR_CALL;
  48. typedef MatchResult (*YarrJITCode16)(const UChar* input, unsigned start, unsigned length, int* output) YARR_CALL;
  49. typedef MatchResult (*YarrJITCodeMatchOnly8)(const LChar* input, unsigned start, unsigned length) YARR_CALL;
  50. typedef MatchResult (*YarrJITCodeMatchOnly16)(const UChar* input, unsigned start, unsigned length) YARR_CALL;
  51. #else
  52. typedef EncodedMatchResult (*YarrJITCode8)(const LChar* input, unsigned start, unsigned length, int* output) YARR_CALL;
  53. typedef EncodedMatchResult (*YarrJITCode16)(const UChar* input, unsigned start, unsigned length, int* output) YARR_CALL;
  54. typedef EncodedMatchResult (*YarrJITCodeMatchOnly8)(const LChar* input, unsigned start, unsigned length) YARR_CALL;
  55. typedef EncodedMatchResult (*YarrJITCodeMatchOnly16)(const UChar* input, unsigned start, unsigned length) YARR_CALL;
  56. #endif
  57. public:
  58. YarrCodeBlock()
  59. : m_needFallBack(false)
  60. {
  61. }
  62. ~YarrCodeBlock()
  63. {
  64. }
  65. void setFallBack(bool fallback) { m_needFallBack = fallback; }
  66. bool isFallBack() { return m_needFallBack; }
  67. bool has8BitCode() { return m_ref8.size(); }
  68. bool has16BitCode() { return m_ref16.size(); }
  69. void set8BitCode(MacroAssemblerCodeRef ref) { m_ref8 = ref; }
  70. void set16BitCode(MacroAssemblerCodeRef ref) { m_ref16 = ref; }
  71. bool has8BitCodeMatchOnly() { return m_matchOnly8.size(); }
  72. bool has16BitCodeMatchOnly() { return m_matchOnly16.size(); }
  73. void set8BitCodeMatchOnly(MacroAssemblerCodeRef matchOnly) { m_matchOnly8 = matchOnly; }
  74. void set16BitCodeMatchOnly(MacroAssemblerCodeRef matchOnly) { m_matchOnly16 = matchOnly; }
  75. MatchResult execute(const LChar* input, unsigned start, unsigned length, int* output)
  76. {
  77. ASSERT(has8BitCode());
  78. return MatchResult(reinterpret_cast<YarrJITCode8>(m_ref8.code().executableAddress())(input, start, length, output));
  79. }
  80. MatchResult execute(const UChar* input, unsigned start, unsigned length, int* output)
  81. {
  82. ASSERT(has16BitCode());
  83. return MatchResult(reinterpret_cast<YarrJITCode16>(m_ref16.code().executableAddress())(input, start, length, output));
  84. }
  85. MatchResult execute(const LChar* input, unsigned start, unsigned length)
  86. {
  87. ASSERT(has8BitCodeMatchOnly());
  88. return MatchResult(reinterpret_cast<YarrJITCodeMatchOnly8>(m_matchOnly8.code().executableAddress())(input, start, length));
  89. }
  90. MatchResult execute(const UChar* input, unsigned start, unsigned length)
  91. {
  92. ASSERT(has16BitCodeMatchOnly());
  93. return MatchResult(reinterpret_cast<YarrJITCodeMatchOnly16>(m_matchOnly16.code().executableAddress())(input, start, length));
  94. }
  95. #if ENABLE(REGEXP_TRACING)
  96. void *getAddr() { return m_ref.code().executableAddress(); }
  97. #endif
  98. void clear()
  99. {
  100. m_ref8 = MacroAssemblerCodeRef();
  101. m_ref16 = MacroAssemblerCodeRef();
  102. m_matchOnly8 = MacroAssemblerCodeRef();
  103. m_matchOnly16 = MacroAssemblerCodeRef();
  104. m_needFallBack = false;
  105. }
  106. private:
  107. MacroAssemblerCodeRef m_ref8;
  108. MacroAssemblerCodeRef m_ref16;
  109. MacroAssemblerCodeRef m_matchOnly8;
  110. MacroAssemblerCodeRef m_matchOnly16;
  111. bool m_needFallBack;
  112. };
  113. enum YarrJITCompileMode {
  114. MatchOnly,
  115. IncludeSubpatterns
  116. };
  117. void jitCompile(YarrPattern&, YarrCharSize, VM*, YarrCodeBlock& jitObject, YarrJITCompileMode = IncludeSubpatterns);
  118. } } // namespace JSC::Yarr
  119. #endif
  120. #endif // YarrJIT_h