cgcpu.pas 217 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442
  1. {
  2. Copyright (c) 2003 by Florian Klaempfl
  3. Member of the Free Pascal development team
  4. This unit implements the code generator for the ARM
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit cgcpu;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. globtype,symtype,symdef,
  23. cgbase,cgutils,cgobj,
  24. aasmbase,aasmcpu,aasmtai,aasmdata,
  25. parabase,
  26. cpubase,cpuinfo,cg64f32,rgcpu;
  27. type
  28. { tbasecgarm is shared between all arm architectures }
  29. tbasecgarm = class(tcg)
  30. { true, if the next arithmetic operation should modify the flags }
  31. cgsetflags : boolean;
  32. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const paraloc : TCGPara);override;
  33. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);override;
  34. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : TCGPara);override;
  35. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  36. procedure a_call_reg(list : TAsmList;reg: tregister);override;
  37. { move instructions }
  38. procedure a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  39. procedure a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  40. function a_internal_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference):treference;
  41. function a_internal_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister):treference;
  42. { fpu move instructions }
  43. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  44. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  45. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  46. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);override;
  47. { comparison operations }
  48. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  49. l : tasmlabel);override;
  50. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  51. procedure a_jmp_name(list : TAsmList;const s : string); override;
  52. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  53. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  54. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  55. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  56. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  57. procedure g_maybe_got_init(list : TAsmList); override;
  58. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  59. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  60. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);override;
  61. procedure g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  62. procedure g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : tcgint;aligned : boolean);
  63. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef); override;
  64. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);override;
  65. procedure g_save_registers(list : TAsmList);override;
  66. procedure g_restore_registers(list : TAsmList);override;
  67. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  68. procedure fixref(list : TAsmList;var ref : treference);
  69. function handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference; virtual;
  70. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  71. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  72. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  73. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  74. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize;intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  75. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize;mmreg, intreg: tregister; shuffle : pmmshuffle); override;
  76. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); override;
  77. { Transform unsupported methods into Internal errors }
  78. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: TCGSize; src, dst: TRegister); override;
  79. { try to generate optimized 32 Bit multiplication, returns true if successful generated }
  80. function try_optimized_mul32_const_reg_reg(list: TAsmList; a: tcgint; src, dst: tregister) : boolean;
  81. { clear out potential overflow bits from 8 or 16 bit operations }
  82. { the upper 24/16 bits of a register after an operation }
  83. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  84. { mla for thumb requires that none of the registers is equal to r13/r15, this method ensures this }
  85. procedure safe_mla(list: TAsmList;op1,op2,op3,op4 : TRegister);
  86. end;
  87. { tcgarm is shared between normal arm and thumb-2 }
  88. tcgarm = class(tbasecgarm)
  89. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  90. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  91. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  92. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  93. size: tcgsize; a: tcgint; src, dst: tregister); override;
  94. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  95. size: tcgsize; src1, src2, dst: tregister); override;
  96. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  97. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  98. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);override;
  99. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  100. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint); override;
  101. {Multiply two 32-bit registers into lo and hi 32-bit registers}
  102. procedure a_mul_reg_reg_pair(list: tasmlist; size: tcgsize; src1,src2,dstlo,dsthi: tregister); override;
  103. end;
  104. { normal arm cg }
  105. tarmcgarm = class(tcgarm)
  106. procedure init_register_allocators;override;
  107. procedure done_register_allocators;override;
  108. end;
  109. { 64 bit cg for all arm flavours }
  110. tbasecg64farm = class(tcg64f32)
  111. end;
  112. { tcg64farm is shared between normal arm and thumb-2 }
  113. tcg64farm = class(tbasecg64farm)
  114. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  115. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  116. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  117. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  118. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  119. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  120. procedure a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister);override;
  121. procedure a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64);override;
  122. end;
  123. tarmcg64farm = class(tcg64farm)
  124. end;
  125. tthumbcgarm = class(tbasecgarm)
  126. procedure init_register_allocators;override;
  127. procedure done_register_allocators;override;
  128. procedure g_proc_entry(list: TAsmList; localsize: longint; nostackframe: boolean);override;
  129. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  130. procedure a_op_reg_reg(list: TAsmList; Op: TOpCG; size: TCGSize; src,dst: TRegister);override;
  131. procedure a_op_const_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; dst: tregister);override;
  132. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister); override;
  133. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  134. procedure a_load_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const Ref: treference; reg: tregister);override;
  135. procedure a_load_const_reg(list: TAsmList; size: tcgsize; a: tcgint; reg: tregister);override;
  136. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint); override;
  137. function handle_load_store(list: TAsmList; op: tasmop; oppostfix: toppostfix; reg: tregister; ref: treference): treference; override;
  138. procedure g_external_wrapper(list : TAsmList; procdef : tprocdef; const externalname : string); override;
  139. end;
  140. tthumbcg64farm = class(tbasecg64farm)
  141. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  142. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  143. end;
  144. tthumb2cgarm = class(tcgarm)
  145. procedure init_register_allocators;override;
  146. procedure done_register_allocators;override;
  147. procedure a_call_reg(list : TAsmList;reg: tregister);override;
  148. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);override;
  149. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  150. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  151. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  152. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  153. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  154. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  155. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  156. function handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference; override;
  157. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  158. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  159. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  160. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize;intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  161. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize;mmreg, intreg: tregister; shuffle : pmmshuffle); override;
  162. end;
  163. tthumb2cg64farm = class(tcg64farm)
  164. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  165. end;
  166. const
  167. OpCmp2AsmCond : Array[topcmp] of TAsmCond = (C_NONE,C_EQ,C_GT,
  168. C_LT,C_GE,C_LE,C_NE,C_LS,C_CC,C_CS,C_HI);
  169. winstackpagesize = 4096;
  170. function get_fpu_postfix(def : tdef) : toppostfix;
  171. procedure create_codegen;
  172. implementation
  173. uses
  174. globals,verbose,systems,cutils,
  175. aopt,aoptcpu,
  176. fmodule,
  177. symconst,symsym,symtable,
  178. tgobj,
  179. procinfo,cpupi,
  180. paramgr;
  181. function get_fpu_postfix(def : tdef) : toppostfix;
  182. begin
  183. if def.typ=floatdef then
  184. begin
  185. case tfloatdef(def).floattype of
  186. s32real:
  187. result:=PF_S;
  188. s64real:
  189. result:=PF_D;
  190. s80real:
  191. result:=PF_E;
  192. else
  193. internalerror(200401272);
  194. end;
  195. end
  196. else
  197. internalerror(200401271);
  198. end;
  199. procedure tarmcgarm.init_register_allocators;
  200. begin
  201. inherited init_register_allocators;
  202. { currently, we always save R14, so we can use it }
  203. if (target_info.system<>system_arm_darwin) then
  204. begin
  205. if assigned(current_procinfo) and (current_procinfo.framepointer<>NR_R11) then
  206. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  207. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R12,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  208. RS_R9,RS_R10,RS_R11,RS_R14],first_int_imreg,[])
  209. else
  210. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  211. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R12,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  212. RS_R9,RS_R10,RS_R14],first_int_imreg,[])
  213. end
  214. else
  215. { r7 is not available on Darwin, it's used as frame pointer (always,
  216. for backtrace support -- also in gcc/clang -> R11 can be used).
  217. r9 is volatile }
  218. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  219. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R9,RS_R12,RS_R4,RS_R5,RS_R6,RS_R8,
  220. RS_R10,RS_R11,RS_R14],first_int_imreg,[]);
  221. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  222. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  223. { The register allocator currently cannot deal with multiple
  224. non-overlapping subregs per register, so we can only use
  225. half the single precision registers for now (as sub registers of the
  226. double precision ones). }
  227. if current_settings.fputype=fpu_vfpv3 then
  228. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  229. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  230. RS_D16,RS_D17,RS_D18,RS_D19,RS_D20,RS_D21,RS_D22,RS_D23,RS_D24,RS_D25,RS_D26,RS_D27,RS_D28,RS_D29,RS_D30,RS_D31,
  231. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  232. ],first_mm_imreg,[])
  233. else
  234. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  235. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15],first_mm_imreg,[]);
  236. end;
  237. procedure tarmcgarm.done_register_allocators;
  238. begin
  239. rg[R_INTREGISTER].free;
  240. rg[R_FPUREGISTER].free;
  241. rg[R_MMREGISTER].free;
  242. inherited done_register_allocators;
  243. end;
  244. procedure tcgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  245. var
  246. imm_shift : byte;
  247. l : tasmlabel;
  248. hr : treference;
  249. imm1, imm2: DWord;
  250. begin
  251. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  252. internalerror(2002090902);
  253. if is_shifter_const(a,imm_shift) then
  254. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  255. else if is_shifter_const(not(a),imm_shift) then
  256. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  257. { loading of constants with mov and orr }
  258. else if (split_into_shifter_const(a,imm1, imm2)) then
  259. begin
  260. list.concat(taicpu.op_reg_const(A_MOV,reg, imm1));
  261. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg, imm2));
  262. end
  263. { loading of constants with mvn and bic }
  264. else if (split_into_shifter_const(not(a), imm1, imm2)) then
  265. begin
  266. list.concat(taicpu.op_reg_const(A_MVN,reg, imm1));
  267. list.concat(taicpu.op_reg_reg_const(A_BIC,reg,reg, imm2));
  268. end
  269. else
  270. begin
  271. reference_reset(hr,4);
  272. current_asmdata.getjumplabel(l);
  273. cg.a_label(current_procinfo.aktlocaldata,l);
  274. hr.symboldata:=current_procinfo.aktlocaldata.last;
  275. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  276. hr.symbol:=l;
  277. hr.base:=NR_PC;
  278. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  279. end;
  280. end;
  281. procedure tcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  282. var
  283. oppostfix:toppostfix;
  284. usedtmpref: treference;
  285. tmpreg,tmpreg2 : tregister;
  286. so : tshifterop;
  287. dir : integer;
  288. begin
  289. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  290. FromSize := ToSize;
  291. case FromSize of
  292. { signed integer registers }
  293. OS_8:
  294. oppostfix:=PF_B;
  295. OS_S8:
  296. oppostfix:=PF_SB;
  297. OS_16:
  298. oppostfix:=PF_H;
  299. OS_S16:
  300. oppostfix:=PF_SH;
  301. OS_32,
  302. OS_S32:
  303. oppostfix:=PF_None;
  304. else
  305. InternalError(200308297);
  306. end;
  307. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  308. begin
  309. if target_info.endian=endian_big then
  310. dir:=-1
  311. else
  312. dir:=1;
  313. case FromSize of
  314. OS_16,OS_S16:
  315. begin
  316. { only complicated references need an extra loadaddr }
  317. if assigned(ref.symbol) or
  318. (ref.index<>NR_NO) or
  319. (ref.offset<-4095) or
  320. (ref.offset>4094) or
  321. { sometimes the compiler reused registers }
  322. (reg=ref.index) or
  323. (reg=ref.base) then
  324. begin
  325. tmpreg2:=getintregister(list,OS_INT);
  326. a_loadaddr_ref_reg(list,ref,tmpreg2);
  327. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  328. end
  329. else
  330. usedtmpref:=ref;
  331. if target_info.endian=endian_big then
  332. inc(usedtmpref.offset,1);
  333. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  334. tmpreg:=getintregister(list,OS_INT);
  335. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  336. inc(usedtmpref.offset,dir);
  337. if FromSize=OS_16 then
  338. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  339. else
  340. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  341. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  342. end;
  343. OS_32,OS_S32:
  344. begin
  345. tmpreg:=getintregister(list,OS_INT);
  346. { only complicated references need an extra loadaddr }
  347. if assigned(ref.symbol) or
  348. (ref.index<>NR_NO) or
  349. (ref.offset<-4095) or
  350. (ref.offset>4092) or
  351. { sometimes the compiler reused registers }
  352. (reg=ref.index) or
  353. (reg=ref.base) then
  354. begin
  355. tmpreg2:=getintregister(list,OS_INT);
  356. a_loadaddr_ref_reg(list,ref,tmpreg2);
  357. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  358. end
  359. else
  360. usedtmpref:=ref;
  361. shifterop_reset(so);so.shiftmode:=SM_LSL;
  362. if ref.alignment=2 then
  363. begin
  364. if target_info.endian=endian_big then
  365. inc(usedtmpref.offset,2);
  366. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  367. inc(usedtmpref.offset,dir*2);
  368. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  369. so.shiftimm:=16;
  370. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  371. end
  372. else
  373. begin
  374. tmpreg2:=getintregister(list,OS_INT);
  375. if target_info.endian=endian_big then
  376. inc(usedtmpref.offset,3);
  377. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  378. inc(usedtmpref.offset,dir);
  379. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  380. inc(usedtmpref.offset,dir);
  381. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg2);
  382. so.shiftimm:=8;
  383. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  384. inc(usedtmpref.offset,dir);
  385. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  386. so.shiftimm:=16;
  387. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg2,so));
  388. so.shiftimm:=24;
  389. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  390. end;
  391. end
  392. else
  393. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  394. end;
  395. end
  396. else
  397. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  398. if (fromsize=OS_S8) and (tosize = OS_16) then
  399. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  400. end;
  401. procedure tcgarm.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  402. var
  403. hsym : tsym;
  404. href : treference;
  405. paraloc : Pcgparalocation;
  406. shift : byte;
  407. begin
  408. { calculate the parameter info for the procdef }
  409. procdef.init_paraloc_info(callerside);
  410. hsym:=tsym(procdef.parast.Find('self'));
  411. if not(assigned(hsym) and
  412. (hsym.typ=paravarsym)) then
  413. internalerror(200305251);
  414. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  415. while paraloc<>nil do
  416. with paraloc^ do
  417. begin
  418. case loc of
  419. LOC_REGISTER:
  420. begin
  421. if is_shifter_const(ioffset,shift) then
  422. a_op_const_reg(list,OP_SUB,size,ioffset,register)
  423. else
  424. begin
  425. a_load_const_reg(list,OS_ADDR,ioffset,NR_R12);
  426. a_op_reg_reg(list,OP_SUB,size,NR_R12,register);
  427. end;
  428. end;
  429. LOC_REFERENCE:
  430. begin
  431. { offset in the wrapper needs to be adjusted for the stored
  432. return address }
  433. reference_reset_base(href,reference.index,reference.offset+sizeof(aint),sizeof(pint));
  434. if is_shifter_const(ioffset,shift) then
  435. a_op_const_ref(list,OP_SUB,size,ioffset,href)
  436. else
  437. begin
  438. a_load_const_reg(list,OS_ADDR,ioffset,NR_R12);
  439. a_op_reg_ref(list,OP_SUB,size,NR_R12,href);
  440. end;
  441. end
  442. else
  443. internalerror(200309189);
  444. end;
  445. paraloc:=next;
  446. end;
  447. end;
  448. procedure tbasecgarm.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const paraloc : TCGPara);
  449. var
  450. ref: treference;
  451. begin
  452. paraloc.check_simple_location;
  453. paramanager.allocparaloc(list,paraloc.location);
  454. case paraloc.location^.loc of
  455. LOC_REGISTER,LOC_CREGISTER:
  456. a_load_const_reg(list,size,a,paraloc.location^.register);
  457. LOC_REFERENCE:
  458. begin
  459. reference_reset(ref,paraloc.alignment);
  460. ref.base:=paraloc.location^.reference.index;
  461. ref.offset:=paraloc.location^.reference.offset;
  462. a_load_const_ref(list,size,a,ref);
  463. end;
  464. else
  465. internalerror(2002081101);
  466. end;
  467. end;
  468. procedure tbasecgarm.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);
  469. var
  470. tmpref, ref: treference;
  471. location: pcgparalocation;
  472. sizeleft: aint;
  473. begin
  474. location := paraloc.location;
  475. tmpref := r;
  476. sizeleft := paraloc.intsize;
  477. while assigned(location) do
  478. begin
  479. paramanager.allocparaloc(list,location);
  480. case location^.loc of
  481. LOC_REGISTER,LOC_CREGISTER:
  482. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  483. LOC_REFERENCE:
  484. begin
  485. reference_reset_base(ref,location^.reference.index,location^.reference.offset,paraloc.alignment);
  486. { doubles in softemu mode have a strange order of registers and references }
  487. if location^.size=OS_32 then
  488. g_concatcopy(list,tmpref,ref,4)
  489. else
  490. begin
  491. g_concatcopy(list,tmpref,ref,sizeleft);
  492. if assigned(location^.next) then
  493. internalerror(2005010710);
  494. end;
  495. end;
  496. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  497. case location^.size of
  498. OS_F32, OS_F64:
  499. a_loadfpu_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  500. else
  501. internalerror(2002072801);
  502. end;
  503. LOC_VOID:
  504. begin
  505. // nothing to do
  506. end;
  507. else
  508. internalerror(2002081103);
  509. end;
  510. inc(tmpref.offset,tcgsize2size[location^.size]);
  511. dec(sizeleft,tcgsize2size[location^.size]);
  512. location := location^.next;
  513. end;
  514. end;
  515. procedure tbasecgarm.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : TCGPara);
  516. var
  517. ref: treference;
  518. tmpreg: tregister;
  519. begin
  520. paraloc.check_simple_location;
  521. paramanager.allocparaloc(list,paraloc.location);
  522. case paraloc.location^.loc of
  523. LOC_REGISTER,LOC_CREGISTER:
  524. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  525. LOC_REFERENCE:
  526. begin
  527. reference_reset(ref,paraloc.alignment);
  528. ref.base := paraloc.location^.reference.index;
  529. ref.offset := paraloc.location^.reference.offset;
  530. tmpreg := getintregister(list,OS_ADDR);
  531. a_loadaddr_ref_reg(list,r,tmpreg);
  532. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  533. end;
  534. else
  535. internalerror(2002080701);
  536. end;
  537. end;
  538. procedure tbasecgarm.a_call_name(list : TAsmList;const s : string; weak: boolean);
  539. var
  540. branchopcode: tasmop;
  541. r : treference;
  542. sym : TAsmSymbol;
  543. begin
  544. { check not really correct: should only be used for non-Thumb cpus }
  545. if CPUARM_HAS_BLX_LABEL in cpu_capabilities[current_settings.cputype] then
  546. branchopcode:=A_BLX
  547. else
  548. branchopcode:=A_BL;
  549. if not(weak) then
  550. sym:=current_asmdata.RefAsmSymbol(s)
  551. else
  552. sym:=current_asmdata.WeakRefAsmSymbol(s);
  553. reference_reset_symbol(r,sym,0,sizeof(pint));
  554. if (tf_pic_uses_got in target_info.flags) and
  555. (cs_create_pic in current_settings.moduleswitches) then
  556. begin
  557. include(current_procinfo.flags,pi_needs_got);
  558. r.refaddr:=addr_pic
  559. end
  560. else
  561. r.refaddr:=addr_full;
  562. list.concat(taicpu.op_ref(branchopcode,r));
  563. {
  564. the compiler does not properly set this flag anymore in pass 1, and
  565. for now we only need it after pass 2 (I hope) (JM)
  566. if not(pi_do_call in current_procinfo.flags) then
  567. internalerror(2003060703);
  568. }
  569. include(current_procinfo.flags,pi_do_call);
  570. end;
  571. procedure tbasecgarm.a_call_reg(list : TAsmList;reg: tregister);
  572. begin
  573. { check not really correct: should only be used for non-Thumb cpus }
  574. if not(CPUARM_HAS_BLX in cpu_capabilities[current_settings.cputype]) then
  575. begin
  576. list.concat(taicpu.op_reg_reg(A_MOV,NR_R14,NR_PC));
  577. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,reg));
  578. end
  579. else
  580. list.concat(taicpu.op_reg(A_BLX, reg));
  581. {
  582. the compiler does not properly set this flag anymore in pass 1, and
  583. for now we only need it after pass 2 (I hope) (JM)
  584. if not(pi_do_call in current_procinfo.flags) then
  585. internalerror(2003060703);
  586. }
  587. include(current_procinfo.flags,pi_do_call);
  588. end;
  589. procedure tcgarm.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  590. begin
  591. a_op_const_reg_reg(list,op,size,a,reg,reg);
  592. end;
  593. procedure tcgarm.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  594. var
  595. tmpreg,tmpresreg : tregister;
  596. tmpref : treference;
  597. begin
  598. tmpreg:=getintregister(list,size);
  599. tmpresreg:=getintregister(list,size);
  600. tmpref:=a_internal_load_ref_reg(list,size,size,ref,tmpreg);
  601. a_op_const_reg_reg(list,op,size,a,tmpreg,tmpresreg);
  602. a_load_reg_ref(list,size,size,tmpresreg,tmpref);
  603. end;
  604. procedure tcgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  605. var
  606. so : tshifterop;
  607. begin
  608. if op = OP_NEG then
  609. begin
  610. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,src,0));
  611. maybeadjustresult(list,OP_NEG,size,dst);
  612. end
  613. else if op = OP_NOT then
  614. begin
  615. if size in [OS_8, OS_16, OS_S8, OS_S16] then
  616. begin
  617. shifterop_reset(so);
  618. so.shiftmode:=SM_LSL;
  619. if size in [OS_8, OS_S8] then
  620. so.shiftimm:=24
  621. else
  622. so.shiftimm:=16;
  623. list.concat(taicpu.op_reg_reg_shifterop(A_MVN,dst,src,so));
  624. {Using a shift here allows this to be folded into another instruction}
  625. if size in [OS_S8, OS_S16] then
  626. so.shiftmode:=SM_ASR
  627. else
  628. so.shiftmode:=SM_LSR;
  629. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,dst,so));
  630. end
  631. else
  632. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  633. end
  634. else
  635. a_op_reg_reg_reg(list,op,size,src,dst,dst);
  636. end;
  637. const
  638. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  639. (A_NONE,A_MOV,A_ADD,A_AND,A_NONE,A_NONE,A_MUL,A_MUL,A_NONE,A_NONE,A_ORR,
  640. A_NONE,A_NONE,A_NONE,A_SUB,A_EOR,A_NONE,A_NONE);
  641. op_reg_opcg2asmop: array[TOpCG] of tasmop =
  642. (A_NONE,A_MOV,A_ADD,A_AND,A_NONE,A_NONE,A_MUL,A_MUL,A_NONE,A_NONE,A_ORR,
  643. A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_NONE,A_ROR);
  644. op_reg_postfix: array[TOpCG] of TOpPostfix =
  645. (PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,
  646. PF_None,PF_None,PF_None,PF_None,PF_None,PF_None,PF_None);
  647. procedure tcgarm.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  648. size: tcgsize; a: tcgint; src, dst: tregister);
  649. var
  650. ovloc : tlocation;
  651. begin
  652. a_op_const_reg_reg_checkoverflow(list,op,size,a,src,dst,false,ovloc);
  653. end;
  654. procedure tcgarm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  655. size: tcgsize; src1, src2, dst: tregister);
  656. var
  657. ovloc : tlocation;
  658. begin
  659. a_op_reg_reg_reg_checkoverflow(list,op,size,src1,src2,dst,false,ovloc);
  660. end;
  661. function opshift2shiftmode(op: TOpCg): tshiftmode;
  662. begin
  663. case op of
  664. OP_SHL: Result:=SM_LSL;
  665. OP_SHR: Result:=SM_LSR;
  666. OP_ROR: Result:=SM_ROR;
  667. OP_ROL: Result:=SM_ROR;
  668. OP_SAR: Result:=SM_ASR;
  669. else internalerror(2012070501);
  670. end
  671. end;
  672. function tbasecgarm.try_optimized_mul32_const_reg_reg(list: TAsmList; a: tcgint; src, dst: tregister) : boolean;
  673. var
  674. multiplier : dword;
  675. power : longint;
  676. shifterop : tshifterop;
  677. bitsset : byte;
  678. negative : boolean;
  679. first : boolean;
  680. b,
  681. cycles : byte;
  682. maxeffort : byte;
  683. begin
  684. result:=true;
  685. cycles:=0;
  686. negative:=a<0;
  687. shifterop.rs:=NR_NO;
  688. shifterop.shiftmode:=SM_LSL;
  689. if negative then
  690. inc(cycles);
  691. multiplier:=dword(abs(a));
  692. bitsset:=popcnt(multiplier and $fffffffe);
  693. { heuristics to estimate how much instructions are reasonable to replace the mul,
  694. this is currently based on XScale timings }
  695. { in the simplest case, we need a mov to load the constant and a mul to carry out the
  696. actual multiplication, this requires min. 1+4 cycles
  697. because the first shift imm. might cause a stall and because we need more instructions
  698. when replacing the mul we generate max. 3 instructions to replace this mul }
  699. maxeffort:=3;
  700. { if the constant is not a shifter op, we need either some mov/mvn/bic/or sequence or
  701. a ldr, so generating one more operation to replace this is beneficial }
  702. if not(is_shifter_const(dword(a),b)) and not(is_shifter_const(not(dword(a)),b)) then
  703. inc(maxeffort);
  704. { if the upper 5 bits are all set or clear, mul is one cycle faster }
  705. if ((dword(a) and $f8000000)=0) or ((dword(a) and $f8000000)=$f8000000) then
  706. dec(maxeffort);
  707. { if the upper 17 bits are all set or clear, mul is another cycle faster }
  708. if ((dword(a) and $ffff8000)=0) or ((dword(a) and $ffff8000)=$ffff8000) then
  709. dec(maxeffort);
  710. { most simple cases }
  711. if a=1 then
  712. a_load_reg_reg(list,OS_32,OS_32,src,dst)
  713. else if a=0 then
  714. a_load_const_reg(list,OS_32,0,dst)
  715. else if a=-1 then
  716. a_op_reg_reg(list,OP_NEG,OS_32,src,dst)
  717. { add up ?
  718. basically, one add is needed for each bit being set in the constant factor
  719. however, the least significant bit is for free, it can be hidden in the initial
  720. instruction
  721. }
  722. else if (bitsset+cycles<=maxeffort) and
  723. (bitsset<=popcnt(dword(nextpowerof2(multiplier,power)-multiplier) and $fffffffe)) then
  724. begin
  725. first:=true;
  726. while multiplier<>0 do
  727. begin
  728. shifterop.shiftimm:=BsrDWord(multiplier);
  729. if odd(multiplier) then
  730. begin
  731. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,shifterop));
  732. dec(multiplier);
  733. end
  734. else
  735. if first then
  736. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,shifterop))
  737. else
  738. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,dst,src,shifterop));
  739. first:=false;
  740. dec(multiplier,1 shl shifterop.shiftimm);
  741. end;
  742. if negative then
  743. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,dst,0));
  744. end
  745. { subtract from the next greater power of two? }
  746. else if popcnt(dword(nextpowerof2(multiplier,power)-multiplier) and $fffffffe)+cycles+1<=maxeffort then
  747. begin
  748. first:=true;
  749. while multiplier<>0 do
  750. begin
  751. if first then
  752. begin
  753. multiplier:=(1 shl power)-multiplier;
  754. shifterop.shiftimm:=power;
  755. end
  756. else
  757. shifterop.shiftimm:=BsrDWord(multiplier);
  758. if odd(multiplier) then
  759. begin
  760. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,shifterop));
  761. dec(multiplier);
  762. end
  763. else
  764. if first then
  765. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,shifterop))
  766. else
  767. begin
  768. list.concat(taicpu.op_reg_reg_reg_shifterop(A_SUB,dst,dst,src,shifterop));
  769. dec(multiplier,1 shl shifterop.shiftimm);
  770. end;
  771. first:=false;
  772. end;
  773. if negative then
  774. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,dst,0));
  775. end
  776. else
  777. result:=false;
  778. end;
  779. procedure tcgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  780. var
  781. shift, lsb, width : byte;
  782. tmpreg : tregister;
  783. so : tshifterop;
  784. l1 : longint;
  785. imm1, imm2: DWord;
  786. begin
  787. optimize_op_const(size, op, a);
  788. case op of
  789. OP_NONE:
  790. begin
  791. if src <> dst then
  792. a_load_reg_reg(list, size, size, src, dst);
  793. exit;
  794. end;
  795. OP_MOVE:
  796. begin
  797. a_load_const_reg(list, size, a, dst);
  798. exit;
  799. end;
  800. end;
  801. ovloc.loc:=LOC_VOID;
  802. if {$ifopt R+}(a<>-2147483648) and{$endif} not setflags and is_shifter_const(-a,shift) then
  803. case op of
  804. OP_ADD:
  805. begin
  806. op:=OP_SUB;
  807. a:=aint(dword(-a));
  808. end;
  809. OP_SUB:
  810. begin
  811. op:=OP_ADD;
  812. a:=aint(dword(-a));
  813. end
  814. end;
  815. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  816. case op of
  817. OP_NEG,OP_NOT:
  818. internalerror(200308281);
  819. OP_SHL,
  820. OP_SHR,
  821. OP_ROL,
  822. OP_ROR,
  823. OP_SAR:
  824. begin
  825. if a>32 then
  826. internalerror(200308294);
  827. shifterop_reset(so);
  828. so.shiftmode:=opshift2shiftmode(op);
  829. if op = OP_ROL then
  830. so.shiftimm:=32-a
  831. else
  832. so.shiftimm:=a;
  833. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  834. end;
  835. else
  836. {if (op in [OP_SUB, OP_ADD]) and
  837. ((a < 0) or
  838. (a > 4095)) then
  839. begin
  840. tmpreg:=getintregister(list,size);
  841. list.concat(taicpu.op_reg_const(A_MOVT, tmpreg, (a shr 16) and $FFFF));
  842. list.concat(taicpu.op_reg_const(A_MOV, tmpreg, a and $FFFF));
  843. list.concat(setoppostfix(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src,tmpreg),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))
  844. ));
  845. end
  846. else}
  847. begin
  848. if cgsetflags or setflags then
  849. a_reg_alloc(list,NR_DEFAULTFLAGS);
  850. list.concat(setoppostfix(
  851. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  852. end;
  853. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  854. begin
  855. ovloc.loc:=LOC_FLAGS;
  856. case op of
  857. OP_ADD:
  858. ovloc.resflags:=F_CS;
  859. OP_SUB:
  860. ovloc.resflags:=F_CC;
  861. end;
  862. end;
  863. end
  864. else
  865. begin
  866. { there could be added some more sophisticated optimizations }
  867. if (op in [OP_IMUL,OP_IDIV]) and (a=-1) then
  868. a_op_reg_reg(list,OP_NEG,size,src,dst)
  869. { we do this here instead in the peephole optimizer because
  870. it saves us a register }
  871. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  872. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  873. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  874. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  875. begin
  876. if l1>32 then{roozbeh does this ever happen?}
  877. internalerror(200308296);
  878. shifterop_reset(so);
  879. so.shiftmode:=SM_LSL;
  880. so.shiftimm:=l1;
  881. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  882. end
  883. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  884. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  885. begin
  886. if l1>32 then{does this ever happen?}
  887. internalerror(201205181);
  888. shifterop_reset(so);
  889. so.shiftmode:=SM_LSL;
  890. so.shiftimm:=l1;
  891. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,so));
  892. end
  893. else if (op in [OP_MUL,OP_IMUL]) and not(cgsetflags or setflags) and try_optimized_mul32_const_reg_reg(list,a,src,dst) then
  894. begin
  895. { nothing to do on success }
  896. end
  897. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  898. broader range of shifterconstants.}
  899. else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  900. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))
  901. { Doing two shifts instead of two bics might allow the peephole optimizer to fold the second shift
  902. into the following instruction}
  903. else if (op = OP_AND) and
  904. is_continuous_mask(a, lsb, width) and
  905. ((lsb = 0) or ((lsb + width) = 32)) then
  906. begin
  907. shifterop_reset(so);
  908. if (width = 16) and
  909. (lsb = 0) and
  910. (current_settings.cputype >= cpu_armv6) then
  911. list.concat(taicpu.op_reg_reg(A_UXTH,dst,src))
  912. else if (width = 8) and
  913. (lsb = 0) and
  914. (current_settings.cputype >= cpu_armv6) then
  915. list.concat(taicpu.op_reg_reg(A_UXTB,dst,src))
  916. else if lsb = 0 then
  917. begin
  918. so.shiftmode:=SM_LSL;
  919. so.shiftimm:=32-width;
  920. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  921. so.shiftmode:=SM_LSR;
  922. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,dst,so));
  923. end
  924. else
  925. begin
  926. so.shiftmode:=SM_LSR;
  927. so.shiftimm:=lsb;
  928. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  929. so.shiftmode:=SM_LSL;
  930. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,dst,so));
  931. end;
  932. end
  933. else if (op = OP_AND) and split_into_shifter_const(not(dword(a)), imm1, imm2) then
  934. begin
  935. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,imm1));
  936. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm2));
  937. end
  938. else if (op in [OP_ADD, OP_SUB, OP_OR, OP_XOR]) and
  939. not(cgsetflags or setflags) and
  940. split_into_shifter_const(a, imm1, imm2) then
  941. begin
  942. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,imm1));
  943. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm2));
  944. end
  945. else
  946. begin
  947. tmpreg:=getintregister(list,size);
  948. a_load_const_reg(list,size,a,tmpreg);
  949. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  950. end;
  951. end;
  952. maybeadjustresult(list,op,size,dst);
  953. end;
  954. procedure tcgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  955. var
  956. so : tshifterop;
  957. tmpreg,overflowreg : tregister;
  958. asmop : tasmop;
  959. begin
  960. ovloc.loc:=LOC_VOID;
  961. case op of
  962. OP_NEG,OP_NOT,
  963. OP_DIV,OP_IDIV:
  964. internalerror(200308283);
  965. OP_SHL,
  966. OP_SHR,
  967. OP_SAR,
  968. OP_ROR:
  969. begin
  970. if (op = OP_ROR) and not(size in [OS_32,OS_S32]) then
  971. internalerror(2008072801);
  972. shifterop_reset(so);
  973. so.rs:=src1;
  974. so.shiftmode:=opshift2shiftmode(op);
  975. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  976. end;
  977. OP_ROL:
  978. begin
  979. if not(size in [OS_32,OS_S32]) then
  980. internalerror(2008072801);
  981. { simulate ROL by ror'ing 32-value }
  982. tmpreg:=getintregister(list,OS_32);
  983. list.concat(taicpu.op_reg_reg_const(A_RSB,tmpreg,src1, 32));
  984. shifterop_reset(so);
  985. so.rs:=tmpreg;
  986. so.shiftmode:=SM_ROR;
  987. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  988. end;
  989. OP_IMUL,
  990. OP_MUL:
  991. begin
  992. if cgsetflags or setflags then
  993. begin
  994. overflowreg:=getintregister(list,size);
  995. if op=OP_IMUL then
  996. asmop:=A_SMULL
  997. else
  998. asmop:=A_UMULL;
  999. { the arm doesn't allow that rd and rm are the same }
  1000. if dst=src2 then
  1001. begin
  1002. if dst<>src1 then
  1003. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  1004. else
  1005. begin
  1006. tmpreg:=getintregister(list,size);
  1007. a_load_reg_reg(list,size,size,src2,dst);
  1008. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  1009. end;
  1010. end
  1011. else
  1012. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  1013. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1014. if op=OP_IMUL then
  1015. begin
  1016. shifterop_reset(so);
  1017. so.shiftmode:=SM_ASR;
  1018. so.shiftimm:=31;
  1019. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  1020. end
  1021. else
  1022. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  1023. ovloc.loc:=LOC_FLAGS;
  1024. ovloc.resflags:=F_NE;
  1025. end
  1026. else
  1027. begin
  1028. { the arm doesn't allow that rd and rm are the same }
  1029. if dst=src2 then
  1030. begin
  1031. if dst<>src1 then
  1032. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  1033. else
  1034. begin
  1035. tmpreg:=getintregister(list,size);
  1036. a_load_reg_reg(list,size,size,src2,dst);
  1037. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  1038. end;
  1039. end
  1040. else
  1041. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  1042. end;
  1043. end;
  1044. else
  1045. begin
  1046. if cgsetflags or setflags then
  1047. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1048. list.concat(setoppostfix(
  1049. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  1050. end;
  1051. end;
  1052. maybeadjustresult(list,op,size,dst);
  1053. end;
  1054. procedure tcgarm.a_mul_reg_reg_pair(list: tasmlist; size: tcgsize; src1,src2,dstlo,dsthi: tregister);
  1055. var
  1056. asmop: tasmop;
  1057. begin
  1058. list.concat(tai_comment.create(strpnew('tcgarm.a_mul_reg_reg_pair called')));
  1059. case size of
  1060. OS_32: asmop:=A_UMULL;
  1061. OS_S32: asmop:=A_SMULL;
  1062. else
  1063. InternalError(2014060802);
  1064. end;
  1065. { The caller might omit dstlo or dsthi, when he is not interested in it, we still
  1066. need valid registers everywhere. In case of dsthi = NR_NO we could fall back to
  1067. 32x32=32 bit multiplication}
  1068. if (dstlo = NR_NO) then
  1069. dstlo:=getintregister(list,size);
  1070. if (dsthi = NR_NO) then
  1071. dsthi:=getintregister(list,size);
  1072. list.concat(taicpu.op_reg_reg_reg_reg(asmop, dstlo, dsthi, src1,src2));
  1073. end;
  1074. function tbasecgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  1075. var
  1076. tmpreg1,tmpreg2 : tregister;
  1077. tmpref : treference;
  1078. l : tasmlabel;
  1079. begin
  1080. tmpreg1:=NR_NO;
  1081. { Be sure to have a base register }
  1082. if (ref.base=NR_NO) then
  1083. begin
  1084. if ref.shiftmode<>SM_None then
  1085. internalerror(2014020701);
  1086. ref.base:=ref.index;
  1087. ref.index:=NR_NO;
  1088. end;
  1089. { absolute symbols can't be handled directly, we've to store the symbol reference
  1090. in the text segment and access it pc relative
  1091. For now, we assume that references where base or index equals to PC are already
  1092. relative, all other references are assumed to be absolute and thus they need
  1093. to be handled extra.
  1094. A proper solution would be to change refoptions to a set and store the information
  1095. if the symbol is absolute or relative there.
  1096. }
  1097. if (assigned(ref.symbol) and
  1098. not(is_pc(ref.base)) and
  1099. not(is_pc(ref.index))
  1100. ) or
  1101. { [#xxx] isn't a valid address operand }
  1102. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  1103. (ref.offset<-4095) or
  1104. (ref.offset>4095) or
  1105. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  1106. ((ref.offset<-255) or
  1107. (ref.offset>255)
  1108. )
  1109. ) or
  1110. (((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and
  1111. ((ref.offset<-1020) or
  1112. (ref.offset>1020) or
  1113. ((abs(ref.offset) mod 4)<>0)
  1114. )
  1115. ) or
  1116. ((GenerateThumbCode) and
  1117. (((oppostfix in [PF_SB,PF_SH]) and (ref.offset<>0)) or
  1118. ((oppostfix=PF_None) and ((ref.offset<0) or ((ref.base<>NR_STACK_POINTER_REG) and (ref.offset>124)) or
  1119. ((ref.base=NR_STACK_POINTER_REG) and (ref.offset>1020)) or ((ref.offset mod 4)<>0))) or
  1120. ((oppostfix=PF_H) and ((ref.offset<0) or (ref.offset>62) or ((ref.offset mod 2)<>0) or ((getsupreg(ref.base) in [RS_R8..RS_R15]) and (ref.offset<>0)))) or
  1121. ((oppostfix=PF_B) and ((ref.offset<0) or (ref.offset>31) or ((getsupreg(ref.base) in [RS_R8..RS_R15]) and (ref.offset<>0))))
  1122. )
  1123. ) then
  1124. begin
  1125. fixref(list,ref);
  1126. end;
  1127. if GenerateThumbCode then
  1128. begin
  1129. { certain thumb load require base and index }
  1130. if (oppostfix in [PF_SB,PF_SH]) and
  1131. (ref.base<>NR_NO) and (ref.index=NR_NO) then
  1132. begin
  1133. tmpreg1:=getintregister(list,OS_ADDR);
  1134. a_load_const_reg(list,OS_ADDR,0,tmpreg1);
  1135. ref.index:=tmpreg1;
  1136. end;
  1137. { "hi" registers cannot be used as base or index }
  1138. if (getsupreg(ref.base) in [RS_R8..RS_R12,RS_R14]) or
  1139. ((ref.base=NR_R13) and (ref.index<>NR_NO)) then
  1140. begin
  1141. tmpreg1:=getintregister(list,OS_ADDR);
  1142. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref.base,tmpreg1);
  1143. ref.base:=tmpreg1;
  1144. end;
  1145. if getsupreg(ref.index) in [RS_R8..RS_R14] then
  1146. begin
  1147. tmpreg1:=getintregister(list,OS_ADDR);
  1148. a_load_reg_reg(list,OS_ADDR,OS_ADDR,ref.index,tmpreg1);
  1149. ref.index:=tmpreg1;
  1150. end;
  1151. end;
  1152. { fold if there is base, index and offset, however, don't fold
  1153. for vfp memory instructions because we later fold the index }
  1154. if not((op in [A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and
  1155. (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  1156. begin
  1157. if tmpreg1<>NR_NO then
  1158. begin
  1159. tmpreg2:=getintregister(list,OS_ADDR);
  1160. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg1,tmpreg2);
  1161. tmpreg1:=tmpreg2;
  1162. end
  1163. else
  1164. begin
  1165. tmpreg1:=getintregister(list,OS_ADDR);
  1166. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg1);
  1167. ref.base:=tmpreg1;
  1168. end;
  1169. ref.offset:=0;
  1170. end;
  1171. { floating point operations have only limited references
  1172. we expect here, that a base is already set }
  1173. if ((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and (ref.index<>NR_NO) then
  1174. begin
  1175. if ref.shiftmode<>SM_none then
  1176. internalerror(200309121);
  1177. if tmpreg1<>NR_NO then
  1178. begin
  1179. if ref.base=tmpreg1 then
  1180. begin
  1181. if ref.signindex<0 then
  1182. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg1,tmpreg1,ref.index))
  1183. else
  1184. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg1,tmpreg1,ref.index));
  1185. ref.index:=NR_NO;
  1186. end
  1187. else
  1188. begin
  1189. if ref.index<>tmpreg1 then
  1190. internalerror(200403161);
  1191. if ref.signindex<0 then
  1192. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg1,ref.base,tmpreg1))
  1193. else
  1194. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg1,ref.base,tmpreg1));
  1195. ref.base:=tmpreg1;
  1196. ref.index:=NR_NO;
  1197. end;
  1198. end
  1199. else
  1200. begin
  1201. tmpreg1:=getintregister(list,OS_ADDR);
  1202. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg1,ref.base,ref.index));
  1203. ref.base:=tmpreg1;
  1204. ref.index:=NR_NO;
  1205. end;
  1206. end;
  1207. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  1208. Result := ref;
  1209. end;
  1210. procedure tbasecgarm.a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);
  1211. var
  1212. oppostfix:toppostfix;
  1213. usedtmpref: treference;
  1214. tmpreg : tregister;
  1215. dir : integer;
  1216. begin
  1217. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  1218. FromSize := ToSize;
  1219. case ToSize of
  1220. { signed integer registers }
  1221. OS_8,
  1222. OS_S8:
  1223. oppostfix:=PF_B;
  1224. OS_16,
  1225. OS_S16:
  1226. oppostfix:=PF_H;
  1227. OS_32,
  1228. OS_S32,
  1229. { for vfp value stored in integer register }
  1230. OS_F32:
  1231. oppostfix:=PF_None;
  1232. else
  1233. InternalError(200308299);
  1234. end;
  1235. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[tosize]) then
  1236. begin
  1237. if target_info.endian=endian_big then
  1238. dir:=-1
  1239. else
  1240. dir:=1;
  1241. case FromSize of
  1242. OS_16,OS_S16:
  1243. begin
  1244. tmpreg:=getintregister(list,OS_INT);
  1245. usedtmpref:=ref;
  1246. if target_info.endian=endian_big then
  1247. inc(usedtmpref.offset,1);
  1248. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,usedtmpref);
  1249. inc(usedtmpref.offset,dir);
  1250. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,reg,tmpreg);
  1251. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1252. end;
  1253. OS_32,OS_S32:
  1254. begin
  1255. tmpreg:=getintregister(list,OS_INT);
  1256. usedtmpref:=ref;
  1257. if ref.alignment=2 then
  1258. begin
  1259. if target_info.endian=endian_big then
  1260. inc(usedtmpref.offset,2);
  1261. usedtmpref:=a_internal_load_reg_ref(list,OS_16,OS_16,reg,usedtmpref);
  1262. a_op_const_reg_reg(list,OP_SHR,OS_INT,16,reg,tmpreg);
  1263. inc(usedtmpref.offset,dir*2);
  1264. a_internal_load_reg_ref(list,OS_16,OS_16,tmpreg,usedtmpref);
  1265. end
  1266. else
  1267. begin
  1268. if target_info.endian=endian_big then
  1269. inc(usedtmpref.offset,3);
  1270. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,usedtmpref);
  1271. a_op_const_reg_reg(list,OP_SHR,OS_INT,8,reg,tmpreg);
  1272. inc(usedtmpref.offset,dir);
  1273. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1274. a_op_const_reg(list,OP_SHR,OS_INT,8,tmpreg);
  1275. inc(usedtmpref.offset,dir);
  1276. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1277. a_op_const_reg(list,OP_SHR,OS_INT,8,tmpreg);
  1278. inc(usedtmpref.offset,dir);
  1279. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1280. end;
  1281. end
  1282. else
  1283. handle_load_store(list,A_STR,oppostfix,reg,ref);
  1284. end;
  1285. end
  1286. else
  1287. handle_load_store(list,A_STR,oppostfix,reg,ref);
  1288. end;
  1289. function tbasecgarm.a_internal_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference):treference;
  1290. var
  1291. oppostfix:toppostfix;
  1292. begin
  1293. case ToSize of
  1294. { signed integer registers }
  1295. OS_8,
  1296. OS_S8:
  1297. oppostfix:=PF_B;
  1298. OS_16,
  1299. OS_S16:
  1300. oppostfix:=PF_H;
  1301. OS_32,
  1302. OS_S32:
  1303. oppostfix:=PF_None;
  1304. else
  1305. InternalError(2003082910);
  1306. end;
  1307. result:=handle_load_store(list,A_STR,oppostfix,reg,ref);
  1308. end;
  1309. function tbasecgarm.a_internal_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister):treference;
  1310. var
  1311. oppostfix:toppostfix;
  1312. begin
  1313. case FromSize of
  1314. { signed integer registers }
  1315. OS_8:
  1316. oppostfix:=PF_B;
  1317. OS_S8:
  1318. oppostfix:=PF_SB;
  1319. OS_16:
  1320. oppostfix:=PF_H;
  1321. OS_S16:
  1322. oppostfix:=PF_SH;
  1323. OS_32,
  1324. OS_S32:
  1325. oppostfix:=PF_None;
  1326. else
  1327. InternalError(200308291);
  1328. end;
  1329. result:=handle_load_store(list,A_LDR,oppostfix,reg,ref);
  1330. end;
  1331. procedure tbasecgarm.a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  1332. var
  1333. so : tshifterop;
  1334. procedure do_shift(shiftmode : tshiftmode; shiftimm : byte; reg : tregister);
  1335. begin
  1336. if GenerateThumbCode then
  1337. begin
  1338. case shiftmode of
  1339. SM_ASR:
  1340. a_op_const_reg_reg(list,OP_SAR,OS_32,shiftimm,reg,reg2);
  1341. SM_LSR:
  1342. a_op_const_reg_reg(list,OP_SHR,OS_32,shiftimm,reg,reg2);
  1343. SM_LSL:
  1344. a_op_const_reg_reg(list,OP_SHL,OS_32,shiftimm,reg,reg2);
  1345. else
  1346. internalerror(2013090301);
  1347. end;
  1348. end
  1349. else
  1350. begin
  1351. so.shiftmode:=shiftmode;
  1352. so.shiftimm:=shiftimm;
  1353. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg,so));
  1354. end;
  1355. end;
  1356. var
  1357. instr: taicpu;
  1358. conv_done: boolean;
  1359. begin
  1360. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1361. internalerror(2002090901);
  1362. conv_done:=false;
  1363. if tosize<>fromsize then
  1364. begin
  1365. shifterop_reset(so);
  1366. conv_done:=true;
  1367. if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  1368. fromsize:=tosize;
  1369. if current_settings.cputype<cpu_armv6 then
  1370. case fromsize of
  1371. OS_8:
  1372. if GenerateThumbCode then
  1373. a_op_const_reg_reg(list,OP_AND,OS_32,$ff,reg1,reg2)
  1374. else
  1375. list.concat(taicpu.op_reg_reg_const(A_AND,reg2,reg1,$ff));
  1376. OS_S8:
  1377. begin
  1378. do_shift(SM_LSL,24,reg1);
  1379. if tosize=OS_16 then
  1380. begin
  1381. do_shift(SM_ASR,8,reg2);
  1382. do_shift(SM_LSR,16,reg2);
  1383. end
  1384. else
  1385. do_shift(SM_ASR,24,reg2);
  1386. end;
  1387. OS_16:
  1388. begin
  1389. do_shift(SM_LSL,16,reg1);
  1390. do_shift(SM_LSR,16,reg2);
  1391. end;
  1392. OS_S16:
  1393. begin
  1394. do_shift(SM_LSL,16,reg1);
  1395. do_shift(SM_ASR,16,reg2)
  1396. end;
  1397. else
  1398. conv_done:=false;
  1399. end
  1400. else
  1401. case fromsize of
  1402. OS_8:
  1403. if GenerateThumbCode then
  1404. list.concat(taicpu.op_reg_reg(A_UXTB,reg2,reg1))
  1405. else
  1406. list.concat(taicpu.op_reg_reg_const(A_AND,reg2,reg1,$ff));
  1407. OS_S8:
  1408. begin
  1409. if tosize=OS_16 then
  1410. begin
  1411. so.shiftmode:=SM_ROR;
  1412. so.shiftimm:=16;
  1413. list.concat(taicpu.op_reg_reg_shifterop(A_SXTB16,reg2,reg1,so));
  1414. do_shift(SM_LSR,16,reg2);
  1415. end
  1416. else
  1417. list.concat(taicpu.op_reg_reg(A_SXTB,reg2,reg1));
  1418. end;
  1419. OS_16:
  1420. list.concat(taicpu.op_reg_reg(A_UXTH,reg2,reg1));
  1421. OS_S16:
  1422. list.concat(taicpu.op_reg_reg(A_SXTH,reg2,reg1));
  1423. else
  1424. conv_done:=false;
  1425. end
  1426. end;
  1427. if not conv_done and (reg1<>reg2) then
  1428. begin
  1429. { same size, only a register mov required }
  1430. instr:=taicpu.op_reg_reg(A_MOV,reg2,reg1);
  1431. list.Concat(instr);
  1432. { Notify the register allocator that we have written a move instruction so
  1433. it can try to eliminate it. }
  1434. add_move_instruction(instr);
  1435. end;
  1436. end;
  1437. procedure tbasecgarm.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);
  1438. var
  1439. href,href2 : treference;
  1440. hloc : pcgparalocation;
  1441. begin
  1442. href:=ref;
  1443. hloc:=paraloc.location;
  1444. while assigned(hloc) do
  1445. begin
  1446. case hloc^.loc of
  1447. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1448. begin
  1449. paramanager.allocparaloc(list,paraloc.location);
  1450. a_loadfpu_ref_reg(list,size,size,ref,hloc^.register);
  1451. end;
  1452. LOC_REGISTER :
  1453. case hloc^.size of
  1454. OS_32,
  1455. OS_F32:
  1456. begin
  1457. paramanager.allocparaloc(list,paraloc.location);
  1458. a_load_ref_reg(list,OS_32,OS_32,href,hloc^.register);
  1459. end;
  1460. OS_64,
  1461. OS_F64:
  1462. cg64.a_load64_ref_cgpara(list,href,paraloc);
  1463. else
  1464. a_load_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  1465. end;
  1466. LOC_REFERENCE :
  1467. begin
  1468. reference_reset_base(href2,hloc^.reference.index,hloc^.reference.offset,paraloc.alignment);
  1469. { concatcopy should choose the best way to copy the data }
  1470. g_concatcopy(list,href,href2,tcgsize2size[hloc^.size]);
  1471. end;
  1472. else
  1473. internalerror(200408241);
  1474. end;
  1475. inc(href.offset,tcgsize2size[hloc^.size]);
  1476. hloc:=hloc^.next;
  1477. end;
  1478. end;
  1479. procedure tbasecgarm.a_loadfpu_reg_reg(list: TAsmList; fromsize,tosize: tcgsize; reg1, reg2: tregister);
  1480. begin
  1481. list.concat(setoppostfix(taicpu.op_reg_reg(A_MVF,reg2,reg1),cgsize2fpuoppostfix[tosize]));
  1482. end;
  1483. procedure tbasecgarm.a_loadfpu_ref_reg(list: TAsmList; fromsize,tosize: tcgsize; const ref: treference; reg: tregister);
  1484. var
  1485. oppostfix:toppostfix;
  1486. begin
  1487. case fromsize of
  1488. OS_32,
  1489. OS_F32:
  1490. oppostfix:=PF_S;
  1491. OS_64,
  1492. OS_F64:
  1493. oppostfix:=PF_D;
  1494. OS_F80:
  1495. oppostfix:=PF_E;
  1496. else
  1497. InternalError(200309021);
  1498. end;
  1499. handle_load_store(list,A_LDF,oppostfix,reg,ref);
  1500. if fromsize<>tosize then
  1501. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  1502. end;
  1503. procedure tbasecgarm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  1504. var
  1505. oppostfix:toppostfix;
  1506. begin
  1507. case tosize of
  1508. OS_F32:
  1509. oppostfix:=PF_S;
  1510. OS_F64:
  1511. oppostfix:=PF_D;
  1512. OS_F80:
  1513. oppostfix:=PF_E;
  1514. else
  1515. InternalError(200309022);
  1516. end;
  1517. handle_load_store(list,A_STF,oppostfix,reg,ref);
  1518. end;
  1519. { comparison operations }
  1520. procedure tbasecgarm.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1521. l : tasmlabel);
  1522. var
  1523. tmpreg : tregister;
  1524. b : byte;
  1525. begin
  1526. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1527. if (not(GenerateThumbCode) and is_shifter_const(a,b)) or
  1528. ((GenerateThumbCode) and is_thumb_imm(a)) then
  1529. list.concat(taicpu.op_reg_const(A_CMP,reg,a))
  1530. { CMN reg,0 and CMN reg,$80000000 are different from CMP reg,$ffffffff
  1531. and CMP reg,$7fffffff regarding the flags according to the ARM manual }
  1532. else if (a<>$7fffffff) and (a<>-1) and not(GenerateThumbCode) and is_shifter_const(-a,b) then
  1533. list.concat(taicpu.op_reg_const(A_CMN,reg,-a))
  1534. else
  1535. begin
  1536. tmpreg:=getintregister(list,size);
  1537. a_load_const_reg(list,size,a,tmpreg);
  1538. list.concat(taicpu.op_reg_reg(A_CMP,reg,tmpreg));
  1539. end;
  1540. a_jmp_cond(list,cmp_op,l);
  1541. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1542. end;
  1543. procedure tbasecgarm.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: TCGSize; src, dst: TRegister);
  1544. begin
  1545. if reverse then
  1546. begin
  1547. list.Concat(taicpu.op_reg_reg(A_CLZ,dst,src));
  1548. list.Concat(taicpu.op_reg_reg_const(A_RSB,dst,dst,31));
  1549. list.Concat(taicpu.op_reg_reg_const(A_AND,dst,dst,255));
  1550. end
  1551. { it is decided during the compilation of the system unit if this code is used or not
  1552. so no additional check for rbit is needed }
  1553. else
  1554. begin
  1555. list.Concat(taicpu.op_reg_reg(A_RBIT,dst,src));
  1556. list.Concat(taicpu.op_reg_reg(A_CLZ,dst,dst));
  1557. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1558. list.Concat(taicpu.op_reg_const(A_CMP,dst,32));
  1559. if GenerateThumb2Code then
  1560. list.Concat(taicpu.op_cond(A_IT, C_EQ));
  1561. list.Concat(setcondition(taicpu.op_reg_const(A_MOV,dst,$ff),C_EQ));
  1562. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1563. end;
  1564. end;
  1565. procedure tbasecgarm.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1566. begin
  1567. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1568. list.concat(taicpu.op_reg_reg(A_CMP,reg2,reg1));
  1569. a_jmp_cond(list,cmp_op,l);
  1570. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1571. end;
  1572. procedure tbasecgarm.a_jmp_name(list : TAsmList;const s : string);
  1573. var
  1574. ai : taicpu;
  1575. begin
  1576. { generate far jump, leave it to the optimizer to get rid of it }
  1577. if GenerateThumbCode then
  1578. ai:=taicpu.op_sym(A_BL,current_asmdata.RefAsmSymbol(s))
  1579. else
  1580. ai:=taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s));
  1581. ai.is_jmp:=true;
  1582. list.concat(ai);
  1583. end;
  1584. procedure tbasecgarm.a_jmp_always(list : TAsmList;l: tasmlabel);
  1585. var
  1586. ai : taicpu;
  1587. begin
  1588. { generate far jump, leave it to the optimizer to get rid of it }
  1589. if GenerateThumbCode then
  1590. ai:=taicpu.op_sym(A_BL,l)
  1591. else
  1592. ai:=taicpu.op_sym(A_B,l);
  1593. ai.is_jmp:=true;
  1594. list.concat(ai);
  1595. end;
  1596. procedure tbasecgarm.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1597. var
  1598. ai : taicpu;
  1599. inv_flags : TResFlags;
  1600. hlabel : TAsmLabel;
  1601. begin
  1602. if GenerateThumbCode then
  1603. begin
  1604. inv_flags:=f;
  1605. inverse_flags(inv_flags);
  1606. { the optimizer has to fix this if jump range is sufficient short }
  1607. current_asmdata.getjumplabel(hlabel);
  1608. ai:=setcondition(taicpu.op_sym(A_B,hlabel),flags_to_cond(inv_flags));
  1609. ai.is_jmp:=true;
  1610. list.concat(ai);
  1611. a_jmp_always(list,l);
  1612. a_label(list,hlabel);
  1613. end
  1614. else
  1615. begin
  1616. ai:=setcondition(taicpu.op_sym(A_B,l),flags_to_cond(f));
  1617. ai.is_jmp:=true;
  1618. list.concat(ai);
  1619. end;
  1620. end;
  1621. procedure tbasecgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  1622. begin
  1623. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  1624. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  1625. end;
  1626. procedure tbasecgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1627. var
  1628. ref : treference;
  1629. shift : byte;
  1630. firstfloatreg,lastfloatreg,
  1631. r : byte;
  1632. mmregs,
  1633. regs, saveregs : tcpuregisterset;
  1634. registerarea,
  1635. r7offset,
  1636. stackmisalignment : pint;
  1637. postfix: toppostfix;
  1638. imm1, imm2: DWord;
  1639. stack_parameters : Boolean;
  1640. begin
  1641. LocalSize:=align(LocalSize,4);
  1642. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  1643. { call instruction does not put anything on the stack }
  1644. registerarea:=0;
  1645. tarmprocinfo(current_procinfo).stackpaddingreg:=High(TSuperRegister);
  1646. lastfloatreg:=RS_NO;
  1647. if not(nostackframe) then
  1648. begin
  1649. firstfloatreg:=RS_NO;
  1650. mmregs:=[];
  1651. case current_settings.fputype of
  1652. fpu_fpa,
  1653. fpu_fpa10,
  1654. fpu_fpa11:
  1655. begin
  1656. { save floating point registers? }
  1657. regs:=rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  1658. for r:=RS_F0 to RS_F7 do
  1659. if r in regs then
  1660. begin
  1661. if firstfloatreg=RS_NO then
  1662. firstfloatreg:=r;
  1663. lastfloatreg:=r;
  1664. inc(registerarea,12);
  1665. end;
  1666. end;
  1667. fpu_vfpv2,
  1668. fpu_vfpv3,
  1669. fpu_vfpv3_d16:
  1670. begin;
  1671. mmregs:=rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall);
  1672. end;
  1673. end;
  1674. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1675. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1676. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  1677. { save int registers }
  1678. reference_reset(ref,4);
  1679. ref.index:=NR_STACK_POINTER_REG;
  1680. ref.addressmode:=AM_PREINDEXED;
  1681. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1682. if not(target_info.system in systems_darwin) then
  1683. begin
  1684. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1685. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1686. begin
  1687. a_reg_alloc(list,NR_R12);
  1688. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  1689. end;
  1690. { the (old) ARM APCS requires saving both the stack pointer (to
  1691. crawl the stack) and the PC (to identify the function this
  1692. stack frame belongs to) -> also save R12 (= copy of R13 on entry)
  1693. and R15 -- still needs updating for EABI and Darwin, they don't
  1694. need that }
  1695. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1696. regs:=regs+[RS_FRAME_POINTER_REG,RS_R12,RS_R14,RS_R15]
  1697. else
  1698. if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  1699. include(regs,RS_R14);
  1700. if regs<>[] then
  1701. begin
  1702. for r:=RS_R0 to RS_R15 do
  1703. if r in regs then
  1704. inc(registerarea,4);
  1705. { if the stack is not 8 byte aligned, try to add an extra register,
  1706. so we can avoid the extra sub/add ...,#4 later (KB) }
  1707. if ((registerarea mod current_settings.alignment.localalignmax) <> 0) then
  1708. for r:=RS_R3 downto RS_R0 do
  1709. if not(r in regs) then
  1710. begin
  1711. regs:=regs+[r];
  1712. inc(registerarea,4);
  1713. tarmprocinfo(current_procinfo).stackpaddingreg:=r;
  1714. break;
  1715. end;
  1716. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  1717. end;
  1718. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1719. begin
  1720. { the framepointer now points to the saved R15, so the saved
  1721. framepointer is at R11-12 (for get_caller_frame) }
  1722. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  1723. a_reg_dealloc(list,NR_R12);
  1724. end;
  1725. end
  1726. else
  1727. begin
  1728. { always save r14 if we use r7 as the framepointer, because
  1729. the parameter offsets are hardcoded in advance and always
  1730. assume that r14 sits on the stack right behind the saved r7
  1731. }
  1732. if current_procinfo.framepointer=NR_FRAME_POINTER_REG then
  1733. include(regs,RS_FRAME_POINTER_REG);
  1734. if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  1735. include(regs,RS_R14);
  1736. if regs<>[] then
  1737. begin
  1738. { on Darwin, you first have to save [r4-r7,lr], and then
  1739. [r8,r10,r11] and make r7 point to the previously saved
  1740. r7 so that you can perform a stack crawl based on it
  1741. ([r7] is previous stack frame, [r7+4] is return address
  1742. }
  1743. include(regs,RS_FRAME_POINTER_REG);
  1744. saveregs:=regs-[RS_R8,RS_R10,RS_R11];
  1745. r7offset:=0;
  1746. for r:=RS_R0 to RS_R15 do
  1747. if r in saveregs then
  1748. begin
  1749. inc(registerarea,4);
  1750. if r<RS_FRAME_POINTER_REG then
  1751. inc(r7offset,4);
  1752. end;
  1753. { save the registers }
  1754. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  1755. { make r7 point to the saved r7 (regardless of whether this
  1756. frame uses the framepointer, for backtrace purposes) }
  1757. if r7offset<>0 then
  1758. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_FRAME_POINTER_REG,NR_R13,r7offset))
  1759. else
  1760. list.concat(taicpu.op_reg_reg(A_MOV,NR_R7,NR_R13));
  1761. { now save the rest (if any) }
  1762. saveregs:=regs-saveregs;
  1763. if saveregs<>[] then
  1764. begin
  1765. for r:=RS_R8 to RS_R11 do
  1766. if r in saveregs then
  1767. inc(registerarea,4);
  1768. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  1769. end;
  1770. end;
  1771. end;
  1772. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  1773. if (LocalSize<>0) or
  1774. ((stackmisalignment<>0) and
  1775. ((pi_do_call in current_procinfo.flags) or
  1776. (po_assembler in current_procinfo.procdef.procoptions))) then
  1777. begin
  1778. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  1779. if stack_parameters and (pi_estimatestacksize in current_procinfo.flags) then
  1780. begin
  1781. if localsize>tarmprocinfo(current_procinfo).stackframesize then
  1782. internalerror(2014030901)
  1783. else
  1784. localsize:=tarmprocinfo(current_procinfo).stackframesize-registerarea;
  1785. end;
  1786. if is_shifter_const(localsize,shift) then
  1787. begin
  1788. a_reg_dealloc(list,NR_R12);
  1789. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  1790. end
  1791. else if split_into_shifter_const(localsize, imm1, imm2) then
  1792. begin
  1793. a_reg_dealloc(list,NR_R12);
  1794. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm1));
  1795. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm2));
  1796. end
  1797. else
  1798. begin
  1799. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1800. a_reg_alloc(list,NR_R12);
  1801. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  1802. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1803. a_reg_dealloc(list,NR_R12);
  1804. end;
  1805. end;
  1806. if (mmregs<>[]) or
  1807. (firstfloatreg<>RS_NO) then
  1808. begin
  1809. reference_reset(ref,4);
  1810. if (tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023) or
  1811. (current_settings.fputype in [fpu_vfpv2,fpu_vfpv3,fpu_vfpv3_d16]) then
  1812. begin
  1813. if not is_shifter_const(tarmprocinfo(current_procinfo).floatregstart,shift) then
  1814. begin
  1815. a_reg_alloc(list,NR_R12);
  1816. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1817. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  1818. a_reg_dealloc(list,NR_R12);
  1819. end
  1820. else
  1821. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_R12,current_procinfo.framepointer,-tarmprocinfo(current_procinfo).floatregstart));
  1822. ref.base:=NR_R12;
  1823. end
  1824. else
  1825. begin
  1826. ref.base:=current_procinfo.framepointer;
  1827. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1828. end;
  1829. case current_settings.fputype of
  1830. fpu_fpa,
  1831. fpu_fpa10,
  1832. fpu_fpa11:
  1833. begin
  1834. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1835. lastfloatreg-firstfloatreg+1,ref));
  1836. end;
  1837. fpu_vfpv2,
  1838. fpu_vfpv3,
  1839. fpu_vfpv3_d16:
  1840. begin
  1841. ref.index:=ref.base;
  1842. ref.base:=NR_NO;
  1843. { FSTMX is deprecated on ARMv6 and later }
  1844. if (current_settings.cputype<cpu_armv6) then
  1845. postfix:=PF_IAX
  1846. else
  1847. postfix:=PF_IAD;
  1848. list.concat(setoppostfix(taicpu.op_ref_regset(A_FSTM,ref,R_MMREGISTER,R_SUBFD,mmregs),postfix));
  1849. end;
  1850. end;
  1851. end;
  1852. end;
  1853. end;
  1854. procedure tbasecgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1855. var
  1856. ref : treference;
  1857. LocalSize : longint;
  1858. firstfloatreg,lastfloatreg,
  1859. r,
  1860. shift : byte;
  1861. mmregs,
  1862. saveregs,
  1863. regs : tcpuregisterset;
  1864. registerarea,
  1865. stackmisalignment: pint;
  1866. paddingreg: TSuperRegister;
  1867. mmpostfix: toppostfix;
  1868. imm1, imm2: DWord;
  1869. begin
  1870. if not(nostackframe) then
  1871. begin
  1872. registerarea:=0;
  1873. firstfloatreg:=RS_NO;
  1874. lastfloatreg:=RS_NO;
  1875. mmregs:=[];
  1876. saveregs:=[];
  1877. case current_settings.fputype of
  1878. fpu_fpa,
  1879. fpu_fpa10,
  1880. fpu_fpa11:
  1881. begin
  1882. { restore floating point registers? }
  1883. regs:=rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  1884. for r:=RS_F0 to RS_F7 do
  1885. if r in regs then
  1886. begin
  1887. if firstfloatreg=RS_NO then
  1888. firstfloatreg:=r;
  1889. lastfloatreg:=r;
  1890. { floating point register space is already included in
  1891. localsize below by calc_stackframe_size
  1892. inc(registerarea,12);
  1893. }
  1894. end;
  1895. end;
  1896. fpu_vfpv2,
  1897. fpu_vfpv3,
  1898. fpu_vfpv3_d16:
  1899. begin;
  1900. { restore vfp registers? }
  1901. mmregs:=rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall);
  1902. end;
  1903. end;
  1904. if (firstfloatreg<>RS_NO) or
  1905. (mmregs<>[]) then
  1906. begin
  1907. reference_reset(ref,4);
  1908. if (tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023) or
  1909. (current_settings.fputype in [fpu_vfpv2,fpu_vfpv3,fpu_vfpv3_d16]) then
  1910. begin
  1911. if not is_shifter_const(tarmprocinfo(current_procinfo).floatregstart,shift) then
  1912. begin
  1913. a_reg_alloc(list,NR_R12);
  1914. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1915. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  1916. a_reg_dealloc(list,NR_R12);
  1917. end
  1918. else
  1919. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_R12,current_procinfo.framepointer,-tarmprocinfo(current_procinfo).floatregstart));
  1920. ref.base:=NR_R12;
  1921. end
  1922. else
  1923. begin
  1924. ref.base:=current_procinfo.framepointer;
  1925. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1926. end;
  1927. case current_settings.fputype of
  1928. fpu_fpa,
  1929. fpu_fpa10,
  1930. fpu_fpa11:
  1931. begin
  1932. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1933. lastfloatreg-firstfloatreg+1,ref));
  1934. end;
  1935. fpu_vfpv2,
  1936. fpu_vfpv3,
  1937. fpu_vfpv3_d16:
  1938. begin
  1939. ref.index:=ref.base;
  1940. ref.base:=NR_NO;
  1941. { FLDMX is deprecated on ARMv6 and later }
  1942. if (current_settings.cputype<cpu_armv6) then
  1943. mmpostfix:=PF_IAX
  1944. else
  1945. mmpostfix:=PF_IAD;
  1946. list.concat(setoppostfix(taicpu.op_ref_regset(A_FLDM,ref,R_MMREGISTER,R_SUBFD,mmregs),mmpostfix));
  1947. end;
  1948. end;
  1949. end;
  1950. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1951. if (pi_do_call in current_procinfo.flags) or
  1952. (regs<>[]) or
  1953. ((target_info.system in systems_darwin) and
  1954. (current_procinfo.framepointer<>NR_STACK_POINTER_REG)) then
  1955. begin
  1956. exclude(regs,RS_R14);
  1957. include(regs,RS_R15);
  1958. if (target_info.system in systems_darwin) then
  1959. include(regs,RS_FRAME_POINTER_REG);
  1960. end;
  1961. if not(target_info.system in systems_darwin) then
  1962. begin
  1963. { restore saved stack pointer to SP (R13) and saved lr to PC (R15).
  1964. The saved PC came after that but is discarded, since we restore
  1965. the stack pointer }
  1966. if (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  1967. regs:=regs+[RS_FRAME_POINTER_REG,RS_R13,RS_R15];
  1968. end
  1969. else
  1970. begin
  1971. { restore R8-R11 already if necessary (they've been stored
  1972. before the others) }
  1973. saveregs:=regs*[RS_R8,RS_R10,RS_R11];
  1974. if saveregs<>[] then
  1975. begin
  1976. reference_reset(ref,4);
  1977. ref.index:=NR_STACK_POINTER_REG;
  1978. ref.addressmode:=AM_PREINDEXED;
  1979. for r:=RS_R8 to RS_R11 do
  1980. if r in saveregs then
  1981. inc(registerarea,4);
  1982. regs:=regs-saveregs;
  1983. end;
  1984. end;
  1985. for r:=RS_R0 to RS_R15 do
  1986. if r in regs then
  1987. inc(registerarea,4);
  1988. { reapply the stack padding reg, in case there was one, see the complimentary
  1989. comment in g_proc_entry() (KB) }
  1990. paddingreg:=tarmprocinfo(current_procinfo).stackpaddingreg;
  1991. if paddingreg < RS_R4 then
  1992. if paddingreg in regs then
  1993. internalerror(201306190)
  1994. else
  1995. begin
  1996. regs:=regs+[paddingreg];
  1997. inc(registerarea,4);
  1998. end;
  1999. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  2000. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  2001. (target_info.system in systems_darwin) then
  2002. begin
  2003. LocalSize:=current_procinfo.calc_stackframe_size;
  2004. if (LocalSize<>0) or
  2005. ((stackmisalignment<>0) and
  2006. ((pi_do_call in current_procinfo.flags) or
  2007. (po_assembler in current_procinfo.procdef.procoptions))) then
  2008. begin
  2009. if pi_estimatestacksize in current_procinfo.flags then
  2010. LocalSize:=tarmprocinfo(current_procinfo).stackframesize-registerarea
  2011. else
  2012. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  2013. if is_shifter_const(LocalSize,shift) then
  2014. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize))
  2015. else if split_into_shifter_const(localsize, imm1, imm2) then
  2016. begin
  2017. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm1));
  2018. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm2));
  2019. end
  2020. else
  2021. begin
  2022. a_reg_alloc(list,NR_R12);
  2023. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  2024. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  2025. a_reg_dealloc(list,NR_R12);
  2026. end;
  2027. end;
  2028. if (target_info.system in systems_darwin) and
  2029. (saveregs<>[]) then
  2030. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  2031. if regs=[] then
  2032. begin
  2033. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  2034. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  2035. else
  2036. list.concat(taicpu.op_reg(A_BX,NR_R14))
  2037. end
  2038. else
  2039. begin
  2040. reference_reset(ref,4);
  2041. ref.index:=NR_STACK_POINTER_REG;
  2042. ref.addressmode:=AM_PREINDEXED;
  2043. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  2044. end;
  2045. end
  2046. else
  2047. begin
  2048. { restore int registers and return }
  2049. reference_reset(ref,4);
  2050. ref.index:=NR_FRAME_POINTER_REG;
  2051. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_EA));
  2052. end;
  2053. end
  2054. else if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  2055. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  2056. else
  2057. list.concat(taicpu.op_reg(A_BX,NR_R14))
  2058. end;
  2059. procedure tbasecgarm.g_maybe_got_init(list : TAsmList);
  2060. var
  2061. ref : treference;
  2062. l : TAsmLabel;
  2063. begin
  2064. if (cs_create_pic in current_settings.moduleswitches) and
  2065. (pi_needs_got in current_procinfo.flags) and
  2066. (tf_pic_uses_got in target_info.flags) then
  2067. begin
  2068. reference_reset(ref,4);
  2069. current_asmdata.getdatalabel(l);
  2070. cg.a_label(current_procinfo.aktlocaldata,l);
  2071. ref.symbol:=l;
  2072. ref.base:=NR_PC;
  2073. ref.symboldata:=current_procinfo.aktlocaldata.last;
  2074. list.concat(Taicpu.op_reg_ref(A_LDR,current_procinfo.got,ref));
  2075. current_asmdata.getaddrlabel(l);
  2076. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_32bit,l,current_asmdata.RefAsmSymbol('_GLOBAL_OFFSET_TABLE_'),-8));
  2077. cg.a_label(list,l);
  2078. list.concat(Taicpu.op_reg_reg_reg(A_ADD,current_procinfo.got,NR_PC,current_procinfo.got));
  2079. end;
  2080. end;
  2081. procedure tbasecgarm.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  2082. var
  2083. b : byte;
  2084. tmpref : treference;
  2085. instr : taicpu;
  2086. begin
  2087. if ref.addressmode<>AM_OFFSET then
  2088. internalerror(200309071);
  2089. tmpref:=ref;
  2090. { Be sure to have a base register }
  2091. if (tmpref.base=NR_NO) then
  2092. begin
  2093. if tmpref.shiftmode<>SM_None then
  2094. internalerror(2014020702);
  2095. if tmpref.signindex<0 then
  2096. internalerror(200312023);
  2097. tmpref.base:=tmpref.index;
  2098. tmpref.index:=NR_NO;
  2099. end;
  2100. if assigned(tmpref.symbol) or
  2101. not((is_shifter_const(tmpref.offset,b)) or
  2102. (is_shifter_const(-tmpref.offset,b))
  2103. ) then
  2104. fixref(list,tmpref);
  2105. { expect a base here if there is an index }
  2106. if (tmpref.base=NR_NO) and (tmpref.index<>NR_NO) then
  2107. internalerror(200312022);
  2108. if tmpref.index<>NR_NO then
  2109. begin
  2110. if tmpref.shiftmode<>SM_None then
  2111. internalerror(200312021);
  2112. if tmpref.signindex<0 then
  2113. a_op_reg_reg_reg(list,OP_SUB,OS_ADDR,tmpref.base,tmpref.index,r)
  2114. else
  2115. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpref.base,tmpref.index,r);
  2116. if tmpref.offset<>0 then
  2117. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,r,r);
  2118. end
  2119. else
  2120. begin
  2121. if tmpref.base=NR_NO then
  2122. a_load_const_reg(list,OS_ADDR,tmpref.offset,r)
  2123. else
  2124. if tmpref.offset<>0 then
  2125. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,tmpref.base,r)
  2126. else
  2127. begin
  2128. instr:=taicpu.op_reg_reg(A_MOV,r,tmpref.base);
  2129. list.concat(instr);
  2130. add_move_instruction(instr);
  2131. end;
  2132. end;
  2133. end;
  2134. procedure tbasecgarm.fixref(list : TAsmList;var ref : treference);
  2135. var
  2136. tmpreg, tmpreg2 : tregister;
  2137. tmpref : treference;
  2138. l, piclabel : tasmlabel;
  2139. indirection_done : boolean;
  2140. begin
  2141. { absolute symbols can't be handled directly, we've to store the symbol reference
  2142. in the text segment and access it pc relative
  2143. For now, we assume that references where base or index equals to PC are already
  2144. relative, all other references are assumed to be absolute and thus they need
  2145. to be handled extra.
  2146. A proper solution would be to change refoptions to a set and store the information
  2147. if the symbol is absolute or relative there.
  2148. }
  2149. { create consts entry }
  2150. reference_reset(tmpref,4);
  2151. current_asmdata.getjumplabel(l);
  2152. cg.a_label(current_procinfo.aktlocaldata,l);
  2153. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2154. piclabel:=nil;
  2155. tmpreg:=NR_NO;
  2156. indirection_done:=false;
  2157. if assigned(ref.symbol) then
  2158. begin
  2159. if (target_info.system=system_arm_darwin) and
  2160. (ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN,AB_COMMON]) then
  2161. begin
  2162. tmpreg:=g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol));
  2163. if ref.offset<>0 then
  2164. a_op_const_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg);
  2165. indirection_done:=true;
  2166. end
  2167. else if (cs_create_pic in current_settings.moduleswitches) then
  2168. if (tf_pic_uses_got in target_info.flags) then
  2169. current_procinfo.aktlocaldata.concat(tai_const.Create_type_sym_offset(aitconst_got,ref.symbol,ref.offset))
  2170. else
  2171. begin
  2172. { ideally, we would want to generate
  2173. ldr r1, LPICConstPool
  2174. LPICLocal:
  2175. ldr/str r2,[pc,r1]
  2176. ...
  2177. LPICConstPool:
  2178. .long _globsym-(LPICLocal+8)
  2179. However, we cannot be sure that the ldr/str will follow
  2180. right after the call to fixref, so we have to load the
  2181. complete address already in a register.
  2182. }
  2183. current_asmdata.getaddrlabel(piclabel);
  2184. current_procinfo.aktlocaldata.concat(tai_const.Create_rel_sym_offset(aitconst_ptr,piclabel,ref.symbol,ref.offset-8));
  2185. end
  2186. else
  2187. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset))
  2188. end
  2189. else
  2190. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ref.offset));
  2191. { load consts entry }
  2192. if not indirection_done then
  2193. begin
  2194. tmpreg:=getintregister(list,OS_INT);
  2195. tmpref.symbol:=l;
  2196. tmpref.base:=NR_PC;
  2197. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  2198. if (cs_create_pic in current_settings.moduleswitches) and
  2199. (tf_pic_uses_got in target_info.flags) and
  2200. assigned(ref.symbol) then
  2201. begin
  2202. reference_reset(tmpref,4);
  2203. tmpref.base:=current_procinfo.got;
  2204. tmpref.index:=tmpreg;
  2205. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  2206. end;
  2207. end;
  2208. if assigned(piclabel) then
  2209. begin
  2210. cg.a_label(list,piclabel);
  2211. tmpreg2:=getaddressregister(list);
  2212. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpreg,NR_PC,tmpreg2);
  2213. tmpreg:=tmpreg2
  2214. end;
  2215. { This routine can be called with PC as base/index in case the offset
  2216. was too large to encode in a load/store. In that case, the entire
  2217. absolute expression has been re-encoded in a new constpool entry, and
  2218. we have to remove the use of PC from the original reference (the code
  2219. above made everything relative to the value loaded from the new
  2220. constpool entry) }
  2221. if is_pc(ref.base) then
  2222. ref.base:=NR_NO;
  2223. if is_pc(ref.index) then
  2224. ref.index:=NR_NO;
  2225. if (ref.base<>NR_NO) then
  2226. begin
  2227. if ref.index<>NR_NO then
  2228. begin
  2229. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  2230. ref.base:=tmpreg;
  2231. end
  2232. else
  2233. if ref.base<>NR_PC then
  2234. begin
  2235. ref.index:=tmpreg;
  2236. ref.shiftimm:=0;
  2237. ref.signindex:=1;
  2238. ref.shiftmode:=SM_None;
  2239. end
  2240. else
  2241. ref.base:=tmpreg;
  2242. end
  2243. else
  2244. ref.base:=tmpreg;
  2245. ref.offset:=0;
  2246. ref.symbol:=nil;
  2247. end;
  2248. procedure tbasecgarm.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  2249. var
  2250. paraloc1,paraloc2,paraloc3 : TCGPara;
  2251. pd : tprocdef;
  2252. begin
  2253. pd:=search_system_proc('MOVE');
  2254. paraloc1.init;
  2255. paraloc2.init;
  2256. paraloc3.init;
  2257. paramanager.getintparaloc(pd,1,paraloc1);
  2258. paramanager.getintparaloc(pd,2,paraloc2);
  2259. paramanager.getintparaloc(pd,3,paraloc3);
  2260. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  2261. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  2262. a_loadaddr_ref_cgpara(list,source,paraloc1);
  2263. paramanager.freecgpara(list,paraloc3);
  2264. paramanager.freecgpara(list,paraloc2);
  2265. paramanager.freecgpara(list,paraloc1);
  2266. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  2267. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  2268. a_call_name(list,'FPC_MOVE',false);
  2269. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  2270. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  2271. paraloc3.done;
  2272. paraloc2.done;
  2273. paraloc1.done;
  2274. end;
  2275. procedure tbasecgarm.g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : tcgint;aligned : boolean);
  2276. const
  2277. maxtmpreg_arm = 10; {roozbeh: can be reduced to 8 or lower if might conflick with reserved ones,also +2 is used becouse of regs required for referencing}
  2278. maxtmpreg_thumb = 5;
  2279. var
  2280. srcref,dstref,usedtmpref,usedtmpref2:treference;
  2281. srcreg,destreg,countreg,r,tmpreg:tregister;
  2282. helpsize:aint;
  2283. copysize:byte;
  2284. cgsize:Tcgsize;
  2285. tmpregisters:array[1..maxtmpreg_arm] of tregister;
  2286. maxtmpreg,
  2287. tmpregi,tmpregi2:byte;
  2288. { will never be called with count<=4 }
  2289. procedure genloop(count : aword;size : byte);
  2290. const
  2291. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  2292. var
  2293. l : tasmlabel;
  2294. begin
  2295. current_asmdata.getjumplabel(l);
  2296. if count<size then size:=1;
  2297. a_load_const_reg(list,OS_INT,count div size,countreg);
  2298. cg.a_label(list,l);
  2299. srcref.addressmode:=AM_POSTINDEXED;
  2300. dstref.addressmode:=AM_POSTINDEXED;
  2301. srcref.offset:=size;
  2302. dstref.offset:=size;
  2303. r:=getintregister(list,size2opsize[size]);
  2304. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  2305. a_reg_alloc(list,NR_DEFAULTFLAGS);
  2306. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1),PF_S));
  2307. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  2308. a_jmp_flags(list,F_NE,l);
  2309. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2310. srcref.offset:=1;
  2311. dstref.offset:=1;
  2312. case count mod size of
  2313. 1:
  2314. begin
  2315. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2316. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2317. end;
  2318. 2:
  2319. if aligned then
  2320. begin
  2321. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2322. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2323. end
  2324. else
  2325. begin
  2326. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2327. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2328. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2329. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2330. end;
  2331. 3:
  2332. if aligned then
  2333. begin
  2334. srcref.offset:=2;
  2335. dstref.offset:=2;
  2336. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2337. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2338. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2339. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2340. end
  2341. else
  2342. begin
  2343. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2344. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2345. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2346. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2347. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2348. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2349. end;
  2350. end;
  2351. { keep the registers alive }
  2352. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  2353. list.concat(taicpu.op_reg_reg(A_MOV,srcreg,srcreg));
  2354. list.concat(taicpu.op_reg_reg(A_MOV,destreg,destreg));
  2355. end;
  2356. { will never be called with count<=4 }
  2357. procedure genloop_thumb(count : aword;size : byte);
  2358. procedure refincofs(const ref : treference;const value : longint = 1);
  2359. begin
  2360. a_op_const_reg(list,OP_ADD,OS_ADDR,value,ref.base);
  2361. end;
  2362. const
  2363. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  2364. var
  2365. l : tasmlabel;
  2366. begin
  2367. current_asmdata.getjumplabel(l);
  2368. if count<size then size:=1;
  2369. a_load_const_reg(list,OS_INT,count div size,countreg);
  2370. cg.a_label(list,l);
  2371. r:=getintregister(list,size2opsize[size]);
  2372. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  2373. refincofs(srcref);
  2374. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  2375. refincofs(dstref);
  2376. a_reg_alloc(list,NR_DEFAULTFLAGS);
  2377. list.concat(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1));
  2378. a_jmp_flags(list,F_NE,l);
  2379. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2380. case count mod size of
  2381. 1:
  2382. begin
  2383. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2384. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2385. end;
  2386. 2:
  2387. if aligned then
  2388. begin
  2389. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2390. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2391. end
  2392. else
  2393. begin
  2394. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2395. refincofs(srcref);
  2396. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2397. refincofs(dstref);
  2398. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2399. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2400. end;
  2401. 3:
  2402. if aligned then
  2403. begin
  2404. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  2405. refincofs(srcref,2);
  2406. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  2407. refincofs(dstref,2);
  2408. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2409. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2410. end
  2411. else
  2412. begin
  2413. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2414. refincofs(srcref);
  2415. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2416. refincofs(dstref);
  2417. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2418. refincofs(srcref);
  2419. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2420. refincofs(dstref);
  2421. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2422. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2423. end;
  2424. end;
  2425. { keep the registers alive }
  2426. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  2427. list.concat(taicpu.op_reg_reg(A_MOV,srcreg,srcreg));
  2428. list.concat(taicpu.op_reg_reg(A_MOV,destreg,destreg));
  2429. end;
  2430. begin
  2431. if len=0 then
  2432. exit;
  2433. if GenerateThumbCode then
  2434. maxtmpreg:=maxtmpreg_thumb
  2435. else
  2436. maxtmpreg:=maxtmpreg_arm;
  2437. helpsize:=12+maxtmpreg*4;//52 with maxtmpreg=10
  2438. dstref:=dest;
  2439. srcref:=source;
  2440. if cs_opt_size in current_settings.optimizerswitches then
  2441. helpsize:=8;
  2442. if aligned and (len=4) then
  2443. begin
  2444. tmpreg:=getintregister(list,OS_32);
  2445. a_load_ref_reg(list,OS_32,OS_32,source,tmpreg);
  2446. a_load_reg_ref(list,OS_32,OS_32,tmpreg,dest);
  2447. end
  2448. else if aligned and (len=2) then
  2449. begin
  2450. tmpreg:=getintregister(list,OS_16);
  2451. a_load_ref_reg(list,OS_16,OS_16,source,tmpreg);
  2452. a_load_reg_ref(list,OS_16,OS_16,tmpreg,dest);
  2453. end
  2454. else if (len<=helpsize) and aligned then
  2455. begin
  2456. tmpregi:=0;
  2457. srcreg:=getintregister(list,OS_ADDR);
  2458. { explicit pc relative addressing, could be
  2459. e.g. a floating point constant }
  2460. if source.base=NR_PC then
  2461. begin
  2462. { ... then we don't need a loadaddr }
  2463. srcref:=source;
  2464. end
  2465. else
  2466. begin
  2467. a_loadaddr_ref_reg(list,source,srcreg);
  2468. reference_reset_base(srcref,srcreg,0,source.alignment);
  2469. end;
  2470. while (len div 4 <> 0) and (tmpregi<maxtmpreg) do
  2471. begin
  2472. inc(tmpregi);
  2473. tmpregisters[tmpregi]:=getintregister(list,OS_32);
  2474. a_load_ref_reg(list,OS_32,OS_32,srcref,tmpregisters[tmpregi]);
  2475. inc(srcref.offset,4);
  2476. dec(len,4);
  2477. end;
  2478. destreg:=getintregister(list,OS_ADDR);
  2479. a_loadaddr_ref_reg(list,dest,destreg);
  2480. reference_reset_base(dstref,destreg,0,dest.alignment);
  2481. tmpregi2:=1;
  2482. while (tmpregi2<=tmpregi) do
  2483. begin
  2484. a_load_reg_ref(list,OS_32,OS_32,tmpregisters[tmpregi2],dstref);
  2485. inc(dstref.offset,4);
  2486. inc(tmpregi2);
  2487. end;
  2488. copysize:=4;
  2489. cgsize:=OS_32;
  2490. while len<>0 do
  2491. begin
  2492. if len<2 then
  2493. begin
  2494. copysize:=1;
  2495. cgsize:=OS_8;
  2496. end
  2497. else if len<4 then
  2498. begin
  2499. copysize:=2;
  2500. cgsize:=OS_16;
  2501. end;
  2502. dec(len,copysize);
  2503. r:=getintregister(list,cgsize);
  2504. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2505. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2506. inc(srcref.offset,copysize);
  2507. inc(dstref.offset,copysize);
  2508. end;{end of while}
  2509. end
  2510. else
  2511. begin
  2512. cgsize:=OS_32;
  2513. if (len<=4) then{len<=4 and not aligned}
  2514. begin
  2515. r:=getintregister(list,cgsize);
  2516. usedtmpref:=a_internal_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2517. if Len=1 then
  2518. a_load_reg_ref(list,OS_8,OS_8,r,dstref)
  2519. else
  2520. begin
  2521. tmpreg:=getintregister(list,cgsize);
  2522. usedtmpref2:=a_internal_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2523. inc(usedtmpref.offset,1);
  2524. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2525. inc(usedtmpref2.offset,1);
  2526. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2527. if len>2 then
  2528. begin
  2529. inc(usedtmpref.offset,1);
  2530. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2531. inc(usedtmpref2.offset,1);
  2532. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2533. if len>3 then
  2534. begin
  2535. inc(usedtmpref.offset,1);
  2536. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2537. inc(usedtmpref2.offset,1);
  2538. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2539. end;
  2540. end;
  2541. end;
  2542. end{end of if len<=4}
  2543. else
  2544. begin{unaligned & 4<len<helpsize **or** aligned/unaligned & len>helpsize}
  2545. destreg:=getintregister(list,OS_ADDR);
  2546. a_loadaddr_ref_reg(list,dest,destreg);
  2547. reference_reset_base(dstref,destreg,0,dest.alignment);
  2548. srcreg:=getintregister(list,OS_ADDR);
  2549. a_loadaddr_ref_reg(list,source,srcreg);
  2550. reference_reset_base(srcref,srcreg,0,source.alignment);
  2551. countreg:=getintregister(list,OS_32);
  2552. // if cs_opt_size in current_settings.optimizerswitches then
  2553. { roozbeh : it seems loading 1 byte is faster becouse of caching/fetching(?) }
  2554. {if aligned then
  2555. genloop(len,4)
  2556. else}
  2557. if GenerateThumbCode then
  2558. genloop_thumb(len,1)
  2559. else
  2560. genloop(len,1);
  2561. end;
  2562. end;
  2563. end;
  2564. procedure tbasecgarm.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  2565. begin
  2566. g_concatcopy_internal(list,source,dest,len,false);
  2567. end;
  2568. procedure tbasecgarm.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  2569. begin
  2570. if (source.alignment in [1,3]) or
  2571. (dest.alignment in [1,3]) then
  2572. g_concatcopy_internal(list,source,dest,len,false)
  2573. else
  2574. g_concatcopy_internal(list,source,dest,len,true);
  2575. end;
  2576. procedure tbasecgarm.g_overflowCheck(list : TAsmList;const l : tlocation;def : tdef);
  2577. var
  2578. ovloc : tlocation;
  2579. begin
  2580. ovloc.loc:=LOC_VOID;
  2581. g_overflowCheck_loc(list,l,def,ovloc);
  2582. end;
  2583. procedure tbasecgarm.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  2584. var
  2585. hl : tasmlabel;
  2586. ai:TAiCpu;
  2587. hflags : tresflags;
  2588. begin
  2589. if not(cs_check_overflow in current_settings.localswitches) then
  2590. exit;
  2591. current_asmdata.getjumplabel(hl);
  2592. case ovloc.loc of
  2593. LOC_VOID:
  2594. begin
  2595. ai:=taicpu.op_sym(A_B,hl);
  2596. ai.is_jmp:=true;
  2597. if not((def.typ=pointerdef) or
  2598. ((def.typ=orddef) and
  2599. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  2600. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  2601. ai.SetCondition(C_VC)
  2602. else
  2603. if TAiCpu(List.Last).opcode in [A_RSB,A_RSC,A_SBC,A_SUB] then
  2604. ai.SetCondition(C_CS)
  2605. else
  2606. ai.SetCondition(C_CC);
  2607. list.concat(ai);
  2608. end;
  2609. LOC_FLAGS:
  2610. begin
  2611. hflags:=ovloc.resflags;
  2612. inverse_flags(hflags);
  2613. cg.a_jmp_flags(list,hflags,hl);
  2614. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2615. end;
  2616. else
  2617. internalerror(200409281);
  2618. end;
  2619. a_call_name(list,'FPC_OVERFLOW',false);
  2620. a_label(list,hl);
  2621. end;
  2622. procedure tbasecgarm.g_save_registers(list : TAsmList);
  2623. begin
  2624. { this work is done in g_proc_entry }
  2625. end;
  2626. procedure tbasecgarm.g_restore_registers(list : TAsmList);
  2627. begin
  2628. { this work is done in g_proc_exit }
  2629. end;
  2630. procedure tbasecgarm.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  2631. var
  2632. ai : taicpu;
  2633. hlabel : TAsmLabel;
  2634. begin
  2635. if GenerateThumbCode then
  2636. begin
  2637. { the optimizer has to fix this if jump range is sufficient short }
  2638. current_asmdata.getjumplabel(hlabel);
  2639. ai:=Taicpu.Op_sym(A_B,hlabel);
  2640. ai.SetCondition(inverse_cond(OpCmp2AsmCond[cond]));
  2641. ai.is_jmp:=true;
  2642. list.concat(ai);
  2643. a_jmp_always(list,l);
  2644. a_label(list,hlabel);
  2645. end
  2646. else
  2647. begin
  2648. ai:=Taicpu.Op_sym(A_B,l);
  2649. ai.SetCondition(OpCmp2AsmCond[cond]);
  2650. ai.is_jmp:=true;
  2651. list.concat(ai);
  2652. end;
  2653. end;
  2654. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  2655. const
  2656. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  2657. (A_FCPYS,A_FCVTSD,A_NONE,A_NONE,A_NONE),
  2658. (A_FCVTDS,A_FCPYD,A_NONE,A_NONE,A_NONE),
  2659. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  2660. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  2661. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  2662. begin
  2663. result:=convertop[fromsize,tosize];
  2664. if result=A_NONE then
  2665. internalerror(200312205);
  2666. end;
  2667. procedure tbasecgarm.a_loadmm_reg_reg(list: tasmlist; fromsize,tosize: tcgsize; reg1,reg2: tregister; shuffle: pmmshuffle);
  2668. var
  2669. instr: taicpu;
  2670. begin
  2671. if shuffle=nil then
  2672. begin
  2673. if fromsize=tosize then
  2674. { needs correct size in case of spilling }
  2675. case fromsize of
  2676. OS_F32:
  2677. instr:=taicpu.op_reg_reg(A_FCPYS,reg2,reg1);
  2678. OS_F64:
  2679. instr:=taicpu.op_reg_reg(A_FCPYD,reg2,reg1);
  2680. else
  2681. internalerror(2009112405);
  2682. end
  2683. else
  2684. internalerror(2009112406);
  2685. end
  2686. else if shufflescalar(shuffle) then
  2687. instr:=taicpu.op_reg_reg(get_scalar_mm_op(tosize,fromsize),reg2,reg1)
  2688. else
  2689. internalerror(2009112407);
  2690. list.concat(instr);
  2691. case instr.opcode of
  2692. A_FCPYS,
  2693. A_FCPYD:
  2694. add_move_instruction(instr);
  2695. end;
  2696. end;
  2697. procedure tbasecgarm.a_loadmm_ref_reg(list: tasmlist; fromsize,tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2698. var
  2699. intreg,
  2700. tmpmmreg : tregister;
  2701. reg64 : tregister64;
  2702. op : tasmop;
  2703. begin
  2704. if assigned(shuffle) and
  2705. not(shufflescalar(shuffle)) then
  2706. internalerror(2009112413);
  2707. case fromsize of
  2708. OS_32,OS_S32:
  2709. begin
  2710. fromsize:=OS_F32;
  2711. { since we are loading an integer, no conversion may be required }
  2712. if (fromsize<>tosize) then
  2713. internalerror(2009112801);
  2714. end;
  2715. OS_64,OS_S64:
  2716. begin
  2717. fromsize:=OS_F64;
  2718. { since we are loading an integer, no conversion may be required }
  2719. if (fromsize<>tosize) then
  2720. internalerror(2009112901);
  2721. end;
  2722. end;
  2723. if (fromsize<>tosize) then
  2724. tmpmmreg:=getmmregister(list,fromsize)
  2725. else
  2726. tmpmmreg:=reg;
  2727. if (ref.alignment in [1,2]) then
  2728. begin
  2729. case fromsize of
  2730. OS_F32:
  2731. begin
  2732. intreg:=getintregister(list,OS_32);
  2733. a_load_ref_reg(list,OS_32,OS_32,ref,intreg);
  2734. a_loadmm_intreg_reg(list,OS_32,OS_F32,intreg,tmpmmreg,mms_movescalar);
  2735. end;
  2736. OS_F64:
  2737. begin
  2738. reg64.reglo:=getintregister(list,OS_32);
  2739. reg64.reghi:=getintregister(list,OS_32);
  2740. cg64.a_load64_ref_reg(list,ref,reg64);
  2741. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,tmpmmreg);
  2742. end;
  2743. else
  2744. internalerror(2009112412);
  2745. end;
  2746. end
  2747. else
  2748. begin
  2749. case fromsize of
  2750. OS_F32:
  2751. op:=A_FLDS;
  2752. OS_F64:
  2753. op:=A_FLDD;
  2754. else
  2755. internalerror(2009112415);
  2756. end;
  2757. handle_load_store(list,op,PF_None,tmpmmreg,ref);
  2758. end;
  2759. if (tmpmmreg<>reg) then
  2760. a_loadmm_reg_reg(list,fromsize,tosize,tmpmmreg,reg,shuffle);
  2761. end;
  2762. procedure tbasecgarm.a_loadmm_reg_ref(list: tasmlist; fromsize,tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2763. var
  2764. intreg,
  2765. tmpmmreg : tregister;
  2766. reg64 : tregister64;
  2767. op : tasmop;
  2768. begin
  2769. if assigned(shuffle) and
  2770. not(shufflescalar(shuffle)) then
  2771. internalerror(2009112416);
  2772. case tosize of
  2773. OS_32,OS_S32:
  2774. begin
  2775. tosize:=OS_F32;
  2776. { since we are loading an integer, no conversion may be required }
  2777. if (fromsize<>tosize) then
  2778. internalerror(2009112801);
  2779. end;
  2780. OS_64,OS_S64:
  2781. begin
  2782. tosize:=OS_F64;
  2783. { since we are loading an integer, no conversion may be required }
  2784. if (fromsize<>tosize) then
  2785. internalerror(2009112901);
  2786. end;
  2787. end;
  2788. if (fromsize<>tosize) then
  2789. begin
  2790. tmpmmreg:=getmmregister(list,tosize);
  2791. a_loadmm_reg_reg(list,fromsize,tosize,reg,tmpmmreg,shuffle);
  2792. end
  2793. else
  2794. tmpmmreg:=reg;
  2795. if (ref.alignment in [1,2]) then
  2796. begin
  2797. case tosize of
  2798. OS_F32:
  2799. begin
  2800. intreg:=getintregister(list,OS_32);
  2801. a_loadmm_reg_intreg(list,OS_F32,OS_32,tmpmmreg,intreg,shuffle);
  2802. a_load_reg_ref(list,OS_32,OS_32,intreg,ref);
  2803. end;
  2804. OS_F64:
  2805. begin
  2806. reg64.reglo:=getintregister(list,OS_32);
  2807. reg64.reghi:=getintregister(list,OS_32);
  2808. cg64.a_loadmm_reg_intreg64(list,OS_F64,tmpmmreg,reg64);
  2809. cg64.a_load64_reg_ref(list,reg64,ref);
  2810. end;
  2811. else
  2812. internalerror(2009112417);
  2813. end;
  2814. end
  2815. else
  2816. begin
  2817. case fromsize of
  2818. OS_F32:
  2819. op:=A_FSTS;
  2820. OS_F64:
  2821. op:=A_FSTD;
  2822. else
  2823. internalerror(2009112418);
  2824. end;
  2825. handle_load_store(list,op,PF_None,tmpmmreg,ref);
  2826. end;
  2827. end;
  2828. procedure tbasecgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  2829. begin
  2830. { this code can only be used to transfer raw data, not to perform
  2831. conversions }
  2832. if (tosize<>OS_F32) then
  2833. internalerror(2009112419);
  2834. if not(fromsize in [OS_32,OS_S32]) then
  2835. internalerror(2009112420);
  2836. if assigned(shuffle) and
  2837. not shufflescalar(shuffle) then
  2838. internalerror(2009112516);
  2839. list.concat(taicpu.op_reg_reg(A_FMSR,mmreg,intreg));
  2840. end;
  2841. procedure tbasecgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister;shuffle : pmmshuffle);
  2842. begin
  2843. { this code can only be used to transfer raw data, not to perform
  2844. conversions }
  2845. if (fromsize<>OS_F32) then
  2846. internalerror(2009112430);
  2847. if not(tosize in [OS_32,OS_S32]) then
  2848. internalerror(2009112420);
  2849. if assigned(shuffle) and
  2850. not shufflescalar(shuffle) then
  2851. internalerror(2009112514);
  2852. list.concat(taicpu.op_reg_reg(A_FMRS,intreg,mmreg));
  2853. end;
  2854. procedure tbasecgarm.a_opmm_reg_reg(list: tasmlist; op: topcg; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2855. var
  2856. tmpreg: tregister;
  2857. begin
  2858. { the vfp doesn't support xor nor any other logical operation, but
  2859. this routine is used to initialise global mm regvars. We can
  2860. easily initialise an mm reg with 0 though. }
  2861. case op of
  2862. OP_XOR:
  2863. begin
  2864. if (src<>dst) or
  2865. (reg_cgsize(src)<>size) or
  2866. assigned(shuffle) then
  2867. internalerror(2009112907);
  2868. tmpreg:=getintregister(list,OS_32);
  2869. a_load_const_reg(list,OS_32,0,tmpreg);
  2870. case size of
  2871. OS_F32:
  2872. list.concat(taicpu.op_reg_reg(A_FMSR,dst,tmpreg));
  2873. OS_F64:
  2874. list.concat(taicpu.op_reg_reg_reg(A_FMDRR,dst,tmpreg,tmpreg));
  2875. else
  2876. internalerror(2009112908);
  2877. end;
  2878. end
  2879. else
  2880. internalerror(2009112906);
  2881. end;
  2882. end;
  2883. procedure tbasecgarm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  2884. procedure loadvmttor12;
  2885. var
  2886. tmpref,
  2887. href : treference;
  2888. extrareg : boolean;
  2889. l : TAsmLabel;
  2890. begin
  2891. reference_reset_base(href,NR_R0,0,sizeof(pint));
  2892. if GenerateThumbCode then
  2893. begin
  2894. if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
  2895. begin
  2896. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2897. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2898. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2899. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2900. end
  2901. else
  2902. begin
  2903. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2904. { create consts entry }
  2905. reference_reset(tmpref,4);
  2906. current_asmdata.getjumplabel(l);
  2907. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  2908. cg.a_label(current_procinfo.aktlocaldata,l);
  2909. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2910. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
  2911. tmpref.symbol:=l;
  2912. tmpref.base:=NR_PC;
  2913. list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
  2914. href.offset:=0;
  2915. href.index:=NR_R1;
  2916. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2917. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2918. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2919. end;
  2920. end
  2921. else
  2922. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2923. end;
  2924. procedure op_onr12methodaddr;
  2925. var
  2926. tmpref,
  2927. href : treference;
  2928. l : TAsmLabel;
  2929. begin
  2930. if (procdef.extnumber=$ffff) then
  2931. Internalerror(200006139);
  2932. if GenerateThumbCode then
  2933. begin
  2934. reference_reset_base(href,NR_R0,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  2935. if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
  2936. begin
  2937. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2938. list.concat(taicpu.op_reg_reg(A_MOV,NR_R0,NR_R12));
  2939. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2940. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2941. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2942. end
  2943. else
  2944. begin
  2945. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2946. { create consts entry }
  2947. reference_reset(tmpref,4);
  2948. current_asmdata.getjumplabel(l);
  2949. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  2950. cg.a_label(current_procinfo.aktlocaldata,l);
  2951. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2952. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
  2953. tmpref.symbol:=l;
  2954. tmpref.base:=NR_PC;
  2955. list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
  2956. list.concat(taicpu.op_reg_reg(A_MOV,NR_R0,NR_R12));
  2957. href.offset:=0;
  2958. href.base:=NR_R0;
  2959. href.index:=NR_R1;
  2960. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2961. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2962. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2963. end;
  2964. end
  2965. else
  2966. begin
  2967. reference_reset_base(href,NR_R12,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  2968. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2969. end;
  2970. list.concat(taicpu.op_reg(A_BX,NR_R12));
  2971. end;
  2972. var
  2973. make_global : boolean;
  2974. tmpref : treference;
  2975. l : TAsmLabel;
  2976. begin
  2977. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  2978. Internalerror(200006137);
  2979. if not assigned(procdef.struct) or
  2980. (procdef.procoptions*[po_classmethod, po_staticmethod,
  2981. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  2982. Internalerror(200006138);
  2983. if procdef.owner.symtabletype<>ObjectSymtable then
  2984. Internalerror(200109191);
  2985. if GenerateThumbCode or GenerateThumb2Code then
  2986. list.concat(tai_thumb_func.create);
  2987. make_global:=false;
  2988. if (not current_module.is_unit) or
  2989. create_smartlink or
  2990. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  2991. make_global:=true;
  2992. if make_global then
  2993. list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  2994. else
  2995. list.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  2996. { the wrapper might need aktlocaldata for the additional data to
  2997. load the constant }
  2998. current_procinfo:=cprocinfo.create(nil);
  2999. { set param1 interface to self }
  3000. g_adjust_self_value(list,procdef,ioffset);
  3001. { case 4 }
  3002. if (po_virtualmethod in procdef.procoptions) and
  3003. not is_objectpascal_helper(procdef.struct) then
  3004. begin
  3005. loadvmttor12;
  3006. op_onr12methodaddr;
  3007. end
  3008. { case 0 }
  3009. else if GenerateThumbCode then
  3010. begin
  3011. { bl cannot be used here because it destroys lr }
  3012. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3013. { create consts entry }
  3014. reference_reset(tmpref,4);
  3015. current_asmdata.getjumplabel(l);
  3016. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3017. cg.a_label(current_procinfo.aktlocaldata,l);
  3018. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3019. current_procinfo.aktlocaldata.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(procdef.mangledname)));
  3020. tmpref.symbol:=l;
  3021. tmpref.base:=NR_PC;
  3022. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,NR_R0);
  3023. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  3024. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3025. list.concat(taicpu.op_reg(A_BX,NR_R12));
  3026. end
  3027. else
  3028. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  3029. list.concatlist(current_procinfo.aktlocaldata);
  3030. current_procinfo.Free;
  3031. current_procinfo:=nil;
  3032. list.concat(Tai_symbol_end.Createname(labelname));
  3033. end;
  3034. procedure tbasecgarm.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  3035. const
  3036. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NEG];
  3037. begin
  3038. if (op in overflowops) and
  3039. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  3040. a_load_reg_reg(list,OS_32,size,dst,dst);
  3041. end;
  3042. procedure tbasecgarm.safe_mla(list : TAsmList; op1,op2,op3,op4 : TRegister);
  3043. procedure checkreg(var reg : TRegister);
  3044. var
  3045. tmpreg : TRegister;
  3046. begin
  3047. if ((GenerateThumbCode or GenerateThumb2Code) and (getsupreg(reg)=RS_R13)) or
  3048. (getsupreg(reg)=RS_R15) then
  3049. begin
  3050. tmpreg:=getintregister(list,OS_INT);
  3051. a_load_reg_reg(list,OS_INT,OS_INT,reg,tmpreg);
  3052. reg:=tmpreg;
  3053. end;
  3054. end;
  3055. begin
  3056. checkreg(op1);
  3057. checkreg(op2);
  3058. checkreg(op3);
  3059. checkreg(op4);
  3060. list.concat(taicpu.op_reg_reg_reg_reg(A_MLA,op1,op2,op3,op4));
  3061. end;
  3062. procedure tcg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  3063. begin
  3064. case op of
  3065. OP_NEG:
  3066. begin
  3067. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3068. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  3069. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  3070. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3071. end;
  3072. OP_NOT:
  3073. begin
  3074. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  3075. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  3076. end;
  3077. else
  3078. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  3079. end;
  3080. end;
  3081. procedure tcg64farm.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  3082. begin
  3083. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  3084. end;
  3085. procedure tcg64farm.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  3086. var
  3087. ovloc : tlocation;
  3088. begin
  3089. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,ovloc);
  3090. end;
  3091. procedure tcg64farm.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  3092. var
  3093. ovloc : tlocation;
  3094. begin
  3095. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,ovloc);
  3096. end;
  3097. procedure tcg64farm.a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister);
  3098. begin
  3099. { this code can only be used to transfer raw data, not to perform
  3100. conversions }
  3101. if (mmsize<>OS_F64) then
  3102. internalerror(2009112405);
  3103. list.concat(taicpu.op_reg_reg_reg(A_FMDRR,mmreg,intreg.reglo,intreg.reghi));
  3104. end;
  3105. procedure tcg64farm.a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64);
  3106. begin
  3107. { this code can only be used to transfer raw data, not to perform
  3108. conversions }
  3109. if (mmsize<>OS_F64) then
  3110. internalerror(2009112406);
  3111. list.concat(taicpu.op_reg_reg_reg(A_FMRRD,intreg.reglo,intreg.reghi,mmreg));
  3112. end;
  3113. procedure tcg64farm.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3114. var
  3115. tmpreg : tregister;
  3116. b : byte;
  3117. begin
  3118. ovloc.loc:=LOC_VOID;
  3119. case op of
  3120. OP_NEG,
  3121. OP_NOT :
  3122. internalerror(2012022501);
  3123. end;
  3124. if (setflags or tbasecgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  3125. begin
  3126. case op of
  3127. OP_ADD:
  3128. begin
  3129. if is_shifter_const(lo(value),b) then
  3130. begin
  3131. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3132. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  3133. end
  3134. else
  3135. begin
  3136. tmpreg:=cg.getintregister(list,OS_32);
  3137. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  3138. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3139. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3140. end;
  3141. if is_shifter_const(hi(value),b) then
  3142. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  3143. else
  3144. begin
  3145. tmpreg:=cg.getintregister(list,OS_32);
  3146. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3147. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  3148. end;
  3149. end;
  3150. OP_SUB:
  3151. begin
  3152. if is_shifter_const(lo(value),b) then
  3153. begin
  3154. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3155. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  3156. end
  3157. else
  3158. begin
  3159. tmpreg:=cg.getintregister(list,OS_32);
  3160. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  3161. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3162. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3163. end;
  3164. if is_shifter_const(hi(value),b) then
  3165. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))),PF_S))
  3166. else
  3167. begin
  3168. tmpreg:=cg.getintregister(list,OS_32);
  3169. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3170. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  3171. end;
  3172. end;
  3173. else
  3174. internalerror(200502131);
  3175. end;
  3176. if size=OS_64 then
  3177. begin
  3178. { the arm has an weired opinion how flags for SUB/ADD are handled }
  3179. ovloc.loc:=LOC_FLAGS;
  3180. case op of
  3181. OP_ADD:
  3182. ovloc.resflags:=F_CS;
  3183. OP_SUB:
  3184. ovloc.resflags:=F_CC;
  3185. end;
  3186. end;
  3187. end
  3188. else
  3189. begin
  3190. case op of
  3191. OP_AND,OP_OR,OP_XOR:
  3192. begin
  3193. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  3194. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  3195. end;
  3196. OP_ADD:
  3197. begin
  3198. if is_shifter_const(aint(lo(value)),b) then
  3199. begin
  3200. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3201. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  3202. end
  3203. else
  3204. begin
  3205. tmpreg:=cg.getintregister(list,OS_32);
  3206. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  3207. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3208. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3209. end;
  3210. if is_shifter_const(aint(hi(value)),b) then
  3211. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  3212. else
  3213. begin
  3214. tmpreg:=cg.getintregister(list,OS_32);
  3215. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  3216. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  3217. end;
  3218. end;
  3219. OP_SUB:
  3220. begin
  3221. if is_shifter_const(aint(lo(value)),b) then
  3222. begin
  3223. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3224. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  3225. end
  3226. else
  3227. begin
  3228. tmpreg:=cg.getintregister(list,OS_32);
  3229. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  3230. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3231. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3232. end;
  3233. if is_shifter_const(aint(hi(value)),b) then
  3234. list.concat(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  3235. else
  3236. begin
  3237. tmpreg:=cg.getintregister(list,OS_32);
  3238. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3239. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  3240. end;
  3241. end;
  3242. else
  3243. internalerror(2003083101);
  3244. end;
  3245. end;
  3246. end;
  3247. procedure tcg64farm.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3248. begin
  3249. ovloc.loc:=LOC_VOID;
  3250. case op of
  3251. OP_NEG,
  3252. OP_NOT :
  3253. internalerror(2012022502);
  3254. end;
  3255. if (setflags or tbasecgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  3256. begin
  3257. case op of
  3258. OP_ADD:
  3259. begin
  3260. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3261. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  3262. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi),PF_S));
  3263. end;
  3264. OP_SUB:
  3265. begin
  3266. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3267. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  3268. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi),PF_S));
  3269. end;
  3270. else
  3271. internalerror(2003083101);
  3272. end;
  3273. if size=OS_64 then
  3274. begin
  3275. { the arm has an weired opinion how flags for SUB/ADD are handled }
  3276. ovloc.loc:=LOC_FLAGS;
  3277. case op of
  3278. OP_ADD:
  3279. ovloc.resflags:=F_CS;
  3280. OP_SUB:
  3281. ovloc.resflags:=F_CC;
  3282. end;
  3283. end;
  3284. end
  3285. else
  3286. begin
  3287. case op of
  3288. OP_AND,OP_OR,OP_XOR:
  3289. begin
  3290. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  3291. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  3292. end;
  3293. OP_ADD:
  3294. begin
  3295. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3296. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  3297. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  3298. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3299. end;
  3300. OP_SUB:
  3301. begin
  3302. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3303. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  3304. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  3305. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3306. end;
  3307. else
  3308. internalerror(2003083101);
  3309. end;
  3310. end;
  3311. end;
  3312. procedure tthumbcgarm.init_register_allocators;
  3313. begin
  3314. inherited init_register_allocators;
  3315. if assigned(current_procinfo) and (current_procinfo.framepointer=NR_R7) then
  3316. rg[R_INTREGISTER]:=trgintcputhumb.create(R_INTREGISTER,R_SUBWHOLE,
  3317. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6],first_int_imreg,[])
  3318. else
  3319. rg[R_INTREGISTER]:=trgintcputhumb.create(R_INTREGISTER,R_SUBWHOLE,
  3320. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7],first_int_imreg,[]);
  3321. end;
  3322. procedure tthumbcgarm.done_register_allocators;
  3323. begin
  3324. rg[R_INTREGISTER].free;
  3325. rg[R_FPUREGISTER].free;
  3326. rg[R_MMREGISTER].free;
  3327. inherited done_register_allocators;
  3328. end;
  3329. procedure tthumbcgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  3330. var
  3331. ref : treference;
  3332. shift : byte;
  3333. r : byte;
  3334. regs, saveregs : tcpuregisterset;
  3335. r7offset,
  3336. stackmisalignment : pint;
  3337. postfix: toppostfix;
  3338. registerarea,
  3339. imm1, imm2: DWord;
  3340. stack_parameters: Boolean;
  3341. begin
  3342. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  3343. LocalSize:=align(LocalSize,4);
  3344. { call instruction does not put anything on the stack }
  3345. stackmisalignment:=0;
  3346. if not(nostackframe) then
  3347. begin
  3348. a_reg_alloc(list,NR_STACK_POINTER_REG);
  3349. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3350. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  3351. { save int registers }
  3352. reference_reset(ref,4);
  3353. ref.index:=NR_STACK_POINTER_REG;
  3354. ref.addressmode:=AM_PREINDEXED;
  3355. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3356. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3357. begin
  3358. //!!!! a_reg_alloc(list,NR_R12);
  3359. //!!!! list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  3360. end;
  3361. { the (old) ARM APCS requires saving both the stack pointer (to
  3362. crawl the stack) and the PC (to identify the function this
  3363. stack frame belongs to) -> also save R12 (= copy of R13 on entry)
  3364. and R15 -- still needs updating for EABI and Darwin, they don't
  3365. need that }
  3366. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3367. regs:=regs+[RS_R7,RS_R14]
  3368. else
  3369. // if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  3370. include(regs,RS_R14);
  3371. { safely estimate stack size }
  3372. if localsize+current_settings.alignment.localalignmax+4>508 then
  3373. begin
  3374. include(rg[R_INTREGISTER].used_in_proc,RS_R4);
  3375. include(regs,RS_R4);
  3376. end;
  3377. registerarea:=0;
  3378. if regs<>[] then
  3379. begin
  3380. for r:=RS_R0 to RS_R15 do
  3381. if r in regs then
  3382. inc(registerarea,4);
  3383. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,regs));
  3384. end;
  3385. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  3386. if stack_parameters or (LocalSize<>0) or
  3387. ((stackmisalignment<>0) and
  3388. ((pi_do_call in current_procinfo.flags) or
  3389. (po_assembler in current_procinfo.procdef.procoptions))) then
  3390. begin
  3391. { do we access stack parameters?
  3392. if yes, the previously estimated stacksize must be used }
  3393. if stack_parameters then
  3394. begin
  3395. if localsize>tarmprocinfo(current_procinfo).stackframesize then
  3396. begin
  3397. writeln(localsize);
  3398. writeln(tarmprocinfo(current_procinfo).stackframesize);
  3399. internalerror(2013040601);
  3400. end
  3401. else
  3402. localsize:=tarmprocinfo(current_procinfo).stackframesize-registerarea;
  3403. end
  3404. else
  3405. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3406. if localsize<508 then
  3407. begin
  3408. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  3409. end
  3410. else if localsize<=1016 then
  3411. begin
  3412. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,508));
  3413. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize-508));
  3414. end
  3415. else
  3416. begin
  3417. a_load_const_reg(list,OS_ADDR,-localsize,NR_R4);
  3418. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R4));
  3419. include(regs,RS_R4);
  3420. //!!!! if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3421. //!!!! a_reg_alloc(list,NR_R12);
  3422. //!!!! a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  3423. //!!!! list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  3424. //!!!! a_reg_dealloc(list,NR_R12);
  3425. end;
  3426. end;
  3427. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3428. begin
  3429. list.concat(taicpu.op_reg_reg_const(A_ADD,current_procinfo.framepointer,NR_STACK_POINTER_REG,0));
  3430. end;
  3431. end;
  3432. end;
  3433. procedure tthumbcgarm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  3434. var
  3435. ref : treference;
  3436. LocalSize : longint;
  3437. r,
  3438. shift : byte;
  3439. saveregs,
  3440. regs : tcpuregisterset;
  3441. registerarea : DWord;
  3442. stackmisalignment: pint;
  3443. imm1, imm2: DWord;
  3444. stack_parameters : Boolean;
  3445. begin
  3446. if not(nostackframe) then
  3447. begin
  3448. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  3449. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3450. include(regs,RS_R15);
  3451. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3452. include(regs,getsupreg(current_procinfo.framepointer));
  3453. registerarea:=0;
  3454. for r:=RS_R0 to RS_R15 do
  3455. if r in regs then
  3456. inc(registerarea,4);
  3457. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  3458. LocalSize:=current_procinfo.calc_stackframe_size;
  3459. if stack_parameters then
  3460. localsize:=tarmprocinfo(current_procinfo).stackframesize-registerarea
  3461. else
  3462. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3463. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  3464. (target_info.system in systems_darwin) then
  3465. begin
  3466. if (LocalSize<>0) or
  3467. ((stackmisalignment<>0) and
  3468. ((pi_do_call in current_procinfo.flags) or
  3469. (po_assembler in current_procinfo.procdef.procoptions))) then
  3470. begin
  3471. if LocalSize=0 then
  3472. else if LocalSize<=508 then
  3473. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize))
  3474. else if LocalSize<=1016 then
  3475. begin
  3476. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,508));
  3477. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize-508));
  3478. end
  3479. else
  3480. begin
  3481. a_reg_alloc(list,NR_R3);
  3482. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R3);
  3483. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R3));
  3484. a_reg_dealloc(list,NR_R3);
  3485. end;
  3486. end;
  3487. if regs=[] then
  3488. begin
  3489. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  3490. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  3491. else
  3492. list.concat(taicpu.op_reg(A_BX,NR_R14))
  3493. end
  3494. else
  3495. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,regs));
  3496. end;
  3497. end
  3498. else if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  3499. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  3500. else
  3501. list.concat(taicpu.op_reg(A_BX,NR_R14))
  3502. end;
  3503. procedure tthumbcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  3504. var
  3505. oppostfix:toppostfix;
  3506. usedtmpref: treference;
  3507. tmpreg,tmpreg2 : tregister;
  3508. dir : integer;
  3509. begin
  3510. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  3511. FromSize := ToSize;
  3512. case FromSize of
  3513. { signed integer registers }
  3514. OS_8:
  3515. oppostfix:=PF_B;
  3516. OS_S8:
  3517. oppostfix:=PF_SB;
  3518. OS_16:
  3519. oppostfix:=PF_H;
  3520. OS_S16:
  3521. oppostfix:=PF_SH;
  3522. OS_32,
  3523. OS_S32:
  3524. oppostfix:=PF_None;
  3525. else
  3526. InternalError(200308298);
  3527. end;
  3528. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  3529. begin
  3530. if target_info.endian=endian_big then
  3531. dir:=-1
  3532. else
  3533. dir:=1;
  3534. case FromSize of
  3535. OS_16,OS_S16:
  3536. begin
  3537. { only complicated references need an extra loadaddr }
  3538. if assigned(ref.symbol) or
  3539. (ref.index<>NR_NO) or
  3540. (ref.offset<-124) or
  3541. (ref.offset>124) or
  3542. { sometimes the compiler reused registers }
  3543. (reg=ref.index) or
  3544. (reg=ref.base) then
  3545. begin
  3546. tmpreg2:=getintregister(list,OS_INT);
  3547. a_loadaddr_ref_reg(list,ref,tmpreg2);
  3548. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  3549. end
  3550. else
  3551. usedtmpref:=ref;
  3552. if target_info.endian=endian_big then
  3553. inc(usedtmpref.offset,1);
  3554. tmpreg:=getintregister(list,OS_INT);
  3555. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  3556. inc(usedtmpref.offset,dir);
  3557. if FromSize=OS_16 then
  3558. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  3559. else
  3560. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  3561. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,8));
  3562. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3563. end;
  3564. OS_32,OS_S32:
  3565. begin
  3566. tmpreg:=getintregister(list,OS_INT);
  3567. { only complicated references need an extra loadaddr }
  3568. if assigned(ref.symbol) or
  3569. (ref.index<>NR_NO) or
  3570. (ref.offset<-124) or
  3571. (ref.offset>124) or
  3572. { sometimes the compiler reused registers }
  3573. (reg=ref.index) or
  3574. (reg=ref.base) then
  3575. begin
  3576. tmpreg2:=getintregister(list,OS_INT);
  3577. a_loadaddr_ref_reg(list,ref,tmpreg2);
  3578. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  3579. end
  3580. else
  3581. usedtmpref:=ref;
  3582. if ref.alignment=2 then
  3583. begin
  3584. if target_info.endian=endian_big then
  3585. inc(usedtmpref.offset,2);
  3586. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  3587. inc(usedtmpref.offset,dir*2);
  3588. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  3589. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,16));
  3590. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3591. end
  3592. else
  3593. begin
  3594. if target_info.endian=endian_big then
  3595. inc(usedtmpref.offset,3);
  3596. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  3597. inc(usedtmpref.offset,dir);
  3598. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3599. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,8));
  3600. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3601. inc(usedtmpref.offset,dir);
  3602. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3603. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,16));
  3604. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3605. inc(usedtmpref.offset,dir);
  3606. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3607. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,24));
  3608. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3609. end;
  3610. end
  3611. else
  3612. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3613. end;
  3614. end
  3615. else
  3616. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3617. if (fromsize=OS_S8) and (tosize = OS_16) then
  3618. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  3619. end;
  3620. procedure tthumbcgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  3621. var
  3622. imm_shift : byte;
  3623. l : tasmlabel;
  3624. hr : treference;
  3625. begin
  3626. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  3627. internalerror(2002090902);
  3628. if is_thumb_imm(a) then
  3629. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  3630. else
  3631. begin
  3632. reference_reset(hr,4);
  3633. current_asmdata.getjumplabel(l);
  3634. cg.a_label(current_procinfo.aktlocaldata,l);
  3635. hr.symboldata:=current_procinfo.aktlocaldata.last;
  3636. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  3637. hr.symbol:=l;
  3638. hr.base:=NR_PC;
  3639. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  3640. end;
  3641. end;
  3642. procedure tthumbcgarm.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  3643. var
  3644. hsym : tsym;
  3645. href,
  3646. tmpref : treference;
  3647. paraloc : Pcgparalocation;
  3648. l : TAsmLabel;
  3649. begin
  3650. { calculate the parameter info for the procdef }
  3651. procdef.init_paraloc_info(callerside);
  3652. hsym:=tsym(procdef.parast.Find('self'));
  3653. if not(assigned(hsym) and
  3654. (hsym.typ=paravarsym)) then
  3655. internalerror(200305251);
  3656. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  3657. while paraloc<>nil do
  3658. with paraloc^ do
  3659. begin
  3660. case loc of
  3661. LOC_REGISTER:
  3662. begin
  3663. if is_thumb_imm(ioffset) then
  3664. a_op_const_reg(list,OP_SUB,size,ioffset,register)
  3665. else
  3666. begin
  3667. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3668. reference_reset(tmpref,4);
  3669. current_asmdata.getjumplabel(l);
  3670. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3671. cg.a_label(current_procinfo.aktlocaldata,l);
  3672. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3673. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ioffset));
  3674. tmpref.symbol:=l;
  3675. tmpref.base:=NR_PC;
  3676. list.concat(taicpu.op_reg_ref(A_LDR,NR_R4,tmpref));
  3677. a_op_reg_reg(list,OP_SUB,size,NR_R4,register);
  3678. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3679. end;
  3680. end;
  3681. LOC_REFERENCE:
  3682. begin
  3683. { offset in the wrapper needs to be adjusted for the stored
  3684. return address }
  3685. reference_reset_base(href,reference.index,reference.offset+sizeof(aint),sizeof(pint));
  3686. if is_thumb_imm(ioffset) then
  3687. a_op_const_ref(list,OP_SUB,size,ioffset,href)
  3688. else
  3689. begin
  3690. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3691. reference_reset(tmpref,4);
  3692. current_asmdata.getjumplabel(l);
  3693. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3694. cg.a_label(current_procinfo.aktlocaldata,l);
  3695. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3696. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ioffset));
  3697. tmpref.symbol:=l;
  3698. tmpref.base:=NR_PC;
  3699. list.concat(taicpu.op_reg_ref(A_LDR,NR_R4,tmpref));
  3700. a_op_reg_ref(list,OP_SUB,size,NR_R4,href);
  3701. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3702. end;
  3703. end
  3704. else
  3705. internalerror(200309189);
  3706. end;
  3707. paraloc:=next;
  3708. end;
  3709. end;
  3710. function tthumbcgarm.handle_load_store(list: TAsmList; op: tasmop; oppostfix: toppostfix; reg: tregister; ref: treference): treference;
  3711. var
  3712. href : treference;
  3713. tmpreg : TRegister;
  3714. begin
  3715. href:=ref;
  3716. if (op in [A_STR,A_STRB,A_STRH]) and
  3717. (abs(ref.offset)>124) then
  3718. begin
  3719. tmpreg:=getintregister(list,OS_ADDR);
  3720. a_loadaddr_ref_reg(list,ref,tmpreg);
  3721. reference_reset_base(href,tmpreg,0,ref.alignment);
  3722. end
  3723. else if ((op=A_LDR) and (oppostfix in [PF_None]) and
  3724. (ref.base<>NR_STACK_POINTER_REG) and
  3725. (abs(ref.offset)>124)) or
  3726. { LDRB limitations }
  3727. (
  3728. (((op=A_LDR) and (oppostfix=PF_B)) or
  3729. ((op=A_LDRB) and (oppostfix=PF_None))) and
  3730. ((ref.base=NR_STACK_POINTER_REG) or
  3731. (ref.index=NR_STACK_POINTER_REG) or
  3732. (abs(ref.offset)>31)
  3733. )
  3734. ) or
  3735. { LDRH limitations }
  3736. (
  3737. (((op=A_LDR) and (oppostfix=PF_H)) or
  3738. ((op=A_LDRH) and (oppostfix=PF_None))) and
  3739. ((ref.base=NR_STACK_POINTER_REG) or
  3740. (ref.index=NR_STACK_POINTER_REG) or
  3741. (abs(ref.offset)>62) or
  3742. ((abs(ref.offset) mod 2)<>0)
  3743. )
  3744. ) then
  3745. begin
  3746. tmpreg:=getintregister(list,OS_ADDR);
  3747. a_loadaddr_ref_reg(list,ref,tmpreg);
  3748. reference_reset_base(href,tmpreg,0,ref.alignment);
  3749. end
  3750. else if (op=A_LDR) and
  3751. (oppostfix in [PF_None]) and
  3752. (ref.base=NR_STACK_POINTER_REG) and
  3753. (abs(ref.offset)>1020) then
  3754. begin
  3755. tmpreg:=getintregister(list,OS_ADDR);
  3756. a_loadaddr_ref_reg(list,ref,tmpreg);
  3757. reference_reset_base(href,tmpreg,0,ref.alignment);
  3758. end
  3759. else if (op=A_LDR) and
  3760. ((oppostfix in [PF_SH,PF_SB]) or
  3761. (abs(ref.offset)>124)) then
  3762. begin
  3763. tmpreg:=getintregister(list,OS_ADDR);
  3764. a_loadaddr_ref_reg(list,ref,tmpreg);
  3765. reference_reset_base(href,tmpreg,0,ref.alignment);
  3766. end;
  3767. Result:=inherited handle_load_store(list, op, oppostfix, reg, href);
  3768. end;
  3769. procedure tthumbcgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  3770. var
  3771. tmpreg,overflowreg : tregister;
  3772. asmop : tasmop;
  3773. begin
  3774. case op of
  3775. OP_NEG:
  3776. list.concat(taicpu.op_reg_reg(A_NEG,dst,src));
  3777. OP_NOT:
  3778. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  3779. OP_DIV,OP_IDIV:
  3780. internalerror(200308284);
  3781. OP_ROL:
  3782. begin
  3783. if not(size in [OS_32,OS_S32]) then
  3784. internalerror(2008072801);
  3785. { simulate ROL by ror'ing 32-value }
  3786. tmpreg:=getintregister(list,OS_32);
  3787. a_load_const_reg(list,OS_32,32,tmpreg);
  3788. list.concat(taicpu.op_reg_reg(A_SUB,tmpreg,src));
  3789. list.concat(taicpu.op_reg_reg(A_ROR,dst,src));
  3790. end;
  3791. else
  3792. begin
  3793. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3794. list.concat(setoppostfix(
  3795. taicpu.op_reg_reg(op_reg_opcg2asmop[op],dst,src),op_reg_postfix[op]));
  3796. end;
  3797. end;
  3798. maybeadjustresult(list,op,size,dst);
  3799. end;
  3800. procedure tthumbcgarm.a_op_const_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; dst: tregister);
  3801. var
  3802. tmpreg : tregister;
  3803. so : tshifterop;
  3804. l1 : longint;
  3805. imm1, imm2: DWord;
  3806. begin
  3807. //!!! ovloc.loc:=LOC_VOID;
  3808. if {$ifopt R+}(a<>-2147483648) and{$endif} {!!!!!! not setflags and } is_thumb_imm(-a) then
  3809. case op of
  3810. OP_ADD:
  3811. begin
  3812. op:=OP_SUB;
  3813. a:=aint(dword(-a));
  3814. end;
  3815. OP_SUB:
  3816. begin
  3817. op:=OP_ADD;
  3818. a:=aint(dword(-a));
  3819. end
  3820. end;
  3821. if is_thumb_imm(a) and (op in [OP_ADD,OP_SUB]) then
  3822. begin
  3823. // if cgsetflags or setflags then
  3824. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3825. list.concat(setoppostfix(
  3826. taicpu.op_reg_const(op_reg_opcg2asmop[op],dst,a),op_reg_postfix[op]));
  3827. if (cgsetflags {!!! or setflags }) and (size in [OS_8,OS_16,OS_32]) then
  3828. begin
  3829. //!!! ovloc.loc:=LOC_FLAGS;
  3830. case op of
  3831. OP_ADD:
  3832. //!!! ovloc.resflags:=F_CS;
  3833. ;
  3834. OP_SUB:
  3835. //!!! ovloc.resflags:=F_CC;
  3836. ;
  3837. end;
  3838. end;
  3839. end
  3840. else
  3841. begin
  3842. { there could be added some more sophisticated optimizations }
  3843. if (op in [OP_MUL,OP_IMUL,OP_DIV,OP_IDIV]) and (a=1) then
  3844. a_load_reg_reg(list,size,size,dst,dst)
  3845. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  3846. a_load_const_reg(list,size,0,dst)
  3847. else if (op in [OP_IMUL,OP_IDIV]) and (a=-1) then
  3848. a_op_reg_reg(list,OP_NEG,size,dst,dst)
  3849. { we do this here instead in the peephole optimizer because
  3850. it saves us a register }
  3851. {$ifdef DUMMY}
  3852. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  3853. a_op_const_reg_reg(list,OP_SHL,size,l1,dst,dst)
  3854. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  3855. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  3856. begin
  3857. if l1>32 then{roozbeh does this ever happen?}
  3858. internalerror(200308296);
  3859. shifterop_reset(so);
  3860. so.shiftmode:=SM_LSL;
  3861. so.shiftimm:=l1;
  3862. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,dst,dst,so));
  3863. end
  3864. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  3865. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  3866. begin
  3867. if l1>32 then{does this ever happen?}
  3868. internalerror(201205181);
  3869. shifterop_reset(so);
  3870. so.shiftmode:=SM_LSL;
  3871. so.shiftimm:=l1;
  3872. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,dst,dst,so));
  3873. end
  3874. else if (op in [OP_MUL,OP_IMUL]) and not(cgsetflags or setflags) and try_optimized_mul32_const_reg_reg(list,a,dst,dst) then
  3875. begin
  3876. { nothing to do on success }
  3877. end
  3878. {$endif DUMMY}
  3879. { x := y and 0; just clears a register, this sometimes gets generated on 64bit ops.
  3880. Just using mov x, #0 might allow some easier optimizations down the line. }
  3881. else if (op = OP_AND) and (dword(a)=0) then
  3882. list.concat(taicpu.op_reg_const(A_MOV,dst,0))
  3883. { x := y AND $FFFFFFFF just copies the register, so use mov for better optimizations }
  3884. else if (op = OP_AND) and (not(dword(a))=0) then
  3885. // do nothing
  3886. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  3887. broader range of shifterconstants.}
  3888. {$ifdef DUMMY}
  3889. else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  3890. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,not(dword(a))))
  3891. else if (op = OP_AND) and split_into_shifter_const(not(dword(a)), imm1, imm2) then
  3892. begin
  3893. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm1));
  3894. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm2));
  3895. end
  3896. else if (op in [OP_ADD, OP_SUB, OP_OR]) and
  3897. not(cgsetflags or setflags) and
  3898. split_into_shifter_const(a, imm1, imm2) then
  3899. begin
  3900. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm1));
  3901. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm2));
  3902. end
  3903. {$endif DUMMY}
  3904. else if (op in [OP_SHL, OP_SHR, OP_SAR]) then
  3905. begin
  3906. list.concat(taicpu.op_reg_reg_const(op_reg_opcg2asmop[op],dst,dst,a));
  3907. end
  3908. else
  3909. begin
  3910. tmpreg:=getintregister(list,size);
  3911. a_load_const_reg(list,size,a,tmpreg);
  3912. a_op_reg_reg(list,op,size,tmpreg,dst);
  3913. end;
  3914. end;
  3915. maybeadjustresult(list,op,size,dst);
  3916. end;
  3917. procedure tthumbcgarm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister);
  3918. begin
  3919. if (op=OP_ADD) and (src=NR_R13) and (dst<>NR_R13) and ((a mod 4)=0) and (a>0) and (a<=1020) then
  3920. list.concat(taicpu.op_reg_reg_const(A_ADD,dst,src,a))
  3921. else
  3922. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  3923. end;
  3924. procedure tthumbcgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  3925. var
  3926. l1,l2 : tasmlabel;
  3927. ai : taicpu;
  3928. begin
  3929. current_asmdata.getjumplabel(l1);
  3930. current_asmdata.getjumplabel(l2);
  3931. ai:=setcondition(taicpu.op_sym(A_B,l1),flags_to_cond(f));
  3932. ai.is_jmp:=true;
  3933. list.concat(ai);
  3934. list.concat(taicpu.op_reg_const(A_MOV,reg,0));
  3935. list.concat(taicpu.op_sym(A_B,l2));
  3936. cg.a_label(list,l1);
  3937. list.concat(taicpu.op_reg_const(A_MOV,reg,1));
  3938. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3939. cg.a_label(list,l2);
  3940. end;
  3941. procedure tthumbcgarm.g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string);
  3942. var
  3943. tmpref : treference;
  3944. l : tasmlabel;
  3945. begin
  3946. { there is no branch instruction on thumb which allows big distances and which leaves LR as it is
  3947. and which allows to switch the instruction set }
  3948. { create const entry }
  3949. reference_reset(tmpref,4);
  3950. current_asmdata.getjumplabel(l);
  3951. tmpref.symbol:=l;
  3952. tmpref.base:=NR_PC;
  3953. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3954. list.concat(taicpu.op_reg_ref(A_LDR,NR_R0,tmpref));
  3955. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  3956. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3957. list.concat(taicpu.op_reg(A_BX,NR_R12));
  3958. { append const entry }
  3959. list.Concat(tai_align.Create(4));
  3960. list.Concat(tai_label.create(l));
  3961. list.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(externalname)));
  3962. end;
  3963. procedure tthumb2cgarm.init_register_allocators;
  3964. begin
  3965. inherited init_register_allocators;
  3966. { currently, we save R14 always, so we can use it }
  3967. if (target_info.system<>system_arm_darwin) then
  3968. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  3969. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  3970. RS_R9,RS_R10,RS_R12,RS_R14],first_int_imreg,[])
  3971. else
  3972. { r9 is not available on Darwin according to the llvm code generator }
  3973. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  3974. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  3975. RS_R10,RS_R12,RS_R14],first_int_imreg,[]);
  3976. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  3977. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  3978. if current_settings.fputype in [fpu_fpv4_s16,fpu_vfpv3_d16] then
  3979. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  3980. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  3981. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  3982. ],first_mm_imreg,[])
  3983. else
  3984. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  3985. [RS_S0,RS_S1,RS_R2,RS_R3,RS_R4,RS_S31],first_mm_imreg,[]);
  3986. end;
  3987. procedure tthumb2cgarm.done_register_allocators;
  3988. begin
  3989. rg[R_INTREGISTER].free;
  3990. rg[R_FPUREGISTER].free;
  3991. rg[R_MMREGISTER].free;
  3992. inherited done_register_allocators;
  3993. end;
  3994. procedure tthumb2cgarm.a_call_reg(list : TAsmList;reg: tregister);
  3995. begin
  3996. list.concat(taicpu.op_reg(A_BLX, reg));
  3997. {
  3998. the compiler does not properly set this flag anymore in pass 1, and
  3999. for now we only need it after pass 2 (I hope) (JM)
  4000. if not(pi_do_call in current_procinfo.flags) then
  4001. internalerror(2003060703);
  4002. }
  4003. include(current_procinfo.flags,pi_do_call);
  4004. end;
  4005. procedure tthumb2cgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  4006. var
  4007. imm_shift : byte;
  4008. l : tasmlabel;
  4009. hr : treference;
  4010. begin
  4011. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  4012. internalerror(2002090902);
  4013. if is_thumb32_imm(a) then
  4014. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  4015. else if is_thumb32_imm(not(a)) then
  4016. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  4017. else if (a and $FFFF)=a then
  4018. list.concat(taicpu.op_reg_const(A_MOVW,reg,a))
  4019. else
  4020. begin
  4021. reference_reset(hr,4);
  4022. current_asmdata.getjumplabel(l);
  4023. cg.a_label(current_procinfo.aktlocaldata,l);
  4024. hr.symboldata:=current_procinfo.aktlocaldata.last;
  4025. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  4026. hr.symbol:=l;
  4027. hr.base:=NR_PC;
  4028. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  4029. end;
  4030. end;
  4031. procedure tthumb2cgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  4032. var
  4033. oppostfix:toppostfix;
  4034. usedtmpref: treference;
  4035. tmpreg,tmpreg2 : tregister;
  4036. so : tshifterop;
  4037. dir : integer;
  4038. begin
  4039. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  4040. FromSize := ToSize;
  4041. case FromSize of
  4042. { signed integer registers }
  4043. OS_8:
  4044. oppostfix:=PF_B;
  4045. OS_S8:
  4046. oppostfix:=PF_SB;
  4047. OS_16:
  4048. oppostfix:=PF_H;
  4049. OS_S16:
  4050. oppostfix:=PF_SH;
  4051. OS_32,
  4052. OS_S32:
  4053. oppostfix:=PF_None;
  4054. else
  4055. InternalError(200308299);
  4056. end;
  4057. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  4058. begin
  4059. if target_info.endian=endian_big then
  4060. dir:=-1
  4061. else
  4062. dir:=1;
  4063. case FromSize of
  4064. OS_16,OS_S16:
  4065. begin
  4066. { only complicated references need an extra loadaddr }
  4067. if assigned(ref.symbol) or
  4068. (ref.index<>NR_NO) or
  4069. (ref.offset<-255) or
  4070. (ref.offset>4094) or
  4071. { sometimes the compiler reused registers }
  4072. (reg=ref.index) or
  4073. (reg=ref.base) then
  4074. begin
  4075. tmpreg2:=getintregister(list,OS_INT);
  4076. a_loadaddr_ref_reg(list,ref,tmpreg2);
  4077. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  4078. end
  4079. else
  4080. usedtmpref:=ref;
  4081. if target_info.endian=endian_big then
  4082. inc(usedtmpref.offset,1);
  4083. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  4084. tmpreg:=getintregister(list,OS_INT);
  4085. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  4086. inc(usedtmpref.offset,dir);
  4087. if FromSize=OS_16 then
  4088. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  4089. else
  4090. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  4091. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4092. end;
  4093. OS_32,OS_S32:
  4094. begin
  4095. tmpreg:=getintregister(list,OS_INT);
  4096. { only complicated references need an extra loadaddr }
  4097. if assigned(ref.symbol) or
  4098. (ref.index<>NR_NO) or
  4099. (ref.offset<-255) or
  4100. (ref.offset>4092) or
  4101. { sometimes the compiler reused registers }
  4102. (reg=ref.index) or
  4103. (reg=ref.base) then
  4104. begin
  4105. tmpreg2:=getintregister(list,OS_INT);
  4106. a_loadaddr_ref_reg(list,ref,tmpreg2);
  4107. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  4108. end
  4109. else
  4110. usedtmpref:=ref;
  4111. shifterop_reset(so);so.shiftmode:=SM_LSL;
  4112. if ref.alignment=2 then
  4113. begin
  4114. if target_info.endian=endian_big then
  4115. inc(usedtmpref.offset,2);
  4116. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  4117. inc(usedtmpref.offset,dir*2);
  4118. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  4119. so.shiftimm:=16;
  4120. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4121. end
  4122. else
  4123. begin
  4124. if target_info.endian=endian_big then
  4125. inc(usedtmpref.offset,3);
  4126. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  4127. inc(usedtmpref.offset,dir);
  4128. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4129. so.shiftimm:=8;
  4130. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4131. inc(usedtmpref.offset,dir);
  4132. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4133. so.shiftimm:=16;
  4134. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4135. inc(usedtmpref.offset,dir);
  4136. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4137. so.shiftimm:=24;
  4138. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4139. end;
  4140. end
  4141. else
  4142. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  4143. end;
  4144. end
  4145. else
  4146. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  4147. if (fromsize=OS_S8) and (tosize = OS_16) then
  4148. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  4149. end;
  4150. procedure tthumb2cgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  4151. begin
  4152. if op = OP_NOT then
  4153. begin
  4154. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  4155. case size of
  4156. OS_8: list.concat(taicpu.op_reg_reg(A_UXTB,dst,dst));
  4157. OS_S8: list.concat(taicpu.op_reg_reg(A_SXTB,dst,dst));
  4158. OS_16: list.concat(taicpu.op_reg_reg(A_UXTH,dst,dst));
  4159. OS_S16: list.concat(taicpu.op_reg_reg(A_SXTH,dst,dst));
  4160. end;
  4161. end
  4162. else
  4163. inherited a_op_reg_reg(list, op, size, src, dst);
  4164. end;
  4165. procedure tthumb2cgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  4166. var
  4167. shift, width : byte;
  4168. tmpreg : tregister;
  4169. so : tshifterop;
  4170. l1 : longint;
  4171. begin
  4172. ovloc.loc:=LOC_VOID;
  4173. if {$ifopt R+}(a<>-2147483648) and{$endif} is_shifter_const(-a,shift) then
  4174. case op of
  4175. OP_ADD:
  4176. begin
  4177. op:=OP_SUB;
  4178. a:=aint(dword(-a));
  4179. end;
  4180. OP_SUB:
  4181. begin
  4182. op:=OP_ADD;
  4183. a:=aint(dword(-a));
  4184. end
  4185. end;
  4186. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  4187. case op of
  4188. OP_NEG,OP_NOT,
  4189. OP_DIV,OP_IDIV:
  4190. internalerror(200308285);
  4191. OP_SHL:
  4192. begin
  4193. if a>32 then
  4194. internalerror(2014020703);
  4195. if a<>0 then
  4196. begin
  4197. shifterop_reset(so);
  4198. so.shiftmode:=SM_LSL;
  4199. so.shiftimm:=a;
  4200. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4201. end
  4202. else
  4203. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4204. end;
  4205. OP_ROL:
  4206. begin
  4207. if a>32 then
  4208. internalerror(2014020704);
  4209. if a<>0 then
  4210. begin
  4211. shifterop_reset(so);
  4212. so.shiftmode:=SM_ROR;
  4213. so.shiftimm:=32-a;
  4214. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4215. end
  4216. else
  4217. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4218. end;
  4219. OP_ROR:
  4220. begin
  4221. if a>32 then
  4222. internalerror(2014020705);
  4223. if a<>0 then
  4224. begin
  4225. shifterop_reset(so);
  4226. so.shiftmode:=SM_ROR;
  4227. so.shiftimm:=a;
  4228. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4229. end
  4230. else
  4231. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4232. end;
  4233. OP_SHR:
  4234. begin
  4235. if a>32 then
  4236. internalerror(200308292);
  4237. shifterop_reset(so);
  4238. if a<>0 then
  4239. begin
  4240. so.shiftmode:=SM_LSR;
  4241. so.shiftimm:=a;
  4242. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4243. end
  4244. else
  4245. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4246. end;
  4247. OP_SAR:
  4248. begin
  4249. if a>32 then
  4250. internalerror(200308295);
  4251. if a<>0 then
  4252. begin
  4253. shifterop_reset(so);
  4254. so.shiftmode:=SM_ASR;
  4255. so.shiftimm:=a;
  4256. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4257. end
  4258. else
  4259. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4260. end;
  4261. else
  4262. if (op in [OP_SUB, OP_ADD]) and
  4263. ((a < 0) or
  4264. (a > 4095)) then
  4265. begin
  4266. tmpreg:=getintregister(list,size);
  4267. a_load_const_reg(list, size, a, tmpreg);
  4268. if cgsetflags or setflags then
  4269. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4270. list.concat(setoppostfix(
  4271. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src,tmpreg),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4272. end
  4273. else
  4274. begin
  4275. if cgsetflags or setflags then
  4276. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4277. list.concat(setoppostfix(
  4278. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4279. end;
  4280. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  4281. begin
  4282. ovloc.loc:=LOC_FLAGS;
  4283. case op of
  4284. OP_ADD:
  4285. ovloc.resflags:=F_CS;
  4286. OP_SUB:
  4287. ovloc.resflags:=F_CC;
  4288. end;
  4289. end;
  4290. end
  4291. else
  4292. begin
  4293. { there could be added some more sophisticated optimizations }
  4294. if (op in [OP_MUL,OP_IMUL]) and (a=1) then
  4295. a_load_reg_reg(list,size,size,src,dst)
  4296. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  4297. a_load_const_reg(list,size,0,dst)
  4298. else if (op in [OP_IMUL]) and (a=-1) then
  4299. a_op_reg_reg(list,OP_NEG,size,src,dst)
  4300. { we do this here instead in the peephole optimizer because
  4301. it saves us a register }
  4302. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  4303. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  4304. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  4305. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  4306. begin
  4307. if l1>32 then{roozbeh does this ever happen?}
  4308. internalerror(200308296);
  4309. shifterop_reset(so);
  4310. so.shiftmode:=SM_LSL;
  4311. so.shiftimm:=l1;
  4312. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  4313. end
  4314. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  4315. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  4316. begin
  4317. if l1>32 then{does this ever happen?}
  4318. internalerror(201205181);
  4319. shifterop_reset(so);
  4320. so.shiftmode:=SM_LSL;
  4321. so.shiftimm:=l1;
  4322. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,so));
  4323. end
  4324. else if (op in [OP_MUL,OP_IMUL]) and not(cgsetflags or setflags) and try_optimized_mul32_const_reg_reg(list,a,src,dst) then
  4325. begin
  4326. { nothing to do on success }
  4327. end
  4328. { x := y and 0; just clears a register, this sometimes gets generated on 64bit ops.
  4329. Just using mov x, #0 might allow some easier optimizations down the line. }
  4330. else if (op = OP_AND) and (dword(a)=0) then
  4331. list.concat(taicpu.op_reg_const(A_MOV,dst,0))
  4332. { x := y AND $FFFFFFFF just copies the register, so use mov for better optimizations }
  4333. else if (op = OP_AND) and (not(dword(a))=0) then
  4334. list.concat(taicpu.op_reg_reg(A_MOV,dst,src))
  4335. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  4336. broader range of shifterconstants.}
  4337. {else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  4338. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))}
  4339. else if (op = OP_AND) and is_thumb32_imm(a) then
  4340. list.concat(taicpu.op_reg_reg_const(A_AND,dst,src,dword(a)))
  4341. else if (op = OP_AND) and (a = $FFFF) then
  4342. list.concat(taicpu.op_reg_reg(A_UXTH,dst,src))
  4343. else if (op = OP_AND) and is_thumb32_imm(not(dword(a))) then
  4344. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))
  4345. else if (op = OP_AND) and is_continuous_mask(not(a), shift, width) then
  4346. begin
  4347. a_load_reg_reg(list,size,size,src,dst);
  4348. list.concat(taicpu.op_reg_const_const(A_BFC,dst,shift,width))
  4349. end
  4350. else
  4351. begin
  4352. tmpreg:=getintregister(list,size);
  4353. a_load_const_reg(list,size,a,tmpreg);
  4354. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  4355. end;
  4356. end;
  4357. maybeadjustresult(list,op,size,dst);
  4358. end;
  4359. const
  4360. op_reg_reg_opcg2asmopThumb2: array[TOpCG] of tasmop =
  4361. (A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_MUL,A_MUL,A_NONE,A_MVN,A_ORR,
  4362. A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_NONE,A_ROR);
  4363. procedure tthumb2cgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  4364. var
  4365. so : tshifterop;
  4366. tmpreg,overflowreg : tregister;
  4367. asmop : tasmop;
  4368. begin
  4369. ovloc.loc:=LOC_VOID;
  4370. case op of
  4371. OP_NEG,OP_NOT:
  4372. internalerror(200308286);
  4373. OP_ROL:
  4374. begin
  4375. if not(size in [OS_32,OS_S32]) then
  4376. internalerror(2008072801);
  4377. { simulate ROL by ror'ing 32-value }
  4378. tmpreg:=getintregister(list,OS_32);
  4379. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,32));
  4380. list.concat(taicpu.op_reg_reg_reg(A_SUB,src1,tmpreg,src1));
  4381. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  4382. end;
  4383. OP_ROR:
  4384. begin
  4385. if not(size in [OS_32,OS_S32]) then
  4386. internalerror(2008072802);
  4387. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  4388. end;
  4389. OP_IMUL,
  4390. OP_MUL:
  4391. begin
  4392. if cgsetflags or setflags then
  4393. begin
  4394. overflowreg:=getintregister(list,size);
  4395. if op=OP_IMUL then
  4396. asmop:=A_SMULL
  4397. else
  4398. asmop:=A_UMULL;
  4399. { the arm doesn't allow that rd and rm are the same }
  4400. if dst=src2 then
  4401. begin
  4402. if dst<>src1 then
  4403. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  4404. else
  4405. begin
  4406. tmpreg:=getintregister(list,size);
  4407. a_load_reg_reg(list,size,size,src2,dst);
  4408. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  4409. end;
  4410. end
  4411. else
  4412. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  4413. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4414. if op=OP_IMUL then
  4415. begin
  4416. shifterop_reset(so);
  4417. so.shiftmode:=SM_ASR;
  4418. so.shiftimm:=31;
  4419. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  4420. end
  4421. else
  4422. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  4423. ovloc.loc:=LOC_FLAGS;
  4424. ovloc.resflags:=F_NE;
  4425. end
  4426. else
  4427. begin
  4428. { the arm doesn't allow that rd and rm are the same }
  4429. if dst=src2 then
  4430. begin
  4431. if dst<>src1 then
  4432. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  4433. else
  4434. begin
  4435. tmpreg:=getintregister(list,size);
  4436. a_load_reg_reg(list,size,size,src2,dst);
  4437. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  4438. end;
  4439. end
  4440. else
  4441. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  4442. end;
  4443. end;
  4444. else
  4445. begin
  4446. if cgsetflags or setflags then
  4447. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4448. {$ifdef dummy}
  4449. { R13 is not allowed for certain instruction operands }
  4450. if op_reg_reg_opcg2asmopThumb2[op] in [A_ADD,A_SUB,A_AND,A_BIC,A_EOR] then
  4451. begin
  4452. if getsupreg(dst)=RS_R13 then
  4453. begin
  4454. tmpreg:=getintregister(list,OS_INT);
  4455. a_load_reg_reg(list,OS_INT,OS_INT,dst,tmpreg);
  4456. dst:=tmpreg;
  4457. end;
  4458. if getsupreg(src1)=RS_R13 then
  4459. begin
  4460. tmpreg:=getintregister(list,OS_INT);
  4461. a_load_reg_reg(list,OS_INT,OS_INT,src1,tmpreg);
  4462. src1:=tmpreg;
  4463. end;
  4464. end;
  4465. {$endif}
  4466. list.concat(setoppostfix(
  4467. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmopThumb2[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4468. end;
  4469. end;
  4470. maybeadjustresult(list,op,size,dst);
  4471. end;
  4472. procedure tthumb2cgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  4473. var item: taicpu;
  4474. begin
  4475. list.concat(taicpu.op_cond(A_ITE, flags_to_cond(f)));
  4476. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  4477. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  4478. end;
  4479. procedure tthumb2cgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  4480. var
  4481. ref : treference;
  4482. shift : byte;
  4483. firstfloatreg,lastfloatreg,
  4484. r : byte;
  4485. regs : tcpuregisterset;
  4486. stackmisalignment: pint;
  4487. begin
  4488. LocalSize:=align(LocalSize,4);
  4489. { call instruction does not put anything on the stack }
  4490. stackmisalignment:=0;
  4491. if not(nostackframe) then
  4492. begin
  4493. firstfloatreg:=RS_NO;
  4494. lastfloatreg:=RS_NO;
  4495. { save floating point registers? }
  4496. for r:=RS_F0 to RS_F7 do
  4497. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  4498. begin
  4499. if firstfloatreg=RS_NO then
  4500. firstfloatreg:=r;
  4501. lastfloatreg:=r;
  4502. inc(stackmisalignment,12);
  4503. end;
  4504. a_reg_alloc(list,NR_STACK_POINTER_REG);
  4505. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4506. begin
  4507. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  4508. a_reg_alloc(list,NR_R12);
  4509. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  4510. end;
  4511. { save int registers }
  4512. reference_reset(ref,4);
  4513. ref.index:=NR_STACK_POINTER_REG;
  4514. ref.addressmode:=AM_PREINDEXED;
  4515. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  4516. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4517. regs:=regs+[RS_FRAME_POINTER_REG,RS_R14]
  4518. else if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  4519. include(regs,RS_R14);
  4520. if regs<>[] then
  4521. begin
  4522. for r:=RS_R0 to RS_R15 do
  4523. if (r in regs) then
  4524. inc(stackmisalignment,4);
  4525. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  4526. end;
  4527. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4528. begin
  4529. { the framepointer now points to the saved R15, so the saved
  4530. framepointer is at R11-12 (for get_caller_frame) }
  4531. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  4532. a_reg_dealloc(list,NR_R12);
  4533. end;
  4534. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  4535. if (LocalSize<>0) or
  4536. ((stackmisalignment<>0) and
  4537. ((pi_do_call in current_procinfo.flags) or
  4538. (po_assembler in current_procinfo.procdef.procoptions))) then
  4539. begin
  4540. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  4541. if not(is_shifter_const(localsize,shift)) then
  4542. begin
  4543. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  4544. a_reg_alloc(list,NR_R12);
  4545. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  4546. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  4547. a_reg_dealloc(list,NR_R12);
  4548. end
  4549. else
  4550. begin
  4551. a_reg_dealloc(list,NR_R12);
  4552. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  4553. end;
  4554. end;
  4555. if firstfloatreg<>RS_NO then
  4556. begin
  4557. reference_reset(ref,4);
  4558. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  4559. begin
  4560. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  4561. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  4562. ref.base:=NR_R12;
  4563. end
  4564. else
  4565. begin
  4566. ref.base:=current_procinfo.framepointer;
  4567. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  4568. end;
  4569. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  4570. lastfloatreg-firstfloatreg+1,ref));
  4571. end;
  4572. end;
  4573. end;
  4574. procedure tthumb2cgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  4575. var
  4576. ref : treference;
  4577. firstfloatreg,lastfloatreg,
  4578. r : byte;
  4579. shift : byte;
  4580. regs : tcpuregisterset;
  4581. LocalSize : longint;
  4582. stackmisalignment: pint;
  4583. begin
  4584. if not(nostackframe) then
  4585. begin
  4586. stackmisalignment:=0;
  4587. { restore floating point register }
  4588. firstfloatreg:=RS_NO;
  4589. lastfloatreg:=RS_NO;
  4590. { save floating point registers? }
  4591. for r:=RS_F0 to RS_F7 do
  4592. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  4593. begin
  4594. if firstfloatreg=RS_NO then
  4595. firstfloatreg:=r;
  4596. lastfloatreg:=r;
  4597. { floating point register space is already included in
  4598. localsize below by calc_stackframe_size
  4599. inc(stackmisalignment,12);
  4600. }
  4601. end;
  4602. if firstfloatreg<>RS_NO then
  4603. begin
  4604. reference_reset(ref,4);
  4605. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  4606. begin
  4607. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  4608. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  4609. ref.base:=NR_R12;
  4610. end
  4611. else
  4612. begin
  4613. ref.base:=current_procinfo.framepointer;
  4614. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  4615. end;
  4616. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  4617. lastfloatreg-firstfloatreg+1,ref));
  4618. end;
  4619. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  4620. if (pi_do_call in current_procinfo.flags) or (regs<>[]) then
  4621. begin
  4622. exclude(regs,RS_R14);
  4623. include(regs,RS_R15);
  4624. end;
  4625. if (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  4626. regs:=regs+[RS_FRAME_POINTER_REG,RS_R15];
  4627. for r:=RS_R0 to RS_R15 do
  4628. if (r in regs) then
  4629. inc(stackmisalignment,4);
  4630. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  4631. LocalSize:=current_procinfo.calc_stackframe_size;
  4632. if (LocalSize<>0) or
  4633. ((stackmisalignment<>0) and
  4634. ((pi_do_call in current_procinfo.flags) or
  4635. (po_assembler in current_procinfo.procdef.procoptions))) then
  4636. begin
  4637. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  4638. if not(is_shifter_const(LocalSize,shift)) then
  4639. begin
  4640. a_reg_alloc(list,NR_R12);
  4641. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  4642. list.concat(taicpu.op_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_R12));
  4643. a_reg_dealloc(list,NR_R12);
  4644. end
  4645. else
  4646. begin
  4647. a_reg_dealloc(list,NR_R12);
  4648. list.concat(taicpu.op_reg_const(A_ADD,NR_STACK_POINTER_REG,LocalSize));
  4649. end;
  4650. end;
  4651. if regs=[] then
  4652. list.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14))
  4653. else
  4654. begin
  4655. reference_reset(ref,4);
  4656. ref.index:=NR_STACK_POINTER_REG;
  4657. ref.addressmode:=AM_PREINDEXED;
  4658. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  4659. end;
  4660. end
  4661. else
  4662. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14));
  4663. end;
  4664. function tthumb2cgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  4665. var
  4666. tmpreg : tregister;
  4667. tmpref : treference;
  4668. l : tasmlabel;
  4669. so: tshifterop;
  4670. begin
  4671. tmpreg:=NR_NO;
  4672. { Be sure to have a base register }
  4673. if (ref.base=NR_NO) then
  4674. begin
  4675. if ref.shiftmode<>SM_None then
  4676. internalerror(2014020706);
  4677. ref.base:=ref.index;
  4678. ref.index:=NR_NO;
  4679. end;
  4680. { absolute symbols can't be handled directly, we've to store the symbol reference
  4681. in the text segment and access it pc relative
  4682. For now, we assume that references where base or index equals to PC are already
  4683. relative, all other references are assumed to be absolute and thus they need
  4684. to be handled extra.
  4685. A proper solution would be to change refoptions to a set and store the information
  4686. if the symbol is absolute or relative there.
  4687. }
  4688. if (assigned(ref.symbol) and
  4689. not(is_pc(ref.base)) and
  4690. not(is_pc(ref.index))
  4691. ) or
  4692. { [#xxx] isn't a valid address operand }
  4693. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  4694. //(ref.offset<-4095) or
  4695. (ref.offset<-255) or
  4696. (ref.offset>4095) or
  4697. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  4698. ((ref.offset<-255) or
  4699. (ref.offset>255)
  4700. )
  4701. ) or
  4702. (((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and
  4703. ((ref.offset<-1020) or
  4704. (ref.offset>1020) or
  4705. ((abs(ref.offset) mod 4)<>0) or
  4706. { the usual pc relative symbol handling assumes possible offsets of +/- 4095 }
  4707. assigned(ref.symbol)
  4708. )
  4709. ) then
  4710. begin
  4711. reference_reset(tmpref,4);
  4712. { load symbol }
  4713. tmpreg:=getintregister(list,OS_INT);
  4714. if assigned(ref.symbol) then
  4715. begin
  4716. current_asmdata.getjumplabel(l);
  4717. cg.a_label(current_procinfo.aktlocaldata,l);
  4718. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  4719. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset));
  4720. { load consts entry }
  4721. tmpref.symbol:=l;
  4722. tmpref.base:=NR_R15;
  4723. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  4724. { in case of LDF/STF, we got rid of the NR_R15 }
  4725. if is_pc(ref.base) then
  4726. ref.base:=NR_NO;
  4727. if is_pc(ref.index) then
  4728. ref.index:=NR_NO;
  4729. end
  4730. else
  4731. a_load_const_reg(list,OS_ADDR,ref.offset,tmpreg);
  4732. if (ref.base<>NR_NO) then
  4733. begin
  4734. if ref.index<>NR_NO then
  4735. begin
  4736. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  4737. ref.base:=tmpreg;
  4738. end
  4739. else
  4740. begin
  4741. ref.index:=tmpreg;
  4742. ref.shiftimm:=0;
  4743. ref.signindex:=1;
  4744. ref.shiftmode:=SM_None;
  4745. end;
  4746. end
  4747. else
  4748. ref.base:=tmpreg;
  4749. ref.offset:=0;
  4750. ref.symbol:=nil;
  4751. end;
  4752. if (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  4753. begin
  4754. if tmpreg<>NR_NO then
  4755. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg,tmpreg)
  4756. else
  4757. begin
  4758. tmpreg:=getintregister(list,OS_ADDR);
  4759. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg);
  4760. ref.base:=tmpreg;
  4761. end;
  4762. ref.offset:=0;
  4763. end;
  4764. { Hack? Thumb2 doesn't allow PC indexed addressing modes(although it does in the specification) }
  4765. if (ref.base=NR_R15) and (ref.index<>NR_NO) and (ref.shiftmode <> sm_none) then
  4766. begin
  4767. tmpreg:=getintregister(list,OS_ADDR);
  4768. list.concat(taicpu.op_reg_reg(A_MOV, tmpreg, NR_R15));
  4769. ref.base := tmpreg;
  4770. end;
  4771. { floating point operations have only limited references
  4772. we expect here, that a base is already set }
  4773. if ((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and (ref.index<>NR_NO) then
  4774. begin
  4775. if ref.shiftmode<>SM_none then
  4776. internalerror(200309121);
  4777. if tmpreg<>NR_NO then
  4778. begin
  4779. if ref.base=tmpreg then
  4780. begin
  4781. if ref.signindex<0 then
  4782. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,tmpreg,ref.index))
  4783. else
  4784. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,tmpreg,ref.index));
  4785. ref.index:=NR_NO;
  4786. end
  4787. else
  4788. begin
  4789. if ref.index<>tmpreg then
  4790. internalerror(200403161);
  4791. if ref.signindex<0 then
  4792. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,ref.base,tmpreg))
  4793. else
  4794. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  4795. ref.base:=tmpreg;
  4796. ref.index:=NR_NO;
  4797. end;
  4798. end
  4799. else
  4800. begin
  4801. tmpreg:=getintregister(list,OS_ADDR);
  4802. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,ref.index));
  4803. ref.base:=tmpreg;
  4804. ref.index:=NR_NO;
  4805. end;
  4806. end;
  4807. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  4808. Result := ref;
  4809. end;
  4810. procedure tthumb2cgarm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  4811. var
  4812. instr: taicpu;
  4813. begin
  4814. if (fromsize=OS_F32) and
  4815. (tosize=OS_F32) then
  4816. begin
  4817. instr:=setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32);
  4818. list.Concat(instr);
  4819. add_move_instruction(instr);
  4820. end
  4821. else if (fromsize=OS_F64) and
  4822. (tosize=OS_F64) then
  4823. begin
  4824. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,tregister(longint(reg2)+1),tregister(longint(reg1)+1)), PF_F32));
  4825. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32));
  4826. end
  4827. else if (fromsize=OS_F32) and
  4828. (tosize=OS_F64) then
  4829. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,reg2,reg1), PF_F32))
  4830. begin
  4831. //list.concat(nil);
  4832. end;
  4833. end;
  4834. procedure tthumb2cgarm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  4835. begin
  4836. if fromsize=OS_F32 then
  4837. handle_load_store(list,A_VLDR,PF_F32,reg,ref)
  4838. else
  4839. handle_load_store(list,A_VLDR,PF_F64,reg,ref);
  4840. end;
  4841. procedure tthumb2cgarm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  4842. begin
  4843. if fromsize=OS_F32 then
  4844. handle_load_store(list,A_VSTR,PF_F32,reg,ref)
  4845. else
  4846. handle_load_store(list,A_VSTR,PF_F64,reg,ref);
  4847. end;
  4848. procedure tthumb2cgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  4849. begin
  4850. if //(shuffle=nil) and
  4851. (tosize=OS_F32) then
  4852. list.Concat(taicpu.op_reg_reg(A_VMOV,mmreg,intreg))
  4853. else
  4854. internalerror(2012100813);
  4855. end;
  4856. procedure tthumb2cgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tcgsize; mmreg, intreg: tregister; shuffle: pmmshuffle);
  4857. begin
  4858. if //(shuffle=nil) and
  4859. (fromsize=OS_F32) then
  4860. list.Concat(taicpu.op_reg_reg(A_VMOV,intreg,mmreg))
  4861. else
  4862. internalerror(2012100814);
  4863. end;
  4864. procedure tthumb2cg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  4865. var tmpreg: tregister;
  4866. begin
  4867. case op of
  4868. OP_NEG:
  4869. begin
  4870. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4871. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  4872. tmpreg:=cg.getintregister(list,OS_32);
  4873. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,0));
  4874. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,tmpreg,regsrc.reghi));
  4875. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  4876. end;
  4877. else
  4878. inherited a_op64_reg_reg(list, op, size, regsrc, regdst);
  4879. end;
  4880. end;
  4881. procedure tthumbcg64farm.a_op64_reg_reg(list: TAsmList; op: TOpCG; size: tcgsize; regsrc, regdst: tregister64);
  4882. begin
  4883. case op of
  4884. OP_NEG:
  4885. begin
  4886. list.concat(taicpu.op_reg_const(A_MOV,regdst.reglo,0));
  4887. list.concat(taicpu.op_reg_const(A_MOV,regdst.reghi,0));
  4888. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4889. list.concat(taicpu.op_reg_reg(A_SUB,regdst.reglo,regsrc.reglo));
  4890. list.concat(taicpu.op_reg_reg(A_SBC,regdst.reghi,regsrc.reghi));
  4891. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  4892. end;
  4893. OP_NOT:
  4894. begin
  4895. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  4896. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  4897. end;
  4898. OP_AND,OP_OR,OP_XOR:
  4899. begin
  4900. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  4901. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  4902. end;
  4903. OP_ADD:
  4904. begin
  4905. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4906. list.concat(taicpu.op_reg_reg(A_ADD,regdst.reglo,regsrc.reglo));
  4907. list.concat(taicpu.op_reg_reg(A_ADC,regdst.reghi,regsrc.reghi));
  4908. end;
  4909. OP_SUB:
  4910. begin
  4911. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4912. list.concat(taicpu.op_reg_reg(A_SUB,regdst.reglo,regsrc.reglo));
  4913. list.concat(taicpu.op_reg_reg(A_SBC,regdst.reghi,regsrc.reghi));
  4914. end;
  4915. else
  4916. internalerror(2003083101);
  4917. end;
  4918. end;
  4919. procedure tthumbcg64farm.a_op64_const_reg(list: TAsmList; op: TOpCG; size: tcgsize; value: int64; reg: tregister64);
  4920. var
  4921. tmpreg : tregister;
  4922. b : byte;
  4923. begin
  4924. case op of
  4925. OP_AND,OP_OR,OP_XOR:
  4926. begin
  4927. cg.a_op_const_reg(list,op,OS_32,aint(lo(value)),reg.reglo);
  4928. cg.a_op_const_reg(list,op,OS_32,aint(hi(value)),reg.reghi);
  4929. end;
  4930. OP_ADD:
  4931. begin
  4932. if (aint(lo(value))>=0) and (aint(lo(value))<=255) then
  4933. begin
  4934. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4935. list.concat(taicpu.op_reg_const(A_ADD,reg.reglo,aint(lo(value))));
  4936. end
  4937. else
  4938. begin
  4939. tmpreg:=cg.getintregister(list,OS_32);
  4940. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  4941. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4942. list.concat(taicpu.op_reg_reg(A_ADD,reg.reglo,tmpreg));
  4943. end;
  4944. tmpreg:=cg.getintregister(list,OS_32);
  4945. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  4946. list.concat(taicpu.op_reg_reg(A_ADC,reg.reghi,tmpreg));
  4947. end;
  4948. OP_SUB:
  4949. begin
  4950. if (aint(lo(value))>=0) and (aint(lo(value))<=255) then
  4951. begin
  4952. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4953. list.concat(taicpu.op_reg_const(A_SUB,reg.reglo,aint(lo(value))))
  4954. end
  4955. else
  4956. begin
  4957. tmpreg:=cg.getintregister(list,OS_32);
  4958. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  4959. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4960. list.concat(taicpu.op_reg_reg(A_SUB,reg.reglo,tmpreg));
  4961. end;
  4962. tmpreg:=cg.getintregister(list,OS_32);
  4963. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  4964. list.concat(taicpu.op_reg_reg(A_SBC,reg.reghi,tmpreg));
  4965. end;
  4966. else
  4967. internalerror(2003083101);
  4968. end;
  4969. end;
  4970. procedure create_codegen;
  4971. begin
  4972. if GenerateThumb2Code then
  4973. begin
  4974. cg:=tthumb2cgarm.create;
  4975. cg64:=tthumb2cg64farm.create;
  4976. casmoptimizer:=TCpuThumb2AsmOptimizer;
  4977. end
  4978. else if GenerateThumbCode then
  4979. begin
  4980. cg:=tthumbcgarm.create;
  4981. cg64:=tthumbcg64farm.create;
  4982. // casmoptimizer:=TCpuThumbAsmOptimizer;
  4983. end
  4984. else
  4985. begin
  4986. cg:=tarmcgarm.create;
  4987. cg64:=tarmcg64farm.create;
  4988. casmoptimizer:=TCpuAsmOptimizer;
  4989. end;
  4990. end;
  4991. end.