1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef V8_REGEXP_REGEXP_BYTECODES_H_
6 #define V8_REGEXP_REGEXP_BYTECODES_H_
7
8 #include "new-regexp/regexp-shim.h"
9
10 namespace v8 {
11 namespace internal {
12
13 // Maximum number of bytecodes that will be used (next power of 2 of actually
14 // defined bytecodes).
15 // All slots between the last actually defined bytecode and maximum id will be
16 // filled with BREAKs, indicating an invalid operation. This way using
17 // BYTECODE_MASK guarantees no OOB access to the dispatch table.
18 constexpr int kRegExpPaddedBytecodeCount = 1 << 6;
19 constexpr int BYTECODE_MASK = kRegExpPaddedBytecodeCount - 1;
20 // The first argument is packed in with the byte code in one word, but so it
21 // has 24 bits, but it can be positive and negative so only use 23 bits for
22 // positive values.
23 const unsigned int MAX_FIRST_ARG = 0x7fffffu;
24 const int BYTECODE_SHIFT = 8;
25 STATIC_ASSERT(1 << BYTECODE_SHIFT > BYTECODE_MASK);
26
27 // TODO(pthier): Argument offsets of bytecodes should be easily accessible by
28 // name or at least by position.
29 // TODO(jgruber): More precise types (e.g. int32/uint32 instead of value32).
30 #define BYTECODE_ITERATOR(V) \
31 V(BREAK, 0, 4) /* bc8 */ \
32 V(PUSH_CP, 1, 4) /* bc8 pad24 */ \
33 V(PUSH_BT, 2, 8) /* bc8 pad24 offset32 */ \
34 V(PUSH_REGISTER, 3, 4) /* bc8 reg_idx24 */ \
35 V(SET_REGISTER_TO_CP, 4, 8) /* bc8 reg_idx24 offset32 */ \
36 V(SET_CP_TO_REGISTER, 5, 4) /* bc8 reg_idx24 */ \
37 V(SET_REGISTER_TO_SP, 6, 4) /* bc8 reg_idx24 */ \
38 V(SET_SP_TO_REGISTER, 7, 4) /* bc8 reg_idx24 */ \
39 V(SET_REGISTER, 8, 8) /* bc8 reg_idx24 value32 */ \
40 V(ADVANCE_REGISTER, 9, 8) /* bc8 reg_idx24 value32 */ \
41 V(POP_CP, 10, 4) /* bc8 pad24 */ \
42 V(POP_BT, 11, 4) /* bc8 pad24 */ \
43 V(POP_REGISTER, 12, 4) /* bc8 reg_idx24 */ \
44 V(FAIL, 13, 4) /* bc8 pad24 */ \
45 V(SUCCEED, 14, 4) /* bc8 pad24 */ \
46 V(ADVANCE_CP, 15, 4) /* bc8 offset24 */ \
47 /* Jump to another bytecode given its offset. */ \
48 /* Bit Layout: */ \
49 /* 0x00 - 0x07: 0x10 (fixed) Bytecode */ \
50 /* 0x08 - 0x1F: 0x00 (unused) Padding */ \
51 /* 0x20 - 0x3F: Address of bytecode to jump to */ \
52 V(GOTO, 16, 8) /* bc8 pad24 addr32 */ \
53 /* Check if offset is in range and load character at given offset. */ \
54 /* Bit Layout: */ \
55 /* 0x00 - 0x07: 0x11 (fixed) Bytecode */ \
56 /* 0x08 - 0x1F: Offset from current position */ \
57 /* 0x20 - 0x3F: Address of bytecode when load is out of range */ \
58 V(LOAD_CURRENT_CHAR, 17, 8) /* bc8 offset24 addr32 */ \
59 /* Load character at given offset without range checks. */ \
60 /* Bit Layout: */ \
61 /* 0x00 - 0x07: 0x12 (fixed) Bytecode */ \
62 /* 0x08 - 0x1F: Offset from current position */ \
63 V(LOAD_CURRENT_CHAR_UNCHECKED, 18, 4) /* bc8 offset24 */ \
64 V(LOAD_2_CURRENT_CHARS, 19, 8) /* bc8 offset24 addr32 */ \
65 V(LOAD_2_CURRENT_CHARS_UNCHECKED, 20, 4) /* bc8 offset24 */ \
66 V(LOAD_4_CURRENT_CHARS, 21, 8) /* bc8 offset24 addr32 */ \
67 V(LOAD_4_CURRENT_CHARS_UNCHECKED, 22, 4) /* bc8 offset24 */ \
68 V(CHECK_4_CHARS, 23, 12) /* bc8 pad24 uint32 addr32 */ \
69 /* Check if current character is equal to a given character */ \
70 /* Bit Layout: */ \
71 /* 0x00 - 0x07: 0x19 (fixed) Bytecode */ \
72 /* 0x08 - 0x0F: 0x00 (unused) Padding */ \
73 /* 0x10 - 0x1F: Character to check */ \
74 /* 0x20 - 0x3F: Address of bytecode when matched */ \
75 V(CHECK_CHAR, 24, 8) /* bc8 pad8 uint16 addr32 */ \
76 V(CHECK_NOT_4_CHARS, 25, 12) /* bc8 pad24 uint32 addr32 */ \
77 V(CHECK_NOT_CHAR, 26, 8) /* bc8 pad8 uint16 addr32 */ \
78 V(AND_CHECK_4_CHARS, 27, 16) /* bc8 pad24 uint32 uint32 addr32 */ \
79 /* Checks if the current character combined with mask (bitwise and) */ \
80 /* matches a character (e.g. used when two characters in a disjunction */ \
81 /* differ by only a single bit */ \
82 /* Bit Layout: */ \
83 /* 0x00 - 0x07: 0x1c (fixed) Bytecode */ \
84 /* 0x08 - 0x0F: 0x00 (unused) Padding */ \
85 /* 0x10 - 0x1F: Character to match against (after mask aplied) */ \
86 /* 0x20 - 0x3F: Bitmask bitwise and combined with current character */ \
87 /* 0x40 - 0x5F: Address of bytecode when matched */ \
88 V(AND_CHECK_CHAR, 28, 12) /* bc8 pad8 uint16 uint32 addr32 */ \
89 V(AND_CHECK_NOT_4_CHARS, 29, 16) /* bc8 pad24 uint32 uint32 addr32 */ \
90 V(AND_CHECK_NOT_CHAR, 30, 12) /* bc8 pad8 uint16 uint32 addr32 */ \
91 V(MINUS_AND_CHECK_NOT_CHAR, 31, 12) /* bc8 pad8 uc16 uc16 uc16 addr32 */ \
92 V(CHECK_CHAR_IN_RANGE, 32, 12) /* bc8 pad24 uc16 uc16 addr32 */ \
93 V(CHECK_CHAR_NOT_IN_RANGE, 33, 12) /* bc8 pad24 uc16 uc16 addr32 */ \
94 /* Checks if the current character matches any of the characters encoded */ \
95 /* in a bit table. Similar to/inspired by boyer moore string search */ \
96 /* Bit Layout: */ \
97 /* 0x00 - 0x07: 0x22 (fixed) Bytecode */ \
98 /* 0x08 - 0x1F: 0x00 (unused) Padding */ \
99 /* 0x20 - 0x3F: Address of bytecode when bit is set */ \
100 /* 0x40 - 0xBF: Bit table */ \
101 V(CHECK_BIT_IN_TABLE, 34, 24) /* bc8 pad24 addr32 bits128 */ \
102 V(CHECK_LT, 35, 8) /* bc8 pad8 uc16 addr32 */ \
103 V(CHECK_GT, 36, 8) /* bc8 pad8 uc16 addr32 */ \
104 V(CHECK_NOT_BACK_REF, 37, 8) /* bc8 reg_idx24 addr32 */ \
105 V(CHECK_NOT_BACK_REF_NO_CASE, 38, 8) /* bc8 reg_idx24 addr32 */ \
106 V(CHECK_NOT_BACK_REF_NO_CASE_UNICODE, 39, 8) /* UNUSED */ \
107 V(CHECK_NOT_BACK_REF_BACKWARD, 40, 8) /* bc8 reg_idx24 addr32 */ \
108 V(CHECK_NOT_BACK_REF_NO_CASE_BACKWARD, 41, 8) /* bc8 reg_idx24 addr32 */ \
109 V(CHECK_NOT_BACK_REF_NO_CASE_UNICODE_BACKWARD, 42, 8) /* UNUSED */ \
110 V(CHECK_NOT_REGS_EQUAL, 43, 12) /* bc8 regidx24 reg_idx32 addr32 */ \
111 V(CHECK_REGISTER_LT, 44, 12) /* bc8 reg_idx24 value32 addr32 */ \
112 V(CHECK_REGISTER_GE, 45, 12) /* bc8 reg_idx24 value32 addr32 */ \
113 V(CHECK_REGISTER_EQ_POS, 46, 8) /* bc8 reg_idx24 addr32 */ \
114 V(CHECK_AT_START, 47, 8) /* bc8 pad24 addr32 */ \
115 V(CHECK_NOT_AT_START, 48, 8) /* bc8 offset24 addr32 */ \
116 /* Checks if the current position matches top of backtrack stack */ \
117 /* Bit Layout: */ \
118 /* 0x00 - 0x07: 0x31 (fixed) Bytecode */ \
119 /* 0x08 - 0x1F: 0x00 (unused) Padding */ \
120 /* 0x20 - 0x3F: Address of bytecode when current matches tos */ \
121 V(CHECK_GREEDY, 49, 8) /* bc8 pad24 addr32 */ \
122 /* Advance character pointer by given offset and jump to another bytecode.*/ \
123 /* Bit Layout: */ \
124 /* 0x00 - 0x07: 0x32 (fixed) Bytecode */ \
125 /* 0x08 - 0x1F: Number of characters to advance */ \
126 /* 0x20 - 0x3F: Address of bytecode to jump to */ \
127 V(ADVANCE_CP_AND_GOTO, 50, 8) /* bc8 offset24 addr32 */ \
128 V(SET_CURRENT_POSITION_FROM_END, 51, 4) /* bc8 idx24 */ \
129 /* Checks if current position + given offset is in range. */ \
130 /* Bit Layout: */ \
131 /* 0x00 - 0x07: 0x34 (fixed) Bytecode */ \
132 /* 0x08 - 0x1F: Offset from current position */ \
133 /* 0x20 - 0x3F: Address of bytecode when position is out of range */ \
134 V(CHECK_CURRENT_POSITION, 52, 8) /* bc8 idx24 addr32 */ \
135 /* Combination of: */ \
136 /* LOAD_CURRENT_CHAR, CHECK_BIT_IN_TABLE and ADVANCE_CP_AND_GOTO */ \
137 /* Emitted by RegExpBytecodePeepholeOptimization. */ \
138 /* Bit Layout: */ \
139 /* 0x00 - 0x07 0x35 (fixed) Bytecode */ \
140 /* 0x08 - 0x1F Load character offset from current position */ \
141 /* 0x20 - 0x3F Number of characters to advance */ \
142 /* 0x40 - 0xBF Bit Table */ \
143 /* 0xC0 - 0xDF Address of bytecode when character is matched */ \
144 /* 0xE0 - 0xFF Address of bytecode when no match */ \
145 V(SKIP_UNTIL_BIT_IN_TABLE, 53, 32) \
146 /* Combination of: */ \
147 /* CHECK_CURRENT_POSITION, LOAD_CURRENT_CHAR_UNCHECKED, AND_CHECK_CHAR */ \
148 /* and ADVANCE_CP_AND_GOTO */ \
149 /* Emitted by RegExpBytecodePeepholeOptimization. */ \
150 /* Bit Layout: */ \
151 /* 0x00 - 0x07 0x36 (fixed) Bytecode */ \
152 /* 0x08 - 0x1F Load character offset from current position */ \
153 /* 0x20 - 0x2F Number of characters to advance */ \
154 /* 0x30 - 0x3F Character to match against (after mask applied) */ \
155 /* 0x40 - 0x5F: Bitmask bitwise and combined with current character */ \
156 /* 0x60 - 0x7F Minimum number of characters this pattern consumes */ \
157 /* 0x80 - 0x9F Address of bytecode when character is matched */ \
158 /* 0xA0 - 0xBF Address of bytecode when no match */ \
159 V(SKIP_UNTIL_CHAR_AND, 54, 24) \
160 /* Combination of: */ \
161 /* LOAD_CURRENT_CHAR, CHECK_CHAR and ADVANCE_CP_AND_GOTO */ \
162 /* Emitted by RegExpBytecodePeepholeOptimization. */ \
163 /* Bit Layout: */ \
164 /* 0x00 - 0x07 0x37 (fixed) Bytecode */ \
165 /* 0x08 - 0x1F Load character offset from current position */ \
166 /* 0x20 - 0x2F Number of characters to advance */ \
167 /* 0x30 - 0x3F Character to match */ \
168 /* 0x40 - 0x5F Address of bytecode when character is matched */ \
169 /* 0x60 - 0x7F Address of bytecode when no match */ \
170 V(SKIP_UNTIL_CHAR, 55, 16) \
171 /* Combination of: */ \
172 /* CHECK_CURRENT_POSITION, LOAD_CURRENT_CHAR_UNCHECKED, CHECK_CHAR */ \
173 /* and ADVANCE_CP_AND_GOTO */ \
174 /* Emitted by RegExpBytecodePeepholeOptimization. */ \
175 /* Bit Layout: */ \
176 /* 0x00 - 0x07 0x38 (fixed) Bytecode */ \
177 /* 0x08 - 0x1F Load character offset from current position */ \
178 /* 0x20 - 0x2F Number of characters to advance */ \
179 /* 0x30 - 0x3F Character to match */ \
180 /* 0x40 - 0x5F Minimum number of characters this pattern consumes */ \
181 /* 0x60 - 0x7F Address of bytecode when character is matched */ \
182 /* 0x80 - 0x9F Address of bytecode when no match */ \
183 V(SKIP_UNTIL_CHAR_POS_CHECKED, 56, 20) \
184 /* Combination of: */ \
185 /* LOAD_CURRENT_CHAR, CHECK_CHAR, CHECK_CHAR and ADVANCE_CP_AND_GOTO */ \
186 /* Emitted by RegExpBytecodePeepholeOptimization. */ \
187 /* Bit Layout: */ \
188 /* 0x00 - 0x07 0x39 (fixed) Bytecode */ \
189 /* 0x08 - 0x1F Load character offset from current position */ \
190 /* 0x20 - 0x3F Number of characters to advance */ \
191 /* 0x40 - 0x4F Character to match */ \
192 /* 0x50 - 0x5F Other Character to match */ \
193 /* 0x60 - 0x7F Address of bytecode when either character is matched */ \
194 /* 0x80 - 0x9F Address of bytecode when no match */ \
195 V(SKIP_UNTIL_CHAR_OR_CHAR, 57, 20) \
196 /* Combination of: */ \
197 /* LOAD_CURRENT_CHAR, CHECK_GT, CHECK_BIT_IN_TABLE, GOTO and */ \
198 /* and ADVANCE_CP_AND_GOTO */ \
199 /* Emitted by RegExpBytecodePeepholeOptimization. */ \
200 /* Bit Layout: */ \
201 /* 0x00 - 0x07 0x3A (fixed) Bytecode */ \
202 /* 0x08 - 0x1F Load character offset from current position */ \
203 /* 0x20 - 0x2F Number of characters to advance */ \
204 /* 0x30 - 0x3F Character to check if it is less than current char */ \
205 /* 0x40 - 0xBF Bit Table */ \
206 /* 0xC0 - 0xDF Address of bytecode when character is matched */ \
207 /* 0xE0 - 0xFF Address of bytecode when no match */ \
208 V(SKIP_UNTIL_GT_OR_NOT_BIT_IN_TABLE, 58, 32)
209
210 #define COUNT(...) +1
211 static constexpr int kRegExpBytecodeCount = BYTECODE_ITERATOR(COUNT);
212 #undef COUNT
213
214 // Just making sure we assigned values above properly. They should be
215 // contiguous, strictly increasing, and start at 0.
216 // TODO(jgruber): Do not explicitly assign values, instead generate them
217 // implicitly from the list order.
218 STATIC_ASSERT(kRegExpBytecodeCount == 59);
219
220 #define DECLARE_BYTECODES(name, code, length) \
221 static constexpr int BC_##name = code;
222 BYTECODE_ITERATOR(DECLARE_BYTECODES)
223 #undef DECLARE_BYTECODES
224
225 static constexpr int kRegExpBytecodeLengths[] = {
226 #define DECLARE_BYTECODE_LENGTH(name, code, length) length,
227 BYTECODE_ITERATOR(DECLARE_BYTECODE_LENGTH)
228 #undef DECLARE_BYTECODE_LENGTH
229 };
230
RegExpBytecodeLength(int bytecode)231 inline constexpr int RegExpBytecodeLength(int bytecode) {
232 return kRegExpBytecodeLengths[bytecode];
233 }
234
235 static const char* const kRegExpBytecodeNames[] = {
236 #define DECLARE_BYTECODE_NAME(name, ...) #name,
237 BYTECODE_ITERATOR(DECLARE_BYTECODE_NAME)
238 #undef DECLARE_BYTECODE_NAME
239 };
240
RegExpBytecodeName(int bytecode)241 inline const char* RegExpBytecodeName(int bytecode) {
242 return kRegExpBytecodeNames[bytecode];
243 }
244
245 void RegExpBytecodeDisassembleSingle(const byte* code_base, const byte* pc);
246 void RegExpBytecodeDisassemble(const byte* code_base, int length,
247 const char* pattern);
248
249 } // namespace internal
250 } // namespace v8
251
252 #endif // V8_REGEXP_REGEXP_BYTECODES_H_
253