RegexByteCode.h 34 KB

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