cgcpu.pas 217 KB

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