RegexByteCode.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. * Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include "RegexBytecodeStreamOptimizer.h"
  8. #include "RegexMatch.h"
  9. #include "RegexOptions.h"
  10. #include <AK/DisjointChunks.h>
  11. #include <AK/Format.h>
  12. #include <AK/Forward.h>
  13. #include <AK/HashMap.h>
  14. #include <AK/NonnullOwnPtr.h>
  15. #include <AK/OwnPtr.h>
  16. #include <AK/Traits.h>
  17. #include <AK/TypeCasts.h>
  18. #include <AK/Types.h>
  19. #include <AK/Vector.h>
  20. #include <LibUnicode/Forward.h>
  21. namespace regex {
  22. using ByteCodeValueType = u64;
  23. #define ENUMERATE_OPCODES \
  24. __ENUMERATE_OPCODE(Compare) \
  25. __ENUMERATE_OPCODE(Jump) \
  26. __ENUMERATE_OPCODE(JumpNonEmpty) \
  27. __ENUMERATE_OPCODE(ForkJump) \
  28. __ENUMERATE_OPCODE(ForkStay) \
  29. __ENUMERATE_OPCODE(ForkReplaceJump) \
  30. __ENUMERATE_OPCODE(ForkReplaceStay) \
  31. __ENUMERATE_OPCODE(FailForks) \
  32. __ENUMERATE_OPCODE(SaveLeftCaptureGroup) \
  33. __ENUMERATE_OPCODE(SaveRightCaptureGroup) \
  34. __ENUMERATE_OPCODE(SaveRightNamedCaptureGroup) \
  35. __ENUMERATE_OPCODE(CheckBegin) \
  36. __ENUMERATE_OPCODE(CheckEnd) \
  37. __ENUMERATE_OPCODE(CheckBoundary) \
  38. __ENUMERATE_OPCODE(Save) \
  39. __ENUMERATE_OPCODE(Restore) \
  40. __ENUMERATE_OPCODE(GoBack) \
  41. __ENUMERATE_OPCODE(ClearCaptureGroup) \
  42. __ENUMERATE_OPCODE(Repeat) \
  43. __ENUMERATE_OPCODE(ResetRepeat) \
  44. __ENUMERATE_OPCODE(Checkpoint) \
  45. __ENUMERATE_OPCODE(Exit)
  46. // clang-format off
  47. enum class OpCodeId : ByteCodeValueType {
  48. #define __ENUMERATE_OPCODE(x) x,
  49. ENUMERATE_OPCODES
  50. #undef __ENUMERATE_OPCODE
  51. First = Compare,
  52. Last = Exit,
  53. };
  54. // clang-format on
  55. #define ENUMERATE_CHARACTER_COMPARE_TYPES \
  56. __ENUMERATE_CHARACTER_COMPARE_TYPE(Undefined) \
  57. __ENUMERATE_CHARACTER_COMPARE_TYPE(Inverse) \
  58. __ENUMERATE_CHARACTER_COMPARE_TYPE(TemporaryInverse) \
  59. __ENUMERATE_CHARACTER_COMPARE_TYPE(AnyChar) \
  60. __ENUMERATE_CHARACTER_COMPARE_TYPE(Char) \
  61. __ENUMERATE_CHARACTER_COMPARE_TYPE(String) \
  62. __ENUMERATE_CHARACTER_COMPARE_TYPE(CharClass) \
  63. __ENUMERATE_CHARACTER_COMPARE_TYPE(CharRange) \
  64. __ENUMERATE_CHARACTER_COMPARE_TYPE(Reference) \
  65. __ENUMERATE_CHARACTER_COMPARE_TYPE(Property) \
  66. __ENUMERATE_CHARACTER_COMPARE_TYPE(GeneralCategory) \
  67. __ENUMERATE_CHARACTER_COMPARE_TYPE(Script) \
  68. __ENUMERATE_CHARACTER_COMPARE_TYPE(ScriptExtension) \
  69. __ENUMERATE_CHARACTER_COMPARE_TYPE(RangeExpressionDummy) \
  70. __ENUMERATE_CHARACTER_COMPARE_TYPE(LookupTable)
  71. enum class CharacterCompareType : ByteCodeValueType {
  72. #define __ENUMERATE_CHARACTER_COMPARE_TYPE(x) x,
  73. ENUMERATE_CHARACTER_COMPARE_TYPES
  74. #undef __ENUMERATE_CHARACTER_COMPARE_TYPE
  75. };
  76. #define ENUMERATE_CHARACTER_CLASSES \
  77. __ENUMERATE_CHARACTER_CLASS(Alnum) \
  78. __ENUMERATE_CHARACTER_CLASS(Cntrl) \
  79. __ENUMERATE_CHARACTER_CLASS(Lower) \
  80. __ENUMERATE_CHARACTER_CLASS(Space) \
  81. __ENUMERATE_CHARACTER_CLASS(Alpha) \
  82. __ENUMERATE_CHARACTER_CLASS(Digit) \
  83. __ENUMERATE_CHARACTER_CLASS(Print) \
  84. __ENUMERATE_CHARACTER_CLASS(Upper) \
  85. __ENUMERATE_CHARACTER_CLASS(Blank) \
  86. __ENUMERATE_CHARACTER_CLASS(Graph) \
  87. __ENUMERATE_CHARACTER_CLASS(Punct) \
  88. __ENUMERATE_CHARACTER_CLASS(Word) \
  89. __ENUMERATE_CHARACTER_CLASS(Xdigit)
  90. enum class CharClass : ByteCodeValueType {
  91. #define __ENUMERATE_CHARACTER_CLASS(x) x,
  92. ENUMERATE_CHARACTER_CLASSES
  93. #undef __ENUMERATE_CHARACTER_CLASS
  94. };
  95. #define ENUMERATE_BOUNDARY_CHECK_TYPES \
  96. __ENUMERATE_BOUNDARY_CHECK_TYPE(Word) \
  97. __ENUMERATE_BOUNDARY_CHECK_TYPE(NonWord)
  98. enum class BoundaryCheckType : ByteCodeValueType {
  99. #define __ENUMERATE_BOUNDARY_CHECK_TYPE(x) x,
  100. ENUMERATE_BOUNDARY_CHECK_TYPES
  101. #undef __ENUMERATE_BOUNDARY_CHECK_TYPE
  102. };
  103. struct CharRange {
  104. u32 const from;
  105. u32 const to;
  106. CharRange(u64 value)
  107. : from(value >> 32)
  108. , to(value & 0xffffffff)
  109. {
  110. }
  111. CharRange(u32 from, u32 to)
  112. : from(from)
  113. , to(to)
  114. {
  115. }
  116. operator ByteCodeValueType() const { return ((u64)from << 32) | to; }
  117. };
  118. struct CompareTypeAndValuePair {
  119. CharacterCompareType type;
  120. ByteCodeValueType value;
  121. };
  122. class OpCode;
  123. class ByteCode : public DisjointChunks<ByteCodeValueType> {
  124. using Base = DisjointChunks<ByteCodeValueType>;
  125. public:
  126. ByteCode()
  127. {
  128. ensure_opcodes_initialized();
  129. }
  130. ByteCode(ByteCode const&) = default;
  131. virtual ~ByteCode() = default;
  132. ByteCode& operator=(ByteCode&&) = default;
  133. ByteCode& operator=(Base&& value)
  134. {
  135. static_cast<Base&>(*this) = move(value);
  136. return *this;
  137. }
  138. template<typename... Args>
  139. void empend(Args&&... args)
  140. {
  141. if (is_empty())
  142. Base::append({});
  143. Base::last_chunk().empend(forward<Args>(args)...);
  144. }
  145. template<typename T>
  146. void append(T&& value)
  147. {
  148. if (is_empty())
  149. Base::append({});
  150. Base::last_chunk().append(forward<T>(value));
  151. }
  152. template<typename T>
  153. void prepend(T&& value)
  154. {
  155. if (is_empty())
  156. return append(forward<T>(value));
  157. Base::first_chunk().prepend(forward<T>(value));
  158. }
  159. void last_chunk() const = delete;
  160. void first_chunk() const = delete;
  161. void insert_bytecode_compare_values(Vector<CompareTypeAndValuePair>&& pairs)
  162. {
  163. Optimizer::append_character_class(*this, move(pairs));
  164. }
  165. void insert_bytecode_check_boundary(BoundaryCheckType type)
  166. {
  167. ByteCode bytecode;
  168. bytecode.empend((ByteCodeValueType)OpCodeId::CheckBoundary);
  169. bytecode.empend((ByteCodeValueType)type);
  170. extend(move(bytecode));
  171. }
  172. void insert_bytecode_clear_capture_group(size_t index)
  173. {
  174. empend(static_cast<ByteCodeValueType>(OpCodeId::ClearCaptureGroup));
  175. empend(index);
  176. }
  177. void insert_bytecode_compare_string(StringView view)
  178. {
  179. ByteCode bytecode;
  180. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::Compare));
  181. bytecode.empend(static_cast<u64>(1)); // number of arguments
  182. ByteCode arguments;
  183. arguments.empend(static_cast<ByteCodeValueType>(CharacterCompareType::String));
  184. arguments.insert_string(view);
  185. bytecode.empend(arguments.size()); // size of arguments
  186. bytecode.extend(move(arguments));
  187. extend(move(bytecode));
  188. }
  189. void insert_bytecode_group_capture_left(size_t capture_groups_count)
  190. {
  191. empend(static_cast<ByteCodeValueType>(OpCodeId::SaveLeftCaptureGroup));
  192. empend(capture_groups_count);
  193. }
  194. void insert_bytecode_group_capture_right(size_t capture_groups_count)
  195. {
  196. empend(static_cast<ByteCodeValueType>(OpCodeId::SaveRightCaptureGroup));
  197. empend(capture_groups_count);
  198. }
  199. void insert_bytecode_group_capture_right(size_t capture_groups_count, StringView name)
  200. {
  201. empend(static_cast<ByteCodeValueType>(OpCodeId::SaveRightNamedCaptureGroup));
  202. empend(reinterpret_cast<ByteCodeValueType>(name.characters_without_null_termination()));
  203. empend(name.length());
  204. empend(capture_groups_count);
  205. }
  206. enum class LookAroundType {
  207. LookAhead,
  208. LookBehind,
  209. NegatedLookAhead,
  210. NegatedLookBehind,
  211. };
  212. void insert_bytecode_lookaround(ByteCode&& lookaround_body, LookAroundType type, size_t match_length = 0)
  213. {
  214. // FIXME: The save stack will grow infinitely with repeated failures
  215. // as we do not discard that on failure (we don't necessarily know how many to pop with the current architecture).
  216. switch (type) {
  217. case LookAroundType::LookAhead: {
  218. // SAVE
  219. // REGEXP BODY
  220. // RESTORE
  221. empend((ByteCodeValueType)OpCodeId::Save);
  222. extend(move(lookaround_body));
  223. empend((ByteCodeValueType)OpCodeId::Restore);
  224. return;
  225. }
  226. case LookAroundType::NegatedLookAhead: {
  227. // JUMP _A
  228. // LABEL _L
  229. // REGEXP BODY
  230. // FAIL
  231. // LABEL _A
  232. // SAVE
  233. // FORKJUMP _L
  234. // RESTORE
  235. auto body_length = lookaround_body.size();
  236. empend((ByteCodeValueType)OpCodeId::Jump);
  237. empend((ByteCodeValueType)body_length + 1); // JUMP to label _A
  238. extend(move(lookaround_body));
  239. empend((ByteCodeValueType)OpCodeId::FailForks);
  240. empend((ByteCodeValueType)OpCodeId::Save);
  241. empend((ByteCodeValueType)OpCodeId::ForkJump);
  242. empend((ByteCodeValueType) - (body_length + 4)); // JUMP to label _L
  243. empend((ByteCodeValueType)OpCodeId::Restore);
  244. return;
  245. }
  246. case LookAroundType::LookBehind:
  247. // SAVE
  248. // GOBACK match_length(BODY)
  249. // REGEXP BODY
  250. // RESTORE
  251. empend((ByteCodeValueType)OpCodeId::Save);
  252. empend((ByteCodeValueType)OpCodeId::GoBack);
  253. empend((ByteCodeValueType)match_length);
  254. extend(move(lookaround_body));
  255. empend((ByteCodeValueType)OpCodeId::Restore);
  256. return;
  257. case LookAroundType::NegatedLookBehind: {
  258. // JUMP _A
  259. // LABEL _L
  260. // GOBACK match_length(BODY)
  261. // REGEXP BODY
  262. // FAIL
  263. // LABEL _A
  264. // SAVE
  265. // FORKJUMP _L
  266. // RESTORE
  267. auto body_length = lookaround_body.size();
  268. empend((ByteCodeValueType)OpCodeId::Jump);
  269. empend((ByteCodeValueType)body_length + 3); // JUMP to label _A
  270. empend((ByteCodeValueType)OpCodeId::GoBack);
  271. empend((ByteCodeValueType)match_length);
  272. extend(move(lookaround_body));
  273. empend((ByteCodeValueType)OpCodeId::FailForks);
  274. empend((ByteCodeValueType)OpCodeId::Save);
  275. empend((ByteCodeValueType)OpCodeId::ForkJump);
  276. empend((ByteCodeValueType) - (body_length + 6)); // JUMP to label _L
  277. empend((ByteCodeValueType)OpCodeId::Restore);
  278. return;
  279. }
  280. }
  281. VERIFY_NOT_REACHED();
  282. }
  283. void insert_bytecode_alternation(ByteCode&& left, ByteCode&& right)
  284. {
  285. // FORKJUMP _ALT
  286. // REGEXP ALT2
  287. // JUMP _END
  288. // LABEL _ALT
  289. // REGEXP ALT1
  290. // LABEL _END
  291. // Optimisation: Eliminate extra work by unifying common pre-and-postfix exprs.
  292. Optimizer::append_alternation(*this, move(left), move(right));
  293. }
  294. template<typename T>
  295. static void transform_bytecode_repetition_min_max(ByteCode& bytecode_to_repeat, T minimum, Optional<T> maximum, size_t min_repetition_mark_id, size_t max_repetition_mark_id, bool greedy = true) requires(IsIntegral<T>)
  296. {
  297. if (!maximum.has_value()) {
  298. if (minimum == 0)
  299. return transform_bytecode_repetition_any(bytecode_to_repeat, greedy);
  300. if (minimum == 1)
  301. return transform_bytecode_repetition_min_one(bytecode_to_repeat, greedy);
  302. }
  303. ByteCode new_bytecode;
  304. new_bytecode.insert_bytecode_repetition_n(bytecode_to_repeat, minimum, min_repetition_mark_id);
  305. if (maximum.has_value()) {
  306. // (REPEAT REGEXP MIN)
  307. // LABEL _MAX_LOOP |
  308. // FORK END |
  309. // REGEXP |
  310. // REPEAT _MAX_LOOP MAX-MIN | if max > min
  311. // FORK END |
  312. // REGEXP |
  313. // LABEL END |
  314. // RESET _MAX_LOOP |
  315. auto jump_kind = static_cast<ByteCodeValueType>(greedy ? OpCodeId::ForkStay : OpCodeId::ForkJump);
  316. if (maximum.value() > minimum) {
  317. new_bytecode.empend(jump_kind);
  318. new_bytecode.empend((ByteCodeValueType)0); // Placeholder for the jump target.
  319. auto pre_loop_fork_jump_index = new_bytecode.size();
  320. new_bytecode.extend(bytecode_to_repeat);
  321. auto repetitions = maximum.value() - minimum;
  322. auto fork_jump_address = new_bytecode.size();
  323. if (repetitions > 1) {
  324. new_bytecode.empend((ByteCodeValueType)OpCodeId::Repeat);
  325. new_bytecode.empend(bytecode_to_repeat.size() + 2);
  326. new_bytecode.empend(static_cast<ByteCodeValueType>(repetitions - 1));
  327. new_bytecode.empend(max_repetition_mark_id);
  328. new_bytecode.empend(jump_kind);
  329. new_bytecode.empend((ByteCodeValueType)0); // Placeholder for the jump target.
  330. auto post_loop_fork_jump_index = new_bytecode.size();
  331. new_bytecode.extend(bytecode_to_repeat);
  332. fork_jump_address = new_bytecode.size();
  333. new_bytecode[post_loop_fork_jump_index - 1] = (ByteCodeValueType)(fork_jump_address - post_loop_fork_jump_index);
  334. new_bytecode.empend((ByteCodeValueType)OpCodeId::ResetRepeat);
  335. new_bytecode.empend((ByteCodeValueType)max_repetition_mark_id);
  336. }
  337. new_bytecode[pre_loop_fork_jump_index - 1] = (ByteCodeValueType)(fork_jump_address - pre_loop_fork_jump_index);
  338. }
  339. } else {
  340. // no maximum value set, repeat finding if possible:
  341. // (REPEAT REGEXP MIN)
  342. // LABEL _START
  343. // CHECKPOINT _C
  344. // REGEXP
  345. // JUMP_NONEMPTY _C _START FORK
  346. // Note: This is only safe because REPEAT will leave one iteration outside (see repetition_n)
  347. new_bytecode.insert(new_bytecode.size() - bytecode_to_repeat.size(), (ByteCodeValueType)OpCodeId::Checkpoint);
  348. auto jump_kind = static_cast<ByteCodeValueType>(greedy ? OpCodeId::ForkJump : OpCodeId::ForkStay);
  349. new_bytecode.empend((ByteCodeValueType)OpCodeId::JumpNonEmpty);
  350. new_bytecode.empend(-bytecode_to_repeat.size() - 4 - 1); // Jump to the last iteration
  351. new_bytecode.empend(-bytecode_to_repeat.size() - 4 - 1); // if _C is not empty.
  352. new_bytecode.empend(jump_kind);
  353. }
  354. bytecode_to_repeat = move(new_bytecode);
  355. }
  356. template<typename T>
  357. void insert_bytecode_repetition_n(ByteCode& bytecode_to_repeat, T n, size_t repetition_mark_id) requires(IsIntegral<T>)
  358. {
  359. // LABEL _LOOP
  360. // REGEXP
  361. // REPEAT _LOOP N-1
  362. // REGEXP
  363. if (n == 0)
  364. return;
  365. // Note: this bytecode layout allows callers to repeat the last REGEXP instruction without the
  366. // REPEAT instruction forcing another loop.
  367. extend(bytecode_to_repeat);
  368. if (n > 1) {
  369. empend(static_cast<ByteCodeValueType>(OpCodeId::Repeat));
  370. empend(bytecode_to_repeat.size());
  371. empend(static_cast<ByteCodeValueType>(n - 1));
  372. empend(repetition_mark_id);
  373. extend(bytecode_to_repeat);
  374. }
  375. }
  376. static void transform_bytecode_repetition_min_one(ByteCode& bytecode_to_repeat, bool greedy)
  377. {
  378. // LABEL _START = -bytecode_to_repeat.size()
  379. // CHECKPOINT _C
  380. // REGEXP
  381. // JUMP_NONEMPTY _C _START FORKSTAY (FORKJUMP -> Greedy)
  382. bytecode_to_repeat.prepend((ByteCodeValueType)OpCodeId::Checkpoint);
  383. bytecode_to_repeat.empend((ByteCodeValueType)OpCodeId::JumpNonEmpty);
  384. bytecode_to_repeat.empend(-bytecode_to_repeat.size() - 3); // Jump to the _START label...
  385. bytecode_to_repeat.empend(-bytecode_to_repeat.size() - 2); // ...if _C is not empty
  386. if (greedy)
  387. bytecode_to_repeat.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkJump));
  388. else
  389. bytecode_to_repeat.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkStay));
  390. }
  391. static void transform_bytecode_repetition_any(ByteCode& bytecode_to_repeat, bool greedy)
  392. {
  393. // LABEL _START
  394. // FORKJUMP _END (FORKSTAY -> Greedy)
  395. // CHECKPOINT _C
  396. // REGEXP
  397. // JUMP_NONEMPTY _C _START JUMP
  398. // LABEL _END
  399. // LABEL _START = m_bytes.size();
  400. ByteCode bytecode;
  401. if (greedy)
  402. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkStay));
  403. else
  404. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkJump));
  405. bytecode.empend(bytecode_to_repeat.size() + 1 + 4); // Jump to the _END label
  406. auto c_label = bytecode.size();
  407. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::Checkpoint));
  408. bytecode.extend(bytecode_to_repeat);
  409. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::JumpNonEmpty));
  410. bytecode.empend(-bytecode.size() - 3); // Jump(...) to the _START label...
  411. bytecode.empend(c_label - bytecode.size() - 2); // ...only if _C passes.
  412. bytecode.empend((ByteCodeValueType)OpCodeId::Jump);
  413. // LABEL _END = bytecode.size()
  414. bytecode_to_repeat = move(bytecode);
  415. }
  416. static void transform_bytecode_repetition_zero_or_one(ByteCode& bytecode_to_repeat, bool greedy)
  417. {
  418. // FORKJUMP _END (FORKSTAY -> Greedy)
  419. // REGEXP
  420. // LABEL _END
  421. ByteCode bytecode;
  422. if (greedy)
  423. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkStay));
  424. else
  425. bytecode.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkJump));
  426. bytecode.empend(bytecode_to_repeat.size()); // Jump to the _END label
  427. bytecode.extend(move(bytecode_to_repeat));
  428. // LABEL _END = bytecode.size()
  429. bytecode_to_repeat = move(bytecode);
  430. }
  431. OpCode& get_opcode(MatchState& state) const;
  432. private:
  433. void insert_string(StringView view)
  434. {
  435. empend((ByteCodeValueType)view.length());
  436. for (size_t i = 0; i < view.length(); ++i)
  437. empend((ByteCodeValueType)view[i]);
  438. }
  439. void ensure_opcodes_initialized();
  440. ALWAYS_INLINE OpCode& get_opcode_by_id(OpCodeId id) const;
  441. static OwnPtr<OpCode> s_opcodes[(size_t)OpCodeId::Last + 1];
  442. static bool s_opcodes_initialized;
  443. };
  444. #define ENUMERATE_EXECUTION_RESULTS \
  445. __ENUMERATE_EXECUTION_RESULT(Continue) \
  446. __ENUMERATE_EXECUTION_RESULT(Fork_PrioHigh) \
  447. __ENUMERATE_EXECUTION_RESULT(Fork_PrioLow) \
  448. __ENUMERATE_EXECUTION_RESULT(Failed) \
  449. __ENUMERATE_EXECUTION_RESULT(Failed_ExecuteLowPrioForks) \
  450. __ENUMERATE_EXECUTION_RESULT(Succeeded)
  451. enum class ExecutionResult : u8 {
  452. #define __ENUMERATE_EXECUTION_RESULT(x) x,
  453. ENUMERATE_EXECUTION_RESULTS
  454. #undef __ENUMERATE_EXECUTION_RESULT
  455. };
  456. char const* execution_result_name(ExecutionResult result);
  457. char const* opcode_id_name(OpCodeId opcode_id);
  458. char const* boundary_check_type_name(BoundaryCheckType);
  459. char const* character_compare_type_name(CharacterCompareType result);
  460. class OpCode {
  461. public:
  462. OpCode() = default;
  463. virtual ~OpCode() = default;
  464. virtual OpCodeId opcode_id() const = 0;
  465. virtual size_t size() const = 0;
  466. virtual ExecutionResult execute(MatchInput const& input, MatchState& state) const = 0;
  467. ALWAYS_INLINE ByteCodeValueType argument(size_t offset) const
  468. {
  469. VERIFY(state().instruction_position + offset <= m_bytecode->size());
  470. return m_bytecode->at(state().instruction_position + 1 + offset);
  471. }
  472. ALWAYS_INLINE char const* name() const;
  473. static char const* name(OpCodeId);
  474. ALWAYS_INLINE void set_state(MatchState& state) { m_state = &state; }
  475. ALWAYS_INLINE void set_bytecode(ByteCode& bytecode) { m_bytecode = &bytecode; }
  476. ALWAYS_INLINE MatchState const& state() const
  477. {
  478. VERIFY(m_state);
  479. return *m_state;
  480. }
  481. String to_string() const
  482. {
  483. return String::formatted("[{:#02X}] {}", (int)opcode_id(), name(opcode_id()));
  484. }
  485. virtual String arguments_string() const = 0;
  486. ALWAYS_INLINE ByteCode const& bytecode() const { return *m_bytecode; }
  487. protected:
  488. ByteCode* m_bytecode { nullptr };
  489. MatchState* m_state { nullptr };
  490. };
  491. class OpCode_Exit final : public OpCode {
  492. public:
  493. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  494. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Exit; }
  495. ALWAYS_INLINE size_t size() const override { return 1; }
  496. String arguments_string() const override { return String::empty(); }
  497. };
  498. class OpCode_FailForks final : public OpCode {
  499. public:
  500. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  501. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::FailForks; }
  502. ALWAYS_INLINE size_t size() const override { return 1; }
  503. String arguments_string() const override { return String::empty(); }
  504. };
  505. class OpCode_Save final : public OpCode {
  506. public:
  507. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  508. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Save; }
  509. ALWAYS_INLINE size_t size() const override { return 1; }
  510. String arguments_string() const override { return String::empty(); }
  511. };
  512. class OpCode_Restore final : public OpCode {
  513. public:
  514. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  515. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Restore; }
  516. ALWAYS_INLINE size_t size() const override { return 1; }
  517. String arguments_string() const override { return String::empty(); }
  518. };
  519. class OpCode_GoBack final : public OpCode {
  520. public:
  521. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  522. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::GoBack; }
  523. ALWAYS_INLINE size_t size() const override { return 2; }
  524. ALWAYS_INLINE size_t count() const { return argument(0); }
  525. String arguments_string() const override { return String::formatted("count={}", count()); }
  526. };
  527. class OpCode_Jump final : public OpCode {
  528. public:
  529. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  530. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Jump; }
  531. ALWAYS_INLINE size_t size() const override { return 2; }
  532. ALWAYS_INLINE ssize_t offset() const { return argument(0); }
  533. String arguments_string() const override
  534. {
  535. return String::formatted("offset={} [&{}]", offset(), state().instruction_position + size() + offset());
  536. }
  537. };
  538. class OpCode_ForkJump : public OpCode {
  539. public:
  540. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  541. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::ForkJump; }
  542. ALWAYS_INLINE size_t size() const override { return 2; }
  543. ALWAYS_INLINE ssize_t offset() const { return argument(0); }
  544. String arguments_string() const override
  545. {
  546. return String::formatted("offset={} [&{}], sp: {}", offset(), state().instruction_position + size() + offset(), state().string_position);
  547. }
  548. };
  549. class OpCode_ForkReplaceJump final : public OpCode_ForkJump {
  550. public:
  551. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  552. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::ForkReplaceJump; }
  553. };
  554. class OpCode_ForkStay : public OpCode {
  555. public:
  556. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  557. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::ForkStay; }
  558. ALWAYS_INLINE size_t size() const override { return 2; }
  559. ALWAYS_INLINE ssize_t offset() const { return argument(0); }
  560. String arguments_string() const override
  561. {
  562. return String::formatted("offset={} [&{}], sp: {}", offset(), state().instruction_position + size() + offset(), state().string_position);
  563. }
  564. };
  565. class OpCode_ForkReplaceStay final : public OpCode_ForkStay {
  566. public:
  567. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  568. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::ForkReplaceStay; }
  569. };
  570. class OpCode_CheckBegin final : public OpCode {
  571. public:
  572. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  573. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::CheckBegin; }
  574. ALWAYS_INLINE size_t size() const override { return 1; }
  575. String arguments_string() const override { return String::empty(); }
  576. };
  577. class OpCode_CheckEnd final : public OpCode {
  578. public:
  579. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  580. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::CheckEnd; }
  581. ALWAYS_INLINE size_t size() const override { return 1; }
  582. String arguments_string() const override { return String::empty(); }
  583. };
  584. class OpCode_CheckBoundary final : public OpCode {
  585. public:
  586. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  587. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::CheckBoundary; }
  588. ALWAYS_INLINE size_t size() const override { return 2; }
  589. ALWAYS_INLINE size_t arguments_count() const { return 1; }
  590. ALWAYS_INLINE BoundaryCheckType type() const { return static_cast<BoundaryCheckType>(argument(0)); }
  591. String arguments_string() const override { return String::formatted("kind={} ({})", (long unsigned int)argument(0), boundary_check_type_name(type())); }
  592. };
  593. class OpCode_ClearCaptureGroup final : public OpCode {
  594. public:
  595. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  596. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::ClearCaptureGroup; }
  597. ALWAYS_INLINE size_t size() const override { return 2; }
  598. ALWAYS_INLINE size_t id() const { return argument(0); }
  599. String arguments_string() const override { return String::formatted("id={}", id()); }
  600. };
  601. class OpCode_SaveLeftCaptureGroup final : public OpCode {
  602. public:
  603. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  604. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::SaveLeftCaptureGroup; }
  605. ALWAYS_INLINE size_t size() const override { return 2; }
  606. ALWAYS_INLINE size_t id() const { return argument(0); }
  607. String arguments_string() const override { return String::formatted("id={}", id()); }
  608. };
  609. class OpCode_SaveRightCaptureGroup final : public OpCode {
  610. public:
  611. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  612. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::SaveRightCaptureGroup; }
  613. ALWAYS_INLINE size_t size() const override { return 2; }
  614. ALWAYS_INLINE size_t id() const { return argument(0); }
  615. String arguments_string() const override { return String::formatted("id={}", id()); }
  616. };
  617. class OpCode_SaveRightNamedCaptureGroup final : public OpCode {
  618. public:
  619. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  620. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::SaveRightNamedCaptureGroup; }
  621. ALWAYS_INLINE size_t size() const override { return 4; }
  622. ALWAYS_INLINE StringView name() const { return { reinterpret_cast<char*>(argument(0)), length() }; }
  623. ALWAYS_INLINE size_t length() const { return argument(1); }
  624. ALWAYS_INLINE size_t id() const { return argument(2); }
  625. String arguments_string() const override
  626. {
  627. return String::formatted("name={}, length={}", name(), length());
  628. }
  629. };
  630. class OpCode_Compare final : public OpCode {
  631. public:
  632. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  633. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Compare; }
  634. ALWAYS_INLINE size_t size() const override { return arguments_size() + 3; }
  635. ALWAYS_INLINE size_t arguments_count() const { return argument(0); }
  636. ALWAYS_INLINE size_t arguments_size() const { return argument(1); }
  637. String arguments_string() const override;
  638. Vector<String> variable_arguments_to_string(Optional<MatchInput> input = {}) const;
  639. Vector<CompareTypeAndValuePair> flat_compares() const;
  640. private:
  641. ALWAYS_INLINE static void compare_char(MatchInput const& input, MatchState& state, u32 ch1, bool inverse, bool& inverse_matched);
  642. ALWAYS_INLINE static bool compare_string(MatchInput const& input, MatchState& state, RegexStringView str, bool& had_zero_length_match);
  643. ALWAYS_INLINE static void compare_character_class(MatchInput const& input, MatchState& state, CharClass character_class, u32 ch, bool inverse, bool& inverse_matched);
  644. ALWAYS_INLINE static void compare_character_range(MatchInput const& input, MatchState& state, u32 from, u32 to, u32 ch, bool inverse, bool& inverse_matched);
  645. ALWAYS_INLINE static void compare_property(MatchInput const& input, MatchState& state, Unicode::Property property, bool inverse, bool& inverse_matched);
  646. ALWAYS_INLINE static void compare_general_category(MatchInput const& input, MatchState& state, Unicode::GeneralCategory general_category, bool inverse, bool& inverse_matched);
  647. ALWAYS_INLINE static void compare_script(MatchInput const& input, MatchState& state, Unicode::Script script, bool inverse, bool& inverse_matched);
  648. ALWAYS_INLINE static void compare_script_extension(MatchInput const& input, MatchState& state, Unicode::Script script, bool inverse, bool& inverse_matched);
  649. };
  650. class OpCode_Repeat : public OpCode {
  651. public:
  652. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  653. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Repeat; }
  654. ALWAYS_INLINE size_t size() const override { return 4; }
  655. ALWAYS_INLINE size_t offset() const { return argument(0); }
  656. ALWAYS_INLINE u64 count() const { return argument(1); }
  657. ALWAYS_INLINE size_t id() const { return argument(2); }
  658. String arguments_string() const override
  659. {
  660. auto reps = id() < state().repetition_marks.size() ? state().repetition_marks.at(id()) : 0;
  661. return String::formatted("offset={} count={} id={} rep={}, sp: {}", offset(), count() + 1, id(), reps + 1, state().string_position);
  662. }
  663. };
  664. class OpCode_ResetRepeat : public OpCode {
  665. public:
  666. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  667. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::ResetRepeat; }
  668. ALWAYS_INLINE size_t size() const override { return 2; }
  669. ALWAYS_INLINE size_t id() const { return argument(0); }
  670. String arguments_string() const override
  671. {
  672. auto reps = id() < state().repetition_marks.size() ? state().repetition_marks.at(id()) : 0;
  673. return String::formatted("id={} rep={}", id(), reps + 1);
  674. }
  675. };
  676. class OpCode_Checkpoint final : public OpCode {
  677. public:
  678. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  679. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::Checkpoint; }
  680. ALWAYS_INLINE size_t size() const override { return 1; }
  681. String arguments_string() const override { return String::empty(); }
  682. };
  683. class OpCode_JumpNonEmpty final : public OpCode {
  684. public:
  685. ExecutionResult execute(MatchInput const& input, MatchState& state) const override;
  686. ALWAYS_INLINE OpCodeId opcode_id() const override { return OpCodeId::JumpNonEmpty; }
  687. ALWAYS_INLINE size_t size() const override { return 4; }
  688. ALWAYS_INLINE ssize_t offset() const { return argument(0); }
  689. ALWAYS_INLINE ssize_t checkpoint() const { return argument(1); }
  690. ALWAYS_INLINE OpCodeId form() const { return (OpCodeId)argument(2); }
  691. String arguments_string() const override
  692. {
  693. return String::formatted("{} offset={} [&{}], cp={} [&{}]",
  694. opcode_id_name(form()),
  695. offset(), state().instruction_position + size() + offset(),
  696. checkpoint(), state().instruction_position + size() + checkpoint());
  697. }
  698. };
  699. template<typename T>
  700. bool is(OpCode const&);
  701. template<typename T>
  702. ALWAYS_INLINE bool is(OpCode const&)
  703. {
  704. return false;
  705. }
  706. template<typename T>
  707. ALWAYS_INLINE bool is(OpCode const* opcode)
  708. {
  709. return is<T>(*opcode);
  710. }
  711. template<>
  712. ALWAYS_INLINE bool is<OpCode_ForkStay>(OpCode const& opcode)
  713. {
  714. return opcode.opcode_id() == OpCodeId::ForkStay;
  715. }
  716. template<>
  717. ALWAYS_INLINE bool is<OpCode_Exit>(OpCode const& opcode)
  718. {
  719. return opcode.opcode_id() == OpCodeId::Exit;
  720. }
  721. template<>
  722. ALWAYS_INLINE bool is<OpCode_Compare>(OpCode const& opcode)
  723. {
  724. return opcode.opcode_id() == OpCodeId::Compare;
  725. }
  726. template<typename T>
  727. ALWAYS_INLINE T const& to(OpCode const& opcode)
  728. {
  729. return verify_cast<T>(opcode);
  730. }
  731. template<typename T>
  732. ALWAYS_INLINE T* to(OpCode* opcode)
  733. {
  734. return verify_cast<T>(opcode);
  735. }
  736. template<typename T>
  737. ALWAYS_INLINE T const* to(OpCode const* opcode)
  738. {
  739. return verify_cast<T>(opcode);
  740. }
  741. template<typename T>
  742. ALWAYS_INLINE T& to(OpCode& opcode)
  743. {
  744. return verify_cast<T>(opcode);
  745. }
  746. }