RegexRuntime.cpp 201 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862
  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. #include "ParserPch.h"
  6. namespace UnifiedRegex
  7. {
  8. // ----------------------------------------------------------------------
  9. // CountDomain
  10. // ----------------------------------------------------------------------
  11. #if ENABLE_REGEX_CONFIG_OPTIONS
  12. void CountDomain::Print(DebugWriter* w) const
  13. {
  14. if (upper != CharCountFlag && lower == (CharCount)upper)
  15. {
  16. w->Print(_u("[%u]"), lower);
  17. }
  18. else
  19. {
  20. w->Print(_u("[%u-"), lower);
  21. if (upper == CharCountFlag)
  22. w->Print(_u("inf]"));
  23. else
  24. w->Print(_u("%u]"), (CharCount)upper);
  25. }
  26. }
  27. #endif
  28. // ----------------------------------------------------------------------
  29. // Matcher (inlined, called from instruction Exec methods)
  30. // ----------------------------------------------------------------------
  31. #define PUSH(contStack, T, ...) (new (contStack.Push<T>()) T(__VA_ARGS__))
  32. #define PUSHA(assertionStack, T, ...) (new (assertionStack.Push()) T(__VA_ARGS__))
  33. #define L2I(O, label) LabelToInstPointer<O##Inst>(Inst::InstTag::O, label)
  34. #define FAIL_PARAMETERS input, inputOffset, instPointer, contStack, assertionStack, qcTicks
  35. #define HARDFAIL_PARAMETERS(mode) input, inputLength, matchStart, inputOffset, instPointer, contStack, assertionStack, qcTicks, mode
  36. // Regex QC heuristics:
  37. // - TicksPerQC
  38. // - Number of ticks from a previous QC needed to cause another QC. The value affects how often QC will be triggered, so
  39. // on slower machines or debug builds, the value needs to be smaller to maintain a reasonable frequency of QCs.
  40. // - TicksPerQcTimeCheck
  41. // - Number of ticks from a previous QC needed to trigger a time check. Elapsed time from the previous QC is checked to
  42. // see if a QC needs to be triggered. The value must be less than TicksPerQc and small enough to reasonably guarantee
  43. // a QC every TimePerQc milliseconds without affecting perf.
  44. // - TimePerQc
  45. // - The target time between QCs
  46. #if defined(_M_ARM)
  47. const uint Matcher::TicksPerQc = 1u << 19
  48. #else
  49. const uint Matcher::TicksPerQc = 1u << (AutoSystemInfo::ShouldQCMoreFrequently() ? 17 : 21)
  50. #endif
  51. #if DBG
  52. >> 2
  53. #endif
  54. ;
  55. const uint Matcher::TicksPerQcTimeCheck = Matcher::TicksPerQc >> 2;
  56. const uint Matcher::TimePerQc = AutoSystemInfo::ShouldQCMoreFrequently() ? 50 : 100; // milliseconds
  57. #if ENABLE_REGEX_CONFIG_OPTIONS
  58. void Matcher::PushStats(ContStack& contStack, const Char* const input) const
  59. {
  60. if (stats != 0)
  61. {
  62. stats->numPushes++;
  63. if (contStack.Position() > stats->stackHWM)
  64. stats->stackHWM = contStack.Position();
  65. }
  66. if (w != 0)
  67. {
  68. w->Print(_u("PUSH "));
  69. contStack.Top()->Print(w, input);
  70. }
  71. }
  72. void Matcher::PopStats(ContStack& contStack, const Char* const input) const
  73. {
  74. if (stats != 0)
  75. {
  76. stats->numPops++;
  77. }
  78. if (w != 0)
  79. {
  80. const Cont* top = contStack.Top();
  81. if (top == 0)
  82. w->PrintEOL(_u("<empty stack>"));
  83. else
  84. {
  85. w->Print(_u("POP "));
  86. top->Print(w, input);
  87. }
  88. }
  89. }
  90. void Matcher::UnPopStats(ContStack& contStack, const Char* const input) const
  91. {
  92. if (stats != 0)
  93. {
  94. stats->numPops--;
  95. }
  96. if (w != 0)
  97. {
  98. const Cont* top = contStack.Top();
  99. if (top == 0)
  100. w->PrintEOL(_u("<empty stack>"));
  101. else
  102. {
  103. w->Print(_u("UNPOP "));
  104. top->Print(w, input);
  105. }
  106. }
  107. }
  108. void Matcher::CompStats() const
  109. {
  110. if (stats != 0)
  111. {
  112. stats->numCompares++;
  113. }
  114. }
  115. void Matcher::InstStats() const
  116. {
  117. if (stats != 0)
  118. {
  119. stats->numInsts++;
  120. }
  121. }
  122. #endif
  123. inline void Matcher::QueryContinue(uint &qcTicks)
  124. {
  125. // See definition of TimePerQc for description of regex QC heuristics
  126. Assert(!(TicksPerQc & TicksPerQc - 1)); // must be a power of 2
  127. Assert(!(TicksPerQcTimeCheck & TicksPerQcTimeCheck - 1)); // must be a power of 2
  128. Assert(TicksPerQcTimeCheck < TicksPerQc);
  129. if (PHASE_OFF1(Js::RegexQcPhase))
  130. {
  131. return;
  132. }
  133. if (++qcTicks & TicksPerQcTimeCheck - 1)
  134. {
  135. return;
  136. }
  137. DoQueryContinue(qcTicks);
  138. }
  139. inline bool Matcher::HardFail(
  140. const Char* const input
  141. , const CharCount inputLength
  142. , CharCount &matchStart
  143. , CharCount &inputOffset
  144. , const uint8 *&instPointer
  145. , ContStack &contStack
  146. , AssertionStack &assertionStack
  147. , uint &qcTicks
  148. , HardFailMode mode)
  149. {
  150. switch (mode)
  151. {
  152. case HardFailMode::BacktrackAndLater:
  153. return Fail(FAIL_PARAMETERS);
  154. case HardFailMode::BacktrackOnly:
  155. if (Fail(FAIL_PARAMETERS))
  156. {
  157. // No use trying any more start positions
  158. matchStart = inputLength;
  159. return true; // STOP EXECUTING
  160. }
  161. else
  162. {
  163. return false;
  164. }
  165. case HardFailMode::LaterOnly:
  166. #if ENABLE_REGEX_CONFIG_OPTIONS
  167. if (w != 0)
  168. {
  169. w->PrintEOL(_u("CLEAR"));
  170. }
  171. #endif
  172. contStack.Clear();
  173. assertionStack.Clear();
  174. return true; // STOP EXECUTING
  175. case HardFailMode::ImmediateFail:
  176. // No use trying any more start positions
  177. matchStart = inputLength;
  178. return true; // STOP EXECUTING
  179. default:
  180. Assume(false);
  181. }
  182. return true;
  183. }
  184. inline bool Matcher::PopAssertion(CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, bool succeeded)
  185. {
  186. AssertionInfo* info = assertionStack.Top();
  187. Assert(info != 0);
  188. assertionStack.Pop();
  189. BeginAssertionInst* begin = L2I(BeginAssertion, info->beginLabel);
  190. // Cut the existing continuations (we never backtrack into an assertion)
  191. // NOTE: We don't include the effective pops in the stats
  192. #if ENABLE_REGEX_CONFIG_OPTIONS
  193. if (w != 0)
  194. {
  195. w->PrintEOL(_u("POP TO %llu"), (unsigned long long)info->contStackPosition);
  196. }
  197. #endif
  198. contStack.PopTo(info->contStackPosition);
  199. // succeeded isNegation action
  200. // --------- ---------- ----------------------------------------------------------------------------------
  201. // false false Fail into outer continuations (inner group bindings will have been undone)
  202. // true false Jump to next label (inner group bindings are now frozen)
  203. // false true Jump to next label (inner group bindings will have been undone and are now frozen)
  204. // true true Fail into outer continuations (inner group binding MUST BE CLEARED)
  205. if (succeeded && begin->isNegation)
  206. {
  207. ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  208. }
  209. if (succeeded == begin->isNegation)
  210. {
  211. // Assertion failed
  212. return false;
  213. }
  214. else
  215. {
  216. // Continue with next label but from original input position
  217. inputOffset = info->startInputOffset;
  218. instPointer = LabelToInstPointer(begin->nextLabel);
  219. return true;
  220. }
  221. }
  222. inline void Matcher::SaveInnerGroups(
  223. const int fromGroupId,
  224. const int toGroupId,
  225. const bool reset,
  226. const Char *const input,
  227. ContStack &contStack)
  228. {
  229. if (toGroupId >= 0)
  230. {
  231. DoSaveInnerGroups(fromGroupId, toGroupId, reset, input, contStack);
  232. }
  233. }
  234. void Matcher::DoSaveInnerGroups(
  235. const int fromGroupId,
  236. const int toGroupId,
  237. const bool reset,
  238. const Char *const input,
  239. ContStack &contStack)
  240. {
  241. Assert(fromGroupId >= 0);
  242. Assert(toGroupId >= 0);
  243. Assert(fromGroupId <= toGroupId);
  244. int undefinedRangeFromId = -1;
  245. int groupId = fromGroupId;
  246. do
  247. {
  248. GroupInfo *const groupInfo = GroupIdToGroupInfo(groupId);
  249. if (groupInfo->IsUndefined())
  250. {
  251. if (undefinedRangeFromId < 0)
  252. {
  253. undefinedRangeFromId = groupId;
  254. }
  255. continue;
  256. }
  257. if (undefinedRangeFromId >= 0)
  258. {
  259. Assert(groupId > 0);
  260. DoSaveInnerGroups_AllUndefined(undefinedRangeFromId, groupId - 1, input, contStack);
  261. undefinedRangeFromId = -1;
  262. }
  263. PUSH(contStack, RestoreGroupCont, groupId, *groupInfo);
  264. #if ENABLE_REGEX_CONFIG_OPTIONS
  265. PushStats(contStack, input);
  266. #endif
  267. if (reset)
  268. {
  269. groupInfo->Reset();
  270. }
  271. } while (++groupId <= toGroupId);
  272. if (undefinedRangeFromId >= 0)
  273. {
  274. Assert(toGroupId >= 0);
  275. DoSaveInnerGroups_AllUndefined(undefinedRangeFromId, toGroupId, input, contStack);
  276. }
  277. }
  278. inline void Matcher::SaveInnerGroups_AllUndefined(
  279. const int fromGroupId,
  280. const int toGroupId,
  281. const Char *const input,
  282. ContStack &contStack)
  283. {
  284. if (toGroupId >= 0)
  285. {
  286. DoSaveInnerGroups_AllUndefined(fromGroupId, toGroupId, input, contStack);
  287. }
  288. }
  289. void Matcher::DoSaveInnerGroups_AllUndefined(
  290. const int fromGroupId,
  291. const int toGroupId,
  292. const Char *const input,
  293. ContStack &contStack)
  294. {
  295. Assert(fromGroupId >= 0);
  296. Assert(toGroupId >= 0);
  297. Assert(fromGroupId <= toGroupId);
  298. #if DBG
  299. for (int groupId = fromGroupId; groupId <= toGroupId; ++groupId)
  300. {
  301. Assert(GroupIdToGroupInfo(groupId)->IsUndefined());
  302. }
  303. #endif
  304. if (fromGroupId == toGroupId)
  305. {
  306. PUSH(contStack, ResetGroupCont, fromGroupId);
  307. }
  308. else
  309. {
  310. PUSH(contStack, ResetGroupRangeCont, fromGroupId, toGroupId);
  311. }
  312. #if ENABLE_REGEX_CONFIG_OPTIONS
  313. PushStats(contStack, input);
  314. #endif
  315. }
  316. inline void Matcher::ResetGroup(int groupId)
  317. {
  318. GroupInfo* info = GroupIdToGroupInfo(groupId);
  319. info->Reset();
  320. }
  321. inline void Matcher::ResetInnerGroups(int minGroupId, int maxGroupId)
  322. {
  323. for (int i = minGroupId; i <= maxGroupId; i++)
  324. {
  325. ResetGroup(i);
  326. }
  327. }
  328. #if ENABLE_REGEX_CONFIG_OPTIONS
  329. bool Inst::IsBaselineMode()
  330. {
  331. return Js::Configuration::Global.flags.BaselineMode;
  332. }
  333. Label Inst::GetPrintLabel(Label label)
  334. {
  335. return IsBaselineMode() ? (Label)0xFFFF : label;
  336. }
  337. template <typename T>
  338. void Inst::PrintBytes(DebugWriter *w, Inst *inst, T *that, const char16 *annotation) const
  339. {
  340. T *start = (T*)that;
  341. byte *startByte = (byte *)start;
  342. byte *baseByte = (byte *)inst;
  343. ptrdiff_t offset = startByte - baseByte;
  344. size_t size = sizeof(*((T *)that));
  345. byte *endByte = startByte + size;
  346. byte *currentByte = startByte;
  347. w->Print(_u("0x%p[+0x%03x](0x%03x) [%s]:"), startByte, offset, size, annotation);
  348. for (; currentByte < endByte; ++currentByte)
  349. {
  350. if ((currentByte - endByte) % 4 == 0)
  351. {
  352. w->Print(_u(" "), *currentByte);
  353. }
  354. w->Print(_u("%02x"), *currentByte);
  355. }
  356. w->PrintEOL(_u(""));
  357. }
  358. template <>
  359. void Inst::PrintBytes(DebugWriter *w, Inst *inst, Inst *that, const char16 *annotation) const
  360. {
  361. Inst *start = (Inst *)that;
  362. size_t baseSize = sizeof(*(Inst *)that);
  363. ptrdiff_t offsetToData = (byte *)&(start->tag) - ((byte *)start);
  364. size_t size = baseSize - offsetToData;
  365. byte *startByte = (byte *)(&(start->tag));
  366. byte *endByte = startByte + size;
  367. byte *currentByte = startByte;
  368. w->Print(_u("0x%p[+0x%03x](0x%03x) [%s]:"), startByte, offsetToData, size, annotation);
  369. for (; currentByte < endByte; ++currentByte)
  370. {
  371. if ((currentByte - endByte) % 4 == 0)
  372. {
  373. w->Print(_u(" "), *currentByte);
  374. }
  375. w->Print(_u("%02x"), *currentByte);
  376. }
  377. w->PrintEOL(_u(""));
  378. }
  379. #define PRINT_BYTES(InstType) \
  380. Inst::PrintBytes<InstType>(w, (Inst *)this, (InstType *)this, _u(#InstType))
  381. #define PRINT_BYTES_ANNOTATED(InstType, Annotation) \
  382. Inst::PrintBytes<InstType>(w, (Inst *)this, (InstType *)this, (Annotation))
  383. #define PRINT_MIXIN(Mixin) \
  384. ((Mixin *)this)->Print(w, litbuf)
  385. #define PRINT_MIXIN_ARGS(Mixin, ...) \
  386. ((Mixin *)this)->Print(w, litbuf, __VA_ARGS__)
  387. #define PRINT_MIXIN_COMMA(Mixin) \
  388. PRINT_MIXIN(Mixin); \
  389. w->Print(_u(", "));
  390. #define PRINT_RE_BYTECODE_BEGIN(Name) \
  391. w->Print(_u("L%04x: "), label); \
  392. if (REGEX_CONFIG_FLAG(RegexBytecodeDebug)) \
  393. { \
  394. w->Print(_u("(0x%03x bytes) "), sizeof(*this)); \
  395. } \
  396. w->Print(_u(Name)); \
  397. w->Print(_u("("));
  398. #define PRINT_RE_BYTECODE_MID() \
  399. w->PrintEOL(_u(")")); \
  400. if (REGEX_CONFIG_FLAG(RegexBytecodeDebug)) \
  401. { \
  402. w->Indent(); \
  403. PRINT_BYTES(Inst);
  404. #define PRINT_RE_BYTECODE_END() \
  405. w->Unindent(); \
  406. } \
  407. return sizeof(*this);
  408. #endif
  409. // ----------------------------------------------------------------------
  410. // Mixins
  411. // ----------------------------------------------------------------------
  412. #if ENABLE_REGEX_CONFIG_OPTIONS
  413. void BackupMixin::Print(DebugWriter* w, const char16* litbuf) const
  414. {
  415. w->Print(_u("backup: "));
  416. backup.Print(w);
  417. }
  418. #endif
  419. #if ENABLE_REGEX_CONFIG_OPTIONS
  420. void CharMixin::Print(DebugWriter* w, const char16* litbuf) const
  421. {
  422. w->Print(_u("c: "));
  423. w->PrintQuotedChar(c);
  424. }
  425. #endif
  426. #if ENABLE_REGEX_CONFIG_OPTIONS
  427. void Char2Mixin::Print(DebugWriter* w, const char16* litbuf) const
  428. {
  429. w->Print(_u("c0: "));
  430. w->PrintQuotedChar(cs[0]);
  431. w->Print(_u(", c1: "));
  432. w->PrintQuotedChar(cs[1]);
  433. }
  434. #endif
  435. #if ENABLE_REGEX_CONFIG_OPTIONS
  436. void Char3Mixin::Print(DebugWriter* w, const char16* litbuf) const
  437. {
  438. w->Print(_u("c0: "));
  439. w->PrintQuotedChar(cs[0]);
  440. w->Print(_u(", c1: "));
  441. w->PrintQuotedChar(cs[1]);
  442. w->Print(_u(", c2: "));
  443. w->PrintQuotedChar(cs[2]);
  444. }
  445. #endif
  446. #if ENABLE_REGEX_CONFIG_OPTIONS
  447. void Char4Mixin::Print(DebugWriter* w, const char16* litbuf) const
  448. {
  449. w->Print(_u("c0: "));
  450. w->PrintQuotedChar(cs[0]);
  451. w->Print(_u(", c1: "));
  452. w->PrintQuotedChar(cs[1]);
  453. w->Print(_u(", c2: "));
  454. w->PrintQuotedChar(cs[2]);
  455. w->Print(_u(", c3: "));
  456. w->PrintQuotedChar(cs[3]);
  457. }
  458. #endif
  459. #if ENABLE_REGEX_CONFIG_OPTIONS
  460. void LiteralMixin::Print(DebugWriter* w, const char16* litbuf, bool isEquivClass) const
  461. {
  462. if (isEquivClass)
  463. {
  464. w->Print(_u("equivLiterals: "));
  465. for (int i = 0; i < CaseInsensitive::EquivClassSize; i++)
  466. {
  467. if (i > 0)
  468. {
  469. w->Print(_u(", "));
  470. }
  471. w->Print(_u("\""));
  472. for (CharCount j = 0; j < length; j++)
  473. {
  474. w->PrintEscapedChar(litbuf[offset + j * CaseInsensitive::EquivClassSize + i]);
  475. }
  476. w->Print(_u("\""));
  477. }
  478. }
  479. else
  480. {
  481. w->Print(_u("literal: "));
  482. w->PrintQuotedString(litbuf + offset, length);
  483. }
  484. }
  485. #endif
  486. // ----------------------------------------------------------------------
  487. // Char2LiteralScannerMixin
  488. // ----------------------------------------------------------------------
  489. bool Char2LiteralScannerMixin::Match(Matcher& matcher, const char16* const input, const CharCount inputLength, CharCount& inputOffset) const
  490. {
  491. if (inputLength == 0)
  492. {
  493. return false;
  494. }
  495. const uint matchC0 = Chars<char16>::CTU(cs[0]);
  496. const uint matchC1 = Chars<char16>::CTU(cs[1]);
  497. const char16 * currentInput = input + inputOffset;
  498. const char16 * endInput = input + inputLength - 1;
  499. while (currentInput < endInput)
  500. {
  501. #if ENABLE_REGEX_CONFIG_OPTIONS
  502. matcher.CompStats();
  503. #endif
  504. while (true)
  505. {
  506. const uint c1 = Chars<char16>::CTU(currentInput[1]);
  507. if (c1 != matchC1)
  508. {
  509. if (c1 == matchC0)
  510. {
  511. break;
  512. }
  513. currentInput += 2;
  514. if (currentInput >= endInput)
  515. {
  516. return false;
  517. }
  518. continue;
  519. }
  520. #if ENABLE_REGEX_CONFIG_OPTIONS
  521. matcher.CompStats();
  522. #endif
  523. // Check the first character
  524. const uint c0 = Chars<char16>::CTU(*currentInput);
  525. if (c0 == matchC0)
  526. {
  527. inputOffset = (CharCount)(currentInput - input);
  528. return true;
  529. }
  530. if (matchC0 == matchC1)
  531. {
  532. break;
  533. }
  534. currentInput +=2;
  535. if (currentInput >= endInput)
  536. {
  537. return false;
  538. }
  539. }
  540. // If the second character in the buffer matches the first in the pattern, continue
  541. // to see if the next character has the second in the pattern
  542. currentInput++;
  543. while (currentInput < endInput)
  544. {
  545. #if ENABLE_REGEX_CONFIG_OPTIONS
  546. matcher.CompStats();
  547. #endif
  548. const uint c1 = Chars<char16>::CTU(currentInput[1]);
  549. if (c1 == matchC1)
  550. {
  551. inputOffset = (CharCount)(currentInput - input);
  552. return true;
  553. }
  554. if (c1 != matchC0)
  555. {
  556. currentInput += 2;
  557. break;
  558. }
  559. currentInput++;
  560. }
  561. }
  562. return false;
  563. }
  564. #if ENABLE_REGEX_CONFIG_OPTIONS
  565. void Char2LiteralScannerMixin::Print(DebugWriter* w, const char16 * litbuf) const
  566. {
  567. Char2Mixin::Print(w, litbuf);
  568. w->Print(_u(" (with two character literal scanner)"));
  569. }
  570. #endif
  571. // ----------------------------------------------------------------------
  572. // ScannerMixinT
  573. // ----------------------------------------------------------------------
  574. template <typename ScannerT>
  575. void ScannerMixinT<ScannerT>::FreeBody(ArenaAllocator* rtAllocator)
  576. {
  577. scanner.FreeBody(rtAllocator, length);
  578. }
  579. template <typename ScannerT>
  580. inline bool
  581. ScannerMixinT<ScannerT>::Match(Matcher& matcher, const char16 * const input, const CharCount inputLength, CharCount& inputOffset) const
  582. {
  583. Assert(length <= matcher.program->rep.insts.litbufLen - offset);
  584. return scanner.template Match<1>(
  585. input
  586. , inputLength
  587. , inputOffset
  588. , matcher.program->rep.insts.litbuf + offset
  589. , length
  590. #if ENABLE_REGEX_CONFIG_OPTIONS
  591. , matcher.stats
  592. #endif
  593. );
  594. }
  595. #if ENABLE_REGEX_CONFIG_OPTIONS
  596. template <typename ScannerT>
  597. void ScannerMixinT<ScannerT>::Print(DebugWriter* w, const char16* litbuf, bool isEquivClass) const
  598. {
  599. LiteralMixin::Print(w, litbuf, isEquivClass);
  600. w->Print(_u(" (with %s scanner)"), ScannerT::GetName());
  601. }
  602. #endif
  603. // explicit instantiation
  604. template struct ScannerMixinT<TextbookBoyerMoore<char16>>;
  605. template struct ScannerMixinT<TextbookBoyerMooreWithLinearMap<char16>>;
  606. // ----------------------------------------------------------------------
  607. // EquivScannerMixinT
  608. // ----------------------------------------------------------------------
  609. template <uint lastPatCharEquivClassSize>
  610. inline bool EquivScannerMixinT<lastPatCharEquivClassSize>::Match(Matcher& matcher, const char16* const input, const CharCount inputLength, CharCount& inputOffset) const
  611. {
  612. Assert(length * CaseInsensitive::EquivClassSize <= matcher.program->rep.insts.litbufLen - offset);
  613. CompileAssert(lastPatCharEquivClassSize >= 1 && lastPatCharEquivClassSize <= CaseInsensitive::EquivClassSize);
  614. return scanner.Match<CaseInsensitive::EquivClassSize, lastPatCharEquivClassSize>(
  615. input
  616. , inputLength
  617. , inputOffset
  618. , matcher.program->rep.insts.litbuf + offset
  619. , length
  620. #if ENABLE_REGEX_CONFIG_OPTIONS
  621. , matcher.stats
  622. #endif
  623. );
  624. }
  625. #if ENABLE_REGEX_CONFIG_OPTIONS
  626. template <uint lastPatCharEquivClassSize>
  627. void EquivScannerMixinT<lastPatCharEquivClassSize>::Print(DebugWriter* w, const char16* litbuf) const
  628. {
  629. __super::Print(w, litbuf, true);
  630. w->Print(_u(" (last char equiv size:%d)"), lastPatCharEquivClassSize);
  631. }
  632. // explicit instantiation
  633. template struct EquivScannerMixinT<1>;
  634. #endif
  635. // ----------------------------------------------------------------------
  636. // ScannerInfo
  637. // ----------------------------------------------------------------------
  638. #if ENABLE_REGEX_CONFIG_OPTIONS
  639. void ScannerInfo::Print(DebugWriter* w, const char16* litbuf) const
  640. {
  641. ScannerMixin::Print(w, litbuf, isEquivClass);
  642. }
  643. #endif
  644. ScannerInfo* ScannersMixin::Add(Recycler *recycler, Program *program, CharCount offset, CharCount length, bool isEquivClass)
  645. {
  646. Assert(numLiterals < MaxNumSyncLiterals);
  647. return program->AddScannerForSyncToLiterals(recycler, numLiterals++, offset, length, isEquivClass);
  648. }
  649. void ScannersMixin::FreeBody(ArenaAllocator* rtAllocator)
  650. {
  651. for (int i = 0; i < numLiterals; i++)
  652. {
  653. infos[i]->FreeBody(rtAllocator);
  654. #if DBG
  655. infos[i] = nullptr;
  656. #endif
  657. }
  658. #if DBG
  659. numLiterals = 0;
  660. #endif
  661. }
  662. #if ENABLE_REGEX_CONFIG_OPTIONS
  663. void ScannersMixin::Print(DebugWriter* w, const char16* litbuf) const
  664. {
  665. w->Print(_u("literals: {"));
  666. for (int i = 0; i < numLiterals; i++)
  667. {
  668. if (i > 0)
  669. {
  670. w->Print(_u(", "));
  671. }
  672. infos[i]->Print(w, litbuf);
  673. }
  674. w->Print(_u("}"));
  675. }
  676. #endif
  677. template<bool IsNegation>
  678. void SetMixin<IsNegation>::FreeBody(ArenaAllocator* rtAllocator)
  679. {
  680. set.FreeBody(rtAllocator);
  681. }
  682. #if ENABLE_REGEX_CONFIG_OPTIONS
  683. template<bool IsNegation>
  684. void SetMixin<IsNegation>::Print(DebugWriter* w, const char16* litbuf) const
  685. {
  686. w->Print(_u("set: "));
  687. if (IsNegation)
  688. {
  689. w->Print(_u("not "));
  690. }
  691. set.Print(w);
  692. }
  693. #endif
  694. #if ENABLE_REGEX_CONFIG_OPTIONS
  695. void TrieMixin::Print(DebugWriter* w, const char16* litbuf) const
  696. {
  697. w->PrintEOL(_u(""));
  698. trie.Print(w);
  699. }
  700. #endif
  701. #if ENABLE_REGEX_CONFIG_OPTIONS
  702. void GroupMixin::Print(DebugWriter* w, const char16* litbuf) const
  703. {
  704. w->Print(_u("groupId: %d"), groupId);
  705. }
  706. #endif
  707. #if ENABLE_REGEX_CONFIG_OPTIONS
  708. void ChompBoundedMixin::Print(DebugWriter* w, const char16* litbuf) const
  709. {
  710. w->Print(_u("repeats: "));
  711. repeats.Print(w);
  712. }
  713. #endif
  714. #if ENABLE_REGEX_CONFIG_OPTIONS
  715. void JumpMixin::Print(DebugWriter* w, const char16* litbuf) const
  716. {
  717. w->Print(_u("targetLabel: L%04x"), Inst::GetPrintLabel(targetLabel));
  718. }
  719. #endif
  720. #if ENABLE_REGEX_CONFIG_OPTIONS
  721. void BodyGroupsMixin::Print(DebugWriter* w, const char16* litbuf) const
  722. {
  723. w->Print(_u("minBodyGroupId: %d, maxBodyGroupId: %d"), minBodyGroupId, maxBodyGroupId);
  724. }
  725. #endif
  726. #if ENABLE_REGEX_CONFIG_OPTIONS
  727. void BeginLoopBasicsMixin::Print(DebugWriter* w, const char16* litbuf) const
  728. {
  729. w->Print(_u("loopId: %d, repeats: "), loopId);
  730. repeats.Print(w);
  731. w->Print(_u(", hasOuterLoops: %s"), hasOuterLoops ? _u("true") : _u("false"));
  732. }
  733. #endif
  734. #if ENABLE_REGEX_CONFIG_OPTIONS
  735. void BeginLoopMixin::Print(DebugWriter* w, const char16* litbuf) const
  736. {
  737. BeginLoopBasicsMixin::Print(w, litbuf);
  738. w->Print(_u(", hasInnerNondet: %s, exitLabel: L%04x, "),
  739. hasInnerNondet ? _u("true") : _u("false"), Inst::GetPrintLabel(exitLabel));
  740. }
  741. #endif
  742. #if ENABLE_REGEX_CONFIG_OPTIONS
  743. void GreedyMixin::Print(DebugWriter* w, const char16* litbuf) const
  744. {
  745. w->Print(_u("greedy: %s"), isGreedy ? _u("true") : _u("false"));
  746. }
  747. #endif
  748. #if ENABLE_REGEX_CONFIG_OPTIONS
  749. void RepeatLoopMixin::Print(DebugWriter* w, const char16* litbuf) const
  750. {
  751. w->Print(_u("beginLabel: L%04x"), Inst::GetPrintLabel(beginLabel));
  752. }
  753. #endif
  754. #if ENABLE_REGEX_CONFIG_OPTIONS
  755. void GreedyLoopNoBacktrackMixin::Print(DebugWriter* w, const char16* litbuf) const
  756. {
  757. w->Print(_u("loopId: %d, exitLabel: L%04x"), loopId, exitLabel);
  758. }
  759. #endif
  760. #if ENABLE_REGEX_CONFIG_OPTIONS
  761. void TryMixin::Print(DebugWriter* w, const char16* litbuf) const
  762. {
  763. w->Print(_u("failLabel: L%04x"), Inst::GetPrintLabel(failLabel));
  764. }
  765. #endif
  766. #if ENABLE_REGEX_CONFIG_OPTIONS
  767. void NegationMixin::Print(DebugWriter* w, const char16* litbuf) const
  768. {
  769. w->Print(_u("isNegation: %s"), isNegation ? _u("true") : _u("false"));
  770. }
  771. #endif
  772. #if ENABLE_REGEX_CONFIG_OPTIONS
  773. void NextLabelMixin::Print(DebugWriter* w, const char16* litbuf) const
  774. {
  775. w->Print(_u("nextLabel: L%04x"), Inst::GetPrintLabel(nextLabel));
  776. }
  777. #endif
  778. #if ENABLE_REGEX_CONFIG_OPTIONS
  779. void FixedLengthMixin::Print(DebugWriter* w, const char16* litbuf) const
  780. {
  781. w->Print(_u("length: %u"), length);
  782. }
  783. #endif
  784. #if ENABLE_REGEX_CONFIG_OPTIONS
  785. void FollowFirstMixin::Print(DebugWriter* w, const char16* litbuf) const
  786. {
  787. w->Print(_u("followFirst: %c"), followFirst);
  788. }
  789. #endif
  790. #if ENABLE_REGEX_CONFIG_OPTIONS
  791. void NoNeedToSaveMixin::Print(DebugWriter* w, const char16* litbuf) const
  792. {
  793. w->Print(_u("noNeedToSave: %s"), noNeedToSave ? _u("true") : _u("false"));
  794. }
  795. #endif
  796. #if ENABLE_REGEX_CONFIG_OPTIONS
  797. void SwitchCase::Print(DebugWriter* w) const
  798. {
  799. w->Print(_u("case "));
  800. w->PrintQuotedChar(c);
  801. w->PrintEOL(_u(": Jump(L%04x)"), targetLabel);
  802. }
  803. #endif
  804. template <uint8 n>
  805. void SwitchMixin<n>::AddCase(char16 c, Label targetLabel)
  806. {
  807. AnalysisAssert(numCases < MaxCases);
  808. uint8 i;
  809. for (i = 0; i < numCases; i++)
  810. {
  811. Assert(cases[i].c != c);
  812. if (cases[i].c > c)
  813. {
  814. break;
  815. }
  816. }
  817. __analysis_assume(numCases < MaxCases);
  818. for (uint8 j = numCases; j > i; j--)
  819. {
  820. cases[j] = cases[j - 1];
  821. }
  822. cases[i].c = c;
  823. cases[i].targetLabel = targetLabel;
  824. numCases++;
  825. }
  826. void UnifiedRegexSwitchMixinForceAllInstantiations()
  827. {
  828. #if ENABLE_REGEX_CONFIG_OPTIONS
  829. #define SWITCH_FORCE_INSTANTIATION_PRINT x.Print(0, 0)
  830. #else
  831. #define SWITCH_FORCE_INSTANTIATION_PRINT
  832. #endif
  833. #define SWITCH_FORCE_INSTANTIATION(n) \
  834. { \
  835. SwitchMixin<n> x; \
  836. x.AddCase(0, 0); \
  837. SWITCH_FORCE_INSTANTIATION_PRINT; \
  838. }
  839. SWITCH_FORCE_INSTANTIATION(2);
  840. SWITCH_FORCE_INSTANTIATION(4);
  841. SWITCH_FORCE_INSTANTIATION(8);
  842. SWITCH_FORCE_INSTANTIATION(16);
  843. SWITCH_FORCE_INSTANTIATION(24);
  844. #undef SWITCH_FORCE_INSTANTIATION_PRINT
  845. #undef SWITCH_FORCE_INSTANTIATION
  846. }
  847. #if ENABLE_REGEX_CONFIG_OPTIONS
  848. template <uint8 n>
  849. void SwitchMixin<n>::Print(DebugWriter* w, const char16* litbuf) const
  850. {
  851. w->EOL();
  852. w->Indent();
  853. for (uint8 i = 0; i < numCases; i++)
  854. {
  855. cases[i].Print(w);
  856. }
  857. w->Unindent();
  858. }
  859. #endif
  860. // ----------------------------------------------------------------------
  861. // NopInst
  862. // ----------------------------------------------------------------------
  863. inline bool NopInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  864. {
  865. return false; // don't stop execution
  866. }
  867. #if ENABLE_REGEX_CONFIG_OPTIONS
  868. int NopInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  869. {
  870. PRINT_RE_BYTECODE_BEGIN("Nop");
  871. PRINT_RE_BYTECODE_MID();
  872. PRINT_BYTES(NopInst);
  873. PRINT_RE_BYTECODE_END();
  874. }
  875. #endif
  876. // ----------------------------------------------------------------------
  877. // FailInst
  878. // ----------------------------------------------------------------------
  879. inline bool FailInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  880. {
  881. return matcher.Fail(FAIL_PARAMETERS);
  882. }
  883. #if ENABLE_REGEX_CONFIG_OPTIONS
  884. int FailInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  885. {
  886. PRINT_RE_BYTECODE_BEGIN("Fail");
  887. PRINT_RE_BYTECODE_MID();
  888. PRINT_BYTES(NopInst);
  889. PRINT_RE_BYTECODE_END();
  890. }
  891. #endif
  892. // ----------------------------------------------------------------------
  893. // SuccInst
  894. // ----------------------------------------------------------------------
  895. inline bool SuccInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  896. {
  897. GroupInfo* info = matcher.GroupIdToGroupInfo(0);
  898. info->offset = matchStart;
  899. info->length = inputOffset - matchStart;
  900. return true; // STOP MATCHING
  901. }
  902. #if ENABLE_REGEX_CONFIG_OPTIONS
  903. int SuccInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  904. {
  905. PRINT_RE_BYTECODE_BEGIN("Succ");
  906. PRINT_RE_BYTECODE_MID();
  907. PRINT_BYTES(NopInst);
  908. PRINT_RE_BYTECODE_END();
  909. }
  910. #endif
  911. // ----------------------------------------------------------------------
  912. // JumpInst
  913. // ----------------------------------------------------------------------
  914. inline bool JumpInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  915. {
  916. instPointer = matcher.LabelToInstPointer(targetLabel);
  917. return false;
  918. }
  919. #if ENABLE_REGEX_CONFIG_OPTIONS
  920. int JumpInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  921. {
  922. PRINT_RE_BYTECODE_BEGIN("Jump");
  923. PRINT_MIXIN(JumpMixin);
  924. PRINT_RE_BYTECODE_MID();
  925. PRINT_BYTES(JumpMixin);
  926. PRINT_RE_BYTECODE_END();
  927. }
  928. #endif
  929. // ----------------------------------------------------------------------
  930. // JumpIfNotCharInst (optimized instruction)
  931. // ----------------------------------------------------------------------
  932. inline bool JumpIfNotCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  933. {
  934. #if ENABLE_REGEX_CONFIG_OPTIONS
  935. matcher.CompStats();
  936. #endif
  937. if (inputOffset < inputLength && input[inputOffset] == c)
  938. {
  939. instPointer += sizeof(*this);
  940. }
  941. else
  942. {
  943. instPointer = matcher.LabelToInstPointer(targetLabel);
  944. }
  945. return false;
  946. }
  947. #if ENABLE_REGEX_CONFIG_OPTIONS
  948. int JumpIfNotCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  949. {
  950. PRINT_RE_BYTECODE_BEGIN("JumpIfNotChar");
  951. PRINT_MIXIN_COMMA(CharMixin);
  952. PRINT_MIXIN(JumpMixin);
  953. PRINT_RE_BYTECODE_MID();
  954. PRINT_BYTES(CharMixin);
  955. PRINT_BYTES(JumpMixin);
  956. PRINT_RE_BYTECODE_END();
  957. }
  958. #endif
  959. // ----------------------------------------------------------------------
  960. // MatchCharOrJumpInst (optimized instruction)
  961. // ----------------------------------------------------------------------
  962. inline bool MatchCharOrJumpInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  963. {
  964. #if ENABLE_REGEX_CONFIG_OPTIONS
  965. matcher.CompStats();
  966. #endif
  967. if (inputOffset < inputLength && input[inputOffset] == c)
  968. {
  969. inputOffset++;
  970. instPointer += sizeof(*this);
  971. }
  972. else
  973. {
  974. instPointer = matcher.LabelToInstPointer(targetLabel);
  975. }
  976. return false;
  977. }
  978. #if ENABLE_REGEX_CONFIG_OPTIONS
  979. int MatchCharOrJumpInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  980. {
  981. PRINT_RE_BYTECODE_BEGIN("MatchCharOrJump");
  982. PRINT_MIXIN_COMMA(CharMixin);
  983. PRINT_MIXIN(JumpMixin);
  984. PRINT_RE_BYTECODE_MID();
  985. PRINT_BYTES(CharMixin);
  986. PRINT_BYTES(JumpMixin);
  987. PRINT_RE_BYTECODE_END();
  988. }
  989. #endif
  990. // ----------------------------------------------------------------------
  991. // JumpIfNotSetInst (optimized instruction)
  992. // ----------------------------------------------------------------------
  993. inline bool JumpIfNotSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  994. {
  995. #if ENABLE_REGEX_CONFIG_OPTIONS
  996. matcher.CompStats();
  997. #endif
  998. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  999. {
  1000. instPointer += sizeof(*this);
  1001. }
  1002. else
  1003. {
  1004. instPointer = matcher.LabelToInstPointer(targetLabel);
  1005. }
  1006. return false;
  1007. }
  1008. #if ENABLE_REGEX_CONFIG_OPTIONS
  1009. int JumpIfNotSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1010. {
  1011. PRINT_RE_BYTECODE_BEGIN("JumpIfNotSet");
  1012. PRINT_MIXIN_COMMA(SetMixin<false>);
  1013. PRINT_MIXIN(JumpMixin);
  1014. PRINT_RE_BYTECODE_MID();
  1015. PRINT_BYTES(SetMixin<false>);
  1016. PRINT_BYTES(JumpMixin);
  1017. PRINT_RE_BYTECODE_END();
  1018. }
  1019. #endif
  1020. // ----------------------------------------------------------------------
  1021. // MatchSetOrJumpInst (optimized instruction)
  1022. // ----------------------------------------------------------------------
  1023. inline bool MatchSetOrJumpInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1024. {
  1025. #if ENABLE_REGEX_CONFIG_OPTIONS
  1026. matcher.CompStats();
  1027. #endif
  1028. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  1029. {
  1030. inputOffset++;
  1031. instPointer += sizeof(*this);
  1032. }
  1033. else
  1034. {
  1035. instPointer = matcher.LabelToInstPointer(targetLabel);
  1036. }
  1037. return false;
  1038. }
  1039. #if ENABLE_REGEX_CONFIG_OPTIONS
  1040. int MatchSetOrJumpInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1041. {
  1042. PRINT_RE_BYTECODE_BEGIN("MatchSetOrJump");
  1043. PRINT_MIXIN_COMMA(SetMixin<false>);
  1044. PRINT_MIXIN(JumpMixin);
  1045. PRINT_RE_BYTECODE_MID();
  1046. PRINT_BYTES(SetMixin<false>);
  1047. PRINT_BYTES(JumpMixin);
  1048. PRINT_RE_BYTECODE_END();
  1049. }
  1050. #endif
  1051. // ----------------------------------------------------------------------
  1052. // Switch(AndConsume)Inst (optimized instructions)
  1053. // ----------------------------------------------------------------------
  1054. #if ENABLE_REGEX_CONFIG_OPTIONS
  1055. #define COMP_STATS matcher.CompStats()
  1056. #define SwitchAndConsumeInstPrintImpl(BaseName, n) \
  1057. int BaseName##n##Inst::Print(DebugWriter* w, Label label, const Char* litbuf) const \
  1058. { \
  1059. PRINT_RE_BYTECODE_BEGIN("SwitchAndConsume"#n); \
  1060. PRINT_MIXIN(SwitchMixin<n>); \
  1061. PRINT_RE_BYTECODE_MID(); \
  1062. PRINT_BYTES(SwitchMixin<n>); \
  1063. PRINT_RE_BYTECODE_END(); \
  1064. }
  1065. #else
  1066. #define COMP_STATS
  1067. #define SwitchAndConsumeInstPrintImpl(BaseName, n)
  1068. #endif
  1069. #define SwitchAndConsumeInstImpl(BaseName, n) \
  1070. inline bool BaseName##n##Inst::Exec(REGEX_INST_EXEC_PARAMETERS) const \
  1071. { \
  1072. if (inputOffset >= inputLength) \
  1073. { \
  1074. return matcher.Fail(FAIL_PARAMETERS); \
  1075. } \
  1076. \
  1077. const uint8 localNumCases = numCases; \
  1078. for (int i = 0; i < localNumCases; i++) \
  1079. { \
  1080. COMP_STATS; \
  1081. if (cases[i].c == input[inputOffset]) \
  1082. { \
  1083. CONSUME; \
  1084. instPointer = matcher.LabelToInstPointer(cases[i].targetLabel); \
  1085. return false; \
  1086. } \
  1087. else if (cases[i].c > input[inputOffset]) \
  1088. { \
  1089. break; \
  1090. } \
  1091. } \
  1092. \
  1093. instPointer += sizeof(*this); \
  1094. return false; \
  1095. } \
  1096. SwitchAndConsumeInstPrintImpl(BaseName, n);
  1097. #define CONSUME
  1098. SwitchAndConsumeInstImpl(Switch, 2);
  1099. SwitchAndConsumeInstImpl(Switch, 4);
  1100. SwitchAndConsumeInstImpl(Switch, 8);
  1101. SwitchAndConsumeInstImpl(Switch, 16);
  1102. SwitchAndConsumeInstImpl(Switch, 24);
  1103. #undef CONSUME
  1104. #define CONSUME inputOffset++
  1105. SwitchAndConsumeInstImpl(SwitchAndConsume, 2);
  1106. SwitchAndConsumeInstImpl(SwitchAndConsume, 4);
  1107. SwitchAndConsumeInstImpl(SwitchAndConsume, 8);
  1108. SwitchAndConsumeInstImpl(SwitchAndConsume, 16);
  1109. SwitchAndConsumeInstImpl(SwitchAndConsume, 24);
  1110. #undef CONSUME
  1111. #undef COMP_STATS
  1112. #undef SwitchAndConsumeInstPrintImpl
  1113. #undef SwitchAndConsumeInstImpl
  1114. // ----------------------------------------------------------------------
  1115. // BOITestInst
  1116. // ----------------------------------------------------------------------
  1117. template <>
  1118. BOITestInst<true>::BOITestInst() : Inst(InstTag::BOIHardFailTest) {}
  1119. template <>
  1120. BOITestInst<false>::BOITestInst() : Inst(InstTag::BOITest) {}
  1121. template <bool canHardFail>
  1122. inline bool BOITestInst<canHardFail>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1123. {
  1124. if (inputOffset > 0)
  1125. {
  1126. if (canHardFail)
  1127. {
  1128. // Clearly trying to start from later in the input won't help, and we know backtracking can't take us earlier in the input
  1129. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  1130. }
  1131. else
  1132. {
  1133. return matcher.Fail(FAIL_PARAMETERS);
  1134. }
  1135. }
  1136. instPointer += sizeof(*this);
  1137. return false;
  1138. }
  1139. #if ENABLE_REGEX_CONFIG_OPTIONS
  1140. template <bool canHardFail>
  1141. int BOITestInst<canHardFail>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1142. {
  1143. if (canHardFail)
  1144. {
  1145. PRINT_RE_BYTECODE_BEGIN("BOIHardFailTest");
  1146. }
  1147. else
  1148. {
  1149. PRINT_RE_BYTECODE_BEGIN("BOITest");
  1150. }
  1151. w->Print(_u("<hardFail>: %s"), canHardFail ? _u("true") : _u("false"));
  1152. PRINT_RE_BYTECODE_MID();
  1153. PRINT_RE_BYTECODE_END();
  1154. }
  1155. #endif
  1156. // ----------------------------------------------------------------------
  1157. // EOITestInst
  1158. // ----------------------------------------------------------------------
  1159. template <>
  1160. EOITestInst<true>::EOITestInst() : Inst(InstTag::EOIHardFailTest) {}
  1161. template <>
  1162. EOITestInst<false>::EOITestInst() : Inst(InstTag::EOITest) {}
  1163. template <bool canHardFail>
  1164. inline bool EOITestInst<canHardFail>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1165. {
  1166. if (inputOffset < inputLength)
  1167. {
  1168. if (canHardFail)
  1169. {
  1170. // We know backtracking can never take us later in the input, but starting from later in the input could help
  1171. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::LaterOnly));
  1172. }
  1173. else
  1174. {
  1175. return matcher.Fail(FAIL_PARAMETERS);
  1176. }
  1177. }
  1178. instPointer += sizeof(*this);
  1179. return false;
  1180. }
  1181. #if ENABLE_REGEX_CONFIG_OPTIONS
  1182. template <bool canHardFail>
  1183. int EOITestInst<canHardFail>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1184. {
  1185. if (canHardFail)
  1186. {
  1187. PRINT_RE_BYTECODE_BEGIN("EOIHardFailTest");
  1188. }
  1189. else
  1190. {
  1191. PRINT_RE_BYTECODE_BEGIN("EOITest");
  1192. }
  1193. w->Print(_u("<hardFail>: %s"), canHardFail ? _u("true") : _u("false"));
  1194. PRINT_RE_BYTECODE_MID();
  1195. PRINT_RE_BYTECODE_END();
  1196. }
  1197. #endif
  1198. // ----------------------------------------------------------------------
  1199. // BOLTestInst
  1200. // ----------------------------------------------------------------------
  1201. inline bool BOLTestInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1202. {
  1203. #if ENABLE_REGEX_CONFIG_OPTIONS
  1204. matcher.CompStats();
  1205. #endif
  1206. if (inputOffset > 0 && !matcher.standardChars->IsNewline(input[inputOffset - 1]))
  1207. {
  1208. return matcher.Fail(FAIL_PARAMETERS);
  1209. }
  1210. instPointer += sizeof(*this);
  1211. return false;
  1212. }
  1213. #if ENABLE_REGEX_CONFIG_OPTIONS
  1214. int BOLTestInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1215. {
  1216. PRINT_RE_BYTECODE_BEGIN("BOLTest");
  1217. PRINT_RE_BYTECODE_MID();
  1218. PRINT_RE_BYTECODE_END();
  1219. }
  1220. #endif
  1221. // ----------------------------------------------------------------------
  1222. // EOLTestInst
  1223. // ----------------------------------------------------------------------
  1224. inline bool EOLTestInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1225. {
  1226. #if ENABLE_REGEX_CONFIG_OPTIONS
  1227. matcher.CompStats();
  1228. #endif
  1229. if (inputOffset < inputLength && !matcher.standardChars->IsNewline(input[inputOffset]))
  1230. {
  1231. return matcher.Fail(FAIL_PARAMETERS);
  1232. }
  1233. instPointer += sizeof(*this);
  1234. return false;
  1235. }
  1236. #if ENABLE_REGEX_CONFIG_OPTIONS
  1237. int EOLTestInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1238. {
  1239. PRINT_RE_BYTECODE_BEGIN("EOLTest");
  1240. PRINT_RE_BYTECODE_MID();
  1241. PRINT_RE_BYTECODE_END();
  1242. }
  1243. #endif
  1244. // ----------------------------------------------------------------------
  1245. // WordBoundaryTestInst
  1246. // ----------------------------------------------------------------------
  1247. template <>
  1248. WordBoundaryTestInst<true>::WordBoundaryTestInst() : Inst(InstTag::NegatedWordBoundaryTest) {}
  1249. template <>
  1250. WordBoundaryTestInst<false>::WordBoundaryTestInst() : Inst(InstTag::WordBoundaryTest) {}
  1251. template <bool isNegation>
  1252. inline bool WordBoundaryTestInst<isNegation>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1253. {
  1254. #if ENABLE_REGEX_CONFIG_OPTIONS
  1255. matcher.CompStats();
  1256. #endif
  1257. const bool prev = inputOffset > 0 && matcher.standardChars->IsWord(input[inputOffset - 1]);
  1258. const bool curr = inputOffset < inputLength && matcher.standardChars->IsWord(input[inputOffset]);
  1259. if (isNegation == (prev != curr))
  1260. {
  1261. return matcher.Fail(FAIL_PARAMETERS);
  1262. }
  1263. instPointer += sizeof(*this);
  1264. return false;
  1265. }
  1266. #if ENABLE_REGEX_CONFIG_OPTIONS
  1267. template <bool isNegation>
  1268. int WordBoundaryTestInst<isNegation>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1269. {
  1270. if (isNegation)
  1271. {
  1272. PRINT_RE_BYTECODE_BEGIN("NegatedWordBoundaryTest");
  1273. }
  1274. else
  1275. {
  1276. PRINT_RE_BYTECODE_BEGIN("WordBoundaryTest");
  1277. }
  1278. PRINT_RE_BYTECODE_MID();
  1279. PRINT_RE_BYTECODE_END();
  1280. }
  1281. #endif
  1282. // ----------------------------------------------------------------------
  1283. // MatchCharInst
  1284. // ----------------------------------------------------------------------
  1285. inline bool MatchCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1286. {
  1287. #if ENABLE_REGEX_CONFIG_OPTIONS
  1288. matcher.CompStats();
  1289. #endif
  1290. if (inputOffset >= inputLength || input[inputOffset] != c)
  1291. {
  1292. return matcher.Fail(FAIL_PARAMETERS);
  1293. }
  1294. inputOffset++;
  1295. instPointer += sizeof(*this);
  1296. return false;
  1297. }
  1298. #if ENABLE_REGEX_CONFIG_OPTIONS
  1299. int MatchCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1300. {
  1301. PRINT_RE_BYTECODE_BEGIN("MatchChar");
  1302. PRINT_MIXIN(CharMixin);
  1303. PRINT_RE_BYTECODE_MID();
  1304. PRINT_BYTES(CharMixin);
  1305. PRINT_RE_BYTECODE_END();
  1306. }
  1307. #endif
  1308. // ----------------------------------------------------------------------
  1309. // MatchChar2Inst
  1310. // ----------------------------------------------------------------------
  1311. inline bool MatchChar2Inst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1312. {
  1313. #if ENABLE_REGEX_CONFIG_OPTIONS
  1314. matcher.CompStats();
  1315. #endif
  1316. if (inputOffset >= inputLength || (input[inputOffset] != cs[0] && input[inputOffset] != cs[1]))
  1317. {
  1318. return matcher.Fail(FAIL_PARAMETERS);
  1319. }
  1320. inputOffset++;
  1321. instPointer += sizeof(*this);
  1322. return false;
  1323. }
  1324. #if ENABLE_REGEX_CONFIG_OPTIONS
  1325. int MatchChar2Inst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1326. {
  1327. PRINT_RE_BYTECODE_BEGIN("MatchChar2");
  1328. PRINT_MIXIN(Char2Mixin);
  1329. PRINT_RE_BYTECODE_MID();
  1330. PRINT_BYTES(Char2Mixin);
  1331. PRINT_RE_BYTECODE_END();
  1332. }
  1333. #endif
  1334. // ----------------------------------------------------------------------
  1335. // MatchChar3Inst
  1336. // ----------------------------------------------------------------------
  1337. inline bool MatchChar3Inst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1338. {
  1339. #if ENABLE_REGEX_CONFIG_OPTIONS
  1340. matcher.CompStats();
  1341. #endif
  1342. if (inputOffset >= inputLength || (input[inputOffset] != cs[0] && input[inputOffset] != cs[1] && input[inputOffset] != cs[2]))
  1343. {
  1344. return matcher.Fail(FAIL_PARAMETERS);
  1345. }
  1346. inputOffset++;
  1347. instPointer += sizeof(*this);
  1348. return false;
  1349. }
  1350. #if ENABLE_REGEX_CONFIG_OPTIONS
  1351. int MatchChar3Inst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1352. {
  1353. PRINT_RE_BYTECODE_BEGIN("MatchChar3");
  1354. PRINT_MIXIN(Char3Mixin);
  1355. PRINT_RE_BYTECODE_MID();
  1356. PRINT_BYTES(Char3Mixin);
  1357. PRINT_RE_BYTECODE_END();
  1358. }
  1359. #endif
  1360. // ----------------------------------------------------------------------
  1361. // MatchChar4Inst
  1362. // ----------------------------------------------------------------------
  1363. inline bool MatchChar4Inst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1364. {
  1365. #if ENABLE_REGEX_CONFIG_OPTIONS
  1366. matcher.CompStats();
  1367. #endif
  1368. if (inputOffset >= inputLength || (input[inputOffset] != cs[0] && input[inputOffset] != cs[1] && input[inputOffset] != cs[2] && input[inputOffset] != cs[3]))
  1369. {
  1370. return matcher.Fail(FAIL_PARAMETERS);
  1371. }
  1372. inputOffset++;
  1373. instPointer += sizeof(*this);
  1374. return false;
  1375. }
  1376. #if ENABLE_REGEX_CONFIG_OPTIONS
  1377. int MatchChar4Inst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1378. {
  1379. PRINT_RE_BYTECODE_BEGIN("MatchChar4");
  1380. PRINT_MIXIN(Char4Mixin);
  1381. PRINT_RE_BYTECODE_MID();
  1382. PRINT_BYTES(Char4Mixin);
  1383. PRINT_RE_BYTECODE_END();
  1384. }
  1385. #endif
  1386. // ----------------------------------------------------------------------
  1387. // MatchSetInst
  1388. // ----------------------------------------------------------------------
  1389. template<bool IsNegation>
  1390. inline bool MatchSetInst<IsNegation>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1391. {
  1392. #if ENABLE_REGEX_CONFIG_OPTIONS
  1393. matcher.CompStats();
  1394. #endif
  1395. if (inputOffset >= inputLength || this->set.Get(input[inputOffset]) == IsNegation)
  1396. {
  1397. return matcher.Fail(FAIL_PARAMETERS);
  1398. }
  1399. inputOffset++;
  1400. instPointer += sizeof(*this);
  1401. return false;
  1402. }
  1403. #if ENABLE_REGEX_CONFIG_OPTIONS
  1404. template<bool IsNegation>
  1405. int MatchSetInst<IsNegation>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1406. {
  1407. if (IsNegation)
  1408. {
  1409. PRINT_RE_BYTECODE_BEGIN("MatchNegatedSet");
  1410. PRINT_MIXIN(SetMixin<true>);
  1411. }
  1412. else
  1413. {
  1414. PRINT_RE_BYTECODE_BEGIN("MatchSet");
  1415. PRINT_MIXIN(SetMixin<false>);
  1416. }
  1417. PRINT_RE_BYTECODE_MID();
  1418. IsNegation ? PRINT_BYTES(SetMixin<true>) : PRINT_BYTES(SetMixin<false>);
  1419. PRINT_RE_BYTECODE_END();
  1420. }
  1421. #endif
  1422. // ----------------------------------------------------------------------
  1423. // MatchLiteralInst
  1424. // ----------------------------------------------------------------------
  1425. inline bool MatchLiteralInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1426. {
  1427. Assert(length <= matcher.program->rep.insts.litbufLen - offset);
  1428. if (length > inputLength - inputOffset)
  1429. {
  1430. return matcher.Fail(FAIL_PARAMETERS);
  1431. }
  1432. const Char *const literalBuffer = matcher.program->rep.insts.litbuf;
  1433. const Char * literalCurr = literalBuffer + offset;
  1434. const Char * inputCurr = input + inputOffset;
  1435. #if ENABLE_REGEX_CONFIG_OPTIONS
  1436. matcher.CompStats();
  1437. #endif
  1438. if (*literalCurr != *inputCurr)
  1439. {
  1440. inputOffset++;
  1441. return matcher.Fail(FAIL_PARAMETERS);
  1442. }
  1443. const Char *const literalEnd = literalCurr + length;
  1444. literalCurr++;
  1445. inputCurr++;
  1446. while (literalCurr < literalEnd)
  1447. {
  1448. #if ENABLE_REGEX_CONFIG_OPTIONS
  1449. matcher.CompStats();
  1450. #endif
  1451. if (*literalCurr != *inputCurr++)
  1452. {
  1453. inputOffset = (CharCount)(inputCurr - input);
  1454. return matcher.Fail(FAIL_PARAMETERS);
  1455. }
  1456. literalCurr++;
  1457. }
  1458. inputOffset = (CharCount)(inputCurr - input);
  1459. instPointer += sizeof(*this);
  1460. return false;
  1461. }
  1462. #if ENABLE_REGEX_CONFIG_OPTIONS
  1463. int MatchLiteralInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1464. {
  1465. PRINT_RE_BYTECODE_BEGIN("MatchLiteral");
  1466. PRINT_MIXIN_ARGS(LiteralMixin, false);
  1467. PRINT_RE_BYTECODE_MID();
  1468. PRINT_BYTES(LiteralMixin);
  1469. PRINT_RE_BYTECODE_END();
  1470. }
  1471. #endif
  1472. // ----------------------------------------------------------------------
  1473. // MatchLiteralEquivInst
  1474. // ----------------------------------------------------------------------
  1475. inline bool MatchLiteralEquivInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1476. {
  1477. if (length > inputLength - inputOffset)
  1478. {
  1479. return matcher.Fail(FAIL_PARAMETERS);
  1480. }
  1481. const Char *const literalBuffer = matcher.program->rep.insts.litbuf;
  1482. CharCount literalOffset = offset;
  1483. const CharCount literalEndOffset = offset + length * CaseInsensitive::EquivClassSize;
  1484. Assert(literalEndOffset <= matcher.program->rep.insts.litbufLen);
  1485. CompileAssert(CaseInsensitive::EquivClassSize == 4);
  1486. do
  1487. {
  1488. #if ENABLE_REGEX_CONFIG_OPTIONS
  1489. matcher.CompStats();
  1490. #endif
  1491. if (input[inputOffset] != literalBuffer[literalOffset]
  1492. && input[inputOffset] != literalBuffer[literalOffset + 1]
  1493. && input[inputOffset] != literalBuffer[literalOffset + 2]
  1494. && input[inputOffset] != literalBuffer[literalOffset + 3])
  1495. {
  1496. return matcher.Fail(FAIL_PARAMETERS);
  1497. }
  1498. inputOffset++;
  1499. literalOffset += CaseInsensitive::EquivClassSize;
  1500. }
  1501. while (literalOffset < literalEndOffset);
  1502. instPointer += sizeof(*this);
  1503. return false;
  1504. }
  1505. #if ENABLE_REGEX_CONFIG_OPTIONS
  1506. int MatchLiteralEquivInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1507. {
  1508. PRINT_RE_BYTECODE_BEGIN("MatchLiteralEquiv");
  1509. PRINT_MIXIN_ARGS(LiteralMixin, true);
  1510. PRINT_RE_BYTECODE_MID();
  1511. PRINT_BYTES(LiteralMixin);
  1512. PRINT_RE_BYTECODE_END();
  1513. }
  1514. #endif
  1515. // ----------------------------------------------------------------------
  1516. // MatchTrieInst (optimized instruction)
  1517. // ----------------------------------------------------------------------
  1518. inline bool MatchTrieInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1519. {
  1520. if (!trie.Match(
  1521. input
  1522. , inputLength
  1523. , inputOffset
  1524. #if ENABLE_REGEX_CONFIG_OPTIONS
  1525. , matcher.stats
  1526. #endif
  1527. ))
  1528. {
  1529. return matcher.Fail(FAIL_PARAMETERS);
  1530. }
  1531. instPointer += sizeof(*this);
  1532. return false;
  1533. }
  1534. void MatchTrieInst::FreeBody(ArenaAllocator* rtAllocator)
  1535. {
  1536. trie.FreeBody(rtAllocator);
  1537. }
  1538. #if ENABLE_REGEX_CONFIG_OPTIONS
  1539. int MatchTrieInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1540. {
  1541. PRINT_RE_BYTECODE_BEGIN("MatchTrie");
  1542. PRINT_MIXIN(TrieMixin);
  1543. PRINT_RE_BYTECODE_MID();
  1544. PRINT_BYTES(TrieMixin);
  1545. PRINT_RE_BYTECODE_END();
  1546. }
  1547. #endif
  1548. // ----------------------------------------------------------------------
  1549. // OptMatchCharInst (optimized instruction)
  1550. // ----------------------------------------------------------------------
  1551. inline bool OptMatchCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1552. {
  1553. #if ENABLE_REGEX_CONFIG_OPTIONS
  1554. matcher.CompStats();
  1555. #endif
  1556. if (inputOffset < inputLength && input[inputOffset] == c)
  1557. {
  1558. inputOffset++;
  1559. }
  1560. instPointer += sizeof(*this);
  1561. return false;
  1562. }
  1563. #if ENABLE_REGEX_CONFIG_OPTIONS
  1564. int OptMatchCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1565. {
  1566. PRINT_RE_BYTECODE_BEGIN("OptMatchChar");
  1567. PRINT_MIXIN(CharMixin);
  1568. PRINT_RE_BYTECODE_MID();
  1569. PRINT_BYTES(CharMixin);
  1570. PRINT_RE_BYTECODE_END();
  1571. }
  1572. #endif
  1573. // ----------------------------------------------------------------------
  1574. // OptMatchSetInst (optimized instruction)
  1575. // ----------------------------------------------------------------------
  1576. inline bool OptMatchSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1577. {
  1578. #if ENABLE_REGEX_CONFIG_OPTIONS
  1579. matcher.CompStats();
  1580. #endif
  1581. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  1582. {
  1583. inputOffset++;
  1584. }
  1585. instPointer += sizeof(*this);
  1586. return false;
  1587. }
  1588. #if ENABLE_REGEX_CONFIG_OPTIONS
  1589. int OptMatchSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1590. {
  1591. PRINT_RE_BYTECODE_BEGIN("OptMatchSet");
  1592. PRINT_MIXIN(SetMixin<false>);
  1593. PRINT_RE_BYTECODE_MID();
  1594. PRINT_BYTES(SetMixin<false>);
  1595. PRINT_RE_BYTECODE_END();
  1596. }
  1597. #endif
  1598. // ----------------------------------------------------------------------
  1599. // SyncToCharAndContinueInst (optimized instruction)
  1600. // ----------------------------------------------------------------------
  1601. inline bool SyncToCharAndContinueInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1602. {
  1603. const Char matchC = c;
  1604. #if ENABLE_REGEX_CONFIG_OPTIONS
  1605. matcher.CompStats();
  1606. #endif
  1607. while (inputOffset < inputLength && input[inputOffset] != matchC)
  1608. {
  1609. #if ENABLE_REGEX_CONFIG_OPTIONS
  1610. matcher.CompStats();
  1611. #endif
  1612. inputOffset++;
  1613. }
  1614. matchStart = inputOffset;
  1615. instPointer += sizeof(*this);
  1616. return false;
  1617. }
  1618. #if ENABLE_REGEX_CONFIG_OPTIONS
  1619. int SyncToCharAndContinueInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1620. {
  1621. PRINT_RE_BYTECODE_BEGIN("SyncToCharAndContinue");
  1622. PRINT_MIXIN(CharMixin);
  1623. PRINT_RE_BYTECODE_MID();
  1624. PRINT_BYTES(CharMixin);
  1625. PRINT_RE_BYTECODE_END();
  1626. }
  1627. #endif
  1628. // ----------------------------------------------------------------------
  1629. // SyncToChar2SetAndContinueInst (optimized instruction)
  1630. // ----------------------------------------------------------------------
  1631. inline bool SyncToChar2SetAndContinueInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1632. {
  1633. const Char matchC0 = cs[0];
  1634. const Char matchC1 = cs[1];
  1635. #if ENABLE_REGEX_CONFIG_OPTIONS
  1636. matcher.CompStats();
  1637. #endif
  1638. while (inputOffset < inputLength && input[inputOffset] != matchC0 && input[inputOffset] != matchC1)
  1639. {
  1640. #if ENABLE_REGEX_CONFIG_OPTIONS
  1641. matcher.CompStats();
  1642. #endif
  1643. inputOffset++;
  1644. }
  1645. matchStart = inputOffset;
  1646. instPointer += sizeof(*this);
  1647. return false;
  1648. }
  1649. #if ENABLE_REGEX_CONFIG_OPTIONS
  1650. int SyncToChar2SetAndContinueInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1651. {
  1652. PRINT_RE_BYTECODE_BEGIN("SyncToChar2SetAndContinue");
  1653. PRINT_MIXIN(Char2Mixin);
  1654. PRINT_RE_BYTECODE_MID();
  1655. PRINT_BYTES(Char2Mixin);
  1656. PRINT_RE_BYTECODE_END();
  1657. }
  1658. #endif
  1659. // ----------------------------------------------------------------------
  1660. // SyncToSetAndContinueInst (optimized instruction)
  1661. // ----------------------------------------------------------------------
  1662. template<bool IsNegation>
  1663. inline bool SyncToSetAndContinueInst<IsNegation>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1664. {
  1665. const RuntimeCharSet<Char>& matchSet = this->set;
  1666. #if ENABLE_REGEX_CONFIG_OPTIONS
  1667. matcher.CompStats();
  1668. #endif
  1669. while (inputOffset < inputLength && matchSet.Get(input[inputOffset]) == IsNegation)
  1670. {
  1671. #if ENABLE_REGEX_CONFIG_OPTIONS
  1672. matcher.CompStats();
  1673. #endif
  1674. inputOffset++;
  1675. }
  1676. matchStart = inputOffset;
  1677. instPointer += sizeof(*this);
  1678. return false;
  1679. }
  1680. #if ENABLE_REGEX_CONFIG_OPTIONS
  1681. template<bool IsNegation>
  1682. int SyncToSetAndContinueInst<IsNegation>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1683. {
  1684. if (IsNegation)
  1685. {
  1686. PRINT_RE_BYTECODE_BEGIN("SyncToNegatedSetAndContinue");
  1687. PRINT_MIXIN(SetMixin<true>);
  1688. }
  1689. else
  1690. {
  1691. PRINT_RE_BYTECODE_BEGIN("SyncToSetAndContinue");
  1692. PRINT_MIXIN(SetMixin<false>);
  1693. }
  1694. PRINT_RE_BYTECODE_MID();
  1695. IsNegation ? PRINT_BYTES(SetMixin<true>) : PRINT_BYTES(SetMixin<false>);
  1696. PRINT_RE_BYTECODE_END();
  1697. }
  1698. #endif
  1699. // ----------------------------------------------------------------------
  1700. // SyncToLiteralAndContinueInst (optimized instruction)
  1701. // ----------------------------------------------------------------------
  1702. template <typename ScannerT>
  1703. inline bool SyncToLiteralAndContinueInstT<ScannerT>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1704. {
  1705. if (!this->Match(matcher, input, inputLength, inputOffset))
  1706. {
  1707. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  1708. }
  1709. matchStart = inputOffset;
  1710. instPointer += sizeof(*this);
  1711. return false;
  1712. }
  1713. #if ENABLE_REGEX_CONFIG_OPTIONS
  1714. // explicit instantiation
  1715. template struct SyncToLiteralAndContinueInstT<Char2LiteralScannerMixin>;
  1716. template struct SyncToLiteralAndContinueInstT<ScannerMixin>;
  1717. template struct SyncToLiteralAndContinueInstT<ScannerMixin_WithLinearCharMap>;
  1718. template struct SyncToLiteralAndContinueInstT<EquivScannerMixin>;
  1719. template struct SyncToLiteralAndContinueInstT<EquivTrivialLastPatCharScannerMixin>;
  1720. // Explicitly define each of these 5 Print functions so that the output will show the actual template param mixin and
  1721. // actual opcode name, even though the logic is basically the same in each definition. See notes below.
  1722. template <>
  1723. int SyncToLiteralAndContinueInstT<Char2LiteralScannerMixin>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1724. {
  1725. // NOTE: this text is unique to this instantiation
  1726. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndContinueInstT<Char2LiteralScannerMixin> aka SyncToChar2LiteralAndContinue");
  1727. PRINT_MIXIN(Char2LiteralScannerMixin); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  1728. PRINT_RE_BYTECODE_MID();
  1729. PRINT_BYTES(Char2LiteralScannerMixin); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  1730. PRINT_RE_BYTECODE_END();
  1731. }
  1732. template <>
  1733. int SyncToLiteralAndContinueInstT<ScannerMixin>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1734. {
  1735. // NOTE: this text is unique to this instantiation
  1736. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndContinueInstT<ScannerMixin> aka SyncToLiteralAndContinue");
  1737. PRINT_MIXIN(ScannerMixin); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  1738. PRINT_RE_BYTECODE_MID();
  1739. PRINT_BYTES(ScannerMixin); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  1740. PRINT_RE_BYTECODE_END();
  1741. }
  1742. template <>
  1743. int SyncToLiteralAndContinueInstT<ScannerMixin_WithLinearCharMap>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1744. {
  1745. // NOTE: this text is unique to this instantiation
  1746. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndContinueInstT<ScannerMixin_WithLinearCharMap> aka SyncToLinearLiteralAndContinue");
  1747. PRINT_MIXIN(ScannerMixin_WithLinearCharMap); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  1748. PRINT_RE_BYTECODE_MID();
  1749. PRINT_BYTES(ScannerMixin_WithLinearCharMap); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  1750. PRINT_RE_BYTECODE_END();
  1751. }
  1752. template <>
  1753. int SyncToLiteralAndContinueInstT<EquivScannerMixin>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1754. {
  1755. // NOTE: this text is unique to this instantiation
  1756. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndContinueInstT<EquivScannerMixin> aka SyncToLiteralEquivAndContinue");
  1757. PRINT_MIXIN(EquivScannerMixin); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  1758. PRINT_RE_BYTECODE_MID();
  1759. PRINT_BYTES(EquivScannerMixin); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  1760. PRINT_RE_BYTECODE_END();
  1761. }
  1762. template <>
  1763. int SyncToLiteralAndContinueInstT<EquivTrivialLastPatCharScannerMixin>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1764. {
  1765. // NOTE: this text is unique to this instantiation
  1766. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndContinueInstT<EquivTrivialLastPatCharScannerMixin> aka SyncToLiteralEquivTrivialLastPatCharAndContinue");
  1767. PRINT_MIXIN(EquivTrivialLastPatCharScannerMixin); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  1768. PRINT_RE_BYTECODE_MID();
  1769. PRINT_BYTES(EquivTrivialLastPatCharScannerMixin); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  1770. PRINT_RE_BYTECODE_END();
  1771. }
  1772. #endif
  1773. // ----------------------------------------------------------------------
  1774. // SyncToCharAndConsumeInst (optimized instruction)
  1775. // ----------------------------------------------------------------------
  1776. inline bool SyncToCharAndConsumeInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1777. {
  1778. const Char matchC = c;
  1779. #if ENABLE_REGEX_CONFIG_OPTIONS
  1780. matcher.CompStats();
  1781. #endif
  1782. while (inputOffset < inputLength && input[inputOffset] != matchC)
  1783. {
  1784. #if ENABLE_REGEX_CONFIG_OPTIONS
  1785. matcher.CompStats();
  1786. #endif
  1787. inputOffset++;
  1788. }
  1789. if (inputOffset >= inputLength)
  1790. {
  1791. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  1792. }
  1793. matchStart = inputOffset++;
  1794. instPointer += sizeof(*this);
  1795. return false;
  1796. }
  1797. #if ENABLE_REGEX_CONFIG_OPTIONS
  1798. int SyncToCharAndConsumeInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1799. {
  1800. PRINT_RE_BYTECODE_BEGIN("SyncToCharAndConsume");
  1801. PRINT_MIXIN(CharMixin);
  1802. PRINT_RE_BYTECODE_MID();
  1803. PRINT_BYTES(CharMixin);
  1804. PRINT_RE_BYTECODE_END();
  1805. }
  1806. #endif
  1807. // ----------------------------------------------------------------------
  1808. // SyncToChar2SetAndConsumeInst (optimized instruction)
  1809. // ----------------------------------------------------------------------
  1810. inline bool SyncToChar2SetAndConsumeInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1811. {
  1812. const Char matchC0 = cs[0];
  1813. const Char matchC1 = cs[1];
  1814. #if ENABLE_REGEX_CONFIG_OPTIONS
  1815. matcher.CompStats();
  1816. #endif
  1817. while (inputOffset < inputLength && (input[inputOffset] != matchC0 && input[inputOffset] != matchC1))
  1818. {
  1819. #if ENABLE_REGEX_CONFIG_OPTIONS
  1820. matcher.CompStats();
  1821. #endif
  1822. inputOffset++;
  1823. }
  1824. if (inputOffset >= inputLength)
  1825. {
  1826. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  1827. }
  1828. matchStart = inputOffset++;
  1829. instPointer += sizeof(*this);
  1830. return false;
  1831. }
  1832. #if ENABLE_REGEX_CONFIG_OPTIONS
  1833. int SyncToChar2SetAndConsumeInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1834. {
  1835. PRINT_RE_BYTECODE_BEGIN("SyncToChar2SetAndConsume");
  1836. PRINT_MIXIN(Char2Mixin);
  1837. PRINT_RE_BYTECODE_MID();
  1838. PRINT_BYTES(Char2Mixin);
  1839. PRINT_RE_BYTECODE_END();
  1840. }
  1841. #endif
  1842. // ----------------------------------------------------------------------
  1843. // SyncToSetAndConsumeInst (optimized instruction)
  1844. // ----------------------------------------------------------------------
  1845. template<bool IsNegation>
  1846. inline bool SyncToSetAndConsumeInst<IsNegation>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1847. {
  1848. const RuntimeCharSet<Char>& matchSet = this->set;
  1849. #if ENABLE_REGEX_CONFIG_OPTIONS
  1850. matcher.CompStats();
  1851. #endif
  1852. while (inputOffset < inputLength && matchSet.Get(input[inputOffset]) == IsNegation)
  1853. {
  1854. #if ENABLE_REGEX_CONFIG_OPTIONS
  1855. matcher.CompStats();
  1856. #endif
  1857. inputOffset++;
  1858. }
  1859. if (inputOffset >= inputLength)
  1860. {
  1861. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  1862. }
  1863. matchStart = inputOffset++;
  1864. instPointer += sizeof(*this);
  1865. return false;
  1866. }
  1867. #if ENABLE_REGEX_CONFIG_OPTIONS
  1868. template<bool IsNegation>
  1869. int SyncToSetAndConsumeInst<IsNegation>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1870. {
  1871. if (IsNegation)
  1872. {
  1873. PRINT_RE_BYTECODE_BEGIN("SyncToNegatedSetAndConsume");
  1874. PRINT_MIXIN(SetMixin<true>);
  1875. }
  1876. else
  1877. {
  1878. PRINT_RE_BYTECODE_BEGIN("SyncToSetAndConsume");
  1879. PRINT_MIXIN(SetMixin<false>);
  1880. }
  1881. PRINT_RE_BYTECODE_MID();
  1882. IsNegation ? PRINT_BYTES(SetMixin<true>) : PRINT_BYTES(SetMixin<false>);
  1883. PRINT_RE_BYTECODE_END();
  1884. }
  1885. #endif
  1886. // ----------------------------------------------------------------------
  1887. // SyncToLiteralAndConsumeInst (optimized instruction)
  1888. // ----------------------------------------------------------------------
  1889. template <typename ScannerT>
  1890. inline bool SyncToLiteralAndConsumeInstT<ScannerT>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1891. {
  1892. if (!this->Match(matcher, input, inputLength, inputOffset))
  1893. {
  1894. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  1895. }
  1896. matchStart = inputOffset;
  1897. inputOffset += ScannerT::GetLiteralLength();
  1898. instPointer += sizeof(*this);
  1899. return false;
  1900. }
  1901. #if ENABLE_REGEX_CONFIG_OPTIONS
  1902. // explicit instantiation
  1903. template struct SyncToLiteralAndConsumeInstT<Char2LiteralScannerMixin>;
  1904. template struct SyncToLiteralAndConsumeInstT<ScannerMixin>;
  1905. template struct SyncToLiteralAndConsumeInstT<ScannerMixin_WithLinearCharMap>;
  1906. template struct SyncToLiteralAndConsumeInstT<EquivScannerMixin>;
  1907. template struct SyncToLiteralAndConsumeInstT<EquivTrivialLastPatCharScannerMixin>;
  1908. // Explicitly define each of these 5 Print functions so that the output will show the actual template param mixin and
  1909. // actual opcode name, even though the logic is basically the same in each definition. See notes below.
  1910. template <>
  1911. int SyncToLiteralAndConsumeInstT<Char2LiteralScannerMixin>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1912. {
  1913. // NOTE: this text is unique to this instantiation
  1914. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndConsumeInstT<Char2LiteralScannerMixin> aka SyncToChar2LiteralAndConsume");
  1915. PRINT_MIXIN(Char2LiteralScannerMixin); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  1916. PRINT_RE_BYTECODE_MID();
  1917. PRINT_BYTES(Char2LiteralScannerMixin); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  1918. PRINT_RE_BYTECODE_END();
  1919. }
  1920. template <>
  1921. int SyncToLiteralAndConsumeInstT<ScannerMixin>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1922. {
  1923. // NOTE: this text is unique to this instantiation
  1924. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndConsumeInstT<ScannerMixin> aka SyncToLiteralAndConsume");
  1925. PRINT_MIXIN(ScannerMixin); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  1926. PRINT_RE_BYTECODE_MID();
  1927. PRINT_BYTES(ScannerMixin); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  1928. PRINT_RE_BYTECODE_END();
  1929. }
  1930. template <>
  1931. int SyncToLiteralAndConsumeInstT<ScannerMixin_WithLinearCharMap>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1932. {
  1933. // NOTE: this text is unique to this instantiation
  1934. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndConsumeInstT<ScannerMixin_WithLinearCharMap> aka SyncToLinearLiteralAndConsume");
  1935. PRINT_MIXIN(ScannerMixin_WithLinearCharMap); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  1936. PRINT_RE_BYTECODE_MID();
  1937. PRINT_BYTES(ScannerMixin_WithLinearCharMap); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  1938. PRINT_RE_BYTECODE_END();
  1939. }
  1940. template <>
  1941. int SyncToLiteralAndConsumeInstT<EquivScannerMixin>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1942. {
  1943. // NOTE: this text is unique to this instantiation
  1944. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndConsumeInstT<EquivScannerMixin> aka SyncToLiteralEquivAndConsume");
  1945. PRINT_MIXIN(EquivScannerMixin); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  1946. PRINT_RE_BYTECODE_MID();
  1947. PRINT_BYTES(EquivScannerMixin); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  1948. PRINT_RE_BYTECODE_END();
  1949. }
  1950. template <>
  1951. int SyncToLiteralAndConsumeInstT<EquivTrivialLastPatCharScannerMixin>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  1952. {
  1953. // NOTE: this text is unique to this instantiation
  1954. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndConsumeInstT<EquivTrivialLastPatCharScannerMixin> aka SyncToLiteralEquivTrivialLastPatCharAndConsume");
  1955. PRINT_MIXIN(EquivTrivialLastPatCharScannerMixin); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  1956. PRINT_RE_BYTECODE_MID();
  1957. PRINT_BYTES(EquivTrivialLastPatCharScannerMixin); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  1958. PRINT_RE_BYTECODE_END();
  1959. }
  1960. #endif
  1961. // ----------------------------------------------------------------------
  1962. // SyncToCharAndBackupInst (optimized instruction)
  1963. // ----------------------------------------------------------------------
  1964. inline bool SyncToCharAndBackupInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  1965. {
  1966. if (backup.lower > inputLength - matchStart)
  1967. {
  1968. // Even match at very end doesn't allow for minimum backup
  1969. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  1970. }
  1971. if (inputOffset < nextSyncInputOffset)
  1972. {
  1973. // We have not yet reached the offset in the input we last synced to before backing up, so it's unnecessary to sync
  1974. // again since we'll sync to the same point in the input and back up to the same place we are at now
  1975. instPointer += sizeof(*this);
  1976. return false;
  1977. }
  1978. if (backup.lower > inputOffset - matchStart)
  1979. {
  1980. // No use looking for match until minimum backup is possible
  1981. inputOffset = matchStart + backup.lower;
  1982. }
  1983. const Char matchC = c;
  1984. while (inputOffset < inputLength && input[inputOffset] != matchC)
  1985. {
  1986. #if ENABLE_REGEX_CONFIG_OPTIONS
  1987. matcher.CompStats();
  1988. #endif
  1989. inputOffset++;
  1990. }
  1991. if (inputOffset >= inputLength)
  1992. {
  1993. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  1994. }
  1995. nextSyncInputOffset = inputOffset + 1;
  1996. if (backup.upper != CharCountFlag)
  1997. {
  1998. // Backup at most by backup.upper for new start
  1999. CharCount maxBackup = inputOffset - matchStart;
  2000. matchStart = inputOffset - min(maxBackup, (CharCount)backup.upper);
  2001. }
  2002. // else: leave start where it is
  2003. // Move input to new match start
  2004. inputOffset = matchStart;
  2005. instPointer += sizeof(*this);
  2006. return false;
  2007. }
  2008. #if ENABLE_REGEX_CONFIG_OPTIONS
  2009. int SyncToCharAndBackupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2010. {
  2011. PRINT_RE_BYTECODE_BEGIN("SyncToCharAndBackup");
  2012. PRINT_MIXIN_COMMA(CharMixin);
  2013. PRINT_MIXIN(BackupMixin);
  2014. PRINT_RE_BYTECODE_MID();
  2015. PRINT_BYTES(CharMixin);
  2016. PRINT_BYTES(BackupMixin);
  2017. PRINT_RE_BYTECODE_END();
  2018. }
  2019. #endif
  2020. // ----------------------------------------------------------------------
  2021. // SyncToSetAndBackupInst (optimized instruction)
  2022. // ----------------------------------------------------------------------
  2023. template<bool IsNegation>
  2024. inline bool SyncToSetAndBackupInst<IsNegation>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2025. {
  2026. if (backup.lower > inputLength - matchStart)
  2027. {
  2028. // Even match at very end doesn't allow for minimum backup
  2029. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  2030. }
  2031. if (inputOffset < nextSyncInputOffset)
  2032. {
  2033. // We have not yet reached the offset in the input we last synced to before backing up, so it's unnecessary to sync
  2034. // again since we'll sync to the same point in the input and back up to the same place we are at now
  2035. instPointer += sizeof(*this);
  2036. return false;
  2037. }
  2038. if (backup.lower > inputOffset - matchStart)
  2039. {
  2040. // No use looking for match until minimum backup is possible
  2041. inputOffset = matchStart + backup.lower;
  2042. }
  2043. const RuntimeCharSet<Char>& matchSet = this->set;
  2044. while (inputOffset < inputLength && matchSet.Get(input[inputOffset]) == IsNegation)
  2045. {
  2046. #if ENABLE_REGEX_CONFIG_OPTIONS
  2047. matcher.CompStats();
  2048. #endif
  2049. inputOffset++;
  2050. }
  2051. if (inputOffset >= inputLength)
  2052. {
  2053. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  2054. }
  2055. nextSyncInputOffset = inputOffset + 1;
  2056. if (backup.upper != CharCountFlag)
  2057. {
  2058. // Backup at most by backup.upper for new start
  2059. CharCount maxBackup = inputOffset - matchStart;
  2060. matchStart = inputOffset - min(maxBackup, (CharCount)backup.upper);
  2061. }
  2062. // else: leave start where it is
  2063. // Move input to new match start
  2064. inputOffset = matchStart;
  2065. instPointer += sizeof(*this);
  2066. return false;
  2067. }
  2068. #if ENABLE_REGEX_CONFIG_OPTIONS
  2069. template<bool IsNegation>
  2070. int SyncToSetAndBackupInst<IsNegation>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2071. {
  2072. if (IsNegation)
  2073. {
  2074. PRINT_RE_BYTECODE_BEGIN("SyncToNegatedSetAndBackup");
  2075. PRINT_MIXIN_COMMA(SetMixin<true>);
  2076. }
  2077. else
  2078. {
  2079. PRINT_RE_BYTECODE_BEGIN("SyncToSetAndBackup");
  2080. PRINT_MIXIN_COMMA(SetMixin<false>);
  2081. }
  2082. PRINT_MIXIN(BackupMixin);
  2083. PRINT_RE_BYTECODE_MID();
  2084. IsNegation ? PRINT_BYTES(SetMixin<true>) : PRINT_BYTES(SetMixin<false>);
  2085. PRINT_BYTES(BackupMixin);
  2086. PRINT_RE_BYTECODE_END();
  2087. }
  2088. #endif
  2089. // ----------------------------------------------------------------------
  2090. // SyncToLiteralAndBackupInst (optimized instruction)
  2091. // ----------------------------------------------------------------------
  2092. template <typename ScannerT>
  2093. inline bool SyncToLiteralAndBackupInstT<ScannerT>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2094. {
  2095. if (backup.lower > inputLength - matchStart)
  2096. {
  2097. // Even match at very end doesn't allow for minimum backup
  2098. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  2099. }
  2100. if(inputOffset < nextSyncInputOffset)
  2101. {
  2102. // We have not yet reached the offset in the input we last synced to before backing up, so it's unnecessary to sync
  2103. // again since we'll sync to the same point in the input and back up to the same place we are at now
  2104. instPointer += sizeof(*this);
  2105. return false;
  2106. }
  2107. if (backup.lower > inputOffset - matchStart)
  2108. {
  2109. // No use looking for match until minimum backup is possible
  2110. inputOffset = matchStart + backup.lower;
  2111. }
  2112. if (!this->Match(matcher, input, inputLength, inputOffset))
  2113. {
  2114. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  2115. }
  2116. nextSyncInputOffset = inputOffset + 1;
  2117. if (backup.upper != CharCountFlag)
  2118. {
  2119. // Set new start at most backup.upper from start of literal
  2120. CharCount maxBackup = inputOffset - matchStart;
  2121. matchStart = inputOffset - min(maxBackup, (CharCount)backup.upper);
  2122. }
  2123. // else: leave start where it is
  2124. // Move input to new match start
  2125. inputOffset = matchStart;
  2126. instPointer += sizeof(*this);
  2127. return false;
  2128. }
  2129. #if ENABLE_REGEX_CONFIG_OPTIONS
  2130. // explicit instantiation
  2131. template struct SyncToLiteralAndBackupInstT<Char2LiteralScannerMixin>;
  2132. template struct SyncToLiteralAndBackupInstT<ScannerMixin>;
  2133. template struct SyncToLiteralAndBackupInstT<ScannerMixin_WithLinearCharMap>;
  2134. template struct SyncToLiteralAndBackupInstT<EquivScannerMixin>;
  2135. template struct SyncToLiteralAndBackupInstT<EquivTrivialLastPatCharScannerMixin>;
  2136. // Explicitly define each of these 5 Print functions so that the output will show the actual template param mixin and
  2137. // actual opcode name, even though the logic is basically the same in each definition. See notes below.
  2138. template <>
  2139. int SyncToLiteralAndBackupInstT<Char2LiteralScannerMixin>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2140. {
  2141. // NOTE: this text is unique to this instantiation
  2142. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndBackupInstT<Char2LiteralScannerMixin> aka SyncToChar2LiteralAndBackup");
  2143. PRINT_MIXIN_COMMA(Char2LiteralScannerMixin); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  2144. PRINT_MIXIN(BackupMixin);
  2145. PRINT_RE_BYTECODE_MID();
  2146. PRINT_BYTES(Char2LiteralScannerMixin); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  2147. PRINT_BYTES(BackupMixin);
  2148. PRINT_RE_BYTECODE_END();
  2149. }
  2150. template <>
  2151. int SyncToLiteralAndBackupInstT<ScannerMixin>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2152. {
  2153. // NOTE: this text is unique to this instantiation
  2154. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndBackupInstT<ScannerMixin> aka SyncToLiteralAndBackup");
  2155. PRINT_MIXIN_COMMA(ScannerMixin); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  2156. PRINT_MIXIN(BackupMixin);
  2157. PRINT_RE_BYTECODE_MID();
  2158. PRINT_BYTES(ScannerMixin); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  2159. PRINT_BYTES(BackupMixin);
  2160. PRINT_RE_BYTECODE_END();
  2161. }
  2162. template <>
  2163. int SyncToLiteralAndBackupInstT<ScannerMixin_WithLinearCharMap>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2164. {
  2165. // NOTE: this text is unique to this instantiation
  2166. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndBackupInstT<ScannerMixin_WithLinearCharMap> aka SyncToLinearLiteralAndBackup");
  2167. PRINT_MIXIN_COMMA(ScannerMixin_WithLinearCharMap); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  2168. PRINT_MIXIN(BackupMixin);
  2169. PRINT_RE_BYTECODE_MID();
  2170. PRINT_BYTES(ScannerMixin_WithLinearCharMap); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  2171. PRINT_BYTES(BackupMixin);
  2172. PRINT_RE_BYTECODE_END();
  2173. }
  2174. template <>
  2175. int SyncToLiteralAndBackupInstT<EquivScannerMixin>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2176. {
  2177. // NOTE: this text is unique to this instantiation
  2178. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndBackupInstT<EquivScannerMixin> aka SyncToLiteralEquivAndBackup");
  2179. PRINT_MIXIN_COMMA(EquivScannerMixin); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  2180. PRINT_MIXIN(BackupMixin);
  2181. PRINT_RE_BYTECODE_MID();
  2182. PRINT_BYTES(EquivScannerMixin); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  2183. PRINT_BYTES(BackupMixin);
  2184. PRINT_RE_BYTECODE_END();
  2185. }
  2186. template <>
  2187. int SyncToLiteralAndBackupInstT<EquivTrivialLastPatCharScannerMixin>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2188. {
  2189. // NOTE: this text is unique to this instantiation
  2190. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralAndBackupInstT<EquivTrivialLastPatCharScannerMixin> aka SyncToLiteralEquivTrivialLastPatCharAndBackup");
  2191. PRINT_MIXIN_COMMA(EquivTrivialLastPatCharScannerMixin); // NOTE: would work with template <typename ScannerT> ScannerT::Print
  2192. PRINT_MIXIN(BackupMixin);
  2193. PRINT_RE_BYTECODE_MID();
  2194. PRINT_BYTES(EquivTrivialLastPatCharScannerMixin); // NOTE: unique because macro expansion and _u(#InstType) happen before template is evaluated (so text would be ScannerT)
  2195. PRINT_BYTES(BackupMixin);
  2196. PRINT_RE_BYTECODE_END();
  2197. }
  2198. #endif
  2199. // ----------------------------------------------------------------------
  2200. // SyncToLiteralsAndBackupInst (optimized instruction)
  2201. // ----------------------------------------------------------------------
  2202. inline bool SyncToLiteralsAndBackupInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2203. {
  2204. if (backup.lower > inputLength - matchStart)
  2205. {
  2206. // Even match at very end doesn't allow for minimum backup
  2207. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  2208. }
  2209. if (inputOffset < nextSyncInputOffset)
  2210. {
  2211. // We have not yet reached the offset in the input we last synced to before backing up, so it's unnecessary to sync
  2212. // again since we'll sync to the same point in the input and back up to the same place we are at now
  2213. instPointer += sizeof(*this);
  2214. return false;
  2215. }
  2216. if (backup.lower > inputOffset - matchStart)
  2217. {
  2218. // No use looking for match until minimum backup is possible
  2219. inputOffset = matchStart + backup.lower;
  2220. }
  2221. int besti = -1;
  2222. CharCount bestMatchOffset = 0;
  2223. if (matcher.literalNextSyncInputOffsets == nullptr)
  2224. {
  2225. Assert(numLiterals <= MaxNumSyncLiterals);
  2226. matcher.literalNextSyncInputOffsets =
  2227. RecyclerNewArrayLeaf(matcher.recycler, CharCount, ScannersMixin::MaxNumSyncLiterals);
  2228. }
  2229. CharCount* literalNextSyncInputOffsets = matcher.literalNextSyncInputOffsets;
  2230. if (firstIteration)
  2231. {
  2232. for (int i = 0; i < numLiterals; i++)
  2233. {
  2234. literalNextSyncInputOffsets[i] = inputOffset;
  2235. }
  2236. }
  2237. for (int i = 0; i < numLiterals; i++)
  2238. {
  2239. CharCount thisMatchOffset = literalNextSyncInputOffsets[i];
  2240. if (inputOffset > thisMatchOffset)
  2241. {
  2242. thisMatchOffset = inputOffset;
  2243. }
  2244. if (infos[i]->isEquivClass
  2245. ? (infos[i]->scanner.Match<CaseInsensitive::EquivClassSize>(
  2246. input
  2247. , inputLength
  2248. , thisMatchOffset
  2249. , matcher.program->rep.insts.litbuf + infos[i]->offset
  2250. , infos[i]->length
  2251. #if ENABLE_REGEX_CONFIG_OPTIONS
  2252. , matcher.stats
  2253. #endif
  2254. ))
  2255. : (infos[i]->scanner.Match<1>(
  2256. input
  2257. , inputLength
  2258. , thisMatchOffset
  2259. , matcher.program->rep.insts.litbuf + infos[i]->offset
  2260. , infos[i]->length
  2261. #if ENABLE_REGEX_CONFIG_OPTIONS
  2262. , matcher.stats
  2263. #endif
  2264. )))
  2265. {
  2266. if (besti < 0 || thisMatchOffset < bestMatchOffset)
  2267. {
  2268. besti = i;
  2269. bestMatchOffset = thisMatchOffset;
  2270. }
  2271. literalNextSyncInputOffsets[i] = thisMatchOffset;
  2272. }
  2273. else
  2274. {
  2275. literalNextSyncInputOffsets[i] = inputLength;
  2276. }
  2277. }
  2278. if (besti < 0)
  2279. {
  2280. // No literals matched
  2281. return matcher.HardFail(HARDFAIL_PARAMETERS(HardFailMode::ImmediateFail));
  2282. }
  2283. nextSyncInputOffset = bestMatchOffset + 1;
  2284. if (backup.upper != CharCountFlag)
  2285. {
  2286. // Set new start at most backup.upper from start of literal
  2287. CharCount maxBackup = bestMatchOffset - matchStart;
  2288. matchStart = bestMatchOffset - min(maxBackup, (CharCount)backup.upper);
  2289. }
  2290. // else: leave start where it is
  2291. // Move input to new match start
  2292. inputOffset = matchStart;
  2293. instPointer += sizeof(*this);
  2294. return false;
  2295. }
  2296. #if ENABLE_REGEX_CONFIG_OPTIONS
  2297. int SyncToLiteralsAndBackupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2298. {
  2299. PRINT_RE_BYTECODE_BEGIN("SyncToLiteralsAndBackup");
  2300. PRINT_MIXIN_COMMA(ScannersMixin);
  2301. PRINT_MIXIN(BackupMixin);
  2302. PRINT_RE_BYTECODE_MID();
  2303. PRINT_BYTES(ScannersMixin);
  2304. PRINT_BYTES(BackupMixin);
  2305. PRINT_RE_BYTECODE_END();
  2306. }
  2307. #endif
  2308. // ----------------------------------------------------------------------
  2309. // MatchGroupInst
  2310. // ----------------------------------------------------------------------
  2311. inline bool MatchGroupInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2312. {
  2313. GroupInfo* const info = matcher.GroupIdToGroupInfo(groupId);
  2314. if (!info->IsUndefined() && info->length > 0)
  2315. {
  2316. if (info->length > inputLength - inputOffset)
  2317. {
  2318. return matcher.Fail(FAIL_PARAMETERS);
  2319. }
  2320. CharCount groupOffset = info->offset;
  2321. const CharCount groupEndOffset = groupOffset + info->length;
  2322. bool isCaseInsensitiveMatch = (matcher.program->flags & IgnoreCaseRegexFlag) != 0;
  2323. bool isCodePointList = (matcher.program->flags & UnicodeRegexFlag) != 0;
  2324. // This is the only place in the runtime machinery we need to convert characters to their equivalence class
  2325. if (isCaseInsensitiveMatch && isCodePointList)
  2326. {
  2327. auto getNextCodePoint = [=](CharCount &offset, CharCount endOffset, codepoint_t &codePoint) {
  2328. if (endOffset <= offset)
  2329. {
  2330. return false;
  2331. }
  2332. Char lowerPart = input[offset];
  2333. if (!Js::NumberUtilities::IsSurrogateLowerPart(lowerPart) || offset + 1 == endOffset)
  2334. {
  2335. codePoint = lowerPart;
  2336. offset += 1;
  2337. return true;
  2338. }
  2339. Char upperPart = input[offset + 1];
  2340. if (!Js::NumberUtilities::IsSurrogateUpperPart(upperPart))
  2341. {
  2342. codePoint = lowerPart;
  2343. offset += 1;
  2344. }
  2345. else
  2346. {
  2347. codePoint = Js::NumberUtilities::SurrogatePairAsCodePoint(lowerPart, upperPart);
  2348. offset += 2;
  2349. }
  2350. return true;
  2351. };
  2352. codepoint_t equivs[CaseInsensitive::EquivClassSize];
  2353. while (true)
  2354. {
  2355. codepoint_t groupCodePoint;
  2356. bool hasGroupCodePoint = getNextCodePoint(groupOffset, groupEndOffset, groupCodePoint);
  2357. if (!hasGroupCodePoint)
  2358. {
  2359. break;
  2360. }
  2361. // We don't need to verify that there is a valid input code point since at the beginning
  2362. // of the function, we make sure that the length of the input is at least as long as the
  2363. // length of the group.
  2364. codepoint_t inputCodePoint;
  2365. getNextCodePoint(inputOffset, inputLength, inputCodePoint);
  2366. bool doesMatch = false;
  2367. if (!Js::NumberUtilities::IsInSupplementaryPlane(groupCodePoint))
  2368. {
  2369. auto toCanonical = [&](codepoint_t c) {
  2370. return matcher.standardChars->ToCanonical(
  2371. CaseInsensitive::MappingSource::CaseFolding,
  2372. static_cast<char16>(c));
  2373. };
  2374. doesMatch = (toCanonical(groupCodePoint) == toCanonical(inputCodePoint));
  2375. }
  2376. else
  2377. {
  2378. uint tblidx = 0;
  2379. uint acth = 0;
  2380. CaseInsensitive::RangeToEquivClass(tblidx, groupCodePoint, groupCodePoint, acth, equivs);
  2381. CompileAssert(CaseInsensitive::EquivClassSize == 4);
  2382. doesMatch =
  2383. inputCodePoint == equivs[0]
  2384. || inputCodePoint == equivs[1]
  2385. || inputCodePoint == equivs[2]
  2386. || inputCodePoint == equivs[3];
  2387. }
  2388. if (!doesMatch)
  2389. {
  2390. return matcher.Fail(FAIL_PARAMETERS);
  2391. }
  2392. }
  2393. }
  2394. else if (isCaseInsensitiveMatch)
  2395. {
  2396. do
  2397. {
  2398. #if ENABLE_REGEX_CONFIG_OPTIONS
  2399. matcher.CompStats();
  2400. #endif
  2401. auto toCanonical = [&](CharCount &offset) {
  2402. return matcher.standardChars->ToCanonical(CaseInsensitive::MappingSource::UnicodeData, input[offset++]);
  2403. };
  2404. if (toCanonical(groupOffset) != toCanonical(inputOffset))
  2405. {
  2406. return matcher.Fail(FAIL_PARAMETERS);
  2407. }
  2408. }
  2409. while (groupOffset < groupEndOffset);
  2410. }
  2411. else
  2412. {
  2413. do
  2414. {
  2415. #if ENABLE_REGEX_CONFIG_OPTIONS
  2416. matcher.CompStats();
  2417. #endif
  2418. if (input[groupOffset++] != input[inputOffset++])
  2419. {
  2420. return matcher.Fail(FAIL_PARAMETERS);
  2421. }
  2422. }
  2423. while (groupOffset < groupEndOffset);
  2424. }
  2425. }
  2426. // else: trivially match empty string
  2427. instPointer += sizeof(*this);
  2428. return false;
  2429. }
  2430. #if ENABLE_REGEX_CONFIG_OPTIONS
  2431. int MatchGroupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2432. {
  2433. PRINT_RE_BYTECODE_BEGIN("MatchGroup");
  2434. PRINT_MIXIN(GroupMixin);
  2435. PRINT_RE_BYTECODE_MID();
  2436. PRINT_BYTES(GroupMixin);
  2437. PRINT_RE_BYTECODE_END();
  2438. }
  2439. #endif
  2440. // ----------------------------------------------------------------------
  2441. // BeginDefineGroupInst
  2442. // ----------------------------------------------------------------------
  2443. inline bool BeginDefineGroupInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2444. {
  2445. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  2446. Assert(groupInfo->IsUndefined());
  2447. groupInfo->offset = inputOffset;
  2448. Assert(groupInfo->IsUndefined());
  2449. instPointer += sizeof(*this);
  2450. return false;
  2451. }
  2452. #if ENABLE_REGEX_CONFIG_OPTIONS
  2453. int BeginDefineGroupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2454. {
  2455. PRINT_RE_BYTECODE_BEGIN("BeginDefineGroup");
  2456. PRINT_MIXIN(GroupMixin);
  2457. PRINT_RE_BYTECODE_MID();
  2458. PRINT_BYTES(GroupMixin);
  2459. PRINT_RE_BYTECODE_END();
  2460. }
  2461. #endif
  2462. // ----------------------------------------------------------------------
  2463. // EndDefineGroupInst
  2464. // ----------------------------------------------------------------------
  2465. inline bool EndDefineGroupInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2466. {
  2467. if (!noNeedToSave)
  2468. {
  2469. // UNDO ACTION: Restore group on backtrack
  2470. PUSH(contStack, ResetGroupCont, groupId);
  2471. #if ENABLE_REGEX_CONFIG_OPTIONS
  2472. matcher.PushStats(contStack, input);
  2473. #endif
  2474. }
  2475. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  2476. Assert(groupInfo->IsUndefined());
  2477. Assert(inputOffset >= groupInfo->offset);
  2478. groupInfo->length = inputOffset - groupInfo->offset;
  2479. instPointer += sizeof(*this);
  2480. return false;
  2481. }
  2482. #if ENABLE_REGEX_CONFIG_OPTIONS
  2483. int EndDefineGroupInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2484. {
  2485. PRINT_RE_BYTECODE_BEGIN("EndDefineGroup");
  2486. PRINT_MIXIN_COMMA(GroupMixin);
  2487. PRINT_MIXIN(NoNeedToSaveMixin);
  2488. PRINT_RE_BYTECODE_MID();
  2489. PRINT_BYTES(GroupMixin);
  2490. PRINT_BYTES(NoNeedToSaveMixin);
  2491. PRINT_RE_BYTECODE_END();
  2492. }
  2493. #endif
  2494. // ----------------------------------------------------------------------
  2495. // DefineGroupFixedInst (optimized instruction)
  2496. // ----------------------------------------------------------------------
  2497. inline bool DefineGroupFixedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2498. {
  2499. if (!noNeedToSave)
  2500. {
  2501. // UNDO ACTION: Restore group on backtrack
  2502. PUSH(contStack, ResetGroupCont, groupId);
  2503. #if ENABLE_REGEX_CONFIG_OPTIONS
  2504. matcher.PushStats(contStack, input);
  2505. #endif
  2506. }
  2507. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  2508. Assert(groupInfo->IsUndefined());
  2509. groupInfo->offset = inputOffset - length;
  2510. groupInfo->length = length;
  2511. instPointer += sizeof(*this);
  2512. return false;
  2513. }
  2514. #if ENABLE_REGEX_CONFIG_OPTIONS
  2515. int DefineGroupFixedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2516. {
  2517. PRINT_RE_BYTECODE_BEGIN("DefineGroupFixed");
  2518. PRINT_MIXIN_COMMA(GroupMixin);
  2519. PRINT_MIXIN_COMMA(FixedLengthMixin);
  2520. PRINT_MIXIN(NoNeedToSaveMixin);
  2521. PRINT_RE_BYTECODE_MID();
  2522. PRINT_BYTES(GroupMixin);
  2523. PRINT_BYTES(FixedLengthMixin);
  2524. PRINT_BYTES(NoNeedToSaveMixin);
  2525. PRINT_RE_BYTECODE_END();
  2526. }
  2527. #endif
  2528. // ----------------------------------------------------------------------
  2529. // BeginLoopInst
  2530. // ----------------------------------------------------------------------
  2531. inline bool BeginLoopInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2532. {
  2533. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2534. // If loop has outer loops, the continuation stack may have choicepoints from an earlier "run" of this loop
  2535. // which, when backtracked to, may expect the loopInfo state to be as it was at the time the choicepoint was
  2536. // pushed.
  2537. // - If the loop is greedy with deterministic body, there may be Resumes into the follow of the loop, but
  2538. // they won't look at the loopInfo state so there's nothing to do.
  2539. // - If the loop is greedy, or if it is non-greedy with lower > 0, AND it has a non-deterministic body,
  2540. // we may have Resume entries which will resume inside the loop body, which may then run to a
  2541. // RepeatLoop, which will then look at the loopInfo state. However, each iteration is protected by
  2542. // a RestoreLoop by RepeatLoopInst below. (****)
  2543. // - If the loop is non-greedy there may be a RepeatLoop on the stack, so we must restore the loopInfo
  2544. // state before backtracking to it.
  2545. if (!isGreedy && hasOuterLoops)
  2546. {
  2547. PUSH(contStack, RestoreLoopCont, loopId, *loopInfo, matcher);
  2548. #if ENABLE_REGEX_CONFIG_OPTIONS
  2549. matcher.PushStats(contStack, input);
  2550. #endif
  2551. }
  2552. // The loop body must always begin with empty inner groups
  2553. // - if the loop is not in an outer they will be empty due to the reset when the match began
  2554. // - if the loop is in an outer loop, they will have been reset by the outer loop's RepeatLoop instruction
  2555. #if DBG
  2556. for (int i = minBodyGroupId; i <= maxBodyGroupId; i++)
  2557. {
  2558. Assert(matcher.GroupIdToGroupInfo(i)->IsUndefined());
  2559. }
  2560. #endif
  2561. loopInfo->number = 0;
  2562. loopInfo->startInputOffset = inputOffset;
  2563. if (repeats.lower == 0)
  2564. {
  2565. if (isGreedy)
  2566. {
  2567. // CHOICEPOINT: Try one iteration of body, if backtrack continue from here with no iterations
  2568. PUSH(contStack, ResumeCont, inputOffset, exitLabel);
  2569. instPointer += sizeof(*this);
  2570. }
  2571. else
  2572. {
  2573. // CHOICEPOINT: Try no iterations of body, if backtrack do one iteration of body from here
  2574. Assert(instPointer == (uint8*)this);
  2575. PUSH(contStack, RepeatLoopCont, matcher.InstPointerToLabel(instPointer), inputOffset);
  2576. instPointer = matcher.LabelToInstPointer(exitLabel);
  2577. }
  2578. #if ENABLE_REGEX_CONFIG_OPTIONS
  2579. matcher.PushStats(contStack, input);
  2580. #endif
  2581. }
  2582. else
  2583. {
  2584. // Must match minimum iterations, so continue to loop body
  2585. instPointer += sizeof(*this);
  2586. }
  2587. return false;
  2588. }
  2589. #if ENABLE_REGEX_CONFIG_OPTIONS
  2590. int BeginLoopInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2591. {
  2592. PRINT_RE_BYTECODE_BEGIN("BeginLoop");
  2593. PRINT_MIXIN_COMMA(BeginLoopMixin);
  2594. PRINT_MIXIN_COMMA(BodyGroupsMixin);
  2595. PRINT_MIXIN(GreedyMixin);
  2596. PRINT_RE_BYTECODE_MID();
  2597. PRINT_BYTES(BeginLoopMixin);
  2598. PRINT_BYTES(BodyGroupsMixin);
  2599. PRINT_BYTES(GreedyMixin);
  2600. PRINT_RE_BYTECODE_END();
  2601. }
  2602. #endif
  2603. // ----------------------------------------------------------------------
  2604. // RepeatLoopInst
  2605. // ----------------------------------------------------------------------
  2606. inline bool RepeatLoopInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2607. {
  2608. BeginLoopInst* begin = matcher.L2I(BeginLoop, beginLabel);
  2609. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2610. // See comment (****) above.
  2611. if (begin->hasInnerNondet)
  2612. {
  2613. PUSH(contStack, RestoreLoopCont, begin->loopId, *loopInfo, matcher);
  2614. #if ENABLE_REGEX_CONFIG_OPTIONS
  2615. matcher.PushStats(contStack, input);
  2616. #endif
  2617. }
  2618. loopInfo->number++;
  2619. if (loopInfo->number < begin->repeats.lower)
  2620. {
  2621. // Must match another iteration of body.
  2622. loopInfo->startInputOffset = inputOffset;
  2623. if(begin->hasInnerNondet)
  2624. {
  2625. // If it backtracks into the loop body of an earlier iteration, it must restore inner groups for that iteration.
  2626. // Save the inner groups and reset them for the next iteration.
  2627. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  2628. }
  2629. else
  2630. {
  2631. // If it backtracks, the entire loop will fail, so no need to restore groups. Just reset the inner groups for
  2632. // the next iteration.
  2633. matcher.ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  2634. }
  2635. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopInst));
  2636. }
  2637. else if (inputOffset == loopInfo->startInputOffset && loopInfo->number > begin->repeats.lower)
  2638. {
  2639. // The minimum number of iterations has been satisfied but the last iteration made no progress.
  2640. // - With greedy & deterministic body, FAIL so as to undo that iteration and restore group bindings.
  2641. // - With greedy & non-deterministic body, FAIL so as to try another body alternative
  2642. // - With non-greedy, we're trying an additional iteration because the follow failed. But
  2643. // since we didn't consume anything the follow will fail again, so fail
  2644. //
  2645. return matcher.Fail(FAIL_PARAMETERS);
  2646. }
  2647. else if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2648. {
  2649. // Success: proceed to remainder.
  2650. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2651. }
  2652. else if (begin->isGreedy)
  2653. {
  2654. // CHOICEPOINT: Try one more iteration of body, if backtrack continue from here with no more iterations
  2655. PUSH(contStack, ResumeCont, inputOffset, begin->exitLabel);
  2656. #if ENABLE_REGEX_CONFIG_OPTIONS
  2657. matcher.PushStats(contStack, input);
  2658. #endif
  2659. loopInfo->startInputOffset = inputOffset;
  2660. // If backtrack, we must continue with previous group bindings
  2661. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  2662. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopInst));
  2663. }
  2664. else
  2665. {
  2666. // CHOICEPOINT: Try no more iterations of body, if backtrack do one more iteration of body from here
  2667. PUSH(contStack, RepeatLoopCont, beginLabel, inputOffset);
  2668. #if ENABLE_REGEX_CONFIG_OPTIONS
  2669. matcher.PushStats(contStack, input);
  2670. #endif
  2671. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2672. }
  2673. return false;
  2674. }
  2675. #if ENABLE_REGEX_CONFIG_OPTIONS
  2676. int RepeatLoopInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2677. {
  2678. PRINT_RE_BYTECODE_BEGIN("RepeatLoop");
  2679. PRINT_MIXIN(RepeatLoopMixin);
  2680. PRINT_RE_BYTECODE_MID();
  2681. PRINT_BYTES(RepeatLoopMixin);
  2682. PRINT_RE_BYTECODE_END();
  2683. }
  2684. #endif
  2685. // ----------------------------------------------------------------------
  2686. // BeginLoopIfCharInst (optimized instruction)
  2687. // ----------------------------------------------------------------------
  2688. inline bool BeginLoopIfCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2689. {
  2690. #if ENABLE_REGEX_CONFIG_OPTIONS
  2691. matcher.CompStats();
  2692. #endif
  2693. if (inputOffset < inputLength && input[inputOffset] == c)
  2694. {
  2695. // Commit to at least one iteration of loop
  2696. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2697. // All inner groups must begin reset
  2698. #if DBG
  2699. for (int i = minBodyGroupId; i <= maxBodyGroupId; i++)
  2700. {
  2701. Assert(matcher.GroupIdToGroupInfo(i)->IsUndefined());
  2702. }
  2703. #endif
  2704. loopInfo->number = 0;
  2705. instPointer += sizeof(*this);
  2706. return false;
  2707. }
  2708. if (repeats.lower > 0)
  2709. {
  2710. return matcher.Fail(FAIL_PARAMETERS);
  2711. }
  2712. instPointer = matcher.LabelToInstPointer(exitLabel);
  2713. return false;
  2714. }
  2715. #if ENABLE_REGEX_CONFIG_OPTIONS
  2716. int BeginLoopIfCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2717. {
  2718. PRINT_RE_BYTECODE_BEGIN("BeginLoopIfChar");
  2719. PRINT_MIXIN_COMMA(CharMixin);
  2720. PRINT_MIXIN_COMMA(BeginLoopMixin);
  2721. PRINT_MIXIN(BodyGroupsMixin);
  2722. PRINT_RE_BYTECODE_MID();
  2723. PRINT_BYTES(CharMixin);
  2724. PRINT_BYTES(BeginLoopMixin);
  2725. PRINT_BYTES(BodyGroupsMixin);
  2726. PRINT_RE_BYTECODE_END();
  2727. }
  2728. #endif
  2729. // ----------------------------------------------------------------------
  2730. // BeginLoopIfSetInst (optimized instruction)
  2731. // ----------------------------------------------------------------------
  2732. inline bool BeginLoopIfSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2733. {
  2734. #if ENABLE_REGEX_CONFIG_OPTIONS
  2735. matcher.CompStats();
  2736. #endif
  2737. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  2738. {
  2739. // Commit to at least one iteration of loop
  2740. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2741. // All inner groups must be begin reset
  2742. #if DBG
  2743. for (int i = minBodyGroupId; i <= maxBodyGroupId; i++)
  2744. {
  2745. Assert(matcher.GroupIdToGroupInfo(i)->IsUndefined());
  2746. }
  2747. #endif
  2748. loopInfo->startInputOffset = inputOffset;
  2749. loopInfo->number = 0;
  2750. instPointer += sizeof(*this);
  2751. return false;
  2752. }
  2753. if (repeats.lower > 0)
  2754. {
  2755. return matcher.Fail(FAIL_PARAMETERS);
  2756. }
  2757. instPointer = matcher.LabelToInstPointer(exitLabel);
  2758. return false;
  2759. }
  2760. #if ENABLE_REGEX_CONFIG_OPTIONS
  2761. int BeginLoopIfSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2762. {
  2763. PRINT_RE_BYTECODE_BEGIN("BeginLoopIfSet");
  2764. PRINT_MIXIN_COMMA(SetMixin<false>);
  2765. PRINT_MIXIN_COMMA(BeginLoopMixin);
  2766. PRINT_MIXIN(BodyGroupsMixin);
  2767. PRINT_RE_BYTECODE_MID();
  2768. PRINT_BYTES(SetMixin<false>);
  2769. PRINT_BYTES(BeginLoopMixin);
  2770. PRINT_BYTES(BodyGroupsMixin);
  2771. PRINT_RE_BYTECODE_END();
  2772. }
  2773. #endif
  2774. // ----------------------------------------------------------------------
  2775. // RepeatLoopIfCharInst (optimized instruction)
  2776. // ----------------------------------------------------------------------
  2777. inline bool RepeatLoopIfCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2778. {
  2779. BeginLoopIfCharInst* begin = matcher.L2I(BeginLoopIfChar, beginLabel);
  2780. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2781. if (begin->hasInnerNondet)
  2782. {
  2783. // May end up backtracking into loop body for iteration just completed: see above.
  2784. PUSH(contStack, RestoreLoopCont, begin->loopId, *loopInfo, matcher);
  2785. #if ENABLE_REGEX_CONFIG_OPTIONS
  2786. matcher.PushStats(contStack, input);
  2787. #endif
  2788. }
  2789. loopInfo->number++;
  2790. #if ENABLE_REGEX_CONFIG_OPTIONS
  2791. matcher.CompStats();
  2792. #endif
  2793. if (inputOffset < inputLength && input[inputOffset] == begin->c)
  2794. {
  2795. if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2796. {
  2797. // If the loop body's first set and the loop's follow set are disjoint, we can just fail here since
  2798. // we know the next character in the input is in the loop body's first set.
  2799. return matcher.Fail(FAIL_PARAMETERS);
  2800. }
  2801. // Commit to one more iteration
  2802. if(begin->hasInnerNondet)
  2803. {
  2804. // If it backtracks into the loop body of an earlier iteration, it must restore inner groups for that iteration.
  2805. // Save the inner groups and reset them for the next iteration.
  2806. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  2807. }
  2808. else
  2809. {
  2810. // If it backtracks, the entire loop will fail, so no need to restore groups. Just reset the inner groups for
  2811. // the next iteration.
  2812. matcher.ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  2813. }
  2814. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopIfCharInst));
  2815. return false;
  2816. }
  2817. if (loopInfo->number < begin->repeats.lower)
  2818. {
  2819. return matcher.Fail(FAIL_PARAMETERS);
  2820. }
  2821. // Proceed to exit
  2822. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2823. return false;
  2824. }
  2825. #if ENABLE_REGEX_CONFIG_OPTIONS
  2826. int RepeatLoopIfCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2827. {
  2828. PRINT_RE_BYTECODE_BEGIN("RepeatLoopIfChar");
  2829. PRINT_MIXIN(RepeatLoopMixin);
  2830. PRINT_RE_BYTECODE_MID();
  2831. PRINT_BYTES(RepeatLoopMixin);
  2832. PRINT_RE_BYTECODE_END();
  2833. }
  2834. #endif
  2835. // ----------------------------------------------------------------------
  2836. // RepeatLoopIfSetInst (optimized instruction)
  2837. // ----------------------------------------------------------------------
  2838. inline bool RepeatLoopIfSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2839. {
  2840. BeginLoopIfSetInst* begin = matcher.L2I(BeginLoopIfSet, beginLabel);
  2841. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2842. if (begin->hasInnerNondet)
  2843. {
  2844. // May end up backtracking into loop body for iteration just completed: see above.
  2845. PUSH(contStack, RestoreLoopCont, begin->loopId, *loopInfo, matcher);
  2846. #if ENABLE_REGEX_CONFIG_OPTIONS
  2847. matcher.PushStats(contStack, input);
  2848. #endif
  2849. }
  2850. loopInfo->number++;
  2851. #if ENABLE_REGEX_CONFIG_OPTIONS
  2852. matcher.CompStats();
  2853. #endif
  2854. if (inputOffset < inputLength && begin->set.Get(input[inputOffset]))
  2855. {
  2856. if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2857. {
  2858. // If the loop body's first set and the loop's follow set are disjoint, we can just fail here since
  2859. // we know the next character in the input is in the loop body's first set.
  2860. return matcher.Fail(FAIL_PARAMETERS);
  2861. }
  2862. // Commit to one more iteration
  2863. if (begin->hasInnerNondet)
  2864. {
  2865. // If it backtracks into the loop body of an earlier iteration, it must restore inner groups for that iteration.
  2866. // Save the inner groups and reset them for the next iteration.
  2867. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  2868. }
  2869. else
  2870. {
  2871. // If it backtracks, the entire loop will fail, so no need to restore groups. Just reset the inner groups for
  2872. // the next iteration.
  2873. matcher.ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  2874. }
  2875. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopIfSetInst));
  2876. return false;
  2877. }
  2878. if (loopInfo->number < begin->repeats.lower)
  2879. {
  2880. return matcher.Fail(FAIL_PARAMETERS);
  2881. }
  2882. // Proceed to exit
  2883. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2884. return false;
  2885. }
  2886. #if ENABLE_REGEX_CONFIG_OPTIONS
  2887. int RepeatLoopIfSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2888. {
  2889. PRINT_RE_BYTECODE_BEGIN("RepeatLoopIfSet");
  2890. PRINT_MIXIN(RepeatLoopMixin);
  2891. PRINT_RE_BYTECODE_MID();
  2892. PRINT_BYTES(RepeatLoopMixin);
  2893. PRINT_RE_BYTECODE_END();
  2894. }
  2895. #endif
  2896. // ----------------------------------------------------------------------
  2897. // BeginLoopFixedInst (optimized instruction)
  2898. // ----------------------------------------------------------------------
  2899. inline bool BeginLoopFixedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2900. {
  2901. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  2902. // If loop is contained in an outer loop, continuation stack may already have a RewindLoopFixed entry for
  2903. // this loop. We must make sure it's state is preserved on backtrack.
  2904. if (hasOuterLoops)
  2905. {
  2906. PUSH(contStack, RestoreLoopCont, loopId, *loopInfo, matcher);
  2907. #if ENABLE_REGEX_CONFIG_OPTIONS
  2908. matcher.PushStats(contStack, input);
  2909. #endif
  2910. }
  2911. // startInputOffset will stay here for all iterations, and we'll use number of length to figure out
  2912. // where in the input to rewind to
  2913. loopInfo->number = 0;
  2914. loopInfo->startInputOffset = inputOffset;
  2915. if (repeats.lower == 0)
  2916. {
  2917. // CHOICEPOINT: Try one iteration of body. Failure of body will rewind input to here and resume with follow.
  2918. Assert(instPointer == (uint8*)this);
  2919. PUSH(contStack, RewindLoopFixedCont, matcher.InstPointerToLabel(instPointer), true);
  2920. #if ENABLE_REGEX_CONFIG_OPTIONS
  2921. matcher.PushStats(contStack, input);
  2922. #endif
  2923. }
  2924. // else: Must match minimum iterations, so continue to loop body. Failure of body signals failure of entire loop.
  2925. instPointer += sizeof(*this);
  2926. return false;
  2927. }
  2928. #if ENABLE_REGEX_CONFIG_OPTIONS
  2929. int BeginLoopFixedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2930. {
  2931. PRINT_RE_BYTECODE_BEGIN("BeginLoopFixed");
  2932. PRINT_MIXIN_COMMA(BeginLoopMixin);
  2933. PRINT_MIXIN(FixedLengthMixin);
  2934. PRINT_RE_BYTECODE_MID();
  2935. PRINT_BYTES(BeginLoopMixin);
  2936. PRINT_BYTES(FixedLengthMixin);
  2937. PRINT_RE_BYTECODE_END();
  2938. }
  2939. #endif
  2940. // ----------------------------------------------------------------------
  2941. // RepeatLoopFixedInst (optimized instruction)
  2942. // ----------------------------------------------------------------------
  2943. inline bool RepeatLoopFixedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2944. {
  2945. BeginLoopFixedInst* begin = matcher.L2I(BeginLoopFixed, beginLabel);
  2946. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  2947. loopInfo->number++;
  2948. if (loopInfo->number < begin->repeats.lower)
  2949. {
  2950. // Must match another iteration of body. Failure of body signals failure of the entire loop.
  2951. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopFixedInst));
  2952. }
  2953. else if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  2954. {
  2955. // Matched maximum number of iterations. Continue with follow.
  2956. if (begin->repeats.lower < begin->repeats.upper)
  2957. {
  2958. // Failure of follow will try one fewer iterations (subject to repeats.lower).
  2959. // Since loop body is non-deterministic and does not define groups the rewind continuation must be on top of the stack.
  2960. Cont *top = contStack.Top();
  2961. Assert(top != 0);
  2962. Assert(top->tag == Cont::ContTag::RewindLoopFixed);
  2963. RewindLoopFixedCont* rewind = (RewindLoopFixedCont*)top;
  2964. rewind->tryingBody = false;
  2965. }
  2966. // else: we never pushed a rewind continuation
  2967. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  2968. }
  2969. else
  2970. {
  2971. // CHOICEPOINT: Try one more iteration of body. Failure of body will rewind input to here and
  2972. // try follow.
  2973. if (loopInfo->number == begin->repeats.lower)
  2974. {
  2975. // i.e. begin->repeats.lower > 0, so continuation won't have been pushed in BeginLoopFixed
  2976. PUSH(contStack, RewindLoopFixedCont, beginLabel, true);
  2977. #if ENABLE_REGEX_CONFIG_OPTIONS
  2978. matcher.PushStats(contStack, input);
  2979. #endif
  2980. }
  2981. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopFixedInst));
  2982. }
  2983. return false;
  2984. }
  2985. #if ENABLE_REGEX_CONFIG_OPTIONS
  2986. int RepeatLoopFixedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  2987. {
  2988. PRINT_RE_BYTECODE_BEGIN("RepeatLoopFixed");
  2989. PRINT_MIXIN(RepeatLoopMixin);
  2990. PRINT_RE_BYTECODE_MID();
  2991. PRINT_BYTES(RepeatLoopMixin);
  2992. PRINT_RE_BYTECODE_END();
  2993. }
  2994. #endif
  2995. // ----------------------------------------------------------------------
  2996. // LoopSetInst (optimized instruction)
  2997. // ----------------------------------------------------------------------
  2998. inline bool LoopSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  2999. {
  3000. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  3001. // If loop is contained in an outer loop, continuation stack may already have a RewindLoopFixed entry for
  3002. // this loop. We must make sure it's state is preserved on backtrack.
  3003. if (hasOuterLoops)
  3004. {
  3005. PUSH(contStack, RestoreLoopCont, loopId, *loopInfo, matcher);
  3006. #if ENABLE_REGEX_CONFIG_OPTIONS
  3007. matcher.PushStats(contStack, input);
  3008. #endif
  3009. }
  3010. // startInputOffset will stay here for all iterations, and we'll use number of length to figure out
  3011. // where in the input to rewind to
  3012. loopInfo->startInputOffset = inputOffset;
  3013. // Consume as many elements of set as possible
  3014. const RuntimeCharSet<Char>& matchSet = this->set;
  3015. const CharCount loopMatchStart = inputOffset;
  3016. const CharCountOrFlag repeatsUpper = repeats.upper;
  3017. const CharCount inputEndOffset =
  3018. static_cast<CharCount>(repeatsUpper) >= inputLength - inputOffset
  3019. ? inputLength
  3020. : inputOffset + static_cast<CharCount>(repeatsUpper);
  3021. #if ENABLE_REGEX_CONFIG_OPTIONS
  3022. matcher.CompStats();
  3023. #endif
  3024. while (inputOffset < inputEndOffset && matchSet.Get(input[inputOffset]))
  3025. {
  3026. #if ENABLE_REGEX_CONFIG_OPTIONS
  3027. matcher.CompStats();
  3028. #endif
  3029. inputOffset++;
  3030. }
  3031. loopInfo->number = inputOffset - loopMatchStart;
  3032. if (loopInfo->number < repeats.lower)
  3033. {
  3034. return matcher.Fail(FAIL_PARAMETERS);
  3035. }
  3036. else if (loopInfo->number > repeats.lower)
  3037. {
  3038. // CHOICEPOINT: If follow fails, try consuming one fewer characters
  3039. Assert(instPointer == (uint8*)this);
  3040. PUSH(contStack, RewindLoopSetCont, matcher.InstPointerToLabel(instPointer));
  3041. #if ENABLE_REGEX_CONFIG_OPTIONS
  3042. matcher.PushStats(contStack, input);
  3043. #endif
  3044. }
  3045. // else: failure of follow signals failure of entire loop
  3046. // Continue with follow
  3047. instPointer += sizeof(*this);
  3048. return false;
  3049. }
  3050. #if ENABLE_REGEX_CONFIG_OPTIONS
  3051. int LoopSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3052. {
  3053. PRINT_RE_BYTECODE_BEGIN("LoopSetInst");
  3054. PRINT_MIXIN_COMMA(SetMixin<false>);
  3055. PRINT_MIXIN(BeginLoopBasicsMixin);
  3056. PRINT_RE_BYTECODE_MID();
  3057. PRINT_BYTES(SetMixin<false>);
  3058. PRINT_BYTES(BeginLoopBasicsMixin);
  3059. PRINT_RE_BYTECODE_END();
  3060. }
  3061. #endif
  3062. inline bool LoopSetWithFollowFirstInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3063. {
  3064. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  3065. // If loop is contained in an outer loop, continuation stack may already have a RewindLoopFixed entry for
  3066. // this loop. We must make sure it's state is preserved on backtrack.
  3067. if (hasOuterLoops)
  3068. {
  3069. PUSH(contStack, RestoreLoopCont, loopId, *loopInfo, matcher);
  3070. #if ENABLE_REGEX_CONFIG_OPTIONS
  3071. matcher.PushStats(contStack, input);
  3072. #endif
  3073. }
  3074. if (loopInfo->offsetsOfFollowFirst)
  3075. {
  3076. loopInfo->offsetsOfFollowFirst->Clear();
  3077. }
  3078. // startInputOffset will stay here for all iterations, and we'll use number of length to figure out
  3079. // where in the input to rewind to
  3080. loopInfo->startInputOffset = inputOffset;
  3081. // Consume as many elements of set as possible
  3082. const RuntimeCharSet<Char>& matchSet = this->set;
  3083. const CharCount loopMatchStart = inputOffset;
  3084. const CharCountOrFlag repeatsUpper = repeats.upper;
  3085. const CharCount inputEndOffset =
  3086. static_cast<CharCount>(repeatsUpper) >= inputLength - inputOffset
  3087. ? inputLength
  3088. : inputOffset + static_cast<CharCount>(repeatsUpper);
  3089. #if ENABLE_REGEX_CONFIG_OPTIONS
  3090. matcher.CompStats();
  3091. #endif
  3092. while (inputOffset < inputEndOffset && matchSet.Get(input[inputOffset]))
  3093. {
  3094. #if ENABLE_REGEX_CONFIG_OPTIONS
  3095. matcher.CompStats();
  3096. #endif
  3097. if (input[inputOffset] == this->followFirst)
  3098. {
  3099. loopInfo->EnsureOffsetsOfFollowFirst(matcher);
  3100. loopInfo->offsetsOfFollowFirst->Add(inputOffset - loopInfo->startInputOffset);
  3101. }
  3102. inputOffset++;
  3103. }
  3104. loopInfo->number = inputOffset - loopMatchStart;
  3105. if (loopInfo->number < repeats.lower)
  3106. {
  3107. return matcher.Fail(FAIL_PARAMETERS);
  3108. }
  3109. else if (loopInfo->number > repeats.lower)
  3110. {
  3111. // CHOICEPOINT: If follow fails, try consuming one fewer characters
  3112. Assert(instPointer == (uint8*)this);
  3113. PUSH(contStack, RewindLoopSetWithFollowFirstCont, matcher.InstPointerToLabel(instPointer));
  3114. #if ENABLE_REGEX_CONFIG_OPTIONS
  3115. matcher.PushStats(contStack, input);
  3116. #endif
  3117. }
  3118. // else: failure of follow signals failure of entire loop
  3119. // Continue with follow
  3120. instPointer += sizeof(*this);
  3121. return false;
  3122. }
  3123. #if ENABLE_REGEX_CONFIG_OPTIONS
  3124. int LoopSetWithFollowFirstInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3125. {
  3126. PRINT_RE_BYTECODE_BEGIN("LoopSetWithFollowFirstInst");
  3127. PRINT_MIXIN_COMMA(SetMixin<false>);
  3128. PRINT_MIXIN_COMMA(BeginLoopBasicsMixin);
  3129. PRINT_MIXIN(FollowFirstMixin);
  3130. PRINT_RE_BYTECODE_MID();
  3131. PRINT_BYTES(SetMixin<false>);
  3132. PRINT_BYTES(BeginLoopBasicsMixin);
  3133. PRINT_MIXIN(FollowFirstMixin);
  3134. PRINT_RE_BYTECODE_END();
  3135. }
  3136. #endif
  3137. // ----------------------------------------------------------------------
  3138. // BeginLoopFixedGroupLastIterationInst (optimized instruction)
  3139. // ----------------------------------------------------------------------
  3140. inline bool BeginLoopFixedGroupLastIterationInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3141. {
  3142. Assert(matcher.GroupIdToGroupInfo(groupId)->IsUndefined());
  3143. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  3144. // If loop is contained in an outer loop, continuation stack may already have a RewindLoopFixedGroupLastIteration entry
  3145. // for this loop. We must make sure it's state is preserved on backtrack.
  3146. if (hasOuterLoops)
  3147. {
  3148. PUSH(contStack, RestoreLoopCont, loopId, *loopInfo, matcher);
  3149. #if ENABLE_REGEX_CONFIG_OPTIONS
  3150. matcher.PushStats(contStack, input);
  3151. #endif
  3152. }
  3153. // If loop is contained in an outer loop or assertion, we must reset the group binding if we backtrack all the way out
  3154. if (!noNeedToSave)
  3155. {
  3156. PUSH(contStack, ResetGroupCont, groupId);
  3157. #if ENABLE_REGEX_CONFIG_OPTIONS
  3158. matcher.PushStats(contStack, input);
  3159. #endif
  3160. }
  3161. // startInputOffset will stay here for all iterations, and we'll use number of length to figure out
  3162. // where in the input to rewind to
  3163. loopInfo->number = 0;
  3164. loopInfo->startInputOffset = inputOffset;
  3165. if (repeats.lower == 0)
  3166. {
  3167. // CHOICEPOINT: Try one iteration of body. Failure of body will rewind input to here and resume with follow.
  3168. Assert(instPointer == (uint8*)this);
  3169. PUSH(contStack, RewindLoopFixedGroupLastIterationCont, matcher.InstPointerToLabel(instPointer), true);
  3170. #if ENABLE_REGEX_CONFIG_OPTIONS
  3171. matcher.PushStats(contStack, input);
  3172. #endif
  3173. }
  3174. // else: Must match minimum iterations, so continue to loop body. Failure of body signals failure of entire loop.
  3175. instPointer += sizeof(*this);
  3176. return false;
  3177. }
  3178. #if ENABLE_REGEX_CONFIG_OPTIONS
  3179. int BeginLoopFixedGroupLastIterationInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3180. {
  3181. PRINT_RE_BYTECODE_BEGIN("BeginLoopFixedGroupLastIteration");
  3182. PRINT_MIXIN_COMMA(BeginLoopMixin);
  3183. PRINT_MIXIN_COMMA(FixedLengthMixin);
  3184. PRINT_MIXIN_COMMA(GroupMixin);
  3185. PRINT_MIXIN(NoNeedToSaveMixin);
  3186. PRINT_RE_BYTECODE_MID();
  3187. PRINT_BYTES(BeginLoopMixin);
  3188. PRINT_BYTES(FixedLengthMixin);
  3189. PRINT_BYTES(GroupMixin);
  3190. PRINT_BYTES(NoNeedToSaveMixin);
  3191. PRINT_RE_BYTECODE_END();
  3192. }
  3193. #endif
  3194. // ----------------------------------------------------------------------
  3195. // RepeatLoopFixedGroupLastIterationInst (optimized instruction)
  3196. // ----------------------------------------------------------------------
  3197. inline bool RepeatLoopFixedGroupLastIterationInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3198. {
  3199. BeginLoopFixedGroupLastIterationInst* begin = matcher.L2I(BeginLoopFixedGroupLastIteration, beginLabel);
  3200. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  3201. loopInfo->number++;
  3202. if (loopInfo->number < begin->repeats.lower)
  3203. {
  3204. // Must match another iteration of body. Failure of body signals failure of the entire loop.
  3205. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopFixedGroupLastIterationInst));
  3206. }
  3207. else if (begin->repeats.upper != CharCountFlag && loopInfo->number >= (CharCount)begin->repeats.upper)
  3208. {
  3209. // Matched maximum number of iterations. Continue with follow.
  3210. if (begin->repeats.lower < begin->repeats.upper)
  3211. {
  3212. // Failure of follow will try one fewer iterations (subject to repeats.lower).
  3213. // Since loop body is non-deterministic and does not define groups the rewind continuation must be on top of the stack.
  3214. Cont *top = contStack.Top();
  3215. Assert(top != 0);
  3216. Assert(top->tag == Cont::ContTag::RewindLoopFixedGroupLastIteration);
  3217. RewindLoopFixedGroupLastIterationCont* rewind = (RewindLoopFixedGroupLastIterationCont*)top;
  3218. rewind->tryingBody = false;
  3219. }
  3220. // else: we never pushed a rewind continuation
  3221. // Bind group
  3222. GroupInfo* groupInfo = matcher.GroupIdToGroupInfo(begin->groupId);
  3223. groupInfo->offset = inputOffset - begin->length;
  3224. groupInfo->length = begin->length;
  3225. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  3226. }
  3227. else
  3228. {
  3229. // CHOICEPOINT: Try one more iteration of body. Failure of body will rewind input to here and
  3230. // try follow.
  3231. if (loopInfo->number == begin->repeats.lower)
  3232. {
  3233. // i.e. begin->repeats.lower > 0, so continuation won't have been pushed in BeginLoopFixed
  3234. PUSH(contStack, RewindLoopFixedGroupLastIterationCont, beginLabel, true);
  3235. #if ENABLE_REGEX_CONFIG_OPTIONS
  3236. matcher.PushStats(contStack, input);
  3237. #endif
  3238. }
  3239. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopFixedGroupLastIterationInst));
  3240. }
  3241. return false;
  3242. }
  3243. #if ENABLE_REGEX_CONFIG_OPTIONS
  3244. int RepeatLoopFixedGroupLastIterationInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3245. {
  3246. PRINT_RE_BYTECODE_BEGIN("RepeatLoopFixedGroupLastIteration");
  3247. PRINT_MIXIN(RepeatLoopMixin);
  3248. PRINT_RE_BYTECODE_MID();
  3249. PRINT_BYTES(RepeatLoopMixin);
  3250. PRINT_RE_BYTECODE_END();
  3251. }
  3252. #endif
  3253. // ----------------------------------------------------------------------
  3254. // BeginGreedyLoopNoBacktrackInst
  3255. // ----------------------------------------------------------------------
  3256. inline bool BeginGreedyLoopNoBacktrackInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3257. {
  3258. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(loopId);
  3259. loopInfo->number = 0;
  3260. loopInfo->startInputOffset = inputOffset;
  3261. // CHOICEPOINT: Try one iteration of body, if backtrack continue from here with no iterations
  3262. PUSH(contStack, ResumeCont, inputOffset, exitLabel);
  3263. instPointer += sizeof(*this);
  3264. #if ENABLE_REGEX_CONFIG_OPTIONS
  3265. matcher.PushStats(contStack, input);
  3266. #endif
  3267. return false;
  3268. }
  3269. #if ENABLE_REGEX_CONFIG_OPTIONS
  3270. int BeginGreedyLoopNoBacktrackInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3271. {
  3272. PRINT_RE_BYTECODE_BEGIN("BeginGreedyLoopNoBacktrack");
  3273. PRINT_MIXIN(GreedyLoopNoBacktrackMixin);
  3274. PRINT_RE_BYTECODE_MID();
  3275. PRINT_BYTES(GreedyLoopNoBacktrackMixin);
  3276. PRINT_RE_BYTECODE_END();
  3277. }
  3278. #endif
  3279. // ----------------------------------------------------------------------
  3280. // RepeatGreedyLoopNoBacktrackInst
  3281. // ----------------------------------------------------------------------
  3282. inline bool RepeatGreedyLoopNoBacktrackInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3283. {
  3284. BeginGreedyLoopNoBacktrackInst* begin = matcher.L2I(BeginGreedyLoopNoBacktrack, beginLabel);
  3285. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  3286. loopInfo->number++;
  3287. if (inputOffset == loopInfo->startInputOffset)
  3288. {
  3289. // No progress
  3290. return matcher.Fail(FAIL_PARAMETERS);
  3291. }
  3292. else
  3293. {
  3294. // CHOICEPOINT: Try one more iteration of body, if backtrack, continue from here with no more iterations.
  3295. // Since the loop body is deterministic and group free, it wouldn't have left any continuation records.
  3296. // Therefore we can simply update the Resume continuation still on the top of the stack with the current
  3297. // input pointer.
  3298. Cont* top = contStack.Top();
  3299. Assert(top != 0 && top->tag == Cont::ContTag::Resume);
  3300. ResumeCont* resume = (ResumeCont*)top;
  3301. resume->origInputOffset = inputOffset;
  3302. loopInfo->startInputOffset = inputOffset;
  3303. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginGreedyLoopNoBacktrackInst));
  3304. }
  3305. return false;
  3306. }
  3307. #if ENABLE_REGEX_CONFIG_OPTIONS
  3308. int RepeatGreedyLoopNoBacktrackInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3309. {
  3310. PRINT_RE_BYTECODE_BEGIN("RepeatGreedyLoopNoBacktrack");
  3311. PRINT_MIXIN(RepeatLoopMixin);
  3312. PRINT_RE_BYTECODE_MID();
  3313. PRINT_BYTES(RepeatLoopMixin);
  3314. PRINT_RE_BYTECODE_END();
  3315. }
  3316. #endif
  3317. // ----------------------------------------------------------------------
  3318. // ChompCharInst (optimized instruction)
  3319. // ----------------------------------------------------------------------
  3320. template<ChompMode Mode>
  3321. inline bool ChompCharInst<Mode>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3322. {
  3323. const Char matchC = c;
  3324. #if ENABLE_REGEX_CONFIG_OPTIONS
  3325. matcher.CompStats();
  3326. #endif
  3327. if (Mode == ChompMode::Star || (inputOffset < inputLength && input[inputOffset] == matchC))
  3328. {
  3329. while (true)
  3330. {
  3331. if (Mode != ChompMode::Star)
  3332. {
  3333. ++inputOffset;
  3334. }
  3335. #if ENABLE_REGEX_CONFIG_OPTIONS
  3336. matcher.CompStats();
  3337. #endif
  3338. if (inputOffset < inputLength && input[inputOffset] == matchC)
  3339. {
  3340. if (Mode == ChompMode::Star)
  3341. {
  3342. ++inputOffset;
  3343. }
  3344. continue;
  3345. }
  3346. break;
  3347. }
  3348. instPointer += sizeof(*this);
  3349. return false;
  3350. }
  3351. return matcher.Fail(FAIL_PARAMETERS);
  3352. }
  3353. #if ENABLE_REGEX_CONFIG_OPTIONS
  3354. template<ChompMode Mode>
  3355. int ChompCharInst<Mode>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3356. {
  3357. if (Mode == ChompMode::Star)
  3358. {
  3359. PRINT_RE_BYTECODE_BEGIN("ChompChar<Star>");
  3360. }
  3361. else
  3362. {
  3363. PRINT_RE_BYTECODE_BEGIN("ChompChar<Plus>");
  3364. }
  3365. PRINT_MIXIN(CharMixin);
  3366. PRINT_RE_BYTECODE_MID();
  3367. PRINT_BYTES(CharMixin);
  3368. PRINT_RE_BYTECODE_END();
  3369. }
  3370. #endif
  3371. // ----------------------------------------------------------------------
  3372. // ChompSetInst (optimized instruction)
  3373. // ----------------------------------------------------------------------
  3374. template<ChompMode Mode>
  3375. inline bool ChompSetInst<Mode>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3376. {
  3377. const RuntimeCharSet<Char>& matchSet = this->set;
  3378. #if ENABLE_REGEX_CONFIG_OPTIONS
  3379. matcher.CompStats();
  3380. #endif
  3381. if(Mode == ChompMode::Star || (inputOffset < inputLength && matchSet.Get(input[inputOffset])))
  3382. {
  3383. while(true)
  3384. {
  3385. if (Mode != ChompMode::Star)
  3386. {
  3387. ++inputOffset;
  3388. }
  3389. #if ENABLE_REGEX_CONFIG_OPTIONS
  3390. matcher.CompStats();
  3391. #endif
  3392. if (inputOffset < inputLength && matchSet.Get(input[inputOffset]))
  3393. {
  3394. if (Mode == ChompMode::Star)
  3395. {
  3396. ++inputOffset;
  3397. }
  3398. continue;
  3399. }
  3400. break;
  3401. }
  3402. instPointer += sizeof(*this);
  3403. return false;
  3404. }
  3405. return matcher.Fail(FAIL_PARAMETERS);
  3406. }
  3407. #if ENABLE_REGEX_CONFIG_OPTIONS
  3408. template<ChompMode Mode>
  3409. int ChompSetInst<Mode>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3410. {
  3411. if (Mode == ChompMode::Star)
  3412. {
  3413. PRINT_RE_BYTECODE_BEGIN("ChompSet<Star>");
  3414. }
  3415. else
  3416. {
  3417. PRINT_RE_BYTECODE_BEGIN("ChompSet<Plus>");
  3418. }
  3419. PRINT_MIXIN(SetMixin<false>);
  3420. PRINT_RE_BYTECODE_MID();
  3421. PRINT_BYTES(SetMixin<false>);
  3422. PRINT_RE_BYTECODE_END();
  3423. }
  3424. #endif
  3425. // ----------------------------------------------------------------------
  3426. // ChompCharGroupInst (optimized instruction)
  3427. // ----------------------------------------------------------------------
  3428. template<ChompMode Mode>
  3429. inline bool ChompCharGroupInst<Mode>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3430. {
  3431. Assert(matcher.GroupIdToGroupInfo(groupId)->IsUndefined());
  3432. const CharCount inputStartOffset = inputOffset;
  3433. const Char matchC = c;
  3434. #if ENABLE_REGEX_CONFIG_OPTIONS
  3435. matcher.CompStats();
  3436. #endif
  3437. if(Mode == ChompMode::Star || (inputOffset < inputLength && input[inputOffset] == matchC))
  3438. {
  3439. while (true)
  3440. {
  3441. if (Mode != ChompMode::Star)
  3442. {
  3443. ++inputOffset;
  3444. }
  3445. #if ENABLE_REGEX_CONFIG_OPTIONS
  3446. matcher.CompStats();
  3447. #endif
  3448. if (inputOffset < inputLength && input[inputOffset] == matchC)
  3449. {
  3450. if (Mode == ChompMode::Star)
  3451. {
  3452. ++inputOffset;
  3453. }
  3454. continue;
  3455. }
  3456. break;
  3457. }
  3458. if (!noNeedToSave)
  3459. {
  3460. // UNDO ACTION: Restore group on backtrack
  3461. PUSH(contStack, ResetGroupCont, groupId);
  3462. #if ENABLE_REGEX_CONFIG_OPTIONS
  3463. matcher.PushStats(contStack, input);
  3464. #endif
  3465. }
  3466. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  3467. groupInfo->offset = inputStartOffset;
  3468. groupInfo->length = inputOffset - inputStartOffset;
  3469. instPointer += sizeof(*this);
  3470. return false;
  3471. }
  3472. return matcher.Fail(FAIL_PARAMETERS);
  3473. }
  3474. #if ENABLE_REGEX_CONFIG_OPTIONS
  3475. template<ChompMode Mode>
  3476. int ChompCharGroupInst<Mode>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3477. {
  3478. if (Mode == ChompMode::Star)
  3479. {
  3480. PRINT_RE_BYTECODE_BEGIN("ChompCharGroup<Star>");
  3481. }
  3482. else
  3483. {
  3484. PRINT_RE_BYTECODE_BEGIN("ChompCharGroup<Plus>");
  3485. }
  3486. PRINT_MIXIN_COMMA(CharMixin);
  3487. PRINT_MIXIN_COMMA(GroupMixin);
  3488. PRINT_MIXIN(NoNeedToSaveMixin);
  3489. PRINT_RE_BYTECODE_MID();
  3490. PRINT_BYTES(CharMixin);
  3491. PRINT_BYTES(GroupMixin);
  3492. PRINT_BYTES(NoNeedToSaveMixin);
  3493. PRINT_RE_BYTECODE_END();
  3494. }
  3495. #endif
  3496. // ----------------------------------------------------------------------
  3497. // ChompSetGroupInst (optimized instruction)
  3498. // ----------------------------------------------------------------------
  3499. template<ChompMode Mode>
  3500. inline bool ChompSetGroupInst<Mode>::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3501. {
  3502. Assert(matcher.GroupIdToGroupInfo(groupId)->IsUndefined());
  3503. const CharCount inputStartOffset = inputOffset;
  3504. const RuntimeCharSet<Char>& matchSet = this->set;
  3505. #if ENABLE_REGEX_CONFIG_OPTIONS
  3506. matcher.CompStats();
  3507. #endif
  3508. if (Mode == ChompMode::Star || (inputOffset < inputLength && matchSet.Get(input[inputOffset])))
  3509. {
  3510. while (true)
  3511. {
  3512. if (Mode != ChompMode::Star)
  3513. {
  3514. ++inputOffset;
  3515. }
  3516. #if ENABLE_REGEX_CONFIG_OPTIONS
  3517. matcher.CompStats();
  3518. #endif
  3519. if (inputOffset < inputLength && matchSet.Get(input[inputOffset]))
  3520. {
  3521. if (Mode == ChompMode::Star)
  3522. {
  3523. ++inputOffset;
  3524. }
  3525. continue;
  3526. }
  3527. break;
  3528. }
  3529. if (!noNeedToSave)
  3530. {
  3531. // UNDO ACTION: Restore group on backtrack
  3532. PUSH(contStack, ResetGroupCont, groupId);
  3533. #if ENABLE_REGEX_CONFIG_OPTIONS
  3534. matcher.PushStats(contStack, input);
  3535. #endif
  3536. }
  3537. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  3538. groupInfo->offset = inputStartOffset;
  3539. groupInfo->length = inputOffset - inputStartOffset;
  3540. instPointer += sizeof(*this);
  3541. return false;
  3542. }
  3543. return matcher.Fail(FAIL_PARAMETERS);
  3544. }
  3545. #if ENABLE_REGEX_CONFIG_OPTIONS
  3546. template<ChompMode Mode>
  3547. int ChompSetGroupInst<Mode>::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3548. {
  3549. if (Mode == ChompMode::Star)
  3550. {
  3551. PRINT_RE_BYTECODE_BEGIN("ChompSetGroup<Star>");
  3552. }
  3553. else
  3554. {
  3555. PRINT_RE_BYTECODE_BEGIN("ChompSetGroup<Plus>");
  3556. }
  3557. PRINT_MIXIN_COMMA(SetMixin<false>);
  3558. PRINT_MIXIN_COMMA(GroupMixin);
  3559. PRINT_MIXIN(NoNeedToSaveMixin);
  3560. PRINT_RE_BYTECODE_MID();
  3561. PRINT_BYTES(SetMixin<false>);
  3562. PRINT_BYTES(GroupMixin);
  3563. PRINT_BYTES(NoNeedToSaveMixin);
  3564. PRINT_RE_BYTECODE_END();
  3565. }
  3566. #endif
  3567. // ----------------------------------------------------------------------
  3568. // ChompCharBoundedInst (optimized instruction)
  3569. // ----------------------------------------------------------------------
  3570. inline bool ChompCharBoundedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3571. {
  3572. const Char matchC = c;
  3573. const CharCount loopMatchStart = inputOffset;
  3574. const CharCountOrFlag repeatsUpper = repeats.upper;
  3575. const CharCount inputEndOffset =
  3576. static_cast<CharCount>(repeatsUpper) >= inputLength - inputOffset
  3577. ? inputLength
  3578. : inputOffset + static_cast<CharCount>(repeatsUpper);
  3579. #if ENABLE_REGEX_CONFIG_OPTIONS
  3580. matcher.CompStats();
  3581. #endif
  3582. while (inputOffset < inputEndOffset && input[inputOffset] == matchC)
  3583. {
  3584. #if ENABLE_REGEX_CONFIG_OPTIONS
  3585. matcher.CompStats();
  3586. #endif
  3587. inputOffset++;
  3588. }
  3589. if (inputOffset - loopMatchStart < repeats.lower)
  3590. {
  3591. return matcher.Fail(FAIL_PARAMETERS);
  3592. }
  3593. instPointer += sizeof(*this);
  3594. return false;
  3595. }
  3596. #if ENABLE_REGEX_CONFIG_OPTIONS
  3597. int ChompCharBoundedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3598. {
  3599. PRINT_RE_BYTECODE_BEGIN("ChompCharBounded");
  3600. PRINT_MIXIN_COMMA(CharMixin);
  3601. PRINT_MIXIN(ChompBoundedMixin);
  3602. PRINT_RE_BYTECODE_MID();
  3603. PRINT_BYTES(CharMixin);
  3604. PRINT_BYTES(ChompBoundedMixin);
  3605. PRINT_RE_BYTECODE_END();
  3606. }
  3607. #endif
  3608. // ----------------------------------------------------------------------
  3609. // ChompSetBoundedInst (optimized instruction)
  3610. // ----------------------------------------------------------------------
  3611. inline bool ChompSetBoundedInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3612. {
  3613. const RuntimeCharSet<Char>& matchSet = this->set;
  3614. const CharCount loopMatchStart = inputOffset;
  3615. const CharCountOrFlag repeatsUpper = repeats.upper;
  3616. const CharCount inputEndOffset =
  3617. static_cast<CharCount>(repeatsUpper) >= inputLength - inputOffset
  3618. ? inputLength
  3619. : inputOffset + static_cast<CharCount>(repeatsUpper);
  3620. #if ENABLE_REGEX_CONFIG_OPTIONS
  3621. matcher.CompStats();
  3622. #endif
  3623. while (inputOffset < inputEndOffset && matchSet.Get(input[inputOffset]))
  3624. {
  3625. #if ENABLE_REGEX_CONFIG_OPTIONS
  3626. matcher.CompStats();
  3627. #endif
  3628. inputOffset++;
  3629. }
  3630. if (inputOffset - loopMatchStart < repeats.lower)
  3631. {
  3632. return matcher.Fail(FAIL_PARAMETERS);
  3633. }
  3634. instPointer += sizeof(*this);
  3635. return false;
  3636. }
  3637. #if ENABLE_REGEX_CONFIG_OPTIONS
  3638. int ChompSetBoundedInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3639. {
  3640. PRINT_RE_BYTECODE_BEGIN("ChompSetBounded");
  3641. PRINT_MIXIN_COMMA(SetMixin<false>);
  3642. PRINT_MIXIN(ChompBoundedMixin);
  3643. PRINT_RE_BYTECODE_MID();
  3644. PRINT_BYTES(SetMixin<false>);
  3645. PRINT_BYTES(ChompBoundedMixin);
  3646. PRINT_RE_BYTECODE_END();
  3647. }
  3648. #endif
  3649. // ----------------------------------------------------------------------
  3650. // ChompSetBoundedGroupLastCharInst (optimized instruction)
  3651. // ----------------------------------------------------------------------
  3652. inline bool ChompSetBoundedGroupLastCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3653. {
  3654. Assert(matcher.GroupIdToGroupInfo(groupId)->IsUndefined());
  3655. const RuntimeCharSet<Char>& matchSet = this->set;
  3656. const CharCount loopMatchStart = inputOffset;
  3657. const CharCountOrFlag repeatsUpper = repeats.upper;
  3658. const CharCount inputEndOffset =
  3659. static_cast<CharCount>(repeatsUpper) >= inputLength - inputOffset
  3660. ? inputLength
  3661. : inputOffset + static_cast<CharCount>(repeatsUpper);
  3662. #if ENABLE_REGEX_CONFIG_OPTIONS
  3663. matcher.CompStats();
  3664. #endif
  3665. while (inputOffset < inputEndOffset && matchSet.Get(input[inputOffset]))
  3666. {
  3667. #if ENABLE_REGEX_CONFIG_OPTIONS
  3668. matcher.CompStats();
  3669. #endif
  3670. inputOffset++;
  3671. }
  3672. if (inputOffset - loopMatchStart < repeats.lower)
  3673. {
  3674. return matcher.Fail(FAIL_PARAMETERS);
  3675. }
  3676. if (inputOffset > loopMatchStart)
  3677. {
  3678. if (!noNeedToSave)
  3679. {
  3680. PUSH(contStack, ResetGroupCont, groupId);
  3681. #if ENABLE_REGEX_CONFIG_OPTIONS
  3682. matcher.PushStats(contStack, input);
  3683. #endif
  3684. }
  3685. GroupInfo *const groupInfo = matcher.GroupIdToGroupInfo(groupId);
  3686. groupInfo->offset = inputOffset - 1;
  3687. groupInfo->length = 1;
  3688. }
  3689. instPointer += sizeof(*this);
  3690. return false;
  3691. }
  3692. #if ENABLE_REGEX_CONFIG_OPTIONS
  3693. int ChompSetBoundedGroupLastCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3694. {
  3695. PRINT_RE_BYTECODE_BEGIN("ChompSetBoundedGroupLastChar");
  3696. PRINT_MIXIN_COMMA(SetMixin<false>);
  3697. PRINT_MIXIN_COMMA(ChompBoundedMixin);
  3698. PRINT_MIXIN_COMMA(GroupMixin);
  3699. PRINT_MIXIN(NoNeedToSaveMixin);
  3700. PRINT_RE_BYTECODE_MID();
  3701. PRINT_BYTES(SetMixin<false>);
  3702. PRINT_BYTES(ChompBoundedMixin);
  3703. PRINT_BYTES(GroupMixin);
  3704. PRINT_BYTES(NoNeedToSaveMixin);
  3705. PRINT_RE_BYTECODE_END();
  3706. }
  3707. #endif
  3708. // ----------------------------------------------------------------------
  3709. // TryInst
  3710. // ----------------------------------------------------------------------
  3711. inline bool TryInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3712. {
  3713. // CHOICEPOINT: Resume at fail label on backtrack
  3714. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3715. #if ENABLE_REGEX_CONFIG_OPTIONS
  3716. matcher.PushStats(contStack, input);
  3717. #endif
  3718. instPointer += sizeof(*this);
  3719. return false;
  3720. }
  3721. #if ENABLE_REGEX_CONFIG_OPTIONS
  3722. int TryInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3723. {
  3724. PRINT_RE_BYTECODE_BEGIN("Try");
  3725. PRINT_MIXIN(TryMixin);
  3726. PRINT_RE_BYTECODE_MID();
  3727. PRINT_BYTES(TryMixin);
  3728. PRINT_RE_BYTECODE_END();
  3729. }
  3730. #endif
  3731. // ----------------------------------------------------------------------
  3732. // TryIfCharInst (optimized instruction)
  3733. // ----------------------------------------------------------------------
  3734. inline bool TryIfCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3735. {
  3736. #if ENABLE_REGEX_CONFIG_OPTIONS
  3737. matcher.CompStats();
  3738. #endif
  3739. if (inputOffset < inputLength && input[inputOffset] == c)
  3740. {
  3741. // CHOICEPOINT: Resume at fail label on backtrack
  3742. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3743. #if ENABLE_REGEX_CONFIG_OPTIONS
  3744. matcher.PushStats(contStack, input);
  3745. #endif
  3746. instPointer += sizeof(*this);
  3747. return false;
  3748. }
  3749. // Proceed directly to exit
  3750. instPointer = matcher.LabelToInstPointer(failLabel);
  3751. return false;
  3752. }
  3753. #if ENABLE_REGEX_CONFIG_OPTIONS
  3754. int TryIfCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3755. {
  3756. PRINT_RE_BYTECODE_BEGIN("TryIfChar");
  3757. PRINT_MIXIN_COMMA(CharMixin);
  3758. PRINT_MIXIN(TryMixin);
  3759. PRINT_RE_BYTECODE_MID();
  3760. PRINT_BYTES(CharMixin);
  3761. PRINT_BYTES(TryMixin);
  3762. PRINT_RE_BYTECODE_END();
  3763. }
  3764. #endif
  3765. // ----------------------------------------------------------------------
  3766. // TryMatchCharInst (optimized instruction)
  3767. // ----------------------------------------------------------------------
  3768. inline bool TryMatchCharInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3769. {
  3770. #if ENABLE_REGEX_CONFIG_OPTIONS
  3771. matcher.CompStats();
  3772. #endif
  3773. if (inputOffset < inputLength && input[inputOffset] == c)
  3774. {
  3775. // CHOICEPOINT: Resume at fail label on backtrack
  3776. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3777. #if ENABLE_REGEX_CONFIG_OPTIONS
  3778. matcher.PushStats(contStack, input);
  3779. #endif
  3780. inputOffset++;
  3781. instPointer += sizeof(*this);
  3782. return false;
  3783. }
  3784. // Proceed directly to exit
  3785. instPointer = matcher.LabelToInstPointer(failLabel);
  3786. return false;
  3787. }
  3788. #if ENABLE_REGEX_CONFIG_OPTIONS
  3789. int TryMatchCharInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3790. {
  3791. PRINT_RE_BYTECODE_BEGIN("TryMatchChar");
  3792. PRINT_MIXIN_COMMA(CharMixin);
  3793. PRINT_MIXIN(TryMixin);
  3794. PRINT_RE_BYTECODE_MID();
  3795. PRINT_BYTES(CharMixin);
  3796. PRINT_BYTES(TryMixin);
  3797. PRINT_RE_BYTECODE_END();
  3798. }
  3799. #endif
  3800. // ----------------------------------------------------------------------
  3801. // TryIfSetInst (optimized instruction)
  3802. // ----------------------------------------------------------------------
  3803. inline bool TryIfSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3804. {
  3805. #if ENABLE_REGEX_CONFIG_OPTIONS
  3806. matcher.CompStats();
  3807. #endif
  3808. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  3809. {
  3810. // CHOICEPOINT: Resume at fail label on backtrack
  3811. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3812. #if ENABLE_REGEX_CONFIG_OPTIONS
  3813. matcher.PushStats(contStack, input);
  3814. #endif
  3815. instPointer += sizeof(*this);
  3816. return false;
  3817. }
  3818. // Proceed directly to exit
  3819. instPointer = matcher.LabelToInstPointer(failLabel);
  3820. return false;
  3821. }
  3822. #if ENABLE_REGEX_CONFIG_OPTIONS
  3823. int TryIfSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3824. {
  3825. PRINT_RE_BYTECODE_BEGIN("TryIfSet");
  3826. PRINT_MIXIN_COMMA(SetMixin<false>);
  3827. PRINT_MIXIN(TryMixin);
  3828. PRINT_RE_BYTECODE_MID();
  3829. PRINT_BYTES(SetMixin<false>);
  3830. PRINT_BYTES(TryMixin);
  3831. PRINT_RE_BYTECODE_END();
  3832. }
  3833. #endif
  3834. // ----------------------------------------------------------------------
  3835. // TryMatchSetInst (optimized instruction)
  3836. // ----------------------------------------------------------------------
  3837. inline bool TryMatchSetInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3838. {
  3839. #if ENABLE_REGEX_CONFIG_OPTIONS
  3840. matcher.CompStats();
  3841. #endif
  3842. if (inputOffset < inputLength && set.Get(input[inputOffset]))
  3843. {
  3844. // CHOICEPOINT: Resume at fail label on backtrack
  3845. PUSH(contStack, ResumeCont, inputOffset, failLabel);
  3846. #if ENABLE_REGEX_CONFIG_OPTIONS
  3847. matcher.PushStats(contStack, input);
  3848. #endif
  3849. inputOffset++;
  3850. instPointer += sizeof(*this);
  3851. return false;
  3852. }
  3853. // Proceed directly to exit
  3854. instPointer = matcher.LabelToInstPointer(failLabel);
  3855. return false;
  3856. }
  3857. #if ENABLE_REGEX_CONFIG_OPTIONS
  3858. int TryMatchSetInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3859. {
  3860. PRINT_RE_BYTECODE_BEGIN("TryMatchSet");
  3861. PRINT_MIXIN_COMMA(SetMixin<false>);
  3862. PRINT_MIXIN(TryMixin);
  3863. PRINT_RE_BYTECODE_MID();
  3864. PRINT_BYTES(SetMixin<false>);
  3865. PRINT_BYTES(TryMixin);
  3866. PRINT_RE_BYTECODE_END();
  3867. }
  3868. #endif
  3869. // ----------------------------------------------------------------------
  3870. // BeginAssertionInst
  3871. // ----------------------------------------------------------------------
  3872. inline bool BeginAssertionInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3873. {
  3874. Assert(instPointer == (uint8*)this);
  3875. if (!isNegation)
  3876. {
  3877. // If the positive assertion binds some groups then on success any RestoreGroup continuations pushed
  3878. // in the assertion body will be cut. Hence if the entire assertion is backtracked over we must restore
  3879. // the current inner group bindings.
  3880. matcher.SaveInnerGroups(minBodyGroupId, maxBodyGroupId, false, input, contStack);
  3881. }
  3882. PUSHA(assertionStack, AssertionInfo, matcher.InstPointerToLabel(instPointer), inputOffset, contStack.Position());
  3883. PUSH(contStack, PopAssertionCont);
  3884. #if ENABLE_REGEX_CONFIG_OPTIONS
  3885. matcher.PushStats(contStack, input);
  3886. #endif
  3887. instPointer += sizeof(*this);
  3888. return false;
  3889. }
  3890. #if ENABLE_REGEX_CONFIG_OPTIONS
  3891. int BeginAssertionInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3892. {
  3893. PRINT_RE_BYTECODE_BEGIN("BeginAssertion");
  3894. PRINT_MIXIN_COMMA(BodyGroupsMixin);
  3895. PRINT_MIXIN_COMMA(NegationMixin);
  3896. PRINT_MIXIN(NextLabelMixin);
  3897. PRINT_RE_BYTECODE_MID();
  3898. PRINT_BYTES(BodyGroupsMixin);
  3899. PRINT_BYTES(NegationMixin);
  3900. PRINT_BYTES(NextLabelMixin);
  3901. PRINT_RE_BYTECODE_END();
  3902. }
  3903. #endif
  3904. // ----------------------------------------------------------------------
  3905. // EndAssertionInst
  3906. // ----------------------------------------------------------------------
  3907. inline bool EndAssertionInst::Exec(REGEX_INST_EXEC_PARAMETERS) const
  3908. {
  3909. if (!matcher.PopAssertion(inputOffset, instPointer, contStack, assertionStack, true))
  3910. {
  3911. // Body of negative assertion succeeded, so backtrack
  3912. return matcher.Fail(FAIL_PARAMETERS);
  3913. }
  3914. // else: body of positive assertion succeeded, instruction pointer already at next instruction
  3915. return false;
  3916. }
  3917. #if ENABLE_REGEX_CONFIG_OPTIONS
  3918. int EndAssertionInst::Print(DebugWriter* w, Label label, const Char* litbuf) const
  3919. {
  3920. PRINT_RE_BYTECODE_BEGIN("EndAssertion");
  3921. PRINT_RE_BYTECODE_MID();
  3922. PRINT_BYTES(EndAssertionInst);
  3923. PRINT_RE_BYTECODE_END();
  3924. }
  3925. #endif
  3926. // ----------------------------------------------------------------------
  3927. // Matcher state
  3928. // ----------------------------------------------------------------------
  3929. #if ENABLE_REGEX_CONFIG_OPTIONS
  3930. void LoopInfo::Print(DebugWriter* w) const
  3931. {
  3932. w->Print(_u("number: %u, startInputOffset: %u"), number, startInputOffset);
  3933. }
  3934. #endif
  3935. void LoopInfo::EnsureOffsetsOfFollowFirst(Matcher& matcher)
  3936. {
  3937. if (this->offsetsOfFollowFirst == nullptr)
  3938. {
  3939. this->offsetsOfFollowFirst = JsUtil::List<CharCount, ArenaAllocator>::New(matcher.pattern->library->GetScriptContext()->RegexAllocator());
  3940. }
  3941. }
  3942. #if ENABLE_REGEX_CONFIG_OPTIONS
  3943. void GroupInfo::Print(DebugWriter* w, const Char* const input) const
  3944. {
  3945. if (IsUndefined())
  3946. {
  3947. w->Print(_u("<undefined> (%u)"), offset);
  3948. }
  3949. else
  3950. {
  3951. w->PrintQuotedString(input + offset, (CharCount)length);
  3952. w->Print(_u(" (%u+%u)"), offset, (CharCount)length);
  3953. }
  3954. }
  3955. #endif
  3956. #if ENABLE_REGEX_CONFIG_OPTIONS
  3957. void AssertionInfo::Print(DebugWriter* w) const
  3958. {
  3959. w->PrintEOL(_u("beginLabel: L%04x, startInputOffset: %u, contStackPosition: $llu"), beginLabel, startInputOffset, static_cast<unsigned long long>(contStackPosition));
  3960. }
  3961. #endif
  3962. // ----------------------------------------------------------------------
  3963. // ResumeCont
  3964. // ----------------------------------------------------------------------
  3965. inline bool ResumeCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3966. {
  3967. inputOffset = origInputOffset;
  3968. instPointer = matcher.LabelToInstPointer(origInstLabel);
  3969. return true; // STOP BACKTRACKING
  3970. }
  3971. #if ENABLE_REGEX_CONFIG_OPTIONS
  3972. int ResumeCont::Print(DebugWriter* w, const Char* const input) const
  3973. {
  3974. w->PrintEOL(_u("Resume(origInputOffset: %u, origInstLabel: L%04x)"), origInputOffset, origInstLabel);
  3975. return sizeof(*this);
  3976. }
  3977. #endif
  3978. // ----------------------------------------------------------------------
  3979. // RestoreLoopCont
  3980. // ----------------------------------------------------------------------
  3981. inline RestoreLoopCont::RestoreLoopCont(int loopId, LoopInfo& origLoopInfo, Matcher& matcher) : Cont(ContTag::RestoreLoop), loopId(loopId)
  3982. {
  3983. this->origLoopInfo.number = origLoopInfo.number;
  3984. this->origLoopInfo.startInputOffset = origLoopInfo.startInputOffset;
  3985. this->origLoopInfo.offsetsOfFollowFirst = nullptr;
  3986. if (origLoopInfo.offsetsOfFollowFirst != nullptr)
  3987. {
  3988. this->origLoopInfo.offsetsOfFollowFirst = JsUtil::List<CharCount, ArenaAllocator>::New(matcher.pattern->library->GetScriptContext()->RegexAllocator());
  3989. this->origLoopInfo.offsetsOfFollowFirst->Copy(origLoopInfo.offsetsOfFollowFirst);
  3990. }
  3991. }
  3992. inline bool RestoreLoopCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  3993. {
  3994. matcher.QueryContinue(qcTicks);
  3995. *matcher.LoopIdToLoopInfo(loopId) = origLoopInfo;
  3996. return false; // KEEP BACKTRACKING
  3997. }
  3998. #if ENABLE_REGEX_CONFIG_OPTIONS
  3999. int RestoreLoopCont::Print(DebugWriter* w, const Char* const input) const
  4000. {
  4001. w->Print(_u("RestoreLoop(loopId: %d, "), loopId);
  4002. origLoopInfo.Print(w);
  4003. w->PrintEOL(_u(")"));
  4004. return sizeof(*this);
  4005. }
  4006. #endif
  4007. // ----------------------------------------------------------------------
  4008. // RestoreGroupCont
  4009. // ----------------------------------------------------------------------
  4010. inline bool RestoreGroupCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  4011. {
  4012. *matcher.GroupIdToGroupInfo(groupId) = origGroupInfo;
  4013. return false; // KEEP BACKTRACKING
  4014. }
  4015. #if ENABLE_REGEX_CONFIG_OPTIONS
  4016. int RestoreGroupCont::Print(DebugWriter* w, const Char* const input) const
  4017. {
  4018. w->Print(_u("RestoreGroup(groupId: %d, "), groupId);
  4019. origGroupInfo.Print(w, input);
  4020. w->PrintEOL(_u(")"));
  4021. return sizeof(*this);
  4022. }
  4023. #endif
  4024. // ----------------------------------------------------------------------
  4025. // ResetGroupCont
  4026. // ----------------------------------------------------------------------
  4027. inline bool ResetGroupCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  4028. {
  4029. matcher.ResetGroup(groupId);
  4030. return false; // KEEP BACKTRACKING
  4031. }
  4032. #if ENABLE_REGEX_CONFIG_OPTIONS
  4033. int ResetGroupCont::Print(DebugWriter* w, const Char* const input) const
  4034. {
  4035. w->PrintEOL(_u("ResetGroup(groupId: %d)"), groupId);
  4036. return sizeof(*this);
  4037. }
  4038. #endif
  4039. // ----------------------------------------------------------------------
  4040. // ResetGroupRangeCont
  4041. // ----------------------------------------------------------------------
  4042. inline bool ResetGroupRangeCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  4043. {
  4044. matcher.ResetInnerGroups(fromGroupId, toGroupId);
  4045. return false; // KEEP BACKTRACKING
  4046. }
  4047. #if ENABLE_REGEX_CONFIG_OPTIONS
  4048. int ResetGroupRangeCont::Print(DebugWriter* w, const Char* const input) const
  4049. {
  4050. w->PrintEOL(_u("ResetGroupRange(fromGroupId: %d, toGroupId: %d)"), fromGroupId, toGroupId);
  4051. return sizeof(*this);
  4052. }
  4053. #endif
  4054. // ----------------------------------------------------------------------
  4055. // RepeatLoopCont
  4056. // ----------------------------------------------------------------------
  4057. inline bool RepeatLoopCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  4058. {
  4059. matcher.QueryContinue(qcTicks);
  4060. // Try one more iteration of a non-greedy loop
  4061. BeginLoopInst* begin = matcher.L2I(BeginLoop, beginLabel);
  4062. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  4063. loopInfo->startInputOffset = inputOffset = origInputOffset;
  4064. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(BeginLoopInst));
  4065. if(begin->hasInnerNondet)
  4066. {
  4067. // If it backtracks into the loop body of an earlier iteration, it must restore inner groups for that iteration.
  4068. // Save the inner groups and reset them for the next iteration.
  4069. matcher.SaveInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId, true, input, contStack);
  4070. }
  4071. else
  4072. {
  4073. // If it backtracks, the entire loop will fail, so no need to restore groups. Just reset the inner groups for
  4074. // the next iteration.
  4075. matcher.ResetInnerGroups(begin->minBodyGroupId, begin->maxBodyGroupId);
  4076. }
  4077. return true; // STOP BACKTRACKING
  4078. }
  4079. #if ENABLE_REGEX_CONFIG_OPTIONS
  4080. int RepeatLoopCont::Print(DebugWriter* w, const Char* const input) const
  4081. {
  4082. w->PrintEOL(_u("RepeatLoop(beginLabel: L%04x, origInputOffset: %u)"), beginLabel, origInputOffset);
  4083. return sizeof(*this);
  4084. }
  4085. #endif
  4086. // ----------------------------------------------------------------------
  4087. // PopAssertionCont
  4088. // ----------------------------------------------------------------------
  4089. inline bool PopAssertionCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  4090. {
  4091. Assert(!assertionStack.IsEmpty());
  4092. if (matcher.PopAssertion(inputOffset, instPointer, contStack, assertionStack, false))
  4093. {
  4094. // Body of negative assertion failed
  4095. return true; // STOP BACKTRACKING
  4096. }
  4097. else
  4098. {
  4099. // Body of positive assertion failed
  4100. return false; // CONTINUE BACKTRACKING
  4101. }
  4102. }
  4103. #if ENABLE_REGEX_CONFIG_OPTIONS
  4104. int PopAssertionCont::Print(DebugWriter* w, const Char* const input) const
  4105. {
  4106. w->PrintEOL(_u("PopAssertion()"));
  4107. return sizeof(*this);
  4108. }
  4109. #endif
  4110. // ----------------------------------------------------------------------
  4111. // RewindLoopFixedCont
  4112. // ----------------------------------------------------------------------
  4113. inline bool RewindLoopFixedCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  4114. {
  4115. matcher.QueryContinue(qcTicks);
  4116. BeginLoopFixedInst* begin = matcher.L2I(BeginLoopFixed, beginLabel);
  4117. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  4118. if (tryingBody)
  4119. {
  4120. tryingBody = false;
  4121. // loopInfo->number is the number of iterations completed before trying body
  4122. Assert(loopInfo->number >= begin->repeats.lower);
  4123. }
  4124. else
  4125. {
  4126. // loopInfo->number is the number of iterations completed before trying follow
  4127. Assert(loopInfo->number > begin->repeats.lower);
  4128. // Try follow with one fewer iteration
  4129. loopInfo->number--;
  4130. }
  4131. // Rewind input
  4132. inputOffset = loopInfo->startInputOffset + loopInfo->number * begin->length;
  4133. if (loopInfo->number > begin->repeats.lower)
  4134. {
  4135. // Un-pop the continuation ready for next time
  4136. contStack.UnPop<RewindLoopFixedCont>();
  4137. #if ENABLE_REGEX_CONFIG_OPTIONS
  4138. matcher.UnPopStats(contStack, input);
  4139. #endif
  4140. }
  4141. // else: Can't try any fewer iterations if follow fails, so leave continuation as popped and let failure propagate
  4142. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  4143. return true; // STOP BACKTRACKING
  4144. }
  4145. #if ENABLE_REGEX_CONFIG_OPTIONS
  4146. int RewindLoopFixedCont::Print(DebugWriter* w, const Char* const input) const
  4147. {
  4148. w->PrintEOL(_u("RewindLoopFixed(beginLabel: L%04x, tryingBody: %s)"), beginLabel, tryingBody ? _u("true") : _u("false"));
  4149. return sizeof(*this);
  4150. }
  4151. #endif
  4152. // ----------------------------------------------------------------------
  4153. // RewindLoopSetCont
  4154. // ----------------------------------------------------------------------
  4155. inline bool RewindLoopSetCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  4156. {
  4157. matcher.QueryContinue(qcTicks);
  4158. LoopSetInst* begin = matcher.L2I(LoopSet, beginLabel);
  4159. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  4160. // loopInfo->number is the number of iterations completed before trying follow
  4161. Assert(loopInfo->number > begin->repeats.lower);
  4162. // Try follow with fewer iterations
  4163. loopInfo->number--;
  4164. // Rewind input
  4165. inputOffset = loopInfo->startInputOffset + loopInfo->number;
  4166. if (loopInfo->number > begin->repeats.lower)
  4167. {
  4168. // Un-pop the continuation ready for next time
  4169. contStack.UnPop<RewindLoopSetCont>();
  4170. #if ENABLE_REGEX_CONFIG_OPTIONS
  4171. matcher.UnPopStats(contStack, input);
  4172. #endif
  4173. }
  4174. // else: Can't try any fewer iterations if follow fails, so leave continuation as popped and let failure propagate
  4175. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(LoopSetInst));
  4176. return true; // STOP BACKTRACKING
  4177. }
  4178. #if ENABLE_REGEX_CONFIG_OPTIONS
  4179. int RewindLoopSetCont::Print(DebugWriter* w, const Char* const input) const
  4180. {
  4181. w->PrintEOL(_u("RewindLoopSet(beginLabel: L%04x)"), beginLabel);
  4182. return sizeof(*this);
  4183. }
  4184. #endif
  4185. // ----------------------------------------------------------------------
  4186. // RewindLoopSetWithFollowFirstCont
  4187. // ----------------------------------------------------------------------
  4188. inline bool RewindLoopSetWithFollowFirstCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  4189. {
  4190. matcher.QueryContinue(qcTicks);
  4191. LoopSetWithFollowFirstInst* begin = matcher.L2I(LoopSetWithFollowFirst, beginLabel);
  4192. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  4193. // loopInfo->number is the number of iterations completed before trying follow
  4194. Assert(loopInfo->number > begin->repeats.lower);
  4195. // Try follow with fewer iterations
  4196. if (loopInfo->offsetsOfFollowFirst == nullptr)
  4197. {
  4198. if (begin->followFirst != MaxUChar)
  4199. {
  4200. // We determined the first character in the follow set at compile time,
  4201. // but didn't find a single match for it in the last iteration of the loop.
  4202. // So, there is no benefit in backtracking.
  4203. loopInfo->number = begin->repeats.lower; // stop backtracking
  4204. }
  4205. else
  4206. {
  4207. // We couldn't determine the first character in the follow set at compile time;
  4208. // fall back to backtracking by one character at a time.
  4209. loopInfo->number--;
  4210. }
  4211. }
  4212. else
  4213. {
  4214. if (loopInfo->offsetsOfFollowFirst->Empty())
  4215. {
  4216. // We have already backtracked to the first offset where we matched the LoopSet's followFirst;
  4217. // no point in backtracking more.
  4218. loopInfo->number = begin->repeats.lower; // stop backtracking
  4219. }
  4220. else
  4221. {
  4222. // Backtrack to the previous offset where we matched the LoopSet's followFirst
  4223. // We will be doing one unnecessary match. But, if we wanted to avoid it, we'd have
  4224. // to propagate to the next Inst, that the first character is already matched.
  4225. // Seems like an overkill to avoid one match.
  4226. loopInfo->number = loopInfo->offsetsOfFollowFirst->RemoveAtEnd();
  4227. }
  4228. }
  4229. // If loopInfo->number now is less than begins->repeats.lower, the loop
  4230. // shouldn't match anything. In that case, stop backtracking.
  4231. loopInfo->number = max(loopInfo->number, begin->repeats.lower);
  4232. // Rewind input
  4233. inputOffset = loopInfo->startInputOffset + loopInfo->number;
  4234. if (loopInfo->number > begin->repeats.lower)
  4235. {
  4236. // Un-pop the continuation ready for next time
  4237. contStack.UnPop<RewindLoopSetWithFollowFirstCont>();
  4238. #if ENABLE_REGEX_CONFIG_OPTIONS
  4239. matcher.UnPopStats(contStack, input);
  4240. #endif
  4241. }
  4242. // else: Can't try any fewer iterations if follow fails, so leave continuation as popped and let failure propagate
  4243. instPointer = matcher.LabelToInstPointer(beginLabel + sizeof(LoopSetWithFollowFirstInst));
  4244. return true; // STOP BACKTRACKING
  4245. }
  4246. #if ENABLE_REGEX_CONFIG_OPTIONS
  4247. int RewindLoopSetWithFollowFirstCont::Print(DebugWriter* w, const Char* const input) const
  4248. {
  4249. w->PrintEOL(_u("RewindLoopSetWithFollowFirst(beginLabel: L%04x)"), beginLabel);
  4250. return sizeof(*this);
  4251. }
  4252. #endif
  4253. // ----------------------------------------------------------------------
  4254. // RewindLoopFixedGroupLastIterationCont
  4255. // ----------------------------------------------------------------------
  4256. inline bool RewindLoopFixedGroupLastIterationCont::Exec(REGEX_CONT_EXEC_PARAMETERS)
  4257. {
  4258. matcher.QueryContinue(qcTicks);
  4259. BeginLoopFixedGroupLastIterationInst* begin = matcher.L2I(BeginLoopFixedGroupLastIteration, beginLabel);
  4260. LoopInfo* loopInfo = matcher.LoopIdToLoopInfo(begin->loopId);
  4261. GroupInfo* groupInfo = matcher.GroupIdToGroupInfo(begin->groupId);
  4262. if (tryingBody)
  4263. {
  4264. tryingBody = false;
  4265. // loopInfo->number is the number of iterations completed before current attempt of body
  4266. Assert(loopInfo->number >= begin->repeats.lower);
  4267. }
  4268. else
  4269. {
  4270. // loopInfo->number is the number of iterations completed before trying follow
  4271. Assert(loopInfo->number > begin->repeats.lower);
  4272. // Try follow with one fewer iteration
  4273. loopInfo->number--;
  4274. }
  4275. // Rewind input
  4276. inputOffset = loopInfo->startInputOffset + loopInfo->number * begin->length;
  4277. if (loopInfo->number > 0)
  4278. {
  4279. // Bind previous iteration's body
  4280. groupInfo->offset = inputOffset - begin->length;
  4281. groupInfo->length = begin->length;
  4282. }
  4283. else
  4284. {
  4285. groupInfo->Reset();
  4286. }
  4287. if (loopInfo->number > begin->repeats.lower)
  4288. {
  4289. // Un-pop the continuation ready for next time
  4290. contStack.UnPop<RewindLoopFixedGroupLastIterationCont>();
  4291. #if ENABLE_REGEX_CONFIG_OPTIONS
  4292. matcher.UnPopStats(contStack, input);
  4293. #endif
  4294. }
  4295. // else: Can't try any fewer iterations if follow fails, so leave continuation as popped and let failure propagate
  4296. instPointer = matcher.LabelToInstPointer(begin->exitLabel);
  4297. return true; // STOP BACKTRACKING
  4298. }
  4299. #if ENABLE_REGEX_CONFIG_OPTIONS
  4300. int RewindLoopFixedGroupLastIterationCont::Print(DebugWriter* w, const Char* const input) const
  4301. {
  4302. w->PrintEOL(_u("RewindLoopFixedGroupLastIteration(beginLabel: L%04x, tryingBody: %s)"), beginLabel, tryingBody ? _u("true") : _u("false"));
  4303. return sizeof(*this);
  4304. }
  4305. #endif
  4306. // ----------------------------------------------------------------------
  4307. // Matcher
  4308. // ----------------------------------------------------------------------
  4309. #if ENABLE_REGEX_CONFIG_OPTIONS
  4310. void ContStack::Print(DebugWriter* w, const Char* const input) const
  4311. {
  4312. for (Iterator it(*this); it; ++it)
  4313. {
  4314. w->Print(_u("%4llu: "), static_cast<unsigned long long>(it.Position()));
  4315. it->Print(w, input);
  4316. }
  4317. }
  4318. #endif
  4319. #if ENABLE_REGEX_CONFIG_OPTIONS
  4320. void AssertionStack::Print(DebugWriter* w, const Matcher* matcher) const
  4321. {
  4322. for (Iterator it(*this); it; ++it)
  4323. {
  4324. it->Print(w);
  4325. }
  4326. }
  4327. #endif
  4328. Matcher::Matcher(Js::ScriptContext* scriptContext, RegexPattern* pattern)
  4329. : pattern(pattern)
  4330. , standardChars(scriptContext->GetThreadContext()->GetStandardChars((char16*)0))
  4331. , program(pattern->rep.unified.program)
  4332. , groupInfos(nullptr)
  4333. , loopInfos(nullptr)
  4334. , literalNextSyncInputOffsets(nullptr)
  4335. , recycler(scriptContext->GetRecycler())
  4336. , previousQcTime(0)
  4337. #if ENABLE_REGEX_CONFIG_OPTIONS
  4338. , stats(0)
  4339. , w(0)
  4340. #endif
  4341. {
  4342. // Don't need to zero out - the constructor for GroupInfo should take care of it
  4343. groupInfos = RecyclerNewArrayLeaf(recycler, GroupInfo, program->numGroups);
  4344. if (program->numLoops > 0)
  4345. {
  4346. loopInfos = RecyclerNewArrayLeafZ(recycler, LoopInfo, program->numLoops);
  4347. }
  4348. }
  4349. Matcher *Matcher::New(Js::ScriptContext* scriptContext, RegexPattern* pattern)
  4350. {
  4351. return RecyclerNew(scriptContext->GetRecycler(), Matcher, scriptContext, pattern);
  4352. }
  4353. Matcher *Matcher::CloneToScriptContext(Js::ScriptContext *scriptContext, RegexPattern *pattern)
  4354. {
  4355. Matcher *result = New(scriptContext, pattern);
  4356. if (groupInfos)
  4357. {
  4358. size_t size = program->numGroups * sizeof(GroupInfo);
  4359. js_memcpy_s(result->groupInfos, size, groupInfos, size);
  4360. }
  4361. if (loopInfos)
  4362. {
  4363. size_t size = program->numLoops * sizeof(LoopInfo);
  4364. js_memcpy_s(result->loopInfos, size, loopInfos, size);
  4365. }
  4366. return result;
  4367. }
  4368. #if DBG
  4369. const Cont::ContTag contTags[] = {
  4370. #define M(O) Cont::ContTag::O,
  4371. #include "RegexContcodes.h"
  4372. #undef M
  4373. };
  4374. const Cont::ContTag minContTag = contTags[0];
  4375. const Cont::ContTag maxContTag = contTags[(sizeof(contTags) / sizeof(Cont::ContTag)) - 1];
  4376. #endif
  4377. void Matcher::DoQueryContinue(const uint qcTicks)
  4378. {
  4379. // See definition of TimePerQc for description of regex QC heuristics
  4380. const uint before = previousQcTime;
  4381. const uint now = GetTickCount();
  4382. if ((!before || now - before < TimePerQc) && qcTicks & TicksPerQc - 1)
  4383. {
  4384. return;
  4385. }
  4386. previousQcTime = now;
  4387. TraceQueryContinue(now);
  4388. // Query-continue can be reentrant and run the same regex again. To prevent the matcher and other persistent objects
  4389. // from being reused reentrantly, save and restore them around the QC call.
  4390. class AutoCleanup
  4391. {
  4392. private:
  4393. RegexPattern *const pattern;
  4394. Matcher *const matcher;
  4395. RegexStacks * regexStacks;
  4396. public:
  4397. AutoCleanup(RegexPattern *const pattern, Matcher *const matcher) : pattern(pattern), matcher(matcher)
  4398. {
  4399. Assert(pattern);
  4400. Assert(matcher);
  4401. Assert(pattern->rep.unified.matcher == matcher);
  4402. pattern->rep.unified.matcher = nullptr;
  4403. const auto scriptContext = pattern->GetScriptContext();
  4404. regexStacks = scriptContext->SaveRegexStacks();
  4405. }
  4406. ~AutoCleanup()
  4407. {
  4408. pattern->rep.unified.matcher = matcher;
  4409. const auto scriptContext = pattern->GetScriptContext();
  4410. scriptContext->RestoreRegexStacks(regexStacks);
  4411. }
  4412. } autoCleanup(pattern, this);
  4413. pattern->GetScriptContext()->GetThreadContext()->CheckScriptInterrupt();
  4414. }
  4415. void Matcher::TraceQueryContinue(const uint now)
  4416. {
  4417. if (!PHASE_TRACE1(Js::RegexQcPhase))
  4418. {
  4419. return;
  4420. }
  4421. Output::Print(_u("Regex QC"));
  4422. static uint n = 0;
  4423. static uint firstQcTime = 0;
  4424. ++n;
  4425. if (firstQcTime)
  4426. {
  4427. Output::Print(_u(" - frequency: %0.1f"), static_cast<double>(n * 1000) / (now - firstQcTime));
  4428. }
  4429. else
  4430. {
  4431. firstQcTime = now;
  4432. }
  4433. Output::Print(_u("\n"));
  4434. Output::Flush();
  4435. }
  4436. bool Matcher::Fail(const Char* const input, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks)
  4437. {
  4438. if (!contStack.IsEmpty())
  4439. {
  4440. if (!RunContStack(input, inputOffset, instPointer, contStack, assertionStack, qcTicks))
  4441. {
  4442. return false;
  4443. }
  4444. }
  4445. Assert(assertionStack.IsEmpty());
  4446. groupInfos[0].Reset();
  4447. return true; // STOP EXECUTION
  4448. }
  4449. inline bool Matcher::RunContStack(const Char* const input, CharCount &inputOffset, const uint8 *&instPointer, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks)
  4450. {
  4451. while (true)
  4452. {
  4453. #if ENABLE_REGEX_CONFIG_OPTIONS
  4454. PopStats(contStack, input);
  4455. #endif
  4456. Cont* cont = contStack.Pop();
  4457. if (cont == 0)
  4458. {
  4459. break;
  4460. }
  4461. Assert(cont->tag >= minContTag && cont->tag <= maxContTag);
  4462. // All these cases RESUME EXECUTION if backtracking finds a stop point
  4463. const Cont::ContTag tag = cont->tag;
  4464. switch (tag)
  4465. {
  4466. #define M(O) case Cont::ContTag::O: if (((O##Cont*)cont)->Exec(*this, input, inputOffset, instPointer, contStack, assertionStack, qcTicks)) return false; break;
  4467. #include "RegexContcodes.h"
  4468. #undef M
  4469. default:
  4470. Assert(false); // should never be reached
  4471. return false; // however, can't use complier optimization if we wnat to return false here
  4472. }
  4473. }
  4474. return true;
  4475. }
  4476. #if DBG
  4477. const Inst::InstTag instTags[] = {
  4478. #define M(TagName) Inst::InstTag::TagName,
  4479. #define MTemplate(TagName, ...) M(TagName)
  4480. #include "RegexOpCodes.h"
  4481. #undef M
  4482. #undef MTemplate
  4483. };
  4484. const Inst::InstTag minInstTag = instTags[0];
  4485. const Inst::InstTag maxInstTag = instTags[(sizeof(instTags) / sizeof(Inst::InstTag)) - 1];
  4486. #endif
  4487. inline void Matcher::Run(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &nextSyncInputOffset, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks, bool firstIteration)
  4488. {
  4489. CharCount inputOffset = matchStart;
  4490. const uint8 *instPointer = program->rep.insts.insts;
  4491. Assert(instPointer != 0);
  4492. while (true)
  4493. {
  4494. Assert(inputOffset >= matchStart && inputOffset <= inputLength);
  4495. Assert(instPointer >= program->rep.insts.insts && instPointer < program->rep.insts.insts + program->rep.insts.instsLen);
  4496. Assert(((Inst*)instPointer)->tag >= minInstTag && ((Inst*)instPointer)->tag <= maxInstTag);
  4497. #if ENABLE_REGEX_CONFIG_OPTIONS
  4498. if (w != 0)
  4499. {
  4500. Print(w, input, inputLength, inputOffset, instPointer, contStack, assertionStack);
  4501. }
  4502. InstStats();
  4503. #endif
  4504. const Inst *inst = (const Inst*)instPointer;
  4505. const Inst::InstTag tag = inst->tag;
  4506. switch (tag)
  4507. {
  4508. #define MBase(TagName, ClassName) \
  4509. case Inst::InstTag::TagName: \
  4510. if (((const ClassName *)inst)->Exec(*this, input, inputLength, matchStart, inputOffset, nextSyncInputOffset, instPointer, contStack, assertionStack, qcTicks, firstIteration)) { return; } \
  4511. break;
  4512. #define M(TagName) MBase(TagName, TagName##Inst)
  4513. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, SpecializedClassName) MBase(TagName, SpecializedClassName)
  4514. #include "RegexOpCodes.h"
  4515. #undef MBase
  4516. #undef M
  4517. #undef MTemplate
  4518. default:
  4519. Assert(false);
  4520. __assume(false);
  4521. }
  4522. }
  4523. }
  4524. #if DBG
  4525. void Matcher::ResetLoopInfos()
  4526. {
  4527. for (int i = 0; i < program->numLoops; i++)
  4528. {
  4529. loopInfos[i].Reset();
  4530. }
  4531. }
  4532. #endif
  4533. inline bool Matcher::MatchHere(const Char* const input, const CharCount inputLength, CharCount &matchStart, CharCount &nextSyncInputOffset, ContStack &contStack, AssertionStack &assertionStack, uint &qcTicks, bool firstIteration)
  4534. {
  4535. // Reset the continuation and assertion stacks ready for fresh run
  4536. // NOTE: We used to do this after the Run, but it's safer to do it here in case unusual control flow exits
  4537. // the matcher without executing the clears.
  4538. contStack.Clear();
  4539. // assertionStack may be non-empty since we can hard fail directly out of matcher without popping assertion
  4540. assertionStack.Clear();
  4541. Assert(contStack.IsEmpty());
  4542. Assert(assertionStack.IsEmpty());
  4543. ResetInnerGroups(0, program->numGroups - 1);
  4544. #if DBG
  4545. ResetLoopInfos();
  4546. #endif
  4547. Run(input, inputLength, matchStart, nextSyncInputOffset, contStack, assertionStack, qcTicks, firstIteration);
  4548. // Leave the continuation and assertion stack memory in place so we don't have to alloc next time
  4549. return WasLastMatchSuccessful();
  4550. }
  4551. inline bool Matcher::MatchSingleCharCaseInsensitive(const Char* const input, const CharCount inputLength, CharCount offset, const Char c)
  4552. {
  4553. CaseInsensitive::MappingSource mappingSource = program->GetCaseMappingSource();
  4554. // If sticky flag is present, break since the 1st character didn't match the pattern character
  4555. if ((program->flags & StickyRegexFlag) != 0)
  4556. {
  4557. #if ENABLE_REGEX_CONFIG_OPTIONS
  4558. CompStats();
  4559. #endif
  4560. if (MatchSingleCharCaseInsensitiveHere(mappingSource, input, offset, c))
  4561. {
  4562. GroupInfo* const info = GroupIdToGroupInfo(0);
  4563. info->offset = offset;
  4564. info->length = 1;
  4565. return true;
  4566. }
  4567. else
  4568. {
  4569. ResetGroup(0);
  4570. return false;
  4571. }
  4572. }
  4573. while (offset < inputLength)
  4574. {
  4575. #if ENABLE_REGEX_CONFIG_OPTIONS
  4576. CompStats();
  4577. #endif
  4578. if (MatchSingleCharCaseInsensitiveHere(mappingSource, input, offset, c))
  4579. {
  4580. GroupInfo* const info = GroupIdToGroupInfo(0);
  4581. info->offset = offset;
  4582. info->length = 1;
  4583. return true;
  4584. }
  4585. offset++;
  4586. }
  4587. ResetGroup(0);
  4588. return false;
  4589. }
  4590. inline bool Matcher::MatchSingleCharCaseInsensitiveHere(
  4591. CaseInsensitive::MappingSource mappingSource,
  4592. const Char* const input,
  4593. const CharCount offset,
  4594. const Char c)
  4595. {
  4596. return (standardChars->ToCanonical(mappingSource, input[offset]) == standardChars->ToCanonical(mappingSource, c));
  4597. }
  4598. inline bool Matcher::MatchSingleCharCaseSensitive(const Char* const input, const CharCount inputLength, CharCount offset, const Char c)
  4599. {
  4600. // If sticky flag is present, break since the 1st character didn't match the pattern character
  4601. if ((program->flags & StickyRegexFlag) != 0)
  4602. {
  4603. #if ENABLE_REGEX_CONFIG_OPTIONS
  4604. CompStats();
  4605. #endif
  4606. if (input[offset] == c)
  4607. {
  4608. GroupInfo* const info = GroupIdToGroupInfo(0);
  4609. info->offset = offset;
  4610. info->length = 1;
  4611. return true;
  4612. }
  4613. else
  4614. {
  4615. ResetGroup(0);
  4616. return false;
  4617. }
  4618. }
  4619. while (offset < inputLength)
  4620. {
  4621. #if ENABLE_REGEX_CONFIG_OPTIONS
  4622. CompStats();
  4623. #endif
  4624. if (input[offset] == c)
  4625. {
  4626. GroupInfo* const info = GroupIdToGroupInfo(0);
  4627. info->offset = offset;
  4628. info->length = 1;
  4629. return true;
  4630. }
  4631. offset++;
  4632. }
  4633. ResetGroup(0);
  4634. return false;
  4635. }
  4636. inline bool Matcher::MatchBoundedWord(const Char* const input, const CharCount inputLength, CharCount offset)
  4637. {
  4638. const StandardChars<Char>& stdchrs = *standardChars;
  4639. if (offset >= inputLength)
  4640. {
  4641. ResetGroup(0);
  4642. return false;
  4643. }
  4644. #if ENABLE_REGEX_CONFIG_OPTIONS
  4645. CompStats();
  4646. #endif
  4647. if ((offset == 0 && stdchrs.IsWord(input[0])) ||
  4648. (offset > 0 && (!stdchrs.IsWord(input[offset - 1]) && stdchrs.IsWord(input[offset]))))
  4649. {
  4650. // Already at start of word
  4651. }
  4652. // If sticky flag is present, return false since we are not at the beginning of the word yet
  4653. else if ((program->flags & StickyRegexFlag) == StickyRegexFlag)
  4654. {
  4655. ResetGroup(0);
  4656. return false;
  4657. }
  4658. else
  4659. {
  4660. if (stdchrs.IsWord(input[offset]))
  4661. {
  4662. // Scan for end of current word
  4663. while (true)
  4664. {
  4665. offset++;
  4666. if (offset >= inputLength)
  4667. {
  4668. ResetGroup(0);
  4669. return false;
  4670. }
  4671. #if ENABLE_REGEX_CONFIG_OPTIONS
  4672. CompStats();
  4673. #endif
  4674. if (!stdchrs.IsWord(input[offset]))
  4675. {
  4676. break;
  4677. }
  4678. }
  4679. }
  4680. // Scan for start of next word
  4681. while (true)
  4682. {
  4683. offset++;
  4684. if (offset >= inputLength)
  4685. {
  4686. ResetGroup(0);
  4687. return false;
  4688. }
  4689. #if ENABLE_REGEX_CONFIG_OPTIONS
  4690. CompStats();
  4691. #endif
  4692. if (stdchrs.IsWord(input[offset]))
  4693. {
  4694. break;
  4695. }
  4696. }
  4697. }
  4698. GroupInfo* const info = GroupIdToGroupInfo(0);
  4699. info->offset = offset;
  4700. // Scan for end of word
  4701. do
  4702. {
  4703. offset++;
  4704. #if ENABLE_REGEX_CONFIG_OPTIONS
  4705. CompStats();
  4706. #endif
  4707. }
  4708. while (offset < inputLength && stdchrs.IsWord(input[offset]));
  4709. info->length = offset - info->offset;
  4710. return true;
  4711. }
  4712. inline bool Matcher::MatchLeadingTrailingSpaces(const Char* const input, const CharCount inputLength, CharCount offset)
  4713. {
  4714. GroupInfo* const info = GroupIdToGroupInfo(0);
  4715. Assert(offset <= inputLength);
  4716. Assert((program->flags & MultilineRegexFlag) == 0);
  4717. if (offset >= inputLength)
  4718. {
  4719. Assert(offset == inputLength);
  4720. if (program->rep.leadingTrailingSpaces.endMinMatch == 0 ||
  4721. (offset == 0 && program->rep.leadingTrailingSpaces.beginMinMatch == 0))
  4722. {
  4723. info->offset = offset;
  4724. info->length = 0;
  4725. return true;
  4726. }
  4727. info->Reset();
  4728. return false;
  4729. }
  4730. const StandardChars<Char> &stdchrs = *standardChars;
  4731. if (offset == 0)
  4732. {
  4733. while (offset < inputLength && stdchrs.IsWhitespaceOrNewline(input[offset]))
  4734. {
  4735. offset++;
  4736. #if ENABLE_REGEX_CONFIG_OPTIONS
  4737. CompStats();
  4738. #endif
  4739. }
  4740. if (offset >= program->rep.leadingTrailingSpaces.beginMinMatch)
  4741. {
  4742. info->offset = 0;
  4743. info->length = offset;
  4744. return true;
  4745. }
  4746. }
  4747. Assert(inputLength > 0);
  4748. const CharCount initOffset = offset;
  4749. offset = inputLength - 1;
  4750. while (offset >= initOffset && stdchrs.IsWhitespaceOrNewline(input[offset]))
  4751. {
  4752. // This can never underflow since initOffset > 0
  4753. Assert(offset > 0);
  4754. offset--;
  4755. #if ENABLE_REGEX_CONFIG_OPTIONS
  4756. CompStats();
  4757. #endif
  4758. }
  4759. offset++;
  4760. CharCount length = inputLength - offset;
  4761. if (length >= program->rep.leadingTrailingSpaces.endMinMatch)
  4762. {
  4763. info->offset = offset;
  4764. info->length = length;
  4765. return true;
  4766. }
  4767. info->Reset();
  4768. return false;
  4769. }
  4770. inline bool Matcher::MatchOctoquad(const Char* const input, const CharCount inputLength, CharCount offset, OctoquadMatcher* matcher)
  4771. {
  4772. if (matcher->Match
  4773. ( input
  4774. , inputLength
  4775. , offset
  4776. #if ENABLE_REGEX_CONFIG_OPTIONS
  4777. , stats
  4778. #endif
  4779. ))
  4780. {
  4781. GroupInfo* const info = GroupIdToGroupInfo(0);
  4782. info->offset = offset;
  4783. info->length = TrigramInfo::PatternLength;
  4784. return true;
  4785. }
  4786. else
  4787. {
  4788. ResetGroup(0);
  4789. return false;
  4790. }
  4791. }
  4792. inline bool Matcher::MatchBOILiteral2(const Char* const input, const CharCount inputLength, CharCount offset, DWORD literal2)
  4793. {
  4794. if (offset == 0 && inputLength >= 2)
  4795. {
  4796. CompileAssert(sizeof(Char) == 2);
  4797. const Program * program = this->program;
  4798. if (program->rep.boiLiteral2.literal == *(DWORD *)input)
  4799. {
  4800. GroupInfo* const info = GroupIdToGroupInfo(0);
  4801. info->offset = 0;
  4802. info->length = 2;
  4803. return true;
  4804. }
  4805. }
  4806. ResetGroup(0);
  4807. return false;
  4808. }
  4809. bool Matcher::Match
  4810. ( const Char* const input
  4811. , const CharCount inputLength
  4812. , CharCount offset
  4813. , Js::ScriptContext * scriptContext
  4814. #if ENABLE_REGEX_CONFIG_OPTIONS
  4815. , RegexStats* stats
  4816. , DebugWriter* w
  4817. #endif
  4818. )
  4819. {
  4820. #if ENABLE_REGEX_CONFIG_OPTIONS
  4821. this->stats = stats;
  4822. this->w = w;
  4823. #endif
  4824. Assert(offset <= inputLength);
  4825. bool res;
  4826. bool loopMatchHere = true;
  4827. Program const *prog = this->program;
  4828. bool isStickyPresent = this->pattern->IsSticky();
  4829. switch (prog->tag)
  4830. {
  4831. case Program::ProgramTag::BOIInstructionsTag:
  4832. if (offset != 0)
  4833. {
  4834. groupInfos[0].Reset();
  4835. res = false;
  4836. break;
  4837. }
  4838. // fall through
  4839. case Program::ProgramTag::BOIInstructionsForStickyFlagTag:
  4840. AssertMsg(prog->tag == Program::ProgramTag::BOIInstructionsTag || isStickyPresent, "prog->tag should be BOIInstructionsForStickyFlagTag if sticky = true.");
  4841. loopMatchHere = false;
  4842. // fall through
  4843. case Program::ProgramTag::InstructionsTag:
  4844. {
  4845. previousQcTime = 0;
  4846. uint qcTicks = 0;
  4847. // This is the next offset in the input from where we will try to sync. For sync instructions that back up, this
  4848. // is used to avoid trying to sync when we have not yet reached the offset in the input we last synced to before
  4849. // backing up.
  4850. CharCount nextSyncInputOffset = offset;
  4851. RegexStacks * regexStacks = scriptContext->RegexStacks();
  4852. // Need to continue matching even if matchStart == inputLim since some patterns may match an empty string at the end
  4853. // of the input. For instance: /a*$/.exec("b")
  4854. bool firstIteration = true;
  4855. do
  4856. {
  4857. // Let there be only one call to MatchHere(), as that call expands the interpreter loop in-place. Having
  4858. // multiple calls to MatchHere() would bloat the code.
  4859. res = MatchHere(input, inputLength, offset, nextSyncInputOffset, regexStacks->contStack, regexStacks->assertionStack, qcTicks, firstIteration);
  4860. firstIteration = false;
  4861. } while(!res && loopMatchHere && ++offset <= inputLength);
  4862. break;
  4863. }
  4864. case Program::ProgramTag::SingleCharTag:
  4865. if (this->pattern->IsIgnoreCase())
  4866. {
  4867. res = MatchSingleCharCaseInsensitive(input, inputLength, offset, prog->rep.singleChar.c);
  4868. }
  4869. else
  4870. {
  4871. res = MatchSingleCharCaseSensitive(input, inputLength, offset, prog->rep.singleChar.c);
  4872. }
  4873. break;
  4874. case Program::ProgramTag::BoundedWordTag:
  4875. res = MatchBoundedWord(input, inputLength, offset);
  4876. break;
  4877. case Program::ProgramTag::LeadingTrailingSpacesTag:
  4878. res = MatchLeadingTrailingSpaces(input, inputLength, offset);
  4879. break;
  4880. case Program::ProgramTag::OctoquadTag:
  4881. res = MatchOctoquad(input, inputLength, offset, prog->rep.octoquad.matcher);
  4882. break;
  4883. case Program::ProgramTag::BOILiteral2Tag:
  4884. res = MatchBOILiteral2(input, inputLength, offset, prog->rep.boiLiteral2.literal);
  4885. break;
  4886. default:
  4887. Assert(false);
  4888. __assume(false);
  4889. }
  4890. #if ENABLE_REGEX_CONFIG_OPTIONS
  4891. this->stats = 0;
  4892. this->w = 0;
  4893. #endif
  4894. return res;
  4895. }
  4896. #if ENABLE_REGEX_CONFIG_OPTIONS
  4897. void Matcher::Print(DebugWriter* w, const Char* const input, const CharCount inputLength, CharCount inputOffset, const uint8* instPointer, ContStack &contStack, AssertionStack &assertionStack) const
  4898. {
  4899. w->PrintEOL(_u("Matcher {"));
  4900. w->Indent();
  4901. w->Print(_u("program: "));
  4902. w->PrintQuotedString(program->source, program->sourceLen);
  4903. w->EOL();
  4904. w->Print(_u("inputPointer: "));
  4905. if (inputLength == 0)
  4906. {
  4907. w->PrintEOL(_u("<empty input>"));
  4908. }
  4909. else if (inputLength > 1024)
  4910. {
  4911. w->PrintEOL(_u("<string too large>"));
  4912. }
  4913. else
  4914. {
  4915. w->PrintEscapedString(input, inputOffset);
  4916. if (inputOffset >= inputLength)
  4917. {
  4918. w->Print(_u("<<<>>>"));
  4919. }
  4920. else
  4921. {
  4922. w->Print(_u("<<<"));
  4923. w->PrintEscapedChar(input[inputOffset]);
  4924. w->Print(_u(">>>"));
  4925. w->PrintEscapedString(input + inputOffset + 1, inputLength - inputOffset - 1);
  4926. }
  4927. w->EOL();
  4928. }
  4929. if (program->tag == Program::ProgramTag::BOIInstructionsTag || program->tag == Program::ProgramTag::InstructionsTag)
  4930. {
  4931. w->Print(_u("instPointer: "));
  4932. const Inst* inst = (const Inst*)instPointer;
  4933. switch (inst->tag)
  4934. {
  4935. #define MBase(TagName, ClassName) \
  4936. case Inst::InstTag::TagName: \
  4937. { \
  4938. const ClassName *actualInst = static_cast<const ClassName *>(inst); \
  4939. actualInst->Print(w, InstPointerToLabel(instPointer), program->rep.insts.litbuf); \
  4940. break; \
  4941. }
  4942. #define M(TagName) MBase(TagName, TagName##Inst)
  4943. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, SpecializedClassName) MBase(TagName, SpecializedClassName)
  4944. #include "RegexOpCodes.h"
  4945. #undef MBase
  4946. #undef M
  4947. #undef MTemplate
  4948. default:
  4949. Assert(false);
  4950. __assume(false);
  4951. }
  4952. w->PrintEOL(_u("groups:"));
  4953. w->Indent();
  4954. for (int i = 0; i < program->numGroups; i++)
  4955. {
  4956. w->Print(_u("%d: "), i);
  4957. groupInfos[i].Print(w, input);
  4958. w->EOL();
  4959. }
  4960. w->Unindent();
  4961. w->PrintEOL(_u("loops:"));
  4962. w->Indent();
  4963. for (int i = 0; i < program->numLoops; i++)
  4964. {
  4965. w->Print(_u("%d: "), i);
  4966. loopInfos[i].Print(w);
  4967. w->EOL();
  4968. }
  4969. w->Unindent();
  4970. w->PrintEOL(_u("contStack: (top to bottom)"));
  4971. w->Indent();
  4972. contStack.Print(w, input);
  4973. w->Unindent();
  4974. w->PrintEOL(_u("assertionStack: (top to bottom)"));
  4975. w->Indent();
  4976. assertionStack.Print(w, this);
  4977. w->Unindent();
  4978. }
  4979. w->Unindent();
  4980. w->PrintEOL(_u("}"));
  4981. w->Flush();
  4982. }
  4983. #endif
  4984. // ----------------------------------------------------------------------
  4985. // Program
  4986. // ----------------------------------------------------------------------
  4987. Program::Program(RegexFlags flags)
  4988. : source(nullptr)
  4989. , sourceLen(0)
  4990. , flags(flags)
  4991. , numGroups(0)
  4992. , numLoops(0)
  4993. {
  4994. tag = ProgramTag::InstructionsTag;
  4995. rep.insts.insts = nullptr;
  4996. rep.insts.instsLen = 0;
  4997. rep.insts.litbuf = nullptr;
  4998. rep.insts.litbufLen = 0;
  4999. rep.insts.scannersForSyncToLiterals = nullptr;
  5000. }
  5001. Program *Program::New(Recycler *recycler, RegexFlags flags)
  5002. {
  5003. return RecyclerNew(recycler, Program, flags);
  5004. }
  5005. Field(ScannerInfo *)*Program::CreateScannerArrayForSyncToLiterals(Recycler *const recycler)
  5006. {
  5007. Assert(tag == ProgramTag::InstructionsTag);
  5008. Assert(!rep.insts.scannersForSyncToLiterals);
  5009. Assert(recycler);
  5010. return
  5011. rep.insts.scannersForSyncToLiterals =
  5012. RecyclerNewArrayZ(recycler, Field(ScannerInfo *), ScannersMixin::MaxNumSyncLiterals);
  5013. }
  5014. ScannerInfo *Program::AddScannerForSyncToLiterals(
  5015. Recycler *const recycler,
  5016. const int scannerIndex,
  5017. const CharCount offset,
  5018. const CharCount length,
  5019. const bool isEquivClass)
  5020. {
  5021. Assert(tag == ProgramTag::InstructionsTag);
  5022. Assert(rep.insts.scannersForSyncToLiterals);
  5023. Assert(recycler);
  5024. Assert(scannerIndex >= 0);
  5025. Assert(scannerIndex < ScannersMixin::MaxNumSyncLiterals);
  5026. Assert(!rep.insts.scannersForSyncToLiterals[scannerIndex]);
  5027. return
  5028. rep.insts.scannersForSyncToLiterals[scannerIndex] =
  5029. RecyclerNewLeaf(recycler, ScannerInfo, offset, length, isEquivClass);
  5030. }
  5031. void Program::FreeBody(ArenaAllocator* rtAllocator)
  5032. {
  5033. if (tag != ProgramTag::InstructionsTag || !rep.insts.insts)
  5034. {
  5035. return;
  5036. }
  5037. Inst *inst = reinterpret_cast<Inst *>(PointerValue(rep.insts.insts));
  5038. const auto instEnd = reinterpret_cast<Inst *>(reinterpret_cast<uint8 *>(inst) + rep.insts.instsLen);
  5039. Assert(inst < instEnd);
  5040. do
  5041. {
  5042. switch(inst->tag)
  5043. {
  5044. #define MBase(TagName, ClassName) \
  5045. case Inst::InstTag::TagName: \
  5046. { \
  5047. const auto actualInst = static_cast<ClassName *>(inst); \
  5048. actualInst->FreeBody(rtAllocator); \
  5049. inst = actualInst + 1; \
  5050. break; \
  5051. }
  5052. #define M(TagName) MBase(TagName, TagName##Inst)
  5053. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, SpecializedClassName) MBase(TagName, SpecializedClassName)
  5054. #include "RegexOpCodes.h"
  5055. #undef MBase
  5056. #undef M
  5057. #undef MTemplate
  5058. default:
  5059. Assert(false);
  5060. __assume(false);
  5061. }
  5062. } while(inst < instEnd);
  5063. Assert(inst == instEnd);
  5064. #if DBG
  5065. rep.insts.insts = nullptr;
  5066. rep.insts.instsLen = 0;
  5067. #endif
  5068. }
  5069. #if ENABLE_REGEX_CONFIG_OPTIONS
  5070. void Program::Print(DebugWriter* w)
  5071. {
  5072. const bool isBaselineMode = Js::Configuration::Global.flags.BaselineMode;
  5073. w->PrintEOL(_u("Program {"));
  5074. w->Indent();
  5075. w->PrintEOL(_u("source: %s"), PointerValue(source));
  5076. w->Print(_u("litbuf: "));
  5077. const char16 *litbuf = this->rep.insts.litbuf;
  5078. size_t litbufLen = 0;
  5079. if (litbuf == nullptr)
  5080. {
  5081. w->PrintEOL(_u("<NONE>"));
  5082. }
  5083. else
  5084. {
  5085. litbufLen = this->rep.insts.litbufLen;
  5086. for (size_t i = 0; i < litbufLen; ++i)
  5087. {
  5088. const char16 c = (char16)litbuf[i];
  5089. w->PrintEscapedChar(c);
  5090. }
  5091. w->PrintEOL(_u(""));
  5092. }
  5093. w->PrintEOL(_u("litbufLen: %u"), litbufLen);
  5094. w->Print(_u("flags: "));
  5095. if ((flags & GlobalRegexFlag) != 0) w->Print(_u("global "));
  5096. if ((flags & MultilineRegexFlag) != 0) w->Print(_u("multiline "));
  5097. if ((flags & IgnoreCaseRegexFlag) != 0) w->Print(_u("ignorecase "));
  5098. if ((flags & DotAllRegexFlag) != 0) w->Print(_u("dotAll "));
  5099. if ((flags & UnicodeRegexFlag) != 0) w->Print(_u("unicode "));
  5100. if ((flags & StickyRegexFlag) != 0) w->Print(_u("sticky "));
  5101. w->EOL();
  5102. w->PrintEOL(_u("numGroups: %d"), numGroups);
  5103. w->PrintEOL(_u("numLoops: %d"), numLoops);
  5104. switch (tag)
  5105. {
  5106. case ProgramTag::BOIInstructionsTag:
  5107. case ProgramTag::InstructionsTag:
  5108. {
  5109. w->PrintEOL(_u("instructions: {"));
  5110. w->Indent();
  5111. if (tag == ProgramTag::BOIInstructionsTag)
  5112. {
  5113. w->PrintEOL(_u(" BOITest(hardFail: true)"));
  5114. }
  5115. uint8* instsLim = rep.insts.insts + rep.insts.instsLen;
  5116. uint8* curr = rep.insts.insts;
  5117. int i = 0;
  5118. while (curr != instsLim)
  5119. {
  5120. const Inst *inst = (const Inst*)curr;
  5121. switch (inst->tag)
  5122. {
  5123. #define MBase(TagName, ClassName) \
  5124. case Inst::InstTag::TagName: \
  5125. { \
  5126. const ClassName *actualInst = static_cast<const ClassName *>(inst); \
  5127. curr += actualInst->Print(w, (Label)(isBaselineMode ? i++ : curr - rep.insts.insts), rep.insts.litbuf); \
  5128. break; \
  5129. }
  5130. #define M(TagName) MBase(TagName, TagName##Inst)
  5131. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, SpecializedClassName) MBase(TagName, SpecializedClassName)
  5132. #include "RegexOpCodes.h"
  5133. #undef MBase
  5134. #undef M
  5135. #undef MTemplate
  5136. default:
  5137. Assert(false);
  5138. __assume(false);
  5139. }
  5140. }
  5141. w->Unindent();
  5142. w->PrintEOL(_u("}"));
  5143. }
  5144. break;
  5145. case ProgramTag::SingleCharTag:
  5146. w->Print(_u("special form: <match single char "));
  5147. w->PrintQuotedChar(rep.singleChar.c);
  5148. w->PrintEOL(_u(">"));
  5149. break;
  5150. case ProgramTag::BoundedWordTag:
  5151. w->PrintEOL(_u("special form: <match bounded word>"));
  5152. break;
  5153. case ProgramTag::LeadingTrailingSpacesTag:
  5154. w->PrintEOL(_u("special form: <match leading/trailing spaces: minBegin=%d minEnd=%d>"),
  5155. rep.leadingTrailingSpaces.beginMinMatch, rep.leadingTrailingSpaces.endMinMatch);
  5156. break;
  5157. case ProgramTag::OctoquadTag:
  5158. w->Print(_u("special form: <octoquad "));
  5159. rep.octoquad.matcher->Print(w);
  5160. w->PrintEOL(_u(">"));
  5161. break;
  5162. }
  5163. w->Unindent();
  5164. w->PrintEOL(_u("}"));
  5165. }
  5166. #endif
  5167. // Template parameter here is the max number of cases
  5168. template void UnifiedRegex::SwitchMixin<2>::AddCase(char16, Label);
  5169. template void UnifiedRegex::SwitchMixin<4>::AddCase(char16, Label);
  5170. template void UnifiedRegex::SwitchMixin<8>::AddCase(char16, Label);
  5171. template void UnifiedRegex::SwitchMixin<16>::AddCase(char16, Label);
  5172. template void UnifiedRegex::SwitchMixin<24>::AddCase(char16, Label);
  5173. #define M(...)
  5174. #define MTemplate(TagName, TemplateDeclaration, GenericClassName, SpecializedClassName) template struct SpecializedClassName;
  5175. #include "RegexOpCodes.h"
  5176. #undef M
  5177. #undef MTemplate
  5178. }