RegexRuntime.h 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. //
  6. // Regex programs and their execution context
  7. //
  8. #pragma once
  9. namespace UnifiedRegex
  10. {
  11. typedef CharCount Label;
  12. // FORWARD
  13. struct ScannerInfo;
  14. class ContStack;
  15. class AssertionStack;
  16. class OctoquadMatcher;
  17. enum class ChompMode : uint8
  18. {
  19. Star, // min = 0, max = infinite
  20. Plus // min = 1, max = infinite
  21. };
  22. // ----------------------------------------------------------------------
  23. // Programs
  24. // ----------------------------------------------------------------------
  25. struct Program : private Chars<char16>
  26. {
  27. friend class Lowerer;
  28. friend class Compiler;
  29. friend struct MatchLiteralNode;
  30. friend struct AltNode;
  31. friend class Matcher;
  32. friend struct LoopInfo;
  33. template <typename ScannerT>
  34. friend struct SyncToLiteralAndConsumeInstT;
  35. template <typename ScannerT>
  36. friend struct SyncToLiteralAndContinueInstT;
  37. template <typename ScannerT>
  38. friend struct SyncToLiteralAndBackupInstT;
  39. template <typename ScannerT>
  40. friend struct ScannerMixinT;
  41. template <uint lastPatCharEquivClassSize>
  42. friend struct EquivScannerMixinT;
  43. #define M(TagName) friend struct TagName##Inst;
  44. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, ...) TemplateDeclaration friend struct GenericClassName;
  45. #include "RegexOpCodes.h"
  46. #undef M
  47. #undef MTemplate
  48. public:
  49. // Copy of original text of regex (without delimiting '/'s or trailing flags), null terminated.
  50. // In run-time allocator, owned by program
  51. Field(Char*) source;
  52. Field(CharCount) sourceLen; // length in char16's, NOT including terminating null
  53. // Number of capturing groups (including implicit overall group at index 0)
  54. Field(uint16) numGroups;
  55. Field(int) numLoops;
  56. Field(RegexFlags) flags;
  57. private:
  58. enum class ProgramTag : uint8
  59. {
  60. InstructionsTag,
  61. BOIInstructionsTag,
  62. BOIInstructionsForStickyFlagTag,
  63. SingleCharTag,
  64. BoundedWordTag,
  65. LeadingTrailingSpacesTag,
  66. OctoquadTag,
  67. BOILiteral2Tag
  68. };
  69. Field(ProgramTag) tag;
  70. struct Instructions
  71. {
  72. // Instruction array, in run-time allocator, owned by program, never null
  73. Field(uint8*) insts;
  74. Field(CharCount) instsLen; // in bytes
  75. // Literals
  76. // In run-time allocator, owned by program, may be 0
  77. Field(CharCount) litbufLen; // length of litbuf in char16's, no terminating null
  78. Field(Char*) litbuf;
  79. // These scanner infos are used by ScannersMixin, which is used by only SyncToLiteralsAndBackupInst. There will only
  80. // ever be only one of those instructions per program. Since scanners are large (> 1 KB), for that instruction they
  81. // are allocated on the recycler with pointers stored here to reference them.
  82. Field(Field(ScannerInfo *)*) scannersForSyncToLiterals;
  83. };
  84. struct SingleChar
  85. {
  86. Field(Char) c;
  87. Field(uint8) padding[sizeof(Instructions) - sizeof(Char)];
  88. };
  89. struct Octoquad
  90. {
  91. Field(OctoquadMatcher*) matcher;
  92. Field(uint8) padding[sizeof(Instructions) - sizeof(void*)];
  93. };
  94. struct BOILiteral2
  95. {
  96. Field(DWORD) literal;
  97. Field(uint8) padding[sizeof(Instructions) - sizeof(DWORD)];
  98. };
  99. struct LeadingTrailingSpaces
  100. {
  101. Field(CharCount) beginMinMatch;
  102. Field(CharCount) endMinMatch;
  103. Field(uint8) padding[sizeof(Instructions) - (sizeof(CharCount) * 2)];
  104. };
  105. struct Other
  106. {
  107. Field(uint8) padding[sizeof(Instructions)];
  108. };
  109. union RepType
  110. {
  111. Field(Instructions) insts;
  112. Field(SingleChar) singleChar;
  113. Field(Octoquad) octoquad;
  114. Field(BOILiteral2) boiLiteral2;
  115. Field(LeadingTrailingSpaces) leadingTrailingSpaces;
  116. Field(Other) other;
  117. RepType() {}
  118. };
  119. Field(RepType) rep;
  120. public:
  121. Program(RegexFlags flags);
  122. static Program *New(Recycler *recycler, RegexFlags flags);
  123. static size_t GetOffsetOfTag() { return offsetof(Program, tag); }
  124. static size_t GetOffsetOfRep() { return offsetof(Program, rep); }
  125. static size_t GetOffsetOfBOILiteral2Literal() { return offsetof(BOILiteral2, literal); }
  126. static ProgramTag GetBOILiteral2Tag() { return ProgramTag::BOILiteral2Tag; }
  127. Field(ScannerInfo *)*CreateScannerArrayForSyncToLiterals(Recycler *const recycler);
  128. ScannerInfo *AddScannerForSyncToLiterals(
  129. Recycler *const recycler,
  130. const int scannerIndex,
  131. const CharCount offset,
  132. const CharCount length,
  133. const bool isEquivClass);
  134. void FreeBody(ArenaAllocator* rtAllocator);
  135. inline CaseInsensitive::MappingSource GetCaseMappingSource() const
  136. {
  137. return (flags & UnicodeRegexFlag) != 0
  138. ? CaseInsensitive::MappingSource::CaseFolding
  139. : CaseInsensitive::MappingSource::UnicodeData;
  140. }
  141. #if ENABLE_REGEX_CONFIG_OPTIONS
  142. void Print(DebugWriter* w);
  143. #endif
  144. };
  145. class Matcher;
  146. // ----------------------------------------------------------------------
  147. // CountDomain
  148. // ----------------------------------------------------------------------
  149. struct CountDomain : private Chars<char16>
  150. {
  151. CharCount lower;
  152. CharCountOrFlag upper; // CharCountFlag => unbounded
  153. inline CountDomain() : lower(0), upper(CharCountFlag) {}
  154. inline CountDomain(CharCount exact) : lower(exact), upper(exact) {}
  155. inline CountDomain(CharCount lower, CharCountOrFlag upper) : lower(lower), upper(upper) {}
  156. inline void Exact(CharCount n)
  157. {
  158. lower = upper = n;
  159. }
  160. inline void Unknown()
  161. {
  162. lower = 0;
  163. upper = CharCountFlag;
  164. }
  165. inline void Lub(const CountDomain& other)
  166. {
  167. lower = min(lower, other.lower);
  168. upper = upper == CharCountFlag || other.upper == CharCountFlag ? CharCountFlag : max(upper, other.upper);
  169. }
  170. inline void Add(const CountDomain& other)
  171. {
  172. lower = lower + other.lower;
  173. upper = upper == CharCountFlag || other.upper == CharCountFlag ? CharCountFlag : upper + other.upper;
  174. }
  175. inline void Sub(const CountDomain& other)
  176. {
  177. lower = other.upper == CharCountFlag || other.upper > lower ? 0 : lower - other.upper;
  178. upper = upper == CharCountFlag ? CharCountFlag : (other.lower > upper ? 0 : upper - other.lower);
  179. }
  180. inline void Mult(const CountDomain& other)
  181. {
  182. if (lower != 0)
  183. {
  184. CharCount maxOther = MaxCharCount / lower;
  185. if (other.lower > maxOther)
  186. // Clip to maximum
  187. lower = MaxCharCount;
  188. else
  189. lower *= other.lower;
  190. }
  191. if (upper != 0 && upper != CharCountFlag)
  192. {
  193. if (other.upper == CharCountFlag)
  194. upper = CharCountFlag;
  195. else
  196. {
  197. CharCount maxOther = MaxCharCount / upper;
  198. if (other.upper > maxOther)
  199. // Clip to 'unbounded'
  200. upper = CharCountFlag;
  201. else
  202. upper *= other.upper;
  203. }
  204. }
  205. }
  206. inline bool CouldMatchEmpty() const
  207. {
  208. return lower == 0;
  209. }
  210. inline bool IsUnbounded() const
  211. {
  212. return upper == CharCountFlag;
  213. }
  214. inline bool IsFixed() const
  215. {
  216. return lower == upper;
  217. }
  218. inline bool IsExact(CharCount n) const
  219. {
  220. return lower == n && upper == n;
  221. }
  222. inline bool IsGreaterThan(const CountDomain& other) const
  223. {
  224. return other.upper != CharCountFlag && lower > other.upper;
  225. }
  226. inline bool IsLessThan(const CountDomain& other) const
  227. {
  228. return upper != CharCountFlag && upper < other.lower;
  229. }
  230. #if ENABLE_REGEX_CONFIG_OPTIONS
  231. void Print(DebugWriter* w) const;
  232. #endif
  233. };
  234. // ----------------------------------------------------------------------
  235. // Mix-in types
  236. // ----------------------------------------------------------------------
  237. #pragma pack(push, 1)
  238. // Contains information about how much to back up after syncing to a literal (for the SyncTo... instructions)
  239. struct BackupMixin
  240. {
  241. const CountDomain backup; // range of characters to backup, if upper is CharCountFlag then backup to existing matchStart
  242. inline BackupMixin(const CountDomain& backup) : backup(backup) {}
  243. #if ENABLE_REGEX_CONFIG_OPTIONS
  244. void Print(DebugWriter* w, const char16* litbuf) const;
  245. #endif
  246. };
  247. struct CharMixin
  248. {
  249. char16 c;
  250. inline CharMixin(char16 c) : c(c) {}
  251. #if ENABLE_REGEX_CONFIG_OPTIONS
  252. void Print(DebugWriter* w, const char16* litbuf) const;
  253. #endif
  254. };
  255. struct Char2Mixin
  256. {
  257. char16 cs[2];
  258. inline Char2Mixin(char16 c0, char16 c1) { cs[0] = c0; cs[1] = c1; }
  259. #if ENABLE_REGEX_CONFIG_OPTIONS
  260. void Print(DebugWriter* w, const char16* litbuf) const;
  261. #endif
  262. };
  263. struct Char3Mixin
  264. {
  265. char16 cs[3];
  266. inline Char3Mixin(char16 c0, char16 c1, char16 c2) { cs[0] = c0; cs[1] = c1; cs[2] = c2; }
  267. #if ENABLE_REGEX_CONFIG_OPTIONS
  268. void Print(DebugWriter* w, const char16* litbuf) const;
  269. #endif
  270. };
  271. struct Char4Mixin
  272. {
  273. char16 cs[4];
  274. inline Char4Mixin(char16 c0, char16 c1, char16 c2, char16 c3) { cs[0] = c0; cs[1] = c1; cs[2] = c2; cs[3] = c3; }
  275. #if ENABLE_REGEX_CONFIG_OPTIONS
  276. void Print(DebugWriter* w, const char16* litbuf) const;
  277. #endif
  278. };
  279. struct LiteralMixin
  280. {
  281. Field(CharCount) offset; // into program's literal buffer
  282. Field(CharCount) length; // in char16's
  283. inline LiteralMixin(CharCount offset, CharCount length) : offset(offset), length(length) {}
  284. #if ENABLE_REGEX_CONFIG_OPTIONS
  285. void Print(DebugWriter* w, const char16* litbuf, bool isEquivClass) const;
  286. #endif
  287. };
  288. template<bool IsNegation>
  289. struct SetMixin
  290. {
  291. RuntimeCharSet<char16> set; // contents always lives in run-time allocator
  292. // set must always be cloned from source
  293. void FreeBody(ArenaAllocator* rtAllocator);
  294. #if ENABLE_REGEX_CONFIG_OPTIONS
  295. void Print(DebugWriter* w, const char16* litbuf) const;
  296. #endif
  297. };
  298. struct TrieMixin
  299. {
  300. RuntimeCharTrie trie;
  301. // Trie must always be cloned
  302. #if ENABLE_REGEX_CONFIG_OPTIONS
  303. void Print(DebugWriter* w, const char16* litbuf) const;
  304. #endif
  305. };
  306. struct Char2LiteralScannerMixin : Char2Mixin
  307. {
  308. // scanner must be setup
  309. Char2LiteralScannerMixin(CharCount offset, CharCount length) : Char2Mixin(0, 0) { Assert(length == 2); }
  310. void Setup(char16 c0, char16 c1) { cs[0] = c0; cs[1] = c1; }
  311. CharCount GetLiteralLength() const { return 2; }
  312. bool Match(Matcher& matcher, const char16* const input, const CharCount inputLength, CharCount& inputOffset) const;
  313. #if ENABLE_REGEX_CONFIG_OPTIONS
  314. void Print(DebugWriter* w, const char16* litbuf) const;
  315. #endif
  316. };
  317. template <typename ScannerT>
  318. struct ScannerMixinT : LiteralMixin
  319. {
  320. Field(ScannerT) scanner;
  321. // scanner must be setup
  322. ScannerMixinT(CharCount offset, CharCount length) : LiteralMixin(offset, length) {}
  323. CharCount GetLiteralLength() const { return length; }
  324. bool Match(Matcher& matcher, const char16* const input, const CharCount inputLength, CharCount& inputOffset) const;
  325. void FreeBody(ArenaAllocator* rtAllocator);
  326. #if ENABLE_REGEX_CONFIG_OPTIONS
  327. void Print(DebugWriter* w, const char16* litbuf, bool isEquivClass = false) const;
  328. #endif
  329. };
  330. typedef ScannerMixinT<TextbookBoyerMoore<char16>> ScannerMixin;
  331. typedef ScannerMixinT<TextbookBoyerMooreWithLinearMap<char16>> ScannerMixin_WithLinearCharMap;
  332. template <uint lastPatCharEquivCLassSize>
  333. struct EquivScannerMixinT : ScannerMixin
  334. {
  335. // scanner must be setup
  336. EquivScannerMixinT(CharCount offset, CharCount length) : ScannerMixin(offset, length) {}
  337. bool Match(Matcher& matcher, const char16* const input, const CharCount inputLength, CharCount& inputOffset) const;
  338. #if ENABLE_REGEX_CONFIG_OPTIONS
  339. void Print(DebugWriter* w, const char16* litbuf) const;
  340. #endif
  341. };
  342. typedef EquivScannerMixinT<CaseInsensitive::EquivClassSize> EquivScannerMixin;
  343. typedef EquivScannerMixinT<1> EquivTrivialLastPatCharScannerMixin;
  344. struct ScannerInfo : ScannerMixin
  345. {
  346. Field(bool) isEquivClass;
  347. // scanner must be setup
  348. inline ScannerInfo(CharCount offset, CharCount length, bool isEquivClass) : ScannerMixin(offset, length), isEquivClass(isEquivClass) {}
  349. #if ENABLE_REGEX_CONFIG_OPTIONS
  350. void Print(DebugWriter* w, const char16* litbuf) const;
  351. #endif
  352. };
  353. struct ScannersMixin
  354. {
  355. static const int MaxNumSyncLiterals = 4;
  356. int numLiterals;
  357. Field(ScannerInfo*)* infos;
  358. // scanner mixins must be added
  359. inline ScannersMixin(Recycler *const recycler, Program *const program)
  360. : numLiterals(0), infos(program->CreateScannerArrayForSyncToLiterals(recycler))
  361. {
  362. }
  363. // Only used at compile time
  364. ScannerInfo* Add(Recycler *recycler, Program *program, CharCount offset, CharCount length, bool isEquivClass);
  365. void FreeBody(ArenaAllocator* rtAllocator);
  366. #if ENABLE_REGEX_CONFIG_OPTIONS
  367. void Print(DebugWriter* w, const char16* litbuf) const;
  368. #endif
  369. };
  370. struct GroupMixin
  371. {
  372. const int groupId;
  373. inline GroupMixin(int groupId) : groupId(groupId) {}
  374. #if ENABLE_REGEX_CONFIG_OPTIONS
  375. void Print(DebugWriter* w, const char16* litbuf) const;
  376. #endif
  377. };
  378. struct ChompBoundedMixin
  379. {
  380. const CountDomain repeats; // if upper is CharCountFlag, consume as many characters as possible
  381. inline ChompBoundedMixin(const CountDomain& repeats) : repeats(repeats) {}
  382. #if ENABLE_REGEX_CONFIG_OPTIONS
  383. void Print(DebugWriter* w, const char16* litbuf) const;
  384. #endif
  385. };
  386. struct JumpMixin
  387. {
  388. Label targetLabel;
  389. // targetLabel must always be fixed up
  390. inline JumpMixin()
  391. {
  392. #if DBG
  393. targetLabel = (Label)-1;
  394. #endif
  395. }
  396. #if ENABLE_REGEX_CONFIG_OPTIONS
  397. void Print(DebugWriter* w, const char16* litbuf) const;
  398. #endif
  399. };
  400. struct BodyGroupsMixin
  401. {
  402. int minBodyGroupId;
  403. int maxBodyGroupId;
  404. inline BodyGroupsMixin(int minBodyGroupId, int maxBodyGroupId) : minBodyGroupId(minBodyGroupId), maxBodyGroupId(maxBodyGroupId) {}
  405. #if ENABLE_REGEX_CONFIG_OPTIONS
  406. void Print(DebugWriter* w, const char16* litbuf) const;
  407. #endif
  408. };
  409. struct BeginLoopBasicsMixin
  410. {
  411. int loopId;
  412. const CountDomain repeats;
  413. bool hasOuterLoops;
  414. inline BeginLoopBasicsMixin(int loopId, const CountDomain& repeats, bool hasOuterLoops)
  415. : loopId(loopId), repeats(repeats), hasOuterLoops(hasOuterLoops)
  416. {}
  417. #if ENABLE_REGEX_CONFIG_OPTIONS
  418. void Print(DebugWriter* w, const char16* litbuf) const;
  419. #endif
  420. };
  421. struct BeginLoopMixin : BeginLoopBasicsMixin
  422. {
  423. bool hasInnerNondet;
  424. Label exitLabel;
  425. // exitLabel must always be fixed up
  426. inline BeginLoopMixin(int loopId, const CountDomain& repeats, bool hasOuterLoops, bool hasInnerNondet)
  427. : BeginLoopBasicsMixin(loopId, repeats, hasOuterLoops), hasInnerNondet(hasInnerNondet)
  428. {
  429. #if DBG
  430. exitLabel = (Label)-1;
  431. #endif
  432. }
  433. #if ENABLE_REGEX_CONFIG_OPTIONS
  434. void Print(DebugWriter* w, const char16* litbuf) const;
  435. #endif
  436. };
  437. struct GreedyMixin
  438. {
  439. bool isGreedy;
  440. inline GreedyMixin(bool isGreedy) : isGreedy(isGreedy) {}
  441. #if ENABLE_REGEX_CONFIG_OPTIONS
  442. void Print(DebugWriter* w, const char16* litbuf) const;
  443. #endif
  444. };
  445. struct RepeatLoopMixin
  446. {
  447. Label beginLabel; // label of the BeginLoopX instruction
  448. inline RepeatLoopMixin(Label beginLabel) : beginLabel(beginLabel) {}
  449. #if ENABLE_REGEX_CONFIG_OPTIONS
  450. void Print(DebugWriter* w, const char16* litbuf) const;
  451. #endif
  452. };
  453. struct GreedyLoopNoBacktrackMixin
  454. {
  455. int loopId;
  456. Label exitLabel;
  457. // exitLabel must always be fixed up
  458. inline GreedyLoopNoBacktrackMixin(int loopId) : loopId(loopId) {}
  459. #if ENABLE_REGEX_CONFIG_OPTIONS
  460. void Print(DebugWriter* w, const char16* litbuf) const;
  461. #endif
  462. };
  463. struct TryMixin
  464. {
  465. Label failLabel;
  466. // failLabel must always be fixed up
  467. inline TryMixin()
  468. {
  469. #if DBG
  470. failLabel = (Label)-1;
  471. #endif
  472. }
  473. #if ENABLE_REGEX_CONFIG_OPTIONS
  474. void Print(DebugWriter* w, const char16* litbuf) const;
  475. #endif
  476. };
  477. struct NegationMixin
  478. {
  479. bool isNegation;
  480. inline NegationMixin(bool isNegation) : isNegation(isNegation) {}
  481. #if ENABLE_REGEX_CONFIG_OPTIONS
  482. void Print(DebugWriter* w, const char16* litbuf) const;
  483. #endif
  484. };
  485. struct NextLabelMixin
  486. {
  487. Label nextLabel;
  488. // nextLabel must always be fixed up
  489. inline NextLabelMixin()
  490. {
  491. #if DBG
  492. nextLabel = (Label)-1;
  493. #endif
  494. }
  495. #if ENABLE_REGEX_CONFIG_OPTIONS
  496. void Print(DebugWriter* w, const char16* litbuf) const;
  497. #endif
  498. };
  499. struct FixedLengthMixin
  500. {
  501. CharCount length;
  502. inline FixedLengthMixin(CharCount length) : length(length) {}
  503. #if ENABLE_REGEX_CONFIG_OPTIONS
  504. void Print(DebugWriter* w, const char16* litbuf) const;
  505. #endif
  506. };
  507. struct FollowFirstMixin : private Chars<char16>
  508. {
  509. Char followFirst;
  510. inline FollowFirstMixin(Char followFirst) : followFirst(followFirst) {}
  511. #if ENABLE_REGEX_CONFIG_OPTIONS
  512. void Print(DebugWriter* w, const char16* litbuf) const;
  513. #endif
  514. };
  515. struct NoNeedToSaveMixin
  516. {
  517. bool noNeedToSave;
  518. inline NoNeedToSaveMixin(bool noNeedToSave) : noNeedToSave(noNeedToSave) {}
  519. #if ENABLE_REGEX_CONFIG_OPTIONS
  520. void Print(DebugWriter* w, const char16* litbuf) const;
  521. #endif
  522. };
  523. struct SwitchCase
  524. {
  525. char16 c;
  526. Label targetLabel;
  527. #if ENABLE_REGEX_CONFIG_OPTIONS
  528. void Print(DebugWriter* w) const;
  529. #endif
  530. };
  531. template <uint8 n>
  532. struct SwitchMixin
  533. {
  534. static constexpr uint8 MaxCases = n;
  535. uint8 numCases;
  536. // numCases cases, in increasing character order
  537. SwitchCase cases[MaxCases];
  538. // Cases must always be added
  539. inline SwitchMixin() : numCases(0)
  540. {
  541. #if DBG
  542. for (uint8 i = 0; i < MaxCases; i++)
  543. {
  544. cases[i].c = (char16)-1;
  545. cases[i].targetLabel = (Label)-1;
  546. }
  547. #endif
  548. }
  549. // Only used at compile time
  550. void AddCase(char16 c, Label targetLabel);
  551. #if ENABLE_REGEX_CONFIG_OPTIONS
  552. void Print(DebugWriter* w, const char16* litbuf) const;
  553. #endif
  554. };
  555. // ----------------------------------------------------------------------
  556. // Instructions
  557. // ----------------------------------------------------------------------
  558. // NOTE: #pragma pack(1) applies to all Inst structs as well as all Mixin structs (see above).
  559. struct Inst : protected Chars<char16>
  560. {
  561. enum class InstTag : uint8
  562. {
  563. #define M(TagName) TagName,
  564. #define MTemplate(TagName, ...) M(TagName)
  565. #include "RegexOpCodes.h"
  566. #undef M
  567. #undef MTemplate
  568. };
  569. Field(InstTag) tag;
  570. inline Inst(InstTag tag) : tag(tag) {}
  571. void FreeBody(ArenaAllocator* rtAllocator) {}
  572. #if ENABLE_REGEX_CONFIG_OPTIONS
  573. static bool IsBaselineMode();
  574. static Label GetPrintLabel(Label label);
  575. template <typename T>
  576. void PrintBytes(DebugWriter *w, Inst *inst, T *that, const char16 *annotation) const;
  577. #endif
  578. };
  579. #define INST_BODY_FREE(T) \
  580. void FreeBody(ArenaAllocator* rtAllocator) \
  581. { \
  582. T::FreeBody(rtAllocator); \
  583. Inst::FreeBody(rtAllocator); \
  584. }
  585. #if ENABLE_REGEX_CONFIG_OPTIONS
  586. #define INST_BODY_PRINT int Print(DebugWriter*w, Label label, const Char* litbuf) const;
  587. #else
  588. #define INST_BODY_PRINT
  589. #endif
  590. #define REGEX_INST_EXEC_PARAMETERS Matcher& matcher, const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount& inputOffset, CharCount &nextSyncInputOffset, const uint8*& instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks, bool firstIteration
  591. #define INST_BODY bool Exec(REGEX_INST_EXEC_PARAMETERS) const; \
  592. INST_BODY_PRINT
  593. //
  594. // Control flow
  595. //
  596. struct NopInst : Inst
  597. {
  598. inline NopInst() : Inst(InstTag::Nop) {}
  599. INST_BODY
  600. };
  601. struct FailInst : Inst
  602. {
  603. inline FailInst() : Inst(InstTag::Fail) {}
  604. INST_BODY
  605. };
  606. struct SuccInst : Inst
  607. {
  608. inline SuccInst() : Inst(InstTag::Succ) {}
  609. INST_BODY
  610. };
  611. struct JumpInst : Inst, JumpMixin
  612. {
  613. // targetLabel must always be fixed up
  614. inline JumpInst() : Inst(InstTag::Jump), JumpMixin() {}
  615. INST_BODY
  616. };
  617. struct JumpIfNotCharInst : Inst, CharMixin, JumpMixin
  618. {
  619. // targetLabel must always be fixed up
  620. inline JumpIfNotCharInst(Char c) : Inst(InstTag::JumpIfNotChar), CharMixin(c), JumpMixin() {}
  621. INST_BODY
  622. };
  623. struct MatchCharOrJumpInst : Inst, CharMixin, JumpMixin
  624. {
  625. // targetLabel must always be fixed up
  626. inline MatchCharOrJumpInst(Char c) : Inst(InstTag::MatchCharOrJump), CharMixin(c), JumpMixin() {}
  627. INST_BODY
  628. };
  629. struct JumpIfNotSetInst : Inst, SetMixin<false>, JumpMixin
  630. {
  631. // set must always be cloned from source
  632. // targetLabel must always be fixed up
  633. inline JumpIfNotSetInst() : Inst(InstTag::JumpIfNotSet), JumpMixin() {}
  634. INST_BODY
  635. INST_BODY_FREE(SetMixin<false>)
  636. };
  637. struct MatchSetOrJumpInst : Inst, SetMixin<false>, JumpMixin
  638. {
  639. // set must always be cloned from source
  640. // targetLabel must always be fixed up
  641. inline MatchSetOrJumpInst() : Inst(InstTag::MatchSetOrJump), JumpMixin() {}
  642. INST_BODY
  643. INST_BODY_FREE(SetMixin<false>)
  644. };
  645. #define SwitchInstActual(n) \
  646. struct Switch##n##Inst : Inst, SwitchMixin<n> \
  647. { \
  648. inline Switch##n##Inst() : Inst(InstTag::Switch##n), SwitchMixin() {} \
  649. INST_BODY \
  650. };
  651. SwitchInstActual(2);
  652. SwitchInstActual(4);
  653. SwitchInstActual(8);
  654. SwitchInstActual(16);
  655. SwitchInstActual(24);
  656. #undef SwitchInstActual
  657. #define SwitchAndConsumeInstActual(n) \
  658. struct SwitchAndConsume##n##Inst : Inst, SwitchMixin<n> \
  659. { \
  660. inline SwitchAndConsume##n##Inst() : Inst(InstTag::SwitchAndConsume##n), SwitchMixin() {} \
  661. INST_BODY \
  662. };
  663. SwitchAndConsumeInstActual(2);
  664. SwitchAndConsumeInstActual(4);
  665. SwitchAndConsumeInstActual(8);
  666. SwitchAndConsumeInstActual(16);
  667. SwitchAndConsumeInstActual(24);
  668. #undef SwitchAndConsumeInstActual
  669. //
  670. // Built-in assertions
  671. //
  672. // BOI = Beginning of Input
  673. template <bool canHardFail>
  674. struct BOITestInst : Inst
  675. {
  676. BOITestInst();
  677. INST_BODY
  678. };
  679. // EOI = End of Input
  680. template <bool canHardFail>
  681. struct EOITestInst : Inst
  682. {
  683. EOITestInst();
  684. INST_BODY
  685. };
  686. // BOL = Beginning of Line (/^.../)
  687. struct BOLTestInst : Inst
  688. {
  689. inline BOLTestInst() : Inst(InstTag::BOLTest) {}
  690. INST_BODY
  691. };
  692. // EOL = End of Line (/...$/)
  693. struct EOLTestInst : Inst
  694. {
  695. inline EOLTestInst() : Inst(InstTag::EOLTest) {}
  696. INST_BODY
  697. };
  698. template <bool isNegation>
  699. struct WordBoundaryTestInst : Inst
  700. {
  701. WordBoundaryTestInst();
  702. INST_BODY
  703. };
  704. //
  705. // Matching
  706. //
  707. struct MatchCharInst : Inst, CharMixin
  708. {
  709. inline MatchCharInst(Char c) : Inst(InstTag::MatchChar), CharMixin(c) {}
  710. INST_BODY
  711. };
  712. struct MatchChar2Inst : Inst, Char2Mixin
  713. {
  714. inline MatchChar2Inst(Char c0, Char c1) : Inst(InstTag::MatchChar2), Char2Mixin(c0, c1) {}
  715. INST_BODY
  716. };
  717. struct MatchChar3Inst : Inst, Char3Mixin
  718. {
  719. inline MatchChar3Inst(Char c0, Char c1, Char c2) : Inst(InstTag::MatchChar3), Char3Mixin(c0, c1, c2) {}
  720. INST_BODY
  721. };
  722. struct MatchChar4Inst : Inst, Char4Mixin
  723. {
  724. inline MatchChar4Inst(Char c0, Char c1, Char c2, Char c3) : Inst(InstTag::MatchChar4), Char4Mixin(c0, c1, c2, c3) {}
  725. INST_BODY
  726. };
  727. template<bool IsNegation>
  728. struct MatchSetInst : Inst, SetMixin<IsNegation>
  729. {
  730. // set must always be cloned from source
  731. inline MatchSetInst() : Inst(IsNegation ? InstTag::MatchNegatedSet : InstTag::MatchSet) {}
  732. INST_BODY
  733. INST_BODY_FREE(SetMixin<IsNegation>)
  734. };
  735. struct MatchLiteralInst : Inst, LiteralMixin
  736. {
  737. inline MatchLiteralInst(CharCount offset, CharCount length) : Inst(InstTag::MatchLiteral), LiteralMixin(offset, length) {}
  738. INST_BODY
  739. };
  740. struct MatchLiteralEquivInst : Inst, LiteralMixin
  741. {
  742. inline MatchLiteralEquivInst(CharCount offset, CharCount length) : Inst(InstTag::MatchLiteralEquiv), LiteralMixin(offset, length) {}
  743. INST_BODY
  744. };
  745. struct MatchTrieInst : Inst, TrieMixin
  746. {
  747. // Trie must always be cloned
  748. inline MatchTrieInst() : Inst(InstTag::MatchTrie) {}
  749. void FreeBody(ArenaAllocator* rtAllocator);
  750. INST_BODY
  751. };
  752. struct OptMatchCharInst : Inst, CharMixin
  753. {
  754. inline OptMatchCharInst(Char c) : Inst(InstTag::OptMatchChar), CharMixin(c) {}
  755. INST_BODY
  756. };
  757. struct OptMatchSetInst : Inst, SetMixin<false>
  758. {
  759. // set must always be cloned from source
  760. inline OptMatchSetInst() : Inst(InstTag::OptMatchSet) {}
  761. INST_BODY
  762. INST_BODY_FREE(SetMixin<false>)
  763. };
  764. //
  765. // Synchronization:
  766. // SyncTo(Char|Char2Set|Set|Char2Literal|Literal|LiteralEquiv|Literals)And(Consume|Continue|Backup)
  767. //
  768. struct SyncToCharAndContinueInst : Inst, CharMixin
  769. {
  770. inline SyncToCharAndContinueInst(Char c) : Inst(InstTag::SyncToCharAndContinue), CharMixin(c) {}
  771. INST_BODY
  772. };
  773. struct SyncToChar2SetAndContinueInst : Inst, Char2Mixin
  774. {
  775. inline SyncToChar2SetAndContinueInst(Char c0, Char c1) : Inst(InstTag::SyncToChar2SetAndContinue), Char2Mixin(c0, c1) {}
  776. INST_BODY
  777. };
  778. template<bool IsNegation>
  779. struct SyncToSetAndContinueInst : Inst, SetMixin<IsNegation>
  780. {
  781. // set must always be cloned from source
  782. inline SyncToSetAndContinueInst() : Inst(IsNegation ? InstTag::SyncToNegatedSetAndContinue : InstTag::SyncToSetAndContinue) {}
  783. INST_BODY
  784. INST_BODY_FREE(SetMixin<IsNegation>)
  785. };
  786. template <typename ScannerT>
  787. struct SyncToLiteralAndContinueInstT : Inst, ScannerT
  788. {
  789. SyncToLiteralAndContinueInstT(InstTag tag, CharCount offset, CharCount length) : Inst(tag), ScannerT(offset, length) {}
  790. INST_BODY
  791. };
  792. // Specialized version of the SyncToLiteralAndContinueInst for a length 2 literal
  793. struct SyncToChar2LiteralAndContinueInst : SyncToLiteralAndContinueInstT<Char2LiteralScannerMixin>
  794. {
  795. SyncToChar2LiteralAndContinueInst(Char c0, Char c1) :
  796. SyncToLiteralAndContinueInstT(InstTag::SyncToChar2LiteralAndContinue, 0, 2) { Char2LiteralScannerMixin::Setup(c0, c1); }
  797. };
  798. struct SyncToLiteralAndContinueInst : SyncToLiteralAndContinueInstT<ScannerMixin>
  799. {
  800. // scanner must be setup
  801. SyncToLiteralAndContinueInst(CharCount offset, CharCount length) :
  802. SyncToLiteralAndContinueInstT(InstTag::SyncToLiteralAndContinue, offset, length) {}
  803. INST_BODY_FREE(ScannerMixin)
  804. };
  805. struct SyncToLinearLiteralAndContinueInst : SyncToLiteralAndContinueInstT<ScannerMixin_WithLinearCharMap>
  806. {
  807. // scanner must be setup
  808. SyncToLinearLiteralAndContinueInst(CharCount offset, CharCount length) :
  809. SyncToLiteralAndContinueInstT(InstTag::SyncToLinearLiteralAndContinue, offset, length) {}
  810. INST_BODY_FREE(ScannerMixin_WithLinearCharMap)
  811. };
  812. struct SyncToLiteralEquivAndContinueInst : SyncToLiteralAndContinueInstT<EquivScannerMixin>
  813. {
  814. // scanner must be setup
  815. SyncToLiteralEquivAndContinueInst(CharCount offset, CharCount length) :
  816. SyncToLiteralAndContinueInstT(InstTag::SyncToLiteralEquivAndContinue, offset, length) {}
  817. INST_BODY_FREE(EquivScannerMixin)
  818. };
  819. struct SyncToLiteralEquivTrivialLastPatCharAndContinueInst : SyncToLiteralAndContinueInstT<EquivTrivialLastPatCharScannerMixin>
  820. {
  821. // scanner must be setup
  822. SyncToLiteralEquivTrivialLastPatCharAndContinueInst(CharCount offset, CharCount length) :
  823. SyncToLiteralAndContinueInstT(InstTag::SyncToLiteralEquivTrivialLastPatCharAndContinue, offset, length) {}
  824. INST_BODY_FREE(EquivTrivialLastPatCharScannerMixin)
  825. };
  826. struct SyncToCharAndConsumeInst : Inst, CharMixin
  827. {
  828. inline SyncToCharAndConsumeInst(Char c) : Inst(InstTag::SyncToCharAndConsume), CharMixin(c) {}
  829. INST_BODY
  830. };
  831. struct SyncToChar2SetAndConsumeInst : Inst, Char2Mixin
  832. {
  833. inline SyncToChar2SetAndConsumeInst(Char c0, Char c1) : Inst(InstTag::SyncToChar2SetAndConsume), Char2Mixin(c0, c1) {}
  834. INST_BODY
  835. };
  836. template<bool IsNegation>
  837. struct SyncToSetAndConsumeInst : Inst, SetMixin<IsNegation>
  838. {
  839. // set must always be cloned from source
  840. inline SyncToSetAndConsumeInst() : Inst(IsNegation ? InstTag::SyncToNegatedSetAndConsume : InstTag::SyncToSetAndConsume) {}
  841. INST_BODY
  842. INST_BODY_FREE(SetMixin<IsNegation>)
  843. };
  844. template <typename ScannerT>
  845. struct SyncToLiteralAndConsumeInstT : Inst, ScannerT
  846. {
  847. SyncToLiteralAndConsumeInstT(InstTag tag, CharCount offset, CharCount length) : Inst(tag), ScannerT(offset, length) {}
  848. INST_BODY
  849. };
  850. // Specialized version of the SyncToLiteralAndConsumeInst for a length 2 literal
  851. struct SyncToChar2LiteralAndConsumeInst : SyncToLiteralAndConsumeInstT<Char2LiteralScannerMixin>
  852. {
  853. SyncToChar2LiteralAndConsumeInst(Char c0, Char c1) :
  854. SyncToLiteralAndConsumeInstT(InstTag::SyncToChar2LiteralAndConsume, 0, 2) { Char2LiteralScannerMixin::Setup(c0, c1); }
  855. };
  856. struct SyncToLiteralAndConsumeInst : SyncToLiteralAndConsumeInstT<ScannerMixin>
  857. {
  858. // scanner must be setup
  859. SyncToLiteralAndConsumeInst(CharCount offset, CharCount length) :
  860. SyncToLiteralAndConsumeInstT(InstTag::SyncToLiteralAndConsume, offset, length) {}
  861. INST_BODY_FREE(ScannerMixin)
  862. };
  863. struct SyncToLinearLiteralAndConsumeInst : SyncToLiteralAndConsumeInstT<ScannerMixin_WithLinearCharMap>
  864. {
  865. // scanner must be setup
  866. SyncToLinearLiteralAndConsumeInst(CharCount offset, CharCount length) :
  867. SyncToLiteralAndConsumeInstT(InstTag::SyncToLinearLiteralAndConsume, offset, length) {}
  868. INST_BODY_FREE(ScannerMixin_WithLinearCharMap)
  869. };
  870. struct SyncToLiteralEquivAndConsumeInst : SyncToLiteralAndConsumeInstT<EquivScannerMixin>
  871. {
  872. // scanner must be setup
  873. SyncToLiteralEquivAndConsumeInst(CharCount offset, CharCount length) :
  874. SyncToLiteralAndConsumeInstT(InstTag::SyncToLiteralEquivAndConsume,offset, length) {}
  875. INST_BODY_FREE(EquivScannerMixin)
  876. };
  877. struct SyncToLiteralEquivTrivialLastPatCharAndConsumeInst : SyncToLiteralAndConsumeInstT<EquivTrivialLastPatCharScannerMixin>
  878. {
  879. // scanner must be setup
  880. SyncToLiteralEquivTrivialLastPatCharAndConsumeInst(CharCount offset, CharCount length) :
  881. SyncToLiteralAndConsumeInstT(InstTag::SyncToLiteralEquivTrivialLastPatCharAndConsume, offset, length) {}
  882. INST_BODY_FREE(EquivTrivialLastPatCharScannerMixin)
  883. };
  884. struct SyncToCharAndBackupInst : Inst, CharMixin, BackupMixin
  885. {
  886. inline SyncToCharAndBackupInst(Char c, const CountDomain& backup) : Inst(InstTag::SyncToCharAndBackup), CharMixin(c), BackupMixin(backup) {}
  887. INST_BODY
  888. };
  889. template<bool IsNegation>
  890. struct SyncToSetAndBackupInst : Inst, SetMixin<IsNegation>, BackupMixin
  891. {
  892. // set must always be cloned from source
  893. inline SyncToSetAndBackupInst(const CountDomain& backup) : Inst(IsNegation ? InstTag::SyncToNegatedSetAndBackup : InstTag::SyncToSetAndBackup), BackupMixin(backup) {}
  894. INST_BODY
  895. INST_BODY_FREE(SetMixin<IsNegation>)
  896. };
  897. template <typename ScannerT>
  898. struct SyncToLiteralAndBackupInstT : Inst, ScannerT, BackupMixin
  899. {
  900. SyncToLiteralAndBackupInstT(InstTag tag, CharCount offset, CharCount length, const CountDomain& backup) : Inst(tag), ScannerT(offset, length), BackupMixin(backup) {}
  901. INST_BODY
  902. };
  903. // Specialized version of the SyncToLiteralAndConsumeInst for a length 2 literal
  904. struct SyncToChar2LiteralAndBackupInst : SyncToLiteralAndBackupInstT<Char2LiteralScannerMixin>
  905. {
  906. SyncToChar2LiteralAndBackupInst(Char c0, Char c1, const CountDomain& backup) :
  907. SyncToLiteralAndBackupInstT(InstTag::SyncToChar2LiteralAndBackup, 0, 2, backup) { Char2LiteralScannerMixin::Setup(c0, c1); }
  908. };
  909. struct SyncToLiteralAndBackupInst : SyncToLiteralAndBackupInstT<ScannerMixin>
  910. {
  911. // scanner must be setup
  912. SyncToLiteralAndBackupInst(CharCount offset, CharCount length, const CountDomain& backup) :
  913. SyncToLiteralAndBackupInstT(InstTag::SyncToLiteralAndBackup, offset, length, backup) {}
  914. INST_BODY_FREE(ScannerMixin)
  915. };
  916. struct SyncToLinearLiteralAndBackupInst : SyncToLiteralAndBackupInstT<ScannerMixin_WithLinearCharMap>
  917. {
  918. // scanner must be setup
  919. SyncToLinearLiteralAndBackupInst(CharCount offset, CharCount length, const CountDomain& backup) :
  920. SyncToLiteralAndBackupInstT(InstTag::SyncToLinearLiteralAndBackup, offset, length, backup) {}
  921. INST_BODY_FREE(ScannerMixin_WithLinearCharMap)
  922. };
  923. struct SyncToLiteralEquivAndBackupInst : SyncToLiteralAndBackupInstT<EquivScannerMixin>
  924. {
  925. // scanner must be setup
  926. SyncToLiteralEquivAndBackupInst(CharCount offset, CharCount length, const CountDomain& backup) :
  927. SyncToLiteralAndBackupInstT(InstTag::SyncToLiteralEquivAndBackup, offset, length, backup) {}
  928. INST_BODY_FREE(EquivScannerMixin)
  929. };
  930. struct SyncToLiteralEquivTrivialLastPatCharAndBackupInst : SyncToLiteralAndBackupInstT<EquivTrivialLastPatCharScannerMixin>
  931. {
  932. // scanner must be setup
  933. SyncToLiteralEquivTrivialLastPatCharAndBackupInst(CharCount offset, CharCount length, const CountDomain& backup) :
  934. SyncToLiteralAndBackupInstT(InstTag::SyncToLiteralEquivTrivialLastPatCharAndBackup, offset, length, backup) {}
  935. INST_BODY_FREE(EquivTrivialLastPatCharScannerMixin)
  936. };
  937. struct SyncToLiteralsAndBackupInst : Inst, ScannersMixin, BackupMixin
  938. {
  939. // scanner mixins must be setup
  940. inline SyncToLiteralsAndBackupInst(Recycler *recycler, Program *program, const CountDomain& backup)
  941. : Inst(InstTag::SyncToLiteralsAndBackup), ScannersMixin(recycler, program), BackupMixin(backup)
  942. {
  943. }
  944. INST_BODY
  945. INST_BODY_FREE(ScannersMixin)
  946. };
  947. //
  948. // Groups
  949. //
  950. struct MatchGroupInst : Inst, GroupMixin
  951. {
  952. inline MatchGroupInst(int groupId) : Inst(InstTag::MatchGroup), GroupMixin(groupId) {}
  953. INST_BODY
  954. };
  955. struct BeginDefineGroupInst : Inst, GroupMixin
  956. {
  957. inline BeginDefineGroupInst(int groupId) : Inst(InstTag::BeginDefineGroup), GroupMixin(groupId) {}
  958. INST_BODY
  959. };
  960. struct EndDefineGroupInst : Inst, GroupMixin, NoNeedToSaveMixin
  961. {
  962. inline EndDefineGroupInst(int groupId, bool noNeedToSave)
  963. : Inst(InstTag::EndDefineGroup), GroupMixin(groupId), NoNeedToSaveMixin(noNeedToSave)
  964. {
  965. }
  966. INST_BODY
  967. };
  968. struct DefineGroupFixedInst : Inst, GroupMixin, FixedLengthMixin, NoNeedToSaveMixin
  969. {
  970. inline DefineGroupFixedInst(int groupId, CharCount length, bool noNeedToSave) : Inst(InstTag::DefineGroupFixed), GroupMixin(groupId), FixedLengthMixin(length), NoNeedToSaveMixin(noNeedToSave) {}
  971. INST_BODY
  972. };
  973. //
  974. // Loops
  975. //
  976. struct BeginLoopInst : Inst, BeginLoopMixin, BodyGroupsMixin, GreedyMixin
  977. {
  978. // exitLabel must always be fixed up
  979. inline BeginLoopInst(int loopId, const CountDomain& repeats, bool hasOuterLoops, bool hasInnerNondet, int minBodyGroupId, int maxBodyGroupId, bool isGreedy)
  980. : Inst(InstTag::BeginLoop), BeginLoopMixin(loopId, repeats, hasOuterLoops, hasInnerNondet), BodyGroupsMixin(minBodyGroupId, maxBodyGroupId), GreedyMixin(isGreedy)
  981. {}
  982. INST_BODY
  983. };
  984. struct RepeatLoopInst : Inst, RepeatLoopMixin
  985. {
  986. inline RepeatLoopInst(Label beginLabel) : Inst(InstTag::RepeatLoop), RepeatLoopMixin(beginLabel) {}
  987. INST_BODY
  988. };
  989. struct BeginLoopIfCharInst : Inst, CharMixin, BeginLoopMixin, BodyGroupsMixin
  990. {
  991. // exitLabel must always be fixed up
  992. inline BeginLoopIfCharInst(Char c, int loopId, const CountDomain& repeats, bool hasOuterLoops, bool hasInnerNondet, int minBodyGroupId, int maxBodyGroupId)
  993. : Inst(InstTag::BeginLoopIfChar), CharMixin(c), BeginLoopMixin(loopId, repeats, hasOuterLoops, hasInnerNondet), BodyGroupsMixin(minBodyGroupId, maxBodyGroupId) {}
  994. INST_BODY
  995. };
  996. struct BeginLoopIfSetInst : Inst, SetMixin<false>, BeginLoopMixin, BodyGroupsMixin
  997. {
  998. // set must always be cloned from source
  999. // exitLabel must always be fixed up
  1000. inline BeginLoopIfSetInst(int loopId, const CountDomain& repeats, bool hasOuterLoops, bool hasInnerNondet, int minBodyGroupId, int maxBodyGroupId)
  1001. : Inst(InstTag::BeginLoopIfSet), BeginLoopMixin(loopId, repeats, hasOuterLoops, hasInnerNondet), BodyGroupsMixin(minBodyGroupId, maxBodyGroupId) {}
  1002. INST_BODY
  1003. INST_BODY_FREE(SetMixin)
  1004. };
  1005. struct RepeatLoopIfCharInst : Inst, RepeatLoopMixin
  1006. {
  1007. inline RepeatLoopIfCharInst(Label beginLabel) : Inst(InstTag::RepeatLoopIfChar), RepeatLoopMixin(beginLabel) {}
  1008. INST_BODY
  1009. };
  1010. struct RepeatLoopIfSetInst : Inst, RepeatLoopMixin
  1011. {
  1012. inline RepeatLoopIfSetInst(Label beginLabel) : Inst(InstTag::RepeatLoopIfSet), RepeatLoopMixin(beginLabel) {}
  1013. INST_BODY
  1014. };
  1015. // Loop is greedy, fixed width, deterministic body, no inner groups
  1016. struct BeginLoopFixedInst : Inst, BeginLoopMixin, FixedLengthMixin
  1017. {
  1018. // exitLabel must always be fixed up
  1019. inline BeginLoopFixedInst(int loopId, const CountDomain& repeats, bool hasOuterLoops, CharCount length)
  1020. : Inst(InstTag::BeginLoopFixed), BeginLoopMixin(loopId, repeats, hasOuterLoops, false), FixedLengthMixin(length) {}
  1021. INST_BODY
  1022. };
  1023. struct RepeatLoopFixedInst : Inst, RepeatLoopMixin
  1024. {
  1025. inline RepeatLoopFixedInst(Label beginLabel) : Inst(InstTag::RepeatLoopFixed), RepeatLoopMixin(beginLabel) {}
  1026. INST_BODY
  1027. };
  1028. // Loop is greedy, contains a MatchSet only
  1029. struct LoopSetInst : Inst, SetMixin<false>, BeginLoopBasicsMixin
  1030. {
  1031. // set must always be cloned from source
  1032. inline LoopSetInst(int loopId, const CountDomain& repeats, bool hasOuterLoops)
  1033. : Inst(InstTag::LoopSet), BeginLoopBasicsMixin(loopId, repeats, hasOuterLoops) {}
  1034. inline LoopSetInst(InstTag tag, int loopId, const CountDomain& repeats, bool hasOuterLoops)
  1035. : Inst(tag), BeginLoopBasicsMixin(loopId, repeats, hasOuterLoops) {}
  1036. INST_BODY
  1037. INST_BODY_FREE(SetMixin)
  1038. };
  1039. // Loop is greedy, contains a MatchSet only, first character in its follow set is known
  1040. struct LoopSetWithFollowFirstInst : LoopSetInst, FollowFirstMixin
  1041. {
  1042. inline LoopSetWithFollowFirstInst(int loopId, const CountDomain& repeats, bool hasOuterLoops, Char followFirst)
  1043. : LoopSetInst(InstTag::LoopSetWithFollowFirst, loopId, repeats, hasOuterLoops), FollowFirstMixin(followFirst) {}
  1044. INST_BODY
  1045. };
  1046. // Loop is greedy, fixed width, deterministic body, one outermost group
  1047. struct BeginLoopFixedGroupLastIterationInst : Inst, BeginLoopMixin, FixedLengthMixin, GroupMixin, NoNeedToSaveMixin
  1048. {
  1049. // exitLabel must always be fixed up
  1050. inline BeginLoopFixedGroupLastIterationInst(int loopId, const CountDomain& repeats, bool hasOuterLoops, CharCount length, int groupId, bool noNeedToSave)
  1051. : Inst(InstTag::BeginLoopFixedGroupLastIteration), BeginLoopMixin(loopId, repeats, hasOuterLoops, false), FixedLengthMixin(length), GroupMixin(groupId), NoNeedToSaveMixin(noNeedToSave) {}
  1052. INST_BODY
  1053. };
  1054. struct RepeatLoopFixedGroupLastIterationInst : Inst, RepeatLoopMixin
  1055. {
  1056. inline RepeatLoopFixedGroupLastIterationInst(Label beginLabel) : Inst(InstTag::RepeatLoopFixedGroupLastIteration), RepeatLoopMixin(beginLabel) {}
  1057. INST_BODY
  1058. };
  1059. // Loop is greedy, deterministic body, lower == 0, upper == inf, follow is irrefutable, no inner groups
  1060. struct BeginGreedyLoopNoBacktrackInst : Inst, GreedyLoopNoBacktrackMixin
  1061. {
  1062. // exitLabel must always be fixed up
  1063. inline BeginGreedyLoopNoBacktrackInst(int loopId) : Inst(InstTag::BeginGreedyLoopNoBacktrack), GreedyLoopNoBacktrackMixin(loopId) {}
  1064. INST_BODY
  1065. };
  1066. struct RepeatGreedyLoopNoBacktrackInst : Inst, RepeatLoopMixin
  1067. {
  1068. inline RepeatGreedyLoopNoBacktrackInst(Label beginLabel) : Inst(InstTag::RepeatGreedyLoopNoBacktrack), RepeatLoopMixin(beginLabel) {}
  1069. INST_BODY
  1070. };
  1071. template<ChompMode Mode>
  1072. struct ChompCharInst : Inst, CharMixin
  1073. {
  1074. ChompCharInst(const Char c) : Inst(Mode == ChompMode::Star ? InstTag::ChompCharStar : InstTag::ChompCharPlus), CharMixin(c) {}
  1075. INST_BODY
  1076. };
  1077. template<ChompMode Mode>
  1078. struct ChompSetInst : Inst, SetMixin<false>
  1079. {
  1080. // set must always be cloned from source
  1081. ChompSetInst() : Inst(Mode == ChompMode::Star ? InstTag::ChompSetStar : InstTag::ChompSetPlus) {}
  1082. INST_BODY
  1083. INST_BODY_FREE(SetMixin)
  1084. };
  1085. template<ChompMode Mode>
  1086. struct ChompCharGroupInst : Inst, CharMixin, GroupMixin, NoNeedToSaveMixin
  1087. {
  1088. ChompCharGroupInst(const Char c, const int groupId, const bool noNeedToSave)
  1089. : Inst(Mode == ChompMode::Star ? InstTag::ChompCharGroupStar : InstTag::ChompCharGroupPlus),
  1090. CharMixin(c),
  1091. GroupMixin(groupId),
  1092. NoNeedToSaveMixin(noNeedToSave)
  1093. {
  1094. }
  1095. INST_BODY
  1096. };
  1097. template<ChompMode Mode>
  1098. struct ChompSetGroupInst : Inst, SetMixin<false>, GroupMixin, NoNeedToSaveMixin
  1099. {
  1100. // set must always be cloned from source
  1101. ChompSetGroupInst(const int groupId, const bool noNeedToSave)
  1102. : Inst(Mode == ChompMode::Star ? InstTag::ChompSetGroupStar : InstTag::ChompSetGroupPlus),
  1103. GroupMixin(groupId),
  1104. NoNeedToSaveMixin(noNeedToSave)
  1105. {
  1106. }
  1107. INST_BODY
  1108. INST_BODY_FREE(SetMixin)
  1109. };
  1110. struct ChompCharBoundedInst : Inst, CharMixin, ChompBoundedMixin
  1111. {
  1112. inline ChompCharBoundedInst(Char c, const CountDomain& repeats) : Inst(InstTag::ChompCharBounded), CharMixin(c), ChompBoundedMixin(repeats) {}
  1113. INST_BODY
  1114. };
  1115. struct ChompSetBoundedInst : Inst, SetMixin<false>, ChompBoundedMixin
  1116. {
  1117. // set must always be cloned from source
  1118. inline ChompSetBoundedInst(const CountDomain& repeats) : Inst(InstTag::ChompSetBounded), ChompBoundedMixin(repeats) {}
  1119. INST_BODY
  1120. INST_BODY_FREE(SetMixin)
  1121. };
  1122. struct ChompSetBoundedGroupLastCharInst : Inst, SetMixin<false>, ChompBoundedMixin, GroupMixin, NoNeedToSaveMixin
  1123. {
  1124. // set must always be cloned from source
  1125. inline ChompSetBoundedGroupLastCharInst(const CountDomain& repeats, int groupId, bool noNeedToSave) : Inst(InstTag::ChompSetBoundedGroupLastChar), ChompBoundedMixin(repeats), GroupMixin(groupId), NoNeedToSaveMixin(noNeedToSave) {}
  1126. INST_BODY
  1127. INST_BODY_FREE(SetMixin)
  1128. };
  1129. //
  1130. // Choicepoints
  1131. //
  1132. struct TryInst : Inst, TryMixin
  1133. {
  1134. // failLabel must always be fixed up
  1135. inline TryInst() : Inst(InstTag::Try), TryMixin() {}
  1136. INST_BODY
  1137. };
  1138. struct TryIfCharInst : Inst, CharMixin, TryMixin
  1139. {
  1140. // failLabel must always be fixed up
  1141. inline TryIfCharInst(Char c) : Inst(InstTag::TryIfChar), CharMixin(c), TryMixin() {}
  1142. INST_BODY
  1143. };
  1144. struct TryMatchCharInst : Inst, CharMixin, TryMixin
  1145. {
  1146. // failLabel must always be fixed up
  1147. inline TryMatchCharInst(Char c) : Inst(InstTag::TryMatchChar), CharMixin(c), TryMixin() {}
  1148. INST_BODY
  1149. };
  1150. struct TryIfSetInst : Inst, SetMixin<false>, TryMixin
  1151. {
  1152. // set is always same as matching BeginLoopIfSetInst set
  1153. // failLabel must always be fixed up
  1154. inline TryIfSetInst() : Inst(InstTag::TryIfSet), TryMixin() {}
  1155. INST_BODY
  1156. INST_BODY_FREE(SetMixin)
  1157. };
  1158. struct TryMatchSetInst : Inst, SetMixin<false>, TryMixin
  1159. {
  1160. // set is always same as matching BeginLoopIfSetInst set
  1161. // failLabel must always be fixed up
  1162. inline TryMatchSetInst() : Inst(InstTag::TryMatchSet), TryMixin() {}
  1163. INST_BODY
  1164. INST_BODY_FREE(SetMixin)
  1165. };
  1166. //
  1167. // User-defined assertions
  1168. //
  1169. struct BeginAssertionInst : Inst, BodyGroupsMixin, NegationMixin, NextLabelMixin
  1170. {
  1171. // nextLabel must always be fixed up
  1172. inline BeginAssertionInst(bool isNegation, int minBodyGroupId, int maxBodyGroupId)
  1173. : Inst(InstTag::BeginAssertion), BodyGroupsMixin(minBodyGroupId, maxBodyGroupId), NegationMixin(isNegation), NextLabelMixin()
  1174. {}
  1175. INST_BODY
  1176. };
  1177. struct EndAssertionInst : Inst
  1178. {
  1179. inline EndAssertionInst() : Inst(InstTag::EndAssertion) {}
  1180. INST_BODY
  1181. };
  1182. #pragma pack(pop)
  1183. // ----------------------------------------------------------------------
  1184. // Matcher state
  1185. // ----------------------------------------------------------------------
  1186. struct LoopInfo : protected Chars<char16>
  1187. {
  1188. CharCount number; // current iteration number
  1189. CharCount startInputOffset; // input offset where the iteration started
  1190. JsUtil::List<CharCount, ArenaAllocator>* offsetsOfFollowFirst; // list of offsets from startInputOffset where we matched with followFirst
  1191. inline void Reset()
  1192. {
  1193. #if DBG
  1194. // So debug prints will look nice
  1195. number = 0;
  1196. startInputOffset = 0;
  1197. if (offsetsOfFollowFirst)
  1198. {
  1199. offsetsOfFollowFirst->ClearAndZero();
  1200. }
  1201. #endif
  1202. }
  1203. inline void EnsureOffsetsOfFollowFirst(Matcher& matcher);
  1204. #if ENABLE_REGEX_CONFIG_OPTIONS
  1205. void Print(DebugWriter* w) const;
  1206. #endif
  1207. };
  1208. struct GroupInfo : protected Chars<char16>
  1209. {
  1210. Field(CharCount) offset;
  1211. Field(CharCountOrFlag) length; // CharCountFlag => group is undefined
  1212. inline GroupInfo() : offset(0), length(CharCountFlag) {}
  1213. inline GroupInfo(CharCount offset, CharCountOrFlag length) : offset(offset), length(length) {}
  1214. //This constructor will only be called by a cross-site marshalling and thus we shouldn't clear offset and length
  1215. GroupInfo(VirtualTableInfoCtorEnum) { }
  1216. inline bool IsUndefined() const { return length == CharCountFlag; }
  1217. inline CharCount EndOffset() const { Assert(length != CharCountFlag); return offset + (CharCount)length; }
  1218. inline void Reset()
  1219. {
  1220. // The start offset must not be changed when backtracking into the group
  1221. length = CharCountFlag;
  1222. }
  1223. #if ENABLE_REGEX_CONFIG_OPTIONS
  1224. void Print(DebugWriter* w, const Char* const input) const;
  1225. #endif
  1226. };
  1227. struct AssertionInfo : private Chars<char16>
  1228. {
  1229. const Label beginLabel; // label of BeginAssertion instruction
  1230. CharCount startInputOffset; // input offset when begun assertion (so can rewind)
  1231. size_t contStackPosition; // top of continuation stack when begun assertion (so can cut)
  1232. inline AssertionInfo(Label beginLabel, CharCount startInputOffset, size_t contStackPosition)
  1233. : beginLabel(beginLabel), startInputOffset(startInputOffset), contStackPosition(contStackPosition) {}
  1234. #if ENABLE_REGEX_CONFIG_OPTIONS
  1235. void Print(DebugWriter* w) const;
  1236. #endif
  1237. };
  1238. // ----------------------------------------------------------------------
  1239. // Continuations
  1240. // ----------------------------------------------------------------------
  1241. struct Cont : protected Chars<char16>
  1242. {
  1243. enum class ContTag : uint8
  1244. {
  1245. #define M(O) O,
  1246. #include "RegexContcodes.h"
  1247. #undef M
  1248. };
  1249. ContTag tag;
  1250. inline Cont(ContTag tag) : tag(tag) {}
  1251. #if ENABLE_REGEX_CONFIG_OPTIONS
  1252. virtual int Print(DebugWriter*w, const Char* const input) const = 0;
  1253. #endif
  1254. };
  1255. #if ENABLE_REGEX_CONFIG_OPTIONS
  1256. #define CONT_PRINT int Print(DebugWriter*w, const Char* const input) const override;
  1257. #else
  1258. #define CONT_PRINT
  1259. #endif
  1260. #define REGEX_CONT_EXEC_PARAMETERS Matcher& matcher, const Char* const input, CharCount& inputOffset, const uint8*& instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks
  1261. #define CONT_BODY bool Exec(REGEX_CONT_EXEC_PARAMETERS); \
  1262. CONT_PRINT
  1263. struct ResumeCont : Cont
  1264. {
  1265. CharCount origInputOffset;
  1266. Label origInstLabel;
  1267. inline ResumeCont(CharCount origInputOffset, Label origInstLabel) : Cont(ContTag::Resume), origInputOffset(origInputOffset), origInstLabel(origInstLabel) {}
  1268. CONT_BODY
  1269. };
  1270. struct RestoreLoopCont : Cont
  1271. {
  1272. int loopId;
  1273. LoopInfo origLoopInfo;
  1274. RestoreLoopCont(int loopId, LoopInfo& origLoopInfo, Matcher& matcher);
  1275. CONT_BODY
  1276. };
  1277. struct RestoreGroupCont : Cont
  1278. {
  1279. int groupId;
  1280. GroupInfo origGroupInfo;
  1281. RestoreGroupCont(int groupId, const GroupInfo &origGroupInfo)
  1282. : Cont(ContTag::RestoreGroup), groupId(groupId), origGroupInfo(origGroupInfo)
  1283. {
  1284. }
  1285. CONT_BODY
  1286. };
  1287. struct ResetGroupCont : Cont
  1288. {
  1289. const int groupId;
  1290. ResetGroupCont(const int groupId) : Cont(ContTag::ResetGroup), groupId(groupId) {}
  1291. CONT_BODY
  1292. };
  1293. struct ResetGroupRangeCont : Cont
  1294. {
  1295. const int fromGroupId;
  1296. const int toGroupId;
  1297. ResetGroupRangeCont(const int fromGroupId, const int toGroupId)
  1298. : Cont(ContTag::ResetGroupRange), fromGroupId(fromGroupId), toGroupId(toGroupId)
  1299. {
  1300. Assert(fromGroupId >= 0);
  1301. Assert(toGroupId >= 0);
  1302. Assert(fromGroupId < toGroupId);
  1303. }
  1304. CONT_BODY
  1305. };
  1306. struct RepeatLoopCont : Cont
  1307. {
  1308. Label beginLabel; // label of BeginLoop instruction
  1309. CharCount origInputOffset; // where to go back to
  1310. inline RepeatLoopCont(Label beginLabel, CharCount origInputOffset) : Cont(ContTag::RepeatLoop), beginLabel(beginLabel), origInputOffset(origInputOffset) {}
  1311. CONT_BODY
  1312. };
  1313. struct PopAssertionCont : Cont
  1314. {
  1315. inline PopAssertionCont() : Cont(ContTag::PopAssertion) {}
  1316. CONT_BODY
  1317. };
  1318. struct RewindLoopFixedCont : Cont
  1319. {
  1320. Label beginLabel; // label of BeginLoopFixed instruction
  1321. bool tryingBody; // true if attempting an additional iteration of loop body, otherwise attempting loop follow
  1322. inline RewindLoopFixedCont(Label beginLabel, bool tryingBody) : Cont(ContTag::RewindLoopFixed), beginLabel(beginLabel), tryingBody(tryingBody) {}
  1323. CONT_BODY
  1324. };
  1325. struct RewindLoopSetCont : Cont
  1326. {
  1327. Label beginLabel; // label of LoopSet instruction
  1328. inline RewindLoopSetCont(Label beginLabel) : Cont(ContTag::RewindLoopSet), beginLabel(beginLabel) {}
  1329. CONT_BODY
  1330. };
  1331. struct RewindLoopSetWithFollowFirstCont : Cont
  1332. {
  1333. Label beginLabel; // label of LoopSet instruction
  1334. inline RewindLoopSetWithFollowFirstCont(Label beginLabel) : Cont(ContTag::RewindLoopSetWithFollowFirst), beginLabel(beginLabel) {}
  1335. CONT_BODY
  1336. };
  1337. struct RewindLoopFixedGroupLastIterationCont : Cont
  1338. {
  1339. Label beginLabel; // label of BeginLoopFixedGroupLastIteration instruction
  1340. bool tryingBody; // true if attempting an additional iteration of loop body, otherwise attempting loop follow
  1341. inline RewindLoopFixedGroupLastIterationCont(Label beginLabel, bool tryingBody) : Cont(ContTag::RewindLoopFixedGroupLastIteration), beginLabel(beginLabel), tryingBody(tryingBody) {}
  1342. CONT_BODY
  1343. };
  1344. // ----------------------------------------------------------------------
  1345. // Matcher
  1346. // ----------------------------------------------------------------------
  1347. class ContStack : public ContinuousPageStackOfVariableElements<Cont>, private Chars<char16>
  1348. {
  1349. public:
  1350. inline ContStack(PageAllocator *const pageAllocator, void (*const outOfMemoryFunc)())
  1351. : ContinuousPageStackOfVariableElements(pageAllocator, outOfMemoryFunc)
  1352. {
  1353. }
  1354. #if ENABLE_REGEX_CONFIG_OPTIONS
  1355. void Print(DebugWriter* w, const Char* const input) const;
  1356. #endif
  1357. };
  1358. class AssertionStack : public ContinuousPageStackOfFixedElements<AssertionInfo>
  1359. {
  1360. public:
  1361. inline AssertionStack(PageAllocator *const pageAllocator, void (*const outOfMemoryFunc)())
  1362. : ContinuousPageStackOfFixedElements(pageAllocator, outOfMemoryFunc)
  1363. {
  1364. }
  1365. #if ENABLE_REGEX_CONFIG_OPTIONS
  1366. void Print(DebugWriter* w, const Matcher* const matcher) const;
  1367. #endif
  1368. };
  1369. struct RegexStacks
  1370. {
  1371. RegexStacks(PageAllocator * pageAllocator) :
  1372. contStack(pageAllocator, Js::Throw::OutOfMemory),
  1373. assertionStack(pageAllocator, Js::Throw::OutOfMemory) {};
  1374. ContStack contStack;
  1375. AssertionStack assertionStack;
  1376. };
  1377. enum class HardFailMode
  1378. {
  1379. BacktrackAndLater,
  1380. BacktrackOnly,
  1381. LaterOnly,
  1382. ImmediateFail
  1383. };
  1384. class Matcher : private Chars<char16>
  1385. {
  1386. #define M(TagName) friend struct TagName##Inst;
  1387. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, ...) TemplateDeclaration friend struct GenericClassName;
  1388. #include "RegexOpCodes.h"
  1389. #undef M
  1390. #undef MTemplate
  1391. #define M(O) friend O##Cont;
  1392. #include "RegexContcodes.h"
  1393. #undef M
  1394. template <typename ScannerT>
  1395. friend struct SyncToLiteralAndConsumeInstT;
  1396. template <typename ScannerT>
  1397. friend struct SyncToLiteralAndContinueInstT;
  1398. template <typename ScannerT>
  1399. friend struct SyncToLiteralAndBackupInstT;
  1400. template <typename ScannerT>
  1401. friend struct ScannerMixinT;
  1402. friend struct Char2LiteralScannerMixin;
  1403. template <uint lastPatCharEquivClassSize>
  1404. friend struct EquivScannerMixinT;
  1405. friend GroupInfo;
  1406. friend LoopInfo;
  1407. public:
  1408. static const uint TicksPerQc;
  1409. static const uint TicksPerQcTimeCheck;
  1410. static const uint TimePerQc; // milliseconds
  1411. private:
  1412. Field(RegexPattern *) const pattern;
  1413. Field(StandardChars<Char>*) standardChars;
  1414. Field(const Program*) program;
  1415. Field(GroupInfo*) groupInfos;
  1416. Field(LoopInfo*) loopInfos;
  1417. // Furthest offsets in the input string that we tried to match during a scan.
  1418. // This is used to prevent unnecessary retraversal of the input string.
  1419. //
  1420. // Assume we have the RegExp /<(foo|bar)/ and the input string "<0bar<0bar<0bar".
  1421. // When we try to match the string, we first scan it fully for "foo", but can't
  1422. // find it. Then we scan for "bar" and find it at index 2. However, since there
  1423. // is no '<' right before it, we continue with our search. We do the same thing
  1424. // two more times starting at indexes 7 and 12 (since those are where the "bar"s
  1425. // are), each time scanning the rest of the string fully for "foo".
  1426. //
  1427. // However, if we cache the furthest offsets we tried, we can skip the searches
  1428. // for "foo" after the first time.
  1429. Field(CharCount*) literalNextSyncInputOffsets;
  1430. FieldNoBarrier(Recycler*) recycler;
  1431. Field(uint) previousQcTime;
  1432. #if ENABLE_REGEX_CONFIG_OPTIONS
  1433. FieldNoBarrier(RegexStats*) stats;
  1434. FieldNoBarrier(DebugWriter*) w;
  1435. #endif
  1436. public:
  1437. Matcher(Js::ScriptContext* scriptContext, RegexPattern* pattern);
  1438. static Matcher *New(Js::ScriptContext* scriptContext, RegexPattern* pattern);
  1439. bool Match
  1440. ( const Char* const input
  1441. , const CharCount inputLength
  1442. , CharCount offset
  1443. , Js::ScriptContext * scriptContext
  1444. #if ENABLE_REGEX_CONFIG_OPTIONS
  1445. , RegexStats* stats
  1446. , DebugWriter* w
  1447. #endif
  1448. );
  1449. inline bool WasLastMatchSuccessful() const
  1450. {
  1451. return !groupInfos[0].IsUndefined();
  1452. }
  1453. inline uint16 NumGroups() const
  1454. {
  1455. return program->numGroups;
  1456. }
  1457. inline GroupInfo GetGroup(int groupId) const
  1458. {
  1459. return *GroupIdToGroupInfo(groupId);
  1460. }
  1461. #if ENABLE_REGEX_CONFIG_OPTIONS
  1462. void Print(DebugWriter* w, const Char* const input, const CharCount inputLength, CharCount inputOffset, const uint8* instPointer, ContStack &contStack, AssertionStack &assertionStack) const;
  1463. #endif
  1464. private:
  1465. #if ENABLE_REGEX_CONFIG_OPTIONS
  1466. void PushStats(ContStack& contStack, const Char* const input) const;
  1467. void PopStats(ContStack& contStack, const Char* const input) const;
  1468. void UnPopStats(ContStack& contStack, const Char* const input) const;
  1469. void CompStats() const;
  1470. void InstStats() const;
  1471. #endif
  1472. private:
  1473. inline void QueryContinue(uint &qcTicks);
  1474. void DoQueryContinue(const uint qcTicks);
  1475. public:
  1476. static void TraceQueryContinue(const uint now);
  1477. private:
  1478. // Try backtracking, or return true if should stop. There could be a match using a later starting point.
  1479. bool Fail(const Char* const input, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks);
  1480. bool RunContStack(const Char* const input, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks);
  1481. // As above, but control whether to try backtracking or later matches
  1482. inline bool HardFail(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks, HardFailMode mode);
  1483. inline void Run(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &nextSyncInputOffset, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks, bool firstIteration);
  1484. inline bool MatchHere(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &nextSyncInputOffset, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks, bool firstIteration);
  1485. // Return true if assertion succeeded
  1486. inline bool PopAssertion(CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, bool isFailed);
  1487. inline Label InstPointerToLabel(const uint8* inst) const
  1488. {
  1489. Assert(inst >= program->rep.insts.insts && inst < program->rep.insts.insts + program->rep.insts.instsLen);
  1490. return (Label)((uint8*)inst - program->rep.insts.insts);
  1491. }
  1492. inline uint8* LabelToInstPointer(Label label) const
  1493. {
  1494. Assert(label < program->rep.insts.instsLen);
  1495. return program->rep.insts.insts + label;
  1496. }
  1497. template <typename T>
  1498. inline T* LabelToInstPointer(Inst::InstTag tag, Label label) const
  1499. {
  1500. Assert(label + sizeof(T) <= program->rep.insts.instsLen);
  1501. Assert(((Inst*)(program->rep.insts.insts + label))->tag == tag);
  1502. return (T*)(program->rep.insts.insts + label);
  1503. }
  1504. inline LoopInfo* LoopIdToLoopInfo(int loopId)
  1505. {
  1506. Assert(loopId >= 0 && loopId < program->numLoops);
  1507. return loopInfos + loopId;
  1508. }
  1509. public:
  1510. inline GroupInfo* GroupIdToGroupInfo(int groupId) const
  1511. {
  1512. Assert(groupId >= 0 && groupId < program->numGroups);
  1513. return groupInfos + groupId;
  1514. }
  1515. Matcher *CloneToScriptContext(Js::ScriptContext *scriptContext, RegexPattern *pattern);
  1516. private:
  1517. typedef bool (UnifiedRegex::Matcher::*ComparerForSingleChar)(const Char left, const Char right);
  1518. Field(ComparerForSingleChar) comparerForSingleChar;
  1519. // Specialized matcher for regex c - case insensitive
  1520. inline bool MatchSingleCharCaseInsensitive(const Char* const input, const CharCount inputLength, CharCount offset, const Char c);
  1521. inline bool MatchSingleCharCaseInsensitiveHere(CaseInsensitive::MappingSource mappingSource, const Char* const input, CharCount offset, const Char c);
  1522. // Specialized matcher for regex c - case sensitive
  1523. inline bool MatchSingleCharCaseSensitive(const Char* const input, const CharCount inputLength, CharCount offset, const Char c);
  1524. // Specialized matcher for regex \b\w+\b
  1525. inline bool MatchBoundedWord(const Char* const input, const CharCount inputLength, CharCount offset);
  1526. // Specialized matcher for regex ^\s*|\s*$
  1527. inline bool MatchLeadingTrailingSpaces(const Char* const input, const CharCount inputLength, CharCount offset);
  1528. // Specialized matcher for octoquad patterns
  1529. inline bool MatchOctoquad(const Char* const input, const CharCount inputLength, CharCount offset, OctoquadMatcher* matcher);
  1530. // Specialized matcher for regex ^literal
  1531. inline bool MatchBOILiteral2(const Char * const input, const CharCount inputLength, CharCount offset, DWORD literal2);
  1532. void SaveInnerGroups(const int fromGroupId, const int toGroupId, const bool reset, const Char *const input, ContStack &contStack);
  1533. void DoSaveInnerGroups(const int fromGroupId, const int toGroupId, const bool reset, const Char *const input, ContStack &contStack);
  1534. void SaveInnerGroups_AllUndefined(const int fromGroupId, const int toGroupId, const Char *const input, ContStack &contStack);
  1535. void DoSaveInnerGroups_AllUndefined(const int fromGroupId, const int toGroupId, const Char *const input, ContStack &contStack);
  1536. void ResetGroup(int groupId);
  1537. void ResetInnerGroups(int minGroupId, int maxGroupId);
  1538. #if DBG
  1539. void ResetLoopInfos();
  1540. #endif
  1541. };
  1542. }
  1543. #undef INST_BODY_FREE
  1544. #undef INST_BODY_PRINT
  1545. #undef INST_BODY
  1546. #undef CONT_PRINT
  1547. #undef CONT_BODY