tc-xtensa.c 349 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200
  1. /* tc-xtensa.c -- Assemble Xtensa instructions.
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to
  14. the Free Software Foundation, 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "as.h"
  17. #include <limits.h>
  18. #include "sb.h"
  19. #include "safe-ctype.h"
  20. #include "tc-xtensa.h"
  21. #include "subsegs.h"
  22. #include "xtensa-relax.h"
  23. #include "dwarf2dbg.h"
  24. #include "xtensa-istack.h"
  25. #include "struc-symbol.h"
  26. #include "xtensa-config.h"
  27. /* Provide default values for new configuration settings. */
  28. #ifndef XSHAL_ABI
  29. #define XSHAL_ABI 0
  30. #endif
  31. #ifndef uint32
  32. #define uint32 unsigned int
  33. #endif
  34. #ifndef int32
  35. #define int32 signed int
  36. #endif
  37. /* Notes:
  38. Naming conventions (used somewhat inconsistently):
  39. The xtensa_ functions are exported
  40. The xg_ functions are internal
  41. We also have a couple of different extensibility mechanisms.
  42. 1) The idiom replacement:
  43. This is used when a line is first parsed to
  44. replace an instruction pattern with another instruction
  45. It is currently limited to replacements of instructions
  46. with constant operands.
  47. 2) The xtensa-relax.c mechanism that has stronger instruction
  48. replacement patterns. When an instruction's immediate field
  49. does not fit the next instruction sequence is attempted.
  50. In addition, "narrow" opcodes are supported this way. */
  51. /* Define characters with special meanings to GAS. */
  52. const char comment_chars[] = "#";
  53. const char line_comment_chars[] = "#";
  54. const char line_separator_chars[] = ";";
  55. const char EXP_CHARS[] = "eE";
  56. const char FLT_CHARS[] = "rRsSfFdDxXpP";
  57. /* Flags to indicate whether the hardware supports the density and
  58. absolute literals options. */
  59. bfd_boolean density_supported = XCHAL_HAVE_DENSITY;
  60. bfd_boolean absolute_literals_supported = XSHAL_USE_ABSOLUTE_LITERALS;
  61. static vliw_insn cur_vinsn;
  62. unsigned xtensa_num_pipe_stages;
  63. unsigned xtensa_fetch_width = XCHAL_INST_FETCH_WIDTH;
  64. static enum debug_info_type xt_saved_debug_type = DEBUG_NONE;
  65. /* Some functions are only valid in the front end. This variable
  66. allows us to assert that we haven't crossed over into the
  67. back end. */
  68. static bfd_boolean past_xtensa_end = FALSE;
  69. /* Flags for properties of the last instruction in a segment. */
  70. #define FLAG_IS_A0_WRITER 0x1
  71. #define FLAG_IS_BAD_LOOPEND 0x2
  72. /* We define a special segment names ".literal" to place literals
  73. into. The .fini and .init sections are special because they
  74. contain code that is moved together by the linker. We give them
  75. their own special .fini.literal and .init.literal sections. */
  76. #define LITERAL_SECTION_NAME xtensa_section_rename (".literal")
  77. #define LIT4_SECTION_NAME xtensa_section_rename (".lit4")
  78. #define INIT_SECTION_NAME xtensa_section_rename (".init")
  79. #define FINI_SECTION_NAME xtensa_section_rename (".fini")
  80. /* This type is used for the directive_stack to keep track of the
  81. state of the literal collection pools. If lit_prefix is set, it is
  82. used to determine the literal section names; otherwise, the literal
  83. sections are determined based on the current text section. The
  84. lit_seg and lit4_seg fields cache these literal sections, with the
  85. current_text_seg field used a tag to indicate whether the cached
  86. values are valid. */
  87. typedef struct lit_state_struct
  88. {
  89. char *lit_prefix;
  90. segT current_text_seg;
  91. segT lit_seg;
  92. segT lit4_seg;
  93. } lit_state;
  94. static lit_state default_lit_sections;
  95. /* We keep a list of literal segments. The seg_list type is the node
  96. for this list. The literal_head pointer is the head of the list,
  97. with the literal_head_h dummy node at the start. */
  98. typedef struct seg_list_struct
  99. {
  100. struct seg_list_struct *next;
  101. segT seg;
  102. } seg_list;
  103. static seg_list literal_head_h;
  104. static seg_list *literal_head = &literal_head_h;
  105. /* Lists of symbols. We keep a list of symbols that label the current
  106. instruction, so that we can adjust the symbols when inserting alignment
  107. for various instructions. We also keep a list of all the symbols on
  108. literals, so that we can fix up those symbols when the literals are
  109. later moved into the text sections. */
  110. typedef struct sym_list_struct
  111. {
  112. struct sym_list_struct *next;
  113. symbolS *sym;
  114. } sym_list;
  115. static sym_list *insn_labels = NULL;
  116. static sym_list *free_insn_labels = NULL;
  117. static sym_list *saved_insn_labels = NULL;
  118. static sym_list *literal_syms;
  119. /* Flags to determine whether to prefer const16 or l32r
  120. if both options are available. */
  121. int prefer_const16 = 0;
  122. int prefer_l32r = 0;
  123. /* Global flag to indicate when we are emitting literals. */
  124. int generating_literals = 0;
  125. /* The following PROPERTY table definitions are copied from
  126. <elf/xtensa.h> and must be kept in sync with the code there. */
  127. /* Flags in the property tables to specify whether blocks of memory
  128. are literals, instructions, data, or unreachable. For
  129. instructions, blocks that begin loop targets and branch targets are
  130. designated. Blocks that do not allow density, instruction
  131. reordering or transformation are also specified. Finally, for
  132. branch targets, branch target alignment priority is included.
  133. Alignment of the next block is specified in the current block
  134. and the size of the current block does not include any fill required
  135. to align to the next block. */
  136. #define XTENSA_PROP_LITERAL 0x00000001
  137. #define XTENSA_PROP_INSN 0x00000002
  138. #define XTENSA_PROP_DATA 0x00000004
  139. #define XTENSA_PROP_UNREACHABLE 0x00000008
  140. /* Instruction only properties at beginning of code. */
  141. #define XTENSA_PROP_INSN_LOOP_TARGET 0x00000010
  142. #define XTENSA_PROP_INSN_BRANCH_TARGET 0x00000020
  143. /* Instruction only properties about code. */
  144. #define XTENSA_PROP_INSN_NO_DENSITY 0x00000040
  145. #define XTENSA_PROP_INSN_NO_REORDER 0x00000080
  146. /* Historically, NO_TRANSFORM was a property of instructions,
  147. but it should apply to literals under certain circumstances. */
  148. #define XTENSA_PROP_NO_TRANSFORM 0x00000100
  149. /* Branch target alignment information. This transmits information
  150. to the linker optimization about the priority of aligning a
  151. particular block for branch target alignment: None, low priority,
  152. high priority, or required. These only need to be checked in
  153. instruction blocks marked as XTENSA_PROP_INSN_BRANCH_TARGET.
  154. Common usage is
  155. switch (GET_XTENSA_PROP_BT_ALIGN (flags))
  156. case XTENSA_PROP_BT_ALIGN_NONE:
  157. case XTENSA_PROP_BT_ALIGN_LOW:
  158. case XTENSA_PROP_BT_ALIGN_HIGH:
  159. case XTENSA_PROP_BT_ALIGN_REQUIRE:
  160. */
  161. #define XTENSA_PROP_BT_ALIGN_MASK 0x00000600
  162. /* No branch target alignment. */
  163. #define XTENSA_PROP_BT_ALIGN_NONE 0x0
  164. /* Low priority branch target alignment. */
  165. #define XTENSA_PROP_BT_ALIGN_LOW 0x1
  166. /* High priority branch target alignment. */
  167. #define XTENSA_PROP_BT_ALIGN_HIGH 0x2
  168. /* Required branch target alignment. */
  169. #define XTENSA_PROP_BT_ALIGN_REQUIRE 0x3
  170. #define GET_XTENSA_PROP_BT_ALIGN(flag) \
  171. (((unsigned) ((flag) & (XTENSA_PROP_BT_ALIGN_MASK))) >> 9)
  172. #define SET_XTENSA_PROP_BT_ALIGN(flag, align) \
  173. (((flag) & (~XTENSA_PROP_BT_ALIGN_MASK)) | \
  174. (((align) << 9) & XTENSA_PROP_BT_ALIGN_MASK))
  175. /* Alignment is specified in the block BEFORE the one that needs
  176. alignment. Up to 5 bits. Use GET_XTENSA_PROP_ALIGNMENT(flags) to
  177. get the required alignment specified as a power of 2. Use
  178. SET_XTENSA_PROP_ALIGNMENT(flags, pow2) to set the required
  179. alignment. Be careful of side effects since the SET will evaluate
  180. flags twice. Also, note that the SIZE of a block in the property
  181. table does not include the alignment size, so the alignment fill
  182. must be calculated to determine if two blocks are contiguous.
  183. TEXT_ALIGN is not currently implemented but is a placeholder for a
  184. possible future implementation. */
  185. #define XTENSA_PROP_ALIGN 0x00000800
  186. #define XTENSA_PROP_ALIGNMENT_MASK 0x0001f000
  187. #define GET_XTENSA_PROP_ALIGNMENT(flag) \
  188. (((unsigned) ((flag) & (XTENSA_PROP_ALIGNMENT_MASK))) >> 12)
  189. #define SET_XTENSA_PROP_ALIGNMENT(flag, align) \
  190. (((flag) & (~XTENSA_PROP_ALIGNMENT_MASK)) | \
  191. (((align) << 12) & XTENSA_PROP_ALIGNMENT_MASK))
  192. #define XTENSA_PROP_INSN_ABSLIT 0x00020000
  193. /* Structure for saving instruction and alignment per-fragment data
  194. that will be written to the object file. This structure is
  195. equivalent to the actual data that will be written out to the file
  196. but is easier to use. We provide a conversion to file flags
  197. in frag_flags_to_number. */
  198. typedef struct frag_flags_struct frag_flags;
  199. struct frag_flags_struct
  200. {
  201. /* is_literal should only be used after xtensa_move_literals.
  202. If you need to check if you are generating a literal fragment,
  203. then use the generating_literals global. */
  204. unsigned is_literal : 1;
  205. unsigned is_insn : 1;
  206. unsigned is_data : 1;
  207. unsigned is_unreachable : 1;
  208. /* is_specific_opcode implies no_transform. */
  209. unsigned is_no_transform : 1;
  210. struct
  211. {
  212. unsigned is_loop_target : 1;
  213. unsigned is_branch_target : 1; /* Branch targets have a priority. */
  214. unsigned bt_align_priority : 2;
  215. unsigned is_no_density : 1;
  216. /* no_longcalls flag does not need to be placed in the object file. */
  217. unsigned is_no_reorder : 1;
  218. /* Uses absolute literal addressing for l32r. */
  219. unsigned is_abslit : 1;
  220. } insn;
  221. unsigned is_align : 1;
  222. unsigned alignment : 5;
  223. };
  224. /* Structure for saving information about a block of property data
  225. for frags that have the same flags. */
  226. struct xtensa_block_info_struct
  227. {
  228. segT sec;
  229. bfd_vma offset;
  230. size_t size;
  231. frag_flags flags;
  232. struct xtensa_block_info_struct *next;
  233. };
  234. /* Structure for saving the current state before emitting literals. */
  235. typedef struct emit_state_struct
  236. {
  237. const char *name;
  238. segT now_seg;
  239. subsegT now_subseg;
  240. int generating_literals;
  241. } emit_state;
  242. /* Opcode placement information */
  243. typedef unsigned long long bitfield;
  244. #define bit_is_set(bit, bf) ((bf) & (0x01ll << (bit)))
  245. #define set_bit(bit, bf) ((bf) |= (0x01ll << (bit)))
  246. #define clear_bit(bit, bf) ((bf) &= ~(0x01ll << (bit)))
  247. #define MAX_FORMATS 32
  248. typedef struct op_placement_info_struct
  249. {
  250. int num_formats;
  251. /* A number describing how restrictive the issue is for this
  252. opcode. For example, an opcode that fits lots of different
  253. formats has a high freedom, as does an opcode that fits
  254. only one format but many slots in that format. The most
  255. restrictive is the opcode that fits only one slot in one
  256. format. */
  257. int issuef;
  258. xtensa_format narrowest;
  259. char narrowest_size;
  260. char narrowest_slot;
  261. /* formats is a bitfield with the Nth bit set
  262. if the opcode fits in the Nth xtensa_format. */
  263. bitfield formats;
  264. /* slots[N]'s Mth bit is set if the op fits in the
  265. Mth slot of the Nth xtensa_format. */
  266. bitfield slots[MAX_FORMATS];
  267. /* A count of the number of slots in a given format
  268. an op can fit (i.e., the bitcount of the slot field above). */
  269. char slots_in_format[MAX_FORMATS];
  270. } op_placement_info, *op_placement_info_table;
  271. op_placement_info_table op_placement_table;
  272. /* Extra expression types. */
  273. #define O_pltrel O_md1 /* like O_symbol but use a PLT reloc */
  274. #define O_hi16 O_md2 /* use high 16 bits of symbolic value */
  275. #define O_lo16 O_md3 /* use low 16 bits of symbolic value */
  276. #define O_pcrel O_md4 /* value is a PC-relative offset */
  277. #define O_tlsfunc O_md5 /* TLS_FUNC/TLSDESC_FN relocation */
  278. #define O_tlsarg O_md6 /* TLS_ARG/TLSDESC_ARG relocation */
  279. #define O_tlscall O_md7 /* TLS_CALL relocation */
  280. #define O_tpoff O_md8 /* TPOFF relocation */
  281. #define O_dtpoff O_md9 /* DTPOFF relocation */
  282. struct suffix_reloc_map
  283. {
  284. char *suffix;
  285. int length;
  286. bfd_reloc_code_real_type reloc;
  287. unsigned char operator;
  288. };
  289. #define SUFFIX_MAP(str, reloc, op) { str, sizeof (str) - 1, reloc, op }
  290. static struct suffix_reloc_map suffix_relocs[] =
  291. {
  292. SUFFIX_MAP ("l", BFD_RELOC_LO16, O_lo16),
  293. SUFFIX_MAP ("h", BFD_RELOC_HI16, O_hi16),
  294. SUFFIX_MAP ("plt", BFD_RELOC_XTENSA_PLT, O_pltrel),
  295. SUFFIX_MAP ("pcrel", BFD_RELOC_32_PCREL, O_pcrel),
  296. SUFFIX_MAP ("tlsfunc", BFD_RELOC_XTENSA_TLS_FUNC, O_tlsfunc),
  297. SUFFIX_MAP ("tlsarg", BFD_RELOC_XTENSA_TLS_ARG, O_tlsarg),
  298. SUFFIX_MAP ("tlscall", BFD_RELOC_XTENSA_TLS_CALL, O_tlscall),
  299. SUFFIX_MAP ("tpoff", BFD_RELOC_XTENSA_TLS_TPOFF, O_tpoff),
  300. SUFFIX_MAP ("dtpoff", BFD_RELOC_XTENSA_TLS_DTPOFF, O_dtpoff),
  301. { (char *) 0, 0, BFD_RELOC_UNUSED, 0 }
  302. };
  303. /* Directives. */
  304. typedef enum
  305. {
  306. directive_none = 0,
  307. directive_literal,
  308. directive_density,
  309. directive_transform,
  310. directive_freeregs,
  311. directive_longcalls,
  312. directive_literal_prefix,
  313. directive_schedule,
  314. directive_absolute_literals,
  315. directive_last_directive
  316. } directiveE;
  317. typedef struct
  318. {
  319. const char *name;
  320. bfd_boolean can_be_negated;
  321. } directive_infoS;
  322. const directive_infoS directive_info[] =
  323. {
  324. { "none", FALSE },
  325. { "literal", FALSE },
  326. { "density", TRUE },
  327. { "transform", TRUE },
  328. { "freeregs", FALSE },
  329. { "longcalls", TRUE },
  330. { "literal_prefix", FALSE },
  331. { "schedule", TRUE },
  332. { "absolute-literals", TRUE }
  333. };
  334. bfd_boolean directive_state[] =
  335. {
  336. FALSE, /* none */
  337. FALSE, /* literal */
  338. #if !XCHAL_HAVE_DENSITY
  339. FALSE, /* density */
  340. #else
  341. TRUE, /* density */
  342. #endif
  343. TRUE, /* transform */
  344. FALSE, /* freeregs */
  345. FALSE, /* longcalls */
  346. FALSE, /* literal_prefix */
  347. FALSE, /* schedule */
  348. #if XSHAL_USE_ABSOLUTE_LITERALS
  349. TRUE /* absolute_literals */
  350. #else
  351. FALSE /* absolute_literals */
  352. #endif
  353. };
  354. /* A circular list of all potential and actual literal pool locations
  355. in a segment. */
  356. struct litpool_frag
  357. {
  358. struct litpool_frag *next;
  359. struct litpool_frag *prev;
  360. fragS *fragP;
  361. addressT addr;
  362. short priority; /* 1, 2, or 3 -- 1 is highest */
  363. short original_priority;
  364. };
  365. /* Map a segment to its litpool_frag list. */
  366. struct litpool_seg
  367. {
  368. struct litpool_seg *next;
  369. asection *seg;
  370. struct litpool_frag frag_list;
  371. int frag_count; /* since last litpool location */
  372. };
  373. static struct litpool_seg litpool_seg_list;
  374. /* Directive functions. */
  375. static void xtensa_begin_directive (int);
  376. static void xtensa_end_directive (int);
  377. static void xtensa_literal_prefix (void);
  378. static void xtensa_literal_position (int);
  379. static void xtensa_literal_pseudo (int);
  380. static void xtensa_frequency_pseudo (int);
  381. static void xtensa_elf_cons (int);
  382. static void xtensa_leb128 (int);
  383. /* Parsing and Idiom Translation. */
  384. static bfd_reloc_code_real_type xtensa_elf_suffix (char **, expressionS *);
  385. /* Various Other Internal Functions. */
  386. extern bfd_boolean xg_is_single_relaxable_insn (TInsn *, TInsn *, bfd_boolean);
  387. static bfd_boolean xg_build_to_insn (TInsn *, TInsn *, BuildInstr *);
  388. static void xtensa_mark_literal_pool_location (void);
  389. static addressT get_expanded_loop_offset (xtensa_opcode);
  390. static fragS *get_literal_pool_location (segT);
  391. static void set_literal_pool_location (segT, fragS *);
  392. static void xtensa_set_frag_assembly_state (fragS *);
  393. static void finish_vinsn (vliw_insn *);
  394. static bfd_boolean emit_single_op (TInsn *);
  395. static int total_frag_text_expansion (fragS *);
  396. static bfd_boolean use_trampolines = TRUE;
  397. static void xtensa_check_frag_count (void);
  398. static void xtensa_create_trampoline_frag (bfd_boolean);
  399. static void xtensa_maybe_create_trampoline_frag (void);
  400. struct trampoline_frag;
  401. static int init_trampoline_frag (struct trampoline_frag *);
  402. static void xtensa_maybe_create_literal_pool_frag (bfd_boolean, bfd_boolean);
  403. static bfd_boolean auto_litpools = FALSE;
  404. static int auto_litpool_limit = 10000;
  405. /* Alignment Functions. */
  406. static int get_text_align_power (unsigned);
  407. static int get_text_align_max_fill_size (int, bfd_boolean, bfd_boolean);
  408. static int branch_align_power (segT);
  409. /* Helpers for xtensa_relax_frag(). */
  410. static long relax_frag_add_nop (fragS *);
  411. /* Accessors for additional per-subsegment information. */
  412. static unsigned get_last_insn_flags (segT, subsegT);
  413. static void set_last_insn_flags (segT, subsegT, unsigned, bfd_boolean);
  414. static float get_subseg_total_freq (segT, subsegT);
  415. static float get_subseg_target_freq (segT, subsegT);
  416. static void set_subseg_freq (segT, subsegT, float, float);
  417. /* Segment list functions. */
  418. static void xtensa_move_literals (void);
  419. static void xtensa_reorder_segments (void);
  420. static void xtensa_switch_to_literal_fragment (emit_state *);
  421. static void xtensa_switch_to_non_abs_literal_fragment (emit_state *);
  422. static void xtensa_switch_section_emit_state (emit_state *, segT, subsegT);
  423. static void xtensa_restore_emit_state (emit_state *);
  424. static segT cache_literal_section (bfd_boolean);
  425. /* Import from elf32-xtensa.c in BFD library. */
  426. extern asection *xtensa_make_property_section (asection *, const char *);
  427. /* op_placement_info functions. */
  428. static void init_op_placement_info_table (void);
  429. extern bfd_boolean opcode_fits_format_slot (xtensa_opcode, xtensa_format, int);
  430. static int xg_get_single_size (xtensa_opcode);
  431. static xtensa_format xg_get_single_format (xtensa_opcode);
  432. static int xg_get_single_slot (xtensa_opcode);
  433. /* TInsn and IStack functions. */
  434. static bfd_boolean tinsn_has_symbolic_operands (const TInsn *);
  435. static bfd_boolean tinsn_has_invalid_symbolic_operands (const TInsn *);
  436. static bfd_boolean tinsn_has_complex_operands (const TInsn *);
  437. static bfd_boolean tinsn_to_insnbuf (TInsn *, xtensa_insnbuf);
  438. static bfd_boolean tinsn_check_arguments (const TInsn *);
  439. static void tinsn_from_chars (TInsn *, char *, int);
  440. static void tinsn_immed_from_frag (TInsn *, fragS *, int);
  441. static int get_num_stack_text_bytes (IStack *);
  442. static int get_num_stack_literal_bytes (IStack *);
  443. static bfd_boolean tinsn_to_slotbuf (xtensa_format, int, TInsn *, xtensa_insnbuf);
  444. /* vliw_insn functions. */
  445. static void xg_init_vinsn (vliw_insn *);
  446. static void xg_copy_vinsn (vliw_insn *, vliw_insn *);
  447. static void xg_clear_vinsn (vliw_insn *);
  448. static bfd_boolean vinsn_has_specific_opcodes (vliw_insn *);
  449. static void xg_free_vinsn (vliw_insn *);
  450. static bfd_boolean vinsn_to_insnbuf
  451. (vliw_insn *, char *, fragS *, bfd_boolean);
  452. static void vinsn_from_chars (vliw_insn *, char *);
  453. /* Expression Utilities. */
  454. bfd_boolean expr_is_const (const expressionS *);
  455. offsetT get_expr_const (const expressionS *);
  456. void set_expr_const (expressionS *, offsetT);
  457. bfd_boolean expr_is_register (const expressionS *);
  458. offsetT get_expr_register (const expressionS *);
  459. void set_expr_symbol_offset (expressionS *, symbolS *, offsetT);
  460. bfd_boolean expr_is_equal (expressionS *, expressionS *);
  461. static void copy_expr (expressionS *, const expressionS *);
  462. /* Section renaming. */
  463. static void build_section_rename (const char *);
  464. /* ISA imported from bfd. */
  465. extern xtensa_isa xtensa_default_isa;
  466. extern int target_big_endian;
  467. static xtensa_opcode xtensa_addi_opcode;
  468. static xtensa_opcode xtensa_addmi_opcode;
  469. static xtensa_opcode xtensa_call0_opcode;
  470. static xtensa_opcode xtensa_call4_opcode;
  471. static xtensa_opcode xtensa_call8_opcode;
  472. static xtensa_opcode xtensa_call12_opcode;
  473. static xtensa_opcode xtensa_callx0_opcode;
  474. static xtensa_opcode xtensa_callx4_opcode;
  475. static xtensa_opcode xtensa_callx8_opcode;
  476. static xtensa_opcode xtensa_callx12_opcode;
  477. static xtensa_opcode xtensa_const16_opcode;
  478. static xtensa_opcode xtensa_entry_opcode;
  479. static xtensa_opcode xtensa_extui_opcode;
  480. static xtensa_opcode xtensa_movi_opcode;
  481. static xtensa_opcode xtensa_movi_n_opcode;
  482. static xtensa_opcode xtensa_isync_opcode;
  483. static xtensa_opcode xtensa_j_opcode;
  484. static xtensa_opcode xtensa_jx_opcode;
  485. static xtensa_opcode xtensa_l32r_opcode;
  486. static xtensa_opcode xtensa_loop_opcode;
  487. static xtensa_opcode xtensa_loopnez_opcode;
  488. static xtensa_opcode xtensa_loopgtz_opcode;
  489. static xtensa_opcode xtensa_nop_opcode;
  490. static xtensa_opcode xtensa_nop_n_opcode;
  491. static xtensa_opcode xtensa_or_opcode;
  492. static xtensa_opcode xtensa_ret_opcode;
  493. static xtensa_opcode xtensa_ret_n_opcode;
  494. static xtensa_opcode xtensa_retw_opcode;
  495. static xtensa_opcode xtensa_retw_n_opcode;
  496. static xtensa_opcode xtensa_rsr_lcount_opcode;
  497. static xtensa_opcode xtensa_waiti_opcode;
  498. static int config_max_slots = 0;
  499. /* Command-line Options. */
  500. bfd_boolean use_literal_section = TRUE;
  501. enum flix_level produce_flix = FLIX_ALL;
  502. static bfd_boolean align_targets = TRUE;
  503. static bfd_boolean warn_unaligned_branch_targets = FALSE;
  504. static bfd_boolean has_a0_b_retw = FALSE;
  505. static bfd_boolean workaround_a0_b_retw = FALSE;
  506. static bfd_boolean workaround_b_j_loop_end = FALSE;
  507. static bfd_boolean workaround_short_loop = FALSE;
  508. static bfd_boolean maybe_has_short_loop = FALSE;
  509. static bfd_boolean workaround_close_loop_end = FALSE;
  510. static bfd_boolean maybe_has_close_loop_end = FALSE;
  511. static bfd_boolean enforce_three_byte_loop_align = FALSE;
  512. /* When workaround_short_loops is TRUE, all loops with early exits must
  513. have at least 3 instructions. workaround_all_short_loops is a modifier
  514. to the workaround_short_loop flag. In addition to the
  515. workaround_short_loop actions, all straightline loopgtz and loopnez
  516. must have at least 3 instructions. */
  517. static bfd_boolean workaround_all_short_loops = FALSE;
  518. static void
  519. xtensa_setup_hw_workarounds (int earliest, int latest)
  520. {
  521. if (earliest > latest)
  522. as_fatal (_("illegal range of target hardware versions"));
  523. /* Enable all workarounds for pre-T1050.0 hardware. */
  524. if (earliest < 105000 || latest < 105000)
  525. {
  526. workaround_a0_b_retw |= TRUE;
  527. workaround_b_j_loop_end |= TRUE;
  528. workaround_short_loop |= TRUE;
  529. workaround_close_loop_end |= TRUE;
  530. workaround_all_short_loops |= TRUE;
  531. enforce_three_byte_loop_align = TRUE;
  532. }
  533. }
  534. enum
  535. {
  536. option_density = OPTION_MD_BASE,
  537. option_no_density,
  538. option_flix,
  539. option_no_generate_flix,
  540. option_no_flix,
  541. option_relax,
  542. option_no_relax,
  543. option_link_relax,
  544. option_no_link_relax,
  545. option_generics,
  546. option_no_generics,
  547. option_transform,
  548. option_no_transform,
  549. option_text_section_literals,
  550. option_no_text_section_literals,
  551. option_absolute_literals,
  552. option_no_absolute_literals,
  553. option_align_targets,
  554. option_no_align_targets,
  555. option_warn_unaligned_targets,
  556. option_longcalls,
  557. option_no_longcalls,
  558. option_workaround_a0_b_retw,
  559. option_no_workaround_a0_b_retw,
  560. option_workaround_b_j_loop_end,
  561. option_no_workaround_b_j_loop_end,
  562. option_workaround_short_loop,
  563. option_no_workaround_short_loop,
  564. option_workaround_all_short_loops,
  565. option_no_workaround_all_short_loops,
  566. option_workaround_close_loop_end,
  567. option_no_workaround_close_loop_end,
  568. option_no_workarounds,
  569. option_rename_section_name,
  570. option_prefer_l32r,
  571. option_prefer_const16,
  572. option_target_hardware,
  573. option_trampolines,
  574. option_no_trampolines,
  575. option_auto_litpools,
  576. option_no_auto_litpools,
  577. option_auto_litpool_limit,
  578. };
  579. const char *md_shortopts = "";
  580. struct option md_longopts[] =
  581. {
  582. { "density", no_argument, NULL, option_density },
  583. { "no-density", no_argument, NULL, option_no_density },
  584. { "flix", no_argument, NULL, option_flix },
  585. { "no-generate-flix", no_argument, NULL, option_no_generate_flix },
  586. { "no-allow-flix", no_argument, NULL, option_no_flix },
  587. /* Both "relax" and "generics" are deprecated and treated as equivalent
  588. to the "transform" option. */
  589. { "relax", no_argument, NULL, option_relax },
  590. { "no-relax", no_argument, NULL, option_no_relax },
  591. { "generics", no_argument, NULL, option_generics },
  592. { "no-generics", no_argument, NULL, option_no_generics },
  593. { "transform", no_argument, NULL, option_transform },
  594. { "no-transform", no_argument, NULL, option_no_transform },
  595. { "text-section-literals", no_argument, NULL, option_text_section_literals },
  596. { "no-text-section-literals", no_argument, NULL,
  597. option_no_text_section_literals },
  598. { "absolute-literals", no_argument, NULL, option_absolute_literals },
  599. { "no-absolute-literals", no_argument, NULL, option_no_absolute_literals },
  600. /* This option was changed from -align-target to -target-align
  601. because it conflicted with the "-al" option. */
  602. { "target-align", no_argument, NULL, option_align_targets },
  603. { "no-target-align", no_argument, NULL, option_no_align_targets },
  604. { "warn-unaligned-targets", no_argument, NULL,
  605. option_warn_unaligned_targets },
  606. { "longcalls", no_argument, NULL, option_longcalls },
  607. { "no-longcalls", no_argument, NULL, option_no_longcalls },
  608. { "no-workaround-a0-b-retw", no_argument, NULL,
  609. option_no_workaround_a0_b_retw },
  610. { "workaround-a0-b-retw", no_argument, NULL, option_workaround_a0_b_retw },
  611. { "no-workaround-b-j-loop-end", no_argument, NULL,
  612. option_no_workaround_b_j_loop_end },
  613. { "workaround-b-j-loop-end", no_argument, NULL,
  614. option_workaround_b_j_loop_end },
  615. { "no-workaround-short-loops", no_argument, NULL,
  616. option_no_workaround_short_loop },
  617. { "workaround-short-loops", no_argument, NULL,
  618. option_workaround_short_loop },
  619. { "no-workaround-all-short-loops", no_argument, NULL,
  620. option_no_workaround_all_short_loops },
  621. { "workaround-all-short-loop", no_argument, NULL,
  622. option_workaround_all_short_loops },
  623. { "prefer-l32r", no_argument, NULL, option_prefer_l32r },
  624. { "prefer-const16", no_argument, NULL, option_prefer_const16 },
  625. { "no-workarounds", no_argument, NULL, option_no_workarounds },
  626. { "no-workaround-close-loop-end", no_argument, NULL,
  627. option_no_workaround_close_loop_end },
  628. { "workaround-close-loop-end", no_argument, NULL,
  629. option_workaround_close_loop_end },
  630. { "rename-section", required_argument, NULL, option_rename_section_name },
  631. { "link-relax", no_argument, NULL, option_link_relax },
  632. { "no-link-relax", no_argument, NULL, option_no_link_relax },
  633. { "target-hardware", required_argument, NULL, option_target_hardware },
  634. { "trampolines", no_argument, NULL, option_trampolines },
  635. { "no-trampolines", no_argument, NULL, option_no_trampolines },
  636. { "auto-litpools", no_argument, NULL, option_auto_litpools },
  637. { "no-auto-litpools", no_argument, NULL, option_no_auto_litpools },
  638. { "auto-litpool-limit", required_argument, NULL, option_auto_litpool_limit },
  639. { NULL, no_argument, NULL, 0 }
  640. };
  641. size_t md_longopts_size = sizeof md_longopts;
  642. int
  643. md_parse_option (int c, char *arg)
  644. {
  645. switch (c)
  646. {
  647. case option_density:
  648. as_warn (_("--density option is ignored"));
  649. return 1;
  650. case option_no_density:
  651. as_warn (_("--no-density option is ignored"));
  652. return 1;
  653. case option_link_relax:
  654. linkrelax = 1;
  655. return 1;
  656. case option_no_link_relax:
  657. linkrelax = 0;
  658. return 1;
  659. case option_flix:
  660. produce_flix = FLIX_ALL;
  661. return 1;
  662. case option_no_generate_flix:
  663. produce_flix = FLIX_NO_GENERATE;
  664. return 1;
  665. case option_no_flix:
  666. produce_flix = FLIX_NONE;
  667. return 1;
  668. case option_generics:
  669. as_warn (_("--generics is deprecated; use --transform instead"));
  670. return md_parse_option (option_transform, arg);
  671. case option_no_generics:
  672. as_warn (_("--no-generics is deprecated; use --no-transform instead"));
  673. return md_parse_option (option_no_transform, arg);
  674. case option_relax:
  675. as_warn (_("--relax is deprecated; use --transform instead"));
  676. return md_parse_option (option_transform, arg);
  677. case option_no_relax:
  678. as_warn (_("--no-relax is deprecated; use --no-transform instead"));
  679. return md_parse_option (option_no_transform, arg);
  680. case option_longcalls:
  681. directive_state[directive_longcalls] = TRUE;
  682. return 1;
  683. case option_no_longcalls:
  684. directive_state[directive_longcalls] = FALSE;
  685. return 1;
  686. case option_text_section_literals:
  687. use_literal_section = FALSE;
  688. return 1;
  689. case option_no_text_section_literals:
  690. use_literal_section = TRUE;
  691. return 1;
  692. case option_absolute_literals:
  693. if (!absolute_literals_supported)
  694. {
  695. as_fatal (_("--absolute-literals option not supported in this Xtensa configuration"));
  696. return 0;
  697. }
  698. directive_state[directive_absolute_literals] = TRUE;
  699. return 1;
  700. case option_no_absolute_literals:
  701. directive_state[directive_absolute_literals] = FALSE;
  702. return 1;
  703. case option_workaround_a0_b_retw:
  704. workaround_a0_b_retw = TRUE;
  705. return 1;
  706. case option_no_workaround_a0_b_retw:
  707. workaround_a0_b_retw = FALSE;
  708. return 1;
  709. case option_workaround_b_j_loop_end:
  710. workaround_b_j_loop_end = TRUE;
  711. return 1;
  712. case option_no_workaround_b_j_loop_end:
  713. workaround_b_j_loop_end = FALSE;
  714. return 1;
  715. case option_workaround_short_loop:
  716. workaround_short_loop = TRUE;
  717. return 1;
  718. case option_no_workaround_short_loop:
  719. workaround_short_loop = FALSE;
  720. return 1;
  721. case option_workaround_all_short_loops:
  722. workaround_all_short_loops = TRUE;
  723. return 1;
  724. case option_no_workaround_all_short_loops:
  725. workaround_all_short_loops = FALSE;
  726. return 1;
  727. case option_workaround_close_loop_end:
  728. workaround_close_loop_end = TRUE;
  729. return 1;
  730. case option_no_workaround_close_loop_end:
  731. workaround_close_loop_end = FALSE;
  732. return 1;
  733. case option_no_workarounds:
  734. workaround_a0_b_retw = FALSE;
  735. workaround_b_j_loop_end = FALSE;
  736. workaround_short_loop = FALSE;
  737. workaround_all_short_loops = FALSE;
  738. workaround_close_loop_end = FALSE;
  739. return 1;
  740. case option_align_targets:
  741. align_targets = TRUE;
  742. return 1;
  743. case option_no_align_targets:
  744. align_targets = FALSE;
  745. return 1;
  746. case option_warn_unaligned_targets:
  747. warn_unaligned_branch_targets = TRUE;
  748. return 1;
  749. case option_rename_section_name:
  750. build_section_rename (arg);
  751. return 1;
  752. case 'Q':
  753. /* -Qy, -Qn: SVR4 arguments controlling whether a .comment section
  754. should be emitted or not. FIXME: Not implemented. */
  755. return 1;
  756. case option_prefer_l32r:
  757. if (prefer_const16)
  758. as_fatal (_("prefer-l32r conflicts with prefer-const16"));
  759. prefer_l32r = 1;
  760. return 1;
  761. case option_prefer_const16:
  762. if (prefer_l32r)
  763. as_fatal (_("prefer-const16 conflicts with prefer-l32r"));
  764. prefer_const16 = 1;
  765. return 1;
  766. case option_target_hardware:
  767. {
  768. int earliest, latest = 0;
  769. if (*arg == 0 || *arg == '-')
  770. as_fatal (_("invalid target hardware version"));
  771. earliest = strtol (arg, &arg, 0);
  772. if (*arg == 0)
  773. latest = earliest;
  774. else if (*arg == '-')
  775. {
  776. if (*++arg == 0)
  777. as_fatal (_("invalid target hardware version"));
  778. latest = strtol (arg, &arg, 0);
  779. }
  780. if (*arg != 0)
  781. as_fatal (_("invalid target hardware version"));
  782. xtensa_setup_hw_workarounds (earliest, latest);
  783. return 1;
  784. }
  785. case option_transform:
  786. /* This option has no affect other than to use the defaults,
  787. which are already set. */
  788. return 1;
  789. case option_no_transform:
  790. /* This option turns off all transformations of any kind.
  791. However, because we want to preserve the state of other
  792. directives, we only change its own field. Thus, before
  793. you perform any transformation, always check if transform
  794. is available. If you use the functions we provide for this
  795. purpose, you will be ok. */
  796. directive_state[directive_transform] = FALSE;
  797. return 1;
  798. case option_trampolines:
  799. use_trampolines = TRUE;
  800. return 1;
  801. case option_no_trampolines:
  802. use_trampolines = FALSE;
  803. return 1;
  804. case option_auto_litpools:
  805. auto_litpools = TRUE;
  806. use_literal_section = FALSE;
  807. return 1;
  808. case option_no_auto_litpools:
  809. auto_litpools = FALSE;
  810. auto_litpool_limit = -1;
  811. return 1;
  812. case option_auto_litpool_limit:
  813. {
  814. int value = 0;
  815. if (auto_litpool_limit < 0)
  816. as_fatal (_("no-auto-litpools is incompatible with auto-litpool-limit"));
  817. if (*arg == 0 || *arg == '-')
  818. as_fatal (_("invalid auto-litpool-limit argument"));
  819. value = strtol (arg, &arg, 10);
  820. if (*arg != 0)
  821. as_fatal (_("invalid auto-litpool-limit argument"));
  822. if (value < 100 || value > 10000)
  823. as_fatal (_("invalid auto-litpool-limit argument (range is 100-10000)"));
  824. auto_litpool_limit = value;
  825. auto_litpools = TRUE;
  826. use_literal_section = FALSE;
  827. return 1;
  828. }
  829. default:
  830. return 0;
  831. }
  832. }
  833. void
  834. md_show_usage (FILE *stream)
  835. {
  836. fputs ("\n\
  837. Xtensa options:\n\
  838. --[no-]text-section-literals\n\
  839. [Do not] put literals in the text section\n\
  840. --[no-]absolute-literals\n\
  841. [Do not] default to use non-PC-relative literals\n\
  842. --[no-]target-align [Do not] try to align branch targets\n\
  843. --[no-]longcalls [Do not] emit 32-bit call sequences\n\
  844. --[no-]transform [Do not] transform instructions\n\
  845. --flix both allow hand-written and generate flix bundles\n\
  846. --no-generate-flix allow hand-written but do not generate\n\
  847. flix bundles\n\
  848. --no-allow-flix neither allow hand-written nor generate\n\
  849. flix bundles\n\
  850. --rename-section old=new Rename section 'old' to 'new'\n\
  851. --[no-]trampolines [Do not] generate trampolines (jumps to jumps)\n\
  852. when jumps do not reach their targets\n\
  853. --[no-]auto-litpools [Do not] automatically create literal pools\n\
  854. --auto-litpool-limit=<value>\n\
  855. (range 100-10000) Maximum number of blocks of\n\
  856. instructions to emit between literal pool\n\
  857. locations; implies --auto-litpools flag\n", stream);
  858. }
  859. /* Functions related to the list of current label symbols. */
  860. static void
  861. xtensa_add_insn_label (symbolS *sym)
  862. {
  863. sym_list *l;
  864. if (!free_insn_labels)
  865. l = (sym_list *) xmalloc (sizeof (sym_list));
  866. else
  867. {
  868. l = free_insn_labels;
  869. free_insn_labels = l->next;
  870. }
  871. l->sym = sym;
  872. l->next = insn_labels;
  873. insn_labels = l;
  874. }
  875. static void
  876. xtensa_clear_insn_labels (void)
  877. {
  878. sym_list **pl;
  879. for (pl = &free_insn_labels; *pl != NULL; pl = &(*pl)->next)
  880. ;
  881. *pl = insn_labels;
  882. insn_labels = NULL;
  883. }
  884. static void
  885. xtensa_move_labels (fragS *new_frag, valueT new_offset)
  886. {
  887. sym_list *lit;
  888. for (lit = insn_labels; lit; lit = lit->next)
  889. {
  890. symbolS *lit_sym = lit->sym;
  891. S_SET_VALUE (lit_sym, new_offset);
  892. symbol_set_frag (lit_sym, new_frag);
  893. }
  894. }
  895. /* Directive data and functions. */
  896. typedef struct state_stackS_struct
  897. {
  898. directiveE directive;
  899. bfd_boolean negated;
  900. bfd_boolean old_state;
  901. const char *file;
  902. unsigned int line;
  903. const void *datum;
  904. struct state_stackS_struct *prev;
  905. } state_stackS;
  906. state_stackS *directive_state_stack;
  907. const pseudo_typeS md_pseudo_table[] =
  908. {
  909. { "align", s_align_bytes, 0 }, /* Defaulting is invalid (0). */
  910. { "literal_position", xtensa_literal_position, 0 },
  911. { "frame", s_ignore, 0 }, /* Formerly used for STABS debugging. */
  912. { "long", xtensa_elf_cons, 4 },
  913. { "word", xtensa_elf_cons, 4 },
  914. { "4byte", xtensa_elf_cons, 4 },
  915. { "short", xtensa_elf_cons, 2 },
  916. { "2byte", xtensa_elf_cons, 2 },
  917. { "sleb128", xtensa_leb128, 1},
  918. { "uleb128", xtensa_leb128, 0},
  919. { "begin", xtensa_begin_directive, 0 },
  920. { "end", xtensa_end_directive, 0 },
  921. { "literal", xtensa_literal_pseudo, 0 },
  922. { "frequency", xtensa_frequency_pseudo, 0 },
  923. { NULL, 0, 0 },
  924. };
  925. static bfd_boolean
  926. use_transform (void)
  927. {
  928. /* After md_end, you should be checking frag by frag, rather
  929. than state directives. */
  930. gas_assert (!past_xtensa_end);
  931. return directive_state[directive_transform];
  932. }
  933. static bfd_boolean
  934. do_align_targets (void)
  935. {
  936. /* Do not use this function after md_end; just look at align_targets
  937. instead. There is no target-align directive, so alignment is either
  938. enabled for all frags or not done at all. */
  939. gas_assert (!past_xtensa_end);
  940. return align_targets && use_transform ();
  941. }
  942. static void
  943. directive_push (directiveE directive, bfd_boolean negated, const void *datum)
  944. {
  945. char *file;
  946. unsigned int line;
  947. state_stackS *stack = (state_stackS *) xmalloc (sizeof (state_stackS));
  948. as_where (&file, &line);
  949. stack->directive = directive;
  950. stack->negated = negated;
  951. stack->old_state = directive_state[directive];
  952. stack->file = file;
  953. stack->line = line;
  954. stack->datum = datum;
  955. stack->prev = directive_state_stack;
  956. directive_state_stack = stack;
  957. directive_state[directive] = !negated;
  958. }
  959. static void
  960. directive_pop (directiveE *directive,
  961. bfd_boolean *negated,
  962. const char **file,
  963. unsigned int *line,
  964. const void **datum)
  965. {
  966. state_stackS *top = directive_state_stack;
  967. if (!directive_state_stack)
  968. {
  969. as_bad (_("unmatched end directive"));
  970. *directive = directive_none;
  971. return;
  972. }
  973. directive_state[directive_state_stack->directive] = top->old_state;
  974. *directive = top->directive;
  975. *negated = top->negated;
  976. *file = top->file;
  977. *line = top->line;
  978. *datum = top->datum;
  979. directive_state_stack = top->prev;
  980. free (top);
  981. }
  982. static void
  983. directive_balance (void)
  984. {
  985. while (directive_state_stack)
  986. {
  987. directiveE directive;
  988. bfd_boolean negated;
  989. const char *file;
  990. unsigned int line;
  991. const void *datum;
  992. directive_pop (&directive, &negated, &file, &line, &datum);
  993. as_warn_where ((char *) file, line,
  994. _(".begin directive with no matching .end directive"));
  995. }
  996. }
  997. static bfd_boolean
  998. inside_directive (directiveE dir)
  999. {
  1000. state_stackS *top = directive_state_stack;
  1001. while (top && top->directive != dir)
  1002. top = top->prev;
  1003. return (top != NULL);
  1004. }
  1005. static void
  1006. get_directive (directiveE *directive, bfd_boolean *negated)
  1007. {
  1008. int len;
  1009. unsigned i;
  1010. char *directive_string;
  1011. if (strncmp (input_line_pointer, "no-", 3) != 0)
  1012. *negated = FALSE;
  1013. else
  1014. {
  1015. *negated = TRUE;
  1016. input_line_pointer += 3;
  1017. }
  1018. len = strspn (input_line_pointer,
  1019. "abcdefghijklmnopqrstuvwxyz_-/0123456789.");
  1020. /* This code is a hack to make .begin [no-][generics|relax] exactly
  1021. equivalent to .begin [no-]transform. We should remove it when
  1022. we stop accepting those options. */
  1023. if (strncmp (input_line_pointer, "generics", strlen ("generics")) == 0)
  1024. {
  1025. as_warn (_("[no-]generics is deprecated; use [no-]transform instead"));
  1026. directive_string = "transform";
  1027. }
  1028. else if (strncmp (input_line_pointer, "relax", strlen ("relax")) == 0)
  1029. {
  1030. as_warn (_("[no-]relax is deprecated; use [no-]transform instead"));
  1031. directive_string = "transform";
  1032. }
  1033. else
  1034. directive_string = input_line_pointer;
  1035. for (i = 0; i < sizeof (directive_info) / sizeof (*directive_info); ++i)
  1036. {
  1037. if (strncmp (directive_string, directive_info[i].name, len) == 0)
  1038. {
  1039. input_line_pointer += len;
  1040. *directive = (directiveE) i;
  1041. if (*negated && !directive_info[i].can_be_negated)
  1042. as_bad (_("directive %s cannot be negated"),
  1043. directive_info[i].name);
  1044. return;
  1045. }
  1046. }
  1047. as_bad (_("unknown directive"));
  1048. *directive = (directiveE) XTENSA_UNDEFINED;
  1049. }
  1050. static void
  1051. xtensa_begin_directive (int ignore ATTRIBUTE_UNUSED)
  1052. {
  1053. directiveE directive;
  1054. bfd_boolean negated;
  1055. emit_state *state;
  1056. lit_state *ls;
  1057. get_directive (&directive, &negated);
  1058. if (directive == (directiveE) XTENSA_UNDEFINED)
  1059. {
  1060. discard_rest_of_line ();
  1061. return;
  1062. }
  1063. if (cur_vinsn.inside_bundle)
  1064. as_bad (_("directives are not valid inside bundles"));
  1065. switch (directive)
  1066. {
  1067. case directive_literal:
  1068. if (!inside_directive (directive_literal))
  1069. {
  1070. /* Previous labels go with whatever follows this directive, not with
  1071. the literal, so save them now. */
  1072. saved_insn_labels = insn_labels;
  1073. insn_labels = NULL;
  1074. }
  1075. as_warn (_(".begin literal is deprecated; use .literal instead"));
  1076. state = (emit_state *) xmalloc (sizeof (emit_state));
  1077. xtensa_switch_to_literal_fragment (state);
  1078. directive_push (directive_literal, negated, state);
  1079. break;
  1080. case directive_literal_prefix:
  1081. /* Have to flush pending output because a movi relaxed to an l32r
  1082. might produce a literal. */
  1083. md_flush_pending_output ();
  1084. /* Check to see if the current fragment is a literal
  1085. fragment. If it is, then this operation is not allowed. */
  1086. if (generating_literals)
  1087. {
  1088. as_bad (_("cannot set literal_prefix inside literal fragment"));
  1089. return;
  1090. }
  1091. /* Allocate the literal state for this section and push
  1092. onto the directive stack. */
  1093. ls = xmalloc (sizeof (lit_state));
  1094. gas_assert (ls);
  1095. *ls = default_lit_sections;
  1096. directive_push (directive_literal_prefix, negated, ls);
  1097. /* Process the new prefix. */
  1098. xtensa_literal_prefix ();
  1099. break;
  1100. case directive_freeregs:
  1101. /* This information is currently unused, but we'll accept the statement
  1102. and just discard the rest of the line. This won't check the syntax,
  1103. but it will accept every correct freeregs directive. */
  1104. input_line_pointer += strcspn (input_line_pointer, "\n");
  1105. directive_push (directive_freeregs, negated, 0);
  1106. break;
  1107. case directive_schedule:
  1108. md_flush_pending_output ();
  1109. frag_var (rs_fill, 0, 0, frag_now->fr_subtype,
  1110. frag_now->fr_symbol, frag_now->fr_offset, NULL);
  1111. directive_push (directive_schedule, negated, 0);
  1112. xtensa_set_frag_assembly_state (frag_now);
  1113. break;
  1114. case directive_density:
  1115. as_warn (_(".begin [no-]density is ignored"));
  1116. break;
  1117. case directive_absolute_literals:
  1118. md_flush_pending_output ();
  1119. if (!absolute_literals_supported && !negated)
  1120. {
  1121. as_warn (_("Xtensa absolute literals option not supported; ignored"));
  1122. break;
  1123. }
  1124. xtensa_set_frag_assembly_state (frag_now);
  1125. directive_push (directive, negated, 0);
  1126. break;
  1127. default:
  1128. md_flush_pending_output ();
  1129. xtensa_set_frag_assembly_state (frag_now);
  1130. directive_push (directive, negated, 0);
  1131. break;
  1132. }
  1133. demand_empty_rest_of_line ();
  1134. }
  1135. static void
  1136. xtensa_end_directive (int ignore ATTRIBUTE_UNUSED)
  1137. {
  1138. directiveE begin_directive, end_directive;
  1139. bfd_boolean begin_negated, end_negated;
  1140. const char *file;
  1141. unsigned int line;
  1142. emit_state *state;
  1143. emit_state **state_ptr;
  1144. lit_state *s;
  1145. if (cur_vinsn.inside_bundle)
  1146. as_bad (_("directives are not valid inside bundles"));
  1147. get_directive (&end_directive, &end_negated);
  1148. md_flush_pending_output ();
  1149. switch ((int) end_directive)
  1150. {
  1151. case XTENSA_UNDEFINED:
  1152. discard_rest_of_line ();
  1153. return;
  1154. case (int) directive_density:
  1155. as_warn (_(".end [no-]density is ignored"));
  1156. demand_empty_rest_of_line ();
  1157. break;
  1158. case (int) directive_absolute_literals:
  1159. if (!absolute_literals_supported && !end_negated)
  1160. {
  1161. as_warn (_("Xtensa absolute literals option not supported; ignored"));
  1162. demand_empty_rest_of_line ();
  1163. return;
  1164. }
  1165. break;
  1166. default:
  1167. break;
  1168. }
  1169. state_ptr = &state; /* use state_ptr to avoid type-punning warning */
  1170. directive_pop (&begin_directive, &begin_negated, &file, &line,
  1171. (const void **) state_ptr);
  1172. if (begin_directive != directive_none)
  1173. {
  1174. if (begin_directive != end_directive || begin_negated != end_negated)
  1175. {
  1176. as_bad (_("does not match begin %s%s at %s:%d"),
  1177. begin_negated ? "no-" : "",
  1178. directive_info[begin_directive].name, file, line);
  1179. }
  1180. else
  1181. {
  1182. switch (end_directive)
  1183. {
  1184. case directive_literal:
  1185. frag_var (rs_fill, 0, 0, 0, NULL, 0, NULL);
  1186. xtensa_restore_emit_state (state);
  1187. xtensa_set_frag_assembly_state (frag_now);
  1188. free (state);
  1189. if (!inside_directive (directive_literal))
  1190. {
  1191. /* Restore the list of current labels. */
  1192. xtensa_clear_insn_labels ();
  1193. insn_labels = saved_insn_labels;
  1194. }
  1195. break;
  1196. case directive_literal_prefix:
  1197. /* Restore the default collection sections from saved state. */
  1198. s = (lit_state *) state;
  1199. gas_assert (s);
  1200. default_lit_sections = *s;
  1201. /* Free the state storage. */
  1202. free (s->lit_prefix);
  1203. free (s);
  1204. break;
  1205. case directive_schedule:
  1206. case directive_freeregs:
  1207. break;
  1208. default:
  1209. xtensa_set_frag_assembly_state (frag_now);
  1210. break;
  1211. }
  1212. }
  1213. }
  1214. demand_empty_rest_of_line ();
  1215. }
  1216. /* Place an aligned literal fragment at the current location. */
  1217. static void
  1218. xtensa_literal_position (int ignore ATTRIBUTE_UNUSED)
  1219. {
  1220. md_flush_pending_output ();
  1221. if (inside_directive (directive_literal))
  1222. as_warn (_(".literal_position inside literal directive; ignoring"));
  1223. xtensa_mark_literal_pool_location ();
  1224. demand_empty_rest_of_line ();
  1225. xtensa_clear_insn_labels ();
  1226. }
  1227. /* Support .literal label, expr, ... */
  1228. static void
  1229. xtensa_literal_pseudo (int ignored ATTRIBUTE_UNUSED)
  1230. {
  1231. emit_state state;
  1232. char *p, *base_name;
  1233. char c;
  1234. segT dest_seg;
  1235. if (inside_directive (directive_literal))
  1236. {
  1237. as_bad (_(".literal not allowed inside .begin literal region"));
  1238. ignore_rest_of_line ();
  1239. return;
  1240. }
  1241. md_flush_pending_output ();
  1242. /* Previous labels go with whatever follows this directive, not with
  1243. the literal, so save them now. */
  1244. saved_insn_labels = insn_labels;
  1245. insn_labels = NULL;
  1246. /* If we are using text-section literals, then this is the right value... */
  1247. dest_seg = now_seg;
  1248. base_name = input_line_pointer;
  1249. xtensa_switch_to_literal_fragment (&state);
  1250. /* ...but if we aren't using text-section-literals, then we
  1251. need to put them in the section we just switched to. */
  1252. if (use_literal_section || directive_state[directive_absolute_literals])
  1253. dest_seg = now_seg;
  1254. /* FIXME, despite the previous comments, dest_seg is unused... */
  1255. (void) dest_seg;
  1256. /* All literals are aligned to four-byte boundaries. */
  1257. frag_align (2, 0, 0);
  1258. record_alignment (now_seg, 2);
  1259. c = get_symbol_name (&base_name);
  1260. /* Just after name is now '\0'. */
  1261. p = input_line_pointer;
  1262. *p = c;
  1263. SKIP_WHITESPACE_AFTER_NAME ();
  1264. if (*input_line_pointer != ',' && *input_line_pointer != ':')
  1265. {
  1266. as_bad (_("expected comma or colon after symbol name; "
  1267. "rest of line ignored"));
  1268. ignore_rest_of_line ();
  1269. xtensa_restore_emit_state (&state);
  1270. return;
  1271. }
  1272. *p = 0;
  1273. colon (base_name);
  1274. *p = c;
  1275. input_line_pointer++; /* skip ',' or ':' */
  1276. xtensa_elf_cons (4);
  1277. xtensa_restore_emit_state (&state);
  1278. /* Restore the list of current labels. */
  1279. xtensa_clear_insn_labels ();
  1280. insn_labels = saved_insn_labels;
  1281. }
  1282. static void
  1283. xtensa_literal_prefix (void)
  1284. {
  1285. char *name;
  1286. int len;
  1287. /* Parse the new prefix from the input_line_pointer. */
  1288. SKIP_WHITESPACE ();
  1289. len = strspn (input_line_pointer,
  1290. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  1291. "abcdefghijklmnopqrstuvwxyz_/0123456789.$");
  1292. /* Get a null-terminated copy of the name. */
  1293. name = xmalloc (len + 1);
  1294. gas_assert (name);
  1295. strncpy (name, input_line_pointer, len);
  1296. name[len] = 0;
  1297. /* Skip the name in the input line. */
  1298. input_line_pointer += len;
  1299. default_lit_sections.lit_prefix = name;
  1300. /* Clear cached literal sections, since the prefix has changed. */
  1301. default_lit_sections.lit_seg = NULL;
  1302. default_lit_sections.lit4_seg = NULL;
  1303. }
  1304. /* Support ".frequency branch_target_frequency fall_through_frequency". */
  1305. static void
  1306. xtensa_frequency_pseudo (int ignored ATTRIBUTE_UNUSED)
  1307. {
  1308. float fall_through_f, target_f;
  1309. fall_through_f = (float) strtod (input_line_pointer, &input_line_pointer);
  1310. if (fall_through_f < 0)
  1311. {
  1312. as_bad (_("fall through frequency must be greater than 0"));
  1313. ignore_rest_of_line ();
  1314. return;
  1315. }
  1316. target_f = (float) strtod (input_line_pointer, &input_line_pointer);
  1317. if (target_f < 0)
  1318. {
  1319. as_bad (_("branch target frequency must be greater than 0"));
  1320. ignore_rest_of_line ();
  1321. return;
  1322. }
  1323. set_subseg_freq (now_seg, now_subseg, target_f + fall_through_f, target_f);
  1324. demand_empty_rest_of_line ();
  1325. }
  1326. /* Like normal .long/.short/.word, except support @plt, etc.
  1327. Clobbers input_line_pointer, checks end-of-line. */
  1328. static void
  1329. xtensa_elf_cons (int nbytes)
  1330. {
  1331. expressionS exp;
  1332. bfd_reloc_code_real_type reloc;
  1333. md_flush_pending_output ();
  1334. if (cur_vinsn.inside_bundle)
  1335. as_bad (_("directives are not valid inside bundles"));
  1336. if (is_it_end_of_statement ())
  1337. {
  1338. demand_empty_rest_of_line ();
  1339. return;
  1340. }
  1341. do
  1342. {
  1343. expression (&exp);
  1344. if (exp.X_op == O_symbol
  1345. && *input_line_pointer == '@'
  1346. && ((reloc = xtensa_elf_suffix (&input_line_pointer, &exp))
  1347. != BFD_RELOC_NONE))
  1348. {
  1349. reloc_howto_type *reloc_howto =
  1350. bfd_reloc_type_lookup (stdoutput, reloc);
  1351. if (reloc == BFD_RELOC_UNUSED || !reloc_howto)
  1352. as_bad (_("unsupported relocation"));
  1353. else if ((reloc >= BFD_RELOC_XTENSA_SLOT0_OP
  1354. && reloc <= BFD_RELOC_XTENSA_SLOT14_OP)
  1355. || (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT
  1356. && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT))
  1357. as_bad (_("opcode-specific %s relocation used outside "
  1358. "an instruction"), reloc_howto->name);
  1359. else if (nbytes != (int) bfd_get_reloc_size (reloc_howto))
  1360. as_bad (_("%s relocations do not fit in %d bytes"),
  1361. reloc_howto->name, nbytes);
  1362. else if (reloc == BFD_RELOC_XTENSA_TLS_FUNC
  1363. || reloc == BFD_RELOC_XTENSA_TLS_ARG
  1364. || reloc == BFD_RELOC_XTENSA_TLS_CALL)
  1365. as_bad (_("invalid use of %s relocation"), reloc_howto->name);
  1366. else
  1367. {
  1368. char *p = frag_more ((int) nbytes);
  1369. xtensa_set_frag_assembly_state (frag_now);
  1370. fix_new_exp (frag_now, p - frag_now->fr_literal,
  1371. nbytes, &exp, reloc_howto->pc_relative, reloc);
  1372. }
  1373. }
  1374. else
  1375. {
  1376. xtensa_set_frag_assembly_state (frag_now);
  1377. emit_expr (&exp, (unsigned int) nbytes);
  1378. }
  1379. }
  1380. while (*input_line_pointer++ == ',');
  1381. input_line_pointer--; /* Put terminator back into stream. */
  1382. demand_empty_rest_of_line ();
  1383. }
  1384. static bfd_boolean is_leb128_expr;
  1385. static void
  1386. xtensa_leb128 (int sign)
  1387. {
  1388. is_leb128_expr = TRUE;
  1389. s_leb128 (sign);
  1390. is_leb128_expr = FALSE;
  1391. }
  1392. /* Parsing and Idiom Translation. */
  1393. /* Parse @plt, etc. and return the desired relocation. */
  1394. static bfd_reloc_code_real_type
  1395. xtensa_elf_suffix (char **str_p, expressionS *exp_p)
  1396. {
  1397. char ident[20];
  1398. char *str = *str_p;
  1399. char *str2;
  1400. int ch;
  1401. int len;
  1402. struct suffix_reloc_map *ptr;
  1403. if (*str++ != '@')
  1404. return BFD_RELOC_NONE;
  1405. for (ch = *str, str2 = ident;
  1406. (str2 < ident + sizeof (ident) - 1
  1407. && (ISALNUM (ch) || ch == '@'));
  1408. ch = *++str)
  1409. {
  1410. *str2++ = (ISLOWER (ch)) ? ch : TOLOWER (ch);
  1411. }
  1412. *str2 = '\0';
  1413. len = str2 - ident;
  1414. ch = ident[0];
  1415. for (ptr = &suffix_relocs[0]; ptr->length > 0; ptr++)
  1416. if (ch == ptr->suffix[0]
  1417. && len == ptr->length
  1418. && memcmp (ident, ptr->suffix, ptr->length) == 0)
  1419. {
  1420. /* Now check for "identifier@suffix+constant". */
  1421. if (*str == '-' || *str == '+')
  1422. {
  1423. char *orig_line = input_line_pointer;
  1424. expressionS new_exp;
  1425. input_line_pointer = str;
  1426. expression (&new_exp);
  1427. if (new_exp.X_op == O_constant)
  1428. {
  1429. exp_p->X_add_number += new_exp.X_add_number;
  1430. str = input_line_pointer;
  1431. }
  1432. if (&input_line_pointer != str_p)
  1433. input_line_pointer = orig_line;
  1434. }
  1435. *str_p = str;
  1436. return ptr->reloc;
  1437. }
  1438. return BFD_RELOC_UNUSED;
  1439. }
  1440. /* Find the matching operator type. */
  1441. static unsigned char
  1442. map_suffix_reloc_to_operator (bfd_reloc_code_real_type reloc)
  1443. {
  1444. struct suffix_reloc_map *sfx;
  1445. unsigned char operator = (unsigned char) -1;
  1446. for (sfx = &suffix_relocs[0]; sfx->suffix; sfx++)
  1447. {
  1448. if (sfx->reloc == reloc)
  1449. {
  1450. operator = sfx->operator;
  1451. break;
  1452. }
  1453. }
  1454. gas_assert (operator != (unsigned char) -1);
  1455. return operator;
  1456. }
  1457. /* Find the matching reloc type. */
  1458. static bfd_reloc_code_real_type
  1459. map_operator_to_reloc (unsigned char operator, bfd_boolean is_literal)
  1460. {
  1461. struct suffix_reloc_map *sfx;
  1462. bfd_reloc_code_real_type reloc = BFD_RELOC_UNUSED;
  1463. for (sfx = &suffix_relocs[0]; sfx->suffix; sfx++)
  1464. {
  1465. if (sfx->operator == operator)
  1466. {
  1467. reloc = sfx->reloc;
  1468. break;
  1469. }
  1470. }
  1471. if (is_literal)
  1472. {
  1473. if (reloc == BFD_RELOC_XTENSA_TLS_FUNC)
  1474. return BFD_RELOC_XTENSA_TLSDESC_FN;
  1475. else if (reloc == BFD_RELOC_XTENSA_TLS_ARG)
  1476. return BFD_RELOC_XTENSA_TLSDESC_ARG;
  1477. }
  1478. if (reloc == BFD_RELOC_UNUSED)
  1479. return BFD_RELOC_32;
  1480. return reloc;
  1481. }
  1482. static const char *
  1483. expression_end (const char *name)
  1484. {
  1485. while (1)
  1486. {
  1487. switch (*name)
  1488. {
  1489. case '}':
  1490. case ';':
  1491. case '\0':
  1492. case ',':
  1493. case ':':
  1494. return name;
  1495. case ' ':
  1496. case '\t':
  1497. ++name;
  1498. continue;
  1499. default:
  1500. return 0;
  1501. }
  1502. }
  1503. }
  1504. #define ERROR_REG_NUM ((unsigned) -1)
  1505. static unsigned
  1506. tc_get_register (const char *prefix)
  1507. {
  1508. unsigned reg;
  1509. const char *next_expr;
  1510. const char *old_line_pointer;
  1511. SKIP_WHITESPACE ();
  1512. old_line_pointer = input_line_pointer;
  1513. if (*input_line_pointer == '$')
  1514. ++input_line_pointer;
  1515. /* Accept "sp" as a synonym for "a1". */
  1516. if (input_line_pointer[0] == 's' && input_line_pointer[1] == 'p'
  1517. && expression_end (input_line_pointer + 2))
  1518. {
  1519. input_line_pointer += 2;
  1520. return 1; /* AR[1] */
  1521. }
  1522. while (*input_line_pointer++ == *prefix++)
  1523. ;
  1524. --input_line_pointer;
  1525. --prefix;
  1526. if (*prefix)
  1527. {
  1528. as_bad (_("bad register name: %s"), old_line_pointer);
  1529. return ERROR_REG_NUM;
  1530. }
  1531. if (!ISDIGIT ((unsigned char) *input_line_pointer))
  1532. {
  1533. as_bad (_("bad register number: %s"), input_line_pointer);
  1534. return ERROR_REG_NUM;
  1535. }
  1536. reg = 0;
  1537. while (ISDIGIT ((int) *input_line_pointer))
  1538. reg = reg * 10 + *input_line_pointer++ - '0';
  1539. if (!(next_expr = expression_end (input_line_pointer)))
  1540. {
  1541. as_bad (_("bad register name: %s"), old_line_pointer);
  1542. return ERROR_REG_NUM;
  1543. }
  1544. input_line_pointer = (char *) next_expr;
  1545. return reg;
  1546. }
  1547. static void
  1548. expression_maybe_register (xtensa_opcode opc, int opnd, expressionS *tok)
  1549. {
  1550. xtensa_isa isa = xtensa_default_isa;
  1551. /* Check if this is an immediate operand. */
  1552. if (xtensa_operand_is_register (isa, opc, opnd) == 0)
  1553. {
  1554. bfd_reloc_code_real_type reloc;
  1555. segT t = expression (tok);
  1556. if (t == absolute_section
  1557. && xtensa_operand_is_PCrelative (isa, opc, opnd) == 1)
  1558. {
  1559. gas_assert (tok->X_op == O_constant);
  1560. tok->X_op = O_symbol;
  1561. tok->X_add_symbol = &abs_symbol;
  1562. }
  1563. if ((tok->X_op == O_constant || tok->X_op == O_symbol)
  1564. && ((reloc = xtensa_elf_suffix (&input_line_pointer, tok))
  1565. != BFD_RELOC_NONE))
  1566. {
  1567. switch (reloc)
  1568. {
  1569. case BFD_RELOC_LO16:
  1570. if (tok->X_op == O_constant)
  1571. {
  1572. tok->X_add_number &= 0xffff;
  1573. return;
  1574. }
  1575. break;
  1576. case BFD_RELOC_HI16:
  1577. if (tok->X_op == O_constant)
  1578. {
  1579. tok->X_add_number = ((unsigned) tok->X_add_number) >> 16;
  1580. return;
  1581. }
  1582. break;
  1583. case BFD_RELOC_UNUSED:
  1584. as_bad (_("unsupported relocation"));
  1585. return;
  1586. case BFD_RELOC_32_PCREL:
  1587. as_bad (_("pcrel relocation not allowed in an instruction"));
  1588. return;
  1589. default:
  1590. break;
  1591. }
  1592. tok->X_op = map_suffix_reloc_to_operator (reloc);
  1593. }
  1594. }
  1595. else
  1596. {
  1597. xtensa_regfile opnd_rf = xtensa_operand_regfile (isa, opc, opnd);
  1598. unsigned reg = tc_get_register (xtensa_regfile_shortname (isa, opnd_rf));
  1599. if (reg != ERROR_REG_NUM) /* Already errored */
  1600. {
  1601. uint32 buf = reg;
  1602. if (xtensa_operand_encode (isa, opc, opnd, &buf))
  1603. as_bad (_("register number out of range"));
  1604. }
  1605. tok->X_op = O_register;
  1606. tok->X_add_symbol = 0;
  1607. tok->X_add_number = reg;
  1608. }
  1609. }
  1610. /* Split up the arguments for an opcode or pseudo-op. */
  1611. static int
  1612. tokenize_arguments (char **args, char *str)
  1613. {
  1614. char *old_input_line_pointer;
  1615. bfd_boolean saw_comma = FALSE;
  1616. bfd_boolean saw_arg = FALSE;
  1617. bfd_boolean saw_colon = FALSE;
  1618. int num_args = 0;
  1619. char *arg_end, *arg;
  1620. int arg_len;
  1621. /* Save and restore input_line_pointer around this function. */
  1622. old_input_line_pointer = input_line_pointer;
  1623. input_line_pointer = str;
  1624. while (*input_line_pointer)
  1625. {
  1626. SKIP_WHITESPACE ();
  1627. switch (*input_line_pointer)
  1628. {
  1629. case '\0':
  1630. case '}':
  1631. goto fini;
  1632. case ':':
  1633. input_line_pointer++;
  1634. if (saw_comma || saw_colon || !saw_arg)
  1635. goto err;
  1636. saw_colon = TRUE;
  1637. break;
  1638. case ',':
  1639. input_line_pointer++;
  1640. if (saw_comma || saw_colon || !saw_arg)
  1641. goto err;
  1642. saw_comma = TRUE;
  1643. break;
  1644. default:
  1645. if (!saw_comma && !saw_colon && saw_arg)
  1646. goto err;
  1647. arg_end = input_line_pointer + 1;
  1648. while (!expression_end (arg_end))
  1649. arg_end += 1;
  1650. arg_len = arg_end - input_line_pointer;
  1651. arg = (char *) xmalloc ((saw_colon ? 1 : 0) + arg_len + 1);
  1652. args[num_args] = arg;
  1653. if (saw_colon)
  1654. *arg++ = ':';
  1655. strncpy (arg, input_line_pointer, arg_len);
  1656. arg[arg_len] = '\0';
  1657. input_line_pointer = arg_end;
  1658. num_args += 1;
  1659. saw_comma = FALSE;
  1660. saw_colon = FALSE;
  1661. saw_arg = TRUE;
  1662. break;
  1663. }
  1664. }
  1665. fini:
  1666. if (saw_comma || saw_colon)
  1667. goto err;
  1668. input_line_pointer = old_input_line_pointer;
  1669. return num_args;
  1670. err:
  1671. if (saw_comma)
  1672. as_bad (_("extra comma"));
  1673. else if (saw_colon)
  1674. as_bad (_("extra colon"));
  1675. else if (!saw_arg)
  1676. as_bad (_("missing argument"));
  1677. else
  1678. as_bad (_("missing comma or colon"));
  1679. input_line_pointer = old_input_line_pointer;
  1680. return -1;
  1681. }
  1682. /* Parse the arguments to an opcode. Return TRUE on error. */
  1683. static bfd_boolean
  1684. parse_arguments (TInsn *insn, int num_args, char **arg_strings)
  1685. {
  1686. expressionS *tok, *last_tok;
  1687. xtensa_opcode opcode = insn->opcode;
  1688. bfd_boolean had_error = TRUE;
  1689. xtensa_isa isa = xtensa_default_isa;
  1690. int n, num_regs = 0;
  1691. int opcode_operand_count;
  1692. int opnd_cnt, last_opnd_cnt;
  1693. unsigned int next_reg = 0;
  1694. char *old_input_line_pointer;
  1695. if (insn->insn_type == ITYPE_LITERAL)
  1696. opcode_operand_count = 1;
  1697. else
  1698. opcode_operand_count = xtensa_opcode_num_operands (isa, opcode);
  1699. tok = insn->tok;
  1700. memset (tok, 0, sizeof (*tok) * MAX_INSN_ARGS);
  1701. /* Save and restore input_line_pointer around this function. */
  1702. old_input_line_pointer = input_line_pointer;
  1703. last_tok = 0;
  1704. last_opnd_cnt = -1;
  1705. opnd_cnt = 0;
  1706. /* Skip invisible operands. */
  1707. while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0)
  1708. {
  1709. opnd_cnt += 1;
  1710. tok++;
  1711. }
  1712. for (n = 0; n < num_args; n++)
  1713. {
  1714. input_line_pointer = arg_strings[n];
  1715. if (*input_line_pointer == ':')
  1716. {
  1717. xtensa_regfile opnd_rf;
  1718. input_line_pointer++;
  1719. if (num_regs == 0)
  1720. goto err;
  1721. gas_assert (opnd_cnt > 0);
  1722. num_regs--;
  1723. opnd_rf = xtensa_operand_regfile (isa, opcode, last_opnd_cnt);
  1724. if (next_reg
  1725. != tc_get_register (xtensa_regfile_shortname (isa, opnd_rf)))
  1726. as_warn (_("incorrect register number, ignoring"));
  1727. next_reg++;
  1728. }
  1729. else
  1730. {
  1731. if (opnd_cnt >= opcode_operand_count)
  1732. {
  1733. as_warn (_("too many arguments"));
  1734. goto err;
  1735. }
  1736. gas_assert (opnd_cnt < MAX_INSN_ARGS);
  1737. expression_maybe_register (opcode, opnd_cnt, tok);
  1738. next_reg = tok->X_add_number + 1;
  1739. if (tok->X_op == O_illegal || tok->X_op == O_absent)
  1740. goto err;
  1741. if (xtensa_operand_is_register (isa, opcode, opnd_cnt) == 1)
  1742. {
  1743. num_regs = xtensa_operand_num_regs (isa, opcode, opnd_cnt) - 1;
  1744. /* minus 1 because we are seeing one right now */
  1745. }
  1746. else
  1747. num_regs = 0;
  1748. last_tok = tok;
  1749. last_opnd_cnt = opnd_cnt;
  1750. demand_empty_rest_of_line ();
  1751. do
  1752. {
  1753. opnd_cnt += 1;
  1754. tok++;
  1755. }
  1756. while (xtensa_operand_is_visible (isa, opcode, opnd_cnt) == 0);
  1757. }
  1758. }
  1759. if (num_regs > 0 && ((int) next_reg != last_tok->X_add_number + 1))
  1760. goto err;
  1761. insn->ntok = tok - insn->tok;
  1762. had_error = FALSE;
  1763. err:
  1764. input_line_pointer = old_input_line_pointer;
  1765. return had_error;
  1766. }
  1767. static int
  1768. get_invisible_operands (TInsn *insn)
  1769. {
  1770. xtensa_isa isa = xtensa_default_isa;
  1771. static xtensa_insnbuf slotbuf = NULL;
  1772. xtensa_format fmt;
  1773. xtensa_opcode opc = insn->opcode;
  1774. int slot, opnd, fmt_found;
  1775. unsigned val;
  1776. if (!slotbuf)
  1777. slotbuf = xtensa_insnbuf_alloc (isa);
  1778. /* Find format/slot where this can be encoded. */
  1779. fmt_found = 0;
  1780. slot = 0;
  1781. for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
  1782. {
  1783. for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
  1784. {
  1785. if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opc) == 0)
  1786. {
  1787. fmt_found = 1;
  1788. break;
  1789. }
  1790. }
  1791. if (fmt_found) break;
  1792. }
  1793. if (!fmt_found)
  1794. {
  1795. as_bad (_("cannot encode opcode \"%s\""), xtensa_opcode_name (isa, opc));
  1796. return -1;
  1797. }
  1798. /* First encode all the visible operands
  1799. (to deal with shared field operands). */
  1800. for (opnd = 0; opnd < insn->ntok; opnd++)
  1801. {
  1802. if (xtensa_operand_is_visible (isa, opc, opnd) == 1
  1803. && (insn->tok[opnd].X_op == O_register
  1804. || insn->tok[opnd].X_op == O_constant))
  1805. {
  1806. val = insn->tok[opnd].X_add_number;
  1807. xtensa_operand_encode (isa, opc, opnd, &val);
  1808. xtensa_operand_set_field (isa, opc, opnd, fmt, slot, slotbuf, val);
  1809. }
  1810. }
  1811. /* Then pull out the values for the invisible ones. */
  1812. for (opnd = 0; opnd < insn->ntok; opnd++)
  1813. {
  1814. if (xtensa_operand_is_visible (isa, opc, opnd) == 0)
  1815. {
  1816. xtensa_operand_get_field (isa, opc, opnd, fmt, slot, slotbuf, &val);
  1817. xtensa_operand_decode (isa, opc, opnd, &val);
  1818. insn->tok[opnd].X_add_number = val;
  1819. if (xtensa_operand_is_register (isa, opc, opnd) == 1)
  1820. insn->tok[opnd].X_op = O_register;
  1821. else
  1822. insn->tok[opnd].X_op = O_constant;
  1823. }
  1824. }
  1825. return 0;
  1826. }
  1827. static void
  1828. xg_reverse_shift_count (char **cnt_argp)
  1829. {
  1830. char *cnt_arg, *new_arg;
  1831. cnt_arg = *cnt_argp;
  1832. /* replace the argument with "31-(argument)" */
  1833. new_arg = (char *) xmalloc (strlen (cnt_arg) + 6);
  1834. sprintf (new_arg, "31-(%s)", cnt_arg);
  1835. free (cnt_arg);
  1836. *cnt_argp = new_arg;
  1837. }
  1838. /* If "arg" is a constant expression, return non-zero with the value
  1839. in *valp. */
  1840. static int
  1841. xg_arg_is_constant (char *arg, offsetT *valp)
  1842. {
  1843. expressionS exp;
  1844. char *save_ptr = input_line_pointer;
  1845. input_line_pointer = arg;
  1846. expression (&exp);
  1847. input_line_pointer = save_ptr;
  1848. if (exp.X_op == O_constant)
  1849. {
  1850. *valp = exp.X_add_number;
  1851. return 1;
  1852. }
  1853. return 0;
  1854. }
  1855. static void
  1856. xg_replace_opname (char **popname, char *newop)
  1857. {
  1858. free (*popname);
  1859. *popname = (char *) xmalloc (strlen (newop) + 1);
  1860. strcpy (*popname, newop);
  1861. }
  1862. static int
  1863. xg_check_num_args (int *pnum_args,
  1864. int expected_num,
  1865. char *opname,
  1866. char **arg_strings)
  1867. {
  1868. int num_args = *pnum_args;
  1869. if (num_args < expected_num)
  1870. {
  1871. as_bad (_("not enough operands (%d) for '%s'; expected %d"),
  1872. num_args, opname, expected_num);
  1873. return -1;
  1874. }
  1875. if (num_args > expected_num)
  1876. {
  1877. as_warn (_("too many operands (%d) for '%s'; expected %d"),
  1878. num_args, opname, expected_num);
  1879. while (num_args-- > expected_num)
  1880. {
  1881. free (arg_strings[num_args]);
  1882. arg_strings[num_args] = 0;
  1883. }
  1884. *pnum_args = expected_num;
  1885. return -1;
  1886. }
  1887. return 0;
  1888. }
  1889. /* If the register is not specified as part of the opcode,
  1890. then get it from the operand and move it to the opcode. */
  1891. static int
  1892. xg_translate_sysreg_op (char **popname, int *pnum_args, char **arg_strings)
  1893. {
  1894. xtensa_isa isa = xtensa_default_isa;
  1895. xtensa_sysreg sr;
  1896. char *opname, *new_opname;
  1897. const char *sr_name;
  1898. int is_user, is_write;
  1899. opname = *popname;
  1900. if (*opname == '_')
  1901. opname += 1;
  1902. is_user = (opname[1] == 'u');
  1903. is_write = (opname[0] == 'w');
  1904. /* Opname == [rw]ur or [rwx]sr... */
  1905. if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
  1906. return -1;
  1907. /* Check if the argument is a symbolic register name. */
  1908. sr = xtensa_sysreg_lookup_name (isa, arg_strings[1]);
  1909. /* Handle WSR to "INTSET" as a special case. */
  1910. if (sr == XTENSA_UNDEFINED && is_write && !is_user
  1911. && !strcasecmp (arg_strings[1], "intset"))
  1912. sr = xtensa_sysreg_lookup_name (isa, "interrupt");
  1913. if (sr == XTENSA_UNDEFINED
  1914. || (xtensa_sysreg_is_user (isa, sr) == 1) != is_user)
  1915. {
  1916. /* Maybe it's a register number.... */
  1917. offsetT val;
  1918. if (!xg_arg_is_constant (arg_strings[1], &val))
  1919. {
  1920. as_bad (_("invalid register '%s' for '%s' instruction"),
  1921. arg_strings[1], opname);
  1922. return -1;
  1923. }
  1924. sr = xtensa_sysreg_lookup (isa, val, is_user);
  1925. if (sr == XTENSA_UNDEFINED)
  1926. {
  1927. as_bad (_("invalid register number (%ld) for '%s' instruction"),
  1928. (long) val, opname);
  1929. return -1;
  1930. }
  1931. }
  1932. /* Remove the last argument, which is now part of the opcode. */
  1933. free (arg_strings[1]);
  1934. arg_strings[1] = 0;
  1935. *pnum_args = 1;
  1936. /* Translate the opcode. */
  1937. sr_name = xtensa_sysreg_name (isa, sr);
  1938. /* Another special case for "WSR.INTSET".... */
  1939. if (is_write && !is_user && !strcasecmp ("interrupt", sr_name))
  1940. sr_name = "intset";
  1941. new_opname = (char *) xmalloc (strlen (sr_name) + 6);
  1942. sprintf (new_opname, "%s.%s", *popname, sr_name);
  1943. free (*popname);
  1944. *popname = new_opname;
  1945. return 0;
  1946. }
  1947. static int
  1948. xtensa_translate_old_userreg_ops (char **popname)
  1949. {
  1950. xtensa_isa isa = xtensa_default_isa;
  1951. xtensa_sysreg sr;
  1952. char *opname, *new_opname;
  1953. const char *sr_name;
  1954. bfd_boolean has_underbar = FALSE;
  1955. opname = *popname;
  1956. if (opname[0] == '_')
  1957. {
  1958. has_underbar = TRUE;
  1959. opname += 1;
  1960. }
  1961. sr = xtensa_sysreg_lookup_name (isa, opname + 1);
  1962. if (sr != XTENSA_UNDEFINED)
  1963. {
  1964. /* The new default name ("nnn") is different from the old default
  1965. name ("URnnn"). The old default is handled below, and we don't
  1966. want to recognize [RW]nnn, so do nothing if the name is the (new)
  1967. default. */
  1968. static char namebuf[10];
  1969. sprintf (namebuf, "%d", xtensa_sysreg_number (isa, sr));
  1970. if (strcmp (namebuf, opname + 1) == 0)
  1971. return 0;
  1972. }
  1973. else
  1974. {
  1975. offsetT val;
  1976. char *end;
  1977. /* Only continue if the reg name is "URnnn". */
  1978. if (opname[1] != 'u' || opname[2] != 'r')
  1979. return 0;
  1980. val = strtoul (opname + 3, &end, 10);
  1981. if (*end != '\0')
  1982. return 0;
  1983. sr = xtensa_sysreg_lookup (isa, val, 1);
  1984. if (sr == XTENSA_UNDEFINED)
  1985. {
  1986. as_bad (_("invalid register number (%ld) for '%s'"),
  1987. (long) val, opname);
  1988. return -1;
  1989. }
  1990. }
  1991. /* Translate the opcode. */
  1992. sr_name = xtensa_sysreg_name (isa, sr);
  1993. new_opname = (char *) xmalloc (strlen (sr_name) + 6);
  1994. sprintf (new_opname, "%s%cur.%s", (has_underbar ? "_" : ""),
  1995. opname[0], sr_name);
  1996. free (*popname);
  1997. *popname = new_opname;
  1998. return 0;
  1999. }
  2000. static int
  2001. xtensa_translate_zero_immed (char *old_op,
  2002. char *new_op,
  2003. char **popname,
  2004. int *pnum_args,
  2005. char **arg_strings)
  2006. {
  2007. char *opname;
  2008. offsetT val;
  2009. opname = *popname;
  2010. gas_assert (opname[0] != '_');
  2011. if (strcmp (opname, old_op) != 0)
  2012. return 0;
  2013. if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
  2014. return -1;
  2015. if (xg_arg_is_constant (arg_strings[1], &val) && val == 0)
  2016. {
  2017. xg_replace_opname (popname, new_op);
  2018. free (arg_strings[1]);
  2019. arg_strings[1] = arg_strings[2];
  2020. arg_strings[2] = 0;
  2021. *pnum_args = 2;
  2022. }
  2023. return 0;
  2024. }
  2025. /* If the instruction is an idiom (i.e., a built-in macro), translate it.
  2026. Returns non-zero if an error was found. */
  2027. static int
  2028. xg_translate_idioms (char **popname, int *pnum_args, char **arg_strings)
  2029. {
  2030. char *opname = *popname;
  2031. bfd_boolean has_underbar = FALSE;
  2032. if (*opname == '_')
  2033. {
  2034. has_underbar = TRUE;
  2035. opname += 1;
  2036. }
  2037. if (strcmp (opname, "mov") == 0)
  2038. {
  2039. if (use_transform () && !has_underbar && density_supported)
  2040. xg_replace_opname (popname, "mov.n");
  2041. else
  2042. {
  2043. if (xg_check_num_args (pnum_args, 2, opname, arg_strings))
  2044. return -1;
  2045. xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
  2046. arg_strings[2] = (char *) xmalloc (strlen (arg_strings[1]) + 1);
  2047. strcpy (arg_strings[2], arg_strings[1]);
  2048. *pnum_args = 3;
  2049. }
  2050. return 0;
  2051. }
  2052. if (strcmp (opname, "bbsi.l") == 0)
  2053. {
  2054. if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
  2055. return -1;
  2056. xg_replace_opname (popname, (has_underbar ? "_bbsi" : "bbsi"));
  2057. if (target_big_endian)
  2058. xg_reverse_shift_count (&arg_strings[1]);
  2059. return 0;
  2060. }
  2061. if (strcmp (opname, "bbci.l") == 0)
  2062. {
  2063. if (xg_check_num_args (pnum_args, 3, opname, arg_strings))
  2064. return -1;
  2065. xg_replace_opname (popname, (has_underbar ? "_bbci" : "bbci"));
  2066. if (target_big_endian)
  2067. xg_reverse_shift_count (&arg_strings[1]);
  2068. return 0;
  2069. }
  2070. /* Don't do anything special with NOPs inside FLIX instructions. They
  2071. are handled elsewhere. Real NOP instructions are always available
  2072. in configurations with FLIX, so this should never be an issue but
  2073. check for it anyway. */
  2074. if (!cur_vinsn.inside_bundle && xtensa_nop_opcode == XTENSA_UNDEFINED
  2075. && strcmp (opname, "nop") == 0)
  2076. {
  2077. if (use_transform () && !has_underbar && density_supported)
  2078. xg_replace_opname (popname, "nop.n");
  2079. else
  2080. {
  2081. if (xg_check_num_args (pnum_args, 0, opname, arg_strings))
  2082. return -1;
  2083. xg_replace_opname (popname, (has_underbar ? "_or" : "or"));
  2084. arg_strings[0] = (char *) xmalloc (3);
  2085. arg_strings[1] = (char *) xmalloc (3);
  2086. arg_strings[2] = (char *) xmalloc (3);
  2087. strcpy (arg_strings[0], "a1");
  2088. strcpy (arg_strings[1], "a1");
  2089. strcpy (arg_strings[2], "a1");
  2090. *pnum_args = 3;
  2091. }
  2092. return 0;
  2093. }
  2094. /* Recognize [RW]UR and [RWX]SR. */
  2095. if ((((opname[0] == 'r' || opname[0] == 'w')
  2096. && (opname[1] == 'u' || opname[1] == 's'))
  2097. || (opname[0] == 'x' && opname[1] == 's'))
  2098. && opname[2] == 'r'
  2099. && opname[3] == '\0')
  2100. return xg_translate_sysreg_op (popname, pnum_args, arg_strings);
  2101. /* Backward compatibility for RUR and WUR: Recognize [RW]UR<nnn> and
  2102. [RW]<name> if <name> is the non-default name of a user register. */
  2103. if ((opname[0] == 'r' || opname[0] == 'w')
  2104. && xtensa_opcode_lookup (xtensa_default_isa, opname) == XTENSA_UNDEFINED)
  2105. return xtensa_translate_old_userreg_ops (popname);
  2106. /* Relax branches that don't allow comparisons against an immediate value
  2107. of zero to the corresponding branches with implicit zero immediates. */
  2108. if (!has_underbar && use_transform ())
  2109. {
  2110. if (xtensa_translate_zero_immed ("bnei", "bnez", popname,
  2111. pnum_args, arg_strings))
  2112. return -1;
  2113. if (xtensa_translate_zero_immed ("beqi", "beqz", popname,
  2114. pnum_args, arg_strings))
  2115. return -1;
  2116. if (xtensa_translate_zero_immed ("bgei", "bgez", popname,
  2117. pnum_args, arg_strings))
  2118. return -1;
  2119. if (xtensa_translate_zero_immed ("blti", "bltz", popname,
  2120. pnum_args, arg_strings))
  2121. return -1;
  2122. }
  2123. return 0;
  2124. }
  2125. /* Functions for dealing with the Xtensa ISA. */
  2126. /* Currently the assembler only allows us to use a single target per
  2127. fragment. Because of this, only one operand for a given
  2128. instruction may be symbolic. If there is a PC-relative operand,
  2129. the last one is chosen. Otherwise, the result is the number of the
  2130. last immediate operand, and if there are none of those, we fail and
  2131. return -1. */
  2132. static int
  2133. get_relaxable_immed (xtensa_opcode opcode)
  2134. {
  2135. int last_immed = -1;
  2136. int noperands, opi;
  2137. if (opcode == XTENSA_UNDEFINED)
  2138. return -1;
  2139. noperands = xtensa_opcode_num_operands (xtensa_default_isa, opcode);
  2140. for (opi = noperands - 1; opi >= 0; opi--)
  2141. {
  2142. if (xtensa_operand_is_visible (xtensa_default_isa, opcode, opi) == 0)
  2143. continue;
  2144. if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, opi) == 1)
  2145. return opi;
  2146. if (last_immed == -1
  2147. && xtensa_operand_is_register (xtensa_default_isa, opcode, opi) == 0)
  2148. last_immed = opi;
  2149. }
  2150. return last_immed;
  2151. }
  2152. static xtensa_opcode
  2153. get_opcode_from_buf (const char *buf, int slot)
  2154. {
  2155. static xtensa_insnbuf insnbuf = NULL;
  2156. static xtensa_insnbuf slotbuf = NULL;
  2157. xtensa_isa isa = xtensa_default_isa;
  2158. xtensa_format fmt;
  2159. if (!insnbuf)
  2160. {
  2161. insnbuf = xtensa_insnbuf_alloc (isa);
  2162. slotbuf = xtensa_insnbuf_alloc (isa);
  2163. }
  2164. xtensa_insnbuf_from_chars (isa, insnbuf, (const unsigned char *) buf, 0);
  2165. fmt = xtensa_format_decode (isa, insnbuf);
  2166. if (fmt == XTENSA_UNDEFINED)
  2167. return XTENSA_UNDEFINED;
  2168. if (slot >= xtensa_format_num_slots (isa, fmt))
  2169. return XTENSA_UNDEFINED;
  2170. xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
  2171. return xtensa_opcode_decode (isa, fmt, slot, slotbuf);
  2172. }
  2173. #ifdef TENSILICA_DEBUG
  2174. /* For debugging, print out the mapping of opcode numbers to opcodes. */
  2175. static void
  2176. xtensa_print_insn_table (void)
  2177. {
  2178. int num_opcodes, num_operands;
  2179. xtensa_opcode opcode;
  2180. xtensa_isa isa = xtensa_default_isa;
  2181. num_opcodes = xtensa_isa_num_opcodes (xtensa_default_isa);
  2182. for (opcode = 0; opcode < num_opcodes; opcode++)
  2183. {
  2184. int opn;
  2185. fprintf (stderr, "%d: %s: ", opcode, xtensa_opcode_name (isa, opcode));
  2186. num_operands = xtensa_opcode_num_operands (isa, opcode);
  2187. for (opn = 0; opn < num_operands; opn++)
  2188. {
  2189. if (xtensa_operand_is_visible (isa, opcode, opn) == 0)
  2190. continue;
  2191. if (xtensa_operand_is_register (isa, opcode, opn) == 1)
  2192. {
  2193. xtensa_regfile opnd_rf =
  2194. xtensa_operand_regfile (isa, opcode, opn);
  2195. fprintf (stderr, "%s ", xtensa_regfile_shortname (isa, opnd_rf));
  2196. }
  2197. else if (xtensa_operand_is_PCrelative (isa, opcode, opn) == 1)
  2198. fputs ("[lLr] ", stderr);
  2199. else
  2200. fputs ("i ", stderr);
  2201. }
  2202. fprintf (stderr, "\n");
  2203. }
  2204. }
  2205. static void
  2206. print_vliw_insn (xtensa_insnbuf vbuf)
  2207. {
  2208. xtensa_isa isa = xtensa_default_isa;
  2209. xtensa_format f = xtensa_format_decode (isa, vbuf);
  2210. xtensa_insnbuf sbuf = xtensa_insnbuf_alloc (isa);
  2211. int op;
  2212. fprintf (stderr, "format = %d\n", f);
  2213. for (op = 0; op < xtensa_format_num_slots (isa, f); op++)
  2214. {
  2215. xtensa_opcode opcode;
  2216. const char *opname;
  2217. int operands;
  2218. xtensa_format_get_slot (isa, f, op, vbuf, sbuf);
  2219. opcode = xtensa_opcode_decode (isa, f, op, sbuf);
  2220. opname = xtensa_opcode_name (isa, opcode);
  2221. fprintf (stderr, "op in slot %i is %s;\n", op, opname);
  2222. fprintf (stderr, " operands = ");
  2223. for (operands = 0;
  2224. operands < xtensa_opcode_num_operands (isa, opcode);
  2225. operands++)
  2226. {
  2227. unsigned int val;
  2228. if (xtensa_operand_is_visible (isa, opcode, operands) == 0)
  2229. continue;
  2230. xtensa_operand_get_field (isa, opcode, operands, f, op, sbuf, &val);
  2231. xtensa_operand_decode (isa, opcode, operands, &val);
  2232. fprintf (stderr, "%d ", val);
  2233. }
  2234. fprintf (stderr, "\n");
  2235. }
  2236. xtensa_insnbuf_free (isa, sbuf);
  2237. }
  2238. #endif /* TENSILICA_DEBUG */
  2239. static bfd_boolean
  2240. is_direct_call_opcode (xtensa_opcode opcode)
  2241. {
  2242. xtensa_isa isa = xtensa_default_isa;
  2243. int n, num_operands;
  2244. if (xtensa_opcode_is_call (isa, opcode) != 1)
  2245. return FALSE;
  2246. num_operands = xtensa_opcode_num_operands (isa, opcode);
  2247. for (n = 0; n < num_operands; n++)
  2248. {
  2249. if (xtensa_operand_is_register (isa, opcode, n) == 0
  2250. && xtensa_operand_is_PCrelative (isa, opcode, n) == 1)
  2251. return TRUE;
  2252. }
  2253. return FALSE;
  2254. }
  2255. /* Convert from BFD relocation type code to slot and operand number.
  2256. Returns non-zero on failure. */
  2257. static int
  2258. decode_reloc (bfd_reloc_code_real_type reloc, int *slot, bfd_boolean *is_alt)
  2259. {
  2260. if (reloc >= BFD_RELOC_XTENSA_SLOT0_OP
  2261. && reloc <= BFD_RELOC_XTENSA_SLOT14_OP)
  2262. {
  2263. *slot = reloc - BFD_RELOC_XTENSA_SLOT0_OP;
  2264. *is_alt = FALSE;
  2265. }
  2266. else if (reloc >= BFD_RELOC_XTENSA_SLOT0_ALT
  2267. && reloc <= BFD_RELOC_XTENSA_SLOT14_ALT)
  2268. {
  2269. *slot = reloc - BFD_RELOC_XTENSA_SLOT0_ALT;
  2270. *is_alt = TRUE;
  2271. }
  2272. else
  2273. return -1;
  2274. return 0;
  2275. }
  2276. /* Convert from slot number to BFD relocation type code for the
  2277. standard PC-relative relocations. Return BFD_RELOC_NONE on
  2278. failure. */
  2279. static bfd_reloc_code_real_type
  2280. encode_reloc (int slot)
  2281. {
  2282. if (slot < 0 || slot > 14)
  2283. return BFD_RELOC_NONE;
  2284. return BFD_RELOC_XTENSA_SLOT0_OP + slot;
  2285. }
  2286. /* Convert from slot numbers to BFD relocation type code for the
  2287. "alternate" relocations. Return BFD_RELOC_NONE on failure. */
  2288. static bfd_reloc_code_real_type
  2289. encode_alt_reloc (int slot)
  2290. {
  2291. if (slot < 0 || slot > 14)
  2292. return BFD_RELOC_NONE;
  2293. return BFD_RELOC_XTENSA_SLOT0_ALT + slot;
  2294. }
  2295. static void
  2296. xtensa_insnbuf_set_operand (xtensa_insnbuf slotbuf,
  2297. xtensa_format fmt,
  2298. int slot,
  2299. xtensa_opcode opcode,
  2300. int operand,
  2301. uint32 value,
  2302. const char *file,
  2303. unsigned int line)
  2304. {
  2305. uint32 valbuf = value;
  2306. if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf))
  2307. {
  2308. if (xtensa_operand_is_PCrelative (xtensa_default_isa, opcode, operand)
  2309. == 1)
  2310. as_bad_where ((char *) file, line,
  2311. _("operand %d of '%s' has out of range value '%u'"),
  2312. operand + 1,
  2313. xtensa_opcode_name (xtensa_default_isa, opcode),
  2314. value);
  2315. else
  2316. as_bad_where ((char *) file, line,
  2317. _("operand %d of '%s' has invalid value '%u'"),
  2318. operand + 1,
  2319. xtensa_opcode_name (xtensa_default_isa, opcode),
  2320. value);
  2321. return;
  2322. }
  2323. xtensa_operand_set_field (xtensa_default_isa, opcode, operand, fmt, slot,
  2324. slotbuf, valbuf);
  2325. }
  2326. static uint32
  2327. xtensa_insnbuf_get_operand (xtensa_insnbuf slotbuf,
  2328. xtensa_format fmt,
  2329. int slot,
  2330. xtensa_opcode opcode,
  2331. int opnum)
  2332. {
  2333. uint32 val = 0;
  2334. (void) xtensa_operand_get_field (xtensa_default_isa, opcode, opnum,
  2335. fmt, slot, slotbuf, &val);
  2336. (void) xtensa_operand_decode (xtensa_default_isa, opcode, opnum, &val);
  2337. return val;
  2338. }
  2339. /* Checks for rules from xtensa-relax tables. */
  2340. /* The routine xg_instruction_matches_option_term must return TRUE
  2341. when a given option term is true. The meaning of all of the option
  2342. terms is given interpretation by this function. */
  2343. static bfd_boolean
  2344. xg_instruction_matches_option_term (TInsn *insn, const ReqOrOption *option)
  2345. {
  2346. if (strcmp (option->option_name, "realnop") == 0
  2347. || strncmp (option->option_name, "IsaUse", 6) == 0)
  2348. {
  2349. /* These conditions were evaluated statically when building the
  2350. relaxation table. There's no need to reevaluate them now. */
  2351. return TRUE;
  2352. }
  2353. else if (strcmp (option->option_name, "FREEREG") == 0)
  2354. return insn->extra_arg.X_op == O_register;
  2355. else
  2356. {
  2357. as_fatal (_("internal error: unknown option name '%s'"),
  2358. option->option_name);
  2359. }
  2360. }
  2361. static bfd_boolean
  2362. xg_instruction_matches_or_options (TInsn *insn,
  2363. const ReqOrOptionList *or_option)
  2364. {
  2365. const ReqOrOption *option;
  2366. /* Must match each of the AND terms. */
  2367. for (option = or_option; option != NULL; option = option->next)
  2368. {
  2369. if (xg_instruction_matches_option_term (insn, option))
  2370. return TRUE;
  2371. }
  2372. return FALSE;
  2373. }
  2374. static bfd_boolean
  2375. xg_instruction_matches_options (TInsn *insn, const ReqOptionList *options)
  2376. {
  2377. const ReqOption *req_options;
  2378. /* Must match each of the AND terms. */
  2379. for (req_options = options;
  2380. req_options != NULL;
  2381. req_options = req_options->next)
  2382. {
  2383. /* Must match one of the OR clauses. */
  2384. if (!xg_instruction_matches_or_options (insn,
  2385. req_options->or_option_terms))
  2386. return FALSE;
  2387. }
  2388. return TRUE;
  2389. }
  2390. /* Return the transition rule that matches or NULL if none matches. */
  2391. static bfd_boolean
  2392. xg_instruction_matches_rule (TInsn *insn, TransitionRule *rule)
  2393. {
  2394. PreconditionList *condition_l;
  2395. if (rule->opcode != insn->opcode)
  2396. return FALSE;
  2397. for (condition_l = rule->conditions;
  2398. condition_l != NULL;
  2399. condition_l = condition_l->next)
  2400. {
  2401. expressionS *exp1;
  2402. expressionS *exp2;
  2403. Precondition *cond = condition_l->precond;
  2404. switch (cond->typ)
  2405. {
  2406. case OP_CONSTANT:
  2407. /* The expression must be the constant. */
  2408. gas_assert (cond->op_num < insn->ntok);
  2409. exp1 = &insn->tok[cond->op_num];
  2410. if (expr_is_const (exp1))
  2411. {
  2412. switch (cond->cmp)
  2413. {
  2414. case OP_EQUAL:
  2415. if (get_expr_const (exp1) != cond->op_data)
  2416. return FALSE;
  2417. break;
  2418. case OP_NOTEQUAL:
  2419. if (get_expr_const (exp1) == cond->op_data)
  2420. return FALSE;
  2421. break;
  2422. default:
  2423. return FALSE;
  2424. }
  2425. }
  2426. else if (expr_is_register (exp1))
  2427. {
  2428. switch (cond->cmp)
  2429. {
  2430. case OP_EQUAL:
  2431. if (get_expr_register (exp1) != cond->op_data)
  2432. return FALSE;
  2433. break;
  2434. case OP_NOTEQUAL:
  2435. if (get_expr_register (exp1) == cond->op_data)
  2436. return FALSE;
  2437. break;
  2438. default:
  2439. return FALSE;
  2440. }
  2441. }
  2442. else
  2443. return FALSE;
  2444. break;
  2445. case OP_OPERAND:
  2446. gas_assert (cond->op_num < insn->ntok);
  2447. gas_assert (cond->op_data < insn->ntok);
  2448. exp1 = &insn->tok[cond->op_num];
  2449. exp2 = &insn->tok[cond->op_data];
  2450. switch (cond->cmp)
  2451. {
  2452. case OP_EQUAL:
  2453. if (!expr_is_equal (exp1, exp2))
  2454. return FALSE;
  2455. break;
  2456. case OP_NOTEQUAL:
  2457. if (expr_is_equal (exp1, exp2))
  2458. return FALSE;
  2459. break;
  2460. }
  2461. break;
  2462. case OP_LITERAL:
  2463. case OP_LABEL:
  2464. default:
  2465. return FALSE;
  2466. }
  2467. }
  2468. if (!xg_instruction_matches_options (insn, rule->options))
  2469. return FALSE;
  2470. return TRUE;
  2471. }
  2472. static int
  2473. transition_rule_cmp (const TransitionRule *a, const TransitionRule *b)
  2474. {
  2475. bfd_boolean a_greater = FALSE;
  2476. bfd_boolean b_greater = FALSE;
  2477. ReqOptionList *l_a = a->options;
  2478. ReqOptionList *l_b = b->options;
  2479. /* We only care if they both are the same except for
  2480. a const16 vs. an l32r. */
  2481. while (l_a && l_b && ((l_a->next == NULL) == (l_b->next == NULL)))
  2482. {
  2483. ReqOrOptionList *l_or_a = l_a->or_option_terms;
  2484. ReqOrOptionList *l_or_b = l_b->or_option_terms;
  2485. while (l_or_a && l_or_b && ((l_a->next == NULL) == (l_b->next == NULL)))
  2486. {
  2487. if (l_or_a->is_true != l_or_b->is_true)
  2488. return 0;
  2489. if (strcmp (l_or_a->option_name, l_or_b->option_name) != 0)
  2490. {
  2491. /* This is the case we care about. */
  2492. if (strcmp (l_or_a->option_name, "IsaUseConst16") == 0
  2493. && strcmp (l_or_b->option_name, "IsaUseL32R") == 0)
  2494. {
  2495. if (prefer_const16)
  2496. a_greater = TRUE;
  2497. else
  2498. b_greater = TRUE;
  2499. }
  2500. else if (strcmp (l_or_a->option_name, "IsaUseL32R") == 0
  2501. && strcmp (l_or_b->option_name, "IsaUseConst16") == 0)
  2502. {
  2503. if (prefer_const16)
  2504. b_greater = TRUE;
  2505. else
  2506. a_greater = TRUE;
  2507. }
  2508. else
  2509. return 0;
  2510. }
  2511. l_or_a = l_or_a->next;
  2512. l_or_b = l_or_b->next;
  2513. }
  2514. if (l_or_a || l_or_b)
  2515. return 0;
  2516. l_a = l_a->next;
  2517. l_b = l_b->next;
  2518. }
  2519. if (l_a || l_b)
  2520. return 0;
  2521. /* Incomparable if the substitution was used differently in two cases. */
  2522. if (a_greater && b_greater)
  2523. return 0;
  2524. if (b_greater)
  2525. return 1;
  2526. if (a_greater)
  2527. return -1;
  2528. return 0;
  2529. }
  2530. static TransitionRule *
  2531. xg_instruction_match (TInsn *insn)
  2532. {
  2533. TransitionTable *table = xg_build_simplify_table (&transition_rule_cmp);
  2534. TransitionList *l;
  2535. gas_assert (insn->opcode < table->num_opcodes);
  2536. /* Walk through all of the possible transitions. */
  2537. for (l = table->table[insn->opcode]; l != NULL; l = l->next)
  2538. {
  2539. TransitionRule *rule = l->rule;
  2540. if (xg_instruction_matches_rule (insn, rule))
  2541. return rule;
  2542. }
  2543. return NULL;
  2544. }
  2545. /* Various Other Internal Functions. */
  2546. static bfd_boolean
  2547. is_unique_insn_expansion (TransitionRule *r)
  2548. {
  2549. if (!r->to_instr || r->to_instr->next != NULL)
  2550. return FALSE;
  2551. if (r->to_instr->typ != INSTR_INSTR)
  2552. return FALSE;
  2553. return TRUE;
  2554. }
  2555. /* Check if there is exactly one relaxation for INSN that converts it to
  2556. another instruction of equal or larger size. If so, and if TARG is
  2557. non-null, go ahead and generate the relaxed instruction into TARG. If
  2558. NARROW_ONLY is true, then only consider relaxations that widen a narrow
  2559. instruction, i.e., ignore relaxations that convert to an instruction of
  2560. equal size. In some contexts where this function is used, only
  2561. a single widening is allowed and the NARROW_ONLY argument is used to
  2562. exclude cases like ADDI being "widened" to an ADDMI, which may
  2563. later be relaxed to an ADDMI/ADDI pair. */
  2564. bfd_boolean
  2565. xg_is_single_relaxable_insn (TInsn *insn, TInsn *targ, bfd_boolean narrow_only)
  2566. {
  2567. TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
  2568. TransitionList *l;
  2569. TransitionRule *match = 0;
  2570. gas_assert (insn->insn_type == ITYPE_INSN);
  2571. gas_assert (insn->opcode < table->num_opcodes);
  2572. for (l = table->table[insn->opcode]; l != NULL; l = l->next)
  2573. {
  2574. TransitionRule *rule = l->rule;
  2575. if (xg_instruction_matches_rule (insn, rule)
  2576. && is_unique_insn_expansion (rule)
  2577. && (xg_get_single_size (insn->opcode) + (narrow_only ? 1 : 0)
  2578. <= xg_get_single_size (rule->to_instr->opcode)))
  2579. {
  2580. if (match)
  2581. return FALSE;
  2582. match = rule;
  2583. }
  2584. }
  2585. if (!match)
  2586. return FALSE;
  2587. if (targ)
  2588. xg_build_to_insn (targ, insn, match->to_instr);
  2589. return TRUE;
  2590. }
  2591. /* Return the maximum number of bytes this opcode can expand to. */
  2592. static int
  2593. xg_get_max_insn_widen_size (xtensa_opcode opcode)
  2594. {
  2595. TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
  2596. TransitionList *l;
  2597. int max_size = xg_get_single_size (opcode);
  2598. gas_assert (opcode < table->num_opcodes);
  2599. for (l = table->table[opcode]; l != NULL; l = l->next)
  2600. {
  2601. TransitionRule *rule = l->rule;
  2602. BuildInstr *build_list;
  2603. int this_size = 0;
  2604. if (!rule)
  2605. continue;
  2606. build_list = rule->to_instr;
  2607. if (is_unique_insn_expansion (rule))
  2608. {
  2609. gas_assert (build_list->typ == INSTR_INSTR);
  2610. this_size = xg_get_max_insn_widen_size (build_list->opcode);
  2611. }
  2612. else
  2613. for (; build_list != NULL; build_list = build_list->next)
  2614. {
  2615. switch (build_list->typ)
  2616. {
  2617. case INSTR_INSTR:
  2618. this_size += xg_get_single_size (build_list->opcode);
  2619. break;
  2620. case INSTR_LITERAL_DEF:
  2621. case INSTR_LABEL_DEF:
  2622. default:
  2623. break;
  2624. }
  2625. }
  2626. if (this_size > max_size)
  2627. max_size = this_size;
  2628. }
  2629. return max_size;
  2630. }
  2631. /* Return the maximum number of literal bytes this opcode can generate. */
  2632. static int
  2633. xg_get_max_insn_widen_literal_size (xtensa_opcode opcode)
  2634. {
  2635. TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
  2636. TransitionList *l;
  2637. int max_size = 0;
  2638. gas_assert (opcode < table->num_opcodes);
  2639. for (l = table->table[opcode]; l != NULL; l = l->next)
  2640. {
  2641. TransitionRule *rule = l->rule;
  2642. BuildInstr *build_list;
  2643. int this_size = 0;
  2644. if (!rule)
  2645. continue;
  2646. build_list = rule->to_instr;
  2647. if (is_unique_insn_expansion (rule))
  2648. {
  2649. gas_assert (build_list->typ == INSTR_INSTR);
  2650. this_size = xg_get_max_insn_widen_literal_size (build_list->opcode);
  2651. }
  2652. else
  2653. for (; build_list != NULL; build_list = build_list->next)
  2654. {
  2655. switch (build_list->typ)
  2656. {
  2657. case INSTR_LITERAL_DEF:
  2658. /* Hard-coded 4-byte literal. */
  2659. this_size += 4;
  2660. break;
  2661. case INSTR_INSTR:
  2662. case INSTR_LABEL_DEF:
  2663. default:
  2664. break;
  2665. }
  2666. }
  2667. if (this_size > max_size)
  2668. max_size = this_size;
  2669. }
  2670. return max_size;
  2671. }
  2672. static bfd_boolean
  2673. xg_is_relaxable_insn (TInsn *insn, int lateral_steps)
  2674. {
  2675. int steps_taken = 0;
  2676. TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
  2677. TransitionList *l;
  2678. gas_assert (insn->insn_type == ITYPE_INSN);
  2679. gas_assert (insn->opcode < table->num_opcodes);
  2680. for (l = table->table[insn->opcode]; l != NULL; l = l->next)
  2681. {
  2682. TransitionRule *rule = l->rule;
  2683. if (xg_instruction_matches_rule (insn, rule))
  2684. {
  2685. if (steps_taken == lateral_steps)
  2686. return TRUE;
  2687. steps_taken++;
  2688. }
  2689. }
  2690. return FALSE;
  2691. }
  2692. static symbolS *
  2693. get_special_literal_symbol (void)
  2694. {
  2695. static symbolS *sym = NULL;
  2696. if (sym == NULL)
  2697. sym = symbol_find_or_make ("SPECIAL_LITERAL0\001");
  2698. return sym;
  2699. }
  2700. static symbolS *
  2701. get_special_label_symbol (void)
  2702. {
  2703. static symbolS *sym = NULL;
  2704. if (sym == NULL)
  2705. sym = symbol_find_or_make ("SPECIAL_LABEL0\001");
  2706. return sym;
  2707. }
  2708. static bfd_boolean
  2709. xg_valid_literal_expression (const expressionS *exp)
  2710. {
  2711. switch (exp->X_op)
  2712. {
  2713. case O_constant:
  2714. case O_symbol:
  2715. case O_big:
  2716. case O_uminus:
  2717. case O_subtract:
  2718. case O_pltrel:
  2719. case O_pcrel:
  2720. case O_tlsfunc:
  2721. case O_tlsarg:
  2722. case O_tpoff:
  2723. case O_dtpoff:
  2724. return TRUE;
  2725. default:
  2726. return FALSE;
  2727. }
  2728. }
  2729. /* This will check to see if the value can be converted into the
  2730. operand type. It will return TRUE if it does not fit. */
  2731. static bfd_boolean
  2732. xg_check_operand (int32 value, xtensa_opcode opcode, int operand)
  2733. {
  2734. uint32 valbuf = value;
  2735. if (xtensa_operand_encode (xtensa_default_isa, opcode, operand, &valbuf))
  2736. return TRUE;
  2737. return FALSE;
  2738. }
  2739. /* Assumes: All immeds are constants. Check that all constants fit
  2740. into their immeds; return FALSE if not. */
  2741. static bfd_boolean
  2742. xg_immeds_fit (const TInsn *insn)
  2743. {
  2744. xtensa_isa isa = xtensa_default_isa;
  2745. int i;
  2746. int n = insn->ntok;
  2747. gas_assert (insn->insn_type == ITYPE_INSN);
  2748. for (i = 0; i < n; ++i)
  2749. {
  2750. const expressionS *exp = &insn->tok[i];
  2751. if (xtensa_operand_is_register (isa, insn->opcode, i) == 1)
  2752. continue;
  2753. switch (exp->X_op)
  2754. {
  2755. case O_register:
  2756. case O_constant:
  2757. if (xg_check_operand (exp->X_add_number, insn->opcode, i))
  2758. return FALSE;
  2759. break;
  2760. default:
  2761. /* The symbol should have a fixup associated with it. */
  2762. gas_assert (FALSE);
  2763. break;
  2764. }
  2765. }
  2766. return TRUE;
  2767. }
  2768. /* This should only be called after we have an initial
  2769. estimate of the addresses. */
  2770. static bfd_boolean
  2771. xg_symbolic_immeds_fit (const TInsn *insn,
  2772. segT pc_seg,
  2773. fragS *pc_frag,
  2774. offsetT pc_offset,
  2775. long stretch)
  2776. {
  2777. xtensa_isa isa = xtensa_default_isa;
  2778. symbolS *symbolP;
  2779. fragS *sym_frag;
  2780. offsetT target, pc;
  2781. uint32 new_offset;
  2782. int i;
  2783. int n = insn->ntok;
  2784. gas_assert (insn->insn_type == ITYPE_INSN);
  2785. for (i = 0; i < n; ++i)
  2786. {
  2787. const expressionS *exp = &insn->tok[i];
  2788. if (xtensa_operand_is_register (isa, insn->opcode, i) == 1)
  2789. continue;
  2790. switch (exp->X_op)
  2791. {
  2792. case O_register:
  2793. case O_constant:
  2794. if (xg_check_operand (exp->X_add_number, insn->opcode, i))
  2795. return FALSE;
  2796. break;
  2797. case O_lo16:
  2798. case O_hi16:
  2799. /* Check for the worst case. */
  2800. if (xg_check_operand (0xffff, insn->opcode, i))
  2801. return FALSE;
  2802. break;
  2803. case O_symbol:
  2804. /* We only allow symbols for PC-relative references.
  2805. If pc_frag == 0, then we don't have frag locations yet. */
  2806. if (pc_frag == 0
  2807. || xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 0)
  2808. return FALSE;
  2809. /* If it is a weak symbol or a symbol in a different section,
  2810. it cannot be known to fit at assembly time. */
  2811. if (S_IS_WEAK (exp->X_add_symbol)
  2812. || S_GET_SEGMENT (exp->X_add_symbol) != pc_seg)
  2813. {
  2814. /* For a direct call with --no-longcalls, be optimistic and
  2815. assume it will be in range. If the symbol is weak and
  2816. undefined, it may remain undefined at link-time, in which
  2817. case it will have a zero value and almost certainly be out
  2818. of range for a direct call; thus, relax for undefined weak
  2819. symbols even if longcalls is not enabled. */
  2820. if (is_direct_call_opcode (insn->opcode)
  2821. && ! pc_frag->tc_frag_data.use_longcalls
  2822. && (! S_IS_WEAK (exp->X_add_symbol)
  2823. || S_IS_DEFINED (exp->X_add_symbol)))
  2824. return TRUE;
  2825. return FALSE;
  2826. }
  2827. symbolP = exp->X_add_symbol;
  2828. sym_frag = symbol_get_frag (symbolP);
  2829. target = S_GET_VALUE (symbolP) + exp->X_add_number;
  2830. pc = pc_frag->fr_address + pc_offset;
  2831. /* If frag has yet to be reached on this pass, assume it
  2832. will move by STRETCH just as we did. If this is not so,
  2833. it will be because some frag between grows, and that will
  2834. force another pass. Beware zero-length frags. There
  2835. should be a faster way to do this. */
  2836. if (stretch != 0
  2837. && sym_frag->relax_marker != pc_frag->relax_marker
  2838. && S_GET_SEGMENT (symbolP) == pc_seg)
  2839. {
  2840. target += stretch;
  2841. }
  2842. new_offset = target;
  2843. xtensa_operand_do_reloc (isa, insn->opcode, i, &new_offset, pc);
  2844. if (xg_check_operand (new_offset, insn->opcode, i))
  2845. return FALSE;
  2846. break;
  2847. default:
  2848. /* The symbol should have a fixup associated with it. */
  2849. return FALSE;
  2850. }
  2851. }
  2852. return TRUE;
  2853. }
  2854. /* Return TRUE on success. */
  2855. static bfd_boolean
  2856. xg_build_to_insn (TInsn *targ, TInsn *insn, BuildInstr *bi)
  2857. {
  2858. BuildOp *op;
  2859. symbolS *sym;
  2860. tinsn_init (targ);
  2861. targ->debug_line = insn->debug_line;
  2862. targ->loc_directive_seen = insn->loc_directive_seen;
  2863. switch (bi->typ)
  2864. {
  2865. case INSTR_INSTR:
  2866. op = bi->ops;
  2867. targ->opcode = bi->opcode;
  2868. targ->insn_type = ITYPE_INSN;
  2869. targ->is_specific_opcode = FALSE;
  2870. for (; op != NULL; op = op->next)
  2871. {
  2872. int op_num = op->op_num;
  2873. int op_data = op->op_data;
  2874. gas_assert (op->op_num < MAX_INSN_ARGS);
  2875. if (targ->ntok <= op_num)
  2876. targ->ntok = op_num + 1;
  2877. switch (op->typ)
  2878. {
  2879. case OP_CONSTANT:
  2880. set_expr_const (&targ->tok[op_num], op_data);
  2881. break;
  2882. case OP_OPERAND:
  2883. gas_assert (op_data < insn->ntok);
  2884. copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
  2885. break;
  2886. case OP_FREEREG:
  2887. if (insn->extra_arg.X_op != O_register)
  2888. return FALSE;
  2889. copy_expr (&targ->tok[op_num], &insn->extra_arg);
  2890. break;
  2891. case OP_LITERAL:
  2892. sym = get_special_literal_symbol ();
  2893. set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
  2894. if (insn->tok[op_data].X_op == O_tlsfunc
  2895. || insn->tok[op_data].X_op == O_tlsarg)
  2896. copy_expr (&targ->extra_arg, &insn->tok[op_data]);
  2897. break;
  2898. case OP_LABEL:
  2899. sym = get_special_label_symbol ();
  2900. set_expr_symbol_offset (&targ->tok[op_num], sym, 0);
  2901. break;
  2902. case OP_OPERAND_HI16U:
  2903. case OP_OPERAND_LOW16U:
  2904. gas_assert (op_data < insn->ntok);
  2905. if (expr_is_const (&insn->tok[op_data]))
  2906. {
  2907. long val;
  2908. copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
  2909. val = xg_apply_userdef_op_fn (op->typ,
  2910. targ->tok[op_num].
  2911. X_add_number);
  2912. targ->tok[op_num].X_add_number = val;
  2913. }
  2914. else
  2915. {
  2916. /* For const16 we can create relocations for these. */
  2917. if (targ->opcode == XTENSA_UNDEFINED
  2918. || (targ->opcode != xtensa_const16_opcode))
  2919. return FALSE;
  2920. gas_assert (op_data < insn->ntok);
  2921. /* Need to build a O_lo16 or O_hi16. */
  2922. copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
  2923. if (targ->tok[op_num].X_op == O_symbol)
  2924. {
  2925. if (op->typ == OP_OPERAND_HI16U)
  2926. targ->tok[op_num].X_op = O_hi16;
  2927. else if (op->typ == OP_OPERAND_LOW16U)
  2928. targ->tok[op_num].X_op = O_lo16;
  2929. else
  2930. return FALSE;
  2931. }
  2932. }
  2933. break;
  2934. default:
  2935. /* currently handles:
  2936. OP_OPERAND_LOW8
  2937. OP_OPERAND_HI24S
  2938. OP_OPERAND_F32MINUS */
  2939. if (xg_has_userdef_op_fn (op->typ))
  2940. {
  2941. gas_assert (op_data < insn->ntok);
  2942. if (expr_is_const (&insn->tok[op_data]))
  2943. {
  2944. long val;
  2945. copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
  2946. val = xg_apply_userdef_op_fn (op->typ,
  2947. targ->tok[op_num].
  2948. X_add_number);
  2949. targ->tok[op_num].X_add_number = val;
  2950. }
  2951. else
  2952. return FALSE; /* We cannot use a relocation for this. */
  2953. break;
  2954. }
  2955. gas_assert (0);
  2956. break;
  2957. }
  2958. }
  2959. break;
  2960. case INSTR_LITERAL_DEF:
  2961. op = bi->ops;
  2962. targ->opcode = XTENSA_UNDEFINED;
  2963. targ->insn_type = ITYPE_LITERAL;
  2964. targ->is_specific_opcode = FALSE;
  2965. for (; op != NULL; op = op->next)
  2966. {
  2967. int op_num = op->op_num;
  2968. int op_data = op->op_data;
  2969. gas_assert (op->op_num < MAX_INSN_ARGS);
  2970. if (targ->ntok <= op_num)
  2971. targ->ntok = op_num + 1;
  2972. switch (op->typ)
  2973. {
  2974. case OP_OPERAND:
  2975. gas_assert (op_data < insn->ntok);
  2976. /* We can only pass resolvable literals through. */
  2977. if (!xg_valid_literal_expression (&insn->tok[op_data]))
  2978. return FALSE;
  2979. copy_expr (&targ->tok[op_num], &insn->tok[op_data]);
  2980. break;
  2981. case OP_LITERAL:
  2982. case OP_CONSTANT:
  2983. case OP_LABEL:
  2984. default:
  2985. gas_assert (0);
  2986. break;
  2987. }
  2988. }
  2989. break;
  2990. case INSTR_LABEL_DEF:
  2991. op = bi->ops;
  2992. targ->opcode = XTENSA_UNDEFINED;
  2993. targ->insn_type = ITYPE_LABEL;
  2994. targ->is_specific_opcode = FALSE;
  2995. /* Literal with no ops is a label? */
  2996. gas_assert (op == NULL);
  2997. break;
  2998. default:
  2999. gas_assert (0);
  3000. }
  3001. return TRUE;
  3002. }
  3003. /* Return TRUE on success. */
  3004. static bfd_boolean
  3005. xg_build_to_stack (IStack *istack, TInsn *insn, BuildInstr *bi)
  3006. {
  3007. for (; bi != NULL; bi = bi->next)
  3008. {
  3009. TInsn *next_insn = istack_push_space (istack);
  3010. if (!xg_build_to_insn (next_insn, insn, bi))
  3011. return FALSE;
  3012. }
  3013. return TRUE;
  3014. }
  3015. /* Return TRUE on valid expansion. */
  3016. static bfd_boolean
  3017. xg_expand_to_stack (IStack *istack, TInsn *insn, int lateral_steps)
  3018. {
  3019. int stack_size = istack->ninsn;
  3020. int steps_taken = 0;
  3021. TransitionTable *table = xg_build_widen_table (&transition_rule_cmp);
  3022. TransitionList *l;
  3023. gas_assert (insn->insn_type == ITYPE_INSN);
  3024. gas_assert (insn->opcode < table->num_opcodes);
  3025. for (l = table->table[insn->opcode]; l != NULL; l = l->next)
  3026. {
  3027. TransitionRule *rule = l->rule;
  3028. if (xg_instruction_matches_rule (insn, rule))
  3029. {
  3030. if (lateral_steps == steps_taken)
  3031. {
  3032. int i;
  3033. /* This is it. Expand the rule to the stack. */
  3034. if (!xg_build_to_stack (istack, insn, rule->to_instr))
  3035. return FALSE;
  3036. /* Check to see if it fits. */
  3037. for (i = stack_size; i < istack->ninsn; i++)
  3038. {
  3039. TInsn *tinsn = &istack->insn[i];
  3040. if (tinsn->insn_type == ITYPE_INSN
  3041. && !tinsn_has_symbolic_operands (tinsn)
  3042. && !xg_immeds_fit (tinsn))
  3043. {
  3044. istack->ninsn = stack_size;
  3045. return FALSE;
  3046. }
  3047. }
  3048. return TRUE;
  3049. }
  3050. steps_taken++;
  3051. }
  3052. }
  3053. return FALSE;
  3054. }
  3055. /* Relax the assembly instruction at least "min_steps".
  3056. Return the number of steps taken.
  3057. For relaxation to correctly terminate, every relaxation chain must
  3058. terminate in one of two ways:
  3059. 1. If the chain from one instruction to the next consists entirely of
  3060. single instructions, then the chain *must* handle all possible
  3061. immediates without failing. It must not ever fail because an
  3062. immediate is out of range. The MOVI.N -> MOVI -> L32R relaxation
  3063. chain is one example. L32R loads 32 bits, and there cannot be an
  3064. immediate larger than 32 bits, so it satisfies this condition.
  3065. Single instruction relaxation chains are as defined by
  3066. xg_is_single_relaxable_instruction.
  3067. 2. Otherwise, the chain must end in a multi-instruction expansion: e.g.,
  3068. BNEZ.N -> BNEZ -> BNEZ.W15 -> BENZ.N/J
  3069. Strictly speaking, in most cases you can violate condition 1 and be OK
  3070. -- in particular when the last two instructions have the same single
  3071. size. But nevertheless, you should guarantee the above two conditions.
  3072. We could fix this so that single-instruction expansions correctly
  3073. terminate when they can't handle the range, but the error messages are
  3074. worse, and it actually turns out that in every case but one (18-bit wide
  3075. branches), you need a multi-instruction expansion to get the full range
  3076. anyway. And because 18-bit branches are handled identically to 15-bit
  3077. branches, there isn't any point in changing it. */
  3078. static int
  3079. xg_assembly_relax (IStack *istack,
  3080. TInsn *insn,
  3081. segT pc_seg,
  3082. fragS *pc_frag, /* if pc_frag == 0, not pc-relative */
  3083. offsetT pc_offset, /* offset in fragment */
  3084. int min_steps, /* minimum conversion steps */
  3085. long stretch) /* number of bytes stretched so far */
  3086. {
  3087. int steps_taken = 0;
  3088. /* Some of its immeds don't fit. Try to build a relaxed version.
  3089. This may go through a couple of stages of single instruction
  3090. transformations before we get there. */
  3091. TInsn single_target;
  3092. TInsn current_insn;
  3093. int lateral_steps = 0;
  3094. int istack_size = istack->ninsn;
  3095. if (xg_symbolic_immeds_fit (insn, pc_seg, pc_frag, pc_offset, stretch)
  3096. && steps_taken >= min_steps)
  3097. {
  3098. istack_push (istack, insn);
  3099. return steps_taken;
  3100. }
  3101. current_insn = *insn;
  3102. /* Walk through all of the single instruction expansions. */
  3103. while (xg_is_single_relaxable_insn (&current_insn, &single_target, FALSE))
  3104. {
  3105. steps_taken++;
  3106. if (xg_symbolic_immeds_fit (&single_target, pc_seg, pc_frag, pc_offset,
  3107. stretch))
  3108. {
  3109. if (steps_taken >= min_steps)
  3110. {
  3111. istack_push (istack, &single_target);
  3112. return steps_taken;
  3113. }
  3114. }
  3115. current_insn = single_target;
  3116. }
  3117. /* Now check for a multi-instruction expansion. */
  3118. while (xg_is_relaxable_insn (&current_insn, lateral_steps))
  3119. {
  3120. if (xg_symbolic_immeds_fit (&current_insn, pc_seg, pc_frag, pc_offset,
  3121. stretch))
  3122. {
  3123. if (steps_taken >= min_steps)
  3124. {
  3125. istack_push (istack, &current_insn);
  3126. return steps_taken;
  3127. }
  3128. }
  3129. steps_taken++;
  3130. if (xg_expand_to_stack (istack, &current_insn, lateral_steps))
  3131. {
  3132. if (steps_taken >= min_steps)
  3133. return steps_taken;
  3134. }
  3135. lateral_steps++;
  3136. istack->ninsn = istack_size;
  3137. }
  3138. /* It's not going to work -- use the original. */
  3139. istack_push (istack, insn);
  3140. return steps_taken;
  3141. }
  3142. static void
  3143. xg_finish_frag (char *last_insn,
  3144. enum xtensa_relax_statesE frag_state,
  3145. enum xtensa_relax_statesE slot0_state,
  3146. int max_growth,
  3147. bfd_boolean is_insn)
  3148. {
  3149. /* Finish off this fragment so that it has at LEAST the desired
  3150. max_growth. If it doesn't fit in this fragment, close this one
  3151. and start a new one. In either case, return a pointer to the
  3152. beginning of the growth area. */
  3153. fragS *old_frag;
  3154. frag_grow (max_growth);
  3155. old_frag = frag_now;
  3156. frag_now->fr_opcode = last_insn;
  3157. if (is_insn)
  3158. frag_now->tc_frag_data.is_insn = TRUE;
  3159. frag_var (rs_machine_dependent, max_growth, max_growth,
  3160. frag_state, frag_now->fr_symbol, frag_now->fr_offset, last_insn);
  3161. old_frag->tc_frag_data.slot_subtypes[0] = slot0_state;
  3162. xtensa_set_frag_assembly_state (frag_now);
  3163. /* Just to make sure that we did not split it up. */
  3164. gas_assert (old_frag->fr_next == frag_now);
  3165. }
  3166. /* Return TRUE if the target frag is one of the next non-empty frags. */
  3167. static bfd_boolean
  3168. is_next_frag_target (const fragS *fragP, const fragS *target)
  3169. {
  3170. if (fragP == NULL)
  3171. return FALSE;
  3172. for (; fragP; fragP = fragP->fr_next)
  3173. {
  3174. if (fragP == target)
  3175. return TRUE;
  3176. if (fragP->fr_fix != 0)
  3177. return FALSE;
  3178. if (fragP->fr_type == rs_fill && fragP->fr_offset != 0)
  3179. return FALSE;
  3180. if ((fragP->fr_type == rs_align || fragP->fr_type == rs_align_code)
  3181. && ((fragP->fr_address % (1 << fragP->fr_offset)) != 0))
  3182. return FALSE;
  3183. if (fragP->fr_type == rs_space)
  3184. return FALSE;
  3185. }
  3186. return FALSE;
  3187. }
  3188. static bfd_boolean
  3189. is_branch_jmp_to_next (TInsn *insn, fragS *fragP)
  3190. {
  3191. xtensa_isa isa = xtensa_default_isa;
  3192. int i;
  3193. int num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
  3194. int target_op = -1;
  3195. symbolS *sym;
  3196. fragS *target_frag;
  3197. if (xtensa_opcode_is_branch (isa, insn->opcode) != 1
  3198. && xtensa_opcode_is_jump (isa, insn->opcode) != 1)
  3199. return FALSE;
  3200. for (i = 0; i < num_ops; i++)
  3201. {
  3202. if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1)
  3203. {
  3204. target_op = i;
  3205. break;
  3206. }
  3207. }
  3208. if (target_op == -1)
  3209. return FALSE;
  3210. if (insn->ntok <= target_op)
  3211. return FALSE;
  3212. if (insn->tok[target_op].X_op != O_symbol)
  3213. return FALSE;
  3214. sym = insn->tok[target_op].X_add_symbol;
  3215. if (sym == NULL)
  3216. return FALSE;
  3217. if (insn->tok[target_op].X_add_number != 0)
  3218. return FALSE;
  3219. target_frag = symbol_get_frag (sym);
  3220. if (target_frag == NULL)
  3221. return FALSE;
  3222. if (is_next_frag_target (fragP->fr_next, target_frag)
  3223. && S_GET_VALUE (sym) == target_frag->fr_address)
  3224. return TRUE;
  3225. return FALSE;
  3226. }
  3227. static void
  3228. xg_add_branch_and_loop_targets (TInsn *insn)
  3229. {
  3230. xtensa_isa isa = xtensa_default_isa;
  3231. int num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
  3232. if (xtensa_opcode_is_loop (isa, insn->opcode) == 1)
  3233. {
  3234. int i = 1;
  3235. if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1
  3236. && insn->tok[i].X_op == O_symbol)
  3237. symbol_get_tc (insn->tok[i].X_add_symbol)->is_loop_target = TRUE;
  3238. return;
  3239. }
  3240. if (xtensa_opcode_is_branch (isa, insn->opcode) == 1
  3241. || xtensa_opcode_is_loop (isa, insn->opcode) == 1)
  3242. {
  3243. int i;
  3244. for (i = 0; i < insn->ntok && i < num_ops; i++)
  3245. {
  3246. if (xtensa_operand_is_PCrelative (isa, insn->opcode, i) == 1
  3247. && insn->tok[i].X_op == O_symbol)
  3248. {
  3249. symbolS *sym = insn->tok[i].X_add_symbol;
  3250. symbol_get_tc (sym)->is_branch_target = TRUE;
  3251. if (S_IS_DEFINED (sym))
  3252. symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
  3253. }
  3254. }
  3255. }
  3256. }
  3257. /* Return FALSE if no error. */
  3258. static bfd_boolean
  3259. xg_build_token_insn (BuildInstr *instr_spec, TInsn *old_insn, TInsn *new_insn)
  3260. {
  3261. int num_ops = 0;
  3262. BuildOp *b_op;
  3263. switch (instr_spec->typ)
  3264. {
  3265. case INSTR_INSTR:
  3266. new_insn->insn_type = ITYPE_INSN;
  3267. new_insn->opcode = instr_spec->opcode;
  3268. break;
  3269. case INSTR_LITERAL_DEF:
  3270. new_insn->insn_type = ITYPE_LITERAL;
  3271. new_insn->opcode = XTENSA_UNDEFINED;
  3272. break;
  3273. case INSTR_LABEL_DEF:
  3274. abort ();
  3275. }
  3276. new_insn->is_specific_opcode = FALSE;
  3277. new_insn->debug_line = old_insn->debug_line;
  3278. new_insn->loc_directive_seen = old_insn->loc_directive_seen;
  3279. for (b_op = instr_spec->ops; b_op != NULL; b_op = b_op->next)
  3280. {
  3281. expressionS *exp;
  3282. const expressionS *src_exp;
  3283. num_ops++;
  3284. switch (b_op->typ)
  3285. {
  3286. case OP_CONSTANT:
  3287. /* The expression must be the constant. */
  3288. gas_assert (b_op->op_num < MAX_INSN_ARGS);
  3289. exp = &new_insn->tok[b_op->op_num];
  3290. set_expr_const (exp, b_op->op_data);
  3291. break;
  3292. case OP_OPERAND:
  3293. gas_assert (b_op->op_num < MAX_INSN_ARGS);
  3294. gas_assert (b_op->op_data < (unsigned) old_insn->ntok);
  3295. src_exp = &old_insn->tok[b_op->op_data];
  3296. exp = &new_insn->tok[b_op->op_num];
  3297. copy_expr (exp, src_exp);
  3298. break;
  3299. case OP_LITERAL:
  3300. case OP_LABEL:
  3301. as_bad (_("can't handle generation of literal/labels yet"));
  3302. gas_assert (0);
  3303. default:
  3304. as_bad (_("can't handle undefined OP TYPE"));
  3305. gas_assert (0);
  3306. }
  3307. }
  3308. new_insn->ntok = num_ops;
  3309. return FALSE;
  3310. }
  3311. /* Return TRUE if it was simplified. */
  3312. static bfd_boolean
  3313. xg_simplify_insn (TInsn *old_insn, TInsn *new_insn)
  3314. {
  3315. TransitionRule *rule;
  3316. BuildInstr *insn_spec;
  3317. if (old_insn->is_specific_opcode || !density_supported)
  3318. return FALSE;
  3319. rule = xg_instruction_match (old_insn);
  3320. if (rule == NULL)
  3321. return FALSE;
  3322. insn_spec = rule->to_instr;
  3323. /* There should only be one. */
  3324. gas_assert (insn_spec != NULL);
  3325. gas_assert (insn_spec->next == NULL);
  3326. if (insn_spec->next != NULL)
  3327. return FALSE;
  3328. xg_build_token_insn (insn_spec, old_insn, new_insn);
  3329. return TRUE;
  3330. }
  3331. /* xg_expand_assembly_insn: (1) Simplify the instruction, i.e., l32i ->
  3332. l32i.n. (2) Check the number of operands. (3) Place the instruction
  3333. tokens into the stack or relax it and place multiple
  3334. instructions/literals onto the stack. Return FALSE if no error. */
  3335. static bfd_boolean
  3336. xg_expand_assembly_insn (IStack *istack, TInsn *orig_insn)
  3337. {
  3338. int noperands;
  3339. TInsn new_insn;
  3340. bfd_boolean do_expand;
  3341. tinsn_init (&new_insn);
  3342. /* Narrow it if we can. xg_simplify_insn now does all the
  3343. appropriate checking (e.g., for the density option). */
  3344. if (xg_simplify_insn (orig_insn, &new_insn))
  3345. orig_insn = &new_insn;
  3346. noperands = xtensa_opcode_num_operands (xtensa_default_isa,
  3347. orig_insn->opcode);
  3348. if (orig_insn->ntok < noperands)
  3349. {
  3350. as_bad (_("found %d operands for '%s': Expected %d"),
  3351. orig_insn->ntok,
  3352. xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
  3353. noperands);
  3354. return TRUE;
  3355. }
  3356. if (orig_insn->ntok > noperands)
  3357. as_warn (_("found too many (%d) operands for '%s': Expected %d"),
  3358. orig_insn->ntok,
  3359. xtensa_opcode_name (xtensa_default_isa, orig_insn->opcode),
  3360. noperands);
  3361. /* If there are not enough operands, we will assert above. If there
  3362. are too many, just cut out the extras here. */
  3363. orig_insn->ntok = noperands;
  3364. if (tinsn_has_invalid_symbolic_operands (orig_insn))
  3365. return TRUE;
  3366. /* Special case for extui opcode which has constraints not handled
  3367. by the ordinary operand encoding checks. The number of operands
  3368. and related syntax issues have already been checked. */
  3369. if (orig_insn->opcode == xtensa_extui_opcode)
  3370. {
  3371. int shiftimm = orig_insn->tok[2].X_add_number;
  3372. int maskimm = orig_insn->tok[3].X_add_number;
  3373. if (shiftimm + maskimm > 32)
  3374. {
  3375. as_bad (_("immediate operands sum to greater than 32"));
  3376. return TRUE;
  3377. }
  3378. }
  3379. /* If the instruction will definitely need to be relaxed, it is better
  3380. to expand it now for better scheduling. Decide whether to expand
  3381. now.... */
  3382. do_expand = (!orig_insn->is_specific_opcode && use_transform ());
  3383. /* Calls should be expanded to longcalls only in the backend relaxation
  3384. so that the assembly scheduler will keep the L32R/CALLX instructions
  3385. adjacent. */
  3386. if (is_direct_call_opcode (orig_insn->opcode))
  3387. do_expand = FALSE;
  3388. if (tinsn_has_symbolic_operands (orig_insn))
  3389. {
  3390. /* The values of symbolic operands are not known yet, so only expand
  3391. now if an operand is "complex" (e.g., difference of symbols) and
  3392. will have to be stored as a literal regardless of the value. */
  3393. if (!tinsn_has_complex_operands (orig_insn))
  3394. do_expand = FALSE;
  3395. }
  3396. else if (xg_immeds_fit (orig_insn))
  3397. do_expand = FALSE;
  3398. if (do_expand)
  3399. xg_assembly_relax (istack, orig_insn, 0, 0, 0, 0, 0);
  3400. else
  3401. istack_push (istack, orig_insn);
  3402. return FALSE;
  3403. }
  3404. /* Return TRUE if the section flags are marked linkonce
  3405. or the name is .gnu.linkonce.*. */
  3406. static int linkonce_len = sizeof (".gnu.linkonce.") - 1;
  3407. static bfd_boolean
  3408. get_is_linkonce_section (bfd *abfd ATTRIBUTE_UNUSED, segT sec)
  3409. {
  3410. flagword flags, link_once_flags;
  3411. flags = bfd_get_section_flags (abfd, sec);
  3412. link_once_flags = (flags & SEC_LINK_ONCE);
  3413. /* Flags might not be set yet. */
  3414. if (!link_once_flags
  3415. && strncmp (segment_name (sec), ".gnu.linkonce.", linkonce_len) == 0)
  3416. link_once_flags = SEC_LINK_ONCE;
  3417. return (link_once_flags != 0);
  3418. }
  3419. static void
  3420. xtensa_add_literal_sym (symbolS *sym)
  3421. {
  3422. sym_list *l;
  3423. l = (sym_list *) xmalloc (sizeof (sym_list));
  3424. l->sym = sym;
  3425. l->next = literal_syms;
  3426. literal_syms = l;
  3427. }
  3428. static symbolS *
  3429. xtensa_create_literal_symbol (segT sec, fragS *frag)
  3430. {
  3431. static int lit_num = 0;
  3432. static char name[256];
  3433. symbolS *symbolP;
  3434. sprintf (name, ".L_lit_sym%d", lit_num);
  3435. /* Create a local symbol. If it is in a linkonce section, we have to
  3436. be careful to make sure that if it is used in a relocation that the
  3437. symbol will be in the output file. */
  3438. if (get_is_linkonce_section (stdoutput, sec))
  3439. {
  3440. symbolP = symbol_new (name, sec, 0, frag);
  3441. S_CLEAR_EXTERNAL (symbolP);
  3442. /* symbolP->local = 1; */
  3443. }
  3444. else
  3445. symbolP = symbol_new (name, sec, 0, frag);
  3446. xtensa_add_literal_sym (symbolP);
  3447. lit_num++;
  3448. return symbolP;
  3449. }
  3450. /* Currently all literals that are generated here are 32-bit L32R targets. */
  3451. static symbolS *
  3452. xg_assemble_literal (/* const */ TInsn *insn)
  3453. {
  3454. emit_state state;
  3455. symbolS *lit_sym = NULL;
  3456. bfd_reloc_code_real_type reloc;
  3457. bfd_boolean pcrel = FALSE;
  3458. char *p;
  3459. /* size = 4 for L32R. It could easily be larger when we move to
  3460. larger constants. Add a parameter later. */
  3461. offsetT litsize = 4;
  3462. offsetT litalign = 2; /* 2^2 = 4 */
  3463. expressionS saved_loc;
  3464. expressionS * emit_val;
  3465. set_expr_symbol_offset (&saved_loc, frag_now->fr_symbol, frag_now_fix ());
  3466. gas_assert (insn->insn_type == ITYPE_LITERAL);
  3467. gas_assert (insn->ntok == 1); /* must be only one token here */
  3468. xtensa_switch_to_literal_fragment (&state);
  3469. emit_val = &insn->tok[0];
  3470. if (emit_val->X_op == O_big)
  3471. {
  3472. int size = emit_val->X_add_number * CHARS_PER_LITTLENUM;
  3473. if (size > litsize)
  3474. {
  3475. /* This happens when someone writes a "movi a2, big_number". */
  3476. as_bad_where (frag_now->fr_file, frag_now->fr_line,
  3477. _("invalid immediate"));
  3478. xtensa_restore_emit_state (&state);
  3479. return NULL;
  3480. }
  3481. }
  3482. /* Force a 4-byte align here. Note that this opens a new frag, so all
  3483. literals done with this function have a frag to themselves. That's
  3484. important for the way text section literals work. */
  3485. frag_align (litalign, 0, 0);
  3486. record_alignment (now_seg, litalign);
  3487. switch (emit_val->X_op)
  3488. {
  3489. case O_pcrel:
  3490. pcrel = TRUE;
  3491. /* fall through */
  3492. case O_pltrel:
  3493. case O_tlsfunc:
  3494. case O_tlsarg:
  3495. case O_tpoff:
  3496. case O_dtpoff:
  3497. p = frag_more (litsize);
  3498. xtensa_set_frag_assembly_state (frag_now);
  3499. reloc = map_operator_to_reloc (emit_val->X_op, TRUE);
  3500. if (emit_val->X_add_symbol)
  3501. emit_val->X_op = O_symbol;
  3502. else
  3503. emit_val->X_op = O_constant;
  3504. fix_new_exp (frag_now, p - frag_now->fr_literal,
  3505. litsize, emit_val, pcrel, reloc);
  3506. break;
  3507. default:
  3508. emit_expr (emit_val, litsize);
  3509. break;
  3510. }
  3511. gas_assert (frag_now->tc_frag_data.literal_frag == NULL);
  3512. frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
  3513. frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
  3514. lit_sym = frag_now->fr_symbol;
  3515. /* Go back. */
  3516. xtensa_restore_emit_state (&state);
  3517. return lit_sym;
  3518. }
  3519. static void
  3520. xg_assemble_literal_space (/* const */ int size, int slot)
  3521. {
  3522. emit_state state;
  3523. /* We might have to do something about this alignment. It only
  3524. takes effect if something is placed here. */
  3525. offsetT litalign = 2; /* 2^2 = 4 */
  3526. fragS *lit_saved_frag;
  3527. gas_assert (size % 4 == 0);
  3528. xtensa_switch_to_literal_fragment (&state);
  3529. /* Force a 4-byte align here. */
  3530. frag_align (litalign, 0, 0);
  3531. record_alignment (now_seg, litalign);
  3532. frag_grow (size);
  3533. lit_saved_frag = frag_now;
  3534. frag_now->tc_frag_data.literal_frag = get_literal_pool_location (now_seg);
  3535. frag_now->fr_symbol = xtensa_create_literal_symbol (now_seg, frag_now);
  3536. xg_finish_frag (0, RELAX_LITERAL, 0, size, FALSE);
  3537. /* Go back. */
  3538. xtensa_restore_emit_state (&state);
  3539. frag_now->tc_frag_data.literal_frags[slot] = lit_saved_frag;
  3540. }
  3541. /* Put in a fixup record based on the opcode.
  3542. Return TRUE on success. */
  3543. static bfd_boolean
  3544. xg_add_opcode_fix (TInsn *tinsn,
  3545. int opnum,
  3546. xtensa_format fmt,
  3547. int slot,
  3548. expressionS *exp,
  3549. fragS *fragP,
  3550. offsetT offset)
  3551. {
  3552. xtensa_opcode opcode = tinsn->opcode;
  3553. bfd_reloc_code_real_type reloc;
  3554. reloc_howto_type *howto;
  3555. int fmt_length;
  3556. fixS *the_fix;
  3557. reloc = BFD_RELOC_NONE;
  3558. /* First try the special cases for "alternate" relocs. */
  3559. if (opcode == xtensa_l32r_opcode)
  3560. {
  3561. if (fragP->tc_frag_data.use_absolute_literals)
  3562. reloc = encode_alt_reloc (slot);
  3563. }
  3564. else if (opcode == xtensa_const16_opcode)
  3565. {
  3566. if (exp->X_op == O_lo16)
  3567. {
  3568. reloc = encode_reloc (slot);
  3569. exp->X_op = O_symbol;
  3570. }
  3571. else if (exp->X_op == O_hi16)
  3572. {
  3573. reloc = encode_alt_reloc (slot);
  3574. exp->X_op = O_symbol;
  3575. }
  3576. }
  3577. if (opnum != get_relaxable_immed (opcode))
  3578. {
  3579. as_bad (_("invalid relocation for operand %i of '%s'"),
  3580. opnum + 1, xtensa_opcode_name (xtensa_default_isa, opcode));
  3581. return FALSE;
  3582. }
  3583. /* Handle erroneous "@h" and "@l" expressions here before they propagate
  3584. into the symbol table where the generic portions of the assembler
  3585. won't know what to do with them. */
  3586. if (exp->X_op == O_lo16 || exp->X_op == O_hi16)
  3587. {
  3588. as_bad (_("invalid expression for operand %i of '%s'"),
  3589. opnum + 1, xtensa_opcode_name (xtensa_default_isa, opcode));
  3590. return FALSE;
  3591. }
  3592. /* Next try the generic relocs. */
  3593. if (reloc == BFD_RELOC_NONE)
  3594. reloc = encode_reloc (slot);
  3595. if (reloc == BFD_RELOC_NONE)
  3596. {
  3597. as_bad (_("invalid relocation in instruction slot %i"), slot);
  3598. return FALSE;
  3599. }
  3600. howto = bfd_reloc_type_lookup (stdoutput, reloc);
  3601. if (!howto)
  3602. {
  3603. as_bad (_("undefined symbol for opcode \"%s\""),
  3604. xtensa_opcode_name (xtensa_default_isa, opcode));
  3605. return FALSE;
  3606. }
  3607. fmt_length = xtensa_format_length (xtensa_default_isa, fmt);
  3608. the_fix = fix_new_exp (fragP, offset, fmt_length, exp,
  3609. howto->pc_relative, reloc);
  3610. the_fix->fx_no_overflow = 1;
  3611. the_fix->tc_fix_data.X_add_symbol = exp->X_add_symbol;
  3612. the_fix->tc_fix_data.X_add_number = exp->X_add_number;
  3613. the_fix->tc_fix_data.slot = slot;
  3614. return TRUE;
  3615. }
  3616. static bfd_boolean
  3617. xg_emit_insn_to_buf (TInsn *tinsn,
  3618. char *buf,
  3619. fragS *fragP,
  3620. offsetT offset,
  3621. bfd_boolean build_fix)
  3622. {
  3623. static xtensa_insnbuf insnbuf = NULL;
  3624. bfd_boolean has_symbolic_immed = FALSE;
  3625. bfd_boolean ok = TRUE;
  3626. if (!insnbuf)
  3627. insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
  3628. has_symbolic_immed = tinsn_to_insnbuf (tinsn, insnbuf);
  3629. if (has_symbolic_immed && build_fix)
  3630. {
  3631. /* Add a fixup. */
  3632. xtensa_format fmt = xg_get_single_format (tinsn->opcode);
  3633. int slot = xg_get_single_slot (tinsn->opcode);
  3634. int opnum = get_relaxable_immed (tinsn->opcode);
  3635. expressionS *exp = &tinsn->tok[opnum];
  3636. if (!xg_add_opcode_fix (tinsn, opnum, fmt, slot, exp, fragP, offset))
  3637. ok = FALSE;
  3638. }
  3639. fragP->tc_frag_data.is_insn = TRUE;
  3640. xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf,
  3641. (unsigned char *) buf, 0);
  3642. return ok;
  3643. }
  3644. static void
  3645. xg_resolve_literals (TInsn *insn, symbolS *lit_sym)
  3646. {
  3647. symbolS *sym = get_special_literal_symbol ();
  3648. int i;
  3649. if (lit_sym == 0)
  3650. return;
  3651. gas_assert (insn->insn_type == ITYPE_INSN);
  3652. for (i = 0; i < insn->ntok; i++)
  3653. if (insn->tok[i].X_add_symbol == sym)
  3654. insn->tok[i].X_add_symbol = lit_sym;
  3655. }
  3656. static void
  3657. xg_resolve_labels (TInsn *insn, symbolS *label_sym)
  3658. {
  3659. symbolS *sym = get_special_label_symbol ();
  3660. int i;
  3661. for (i = 0; i < insn->ntok; i++)
  3662. if (insn->tok[i].X_add_symbol == sym)
  3663. insn->tok[i].X_add_symbol = label_sym;
  3664. }
  3665. /* Return TRUE if the instruction can write to the specified
  3666. integer register. */
  3667. static bfd_boolean
  3668. is_register_writer (const TInsn *insn, const char *regset, int regnum)
  3669. {
  3670. int i;
  3671. int num_ops;
  3672. xtensa_isa isa = xtensa_default_isa;
  3673. num_ops = xtensa_opcode_num_operands (isa, insn->opcode);
  3674. for (i = 0; i < num_ops; i++)
  3675. {
  3676. char inout;
  3677. inout = xtensa_operand_inout (isa, insn->opcode, i);
  3678. if ((inout == 'o' || inout == 'm')
  3679. && xtensa_operand_is_register (isa, insn->opcode, i) == 1)
  3680. {
  3681. xtensa_regfile opnd_rf =
  3682. xtensa_operand_regfile (isa, insn->opcode, i);
  3683. if (!strcmp (xtensa_regfile_shortname (isa, opnd_rf), regset))
  3684. {
  3685. if ((insn->tok[i].X_op == O_register)
  3686. && (insn->tok[i].X_add_number == regnum))
  3687. return TRUE;
  3688. }
  3689. }
  3690. }
  3691. return FALSE;
  3692. }
  3693. static bfd_boolean
  3694. is_bad_loopend_opcode (const TInsn *tinsn)
  3695. {
  3696. xtensa_opcode opcode = tinsn->opcode;
  3697. if (opcode == XTENSA_UNDEFINED)
  3698. return FALSE;
  3699. if (opcode == xtensa_call0_opcode
  3700. || opcode == xtensa_callx0_opcode
  3701. || opcode == xtensa_call4_opcode
  3702. || opcode == xtensa_callx4_opcode
  3703. || opcode == xtensa_call8_opcode
  3704. || opcode == xtensa_callx8_opcode
  3705. || opcode == xtensa_call12_opcode
  3706. || opcode == xtensa_callx12_opcode
  3707. || opcode == xtensa_isync_opcode
  3708. || opcode == xtensa_ret_opcode
  3709. || opcode == xtensa_ret_n_opcode
  3710. || opcode == xtensa_retw_opcode
  3711. || opcode == xtensa_retw_n_opcode
  3712. || opcode == xtensa_waiti_opcode
  3713. || opcode == xtensa_rsr_lcount_opcode)
  3714. return TRUE;
  3715. return FALSE;
  3716. }
  3717. /* Labels that begin with ".Ln" or ".LM" are unaligned.
  3718. This allows the debugger to add unaligned labels.
  3719. Also, the assembler generates stabs labels that need
  3720. not be aligned: FAKE_LABEL_NAME . {"F", "L", "endfunc"}. */
  3721. static bfd_boolean
  3722. is_unaligned_label (symbolS *sym)
  3723. {
  3724. const char *name = S_GET_NAME (sym);
  3725. static size_t fake_size = 0;
  3726. if (name
  3727. && name[0] == '.'
  3728. && name[1] == 'L' && (name[2] == 'n' || name[2] == 'M'))
  3729. return TRUE;
  3730. /* FAKE_LABEL_NAME followed by "F", "L" or "endfunc" */
  3731. if (fake_size == 0)
  3732. fake_size = strlen (FAKE_LABEL_NAME);
  3733. if (name
  3734. && strncmp (FAKE_LABEL_NAME, name, fake_size) == 0
  3735. && (name[fake_size] == 'F'
  3736. || name[fake_size] == 'L'
  3737. || (name[fake_size] == 'e'
  3738. && strncmp ("endfunc", name+fake_size, 7) == 0)))
  3739. return TRUE;
  3740. return FALSE;
  3741. }
  3742. static fragS *
  3743. next_non_empty_frag (const fragS *fragP)
  3744. {
  3745. fragS *next_fragP = fragP->fr_next;
  3746. /* Sometimes an empty will end up here due storage allocation issues.
  3747. So we have to skip until we find something legit. */
  3748. while (next_fragP && next_fragP->fr_fix == 0)
  3749. next_fragP = next_fragP->fr_next;
  3750. if (next_fragP == NULL || next_fragP->fr_fix == 0)
  3751. return NULL;
  3752. return next_fragP;
  3753. }
  3754. static bfd_boolean
  3755. next_frag_opcode_is_loop (const fragS *fragP, xtensa_opcode *opcode)
  3756. {
  3757. xtensa_opcode out_opcode;
  3758. const fragS *next_fragP = next_non_empty_frag (fragP);
  3759. if (next_fragP == NULL)
  3760. return FALSE;
  3761. out_opcode = get_opcode_from_buf (next_fragP->fr_literal, 0);
  3762. if (xtensa_opcode_is_loop (xtensa_default_isa, out_opcode) == 1)
  3763. {
  3764. *opcode = out_opcode;
  3765. return TRUE;
  3766. }
  3767. return FALSE;
  3768. }
  3769. static int
  3770. frag_format_size (const fragS *fragP)
  3771. {
  3772. static xtensa_insnbuf insnbuf = NULL;
  3773. xtensa_isa isa = xtensa_default_isa;
  3774. xtensa_format fmt;
  3775. int fmt_size;
  3776. if (!insnbuf)
  3777. insnbuf = xtensa_insnbuf_alloc (isa);
  3778. if (fragP == NULL)
  3779. return XTENSA_UNDEFINED;
  3780. xtensa_insnbuf_from_chars (isa, insnbuf,
  3781. (unsigned char *) fragP->fr_literal, 0);
  3782. fmt = xtensa_format_decode (isa, insnbuf);
  3783. if (fmt == XTENSA_UNDEFINED)
  3784. return XTENSA_UNDEFINED;
  3785. fmt_size = xtensa_format_length (isa, fmt);
  3786. /* If the next format won't be changing due to relaxation, just
  3787. return the length of the first format. */
  3788. if (fragP->fr_opcode != fragP->fr_literal)
  3789. return fmt_size;
  3790. /* If during relaxation we have to pull an instruction out of a
  3791. multi-slot instruction, we will return the more conservative
  3792. number. This works because alignment on bigger instructions
  3793. is more restrictive than alignment on smaller instructions.
  3794. This is more conservative than we would like, but it happens
  3795. infrequently. */
  3796. if (xtensa_format_num_slots (xtensa_default_isa, fmt) > 1)
  3797. return fmt_size;
  3798. /* If we aren't doing one of our own relaxations or it isn't
  3799. slot-based, then the insn size won't change. */
  3800. if (fragP->fr_type != rs_machine_dependent)
  3801. return fmt_size;
  3802. if (fragP->fr_subtype != RELAX_SLOTS)
  3803. return fmt_size;
  3804. /* If an instruction is about to grow, return the longer size. */
  3805. if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP1
  3806. || fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP2
  3807. || fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED_STEP3)
  3808. {
  3809. /* For most frags at RELAX_IMMED_STEPX, with X > 0, the first
  3810. instruction in the relaxed version is of length 3. (The case
  3811. where we have to pull the instruction out of a FLIX bundle
  3812. is handled conservatively above.) However, frags with opcodes
  3813. that are expanding to wide branches end up having formats that
  3814. are not determinable by the RELAX_IMMED_STEPX enumeration, and
  3815. we can't tell directly what format the relaxer picked. This
  3816. is a wart in the design of the relaxer that should someday be
  3817. fixed, but would require major changes, or at least should
  3818. be accompanied by major changes to make use of that data.
  3819. In any event, we can tell that we are expanding from a single-slot
  3820. format to a wider one with the logic below. */
  3821. int i;
  3822. int relaxed_size = fmt_size + fragP->tc_frag_data.text_expansion[0];
  3823. for (i = 0; i < xtensa_isa_num_formats (isa); i++)
  3824. {
  3825. if (relaxed_size == xtensa_format_length (isa, i))
  3826. return relaxed_size;
  3827. }
  3828. return 3;
  3829. }
  3830. if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
  3831. return 2 + fragP->tc_frag_data.text_expansion[0];
  3832. return fmt_size;
  3833. }
  3834. static int
  3835. next_frag_format_size (const fragS *fragP)
  3836. {
  3837. const fragS *next_fragP = next_non_empty_frag (fragP);
  3838. return frag_format_size (next_fragP);
  3839. }
  3840. /* In early Xtensa Processors, for reasons that are unclear, the ISA
  3841. required two-byte instructions to be treated as three-byte instructions
  3842. for loop instruction alignment. This restriction was removed beginning
  3843. with Xtensa LX. Now the only requirement on loop instruction alignment
  3844. is that the first instruction of the loop must appear at an address that
  3845. does not cross a fetch boundary. */
  3846. static int
  3847. get_loop_align_size (int insn_size)
  3848. {
  3849. if (insn_size == XTENSA_UNDEFINED)
  3850. return xtensa_fetch_width;
  3851. if (enforce_three_byte_loop_align && insn_size == 2)
  3852. return 3;
  3853. return insn_size;
  3854. }
  3855. /* If the next legit fragment is an end-of-loop marker,
  3856. switch its state so it will instantiate a NOP. */
  3857. static void
  3858. update_next_frag_state (fragS *fragP)
  3859. {
  3860. fragS *next_fragP = fragP->fr_next;
  3861. fragS *new_target = NULL;
  3862. if (align_targets)
  3863. {
  3864. /* We are guaranteed there will be one of these... */
  3865. while (!(next_fragP->fr_type == rs_machine_dependent
  3866. && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE
  3867. || next_fragP->fr_subtype == RELAX_UNREACHABLE)))
  3868. next_fragP = next_fragP->fr_next;
  3869. gas_assert (next_fragP->fr_type == rs_machine_dependent
  3870. && (next_fragP->fr_subtype == RELAX_MAYBE_UNREACHABLE
  3871. || next_fragP->fr_subtype == RELAX_UNREACHABLE));
  3872. /* ...and one of these. */
  3873. new_target = next_fragP->fr_next;
  3874. while (!(new_target->fr_type == rs_machine_dependent
  3875. && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN
  3876. || new_target->fr_subtype == RELAX_DESIRE_ALIGN)))
  3877. new_target = new_target->fr_next;
  3878. gas_assert (new_target->fr_type == rs_machine_dependent
  3879. && (new_target->fr_subtype == RELAX_MAYBE_DESIRE_ALIGN
  3880. || new_target->fr_subtype == RELAX_DESIRE_ALIGN));
  3881. }
  3882. while (next_fragP && next_fragP->fr_fix == 0)
  3883. {
  3884. if (next_fragP->fr_type == rs_machine_dependent
  3885. && next_fragP->fr_subtype == RELAX_LOOP_END)
  3886. {
  3887. next_fragP->fr_subtype = RELAX_LOOP_END_ADD_NOP;
  3888. return;
  3889. }
  3890. next_fragP = next_fragP->fr_next;
  3891. }
  3892. }
  3893. static bfd_boolean
  3894. next_frag_is_branch_target (const fragS *fragP)
  3895. {
  3896. /* Sometimes an empty will end up here due to storage allocation issues,
  3897. so we have to skip until we find something legit. */
  3898. for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
  3899. {
  3900. if (fragP->tc_frag_data.is_branch_target)
  3901. return TRUE;
  3902. if (fragP->fr_fix != 0)
  3903. break;
  3904. }
  3905. return FALSE;
  3906. }
  3907. static bfd_boolean
  3908. next_frag_is_loop_target (const fragS *fragP)
  3909. {
  3910. /* Sometimes an empty will end up here due storage allocation issues.
  3911. So we have to skip until we find something legit. */
  3912. for (fragP = fragP->fr_next; fragP; fragP = fragP->fr_next)
  3913. {
  3914. if (fragP->tc_frag_data.is_loop_target)
  3915. return TRUE;
  3916. if (fragP->fr_fix != 0)
  3917. break;
  3918. }
  3919. return FALSE;
  3920. }
  3921. /* As specified in the relaxation table, when a loop instruction is
  3922. relaxed, there are 24 bytes between the loop instruction itself and
  3923. the first instruction in the loop. */
  3924. #define RELAXED_LOOP_INSN_BYTES 24
  3925. static addressT
  3926. next_frag_pre_opcode_bytes (const fragS *fragp)
  3927. {
  3928. const fragS *next_fragp = fragp->fr_next;
  3929. xtensa_opcode next_opcode;
  3930. if (!next_frag_opcode_is_loop (fragp, &next_opcode))
  3931. return 0;
  3932. /* Sometimes an empty will end up here due to storage allocation issues,
  3933. so we have to skip until we find something legit. */
  3934. while (next_fragp->fr_fix == 0)
  3935. next_fragp = next_fragp->fr_next;
  3936. if (next_fragp->fr_type != rs_machine_dependent)
  3937. return 0;
  3938. /* There is some implicit knowledge encoded in here.
  3939. The LOOP instructions that are NOT RELAX_IMMED have
  3940. been relaxed. Note that we can assume that the LOOP
  3941. instruction is in slot 0 because loops aren't bundleable. */
  3942. if (next_fragp->tc_frag_data.slot_subtypes[0] > RELAX_IMMED)
  3943. return get_expanded_loop_offset (next_opcode) + RELAXED_LOOP_INSN_BYTES;
  3944. return 0;
  3945. }
  3946. /* Mark a location where we can later insert literal frags. Update
  3947. the section's literal_pool_loc, so subsequent literals can be
  3948. placed nearest to their use. */
  3949. static void
  3950. xtensa_mark_literal_pool_location (void)
  3951. {
  3952. /* Any labels pointing to the current location need
  3953. to be adjusted to after the literal pool. */
  3954. emit_state s;
  3955. fragS *pool_location;
  3956. if (use_literal_section)
  3957. return;
  3958. /* We stash info in these frags so we can later move the literal's
  3959. fixes into this frchain's fix list. */
  3960. pool_location = frag_now;
  3961. frag_now->tc_frag_data.lit_frchain = frchain_now;
  3962. frag_now->tc_frag_data.literal_frag = frag_now;
  3963. /* Just record this frag. */
  3964. xtensa_maybe_create_literal_pool_frag (FALSE, FALSE);
  3965. frag_variant (rs_machine_dependent, 0, 0,
  3966. RELAX_LITERAL_POOL_BEGIN, NULL, 0, NULL);
  3967. xtensa_set_frag_assembly_state (frag_now);
  3968. frag_now->tc_frag_data.lit_seg = now_seg;
  3969. frag_variant (rs_machine_dependent, 0, 0,
  3970. RELAX_LITERAL_POOL_END, NULL, 0, NULL);
  3971. xtensa_set_frag_assembly_state (frag_now);
  3972. /* Now put a frag into the literal pool that points to this location. */
  3973. set_literal_pool_location (now_seg, pool_location);
  3974. xtensa_switch_to_non_abs_literal_fragment (&s);
  3975. frag_align (2, 0, 0);
  3976. record_alignment (now_seg, 2);
  3977. /* Close whatever frag is there. */
  3978. frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
  3979. xtensa_set_frag_assembly_state (frag_now);
  3980. frag_now->tc_frag_data.literal_frag = pool_location;
  3981. frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
  3982. xtensa_restore_emit_state (&s);
  3983. xtensa_set_frag_assembly_state (frag_now);
  3984. }
  3985. /* Build a nop of the correct size into tinsn. */
  3986. static void
  3987. build_nop (TInsn *tinsn, int size)
  3988. {
  3989. tinsn_init (tinsn);
  3990. switch (size)
  3991. {
  3992. case 2:
  3993. tinsn->opcode = xtensa_nop_n_opcode;
  3994. tinsn->ntok = 0;
  3995. if (tinsn->opcode == XTENSA_UNDEFINED)
  3996. as_fatal (_("opcode 'NOP.N' unavailable in this configuration"));
  3997. break;
  3998. case 3:
  3999. if (xtensa_nop_opcode == XTENSA_UNDEFINED)
  4000. {
  4001. tinsn->opcode = xtensa_or_opcode;
  4002. set_expr_const (&tinsn->tok[0], 1);
  4003. set_expr_const (&tinsn->tok[1], 1);
  4004. set_expr_const (&tinsn->tok[2], 1);
  4005. tinsn->ntok = 3;
  4006. }
  4007. else
  4008. tinsn->opcode = xtensa_nop_opcode;
  4009. gas_assert (tinsn->opcode != XTENSA_UNDEFINED);
  4010. }
  4011. }
  4012. /* Assemble a NOP of the requested size in the buffer. User must have
  4013. allocated "buf" with at least "size" bytes. */
  4014. static void
  4015. assemble_nop (int size, char *buf)
  4016. {
  4017. static xtensa_insnbuf insnbuf = NULL;
  4018. TInsn tinsn;
  4019. build_nop (&tinsn, size);
  4020. if (!insnbuf)
  4021. insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
  4022. tinsn_to_insnbuf (&tinsn, insnbuf);
  4023. xtensa_insnbuf_to_chars (xtensa_default_isa, insnbuf,
  4024. (unsigned char *) buf, 0);
  4025. }
  4026. /* Return the number of bytes for the offset of the expanded loop
  4027. instruction. This should be incorporated into the relaxation
  4028. specification but is hard-coded here. This is used to auto-align
  4029. the loop instruction. It is invalid to call this function if the
  4030. configuration does not have loops or if the opcode is not a loop
  4031. opcode. */
  4032. static addressT
  4033. get_expanded_loop_offset (xtensa_opcode opcode)
  4034. {
  4035. /* This is the OFFSET of the loop instruction in the expanded loop.
  4036. This MUST correspond directly to the specification of the loop
  4037. expansion. It will be validated on fragment conversion. */
  4038. gas_assert (opcode != XTENSA_UNDEFINED);
  4039. if (opcode == xtensa_loop_opcode)
  4040. return 0;
  4041. if (opcode == xtensa_loopnez_opcode)
  4042. return 3;
  4043. if (opcode == xtensa_loopgtz_opcode)
  4044. return 6;
  4045. as_fatal (_("get_expanded_loop_offset: invalid opcode"));
  4046. return 0;
  4047. }
  4048. static fragS *
  4049. get_literal_pool_location (segT seg)
  4050. {
  4051. struct litpool_seg *lps = litpool_seg_list.next;
  4052. struct litpool_frag *lpf;
  4053. for ( ; lps && lps->seg->id != seg->id; lps = lps->next)
  4054. ;
  4055. if (lps)
  4056. {
  4057. for (lpf = lps->frag_list.prev; lpf->fragP; lpf = lpf->prev)
  4058. { /* Skip "candidates" for now. */
  4059. if (lpf->fragP->fr_subtype == RELAX_LITERAL_POOL_BEGIN &&
  4060. lpf->priority == 1)
  4061. return lpf->fragP;
  4062. }
  4063. /* Must convert a lower-priority pool. */
  4064. for (lpf = lps->frag_list.prev; lpf->fragP; lpf = lpf->prev)
  4065. {
  4066. if (lpf->fragP->fr_subtype == RELAX_LITERAL_POOL_BEGIN)
  4067. return lpf->fragP;
  4068. }
  4069. /* Still no match -- try for a low priority pool. */
  4070. for (lpf = lps->frag_list.prev; lpf->fragP; lpf = lpf->prev)
  4071. {
  4072. if (lpf->fragP->fr_subtype == RELAX_LITERAL_POOL_CANDIDATE_BEGIN)
  4073. return lpf->fragP;
  4074. }
  4075. }
  4076. return seg_info (seg)->tc_segment_info_data.literal_pool_loc;
  4077. }
  4078. static void
  4079. set_literal_pool_location (segT seg, fragS *literal_pool_loc)
  4080. {
  4081. seg_info (seg)->tc_segment_info_data.literal_pool_loc = literal_pool_loc;
  4082. }
  4083. /* Set frag assembly state should be called when a new frag is
  4084. opened and after a frag has been closed. */
  4085. static void
  4086. xtensa_set_frag_assembly_state (fragS *fragP)
  4087. {
  4088. if (!density_supported)
  4089. fragP->tc_frag_data.is_no_density = TRUE;
  4090. /* This function is called from subsegs_finish, which is called
  4091. after xtensa_end, so we can't use "use_transform" or
  4092. "use_schedule" here. */
  4093. if (!directive_state[directive_transform])
  4094. fragP->tc_frag_data.is_no_transform = TRUE;
  4095. if (directive_state[directive_longcalls])
  4096. fragP->tc_frag_data.use_longcalls = TRUE;
  4097. fragP->tc_frag_data.use_absolute_literals =
  4098. directive_state[directive_absolute_literals];
  4099. fragP->tc_frag_data.is_assembly_state_set = TRUE;
  4100. }
  4101. static bfd_boolean
  4102. relaxable_section (asection *sec)
  4103. {
  4104. return ((sec->flags & SEC_DEBUGGING) == 0
  4105. && strcmp (sec->name, ".eh_frame") != 0);
  4106. }
  4107. static void
  4108. xtensa_mark_frags_for_org (void)
  4109. {
  4110. segT *seclist;
  4111. /* Walk over each fragment of all of the current segments. If we find
  4112. a .org frag in any of the segments, mark all frags prior to it as
  4113. "no transform", which will prevent linker optimizations from messing
  4114. up the .org distance. This should be done after
  4115. xtensa_find_unmarked_state_frags, because we don't want to worry here
  4116. about that function trashing the data we save here. */
  4117. for (seclist = &stdoutput->sections;
  4118. seclist && *seclist;
  4119. seclist = &(*seclist)->next)
  4120. {
  4121. segT sec = *seclist;
  4122. segment_info_type *seginfo;
  4123. fragS *fragP;
  4124. flagword flags;
  4125. flags = bfd_get_section_flags (stdoutput, sec);
  4126. if (flags & SEC_DEBUGGING)
  4127. continue;
  4128. if (!(flags & SEC_ALLOC))
  4129. continue;
  4130. seginfo = seg_info (sec);
  4131. if (seginfo && seginfo->frchainP)
  4132. {
  4133. fragS *last_fragP = seginfo->frchainP->frch_root;
  4134. for (fragP = seginfo->frchainP->frch_root; fragP;
  4135. fragP = fragP->fr_next)
  4136. {
  4137. /* cvt_frag_to_fill has changed the fr_type of org frags to
  4138. rs_fill, so use the value as cached in rs_subtype here. */
  4139. if (fragP->fr_subtype == RELAX_ORG)
  4140. {
  4141. while (last_fragP != fragP->fr_next)
  4142. {
  4143. last_fragP->tc_frag_data.is_no_transform = TRUE;
  4144. last_fragP = last_fragP->fr_next;
  4145. }
  4146. }
  4147. }
  4148. }
  4149. }
  4150. }
  4151. static void
  4152. xtensa_find_unmarked_state_frags (void)
  4153. {
  4154. segT *seclist;
  4155. /* Walk over each fragment of all of the current segments. For each
  4156. unmarked fragment, mark it with the same info as the previous
  4157. fragment. */
  4158. for (seclist = &stdoutput->sections;
  4159. seclist && *seclist;
  4160. seclist = &(*seclist)->next)
  4161. {
  4162. segT sec = *seclist;
  4163. segment_info_type *seginfo;
  4164. fragS *fragP;
  4165. flagword flags;
  4166. flags = bfd_get_section_flags (stdoutput, sec);
  4167. if (flags & SEC_DEBUGGING)
  4168. continue;
  4169. if (!(flags & SEC_ALLOC))
  4170. continue;
  4171. seginfo = seg_info (sec);
  4172. if (seginfo && seginfo->frchainP)
  4173. {
  4174. fragS *last_fragP = 0;
  4175. for (fragP = seginfo->frchainP->frch_root; fragP;
  4176. fragP = fragP->fr_next)
  4177. {
  4178. if (fragP->fr_fix != 0
  4179. && !fragP->tc_frag_data.is_assembly_state_set)
  4180. {
  4181. if (last_fragP == 0)
  4182. {
  4183. as_warn_where (fragP->fr_file, fragP->fr_line,
  4184. _("assembly state not set for first frag in section %s"),
  4185. sec->name);
  4186. }
  4187. else
  4188. {
  4189. fragP->tc_frag_data.is_assembly_state_set = TRUE;
  4190. fragP->tc_frag_data.is_no_density =
  4191. last_fragP->tc_frag_data.is_no_density;
  4192. fragP->tc_frag_data.is_no_transform =
  4193. last_fragP->tc_frag_data.is_no_transform;
  4194. fragP->tc_frag_data.use_longcalls =
  4195. last_fragP->tc_frag_data.use_longcalls;
  4196. fragP->tc_frag_data.use_absolute_literals =
  4197. last_fragP->tc_frag_data.use_absolute_literals;
  4198. }
  4199. }
  4200. if (fragP->tc_frag_data.is_assembly_state_set)
  4201. last_fragP = fragP;
  4202. }
  4203. }
  4204. }
  4205. }
  4206. static void
  4207. xtensa_find_unaligned_branch_targets (bfd *abfd ATTRIBUTE_UNUSED,
  4208. asection *sec,
  4209. void *unused ATTRIBUTE_UNUSED)
  4210. {
  4211. flagword flags = bfd_get_section_flags (abfd, sec);
  4212. segment_info_type *seginfo = seg_info (sec);
  4213. fragS *frag = seginfo->frchainP->frch_root;
  4214. if (flags & SEC_CODE)
  4215. {
  4216. xtensa_isa isa = xtensa_default_isa;
  4217. xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa);
  4218. while (frag != NULL)
  4219. {
  4220. if (frag->tc_frag_data.is_branch_target)
  4221. {
  4222. int op_size;
  4223. addressT branch_align, frag_addr;
  4224. xtensa_format fmt;
  4225. xtensa_insnbuf_from_chars
  4226. (isa, insnbuf, (unsigned char *) frag->fr_literal, 0);
  4227. fmt = xtensa_format_decode (isa, insnbuf);
  4228. op_size = xtensa_format_length (isa, fmt);
  4229. branch_align = 1 << branch_align_power (sec);
  4230. frag_addr = frag->fr_address % branch_align;
  4231. if (frag_addr + op_size > branch_align)
  4232. as_warn_where (frag->fr_file, frag->fr_line,
  4233. _("unaligned branch target: %d bytes at 0x%lx"),
  4234. op_size, (long) frag->fr_address);
  4235. }
  4236. frag = frag->fr_next;
  4237. }
  4238. xtensa_insnbuf_free (isa, insnbuf);
  4239. }
  4240. }
  4241. static void
  4242. xtensa_find_unaligned_loops (bfd *abfd ATTRIBUTE_UNUSED,
  4243. asection *sec,
  4244. void *unused ATTRIBUTE_UNUSED)
  4245. {
  4246. flagword flags = bfd_get_section_flags (abfd, sec);
  4247. segment_info_type *seginfo = seg_info (sec);
  4248. fragS *frag = seginfo->frchainP->frch_root;
  4249. xtensa_isa isa = xtensa_default_isa;
  4250. if (flags & SEC_CODE)
  4251. {
  4252. xtensa_insnbuf insnbuf = xtensa_insnbuf_alloc (isa);
  4253. while (frag != NULL)
  4254. {
  4255. if (frag->tc_frag_data.is_first_loop_insn)
  4256. {
  4257. int op_size;
  4258. addressT frag_addr;
  4259. xtensa_format fmt;
  4260. if (frag->fr_fix == 0)
  4261. frag = next_non_empty_frag (frag);
  4262. if (frag)
  4263. {
  4264. xtensa_insnbuf_from_chars
  4265. (isa, insnbuf, (unsigned char *) frag->fr_literal, 0);
  4266. fmt = xtensa_format_decode (isa, insnbuf);
  4267. op_size = xtensa_format_length (isa, fmt);
  4268. frag_addr = frag->fr_address % xtensa_fetch_width;
  4269. if (frag_addr + op_size > xtensa_fetch_width)
  4270. as_warn_where (frag->fr_file, frag->fr_line,
  4271. _("unaligned loop: %d bytes at 0x%lx"),
  4272. op_size, (long) frag->fr_address);
  4273. }
  4274. }
  4275. frag = frag->fr_next;
  4276. }
  4277. xtensa_insnbuf_free (isa, insnbuf);
  4278. }
  4279. }
  4280. static int
  4281. xg_apply_fix_value (fixS *fixP, valueT val)
  4282. {
  4283. xtensa_isa isa = xtensa_default_isa;
  4284. static xtensa_insnbuf insnbuf = NULL;
  4285. static xtensa_insnbuf slotbuf = NULL;
  4286. xtensa_format fmt;
  4287. int slot;
  4288. bfd_boolean alt_reloc;
  4289. xtensa_opcode opcode;
  4290. char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
  4291. if (decode_reloc (fixP->fx_r_type, &slot, &alt_reloc)
  4292. || alt_reloc)
  4293. as_fatal (_("unexpected fix"));
  4294. if (!insnbuf)
  4295. {
  4296. insnbuf = xtensa_insnbuf_alloc (isa);
  4297. slotbuf = xtensa_insnbuf_alloc (isa);
  4298. }
  4299. xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) fixpos, 0);
  4300. fmt = xtensa_format_decode (isa, insnbuf);
  4301. if (fmt == XTENSA_UNDEFINED)
  4302. as_fatal (_("undecodable fix"));
  4303. xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
  4304. opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
  4305. if (opcode == XTENSA_UNDEFINED)
  4306. as_fatal (_("undecodable fix"));
  4307. /* CONST16 immediates are not PC-relative, despite the fact that we
  4308. reuse the normal PC-relative operand relocations for the low part
  4309. of a CONST16 operand. */
  4310. if (opcode == xtensa_const16_opcode)
  4311. return 0;
  4312. xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode,
  4313. get_relaxable_immed (opcode), val,
  4314. fixP->fx_file, fixP->fx_line);
  4315. xtensa_format_set_slot (isa, fmt, slot, insnbuf, slotbuf);
  4316. xtensa_insnbuf_to_chars (isa, insnbuf, (unsigned char *) fixpos, 0);
  4317. return 1;
  4318. }
  4319. /* External Functions and Other GAS Hooks. */
  4320. const char *
  4321. xtensa_target_format (void)
  4322. {
  4323. return (target_big_endian ? "elf32-xtensa-be" : "elf32-xtensa-le");
  4324. }
  4325. void
  4326. xtensa_file_arch_init (bfd *abfd)
  4327. {
  4328. bfd_set_private_flags (abfd, 0x100 | 0x200);
  4329. }
  4330. void
  4331. md_number_to_chars (char *buf, valueT val, int n)
  4332. {
  4333. if (target_big_endian)
  4334. number_to_chars_bigendian (buf, val, n);
  4335. else
  4336. number_to_chars_littleendian (buf, val, n);
  4337. }
  4338. /* This function is called once, at assembler startup time. It should
  4339. set up all the tables, etc. that the MD part of the assembler will
  4340. need. */
  4341. void
  4342. md_begin (void)
  4343. {
  4344. segT current_section = now_seg;
  4345. int current_subsec = now_subseg;
  4346. xtensa_isa isa;
  4347. int i;
  4348. xtensa_default_isa = xtensa_isa_init (0, 0);
  4349. isa = xtensa_default_isa;
  4350. linkrelax = 1;
  4351. /* Set up the literal sections. */
  4352. memset (&default_lit_sections, 0, sizeof (default_lit_sections));
  4353. subseg_set (current_section, current_subsec);
  4354. xtensa_addi_opcode = xtensa_opcode_lookup (isa, "addi");
  4355. xtensa_addmi_opcode = xtensa_opcode_lookup (isa, "addmi");
  4356. xtensa_call0_opcode = xtensa_opcode_lookup (isa, "call0");
  4357. xtensa_call4_opcode = xtensa_opcode_lookup (isa, "call4");
  4358. xtensa_call8_opcode = xtensa_opcode_lookup (isa, "call8");
  4359. xtensa_call12_opcode = xtensa_opcode_lookup (isa, "call12");
  4360. xtensa_callx0_opcode = xtensa_opcode_lookup (isa, "callx0");
  4361. xtensa_callx4_opcode = xtensa_opcode_lookup (isa, "callx4");
  4362. xtensa_callx8_opcode = xtensa_opcode_lookup (isa, "callx8");
  4363. xtensa_callx12_opcode = xtensa_opcode_lookup (isa, "callx12");
  4364. xtensa_const16_opcode = xtensa_opcode_lookup (isa, "const16");
  4365. xtensa_entry_opcode = xtensa_opcode_lookup (isa, "entry");
  4366. xtensa_extui_opcode = xtensa_opcode_lookup (isa, "extui");
  4367. xtensa_movi_opcode = xtensa_opcode_lookup (isa, "movi");
  4368. xtensa_movi_n_opcode = xtensa_opcode_lookup (isa, "movi.n");
  4369. xtensa_isync_opcode = xtensa_opcode_lookup (isa, "isync");
  4370. xtensa_j_opcode = xtensa_opcode_lookup (isa, "j");
  4371. xtensa_jx_opcode = xtensa_opcode_lookup (isa, "jx");
  4372. xtensa_l32r_opcode = xtensa_opcode_lookup (isa, "l32r");
  4373. xtensa_loop_opcode = xtensa_opcode_lookup (isa, "loop");
  4374. xtensa_loopnez_opcode = xtensa_opcode_lookup (isa, "loopnez");
  4375. xtensa_loopgtz_opcode = xtensa_opcode_lookup (isa, "loopgtz");
  4376. xtensa_nop_opcode = xtensa_opcode_lookup (isa, "nop");
  4377. xtensa_nop_n_opcode = xtensa_opcode_lookup (isa, "nop.n");
  4378. xtensa_or_opcode = xtensa_opcode_lookup (isa, "or");
  4379. xtensa_ret_opcode = xtensa_opcode_lookup (isa, "ret");
  4380. xtensa_ret_n_opcode = xtensa_opcode_lookup (isa, "ret.n");
  4381. xtensa_retw_opcode = xtensa_opcode_lookup (isa, "retw");
  4382. xtensa_retw_n_opcode = xtensa_opcode_lookup (isa, "retw.n");
  4383. xtensa_rsr_lcount_opcode = xtensa_opcode_lookup (isa, "rsr.lcount");
  4384. xtensa_waiti_opcode = xtensa_opcode_lookup (isa, "waiti");
  4385. for (i = 0; i < xtensa_isa_num_formats (isa); i++)
  4386. {
  4387. int format_slots = xtensa_format_num_slots (isa, i);
  4388. if (format_slots > config_max_slots)
  4389. config_max_slots = format_slots;
  4390. }
  4391. xg_init_vinsn (&cur_vinsn);
  4392. xtensa_num_pipe_stages = xtensa_isa_num_pipe_stages (isa);
  4393. init_op_placement_info_table ();
  4394. /* Set up the assembly state. */
  4395. if (!frag_now->tc_frag_data.is_assembly_state_set)
  4396. xtensa_set_frag_assembly_state (frag_now);
  4397. }
  4398. /* TC_INIT_FIX_DATA hook */
  4399. void
  4400. xtensa_init_fix_data (fixS *x)
  4401. {
  4402. x->tc_fix_data.slot = 0;
  4403. x->tc_fix_data.X_add_symbol = NULL;
  4404. x->tc_fix_data.X_add_number = 0;
  4405. }
  4406. /* tc_frob_label hook */
  4407. void
  4408. xtensa_frob_label (symbolS *sym)
  4409. {
  4410. float freq;
  4411. if (cur_vinsn.inside_bundle)
  4412. {
  4413. as_bad (_("labels are not valid inside bundles"));
  4414. return;
  4415. }
  4416. freq = get_subseg_target_freq (now_seg, now_subseg);
  4417. /* Since the label was already attached to a frag associated with the
  4418. previous basic block, it now needs to be reset to the current frag. */
  4419. symbol_set_frag (sym, frag_now);
  4420. S_SET_VALUE (sym, (valueT) frag_now_fix ());
  4421. if (generating_literals)
  4422. xtensa_add_literal_sym (sym);
  4423. else
  4424. xtensa_add_insn_label (sym);
  4425. if (symbol_get_tc (sym)->is_loop_target)
  4426. {
  4427. if ((get_last_insn_flags (now_seg, now_subseg)
  4428. & FLAG_IS_BAD_LOOPEND) != 0)
  4429. as_bad (_("invalid last instruction for a zero-overhead loop"));
  4430. xtensa_set_frag_assembly_state (frag_now);
  4431. frag_var (rs_machine_dependent, 4, 4, RELAX_LOOP_END,
  4432. frag_now->fr_symbol, frag_now->fr_offset, NULL);
  4433. xtensa_set_frag_assembly_state (frag_now);
  4434. xtensa_move_labels (frag_now, 0);
  4435. }
  4436. /* No target aligning in the absolute section. */
  4437. if (now_seg != absolute_section
  4438. && !is_unaligned_label (sym)
  4439. && !generating_literals)
  4440. {
  4441. xtensa_set_frag_assembly_state (frag_now);
  4442. if (do_align_targets ())
  4443. frag_var (rs_machine_dependent, 0, (int) freq,
  4444. RELAX_DESIRE_ALIGN_IF_TARGET, frag_now->fr_symbol,
  4445. frag_now->fr_offset, NULL);
  4446. else
  4447. frag_var (rs_fill, 0, 0, frag_now->fr_subtype,
  4448. frag_now->fr_symbol, frag_now->fr_offset, NULL);
  4449. xtensa_set_frag_assembly_state (frag_now);
  4450. xtensa_move_labels (frag_now, 0);
  4451. }
  4452. /* We need to mark the following properties even if we aren't aligning. */
  4453. /* If the label is already known to be a branch target, i.e., a
  4454. forward branch, mark the frag accordingly. Backward branches
  4455. are handled by xg_add_branch_and_loop_targets. */
  4456. if (symbol_get_tc (sym)->is_branch_target)
  4457. symbol_get_frag (sym)->tc_frag_data.is_branch_target = TRUE;
  4458. /* Loops only go forward, so they can be identified here. */
  4459. if (symbol_get_tc (sym)->is_loop_target)
  4460. symbol_get_frag (sym)->tc_frag_data.is_loop_target = TRUE;
  4461. dwarf2_emit_label (sym);
  4462. }
  4463. /* tc_unrecognized_line hook */
  4464. int
  4465. xtensa_unrecognized_line (int ch)
  4466. {
  4467. switch (ch)
  4468. {
  4469. case '{' :
  4470. if (cur_vinsn.inside_bundle == 0)
  4471. {
  4472. /* PR8110: Cannot emit line number info inside a FLIX bundle
  4473. when using --gstabs. Temporarily disable debug info. */
  4474. generate_lineno_debug ();
  4475. if (debug_type == DEBUG_STABS)
  4476. {
  4477. xt_saved_debug_type = debug_type;
  4478. debug_type = DEBUG_NONE;
  4479. }
  4480. cur_vinsn.inside_bundle = 1;
  4481. }
  4482. else
  4483. {
  4484. as_bad (_("extra opening brace"));
  4485. return 0;
  4486. }
  4487. break;
  4488. case '}' :
  4489. if (cur_vinsn.inside_bundle)
  4490. finish_vinsn (&cur_vinsn);
  4491. else
  4492. {
  4493. as_bad (_("extra closing brace"));
  4494. return 0;
  4495. }
  4496. break;
  4497. default:
  4498. as_bad (_("syntax error"));
  4499. return 0;
  4500. }
  4501. return 1;
  4502. }
  4503. /* md_flush_pending_output hook */
  4504. void
  4505. xtensa_flush_pending_output (void)
  4506. {
  4507. /* This line fixes a bug where automatically generated gstabs info
  4508. separates a function label from its entry instruction, ending up
  4509. with the literal position between the function label and the entry
  4510. instruction and crashing code. It only happens with --gstabs and
  4511. --text-section-literals, and when several other obscure relaxation
  4512. conditions are met. */
  4513. if (outputting_stabs_line_debug)
  4514. return;
  4515. if (cur_vinsn.inside_bundle)
  4516. as_bad (_("missing closing brace"));
  4517. /* If there is a non-zero instruction fragment, close it. */
  4518. if (frag_now_fix () != 0 && frag_now->tc_frag_data.is_insn)
  4519. {
  4520. frag_wane (frag_now);
  4521. frag_new (0);
  4522. xtensa_set_frag_assembly_state (frag_now);
  4523. }
  4524. frag_now->tc_frag_data.is_insn = FALSE;
  4525. xtensa_clear_insn_labels ();
  4526. }
  4527. /* We had an error while parsing an instruction. The string might look
  4528. like this: "insn arg1, arg2 }". If so, we need to see the closing
  4529. brace and reset some fields. Otherwise, the vinsn never gets closed
  4530. and the num_slots field will grow past the end of the array of slots,
  4531. and bad things happen. */
  4532. static void
  4533. error_reset_cur_vinsn (void)
  4534. {
  4535. if (cur_vinsn.inside_bundle)
  4536. {
  4537. if (*input_line_pointer == '}'
  4538. || *(input_line_pointer - 1) == '}'
  4539. || *(input_line_pointer - 2) == '}')
  4540. xg_clear_vinsn (&cur_vinsn);
  4541. }
  4542. }
  4543. void
  4544. md_assemble (char *str)
  4545. {
  4546. xtensa_isa isa = xtensa_default_isa;
  4547. char *opname;
  4548. unsigned opnamelen;
  4549. bfd_boolean has_underbar = FALSE;
  4550. char *arg_strings[MAX_INSN_ARGS];
  4551. int num_args;
  4552. TInsn orig_insn; /* Original instruction from the input. */
  4553. tinsn_init (&orig_insn);
  4554. /* Split off the opcode. */
  4555. opnamelen = strspn (str, "abcdefghijklmnopqrstuvwxyz_/0123456789.");
  4556. opname = xmalloc (opnamelen + 1);
  4557. memcpy (opname, str, opnamelen);
  4558. opname[opnamelen] = '\0';
  4559. num_args = tokenize_arguments (arg_strings, str + opnamelen);
  4560. if (num_args == -1)
  4561. {
  4562. as_bad (_("syntax error"));
  4563. return;
  4564. }
  4565. if (xg_translate_idioms (&opname, &num_args, arg_strings))
  4566. return;
  4567. /* Check for an underbar prefix. */
  4568. if (*opname == '_')
  4569. {
  4570. has_underbar = TRUE;
  4571. opname += 1;
  4572. }
  4573. orig_insn.insn_type = ITYPE_INSN;
  4574. orig_insn.ntok = 0;
  4575. orig_insn.is_specific_opcode = (has_underbar || !use_transform ());
  4576. orig_insn.opcode = xtensa_opcode_lookup (isa, opname);
  4577. /* Special case: Check for "CALLXn.TLS" psuedo op. If found, grab its
  4578. extra argument and set the opcode to "CALLXn". */
  4579. if (orig_insn.opcode == XTENSA_UNDEFINED
  4580. && strncasecmp (opname, "callx", 5) == 0)
  4581. {
  4582. unsigned long window_size;
  4583. char *suffix;
  4584. window_size = strtoul (opname + 5, &suffix, 10);
  4585. if (suffix != opname + 5
  4586. && (window_size == 0
  4587. || window_size == 4
  4588. || window_size == 8
  4589. || window_size == 12)
  4590. && strcasecmp (suffix, ".tls") == 0)
  4591. {
  4592. switch (window_size)
  4593. {
  4594. case 0: orig_insn.opcode = xtensa_callx0_opcode; break;
  4595. case 4: orig_insn.opcode = xtensa_callx4_opcode; break;
  4596. case 8: orig_insn.opcode = xtensa_callx8_opcode; break;
  4597. case 12: orig_insn.opcode = xtensa_callx12_opcode; break;
  4598. }
  4599. if (num_args != 2)
  4600. as_bad (_("wrong number of operands for '%s'"), opname);
  4601. else
  4602. {
  4603. bfd_reloc_code_real_type reloc;
  4604. char *old_input_line_pointer;
  4605. expressionS *tok = &orig_insn.extra_arg;
  4606. old_input_line_pointer = input_line_pointer;
  4607. input_line_pointer = arg_strings[num_args - 1];
  4608. expression (tok);
  4609. if (tok->X_op == O_symbol
  4610. && ((reloc = xtensa_elf_suffix (&input_line_pointer, tok))
  4611. == BFD_RELOC_XTENSA_TLS_CALL))
  4612. tok->X_op = map_suffix_reloc_to_operator (reloc);
  4613. else
  4614. as_bad (_("bad relocation expression for '%s'"), opname);
  4615. input_line_pointer = old_input_line_pointer;
  4616. num_args -= 1;
  4617. }
  4618. }
  4619. }
  4620. /* Special case: Check for "j.l" psuedo op. */
  4621. if (orig_insn.opcode == XTENSA_UNDEFINED
  4622. && strncasecmp (opname, "j.l", 3) == 0)
  4623. {
  4624. if (num_args != 2)
  4625. as_bad (_("wrong number of operands for '%s'"), opname);
  4626. else
  4627. {
  4628. char *old_input_line_pointer;
  4629. expressionS *tok = &orig_insn.extra_arg;
  4630. old_input_line_pointer = input_line_pointer;
  4631. input_line_pointer = arg_strings[num_args - 1];
  4632. expression_maybe_register (xtensa_jx_opcode, 0, tok);
  4633. input_line_pointer = old_input_line_pointer;
  4634. num_args -= 1;
  4635. orig_insn.opcode = xtensa_j_opcode;
  4636. }
  4637. }
  4638. if (orig_insn.opcode == XTENSA_UNDEFINED)
  4639. {
  4640. xtensa_format fmt = xtensa_format_lookup (isa, opname);
  4641. if (fmt == XTENSA_UNDEFINED)
  4642. {
  4643. as_bad (_("unknown opcode or format name '%s'"), opname);
  4644. error_reset_cur_vinsn ();
  4645. return;
  4646. }
  4647. if (!cur_vinsn.inside_bundle)
  4648. {
  4649. as_bad (_("format names only valid inside bundles"));
  4650. error_reset_cur_vinsn ();
  4651. return;
  4652. }
  4653. if (cur_vinsn.format != XTENSA_UNDEFINED)
  4654. as_warn (_("multiple formats specified for one bundle; using '%s'"),
  4655. opname);
  4656. cur_vinsn.format = fmt;
  4657. free (has_underbar ? opname - 1 : opname);
  4658. error_reset_cur_vinsn ();
  4659. return;
  4660. }
  4661. /* Parse the arguments. */
  4662. if (parse_arguments (&orig_insn, num_args, arg_strings))
  4663. {
  4664. as_bad (_("syntax error"));
  4665. error_reset_cur_vinsn ();
  4666. return;
  4667. }
  4668. /* Free the opcode and argument strings, now that they've been parsed. */
  4669. free (has_underbar ? opname - 1 : opname);
  4670. opname = 0;
  4671. while (num_args-- > 0)
  4672. free (arg_strings[num_args]);
  4673. /* Get expressions for invisible operands. */
  4674. if (get_invisible_operands (&orig_insn))
  4675. {
  4676. error_reset_cur_vinsn ();
  4677. return;
  4678. }
  4679. /* Check for the right number and type of arguments. */
  4680. if (tinsn_check_arguments (&orig_insn))
  4681. {
  4682. error_reset_cur_vinsn ();
  4683. return;
  4684. }
  4685. /* Record the line number for each TInsn, because a FLIX bundle may be
  4686. spread across multiple input lines and individual instructions may be
  4687. moved around in some cases. */
  4688. orig_insn.loc_directive_seen = dwarf2_loc_directive_seen;
  4689. dwarf2_where (&orig_insn.debug_line);
  4690. dwarf2_consume_line_info ();
  4691. xg_add_branch_and_loop_targets (&orig_insn);
  4692. /* Check that immediate value for ENTRY is >= 16. */
  4693. if (orig_insn.opcode == xtensa_entry_opcode && orig_insn.ntok >= 3)
  4694. {
  4695. expressionS *exp = &orig_insn.tok[2];
  4696. if (exp->X_op == O_constant && exp->X_add_number < 16)
  4697. as_warn (_("entry instruction with stack decrement < 16"));
  4698. }
  4699. /* Finish it off:
  4700. assemble_tokens (opcode, tok, ntok);
  4701. expand the tokens from the orig_insn into the
  4702. stack of instructions that will not expand
  4703. unless required at relaxation time. */
  4704. if (!cur_vinsn.inside_bundle)
  4705. emit_single_op (&orig_insn);
  4706. else /* We are inside a bundle. */
  4707. {
  4708. cur_vinsn.slots[cur_vinsn.num_slots] = orig_insn;
  4709. cur_vinsn.num_slots++;
  4710. if (*input_line_pointer == '}'
  4711. || *(input_line_pointer - 1) == '}'
  4712. || *(input_line_pointer - 2) == '}')
  4713. finish_vinsn (&cur_vinsn);
  4714. }
  4715. /* We've just emitted a new instruction so clear the list of labels. */
  4716. xtensa_clear_insn_labels ();
  4717. xtensa_check_frag_count ();
  4718. }
  4719. /* HANDLE_ALIGN hook */
  4720. /* For a .align directive, we mark the previous block with the alignment
  4721. information. This will be placed in the object file in the
  4722. property section corresponding to this section. */
  4723. void
  4724. xtensa_handle_align (fragS *fragP)
  4725. {
  4726. if (linkrelax
  4727. && ! fragP->tc_frag_data.is_literal
  4728. && (fragP->fr_type == rs_align
  4729. || fragP->fr_type == rs_align_code)
  4730. && fragP->fr_offset > 0
  4731. && now_seg != bss_section)
  4732. {
  4733. fragP->tc_frag_data.is_align = TRUE;
  4734. fragP->tc_frag_data.alignment = fragP->fr_offset;
  4735. }
  4736. if (fragP->fr_type == rs_align_test)
  4737. {
  4738. int count;
  4739. count = fragP->fr_next->fr_address - fragP->fr_address - fragP->fr_fix;
  4740. if (count != 0)
  4741. as_bad_where (fragP->fr_file, fragP->fr_line,
  4742. _("unaligned entry instruction"));
  4743. }
  4744. if (linkrelax && fragP->fr_type == rs_org)
  4745. fragP->fr_subtype = RELAX_ORG;
  4746. }
  4747. /* TC_FRAG_INIT hook */
  4748. void
  4749. xtensa_frag_init (fragS *frag)
  4750. {
  4751. xtensa_set_frag_assembly_state (frag);
  4752. }
  4753. symbolS *
  4754. md_undefined_symbol (char *name ATTRIBUTE_UNUSED)
  4755. {
  4756. return NULL;
  4757. }
  4758. /* Round up a section size to the appropriate boundary. */
  4759. valueT
  4760. md_section_align (segT segment ATTRIBUTE_UNUSED, valueT size)
  4761. {
  4762. return size; /* Byte alignment is fine. */
  4763. }
  4764. long
  4765. md_pcrel_from (fixS *fixP)
  4766. {
  4767. char *insn_p;
  4768. static xtensa_insnbuf insnbuf = NULL;
  4769. static xtensa_insnbuf slotbuf = NULL;
  4770. int opnum;
  4771. uint32 opnd_value;
  4772. xtensa_opcode opcode;
  4773. xtensa_format fmt;
  4774. int slot;
  4775. xtensa_isa isa = xtensa_default_isa;
  4776. valueT addr = fixP->fx_where + fixP->fx_frag->fr_address;
  4777. bfd_boolean alt_reloc;
  4778. if (fixP->fx_r_type == BFD_RELOC_XTENSA_ASM_EXPAND)
  4779. return 0;
  4780. if (fixP->fx_r_type == BFD_RELOC_32_PCREL)
  4781. return addr;
  4782. if (!insnbuf)
  4783. {
  4784. insnbuf = xtensa_insnbuf_alloc (isa);
  4785. slotbuf = xtensa_insnbuf_alloc (isa);
  4786. }
  4787. insn_p = &fixP->fx_frag->fr_literal[fixP->fx_where];
  4788. xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) insn_p, 0);
  4789. fmt = xtensa_format_decode (isa, insnbuf);
  4790. if (fmt == XTENSA_UNDEFINED)
  4791. as_fatal (_("bad instruction format"));
  4792. if (decode_reloc (fixP->fx_r_type, &slot, &alt_reloc) != 0)
  4793. as_fatal (_("invalid relocation"));
  4794. xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
  4795. opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
  4796. /* Check for "alternate" relocations (operand not specified). None
  4797. of the current uses for these are really PC-relative. */
  4798. if (alt_reloc || opcode == xtensa_const16_opcode)
  4799. {
  4800. if (opcode != xtensa_l32r_opcode
  4801. && opcode != xtensa_const16_opcode)
  4802. as_fatal (_("invalid relocation for '%s' instruction"),
  4803. xtensa_opcode_name (isa, opcode));
  4804. return 0;
  4805. }
  4806. opnum = get_relaxable_immed (opcode);
  4807. opnd_value = 0;
  4808. if (xtensa_operand_is_PCrelative (isa, opcode, opnum) != 1
  4809. || xtensa_operand_do_reloc (isa, opcode, opnum, &opnd_value, addr))
  4810. {
  4811. as_bad_where (fixP->fx_file,
  4812. fixP->fx_line,
  4813. _("invalid relocation for operand %d of '%s'"),
  4814. opnum, xtensa_opcode_name (isa, opcode));
  4815. return 0;
  4816. }
  4817. return 0 - opnd_value;
  4818. }
  4819. /* TC_FORCE_RELOCATION hook */
  4820. int
  4821. xtensa_force_relocation (fixS *fix)
  4822. {
  4823. switch (fix->fx_r_type)
  4824. {
  4825. case BFD_RELOC_XTENSA_ASM_EXPAND:
  4826. case BFD_RELOC_XTENSA_SLOT0_ALT:
  4827. case BFD_RELOC_XTENSA_SLOT1_ALT:
  4828. case BFD_RELOC_XTENSA_SLOT2_ALT:
  4829. case BFD_RELOC_XTENSA_SLOT3_ALT:
  4830. case BFD_RELOC_XTENSA_SLOT4_ALT:
  4831. case BFD_RELOC_XTENSA_SLOT5_ALT:
  4832. case BFD_RELOC_XTENSA_SLOT6_ALT:
  4833. case BFD_RELOC_XTENSA_SLOT7_ALT:
  4834. case BFD_RELOC_XTENSA_SLOT8_ALT:
  4835. case BFD_RELOC_XTENSA_SLOT9_ALT:
  4836. case BFD_RELOC_XTENSA_SLOT10_ALT:
  4837. case BFD_RELOC_XTENSA_SLOT11_ALT:
  4838. case BFD_RELOC_XTENSA_SLOT12_ALT:
  4839. case BFD_RELOC_XTENSA_SLOT13_ALT:
  4840. case BFD_RELOC_XTENSA_SLOT14_ALT:
  4841. return 1;
  4842. default:
  4843. break;
  4844. }
  4845. if (linkrelax && fix->fx_addsy
  4846. && relaxable_section (S_GET_SEGMENT (fix->fx_addsy)))
  4847. return 1;
  4848. return generic_force_reloc (fix);
  4849. }
  4850. /* TC_VALIDATE_FIX_SUB hook */
  4851. int
  4852. xtensa_validate_fix_sub (fixS *fix)
  4853. {
  4854. segT add_symbol_segment, sub_symbol_segment;
  4855. /* The difference of two symbols should be resolved by the assembler when
  4856. linkrelax is not set. If the linker may relax the section containing
  4857. the symbols, then an Xtensa DIFF relocation must be generated so that
  4858. the linker knows to adjust the difference value. */
  4859. if (!linkrelax || fix->fx_addsy == NULL)
  4860. return 0;
  4861. /* Make sure both symbols are in the same segment, and that segment is
  4862. "normal" and relaxable. If the segment is not "normal", then the
  4863. fix is not valid. If the segment is not "relaxable", then the fix
  4864. should have been handled earlier. */
  4865. add_symbol_segment = S_GET_SEGMENT (fix->fx_addsy);
  4866. if (! SEG_NORMAL (add_symbol_segment) ||
  4867. ! relaxable_section (add_symbol_segment))
  4868. return 0;
  4869. sub_symbol_segment = S_GET_SEGMENT (fix->fx_subsy);
  4870. return (sub_symbol_segment == add_symbol_segment);
  4871. }
  4872. /* NO_PSEUDO_DOT hook */
  4873. /* This function has nothing to do with pseudo dots, but this is the
  4874. nearest macro to where the check needs to take place. FIXME: This
  4875. seems wrong. */
  4876. bfd_boolean
  4877. xtensa_check_inside_bundle (void)
  4878. {
  4879. if (cur_vinsn.inside_bundle && input_line_pointer[-1] == '.')
  4880. as_bad (_("directives are not valid inside bundles"));
  4881. /* This function must always return FALSE because it is called via a
  4882. macro that has nothing to do with bundling. */
  4883. return FALSE;
  4884. }
  4885. /* md_elf_section_change_hook */
  4886. void
  4887. xtensa_elf_section_change_hook (void)
  4888. {
  4889. /* Set up the assembly state. */
  4890. if (!frag_now->tc_frag_data.is_assembly_state_set)
  4891. xtensa_set_frag_assembly_state (frag_now);
  4892. }
  4893. /* tc_fix_adjustable hook */
  4894. bfd_boolean
  4895. xtensa_fix_adjustable (fixS *fixP)
  4896. {
  4897. /* We need the symbol name for the VTABLE entries. */
  4898. if (fixP->fx_r_type == BFD_RELOC_VTABLE_INHERIT
  4899. || fixP->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
  4900. return 0;
  4901. return 1;
  4902. }
  4903. /* tc_symbol_new_hook */
  4904. symbolS *expr_symbols = NULL;
  4905. void
  4906. xtensa_symbol_new_hook (symbolS *sym)
  4907. {
  4908. if (is_leb128_expr && S_GET_SEGMENT (sym) == expr_section)
  4909. {
  4910. symbol_get_tc (sym)->next_expr_symbol = expr_symbols;
  4911. expr_symbols = sym;
  4912. }
  4913. }
  4914. void
  4915. md_apply_fix (fixS *fixP, valueT *valP, segT seg)
  4916. {
  4917. char *const fixpos = fixP->fx_frag->fr_literal + fixP->fx_where;
  4918. valueT val = 0;
  4919. /* Subtracted symbols are only allowed for a few relocation types, and
  4920. unless linkrelax is enabled, they should not make it to this point. */
  4921. if (fixP->fx_subsy && !(linkrelax && (fixP->fx_r_type == BFD_RELOC_32
  4922. || fixP->fx_r_type == BFD_RELOC_16
  4923. || fixP->fx_r_type == BFD_RELOC_8)))
  4924. as_bad_where (fixP->fx_file, fixP->fx_line, _("expression too complex"));
  4925. switch (fixP->fx_r_type)
  4926. {
  4927. case BFD_RELOC_32_PCREL:
  4928. case BFD_RELOC_32:
  4929. case BFD_RELOC_16:
  4930. case BFD_RELOC_8:
  4931. if (fixP->fx_subsy)
  4932. {
  4933. switch (fixP->fx_r_type)
  4934. {
  4935. case BFD_RELOC_8:
  4936. fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF8;
  4937. fixP->fx_signed = 1;
  4938. break;
  4939. case BFD_RELOC_16:
  4940. fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF16;
  4941. fixP->fx_signed = 1;
  4942. break;
  4943. case BFD_RELOC_32:
  4944. fixP->fx_r_type = BFD_RELOC_XTENSA_DIFF32;
  4945. fixP->fx_signed = 1;
  4946. break;
  4947. default:
  4948. break;
  4949. }
  4950. val = (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset
  4951. - S_GET_VALUE (fixP->fx_subsy));
  4952. /* The difference value gets written out, and the DIFF reloc
  4953. identifies the address of the subtracted symbol (i.e., the one
  4954. with the lowest address). */
  4955. *valP = val;
  4956. fixP->fx_offset -= val;
  4957. fixP->fx_subsy = NULL;
  4958. }
  4959. else if (! fixP->fx_addsy)
  4960. {
  4961. val = *valP;
  4962. fixP->fx_done = 1;
  4963. }
  4964. /* fall through */
  4965. case BFD_RELOC_XTENSA_PLT:
  4966. md_number_to_chars (fixpos, val, fixP->fx_size);
  4967. fixP->fx_no_overflow = 0; /* Use the standard overflow check. */
  4968. break;
  4969. case BFD_RELOC_XTENSA_TLSDESC_FN:
  4970. case BFD_RELOC_XTENSA_TLSDESC_ARG:
  4971. case BFD_RELOC_XTENSA_TLS_TPOFF:
  4972. case BFD_RELOC_XTENSA_TLS_DTPOFF:
  4973. S_SET_THREAD_LOCAL (fixP->fx_addsy);
  4974. md_number_to_chars (fixpos, 0, fixP->fx_size);
  4975. fixP->fx_no_overflow = 0; /* Use the standard overflow check. */
  4976. break;
  4977. case BFD_RELOC_XTENSA_SLOT0_OP:
  4978. case BFD_RELOC_XTENSA_SLOT1_OP:
  4979. case BFD_RELOC_XTENSA_SLOT2_OP:
  4980. case BFD_RELOC_XTENSA_SLOT3_OP:
  4981. case BFD_RELOC_XTENSA_SLOT4_OP:
  4982. case BFD_RELOC_XTENSA_SLOT5_OP:
  4983. case BFD_RELOC_XTENSA_SLOT6_OP:
  4984. case BFD_RELOC_XTENSA_SLOT7_OP:
  4985. case BFD_RELOC_XTENSA_SLOT8_OP:
  4986. case BFD_RELOC_XTENSA_SLOT9_OP:
  4987. case BFD_RELOC_XTENSA_SLOT10_OP:
  4988. case BFD_RELOC_XTENSA_SLOT11_OP:
  4989. case BFD_RELOC_XTENSA_SLOT12_OP:
  4990. case BFD_RELOC_XTENSA_SLOT13_OP:
  4991. case BFD_RELOC_XTENSA_SLOT14_OP:
  4992. if (linkrelax)
  4993. {
  4994. /* Write the tentative value of a PC-relative relocation to a
  4995. local symbol into the instruction. The value will be ignored
  4996. by the linker, and it makes the object file disassembly
  4997. readable when all branch targets are encoded in relocations. */
  4998. gas_assert (fixP->fx_addsy);
  4999. if (S_GET_SEGMENT (fixP->fx_addsy) == seg
  5000. && !S_FORCE_RELOC (fixP->fx_addsy, 1))
  5001. {
  5002. val = (S_GET_VALUE (fixP->fx_addsy) + fixP->fx_offset
  5003. - md_pcrel_from (fixP));
  5004. (void) xg_apply_fix_value (fixP, val);
  5005. }
  5006. }
  5007. else if (! fixP->fx_addsy)
  5008. {
  5009. val = *valP;
  5010. if (xg_apply_fix_value (fixP, val))
  5011. fixP->fx_done = 1;
  5012. }
  5013. break;
  5014. case BFD_RELOC_XTENSA_ASM_EXPAND:
  5015. case BFD_RELOC_XTENSA_TLS_FUNC:
  5016. case BFD_RELOC_XTENSA_TLS_ARG:
  5017. case BFD_RELOC_XTENSA_TLS_CALL:
  5018. case BFD_RELOC_XTENSA_SLOT0_ALT:
  5019. case BFD_RELOC_XTENSA_SLOT1_ALT:
  5020. case BFD_RELOC_XTENSA_SLOT2_ALT:
  5021. case BFD_RELOC_XTENSA_SLOT3_ALT:
  5022. case BFD_RELOC_XTENSA_SLOT4_ALT:
  5023. case BFD_RELOC_XTENSA_SLOT5_ALT:
  5024. case BFD_RELOC_XTENSA_SLOT6_ALT:
  5025. case BFD_RELOC_XTENSA_SLOT7_ALT:
  5026. case BFD_RELOC_XTENSA_SLOT8_ALT:
  5027. case BFD_RELOC_XTENSA_SLOT9_ALT:
  5028. case BFD_RELOC_XTENSA_SLOT10_ALT:
  5029. case BFD_RELOC_XTENSA_SLOT11_ALT:
  5030. case BFD_RELOC_XTENSA_SLOT12_ALT:
  5031. case BFD_RELOC_XTENSA_SLOT13_ALT:
  5032. case BFD_RELOC_XTENSA_SLOT14_ALT:
  5033. /* These all need to be resolved at link-time. Do nothing now. */
  5034. break;
  5035. case BFD_RELOC_VTABLE_INHERIT:
  5036. case BFD_RELOC_VTABLE_ENTRY:
  5037. fixP->fx_done = 0;
  5038. break;
  5039. default:
  5040. as_bad (_("unhandled local relocation fix %s"),
  5041. bfd_get_reloc_code_name (fixP->fx_r_type));
  5042. }
  5043. }
  5044. char *
  5045. md_atof (int type, char *litP, int *sizeP)
  5046. {
  5047. return ieee_md_atof (type, litP, sizeP, target_big_endian);
  5048. }
  5049. int
  5050. md_estimate_size_before_relax (fragS *fragP, segT seg ATTRIBUTE_UNUSED)
  5051. {
  5052. return total_frag_text_expansion (fragP);
  5053. }
  5054. /* Translate internal representation of relocation info to BFD target
  5055. format. */
  5056. arelent *
  5057. tc_gen_reloc (asection *section ATTRIBUTE_UNUSED, fixS *fixp)
  5058. {
  5059. arelent *reloc;
  5060. reloc = (arelent *) xmalloc (sizeof (arelent));
  5061. reloc->sym_ptr_ptr = (asymbol **) xmalloc (sizeof (asymbol *));
  5062. *reloc->sym_ptr_ptr = symbol_get_bfdsym (fixp->fx_addsy);
  5063. reloc->address = fixp->fx_frag->fr_address + fixp->fx_where;
  5064. /* Make sure none of our internal relocations make it this far.
  5065. They'd better have been fully resolved by this point. */
  5066. gas_assert ((int) fixp->fx_r_type > 0);
  5067. reloc->addend = fixp->fx_offset;
  5068. reloc->howto = bfd_reloc_type_lookup (stdoutput, fixp->fx_r_type);
  5069. if (reloc->howto == NULL)
  5070. {
  5071. as_bad_where (fixp->fx_file, fixp->fx_line,
  5072. _("cannot represent `%s' relocation in object file"),
  5073. bfd_get_reloc_code_name (fixp->fx_r_type));
  5074. free (reloc->sym_ptr_ptr);
  5075. free (reloc);
  5076. return NULL;
  5077. }
  5078. if (!fixp->fx_pcrel != !reloc->howto->pc_relative)
  5079. as_fatal (_("internal error; cannot generate `%s' relocation"),
  5080. bfd_get_reloc_code_name (fixp->fx_r_type));
  5081. return reloc;
  5082. }
  5083. /* Checks for resource conflicts between instructions. */
  5084. /* The func unit stuff could be implemented as bit-vectors rather
  5085. than the iterative approach here. If it ends up being too
  5086. slow, we will switch it. */
  5087. resource_table *
  5088. new_resource_table (void *data,
  5089. int cycles,
  5090. int nu,
  5091. unit_num_copies_func uncf,
  5092. opcode_num_units_func onuf,
  5093. opcode_funcUnit_use_unit_func ouuf,
  5094. opcode_funcUnit_use_stage_func ousf)
  5095. {
  5096. int i;
  5097. resource_table *rt = (resource_table *) xmalloc (sizeof (resource_table));
  5098. rt->data = data;
  5099. rt->cycles = cycles;
  5100. rt->allocated_cycles = cycles;
  5101. rt->num_units = nu;
  5102. rt->unit_num_copies = uncf;
  5103. rt->opcode_num_units = onuf;
  5104. rt->opcode_unit_use = ouuf;
  5105. rt->opcode_unit_stage = ousf;
  5106. rt->units = (unsigned char **) xcalloc (cycles, sizeof (unsigned char *));
  5107. for (i = 0; i < cycles; i++)
  5108. rt->units[i] = (unsigned char *) xcalloc (nu, sizeof (unsigned char));
  5109. return rt;
  5110. }
  5111. void
  5112. clear_resource_table (resource_table *rt)
  5113. {
  5114. int i, j;
  5115. for (i = 0; i < rt->allocated_cycles; i++)
  5116. for (j = 0; j < rt->num_units; j++)
  5117. rt->units[i][j] = 0;
  5118. }
  5119. /* We never shrink it, just fake it into thinking so. */
  5120. void
  5121. resize_resource_table (resource_table *rt, int cycles)
  5122. {
  5123. int i, old_cycles;
  5124. rt->cycles = cycles;
  5125. if (cycles <= rt->allocated_cycles)
  5126. return;
  5127. old_cycles = rt->allocated_cycles;
  5128. rt->allocated_cycles = cycles;
  5129. rt->units = xrealloc (rt->units,
  5130. rt->allocated_cycles * sizeof (unsigned char *));
  5131. for (i = 0; i < old_cycles; i++)
  5132. rt->units[i] = xrealloc (rt->units[i],
  5133. rt->num_units * sizeof (unsigned char));
  5134. for (i = old_cycles; i < cycles; i++)
  5135. rt->units[i] = xcalloc (rt->num_units, sizeof (unsigned char));
  5136. }
  5137. bfd_boolean
  5138. resources_available (resource_table *rt, xtensa_opcode opcode, int cycle)
  5139. {
  5140. int i;
  5141. int uses = (rt->opcode_num_units) (rt->data, opcode);
  5142. for (i = 0; i < uses; i++)
  5143. {
  5144. xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
  5145. int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
  5146. int copies_in_use = rt->units[stage + cycle][unit];
  5147. int copies = (rt->unit_num_copies) (rt->data, unit);
  5148. if (copies_in_use >= copies)
  5149. return FALSE;
  5150. }
  5151. return TRUE;
  5152. }
  5153. void
  5154. reserve_resources (resource_table *rt, xtensa_opcode opcode, int cycle)
  5155. {
  5156. int i;
  5157. int uses = (rt->opcode_num_units) (rt->data, opcode);
  5158. for (i = 0; i < uses; i++)
  5159. {
  5160. xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
  5161. int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
  5162. /* Note that this allows resources to be oversubscribed. That's
  5163. essential to the way the optional scheduler works.
  5164. resources_available reports when a resource is over-subscribed,
  5165. so it's easy to tell. */
  5166. rt->units[stage + cycle][unit]++;
  5167. }
  5168. }
  5169. void
  5170. release_resources (resource_table *rt, xtensa_opcode opcode, int cycle)
  5171. {
  5172. int i;
  5173. int uses = (rt->opcode_num_units) (rt->data, opcode);
  5174. for (i = 0; i < uses; i++)
  5175. {
  5176. xtensa_funcUnit unit = (rt->opcode_unit_use) (rt->data, opcode, i);
  5177. int stage = (rt->opcode_unit_stage) (rt->data, opcode, i);
  5178. gas_assert (rt->units[stage + cycle][unit] > 0);
  5179. rt->units[stage + cycle][unit]--;
  5180. }
  5181. }
  5182. /* Wrapper functions make parameterized resource reservation
  5183. more convenient. */
  5184. int
  5185. opcode_funcUnit_use_unit (void *data, xtensa_opcode opcode, int idx)
  5186. {
  5187. xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx);
  5188. return use->unit;
  5189. }
  5190. int
  5191. opcode_funcUnit_use_stage (void *data, xtensa_opcode opcode, int idx)
  5192. {
  5193. xtensa_funcUnit_use *use = xtensa_opcode_funcUnit_use (data, opcode, idx);
  5194. return use->stage;
  5195. }
  5196. /* Note that this function does not check issue constraints, but
  5197. solely whether the hardware is available to execute the given
  5198. instructions together. It also doesn't check if the tinsns
  5199. write the same state, or access the same tieports. That is
  5200. checked by check_t1_t2_reads_and_writes. */
  5201. static bfd_boolean
  5202. resources_conflict (vliw_insn *vinsn)
  5203. {
  5204. int i;
  5205. static resource_table *rt = NULL;
  5206. /* This is the most common case by far. Optimize it. */
  5207. if (vinsn->num_slots == 1)
  5208. return FALSE;
  5209. if (rt == NULL)
  5210. {
  5211. xtensa_isa isa = xtensa_default_isa;
  5212. rt = new_resource_table
  5213. (isa, xtensa_num_pipe_stages,
  5214. xtensa_isa_num_funcUnits (isa),
  5215. (unit_num_copies_func) xtensa_funcUnit_num_copies,
  5216. (opcode_num_units_func) xtensa_opcode_num_funcUnit_uses,
  5217. opcode_funcUnit_use_unit,
  5218. opcode_funcUnit_use_stage);
  5219. }
  5220. clear_resource_table (rt);
  5221. for (i = 0; i < vinsn->num_slots; i++)
  5222. {
  5223. if (!resources_available (rt, vinsn->slots[i].opcode, 0))
  5224. return TRUE;
  5225. reserve_resources (rt, vinsn->slots[i].opcode, 0);
  5226. }
  5227. return FALSE;
  5228. }
  5229. /* finish_vinsn, emit_single_op and helper functions. */
  5230. static bfd_boolean find_vinsn_conflicts (vliw_insn *);
  5231. static xtensa_format xg_find_narrowest_format (vliw_insn *);
  5232. static void xg_assemble_vliw_tokens (vliw_insn *);
  5233. /* We have reached the end of a bundle; emit into the frag. */
  5234. static void
  5235. finish_vinsn (vliw_insn *vinsn)
  5236. {
  5237. IStack slotstack;
  5238. int i;
  5239. char *file_name;
  5240. unsigned line;
  5241. if (find_vinsn_conflicts (vinsn))
  5242. {
  5243. xg_clear_vinsn (vinsn);
  5244. return;
  5245. }
  5246. /* First, find a format that works. */
  5247. if (vinsn->format == XTENSA_UNDEFINED)
  5248. vinsn->format = xg_find_narrowest_format (vinsn);
  5249. if (xtensa_format_num_slots (xtensa_default_isa, vinsn->format) > 1
  5250. && produce_flix == FLIX_NONE)
  5251. {
  5252. as_bad (_("The option \"--no-allow-flix\" prohibits multi-slot flix."));
  5253. xg_clear_vinsn (vinsn);
  5254. return;
  5255. }
  5256. if (vinsn->format == XTENSA_UNDEFINED)
  5257. {
  5258. as_where (&file_name, &line);
  5259. as_bad_where (file_name, line,
  5260. _("couldn't find a valid instruction format"));
  5261. fprintf (stderr, _(" ops were: "));
  5262. for (i = 0; i < vinsn->num_slots; i++)
  5263. fprintf (stderr, _(" %s;"),
  5264. xtensa_opcode_name (xtensa_default_isa,
  5265. vinsn->slots[i].opcode));
  5266. fprintf (stderr, _("\n"));
  5267. xg_clear_vinsn (vinsn);
  5268. return;
  5269. }
  5270. if (vinsn->num_slots
  5271. != xtensa_format_num_slots (xtensa_default_isa, vinsn->format))
  5272. {
  5273. as_bad (_("format '%s' allows %d slots, but there are %d opcodes"),
  5274. xtensa_format_name (xtensa_default_isa, vinsn->format),
  5275. xtensa_format_num_slots (xtensa_default_isa, vinsn->format),
  5276. vinsn->num_slots);
  5277. xg_clear_vinsn (vinsn);
  5278. return;
  5279. }
  5280. if (resources_conflict (vinsn))
  5281. {
  5282. as_where (&file_name, &line);
  5283. as_bad_where (file_name, line, _("illegal resource usage in bundle"));
  5284. fprintf (stderr, " ops were: ");
  5285. for (i = 0; i < vinsn->num_slots; i++)
  5286. fprintf (stderr, " %s;",
  5287. xtensa_opcode_name (xtensa_default_isa,
  5288. vinsn->slots[i].opcode));
  5289. fprintf (stderr, "\n");
  5290. xg_clear_vinsn (vinsn);
  5291. return;
  5292. }
  5293. for (i = 0; i < vinsn->num_slots; i++)
  5294. {
  5295. if (vinsn->slots[i].opcode != XTENSA_UNDEFINED)
  5296. {
  5297. symbolS *lit_sym = NULL;
  5298. int j;
  5299. bfd_boolean e = FALSE;
  5300. bfd_boolean saved_density = density_supported;
  5301. /* We don't want to narrow ops inside multi-slot bundles. */
  5302. if (vinsn->num_slots > 1)
  5303. density_supported = FALSE;
  5304. istack_init (&slotstack);
  5305. if (vinsn->slots[i].opcode == xtensa_nop_opcode)
  5306. {
  5307. vinsn->slots[i].opcode =
  5308. xtensa_format_slot_nop_opcode (xtensa_default_isa,
  5309. vinsn->format, i);
  5310. vinsn->slots[i].ntok = 0;
  5311. }
  5312. if (xg_expand_assembly_insn (&slotstack, &vinsn->slots[i]))
  5313. {
  5314. e = TRUE;
  5315. continue;
  5316. }
  5317. density_supported = saved_density;
  5318. if (e)
  5319. {
  5320. xg_clear_vinsn (vinsn);
  5321. return;
  5322. }
  5323. for (j = 0; j < slotstack.ninsn; j++)
  5324. {
  5325. TInsn *insn = &slotstack.insn[j];
  5326. if (insn->insn_type == ITYPE_LITERAL)
  5327. {
  5328. gas_assert (lit_sym == NULL);
  5329. lit_sym = xg_assemble_literal (insn);
  5330. }
  5331. else
  5332. {
  5333. gas_assert (insn->insn_type == ITYPE_INSN);
  5334. if (lit_sym)
  5335. xg_resolve_literals (insn, lit_sym);
  5336. if (j != slotstack.ninsn - 1)
  5337. emit_single_op (insn);
  5338. }
  5339. }
  5340. if (vinsn->num_slots > 1)
  5341. {
  5342. if (opcode_fits_format_slot
  5343. (slotstack.insn[slotstack.ninsn - 1].opcode,
  5344. vinsn->format, i))
  5345. {
  5346. vinsn->slots[i] = slotstack.insn[slotstack.ninsn - 1];
  5347. }
  5348. else
  5349. {
  5350. emit_single_op (&slotstack.insn[slotstack.ninsn - 1]);
  5351. if (vinsn->format == XTENSA_UNDEFINED)
  5352. vinsn->slots[i].opcode = xtensa_nop_opcode;
  5353. else
  5354. vinsn->slots[i].opcode
  5355. = xtensa_format_slot_nop_opcode (xtensa_default_isa,
  5356. vinsn->format, i);
  5357. vinsn->slots[i].ntok = 0;
  5358. }
  5359. }
  5360. else
  5361. {
  5362. vinsn->slots[0] = slotstack.insn[slotstack.ninsn - 1];
  5363. vinsn->format = XTENSA_UNDEFINED;
  5364. }
  5365. }
  5366. }
  5367. /* Now check resource conflicts on the modified bundle. */
  5368. if (resources_conflict (vinsn))
  5369. {
  5370. as_where (&file_name, &line);
  5371. as_bad_where (file_name, line, _("illegal resource usage in bundle"));
  5372. fprintf (stderr, " ops were: ");
  5373. for (i = 0; i < vinsn->num_slots; i++)
  5374. fprintf (stderr, " %s;",
  5375. xtensa_opcode_name (xtensa_default_isa,
  5376. vinsn->slots[i].opcode));
  5377. fprintf (stderr, "\n");
  5378. xg_clear_vinsn (vinsn);
  5379. return;
  5380. }
  5381. /* First, find a format that works. */
  5382. if (vinsn->format == XTENSA_UNDEFINED)
  5383. vinsn->format = xg_find_narrowest_format (vinsn);
  5384. xg_assemble_vliw_tokens (vinsn);
  5385. xg_clear_vinsn (vinsn);
  5386. xtensa_check_frag_count ();
  5387. }
  5388. /* Given an vliw instruction, what conflicts are there in register
  5389. usage and in writes to states and queues?
  5390. This function does two things:
  5391. 1. Reports an error when a vinsn contains illegal combinations
  5392. of writes to registers states or queues.
  5393. 2. Marks individual tinsns as not relaxable if the combination
  5394. contains antidependencies.
  5395. Job 2 handles things like swap semantics in instructions that need
  5396. to be relaxed. For example,
  5397. addi a0, a1, 100000
  5398. normally would be relaxed to
  5399. l32r a0, some_label
  5400. add a0, a1, a0
  5401. _but_, if the above instruction is bundled with an a0 reader, e.g.,
  5402. { addi a0, a1, 10000 ; add a2, a0, a4 ; }
  5403. then we can't relax it into
  5404. l32r a0, some_label
  5405. { add a0, a1, a0 ; add a2, a0, a4 ; }
  5406. because the value of a0 is trashed before the second add can read it. */
  5407. static char check_t1_t2_reads_and_writes (TInsn *, TInsn *);
  5408. static bfd_boolean
  5409. find_vinsn_conflicts (vliw_insn *vinsn)
  5410. {
  5411. int i, j;
  5412. int branches = 0;
  5413. xtensa_isa isa = xtensa_default_isa;
  5414. gas_assert (!past_xtensa_end);
  5415. for (i = 0 ; i < vinsn->num_slots; i++)
  5416. {
  5417. TInsn *op1 = &vinsn->slots[i];
  5418. if (op1->is_specific_opcode)
  5419. op1->keep_wide = TRUE;
  5420. else
  5421. op1->keep_wide = FALSE;
  5422. }
  5423. for (i = 0 ; i < vinsn->num_slots; i++)
  5424. {
  5425. TInsn *op1 = &vinsn->slots[i];
  5426. if (xtensa_opcode_is_branch (isa, op1->opcode) == 1)
  5427. branches++;
  5428. for (j = 0; j < vinsn->num_slots; j++)
  5429. {
  5430. if (i != j)
  5431. {
  5432. TInsn *op2 = &vinsn->slots[j];
  5433. char conflict_type = check_t1_t2_reads_and_writes (op1, op2);
  5434. switch (conflict_type)
  5435. {
  5436. case 'c':
  5437. as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same register"),
  5438. xtensa_opcode_name (isa, op1->opcode), i,
  5439. xtensa_opcode_name (isa, op2->opcode), j);
  5440. return TRUE;
  5441. case 'd':
  5442. as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same state"),
  5443. xtensa_opcode_name (isa, op1->opcode), i,
  5444. xtensa_opcode_name (isa, op2->opcode), j);
  5445. return TRUE;
  5446. case 'e':
  5447. as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) write the same port"),
  5448. xtensa_opcode_name (isa, op1->opcode), i,
  5449. xtensa_opcode_name (isa, op2->opcode), j);
  5450. return TRUE;
  5451. case 'f':
  5452. as_bad (_("opcodes '%s' (slot %d) and '%s' (slot %d) both have volatile port accesses"),
  5453. xtensa_opcode_name (isa, op1->opcode), i,
  5454. xtensa_opcode_name (isa, op2->opcode), j);
  5455. return TRUE;
  5456. default:
  5457. /* Everything is OK. */
  5458. break;
  5459. }
  5460. op2->is_specific_opcode = (op2->is_specific_opcode
  5461. || conflict_type == 'a');
  5462. }
  5463. }
  5464. }
  5465. if (branches > 1)
  5466. {
  5467. as_bad (_("multiple branches or jumps in the same bundle"));
  5468. return TRUE;
  5469. }
  5470. return FALSE;
  5471. }
  5472. /* Check how the state used by t1 and t2 relate.
  5473. Cases found are:
  5474. case A: t1 reads a register t2 writes (an antidependency within a bundle)
  5475. case B: no relationship between what is read and written (both could
  5476. read the same reg though)
  5477. case C: t1 writes a register t2 writes (a register conflict within a
  5478. bundle)
  5479. case D: t1 writes a state that t2 also writes
  5480. case E: t1 writes a tie queue that t2 also writes
  5481. case F: two volatile queue accesses
  5482. */
  5483. static char
  5484. check_t1_t2_reads_and_writes (TInsn *t1, TInsn *t2)
  5485. {
  5486. xtensa_isa isa = xtensa_default_isa;
  5487. xtensa_regfile t1_regfile, t2_regfile;
  5488. int t1_reg, t2_reg;
  5489. int t1_base_reg, t1_last_reg;
  5490. int t2_base_reg, t2_last_reg;
  5491. char t1_inout, t2_inout;
  5492. int i, j;
  5493. char conflict = 'b';
  5494. int t1_states;
  5495. int t2_states;
  5496. int t1_interfaces;
  5497. int t2_interfaces;
  5498. bfd_boolean t1_volatile = FALSE;
  5499. bfd_boolean t2_volatile = FALSE;
  5500. /* Check registers. */
  5501. for (j = 0; j < t2->ntok; j++)
  5502. {
  5503. if (xtensa_operand_is_register (isa, t2->opcode, j) != 1)
  5504. continue;
  5505. t2_regfile = xtensa_operand_regfile (isa, t2->opcode, j);
  5506. t2_base_reg = t2->tok[j].X_add_number;
  5507. t2_last_reg = t2_base_reg + xtensa_operand_num_regs (isa, t2->opcode, j);
  5508. for (i = 0; i < t1->ntok; i++)
  5509. {
  5510. if (xtensa_operand_is_register (isa, t1->opcode, i) != 1)
  5511. continue;
  5512. t1_regfile = xtensa_operand_regfile (isa, t1->opcode, i);
  5513. if (t1_regfile != t2_regfile)
  5514. continue;
  5515. t1_inout = xtensa_operand_inout (isa, t1->opcode, i);
  5516. t2_inout = xtensa_operand_inout (isa, t2->opcode, j);
  5517. if (xtensa_operand_is_known_reg (isa, t1->opcode, i) == 0
  5518. || xtensa_operand_is_known_reg (isa, t2->opcode, j) == 0)
  5519. {
  5520. if (t1_inout == 'm' || t1_inout == 'o'
  5521. || t2_inout == 'm' || t2_inout == 'o')
  5522. {
  5523. conflict = 'a';
  5524. continue;
  5525. }
  5526. }
  5527. t1_base_reg = t1->tok[i].X_add_number;
  5528. t1_last_reg = (t1_base_reg
  5529. + xtensa_operand_num_regs (isa, t1->opcode, i));
  5530. for (t1_reg = t1_base_reg; t1_reg < t1_last_reg; t1_reg++)
  5531. {
  5532. for (t2_reg = t2_base_reg; t2_reg < t2_last_reg; t2_reg++)
  5533. {
  5534. if (t1_reg != t2_reg)
  5535. continue;
  5536. if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o'))
  5537. {
  5538. conflict = 'a';
  5539. continue;
  5540. }
  5541. if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o'))
  5542. {
  5543. conflict = 'a';
  5544. continue;
  5545. }
  5546. if (t1_inout != 'i' && t2_inout != 'i')
  5547. return 'c';
  5548. }
  5549. }
  5550. }
  5551. }
  5552. /* Check states. */
  5553. t1_states = xtensa_opcode_num_stateOperands (isa, t1->opcode);
  5554. t2_states = xtensa_opcode_num_stateOperands (isa, t2->opcode);
  5555. for (j = 0; j < t2_states; j++)
  5556. {
  5557. xtensa_state t2_so = xtensa_stateOperand_state (isa, t2->opcode, j);
  5558. t2_inout = xtensa_stateOperand_inout (isa, t2->opcode, j);
  5559. for (i = 0; i < t1_states; i++)
  5560. {
  5561. xtensa_state t1_so = xtensa_stateOperand_state (isa, t1->opcode, i);
  5562. t1_inout = xtensa_stateOperand_inout (isa, t1->opcode, i);
  5563. if (t1_so != t2_so || xtensa_state_is_shared_or (isa, t1_so) == 1)
  5564. continue;
  5565. if (t2_inout == 'i' && (t1_inout == 'm' || t1_inout == 'o'))
  5566. {
  5567. conflict = 'a';
  5568. continue;
  5569. }
  5570. if (t1_inout == 'i' && (t2_inout == 'm' || t2_inout == 'o'))
  5571. {
  5572. conflict = 'a';
  5573. continue;
  5574. }
  5575. if (t1_inout != 'i' && t2_inout != 'i')
  5576. return 'd';
  5577. }
  5578. }
  5579. /* Check tieports. */
  5580. t1_interfaces = xtensa_opcode_num_interfaceOperands (isa, t1->opcode);
  5581. t2_interfaces = xtensa_opcode_num_interfaceOperands (isa, t2->opcode);
  5582. for (j = 0; j < t2_interfaces; j++)
  5583. {
  5584. xtensa_interface t2_int
  5585. = xtensa_interfaceOperand_interface (isa, t2->opcode, j);
  5586. int t2_class = xtensa_interface_class_id (isa, t2_int);
  5587. t2_inout = xtensa_interface_inout (isa, t2_int);
  5588. if (xtensa_interface_has_side_effect (isa, t2_int) == 1)
  5589. t2_volatile = TRUE;
  5590. for (i = 0; i < t1_interfaces; i++)
  5591. {
  5592. xtensa_interface t1_int
  5593. = xtensa_interfaceOperand_interface (isa, t1->opcode, j);
  5594. int t1_class = xtensa_interface_class_id (isa, t1_int);
  5595. t1_inout = xtensa_interface_inout (isa, t1_int);
  5596. if (xtensa_interface_has_side_effect (isa, t1_int) == 1)
  5597. t1_volatile = TRUE;
  5598. if (t1_volatile && t2_volatile && (t1_class == t2_class))
  5599. return 'f';
  5600. if (t1_int != t2_int)
  5601. continue;
  5602. if (t2_inout == 'i' && t1_inout == 'o')
  5603. {
  5604. conflict = 'a';
  5605. continue;
  5606. }
  5607. if (t1_inout == 'i' && t2_inout == 'o')
  5608. {
  5609. conflict = 'a';
  5610. continue;
  5611. }
  5612. if (t1_inout != 'i' && t2_inout != 'i')
  5613. return 'e';
  5614. }
  5615. }
  5616. return conflict;
  5617. }
  5618. static xtensa_format
  5619. xg_find_narrowest_format (vliw_insn *vinsn)
  5620. {
  5621. /* Right now we assume that the ops within the vinsn are properly
  5622. ordered for the slots that the programmer wanted them in. In
  5623. other words, we don't rearrange the ops in hopes of finding a
  5624. better format. The scheduler handles that. */
  5625. xtensa_isa isa = xtensa_default_isa;
  5626. xtensa_format format;
  5627. xtensa_opcode nop_opcode = xtensa_nop_opcode;
  5628. if (vinsn->num_slots == 1)
  5629. return xg_get_single_format (vinsn->slots[0].opcode);
  5630. for (format = 0; format < xtensa_isa_num_formats (isa); format++)
  5631. {
  5632. vliw_insn v_copy;
  5633. xg_copy_vinsn (&v_copy, vinsn);
  5634. if (xtensa_format_num_slots (isa, format) == v_copy.num_slots)
  5635. {
  5636. int slot;
  5637. int fit = 0;
  5638. for (slot = 0; slot < v_copy.num_slots; slot++)
  5639. {
  5640. if (v_copy.slots[slot].opcode == nop_opcode)
  5641. {
  5642. v_copy.slots[slot].opcode =
  5643. xtensa_format_slot_nop_opcode (isa, format, slot);
  5644. v_copy.slots[slot].ntok = 0;
  5645. }
  5646. if (opcode_fits_format_slot (v_copy.slots[slot].opcode,
  5647. format, slot))
  5648. fit++;
  5649. else if (v_copy.num_slots > 1)
  5650. {
  5651. TInsn widened;
  5652. /* Try the widened version. */
  5653. if (!v_copy.slots[slot].keep_wide
  5654. && !v_copy.slots[slot].is_specific_opcode
  5655. && xg_is_single_relaxable_insn (&v_copy.slots[slot],
  5656. &widened, TRUE)
  5657. && opcode_fits_format_slot (widened.opcode,
  5658. format, slot))
  5659. {
  5660. v_copy.slots[slot] = widened;
  5661. fit++;
  5662. }
  5663. }
  5664. }
  5665. if (fit == v_copy.num_slots)
  5666. {
  5667. xg_copy_vinsn (vinsn, &v_copy);
  5668. xtensa_format_encode (isa, format, vinsn->insnbuf);
  5669. vinsn->format = format;
  5670. break;
  5671. }
  5672. }
  5673. }
  5674. if (format == xtensa_isa_num_formats (isa))
  5675. return XTENSA_UNDEFINED;
  5676. return format;
  5677. }
  5678. /* Return the additional space needed in a frag
  5679. for possible relaxations of any ops in a VLIW insn.
  5680. Also fill out the relaxations that might be required of
  5681. each tinsn in the vinsn. */
  5682. static int
  5683. relaxation_requirements (vliw_insn *vinsn, bfd_boolean *pfinish_frag)
  5684. {
  5685. bfd_boolean finish_frag = FALSE;
  5686. int extra_space = 0;
  5687. int slot;
  5688. for (slot = 0; slot < vinsn->num_slots; slot++)
  5689. {
  5690. TInsn *tinsn = &vinsn->slots[slot];
  5691. if (!tinsn_has_symbolic_operands (tinsn))
  5692. {
  5693. /* A narrow instruction could be widened later to help
  5694. alignment issues. */
  5695. if (xg_is_single_relaxable_insn (tinsn, 0, TRUE)
  5696. && !tinsn->is_specific_opcode
  5697. && vinsn->num_slots == 1)
  5698. {
  5699. /* Difference in bytes between narrow and wide insns... */
  5700. extra_space += 1;
  5701. tinsn->subtype = RELAX_NARROW;
  5702. }
  5703. }
  5704. else
  5705. {
  5706. if (workaround_b_j_loop_end
  5707. && tinsn->opcode == xtensa_jx_opcode
  5708. && use_transform ())
  5709. {
  5710. /* Add 2 of these. */
  5711. extra_space += 3; /* for the nop size */
  5712. tinsn->subtype = RELAX_ADD_NOP_IF_PRE_LOOP_END;
  5713. }
  5714. /* Need to assemble it with space for the relocation. */
  5715. if (xg_is_relaxable_insn (tinsn, 0)
  5716. && !tinsn->is_specific_opcode)
  5717. {
  5718. int max_size = xg_get_max_insn_widen_size (tinsn->opcode);
  5719. int max_literal_size =
  5720. xg_get_max_insn_widen_literal_size (tinsn->opcode);
  5721. tinsn->literal_space = max_literal_size;
  5722. tinsn->subtype = RELAX_IMMED;
  5723. extra_space += max_size;
  5724. }
  5725. else
  5726. {
  5727. /* A fix record will be added for this instruction prior
  5728. to relaxation, so make it end the frag. */
  5729. finish_frag = TRUE;
  5730. }
  5731. }
  5732. }
  5733. *pfinish_frag = finish_frag;
  5734. return extra_space;
  5735. }
  5736. static void
  5737. bundle_tinsn (TInsn *tinsn, vliw_insn *vinsn)
  5738. {
  5739. xtensa_isa isa = xtensa_default_isa;
  5740. int slot, chosen_slot;
  5741. vinsn->format = xg_get_single_format (tinsn->opcode);
  5742. gas_assert (vinsn->format != XTENSA_UNDEFINED);
  5743. vinsn->num_slots = xtensa_format_num_slots (isa, vinsn->format);
  5744. chosen_slot = xg_get_single_slot (tinsn->opcode);
  5745. for (slot = 0; slot < vinsn->num_slots; slot++)
  5746. {
  5747. if (slot == chosen_slot)
  5748. vinsn->slots[slot] = *tinsn;
  5749. else
  5750. {
  5751. vinsn->slots[slot].opcode =
  5752. xtensa_format_slot_nop_opcode (isa, vinsn->format, slot);
  5753. vinsn->slots[slot].ntok = 0;
  5754. vinsn->slots[slot].insn_type = ITYPE_INSN;
  5755. }
  5756. }
  5757. }
  5758. static bfd_boolean
  5759. emit_single_op (TInsn *orig_insn)
  5760. {
  5761. int i;
  5762. IStack istack; /* put instructions into here */
  5763. symbolS *lit_sym = NULL;
  5764. symbolS *label_sym = NULL;
  5765. istack_init (&istack);
  5766. /* Special-case for "movi aX, foo" which is guaranteed to need relaxing.
  5767. Because the scheduling and bundling characteristics of movi and
  5768. l32r or const16 are so different, we can do much better if we relax
  5769. it prior to scheduling and bundling, rather than after. */
  5770. if ((orig_insn->opcode == xtensa_movi_opcode
  5771. || orig_insn->opcode == xtensa_movi_n_opcode)
  5772. && !cur_vinsn.inside_bundle
  5773. && (orig_insn->tok[1].X_op == O_symbol
  5774. || orig_insn->tok[1].X_op == O_pltrel
  5775. || orig_insn->tok[1].X_op == O_tlsfunc
  5776. || orig_insn->tok[1].X_op == O_tlsarg
  5777. || orig_insn->tok[1].X_op == O_tpoff
  5778. || orig_insn->tok[1].X_op == O_dtpoff)
  5779. && !orig_insn->is_specific_opcode && use_transform ())
  5780. xg_assembly_relax (&istack, orig_insn, now_seg, frag_now, 0, 1, 0);
  5781. else
  5782. if (xg_expand_assembly_insn (&istack, orig_insn))
  5783. return TRUE;
  5784. for (i = 0; i < istack.ninsn; i++)
  5785. {
  5786. TInsn *insn = &istack.insn[i];
  5787. switch (insn->insn_type)
  5788. {
  5789. case ITYPE_LITERAL:
  5790. gas_assert (lit_sym == NULL);
  5791. lit_sym = xg_assemble_literal (insn);
  5792. break;
  5793. case ITYPE_LABEL:
  5794. {
  5795. static int relaxed_sym_idx = 0;
  5796. char *label = xmalloc (strlen (FAKE_LABEL_NAME) + 12);
  5797. sprintf (label, "%s_rl_%x", FAKE_LABEL_NAME, relaxed_sym_idx++);
  5798. colon (label);
  5799. gas_assert (label_sym == NULL);
  5800. label_sym = symbol_find_or_make (label);
  5801. gas_assert (label_sym);
  5802. free (label);
  5803. }
  5804. break;
  5805. case ITYPE_INSN:
  5806. {
  5807. vliw_insn v;
  5808. if (lit_sym)
  5809. xg_resolve_literals (insn, lit_sym);
  5810. if (label_sym)
  5811. xg_resolve_labels (insn, label_sym);
  5812. xg_init_vinsn (&v);
  5813. bundle_tinsn (insn, &v);
  5814. finish_vinsn (&v);
  5815. xg_free_vinsn (&v);
  5816. }
  5817. break;
  5818. default:
  5819. gas_assert (0);
  5820. break;
  5821. }
  5822. }
  5823. return FALSE;
  5824. }
  5825. static int
  5826. total_frag_text_expansion (fragS *fragP)
  5827. {
  5828. int slot;
  5829. int total_expansion = 0;
  5830. for (slot = 0; slot < config_max_slots; slot++)
  5831. total_expansion += fragP->tc_frag_data.text_expansion[slot];
  5832. return total_expansion;
  5833. }
  5834. /* Emit a vliw instruction to the current fragment. */
  5835. static void
  5836. xg_assemble_vliw_tokens (vliw_insn *vinsn)
  5837. {
  5838. bfd_boolean finish_frag;
  5839. bfd_boolean is_jump = FALSE;
  5840. bfd_boolean is_branch = FALSE;
  5841. xtensa_isa isa = xtensa_default_isa;
  5842. int insn_size;
  5843. int extra_space;
  5844. char *f = NULL;
  5845. int slot;
  5846. struct dwarf2_line_info debug_line;
  5847. bfd_boolean loc_directive_seen = FALSE;
  5848. TInsn *tinsn;
  5849. memset (&debug_line, 0, sizeof (struct dwarf2_line_info));
  5850. if (generating_literals)
  5851. {
  5852. static int reported = 0;
  5853. if (reported < 4)
  5854. as_bad_where (frag_now->fr_file, frag_now->fr_line,
  5855. _("cannot assemble into a literal fragment"));
  5856. if (reported == 3)
  5857. as_bad (_("..."));
  5858. reported++;
  5859. return;
  5860. }
  5861. if (frag_now_fix () != 0
  5862. && (! frag_now->tc_frag_data.is_insn
  5863. || (vinsn_has_specific_opcodes (vinsn) && use_transform ())
  5864. || (!use_transform ()) != frag_now->tc_frag_data.is_no_transform
  5865. || (directive_state[directive_longcalls]
  5866. != frag_now->tc_frag_data.use_longcalls)
  5867. || (directive_state[directive_absolute_literals]
  5868. != frag_now->tc_frag_data.use_absolute_literals)))
  5869. {
  5870. frag_wane (frag_now);
  5871. frag_new (0);
  5872. xtensa_set_frag_assembly_state (frag_now);
  5873. }
  5874. if (workaround_a0_b_retw
  5875. && vinsn->num_slots == 1
  5876. && (get_last_insn_flags (now_seg, now_subseg) & FLAG_IS_A0_WRITER) != 0
  5877. && xtensa_opcode_is_branch (isa, vinsn->slots[0].opcode) == 1
  5878. && use_transform ())
  5879. {
  5880. has_a0_b_retw = TRUE;
  5881. /* Mark this fragment with the special RELAX_ADD_NOP_IF_A0_B_RETW.
  5882. After the first assembly pass we will check all of them and
  5883. add a nop if needed. */
  5884. frag_now->tc_frag_data.is_insn = TRUE;
  5885. frag_var (rs_machine_dependent, 4, 4,
  5886. RELAX_ADD_NOP_IF_A0_B_RETW,
  5887. frag_now->fr_symbol,
  5888. frag_now->fr_offset,
  5889. NULL);
  5890. xtensa_set_frag_assembly_state (frag_now);
  5891. frag_now->tc_frag_data.is_insn = TRUE;
  5892. frag_var (rs_machine_dependent, 4, 4,
  5893. RELAX_ADD_NOP_IF_A0_B_RETW,
  5894. frag_now->fr_symbol,
  5895. frag_now->fr_offset,
  5896. NULL);
  5897. xtensa_set_frag_assembly_state (frag_now);
  5898. }
  5899. for (slot = 0; slot < vinsn->num_slots; slot++)
  5900. {
  5901. tinsn = &vinsn->slots[slot];
  5902. /* See if the instruction implies an aligned section. */
  5903. if (xtensa_opcode_is_loop (isa, tinsn->opcode) == 1)
  5904. record_alignment (now_seg, 2);
  5905. /* Determine the best line number for debug info. */
  5906. if ((tinsn->loc_directive_seen || !loc_directive_seen)
  5907. && (tinsn->debug_line.filenum != debug_line.filenum
  5908. || tinsn->debug_line.line < debug_line.line
  5909. || tinsn->debug_line.column < debug_line.column))
  5910. debug_line = tinsn->debug_line;
  5911. if (tinsn->loc_directive_seen)
  5912. loc_directive_seen = TRUE;
  5913. }
  5914. /* Special cases for instructions that force an alignment... */
  5915. /* None of these opcodes are bundle-able. */
  5916. if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1)
  5917. {
  5918. int max_fill;
  5919. /* Remember the symbol that marks the end of the loop in the frag
  5920. that marks the start of the loop. This way we can easily find
  5921. the end of the loop at the beginning, without adding special code
  5922. to mark the loop instructions themselves. */
  5923. symbolS *target_sym = NULL;
  5924. if (vinsn->slots[0].tok[1].X_op == O_symbol)
  5925. target_sym = vinsn->slots[0].tok[1].X_add_symbol;
  5926. xtensa_set_frag_assembly_state (frag_now);
  5927. frag_now->tc_frag_data.is_insn = TRUE;
  5928. max_fill = get_text_align_max_fill_size
  5929. (get_text_align_power (xtensa_fetch_width),
  5930. TRUE, frag_now->tc_frag_data.is_no_density);
  5931. if (use_transform ())
  5932. frag_var (rs_machine_dependent, max_fill, max_fill,
  5933. RELAX_ALIGN_NEXT_OPCODE, target_sym, 0, NULL);
  5934. else
  5935. frag_var (rs_machine_dependent, 0, 0,
  5936. RELAX_CHECK_ALIGN_NEXT_OPCODE, target_sym, 0, NULL);
  5937. xtensa_set_frag_assembly_state (frag_now);
  5938. }
  5939. if (vinsn->slots[0].opcode == xtensa_entry_opcode
  5940. && !vinsn->slots[0].is_specific_opcode)
  5941. {
  5942. xtensa_mark_literal_pool_location ();
  5943. xtensa_move_labels (frag_now, 0);
  5944. frag_var (rs_align_test, 1, 1, 0, NULL, 2, NULL);
  5945. }
  5946. if (vinsn->num_slots == 1)
  5947. {
  5948. if (workaround_a0_b_retw && use_transform ())
  5949. set_last_insn_flags (now_seg, now_subseg, FLAG_IS_A0_WRITER,
  5950. is_register_writer (&vinsn->slots[0], "a", 0));
  5951. set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND,
  5952. is_bad_loopend_opcode (&vinsn->slots[0]));
  5953. }
  5954. else
  5955. set_last_insn_flags (now_seg, now_subseg, FLAG_IS_BAD_LOOPEND, FALSE);
  5956. insn_size = xtensa_format_length (isa, vinsn->format);
  5957. extra_space = relaxation_requirements (vinsn, &finish_frag);
  5958. /* vinsn_to_insnbuf will produce the error. */
  5959. if (vinsn->format != XTENSA_UNDEFINED)
  5960. {
  5961. f = frag_more (insn_size + extra_space);
  5962. xtensa_set_frag_assembly_state (frag_now);
  5963. frag_now->tc_frag_data.is_insn = TRUE;
  5964. }
  5965. vinsn_to_insnbuf (vinsn, f, frag_now, FALSE);
  5966. if (vinsn->format == XTENSA_UNDEFINED)
  5967. return;
  5968. xtensa_insnbuf_to_chars (isa, vinsn->insnbuf, (unsigned char *) f, 0);
  5969. if (debug_type == DEBUG_DWARF2 || loc_directive_seen)
  5970. dwarf2_gen_line_info (frag_now_fix () - (insn_size + extra_space),
  5971. &debug_line);
  5972. for (slot = 0; slot < vinsn->num_slots; slot++)
  5973. {
  5974. tinsn = &vinsn->slots[slot];
  5975. frag_now->tc_frag_data.slot_subtypes[slot] = tinsn->subtype;
  5976. frag_now->tc_frag_data.slot_symbols[slot] = tinsn->symbol;
  5977. frag_now->tc_frag_data.slot_offsets[slot] = tinsn->offset;
  5978. frag_now->tc_frag_data.literal_frags[slot] = tinsn->literal_frag;
  5979. if (tinsn->opcode == xtensa_l32r_opcode)
  5980. {
  5981. frag_now->tc_frag_data.literal_frags[slot] =
  5982. tinsn->tok[1].X_add_symbol->sy_frag;
  5983. }
  5984. if (tinsn->literal_space != 0)
  5985. xg_assemble_literal_space (tinsn->literal_space, slot);
  5986. frag_now->tc_frag_data.free_reg[slot] = tinsn->extra_arg;
  5987. if (tinsn->subtype == RELAX_NARROW)
  5988. gas_assert (vinsn->num_slots == 1);
  5989. if (xtensa_opcode_is_jump (isa, tinsn->opcode) == 1)
  5990. is_jump = TRUE;
  5991. if (xtensa_opcode_is_branch (isa, tinsn->opcode) == 1)
  5992. is_branch = TRUE;
  5993. if (tinsn->subtype || tinsn->symbol || tinsn->offset
  5994. || tinsn->literal_frag || is_jump || is_branch)
  5995. finish_frag = TRUE;
  5996. }
  5997. if (vinsn_has_specific_opcodes (vinsn) && use_transform ())
  5998. frag_now->tc_frag_data.is_specific_opcode = TRUE;
  5999. if (finish_frag)
  6000. {
  6001. frag_variant (rs_machine_dependent,
  6002. extra_space, extra_space, RELAX_SLOTS,
  6003. frag_now->fr_symbol, frag_now->fr_offset, f);
  6004. xtensa_set_frag_assembly_state (frag_now);
  6005. }
  6006. /* Special cases for loops:
  6007. close_loop_end should be inserted AFTER short_loop.
  6008. Make sure that CLOSE loops are processed BEFORE short_loops
  6009. when converting them. */
  6010. /* "short_loop": Add a NOP if the loop is < 4 bytes. */
  6011. if (xtensa_opcode_is_loop (isa, vinsn->slots[0].opcode) == 1
  6012. && !vinsn->slots[0].is_specific_opcode)
  6013. {
  6014. if (workaround_short_loop && use_transform ())
  6015. {
  6016. maybe_has_short_loop = TRUE;
  6017. frag_now->tc_frag_data.is_insn = TRUE;
  6018. frag_var (rs_machine_dependent, 4, 4,
  6019. RELAX_ADD_NOP_IF_SHORT_LOOP,
  6020. frag_now->fr_symbol, frag_now->fr_offset, NULL);
  6021. frag_now->tc_frag_data.is_insn = TRUE;
  6022. frag_var (rs_machine_dependent, 4, 4,
  6023. RELAX_ADD_NOP_IF_SHORT_LOOP,
  6024. frag_now->fr_symbol, frag_now->fr_offset, NULL);
  6025. }
  6026. /* "close_loop_end": Add up to 12 bytes of NOPs to keep a
  6027. loop at least 12 bytes away from another loop's end. */
  6028. if (workaround_close_loop_end && use_transform ())
  6029. {
  6030. maybe_has_close_loop_end = TRUE;
  6031. frag_now->tc_frag_data.is_insn = TRUE;
  6032. frag_var (rs_machine_dependent, 12, 12,
  6033. RELAX_ADD_NOP_IF_CLOSE_LOOP_END,
  6034. frag_now->fr_symbol, frag_now->fr_offset, NULL);
  6035. }
  6036. }
  6037. if (use_transform ())
  6038. {
  6039. if (is_jump)
  6040. {
  6041. gas_assert (finish_frag);
  6042. frag_var (rs_machine_dependent,
  6043. xtensa_fetch_width, xtensa_fetch_width,
  6044. RELAX_UNREACHABLE,
  6045. frag_now->fr_symbol, frag_now->fr_offset, NULL);
  6046. xtensa_set_frag_assembly_state (frag_now);
  6047. xtensa_maybe_create_trampoline_frag ();
  6048. /* Always create one here. */
  6049. xtensa_maybe_create_literal_pool_frag (TRUE, FALSE);
  6050. }
  6051. else if (is_branch && do_align_targets ())
  6052. {
  6053. gas_assert (finish_frag);
  6054. frag_var (rs_machine_dependent,
  6055. xtensa_fetch_width, xtensa_fetch_width,
  6056. RELAX_MAYBE_UNREACHABLE,
  6057. frag_now->fr_symbol, frag_now->fr_offset, NULL);
  6058. xtensa_set_frag_assembly_state (frag_now);
  6059. frag_var (rs_machine_dependent,
  6060. 0, 0,
  6061. RELAX_MAYBE_DESIRE_ALIGN,
  6062. frag_now->fr_symbol, frag_now->fr_offset, NULL);
  6063. xtensa_set_frag_assembly_state (frag_now);
  6064. }
  6065. }
  6066. /* Now, if the original opcode was a call... */
  6067. if (do_align_targets ()
  6068. && xtensa_opcode_is_call (isa, vinsn->slots[0].opcode) == 1)
  6069. {
  6070. float freq = get_subseg_total_freq (now_seg, now_subseg);
  6071. frag_now->tc_frag_data.is_insn = TRUE;
  6072. frag_var (rs_machine_dependent, 4, (int) freq, RELAX_DESIRE_ALIGN,
  6073. frag_now->fr_symbol, frag_now->fr_offset, NULL);
  6074. xtensa_set_frag_assembly_state (frag_now);
  6075. }
  6076. if (vinsn_has_specific_opcodes (vinsn) && use_transform ())
  6077. {
  6078. frag_wane (frag_now);
  6079. frag_new (0);
  6080. xtensa_set_frag_assembly_state (frag_now);
  6081. }
  6082. }
  6083. /* xtensa_end and helper functions. */
  6084. static void xtensa_cleanup_align_frags (void);
  6085. static void xtensa_fix_target_frags (void);
  6086. static void xtensa_mark_narrow_branches (void);
  6087. static void xtensa_mark_zcl_first_insns (void);
  6088. static void xtensa_mark_difference_of_two_symbols (void);
  6089. static void xtensa_fix_a0_b_retw_frags (void);
  6090. static void xtensa_fix_b_j_loop_end_frags (void);
  6091. static void xtensa_fix_close_loop_end_frags (void);
  6092. static void xtensa_fix_short_loop_frags (void);
  6093. static void xtensa_sanity_check (void);
  6094. static void xtensa_add_config_info (void);
  6095. void
  6096. xtensa_end (void)
  6097. {
  6098. directive_balance ();
  6099. xtensa_flush_pending_output ();
  6100. past_xtensa_end = TRUE;
  6101. xtensa_move_literals ();
  6102. xtensa_reorder_segments ();
  6103. xtensa_cleanup_align_frags ();
  6104. xtensa_fix_target_frags ();
  6105. if (workaround_a0_b_retw && has_a0_b_retw)
  6106. xtensa_fix_a0_b_retw_frags ();
  6107. if (workaround_b_j_loop_end)
  6108. xtensa_fix_b_j_loop_end_frags ();
  6109. /* "close_loop_end" should be processed BEFORE "short_loop". */
  6110. if (workaround_close_loop_end && maybe_has_close_loop_end)
  6111. xtensa_fix_close_loop_end_frags ();
  6112. if (workaround_short_loop && maybe_has_short_loop)
  6113. xtensa_fix_short_loop_frags ();
  6114. if (align_targets)
  6115. xtensa_mark_narrow_branches ();
  6116. xtensa_mark_zcl_first_insns ();
  6117. xtensa_sanity_check ();
  6118. xtensa_add_config_info ();
  6119. xtensa_check_frag_count ();
  6120. }
  6121. struct trampoline_frag
  6122. {
  6123. struct trampoline_frag *next;
  6124. bfd_boolean needs_jump_around;
  6125. fragS *fragP;
  6126. fixS *fixP;
  6127. };
  6128. struct trampoline_seg
  6129. {
  6130. struct trampoline_seg *next;
  6131. asection *seg;
  6132. struct trampoline_frag trampoline_list;
  6133. };
  6134. static struct trampoline_seg trampoline_seg_list;
  6135. #define J_RANGE (128 * 1024)
  6136. static int unreachable_count = 0;
  6137. static void
  6138. xtensa_maybe_create_trampoline_frag (void)
  6139. {
  6140. if (!use_trampolines)
  6141. return;
  6142. /* We create an area for possible trampolines every 10 unreachable frags.
  6143. These are preferred over the ones not preceded by an unreachable frag,
  6144. because we don't have to jump around them. This function is called after
  6145. each RELAX_UNREACHABLE frag is created. */
  6146. if (++unreachable_count > 10)
  6147. {
  6148. xtensa_create_trampoline_frag (FALSE);
  6149. clear_frag_count ();
  6150. unreachable_count = 0;
  6151. }
  6152. }
  6153. static void
  6154. xtensa_check_frag_count (void)
  6155. {
  6156. if (!use_trampolines || frag_now->tc_frag_data.is_no_transform)
  6157. return;
  6158. /* We create an area for possible trampolines every 8000 frags or so. This
  6159. is an estimate based on the max range of a "j" insn (+/-128K) divided
  6160. by a typical frag byte count (16), minus a few for safety. This function
  6161. is called after each source line is processed. */
  6162. if (get_frag_count () > 8000)
  6163. {
  6164. xtensa_create_trampoline_frag (TRUE);
  6165. clear_frag_count ();
  6166. unreachable_count = 0;
  6167. }
  6168. /* We create an area for a possible literal pool every N (default 5000)
  6169. frags or so. */
  6170. xtensa_maybe_create_literal_pool_frag (TRUE, TRUE);
  6171. }
  6172. static xtensa_insnbuf trampoline_buf = NULL;
  6173. static xtensa_insnbuf trampoline_slotbuf = NULL;
  6174. static xtensa_insnbuf litpool_buf = NULL;
  6175. static xtensa_insnbuf litpool_slotbuf = NULL;
  6176. #define TRAMPOLINE_FRAG_SIZE 3000
  6177. static void
  6178. xtensa_create_trampoline_frag (bfd_boolean needs_jump_around)
  6179. {
  6180. /* Emit a frag where we can place intermediate jump instructions,
  6181. in case we need to jump farther than 128K bytes.
  6182. Each jump instruction takes three bytes.
  6183. We allocate enough for 1000 trampolines in each frag.
  6184. If that's not enough, oh well. */
  6185. struct trampoline_seg *ts = trampoline_seg_list.next;
  6186. struct trampoline_frag *tf;
  6187. char *varP;
  6188. fragS *fragP;
  6189. int size = TRAMPOLINE_FRAG_SIZE;
  6190. for ( ; ts; ts = ts->next)
  6191. {
  6192. if (ts->seg == now_seg)
  6193. break;
  6194. }
  6195. if (ts == NULL)
  6196. {
  6197. ts = (struct trampoline_seg *)xcalloc(sizeof (struct trampoline_seg), 1);
  6198. ts->next = trampoline_seg_list.next;
  6199. trampoline_seg_list.next = ts;
  6200. ts->seg = now_seg;
  6201. }
  6202. frag_wane (frag_now);
  6203. frag_new (0);
  6204. xtensa_set_frag_assembly_state (frag_now);
  6205. varP = frag_var (rs_machine_dependent, size, size, RELAX_TRAMPOLINE, NULL, 0, NULL);
  6206. fragP = (fragS *)(varP - SIZEOF_STRUCT_FRAG);
  6207. if (trampoline_buf == NULL)
  6208. {
  6209. trampoline_buf = xtensa_insnbuf_alloc (xtensa_default_isa);
  6210. trampoline_slotbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
  6211. }
  6212. tf = (struct trampoline_frag *)xmalloc(sizeof (struct trampoline_frag));
  6213. tf->next = ts->trampoline_list.next;
  6214. ts->trampoline_list.next = tf;
  6215. tf->needs_jump_around = needs_jump_around;
  6216. tf->fragP = fragP;
  6217. tf->fixP = NULL;
  6218. }
  6219. static struct trampoline_seg *
  6220. find_trampoline_seg (asection *seg)
  6221. {
  6222. struct trampoline_seg *ts = trampoline_seg_list.next;
  6223. for ( ; ts; ts = ts->next)
  6224. {
  6225. if (ts->seg == seg)
  6226. return ts;
  6227. }
  6228. return NULL;
  6229. }
  6230. void dump_trampolines (void);
  6231. void
  6232. dump_trampolines (void)
  6233. {
  6234. struct trampoline_seg *ts = trampoline_seg_list.next;
  6235. for ( ; ts; ts = ts->next)
  6236. {
  6237. asection *seg = ts->seg;
  6238. if (seg == NULL)
  6239. continue;
  6240. fprintf(stderr, "SECTION %s\n", seg->name);
  6241. struct trampoline_frag *tf = ts->trampoline_list.next;
  6242. for ( ; tf; tf = tf->next)
  6243. {
  6244. if (tf->fragP == NULL)
  6245. continue;
  6246. fprintf(stderr, " 0x%08x: fix=%d, jump_around=%s\n",
  6247. (int)tf->fragP->fr_address, (int)tf->fragP->fr_fix,
  6248. tf->needs_jump_around ? "T" : "F");
  6249. }
  6250. }
  6251. }
  6252. static void dump_litpools (void) __attribute__ ((unused));
  6253. static void
  6254. dump_litpools (void)
  6255. {
  6256. struct litpool_seg *lps = litpool_seg_list.next;
  6257. struct litpool_frag *lpf;
  6258. for ( ; lps ; lps = lps->next )
  6259. {
  6260. printf("litpool seg %s\n", lps->seg->name);
  6261. for ( lpf = lps->frag_list.next; lpf->fragP; lpf = lpf->next )
  6262. {
  6263. fragS *litfrag = lpf->fragP->fr_next;
  6264. int count = 0;
  6265. while (litfrag && litfrag->fr_subtype != RELAX_LITERAL_POOL_END)
  6266. {
  6267. if (litfrag->fr_fix == 4)
  6268. count++;
  6269. litfrag = litfrag->fr_next;
  6270. }
  6271. printf(" %ld <%d:%d> (%d) [%d]: ",
  6272. lpf->addr, lpf->priority, lpf->original_priority,
  6273. lpf->fragP->fr_line, count);
  6274. //dump_frag(lpf->fragP);
  6275. }
  6276. }
  6277. }
  6278. static void
  6279. xtensa_maybe_create_literal_pool_frag (bfd_boolean create,
  6280. bfd_boolean only_if_needed)
  6281. {
  6282. struct litpool_seg *lps = litpool_seg_list.next;
  6283. fragS *fragP;
  6284. struct litpool_frag *lpf;
  6285. bfd_boolean needed = FALSE;
  6286. if (use_literal_section || !auto_litpools)
  6287. return;
  6288. for ( ; lps ; lps = lps->next )
  6289. {
  6290. if (lps->seg == now_seg)
  6291. break;
  6292. }
  6293. if (lps == NULL)
  6294. {
  6295. lps = (struct litpool_seg *)xcalloc (sizeof (struct litpool_seg), 1);
  6296. lps->next = litpool_seg_list.next;
  6297. litpool_seg_list.next = lps;
  6298. lps->seg = now_seg;
  6299. lps->frag_list.next = &lps->frag_list;
  6300. lps->frag_list.prev = &lps->frag_list;
  6301. }
  6302. lps->frag_count++;
  6303. if (create)
  6304. {
  6305. if (only_if_needed)
  6306. {
  6307. if (past_xtensa_end || !use_transform() ||
  6308. frag_now->tc_frag_data.is_no_transform)
  6309. {
  6310. return;
  6311. }
  6312. if (auto_litpool_limit <= 0)
  6313. {
  6314. /* Don't create a litpool based only on frag count. */
  6315. return;
  6316. }
  6317. else if (lps->frag_count > auto_litpool_limit)
  6318. {
  6319. needed = TRUE;
  6320. }
  6321. else
  6322. {
  6323. return;
  6324. }
  6325. }
  6326. else
  6327. {
  6328. needed = TRUE;
  6329. }
  6330. }
  6331. if (needed)
  6332. {
  6333. int size = (only_if_needed) ? 3 : 0; /* Space for a "j" insn. */
  6334. /* Create a potential site for a literal pool. */
  6335. frag_wane (frag_now);
  6336. frag_new (0);
  6337. xtensa_set_frag_assembly_state (frag_now);
  6338. fragP = frag_now;
  6339. fragP->tc_frag_data.lit_frchain = frchain_now;
  6340. fragP->tc_frag_data.literal_frag = fragP;
  6341. frag_var (rs_machine_dependent, size, size,
  6342. (only_if_needed) ?
  6343. RELAX_LITERAL_POOL_CANDIDATE_BEGIN :
  6344. RELAX_LITERAL_POOL_BEGIN,
  6345. NULL, 0, NULL);
  6346. frag_now->tc_frag_data.lit_seg = now_seg;
  6347. frag_variant (rs_machine_dependent, 0, 0,
  6348. RELAX_LITERAL_POOL_END, NULL, 0, NULL);
  6349. xtensa_set_frag_assembly_state (frag_now);
  6350. }
  6351. else
  6352. {
  6353. /* RELAX_LITERAL_POOL_BEGIN frag is being created;
  6354. just record it here. */
  6355. fragP = frag_now;
  6356. }
  6357. lpf = (struct litpool_frag *)xmalloc(sizeof (struct litpool_frag));
  6358. /* Insert at tail of circular list. */
  6359. lpf->addr = 0;
  6360. lps->frag_list.prev->next = lpf;
  6361. lpf->next = &lps->frag_list;
  6362. lpf->prev = lps->frag_list.prev;
  6363. lps->frag_list.prev = lpf;
  6364. lpf->fragP = fragP;
  6365. lpf->priority = (needed) ? (only_if_needed) ? 3 : 2 : 1;
  6366. lpf->original_priority = lpf->priority;
  6367. lps->frag_count = 0;
  6368. }
  6369. static void
  6370. xtensa_cleanup_align_frags (void)
  6371. {
  6372. frchainS *frchP;
  6373. asection *s;
  6374. for (s = stdoutput->sections; s; s = s->next)
  6375. for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
  6376. {
  6377. fragS *fragP;
  6378. /* Walk over all of the fragments in a subsection. */
  6379. for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
  6380. {
  6381. if ((fragP->fr_type == rs_align
  6382. || fragP->fr_type == rs_align_code
  6383. || (fragP->fr_type == rs_machine_dependent
  6384. && (fragP->fr_subtype == RELAX_DESIRE_ALIGN
  6385. || fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)))
  6386. && fragP->fr_fix == 0)
  6387. {
  6388. fragS *next = fragP->fr_next;
  6389. while (next
  6390. && next->fr_fix == 0
  6391. && next->fr_type == rs_machine_dependent
  6392. && next->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
  6393. {
  6394. frag_wane (next);
  6395. next = next->fr_next;
  6396. }
  6397. }
  6398. /* If we don't widen branch targets, then they
  6399. will be easier to align. */
  6400. if (fragP->tc_frag_data.is_branch_target
  6401. && fragP->fr_opcode == fragP->fr_literal
  6402. && fragP->fr_type == rs_machine_dependent
  6403. && fragP->fr_subtype == RELAX_SLOTS
  6404. && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
  6405. frag_wane (fragP);
  6406. if (fragP->fr_type == rs_machine_dependent
  6407. && fragP->fr_subtype == RELAX_UNREACHABLE)
  6408. fragP->tc_frag_data.is_unreachable = TRUE;
  6409. }
  6410. }
  6411. }
  6412. /* Re-process all of the fragments looking to convert all of the
  6413. RELAX_DESIRE_ALIGN_IF_TARGET fragments. If there is a branch
  6414. target in the next fragment, convert this to RELAX_DESIRE_ALIGN.
  6415. Otherwise, convert to a .fill 0. */
  6416. static void
  6417. xtensa_fix_target_frags (void)
  6418. {
  6419. frchainS *frchP;
  6420. asection *s;
  6421. /* When this routine is called, all of the subsections are still intact
  6422. so we walk over subsections instead of sections. */
  6423. for (s = stdoutput->sections; s; s = s->next)
  6424. for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
  6425. {
  6426. fragS *fragP;
  6427. /* Walk over all of the fragments in a subsection. */
  6428. for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
  6429. {
  6430. if (fragP->fr_type == rs_machine_dependent
  6431. && fragP->fr_subtype == RELAX_DESIRE_ALIGN_IF_TARGET)
  6432. {
  6433. if (next_frag_is_branch_target (fragP))
  6434. fragP->fr_subtype = RELAX_DESIRE_ALIGN;
  6435. else
  6436. frag_wane (fragP);
  6437. }
  6438. }
  6439. }
  6440. }
  6441. static bfd_boolean is_narrow_branch_guaranteed_in_range (fragS *, TInsn *);
  6442. static void
  6443. xtensa_mark_narrow_branches (void)
  6444. {
  6445. frchainS *frchP;
  6446. asection *s;
  6447. for (s = stdoutput->sections; s; s = s->next)
  6448. for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
  6449. {
  6450. fragS *fragP;
  6451. /* Walk over all of the fragments in a subsection. */
  6452. for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
  6453. {
  6454. if (fragP->fr_type == rs_machine_dependent
  6455. && fragP->fr_subtype == RELAX_SLOTS
  6456. && fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED)
  6457. {
  6458. vliw_insn vinsn;
  6459. vinsn_from_chars (&vinsn, fragP->fr_opcode);
  6460. tinsn_immed_from_frag (&vinsn.slots[0], fragP, 0);
  6461. if (vinsn.num_slots == 1
  6462. && xtensa_opcode_is_branch (xtensa_default_isa,
  6463. vinsn.slots[0].opcode) == 1
  6464. && xg_get_single_size (vinsn.slots[0].opcode) == 2
  6465. && is_narrow_branch_guaranteed_in_range (fragP,
  6466. &vinsn.slots[0]))
  6467. {
  6468. fragP->fr_subtype = RELAX_SLOTS;
  6469. fragP->tc_frag_data.slot_subtypes[0] = RELAX_NARROW;
  6470. fragP->tc_frag_data.is_aligning_branch = 1;
  6471. }
  6472. }
  6473. }
  6474. }
  6475. }
  6476. /* A branch is typically widened only when its target is out of
  6477. range. However, we would like to widen them to align a subsequent
  6478. branch target when possible.
  6479. Because the branch relaxation code is so convoluted, the optimal solution
  6480. (combining the two cases) is difficult to get right in all circumstances.
  6481. We therefore go with an "almost as good" solution, where we only
  6482. use for alignment narrow branches that definitely will not expand to a
  6483. jump and a branch. These functions find and mark these cases. */
  6484. /* The range in bytes of BNEZ.N and BEQZ.N. The target operand is encoded
  6485. as PC + 4 + imm6, where imm6 is a 6-bit immediate ranging from 0 to 63.
  6486. We start counting beginning with the frag after the 2-byte branch, so the
  6487. maximum offset is (4 - 2) + 63 = 65. */
  6488. #define MAX_IMMED6 65
  6489. static offsetT unrelaxed_frag_max_size (fragS *);
  6490. static bfd_boolean
  6491. is_narrow_branch_guaranteed_in_range (fragS *fragP, TInsn *tinsn)
  6492. {
  6493. const expressionS *exp = &tinsn->tok[1];
  6494. symbolS *symbolP = exp->X_add_symbol;
  6495. offsetT max_distance = exp->X_add_number;
  6496. fragS *target_frag;
  6497. if (exp->X_op != O_symbol)
  6498. return FALSE;
  6499. target_frag = symbol_get_frag (symbolP);
  6500. max_distance += (S_GET_VALUE (symbolP) - target_frag->fr_address);
  6501. if (is_branch_jmp_to_next (tinsn, fragP))
  6502. return FALSE;
  6503. /* The branch doesn't branch over it's own frag,
  6504. but over the subsequent ones. */
  6505. fragP = fragP->fr_next;
  6506. while (fragP != NULL && fragP != target_frag && max_distance <= MAX_IMMED6)
  6507. {
  6508. max_distance += unrelaxed_frag_max_size (fragP);
  6509. fragP = fragP->fr_next;
  6510. }
  6511. if (max_distance <= MAX_IMMED6 && fragP == target_frag)
  6512. return TRUE;
  6513. return FALSE;
  6514. }
  6515. static void
  6516. xtensa_mark_zcl_first_insns (void)
  6517. {
  6518. frchainS *frchP;
  6519. asection *s;
  6520. for (s = stdoutput->sections; s; s = s->next)
  6521. for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
  6522. {
  6523. fragS *fragP;
  6524. /* Walk over all of the fragments in a subsection. */
  6525. for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
  6526. {
  6527. if (fragP->fr_type == rs_machine_dependent
  6528. && (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE
  6529. || fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE))
  6530. {
  6531. /* Find the loop frag. */
  6532. fragS *loop_frag = next_non_empty_frag (fragP);
  6533. /* Find the first insn frag. */
  6534. fragS *targ_frag = next_non_empty_frag (loop_frag);
  6535. /* Handle a corner case that comes up in hardware
  6536. diagnostics. The original assembly looks like this:
  6537. loop aX, LabelA
  6538. <empty_frag>--not found by next_non_empty_frag
  6539. loop aY, LabelB
  6540. Depending on the start address, the assembler may or
  6541. may not change it to look something like this:
  6542. loop aX, LabelA
  6543. nop--frag isn't empty anymore
  6544. loop aY, LabelB
  6545. So set up to check the alignment of the nop if it
  6546. exists */
  6547. while (loop_frag != targ_frag)
  6548. {
  6549. if (loop_frag->fr_type == rs_machine_dependent
  6550. && (loop_frag->fr_subtype == RELAX_ALIGN_NEXT_OPCODE
  6551. || loop_frag->fr_subtype
  6552. == RELAX_CHECK_ALIGN_NEXT_OPCODE))
  6553. targ_frag = loop_frag;
  6554. else
  6555. loop_frag = loop_frag->fr_next;
  6556. }
  6557. /* Of course, sometimes (mostly for toy test cases) a
  6558. zero-cost loop instruction is the last in a section. */
  6559. if (targ_frag)
  6560. {
  6561. targ_frag->tc_frag_data.is_first_loop_insn = TRUE;
  6562. /* Do not widen a frag that is the first instruction of a
  6563. zero-cost loop. It makes that loop harder to align. */
  6564. if (targ_frag->fr_type == rs_machine_dependent
  6565. && targ_frag->fr_subtype == RELAX_SLOTS
  6566. && (targ_frag->tc_frag_data.slot_subtypes[0]
  6567. == RELAX_NARROW))
  6568. {
  6569. if (targ_frag->tc_frag_data.is_aligning_branch)
  6570. targ_frag->tc_frag_data.slot_subtypes[0] = RELAX_IMMED;
  6571. else
  6572. {
  6573. frag_wane (targ_frag);
  6574. targ_frag->tc_frag_data.slot_subtypes[0] = 0;
  6575. }
  6576. }
  6577. }
  6578. if (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)
  6579. frag_wane (fragP);
  6580. }
  6581. }
  6582. }
  6583. }
  6584. /* When a difference-of-symbols expression is encoded as a uleb128 or
  6585. sleb128 value, the linker is unable to adjust that value to account for
  6586. link-time relaxation. Mark all the code between such symbols so that
  6587. its size cannot be changed by linker relaxation. */
  6588. static void
  6589. xtensa_mark_difference_of_two_symbols (void)
  6590. {
  6591. symbolS *expr_sym;
  6592. for (expr_sym = expr_symbols; expr_sym;
  6593. expr_sym = symbol_get_tc (expr_sym)->next_expr_symbol)
  6594. {
  6595. expressionS *exp = symbol_get_value_expression (expr_sym);
  6596. if (exp->X_op == O_subtract)
  6597. {
  6598. symbolS *left = exp->X_add_symbol;
  6599. symbolS *right = exp->X_op_symbol;
  6600. /* Difference of two symbols not in the same section
  6601. are handled with relocations in the linker. */
  6602. if (S_GET_SEGMENT (left) == S_GET_SEGMENT (right))
  6603. {
  6604. fragS *start;
  6605. fragS *end;
  6606. fragS *walk;
  6607. if (symbol_get_frag (left)->fr_address
  6608. <= symbol_get_frag (right)->fr_address)
  6609. {
  6610. start = symbol_get_frag (left);
  6611. end = symbol_get_frag (right);
  6612. }
  6613. else
  6614. {
  6615. start = symbol_get_frag (right);
  6616. end = symbol_get_frag (left);
  6617. }
  6618. if (start->tc_frag_data.no_transform_end != NULL)
  6619. walk = start->tc_frag_data.no_transform_end;
  6620. else
  6621. walk = start;
  6622. do
  6623. {
  6624. walk->tc_frag_data.is_no_transform = 1;
  6625. walk = walk->fr_next;
  6626. }
  6627. while (walk && walk->fr_address < end->fr_address);
  6628. start->tc_frag_data.no_transform_end = walk;
  6629. }
  6630. }
  6631. }
  6632. }
  6633. /* Re-process all of the fragments looking to convert all of the
  6634. RELAX_ADD_NOP_IF_A0_B_RETW. If the next instruction is a
  6635. conditional branch or a retw/retw.n, convert this frag to one that
  6636. will generate a NOP. In any case close it off with a .fill 0. */
  6637. static bfd_boolean next_instrs_are_b_retw (fragS *);
  6638. static void
  6639. xtensa_fix_a0_b_retw_frags (void)
  6640. {
  6641. frchainS *frchP;
  6642. asection *s;
  6643. /* When this routine is called, all of the subsections are still intact
  6644. so we walk over subsections instead of sections. */
  6645. for (s = stdoutput->sections; s; s = s->next)
  6646. for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
  6647. {
  6648. fragS *fragP;
  6649. /* Walk over all of the fragments in a subsection. */
  6650. for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
  6651. {
  6652. if (fragP->fr_type == rs_machine_dependent
  6653. && fragP->fr_subtype == RELAX_ADD_NOP_IF_A0_B_RETW)
  6654. {
  6655. if (next_instrs_are_b_retw (fragP))
  6656. {
  6657. if (fragP->tc_frag_data.is_no_transform)
  6658. as_bad (_("instruction sequence (write a0, branch, retw) may trigger hardware errata"));
  6659. else
  6660. relax_frag_add_nop (fragP);
  6661. }
  6662. frag_wane (fragP);
  6663. }
  6664. }
  6665. }
  6666. }
  6667. static bfd_boolean
  6668. next_instrs_are_b_retw (fragS *fragP)
  6669. {
  6670. xtensa_opcode opcode;
  6671. xtensa_format fmt;
  6672. const fragS *next_fragP = next_non_empty_frag (fragP);
  6673. static xtensa_insnbuf insnbuf = NULL;
  6674. static xtensa_insnbuf slotbuf = NULL;
  6675. xtensa_isa isa = xtensa_default_isa;
  6676. int offset = 0;
  6677. int slot;
  6678. bfd_boolean branch_seen = FALSE;
  6679. if (!insnbuf)
  6680. {
  6681. insnbuf = xtensa_insnbuf_alloc (isa);
  6682. slotbuf = xtensa_insnbuf_alloc (isa);
  6683. }
  6684. if (next_fragP == NULL)
  6685. return FALSE;
  6686. /* Check for the conditional branch. */
  6687. xtensa_insnbuf_from_chars
  6688. (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0);
  6689. fmt = xtensa_format_decode (isa, insnbuf);
  6690. if (fmt == XTENSA_UNDEFINED)
  6691. return FALSE;
  6692. for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
  6693. {
  6694. xtensa_format_get_slot (isa, fmt, slot, insnbuf, slotbuf);
  6695. opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
  6696. branch_seen = (branch_seen
  6697. || xtensa_opcode_is_branch (isa, opcode) == 1);
  6698. }
  6699. if (!branch_seen)
  6700. return FALSE;
  6701. offset += xtensa_format_length (isa, fmt);
  6702. if (offset == next_fragP->fr_fix)
  6703. {
  6704. next_fragP = next_non_empty_frag (next_fragP);
  6705. offset = 0;
  6706. }
  6707. if (next_fragP == NULL)
  6708. return FALSE;
  6709. /* Check for the retw/retw.n. */
  6710. xtensa_insnbuf_from_chars
  6711. (isa, insnbuf, (unsigned char *) &next_fragP->fr_literal[offset], 0);
  6712. fmt = xtensa_format_decode (isa, insnbuf);
  6713. /* Because RETW[.N] is not bundleable, a VLIW bundle here means that we
  6714. have no problems. */
  6715. if (fmt == XTENSA_UNDEFINED
  6716. || xtensa_format_num_slots (isa, fmt) != 1)
  6717. return FALSE;
  6718. xtensa_format_get_slot (isa, fmt, 0, insnbuf, slotbuf);
  6719. opcode = xtensa_opcode_decode (isa, fmt, 0, slotbuf);
  6720. if (opcode == xtensa_retw_opcode || opcode == xtensa_retw_n_opcode)
  6721. return TRUE;
  6722. return FALSE;
  6723. }
  6724. /* Re-process all of the fragments looking to convert all of the
  6725. RELAX_ADD_NOP_IF_PRE_LOOP_END. If there is one instruction and a
  6726. loop end label, convert this frag to one that will generate a NOP.
  6727. In any case close it off with a .fill 0. */
  6728. static bfd_boolean next_instr_is_loop_end (fragS *);
  6729. static void
  6730. xtensa_fix_b_j_loop_end_frags (void)
  6731. {
  6732. frchainS *frchP;
  6733. asection *s;
  6734. /* When this routine is called, all of the subsections are still intact
  6735. so we walk over subsections instead of sections. */
  6736. for (s = stdoutput->sections; s; s = s->next)
  6737. for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
  6738. {
  6739. fragS *fragP;
  6740. /* Walk over all of the fragments in a subsection. */
  6741. for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
  6742. {
  6743. if (fragP->fr_type == rs_machine_dependent
  6744. && fragP->fr_subtype == RELAX_ADD_NOP_IF_PRE_LOOP_END)
  6745. {
  6746. if (next_instr_is_loop_end (fragP))
  6747. {
  6748. if (fragP->tc_frag_data.is_no_transform)
  6749. as_bad (_("branching or jumping to a loop end may trigger hardware errata"));
  6750. else
  6751. relax_frag_add_nop (fragP);
  6752. }
  6753. frag_wane (fragP);
  6754. }
  6755. }
  6756. }
  6757. }
  6758. static bfd_boolean
  6759. next_instr_is_loop_end (fragS *fragP)
  6760. {
  6761. const fragS *next_fragP;
  6762. if (next_frag_is_loop_target (fragP))
  6763. return FALSE;
  6764. next_fragP = next_non_empty_frag (fragP);
  6765. if (next_fragP == NULL)
  6766. return FALSE;
  6767. if (!next_frag_is_loop_target (next_fragP))
  6768. return FALSE;
  6769. /* If the size is >= 3 then there is more than one instruction here.
  6770. The hardware bug will not fire. */
  6771. if (next_fragP->fr_fix > 3)
  6772. return FALSE;
  6773. return TRUE;
  6774. }
  6775. /* Re-process all of the fragments looking to convert all of the
  6776. RELAX_ADD_NOP_IF_CLOSE_LOOP_END. If there is an loop end that is
  6777. not MY loop's loop end within 12 bytes, add enough nops here to
  6778. make it at least 12 bytes away. In any case close it off with a
  6779. .fill 0. */
  6780. static offsetT min_bytes_to_other_loop_end
  6781. (fragS *, fragS *, offsetT);
  6782. static void
  6783. xtensa_fix_close_loop_end_frags (void)
  6784. {
  6785. frchainS *frchP;
  6786. asection *s;
  6787. /* When this routine is called, all of the subsections are still intact
  6788. so we walk over subsections instead of sections. */
  6789. for (s = stdoutput->sections; s; s = s->next)
  6790. for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
  6791. {
  6792. fragS *fragP;
  6793. fragS *current_target = NULL;
  6794. /* Walk over all of the fragments in a subsection. */
  6795. for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
  6796. {
  6797. if (fragP->fr_type == rs_machine_dependent
  6798. && ((fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
  6799. || (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)))
  6800. current_target = symbol_get_frag (fragP->fr_symbol);
  6801. if (current_target
  6802. && fragP->fr_type == rs_machine_dependent
  6803. && fragP->fr_subtype == RELAX_ADD_NOP_IF_CLOSE_LOOP_END)
  6804. {
  6805. offsetT min_bytes;
  6806. int bytes_added = 0;
  6807. #define REQUIRED_LOOP_DIVIDING_BYTES 12
  6808. /* Max out at 12. */
  6809. min_bytes = min_bytes_to_other_loop_end
  6810. (fragP->fr_next, current_target, REQUIRED_LOOP_DIVIDING_BYTES);
  6811. if (min_bytes < REQUIRED_LOOP_DIVIDING_BYTES)
  6812. {
  6813. if (fragP->tc_frag_data.is_no_transform)
  6814. as_bad (_("loop end too close to another loop end may trigger hardware errata"));
  6815. else
  6816. {
  6817. while (min_bytes + bytes_added
  6818. < REQUIRED_LOOP_DIVIDING_BYTES)
  6819. {
  6820. int length = 3;
  6821. if (fragP->fr_var < length)
  6822. as_fatal (_("fr_var %lu < length %d"),
  6823. (long) fragP->fr_var, length);
  6824. else
  6825. {
  6826. assemble_nop (length,
  6827. fragP->fr_literal + fragP->fr_fix);
  6828. fragP->fr_fix += length;
  6829. fragP->fr_var -= length;
  6830. }
  6831. bytes_added += length;
  6832. }
  6833. }
  6834. }
  6835. frag_wane (fragP);
  6836. }
  6837. gas_assert (fragP->fr_type != rs_machine_dependent
  6838. || fragP->fr_subtype != RELAX_ADD_NOP_IF_CLOSE_LOOP_END);
  6839. }
  6840. }
  6841. }
  6842. static offsetT unrelaxed_frag_min_size (fragS *);
  6843. static offsetT
  6844. min_bytes_to_other_loop_end (fragS *fragP,
  6845. fragS *current_target,
  6846. offsetT max_size)
  6847. {
  6848. offsetT offset = 0;
  6849. fragS *current_fragP;
  6850. for (current_fragP = fragP;
  6851. current_fragP;
  6852. current_fragP = current_fragP->fr_next)
  6853. {
  6854. if (current_fragP->tc_frag_data.is_loop_target
  6855. && current_fragP != current_target)
  6856. return offset;
  6857. offset += unrelaxed_frag_min_size (current_fragP);
  6858. if (offset >= max_size)
  6859. return max_size;
  6860. }
  6861. return max_size;
  6862. }
  6863. static offsetT
  6864. unrelaxed_frag_min_size (fragS *fragP)
  6865. {
  6866. offsetT size = fragP->fr_fix;
  6867. /* Add fill size. */
  6868. if (fragP->fr_type == rs_fill)
  6869. size += fragP->fr_offset;
  6870. return size;
  6871. }
  6872. static offsetT
  6873. unrelaxed_frag_max_size (fragS *fragP)
  6874. {
  6875. offsetT size = fragP->fr_fix;
  6876. switch (fragP->fr_type)
  6877. {
  6878. case 0:
  6879. /* Empty frags created by the obstack allocation scheme
  6880. end up with type 0. */
  6881. break;
  6882. case rs_fill:
  6883. case rs_org:
  6884. case rs_space:
  6885. size += fragP->fr_offset;
  6886. break;
  6887. case rs_align:
  6888. case rs_align_code:
  6889. case rs_align_test:
  6890. case rs_leb128:
  6891. case rs_cfa:
  6892. case rs_dwarf2dbg:
  6893. /* No further adjustments needed. */
  6894. break;
  6895. case rs_machine_dependent:
  6896. if (fragP->fr_subtype != RELAX_DESIRE_ALIGN)
  6897. size += fragP->fr_var;
  6898. break;
  6899. default:
  6900. /* We had darn well better know how big it is. */
  6901. gas_assert (0);
  6902. break;
  6903. }
  6904. return size;
  6905. }
  6906. /* Re-process all of the fragments looking to convert all
  6907. of the RELAX_ADD_NOP_IF_SHORT_LOOP. If:
  6908. A)
  6909. 1) the instruction size count to the loop end label
  6910. is too short (<= 2 instructions),
  6911. 2) loop has a jump or branch in it
  6912. or B)
  6913. 1) workaround_all_short_loops is TRUE
  6914. 2) The generating loop was a 'loopgtz' or 'loopnez'
  6915. 3) the instruction size count to the loop end label is too short
  6916. (<= 2 instructions)
  6917. then convert this frag (and maybe the next one) to generate a NOP.
  6918. In any case close it off with a .fill 0. */
  6919. static int count_insns_to_loop_end (fragS *, bfd_boolean, int);
  6920. static bfd_boolean branch_before_loop_end (fragS *);
  6921. static void
  6922. xtensa_fix_short_loop_frags (void)
  6923. {
  6924. frchainS *frchP;
  6925. asection *s;
  6926. /* When this routine is called, all of the subsections are still intact
  6927. so we walk over subsections instead of sections. */
  6928. for (s = stdoutput->sections; s; s = s->next)
  6929. for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
  6930. {
  6931. fragS *fragP;
  6932. xtensa_opcode current_opcode = XTENSA_UNDEFINED;
  6933. /* Walk over all of the fragments in a subsection. */
  6934. for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
  6935. {
  6936. if (fragP->fr_type == rs_machine_dependent
  6937. && ((fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE)
  6938. || (fragP->fr_subtype == RELAX_CHECK_ALIGN_NEXT_OPCODE)))
  6939. {
  6940. TInsn t_insn;
  6941. fragS *loop_frag = next_non_empty_frag (fragP);
  6942. tinsn_from_chars (&t_insn, loop_frag->fr_opcode, 0);
  6943. current_opcode = t_insn.opcode;
  6944. gas_assert (xtensa_opcode_is_loop (xtensa_default_isa,
  6945. current_opcode) == 1);
  6946. }
  6947. if (fragP->fr_type == rs_machine_dependent
  6948. && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
  6949. {
  6950. if (count_insns_to_loop_end (fragP->fr_next, TRUE, 3) < 3
  6951. && (branch_before_loop_end (fragP->fr_next)
  6952. || (workaround_all_short_loops
  6953. && current_opcode != XTENSA_UNDEFINED
  6954. && current_opcode != xtensa_loop_opcode)))
  6955. {
  6956. if (fragP->tc_frag_data.is_no_transform)
  6957. as_bad (_("loop containing less than three instructions may trigger hardware errata"));
  6958. else
  6959. relax_frag_add_nop (fragP);
  6960. }
  6961. frag_wane (fragP);
  6962. }
  6963. }
  6964. }
  6965. }
  6966. static int unrelaxed_frag_min_insn_count (fragS *);
  6967. static int
  6968. count_insns_to_loop_end (fragS *base_fragP,
  6969. bfd_boolean count_relax_add,
  6970. int max_count)
  6971. {
  6972. fragS *fragP = NULL;
  6973. int insn_count = 0;
  6974. fragP = base_fragP;
  6975. for (; fragP && !fragP->tc_frag_data.is_loop_target; fragP = fragP->fr_next)
  6976. {
  6977. insn_count += unrelaxed_frag_min_insn_count (fragP);
  6978. if (insn_count >= max_count)
  6979. return max_count;
  6980. if (count_relax_add)
  6981. {
  6982. if (fragP->fr_type == rs_machine_dependent
  6983. && fragP->fr_subtype == RELAX_ADD_NOP_IF_SHORT_LOOP)
  6984. {
  6985. /* In order to add the appropriate number of
  6986. NOPs, we count an instruction for downstream
  6987. occurrences. */
  6988. insn_count++;
  6989. if (insn_count >= max_count)
  6990. return max_count;
  6991. }
  6992. }
  6993. }
  6994. return insn_count;
  6995. }
  6996. static int
  6997. unrelaxed_frag_min_insn_count (fragS *fragP)
  6998. {
  6999. xtensa_isa isa = xtensa_default_isa;
  7000. static xtensa_insnbuf insnbuf = NULL;
  7001. int insn_count = 0;
  7002. int offset = 0;
  7003. if (!fragP->tc_frag_data.is_insn)
  7004. return insn_count;
  7005. if (!insnbuf)
  7006. insnbuf = xtensa_insnbuf_alloc (isa);
  7007. /* Decode the fixed instructions. */
  7008. while (offset < fragP->fr_fix)
  7009. {
  7010. xtensa_format fmt;
  7011. xtensa_insnbuf_from_chars
  7012. (isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0);
  7013. fmt = xtensa_format_decode (isa, insnbuf);
  7014. if (fmt == XTENSA_UNDEFINED)
  7015. {
  7016. as_fatal (_("undecodable instruction in instruction frag"));
  7017. return insn_count;
  7018. }
  7019. offset += xtensa_format_length (isa, fmt);
  7020. insn_count++;
  7021. }
  7022. return insn_count;
  7023. }
  7024. static bfd_boolean unrelaxed_frag_has_b_j (fragS *);
  7025. static bfd_boolean
  7026. branch_before_loop_end (fragS *base_fragP)
  7027. {
  7028. fragS *fragP;
  7029. for (fragP = base_fragP;
  7030. fragP && !fragP->tc_frag_data.is_loop_target;
  7031. fragP = fragP->fr_next)
  7032. {
  7033. if (unrelaxed_frag_has_b_j (fragP))
  7034. return TRUE;
  7035. }
  7036. return FALSE;
  7037. }
  7038. static bfd_boolean
  7039. unrelaxed_frag_has_b_j (fragS *fragP)
  7040. {
  7041. static xtensa_insnbuf insnbuf = NULL;
  7042. xtensa_isa isa = xtensa_default_isa;
  7043. int offset = 0;
  7044. if (!fragP->tc_frag_data.is_insn)
  7045. return FALSE;
  7046. if (!insnbuf)
  7047. insnbuf = xtensa_insnbuf_alloc (isa);
  7048. /* Decode the fixed instructions. */
  7049. while (offset < fragP->fr_fix)
  7050. {
  7051. xtensa_format fmt;
  7052. int slot;
  7053. xtensa_insnbuf_from_chars
  7054. (isa, insnbuf, (unsigned char *) fragP->fr_literal + offset, 0);
  7055. fmt = xtensa_format_decode (isa, insnbuf);
  7056. if (fmt == XTENSA_UNDEFINED)
  7057. return FALSE;
  7058. for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
  7059. {
  7060. xtensa_opcode opcode =
  7061. get_opcode_from_buf (fragP->fr_literal + offset, slot);
  7062. if (xtensa_opcode_is_branch (isa, opcode) == 1
  7063. || xtensa_opcode_is_jump (isa, opcode) == 1)
  7064. return TRUE;
  7065. }
  7066. offset += xtensa_format_length (isa, fmt);
  7067. }
  7068. return FALSE;
  7069. }
  7070. /* Checks to be made after initial assembly but before relaxation. */
  7071. static bfd_boolean is_empty_loop (const TInsn *, fragS *);
  7072. static bfd_boolean is_local_forward_loop (const TInsn *, fragS *);
  7073. static void
  7074. xtensa_sanity_check (void)
  7075. {
  7076. char *file_name;
  7077. unsigned line;
  7078. frchainS *frchP;
  7079. asection *s;
  7080. as_where (&file_name, &line);
  7081. for (s = stdoutput->sections; s; s = s->next)
  7082. for (frchP = seg_info (s)->frchainP; frchP; frchP = frchP->frch_next)
  7083. {
  7084. fragS *fragP;
  7085. /* Walk over all of the fragments in a subsection. */
  7086. for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
  7087. {
  7088. if (fragP->fr_type == rs_machine_dependent
  7089. && fragP->fr_subtype == RELAX_SLOTS
  7090. && fragP->tc_frag_data.slot_subtypes[0] == RELAX_IMMED)
  7091. {
  7092. static xtensa_insnbuf insnbuf = NULL;
  7093. TInsn t_insn;
  7094. if (fragP->fr_opcode != NULL)
  7095. {
  7096. if (!insnbuf)
  7097. insnbuf = xtensa_insnbuf_alloc (xtensa_default_isa);
  7098. tinsn_from_chars (&t_insn, fragP->fr_opcode, 0);
  7099. tinsn_immed_from_frag (&t_insn, fragP, 0);
  7100. if (xtensa_opcode_is_loop (xtensa_default_isa,
  7101. t_insn.opcode) == 1)
  7102. {
  7103. if (is_empty_loop (&t_insn, fragP))
  7104. {
  7105. new_logical_line (fragP->fr_file, fragP->fr_line);
  7106. as_bad (_("invalid empty loop"));
  7107. }
  7108. if (!is_local_forward_loop (&t_insn, fragP))
  7109. {
  7110. new_logical_line (fragP->fr_file, fragP->fr_line);
  7111. as_bad (_("loop target does not follow "
  7112. "loop instruction in section"));
  7113. }
  7114. }
  7115. }
  7116. }
  7117. }
  7118. }
  7119. new_logical_line (file_name, line);
  7120. }
  7121. #define LOOP_IMMED_OPN 1
  7122. /* Return TRUE if the loop target is the next non-zero fragment. */
  7123. static bfd_boolean
  7124. is_empty_loop (const TInsn *insn, fragS *fragP)
  7125. {
  7126. const expressionS *exp;
  7127. symbolS *symbolP;
  7128. fragS *next_fragP;
  7129. if (insn->insn_type != ITYPE_INSN)
  7130. return FALSE;
  7131. if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1)
  7132. return FALSE;
  7133. if (insn->ntok <= LOOP_IMMED_OPN)
  7134. return FALSE;
  7135. exp = &insn->tok[LOOP_IMMED_OPN];
  7136. if (exp->X_op != O_symbol)
  7137. return FALSE;
  7138. symbolP = exp->X_add_symbol;
  7139. if (!symbolP)
  7140. return FALSE;
  7141. if (symbol_get_frag (symbolP) == NULL)
  7142. return FALSE;
  7143. if (S_GET_VALUE (symbolP) != 0)
  7144. return FALSE;
  7145. /* Walk through the zero-size fragments from this one. If we find
  7146. the target fragment, then this is a zero-size loop. */
  7147. for (next_fragP = fragP->fr_next;
  7148. next_fragP != NULL;
  7149. next_fragP = next_fragP->fr_next)
  7150. {
  7151. if (next_fragP == symbol_get_frag (symbolP))
  7152. return TRUE;
  7153. if (next_fragP->fr_fix != 0)
  7154. return FALSE;
  7155. }
  7156. return FALSE;
  7157. }
  7158. static bfd_boolean
  7159. is_local_forward_loop (const TInsn *insn, fragS *fragP)
  7160. {
  7161. const expressionS *exp;
  7162. symbolS *symbolP;
  7163. fragS *next_fragP;
  7164. if (insn->insn_type != ITYPE_INSN)
  7165. return FALSE;
  7166. if (xtensa_opcode_is_loop (xtensa_default_isa, insn->opcode) != 1)
  7167. return FALSE;
  7168. if (insn->ntok <= LOOP_IMMED_OPN)
  7169. return FALSE;
  7170. exp = &insn->tok[LOOP_IMMED_OPN];
  7171. if (exp->X_op != O_symbol)
  7172. return FALSE;
  7173. symbolP = exp->X_add_symbol;
  7174. if (!symbolP)
  7175. return FALSE;
  7176. if (symbol_get_frag (symbolP) == NULL)
  7177. return FALSE;
  7178. /* Walk through fragments until we find the target.
  7179. If we do not find the target, then this is an invalid loop. */
  7180. for (next_fragP = fragP->fr_next;
  7181. next_fragP != NULL;
  7182. next_fragP = next_fragP->fr_next)
  7183. {
  7184. if (next_fragP == symbol_get_frag (symbolP))
  7185. return TRUE;
  7186. }
  7187. return FALSE;
  7188. }
  7189. #define XTINFO_NAME "Xtensa_Info"
  7190. #define XTINFO_NAMESZ 12
  7191. #define XTINFO_TYPE 1
  7192. static void
  7193. xtensa_add_config_info (void)
  7194. {
  7195. asection *info_sec;
  7196. char *data, *p;
  7197. int sz;
  7198. info_sec = subseg_new (".xtensa.info", 0);
  7199. bfd_set_section_flags (stdoutput, info_sec, SEC_HAS_CONTENTS | SEC_READONLY);
  7200. data = xmalloc (100);
  7201. sprintf (data, "USE_ABSOLUTE_LITERALS=%d\nABI=%d\n",
  7202. XSHAL_USE_ABSOLUTE_LITERALS, XSHAL_ABI);
  7203. sz = strlen (data) + 1;
  7204. /* Add enough null terminators to pad to a word boundary. */
  7205. do
  7206. data[sz++] = 0;
  7207. while ((sz & 3) != 0);
  7208. /* Follow the standard note section layout:
  7209. First write the length of the name string. */
  7210. p = frag_more (4);
  7211. md_number_to_chars (p, (valueT) XTINFO_NAMESZ, 4);
  7212. /* Next comes the length of the "descriptor", i.e., the actual data. */
  7213. p = frag_more (4);
  7214. md_number_to_chars (p, (valueT) sz, 4);
  7215. /* Write the note type. */
  7216. p = frag_more (4);
  7217. md_number_to_chars (p, (valueT) XTINFO_TYPE, 4);
  7218. /* Write the name field. */
  7219. p = frag_more (XTINFO_NAMESZ);
  7220. memcpy (p, XTINFO_NAME, XTINFO_NAMESZ);
  7221. /* Finally, write the descriptor. */
  7222. p = frag_more (sz);
  7223. memcpy (p, data, sz);
  7224. free (data);
  7225. }
  7226. /* Alignment Functions. */
  7227. static int
  7228. get_text_align_power (unsigned target_size)
  7229. {
  7230. if (target_size <= 4)
  7231. return 2;
  7232. if (target_size <= 8)
  7233. return 3;
  7234. if (target_size <= 16)
  7235. return 4;
  7236. if (target_size <= 32)
  7237. return 5;
  7238. if (target_size <= 64)
  7239. return 6;
  7240. if (target_size <= 128)
  7241. return 7;
  7242. if (target_size <= 256)
  7243. return 8;
  7244. if (target_size <= 512)
  7245. return 9;
  7246. if (target_size <= 1024)
  7247. return 10;
  7248. gas_assert (0);
  7249. return 0;
  7250. }
  7251. static int
  7252. get_text_align_max_fill_size (int align_pow,
  7253. bfd_boolean use_nops,
  7254. bfd_boolean use_no_density)
  7255. {
  7256. if (!use_nops)
  7257. return (1 << align_pow);
  7258. if (use_no_density)
  7259. return 3 * (1 << align_pow);
  7260. return 1 + (1 << align_pow);
  7261. }
  7262. /* Calculate the minimum bytes of fill needed at "address" to align a
  7263. target instruction of size "target_size" so that it does not cross a
  7264. power-of-two boundary specified by "align_pow". If "use_nops" is FALSE,
  7265. the fill can be an arbitrary number of bytes. Otherwise, the space must
  7266. be filled by NOP instructions. */
  7267. static int
  7268. get_text_align_fill_size (addressT address,
  7269. int align_pow,
  7270. int target_size,
  7271. bfd_boolean use_nops,
  7272. bfd_boolean use_no_density)
  7273. {
  7274. addressT alignment, fill, fill_limit, fill_step;
  7275. bfd_boolean skip_one = FALSE;
  7276. alignment = (1 << align_pow);
  7277. gas_assert (target_size > 0 && alignment >= (addressT) target_size);
  7278. if (!use_nops)
  7279. {
  7280. fill_limit = alignment;
  7281. fill_step = 1;
  7282. }
  7283. else if (!use_no_density)
  7284. {
  7285. /* Combine 2- and 3-byte NOPs to fill anything larger than one. */
  7286. fill_limit = alignment * 2;
  7287. fill_step = 1;
  7288. skip_one = TRUE;
  7289. }
  7290. else
  7291. {
  7292. /* Fill with 3-byte NOPs -- can only fill multiples of 3. */
  7293. fill_limit = alignment * 3;
  7294. fill_step = 3;
  7295. }
  7296. /* Try all fill sizes until finding one that works. */
  7297. for (fill = 0; fill < fill_limit; fill += fill_step)
  7298. {
  7299. if (skip_one && fill == 1)
  7300. continue;
  7301. if ((address + fill) >> align_pow
  7302. == (address + fill + target_size - 1) >> align_pow)
  7303. return fill;
  7304. }
  7305. gas_assert (0);
  7306. return 0;
  7307. }
  7308. static int
  7309. branch_align_power (segT sec)
  7310. {
  7311. /* If the Xtensa processor has a fetch width of X, and
  7312. the section is aligned to at least that boundary, then a branch
  7313. target need only fit within that aligned block of memory to avoid
  7314. a stall. Otherwise, try to fit branch targets within 4-byte
  7315. aligned blocks (which may be insufficient, e.g., if the section
  7316. has no alignment, but it's good enough). */
  7317. int fetch_align = get_text_align_power(xtensa_fetch_width);
  7318. int sec_align = get_recorded_alignment (sec);
  7319. if (sec_align >= fetch_align)
  7320. return fetch_align;
  7321. return 2;
  7322. }
  7323. /* This will assert if it is not possible. */
  7324. static int
  7325. get_text_align_nop_count (offsetT fill_size, bfd_boolean use_no_density)
  7326. {
  7327. int count = 0;
  7328. if (use_no_density)
  7329. {
  7330. gas_assert (fill_size % 3 == 0);
  7331. return (fill_size / 3);
  7332. }
  7333. gas_assert (fill_size != 1); /* Bad argument. */
  7334. while (fill_size > 1)
  7335. {
  7336. int insn_size = 3;
  7337. if (fill_size == 2 || fill_size == 4)
  7338. insn_size = 2;
  7339. fill_size -= insn_size;
  7340. count++;
  7341. }
  7342. gas_assert (fill_size != 1); /* Bad algorithm. */
  7343. return count;
  7344. }
  7345. static int
  7346. get_text_align_nth_nop_size (offsetT fill_size,
  7347. int n,
  7348. bfd_boolean use_no_density)
  7349. {
  7350. int count = 0;
  7351. if (use_no_density)
  7352. return 3;
  7353. gas_assert (fill_size != 1); /* Bad argument. */
  7354. while (fill_size > 1)
  7355. {
  7356. int insn_size = 3;
  7357. if (fill_size == 2 || fill_size == 4)
  7358. insn_size = 2;
  7359. fill_size -= insn_size;
  7360. count++;
  7361. if (n + 1 == count)
  7362. return insn_size;
  7363. }
  7364. gas_assert (0);
  7365. return 0;
  7366. }
  7367. /* For the given fragment, find the appropriate address
  7368. for it to begin at if we are using NOPs to align it. */
  7369. static addressT
  7370. get_noop_aligned_address (fragS *fragP, addressT address)
  7371. {
  7372. /* The rule is: get next fragment's FIRST instruction. Find
  7373. the smallest number of bytes that need to be added to
  7374. ensure that the next fragment's FIRST instruction will fit
  7375. in a single word.
  7376. E.G., 2 bytes : 0, 1, 2 mod 4
  7377. 3 bytes: 0, 1 mod 4
  7378. If the FIRST instruction MIGHT be relaxed,
  7379. assume that it will become a 3-byte instruction.
  7380. Note again here that LOOP instructions are not bundleable,
  7381. and this relaxation only applies to LOOP opcodes. */
  7382. int fill_size = 0;
  7383. int first_insn_size;
  7384. int loop_insn_size;
  7385. addressT pre_opcode_bytes;
  7386. int align_power;
  7387. fragS *first_insn;
  7388. xtensa_opcode opcode;
  7389. bfd_boolean is_loop;
  7390. gas_assert (fragP->fr_type == rs_machine_dependent);
  7391. gas_assert (fragP->fr_subtype == RELAX_ALIGN_NEXT_OPCODE);
  7392. /* Find the loop frag. */
  7393. first_insn = next_non_empty_frag (fragP);
  7394. /* Now find the first insn frag. */
  7395. first_insn = next_non_empty_frag (first_insn);
  7396. is_loop = next_frag_opcode_is_loop (fragP, &opcode);
  7397. gas_assert (is_loop);
  7398. loop_insn_size = xg_get_single_size (opcode);
  7399. pre_opcode_bytes = next_frag_pre_opcode_bytes (fragP);
  7400. pre_opcode_bytes += loop_insn_size;
  7401. /* For loops, the alignment depends on the size of the
  7402. instruction following the loop, not the LOOP instruction. */
  7403. if (first_insn == NULL)
  7404. first_insn_size = xtensa_fetch_width;
  7405. else
  7406. first_insn_size = get_loop_align_size (frag_format_size (first_insn));
  7407. /* If it was 8, then we'll need a larger alignment for the section. */
  7408. align_power = get_text_align_power (first_insn_size);
  7409. record_alignment (now_seg, align_power);
  7410. fill_size = get_text_align_fill_size
  7411. (address + pre_opcode_bytes, align_power, first_insn_size, TRUE,
  7412. fragP->tc_frag_data.is_no_density);
  7413. return address + fill_size;
  7414. }
  7415. /* 3 mechanisms for relaxing an alignment:
  7416. Align to a power of 2.
  7417. Align so the next fragment's instruction does not cross a word boundary.
  7418. Align the current instruction so that if the next instruction
  7419. were 3 bytes, it would not cross a word boundary.
  7420. We can align with:
  7421. zeros - This is easy; always insert zeros.
  7422. nops - 3-byte and 2-byte instructions
  7423. 2 - 2-byte nop
  7424. 3 - 3-byte nop
  7425. 4 - 2 2-byte nops
  7426. >=5 : 3-byte instruction + fn (n-3)
  7427. widening - widen previous instructions. */
  7428. static offsetT
  7429. get_aligned_diff (fragS *fragP, addressT address, offsetT *max_diff)
  7430. {
  7431. addressT target_address, loop_insn_offset;
  7432. int target_size;
  7433. xtensa_opcode loop_opcode;
  7434. bfd_boolean is_loop;
  7435. int align_power;
  7436. offsetT opt_diff;
  7437. offsetT branch_align;
  7438. fragS *loop_frag;
  7439. gas_assert (fragP->fr_type == rs_machine_dependent);
  7440. switch (fragP->fr_subtype)
  7441. {
  7442. case RELAX_DESIRE_ALIGN:
  7443. target_size = next_frag_format_size (fragP);
  7444. if (target_size == XTENSA_UNDEFINED)
  7445. target_size = 3;
  7446. align_power = branch_align_power (now_seg);
  7447. branch_align = 1 << align_power;
  7448. /* Don't count on the section alignment being as large as the target. */
  7449. if (target_size > branch_align)
  7450. target_size = branch_align;
  7451. opt_diff = get_text_align_fill_size (address, align_power,
  7452. target_size, FALSE, FALSE);
  7453. *max_diff = (opt_diff + branch_align
  7454. - (target_size + ((address + opt_diff) % branch_align)));
  7455. gas_assert (*max_diff >= opt_diff);
  7456. return opt_diff;
  7457. case RELAX_ALIGN_NEXT_OPCODE:
  7458. /* The next non-empty frag after this one holds the LOOP instruction
  7459. that needs to be aligned. The required alignment depends on the
  7460. size of the next non-empty frag after the loop frag, i.e., the
  7461. first instruction in the loop. */
  7462. loop_frag = next_non_empty_frag (fragP);
  7463. target_size = get_loop_align_size (next_frag_format_size (loop_frag));
  7464. loop_insn_offset = 0;
  7465. is_loop = next_frag_opcode_is_loop (fragP, &loop_opcode);
  7466. gas_assert (is_loop);
  7467. /* If the loop has been expanded then the LOOP instruction
  7468. could be at an offset from this fragment. */
  7469. if (loop_frag->tc_frag_data.slot_subtypes[0] != RELAX_IMMED)
  7470. loop_insn_offset = get_expanded_loop_offset (loop_opcode);
  7471. /* In an ideal world, which is what we are shooting for here,
  7472. we wouldn't need to use any NOPs immediately prior to the
  7473. LOOP instruction. If this approach fails, relax_frag_loop_align
  7474. will call get_noop_aligned_address. */
  7475. target_address =
  7476. address + loop_insn_offset + xg_get_single_size (loop_opcode);
  7477. align_power = get_text_align_power (target_size);
  7478. opt_diff = get_text_align_fill_size (target_address, align_power,
  7479. target_size, FALSE, FALSE);
  7480. *max_diff = xtensa_fetch_width
  7481. - ((target_address + opt_diff) % xtensa_fetch_width)
  7482. - target_size + opt_diff;
  7483. gas_assert (*max_diff >= opt_diff);
  7484. return opt_diff;
  7485. default:
  7486. break;
  7487. }
  7488. gas_assert (0);
  7489. return 0;
  7490. }
  7491. /* md_relax_frag Hook and Helper Functions. */
  7492. static long relax_frag_loop_align (fragS *, long);
  7493. static long relax_frag_for_align (fragS *, long);
  7494. static long relax_frag_immed
  7495. (segT, fragS *, long, int, xtensa_format, int, int *, bfd_boolean);
  7496. typedef struct cached_fixup cached_fixupS;
  7497. struct cached_fixup
  7498. {
  7499. int addr;
  7500. int target;
  7501. int delta;
  7502. fixS *fixP;
  7503. };
  7504. typedef struct fixup_cache fixup_cacheS;
  7505. struct fixup_cache
  7506. {
  7507. cached_fixupS *fixups;
  7508. unsigned n_fixups;
  7509. unsigned n_max;
  7510. segT seg;
  7511. fragS *first_frag;
  7512. };
  7513. static int fixup_order (const void *a, const void *b)
  7514. {
  7515. const cached_fixupS *pa = a;
  7516. const cached_fixupS *pb = b;
  7517. if (pa->addr == pb->addr)
  7518. {
  7519. if (pa->target == pb->target)
  7520. {
  7521. if (pa->fixP->fx_r_type == pb->fixP->fx_r_type)
  7522. return 0;
  7523. return pa->fixP->fx_r_type < pb->fixP->fx_r_type ? -1 : 1;
  7524. }
  7525. return pa->target - pb->target;
  7526. }
  7527. return pa->addr - pb->addr;
  7528. }
  7529. static bfd_boolean xtensa_make_cached_fixup (cached_fixupS *o, fixS *fixP)
  7530. {
  7531. xtensa_isa isa = xtensa_default_isa;
  7532. int addr = fixP->fx_frag->fr_address;
  7533. int target;
  7534. int delta;
  7535. symbolS *s = fixP->fx_addsy;
  7536. int slot;
  7537. xtensa_format fmt;
  7538. xtensa_opcode opcode;
  7539. if (fixP->fx_r_type < BFD_RELOC_XTENSA_SLOT0_OP ||
  7540. fixP->fx_r_type > BFD_RELOC_XTENSA_SLOT14_OP)
  7541. return FALSE;
  7542. target = S_GET_VALUE (s);
  7543. delta = target - addr;
  7544. if (abs(delta) < J_RANGE / 2)
  7545. return FALSE;
  7546. xtensa_insnbuf_from_chars (isa, trampoline_buf,
  7547. (unsigned char *) fixP->fx_frag->fr_literal +
  7548. fixP->fx_where, 0);
  7549. fmt = xtensa_format_decode (isa, trampoline_buf);
  7550. gas_assert (fmt != XTENSA_UNDEFINED);
  7551. slot = fixP->tc_fix_data.slot;
  7552. xtensa_format_get_slot (isa, fmt, slot, trampoline_buf, trampoline_slotbuf);
  7553. opcode = xtensa_opcode_decode (isa, fmt, slot, trampoline_slotbuf);
  7554. if (opcode != xtensa_j_opcode)
  7555. return FALSE;
  7556. o->addr = addr;
  7557. o->target = target;
  7558. o->delta = delta;
  7559. o->fixP = fixP;
  7560. return TRUE;
  7561. }
  7562. static void xtensa_realloc_fixup_cache (fixup_cacheS *cache, unsigned add)
  7563. {
  7564. if (cache->n_fixups + add > cache->n_max)
  7565. {
  7566. cache->n_max = (cache->n_fixups + add) * 2;
  7567. cache->fixups = xrealloc (cache->fixups,
  7568. sizeof (*cache->fixups) * cache->n_max);
  7569. }
  7570. }
  7571. static void xtensa_cache_relaxable_fixups (fixup_cacheS *cache,
  7572. segment_info_type *seginfo)
  7573. {
  7574. fixS *fixP;
  7575. cache->n_fixups = 0;
  7576. for (fixP = seginfo->fix_root; fixP ; fixP = fixP->fx_next)
  7577. {
  7578. xtensa_realloc_fixup_cache (cache, 1);
  7579. if (xtensa_make_cached_fixup (cache->fixups + cache->n_fixups, fixP))
  7580. ++cache->n_fixups;
  7581. }
  7582. qsort (cache->fixups, cache->n_fixups, sizeof (*cache->fixups), fixup_order);
  7583. }
  7584. static unsigned xtensa_find_first_cached_fixup (const fixup_cacheS *cache,
  7585. int addr)
  7586. {
  7587. unsigned a = 0;
  7588. unsigned b = cache->n_fixups;
  7589. while (b - a > 1)
  7590. {
  7591. unsigned c = (a + b) / 2;
  7592. if (cache->fixups[c].addr < addr)
  7593. a = c;
  7594. else
  7595. b = c;
  7596. }
  7597. return a;
  7598. }
  7599. static void xtensa_delete_cached_fixup (fixup_cacheS *cache, unsigned i)
  7600. {
  7601. memmove (cache->fixups + i, cache->fixups + i + 1,
  7602. (cache->n_fixups - i - 1) * sizeof (*cache->fixups));
  7603. --cache->n_fixups;
  7604. }
  7605. static bfd_boolean xtensa_add_cached_fixup (fixup_cacheS *cache, fixS *fixP)
  7606. {
  7607. cached_fixupS o;
  7608. unsigned i;
  7609. if (!xtensa_make_cached_fixup (&o, fixP))
  7610. return FALSE;
  7611. xtensa_realloc_fixup_cache (cache, 1);
  7612. i = xtensa_find_first_cached_fixup (cache, o.addr);
  7613. if (i < cache->n_fixups)
  7614. {
  7615. ++i;
  7616. memmove (cache->fixups + i + 1, cache->fixups + i,
  7617. (cache->n_fixups - i) * sizeof (*cache->fixups));
  7618. }
  7619. cache->fixups[i] = o;
  7620. ++cache->n_fixups;
  7621. return TRUE;
  7622. }
  7623. /* Return the number of bytes added to this fragment, given that the
  7624. input has been stretched already by "stretch". */
  7625. long
  7626. xtensa_relax_frag (fragS *fragP, long stretch, int *stretched_p)
  7627. {
  7628. xtensa_isa isa = xtensa_default_isa;
  7629. int unreported = fragP->tc_frag_data.unreported_expansion;
  7630. long new_stretch = 0;
  7631. char *file_name;
  7632. unsigned line;
  7633. int lit_size;
  7634. static xtensa_insnbuf vbuf = NULL;
  7635. int slot, num_slots;
  7636. xtensa_format fmt;
  7637. as_where (&file_name, &line);
  7638. new_logical_line (fragP->fr_file, fragP->fr_line);
  7639. fragP->tc_frag_data.unreported_expansion = 0;
  7640. switch (fragP->fr_subtype)
  7641. {
  7642. case RELAX_ALIGN_NEXT_OPCODE:
  7643. /* Always convert. */
  7644. if (fragP->tc_frag_data.relax_seen)
  7645. new_stretch = relax_frag_loop_align (fragP, stretch);
  7646. break;
  7647. case RELAX_LOOP_END:
  7648. /* Do nothing. */
  7649. break;
  7650. case RELAX_LOOP_END_ADD_NOP:
  7651. /* Add a NOP and switch to .fill 0. */
  7652. new_stretch = relax_frag_add_nop (fragP);
  7653. frag_wane (fragP);
  7654. break;
  7655. case RELAX_DESIRE_ALIGN:
  7656. /* Do nothing. The narrowing before this frag will either align
  7657. it or not. */
  7658. break;
  7659. case RELAX_LITERAL:
  7660. case RELAX_LITERAL_FINAL:
  7661. return 0;
  7662. case RELAX_LITERAL_NR:
  7663. lit_size = 4;
  7664. fragP->fr_subtype = RELAX_LITERAL_FINAL;
  7665. gas_assert (unreported == lit_size);
  7666. memset (&fragP->fr_literal[fragP->fr_fix], 0, 4);
  7667. fragP->fr_var -= lit_size;
  7668. fragP->fr_fix += lit_size;
  7669. new_stretch = 4;
  7670. break;
  7671. case RELAX_SLOTS:
  7672. if (vbuf == NULL)
  7673. vbuf = xtensa_insnbuf_alloc (isa);
  7674. xtensa_insnbuf_from_chars
  7675. (isa, vbuf, (unsigned char *) fragP->fr_opcode, 0);
  7676. fmt = xtensa_format_decode (isa, vbuf);
  7677. num_slots = xtensa_format_num_slots (isa, fmt);
  7678. for (slot = 0; slot < num_slots; slot++)
  7679. {
  7680. switch (fragP->tc_frag_data.slot_subtypes[slot])
  7681. {
  7682. case RELAX_NARROW:
  7683. if (fragP->tc_frag_data.relax_seen)
  7684. new_stretch += relax_frag_for_align (fragP, stretch);
  7685. break;
  7686. case RELAX_IMMED:
  7687. case RELAX_IMMED_STEP1:
  7688. case RELAX_IMMED_STEP2:
  7689. case RELAX_IMMED_STEP3:
  7690. /* Place the immediate. */
  7691. new_stretch += relax_frag_immed
  7692. (now_seg, fragP, stretch,
  7693. fragP->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED,
  7694. fmt, slot, stretched_p, FALSE);
  7695. break;
  7696. default:
  7697. /* This is OK; see the note in xg_assemble_vliw_tokens. */
  7698. break;
  7699. }
  7700. }
  7701. break;
  7702. case RELAX_LITERAL_POOL_BEGIN:
  7703. if (fragP->fr_var != 0)
  7704. {
  7705. /* We have a converted "candidate" literal pool;
  7706. assemble a jump around it. */
  7707. TInsn insn;
  7708. if (!litpool_slotbuf)
  7709. {
  7710. litpool_buf = xtensa_insnbuf_alloc (isa);
  7711. litpool_slotbuf = xtensa_insnbuf_alloc (isa);
  7712. }
  7713. new_stretch += 3;
  7714. fragP->tc_frag_data.relax_seen = FALSE; /* Need another pass. */
  7715. fragP->tc_frag_data.is_insn = TRUE;
  7716. tinsn_init (&insn);
  7717. insn.insn_type = ITYPE_INSN;
  7718. insn.opcode = xtensa_j_opcode;
  7719. insn.ntok = 1;
  7720. set_expr_symbol_offset (&insn.tok[0], fragP->fr_symbol,
  7721. fragP->fr_fix);
  7722. fmt = xg_get_single_format (xtensa_j_opcode);
  7723. tinsn_to_slotbuf (fmt, 0, &insn, litpool_slotbuf);
  7724. xtensa_format_set_slot (isa, fmt, 0, litpool_buf, litpool_slotbuf);
  7725. xtensa_insnbuf_to_chars (isa, litpool_buf,
  7726. (unsigned char *)fragP->fr_literal +
  7727. fragP->fr_fix, 3);
  7728. fragP->fr_fix += 3;
  7729. fragP->fr_var -= 3;
  7730. /* Add a fix-up. */
  7731. fix_new (fragP, 0, 3, fragP->fr_symbol, 0, TRUE,
  7732. BFD_RELOC_XTENSA_SLOT0_OP);
  7733. }
  7734. break;
  7735. case RELAX_LITERAL_POOL_END:
  7736. case RELAX_LITERAL_POOL_CANDIDATE_BEGIN:
  7737. case RELAX_MAYBE_UNREACHABLE:
  7738. case RELAX_MAYBE_DESIRE_ALIGN:
  7739. /* No relaxation required. */
  7740. break;
  7741. case RELAX_FILL_NOP:
  7742. case RELAX_UNREACHABLE:
  7743. if (fragP->tc_frag_data.relax_seen)
  7744. new_stretch += relax_frag_for_align (fragP, stretch);
  7745. break;
  7746. case RELAX_TRAMPOLINE:
  7747. if (fragP->tc_frag_data.relax_seen)
  7748. {
  7749. static fixup_cacheS fixup_cache;
  7750. segment_info_type *seginfo = seg_info (now_seg);
  7751. int trampaddr = fragP->fr_address + fragP->fr_fix;
  7752. int searchaddr = trampaddr < J_RANGE ? 0 : trampaddr - J_RANGE;
  7753. unsigned i;
  7754. if (now_seg != fixup_cache.seg ||
  7755. fragP == fixup_cache.first_frag ||
  7756. fixup_cache.first_frag == NULL)
  7757. {
  7758. xtensa_cache_relaxable_fixups (&fixup_cache, seginfo);
  7759. fixup_cache.seg = now_seg;
  7760. fixup_cache.first_frag = fragP;
  7761. }
  7762. /* Scan for jumps that will not reach. */
  7763. for (i = xtensa_find_first_cached_fixup (&fixup_cache, searchaddr);
  7764. i < fixup_cache.n_fixups; ++i)
  7765. {
  7766. fixS *fixP = fixup_cache.fixups[i].fixP;
  7767. int target = fixup_cache.fixups[i].target;
  7768. int addr = fixup_cache.fixups[i].addr;
  7769. int delta = fixup_cache.fixups[i].delta + stretch;
  7770. trampaddr = fragP->fr_address + fragP->fr_fix;
  7771. if (addr + J_RANGE < trampaddr)
  7772. continue;
  7773. if (addr > trampaddr + J_RANGE)
  7774. break;
  7775. if (abs (delta) < J_RANGE)
  7776. continue;
  7777. slot = fixP->tc_fix_data.slot;
  7778. if (delta > J_RANGE || delta < -1 * J_RANGE)
  7779. { /* Found an out-of-range jump; scan the list of trampolines for the best match. */
  7780. struct trampoline_seg *ts = find_trampoline_seg (now_seg);
  7781. struct trampoline_frag *tf = ts->trampoline_list.next;
  7782. struct trampoline_frag *prev = &ts->trampoline_list;
  7783. int lower = (target < addr) ? target : addr;
  7784. int upper = (target > addr) ? target : addr;
  7785. int midpoint = lower + (upper - lower) / 2;
  7786. if ((upper - lower) > 2 * J_RANGE)
  7787. {
  7788. /* One trampoline won't suffice; we need multiple jumps.
  7789. Jump to the trampoline that's farthest, but still in
  7790. range relative to the original "j" instruction. */
  7791. for ( ; tf; prev = tf, tf = tf->next )
  7792. {
  7793. int this_addr = tf->fragP->fr_address + tf->fragP->fr_fix;
  7794. int next_addr = (tf->next) ? tf->next->fragP->fr_address + tf->next->fragP->fr_fix : 0 ;
  7795. if (addr == lower)
  7796. {
  7797. /* Forward jump. */
  7798. if (this_addr - addr < J_RANGE)
  7799. break;
  7800. }
  7801. else
  7802. {
  7803. /* Backward jump. */
  7804. if (next_addr == 0 || addr - next_addr > J_RANGE)
  7805. break;
  7806. }
  7807. }
  7808. }
  7809. else
  7810. {
  7811. struct trampoline_frag *best_tf = NULL;
  7812. int best_delta = 0;
  7813. for ( ; tf; prev = tf, tf = tf->next )
  7814. {
  7815. int this_addr = tf->fragP->fr_address + tf->fragP->fr_fix;
  7816. int this_delta = abs (this_addr - midpoint);
  7817. if (!best_tf || this_delta < best_delta)
  7818. {
  7819. best_tf = tf;
  7820. best_delta = this_delta;
  7821. }
  7822. }
  7823. tf = best_tf;
  7824. }
  7825. if (tf->fragP == fragP)
  7826. {
  7827. if (abs (addr - trampaddr) < J_RANGE)
  7828. { /* The trampoline is in range of original; fix it! */
  7829. fixS *newfixP;
  7830. int offset;
  7831. TInsn insn;
  7832. symbolS *lsym;
  7833. fragS *fP; /* The out-of-range jump. */
  7834. new_stretch += init_trampoline_frag (tf);
  7835. offset = fragP->fr_fix; /* Where to assemble the j insn. */
  7836. lsym = fragP->fr_symbol;
  7837. fP = fixP->fx_frag;
  7838. /* Assemble a jump to the target label here. */
  7839. tinsn_init (&insn);
  7840. insn.insn_type = ITYPE_INSN;
  7841. insn.opcode = xtensa_j_opcode;
  7842. insn.ntok = 1;
  7843. set_expr_symbol_offset (&insn.tok[0], lsym, offset);
  7844. fmt = xg_get_single_format (xtensa_j_opcode);
  7845. tinsn_to_slotbuf (fmt, 0, &insn, trampoline_slotbuf);
  7846. xtensa_format_set_slot (isa, fmt, 0, trampoline_buf, trampoline_slotbuf);
  7847. xtensa_insnbuf_to_chars (isa, trampoline_buf, (unsigned char *)fragP->fr_literal + offset, 3);
  7848. fragP->fr_fix += 3;
  7849. fragP->fr_var -= 3;
  7850. /* Add a fix-up for the original j insn. */
  7851. newfixP = fix_new (fP, fixP->fx_where, fixP->fx_size, lsym, fragP->fr_fix - 3, TRUE, fixP->fx_r_type);
  7852. newfixP->fx_no_overflow = 1;
  7853. newfixP->tc_fix_data.X_add_symbol = lsym;
  7854. newfixP->tc_fix_data.X_add_number = offset;
  7855. newfixP->tc_fix_data.slot = slot;
  7856. xtensa_delete_cached_fixup (&fixup_cache, i);
  7857. xtensa_add_cached_fixup (&fixup_cache, newfixP);
  7858. /* Move the fix-up from the original j insn to this one. */
  7859. fixP->fx_frag = fragP;
  7860. fixP->fx_where = fragP->fr_fix - 3;
  7861. fixP->tc_fix_data.slot = 0;
  7862. xtensa_add_cached_fixup (&fixup_cache, fixP);
  7863. /* re-do current fixup */
  7864. --i;
  7865. /* Adjust the jump around this trampoline (if present). */
  7866. if (tf->fixP != NULL)
  7867. {
  7868. tf->fixP->fx_offset += 3;
  7869. }
  7870. new_stretch += 3;
  7871. fragP->tc_frag_data.relax_seen = FALSE; /* Need another pass. */
  7872. /* Do we have room for more? */
  7873. if (fragP->fr_var < 3)
  7874. { /* No, convert to fill. */
  7875. frag_wane (fragP);
  7876. fragP->fr_subtype = 0;
  7877. /* Remove from the trampoline_list. */
  7878. prev->next = tf->next;
  7879. if (fragP == fixup_cache.first_frag)
  7880. fixup_cache.first_frag = NULL;
  7881. break;
  7882. }
  7883. }
  7884. }
  7885. }
  7886. }
  7887. }
  7888. break;
  7889. default:
  7890. as_bad (_("bad relaxation state"));
  7891. }
  7892. /* Tell gas we need another relaxation pass. */
  7893. if (! fragP->tc_frag_data.relax_seen)
  7894. {
  7895. fragP->tc_frag_data.relax_seen = TRUE;
  7896. *stretched_p = 1;
  7897. }
  7898. new_logical_line (file_name, line);
  7899. return new_stretch;
  7900. }
  7901. static long
  7902. relax_frag_loop_align (fragS *fragP, long stretch)
  7903. {
  7904. addressT old_address, old_next_address, old_size;
  7905. addressT new_address, new_next_address, new_size;
  7906. addressT growth;
  7907. /* All the frags with relax_frag_for_alignment prior to this one in the
  7908. section have been done, hopefully eliminating the need for a NOP here.
  7909. But, this will put it in if necessary. */
  7910. /* Calculate the old address of this fragment and the next fragment. */
  7911. old_address = fragP->fr_address - stretch;
  7912. old_next_address = (fragP->fr_address - stretch + fragP->fr_fix +
  7913. fragP->tc_frag_data.text_expansion[0]);
  7914. old_size = old_next_address - old_address;
  7915. /* Calculate the new address of this fragment and the next fragment. */
  7916. new_address = fragP->fr_address;
  7917. new_next_address =
  7918. get_noop_aligned_address (fragP, fragP->fr_address + fragP->fr_fix);
  7919. new_size = new_next_address - new_address;
  7920. growth = new_size - old_size;
  7921. /* Fix up the text_expansion field and return the new growth. */
  7922. fragP->tc_frag_data.text_expansion[0] += growth;
  7923. return growth;
  7924. }
  7925. /* Add a NOP instruction. */
  7926. static long
  7927. relax_frag_add_nop (fragS *fragP)
  7928. {
  7929. char *nop_buf = fragP->fr_literal + fragP->fr_fix;
  7930. int length = fragP->tc_frag_data.is_no_density ? 3 : 2;
  7931. assemble_nop (length, nop_buf);
  7932. fragP->tc_frag_data.is_insn = TRUE;
  7933. if (fragP->fr_var < length)
  7934. {
  7935. as_fatal (_("fr_var (%ld) < length (%d)"), (long) fragP->fr_var, length);
  7936. return 0;
  7937. }
  7938. fragP->fr_fix += length;
  7939. fragP->fr_var -= length;
  7940. return length;
  7941. }
  7942. static long future_alignment_required (fragS *, long);
  7943. static long
  7944. relax_frag_for_align (fragS *fragP, long stretch)
  7945. {
  7946. /* Overview of the relaxation procedure for alignment:
  7947. We can widen with NOPs or by widening instructions or by filling
  7948. bytes after jump instructions. Find the opportune places and widen
  7949. them if necessary. */
  7950. long stretch_me;
  7951. long diff;
  7952. gas_assert (fragP->fr_subtype == RELAX_FILL_NOP
  7953. || fragP->fr_subtype == RELAX_UNREACHABLE
  7954. || (fragP->fr_subtype == RELAX_SLOTS
  7955. && fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW));
  7956. stretch_me = future_alignment_required (fragP, stretch);
  7957. diff = stretch_me - fragP->tc_frag_data.text_expansion[0];
  7958. if (diff == 0)
  7959. return 0;
  7960. if (diff < 0)
  7961. {
  7962. /* We expanded on a previous pass. Can we shrink now? */
  7963. long shrink = fragP->tc_frag_data.text_expansion[0] - stretch_me;
  7964. if (shrink <= stretch && stretch > 0)
  7965. {
  7966. fragP->tc_frag_data.text_expansion[0] = stretch_me;
  7967. return -shrink;
  7968. }
  7969. return 0;
  7970. }
  7971. /* Below here, diff > 0. */
  7972. fragP->tc_frag_data.text_expansion[0] = stretch_me;
  7973. return diff;
  7974. }
  7975. /* Return the address of the next frag that should be aligned.
  7976. By "address" we mean the address it _would_ be at if there
  7977. is no action taken to align it between here and the target frag.
  7978. In other words, if no narrows and no fill nops are used between
  7979. here and the frag to align, _even_if_ some of the frags we use
  7980. to align targets have already expanded on a previous relaxation
  7981. pass.
  7982. Also, count each frag that may be used to help align the target.
  7983. Return 0 if there are no frags left in the chain that need to be
  7984. aligned. */
  7985. static addressT
  7986. find_address_of_next_align_frag (fragS **fragPP,
  7987. int *wide_nops,
  7988. int *narrow_nops,
  7989. int *widens,
  7990. bfd_boolean *paddable)
  7991. {
  7992. fragS *fragP = *fragPP;
  7993. addressT address = fragP->fr_address;
  7994. /* Do not reset the counts to 0. */
  7995. while (fragP)
  7996. {
  7997. /* Limit this to a small search. */
  7998. if (*widens >= (int) xtensa_fetch_width)
  7999. {
  8000. *fragPP = fragP;
  8001. return 0;
  8002. }
  8003. address += fragP->fr_fix;
  8004. if (fragP->fr_type == rs_fill)
  8005. address += fragP->fr_offset * fragP->fr_var;
  8006. else if (fragP->fr_type == rs_machine_dependent)
  8007. {
  8008. switch (fragP->fr_subtype)
  8009. {
  8010. case RELAX_UNREACHABLE:
  8011. *paddable = TRUE;
  8012. break;
  8013. case RELAX_FILL_NOP:
  8014. (*wide_nops)++;
  8015. if (!fragP->tc_frag_data.is_no_density)
  8016. (*narrow_nops)++;
  8017. break;
  8018. case RELAX_SLOTS:
  8019. if (fragP->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
  8020. {
  8021. (*widens)++;
  8022. break;
  8023. }
  8024. address += total_frag_text_expansion (fragP);
  8025. break;
  8026. case RELAX_IMMED:
  8027. address += fragP->tc_frag_data.text_expansion[0];
  8028. break;
  8029. case RELAX_ALIGN_NEXT_OPCODE:
  8030. case RELAX_DESIRE_ALIGN:
  8031. *fragPP = fragP;
  8032. return address;
  8033. case RELAX_MAYBE_UNREACHABLE:
  8034. case RELAX_MAYBE_DESIRE_ALIGN:
  8035. /* Do nothing. */
  8036. break;
  8037. default:
  8038. /* Just punt if we don't know the type. */
  8039. *fragPP = fragP;
  8040. return 0;
  8041. }
  8042. }
  8043. else
  8044. {
  8045. /* Just punt if we don't know the type. */
  8046. *fragPP = fragP;
  8047. return 0;
  8048. }
  8049. fragP = fragP->fr_next;
  8050. }
  8051. *fragPP = fragP;
  8052. return 0;
  8053. }
  8054. static long bytes_to_stretch (fragS *, int, int, int, int);
  8055. static long
  8056. future_alignment_required (fragS *fragP, long stretch ATTRIBUTE_UNUSED)
  8057. {
  8058. fragS *this_frag = fragP;
  8059. long address;
  8060. int num_widens = 0;
  8061. int wide_nops = 0;
  8062. int narrow_nops = 0;
  8063. bfd_boolean paddable = FALSE;
  8064. offsetT local_opt_diff;
  8065. offsetT opt_diff;
  8066. offsetT max_diff;
  8067. int stretch_amount = 0;
  8068. int local_stretch_amount;
  8069. int global_stretch_amount;
  8070. address = find_address_of_next_align_frag
  8071. (&fragP, &wide_nops, &narrow_nops, &num_widens, &paddable);
  8072. if (!address)
  8073. {
  8074. if (this_frag->tc_frag_data.is_aligning_branch)
  8075. this_frag->tc_frag_data.slot_subtypes[0] = RELAX_IMMED;
  8076. else
  8077. frag_wane (this_frag);
  8078. }
  8079. else
  8080. {
  8081. local_opt_diff = get_aligned_diff (fragP, address, &max_diff);
  8082. opt_diff = local_opt_diff;
  8083. gas_assert (opt_diff >= 0);
  8084. gas_assert (max_diff >= opt_diff);
  8085. if (max_diff == 0)
  8086. return 0;
  8087. if (fragP)
  8088. fragP = fragP->fr_next;
  8089. while (fragP && opt_diff < max_diff && address)
  8090. {
  8091. /* We only use these to determine if we can exit early
  8092. because there will be plenty of ways to align future
  8093. align frags. */
  8094. int glob_widens = 0;
  8095. int dnn = 0;
  8096. int dw = 0;
  8097. bfd_boolean glob_pad = 0;
  8098. address = find_address_of_next_align_frag
  8099. (&fragP, &glob_widens, &dnn, &dw, &glob_pad);
  8100. /* If there is a padable portion, then skip. */
  8101. if (glob_pad || glob_widens >= (1 << branch_align_power (now_seg)))
  8102. address = 0;
  8103. if (address)
  8104. {
  8105. offsetT next_m_diff;
  8106. offsetT next_o_diff;
  8107. /* Downrange frags haven't had stretch added to them yet. */
  8108. address += stretch;
  8109. /* The address also includes any text expansion from this
  8110. frag in a previous pass, but we don't want that. */
  8111. address -= this_frag->tc_frag_data.text_expansion[0];
  8112. /* Assume we are going to move at least opt_diff. In
  8113. reality, we might not be able to, but assuming that
  8114. we will helps catch cases where moving opt_diff pushes
  8115. the next target from aligned to unaligned. */
  8116. address += opt_diff;
  8117. next_o_diff = get_aligned_diff (fragP, address, &next_m_diff);
  8118. /* Now cleanup for the adjustments to address. */
  8119. next_o_diff += opt_diff;
  8120. next_m_diff += opt_diff;
  8121. if (next_o_diff <= max_diff && next_o_diff > opt_diff)
  8122. opt_diff = next_o_diff;
  8123. if (next_m_diff < max_diff)
  8124. max_diff = next_m_diff;
  8125. fragP = fragP->fr_next;
  8126. }
  8127. }
  8128. /* If there are enough wideners in between, do it. */
  8129. if (paddable)
  8130. {
  8131. if (this_frag->fr_subtype == RELAX_UNREACHABLE)
  8132. {
  8133. gas_assert (opt_diff <= (signed) xtensa_fetch_width);
  8134. return opt_diff;
  8135. }
  8136. return 0;
  8137. }
  8138. local_stretch_amount
  8139. = bytes_to_stretch (this_frag, wide_nops, narrow_nops,
  8140. num_widens, local_opt_diff);
  8141. global_stretch_amount
  8142. = bytes_to_stretch (this_frag, wide_nops, narrow_nops,
  8143. num_widens, opt_diff);
  8144. /* If the condition below is true, then the frag couldn't
  8145. stretch the correct amount for the global case, so we just
  8146. optimize locally. We'll rely on the subsequent frags to get
  8147. the correct alignment in the global case. */
  8148. if (global_stretch_amount < local_stretch_amount)
  8149. stretch_amount = local_stretch_amount;
  8150. else
  8151. stretch_amount = global_stretch_amount;
  8152. if (this_frag->fr_subtype == RELAX_SLOTS
  8153. && this_frag->tc_frag_data.slot_subtypes[0] == RELAX_NARROW)
  8154. gas_assert (stretch_amount <= 1);
  8155. else if (this_frag->fr_subtype == RELAX_FILL_NOP)
  8156. {
  8157. if (this_frag->tc_frag_data.is_no_density)
  8158. gas_assert (stretch_amount == 3 || stretch_amount == 0);
  8159. else
  8160. gas_assert (stretch_amount <= 3);
  8161. }
  8162. }
  8163. return stretch_amount;
  8164. }
  8165. /* The idea: widen everything you can to get a target or loop aligned,
  8166. then start using NOPs.
  8167. wide_nops = the number of wide NOPs available for aligning
  8168. narrow_nops = the number of narrow NOPs available for aligning
  8169. (a subset of wide_nops)
  8170. widens = the number of narrow instructions that should be widened
  8171. */
  8172. static long
  8173. bytes_to_stretch (fragS *this_frag,
  8174. int wide_nops,
  8175. int narrow_nops,
  8176. int num_widens,
  8177. int desired_diff)
  8178. {
  8179. int nops_needed;
  8180. int nop_bytes;
  8181. int extra_bytes;
  8182. int bytes_short = desired_diff - num_widens;
  8183. gas_assert (desired_diff >= 0
  8184. && desired_diff < (signed) xtensa_fetch_width);
  8185. if (desired_diff == 0)
  8186. return 0;
  8187. gas_assert (wide_nops > 0 || num_widens > 0);
  8188. /* Always prefer widening to NOP-filling. */
  8189. if (bytes_short < 0)
  8190. {
  8191. /* There are enough RELAX_NARROW frags after this one
  8192. to align the target without widening this frag in any way. */
  8193. return 0;
  8194. }
  8195. if (bytes_short == 0)
  8196. {
  8197. /* Widen every narrow between here and the align target
  8198. and the align target will be properly aligned. */
  8199. if (this_frag->fr_subtype == RELAX_FILL_NOP)
  8200. return 0;
  8201. else
  8202. return 1;
  8203. }
  8204. /* From here we will need at least one NOP to get an alignment.
  8205. However, we may not be able to align at all, in which case,
  8206. don't widen. */
  8207. nops_needed = desired_diff / 3;
  8208. /* If there aren't enough nops, don't widen. */
  8209. if (nops_needed > wide_nops)
  8210. return 0;
  8211. /* First try it with all wide nops. */
  8212. nop_bytes = nops_needed * 3;
  8213. extra_bytes = desired_diff - nop_bytes;
  8214. if (nop_bytes + num_widens >= desired_diff)
  8215. {
  8216. if (this_frag->fr_subtype == RELAX_FILL_NOP)
  8217. return 3;
  8218. else if (num_widens == extra_bytes)
  8219. return 1;
  8220. return 0;
  8221. }
  8222. /* Add a narrow nop. */
  8223. nops_needed++;
  8224. nop_bytes += 2;
  8225. extra_bytes -= 2;
  8226. if (narrow_nops == 0 || nops_needed > wide_nops)
  8227. return 0;
  8228. if (nop_bytes + num_widens >= desired_diff && extra_bytes >= 0)
  8229. {
  8230. if (this_frag->fr_subtype == RELAX_FILL_NOP)
  8231. return !this_frag->tc_frag_data.is_no_density ? 2 : 3;
  8232. else if (num_widens == extra_bytes)
  8233. return 1;
  8234. return 0;
  8235. }
  8236. /* Replace a wide nop with a narrow nop--we can get here if
  8237. extra_bytes was negative in the previous conditional. */
  8238. if (narrow_nops == 1)
  8239. return 0;
  8240. nop_bytes--;
  8241. extra_bytes++;
  8242. if (nop_bytes + num_widens >= desired_diff)
  8243. {
  8244. if (this_frag->fr_subtype == RELAX_FILL_NOP)
  8245. return !this_frag->tc_frag_data.is_no_density ? 2 : 3;
  8246. else if (num_widens == extra_bytes)
  8247. return 1;
  8248. return 0;
  8249. }
  8250. /* If we can't satisfy any of the above cases, then we can't align
  8251. using padding or fill nops. */
  8252. return 0;
  8253. }
  8254. static struct trampoline_frag *
  8255. search_trampolines (TInsn *tinsn, fragS *fragP, bfd_boolean unreachable_only)
  8256. {
  8257. struct trampoline_seg *ts = find_trampoline_seg (now_seg);
  8258. struct trampoline_frag *tf = (ts) ? ts->trampoline_list.next : NULL;
  8259. struct trampoline_frag *best_tf = NULL;
  8260. int best_delta = 0;
  8261. int best_addr = 0;
  8262. symbolS *sym = tinsn->tok[0].X_add_symbol;
  8263. offsetT target = S_GET_VALUE (sym) + tinsn->tok[0].X_add_number;
  8264. offsetT addr = fragP->fr_address;
  8265. offsetT lower = (addr < target) ? addr : target;
  8266. offsetT upper = (addr > target) ? addr : target;
  8267. int delta = upper - lower;
  8268. offsetT midpoint = lower + delta / 2;
  8269. int this_delta = -1;
  8270. int this_addr = -1;
  8271. if (delta > 2 * J_RANGE)
  8272. {
  8273. /* One trampoline won't do; we need multiple.
  8274. Choose the farthest trampoline that's still in range of the original
  8275. and let a later pass finish the job. */
  8276. for ( ; tf; tf = tf->next)
  8277. {
  8278. int next_addr = (tf->next) ? tf->next->fragP->fr_address + tf->next->fragP->fr_fix : 0;
  8279. this_addr = tf->fragP->fr_address + tf->fragP->fr_fix;
  8280. if (lower == addr)
  8281. {
  8282. /* Forward jump. */
  8283. if (this_addr - addr < J_RANGE)
  8284. break;
  8285. }
  8286. else
  8287. {
  8288. /* Backward jump. */
  8289. if (next_addr == 0 || addr - next_addr > J_RANGE)
  8290. break;
  8291. }
  8292. }
  8293. if (abs (addr - this_addr) < J_RANGE)
  8294. return tf;
  8295. return NULL;
  8296. }
  8297. for ( ; tf; tf = tf->next)
  8298. {
  8299. this_addr = tf->fragP->fr_address + tf->fragP->fr_fix;
  8300. this_delta = abs (this_addr - midpoint);
  8301. if (unreachable_only && tf->needs_jump_around)
  8302. continue;
  8303. if (!best_tf || this_delta < best_delta)
  8304. {
  8305. best_tf = tf;
  8306. best_delta = this_delta;
  8307. best_addr = this_addr;
  8308. }
  8309. }
  8310. if (best_tf &&
  8311. best_delta < J_RANGE &&
  8312. abs(best_addr - lower) < J_RANGE &&
  8313. abs(best_addr - upper) < J_RANGE)
  8314. return best_tf;
  8315. return NULL; /* No suitable trampoline found. */
  8316. }
  8317. static struct trampoline_frag *
  8318. get_best_trampoline (TInsn *tinsn, fragS *fragP)
  8319. {
  8320. struct trampoline_frag *tf = NULL;
  8321. tf = search_trampolines (tinsn, fragP, TRUE); /* Try unreachable first. */
  8322. if (tf == NULL)
  8323. tf = search_trampolines (tinsn, fragP, FALSE); /* Try ones needing a jump-around, too. */
  8324. return tf;
  8325. }
  8326. static void
  8327. check_and_update_trampolines (void)
  8328. {
  8329. struct trampoline_seg *ts = find_trampoline_seg (now_seg);
  8330. struct trampoline_frag *tf = ts->trampoline_list.next;
  8331. struct trampoline_frag *prev = &ts->trampoline_list;
  8332. for ( ; tf; prev = tf, tf = tf->next)
  8333. {
  8334. if (tf->fragP->fr_var < 3)
  8335. {
  8336. frag_wane (tf->fragP);
  8337. prev->next = tf->next;
  8338. tf->fragP = NULL;
  8339. }
  8340. }
  8341. }
  8342. static int
  8343. init_trampoline_frag (struct trampoline_frag *trampP)
  8344. {
  8345. fragS *fp = trampP->fragP;
  8346. int growth = 0;
  8347. if (fp->fr_fix == 0)
  8348. {
  8349. symbolS *lsym;
  8350. char label[10 + 2 * sizeof(fp)];
  8351. sprintf (label, ".L0_TR_%p", fp);
  8352. lsym = (symbolS *)local_symbol_make (label, now_seg, 0, fp);
  8353. fp->fr_symbol = lsym;
  8354. if (trampP->needs_jump_around)
  8355. {
  8356. /* Add a jump around this block of jumps, in case
  8357. control flows into this block. */
  8358. fixS *fixP;
  8359. TInsn insn;
  8360. xtensa_format fmt;
  8361. xtensa_isa isa = xtensa_default_isa;
  8362. fp->tc_frag_data.is_insn = 1;
  8363. /* Assemble a jump insn. */
  8364. tinsn_init (&insn);
  8365. insn.insn_type = ITYPE_INSN;
  8366. insn.opcode = xtensa_j_opcode;
  8367. insn.ntok = 1;
  8368. set_expr_symbol_offset (&insn.tok[0], lsym, 3);
  8369. fmt = xg_get_single_format (xtensa_j_opcode);
  8370. tinsn_to_slotbuf (fmt, 0, &insn, trampoline_slotbuf);
  8371. xtensa_format_set_slot (isa, fmt, 0, trampoline_buf, trampoline_slotbuf);
  8372. xtensa_insnbuf_to_chars (isa, trampoline_buf, (unsigned char *)fp->fr_literal, 3);
  8373. fp->fr_fix += 3;
  8374. fp->fr_var -= 3;
  8375. growth = 3;
  8376. fixP = fix_new (fp, 0, 3, lsym, 3, TRUE, BFD_RELOC_XTENSA_SLOT0_OP);
  8377. trampP->fixP = fixP;
  8378. }
  8379. }
  8380. return growth;
  8381. }
  8382. static int
  8383. add_jump_to_trampoline (struct trampoline_frag *trampP, fragS *origfrag)
  8384. {
  8385. fragS *tramp = trampP->fragP;
  8386. fixS *fixP;
  8387. int offset = tramp->fr_fix; /* Where to assemble the j insn. */
  8388. TInsn insn;
  8389. symbolS *lsym;
  8390. symbolS *tsym;
  8391. int toffset;
  8392. xtensa_format fmt;
  8393. xtensa_isa isa = xtensa_default_isa;
  8394. int growth = 0;
  8395. lsym = tramp->fr_symbol;
  8396. /* Assemble a jump to the target label in the trampoline frag. */
  8397. tsym = origfrag->tc_frag_data.slot_symbols[0];
  8398. toffset = origfrag-> tc_frag_data.slot_offsets[0];
  8399. tinsn_init (&insn);
  8400. insn.insn_type = ITYPE_INSN;
  8401. insn.opcode = xtensa_j_opcode;
  8402. insn.ntok = 1;
  8403. set_expr_symbol_offset (&insn.tok[0], tsym, toffset);
  8404. fmt = xg_get_single_format (xtensa_j_opcode);
  8405. tinsn_to_slotbuf (fmt, 0, &insn, trampoline_slotbuf);
  8406. xtensa_format_set_slot (isa, fmt, 0, trampoline_buf, trampoline_slotbuf);
  8407. xtensa_insnbuf_to_chars (isa, trampoline_buf, (unsigned char *)tramp->fr_literal + offset, 3);
  8408. tramp->fr_fix += 3;
  8409. tramp->fr_var -= 3;
  8410. growth = 3;
  8411. /* add a fix-up for the trampoline jump. */
  8412. fixP = fix_new (tramp, tramp->fr_fix - 3, 3, tsym, toffset, TRUE, BFD_RELOC_XTENSA_SLOT0_OP);
  8413. /* Modify the jump at the start of this trampoline to point past the newly-added jump. */
  8414. fixP = trampP->fixP;
  8415. if (fixP)
  8416. fixP->fx_offset += 3;
  8417. /* Modify the original j to point here. */
  8418. origfrag->tc_frag_data.slot_symbols[0] = lsym;
  8419. origfrag->tc_frag_data.slot_offsets[0] = tramp->fr_fix - 3;
  8420. /* If trampoline is full, remove it from the list. */
  8421. check_and_update_trampolines ();
  8422. return growth;
  8423. }
  8424. static long
  8425. relax_frag_immed (segT segP,
  8426. fragS *fragP,
  8427. long stretch,
  8428. int min_steps,
  8429. xtensa_format fmt,
  8430. int slot,
  8431. int *stretched_p,
  8432. bfd_boolean estimate_only)
  8433. {
  8434. TInsn tinsn;
  8435. int old_size;
  8436. bfd_boolean negatable_branch = FALSE;
  8437. bfd_boolean branch_jmp_to_next = FALSE;
  8438. bfd_boolean from_wide_insn = FALSE;
  8439. xtensa_isa isa = xtensa_default_isa;
  8440. IStack istack;
  8441. offsetT frag_offset;
  8442. int num_steps;
  8443. int num_text_bytes, num_literal_bytes;
  8444. int literal_diff, total_text_diff, this_text_diff;
  8445. gas_assert (fragP->fr_opcode != NULL);
  8446. xg_clear_vinsn (&cur_vinsn);
  8447. vinsn_from_chars (&cur_vinsn, fragP->fr_opcode);
  8448. if (cur_vinsn.num_slots > 1)
  8449. from_wide_insn = TRUE;
  8450. tinsn = cur_vinsn.slots[slot];
  8451. tinsn_immed_from_frag (&tinsn, fragP, slot);
  8452. if (estimate_only && xtensa_opcode_is_loop (isa, tinsn.opcode) == 1)
  8453. return 0;
  8454. if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform)
  8455. branch_jmp_to_next = is_branch_jmp_to_next (&tinsn, fragP);
  8456. negatable_branch = (xtensa_opcode_is_branch (isa, tinsn.opcode) == 1);
  8457. old_size = xtensa_format_length (isa, fmt);
  8458. /* Special case: replace a branch to the next instruction with a NOP.
  8459. This is required to work around a hardware bug in T1040.0 and also
  8460. serves as an optimization. */
  8461. if (branch_jmp_to_next
  8462. && ((old_size == 2) || (old_size == 3))
  8463. && !next_frag_is_loop_target (fragP))
  8464. return 0;
  8465. /* Here is the fun stuff: Get the immediate field from this
  8466. instruction. If it fits, we are done. If not, find the next
  8467. instruction sequence that fits. */
  8468. frag_offset = fragP->fr_opcode - fragP->fr_literal;
  8469. istack_init (&istack);
  8470. num_steps = xg_assembly_relax (&istack, &tinsn, segP, fragP, frag_offset,
  8471. min_steps, stretch);
  8472. gas_assert (num_steps >= min_steps && num_steps <= RELAX_IMMED_MAXSTEPS);
  8473. fragP->tc_frag_data.slot_subtypes[slot] = (int) RELAX_IMMED + num_steps;
  8474. /* Figure out the number of bytes needed. */
  8475. num_literal_bytes = get_num_stack_literal_bytes (&istack);
  8476. literal_diff
  8477. = num_literal_bytes - fragP->tc_frag_data.literal_expansion[slot];
  8478. num_text_bytes = get_num_stack_text_bytes (&istack);
  8479. if (from_wide_insn)
  8480. {
  8481. int first = 0;
  8482. while (istack.insn[first].opcode == XTENSA_UNDEFINED)
  8483. first++;
  8484. num_text_bytes += old_size;
  8485. if (opcode_fits_format_slot (istack.insn[first].opcode, fmt, slot))
  8486. num_text_bytes -= xg_get_single_size (istack.insn[first].opcode);
  8487. else
  8488. {
  8489. /* The first instruction in the relaxed sequence will go after
  8490. the current wide instruction, and thus its symbolic immediates
  8491. might not fit. */
  8492. istack_init (&istack);
  8493. num_steps = xg_assembly_relax (&istack, &tinsn, segP, fragP,
  8494. frag_offset + old_size,
  8495. min_steps, stretch + old_size);
  8496. gas_assert (num_steps >= min_steps && num_steps <= RELAX_IMMED_MAXSTEPS);
  8497. fragP->tc_frag_data.slot_subtypes[slot]
  8498. = (int) RELAX_IMMED + num_steps;
  8499. num_literal_bytes = get_num_stack_literal_bytes (&istack);
  8500. literal_diff
  8501. = num_literal_bytes - fragP->tc_frag_data.literal_expansion[slot];
  8502. num_text_bytes = get_num_stack_text_bytes (&istack) + old_size;
  8503. }
  8504. }
  8505. total_text_diff = num_text_bytes - old_size;
  8506. this_text_diff = total_text_diff - fragP->tc_frag_data.text_expansion[slot];
  8507. /* It MUST get larger. If not, we could get an infinite loop. */
  8508. gas_assert (num_text_bytes >= 0);
  8509. gas_assert (literal_diff >= 0);
  8510. gas_assert (total_text_diff >= 0);
  8511. fragP->tc_frag_data.text_expansion[slot] = total_text_diff;
  8512. fragP->tc_frag_data.literal_expansion[slot] = num_literal_bytes;
  8513. gas_assert (fragP->tc_frag_data.text_expansion[slot] >= 0);
  8514. gas_assert (fragP->tc_frag_data.literal_expansion[slot] >= 0);
  8515. /* Find the associated expandable literal for this. */
  8516. if (literal_diff != 0)
  8517. {
  8518. fragS *lit_fragP = fragP->tc_frag_data.literal_frags[slot];
  8519. if (lit_fragP)
  8520. {
  8521. gas_assert (literal_diff == 4);
  8522. lit_fragP->tc_frag_data.unreported_expansion += literal_diff;
  8523. /* We expect that the literal section state has NOT been
  8524. modified yet. */
  8525. gas_assert (lit_fragP->fr_type == rs_machine_dependent
  8526. && lit_fragP->fr_subtype == RELAX_LITERAL);
  8527. lit_fragP->fr_subtype = RELAX_LITERAL_NR;
  8528. /* We need to mark this section for another iteration
  8529. of relaxation. */
  8530. (*stretched_p)++;
  8531. }
  8532. }
  8533. if (negatable_branch && istack.ninsn > 1)
  8534. update_next_frag_state (fragP);
  8535. /* If last insn is a jump, and it cannot reach its target, try to find a trampoline. */
  8536. if (istack.ninsn > 2 &&
  8537. istack.insn[istack.ninsn - 1].insn_type == ITYPE_LABEL &&
  8538. istack.insn[istack.ninsn - 2].insn_type == ITYPE_INSN &&
  8539. istack.insn[istack.ninsn - 2].opcode == xtensa_j_opcode)
  8540. {
  8541. TInsn *jinsn = &istack.insn[istack.ninsn - 2];
  8542. if (!xg_symbolic_immeds_fit (jinsn, segP, fragP, fragP->fr_offset, total_text_diff))
  8543. {
  8544. struct trampoline_frag *tf = get_best_trampoline (jinsn, fragP);
  8545. if (tf)
  8546. {
  8547. this_text_diff += init_trampoline_frag (tf);
  8548. this_text_diff += add_jump_to_trampoline (tf, fragP);
  8549. }
  8550. else
  8551. {
  8552. /* If target symbol is undefined, assume it will reach once linked. */
  8553. expressionS *exp = &istack.insn[istack.ninsn - 2].tok[0];
  8554. if (exp->X_op == O_symbol && S_IS_DEFINED (exp->X_add_symbol))
  8555. {
  8556. as_bad_where (fragP->fr_file, fragP->fr_line,
  8557. _("jump target out of range; no usable trampoline found"));
  8558. }
  8559. }
  8560. }
  8561. }
  8562. return this_text_diff;
  8563. }
  8564. /* md_convert_frag Hook and Helper Functions. */
  8565. static void convert_frag_align_next_opcode (fragS *);
  8566. static void convert_frag_narrow (segT, fragS *, xtensa_format, int);
  8567. static void convert_frag_fill_nop (fragS *);
  8568. static void convert_frag_immed (segT, fragS *, int, xtensa_format, int);
  8569. void
  8570. md_convert_frag (bfd *abfd ATTRIBUTE_UNUSED, segT sec, fragS *fragp)
  8571. {
  8572. static xtensa_insnbuf vbuf = NULL;
  8573. xtensa_isa isa = xtensa_default_isa;
  8574. int slot;
  8575. int num_slots;
  8576. xtensa_format fmt;
  8577. char *file_name;
  8578. unsigned line;
  8579. as_where (&file_name, &line);
  8580. new_logical_line (fragp->fr_file, fragp->fr_line);
  8581. switch (fragp->fr_subtype)
  8582. {
  8583. case RELAX_ALIGN_NEXT_OPCODE:
  8584. /* Always convert. */
  8585. convert_frag_align_next_opcode (fragp);
  8586. break;
  8587. case RELAX_DESIRE_ALIGN:
  8588. /* Do nothing. If not aligned already, too bad. */
  8589. break;
  8590. case RELAX_LITERAL:
  8591. case RELAX_LITERAL_FINAL:
  8592. break;
  8593. case RELAX_SLOTS:
  8594. if (vbuf == NULL)
  8595. vbuf = xtensa_insnbuf_alloc (isa);
  8596. xtensa_insnbuf_from_chars
  8597. (isa, vbuf, (unsigned char *) fragp->fr_opcode, 0);
  8598. fmt = xtensa_format_decode (isa, vbuf);
  8599. num_slots = xtensa_format_num_slots (isa, fmt);
  8600. for (slot = 0; slot < num_slots; slot++)
  8601. {
  8602. switch (fragp->tc_frag_data.slot_subtypes[slot])
  8603. {
  8604. case RELAX_NARROW:
  8605. convert_frag_narrow (sec, fragp, fmt, slot);
  8606. break;
  8607. case RELAX_IMMED:
  8608. case RELAX_IMMED_STEP1:
  8609. case RELAX_IMMED_STEP2:
  8610. case RELAX_IMMED_STEP3:
  8611. /* Place the immediate. */
  8612. convert_frag_immed
  8613. (sec, fragp,
  8614. fragp->tc_frag_data.slot_subtypes[slot] - RELAX_IMMED,
  8615. fmt, slot);
  8616. break;
  8617. default:
  8618. /* This is OK because some slots could have
  8619. relaxations and others have none. */
  8620. break;
  8621. }
  8622. }
  8623. break;
  8624. case RELAX_UNREACHABLE:
  8625. memset (&fragp->fr_literal[fragp->fr_fix], 0, fragp->fr_var);
  8626. fragp->fr_fix += fragp->tc_frag_data.text_expansion[0];
  8627. fragp->fr_var -= fragp->tc_frag_data.text_expansion[0];
  8628. frag_wane (fragp);
  8629. break;
  8630. case RELAX_MAYBE_UNREACHABLE:
  8631. case RELAX_MAYBE_DESIRE_ALIGN:
  8632. frag_wane (fragp);
  8633. break;
  8634. case RELAX_FILL_NOP:
  8635. convert_frag_fill_nop (fragp);
  8636. break;
  8637. case RELAX_LITERAL_NR:
  8638. if (use_literal_section)
  8639. {
  8640. /* This should have been handled during relaxation. When
  8641. relaxing a code segment, literals sometimes need to be
  8642. added to the corresponding literal segment. If that
  8643. literal segment has already been relaxed, then we end up
  8644. in this situation. Marking the literal segments as data
  8645. would make this happen less often (since GAS always relaxes
  8646. code before data), but we could still get into trouble if
  8647. there are instructions in a segment that is not marked as
  8648. containing code. Until we can implement a better solution,
  8649. cheat and adjust the addresses of all the following frags.
  8650. This could break subsequent alignments, but the linker's
  8651. literal coalescing will do that anyway. */
  8652. fragS *f;
  8653. fragp->fr_subtype = RELAX_LITERAL_FINAL;
  8654. gas_assert (fragp->tc_frag_data.unreported_expansion == 4);
  8655. memset (&fragp->fr_literal[fragp->fr_fix], 0, 4);
  8656. fragp->fr_var -= 4;
  8657. fragp->fr_fix += 4;
  8658. for (f = fragp->fr_next; f; f = f->fr_next)
  8659. f->fr_address += 4;
  8660. }
  8661. else
  8662. as_bad (_("invalid relaxation fragment result"));
  8663. break;
  8664. case RELAX_TRAMPOLINE:
  8665. break;
  8666. }
  8667. fragp->fr_var = 0;
  8668. new_logical_line (file_name, line);
  8669. }
  8670. static void
  8671. convert_frag_align_next_opcode (fragS *fragp)
  8672. {
  8673. char *nop_buf; /* Location for Writing. */
  8674. bfd_boolean use_no_density = fragp->tc_frag_data.is_no_density;
  8675. addressT aligned_address;
  8676. offsetT fill_size;
  8677. int nop, nop_count;
  8678. aligned_address = get_noop_aligned_address (fragp, fragp->fr_address +
  8679. fragp->fr_fix);
  8680. fill_size = aligned_address - (fragp->fr_address + fragp->fr_fix);
  8681. nop_count = get_text_align_nop_count (fill_size, use_no_density);
  8682. nop_buf = fragp->fr_literal + fragp->fr_fix;
  8683. for (nop = 0; nop < nop_count; nop++)
  8684. {
  8685. int nop_size;
  8686. nop_size = get_text_align_nth_nop_size (fill_size, nop, use_no_density);
  8687. assemble_nop (nop_size, nop_buf);
  8688. nop_buf += nop_size;
  8689. }
  8690. fragp->fr_fix += fill_size;
  8691. fragp->fr_var -= fill_size;
  8692. }
  8693. static void
  8694. convert_frag_narrow (segT segP, fragS *fragP, xtensa_format fmt, int slot)
  8695. {
  8696. TInsn tinsn, single_target;
  8697. int size, old_size, diff;
  8698. offsetT frag_offset;
  8699. gas_assert (slot == 0);
  8700. tinsn_from_chars (&tinsn, fragP->fr_opcode, 0);
  8701. if (fragP->tc_frag_data.is_aligning_branch == 1)
  8702. {
  8703. gas_assert (fragP->tc_frag_data.text_expansion[0] == 1
  8704. || fragP->tc_frag_data.text_expansion[0] == 0);
  8705. convert_frag_immed (segP, fragP, fragP->tc_frag_data.text_expansion[0],
  8706. fmt, slot);
  8707. return;
  8708. }
  8709. if (fragP->tc_frag_data.text_expansion[0] == 0)
  8710. {
  8711. /* No conversion. */
  8712. fragP->fr_var = 0;
  8713. return;
  8714. }
  8715. gas_assert (fragP->fr_opcode != NULL);
  8716. /* Frags in this relaxation state should only contain
  8717. single instruction bundles. */
  8718. tinsn_immed_from_frag (&tinsn, fragP, 0);
  8719. /* Just convert it to a wide form.... */
  8720. size = 0;
  8721. old_size = xg_get_single_size (tinsn.opcode);
  8722. tinsn_init (&single_target);
  8723. frag_offset = fragP->fr_opcode - fragP->fr_literal;
  8724. if (! xg_is_single_relaxable_insn (&tinsn, &single_target, FALSE))
  8725. {
  8726. as_bad (_("unable to widen instruction"));
  8727. return;
  8728. }
  8729. size = xg_get_single_size (single_target.opcode);
  8730. xg_emit_insn_to_buf (&single_target, fragP->fr_opcode, fragP,
  8731. frag_offset, TRUE);
  8732. diff = size - old_size;
  8733. gas_assert (diff >= 0);
  8734. gas_assert (diff <= fragP->fr_var);
  8735. fragP->fr_var -= diff;
  8736. fragP->fr_fix += diff;
  8737. /* clean it up */
  8738. fragP->fr_var = 0;
  8739. }
  8740. static void
  8741. convert_frag_fill_nop (fragS *fragP)
  8742. {
  8743. char *loc = &fragP->fr_literal[fragP->fr_fix];
  8744. int size = fragP->tc_frag_data.text_expansion[0];
  8745. gas_assert ((unsigned) size == (fragP->fr_next->fr_address
  8746. - fragP->fr_address - fragP->fr_fix));
  8747. if (size == 0)
  8748. {
  8749. /* No conversion. */
  8750. fragP->fr_var = 0;
  8751. return;
  8752. }
  8753. assemble_nop (size, loc);
  8754. fragP->tc_frag_data.is_insn = TRUE;
  8755. fragP->fr_var -= size;
  8756. fragP->fr_fix += size;
  8757. frag_wane (fragP);
  8758. }
  8759. static fixS *fix_new_exp_in_seg
  8760. (segT, subsegT, fragS *, int, int, expressionS *, int,
  8761. bfd_reloc_code_real_type);
  8762. static void convert_frag_immed_finish_loop (segT, fragS *, TInsn *);
  8763. static void
  8764. convert_frag_immed (segT segP,
  8765. fragS *fragP,
  8766. int min_steps,
  8767. xtensa_format fmt,
  8768. int slot)
  8769. {
  8770. char *immed_instr = fragP->fr_opcode;
  8771. TInsn orig_tinsn;
  8772. bfd_boolean expanded = FALSE;
  8773. bfd_boolean branch_jmp_to_next = FALSE;
  8774. char *fr_opcode = fragP->fr_opcode;
  8775. xtensa_isa isa = xtensa_default_isa;
  8776. bfd_boolean from_wide_insn = FALSE;
  8777. int bytes;
  8778. bfd_boolean is_loop;
  8779. gas_assert (fr_opcode != NULL);
  8780. xg_clear_vinsn (&cur_vinsn);
  8781. vinsn_from_chars (&cur_vinsn, fr_opcode);
  8782. if (cur_vinsn.num_slots > 1)
  8783. from_wide_insn = TRUE;
  8784. orig_tinsn = cur_vinsn.slots[slot];
  8785. tinsn_immed_from_frag (&orig_tinsn, fragP, slot);
  8786. is_loop = xtensa_opcode_is_loop (xtensa_default_isa, orig_tinsn.opcode) == 1;
  8787. if (workaround_b_j_loop_end && ! fragP->tc_frag_data.is_no_transform)
  8788. branch_jmp_to_next = is_branch_jmp_to_next (&orig_tinsn, fragP);
  8789. if (branch_jmp_to_next && !next_frag_is_loop_target (fragP))
  8790. {
  8791. /* Conversion just inserts a NOP and marks the fix as completed. */
  8792. bytes = xtensa_format_length (isa, fmt);
  8793. if (bytes >= 4)
  8794. {
  8795. cur_vinsn.slots[slot].opcode =
  8796. xtensa_format_slot_nop_opcode (isa, cur_vinsn.format, slot);
  8797. cur_vinsn.slots[slot].ntok = 0;
  8798. }
  8799. else
  8800. {
  8801. bytes += fragP->tc_frag_data.text_expansion[0];
  8802. gas_assert (bytes == 2 || bytes == 3);
  8803. build_nop (&cur_vinsn.slots[0], bytes);
  8804. fragP->fr_fix += fragP->tc_frag_data.text_expansion[0];
  8805. }
  8806. vinsn_to_insnbuf (&cur_vinsn, fr_opcode, frag_now, TRUE);
  8807. xtensa_insnbuf_to_chars
  8808. (isa, cur_vinsn.insnbuf, (unsigned char *) fr_opcode, 0);
  8809. fragP->fr_var = 0;
  8810. }
  8811. else
  8812. {
  8813. /* Here is the fun stuff: Get the immediate field from this
  8814. instruction. If it fits, we're done. If not, find the next
  8815. instruction sequence that fits. */
  8816. IStack istack;
  8817. int i;
  8818. symbolS *lit_sym = NULL;
  8819. int total_size = 0;
  8820. int target_offset = 0;
  8821. int old_size;
  8822. int diff;
  8823. symbolS *gen_label = NULL;
  8824. offsetT frag_offset;
  8825. bfd_boolean first = TRUE;
  8826. /* It does not fit. Find something that does and
  8827. convert immediately. */
  8828. frag_offset = fr_opcode - fragP->fr_literal;
  8829. istack_init (&istack);
  8830. xg_assembly_relax (&istack, &orig_tinsn,
  8831. segP, fragP, frag_offset, min_steps, 0);
  8832. old_size = xtensa_format_length (isa, fmt);
  8833. /* Assemble this right inline. */
  8834. /* First, create the mapping from a label name to the REAL label. */
  8835. target_offset = 0;
  8836. for (i = 0; i < istack.ninsn; i++)
  8837. {
  8838. TInsn *tinsn = &istack.insn[i];
  8839. fragS *lit_frag;
  8840. switch (tinsn->insn_type)
  8841. {
  8842. case ITYPE_LITERAL:
  8843. if (lit_sym != NULL)
  8844. as_bad (_("multiple literals in expansion"));
  8845. /* First find the appropriate space in the literal pool. */
  8846. lit_frag = fragP->tc_frag_data.literal_frags[slot];
  8847. if (lit_frag == NULL)
  8848. as_bad (_("no registered fragment for literal"));
  8849. if (tinsn->ntok != 1)
  8850. as_bad (_("number of literal tokens != 1"));
  8851. /* Set the literal symbol and add a fixup. */
  8852. lit_sym = lit_frag->fr_symbol;
  8853. break;
  8854. case ITYPE_LABEL:
  8855. if (align_targets && !is_loop)
  8856. {
  8857. fragS *unreach = fragP->fr_next;
  8858. while (!(unreach->fr_type == rs_machine_dependent
  8859. && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE
  8860. || unreach->fr_subtype == RELAX_UNREACHABLE)))
  8861. {
  8862. unreach = unreach->fr_next;
  8863. }
  8864. gas_assert (unreach->fr_type == rs_machine_dependent
  8865. && (unreach->fr_subtype == RELAX_MAYBE_UNREACHABLE
  8866. || unreach->fr_subtype == RELAX_UNREACHABLE));
  8867. target_offset += unreach->tc_frag_data.text_expansion[0];
  8868. }
  8869. gas_assert (gen_label == NULL);
  8870. gen_label = symbol_new (FAKE_LABEL_NAME, now_seg,
  8871. fr_opcode - fragP->fr_literal
  8872. + target_offset, fragP);
  8873. break;
  8874. case ITYPE_INSN:
  8875. if (first && from_wide_insn)
  8876. {
  8877. target_offset += xtensa_format_length (isa, fmt);
  8878. first = FALSE;
  8879. if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot))
  8880. target_offset += xg_get_single_size (tinsn->opcode);
  8881. }
  8882. else
  8883. target_offset += xg_get_single_size (tinsn->opcode);
  8884. break;
  8885. }
  8886. }
  8887. total_size = 0;
  8888. first = TRUE;
  8889. for (i = 0; i < istack.ninsn; i++)
  8890. {
  8891. TInsn *tinsn = &istack.insn[i];
  8892. fragS *lit_frag;
  8893. int size;
  8894. segT target_seg;
  8895. bfd_reloc_code_real_type reloc_type;
  8896. switch (tinsn->insn_type)
  8897. {
  8898. case ITYPE_LITERAL:
  8899. lit_frag = fragP->tc_frag_data.literal_frags[slot];
  8900. /* Already checked. */
  8901. gas_assert (lit_frag != NULL);
  8902. gas_assert (lit_sym != NULL);
  8903. gas_assert (tinsn->ntok == 1);
  8904. /* Add a fixup. */
  8905. target_seg = S_GET_SEGMENT (lit_sym);
  8906. gas_assert (target_seg);
  8907. reloc_type = map_operator_to_reloc (tinsn->tok[0].X_op, TRUE);
  8908. fix_new_exp_in_seg (target_seg, 0, lit_frag, 0, 4,
  8909. &tinsn->tok[0], FALSE, reloc_type);
  8910. break;
  8911. case ITYPE_LABEL:
  8912. break;
  8913. case ITYPE_INSN:
  8914. xg_resolve_labels (tinsn, gen_label);
  8915. xg_resolve_literals (tinsn, lit_sym);
  8916. if (from_wide_insn && first)
  8917. {
  8918. first = FALSE;
  8919. if (opcode_fits_format_slot (tinsn->opcode, fmt, slot))
  8920. {
  8921. cur_vinsn.slots[slot] = *tinsn;
  8922. }
  8923. else
  8924. {
  8925. cur_vinsn.slots[slot].opcode =
  8926. xtensa_format_slot_nop_opcode (isa, fmt, slot);
  8927. cur_vinsn.slots[slot].ntok = 0;
  8928. }
  8929. vinsn_to_insnbuf (&cur_vinsn, immed_instr, fragP, TRUE);
  8930. xtensa_insnbuf_to_chars (isa, cur_vinsn.insnbuf,
  8931. (unsigned char *) immed_instr, 0);
  8932. fragP->tc_frag_data.is_insn = TRUE;
  8933. size = xtensa_format_length (isa, fmt);
  8934. if (!opcode_fits_format_slot (tinsn->opcode, fmt, slot))
  8935. {
  8936. xg_emit_insn_to_buf
  8937. (tinsn, immed_instr + size, fragP,
  8938. immed_instr - fragP->fr_literal + size, TRUE);
  8939. size += xg_get_single_size (tinsn->opcode);
  8940. }
  8941. }
  8942. else
  8943. {
  8944. size = xg_get_single_size (tinsn->opcode);
  8945. xg_emit_insn_to_buf (tinsn, immed_instr, fragP,
  8946. immed_instr - fragP->fr_literal, TRUE);
  8947. }
  8948. immed_instr += size;
  8949. total_size += size;
  8950. break;
  8951. }
  8952. }
  8953. diff = total_size - old_size;
  8954. gas_assert (diff >= 0);
  8955. if (diff != 0)
  8956. expanded = TRUE;
  8957. gas_assert (diff <= fragP->fr_var);
  8958. fragP->fr_var -= diff;
  8959. fragP->fr_fix += diff;
  8960. }
  8961. /* Check for undefined immediates in LOOP instructions. */
  8962. if (is_loop)
  8963. {
  8964. symbolS *sym;
  8965. sym = orig_tinsn.tok[1].X_add_symbol;
  8966. if (sym != NULL && !S_IS_DEFINED (sym))
  8967. {
  8968. as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
  8969. return;
  8970. }
  8971. sym = orig_tinsn.tok[1].X_op_symbol;
  8972. if (sym != NULL && !S_IS_DEFINED (sym))
  8973. {
  8974. as_bad (_("unresolved loop target symbol: %s"), S_GET_NAME (sym));
  8975. return;
  8976. }
  8977. }
  8978. if (expanded && xtensa_opcode_is_loop (isa, orig_tinsn.opcode) == 1)
  8979. convert_frag_immed_finish_loop (segP, fragP, &orig_tinsn);
  8980. if (expanded && is_direct_call_opcode (orig_tinsn.opcode))
  8981. {
  8982. /* Add an expansion note on the expanded instruction. */
  8983. fix_new_exp_in_seg (now_seg, 0, fragP, fr_opcode - fragP->fr_literal, 4,
  8984. &orig_tinsn.tok[0], TRUE,
  8985. BFD_RELOC_XTENSA_ASM_EXPAND);
  8986. }
  8987. }
  8988. /* Add a new fix expression into the desired segment. We have to
  8989. switch to that segment to do this. */
  8990. static fixS *
  8991. fix_new_exp_in_seg (segT new_seg,
  8992. subsegT new_subseg,
  8993. fragS *frag,
  8994. int where,
  8995. int size,
  8996. expressionS *exp,
  8997. int pcrel,
  8998. bfd_reloc_code_real_type r_type)
  8999. {
  9000. fixS *new_fix;
  9001. segT seg = now_seg;
  9002. subsegT subseg = now_subseg;
  9003. gas_assert (new_seg != 0);
  9004. subseg_set (new_seg, new_subseg);
  9005. new_fix = fix_new_exp (frag, where, size, exp, pcrel, r_type);
  9006. subseg_set (seg, subseg);
  9007. return new_fix;
  9008. }
  9009. /* Relax a loop instruction so that it can span loop >256 bytes.
  9010. loop as, .L1
  9011. .L0:
  9012. rsr as, LEND
  9013. wsr as, LBEG
  9014. addi as, as, lo8 (label-.L1)
  9015. addmi as, as, mid8 (label-.L1)
  9016. wsr as, LEND
  9017. isync
  9018. rsr as, LCOUNT
  9019. addi as, as, 1
  9020. .L1:
  9021. <<body>>
  9022. label:
  9023. */
  9024. static void
  9025. convert_frag_immed_finish_loop (segT segP, fragS *fragP, TInsn *tinsn)
  9026. {
  9027. TInsn loop_insn;
  9028. TInsn addi_insn;
  9029. TInsn addmi_insn;
  9030. unsigned long target;
  9031. static xtensa_insnbuf insnbuf = NULL;
  9032. unsigned int loop_length, loop_length_hi, loop_length_lo;
  9033. xtensa_isa isa = xtensa_default_isa;
  9034. addressT loop_offset;
  9035. addressT addi_offset = 9;
  9036. addressT addmi_offset = 12;
  9037. fragS *next_fragP;
  9038. int target_count;
  9039. if (!insnbuf)
  9040. insnbuf = xtensa_insnbuf_alloc (isa);
  9041. /* Get the loop offset. */
  9042. loop_offset = get_expanded_loop_offset (tinsn->opcode);
  9043. /* Validate that there really is a LOOP at the loop_offset. Because
  9044. loops are not bundleable, we can assume that the instruction will be
  9045. in slot 0. */
  9046. tinsn_from_chars (&loop_insn, fragP->fr_opcode + loop_offset, 0);
  9047. tinsn_immed_from_frag (&loop_insn, fragP, 0);
  9048. gas_assert (xtensa_opcode_is_loop (isa, loop_insn.opcode) == 1);
  9049. addi_offset += loop_offset;
  9050. addmi_offset += loop_offset;
  9051. gas_assert (tinsn->ntok == 2);
  9052. if (tinsn->tok[1].X_op == O_constant)
  9053. target = tinsn->tok[1].X_add_number;
  9054. else if (tinsn->tok[1].X_op == O_symbol)
  9055. {
  9056. /* Find the fragment. */
  9057. symbolS *sym = tinsn->tok[1].X_add_symbol;
  9058. gas_assert (S_GET_SEGMENT (sym) == segP
  9059. || S_GET_SEGMENT (sym) == absolute_section);
  9060. target = (S_GET_VALUE (sym) + tinsn->tok[1].X_add_number);
  9061. }
  9062. else
  9063. {
  9064. as_bad (_("invalid expression evaluation type %d"), tinsn->tok[1].X_op);
  9065. target = 0;
  9066. }
  9067. loop_length = target - (fragP->fr_address + fragP->fr_fix);
  9068. loop_length_hi = loop_length & ~0x0ff;
  9069. loop_length_lo = loop_length & 0x0ff;
  9070. if (loop_length_lo >= 128)
  9071. {
  9072. loop_length_lo -= 256;
  9073. loop_length_hi += 256;
  9074. }
  9075. /* Because addmi sign-extends the immediate, 'loop_length_hi' can be at most
  9076. 32512. If the loop is larger than that, then we just fail. */
  9077. if (loop_length_hi > 32512)
  9078. as_bad_where (fragP->fr_file, fragP->fr_line,
  9079. _("loop too long for LOOP instruction"));
  9080. tinsn_from_chars (&addi_insn, fragP->fr_opcode + addi_offset, 0);
  9081. gas_assert (addi_insn.opcode == xtensa_addi_opcode);
  9082. tinsn_from_chars (&addmi_insn, fragP->fr_opcode + addmi_offset, 0);
  9083. gas_assert (addmi_insn.opcode == xtensa_addmi_opcode);
  9084. set_expr_const (&addi_insn.tok[2], loop_length_lo);
  9085. tinsn_to_insnbuf (&addi_insn, insnbuf);
  9086. fragP->tc_frag_data.is_insn = TRUE;
  9087. xtensa_insnbuf_to_chars
  9088. (isa, insnbuf, (unsigned char *) fragP->fr_opcode + addi_offset, 0);
  9089. set_expr_const (&addmi_insn.tok[2], loop_length_hi);
  9090. tinsn_to_insnbuf (&addmi_insn, insnbuf);
  9091. xtensa_insnbuf_to_chars
  9092. (isa, insnbuf, (unsigned char *) fragP->fr_opcode + addmi_offset, 0);
  9093. /* Walk through all of the frags from here to the loop end
  9094. and mark them as no_transform to keep them from being modified
  9095. by the linker. If we ever have a relocation for the
  9096. addi/addmi of the difference of two symbols we can remove this. */
  9097. target_count = 0;
  9098. for (next_fragP = fragP; next_fragP != NULL;
  9099. next_fragP = next_fragP->fr_next)
  9100. {
  9101. next_fragP->tc_frag_data.is_no_transform = TRUE;
  9102. if (next_fragP->tc_frag_data.is_loop_target)
  9103. target_count++;
  9104. if (target_count == 2)
  9105. break;
  9106. }
  9107. }
  9108. /* A map that keeps information on a per-subsegment basis. This is
  9109. maintained during initial assembly, but is invalid once the
  9110. subsegments are smashed together. I.E., it cannot be used during
  9111. the relaxation. */
  9112. typedef struct subseg_map_struct
  9113. {
  9114. /* the key */
  9115. segT seg;
  9116. subsegT subseg;
  9117. /* the data */
  9118. unsigned flags;
  9119. float total_freq; /* fall-through + branch target frequency */
  9120. float target_freq; /* branch target frequency alone */
  9121. struct subseg_map_struct *next;
  9122. } subseg_map;
  9123. static subseg_map *sseg_map = NULL;
  9124. static subseg_map *
  9125. get_subseg_info (segT seg, subsegT subseg)
  9126. {
  9127. subseg_map *subseg_e;
  9128. for (subseg_e = sseg_map; subseg_e; subseg_e = subseg_e->next)
  9129. {
  9130. if (seg == subseg_e->seg && subseg == subseg_e->subseg)
  9131. break;
  9132. }
  9133. return subseg_e;
  9134. }
  9135. static subseg_map *
  9136. add_subseg_info (segT seg, subsegT subseg)
  9137. {
  9138. subseg_map *subseg_e = (subseg_map *) xmalloc (sizeof (subseg_map));
  9139. memset (subseg_e, 0, sizeof (subseg_map));
  9140. subseg_e->seg = seg;
  9141. subseg_e->subseg = subseg;
  9142. subseg_e->flags = 0;
  9143. /* Start off considering every branch target very important. */
  9144. subseg_e->target_freq = 1.0;
  9145. subseg_e->total_freq = 1.0;
  9146. subseg_e->next = sseg_map;
  9147. sseg_map = subseg_e;
  9148. return subseg_e;
  9149. }
  9150. static unsigned
  9151. get_last_insn_flags (segT seg, subsegT subseg)
  9152. {
  9153. subseg_map *subseg_e = get_subseg_info (seg, subseg);
  9154. if (subseg_e)
  9155. return subseg_e->flags;
  9156. return 0;
  9157. }
  9158. static void
  9159. set_last_insn_flags (segT seg,
  9160. subsegT subseg,
  9161. unsigned fl,
  9162. bfd_boolean val)
  9163. {
  9164. subseg_map *subseg_e = get_subseg_info (seg, subseg);
  9165. if (! subseg_e)
  9166. subseg_e = add_subseg_info (seg, subseg);
  9167. if (val)
  9168. subseg_e->flags |= fl;
  9169. else
  9170. subseg_e->flags &= ~fl;
  9171. }
  9172. static float
  9173. get_subseg_total_freq (segT seg, subsegT subseg)
  9174. {
  9175. subseg_map *subseg_e = get_subseg_info (seg, subseg);
  9176. if (subseg_e)
  9177. return subseg_e->total_freq;
  9178. return 1.0;
  9179. }
  9180. static float
  9181. get_subseg_target_freq (segT seg, subsegT subseg)
  9182. {
  9183. subseg_map *subseg_e = get_subseg_info (seg, subseg);
  9184. if (subseg_e)
  9185. return subseg_e->target_freq;
  9186. return 1.0;
  9187. }
  9188. static void
  9189. set_subseg_freq (segT seg, subsegT subseg, float total_f, float target_f)
  9190. {
  9191. subseg_map *subseg_e = get_subseg_info (seg, subseg);
  9192. if (! subseg_e)
  9193. subseg_e = add_subseg_info (seg, subseg);
  9194. subseg_e->total_freq = total_f;
  9195. subseg_e->target_freq = target_f;
  9196. }
  9197. /* Segment Lists and emit_state Stuff. */
  9198. static void
  9199. xtensa_move_seg_list_to_beginning (seg_list *head)
  9200. {
  9201. head = head->next;
  9202. while (head)
  9203. {
  9204. segT literal_section = head->seg;
  9205. /* Move the literal section to the front of the section list. */
  9206. gas_assert (literal_section);
  9207. if (literal_section != stdoutput->sections)
  9208. {
  9209. bfd_section_list_remove (stdoutput, literal_section);
  9210. bfd_section_list_prepend (stdoutput, literal_section);
  9211. }
  9212. head = head->next;
  9213. }
  9214. }
  9215. static void mark_literal_frags (seg_list *);
  9216. static void
  9217. xtensa_move_literals (void)
  9218. {
  9219. seg_list *segment;
  9220. frchainS *frchain_from, *frchain_to;
  9221. fragS *search_frag, *next_frag, *literal_pool, *insert_after;
  9222. fragS **frag_splice;
  9223. emit_state state;
  9224. segT dest_seg;
  9225. fixS *fix, *next_fix, **fix_splice;
  9226. sym_list *lit;
  9227. struct litpool_seg *lps;
  9228. mark_literal_frags (literal_head->next);
  9229. if (use_literal_section)
  9230. return;
  9231. /* Assign addresses (rough estimates) to the potential literal pool locations
  9232. and create new ones if the gaps are too large. */
  9233. for (lps = litpool_seg_list.next; lps; lps = lps->next)
  9234. {
  9235. frchainS *frchP = seg_info (lps->seg)->frchainP;
  9236. struct litpool_frag *lpf = lps->frag_list.next;
  9237. addressT addr = 0;
  9238. for ( ; frchP; frchP = frchP->frch_next)
  9239. {
  9240. fragS *fragP;
  9241. for (fragP = frchP->frch_root; fragP; fragP = fragP->fr_next)
  9242. {
  9243. if (lpf && fragP == lpf->fragP)
  9244. {
  9245. gas_assert(fragP->fr_type == rs_machine_dependent &&
  9246. (fragP->fr_subtype == RELAX_LITERAL_POOL_BEGIN ||
  9247. fragP->fr_subtype == RELAX_LITERAL_POOL_CANDIDATE_BEGIN));
  9248. /* Found a litpool location. */
  9249. lpf->addr = addr;
  9250. lpf = lpf->next;
  9251. }
  9252. if (fragP->fr_type == rs_machine_dependent &&
  9253. fragP->fr_subtype == RELAX_SLOTS)
  9254. {
  9255. int slot;
  9256. for (slot = 0; slot < MAX_SLOTS; slot++)
  9257. {
  9258. if (fragP->tc_frag_data.literal_frags[slot])
  9259. {
  9260. /* L32R; point its literal to the nearest litpool
  9261. preferring non-"candidate" positions to avoid
  9262. the jump-around. */
  9263. fragS *litfrag = fragP->tc_frag_data.literal_frags[slot];
  9264. struct litpool_frag *lp = lpf->prev;
  9265. if (!lp->fragP)
  9266. {
  9267. break;
  9268. }
  9269. while (lp->fragP->fr_subtype ==
  9270. RELAX_LITERAL_POOL_CANDIDATE_BEGIN)
  9271. {
  9272. lp = lp->prev;
  9273. if (lp->fragP == NULL)
  9274. {
  9275. /* End of list; have to bite the bullet.
  9276. Take the nearest. */
  9277. lp = lpf->prev;
  9278. break;
  9279. }
  9280. /* Does it (conservatively) reach? */
  9281. if (addr - lp->addr <= 128 * 1024)
  9282. {
  9283. if (lp->fragP->fr_subtype == RELAX_LITERAL_POOL_BEGIN)
  9284. {
  9285. /* Found a good one. */
  9286. break;
  9287. }
  9288. else if (lp->prev->fragP &&
  9289. addr - lp->prev->addr > 128 * 1024)
  9290. {
  9291. /* This is still a "candidate" but the next one
  9292. will be too far away, so revert to the nearest
  9293. one, convert it and add the jump around. */
  9294. fragS *poolbeg;
  9295. fragS *poolend;
  9296. symbolS *lsym;
  9297. char label[10 + 2 * sizeof (fragS *)];
  9298. lp = lpf->prev;
  9299. poolbeg = lp->fragP;
  9300. lp->priority = 1;
  9301. poolbeg->fr_subtype = RELAX_LITERAL_POOL_BEGIN;
  9302. poolend = poolbeg->fr_next;
  9303. gas_assert (poolend->fr_type == rs_machine_dependent &&
  9304. poolend->fr_subtype == RELAX_LITERAL_POOL_END);
  9305. /* Create a local symbol pointing to the
  9306. end of the pool. */
  9307. sprintf (label, ".L0_LT_%p", poolbeg);
  9308. lsym = (symbolS *)local_symbol_make (label, lps->seg,
  9309. 0, poolend);
  9310. poolbeg->fr_symbol = lsym;
  9311. /* Rest is done in xtensa_relax_frag. */
  9312. }
  9313. }
  9314. }
  9315. if (! litfrag->tc_frag_data.literal_frag)
  9316. {
  9317. /* Take earliest use of this literal to avoid
  9318. forward refs. */
  9319. litfrag->tc_frag_data.literal_frag = lp->fragP;
  9320. }
  9321. }
  9322. }
  9323. }
  9324. addr += fragP->fr_fix;
  9325. if (fragP->fr_type == rs_fill)
  9326. addr += fragP->fr_offset;
  9327. }
  9328. }
  9329. }
  9330. for (segment = literal_head->next; segment; segment = segment->next)
  9331. {
  9332. /* Keep the literals for .init and .fini in separate sections. */
  9333. if (!strcmp (segment_name (segment->seg), INIT_SECTION_NAME)
  9334. || !strcmp (segment_name (segment->seg), FINI_SECTION_NAME))
  9335. continue;
  9336. frchain_from = seg_info (segment->seg)->frchainP;
  9337. search_frag = frchain_from->frch_root;
  9338. literal_pool = NULL;
  9339. frchain_to = NULL;
  9340. frag_splice = &(frchain_from->frch_root);
  9341. while (search_frag && !search_frag->tc_frag_data.literal_frag)
  9342. {
  9343. gas_assert (search_frag->fr_fix == 0
  9344. || search_frag->fr_type == rs_align);
  9345. search_frag = search_frag->fr_next;
  9346. }
  9347. if (!search_frag)
  9348. {
  9349. search_frag = frchain_from->frch_root;
  9350. as_bad_where (search_frag->fr_file, search_frag->fr_line,
  9351. _("literal pool location required for text-section-literals; specify with .literal_position"));
  9352. continue;
  9353. }
  9354. gas_assert (search_frag->tc_frag_data.literal_frag->fr_subtype
  9355. == RELAX_LITERAL_POOL_BEGIN);
  9356. xtensa_switch_section_emit_state (&state, segment->seg, 0);
  9357. /* Make sure that all the frags in this series are closed, and
  9358. that there is at least one left over of zero-size. This
  9359. prevents us from making a segment with an frchain without any
  9360. frags in it. */
  9361. frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
  9362. xtensa_set_frag_assembly_state (frag_now);
  9363. frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
  9364. xtensa_set_frag_assembly_state (frag_now);
  9365. while (search_frag != frag_now)
  9366. {
  9367. next_frag = search_frag->fr_next;
  9368. if (search_frag->tc_frag_data.literal_frag)
  9369. {
  9370. literal_pool = search_frag->tc_frag_data.literal_frag;
  9371. gas_assert (literal_pool->fr_subtype == RELAX_LITERAL_POOL_BEGIN);
  9372. frchain_to = literal_pool->tc_frag_data.lit_frchain;
  9373. gas_assert (frchain_to);
  9374. }
  9375. if (search_frag->fr_type == rs_fill && search_frag->fr_fix == 0)
  9376. {
  9377. /* Skip empty fill frags. */
  9378. *frag_splice = next_frag;
  9379. search_frag = next_frag;
  9380. continue;
  9381. }
  9382. if (search_frag->fr_type == rs_align)
  9383. {
  9384. /* Skip alignment frags, because the pool as a whole will be
  9385. aligned if used, and we don't want to force alignment if the
  9386. pool is unused. */
  9387. *frag_splice = next_frag;
  9388. search_frag = next_frag;
  9389. continue;
  9390. }
  9391. /* First, move the frag out of the literal section and
  9392. to the appropriate place. */
  9393. /* Insert an aligmnent frag at start of pool. */
  9394. if (literal_pool->fr_next->fr_type == rs_machine_dependent &&
  9395. literal_pool->fr_next->fr_subtype == RELAX_LITERAL_POOL_END)
  9396. {
  9397. segT pool_seg = literal_pool->fr_next->tc_frag_data.lit_seg;
  9398. emit_state prev_state;
  9399. fragS *prev_frag;
  9400. fragS *align_frag;
  9401. xtensa_switch_section_emit_state (&prev_state, pool_seg, 0);
  9402. prev_frag = frag_now;
  9403. frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
  9404. align_frag = frag_now;
  9405. frag_align (2, 0, 0);
  9406. /* Splice it into the right place. */
  9407. prev_frag->fr_next = align_frag->fr_next;
  9408. align_frag->fr_next = literal_pool->fr_next;
  9409. literal_pool->fr_next = align_frag;
  9410. /* Insert after this one. */
  9411. literal_pool->tc_frag_data.literal_frag = align_frag;
  9412. xtensa_restore_emit_state (&prev_state);
  9413. }
  9414. insert_after = literal_pool->tc_frag_data.literal_frag;
  9415. dest_seg = insert_after->fr_next->tc_frag_data.lit_seg;
  9416. /* Skip align frag. */
  9417. if (insert_after->fr_next->fr_type == rs_align)
  9418. {
  9419. insert_after = insert_after->fr_next;
  9420. }
  9421. *frag_splice = next_frag;
  9422. search_frag->fr_next = insert_after->fr_next;
  9423. insert_after->fr_next = search_frag;
  9424. search_frag->tc_frag_data.lit_seg = dest_seg;
  9425. literal_pool->tc_frag_data.literal_frag = search_frag;
  9426. /* Now move any fixups associated with this frag to the
  9427. right section. */
  9428. fix = frchain_from->fix_root;
  9429. fix_splice = &(frchain_from->fix_root);
  9430. while (fix)
  9431. {
  9432. next_fix = fix->fx_next;
  9433. if (fix->fx_frag == search_frag)
  9434. {
  9435. *fix_splice = next_fix;
  9436. fix->fx_next = frchain_to->fix_root;
  9437. frchain_to->fix_root = fix;
  9438. if (frchain_to->fix_tail == NULL)
  9439. frchain_to->fix_tail = fix;
  9440. }
  9441. else
  9442. fix_splice = &(fix->fx_next);
  9443. fix = next_fix;
  9444. }
  9445. search_frag = next_frag;
  9446. }
  9447. if (frchain_from->fix_root != NULL)
  9448. {
  9449. frchain_from = seg_info (segment->seg)->frchainP;
  9450. as_warn (_("fixes not all moved from %s"), segment->seg->name);
  9451. gas_assert (frchain_from->fix_root == NULL);
  9452. }
  9453. frchain_from->fix_tail = NULL;
  9454. xtensa_restore_emit_state (&state);
  9455. }
  9456. /* Now fix up the SEGMENT value for all the literal symbols. */
  9457. for (lit = literal_syms; lit; lit = lit->next)
  9458. {
  9459. symbolS *lit_sym = lit->sym;
  9460. segT dseg = symbol_get_frag (lit_sym)->tc_frag_data.lit_seg;
  9461. if (dseg)
  9462. S_SET_SEGMENT (lit_sym, dseg);
  9463. }
  9464. }
  9465. /* Walk over all the frags for segments in a list and mark them as
  9466. containing literals. As clunky as this is, we can't rely on frag_var
  9467. and frag_variant to get called in all situations. */
  9468. static void
  9469. mark_literal_frags (seg_list *segment)
  9470. {
  9471. frchainS *frchain_from;
  9472. fragS *search_frag;
  9473. while (segment)
  9474. {
  9475. frchain_from = seg_info (segment->seg)->frchainP;
  9476. search_frag = frchain_from->frch_root;
  9477. while (search_frag)
  9478. {
  9479. search_frag->tc_frag_data.is_literal = TRUE;
  9480. search_frag = search_frag->fr_next;
  9481. }
  9482. segment = segment->next;
  9483. }
  9484. }
  9485. static void
  9486. xtensa_reorder_seg_list (seg_list *head, segT after)
  9487. {
  9488. /* Move all of the sections in the section list to come
  9489. after "after" in the gnu segment list. */
  9490. head = head->next;
  9491. while (head)
  9492. {
  9493. segT literal_section = head->seg;
  9494. /* Move the literal section after "after". */
  9495. gas_assert (literal_section);
  9496. if (literal_section != after)
  9497. {
  9498. bfd_section_list_remove (stdoutput, literal_section);
  9499. bfd_section_list_insert_after (stdoutput, after, literal_section);
  9500. }
  9501. head = head->next;
  9502. }
  9503. }
  9504. /* Push all the literal segments to the end of the gnu list. */
  9505. static void
  9506. xtensa_reorder_segments (void)
  9507. {
  9508. segT sec;
  9509. segT last_sec = 0;
  9510. int old_count = 0;
  9511. int new_count = 0;
  9512. for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
  9513. {
  9514. last_sec = sec;
  9515. old_count++;
  9516. }
  9517. /* Now that we have the last section, push all the literal
  9518. sections to the end. */
  9519. xtensa_reorder_seg_list (literal_head, last_sec);
  9520. /* Now perform the final error check. */
  9521. for (sec = stdoutput->sections; sec != NULL; sec = sec->next)
  9522. new_count++;
  9523. gas_assert (new_count == old_count);
  9524. }
  9525. /* Change the emit state (seg, subseg, and frag related stuff) to the
  9526. correct location. Return a emit_state which can be passed to
  9527. xtensa_restore_emit_state to return to current fragment. */
  9528. static void
  9529. xtensa_switch_to_literal_fragment (emit_state *result)
  9530. {
  9531. if (directive_state[directive_absolute_literals])
  9532. {
  9533. segT lit4_seg = cache_literal_section (TRUE);
  9534. xtensa_switch_section_emit_state (result, lit4_seg, 0);
  9535. }
  9536. else
  9537. xtensa_switch_to_non_abs_literal_fragment (result);
  9538. /* Do a 4-byte align here. */
  9539. frag_align (2, 0, 0);
  9540. record_alignment (now_seg, 2);
  9541. }
  9542. static void
  9543. xtensa_switch_to_non_abs_literal_fragment (emit_state *result)
  9544. {
  9545. static bfd_boolean recursive = FALSE;
  9546. fragS *pool_location = get_literal_pool_location (now_seg);
  9547. segT lit_seg;
  9548. bfd_boolean is_init =
  9549. (now_seg && !strcmp (segment_name (now_seg), INIT_SECTION_NAME));
  9550. bfd_boolean is_fini =
  9551. (now_seg && !strcmp (segment_name (now_seg), FINI_SECTION_NAME));
  9552. if (pool_location == NULL
  9553. && !use_literal_section
  9554. && !recursive
  9555. && !is_init && ! is_fini)
  9556. {
  9557. if (!auto_litpools)
  9558. {
  9559. as_bad (_("literal pool location required for text-section-literals; specify with .literal_position"));
  9560. }
  9561. /* When we mark a literal pool location, we want to put a frag in
  9562. the literal pool that points to it. But to do that, we want to
  9563. switch_to_literal_fragment. But literal sections don't have
  9564. literal pools, so their location is always null, so we would
  9565. recurse forever. This is kind of hacky, but it works. */
  9566. recursive = TRUE;
  9567. xtensa_mark_literal_pool_location ();
  9568. recursive = FALSE;
  9569. }
  9570. lit_seg = cache_literal_section (FALSE);
  9571. xtensa_switch_section_emit_state (result, lit_seg, 0);
  9572. if (!use_literal_section
  9573. && !is_init && !is_fini
  9574. && get_literal_pool_location (now_seg) != pool_location)
  9575. {
  9576. /* Close whatever frag is there. */
  9577. frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
  9578. xtensa_set_frag_assembly_state (frag_now);
  9579. frag_now->tc_frag_data.literal_frag = pool_location;
  9580. frag_variant (rs_fill, 0, 0, 0, NULL, 0, NULL);
  9581. xtensa_set_frag_assembly_state (frag_now);
  9582. }
  9583. }
  9584. /* Call this function before emitting data into the literal section.
  9585. This is a helper function for xtensa_switch_to_literal_fragment.
  9586. This is similar to a .section new_now_seg subseg. */
  9587. static void
  9588. xtensa_switch_section_emit_state (emit_state *state,
  9589. segT new_now_seg,
  9590. subsegT new_now_subseg)
  9591. {
  9592. state->name = now_seg->name;
  9593. state->now_seg = now_seg;
  9594. state->now_subseg = now_subseg;
  9595. state->generating_literals = generating_literals;
  9596. generating_literals++;
  9597. subseg_set (new_now_seg, new_now_subseg);
  9598. }
  9599. /* Use to restore the emitting into the normal place. */
  9600. static void
  9601. xtensa_restore_emit_state (emit_state *state)
  9602. {
  9603. generating_literals = state->generating_literals;
  9604. subseg_set (state->now_seg, state->now_subseg);
  9605. }
  9606. /* Predicate function used to look up a section in a particular group. */
  9607. static bfd_boolean
  9608. match_section_group (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *inf)
  9609. {
  9610. const char *gname = inf;
  9611. const char *group_name = elf_group_name (sec);
  9612. return (group_name == gname
  9613. || (group_name != NULL
  9614. && gname != NULL
  9615. && strcmp (group_name, gname) == 0));
  9616. }
  9617. /* Get the literal section to be used for the current text section.
  9618. The result may be cached in the default_lit_sections structure. */
  9619. static segT
  9620. cache_literal_section (bfd_boolean use_abs_literals)
  9621. {
  9622. const char *text_name, *group_name = 0;
  9623. char *base_name, *name, *suffix;
  9624. segT *pcached;
  9625. segT seg, current_section;
  9626. int current_subsec;
  9627. bfd_boolean linkonce = FALSE;
  9628. /* Save the current section/subsection. */
  9629. current_section = now_seg;
  9630. current_subsec = now_subseg;
  9631. /* Clear the cached values if they are no longer valid. */
  9632. if (now_seg != default_lit_sections.current_text_seg)
  9633. {
  9634. default_lit_sections.current_text_seg = now_seg;
  9635. default_lit_sections.lit_seg = NULL;
  9636. default_lit_sections.lit4_seg = NULL;
  9637. }
  9638. /* Check if the literal section is already cached. */
  9639. if (use_abs_literals)
  9640. pcached = &default_lit_sections.lit4_seg;
  9641. else
  9642. pcached = &default_lit_sections.lit_seg;
  9643. if (*pcached)
  9644. return *pcached;
  9645. text_name = default_lit_sections.lit_prefix;
  9646. if (! text_name || ! *text_name)
  9647. {
  9648. text_name = segment_name (current_section);
  9649. group_name = elf_group_name (current_section);
  9650. linkonce = (current_section->flags & SEC_LINK_ONCE) != 0;
  9651. }
  9652. base_name = use_abs_literals ? ".lit4" : ".literal";
  9653. if (group_name)
  9654. {
  9655. name = xmalloc (strlen (base_name) + strlen (group_name) + 2);
  9656. sprintf (name, "%s.%s", base_name, group_name);
  9657. }
  9658. else if (strncmp (text_name, ".gnu.linkonce.", linkonce_len) == 0)
  9659. {
  9660. suffix = strchr (text_name + linkonce_len, '.');
  9661. name = xmalloc (linkonce_len + strlen (base_name) + 1
  9662. + (suffix ? strlen (suffix) : 0));
  9663. strcpy (name, ".gnu.linkonce");
  9664. strcat (name, base_name);
  9665. if (suffix)
  9666. strcat (name, suffix);
  9667. linkonce = TRUE;
  9668. }
  9669. else
  9670. {
  9671. /* If the section name begins or ends with ".text", then replace
  9672. that portion instead of appending an additional suffix. */
  9673. size_t len = strlen (text_name);
  9674. if (len >= 5
  9675. && (strcmp (text_name + len - 5, ".text") == 0
  9676. || strncmp (text_name, ".text", 5) == 0))
  9677. len -= 5;
  9678. name = xmalloc (len + strlen (base_name) + 1);
  9679. if (strncmp (text_name, ".text", 5) == 0)
  9680. {
  9681. strcpy (name, base_name);
  9682. strcat (name, text_name + 5);
  9683. }
  9684. else
  9685. {
  9686. strcpy (name, text_name);
  9687. strcpy (name + len, base_name);
  9688. }
  9689. }
  9690. /* Canonicalize section names to allow renaming literal sections.
  9691. The group name, if any, came from the current text section and
  9692. has already been canonicalized. */
  9693. name = tc_canonicalize_symbol_name (name);
  9694. seg = bfd_get_section_by_name_if (stdoutput, name, match_section_group,
  9695. (void *) group_name);
  9696. if (! seg)
  9697. {
  9698. flagword flags;
  9699. seg = subseg_force_new (name, 0);
  9700. if (! use_abs_literals)
  9701. {
  9702. /* Add the newly created literal segment to the list. */
  9703. seg_list *n = (seg_list *) xmalloc (sizeof (seg_list));
  9704. n->seg = seg;
  9705. n->next = literal_head->next;
  9706. literal_head->next = n;
  9707. }
  9708. flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_ALLOC | SEC_LOAD
  9709. | (linkonce ? (SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD) : 0)
  9710. | (use_abs_literals ? SEC_DATA : SEC_CODE));
  9711. elf_group_name (seg) = group_name;
  9712. bfd_set_section_flags (stdoutput, seg, flags);
  9713. bfd_set_section_alignment (stdoutput, seg, 2);
  9714. }
  9715. *pcached = seg;
  9716. subseg_set (current_section, current_subsec);
  9717. return seg;
  9718. }
  9719. /* Property Tables Stuff. */
  9720. #define XTENSA_INSN_SEC_NAME ".xt.insn"
  9721. #define XTENSA_LIT_SEC_NAME ".xt.lit"
  9722. #define XTENSA_PROP_SEC_NAME ".xt.prop"
  9723. typedef bfd_boolean (*frag_predicate) (const fragS *);
  9724. typedef void (*frag_flags_fn) (const fragS *, frag_flags *);
  9725. static bfd_boolean get_frag_is_literal (const fragS *);
  9726. static void xtensa_create_property_segments
  9727. (frag_predicate, frag_predicate, const char *, xt_section_type);
  9728. static void xtensa_create_xproperty_segments
  9729. (frag_flags_fn, const char *, xt_section_type);
  9730. static bfd_boolean exclude_section_from_property_tables (segT);
  9731. static bfd_boolean section_has_property (segT, frag_predicate);
  9732. static bfd_boolean section_has_xproperty (segT, frag_flags_fn);
  9733. static void add_xt_block_frags
  9734. (segT, xtensa_block_info **, frag_predicate, frag_predicate);
  9735. static bfd_boolean xtensa_frag_flags_is_empty (const frag_flags *);
  9736. static void xtensa_frag_flags_init (frag_flags *);
  9737. static void get_frag_property_flags (const fragS *, frag_flags *);
  9738. static flagword frag_flags_to_number (const frag_flags *);
  9739. static void add_xt_prop_frags (segT, xtensa_block_info **, frag_flags_fn);
  9740. /* Set up property tables after relaxation. */
  9741. void
  9742. xtensa_post_relax_hook (void)
  9743. {
  9744. xtensa_move_seg_list_to_beginning (literal_head);
  9745. xtensa_find_unmarked_state_frags ();
  9746. xtensa_mark_frags_for_org ();
  9747. xtensa_mark_difference_of_two_symbols ();
  9748. xtensa_create_property_segments (get_frag_is_literal,
  9749. NULL,
  9750. XTENSA_LIT_SEC_NAME,
  9751. xt_literal_sec);
  9752. xtensa_create_xproperty_segments (get_frag_property_flags,
  9753. XTENSA_PROP_SEC_NAME,
  9754. xt_prop_sec);
  9755. if (warn_unaligned_branch_targets)
  9756. bfd_map_over_sections (stdoutput, xtensa_find_unaligned_branch_targets, 0);
  9757. bfd_map_over_sections (stdoutput, xtensa_find_unaligned_loops, 0);
  9758. }
  9759. /* This function is only meaningful after xtensa_move_literals. */
  9760. static bfd_boolean
  9761. get_frag_is_literal (const fragS *fragP)
  9762. {
  9763. gas_assert (fragP != NULL);
  9764. return fragP->tc_frag_data.is_literal;
  9765. }
  9766. static void
  9767. xtensa_create_property_segments (frag_predicate property_function,
  9768. frag_predicate end_property_function,
  9769. const char *section_name_base,
  9770. xt_section_type sec_type)
  9771. {
  9772. segT *seclist;
  9773. /* Walk over all of the current segments.
  9774. Walk over each fragment
  9775. For each non-empty fragment,
  9776. Build a property record (append where possible). */
  9777. for (seclist = &stdoutput->sections;
  9778. seclist && *seclist;
  9779. seclist = &(*seclist)->next)
  9780. {
  9781. segT sec = *seclist;
  9782. if (exclude_section_from_property_tables (sec))
  9783. continue;
  9784. if (section_has_property (sec, property_function))
  9785. {
  9786. segment_info_type *xt_seg_info;
  9787. xtensa_block_info **xt_blocks;
  9788. segT prop_sec = xtensa_make_property_section (sec, section_name_base);
  9789. prop_sec->output_section = prop_sec;
  9790. subseg_set (prop_sec, 0);
  9791. xt_seg_info = seg_info (prop_sec);
  9792. xt_blocks = &xt_seg_info->tc_segment_info_data.blocks[sec_type];
  9793. /* Walk over all of the frchains here and add new sections. */
  9794. add_xt_block_frags (sec, xt_blocks, property_function,
  9795. end_property_function);
  9796. }
  9797. }
  9798. /* Now we fill them out.... */
  9799. for (seclist = &stdoutput->sections;
  9800. seclist && *seclist;
  9801. seclist = &(*seclist)->next)
  9802. {
  9803. segment_info_type *seginfo;
  9804. xtensa_block_info *block;
  9805. segT sec = *seclist;
  9806. seginfo = seg_info (sec);
  9807. block = seginfo->tc_segment_info_data.blocks[sec_type];
  9808. if (block)
  9809. {
  9810. xtensa_block_info *cur_block;
  9811. int num_recs = 0;
  9812. bfd_size_type rec_size;
  9813. for (cur_block = block; cur_block; cur_block = cur_block->next)
  9814. num_recs++;
  9815. rec_size = num_recs * 8;
  9816. bfd_set_section_size (stdoutput, sec, rec_size);
  9817. if (num_recs)
  9818. {
  9819. char *frag_data;
  9820. int i;
  9821. subseg_set (sec, 0);
  9822. frag_data = frag_more (rec_size);
  9823. cur_block = block;
  9824. for (i = 0; i < num_recs; i++)
  9825. {
  9826. fixS *fix;
  9827. /* Write the fixup. */
  9828. gas_assert (cur_block);
  9829. fix = fix_new (frag_now, i * 8, 4,
  9830. section_symbol (cur_block->sec),
  9831. cur_block->offset,
  9832. FALSE, BFD_RELOC_32);
  9833. fix->fx_file = "<internal>";
  9834. fix->fx_line = 0;
  9835. /* Write the length. */
  9836. md_number_to_chars (&frag_data[4 + i * 8],
  9837. cur_block->size, 4);
  9838. cur_block = cur_block->next;
  9839. }
  9840. frag_wane (frag_now);
  9841. frag_new (0);
  9842. frag_wane (frag_now);
  9843. }
  9844. }
  9845. }
  9846. }
  9847. static void
  9848. xtensa_create_xproperty_segments (frag_flags_fn flag_fn,
  9849. const char *section_name_base,
  9850. xt_section_type sec_type)
  9851. {
  9852. segT *seclist;
  9853. /* Walk over all of the current segments.
  9854. Walk over each fragment.
  9855. For each fragment that has instructions,
  9856. build an instruction record (append where possible). */
  9857. for (seclist = &stdoutput->sections;
  9858. seclist && *seclist;
  9859. seclist = &(*seclist)->next)
  9860. {
  9861. segT sec = *seclist;
  9862. if (exclude_section_from_property_tables (sec))
  9863. continue;
  9864. if (section_has_xproperty (sec, flag_fn))
  9865. {
  9866. segment_info_type *xt_seg_info;
  9867. xtensa_block_info **xt_blocks;
  9868. segT prop_sec = xtensa_make_property_section (sec, section_name_base);
  9869. prop_sec->output_section = prop_sec;
  9870. subseg_set (prop_sec, 0);
  9871. xt_seg_info = seg_info (prop_sec);
  9872. xt_blocks = &xt_seg_info->tc_segment_info_data.blocks[sec_type];
  9873. /* Walk over all of the frchains here and add new sections. */
  9874. add_xt_prop_frags (sec, xt_blocks, flag_fn);
  9875. }
  9876. }
  9877. /* Now we fill them out.... */
  9878. for (seclist = &stdoutput->sections;
  9879. seclist && *seclist;
  9880. seclist = &(*seclist)->next)
  9881. {
  9882. segment_info_type *seginfo;
  9883. xtensa_block_info *block;
  9884. segT sec = *seclist;
  9885. seginfo = seg_info (sec);
  9886. block = seginfo->tc_segment_info_data.blocks[sec_type];
  9887. if (block)
  9888. {
  9889. xtensa_block_info *cur_block;
  9890. int num_recs = 0;
  9891. bfd_size_type rec_size;
  9892. for (cur_block = block; cur_block; cur_block = cur_block->next)
  9893. num_recs++;
  9894. rec_size = num_recs * (8 + 4);
  9895. bfd_set_section_size (stdoutput, sec, rec_size);
  9896. /* elf_section_data (sec)->this_hdr.sh_entsize = 12; */
  9897. if (num_recs)
  9898. {
  9899. char *frag_data;
  9900. int i;
  9901. subseg_set (sec, 0);
  9902. frag_data = frag_more (rec_size);
  9903. cur_block = block;
  9904. for (i = 0; i < num_recs; i++)
  9905. {
  9906. fixS *fix;
  9907. /* Write the fixup. */
  9908. gas_assert (cur_block);
  9909. fix = fix_new (frag_now, i * 12, 4,
  9910. section_symbol (cur_block->sec),
  9911. cur_block->offset,
  9912. FALSE, BFD_RELOC_32);
  9913. fix->fx_file = "<internal>";
  9914. fix->fx_line = 0;
  9915. /* Write the length. */
  9916. md_number_to_chars (&frag_data[4 + i * 12],
  9917. cur_block->size, 4);
  9918. md_number_to_chars (&frag_data[8 + i * 12],
  9919. frag_flags_to_number (&cur_block->flags),
  9920. sizeof (flagword));
  9921. cur_block = cur_block->next;
  9922. }
  9923. frag_wane (frag_now);
  9924. frag_new (0);
  9925. frag_wane (frag_now);
  9926. }
  9927. }
  9928. }
  9929. }
  9930. static bfd_boolean
  9931. exclude_section_from_property_tables (segT sec)
  9932. {
  9933. flagword flags = bfd_get_section_flags (stdoutput, sec);
  9934. /* Sections that don't contribute to the memory footprint are excluded. */
  9935. if ((flags & SEC_DEBUGGING)
  9936. || !(flags & SEC_ALLOC)
  9937. || (flags & SEC_MERGE))
  9938. return TRUE;
  9939. /* Linker cie and fde optimizations mess up property entries for
  9940. eh_frame sections, but there is nothing inside them relevant to
  9941. property tables anyway. */
  9942. if (strcmp (sec->name, ".eh_frame") == 0)
  9943. return TRUE;
  9944. return FALSE;
  9945. }
  9946. static bfd_boolean
  9947. section_has_property (segT sec, frag_predicate property_function)
  9948. {
  9949. segment_info_type *seginfo = seg_info (sec);
  9950. fragS *fragP;
  9951. if (seginfo && seginfo->frchainP)
  9952. {
  9953. for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
  9954. {
  9955. if (property_function (fragP)
  9956. && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
  9957. return TRUE;
  9958. }
  9959. }
  9960. return FALSE;
  9961. }
  9962. static bfd_boolean
  9963. section_has_xproperty (segT sec, frag_flags_fn property_function)
  9964. {
  9965. segment_info_type *seginfo = seg_info (sec);
  9966. fragS *fragP;
  9967. if (seginfo && seginfo->frchainP)
  9968. {
  9969. for (fragP = seginfo->frchainP->frch_root; fragP; fragP = fragP->fr_next)
  9970. {
  9971. frag_flags prop_flags;
  9972. property_function (fragP, &prop_flags);
  9973. if (!xtensa_frag_flags_is_empty (&prop_flags))
  9974. return TRUE;
  9975. }
  9976. }
  9977. return FALSE;
  9978. }
  9979. /* Two types of block sections exist right now: literal and insns. */
  9980. static void
  9981. add_xt_block_frags (segT sec,
  9982. xtensa_block_info **xt_block,
  9983. frag_predicate property_function,
  9984. frag_predicate end_property_function)
  9985. {
  9986. fragS *fragP;
  9987. /* Build it if needed. */
  9988. while (*xt_block != NULL)
  9989. xt_block = &(*xt_block)->next;
  9990. /* We are either at NULL at the beginning or at the end. */
  9991. /* Walk through the frags. */
  9992. if (seg_info (sec)->frchainP)
  9993. {
  9994. for (fragP = seg_info (sec)->frchainP->frch_root;
  9995. fragP;
  9996. fragP = fragP->fr_next)
  9997. {
  9998. if (property_function (fragP)
  9999. && (fragP->fr_type != rs_fill || fragP->fr_fix != 0))
  10000. {
  10001. if (*xt_block != NULL)
  10002. {
  10003. if ((*xt_block)->offset + (*xt_block)->size
  10004. == fragP->fr_address)
  10005. (*xt_block)->size += fragP->fr_fix;
  10006. else
  10007. xt_block = &((*xt_block)->next);
  10008. }
  10009. if (*xt_block == NULL)
  10010. {
  10011. xtensa_block_info *new_block = (xtensa_block_info *)
  10012. xmalloc (sizeof (xtensa_block_info));
  10013. new_block->sec = sec;
  10014. new_block->offset = fragP->fr_address;
  10015. new_block->size = fragP->fr_fix;
  10016. new_block->next = NULL;
  10017. xtensa_frag_flags_init (&new_block->flags);
  10018. *xt_block = new_block;
  10019. }
  10020. if (end_property_function
  10021. && end_property_function (fragP))
  10022. {
  10023. xt_block = &((*xt_block)->next);
  10024. }
  10025. }
  10026. }
  10027. }
  10028. }
  10029. /* Break the encapsulation of add_xt_prop_frags here. */
  10030. static bfd_boolean
  10031. xtensa_frag_flags_is_empty (const frag_flags *prop_flags)
  10032. {
  10033. if (prop_flags->is_literal
  10034. || prop_flags->is_insn
  10035. || prop_flags->is_data
  10036. || prop_flags->is_unreachable)
  10037. return FALSE;
  10038. return TRUE;
  10039. }
  10040. static void
  10041. xtensa_frag_flags_init (frag_flags *prop_flags)
  10042. {
  10043. memset (prop_flags, 0, sizeof (frag_flags));
  10044. }
  10045. static void
  10046. get_frag_property_flags (const fragS *fragP, frag_flags *prop_flags)
  10047. {
  10048. xtensa_frag_flags_init (prop_flags);
  10049. if (fragP->tc_frag_data.is_literal)
  10050. prop_flags->is_literal = TRUE;
  10051. if (fragP->tc_frag_data.is_specific_opcode
  10052. || fragP->tc_frag_data.is_no_transform)
  10053. {
  10054. prop_flags->is_no_transform = TRUE;
  10055. if (xtensa_frag_flags_is_empty (prop_flags))
  10056. prop_flags->is_data = TRUE;
  10057. }
  10058. if (fragP->tc_frag_data.is_unreachable)
  10059. prop_flags->is_unreachable = TRUE;
  10060. else if (fragP->tc_frag_data.is_insn)
  10061. {
  10062. prop_flags->is_insn = TRUE;
  10063. if (fragP->tc_frag_data.is_loop_target)
  10064. prop_flags->insn.is_loop_target = TRUE;
  10065. if (fragP->tc_frag_data.is_branch_target)
  10066. prop_flags->insn.is_branch_target = TRUE;
  10067. if (fragP->tc_frag_data.is_no_density)
  10068. prop_flags->insn.is_no_density = TRUE;
  10069. if (fragP->tc_frag_data.use_absolute_literals)
  10070. prop_flags->insn.is_abslit = TRUE;
  10071. }
  10072. if (fragP->tc_frag_data.is_align)
  10073. {
  10074. prop_flags->is_align = TRUE;
  10075. prop_flags->alignment = fragP->tc_frag_data.alignment;
  10076. if (xtensa_frag_flags_is_empty (prop_flags))
  10077. prop_flags->is_data = TRUE;
  10078. }
  10079. }
  10080. static flagword
  10081. frag_flags_to_number (const frag_flags *prop_flags)
  10082. {
  10083. flagword num = 0;
  10084. if (prop_flags->is_literal)
  10085. num |= XTENSA_PROP_LITERAL;
  10086. if (prop_flags->is_insn)
  10087. num |= XTENSA_PROP_INSN;
  10088. if (prop_flags->is_data)
  10089. num |= XTENSA_PROP_DATA;
  10090. if (prop_flags->is_unreachable)
  10091. num |= XTENSA_PROP_UNREACHABLE;
  10092. if (prop_flags->insn.is_loop_target)
  10093. num |= XTENSA_PROP_INSN_LOOP_TARGET;
  10094. if (prop_flags->insn.is_branch_target)
  10095. {
  10096. num |= XTENSA_PROP_INSN_BRANCH_TARGET;
  10097. num = SET_XTENSA_PROP_BT_ALIGN (num, prop_flags->insn.bt_align_priority);
  10098. }
  10099. if (prop_flags->insn.is_no_density)
  10100. num |= XTENSA_PROP_INSN_NO_DENSITY;
  10101. if (prop_flags->is_no_transform)
  10102. num |= XTENSA_PROP_NO_TRANSFORM;
  10103. if (prop_flags->insn.is_no_reorder)
  10104. num |= XTENSA_PROP_INSN_NO_REORDER;
  10105. if (prop_flags->insn.is_abslit)
  10106. num |= XTENSA_PROP_INSN_ABSLIT;
  10107. if (prop_flags->is_align)
  10108. {
  10109. num |= XTENSA_PROP_ALIGN;
  10110. num = SET_XTENSA_PROP_ALIGNMENT (num, prop_flags->alignment);
  10111. }
  10112. return num;
  10113. }
  10114. static bfd_boolean
  10115. xtensa_frag_flags_combinable (const frag_flags *prop_flags_1,
  10116. const frag_flags *prop_flags_2)
  10117. {
  10118. /* Cannot combine with an end marker. */
  10119. if (prop_flags_1->is_literal != prop_flags_2->is_literal)
  10120. return FALSE;
  10121. if (prop_flags_1->is_insn != prop_flags_2->is_insn)
  10122. return FALSE;
  10123. if (prop_flags_1->is_data != prop_flags_2->is_data)
  10124. return FALSE;
  10125. if (prop_flags_1->is_insn)
  10126. {
  10127. /* Properties of the beginning of the frag. */
  10128. if (prop_flags_2->insn.is_loop_target)
  10129. return FALSE;
  10130. if (prop_flags_2->insn.is_branch_target)
  10131. return FALSE;
  10132. if (prop_flags_1->insn.is_no_density !=
  10133. prop_flags_2->insn.is_no_density)
  10134. return FALSE;
  10135. if (prop_flags_1->is_no_transform !=
  10136. prop_flags_2->is_no_transform)
  10137. return FALSE;
  10138. if (prop_flags_1->insn.is_no_reorder !=
  10139. prop_flags_2->insn.is_no_reorder)
  10140. return FALSE;
  10141. if (prop_flags_1->insn.is_abslit !=
  10142. prop_flags_2->insn.is_abslit)
  10143. return FALSE;
  10144. }
  10145. if (prop_flags_1->is_align)
  10146. return FALSE;
  10147. return TRUE;
  10148. }
  10149. static bfd_vma
  10150. xt_block_aligned_size (const xtensa_block_info *xt_block)
  10151. {
  10152. bfd_vma end_addr;
  10153. unsigned align_bits;
  10154. if (!xt_block->flags.is_align)
  10155. return xt_block->size;
  10156. end_addr = xt_block->offset + xt_block->size;
  10157. align_bits = xt_block->flags.alignment;
  10158. end_addr = ((end_addr + ((1 << align_bits) -1)) >> align_bits) << align_bits;
  10159. return end_addr - xt_block->offset;
  10160. }
  10161. static bfd_boolean
  10162. xtensa_xt_block_combine (xtensa_block_info *xt_block,
  10163. const xtensa_block_info *xt_block_2)
  10164. {
  10165. if (xt_block->sec != xt_block_2->sec)
  10166. return FALSE;
  10167. if (xt_block->offset + xt_block_aligned_size (xt_block)
  10168. != xt_block_2->offset)
  10169. return FALSE;
  10170. if (xt_block_2->size == 0
  10171. && (!xt_block_2->flags.is_unreachable
  10172. || xt_block->flags.is_unreachable))
  10173. {
  10174. if (xt_block_2->flags.is_align
  10175. && xt_block->flags.is_align)
  10176. {
  10177. /* Nothing needed. */
  10178. if (xt_block->flags.alignment >= xt_block_2->flags.alignment)
  10179. return TRUE;
  10180. }
  10181. else
  10182. {
  10183. if (xt_block_2->flags.is_align)
  10184. {
  10185. /* Push alignment to previous entry. */
  10186. xt_block->flags.is_align = xt_block_2->flags.is_align;
  10187. xt_block->flags.alignment = xt_block_2->flags.alignment;
  10188. }
  10189. return TRUE;
  10190. }
  10191. }
  10192. if (!xtensa_frag_flags_combinable (&xt_block->flags,
  10193. &xt_block_2->flags))
  10194. return FALSE;
  10195. xt_block->size += xt_block_2->size;
  10196. if (xt_block_2->flags.is_align)
  10197. {
  10198. xt_block->flags.is_align = TRUE;
  10199. xt_block->flags.alignment = xt_block_2->flags.alignment;
  10200. }
  10201. return TRUE;
  10202. }
  10203. static void
  10204. add_xt_prop_frags (segT sec,
  10205. xtensa_block_info **xt_block,
  10206. frag_flags_fn property_function)
  10207. {
  10208. fragS *fragP;
  10209. /* Build it if needed. */
  10210. while (*xt_block != NULL)
  10211. {
  10212. xt_block = &(*xt_block)->next;
  10213. }
  10214. /* We are either at NULL at the beginning or at the end. */
  10215. /* Walk through the frags. */
  10216. if (seg_info (sec)->frchainP)
  10217. {
  10218. for (fragP = seg_info (sec)->frchainP->frch_root; fragP;
  10219. fragP = fragP->fr_next)
  10220. {
  10221. xtensa_block_info tmp_block;
  10222. tmp_block.sec = sec;
  10223. tmp_block.offset = fragP->fr_address;
  10224. tmp_block.size = fragP->fr_fix;
  10225. tmp_block.next = NULL;
  10226. property_function (fragP, &tmp_block.flags);
  10227. if (!xtensa_frag_flags_is_empty (&tmp_block.flags))
  10228. /* && fragP->fr_fix != 0) */
  10229. {
  10230. if ((*xt_block) == NULL
  10231. || !xtensa_xt_block_combine (*xt_block, &tmp_block))
  10232. {
  10233. xtensa_block_info *new_block;
  10234. if ((*xt_block) != NULL)
  10235. xt_block = &(*xt_block)->next;
  10236. new_block = (xtensa_block_info *)
  10237. xmalloc (sizeof (xtensa_block_info));
  10238. *new_block = tmp_block;
  10239. *xt_block = new_block;
  10240. }
  10241. }
  10242. }
  10243. }
  10244. }
  10245. /* op_placement_info_table */
  10246. /* op_placement_info makes it easier to determine which
  10247. ops can go in which slots. */
  10248. static void
  10249. init_op_placement_info_table (void)
  10250. {
  10251. xtensa_isa isa = xtensa_default_isa;
  10252. xtensa_insnbuf ibuf = xtensa_insnbuf_alloc (isa);
  10253. xtensa_opcode opcode;
  10254. xtensa_format fmt;
  10255. int slot;
  10256. int num_opcodes = xtensa_isa_num_opcodes (isa);
  10257. op_placement_table = (op_placement_info_table)
  10258. xmalloc (sizeof (op_placement_info) * num_opcodes);
  10259. gas_assert (xtensa_isa_num_formats (isa) < MAX_FORMATS);
  10260. for (opcode = 0; opcode < num_opcodes; opcode++)
  10261. {
  10262. op_placement_info *opi = &op_placement_table[opcode];
  10263. /* FIXME: Make tinsn allocation dynamic. */
  10264. if (xtensa_opcode_num_operands (isa, opcode) > MAX_INSN_ARGS)
  10265. as_fatal (_("too many operands in instruction"));
  10266. opi->narrowest = XTENSA_UNDEFINED;
  10267. opi->narrowest_size = 0x7F;
  10268. opi->narrowest_slot = 0;
  10269. opi->formats = 0;
  10270. opi->num_formats = 0;
  10271. opi->issuef = 0;
  10272. for (fmt = 0; fmt < xtensa_isa_num_formats (isa); fmt++)
  10273. {
  10274. opi->slots[fmt] = 0;
  10275. for (slot = 0; slot < xtensa_format_num_slots (isa, fmt); slot++)
  10276. {
  10277. if (xtensa_opcode_encode (isa, fmt, slot, ibuf, opcode) == 0)
  10278. {
  10279. int fmt_length = xtensa_format_length (isa, fmt);
  10280. opi->issuef++;
  10281. set_bit (fmt, opi->formats);
  10282. set_bit (slot, opi->slots[fmt]);
  10283. if (fmt_length < opi->narrowest_size
  10284. || (fmt_length == opi->narrowest_size
  10285. && (xtensa_format_num_slots (isa, fmt)
  10286. < xtensa_format_num_slots (isa,
  10287. opi->narrowest))))
  10288. {
  10289. opi->narrowest = fmt;
  10290. opi->narrowest_size = fmt_length;
  10291. opi->narrowest_slot = slot;
  10292. }
  10293. }
  10294. }
  10295. if (opi->formats)
  10296. opi->num_formats++;
  10297. }
  10298. }
  10299. xtensa_insnbuf_free (isa, ibuf);
  10300. }
  10301. bfd_boolean
  10302. opcode_fits_format_slot (xtensa_opcode opcode, xtensa_format fmt, int slot)
  10303. {
  10304. return bit_is_set (slot, op_placement_table[opcode].slots[fmt]);
  10305. }
  10306. /* If the opcode is available in a single slot format, return its size. */
  10307. static int
  10308. xg_get_single_size (xtensa_opcode opcode)
  10309. {
  10310. return op_placement_table[opcode].narrowest_size;
  10311. }
  10312. static xtensa_format
  10313. xg_get_single_format (xtensa_opcode opcode)
  10314. {
  10315. return op_placement_table[opcode].narrowest;
  10316. }
  10317. static int
  10318. xg_get_single_slot (xtensa_opcode opcode)
  10319. {
  10320. return op_placement_table[opcode].narrowest_slot;
  10321. }
  10322. /* Instruction Stack Functions (from "xtensa-istack.h"). */
  10323. void
  10324. istack_init (IStack *stack)
  10325. {
  10326. stack->ninsn = 0;
  10327. }
  10328. bfd_boolean
  10329. istack_empty (IStack *stack)
  10330. {
  10331. return (stack->ninsn == 0);
  10332. }
  10333. bfd_boolean
  10334. istack_full (IStack *stack)
  10335. {
  10336. return (stack->ninsn == MAX_ISTACK);
  10337. }
  10338. /* Return a pointer to the top IStack entry.
  10339. It is an error to call this if istack_empty () is TRUE. */
  10340. TInsn *
  10341. istack_top (IStack *stack)
  10342. {
  10343. int rec = stack->ninsn - 1;
  10344. gas_assert (!istack_empty (stack));
  10345. return &stack->insn[rec];
  10346. }
  10347. /* Add a new TInsn to an IStack.
  10348. It is an error to call this if istack_full () is TRUE. */
  10349. void
  10350. istack_push (IStack *stack, TInsn *insn)
  10351. {
  10352. int rec = stack->ninsn;
  10353. gas_assert (!istack_full (stack));
  10354. stack->insn[rec] = *insn;
  10355. stack->ninsn++;
  10356. }
  10357. /* Clear space for the next TInsn on the IStack and return a pointer
  10358. to it. It is an error to call this if istack_full () is TRUE. */
  10359. TInsn *
  10360. istack_push_space (IStack *stack)
  10361. {
  10362. int rec = stack->ninsn;
  10363. TInsn *insn;
  10364. gas_assert (!istack_full (stack));
  10365. insn = &stack->insn[rec];
  10366. tinsn_init (insn);
  10367. stack->ninsn++;
  10368. return insn;
  10369. }
  10370. /* Remove the last pushed instruction. It is an error to call this if
  10371. istack_empty () returns TRUE. */
  10372. void
  10373. istack_pop (IStack *stack)
  10374. {
  10375. int rec = stack->ninsn - 1;
  10376. gas_assert (!istack_empty (stack));
  10377. stack->ninsn--;
  10378. tinsn_init (&stack->insn[rec]);
  10379. }
  10380. /* TInsn functions. */
  10381. void
  10382. tinsn_init (TInsn *dst)
  10383. {
  10384. memset (dst, 0, sizeof (TInsn));
  10385. }
  10386. /* Return TRUE if ANY of the operands in the insn are symbolic. */
  10387. static bfd_boolean
  10388. tinsn_has_symbolic_operands (const TInsn *insn)
  10389. {
  10390. int i;
  10391. int n = insn->ntok;
  10392. gas_assert (insn->insn_type == ITYPE_INSN);
  10393. for (i = 0; i < n; ++i)
  10394. {
  10395. switch (insn->tok[i].X_op)
  10396. {
  10397. case O_register:
  10398. case O_constant:
  10399. break;
  10400. default:
  10401. return TRUE;
  10402. }
  10403. }
  10404. return FALSE;
  10405. }
  10406. bfd_boolean
  10407. tinsn_has_invalid_symbolic_operands (const TInsn *insn)
  10408. {
  10409. xtensa_isa isa = xtensa_default_isa;
  10410. int i;
  10411. int n = insn->ntok;
  10412. gas_assert (insn->insn_type == ITYPE_INSN);
  10413. for (i = 0; i < n; ++i)
  10414. {
  10415. switch (insn->tok[i].X_op)
  10416. {
  10417. case O_register:
  10418. case O_constant:
  10419. break;
  10420. case O_big:
  10421. case O_illegal:
  10422. case O_absent:
  10423. /* Errors for these types are caught later. */
  10424. break;
  10425. case O_hi16:
  10426. case O_lo16:
  10427. default:
  10428. /* Symbolic immediates are only allowed on the last immediate
  10429. operand. At this time, CONST16 is the only opcode where we
  10430. support non-PC-relative relocations. */
  10431. if (i != get_relaxable_immed (insn->opcode)
  10432. || (xtensa_operand_is_PCrelative (isa, insn->opcode, i) != 1
  10433. && insn->opcode != xtensa_const16_opcode))
  10434. {
  10435. as_bad (_("invalid symbolic operand"));
  10436. return TRUE;
  10437. }
  10438. }
  10439. }
  10440. return FALSE;
  10441. }
  10442. /* For assembly code with complex expressions (e.g. subtraction),
  10443. we have to build them in the literal pool so that
  10444. their results are calculated correctly after relaxation.
  10445. The relaxation only handles expressions that
  10446. boil down to SYMBOL + OFFSET. */
  10447. static bfd_boolean
  10448. tinsn_has_complex_operands (const TInsn *insn)
  10449. {
  10450. int i;
  10451. int n = insn->ntok;
  10452. gas_assert (insn->insn_type == ITYPE_INSN);
  10453. for (i = 0; i < n; ++i)
  10454. {
  10455. switch (insn->tok[i].X_op)
  10456. {
  10457. case O_register:
  10458. case O_constant:
  10459. case O_symbol:
  10460. case O_lo16:
  10461. case O_hi16:
  10462. break;
  10463. default:
  10464. return TRUE;
  10465. }
  10466. }
  10467. return FALSE;
  10468. }
  10469. /* Encode a TInsn opcode and its constant operands into slotbuf.
  10470. Return TRUE if there is a symbol in the immediate field. This
  10471. function assumes that:
  10472. 1) The number of operands are correct.
  10473. 2) The insn_type is ITYPE_INSN.
  10474. 3) The opcode can be encoded in the specified format and slot.
  10475. 4) Operands are either O_constant or O_symbol, and all constants fit. */
  10476. static bfd_boolean
  10477. tinsn_to_slotbuf (xtensa_format fmt,
  10478. int slot,
  10479. TInsn *tinsn,
  10480. xtensa_insnbuf slotbuf)
  10481. {
  10482. xtensa_isa isa = xtensa_default_isa;
  10483. xtensa_opcode opcode = tinsn->opcode;
  10484. bfd_boolean has_fixup = FALSE;
  10485. int noperands = xtensa_opcode_num_operands (isa, opcode);
  10486. int i;
  10487. gas_assert (tinsn->insn_type == ITYPE_INSN);
  10488. if (noperands != tinsn->ntok)
  10489. as_fatal (_("operand number mismatch"));
  10490. if (xtensa_opcode_encode (isa, fmt, slot, slotbuf, opcode))
  10491. {
  10492. as_bad (_("cannot encode opcode \"%s\" in the given format \"%s\""),
  10493. xtensa_opcode_name (isa, opcode), xtensa_format_name (isa, fmt));
  10494. return FALSE;
  10495. }
  10496. for (i = 0; i < noperands; i++)
  10497. {
  10498. expressionS *exp = &tinsn->tok[i];
  10499. int rc;
  10500. unsigned line;
  10501. char *file_name;
  10502. uint32 opnd_value;
  10503. switch (exp->X_op)
  10504. {
  10505. case O_register:
  10506. if (xtensa_operand_is_visible (isa, opcode, i) == 0)
  10507. break;
  10508. /* The register number has already been checked in
  10509. expression_maybe_register, so we don't need to check here. */
  10510. opnd_value = exp->X_add_number;
  10511. (void) xtensa_operand_encode (isa, opcode, i, &opnd_value);
  10512. rc = xtensa_operand_set_field (isa, opcode, i, fmt, slot, slotbuf,
  10513. opnd_value);
  10514. if (rc != 0)
  10515. as_warn (_("xtensa-isa failure: %s"), xtensa_isa_error_msg (isa));
  10516. break;
  10517. case O_constant:
  10518. if (xtensa_operand_is_visible (isa, opcode, i) == 0)
  10519. break;
  10520. as_where (&file_name, &line);
  10521. /* It is a constant and we called this function
  10522. then we have to try to fit it. */
  10523. xtensa_insnbuf_set_operand (slotbuf, fmt, slot, opcode, i,
  10524. exp->X_add_number, file_name, line);
  10525. break;
  10526. default:
  10527. has_fixup = TRUE;
  10528. break;
  10529. }
  10530. }
  10531. return has_fixup;
  10532. }
  10533. /* Encode a single TInsn into an insnbuf. If the opcode can only be encoded
  10534. into a multi-slot instruction, fill the other slots with NOPs.
  10535. Return TRUE if there is a symbol in the immediate field. See also the
  10536. assumptions listed for tinsn_to_slotbuf. */
  10537. static bfd_boolean
  10538. tinsn_to_insnbuf (TInsn *tinsn, xtensa_insnbuf insnbuf)
  10539. {
  10540. static xtensa_insnbuf slotbuf = 0;
  10541. static vliw_insn vinsn;
  10542. xtensa_isa isa = xtensa_default_isa;
  10543. bfd_boolean has_fixup = FALSE;
  10544. int i;
  10545. if (!slotbuf)
  10546. {
  10547. slotbuf = xtensa_insnbuf_alloc (isa);
  10548. xg_init_vinsn (&vinsn);
  10549. }
  10550. xg_clear_vinsn (&vinsn);
  10551. bundle_tinsn (tinsn, &vinsn);
  10552. xtensa_format_encode (isa, vinsn.format, insnbuf);
  10553. for (i = 0; i < vinsn.num_slots; i++)
  10554. {
  10555. /* Only one slot may have a fix-up because the rest contains NOPs. */
  10556. has_fixup |=
  10557. tinsn_to_slotbuf (vinsn.format, i, &vinsn.slots[i], vinsn.slotbuf[i]);
  10558. xtensa_format_set_slot (isa, vinsn.format, i, insnbuf, vinsn.slotbuf[i]);
  10559. }
  10560. return has_fixup;
  10561. }
  10562. /* Check the instruction arguments. Return TRUE on failure. */
  10563. static bfd_boolean
  10564. tinsn_check_arguments (const TInsn *insn)
  10565. {
  10566. xtensa_isa isa = xtensa_default_isa;
  10567. xtensa_opcode opcode = insn->opcode;
  10568. xtensa_regfile t1_regfile, t2_regfile;
  10569. int t1_reg, t2_reg;
  10570. int t1_base_reg, t1_last_reg;
  10571. int t2_base_reg, t2_last_reg;
  10572. char t1_inout, t2_inout;
  10573. int i, j;
  10574. if (opcode == XTENSA_UNDEFINED)
  10575. {
  10576. as_bad (_("invalid opcode"));
  10577. return TRUE;
  10578. }
  10579. if (xtensa_opcode_num_operands (isa, opcode) > insn->ntok)
  10580. {
  10581. as_bad (_("too few operands"));
  10582. return TRUE;
  10583. }
  10584. if (xtensa_opcode_num_operands (isa, opcode) < insn->ntok)
  10585. {
  10586. as_bad (_("too many operands"));
  10587. return TRUE;
  10588. }
  10589. /* Check registers. */
  10590. for (j = 0; j < insn->ntok; j++)
  10591. {
  10592. if (xtensa_operand_is_register (isa, insn->opcode, j) != 1)
  10593. continue;
  10594. t2_regfile = xtensa_operand_regfile (isa, insn->opcode, j);
  10595. t2_base_reg = insn->tok[j].X_add_number;
  10596. t2_last_reg
  10597. = t2_base_reg + xtensa_operand_num_regs (isa, insn->opcode, j);
  10598. for (i = 0; i < insn->ntok; i++)
  10599. {
  10600. if (i == j)
  10601. continue;
  10602. if (xtensa_operand_is_register (isa, insn->opcode, i) != 1)
  10603. continue;
  10604. t1_regfile = xtensa_operand_regfile (isa, insn->opcode, i);
  10605. if (t1_regfile != t2_regfile)
  10606. continue;
  10607. t1_inout = xtensa_operand_inout (isa, insn->opcode, i);
  10608. t2_inout = xtensa_operand_inout (isa, insn->opcode, j);
  10609. t1_base_reg = insn->tok[i].X_add_number;
  10610. t1_last_reg = (t1_base_reg
  10611. + xtensa_operand_num_regs (isa, insn->opcode, i));
  10612. for (t1_reg = t1_base_reg; t1_reg < t1_last_reg; t1_reg++)
  10613. {
  10614. for (t2_reg = t2_base_reg; t2_reg < t2_last_reg; t2_reg++)
  10615. {
  10616. if (t1_reg != t2_reg)
  10617. continue;
  10618. if (t1_inout != 'i' && t2_inout != 'i')
  10619. {
  10620. as_bad (_("multiple writes to the same register"));
  10621. return TRUE;
  10622. }
  10623. }
  10624. }
  10625. }
  10626. }
  10627. return FALSE;
  10628. }
  10629. /* Load an instruction from its encoded form. */
  10630. static void
  10631. tinsn_from_chars (TInsn *tinsn, char *f, int slot)
  10632. {
  10633. vliw_insn vinsn;
  10634. xg_init_vinsn (&vinsn);
  10635. vinsn_from_chars (&vinsn, f);
  10636. *tinsn = vinsn.slots[slot];
  10637. xg_free_vinsn (&vinsn);
  10638. }
  10639. static void
  10640. tinsn_from_insnbuf (TInsn *tinsn,
  10641. xtensa_insnbuf slotbuf,
  10642. xtensa_format fmt,
  10643. int slot)
  10644. {
  10645. int i;
  10646. xtensa_isa isa = xtensa_default_isa;
  10647. /* Find the immed. */
  10648. tinsn_init (tinsn);
  10649. tinsn->insn_type = ITYPE_INSN;
  10650. tinsn->is_specific_opcode = FALSE; /* must not be specific */
  10651. tinsn->opcode = xtensa_opcode_decode (isa, fmt, slot, slotbuf);
  10652. tinsn->ntok = xtensa_opcode_num_operands (isa, tinsn->opcode);
  10653. for (i = 0; i < tinsn->ntok; i++)
  10654. {
  10655. set_expr_const (&tinsn->tok[i],
  10656. xtensa_insnbuf_get_operand (slotbuf, fmt, slot,
  10657. tinsn->opcode, i));
  10658. }
  10659. }
  10660. /* Read the value of the relaxable immed from the fr_symbol and fr_offset. */
  10661. static void
  10662. tinsn_immed_from_frag (TInsn *tinsn, fragS *fragP, int slot)
  10663. {
  10664. xtensa_opcode opcode = tinsn->opcode;
  10665. int opnum;
  10666. if (fragP->tc_frag_data.slot_symbols[slot])
  10667. {
  10668. opnum = get_relaxable_immed (opcode);
  10669. gas_assert (opnum >= 0);
  10670. set_expr_symbol_offset (&tinsn->tok[opnum],
  10671. fragP->tc_frag_data.slot_symbols[slot],
  10672. fragP->tc_frag_data.slot_offsets[slot]);
  10673. }
  10674. tinsn->extra_arg = fragP->tc_frag_data.free_reg[slot];
  10675. }
  10676. static int
  10677. get_num_stack_text_bytes (IStack *istack)
  10678. {
  10679. int i;
  10680. int text_bytes = 0;
  10681. for (i = 0; i < istack->ninsn; i++)
  10682. {
  10683. TInsn *tinsn = &istack->insn[i];
  10684. if (tinsn->insn_type == ITYPE_INSN)
  10685. text_bytes += xg_get_single_size (tinsn->opcode);
  10686. }
  10687. return text_bytes;
  10688. }
  10689. static int
  10690. get_num_stack_literal_bytes (IStack *istack)
  10691. {
  10692. int i;
  10693. int lit_bytes = 0;
  10694. for (i = 0; i < istack->ninsn; i++)
  10695. {
  10696. TInsn *tinsn = &istack->insn[i];
  10697. if (tinsn->insn_type == ITYPE_LITERAL && tinsn->ntok == 1)
  10698. lit_bytes += 4;
  10699. }
  10700. return lit_bytes;
  10701. }
  10702. /* vliw_insn functions. */
  10703. static void
  10704. xg_init_vinsn (vliw_insn *v)
  10705. {
  10706. int i;
  10707. xtensa_isa isa = xtensa_default_isa;
  10708. xg_clear_vinsn (v);
  10709. v->insnbuf = xtensa_insnbuf_alloc (isa);
  10710. if (v->insnbuf == NULL)
  10711. as_fatal (_("out of memory"));
  10712. for (i = 0; i < config_max_slots; i++)
  10713. {
  10714. v->slotbuf[i] = xtensa_insnbuf_alloc (isa);
  10715. if (v->slotbuf[i] == NULL)
  10716. as_fatal (_("out of memory"));
  10717. }
  10718. }
  10719. static void
  10720. xg_clear_vinsn (vliw_insn *v)
  10721. {
  10722. int i;
  10723. memset (v, 0, offsetof (vliw_insn, slots)
  10724. + sizeof(TInsn) * config_max_slots);
  10725. v->format = XTENSA_UNDEFINED;
  10726. v->num_slots = 0;
  10727. v->inside_bundle = FALSE;
  10728. if (xt_saved_debug_type != DEBUG_NONE)
  10729. debug_type = xt_saved_debug_type;
  10730. for (i = 0; i < config_max_slots; i++)
  10731. v->slots[i].opcode = XTENSA_UNDEFINED;
  10732. }
  10733. static void
  10734. xg_copy_vinsn (vliw_insn *dst, vliw_insn *src)
  10735. {
  10736. memcpy (dst, src,
  10737. offsetof(vliw_insn, slots) + src->num_slots * sizeof(TInsn));
  10738. dst->insnbuf = src->insnbuf;
  10739. memcpy (dst->slotbuf, src->slotbuf, src->num_slots * sizeof(xtensa_insnbuf));
  10740. }
  10741. static bfd_boolean
  10742. vinsn_has_specific_opcodes (vliw_insn *v)
  10743. {
  10744. int i;
  10745. for (i = 0; i < v->num_slots; i++)
  10746. {
  10747. if (v->slots[i].is_specific_opcode)
  10748. return TRUE;
  10749. }
  10750. return FALSE;
  10751. }
  10752. static void
  10753. xg_free_vinsn (vliw_insn *v)
  10754. {
  10755. int i;
  10756. xtensa_insnbuf_free (xtensa_default_isa, v->insnbuf);
  10757. for (i = 0; i < config_max_slots; i++)
  10758. xtensa_insnbuf_free (xtensa_default_isa, v->slotbuf[i]);
  10759. }
  10760. /* Encode a vliw_insn into an insnbuf. Return TRUE if there are any symbolic
  10761. operands. See also the assumptions listed for tinsn_to_slotbuf. */
  10762. static bfd_boolean
  10763. vinsn_to_insnbuf (vliw_insn *vinsn,
  10764. char *frag_offset,
  10765. fragS *fragP,
  10766. bfd_boolean record_fixup)
  10767. {
  10768. xtensa_isa isa = xtensa_default_isa;
  10769. xtensa_format fmt = vinsn->format;
  10770. xtensa_insnbuf insnbuf = vinsn->insnbuf;
  10771. int slot;
  10772. bfd_boolean has_fixup = FALSE;
  10773. xtensa_format_encode (isa, fmt, insnbuf);
  10774. for (slot = 0; slot < vinsn->num_slots; slot++)
  10775. {
  10776. TInsn *tinsn = &vinsn->slots[slot];
  10777. expressionS *extra_arg = &tinsn->extra_arg;
  10778. bfd_boolean tinsn_has_fixup =
  10779. tinsn_to_slotbuf (vinsn->format, slot, tinsn,
  10780. vinsn->slotbuf[slot]);
  10781. xtensa_format_set_slot (isa, fmt, slot,
  10782. insnbuf, vinsn->slotbuf[slot]);
  10783. if (extra_arg->X_op != O_illegal && extra_arg->X_op != O_register)
  10784. {
  10785. if (vinsn->num_slots != 1)
  10786. as_bad (_("TLS relocation not allowed in FLIX bundle"));
  10787. else if (record_fixup)
  10788. /* Instructions that generate TLS relocations should always be
  10789. relaxed in the front-end. If "record_fixup" is set, then this
  10790. function is being called during back-end relaxation, so flag
  10791. the unexpected behavior as an error. */
  10792. as_bad (_("unexpected TLS relocation"));
  10793. else
  10794. fix_new (fragP, frag_offset - fragP->fr_literal,
  10795. xtensa_format_length (isa, fmt),
  10796. extra_arg->X_add_symbol, extra_arg->X_add_number,
  10797. FALSE, map_operator_to_reloc (extra_arg->X_op, FALSE));
  10798. }
  10799. if (tinsn_has_fixup)
  10800. {
  10801. int i;
  10802. xtensa_opcode opcode = tinsn->opcode;
  10803. int noperands = xtensa_opcode_num_operands (isa, opcode);
  10804. has_fixup = TRUE;
  10805. for (i = 0; i < noperands; i++)
  10806. {
  10807. expressionS* exp = &tinsn->tok[i];
  10808. switch (exp->X_op)
  10809. {
  10810. case O_symbol:
  10811. case O_lo16:
  10812. case O_hi16:
  10813. if (get_relaxable_immed (opcode) == i)
  10814. {
  10815. /* Add a fix record for the instruction, except if this
  10816. function is being called prior to relaxation, i.e.,
  10817. if record_fixup is false, and the instruction might
  10818. be relaxed later. */
  10819. if (record_fixup
  10820. || tinsn->is_specific_opcode
  10821. || !xg_is_relaxable_insn (tinsn, 0))
  10822. {
  10823. xg_add_opcode_fix (tinsn, i, fmt, slot, exp, fragP,
  10824. frag_offset - fragP->fr_literal);
  10825. }
  10826. else
  10827. {
  10828. if (exp->X_op != O_symbol)
  10829. as_bad (_("invalid operand"));
  10830. tinsn->symbol = exp->X_add_symbol;
  10831. tinsn->offset = exp->X_add_number;
  10832. }
  10833. }
  10834. else
  10835. as_bad (_("symbolic operand not allowed"));
  10836. break;
  10837. case O_constant:
  10838. case O_register:
  10839. break;
  10840. default:
  10841. as_bad (_("expression too complex"));
  10842. break;
  10843. }
  10844. }
  10845. }
  10846. }
  10847. return has_fixup;
  10848. }
  10849. static void
  10850. vinsn_from_chars (vliw_insn *vinsn, char *f)
  10851. {
  10852. static xtensa_insnbuf insnbuf = NULL;
  10853. static xtensa_insnbuf slotbuf = NULL;
  10854. int i;
  10855. xtensa_format fmt;
  10856. xtensa_isa isa = xtensa_default_isa;
  10857. if (!insnbuf)
  10858. {
  10859. insnbuf = xtensa_insnbuf_alloc (isa);
  10860. slotbuf = xtensa_insnbuf_alloc (isa);
  10861. }
  10862. xtensa_insnbuf_from_chars (isa, insnbuf, (unsigned char *) f, 0);
  10863. fmt = xtensa_format_decode (isa, insnbuf);
  10864. if (fmt == XTENSA_UNDEFINED)
  10865. as_fatal (_("cannot decode instruction format"));
  10866. vinsn->format = fmt;
  10867. vinsn->num_slots = xtensa_format_num_slots (isa, fmt);
  10868. for (i = 0; i < vinsn->num_slots; i++)
  10869. {
  10870. TInsn *tinsn = &vinsn->slots[i];
  10871. xtensa_format_get_slot (isa, fmt, i, insnbuf, slotbuf);
  10872. tinsn_from_insnbuf (tinsn, slotbuf, fmt, i);
  10873. }
  10874. }
  10875. /* Expression utilities. */
  10876. /* Return TRUE if the expression is an integer constant. */
  10877. bfd_boolean
  10878. expr_is_const (const expressionS *s)
  10879. {
  10880. return (s->X_op == O_constant);
  10881. }
  10882. /* Get the expression constant.
  10883. Calling this is illegal if expr_is_const () returns TRUE. */
  10884. offsetT
  10885. get_expr_const (const expressionS *s)
  10886. {
  10887. gas_assert (expr_is_const (s));
  10888. return s->X_add_number;
  10889. }
  10890. /* Set the expression to a constant value. */
  10891. void
  10892. set_expr_const (expressionS *s, offsetT val)
  10893. {
  10894. s->X_op = O_constant;
  10895. s->X_add_number = val;
  10896. s->X_add_symbol = NULL;
  10897. s->X_op_symbol = NULL;
  10898. }
  10899. bfd_boolean
  10900. expr_is_register (const expressionS *s)
  10901. {
  10902. return (s->X_op == O_register);
  10903. }
  10904. /* Get the expression constant.
  10905. Calling this is illegal if expr_is_const () returns TRUE. */
  10906. offsetT
  10907. get_expr_register (const expressionS *s)
  10908. {
  10909. gas_assert (expr_is_register (s));
  10910. return s->X_add_number;
  10911. }
  10912. /* Set the expression to a symbol + constant offset. */
  10913. void
  10914. set_expr_symbol_offset (expressionS *s, symbolS *sym, offsetT offset)
  10915. {
  10916. s->X_op = O_symbol;
  10917. s->X_add_symbol = sym;
  10918. s->X_op_symbol = NULL; /* unused */
  10919. s->X_add_number = offset;
  10920. }
  10921. /* Return TRUE if the two expressions are equal. */
  10922. bfd_boolean
  10923. expr_is_equal (expressionS *s1, expressionS *s2)
  10924. {
  10925. if (s1->X_op != s2->X_op)
  10926. return FALSE;
  10927. if (s1->X_add_symbol != s2->X_add_symbol)
  10928. return FALSE;
  10929. if (s1->X_op_symbol != s2->X_op_symbol)
  10930. return FALSE;
  10931. if (s1->X_add_number != s2->X_add_number)
  10932. return FALSE;
  10933. return TRUE;
  10934. }
  10935. static void
  10936. copy_expr (expressionS *dst, const expressionS *src)
  10937. {
  10938. memcpy (dst, src, sizeof (expressionS));
  10939. }
  10940. /* Support for the "--rename-section" option. */
  10941. struct rename_section_struct
  10942. {
  10943. char *old_name;
  10944. char *new_name;
  10945. struct rename_section_struct *next;
  10946. };
  10947. static struct rename_section_struct *section_rename;
  10948. /* Parse the string "oldname=new_name(:oldname2=new_name2)*" and add
  10949. entries to the section_rename list. Note: Specifying multiple
  10950. renamings separated by colons is not documented and is retained only
  10951. for backward compatibility. */
  10952. static void
  10953. build_section_rename (const char *arg)
  10954. {
  10955. struct rename_section_struct *r;
  10956. char *this_arg = NULL;
  10957. char *next_arg = NULL;
  10958. for (this_arg = xstrdup (arg); this_arg != NULL; this_arg = next_arg)
  10959. {
  10960. char *old_name, *new_name;
  10961. if (this_arg)
  10962. {
  10963. next_arg = strchr (this_arg, ':');
  10964. if (next_arg)
  10965. {
  10966. *next_arg = '\0';
  10967. next_arg++;
  10968. }
  10969. }
  10970. old_name = this_arg;
  10971. new_name = strchr (this_arg, '=');
  10972. if (*old_name == '\0')
  10973. {
  10974. as_warn (_("ignoring extra '-rename-section' delimiter ':'"));
  10975. continue;
  10976. }
  10977. if (!new_name || new_name[1] == '\0')
  10978. {
  10979. as_warn (_("ignoring invalid '-rename-section' specification: '%s'"),
  10980. old_name);
  10981. continue;
  10982. }
  10983. *new_name = '\0';
  10984. new_name++;
  10985. /* Check for invalid section renaming. */
  10986. for (r = section_rename; r != NULL; r = r->next)
  10987. {
  10988. if (strcmp (r->old_name, old_name) == 0)
  10989. as_bad (_("section %s renamed multiple times"), old_name);
  10990. if (strcmp (r->new_name, new_name) == 0)
  10991. as_bad (_("multiple sections remapped to output section %s"),
  10992. new_name);
  10993. }
  10994. /* Now add it. */
  10995. r = (struct rename_section_struct *)
  10996. xmalloc (sizeof (struct rename_section_struct));
  10997. r->old_name = xstrdup (old_name);
  10998. r->new_name = xstrdup (new_name);
  10999. r->next = section_rename;
  11000. section_rename = r;
  11001. }
  11002. }
  11003. char *
  11004. xtensa_section_rename (char *name)
  11005. {
  11006. struct rename_section_struct *r = section_rename;
  11007. for (r = section_rename; r != NULL; r = r->next)
  11008. {
  11009. if (strcmp (r->old_name, name) == 0)
  11010. return r->new_name;
  11011. }
  11012. return name;
  11013. }