verilog-mode.el 549 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511
  1. ;;; verilog-mode.el --- major mode for editing verilog source in Emacs
  2. ;; Copyright (C) 1996-2017 Free Software Foundation, Inc.
  3. ;; Author: Michael McNamara <mac@verilog.com>
  4. ;; Wilson Snyder <wsnyder@wsnyder.org>
  5. ;; http://www.verilog.com
  6. ;; http://www.veripool.org
  7. ;; Created: 3 Jan 1996
  8. ;; Keywords: languages
  9. ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
  10. ;; file on 19/3/2008, and the maintainer agreed that when a bug is
  11. ;; filed in the Emacs bug reporting system against this file, a copy
  12. ;; of the bug report be sent to the maintainer's email address.
  13. ;; This code supports Emacs 21.1 and later
  14. ;; And XEmacs 21.1 and later
  15. ;; Please do not make changes that break Emacs 21. Thanks!
  16. ;;
  17. ;;
  18. ;; This file is part of GNU Emacs.
  19. ;; GNU Emacs is free software: you can redistribute it and/or modify
  20. ;; it under the terms of the GNU General Public License as published by
  21. ;; the Free Software Foundation, either version 3 of the License, or
  22. ;; (at your option) any later version.
  23. ;; GNU Emacs is distributed in the hope that it will be useful,
  24. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. ;; GNU General Public License for more details.
  27. ;; You should have received a copy of the GNU General Public License
  28. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  29. ;;; Commentary:
  30. ;; USAGE
  31. ;; =====
  32. ;; A major mode for editing Verilog and SystemVerilog HDL source code (IEEE
  33. ;; 1364-2005 and IEEE 1800-2012 standards). When you have entered Verilog
  34. ;; mode, you may get more info by pressing C-h m. You may also get online
  35. ;; help describing various functions by: C-h f <Name of function you want
  36. ;; described>
  37. ;; KNOWN BUGS / BUG REPORTS
  38. ;; =======================
  39. ;; SystemVerilog is a rapidly evolving language, and hence this mode is
  40. ;; under continuous development. Please report any issues to the issue
  41. ;; tracker at
  42. ;;
  43. ;; http://www.veripool.org/verilog-mode
  44. ;;
  45. ;; Please use verilog-submit-bug-report to submit a report; type C-c
  46. ;; C-b to invoke this and as a result we will have a much easier time
  47. ;; of reproducing the bug you find, and hence fixing it.
  48. ;; INSTALLING THE MODE
  49. ;; ===================
  50. ;; An older version of this mode may be already installed as a part of
  51. ;; your environment, and one method of updating would be to update
  52. ;; your Emacs environment. Sometimes this is difficult for local
  53. ;; political/control reasons, and hence you can always install a
  54. ;; private copy (or even a shared copy) which overrides the system
  55. ;; default.
  56. ;; You can get step by step help in installing this file by going to
  57. ;; <http://www.verilog.com/emacs_install.html>
  58. ;; The short list of installation instructions are: To set up
  59. ;; automatic Verilog mode, put this file in your load path, and put
  60. ;; the following in code (please un comment it first!) in your
  61. ;; .emacs, or in your site's site-load.el
  62. ;; (autoload 'verilog-mode "verilog-mode" "Verilog mode" t )
  63. ;; (add-to-list 'auto-mode-alist '("\\.[ds]?vh?\\'" . verilog-mode))
  64. ;; Be sure to examine at the help for verilog-auto, and the other
  65. ;; verilog-auto-* functions for some major coding time savers.
  66. ;;
  67. ;; If you want to customize Verilog mode to fit your needs better,
  68. ;; you may add the below lines (the values of the variables presented
  69. ;; here are the defaults). Note also that if you use an Emacs that
  70. ;; supports custom, it's probably better to use the custom menu to
  71. ;; edit these. If working as a member of a large team these settings
  72. ;; should be common across all users (in a site-start file), or set
  73. ;; in Local Variables in every file. Otherwise, different people's
  74. ;; AUTO expansion may result different whitespace changes.
  75. ;;
  76. ;; ;; Enable syntax highlighting of **all** languages
  77. ;; (global-font-lock-mode t)
  78. ;;
  79. ;; ;; User customization for Verilog mode
  80. ;; (setq verilog-indent-level 3
  81. ;; verilog-indent-level-module 3
  82. ;; verilog-indent-level-declaration 3
  83. ;; verilog-indent-level-behavioral 3
  84. ;; verilog-indent-level-directive 1
  85. ;; verilog-case-indent 2
  86. ;; verilog-auto-newline t
  87. ;; verilog-auto-indent-on-newline t
  88. ;; verilog-tab-always-indent t
  89. ;; verilog-auto-endcomments t
  90. ;; verilog-minimum-comment-distance 40
  91. ;; verilog-indent-begin-after-if t
  92. ;; verilog-auto-lineup 'declarations
  93. ;; verilog-highlight-p1800-keywords nil
  94. ;; verilog-linter "my_lint_shell_command"
  95. ;; )
  96. ;;; History:
  97. ;;
  98. ;; See commit history at http://www.veripool.org/verilog-mode.html
  99. ;; (This section is required to appease checkdoc.)
  100. ;;; Code:
  101. ;;
  102. ;; This variable will always hold the version number of the mode
  103. (defconst verilog-mode-version "2016-11-14-26d3540-vpo-GNU"
  104. "Version of this Verilog mode.")
  105. (defconst verilog-mode-release-emacs t
  106. "If non-nil, this version of Verilog mode was released with Emacs itself.")
  107. (defun verilog-version ()
  108. "Inform caller of the version of this file."
  109. (interactive)
  110. (message "Using verilog-mode version %s" verilog-mode-version))
  111. ;; Insure we have certain packages, and deal with it if we don't
  112. ;; Be sure to note which Emacs flavor and version added each feature.
  113. (eval-when-compile
  114. ;; Provide stuff if we are XEmacs
  115. (when (featurep 'xemacs)
  116. (condition-case nil
  117. (require 'easymenu)
  118. (error nil))
  119. (condition-case nil
  120. (require 'regexp-opt)
  121. (error nil))
  122. ;; Bug in 19.28 through 19.30 skeleton.el, not provided.
  123. (condition-case nil
  124. (load "skeleton")
  125. (error nil))
  126. (condition-case nil
  127. (if (fboundp 'when)
  128. nil ; fab
  129. (defmacro when (cond &rest body)
  130. (list 'if cond (cons 'progn body))))
  131. (error nil))
  132. (condition-case nil
  133. (if (fboundp 'unless)
  134. nil ; fab
  135. (defmacro unless (cond &rest body)
  136. (cons 'if (cons cond (cons nil body)))))
  137. (error nil))
  138. (condition-case nil
  139. (if (fboundp 'store-match-data)
  140. nil ; fab
  141. (defmacro store-match-data (&rest _args) nil))
  142. (error nil))
  143. (condition-case nil
  144. (if (fboundp 'char-before)
  145. nil ; great
  146. (defmacro char-before (&rest _body)
  147. (char-after (1- (point)))))
  148. (error nil))
  149. (condition-case nil
  150. (if (fboundp 'when)
  151. nil ; fab
  152. (defsubst point-at-bol (&optional N)
  153. (save-excursion (beginning-of-line N) (point))))
  154. (error nil))
  155. (condition-case nil
  156. (if (fboundp 'when)
  157. nil ; fab
  158. (defsubst point-at-eol (&optional N)
  159. (save-excursion (end-of-line N) (point))))
  160. (error nil))
  161. (condition-case nil
  162. (require 'custom)
  163. (error nil))
  164. (condition-case nil
  165. (if (fboundp 'match-string-no-properties)
  166. nil ; great
  167. (defsubst match-string-no-properties (num &optional string)
  168. "Return string of text matched by last search, without text properties.
  169. NUM specifies which parenthesized expression in the last regexp.
  170. Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
  171. Zero means the entire text matched by the whole regexp or whole string.
  172. STRING should be given if the last search was by `string-match' on STRING."
  173. (if (match-beginning num)
  174. (if string
  175. (let ((result
  176. (substring string
  177. (match-beginning num) (match-end num))))
  178. (set-text-properties 0 (length result) nil result)
  179. result)
  180. (buffer-substring-no-properties (match-beginning num)
  181. (match-end num)
  182. (current-buffer)))))
  183. )
  184. (error nil))
  185. (if (and (featurep 'custom) (fboundp 'custom-declare-variable))
  186. nil ; We've got what we needed
  187. ;; We have the old custom-library, hack around it!
  188. (defmacro defgroup (&rest _args) nil)
  189. (defmacro customize (&rest _args)
  190. (message
  191. "Sorry, Customize is not available with this version of Emacs"))
  192. (defmacro defcustom (var value doc &rest _args)
  193. `(defvar ,var ,value ,doc))
  194. )
  195. (if (fboundp 'defface)
  196. nil ; great!
  197. (defmacro defface (var values doc &rest _args)
  198. `(make-face ,var))
  199. )
  200. (if (and (featurep 'custom) (fboundp 'customize-group))
  201. nil ; We've got what we needed
  202. ;; We have an intermediate custom-library, hack around it!
  203. (defmacro customize-group (var &rest _args)
  204. `(customize ,var))
  205. )
  206. (defvar inhibit-modification-hooks)
  207. (defvar inhibit-point-motion-hooks)
  208. (defvar deactivate-mark)
  209. )
  210. ;;
  211. ;; OK, do this stuff if we are NOT XEmacs:
  212. (unless (featurep 'xemacs)
  213. (unless (fboundp 'region-active-p)
  214. (defmacro region-active-p ()
  215. `(and transient-mark-mode mark-active))))
  216. )
  217. ;; Provide a regular expression optimization routine, using regexp-opt
  218. ;; if provided by the user's elisp libraries
  219. (eval-and-compile
  220. ;; The below were disabled when GNU Emacs 22 was released;
  221. ;; perhaps some still need to be there to support Emacs 21.
  222. (if (featurep 'xemacs)
  223. (if (fboundp 'regexp-opt)
  224. ;; regexp-opt is defined, does it take 3 or 2 arguments?
  225. (if (fboundp 'function-max-args)
  226. (let ((args (function-max-args `regexp-opt)))
  227. (cond
  228. ((eq args 3) ; It takes 3
  229. (condition-case nil ; Hide this defun from emacses
  230. ; with just a two input regexp
  231. (defun verilog-regexp-opt (a b)
  232. "Deal with differing number of required arguments for `regexp-opt'.
  233. Call `regexp-opt' on A and B."
  234. (regexp-opt a b t))
  235. (error nil))
  236. )
  237. ((eq args 2) ; It takes 2
  238. (defun verilog-regexp-opt (a b)
  239. "Call `regexp-opt' on A and B."
  240. (regexp-opt a b))
  241. )
  242. (t nil)))
  243. ;; We can't tell; assume it takes 2
  244. (defun verilog-regexp-opt (a b)
  245. "Call `regexp-opt' on A and B."
  246. (regexp-opt a b))
  247. )
  248. ;; There is no regexp-opt, provide our own
  249. (defun verilog-regexp-opt (strings &optional paren _shy)
  250. (let ((open (if paren "\\(" "")) (close (if paren "\\)" "")))
  251. (concat open (mapconcat 'regexp-quote strings "\\|") close)))
  252. )
  253. ;; Emacs.
  254. (defalias 'verilog-regexp-opt 'regexp-opt)))
  255. ;; emacs >=22 has looking-back, but older emacs and xemacs don't.
  256. ;; This function is lifted directly from emacs's subr.el
  257. ;; so that it can be used by xemacs.
  258. ;; The idea for this was borrowed from org-mode via this link:
  259. ;; https://lists.gnu.org/archive/html/emacs-orgmode/2009-12/msg00032.html
  260. (eval-and-compile
  261. (cond
  262. ((fboundp 'looking-back)
  263. (defalias 'verilog-looking-back 'looking-back))
  264. (t
  265. (defun verilog-looking-back (regexp limit &optional greedy)
  266. "Return non-nil if text before point matches regular expression REGEXP.
  267. Like `looking-at' except matches before point, and is slower.
  268. LIMIT if non-nil speeds up the search by specifying a minimum
  269. starting position, to avoid checking matches that would start
  270. before LIMIT.
  271. If GREEDY is non-nil, extend the match backwards as far as
  272. possible, stopping when a single additional previous character
  273. cannot be part of a match for REGEXP. When the match is
  274. extended, its starting position is allowed to occur before
  275. LIMIT.
  276. As a general recommendation, try to avoid using `looking-back'
  277. wherever possible, since it is slow."
  278. (let ((start (point))
  279. (pos
  280. (save-excursion
  281. (and (re-search-backward (concat "\\(?:" regexp "\\)\\=") limit t)
  282. (point)))))
  283. (if (and greedy pos)
  284. (save-restriction
  285. (narrow-to-region (point-min) start)
  286. (while (and (> pos (point-min))
  287. (save-excursion
  288. (goto-char pos)
  289. (backward-char 1)
  290. (looking-at (concat "\\(?:" regexp "\\)\\'"))))
  291. (setq pos (1- pos)))
  292. (save-excursion
  293. (goto-char pos)
  294. (looking-at (concat "\\(?:" regexp "\\)\\'")))))
  295. (not (null pos)))))))
  296. (eval-and-compile
  297. (cond
  298. ((fboundp 'restore-buffer-modified-p)
  299. ;; Faster, as does not update mode line when nothing changes
  300. (defalias 'verilog-restore-buffer-modified-p 'restore-buffer-modified-p))
  301. (t
  302. (defalias 'verilog-restore-buffer-modified-p 'set-buffer-modified-p))))
  303. (eval-and-compile
  304. ;; Both xemacs and emacs
  305. (condition-case nil
  306. (require 'diff) ; diff-command and diff-switches
  307. (error nil))
  308. (condition-case nil
  309. (require 'compile) ; compilation-error-regexp-alist-alist
  310. (error nil))
  311. (condition-case nil
  312. (unless (fboundp 'buffer-chars-modified-tick) ; Emacs 22 added
  313. (defmacro buffer-chars-modified-tick () (buffer-modified-tick)))
  314. (error nil))
  315. ;; Added in Emacs 24.1
  316. (condition-case nil
  317. (unless (fboundp 'prog-mode)
  318. (define-derived-mode prog-mode fundamental-mode "Prog"))
  319. (error nil))
  320. ;; Added in Emacs 25.1
  321. (condition-case nil
  322. (unless (fboundp 'forward-word-strictly)
  323. (defalias 'forward-word-strictly 'forward-word))
  324. (error nil)))
  325. (eval-when-compile
  326. (defun verilog-regexp-words (a)
  327. "Call `regexp-opt' with word delimiters for the words A."
  328. (concat "\\<" (verilog-regexp-opt a t) "\\>")))
  329. (defun verilog-regexp-words (a)
  330. "Call `regexp-opt' with word delimiters for the words A."
  331. ;; The FAQ references this function, so user LISP sometimes calls it
  332. (concat "\\<" (verilog-regexp-opt a t) "\\>"))
  333. (defun verilog-easy-menu-filter (menu)
  334. "Filter `easy-menu-define' MENU to support new features."
  335. (cond ((not (featurep 'xemacs))
  336. menu) ; GNU Emacs - passthru
  337. ;; XEmacs doesn't support :help. Strip it.
  338. ;; Recursively filter the a submenu
  339. ((listp menu)
  340. (mapcar 'verilog-easy-menu-filter menu))
  341. ;; Look for [:help "blah"] and remove
  342. ((vectorp menu)
  343. (let ((i 0) (out []))
  344. (while (< i (length menu))
  345. (if (equal `:help (aref menu i))
  346. (setq i (+ 2 i))
  347. (setq out (vconcat out (vector (aref menu i)))
  348. i (1+ i))))
  349. out))
  350. (t menu))) ; Default - ok
  351. ;;(verilog-easy-menu-filter
  352. ;; `("Verilog" ("MA" ["SAA" nil :help "Help SAA"] ["SAB" nil :help "Help SAA"])
  353. ;; "----" ["MB" nil :help "Help MB"]))
  354. (defun verilog-define-abbrev (table name expansion &optional hook)
  355. "Filter `define-abbrev' TABLE NAME EXPANSION and call HOOK.
  356. Provides SYSTEM-FLAG in newer Emacs."
  357. (condition-case nil
  358. (define-abbrev table name expansion hook 0 t)
  359. (error
  360. (define-abbrev table name expansion hook))))
  361. (defun verilog-customize ()
  362. "Customize variables and other settings used by Verilog-Mode."
  363. (interactive)
  364. (customize-group 'verilog-mode))
  365. (defun verilog-font-customize ()
  366. "Customize fonts used by Verilog-Mode."
  367. (interactive)
  368. (if (fboundp 'customize-apropos)
  369. (customize-apropos "font-lock-*" 'faces)))
  370. (defun verilog-booleanp (value)
  371. "Return t if VALUE is boolean.
  372. This implements GNU Emacs 22.1's `booleanp' function in earlier Emacs.
  373. This function may be removed when Emacs 21 is no longer supported."
  374. (or (equal value t) (equal value nil)))
  375. (defun verilog-insert-last-command-event ()
  376. "Insert the `last-command-event'."
  377. (insert (if (featurep 'xemacs)
  378. ;; XEmacs 21.5 doesn't like last-command-event
  379. last-command-char
  380. ;; And GNU Emacs 22 has obsoleted last-command-char
  381. last-command-event)))
  382. (defvar verilog-no-change-functions nil
  383. "True if `after-change-functions' is disabled.
  384. Use of `syntax-ppss' may break, as ppss's cache may get corrupted.")
  385. (defvar verilog-in-hooks nil
  386. "True when within a `verilog-run-hooks' block.")
  387. (defmacro verilog-run-hooks (&rest hooks)
  388. "Run each hook in HOOKS using `run-hooks'.
  389. Set `verilog-in-hooks' during this time, to assist AUTO caches."
  390. `(let ((verilog-in-hooks t))
  391. (run-hooks ,@hooks)))
  392. (defun verilog-syntax-ppss (&optional pos)
  393. (when verilog-no-change-functions
  394. (if verilog-in-hooks
  395. (verilog-scan-cache-flush)
  396. ;; else don't let the AUTO code itself get away with flushing the cache,
  397. ;; as that'll make things very slow
  398. (backtrace)
  399. (error "%s: Internal problem; use of syntax-ppss when cache may be corrupt"
  400. (verilog-point-text))))
  401. (if (fboundp 'syntax-ppss)
  402. (syntax-ppss pos)
  403. (parse-partial-sexp (point-min) (or pos (point)))))
  404. (defgroup verilog-mode nil
  405. "Major mode for Verilog source code."
  406. :version "22.2"
  407. :group 'languages)
  408. ;; (defgroup verilog-mode-fonts nil
  409. ;; "Facilitates easy customization fonts used in Verilog source text"
  410. ;; :link '(customize-apropos "font-lock-*" 'faces)
  411. ;; :group 'verilog-mode)
  412. (defgroup verilog-mode-indent nil
  413. "Customize indentation and highlighting of Verilog source text."
  414. :group 'verilog-mode)
  415. (defgroup verilog-mode-actions nil
  416. "Customize actions on Verilog source text."
  417. :group 'verilog-mode)
  418. (defgroup verilog-mode-auto nil
  419. "Customize AUTO actions when expanding Verilog source text."
  420. :group 'verilog-mode)
  421. (defvar verilog-debug nil
  422. "Non-nil means enable debug messages for `verilog-mode' internals.")
  423. (defvar verilog-warn-fatal nil
  424. "Non-nil means `verilog-warn-error' warnings are fatal `error's.")
  425. (defcustom verilog-linter
  426. "echo 'No verilog-linter set, see \"M-x describe-variable verilog-linter\"'"
  427. "Unix program and arguments to call to run a lint checker on Verilog source.
  428. Depending on the `verilog-set-compile-command', this may be invoked when
  429. you type \\[compile]. When the compile completes, \\[next-error] will take
  430. you to the next lint error."
  431. :type 'string
  432. :group 'verilog-mode-actions)
  433. ;; We don't mark it safe, as it's used as a shell command
  434. (defcustom verilog-coverage
  435. "echo 'No verilog-coverage set, see \"M-x describe-variable verilog-coverage\"'"
  436. "Program and arguments to use to annotate for coverage Verilog source.
  437. Depending on the `verilog-set-compile-command', this may be invoked when
  438. you type \\[compile]. When the compile completes, \\[next-error] will take
  439. you to the next lint error."
  440. :type 'string
  441. :group 'verilog-mode-actions)
  442. ;; We don't mark it safe, as it's used as a shell command
  443. (defcustom verilog-simulator
  444. "echo 'No verilog-simulator set, see \"M-x describe-variable verilog-simulator\"'"
  445. "Program and arguments to use to interpret Verilog source.
  446. Depending on the `verilog-set-compile-command', this may be invoked when
  447. you type \\[compile]. When the compile completes, \\[next-error] will take
  448. you to the next lint error."
  449. :type 'string
  450. :group 'verilog-mode-actions)
  451. ;; We don't mark it safe, as it's used as a shell command
  452. (defcustom verilog-compiler
  453. "echo 'No verilog-compiler set, see \"M-x describe-variable verilog-compiler\"'"
  454. "Program and arguments to use to compile Verilog source.
  455. Depending on the `verilog-set-compile-command', this may be invoked when
  456. you type \\[compile]. When the compile completes, \\[next-error] will take
  457. you to the next lint error."
  458. :type 'string
  459. :group 'verilog-mode-actions)
  460. ;; We don't mark it safe, as it's used as a shell command
  461. (defcustom verilog-preprocessor
  462. ;; Very few tools give preprocessed output, so we'll default to Verilog-Perl
  463. "vppreproc __FLAGS__ __FILE__"
  464. "Program and arguments to use to preprocess Verilog source.
  465. This is invoked with `verilog-preprocess', and depending on the
  466. `verilog-set-compile-command', may also be invoked when you type
  467. \\[compile]. When the compile completes, \\[next-error] will
  468. take you to the next lint error."
  469. :type 'string
  470. :group 'verilog-mode-actions)
  471. ;; We don't mark it safe, as it's used as a shell command
  472. (defvar verilog-preprocess-history nil
  473. "History for `verilog-preprocess'.")
  474. (defvar verilog-tool 'verilog-linter
  475. "Which tool to use for building compiler-command.
  476. Either nil, `verilog-linter', `verilog-compiler',
  477. `verilog-coverage', `verilog-preprocessor', or `verilog-simulator'.
  478. Alternatively use the \"Choose Compilation Action\" menu. See
  479. `verilog-set-compile-command' for more information.")
  480. (defcustom verilog-highlight-translate-off nil
  481. "Non-nil means background-highlight code excluded from translation.
  482. That is, all code between \"// synopsys translate_off\" and
  483. \"// synopsys translate_on\" is highlighted using a different background color
  484. \(face `verilog-font-lock-translate-off-face').
  485. Note: This will slow down on-the-fly fontification (and thus editing).
  486. Note: Activate the new setting in a Verilog buffer by re-fontifying it (menu
  487. entry \"Fontify Buffer\"). XEmacs: turn off and on font locking."
  488. :type 'boolean
  489. :group 'verilog-mode-indent)
  490. ;; Note we don't use :safe, as that would break on Emacsen before 22.0.
  491. (put 'verilog-highlight-translate-off 'safe-local-variable 'verilog-booleanp)
  492. (defcustom verilog-auto-lineup 'declarations
  493. "Type of statements to lineup across multiple lines.
  494. If `all' is selected, then all line ups described below are done.
  495. If `declarations', then just declarations are lined up with any
  496. preceding declarations, taking into account widths and the like,
  497. so or example the code:
  498. reg [31:0] a;
  499. reg b;
  500. would become
  501. reg [31:0] a;
  502. reg b;
  503. If `assignment', then assignments are lined up with any preceding
  504. assignments, so for example the code
  505. a_long_variable <= b + c;
  506. d = e + f;
  507. would become
  508. a_long_variable <= b + c;
  509. d = e + f;
  510. In order to speed up editing, large blocks of statements are lined up
  511. only when a \\[verilog-pretty-expr] is typed; and large blocks of declarations
  512. are lineup only when \\[verilog-pretty-declarations] is typed."
  513. :type '(radio (const :tag "Line up Assignments and Declarations" all)
  514. (const :tag "Line up Assignment statements" assignments )
  515. (const :tag "Line up Declarations" declarations)
  516. (function :tag "Other"))
  517. :group 'verilog-mode-indent )
  518. (put 'verilog-auto-lineup 'safe-local-variable
  519. '(lambda (x) (memq x '(nil all assignments declarations))))
  520. (defcustom verilog-indent-level 3
  521. "Indentation of Verilog statements with respect to containing block."
  522. :group 'verilog-mode-indent
  523. :type 'integer)
  524. (put 'verilog-indent-level 'safe-local-variable 'integerp)
  525. (defcustom verilog-indent-level-module 3
  526. "Indentation of Module level Verilog statements (eg always, initial).
  527. Set to 0 to get initial and always statements lined up on the left side of
  528. your screen."
  529. :group 'verilog-mode-indent
  530. :type 'integer)
  531. (put 'verilog-indent-level-module 'safe-local-variable 'integerp)
  532. (defcustom verilog-indent-level-declaration 3
  533. "Indentation of declarations with respect to containing block.
  534. Set to 0 to get them list right under containing block."
  535. :group 'verilog-mode-indent
  536. :type 'integer)
  537. (put 'verilog-indent-level-declaration 'safe-local-variable 'integerp)
  538. (defcustom verilog-indent-declaration-macros nil
  539. "How to treat macro expansions in a declaration.
  540. If nil, indent as:
  541. input [31:0] a;
  542. input \\=`CP;
  543. output c;
  544. If non nil, treat as:
  545. input [31:0] a;
  546. input \\=`CP ;
  547. output c;"
  548. :group 'verilog-mode-indent
  549. :type 'boolean)
  550. (put 'verilog-indent-declaration-macros 'safe-local-variable 'verilog-booleanp)
  551. (defcustom verilog-indent-lists t
  552. "How to treat indenting items in a list.
  553. If t (the default), indent as:
  554. always @( posedge a or
  555. reset ) begin
  556. If nil, treat as:
  557. always @( posedge a or
  558. reset ) begin"
  559. :group 'verilog-mode-indent
  560. :type 'boolean)
  561. (put 'verilog-indent-lists 'safe-local-variable 'verilog-booleanp)
  562. (defcustom verilog-indent-level-behavioral 3
  563. "Absolute indentation of first begin in a task or function block.
  564. Set to 0 to get such code to start at the left side of the screen."
  565. :group 'verilog-mode-indent
  566. :type 'integer)
  567. (put 'verilog-indent-level-behavioral 'safe-local-variable 'integerp)
  568. (defcustom verilog-indent-level-directive 1
  569. "Indentation to add to each level of \\=`ifdef declarations.
  570. Set to 0 to have all directives start at the left side of the screen."
  571. :group 'verilog-mode-indent
  572. :type 'integer)
  573. (put 'verilog-indent-level-directive 'safe-local-variable 'integerp)
  574. (defcustom verilog-cexp-indent 2
  575. "Indentation of Verilog statements split across lines."
  576. :group 'verilog-mode-indent
  577. :type 'integer)
  578. (put 'verilog-cexp-indent 'safe-local-variable 'integerp)
  579. (defcustom verilog-case-indent 2
  580. "Indentation for case statements."
  581. :group 'verilog-mode-indent
  582. :type 'integer)
  583. (put 'verilog-case-indent 'safe-local-variable 'integerp)
  584. (defcustom verilog-auto-newline t
  585. "Non-nil means automatically newline after semicolons."
  586. :group 'verilog-mode-indent
  587. :type 'boolean)
  588. (put 'verilog-auto-newline 'safe-local-variable 'verilog-booleanp)
  589. (defcustom verilog-auto-indent-on-newline t
  590. "Non-nil means automatically indent line after newline."
  591. :group 'verilog-mode-indent
  592. :type 'boolean)
  593. (put 'verilog-auto-indent-on-newline 'safe-local-variable 'verilog-booleanp)
  594. (defcustom verilog-tab-always-indent t
  595. "Non-nil means TAB should always re-indent the current line.
  596. A nil value means TAB will only reindent when at the beginning of the line."
  597. :group 'verilog-mode-indent
  598. :type 'boolean)
  599. (put 'verilog-tab-always-indent 'safe-local-variable 'verilog-booleanp)
  600. (defcustom verilog-tab-to-comment nil
  601. "Non-nil means TAB moves to the right hand column in preparation for a comment."
  602. :group 'verilog-mode-actions
  603. :type 'boolean)
  604. (put 'verilog-tab-to-comment 'safe-local-variable 'verilog-booleanp)
  605. (defcustom verilog-indent-begin-after-if t
  606. "Non-nil means indent begin statements following if, else, while, etc.
  607. Otherwise, line them up."
  608. :group 'verilog-mode-indent
  609. :type 'boolean)
  610. (put 'verilog-indent-begin-after-if 'safe-local-variable 'verilog-booleanp)
  611. (defcustom verilog-align-ifelse nil
  612. "Non-nil means align `else' under matching `if'.
  613. Otherwise else is lined up with first character on line holding matching if."
  614. :group 'verilog-mode-indent
  615. :type 'boolean)
  616. (put 'verilog-align-ifelse 'safe-local-variable 'verilog-booleanp)
  617. (defcustom verilog-minimum-comment-distance 10
  618. "Minimum distance (in lines) between begin and end required before a comment.
  619. Setting this variable to zero results in every end acquiring a comment; the
  620. default avoids too many redundant comments in tight quarters."
  621. :group 'verilog-mode-indent
  622. :type 'integer)
  623. (put 'verilog-minimum-comment-distance 'safe-local-variable 'integerp)
  624. (defcustom verilog-highlight-p1800-keywords nil
  625. "Non-nil means highlight words newly reserved by IEEE-1800.
  626. These will appear in `verilog-font-lock-p1800-face' in order to gently
  627. suggest changing where these words are used as variables to something else.
  628. A nil value means highlight these words as appropriate for the SystemVerilog
  629. IEEE-1800 standard. Note that changing this will require restarting Emacs
  630. to see the effect as font color choices are cached by Emacs."
  631. :group 'verilog-mode-indent
  632. :type 'boolean)
  633. (put 'verilog-highlight-p1800-keywords 'safe-local-variable 'verilog-booleanp)
  634. (defcustom verilog-highlight-grouping-keywords nil
  635. "Non-nil means highlight grouping keywords more dramatically.
  636. If false, these words are in the `font-lock-type-face'; if True
  637. then they are in `verilog-font-lock-grouping-keywords-face'.
  638. Some find that special highlighting on these grouping constructs
  639. allow the structure of the code to be understood at a glance."
  640. :group 'verilog-mode-indent
  641. :type 'boolean)
  642. (put 'verilog-highlight-grouping-keywords 'safe-local-variable 'verilog-booleanp)
  643. (defcustom verilog-highlight-modules nil
  644. "Non-nil means highlight module statements for `verilog-load-file-at-point'.
  645. When true, mousing over module names will allow jumping to the
  646. module definition. If false, this is not supported. Setting
  647. this is experimental, and may lead to bad performance."
  648. :group 'verilog-mode-indent
  649. :type 'boolean)
  650. (put 'verilog-highlight-modules 'safe-local-variable 'verilog-booleanp)
  651. (defcustom verilog-highlight-includes t
  652. "Non-nil means highlight module statements for `verilog-load-file-at-point'.
  653. When true, mousing over include file names will allow jumping to the
  654. file referenced. If false, this is not supported."
  655. :group 'verilog-mode-indent
  656. :type 'boolean)
  657. (put 'verilog-highlight-includes 'safe-local-variable 'verilog-booleanp)
  658. (defcustom verilog-auto-declare-nettype nil
  659. "Non-nil specifies the data type to use with `verilog-auto-input' etc.
  660. Set this to \"wire\" if the Verilog code uses \"\\=`default_nettype
  661. none\". Note using \\=`default_nettype none isn't recommended practice; this
  662. mode is experimental."
  663. :version "24.1" ; rev670
  664. :group 'verilog-mode-actions
  665. :type 'boolean)
  666. (put 'verilog-auto-declare-nettype 'safe-local-variable `stringp)
  667. (defcustom verilog-auto-wire-comment t
  668. "Non-nil indicates to insert to/from comments with `verilog-auto-wire' etc."
  669. :version "25.1"
  670. :group 'verilog-mode-actions
  671. :type 'boolean)
  672. (put 'verilog-auto-wire-comment 'safe-local-variable `verilog-booleanp)
  673. (defcustom verilog-auto-wire-type nil
  674. "Non-nil specifies the data type to use with `verilog-auto-wire' etc.
  675. Set this to \"logic\" for SystemVerilog code, or use `verilog-auto-logic'."
  676. :version "24.1" ; rev673
  677. :group 'verilog-mode-actions
  678. :type 'boolean)
  679. (put 'verilog-auto-wire-type 'safe-local-variable `stringp)
  680. (defcustom verilog-auto-endcomments t
  681. "Non-nil means insert a comment /* ... */ after `end's.
  682. The name of the function or case will be set between the braces."
  683. :group 'verilog-mode-actions
  684. :type 'boolean)
  685. (put 'verilog-auto-endcomments 'safe-local-variable 'verilog-booleanp)
  686. (defcustom verilog-auto-delete-trailing-whitespace nil
  687. "Non-nil means to `delete-trailing-whitespace' in `verilog-auto'."
  688. :version "24.1" ; rev703
  689. :group 'verilog-mode-actions
  690. :type 'boolean)
  691. (put 'verilog-auto-delete-trailing-whitespace 'safe-local-variable 'verilog-booleanp)
  692. (defcustom verilog-auto-ignore-concat nil
  693. "Non-nil means ignore signals in {...} concatenations for AUTOWIRE etc.
  694. This will exclude signals referenced as pin connections in {...}
  695. from AUTOWIRE, AUTOOUTPUT and friends. This flag should be set
  696. for backward compatibility only and not set in new designs; it
  697. may be removed in future versions."
  698. :group 'verilog-mode-actions
  699. :type 'boolean)
  700. (put 'verilog-auto-ignore-concat 'safe-local-variable 'verilog-booleanp)
  701. (defcustom verilog-auto-read-includes nil
  702. "Non-nil means to automatically read includes before AUTOs.
  703. This will do a `verilog-read-defines' and `verilog-read-includes' before
  704. each AUTO expansion. This makes it easier to embed defines and includes,
  705. but can result in very slow reading times if there are many or large
  706. include files."
  707. :group 'verilog-mode-actions
  708. :type 'boolean)
  709. (put 'verilog-auto-read-includes 'safe-local-variable 'verilog-booleanp)
  710. (defcustom verilog-auto-save-policy nil
  711. "Non-nil indicates action to take when saving a Verilog buffer with AUTOs.
  712. A value of `force' will always do a \\[verilog-auto] automatically if
  713. needed on every save. A value of `detect' will do \\[verilog-auto]
  714. automatically when it thinks necessary. A value of `ask' will query the
  715. user when it thinks updating is needed.
  716. You should not rely on the `ask' or `detect' policies, they are safeguards
  717. only. They do not detect when AUTOINSTs need to be updated because a
  718. sub-module's port list has changed."
  719. :group 'verilog-mode-actions
  720. :type '(choice (const nil) (const ask) (const detect) (const force)))
  721. (defcustom verilog-auto-star-expand t
  722. "Non-nil means to expand SystemVerilog .* instance ports.
  723. They will be expanded in the same way as if there was an AUTOINST in the
  724. instantiation. See also `verilog-auto-star' and `verilog-auto-star-save'."
  725. :group 'verilog-mode-actions
  726. :type 'boolean)
  727. (put 'verilog-auto-star-expand 'safe-local-variable 'verilog-booleanp)
  728. (defcustom verilog-auto-star-save nil
  729. "Non-nil means save to disk SystemVerilog .* instance expansions.
  730. A nil value indicates direct connections will be removed before saving.
  731. Only meaningful to those created due to `verilog-auto-star-expand' being set.
  732. Instead of setting this, you may want to use /*AUTOINST*/, which will
  733. always be saved."
  734. :group 'verilog-mode-actions
  735. :type 'boolean)
  736. (put 'verilog-auto-star-save 'safe-local-variable 'verilog-booleanp)
  737. (defvar verilog-auto-update-tick nil
  738. "Modification tick at which autos were last performed.")
  739. (defvar verilog-auto-last-file-locals nil
  740. "Text from file-local-variables during last evaluation.")
  741. (defvar verilog-diff-function 'verilog-diff-report
  742. "Function to run when `verilog-diff-auto' detects differences.
  743. Function takes three arguments, the original buffer, the
  744. difference buffer, and the point in original buffer with the
  745. first difference.")
  746. (defvar verilog-diff-ignore-regexp nil
  747. "Non-nil specifies regexp which `verilog-diff-auto' will ignore.
  748. This is typically nil.")
  749. ;;; Compile support:
  750. ;;
  751. (require 'compile)
  752. (defvar verilog-error-regexp-added nil)
  753. (defvar verilog-error-regexp-emacs-alist
  754. '(
  755. (verilog-xl-1
  756. "\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 3)
  757. (verilog-xl-2
  758. "([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 1 3)
  759. (verilog-IES
  760. ".*\\*[WE],[0-9A-Z]+\\(\\[[0-9A-Z_,]+\\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)" 2 3)
  761. (verilog-surefire-1
  762. "[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 2)
  763. (verilog-surefire-2
  764. "\\(WARNING\\|ERROR\\|INFO\\)[^:]*: \\([^,]+\\),\\s-+\\(line \\)?\\([0-9]+\\):" 2 4 )
  765. (verilog-verbose
  766. "\
  767. \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
  768. :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 2 5)
  769. (verilog-xsim
  770. "\\(Error\\|Warning\\).*in file (\\([^ \t]+\\) at line *\\([0-9]+\\))" 2 3)
  771. (verilog-vcs-1
  772. "\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 3)
  773. (verilog-vcs-2
  774. "Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 2)
  775. (verilog-vcs-3
  776. "\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 3)
  777. (verilog-vcs-4
  778. "syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 2)
  779. (verilog-verilator
  780. "%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 4)
  781. (verilog-leda
  782. "^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 1 2)
  783. )
  784. "List of regexps for Verilog compilers.
  785. See `compilation-error-regexp-alist' for the formatting. For Emacs 22+.")
  786. (defvar verilog-error-regexp-xemacs-alist
  787. ;; Emacs form is '((v-tool "re" 1 2) ...)
  788. ;; XEmacs form is '(verilog ("re" 1 2) ...)
  789. ;; So we can just map from Emacs to XEmacs
  790. (cons 'verilog (mapcar 'cdr verilog-error-regexp-emacs-alist))
  791. "List of regexps for Verilog compilers.
  792. See `compilation-error-regexp-alist-alist' for the formatting. For XEmacs.")
  793. (defvar verilog-error-font-lock-keywords
  794. '(
  795. ;; verilog-xl-1
  796. ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
  797. ("\\(Error\\|Warning\\)!.*\n?.*\"\\([^\"]+\\)\", \\([0-9]+\\)" 2 bold t)
  798. ;; verilog-xl-2
  799. ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 1 bold t)
  800. ("([WE][0-9A-Z]+)[ \t]+\\([^ \t\n,]+\\)[, \t]+\\(line[ \t]+\\)?\\([0-9]+\\):.*$" 3 bold t)
  801. ;; verilog-IES (nc-verilog)
  802. (".*\\*[WE],[0-9A-Z]+\\(\\[[0-9A-Z_,]+\\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)|" 2 bold t)
  803. (".*\\*[WE],[0-9A-Z]+\\(\\[[0-9A-Z_,]+\\]\\)? (\\([^ \t,]+\\),\\([0-9]+\\)|" 3 bold t)
  804. ;; verilog-surefire-1
  805. ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 1 bold t)
  806. ("[^\n]*\\[\\([^:]+\\):\\([0-9]+\\)\\]" 2 bold t)
  807. ;; verilog-surefire-2
  808. ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 2 bold t)
  809. ("\\(WARNING\\|ERROR\\|INFO\\): \\([^,]+\\), line \\([0-9]+\\):" 3 bold t)
  810. ;; verilog-verbose
  811. ("\
  812. \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
  813. :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
  814. ("\
  815. \\([a-zA-Z]?:?[^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)\\([) \t]\\|\
  816. :\\([^0-9\n]\\|\\([0-9]+:\\)\\)\\)" 1 bold t)
  817. ;; verilog-vcs-1
  818. ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 2 bold t)
  819. ("\\(Error\\|Warning\\):[^(]*(\\([^ \t]+\\) line *\\([0-9]+\\))" 3 bold t)
  820. ;; verilog-vcs-2
  821. ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
  822. ("Warning:.*(port.*(\\([^ \t]+\\) line \\([0-9]+\\))" 1 bold t)
  823. ;; verilog-vcs-3
  824. ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
  825. ("\\(Error\\|Warning\\):[\n.]*\\([^ \t]+\\) *\\([0-9]+\\):" 3 bold t)
  826. ;; verilog-vcs-4
  827. ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 1 bold t)
  828. ("syntax error:.*\n\\([^ \t]+\\) *\\([0-9]+\\):" 2 bold t)
  829. ;; verilog-verilator
  830. (".*%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 3 bold t)
  831. (".*%?\\(Error\\|Warning\\)\\(-[^:]+\\|\\):[\n ]*\\([^ \t:]+\\):\\([0-9]+\\):" 4 bold t)
  832. ;; verilog-leda
  833. ("^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 1 bold t)
  834. ("^In file \\([^ \t]+\\)[ \t]+line[ \t]+\\([0-9]+\\):\n[^\n]*\n[^\n]*\n\\(Warning\\|Error\\|Failure\\)[^\n]*" 2 bold t)
  835. )
  836. "Keywords to also highlight in Verilog *compilation* buffers.
  837. Only used in XEmacs; GNU Emacs uses `verilog-error-regexp-emacs-alist'.")
  838. (defcustom verilog-library-flags '("")
  839. "List of standard Verilog arguments to use for /*AUTOINST*/.
  840. These arguments are used to find files for `verilog-auto', and match
  841. the flags accepted by a standard Verilog-XL simulator.
  842. -f filename Reads more `verilog-library-flags' from the filename.
  843. +incdir+dir Adds the directory to `verilog-library-directories'.
  844. -Idir Adds the directory to `verilog-library-directories'.
  845. -y dir Adds the directory to `verilog-library-directories'.
  846. +libext+.v Adds the extensions to `verilog-library-extensions'.
  847. -v filename Adds the filename to `verilog-library-files'.
  848. filename Adds the filename to `verilog-library-files'.
  849. This is not recommended, -v is a better choice.
  850. You might want these defined in each file; put at the *END* of your file
  851. something like:
  852. // Local Variables:
  853. // verilog-library-flags:(\"-y dir -y otherdir\")
  854. // End:
  855. Verilog-mode attempts to detect changes to this local variable, but they
  856. are only insured to be correct when the file is first visited. Thus if you
  857. have problems, use \\[find-alternate-file] RET to have these take effect.
  858. See also the variables mentioned above."
  859. :group 'verilog-mode-auto
  860. :type '(repeat string))
  861. (put 'verilog-library-flags 'safe-local-variable 'listp)
  862. (defcustom verilog-library-directories '(".")
  863. "List of directories when looking for files for /*AUTOINST*/.
  864. The directory may be relative to the current file, or absolute.
  865. Environment variables are also expanded in the directory names.
  866. Having at least the current directory is a good idea.
  867. You might want these defined in each file; put at the *END* of your file
  868. something like:
  869. // Local Variables:
  870. // verilog-library-directories:(\".\" \"subdir\" \"subdir2\")
  871. // End:
  872. Verilog-mode attempts to detect changes to this local variable, but they
  873. are only insured to be correct when the file is first visited. Thus if you
  874. have problems, use \\[find-alternate-file] RET to have these take effect.
  875. See also `verilog-library-flags', `verilog-library-files'
  876. and `verilog-library-extensions'."
  877. :group 'verilog-mode-auto
  878. :type '(repeat file))
  879. (put 'verilog-library-directories 'safe-local-variable 'listp)
  880. (defcustom verilog-library-files '()
  881. "List of files to search for modules.
  882. AUTOINST will use this when it needs to resolve a module name.
  883. This is a complete path, usually to a technology file with many standard
  884. cells defined in it.
  885. You might want these defined in each file; put at the *END* of your file
  886. something like:
  887. // Local Variables:
  888. // verilog-library-files:(\"/some/path/technology.v\" \"/some/path/tech2.v\")
  889. // End:
  890. Verilog-mode attempts to detect changes to this local variable, but they
  891. are only insured to be correct when the file is first visited. Thus if you
  892. have problems, use \\[find-alternate-file] RET to have these take effect.
  893. See also `verilog-library-flags', `verilog-library-directories'."
  894. :group 'verilog-mode-auto
  895. :type '(repeat directory))
  896. (put 'verilog-library-files 'safe-local-variable 'listp)
  897. (defcustom verilog-library-extensions '(".v" ".sv")
  898. "List of extensions to use when looking for files for /*AUTOINST*/.
  899. See also `verilog-library-flags', `verilog-library-directories'."
  900. :type '(repeat string)
  901. :group 'verilog-mode-auto)
  902. (put 'verilog-library-extensions 'safe-local-variable 'listp)
  903. (defcustom verilog-active-low-regexp nil
  904. "If true, treat signals matching this regexp as active low.
  905. This is used for AUTORESET and AUTOTIEOFF. For proper behavior,
  906. you will probably also need `verilog-auto-reset-widths' set."
  907. :group 'verilog-mode-auto
  908. :type '(choice (const nil) regexp))
  909. (put 'verilog-active-low-regexp 'safe-local-variable 'stringp)
  910. (defcustom verilog-auto-sense-include-inputs nil
  911. "Non-nil means AUTOSENSE should include all inputs.
  912. If nil, only inputs that are NOT output signals in the same block are
  913. included."
  914. :group 'verilog-mode-auto
  915. :type 'boolean)
  916. (put 'verilog-auto-sense-include-inputs 'safe-local-variable 'verilog-booleanp)
  917. (defcustom verilog-auto-sense-defines-constant nil
  918. "Non-nil means AUTOSENSE should assume all defines represent constants.
  919. When true, the defines will not be included in sensitivity lists. To
  920. maintain compatibility with other sites, this should be set at the bottom
  921. of each Verilog file that requires it, rather than being set globally."
  922. :group 'verilog-mode-auto
  923. :type 'boolean)
  924. (put 'verilog-auto-sense-defines-constant 'safe-local-variable 'verilog-booleanp)
  925. (defcustom verilog-auto-reset-blocking-in-non t
  926. "Non-nil means AUTORESET will reset blocking statements.
  927. When true, AUTORESET will reset in blocking statements those
  928. signals which were assigned with blocking assignments (=) even in
  929. a block with non-blocking assignments (<=).
  930. If nil, all blocking assigned signals are ignored when any
  931. non-blocking assignment is in the AUTORESET block. This allows
  932. blocking assignments to be used for temporary values and not have
  933. those temporaries reset. See example in `verilog-auto-reset'."
  934. :version "24.1" ; rev718
  935. :type 'boolean
  936. :group 'verilog-mode-auto)
  937. (put 'verilog-auto-reset-blocking-in-non 'safe-local-variable 'verilog-booleanp)
  938. (defcustom verilog-auto-reset-widths t
  939. "True means AUTORESET should determine the width of signals.
  940. This is then used to set the width of the zero (32'h0 for example). This
  941. is required by some lint tools that aren't smart enough to ignore widths of
  942. the constant zero. This may result in ugly code when parameters determine
  943. the MSB or LSB of a signal inside an AUTORESET.
  944. If nil, AUTORESET uses \"0\" as the constant.
  945. If `unbased', AUTORESET used the unbased unsized literal \"\\='0\"
  946. as the constant. This setting is strongly recommended for
  947. SystemVerilog designs."
  948. :type 'boolean
  949. :group 'verilog-mode-auto)
  950. (put 'verilog-auto-reset-widths 'safe-local-variable
  951. '(lambda (x) (memq x '(nil t unbased))))
  952. (defcustom verilog-assignment-delay ""
  953. "Text used for delays in delayed assignments. Add a trailing space if set."
  954. :group 'verilog-mode-auto
  955. :type 'string)
  956. (put 'verilog-assignment-delay 'safe-local-variable 'stringp)
  957. (defcustom verilog-auto-arg-format 'packed
  958. "Formatting to use for AUTOARG signal names.
  959. If `packed', then as many inputs and outputs that fit within
  960. `fill-column' will be put onto one line.
  961. If `single', then a single input or output will be put onto each
  962. line."
  963. :version "25.1"
  964. :type '(radio (const :tag "Line up Assignments and Declarations" packed)
  965. (const :tag "Line up Assignment statements" single))
  966. :group 'verilog-mode-auto)
  967. (put 'verilog-auto-arg-format 'safe-local-variable
  968. '(lambda (x) (memq x '(packed single))))
  969. (defcustom verilog-auto-arg-sort nil
  970. "Non-nil means AUTOARG signal names will be sorted, not in declaration order.
  971. Declaration order is advantageous with order based instantiations
  972. and is the default for backward compatibility. Sorted order
  973. reduces changes when declarations are moved around in a file, and
  974. it's bad practice to rely on order based instantiations anyhow.
  975. See also `verilog-auto-inst-sort'."
  976. :group 'verilog-mode-auto
  977. :type 'boolean)
  978. (put 'verilog-auto-arg-sort 'safe-local-variable 'verilog-booleanp)
  979. (defcustom verilog-auto-inst-dot-name nil
  980. "Non-nil means when creating ports with AUTOINST, use .name syntax.
  981. This will use \".port\" instead of \".port(port)\" when possible.
  982. This is only legal in SystemVerilog files, and will confuse older
  983. simulators. Setting `verilog-auto-inst-vector' to nil may also
  984. be desirable to increase how often .name will be used."
  985. :group 'verilog-mode-auto
  986. :type 'boolean)
  987. (put 'verilog-auto-inst-dot-name 'safe-local-variable 'verilog-booleanp)
  988. (defcustom verilog-auto-inst-param-value nil
  989. "Non-nil means AUTOINST will replace parameters with the parameter value.
  990. If nil, leave parameters as symbolic names.
  991. Parameters must be in Verilog 2001 format #(...), and if a parameter is not
  992. listed as such there (as when the default value is acceptable), it will not
  993. be replaced, and will remain symbolic.
  994. For example, imagine a submodule uses parameters to declare the size of its
  995. inputs. This is then used by an upper module:
  996. module InstModule (o,i);
  997. parameter WIDTH;
  998. input [WIDTH-1:0] i;
  999. parameter type OUT_t;
  1000. output OUT_t o;
  1001. endmodule
  1002. module ExampInst;
  1003. /*AUTOOUTPUT*/
  1004. // Beginning of automatic outputs
  1005. output OUT_t o;
  1006. // End of automatics
  1007. InstModule
  1008. #(.WIDTH(10),
  1009. ,.OUT_t(upper_t))
  1010. instName
  1011. (/*AUTOINST*/
  1012. .i (i[WIDTH-1:0]),
  1013. .o (o));
  1014. Note even though WIDTH=10, the AUTOINST has left the parameter as
  1015. a symbolic name. Likewise the OUT_t is preserved as the name
  1016. from the instantiated module.
  1017. If `verilog-auto-inst-param-value' is set, this will
  1018. instead expand to:
  1019. module ExampInst;
  1020. /*AUTOOUTPUT*/
  1021. // Beginning of automatic outputs
  1022. output upper_t o;
  1023. // End of automatics
  1024. InstModule
  1025. #(.WIDTH(10),
  1026. ,.OUT_t(upper_t))
  1027. instName
  1028. (/*AUTOINST*/
  1029. .i (i[9:0]),
  1030. .o (o));
  1031. Note that the instantiation now has \"i[9:0]\" as the WIDTH
  1032. was expanded. Likewise the data type of \"o\" in the AUTOOUTPUT
  1033. is now upper_t, from the OUT_t parameter override.
  1034. This second expansion of parameter types can be overridden with
  1035. `verilog-auto-inst-param-value-type'."
  1036. :group 'verilog-mode-auto
  1037. :type 'boolean)
  1038. (put 'verilog-auto-inst-param-value 'safe-local-variable 'verilog-booleanp)
  1039. (defcustom verilog-auto-inst-param-value-type t
  1040. "Non-nil means expand parameter type in instantiations.
  1041. If nil, leave parameter types as symbolic names.
  1042. See `verilog-auto-inst-param-value'."
  1043. :version "25.1"
  1044. :group 'verilog-mode-auto
  1045. :type 'boolean)
  1046. (put 'verilog-auto-inst-param-value-type 'safe-local-variable 'verilog-booleanp)
  1047. (defcustom verilog-auto-inst-sort nil
  1048. "Non-nil means AUTOINST signals will be sorted, not in declaration order.
  1049. Also affects AUTOINSTPARAM. Declaration order is the default for
  1050. backward compatibility, and as some teams prefer signals that are
  1051. declared together to remain together. Sorted order reduces
  1052. changes when declarations are moved around in a file.
  1053. See also `verilog-auto-arg-sort'."
  1054. :version "24.1" ; rev688
  1055. :group 'verilog-mode-auto
  1056. :type 'boolean)
  1057. (put 'verilog-auto-inst-sort 'safe-local-variable 'verilog-booleanp)
  1058. (defcustom verilog-auto-inst-vector t
  1059. "Non-nil means when creating default ports with AUTOINST, use bus subscripts.
  1060. If nil, skip the subscript when it matches the entire bus as declared in
  1061. the module (AUTOWIRE signals always are subscripted, you must manually
  1062. declare the wire to have the subscripts removed.) Setting this to nil may
  1063. speed up some simulators, but is less general and harder to read, so avoid."
  1064. :group 'verilog-mode-auto
  1065. :type 'boolean)
  1066. (put 'verilog-auto-inst-vector 'safe-local-variable 'verilog-booleanp)
  1067. (defcustom verilog-auto-inst-template-numbers nil
  1068. "If true, when creating templated ports with AUTOINST, add a comment.
  1069. If t, the comment will add the line number of the template that
  1070. was used for that port declaration. This setting is suggested
  1071. only for debugging use, as regular use may cause a large numbers
  1072. of merge conflicts.
  1073. If `lhs', the comment will show the left hand side of the
  1074. AUTO_TEMPLATE rule that is matched. This is less precise than
  1075. numbering (t) when multiple rules have the same pin name, but
  1076. won't merge conflict."
  1077. :group 'verilog-mode-auto
  1078. :type '(choice (const nil) (const t) (const lhs)))
  1079. (put 'verilog-auto-inst-template-numbers 'safe-local-variable
  1080. '(lambda (x) (memq x '(nil t lhs))))
  1081. (defcustom verilog-auto-inst-column 40
  1082. "Indent-to column number for net name part of AUTOINST created pin."
  1083. :group 'verilog-mode-indent
  1084. :type 'integer)
  1085. (put 'verilog-auto-inst-column 'safe-local-variable 'integerp)
  1086. (defcustom verilog-auto-inst-interfaced-ports nil
  1087. "Non-nil means include interfaced ports in AUTOINST expansions."
  1088. :version "24.3" ; rev773, default change rev815
  1089. :group 'verilog-mode-auto
  1090. :type 'boolean)
  1091. (put 'verilog-auto-inst-interfaced-ports 'safe-local-variable 'verilog-booleanp)
  1092. (defcustom verilog-auto-input-ignore-regexp nil
  1093. "If non-nil, when creating AUTOINPUT, ignore signals matching this regexp.
  1094. See the \\[verilog-faq] for examples on using this."
  1095. :group 'verilog-mode-auto
  1096. :type '(choice (const nil) regexp))
  1097. (put 'verilog-auto-input-ignore-regexp 'safe-local-variable 'stringp)
  1098. (defcustom verilog-auto-inout-ignore-regexp nil
  1099. "If non-nil, when creating AUTOINOUT, ignore signals matching this regexp.
  1100. See the \\[verilog-faq] for examples on using this."
  1101. :group 'verilog-mode-auto
  1102. :type '(choice (const nil) regexp))
  1103. (put 'verilog-auto-inout-ignore-regexp 'safe-local-variable 'stringp)
  1104. (defcustom verilog-auto-output-ignore-regexp nil
  1105. "If non-nil, when creating AUTOOUTPUT, ignore signals matching this regexp.
  1106. See the \\[verilog-faq] for examples on using this."
  1107. :group 'verilog-mode-auto
  1108. :type '(choice (const nil) regexp))
  1109. (put 'verilog-auto-output-ignore-regexp 'safe-local-variable 'stringp)
  1110. (defcustom verilog-auto-template-warn-unused nil
  1111. "Non-nil means report warning if an AUTO_TEMPLATE line is not used.
  1112. This feature is not supported before Emacs 21.1 or XEmacs 21.4."
  1113. :version "24.3" ; rev787
  1114. :group 'verilog-mode-auto
  1115. :type 'boolean)
  1116. (put 'verilog-auto-template-warn-unused 'safe-local-variable 'verilog-booleanp)
  1117. (defcustom verilog-auto-tieoff-declaration "wire"
  1118. "Data type used for the declaration for AUTOTIEOFF.
  1119. If \"wire\" then create a wire, if \"assign\" create an
  1120. assignment, else the data type for variable creation."
  1121. :version "24.1" ; rev713
  1122. :group 'verilog-mode-auto
  1123. :type 'string)
  1124. (put 'verilog-auto-tieoff-declaration 'safe-local-variable 'stringp)
  1125. (defcustom verilog-auto-tieoff-ignore-regexp nil
  1126. "If non-nil, when creating AUTOTIEOFF, ignore signals matching this regexp.
  1127. See the \\[verilog-faq] for examples on using this."
  1128. :group 'verilog-mode-auto
  1129. :type '(choice (const nil) regexp))
  1130. (put 'verilog-auto-tieoff-ignore-regexp 'safe-local-variable 'stringp)
  1131. (defcustom verilog-auto-unused-ignore-regexp nil
  1132. "If non-nil, when creating AUTOUNUSED, ignore signals matching this regexp.
  1133. See the \\[verilog-faq] for examples on using this."
  1134. :group 'verilog-mode-auto
  1135. :type '(choice (const nil) regexp))
  1136. (put 'verilog-auto-unused-ignore-regexp 'safe-local-variable 'stringp)
  1137. (defcustom verilog-case-fold t
  1138. "Non-nil means `verilog-mode' regexps should ignore case.
  1139. This variable is t for backward compatibility; nil is suggested."
  1140. :version "24.4"
  1141. :group 'verilog-mode
  1142. :type 'boolean)
  1143. (put 'verilog-case-fold 'safe-local-variable 'verilog-booleanp)
  1144. (defcustom verilog-typedef-regexp nil
  1145. "If non-nil, regular expression that matches Verilog-2001 typedef names.
  1146. For example, \"_t$\" matches typedefs named with _t, as in the C language.
  1147. See also `verilog-case-fold'."
  1148. :group 'verilog-mode-auto
  1149. :type '(choice (const nil) regexp))
  1150. (put 'verilog-typedef-regexp 'safe-local-variable 'stringp)
  1151. (defcustom verilog-mode-hook 'verilog-set-compile-command
  1152. "Hook run after Verilog mode is loaded."
  1153. :type 'hook
  1154. :group 'verilog-mode)
  1155. (defcustom verilog-auto-hook nil
  1156. "Hook run after `verilog-mode' updates AUTOs."
  1157. :group 'verilog-mode-auto
  1158. :type 'hook)
  1159. (defcustom verilog-before-auto-hook nil
  1160. "Hook run before `verilog-mode' updates AUTOs."
  1161. :group 'verilog-mode-auto
  1162. :type 'hook)
  1163. (defcustom verilog-delete-auto-hook nil
  1164. "Hook run after `verilog-mode' deletes AUTOs."
  1165. :group 'verilog-mode-auto
  1166. :type 'hook)
  1167. (defcustom verilog-before-delete-auto-hook nil
  1168. "Hook run before `verilog-mode' deletes AUTOs."
  1169. :group 'verilog-mode-auto
  1170. :type 'hook)
  1171. (defcustom verilog-getopt-flags-hook nil
  1172. "Hook run after `verilog-getopt-flags' determines the Verilog option lists."
  1173. :group 'verilog-mode-auto
  1174. :type 'hook)
  1175. (defcustom verilog-before-getopt-flags-hook nil
  1176. "Hook run before `verilog-getopt-flags' determines the Verilog option lists."
  1177. :group 'verilog-mode-auto
  1178. :type 'hook)
  1179. (defcustom verilog-before-save-font-hook nil
  1180. "Hook run before `verilog-save-font-mods' removes highlighting."
  1181. :version "24.3" ; rev735
  1182. :group 'verilog-mode-auto
  1183. :type 'hook)
  1184. (defcustom verilog-after-save-font-hook nil
  1185. "Hook run after `verilog-save-font-mods' restores highlighting."
  1186. :version "24.3" ; rev735
  1187. :group 'verilog-mode-auto
  1188. :type 'hook)
  1189. (defvar verilog-imenu-generic-expression
  1190. '((nil "^\\s-*\\(?:m\\(?:odule\\|acromodule\\)\\|p\\(?:rimitive\\|rogram\\|ackage\\)\\)\\s-+\\([a-zA-Z0-9_.:]+\\)" 1)
  1191. ("*Variables*" "^\\s-*\\(reg\\|wire\\|logic\\)\\s-+\\(\\|\\[[^]]+\\]\\s-+\\)\\([A-Za-z0-9_]+\\)" 3)
  1192. ("*Classes*" "^\\s-*\\(?:\\(?:virtual\\|interface\\)\\s-+\\)?class\\s-+\\([A-Za-z_][A-Za-z0-9_]+\\)" 1)
  1193. ("*Tasks*" "^\\s-*\\(?:\\(?:static\\|pure\\|virtual\\|local\\|protected\\)\\s-+\\)*task\\s-+\\(?:\\(?:static\\|automatic\\)\\s-+\\)?\\([A-Za-z_][A-Za-z0-9_:]+\\)" 1)
  1194. ("*Functions*" "^\\s-*\\(?:\\(?:static\\|pure\\|virtual\\|local\\|protected\\)\\s-+\\)*function\\s-+\\(?:\\(?:static\\|automatic\\)\\s-+\\)?\\(?:\\w+\\s-+\\)?\\([A-Za-z_][A-Za-z0-9_:]+\\)" 1)
  1195. ("*Interfaces*" "^\\s-*interface\\s-+\\([a-zA-Z_0-9]+\\)" 1)
  1196. ("*Types*" "^\\s-*typedef\\s-+.*\\s-+\\([a-zA-Z_0-9]+\\)\\s-*;" 1))
  1197. "Imenu expression for Verilog mode. See `imenu-generic-expression'.")
  1198. ;;
  1199. ;; provide a verilog-header function.
  1200. ;; Customization variables:
  1201. ;;
  1202. (defvar verilog-date-scientific-format nil
  1203. "If non-nil, dates are written in scientific format (e.g. 1997/09/17).
  1204. If nil, in European format (e.g. 17.09.1997). The brain-dead American
  1205. format (e.g. 09/17/1997) is not supported.")
  1206. (defvar verilog-company nil
  1207. "Default name of Company for Verilog header.
  1208. If set will become buffer local.")
  1209. (make-variable-buffer-local 'verilog-company)
  1210. (defvar verilog-project nil
  1211. "Default name of Project for Verilog header.
  1212. If set will become buffer local.")
  1213. (make-variable-buffer-local 'verilog-project)
  1214. ;;; Keymap and Menu:
  1215. ;;
  1216. (defvar verilog-mode-map
  1217. (let ((map (make-sparse-keymap)))
  1218. (define-key map ";" 'electric-verilog-semi)
  1219. (define-key map [(control 59)] 'electric-verilog-semi-with-comment)
  1220. (define-key map ":" 'electric-verilog-colon)
  1221. ;;(define-key map "=" 'electric-verilog-equal)
  1222. (define-key map "`" 'electric-verilog-tick)
  1223. (define-key map "\t" 'electric-verilog-tab)
  1224. (define-key map "\r" 'electric-verilog-terminate-line)
  1225. ;; backspace/delete key bindings
  1226. (define-key map [backspace] 'backward-delete-char-untabify)
  1227. (unless (boundp 'delete-key-deletes-forward) ; XEmacs variable
  1228. (define-key map [delete] 'delete-char)
  1229. (define-key map [(meta delete)] 'kill-word))
  1230. (define-key map "\M-\C-b" 'electric-verilog-backward-sexp)
  1231. (define-key map "\M-\C-f" 'electric-verilog-forward-sexp)
  1232. (define-key map "\M-\r" `electric-verilog-terminate-and-indent)
  1233. (define-key map "\M-\t" (if (fboundp 'completion-at-point)
  1234. 'completion-at-point 'verilog-complete-word))
  1235. (define-key map "\M-?" (if (fboundp 'completion-help-at-point)
  1236. 'completion-help-at-point 'verilog-show-completions))
  1237. ;; Note \C-c and letter are reserved for users
  1238. (define-key map "\C-c`" 'verilog-lint-off)
  1239. (define-key map "\C-c*" 'verilog-delete-auto-star-implicit)
  1240. (define-key map "\C-c?" 'verilog-diff-auto)
  1241. (define-key map "\C-c\C-r" 'verilog-label-be)
  1242. (define-key map "\C-c\C-i" 'verilog-pretty-declarations)
  1243. (define-key map "\C-c=" 'verilog-pretty-expr)
  1244. (define-key map "\C-c\C-b" 'verilog-submit-bug-report)
  1245. (define-key map "\C-c/" 'verilog-star-comment)
  1246. (define-key map "\C-c\C-c" 'verilog-comment-region)
  1247. (define-key map "\C-c\C-u" 'verilog-uncomment-region)
  1248. (when (featurep 'xemacs)
  1249. (define-key map [(meta control h)] 'verilog-mark-defun)
  1250. (define-key map "\M-\C-a" 'verilog-beg-of-defun)
  1251. (define-key map "\M-\C-e" 'verilog-end-of-defun))
  1252. (define-key map "\C-c\C-d" 'verilog-goto-defun)
  1253. (define-key map "\C-c\C-k" 'verilog-delete-auto)
  1254. (define-key map "\C-c\C-a" 'verilog-auto)
  1255. (define-key map "\C-c\C-s" 'verilog-auto-save-compile)
  1256. (define-key map "\C-c\C-p" 'verilog-preprocess)
  1257. (define-key map "\C-c\C-z" 'verilog-inject-auto)
  1258. (define-key map "\C-c\C-e" 'verilog-expand-vector)
  1259. (define-key map "\C-c\C-h" 'verilog-header)
  1260. map)
  1261. "Keymap used in Verilog mode.")
  1262. ;; menus
  1263. (easy-menu-define
  1264. verilog-menu verilog-mode-map "Menu for Verilog mode"
  1265. (verilog-easy-menu-filter
  1266. `("Verilog"
  1267. ("Choose Compilation Action"
  1268. ["None"
  1269. (progn
  1270. (setq verilog-tool nil)
  1271. (verilog-set-compile-command))
  1272. :style radio
  1273. :selected (equal verilog-tool nil)
  1274. :help "When invoking compilation, use compile-command"]
  1275. ["Lint"
  1276. (progn
  1277. (setq verilog-tool 'verilog-linter)
  1278. (verilog-set-compile-command))
  1279. :style radio
  1280. :selected (equal verilog-tool `verilog-linter)
  1281. :help "When invoking compilation, use lint checker"]
  1282. ["Coverage"
  1283. (progn
  1284. (setq verilog-tool 'verilog-coverage)
  1285. (verilog-set-compile-command))
  1286. :style radio
  1287. :selected (equal verilog-tool `verilog-coverage)
  1288. :help "When invoking compilation, annotate for coverage"]
  1289. ["Simulator"
  1290. (progn
  1291. (setq verilog-tool 'verilog-simulator)
  1292. (verilog-set-compile-command))
  1293. :style radio
  1294. :selected (equal verilog-tool `verilog-simulator)
  1295. :help "When invoking compilation, interpret Verilog source"]
  1296. ["Compiler"
  1297. (progn
  1298. (setq verilog-tool 'verilog-compiler)
  1299. (verilog-set-compile-command))
  1300. :style radio
  1301. :selected (equal verilog-tool `verilog-compiler)
  1302. :help "When invoking compilation, compile Verilog source"]
  1303. ["Preprocessor"
  1304. (progn
  1305. (setq verilog-tool 'verilog-preprocessor)
  1306. (verilog-set-compile-command))
  1307. :style radio
  1308. :selected (equal verilog-tool `verilog-preprocessor)
  1309. :help "When invoking compilation, preprocess Verilog source, see also `verilog-preprocess'"]
  1310. )
  1311. ("Move"
  1312. ["Beginning of function" verilog-beg-of-defun
  1313. :keys "C-M-a"
  1314. :help "Move backward to the beginning of the current function or procedure"]
  1315. ["End of function" verilog-end-of-defun
  1316. :keys "C-M-e"
  1317. :help "Move forward to the end of the current function or procedure"]
  1318. ["Mark function" verilog-mark-defun
  1319. :keys "C-M-h"
  1320. :help "Mark the current Verilog function or procedure"]
  1321. ["Goto function/module" verilog-goto-defun
  1322. :help "Move to specified Verilog module/task/function"]
  1323. ["Move to beginning of block" electric-verilog-backward-sexp
  1324. :help "Move backward over one balanced expression"]
  1325. ["Move to end of block" electric-verilog-forward-sexp
  1326. :help "Move forward over one balanced expression"]
  1327. )
  1328. ("Comments"
  1329. ["Comment Region" verilog-comment-region
  1330. :help "Put marked area into a comment"]
  1331. ["UnComment Region" verilog-uncomment-region
  1332. :help "Uncomment an area commented with Comment Region"]
  1333. ["Multi-line comment insert" verilog-star-comment
  1334. :help "Insert Verilog /* */ comment at point"]
  1335. ["Lint error to comment" verilog-lint-off
  1336. :help "Convert a Verilog linter warning line into a disable statement"]
  1337. )
  1338. "----"
  1339. ["Compile" compile
  1340. :help "Perform compilation-action (above) on the current buffer"]
  1341. ["AUTO, Save, Compile" verilog-auto-save-compile
  1342. :help "Recompute AUTOs, save buffer, and compile"]
  1343. ["Next Compile Error" next-error
  1344. :help "Visit next compilation error message and corresponding source code"]
  1345. ["Ignore Lint Warning at point" verilog-lint-off
  1346. :help "Convert a Verilog linter warning line into a disable statement"]
  1347. "----"
  1348. ["Line up declarations around point" verilog-pretty-declarations
  1349. :help "Line up declarations around point"]
  1350. ["Line up equations around point" verilog-pretty-expr
  1351. :help "Line up expressions around point"]
  1352. ["Redo/insert comments on every end" verilog-label-be
  1353. :help "Label matching begin ... end statements"]
  1354. ["Expand [x:y] vector line" verilog-expand-vector
  1355. :help "Take a signal vector on the current line and expand it to multiple lines"]
  1356. ["Insert begin-end block" verilog-insert-block
  1357. :help "Insert begin ... end"]
  1358. ["Complete word" ,(if (fboundp 'completion-at-point)
  1359. 'completion-at-point 'verilog-complete-word)
  1360. :help "Complete word at point"]
  1361. "----"
  1362. ["Recompute AUTOs" verilog-auto
  1363. :help "Expand AUTO meta-comment statements"]
  1364. ["Kill AUTOs" verilog-delete-auto
  1365. :help "Remove AUTO expansions"]
  1366. ["Diff AUTOs" verilog-diff-auto
  1367. :help "Show differences in AUTO expansions"]
  1368. ["Inject AUTOs" verilog-inject-auto
  1369. :help "Inject AUTOs into legacy non-AUTO buffer"]
  1370. ("AUTO Help..."
  1371. ["AUTO General" (describe-function 'verilog-auto)
  1372. :help "Help introduction on AUTOs"]
  1373. ["AUTO Library Flags" (describe-variable 'verilog-library-flags)
  1374. :help "Help on verilog-library-flags"]
  1375. ["AUTO Library Path" (describe-variable 'verilog-library-directories)
  1376. :help "Help on verilog-library-directories"]
  1377. ["AUTO Library Files" (describe-variable 'verilog-library-files)
  1378. :help "Help on verilog-library-files"]
  1379. ["AUTO Library Extensions" (describe-variable 'verilog-library-extensions)
  1380. :help "Help on verilog-library-extensions"]
  1381. ["AUTO `define Reading" (describe-function 'verilog-read-defines)
  1382. :help "Help on reading `defines"]
  1383. ["AUTO `include Reading" (describe-function 'verilog-read-includes)
  1384. :help "Help on parsing `includes"]
  1385. ["AUTOARG" (describe-function 'verilog-auto-arg)
  1386. :help "Help on AUTOARG - declaring module port list"]
  1387. ["AUTOASCIIENUM" (describe-function 'verilog-auto-ascii-enum)
  1388. :help "Help on AUTOASCIIENUM - creating ASCII for enumerations"]
  1389. ["AUTOASSIGNMODPORT" (describe-function 'verilog-auto-assign-modport)
  1390. :help "Help on AUTOASSIGNMODPORT - creating assignments to/from modports"]
  1391. ["AUTOINOUT" (describe-function 'verilog-auto-inout)
  1392. :help "Help on AUTOINOUT - adding inouts from cells"]
  1393. ["AUTOINOUTCOMP" (describe-function 'verilog-auto-inout-comp)
  1394. :help "Help on AUTOINOUTCOMP - copying complemented i/o from another file"]
  1395. ["AUTOINOUTIN" (describe-function 'verilog-auto-inout-in)
  1396. :help "Help on AUTOINOUTIN - copying i/o from another file as all inputs"]
  1397. ["AUTOINOUTMODPORT" (describe-function 'verilog-auto-inout-modport)
  1398. :help "Help on AUTOINOUTMODPORT - copying i/o from an interface modport"]
  1399. ["AUTOINOUTMODULE" (describe-function 'verilog-auto-inout-module)
  1400. :help "Help on AUTOINOUTMODULE - copying i/o from another file"]
  1401. ["AUTOINOUTPARAM" (describe-function 'verilog-auto-inout-param)
  1402. :help "Help on AUTOINOUTPARAM - copying parameters from another file"]
  1403. ["AUTOINPUT" (describe-function 'verilog-auto-input)
  1404. :help "Help on AUTOINPUT - adding inputs from cells"]
  1405. ["AUTOINSERTLISP" (describe-function 'verilog-auto-insert-lisp)
  1406. :help "Help on AUTOINSERTLISP - insert text from a lisp function"]
  1407. ["AUTOINSERTLAST" (describe-function 'verilog-auto-insert-last)
  1408. :help "Help on AUTOINSERTLISPLAST - insert text from a lisp function"]
  1409. ["AUTOINST" (describe-function 'verilog-auto-inst)
  1410. :help "Help on AUTOINST - adding pins for cells"]
  1411. ["AUTOINST (.*)" (describe-function 'verilog-auto-star)
  1412. :help "Help on expanding Verilog-2001 .* pins"]
  1413. ["AUTOINSTPARAM" (describe-function 'verilog-auto-inst-param)
  1414. :help "Help on AUTOINSTPARAM - adding parameter pins to cells"]
  1415. ["AUTOLOGIC" (describe-function 'verilog-auto-logic)
  1416. :help "Help on AUTOLOGIC - declaring logic signals"]
  1417. ["AUTOOUTPUT" (describe-function 'verilog-auto-output)
  1418. :help "Help on AUTOOUTPUT - adding outputs from cells"]
  1419. ["AUTOOUTPUTEVERY" (describe-function 'verilog-auto-output-every)
  1420. :help "Help on AUTOOUTPUTEVERY - adding outputs of all signals"]
  1421. ["AUTOREG" (describe-function 'verilog-auto-reg)
  1422. :help "Help on AUTOREG - declaring registers for non-wires"]
  1423. ["AUTOREGINPUT" (describe-function 'verilog-auto-reg-input)
  1424. :help "Help on AUTOREGINPUT - declaring inputs for non-wires"]
  1425. ["AUTORESET" (describe-function 'verilog-auto-reset)
  1426. :help "Help on AUTORESET - resetting always blocks"]
  1427. ["AUTOSENSE or AS" (describe-function 'verilog-auto-sense)
  1428. :help "Help on AUTOSENSE - sensitivity lists for always blocks"]
  1429. ["AUTOTIEOFF" (describe-function 'verilog-auto-tieoff)
  1430. :help "Help on AUTOTIEOFF - tying off unused outputs"]
  1431. ["AUTOUNDEF" (describe-function 'verilog-auto-undef)
  1432. :help "Help on AUTOUNDEF - undefine all local defines"]
  1433. ["AUTOUNUSED" (describe-function 'verilog-auto-unused)
  1434. :help "Help on AUTOUNUSED - terminating unused inputs"]
  1435. ["AUTOWIRE" (describe-function 'verilog-auto-wire)
  1436. :help "Help on AUTOWIRE - declaring wires for cells"]
  1437. )
  1438. "----"
  1439. ["Submit bug report" verilog-submit-bug-report
  1440. :help "Submit via mail a bug report on verilog-mode.el"]
  1441. ["Version and FAQ" verilog-faq
  1442. :help "Show the current version, and where to get the FAQ etc"]
  1443. ["Customize Verilog Mode..." verilog-customize
  1444. :help "Customize variables and other settings used by Verilog-Mode"]
  1445. ["Customize Verilog Fonts & Colors" verilog-font-customize
  1446. :help "Customize fonts used by Verilog-Mode."])))
  1447. (easy-menu-define
  1448. verilog-stmt-menu verilog-mode-map "Menu for statement templates in Verilog."
  1449. (verilog-easy-menu-filter
  1450. '("Statements"
  1451. ["Header" verilog-sk-header
  1452. :help "Insert a header block at the top of file"]
  1453. ["Comment" verilog-sk-comment
  1454. :help "Insert a comment block"]
  1455. "----"
  1456. ["Module" verilog-sk-module
  1457. :help "Insert a module .. (/*AUTOARG*/);.. endmodule block"]
  1458. ["OVM Class" verilog-sk-ovm-class
  1459. :help "Insert an OVM class block"]
  1460. ["UVM Object" verilog-sk-uvm-object
  1461. :help "Insert an UVM object block"]
  1462. ["UVM Component" verilog-sk-uvm-component
  1463. :help "Insert an UVM component block"]
  1464. ["Primitive" verilog-sk-primitive
  1465. :help "Insert a primitive .. (.. );.. endprimitive block"]
  1466. "----"
  1467. ["Input" verilog-sk-input
  1468. :help "Insert an input declaration"]
  1469. ["Output" verilog-sk-output
  1470. :help "Insert an output declaration"]
  1471. ["Inout" verilog-sk-inout
  1472. :help "Insert an inout declaration"]
  1473. ["Wire" verilog-sk-wire
  1474. :help "Insert a wire declaration"]
  1475. ["Reg" verilog-sk-reg
  1476. :help "Insert a register declaration"]
  1477. ["Define thing under point as a register" verilog-sk-define-signal
  1478. :help "Define signal under point as a register at the top of the module"]
  1479. "----"
  1480. ["Initial" verilog-sk-initial
  1481. :help "Insert an initial begin .. end block"]
  1482. ["Always" verilog-sk-always
  1483. :help "Insert an always @(AS) begin .. end block"]
  1484. ["Function" verilog-sk-function
  1485. :help "Insert a function .. begin .. end endfunction block"]
  1486. ["Task" verilog-sk-task
  1487. :help "Insert a task .. begin .. end endtask block"]
  1488. ["Specify" verilog-sk-specify
  1489. :help "Insert a specify .. endspecify block"]
  1490. ["Generate" verilog-sk-generate
  1491. :help "Insert a generate .. endgenerate block"]
  1492. "----"
  1493. ["Begin" verilog-sk-begin
  1494. :help "Insert a begin .. end block"]
  1495. ["If" verilog-sk-if
  1496. :help "Insert an if (..) begin .. end block"]
  1497. ["(if) else" verilog-sk-else-if
  1498. :help "Insert an else if (..) begin .. end block"]
  1499. ["For" verilog-sk-for
  1500. :help "Insert a for (...) begin .. end block"]
  1501. ["While" verilog-sk-while
  1502. :help "Insert a while (...) begin .. end block"]
  1503. ["Fork" verilog-sk-fork
  1504. :help "Insert a fork begin .. end .. join block"]
  1505. ["Repeat" verilog-sk-repeat
  1506. :help "Insert a repeat (..) begin .. end block"]
  1507. ["Case" verilog-sk-case
  1508. :help "Insert a case block, prompting for details"]
  1509. ["Casex" verilog-sk-casex
  1510. :help "Insert a casex (...) item: begin.. end endcase block"]
  1511. ["Casez" verilog-sk-casez
  1512. :help "Insert a casez (...) item: begin.. end endcase block"])))
  1513. (defvar verilog-mode-abbrev-table nil
  1514. "Abbrev table in use in Verilog-mode buffers.")
  1515. (define-abbrev-table 'verilog-mode-abbrev-table ())
  1516. (verilog-define-abbrev verilog-mode-abbrev-table "class" "" 'verilog-sk-ovm-class)
  1517. (verilog-define-abbrev verilog-mode-abbrev-table "always" "" 'verilog-sk-always)
  1518. (verilog-define-abbrev verilog-mode-abbrev-table "begin" nil `verilog-sk-begin)
  1519. (verilog-define-abbrev verilog-mode-abbrev-table "case" "" `verilog-sk-case)
  1520. (verilog-define-abbrev verilog-mode-abbrev-table "for" "" `verilog-sk-for)
  1521. (verilog-define-abbrev verilog-mode-abbrev-table "generate" "" `verilog-sk-generate)
  1522. (verilog-define-abbrev verilog-mode-abbrev-table "initial" "" `verilog-sk-initial)
  1523. (verilog-define-abbrev verilog-mode-abbrev-table "fork" "" `verilog-sk-fork)
  1524. (verilog-define-abbrev verilog-mode-abbrev-table "module" "" `verilog-sk-module)
  1525. (verilog-define-abbrev verilog-mode-abbrev-table "primitive" "" `verilog-sk-primitive)
  1526. (verilog-define-abbrev verilog-mode-abbrev-table "repeat" "" `verilog-sk-repeat)
  1527. (verilog-define-abbrev verilog-mode-abbrev-table "specify" "" `verilog-sk-specify)
  1528. (verilog-define-abbrev verilog-mode-abbrev-table "task" "" `verilog-sk-task)
  1529. (verilog-define-abbrev verilog-mode-abbrev-table "while" "" `verilog-sk-while)
  1530. (verilog-define-abbrev verilog-mode-abbrev-table "casex" "" `verilog-sk-casex)
  1531. (verilog-define-abbrev verilog-mode-abbrev-table "casez" "" `verilog-sk-casez)
  1532. (verilog-define-abbrev verilog-mode-abbrev-table "if" "" `verilog-sk-if)
  1533. (verilog-define-abbrev verilog-mode-abbrev-table "else if" "" `verilog-sk-else-if)
  1534. (verilog-define-abbrev verilog-mode-abbrev-table "assign" "" `verilog-sk-assign)
  1535. (verilog-define-abbrev verilog-mode-abbrev-table "function" "" `verilog-sk-function)
  1536. (verilog-define-abbrev verilog-mode-abbrev-table "input" "" `verilog-sk-input)
  1537. (verilog-define-abbrev verilog-mode-abbrev-table "output" "" `verilog-sk-output)
  1538. (verilog-define-abbrev verilog-mode-abbrev-table "inout" "" `verilog-sk-inout)
  1539. (verilog-define-abbrev verilog-mode-abbrev-table "wire" "" `verilog-sk-wire)
  1540. (verilog-define-abbrev verilog-mode-abbrev-table "reg" "" `verilog-sk-reg)
  1541. ;;
  1542. ;; Macros
  1543. ;;
  1544. (defsubst verilog-within-string ()
  1545. (nth 3 (parse-partial-sexp (point-at-bol) (point))))
  1546. (defsubst verilog-string-match-fold (regexp string &optional start)
  1547. "Like `string-match', but use `verilog-case-fold'.
  1548. Return index of start of first match for REGEXP in STRING, or nil.
  1549. Matching ignores case if `verilog-case-fold' is non-nil.
  1550. If third arg START is non-nil, start search at that index in STRING."
  1551. (let ((case-fold-search verilog-case-fold))
  1552. (string-match regexp string start)))
  1553. (defsubst verilog-string-replace-matches (from-string to-string fixedcase literal string)
  1554. "Replace occurrences of FROM-STRING with TO-STRING.
  1555. FIXEDCASE and LITERAL as in `replace-match'. STRING is what to replace.
  1556. The case (verilog-string-replace-matches \"o\" \"oo\" nil nil \"foobar\")
  1557. will break, as the o's continuously replace. xa -> x works ok though."
  1558. ;; Hopefully soon to an Emacs built-in
  1559. ;; Also note \ in the replacement prevent multiple replacements; IE
  1560. ;; (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil "wire@_@")
  1561. ;; Gives "wire\([0-9]+\)_@" not "wire\([0-9]+\)_\([0-9]+\)"
  1562. (let ((start 0))
  1563. (while (string-match from-string string start)
  1564. (setq string (replace-match to-string fixedcase literal string)
  1565. start (min (length string) (+ (match-beginning 0) (length to-string)))))
  1566. string))
  1567. (defsubst verilog-string-remove-spaces (string)
  1568. "Remove spaces surrounding STRING."
  1569. (save-match-data
  1570. (setq string (verilog-string-replace-matches "^\\s-+" "" nil nil string))
  1571. (setq string (verilog-string-replace-matches "\\s-+$" "" nil nil string))
  1572. string))
  1573. (defsubst verilog-re-search-forward (REGEXP BOUND NOERROR)
  1574. ;; checkdoc-params: (REGEXP BOUND NOERROR)
  1575. "Like `re-search-forward', but skips over match in comments or strings."
  1576. (let ((mdata '(nil nil))) ; So match-end will return nil if no matches found
  1577. (while (and
  1578. (re-search-forward REGEXP BOUND NOERROR)
  1579. (setq mdata (match-data))
  1580. (and (verilog-skip-forward-comment-or-string)
  1581. (progn
  1582. (setq mdata '(nil nil))
  1583. (if BOUND
  1584. (< (point) BOUND)
  1585. t)))))
  1586. (store-match-data mdata)
  1587. (match-end 0)))
  1588. (defsubst verilog-re-search-backward (REGEXP BOUND NOERROR)
  1589. ;; checkdoc-params: (REGEXP BOUND NOERROR)
  1590. "Like `re-search-backward', but skips over match in comments or strings."
  1591. (let ((mdata '(nil nil))) ; So match-end will return nil if no matches found
  1592. (while (and
  1593. (re-search-backward REGEXP BOUND NOERROR)
  1594. (setq mdata (match-data))
  1595. (and (verilog-skip-backward-comment-or-string)
  1596. (progn
  1597. (setq mdata '(nil nil))
  1598. (if BOUND
  1599. (> (point) BOUND)
  1600. t)))))
  1601. (store-match-data mdata)
  1602. (match-end 0)))
  1603. (defsubst verilog-re-search-forward-quick (regexp bound noerror)
  1604. "Like `verilog-re-search-forward', including use of REGEXP BOUND and NOERROR,
  1605. but trashes match data and is faster for REGEXP that doesn't match often.
  1606. This uses `verilog-scan' and text properties to ignore comments,
  1607. so there may be a large up front penalty for the first search."
  1608. (let (pt)
  1609. (while (and (not pt)
  1610. (re-search-forward regexp bound noerror))
  1611. (if (verilog-inside-comment-or-string-p (match-beginning 0))
  1612. (re-search-forward "[/\"\n]" nil t) ; Only way a comment or quote can end
  1613. (setq pt (match-end 0))))
  1614. pt))
  1615. (defsubst verilog-re-search-backward-quick (regexp bound noerror)
  1616. ;; checkdoc-params: (REGEXP BOUND NOERROR)
  1617. "Like `verilog-re-search-backward', including use of REGEXP BOUND and NOERROR,
  1618. but trashes match data and is faster for REGEXP that doesn't match often.
  1619. This uses `verilog-scan' and text properties to ignore comments,
  1620. so there may be a large up front penalty for the first search."
  1621. (let (pt)
  1622. (while (and (not pt)
  1623. (re-search-backward regexp bound noerror))
  1624. (if (verilog-inside-comment-or-string-p (match-beginning 0))
  1625. (re-search-backward "[/\"]" nil t) ; Only way a comment or quote can begin
  1626. (setq pt (match-beginning 0))))
  1627. pt))
  1628. (defsubst verilog-re-search-forward-substr (substr regexp bound noerror)
  1629. "Like `re-search-forward', but first search for SUBSTR constant.
  1630. Then searched for the normal REGEXP (which contains SUBSTR), with given
  1631. BOUND and NOERROR. The REGEXP must fit within a single line.
  1632. This speeds up complicated regexp matches."
  1633. ;; Problem with overlap: search-forward BAR then FOOBARBAZ won't match.
  1634. ;; thus require matches to be on one line, and use beginning-of-line.
  1635. (let (done)
  1636. (while (and (not done)
  1637. (search-forward substr bound noerror))
  1638. (save-excursion
  1639. (beginning-of-line)
  1640. (setq done (re-search-forward regexp (point-at-eol) noerror)))
  1641. (unless (and (<= (match-beginning 0) (point))
  1642. (>= (match-end 0) (point)))
  1643. (setq done nil)))
  1644. (when done (goto-char done))
  1645. done))
  1646. ;;(verilog-re-search-forward-substr "-end" "get-end-of" nil t) ; -end (test bait)
  1647. (defsubst verilog-re-search-backward-substr (substr regexp bound noerror)
  1648. "Like `re-search-backward', but first search for SUBSTR constant.
  1649. Then searched for the normal REGEXP (which contains SUBSTR), with given
  1650. BOUND and NOERROR. The REGEXP must fit within a single line.
  1651. This speeds up complicated regexp matches."
  1652. ;; Problem with overlap: search-backward BAR then FOOBARBAZ won't match.
  1653. ;; thus require matches to be on one line, and use beginning-of-line.
  1654. (let (done)
  1655. (while (and (not done)
  1656. (search-backward substr bound noerror))
  1657. (save-excursion
  1658. (end-of-line)
  1659. (setq done (re-search-backward regexp (point-at-bol) noerror)))
  1660. (unless (and (<= (match-beginning 0) (point))
  1661. (>= (match-end 0) (point)))
  1662. (setq done nil)))
  1663. (when done (goto-char done))
  1664. done))
  1665. ;;(verilog-re-search-backward-substr "-end" "get-end-of" nil t) ; -end (test bait)
  1666. (defun verilog-delete-trailing-whitespace ()
  1667. "Delete trailing spaces or tabs, but not newlines nor linefeeds.
  1668. Also add missing final newline.
  1669. To call this from the command line, see \\[verilog-batch-diff-auto].
  1670. To call on \\[verilog-auto], set `verilog-auto-delete-trailing-whitespace'."
  1671. ;; Similar to `delete-trailing-whitespace' but that's not present in XEmacs
  1672. (save-excursion
  1673. (goto-char (point-min))
  1674. (while (re-search-forward "[ \t]+$" nil t) ; Not syntactic WS as no formfeed
  1675. (replace-match "" nil nil))
  1676. (goto-char (point-max))
  1677. (unless (bolp) (insert "\n"))))
  1678. (defvar compile-command)
  1679. (defvar create-lockfiles) ; Emacs 24
  1680. ;; compilation program
  1681. (defun verilog-set-compile-command ()
  1682. "Function to compute shell command to compile Verilog.
  1683. This reads `verilog-tool' and sets `compile-command'. This specifies the
  1684. program that executes when you type \\[compile] or
  1685. \\[verilog-auto-save-compile].
  1686. By default `verilog-tool' uses a Makefile if one exists in the
  1687. current directory. If not, it is set to the `verilog-linter',
  1688. `verilog-compiler', `verilog-coverage', `verilog-preprocessor',
  1689. or `verilog-simulator' variables, as selected with the Verilog ->
  1690. \"Choose Compilation Action\" menu.
  1691. You should set `verilog-tool' or the other variables to the path and
  1692. arguments for your Verilog simulator. For example:
  1693. \"vcs -p123 -O\"
  1694. or a string like:
  1695. \"(cd /tmp; surecov %s)\".
  1696. In the former case, the path to the current buffer is concat'ed to the
  1697. value of `verilog-tool'; in the later, the path to the current buffer is
  1698. substituted for the %s.
  1699. Where __FLAGS__ appears in the string `verilog-current-flags'
  1700. will be substituted.
  1701. Where __FILE__ appears in the string, the variable
  1702. `buffer-file-name' of the current buffer, without the directory
  1703. portion, will be substituted."
  1704. (interactive)
  1705. (cond
  1706. ((or (file-exists-p "makefile") ;If there is a makefile, use it
  1707. (file-exists-p "Makefile"))
  1708. (set (make-local-variable 'compile-command) "make "))
  1709. (t
  1710. (set (make-local-variable 'compile-command)
  1711. (if verilog-tool
  1712. (if (string-match "%s" (eval verilog-tool))
  1713. (format (eval verilog-tool) (or buffer-file-name ""))
  1714. (concat (eval verilog-tool) " " (or buffer-file-name "")))
  1715. ""))))
  1716. (verilog-modify-compile-command))
  1717. (defun verilog-expand-command (command)
  1718. "Replace meta-information in COMMAND and return it.
  1719. Where __FLAGS__ appears in the string `verilog-current-flags'
  1720. will be substituted. Where __FILE__ appears in the string, the
  1721. current buffer's file-name, without the directory portion, will
  1722. be substituted."
  1723. (setq command (verilog-string-replace-matches
  1724. ;; Note \\b only works if under verilog syntax table
  1725. "\\b__FLAGS__\\b" (verilog-current-flags)
  1726. t t command))
  1727. (setq command (verilog-string-replace-matches
  1728. "\\b__FILE__\\b" (file-name-nondirectory
  1729. (or (buffer-file-name) ""))
  1730. t t command))
  1731. command)
  1732. (defun verilog-modify-compile-command ()
  1733. "Update `compile-command' using `verilog-expand-command'."
  1734. (when (and
  1735. (stringp compile-command)
  1736. (string-match "\\b\\(__FLAGS__\\|__FILE__\\)\\b" compile-command))
  1737. (set (make-local-variable 'compile-command)
  1738. (verilog-expand-command compile-command))))
  1739. (if (featurep 'xemacs)
  1740. ;; Following code only gets called from compilation-mode-hook on XEmacs to add error handling.
  1741. (defun verilog-error-regexp-add-xemacs ()
  1742. "Teach XEmacs about verilog errors.
  1743. Called by `compilation-mode-hook'. This allows \\[next-error] to
  1744. find the errors."
  1745. (interactive)
  1746. (if (boundp 'compilation-error-regexp-systems-alist)
  1747. (if (and
  1748. (not (equal compilation-error-regexp-systems-list 'all))
  1749. (not (member compilation-error-regexp-systems-list 'verilog)))
  1750. (push 'verilog compilation-error-regexp-systems-list)))
  1751. (if (boundp 'compilation-error-regexp-alist-alist)
  1752. (if (not (assoc 'verilog compilation-error-regexp-alist-alist))
  1753. (setcdr compilation-error-regexp-alist-alist
  1754. (cons verilog-error-regexp-xemacs-alist
  1755. (cdr compilation-error-regexp-alist-alist)))))
  1756. (if (boundp 'compilation-font-lock-keywords)
  1757. (progn
  1758. (set (make-local-variable 'compilation-font-lock-keywords)
  1759. verilog-error-font-lock-keywords)
  1760. (font-lock-set-defaults)))
  1761. ;; Need to re-run compilation-error-regexp builder
  1762. (if (fboundp 'compilation-build-compilation-error-regexp-alist)
  1763. (compilation-build-compilation-error-regexp-alist))
  1764. ))
  1765. ;; Following code only gets called from compilation-mode-hook on Emacs to add error handling.
  1766. (defun verilog-error-regexp-add-emacs ()
  1767. "Tell Emacs compile that we are Verilog.
  1768. Called by `compilation-mode-hook'. This allows \\[next-error] to
  1769. find the errors."
  1770. (interactive)
  1771. (when (boundp 'compilation-error-regexp-alist-alist)
  1772. (when (not (assoc 'verilog-xl-1 compilation-error-regexp-alist-alist))
  1773. (mapcar
  1774. (lambda (item)
  1775. (push (car item) compilation-error-regexp-alist)
  1776. (push item compilation-error-regexp-alist-alist))
  1777. verilog-error-regexp-emacs-alist))))
  1778. (if (featurep 'xemacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-xemacs))
  1779. (if (featurep 'emacs) (add-hook 'compilation-mode-hook 'verilog-error-regexp-add-emacs))
  1780. (defconst verilog-compiler-directives
  1781. (eval-when-compile
  1782. '(
  1783. ;; compiler directives, from IEEE 1800-2012 section 22.1
  1784. "`__FILE__" "`__LINE" "`begin_keywords" "`celldefine" "`default_nettype"
  1785. "`define" "`else" "`elsif" "`end_keywords" "`endcelldefine" "`endif"
  1786. "`ifdef" "`ifndef" "`include" "`line" "`nounconnected_drive" "`pragma"
  1787. "`resetall" "`timescale" "`unconnected_drive" "`undef" "`undefineall"
  1788. ;; compiler directives not covered by IEEE 1800
  1789. "`case" "`default" "`endfor" "`endprotect" "`endswitch" "`endwhile" "`for"
  1790. "`format" "`if" "`let" "`protect" "`switch" "`timescale" "`time_scale"
  1791. "`while"
  1792. ))
  1793. "List of Verilog compiler directives.")
  1794. (defconst verilog-directive-re
  1795. (verilog-regexp-words verilog-compiler-directives))
  1796. (defconst verilog-directive-re-1
  1797. (concat "[ \t]*" verilog-directive-re))
  1798. (defconst verilog-directive-begin
  1799. "\\<`\\(for\\|i\\(f\\|fdef\\|fndef\\)\\|switch\\|while\\)\\>")
  1800. (defconst verilog-directive-middle
  1801. "\\<`\\(else\\|elsif\\|default\\|case\\)\\>")
  1802. (defconst verilog-directive-end
  1803. "`\\(endfor\\|endif\\|endswitch\\|endwhile\\)\\>")
  1804. (defconst verilog-ovm-begin-re
  1805. (eval-when-compile
  1806. (verilog-regexp-opt
  1807. '(
  1808. "`ovm_component_utils_begin"
  1809. "`ovm_component_param_utils_begin"
  1810. "`ovm_field_utils_begin"
  1811. "`ovm_object_utils_begin"
  1812. "`ovm_object_param_utils_begin"
  1813. "`ovm_sequence_utils_begin"
  1814. "`ovm_sequencer_utils_begin"
  1815. ) nil )))
  1816. (defconst verilog-ovm-end-re
  1817. (eval-when-compile
  1818. (verilog-regexp-opt
  1819. '(
  1820. "`ovm_component_utils_end"
  1821. "`ovm_field_utils_end"
  1822. "`ovm_object_utils_end"
  1823. "`ovm_sequence_utils_end"
  1824. "`ovm_sequencer_utils_end"
  1825. ) nil )))
  1826. (defconst verilog-uvm-begin-re
  1827. (eval-when-compile
  1828. (verilog-regexp-opt
  1829. '(
  1830. "`uvm_component_utils_begin"
  1831. "`uvm_component_param_utils_begin"
  1832. "`uvm_field_utils_begin"
  1833. "`uvm_object_utils_begin"
  1834. "`uvm_object_param_utils_begin"
  1835. "`uvm_sequence_utils_begin"
  1836. "`uvm_sequencer_utils_begin"
  1837. ) nil )))
  1838. (defconst verilog-uvm-end-re
  1839. (eval-when-compile
  1840. (verilog-regexp-opt
  1841. '(
  1842. "`uvm_component_utils_end"
  1843. "`uvm_field_utils_end"
  1844. "`uvm_object_utils_end"
  1845. "`uvm_sequence_utils_end"
  1846. "`uvm_sequencer_utils_end"
  1847. ) nil )))
  1848. (defconst verilog-vmm-begin-re
  1849. (eval-when-compile
  1850. (verilog-regexp-opt
  1851. '(
  1852. "`vmm_data_member_begin"
  1853. "`vmm_env_member_begin"
  1854. "`vmm_scenario_member_begin"
  1855. "`vmm_subenv_member_begin"
  1856. "`vmm_xactor_member_begin"
  1857. ) nil ) ) )
  1858. (defconst verilog-vmm-end-re
  1859. (eval-when-compile
  1860. (verilog-regexp-opt
  1861. '(
  1862. "`vmm_data_member_end"
  1863. "`vmm_env_member_end"
  1864. "`vmm_scenario_member_end"
  1865. "`vmm_subenv_member_end"
  1866. "`vmm_xactor_member_end"
  1867. ) nil ) ) )
  1868. (defconst verilog-vmm-statement-re
  1869. (eval-when-compile
  1870. (verilog-regexp-opt
  1871. '(
  1872. "`vmm_\\(data\\|env\\|scenario\\|subenv\\|xactor\\)_member_\\(scalar\\|string\\|enum\\|vmm_data\\|channel\\|xactor\\|subenv\\|user_defined\\)\\(_array\\)?"
  1873. ;; "`vmm_xactor_member_enum_array"
  1874. ;; "`vmm_xactor_member_scalar_array"
  1875. ;; "`vmm_xactor_member_scalar"
  1876. ) nil )))
  1877. (defconst verilog-ovm-statement-re
  1878. (eval-when-compile
  1879. (verilog-regexp-opt
  1880. '(
  1881. ;; Statements
  1882. "`DUT_ERROR"
  1883. "`MESSAGE"
  1884. "`dut_error"
  1885. "`message"
  1886. "`ovm_analysis_imp_decl"
  1887. "`ovm_blocking_get_imp_decl"
  1888. "`ovm_blocking_get_peek_imp_decl"
  1889. "`ovm_blocking_master_imp_decl"
  1890. "`ovm_blocking_peek_imp_decl"
  1891. "`ovm_blocking_put_imp_decl"
  1892. "`ovm_blocking_slave_imp_decl"
  1893. "`ovm_blocking_transport_imp_decl"
  1894. "`ovm_component_registry"
  1895. "`ovm_component_registry_param"
  1896. "`ovm_component_utils"
  1897. "`ovm_create"
  1898. "`ovm_create_seq"
  1899. "`ovm_declare_sequence_lib"
  1900. "`ovm_do"
  1901. "`ovm_do_seq"
  1902. "`ovm_do_seq_with"
  1903. "`ovm_do_with"
  1904. "`ovm_error"
  1905. "`ovm_fatal"
  1906. "`ovm_field_aa_int_byte"
  1907. "`ovm_field_aa_int_byte_unsigned"
  1908. "`ovm_field_aa_int_int"
  1909. "`ovm_field_aa_int_int_unsigned"
  1910. "`ovm_field_aa_int_integer"
  1911. "`ovm_field_aa_int_integer_unsigned"
  1912. "`ovm_field_aa_int_key"
  1913. "`ovm_field_aa_int_longint"
  1914. "`ovm_field_aa_int_longint_unsigned"
  1915. "`ovm_field_aa_int_shortint"
  1916. "`ovm_field_aa_int_shortint_unsigned"
  1917. "`ovm_field_aa_int_string"
  1918. "`ovm_field_aa_object_int"
  1919. "`ovm_field_aa_object_string"
  1920. "`ovm_field_aa_string_int"
  1921. "`ovm_field_aa_string_string"
  1922. "`ovm_field_array_int"
  1923. "`ovm_field_array_object"
  1924. "`ovm_field_array_string"
  1925. "`ovm_field_enum"
  1926. "`ovm_field_event"
  1927. "`ovm_field_int"
  1928. "`ovm_field_object"
  1929. "`ovm_field_queue_int"
  1930. "`ovm_field_queue_object"
  1931. "`ovm_field_queue_string"
  1932. "`ovm_field_sarray_int"
  1933. "`ovm_field_string"
  1934. "`ovm_field_utils"
  1935. "`ovm_file"
  1936. "`ovm_get_imp_decl"
  1937. "`ovm_get_peek_imp_decl"
  1938. "`ovm_info"
  1939. "`ovm_info1"
  1940. "`ovm_info2"
  1941. "`ovm_info3"
  1942. "`ovm_info4"
  1943. "`ovm_line"
  1944. "`ovm_master_imp_decl"
  1945. "`ovm_msg_detail"
  1946. "`ovm_non_blocking_transport_imp_decl"
  1947. "`ovm_nonblocking_get_imp_decl"
  1948. "`ovm_nonblocking_get_peek_imp_decl"
  1949. "`ovm_nonblocking_master_imp_decl"
  1950. "`ovm_nonblocking_peek_imp_decl"
  1951. "`ovm_nonblocking_put_imp_decl"
  1952. "`ovm_nonblocking_slave_imp_decl"
  1953. "`ovm_object_registry"
  1954. "`ovm_object_registry_param"
  1955. "`ovm_object_utils"
  1956. "`ovm_peek_imp_decl"
  1957. "`ovm_phase_func_decl"
  1958. "`ovm_phase_task_decl"
  1959. "`ovm_print_aa_int_object"
  1960. "`ovm_print_aa_string_int"
  1961. "`ovm_print_aa_string_object"
  1962. "`ovm_print_aa_string_string"
  1963. "`ovm_print_array_int"
  1964. "`ovm_print_array_object"
  1965. "`ovm_print_array_string"
  1966. "`ovm_print_object_queue"
  1967. "`ovm_print_queue_int"
  1968. "`ovm_print_string_queue"
  1969. "`ovm_put_imp_decl"
  1970. "`ovm_rand_send"
  1971. "`ovm_rand_send_with"
  1972. "`ovm_send"
  1973. "`ovm_sequence_utils"
  1974. "`ovm_slave_imp_decl"
  1975. "`ovm_transport_imp_decl"
  1976. "`ovm_update_sequence_lib"
  1977. "`ovm_update_sequence_lib_and_item"
  1978. "`ovm_warning"
  1979. "`static_dut_error"
  1980. "`static_message") nil )))
  1981. (defconst verilog-uvm-statement-re
  1982. (eval-when-compile
  1983. (verilog-regexp-opt
  1984. '(
  1985. ;; Statements
  1986. "`uvm_analysis_imp_decl"
  1987. "`uvm_blocking_get_imp_decl"
  1988. "`uvm_blocking_get_peek_imp_decl"
  1989. "`uvm_blocking_master_imp_decl"
  1990. "`uvm_blocking_peek_imp_decl"
  1991. "`uvm_blocking_put_imp_decl"
  1992. "`uvm_blocking_slave_imp_decl"
  1993. "`uvm_blocking_transport_imp_decl"
  1994. "`uvm_component_param_utils"
  1995. "`uvm_component_registry"
  1996. "`uvm_component_registry_param"
  1997. "`uvm_component_utils"
  1998. "`uvm_create"
  1999. "`uvm_create_on"
  2000. "`uvm_create_seq" ; Undocumented in 1.1
  2001. "`uvm_declare_p_sequencer"
  2002. "`uvm_declare_sequence_lib" ; Deprecated in 1.1
  2003. "`uvm_do"
  2004. "`uvm_do_callbacks"
  2005. "`uvm_do_callbacks_exit_on"
  2006. "`uvm_do_obj_callbacks"
  2007. "`uvm_do_obj_callbacks_exit_on"
  2008. "`uvm_do_on"
  2009. "`uvm_do_on_pri"
  2010. "`uvm_do_on_pri_with"
  2011. "`uvm_do_on_with"
  2012. "`uvm_do_pri"
  2013. "`uvm_do_pri_with"
  2014. "`uvm_do_seq" ; Undocumented in 1.1
  2015. "`uvm_do_seq_with" ; Undocumented in 1.1
  2016. "`uvm_do_with"
  2017. "`uvm_error"
  2018. "`uvm_error_context"
  2019. "`uvm_fatal"
  2020. "`uvm_fatal_context"
  2021. "`uvm_field_aa_int_byte"
  2022. "`uvm_field_aa_int_byte_unsigned"
  2023. "`uvm_field_aa_int_enum"
  2024. "`uvm_field_aa_int_int"
  2025. "`uvm_field_aa_int_int_unsigned"
  2026. "`uvm_field_aa_int_integer"
  2027. "`uvm_field_aa_int_integer_unsigned"
  2028. "`uvm_field_aa_int_key"
  2029. "`uvm_field_aa_int_longint"
  2030. "`uvm_field_aa_int_longint_unsigned"
  2031. "`uvm_field_aa_int_shortint"
  2032. "`uvm_field_aa_int_shortint_unsigned"
  2033. "`uvm_field_aa_int_string"
  2034. "`uvm_field_aa_object_int"
  2035. "`uvm_field_aa_object_string"
  2036. "`uvm_field_aa_string_int"
  2037. "`uvm_field_aa_string_string"
  2038. "`uvm_field_array_enum"
  2039. "`uvm_field_array_int"
  2040. "`uvm_field_array_object"
  2041. "`uvm_field_array_string"
  2042. "`uvm_field_enum"
  2043. "`uvm_field_event"
  2044. "`uvm_field_int"
  2045. "`uvm_field_object"
  2046. "`uvm_field_queue_enum"
  2047. "`uvm_field_queue_int"
  2048. "`uvm_field_queue_object"
  2049. "`uvm_field_queue_string"
  2050. "`uvm_field_real"
  2051. "`uvm_field_sarray_enum"
  2052. "`uvm_field_sarray_int"
  2053. "`uvm_field_sarray_object"
  2054. "`uvm_field_sarray_string"
  2055. "`uvm_field_string"
  2056. "`uvm_field_utils"
  2057. "`uvm_file" ; Undocumented in 1.1, use `__FILE__
  2058. "`uvm_get_imp_decl"
  2059. "`uvm_get_peek_imp_decl"
  2060. "`uvm_info"
  2061. "`uvm_info_context"
  2062. "`uvm_line" ; Undocumented in 1.1, use `__LINE__
  2063. "`uvm_master_imp_decl"
  2064. "`uvm_non_blocking_transport_imp_decl" ; Deprecated in 1.1
  2065. "`uvm_nonblocking_get_imp_decl"
  2066. "`uvm_nonblocking_get_peek_imp_decl"
  2067. "`uvm_nonblocking_master_imp_decl"
  2068. "`uvm_nonblocking_peek_imp_decl"
  2069. "`uvm_nonblocking_put_imp_decl"
  2070. "`uvm_nonblocking_slave_imp_decl"
  2071. "`uvm_nonblocking_transport_imp_decl"
  2072. "`uvm_object_param_utils"
  2073. "`uvm_object_registry"
  2074. "`uvm_object_registry_param" ; Undocumented in 1.1
  2075. "`uvm_object_utils"
  2076. "`uvm_pack_array"
  2077. "`uvm_pack_arrayN"
  2078. "`uvm_pack_enum"
  2079. "`uvm_pack_enumN"
  2080. "`uvm_pack_int"
  2081. "`uvm_pack_intN"
  2082. "`uvm_pack_queue"
  2083. "`uvm_pack_queueN"
  2084. "`uvm_pack_real"
  2085. "`uvm_pack_sarray"
  2086. "`uvm_pack_sarrayN"
  2087. "`uvm_pack_string"
  2088. "`uvm_peek_imp_decl"
  2089. "`uvm_put_imp_decl"
  2090. "`uvm_rand_send"
  2091. "`uvm_rand_send_pri"
  2092. "`uvm_rand_send_pri_with"
  2093. "`uvm_rand_send_with"
  2094. "`uvm_record_attribute"
  2095. "`uvm_record_field"
  2096. "`uvm_register_cb"
  2097. "`uvm_send"
  2098. "`uvm_send_pri"
  2099. "`uvm_sequence_utils" ; Deprecated in 1.1
  2100. "`uvm_set_super_type"
  2101. "`uvm_slave_imp_decl"
  2102. "`uvm_transport_imp_decl"
  2103. "`uvm_unpack_array"
  2104. "`uvm_unpack_arrayN"
  2105. "`uvm_unpack_enum"
  2106. "`uvm_unpack_enumN"
  2107. "`uvm_unpack_int"
  2108. "`uvm_unpack_intN"
  2109. "`uvm_unpack_queue"
  2110. "`uvm_unpack_queueN"
  2111. "`uvm_unpack_real"
  2112. "`uvm_unpack_sarray"
  2113. "`uvm_unpack_sarrayN"
  2114. "`uvm_unpack_string"
  2115. "`uvm_update_sequence_lib" ; Deprecated in 1.1
  2116. "`uvm_update_sequence_lib_and_item" ; Deprecated in 1.1
  2117. "`uvm_warning"
  2118. "`uvm_warning_context") nil )))
  2119. ;;
  2120. ;; Regular expressions used to calculate indent, etc.
  2121. ;;
  2122. (defconst verilog-symbol-re "\\<[a-zA-Z_][a-zA-Z_0-9.]*\\>")
  2123. ;; Want to match
  2124. ;; aa :
  2125. ;; aa,bb :
  2126. ;; a[34:32] :
  2127. ;; a,
  2128. ;; b :
  2129. (defconst verilog-assignment-operator-re
  2130. (eval-when-compile
  2131. (verilog-regexp-opt
  2132. `(
  2133. ;; blocking assignment_operator
  2134. "=" "+=" "-=" "*=" "/=" "%=" "&=" "|=" "^=" "<<=" ">>=" "<<<=" ">>>="
  2135. ;; non blocking assignment operator
  2136. "<="
  2137. ;; comparison
  2138. "==" "!=" "===" "!==" "<=" ">=" "==?" "!=?" "<->"
  2139. ;; event_trigger
  2140. "->" "->>"
  2141. ;; property_expr
  2142. "|->" "|=>" "#-#" "#=#"
  2143. ;; distribution weighting
  2144. ":=" ":/"
  2145. ) 't
  2146. )))
  2147. (defconst verilog-assignment-operation-re
  2148. (concat
  2149. ;; "\\(^\\s-*[A-Za-z0-9_]+\\(\\[\\([A-Za-z0-9_]+\\)\\]\\)*\\s-*\\)"
  2150. ;; "\\(^\\s-*[^=<>+-*/%&|^:\\s-]+[^=<>+-*/%&|^\n]*?\\)"
  2151. "\\(^.*?\\)" "\\B" verilog-assignment-operator-re "\\B" ))
  2152. (defconst verilog-label-re (concat verilog-symbol-re "\\s-*:\\s-*"))
  2153. (defconst verilog-property-re
  2154. (concat "\\(" verilog-label-re "\\)?"
  2155. ;; "\\(assert\\|assume\\|cover\\)\\s-+property\\>"
  2156. "\\(\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(assert\\)"))
  2157. (defconst verilog-no-indent-begin-re
  2158. (eval-when-compile
  2159. (verilog-regexp-words
  2160. '("always" "always_comb" "always_ff" "always_latch" "initial" "final" ; procedural blocks
  2161. "if" "else" ; conditional statements
  2162. "while" "for" "foreach" "repeat" "do" "forever" )))) ; loop statements
  2163. (defconst verilog-ends-re
  2164. ;; Parenthesis indicate type of keyword found
  2165. (concat
  2166. "\\(\\<else\\>\\)\\|" ; 1
  2167. "\\(\\<if\\>\\)\\|" ; 2
  2168. "\\(\\<assert\\>\\)\\|" ; 3
  2169. "\\(\\<end\\>\\)\\|" ; 3.1
  2170. "\\(\\<endcase\\>\\)\\|" ; 4
  2171. "\\(\\<endfunction\\>\\)\\|" ; 5
  2172. "\\(\\<endtask\\>\\)\\|" ; 6
  2173. "\\(\\<endspecify\\>\\)\\|" ; 7
  2174. "\\(\\<endtable\\>\\)\\|" ; 8
  2175. "\\(\\<endgenerate\\>\\)\\|" ; 9
  2176. "\\(\\<join\\(_any\\|_none\\)?\\>\\)\\|" ; 10
  2177. "\\(\\<endclass\\>\\)\\|" ; 11
  2178. "\\(\\<endgroup\\>\\)\\|" ; 12
  2179. ;; VMM
  2180. "\\(\\<`vmm_data_member_end\\>\\)\\|"
  2181. "\\(\\<`vmm_env_member_end\\>\\)\\|"
  2182. "\\(\\<`vmm_scenario_member_end\\>\\)\\|"
  2183. "\\(\\<`vmm_subenv_member_end\\>\\)\\|"
  2184. "\\(\\<`vmm_xactor_member_end\\>\\)\\|"
  2185. ;; OVM
  2186. "\\(\\<`ovm_component_utils_end\\>\\)\\|"
  2187. "\\(\\<`ovm_field_utils_end\\>\\)\\|"
  2188. "\\(\\<`ovm_object_utils_end\\>\\)\\|"
  2189. "\\(\\<`ovm_sequence_utils_end\\>\\)\\|"
  2190. "\\(\\<`ovm_sequencer_utils_end\\>\\)"
  2191. ;; UVM
  2192. "\\(\\<`uvm_component_utils_end\\>\\)\\|"
  2193. "\\(\\<`uvm_field_utils_end\\>\\)\\|"
  2194. "\\(\\<`uvm_object_utils_end\\>\\)\\|"
  2195. "\\(\\<`uvm_sequence_utils_end\\>\\)\\|"
  2196. "\\(\\<`uvm_sequencer_utils_end\\>\\)"
  2197. ))
  2198. (defconst verilog-auto-end-comment-lines-re
  2199. ;; Matches to names in this list cause auto-end-commenting
  2200. (concat "\\("
  2201. verilog-directive-re "\\)\\|\\("
  2202. (eval-when-compile
  2203. (verilog-regexp-words
  2204. `( "begin"
  2205. "else"
  2206. "end"
  2207. "endcase"
  2208. "endclass"
  2209. "endclocking"
  2210. "endgroup"
  2211. "endfunction"
  2212. "endmodule"
  2213. "endprogram"
  2214. "endprimitive"
  2215. "endinterface"
  2216. "endpackage"
  2217. "endsequence"
  2218. "endproperty"
  2219. "endspecify"
  2220. "endtable"
  2221. "endtask"
  2222. "join"
  2223. "join_any"
  2224. "join_none"
  2225. "module"
  2226. "macromodule"
  2227. "primitive"
  2228. "interface"
  2229. "package")))
  2230. "\\)"))
  2231. ;; NOTE: verilog-leap-to-head expects that verilog-end-block-re and
  2232. ;; verilog-end-block-ordered-re matches exactly the same strings.
  2233. (defconst verilog-end-block-ordered-re
  2234. ;; Parenthesis indicate type of keyword found
  2235. (concat "\\(\\<endcase\\>\\)\\|" ; 1
  2236. "\\(\\<end\\>\\)\\|" ; 2
  2237. "\\(\\<end" ; 3, but not used
  2238. "\\(" ; 4, but not used
  2239. "\\(function\\)\\|" ; 5
  2240. "\\(task\\)\\|" ; 6
  2241. "\\(module\\)\\|" ; 7
  2242. "\\(primitive\\)\\|" ; 8
  2243. "\\(interface\\)\\|" ; 9
  2244. "\\(package\\)\\|" ; 10
  2245. "\\(class\\)\\|" ; 11
  2246. "\\(group\\)\\|" ; 12
  2247. "\\(program\\)\\|" ; 13
  2248. "\\(sequence\\)\\|" ; 14
  2249. "\\(clocking\\)\\|" ; 15
  2250. "\\(property\\)\\|" ; 16
  2251. "\\)\\>\\)"))
  2252. (defconst verilog-end-block-re
  2253. (eval-when-compile
  2254. (verilog-regexp-words
  2255. `("end" ; closes begin
  2256. "endcase" ; closes any of case, casex casez or randcase
  2257. "join" "join_any" "join_none" ; closes fork
  2258. "endclass"
  2259. "endtable"
  2260. "endspecify"
  2261. "endfunction"
  2262. "endgenerate"
  2263. "endtask"
  2264. "endgroup"
  2265. "endproperty"
  2266. "endinterface"
  2267. "endpackage"
  2268. "endprogram"
  2269. "endsequence"
  2270. "endclocking"
  2271. ;; OVM
  2272. "`ovm_component_utils_end"
  2273. "`ovm_field_utils_end"
  2274. "`ovm_object_utils_end"
  2275. "`ovm_sequence_utils_end"
  2276. "`ovm_sequencer_utils_end"
  2277. ;; UVM
  2278. "`uvm_component_utils_end"
  2279. "`uvm_field_utils_end"
  2280. "`uvm_object_utils_end"
  2281. "`uvm_sequence_utils_end"
  2282. "`uvm_sequencer_utils_end"
  2283. ;; VMM
  2284. "`vmm_data_member_end"
  2285. "`vmm_env_member_end"
  2286. "`vmm_scenario_member_end"
  2287. "`vmm_subenv_member_end"
  2288. "`vmm_xactor_member_end"
  2289. ))))
  2290. (defconst verilog-endcomment-reason-re
  2291. ;; Parenthesis indicate type of keyword found
  2292. (concat
  2293. "\\(\\<begin\\>\\)\\|" ; 1
  2294. "\\(\\<else\\>\\)\\|" ; 2
  2295. "\\(\\<end\\>\\s-+\\<else\\>\\)\\|" ; 3
  2296. "\\(\\<always\\(?:_ff\\)?\\>\\(?:[ \t]*@\\)\\)\\|" ; 4 (matches always or always_ff w/ @...)
  2297. "\\(\\<always\\(?:_comb\\|_latch\\)?\\>\\)\\|" ; 5 (matches always, always_comb, always_latch w/o @...)
  2298. "\\(\\<fork\\>\\)\\|" ; 7
  2299. "\\(\\<if\\>\\)\\|"
  2300. verilog-property-re "\\|"
  2301. "\\(\\(" verilog-label-re "\\)?\\<assert\\>\\)\\|"
  2302. "\\(\\<clocking\\>\\)\\|"
  2303. "\\(\\<task\\>\\)\\|"
  2304. "\\(\\<function\\>\\)\\|"
  2305. "\\(\\<initial\\>\\)\\|"
  2306. "\\(\\<interface\\>\\)\\|"
  2307. "\\(\\<package\\>\\)\\|"
  2308. "\\(\\<final\\>\\)\\|"
  2309. "\\(@\\)\\|"
  2310. "\\(\\<while\\>\\)\\|\\(\\<do\\>\\)\\|"
  2311. "\\(\\<for\\(ever\\|each\\)?\\>\\)\\|"
  2312. "\\(\\<repeat\\>\\)\\|\\(\\<wait\\>\\)\\|"
  2313. "#"))
  2314. (defconst verilog-named-block-re "begin[ \t]*:")
  2315. ;; These words begin a block which can occur inside a module which should be indented,
  2316. ;; and closed with the respective word from the end-block list
  2317. (defconst verilog-beg-block-re
  2318. (eval-when-compile
  2319. (verilog-regexp-words
  2320. `("begin"
  2321. "case" "casex" "casez" "randcase"
  2322. "clocking"
  2323. "generate"
  2324. "fork"
  2325. "function"
  2326. "property"
  2327. "specify"
  2328. "table"
  2329. "task"
  2330. ;; OVM
  2331. "`ovm_component_utils_begin"
  2332. "`ovm_component_param_utils_begin"
  2333. "`ovm_field_utils_begin"
  2334. "`ovm_object_utils_begin"
  2335. "`ovm_object_param_utils_begin"
  2336. "`ovm_sequence_utils_begin"
  2337. "`ovm_sequencer_utils_begin"
  2338. ;; UVM
  2339. "`uvm_component_utils_begin"
  2340. "`uvm_component_param_utils_begin"
  2341. "`uvm_field_utils_begin"
  2342. "`uvm_object_utils_begin"
  2343. "`uvm_object_param_utils_begin"
  2344. "`uvm_sequence_utils_begin"
  2345. "`uvm_sequencer_utils_begin"
  2346. ;; VMM
  2347. "`vmm_data_member_begin"
  2348. "`vmm_env_member_begin"
  2349. "`vmm_scenario_member_begin"
  2350. "`vmm_subenv_member_begin"
  2351. "`vmm_xactor_member_begin"
  2352. ))))
  2353. ;; These are the same words, in a specific order in the regular
  2354. ;; expression so that matching will work nicely for
  2355. ;; verilog-forward-sexp and verilog-calc-indent
  2356. (defconst verilog-beg-block-re-ordered
  2357. ( concat "\\(\\<begin\\>\\)" ;1
  2358. "\\|\\(\\<randcase\\>\\|\\(\\<unique0?\\s-+\\|priority\\s-+\\)?case[xz]?\\>\\)" ; 2,3
  2359. "\\|\\(\\(\\<disable\\>\\s-+\\|\\<wait\\>\\s-+\\)?fork\\>\\)" ;4,5
  2360. "\\|\\(\\<class\\>\\)" ;6
  2361. "\\|\\(\\<table\\>\\)" ;7
  2362. "\\|\\(\\<specify\\>\\)" ;8
  2363. "\\|\\(\\<function\\>\\)" ;9
  2364. "\\|\\(\\(?:\\<\\(?:virtual\\|protected\\|static\\)\\>\\s-+\\)*\\<function\\>\\)" ;10
  2365. "\\|\\(\\<task\\>\\)" ;11
  2366. "\\|\\(\\(?:\\<\\(?:virtual\\|protected\\|static\\)\\>\\s-+\\)*\\<task\\>\\)" ;12
  2367. "\\|\\(\\<generate\\>\\)" ;13
  2368. "\\|\\(\\<covergroup\\>\\)" ;14
  2369. "\\|\\(\\(?:\\(?:\\<cover\\>\\s-+\\)\\|\\(?:\\<assert\\>\\s-+\\)\\)*\\<property\\>\\)" ;15
  2370. "\\|\\(\\<\\(?:rand\\)?sequence\\>\\)" ;16
  2371. "\\|\\(\\<clocking\\>\\)" ;17
  2372. "\\|\\(\\<`[ou]vm_[a-z_]+_begin\\>\\)" ;18
  2373. "\\|\\(\\<`vmm_[a-z_]+_member_begin\\>\\)"
  2374. ;;
  2375. ))
  2376. (defconst verilog-end-block-ordered-rry
  2377. [ "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
  2378. "\\(\\<randcase\\>\\|\\<case[xz]?\\>\\)\\|\\(\\<endcase\\>\\)"
  2379. "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)"
  2380. "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)"
  2381. "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)"
  2382. "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)"
  2383. "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)"
  2384. "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)"
  2385. "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)"
  2386. "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)"
  2387. "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)"
  2388. "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)"
  2389. "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)"
  2390. ] )
  2391. (defconst verilog-nameable-item-re
  2392. (eval-when-compile
  2393. (verilog-regexp-words
  2394. `("begin"
  2395. "fork"
  2396. "join" "join_any" "join_none"
  2397. "end"
  2398. "endcase"
  2399. "endchecker"
  2400. "endclass"
  2401. "endclocking"
  2402. "endconfig"
  2403. "endfunction"
  2404. "endgenerate"
  2405. "endgroup"
  2406. "endmodule"
  2407. "endprimitive"
  2408. "endinterface"
  2409. "endpackage"
  2410. "endprogram"
  2411. "endproperty"
  2412. "endsequence"
  2413. "endspecify"
  2414. "endtable"
  2415. "endtask" )
  2416. )))
  2417. (defconst verilog-declaration-opener
  2418. (eval-when-compile
  2419. (verilog-regexp-words
  2420. `("module" "begin" "task" "function"))))
  2421. (defconst verilog-declaration-prefix-re
  2422. (eval-when-compile
  2423. (verilog-regexp-words
  2424. `(
  2425. ;; port direction
  2426. "inout" "input" "output" "ref"
  2427. ;; changeableness
  2428. "const" "static" "protected" "local"
  2429. ;; parameters
  2430. "localparam" "parameter" "var"
  2431. ;; type creation
  2432. "typedef"
  2433. ))))
  2434. (defconst verilog-declaration-core-re
  2435. (eval-when-compile
  2436. (verilog-regexp-words
  2437. `(
  2438. ;; port direction (by themselves)
  2439. "inout" "input" "output"
  2440. ;; integer_atom_type
  2441. "byte" "shortint" "int" "longint" "integer" "time"
  2442. ;; integer_vector_type
  2443. "bit" "logic" "reg"
  2444. ;; non_integer_type
  2445. "shortreal" "real" "realtime"
  2446. ;; net_type
  2447. "supply0" "supply1" "tri" "triand" "trior" "trireg" "tri0" "tri1" "uwire" "wire" "wand" "wor"
  2448. ;; misc
  2449. "string" "event" "chandle" "virtual" "enum" "genvar"
  2450. "struct" "union"
  2451. ;; builtin classes
  2452. "mailbox" "semaphore"
  2453. ))))
  2454. (defconst verilog-declaration-re
  2455. (concat "\\(" verilog-declaration-prefix-re "\\s-*\\)?" verilog-declaration-core-re))
  2456. (defconst verilog-range-re "\\(\\[[^]]*\\]\\s-*\\)+")
  2457. (defconst verilog-optional-signed-re "\\s-*\\(\\(un\\)?signed\\)?")
  2458. (defconst verilog-optional-signed-range-re
  2459. (concat
  2460. "\\s-*\\(\\<\\(reg\\|wire\\)\\>\\s-*\\)?\\(\\<\\(un\\)?signed\\>\\s-*\\)?\\(" verilog-range-re "\\)?"))
  2461. (defconst verilog-macroexp-re "`\\sw+")
  2462. (defconst verilog-delay-re "#\\s-*\\(\\([0-9_]+\\('s?[hdxbo][0-9a-fA-F_xz]+\\)?\\)\\|\\(([^()]*)\\)\\|\\(\\sw+\\)\\)")
  2463. (defconst verilog-declaration-re-2-no-macro
  2464. (concat "\\s-*" verilog-declaration-re
  2465. "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
  2466. "\\)?"))
  2467. (defconst verilog-declaration-re-2-macro
  2468. (concat "\\s-*" verilog-declaration-re
  2469. "\\s-*\\(\\(" verilog-optional-signed-range-re "\\)\\|\\(" verilog-delay-re "\\)"
  2470. "\\|\\(" verilog-macroexp-re "\\)"
  2471. "\\)?"))
  2472. (defconst verilog-declaration-re-1-macro
  2473. (concat "^" verilog-declaration-re-2-macro))
  2474. (defconst verilog-declaration-re-1-no-macro (concat "^" verilog-declaration-re-2-no-macro))
  2475. (defconst verilog-defun-re
  2476. (eval-when-compile (verilog-regexp-words `("macromodule" "module" "class" "program" "interface" "package" "primitive" "config"))))
  2477. (defconst verilog-end-defun-re
  2478. (eval-when-compile (verilog-regexp-words `("endmodule" "endclass" "endprogram" "endinterface" "endpackage" "endprimitive" "endconfig"))))
  2479. (defconst verilog-zero-indent-re
  2480. (concat verilog-defun-re "\\|" verilog-end-defun-re))
  2481. (defconst verilog-inst-comment-re
  2482. (eval-when-compile (verilog-regexp-words `("Outputs" "Inouts" "Inputs" "Interfaces" "Interfaced"))))
  2483. (defconst verilog-behavioral-block-beg-re
  2484. (eval-when-compile (verilog-regexp-words `("initial" "final" "always" "always_comb" "always_latch" "always_ff"
  2485. "function" "task"))))
  2486. (defconst verilog-coverpoint-re "\\w+\\s*:\\s*\\(coverpoint\\|cross\\constraint\\)" )
  2487. (defconst verilog-in-constraint-re ; keywords legal in constraint blocks starting a statement/block
  2488. (eval-when-compile (verilog-regexp-words `("if" "else" "solve" "foreach"))))
  2489. (defconst verilog-indent-re
  2490. (eval-when-compile
  2491. (verilog-regexp-words
  2492. `(
  2493. "{"
  2494. "always" "always_latch" "always_ff" "always_comb"
  2495. "begin" "end"
  2496. ;; "unique" "priority"
  2497. "case" "casex" "casez" "randcase" "endcase"
  2498. "class" "endclass"
  2499. "clocking" "endclocking"
  2500. "config" "endconfig"
  2501. "covergroup" "endgroup"
  2502. "fork" "join" "join_any" "join_none"
  2503. "function" "endfunction"
  2504. "final"
  2505. "generate" "endgenerate"
  2506. "initial"
  2507. "interface" "endinterface"
  2508. "module" "macromodule" "endmodule"
  2509. "package" "endpackage"
  2510. "primitive" "endprimitive"
  2511. "program" "endprogram"
  2512. "property" "endproperty"
  2513. "sequence" "randsequence" "endsequence"
  2514. "specify" "endspecify"
  2515. "table" "endtable"
  2516. "task" "endtask"
  2517. "virtual"
  2518. "`case"
  2519. "`default"
  2520. "`define" "`undef"
  2521. "`if" "`ifdef" "`ifndef" "`else" "`elsif" "`endif"
  2522. "`while" "`endwhile"
  2523. "`for" "`endfor"
  2524. "`format"
  2525. "`include"
  2526. "`let"
  2527. "`protect" "`endprotect"
  2528. "`switch" "`endswitch"
  2529. "`timescale"
  2530. "`time_scale"
  2531. ;; OVM Begin tokens
  2532. "`ovm_component_utils_begin"
  2533. "`ovm_component_param_utils_begin"
  2534. "`ovm_field_utils_begin"
  2535. "`ovm_object_utils_begin"
  2536. "`ovm_object_param_utils_begin"
  2537. "`ovm_sequence_utils_begin"
  2538. "`ovm_sequencer_utils_begin"
  2539. ;; OVM End tokens
  2540. "`ovm_component_utils_end"
  2541. "`ovm_field_utils_end"
  2542. "`ovm_object_utils_end"
  2543. "`ovm_sequence_utils_end"
  2544. "`ovm_sequencer_utils_end"
  2545. ;; UVM Begin tokens
  2546. "`uvm_component_utils_begin"
  2547. "`uvm_component_param_utils_begin"
  2548. "`uvm_field_utils_begin"
  2549. "`uvm_object_utils_begin"
  2550. "`uvm_object_param_utils_begin"
  2551. "`uvm_sequence_utils_begin"
  2552. "`uvm_sequencer_utils_begin"
  2553. ;; UVM End tokens
  2554. "`uvm_component_utils_end" ; Typo in spec, it's not uvm_component_end
  2555. "`uvm_field_utils_end"
  2556. "`uvm_object_utils_end"
  2557. "`uvm_sequence_utils_end"
  2558. "`uvm_sequencer_utils_end"
  2559. ;; VMM Begin tokens
  2560. "`vmm_data_member_begin"
  2561. "`vmm_env_member_begin"
  2562. "`vmm_scenario_member_begin"
  2563. "`vmm_subenv_member_begin"
  2564. "`vmm_xactor_member_begin"
  2565. ;; VMM End tokens
  2566. "`vmm_data_member_end"
  2567. "`vmm_env_member_end"
  2568. "`vmm_scenario_member_end"
  2569. "`vmm_subenv_member_end"
  2570. "`vmm_xactor_member_end"
  2571. ))))
  2572. (defconst verilog-defun-level-not-generate-re
  2573. (eval-when-compile
  2574. (verilog-regexp-words
  2575. `( "module" "macromodule" "primitive" "class" "program"
  2576. "interface" "package" "config"))))
  2577. (defconst verilog-defun-level-re
  2578. (eval-when-compile
  2579. (verilog-regexp-words
  2580. (append
  2581. `( "module" "macromodule" "primitive" "class" "program"
  2582. "interface" "package" "config")
  2583. `( "initial" "final" "always" "always_comb" "always_ff"
  2584. "always_latch" "endtask" "endfunction" )))))
  2585. (defconst verilog-defun-level-generate-only-re
  2586. (eval-when-compile
  2587. (verilog-regexp-words
  2588. `( "initial" "final" "always" "always_comb" "always_ff"
  2589. "always_latch" "endtask" "endfunction" ))))
  2590. (defconst verilog-cpp-level-re
  2591. (eval-when-compile
  2592. (verilog-regexp-words
  2593. `(
  2594. "endmodule" "endprimitive" "endinterface" "endpackage" "endprogram" "endclass"
  2595. ))))
  2596. (defconst verilog-dpi-import-export-re
  2597. (eval-when-compile
  2598. "\\(\\<\\(import\\|export\\)\\>\\s-+\"DPI\\(-C\\)?\"\\s-+\\(\\<\\(context\\|pure\\)\\>\\s-+\\)?\\([A-Za-z_][A-Za-z0-9_]*\\s-*=\\s-*\\)?\\<\\(function\\|task\\)\\>\\)"
  2599. ))
  2600. (defconst verilog-default-clocking-re "\\<default\\s-+clocking\\>")
  2601. (defconst verilog-disable-fork-re "\\(disable\\|wait\\)\\s-+fork\\>")
  2602. (defconst verilog-extended-case-re "\\(\\(unique0?\\s-+\\|priority\\s-+\\)?case[xz]?\\|randcase\\)")
  2603. (defconst verilog-extended-complete-re
  2604. ;; verilog-beg-of-statement also looks backward one token to extend this match
  2605. (concat "\\(\\(\\<extern\\s-+\\|\\<\\(\\<\\(pure\\|context\\)\\>\\s-+\\)?virtual\\s-+\\|\\<protected\\s-+\\|\\<static\\s-+\\)*\\(\\<function\\>\\|\\<task\\>\\)\\)"
  2606. "\\|\\(\\(\\<typedef\\>\\s-+\\)*\\(\\<struct\\>\\|\\<union\\>\\|\\<class\\>\\)\\)"
  2607. "\\|\\(\\(\\<\\(import\\|export\\)\\>\\s-+\\)?\\(\"DPI\\(-C\\)?\"\\s-+\\)?\\(\\<\\(pure\\|context\\)\\>\\s-+\\)?\\([A-Za-z_][A-Za-z0-9_]*\\s-*=\\s-*\\)?\\(function\\>\\|task\\>\\)\\)"
  2608. "\\|" verilog-extended-case-re ))
  2609. (defconst verilog-basic-complete-re
  2610. (eval-when-compile
  2611. (verilog-regexp-words
  2612. `(
  2613. "always" "assign" "always_latch" "always_ff" "always_comb" "constraint"
  2614. "import" "initial" "final" "module" "macromodule" "repeat" "randcase" "while"
  2615. "if" "for" "forever" "foreach" "else" "parameter" "do" "localparam" "assert"
  2616. ))))
  2617. (defconst verilog-complete-reg
  2618. (concat
  2619. verilog-extended-complete-re "\\|\\(" verilog-basic-complete-re "\\)"))
  2620. (defconst verilog-end-statement-re
  2621. (concat "\\(" verilog-beg-block-re "\\)\\|\\("
  2622. verilog-end-block-re "\\)"))
  2623. (defconst verilog-endcase-re
  2624. (concat verilog-extended-case-re "\\|"
  2625. "\\(endcase\\)\\|"
  2626. verilog-defun-re
  2627. ))
  2628. (defconst verilog-exclude-str-start "/* -----\\/----- EXCLUDED -----\\/-----"
  2629. "String used to mark beginning of excluded text.")
  2630. (defconst verilog-exclude-str-end " -----/\\----- EXCLUDED -----/\\----- */"
  2631. "String used to mark end of excluded text.")
  2632. (defconst verilog-preprocessor-re
  2633. (eval-when-compile
  2634. (concat
  2635. ;; single words
  2636. "\\(?:"
  2637. (verilog-regexp-words
  2638. `("`__FILE__"
  2639. "`__LINE__"
  2640. "`celldefine"
  2641. "`else"
  2642. "`end_keywords"
  2643. "`endcelldefine"
  2644. "`endif"
  2645. "`nounconnected_drive"
  2646. "`resetall"
  2647. "`unconnected_drive"
  2648. "`undefineall"))
  2649. "\\)\\|\\(?:"
  2650. ;; two words: i.e. `ifdef DEFINE
  2651. "\\<\\(`elsif\\|`ifn?def\\|`undef\\|`default_nettype\\|`begin_keywords\\)\\>\\s-"
  2652. "\\)\\|\\(?:"
  2653. ;; `line number "filename" level
  2654. "\\<\\(`line\\)\\>\\s-+[0-9]+\\s-+\"[^\"]+\"\\s-+[012]"
  2655. "\\)\\|\\(?:"
  2656. ;;`include "file" or `include <file>
  2657. "\\<\\(`include\\)\\>\\s-+\\(?:\"[^\"]+\"\\|<[^>]+>\\)"
  2658. "\\)\\|\\(?:"
  2659. ;; `pragma <stuff> (no mention in IEEE 1800-2012 that pragma can span multiple lines
  2660. "\\<\\(`pragma\\)\\>\\s-+.+$"
  2661. "\\)\\|\\(?:"
  2662. ;; `timescale time_unit / time_precision
  2663. "\\<\\(`timescale\\)\\>\\s-+10\\{0,2\\}\\s-*[munpf]?s\\s-*\\/\\s-*10\\{0,2\\}\\s-*[munpf]?s"
  2664. "\\)\\|\\(?:"
  2665. ;; `define and `if can span multiple lines if line ends in '\'. NOTE: `if is not IEEE 1800-2012
  2666. ;; from http://www.emacswiki.org/emacs/MultilineRegexp
  2667. (concat "\\<\\(`define\\|`if\\)\\>" ; directive
  2668. "\\s-+" ; separator
  2669. "\\(?:.*?\\(?:\n.*\\)*?\\)" ; definition: to end of line, then maybe more lines (excludes any trailing \n)
  2670. "\\(?:\n\\s-*\n\\|\\'\\)") ; blank line or EOF
  2671. "\\)\\|\\(?:"
  2672. ;; `<macro>() : i.e. `uvm_info(a,b,c) or any other pre-defined macro
  2673. ;; Since parameters inside the macro can have parentheses, and
  2674. ;; the macro can span multiple lines, just look for the opening
  2675. ;; parentheses and then continue to the end of the first
  2676. ;; non-escaped EOL
  2677. (concat "\\<`\\w+\\>\\s-*("
  2678. "\\(?:.*?\\(?:\n.*\\)*?\\)" ; definition: to end of line, then maybe more lines (excludes any trailing \n)
  2679. "\\(?:\n\\s-*\n\\|\\'\\)") ; blank line or EOF
  2680. "\\)"
  2681. )))
  2682. (defconst verilog-keywords
  2683. (append verilog-compiler-directives
  2684. '(
  2685. "after" "alias" "always" "always_comb" "always_ff" "always_latch" "and"
  2686. "assert" "assign" "assume" "automatic" "before" "begin" "bind"
  2687. "bins" "binsof" "bit" "break" "buf" "bufif0" "bufif1" "byte"
  2688. "case" "casex" "casez" "cell" "chandle" "class" "clocking" "cmos"
  2689. "config" "const" "constraint" "context" "continue" "cover"
  2690. "covergroup" "coverpoint" "cross" "deassign" "default" "defparam"
  2691. "design" "disable" "dist" "do" "edge" "else" "end" "endcase"
  2692. "endclass" "endclocking" "endconfig" "endfunction" "endgenerate"
  2693. "endgroup" "endinterface" "endmodule" "endpackage" "endprimitive"
  2694. "endprogram" "endproperty" "endspecify" "endsequence" "endtable"
  2695. "endtask" "enum" "event" "expect" "export" "extends" "extern"
  2696. "final" "first_match" "for" "force" "foreach" "forever" "fork"
  2697. "forkjoin" "function" "generate" "genvar" "highz0" "highz1" "if"
  2698. "iff" "ifnone" "ignore_bins" "illegal_bins" "import" "incdir"
  2699. "include" "initial" "inout" "input" "inside" "instance" "int"
  2700. "integer" "interface" "intersect" "join" "join_any" "join_none"
  2701. "large" "liblist" "library" "local" "localparam" "logic"
  2702. "longint" "macromodule" "mailbox" "matches" "medium" "modport" "module"
  2703. "nand" "negedge" "new" "nmos" "nor" "noshowcancelled" "not"
  2704. "notif0" "notif1" "null" "or" "output" "package" "packed"
  2705. "parameter" "pmos" "posedge" "primitive" "priority" "program"
  2706. "property" "protected" "pull0" "pull1" "pulldown" "pullup"
  2707. "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
  2708. "randcase" "randsequence" "rcmos" "real" "realtime" "ref" "reg"
  2709. "release" "repeat" "return" "rnmos" "rpmos" "rtran" "rtranif0"
  2710. "rtranif1" "scalared" "semaphore" "sequence" "shortint" "shortreal"
  2711. "showcancelled" "signed" "small" "solve" "specify" "specparam"
  2712. "static" "string" "strong0" "strong1" "struct" "super" "supply0"
  2713. "supply1" "table" "tagged" "task" "this" "throughout" "time"
  2714. "timeprecision" "timeunit" "tran" "tranif0" "tranif1" "tri"
  2715. "tri0" "tri1" "triand" "trior" "trireg" "type" "typedef" "union"
  2716. "unique" "unsigned" "use" "uwire" "var" "vectored" "virtual" "void"
  2717. "wait" "wait_order" "wand" "weak0" "weak1" "while" "wildcard"
  2718. "wire" "with" "within" "wor" "xnor" "xor"
  2719. ;; 1800-2009
  2720. "accept_on" "checker" "endchecker" "eventually" "global" "implies"
  2721. "let" "nexttime" "reject_on" "restrict" "s_always" "s_eventually"
  2722. "s_nexttime" "s_until" "s_until_with" "strong" "sync_accept_on"
  2723. "sync_reject_on" "unique0" "until" "until_with" "untyped" "weak"
  2724. ;; 1800-2012
  2725. "implements" "interconnect" "nettype" "soft"
  2726. ))
  2727. "List of Verilog keywords.")
  2728. (defconst verilog-comment-start-regexp "//\\|/\\*"
  2729. "Dual comment value for `comment-start-regexp'.")
  2730. (defvar verilog-mode-syntax-table
  2731. (let ((table (make-syntax-table)))
  2732. ;; Populate the syntax TABLE.
  2733. (modify-syntax-entry ?\\ "\\" table)
  2734. (modify-syntax-entry ?+ "." table)
  2735. (modify-syntax-entry ?- "." table)
  2736. (modify-syntax-entry ?= "." table)
  2737. (modify-syntax-entry ?% "." table)
  2738. (modify-syntax-entry ?< "." table)
  2739. (modify-syntax-entry ?> "." table)
  2740. (modify-syntax-entry ?& "." table)
  2741. (modify-syntax-entry ?| "." table)
  2742. (modify-syntax-entry ?` "w" table) ; ` is part of definition symbols in Verilog
  2743. (modify-syntax-entry ?_ "w" table)
  2744. (modify-syntax-entry ?\' "." table)
  2745. ;; Set up TABLE to handle block and line style comments.
  2746. (if (featurep 'xemacs)
  2747. (progn
  2748. ;; XEmacs (formerly Lucid) has the best implementation
  2749. (modify-syntax-entry ?/ ". 1456" table)
  2750. (modify-syntax-entry ?* ". 23" table)
  2751. (modify-syntax-entry ?\n "> b" table))
  2752. ;; Emacs does things differently, but we can work with it
  2753. (modify-syntax-entry ?/ ". 124b" table)
  2754. (modify-syntax-entry ?* ". 23" table)
  2755. (modify-syntax-entry ?\n "> b" table))
  2756. table)
  2757. "Syntax table used in Verilog mode buffers.")
  2758. (defvar verilog-font-lock-keywords nil
  2759. "Default highlighting for Verilog mode.")
  2760. (defvar verilog-font-lock-keywords-1 nil
  2761. "Subdued level highlighting for Verilog mode.")
  2762. (defvar verilog-font-lock-keywords-2 nil
  2763. "Medium level highlighting for Verilog mode.
  2764. See also `verilog-font-lock-extra-types'.")
  2765. (defvar verilog-font-lock-keywords-3 nil
  2766. "Gaudy level highlighting for Verilog mode.
  2767. See also `verilog-font-lock-extra-types'.")
  2768. (defvar verilog-font-lock-translate-off-face
  2769. 'verilog-font-lock-translate-off-face
  2770. "Font to use for translated off regions.")
  2771. (defface verilog-font-lock-translate-off-face
  2772. '((((class color)
  2773. (background light))
  2774. (:background "gray90" :italic t ))
  2775. (((class color)
  2776. (background dark))
  2777. (:background "gray10" :italic t ))
  2778. (((class grayscale) (background light))
  2779. (:foreground "DimGray" :italic t))
  2780. (((class grayscale) (background dark))
  2781. (:foreground "LightGray" :italic t))
  2782. (t (:italis t)))
  2783. "Font lock mode face used to background highlight translate-off regions."
  2784. :group 'font-lock-highlighting-faces)
  2785. (defvar verilog-font-lock-p1800-face
  2786. 'verilog-font-lock-p1800-face
  2787. "Font to use for p1800 keywords.")
  2788. (defface verilog-font-lock-p1800-face
  2789. '((((class color)
  2790. (background light))
  2791. (:foreground "DarkOrange3" :bold t ))
  2792. (((class color)
  2793. (background dark))
  2794. (:foreground "orange1" :bold t ))
  2795. (t (:italic t)))
  2796. "Font lock mode face used to highlight P1800 keywords."
  2797. :group 'font-lock-highlighting-faces)
  2798. (defvar verilog-font-lock-ams-face
  2799. 'verilog-font-lock-ams-face
  2800. "Font to use for Analog/Mixed Signal keywords.")
  2801. (defface verilog-font-lock-ams-face
  2802. '((((class color)
  2803. (background light))
  2804. (:foreground "Purple" :bold t ))
  2805. (((class color)
  2806. (background dark))
  2807. (:foreground "orange1" :bold t ))
  2808. (t (:italic t)))
  2809. "Font lock mode face used to highlight AMS keywords."
  2810. :group 'font-lock-highlighting-faces)
  2811. (defvar verilog-font-lock-grouping-keywords-face
  2812. 'verilog-font-lock-grouping-keywords-face
  2813. "Font to use for Verilog Grouping Keywords (such as begin..end).")
  2814. (defface verilog-font-lock-grouping-keywords-face
  2815. '((((class color)
  2816. (background light))
  2817. (:foreground "Purple" :bold t ))
  2818. (((class color)
  2819. (background dark))
  2820. (:foreground "orange1" :bold t ))
  2821. (t (:italic t)))
  2822. "Font lock mode face used to highlight verilog grouping keywords."
  2823. :group 'font-lock-highlighting-faces)
  2824. (let* ((verilog-type-font-keywords
  2825. (eval-when-compile
  2826. (verilog-regexp-opt
  2827. '(
  2828. "and" "bit" "buf" "bufif0" "bufif1" "cmos" "defparam"
  2829. "event" "genvar" "inout" "input" "integer" "localparam"
  2830. "logic" "mailbox" "nand" "nmos" "nor" "not" "notif0" "notif1" "or"
  2831. "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
  2832. "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran"
  2833. "rtranif0" "rtranif1" "semaphore" "signed" "struct" "supply"
  2834. "supply0" "supply1" "time" "tran" "tranif0" "tranif1"
  2835. "tri" "tri0" "tri1" "triand" "trior" "trireg" "typedef"
  2836. "uwire" "vectored" "wand" "wire" "wor" "xnor" "xor"
  2837. ) nil )))
  2838. (verilog-pragma-keywords
  2839. (eval-when-compile
  2840. (verilog-regexp-opt
  2841. '("surefire" "auto" "synopsys" "rtl_synthesis" "verilint" "leda" "0in"
  2842. ) nil )))
  2843. (verilog-1800-2005-keywords
  2844. (eval-when-compile
  2845. (verilog-regexp-opt
  2846. '("alias" "assert" "assume" "automatic" "before" "bind"
  2847. "bins" "binsof" "break" "byte" "cell" "chandle" "class"
  2848. "clocking" "config" "const" "constraint" "context" "continue"
  2849. "cover" "covergroup" "coverpoint" "cross" "deassign" "design"
  2850. "dist" "do" "edge" "endclass" "endclocking" "endconfig"
  2851. "endgroup" "endprogram" "endproperty" "endsequence" "enum"
  2852. "expect" "export" "extends" "extern" "first_match" "foreach"
  2853. "forkjoin" "genvar" "highz0" "highz1" "ifnone" "ignore_bins"
  2854. "illegal_bins" "import" "incdir" "include" "inside" "instance"
  2855. "int" "intersect" "large" "liblist" "library" "local" "longint"
  2856. "matches" "medium" "modport" "new" "noshowcancelled" "null"
  2857. "packed" "program" "property" "protected" "pull0" "pull1"
  2858. "pulsestyle_onevent" "pulsestyle_ondetect" "pure" "rand" "randc"
  2859. "randcase" "randsequence" "ref" "release" "return" "scalared"
  2860. "sequence" "shortint" "shortreal" "showcancelled" "small" "solve"
  2861. "specparam" "static" "string" "strong0" "strong1" "struct"
  2862. "super" "tagged" "this" "throughout" "timeprecision" "timeunit"
  2863. "type" "union" "unsigned" "use" "var" "virtual" "void"
  2864. "wait_order" "weak0" "weak1" "wildcard" "with" "within"
  2865. ) nil )))
  2866. (verilog-1800-2009-keywords
  2867. (eval-when-compile
  2868. (verilog-regexp-opt
  2869. '("accept_on" "checker" "endchecker" "eventually" "global"
  2870. "implies" "let" "nexttime" "reject_on" "restrict" "s_always"
  2871. "s_eventually" "s_nexttime" "s_until" "s_until_with" "strong"
  2872. "sync_accept_on" "sync_reject_on" "unique0" "until"
  2873. "until_with" "untyped" "weak" ) nil )))
  2874. (verilog-1800-2012-keywords
  2875. (eval-when-compile
  2876. (verilog-regexp-opt
  2877. '("implements" "interconnect" "nettype" "soft" ) nil )))
  2878. (verilog-ams-keywords
  2879. (eval-when-compile
  2880. (verilog-regexp-opt
  2881. '("above" "abs" "absdelay" "acos" "acosh" "ac_stim"
  2882. "aliasparam" "analog" "analysis" "asin" "asinh" "atan" "atan2" "atanh"
  2883. "branch" "ceil" "connectmodule" "connectrules" "cos" "cosh" "ddt"
  2884. "ddx" "discipline" "driver_update" "enddiscipline" "endconnectrules"
  2885. "endnature" "endparamset" "exclude" "exp" "final_step" "flicker_noise"
  2886. "floor" "flow" "from" "ground" "hypot" "idt" "idtmod" "inf"
  2887. "initial_step" "laplace_nd" "laplace_np" "laplace_zd" "laplace_zp"
  2888. "last_crossing" "limexp" "ln" "log" "max" "min" "nature"
  2889. "net_resolution" "noise_table" "paramset" "potential" "pow" "sin"
  2890. "sinh" "slew" "sqrt" "tan" "tanh" "timer" "transition" "white_noise"
  2891. "wreal" "zi_nd" "zi_np" "zi_zd" ) nil )))
  2892. (verilog-font-keywords
  2893. (eval-when-compile
  2894. (verilog-regexp-opt
  2895. '(
  2896. "assign" "case" "casex" "casez" "randcase" "deassign"
  2897. "default" "disable" "else" "endcase" "endfunction"
  2898. "endgenerate" "endinterface" "endmodule" "endprimitive"
  2899. "endspecify" "endtable" "endtask" "final" "for" "force" "return" "break"
  2900. "continue" "forever" "fork" "function" "generate" "if" "iff" "initial"
  2901. "interface" "join" "join_any" "join_none" "macromodule" "module" "negedge"
  2902. "package" "endpackage" "always" "always_comb" "always_ff"
  2903. "always_latch" "posedge" "primitive" "priority" "release"
  2904. "repeat" "specify" "table" "task" "unique" "wait" "while"
  2905. "class" "program" "endclass" "endprogram"
  2906. ) nil )))
  2907. (verilog-font-grouping-keywords
  2908. (eval-when-compile
  2909. (verilog-regexp-opt
  2910. '( "begin" "end" ) nil ))))
  2911. (setq verilog-font-lock-keywords
  2912. (list
  2913. ;; Fontify all builtin keywords
  2914. (concat "\\<\\(" verilog-font-keywords "\\|"
  2915. ;; And user/system tasks and functions
  2916. "\\$[a-zA-Z][a-zA-Z0-9_\\$]*"
  2917. "\\)\\>")
  2918. ;; Fontify all types
  2919. (if verilog-highlight-grouping-keywords
  2920. (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
  2921. 'verilog-font-lock-grouping-keywords-face)
  2922. (cons (concat "\\<\\(" verilog-font-grouping-keywords "\\)\\>")
  2923. 'font-lock-type-face))
  2924. (cons (concat "\\<\\(" verilog-type-font-keywords "\\)\\>")
  2925. 'font-lock-type-face)
  2926. ;; Fontify IEEE-1800-2005 keywords appropriately
  2927. (if verilog-highlight-p1800-keywords
  2928. (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
  2929. 'verilog-font-lock-p1800-face)
  2930. (cons (concat "\\<\\(" verilog-1800-2005-keywords "\\)\\>")
  2931. 'font-lock-type-face))
  2932. ;; Fontify IEEE-1800-2009 keywords appropriately
  2933. (if verilog-highlight-p1800-keywords
  2934. (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
  2935. 'verilog-font-lock-p1800-face)
  2936. (cons (concat "\\<\\(" verilog-1800-2009-keywords "\\)\\>")
  2937. 'font-lock-type-face))
  2938. ;; Fontify IEEE-1800-2012 keywords appropriately
  2939. (if verilog-highlight-p1800-keywords
  2940. (cons (concat "\\<\\(" verilog-1800-2012-keywords "\\)\\>")
  2941. 'verilog-font-lock-p1800-face)
  2942. (cons (concat "\\<\\(" verilog-1800-2012-keywords "\\)\\>")
  2943. 'font-lock-type-face))
  2944. ;; Fontify Verilog-AMS keywords
  2945. (cons (concat "\\<\\(" verilog-ams-keywords "\\)\\>")
  2946. 'verilog-font-lock-ams-face)))
  2947. (setq verilog-font-lock-keywords-1
  2948. (append verilog-font-lock-keywords
  2949. (list
  2950. ;; Fontify module definitions
  2951. (list
  2952. "\\<\\(\\(macro\\)?module\\|primitive\\|class\\|program\\|interface\\|package\\|task\\)\\>\\s-*\\(\\sw+\\)"
  2953. '(1 font-lock-keyword-face)
  2954. '(3 font-lock-function-name-face 'prepend))
  2955. ;; Fontify function definitions
  2956. (list
  2957. (concat "\\<function\\>\\s-+\\(integer\\|real\\(time\\)?\\|time\\)\\s-+\\(\\sw+\\)" )
  2958. '(1 font-lock-keyword-face)
  2959. '(3 font-lock-constant-face prepend))
  2960. '("\\<function\\>\\s-+\\(\\[[^]]+\\]\\)\\s-+\\(\\sw+\\)"
  2961. (1 font-lock-keyword-face)
  2962. (2 font-lock-constant-face append))
  2963. '("\\<function\\>\\s-+\\(\\sw+\\)"
  2964. 1 'font-lock-constant-face append))))
  2965. (setq verilog-font-lock-keywords-2
  2966. (append verilog-font-lock-keywords-1
  2967. (list
  2968. ;; Fontify pragmas
  2969. (concat "\\(//\\s-*\\(" verilog-pragma-keywords "\\)\\s-.*\\)")
  2970. ;; Fontify escaped names
  2971. '("\\(\\\\\\S-*\\s-\\)" 0 font-lock-function-name-face)
  2972. ;; Fontify macro definitions/ uses
  2973. '("`\\s-*[A-Za-z][A-Za-z0-9_]*" 0 (if (boundp 'font-lock-preprocessor-face)
  2974. 'font-lock-preprocessor-face
  2975. 'font-lock-type-face))
  2976. ;; Fontify delays/numbers
  2977. '("\\(@\\)\\|\\([ \t\n\f\r]#\\s-*\\(\\([0-9_.]+\\('s?[hdxbo][0-9a-fA-F_xz]*\\)?\\)\\|\\(([^()]+)\\|\\sw+\\)\\)\\)"
  2978. 0 font-lock-type-face append)
  2979. ;; Fontify property/sequence cycle delays - these start with '##'
  2980. '("\\(##\\(\\sw+\\|\\[[^]]+\\]\\)\\)"
  2981. 0 font-lock-type-face append)
  2982. ;; Fontify instantiation names
  2983. '("\\([A-Za-z][A-Za-z0-9_]*\\)\\s-*(" 1 font-lock-function-name-face)
  2984. )))
  2985. (setq verilog-font-lock-keywords-3
  2986. (append verilog-font-lock-keywords-2
  2987. (when verilog-highlight-translate-off
  2988. (list
  2989. ;; Fontify things in translate off regions
  2990. '(verilog-match-translate-off
  2991. (0 'verilog-font-lock-translate-off-face prepend))
  2992. )))))
  2993. ;;
  2994. ;; Buffer state preservation
  2995. (defmacro verilog-save-buffer-state (&rest body)
  2996. "Execute BODY forms, saving state around insignificant change.
  2997. Changes in text properties like `face' or `syntax-table' are
  2998. considered insignificant. This macro allows text properties to
  2999. be changed, even in a read-only buffer.
  3000. A change is considered significant if it affects the buffer text
  3001. in any way that isn't completely restored again. Any
  3002. user-visible changes to the buffer must not be within a
  3003. `verilog-save-buffer-state'."
  3004. `(let ((inhibit-point-motion-hooks t)
  3005. (verilog-no-change-functions t))
  3006. ,(if (fboundp 'with-silent-modifications)
  3007. `(with-silent-modifications ,@body)
  3008. ;; Backward compatible version of with-silent-modifications
  3009. `(let* ((modified (buffer-modified-p))
  3010. (buffer-undo-list t)
  3011. (inhibit-read-only t)
  3012. (inhibit-modification-hooks t)
  3013. ;; XEmacs ignores inhibit-modification-hooks.
  3014. before-change-functions after-change-functions
  3015. deactivate-mark
  3016. buffer-file-name ; Prevent primitives checking
  3017. buffer-file-truename) ; for file modification
  3018. (unwind-protect
  3019. (progn ,@body)
  3020. (and (not modified)
  3021. (buffer-modified-p)
  3022. (verilog-restore-buffer-modified-p nil)))))))
  3023. (defvar verilog-save-font-mod-hooked nil
  3024. "Local variable when inside a `verilog-save-font-no-change-functions' block.")
  3025. (make-variable-buffer-local 'verilog-save-font-mod-hooked)
  3026. (defmacro verilog-save-font-no-change-functions (&rest body)
  3027. "Execute BODY forms, disabling all change hooks in BODY.
  3028. Includes temporary disabling of `font-lock' to restore the buffer
  3029. to full text form for parsing. Additional actions may be specified with
  3030. `verilog-before-save-font-hook' and `verilog-after-save-font-hook'.
  3031. For insignificant changes, see instead `verilog-save-buffer-state'."
  3032. `(if verilog-save-font-mod-hooked ; Short-circuit a recursive call
  3033. (progn ,@body)
  3034. ;; Before version 20, match-string with font-lock returns a
  3035. ;; vector that is not equal to the string. IE if on "input"
  3036. ;; nil==(equal "input" (progn (looking-at "input") (match-string 0)))
  3037. ;; Therefore we must remove and restore font-lock mode
  3038. (verilog-run-hooks 'verilog-before-save-font-hook)
  3039. (let* ((verilog-save-font-mod-hooked (- (point-max) (point-min)))
  3040. ;; Significant speed savings with no font-lock properties
  3041. (fontlocked (when (and (boundp 'font-lock-mode) font-lock-mode)
  3042. (font-lock-mode 0)
  3043. t)))
  3044. (run-hook-with-args 'before-change-functions (point-min) (point-max))
  3045. (unwind-protect
  3046. ;; Must inhibit and restore hooks before restoring font-lock
  3047. (let* ((inhibit-point-motion-hooks t)
  3048. (inhibit-modification-hooks t)
  3049. (verilog-no-change-functions t)
  3050. ;; XEmacs and pre-Emacs 21 ignore inhibit-modification-hooks.
  3051. before-change-functions after-change-functions)
  3052. (progn ,@body))
  3053. ;; Unwind forms
  3054. (run-hook-with-args 'after-change-functions (point-min) (point-max)
  3055. verilog-save-font-mod-hooked) ; old length
  3056. (when fontlocked (font-lock-mode t))
  3057. (verilog-run-hooks 'verilog-after-save-font-hook)))))
  3058. ;;
  3059. ;; Comment detection and caching
  3060. (defvar verilog-scan-cache-preserving nil
  3061. "If true, the specified buffer's comment properties are static.
  3062. Buffer changes will be ignored. See `verilog-inside-comment-or-string-p'
  3063. and `verilog-scan'.")
  3064. (defvar verilog-scan-cache-tick nil
  3065. "Modification tick at which `verilog-scan' was last completed.")
  3066. (make-variable-buffer-local 'verilog-scan-cache-tick)
  3067. (defun verilog-scan-cache-flush ()
  3068. "Flush the `verilog-scan' cache."
  3069. (setq verilog-scan-cache-tick nil))
  3070. (defun verilog-scan-cache-ok-p ()
  3071. "Return t if the scan cache is up to date."
  3072. (or (and verilog-scan-cache-preserving
  3073. (eq verilog-scan-cache-preserving (current-buffer))
  3074. verilog-scan-cache-tick)
  3075. (equal verilog-scan-cache-tick (buffer-chars-modified-tick))))
  3076. (defmacro verilog-save-scan-cache (&rest body)
  3077. "Execute the BODY forms, allowing scan cache preservation within BODY.
  3078. This requires that insertions must use `verilog-insert'."
  3079. ;; If the buffer is out of date, trash it, as we'll not check later the tick
  3080. ;; Note this must work properly if there's multiple layers of calls
  3081. ;; to verilog-save-scan-cache even with differing ticks.
  3082. `(progn
  3083. (unless (verilog-scan-cache-ok-p) ; Must be before let
  3084. (setq verilog-scan-cache-tick nil))
  3085. (let* ((verilog-scan-cache-preserving (current-buffer)))
  3086. (progn ,@body))))
  3087. (defun verilog-scan-region (beg end)
  3088. "Parse between BEG and END for `verilog-inside-comment-or-string-p'.
  3089. This creates v-cmts properties where comments are in force."
  3090. ;; Why properties and not overlays? Overlays have much slower non O(1)
  3091. ;; lookup times.
  3092. ;; This function is warm - called on every verilog-insert
  3093. (save-excursion
  3094. (save-match-data
  3095. (verilog-save-buffer-state
  3096. (let (pt)
  3097. (goto-char beg)
  3098. (while (< (point) end)
  3099. (cond ((looking-at "//")
  3100. (setq pt (point))
  3101. (or (search-forward "\n" end t)
  3102. (goto-char end))
  3103. ;; "1+": The leading // or /* itself isn't considered as
  3104. ;; being "inside" the comment, so that a (search-backward)
  3105. ;; that lands at the start of the // won't mis-indicate
  3106. ;; it's inside a comment. Also otherwise it would be
  3107. ;; hard to find a commented out /*AS*/ vs one that isn't
  3108. (put-text-property (1+ pt) (point) 'v-cmts t))
  3109. ((looking-at "/\\*")
  3110. (setq pt (point))
  3111. (or (search-forward "*/" end t)
  3112. ;; No error - let later code indicate it so we can
  3113. ;; use inside functions on-the-fly
  3114. ;;(error "%s: Unmatched /* */, at char %d"
  3115. ;; (verilog-point-text) (point))
  3116. (goto-char end))
  3117. (put-text-property (1+ pt) (point) 'v-cmts t))
  3118. ((looking-at "\"")
  3119. (setq pt (point))
  3120. (or (re-search-forward "[^\\]\"" end t) ; don't forward-char first, since we look for a non backslash first
  3121. ;; No error - let later code indicate it so we can
  3122. (goto-char end))
  3123. (put-text-property (1+ pt) (point) 'v-cmts t))
  3124. (t
  3125. (forward-char 1)
  3126. (if (re-search-forward "[/\"]" end t)
  3127. (backward-char 1)
  3128. (goto-char end))))))))))
  3129. (defun verilog-scan ()
  3130. "Parse the buffer, marking all comments with properties.
  3131. Also assumes any text inserted since `verilog-scan-cache-tick'
  3132. either is ok to parse as a non-comment, or `verilog-insert' was used."
  3133. ;; See also `verilog-scan-debug' and `verilog-scan-and-debug'
  3134. (unless (verilog-scan-cache-ok-p)
  3135. (save-excursion
  3136. (verilog-save-buffer-state
  3137. (when verilog-debug
  3138. (message "Scanning %s cache=%s cachetick=%S tick=%S" (current-buffer)
  3139. verilog-scan-cache-preserving verilog-scan-cache-tick
  3140. (buffer-chars-modified-tick)))
  3141. (remove-text-properties (point-min) (point-max) '(v-cmts nil))
  3142. (verilog-scan-region (point-min) (point-max))
  3143. (setq verilog-scan-cache-tick (buffer-chars-modified-tick))
  3144. (when verilog-debug (message "Scanning... done"))))))
  3145. (defun verilog-scan-debug ()
  3146. "For debugging, show with display face results of `verilog-scan'."
  3147. (font-lock-mode 0)
  3148. ;;(if dbg (setq dbg (concat dbg (format "verilog-scan-debug\n"))))
  3149. (save-excursion
  3150. (goto-char (point-min))
  3151. (remove-text-properties (point-min) (point-max) '(face nil))
  3152. (while (not (eobp))
  3153. (cond ((get-text-property (point) 'v-cmts)
  3154. (put-text-property (point) (1+ (point)) `face 'underline)
  3155. ;;(if dbg (setq dbg (concat dbg (format " v-cmts at %S\n" (point)))))
  3156. (forward-char 1))
  3157. (t
  3158. (goto-char (or (next-property-change (point)) (point-max))))))))
  3159. (defun verilog-scan-and-debug ()
  3160. "For debugging, run `verilog-scan' and `verilog-scan-debug'."
  3161. (let (verilog-scan-cache-preserving
  3162. verilog-scan-cache-tick)
  3163. (goto-char (point-min))
  3164. (verilog-scan)
  3165. (verilog-scan-debug)))
  3166. (defun verilog-inside-comment-or-string-p (&optional pos)
  3167. "Check if optional point POS is inside a comment.
  3168. This may require a slow pre-parse of the buffer with `verilog-scan'
  3169. to establish comment properties on all text."
  3170. ;; This function is very hot
  3171. (verilog-scan)
  3172. (if pos
  3173. (and (>= pos (point-min))
  3174. (get-text-property pos 'v-cmts))
  3175. (get-text-property (point) 'v-cmts)))
  3176. (defun verilog-insert (&rest stuff)
  3177. "Insert STUFF arguments, tracking for `verilog-inside-comment-or-string-p'.
  3178. Any insert that includes a comment must have the entire comment
  3179. inserted using a single call to `verilog-insert'."
  3180. (let ((pt (point)))
  3181. (while stuff
  3182. (insert (car stuff))
  3183. (setq stuff (cdr stuff)))
  3184. (verilog-scan-region pt (point))))
  3185. ;; More searching
  3186. (defun verilog-declaration-end ()
  3187. (search-forward ";"))
  3188. (defun verilog-point-text (&optional pointnum)
  3189. "Return text describing where POINTNUM or current point is (for errors).
  3190. Use filename, if current buffer being edited shorten to just buffer name."
  3191. (concat (or (and (equal (window-buffer) (current-buffer))
  3192. (buffer-name))
  3193. buffer-file-name
  3194. (buffer-name))
  3195. ":" (int-to-string (1+ (count-lines (point-min) (or pointnum (point)))))))
  3196. (defun electric-verilog-backward-sexp ()
  3197. "Move backward over one balanced expression."
  3198. (interactive)
  3199. ;; before that see if we are in a comment
  3200. (verilog-backward-sexp))
  3201. (defun electric-verilog-forward-sexp ()
  3202. "Move forward over one balanced expression."
  3203. (interactive)
  3204. ;; before that see if we are in a comment
  3205. (verilog-forward-sexp))
  3206. (defun verilog-forward-sexp-function (arg)
  3207. "Move forward ARG sexps."
  3208. ;; Used by hs-minor-mode
  3209. (if (< arg 0)
  3210. (verilog-backward-sexp)
  3211. (verilog-forward-sexp)))
  3212. (defun verilog-backward-sexp ()
  3213. (let ((reg)
  3214. (elsec 1)
  3215. (found nil)
  3216. (st (point)))
  3217. (if (not (looking-at "\\<"))
  3218. (forward-word-strictly -1))
  3219. (cond
  3220. ((verilog-skip-backward-comment-or-string))
  3221. ((looking-at "\\<else\\>")
  3222. (setq reg (concat
  3223. verilog-end-block-re
  3224. "\\|\\(\\<else\\>\\)"
  3225. "\\|\\(\\<if\\>\\)"))
  3226. (while (and (not found)
  3227. (verilog-re-search-backward reg nil 'move))
  3228. (cond
  3229. ((match-end 1) ; matched verilog-end-block-re
  3230. ;; try to leap back to matching outward block by striding across
  3231. ;; indent level changing tokens then immediately
  3232. ;; previous line governs indentation.
  3233. (verilog-leap-to-head))
  3234. ((match-end 2) ; else, we're in deep
  3235. (setq elsec (1+ elsec)))
  3236. ((match-end 3) ; found it
  3237. (setq elsec (1- elsec))
  3238. (if (= 0 elsec)
  3239. ;; Now previous line describes syntax
  3240. (setq found 't))))))
  3241. ((looking-at verilog-end-block-re)
  3242. (verilog-leap-to-head))
  3243. ((looking-at "\\(endmodule\\>\\)\\|\\(\\<endprimitive\\>\\)\\|\\(\\<endclass\\>\\)\\|\\(\\<endprogram\\>\\)\\|\\(\\<endinterface\\>\\)\\|\\(\\<endpackage\\>\\)")
  3244. (cond
  3245. ((match-end 1)
  3246. (verilog-re-search-backward "\\<\\(macro\\)?module\\>" nil 'move))
  3247. ((match-end 2)
  3248. (verilog-re-search-backward "\\<primitive\\>" nil 'move))
  3249. ((match-end 3)
  3250. (verilog-re-search-backward "\\<class\\>" nil 'move))
  3251. ((match-end 4)
  3252. (verilog-re-search-backward "\\<program\\>" nil 'move))
  3253. ((match-end 5)
  3254. (verilog-re-search-backward "\\<interface\\>" nil 'move))
  3255. ((match-end 6)
  3256. (verilog-re-search-backward "\\<package\\>" nil 'move))
  3257. (t
  3258. (goto-char st)
  3259. (backward-sexp 1))))
  3260. (t
  3261. (goto-char st)
  3262. (backward-sexp)))))
  3263. (defun verilog-forward-sexp ()
  3264. (let ((reg)
  3265. (md 2)
  3266. (st (point))
  3267. (nest 'yes))
  3268. (if (not (looking-at "\\<"))
  3269. (forward-word-strictly -1))
  3270. (cond
  3271. ((verilog-skip-forward-comment-or-string)
  3272. (verilog-forward-syntactic-ws))
  3273. ((looking-at verilog-beg-block-re-ordered)
  3274. (cond
  3275. ((match-end 1);
  3276. ;; Search forward for matching end
  3277. (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
  3278. ((match-end 2)
  3279. ;; Search forward for matching endcase
  3280. (setq reg "\\(\\<randcase\\>\\|\\(\\<unique0?\\>\\s-+\\|\\<priority\\>\\s-+\\)?\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" )
  3281. (setq md 3) ; ender is third item in regexp
  3282. )
  3283. ((match-end 4)
  3284. ;; might be "disable fork" or "wait fork"
  3285. (let
  3286. (here)
  3287. (if (or
  3288. (looking-at verilog-disable-fork-re)
  3289. (and (looking-at "fork")
  3290. (progn
  3291. (setq here (point)) ; sometimes a fork is just a fork
  3292. (forward-word-strictly -1)
  3293. (looking-at verilog-disable-fork-re))))
  3294. (progn ; it is a disable fork; ignore it
  3295. (goto-char (match-end 0))
  3296. (forward-word-strictly 1)
  3297. (setq reg nil))
  3298. (progn ; it is a nice simple fork
  3299. (goto-char here) ; return from looking for "disable fork"
  3300. ;; Search forward for matching join
  3301. (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))))
  3302. ((match-end 6)
  3303. ;; Search forward for matching endclass
  3304. (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
  3305. ((match-end 7)
  3306. ;; Search forward for matching endtable
  3307. (setq reg "\\<endtable\\>" )
  3308. (setq nest 'no))
  3309. ((match-end 8)
  3310. ;; Search forward for matching endspecify
  3311. (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
  3312. ((match-end 9)
  3313. ;; Search forward for matching endfunction
  3314. (setq reg "\\<endfunction\\>" )
  3315. (setq nest 'no))
  3316. ((match-end 10)
  3317. ;; Search forward for matching endfunction
  3318. (setq reg "\\<endfunction\\>" )
  3319. (setq nest 'no))
  3320. ((match-end 11)
  3321. ;; Search forward for matching endtask
  3322. (setq reg "\\<endtask\\>" )
  3323. (setq nest 'no))
  3324. ((match-end 12)
  3325. ;; Search forward for matching endtask
  3326. (setq reg "\\<endtask\\>" )
  3327. (setq nest 'no))
  3328. ((match-end 12)
  3329. ;; Search forward for matching endgenerate
  3330. (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
  3331. ((match-end 13)
  3332. ;; Search forward for matching endgroup
  3333. (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
  3334. ((match-end 14)
  3335. ;; Search forward for matching endproperty
  3336. (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
  3337. ((match-end 15)
  3338. ;; Search forward for matching endsequence
  3339. (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" )
  3340. (setq md 3)) ; 3 to get to endsequence in the reg above
  3341. ((match-end 17)
  3342. ;; Search forward for matching endclocking
  3343. (setq reg "\\(\\<clocking\\>\\)\\|\\(\\<endclocking\\>\\)" )))
  3344. (if (and reg
  3345. (forward-word-strictly 1))
  3346. (catch 'skip
  3347. (if (eq nest 'yes)
  3348. (let ((depth 1)
  3349. here)
  3350. (while (verilog-re-search-forward reg nil 'move)
  3351. (cond
  3352. ((match-end md) ; a closer in regular expression, so we are climbing out
  3353. (setq depth (1- depth))
  3354. (if (= 0 depth) ; we are out!
  3355. (throw 'skip 1)))
  3356. ((match-end 1) ; an opener in the r-e, so we are in deeper now
  3357. (setq here (point)) ; remember where we started
  3358. (goto-char (match-beginning 1))
  3359. (cond
  3360. ((if (or
  3361. (looking-at verilog-disable-fork-re)
  3362. (and (looking-at "fork")
  3363. (progn
  3364. (forward-word-strictly -1)
  3365. (looking-at verilog-disable-fork-re))))
  3366. (progn ; it is a disable fork; another false alarm
  3367. (goto-char (match-end 0)))
  3368. (progn ; it is a simple fork (or has nothing to do with fork)
  3369. (goto-char here)
  3370. (setq depth (1+ depth))))))))))
  3371. (if (verilog-re-search-forward reg nil 'move)
  3372. (throw 'skip 1))))))
  3373. ((looking-at (concat
  3374. "\\(\\<\\(macro\\)?module\\>\\)\\|"
  3375. "\\(\\<primitive\\>\\)\\|"
  3376. "\\(\\<class\\>\\)\\|"
  3377. "\\(\\<program\\>\\)\\|"
  3378. "\\(\\<interface\\>\\)\\|"
  3379. "\\(\\<package\\>\\)"))
  3380. (cond
  3381. ((match-end 1)
  3382. (verilog-re-search-forward "\\<endmodule\\>" nil 'move))
  3383. ((match-end 2)
  3384. (verilog-re-search-forward "\\<endprimitive\\>" nil 'move))
  3385. ((match-end 3)
  3386. (verilog-re-search-forward "\\<endclass\\>" nil 'move))
  3387. ((match-end 4)
  3388. (verilog-re-search-forward "\\<endprogram\\>" nil 'move))
  3389. ((match-end 5)
  3390. (verilog-re-search-forward "\\<endinterface\\>" nil 'move))
  3391. ((match-end 6)
  3392. (verilog-re-search-forward "\\<endpackage\\>" nil 'move))
  3393. (t
  3394. (goto-char st)
  3395. (if (= (following-char) ?\) )
  3396. (forward-char 1)
  3397. (forward-sexp 1)))))
  3398. (t
  3399. (goto-char st)
  3400. (if (= (following-char) ?\) )
  3401. (forward-char 1)
  3402. (forward-sexp 1))))))
  3403. (defun verilog-declaration-beg ()
  3404. (verilog-re-search-backward verilog-declaration-re (bobp) t))
  3405. ;;
  3406. ;;
  3407. ;; Mode
  3408. ;;
  3409. (defvar verilog-which-tool 1)
  3410. ;;;###autoload
  3411. (define-derived-mode verilog-mode prog-mode "Verilog"
  3412. "Major mode for editing Verilog code.
  3413. \\<verilog-mode-map>
  3414. See \\[describe-function] verilog-auto (\\[verilog-auto]) for details on how
  3415. AUTOs can improve coding efficiency.
  3416. Use \\[verilog-faq] for a pointer to frequently asked questions.
  3417. NEWLINE, TAB indents for Verilog code.
  3418. Delete converts tabs to spaces as it moves back.
  3419. Supports highlighting.
  3420. Turning on Verilog mode calls the value of the variable `verilog-mode-hook'
  3421. with no args, if that value is non-nil.
  3422. Variables controlling indentation/edit style:
  3423. variable `verilog-indent-level' (default 3)
  3424. Indentation of Verilog statements with respect to containing block.
  3425. `verilog-indent-level-module' (default 3)
  3426. Absolute indentation of Module level Verilog statements.
  3427. Set to 0 to get initial and always statements lined up
  3428. on the left side of your screen.
  3429. `verilog-indent-level-declaration' (default 3)
  3430. Indentation of declarations with respect to containing block.
  3431. Set to 0 to get them list right under containing block.
  3432. `verilog-indent-level-behavioral' (default 3)
  3433. Indentation of first begin in a task or function block
  3434. Set to 0 to get such code to lined up underneath the task or
  3435. function keyword.
  3436. `verilog-indent-level-directive' (default 1)
  3437. Indentation of \\=`ifdef/\\=`endif blocks.
  3438. `verilog-cexp-indent' (default 1)
  3439. Indentation of Verilog statements broken across lines i.e.:
  3440. if (a)
  3441. begin
  3442. `verilog-case-indent' (default 2)
  3443. Indentation for case statements.
  3444. `verilog-auto-newline' (default nil)
  3445. Non-nil means automatically newline after semicolons and the punctuation
  3446. mark after an end.
  3447. `verilog-auto-indent-on-newline' (default t)
  3448. Non-nil means automatically indent line after newline.
  3449. `verilog-tab-always-indent' (default t)
  3450. Non-nil means TAB in Verilog mode should always reindent the current line,
  3451. regardless of where in the line point is when the TAB command is used.
  3452. `verilog-indent-begin-after-if' (default t)
  3453. Non-nil means to indent begin statements following a preceding
  3454. if, else, while, for and repeat statements, if any. Otherwise,
  3455. the begin is lined up with the preceding token. If t, you get:
  3456. if (a)
  3457. begin // amount of indent based on `verilog-cexp-indent'
  3458. otherwise you get:
  3459. if (a)
  3460. begin
  3461. `verilog-auto-endcomments' (default t)
  3462. Non-nil means a comment /* ... */ is set after the ends which ends
  3463. cases, tasks, functions and modules.
  3464. The type and name of the object will be set between the braces.
  3465. `verilog-minimum-comment-distance' (default 10)
  3466. Minimum distance (in lines) between begin and end required before a comment
  3467. will be inserted. Setting this variable to zero results in every
  3468. end acquiring a comment; the default avoids too many redundant
  3469. comments in tight quarters.
  3470. `verilog-auto-lineup' (default `declarations')
  3471. List of contexts where auto lineup of code should be done.
  3472. Variables controlling other actions:
  3473. `verilog-linter' (default `surelint')
  3474. Unix program to call to run the lint checker. This is the default
  3475. command for \\[compile-command] and \\[verilog-auto-save-compile].
  3476. See \\[customize] for the complete list of variables.
  3477. AUTO expansion functions are, in part:
  3478. \\[verilog-auto] Expand AUTO statements.
  3479. \\[verilog-delete-auto] Remove the AUTOs.
  3480. \\[verilog-inject-auto] Insert AUTOs for the first time.
  3481. Some other functions are:
  3482. \\[completion-at-point] Complete word with appropriate possibilities.
  3483. \\[verilog-mark-defun] Mark function.
  3484. \\[verilog-beg-of-defun] Move to beginning of current function.
  3485. \\[verilog-end-of-defun] Move to end of current function.
  3486. \\[verilog-label-be] Label matching begin ... end, fork ... join, etc statements.
  3487. \\[verilog-comment-region] Put marked area in a comment.
  3488. \\[verilog-uncomment-region] Uncomment an area commented with \\[verilog-comment-region].
  3489. \\[verilog-insert-block] Insert begin ... end.
  3490. \\[verilog-star-comment] Insert /* ... */.
  3491. \\[verilog-sk-always] Insert an always @(AS) begin .. end block.
  3492. \\[verilog-sk-begin] Insert a begin .. end block.
  3493. \\[verilog-sk-case] Insert a case block, prompting for details.
  3494. \\[verilog-sk-for] Insert a for (...) begin .. end block, prompting for details.
  3495. \\[verilog-sk-generate] Insert a generate .. endgenerate block.
  3496. \\[verilog-sk-header] Insert a header block at the top of file.
  3497. \\[verilog-sk-initial] Insert an initial begin .. end block.
  3498. \\[verilog-sk-fork] Insert a fork begin .. end .. join block.
  3499. \\[verilog-sk-module] Insert a module .. (/*AUTOARG*/);.. endmodule block.
  3500. \\[verilog-sk-ovm-class] Insert an OVM Class block.
  3501. \\[verilog-sk-uvm-object] Insert an UVM Object block.
  3502. \\[verilog-sk-uvm-component] Insert an UVM Component block.
  3503. \\[verilog-sk-primitive] Insert a primitive .. (.. );.. endprimitive block.
  3504. \\[verilog-sk-repeat] Insert a repeat (..) begin .. end block.
  3505. \\[verilog-sk-specify] Insert a specify .. endspecify block.
  3506. \\[verilog-sk-task] Insert a task .. begin .. end endtask block.
  3507. \\[verilog-sk-while] Insert a while (...) begin .. end block, prompting for details.
  3508. \\[verilog-sk-casex] Insert a casex (...) item: begin.. end endcase block, prompting for details.
  3509. \\[verilog-sk-casez] Insert a casez (...) item: begin.. end endcase block, prompting for details.
  3510. \\[verilog-sk-if] Insert an if (..) begin .. end block.
  3511. \\[verilog-sk-else-if] Insert an else if (..) begin .. end block.
  3512. \\[verilog-sk-comment] Insert a comment block.
  3513. \\[verilog-sk-assign] Insert an assign .. = ..; statement.
  3514. \\[verilog-sk-function] Insert a function .. begin .. end endfunction block.
  3515. \\[verilog-sk-input] Insert an input declaration, prompting for details.
  3516. \\[verilog-sk-output] Insert an output declaration, prompting for details.
  3517. \\[verilog-sk-state-machine] Insert a state machine definition, prompting for details.
  3518. \\[verilog-sk-inout] Insert an inout declaration, prompting for details.
  3519. \\[verilog-sk-wire] Insert a wire declaration, prompting for details.
  3520. \\[verilog-sk-reg] Insert a register declaration, prompting for details.
  3521. \\[verilog-sk-define-signal] Define signal under point as a register at the top of the module.
  3522. All key bindings can be seen in a Verilog-buffer with \\[describe-bindings].
  3523. Key bindings specific to `verilog-mode-map' are:
  3524. \\{verilog-mode-map}"
  3525. :abbrev-table verilog-mode-abbrev-table
  3526. (set (make-local-variable 'beginning-of-defun-function)
  3527. 'verilog-beg-of-defun)
  3528. (set (make-local-variable 'end-of-defun-function)
  3529. 'verilog-end-of-defun)
  3530. (set-syntax-table verilog-mode-syntax-table)
  3531. (set (make-local-variable 'indent-line-function)
  3532. #'verilog-indent-line-relative)
  3533. (set (make-local-variable 'comment-indent-function) 'verilog-comment-indent)
  3534. (set (make-local-variable 'parse-sexp-ignore-comments) nil)
  3535. (set (make-local-variable 'comment-start) "// ")
  3536. (set (make-local-variable 'comment-end) "")
  3537. (set (make-local-variable 'comment-start-skip) "/\\*+ *\\|// *")
  3538. (set (make-local-variable 'comment-multi-line) nil)
  3539. ;; Set up for compilation
  3540. (setq verilog-which-tool 1)
  3541. (setq verilog-tool 'verilog-linter)
  3542. (verilog-set-compile-command)
  3543. (when (boundp 'hack-local-variables-hook) ; Also modify any file-local-variables
  3544. (add-hook 'hack-local-variables-hook 'verilog-modify-compile-command t))
  3545. ;; Setting up menus
  3546. (when (featurep 'xemacs)
  3547. (easy-menu-add verilog-stmt-menu)
  3548. (easy-menu-add verilog-menu)
  3549. (setq mode-popup-menu (cons "Verilog Mode" verilog-stmt-menu)))
  3550. ;; Stuff for GNU Emacs
  3551. (set (make-local-variable 'font-lock-defaults)
  3552. `((verilog-font-lock-keywords
  3553. verilog-font-lock-keywords-1
  3554. verilog-font-lock-keywords-2
  3555. verilog-font-lock-keywords-3)
  3556. nil nil nil
  3557. ,(if (functionp 'syntax-ppss)
  3558. ;; verilog-beg-of-defun uses syntax-ppss, and syntax-ppss uses
  3559. ;; font-lock-beginning-of-syntax-function, so
  3560. ;; font-lock-beginning-of-syntax-function, can't use
  3561. ;; verilog-beg-of-defun.
  3562. nil
  3563. 'verilog-beg-of-defun)))
  3564. ;;------------------------------------------------------------
  3565. ;; now hook in 'verilog-highlight-include-files (eldo-mode.el&spice-mode.el)
  3566. ;; all buffer local:
  3567. (unless noninteractive ; Else can't see the result, and change hooks are slow
  3568. (when (featurep 'xemacs)
  3569. (make-local-hook 'font-lock-mode-hook)
  3570. (make-local-hook 'font-lock-after-fontify-buffer-hook); doesn't exist in Emacs
  3571. (make-local-hook 'after-change-functions))
  3572. (add-hook 'font-lock-mode-hook 'verilog-highlight-buffer t t)
  3573. (add-hook 'font-lock-after-fontify-buffer-hook 'verilog-highlight-buffer t t) ; not in Emacs
  3574. (add-hook 'after-change-functions 'verilog-highlight-region t t))
  3575. ;; Tell imenu how to handle Verilog.
  3576. (set (make-local-variable 'imenu-generic-expression)
  3577. verilog-imenu-generic-expression)
  3578. ;; Tell which-func-modes that imenu knows about verilog
  3579. (when (and (boundp 'which-func-modes) (listp which-func-modes))
  3580. (add-to-list 'which-func-modes 'verilog-mode))
  3581. ;; hideshow support
  3582. (when (boundp 'hs-special-modes-alist)
  3583. (unless (assq 'verilog-mode hs-special-modes-alist)
  3584. (setq hs-special-modes-alist
  3585. (cons '(verilog-mode-mode "\\<begin\\>" "\\<end\\>" nil
  3586. verilog-forward-sexp-function)
  3587. hs-special-modes-alist))))
  3588. (add-hook 'completion-at-point-functions
  3589. #'verilog-completion-at-point nil 'local)
  3590. ;; Stuff for autos
  3591. (add-hook 'write-contents-hooks 'verilog-auto-save-check nil 'local)
  3592. ;; verilog-mode-hook call added by define-derived-mode
  3593. )
  3594. ;;; Integration with the speedbar
  3595. ;;
  3596. ;; Avoid problems with XEmacs byte-compiles.
  3597. ;; For GNU Emacs, the eval-after-load will handle if it isn't loaded yet.
  3598. (when (eval-when-compile (fboundp 'declare-function))
  3599. (declare-function speedbar-add-supported-extension "speedbar" (extension)))
  3600. (defun verilog-speedbar-initialize ()
  3601. "Initialize speedbar to understand `verilog-mode'."
  3602. ;; Set Verilog file extensions (extracted from `auto-mode-alist')
  3603. (let ((mode-alist auto-mode-alist))
  3604. (while mode-alist
  3605. (when (eq (cdar mode-alist) 'verilog-mode)
  3606. (speedbar-add-supported-extension (caar mode-alist)))
  3607. (setq mode-alist (cdr mode-alist)))))
  3608. ;; If the speedbar is loaded, execute initialization instructions right away,
  3609. ;; otherwise add the initialization instructions to the speedbar loader.
  3610. (eval-after-load "speedbar" '(verilog-speedbar-initialize))
  3611. ;;; Electric functions:
  3612. ;;
  3613. (defun electric-verilog-terminate-line (&optional arg)
  3614. "Terminate line and indent next line.
  3615. With optional ARG, remove existing end of line comments."
  3616. (interactive)
  3617. ;; before that see if we are in a comment
  3618. (let ((state (save-excursion (verilog-syntax-ppss))))
  3619. (cond
  3620. ((nth 7 state) ; Inside // comment
  3621. (if (eolp)
  3622. (progn
  3623. (delete-horizontal-space)
  3624. (newline))
  3625. (progn
  3626. (newline)
  3627. (insert "// ")
  3628. (beginning-of-line)))
  3629. (verilog-indent-line))
  3630. ((nth 4 state) ; Inside any comment (hence /**/)
  3631. (newline)
  3632. (verilog-more-comment))
  3633. ((eolp)
  3634. ;; First, check if current line should be indented
  3635. (if (save-excursion
  3636. (delete-horizontal-space)
  3637. (beginning-of-line)
  3638. (skip-chars-forward " \t")
  3639. (if (looking-at verilog-auto-end-comment-lines-re)
  3640. (let ((indent-str (verilog-indent-line)))
  3641. ;; Maybe we should set some endcomments
  3642. (if verilog-auto-endcomments
  3643. (verilog-set-auto-endcomments indent-str arg))
  3644. (end-of-line)
  3645. (delete-horizontal-space)
  3646. (if arg
  3647. ()
  3648. (newline))
  3649. nil)
  3650. (progn
  3651. (end-of-line)
  3652. (delete-horizontal-space)
  3653. 't)))
  3654. ;; see if we should line up assignments
  3655. (progn
  3656. (if (or (eq 'all verilog-auto-lineup)
  3657. (eq 'assignments verilog-auto-lineup))
  3658. (verilog-pretty-expr t "\\(<\\|:\\)?=" ))
  3659. (newline))
  3660. (forward-line 1))
  3661. ;; Indent next line
  3662. (if verilog-auto-indent-on-newline
  3663. (verilog-indent-line)))
  3664. (t
  3665. (newline)))))
  3666. (defun electric-verilog-terminate-and-indent ()
  3667. "Insert a newline and indent for the next statement."
  3668. (interactive)
  3669. (electric-verilog-terminate-line 1))
  3670. (defun electric-verilog-semi ()
  3671. "Insert `;' character and reindent the line."
  3672. (interactive)
  3673. (verilog-insert-last-command-event)
  3674. (if (or (verilog-in-comment-or-string-p)
  3675. (verilog-in-escaped-name-p))
  3676. ()
  3677. (save-excursion
  3678. (beginning-of-line)
  3679. (verilog-forward-ws&directives)
  3680. (verilog-indent-line))
  3681. (if (and verilog-auto-newline
  3682. (not (verilog-parenthesis-depth)))
  3683. (electric-verilog-terminate-line))))
  3684. (defun electric-verilog-semi-with-comment ()
  3685. "Insert `;' character, reindent the line and indent for comment."
  3686. (interactive)
  3687. (insert ";")
  3688. (save-excursion
  3689. (beginning-of-line)
  3690. (verilog-indent-line))
  3691. (indent-for-comment))
  3692. (defun electric-verilog-colon ()
  3693. "Insert `:' and do all indentations except line indent on this line."
  3694. (interactive)
  3695. (verilog-insert-last-command-event)
  3696. ;; Do nothing if within string.
  3697. (if (or
  3698. (verilog-within-string)
  3699. (not (verilog-in-case-region-p)))
  3700. ()
  3701. (save-excursion
  3702. (let ((p (point))
  3703. (lim (progn (verilog-beg-of-statement) (point))))
  3704. (goto-char p)
  3705. (verilog-backward-case-item lim)
  3706. (verilog-indent-line)))
  3707. ;; (let ((verilog-tab-always-indent nil))
  3708. ;; (verilog-indent-line))
  3709. ))
  3710. ;;(defun electric-verilog-equal ()
  3711. ;; "Insert `=', and do indentation if within block."
  3712. ;; (interactive)
  3713. ;; (verilog-insert-last-command-event)
  3714. ;; Could auto line up expressions, but not yet
  3715. ;; (if (eq (car (verilog-calculate-indent)) 'block)
  3716. ;; (let ((verilog-tab-always-indent nil))
  3717. ;; (verilog-indent-command)))
  3718. ;; )
  3719. (defun electric-verilog-tick ()
  3720. "Insert back-tick, and indent to column 0 if this is a CPP directive."
  3721. (interactive)
  3722. (verilog-insert-last-command-event)
  3723. (save-excursion
  3724. (if (verilog-in-directive-p)
  3725. (verilog-indent-line))))
  3726. (defun electric-verilog-tab ()
  3727. "Function called when TAB is pressed in Verilog mode."
  3728. (interactive)
  3729. ;; If verilog-tab-always-indent, indent the beginning of the line.
  3730. (cond
  3731. ;; The region is active, indent it.
  3732. ((and (region-active-p)
  3733. (not (eq (region-beginning) (region-end))))
  3734. (indent-region (region-beginning) (region-end) nil))
  3735. ((or verilog-tab-always-indent
  3736. (save-excursion
  3737. (skip-chars-backward " \t")
  3738. (bolp)))
  3739. (let* ((oldpnt (point))
  3740. (boi-point
  3741. (save-excursion
  3742. (beginning-of-line)
  3743. (skip-chars-forward " \t")
  3744. (verilog-indent-line)
  3745. (back-to-indentation)
  3746. (point))))
  3747. (if (< (point) boi-point)
  3748. (back-to-indentation)
  3749. (cond ((not verilog-tab-to-comment))
  3750. ((not (eolp))
  3751. (end-of-line))
  3752. (t
  3753. (indent-for-comment)
  3754. (when (and (eolp) (= oldpnt (point)))
  3755. ;; kill existing comment
  3756. (beginning-of-line)
  3757. (re-search-forward comment-start-skip oldpnt 'move)
  3758. (goto-char (match-beginning 0))
  3759. (skip-chars-backward " \t")
  3760. (kill-region (point) oldpnt)))))))
  3761. (t (progn (insert "\t")))))
  3762. ;;; Interactive functions:
  3763. ;;
  3764. (defun verilog-indent-buffer ()
  3765. "Indent-region the entire buffer as Verilog code.
  3766. To call this from the command line, see \\[verilog-batch-indent]."
  3767. (interactive)
  3768. (verilog-mode)
  3769. (indent-region (point-min) (point-max) nil))
  3770. (defun verilog-insert-block ()
  3771. "Insert Verilog begin ... end; block in the code with right indentation."
  3772. (interactive)
  3773. (verilog-indent-line)
  3774. (insert "begin")
  3775. (electric-verilog-terminate-line)
  3776. (save-excursion
  3777. (electric-verilog-terminate-line)
  3778. (insert "end")
  3779. (beginning-of-line)
  3780. (verilog-indent-line)))
  3781. (defun verilog-star-comment ()
  3782. "Insert Verilog star comment at point."
  3783. (interactive)
  3784. (verilog-indent-line)
  3785. (insert "/*")
  3786. (save-excursion
  3787. (newline)
  3788. (insert " */"))
  3789. (newline)
  3790. (insert " * "))
  3791. (defun verilog-insert-1 (fmt max)
  3792. "Use format string FMT to insert integers 0 to MAX - 1.
  3793. Inserts one integer per line, at the current column. Stops early
  3794. if it reaches the end of the buffer."
  3795. (let ((col (current-column))
  3796. (n 0))
  3797. (save-excursion
  3798. (while (< n max)
  3799. (insert (format fmt n))
  3800. (forward-line 1)
  3801. ;; Note that this function does not bother to check for lines
  3802. ;; shorter than col.
  3803. (if (eobp)
  3804. (setq n max)
  3805. (setq n (1+ n))
  3806. (move-to-column col))))))
  3807. (defun verilog-insert-indices (max)
  3808. "Insert a set of indices into a rectangle.
  3809. The upper left corner is defined by point. Indices begin with 0
  3810. and extend to the MAX - 1. If no prefix arg is given, the user
  3811. is prompted for a value. The indices are surrounded by square
  3812. brackets []. For example, the following code with the point
  3813. located after the first `a' gives:
  3814. a = b a[ 0] = b
  3815. a = b a[ 1] = b
  3816. a = b a[ 2] = b
  3817. a = b a[ 3] = b
  3818. a = b ==> insert-indices ==> a[ 4] = b
  3819. a = b a[ 5] = b
  3820. a = b a[ 6] = b
  3821. a = b a[ 7] = b
  3822. a = b a[ 8] = b"
  3823. (interactive "NMAX: ")
  3824. (verilog-insert-1 "[%3d]" max))
  3825. (defun verilog-generate-numbers (max)
  3826. "Insert a set of generated numbers into a rectangle.
  3827. The upper left corner is defined by point. The numbers are padded to three
  3828. digits, starting with 000 and extending to (MAX - 1). If no prefix argument
  3829. is supplied, then the user is prompted for the MAX number. Consider the
  3830. following code fragment:
  3831. buf buf buf buf000
  3832. buf buf buf buf001
  3833. buf buf buf buf002
  3834. buf buf buf buf003
  3835. buf buf ==> generate-numbers ==> buf buf004
  3836. buf buf buf buf005
  3837. buf buf buf buf006
  3838. buf buf buf buf007
  3839. buf buf buf buf008"
  3840. (interactive "NMAX: ")
  3841. (verilog-insert-1 "%3.3d" max))
  3842. (defun verilog-mark-defun ()
  3843. "Mark the current Verilog function (or procedure).
  3844. This puts the mark at the end, and point at the beginning."
  3845. (interactive)
  3846. (if (featurep 'xemacs)
  3847. (progn
  3848. (push-mark (point))
  3849. (verilog-end-of-defun)
  3850. (push-mark (point))
  3851. (verilog-beg-of-defun)
  3852. (if (fboundp 'zmacs-activate-region)
  3853. (zmacs-activate-region)))
  3854. (mark-defun)))
  3855. (defun verilog-comment-region (start end)
  3856. ;; checkdoc-params: (start end)
  3857. "Put the region into a Verilog comment.
  3858. The comments that are in this area are \"deformed\":
  3859. `*)' becomes `!(*' and `}' becomes `!{'.
  3860. These deformed comments are returned to normal if you use
  3861. \\[verilog-uncomment-region] to undo the commenting.
  3862. The commented area starts with `verilog-exclude-str-start', and ends with
  3863. `verilog-exclude-str-end'. But if you change these variables,
  3864. \\[verilog-uncomment-region] won't recognize the comments."
  3865. (interactive "r")
  3866. (save-excursion
  3867. ;; Insert start and endcomments
  3868. (goto-char end)
  3869. (if (and (save-excursion (skip-chars-forward " \t") (eolp))
  3870. (not (save-excursion (skip-chars-backward " \t") (bolp))))
  3871. (forward-line 1)
  3872. (beginning-of-line))
  3873. (insert verilog-exclude-str-end)
  3874. (setq end (point))
  3875. (newline)
  3876. (goto-char start)
  3877. (beginning-of-line)
  3878. (insert verilog-exclude-str-start)
  3879. (newline)
  3880. ;; Replace end-comments within commented area
  3881. (goto-char end)
  3882. (save-excursion
  3883. (while (re-search-backward "\\*/" start t)
  3884. (replace-match "*-/" t t)))
  3885. (save-excursion
  3886. (let ((s+1 (1+ start)))
  3887. (while (re-search-backward "/\\*" s+1 t)
  3888. (replace-match "/-*" t t))))))
  3889. (defun verilog-uncomment-region ()
  3890. "Uncomment a commented area; change deformed comments back to normal.
  3891. This command does nothing if the pointer is not in a commented
  3892. area. See also `verilog-comment-region'."
  3893. (interactive)
  3894. (save-excursion
  3895. (let ((start (point))
  3896. (end (point)))
  3897. ;; Find the boundaries of the comment
  3898. (save-excursion
  3899. (setq start (progn (search-backward verilog-exclude-str-start nil t)
  3900. (point)))
  3901. (setq end (progn (search-forward verilog-exclude-str-end nil t)
  3902. (point))))
  3903. ;; Check if we're really inside a comment
  3904. (if (or (equal start (point)) (<= end (point)))
  3905. (message "Not standing within commented area.")
  3906. (progn
  3907. ;; Remove endcomment
  3908. (goto-char end)
  3909. (beginning-of-line)
  3910. (let ((pos (point)))
  3911. (end-of-line)
  3912. (delete-region pos (1+ (point))))
  3913. ;; Change comments back to normal
  3914. (save-excursion
  3915. (while (re-search-backward "\\*-/" start t)
  3916. (replace-match "*/" t t)))
  3917. (save-excursion
  3918. (while (re-search-backward "/-\\*" start t)
  3919. (replace-match "/*" t t)))
  3920. ;; Remove start comment
  3921. (goto-char start)
  3922. (beginning-of-line)
  3923. (let ((pos (point)))
  3924. (end-of-line)
  3925. (delete-region pos (1+ (point)))))))))
  3926. (defun verilog-beg-of-defun ()
  3927. "Move backward to the beginning of the current function or procedure."
  3928. (interactive)
  3929. (verilog-re-search-backward verilog-defun-re nil 'move))
  3930. (defun verilog-beg-of-defun-quick ()
  3931. "Move backward to the beginning of the current function or procedure.
  3932. Uses `verilog-scan' cache."
  3933. (interactive)
  3934. (verilog-re-search-backward-quick verilog-defun-re nil 'move))
  3935. (defun verilog-end-of-defun ()
  3936. "Move forward to the end of the current function or procedure."
  3937. (interactive)
  3938. (verilog-re-search-forward verilog-end-defun-re nil 'move))
  3939. (defun verilog-get-end-of-defun ()
  3940. (save-excursion
  3941. (cond ((verilog-re-search-forward-quick verilog-end-defun-re nil t)
  3942. (point))
  3943. (t
  3944. (error "%s: Can't find endmodule" (verilog-point-text))
  3945. (point-max)))))
  3946. (defun verilog-label-be ()
  3947. "Label matching begin ... end, fork ... join and case ... endcase statements."
  3948. (interactive)
  3949. (let ((cnt 0)
  3950. (case-fold-search nil)
  3951. (oldpos (point))
  3952. (b (progn
  3953. (verilog-beg-of-defun)
  3954. (point-marker)))
  3955. (e (progn
  3956. (verilog-end-of-defun)
  3957. (point-marker))))
  3958. (goto-char (marker-position b))
  3959. (if (> (- e b) 200)
  3960. (message "Relabeling module..."))
  3961. (while (and
  3962. (> (marker-position e) (point))
  3963. (verilog-re-search-forward
  3964. verilog-auto-end-comment-lines-re
  3965. nil 'move))
  3966. (goto-char (match-beginning 0))
  3967. (let ((indent-str (verilog-indent-line)))
  3968. (verilog-set-auto-endcomments indent-str 't)
  3969. (end-of-line)
  3970. (delete-horizontal-space))
  3971. (setq cnt (1+ cnt))
  3972. (if (= 9 (% cnt 10))
  3973. (message "%d..." cnt)))
  3974. (goto-char oldpos)
  3975. (if (or
  3976. (> (- e b) 200)
  3977. (> cnt 20))
  3978. (message "%d lines auto commented" cnt))))
  3979. (defun verilog-beg-of-statement ()
  3980. "Move backward to beginning of statement."
  3981. (interactive)
  3982. ;; Move back token by token until we see the end
  3983. ;; of some earlier line.
  3984. (let (h)
  3985. (while
  3986. ;; If the current point does not begin a new
  3987. ;; statement, as in the character ahead of us is a ';', or SOF
  3988. ;; or the string after us unambiguously starts a statement,
  3989. ;; or the token before us unambiguously ends a statement,
  3990. ;; then move back a token and test again.
  3991. (not (or
  3992. ;; stop if beginning of buffer
  3993. (bobp)
  3994. ;; stop if looking at a pre-processor directive
  3995. (looking-at "`\\w+")
  3996. ;; stop if we find a ;
  3997. (= (preceding-char) ?\;)
  3998. ;; stop if we see a named coverpoint
  3999. (looking-at "\\w+\\W*:\\W*\\(coverpoint\\|cross\\|constraint\\)")
  4000. ;; keep going if we are in the middle of a word
  4001. (not (or (looking-at "\\<") (forward-word-strictly -1)))
  4002. ;; stop if we see an assertion (perhaps labeled)
  4003. (and
  4004. (looking-at "\\(\\w+\\W*:\\W*\\)?\\(\\<\\(assert\\|assume\\|cover\\)\\>\\s-+\\<property\\>\\)\\|\\(\\<assert\\>\\)")
  4005. (progn
  4006. (setq h (point))
  4007. (save-excursion
  4008. (verilog-backward-token)
  4009. (if (and (looking-at verilog-label-re)
  4010. (not (looking-at verilog-end-block-re)))
  4011. (setq h (point))))
  4012. (goto-char h)))
  4013. ;; stop if we see an extended complete reg, perhaps a complete one
  4014. (and
  4015. (looking-at verilog-complete-reg)
  4016. (let* ((p (point)))
  4017. (while (and (looking-at verilog-extended-complete-re)
  4018. (progn (setq p (point))
  4019. (verilog-backward-token)
  4020. (/= p (point)))))
  4021. (goto-char p)))
  4022. ;; stop if we see a complete reg (previous found extended ones)
  4023. (looking-at verilog-basic-complete-re)
  4024. ;; stop if previous token is an ender
  4025. (save-excursion
  4026. (verilog-backward-token)
  4027. (looking-at verilog-end-block-re))))
  4028. (verilog-backward-syntactic-ws)
  4029. (verilog-backward-token))
  4030. ;; Now point is where the previous line ended.
  4031. (verilog-forward-syntactic-ws)
  4032. ;; Skip forward over any preprocessor directives, as they have wacky indentation
  4033. (if (looking-at verilog-preprocessor-re)
  4034. (progn (goto-char (match-end 0))
  4035. (verilog-forward-syntactic-ws)))))
  4036. (defun verilog-beg-of-statement-1 ()
  4037. "Move backward to beginning of statement."
  4038. (interactive)
  4039. (if (verilog-in-comment-p)
  4040. (verilog-backward-syntactic-ws))
  4041. (let ((pt (point)))
  4042. (catch 'done
  4043. (while (not (looking-at verilog-complete-reg))
  4044. (setq pt (point))
  4045. (verilog-backward-syntactic-ws)
  4046. (if (or (bolp)
  4047. (= (preceding-char) ?\;)
  4048. (progn
  4049. (verilog-backward-token)
  4050. (looking-at verilog-ends-re)))
  4051. (progn
  4052. (goto-char pt)
  4053. (throw 'done t)))))
  4054. (verilog-forward-syntactic-ws)))
  4055. ;;
  4056. ;; (while (and
  4057. ;; (not (looking-at verilog-complete-reg))
  4058. ;; (not (bolp))
  4059. ;; (not (= (preceding-char) ?\;)))
  4060. ;; (verilog-backward-token)
  4061. ;; (verilog-backward-syntactic-ws)
  4062. ;; (setq pt (point)))
  4063. ;; (goto-char pt)
  4064. ;; ;(verilog-forward-syntactic-ws)
  4065. (defun verilog-end-of-statement ()
  4066. "Move forward to end of current statement."
  4067. (interactive)
  4068. (let ((nest 0) pos)
  4069. (cond
  4070. ((verilog-in-directive-p)
  4071. (forward-line 1)
  4072. (backward-char 1))
  4073. ((looking-at verilog-beg-block-re)
  4074. (verilog-forward-sexp))
  4075. ((equal (char-after) ?\})
  4076. (forward-char))
  4077. ;; Skip to end of statement
  4078. ((condition-case nil
  4079. (setq pos
  4080. (catch 'found
  4081. (while t
  4082. (forward-sexp 1)
  4083. (verilog-skip-forward-comment-or-string)
  4084. (if (eolp)
  4085. (forward-line 1))
  4086. (cond ((looking-at "[ \t]*;")
  4087. (skip-chars-forward "^;")
  4088. (forward-char 1)
  4089. (throw 'found (point)))
  4090. ((save-excursion
  4091. (forward-sexp -1)
  4092. (looking-at verilog-beg-block-re))
  4093. (goto-char (match-beginning 0))
  4094. (throw 'found nil))
  4095. ((looking-at "[ \t]*)")
  4096. (throw 'found (point)))
  4097. ((eobp)
  4098. (throw 'found (point)))
  4099. )))
  4100. )
  4101. (error nil))
  4102. (if (not pos)
  4103. ;; Skip a whole block
  4104. (catch 'found
  4105. (while t
  4106. (verilog-re-search-forward verilog-end-statement-re nil 'move)
  4107. (setq nest (if (match-end 1)
  4108. (1+ nest)
  4109. (1- nest)))
  4110. (cond ((eobp)
  4111. (throw 'found (point)))
  4112. ((= 0 nest)
  4113. (throw 'found (verilog-end-of-statement))))))
  4114. pos)))))
  4115. (defun verilog-in-case-region-p ()
  4116. "Return true if in a case region.
  4117. More specifically, point @ in the line foo : @ begin"
  4118. (interactive)
  4119. (save-excursion
  4120. (if (and
  4121. (progn (verilog-forward-syntactic-ws)
  4122. (looking-at "\\<begin\\>"))
  4123. (progn (verilog-backward-syntactic-ws)
  4124. (= (preceding-char) ?\:)))
  4125. (catch 'found
  4126. (let ((nest 1))
  4127. (while t
  4128. (verilog-re-search-backward
  4129. (concat "\\(\\<module\\>\\)\\|\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|"
  4130. "\\(\\<endcase\\>\\)\\>")
  4131. nil 'move)
  4132. (cond
  4133. ((match-end 3)
  4134. (setq nest (1+ nest)))
  4135. ((match-end 2)
  4136. (if (= nest 1)
  4137. (throw 'found 1))
  4138. (setq nest (1- nest)))
  4139. (t
  4140. (throw 'found (= nest 0)))))))
  4141. nil)))
  4142. (defun verilog-backward-up-list (arg)
  4143. "Call `backward-up-list' ARG, ignoring comments."
  4144. (let ((parse-sexp-ignore-comments t))
  4145. (backward-up-list arg)))
  4146. (defun verilog-forward-sexp-cmt (arg)
  4147. "Call `forward-sexp' ARG, inside comments."
  4148. (let ((parse-sexp-ignore-comments nil))
  4149. (forward-sexp arg)))
  4150. (defun verilog-forward-sexp-ign-cmt (arg)
  4151. "Call `forward-sexp' ARG, ignoring comments."
  4152. (let ((parse-sexp-ignore-comments t))
  4153. (forward-sexp arg)))
  4154. (defun verilog-in-generate-region-p ()
  4155. "Return true if in a generate region.
  4156. More specifically, after a generate and before an endgenerate."
  4157. (interactive)
  4158. (let ((nest 1))
  4159. (save-excursion
  4160. (catch 'done
  4161. (while (and
  4162. (/= nest 0)
  4163. (verilog-re-search-backward
  4164. "\\<\\(module\\)\\|\\(generate\\)\\|\\(endgenerate\\)\\>" nil 'move)
  4165. (cond
  4166. ((match-end 1) ; module - we have crawled out
  4167. (throw 'done 1))
  4168. ((match-end 2) ; generate
  4169. (setq nest (1- nest)))
  4170. ((match-end 3) ; endgenerate
  4171. (setq nest (1+ nest))))))))
  4172. (= nest 0) )) ; return nest
  4173. (defun verilog-in-fork-region-p ()
  4174. "Return true if between a fork and join."
  4175. (interactive)
  4176. (let ((lim (save-excursion (verilog-beg-of-defun) (point)))
  4177. (nest 1))
  4178. (save-excursion
  4179. (while (and
  4180. (/= nest 0)
  4181. (verilog-re-search-backward "\\<\\(fork\\)\\|\\(join\\(_any\\|_none\\)?\\)\\>" lim 'move)
  4182. (cond
  4183. ((match-end 1) ; fork
  4184. (setq nest (1- nest)))
  4185. ((match-end 2) ; join
  4186. (setq nest (1+ nest)))))))
  4187. (= nest 0) )) ; return nest
  4188. (defun verilog-in-deferred-immediate-final-p ()
  4189. "Return true if inside an `assert/assume/cover final' statement."
  4190. (interactive)
  4191. (and (looking-at "final")
  4192. (verilog-looking-back "\\<\\(?:assert\\|assume\\|cover\\)\\>\\s-+" nil))
  4193. )
  4194. (defun verilog-backward-case-item (lim)
  4195. "Skip backward to nearest enclosing case item.
  4196. Limit search to point LIM."
  4197. (interactive)
  4198. (let ((str 'nil)
  4199. (lim1
  4200. (progn
  4201. (save-excursion
  4202. (verilog-re-search-backward verilog-endcomment-reason-re
  4203. lim 'move)
  4204. (point)))))
  4205. ;; Try to find the real :
  4206. (if (save-excursion (search-backward ":" lim1 t))
  4207. (let ((colon 0)
  4208. b e )
  4209. (while
  4210. (and
  4211. (< colon 1)
  4212. (verilog-re-search-backward "\\(\\[\\)\\|\\(\\]\\)\\|\\(:\\)"
  4213. lim1 'move))
  4214. (cond
  4215. ((match-end 1) ; [
  4216. (setq colon (1+ colon))
  4217. (if (>= colon 0)
  4218. (error "%s: unbalanced [" (verilog-point-text))))
  4219. ((match-end 2) ; ]
  4220. (setq colon (1- colon)))
  4221. ((match-end 3) ; :
  4222. (setq colon (1+ colon)))))
  4223. ;; Skip back to beginning of case item
  4224. (skip-chars-backward "\t ")
  4225. (verilog-skip-backward-comment-or-string)
  4226. (setq e (point))
  4227. (setq b
  4228. (progn
  4229. (if
  4230. (verilog-re-search-backward
  4231. "\\<\\(randcase\\|case[zx]?\\)\\>\\|;\\|\\<end\\>" nil 'move)
  4232. (progn
  4233. (cond
  4234. ((match-end 1)
  4235. (goto-char (match-end 1))
  4236. (verilog-forward-ws&directives)
  4237. (if (looking-at "(")
  4238. (progn
  4239. (forward-sexp)
  4240. (verilog-forward-ws&directives)))
  4241. (point))
  4242. (t
  4243. (goto-char (match-end 0))
  4244. (verilog-forward-ws&directives)
  4245. (point))))
  4246. (error "Malformed case item"))))
  4247. (setq str (buffer-substring b e))
  4248. (if
  4249. (setq e
  4250. (string-match
  4251. "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
  4252. (setq str (concat (substring str 0 e) "...")))
  4253. str)
  4254. 'nil)))
  4255. ;;; Other functions:
  4256. ;;
  4257. (defun verilog-kill-existing-comment ()
  4258. "Kill auto comment on this line."
  4259. (save-excursion
  4260. (let* (
  4261. (e (progn
  4262. (end-of-line)
  4263. (point)))
  4264. (b (progn
  4265. (beginning-of-line)
  4266. (search-forward "//" e t))))
  4267. (if b
  4268. (delete-region (- b 2) e)))))
  4269. (defconst verilog-directive-nest-re
  4270. (concat "\\(`else\\>\\)\\|"
  4271. "\\(`endif\\>\\)\\|"
  4272. "\\(`if\\>\\)\\|"
  4273. "\\(`ifdef\\>\\)\\|"
  4274. "\\(`ifndef\\>\\)\\|"
  4275. "\\(`elsif\\>\\)"))
  4276. (defun verilog-set-auto-endcomments (indent-str kill-existing-comment)
  4277. "Add ending comment with given INDENT-STR.
  4278. With KILL-EXISTING-COMMENT, remove what was there before.
  4279. Insert `// case: 7 ' or `// NAME ' on this line if appropriate.
  4280. Insert `// case expr ' if this line ends a case block.
  4281. Insert `// ifdef FOO ' if this line ends code conditional on FOO.
  4282. Insert `// NAME ' if this line ends a function, task, module,
  4283. primitive or interface named NAME."
  4284. (save-excursion
  4285. (cond
  4286. (; Comment close preprocessor directives
  4287. (and
  4288. (looking-at "\\(`endif\\)\\|\\(`else\\)")
  4289. (or kill-existing-comment
  4290. (not (save-excursion
  4291. (end-of-line)
  4292. (search-backward "//" (point-at-bol) t)))))
  4293. (let ((nest 1) b e
  4294. m
  4295. (else (if (match-end 2) "!" " ")))
  4296. (end-of-line)
  4297. (if kill-existing-comment
  4298. (verilog-kill-existing-comment))
  4299. (delete-horizontal-space)
  4300. (save-excursion
  4301. (backward-sexp 1)
  4302. (while (and (/= nest 0)
  4303. (verilog-re-search-backward verilog-directive-nest-re nil 'move))
  4304. (cond
  4305. ((match-end 1) ; `else
  4306. (if (= nest 1)
  4307. (setq else "!")))
  4308. ((match-end 2) ; `endif
  4309. (setq nest (1+ nest)))
  4310. ((match-end 3) ; `if
  4311. (setq nest (1- nest)))
  4312. ((match-end 4) ; `ifdef
  4313. (setq nest (1- nest)))
  4314. ((match-end 5) ; `ifndef
  4315. (setq nest (1- nest)))
  4316. ((match-end 6) ; `elsif
  4317. (if (= nest 1)
  4318. (progn
  4319. (setq else "!")
  4320. (setq nest 0))))))
  4321. (if (match-end 0)
  4322. (setq
  4323. m (buffer-substring
  4324. (match-beginning 0)
  4325. (match-end 0))
  4326. b (progn
  4327. (skip-chars-forward "^ \t")
  4328. (verilog-forward-syntactic-ws)
  4329. (point))
  4330. e (progn
  4331. (skip-chars-forward "a-zA-Z0-9_")
  4332. (point)))))
  4333. (if b
  4334. (if (> (count-lines (point) b) verilog-minimum-comment-distance)
  4335. (insert (concat " // " else m " " (buffer-substring b e))))
  4336. (progn
  4337. (insert " // unmatched `else, `elsif or `endif")
  4338. (ding 't)))))
  4339. (; Comment close case/class/function/task/module and named block
  4340. (and (looking-at "\\<end")
  4341. (or kill-existing-comment
  4342. (not (save-excursion
  4343. (end-of-line)
  4344. (search-backward "//" (point-at-bol) t)))))
  4345. (let ((type (car indent-str)))
  4346. (unless (eq type 'declaration)
  4347. (unless (looking-at (concat "\\(" verilog-end-block-ordered-re "\\)[ \t]*:")) ; ignore named ends
  4348. (if (looking-at verilog-end-block-ordered-re)
  4349. (cond
  4350. (;- This is a case block; search back for the start of this case
  4351. (match-end 1) ; of verilog-end-block-ordered-re
  4352. (let ((err 't)
  4353. (str "UNMATCHED!!"))
  4354. (save-excursion
  4355. (verilog-leap-to-head)
  4356. (cond
  4357. ((looking-at "\\<randcase\\>")
  4358. (setq str "randcase")
  4359. (setq err nil))
  4360. ((looking-at "\\(\\(unique0?\\s-+\\|priority\\s-+\\)?case[xz]?\\)")
  4361. (goto-char (match-end 0))
  4362. (setq str (concat (match-string 0) " " (verilog-get-expr)))
  4363. (setq err nil))
  4364. ))
  4365. (end-of-line)
  4366. (if kill-existing-comment
  4367. (verilog-kill-existing-comment))
  4368. (delete-horizontal-space)
  4369. (insert (concat " // " str ))
  4370. (if err (ding 't))))
  4371. (;- This is a begin..end block
  4372. (match-end 2) ; of verilog-end-block-ordered-re
  4373. (let ((str " // UNMATCHED !!")
  4374. (err 't)
  4375. (here (point))
  4376. there
  4377. cntx)
  4378. (save-excursion
  4379. (verilog-leap-to-head)
  4380. (setq there (point))
  4381. (if (not (match-end 0))
  4382. (progn
  4383. (goto-char here)
  4384. (end-of-line)
  4385. (if kill-existing-comment
  4386. (verilog-kill-existing-comment))
  4387. (delete-horizontal-space)
  4388. (insert str)
  4389. (ding 't))
  4390. (let ((lim
  4391. (save-excursion (verilog-beg-of-defun) (point)))
  4392. (here (point)))
  4393. (cond
  4394. (;-- handle named block differently
  4395. (looking-at verilog-named-block-re)
  4396. (search-forward ":")
  4397. (setq there (point))
  4398. (setq str (verilog-get-expr))
  4399. (setq err nil)
  4400. (setq str (concat " // block: " str )))
  4401. ((verilog-in-case-region-p) ;-- handle case item differently
  4402. (goto-char here)
  4403. (setq str (verilog-backward-case-item lim))
  4404. (setq there (point))
  4405. (setq err nil)
  4406. (setq str (concat " // case: " str )))
  4407. (;- try to find "reason" for this begin
  4408. (cond
  4409. (;
  4410. (eq here (progn
  4411. ;; (verilog-backward-token)
  4412. (verilog-beg-of-statement)
  4413. (point)))
  4414. (setq err nil)
  4415. (setq str ""))
  4416. ((looking-at verilog-endcomment-reason-re)
  4417. (setq there (match-end 0))
  4418. (setq cntx (concat (match-string 0) " "))
  4419. (cond
  4420. (;- begin
  4421. (match-end 1)
  4422. (setq err nil)
  4423. (save-excursion
  4424. (if (and (verilog-continued-line)
  4425. (looking-at "\\<repeat\\>\\|\\<wait\\>\\|\\<always\\>"))
  4426. (progn
  4427. (goto-char (match-end 0))
  4428. (setq there (point))
  4429. (setq str
  4430. (concat " // " (match-string 0) " " (verilog-get-expr))))
  4431. (setq str ""))))
  4432. (;- else
  4433. (match-end 2)
  4434. (let ((nest 0)
  4435. ( reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
  4436. (catch 'skip
  4437. (while (verilog-re-search-backward reg nil 'move)
  4438. (cond
  4439. ((match-end 1) ; begin
  4440. (setq nest (1- nest)))
  4441. ((match-end 2) ; end
  4442. (setq nest (1+ nest)))
  4443. ((match-end 3)
  4444. (if (= 0 nest)
  4445. (progn
  4446. (goto-char (match-end 0))
  4447. (setq there (point))
  4448. (setq err nil)
  4449. (setq str (verilog-get-expr))
  4450. (setq str (concat " // else: !if" str ))
  4451. (throw 'skip 1))))
  4452. ((match-end 4)
  4453. (if (= 0 nest)
  4454. (progn
  4455. (goto-char (match-end 0))
  4456. (setq there (point))
  4457. (setq err nil)
  4458. (setq str (verilog-get-expr))
  4459. (setq str (concat " // else: !assert " str ))
  4460. (throw 'skip 1)))))))))
  4461. (;- end else
  4462. (match-end 3)
  4463. (goto-char there)
  4464. (let ((nest 0)
  4465. (reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|\\(\\<if\\>\\)\\|\\(assert\\)"))
  4466. (catch 'skip
  4467. (while (verilog-re-search-backward reg nil 'move)
  4468. (cond
  4469. ((match-end 1) ; begin
  4470. (setq nest (1- nest)))
  4471. ((match-end 2) ; end
  4472. (setq nest (1+ nest)))
  4473. ((match-end 3)
  4474. (if (= 0 nest)
  4475. (progn
  4476. (goto-char (match-end 0))
  4477. (setq there (point))
  4478. (setq err nil)
  4479. (setq str (verilog-get-expr))
  4480. (setq str (concat " // else: !if" str ))
  4481. (throw 'skip 1))))
  4482. ((match-end 4)
  4483. (if (= 0 nest)
  4484. (progn
  4485. (goto-char (match-end 0))
  4486. (setq there (point))
  4487. (setq err nil)
  4488. (setq str (verilog-get-expr))
  4489. (setq str (concat " // else: !assert " str ))
  4490. (throw 'skip 1)))))))))
  4491. (; always, always_comb, always_latch w/o @...
  4492. (match-end 5)
  4493. (goto-char (match-end 0))
  4494. (setq there (point))
  4495. (setq err nil)
  4496. (setq str (concat " // " cntx )))
  4497. (;- task/function/initial et cetera
  4498. t
  4499. (match-end 0)
  4500. (goto-char (match-end 0))
  4501. (setq there (point))
  4502. (setq err nil)
  4503. (setq str (concat " // " cntx (verilog-get-expr))))
  4504. (;-- otherwise...
  4505. (setq str " // auto-endcomment confused "))))
  4506. ((and
  4507. (verilog-in-case-region-p) ;-- handle case item differently
  4508. (progn
  4509. (setq there (point))
  4510. (goto-char here)
  4511. (setq str (verilog-backward-case-item lim))))
  4512. (setq err nil)
  4513. (setq str (concat " // case: " str )))
  4514. ((verilog-in-fork-region-p)
  4515. (setq err nil)
  4516. (setq str " // fork branch" ))
  4517. ((looking-at "\\<end\\>")
  4518. ;; HERE
  4519. (forward-word-strictly 1)
  4520. (verilog-forward-syntactic-ws)
  4521. (setq err nil)
  4522. (setq str (verilog-get-expr))
  4523. (setq str (concat " // " cntx str )))
  4524. ))))
  4525. (goto-char here)
  4526. (end-of-line)
  4527. (if kill-existing-comment
  4528. (verilog-kill-existing-comment))
  4529. (delete-horizontal-space)
  4530. (if (or err
  4531. (> (count-lines here there) verilog-minimum-comment-distance))
  4532. (insert str))
  4533. (if err (ding 't))
  4534. ))))
  4535. (;- this is endclass, which can be nested
  4536. (match-end 11) ; of verilog-end-block-ordered-re
  4537. ;;(goto-char there)
  4538. (let ((nest 0)
  4539. (reg "\\<\\(class\\)\\|\\(endclass\\)\\|\\(package\\|primitive\\|\\(macro\\)?module\\)\\>")
  4540. string)
  4541. (save-excursion
  4542. (catch 'skip
  4543. (while (verilog-re-search-backward reg nil 'move)
  4544. (cond
  4545. ((match-end 3) ; endclass
  4546. (ding 't)
  4547. (setq string "unmatched endclass")
  4548. (throw 'skip 1))
  4549. ((match-end 2) ; endclass
  4550. (setq nest (1+ nest)))
  4551. ((match-end 1) ; class
  4552. (setq nest (1- nest))
  4553. (if (< nest 0)
  4554. (progn
  4555. (goto-char (match-end 0))
  4556. (let (b e)
  4557. (setq b (progn
  4558. (skip-chars-forward "^ \t")
  4559. (verilog-forward-ws&directives)
  4560. (point))
  4561. e (progn
  4562. (skip-chars-forward "a-zA-Z0-9_")
  4563. (point)))
  4564. (setq string (buffer-substring b e)))
  4565. (throw 'skip 1))))
  4566. ))))
  4567. (end-of-line)
  4568. (if kill-existing-comment
  4569. (verilog-kill-existing-comment))
  4570. (delete-horizontal-space)
  4571. (insert (concat " // " string ))))
  4572. (; - this is end{function,generate,task,module,primitive,table,generate}
  4573. ;; - which can not be nested.
  4574. t
  4575. (let (string reg (name-re nil))
  4576. (end-of-line)
  4577. (if kill-existing-comment
  4578. (save-match-data
  4579. (verilog-kill-existing-comment)))
  4580. (delete-horizontal-space)
  4581. (backward-sexp)
  4582. (cond
  4583. ((match-end 5) ; of verilog-end-block-ordered-re
  4584. (setq reg "\\(\\<function\\>\\)\\|\\(\\<\\(endfunction\\|task\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
  4585. (setq name-re "\\w+\\(?:\n\\|\\s-\\)*[(;]"))
  4586. ((match-end 6) ; of verilog-end-block-ordered-re
  4587. (setq reg "\\(\\<task\\>\\)\\|\\(\\<\\(endtask\\|function\\|\\(macro\\)?module\\|primitive\\)\\>\\)")
  4588. (setq name-re "\\w+\\(?:\n\\|\\s-\\)*[(;]"))
  4589. ((match-end 7) ; of verilog-end-block-ordered-re
  4590. (setq reg "\\(\\<\\(macro\\)?module\\>\\)\\|\\<endmodule\\>"))
  4591. ((match-end 8) ; of verilog-end-block-ordered-re
  4592. (setq reg "\\(\\<primitive\\>\\)\\|\\(\\<\\(endprimitive\\|package\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
  4593. ((match-end 9) ; of verilog-end-block-ordered-re
  4594. (setq reg "\\(\\<interface\\>\\)\\|\\(\\<\\(endinterface\\|package\\|primitive\\|\\(macro\\)?module\\)\\>\\)"))
  4595. ((match-end 10) ; of verilog-end-block-ordered-re
  4596. (setq reg "\\(\\<package\\>\\)\\|\\(\\<\\(endpackage\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
  4597. ((match-end 11) ; of verilog-end-block-ordered-re
  4598. (setq reg "\\(\\<class\\>\\)\\|\\(\\<\\(endclass\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
  4599. ((match-end 12) ; of verilog-end-block-ordered-re
  4600. (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<\\(endcovergroup\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
  4601. ((match-end 13) ; of verilog-end-block-ordered-re
  4602. (setq reg "\\(\\<program\\>\\)\\|\\(\\<\\(endprogram\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
  4603. ((match-end 14) ; of verilog-end-block-ordered-re
  4604. (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<\\(endsequence\\|primitive\\|interface\\|\\(macro\\)?module\\)\\>\\)"))
  4605. ((match-end 15) ; of verilog-end-block-ordered-re
  4606. (setq reg "\\(\\<clocking\\>\\)\\|\\<endclocking\\>"))
  4607. ((match-end 16) ; of verilog-end-block-ordered-re
  4608. (setq reg "\\(\\<property\\>\\)\\|\\<endproperty\\>"))
  4609. (t (error "Problem in verilog-set-auto-endcomments")))
  4610. (let (b e)
  4611. (save-excursion
  4612. (verilog-re-search-backward reg nil 'move)
  4613. (cond
  4614. ((match-end 1)
  4615. (setq b (progn
  4616. (skip-chars-forward "^ \t")
  4617. (verilog-forward-ws&directives)
  4618. (if (looking-at "static\\|automatic")
  4619. (progn
  4620. (goto-char (match-end 0))
  4621. (verilog-forward-ws&directives)))
  4622. (if (and name-re (verilog-re-search-forward name-re nil 'move))
  4623. (progn
  4624. (goto-char (match-beginning 0))
  4625. (verilog-forward-ws&directives)))
  4626. (point))
  4627. e (progn
  4628. (skip-chars-forward "a-zA-Z0-9_")
  4629. (point)))
  4630. (setq string (buffer-substring b e)))
  4631. (t
  4632. (ding 't)
  4633. (setq string "unmatched end(function|task|module|primitive|interface|package|class|clocking)")))))
  4634. (end-of-line)
  4635. (insert (concat " // " string )))
  4636. ))))))))))
  4637. (defun verilog-get-expr()
  4638. "Grab expression at point, e.g., case ( a | b & (c ^d))."
  4639. (let* ((b (progn
  4640. (verilog-forward-syntactic-ws)
  4641. (skip-chars-forward " \t")
  4642. (point)))
  4643. (e (let ((par 1))
  4644. (cond
  4645. ((looking-at "@")
  4646. (forward-char 1)
  4647. (verilog-forward-syntactic-ws)
  4648. (if (looking-at "(")
  4649. (progn
  4650. (forward-char 1)
  4651. (while (and (/= par 0)
  4652. (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
  4653. (cond
  4654. ((match-end 1)
  4655. (setq par (1+ par)))
  4656. ((match-end 2)
  4657. (setq par (1- par)))))))
  4658. (point))
  4659. ((looking-at "(")
  4660. (forward-char 1)
  4661. (while (and (/= par 0)
  4662. (verilog-re-search-forward "\\((\\)\\|\\()\\)" nil 'move))
  4663. (cond
  4664. ((match-end 1)
  4665. (setq par (1+ par)))
  4666. ((match-end 2)
  4667. (setq par (1- par)))))
  4668. (point))
  4669. ((looking-at "\\[")
  4670. (forward-char 1)
  4671. (while (and (/= par 0)
  4672. (verilog-re-search-forward "\\(\\[\\)\\|\\(\\]\\)" nil 'move))
  4673. (cond
  4674. ((match-end 1)
  4675. (setq par (1+ par)))
  4676. ((match-end 2)
  4677. (setq par (1- par)))))
  4678. (verilog-forward-syntactic-ws)
  4679. (skip-chars-forward "^ \t\n\f")
  4680. (point))
  4681. ((looking-at "/[/\\*]")
  4682. b)
  4683. ('t
  4684. (skip-chars-forward "^: \t\n\f")
  4685. (point)))))
  4686. (str (buffer-substring b e)))
  4687. (if (setq e (string-match "[ \t]*\\(\\(\n\\)\\|\\(//\\)\\|\\(/\\*\\)\\)" str))
  4688. (setq str (concat (substring str 0 e) "...")))
  4689. str))
  4690. (defun verilog-expand-vector ()
  4691. "Take a signal vector on the current line and expand it to multiple lines.
  4692. Useful for creating tri's and other expanded fields."
  4693. (interactive)
  4694. (verilog-expand-vector-internal "[" "]"))
  4695. (defun verilog-expand-vector-internal (bra ket)
  4696. "Given BRA, the start brace and KET, the end brace, expand one line into many lines."
  4697. (save-excursion
  4698. (forward-line 0)
  4699. (let ((signal-string (buffer-substring (point)
  4700. (progn
  4701. (end-of-line) (point)))))
  4702. (if (string-match
  4703. (concat "\\(.*\\)"
  4704. (regexp-quote bra)
  4705. "\\([0-9]*\\)\\(:[0-9]*\\|\\)\\(::[0-9---]*\\|\\)"
  4706. (regexp-quote ket)
  4707. "\\(.*\\)$") signal-string)
  4708. (let* ((sig-head (match-string 1 signal-string))
  4709. (vec-start (string-to-number (match-string 2 signal-string)))
  4710. (vec-end (if (= (match-beginning 3) (match-end 3))
  4711. vec-start
  4712. (string-to-number
  4713. (substring signal-string (1+ (match-beginning 3))
  4714. (match-end 3)))))
  4715. (vec-range
  4716. (if (= (match-beginning 4) (match-end 4))
  4717. 1
  4718. (string-to-number
  4719. (substring signal-string (+ 2 (match-beginning 4))
  4720. (match-end 4)))))
  4721. (sig-tail (match-string 5 signal-string))
  4722. vec)
  4723. ;; Decode vectors
  4724. (setq vec nil)
  4725. (if (< vec-range 0)
  4726. (let ((tmp vec-start))
  4727. (setq vec-start vec-end
  4728. vec-end tmp
  4729. vec-range (- vec-range))))
  4730. (if (< vec-end vec-start)
  4731. (while (<= vec-end vec-start)
  4732. (setq vec (append vec (list vec-start)))
  4733. (setq vec-start (- vec-start vec-range)))
  4734. (while (<= vec-start vec-end)
  4735. (setq vec (append vec (list vec-start)))
  4736. (setq vec-start (+ vec-start vec-range))))
  4737. ;;
  4738. ;; Delete current line
  4739. (delete-region (point) (progn (forward-line 0) (point)))
  4740. ;;
  4741. ;; Expand vector
  4742. (while vec
  4743. (insert (concat sig-head bra
  4744. (int-to-string (car vec)) ket sig-tail "\n"))
  4745. (setq vec (cdr vec)))
  4746. (delete-char -1)
  4747. ;;
  4748. )))))
  4749. (defun verilog-strip-comments ()
  4750. "Strip all comments from the Verilog code."
  4751. (interactive)
  4752. (goto-char (point-min))
  4753. (while (re-search-forward "//" nil t)
  4754. (if (verilog-within-string)
  4755. (re-search-forward "\"" nil t)
  4756. (if (verilog-in-star-comment-p)
  4757. (re-search-forward "\\*/" nil t)
  4758. (let ((bpt (- (point) 2)))
  4759. (end-of-line)
  4760. (delete-region bpt (point))))))
  4761. ;;
  4762. (goto-char (point-min))
  4763. (while (re-search-forward "/\\*" nil t)
  4764. (if (verilog-within-string)
  4765. (re-search-forward "\"" nil t)
  4766. (let ((bpt (- (point) 2)))
  4767. (re-search-forward "\\*/")
  4768. (delete-region bpt (point))))))
  4769. (defun verilog-one-line ()
  4770. "Convert structural Verilog instances to occupy one line."
  4771. (interactive)
  4772. (goto-char (point-min))
  4773. (while (re-search-forward "\\([^;]\\)[ \t]*\n[ \t]*" nil t)
  4774. (replace-match "\\1 " nil nil)))
  4775. (defun verilog-linter-name ()
  4776. "Return name of linter, either surelint or verilint."
  4777. (let ((compile-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
  4778. compile-command))
  4779. (lint-word1 (verilog-string-replace-matches "\\s .*$" "" nil nil
  4780. verilog-linter)))
  4781. (cond ((equal compile-word1 "surelint") `surelint)
  4782. ((equal compile-word1 "verilint") `verilint)
  4783. ((equal lint-word1 "surelint") `surelint)
  4784. ((equal lint-word1 "verilint") `verilint)
  4785. (t `surelint)))) ; back compatibility
  4786. (defun verilog-lint-off ()
  4787. "Convert a Verilog linter warning line into a disable statement.
  4788. For example:
  4789. pci_bfm_null.v, line 46: Unused input: pci_rst_
  4790. becomes a comment for the appropriate tool.
  4791. The first word of the `compile-command' or `verilog-linter'
  4792. variables is used to determine which product is being used.
  4793. See \\[verilog-surelint-off] and \\[verilog-verilint-off]."
  4794. (interactive)
  4795. (let ((linter (verilog-linter-name)))
  4796. (cond ((equal linter `surelint)
  4797. (verilog-surelint-off))
  4798. ((equal linter `verilint)
  4799. (verilog-verilint-off))
  4800. (t (error "Linter name not set")))))
  4801. (defvar compilation-last-buffer)
  4802. (defvar next-error-last-buffer)
  4803. (defun verilog-surelint-off ()
  4804. "Convert a SureLint warning line into a disable statement.
  4805. Run from Verilog source window; assumes there is a *compile* buffer
  4806. with point set appropriately.
  4807. For example:
  4808. WARNING [STD-UDDONX]: xx.v, line 8: output out is never assigned.
  4809. becomes:
  4810. // surefire lint_line_off UDDONX"
  4811. (interactive)
  4812. (let ((buff (if (boundp 'next-error-last-buffer)
  4813. next-error-last-buffer
  4814. compilation-last-buffer)))
  4815. (when (buffer-live-p buff)
  4816. (save-excursion
  4817. (switch-to-buffer buff)
  4818. (beginning-of-line)
  4819. (when
  4820. (looking-at "\\(INFO\\|WARNING\\|ERROR\\) \\[[^-]+-\\([^]]+\\)\\]: \\([^,]+\\), line \\([0-9]+\\): \\(.*\\)$")
  4821. (let* ((code (match-string 2))
  4822. (file (match-string 3))
  4823. (line (match-string 4))
  4824. (buffer (get-file-buffer file))
  4825. dir filename)
  4826. (unless buffer
  4827. (progn
  4828. (setq buffer
  4829. (and (file-exists-p file)
  4830. (find-file-noselect file)))
  4831. (or buffer
  4832. (let* ((pop-up-windows t))
  4833. (let ((name (expand-file-name
  4834. (read-file-name
  4835. (format "Find this error in: (default %s) "
  4836. file)
  4837. dir file t))))
  4838. (if (file-directory-p name)
  4839. (setq name (expand-file-name filename name)))
  4840. (setq buffer
  4841. (and (file-exists-p name)
  4842. (find-file-noselect name))))))))
  4843. (switch-to-buffer buffer)
  4844. (goto-char (point-min))
  4845. (forward-line (- (string-to-number line)))
  4846. (end-of-line)
  4847. (catch 'already
  4848. (cond
  4849. ((verilog-in-slash-comment-p)
  4850. (re-search-backward "//")
  4851. (cond
  4852. ((looking-at "// surefire lint_off_line ")
  4853. (goto-char (match-end 0))
  4854. (let ((lim (point-at-eol)))
  4855. (if (re-search-forward code lim 'move)
  4856. (throw 'already t)
  4857. (insert (concat " " code)))))
  4858. (t
  4859. )))
  4860. ((verilog-in-star-comment-p)
  4861. (re-search-backward "/\\*")
  4862. (insert (format " // surefire lint_off_line %6s" code )))
  4863. (t
  4864. (insert (format " // surefire lint_off_line %6s" code ))
  4865. )))))))))
  4866. (defun verilog-verilint-off ()
  4867. "Convert a Verilint warning line into a disable statement.
  4868. For example:
  4869. (W240) pci_bfm_null.v, line 46: Unused input: pci_rst_
  4870. becomes:
  4871. //Verilint 240 off // WARNING: Unused input"
  4872. (interactive)
  4873. (save-excursion
  4874. (beginning-of-line)
  4875. (when (looking-at "\\(.*\\)([WE]\\([0-9A-Z]+\\)).*,\\s +line\\s +[0-9]+:\\s +\\([^:\n]+\\):?.*$")
  4876. (replace-match (format
  4877. ;; %3s makes numbers 1-999 line up nicely
  4878. "\\1//Verilint %3s off // WARNING: \\3"
  4879. (match-string 2)))
  4880. (beginning-of-line)
  4881. (verilog-indent-line))))
  4882. (defun verilog-auto-save-compile ()
  4883. "Update automatics with \\[verilog-auto], save the buffer, and compile."
  4884. (interactive)
  4885. (verilog-auto) ; Always do it for safety
  4886. (save-buffer)
  4887. (compile compile-command))
  4888. (defun verilog-preprocess (&optional command filename)
  4889. "Preprocess the buffer, similar to `compile', but put output in Verilog-Mode.
  4890. Takes optional COMMAND or defaults to `verilog-preprocessor', and
  4891. FILENAME to find directory to run in, or defaults to `buffer-file-name'."
  4892. (interactive
  4893. (list
  4894. (let ((default (verilog-expand-command verilog-preprocessor)))
  4895. (set (make-local-variable `verilog-preprocessor)
  4896. (read-from-minibuffer "Run Preprocessor (like this): "
  4897. default nil nil
  4898. 'verilog-preprocess-history default)))))
  4899. (unless command (setq command (verilog-expand-command verilog-preprocessor)))
  4900. (let* ((fontlocked (and (boundp 'font-lock-mode) font-lock-mode))
  4901. (dir (file-name-directory (or filename buffer-file-name)))
  4902. (cmd (concat "cd " dir "; " command)))
  4903. (with-output-to-temp-buffer "*Verilog-Preprocessed*"
  4904. (with-current-buffer (get-buffer "*Verilog-Preprocessed*")
  4905. (insert (concat "// " cmd "\n"))
  4906. (call-process shell-file-name nil t nil shell-command-switch cmd)
  4907. (verilog-mode)
  4908. ;; Without this force, it takes a few idle seconds
  4909. ;; to get the color, which is very jarring
  4910. (unless (fboundp 'font-lock-ensure)
  4911. ;; We should use font-lock-ensure in preference to
  4912. ;; font-lock-fontify-buffer, but IIUC the problem this is supposed to
  4913. ;; solve only appears in Emacsen older than font-lock-ensure anyway.
  4914. ;; So avoid bytecomp's interactive-only by going through intern.
  4915. (when fontlocked (funcall (intern "font-lock-fontify-buffer"))))))))
  4916. ;;; Batch:
  4917. ;;
  4918. (defun verilog-warn (string &rest args)
  4919. "Print a warning with `format' using STRING and optional ARGS."
  4920. (apply 'message (concat "%%Warning: " string) args))
  4921. (defun verilog-warn-error (string &rest args)
  4922. "Call `error' using STRING and optional ARGS.
  4923. If `verilog-warn-fatal' is non-nil, call `verilog-warn' instead."
  4924. (if verilog-warn-fatal
  4925. (apply 'error string args)
  4926. (apply 'verilog-warn string args)))
  4927. (defmacro verilog-batch-error-wrapper (&rest body)
  4928. "Execute BODY and add error prefix to any errors found.
  4929. This lets programs calling batch mode to easily extract error messages."
  4930. `(let ((verilog-warn-fatal nil))
  4931. (condition-case err
  4932. (progn ,@body)
  4933. (error
  4934. (error "%%Error: %s%s" (error-message-string err)
  4935. (if (featurep 'xemacs) "\n" "")))))) ; XEmacs forgets to add a newline
  4936. (defun verilog-batch-execute-func (funref &optional no-save)
  4937. "Internal processing of a batch command.
  4938. Runs FUNREF on all command arguments.
  4939. Save the result unless optional NO-SAVE is t."
  4940. (verilog-batch-error-wrapper
  4941. ;; Setting global variables like that is *VERY NASTY* !!! --Stef
  4942. ;; However, this function is called only when Emacs is being used as
  4943. ;; a standalone language instead of as an editor, so we'll live.
  4944. ;;
  4945. ;; General globals needed
  4946. (setq make-backup-files nil)
  4947. (setq-default make-backup-files nil)
  4948. (setq enable-local-variables t)
  4949. (setq enable-local-eval t)
  4950. (setq create-lockfiles nil)
  4951. ;; Make sure any sub-files we read get proper mode
  4952. (setq-default major-mode 'verilog-mode)
  4953. ;; Ditto files already read in
  4954. ;; Remember buffer list, so don't later pickup any verilog-getopt files
  4955. (let ((orig-buffer-list (buffer-list)))
  4956. (mapc (lambda (buf)
  4957. (when (buffer-file-name buf)
  4958. (with-current-buffer buf
  4959. (verilog-mode)
  4960. (verilog-auto-reeval-locals)
  4961. (verilog-getopt-flags))))
  4962. orig-buffer-list)
  4963. ;; Process the files
  4964. (mapcar (lambda (buf)
  4965. (when (buffer-file-name buf)
  4966. (save-excursion
  4967. (if (not (file-exists-p (buffer-file-name buf)))
  4968. (error
  4969. "File not found: %s" (buffer-file-name buf)))
  4970. (message "Processing %s" (buffer-file-name buf))
  4971. (set-buffer buf)
  4972. (funcall funref)
  4973. (when (and (not no-save)
  4974. (buffer-modified-p)) ; Avoid "no changes to be saved"
  4975. (save-buffer)))))
  4976. orig-buffer-list))))
  4977. (defun verilog-batch-auto ()
  4978. "For use with --batch, perform automatic expansions as a stand-alone tool.
  4979. This sets up the appropriate Verilog mode environment, updates automatics
  4980. with \\[verilog-auto] on all command-line files, and saves the buffers.
  4981. For proper results, multiple filenames need to be passed on the command
  4982. line in bottom-up order."
  4983. (unless noninteractive
  4984. (error "Use verilog-batch-auto only with --batch")) ; Otherwise we'd mess up buffer modes
  4985. (verilog-batch-execute-func `verilog-auto))
  4986. (defun verilog-batch-delete-auto ()
  4987. "For use with --batch, perform automatic deletion as a stand-alone tool.
  4988. This sets up the appropriate Verilog mode environment, deletes automatics
  4989. with \\[verilog-delete-auto] on all command-line files, and saves the buffers."
  4990. (unless noninteractive
  4991. (error "Use verilog-batch-delete-auto only with --batch")) ; Otherwise we'd mess up buffer modes
  4992. (verilog-batch-execute-func `verilog-delete-auto))
  4993. (defun verilog-batch-delete-trailing-whitespace ()
  4994. "For use with --batch, perform whitespace deletion as a stand-alone tool.
  4995. This sets up the appropriate Verilog mode environment, removes
  4996. whitespace with \\[verilog-delete-trailing-whitespace] on all
  4997. command-line files, and saves the buffers."
  4998. (unless noninteractive
  4999. (error "Use verilog-batch-delete-trailing-whitespace only with --batch")) ; Otherwise we'd mess up buffer modes
  5000. (verilog-batch-execute-func `verilog-delete-trailing-whitespace))
  5001. (defun verilog-batch-diff-auto ()
  5002. "For use with --batch, perform automatic differences as a stand-alone tool.
  5003. This sets up the appropriate Verilog mode environment, expand automatics
  5004. with \\[verilog-diff-auto] on all command-line files, and reports an error
  5005. if any differences are observed. This is appropriate for adding to regressions
  5006. to insure automatics are always properly maintained."
  5007. (unless noninteractive
  5008. (error "Use verilog-batch-diff-auto only with --batch")) ; Otherwise we'd mess up buffer modes
  5009. (verilog-batch-execute-func `verilog-diff-auto t))
  5010. (defun verilog-batch-inject-auto ()
  5011. "For use with --batch, perform automatic injection as a stand-alone tool.
  5012. This sets up the appropriate Verilog mode environment, injects new automatics
  5013. with \\[verilog-inject-auto] on all command-line files, and saves the buffers.
  5014. For proper results, multiple filenames need to be passed on the command
  5015. line in bottom-up order."
  5016. (unless noninteractive
  5017. (error "Use verilog-batch-inject-auto only with --batch")) ; Otherwise we'd mess up buffer modes
  5018. (verilog-batch-execute-func `verilog-inject-auto))
  5019. (defun verilog-batch-indent ()
  5020. "For use with --batch, reindent an entire file as a stand-alone tool.
  5021. This sets up the appropriate Verilog mode environment, calls
  5022. \\[verilog-indent-buffer] on all command-line files, and saves the buffers."
  5023. (unless noninteractive
  5024. (error "Use verilog-batch-indent only with --batch")) ; Otherwise we'd mess up buffer modes
  5025. (verilog-batch-execute-func `verilog-indent-buffer))
  5026. ;;; Indentation:
  5027. ;;
  5028. (defconst verilog-indent-alist
  5029. '((block . (+ ind verilog-indent-level))
  5030. (case . (+ ind verilog-case-indent))
  5031. (cparenexp . (+ ind verilog-indent-level))
  5032. (cexp . (+ ind verilog-cexp-indent))
  5033. (defun . verilog-indent-level-module)
  5034. (declaration . verilog-indent-level-declaration)
  5035. (directive . (verilog-calculate-indent-directive))
  5036. (tf . verilog-indent-level)
  5037. (behavioral . (+ verilog-indent-level-behavioral verilog-indent-level-module))
  5038. (statement . ind)
  5039. (cpp . 0)
  5040. (comment . (verilog-comment-indent))
  5041. (unknown . 3)
  5042. (string . 0)))
  5043. (defun verilog-continued-line-1 (lim)
  5044. "Return true if this is a continued line.
  5045. Set point to where line starts. Limit search to point LIM."
  5046. (let ((continued 't))
  5047. (if (eq 0 (forward-line -1))
  5048. (progn
  5049. (end-of-line)
  5050. (verilog-backward-ws&directives lim)
  5051. (if (bobp)
  5052. (setq continued nil)
  5053. (setq continued (verilog-backward-token))))
  5054. (setq continued nil))
  5055. continued))
  5056. (defun verilog-calculate-indent ()
  5057. "Calculate the indent of the current Verilog line.
  5058. Examine previous lines. Once a line is found that is definitive as to the
  5059. type of the current line, return that lines' indent level and its type.
  5060. Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
  5061. (save-excursion
  5062. (let* ((starting_position (point))
  5063. (case-fold-search nil)
  5064. (par 0)
  5065. (begin (looking-at "[ \t]*begin\\>"))
  5066. (lim (save-excursion (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)" nil t)))
  5067. (structres nil)
  5068. (type (catch 'nesting
  5069. ;; Keep working backwards until we can figure out
  5070. ;; what type of statement this is.
  5071. ;; Basically we need to figure out
  5072. ;; 1) if this is a continuation of the previous line;
  5073. ;; 2) are we in a block scope (begin..end)
  5074. ;; if we are in a comment, done.
  5075. (if (verilog-in-star-comment-p)
  5076. (throw 'nesting 'comment))
  5077. ;; if we have a directive, done.
  5078. (if (save-excursion (beginning-of-line)
  5079. (and (looking-at verilog-directive-re-1)
  5080. (not (or (looking-at "[ \t]*`[ou]vm_")
  5081. (looking-at "[ \t]*`vmm_")))))
  5082. (throw 'nesting 'directive))
  5083. ;; indent structs as if there were module level
  5084. (setq structres (verilog-in-struct-nested-p))
  5085. (cond ((not structres) nil)
  5086. ;;((and structres (equal (char-after) ?\})) (throw 'nesting 'struct-close))
  5087. ((> structres 0) (throw 'nesting 'nested-struct))
  5088. ((= structres 0) (throw 'nesting 'block))
  5089. (t nil))
  5090. ;; if we are in a parenthesized list, and the user likes to indent these, return.
  5091. ;; unless we are in the newfangled coverpoint or constraint blocks
  5092. (if (and
  5093. verilog-indent-lists
  5094. (verilog-in-paren)
  5095. (not (verilog-in-coverage-p))
  5096. )
  5097. (progn (setq par 1)
  5098. (throw 'nesting 'block)))
  5099. ;; See if we are continuing a previous line
  5100. (while t
  5101. ;; trap out if we crawl off the top of the buffer
  5102. (if (bobp) (throw 'nesting 'cpp))
  5103. (if (and (verilog-continued-line-1 lim)
  5104. (or (not (verilog-in-coverage-p))
  5105. (looking-at verilog-in-constraint-re) )) ; may still get hosed if concat in constraint
  5106. (let ((sp (point)))
  5107. (if (and
  5108. (not (looking-at verilog-complete-reg))
  5109. (verilog-continued-line-1 lim))
  5110. (progn (goto-char sp)
  5111. (throw 'nesting 'cexp))
  5112. (goto-char sp))
  5113. (if (and (verilog-in-coverage-p)
  5114. (looking-at verilog-in-constraint-re))
  5115. (progn
  5116. (beginning-of-line)
  5117. (skip-chars-forward " \t")
  5118. (throw 'nesting 'constraint)))
  5119. (if (and begin
  5120. (not verilog-indent-begin-after-if)
  5121. (looking-at verilog-no-indent-begin-re))
  5122. (progn
  5123. (beginning-of-line)
  5124. (skip-chars-forward " \t")
  5125. (throw 'nesting 'statement))
  5126. (progn
  5127. (throw 'nesting 'cexp))))
  5128. ;; not a continued line
  5129. (goto-char starting_position))
  5130. (if (looking-at "\\<else\\>")
  5131. ;; search back for governing if, striding across begin..end pairs
  5132. ;; appropriately
  5133. (let ((elsec 1))
  5134. (while (verilog-re-search-backward verilog-ends-re nil 'move)
  5135. (cond
  5136. ((match-end 1) ; else, we're in deep
  5137. (setq elsec (1+ elsec)))
  5138. ((match-end 2) ; if
  5139. (setq elsec (1- elsec))
  5140. (if (= 0 elsec)
  5141. (if verilog-align-ifelse
  5142. (throw 'nesting 'statement)
  5143. (progn ; back up to first word on this line
  5144. (beginning-of-line)
  5145. (verilog-forward-syntactic-ws)
  5146. (throw 'nesting 'statement)))))
  5147. ((match-end 3) ; assert block
  5148. (setq elsec (1- elsec))
  5149. (verilog-beg-of-statement) ; doesn't get to beginning
  5150. (if (looking-at verilog-property-re)
  5151. (throw 'nesting 'statement) ; We don't need an endproperty for these
  5152. (throw 'nesting 'block) ; We still need an endproperty
  5153. ))
  5154. (t ; endblock
  5155. ;; try to leap back to matching outward block by striding across
  5156. ;; indent level changing tokens then immediately
  5157. ;; previous line governs indentation.
  5158. (let (( reg) (nest 1))
  5159. ;; verilog-ends => else|if|end|join(_any|_none|)|endcase|endclass|endtable|endspecify|endfunction|endtask|endgenerate|endgroup
  5160. (cond
  5161. ((match-end 4) ; end
  5162. ;; Search back for matching begin
  5163. (setq reg "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)" ))
  5164. ((match-end 5) ; endcase
  5165. ;; Search back for matching case
  5166. (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
  5167. ((match-end 6) ; endfunction
  5168. ;; Search back for matching function
  5169. (setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
  5170. ((match-end 7) ; endtask
  5171. ;; Search back for matching task
  5172. (setq reg "\\(\\<task\\>\\)\\|\\(\\<endtask\\>\\)" ))
  5173. ((match-end 8) ; endspecify
  5174. ;; Search back for matching specify
  5175. (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
  5176. ((match-end 9) ; endtable
  5177. ;; Search back for matching table
  5178. (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
  5179. ((match-end 10) ; endgenerate
  5180. ;; Search back for matching generate
  5181. (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
  5182. ((match-end 11) ; joins
  5183. ;; Search back for matching fork
  5184. (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|none\\)?\\>\\)" ))
  5185. ((match-end 12) ; class
  5186. ;; Search back for matching class
  5187. (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
  5188. ((match-end 13) ; covergroup
  5189. ;; Search back for matching covergroup
  5190. (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" )))
  5191. (catch 'skip
  5192. (while (verilog-re-search-backward reg nil 'move)
  5193. (cond
  5194. ((match-end 1) ; begin
  5195. (setq nest (1- nest))
  5196. (if (= 0 nest)
  5197. (throw 'skip 1)))
  5198. ((match-end 2) ; end
  5199. (setq nest (1+ nest)))))
  5200. )))))))
  5201. (throw 'nesting (verilog-calc-1)))
  5202. ) ; catch nesting
  5203. ) ; type
  5204. )
  5205. ;; Return type of block and indent level.
  5206. (if (not type)
  5207. (setq type 'cpp))
  5208. (if (> par 0) ; Unclosed Parenthesis
  5209. (list 'cparenexp par)
  5210. (cond
  5211. ((eq type 'case)
  5212. (list type (verilog-case-indent-level)))
  5213. ((eq type 'statement)
  5214. (list type (current-column)))
  5215. ((eq type 'defun)
  5216. (list type 0))
  5217. ((eq type 'constraint)
  5218. (list 'block (current-column)))
  5219. ((eq type 'nested-struct)
  5220. (list 'block structres))
  5221. (t
  5222. (list type (verilog-current-indent-level))))))))
  5223. (defun verilog-wai ()
  5224. "Show matching nesting block for debugging."
  5225. (interactive)
  5226. (save-excursion
  5227. (let* ((type (verilog-calc-1))
  5228. depth)
  5229. ;; Return type of block and indent level.
  5230. (if (not type)
  5231. (setq type 'cpp))
  5232. (if (and
  5233. verilog-indent-lists
  5234. (not(or (verilog-in-coverage-p)
  5235. (verilog-in-struct-p)))
  5236. (verilog-in-paren))
  5237. (setq depth 1)
  5238. (cond
  5239. ((eq type 'case)
  5240. (setq depth (verilog-case-indent-level)))
  5241. ((eq type 'statement)
  5242. (setq depth (current-column)))
  5243. ((eq type 'defun)
  5244. (setq depth 0))
  5245. (t
  5246. (setq depth (verilog-current-indent-level)))))
  5247. (message "You are at nesting %s depth %d" type depth))))
  5248. (defun verilog-calc-1 ()
  5249. (catch 'nesting
  5250. (let ((re (concat "\\({\\|}\\|" verilog-indent-re "\\)"))
  5251. (inconstraint (verilog-in-coverage-p)))
  5252. (while (verilog-re-search-backward re nil 'move)
  5253. (catch 'continue
  5254. (cond
  5255. ((equal (char-after) ?\{)
  5256. ;; block type returned based on outer constraint { or inner
  5257. (if (verilog-at-constraint-p)
  5258. (cond (inconstraint
  5259. (beginning-of-line nil)
  5260. (skip-chars-forward " \t")
  5261. (throw 'nesting 'constraint))
  5262. (t
  5263. (throw 'nesting 'statement)))))
  5264. ((equal (char-after) ?\})
  5265. (let (par-pos
  5266. (there (verilog-at-close-constraint-p)))
  5267. (if there ; we are at the } that closes a constraint. Find the { that opens it
  5268. (progn
  5269. (if (> (verilog-in-paren-count) 0)
  5270. (forward-char 1))
  5271. (setq par-pos (verilog-parenthesis-depth))
  5272. (cond (par-pos
  5273. (goto-char par-pos)
  5274. (forward-char 1))
  5275. (t
  5276. (backward-char 1)))))))
  5277. ((looking-at verilog-beg-block-re-ordered)
  5278. (cond
  5279. ((match-end 2) ; *sigh* could be "unique case" or "priority casex"
  5280. (let ((here (point)))
  5281. (verilog-beg-of-statement)
  5282. (if (looking-at verilog-extended-case-re)
  5283. (throw 'nesting 'case)
  5284. (goto-char here)))
  5285. (throw 'nesting 'case))
  5286. ((match-end 4) ; *sigh* could be "disable fork"
  5287. (let ((here (point)))
  5288. (verilog-beg-of-statement)
  5289. (if (looking-at verilog-disable-fork-re)
  5290. t ; this is a normal statement
  5291. (progn ; or is fork, starts a new block
  5292. (goto-char here)
  5293. (throw 'nesting 'block)))))
  5294. ((match-end 17) ; *sigh* might be a clocking declaration
  5295. (let ((here (point)))
  5296. (cond ((verilog-in-paren)
  5297. t) ; this is a normal statement
  5298. ((save-excursion
  5299. (verilog-beg-of-statement)
  5300. (looking-at verilog-default-clocking-re))
  5301. t) ; default clocking, normal statement
  5302. (t
  5303. (goto-char here) ; or is clocking, starts a new block
  5304. (throw 'nesting 'block)))))
  5305. ;; need to consider typedef struct here...
  5306. ((looking-at "\\<class\\|struct\\|function\\|task\\>")
  5307. ;; *sigh* These words have an optional prefix:
  5308. ;; extern {virtual|protected}? function a();
  5309. ;; typedef class foo;
  5310. ;; and we don't want to confuse this with
  5311. ;; function a();
  5312. ;; property
  5313. ;; ...
  5314. ;; endfunction
  5315. (verilog-beg-of-statement)
  5316. (cond
  5317. ((looking-at verilog-dpi-import-export-re)
  5318. (throw 'continue 'foo))
  5319. ((looking-at "\\<pure\\>\\s-+\\<virtual\\>\\s-+\\(?:\\<\\(local\\|protected\\|static\\)\\>\\s-+\\)?\\<\\(function\\|task\\)\\>\\s-+")
  5320. (throw 'nesting 'statement))
  5321. ((looking-at verilog-beg-block-re-ordered)
  5322. (throw 'nesting 'block))
  5323. (t
  5324. (throw 'nesting 'defun))))
  5325. ;;
  5326. ((looking-at "\\<property\\>")
  5327. ;; *sigh*
  5328. ;; {assert|assume|cover} property (); are complete
  5329. ;; and could also be labeled: - foo: assert property
  5330. ;; but
  5331. ;; property ID () ... needs end_property
  5332. (verilog-beg-of-statement)
  5333. (if (looking-at verilog-property-re)
  5334. (throw 'continue 'statement) ; We don't need an endproperty for these
  5335. (throw 'nesting 'block) ;We still need an endproperty
  5336. ))
  5337. (t (throw 'nesting 'block))))
  5338. ((looking-at verilog-end-block-re)
  5339. (verilog-leap-to-head)
  5340. (if (verilog-in-case-region-p)
  5341. (progn
  5342. (verilog-leap-to-case-head)
  5343. (if (looking-at verilog-extended-case-re)
  5344. (throw 'nesting 'case)))))
  5345. ((looking-at verilog-defun-level-re)
  5346. (if (looking-at verilog-defun-level-generate-only-re)
  5347. (if (or (verilog-in-generate-region-p)
  5348. (verilog-in-deferred-immediate-final-p))
  5349. (throw 'continue 'foo) ; always block in a generate - keep looking
  5350. (throw 'nesting 'defun))
  5351. (throw 'nesting 'defun)))
  5352. ((looking-at verilog-cpp-level-re)
  5353. (throw 'nesting 'cpp))
  5354. ((bobp)
  5355. (throw 'nesting 'cpp)))))
  5356. (throw 'nesting 'cpp))))
  5357. (defun verilog-calculate-indent-directive ()
  5358. "Return indentation level for directive.
  5359. For speed, the searcher looks at the last directive, not the indent
  5360. of the appropriate enclosing block."
  5361. (let ((base -1) ; Indent of the line that determines our indentation
  5362. (ind 0)) ; Relative offset caused by other directives (like `endif on same line as `else)
  5363. ;; Start at current location, scan back for another directive
  5364. (save-excursion
  5365. (beginning-of-line)
  5366. (while (and (< base 0)
  5367. (verilog-re-search-backward verilog-directive-re nil t))
  5368. (cond ((save-excursion (skip-chars-backward " \t") (bolp))
  5369. (setq base (current-indentation))))
  5370. (cond ((and (looking-at verilog-directive-end) (< base 0)) ; Only matters when not at BOL
  5371. (setq ind (- ind verilog-indent-level-directive)))
  5372. ((and (looking-at verilog-directive-middle) (>= base 0)) ; Only matters when at BOL
  5373. (setq ind (+ ind verilog-indent-level-directive)))
  5374. ((looking-at verilog-directive-begin)
  5375. (setq ind (+ ind verilog-indent-level-directive)))))
  5376. ;; Adjust indent to starting indent of critical line
  5377. (setq ind (max 0 (+ ind base))))
  5378. (save-excursion
  5379. (beginning-of-line)
  5380. (skip-chars-forward " \t")
  5381. (cond ((or (looking-at verilog-directive-middle)
  5382. (looking-at verilog-directive-end))
  5383. (setq ind (max 0 (- ind verilog-indent-level-directive))))))
  5384. ind))
  5385. (defun verilog-leap-to-case-head ()
  5386. (let ((nest 1))
  5387. (while (/= 0 nest)
  5388. (verilog-re-search-backward
  5389. (concat
  5390. "\\(\\<randcase\\>\\|\\(\\<unique0?\\s-+\\|priority\\s-+\\)?\\<case[xz]?\\>\\)"
  5391. "\\|\\(\\<endcase\\>\\)" )
  5392. nil 'move)
  5393. (cond
  5394. ((match-end 1)
  5395. (let ((here (point)))
  5396. (verilog-beg-of-statement)
  5397. (unless (looking-at verilog-extended-case-re)
  5398. (goto-char here)))
  5399. (setq nest (1- nest)))
  5400. ((match-end 3)
  5401. (setq nest (1+ nest)))
  5402. ((bobp)
  5403. (ding 't)
  5404. (setq nest 0))))))
  5405. (defun verilog-leap-to-head ()
  5406. "Move point to the head of this block.
  5407. Jump from end to matching begin, from endcase to matching case, and so on."
  5408. (let ((reg nil)
  5409. snest
  5410. (nesting 'yes)
  5411. (nest 1))
  5412. (cond
  5413. ((looking-at "\\<end\\>")
  5414. ;; 1: Search back for matching begin
  5415. (setq reg (concat "\\(\\<begin\\>\\)\\|\\(\\<end\\>\\)\\|"
  5416. "\\(\\<endcase\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" )))
  5417. ((looking-at "\\<endtask\\>")
  5418. ;; 2: Search back for matching task
  5419. (setq reg "\\(\\<task\\>\\)\\|\\(\\(\\<\\(virtual\\|protected\\|static\\)\\>\\s-+\\)+\\<task\\>\\)")
  5420. (setq nesting 'no))
  5421. ((looking-at "\\<endcase\\>")
  5422. (catch 'nesting
  5423. (verilog-leap-to-case-head) )
  5424. (setq reg nil) ; to force skip
  5425. )
  5426. ((looking-at "\\<join\\(_any\\|_none\\)?\\>")
  5427. ;; 4: Search back for matching fork
  5428. (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
  5429. ((looking-at "\\<endclass\\>")
  5430. ;; 5: Search back for matching class
  5431. (setq reg "\\(\\<class\\>\\)\\|\\(\\<endclass\\>\\)" ))
  5432. ((looking-at "\\<endtable\\>")
  5433. ;; 6: Search back for matching table
  5434. (setq reg "\\(\\<table\\>\\)\\|\\(\\<endtable\\>\\)" ))
  5435. ((looking-at "\\<endspecify\\>")
  5436. ;; 7: Search back for matching specify
  5437. (setq reg "\\(\\<specify\\>\\)\\|\\(\\<endspecify\\>\\)" ))
  5438. ((looking-at "\\<endfunction\\>")
  5439. ;; 8: Search back for matching function
  5440. (setq reg "\\(\\<function\\>\\)\\|\\(\\(\\<\\(virtual\\|protected\\|static\\)\\>\\s-+\\)+\\<function\\>\\)")
  5441. (setq nesting 'no))
  5442. ;;(setq reg "\\(\\<function\\>\\)\\|\\(\\<endfunction\\>\\)" ))
  5443. ((looking-at "\\<endgenerate\\>")
  5444. ;; 8: Search back for matching generate
  5445. (setq reg "\\(\\<generate\\>\\)\\|\\(\\<endgenerate\\>\\)" ))
  5446. ((looking-at "\\<endgroup\\>")
  5447. ;; 10: Search back for matching covergroup
  5448. (setq reg "\\(\\<covergroup\\>\\)\\|\\(\\<endgroup\\>\\)" ))
  5449. ((looking-at "\\<endproperty\\>")
  5450. ;; 11: Search back for matching property
  5451. (setq reg "\\(\\<property\\>\\)\\|\\(\\<endproperty\\>\\)" ))
  5452. ((looking-at verilog-uvm-end-re)
  5453. ;; 12: Search back for matching sequence
  5454. (setq reg (concat "\\(" verilog-uvm-begin-re "\\|" verilog-uvm-end-re "\\)")))
  5455. ((looking-at verilog-ovm-end-re)
  5456. ;; 12: Search back for matching sequence
  5457. (setq reg (concat "\\(" verilog-ovm-begin-re "\\|" verilog-ovm-end-re "\\)")))
  5458. ((looking-at verilog-vmm-end-re)
  5459. ;; 12: Search back for matching sequence
  5460. (setq reg (concat "\\(" verilog-vmm-begin-re "\\|" verilog-vmm-end-re "\\)")))
  5461. ((looking-at "\\<endinterface\\>")
  5462. ;; 12: Search back for matching interface
  5463. (setq reg "\\(\\<interface\\>\\)\\|\\(\\<endinterface\\>\\)" ))
  5464. ((looking-at "\\<endsequence\\>")
  5465. ;; 12: Search back for matching sequence
  5466. (setq reg "\\(\\<\\(rand\\)?sequence\\>\\)\\|\\(\\<endsequence\\>\\)" ))
  5467. ((looking-at "\\<endclocking\\>")
  5468. ;; 12: Search back for matching clocking
  5469. (setq reg "\\(\\<clocking\\)\\|\\(\\<endclocking\\>\\)" )))
  5470. (if reg
  5471. (catch 'skip
  5472. (if (eq nesting 'yes)
  5473. (let (sreg)
  5474. (while (verilog-re-search-backward reg nil 'move)
  5475. (cond
  5476. ((match-end 1) ; begin
  5477. (if (looking-at "fork")
  5478. (let ((here (point)))
  5479. (verilog-beg-of-statement)
  5480. (unless (looking-at verilog-disable-fork-re)
  5481. (goto-char here)
  5482. (setq nest (1- nest))))
  5483. (setq nest (1- nest)))
  5484. (if (= 0 nest)
  5485. ;; Now previous line describes syntax
  5486. (throw 'skip 1))
  5487. (if (and snest
  5488. (= snest nest))
  5489. (setq reg sreg)))
  5490. ((match-end 2) ; end
  5491. (setq nest (1+ nest)))
  5492. ((match-end 3)
  5493. ;; endcase, jump to case
  5494. (setq snest nest)
  5495. (setq nest (1+ nest))
  5496. (setq sreg reg)
  5497. (setq reg "\\(\\<randcase\\>\\|\\<case[xz]?\\>[^:]\\)\\|\\(\\<endcase\\>\\)" ))
  5498. ((match-end 4)
  5499. ;; join, jump to fork
  5500. (setq snest nest)
  5501. (setq nest (1+ nest))
  5502. (setq sreg reg)
  5503. (setq reg "\\(\\<fork\\>\\)\\|\\(\\<join\\(_any\\|_none\\)?\\>\\)" ))
  5504. )))
  5505. ;; no nesting
  5506. (if (and
  5507. (verilog-re-search-backward reg nil 'move)
  5508. (match-end 1)) ; task -> could be virtual and/or protected
  5509. (progn
  5510. (verilog-beg-of-statement)
  5511. (throw 'skip 1))
  5512. (throw 'skip 1)))))))
  5513. (defun verilog-continued-line ()
  5514. "Return true if this is a continued line.
  5515. Set point to where line starts."
  5516. (let ((continued 't))
  5517. (if (eq 0 (forward-line -1))
  5518. (progn
  5519. (end-of-line)
  5520. (verilog-backward-ws&directives)
  5521. (if (bobp)
  5522. (setq continued nil)
  5523. (while (and continued
  5524. (save-excursion
  5525. (skip-chars-backward " \t")
  5526. (not (bolp))))
  5527. (setq continued (verilog-backward-token)))))
  5528. (setq continued nil))
  5529. continued))
  5530. (defun verilog-backward-token ()
  5531. "Step backward token, returning true if this is a continued line."
  5532. (interactive)
  5533. (verilog-backward-syntactic-ws)
  5534. (cond
  5535. ((bolp)
  5536. nil)
  5537. (;-- Anything ending in a ; is complete
  5538. (= (preceding-char) ?\;)
  5539. nil)
  5540. (; If a "}" is prefixed by a ";", then this is a complete statement
  5541. ;; i.e.: constraint foo { a = b; }
  5542. (= (preceding-char) ?\})
  5543. (progn
  5544. (backward-char)
  5545. (not(verilog-at-close-constraint-p))))
  5546. (;-- constraint foo { a = b }
  5547. ;; is a complete statement. *sigh*
  5548. (= (preceding-char) ?\{)
  5549. (progn
  5550. (backward-char)
  5551. (not (verilog-at-constraint-p))))
  5552. (;" string "
  5553. (= (preceding-char) ?\")
  5554. (backward-char)
  5555. (verilog-skip-backward-comment-or-string)
  5556. nil)
  5557. (; [3:4]
  5558. (= (preceding-char) ?\])
  5559. (backward-char)
  5560. (verilog-backward-open-bracket)
  5561. t)
  5562. (;-- Could be 'case (foo)' or 'always @(bar)' which is complete
  5563. ;; also could be simply '@(foo)'
  5564. ;; or foo u1 #(a=8)
  5565. ;; (b, ... which ISN'T complete
  5566. ;; Do we need this???
  5567. (= (preceding-char) ?\))
  5568. (progn
  5569. (backward-char)
  5570. (verilog-backward-up-list 1)
  5571. (verilog-backward-syntactic-ws)
  5572. (let ((back (point)))
  5573. (forward-word-strictly -1)
  5574. (cond
  5575. ;;XX
  5576. ((looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|case\\(\\|[xz]\\)\\|for\\(\\|each\\|ever\\)\\|i\\(f\\|nitial\\)\\|repeat\\|while\\)\\>")
  5577. (not (looking-at "\\<randcase\\>\\|\\<case[xz]?\\>[^:]")))
  5578. ((looking-at verilog-uvm-statement-re)
  5579. nil)
  5580. ((looking-at verilog-uvm-begin-re)
  5581. t)
  5582. ((looking-at verilog-uvm-end-re)
  5583. t)
  5584. ((looking-at verilog-ovm-statement-re)
  5585. nil)
  5586. ((looking-at verilog-ovm-begin-re)
  5587. t)
  5588. ((looking-at verilog-ovm-end-re)
  5589. t)
  5590. ;; JBA find VMM macros
  5591. ((looking-at verilog-vmm-statement-re)
  5592. nil )
  5593. ((looking-at verilog-vmm-begin-re)
  5594. t)
  5595. ((looking-at verilog-vmm-end-re)
  5596. nil)
  5597. ;; JBA trying to catch macro lines with no ; at end
  5598. ((looking-at "\\<`")
  5599. nil)
  5600. (t
  5601. (goto-char back)
  5602. (cond
  5603. ((= (preceding-char) ?\@)
  5604. (backward-char)
  5605. (save-excursion
  5606. (verilog-backward-token)
  5607. (not (looking-at "\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\|initial\\|while\\)\\>"))))
  5608. ((= (preceding-char) ?\#)
  5609. (backward-char))
  5610. (t t)))))))
  5611. (;-- any of begin|initial|while are complete statements; 'begin : foo' is also complete
  5612. t
  5613. (forward-word-strictly -1)
  5614. (while (or (= (preceding-char) ?\_)
  5615. (= (preceding-char) ?\@)
  5616. (= (preceding-char) ?\.))
  5617. (forward-word-strictly -1))
  5618. (cond
  5619. ((looking-at "\\<else\\>")
  5620. t)
  5621. ((looking-at verilog-behavioral-block-beg-re)
  5622. t)
  5623. ((looking-at verilog-indent-re)
  5624. nil)
  5625. (t
  5626. (let
  5627. ((back (point)))
  5628. (verilog-backward-syntactic-ws)
  5629. (cond
  5630. ((= (preceding-char) ?\:)
  5631. (backward-char)
  5632. (verilog-backward-syntactic-ws)
  5633. (backward-sexp)
  5634. (if (looking-at verilog-nameable-item-re )
  5635. nil
  5636. t))
  5637. ((= (preceding-char) ?\#)
  5638. (backward-char)
  5639. t)
  5640. ((= (preceding-char) ?\`)
  5641. (backward-char)
  5642. t)
  5643. (t
  5644. (goto-char back)
  5645. t))))))))
  5646. (defun verilog-backward-syntactic-ws ()
  5647. "Move backwards putting point after first non-whitespace non-comment."
  5648. (verilog-skip-backward-comments)
  5649. (forward-comment (- (buffer-size))))
  5650. (defun verilog-backward-syntactic-ws-quick ()
  5651. "As with `verilog-backward-syntactic-ws' but use `verilog-scan' cache."
  5652. (while (cond ((bobp)
  5653. nil) ; Done
  5654. ((< (skip-syntax-backward " ") 0)
  5655. t)
  5656. ((eq (preceding-char) ?\n) ; \n's terminate // so aren't space syntax
  5657. (forward-char -1)
  5658. t)
  5659. ((or (verilog-inside-comment-or-string-p (1- (point)))
  5660. (verilog-inside-comment-or-string-p (point)))
  5661. (re-search-backward "[/\"]" nil t) ; Only way a comment or quote can begin
  5662. t))))
  5663. (defun verilog-forward-syntactic-ws ()
  5664. (verilog-skip-forward-comment-p)
  5665. (forward-comment (buffer-size)))
  5666. (defun verilog-backward-ws&directives (&optional bound)
  5667. "Backward skip over syntactic whitespace and compiler directives for Emacs 19.
  5668. Optional BOUND limits search."
  5669. (save-restriction
  5670. (let* ((bound (or bound (point-min)))
  5671. (here bound)
  5672. (p nil) )
  5673. (if (< bound (point))
  5674. (progn
  5675. (let ((state (save-excursion (verilog-syntax-ppss))))
  5676. (cond
  5677. ((nth 7 state) ; in // comment
  5678. (verilog-re-search-backward "//" nil 'move)
  5679. (skip-chars-backward "/"))
  5680. ((nth 4 state) ; in /* */ comment
  5681. (verilog-re-search-backward "/\\*" nil 'move))))
  5682. (narrow-to-region bound (point))
  5683. (while (/= here (point))
  5684. (setq here (point))
  5685. (verilog-skip-backward-comments)
  5686. (setq p
  5687. (save-excursion
  5688. (beginning-of-line)
  5689. ;; for as long as we're right after a continued line, keep moving up
  5690. (while (and (verilog-looking-back "\\\\[\n\r\f]" nil)
  5691. (forward-line -1)))
  5692. (cond
  5693. ((and verilog-highlight-translate-off
  5694. (verilog-within-translate-off))
  5695. (verilog-back-to-start-translate-off (point-min)))
  5696. ((looking-at verilog-directive-re-1)
  5697. (point))
  5698. (t
  5699. nil))))
  5700. (if p (goto-char p))))))))
  5701. (defun verilog-forward-ws&directives (&optional bound)
  5702. "Forward skip over syntactic whitespace and compiler directives for Emacs 19.
  5703. Optional BOUND limits search."
  5704. (save-restriction
  5705. (let* ((bound (or bound (point-max)))
  5706. (here bound)
  5707. jump)
  5708. (if (> bound (point))
  5709. (progn
  5710. (let ((state (save-excursion (verilog-syntax-ppss))))
  5711. (cond
  5712. ((nth 7 state) ; in // comment
  5713. (end-of-line)
  5714. (forward-char 1)
  5715. (skip-chars-forward " \t\n\f")
  5716. )
  5717. ((nth 4 state) ; in /* */ comment
  5718. (verilog-re-search-forward "\\*/\\s-*" nil 'move))))
  5719. (narrow-to-region (point) bound)
  5720. (while (/= here (point))
  5721. (setq here (point)
  5722. jump nil)
  5723. (forward-comment (buffer-size))
  5724. (and (looking-at "\\s-*(\\*.*\\*)\\s-*") ; Attribute
  5725. (goto-char (match-end 0)))
  5726. (save-excursion
  5727. (beginning-of-line)
  5728. (if (looking-at verilog-directive-re-1)
  5729. (setq jump t)))
  5730. (if jump
  5731. (beginning-of-line 2))))))))
  5732. (defun verilog-in-comment-p ()
  5733. "Return true if in a star or // comment."
  5734. (let ((state (save-excursion (verilog-syntax-ppss))))
  5735. (or (nth 4 state) (nth 7 state))))
  5736. (defun verilog-in-star-comment-p ()
  5737. "Return true if in a star comment."
  5738. (let ((state (save-excursion (verilog-syntax-ppss))))
  5739. (and
  5740. (nth 4 state) ; t if in a comment of style a // or b /**/
  5741. (not
  5742. (nth 7 state) ; t if in a comment of style b /**/
  5743. ))))
  5744. (defun verilog-in-slash-comment-p ()
  5745. "Return true if in a slash comment."
  5746. (let ((state (save-excursion (verilog-syntax-ppss))))
  5747. (nth 7 state)))
  5748. (defun verilog-in-comment-or-string-p ()
  5749. "Return true if in a string or comment."
  5750. (let ((state (save-excursion (verilog-syntax-ppss))))
  5751. (or (nth 3 state) (nth 4 state) (nth 7 state)))) ; Inside string or comment)
  5752. (defun verilog-in-attribute-p ()
  5753. "Return true if point is in an attribute (* [] attribute *)."
  5754. (save-match-data
  5755. (save-excursion
  5756. (verilog-re-search-backward "\\((\\*\\)\\|\\(\\*)\\)" nil 'move)
  5757. (cond
  5758. ((match-end 1)
  5759. (progn (goto-char (match-end 1))
  5760. (not (looking-at "\\s-*)")))
  5761. nil)
  5762. ((match-end 2)
  5763. (progn (goto-char (match-beginning 2))
  5764. (not (looking-at "(\\s-*")))
  5765. nil)
  5766. (t nil)))))
  5767. (defun verilog-in-parameter-p ()
  5768. "Return true if point is in a parameter assignment #( p1=1, p2=5)."
  5769. (save-match-data
  5770. (save-excursion
  5771. (verilog-re-search-backward "\\(#(\\)\\|\\()\\)" nil 'move)
  5772. (numberp (match-beginning 1)))))
  5773. (defun verilog-in-escaped-name-p ()
  5774. "Return true if in an escaped name."
  5775. (save-excursion
  5776. (backward-char)
  5777. (skip-chars-backward "^ \t\n\f")
  5778. (if (equal (char-after (point) ) ?\\ )
  5779. t
  5780. nil)))
  5781. (defun verilog-in-directive-p ()
  5782. "Return true if in a directive."
  5783. (save-excursion
  5784. (beginning-of-line)
  5785. (looking-at verilog-directive-re-1)))
  5786. (defun verilog-in-parenthesis-p ()
  5787. "Return true if in a ( ) expression (but not { } or [ ])."
  5788. (save-match-data
  5789. (save-excursion
  5790. (verilog-re-search-backward "\\((\\)\\|\\()\\)" nil 'move)
  5791. (numberp (match-beginning 1)))))
  5792. (defun verilog-in-paren ()
  5793. "Return true if in a parenthetical expression.
  5794. May cache result using `verilog-syntax-ppss'."
  5795. (let ((state (save-excursion (verilog-syntax-ppss))))
  5796. (> (nth 0 state) 0 )))
  5797. (defun verilog-in-paren-count ()
  5798. "Return paren depth, floor to 0.
  5799. May cache result using `verilog-syntax-ppss'."
  5800. (let ((state (save-excursion (verilog-syntax-ppss))))
  5801. (if (> (nth 0 state) 0)
  5802. (nth 0 state)
  5803. 0 )))
  5804. (defun verilog-in-paren-quick ()
  5805. "Return true if in a parenthetical expression.
  5806. Always starts from `point-min', to allow inserts with hooks disabled."
  5807. ;; The -quick refers to its use alongside the other -quick functions,
  5808. ;; not that it's likely to be faster than verilog-in-paren.
  5809. (let ((state (save-excursion (parse-partial-sexp (point-min) (point)))))
  5810. (> (nth 0 state) 0 )))
  5811. (defun verilog-in-struct-p ()
  5812. "Return true if in a struct declaration."
  5813. (interactive)
  5814. (save-excursion
  5815. (if (verilog-in-paren)
  5816. (progn
  5817. (verilog-backward-up-list 1)
  5818. (verilog-at-struct-p)
  5819. )
  5820. nil)))
  5821. (defun verilog-in-struct-nested-p ()
  5822. "Return nil for not in struct.
  5823. Return 0 for in non-nested struct.
  5824. Return >0 for nested struct."
  5825. (interactive)
  5826. (let (col)
  5827. (save-excursion
  5828. (if (verilog-in-paren)
  5829. (progn
  5830. (verilog-backward-up-list 1)
  5831. (setq col (verilog-at-struct-mv-p))
  5832. (if col
  5833. (if (verilog-in-struct-p) (current-column) 0)))
  5834. nil))))
  5835. (defun verilog-in-coverage-p ()
  5836. "Return true if in a constraint or coverpoint expression."
  5837. (interactive)
  5838. (save-excursion
  5839. (if (verilog-in-paren)
  5840. (progn
  5841. (verilog-backward-up-list 1)
  5842. (verilog-at-constraint-p)
  5843. )
  5844. nil)))
  5845. (defun verilog-at-close-constraint-p ()
  5846. "If at the } that closes a constraint or covergroup, return true."
  5847. (if (and
  5848. (equal (char-after) ?\})
  5849. (verilog-in-coverage-p))
  5850. (save-excursion
  5851. (verilog-backward-ws&directives)
  5852. (if (or (equal (char-before) ?\;)
  5853. (equal (char-before) ?\}) ; can end with inner constraint { } block or ;
  5854. (equal (char-before) ?\{)) ; empty constraint block
  5855. (point)
  5856. nil))))
  5857. (defun verilog-at-constraint-p ()
  5858. "If at the { of a constraint or coverpoint definition, return true, moving point to constraint."
  5859. (if (save-excursion
  5860. (let ((p (point)))
  5861. (and
  5862. (equal (char-after) ?\{)
  5863. (ignore-errors (forward-list))
  5864. (progn (backward-char 1)
  5865. (verilog-backward-ws&directives)
  5866. (and
  5867. (or (equal (char-before) ?\{) ; empty case
  5868. (equal (char-before) ?\;)
  5869. (equal (char-before) ?\}))
  5870. ;; skip what looks like bus repetition operator {#{
  5871. (not (string-match "^{\\s-*[0-9]+\\s-*{" (buffer-substring p (point)))))))))
  5872. (progn
  5873. (let ( (pt (point)) (pass 0))
  5874. (verilog-backward-ws&directives)
  5875. (verilog-backward-token)
  5876. (if (looking-at (concat "\\<constraint\\|coverpoint\\|cross\\|with\\>\\|" verilog-in-constraint-re))
  5877. (progn (setq pass 1)
  5878. (if (looking-at "\\<with\\>")
  5879. (progn (verilog-backward-ws&directives)
  5880. (beginning-of-line) ; 1
  5881. (verilog-forward-ws&directives)
  5882. 1 )
  5883. (verilog-beg-of-statement)
  5884. ))
  5885. ;; if first word token not keyword, it maybe the instance name
  5886. ;; check next word token
  5887. (if (looking-at "\\<\\w+\\>\\|\\s-*(\\s-*\\S-+")
  5888. (progn (verilog-beg-of-statement)
  5889. (if (looking-at (concat "\\<\\(constraint\\|"
  5890. "\\(?:\\w+\\s-*:\\s-*\\)?\\(coverpoint\\|cross\\)"
  5891. "\\|with\\)\\>\\|" verilog-in-constraint-re))
  5892. (setq pass 1)))))
  5893. (if (eq pass 0)
  5894. (progn (goto-char pt) nil) 1)))
  5895. ;; not
  5896. nil))
  5897. (defun verilog-at-struct-p ()
  5898. "If at the { of a struct, return true, not moving point."
  5899. (save-excursion
  5900. (if (and (equal (char-after) ?\{)
  5901. (verilog-backward-token))
  5902. (looking-at "\\<struct\\|union\\|packed\\|\\(un\\)?signed\\>")
  5903. nil)))
  5904. (defun verilog-at-struct-mv-p ()
  5905. "If at the { of a struct, return true, moving point to struct."
  5906. (let ((pt (point)))
  5907. (if (and (equal (char-after) ?\{)
  5908. (verilog-backward-token))
  5909. (if (looking-at "\\<struct\\|union\\|packed\\|\\(un\\)?signed\\>")
  5910. (progn (verilog-beg-of-statement) (point))
  5911. (progn (goto-char pt) nil))
  5912. (progn (goto-char pt) nil))))
  5913. (defun verilog-at-close-struct-p ()
  5914. "If at the } that closes a struct, return true."
  5915. (if (and
  5916. (equal (char-after) ?\})
  5917. (verilog-in-struct-p))
  5918. ;; true
  5919. (save-excursion
  5920. (if (looking-at "}\\(?:\\s-*\\w+\\s-*\\)?;") 1))
  5921. ;; false
  5922. nil))
  5923. (defun verilog-parenthesis-depth ()
  5924. "Return non zero if in parenthetical-expression."
  5925. (save-excursion (nth 1 (verilog-syntax-ppss))))
  5926. (defun verilog-skip-forward-comment-or-string ()
  5927. "Return true if in a string or comment."
  5928. (let ((state (save-excursion (verilog-syntax-ppss))))
  5929. (cond
  5930. ((nth 3 state) ;Inside string
  5931. (search-forward "\"")
  5932. t)
  5933. ((nth 7 state) ;Inside // comment
  5934. (forward-line 1)
  5935. t)
  5936. ((nth 4 state) ;Inside any comment (hence /**/)
  5937. (search-forward "*/"))
  5938. (t
  5939. nil))))
  5940. (defun verilog-skip-backward-comment-or-string ()
  5941. "Return true if in a string or comment."
  5942. (let ((state (save-excursion (verilog-syntax-ppss))))
  5943. (cond
  5944. ((nth 3 state) ;Inside string
  5945. (search-backward "\"")
  5946. t)
  5947. ((nth 7 state) ;Inside // comment
  5948. (search-backward "//")
  5949. (skip-chars-backward "/")
  5950. t)
  5951. ((nth 4 state) ;Inside /* */ comment
  5952. (search-backward "/*")
  5953. t)
  5954. (t
  5955. nil))))
  5956. (defun verilog-skip-backward-comments ()
  5957. "Return true if a comment was skipped."
  5958. (let ((more t))
  5959. (while more
  5960. (setq more
  5961. (let ((state (save-excursion (verilog-syntax-ppss))))
  5962. (cond
  5963. ((nth 7 state) ;Inside // comment
  5964. (search-backward "//")
  5965. (skip-chars-backward "/")
  5966. (skip-chars-backward " \t\n\f")
  5967. t)
  5968. ((nth 4 state) ;Inside /* */ comment
  5969. (search-backward "/*")
  5970. (skip-chars-backward " \t\n\f")
  5971. t)
  5972. ((and (not (bobp))
  5973. (= (char-before) ?\/)
  5974. (= (char-before (1- (point))) ?\*))
  5975. (goto-char (- (point) 2))
  5976. t) ; Let nth 4 state handle the rest
  5977. ((and (not (bobp))
  5978. ;;(verilog-looking-back "\\*)" nil) ;; super slow, use two char-before instead
  5979. (= (char-before) ?\))
  5980. (= (char-before (1- (point))) ?\*)
  5981. (not (verilog-looking-back "(\\s-*\\*)" nil))) ;; slow but unlikely to be called
  5982. (goto-char (- (point) 2))
  5983. (if (search-backward "(*" nil t)
  5984. (progn
  5985. (skip-chars-backward " \t\n\f")
  5986. t)
  5987. (progn
  5988. (goto-char (+ (point) 2))
  5989. nil)))
  5990. (t
  5991. (/= (skip-chars-backward " \t\n\f") 0))))))))
  5992. (defun verilog-skip-forward-comment-p ()
  5993. "If in comment, move to end and return true."
  5994. (let* (h
  5995. (state (save-excursion (verilog-syntax-ppss)))
  5996. (skip (cond
  5997. ((nth 3 state) ;Inside string
  5998. t)
  5999. ((nth 7 state) ;Inside // comment
  6000. (end-of-line)
  6001. (forward-char 1)
  6002. t)
  6003. ((nth 4 state) ;Inside /* comment
  6004. (search-forward "*/")
  6005. t)
  6006. ((verilog-in-attribute-p) ;Inside (* attribute
  6007. (search-forward "*)" nil t)
  6008. t)
  6009. (t nil))))
  6010. (skip-chars-forward " \t\n\f")
  6011. (while
  6012. (cond
  6013. ((looking-at "\\/\\*")
  6014. (progn
  6015. (setq h (point))
  6016. (goto-char (match-end 0))
  6017. (if (search-forward "*/" nil t)
  6018. (progn
  6019. (skip-chars-forward " \t\n\f")
  6020. (setq skip 't))
  6021. (progn
  6022. (goto-char h)
  6023. nil))))
  6024. ((and (looking-at "(\\*") ; attribute start, but not an event (*) or (* )
  6025. (not (looking-at "(\\*\\s-*)")))
  6026. (progn
  6027. (setq h (point))
  6028. (goto-char (match-end 0))
  6029. (if (search-forward "*)" nil t)
  6030. (progn
  6031. (skip-chars-forward " \t\n\f")
  6032. (setq skip 't))
  6033. (progn
  6034. (goto-char h)
  6035. nil))))
  6036. (t nil)))
  6037. skip))
  6038. (defun verilog-indent-line-relative ()
  6039. "Cheap version of indent line.
  6040. Only look at a few lines to determine indent level."
  6041. (interactive)
  6042. (let ((indent-str)
  6043. (sp (point)))
  6044. (if (looking-at "^[ \t]*$")
  6045. (cond ;- A blank line; No need to be too smart.
  6046. ((bobp)
  6047. (setq indent-str (list 'cpp 0)))
  6048. ((verilog-continued-line)
  6049. (let ((sp1 (point)))
  6050. (if (verilog-continued-line)
  6051. (progn
  6052. (goto-char sp)
  6053. (setq indent-str
  6054. (list 'statement (verilog-current-indent-level))))
  6055. (goto-char sp1)
  6056. (setq indent-str (list 'block (verilog-current-indent-level)))))
  6057. (goto-char sp))
  6058. ((goto-char sp)
  6059. (setq indent-str (verilog-calculate-indent))))
  6060. (progn (skip-chars-forward " \t")
  6061. (setq indent-str (verilog-calculate-indent))))
  6062. (verilog-do-indent indent-str)))
  6063. (defun verilog-indent-line ()
  6064. "Indent for special part of code."
  6065. (verilog-do-indent (verilog-calculate-indent)))
  6066. (defun verilog-do-indent (indent-str)
  6067. (let ((type (car indent-str))
  6068. (ind (car (cdr indent-str))))
  6069. (cond
  6070. (; handle continued exp
  6071. (eq type 'cexp)
  6072. (let ((here (point)))
  6073. (verilog-backward-syntactic-ws)
  6074. (cond
  6075. ((or
  6076. (= (preceding-char) ?\,)
  6077. (save-excursion
  6078. (verilog-beg-of-statement-1)
  6079. (looking-at verilog-declaration-re)))
  6080. (let* ( fst
  6081. (val
  6082. (save-excursion
  6083. (backward-char 1)
  6084. (verilog-beg-of-statement-1)
  6085. (setq fst (point))
  6086. (if (looking-at verilog-declaration-re)
  6087. (progn ; we have multiple words
  6088. (goto-char (match-end 0))
  6089. (skip-chars-forward " \t")
  6090. (cond
  6091. ((and verilog-indent-declaration-macros
  6092. (= (following-char) ?\`))
  6093. (progn
  6094. (forward-char 1)
  6095. (forward-word-strictly 1)
  6096. (skip-chars-forward " \t")))
  6097. ((= (following-char) ?\[)
  6098. (progn
  6099. (forward-char 1)
  6100. (verilog-backward-up-list -1)
  6101. (skip-chars-forward " \t"))))
  6102. (current-column))
  6103. (progn
  6104. (goto-char fst)
  6105. (+ (current-column) verilog-cexp-indent))))))
  6106. (goto-char here)
  6107. (indent-line-to val)
  6108. (if (and (not verilog-indent-lists)
  6109. (verilog-in-paren))
  6110. (verilog-pretty-declarations-auto))
  6111. ))
  6112. ((= (preceding-char) ?\) )
  6113. (goto-char here)
  6114. (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
  6115. (indent-line-to val)))
  6116. (t
  6117. (goto-char here)
  6118. (let ((val))
  6119. (verilog-beg-of-statement-1)
  6120. (if (and (< (point) here)
  6121. (verilog-re-search-forward "=[ \\t]*" here 'move)
  6122. ;; not at a |=>, #=#, or [=n] operator
  6123. (not (string-match "\\[=.\\|#=#\\||=>"
  6124. (or (buffer-substring (- (point) 2) (1+ (point)))
  6125. "")))) ; don't let buffer over/under-run spoil the party
  6126. (setq val (current-column))
  6127. (setq val (eval (cdr (assoc type verilog-indent-alist)))))
  6128. (goto-char here)
  6129. (indent-line-to val))))))
  6130. (; handle inside parenthetical expressions
  6131. (eq type 'cparenexp)
  6132. (let* ( here
  6133. (val (save-excursion
  6134. (verilog-backward-up-list 1)
  6135. (forward-char 1)
  6136. (if verilog-indent-lists
  6137. (skip-chars-forward " \t")
  6138. (verilog-forward-syntactic-ws))
  6139. (setq here (point))
  6140. (current-column)))
  6141. (decl (save-excursion
  6142. (goto-char here)
  6143. (verilog-forward-syntactic-ws)
  6144. (setq here (point))
  6145. (looking-at verilog-declaration-re))))
  6146. (indent-line-to val)
  6147. (if decl
  6148. (verilog-pretty-declarations-auto))))
  6149. (;-- Handle the ends
  6150. (or
  6151. (looking-at verilog-end-block-re)
  6152. (verilog-at-close-constraint-p)
  6153. (verilog-at-close-struct-p))
  6154. (let ((val (if (eq type 'statement)
  6155. (- ind verilog-indent-level)
  6156. ind)))
  6157. (indent-line-to val)))
  6158. (;-- Case -- maybe line 'em up
  6159. (and (eq type 'case) (not (looking-at "^[ \t]*$")))
  6160. (progn
  6161. (cond
  6162. ((looking-at "\\<endcase\\>")
  6163. (indent-line-to ind))
  6164. (t
  6165. (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
  6166. (indent-line-to val))))))
  6167. (;-- defun
  6168. (and (eq type 'defun)
  6169. (looking-at verilog-zero-indent-re))
  6170. (indent-line-to 0))
  6171. (;-- declaration
  6172. (and (or
  6173. (eq type 'defun)
  6174. (eq type 'block))
  6175. (looking-at verilog-declaration-re)
  6176. ;; Do not consider "virtual function", "virtual task", "virtual class"
  6177. ;; as declarations
  6178. (not (looking-at (concat verilog-declaration-re
  6179. "\\s-+\\(function\\|task\\|class\\)\\b"))))
  6180. (verilog-indent-declaration ind))
  6181. (;-- form feeds - ignored as bug in indent-line-to in < 24.5
  6182. (looking-at "\f"))
  6183. (;-- Everything else
  6184. t
  6185. (let ((val (eval (cdr (assoc type verilog-indent-alist)))))
  6186. (indent-line-to val))))
  6187. (if (looking-at "[ \t]+$")
  6188. (skip-chars-forward " \t"))
  6189. indent-str ; Return indent data
  6190. ))
  6191. (defun verilog-current-indent-level ()
  6192. "Return the indent-level of the current statement."
  6193. (save-excursion
  6194. (let (par-pos)
  6195. (beginning-of-line)
  6196. (setq par-pos (verilog-parenthesis-depth))
  6197. (while par-pos
  6198. (goto-char par-pos)
  6199. (beginning-of-line)
  6200. (setq par-pos (verilog-parenthesis-depth)))
  6201. (skip-chars-forward " \t")
  6202. (current-column))))
  6203. (defun verilog-case-indent-level ()
  6204. "Return the indent-level of the current statement.
  6205. Do not count named blocks or case-statements."
  6206. (save-excursion
  6207. (skip-chars-forward " \t")
  6208. (cond
  6209. ((looking-at verilog-named-block-re)
  6210. (current-column))
  6211. ((and (not (looking-at verilog-extended-case-re))
  6212. (looking-at "^[^:;]+[ \t]*:"))
  6213. (verilog-re-search-forward ":" nil t)
  6214. (skip-chars-forward " \t")
  6215. (current-column))
  6216. (t
  6217. (current-column)))))
  6218. (defun verilog-indent-comment ()
  6219. "Indent current line as comment."
  6220. (let* ((stcol
  6221. (cond
  6222. ((verilog-in-star-comment-p)
  6223. (save-excursion
  6224. (re-search-backward "/\\*" nil t)
  6225. (1+(current-column))))
  6226. (comment-column
  6227. comment-column )
  6228. (t
  6229. (save-excursion
  6230. (re-search-backward "//" nil t)
  6231. (current-column))))))
  6232. (indent-line-to stcol)
  6233. stcol))
  6234. (defun verilog-more-comment ()
  6235. "Make more comment lines like the previous."
  6236. (let* ((star 0)
  6237. (stcol
  6238. (cond
  6239. ((verilog-in-star-comment-p)
  6240. (save-excursion
  6241. (setq star 1)
  6242. (re-search-backward "/\\*" nil t)
  6243. (1+(current-column))))
  6244. (comment-column
  6245. comment-column )
  6246. (t
  6247. (save-excursion
  6248. (re-search-backward "//" nil t)
  6249. (current-column))))))
  6250. (progn
  6251. (indent-to stcol)
  6252. (if (and star
  6253. (save-excursion
  6254. (forward-line -1)
  6255. (skip-chars-forward " \t")
  6256. (looking-at "\\*")))
  6257. (insert "* ")))))
  6258. (defun verilog-comment-indent (&optional _arg)
  6259. "Return the column number the line should be indented to.
  6260. _ARG is ignored, for `comment-indent-function' compatibility."
  6261. (cond
  6262. ((verilog-in-star-comment-p)
  6263. (save-excursion
  6264. (re-search-backward "/\\*" nil t)
  6265. (1+(current-column))))
  6266. ( comment-column
  6267. comment-column )
  6268. (t
  6269. (save-excursion
  6270. (re-search-backward "//" nil t)
  6271. (current-column)))))
  6272. ;;
  6273. (defun verilog-pretty-declarations-auto (&optional quiet)
  6274. "Call `verilog-pretty-declarations' QUIET based on `verilog-auto-lineup'."
  6275. (when (or (eq 'all verilog-auto-lineup)
  6276. (eq 'declarations verilog-auto-lineup))
  6277. (verilog-pretty-declarations quiet)))
  6278. (defun verilog-pretty-declarations (&optional quiet)
  6279. "Line up declarations around point.
  6280. Be verbose about progress unless optional QUIET set."
  6281. (interactive)
  6282. (let* ((m1 (make-marker))
  6283. (e (point))
  6284. el
  6285. r
  6286. (here (point))
  6287. ind
  6288. start
  6289. startpos
  6290. end
  6291. endpos
  6292. base-ind
  6293. )
  6294. (save-excursion
  6295. (if (progn
  6296. ;; (verilog-beg-of-statement-1)
  6297. (beginning-of-line)
  6298. (verilog-forward-syntactic-ws)
  6299. (and (not (verilog-in-directive-p)) ; could have `define input foo
  6300. (looking-at verilog-declaration-re)))
  6301. (progn
  6302. (if (verilog-parenthesis-depth)
  6303. ;; in an argument list or parameter block
  6304. (setq el (verilog-backward-up-list -1)
  6305. start (progn
  6306. (goto-char e)
  6307. (verilog-backward-up-list 1)
  6308. (forward-line) ; ignore ( input foo,
  6309. (verilog-re-search-forward verilog-declaration-re el 'move)
  6310. (goto-char (match-beginning 0))
  6311. (skip-chars-backward " \t")
  6312. (point))
  6313. startpos (set-marker (make-marker) start)
  6314. end (progn
  6315. (goto-char start)
  6316. (verilog-backward-up-list -1)
  6317. (forward-char -1)
  6318. (verilog-backward-syntactic-ws)
  6319. (point))
  6320. endpos (set-marker (make-marker) end)
  6321. base-ind (progn
  6322. (goto-char start)
  6323. (forward-char 1)
  6324. (skip-chars-forward " \t")
  6325. (current-column)))
  6326. ;; in a declaration block (not in argument list)
  6327. (setq
  6328. start (progn
  6329. (verilog-beg-of-statement-1)
  6330. (while (and (looking-at verilog-declaration-re)
  6331. (not (bobp)))
  6332. (skip-chars-backward " \t")
  6333. (setq e (point))
  6334. (beginning-of-line)
  6335. (verilog-backward-syntactic-ws)
  6336. (backward-char)
  6337. (verilog-beg-of-statement-1))
  6338. e)
  6339. startpos (set-marker (make-marker) start)
  6340. end (progn
  6341. (goto-char here)
  6342. (verilog-end-of-statement)
  6343. (setq e (point)) ;Might be on last line
  6344. (verilog-forward-syntactic-ws)
  6345. (while (looking-at verilog-declaration-re)
  6346. (verilog-end-of-statement)
  6347. (setq e (point))
  6348. (verilog-forward-syntactic-ws))
  6349. e)
  6350. endpos (set-marker (make-marker) end)
  6351. base-ind (progn
  6352. (goto-char start)
  6353. (verilog-do-indent (verilog-calculate-indent))
  6354. (verilog-forward-ws&directives)
  6355. (current-column))))
  6356. ;; OK, start and end are set
  6357. (goto-char (marker-position startpos))
  6358. (if (and (not quiet)
  6359. (> (- end start) 100))
  6360. (message "Lining up declarations..(please stand by)"))
  6361. ;; Get the beginning of line indent first
  6362. (while (progn (setq e (marker-position endpos))
  6363. (< (point) e))
  6364. (cond
  6365. ((save-excursion (skip-chars-backward " \t")
  6366. (bolp))
  6367. (verilog-forward-ws&directives)
  6368. (indent-line-to base-ind)
  6369. (verilog-forward-ws&directives)
  6370. (if (< (point) e)
  6371. (verilog-re-search-forward "[ \t\n\f]" e 'move)))
  6372. (t
  6373. (just-one-space)
  6374. (verilog-re-search-forward "[ \t\n\f]" e 'move)))
  6375. ;;(forward-line)
  6376. )
  6377. ;; Now find biggest prefix
  6378. (setq ind (verilog-get-lineup-indent (marker-position startpos) endpos))
  6379. ;; Now indent each line.
  6380. (goto-char (marker-position startpos))
  6381. (while (progn (setq e (marker-position endpos))
  6382. (setq r (- e (point)))
  6383. (> r 0))
  6384. (setq e (point))
  6385. (unless quiet (message "%d" r))
  6386. ;; (verilog-do-indent (verilog-calculate-indent)))
  6387. (verilog-forward-ws&directives)
  6388. (cond
  6389. ((or (and verilog-indent-declaration-macros
  6390. (looking-at verilog-declaration-re-2-macro))
  6391. (looking-at verilog-declaration-re-2-no-macro))
  6392. (let ((p (match-end 0)))
  6393. (set-marker m1 p)
  6394. (if (verilog-re-search-forward "[[#`]" p 'move)
  6395. (progn
  6396. (forward-char -1)
  6397. (just-one-space)
  6398. (goto-char (marker-position m1))
  6399. (just-one-space)
  6400. (indent-to ind))
  6401. (progn
  6402. (just-one-space)
  6403. (indent-to ind)))))
  6404. ((verilog-continued-line-1 (marker-position startpos))
  6405. (goto-char e)
  6406. (indent-line-to ind))
  6407. ((verilog-in-struct-p)
  6408. ;; could have a declaration of a user defined item
  6409. (goto-char e)
  6410. (verilog-end-of-statement))
  6411. (t ; Must be comment or white space
  6412. (goto-char e)
  6413. (verilog-forward-ws&directives)
  6414. (forward-line -1)))
  6415. (forward-line 1))
  6416. (unless quiet (message "")))))))
  6417. (defun verilog-pretty-expr (&optional quiet _myre)
  6418. "Line up expressions around point, optionally QUIET with regexp _MYRE ignored."
  6419. (interactive)
  6420. (if (not (verilog-in-comment-or-string-p))
  6421. (save-excursion
  6422. (let ( (rexp (concat "^\\s-*" verilog-complete-reg))
  6423. (rexp1 (concat "^\\s-*" verilog-basic-complete-re)))
  6424. (beginning-of-line)
  6425. (if (and (not (looking-at rexp ))
  6426. (looking-at verilog-assignment-operation-re)
  6427. (save-excursion
  6428. (goto-char (match-end 2))
  6429. (and (not (verilog-in-attribute-p))
  6430. (not (verilog-in-parameter-p))
  6431. (not (verilog-in-comment-or-string-p)))))
  6432. (let* ((here (point))
  6433. (e) (r)
  6434. (start
  6435. (progn
  6436. (beginning-of-line)
  6437. (setq e (point))
  6438. (verilog-backward-syntactic-ws)
  6439. (beginning-of-line)
  6440. (while (and (not (looking-at rexp1))
  6441. (looking-at verilog-assignment-operation-re)
  6442. (not (bobp))
  6443. )
  6444. (setq e (point))
  6445. (verilog-backward-syntactic-ws)
  6446. (beginning-of-line)
  6447. ) ;Ack, need to grok `define
  6448. e))
  6449. (end
  6450. (progn
  6451. (goto-char here)
  6452. (end-of-line)
  6453. (setq e (point)) ;Might be on last line
  6454. (verilog-forward-syntactic-ws)
  6455. (beginning-of-line)
  6456. (while (and
  6457. (not (looking-at rexp1 ))
  6458. (looking-at verilog-assignment-operation-re)
  6459. (progn
  6460. (end-of-line)
  6461. (not (eq e (point)))))
  6462. (setq e (point))
  6463. (verilog-forward-syntactic-ws)
  6464. (beginning-of-line)
  6465. )
  6466. e))
  6467. (endpos (set-marker (make-marker) end))
  6468. (ind)
  6469. )
  6470. (goto-char start)
  6471. (verilog-do-indent (verilog-calculate-indent))
  6472. (if (and (not quiet)
  6473. (> (- end start) 100))
  6474. (message "Lining up expressions..(please stand by)"))
  6475. ;; Set indent to minimum throughout region
  6476. (while (< (point) (marker-position endpos))
  6477. (beginning-of-line)
  6478. (verilog-just-one-space verilog-assignment-operation-re)
  6479. (beginning-of-line)
  6480. (verilog-do-indent (verilog-calculate-indent))
  6481. (end-of-line)
  6482. (verilog-forward-syntactic-ws)
  6483. )
  6484. ;; Now find biggest prefix
  6485. (setq ind (verilog-get-lineup-indent-2 verilog-assignment-operation-re start endpos))
  6486. ;; Now indent each line.
  6487. (goto-char start)
  6488. (while (progn (setq e (marker-position endpos))
  6489. (setq r (- e (point)))
  6490. (> r 0))
  6491. (setq e (point))
  6492. (if (not quiet) (message "%d" r))
  6493. (cond
  6494. ((looking-at verilog-assignment-operation-re)
  6495. (goto-char (match-beginning 2))
  6496. (if (not (or (verilog-in-parenthesis-p) ; leave attributes and comparisons alone
  6497. (verilog-in-coverage-p)))
  6498. (if (eq (char-after) ?=)
  6499. (indent-to (1+ ind)) ; line up the = of the <= with surrounding =
  6500. (indent-to ind)
  6501. ))
  6502. )
  6503. ((verilog-continued-line-1 start)
  6504. (goto-char e)
  6505. (indent-line-to ind))
  6506. (t ; Must be comment or white space
  6507. (goto-char e)
  6508. (verilog-forward-ws&directives)
  6509. (forward-line -1))
  6510. )
  6511. (forward-line 1))
  6512. (unless quiet (message ""))
  6513. ))))))
  6514. (defun verilog-just-one-space (myre)
  6515. "Remove extra spaces around regular expression MYRE."
  6516. (interactive)
  6517. (if (and (not(looking-at verilog-complete-reg))
  6518. (looking-at myre))
  6519. (let ((p1 (match-end 1))
  6520. (p2 (match-end 2)))
  6521. (progn
  6522. (goto-char p2)
  6523. (just-one-space)
  6524. (goto-char p1)
  6525. (just-one-space)))))
  6526. (defun verilog-indent-declaration (baseind)
  6527. "Indent current lines as declaration.
  6528. Line up the variable names based on previous declaration's indentation.
  6529. BASEIND is the base indent to offset everything."
  6530. (interactive)
  6531. (let ((pos (point-marker))
  6532. (lim (save-excursion
  6533. ;; (verilog-re-search-backward verilog-declaration-opener nil 'move)
  6534. (verilog-re-search-backward "\\(\\<begin\\>\\)\\|\\(\\<module\\>\\)\\|\\(\\<task\\>\\)" nil 'move)
  6535. (point)))
  6536. (ind)
  6537. (val)
  6538. (m1 (make-marker)))
  6539. (setq val
  6540. (+ baseind (eval (cdr (assoc 'declaration verilog-indent-alist)))))
  6541. (indent-line-to val)
  6542. ;; Use previous declaration (in this module) as template.
  6543. (if (or (eq 'all verilog-auto-lineup)
  6544. (eq 'declarations verilog-auto-lineup))
  6545. (if (verilog-re-search-backward
  6546. (or (and verilog-indent-declaration-macros
  6547. verilog-declaration-re-1-macro)
  6548. verilog-declaration-re-1-no-macro) lim t)
  6549. (progn
  6550. (goto-char (match-end 0))
  6551. (skip-chars-forward " \t")
  6552. (setq ind (current-column))
  6553. (goto-char pos)
  6554. (setq val
  6555. (+ baseind
  6556. (eval (cdr (assoc 'declaration verilog-indent-alist)))))
  6557. (indent-line-to val)
  6558. (if (and verilog-indent-declaration-macros
  6559. (looking-at verilog-declaration-re-2-macro))
  6560. (let ((p (match-end 0)))
  6561. (set-marker m1 p)
  6562. (if (verilog-re-search-forward "[[#`]" p 'move)
  6563. (progn
  6564. (forward-char -1)
  6565. (just-one-space)
  6566. (goto-char (marker-position m1))
  6567. (just-one-space)
  6568. (indent-to ind))
  6569. (if (/= (current-column) ind)
  6570. (progn
  6571. (just-one-space)
  6572. (indent-to ind)))))
  6573. (if (looking-at verilog-declaration-re-2-no-macro)
  6574. (let ((p (match-end 0)))
  6575. (set-marker m1 p)
  6576. (if (verilog-re-search-forward "[[`#]" p 'move)
  6577. (progn
  6578. (forward-char -1)
  6579. (just-one-space)
  6580. (goto-char (marker-position m1))
  6581. (just-one-space)
  6582. (indent-to ind))
  6583. (if (/= (current-column) ind)
  6584. (progn
  6585. (just-one-space)
  6586. (indent-to ind))))))))))
  6587. (goto-char pos)))
  6588. (defun verilog-get-lineup-indent (b edpos)
  6589. "Return the indent level that will line up several lines within the region.
  6590. Region is defined by B and EDPOS."
  6591. (save-excursion
  6592. (let ((ind 0) e)
  6593. (goto-char b)
  6594. ;; Get rightmost position
  6595. (while (progn (setq e (marker-position edpos))
  6596. (< (point) e))
  6597. (if (verilog-re-search-forward
  6598. (or (and verilog-indent-declaration-macros
  6599. verilog-declaration-re-1-macro)
  6600. verilog-declaration-re-1-no-macro) e 'move)
  6601. (progn
  6602. (goto-char (match-end 0))
  6603. (verilog-backward-syntactic-ws)
  6604. (if (> (current-column) ind)
  6605. (setq ind (current-column)))
  6606. (goto-char (match-end 0)))))
  6607. (if (> ind 0)
  6608. (1+ ind)
  6609. ;; No lineup-string found
  6610. (goto-char b)
  6611. (end-of-line)
  6612. (verilog-backward-syntactic-ws)
  6613. ;;(skip-chars-backward " \t")
  6614. (1+ (current-column))))))
  6615. (defun verilog-get-lineup-indent-2 (myre b edpos)
  6616. "Return the indent level that will line up several lines within the region."
  6617. (save-excursion
  6618. (let ((ind 0) e)
  6619. (goto-char b)
  6620. ;; Get rightmost position
  6621. (while (progn (setq e (marker-position edpos))
  6622. (< (point) e))
  6623. (if (and (verilog-re-search-forward myre e 'move)
  6624. (not (verilog-in-attribute-p))) ; skip attribute exprs
  6625. (progn
  6626. (goto-char (match-beginning 2))
  6627. (verilog-backward-syntactic-ws)
  6628. (if (> (current-column) ind)
  6629. (setq ind (current-column)))
  6630. (goto-char (match-end 0)))
  6631. ))
  6632. (if (> ind 0)
  6633. (1+ ind)
  6634. ;; No lineup-string found
  6635. (goto-char b)
  6636. (end-of-line)
  6637. (skip-chars-backward " \t")
  6638. (1+ (current-column))))))
  6639. (defun verilog-comment-depth (type val)
  6640. "A useful mode debugging aide. TYPE and VAL are comments for insertion."
  6641. (save-excursion
  6642. (let
  6643. ((b (prog2
  6644. (beginning-of-line)
  6645. (point-marker)
  6646. (end-of-line))))
  6647. (if (re-search-backward " /\\* [#-]# [a-zA-Z]+ [0-9]+ ## \\*/" b t)
  6648. (progn
  6649. (replace-match " /* -# ## */")
  6650. (end-of-line))
  6651. (progn
  6652. (end-of-line)
  6653. (insert " /* ## ## */"))))
  6654. (backward-char 6)
  6655. (insert
  6656. (format "%s %d" type val))))
  6657. ;;; Completion:
  6658. ;;
  6659. (defvar verilog-str nil)
  6660. (defvar verilog-all nil)
  6661. (defvar verilog-pred nil)
  6662. (defvar verilog-buffer-to-use nil)
  6663. (defvar verilog-flag nil)
  6664. (defvar verilog-toggle-completions nil
  6665. "True means \\<verilog-mode-map>\\[verilog-complete-word] should try all possible completions one by one.
  6666. Repeated use of \\[verilog-complete-word] will show you all of them.
  6667. Normally, when there is more than one possible completion,
  6668. it displays a list of all possible completions.")
  6669. (when (boundp 'completion-cycle-threshold)
  6670. (make-obsolete-variable
  6671. 'verilog-toggle-completions 'completion-cycle-threshold "26.1"))
  6672. (defvar verilog-type-keywords
  6673. '(
  6674. "and" "buf" "bufif0" "bufif1" "cmos" "defparam" "inout" "input"
  6675. "integer" "localparam" "logic" "mailbox" "nand" "nmos" "nor" "not" "notif0"
  6676. "notif1" "or" "output" "parameter" "pmos" "pull0" "pull1" "pulldown" "pullup"
  6677. "rcmos" "real" "realtime" "reg" "rnmos" "rpmos" "rtran" "rtranif0"
  6678. "rtranif1" "semaphore" "time" "tran" "tranif0" "tranif1" "tri" "tri0" "tri1"
  6679. "triand" "trior" "trireg" "wand" "wire" "wor" "xnor" "xor"
  6680. )
  6681. "Keywords for types used when completing a word in a declaration or parmlist.
  6682. \(integer, real, reg...)")
  6683. (defvar verilog-cpp-keywords
  6684. '("module" "macromodule" "primitive" "timescale" "define" "ifdef" "ifndef" "else"
  6685. "endif")
  6686. "Keywords to complete when at first word of a line in declarative scope.
  6687. \(initial, always, begin, assign...)
  6688. The procedures and variables defined within the Verilog program
  6689. will be completed at runtime and should not be added to this list.")
  6690. (defvar verilog-defun-keywords
  6691. (append
  6692. '(
  6693. "always" "always_comb" "always_ff" "always_latch" "assign"
  6694. "begin" "end" "generate" "endgenerate" "module" "endmodule"
  6695. "specify" "endspecify" "function" "endfunction" "initial" "final"
  6696. "task" "endtask" "primitive" "endprimitive"
  6697. )
  6698. verilog-type-keywords)
  6699. "Keywords to complete when at first word of a line in declarative scope.
  6700. \(initial, always, begin, assign...)
  6701. The procedures and variables defined within the Verilog program
  6702. will be completed at runtime and should not be added to this list.")
  6703. (defvar verilog-block-keywords
  6704. '(
  6705. "begin" "break" "case" "continue" "else" "end" "endfunction"
  6706. "endgenerate" "endinterface" "endpackage" "endspecify" "endtask"
  6707. "for" "fork" "if" "join" "join_any" "join_none" "repeat" "return"
  6708. "while")
  6709. "Keywords to complete when at first word of a line in behavioral scope.
  6710. \(begin, if, then, else, for, fork...)
  6711. The procedures and variables defined within the Verilog program
  6712. will be completed at runtime and should not be added to this list.")
  6713. (defvar verilog-tf-keywords
  6714. '("begin" "break" "fork" "join" "join_any" "join_none" "case" "end" "endtask" "endfunction" "if" "else" "for" "while" "repeat")
  6715. "Keywords to complete when at first word of a line in a task or function.
  6716. \(begin, if, then, else, for, fork.)
  6717. The procedures and variables defined within the Verilog program
  6718. will be completed at runtime and should not be added to this list.")
  6719. (defvar verilog-case-keywords
  6720. '("begin" "fork" "join" "join_any" "join_none" "case" "end" "endcase" "if" "else" "for" "repeat")
  6721. "Keywords to complete when at first word of a line in case scope.
  6722. \(begin, if, then, else, for, fork...)
  6723. The procedures and variables defined within the Verilog program
  6724. will be completed at runtime and should not be added to this list.")
  6725. (defvar verilog-separator-keywords
  6726. '("else" "then" "begin")
  6727. "Keywords to complete when NOT standing at the first word of a statement.
  6728. \(else, then, begin...)
  6729. Variables and function names defined within the Verilog program
  6730. will be completed at runtime and should not be added to this list.")
  6731. (defvar verilog-gate-ios
  6732. ;; All these have an implied {"input"...} at the end
  6733. '(("and" "output")
  6734. ("buf" "output")
  6735. ("bufif0" "output")
  6736. ("bufif1" "output")
  6737. ("cmos" "output")
  6738. ("nand" "output")
  6739. ("nmos" "output")
  6740. ("nor" "output")
  6741. ("not" "output")
  6742. ("notif0" "output")
  6743. ("notif1" "output")
  6744. ("or" "output")
  6745. ("pmos" "output")
  6746. ("pulldown" "output")
  6747. ("pullup" "output")
  6748. ("rcmos" "output")
  6749. ("rnmos" "output")
  6750. ("rpmos" "output")
  6751. ("rtran" "inout" "inout")
  6752. ("rtranif0" "inout" "inout")
  6753. ("rtranif1" "inout" "inout")
  6754. ("tran" "inout" "inout")
  6755. ("tranif0" "inout" "inout")
  6756. ("tranif1" "inout" "inout")
  6757. ("xnor" "output")
  6758. ("xor" "output"))
  6759. "Map of direction for each positional argument to each gate primitive.")
  6760. (defvar verilog-gate-keywords (mapcar `car verilog-gate-ios)
  6761. "Keywords for gate primitives.")
  6762. (defun verilog-string-diff (str1 str2)
  6763. "Return index of first letter where STR1 and STR2 differs."
  6764. (catch 'done
  6765. (let ((diff 0))
  6766. (while t
  6767. (if (or (> (1+ diff) (length str1))
  6768. (> (1+ diff) (length str2)))
  6769. (throw 'done diff))
  6770. (or (equal (aref str1 diff) (aref str2 diff))
  6771. (throw 'done diff))
  6772. (setq diff (1+ diff))))))
  6773. ;; Calculate all possible completions for functions if argument is `function',
  6774. ;; completions for procedures if argument is `procedure' or both functions and
  6775. ;; procedures otherwise.
  6776. (defun verilog-func-completion (type)
  6777. "Build regular expression for module/task/function names.
  6778. TYPE is `module', `tf' for task or function, or t if unknown."
  6779. (if (string= verilog-str "")
  6780. (setq verilog-str "[a-zA-Z_]"))
  6781. (let ((verilog-str (concat (cond
  6782. ((eq type 'module) "\\<\\(module\\)\\s +")
  6783. ((eq type 'tf) "\\<\\(task\\|function\\)\\s +")
  6784. (t "\\<\\(task\\|function\\|module\\)\\s +"))
  6785. "\\<\\(" verilog-str "[a-zA-Z0-9_.]*\\)\\>"))
  6786. match)
  6787. (if (not (looking-at verilog-defun-re))
  6788. (verilog-re-search-backward verilog-defun-re nil t))
  6789. (forward-char 1)
  6790. ;; Search through all reachable functions
  6791. (goto-char (point-min))
  6792. (while (verilog-re-search-forward verilog-str (point-max) t)
  6793. (progn (setq match (buffer-substring (match-beginning 2)
  6794. (match-end 2)))
  6795. (if (or (null verilog-pred)
  6796. (funcall verilog-pred match))
  6797. (setq verilog-all (cons match verilog-all)))))
  6798. (if (match-beginning 0)
  6799. (goto-char (match-beginning 0)))))
  6800. (defun verilog-get-completion-decl (end)
  6801. "Macro for searching through current declaration (var, type or const)
  6802. for matches of `str' and adding the occurrence tp `all' through point END."
  6803. (let ((re (or (and verilog-indent-declaration-macros
  6804. verilog-declaration-re-2-macro)
  6805. verilog-declaration-re-2-no-macro))
  6806. decl-end match)
  6807. ;; Traverse lines
  6808. (while (and (< (point) end)
  6809. (verilog-re-search-forward re end t))
  6810. ;; Traverse current line
  6811. (setq decl-end (save-excursion (verilog-declaration-end)))
  6812. (while (and (verilog-re-search-forward verilog-symbol-re decl-end t)
  6813. (not (match-end 1)))
  6814. (setq match (buffer-substring (match-beginning 0) (match-end 0)))
  6815. (if (string-match (concat "\\<" verilog-str) match)
  6816. (if (or (null verilog-pred)
  6817. (funcall verilog-pred match))
  6818. (setq verilog-all (cons match verilog-all)))))
  6819. (forward-line 1)))
  6820. verilog-all)
  6821. (defun verilog-var-completion ()
  6822. "Calculate all possible completions for variables (or constants)."
  6823. (let ((start (point)))
  6824. ;; Search for all reachable var declarations
  6825. (verilog-beg-of-defun)
  6826. (save-excursion
  6827. ;; Check var declarations
  6828. (verilog-get-completion-decl start))))
  6829. (defun verilog-keyword-completion (keyword-list)
  6830. "Give list of all possible completions of keywords in KEYWORD-LIST."
  6831. (mapcar (lambda (s)
  6832. (if (string-match (concat "\\<" verilog-str) s)
  6833. (if (or (null verilog-pred)
  6834. (funcall verilog-pred s))
  6835. (setq verilog-all (cons s verilog-all)))))
  6836. keyword-list))
  6837. (defun verilog-completion (verilog-str verilog-pred verilog-flag)
  6838. "Function passed to `completing-read', `try-completion' or `all-completions'.
  6839. Called to get completion on VERILOG-STR. If VERILOG-PRED is non-nil, it
  6840. must be a function to be called for every match to check if this should
  6841. really be a match. If VERILOG-FLAG is t, the function returns a list of
  6842. all possible completions. If VERILOG-FLAG is nil it returns a string,
  6843. the longest possible completion, or t if VERILOG-STR is an exact match.
  6844. If VERILOG-FLAG is `lambda', the function returns t if VERILOG-STR is an
  6845. exact match, nil otherwise."
  6846. (save-excursion
  6847. (let ((verilog-all nil))
  6848. ;; Set buffer to use for searching labels. This should be set
  6849. ;; within functions which use verilog-completions
  6850. (set-buffer verilog-buffer-to-use)
  6851. ;; Determine what should be completed
  6852. (let ((state (car (verilog-calculate-indent))))
  6853. (cond ((eq state 'defun)
  6854. (save-excursion (verilog-var-completion))
  6855. (verilog-func-completion 'module)
  6856. (verilog-keyword-completion verilog-defun-keywords))
  6857. ((eq state 'behavioral)
  6858. (save-excursion (verilog-var-completion))
  6859. (verilog-func-completion 'module)
  6860. (verilog-keyword-completion verilog-defun-keywords))
  6861. ((eq state 'block)
  6862. (save-excursion (verilog-var-completion))
  6863. (verilog-func-completion 'tf)
  6864. (verilog-keyword-completion verilog-block-keywords))
  6865. ((eq state 'case)
  6866. (save-excursion (verilog-var-completion))
  6867. (verilog-func-completion 'tf)
  6868. (verilog-keyword-completion verilog-case-keywords))
  6869. ((eq state 'tf)
  6870. (save-excursion (verilog-var-completion))
  6871. (verilog-func-completion 'tf)
  6872. (verilog-keyword-completion verilog-tf-keywords))
  6873. ((eq state 'cpp)
  6874. (save-excursion (verilog-var-completion))
  6875. (verilog-keyword-completion verilog-cpp-keywords))
  6876. ((eq state 'cparenexp)
  6877. (save-excursion (verilog-var-completion)))
  6878. (t;--Anywhere else
  6879. (save-excursion (verilog-var-completion))
  6880. (verilog-func-completion 'both)
  6881. (verilog-keyword-completion verilog-separator-keywords))))
  6882. ;; Now we have built a list of all matches. Give response to caller
  6883. (verilog-completion-response))))
  6884. (defun verilog-completion-response ()
  6885. (cond ((or (equal verilog-flag 'lambda) (null verilog-flag))
  6886. ;; This was not called by all-completions
  6887. (if (null verilog-all)
  6888. ;; Return nil if there was no matching label
  6889. nil
  6890. ;; Get longest string common in the labels
  6891. ;; FIXME: Why not use `try-completion'?
  6892. (let* ((elm (cdr verilog-all))
  6893. (match (car verilog-all))
  6894. (min (length match))
  6895. tmp)
  6896. (if (string= match verilog-str)
  6897. ;; Return t if first match was an exact match
  6898. (setq match t)
  6899. (while (not (null elm))
  6900. ;; Find longest common string
  6901. (if (< (setq tmp (verilog-string-diff match (car elm))) min)
  6902. (progn
  6903. (setq min tmp)
  6904. (setq match (substring match 0 min))))
  6905. ;; Terminate with match=t if this is an exact match
  6906. (if (string= (car elm) verilog-str)
  6907. (progn
  6908. (setq match t)
  6909. (setq elm nil))
  6910. (setq elm (cdr elm)))))
  6911. ;; If this is a test just for exact match, return nil ot t
  6912. (if (and (equal verilog-flag 'lambda) (not (equal match 't)))
  6913. nil
  6914. match))))
  6915. ;; If flag is t, this was called by all-completions. Return
  6916. ;; list of all possible completions
  6917. (verilog-flag
  6918. verilog-all)))
  6919. (defvar verilog-last-word-numb 0)
  6920. (defvar verilog-last-word-shown nil)
  6921. (defvar verilog-last-completions nil)
  6922. (defun verilog-completion-at-point ()
  6923. "Used as an element of `completion-at-point-functions'.
  6924. \(See also `verilog-type-keywords' and
  6925. `verilog-separator-keywords'.)"
  6926. (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
  6927. (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
  6928. (verilog-str (buffer-substring b e))
  6929. ;; The following variable is used in verilog-completion
  6930. (verilog-buffer-to-use (current-buffer))
  6931. (allcomp (if (and verilog-toggle-completions
  6932. (string= verilog-last-word-shown verilog-str))
  6933. verilog-last-completions
  6934. (all-completions verilog-str 'verilog-completion))))
  6935. (list b e allcomp)))
  6936. (defun verilog-complete-word ()
  6937. "Complete word at current point.
  6938. \(See also `verilog-toggle-completions', `verilog-type-keywords',
  6939. and `verilog-separator-keywords'.)"
  6940. ;; NOTE: This is just a fallback for Emacs versions lacking
  6941. ;; `completion-at-point'.
  6942. (interactive)
  6943. (let* ((comp-info (verilog-completion-at-point))
  6944. (b (nth 0 comp-info))
  6945. (e (nth 1 comp-info))
  6946. (verilog-str (buffer-substring b e))
  6947. (allcomp (nth 2 comp-info))
  6948. (match (if verilog-toggle-completions
  6949. "" (try-completion
  6950. verilog-str (mapcar (lambda (elm)
  6951. (cons elm 0)) allcomp)))))
  6952. ;; Delete old string
  6953. (delete-region b e)
  6954. ;; Toggle-completions inserts whole labels
  6955. (if verilog-toggle-completions
  6956. (progn
  6957. ;; Update entry number in list
  6958. (setq verilog-last-completions allcomp
  6959. verilog-last-word-numb
  6960. (if (>= verilog-last-word-numb (1- (length allcomp)))
  6961. 0
  6962. (1+ verilog-last-word-numb)))
  6963. (setq verilog-last-word-shown (elt allcomp verilog-last-word-numb))
  6964. ;; Display next match or same string if no match was found
  6965. (if (not (null allcomp))
  6966. (insert "" verilog-last-word-shown)
  6967. (insert "" verilog-str)
  6968. (message "(No match)")))
  6969. ;; The other form of completion does not necessarily do that.
  6970. ;; Insert match if found, or the original string if no match
  6971. (if (or (null match) (equal match 't))
  6972. (progn (insert "" verilog-str)
  6973. (message "(No match)"))
  6974. (insert "" match))
  6975. ;; Give message about current status of completion
  6976. (cond ((equal match 't)
  6977. (if (not (null (cdr allcomp)))
  6978. (message "(Complete but not unique)")
  6979. (message "(Sole completion)")))
  6980. ;; Display buffer if the current completion didn't help
  6981. ;; on completing the label.
  6982. ((and (not (null (cdr allcomp))) (= (length verilog-str)
  6983. (length match)))
  6984. (with-output-to-temp-buffer "*Completions*"
  6985. (display-completion-list allcomp))
  6986. ;; Wait for a key press. Then delete *Completion* window
  6987. (momentary-string-display "" (point))
  6988. (delete-window (get-buffer-window (get-buffer "*Completions*")))
  6989. )))))
  6990. (defun verilog-show-completions ()
  6991. "Show all possible completions at current point."
  6992. ;; NOTE: This is just a fallback for Emacs versions lacking
  6993. ;; `completion-help-at-point'.
  6994. (interactive)
  6995. ;; Show possible completions in a temporary buffer.
  6996. (with-output-to-temp-buffer "*Completions*"
  6997. (display-completion-list (nth 2 (verilog-completion-at-point))))
  6998. ;; Wait for a key press. Then delete *Completion* window
  6999. (momentary-string-display "" (point))
  7000. (delete-window (get-buffer-window (get-buffer "*Completions*"))))
  7001. (defun verilog-get-default-symbol ()
  7002. "Return symbol around current point as a string."
  7003. (save-excursion
  7004. (buffer-substring (progn
  7005. (skip-chars-backward " \t")
  7006. (skip-chars-backward "a-zA-Z0-9_")
  7007. (point))
  7008. (progn
  7009. (skip-chars-forward "a-zA-Z0-9_")
  7010. (point)))))
  7011. (defun verilog-build-defun-re (str &optional arg)
  7012. "Return function/task/module starting with STR as regular expression.
  7013. With optional second ARG non-nil, STR is the complete name of the instruction."
  7014. (if arg
  7015. (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "\\)\\>")
  7016. (concat "^\\(function\\|task\\|module\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))
  7017. (defun verilog-comp-defun (verilog-str verilog-pred verilog-flag)
  7018. "Function passed to `completing-read', `try-completion' or `all-completions'.
  7019. Returns a completion on any function name based on VERILOG-STR prefix. If
  7020. VERILOG-PRED is non-nil, it must be a function to be called for every match
  7021. to check if this should really be a match. If VERILOG-FLAG is t, the
  7022. function returns a list of all possible completions. If it is nil it
  7023. returns a string, the longest possible completion, or t if VERILOG-STR is
  7024. an exact match. If VERILOG-FLAG is `lambda', the function returns t if
  7025. VERILOG-STR is an exact match, nil otherwise."
  7026. (save-excursion
  7027. (let ((verilog-all nil)
  7028. match)
  7029. ;; Set buffer to use for searching labels. This should be set
  7030. ;; within functions which use verilog-completions
  7031. (set-buffer verilog-buffer-to-use)
  7032. (let ((verilog-str verilog-str))
  7033. ;; Build regular expression for functions
  7034. (if (string= verilog-str "")
  7035. (setq verilog-str (verilog-build-defun-re "[a-zA-Z_]"))
  7036. (setq verilog-str (verilog-build-defun-re verilog-str)))
  7037. (goto-char (point-min))
  7038. ;; Build a list of all possible completions
  7039. (while (verilog-re-search-forward verilog-str nil t)
  7040. (setq match (buffer-substring (match-beginning 2) (match-end 2)))
  7041. (if (or (null verilog-pred)
  7042. (funcall verilog-pred match))
  7043. (setq verilog-all (cons match verilog-all)))))
  7044. ;; Now we have built a list of all matches. Give response to caller
  7045. (verilog-completion-response))))
  7046. (defun verilog-goto-defun ()
  7047. "Move to specified Verilog module/interface/task/function.
  7048. The default is a name found in the buffer around point.
  7049. If search fails, other files are checked based on
  7050. `verilog-library-flags'."
  7051. (interactive)
  7052. (let* ((default (verilog-get-default-symbol))
  7053. ;; The following variable is used in verilog-comp-function
  7054. (verilog-buffer-to-use (current-buffer))
  7055. (label (if (not (string= default ""))
  7056. ;; Do completion with default
  7057. (completing-read (concat "Goto-Label: (default "
  7058. default ") ")
  7059. 'verilog-comp-defun nil nil "")
  7060. ;; There is no default value. Complete without it
  7061. (completing-read "Goto-Label: "
  7062. 'verilog-comp-defun nil nil "")))
  7063. pt)
  7064. ;; Make sure library paths are correct, in case need to resolve module
  7065. (verilog-auto-reeval-locals)
  7066. (verilog-getopt-flags)
  7067. ;; If there was no response on prompt, use default value
  7068. (if (string= label "")
  7069. (setq label default))
  7070. ;; Goto right place in buffer if label is not an empty string
  7071. (or (string= label "")
  7072. (progn
  7073. (save-excursion
  7074. (goto-char (point-min))
  7075. (setq pt
  7076. (re-search-forward (verilog-build-defun-re label t) nil t)))
  7077. (when pt
  7078. (goto-char pt)
  7079. (beginning-of-line))
  7080. pt)
  7081. (verilog-goto-defun-file label))))
  7082. ;; Eliminate compile warning
  7083. (defvar occur-pos-list)
  7084. (defun verilog-showscopes ()
  7085. "List all scopes in this module."
  7086. (interactive)
  7087. (let ((buffer (current-buffer))
  7088. (linenum 1)
  7089. (nlines 0)
  7090. (first 1)
  7091. (prevpos (point-min))
  7092. (final-context-start (make-marker))
  7093. (regexp "\\(module\\s-+\\w+\\s-*(\\)\\|\\(\\w+\\s-+\\w+\\s-*(\\)"))
  7094. (with-output-to-temp-buffer "*Occur*"
  7095. (save-excursion
  7096. (message "Searching for %s ..." regexp)
  7097. ;; Find next match, but give up if prev match was at end of buffer.
  7098. (while (and (not (= prevpos (point-max)))
  7099. (verilog-re-search-forward regexp nil t))
  7100. (goto-char (match-beginning 0))
  7101. (beginning-of-line)
  7102. (save-match-data
  7103. (setq linenum (+ linenum (count-lines prevpos (point)))))
  7104. (setq prevpos (point))
  7105. (goto-char (match-end 0))
  7106. (let* ((start (save-excursion
  7107. (goto-char (match-beginning 0))
  7108. (forward-line (if (< nlines 0) nlines (- nlines)))
  7109. (point)))
  7110. (end (save-excursion
  7111. (goto-char (match-end 0))
  7112. (if (> nlines 0)
  7113. (forward-line (1+ nlines))
  7114. (forward-line 1))
  7115. (point)))
  7116. (tag (format "%3d" linenum))
  7117. (empty (make-string (length tag) ?\ ))
  7118. tem)
  7119. (save-excursion
  7120. (setq tem (make-marker))
  7121. (set-marker tem (point))
  7122. (set-buffer standard-output)
  7123. (setq occur-pos-list (cons tem occur-pos-list))
  7124. (or first (zerop nlines)
  7125. (insert "--------\n"))
  7126. (setq first nil)
  7127. (insert-buffer-substring buffer start end)
  7128. (backward-char (- end start))
  7129. (setq tem (if (< nlines 0) (- nlines) nlines))
  7130. (while (> tem 0)
  7131. (insert empty ?:)
  7132. (forward-line 1)
  7133. (setq tem (1- tem)))
  7134. (let ((this-linenum linenum))
  7135. (set-marker final-context-start
  7136. (+ (point) (- (match-end 0) (match-beginning 0))))
  7137. (while (< (point) final-context-start)
  7138. (if (null tag)
  7139. (setq tag (format "%3d" this-linenum)))
  7140. (insert tag ?:)))))))
  7141. (set-buffer-modified-p nil))))
  7142. ;; Highlight helper functions
  7143. (defconst verilog-directive-regexp "\\(translate\\|coverage\\|lint\\)_")
  7144. (defun verilog-within-translate-off ()
  7145. "Return point if within translate-off region, else nil."
  7146. (and (save-excursion
  7147. (re-search-backward
  7148. (concat "//\\s-*.*\\s-*" verilog-directive-regexp "\\(on\\|off\\)\\>")
  7149. nil t))
  7150. (equal "off" (match-string 2))
  7151. (point)))
  7152. (defun verilog-start-translate-off (limit)
  7153. "Return point before translate-off directive if before LIMIT, else nil."
  7154. (when (re-search-forward
  7155. (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
  7156. limit t)
  7157. (match-beginning 0)))
  7158. (defun verilog-back-to-start-translate-off (limit)
  7159. "Return point before translate-off directive if before LIMIT, else nil."
  7160. (when (re-search-backward
  7161. (concat "//\\s-*.*\\s-*" verilog-directive-regexp "off\\>")
  7162. limit t)
  7163. (match-beginning 0)))
  7164. (defun verilog-end-translate-off (limit)
  7165. "Return point after translate-on directive if before LIMIT, else nil."
  7166. (re-search-forward (concat
  7167. "//\\s-*.*\\s-*" verilog-directive-regexp "on\\>") limit t))
  7168. (defun verilog-match-translate-off (limit)
  7169. "Match a translate-off block, setting `match-data' and returning t, else nil.
  7170. Bound search by LIMIT."
  7171. (when (< (point) limit)
  7172. (let ((start (or (verilog-within-translate-off)
  7173. (verilog-start-translate-off limit)))
  7174. (case-fold-search t))
  7175. (when start
  7176. (let ((end (or (verilog-end-translate-off limit) limit)))
  7177. (set-match-data (list start end))
  7178. (goto-char end))))))
  7179. (defun verilog-font-lock-match-item (limit)
  7180. "Match, and move over, any declaration item after point.
  7181. Bound search by LIMIT. Adapted from
  7182. `font-lock-match-c-style-declaration-item-and-skip-to-next'."
  7183. (condition-case nil
  7184. (save-restriction
  7185. (narrow-to-region (point-min) limit)
  7186. ;; match item
  7187. (when (looking-at "\\s-*\\([a-zA-Z]\\w*\\)")
  7188. (save-match-data
  7189. (goto-char (match-end 1))
  7190. ;; move to next item
  7191. (if (looking-at "\\(\\s-*,\\)")
  7192. (goto-char (match-end 1))
  7193. (end-of-line) t))))
  7194. (error nil)))
  7195. ;; Added by Subbu Meiyappan for Header
  7196. (defun verilog-header ()
  7197. "Insert a standard Verilog file header.
  7198. See also `verilog-sk-header' for an alternative format."
  7199. (interactive)
  7200. (let ((start (point)))
  7201. (insert "\
  7202. //-----------------------------------------------------------------------------
  7203. // Title : <title>
  7204. // Project : <project>
  7205. //-----------------------------------------------------------------------------
  7206. // File : <filename>
  7207. // Author : <author>
  7208. // Created : <credate>
  7209. // Last modified : <moddate>
  7210. //-----------------------------------------------------------------------------
  7211. // Description :
  7212. // <description>
  7213. //-----------------------------------------------------------------------------
  7214. // Copyright (c) <copydate> by <company> This model is the confidential and
  7215. // proprietary property of <company> and the possession or use of this
  7216. // file requires a written license from <company>.
  7217. //------------------------------------------------------------------------------
  7218. // Modification history :
  7219. // <modhist>
  7220. //-----------------------------------------------------------------------------
  7221. ")
  7222. (goto-char start)
  7223. (search-forward "<filename>")
  7224. (replace-match (buffer-name) t t)
  7225. (search-forward "<author>") (replace-match "" t t)
  7226. (insert (user-full-name))
  7227. (insert " <" (user-login-name) "@" (system-name) ">")
  7228. (search-forward "<credate>") (replace-match "" t t)
  7229. (verilog-insert-date)
  7230. (search-forward "<moddate>") (replace-match "" t t)
  7231. (verilog-insert-date)
  7232. (search-forward "<copydate>") (replace-match "" t t)
  7233. (verilog-insert-year)
  7234. (search-forward "<modhist>") (replace-match "" t t)
  7235. (verilog-insert-date)
  7236. (insert " : created")
  7237. (goto-char start)
  7238. (let (string)
  7239. (setq string (read-string "title: "))
  7240. (search-forward "<title>")
  7241. (replace-match string t t)
  7242. (setq string (read-string "project: " verilog-project))
  7243. (setq verilog-project string)
  7244. (search-forward "<project>")
  7245. (replace-match string t t)
  7246. (setq string (read-string "Company: " verilog-company))
  7247. (setq verilog-company string)
  7248. (search-forward "<company>")
  7249. (replace-match string t t)
  7250. (search-forward "<company>")
  7251. (replace-match string t t)
  7252. (search-forward "<company>")
  7253. (replace-match string t t)
  7254. (search-backward "<description>")
  7255. (replace-match "" t t))))
  7256. ;; verilog-header Uses the verilog-insert-date function
  7257. (defun verilog-insert-date ()
  7258. "Insert date from the system."
  7259. (interactive)
  7260. (if verilog-date-scientific-format
  7261. (insert (format-time-string "%Y/%m/%d"))
  7262. (insert (format-time-string "%d.%m.%Y"))))
  7263. (defun verilog-insert-year ()
  7264. "Insert year from the system."
  7265. (interactive)
  7266. (insert (format-time-string "%Y")))
  7267. ;;; Signal list parsing:
  7268. ;;
  7269. ;; Elements of a signal list
  7270. ;; Unfortunately we use 'assoc' on this, so can't be a vector
  7271. (defsubst verilog-sig-new (name bits comment mem enum signed type multidim modport)
  7272. (list name bits comment mem enum signed type multidim modport))
  7273. (defsubst verilog-sig-name (sig)
  7274. (car sig))
  7275. (defsubst verilog-sig-bits (sig) ; First element of packed array (pre signal-name)
  7276. (nth 1 sig))
  7277. (defsubst verilog-sig-comment (sig)
  7278. (nth 2 sig))
  7279. (defsubst verilog-sig-memory (sig) ; Unpacked array (post signal-name)
  7280. (nth 3 sig))
  7281. (defsubst verilog-sig-enum (sig)
  7282. (nth 4 sig))
  7283. (defsubst verilog-sig-signed (sig)
  7284. (nth 5 sig))
  7285. (defsubst verilog-sig-type (sig)
  7286. (nth 6 sig))
  7287. (defsubst verilog-sig-type-set (sig type)
  7288. (setcar (nthcdr 6 sig) type))
  7289. (defsubst verilog-sig-multidim (sig) ; Second and additional elements of packed array
  7290. (nth 7 sig))
  7291. (defsubst verilog-sig-multidim-string (sig)
  7292. (if (verilog-sig-multidim sig)
  7293. (let ((str "") (args (verilog-sig-multidim sig)))
  7294. (while args
  7295. (setq str (concat (car args) str))
  7296. (setq args (cdr args)))
  7297. str)))
  7298. (defsubst verilog-sig-modport (sig)
  7299. (nth 8 sig))
  7300. (defsubst verilog-sig-width (sig)
  7301. (verilog-make-width-expression (verilog-sig-bits sig)))
  7302. (defsubst verilog-alw-new (outputs-del outputs-imm temps inputs)
  7303. (vector outputs-del outputs-imm temps inputs))
  7304. (defsubst verilog-alw-get-outputs-delayed (sigs)
  7305. (aref sigs 0))
  7306. (defsubst verilog-alw-get-outputs-immediate (sigs)
  7307. (aref sigs 1))
  7308. (defsubst verilog-alw-get-temps (sigs)
  7309. (aref sigs 2))
  7310. (defsubst verilog-alw-get-inputs (sigs)
  7311. (aref sigs 3))
  7312. (defsubst verilog-alw-get-uses-delayed (sigs)
  7313. (aref sigs 0))
  7314. (defsubst verilog-modport-new (name clockings decls)
  7315. (list name clockings decls))
  7316. (defsubst verilog-modport-name (sig)
  7317. (car sig))
  7318. (defsubst verilog-modport-clockings (sig)
  7319. (nth 1 sig)) ; Returns list of names
  7320. (defsubst verilog-modport-clockings-add (sig val)
  7321. (setcar (nthcdr 1 sig) (cons val (nth 1 sig))))
  7322. (defsubst verilog-modport-decls (sig)
  7323. (nth 2 sig)) ; Returns verilog-decls-* structure
  7324. (defsubst verilog-modport-decls-set (sig val)
  7325. (setcar (nthcdr 2 sig) val))
  7326. (defsubst verilog-modi-new (name fob pt type)
  7327. (vector name fob pt type))
  7328. (defsubst verilog-modi-name (modi)
  7329. (aref modi 0))
  7330. (defsubst verilog-modi-file-or-buffer (modi)
  7331. (aref modi 1))
  7332. (defsubst verilog-modi-get-point (modi)
  7333. (aref modi 2))
  7334. (defsubst verilog-modi-get-type (modi) ; "module" or "interface"
  7335. (aref modi 3))
  7336. (defsubst verilog-modi-get-decls (modi)
  7337. (verilog-modi-cache-results modi 'verilog-read-decls))
  7338. (defsubst verilog-modi-get-sub-decls (modi)
  7339. (verilog-modi-cache-results modi 'verilog-read-sub-decls))
  7340. ;; Signal reading for given module
  7341. ;; Note these all take modi's - as returned from verilog-modi-current
  7342. (defsubst verilog-decls-new (out inout in vars modports assigns consts gparams interfaces)
  7343. (vector out inout in vars modports assigns consts gparams interfaces))
  7344. (defsubst verilog-decls-append (a b)
  7345. (cond ((not a) b) ((not b) a)
  7346. (t (vector (append (aref a 0) (aref b 0)) (append (aref a 1) (aref b 1))
  7347. (append (aref a 2) (aref b 2)) (append (aref a 3) (aref b 3))
  7348. (append (aref a 4) (aref b 4)) (append (aref a 5) (aref b 5))
  7349. (append (aref a 6) (aref b 6)) (append (aref a 7) (aref b 7))
  7350. (append (aref a 8) (aref b 8))))))
  7351. (defsubst verilog-decls-get-outputs (decls)
  7352. (aref decls 0))
  7353. (defsubst verilog-decls-get-inouts (decls)
  7354. (aref decls 1))
  7355. (defsubst verilog-decls-get-inputs (decls)
  7356. (aref decls 2))
  7357. (defsubst verilog-decls-get-vars (decls)
  7358. (aref decls 3))
  7359. (defsubst verilog-decls-get-modports (decls) ; Also for clocking blocks; contains another verilog-decls struct
  7360. (aref decls 4)) ; Returns verilog-modport* structure
  7361. (defsubst verilog-decls-get-assigns (decls)
  7362. (aref decls 5))
  7363. (defsubst verilog-decls-get-consts (decls)
  7364. (aref decls 6))
  7365. (defsubst verilog-decls-get-gparams (decls)
  7366. (aref decls 7))
  7367. (defsubst verilog-decls-get-interfaces (decls)
  7368. (aref decls 8))
  7369. (defsubst verilog-subdecls-new (out inout in intf intfd)
  7370. (vector out inout in intf intfd))
  7371. (defsubst verilog-subdecls-get-outputs (subdecls)
  7372. (aref subdecls 0))
  7373. (defsubst verilog-subdecls-get-inouts (subdecls)
  7374. (aref subdecls 1))
  7375. (defsubst verilog-subdecls-get-inputs (subdecls)
  7376. (aref subdecls 2))
  7377. (defsubst verilog-subdecls-get-interfaces (subdecls)
  7378. (aref subdecls 3))
  7379. (defsubst verilog-subdecls-get-interfaced (subdecls)
  7380. (aref subdecls 4))
  7381. (defun verilog-signals-from-signame (signame-list)
  7382. "Return signals in standard form from SIGNAME-LIST, a simple list of names."
  7383. (mapcar (lambda (name) (verilog-sig-new name nil nil nil nil nil nil nil nil))
  7384. signame-list))
  7385. (defun verilog-signals-in (in-list not-list)
  7386. "Return list of signals in IN-LIST that are also in NOT-LIST.
  7387. Also remove any duplicates in IN-LIST.
  7388. Signals must be in standard (base vector) form."
  7389. ;; This function is hot, so implemented as O(1)
  7390. (cond ((eval-when-compile (fboundp 'make-hash-table))
  7391. (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
  7392. (ht-not (make-hash-table :test 'equal :rehash-size 4.0))
  7393. out-list)
  7394. (while not-list
  7395. (puthash (car (car not-list)) t ht-not)
  7396. (setq not-list (cdr not-list)))
  7397. (while in-list
  7398. (when (and (gethash (verilog-sig-name (car in-list)) ht-not)
  7399. (not (gethash (verilog-sig-name (car in-list)) ht)))
  7400. (setq out-list (cons (car in-list) out-list))
  7401. (puthash (verilog-sig-name (car in-list)) t ht))
  7402. (setq in-list (cdr in-list)))
  7403. (nreverse out-list)))
  7404. ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
  7405. (t
  7406. (let (out-list)
  7407. (while in-list
  7408. (if (and (assoc (verilog-sig-name (car in-list)) not-list)
  7409. (not (assoc (verilog-sig-name (car in-list)) out-list)))
  7410. (setq out-list (cons (car in-list) out-list)))
  7411. (setq in-list (cdr in-list)))
  7412. (nreverse out-list)))))
  7413. ;;(verilog-signals-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("C" "")))
  7414. (defun verilog-signals-not-in (in-list not-list)
  7415. "Return list of signals in IN-LIST that aren't also in NOT-LIST.
  7416. Also remove any duplicates in IN-LIST.
  7417. Signals must be in standard (base vector) form."
  7418. ;; This function is hot, so implemented as O(1)
  7419. (cond ((eval-when-compile (fboundp 'make-hash-table))
  7420. (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
  7421. out-list)
  7422. (while not-list
  7423. (puthash (car (car not-list)) t ht)
  7424. (setq not-list (cdr not-list)))
  7425. (while in-list
  7426. (when (not (gethash (verilog-sig-name (car in-list)) ht))
  7427. (setq out-list (cons (car in-list) out-list))
  7428. (puthash (verilog-sig-name (car in-list)) t ht))
  7429. (setq in-list (cdr in-list)))
  7430. (nreverse out-list)))
  7431. ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
  7432. (t
  7433. (let (out-list)
  7434. (while in-list
  7435. (if (and (not (assoc (verilog-sig-name (car in-list)) not-list))
  7436. (not (assoc (verilog-sig-name (car in-list)) out-list)))
  7437. (setq out-list (cons (car in-list) out-list)))
  7438. (setq in-list (cdr in-list)))
  7439. (nreverse out-list)))))
  7440. ;;(verilog-signals-not-in '(("A" "") ("B" "") ("DEL" "[2:3]")) '(("DEL" "") ("EXT" "")))
  7441. (defun verilog-signals-not-in-struct (in-list not-list)
  7442. "Return list of signals in IN-LIST that aren't also in NOT-LIST.
  7443. Also remove any duplicates in IN-LIST.
  7444. Any structure in not-list will remove all members in in-list.
  7445. Signals must be in standard (base vector) form."
  7446. (cond ((eval-when-compile (fboundp 'make-hash-table))
  7447. (let ((ht (make-hash-table :test 'equal :rehash-size 4.0))
  7448. out-list addit nm)
  7449. (while not-list
  7450. (puthash (car (car not-list)) t ht)
  7451. (setq not-list (cdr not-list)))
  7452. (while in-list
  7453. (setq nm (verilog-sig-name (car in-list)))
  7454. (when (not (gethash nm ht))
  7455. (setq addit t)
  7456. (while (string-match "^\\([^\\].*\\)\\.[^.]+$" nm)
  7457. (setq nm (match-string 1 nm))
  7458. (setq addit (and addit
  7459. (not (gethash nm ht)))))
  7460. (when addit
  7461. (setq out-list (cons (car in-list) out-list))
  7462. (puthash (verilog-sig-name (car in-list)) t ht)))
  7463. (setq in-list (cdr in-list)))
  7464. (nreverse out-list)))
  7465. ;; Slower Fallback if no hash tables (pre Emacs 21.1/XEmacs 21.4)
  7466. (t
  7467. (let (out-list addit nm)
  7468. (while in-list
  7469. (setq nm (verilog-sig-name (car in-list)))
  7470. (when (and (not (assoc nm not-list))
  7471. (not (assoc nm out-list)))
  7472. (setq addit t)
  7473. (while (string-match "^\\([^\\].*\\)\\.[^.]+$" nm)
  7474. (setq nm (match-string 1 nm))
  7475. (setq addit (and addit
  7476. (not (assoc nm not-list)))))
  7477. (when addit
  7478. (setq out-list (cons (car in-list) out-list))))
  7479. (setq in-list (cdr in-list)))
  7480. (nreverse out-list)))))
  7481. ;;(verilog-signals-not-in-struct '(("A" "") ("B" "") ("DEL.SUB.A" "[2:3]")) '(("DEL.SUB" "") ("EXT" "")))
  7482. (defun verilog-signals-memory (in-list)
  7483. "Return list of signals in IN-LIST that are memorized (multidimensional)."
  7484. (let (out-list)
  7485. (while in-list
  7486. (if (nth 3 (car in-list))
  7487. (setq out-list (cons (car in-list) out-list)))
  7488. (setq in-list (cdr in-list)))
  7489. out-list))
  7490. ;;(verilog-signals-memory '(("A" nil nil "[3:0]")) '(("B" nil nil nil)))
  7491. (defun verilog-signals-sort-compare (a b)
  7492. "Compare signal A and B for sorting."
  7493. (string< (verilog-sig-name a) (verilog-sig-name b)))
  7494. (defun verilog-signals-not-params (in-list)
  7495. "Return list of signals in IN-LIST that aren't parameters or numeric constants."
  7496. (let (out-list)
  7497. (while in-list
  7498. ;; Namespace intentionally short for AUTOs and compatibility
  7499. (unless (boundp (intern (concat "vh-" (verilog-sig-name (car in-list)))))
  7500. (setq out-list (cons (car in-list) out-list)))
  7501. (setq in-list (cdr in-list)))
  7502. (nreverse out-list)))
  7503. (defun verilog-signals-with (func in-list)
  7504. "Return list of signals where FUNC is true executed on each signal in IN-LIST."
  7505. (let (out-list)
  7506. (while in-list
  7507. (when (funcall func (car in-list))
  7508. (setq out-list (cons (car in-list) out-list)))
  7509. (setq in-list (cdr in-list)))
  7510. (nreverse out-list)))
  7511. (defun verilog-signals-combine-bus (in-list)
  7512. "Return a list of signals in IN-LIST, with buses combined.
  7513. Duplicate signals are also removed. For example A[2] and A[1] become A[2:1]."
  7514. (let (combo
  7515. buswarn
  7516. out-list
  7517. sig highbit lowbit ; Temp information about current signal
  7518. sv-name sv-highbit sv-lowbit ; Details about signal we are forming
  7519. sv-comment sv-memory sv-enum sv-signed sv-type sv-multidim sv-busstring
  7520. sv-modport
  7521. bus)
  7522. ;; Shove signals so duplicated signals will be adjacent
  7523. (setq in-list (sort in-list `verilog-signals-sort-compare))
  7524. (while in-list
  7525. (setq sig (car in-list))
  7526. ;; No current signal; form from existing details
  7527. (unless sv-name
  7528. (setq sv-name (verilog-sig-name sig)
  7529. sv-highbit nil
  7530. sv-busstring nil
  7531. sv-comment (verilog-sig-comment sig)
  7532. sv-memory (verilog-sig-memory sig)
  7533. sv-enum (verilog-sig-enum sig)
  7534. sv-signed (verilog-sig-signed sig)
  7535. sv-type (verilog-sig-type sig)
  7536. sv-multidim (verilog-sig-multidim sig)
  7537. sv-modport (verilog-sig-modport sig)
  7538. combo ""
  7539. buswarn ""))
  7540. ;; Extract bus details
  7541. (setq bus (verilog-sig-bits sig))
  7542. (setq bus (and bus (verilog-simplify-range-expression bus)))
  7543. (cond ((and bus
  7544. (or (and (string-match "\\[\\([0-9]+\\):\\([0-9]+\\)\\]" bus)
  7545. (setq highbit (string-to-number (match-string 1 bus))
  7546. lowbit (string-to-number
  7547. (match-string 2 bus))))
  7548. (and (string-match "\\[\\([0-9]+\\)\\]" bus)
  7549. (setq highbit (string-to-number (match-string 1 bus))
  7550. lowbit highbit))))
  7551. ;; Combine bits in bus
  7552. (if sv-highbit
  7553. (setq sv-highbit (max highbit sv-highbit)
  7554. sv-lowbit (min lowbit sv-lowbit))
  7555. (setq sv-highbit highbit
  7556. sv-lowbit lowbit)))
  7557. (bus
  7558. ;; String, probably something like `preproc:0
  7559. (setq sv-busstring bus)))
  7560. ;; Peek ahead to next signal
  7561. (setq in-list (cdr in-list))
  7562. (setq sig (car in-list))
  7563. (cond ((and sig (equal sv-name (verilog-sig-name sig)))
  7564. ;; Combine with this signal
  7565. (when (and sv-busstring
  7566. (not (equal sv-busstring (verilog-sig-bits sig))))
  7567. (when nil ; Debugging
  7568. (message (concat "Warning, can't merge into single bus `%s%s'"
  7569. ", the AUTOs may be wrong")
  7570. sv-name bus))
  7571. (setq buswarn ", Couldn't Merge"))
  7572. (if (verilog-sig-comment sig) (setq combo ", ..."))
  7573. (setq sv-memory (or sv-memory (verilog-sig-memory sig))
  7574. sv-enum (or sv-enum (verilog-sig-enum sig))
  7575. sv-signed (or sv-signed (verilog-sig-signed sig))
  7576. sv-type (or sv-type (verilog-sig-type sig))
  7577. sv-multidim (or sv-multidim (verilog-sig-multidim sig))
  7578. sv-modport (or sv-modport (verilog-sig-modport sig))))
  7579. ;; Doesn't match next signal, add to queue, zero in prep for next
  7580. ;; Note sig may also be nil for the last signal in the list
  7581. (t
  7582. (setq out-list
  7583. (cons (verilog-sig-new
  7584. sv-name
  7585. (or sv-busstring
  7586. (if sv-highbit
  7587. (concat "[" (int-to-string sv-highbit) ":"
  7588. (int-to-string sv-lowbit) "]")))
  7589. (concat sv-comment combo buswarn)
  7590. sv-memory sv-enum sv-signed sv-type sv-multidim sv-modport)
  7591. out-list)
  7592. sv-name nil))))
  7593. ;;
  7594. out-list))
  7595. (defun verilog-sig-tieoff (sig)
  7596. "Return tieoff expression for given SIG, with appropriate width.
  7597. Tieoff value uses `verilog-active-low-regexp' and
  7598. `verilog-auto-reset-widths'."
  7599. (concat
  7600. (if (and verilog-active-low-regexp
  7601. (verilog-string-match-fold verilog-active-low-regexp (verilog-sig-name sig)))
  7602. "~" "")
  7603. (cond ((not verilog-auto-reset-widths)
  7604. "0")
  7605. ((equal verilog-auto-reset-widths 'unbased)
  7606. "'0")
  7607. ;; Else presume verilog-auto-reset-widths is true
  7608. (t
  7609. (let* ((width (verilog-sig-width sig)))
  7610. (cond ((not width)
  7611. "`0/*NOWIDTH*/")
  7612. ((string-match "^[0-9]+$" width)
  7613. (concat width (if (verilog-sig-signed sig) "'sh0" "'h0")))
  7614. (t
  7615. (concat "{" width "{1'b0}}"))))))))
  7616. ;;
  7617. ;; Dumping
  7618. ;;
  7619. (defun verilog-decls-princ (decls &optional header prefix)
  7620. "For debug, dump the `verilog-read-decls' structure DECLS.
  7621. Use optional HEADER and PREFIX."
  7622. (when decls
  7623. (if header (princ header))
  7624. (setq prefix (or prefix ""))
  7625. (verilog-signals-princ (verilog-decls-get-outputs decls)
  7626. (concat prefix "Outputs:\n") (concat prefix " "))
  7627. (verilog-signals-princ (verilog-decls-get-inouts decls)
  7628. (concat prefix "Inout:\n") (concat prefix " "))
  7629. (verilog-signals-princ (verilog-decls-get-inputs decls)
  7630. (concat prefix "Inputs:\n") (concat prefix " "))
  7631. (verilog-signals-princ (verilog-decls-get-vars decls)
  7632. (concat prefix "Vars:\n") (concat prefix " "))
  7633. (verilog-signals-princ (verilog-decls-get-assigns decls)
  7634. (concat prefix "Assigns:\n") (concat prefix " "))
  7635. (verilog-signals-princ (verilog-decls-get-consts decls)
  7636. (concat prefix "Consts:\n") (concat prefix " "))
  7637. (verilog-signals-princ (verilog-decls-get-gparams decls)
  7638. (concat prefix "Gparams:\n") (concat prefix " "))
  7639. (verilog-signals-princ (verilog-decls-get-interfaces decls)
  7640. (concat prefix "Interfaces:\n") (concat prefix " "))
  7641. (verilog-modport-princ (verilog-decls-get-modports decls)
  7642. (concat prefix "Modports:\n") (concat prefix " "))
  7643. (princ "\n")))
  7644. (defun verilog-signals-princ (signals &optional header prefix)
  7645. "For debug, dump internal SIGNALS structures, with HEADER and PREFIX."
  7646. (when signals
  7647. (if header (princ header))
  7648. (while signals
  7649. (let ((sig (car signals)))
  7650. (setq signals (cdr signals))
  7651. (princ prefix)
  7652. (princ "\"") (princ (verilog-sig-name sig)) (princ "\"")
  7653. (princ " bits=") (princ (verilog-sig-bits sig))
  7654. (princ " cmt=") (princ (verilog-sig-comment sig))
  7655. (princ " mem=") (princ (verilog-sig-memory sig))
  7656. (princ " enum=") (princ (verilog-sig-enum sig))
  7657. (princ " sign=") (princ (verilog-sig-signed sig))
  7658. (princ " type=") (princ (verilog-sig-type sig))
  7659. (princ " dim=") (princ (verilog-sig-multidim sig))
  7660. (princ " modp=") (princ (verilog-sig-modport sig))
  7661. (princ "\n")))))
  7662. (defun verilog-modport-princ (modports &optional header prefix)
  7663. "For debug, dump internal MODPORTS structures, with HEADER and PREFIX."
  7664. (when modports
  7665. (if header (princ header))
  7666. (while modports
  7667. (let ((sig (car modports)))
  7668. (setq modports (cdr modports))
  7669. (princ prefix)
  7670. (princ "\"") (princ (verilog-modport-name sig)) (princ "\"")
  7671. (princ " clockings=") (princ (verilog-modport-clockings sig))
  7672. (princ "\n")
  7673. (verilog-decls-princ (verilog-modport-decls sig)
  7674. (concat prefix " syms:\n")
  7675. (concat prefix " "))))))
  7676. ;;
  7677. ;; Port/Wire/Etc Reading
  7678. ;;
  7679. (defun verilog-read-inst-backward-name ()
  7680. "Internal. Move point back to beginning of inst-name."
  7681. (verilog-backward-open-paren)
  7682. (let (done)
  7683. (while (not done)
  7684. (verilog-re-search-backward-quick "\\()\\|\\b[a-zA-Z0-9`_$]\\|\\]\\)" nil nil) ; ] isn't word boundary
  7685. (cond ((looking-at ")")
  7686. (verilog-backward-open-paren))
  7687. (t (setq done t)))))
  7688. (while (looking-at "\\]")
  7689. (verilog-backward-open-bracket)
  7690. (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_$]\\|\\]\\)" nil nil))
  7691. (skip-chars-backward "a-zA-Z0-9`_$"))
  7692. (defun verilog-read-inst-module-matcher ()
  7693. "Set match data 0 with module_name when point is inside instantiation."
  7694. (verilog-read-inst-backward-name)
  7695. ;; Skip over instantiation name
  7696. (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_$]\\|)\\)" nil nil) ; ) isn't word boundary
  7697. ;; Check for parameterized instantiations
  7698. (when (looking-at ")")
  7699. (verilog-backward-open-paren)
  7700. (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_$]" nil nil))
  7701. (skip-chars-backward "a-zA-Z0-9'_$")
  7702. ;; #1 is legal syntax for gate primitives
  7703. (when (save-excursion
  7704. (verilog-backward-syntactic-ws-quick)
  7705. (eq ?# (char-before)))
  7706. (verilog-re-search-backward-quick "\\b[a-zA-Z0-9`_$]" nil nil)
  7707. (skip-chars-backward "a-zA-Z0-9'_$"))
  7708. (looking-at "[a-zA-Z0-9`_$]+")
  7709. ;; Important: don't use match string, this must work with Emacs 19 font-lock on
  7710. (buffer-substring-no-properties (match-beginning 0) (match-end 0))
  7711. ;; Caller assumes match-beginning/match-end is still set
  7712. )
  7713. (defun verilog-read-inst-module ()
  7714. "Return module_name when point is inside instantiation."
  7715. (save-excursion
  7716. (verilog-read-inst-module-matcher)))
  7717. (defun verilog-read-inst-name ()
  7718. "Return instance_name when point is inside instantiation."
  7719. (save-excursion
  7720. (verilog-read-inst-backward-name)
  7721. (looking-at "[a-zA-Z0-9`_$]+")
  7722. ;; Important: don't use match string, this must work with Emacs 19 font-lock on
  7723. (buffer-substring-no-properties (match-beginning 0) (match-end 0))))
  7724. (defun verilog-read-module-name ()
  7725. "Return module name when after its ( or ;."
  7726. (save-excursion
  7727. (re-search-backward "[(;]")
  7728. ;; Due to "module x import y (" we must search for declaration begin
  7729. (verilog-re-search-backward-quick verilog-defun-re nil nil)
  7730. (goto-char (match-end 0))
  7731. (verilog-re-search-forward-quick "\\b[a-zA-Z0-9`_$]+" nil nil)
  7732. ;; Important: don't use match string, this must work with Emacs 19 font-lock on
  7733. (verilog-symbol-detick
  7734. (buffer-substring-no-properties (match-beginning 0) (match-end 0)) t)))
  7735. (defun verilog-read-inst-param-value ()
  7736. "Return list of parameters and values when point is inside instantiation."
  7737. (save-excursion
  7738. (verilog-read-inst-backward-name)
  7739. ;; Skip over instantiation name
  7740. (verilog-re-search-backward-quick "\\(\\b[a-zA-Z0-9`_$]\\|)\\)" nil nil) ; ) isn't word boundary
  7741. ;; If there are parameterized instantiations
  7742. (when (looking-at ")")
  7743. (let ((end-pt (point))
  7744. params
  7745. param-name paren-beg-pt param-value)
  7746. (verilog-backward-open-paren)
  7747. (while (verilog-re-search-forward-quick "\\." end-pt t)
  7748. (verilog-re-search-forward-quick "\\([a-zA-Z0-9`_$]\\)" nil nil)
  7749. (skip-chars-backward "a-zA-Z0-9'_$")
  7750. (looking-at "[a-zA-Z0-9`_$]+")
  7751. (setq param-name (buffer-substring-no-properties
  7752. (match-beginning 0) (match-end 0)))
  7753. (verilog-re-search-forward-quick "(" nil nil)
  7754. (setq paren-beg-pt (point))
  7755. (verilog-forward-close-paren)
  7756. (setq param-value (verilog-string-remove-spaces
  7757. (buffer-substring-no-properties
  7758. paren-beg-pt (1- (point)))))
  7759. (setq params (cons (list param-name param-value) params)))
  7760. params))))
  7761. (defun verilog-read-auto-params (num-param &optional max-param)
  7762. "Return parameter list inside auto.
  7763. Optional NUM-PARAM and MAX-PARAM check for a specific number of parameters."
  7764. (let ((olist))
  7765. (save-excursion
  7766. ;; /*AUTOPUNT("parameter", "parameter")*/
  7767. (backward-sexp 1)
  7768. (while (looking-at "(?\\s *\"\\([^\"]*\\)\"\\s *,?")
  7769. (setq olist (cons (match-string 1) olist))
  7770. (goto-char (match-end 0))))
  7771. (or (eq nil num-param)
  7772. (<= num-param (length olist))
  7773. (error "%s: Expected %d parameters" (verilog-point-text) num-param))
  7774. (if (eq max-param nil) (setq max-param num-param))
  7775. (or (eq nil max-param)
  7776. (>= max-param (length olist))
  7777. (error "%s: Expected <= %d parameters" (verilog-point-text) max-param))
  7778. (nreverse olist)))
  7779. (defun verilog-read-decls ()
  7780. "Compute signal declaration information for the current module at point.
  7781. Return an array of [outputs inouts inputs wire reg assign const]."
  7782. (let ((end-mod-point (or (verilog-get-end-of-defun) (point-max)))
  7783. (functask 0) (paren 0) (sig-paren 0) (v2kargs-ok t)
  7784. in-modport in-clocking in-ign-to-semi ptype ign-prop
  7785. sigs-in sigs-out sigs-inout sigs-var sigs-assign sigs-const
  7786. sigs-gparam sigs-intf sigs-modports
  7787. vec expect-signal keywd last-keywd newsig rvalue enum io
  7788. signed typedefed multidim
  7789. modport
  7790. varstack tmp)
  7791. ;;(if dbg (setq dbg (concat dbg (format "\n\nverilog-read-decls START PT %s END %s\n" (point) end-mod-point))))
  7792. (save-excursion
  7793. (verilog-beg-of-defun-quick)
  7794. (setq sigs-const (verilog-read-auto-constants (point) end-mod-point))
  7795. (while (< (point) end-mod-point)
  7796. ;;(if dbg (setq dbg (concat dbg (format "Pt %s Vec %s C%c Kwd'%s'\n" (point) vec (following-char) keywd))))
  7797. (cond
  7798. ((looking-at "//")
  7799. (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
  7800. (setq enum (match-string 2)))
  7801. (search-forward "\n"))
  7802. ((looking-at "/\\*")
  7803. (forward-char 2)
  7804. (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
  7805. (setq enum (match-string 2)))
  7806. (or (search-forward "*/")
  7807. (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
  7808. ((looking-at "(\\*")
  7809. ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
  7810. (forward-char 1)
  7811. (or (search-forward "*)")
  7812. (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
  7813. ((eq ?\" (following-char))
  7814. (or (re-search-forward "[^\\]\"" nil t) ; don't forward-char first, since we look for a non backslash first
  7815. (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
  7816. ((eq ?\; (following-char))
  7817. (cond (in-ign-to-semi ; Such as inside a "import ...;" in a module header
  7818. (setq in-ign-to-semi nil))
  7819. ((and in-modport (not (eq in-modport t))) ; end of a modport declaration
  7820. (verilog-modport-decls-set
  7821. in-modport
  7822. (verilog-decls-new sigs-out sigs-inout sigs-in
  7823. nil nil nil nil nil nil))
  7824. ;; Pop from varstack to restore state to pre-clocking
  7825. (setq tmp (car varstack)
  7826. varstack (cdr varstack)
  7827. sigs-out (aref tmp 0)
  7828. sigs-inout (aref tmp 1)
  7829. sigs-in (aref tmp 2))
  7830. (setq vec nil io nil expect-signal nil newsig nil paren 0 rvalue nil
  7831. v2kargs-ok nil in-modport nil ign-prop nil))
  7832. (t
  7833. (setq vec nil io nil expect-signal nil newsig nil paren 0 rvalue nil
  7834. v2kargs-ok nil in-modport nil ign-prop nil)))
  7835. (forward-char 1))
  7836. ((eq ?= (following-char))
  7837. (setq rvalue t newsig nil)
  7838. (forward-char 1))
  7839. ((and (eq ?, (following-char))
  7840. (eq paren sig-paren))
  7841. (setq rvalue nil)
  7842. (forward-char 1))
  7843. ;; ,'s can occur inside {} & funcs
  7844. ((looking-at "[{(]")
  7845. (setq paren (1+ paren))
  7846. (forward-char 1))
  7847. ((looking-at "[})]")
  7848. (setq paren (1- paren))
  7849. (forward-char 1)
  7850. (when (< paren sig-paren)
  7851. (setq expect-signal nil rvalue nil))) ; ) that ends variables inside v2k arg list
  7852. ((looking-at "\\s-*\\(\\[[^]]+\\]\\)")
  7853. (goto-char (match-end 0))
  7854. (cond (newsig ; Memory, not just width. Patch last signal added's memory (nth 3)
  7855. (setcar (cdr (cdr (cdr newsig)))
  7856. (if (verilog-sig-memory newsig)
  7857. (concat (verilog-sig-memory newsig) (match-string 1))
  7858. (match-string-no-properties 1))))
  7859. (vec ; Multidimensional
  7860. (setq multidim (cons vec multidim))
  7861. (setq vec (verilog-string-replace-matches
  7862. "\\s-+" "" nil nil (match-string-no-properties 1))))
  7863. (t ; Bit width
  7864. (setq vec (verilog-string-replace-matches
  7865. "\\s-+" "" nil nil (match-string-no-properties 1))))))
  7866. ;; Normal or escaped identifier -- note we remember the \ if escaped
  7867. ((looking-at "\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
  7868. (goto-char (match-end 0))
  7869. (setq last-keywd keywd
  7870. keywd (match-string-no-properties 1))
  7871. (when (string-match "^\\\\" (match-string 1))
  7872. (setq keywd (concat keywd " "))) ; Escaped ID needs space at end
  7873. ;; Add any :: package names to same identifier
  7874. (while (looking-at "\\s-*::\\s-*\\([a-zA-Z0-9`_$]+\\|\\\\[^ \t\n\f]+\\)")
  7875. (goto-char (match-end 0))
  7876. (setq keywd (concat keywd "::" (match-string 1)))
  7877. (when (string-match "^\\\\" (match-string 1))
  7878. (setq keywd (concat keywd " ")))) ; Escaped ID needs space at end
  7879. (cond ((equal keywd "input")
  7880. (setq vec nil enum nil rvalue nil newsig nil signed nil
  7881. typedefed nil multidim nil ptype nil modport nil
  7882. expect-signal 'sigs-in io t sig-paren paren))
  7883. ((equal keywd "output")
  7884. (setq vec nil enum nil rvalue nil newsig nil signed nil
  7885. typedefed nil multidim nil ptype nil modport nil
  7886. expect-signal 'sigs-out io t sig-paren paren))
  7887. ((equal keywd "inout")
  7888. (setq vec nil enum nil rvalue nil newsig nil signed nil
  7889. typedefed nil multidim nil ptype nil modport nil
  7890. expect-signal 'sigs-inout io t sig-paren paren))
  7891. ((equal keywd "parameter")
  7892. (setq vec nil enum nil rvalue nil signed nil
  7893. typedefed nil multidim nil ptype nil modport nil
  7894. expect-signal 'sigs-gparam io t sig-paren paren))
  7895. ((member keywd '("wire" "reg" ; Fast
  7896. ;; net_type
  7897. "tri" "tri0" "tri1" "triand" "trior" "trireg"
  7898. "uwire" "wand" "wor"
  7899. ;; integer_atom_type
  7900. "byte" "shortint" "int" "longint" "integer" "time"
  7901. "supply0" "supply1"
  7902. ;; integer_vector_type - "reg" above
  7903. "bit" "logic"
  7904. ;; non_integer_type
  7905. "shortreal" "real" "realtime"
  7906. ;; data_type
  7907. "string" "event" "chandle"))
  7908. (cond (io
  7909. (setq typedefed
  7910. (if typedefed (concat typedefed " " keywd) keywd)))
  7911. (t (setq vec nil enum nil rvalue nil signed nil
  7912. typedefed nil multidim nil sig-paren paren
  7913. expect-signal 'sigs-var modport nil))))
  7914. ((equal keywd "assign")
  7915. (setq vec nil enum nil rvalue nil signed nil
  7916. typedefed nil multidim nil ptype nil modport nil
  7917. expect-signal 'sigs-assign sig-paren paren))
  7918. ((member keywd '("localparam" "genvar"))
  7919. (setq vec nil enum nil rvalue nil signed nil
  7920. typedefed nil multidim nil ptype nil modport nil
  7921. expect-signal 'sigs-const sig-paren paren))
  7922. ((member keywd '("signed" "unsigned"))
  7923. (setq signed keywd))
  7924. ((member keywd '("assert" "assume" "cover" "expect" "restrict"))
  7925. (setq ign-prop t))
  7926. ((member keywd '("class" "covergroup" "function"
  7927. "property" "randsequence" "sequence" "task"))
  7928. (unless ign-prop
  7929. (setq functask (1+ functask))))
  7930. ((member keywd '("endclass" "endgroup" "endfunction"
  7931. "endproperty" "endsequence" "endtask"))
  7932. (setq functask (1- functask)))
  7933. ((equal keywd "modport")
  7934. (setq in-modport t))
  7935. ((and (equal keywd "clocking")
  7936. (not (equal last-keywd "default")))
  7937. (setq in-clocking t))
  7938. ((equal keywd "import")
  7939. (if v2kargs-ok ; import in module header, not a modport import
  7940. (setq in-ign-to-semi t rvalue t)))
  7941. ((equal keywd "type")
  7942. (setq ptype t))
  7943. ((equal keywd "var"))
  7944. ;; Ifdef? Ignore name of define
  7945. ((member keywd '("`ifdef" "`ifndef" "`elsif"))
  7946. (setq rvalue t))
  7947. ;; Type?
  7948. ((unless ptype
  7949. (verilog-typedef-name-p keywd))
  7950. (cond (io
  7951. (setq typedefed
  7952. (if typedefed (concat typedefed " " keywd) keywd)))
  7953. (t (setq vec nil enum nil rvalue nil signed nil
  7954. typedefed keywd ; Have a type
  7955. multidim nil sig-paren paren
  7956. expect-signal 'sigs-var modport nil))))
  7957. ;; Interface with optional modport in v2k arglist?
  7958. ;; Skip over parsing modport, and take the interface name as the type
  7959. ((and v2kargs-ok
  7960. (eq paren 1)
  7961. (not rvalue)
  7962. (looking-at "\\s-*\\(\\.\\(\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*\\)\\|\\)\\s-*[a-zA-Z`_$][a-zA-Z0-9`_$]*"))
  7963. (when (match-end 2) (goto-char (match-end 2)))
  7964. (setq vec nil enum nil rvalue nil signed nil
  7965. typedefed keywd multidim nil ptype nil modport (match-string 2)
  7966. newsig nil sig-paren paren
  7967. expect-signal 'sigs-intf io t ))
  7968. ;; Ignore dotted LHS assignments: "assign foo.bar = z;"
  7969. ((looking-at "\\s-*\\.")
  7970. (goto-char (match-end 0))
  7971. (when (not rvalue)
  7972. (setq expect-signal nil)))
  7973. ;; "modport <keywd>"
  7974. ((and (eq in-modport t)
  7975. (not (member keywd verilog-keywords)))
  7976. (setq in-modport (verilog-modport-new keywd nil nil))
  7977. (setq sigs-modports (cons in-modport sigs-modports))
  7978. ;; Push old sig values to stack and point to new signal list
  7979. (setq varstack (cons (vector sigs-out sigs-inout sigs-in)
  7980. varstack))
  7981. (setq sigs-in nil sigs-inout nil sigs-out nil))
  7982. ;; "modport x (clocking <keywd>)"
  7983. ((and in-modport in-clocking)
  7984. (verilog-modport-clockings-add in-modport keywd)
  7985. (setq in-clocking nil))
  7986. ;; endclocking
  7987. ((and in-clocking
  7988. (equal keywd "endclocking"))
  7989. (unless (eq in-clocking t)
  7990. (verilog-modport-decls-set
  7991. in-clocking
  7992. (verilog-decls-new sigs-out sigs-inout sigs-in
  7993. nil nil nil nil nil nil))
  7994. ;; Pop from varstack to restore state to pre-clocking
  7995. (setq tmp (car varstack)
  7996. varstack (cdr varstack)
  7997. sigs-out (aref tmp 0)
  7998. sigs-inout (aref tmp 1)
  7999. sigs-in (aref tmp 2)))
  8000. (setq in-clocking nil))
  8001. ;; "clocking <keywd>"
  8002. ((and (eq in-clocking t)
  8003. (not (member keywd verilog-keywords)))
  8004. (setq in-clocking (verilog-modport-new keywd nil nil))
  8005. (setq sigs-modports (cons in-clocking sigs-modports))
  8006. ;; Push old sig values to stack and point to new signal list
  8007. (setq varstack (cons (vector sigs-out sigs-inout sigs-in)
  8008. varstack))
  8009. (setq sigs-in nil sigs-inout nil sigs-out nil))
  8010. ;; New signal, maybe?
  8011. ((and expect-signal
  8012. (not rvalue)
  8013. (eq functask 0)
  8014. (not (member keywd verilog-keywords)))
  8015. ;; Add new signal to expect-signal's variable
  8016. ;;(if dbg (setq dbg (concat dbg (format "Pt %s New sig %s'\n" (point) keywd))))
  8017. (setq newsig (verilog-sig-new keywd vec nil nil enum signed typedefed multidim modport))
  8018. (set expect-signal (cons newsig
  8019. (symbol-value expect-signal))))))
  8020. (t
  8021. (forward-char 1)))
  8022. (skip-syntax-forward " "))
  8023. ;; Return arguments
  8024. (setq tmp (verilog-decls-new (nreverse sigs-out)
  8025. (nreverse sigs-inout)
  8026. (nreverse sigs-in)
  8027. (nreverse sigs-var)
  8028. (nreverse sigs-modports)
  8029. (nreverse sigs-assign)
  8030. (nreverse sigs-const)
  8031. (nreverse sigs-gparam)
  8032. (nreverse sigs-intf)))
  8033. ;;(if dbg (verilog-decls-princ tmp))
  8034. tmp)))
  8035. (defvar verilog-read-sub-decls-in-interfaced nil
  8036. "For `verilog-read-sub-decls', process next signal as under interfaced block.")
  8037. (defvar verilog-read-sub-decls-gate-ios nil
  8038. "For `verilog-read-sub-decls', gate IO pins remaining, nil if non-primitive.")
  8039. (eval-when-compile
  8040. ;; Prevent compile warnings; these are let's, not globals
  8041. ;; Do not remove the eval-when-compile
  8042. ;; - we want an error when we are debugging this code if they are refed.
  8043. (defvar sigs-in)
  8044. (defvar sigs-inout)
  8045. (defvar sigs-intf)
  8046. (defvar sigs-intfd)
  8047. (defvar sigs-out)
  8048. (defvar sigs-out-d)
  8049. (defvar sigs-out-i)
  8050. (defvar sigs-out-unk)
  8051. (defvar sigs-temp)
  8052. ;; These are known to be from other packages and may not be defined
  8053. (defvar diff-command)
  8054. ;; There are known to be from newer versions of Emacs
  8055. (defvar create-lockfiles)
  8056. (defvar which-func-modes))
  8057. (defun verilog-read-sub-decls-type (par-values portdata)
  8058. "For `verilog-read-sub-decls-line', decode a signal type."
  8059. (let* ((type (verilog-sig-type portdata))
  8060. (pvassoc (assoc type par-values)))
  8061. (cond ((member type '("wire" "reg")) nil)
  8062. (pvassoc (nth 1 pvassoc))
  8063. (t type))))
  8064. (defun verilog-read-sub-decls-sig (submoddecls par-values comment port sig vec multidim mem)
  8065. "For `verilog-read-sub-decls-line', add a signal."
  8066. ;; sig eq t to indicate .name syntax
  8067. ;;(message "vrsds: %s(%S)" port sig)
  8068. (let ((dotname (eq sig t))
  8069. portdata)
  8070. (when sig
  8071. (setq port (verilog-symbol-detick-denumber port))
  8072. (setq sig (if dotname port (verilog-symbol-detick-denumber sig)))
  8073. (if vec (setq vec (verilog-symbol-detick-denumber vec)))
  8074. (if multidim (setq multidim (mapcar `verilog-symbol-detick-denumber multidim)))
  8075. (if mem (setq mem (verilog-symbol-detick-denumber mem)))
  8076. (unless (or (not sig)
  8077. (equal sig "")) ; Ignore .foo(1'b1) assignments
  8078. (cond ((or (setq portdata (assoc port (verilog-decls-get-inouts submoddecls)))
  8079. (equal "inout" verilog-read-sub-decls-gate-ios))
  8080. (setq sigs-inout
  8081. (cons (verilog-sig-new
  8082. sig
  8083. (if dotname (verilog-sig-bits portdata) vec)
  8084. (concat "To/From " comment)
  8085. mem
  8086. nil
  8087. (verilog-sig-signed portdata)
  8088. (verilog-read-sub-decls-type par-values portdata)
  8089. multidim nil)
  8090. sigs-inout)))
  8091. ((or (setq portdata (assoc port (verilog-decls-get-outputs submoddecls)))
  8092. (equal "output" verilog-read-sub-decls-gate-ios))
  8093. (setq sigs-out
  8094. (cons (verilog-sig-new
  8095. sig
  8096. (if dotname (verilog-sig-bits portdata) vec)
  8097. (concat "From " comment)
  8098. mem
  8099. nil
  8100. (verilog-sig-signed portdata)
  8101. ;; Though ok in SV, in V2K code, propagating the
  8102. ;; "reg" in "output reg" upwards isn't legal.
  8103. ;; Also for backwards compatibility we don't propagate
  8104. ;; "input wire" upwards.
  8105. ;; See also `verilog-signals-edit-wire-reg'.
  8106. (verilog-read-sub-decls-type par-values portdata)
  8107. multidim nil)
  8108. sigs-out)))
  8109. ((or (setq portdata (assoc port (verilog-decls-get-inputs submoddecls)))
  8110. (equal "input" verilog-read-sub-decls-gate-ios))
  8111. (setq sigs-in
  8112. (cons (verilog-sig-new
  8113. sig
  8114. (if dotname (verilog-sig-bits portdata) vec)
  8115. (concat "To " comment)
  8116. mem
  8117. nil
  8118. (verilog-sig-signed portdata)
  8119. (verilog-read-sub-decls-type par-values portdata)
  8120. multidim nil)
  8121. sigs-in)))
  8122. ((setq portdata (assoc port (verilog-decls-get-interfaces submoddecls)))
  8123. (setq sigs-intf
  8124. (cons (verilog-sig-new
  8125. sig
  8126. (if dotname (verilog-sig-bits portdata) vec)
  8127. (concat "To/From " comment)
  8128. mem
  8129. nil
  8130. (verilog-sig-signed portdata)
  8131. (verilog-read-sub-decls-type par-values portdata)
  8132. multidim nil)
  8133. sigs-intf)))
  8134. ((setq portdata (and verilog-read-sub-decls-in-interfaced
  8135. (assoc port (verilog-decls-get-vars submoddecls))))
  8136. (setq sigs-intfd
  8137. (cons (verilog-sig-new
  8138. sig
  8139. (if dotname (verilog-sig-bits portdata) vec)
  8140. (concat "To/From " comment)
  8141. mem
  8142. nil
  8143. (verilog-sig-signed portdata)
  8144. (verilog-read-sub-decls-type par-values portdata)
  8145. multidim nil)
  8146. sigs-intf)))
  8147. ;; (t -- warning pin isn't defined.) ; Leave for lint tool
  8148. )))))
  8149. (defun verilog-read-sub-decls-expr (submoddecls par-values comment port expr)
  8150. "For `verilog-read-sub-decls-line', parse a subexpression and add signals."
  8151. ;;(message "vrsde: `%s'" expr)
  8152. ;; Replace special /*[....]*/ comments inserted by verilog-auto-inst-port
  8153. (setq expr (verilog-string-replace-matches "/\\*\\(\\.?\\[[^*]+\\]\\)\\*/" "\\1" nil nil expr))
  8154. ;; Remove front operators
  8155. (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
  8156. ;;
  8157. (cond
  8158. ;; {..., a, b} requires us to recurse on a,b
  8159. ;; To support {#{},{#{a,b}} we'll just split everything on [{},]
  8160. ((string-match "^\\s-*{\\(.*\\)}\\s-*$" expr)
  8161. (unless verilog-auto-ignore-concat
  8162. (let ((mlst (split-string (match-string 1 expr) "[{},]"))
  8163. mstr)
  8164. (while (setq mstr (pop mlst))
  8165. (verilog-read-sub-decls-expr submoddecls par-values comment port mstr)))))
  8166. (t
  8167. (let (sig vec multidim mem)
  8168. ;; Remove leading reduction operators, etc
  8169. (setq expr (verilog-string-replace-matches "^\\s-*[---+~!|&]+\\s-*" "" nil nil expr))
  8170. ;;(message "vrsde-ptop: `%s'" expr)
  8171. (cond ; Find \signal. Final space is part of escaped signal name
  8172. ((string-match "^\\s-*\\(\\\\[^ \t\n\f]+\\s-\\)" expr)
  8173. ;;(message "vrsde-s: `%s'" (match-string 1 expr))
  8174. (setq sig (match-string 1 expr)
  8175. expr (substring expr (match-end 0))))
  8176. ;; Find signal
  8177. ((string-match "^\\s-*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" expr)
  8178. ;;(message "vrsde-s: `%s'" (match-string 1 expr))
  8179. (setq sig (verilog-string-remove-spaces (match-string 1 expr))
  8180. expr (substring expr (match-end 0)))))
  8181. ;; Find [vector] or [multi][multi][multi][vector]
  8182. (while (string-match "^\\s-*\\(\\[[^]]+\\]\\)" expr)
  8183. ;;(message "vrsde-v: `%s'" (match-string 1 expr))
  8184. (when vec (setq multidim (cons vec multidim)))
  8185. (setq vec (match-string 1 expr)
  8186. expr (substring expr (match-end 0))))
  8187. ;; Find .[unpacked_memory] or .[unpacked][unpacked]...
  8188. (while (string-match "^\\s-*\\.\\(\\(\\[[^]]+\\]\\)+\\)" expr)
  8189. ;;(message "vrsde-m: `%s'" (match-string 1 expr))
  8190. (setq mem (match-string 1 expr)
  8191. expr (substring expr (match-end 0))))
  8192. ;; If found signal, and nothing unrecognized, add the signal
  8193. ;;(message "vrsde-rem: `%s'" expr)
  8194. (when (and sig (string-match "^\\s-*$" expr))
  8195. (verilog-read-sub-decls-sig submoddecls par-values comment port sig vec multidim mem))))))
  8196. (defun verilog-read-sub-decls-line (submoddecls par-values comment)
  8197. "For `verilog-read-sub-decls', read lines of port defs until none match.
  8198. Inserts the list of signals found, using submodi to look up each port."
  8199. (let (done port)
  8200. (save-excursion
  8201. (forward-line 1)
  8202. (while (not done)
  8203. ;; Get port name
  8204. (cond ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*(\\s-*")
  8205. (setq port (match-string-no-properties 1))
  8206. (goto-char (match-end 0)))
  8207. ;; .\escaped (
  8208. ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*(\\s-*")
  8209. (setq port (concat (match-string-no-properties 1) " ")) ; escaped id's need trailing space
  8210. (goto-char (match-end 0)))
  8211. ;; .name
  8212. ((looking-at "\\s-*\\.\\s-*\\([a-zA-Z0-9`_$]*\\)\\s-*[,)/]")
  8213. (verilog-read-sub-decls-sig
  8214. submoddecls par-values comment (match-string-no-properties 1) t ; sig==t for .name
  8215. nil nil nil) ; vec multidim mem
  8216. (setq port nil))
  8217. ;; .\escaped_name
  8218. ((looking-at "\\s-*\\.\\s-*\\(\\\\[^ \t\n\f]*\\)\\s-*[,)/]")
  8219. (verilog-read-sub-decls-sig
  8220. submoddecls par-values comment (concat (match-string-no-properties 1) " ") t ; sig==t for .name
  8221. nil nil nil) ; vec multidim mem
  8222. (setq port nil))
  8223. ;; random
  8224. ((looking-at "\\s-*\\.[^(]*(")
  8225. (setq port nil) ; skip this line
  8226. (goto-char (match-end 0)))
  8227. (t
  8228. (setq port nil done t))) ; Unknown, ignore rest of line
  8229. ;; Get signal name. Point is at the first-non-space after (
  8230. ;; We intentionally ignore (non-escaped) signals with .s in them
  8231. ;; this prevents AUTOWIRE etc from noticing hierarchical sigs.
  8232. (when port
  8233. (cond ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*)")
  8234. (verilog-read-sub-decls-sig
  8235. submoddecls par-values comment port
  8236. (verilog-string-remove-spaces (match-string-no-properties 1)) ; sig
  8237. nil nil nil)) ; vec multidim mem
  8238. ;;
  8239. ((looking-at "\\([a-zA-Z_][a-zA-Z_0-9]*\\)\\s-*\\(\\[[^]]+\\]\\)\\s-*)")
  8240. (verilog-read-sub-decls-sig
  8241. submoddecls par-values comment port
  8242. (verilog-string-remove-spaces (match-string-no-properties 1)) ; sig
  8243. (match-string-no-properties 2) nil nil)) ; vec multidim mem
  8244. ;; Fastpath was above looking-at's.
  8245. ;; For something more complicated invoke a parser
  8246. ((looking-at "[^)]+")
  8247. (verilog-read-sub-decls-expr
  8248. submoddecls par-values comment port
  8249. (buffer-substring-no-properties
  8250. (point) (1- (progn (search-backward "(") ; start at (
  8251. (verilog-forward-sexp-ign-cmt 1)
  8252. (point)))))))) ; expr
  8253. ;;
  8254. (forward-line 1)))))
  8255. ;;(verilog-read-sub-decls-line (verilog-subdecls-new nil nil nil nil nil) nil "Cmt")
  8256. (defun verilog-read-sub-decls-gate (submoddecls par-values comment submod end-inst-point)
  8257. "For `verilog-read-sub-decls', read lines of UDP gate decl until none match.
  8258. Inserts the list of signals found."
  8259. (save-excursion
  8260. (let ((iolist (cdr (assoc submod verilog-gate-ios))))
  8261. (while (< (point) end-inst-point)
  8262. ;; Get primitive's signal name, as will never have port, and no trailing )
  8263. (cond ((looking-at "//")
  8264. (search-forward "\n"))
  8265. ((looking-at "/\\*")
  8266. (or (search-forward "*/")
  8267. (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
  8268. ((looking-at "(\\*")
  8269. ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
  8270. (forward-char 1)
  8271. (or (search-forward "*)")
  8272. (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
  8273. ;; On pins, parse and advance to next pin
  8274. ;; Looking at pin, but *not* an // Output comment, or ) to end the inst
  8275. ((looking-at "\\s-*[a-zA-Z0-9`_$({}\\\\][^,]*")
  8276. (goto-char (match-end 0))
  8277. (setq verilog-read-sub-decls-gate-ios (or (car iolist) "input")
  8278. iolist (cdr iolist))
  8279. (verilog-read-sub-decls-expr
  8280. submoddecls par-values comment "primitive_port"
  8281. (match-string 0)))
  8282. (t
  8283. (forward-char 1)
  8284. (skip-syntax-forward " ")))))))
  8285. (defun verilog-read-sub-decls ()
  8286. "Internally parse signals going to modules under this module.
  8287. Return an array of [ outputs inouts inputs ] signals for modules that are
  8288. instantiated in this module. For example if declare A A (.B(SIG)) and SIG
  8289. is an output, then SIG will be included in the list.
  8290. This only works on instantiations created with /*AUTOINST*/ converted by
  8291. \\[verilog-auto-inst]. Otherwise, it would have to read in the whole
  8292. component library to determine connectivity of the design.
  8293. One work around for this problem is to manually create // Inputs and //
  8294. Outputs comments above subcell signals, for example:
  8295. module ModuleName (
  8296. // Outputs
  8297. .out (out),
  8298. // Inputs
  8299. .in (in));"
  8300. (save-excursion
  8301. (let ((end-mod-point (verilog-get-end-of-defun))
  8302. st-point end-inst-point par-values
  8303. ;; below 3 modified by verilog-read-sub-decls-line
  8304. sigs-out sigs-inout sigs-in sigs-intf sigs-intfd)
  8305. (verilog-beg-of-defun-quick)
  8306. (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-mod-point t)
  8307. (save-excursion
  8308. (goto-char (match-beginning 0))
  8309. (setq par-values (and verilog-auto-inst-param-value
  8310. verilog-auto-inst-param-value-type
  8311. (verilog-read-inst-param-value)))
  8312. (unless (verilog-inside-comment-or-string-p)
  8313. ;; Attempt to snarf a comment
  8314. (let* ((submod (verilog-read-inst-module))
  8315. (inst (verilog-read-inst-name))
  8316. (subprim (member submod verilog-gate-keywords))
  8317. (comment (concat inst " of " submod ".v"))
  8318. submodi submoddecls)
  8319. (cond
  8320. (subprim
  8321. (setq submodi `primitive
  8322. submoddecls (verilog-decls-new nil nil nil nil nil nil nil nil nil)
  8323. comment (concat inst " of " submod))
  8324. (verilog-backward-open-paren)
  8325. (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
  8326. (point))
  8327. st-point (point))
  8328. (forward-char 1)
  8329. (verilog-read-sub-decls-gate submoddecls par-values comment submod end-inst-point))
  8330. ;; Non-primitive
  8331. (t
  8332. (when (setq submodi (verilog-modi-lookup submod t))
  8333. (setq submoddecls (verilog-modi-get-decls submodi)
  8334. verilog-read-sub-decls-gate-ios nil)
  8335. (verilog-backward-open-paren)
  8336. (setq end-inst-point (save-excursion (verilog-forward-sexp-ign-cmt 1)
  8337. (point))
  8338. st-point (point))
  8339. ;; This could have used a list created by verilog-auto-inst
  8340. ;; However I want it to be runnable even on user's manually added signals
  8341. (let ((verilog-read-sub-decls-in-interfaced t))
  8342. (while (re-search-forward "\\s *(?\\s *// Interfaced" end-inst-point t)
  8343. (verilog-read-sub-decls-line submoddecls par-values comment))) ; Modifies sigs-ifd
  8344. (goto-char st-point)
  8345. (while (re-search-forward "\\s *(?\\s *// Interfaces" end-inst-point t)
  8346. (verilog-read-sub-decls-line submoddecls par-values comment)) ; Modifies sigs-out
  8347. (goto-char st-point)
  8348. (while (re-search-forward "\\s *(?\\s *// Outputs" end-inst-point t)
  8349. (verilog-read-sub-decls-line submoddecls par-values comment)) ; Modifies sigs-out
  8350. (goto-char st-point)
  8351. (while (re-search-forward "\\s *(?\\s *// Inouts" end-inst-point t)
  8352. (verilog-read-sub-decls-line submoddecls par-values comment)) ; Modifies sigs-inout
  8353. (goto-char st-point)
  8354. (while (re-search-forward "\\s *(?\\s *// Inputs" end-inst-point t)
  8355. (verilog-read-sub-decls-line submoddecls par-values comment)) ; Modifies sigs-in
  8356. )))))))
  8357. ;; Combine duplicate bits
  8358. ;;(setq rr (vector sigs-out sigs-inout sigs-in))
  8359. (verilog-subdecls-new
  8360. (verilog-signals-combine-bus (nreverse sigs-out))
  8361. (verilog-signals-combine-bus (nreverse sigs-inout))
  8362. (verilog-signals-combine-bus (nreverse sigs-in))
  8363. (verilog-signals-combine-bus (nreverse sigs-intf))
  8364. (verilog-signals-combine-bus (nreverse sigs-intfd))))))
  8365. (defun verilog-read-inst-pins ()
  8366. "Return an array of [ pins ] for the current instantiation at point.
  8367. For example if declare A A (.B(SIG)) then B will be included in the list."
  8368. (save-excursion
  8369. (let ((end-mod-point (point)) ; presume at /*AUTOINST*/ point
  8370. pins pin)
  8371. (verilog-backward-open-paren)
  8372. (while (re-search-forward "\\.\\([^(,) \t\n\f]*\\)\\s-*" end-mod-point t)
  8373. (setq pin (match-string 1))
  8374. (unless (verilog-inside-comment-or-string-p)
  8375. (setq pins (cons (list pin) pins))
  8376. (when (looking-at "(")
  8377. (verilog-forward-sexp-ign-cmt 1))))
  8378. (vector pins))))
  8379. (defun verilog-read-arg-pins ()
  8380. "Return an array of [ pins ] for the current argument declaration at point."
  8381. (save-excursion
  8382. (let ((end-mod-point (point)) ; presume at /*AUTOARG*/ point
  8383. pins pin)
  8384. (verilog-backward-open-paren)
  8385. (while (re-search-forward "\\([a-zA-Z0-9$_.%`]+\\)" end-mod-point t)
  8386. (setq pin (match-string 1))
  8387. (unless (verilog-inside-comment-or-string-p)
  8388. (setq pins (cons (list pin) pins))))
  8389. (vector pins))))
  8390. (defun verilog-read-auto-constants (beg end-mod-point)
  8391. "Return a list of AUTO_CONSTANTs used in the region from BEG to END-MOD-POINT."
  8392. ;; Insert new
  8393. (save-excursion
  8394. (let (sig-list tpl-end-pt)
  8395. (goto-char beg)
  8396. (while (re-search-forward "\\<AUTO_CONSTANT" end-mod-point t)
  8397. (if (not (looking-at "\\s *("))
  8398. (error "%s: Missing () after AUTO_CONSTANT" (verilog-point-text)))
  8399. (search-forward "(" end-mod-point)
  8400. (setq tpl-end-pt (save-excursion
  8401. (backward-char 1)
  8402. (verilog-forward-sexp-cmt 1) ; Moves to paren that closes argdecl's
  8403. (backward-char 1)
  8404. (point)))
  8405. (while (re-search-forward "\\s-*\\([\"a-zA-Z0-9$_.%`]+\\)\\s-*,*" tpl-end-pt t)
  8406. (setq sig-list (cons (list (match-string 1) nil nil) sig-list))))
  8407. sig-list)))
  8408. (defvar verilog-cache-has-lisp nil "True if any AUTO_LISP in buffer.")
  8409. (make-variable-buffer-local 'verilog-cache-has-lisp)
  8410. (defun verilog-read-auto-lisp-present ()
  8411. "Set `verilog-cache-has-lisp' if any AUTO_LISP in this buffer."
  8412. (save-excursion
  8413. (goto-char (point-min))
  8414. (setq verilog-cache-has-lisp (re-search-forward "\\<AUTO_LISP(" nil t))))
  8415. (defun verilog-read-auto-lisp (start end)
  8416. "Look for and evaluate an AUTO_LISP between START and END.
  8417. Must call `verilog-read-auto-lisp-present' before this function."
  8418. ;; This function is expensive for large buffers, so we cache if any AUTO_LISP exists
  8419. (when verilog-cache-has-lisp
  8420. (save-excursion
  8421. (goto-char start)
  8422. (while (re-search-forward "\\<AUTO_LISP(" end t)
  8423. (backward-char)
  8424. (let* ((beg-pt (prog1 (point)
  8425. (verilog-forward-sexp-cmt 1))) ; Closing paren
  8426. (end-pt (point))
  8427. (verilog-in-hooks t))
  8428. (eval-region beg-pt end-pt nil))))))
  8429. (defun verilog-read-always-signals-recurse
  8430. (exit-keywd rvalue temp-next)
  8431. "Recursive routine for parentheses/bracket matching.
  8432. EXIT-KEYWD is expression to stop at, nil if top level.
  8433. RVALUE is true if at right hand side of equal.
  8434. IGNORE-NEXT is true to ignore next token, fake from inside case statement."
  8435. (let* ((semi-rvalue (equal "endcase" exit-keywd)) ; true if after a ; we are looking for rvalue
  8436. keywd last-keywd sig-tolk sig-last-tolk gotend got-sig got-list end-else-check
  8437. ignore-next)
  8438. ;;(if dbg (setq dbg (concat dbg (format "Recursion %S %S %S\n" exit-keywd rvalue temp-next))))
  8439. (while (not (or (eobp) gotend))
  8440. (cond
  8441. ((looking-at "//")
  8442. (search-forward "\n"))
  8443. ((looking-at "/\\*")
  8444. (or (search-forward "*/")
  8445. (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
  8446. ((looking-at "(\\*")
  8447. ;; To advance past either "(*)" or "(* ... *)" don't forward past first *
  8448. (forward-char 1)
  8449. (or (search-forward "*)")
  8450. (error "%s: Unmatched (* *), at char %d" (verilog-point-text) (point))))
  8451. (t (setq keywd (buffer-substring-no-properties
  8452. (point)
  8453. (save-excursion (when (eq 0 (skip-chars-forward "a-zA-Z0-9$_.%`"))
  8454. (forward-char 1))
  8455. (point)))
  8456. sig-last-tolk sig-tolk
  8457. sig-tolk nil)
  8458. ;;(if dbg (setq dbg (concat dbg (format "\tPt=%S %S\trv=%S in=%S ee=%S gs=%S\n" (point) keywd rvalue ignore-next end-else-check got-sig))))
  8459. (cond
  8460. ((equal keywd "\"")
  8461. (or (re-search-forward "[^\\]\"" nil t)
  8462. (error "%s: Unmatched quotes, at char %d" (verilog-point-text) (point))))
  8463. ;; else at top level loop, keep parsing
  8464. ((and end-else-check (equal keywd "else"))
  8465. ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else %s\n" keywd))))
  8466. ;; no forward movement, want to see else in lower loop
  8467. (setq end-else-check nil))
  8468. ;; End at top level loop
  8469. ((and end-else-check (looking-at "[^ \t\n\f]"))
  8470. ;;(if dbg (setq dbg (concat dbg (format "\tif-check-else-other %s\n" keywd))))
  8471. (setq gotend t))
  8472. ;; Final statement?
  8473. ((and exit-keywd (and (equal keywd exit-keywd)
  8474. (not (looking-at "::"))))
  8475. (setq gotend t)
  8476. (forward-char (length keywd)))
  8477. ;; Standard tokens...
  8478. ((equal keywd ";")
  8479. (setq ignore-next nil rvalue semi-rvalue)
  8480. ;; Final statement at top level loop?
  8481. (when (not exit-keywd)
  8482. ;;(if dbg (setq dbg (concat dbg (format "\ttop-end-check %s\n" keywd))))
  8483. (setq end-else-check t))
  8484. (forward-char 1))
  8485. ((equal keywd "'")
  8486. (if (looking-at "'[sS]?[hdxboHDXBO]?[ \t]*[0-9a-fA-F_xzXZ?]+")
  8487. (goto-char (match-end 0))
  8488. (forward-char 1)))
  8489. ((equal keywd ":") ; Case statement, begin/end label, x?y:z
  8490. (cond ((looking-at "::")
  8491. (forward-char 1)) ; Another forward-char below
  8492. ((equal "endcase" exit-keywd) ; case x: y=z; statement next
  8493. (setq ignore-next nil rvalue nil))
  8494. ((equal "?" exit-keywd) ; x?y:z rvalue
  8495. ) ; NOP
  8496. ((equal "]" exit-keywd) ; [x:y] rvalue
  8497. ) ; NOP
  8498. (got-sig ; label: statement
  8499. (setq ignore-next nil rvalue semi-rvalue got-sig nil))
  8500. ((not rvalue) ; begin label
  8501. (setq ignore-next t rvalue nil)))
  8502. (forward-char 1))
  8503. ((equal keywd "=")
  8504. (when got-sig
  8505. ;;(if dbg (setq dbg (concat dbg (format "\t\tequal got-sig=%S got-list=%s\n" got-sig got-list))))
  8506. (set got-list (cons got-sig (symbol-value got-list)))
  8507. (setq got-sig nil))
  8508. (when (not rvalue)
  8509. (if (eq (char-before) ?< )
  8510. (setq sigs-out-d (append sigs-out-d sigs-out-unk)
  8511. sigs-out-unk nil)
  8512. (setq sigs-out-i (append sigs-out-i sigs-out-unk)
  8513. sigs-out-unk nil)))
  8514. (setq ignore-next nil rvalue t)
  8515. (forward-char 1))
  8516. ((equal keywd "?")
  8517. (forward-char 1)
  8518. (verilog-read-always-signals-recurse ":" rvalue nil))
  8519. ((equal keywd "[")
  8520. (forward-char 1)
  8521. (verilog-read-always-signals-recurse "]" t nil))
  8522. ((equal keywd "(")
  8523. (forward-char 1)
  8524. (cond (sig-last-tolk ; Function call; zap last signal
  8525. (setq got-sig nil)))
  8526. (cond ((equal last-keywd "for")
  8527. ;; temp-next: Variables on LHS are lvalues, but generally we want
  8528. ;; to ignore them, assuming they are loop increments
  8529. (verilog-read-always-signals-recurse ";" nil t)
  8530. (verilog-read-always-signals-recurse ";" t nil)
  8531. (verilog-read-always-signals-recurse ")" nil nil))
  8532. (t (verilog-read-always-signals-recurse ")" t nil))))
  8533. ((equal keywd "begin")
  8534. (skip-syntax-forward "w_")
  8535. (verilog-read-always-signals-recurse "end" nil nil)
  8536. ;;(if dbg (setq dbg (concat dbg (format "\tgot-end %s\n" exit-keywd))))
  8537. (setq ignore-next nil rvalue semi-rvalue)
  8538. (if (not exit-keywd) (setq end-else-check t)))
  8539. ((member keywd '("case" "casex" "casez" "randcase"))
  8540. (skip-syntax-forward "w_")
  8541. (verilog-read-always-signals-recurse "endcase" t nil)
  8542. (setq ignore-next nil rvalue semi-rvalue)
  8543. (if (not exit-keywd) (setq gotend t))) ; top level begin/end
  8544. ((string-match "^[$`a-zA-Z_]" keywd) ; not exactly word constituent
  8545. (cond ((member keywd '("`ifdef" "`ifndef" "`elsif"))
  8546. (setq ignore-next t))
  8547. ((or ignore-next
  8548. (member keywd verilog-keywords)
  8549. (string-match "^\\$" keywd)) ; PLI task
  8550. (setq ignore-next nil))
  8551. (t
  8552. (setq keywd (verilog-symbol-detick-denumber keywd))
  8553. (when got-sig
  8554. (set got-list (cons got-sig (symbol-value got-list)))
  8555. ;;(if dbg (setq dbg (concat dbg (format "\t\tgot-sig=%S got-list=%S\n" got-sig got-list))))
  8556. )
  8557. (setq got-list (cond (temp-next 'sigs-temp)
  8558. (rvalue 'sigs-in)
  8559. (t 'sigs-out-unk))
  8560. got-sig (if (or (not keywd)
  8561. (assoc keywd (symbol-value got-list)))
  8562. nil (list keywd nil nil))
  8563. temp-next nil
  8564. sig-tolk t)))
  8565. (skip-chars-forward "a-zA-Z0-9$_.%`"))
  8566. (t
  8567. (forward-char 1)))
  8568. ;; End of non-comment token
  8569. (setq last-keywd keywd)))
  8570. (skip-syntax-forward " "))
  8571. ;; Append the final pending signal
  8572. (when got-sig
  8573. ;;(if dbg (setq dbg (concat dbg (format "\t\tfinal got-sig=%S got-list=%s\n" got-sig got-list))))
  8574. (set got-list (cons got-sig (symbol-value got-list)))
  8575. (setq got-sig nil))
  8576. ;;(if dbg (setq dbg (concat dbg (format "ENDRecursion %s\n" exit-keywd))))
  8577. ))
  8578. (defun verilog-read-always-signals ()
  8579. "Parse always block at point and return list of (outputs inout inputs)."
  8580. (save-excursion
  8581. (let* (;(dbg "")
  8582. sigs-out-d sigs-out-i sigs-out-unk sigs-temp sigs-in)
  8583. (verilog-read-always-signals-recurse nil nil nil)
  8584. (setq sigs-out-i (append sigs-out-i sigs-out-unk)
  8585. sigs-out-unk nil)
  8586. ;;(if dbg (with-current-buffer (get-buffer-create "*vl-dbg*") (delete-region (point-min) (point-max)) (insert dbg) (setq dbg "")))
  8587. ;; Return what was found
  8588. (verilog-alw-new sigs-out-d sigs-out-i sigs-temp sigs-in))))
  8589. (defun verilog-read-instants ()
  8590. "Parse module at point and return list of ( ( file instance ) ... )."
  8591. (verilog-beg-of-defun-quick)
  8592. (let* ((end-mod-point (verilog-get-end-of-defun))
  8593. (state nil)
  8594. (instants-list nil))
  8595. (save-excursion
  8596. (while (< (point) end-mod-point)
  8597. ;; Stay at level 0, no comments
  8598. (while (progn
  8599. (setq state (parse-partial-sexp (point) end-mod-point 0 t nil))
  8600. (or (> (car state) 0) ; in parens
  8601. (nth 5 state) ; comment
  8602. ))
  8603. (forward-line 1))
  8604. (beginning-of-line)
  8605. (if (looking-at "^\\s-*\\([a-zA-Z0-9`_$]+\\)\\s-+\\([a-zA-Z0-9`_$]+\\)\\s-*(")
  8606. ;;(if (looking-at "^\\(.+\\)$")
  8607. (let ((module (match-string 1))
  8608. (instant (match-string 2)))
  8609. (if (not (member module verilog-keywords))
  8610. (setq instants-list (cons (list module instant) instants-list)))))
  8611. (forward-line 1)))
  8612. instants-list))
  8613. (defun verilog-read-auto-template-middle ()
  8614. "With point in middle of an AUTO_TEMPLATE, parse it.
  8615. Returns REGEXP and list of ( (signal_name connection_name)... )."
  8616. (save-excursion
  8617. ;; Find beginning
  8618. (let ((tpl-regexp "\\([0-9]+\\)")
  8619. (lineno -1) ; -1 to offset for the AUTO_TEMPLATE's newline
  8620. (templateno 0)
  8621. tpl-sig-list tpl-wild-list tpl-end-pt rep)
  8622. ;; Parse "REGEXP"
  8623. ;; We reserve @"..." for future lisp expressions that evaluate
  8624. ;; once-per-AUTOINST
  8625. (when (looking-at "\\s-*\"\\([^\"]*\\)\"")
  8626. (setq tpl-regexp (match-string 1))
  8627. (goto-char (match-end 0)))
  8628. (search-forward "(")
  8629. ;; Parse lines in the template
  8630. (when (or verilog-auto-inst-template-numbers
  8631. verilog-auto-template-warn-unused)
  8632. (save-excursion
  8633. (let ((pre-pt (point)))
  8634. (goto-char (point-min))
  8635. (while (search-forward "AUTO_TEMPLATE" pre-pt t)
  8636. (setq templateno (1+ templateno)))
  8637. (while (< (point) pre-pt)
  8638. (forward-line 1)
  8639. (setq lineno (1+ lineno))))))
  8640. (setq tpl-end-pt (save-excursion
  8641. (backward-char 1)
  8642. (verilog-forward-sexp-cmt 1) ; Moves to paren that closes argdecl's
  8643. (backward-char 1)
  8644. (point)))
  8645. ;;
  8646. (while (< (point) tpl-end-pt)
  8647. (cond ((looking-at "\\s-*\\.\\([a-zA-Z0-9`_$]+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
  8648. (setq tpl-sig-list
  8649. (cons (list
  8650. (match-string-no-properties 1)
  8651. (match-string-no-properties 2)
  8652. templateno lineno)
  8653. tpl-sig-list))
  8654. (goto-char (match-end 0)))
  8655. ;; Regexp form??
  8656. ((looking-at
  8657. ;; Regexp bug in XEmacs disallows ][ inside [], and wants + last
  8658. "\\s-*\\.\\(\\([a-zA-Z0-9`_$+@^.*?|---]+\\|[][]\\|\\\\[()|]\\)+\\)\\s-*(\\(.*\\))\\s-*\\(,\\|)\\s-*;\\)")
  8659. (setq rep (match-string-no-properties 3))
  8660. (goto-char (match-end 0))
  8661. (setq tpl-wild-list
  8662. (cons (list
  8663. (concat "^"
  8664. (verilog-string-replace-matches "@" "\\\\([0-9]+\\\\)" nil nil
  8665. (match-string 1))
  8666. "$")
  8667. rep
  8668. templateno lineno)
  8669. tpl-wild-list)))
  8670. ((looking-at "[ \t\f]+")
  8671. (goto-char (match-end 0)))
  8672. ((looking-at "\n")
  8673. (setq lineno (1+ lineno))
  8674. (goto-char (match-end 0)))
  8675. ((looking-at "//")
  8676. (search-forward "\n")
  8677. (setq lineno (1+ lineno)))
  8678. ((looking-at "/\\*")
  8679. (forward-char 2)
  8680. (or (search-forward "*/")
  8681. (error "%s: Unmatched /* */, at char %d" (verilog-point-text) (point))))
  8682. (t
  8683. (error "%s: AUTO_TEMPLATE parsing error: %s"
  8684. (verilog-point-text)
  8685. (progn (looking-at ".*$") (match-string 0))))))
  8686. ;; Return
  8687. (vector tpl-regexp
  8688. (list tpl-sig-list tpl-wild-list)))))
  8689. (defun verilog-read-auto-template (module)
  8690. "Look for an auto_template for the instantiation of the given MODULE.
  8691. If found returns `verilog-read-auto-template-inside' structure."
  8692. (save-excursion
  8693. ;; Find beginning
  8694. (let ((pt (point)))
  8695. ;; Note this search is expensive, as we hunt from mod-begin to point
  8696. ;; for every instantiation. Likewise in verilog-read-auto-lisp.
  8697. ;; So, we look first for an exact string rather than a slow regexp.
  8698. ;; Someday we may keep a cache of every template, but this would also
  8699. ;; need to record the relative position of each AUTOINST, as multiple
  8700. ;; templates exist for each module, and we're inserting lines.
  8701. (cond ((or
  8702. ;; See also regexp in `verilog-auto-template-lint'
  8703. (verilog-re-search-backward-substr
  8704. "AUTO_TEMPLATE"
  8705. (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)
  8706. ;; Also try forward of this AUTOINST
  8707. ;; This is for historical support; this isn't speced as working
  8708. (progn
  8709. (goto-char pt)
  8710. (verilog-re-search-forward-substr
  8711. "AUTO_TEMPLATE"
  8712. (concat "^\\s-*/?\\*?\\s-*" module "\\s-+AUTO_TEMPLATE") nil t)))
  8713. (goto-char (match-end 0))
  8714. (verilog-read-auto-template-middle))
  8715. ;; If no template found
  8716. (t (vector "" nil))))))
  8717. ;;(progn (find-file "auto-template.v") (verilog-read-auto-template "ptl_entry"))
  8718. (defvar verilog-auto-template-hits nil "Successful lookups with `verilog-read-auto-template-hit'.")
  8719. (make-variable-buffer-local 'verilog-auto-template-hits)
  8720. (defun verilog-read-auto-template-init ()
  8721. "Initialize `verilog-read-auto-template'."
  8722. (when (eval-when-compile (fboundp 'make-hash-table)) ; else feature not allowed
  8723. (when verilog-auto-template-warn-unused
  8724. (setq verilog-auto-template-hits
  8725. (make-hash-table :test 'equal :rehash-size 4.0)))))
  8726. (defun verilog-read-auto-template-hit (tpl-ass)
  8727. "Record that TPL-ASS template from `verilog-read-auto-template' was used."
  8728. (when (eval-when-compile (fboundp 'make-hash-table)) ; else feature not allowed
  8729. (when verilog-auto-template-warn-unused
  8730. (unless verilog-auto-template-hits
  8731. (verilog-read-auto-template-init))
  8732. (puthash (vector (nth 2 tpl-ass) (nth 3 tpl-ass)) t
  8733. verilog-auto-template-hits))))
  8734. (defun verilog-set-define (defname defvalue &optional buffer enumname)
  8735. "Set the definition DEFNAME to the DEFVALUE in the given BUFFER.
  8736. Optionally associate it with the specified enumeration ENUMNAME."
  8737. (with-current-buffer (or buffer (current-buffer))
  8738. ;; Namespace intentionally short for AUTOs and compatibility
  8739. (let ((mac (intern (concat "vh-" defname))))
  8740. ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
  8741. ;; Need to define to a constant if no value given
  8742. (set (make-local-variable mac)
  8743. (if (equal defvalue "") "1" defvalue)))
  8744. (if enumname
  8745. ;; Namespace intentionally short for AUTOs and compatibility
  8746. (let ((enumvar (intern (concat "venum-" enumname))))
  8747. ;;(message "Define %s=%s" defname defvalue) (sleep-for 1)
  8748. (unless (boundp enumvar) (set enumvar nil))
  8749. (add-to-list (make-local-variable enumvar) defname)))))
  8750. (defun verilog-read-defines (&optional filename recurse subcall)
  8751. "Read \\=`defines and parameters for the current file, or optional FILENAME.
  8752. If the filename is provided, `verilog-library-flags' will be used to
  8753. resolve it. If optional RECURSE is non-nil, recurse through \\=`includes.
  8754. Localparams must be simple assignments to constants, or have their own
  8755. \"localparam\" label rather than a list of localparams. Thus:
  8756. localparam X = 5, Y = 10; // Ok
  8757. localparam X = {1\\='b1, 2\\='h2}; // Ok
  8758. localparam X = {1\\='b1, 2\\='h2}, Y = 10; // Bad, make into 2 localparam lines
  8759. Defines must be simple text substitutions, one on a line, starting
  8760. at the beginning of the line. Any ifdefs or multiline comments around the
  8761. define are ignored.
  8762. Defines are stored inside Emacs variables using the name
  8763. vh-{definename}.
  8764. Localparams define what symbols are constants so that AUTOSENSE
  8765. will not include them in sensitivity lists. However any
  8766. parameters in the include file are not considered ports in the
  8767. including file, thus will not appear in AUTOINSTPARAM lists for a
  8768. parent module..
  8769. The file variables feature can be used to set defines that
  8770. `verilog-mode' can see; put at the *END* of your file something
  8771. like:
  8772. // Local Variables:
  8773. // vh-macro:\"macro_definition\"
  8774. // End:
  8775. If macros are defined earlier in the same file and you want their values,
  8776. you can read them automatically with:
  8777. // Local Variables:
  8778. // verilog-auto-read-includes:t
  8779. // End:
  8780. Or a more specific alternative example, which requires having
  8781. `enable-local-eval' non-nil:
  8782. // Local Variables:
  8783. // eval:(verilog-read-defines)
  8784. // eval:(verilog-read-defines \"group_standard_includes.v\")
  8785. // End:
  8786. Note these are only read when the file is first visited, you must use
  8787. \\[find-alternate-file] RET to have these take effect after editing them!
  8788. If you want to disable the \"Process `eval' or hook local variables\"
  8789. warning message, you need to add to your init file:
  8790. (setq enable-local-eval t)"
  8791. (let ((origbuf (current-buffer)))
  8792. (save-excursion
  8793. (unless subcall (verilog-getopt-flags))
  8794. (when filename
  8795. (let ((fns (verilog-library-filenames filename (buffer-file-name))))
  8796. (if fns
  8797. (set-buffer (find-file-noselect (car fns)))
  8798. (error "%s: Can't find verilog-read-defines file: %s"
  8799. (verilog-point-text) filename))))
  8800. (when recurse
  8801. (goto-char (point-min))
  8802. (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
  8803. (let ((inc (verilog-string-replace-matches
  8804. "\"" "" nil nil (match-string-no-properties 1))))
  8805. (unless (verilog-inside-comment-or-string-p)
  8806. (verilog-read-defines inc recurse t)))))
  8807. ;; Read `defines
  8808. ;; note we don't use verilog-re... it's faster this way, and that
  8809. ;; function has problems when comments are at the end of the define
  8810. (goto-char (point-min))
  8811. (while (re-search-forward "^\\s-*`define\\s-+\\([a-zA-Z0-9_$]+\\)\\s-+\\(.*\\)$" nil t)
  8812. (let ((defname (match-string-no-properties 1))
  8813. (defvalue (match-string-no-properties 2)))
  8814. (unless (verilog-inside-comment-or-string-p (match-beginning 0))
  8815. (setq defvalue (verilog-string-replace-matches "\\s-*/[/*].*$" "" nil nil defvalue))
  8816. (verilog-set-define defname defvalue origbuf))))
  8817. ;; Hack: Read parameters
  8818. (goto-char (point-min))
  8819. (while (re-search-forward
  8820. "^\\s-*\\(parameter\\|localparam\\)\\(\\s-*\\[[^]]*\\]\\)?\\s-*" nil t)
  8821. (let (enumname)
  8822. ;; The primary way of getting defines is verilog-read-decls
  8823. ;; However, that isn't called yet for included files, so we'll add another scheme
  8824. (if (looking-at "[^\n]*\\(auto\\|synopsys\\)\\s +enum\\s +\\([a-zA-Z0-9_]+\\)")
  8825. (setq enumname (match-string-no-properties 2)))
  8826. (forward-comment 99999)
  8827. (while (looking-at (concat "\\s-*,?\\s-*\\(?:/[/*].*?$\\)?\\s-*\\([a-zA-Z0-9_$]+\\)"
  8828. "\\s-*=\\s-*\\([^;,]*\\),?\\s-*\\(/[/*].*?$\\)?\\s-*"))
  8829. (unless (verilog-inside-comment-or-string-p (match-beginning 0))
  8830. (verilog-set-define (match-string-no-properties 1)
  8831. (match-string-no-properties 2) origbuf enumname))
  8832. (goto-char (match-end 0))
  8833. (forward-comment 99999)))))))
  8834. (defun verilog-read-includes ()
  8835. "Read \\=`includes for the current file.
  8836. This will find all of the \\=`includes which are at the beginning of lines,
  8837. ignoring any ifdefs or multiline comments around them.
  8838. `verilog-read-defines' is then performed on the current and each included
  8839. file.
  8840. It is often useful put at the *END* of your file something like:
  8841. // Local Variables:
  8842. // verilog-auto-read-includes:t
  8843. // End:
  8844. Or the equivalent longer version, which requires having
  8845. `enable-local-eval' non-nil:
  8846. // Local Variables:
  8847. // eval:(verilog-read-defines)
  8848. // eval:(verilog-read-includes)
  8849. // End:
  8850. Note includes are only read when the file is first visited, you must use
  8851. \\[find-alternate-file] RET to have these take effect after editing them!
  8852. It is good to get in the habit of including all needed files in each .v
  8853. file that needs it, rather than waiting for compile time. This will aid
  8854. this process, Verilint, and readability. To prevent defining the same
  8855. variable over and over when many modules are compiled together, put a test
  8856. around the inside each include file:
  8857. foo.v (an include file):
  8858. \\=`ifdef _FOO_V // include if not already included
  8859. \\=`else
  8860. \\=`define _FOO_V
  8861. ... contents of file
  8862. \\=`endif // _FOO_V"
  8863. ;;slow: (verilog-read-defines nil t)
  8864. (save-excursion
  8865. (verilog-getopt-flags)
  8866. (goto-char (point-min))
  8867. (while (re-search-forward "^\\s-*`include\\s-+\\([^ \t\n\f]+\\)" nil t)
  8868. (let ((inc (verilog-string-replace-matches "\"" "" nil nil (match-string 1))))
  8869. (verilog-read-defines inc nil t)))))
  8870. (defun verilog-read-signals (&optional start end)
  8871. "Return a simple list of all possible signals in the file.
  8872. Bounded by optional region from START to END. Overly aggressive but fast.
  8873. Some macros and such are also found and included. For dinotrace.el."
  8874. (let (sigs-all keywd)
  8875. (progn;save-excursion
  8876. (goto-char (or start (point-min)))
  8877. (setq end (or end (point-max)))
  8878. (while (re-search-forward "[\"/a-zA-Z_.%`]" end t)
  8879. (forward-char -1)
  8880. (cond
  8881. ((looking-at "//")
  8882. (search-forward "\n"))
  8883. ((looking-at "/\\*")
  8884. (search-forward "*/"))
  8885. ((looking-at "(\\*")
  8886. (or (looking-at "(\\*\\s-*)") ; It's an "always @ (*)"
  8887. (search-forward "*)")))
  8888. ((eq ?\" (following-char))
  8889. (re-search-forward "[^\\]\"")) ; don't forward-char first, since we look for a non backslash first
  8890. ((looking-at "\\s-*\\([a-zA-Z0-9$_.%`]+\\)")
  8891. (goto-char (match-end 0))
  8892. (setq keywd (match-string-no-properties 1))
  8893. (or (member keywd verilog-keywords)
  8894. (member keywd sigs-all)
  8895. (setq sigs-all (cons keywd sigs-all))))
  8896. (t (forward-char 1))))
  8897. ;; Return list
  8898. sigs-all)))
  8899. ;;
  8900. ;; Argument file parsing
  8901. ;;
  8902. (defun verilog-getopt (arglist)
  8903. "Parse -f, -v etc arguments in ARGLIST list or string."
  8904. (unless (listp arglist) (setq arglist (list arglist)))
  8905. (let ((space-args '())
  8906. arg next-param)
  8907. ;; Split on spaces, so users can pass whole command lines
  8908. (while arglist
  8909. (setq arg (car arglist)
  8910. arglist (cdr arglist))
  8911. (while (string-match "^\\([^ \t\n\f]+\\)[ \t\n\f]*\\(.*$\\)" arg)
  8912. (setq space-args (append space-args
  8913. (list (match-string-no-properties 1 arg))))
  8914. (setq arg (match-string 2 arg))))
  8915. ;; Parse arguments
  8916. (while space-args
  8917. (setq arg (car space-args)
  8918. space-args (cdr space-args))
  8919. (cond
  8920. ;; Need another arg
  8921. ((equal arg "-f")
  8922. (setq next-param arg))
  8923. ((equal arg "-v")
  8924. (setq next-param arg))
  8925. ((equal arg "-y")
  8926. (setq next-param arg))
  8927. ;; +libext+(ext1)+(ext2)...
  8928. ((string-match "^\\+libext\\+\\(.*\\)" arg)
  8929. (setq arg (match-string 1 arg))
  8930. (while (string-match "\\([^+]+\\)\\+?\\(.*\\)" arg)
  8931. (verilog-add-list-unique `verilog-library-extensions
  8932. (match-string 1 arg))
  8933. (setq arg (match-string 2 arg))))
  8934. ;;
  8935. ((or (string-match "^-D\\([^+=]*\\)[+=]\\(.*\\)" arg) ; -Ddefine=val
  8936. (string-match "^-D\\([^+=]*\\)\\(\\)" arg) ; -Ddefine
  8937. (string-match "^\\+define\\([^+=]*\\)[+=]\\(.*\\)" arg) ; +define+val
  8938. (string-match "^\\+define\\([^+=]*\\)\\(\\)" arg)) ; +define+define
  8939. (verilog-set-define (match-string 1 arg) (match-string 2 arg)))
  8940. ;;
  8941. ((or (string-match "^\\+incdir\\+\\(.*\\)" arg) ; +incdir+dir
  8942. (string-match "^-I\\(.*\\)" arg)) ; -Idir
  8943. (verilog-add-list-unique `verilog-library-directories
  8944. (match-string 1 (substitute-in-file-name arg))))
  8945. ;; Ignore
  8946. ((equal "+librescan" arg))
  8947. ((string-match "^-U\\(.*\\)" arg)) ; -Udefine
  8948. ;; Second parameters
  8949. ((equal next-param "-f")
  8950. (setq next-param nil)
  8951. (verilog-getopt-file (substitute-in-file-name arg)))
  8952. ((equal next-param "-v")
  8953. (setq next-param nil)
  8954. (verilog-add-list-unique `verilog-library-files
  8955. (substitute-in-file-name arg)))
  8956. ((equal next-param "-y")
  8957. (setq next-param nil)
  8958. (verilog-add-list-unique `verilog-library-directories
  8959. (substitute-in-file-name arg)))
  8960. ;; Filename
  8961. ((string-match "^[^-+]" arg)
  8962. (verilog-add-list-unique `verilog-library-files
  8963. (substitute-in-file-name arg)))
  8964. ;; Default - ignore; no warning
  8965. ))))
  8966. ;;(verilog-getopt (list "+libext+.a+.b" "+incdir+foodir" "+define+a+aval" "-f" "otherf" "-v" "library" "-y" "dir"))
  8967. (defun verilog-getopt-file (filename)
  8968. "Read Verilog options from the specified FILENAME."
  8969. (save-excursion
  8970. (let ((fns (verilog-library-filenames filename (buffer-file-name)))
  8971. (orig-buffer (current-buffer))
  8972. line)
  8973. (if fns
  8974. (set-buffer (find-file-noselect (car fns)))
  8975. (error "%s: Can't find verilog-getopt-file -f file: %s"
  8976. (verilog-point-text) filename))
  8977. (goto-char (point-min))
  8978. (while (not (eobp))
  8979. (setq line (buffer-substring (point) (point-at-eol)))
  8980. (forward-line 1)
  8981. (when (string-match "//" line)
  8982. (setq line (substring line 0 (match-beginning 0))))
  8983. (with-current-buffer orig-buffer ; Variables are buffer-local, so need right context.
  8984. (verilog-getopt line))))))
  8985. (defun verilog-getopt-flags ()
  8986. "Convert `verilog-library-flags' into standard library variables."
  8987. ;; If the flags are local, then all the outputs should be local also
  8988. (when (local-variable-p `verilog-library-flags (current-buffer))
  8989. (mapc 'make-local-variable '(verilog-library-extensions
  8990. verilog-library-directories
  8991. verilog-library-files
  8992. verilog-library-flags)))
  8993. ;; Allow user to customize
  8994. (verilog-run-hooks 'verilog-before-getopt-flags-hook)
  8995. ;; Process arguments
  8996. (verilog-getopt verilog-library-flags)
  8997. ;; Allow user to customize
  8998. (verilog-run-hooks 'verilog-getopt-flags-hook))
  8999. (defun verilog-add-list-unique (varref object)
  9000. "Append to VARREF list the given OBJECT,
  9001. unless it is already a member of the variable's list."
  9002. (unless (member object (symbol-value varref))
  9003. (set varref (append (symbol-value varref) (list object))))
  9004. varref)
  9005. ;;(progn (setq l '()) (verilog-add-list-unique `l "a") (verilog-add-list-unique `l "a") l)
  9006. (defun verilog-current-flags ()
  9007. "Convert `verilog-library-flags' and similar variables to command line.
  9008. Used for __FLAGS__ in `verilog-expand-command'."
  9009. (let ((cmd (mapconcat `concat verilog-library-flags " ")))
  9010. (when (equal cmd "")
  9011. (setq cmd (concat
  9012. "+libext+" (mapconcat `concat verilog-library-extensions "+")
  9013. (mapconcat (lambda (i) (concat " -y " i " +incdir+" i))
  9014. verilog-library-directories "")
  9015. (mapconcat (lambda (i) (concat " -v " i))
  9016. verilog-library-files ""))))
  9017. cmd))
  9018. ;;(verilog-current-flags)
  9019. ;;; Cached directory support:
  9020. ;;
  9021. (defvar verilog-dir-cache-preserving nil
  9022. "If true, the directory cache is enabled, and file system changes are ignored.
  9023. See `verilog-dir-exists-p' and `verilog-dir-files'.")
  9024. ;; If adding new cached variable, add also to verilog-preserve-dir-cache
  9025. (defvar verilog-dir-cache-list nil
  9026. "Alist of (((Cwd Dirname) Results)...) for caching `verilog-dir-files'.")
  9027. (defvar verilog-dir-cache-lib-filenames nil
  9028. "Cached data for `verilog-library-filenames'.")
  9029. (defmacro verilog-preserve-dir-cache (&rest body)
  9030. "Execute the BODY forms, allowing directory cache preservation within BODY.
  9031. This means that changes inside BODY made to the file system will not be
  9032. seen by the `verilog-dir-files' and related functions."
  9033. `(let ((verilog-dir-cache-preserving (current-buffer))
  9034. verilog-dir-cache-list
  9035. verilog-dir-cache-lib-filenames)
  9036. (progn ,@body)))
  9037. (defun verilog-dir-files (dirname)
  9038. "Return all filenames in the DIRNAME directory.
  9039. Relative paths depend on the `default-directory'.
  9040. Results are cached if inside `verilog-preserve-dir-cache'."
  9041. (unless verilog-dir-cache-preserving
  9042. (setq verilog-dir-cache-list nil)) ; Cache disabled
  9043. ;; We don't use expand-file-name on the dirname to make key, as it's slow
  9044. (let* ((cache-key (list dirname default-directory))
  9045. (fass (assoc cache-key verilog-dir-cache-list))
  9046. exp-dirname data)
  9047. (cond (fass ; Return data from cache hit
  9048. (nth 1 fass))
  9049. (t
  9050. (setq exp-dirname (expand-file-name dirname)
  9051. data (and (file-directory-p exp-dirname)
  9052. (directory-files exp-dirname nil nil nil)))
  9053. ;; Note we also encache nil for non-existing dirs.
  9054. (setq verilog-dir-cache-list (cons (list cache-key data)
  9055. verilog-dir-cache-list))
  9056. data))))
  9057. ;; Miss-and-hit test:
  9058. ;;(verilog-preserve-dir-cache (prin1 (verilog-dir-files "."))
  9059. ;; (prin1 (verilog-dir-files ".")) nil)
  9060. (defun verilog-dir-file-exists-p (filename)
  9061. "Return true if FILENAME exists.
  9062. Like `file-exists-p' but results are cached if inside
  9063. `verilog-preserve-dir-cache'."
  9064. (let* ((dirname (file-name-directory filename))
  9065. ;; Correct for file-name-nondirectory returning same if no slash.
  9066. (dirnamed (if (or (not dirname) (equal dirname filename))
  9067. default-directory dirname))
  9068. (flist (verilog-dir-files dirnamed)))
  9069. (and flist
  9070. (member (file-name-nondirectory filename) flist)
  9071. t)))
  9072. ;;(verilog-dir-file-exists-p "verilog-mode.el")
  9073. ;;(verilog-dir-file-exists-p "../verilog-mode/verilog-mode.el")
  9074. ;;; Module name lookup:
  9075. ;;
  9076. (defun verilog-module-inside-filename-p (module filename)
  9077. "Return modi if MODULE is specified inside FILENAME, else nil.
  9078. Allows version control to check out the file if need be."
  9079. (and (or (file-exists-p filename)
  9080. (and (fboundp 'vc-backend)
  9081. (vc-backend filename)))
  9082. (let (modi type)
  9083. (with-current-buffer (find-file-noselect filename)
  9084. (save-excursion
  9085. (goto-char (point-min))
  9086. (while (and
  9087. ;; It may be tempting to look for verilog-defun-re,
  9088. ;; don't, it slows things down a lot!
  9089. (verilog-re-search-forward-quick "\\<\\(module\\|interface\\|program\\)\\>" nil t)
  9090. (setq type (match-string-no-properties 0))
  9091. (verilog-re-search-forward-quick "[(;]" nil t))
  9092. (if (equal module (verilog-read-module-name))
  9093. (setq modi (verilog-modi-new module filename (point) type))))
  9094. modi)))))
  9095. (defun verilog-is-number (symbol)
  9096. "Return true if SYMBOL is number-like."
  9097. (or (string-match "^[0-9 \t:]+$" symbol)
  9098. (string-match "^[---]*[0-9]+$" symbol)
  9099. (string-match "^[0-9 \t]+'s?[hdxbo][0-9a-fA-F_xz? \t]*$" symbol)))
  9100. (defun verilog-symbol-detick (symbol wing-it)
  9101. "Return an expanded SYMBOL name without any defines.
  9102. If the variable vh-{symbol} is defined, return that value.
  9103. If undefined, and WING-IT, return just SYMBOL without the tick, else nil."
  9104. (while (and symbol (string-match "^`" symbol))
  9105. (setq symbol (substring symbol 1))
  9106. (setq symbol
  9107. ;; Namespace intentionally short for AUTOs and compatibility
  9108. (if (boundp (intern (concat "vh-" symbol)))
  9109. ;; Emacs has a bug where boundp on a buffer-local
  9110. ;; variable in only one buffer returns t in another.
  9111. ;; This can confuse, so check for nil.
  9112. ;; Namespace intentionally short for AUTOs and compatibility
  9113. (let ((val (eval (intern (concat "vh-" symbol)))))
  9114. (if (eq val nil)
  9115. (if wing-it symbol nil)
  9116. val))
  9117. (if wing-it symbol nil))))
  9118. symbol)
  9119. ;;(verilog-symbol-detick "`mod" nil)
  9120. (defun verilog-symbol-detick-denumber (symbol)
  9121. "Return SYMBOL with defines converted and any numbers dropped to nil."
  9122. (when (string-match "^`" symbol)
  9123. ;; This only will work if the define is a simple signal, not
  9124. ;; something like a[b]. Sorry, it should be substituted into the parser
  9125. (setq symbol
  9126. (verilog-string-replace-matches
  9127. "\\[[^0-9: \t]+\\]" "" nil nil
  9128. (or (verilog-symbol-detick symbol nil)
  9129. (if verilog-auto-sense-defines-constant
  9130. "0"
  9131. symbol)))))
  9132. (if (verilog-is-number symbol)
  9133. nil
  9134. symbol))
  9135. (defun verilog-symbol-detick-text (text)
  9136. "Return TEXT without any known defines.
  9137. If the variable vh-{symbol} is defined, substitute that value."
  9138. (let ((ok t) symbol val)
  9139. (while (and ok (string-match "`\\([a-zA-Z0-9_]+\\)" text))
  9140. (setq symbol (match-string 1 text))
  9141. ;;(message symbol)
  9142. (cond ((and
  9143. ;; Namespace intentionally short for AUTOs and compatibility
  9144. (boundp (intern (concat "vh-" symbol)))
  9145. ;; Emacs has a bug where boundp on a buffer-local
  9146. ;; variable in only one buffer returns t in another.
  9147. ;; This can confuse, so check for nil.
  9148. ;; Namespace intentionally short for AUTOs and compatibility
  9149. (setq val (eval (intern (concat "vh-" symbol)))))
  9150. (setq text (replace-match val nil nil text)))
  9151. (t (setq ok nil)))))
  9152. text)
  9153. ;;(progn (setq vh-mod "`foo" vh-foo "bar") (verilog-symbol-detick-text "bar `mod `undefed"))
  9154. (defun verilog-expand-dirnames (&optional dirnames)
  9155. "Return a list of existing directories given a list of wildcarded DIRNAMES.
  9156. Or, just the existing dirnames themselves if there are no wildcards."
  9157. ;; Note this function is performance critical.
  9158. ;; Do not call anything that requires disk access that cannot be cached.
  9159. (interactive)
  9160. (unless dirnames
  9161. (error "`verilog-library-directories' should include at least `.'"))
  9162. (setq dirnames (reverse dirnames)) ; not nreverse
  9163. (let ((dirlist nil)
  9164. pattern dirfile dirfiles dirname root filename rest basefile)
  9165. (while dirnames
  9166. (setq dirname (substitute-in-file-name (car dirnames))
  9167. dirnames (cdr dirnames))
  9168. (cond ((string-match (concat "^\\(\\|[/\\]*[^*?]*[/\\]\\)" ; root
  9169. "\\([^/\\]*[*?][^/\\]*\\)" ; filename with *?
  9170. "\\(.*\\)") ; rest
  9171. dirname)
  9172. (setq root (match-string 1 dirname)
  9173. filename (match-string 2 dirname)
  9174. rest (match-string 3 dirname)
  9175. pattern filename)
  9176. ;; now replace those * and ? with .+ and .
  9177. ;; use ^ and /> to get only whole file names
  9178. (setq pattern (verilog-string-replace-matches "[*]" ".+" nil nil pattern)
  9179. pattern (verilog-string-replace-matches "[?]" "." nil nil pattern)
  9180. pattern (concat "^" pattern "$")
  9181. dirfiles (verilog-dir-files root))
  9182. (while dirfiles
  9183. (setq basefile (car dirfiles)
  9184. dirfile (expand-file-name (concat root basefile rest))
  9185. dirfiles (cdr dirfiles))
  9186. (if (and (string-match pattern basefile)
  9187. ;; Don't allow abc/*/rtl to match abc/rtl via ..
  9188. (not (equal basefile "."))
  9189. (not (equal basefile ".."))
  9190. (file-directory-p dirfile))
  9191. (setq dirlist (cons dirfile dirlist)))))
  9192. ;; Defaults
  9193. (t
  9194. (if (file-directory-p dirname)
  9195. (setq dirlist (cons dirname dirlist))))))
  9196. dirlist))
  9197. ;;(verilog-expand-dirnames (list "." ".." "nonexist" "../*" "/home/wsnyder/*/v"))
  9198. (defun verilog-library-filenames (filename &optional current check-ext)
  9199. "Return a search path to find the given FILENAME or module name.
  9200. Uses the optional CURRENT filename or variable `buffer-file-name', plus
  9201. `verilog-library-directories' and `verilog-library-extensions'
  9202. variables to build the path. With optional CHECK-EXT also check
  9203. `verilog-library-extensions'."
  9204. (unless current (setq current (buffer-file-name)))
  9205. (unless verilog-dir-cache-preserving
  9206. (setq verilog-dir-cache-lib-filenames nil))
  9207. (let* ((cache-key (list filename current check-ext))
  9208. (fass (assoc cache-key verilog-dir-cache-lib-filenames))
  9209. chkdirs chkdir chkexts fn outlist)
  9210. (cond (fass ; Return data from cache hit
  9211. (nth 1 fass))
  9212. (t
  9213. ;; Note this expand can't be easily cached, as we need to
  9214. ;; pick up buffer-local variables for newly read sub-module files
  9215. (setq chkdirs (verilog-expand-dirnames verilog-library-directories))
  9216. (while chkdirs
  9217. (setq chkdir (expand-file-name (car chkdirs)
  9218. (file-name-directory current))
  9219. chkexts (if check-ext verilog-library-extensions `("")))
  9220. (while chkexts
  9221. (setq fn (expand-file-name (concat filename (car chkexts))
  9222. chkdir))
  9223. ;;(message "Check for %s" fn)
  9224. (if (verilog-dir-file-exists-p fn)
  9225. (setq outlist (cons (expand-file-name
  9226. fn (file-name-directory current))
  9227. outlist)))
  9228. (setq chkexts (cdr chkexts)))
  9229. (setq chkdirs (cdr chkdirs)))
  9230. (setq outlist (nreverse outlist))
  9231. (setq verilog-dir-cache-lib-filenames
  9232. (cons (list cache-key outlist)
  9233. verilog-dir-cache-lib-filenames))
  9234. outlist))))
  9235. (defun verilog-module-filenames (module current)
  9236. "Return a search path to find the given MODULE name.
  9237. Uses the CURRENT filename, `verilog-library-extensions',
  9238. `verilog-library-directories' and `verilog-library-files'
  9239. variables to build the path."
  9240. ;; Return search locations for it
  9241. (append (list current) ; first, current buffer
  9242. (verilog-library-filenames module current t)
  9243. ;; Finally any libraries; fixed up if using e.g. tramp
  9244. (mapcar (lambda (fname)
  9245. (if (file-name-absolute-p fname)
  9246. (concat (file-remote-p current) fname)
  9247. fname))
  9248. verilog-library-files)))
  9249. ;;
  9250. ;; Module Information
  9251. ;;
  9252. ;; Many of these functions work on "modi" a module information structure
  9253. ;; A modi is: [module-name-string file-name begin-point]
  9254. (defvar verilog-cache-enabled t
  9255. "Non-nil enables caching of signals, etc. Set to nil for debugging to make things SLOW!")
  9256. (defvar verilog-modi-cache-list nil
  9257. "Cache of ((Module Function) Buf-Tick Buf-Modtime Func-Returns)...
  9258. For speeding up verilog-modi-get-* commands.
  9259. Buffer-local.")
  9260. (make-variable-buffer-local 'verilog-modi-cache-list)
  9261. (defvar verilog-modi-cache-preserve-tick nil
  9262. "Modification tick after which the cache is still considered valid.
  9263. Use `verilog-preserve-modi-cache' to set it.")
  9264. (defvar verilog-modi-cache-preserve-buffer nil
  9265. "Modification tick after which the cache is still considered valid.
  9266. Use `verilog-preserve-modi-cache' to set it.")
  9267. (defvar verilog-modi-cache-current-enable nil
  9268. "Non-nil means allow caching `verilog-modi-current', set by let().")
  9269. (defvar verilog-modi-cache-current nil
  9270. "Currently active `verilog-modi-current', if any, set by let().")
  9271. (defvar verilog-modi-cache-current-max nil
  9272. "Current endmodule point for `verilog-modi-cache-current', if any.")
  9273. (defun verilog-modi-current ()
  9274. "Return the modi structure for the module currently at point, possibly cached."
  9275. (cond ((and verilog-modi-cache-current
  9276. (>= (point) (verilog-modi-get-point verilog-modi-cache-current))
  9277. (<= (point) verilog-modi-cache-current-max))
  9278. ;; Slow assertion, for debugging the cache:
  9279. ;;(or (equal verilog-modi-cache-current (verilog-modi-current-get)) (debug))
  9280. verilog-modi-cache-current)
  9281. (verilog-modi-cache-current-enable
  9282. (setq verilog-modi-cache-current (verilog-modi-current-get)
  9283. verilog-modi-cache-current-max
  9284. ;; The cache expires when we pass "endmodule" as then the
  9285. ;; current modi may change to the next module
  9286. ;; This relies on the AUTOs generally inserting, not deleting text
  9287. (save-excursion
  9288. (verilog-re-search-forward-quick verilog-end-defun-re nil nil)))
  9289. verilog-modi-cache-current)
  9290. (t
  9291. (verilog-modi-current-get))))
  9292. (defun verilog-modi-current-get ()
  9293. "Return the modi structure for the module currently at point."
  9294. (let* (name type pt)
  9295. ;; read current module's name
  9296. (save-excursion
  9297. (verilog-re-search-backward-quick verilog-defun-re nil nil)
  9298. (setq type (match-string-no-properties 0))
  9299. (verilog-re-search-forward-quick "(" nil nil)
  9300. (setq name (verilog-read-module-name))
  9301. (setq pt (point)))
  9302. ;; return modi - note this vector built two places
  9303. (verilog-modi-new name (or (buffer-file-name) (current-buffer)) pt type)))
  9304. (defvar verilog-modi-lookup-cache nil "Hash of (modulename modi).")
  9305. (make-variable-buffer-local 'verilog-modi-lookup-cache)
  9306. (defvar verilog-modi-lookup-last-current nil "Cache of `current-buffer' at last lookup.")
  9307. (defvar verilog-modi-lookup-last-tick nil "Cache of `buffer-chars-modified-tick' at last lookup.")
  9308. (defun verilog-modi-lookup (module allow-cache &optional ignore-error)
  9309. "Find the file and point at which MODULE is defined.
  9310. If ALLOW-CACHE is set, check and remember cache of previous lookups.
  9311. Return modi if successful, else print message unless IGNORE-ERROR is true."
  9312. (let* ((current (or (buffer-file-name) (current-buffer)))
  9313. modi)
  9314. ;; Check cache
  9315. ;;(message "verilog-modi-lookup: %s" module)
  9316. (cond ((and verilog-modi-lookup-cache
  9317. verilog-cache-enabled
  9318. allow-cache
  9319. (setq modi (gethash module verilog-modi-lookup-cache))
  9320. (equal verilog-modi-lookup-last-current current)
  9321. ;; If hit is in current buffer, then tick must match
  9322. (or (equal verilog-modi-lookup-last-tick (buffer-chars-modified-tick))
  9323. (not (equal current (verilog-modi-file-or-buffer modi)))))
  9324. ;;(message "verilog-modi-lookup: HIT %S" modi)
  9325. modi)
  9326. ;; Miss
  9327. (t (let* ((realname (verilog-symbol-detick module t))
  9328. (orig-filenames (verilog-module-filenames realname current))
  9329. (filenames orig-filenames)
  9330. mif)
  9331. (while (and filenames (not mif))
  9332. (if (not (setq mif (verilog-module-inside-filename-p realname (car filenames))))
  9333. (setq filenames (cdr filenames))))
  9334. ;; mif has correct form to become later elements of modi
  9335. (setq modi mif)
  9336. (or mif ignore-error
  9337. (error
  9338. (concat
  9339. "%s: Can't locate `%s' module definition%s"
  9340. "\n Check the verilog-library-directories variable."
  9341. "\n I looked in (if not listed, doesn't exist):\n\t%s")
  9342. (verilog-point-text) module
  9343. (if (not (equal module realname))
  9344. (concat " (Expanded macro to " realname ")")
  9345. "")
  9346. (mapconcat 'concat orig-filenames "\n\t")))
  9347. (when (eval-when-compile (fboundp 'make-hash-table))
  9348. (unless verilog-modi-lookup-cache
  9349. (setq verilog-modi-lookup-cache
  9350. (make-hash-table :test 'equal :rehash-size 4.0)))
  9351. (puthash module modi verilog-modi-lookup-cache))
  9352. (setq verilog-modi-lookup-last-current current
  9353. verilog-modi-lookup-last-tick (buffer-chars-modified-tick)))))
  9354. modi))
  9355. (defun verilog-modi-filename (modi)
  9356. "Filename of MODI, or name of buffer if it's never been saved."
  9357. (if (bufferp (verilog-modi-file-or-buffer modi))
  9358. (or (buffer-file-name (verilog-modi-file-or-buffer modi))
  9359. (buffer-name (verilog-modi-file-or-buffer modi)))
  9360. (verilog-modi-file-or-buffer modi)))
  9361. (defun verilog-modi-goto (modi)
  9362. "Move point/buffer to specified MODI."
  9363. (or modi (error "Passed unfound modi to goto, check earlier"))
  9364. (set-buffer (if (bufferp (verilog-modi-file-or-buffer modi))
  9365. (verilog-modi-file-or-buffer modi)
  9366. (find-file-noselect (verilog-modi-file-or-buffer modi))))
  9367. (or (equal major-mode `verilog-mode) ; Put into Verilog mode to get syntax
  9368. (verilog-mode))
  9369. (goto-char (verilog-modi-get-point modi)))
  9370. (defun verilog-goto-defun-file (module)
  9371. "Move point to the file at which a given MODULE is defined."
  9372. (interactive "sGoto File for Module: ")
  9373. (let* ((modi (verilog-modi-lookup module nil)))
  9374. (when modi
  9375. (verilog-modi-goto modi)
  9376. (switch-to-buffer (current-buffer)))))
  9377. (defun verilog-modi-cache-results (modi function)
  9378. "Run on MODI the given FUNCTION. Locate the module in a file.
  9379. Cache the output of function so next call may have faster access."
  9380. (let (fass)
  9381. (save-excursion ; Cache is buffer-local so can't avoid this.
  9382. (verilog-modi-goto modi)
  9383. (if (and (setq fass (assoc (list modi function)
  9384. verilog-modi-cache-list))
  9385. ;; Destroy caching when incorrect; Modified or file changed
  9386. (not (and verilog-cache-enabled
  9387. (or (equal (buffer-chars-modified-tick) (nth 1 fass))
  9388. (and verilog-modi-cache-preserve-tick
  9389. (<= verilog-modi-cache-preserve-tick (nth 1 fass))
  9390. (equal verilog-modi-cache-preserve-buffer (current-buffer))))
  9391. (equal (visited-file-modtime) (nth 2 fass)))))
  9392. (setq verilog-modi-cache-list nil
  9393. fass nil))
  9394. (cond (fass
  9395. ;; Return data from cache hit
  9396. (nth 3 fass))
  9397. (t
  9398. ;; Read from file
  9399. ;; Clear then restore any highlighting to make emacs19 happy
  9400. (let ((func-returns
  9401. (verilog-save-font-no-change-functions
  9402. (funcall function))))
  9403. ;; Cache for next time
  9404. (setq verilog-modi-cache-list
  9405. (cons (list (list modi function)
  9406. (buffer-chars-modified-tick)
  9407. (visited-file-modtime)
  9408. func-returns)
  9409. verilog-modi-cache-list))
  9410. func-returns))))))
  9411. (defun verilog-modi-cache-add (modi function element sig-list)
  9412. "Add function return results to the module cache.
  9413. Update MODI's cache for given FUNCTION so that the return ELEMENT of that
  9414. function now contains the additional SIG-LIST parameters."
  9415. (let (fass)
  9416. (save-excursion
  9417. (verilog-modi-goto modi)
  9418. (if (setq fass (assoc (list modi function)
  9419. verilog-modi-cache-list))
  9420. (let ((func-returns (nth 3 fass)))
  9421. (aset func-returns element
  9422. (append sig-list (aref func-returns element))))))))
  9423. (defmacro verilog-preserve-modi-cache (&rest body)
  9424. "Execute the BODY forms, allowing cache preservation within BODY.
  9425. This means that changes to the buffer will not result in the cache being
  9426. flushed. If the changes affect the modsig state, they must call the
  9427. modsig-cache-add-* function, else the results of later calls may be
  9428. incorrect. Without this, changes are assumed to be adding/removing signals
  9429. and invalidating the cache."
  9430. `(let ((verilog-modi-cache-preserve-tick (buffer-chars-modified-tick))
  9431. (verilog-modi-cache-preserve-buffer (current-buffer)))
  9432. (progn ,@body)))
  9433. (defun verilog-modi-modport-lookup-one (modi name &optional ignore-error)
  9434. "Given a MODI, return the declarations related to the given modport NAME.
  9435. Report errors unless optional IGNORE-ERROR."
  9436. ;; Recursive routine - see below
  9437. (let* ((realname (verilog-symbol-detick name t))
  9438. (modport (assoc name (verilog-decls-get-modports (verilog-modi-get-decls modi)))))
  9439. (or modport ignore-error
  9440. (error "%s: Can't locate `%s' modport definition%s"
  9441. (verilog-point-text) name
  9442. (if (not (equal name realname))
  9443. (concat " (Expanded macro to " realname ")")
  9444. "")))
  9445. (let* ((decls (verilog-modport-decls modport))
  9446. (clks (verilog-modport-clockings modport)))
  9447. ;; Now expand any clocking's
  9448. (while clks
  9449. (setq decls (verilog-decls-append
  9450. decls
  9451. (verilog-modi-modport-lookup-one modi (car clks) ignore-error)))
  9452. (setq clks (cdr clks)))
  9453. decls)))
  9454. (defun verilog-modi-modport-lookup (modi name-re &optional ignore-error)
  9455. "Given a MODI, return the declarations related to the given modport NAME-RE.
  9456. If the modport points to any clocking blocks, expand the signals to include
  9457. those clocking block's signals."
  9458. ;; Recursive routine - see below
  9459. (let* ((mod-decls (verilog-modi-get-decls modi))
  9460. (clks (verilog-decls-get-modports mod-decls))
  9461. (name-re (concat "^" name-re "$"))
  9462. (decls (verilog-decls-new nil nil nil nil nil nil nil nil nil)))
  9463. ;; Pull in all modports
  9464. (while clks
  9465. (when (string-match name-re (verilog-modport-name (car clks)))
  9466. (setq decls (verilog-decls-append
  9467. decls
  9468. (verilog-modi-modport-lookup-one modi (verilog-modport-name (car clks)) ignore-error))))
  9469. (setq clks (cdr clks)))
  9470. decls))
  9471. (defun verilog-signals-matching-enum (in-list enum)
  9472. "Return all signals in IN-LIST matching the given ENUM."
  9473. (let (out-list)
  9474. (while in-list
  9475. (if (equal (verilog-sig-enum (car in-list)) enum)
  9476. (setq out-list (cons (car in-list) out-list)))
  9477. (setq in-list (cdr in-list)))
  9478. ;; New scheme
  9479. ;; Namespace intentionally short for AUTOs and compatibility
  9480. (let* ((enumvar (intern (concat "venum-" enum)))
  9481. (enumlist (and (boundp enumvar) (eval enumvar))))
  9482. (while enumlist
  9483. (add-to-list 'out-list (list (car enumlist)))
  9484. (setq enumlist (cdr enumlist))))
  9485. (nreverse out-list)))
  9486. (defun verilog-signals-matching-regexp (in-list regexp)
  9487. "Return all signals in IN-LIST matching the given REGEXP, if non-nil."
  9488. (if (or (not regexp) (equal regexp ""))
  9489. in-list
  9490. (let ((case-fold-search verilog-case-fold)
  9491. out-list)
  9492. (while in-list
  9493. (if (string-match regexp (verilog-sig-name (car in-list)))
  9494. (setq out-list (cons (car in-list) out-list)))
  9495. (setq in-list (cdr in-list)))
  9496. (nreverse out-list))))
  9497. (defun verilog-signals-not-matching-regexp (in-list regexp)
  9498. "Return all signals in IN-LIST not matching the given REGEXP, if non-nil."
  9499. (if (or (not regexp) (equal regexp ""))
  9500. in-list
  9501. (let ((case-fold-search verilog-case-fold)
  9502. out-list)
  9503. (while in-list
  9504. (if (not (string-match regexp (verilog-sig-name (car in-list))))
  9505. (setq out-list (cons (car in-list) out-list)))
  9506. (setq in-list (cdr in-list)))
  9507. (nreverse out-list))))
  9508. (defun verilog-signals-matching-dir-re (in-list decl-type regexp)
  9509. "Return all signals in IN-LIST matching the given DECL-TYPE and REGEXP,
  9510. if non-nil."
  9511. (if (or (not regexp) (equal regexp ""))
  9512. in-list
  9513. (let (out-list to-match)
  9514. (while in-list
  9515. ;; Note verilog-insert-one-definition matches on this order
  9516. (setq to-match (concat
  9517. decl-type
  9518. " " (verilog-sig-signed (car in-list))
  9519. " " (verilog-sig-multidim (car in-list))
  9520. (verilog-sig-bits (car in-list))))
  9521. (if (string-match regexp to-match)
  9522. (setq out-list (cons (car in-list) out-list)))
  9523. (setq in-list (cdr in-list)))
  9524. (nreverse out-list))))
  9525. (defun verilog-signals-edit-wire-reg (in-list)
  9526. "Return all signals in IN-LIST with wire/reg data types made blank."
  9527. (mapcar (lambda (sig)
  9528. (when (member (verilog-sig-type sig) '("wire" "reg"))
  9529. (verilog-sig-type-set sig nil))
  9530. sig) in-list))
  9531. ;; Combined
  9532. (defun verilog-decls-get-signals (decls)
  9533. "Return all declared signals in DECLS, excluding `assign' statements."
  9534. (append
  9535. (verilog-decls-get-outputs decls)
  9536. (verilog-decls-get-inouts decls)
  9537. (verilog-decls-get-inputs decls)
  9538. (verilog-decls-get-vars decls)
  9539. (verilog-decls-get-consts decls)
  9540. (verilog-decls-get-gparams decls)))
  9541. (defun verilog-decls-get-ports (decls)
  9542. (append
  9543. (verilog-decls-get-outputs decls)
  9544. (verilog-decls-get-inouts decls)
  9545. (verilog-decls-get-inputs decls)))
  9546. (defun verilog-decls-get-iovars (decls)
  9547. (append
  9548. (verilog-decls-get-vars decls)
  9549. (verilog-decls-get-outputs decls)
  9550. (verilog-decls-get-inouts decls)
  9551. (verilog-decls-get-inputs decls)))
  9552. (defsubst verilog-modi-cache-add-outputs (modi sig-list)
  9553. (verilog-modi-cache-add modi 'verilog-read-decls 0 sig-list))
  9554. (defsubst verilog-modi-cache-add-inouts (modi sig-list)
  9555. (verilog-modi-cache-add modi 'verilog-read-decls 1 sig-list))
  9556. (defsubst verilog-modi-cache-add-inputs (modi sig-list)
  9557. (verilog-modi-cache-add modi 'verilog-read-decls 2 sig-list))
  9558. (defsubst verilog-modi-cache-add-vars (modi sig-list)
  9559. (verilog-modi-cache-add modi 'verilog-read-decls 3 sig-list))
  9560. (defsubst verilog-modi-cache-add-gparams (modi sig-list)
  9561. (verilog-modi-cache-add modi 'verilog-read-decls 7 sig-list))
  9562. ;;; Auto creation utilities:
  9563. ;;
  9564. (defun verilog-auto-re-search-do (search-for func)
  9565. "Search for the given auto text regexp SEARCH-FOR, and perform FUNC where it occurs."
  9566. (goto-char (point-min))
  9567. (while (verilog-re-search-forward-quick search-for nil t)
  9568. (funcall func)))
  9569. (defun verilog-insert-one-definition (sig type indent-pt)
  9570. "Print out a definition for SIG of the given TYPE,
  9571. with appropriate INDENT-PT indentation."
  9572. (indent-to indent-pt)
  9573. ;; Note verilog-signals-matching-dir-re matches on this order
  9574. (insert type)
  9575. (when (verilog-sig-modport sig)
  9576. (insert "." (verilog-sig-modport sig)))
  9577. (when (verilog-sig-signed sig)
  9578. (insert " " (verilog-sig-signed sig)))
  9579. (when (verilog-sig-multidim sig)
  9580. (insert " " (verilog-sig-multidim-string sig)))
  9581. (when (verilog-sig-bits sig)
  9582. (insert " " (verilog-sig-bits sig)))
  9583. (indent-to (max 24 (+ indent-pt 16)))
  9584. (unless (= (char-syntax (preceding-char)) ?\ )
  9585. (insert " ")) ; Need space between "]name" if indent-to did nothing
  9586. (insert (verilog-sig-name sig))
  9587. (when (verilog-sig-memory sig)
  9588. (insert " " (verilog-sig-memory sig))))
  9589. (defun verilog-insert-definition (modi sigs direction indent-pt v2k &optional dont-sort)
  9590. "Print out a definition for MODI's list of SIGS of the given DIRECTION,
  9591. with appropriate INDENT-PT indentation. If V2K, use Verilog 2001 I/O
  9592. format. Sort unless DONT-SORT. DIRECTION is normally wire/reg/output.
  9593. When MODI is non-null, also add to modi-cache, for tracking."
  9594. (when modi
  9595. (cond ((equal direction "wire")
  9596. (verilog-modi-cache-add-vars modi sigs))
  9597. ((equal direction "reg")
  9598. (verilog-modi-cache-add-vars modi sigs))
  9599. ((equal direction "output")
  9600. (verilog-modi-cache-add-outputs modi sigs)
  9601. (when verilog-auto-declare-nettype
  9602. (verilog-modi-cache-add-vars modi sigs)))
  9603. ((equal direction "input")
  9604. (verilog-modi-cache-add-inputs modi sigs)
  9605. (when verilog-auto-declare-nettype
  9606. (verilog-modi-cache-add-vars modi sigs)))
  9607. ((equal direction "inout")
  9608. (verilog-modi-cache-add-inouts modi sigs)
  9609. (when verilog-auto-declare-nettype
  9610. (verilog-modi-cache-add-vars modi sigs)))
  9611. ((equal direction "interface"))
  9612. ((equal direction "parameter")
  9613. (verilog-modi-cache-add-gparams modi sigs))
  9614. (t
  9615. (error "Unsupported verilog-insert-definition direction: `%s'" direction))))
  9616. (or dont-sort
  9617. (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
  9618. (while sigs
  9619. (let ((sig (car sigs)))
  9620. (verilog-insert-one-definition
  9621. sig
  9622. ;; Want "type x" or "output type x", not "wire type x"
  9623. (cond ((or (verilog-sig-type sig)
  9624. verilog-auto-wire-type)
  9625. (concat
  9626. (when (member direction '("input" "output" "inout"))
  9627. (concat direction " "))
  9628. (or (verilog-sig-type sig)
  9629. verilog-auto-wire-type)))
  9630. ((and verilog-auto-declare-nettype
  9631. (member direction '("input" "output" "inout")))
  9632. (concat direction " " verilog-auto-declare-nettype))
  9633. (t
  9634. direction))
  9635. indent-pt)
  9636. (insert (if v2k "," ";"))
  9637. (if (or (not verilog-auto-wire-comment)
  9638. (not (verilog-sig-comment sig))
  9639. (equal "" (verilog-sig-comment sig)))
  9640. (insert "\n")
  9641. (indent-to (max 48 (+ indent-pt 40)))
  9642. (verilog-insert "// " (verilog-sig-comment sig) "\n"))
  9643. (setq sigs (cdr sigs)))))
  9644. (eval-when-compile
  9645. (if (not (boundp 'indent-pt))
  9646. (defvar indent-pt nil "Local used by `verilog-insert-indent'.")))
  9647. (defun verilog-insert-indent (&rest stuff)
  9648. "Indent to position stored in local `indent-pt' variable, then insert STUFF.
  9649. Presumes that any newlines end a list element."
  9650. (let ((need-indent t))
  9651. (while stuff
  9652. (if need-indent (indent-to indent-pt))
  9653. (setq need-indent nil)
  9654. (verilog-insert (car stuff))
  9655. (setq need-indent (string-match "\n$" (car stuff))
  9656. stuff (cdr stuff)))))
  9657. ;;(let ((indent-pt 10)) (verilog-insert-indent "hello\n" "addon" "there\n"))
  9658. (defun verilog-forward-or-insert-line ()
  9659. "Move forward a line, unless at EOB, then insert a newline."
  9660. (if (eobp) (insert "\n")
  9661. (forward-line)))
  9662. (defun verilog-repair-open-comma ()
  9663. "Insert comma if previous argument is other than an open parenthesis or endif."
  9664. ;; We can't just search backward for ) as it might be inside another expression.
  9665. ;; Also want "`ifdef X input foo `endif" to just leave things to the human to deal with
  9666. (save-excursion
  9667. (verilog-backward-syntactic-ws-quick)
  9668. (when (and (not (save-excursion ; Not beginning (, or existing ,
  9669. (backward-char 1)
  9670. (looking-at "[(,]")))
  9671. (not (save-excursion ; Not `endif, or user define
  9672. (backward-char 1)
  9673. (skip-chars-backward "[a-zA-Z0-9_`]")
  9674. (looking-at "`"))))
  9675. (insert ","))))
  9676. (defun verilog-repair-close-comma ()
  9677. "If point is at a comma followed by a close parenthesis, fix it.
  9678. This repairs those mis-inserted by an AUTOARG."
  9679. ;; It would be much nicer if Verilog allowed extra commas like Perl does!
  9680. (save-excursion
  9681. (verilog-forward-close-paren)
  9682. (backward-char 1)
  9683. (verilog-backward-syntactic-ws-quick)
  9684. (backward-char 1)
  9685. (when (looking-at ",")
  9686. (delete-char 1))))
  9687. (defun verilog-make-width-expression (range-exp)
  9688. "Return an expression calculating the length of a range [x:y] in RANGE-EXP."
  9689. ;; strip off the []
  9690. (cond ((not range-exp)
  9691. "1")
  9692. (t
  9693. (if (string-match "^\\[\\(.*\\)\\]$" range-exp)
  9694. (setq range-exp (match-string 1 range-exp)))
  9695. (cond ((not range-exp)
  9696. "1")
  9697. ;; [#:#] We can compute a numeric result
  9698. ((string-match "^\\s *\\([0-9]+\\)\\s *:\\s *\\([0-9]+\\)\\s *$"
  9699. range-exp)
  9700. (int-to-string
  9701. (1+ (abs (- (string-to-number (match-string 1 range-exp))
  9702. (string-to-number (match-string 2 range-exp)))))))
  9703. ;; [PARAM-1:0] can just return PARAM
  9704. ((string-match "^\\s *\\([a-zA-Z_][a-zA-Z0-9_]*\\)\\s *-\\s *1\\s *:\\s *0\\s *$" range-exp)
  9705. (match-string 1 range-exp))
  9706. ;; [arbitrary] need math
  9707. ((string-match "^\\(.*\\)\\s *:\\s *\\(.*\\)\\s *$" range-exp)
  9708. (concat "(1+(" (match-string 1 range-exp) ")"
  9709. (if (equal "0" (match-string 2 range-exp))
  9710. "" ; Don't bother with -(0)
  9711. (concat "-(" (match-string 2 range-exp) ")"))
  9712. ")"))
  9713. (t nil)))))
  9714. ;;(verilog-make-width-expression "`A:`B")
  9715. (defun verilog-simplify-range-expression (expr)
  9716. "Return a simplified range expression with constants eliminated from EXPR."
  9717. ;; Note this is always called with brackets; ie [z] or [z:z]
  9718. (if (not (string-match "[---+*()]" expr))
  9719. expr ; short-circuit
  9720. (let ((out expr)
  9721. (last-pass ""))
  9722. (while (not (equal last-pass out))
  9723. (setq last-pass out)
  9724. ;; Prefix regexp needs beginning of match, or some symbol of
  9725. ;; lesser or equal precedence. We assume the [:]'s exist in expr.
  9726. ;; Ditto the end.
  9727. (while (string-match
  9728. (concat "\\([[({:*+-]\\)" ; - must be last
  9729. "(\\<\\([0-9A-Za-z_]+\\))"
  9730. "\\([])}:*+-]\\)")
  9731. out)
  9732. (setq out (replace-match "\\1\\2\\3" nil nil out)))
  9733. (while (string-match
  9734. (concat "\\([[({:*+-]\\)" ; - must be last
  9735. "\\$clog2\\s *(\\<\\([0-9]+\\))"
  9736. "\\([])}:*+-]\\)")
  9737. out)
  9738. (setq out (replace-match
  9739. (concat
  9740. (match-string 1 out)
  9741. (int-to-string (verilog-clog2 (string-to-number (match-string 2 out))))
  9742. (match-string 3 out))
  9743. nil nil out)))
  9744. ;; For precedence do * before +/-
  9745. (while (string-match
  9746. (concat "\\([[({:*+-]\\)"
  9747. "\\([0-9]+\\)\\s *\\([*]\\)\\s *\\([0-9]+\\)"
  9748. "\\([])}:*+-]\\)")
  9749. out)
  9750. (setq out (replace-match
  9751. (concat (match-string 1 out)
  9752. (int-to-string (* (string-to-number (match-string 2 out))
  9753. (string-to-number (match-string 4 out))))
  9754. (match-string 5 out))
  9755. nil nil out)))
  9756. (while (string-match
  9757. (concat "\\([[({:+-]\\)" ; No * here as higher prec
  9758. "\\([0-9]+\\)\\s *\\([---+]\\)\\s *\\([0-9]+\\)"
  9759. "\\([])}:+-]\\)")
  9760. out)
  9761. (let ((pre (match-string 1 out))
  9762. (lhs (string-to-number (match-string 2 out)))
  9763. (rhs (string-to-number (match-string 4 out)))
  9764. (post (match-string 5 out))
  9765. val)
  9766. (when (equal pre "-")
  9767. (setq lhs (- lhs)))
  9768. (setq val (if (equal (match-string 3 out) "-")
  9769. (- lhs rhs)
  9770. (+ lhs rhs))
  9771. out (replace-match
  9772. (concat (if (and (equal pre "-")
  9773. (< val 0))
  9774. "" ; Not "--20" but just "-20"
  9775. pre)
  9776. (int-to-string val)
  9777. post)
  9778. nil nil out)) )))
  9779. out)))
  9780. ;;(verilog-simplify-range-expression "[1:3]") ; 1
  9781. ;;(verilog-simplify-range-expression "[(1):3]") ; 1
  9782. ;;(verilog-simplify-range-expression "[(((16)+1)+1+(1+1))]") ; 20
  9783. ;;(verilog-simplify-range-expression "[(2*3+6*7)]") ; 48
  9784. ;;(verilog-simplify-range-expression "[(FOO*4-1*2)]") ; FOO*4-2
  9785. ;;(verilog-simplify-range-expression "[(FOO*4+1-1)]") ; FOO*4+0
  9786. ;;(verilog-simplify-range-expression "[(func(BAR))]") ; func(BAR)
  9787. ;;(verilog-simplify-range-expression "[FOO-1+1-1+1]") ; FOO-0
  9788. ;;(verilog-simplify-range-expression "[$clog2(2)]") ; 1
  9789. ;;(verilog-simplify-range-expression "[$clog2(7)]") ; 3
  9790. (defun verilog-clog2 (value)
  9791. "Compute $clog2 - ceiling log2 of VALUE."
  9792. (if (< value 1)
  9793. 0
  9794. (ceiling (/ (log value) (log 2)))))
  9795. (defun verilog-typedef-name-p (variable-name)
  9796. "Return true if the VARIABLE-NAME is a type definition."
  9797. (when verilog-typedef-regexp
  9798. (verilog-string-match-fold verilog-typedef-regexp variable-name)))
  9799. ;;; Auto deletion:
  9800. ;;
  9801. (defun verilog-delete-autos-lined ()
  9802. "Delete autos that occupy multiple lines, between begin and end comments."
  9803. ;; The newline must not have a comment property, so we must
  9804. ;; delete the end auto's newline, not the first newline
  9805. (forward-line 1)
  9806. (let ((pt (point)))
  9807. (when (and
  9808. (looking-at "\\s-*// Beginning")
  9809. (search-forward "// End of automatic" nil t))
  9810. ;; End exists
  9811. (end-of-line)
  9812. (forward-line 1)
  9813. (delete-region pt (point)))))
  9814. (defun verilog-delete-empty-auto-pair ()
  9815. "Delete begin/end auto pair at point, if empty."
  9816. (forward-line 0)
  9817. (when (looking-at (concat "\\s-*// Beginning of automatic.*\n"
  9818. "\\s-*// End of automatics\n"))
  9819. (delete-region (point) (save-excursion (forward-line 2) (point)))))
  9820. (defun verilog-forward-close-paren ()
  9821. "Find the close parenthesis that match the current point.
  9822. Ignore other close parenthesis with matching open parens."
  9823. (let ((parens 1))
  9824. (while (> parens 0)
  9825. (unless (verilog-re-search-forward-quick "[()]" nil t)
  9826. (error "%s: Mismatching ()" (verilog-point-text)))
  9827. (cond ((= (preceding-char) ?\( )
  9828. (setq parens (1+ parens)))
  9829. ((= (preceding-char) ?\) )
  9830. (setq parens (1- parens)))))))
  9831. (defun verilog-backward-open-paren ()
  9832. "Find the open parenthesis that match the current point.
  9833. Ignore other open parenthesis with matching close parens."
  9834. (let ((parens 1))
  9835. (while (> parens 0)
  9836. (unless (verilog-re-search-backward-quick "[()]" nil t)
  9837. (error "%s: Mismatching ()" (verilog-point-text)))
  9838. (cond ((= (following-char) ?\) )
  9839. (setq parens (1+ parens)))
  9840. ((= (following-char) ?\( )
  9841. (setq parens (1- parens)))))))
  9842. (defun verilog-backward-open-bracket ()
  9843. "Find the open bracket that match the current point.
  9844. Ignore other open bracket with matching close bracket."
  9845. (let ((parens 1))
  9846. (while (> parens 0)
  9847. (unless (verilog-re-search-backward-quick "[][]" nil t)
  9848. (error "%s: Mismatching []" (verilog-point-text)))
  9849. (cond ((= (following-char) ?\] )
  9850. (setq parens (1+ parens)))
  9851. ((= (following-char) ?\[ )
  9852. (setq parens (1- parens)))))))
  9853. (defun verilog-delete-to-paren ()
  9854. "Delete the automatic inst/sense/arg created by autos.
  9855. Deletion stops at the matching end parenthesis, outside comments."
  9856. (delete-region (point)
  9857. (save-excursion
  9858. (verilog-backward-open-paren)
  9859. (verilog-forward-sexp-ign-cmt 1) ; Moves to paren that closes argdecl's
  9860. (backward-char 1)
  9861. (point))))
  9862. (defun verilog-auto-star-safe ()
  9863. "Return if a .* AUTOINST is safe to delete or expand.
  9864. It was created by the AUTOS themselves, or by the user."
  9865. (and verilog-auto-star-expand
  9866. (looking-at
  9867. (concat "[ \t\n\f,]*\\([)]\\|// " verilog-inst-comment-re "\\)"))))
  9868. (defun verilog-delete-auto-star-all ()
  9869. "Delete a .* AUTOINST, if it is safe."
  9870. (when (verilog-auto-star-safe)
  9871. (verilog-delete-to-paren)))
  9872. (defun verilog-delete-auto-star-implicit ()
  9873. "Delete all .* implicit connections created by `verilog-auto-star'.
  9874. This function will be called automatically at save unless
  9875. `verilog-auto-star-save' is set, any non-templated expanded pins will be
  9876. removed."
  9877. (interactive)
  9878. (let (paren-pt indent have-close-paren)
  9879. (save-excursion
  9880. (goto-char (point-min))
  9881. ;; We need to match these even outside of comments.
  9882. ;; For reasonable performance, we don't check if inside comments, sorry.
  9883. (while (re-search-forward "// Implicit \\.\\*" nil t)
  9884. (setq paren-pt (point))
  9885. (beginning-of-line)
  9886. (setq have-close-paren
  9887. (save-excursion
  9888. (when (search-forward ");" paren-pt t)
  9889. (setq indent (current-indentation))
  9890. t)))
  9891. (delete-region (point) (+ 1 paren-pt)) ; Nuke line incl CR
  9892. (when have-close-paren
  9893. ;; Delete extra commentary
  9894. (save-excursion
  9895. (while (progn
  9896. (forward-line -1)
  9897. (looking-at (concat "\\s *//\\s *" verilog-inst-comment-re "\n")))
  9898. (delete-region (match-beginning 0) (match-end 0))))
  9899. ;; If it is simple, we can put the ); on the same line as the last text
  9900. (let ((rtn-pt (point)))
  9901. (save-excursion
  9902. (while (progn (backward-char 1)
  9903. (looking-at "[ \t\n\f]")))
  9904. (when (looking-at ",")
  9905. (delete-region (+ 1 (point)) rtn-pt))))
  9906. (when (bolp)
  9907. (indent-to indent))
  9908. (insert ");\n")
  9909. ;; Still need to kill final comma - always is one as we put one after the .*
  9910. (re-search-backward ",")
  9911. (delete-char 1))))))
  9912. (defun verilog-delete-auto-buffer ()
  9913. "Perform `verilog-delete-auto' on the current buffer.
  9914. Intended for internal use inside a `verilog-save-font-no-change-functions' block."
  9915. ;; Allow user to customize
  9916. (verilog-run-hooks 'verilog-before-delete-auto-hook)
  9917. ;; Remove those that have multi-line insertions, possibly with parameters
  9918. ;; We allow anything beginning with AUTO, so that users can add their own
  9919. ;; patterns
  9920. (verilog-auto-re-search-do
  9921. (concat "/\\*AUTO[A-Za-z0-9_]+"
  9922. ;; Optional parens or quoted parameter or .* for (((...)))
  9923. "\\(\\|([^)]*)\\|(\"[^\"]*\")\\).*?"
  9924. "\\*/")
  9925. 'verilog-delete-autos-lined)
  9926. ;; Remove those that are in parenthesis
  9927. (verilog-auto-re-search-do
  9928. (concat "/\\*"
  9929. (eval-when-compile
  9930. (verilog-regexp-words
  9931. `("AS" "AUTOARG" "AUTOCONCATWIDTH" "AUTOINST" "AUTOINSTPARAM"
  9932. "AUTOSENSE")))
  9933. "\\*/")
  9934. 'verilog-delete-to-paren)
  9935. ;; Do .* instantiations, but avoid removing any user pins by looking for our magic comments
  9936. (verilog-auto-re-search-do "\\.\\*"
  9937. 'verilog-delete-auto-star-all)
  9938. ;; Remove template comments ... anywhere in case was pasted after AUTOINST removed
  9939. (goto-char (point-min))
  9940. (while (re-search-forward "\\s-*// \\(Templated\\|Implicit \\.\\*\\)\\([ \tLT0-9]*\\| LHS: .*\\)?$" nil t)
  9941. (replace-match ""))
  9942. ;; Final customize
  9943. (verilog-run-hooks 'verilog-delete-auto-hook))
  9944. (defun verilog-delete-auto ()
  9945. "Delete the automatic outputs, regs, and wires created by \\[verilog-auto].
  9946. Use \\[verilog-auto] to re-insert the updated AUTOs.
  9947. The hooks `verilog-before-delete-auto-hook' and `verilog-delete-auto-hook' are
  9948. called before and after this function, respectively."
  9949. (interactive)
  9950. (save-excursion
  9951. (if (buffer-file-name)
  9952. (find-file-noselect (buffer-file-name))) ; To check we have latest version
  9953. (verilog-save-font-no-change-functions
  9954. (verilog-save-scan-cache
  9955. (verilog-delete-auto-buffer)))))
  9956. ;;; Auto inject:
  9957. ;;
  9958. (defun verilog-inject-auto ()
  9959. "Examine legacy non-AUTO code and insert AUTOs in appropriate places.
  9960. Any always @ blocks with sensitivity lists that match computed lists will
  9961. be replaced with /*AS*/ comments.
  9962. Any cells will get /*AUTOINST*/ added to the end of the pin list.
  9963. Pins with have identical names will be deleted.
  9964. Argument lists will not be deleted, /*AUTOARG*/ will only be inserted to
  9965. support adding new ports. You may wish to delete older ports yourself.
  9966. For example:
  9967. module ExampInject (i, o);
  9968. input i;
  9969. input j;
  9970. output o;
  9971. always @ (i or j)
  9972. o = i | j;
  9973. InstModule instName
  9974. (.foobar(baz),
  9975. j(j));
  9976. endmodule
  9977. Typing \\[verilog-inject-auto] will make this into:
  9978. module ExampInject (i, o/*AUTOARG*/
  9979. // Inputs
  9980. j);
  9981. input i;
  9982. output o;
  9983. always @ (/*AS*/i or j)
  9984. o = i | j;
  9985. InstModule instName
  9986. (.foobar(baz),
  9987. /*AUTOINST*/
  9988. // Outputs
  9989. j(j));
  9990. endmodule"
  9991. (interactive)
  9992. (verilog-auto t))
  9993. (defun verilog-inject-arg ()
  9994. "Inject AUTOARG into new code. See `verilog-inject-auto'."
  9995. ;; Presume one module per file.
  9996. (save-excursion
  9997. (goto-char (point-min))
  9998. (while (verilog-re-search-forward-quick "\\<module\\>" nil t)
  9999. (let ((endmodp (save-excursion
  10000. (verilog-re-search-forward-quick "\\<endmodule\\>" nil t)
  10001. (point))))
  10002. ;; See if there's already a comment .. inside a comment so not verilog-re-search
  10003. (when (not (re-search-forward "/\\*AUTOARG\\*/" endmodp t))
  10004. (verilog-re-search-forward-quick ";" nil t)
  10005. (backward-char 1)
  10006. (verilog-backward-syntactic-ws-quick)
  10007. (backward-char 1) ; Moves to paren that closes argdecl's
  10008. (when (looking-at ")")
  10009. (verilog-insert "/*AUTOARG*/")))))))
  10010. (defun verilog-inject-sense ()
  10011. "Inject AUTOSENSE into new code. See `verilog-inject-auto'."
  10012. (save-excursion
  10013. (goto-char (point-min))
  10014. (while (verilog-re-search-forward-quick "\\<always\\s *@\\s *(" nil t)
  10015. (let* ((start-pt (point))
  10016. (modi (verilog-modi-current))
  10017. (moddecls (verilog-modi-get-decls modi))
  10018. pre-sigs
  10019. got-sigs)
  10020. (backward-char 1)
  10021. (verilog-forward-sexp-ign-cmt 1)
  10022. (backward-char 1) ; End )
  10023. (when (not (verilog-re-search-backward-quick "/\\*\\(AUTOSENSE\\|AS\\)\\*/" start-pt t))
  10024. (setq pre-sigs (verilog-signals-from-signame
  10025. (verilog-read-signals start-pt (point)))
  10026. got-sigs (verilog-auto-sense-sigs moddecls nil))
  10027. (when (not (or (verilog-signals-not-in pre-sigs got-sigs) ; Both are equal?
  10028. (verilog-signals-not-in got-sigs pre-sigs)))
  10029. (delete-region start-pt (point))
  10030. (verilog-insert "/*AS*/")))))))
  10031. (defun verilog-inject-inst ()
  10032. "Inject AUTOINST into new code. See `verilog-inject-auto'."
  10033. (save-excursion
  10034. (goto-char (point-min))
  10035. ;; It's hard to distinguish modules; we'll instead search for pins.
  10036. (while (verilog-re-search-forward-quick "\\.\\s *[a-zA-Z0-9`_$]+\\s *(\\s *[a-zA-Z0-9`_$]+\\s *)" nil t)
  10037. (verilog-backward-open-paren) ; Inst start
  10038. (cond
  10039. ((= (preceding-char) ?\#) ; #(...) parameter section, not pin. Skip.
  10040. (forward-char 1)
  10041. (verilog-forward-close-paren)) ; Parameters done
  10042. (t
  10043. (forward-char 1)
  10044. (let ((indent-pt (+ (current-column)))
  10045. (end-pt (save-excursion (verilog-forward-close-paren) (point))))
  10046. (cond ((verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-pt t)
  10047. (goto-char end-pt)) ; Already there, continue search with next instance
  10048. (t
  10049. ;; Delete identical interconnect
  10050. (let ((case-fold-search nil)) ; So we don't convert upper-to-lower, etc
  10051. (while (verilog-re-search-forward-quick "\\.\\s *\\([a-zA-Z0-9`_$]+\\)*\\s *(\\s *\\1\\s *)\\s *" end-pt t)
  10052. (delete-region (match-beginning 0) (match-end 0))
  10053. (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))) ; Keep it correct
  10054. (while (or (looking-at "[ \t\n\f,]+")
  10055. (looking-at "//[^\n]*"))
  10056. (delete-region (match-beginning 0) (match-end 0))
  10057. (setq end-pt (- end-pt (- (match-end 0) (match-beginning 0)))))))
  10058. (verilog-forward-close-paren)
  10059. (backward-char 1)
  10060. ;; Not verilog-re-search, as we don't want to strip comments
  10061. (while (re-search-backward "[ \t\n\f]+" (- (point) 1) t)
  10062. (delete-region (match-beginning 0) (match-end 0)))
  10063. (verilog-insert "\n")
  10064. (verilog-insert-indent "/*AUTOINST*/")))))))))
  10065. ;;
  10066. ;; Auto diff:
  10067. ;;
  10068. (defun verilog-diff-buffers-p (b1 b2 &optional whitespace regexp)
  10069. "Return nil if buffers B1 and B2 have same contents.
  10070. Else, return point in B1 that first mismatches.
  10071. If optional WHITESPACE true, ignore whitespace.
  10072. If optional REGEXP, ignore differences matching it."
  10073. (save-excursion
  10074. (let* ((case-fold-search nil) ; compare-buffer-substrings cares
  10075. (p1 (with-current-buffer b1 (goto-char (point-min))))
  10076. (p2 (with-current-buffer b2 (goto-char (point-min))))
  10077. (maxp1 (with-current-buffer b1 (point-max)))
  10078. (maxp2 (with-current-buffer b2 (point-max)))
  10079. (op1 -1) (op2 -1)
  10080. progress size)
  10081. (while (not (and (eq p1 op1) (eq p2 op2)))
  10082. ;; If both windows have whitespace optionally skip over it.
  10083. (when whitespace
  10084. ;; skip-syntax-* doesn't count \n
  10085. (with-current-buffer b1
  10086. (goto-char p1)
  10087. (skip-chars-forward " \t\n\r\f\v")
  10088. (setq p1 (point)))
  10089. (with-current-buffer b2
  10090. (goto-char p2)
  10091. (skip-chars-forward " \t\n\r\f\v")
  10092. (setq p2 (point))))
  10093. (when regexp
  10094. (with-current-buffer b1
  10095. (goto-char p1)
  10096. (when (looking-at regexp)
  10097. (setq p1 (match-end 0))))
  10098. (with-current-buffer b2
  10099. (goto-char p2)
  10100. (when (looking-at regexp)
  10101. (setq p2 (match-end 0)))))
  10102. (setq size (min (- maxp1 p1) (- maxp2 p2)))
  10103. (setq progress (compare-buffer-substrings b2 p2 (+ size p2)
  10104. b1 p1 (+ size p1)))
  10105. (setq progress (if (zerop progress) size (1- (abs progress))))
  10106. (setq op1 p1 op2 p2
  10107. p1 (+ p1 progress)
  10108. p2 (+ p2 progress)))
  10109. ;; Return value
  10110. (if (and (eq p1 maxp1) (eq p2 maxp2))
  10111. nil p1))))
  10112. (defun verilog-diff-file-with-buffer (f1 b2 &optional whitespace show)
  10113. "View the differences between file F1 and buffer B2.
  10114. This requires the external program `diff-command' to be in your `exec-path',
  10115. and uses `diff-switches' in which you may want to have \"-u\" flag.
  10116. Ignores WHITESPACE if t, and writes output to stdout if SHOW."
  10117. ;; Similar to `diff-buffer-with-file' but works on XEmacs, and doesn't
  10118. ;; call `diff' as `diff' has different calling semantics on different
  10119. ;; versions of Emacs.
  10120. (if (not (file-exists-p f1))
  10121. (message "Buffer `%s' has no associated file on disk" (buffer-name b2))
  10122. (with-temp-buffer "*Verilog-Diff*"
  10123. (let ((outbuf (current-buffer))
  10124. (f2 (make-temp-file "vm-diff-auto-")))
  10125. (unwind-protect
  10126. (progn
  10127. (with-current-buffer b2
  10128. (save-restriction
  10129. (widen)
  10130. (write-region (point-min) (point-max) f2 nil 'nomessage)))
  10131. (call-process diff-command nil outbuf t
  10132. diff-switches ; User may want -u in diff-switches
  10133. (if whitespace "-b" "")
  10134. f1 f2)
  10135. ;; Print out results. Alternatively we could have call-processed
  10136. ;; ourself, but this way we can reuse diff switches
  10137. (when show
  10138. (with-current-buffer outbuf (message "%s" (buffer-string))))))
  10139. (sit-for 0)
  10140. (when (file-exists-p f2)
  10141. (delete-file f2))))))
  10142. (defun verilog-diff-report (b1 b2 diffpt)
  10143. "Report differences detected with `verilog-diff-auto'.
  10144. Differences are between buffers B1 and B2, starting at point
  10145. DIFFPT. This function is called via `verilog-diff-function'."
  10146. (let ((name1 (with-current-buffer b1 (buffer-file-name))))
  10147. (verilog-warn-error "%s:%d: Difference in AUTO expansion found"
  10148. name1 (with-current-buffer b1
  10149. (count-lines (point-min) diffpt)))
  10150. (cond (noninteractive
  10151. (verilog-diff-file-with-buffer name1 b2 t t))
  10152. (t
  10153. (ediff-buffers b1 b2)))))
  10154. (defun verilog-diff-auto ()
  10155. "Expand AUTOs in a temporary buffer and indicate any change.
  10156. Whitespace is ignored when detecting differences, but once a
  10157. difference is detected, whitespace differences may be shown.
  10158. To call this from the command line, see \\[verilog-batch-diff-auto].
  10159. The action on differences is selected with
  10160. `verilog-diff-function'. The default is `verilog-diff-report'
  10161. which will report an error and run `ediff' in interactive mode,
  10162. or `diff' in batch mode."
  10163. (interactive)
  10164. (let ((b1 (current-buffer)) b2 diffpt
  10165. (name1 (buffer-file-name))
  10166. (newname "*Verilog-Diff*"))
  10167. (save-excursion
  10168. (when (get-buffer newname)
  10169. (kill-buffer newname))
  10170. (setq b2 (let (buffer-file-name) ; Else clone is upset
  10171. (clone-buffer newname)))
  10172. (with-current-buffer b2
  10173. ;; auto requires the filename, but can't have same filename in two
  10174. ;; buffers; so override both b1 and b2's names
  10175. (let ((buffer-file-name name1))
  10176. (unwind-protect
  10177. (progn
  10178. (with-current-buffer b1 (setq buffer-file-name nil))
  10179. (verilog-auto)
  10180. (when (not verilog-auto-star-save)
  10181. (verilog-delete-auto-star-implicit)))
  10182. ;; Restore name if unwind
  10183. (with-current-buffer b1 (setq buffer-file-name name1)))))
  10184. ;;
  10185. (setq diffpt (verilog-diff-buffers-p b1 b2 t verilog-diff-ignore-regexp))
  10186. (cond ((not diffpt)
  10187. (unless noninteractive (message "AUTO expansion identical"))
  10188. (kill-buffer newname)) ; Nice to cleanup after oneself
  10189. (t
  10190. (funcall verilog-diff-function b1 b2 diffpt)))
  10191. ;; Return result of compare
  10192. diffpt)))
  10193. ;;
  10194. ;; Auto save
  10195. ;;
  10196. (defun verilog-auto-save-check ()
  10197. "On saving see if we need auto update."
  10198. (cond ((not verilog-auto-save-policy)) ; disabled
  10199. ((not (save-excursion
  10200. (save-match-data
  10201. (let ((case-fold-search nil))
  10202. (goto-char (point-min))
  10203. (re-search-forward "AUTO" nil t))))))
  10204. ((eq verilog-auto-save-policy 'force)
  10205. (verilog-auto))
  10206. ((not (buffer-modified-p)))
  10207. ((eq verilog-auto-update-tick (buffer-chars-modified-tick))) ; up-to-date
  10208. ((eq verilog-auto-save-policy 'detect)
  10209. (verilog-auto))
  10210. (t
  10211. (when (yes-or-no-p "AUTO statements not recomputed, do it now? ")
  10212. (verilog-auto))
  10213. ;; Don't ask again if didn't update
  10214. (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))))
  10215. (when (not verilog-auto-star-save)
  10216. (verilog-delete-auto-star-implicit))
  10217. nil) ; Always return nil -- we don't write the file ourselves
  10218. (defun verilog-auto-read-locals ()
  10219. "Return file local variable segment at bottom of file."
  10220. (save-excursion
  10221. (goto-char (point-max))
  10222. (if (re-search-backward "Local Variables:" nil t)
  10223. (buffer-substring-no-properties (point) (point-max))
  10224. "")))
  10225. (defun verilog-auto-reeval-locals (&optional force)
  10226. "Read file local variable segment at bottom of file if it has changed.
  10227. If FORCE, always reread it."
  10228. (let ((curlocal (verilog-auto-read-locals)))
  10229. (when (or force (not (equal verilog-auto-last-file-locals curlocal)))
  10230. (set (make-local-variable 'verilog-auto-last-file-locals) curlocal)
  10231. ;; Note this may cause this function to be recursively invoked,
  10232. ;; because hack-local-variables may call (verilog-mode)
  10233. ;; The above when statement will prevent it from recursing forever.
  10234. (hack-local-variables)
  10235. t)))
  10236. ;;; Auto creation:
  10237. ;;
  10238. (defun verilog-auto-arg-ports (sigs message indent-pt)
  10239. "Print a list of ports for AUTOARG.
  10240. Takes SIGS list, adds MESSAGE to front and inserts each at INDENT-PT."
  10241. (when sigs
  10242. (when verilog-auto-arg-sort
  10243. (setq sigs (sort (copy-alist sigs) `verilog-signals-sort-compare)))
  10244. (insert "\n")
  10245. (indent-to indent-pt)
  10246. (insert message)
  10247. (insert "\n")
  10248. (let ((space ""))
  10249. (indent-to indent-pt)
  10250. (while sigs
  10251. (cond ((equal verilog-auto-arg-format 'single)
  10252. (insert space)
  10253. (indent-to indent-pt)
  10254. (setq space "\n"))
  10255. ;; verilog-auto-arg-format 'packed
  10256. ((> (+ 2 (current-column) (length (verilog-sig-name (car sigs)))) fill-column)
  10257. (insert "\n")
  10258. (indent-to indent-pt)
  10259. (setq space " "))
  10260. (t
  10261. (insert space)
  10262. (setq space " ")))
  10263. (insert (verilog-sig-name (car sigs)) ",")
  10264. (setq sigs (cdr sigs))))))
  10265. (defun verilog-auto-arg ()
  10266. "Expand AUTOARG statements.
  10267. Replace the argument declarations at the beginning of the
  10268. module with ones automatically derived from input and output
  10269. statements. This can be dangerous if the module is instantiated
  10270. using position-based connections, so use only name-based when
  10271. instantiating the resulting module. Long lines are split based
  10272. on the `fill-column', see \\[set-fill-column].
  10273. Limitations:
  10274. Concatenation and outputting partial buses is not supported.
  10275. Typedefs must match `verilog-typedef-regexp', which is disabled by default.
  10276. For example:
  10277. module ExampArg (/*AUTOARG*/);
  10278. input i;
  10279. output o;
  10280. endmodule
  10281. Typing \\[verilog-auto] will make this into:
  10282. module ExampArg (/*AUTOARG*/
  10283. // Outputs
  10284. o,
  10285. // Inputs
  10286. i
  10287. );
  10288. input i;
  10289. output o;
  10290. endmodule
  10291. The argument declarations may be printed in declaration order to
  10292. best suit order based instantiations, or alphabetically, based on
  10293. the `verilog-auto-arg-sort' variable.
  10294. Formatting is controlled with `verilog-auto-arg-format' variable.
  10295. Any ports declared between the ( and /*AUTOARG*/ are presumed to be
  10296. predeclared and are not redeclared by AUTOARG. AUTOARG will make a
  10297. conservative guess on adding a comma for the first signal, if you have
  10298. any ifdefs or complicated expressions before the AUTOARG you will need
  10299. to choose the comma yourself.
  10300. Avoid declaring ports manually, as it makes code harder to maintain."
  10301. (save-excursion
  10302. (let* ((modi (verilog-modi-current))
  10303. (moddecls (verilog-modi-get-decls modi))
  10304. (skip-pins (aref (verilog-read-arg-pins) 0)))
  10305. (verilog-repair-open-comma)
  10306. (verilog-auto-arg-ports (verilog-signals-not-in
  10307. (verilog-decls-get-outputs moddecls)
  10308. skip-pins)
  10309. "// Outputs"
  10310. verilog-indent-level-declaration)
  10311. (verilog-auto-arg-ports (verilog-signals-not-in
  10312. (verilog-decls-get-inouts moddecls)
  10313. skip-pins)
  10314. "// Inouts"
  10315. verilog-indent-level-declaration)
  10316. (verilog-auto-arg-ports (verilog-signals-not-in
  10317. (verilog-decls-get-inputs moddecls)
  10318. skip-pins)
  10319. "// Inputs"
  10320. verilog-indent-level-declaration)
  10321. (verilog-repair-close-comma)
  10322. (unless (eq (char-before) ?/ )
  10323. (insert "\n"))
  10324. (indent-to verilog-indent-level-declaration))))
  10325. (defun verilog-auto-assign-modport ()
  10326. "Expand AUTOASSIGNMODPORT statements, as part of \\[verilog-auto].
  10327. Take input/output/inout statements from the specified interface
  10328. and modport and use to build assignments into the modport, for
  10329. making verification modules that connect to UVM interfaces.
  10330. The first parameter is the name of an interface.
  10331. The second parameter is a regexp of modports to read from in
  10332. that interface.
  10333. The third parameter is the instance name to use to dot reference into.
  10334. The optional fourth parameter is a regular expression, and only
  10335. signals matching the regular expression will be included.
  10336. Limitations:
  10337. Interface names must be resolvable to filenames. See `verilog-auto-inst'.
  10338. Inouts are not supported, as assignments must be unidirectional.
  10339. If a signal is part of the interface header and in both a
  10340. modport and the interface itself, it will not be listed. (As
  10341. this would result in a syntax error when the connections are
  10342. made.)
  10343. See the example in `verilog-auto-inout-modport'."
  10344. (save-excursion
  10345. (let* ((params (verilog-read-auto-params 3 4))
  10346. (submod (nth 0 params))
  10347. (modport-re (nth 1 params))
  10348. (inst-name (nth 2 params))
  10349. (regexp (nth 3 params))
  10350. direction-re submodi) ; direction argument not supported until requested
  10351. ;; Lookup position, etc of co-module
  10352. ;; Note this may raise an error
  10353. (when (setq submodi (verilog-modi-lookup submod t))
  10354. (let* ((indent-pt (current-indentation))
  10355. (submoddecls (verilog-modi-get-decls submodi))
  10356. (submodportdecls (verilog-modi-modport-lookup submodi modport-re))
  10357. (sig-list-i (verilog-signals-in ; Decls doesn't have data types, must resolve
  10358. (verilog-decls-get-vars submoddecls)
  10359. (verilog-signals-not-in
  10360. (verilog-decls-get-inputs submodportdecls)
  10361. (verilog-decls-get-ports submoddecls))))
  10362. (sig-list-o (verilog-signals-in ; Decls doesn't have data types, must resolve
  10363. (verilog-decls-get-vars submoddecls)
  10364. (verilog-signals-not-in
  10365. (verilog-decls-get-outputs submodportdecls)
  10366. (verilog-decls-get-ports submoddecls)))))
  10367. (forward-line 1)
  10368. (setq sig-list-i (verilog-signals-edit-wire-reg
  10369. (verilog-signals-matching-dir-re
  10370. (verilog-signals-matching-regexp sig-list-i regexp)
  10371. "input" direction-re))
  10372. sig-list-o (verilog-signals-edit-wire-reg
  10373. (verilog-signals-matching-dir-re
  10374. (verilog-signals-matching-regexp sig-list-o regexp)
  10375. "output" direction-re)))
  10376. (setq sig-list-i (sort (copy-alist sig-list-i) `verilog-signals-sort-compare))
  10377. (setq sig-list-o (sort (copy-alist sig-list-o) `verilog-signals-sort-compare))
  10378. (when (or sig-list-i sig-list-o)
  10379. (verilog-insert-indent "// Beginning of automatic assignments from modport\n")
  10380. ;; Don't sort them so an upper AUTOINST will match the main module
  10381. (let ((sigs sig-list-o))
  10382. (while sigs
  10383. (verilog-insert-indent "assign " (verilog-sig-name (car sigs))
  10384. " = " inst-name
  10385. "." (verilog-sig-name (car sigs)) ";\n")
  10386. (setq sigs (cdr sigs))))
  10387. (let ((sigs sig-list-i))
  10388. (while sigs
  10389. (verilog-insert-indent "assign " inst-name
  10390. "." (verilog-sig-name (car sigs))
  10391. " = " (verilog-sig-name (car sigs)) ";\n")
  10392. (setq sigs (cdr sigs))))
  10393. (verilog-insert-indent "// End of automatics\n")))))))
  10394. (defun verilog-auto-inst-port-map (_port-st)
  10395. nil)
  10396. (defvar vl-cell-type nil "See `verilog-auto-inst'.") ; Prevent compile warning
  10397. (defvar vl-cell-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
  10398. (defvar vl-modport nil "See `verilog-auto-inst'.") ; Prevent compile warning
  10399. (defvar vl-name nil "See `verilog-auto-inst'.") ; Prevent compile warning
  10400. (defvar vl-width nil "See `verilog-auto-inst'.") ; Prevent compile warning
  10401. (defvar vl-dir nil "See `verilog-auto-inst'.") ; Prevent compile warning
  10402. (defvar vl-bits nil "See `verilog-auto-inst'.") ; Prevent compile warning
  10403. (defvar vl-mbits nil "See `verilog-auto-inst'.") ; Prevent compile warning
  10404. (defun verilog-auto-inst-port (port-st indent-pt moddecls tpl-list tpl-num for-star par-values)
  10405. "Print out an instantiation connection for this PORT-ST.
  10406. Insert to INDENT-PT, use template TPL-LIST.
  10407. @ are instantiation numbers, replaced with TPL-NUM.
  10408. @\"(expression @)\" are evaluated, with @ as a variable.
  10409. If FOR-STAR add comment it is a .* expansion.
  10410. If PAR-VALUES replace final strings with these parameter values."
  10411. (let* ((port (verilog-sig-name port-st))
  10412. (tpl-ass (or (assoc port (car tpl-list))
  10413. (verilog-auto-inst-port-map port-st)))
  10414. ;; vl-* are documented for user use
  10415. (vl-name (verilog-sig-name port-st))
  10416. (vl-width (verilog-sig-width port-st))
  10417. (vl-modport (verilog-sig-modport port-st))
  10418. (vl-memory (verilog-sig-memory port-st))
  10419. (vl-mbits (if (verilog-sig-multidim port-st)
  10420. (verilog-sig-multidim-string port-st) ""))
  10421. (vl-bits (if (or verilog-auto-inst-vector
  10422. (not (assoc port (verilog-decls-get-signals moddecls)))
  10423. (not (equal (verilog-sig-bits port-st)
  10424. (verilog-sig-bits
  10425. (assoc port (verilog-decls-get-signals moddecls))))))
  10426. (or (verilog-sig-bits port-st) "")
  10427. ""))
  10428. (case-fold-search nil)
  10429. (check-values par-values)
  10430. tpl-net dflt-bits)
  10431. ;; Replace parameters in bit-width
  10432. (when (and check-values
  10433. (not (equal vl-bits "")))
  10434. (while check-values
  10435. (setq vl-bits (verilog-string-replace-matches
  10436. (concat "\\<" (nth 0 (car check-values)) "\\>")
  10437. (concat "(" (nth 1 (car check-values)) ")")
  10438. t t vl-bits)
  10439. vl-mbits (verilog-string-replace-matches
  10440. (concat "\\<" (nth 0 (car check-values)) "\\>")
  10441. (concat "(" (nth 1 (car check-values)) ")")
  10442. t t vl-mbits)
  10443. vl-memory (when vl-memory
  10444. (verilog-string-replace-matches
  10445. (concat "\\<" (nth 0 (car check-values)) "\\>")
  10446. (concat "(" (nth 1 (car check-values)) ")")
  10447. t t vl-memory))
  10448. check-values (cdr check-values)))
  10449. (setq vl-bits (verilog-simplify-range-expression vl-bits)
  10450. vl-mbits (verilog-simplify-range-expression vl-mbits)
  10451. vl-memory (when vl-memory (verilog-simplify-range-expression vl-memory))
  10452. vl-width (verilog-make-width-expression vl-bits))) ; Not in the loop for speed
  10453. ;; Default net value if not found
  10454. (setq dflt-bits (if (or (and (verilog-sig-bits port-st)
  10455. (verilog-sig-multidim port-st))
  10456. (verilog-sig-memory port-st))
  10457. (concat "/*" vl-mbits vl-bits
  10458. ;; .[ used to separate packed from unpacked
  10459. (if vl-memory "." "")
  10460. (if vl-memory vl-memory "")
  10461. "*/")
  10462. (concat vl-bits))
  10463. tpl-net (concat port
  10464. (if (and vl-modport
  10465. ;; .modport cannot be added if attachment is
  10466. ;; already declared as modport, VCS croaks
  10467. (let ((sig (assoc port (verilog-decls-get-interfaces moddecls))))
  10468. (not (and sig (verilog-sig-modport sig)))))
  10469. (concat "." vl-modport) "")
  10470. dflt-bits))
  10471. ;; Find template
  10472. (cond (tpl-ass ; Template of exact port name
  10473. (setq tpl-net (nth 1 tpl-ass)))
  10474. ((nth 1 tpl-list) ; Wildcards in template, search them
  10475. (let ((wildcards (nth 1 tpl-list)))
  10476. (while wildcards
  10477. (when (string-match (nth 0 (car wildcards)) port)
  10478. (setq tpl-ass (car wildcards) ; so allow @ parsing
  10479. tpl-net (replace-match (nth 1 (car wildcards))
  10480. t nil port)))
  10481. (setq wildcards (cdr wildcards))))))
  10482. ;; Parse Templated variable
  10483. (when tpl-ass
  10484. ;; Evaluate @"(lispcode)"
  10485. (when (string-match "@\".*[^\\]\"" tpl-net)
  10486. (while (string-match "@\"\\(\\([^\\\"]*\\(\\\\.\\)*\\)*\\)\"" tpl-net)
  10487. (setq tpl-net
  10488. (concat
  10489. (substring tpl-net 0 (match-beginning 0))
  10490. (save-match-data
  10491. (let* ((expr (match-string 1 tpl-net))
  10492. (value
  10493. (progn
  10494. (setq expr (verilog-string-replace-matches "\\\\\"" "\"" nil nil expr))
  10495. (setq expr (verilog-string-replace-matches "@" tpl-num nil nil expr))
  10496. (prin1 (eval (car (read-from-string expr)))
  10497. (lambda (_ch) ())))))
  10498. (if (numberp value) (setq value (number-to-string value)))
  10499. value))
  10500. (substring tpl-net (match-end 0))))))
  10501. ;; Replace @ and [] magic variables in final output
  10502. (setq tpl-net (verilog-string-replace-matches "@" tpl-num nil nil tpl-net))
  10503. (setq tpl-net (verilog-string-replace-matches "\\[\\]\\[\\]" dflt-bits nil nil tpl-net))
  10504. (setq tpl-net (verilog-string-replace-matches "\\[\\]" vl-bits nil nil tpl-net)))
  10505. ;; Insert it
  10506. (indent-to indent-pt)
  10507. (insert "." port)
  10508. (unless (and verilog-auto-inst-dot-name
  10509. (equal port tpl-net))
  10510. (indent-to verilog-auto-inst-column)
  10511. (insert "(" tpl-net ")"))
  10512. (insert ",")
  10513. (cond (tpl-ass
  10514. (verilog-read-auto-template-hit tpl-ass)
  10515. (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
  10516. verilog-auto-inst-column))
  10517. ;; verilog-insert requires the complete comment in one call - including the newline
  10518. (cond ((equal verilog-auto-inst-template-numbers `lhs)
  10519. (verilog-insert " // Templated"
  10520. " LHS: " (nth 0 tpl-ass)
  10521. "\n"))
  10522. (verilog-auto-inst-template-numbers
  10523. (verilog-insert " // Templated"
  10524. " T" (int-to-string (nth 2 tpl-ass))
  10525. " L" (int-to-string (nth 3 tpl-ass))
  10526. "\n"))
  10527. (t
  10528. (verilog-insert " // Templated\n"))))
  10529. (for-star
  10530. (indent-to (+ (if (< verilog-auto-inst-column 48) 24 16)
  10531. verilog-auto-inst-column))
  10532. (verilog-insert " // Implicit .*\n"))
  10533. (t
  10534. (insert "\n")))))
  10535. ;;(verilog-auto-inst-port (list "foo" "[5:0]") 10 (list (list "foo" "a@\"(% (+ @ 1) 4)\"a")) "3")
  10536. ;;(x "incom[@\"(+ (* 8 @) 7)\":@\"(* 8 @)\"]")
  10537. ;;(x ".out (outgo[@\"(concat (+ (* 8 @) 7) \\\":\\\" ( * 8 @))\"]));")
  10538. (defun verilog-auto-inst-port-list (sig-list indent-pt moddecls tpl-list tpl-num for-star par-values)
  10539. "For `verilog-auto-inst' print a list of ports using `verilog-auto-inst-port'."
  10540. (when verilog-auto-inst-sort
  10541. (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare)))
  10542. (mapc (lambda (port)
  10543. (verilog-auto-inst-port port indent-pt moddecls
  10544. tpl-list tpl-num for-star par-values))
  10545. sig-list))
  10546. (defun verilog-auto-inst-first ()
  10547. "Insert , etc before first ever port in this instant, as part of \\[verilog-auto-inst]."
  10548. ;; Do we need a trailing comma?
  10549. ;; There maybe an ifdef or something similar before us. What a mess. Thus
  10550. ;; to avoid trouble we only insert on preceding ) or *.
  10551. ;; Insert first port on new line
  10552. (insert "\n") ; Must insert before search, so point will move forward if insert comma
  10553. (save-excursion
  10554. (verilog-re-search-backward-quick "[^ \t\n\f]" nil nil)
  10555. (when (looking-at ")\\|\\*") ; Generally don't insert, unless we are fairly sure
  10556. (forward-char 1)
  10557. (insert ","))))
  10558. (defun verilog-auto-star ()
  10559. "Expand SystemVerilog .* pins, as part of \\[verilog-auto].
  10560. If `verilog-auto-star-expand' is set, .* pins are treated if they were
  10561. AUTOINST statements, otherwise they are ignored. For safety, Verilog mode
  10562. will also ignore any .* that are not last in your pin list (this prevents
  10563. it from deleting pins following the .* when it expands the AUTOINST.)
  10564. On writing your file, unless `verilog-auto-star-save' is set, any
  10565. non-templated expanded pins will be removed. You may do this at any time
  10566. with \\[verilog-delete-auto-star-implicit].
  10567. If you are converting a module to use .* for the first time, you may wish
  10568. to use \\[verilog-inject-auto] and then replace the created AUTOINST with .*.
  10569. See `verilog-auto-inst' for examples, templates, and more information."
  10570. (when (verilog-auto-star-safe)
  10571. (verilog-auto-inst)))
  10572. (defun verilog-auto-inst ()
  10573. "Expand AUTOINST statements, as part of \\[verilog-auto].
  10574. Replace the pin connections to an instantiation or interface
  10575. declaration with ones automatically derived from the module or
  10576. interface header of the instantiated item.
  10577. If `verilog-auto-star-expand' is set, also expand SystemVerilog .* ports,
  10578. and delete them before saving unless `verilog-auto-star-save' is set.
  10579. See `verilog-auto-star' for more information.
  10580. The pins are printed in declaration order or alphabetically,
  10581. based on the `verilog-auto-inst-sort' variable.
  10582. Limitations:
  10583. Module names must be resolvable to filenames by adding a
  10584. `verilog-library-extensions', and being found in the same directory, or
  10585. by changing the variable `verilog-library-flags' or
  10586. `verilog-library-directories'. Macros `modname are translated through the
  10587. vh-{name} Emacs variable, if that is not found, it just ignores the \\=`.
  10588. In templates you must have one signal per line, ending in a ), or ));,
  10589. and have proper () nesting, including a final ); to end the template.
  10590. Typedefs must match `verilog-typedef-regexp', which is disabled by default.
  10591. SystemVerilog multidimensional input/output has only experimental support.
  10592. SystemVerilog .name syntax is used if `verilog-auto-inst-dot-name' is set.
  10593. Parameters referenced by the instantiation will remain symbolic, unless
  10594. `verilog-auto-inst-param-value' is set.
  10595. Gate primitives (and/or) may have AUTOINST for the purpose of
  10596. AUTOWIRE declarations, etc. Gates are the only case when
  10597. position based connections are passed.
  10598. The array part of arrayed instances are ignored; this may
  10599. result in undesirable default AUTOINST connections; use a
  10600. template instead.
  10601. For example, first take the submodule InstModule.v:
  10602. module InstModule (o,i);
  10603. output [31:0] o;
  10604. input i;
  10605. wire [31:0] o = {32{i}};
  10606. endmodule
  10607. This is then used in an upper level module:
  10608. module ExampInst (o,i);
  10609. output o;
  10610. input i;
  10611. InstModule instName
  10612. (/*AUTOINST*/);
  10613. endmodule
  10614. Typing \\[verilog-auto] will make this into:
  10615. module ExampInst (o,i);
  10616. output o;
  10617. input i;
  10618. InstModule instName
  10619. (/*AUTOINST*/
  10620. // Outputs
  10621. .ov (ov[31:0]),
  10622. // Inputs
  10623. .i (i));
  10624. endmodule
  10625. Where the list of inputs and outputs came from the inst module.
  10626. Exceptions:
  10627. Unless you are instantiating a module multiple times, or the module is
  10628. something trivial like an adder, DO NOT CHANGE SIGNAL NAMES ACROSS HIERARCHY.
  10629. It just makes for unmaintainable code. To sanitize signal names, try
  10630. vrename from URL `http://www.veripool.org'.
  10631. When you need to violate this suggestion there are two ways to list
  10632. exceptions, placing them before the AUTOINST, or using templates.
  10633. Any ports defined before the /*AUTOINST*/ are not included in the list of
  10634. automatics. This is similar to making a template as described below, but
  10635. is restricted to simple connections just like you normally make. Also note
  10636. that any signals before the AUTOINST will only be picked up by AUTOWIRE if
  10637. you have the appropriate // Input or // Output comment, and exactly the
  10638. same line formatting as AUTOINST itself uses.
  10639. InstModule instName
  10640. (// Inputs
  10641. .i (my_i_dont_mess_with_it),
  10642. /*AUTOINST*/
  10643. // Outputs
  10644. .ov (ov[31:0]));
  10645. Templates:
  10646. For multiple instantiations based upon a single template, create a
  10647. commented out template:
  10648. /* InstModule AUTO_TEMPLATE (
  10649. .sig3 (sigz[]),
  10650. );
  10651. */
  10652. Templates go ABOVE the instantiation(s). When an instantiation is
  10653. expanded `verilog-mode' simply searches up for the closest template.
  10654. Thus you can have multiple templates for the same module, just alternate
  10655. between the template for an instantiation and the instantiation itself.
  10656. (For backward compatibility if no template is found above, it
  10657. will also look below, but do not use this behavior in new designs.)
  10658. The module name must be the same as the name of the module in the
  10659. instantiation name, and the code \"AUTO_TEMPLATE\" must be in these exact
  10660. words and capitalized. Only signals that must be different for each
  10661. instantiation need to be listed.
  10662. Inside a template, a [] in a connection name (with nothing else
  10663. inside the brackets) will be replaced by the same bus subscript
  10664. as it is being connected to, or the [] will be removed if it is
  10665. a single bit signal.
  10666. Inside a template, a [][] in a connection name will behave
  10667. similarly to a [] for scalar or single-dimensional connection;
  10668. for a multidimensional connection it will print a comment
  10669. similar to that printed when a template is not used. Generally
  10670. it is a good idea to do this for all connections in a template,
  10671. as then they will work for any width signal, and with AUTOWIRE.
  10672. See PTL_BUS becoming PTL_BUSNEW below.
  10673. Inside a template, a [] in a connection name (with nothing else inside
  10674. the brackets) will be replaced by the same bus subscript as it is being
  10675. connected to, or the [] will be removed if it is a single bit signal.
  10676. Generally it is a good idea to do this for all connections in a template,
  10677. as then they will work for any width signal, and with AUTOWIRE. See
  10678. PTL_BUS becoming PTL_BUSNEW below.
  10679. If you have a complicated template, set `verilog-auto-inst-template-numbers'
  10680. to see which regexps are matching. Don't leave that mode set after
  10681. debugging is completed though, it will result in lots of extra differences
  10682. and merge conflicts.
  10683. Setting `verilog-auto-template-warn-unused' will report errors
  10684. if any template lines are unused.
  10685. For example:
  10686. /* InstModule AUTO_TEMPLATE (
  10687. .ptl_bus (ptl_busnew[]),
  10688. );
  10689. */
  10690. InstModule ms2m (/*AUTOINST*/);
  10691. Typing \\[verilog-auto] will make this into:
  10692. InstModule ms2m (/*AUTOINST*/
  10693. // Outputs
  10694. .NotInTemplate (NotInTemplate),
  10695. .ptl_bus (ptl_busnew[3:0]), // Templated
  10696. ....
  10697. Multiple Module Templates:
  10698. The same template lines can be applied to multiple modules with
  10699. the syntax as follows:
  10700. /* InstModuleA AUTO_TEMPLATE
  10701. InstModuleB AUTO_TEMPLATE
  10702. InstModuleC AUTO_TEMPLATE
  10703. InstModuleD AUTO_TEMPLATE (
  10704. .ptl_bus (ptl_busnew[]),
  10705. );
  10706. */
  10707. Note there is only one AUTO_TEMPLATE opening parenthesis.
  10708. @ Templates:
  10709. It is common to instantiate a cell multiple times, so templates make it
  10710. trivial to substitute part of the cell name into the connection name.
  10711. /* InstName AUTO_TEMPLATE <optional \"REGEXP\"> (
  10712. .sig1 (sigx[@]),
  10713. .sig2 (sigy[@\"(% (+ 1 @) 4)\"]),
  10714. );
  10715. */
  10716. If no regular expression is provided immediately after the AUTO_TEMPLATE
  10717. keyword, then the @ character in any connection names will be replaced
  10718. with the instantiation number; the first digits found in the cell's
  10719. instantiation name.
  10720. If a regular expression is provided, the @ character will be replaced
  10721. with the first () grouping that matches against the cell name. Using a
  10722. regexp of `\\([0-9]+\\)' provides identical values for @ as when no
  10723. regexp is provided. If you use multiple layers of parenthesis,
  10724. `test\\([^0-9]+\\)_\\([0-9]+\\)' would replace @ with non-number
  10725. characters after test and before _, whereas
  10726. `\\(test\\([a-z]+\\)_\\([0-9]+\\)\\)' would replace @ with the entire
  10727. match.
  10728. For example:
  10729. /* InstModule AUTO_TEMPLATE (
  10730. .ptl_mapvalidx (ptl_mapvalid[@]),
  10731. .ptl_mapvalidp1x (ptl_mapvalid[@\"(% (+ 1 @) 4)\"]),
  10732. );
  10733. */
  10734. InstModule ms2m (/*AUTOINST*/);
  10735. Typing \\[verilog-auto] will make this into:
  10736. InstModule ms2m (/*AUTOINST*/
  10737. // Outputs
  10738. .ptl_mapvalidx (ptl_mapvalid[2]),
  10739. .ptl_mapvalidp1x (ptl_mapvalid[3]));
  10740. Note the @ character was replaced with the 2 from \"ms2m\".
  10741. Alternatively, using a regular expression for @:
  10742. /* InstModule AUTO_TEMPLATE \"_\\([a-z]+\\)\" (
  10743. .ptl_mapvalidx (@_ptl_mapvalid),
  10744. .ptl_mapvalidp1x (ptl_mapvalid_@),
  10745. );
  10746. */
  10747. InstModule ms2_FOO (/*AUTOINST*/);
  10748. InstModule ms2_BAR (/*AUTOINST*/);
  10749. Typing \\[verilog-auto] will make this into:
  10750. InstModule ms2_FOO (/*AUTOINST*/
  10751. // Outputs
  10752. .ptl_mapvalidx (FOO_ptl_mapvalid),
  10753. .ptl_mapvalidp1x (ptl_mapvalid_FOO));
  10754. InstModule ms2_BAR (/*AUTOINST*/
  10755. // Outputs
  10756. .ptl_mapvalidx (BAR_ptl_mapvalid),
  10757. .ptl_mapvalidp1x (ptl_mapvalid_BAR));
  10758. Regexp Templates:
  10759. A template entry of the form
  10760. .pci_req\\([0-9]+\\)_l (pci_req_jtag_[\\1]),
  10761. will apply an Emacs style regular expression search for any port beginning
  10762. in pci_req followed by numbers and ending in _l and connecting that to
  10763. the pci_req_jtag_[] net, with the bus subscript coming from what matches
  10764. inside the first set of \\( \\). Thus pci_req2_l becomes pci_req_jtag_[2].
  10765. Since \\([0-9]+\\) is so common and ugly to read, a @ in the port name
  10766. does the same thing. (Note a @ in the connection/replacement text is
  10767. completely different -- still use \\1 there!) Thus this is the same as
  10768. the above template:
  10769. .pci_req@_l (pci_req_jtag_[\\1]),
  10770. Here's another example to remove the _l, useful when naming conventions
  10771. specify _ alone to mean active low. Note the use of [] to keep the bus
  10772. subscript:
  10773. .\\(.*\\)_l (\\1_[]),
  10774. Lisp Templates:
  10775. First any regular expression template is expanded.
  10776. If the syntax @\"( ... )\" is found in a connection, the expression in
  10777. quotes will be evaluated as a Lisp expression, with @ replaced by the
  10778. instantiation number. The MAPVALIDP1X example above would put @+1 modulo
  10779. 4 into the brackets. Quote all double-quotes inside the expression with
  10780. a leading backslash (\\\"...\\\"); or if the Lisp template is also a
  10781. regexp template backslash the backslash quote (\\\\\"...\\\\\").
  10782. There are special variables defined that are useful in these
  10783. Lisp functions:
  10784. vl-name Name portion of the input/output port.
  10785. vl-bits Bus bits portion of the input/output port (`[2:0]').
  10786. vl-mbits Multidimensional array bits for port (`[2:0][3:0]').
  10787. vl-width Width of the input/output port (`3' for [2:0]).
  10788. May be a (...) expression if bits isn't a constant.
  10789. vl-dir Direction of the pin input/output/inout/interface.
  10790. vl-modport The modport, if an interface with a modport.
  10791. vl-cell-type Module name/type of the cell (`InstModule').
  10792. vl-cell-name Instance name of the cell (`instName').
  10793. Normal Lisp variables may be used in expressions. See
  10794. `verilog-read-defines' which can set vh-{definename} variables for use
  10795. here. Also, any comments of the form:
  10796. /*AUTO_LISP(setq foo 1)*/
  10797. will evaluate any Lisp expression inside the parenthesis between the
  10798. beginning of the buffer and the point of the AUTOINST. This allows
  10799. functions to be defined or variables to be changed between instantiations.
  10800. (See also `verilog-auto-insert-lisp' if you want the output from your
  10801. lisp function to be inserted.)
  10802. Note that when using lisp expressions errors may occur when @ is not a
  10803. number; you may need to use the standard Emacs Lisp functions
  10804. `number-to-string' and `string-to-number'.
  10805. After the evaluation is completed, @ substitution and [] substitution
  10806. occur.
  10807. For more information see the \\[verilog-faq] and forums at URL
  10808. `http://www.veripool.org'."
  10809. (save-excursion
  10810. ;; Find beginning
  10811. (let* ((pt (point))
  10812. (for-star (save-excursion (backward-char 2) (looking-at "\\.\\*")))
  10813. (indent-pt (save-excursion (verilog-backward-open-paren)
  10814. (1+ (current-column))))
  10815. (verilog-auto-inst-column (max verilog-auto-inst-column
  10816. (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
  10817. (modi (verilog-modi-current))
  10818. (moddecls (verilog-modi-get-decls modi))
  10819. submod submodi submoddecls
  10820. inst skip-pins tpl-list tpl-num did-first par-values)
  10821. ;; Find module name that is instantiated
  10822. (setq submod (verilog-read-inst-module)
  10823. inst (verilog-read-inst-name)
  10824. vl-cell-type submod
  10825. vl-cell-name inst
  10826. skip-pins (aref (verilog-read-inst-pins) 0))
  10827. ;; Parse any AUTO_LISP() before here
  10828. (verilog-read-auto-lisp (point-min) pt)
  10829. ;; Read parameters (after AUTO_LISP)
  10830. (setq par-values (and verilog-auto-inst-param-value
  10831. (verilog-read-inst-param-value)))
  10832. ;; Lookup position, etc of submodule
  10833. ;; Note this may raise an error
  10834. (when (and (not (member submod verilog-gate-keywords))
  10835. (setq submodi (verilog-modi-lookup submod t)))
  10836. (setq submoddecls (verilog-modi-get-decls submodi))
  10837. ;; If there's a number in the instantiation, it may be an argument to the
  10838. ;; automatic variable instantiation program.
  10839. (let* ((tpl-info (verilog-read-auto-template submod))
  10840. (tpl-regexp (aref tpl-info 0)))
  10841. (setq tpl-num (if (verilog-string-match-fold tpl-regexp inst)
  10842. (match-string 1 inst)
  10843. "")
  10844. tpl-list (aref tpl-info 1)))
  10845. ;; Find submodule's signals and dump
  10846. (let ((sig-list (and (equal (verilog-modi-get-type submodi) "interface")
  10847. (verilog-signals-not-in
  10848. (verilog-decls-get-vars submoddecls)
  10849. skip-pins)))
  10850. (vl-dir "interfaced"))
  10851. (when (and sig-list
  10852. verilog-auto-inst-interfaced-ports)
  10853. (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
  10854. ;; Note these are searched for in verilog-read-sub-decls.
  10855. (verilog-insert-indent "// Interfaced\n")
  10856. (verilog-auto-inst-port-list sig-list indent-pt moddecls
  10857. tpl-list tpl-num for-star par-values)))
  10858. (let ((sig-list (verilog-signals-not-in
  10859. (verilog-decls-get-interfaces submoddecls)
  10860. skip-pins))
  10861. (vl-dir "interface"))
  10862. (when sig-list
  10863. (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
  10864. ;; Note these are searched for in verilog-read-sub-decls.
  10865. (verilog-insert-indent "// Interfaces\n")
  10866. (verilog-auto-inst-port-list sig-list indent-pt moddecls
  10867. tpl-list tpl-num for-star par-values)))
  10868. (let ((sig-list (verilog-signals-not-in
  10869. (verilog-decls-get-outputs submoddecls)
  10870. skip-pins))
  10871. (vl-dir "output"))
  10872. (when sig-list
  10873. (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
  10874. (verilog-insert-indent "// Outputs\n")
  10875. (verilog-auto-inst-port-list sig-list indent-pt moddecls
  10876. tpl-list tpl-num for-star par-values)))
  10877. (let ((sig-list (verilog-signals-not-in
  10878. (verilog-decls-get-inouts submoddecls)
  10879. skip-pins))
  10880. (vl-dir "inout"))
  10881. (when sig-list
  10882. (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
  10883. (verilog-insert-indent "// Inouts\n")
  10884. (verilog-auto-inst-port-list sig-list indent-pt moddecls
  10885. tpl-list tpl-num for-star par-values)))
  10886. (let ((sig-list (verilog-signals-not-in
  10887. (verilog-decls-get-inputs submoddecls)
  10888. skip-pins))
  10889. (vl-dir "input"))
  10890. (when sig-list
  10891. (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
  10892. (verilog-insert-indent "// Inputs\n")
  10893. (verilog-auto-inst-port-list sig-list indent-pt moddecls
  10894. tpl-list tpl-num for-star par-values)))
  10895. ;; Kill extra semi
  10896. (save-excursion
  10897. (cond (did-first
  10898. (re-search-backward "," pt t)
  10899. (delete-char 1)
  10900. (insert ");")
  10901. (search-forward "\n") ; Added by inst-port
  10902. (delete-char -1)
  10903. (if (search-forward ")" nil t) ; From user, moved up a line
  10904. (delete-char -1))
  10905. (if (search-forward ";" nil t) ; Don't error if user had syntax error and forgot it
  10906. (delete-char -1)))))))))
  10907. (defun verilog-auto-inst-param ()
  10908. "Expand AUTOINSTPARAM statements, as part of \\[verilog-auto].
  10909. Replace the parameter connections to an instantiation with ones
  10910. automatically derived from the module header of the instantiated netlist.
  10911. See \\[verilog-auto-inst] for limitations, and templates to customize the
  10912. output.
  10913. For example, first take the submodule InstModule.v:
  10914. module InstModule (o,i);
  10915. parameter PAR;
  10916. endmodule
  10917. This is then used in an upper level module:
  10918. module ExampInst (o,i);
  10919. parameter PAR;
  10920. InstModule #(/*AUTOINSTPARAM*/)
  10921. instName (/*AUTOINST*/);
  10922. endmodule
  10923. Typing \\[verilog-auto] will make this into:
  10924. module ExampInst (o,i);
  10925. output o;
  10926. input i;
  10927. InstModule #(/*AUTOINSTPARAM*/
  10928. // Parameters
  10929. .PAR (PAR));
  10930. instName (/*AUTOINST*/);
  10931. endmodule
  10932. Where the list of parameter connections come from the inst module.
  10933. Templates:
  10934. You can customize the parameter connections using AUTO_TEMPLATEs,
  10935. just as you would with \\[verilog-auto-inst]."
  10936. (save-excursion
  10937. ;; Find beginning
  10938. (let* ((pt (point))
  10939. (indent-pt (save-excursion (verilog-backward-open-paren)
  10940. (1+ (current-column))))
  10941. (verilog-auto-inst-column (max verilog-auto-inst-column
  10942. (+ 16 (* 8 (/ (+ indent-pt 7) 8)))))
  10943. (modi (verilog-modi-current))
  10944. (moddecls (verilog-modi-get-decls modi))
  10945. submod submodi submoddecls
  10946. inst skip-pins tpl-list tpl-num did-first)
  10947. ;; Find module name that is instantiated
  10948. (setq submod (save-excursion
  10949. ;; Get to the point where AUTOINST normally is to read the module
  10950. (verilog-re-search-forward-quick "[(;]" nil nil)
  10951. (verilog-read-inst-module))
  10952. inst (save-excursion
  10953. ;; Get to the point where AUTOINST normally is to read the module
  10954. (verilog-re-search-forward-quick "[(;]" nil nil)
  10955. (verilog-read-inst-name))
  10956. vl-cell-type submod
  10957. vl-cell-name inst
  10958. skip-pins (aref (verilog-read-inst-pins) 0))
  10959. ;; Parse any AUTO_LISP() before here
  10960. (verilog-read-auto-lisp (point-min) pt)
  10961. ;; Lookup position, etc of submodule
  10962. ;; Note this may raise an error
  10963. (when (setq submodi (verilog-modi-lookup submod t))
  10964. (setq submoddecls (verilog-modi-get-decls submodi))
  10965. ;; If there's a number in the instantiation, it may be an argument to the
  10966. ;; automatic variable instantiation program.
  10967. (let* ((tpl-info (verilog-read-auto-template submod))
  10968. (tpl-regexp (aref tpl-info 0)))
  10969. (setq tpl-num (if (verilog-string-match-fold tpl-regexp inst)
  10970. (match-string 1 inst)
  10971. "")
  10972. tpl-list (aref tpl-info 1)))
  10973. ;; Find submodule's signals and dump
  10974. (let ((sig-list (verilog-signals-not-in
  10975. (verilog-decls-get-gparams submoddecls)
  10976. skip-pins))
  10977. (vl-dir "parameter"))
  10978. (when sig-list
  10979. (when (not did-first) (verilog-auto-inst-first) (setq did-first t))
  10980. ;; Note these are searched for in verilog-read-sub-decls.
  10981. (verilog-insert-indent "// Parameters\n")
  10982. (verilog-auto-inst-port-list sig-list indent-pt moddecls
  10983. tpl-list tpl-num nil nil)))
  10984. ;; Kill extra semi
  10985. (save-excursion
  10986. (cond (did-first
  10987. (re-search-backward "," pt t)
  10988. (delete-char 1)
  10989. (insert ")")
  10990. (search-forward "\n") ; Added by inst-port
  10991. (delete-char -1)
  10992. (if (search-forward ")" nil t) ; From user, moved up a line
  10993. (delete-char -1)))))))))
  10994. (defun verilog-auto-reg ()
  10995. "Expand AUTOREG statements, as part of \\[verilog-auto].
  10996. Make reg statements for any output that isn't already declared,
  10997. and isn't a wire output from a block. `verilog-auto-wire-type'
  10998. may be used to change the datatype of the declarations.
  10999. Limitations:
  11000. This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
  11001. This does NOT work on memories, declare those yourself.
  11002. An example:
  11003. module ExampReg (o,i);
  11004. output o;
  11005. input i;
  11006. /*AUTOREG*/
  11007. always o = i;
  11008. endmodule
  11009. Typing \\[verilog-auto] will make this into:
  11010. module ExampReg (o,i);
  11011. output o;
  11012. input i;
  11013. /*AUTOREG*/
  11014. // Beginning of automatic regs (for this module's undeclared outputs)
  11015. reg o;
  11016. // End of automatics
  11017. always o = i;
  11018. endmodule"
  11019. (save-excursion
  11020. ;; Point must be at insertion point.
  11021. (let* ((indent-pt (current-indentation))
  11022. (modi (verilog-modi-current))
  11023. (moddecls (verilog-modi-get-decls modi))
  11024. (modsubdecls (verilog-modi-get-sub-decls modi))
  11025. (sig-list (verilog-signals-not-in
  11026. (verilog-decls-get-outputs moddecls)
  11027. (append (verilog-signals-with ; ignore typed signals
  11028. 'verilog-sig-type
  11029. (verilog-decls-get-outputs moddecls))
  11030. (verilog-decls-get-vars moddecls)
  11031. (verilog-decls-get-assigns moddecls)
  11032. (verilog-decls-get-consts moddecls)
  11033. (verilog-decls-get-gparams moddecls)
  11034. (verilog-subdecls-get-interfaced modsubdecls)
  11035. (verilog-subdecls-get-outputs modsubdecls)
  11036. (verilog-subdecls-get-inouts modsubdecls)))))
  11037. (when sig-list
  11038. (verilog-forward-or-insert-line)
  11039. (verilog-insert-indent "// Beginning of automatic regs (for this module's undeclared outputs)\n")
  11040. (verilog-insert-definition modi sig-list "reg" indent-pt nil)
  11041. (verilog-insert-indent "// End of automatics\n")))))
  11042. (defun verilog-auto-reg-input ()
  11043. "Expand AUTOREGINPUT statements, as part of \\[verilog-auto].
  11044. Make reg statements instantiation inputs that aren't already declared.
  11045. This is useful for making a top level shell for testing the module that is
  11046. to be instantiated.
  11047. Limitations:
  11048. This ONLY detects inputs of AUTOINSTants (see `verilog-read-sub-decls').
  11049. This does NOT work on memories, declare those yourself.
  11050. An example (see `verilog-auto-inst' for what else is going on here):
  11051. module ExampRegInput (o,i);
  11052. output o;
  11053. input i;
  11054. /*AUTOREGINPUT*/
  11055. InstModule instName
  11056. (/*AUTOINST*/);
  11057. endmodule
  11058. Typing \\[verilog-auto] will make this into:
  11059. module ExampRegInput (o,i);
  11060. output o;
  11061. input i;
  11062. /*AUTOREGINPUT*/
  11063. // Beginning of automatic reg inputs (for undeclared ...
  11064. reg [31:0] iv; // From inst of inst.v
  11065. // End of automatics
  11066. InstModule instName
  11067. (/*AUTOINST*/
  11068. // Outputs
  11069. .o (o[31:0]),
  11070. // Inputs
  11071. .iv (iv));
  11072. endmodule"
  11073. (save-excursion
  11074. ;; Point must be at insertion point.
  11075. (let* ((indent-pt (current-indentation))
  11076. (modi (verilog-modi-current))
  11077. (moddecls (verilog-modi-get-decls modi))
  11078. (modsubdecls (verilog-modi-get-sub-decls modi))
  11079. (sig-list (verilog-signals-combine-bus
  11080. (verilog-signals-not-in
  11081. (append (verilog-subdecls-get-inputs modsubdecls)
  11082. (verilog-subdecls-get-inouts modsubdecls))
  11083. (append (verilog-decls-get-signals moddecls)
  11084. (verilog-decls-get-assigns moddecls))))))
  11085. (when sig-list
  11086. (verilog-forward-or-insert-line)
  11087. (verilog-insert-indent "// Beginning of automatic reg inputs (for undeclared instantiated-module inputs)\n")
  11088. (verilog-insert-definition modi sig-list "reg" indent-pt nil)
  11089. (verilog-insert-indent "// End of automatics\n")))))
  11090. (defun verilog-auto-logic-setup ()
  11091. "Prepare variables due to AUTOLOGIC."
  11092. (unless verilog-auto-wire-type
  11093. (set (make-local-variable 'verilog-auto-wire-type)
  11094. "logic")))
  11095. (defun verilog-auto-logic ()
  11096. "Expand AUTOLOGIC statements, as part of \\[verilog-auto].
  11097. Make wire statements using the SystemVerilog logic keyword.
  11098. This is currently equivalent to:
  11099. /*AUTOWIRE*/
  11100. with the below at the bottom of the file
  11101. // Local Variables:
  11102. // verilog-auto-logic-type:\"logic\"
  11103. // End:
  11104. In the future AUTOLOGIC may declare additional identifiers,
  11105. while AUTOWIRE will not."
  11106. (save-excursion
  11107. (verilog-auto-logic-setup)
  11108. (verilog-auto-wire)))
  11109. (defun verilog-auto-wire ()
  11110. "Expand AUTOWIRE statements, as part of \\[verilog-auto].
  11111. Make wire statements for instantiations outputs that aren't
  11112. already declared. `verilog-auto-wire-type' may be used to change
  11113. the datatype of the declarations.
  11114. Limitations:
  11115. This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls'),
  11116. and all buses must have widths, such as those from AUTOINST, or using []
  11117. in AUTO_TEMPLATEs.
  11118. This does NOT work on memories or SystemVerilog .name connections,
  11119. declare those yourself.
  11120. Verilog mode will add \"Couldn't Merge\" comments to signals it cannot
  11121. determine how to bus together. This occurs when you have ports with
  11122. non-numeric or non-sequential bus subscripts. If Verilog mode
  11123. mis-guessed, you'll have to declare them yourself.
  11124. An example (see `verilog-auto-inst' for what else is going on here):
  11125. module ExampWire (o,i);
  11126. output o;
  11127. input i;
  11128. /*AUTOWIRE*/
  11129. InstModule instName
  11130. (/*AUTOINST*/);
  11131. endmodule
  11132. Typing \\[verilog-auto] will make this into:
  11133. module ExampWire (o,i);
  11134. output o;
  11135. input i;
  11136. /*AUTOWIRE*/
  11137. // Beginning of automatic wires
  11138. wire [31:0] ov; // From inst of inst.v
  11139. // End of automatics
  11140. InstModule instName
  11141. (/*AUTOINST*/
  11142. // Outputs
  11143. .ov (ov[31:0]),
  11144. // Inputs
  11145. .i (i));
  11146. wire o = | ov;
  11147. endmodule"
  11148. (save-excursion
  11149. ;; Point must be at insertion point.
  11150. (let* ((indent-pt (current-indentation))
  11151. (modi (verilog-modi-current))
  11152. (moddecls (verilog-modi-get-decls modi))
  11153. (modsubdecls (verilog-modi-get-sub-decls modi))
  11154. (sig-list (verilog-signals-combine-bus
  11155. (verilog-signals-not-in
  11156. (append (verilog-subdecls-get-outputs modsubdecls)
  11157. (verilog-subdecls-get-inouts modsubdecls))
  11158. (verilog-decls-get-signals moddecls)))))
  11159. (when sig-list
  11160. (verilog-forward-or-insert-line)
  11161. (verilog-insert-indent "// Beginning of automatic wires (for undeclared instantiated-module outputs)\n")
  11162. (verilog-insert-definition modi sig-list "wire" indent-pt nil)
  11163. (verilog-insert-indent "// End of automatics\n")
  11164. ;; We used to optionally call verilog-pretty-declarations and
  11165. ;; verilog-pretty-expr here, but it's too slow on huge modules,
  11166. ;; plus makes everyone's module change. Finally those call
  11167. ;; syntax-ppss which is broken when change hooks are disabled.
  11168. ))))
  11169. (defun verilog-auto-output ()
  11170. "Expand AUTOOUTPUT statements, as part of \\[verilog-auto].
  11171. Make output statements for any output signal from an /*AUTOINST*/ that
  11172. isn't an input to another AUTOINST. This is useful for modules which
  11173. only instantiate other modules.
  11174. Limitations:
  11175. This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
  11176. If placed inside the parenthesis of a module declaration, it creates
  11177. Verilog 2001 style, else uses Verilog 1995 style.
  11178. If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
  11179. instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
  11180. Typedefs must match `verilog-typedef-regexp', which is disabled by default.
  11181. Types are added to declarations if an AUTOLOGIC or
  11182. `verilog-auto-wire-type' is set to logic.
  11183. Signals matching `verilog-auto-output-ignore-regexp' are not included.
  11184. An example (see `verilog-auto-inst' for what else is going on here):
  11185. module ExampOutput (ov,i);
  11186. input i;
  11187. /*AUTOOUTPUT*/
  11188. InstModule instName
  11189. (/*AUTOINST*/);
  11190. endmodule
  11191. Typing \\[verilog-auto] will make this into:
  11192. module ExampOutput (ov,i);
  11193. input i;
  11194. /*AUTOOUTPUT*/
  11195. // Beginning of automatic outputs (from unused autoinst outputs)
  11196. output [31:0] ov; // From inst of inst.v
  11197. // End of automatics
  11198. InstModule instName
  11199. (/*AUTOINST*/
  11200. // Outputs
  11201. .ov (ov[31:0]),
  11202. // Inputs
  11203. .i (i));
  11204. endmodule
  11205. You may also provide an optional regular expression, in which case only
  11206. signals matching the regular expression will be included. For example the
  11207. same expansion will result from only extracting outputs starting with ov:
  11208. /*AUTOOUTPUT(\"^ov\")*/"
  11209. (save-excursion
  11210. ;; Point must be at insertion point.
  11211. (let* ((indent-pt (current-indentation))
  11212. (params (verilog-read-auto-params 0 1))
  11213. (regexp (nth 0 params))
  11214. (v2k (verilog-in-paren-quick))
  11215. (modi (verilog-modi-current))
  11216. (moddecls (verilog-modi-get-decls modi))
  11217. (modsubdecls (verilog-modi-get-sub-decls modi))
  11218. (sig-list (verilog-signals-not-in
  11219. (verilog-subdecls-get-outputs modsubdecls)
  11220. (append (verilog-decls-get-outputs moddecls)
  11221. (verilog-decls-get-inouts moddecls)
  11222. (verilog-decls-get-inputs moddecls)
  11223. (verilog-subdecls-get-inputs modsubdecls)
  11224. (verilog-subdecls-get-inouts modsubdecls)))))
  11225. (when regexp
  11226. (setq sig-list (verilog-signals-matching-regexp
  11227. sig-list regexp)))
  11228. (setq sig-list (verilog-signals-not-matching-regexp
  11229. sig-list verilog-auto-output-ignore-regexp))
  11230. (verilog-forward-or-insert-line)
  11231. (when v2k (verilog-repair-open-comma))
  11232. (when sig-list
  11233. (verilog-insert-indent "// Beginning of automatic outputs (from unused autoinst outputs)\n")
  11234. (verilog-insert-definition modi sig-list "output" indent-pt v2k)
  11235. (verilog-insert-indent "// End of automatics\n"))
  11236. (when v2k (verilog-repair-close-comma)))))
  11237. (defun verilog-auto-output-every ()
  11238. "Expand AUTOOUTPUTEVERY statements, as part of \\[verilog-auto].
  11239. Make output statements for any signals that aren't primary inputs or
  11240. outputs already. This makes every signal in the design an output. This is
  11241. useful to get Synopsys to preserve every signal in the design, since it
  11242. won't optimize away the outputs.
  11243. An example:
  11244. module ExampOutputEvery (o,i,tempa,tempb);
  11245. output o;
  11246. input i;
  11247. /*AUTOOUTPUTEVERY*/
  11248. wire tempa = i;
  11249. wire tempb = tempa;
  11250. wire o = tempb;
  11251. endmodule
  11252. Typing \\[verilog-auto] will make this into:
  11253. module ExampOutputEvery (o,i,tempa,tempb);
  11254. output o;
  11255. input i;
  11256. /*AUTOOUTPUTEVERY*/
  11257. // Beginning of automatic outputs (every signal)
  11258. output tempb;
  11259. output tempa;
  11260. // End of automatics
  11261. wire tempa = i;
  11262. wire tempb = tempa;
  11263. wire o = tempb;
  11264. endmodule
  11265. You may also provide an optional regular expression, in which case only
  11266. signals matching the regular expression will be included. For example the
  11267. same expansion will result from only extracting outputs starting with ov:
  11268. /*AUTOOUTPUTEVERY(\"^ov\")*/"
  11269. (save-excursion
  11270. ;;Point must be at insertion point
  11271. (let* ((indent-pt (current-indentation))
  11272. (params (verilog-read-auto-params 0 1))
  11273. (regexp (nth 0 params))
  11274. (v2k (verilog-in-paren-quick))
  11275. (modi (verilog-modi-current))
  11276. (moddecls (verilog-modi-get-decls modi))
  11277. (sig-list (verilog-signals-combine-bus
  11278. (verilog-signals-not-in
  11279. (verilog-decls-get-signals moddecls)
  11280. (verilog-decls-get-ports moddecls)))))
  11281. (when regexp
  11282. (setq sig-list (verilog-signals-matching-regexp
  11283. sig-list regexp)))
  11284. (setq sig-list (verilog-signals-not-matching-regexp
  11285. sig-list verilog-auto-output-ignore-regexp))
  11286. (verilog-forward-or-insert-line)
  11287. (when v2k (verilog-repair-open-comma))
  11288. (when sig-list
  11289. (verilog-insert-indent "// Beginning of automatic outputs (every signal)\n")
  11290. (verilog-insert-definition modi sig-list "output" indent-pt v2k)
  11291. (verilog-insert-indent "// End of automatics\n"))
  11292. (when v2k (verilog-repair-close-comma)))))
  11293. (defun verilog-auto-input ()
  11294. "Expand AUTOINPUT statements, as part of \\[verilog-auto].
  11295. Make input statements for any input signal into an /*AUTOINST*/ that
  11296. isn't declared elsewhere inside the module. This is useful for modules which
  11297. only instantiate other modules.
  11298. Limitations:
  11299. This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
  11300. If placed inside the parenthesis of a module declaration, it creates
  11301. Verilog 2001 style, else uses Verilog 1995 style.
  11302. If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
  11303. instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
  11304. Typedefs must match `verilog-typedef-regexp', which is disabled by default.
  11305. Types are added to declarations if an AUTOLOGIC or
  11306. `verilog-auto-wire-type' is set to logic.
  11307. Signals matching `verilog-auto-input-ignore-regexp' are not included.
  11308. An example (see `verilog-auto-inst' for what else is going on here):
  11309. module ExampInput (ov,i);
  11310. output [31:0] ov;
  11311. /*AUTOINPUT*/
  11312. InstModule instName
  11313. (/*AUTOINST*/);
  11314. endmodule
  11315. Typing \\[verilog-auto] will make this into:
  11316. module ExampInput (ov,i);
  11317. output [31:0] ov;
  11318. /*AUTOINPUT*/
  11319. // Beginning of automatic inputs (from unused autoinst inputs)
  11320. input i; // From inst of inst.v
  11321. // End of automatics
  11322. InstModule instName
  11323. (/*AUTOINST*/
  11324. // Outputs
  11325. .ov (ov[31:0]),
  11326. // Inputs
  11327. .i (i));
  11328. endmodule
  11329. You may also provide an optional regular expression, in which case only
  11330. signals matching the regular expression will be included. For example the
  11331. same expansion will result from only extracting inputs starting with i:
  11332. /*AUTOINPUT(\"^i\")*/"
  11333. (save-excursion
  11334. (let* ((indent-pt (current-indentation))
  11335. (params (verilog-read-auto-params 0 1))
  11336. (regexp (nth 0 params))
  11337. (v2k (verilog-in-paren-quick))
  11338. (modi (verilog-modi-current))
  11339. (moddecls (verilog-modi-get-decls modi))
  11340. (modsubdecls (verilog-modi-get-sub-decls modi))
  11341. (sig-list (verilog-signals-not-in
  11342. (verilog-subdecls-get-inputs modsubdecls)
  11343. (append (verilog-decls-get-inputs moddecls)
  11344. (verilog-decls-get-inouts moddecls)
  11345. (verilog-decls-get-outputs moddecls)
  11346. (verilog-decls-get-vars moddecls)
  11347. (verilog-decls-get-consts moddecls)
  11348. (verilog-decls-get-gparams moddecls)
  11349. (verilog-subdecls-get-interfaced modsubdecls)
  11350. (verilog-subdecls-get-outputs modsubdecls)
  11351. (verilog-subdecls-get-inouts modsubdecls)))))
  11352. (when regexp
  11353. (setq sig-list (verilog-signals-matching-regexp
  11354. sig-list regexp)))
  11355. (setq sig-list (verilog-signals-not-matching-regexp
  11356. sig-list verilog-auto-input-ignore-regexp))
  11357. (verilog-forward-or-insert-line)
  11358. (when v2k (verilog-repair-open-comma))
  11359. (when sig-list
  11360. (verilog-insert-indent "// Beginning of automatic inputs (from unused autoinst inputs)\n")
  11361. (verilog-insert-definition modi sig-list "input" indent-pt v2k)
  11362. (verilog-insert-indent "// End of automatics\n"))
  11363. (when v2k (verilog-repair-close-comma)))))
  11364. (defun verilog-auto-inout ()
  11365. "Expand AUTOINOUT statements, as part of \\[verilog-auto].
  11366. Make inout statements for any inout signal in an /*AUTOINST*/ that
  11367. isn't declared elsewhere inside the module.
  11368. Limitations:
  11369. This ONLY detects outputs of AUTOINSTants (see `verilog-read-sub-decls').
  11370. If placed inside the parenthesis of a module declaration, it creates
  11371. Verilog 2001 style, else uses Verilog 1995 style.
  11372. If any concatenation, or bit-subscripts are missing in the AUTOINSTant's
  11373. instantiation, all bets are off. (For example due to an AUTO_TEMPLATE).
  11374. Typedefs must match `verilog-typedef-regexp', which is disabled by default.
  11375. Types are added to declarations if an AUTOLOGIC or
  11376. `verilog-auto-wire-type' is set to logic.
  11377. Signals matching `verilog-auto-inout-ignore-regexp' are not included.
  11378. An example (see `verilog-auto-inst' for what else is going on here):
  11379. module ExampInout (ov,i);
  11380. input i;
  11381. /*AUTOINOUT*/
  11382. InstModule instName
  11383. (/*AUTOINST*/);
  11384. endmodule
  11385. Typing \\[verilog-auto] will make this into:
  11386. module ExampInout (ov,i);
  11387. input i;
  11388. /*AUTOINOUT*/
  11389. // Beginning of automatic inouts (from unused autoinst inouts)
  11390. inout [31:0] ov; // From inst of inst.v
  11391. // End of automatics
  11392. InstModule instName
  11393. (/*AUTOINST*/
  11394. // Inouts
  11395. .ov (ov[31:0]),
  11396. // Inputs
  11397. .i (i));
  11398. endmodule
  11399. You may also provide an optional regular expression, in which case only
  11400. signals matching the regular expression will be included. For example the
  11401. same expansion will result from only extracting inouts starting with i:
  11402. /*AUTOINOUT(\"^i\")*/"
  11403. (save-excursion
  11404. ;; Point must be at insertion point.
  11405. (let* ((indent-pt (current-indentation))
  11406. (params (verilog-read-auto-params 0 1))
  11407. (regexp (nth 0 params))
  11408. (v2k (verilog-in-paren-quick))
  11409. (modi (verilog-modi-current))
  11410. (moddecls (verilog-modi-get-decls modi))
  11411. (modsubdecls (verilog-modi-get-sub-decls modi))
  11412. (sig-list (verilog-signals-not-in
  11413. (verilog-subdecls-get-inouts modsubdecls)
  11414. (append (verilog-decls-get-outputs moddecls)
  11415. (verilog-decls-get-inouts moddecls)
  11416. (verilog-decls-get-inputs moddecls)
  11417. (verilog-subdecls-get-inputs modsubdecls)
  11418. (verilog-subdecls-get-outputs modsubdecls)))))
  11419. (when regexp
  11420. (setq sig-list (verilog-signals-matching-regexp
  11421. sig-list regexp)))
  11422. (setq sig-list (verilog-signals-not-matching-regexp
  11423. sig-list verilog-auto-inout-ignore-regexp))
  11424. (verilog-forward-or-insert-line)
  11425. (when v2k (verilog-repair-open-comma))
  11426. (when sig-list
  11427. (verilog-insert-indent "// Beginning of automatic inouts (from unused autoinst inouts)\n")
  11428. (verilog-insert-definition modi sig-list "inout" indent-pt v2k)
  11429. (verilog-insert-indent "// End of automatics\n"))
  11430. (when v2k (verilog-repair-close-comma)))))
  11431. (defun verilog-auto-inout-module (&optional complement all-in)
  11432. "Expand AUTOINOUTMODULE statements, as part of \\[verilog-auto].
  11433. Take input/output/inout statements from the specified module and insert
  11434. into the current module. This is useful for making null templates and
  11435. shell modules which need to have identical I/O with another module.
  11436. Any I/O which are already defined in this module will not be redefined.
  11437. For the complement of this function, see `verilog-auto-inout-comp',
  11438. and to make monitors with all inputs, see `verilog-auto-inout-in'.
  11439. Limitations:
  11440. If placed inside the parenthesis of a module declaration, it creates
  11441. Verilog 2001 style, else uses Verilog 1995 style.
  11442. Concatenation and outputting partial buses is not supported.
  11443. Module names must be resolvable to filenames. See `verilog-auto-inst'.
  11444. Signals are not inserted in the same order as in the original module,
  11445. though they will appear to be in the same order to an AUTOINST
  11446. instantiating either module.
  11447. Signals declared as \"output reg\" or \"output wire\" etc will
  11448. lose the wire/reg declaration so that shell modules may
  11449. generate those outputs differently. However, \"output logic\"
  11450. is propagated.
  11451. An example:
  11452. module ExampShell (/*AUTOARG*/);
  11453. /*AUTOINOUTMODULE(\"ExampMain\")*/
  11454. endmodule
  11455. module ExampMain (i,o,io);
  11456. input i;
  11457. output o;
  11458. inout io;
  11459. endmodule
  11460. Typing \\[verilog-auto] will make this into:
  11461. module ExampShell (/*AUTOARG*/i,o,io);
  11462. /*AUTOINOUTMODULE(\"ExampMain\")*/
  11463. // Beginning of automatic in/out/inouts (from specific module)
  11464. output o;
  11465. inout io;
  11466. input i;
  11467. // End of automatics
  11468. endmodule
  11469. You may also provide an optional regular expression, in which case only
  11470. signals matching the regular expression will be included. For example the
  11471. same expansion will result from only extracting signals starting with i:
  11472. /*AUTOINOUTMODULE(\"ExampMain\",\"^i\")*/
  11473. You may also provide an optional third argument regular
  11474. expression, in which case only signals which have that pin
  11475. direction and data type matching that regular expression will be
  11476. included. This matches against everything before the signal name
  11477. in the declaration, for example against \"input\" (single
  11478. bit), \"output logic\" (direction and type) or
  11479. \"output [1:0]\" (direction and implicit type). You also
  11480. probably want to skip spaces in your regexp.
  11481. For example, the below will result in matching the output \"o\"
  11482. against the previous example's module:
  11483. /*AUTOINOUTMODULE(\"ExampMain\",\"\",\"^output.*\")*/
  11484. You may also provide an optional fourth argument regular
  11485. expression, which if not \"\" only signals which do NOT match
  11486. that expression are included."
  11487. ;; Beware spacing of quotes in above as can mess up Emacs indenter
  11488. (save-excursion
  11489. (let* ((params (verilog-read-auto-params 1 4))
  11490. (submod (nth 0 params))
  11491. (regexp (nth 1 params))
  11492. (direction-re (nth 2 params))
  11493. (not-re (nth 3 params))
  11494. submodi)
  11495. ;; Lookup position, etc of co-module
  11496. ;; Note this may raise an error
  11497. (when (setq submodi (verilog-modi-lookup submod t))
  11498. (let* ((indent-pt (current-indentation))
  11499. (v2k (verilog-in-paren-quick))
  11500. (modi (verilog-modi-current))
  11501. (moddecls (verilog-modi-get-decls modi))
  11502. (submoddecls (verilog-modi-get-decls submodi))
  11503. (sig-list-i (verilog-signals-not-in
  11504. (cond (all-in
  11505. (append
  11506. (verilog-decls-get-inputs submoddecls)
  11507. (verilog-decls-get-inouts submoddecls)
  11508. (verilog-decls-get-outputs submoddecls)))
  11509. (complement
  11510. (verilog-decls-get-outputs submoddecls))
  11511. (t (verilog-decls-get-inputs submoddecls)))
  11512. (append (verilog-decls-get-inputs moddecls))))
  11513. (sig-list-o (verilog-signals-not-in
  11514. (cond (all-in nil)
  11515. (complement
  11516. (verilog-decls-get-inputs submoddecls))
  11517. (t (verilog-decls-get-outputs submoddecls)))
  11518. (append (verilog-decls-get-outputs moddecls))))
  11519. (sig-list-io (verilog-signals-not-in
  11520. (cond (all-in nil)
  11521. (t (verilog-decls-get-inouts submoddecls)))
  11522. (append (verilog-decls-get-inouts moddecls))))
  11523. (sig-list-if (verilog-signals-not-in
  11524. (verilog-decls-get-interfaces submoddecls)
  11525. (append (verilog-decls-get-interfaces moddecls)))))
  11526. (forward-line 1)
  11527. (setq sig-list-i (verilog-signals-edit-wire-reg
  11528. (verilog-signals-not-matching-regexp
  11529. (verilog-signals-matching-dir-re
  11530. (verilog-signals-matching-regexp sig-list-i regexp)
  11531. "input" direction-re) not-re))
  11532. sig-list-o (verilog-signals-edit-wire-reg
  11533. (verilog-signals-not-matching-regexp
  11534. (verilog-signals-matching-dir-re
  11535. (verilog-signals-matching-regexp sig-list-o regexp)
  11536. "output" direction-re) not-re))
  11537. sig-list-io (verilog-signals-edit-wire-reg
  11538. (verilog-signals-not-matching-regexp
  11539. (verilog-signals-matching-dir-re
  11540. (verilog-signals-matching-regexp sig-list-io regexp)
  11541. "inout" direction-re) not-re))
  11542. sig-list-if (verilog-signals-not-matching-regexp
  11543. (verilog-signals-matching-dir-re
  11544. (verilog-signals-matching-regexp sig-list-if regexp)
  11545. "interface" direction-re) not-re))
  11546. (when v2k (verilog-repair-open-comma))
  11547. (when (or sig-list-i sig-list-o sig-list-io sig-list-if)
  11548. (verilog-insert-indent "// Beginning of automatic in/out/inouts (from specific module)\n")
  11549. ;; Don't sort them so an upper AUTOINST will match the main module
  11550. (verilog-insert-definition modi sig-list-o "output" indent-pt v2k t)
  11551. (verilog-insert-definition modi sig-list-io "inout" indent-pt v2k t)
  11552. (verilog-insert-definition modi sig-list-i "input" indent-pt v2k t)
  11553. (verilog-insert-definition modi sig-list-if "interface" indent-pt v2k t)
  11554. (verilog-insert-indent "// End of automatics\n"))
  11555. (when v2k (verilog-repair-close-comma)))))))
  11556. (defun verilog-auto-inout-comp ()
  11557. "Expand AUTOINOUTCOMP statements, as part of \\[verilog-auto].
  11558. Take input/output/inout statements from the specified module and
  11559. insert the inverse into the current module (inputs become outputs
  11560. and vice-versa.) This is useful for making test and stimulus
  11561. modules which need to have complementing I/O with another module.
  11562. Any I/O which are already defined in this module will not be
  11563. redefined. For the complement of this function, see
  11564. `verilog-auto-inout-module'.
  11565. Limitations:
  11566. If placed inside the parenthesis of a module declaration, it creates
  11567. Verilog 2001 style, else uses Verilog 1995 style.
  11568. Concatenation and outputting partial buses is not supported.
  11569. Module names must be resolvable to filenames. See `verilog-auto-inst'.
  11570. Signals are not inserted in the same order as in the original module,
  11571. though they will appear to be in the same order to an AUTOINST
  11572. instantiating either module.
  11573. An example:
  11574. module ExampShell (/*AUTOARG*/);
  11575. /*AUTOINOUTCOMP(\"ExampMain\")*/
  11576. endmodule
  11577. module ExampMain (i,o,io);
  11578. input i;
  11579. output o;
  11580. inout io;
  11581. endmodule
  11582. Typing \\[verilog-auto] will make this into:
  11583. module ExampShell (/*AUTOARG*/i,o,io);
  11584. /*AUTOINOUTCOMP(\"ExampMain\")*/
  11585. // Beginning of automatic in/out/inouts (from specific module)
  11586. output i;
  11587. inout io;
  11588. input o;
  11589. // End of automatics
  11590. endmodule
  11591. You may also provide an optional regular expression, in which case only
  11592. signals matching the regular expression will be included. For example the
  11593. same expansion will result from only extracting signals starting with i:
  11594. /*AUTOINOUTCOMP(\"ExampMain\",\"^i\")*/
  11595. You may also provide an optional third argument regular
  11596. expression, in which case only signals which have that pin
  11597. direction and data type matching that regular expression will be
  11598. included. This matches against everything before the signal name
  11599. in the declaration, for example against \"input\" (single
  11600. bit), \"output logic\" (direction and type)
  11601. or \"output [1:0]\" (direction and implicit type). You also
  11602. probably want to skip spaces in your regexp.
  11603. For example, the below will result in matching the output \"o\"
  11604. against the previous example's module:
  11605. /*AUTOINOUTCOMP(\"ExampMain\",\"\",\"^output.*\")*/
  11606. You may also provide an optional fourth argument regular
  11607. expression, which if not \"\" only signals which do NOT match
  11608. that expression are included."
  11609. ;; Beware spacing of quotes in above as can mess up Emacs indenter
  11610. (verilog-auto-inout-module t nil))
  11611. (defun verilog-auto-inout-in ()
  11612. "Expand AUTOINOUTIN statements, as part of \\[verilog-auto].
  11613. Take input/output/inout statements from the specified module and
  11614. insert them as all inputs into the current module. This is
  11615. useful for making monitor modules which need to see all signals
  11616. as inputs based on another module. Any I/O which are already
  11617. defined in this module will not be redefined. See also
  11618. `verilog-auto-inout-module'.
  11619. Limitations:
  11620. If placed inside the parenthesis of a module declaration, it creates
  11621. Verilog 2001 style, else uses Verilog 1995 style.
  11622. Concatenation and outputting partial buses is not supported.
  11623. Module names must be resolvable to filenames. See `verilog-auto-inst'.
  11624. Signals are not inserted in the same order as in the original module,
  11625. though they will appear to be in the same order to an AUTOINST
  11626. instantiating either module.
  11627. An example:
  11628. module ExampShell (/*AUTOARG*/);
  11629. /*AUTOINOUTIN(\"ExampMain\")*/
  11630. endmodule
  11631. module ExampMain (i,o,io);
  11632. input i;
  11633. output o;
  11634. inout io;
  11635. endmodule
  11636. Typing \\[verilog-auto] will make this into:
  11637. module ExampShell (/*AUTOARG*/i,o,io);
  11638. /*AUTOINOUTIN(\"ExampMain\")*/
  11639. // Beginning of automatic in/out/inouts (from specific module)
  11640. input i;
  11641. input io;
  11642. input o;
  11643. // End of automatics
  11644. endmodule
  11645. You may also provide an optional regular expression, in which case only
  11646. signals matching the regular expression will be included. For example the
  11647. same expansion will result from only extracting signals starting with i:
  11648. /*AUTOINOUTIN(\"ExampMain\",\"^i\")*/"
  11649. (verilog-auto-inout-module nil t))
  11650. (defun verilog-auto-inout-param ()
  11651. "Expand AUTOINOUTPARAM statements, as part of \\[verilog-auto].
  11652. Take input/output/inout statements from the specified module and insert
  11653. into the current module. This is useful for making null templates and
  11654. shell modules which need to have identical I/O with another module.
  11655. Any I/O which are already defined in this module will not be redefined.
  11656. For the complement of this function, see `verilog-auto-inout-comp',
  11657. and to make monitors with all inputs, see `verilog-auto-inout-in'.
  11658. Limitations:
  11659. If placed inside the parenthesis of a module declaration, it creates
  11660. Verilog 2001 style, else uses Verilog 1995 style.
  11661. Module names must be resolvable to filenames. See `verilog-auto-inst'.
  11662. Parameters are inserted in the same order as in the original module.
  11663. Parameters do not have values, which is SystemVerilog 2009 syntax.
  11664. An example:
  11665. module ExampShell ();
  11666. /*AUTOINOUTPARAM(\"ExampMain\")*/
  11667. endmodule
  11668. module ExampMain ();
  11669. parameter PARAM = 22;
  11670. endmodule
  11671. Typing \\[verilog-auto] will make this into:
  11672. module ExampShell (/*AUTOARG*/i,o,io);
  11673. /*AUTOINOUTPARAM(\"ExampMain\")*/
  11674. // Beginning of automatic parameters (from specific module)
  11675. parameter PARAM;
  11676. // End of automatics
  11677. endmodule
  11678. You may also provide an optional regular expression, in which case only
  11679. parameters matching the regular expression will be included. For example the
  11680. same expansion will result from only extracting parameters starting with i:
  11681. /*AUTOINOUTPARAM(\"ExampMain\",\"^i\")*/"
  11682. (save-excursion
  11683. (let* ((params (verilog-read-auto-params 1 2))
  11684. (submod (nth 0 params))
  11685. (regexp (nth 1 params))
  11686. submodi)
  11687. ;; Lookup position, etc of co-module
  11688. ;; Note this may raise an error
  11689. (when (setq submodi (verilog-modi-lookup submod t))
  11690. (let* ((indent-pt (current-indentation))
  11691. (v2k (verilog-in-paren-quick))
  11692. (modi (verilog-modi-current))
  11693. (moddecls (verilog-modi-get-decls modi))
  11694. (submoddecls (verilog-modi-get-decls submodi))
  11695. (sig-list-p (verilog-signals-not-in
  11696. (verilog-decls-get-gparams submoddecls)
  11697. (append (verilog-decls-get-gparams moddecls)))))
  11698. (forward-line 1)
  11699. (setq sig-list-p (verilog-signals-matching-regexp sig-list-p regexp))
  11700. (when v2k (verilog-repair-open-comma))
  11701. (when sig-list-p
  11702. (verilog-insert-indent "// Beginning of automatic parameters (from specific module)\n")
  11703. ;; Don't sort them so an upper AUTOINST will match the main module
  11704. (verilog-insert-definition modi sig-list-p "parameter" indent-pt v2k t)
  11705. (verilog-insert-indent "// End of automatics\n"))
  11706. (when v2k (verilog-repair-close-comma)))))))
  11707. (defun verilog-auto-inout-modport ()
  11708. "Expand AUTOINOUTMODPORT statements, as part of \\[verilog-auto].
  11709. Take input/output/inout statements from the specified interface
  11710. and modport and insert into the current module. This is useful
  11711. for making verification modules that connect to UVM interfaces.
  11712. The first parameter is the name of an interface.
  11713. The second parameter is a regexp of modports to read from in
  11714. that interface.
  11715. The optional third parameter is a regular expression, and only
  11716. signals matching the regular expression will be included.
  11717. Limitations:
  11718. If placed inside the parenthesis of a module declaration, it creates
  11719. Verilog 2001 style, else uses Verilog 1995 style.
  11720. Interface names must be resolvable to filenames. See `verilog-auto-inst'.
  11721. As with other autos, any inputs/outputs declared in the module
  11722. will suppress the AUTO from redeclaring an inputs/outputs by
  11723. the same name.
  11724. An example:
  11725. interface ExampIf
  11726. ( input logic clk );
  11727. logic req_val;
  11728. logic [7:0] req_dat;
  11729. clocking mon_clkblk @(posedge clk);
  11730. input req_val;
  11731. input req_dat;
  11732. endclocking
  11733. modport mp(clocking mon_clkblk);
  11734. endinterface
  11735. module ExampMain
  11736. ( input clk,
  11737. /*AUTOINOUTMODPORT(\"ExampIf\" \"mp\")*/
  11738. // Beginning of automatic in/out/inouts (from modport)
  11739. input [7:0] req_dat,
  11740. input req_val
  11741. // End of automatics
  11742. );
  11743. /*AUTOASSIGNMODPORT(\"ExampIf\" \"mp\")*/
  11744. endmodule
  11745. Typing \\[verilog-auto] will make this into:
  11746. ...
  11747. module ExampMain
  11748. ( input clk,
  11749. /*AUTOINOUTMODPORT(\"ExampIf\" \"mp\")*/
  11750. // Beginning of automatic in/out/inouts (from modport)
  11751. input req_dat,
  11752. input req_val
  11753. // End of automatics
  11754. );
  11755. If the modport is part of a UVM monitor/driver class, this
  11756. creates a wrapper module that may be used to instantiate the
  11757. driver/monitor using AUTOINST in the testbench."
  11758. (save-excursion
  11759. (let* ((params (verilog-read-auto-params 2 3))
  11760. (submod (nth 0 params))
  11761. (modport-re (nth 1 params))
  11762. (regexp (nth 2 params))
  11763. direction-re submodi) ; direction argument not supported until requested
  11764. ;; Lookup position, etc of co-module
  11765. ;; Note this may raise an error
  11766. (when (setq submodi (verilog-modi-lookup submod t))
  11767. (let* ((indent-pt (current-indentation))
  11768. (v2k (verilog-in-paren-quick))
  11769. (modi (verilog-modi-current))
  11770. (moddecls (verilog-modi-get-decls modi))
  11771. (submoddecls (verilog-modi-get-decls submodi))
  11772. (submodportdecls (verilog-modi-modport-lookup submodi modport-re))
  11773. (sig-list-i (verilog-signals-in ; Decls doesn't have data types, must resolve
  11774. (verilog-decls-get-vars submoddecls)
  11775. (verilog-signals-not-in
  11776. (verilog-decls-get-inputs submodportdecls)
  11777. (append (verilog-decls-get-ports submoddecls)
  11778. (verilog-decls-get-ports moddecls)))))
  11779. (sig-list-o (verilog-signals-in ; Decls doesn't have data types, must resolve
  11780. (verilog-decls-get-vars submoddecls)
  11781. (verilog-signals-not-in
  11782. (verilog-decls-get-outputs submodportdecls)
  11783. (append (verilog-decls-get-ports submoddecls)
  11784. (verilog-decls-get-ports moddecls)))))
  11785. (sig-list-io (verilog-signals-in ; Decls doesn't have data types, must resolve
  11786. (verilog-decls-get-vars submoddecls)
  11787. (verilog-signals-not-in
  11788. (verilog-decls-get-inouts submodportdecls)
  11789. (append (verilog-decls-get-ports submoddecls)
  11790. (verilog-decls-get-ports moddecls))))))
  11791. (forward-line 1)
  11792. (setq sig-list-i (verilog-signals-edit-wire-reg
  11793. (verilog-signals-matching-dir-re
  11794. (verilog-signals-matching-regexp sig-list-i regexp)
  11795. "input" direction-re))
  11796. sig-list-o (verilog-signals-edit-wire-reg
  11797. (verilog-signals-matching-dir-re
  11798. (verilog-signals-matching-regexp sig-list-o regexp)
  11799. "output" direction-re))
  11800. sig-list-io (verilog-signals-edit-wire-reg
  11801. (verilog-signals-matching-dir-re
  11802. (verilog-signals-matching-regexp sig-list-io regexp)
  11803. "inout" direction-re)))
  11804. (when v2k (verilog-repair-open-comma))
  11805. (when (or sig-list-i sig-list-o sig-list-io)
  11806. (verilog-insert-indent "// Beginning of automatic in/out/inouts (from modport)\n")
  11807. ;; Don't sort them so an upper AUTOINST will match the main module
  11808. (verilog-insert-definition modi sig-list-o "output" indent-pt v2k t)
  11809. (verilog-insert-definition modi sig-list-io "inout" indent-pt v2k t)
  11810. (verilog-insert-definition modi sig-list-i "input" indent-pt v2k t)
  11811. (verilog-insert-indent "// End of automatics\n"))
  11812. (when v2k (verilog-repair-close-comma)))))))
  11813. (defun verilog-auto-insert-lisp ()
  11814. "Expand AUTOINSERTLISP statements, as part of \\[verilog-auto].
  11815. The Lisp code provided is called before other AUTOS are expanded,
  11816. and the Lisp code generally will call `insert' to insert text
  11817. into the current file beginning on the line after the
  11818. AUTOINSERTLISP.
  11819. See also AUTOINSERTLAST and `verilog-auto-insert-last' which
  11820. executes after (as opposed to before) other AUTOs.
  11821. See also AUTO_LISP, which takes a Lisp expression and evaluates
  11822. it during `verilog-auto-inst' but does not insert any text.
  11823. An example:
  11824. module ExampInsertLisp;
  11825. /*AUTOINSERTLISP(my-verilog-insert-hello \"world\")*/
  11826. endmodule
  11827. // For this example we declare the function in the
  11828. // module's file itself. Often you'd define it instead
  11829. // in a site-start.el or init file.
  11830. /*
  11831. Local Variables:
  11832. eval:
  11833. (defun my-verilog-insert-hello (who)
  11834. (insert (concat \"initial $write(\\\"hello \" who \"\\\");\\n\")))
  11835. End:
  11836. */
  11837. Typing \\[verilog-auto] will call my-verilog-insert-hello and
  11838. expand the above into:
  11839. // Beginning of automatic insert lisp
  11840. initial $write(\"hello world\");
  11841. // End of automatics
  11842. You can also call an external program and insert the returned
  11843. text:
  11844. /*AUTOINSERTLISP(insert (shell-command-to-string \"echo //hello\"))*/
  11845. // Beginning of automatic insert lisp
  11846. //hello
  11847. // End of automatics"
  11848. (save-excursion
  11849. ;; Point is at end of /*AUTO...*/
  11850. (let* ((indent-pt (current-indentation))
  11851. (cmd-end-pt (save-excursion (search-backward ")")
  11852. (forward-char)
  11853. (point))) ; Closing paren
  11854. (cmd-beg-pt (save-excursion (goto-char cmd-end-pt)
  11855. (backward-sexp 1) ; Inside comment
  11856. (point))) ; Beginning paren
  11857. (cmd (buffer-substring-no-properties cmd-beg-pt cmd-end-pt)))
  11858. (verilog-forward-or-insert-line)
  11859. ;; Some commands don't move point (like insert-file) so we always
  11860. ;; add the begin/end comments, then delete it if not needed
  11861. (verilog-insert-indent "// Beginning of automatic insert lisp\n")
  11862. (verilog-insert-indent "// End of automatics\n")
  11863. (forward-line -1)
  11864. (eval (read cmd))
  11865. (forward-line -1)
  11866. (setq verilog-scan-cache-tick nil) ; Clear cache; inserted unknown text
  11867. (verilog-delete-empty-auto-pair))))
  11868. (defun verilog-auto-insert-last ()
  11869. "Expand AUTOINSERTLAST statements, as part of \\[verilog-auto].
  11870. The Lisp code provided is called after all other AUTOS have been
  11871. expanded, and the Lisp code generally will call `insert' to
  11872. insert text into the current file beginning on the line after the
  11873. AUTOINSERTLAST.
  11874. Other than when called (after AUTOs are expanded), the functionality
  11875. is otherwise identical to AUTOINSERTLISP and `verilog-auto-insert-lisp' which
  11876. executes before (as opposed to after) other AUTOs.
  11877. See `verilog-auto-insert-lisp' for examples."
  11878. (verilog-auto-insert-lisp))
  11879. (defun verilog-auto-sense-sigs (moddecls presense-sigs)
  11880. "Return list of signals for current AUTOSENSE block."
  11881. (let* ((sigss (save-excursion
  11882. (search-forward ")")
  11883. (verilog-read-always-signals)))
  11884. (sig-list (verilog-signals-not-params
  11885. (verilog-signals-not-in (verilog-alw-get-inputs sigss)
  11886. (append (and (not verilog-auto-sense-include-inputs)
  11887. (verilog-alw-get-outputs-delayed sigss))
  11888. (and (not verilog-auto-sense-include-inputs)
  11889. (verilog-alw-get-outputs-immediate sigss))
  11890. (verilog-alw-get-temps sigss)
  11891. (verilog-decls-get-consts moddecls)
  11892. (verilog-decls-get-gparams moddecls)
  11893. presense-sigs)))))
  11894. sig-list))
  11895. (defun verilog-auto-sense ()
  11896. "Expand AUTOSENSE statements, as part of \\[verilog-auto].
  11897. Replace the always (/*AUTOSENSE*/) sensitivity list (/*AS*/ for short)
  11898. with one automatically derived from all inputs declared in the always
  11899. statement. Signals that are generated within the same always block are NOT
  11900. placed into the sensitivity list (see `verilog-auto-sense-include-inputs').
  11901. Long lines are split based on the `fill-column', see \\[set-fill-column].
  11902. Limitations:
  11903. Verilog does not allow memories (multidimensional arrays) in sensitivity
  11904. lists. AUTOSENSE will thus exclude them, and add a /*memory or*/ comment.
  11905. Constant signals:
  11906. AUTOSENSE cannot always determine if a \\=`define is a constant or a signal
  11907. (it could be in an include file for example). If a \\=`define or other signal
  11908. is put into the AUTOSENSE list and is not desired, use the AUTO_CONSTANT
  11909. declaration anywhere in the module (parenthesis are required):
  11910. /* AUTO_CONSTANT ( \\=`this_is_really_constant_dont_autosense_it ) */
  11911. Better yet, use a parameter, which will be understood to be constant
  11912. automatically.
  11913. OOps!
  11914. If AUTOSENSE makes a mistake, please report it. (First try putting
  11915. a begin/end after your always!) As a workaround, if a signal that
  11916. shouldn't be in the sensitivity list was, use the AUTO_CONSTANT above.
  11917. If a signal should be in the sensitivity list wasn't, placing it before
  11918. the /*AUTOSENSE*/ comment will prevent it from being deleted when the
  11919. autos are updated (or added if it occurs there already).
  11920. An example:
  11921. always @ (/*AS*/) begin
  11922. /* AUTO_CONSTANT (\\=`constant) */
  11923. outin = ina | inb | \\=`constant;
  11924. out = outin;
  11925. end
  11926. Typing \\[verilog-auto] will make this into:
  11927. always @ (/*AS*/ina or inb) begin
  11928. /* AUTO_CONSTANT (\\=`constant) */
  11929. outin = ina | inb | \\=`constant;
  11930. out = outin;
  11931. end
  11932. Note in Verilog 2001, you can often get the same result from the new @*
  11933. operator. (This was added to the language in part due to AUTOSENSE!)
  11934. always @* begin
  11935. outin = ina | inb | \\=`constant;
  11936. out = outin;
  11937. end"
  11938. (save-excursion
  11939. ;; Find beginning
  11940. (let* ((start-pt (save-excursion
  11941. (verilog-re-search-backward-quick "(" nil t)
  11942. (point)))
  11943. (indent-pt (save-excursion
  11944. (or (and (goto-char start-pt) (1+ (current-column)))
  11945. (current-indentation))))
  11946. (modi (verilog-modi-current))
  11947. (moddecls (verilog-modi-get-decls modi))
  11948. (sig-memories (verilog-signals-memory
  11949. (verilog-decls-get-vars moddecls)))
  11950. sig-list not-first presense-sigs)
  11951. ;; Read signals in always, eliminate outputs from sense list
  11952. (setq presense-sigs (verilog-signals-from-signame
  11953. (save-excursion
  11954. (verilog-read-signals start-pt (point)))))
  11955. (setq sig-list (verilog-auto-sense-sigs moddecls presense-sigs))
  11956. (when sig-memories
  11957. (let ((tlen (length sig-list)))
  11958. (setq sig-list (verilog-signals-not-in sig-list sig-memories))
  11959. (if (not (eq tlen (length sig-list))) (verilog-insert " /*memory or*/ "))))
  11960. (if (and presense-sigs ; Add a "or" if not "(.... or /*AUTOSENSE*/"
  11961. (save-excursion (goto-char (point))
  11962. (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
  11963. (verilog-re-search-backward-quick "\\s-" start-pt t)
  11964. (while (looking-at "\\s-`endif")
  11965. (verilog-re-search-backward-quick "[a-zA-Z0-9$_.%`]+" start-pt t)
  11966. (verilog-re-search-backward-quick "\\s-" start-pt t))
  11967. (not (looking-at "\\s-or\\b"))))
  11968. (setq not-first t))
  11969. (setq sig-list (sort sig-list `verilog-signals-sort-compare))
  11970. (while sig-list
  11971. (cond ((> (+ 4 (current-column) (length (verilog-sig-name (car sig-list)))) fill-column) ;+4 for width of or
  11972. (insert "\n")
  11973. (indent-to indent-pt)
  11974. (if not-first (insert "or ")))
  11975. (not-first (insert " or ")))
  11976. (insert (verilog-sig-name (car sig-list)))
  11977. (setq sig-list (cdr sig-list)
  11978. not-first t)))))
  11979. (defun verilog-auto-reset ()
  11980. "Expand AUTORESET statements, as part of \\[verilog-auto].
  11981. Replace the /*AUTORESET*/ comment with code to initialize all
  11982. registers set elsewhere in the always block.
  11983. Limitations:
  11984. AUTORESET will not clear memories.
  11985. AUTORESET uses <= if the signal has a <= assignment in the block,
  11986. else it uses =.
  11987. If <= is used, all = assigned variables are ignored if
  11988. `verilog-auto-reset-blocking-in-non' is nil; they are presumed
  11989. to be temporaries.
  11990. /*AUTORESET*/ presumes that any signals mentioned between the previous
  11991. begin/case/if statement and the AUTORESET comment are being reset manually
  11992. and should not be automatically reset. This includes omitting any signals
  11993. used on the right hand side of assignments.
  11994. By default, AUTORESET will include the width of the signal in the
  11995. autos, SystemVerilog designs may want to change this. To control
  11996. this behavior, see `verilog-auto-reset-widths'. In some cases
  11997. AUTORESET must use a \\='0 assignment and it will print NOWIDTH; use
  11998. `verilog-auto-reset-widths' unbased to prevent this.
  11999. AUTORESET ties signals to deasserted, which is presumed to be zero.
  12000. Signals that match `verilog-active-low-regexp' will be deasserted by tying
  12001. them to a one.
  12002. AUTORESET may try to reset arrays or structures that cannot be
  12003. reset by a simple assignment, resulting in compile errors. This
  12004. is a feature to be taken as a hint that you need to reset these
  12005. signals manually (or put them into a \"\\=`ifdef NEVER signal<=\\=`0;
  12006. \\=`endif\" so Verilog-Mode ignores them.)
  12007. An example:
  12008. always @(posedge clk or negedge reset_l) begin
  12009. if (!reset_l) begin
  12010. c <= 1;
  12011. /*AUTORESET*/
  12012. end
  12013. else begin
  12014. a <= in_a;
  12015. b <= in_b;
  12016. c <= in_c;
  12017. end
  12018. end
  12019. Typing \\[verilog-auto] will make this into:
  12020. always @(posedge core_clk or negedge reset_l) begin
  12021. if (!reset_l) begin
  12022. c <= 1;
  12023. /*AUTORESET*/
  12024. // Beginning of autoreset for uninitialized flops
  12025. a <= 0;
  12026. b = 0; // if `verilog-auto-reset-blocking-in-non' true
  12027. // End of automatics
  12028. end
  12029. else begin
  12030. a <= in_a;
  12031. b = in_b;
  12032. c <= in_c;
  12033. end
  12034. end"
  12035. (interactive)
  12036. (save-excursion
  12037. ;; Find beginning
  12038. (let* ((indent-pt (current-indentation))
  12039. (modi (verilog-modi-current))
  12040. (moddecls (verilog-modi-get-decls modi))
  12041. (all-list (verilog-decls-get-signals moddecls))
  12042. sigss sig-list dly-list prereset-sigs)
  12043. ;; Read signals in always, eliminate outputs from reset list
  12044. (setq prereset-sigs (verilog-signals-from-signame
  12045. (save-excursion
  12046. (verilog-read-signals
  12047. (save-excursion
  12048. (verilog-re-search-backward-quick
  12049. "\\(@\\|\\<\\(begin\\|if\\|case[xz]?\\|always\\(_latch\\|_ff\\|_comb\\)?\\)\\>\\)" nil t)
  12050. (point))
  12051. (point)))))
  12052. (save-excursion
  12053. (verilog-re-search-backward-quick "\\(@\\|\\<\\(always\\(_latch\\|_ff\\|_comb\\)?\\)\\>\\)" nil t)
  12054. (setq sigss (verilog-read-always-signals)))
  12055. (setq dly-list (verilog-alw-get-outputs-delayed sigss))
  12056. (setq sig-list (verilog-signals-not-in-struct
  12057. (append
  12058. (verilog-alw-get-outputs-delayed sigss)
  12059. (when (or (not (verilog-alw-get-uses-delayed sigss))
  12060. verilog-auto-reset-blocking-in-non)
  12061. (verilog-alw-get-outputs-immediate sigss)))
  12062. (append
  12063. (verilog-alw-get-temps sigss)
  12064. prereset-sigs)))
  12065. (setq sig-list (sort sig-list `verilog-signals-sort-compare))
  12066. (when sig-list
  12067. (insert "\n");
  12068. (verilog-insert-indent "// Beginning of autoreset for uninitialized flops\n");
  12069. (while sig-list
  12070. (let ((sig (or (assoc (verilog-sig-name (car sig-list)) all-list) ; As sig-list has no widths
  12071. (car sig-list))))
  12072. (indent-to indent-pt)
  12073. (insert (verilog-sig-name sig)
  12074. (if (assoc (verilog-sig-name sig) dly-list)
  12075. (concat " <= " verilog-assignment-delay)
  12076. " = ")
  12077. (verilog-sig-tieoff sig)
  12078. ";\n")
  12079. (setq sig-list (cdr sig-list))))
  12080. (verilog-insert-indent "// End of automatics")))))
  12081. (defun verilog-auto-tieoff ()
  12082. "Expand AUTOTIEOFF statements, as part of \\[verilog-auto].
  12083. Replace the /*AUTOTIEOFF*/ comment with code to wire-tie all unused output
  12084. signals to deasserted.
  12085. /*AUTOTIEOFF*/ is used to make stub modules; modules that have the same
  12086. input/output list as another module, but no internals. Specifically, it
  12087. finds all outputs in the module, and if that input is not otherwise declared
  12088. as a register or wire, creates a tieoff.
  12089. AUTORESET ties signals to deasserted, which is presumed to be zero.
  12090. Signals that match `verilog-active-low-regexp' will be deasserted by tying
  12091. them to a one.
  12092. You can add signals you do not want included in AUTOTIEOFF with
  12093. `verilog-auto-tieoff-ignore-regexp'.
  12094. `verilog-auto-wire-type' may be used to change the datatype of
  12095. the declarations.
  12096. `verilog-auto-reset-widths' may be used to change how the tieoff
  12097. value's width is generated.
  12098. An example of making a stub for another module:
  12099. module ExampStub (/*AUTOINST*/);
  12100. /*AUTOINOUTPARAM(\"Foo\")*/
  12101. /*AUTOINOUTMODULE(\"Foo\")*/
  12102. /*AUTOTIEOFF*/
  12103. // verilator lint_off UNUSED
  12104. wire _unused_ok = &{1\\='b0,
  12105. /*AUTOUNUSED*/
  12106. 1\\='b0};
  12107. // verilator lint_on UNUSED
  12108. endmodule
  12109. Typing \\[verilog-auto] will make this into:
  12110. module ExampStub (/*AUTOINST*/...);
  12111. /*AUTOINOUTPARAM(\"Foo\")*/
  12112. /*AUTOINOUTMODULE(\"Foo\")*/
  12113. // Beginning of autotieoff
  12114. output [2:0] foo;
  12115. // End of automatics
  12116. /*AUTOTIEOFF*/
  12117. // Beginning of autotieoff
  12118. wire [2:0] foo = 3\\='b0;
  12119. // End of automatics
  12120. ...
  12121. endmodule"
  12122. (interactive)
  12123. (save-excursion
  12124. ;; Find beginning
  12125. (let* ((indent-pt (current-indentation))
  12126. (modi (verilog-modi-current))
  12127. (moddecls (verilog-modi-get-decls modi))
  12128. (modsubdecls (verilog-modi-get-sub-decls modi))
  12129. (sig-list (verilog-signals-not-in
  12130. (verilog-decls-get-outputs moddecls)
  12131. (append (verilog-decls-get-vars moddecls)
  12132. (verilog-decls-get-assigns moddecls)
  12133. (verilog-decls-get-consts moddecls)
  12134. (verilog-decls-get-gparams moddecls)
  12135. (verilog-subdecls-get-interfaced modsubdecls)
  12136. (verilog-subdecls-get-outputs modsubdecls)
  12137. (verilog-subdecls-get-inouts modsubdecls)))))
  12138. (setq sig-list (verilog-signals-not-matching-regexp
  12139. sig-list verilog-auto-tieoff-ignore-regexp))
  12140. (when sig-list
  12141. (verilog-forward-or-insert-line)
  12142. (verilog-insert-indent "// Beginning of automatic tieoffs (for this module's unterminated outputs)\n")
  12143. (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
  12144. (verilog-modi-cache-add-vars modi sig-list) ; Before we trash list
  12145. (while sig-list
  12146. (let ((sig (car sig-list)))
  12147. (cond ((equal verilog-auto-tieoff-declaration "assign")
  12148. (indent-to indent-pt)
  12149. (insert "assign " (verilog-sig-name sig)))
  12150. (t
  12151. (verilog-insert-one-definition sig verilog-auto-tieoff-declaration indent-pt)))
  12152. (indent-to (max 48 (+ indent-pt 40)))
  12153. (insert "= " (verilog-sig-tieoff sig)
  12154. ";\n")
  12155. (setq sig-list (cdr sig-list))))
  12156. (verilog-insert-indent "// End of automatics\n")))))
  12157. (defun verilog-auto-undef ()
  12158. "Expand AUTOUNDEF statements, as part of \\[verilog-auto].
  12159. Take any \\=`defines since the last AUTOUNDEF in the current file
  12160. and create \\=`undefs for them. This is used to insure that
  12161. file-local defines do not pollute the global \\=`define name space.
  12162. Limitations:
  12163. AUTOUNDEF presumes any identifier following \\=`define is the
  12164. name of a define. Any \\=`ifdefs are ignored.
  12165. AUTOUNDEF suppresses creating an \\=`undef for any define that was
  12166. \\=`undefed before the AUTOUNDEF. This may be used to work around
  12167. the ignoring of \\=`ifdefs as shown below.
  12168. An example:
  12169. \\=`define XX_FOO
  12170. \\=`define M_BAR(x)
  12171. \\=`define M_BAZ
  12172. ...
  12173. \\=`ifdef NEVER
  12174. \\=`undef M_BAZ // Emacs will see this and not \\=`undef M_BAZ
  12175. \\=`endif
  12176. ...
  12177. /*AUTOUNDEF*/
  12178. Typing \\[verilog-auto] will make this into:
  12179. ...
  12180. /*AUTOUNDEF*/
  12181. // Beginning of automatic undefs
  12182. \\=`undef XX_FOO
  12183. \\=`undef M_BAR
  12184. // End of automatics
  12185. You may also provide an optional regular expression, in which case only
  12186. defines the regular expression will be undefed."
  12187. (save-excursion
  12188. (let* ((params (verilog-read-auto-params 0 1))
  12189. (regexp (nth 0 params))
  12190. (indent-pt (current-indentation))
  12191. (end-pt (point))
  12192. defs def)
  12193. (save-excursion
  12194. ;; Scan from start of file, or last AUTOUNDEF
  12195. (or (verilog-re-search-backward-quick "/\\*AUTOUNDEF\\>" end-pt t)
  12196. (goto-char (point-min)))
  12197. (while (verilog-re-search-forward-quick
  12198. "`\\(define\\|undef\\)\\s-*\\([a-zA-Z_][a-zA-Z_0-9]*\\)" end-pt t)
  12199. (cond ((equal (match-string-no-properties 1) "define")
  12200. (setq def (match-string-no-properties 2))
  12201. (when (and (or (not regexp)
  12202. (string-match regexp def))
  12203. (not (member def defs))) ; delete-dups not in 21.1
  12204. (setq defs (cons def defs))))
  12205. (t
  12206. (setq defs (delete (match-string-no-properties 2) defs))))))
  12207. ;; Insert
  12208. (setq defs (sort defs 'string<))
  12209. (when defs
  12210. (verilog-forward-or-insert-line)
  12211. (verilog-insert-indent "// Beginning of automatic undefs\n")
  12212. (while defs
  12213. (verilog-insert-indent "`undef " (car defs) "\n")
  12214. (setq defs (cdr defs)))
  12215. (verilog-insert-indent "// End of automatics\n")))))
  12216. (defun verilog-auto-unused ()
  12217. "Expand AUTOUNUSED statements, as part of \\[verilog-auto].
  12218. Replace the /*AUTOUNUSED*/ comment with a comma separated list of all unused
  12219. input and inout signals.
  12220. /*AUTOUNUSED*/ is used to make stub modules; modules that have the same
  12221. input/output list as another module, but no internals. Specifically, it
  12222. finds all inputs and inouts in the module, and if that input is not otherwise
  12223. used, adds it to a comma separated list.
  12224. The comma separated list is intended to be used to create a _unused_ok
  12225. signal. Using the exact name \"_unused_ok\" for name of the temporary
  12226. signal is recommended as it will insure maximum forward compatibility, it
  12227. also makes lint warnings easy to understand; ignore any unused warnings
  12228. with \"unused\" in the signal name.
  12229. To reduce simulation time, the _unused_ok signal should be forced to a
  12230. constant to prevent wiggling. The easiest thing to do is use a
  12231. reduction-and with 1\\='b0 as shown.
  12232. This way all unused signals are in one place, making it convenient to add
  12233. your tool's specific pragmas around the assignment to disable any unused
  12234. warnings.
  12235. You can add signals you do not want included in AUTOUNUSED with
  12236. `verilog-auto-unused-ignore-regexp'.
  12237. An example of making a stub for another module:
  12238. module ExampStub (/*AUTOINST*/);
  12239. /*AUTOINOUTPARAM(\"Examp\")*/
  12240. /*AUTOINOUTMODULE(\"Examp\")*/
  12241. /*AUTOTIEOFF*/
  12242. // verilator lint_off UNUSED
  12243. wire _unused_ok = &{1\\='b0,
  12244. /*AUTOUNUSED*/
  12245. 1\\='b0};
  12246. // verilator lint_on UNUSED
  12247. endmodule
  12248. Typing \\[verilog-auto] will make this into:
  12249. ...
  12250. // verilator lint_off UNUSED
  12251. wire _unused_ok = &{1\\='b0,
  12252. /*AUTOUNUSED*/
  12253. // Beginning of automatics
  12254. unused_input_a,
  12255. unused_input_b,
  12256. unused_input_c,
  12257. // End of automatics
  12258. 1\\='b0};
  12259. // verilator lint_on UNUSED
  12260. endmodule"
  12261. (interactive)
  12262. (save-excursion
  12263. ;; Find beginning
  12264. (let* ((indent-pt (progn (search-backward "/*") (current-column)))
  12265. (modi (verilog-modi-current))
  12266. (moddecls (verilog-modi-get-decls modi))
  12267. (modsubdecls (verilog-modi-get-sub-decls modi))
  12268. (sig-list (verilog-signals-not-in
  12269. (append (verilog-decls-get-inputs moddecls)
  12270. (verilog-decls-get-inouts moddecls))
  12271. (append (verilog-subdecls-get-inputs modsubdecls)
  12272. (verilog-subdecls-get-inouts modsubdecls)))))
  12273. (setq sig-list (verilog-signals-not-matching-regexp
  12274. sig-list verilog-auto-unused-ignore-regexp))
  12275. (when sig-list
  12276. (verilog-forward-or-insert-line)
  12277. (verilog-insert-indent "// Beginning of automatic unused inputs\n")
  12278. (setq sig-list (sort (copy-alist sig-list) `verilog-signals-sort-compare))
  12279. (while sig-list
  12280. (let ((sig (car sig-list)))
  12281. (indent-to indent-pt)
  12282. (insert (verilog-sig-name sig) ",\n")
  12283. (setq sig-list (cdr sig-list))))
  12284. (verilog-insert-indent "// End of automatics\n")))))
  12285. (defun verilog-enum-ascii (signm elim-regexp)
  12286. "Convert an enum name SIGNM to an ascii string for insertion.
  12287. Remove user provided prefix ELIM-REGEXP."
  12288. (or elim-regexp (setq elim-regexp "_ DONT MATCH IT_"))
  12289. (let ((case-fold-search t))
  12290. ;; All upper becomes all lower for readability
  12291. (downcase (verilog-string-replace-matches elim-regexp "" nil nil signm))))
  12292. (defun verilog-auto-ascii-enum ()
  12293. "Expand AUTOASCIIENUM statements, as part of \\[verilog-auto].
  12294. Create a register to contain the ASCII decode of an enumerated signal type.
  12295. This will allow trace viewers to show the ASCII name of states.
  12296. First, parameters are built into an enumeration using the synopsys enum
  12297. comment. The comment must be between the keyword and the symbol.
  12298. \(Annoying, but that's what Synopsys's dc_shell FSM reader requires.)
  12299. Next, registers which that enum applies to are also tagged with the same
  12300. enum.
  12301. Finally, an AUTOASCIIENUM command is used.
  12302. The first parameter is the name of the signal to be decoded.
  12303. The second parameter is the name to store the ASCII code into. For the
  12304. signal foo, I suggest the name _foo__ascii, where the leading _ indicates
  12305. a signal that is just for simulation, and the magic characters _ascii
  12306. tell viewers like Dinotrace to display in ASCII format.
  12307. The third optional parameter is a string which will be removed
  12308. from the state names. It defaults to \"\" which removes nothing.
  12309. The fourth optional parameter is \"onehot\" to force one-hot
  12310. decoding. If unspecified, if and only if the first parameter
  12311. width is 2^(number of states in enum) and does NOT match the
  12312. width of the enum, the signal is assumed to be a one-hot
  12313. decode. Otherwise, it's a normal encoded state vector.
  12314. `verilog-auto-wire-type' may be used to change the datatype of
  12315. the declarations.
  12316. \"auto enum\" may be used in place of \"synopsys enum\".
  12317. An example:
  12318. //== State enumeration
  12319. parameter [2:0] // synopsys enum state_info
  12320. SM_IDLE = 3\\='b000,
  12321. SM_SEND = 3\\='b001,
  12322. SM_WAIT1 = 3\\='b010;
  12323. //== State variables
  12324. reg [2:0] /* synopsys enum state_info */
  12325. state_r; /* synopsys state_vector state_r */
  12326. reg [2:0] /* synopsys enum state_info */
  12327. state_e1;
  12328. /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
  12329. Typing \\[verilog-auto] will make this into:
  12330. ... same front matter ...
  12331. /*AUTOASCIIENUM(\"state_r\", \"state_ascii_r\", \"SM_\")*/
  12332. // Beginning of automatic ASCII enum decoding
  12333. reg [39:0] state_ascii_r; // Decode of state_r
  12334. always @(state_r) begin
  12335. case ({state_r})
  12336. SM_IDLE: state_ascii_r = \"idle \";
  12337. SM_SEND: state_ascii_r = \"send \";
  12338. SM_WAIT1: state_ascii_r = \"wait1\";
  12339. default: state_ascii_r = \"%Erro\";
  12340. endcase
  12341. end
  12342. // End of automatics"
  12343. (save-excursion
  12344. (let* ((params (verilog-read-auto-params 2 4))
  12345. (undecode-name (nth 0 params))
  12346. (ascii-name (nth 1 params))
  12347. (elim-regexp (and (nth 2 params)
  12348. (not (equal (nth 2 params) ""))
  12349. (nth 2 params)))
  12350. (one-hot-flag (nth 3 params))
  12351. ;;
  12352. (indent-pt (current-indentation))
  12353. (modi (verilog-modi-current))
  12354. (moddecls (verilog-modi-get-decls modi))
  12355. ;;
  12356. (sig-list-consts (append (verilog-decls-get-consts moddecls)
  12357. (verilog-decls-get-gparams moddecls)))
  12358. (sig-list-all (verilog-decls-get-iovars moddecls))
  12359. ;;
  12360. (undecode-sig (or (assoc undecode-name sig-list-all)
  12361. (error "%s: Signal `%s' not found in design"
  12362. (verilog-point-text) undecode-name)))
  12363. (undecode-enum (or (verilog-sig-enum undecode-sig)
  12364. (error "%s: Signal `%s' does not have an enum tag"
  12365. (verilog-point-text) undecode-name)))
  12366. ;;
  12367. (enum-sigs (verilog-signals-not-in
  12368. (or (verilog-signals-matching-enum sig-list-consts undecode-enum)
  12369. (error "%s: No state definitions for `%s'"
  12370. (verilog-point-text) undecode-enum))
  12371. nil))
  12372. ;;
  12373. (one-hot (or
  12374. (string-match "onehot" (or one-hot-flag ""))
  12375. (and ; width(enum) != width(sig)
  12376. (or (not (verilog-sig-bits (car enum-sigs)))
  12377. (not (equal (verilog-sig-width (car enum-sigs))
  12378. (verilog-sig-width undecode-sig))))
  12379. ;; count(enums) == width(sig)
  12380. (equal (number-to-string (length enum-sigs))
  12381. (verilog-sig-width undecode-sig)))))
  12382. (enum-chars 0)
  12383. (ascii-chars 0))
  12384. ;;
  12385. ;; Find number of ascii chars needed
  12386. (let ((tmp-sigs enum-sigs))
  12387. (while tmp-sigs
  12388. (setq enum-chars (max enum-chars (length (verilog-sig-name (car tmp-sigs))))
  12389. ascii-chars (max ascii-chars (length (verilog-enum-ascii
  12390. (verilog-sig-name (car tmp-sigs))
  12391. elim-regexp)))
  12392. tmp-sigs (cdr tmp-sigs))))
  12393. ;;
  12394. (verilog-forward-or-insert-line)
  12395. (verilog-insert-indent "// Beginning of automatic ASCII enum decoding\n")
  12396. (let ((decode-sig-list (list (list ascii-name (format "[%d:0]" (- (* ascii-chars 8) 1))
  12397. (concat "Decode of " undecode-name) nil nil))))
  12398. (verilog-insert-definition modi decode-sig-list "reg" indent-pt nil))
  12399. ;;
  12400. (verilog-insert-indent "always @(" undecode-name ") begin\n")
  12401. (setq indent-pt (+ indent-pt verilog-indent-level))
  12402. (verilog-insert-indent "case ({" undecode-name "})\n")
  12403. (setq indent-pt (+ indent-pt verilog-case-indent))
  12404. ;;
  12405. (let ((tmp-sigs enum-sigs)
  12406. (chrfmt (format "%%-%ds %s = \"%%-%ds\";\n"
  12407. (+ (if one-hot 9 1) (max 8 enum-chars))
  12408. ascii-name ascii-chars))
  12409. (errname (substring "%Error" 0 (min 6 ascii-chars))))
  12410. (while tmp-sigs
  12411. (verilog-insert-indent
  12412. (concat
  12413. (format chrfmt
  12414. (concat (if one-hot "(")
  12415. ;; Use enum-sigs length as that's numeric
  12416. ;; verilog-sig-width undecode-sig might not be.
  12417. (if one-hot (number-to-string (length enum-sigs)))
  12418. ;; We use a shift instead of var[index]
  12419. ;; so that a non-one hot value will show as error.
  12420. (if one-hot "'b1<<")
  12421. (verilog-sig-name (car tmp-sigs))
  12422. (if one-hot ")") ":")
  12423. (verilog-enum-ascii (verilog-sig-name (car tmp-sigs))
  12424. elim-regexp))))
  12425. (setq tmp-sigs (cdr tmp-sigs)))
  12426. (verilog-insert-indent (format chrfmt "default:" errname)))
  12427. ;;
  12428. (setq indent-pt (- indent-pt verilog-case-indent))
  12429. (verilog-insert-indent "endcase\n")
  12430. (setq indent-pt (- indent-pt verilog-indent-level))
  12431. (verilog-insert-indent "end\n"
  12432. "// End of automatics\n"))))
  12433. (defun verilog-auto-templated-rel ()
  12434. "Replace Templated relative line numbers with absolute line numbers.
  12435. Internal use only. This hacks around the line numbers in AUTOINST Templates
  12436. being different from the final output's line numbering."
  12437. (let ((templateno 0) (template-line (list 0)) (buf-line 1))
  12438. ;; Find line number each template is on
  12439. ;; Count lines as we go, as otherwise it's O(n^2) to use count-lines
  12440. (goto-char (point-min))
  12441. (while (not (eobp))
  12442. (when (looking-at ".*AUTO_TEMPLATE")
  12443. (setq templateno (1+ templateno))
  12444. (setq template-line (cons buf-line template-line)))
  12445. (setq buf-line (1+ buf-line))
  12446. (forward-line 1))
  12447. (setq template-line (nreverse template-line))
  12448. ;; Replace T# L# with absolute line number
  12449. (goto-char (point-min))
  12450. (while (re-search-forward " Templated T\\([0-9]+\\) L\\([0-9]+\\)" nil t)
  12451. (replace-match
  12452. (concat " Templated "
  12453. (int-to-string (+ (nth (string-to-number (match-string 1))
  12454. template-line)
  12455. (string-to-number (match-string 2)))))
  12456. t t))))
  12457. (defun verilog-auto-template-lint ()
  12458. "Check AUTO_TEMPLATEs for unused lines.
  12459. Enable with `verilog-auto-template-warn-unused'."
  12460. (let ((name1 (or (buffer-file-name) (buffer-name))))
  12461. (save-excursion
  12462. (goto-char (point-min))
  12463. (while (re-search-forward
  12464. "^\\s-*/?\\*?\\s-*[a-zA-Z0-9`_$]+\\s-+AUTO_TEMPLATE" nil t)
  12465. (let* ((tpl-info (verilog-read-auto-template-middle))
  12466. (tpl-list (aref tpl-info 1))
  12467. (tlines (append (nth 0 tpl-list) (nth 1 tpl-list)))
  12468. tpl-ass)
  12469. (while tlines
  12470. (setq tpl-ass (car tlines)
  12471. tlines (cdr tlines))
  12472. ;;
  12473. (unless (or (not (eval-when-compile (fboundp 'make-hash-table))) ; Not supported, no warning
  12474. (not verilog-auto-template-hits)
  12475. (gethash (vector (nth 2 tpl-ass) (nth 3 tpl-ass))
  12476. verilog-auto-template-hits))
  12477. (verilog-warn-error "%s:%d: AUTO_TEMPLATE line unused: \".%s (%s)\""
  12478. name1
  12479. (+ (elt tpl-ass 3) ; Template line number
  12480. (count-lines (point-min) (point)))
  12481. (elt tpl-ass 0) (elt tpl-ass 1))
  12482. )))))))
  12483. ;;; Auto top level:
  12484. ;;
  12485. (defun verilog-auto (&optional inject) ; Use verilog-inject-auto instead of passing an arg
  12486. "Expand AUTO statements.
  12487. Look for any /*AUTO...*/ commands in the code, as used in
  12488. instantiations or argument headers. Update the list of signals
  12489. following the /*AUTO...*/ command.
  12490. Use \\[verilog-delete-auto] to remove the AUTOs.
  12491. Use \\[verilog-diff-auto] to see differences in AUTO expansion.
  12492. Use \\[verilog-inject-auto] to insert AUTOs for the first time.
  12493. Use \\[verilog-faq] for a pointer to frequently asked questions.
  12494. For new users, we recommend setting `verilog-case-fold' to nil
  12495. and `verilog-auto-arg-sort' to t.
  12496. The hooks `verilog-before-auto-hook' and `verilog-auto-hook' are
  12497. called before and after this function, respectively.
  12498. For example:
  12499. module ModuleName (/*AUTOARG*/);
  12500. /*AUTOINPUT*/
  12501. /*AUTOOUTPUT*/
  12502. /*AUTOWIRE*/
  12503. /*AUTOREG*/
  12504. InstMod instName #(/*AUTOINSTPARAM*/) (/*AUTOINST*/);
  12505. You can also update the AUTOs from the shell using:
  12506. emacs --batch <filenames.v> -f verilog-batch-auto
  12507. Or fix indentation with:
  12508. emacs --batch <filenames.v> -f verilog-batch-indent
  12509. Likewise, you can delete or inject AUTOs with:
  12510. emacs --batch <filenames.v> -f verilog-batch-delete-auto
  12511. emacs --batch <filenames.v> -f verilog-batch-inject-auto
  12512. Or check if AUTOs have the same expansion
  12513. emacs --batch <filenames.v> -f verilog-batch-diff-auto
  12514. Using \\[describe-function], see also:
  12515. `verilog-auto-arg' for AUTOARG module instantiations
  12516. `verilog-auto-ascii-enum' for AUTOASCIIENUM enumeration decoding
  12517. `verilog-auto-assign-modport' for AUTOASSIGNMODPORT assignment to/from modport
  12518. `verilog-auto-inout' for AUTOINOUT making hierarchy inouts
  12519. `verilog-auto-inout-comp' for AUTOINOUTCOMP copy complemented i/o
  12520. `verilog-auto-inout-in' for AUTOINOUTIN inputs for all i/o
  12521. `verilog-auto-inout-modport' for AUTOINOUTMODPORT i/o from an interface modport
  12522. `verilog-auto-inout-module' for AUTOINOUTMODULE copying i/o from elsewhere
  12523. `verilog-auto-inout-param' for AUTOINOUTPARAM copying params from elsewhere
  12524. `verilog-auto-input' for AUTOINPUT making hierarchy inputs
  12525. `verilog-auto-insert-lisp' for AUTOINSERTLISP insert code from lisp function
  12526. `verilog-auto-insert-last' for AUTOINSERTLAST insert code from lisp function
  12527. `verilog-auto-inst' for AUTOINST instantiation pins
  12528. `verilog-auto-star' for AUTOINST .* SystemVerilog pins
  12529. `verilog-auto-inst-param' for AUTOINSTPARAM instantiation params
  12530. `verilog-auto-logic' for AUTOLOGIC declaring logic signals
  12531. `verilog-auto-output' for AUTOOUTPUT making hierarchy outputs
  12532. `verilog-auto-output-every' for AUTOOUTPUTEVERY making all outputs
  12533. `verilog-auto-reg' for AUTOREG registers
  12534. `verilog-auto-reg-input' for AUTOREGINPUT instantiation registers
  12535. `verilog-auto-reset' for AUTORESET flop resets
  12536. `verilog-auto-sense' for AUTOSENSE or AS always sensitivity lists
  12537. `verilog-auto-tieoff' for AUTOTIEOFF output tieoffs
  12538. `verilog-auto-undef' for AUTOUNDEF \\=`undef of local \\=`defines
  12539. `verilog-auto-unused' for AUTOUNUSED unused inputs/inouts
  12540. `verilog-auto-wire' for AUTOWIRE instantiation wires
  12541. `verilog-read-defines' for reading \\=`define values
  12542. `verilog-read-includes' for reading \\=`includes
  12543. If you have bugs with these autos, please file an issue at
  12544. URL `http://www.veripool.org/verilog-mode' or contact the AUTOAUTHOR
  12545. Wilson Snyder (wsnyder@wsnyder.org)."
  12546. (interactive)
  12547. (unless noninteractive (message "Updating AUTOs..."))
  12548. (if (fboundp 'dinotrace-unannotate-all)
  12549. (dinotrace-unannotate-all))
  12550. ;; Disable change hooks for speed
  12551. ;; This let can't be part of above let; must restore
  12552. ;; after-change-functions before font-lock resumes
  12553. (verilog-save-font-no-change-functions
  12554. (let ((oldbuf (if (not (buffer-modified-p))
  12555. (buffer-string)))
  12556. (case-fold-search verilog-case-fold)
  12557. ;; Cache directories; we don't write new files, so can't change
  12558. (verilog-dir-cache-preserving t)
  12559. ;; Cache current module
  12560. (verilog-modi-cache-current-enable t)
  12561. (verilog-modi-cache-current-max (point-min)) ; IE it's invalid
  12562. verilog-modi-cache-current)
  12563. (verilog-save-scan-cache
  12564. (save-excursion
  12565. ;; Wipe cache; otherwise if we AUTOed a block above this one,
  12566. ;; we'll misremember we have generated IOs, confusing AUTOOUTPUT
  12567. (setq verilog-modi-cache-list nil)
  12568. ;; Local state
  12569. (verilog-read-auto-template-init)
  12570. ;; If we're not in verilog-mode, change syntax table so parsing works right
  12571. (unless (eq major-mode `verilog-mode) (verilog-mode))
  12572. ;; Allow user to customize
  12573. (verilog-run-hooks 'verilog-before-auto-hook)
  12574. ;; Try to save the user from needing to revert-file to reread file local-variables
  12575. (verilog-auto-reeval-locals)
  12576. (verilog-read-auto-lisp-present)
  12577. (verilog-read-auto-lisp (point-min) (point-max))
  12578. (verilog-getopt-flags)
  12579. ;; From here on out, we can cache anything we read from disk
  12580. (verilog-preserve-dir-cache
  12581. ;; These two may seem obvious to do always, but on large includes it can be way too slow
  12582. (when verilog-auto-read-includes
  12583. (verilog-read-includes)
  12584. (verilog-read-defines nil nil t))
  12585. ;; Setup variables due to SystemVerilog expansion
  12586. (verilog-auto-re-search-do "/\\*AUTOLOGIC\\*/" 'verilog-auto-logic-setup)
  12587. ;; This particular ordering is important
  12588. ;; INST: Lower modules correct, no internal dependencies, FIRST
  12589. (verilog-preserve-modi-cache
  12590. ;; Clear existing autos else we'll be screwed by existing ones
  12591. (verilog-delete-auto-buffer)
  12592. ;; Injection if appropriate
  12593. (when inject
  12594. (verilog-inject-inst)
  12595. (verilog-inject-sense)
  12596. (verilog-inject-arg))
  12597. ;;
  12598. ;; Do user inserts first, so their code can insert AUTOs
  12599. (verilog-auto-re-search-do "/\\*AUTOINSERTLISP(.*?)\\*/"
  12600. 'verilog-auto-insert-lisp)
  12601. ;; Expand instances before need the signals the instances input/output
  12602. (verilog-auto-re-search-do "/\\*AUTOINSTPARAM\\*/" 'verilog-auto-inst-param)
  12603. (verilog-auto-re-search-do "/\\*AUTOINST\\*/" 'verilog-auto-inst)
  12604. (verilog-auto-re-search-do "\\.\\*" 'verilog-auto-star)
  12605. ;; Doesn't matter when done, but combine it with a common changer
  12606. (verilog-auto-re-search-do "/\\*\\(AUTOSENSE\\|AS\\)\\*/" 'verilog-auto-sense)
  12607. (verilog-auto-re-search-do "/\\*AUTORESET\\*/" 'verilog-auto-reset)
  12608. ;; Must be done before autoin/out as creates a reg
  12609. (verilog-auto-re-search-do "/\\*AUTOASCIIENUM(.*?)\\*/" 'verilog-auto-ascii-enum)
  12610. ;;
  12611. ;; first in/outs from other files
  12612. (verilog-auto-re-search-do "/\\*AUTOINOUTMODPORT(.*?)\\*/" 'verilog-auto-inout-modport)
  12613. (verilog-auto-re-search-do "/\\*AUTOINOUTMODULE(.*?)\\*/" 'verilog-auto-inout-module)
  12614. (verilog-auto-re-search-do "/\\*AUTOINOUTCOMP(.*?)\\*/" 'verilog-auto-inout-comp)
  12615. (verilog-auto-re-search-do "/\\*AUTOINOUTIN(.*?)\\*/" 'verilog-auto-inout-in)
  12616. (verilog-auto-re-search-do "/\\*AUTOINOUTPARAM(.*?)\\*/" 'verilog-auto-inout-param)
  12617. ;; next in/outs which need previous sucked inputs first
  12618. (verilog-auto-re-search-do "/\\*AUTOOUTPUT\\((.*?)\\)?\\*/" 'verilog-auto-output)
  12619. (verilog-auto-re-search-do "/\\*AUTOINPUT\\((.*?)\\)?\\*/" 'verilog-auto-input)
  12620. (verilog-auto-re-search-do "/\\*AUTOINOUT\\((.*?)\\)?\\*/" 'verilog-auto-inout)
  12621. ;; Then tie off those in/outs
  12622. (verilog-auto-re-search-do "/\\*AUTOTIEOFF\\*/" 'verilog-auto-tieoff)
  12623. ;; These can be anywhere after AUTOINSERTLISP
  12624. (verilog-auto-re-search-do "/\\*AUTOUNDEF\\((.*?)\\)?\\*/" 'verilog-auto-undef)
  12625. ;; Wires/regs must be after inputs/outputs
  12626. (verilog-auto-re-search-do "/\\*AUTOASSIGNMODPORT(.*?)\\*/" 'verilog-auto-assign-modport)
  12627. (verilog-auto-re-search-do "/\\*AUTOLOGIC\\*/" 'verilog-auto-logic)
  12628. (verilog-auto-re-search-do "/\\*AUTOWIRE\\*/" 'verilog-auto-wire)
  12629. (verilog-auto-re-search-do "/\\*AUTOREG\\*/" 'verilog-auto-reg)
  12630. (verilog-auto-re-search-do "/\\*AUTOREGINPUT\\*/" 'verilog-auto-reg-input)
  12631. ;; outputevery needs AUTOOUTPUTs done first
  12632. (verilog-auto-re-search-do "/\\*AUTOOUTPUTEVERY\\((.*?)\\)?\\*/" 'verilog-auto-output-every)
  12633. ;; After we've created all new variables
  12634. (verilog-auto-re-search-do "/\\*AUTOUNUSED\\*/" 'verilog-auto-unused)
  12635. ;; Must be after all inputs outputs are generated
  12636. (verilog-auto-re-search-do "/\\*AUTOARG\\*/" 'verilog-auto-arg)
  12637. ;; User inserts
  12638. (verilog-auto-re-search-do "/\\*AUTOINSERTLAST(.*?)\\*/" 'verilog-auto-insert-last)
  12639. ;; Fix line numbers (comments only)
  12640. (when verilog-auto-inst-template-numbers
  12641. (verilog-auto-templated-rel))
  12642. (when verilog-auto-template-warn-unused
  12643. (verilog-auto-template-lint))))
  12644. ;;
  12645. (verilog-run-hooks 'verilog-auto-hook)
  12646. ;;
  12647. (when verilog-auto-delete-trailing-whitespace
  12648. (verilog-delete-trailing-whitespace))
  12649. ;;
  12650. (set (make-local-variable 'verilog-auto-update-tick) (buffer-chars-modified-tick))
  12651. ;;
  12652. ;; If end result is same as when started, clear modified flag
  12653. (cond ((and oldbuf (equal oldbuf (buffer-string)))
  12654. (verilog-restore-buffer-modified-p nil)
  12655. (unless noninteractive (message "Updating AUTOs...done (no changes)")))
  12656. (t (unless noninteractive (message "Updating AUTOs...done"))))
  12657. ;; End of save-cache
  12658. )))))
  12659. ;;; Skeletons:
  12660. ;;
  12661. (defvar verilog-template-map
  12662. (let ((map (make-sparse-keymap)))
  12663. (define-key map "a" 'verilog-sk-always)
  12664. (define-key map "b" 'verilog-sk-begin)
  12665. (define-key map "c" 'verilog-sk-case)
  12666. (define-key map "f" 'verilog-sk-for)
  12667. (define-key map "g" 'verilog-sk-generate)
  12668. (define-key map "h" 'verilog-sk-header)
  12669. (define-key map "i" 'verilog-sk-initial)
  12670. (define-key map "j" 'verilog-sk-fork)
  12671. (define-key map "m" 'verilog-sk-module)
  12672. (define-key map "o" 'verilog-sk-ovm-class)
  12673. (define-key map "p" 'verilog-sk-primitive)
  12674. (define-key map "r" 'verilog-sk-repeat)
  12675. (define-key map "s" 'verilog-sk-specify)
  12676. (define-key map "t" 'verilog-sk-task)
  12677. (define-key map "u" 'verilog-sk-uvm-object)
  12678. (define-key map "w" 'verilog-sk-while)
  12679. (define-key map "x" 'verilog-sk-casex)
  12680. (define-key map "z" 'verilog-sk-casez)
  12681. (define-key map "?" 'verilog-sk-if)
  12682. (define-key map ":" 'verilog-sk-else-if)
  12683. (define-key map "/" 'verilog-sk-comment)
  12684. (define-key map "A" 'verilog-sk-assign)
  12685. (define-key map "F" 'verilog-sk-function)
  12686. (define-key map "I" 'verilog-sk-input)
  12687. (define-key map "O" 'verilog-sk-output)
  12688. (define-key map "S" 'verilog-sk-state-machine)
  12689. (define-key map "=" 'verilog-sk-inout)
  12690. (define-key map "U" 'verilog-sk-uvm-component)
  12691. (define-key map "W" 'verilog-sk-wire)
  12692. (define-key map "R" 'verilog-sk-reg)
  12693. (define-key map "D" 'verilog-sk-define-signal)
  12694. map)
  12695. "Keymap used in Verilog mode for smart template operations.")
  12696. ;;
  12697. ;; Place the templates into Verilog Mode. They may be inserted under any key.
  12698. ;; C-c C-t will be the default. If you use templates a lot, you
  12699. ;; may want to consider moving the binding to another key in your init
  12700. ;; file.
  12701. ;;
  12702. ;; Note \C-c and letter are reserved for users
  12703. (define-key verilog-mode-map "\C-c\C-t" verilog-template-map)
  12704. ;; ---- statement skeletons ------------------------------------------
  12705. (define-skeleton verilog-sk-prompt-condition
  12706. "Prompt for the loop condition."
  12707. "[condition]: " str )
  12708. (define-skeleton verilog-sk-prompt-init
  12709. "Prompt for the loop init statement."
  12710. "[initial statement]: " str )
  12711. (define-skeleton verilog-sk-prompt-inc
  12712. "Prompt for the loop increment statement."
  12713. "[increment statement]: " str )
  12714. (define-skeleton verilog-sk-prompt-name
  12715. "Prompt for the name of something."
  12716. "[name]: " str)
  12717. (define-skeleton verilog-sk-prompt-clock
  12718. "Prompt for the name of something."
  12719. "name and edge of clock(s): " str)
  12720. (defvar verilog-sk-reset nil)
  12721. (defun verilog-sk-prompt-reset ()
  12722. "Prompt for the name of a state machine reset."
  12723. (setq verilog-sk-reset (read-string "name of reset: " "rst")))
  12724. (define-skeleton verilog-sk-prompt-state-selector
  12725. "Prompt for the name of a state machine selector."
  12726. "name of selector (eg {a,b,c,d}): " str )
  12727. (define-skeleton verilog-sk-prompt-output
  12728. "Prompt for the name of something."
  12729. "output: " str)
  12730. (define-skeleton verilog-sk-prompt-msb
  12731. "Prompt for most significant bit specification."
  12732. "msb:" str & ?: & '(verilog-sk-prompt-lsb) | -1 )
  12733. (define-skeleton verilog-sk-prompt-lsb
  12734. "Prompt for least significant bit specification."
  12735. "lsb:" str )
  12736. (defvar verilog-sk-p nil)
  12737. (define-skeleton verilog-sk-prompt-width
  12738. "Prompt for a width specification."
  12739. ()
  12740. (progn
  12741. (setq verilog-sk-p (point))
  12742. (verilog-sk-prompt-msb)
  12743. (if (> (point) verilog-sk-p) "] " " ")))
  12744. (defun verilog-sk-header ()
  12745. "Insert a descriptive header at the top of the file.
  12746. See also `verilog-header' for an alternative format."
  12747. (interactive "*")
  12748. (save-excursion
  12749. (goto-char (point-min))
  12750. (verilog-sk-header-tmpl)))
  12751. (define-skeleton verilog-sk-header-tmpl
  12752. "Insert a comment block containing the module title, author, etc."
  12753. "[Description]: "
  12754. "// -*- Mode: Verilog -*-"
  12755. "\n// Filename : " (buffer-name)
  12756. "\n// Description : " str
  12757. "\n// Author : " (user-full-name)
  12758. "\n// Created On : " (current-time-string)
  12759. "\n// Last Modified By: " (user-full-name)
  12760. "\n// Last Modified On: " (current-time-string)
  12761. "\n// Update Count : 0"
  12762. "\n// Status : Unknown, Use with caution!"
  12763. "\n")
  12764. (define-skeleton verilog-sk-module
  12765. "Insert a module definition."
  12766. ()
  12767. > "module " '(verilog-sk-prompt-name) " (/*AUTOARG*/ ) ;" \n
  12768. > _ \n
  12769. > (- verilog-indent-level-behavioral) "endmodule" (progn (electric-verilog-terminate-line) nil))
  12770. ;; ------------------------------------------------------------------------
  12771. ;; Define a default OVM class, with macros and new()
  12772. ;; ------------------------------------------------------------------------
  12773. (define-skeleton verilog-sk-ovm-class
  12774. "Insert a class definition"
  12775. ()
  12776. > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
  12777. > _ \n
  12778. > "`ovm_object_utils_begin(" name ")" \n
  12779. > (- verilog-indent-level) " `ovm_object_utils_end" \n
  12780. > _ \n
  12781. > "function new(string name=\"" name "\");" \n
  12782. > "super.new(name);" \n
  12783. > (- verilog-indent-level) "endfunction" \n
  12784. > _ \n
  12785. > "endclass" (progn (electric-verilog-terminate-line) nil))
  12786. (define-skeleton verilog-sk-uvm-object
  12787. "Insert a class definition"
  12788. ()
  12789. > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
  12790. > _ \n
  12791. > "`uvm_object_utils_begin(" name ")" \n
  12792. > (- verilog-indent-level) "`uvm_object_utils_end" \n
  12793. > _ \n
  12794. > "function new(string name=\"" name "\");" \n
  12795. > "super.new(name);" \n
  12796. > (- verilog-indent-level) "endfunction" \n
  12797. > _ \n
  12798. > "endclass" (progn (electric-verilog-terminate-line) nil))
  12799. (define-skeleton verilog-sk-uvm-component
  12800. "Insert a class definition"
  12801. ()
  12802. > "class " (setq name (skeleton-read "Name: ")) " extends " (skeleton-read "Extends: ") ";" \n
  12803. > _ \n
  12804. > "`uvm_component_utils_begin(" name ")" \n
  12805. > (- verilog-indent-level) "`uvm_component_utils_end" \n
  12806. > _ \n
  12807. > "function new(string name=\"\", uvm_component parent);" \n
  12808. > "super.new(name, parent);" \n
  12809. > (- verilog-indent-level) "endfunction" \n
  12810. > _ \n
  12811. > "endclass" (progn (electric-verilog-terminate-line) nil))
  12812. (define-skeleton verilog-sk-primitive
  12813. "Insert a task definition."
  12814. ()
  12815. > "primitive " '(verilog-sk-prompt-name) " ( " '(verilog-sk-prompt-output) ("input:" ", " str ) " );"\n
  12816. > _ \n
  12817. > (- verilog-indent-level-behavioral) "endprimitive" (progn (electric-verilog-terminate-line) nil))
  12818. (define-skeleton verilog-sk-task
  12819. "Insert a task definition."
  12820. ()
  12821. > "task " '(verilog-sk-prompt-name) & ?; \n
  12822. > _ \n
  12823. > "begin" \n
  12824. > \n
  12825. > (- verilog-indent-level-behavioral) "end" \n
  12826. > (- verilog-indent-level-behavioral) "endtask" (progn (electric-verilog-terminate-line) nil))
  12827. (define-skeleton verilog-sk-function
  12828. "Insert a function definition."
  12829. ()
  12830. > "function [" '(verilog-sk-prompt-width) | -1 '(verilog-sk-prompt-name) ?; \n
  12831. > _ \n
  12832. > "begin" \n
  12833. > \n
  12834. > (- verilog-indent-level-behavioral) "end" \n
  12835. > (- verilog-indent-level-behavioral) "endfunction" (progn (electric-verilog-terminate-line) nil))
  12836. (define-skeleton verilog-sk-always
  12837. "Insert always block. Uses the minibuffer to prompt
  12838. for sensitivity list."
  12839. ()
  12840. > "always @ ( /*AUTOSENSE*/ ) begin\n"
  12841. > _ \n
  12842. > (- verilog-indent-level-behavioral) "end" \n >
  12843. )
  12844. (define-skeleton verilog-sk-initial
  12845. "Insert an initial block."
  12846. ()
  12847. > "initial begin\n"
  12848. > _ \n
  12849. > (- verilog-indent-level-behavioral) "end" \n > )
  12850. (define-skeleton verilog-sk-specify
  12851. "Insert specify block. "
  12852. ()
  12853. > "specify\n"
  12854. > _ \n
  12855. > (- verilog-indent-level-behavioral) "endspecify" \n > )
  12856. (define-skeleton verilog-sk-generate
  12857. "Insert generate block. "
  12858. ()
  12859. > "generate\n"
  12860. > _ \n
  12861. > (- verilog-indent-level-behavioral) "endgenerate" \n > )
  12862. (define-skeleton verilog-sk-begin
  12863. "Insert begin end block. Uses the minibuffer to prompt for name."
  12864. ()
  12865. > "begin" '(verilog-sk-prompt-name) \n
  12866. > _ \n
  12867. > (- verilog-indent-level-behavioral) "end" )
  12868. (define-skeleton verilog-sk-fork
  12869. "Insert a fork join block."
  12870. ()
  12871. > "fork\n"
  12872. > "begin" \n
  12873. > _ \n
  12874. > (- verilog-indent-level-behavioral) "end" \n
  12875. > "begin" \n
  12876. > \n
  12877. > (- verilog-indent-level-behavioral) "end" \n
  12878. > (- verilog-indent-level-behavioral) "join" \n
  12879. > )
  12880. (define-skeleton verilog-sk-case
  12881. "Build skeleton case statement, prompting for the selector expression,
  12882. and the case items."
  12883. "[selector expression]: "
  12884. > "case (" str ") " \n
  12885. > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
  12886. resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
  12887. (define-skeleton verilog-sk-casex
  12888. "Build skeleton casex statement, prompting for the selector expression,
  12889. and the case items."
  12890. "[selector expression]: "
  12891. > "casex (" str ") " \n
  12892. > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
  12893. resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
  12894. (define-skeleton verilog-sk-casez
  12895. "Build skeleton casez statement, prompting for the selector expression,
  12896. and the case items."
  12897. "[selector expression]: "
  12898. > "casez (" str ") " \n
  12899. > ("case selector: " str ": begin" \n > _ \n > (- verilog-indent-level-behavioral) "end" \n > )
  12900. resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil))
  12901. (define-skeleton verilog-sk-if
  12902. "Insert a skeleton if statement."
  12903. > "if (" '(verilog-sk-prompt-condition) & ")" " begin" \n
  12904. > _ \n
  12905. > (- verilog-indent-level-behavioral) "end " \n )
  12906. (define-skeleton verilog-sk-else-if
  12907. "Insert a skeleton else if statement."
  12908. > (verilog-indent-line) "else if ("
  12909. (progn (setq verilog-sk-p (point)) nil) '(verilog-sk-prompt-condition) (if (> (point) verilog-sk-p) ") " -1 ) & " begin" \n
  12910. > _ \n
  12911. > "end" (progn (electric-verilog-terminate-line) nil))
  12912. (define-skeleton verilog-sk-datadef
  12913. "Common routine to get data definition."
  12914. ()
  12915. '(verilog-sk-prompt-width) | -1 ("name (RET to end):" str ", ") -2 ";" \n)
  12916. (define-skeleton verilog-sk-input
  12917. "Insert an input definition."
  12918. ()
  12919. > "input [" '(verilog-sk-datadef))
  12920. (define-skeleton verilog-sk-output
  12921. "Insert an output definition."
  12922. ()
  12923. > "output [" '(verilog-sk-datadef))
  12924. (define-skeleton verilog-sk-inout
  12925. "Insert an inout definition."
  12926. ()
  12927. > "inout [" '(verilog-sk-datadef))
  12928. (defvar verilog-sk-signal nil)
  12929. (define-skeleton verilog-sk-def-reg
  12930. "Insert a reg definition."
  12931. ()
  12932. > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-signal ";" \n (verilog-pretty-declarations-auto) )
  12933. (defun verilog-sk-define-signal ()
  12934. "Insert a definition of signal under point at top of module."
  12935. (interactive "*")
  12936. (let* ((sig-re "[a-zA-Z0-9_]*")
  12937. (v1 (buffer-substring
  12938. (save-excursion
  12939. (skip-chars-backward sig-re)
  12940. (point))
  12941. (save-excursion
  12942. (skip-chars-forward sig-re)
  12943. (point)))))
  12944. (if (not (member v1 verilog-keywords))
  12945. (save-excursion
  12946. (setq verilog-sk-signal v1)
  12947. (verilog-beg-of-defun)
  12948. (verilog-end-of-statement)
  12949. (verilog-forward-syntactic-ws)
  12950. (verilog-sk-def-reg)
  12951. (message "signal at point is %s" v1))
  12952. (message "object at point (%s) is a keyword" v1))))
  12953. (define-skeleton verilog-sk-wire
  12954. "Insert a wire definition."
  12955. ()
  12956. > "wire [" '(verilog-sk-datadef))
  12957. (define-skeleton verilog-sk-reg
  12958. "Insert a reg definition."
  12959. ()
  12960. > "reg [" '(verilog-sk-datadef))
  12961. (define-skeleton verilog-sk-assign
  12962. "Insert a skeleton assign statement."
  12963. ()
  12964. > "assign " '(verilog-sk-prompt-name) " = " _ ";" \n)
  12965. (define-skeleton verilog-sk-while
  12966. "Insert a skeleton while loop statement."
  12967. ()
  12968. > "while (" '(verilog-sk-prompt-condition) ") begin" \n
  12969. > _ \n
  12970. > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
  12971. (define-skeleton verilog-sk-repeat
  12972. "Insert a skeleton repeat loop statement."
  12973. ()
  12974. > "repeat (" '(verilog-sk-prompt-condition) ") begin" \n
  12975. > _ \n
  12976. > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
  12977. (define-skeleton verilog-sk-for
  12978. "Insert a skeleton while loop statement."
  12979. ()
  12980. > "for ("
  12981. '(verilog-sk-prompt-init) "; "
  12982. '(verilog-sk-prompt-condition) "; "
  12983. '(verilog-sk-prompt-inc)
  12984. ") begin" \n
  12985. > _ \n
  12986. > (- verilog-indent-level-behavioral) "end " (progn (electric-verilog-terminate-line) nil))
  12987. (define-skeleton verilog-sk-comment
  12988. "Inserts three comment lines, making a display comment."
  12989. ()
  12990. > "/*\n"
  12991. > "* " _ \n
  12992. > "*/")
  12993. (define-skeleton verilog-sk-state-machine
  12994. "Insert a state machine definition."
  12995. "Name of state variable: "
  12996. '(setq input "state")
  12997. > "// State registers for " str | -23 \n
  12998. '(setq verilog-sk-state str)
  12999. > "reg [" '(verilog-sk-prompt-width) | -1 verilog-sk-state ", next_" verilog-sk-state ?; \n
  13000. '(setq input nil)
  13001. > \n
  13002. > "// State FF for " verilog-sk-state \n
  13003. > "always @ ( " (read-string "clock:" "posedge clk") " or " (verilog-sk-prompt-reset) " ) begin" \n
  13004. > "if ( " verilog-sk-reset " ) " verilog-sk-state " = 0; else" \n
  13005. > verilog-sk-state " = next_" verilog-sk-state ?; \n
  13006. > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil)
  13007. > \n
  13008. > "// Next State Logic for " verilog-sk-state \n
  13009. > "always @ ( /*AUTOSENSE*/ ) begin\n"
  13010. > "case (" '(verilog-sk-prompt-state-selector) ") " \n
  13011. > ("case selector: " str ": begin" \n > "next_" verilog-sk-state " = " _ ";" \n > (- verilog-indent-level-behavioral) "end" \n )
  13012. resume: > (- verilog-case-indent) "endcase" (progn (electric-verilog-terminate-line) nil)
  13013. > (- verilog-indent-level-behavioral) "end" (progn (electric-verilog-terminate-line) nil))
  13014. ;;; Mouse Events:
  13015. ;;
  13016. ;; Include file loading with mouse/return event
  13017. ;;
  13018. ;; idea & first impl.: M. Rouat (eldo-mode.el)
  13019. ;; second (emacs/xemacs) impl.: G. Van der Plas (spice-mode.el)
  13020. (if (featurep 'xemacs)
  13021. (require 'overlay))
  13022. (defconst verilog-include-file-regexp
  13023. "^`include\\s-+\"\\([^\n\"]*\\)\""
  13024. "Regexp that matches the include file.")
  13025. (defvar verilog-mode-mouse-map
  13026. (let ((map (make-sparse-keymap))) ; as described in info pages, make a map
  13027. (set-keymap-parent map verilog-mode-map)
  13028. ;; mouse button bindings
  13029. (define-key map "\r" 'verilog-load-file-at-point)
  13030. (if (featurep 'xemacs)
  13031. (define-key map 'button2 'verilog-load-file-at-mouse);ffap-at-mouse ?
  13032. (define-key map [mouse-2] 'verilog-load-file-at-mouse))
  13033. (if (featurep 'xemacs)
  13034. (define-key map 'Sh-button2 'mouse-yank) ; you wanna paste don't you ?
  13035. (define-key map [S-mouse-2] 'mouse-yank-at-click))
  13036. map)
  13037. "Map containing mouse bindings for `verilog-mode'.")
  13038. (defun verilog-highlight-region (beg end _old-len)
  13039. "Colorize included files and modules in the (changed?) region.
  13040. Clicking on the middle-mouse button loads them in a buffer (as in dired)."
  13041. (when (or verilog-highlight-includes
  13042. verilog-highlight-modules)
  13043. (save-excursion
  13044. (save-match-data ; A query-replace may call this function - do not disturb
  13045. (verilog-save-buffer-state
  13046. (verilog-save-scan-cache
  13047. (let (end-point)
  13048. (goto-char end)
  13049. (setq end-point (point-at-eol))
  13050. (goto-char beg)
  13051. (beginning-of-line) ; scan entire line
  13052. ;; delete overlays existing on this line
  13053. (let ((overlays (overlays-in (point) end-point)))
  13054. (while overlays
  13055. (if (and
  13056. (overlay-get (car overlays) 'detachable)
  13057. (or (overlay-get (car overlays) 'verilog-include-file)
  13058. (overlay-get (car overlays) 'verilog-inst-module)))
  13059. (delete-overlay (car overlays)))
  13060. (setq overlays (cdr overlays))))
  13061. ;;
  13062. ;; make new include overlays
  13063. (when verilog-highlight-includes
  13064. (while (search-forward-regexp verilog-include-file-regexp end-point t)
  13065. (goto-char (match-beginning 1))
  13066. (let ((ov (make-overlay (match-beginning 1) (match-end 1))))
  13067. (overlay-put ov 'start-closed 't)
  13068. (overlay-put ov 'end-closed 't)
  13069. (overlay-put ov 'evaporate 't)
  13070. (overlay-put ov 'verilog-include-file 't)
  13071. (overlay-put ov 'mouse-face 'highlight)
  13072. (overlay-put ov 'local-map verilog-mode-mouse-map))))
  13073. ;;
  13074. ;; make new module overlays
  13075. (goto-char beg)
  13076. ;; This scanner is syntax-fragile, so don't get bent
  13077. (when verilog-highlight-modules
  13078. (condition-case nil
  13079. (while (verilog-re-search-forward-quick "\\(/\\*AUTOINST\\*/\\|\\.\\*\\)" end-point t)
  13080. (save-excursion
  13081. (goto-char (match-beginning 0))
  13082. (unless (verilog-inside-comment-or-string-p)
  13083. (verilog-read-inst-module-matcher) ; sets match 0
  13084. (let* ((ov (make-overlay (match-beginning 0) (match-end 0))))
  13085. (overlay-put ov 'start-closed 't)
  13086. (overlay-put ov 'end-closed 't)
  13087. (overlay-put ov 'evaporate 't)
  13088. (overlay-put ov 'verilog-inst-module 't)
  13089. (overlay-put ov 'mouse-face 'highlight)
  13090. (overlay-put ov 'local-map verilog-mode-mouse-map)))))
  13091. (error nil)))
  13092. ;;
  13093. ;; Future highlights:
  13094. ;; variables - make an Occur buffer of where referenced
  13095. ;; pins - make an Occur buffer of the sig in the declaration module
  13096. )))))))
  13097. (defun verilog-highlight-buffer ()
  13098. "Colorize included files and modules across the whole buffer."
  13099. ;; Invoked via verilog-mode calling font-lock then `font-lock-mode-hook'
  13100. (interactive)
  13101. ;; delete and remake overlays
  13102. (verilog-highlight-region (point-min) (point-max) nil))
  13103. ;; Deprecated, but was interactive, so we'll keep it around
  13104. (defalias 'verilog-colorize-include-files-buffer 'verilog-highlight-buffer)
  13105. ;; ffap-at-mouse isn't useful for Verilog mode. It uses library paths.
  13106. ;; so define this function to do more or less the same as ffap-at-mouse
  13107. ;; but first resolve filename...
  13108. (defun verilog-load-file-at-mouse (event)
  13109. "Load file under button 2 click's EVENT.
  13110. Files are checked based on `verilog-library-flags'."
  13111. (interactive "@e")
  13112. (save-excursion ; implement a Verilog specific ffap-at-mouse
  13113. (mouse-set-point event)
  13114. (verilog-load-file-at-point t)))
  13115. ;; ffap isn't usable for Verilog mode. It uses library paths.
  13116. ;; so define this function to do more or less the same as ffap
  13117. ;; but first resolve filename...
  13118. (defun verilog-load-file-at-point (&optional warn)
  13119. "Load file under point.
  13120. If WARN, throw warning if not found.
  13121. Files are checked based on `verilog-library-flags'."
  13122. (interactive)
  13123. (save-excursion ; implement a Verilog specific ffap
  13124. (let ((overlays (overlays-in (point) (point)))
  13125. hit)
  13126. (while (and overlays (not hit))
  13127. (when (overlay-get (car overlays) 'verilog-inst-module)
  13128. (verilog-goto-defun-file (buffer-substring
  13129. (overlay-start (car overlays))
  13130. (overlay-end (car overlays))))
  13131. (setq hit t))
  13132. (setq overlays (cdr overlays)))
  13133. ;; Include?
  13134. (beginning-of-line)
  13135. (when (and (not hit)
  13136. (looking-at verilog-include-file-regexp))
  13137. (if (and (car (verilog-library-filenames
  13138. (match-string 1) (buffer-file-name)))
  13139. (file-readable-p (car (verilog-library-filenames
  13140. (match-string 1) (buffer-file-name)))))
  13141. (find-file (car (verilog-library-filenames
  13142. (match-string 1) (buffer-file-name))))
  13143. (when warn
  13144. (message
  13145. "File `%s' isn't readable, use shift-mouse2 to paste in this field"
  13146. (match-string 1))))))))
  13147. ;;; Bug reporting:
  13148. ;;
  13149. (defun verilog-faq ()
  13150. "Tell the user their current version, and where to get the FAQ etc."
  13151. (interactive)
  13152. (with-output-to-temp-buffer "*verilog-mode help*"
  13153. (princ (format "You are using verilog-mode %s\n" verilog-mode-version))
  13154. (princ "\n")
  13155. (princ "For new releases, see http://www.verilog.com\n")
  13156. (princ "\n")
  13157. (princ "For frequently asked questions, see http://www.veripool.org/verilog-mode-faq.html\n")
  13158. (princ "\n")
  13159. (princ "To submit a bug, use M-x verilog-submit-bug-report\n")
  13160. (princ "\n")))
  13161. (autoload 'reporter-submit-bug-report "reporter")
  13162. (defvar reporter-prompt-for-summary-p)
  13163. (defun verilog-submit-bug-report ()
  13164. "Submit via mail a bug report on verilog-mode.el."
  13165. (interactive)
  13166. (let ((reporter-prompt-for-summary-p t))
  13167. (reporter-submit-bug-report
  13168. "mac@verilog.com, wsnyder@wsnyder.org"
  13169. (concat "verilog-mode v" verilog-mode-version)
  13170. '(
  13171. verilog-active-low-regexp
  13172. verilog-after-save-font-hook
  13173. verilog-align-ifelse
  13174. verilog-assignment-delay
  13175. verilog-auto-arg-sort
  13176. verilog-auto-declare-nettype
  13177. verilog-auto-delete-trailing-whitespace
  13178. verilog-auto-endcomments
  13179. verilog-auto-hook
  13180. verilog-auto-ignore-concat
  13181. verilog-auto-indent-on-newline
  13182. verilog-auto-inout-ignore-regexp
  13183. verilog-auto-input-ignore-regexp
  13184. verilog-auto-inst-column
  13185. verilog-auto-inst-dot-name
  13186. verilog-auto-inst-interfaced-ports
  13187. verilog-auto-inst-param-value
  13188. verilog-auto-inst-sort
  13189. verilog-auto-inst-template-numbers
  13190. verilog-auto-inst-vector
  13191. verilog-auto-lineup
  13192. verilog-auto-newline
  13193. verilog-auto-output-ignore-regexp
  13194. verilog-auto-read-includes
  13195. verilog-auto-reset-blocking-in-non
  13196. verilog-auto-reset-widths
  13197. verilog-auto-save-policy
  13198. verilog-auto-sense-defines-constant
  13199. verilog-auto-sense-include-inputs
  13200. verilog-auto-star-expand
  13201. verilog-auto-star-save
  13202. verilog-auto-template-warn-unused
  13203. verilog-auto-tieoff-declaration
  13204. verilog-auto-tieoff-ignore-regexp
  13205. verilog-auto-unused-ignore-regexp
  13206. verilog-auto-wire-type
  13207. verilog-before-auto-hook
  13208. verilog-before-delete-auto-hook
  13209. verilog-before-getopt-flags-hook
  13210. verilog-before-save-font-hook
  13211. verilog-cache-enabled
  13212. verilog-case-fold
  13213. verilog-case-indent
  13214. verilog-cexp-indent
  13215. verilog-compiler
  13216. verilog-coverage
  13217. verilog-delete-auto-hook
  13218. verilog-getopt-flags-hook
  13219. verilog-highlight-grouping-keywords
  13220. verilog-highlight-includes
  13221. verilog-highlight-modules
  13222. verilog-highlight-p1800-keywords
  13223. verilog-highlight-translate-off
  13224. verilog-indent-begin-after-if
  13225. verilog-indent-declaration-macros
  13226. verilog-indent-level
  13227. verilog-indent-level-behavioral
  13228. verilog-indent-level-declaration
  13229. verilog-indent-level-directive
  13230. verilog-indent-level-module
  13231. verilog-indent-lists
  13232. verilog-library-directories
  13233. verilog-library-extensions
  13234. verilog-library-files
  13235. verilog-library-flags
  13236. verilog-linter
  13237. verilog-minimum-comment-distance
  13238. verilog-mode-hook
  13239. verilog-mode-release-emacs
  13240. verilog-mode-version
  13241. verilog-preprocessor
  13242. verilog-simulator
  13243. verilog-tab-always-indent
  13244. verilog-tab-to-comment
  13245. verilog-typedef-regexp
  13246. verilog-warn-fatal
  13247. )
  13248. nil nil
  13249. (concat "Hi Mac,
  13250. I want to report a bug.
  13251. Before I go further, I want to say that Verilog mode has changed my life.
  13252. I save so much time, my files are colored nicely, my co workers respect
  13253. my coding ability... until now. I'd really appreciate anything you
  13254. could do to help me out with this minor deficiency in the product.
  13255. I've taken a look at the Verilog-Mode FAQ at
  13256. http://www.veripool.org/verilog-mode-faq.html.
  13257. And, I've considered filing the bug on the issue tracker at
  13258. http://www.veripool.org/verilog-mode-bugs
  13259. since I realize that public bugs are easier for you to track,
  13260. and for others to search, but would prefer to email.
  13261. So, to reproduce the bug, start a fresh Emacs via " invocation-name "
  13262. -no-init-file -no-site-file'. In a new buffer, in Verilog mode, type
  13263. the code included below.
  13264. Given those lines, I expected [[Fill in here]] to happen;
  13265. but instead, [[Fill in here]] happens!.
  13266. == The code: =="))))
  13267. (provide 'verilog-mode)
  13268. ;; Local Variables:
  13269. ;; checkdoc-permit-comma-termination-flag:t
  13270. ;; checkdoc-force-docstrings-flag:nil
  13271. ;; indent-tabs-mode:nil
  13272. ;; End:
  13273. ;;; verilog-mode.el ends here