1 | /* |
2 | * Copyright (C) 2015 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. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #pragma once |
27 | |
28 | #if ENABLE(CONTENT_EXTENSIONS) |
29 | |
30 | #include "DFABytecode.h" |
31 | #include <wtf/Vector.h> |
32 | |
33 | namespace WebCore { |
34 | |
35 | namespace ContentExtensions { |
36 | |
37 | struct DFA; |
38 | class DFANode; |
39 | |
40 | class WEBCORE_EXPORT DFABytecodeCompiler { |
41 | public: |
42 | DFABytecodeCompiler(const DFA& dfa, Vector<DFABytecode>& bytecode) |
43 | : m_bytecode(bytecode) |
44 | , m_dfa(dfa) |
45 | { |
46 | } |
47 | |
48 | void compile(); |
49 | |
50 | private: |
51 | struct Range { |
52 | Range(uint8_t min, uint8_t max, uint32_t destination, bool caseSensitive) |
53 | : min(min) |
54 | , max(max) |
55 | , destination(destination) |
56 | , caseSensitive(caseSensitive) |
57 | { |
58 | } |
59 | uint8_t min; |
60 | uint8_t max; |
61 | uint32_t destination; |
62 | bool caseSensitive; |
63 | }; |
64 | struct JumpTable { |
65 | ~JumpTable() |
66 | { |
67 | ASSERT(min + destinations.size() == static_cast<size_t>(max + 1)); |
68 | ASSERT(min == max || destinations.size() > 1); |
69 | } |
70 | |
71 | uint8_t min { 0 }; |
72 | uint8_t max { 0 }; |
73 | bool caseSensitive { true }; |
74 | Vector<uint32_t> destinations; |
75 | }; |
76 | struct Transitions { |
77 | Vector<JumpTable> jumpTables; |
78 | Vector<Range> ranges; |
79 | bool useFallbackTransition { false }; |
80 | uint32_t fallbackTransitionTarget { std::numeric_limits<uint32_t>::max() }; |
81 | }; |
82 | JumpTable (Vector<Range>&, unsigned first, unsigned last); |
83 | Transitions transitions(const DFANode&); |
84 | |
85 | unsigned compiledNodeMaxBytecodeSize(uint32_t index); |
86 | void compileNode(uint32_t index, bool root); |
87 | unsigned nodeTransitionsMaxBytecodeSize(const DFANode&); |
88 | void compileNodeTransitions(uint32_t nodeIndex); |
89 | unsigned checkForJumpTableMaxBytecodeSize(const JumpTable&); |
90 | unsigned checkForRangeMaxBytecodeSize(const Range&); |
91 | void compileJumpTable(uint32_t nodeIndex, const JumpTable&); |
92 | void compileCheckForRange(uint32_t nodeIndex, const Range&); |
93 | int32_t longestPossibleJump(uint32_t jumpLocation, uint32_t sourceNodeIndex, uint32_t destinationNodeIndex); |
94 | |
95 | void emitAppendAction(uint64_t); |
96 | void emitJump(uint32_t sourceNodeIndex, uint32_t destinationNodeIndex); |
97 | void emitCheckValue(uint8_t value, uint32_t sourceNodeIndex, uint32_t destinationNodeIndex, bool caseSensitive); |
98 | void emitCheckValueRange(uint8_t lowValue, uint8_t highValue, uint32_t sourceNodeIndex, uint32_t destinationNodeIndex, bool caseSensitive); |
99 | void emitTerminate(); |
100 | |
101 | Vector<DFABytecode>& m_bytecode; |
102 | const DFA& m_dfa; |
103 | |
104 | Vector<uint32_t> m_maxNodeStartOffsets; |
105 | Vector<uint32_t> m_nodeStartOffsets; |
106 | |
107 | struct LinkRecord { |
108 | DFABytecodeJumpSize jumpSize; |
109 | int32_t longestPossibleJump; |
110 | uint32_t instructionLocation; |
111 | uint32_t jumpLocation; |
112 | uint32_t destinationNodeIndex; |
113 | }; |
114 | Vector<LinkRecord> m_linkRecords; |
115 | }; |
116 | |
117 | } // namespace ContentExtensions |
118 | } // namespace WebCore |
119 | |
120 | #endif // ENABLE(CONTENT_EXTENSIONS) |
121 | |