RegexOptimizer.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /*
  2. * Copyright (c) 2021, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/QuickSort.h>
  7. #include <AK/RedBlackTree.h>
  8. #include <AK/Stack.h>
  9. #include <LibRegex/Regex.h>
  10. #include <LibRegex/RegexBytecodeStreamOptimizer.h>
  11. #if REGEX_DEBUG
  12. # include <AK/ScopeGuard.h>
  13. # include <AK/ScopeLogger.h>
  14. #endif
  15. namespace regex {
  16. using Detail::Block;
  17. template<typename Parser>
  18. void Regex<Parser>::run_optimization_passes()
  19. {
  20. parser_result.bytecode.flatten();
  21. // Rewrite fork loops as atomic groups
  22. // e.g. a*b -> (ATOMIC a*)b
  23. attempt_rewrite_loops_as_atomic_groups(split_basic_blocks(parser_result.bytecode));
  24. parser_result.bytecode.flatten();
  25. }
  26. template<typename Parser>
  27. typename Regex<Parser>::BasicBlockList Regex<Parser>::split_basic_blocks(ByteCode const& bytecode)
  28. {
  29. BasicBlockList block_boundaries;
  30. size_t end_of_last_block = 0;
  31. auto bytecode_size = bytecode.size();
  32. MatchState state;
  33. state.instruction_position = 0;
  34. auto check_jump = [&]<typename T>(OpCode const& opcode) {
  35. auto& op = static_cast<T const&>(opcode);
  36. ssize_t jump_offset = op.size() + op.offset();
  37. if (jump_offset >= 0) {
  38. block_boundaries.append({ end_of_last_block, state.instruction_position });
  39. end_of_last_block = state.instruction_position + opcode.size();
  40. } else {
  41. // This op jumps back, see if that's within this "block".
  42. if (jump_offset + state.instruction_position > end_of_last_block) {
  43. // Split the block!
  44. block_boundaries.append({ end_of_last_block, jump_offset + state.instruction_position });
  45. block_boundaries.append({ jump_offset + state.instruction_position, state.instruction_position });
  46. end_of_last_block = state.instruction_position + opcode.size();
  47. } else {
  48. // Nope, it's just a jump to another block
  49. block_boundaries.append({ end_of_last_block, state.instruction_position });
  50. end_of_last_block = state.instruction_position + opcode.size();
  51. }
  52. }
  53. };
  54. for (;;) {
  55. auto& opcode = bytecode.get_opcode(state);
  56. switch (opcode.opcode_id()) {
  57. case OpCodeId::Jump:
  58. check_jump.template operator()<OpCode_Jump>(opcode);
  59. break;
  60. case OpCodeId::JumpNonEmpty:
  61. check_jump.template operator()<OpCode_JumpNonEmpty>(opcode);
  62. break;
  63. case OpCodeId::ForkJump:
  64. check_jump.template operator()<OpCode_ForkJump>(opcode);
  65. break;
  66. case OpCodeId::ForkStay:
  67. check_jump.template operator()<OpCode_ForkStay>(opcode);
  68. break;
  69. case OpCodeId::FailForks:
  70. block_boundaries.append({ end_of_last_block, state.instruction_position });
  71. end_of_last_block = state.instruction_position + opcode.size();
  72. break;
  73. case OpCodeId::Repeat: {
  74. // Repeat produces two blocks, one containing its repeated expr, and one after that.
  75. auto repeat_start = state.instruction_position - static_cast<OpCode_Repeat const&>(opcode).offset();
  76. if (repeat_start > end_of_last_block)
  77. block_boundaries.append({ end_of_last_block, repeat_start });
  78. block_boundaries.append({ repeat_start, state.instruction_position });
  79. end_of_last_block = state.instruction_position + opcode.size();
  80. break;
  81. }
  82. default:
  83. break;
  84. }
  85. auto next_ip = state.instruction_position + opcode.size();
  86. if (next_ip < bytecode_size)
  87. state.instruction_position = next_ip;
  88. else
  89. break;
  90. }
  91. if (end_of_last_block < bytecode_size)
  92. block_boundaries.append({ end_of_last_block, bytecode_size });
  93. quick_sort(block_boundaries, [](auto& a, auto& b) { return a.start < b.start; });
  94. return block_boundaries;
  95. }
  96. static bool has_overlap(Vector<CompareTypeAndValuePair> const& lhs, Vector<CompareTypeAndValuePair> const& rhs)
  97. {
  98. // We have to fully interpret the two sequences to determine if they overlap (that is, keep track of inversion state and what ranges they cover).
  99. bool inverse { false };
  100. bool temporary_inverse { false };
  101. bool reset_temporary_inverse { false };
  102. auto current_lhs_inversion_state = [&]() -> bool { return temporary_inverse ^ inverse; };
  103. RedBlackTree<u32, u32> lhs_ranges;
  104. RedBlackTree<u32, u32> lhs_negated_ranges;
  105. HashTable<CharClass> lhs_char_classes;
  106. HashTable<CharClass> lhs_negated_char_classes;
  107. auto range_contains = [&]<typename T>(T& value) -> bool {
  108. u32 start;
  109. u32 end;
  110. if constexpr (IsSame<T, CharRange>) {
  111. start = value.from;
  112. end = value.to;
  113. } else {
  114. start = value;
  115. end = value;
  116. }
  117. auto* max = lhs_ranges.find_smallest_not_below(start);
  118. return max && *max <= end;
  119. };
  120. auto char_class_contains = [&](CharClass const& value) -> bool {
  121. if (lhs_char_classes.contains(value))
  122. return true;
  123. if (lhs_negated_char_classes.contains(value))
  124. return false;
  125. // This char class might match something in the ranges we have, and checking that is far too expensive, so just bail out.
  126. return true;
  127. };
  128. for (auto const& pair : lhs) {
  129. if (reset_temporary_inverse) {
  130. reset_temporary_inverse = false;
  131. temporary_inverse = false;
  132. } else {
  133. reset_temporary_inverse = true;
  134. }
  135. switch (pair.type) {
  136. case CharacterCompareType::Inverse:
  137. inverse = !inverse;
  138. break;
  139. case CharacterCompareType::TemporaryInverse:
  140. temporary_inverse = true;
  141. reset_temporary_inverse = true;
  142. break;
  143. case CharacterCompareType::AnyChar:
  144. // Special case: if not inverted, AnyChar is always in the range.
  145. if (!current_lhs_inversion_state())
  146. return true;
  147. break;
  148. case CharacterCompareType::Char:
  149. if (!current_lhs_inversion_state())
  150. lhs_ranges.insert(pair.value, pair.value);
  151. else
  152. lhs_negated_ranges.insert(pair.value, pair.value);
  153. break;
  154. case CharacterCompareType::String:
  155. // FIXME: We just need to look at the last character of this string, but we only have the first character here.
  156. // Just bail out to avoid false positives.
  157. return true;
  158. case CharacterCompareType::CharClass:
  159. if (!current_lhs_inversion_state())
  160. lhs_char_classes.set(static_cast<CharClass>(pair.value));
  161. else
  162. lhs_negated_char_classes.set(static_cast<CharClass>(pair.value));
  163. break;
  164. case CharacterCompareType::CharRange: {
  165. auto range = CharRange(pair.value);
  166. if (!current_lhs_inversion_state())
  167. lhs_ranges.insert(range.from, range.to);
  168. else
  169. lhs_negated_ranges.insert(range.from, range.to);
  170. break;
  171. }
  172. case CharacterCompareType::LookupTable:
  173. // We've transformed this into a series of ranges in flat_compares(), so bail out if we see it.
  174. return true;
  175. case CharacterCompareType::Reference:
  176. // We've handled this before coming here.
  177. break;
  178. case CharacterCompareType::Property:
  179. case CharacterCompareType::GeneralCategory:
  180. case CharacterCompareType::Script:
  181. case CharacterCompareType::ScriptExtension:
  182. case CharacterCompareType::And:
  183. case CharacterCompareType::Or:
  184. case CharacterCompareType::EndAndOr:
  185. // FIXME: These are too difficult to handle, so bail out.
  186. return true;
  187. case CharacterCompareType::Undefined:
  188. case CharacterCompareType::RangeExpressionDummy:
  189. // These do not occur in valid bytecode.
  190. VERIFY_NOT_REACHED();
  191. }
  192. }
  193. if constexpr (REGEX_DEBUG) {
  194. dbgln("lhs ranges:");
  195. for (auto it = lhs_ranges.begin(); it != lhs_ranges.end(); ++it)
  196. dbgln(" {}..{}", it.key(), *it);
  197. dbgln("lhs negated ranges:");
  198. for (auto it = lhs_negated_ranges.begin(); it != lhs_negated_ranges.end(); ++it)
  199. dbgln(" {}..{}", it.key(), *it);
  200. }
  201. for (auto const& pair : rhs) {
  202. if (reset_temporary_inverse) {
  203. reset_temporary_inverse = false;
  204. temporary_inverse = false;
  205. } else {
  206. reset_temporary_inverse = true;
  207. }
  208. dbgln_if(REGEX_DEBUG, "check {} ({})...", character_compare_type_name(pair.type), pair.value);
  209. switch (pair.type) {
  210. case CharacterCompareType::Inverse:
  211. inverse = !inverse;
  212. break;
  213. case CharacterCompareType::TemporaryInverse:
  214. temporary_inverse = true;
  215. reset_temporary_inverse = true;
  216. break;
  217. case CharacterCompareType::AnyChar:
  218. // Special case: if not inverted, AnyChar is always in the range.
  219. if (!current_lhs_inversion_state())
  220. return true;
  221. break;
  222. case CharacterCompareType::Char:
  223. if (!current_lhs_inversion_state() && range_contains(pair.value))
  224. return true;
  225. break;
  226. case CharacterCompareType::String:
  227. // FIXME: We just need to look at the last character of this string, but we only have the first character here.
  228. // Just bail out to avoid false positives.
  229. return true;
  230. case CharacterCompareType::CharClass:
  231. if (!current_lhs_inversion_state() && char_class_contains(static_cast<CharClass>(pair.value)))
  232. return true;
  233. break;
  234. case CharacterCompareType::CharRange: {
  235. auto range = CharRange(pair.value);
  236. if (!current_lhs_inversion_state() && range_contains(range))
  237. return true;
  238. break;
  239. }
  240. case CharacterCompareType::LookupTable:
  241. // We've transformed this into a series of ranges in flat_compares(), so bail out if we see it.
  242. return true;
  243. case CharacterCompareType::Reference:
  244. // We've handled this before coming here.
  245. break;
  246. case CharacterCompareType::Property:
  247. case CharacterCompareType::GeneralCategory:
  248. case CharacterCompareType::Script:
  249. case CharacterCompareType::ScriptExtension:
  250. case CharacterCompareType::And:
  251. case CharacterCompareType::Or:
  252. case CharacterCompareType::EndAndOr:
  253. // FIXME: These are too difficult to handle, so bail out.
  254. return true;
  255. case CharacterCompareType::Undefined:
  256. case CharacterCompareType::RangeExpressionDummy:
  257. // These do not occur in valid bytecode.
  258. VERIFY_NOT_REACHED();
  259. }
  260. }
  261. return false;
  262. }
  263. enum class AtomicRewritePreconditionResult {
  264. SatisfiedWithProperHeader,
  265. SatisfiedWithEmptyHeader,
  266. NotSatisfied,
  267. };
  268. static AtomicRewritePreconditionResult block_satisfies_atomic_rewrite_precondition(ByteCode const& bytecode, Block const& repeated_block, Block const& following_block)
  269. {
  270. Vector<Vector<CompareTypeAndValuePair>> repeated_values;
  271. HashTable<size_t> active_capture_groups;
  272. MatchState state;
  273. for (state.instruction_position = repeated_block.start; state.instruction_position < repeated_block.end;) {
  274. auto& opcode = bytecode.get_opcode(state);
  275. switch (opcode.opcode_id()) {
  276. case OpCodeId::Compare: {
  277. auto compares = static_cast<OpCode_Compare const&>(opcode).flat_compares();
  278. if (repeated_values.is_empty() && any_of(compares, [](auto& compare) { return compare.type == CharacterCompareType::AnyChar; }))
  279. return AtomicRewritePreconditionResult::NotSatisfied;
  280. repeated_values.append(move(compares));
  281. break;
  282. }
  283. case OpCodeId::CheckBegin:
  284. case OpCodeId::CheckEnd:
  285. if (repeated_values.is_empty())
  286. return AtomicRewritePreconditionResult::SatisfiedWithProperHeader;
  287. break;
  288. case OpCodeId::CheckBoundary:
  289. // FIXME: What should we do with these? for now, let's fail.
  290. return AtomicRewritePreconditionResult::NotSatisfied;
  291. case OpCodeId::Restore:
  292. case OpCodeId::GoBack:
  293. return AtomicRewritePreconditionResult::NotSatisfied;
  294. case OpCodeId::SaveRightCaptureGroup:
  295. active_capture_groups.set(static_cast<OpCode_SaveRightCaptureGroup const&>(opcode).id());
  296. break;
  297. case OpCodeId::SaveLeftCaptureGroup:
  298. active_capture_groups.set(static_cast<OpCode_SaveLeftCaptureGroup const&>(opcode).id());
  299. break;
  300. default:
  301. break;
  302. }
  303. state.instruction_position += opcode.size();
  304. }
  305. dbgln_if(REGEX_DEBUG, "Found {} entries in reference", repeated_values.size());
  306. dbgln_if(REGEX_DEBUG, "Found {} active capture groups", active_capture_groups.size());
  307. bool following_block_has_at_least_one_compare = false;
  308. // Find the first compare in the following block, it must NOT match any of the values in `repeated_values'.
  309. for (state.instruction_position = following_block.start; state.instruction_position < following_block.end;) {
  310. auto& opcode = bytecode.get_opcode(state);
  311. switch (opcode.opcode_id()) {
  312. // Note: These have to exist since we're effectively repeating the following block as well
  313. case OpCodeId::SaveRightCaptureGroup:
  314. active_capture_groups.set(static_cast<OpCode_SaveRightCaptureGroup const&>(opcode).id());
  315. break;
  316. case OpCodeId::SaveLeftCaptureGroup:
  317. active_capture_groups.set(static_cast<OpCode_SaveLeftCaptureGroup const&>(opcode).id());
  318. break;
  319. case OpCodeId::Compare: {
  320. following_block_has_at_least_one_compare = true;
  321. // We found a compare, let's see what it has.
  322. auto compares = static_cast<OpCode_Compare const&>(opcode).flat_compares();
  323. if (compares.is_empty())
  324. break;
  325. if (any_of(compares, [&](auto& compare) {
  326. return compare.type == CharacterCompareType::AnyChar
  327. || (compare.type == CharacterCompareType::Reference && active_capture_groups.contains(compare.value));
  328. }))
  329. return AtomicRewritePreconditionResult::NotSatisfied;
  330. if (any_of(repeated_values, [&](auto& repeated_value) { return has_overlap(compares, repeated_value); }))
  331. return AtomicRewritePreconditionResult::NotSatisfied;
  332. return AtomicRewritePreconditionResult::SatisfiedWithProperHeader;
  333. }
  334. case OpCodeId::CheckBegin:
  335. case OpCodeId::CheckEnd:
  336. return AtomicRewritePreconditionResult::SatisfiedWithProperHeader; // Nothing can match the end!
  337. case OpCodeId::CheckBoundary:
  338. // FIXME: What should we do with these? For now, consider them a failure.
  339. return AtomicRewritePreconditionResult::NotSatisfied;
  340. default:
  341. break;
  342. }
  343. state.instruction_position += opcode.size();
  344. }
  345. if (following_block_has_at_least_one_compare)
  346. return AtomicRewritePreconditionResult::SatisfiedWithProperHeader;
  347. return AtomicRewritePreconditionResult::SatisfiedWithEmptyHeader;
  348. }
  349. template<typename Parser>
  350. void Regex<Parser>::attempt_rewrite_loops_as_atomic_groups(BasicBlockList const& basic_blocks)
  351. {
  352. auto& bytecode = parser_result.bytecode;
  353. if constexpr (REGEX_DEBUG) {
  354. RegexDebug dbg;
  355. dbg.print_bytecode(*this);
  356. for (auto const& block : basic_blocks)
  357. dbgln("block from {} to {}", block.start, block.end);
  358. }
  359. // A pattern such as:
  360. // bb0 | RE0
  361. // | ForkX bb0
  362. // -------------------------
  363. // bb1 | RE1
  364. // can be rewritten as:
  365. // -------------------------
  366. // bb0 | RE0
  367. // | ForkReplaceX bb0
  368. // -------------------------
  369. // bb1 | RE1
  370. // provided that first(RE1) not-in end(RE0), which is to say
  371. // that RE1 cannot start with whatever RE0 has matched (ever).
  372. //
  373. // Alternatively, a second form of this pattern can also occur:
  374. // bb0 | *
  375. // | ForkX bb2
  376. // ------------------------
  377. // bb1 | RE0
  378. // | Jump bb0
  379. // ------------------------
  380. // bb2 | RE1
  381. // which can be transformed (with the same preconditions) to:
  382. // bb0 | *
  383. // | ForkReplaceX bb2
  384. // ------------------------
  385. // bb1 | RE0
  386. // | Jump bb0
  387. // ------------------------
  388. // bb2 | RE1
  389. enum class AlternateForm {
  390. DirectLoopWithoutHeader, // loop without proper header, a block forking to itself. i.e. the first form.
  391. DirectLoopWithoutHeaderAndEmptyFollow, // loop without proper header, a block forking to itself. i.e. the first form but with RE1 being empty.
  392. DirectLoopWithHeader, // loop with proper header, i.e. the second form.
  393. };
  394. struct CandidateBlock {
  395. Block forking_block;
  396. Optional<Block> new_target_block;
  397. AlternateForm form;
  398. };
  399. Vector<CandidateBlock> candidate_blocks;
  400. auto is_an_eligible_jump = [](OpCode const& opcode, size_t ip, size_t block_start, AlternateForm alternate_form) {
  401. switch (opcode.opcode_id()) {
  402. case OpCodeId::JumpNonEmpty: {
  403. auto const& op = static_cast<OpCode_JumpNonEmpty const&>(opcode);
  404. auto form = op.form();
  405. if (form != OpCodeId::Jump && alternate_form == AlternateForm::DirectLoopWithHeader)
  406. return false;
  407. if (form != OpCodeId::ForkJump && form != OpCodeId::ForkStay && alternate_form == AlternateForm::DirectLoopWithoutHeader)
  408. return false;
  409. return op.offset() + ip + opcode.size() == block_start;
  410. }
  411. case OpCodeId::ForkJump:
  412. if (alternate_form == AlternateForm::DirectLoopWithHeader)
  413. return false;
  414. return static_cast<OpCode_ForkJump const&>(opcode).offset() + ip + opcode.size() == block_start;
  415. case OpCodeId::ForkStay:
  416. if (alternate_form == AlternateForm::DirectLoopWithHeader)
  417. return false;
  418. return static_cast<OpCode_ForkStay const&>(opcode).offset() + ip + opcode.size() == block_start;
  419. case OpCodeId::Jump:
  420. // Infinite loop does *not* produce forks.
  421. if (alternate_form == AlternateForm::DirectLoopWithoutHeader)
  422. return false;
  423. if (alternate_form == AlternateForm::DirectLoopWithHeader)
  424. return static_cast<OpCode_Jump const&>(opcode).offset() + ip + opcode.size() == block_start;
  425. VERIFY_NOT_REACHED();
  426. default:
  427. return false;
  428. }
  429. };
  430. for (size_t i = 0; i < basic_blocks.size(); ++i) {
  431. auto forking_block = basic_blocks[i];
  432. Optional<Block> fork_fallback_block;
  433. if (i + 1 < basic_blocks.size())
  434. fork_fallback_block = basic_blocks[i + 1];
  435. MatchState state;
  436. // Check if the last instruction in this block is a jump to the block itself:
  437. {
  438. state.instruction_position = forking_block.end;
  439. auto& opcode = bytecode.get_opcode(state);
  440. if (is_an_eligible_jump(opcode, state.instruction_position, forking_block.start, AlternateForm::DirectLoopWithoutHeader)) {
  441. // We've found RE0 (and RE1 is just the following block, if any), let's see if the precondition applies.
  442. // if RE1 is empty, there's no first(RE1), so this is an automatic pass.
  443. if (!fork_fallback_block.has_value() || fork_fallback_block->end == fork_fallback_block->start) {
  444. candidate_blocks.append({ forking_block, fork_fallback_block, AlternateForm::DirectLoopWithoutHeader });
  445. break;
  446. }
  447. auto precondition = block_satisfies_atomic_rewrite_precondition(bytecode, forking_block, *fork_fallback_block);
  448. if (precondition == AtomicRewritePreconditionResult::SatisfiedWithProperHeader) {
  449. candidate_blocks.append({ forking_block, fork_fallback_block, AlternateForm::DirectLoopWithoutHeader });
  450. break;
  451. }
  452. if (precondition == AtomicRewritePreconditionResult::SatisfiedWithEmptyHeader) {
  453. candidate_blocks.append({ forking_block, fork_fallback_block, AlternateForm::DirectLoopWithoutHeaderAndEmptyFollow });
  454. break;
  455. }
  456. }
  457. }
  458. // Check if the last instruction in the last block is a direct jump to this block
  459. if (fork_fallback_block.has_value()) {
  460. state.instruction_position = fork_fallback_block->end;
  461. auto& opcode = bytecode.get_opcode(state);
  462. if (is_an_eligible_jump(opcode, state.instruction_position, forking_block.start, AlternateForm::DirectLoopWithHeader)) {
  463. // We've found bb1 and bb0, let's just make sure that bb0 forks to bb2.
  464. state.instruction_position = forking_block.end;
  465. auto& opcode = bytecode.get_opcode(state);
  466. if (opcode.opcode_id() == OpCodeId::ForkJump || opcode.opcode_id() == OpCodeId::ForkStay) {
  467. Optional<Block> block_following_fork_fallback;
  468. if (i + 2 < basic_blocks.size())
  469. block_following_fork_fallback = basic_blocks[i + 2];
  470. if (!block_following_fork_fallback.has_value()
  471. || block_satisfies_atomic_rewrite_precondition(bytecode, *fork_fallback_block, *block_following_fork_fallback) != AtomicRewritePreconditionResult::NotSatisfied) {
  472. candidate_blocks.append({ forking_block, {}, AlternateForm::DirectLoopWithHeader });
  473. break;
  474. }
  475. }
  476. }
  477. }
  478. }
  479. dbgln_if(REGEX_DEBUG, "Found {} candidate blocks", candidate_blocks.size());
  480. if (candidate_blocks.is_empty()) {
  481. dbgln_if(REGEX_DEBUG, "Failed to find anything for {}", pattern_value);
  482. return;
  483. }
  484. RedBlackTree<size_t, size_t> needed_patches;
  485. // Reverse the blocks, so we can patch the bytecode without messing with the latter patches.
  486. quick_sort(candidate_blocks, [](auto& a, auto& b) { return b.forking_block.start > a.forking_block.start; });
  487. for (auto& candidate : candidate_blocks) {
  488. // Note that both forms share a ForkReplace patch in forking_block.
  489. // Patch the ForkX in forking_block to be a ForkReplaceX instead.
  490. auto& opcode_id = bytecode[candidate.forking_block.end];
  491. if (opcode_id == (ByteCodeValueType)OpCodeId::ForkStay) {
  492. opcode_id = (ByteCodeValueType)OpCodeId::ForkReplaceStay;
  493. } else if (opcode_id == (ByteCodeValueType)OpCodeId::ForkJump) {
  494. opcode_id = (ByteCodeValueType)OpCodeId::ForkReplaceJump;
  495. } else if (opcode_id == (ByteCodeValueType)OpCodeId::JumpNonEmpty) {
  496. auto& jump_opcode_id = bytecode[candidate.forking_block.end + 3];
  497. if (jump_opcode_id == (ByteCodeValueType)OpCodeId::ForkStay)
  498. jump_opcode_id = (ByteCodeValueType)OpCodeId::ForkReplaceStay;
  499. else if (jump_opcode_id == (ByteCodeValueType)OpCodeId::ForkJump)
  500. jump_opcode_id = (ByteCodeValueType)OpCodeId::ForkReplaceJump;
  501. else
  502. VERIFY_NOT_REACHED();
  503. } else {
  504. VERIFY_NOT_REACHED();
  505. }
  506. }
  507. if (!needed_patches.is_empty()) {
  508. MatchState state;
  509. auto bytecode_size = bytecode.size();
  510. state.instruction_position = 0;
  511. struct Patch {
  512. ssize_t value;
  513. size_t offset;
  514. bool should_negate { false };
  515. };
  516. for (;;) {
  517. if (state.instruction_position >= bytecode_size)
  518. break;
  519. auto& opcode = bytecode.get_opcode(state);
  520. Stack<Patch, 2> patch_points;
  521. switch (opcode.opcode_id()) {
  522. case OpCodeId::Jump:
  523. patch_points.push({ static_cast<OpCode_Jump const&>(opcode).offset(), state.instruction_position + 1 });
  524. break;
  525. case OpCodeId::JumpNonEmpty:
  526. patch_points.push({ static_cast<OpCode_JumpNonEmpty const&>(opcode).offset(), state.instruction_position + 1 });
  527. patch_points.push({ static_cast<OpCode_JumpNonEmpty const&>(opcode).checkpoint(), state.instruction_position + 2 });
  528. break;
  529. case OpCodeId::ForkJump:
  530. patch_points.push({ static_cast<OpCode_ForkJump const&>(opcode).offset(), state.instruction_position + 1 });
  531. break;
  532. case OpCodeId::ForkStay:
  533. patch_points.push({ static_cast<OpCode_ForkStay const&>(opcode).offset(), state.instruction_position + 1 });
  534. break;
  535. case OpCodeId::Repeat:
  536. patch_points.push({ -(ssize_t) static_cast<OpCode_Repeat const&>(opcode).offset(), state.instruction_position + 1, true });
  537. break;
  538. default:
  539. break;
  540. }
  541. while (!patch_points.is_empty()) {
  542. auto& patch_point = patch_points.top();
  543. auto target_offset = patch_point.value + state.instruction_position + opcode.size();
  544. constexpr auto do_patch = [](auto& patch_it, auto& patch_point, auto& target_offset, auto& bytecode, auto ip) {
  545. if (patch_it.key() == ip)
  546. return;
  547. if (patch_point.value < 0 && target_offset <= patch_it.key() && ip > patch_it.key())
  548. bytecode[patch_point.offset] += (patch_point.should_negate ? 1 : -1) * (*patch_it);
  549. else if (patch_point.value > 0 && target_offset >= patch_it.key() && ip < patch_it.key())
  550. bytecode[patch_point.offset] += (patch_point.should_negate ? -1 : 1) * (*patch_it);
  551. };
  552. if (auto patch_it = needed_patches.find_largest_not_above_iterator(target_offset); !patch_it.is_end())
  553. do_patch(patch_it, patch_point, target_offset, bytecode, state.instruction_position);
  554. else if (auto patch_it = needed_patches.find_largest_not_above_iterator(state.instruction_position); !patch_it.is_end())
  555. do_patch(patch_it, patch_point, target_offset, bytecode, state.instruction_position);
  556. patch_points.pop();
  557. }
  558. state.instruction_position += opcode.size();
  559. }
  560. }
  561. if constexpr (REGEX_DEBUG) {
  562. warnln("Transformed to:");
  563. RegexDebug dbg;
  564. dbg.print_bytecode(*this);
  565. }
  566. }
  567. void Optimizer::append_alternation(ByteCode& target, ByteCode&& left, ByteCode&& right)
  568. {
  569. Array<ByteCode, 2> alternatives;
  570. alternatives[0] = move(left);
  571. alternatives[1] = move(right);
  572. append_alternation(target, alternatives);
  573. }
  574. void Optimizer::append_alternation(ByteCode& target, Span<ByteCode> alternatives)
  575. {
  576. if (alternatives.size() == 0)
  577. return;
  578. if (alternatives.size() == 1)
  579. return target.extend(move(alternatives[0]));
  580. if (all_of(alternatives, [](auto& x) { return x.is_empty(); }))
  581. return;
  582. for (auto& entry : alternatives)
  583. entry.flatten();
  584. #if REGEX_DEBUG
  585. ScopeLogger<true> log;
  586. warnln("Alternations:");
  587. RegexDebug dbg;
  588. for (auto& entry : alternatives) {
  589. warnln("----------");
  590. dbg.print_bytecode(entry);
  591. }
  592. ScopeGuard print_at_end {
  593. [&] {
  594. warnln("======================");
  595. RegexDebug dbg;
  596. dbg.print_bytecode(target);
  597. }
  598. };
  599. #endif
  600. Vector<Vector<Detail::Block>> basic_blocks;
  601. basic_blocks.ensure_capacity(alternatives.size());
  602. for (auto& entry : alternatives)
  603. basic_blocks.append(Regex<PosixBasicParser>::split_basic_blocks(entry));
  604. size_t left_skip = 0;
  605. size_t shared_block_count = basic_blocks.first().size();
  606. for (auto& entry : basic_blocks)
  607. shared_block_count = min(shared_block_count, entry.size());
  608. MatchState state;
  609. for (size_t block_index = 0; block_index < shared_block_count; block_index++) {
  610. auto& left_block = basic_blocks.first()[block_index];
  611. auto left_end = block_index + 1 == basic_blocks.first().size() ? left_block.end : basic_blocks.first()[block_index + 1].start;
  612. auto can_continue = true;
  613. for (size_t i = 1; i < alternatives.size(); ++i) {
  614. auto& right_blocks = basic_blocks[i];
  615. auto& right_block = right_blocks[block_index];
  616. auto right_end = block_index + 1 == right_blocks.size() ? right_block.end : right_blocks[block_index + 1].start;
  617. if (left_end - left_block.start != right_end - right_block.start) {
  618. can_continue = false;
  619. break;
  620. }
  621. if (alternatives[0].spans().slice(left_block.start, left_end - left_block.start) != alternatives[i].spans().slice(right_block.start, right_end - right_block.start)) {
  622. can_continue = false;
  623. break;
  624. }
  625. }
  626. if (!can_continue)
  627. break;
  628. size_t i = 0;
  629. for (auto& entry : alternatives) {
  630. auto& blocks = basic_blocks[i];
  631. auto& block = blocks[block_index];
  632. auto end = block_index + 1 == blocks.size() ? block.end : blocks[block_index + 1].start;
  633. state.instruction_position = block.start;
  634. size_t skip = 0;
  635. while (state.instruction_position < end) {
  636. auto& opcode = entry.get_opcode(state);
  637. state.instruction_position += opcode.size();
  638. skip = state.instruction_position;
  639. }
  640. left_skip = min(skip, left_skip);
  641. }
  642. }
  643. dbgln_if(REGEX_DEBUG, "Skipping {}/{} bytecode entries from {}", left_skip, 0, alternatives[0].size());
  644. if (left_skip > 0) {
  645. target.extend(alternatives[0].release_slice(basic_blocks.first().first().start, left_skip));
  646. auto first = true;
  647. for (auto& entry : alternatives) {
  648. if (first) {
  649. first = false;
  650. continue;
  651. }
  652. entry = entry.release_slice(left_skip);
  653. }
  654. }
  655. if (all_of(alternatives, [](auto& entry) { return entry.is_empty(); }))
  656. return;
  657. size_t patch_start = target.size();
  658. for (size_t i = 1; i < alternatives.size(); ++i) {
  659. target.empend(static_cast<ByteCodeValueType>(OpCodeId::ForkJump));
  660. target.empend(0u); // To be filled later.
  661. }
  662. size_t size_to_jump = 0;
  663. bool seen_one_empty = false;
  664. for (size_t i = alternatives.size(); i > 0; --i) {
  665. auto& entry = alternatives[i - 1];
  666. if (entry.is_empty()) {
  667. if (seen_one_empty)
  668. continue;
  669. seen_one_empty = true;
  670. }
  671. auto is_first = i == 1;
  672. auto instruction_size = entry.size() + (is_first ? 0 : 2); // Jump; -> +2
  673. size_to_jump += instruction_size;
  674. if (!is_first)
  675. target[patch_start + (i - 2) * 2 + 1] = size_to_jump + (alternatives.size() - i) * 2;
  676. dbgln_if(REGEX_DEBUG, "{} size = {}, cum={}", i - 1, instruction_size, size_to_jump);
  677. }
  678. seen_one_empty = false;
  679. for (size_t i = alternatives.size(); i > 0; --i) {
  680. auto& chunk = alternatives[i - 1];
  681. if (chunk.is_empty()) {
  682. if (seen_one_empty)
  683. continue;
  684. seen_one_empty = true;
  685. }
  686. ByteCode* previous_chunk = nullptr;
  687. size_t j = i - 1;
  688. auto seen_one_empty_before = chunk.is_empty();
  689. while (j >= 1) {
  690. --j;
  691. auto& candidate_chunk = alternatives[j];
  692. if (candidate_chunk.is_empty()) {
  693. if (seen_one_empty_before)
  694. continue;
  695. }
  696. previous_chunk = &candidate_chunk;
  697. break;
  698. }
  699. size_to_jump -= chunk.size() + (previous_chunk ? 2 : 0);
  700. target.extend(move(chunk));
  701. target.empend(static_cast<ByteCodeValueType>(OpCodeId::Jump));
  702. target.empend(size_to_jump); // Jump to the _END label
  703. }
  704. }
  705. enum class LookupTableInsertionOutcome {
  706. Successful,
  707. ReplaceWithAnyChar,
  708. TemporaryInversionNeeded,
  709. PermanentInversionNeeded,
  710. FlushOnInsertion,
  711. FinishFlushOnInsertion,
  712. CannotPlaceInTable,
  713. };
  714. static LookupTableInsertionOutcome insert_into_lookup_table(RedBlackTree<ByteCodeValueType, CharRange>& table, CompareTypeAndValuePair pair)
  715. {
  716. switch (pair.type) {
  717. case CharacterCompareType::Inverse:
  718. return LookupTableInsertionOutcome::PermanentInversionNeeded;
  719. case CharacterCompareType::TemporaryInverse:
  720. return LookupTableInsertionOutcome::TemporaryInversionNeeded;
  721. case CharacterCompareType::AnyChar:
  722. return LookupTableInsertionOutcome::ReplaceWithAnyChar;
  723. case CharacterCompareType::CharClass:
  724. return LookupTableInsertionOutcome::CannotPlaceInTable;
  725. case CharacterCompareType::Char:
  726. table.insert(pair.value, { (u32)pair.value, (u32)pair.value });
  727. break;
  728. case CharacterCompareType::CharRange: {
  729. CharRange range { pair.value };
  730. table.insert(range.from, range);
  731. break;
  732. }
  733. case CharacterCompareType::EndAndOr:
  734. return LookupTableInsertionOutcome::FinishFlushOnInsertion;
  735. case CharacterCompareType::And:
  736. return LookupTableInsertionOutcome::FlushOnInsertion;
  737. case CharacterCompareType::Reference:
  738. case CharacterCompareType::Property:
  739. case CharacterCompareType::GeneralCategory:
  740. case CharacterCompareType::Script:
  741. case CharacterCompareType::ScriptExtension:
  742. case CharacterCompareType::Or:
  743. return LookupTableInsertionOutcome::CannotPlaceInTable;
  744. case CharacterCompareType::Undefined:
  745. case CharacterCompareType::RangeExpressionDummy:
  746. case CharacterCompareType::String:
  747. case CharacterCompareType::LookupTable:
  748. VERIFY_NOT_REACHED();
  749. }
  750. return LookupTableInsertionOutcome::Successful;
  751. }
  752. void Optimizer::append_character_class(ByteCode& target, Vector<CompareTypeAndValuePair>&& pairs)
  753. {
  754. ByteCode arguments;
  755. size_t argument_count = 0;
  756. if (pairs.size() <= 1) {
  757. for (auto& pair : pairs) {
  758. arguments.append(to_underlying(pair.type));
  759. if (pair.type != CharacterCompareType::AnyChar
  760. && pair.type != CharacterCompareType::TemporaryInverse
  761. && pair.type != CharacterCompareType::Inverse
  762. && pair.type != CharacterCompareType::And
  763. && pair.type != CharacterCompareType::Or
  764. && pair.type != CharacterCompareType::EndAndOr)
  765. arguments.append(pair.value);
  766. ++argument_count;
  767. }
  768. } else {
  769. RedBlackTree<ByteCodeValueType, CharRange> table;
  770. RedBlackTree<ByteCodeValueType, CharRange> inverted_table;
  771. auto* current_table = &table;
  772. auto* current_inverted_table = &inverted_table;
  773. bool invert_for_next_iteration = false;
  774. bool is_currently_inverted = false;
  775. auto flush_tables = [&] {
  776. auto append_table = [&](auto& table) {
  777. ++argument_count;
  778. arguments.append(to_underlying(CharacterCompareType::LookupTable));
  779. auto size_index = arguments.size();
  780. arguments.append(0);
  781. Optional<CharRange> active_range;
  782. size_t range_count = 0;
  783. for (auto& range : table) {
  784. if (!active_range.has_value()) {
  785. active_range = range;
  786. continue;
  787. }
  788. if (range.from <= active_range->to + 1 && range.to + 1 >= active_range->from) {
  789. active_range = CharRange { min(range.from, active_range->from), max(range.to, active_range->to) };
  790. } else {
  791. ++range_count;
  792. arguments.append(active_range.release_value());
  793. active_range = range;
  794. }
  795. }
  796. if (active_range.has_value()) {
  797. ++range_count;
  798. arguments.append(active_range.release_value());
  799. }
  800. arguments[size_index] = range_count;
  801. };
  802. auto contains_regular_table = !table.is_empty();
  803. auto contains_inverted_table = !inverted_table.is_empty();
  804. if (contains_regular_table)
  805. append_table(table);
  806. if (contains_inverted_table) {
  807. ++argument_count;
  808. arguments.append(to_underlying(CharacterCompareType::TemporaryInverse));
  809. append_table(inverted_table);
  810. }
  811. table.clear();
  812. inverted_table.clear();
  813. };
  814. auto flush_on_every_insertion = false;
  815. for (auto& value : pairs) {
  816. auto should_invert_after_this_iteration = invert_for_next_iteration;
  817. invert_for_next_iteration = false;
  818. auto insertion_result = insert_into_lookup_table(*current_table, value);
  819. switch (insertion_result) {
  820. case LookupTableInsertionOutcome::Successful:
  821. if (flush_on_every_insertion)
  822. flush_tables();
  823. break;
  824. case LookupTableInsertionOutcome::ReplaceWithAnyChar: {
  825. table.clear();
  826. inverted_table.clear();
  827. arguments.append(to_underlying(CharacterCompareType::AnyChar));
  828. ++argument_count;
  829. break;
  830. }
  831. case LookupTableInsertionOutcome::TemporaryInversionNeeded:
  832. swap(current_table, current_inverted_table);
  833. invert_for_next_iteration = true;
  834. is_currently_inverted = !is_currently_inverted;
  835. break;
  836. case LookupTableInsertionOutcome::PermanentInversionNeeded:
  837. flush_tables();
  838. arguments.append(to_underlying(CharacterCompareType::Inverse));
  839. ++argument_count;
  840. break;
  841. case LookupTableInsertionOutcome::FlushOnInsertion:
  842. case LookupTableInsertionOutcome::FinishFlushOnInsertion:
  843. flush_tables();
  844. flush_on_every_insertion = insertion_result == LookupTableInsertionOutcome::FlushOnInsertion;
  845. [[fallthrough]];
  846. case LookupTableInsertionOutcome::CannotPlaceInTable:
  847. if (is_currently_inverted) {
  848. arguments.append(to_underlying(CharacterCompareType::TemporaryInverse));
  849. ++argument_count;
  850. }
  851. arguments.append(to_underlying(value.type));
  852. if (value.type != CharacterCompareType::AnyChar
  853. && value.type != CharacterCompareType::TemporaryInverse
  854. && value.type != CharacterCompareType::Inverse
  855. && value.type != CharacterCompareType::And
  856. && value.type != CharacterCompareType::Or
  857. && value.type != CharacterCompareType::EndAndOr)
  858. arguments.append(value.value);
  859. ++argument_count;
  860. break;
  861. }
  862. if (should_invert_after_this_iteration) {
  863. swap(current_table, current_inverted_table);
  864. is_currently_inverted = !is_currently_inverted;
  865. }
  866. }
  867. flush_tables();
  868. }
  869. target.empend(static_cast<ByteCodeValueType>(OpCodeId::Compare));
  870. target.empend(argument_count); // number of arguments
  871. target.empend(arguments.size()); // size of arguments
  872. target.extend(move(arguments));
  873. }
  874. template void Regex<PosixBasicParser>::run_optimization_passes();
  875. template void Regex<PosixExtendedParser>::run_optimization_passes();
  876. template void Regex<ECMA262Parser>::run_optimization_passes();
  877. }