RegexRuntime.cpp 201 KB

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