cgcpu.pas 218 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457
  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(setoppostfix(taicpu.op_ref_regset(A_FSTM,ref,R_MMREGISTER,R_SUBFD,mmregs),postfix));
  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(setoppostfix(taicpu.op_ref_regset(A_FLDM,ref,R_MMREGISTER,R_SUBFD,mmregs),mmpostfix));
  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_FCPYS,A_FCVTSD,A_NONE,A_NONE,A_NONE),
  2669. (A_FCVTDS,A_FCPYD,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. procedure tbasecgarm.a_loadmm_reg_reg(list: tasmlist; fromsize,tosize: tcgsize; reg1,reg2: tregister; shuffle: pmmshuffle);
  2679. var
  2680. instr: taicpu;
  2681. begin
  2682. if shuffle=nil then
  2683. begin
  2684. if fromsize=tosize then
  2685. { needs correct size in case of spilling }
  2686. case fromsize of
  2687. OS_F32:
  2688. instr:=taicpu.op_reg_reg(A_FCPYS,reg2,reg1);
  2689. OS_F64:
  2690. instr:=taicpu.op_reg_reg(A_FCPYD,reg2,reg1);
  2691. else
  2692. internalerror(2009112405);
  2693. end
  2694. else
  2695. internalerror(2009112406);
  2696. end
  2697. else if shufflescalar(shuffle) then
  2698. instr:=taicpu.op_reg_reg(get_scalar_mm_op(tosize,fromsize),reg2,reg1)
  2699. else
  2700. internalerror(2009112407);
  2701. list.concat(instr);
  2702. case instr.opcode of
  2703. A_FCPYS,
  2704. A_FCPYD:
  2705. add_move_instruction(instr);
  2706. end;
  2707. end;
  2708. procedure tbasecgarm.a_loadmm_ref_reg(list: tasmlist; fromsize,tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2709. var
  2710. intreg,
  2711. tmpmmreg : tregister;
  2712. reg64 : tregister64;
  2713. op : tasmop;
  2714. begin
  2715. if assigned(shuffle) and
  2716. not(shufflescalar(shuffle)) then
  2717. internalerror(2009112413);
  2718. case fromsize of
  2719. OS_32,OS_S32:
  2720. begin
  2721. fromsize:=OS_F32;
  2722. { since we are loading an integer, no conversion may be required }
  2723. if (fromsize<>tosize) then
  2724. internalerror(2009112801);
  2725. end;
  2726. OS_64,OS_S64:
  2727. begin
  2728. fromsize:=OS_F64;
  2729. { since we are loading an integer, no conversion may be required }
  2730. if (fromsize<>tosize) then
  2731. internalerror(2009112901);
  2732. end;
  2733. end;
  2734. if (fromsize<>tosize) then
  2735. tmpmmreg:=getmmregister(list,fromsize)
  2736. else
  2737. tmpmmreg:=reg;
  2738. if (ref.alignment in [1,2]) then
  2739. begin
  2740. case fromsize of
  2741. OS_F32:
  2742. begin
  2743. intreg:=getintregister(list,OS_32);
  2744. a_load_ref_reg(list,OS_32,OS_32,ref,intreg);
  2745. a_loadmm_intreg_reg(list,OS_32,OS_F32,intreg,tmpmmreg,mms_movescalar);
  2746. end;
  2747. OS_F64:
  2748. begin
  2749. reg64.reglo:=getintregister(list,OS_32);
  2750. reg64.reghi:=getintregister(list,OS_32);
  2751. cg64.a_load64_ref_reg(list,ref,reg64);
  2752. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,tmpmmreg);
  2753. end;
  2754. else
  2755. internalerror(2009112412);
  2756. end;
  2757. end
  2758. else
  2759. begin
  2760. case fromsize of
  2761. OS_F32:
  2762. op:=A_FLDS;
  2763. OS_F64:
  2764. op:=A_FLDD;
  2765. else
  2766. internalerror(2009112415);
  2767. end;
  2768. handle_load_store(list,op,PF_None,tmpmmreg,ref);
  2769. end;
  2770. if (tmpmmreg<>reg) then
  2771. a_loadmm_reg_reg(list,fromsize,tosize,tmpmmreg,reg,shuffle);
  2772. end;
  2773. procedure tbasecgarm.a_loadmm_reg_ref(list: tasmlist; fromsize,tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2774. var
  2775. intreg,
  2776. tmpmmreg : tregister;
  2777. reg64 : tregister64;
  2778. op : tasmop;
  2779. begin
  2780. if assigned(shuffle) and
  2781. not(shufflescalar(shuffle)) then
  2782. internalerror(2009112416);
  2783. case tosize of
  2784. OS_32,OS_S32:
  2785. begin
  2786. tosize:=OS_F32;
  2787. { since we are loading an integer, no conversion may be required }
  2788. if (fromsize<>tosize) then
  2789. internalerror(2009112801);
  2790. end;
  2791. OS_64,OS_S64:
  2792. begin
  2793. tosize:=OS_F64;
  2794. { since we are loading an integer, no conversion may be required }
  2795. if (fromsize<>tosize) then
  2796. internalerror(2009112901);
  2797. end;
  2798. end;
  2799. if (fromsize<>tosize) then
  2800. begin
  2801. tmpmmreg:=getmmregister(list,tosize);
  2802. a_loadmm_reg_reg(list,fromsize,tosize,reg,tmpmmreg,shuffle);
  2803. end
  2804. else
  2805. tmpmmreg:=reg;
  2806. if (ref.alignment in [1,2]) then
  2807. begin
  2808. case tosize of
  2809. OS_F32:
  2810. begin
  2811. intreg:=getintregister(list,OS_32);
  2812. a_loadmm_reg_intreg(list,OS_F32,OS_32,tmpmmreg,intreg,shuffle);
  2813. a_load_reg_ref(list,OS_32,OS_32,intreg,ref);
  2814. end;
  2815. OS_F64:
  2816. begin
  2817. reg64.reglo:=getintregister(list,OS_32);
  2818. reg64.reghi:=getintregister(list,OS_32);
  2819. cg64.a_loadmm_reg_intreg64(list,OS_F64,tmpmmreg,reg64);
  2820. cg64.a_load64_reg_ref(list,reg64,ref);
  2821. end;
  2822. else
  2823. internalerror(2009112417);
  2824. end;
  2825. end
  2826. else
  2827. begin
  2828. case fromsize of
  2829. OS_F32:
  2830. op:=A_FSTS;
  2831. OS_F64:
  2832. op:=A_FSTD;
  2833. else
  2834. internalerror(2009112418);
  2835. end;
  2836. handle_load_store(list,op,PF_None,tmpmmreg,ref);
  2837. end;
  2838. end;
  2839. procedure tbasecgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  2840. begin
  2841. { this code can only be used to transfer raw data, not to perform
  2842. conversions }
  2843. if (tosize<>OS_F32) then
  2844. internalerror(2009112419);
  2845. if not(fromsize in [OS_32,OS_S32]) then
  2846. internalerror(2009112420);
  2847. if assigned(shuffle) and
  2848. not shufflescalar(shuffle) then
  2849. internalerror(2009112516);
  2850. list.concat(taicpu.op_reg_reg(A_FMSR,mmreg,intreg));
  2851. end;
  2852. procedure tbasecgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister;shuffle : pmmshuffle);
  2853. begin
  2854. { this code can only be used to transfer raw data, not to perform
  2855. conversions }
  2856. if (fromsize<>OS_F32) then
  2857. internalerror(2009112430);
  2858. if not(tosize in [OS_32,OS_S32]) then
  2859. internalerror(2009112420);
  2860. if assigned(shuffle) and
  2861. not shufflescalar(shuffle) then
  2862. internalerror(2009112514);
  2863. list.concat(taicpu.op_reg_reg(A_FMRS,intreg,mmreg));
  2864. end;
  2865. procedure tbasecgarm.a_opmm_reg_reg(list: tasmlist; op: topcg; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2866. var
  2867. tmpreg: tregister;
  2868. begin
  2869. { the vfp doesn't support xor nor any other logical operation, but
  2870. this routine is used to initialise global mm regvars. We can
  2871. easily initialise an mm reg with 0 though. }
  2872. case op of
  2873. OP_XOR:
  2874. begin
  2875. if (src<>dst) or
  2876. (reg_cgsize(src)<>size) or
  2877. assigned(shuffle) then
  2878. internalerror(2009112907);
  2879. tmpreg:=getintregister(list,OS_32);
  2880. a_load_const_reg(list,OS_32,0,tmpreg);
  2881. case size of
  2882. OS_F32:
  2883. list.concat(taicpu.op_reg_reg(A_FMSR,dst,tmpreg));
  2884. OS_F64:
  2885. list.concat(taicpu.op_reg_reg_reg(A_FMDRR,dst,tmpreg,tmpreg));
  2886. else
  2887. internalerror(2009112908);
  2888. end;
  2889. end
  2890. else
  2891. internalerror(2009112906);
  2892. end;
  2893. end;
  2894. procedure tbasecgarm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  2895. procedure loadvmttor12;
  2896. var
  2897. tmpref,
  2898. href : treference;
  2899. extrareg : boolean;
  2900. l : TAsmLabel;
  2901. begin
  2902. reference_reset_base(href,NR_R0,0,sizeof(pint));
  2903. if GenerateThumbCode then
  2904. begin
  2905. if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
  2906. begin
  2907. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2908. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2909. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2910. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2911. end
  2912. else
  2913. begin
  2914. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2915. { create consts entry }
  2916. reference_reset(tmpref,4);
  2917. current_asmdata.getjumplabel(l);
  2918. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  2919. cg.a_label(current_procinfo.aktlocaldata,l);
  2920. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2921. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
  2922. tmpref.symbol:=l;
  2923. tmpref.base:=NR_PC;
  2924. list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
  2925. href.offset:=0;
  2926. href.index:=NR_R1;
  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,RS_R1]));
  2930. end;
  2931. end
  2932. else
  2933. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2934. end;
  2935. procedure op_onr12methodaddr;
  2936. var
  2937. tmpref,
  2938. href : treference;
  2939. l : TAsmLabel;
  2940. begin
  2941. if (procdef.extnumber=$ffff) then
  2942. Internalerror(200006139);
  2943. if GenerateThumbCode then
  2944. begin
  2945. reference_reset_base(href,NR_R0,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  2946. if (href.offset in [0..124]) and ((href.offset mod 4)=0) then
  2947. begin
  2948. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2949. list.concat(taicpu.op_reg_reg(A_MOV,NR_R0,NR_R12));
  2950. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2951. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2952. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  2953. end
  2954. else
  2955. begin
  2956. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2957. { create consts entry }
  2958. reference_reset(tmpref,4);
  2959. current_asmdata.getjumplabel(l);
  2960. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  2961. cg.a_label(current_procinfo.aktlocaldata,l);
  2962. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  2963. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(href.offset));
  2964. tmpref.symbol:=l;
  2965. tmpref.base:=NR_PC;
  2966. list.concat(taicpu.op_reg_ref(A_LDR,NR_R1,tmpref));
  2967. list.concat(taicpu.op_reg_reg(A_MOV,NR_R0,NR_R12));
  2968. href.offset:=0;
  2969. href.base:=NR_R0;
  2970. href.index:=NR_R1;
  2971. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R0);
  2972. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  2973. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0,RS_R1]));
  2974. end;
  2975. end
  2976. else
  2977. begin
  2978. reference_reset_base(href,NR_R12,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  2979. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2980. end;
  2981. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  2982. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12))
  2983. else
  2984. list.concat(taicpu.op_reg(A_BX,NR_R12));
  2985. end;
  2986. var
  2987. make_global : boolean;
  2988. tmpref : treference;
  2989. l : TAsmLabel;
  2990. begin
  2991. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  2992. Internalerror(200006137);
  2993. if not assigned(procdef.struct) or
  2994. (procdef.procoptions*[po_classmethod, po_staticmethod,
  2995. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  2996. Internalerror(200006138);
  2997. if procdef.owner.symtabletype<>ObjectSymtable then
  2998. Internalerror(200109191);
  2999. if GenerateThumbCode or GenerateThumb2Code then
  3000. list.concat(tai_thumb_func.create);
  3001. make_global:=false;
  3002. if (not current_module.is_unit) or
  3003. create_smartlink or
  3004. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  3005. make_global:=true;
  3006. if make_global then
  3007. list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  3008. else
  3009. list.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  3010. { the wrapper might need aktlocaldata for the additional data to
  3011. load the constant }
  3012. current_procinfo:=cprocinfo.create(nil);
  3013. { set param1 interface to self }
  3014. g_adjust_self_value(list,procdef,ioffset);
  3015. { case 4 }
  3016. if (po_virtualmethod in procdef.procoptions) and
  3017. not is_objectpascal_helper(procdef.struct) then
  3018. begin
  3019. loadvmttor12;
  3020. op_onr12methodaddr;
  3021. end
  3022. { case 0 }
  3023. else if GenerateThumbCode then
  3024. begin
  3025. { bl cannot be used here because it destroys lr }
  3026. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3027. { create consts entry }
  3028. reference_reset(tmpref,4);
  3029. current_asmdata.getjumplabel(l);
  3030. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3031. cg.a_label(current_procinfo.aktlocaldata,l);
  3032. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3033. current_procinfo.aktlocaldata.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(procdef.mangledname)));
  3034. tmpref.symbol:=l;
  3035. tmpref.base:=NR_PC;
  3036. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,NR_R0);
  3037. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  3038. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3039. list.concat(taicpu.op_reg(A_BX,NR_R12));
  3040. end
  3041. else
  3042. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  3043. list.concatlist(current_procinfo.aktlocaldata);
  3044. current_procinfo.Free;
  3045. current_procinfo:=nil;
  3046. list.concat(Tai_symbol_end.Createname(labelname));
  3047. end;
  3048. procedure tbasecgarm.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  3049. const
  3050. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NEG];
  3051. begin
  3052. if (op in overflowops) and
  3053. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  3054. a_load_reg_reg(list,OS_32,size,dst,dst);
  3055. end;
  3056. procedure tbasecgarm.safe_mla(list : TAsmList; op1,op2,op3,op4 : TRegister);
  3057. procedure checkreg(var reg : TRegister);
  3058. var
  3059. tmpreg : TRegister;
  3060. begin
  3061. if ((GenerateThumbCode or GenerateThumb2Code) and (getsupreg(reg)=RS_R13)) or
  3062. (getsupreg(reg)=RS_R15) then
  3063. begin
  3064. tmpreg:=getintregister(list,OS_INT);
  3065. a_load_reg_reg(list,OS_INT,OS_INT,reg,tmpreg);
  3066. reg:=tmpreg;
  3067. end;
  3068. end;
  3069. begin
  3070. checkreg(op1);
  3071. checkreg(op2);
  3072. checkreg(op3);
  3073. checkreg(op4);
  3074. list.concat(taicpu.op_reg_reg_reg_reg(A_MLA,op1,op2,op3,op4));
  3075. end;
  3076. procedure tcg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  3077. begin
  3078. case op of
  3079. OP_NEG:
  3080. begin
  3081. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3082. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  3083. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  3084. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3085. end;
  3086. OP_NOT:
  3087. begin
  3088. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  3089. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  3090. end;
  3091. else
  3092. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  3093. end;
  3094. end;
  3095. procedure tcg64farm.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  3096. begin
  3097. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  3098. end;
  3099. procedure tcg64farm.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  3100. var
  3101. ovloc : tlocation;
  3102. begin
  3103. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,ovloc);
  3104. end;
  3105. procedure tcg64farm.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  3106. var
  3107. ovloc : tlocation;
  3108. begin
  3109. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,ovloc);
  3110. end;
  3111. procedure tcg64farm.a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister);
  3112. begin
  3113. { this code can only be used to transfer raw data, not to perform
  3114. conversions }
  3115. if (mmsize<>OS_F64) then
  3116. internalerror(2009112405);
  3117. list.concat(taicpu.op_reg_reg_reg(A_FMDRR,mmreg,intreg.reglo,intreg.reghi));
  3118. end;
  3119. procedure tcg64farm.a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64);
  3120. begin
  3121. { this code can only be used to transfer raw data, not to perform
  3122. conversions }
  3123. if (mmsize<>OS_F64) then
  3124. internalerror(2009112406);
  3125. list.concat(taicpu.op_reg_reg_reg(A_FMRRD,intreg.reglo,intreg.reghi,mmreg));
  3126. end;
  3127. procedure tcg64farm.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3128. var
  3129. tmpreg : tregister;
  3130. b : byte;
  3131. begin
  3132. ovloc.loc:=LOC_VOID;
  3133. case op of
  3134. OP_NEG,
  3135. OP_NOT :
  3136. internalerror(2012022501);
  3137. end;
  3138. if (setflags or tbasecgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  3139. begin
  3140. case op of
  3141. OP_ADD:
  3142. begin
  3143. if is_shifter_const(lo(value),b) then
  3144. begin
  3145. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3146. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  3147. end
  3148. else
  3149. begin
  3150. tmpreg:=cg.getintregister(list,OS_32);
  3151. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  3152. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3153. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3154. end;
  3155. if is_shifter_const(hi(value),b) then
  3156. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  3157. else
  3158. begin
  3159. tmpreg:=cg.getintregister(list,OS_32);
  3160. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3161. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  3162. end;
  3163. end;
  3164. OP_SUB:
  3165. begin
  3166. if is_shifter_const(lo(value),b) then
  3167. begin
  3168. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3169. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  3170. end
  3171. else
  3172. begin
  3173. tmpreg:=cg.getintregister(list,OS_32);
  3174. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  3175. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3176. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3177. end;
  3178. if is_shifter_const(hi(value),b) then
  3179. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))),PF_S))
  3180. else
  3181. begin
  3182. tmpreg:=cg.getintregister(list,OS_32);
  3183. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3184. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  3185. end;
  3186. end;
  3187. else
  3188. internalerror(200502131);
  3189. end;
  3190. if size=OS_64 then
  3191. begin
  3192. { the arm has an weired opinion how flags for SUB/ADD are handled }
  3193. ovloc.loc:=LOC_FLAGS;
  3194. case op of
  3195. OP_ADD:
  3196. ovloc.resflags:=F_CS;
  3197. OP_SUB:
  3198. ovloc.resflags:=F_CC;
  3199. end;
  3200. end;
  3201. end
  3202. else
  3203. begin
  3204. case op of
  3205. OP_AND,OP_OR,OP_XOR:
  3206. begin
  3207. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  3208. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  3209. end;
  3210. OP_ADD:
  3211. begin
  3212. if is_shifter_const(aint(lo(value)),b) then
  3213. begin
  3214. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3215. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  3216. end
  3217. else
  3218. begin
  3219. tmpreg:=cg.getintregister(list,OS_32);
  3220. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  3221. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3222. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3223. end;
  3224. if is_shifter_const(aint(hi(value)),b) then
  3225. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  3226. else
  3227. begin
  3228. tmpreg:=cg.getintregister(list,OS_32);
  3229. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  3230. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  3231. end;
  3232. end;
  3233. OP_SUB:
  3234. begin
  3235. if is_shifter_const(aint(lo(value)),b) then
  3236. begin
  3237. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3238. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  3239. end
  3240. else
  3241. begin
  3242. tmpreg:=cg.getintregister(list,OS_32);
  3243. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  3244. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3245. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  3246. end;
  3247. if is_shifter_const(aint(hi(value)),b) then
  3248. list.concat(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  3249. else
  3250. begin
  3251. tmpreg:=cg.getintregister(list,OS_32);
  3252. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  3253. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  3254. end;
  3255. end;
  3256. else
  3257. internalerror(2003083101);
  3258. end;
  3259. end;
  3260. end;
  3261. procedure tcg64farm.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  3262. begin
  3263. ovloc.loc:=LOC_VOID;
  3264. case op of
  3265. OP_NEG,
  3266. OP_NOT :
  3267. internalerror(2012022502);
  3268. end;
  3269. if (setflags or tbasecgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  3270. begin
  3271. case op of
  3272. OP_ADD:
  3273. begin
  3274. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3275. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  3276. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi),PF_S));
  3277. end;
  3278. OP_SUB:
  3279. begin
  3280. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3281. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  3282. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi),PF_S));
  3283. end;
  3284. else
  3285. internalerror(2003083101);
  3286. end;
  3287. if size=OS_64 then
  3288. begin
  3289. { the arm has an weired opinion how flags for SUB/ADD are handled }
  3290. ovloc.loc:=LOC_FLAGS;
  3291. case op of
  3292. OP_ADD:
  3293. ovloc.resflags:=F_CS;
  3294. OP_SUB:
  3295. ovloc.resflags:=F_CC;
  3296. end;
  3297. end;
  3298. end
  3299. else
  3300. begin
  3301. case op of
  3302. OP_AND,OP_OR,OP_XOR:
  3303. begin
  3304. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  3305. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  3306. end;
  3307. OP_ADD:
  3308. begin
  3309. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3310. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  3311. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  3312. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3313. end;
  3314. OP_SUB:
  3315. begin
  3316. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3317. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  3318. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  3319. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3320. end;
  3321. else
  3322. internalerror(2003083101);
  3323. end;
  3324. end;
  3325. end;
  3326. procedure tthumbcgarm.init_register_allocators;
  3327. begin
  3328. inherited init_register_allocators;
  3329. if assigned(current_procinfo) and (current_procinfo.framepointer=NR_R7) then
  3330. rg[R_INTREGISTER]:=trgintcputhumb.create(R_INTREGISTER,R_SUBWHOLE,
  3331. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6],first_int_imreg,[])
  3332. else
  3333. rg[R_INTREGISTER]:=trgintcputhumb.create(R_INTREGISTER,R_SUBWHOLE,
  3334. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7],first_int_imreg,[]);
  3335. end;
  3336. procedure tthumbcgarm.done_register_allocators;
  3337. begin
  3338. rg[R_INTREGISTER].free;
  3339. rg[R_FPUREGISTER].free;
  3340. rg[R_MMREGISTER].free;
  3341. inherited done_register_allocators;
  3342. end;
  3343. procedure tthumbcgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  3344. var
  3345. ref : treference;
  3346. shift : byte;
  3347. r : byte;
  3348. regs, saveregs : tcpuregisterset;
  3349. r7offset,
  3350. stackmisalignment : pint;
  3351. postfix: toppostfix;
  3352. registerarea,
  3353. imm1, imm2: DWord;
  3354. stack_parameters: Boolean;
  3355. begin
  3356. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  3357. LocalSize:=align(LocalSize,4);
  3358. { call instruction does not put anything on the stack }
  3359. stackmisalignment:=0;
  3360. if not(nostackframe) then
  3361. begin
  3362. a_reg_alloc(list,NR_STACK_POINTER_REG);
  3363. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3364. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  3365. { save int registers }
  3366. reference_reset(ref,4);
  3367. ref.index:=NR_STACK_POINTER_REG;
  3368. ref.addressmode:=AM_PREINDEXED;
  3369. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3370. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3371. begin
  3372. //!!!! a_reg_alloc(list,NR_R12);
  3373. //!!!! list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  3374. end;
  3375. { the (old) ARM APCS requires saving both the stack pointer (to
  3376. crawl the stack) and the PC (to identify the function this
  3377. stack frame belongs to) -> also save R12 (= copy of R13 on entry)
  3378. and R15 -- still needs updating for EABI and Darwin, they don't
  3379. need that }
  3380. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3381. regs:=regs+[RS_R7,RS_R14]
  3382. else
  3383. // if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  3384. include(regs,RS_R14);
  3385. { safely estimate stack size }
  3386. if localsize+current_settings.alignment.localalignmax+4>508 then
  3387. begin
  3388. include(rg[R_INTREGISTER].used_in_proc,RS_R4);
  3389. include(regs,RS_R4);
  3390. end;
  3391. registerarea:=0;
  3392. if regs<>[] then
  3393. begin
  3394. for r:=RS_R0 to RS_R15 do
  3395. if r in regs then
  3396. inc(registerarea,4);
  3397. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,regs));
  3398. end;
  3399. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  3400. if stack_parameters or (LocalSize<>0) or
  3401. ((stackmisalignment<>0) and
  3402. ((pi_do_call in current_procinfo.flags) or
  3403. (po_assembler in current_procinfo.procdef.procoptions))) then
  3404. begin
  3405. { do we access stack parameters?
  3406. if yes, the previously estimated stacksize must be used }
  3407. if stack_parameters then
  3408. begin
  3409. if localsize>tarmprocinfo(current_procinfo).stackframesize then
  3410. begin
  3411. writeln(localsize);
  3412. writeln(tarmprocinfo(current_procinfo).stackframesize);
  3413. internalerror(2013040601);
  3414. end
  3415. else
  3416. localsize:=tarmprocinfo(current_procinfo).stackframesize-registerarea;
  3417. end
  3418. else
  3419. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3420. if localsize<508 then
  3421. begin
  3422. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  3423. end
  3424. else if localsize<=1016 then
  3425. begin
  3426. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,508));
  3427. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize-508));
  3428. end
  3429. else
  3430. begin
  3431. a_load_const_reg(list,OS_ADDR,-localsize,NR_R4);
  3432. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R4));
  3433. include(regs,RS_R4);
  3434. //!!!! if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3435. //!!!! a_reg_alloc(list,NR_R12);
  3436. //!!!! a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  3437. //!!!! list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  3438. //!!!! a_reg_dealloc(list,NR_R12);
  3439. end;
  3440. end;
  3441. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3442. begin
  3443. list.concat(taicpu.op_reg_reg_const(A_ADD,current_procinfo.framepointer,NR_STACK_POINTER_REG,0));
  3444. end;
  3445. end;
  3446. end;
  3447. procedure tthumbcgarm.g_proc_exit(list: TAsmList; parasize: longint; nostackframe: boolean);
  3448. var
  3449. ref : treference;
  3450. LocalSize : longint;
  3451. r,
  3452. shift : byte;
  3453. saveregs,
  3454. regs : tcpuregisterset;
  3455. registerarea : DWord;
  3456. stackmisalignment: pint;
  3457. imm1, imm2: DWord;
  3458. stack_parameters : Boolean;
  3459. begin
  3460. if not(nostackframe) then
  3461. begin
  3462. stack_parameters:=current_procinfo.procdef.stack_tainting_parameter(calleeside);
  3463. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3464. include(regs,RS_R15);
  3465. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3466. include(regs,getsupreg(current_procinfo.framepointer));
  3467. registerarea:=0;
  3468. for r:=RS_R0 to RS_R15 do
  3469. if r in regs then
  3470. inc(registerarea,4);
  3471. stackmisalignment:=registerarea mod current_settings.alignment.localalignmax;
  3472. LocalSize:=current_procinfo.calc_stackframe_size;
  3473. if stack_parameters then
  3474. localsize:=tarmprocinfo(current_procinfo).stackframesize-registerarea
  3475. else
  3476. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3477. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  3478. (target_info.system in systems_darwin) then
  3479. begin
  3480. if (LocalSize<>0) or
  3481. ((stackmisalignment<>0) and
  3482. ((pi_do_call in current_procinfo.flags) or
  3483. (po_assembler in current_procinfo.procdef.procoptions))) then
  3484. begin
  3485. if LocalSize=0 then
  3486. else if LocalSize<=508 then
  3487. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize))
  3488. else if LocalSize<=1016 then
  3489. begin
  3490. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,508));
  3491. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,localsize-508));
  3492. end
  3493. else
  3494. begin
  3495. a_reg_alloc(list,NR_R3);
  3496. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R3);
  3497. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R3));
  3498. a_reg_dealloc(list,NR_R3);
  3499. end;
  3500. end;
  3501. if regs=[] then
  3502. begin
  3503. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  3504. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  3505. else
  3506. list.concat(taicpu.op_reg(A_BX,NR_R14))
  3507. end
  3508. else
  3509. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,regs));
  3510. end;
  3511. end
  3512. else if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  3513. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  3514. else
  3515. list.concat(taicpu.op_reg(A_BX,NR_R14))
  3516. end;
  3517. procedure tthumbcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  3518. var
  3519. oppostfix:toppostfix;
  3520. usedtmpref: treference;
  3521. tmpreg,tmpreg2 : tregister;
  3522. dir : integer;
  3523. begin
  3524. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  3525. FromSize := ToSize;
  3526. case FromSize of
  3527. { signed integer registers }
  3528. OS_8:
  3529. oppostfix:=PF_B;
  3530. OS_S8:
  3531. oppostfix:=PF_SB;
  3532. OS_16:
  3533. oppostfix:=PF_H;
  3534. OS_S16:
  3535. oppostfix:=PF_SH;
  3536. OS_32,
  3537. OS_S32:
  3538. oppostfix:=PF_None;
  3539. else
  3540. InternalError(200308298);
  3541. end;
  3542. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  3543. begin
  3544. if target_info.endian=endian_big then
  3545. dir:=-1
  3546. else
  3547. dir:=1;
  3548. case FromSize of
  3549. OS_16,OS_S16:
  3550. begin
  3551. { only complicated references need an extra loadaddr }
  3552. if assigned(ref.symbol) or
  3553. (ref.index<>NR_NO) or
  3554. (ref.offset<-124) or
  3555. (ref.offset>124) or
  3556. { sometimes the compiler reused registers }
  3557. (reg=ref.index) or
  3558. (reg=ref.base) then
  3559. begin
  3560. tmpreg2:=getintregister(list,OS_INT);
  3561. a_loadaddr_ref_reg(list,ref,tmpreg2);
  3562. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  3563. end
  3564. else
  3565. usedtmpref:=ref;
  3566. if target_info.endian=endian_big then
  3567. inc(usedtmpref.offset,1);
  3568. tmpreg:=getintregister(list,OS_INT);
  3569. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  3570. inc(usedtmpref.offset,dir);
  3571. if FromSize=OS_16 then
  3572. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  3573. else
  3574. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  3575. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,8));
  3576. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3577. end;
  3578. OS_32,OS_S32:
  3579. begin
  3580. tmpreg:=getintregister(list,OS_INT);
  3581. { only complicated references need an extra loadaddr }
  3582. if assigned(ref.symbol) or
  3583. (ref.index<>NR_NO) or
  3584. (ref.offset<-124) or
  3585. (ref.offset>124) or
  3586. { sometimes the compiler reused registers }
  3587. (reg=ref.index) or
  3588. (reg=ref.base) then
  3589. begin
  3590. tmpreg2:=getintregister(list,OS_INT);
  3591. a_loadaddr_ref_reg(list,ref,tmpreg2);
  3592. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  3593. end
  3594. else
  3595. usedtmpref:=ref;
  3596. if ref.alignment=2 then
  3597. begin
  3598. if target_info.endian=endian_big then
  3599. inc(usedtmpref.offset,2);
  3600. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  3601. inc(usedtmpref.offset,dir*2);
  3602. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  3603. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,16));
  3604. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3605. end
  3606. else
  3607. begin
  3608. if target_info.endian=endian_big then
  3609. inc(usedtmpref.offset,3);
  3610. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  3611. inc(usedtmpref.offset,dir);
  3612. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3613. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,8));
  3614. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3615. inc(usedtmpref.offset,dir);
  3616. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3617. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,16));
  3618. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3619. inc(usedtmpref.offset,dir);
  3620. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3621. list.concat(taicpu.op_reg_const(A_LSL,tmpreg,24));
  3622. list.concat(taicpu.op_reg_reg(A_ORR,reg,tmpreg));
  3623. end;
  3624. end
  3625. else
  3626. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3627. end;
  3628. end
  3629. else
  3630. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3631. if (fromsize=OS_S8) and (tosize = OS_16) then
  3632. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  3633. end;
  3634. procedure tthumbcgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  3635. var
  3636. imm_shift : byte;
  3637. l : tasmlabel;
  3638. hr : treference;
  3639. begin
  3640. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  3641. internalerror(2002090902);
  3642. if is_thumb_imm(a) then
  3643. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  3644. else
  3645. begin
  3646. reference_reset(hr,4);
  3647. current_asmdata.getjumplabel(l);
  3648. cg.a_label(current_procinfo.aktlocaldata,l);
  3649. hr.symboldata:=current_procinfo.aktlocaldata.last;
  3650. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  3651. hr.symbol:=l;
  3652. hr.base:=NR_PC;
  3653. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  3654. end;
  3655. end;
  3656. procedure tthumbcgarm.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  3657. var
  3658. hsym : tsym;
  3659. href,
  3660. tmpref : treference;
  3661. paraloc : Pcgparalocation;
  3662. l : TAsmLabel;
  3663. begin
  3664. { calculate the parameter info for the procdef }
  3665. procdef.init_paraloc_info(callerside);
  3666. hsym:=tsym(procdef.parast.Find('self'));
  3667. if not(assigned(hsym) and
  3668. (hsym.typ=paravarsym)) then
  3669. internalerror(200305251);
  3670. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  3671. while paraloc<>nil do
  3672. with paraloc^ do
  3673. begin
  3674. case loc of
  3675. LOC_REGISTER:
  3676. begin
  3677. if is_thumb_imm(ioffset) then
  3678. a_op_const_reg(list,OP_SUB,size,ioffset,register)
  3679. else
  3680. begin
  3681. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3682. reference_reset(tmpref,4);
  3683. current_asmdata.getjumplabel(l);
  3684. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3685. cg.a_label(current_procinfo.aktlocaldata,l);
  3686. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3687. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ioffset));
  3688. tmpref.symbol:=l;
  3689. tmpref.base:=NR_PC;
  3690. list.concat(taicpu.op_reg_ref(A_LDR,NR_R4,tmpref));
  3691. a_op_reg_reg(list,OP_SUB,size,NR_R4,register);
  3692. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3693. end;
  3694. end;
  3695. LOC_REFERENCE:
  3696. begin
  3697. { offset in the wrapper needs to be adjusted for the stored
  3698. return address }
  3699. reference_reset_base(href,reference.index,reference.offset+sizeof(aint),sizeof(pint));
  3700. if is_thumb_imm(ioffset) then
  3701. a_op_const_ref(list,OP_SUB,size,ioffset,href)
  3702. else
  3703. begin
  3704. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3705. reference_reset(tmpref,4);
  3706. current_asmdata.getjumplabel(l);
  3707. current_procinfo.aktlocaldata.Concat(tai_align.Create(4));
  3708. cg.a_label(current_procinfo.aktlocaldata,l);
  3709. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3710. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ioffset));
  3711. tmpref.symbol:=l;
  3712. tmpref.base:=NR_PC;
  3713. list.concat(taicpu.op_reg_ref(A_LDR,NR_R4,tmpref));
  3714. a_op_reg_ref(list,OP_SUB,size,NR_R4,href);
  3715. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R4]));
  3716. end;
  3717. end
  3718. else
  3719. internalerror(200309189);
  3720. end;
  3721. paraloc:=next;
  3722. end;
  3723. end;
  3724. function tthumbcgarm.handle_load_store(list: TAsmList; op: tasmop; oppostfix: toppostfix; reg: tregister; ref: treference): treference;
  3725. var
  3726. href : treference;
  3727. tmpreg : TRegister;
  3728. begin
  3729. href:=ref;
  3730. if { LDR/STR limitations }
  3731. (
  3732. (((op=A_LDR) and (oppostfix=PF_None)) or
  3733. ((op=A_STR) and (oppostfix=PF_None))) and
  3734. (ref.base<>NR_STACK_POINTER_REG) and
  3735. (abs(ref.offset)>124)
  3736. ) or
  3737. { LDRB/STRB limitations }
  3738. (
  3739. (((op=A_LDR) and (oppostfix=PF_B)) or
  3740. ((op=A_LDRB) and (oppostfix=PF_None)) or
  3741. ((op=A_STR) and (oppostfix=PF_B)) or
  3742. ((op=A_STRB) and (oppostfix=PF_None))) and
  3743. ((ref.base=NR_STACK_POINTER_REG) or
  3744. (ref.index=NR_STACK_POINTER_REG) or
  3745. (abs(ref.offset)>31)
  3746. )
  3747. ) or
  3748. { LDRH/STRH limitations }
  3749. (
  3750. (((op=A_LDR) and (oppostfix=PF_H)) or
  3751. ((op=A_LDRH) and (oppostfix=PF_None)) or
  3752. ((op=A_STR) and (oppostfix=PF_H)) or
  3753. ((op=A_STRH) and (oppostfix=PF_None))) and
  3754. ((ref.base=NR_STACK_POINTER_REG) or
  3755. (ref.index=NR_STACK_POINTER_REG) or
  3756. (abs(ref.offset)>62) or
  3757. ((abs(ref.offset) mod 2)<>0)
  3758. )
  3759. ) then
  3760. begin
  3761. tmpreg:=getintregister(list,OS_ADDR);
  3762. a_loadaddr_ref_reg(list,ref,tmpreg);
  3763. reference_reset_base(href,tmpreg,0,ref.alignment);
  3764. end
  3765. else if (op=A_LDR) and
  3766. (oppostfix in [PF_None]) and
  3767. (ref.base=NR_STACK_POINTER_REG) and
  3768. (abs(ref.offset)>1020) then
  3769. begin
  3770. tmpreg:=getintregister(list,OS_ADDR);
  3771. a_loadaddr_ref_reg(list,ref,tmpreg);
  3772. reference_reset_base(href,tmpreg,0,ref.alignment);
  3773. end
  3774. else if (op=A_LDR) and
  3775. ((oppostfix in [PF_SH,PF_SB]) or
  3776. (abs(ref.offset)>124)) then
  3777. begin
  3778. tmpreg:=getintregister(list,OS_ADDR);
  3779. a_loadaddr_ref_reg(list,ref,tmpreg);
  3780. reference_reset_base(href,tmpreg,0,ref.alignment);
  3781. end;
  3782. Result:=inherited handle_load_store(list, op, oppostfix, reg, href);
  3783. end;
  3784. procedure tthumbcgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  3785. var
  3786. tmpreg,overflowreg : tregister;
  3787. asmop : tasmop;
  3788. begin
  3789. case op of
  3790. OP_NEG:
  3791. list.concat(taicpu.op_reg_reg(A_NEG,dst,src));
  3792. OP_NOT:
  3793. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  3794. OP_DIV,OP_IDIV:
  3795. internalerror(200308284);
  3796. OP_ROL:
  3797. begin
  3798. if not(size in [OS_32,OS_S32]) then
  3799. internalerror(2008072801);
  3800. { simulate ROL by ror'ing 32-value }
  3801. tmpreg:=getintregister(list,OS_32);
  3802. a_load_const_reg(list,OS_32,32,tmpreg);
  3803. list.concat(taicpu.op_reg_reg(A_SUB,tmpreg,src));
  3804. list.concat(taicpu.op_reg_reg(A_ROR,dst,src));
  3805. end;
  3806. else
  3807. begin
  3808. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3809. list.concat(setoppostfix(
  3810. taicpu.op_reg_reg(op_reg_opcg2asmop[op],dst,src),op_reg_postfix[op]));
  3811. end;
  3812. end;
  3813. maybeadjustresult(list,op,size,dst);
  3814. end;
  3815. procedure tthumbcgarm.a_op_const_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; dst: tregister);
  3816. var
  3817. tmpreg : tregister;
  3818. so : tshifterop;
  3819. l1 : longint;
  3820. imm1, imm2: DWord;
  3821. begin
  3822. //!!! ovloc.loc:=LOC_VOID;
  3823. if {$ifopt R+}(a<>-2147483648) and{$endif} {!!!!!! not setflags and } is_thumb_imm(-a) then
  3824. case op of
  3825. OP_ADD:
  3826. begin
  3827. op:=OP_SUB;
  3828. a:=aint(dword(-a));
  3829. end;
  3830. OP_SUB:
  3831. begin
  3832. op:=OP_ADD;
  3833. a:=aint(dword(-a));
  3834. end
  3835. end;
  3836. if is_thumb_imm(a) and (op in [OP_ADD,OP_SUB]) then
  3837. begin
  3838. // if cgsetflags or setflags then
  3839. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3840. list.concat(setoppostfix(
  3841. taicpu.op_reg_const(op_reg_opcg2asmop[op],dst,a),op_reg_postfix[op]));
  3842. if (cgsetflags {!!! or setflags }) and (size in [OS_8,OS_16,OS_32]) then
  3843. begin
  3844. //!!! ovloc.loc:=LOC_FLAGS;
  3845. case op of
  3846. OP_ADD:
  3847. //!!! ovloc.resflags:=F_CS;
  3848. ;
  3849. OP_SUB:
  3850. //!!! ovloc.resflags:=F_CC;
  3851. ;
  3852. end;
  3853. end;
  3854. end
  3855. else
  3856. begin
  3857. { there could be added some more sophisticated optimizations }
  3858. if (op in [OP_MUL,OP_IMUL,OP_DIV,OP_IDIV]) and (a=1) then
  3859. a_load_reg_reg(list,size,size,dst,dst)
  3860. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  3861. a_load_const_reg(list,size,0,dst)
  3862. else if (op in [OP_IMUL,OP_IDIV]) and (a=-1) then
  3863. a_op_reg_reg(list,OP_NEG,size,dst,dst)
  3864. { we do this here instead in the peephole optimizer because
  3865. it saves us a register }
  3866. {$ifdef DUMMY}
  3867. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  3868. a_op_const_reg_reg(list,OP_SHL,size,l1,dst,dst)
  3869. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  3870. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  3871. begin
  3872. if l1>32 then{roozbeh does this ever happen?}
  3873. internalerror(200308296);
  3874. shifterop_reset(so);
  3875. so.shiftmode:=SM_LSL;
  3876. so.shiftimm:=l1;
  3877. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,dst,dst,so));
  3878. end
  3879. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  3880. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  3881. begin
  3882. if l1>32 then{does this ever happen?}
  3883. internalerror(201205181);
  3884. shifterop_reset(so);
  3885. so.shiftmode:=SM_LSL;
  3886. so.shiftimm:=l1;
  3887. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,dst,dst,so));
  3888. end
  3889. 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
  3890. begin
  3891. { nothing to do on success }
  3892. end
  3893. {$endif DUMMY}
  3894. { x := y and 0; just clears a register, this sometimes gets generated on 64bit ops.
  3895. Just using mov x, #0 might allow some easier optimizations down the line. }
  3896. else if (op = OP_AND) and (dword(a)=0) then
  3897. list.concat(taicpu.op_reg_const(A_MOV,dst,0))
  3898. { x := y AND $FFFFFFFF just copies the register, so use mov for better optimizations }
  3899. else if (op = OP_AND) and (not(dword(a))=0) then
  3900. // do nothing
  3901. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  3902. broader range of shifterconstants.}
  3903. {$ifdef DUMMY}
  3904. else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  3905. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,not(dword(a))))
  3906. else if (op = OP_AND) and split_into_shifter_const(not(dword(a)), imm1, imm2) then
  3907. begin
  3908. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm1));
  3909. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm2));
  3910. end
  3911. else if (op in [OP_ADD, OP_SUB, OP_OR]) and
  3912. not(cgsetflags or setflags) and
  3913. split_into_shifter_const(a, imm1, imm2) then
  3914. begin
  3915. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm1));
  3916. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm2));
  3917. end
  3918. {$endif DUMMY}
  3919. else if (op in [OP_SHL, OP_SHR, OP_SAR]) then
  3920. begin
  3921. list.concat(taicpu.op_reg_reg_const(op_reg_opcg2asmop[op],dst,dst,a));
  3922. end
  3923. else
  3924. begin
  3925. tmpreg:=getintregister(list,size);
  3926. a_load_const_reg(list,size,a,tmpreg);
  3927. a_op_reg_reg(list,op,size,tmpreg,dst);
  3928. end;
  3929. end;
  3930. maybeadjustresult(list,op,size,dst);
  3931. end;
  3932. procedure tthumbcgarm.a_op_const_reg_reg(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister);
  3933. begin
  3934. if (op=OP_ADD) and (src=NR_R13) and (dst<>NR_R13) and ((a mod 4)=0) and (a>0) and (a<=1020) then
  3935. list.concat(taicpu.op_reg_reg_const(A_ADD,dst,src,a))
  3936. else
  3937. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  3938. end;
  3939. procedure tthumbcgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  3940. var
  3941. l1,l2 : tasmlabel;
  3942. ai : taicpu;
  3943. begin
  3944. current_asmdata.getjumplabel(l1);
  3945. current_asmdata.getjumplabel(l2);
  3946. ai:=setcondition(taicpu.op_sym(A_B,l1),flags_to_cond(f));
  3947. ai.is_jmp:=true;
  3948. list.concat(ai);
  3949. list.concat(taicpu.op_reg_const(A_MOV,reg,0));
  3950. list.concat(taicpu.op_sym(A_B,l2));
  3951. cg.a_label(list,l1);
  3952. list.concat(taicpu.op_reg_const(A_MOV,reg,1));
  3953. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3954. cg.a_label(list,l2);
  3955. end;
  3956. procedure tthumbcgarm.g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string);
  3957. var
  3958. tmpref : treference;
  3959. l : tasmlabel;
  3960. begin
  3961. { there is no branch instruction on thumb which allows big distances and which leaves LR as it is
  3962. and which allows to switch the instruction set }
  3963. { create const entry }
  3964. reference_reset(tmpref,4);
  3965. current_asmdata.getjumplabel(l);
  3966. tmpref.symbol:=l;
  3967. tmpref.base:=NR_PC;
  3968. list.concat(taicpu.op_regset(A_PUSH,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3969. list.concat(taicpu.op_reg_ref(A_LDR,NR_R0,tmpref));
  3970. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_R0));
  3971. list.concat(taicpu.op_regset(A_POP,R_INTREGISTER,R_SUBWHOLE,[RS_R0]));
  3972. list.concat(taicpu.op_reg(A_BX,NR_R12));
  3973. { append const entry }
  3974. list.Concat(tai_align.Create(4));
  3975. list.Concat(tai_label.create(l));
  3976. list.concat(tai_const.Create_sym(current_asmdata.RefAsmSymbol(externalname)));
  3977. end;
  3978. procedure tthumb2cgarm.init_register_allocators;
  3979. begin
  3980. inherited init_register_allocators;
  3981. { currently, we save R14 always, so we can use it }
  3982. if (target_info.system<>system_arm_darwin) then
  3983. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  3984. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  3985. RS_R9,RS_R10,RS_R12,RS_R14],first_int_imreg,[])
  3986. else
  3987. { r9 is not available on Darwin according to the llvm code generator }
  3988. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  3989. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  3990. RS_R10,RS_R12,RS_R14],first_int_imreg,[]);
  3991. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  3992. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  3993. if current_settings.fputype in [fpu_fpv4_s16,fpu_vfpv3_d16] then
  3994. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  3995. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  3996. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  3997. ],first_mm_imreg,[])
  3998. else
  3999. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  4000. [RS_S0,RS_S1,RS_R2,RS_R3,RS_R4,RS_S31],first_mm_imreg,[]);
  4001. end;
  4002. procedure tthumb2cgarm.done_register_allocators;
  4003. begin
  4004. rg[R_INTREGISTER].free;
  4005. rg[R_FPUREGISTER].free;
  4006. rg[R_MMREGISTER].free;
  4007. inherited done_register_allocators;
  4008. end;
  4009. procedure tthumb2cgarm.a_call_reg(list : TAsmList;reg: tregister);
  4010. begin
  4011. list.concat(taicpu.op_reg(A_BLX, reg));
  4012. {
  4013. the compiler does not properly set this flag anymore in pass 1, and
  4014. for now we only need it after pass 2 (I hope) (JM)
  4015. if not(pi_do_call in current_procinfo.flags) then
  4016. internalerror(2003060703);
  4017. }
  4018. include(current_procinfo.flags,pi_do_call);
  4019. end;
  4020. procedure tthumb2cgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  4021. var
  4022. imm_shift : byte;
  4023. l : tasmlabel;
  4024. hr : treference;
  4025. begin
  4026. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  4027. internalerror(2002090902);
  4028. if is_thumb32_imm(a) then
  4029. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  4030. else if is_thumb32_imm(not(a)) then
  4031. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  4032. else if (a and $FFFF)=a then
  4033. list.concat(taicpu.op_reg_const(A_MOVW,reg,a))
  4034. else
  4035. begin
  4036. reference_reset(hr,4);
  4037. current_asmdata.getjumplabel(l);
  4038. cg.a_label(current_procinfo.aktlocaldata,l);
  4039. hr.symboldata:=current_procinfo.aktlocaldata.last;
  4040. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  4041. hr.symbol:=l;
  4042. hr.base:=NR_PC;
  4043. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  4044. end;
  4045. end;
  4046. procedure tthumb2cgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  4047. var
  4048. oppostfix:toppostfix;
  4049. usedtmpref: treference;
  4050. tmpreg,tmpreg2 : tregister;
  4051. so : tshifterop;
  4052. dir : integer;
  4053. begin
  4054. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  4055. FromSize := ToSize;
  4056. case FromSize of
  4057. { signed integer registers }
  4058. OS_8:
  4059. oppostfix:=PF_B;
  4060. OS_S8:
  4061. oppostfix:=PF_SB;
  4062. OS_16:
  4063. oppostfix:=PF_H;
  4064. OS_S16:
  4065. oppostfix:=PF_SH;
  4066. OS_32,
  4067. OS_S32:
  4068. oppostfix:=PF_None;
  4069. else
  4070. InternalError(200308299);
  4071. end;
  4072. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  4073. begin
  4074. if target_info.endian=endian_big then
  4075. dir:=-1
  4076. else
  4077. dir:=1;
  4078. case FromSize of
  4079. OS_16,OS_S16:
  4080. begin
  4081. { only complicated references need an extra loadaddr }
  4082. if assigned(ref.symbol) or
  4083. (ref.index<>NR_NO) or
  4084. (ref.offset<-255) or
  4085. (ref.offset>4094) or
  4086. { sometimes the compiler reused registers }
  4087. (reg=ref.index) or
  4088. (reg=ref.base) then
  4089. begin
  4090. tmpreg2:=getintregister(list,OS_INT);
  4091. a_loadaddr_ref_reg(list,ref,tmpreg2);
  4092. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  4093. end
  4094. else
  4095. usedtmpref:=ref;
  4096. if target_info.endian=endian_big then
  4097. inc(usedtmpref.offset,1);
  4098. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  4099. tmpreg:=getintregister(list,OS_INT);
  4100. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  4101. inc(usedtmpref.offset,dir);
  4102. if FromSize=OS_16 then
  4103. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  4104. else
  4105. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  4106. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4107. end;
  4108. OS_32,OS_S32:
  4109. begin
  4110. tmpreg:=getintregister(list,OS_INT);
  4111. { only complicated references need an extra loadaddr }
  4112. if assigned(ref.symbol) or
  4113. (ref.index<>NR_NO) or
  4114. (ref.offset<-255) or
  4115. (ref.offset>4092) or
  4116. { sometimes the compiler reused registers }
  4117. (reg=ref.index) or
  4118. (reg=ref.base) then
  4119. begin
  4120. tmpreg2:=getintregister(list,OS_INT);
  4121. a_loadaddr_ref_reg(list,ref,tmpreg2);
  4122. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  4123. end
  4124. else
  4125. usedtmpref:=ref;
  4126. shifterop_reset(so);so.shiftmode:=SM_LSL;
  4127. if ref.alignment=2 then
  4128. begin
  4129. if target_info.endian=endian_big then
  4130. inc(usedtmpref.offset,2);
  4131. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  4132. inc(usedtmpref.offset,dir*2);
  4133. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  4134. so.shiftimm:=16;
  4135. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4136. end
  4137. else
  4138. begin
  4139. if target_info.endian=endian_big then
  4140. inc(usedtmpref.offset,3);
  4141. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  4142. inc(usedtmpref.offset,dir);
  4143. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4144. so.shiftimm:=8;
  4145. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4146. inc(usedtmpref.offset,dir);
  4147. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4148. so.shiftimm:=16;
  4149. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4150. inc(usedtmpref.offset,dir);
  4151. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  4152. so.shiftimm:=24;
  4153. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  4154. end;
  4155. end
  4156. else
  4157. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  4158. end;
  4159. end
  4160. else
  4161. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  4162. if (fromsize=OS_S8) and (tosize = OS_16) then
  4163. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  4164. end;
  4165. procedure tthumb2cgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  4166. begin
  4167. if op = OP_NOT then
  4168. begin
  4169. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  4170. case size of
  4171. OS_8: list.concat(taicpu.op_reg_reg(A_UXTB,dst,dst));
  4172. OS_S8: list.concat(taicpu.op_reg_reg(A_SXTB,dst,dst));
  4173. OS_16: list.concat(taicpu.op_reg_reg(A_UXTH,dst,dst));
  4174. OS_S16: list.concat(taicpu.op_reg_reg(A_SXTH,dst,dst));
  4175. end;
  4176. end
  4177. else
  4178. inherited a_op_reg_reg(list, op, size, src, dst);
  4179. end;
  4180. procedure tthumb2cgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  4181. var
  4182. shift, width : byte;
  4183. tmpreg : tregister;
  4184. so : tshifterop;
  4185. l1 : longint;
  4186. begin
  4187. ovloc.loc:=LOC_VOID;
  4188. if {$ifopt R+}(a<>-2147483648) and{$endif} is_shifter_const(-a,shift) then
  4189. case op of
  4190. OP_ADD:
  4191. begin
  4192. op:=OP_SUB;
  4193. a:=aint(dword(-a));
  4194. end;
  4195. OP_SUB:
  4196. begin
  4197. op:=OP_ADD;
  4198. a:=aint(dword(-a));
  4199. end
  4200. end;
  4201. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  4202. case op of
  4203. OP_NEG,OP_NOT,
  4204. OP_DIV,OP_IDIV:
  4205. internalerror(200308285);
  4206. OP_SHL:
  4207. begin
  4208. if a>32 then
  4209. internalerror(2014020703);
  4210. if a<>0 then
  4211. begin
  4212. shifterop_reset(so);
  4213. so.shiftmode:=SM_LSL;
  4214. so.shiftimm:=a;
  4215. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4216. end
  4217. else
  4218. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4219. end;
  4220. OP_ROL:
  4221. begin
  4222. if a>32 then
  4223. internalerror(2014020704);
  4224. if a<>0 then
  4225. begin
  4226. shifterop_reset(so);
  4227. so.shiftmode:=SM_ROR;
  4228. so.shiftimm:=32-a;
  4229. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4230. end
  4231. else
  4232. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4233. end;
  4234. OP_ROR:
  4235. begin
  4236. if a>32 then
  4237. internalerror(2014020705);
  4238. if a<>0 then
  4239. begin
  4240. shifterop_reset(so);
  4241. so.shiftmode:=SM_ROR;
  4242. so.shiftimm:=a;
  4243. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4244. end
  4245. else
  4246. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4247. end;
  4248. OP_SHR:
  4249. begin
  4250. if a>32 then
  4251. internalerror(200308292);
  4252. shifterop_reset(so);
  4253. if a<>0 then
  4254. begin
  4255. so.shiftmode:=SM_LSR;
  4256. so.shiftimm:=a;
  4257. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4258. end
  4259. else
  4260. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4261. end;
  4262. OP_SAR:
  4263. begin
  4264. if a>32 then
  4265. internalerror(200308295);
  4266. if a<>0 then
  4267. begin
  4268. shifterop_reset(so);
  4269. so.shiftmode:=SM_ASR;
  4270. so.shiftimm:=a;
  4271. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  4272. end
  4273. else
  4274. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  4275. end;
  4276. else
  4277. if (op in [OP_SUB, OP_ADD]) and
  4278. ((a < 0) or
  4279. (a > 4095)) then
  4280. begin
  4281. tmpreg:=getintregister(list,size);
  4282. a_load_const_reg(list, size, a, tmpreg);
  4283. if cgsetflags or setflags then
  4284. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4285. list.concat(setoppostfix(
  4286. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src,tmpreg),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4287. end
  4288. else
  4289. begin
  4290. if cgsetflags or setflags then
  4291. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4292. list.concat(setoppostfix(
  4293. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4294. end;
  4295. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  4296. begin
  4297. ovloc.loc:=LOC_FLAGS;
  4298. case op of
  4299. OP_ADD:
  4300. ovloc.resflags:=F_CS;
  4301. OP_SUB:
  4302. ovloc.resflags:=F_CC;
  4303. end;
  4304. end;
  4305. end
  4306. else
  4307. begin
  4308. { there could be added some more sophisticated optimizations }
  4309. if (op in [OP_MUL,OP_IMUL]) and (a=1) then
  4310. a_load_reg_reg(list,size,size,src,dst)
  4311. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  4312. a_load_const_reg(list,size,0,dst)
  4313. else if (op in [OP_IMUL]) and (a=-1) then
  4314. a_op_reg_reg(list,OP_NEG,size,src,dst)
  4315. { we do this here instead in the peephole optimizer because
  4316. it saves us a register }
  4317. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  4318. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  4319. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  4320. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  4321. begin
  4322. if l1>32 then{roozbeh does this ever happen?}
  4323. internalerror(200308296);
  4324. shifterop_reset(so);
  4325. so.shiftmode:=SM_LSL;
  4326. so.shiftimm:=l1;
  4327. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  4328. end
  4329. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  4330. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  4331. begin
  4332. if l1>32 then{does this ever happen?}
  4333. internalerror(201205181);
  4334. shifterop_reset(so);
  4335. so.shiftmode:=SM_LSL;
  4336. so.shiftimm:=l1;
  4337. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,so));
  4338. end
  4339. 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
  4340. begin
  4341. { nothing to do on success }
  4342. end
  4343. { x := y and 0; just clears a register, this sometimes gets generated on 64bit ops.
  4344. Just using mov x, #0 might allow some easier optimizations down the line. }
  4345. else if (op = OP_AND) and (dword(a)=0) then
  4346. list.concat(taicpu.op_reg_const(A_MOV,dst,0))
  4347. { x := y AND $FFFFFFFF just copies the register, so use mov for better optimizations }
  4348. else if (op = OP_AND) and (not(dword(a))=0) then
  4349. list.concat(taicpu.op_reg_reg(A_MOV,dst,src))
  4350. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  4351. broader range of shifterconstants.}
  4352. {else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  4353. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))}
  4354. else if (op = OP_AND) and is_thumb32_imm(a) then
  4355. list.concat(taicpu.op_reg_reg_const(A_AND,dst,src,dword(a)))
  4356. else if (op = OP_AND) and (a = $FFFF) then
  4357. list.concat(taicpu.op_reg_reg(A_UXTH,dst,src))
  4358. else if (op = OP_AND) and is_thumb32_imm(not(dword(a))) then
  4359. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))
  4360. else if (op = OP_AND) and is_continuous_mask(not(a), shift, width) then
  4361. begin
  4362. a_load_reg_reg(list,size,size,src,dst);
  4363. list.concat(taicpu.op_reg_const_const(A_BFC,dst,shift,width))
  4364. end
  4365. else
  4366. begin
  4367. tmpreg:=getintregister(list,size);
  4368. a_load_const_reg(list,size,a,tmpreg);
  4369. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  4370. end;
  4371. end;
  4372. maybeadjustresult(list,op,size,dst);
  4373. end;
  4374. const
  4375. op_reg_reg_opcg2asmopThumb2: array[TOpCG] of tasmop =
  4376. (A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_MUL,A_MUL,A_NONE,A_MVN,A_ORR,
  4377. A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_NONE,A_ROR);
  4378. procedure tthumb2cgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  4379. var
  4380. so : tshifterop;
  4381. tmpreg,overflowreg : tregister;
  4382. asmop : tasmop;
  4383. begin
  4384. ovloc.loc:=LOC_VOID;
  4385. case op of
  4386. OP_NEG,OP_NOT:
  4387. internalerror(200308286);
  4388. OP_ROL:
  4389. begin
  4390. if not(size in [OS_32,OS_S32]) then
  4391. internalerror(2008072801);
  4392. { simulate ROL by ror'ing 32-value }
  4393. tmpreg:=getintregister(list,OS_32);
  4394. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,32));
  4395. list.concat(taicpu.op_reg_reg_reg(A_SUB,src1,tmpreg,src1));
  4396. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  4397. end;
  4398. OP_ROR:
  4399. begin
  4400. if not(size in [OS_32,OS_S32]) then
  4401. internalerror(2008072802);
  4402. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  4403. end;
  4404. OP_IMUL,
  4405. OP_MUL:
  4406. begin
  4407. if cgsetflags or setflags then
  4408. begin
  4409. overflowreg:=getintregister(list,size);
  4410. if op=OP_IMUL then
  4411. asmop:=A_SMULL
  4412. else
  4413. asmop:=A_UMULL;
  4414. { the arm doesn't allow that rd and rm are the same }
  4415. if dst=src2 then
  4416. begin
  4417. if dst<>src1 then
  4418. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  4419. else
  4420. begin
  4421. tmpreg:=getintregister(list,size);
  4422. a_load_reg_reg(list,size,size,src2,dst);
  4423. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  4424. end;
  4425. end
  4426. else
  4427. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  4428. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4429. if op=OP_IMUL then
  4430. begin
  4431. shifterop_reset(so);
  4432. so.shiftmode:=SM_ASR;
  4433. so.shiftimm:=31;
  4434. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  4435. end
  4436. else
  4437. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  4438. ovloc.loc:=LOC_FLAGS;
  4439. ovloc.resflags:=F_NE;
  4440. end
  4441. else
  4442. begin
  4443. { the arm doesn't allow that rd and rm are the same }
  4444. if dst=src2 then
  4445. begin
  4446. if dst<>src1 then
  4447. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  4448. else
  4449. begin
  4450. tmpreg:=getintregister(list,size);
  4451. a_load_reg_reg(list,size,size,src2,dst);
  4452. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  4453. end;
  4454. end
  4455. else
  4456. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  4457. end;
  4458. end;
  4459. else
  4460. begin
  4461. if cgsetflags or setflags then
  4462. a_reg_alloc(list,NR_DEFAULTFLAGS);
  4463. {$ifdef dummy}
  4464. { R13 is not allowed for certain instruction operands }
  4465. if op_reg_reg_opcg2asmopThumb2[op] in [A_ADD,A_SUB,A_AND,A_BIC,A_EOR] then
  4466. begin
  4467. if getsupreg(dst)=RS_R13 then
  4468. begin
  4469. tmpreg:=getintregister(list,OS_INT);
  4470. a_load_reg_reg(list,OS_INT,OS_INT,dst,tmpreg);
  4471. dst:=tmpreg;
  4472. end;
  4473. if getsupreg(src1)=RS_R13 then
  4474. begin
  4475. tmpreg:=getintregister(list,OS_INT);
  4476. a_load_reg_reg(list,OS_INT,OS_INT,src1,tmpreg);
  4477. src1:=tmpreg;
  4478. end;
  4479. end;
  4480. {$endif}
  4481. list.concat(setoppostfix(
  4482. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmopThumb2[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  4483. end;
  4484. end;
  4485. maybeadjustresult(list,op,size,dst);
  4486. end;
  4487. procedure tthumb2cgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  4488. var item: taicpu;
  4489. begin
  4490. list.concat(taicpu.op_cond(A_ITE, flags_to_cond(f)));
  4491. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  4492. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  4493. end;
  4494. procedure tthumb2cgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  4495. var
  4496. ref : treference;
  4497. shift : byte;
  4498. firstfloatreg,lastfloatreg,
  4499. r : byte;
  4500. regs : tcpuregisterset;
  4501. stackmisalignment: pint;
  4502. begin
  4503. LocalSize:=align(LocalSize,4);
  4504. { call instruction does not put anything on the stack }
  4505. stackmisalignment:=0;
  4506. if not(nostackframe) then
  4507. begin
  4508. firstfloatreg:=RS_NO;
  4509. lastfloatreg:=RS_NO;
  4510. { save floating point registers? }
  4511. for r:=RS_F0 to RS_F7 do
  4512. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  4513. begin
  4514. if firstfloatreg=RS_NO then
  4515. firstfloatreg:=r;
  4516. lastfloatreg:=r;
  4517. inc(stackmisalignment,12);
  4518. end;
  4519. a_reg_alloc(list,NR_STACK_POINTER_REG);
  4520. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4521. begin
  4522. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  4523. a_reg_alloc(list,NR_R12);
  4524. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  4525. end;
  4526. { save int registers }
  4527. reference_reset(ref,4);
  4528. ref.index:=NR_STACK_POINTER_REG;
  4529. ref.addressmode:=AM_PREINDEXED;
  4530. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  4531. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4532. regs:=regs+[RS_FRAME_POINTER_REG,RS_R14]
  4533. else if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  4534. include(regs,RS_R14);
  4535. if regs<>[] then
  4536. begin
  4537. for r:=RS_R0 to RS_R15 do
  4538. if (r in regs) then
  4539. inc(stackmisalignment,4);
  4540. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  4541. end;
  4542. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  4543. begin
  4544. { the framepointer now points to the saved R15, so the saved
  4545. framepointer is at R11-12 (for get_caller_frame) }
  4546. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  4547. a_reg_dealloc(list,NR_R12);
  4548. end;
  4549. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  4550. if (LocalSize<>0) or
  4551. ((stackmisalignment<>0) and
  4552. ((pi_do_call in current_procinfo.flags) or
  4553. (po_assembler in current_procinfo.procdef.procoptions))) then
  4554. begin
  4555. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  4556. if not(is_shifter_const(localsize,shift)) then
  4557. begin
  4558. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  4559. a_reg_alloc(list,NR_R12);
  4560. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  4561. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  4562. a_reg_dealloc(list,NR_R12);
  4563. end
  4564. else
  4565. begin
  4566. a_reg_dealloc(list,NR_R12);
  4567. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  4568. end;
  4569. end;
  4570. if firstfloatreg<>RS_NO then
  4571. begin
  4572. reference_reset(ref,4);
  4573. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  4574. begin
  4575. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  4576. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  4577. ref.base:=NR_R12;
  4578. end
  4579. else
  4580. begin
  4581. ref.base:=current_procinfo.framepointer;
  4582. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  4583. end;
  4584. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  4585. lastfloatreg-firstfloatreg+1,ref));
  4586. end;
  4587. end;
  4588. end;
  4589. procedure tthumb2cgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  4590. var
  4591. ref : treference;
  4592. firstfloatreg,lastfloatreg,
  4593. r : byte;
  4594. shift : byte;
  4595. regs : tcpuregisterset;
  4596. LocalSize : longint;
  4597. stackmisalignment: pint;
  4598. begin
  4599. if not(nostackframe) then
  4600. begin
  4601. stackmisalignment:=0;
  4602. { restore floating point register }
  4603. firstfloatreg:=RS_NO;
  4604. lastfloatreg:=RS_NO;
  4605. { save floating point registers? }
  4606. for r:=RS_F0 to RS_F7 do
  4607. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  4608. begin
  4609. if firstfloatreg=RS_NO then
  4610. firstfloatreg:=r;
  4611. lastfloatreg:=r;
  4612. { floating point register space is already included in
  4613. localsize below by calc_stackframe_size
  4614. inc(stackmisalignment,12);
  4615. }
  4616. end;
  4617. if firstfloatreg<>RS_NO then
  4618. begin
  4619. reference_reset(ref,4);
  4620. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  4621. begin
  4622. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  4623. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  4624. ref.base:=NR_R12;
  4625. end
  4626. else
  4627. begin
  4628. ref.base:=current_procinfo.framepointer;
  4629. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  4630. end;
  4631. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  4632. lastfloatreg-firstfloatreg+1,ref));
  4633. end;
  4634. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  4635. if (pi_do_call in current_procinfo.flags) or (regs<>[]) then
  4636. begin
  4637. exclude(regs,RS_R14);
  4638. include(regs,RS_R15);
  4639. end;
  4640. if (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  4641. regs:=regs+[RS_FRAME_POINTER_REG,RS_R15];
  4642. for r:=RS_R0 to RS_R15 do
  4643. if (r in regs) then
  4644. inc(stackmisalignment,4);
  4645. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  4646. LocalSize:=current_procinfo.calc_stackframe_size;
  4647. if (LocalSize<>0) or
  4648. ((stackmisalignment<>0) and
  4649. ((pi_do_call in current_procinfo.flags) or
  4650. (po_assembler in current_procinfo.procdef.procoptions))) then
  4651. begin
  4652. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  4653. if not(is_shifter_const(LocalSize,shift)) then
  4654. begin
  4655. a_reg_alloc(list,NR_R12);
  4656. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  4657. list.concat(taicpu.op_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_R12));
  4658. a_reg_dealloc(list,NR_R12);
  4659. end
  4660. else
  4661. begin
  4662. a_reg_dealloc(list,NR_R12);
  4663. list.concat(taicpu.op_reg_const(A_ADD,NR_STACK_POINTER_REG,LocalSize));
  4664. end;
  4665. end;
  4666. if regs=[] then
  4667. list.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14))
  4668. else
  4669. begin
  4670. reference_reset(ref,4);
  4671. ref.index:=NR_STACK_POINTER_REG;
  4672. ref.addressmode:=AM_PREINDEXED;
  4673. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  4674. end;
  4675. end
  4676. else
  4677. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14));
  4678. end;
  4679. function tthumb2cgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  4680. var
  4681. tmpreg : tregister;
  4682. tmpref : treference;
  4683. l : tasmlabel;
  4684. so: tshifterop;
  4685. begin
  4686. tmpreg:=NR_NO;
  4687. { Be sure to have a base register }
  4688. if (ref.base=NR_NO) then
  4689. begin
  4690. if ref.shiftmode<>SM_None then
  4691. internalerror(2014020706);
  4692. ref.base:=ref.index;
  4693. ref.index:=NR_NO;
  4694. end;
  4695. { absolute symbols can't be handled directly, we've to store the symbol reference
  4696. in the text segment and access it pc relative
  4697. For now, we assume that references where base or index equals to PC are already
  4698. relative, all other references are assumed to be absolute and thus they need
  4699. to be handled extra.
  4700. A proper solution would be to change refoptions to a set and store the information
  4701. if the symbol is absolute or relative there.
  4702. }
  4703. if (assigned(ref.symbol) and
  4704. not(is_pc(ref.base)) and
  4705. not(is_pc(ref.index))
  4706. ) or
  4707. { [#xxx] isn't a valid address operand }
  4708. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  4709. //(ref.offset<-4095) or
  4710. (ref.offset<-255) or
  4711. (ref.offset>4095) or
  4712. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  4713. ((ref.offset<-255) or
  4714. (ref.offset>255)
  4715. )
  4716. ) or
  4717. (((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) or (op=A_VSTR) or (op=A_VLDR)) and
  4718. ((ref.offset<-1020) or
  4719. (ref.offset>1020) or
  4720. ((abs(ref.offset) mod 4)<>0) or
  4721. { the usual pc relative symbol handling assumes possible offsets of +/- 4095 }
  4722. assigned(ref.symbol)
  4723. )
  4724. ) then
  4725. begin
  4726. reference_reset(tmpref,4);
  4727. { load symbol }
  4728. tmpreg:=getintregister(list,OS_INT);
  4729. if assigned(ref.symbol) then
  4730. begin
  4731. current_asmdata.getjumplabel(l);
  4732. cg.a_label(current_procinfo.aktlocaldata,l);
  4733. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  4734. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset));
  4735. { load consts entry }
  4736. tmpref.symbol:=l;
  4737. tmpref.base:=NR_R15;
  4738. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  4739. { in case of LDF/STF, we got rid of the NR_R15 }
  4740. if is_pc(ref.base) then
  4741. ref.base:=NR_NO;
  4742. if is_pc(ref.index) then
  4743. ref.index:=NR_NO;
  4744. end
  4745. else
  4746. a_load_const_reg(list,OS_ADDR,ref.offset,tmpreg);
  4747. if (ref.base<>NR_NO) then
  4748. begin
  4749. if ref.index<>NR_NO then
  4750. begin
  4751. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  4752. ref.base:=tmpreg;
  4753. end
  4754. else
  4755. begin
  4756. ref.index:=tmpreg;
  4757. ref.shiftimm:=0;
  4758. ref.signindex:=1;
  4759. ref.shiftmode:=SM_None;
  4760. end;
  4761. end
  4762. else
  4763. ref.base:=tmpreg;
  4764. ref.offset:=0;
  4765. ref.symbol:=nil;
  4766. end;
  4767. if (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  4768. begin
  4769. if tmpreg<>NR_NO then
  4770. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg,tmpreg)
  4771. else
  4772. begin
  4773. tmpreg:=getintregister(list,OS_ADDR);
  4774. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg);
  4775. ref.base:=tmpreg;
  4776. end;
  4777. ref.offset:=0;
  4778. end;
  4779. { Hack? Thumb2 doesn't allow PC indexed addressing modes(although it does in the specification) }
  4780. if (ref.base=NR_R15) and (ref.index<>NR_NO) and (ref.shiftmode <> sm_none) then
  4781. begin
  4782. tmpreg:=getintregister(list,OS_ADDR);
  4783. list.concat(taicpu.op_reg_reg(A_MOV, tmpreg, NR_R15));
  4784. ref.base := tmpreg;
  4785. end;
  4786. { floating point operations have only limited references
  4787. we expect here, that a base is already set }
  4788. 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
  4789. begin
  4790. if ref.shiftmode<>SM_none then
  4791. internalerror(200309121);
  4792. if tmpreg<>NR_NO then
  4793. begin
  4794. if ref.base=tmpreg then
  4795. begin
  4796. if ref.signindex<0 then
  4797. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,tmpreg,ref.index))
  4798. else
  4799. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,tmpreg,ref.index));
  4800. ref.index:=NR_NO;
  4801. end
  4802. else
  4803. begin
  4804. if ref.index<>tmpreg then
  4805. internalerror(200403161);
  4806. if ref.signindex<0 then
  4807. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,ref.base,tmpreg))
  4808. else
  4809. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  4810. ref.base:=tmpreg;
  4811. ref.index:=NR_NO;
  4812. end;
  4813. end
  4814. else
  4815. begin
  4816. tmpreg:=getintregister(list,OS_ADDR);
  4817. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,ref.index));
  4818. ref.base:=tmpreg;
  4819. ref.index:=NR_NO;
  4820. end;
  4821. end;
  4822. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  4823. Result := ref;
  4824. end;
  4825. procedure tthumb2cgarm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  4826. var
  4827. instr: taicpu;
  4828. begin
  4829. if (fromsize=OS_F32) and
  4830. (tosize=OS_F32) then
  4831. begin
  4832. instr:=setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32);
  4833. list.Concat(instr);
  4834. add_move_instruction(instr);
  4835. end
  4836. else if (fromsize=OS_F64) and
  4837. (tosize=OS_F64) then
  4838. begin
  4839. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,tregister(longint(reg2)+1),tregister(longint(reg1)+1)), PF_F32));
  4840. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32));
  4841. end
  4842. else if (fromsize=OS_F32) and
  4843. (tosize=OS_F64) then
  4844. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,reg2,reg1), PF_F32))
  4845. begin
  4846. //list.concat(nil);
  4847. end;
  4848. end;
  4849. procedure tthumb2cgarm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  4850. begin
  4851. if fromsize=OS_F32 then
  4852. handle_load_store(list,A_VLDR,PF_F32,reg,ref)
  4853. else
  4854. handle_load_store(list,A_VLDR,PF_F64,reg,ref);
  4855. end;
  4856. procedure tthumb2cgarm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  4857. begin
  4858. if fromsize=OS_F32 then
  4859. handle_load_store(list,A_VSTR,PF_F32,reg,ref)
  4860. else
  4861. handle_load_store(list,A_VSTR,PF_F64,reg,ref);
  4862. end;
  4863. procedure tthumb2cgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  4864. begin
  4865. if //(shuffle=nil) and
  4866. (tosize=OS_F32) then
  4867. list.Concat(taicpu.op_reg_reg(A_VMOV,mmreg,intreg))
  4868. else
  4869. internalerror(2012100813);
  4870. end;
  4871. procedure tthumb2cgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tcgsize; mmreg, intreg: tregister; shuffle: pmmshuffle);
  4872. begin
  4873. if //(shuffle=nil) and
  4874. (fromsize=OS_F32) then
  4875. list.Concat(taicpu.op_reg_reg(A_VMOV,intreg,mmreg))
  4876. else
  4877. internalerror(2012100814);
  4878. end;
  4879. procedure tthumb2cg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  4880. var tmpreg: tregister;
  4881. begin
  4882. case op of
  4883. OP_NEG:
  4884. begin
  4885. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4886. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  4887. tmpreg:=cg.getintregister(list,OS_32);
  4888. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,0));
  4889. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,tmpreg,regsrc.reghi));
  4890. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  4891. end;
  4892. else
  4893. inherited a_op64_reg_reg(list, op, size, regsrc, regdst);
  4894. end;
  4895. end;
  4896. procedure tthumbcg64farm.a_op64_reg_reg(list: TAsmList; op: TOpCG; size: tcgsize; regsrc, regdst: tregister64);
  4897. begin
  4898. case op of
  4899. OP_NEG:
  4900. begin
  4901. list.concat(taicpu.op_reg_const(A_MOV,regdst.reglo,0));
  4902. list.concat(taicpu.op_reg_const(A_MOV,regdst.reghi,0));
  4903. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4904. list.concat(taicpu.op_reg_reg(A_SUB,regdst.reglo,regsrc.reglo));
  4905. list.concat(taicpu.op_reg_reg(A_SBC,regdst.reghi,regsrc.reghi));
  4906. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  4907. end;
  4908. OP_NOT:
  4909. begin
  4910. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  4911. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  4912. end;
  4913. OP_AND,OP_OR,OP_XOR:
  4914. begin
  4915. cg.a_op_reg_reg(list,op,OS_32,regsrc.reglo,regdst.reglo);
  4916. cg.a_op_reg_reg(list,op,OS_32,regsrc.reghi,regdst.reghi);
  4917. end;
  4918. OP_ADD:
  4919. begin
  4920. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4921. list.concat(taicpu.op_reg_reg(A_ADD,regdst.reglo,regsrc.reglo));
  4922. list.concat(taicpu.op_reg_reg(A_ADC,regdst.reghi,regsrc.reghi));
  4923. end;
  4924. OP_SUB:
  4925. begin
  4926. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4927. list.concat(taicpu.op_reg_reg(A_SUB,regdst.reglo,regsrc.reglo));
  4928. list.concat(taicpu.op_reg_reg(A_SBC,regdst.reghi,regsrc.reghi));
  4929. end;
  4930. else
  4931. internalerror(2003083101);
  4932. end;
  4933. end;
  4934. procedure tthumbcg64farm.a_op64_const_reg(list: TAsmList; op: TOpCG; size: tcgsize; value: int64; reg: tregister64);
  4935. var
  4936. tmpreg : tregister;
  4937. b : byte;
  4938. begin
  4939. case op of
  4940. OP_AND,OP_OR,OP_XOR:
  4941. begin
  4942. cg.a_op_const_reg(list,op,OS_32,aint(lo(value)),reg.reglo);
  4943. cg.a_op_const_reg(list,op,OS_32,aint(hi(value)),reg.reghi);
  4944. end;
  4945. OP_ADD:
  4946. begin
  4947. if (aint(lo(value))>=0) and (aint(lo(value))<=255) then
  4948. begin
  4949. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4950. list.concat(taicpu.op_reg_const(A_ADD,reg.reglo,aint(lo(value))));
  4951. end
  4952. else
  4953. begin
  4954. tmpreg:=cg.getintregister(list,OS_32);
  4955. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  4956. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4957. list.concat(taicpu.op_reg_reg(A_ADD,reg.reglo,tmpreg));
  4958. end;
  4959. tmpreg:=cg.getintregister(list,OS_32);
  4960. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  4961. list.concat(taicpu.op_reg_reg(A_ADC,reg.reghi,tmpreg));
  4962. end;
  4963. OP_SUB:
  4964. begin
  4965. if (aint(lo(value))>=0) and (aint(lo(value))<=255) then
  4966. begin
  4967. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4968. list.concat(taicpu.op_reg_const(A_SUB,reg.reglo,aint(lo(value))))
  4969. end
  4970. else
  4971. begin
  4972. tmpreg:=cg.getintregister(list,OS_32);
  4973. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  4974. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  4975. list.concat(taicpu.op_reg_reg(A_SUB,reg.reglo,tmpreg));
  4976. end;
  4977. tmpreg:=cg.getintregister(list,OS_32);
  4978. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  4979. list.concat(taicpu.op_reg_reg(A_SBC,reg.reghi,tmpreg));
  4980. end;
  4981. else
  4982. internalerror(2003083101);
  4983. end;
  4984. end;
  4985. procedure create_codegen;
  4986. begin
  4987. if GenerateThumb2Code then
  4988. begin
  4989. cg:=tthumb2cgarm.create;
  4990. cg64:=tthumb2cg64farm.create;
  4991. casmoptimizer:=TCpuThumb2AsmOptimizer;
  4992. end
  4993. else if GenerateThumbCode then
  4994. begin
  4995. cg:=tthumbcgarm.create;
  4996. cg64:=tthumbcg64farm.create;
  4997. // casmoptimizer:=TCpuThumbAsmOptimizer;
  4998. end
  4999. else
  5000. begin
  5001. cg:=tarmcgarm.create;
  5002. cg64:=tarmcg64farm.create;
  5003. casmoptimizer:=TCpuAsmOptimizer;
  5004. end;
  5005. end;
  5006. end.