cgcpu.pas 163 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134
  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,node,cg64f32,rgcpu;
  27. type
  28. tcgarm = class(tcg)
  29. { true, if the next arithmetic operation should modify the flags }
  30. cgsetflags : boolean;
  31. procedure a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const paraloc : TCGPara);override;
  32. procedure a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);override;
  33. procedure a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : TCGPara);override;
  34. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  35. procedure a_call_reg(list : TAsmList;reg: tregister);override;
  36. procedure a_call_ref(list : TAsmList;ref: treference);override;
  37. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  38. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  39. procedure a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  40. size: tcgsize; a: tcgint; src, dst: tregister); override;
  41. procedure a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  42. size: tcgsize; src1, src2, dst: tregister); override;
  43. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  44. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  45. { move instructions }
  46. procedure a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);override;
  47. procedure a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);override;
  48. function a_internal_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference):treference;
  49. function a_internal_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister):treference;
  50. { fpu move instructions }
  51. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  52. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  53. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  54. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);override;
  55. { comparison operations }
  56. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  57. l : tasmlabel);override;
  58. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  59. procedure a_jmp_name(list : TAsmList;const s : string); override;
  60. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  61. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  62. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  63. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  64. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  65. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  66. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  67. procedure g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);override;
  68. procedure g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  69. procedure g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : tcgint;aligned : boolean);
  70. procedure g_overflowcheck(list: TAsmList; const l: tlocation; def: tdef); override;
  71. procedure g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);override;
  72. procedure g_save_registers(list : TAsmList);override;
  73. procedure g_restore_registers(list : TAsmList);override;
  74. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  75. procedure fixref(list : TAsmList;var ref : treference);
  76. function handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference; virtual;
  77. procedure g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);override;
  78. procedure g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint); override;
  79. procedure g_stackpointer_alloc(list : TAsmList;size : longint);override;
  80. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  81. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  82. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  83. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize;intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  84. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize;mmreg, intreg: tregister; shuffle : pmmshuffle); override;
  85. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle); override;
  86. { Transform unsupported methods into Internal errors }
  87. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: TCGSize; src, dst: TRegister); override;
  88. { try to generate optimized 32 Bit multiplication, returns true if successful generated }
  89. function try_optimized_mul32_const_reg_reg(list: TAsmList; a: tcgint; src, dst: tregister) : boolean;
  90. { clear out potential overflow bits from 8 or 16 bit operations }
  91. { the upper 24/16 bits of a register after an operation }
  92. procedure maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  93. function get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  94. end;
  95. tarmcgarm = class(tcgarm)
  96. procedure init_register_allocators;override;
  97. procedure done_register_allocators;override;
  98. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);override;
  99. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  100. end;
  101. tcg64farm = class(tcg64f32)
  102. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  103. procedure a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);override;
  104. procedure a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);override;
  105. procedure a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);override;
  106. procedure a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  107. procedure a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);override;
  108. procedure a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister);override;
  109. procedure a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64);override;
  110. end;
  111. Tthumb2cgarm = class(tcgarm)
  112. procedure init_register_allocators;override;
  113. procedure done_register_allocators;override;
  114. procedure a_call_reg(list : TAsmList;reg: tregister);override;
  115. procedure a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);override;
  116. procedure a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);override;
  117. procedure a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  118. procedure a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);override;
  119. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister); override;
  120. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  121. procedure g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean); override;
  122. function handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference; override;
  123. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  124. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  125. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  126. procedure a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize;intreg, mmreg: tregister; shuffle: pmmshuffle); override;
  127. procedure a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize;mmreg, intreg: tregister; shuffle : pmmshuffle); override;
  128. end;
  129. tthumb2cg64farm = class(tcg64farm)
  130. procedure a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);override;
  131. end;
  132. const
  133. OpCmp2AsmCond : Array[topcmp] of TAsmCond = (C_NONE,C_EQ,C_GT,
  134. C_LT,C_GE,C_LE,C_NE,C_LS,C_CC,C_CS,C_HI);
  135. winstackpagesize = 4096;
  136. function get_fpu_postfix(def : tdef) : toppostfix;
  137. procedure create_codegen;
  138. implementation
  139. uses
  140. globals,verbose,systems,cutils,sysutils,
  141. aopt,aoptcpu,
  142. fmodule,
  143. symconst,symsym,
  144. tgobj,
  145. procinfo,cpupi,
  146. paramgr;
  147. function get_fpu_postfix(def : tdef) : toppostfix;
  148. begin
  149. if def.typ=floatdef then
  150. begin
  151. case tfloatdef(def).floattype of
  152. s32real:
  153. result:=PF_S;
  154. s64real:
  155. result:=PF_D;
  156. s80real:
  157. result:=PF_E;
  158. else
  159. internalerror(200401272);
  160. end;
  161. end
  162. else
  163. internalerror(200401271);
  164. end;
  165. procedure tarmcgarm.init_register_allocators;
  166. begin
  167. inherited init_register_allocators;
  168. { currently, we always save R14, so we can use it }
  169. if (target_info.system<>system_arm_darwin) then
  170. begin
  171. if assigned(current_procinfo) and (current_procinfo.framepointer<>NR_R11) then
  172. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  173. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R12,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  174. RS_R9,RS_R10,RS_R11,RS_R14],first_int_imreg,[])
  175. else
  176. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  177. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R12,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  178. RS_R9,RS_R10,RS_R14],first_int_imreg,[])
  179. end
  180. else
  181. { r7 is not available on Darwin, it's used as frame pointer (always,
  182. for backtrace support -- also in gcc/clang -> R11 can be used).
  183. r9 is volatile }
  184. rg[R_INTREGISTER]:=trgintcpu.create(R_INTREGISTER,R_SUBWHOLE,
  185. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R9,RS_R12,RS_R4,RS_R5,RS_R6,RS_R8,
  186. RS_R10,RS_R11,RS_R14],first_int_imreg,[]);
  187. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  188. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  189. { The register allocator currently cannot deal with multiple
  190. non-overlapping subregs per register, so we can only use
  191. half the single precision registers for now (as sub registers of the
  192. double precision ones). }
  193. if current_settings.fputype=fpu_vfpv3 then
  194. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  195. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  196. 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,
  197. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  198. ],first_mm_imreg,[])
  199. else
  200. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  201. [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,[]);
  202. end;
  203. procedure tarmcgarm.done_register_allocators;
  204. begin
  205. rg[R_INTREGISTER].free;
  206. rg[R_FPUREGISTER].free;
  207. rg[R_MMREGISTER].free;
  208. inherited done_register_allocators;
  209. end;
  210. procedure tarmcgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  211. var
  212. imm_shift : byte;
  213. l : tasmlabel;
  214. hr : treference;
  215. imm1, imm2: DWord;
  216. begin
  217. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  218. internalerror(2002090902);
  219. if is_shifter_const(a,imm_shift) then
  220. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  221. else if is_shifter_const(not(a),imm_shift) then
  222. list.concat(taicpu.op_reg_const(A_MVN,reg,not(a)))
  223. { loading of constants with mov and orr }
  224. else if (split_into_shifter_const(a,imm1, imm2)) then
  225. begin
  226. list.concat(taicpu.op_reg_const(A_MOV,reg, imm1));
  227. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg, imm2));
  228. end
  229. { loading of constants with mvn and bic }
  230. else if (split_into_shifter_const(not(a), imm1, imm2)) then
  231. begin
  232. list.concat(taicpu.op_reg_const(A_MVN,reg, imm1));
  233. list.concat(taicpu.op_reg_reg_const(A_BIC,reg,reg, imm2));
  234. end
  235. else
  236. begin
  237. reference_reset(hr,4);
  238. current_asmdata.getjumplabel(l);
  239. cg.a_label(current_procinfo.aktlocaldata,l);
  240. hr.symboldata:=current_procinfo.aktlocaldata.last;
  241. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  242. hr.symbol:=l;
  243. hr.base:=NR_PC;
  244. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  245. end;
  246. end;
  247. procedure tarmcgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  248. var
  249. oppostfix:toppostfix;
  250. usedtmpref: treference;
  251. tmpreg,tmpreg2 : tregister;
  252. so : tshifterop;
  253. dir : integer;
  254. begin
  255. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  256. FromSize := ToSize;
  257. case FromSize of
  258. { signed integer registers }
  259. OS_8:
  260. oppostfix:=PF_B;
  261. OS_S8:
  262. oppostfix:=PF_SB;
  263. OS_16:
  264. oppostfix:=PF_H;
  265. OS_S16:
  266. oppostfix:=PF_SH;
  267. OS_32,
  268. OS_S32:
  269. oppostfix:=PF_None;
  270. else
  271. InternalError(200308297);
  272. end;
  273. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  274. begin
  275. if target_info.endian=endian_big then
  276. dir:=-1
  277. else
  278. dir:=1;
  279. case FromSize of
  280. OS_16,OS_S16:
  281. begin
  282. { only complicated references need an extra loadaddr }
  283. if assigned(ref.symbol) or
  284. (ref.index<>NR_NO) or
  285. (ref.offset<-4095) or
  286. (ref.offset>4094) or
  287. { sometimes the compiler reused registers }
  288. (reg=ref.index) or
  289. (reg=ref.base) then
  290. begin
  291. tmpreg2:=getintregister(list,OS_INT);
  292. a_loadaddr_ref_reg(list,ref,tmpreg2);
  293. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  294. end
  295. else
  296. usedtmpref:=ref;
  297. if target_info.endian=endian_big then
  298. inc(usedtmpref.offset,1);
  299. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  300. tmpreg:=getintregister(list,OS_INT);
  301. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  302. inc(usedtmpref.offset,dir);
  303. if FromSize=OS_16 then
  304. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  305. else
  306. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  307. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  308. end;
  309. OS_32,OS_S32:
  310. begin
  311. tmpreg:=getintregister(list,OS_INT);
  312. { only complicated references need an extra loadaddr }
  313. if assigned(ref.symbol) or
  314. (ref.index<>NR_NO) or
  315. (ref.offset<-4095) or
  316. (ref.offset>4092) or
  317. { sometimes the compiler reused registers }
  318. (reg=ref.index) or
  319. (reg=ref.base) then
  320. begin
  321. tmpreg2:=getintregister(list,OS_INT);
  322. a_loadaddr_ref_reg(list,ref,tmpreg2);
  323. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  324. end
  325. else
  326. usedtmpref:=ref;
  327. shifterop_reset(so);so.shiftmode:=SM_LSL;
  328. if ref.alignment=2 then
  329. begin
  330. if target_info.endian=endian_big then
  331. inc(usedtmpref.offset,2);
  332. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  333. inc(usedtmpref.offset,dir*2);
  334. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  335. so.shiftimm:=16;
  336. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  337. end
  338. else
  339. begin
  340. tmpreg2:=getintregister(list,OS_INT);
  341. if target_info.endian=endian_big then
  342. inc(usedtmpref.offset,3);
  343. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  344. inc(usedtmpref.offset,dir);
  345. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  346. inc(usedtmpref.offset,dir);
  347. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg2);
  348. so.shiftimm:=8;
  349. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  350. inc(usedtmpref.offset,dir);
  351. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  352. so.shiftimm:=16;
  353. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg2,so));
  354. so.shiftimm:=24;
  355. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  356. end;
  357. end
  358. else
  359. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  360. end;
  361. end
  362. else
  363. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  364. if (fromsize=OS_S8) and (tosize = OS_16) then
  365. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  366. end;
  367. procedure tcgarm.a_load_const_cgpara(list : TAsmList;size : tcgsize;a : tcgint;const paraloc : TCGPara);
  368. var
  369. ref: treference;
  370. begin
  371. paraloc.check_simple_location;
  372. paramanager.allocparaloc(list,paraloc.location);
  373. case paraloc.location^.loc of
  374. LOC_REGISTER,LOC_CREGISTER:
  375. a_load_const_reg(list,size,a,paraloc.location^.register);
  376. LOC_REFERENCE:
  377. begin
  378. reference_reset(ref,paraloc.alignment);
  379. ref.base:=paraloc.location^.reference.index;
  380. ref.offset:=paraloc.location^.reference.offset;
  381. a_load_const_ref(list,size,a,ref);
  382. end;
  383. else
  384. internalerror(2002081101);
  385. end;
  386. end;
  387. procedure tcgarm.a_load_ref_cgpara(list : TAsmList;size : tcgsize;const r : treference;const paraloc : TCGPara);
  388. var
  389. tmpref, ref: treference;
  390. location: pcgparalocation;
  391. sizeleft: aint;
  392. begin
  393. location := paraloc.location;
  394. tmpref := r;
  395. sizeleft := paraloc.intsize;
  396. while assigned(location) do
  397. begin
  398. paramanager.allocparaloc(list,location);
  399. case location^.loc of
  400. LOC_REGISTER,LOC_CREGISTER:
  401. a_load_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  402. LOC_REFERENCE:
  403. begin
  404. reference_reset_base(ref,location^.reference.index,location^.reference.offset,paraloc.alignment);
  405. { doubles in softemu mode have a strange order of registers and references }
  406. if location^.size=OS_32 then
  407. g_concatcopy(list,tmpref,ref,4)
  408. else
  409. begin
  410. g_concatcopy(list,tmpref,ref,sizeleft);
  411. if assigned(location^.next) then
  412. internalerror(2005010710);
  413. end;
  414. end;
  415. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  416. case location^.size of
  417. OS_F32, OS_F64:
  418. a_loadfpu_ref_reg(list,location^.size,location^.size,tmpref,location^.register);
  419. else
  420. internalerror(2002072801);
  421. end;
  422. LOC_VOID:
  423. begin
  424. // nothing to do
  425. end;
  426. else
  427. internalerror(2002081103);
  428. end;
  429. inc(tmpref.offset,tcgsize2size[location^.size]);
  430. dec(sizeleft,tcgsize2size[location^.size]);
  431. location := location^.next;
  432. end;
  433. end;
  434. procedure tcgarm.a_loadaddr_ref_cgpara(list : TAsmList;const r : treference;const paraloc : TCGPara);
  435. var
  436. ref: treference;
  437. tmpreg: tregister;
  438. begin
  439. paraloc.check_simple_location;
  440. paramanager.allocparaloc(list,paraloc.location);
  441. case paraloc.location^.loc of
  442. LOC_REGISTER,LOC_CREGISTER:
  443. a_loadaddr_ref_reg(list,r,paraloc.location^.register);
  444. LOC_REFERENCE:
  445. begin
  446. reference_reset(ref,paraloc.alignment);
  447. ref.base := paraloc.location^.reference.index;
  448. ref.offset := paraloc.location^.reference.offset;
  449. tmpreg := getintregister(list,OS_ADDR);
  450. a_loadaddr_ref_reg(list,r,tmpreg);
  451. a_load_reg_ref(list,OS_ADDR,OS_ADDR,tmpreg,ref);
  452. end;
  453. else
  454. internalerror(2002080701);
  455. end;
  456. end;
  457. procedure tcgarm.a_call_name(list : TAsmList;const s : string; weak: boolean);
  458. var
  459. branchopcode: tasmop;
  460. begin
  461. { check not really correct: should only be used for non-Thumb cpus }
  462. if CPUARM_HAS_BLX_LABEL in cpu_capabilities[current_settings.cputype] then
  463. branchopcode:=A_BLX
  464. else
  465. branchopcode:=A_BL;
  466. if target_info.system<>system_arm_darwin then
  467. if not weak then
  468. list.concat(taicpu.op_sym(branchopcode,current_asmdata.RefAsmSymbol(s)))
  469. else
  470. list.concat(taicpu.op_sym(branchopcode,current_asmdata.WeakRefAsmSymbol(s)))
  471. else
  472. list.concat(taicpu.op_sym(branchopcode,get_darwin_call_stub(s,weak)));
  473. {
  474. the compiler does not properly set this flag anymore in pass 1, and
  475. for now we only need it after pass 2 (I hope) (JM)
  476. if not(pi_do_call in current_procinfo.flags) then
  477. internalerror(2003060703);
  478. }
  479. include(current_procinfo.flags,pi_do_call);
  480. end;
  481. procedure tcgarm.a_call_reg(list : TAsmList;reg: tregister);
  482. begin
  483. { check not really correct: should only be used for non-Thumb cpus }
  484. if not(CPUARM_HAS_BLX in cpu_capabilities[current_settings.cputype]) then
  485. begin
  486. list.concat(taicpu.op_reg_reg(A_MOV,NR_R14,NR_PC));
  487. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,reg));
  488. end
  489. else
  490. list.concat(taicpu.op_reg(A_BLX, reg));
  491. {
  492. the compiler does not properly set this flag anymore in pass 1, and
  493. for now we only need it after pass 2 (I hope) (JM)
  494. if not(pi_do_call in current_procinfo.flags) then
  495. internalerror(2003060703);
  496. }
  497. include(current_procinfo.flags,pi_do_call);
  498. end;
  499. procedure tcgarm.a_call_ref(list : TAsmList;ref: treference);
  500. begin
  501. a_reg_alloc(list,NR_R12);
  502. a_load_ref_reg(list,OS_ADDR,OS_ADDR,ref,NR_R12);
  503. a_call_reg(list,NR_R12);
  504. a_reg_dealloc(list,NR_R12);
  505. include(current_procinfo.flags,pi_do_call);
  506. end;
  507. procedure tcgarm.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  508. begin
  509. a_op_const_reg_reg(list,op,size,a,reg,reg);
  510. end;
  511. procedure tcgarm.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  512. var
  513. so : tshifterop;
  514. begin
  515. if op = OP_NEG then
  516. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,src,0))
  517. else if op = OP_NOT then
  518. begin
  519. if size in [OS_8, OS_16, OS_S8, OS_S16] then
  520. begin
  521. shifterop_reset(so);
  522. so.shiftmode:=SM_LSL;
  523. if size in [OS_8, OS_S8] then
  524. so.shiftimm:=24
  525. else
  526. so.shiftimm:=16;
  527. list.concat(taicpu.op_reg_reg_shifterop(A_MVN,dst,src,so));
  528. {Using a shift here allows this to be folded into another instruction}
  529. if size in [OS_S8, OS_S16] then
  530. so.shiftmode:=SM_ASR
  531. else
  532. so.shiftmode:=SM_LSR;
  533. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,dst,so));
  534. end
  535. else
  536. list.concat(taicpu.op_reg_reg(A_MVN,dst,src));
  537. end
  538. else
  539. a_op_reg_reg_reg(list,op,OS_32,src,dst,dst);
  540. end;
  541. const
  542. op_reg_reg_opcg2asmop: array[TOpCG] of tasmop =
  543. (A_NONE,A_MOV,A_ADD,A_AND,A_NONE,A_NONE,A_MUL,A_MUL,A_NONE,A_NONE,A_ORR,
  544. A_NONE,A_NONE,A_NONE,A_SUB,A_EOR,A_NONE,A_NONE);
  545. procedure tcgarm.a_op_const_reg_reg(list: TAsmList; op: TOpCg;
  546. size: tcgsize; a: tcgint; src, dst: tregister);
  547. var
  548. ovloc : tlocation;
  549. begin
  550. a_op_const_reg_reg_checkoverflow(list,op,size,a,src,dst,false,ovloc);
  551. end;
  552. procedure tcgarm.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  553. size: tcgsize; src1, src2, dst: tregister);
  554. var
  555. ovloc : tlocation;
  556. begin
  557. a_op_reg_reg_reg_checkoverflow(list,op,size,src1,src2,dst,false,ovloc);
  558. end;
  559. function opshift2shiftmode(op: TOpCg): tshiftmode;
  560. begin
  561. case op of
  562. OP_SHL: Result:=SM_LSL;
  563. OP_SHR: Result:=SM_LSR;
  564. OP_ROR: Result:=SM_ROR;
  565. OP_ROL: Result:=SM_ROR;
  566. OP_SAR: Result:=SM_ASR;
  567. else internalerror(2012070501);
  568. end
  569. end;
  570. function tcgarm.try_optimized_mul32_const_reg_reg(list: TAsmList; a: tcgint; src, dst: tregister) : boolean;
  571. var
  572. multiplier : dword;
  573. power : longint;
  574. shifterop : tshifterop;
  575. bitsset : byte;
  576. negative : boolean;
  577. first : boolean;
  578. b,
  579. cycles : byte;
  580. maxeffort : byte;
  581. begin
  582. result:=true;
  583. cycles:=0;
  584. negative:=a<0;
  585. shifterop.rs:=NR_NO;
  586. shifterop.shiftmode:=SM_LSL;
  587. if negative then
  588. inc(cycles);
  589. multiplier:=dword(abs(a));
  590. bitsset:=popcnt(multiplier and $fffffffe);
  591. { heuristics to estimate how much instructions are reasonable to replace the mul,
  592. this is currently based on XScale timings }
  593. { in the simplest case, we need a mov to load the constant and a mul to carry out the
  594. actual multiplication, this requires min. 1+4 cycles
  595. because the first shift imm. might cause a stall and because we need more instructions
  596. when replacing the mul we generate max. 3 instructions to replace this mul }
  597. maxeffort:=3;
  598. { if the constant is not a shifter op, we need either some mov/mvn/bic/or sequence or
  599. a ldr, so generating one more operation to replace this is beneficial }
  600. if not(is_shifter_const(dword(a),b)) and not(is_shifter_const(not(dword(a)),b)) then
  601. inc(maxeffort);
  602. { if the upper 5 bits are all set or clear, mul is one cycle faster }
  603. if ((dword(a) and $f8000000)=0) or ((dword(a) and $f8000000)=$f8000000) then
  604. dec(maxeffort);
  605. { if the upper 17 bits are all set or clear, mul is another cycle faster }
  606. if ((dword(a) and $ffff8000)=0) or ((dword(a) and $ffff8000)=$ffff8000) then
  607. dec(maxeffort);
  608. { most simple cases }
  609. if a=1 then
  610. a_load_reg_reg(list,OS_32,OS_32,src,dst)
  611. else if a=0 then
  612. a_load_const_reg(list,OS_32,0,dst)
  613. else if a=-1 then
  614. a_op_reg_reg(list,OP_NEG,OS_32,src,dst)
  615. { add up ?
  616. basically, one add is needed for each bit being set in the constant factor
  617. however, the least significant bit is for free, it can be hidden in the initial
  618. instruction
  619. }
  620. else if (bitsset+cycles<=maxeffort) and
  621. (bitsset<=popcnt(dword(nextpowerof2(multiplier,power)-multiplier) and $fffffffe)) then
  622. begin
  623. first:=true;
  624. while multiplier<>0 do
  625. begin
  626. shifterop.shiftimm:=BsrDWord(multiplier);
  627. if odd(multiplier) then
  628. begin
  629. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,shifterop));
  630. dec(multiplier);
  631. end
  632. else
  633. if first then
  634. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,shifterop))
  635. else
  636. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,dst,src,shifterop));
  637. first:=false;
  638. dec(multiplier,1 shl shifterop.shiftimm);
  639. end;
  640. if negative then
  641. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,dst,0));
  642. end
  643. { subtract from the next greater power of two? }
  644. else if popcnt(dword(nextpowerof2(multiplier,power)-multiplier) and $fffffffe)+cycles+1<=maxeffort then
  645. begin
  646. first:=true;
  647. while multiplier<>0 do
  648. begin
  649. if first then
  650. begin
  651. multiplier:=(1 shl power)-multiplier;
  652. shifterop.shiftimm:=power;
  653. end
  654. else
  655. shifterop.shiftimm:=BsrDWord(multiplier);
  656. if odd(multiplier) then
  657. begin
  658. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,shifterop));
  659. dec(multiplier);
  660. end
  661. else
  662. if first then
  663. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,shifterop))
  664. else
  665. begin
  666. list.concat(taicpu.op_reg_reg_reg_shifterop(A_SUB,dst,dst,src,shifterop));
  667. dec(multiplier,1 shl shifterop.shiftimm);
  668. end;
  669. first:=false;
  670. end;
  671. if negative then
  672. list.concat(taicpu.op_reg_reg_const(A_RSB,dst,dst,0));
  673. end
  674. else
  675. result:=false;
  676. end;
  677. procedure tcgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  678. var
  679. shift : byte;
  680. tmpreg : tregister;
  681. so : tshifterop;
  682. l1 : longint;
  683. imm1, imm2: DWord;
  684. begin
  685. ovloc.loc:=LOC_VOID;
  686. if {$ifopt R+}(a<>-2147483648) and{$endif} is_shifter_const(-a,shift) then
  687. case op of
  688. OP_ADD:
  689. begin
  690. op:=OP_SUB;
  691. a:=aint(dword(-a));
  692. end;
  693. OP_SUB:
  694. begin
  695. op:=OP_ADD;
  696. a:=aint(dword(-a));
  697. end
  698. end;
  699. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  700. case op of
  701. OP_NEG,OP_NOT:
  702. internalerror(200308281);
  703. OP_SHL,
  704. OP_SHR,
  705. OP_ROL,
  706. OP_ROR,
  707. OP_SAR:
  708. begin
  709. if a>32 then
  710. internalerror(200308294);
  711. if a<>0 then
  712. begin
  713. shifterop_reset(so);
  714. so.shiftmode:=opshift2shiftmode(op);
  715. if op = OP_ROL then
  716. so.shiftimm:=32-a
  717. else
  718. so.shiftimm:=a;
  719. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  720. end
  721. else
  722. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  723. end;
  724. else
  725. {if (op in [OP_SUB, OP_ADD]) and
  726. ((a < 0) or
  727. (a > 4095)) then
  728. begin
  729. tmpreg:=getintregister(list,size);
  730. list.concat(taicpu.op_reg_const(A_MOVT, tmpreg, (a shr 16) and $FFFF));
  731. list.concat(taicpu.op_reg_const(A_MOV, tmpreg, a and $FFFF));
  732. list.concat(setoppostfix(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src,tmpreg),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))
  733. ));
  734. end
  735. else}
  736. begin
  737. if cgsetflags or setflags then
  738. a_reg_alloc(list,NR_DEFAULTFLAGS);
  739. list.concat(setoppostfix(
  740. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  741. end;
  742. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  743. begin
  744. ovloc.loc:=LOC_FLAGS;
  745. case op of
  746. OP_ADD:
  747. ovloc.resflags:=F_CS;
  748. OP_SUB:
  749. ovloc.resflags:=F_CC;
  750. end;
  751. end;
  752. end
  753. else
  754. begin
  755. { there could be added some more sophisticated optimizations }
  756. if (op in [OP_MUL,OP_IMUL,OP_DIV,OP_IDIV]) and (a=1) then
  757. a_load_reg_reg(list,size,size,src,dst)
  758. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  759. a_load_const_reg(list,size,0,dst)
  760. else if (op in [OP_IMUL,OP_IDIV]) and (a=-1) then
  761. a_op_reg_reg(list,OP_NEG,size,src,dst)
  762. { we do this here instead in the peephole optimizer because
  763. it saves us a register }
  764. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  765. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  766. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  767. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  768. begin
  769. if l1>32 then{roozbeh does this ever happen?}
  770. internalerror(200308296);
  771. shifterop_reset(so);
  772. so.shiftmode:=SM_LSL;
  773. so.shiftimm:=l1;
  774. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  775. end
  776. { for example : b=a*7 -> b=a*8-a with rsb instruction and shl }
  777. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a+1,l1) and not(cgsetflags or setflags) then
  778. begin
  779. if l1>32 then{does this ever happen?}
  780. internalerror(201205181);
  781. shifterop_reset(so);
  782. so.shiftmode:=SM_LSL;
  783. so.shiftimm:=l1;
  784. list.concat(taicpu.op_reg_reg_reg_shifterop(A_RSB,dst,src,src,so));
  785. end
  786. 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
  787. begin
  788. { nothing to do on success }
  789. end
  790. { x := y and 0; just clears a register, this sometimes gets generated on 64bit ops.
  791. Just using mov x, #0 might allow some easier optimizations down the line. }
  792. else if (op = OP_AND) and (dword(a)=0) then
  793. list.concat(taicpu.op_reg_const(A_MOV,dst,0))
  794. { x := y AND $FFFFFFFF just copies the register, so use mov for better optimizations }
  795. else if (op = OP_AND) and (not(dword(a))=0) then
  796. list.concat(taicpu.op_reg_reg(A_MOV,dst,src))
  797. { BIC clears the specified bits, while AND keeps them, using BIC allows to use a
  798. broader range of shifterconstants.}
  799. else if (op = OP_AND) and is_shifter_const(not(dword(a)),shift) then
  800. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,not(dword(a))))
  801. else if (op = OP_AND) and split_into_shifter_const(not(dword(a)), imm1, imm2) then
  802. begin
  803. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,src,imm1));
  804. list.concat(taicpu.op_reg_reg_const(A_BIC,dst,dst,imm2));
  805. end
  806. else if (op in [OP_ADD, OP_SUB, OP_OR]) and
  807. not(cgsetflags or setflags) and
  808. split_into_shifter_const(a, imm1, imm2) then
  809. begin
  810. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,imm1));
  811. list.concat(taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,dst,imm2));
  812. end
  813. else
  814. begin
  815. tmpreg:=getintregister(list,size);
  816. a_load_const_reg(list,size,a,tmpreg);
  817. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  818. end;
  819. end;
  820. maybeadjustresult(list,op,size,dst);
  821. end;
  822. procedure tcgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  823. var
  824. so : tshifterop;
  825. tmpreg,overflowreg : tregister;
  826. asmop : tasmop;
  827. begin
  828. ovloc.loc:=LOC_VOID;
  829. case op of
  830. OP_NEG,OP_NOT,
  831. OP_DIV,OP_IDIV:
  832. internalerror(200308281);
  833. OP_SHL,
  834. OP_SHR,
  835. OP_SAR,
  836. OP_ROR:
  837. begin
  838. if (op = OP_ROR) and not(size in [OS_32,OS_S32]) then
  839. internalerror(2008072801);
  840. shifterop_reset(so);
  841. so.rs:=src1;
  842. so.shiftmode:=opshift2shiftmode(op);
  843. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  844. end;
  845. OP_ROL:
  846. begin
  847. if not(size in [OS_32,OS_S32]) then
  848. internalerror(2008072801);
  849. { simulate ROL by ror'ing 32-value }
  850. tmpreg:=getintregister(list,OS_32);
  851. list.concat(taicpu.op_reg_reg_const(A_RSB,tmpreg,src1, 32));
  852. shifterop_reset(so);
  853. so.rs:=tmpreg;
  854. so.shiftmode:=SM_ROR;
  855. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src2,so));
  856. end;
  857. OP_IMUL,
  858. OP_MUL:
  859. begin
  860. if cgsetflags or setflags then
  861. begin
  862. overflowreg:=getintregister(list,size);
  863. if op=OP_IMUL then
  864. asmop:=A_SMULL
  865. else
  866. asmop:=A_UMULL;
  867. { the arm doesn't allow that rd and rm are the same }
  868. if dst=src2 then
  869. begin
  870. if dst<>src1 then
  871. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  872. else
  873. begin
  874. tmpreg:=getintregister(list,size);
  875. a_load_reg_reg(list,size,size,src2,dst);
  876. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  877. end;
  878. end
  879. else
  880. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  881. a_reg_alloc(list,NR_DEFAULTFLAGS);
  882. if op=OP_IMUL then
  883. begin
  884. shifterop_reset(so);
  885. so.shiftmode:=SM_ASR;
  886. so.shiftimm:=31;
  887. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  888. end
  889. else
  890. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  891. ovloc.loc:=LOC_FLAGS;
  892. ovloc.resflags:=F_NE;
  893. end
  894. else
  895. begin
  896. { the arm doesn't allow that rd and rm are the same }
  897. if dst=src2 then
  898. begin
  899. if dst<>src1 then
  900. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  901. else
  902. begin
  903. tmpreg:=getintregister(list,size);
  904. a_load_reg_reg(list,size,size,src2,dst);
  905. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  906. end;
  907. end
  908. else
  909. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  910. end;
  911. end;
  912. else
  913. begin
  914. if cgsetflags or setflags then
  915. a_reg_alloc(list,NR_DEFAULTFLAGS);
  916. list.concat(setoppostfix(
  917. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  918. end;
  919. end;
  920. maybeadjustresult(list,op,size,dst);
  921. end;
  922. function tcgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  923. var
  924. tmpreg : tregister;
  925. tmpref : treference;
  926. l : tasmlabel;
  927. begin
  928. tmpreg:=NR_NO;
  929. { Be sure to have a base register }
  930. if (ref.base=NR_NO) then
  931. begin
  932. if ref.shiftmode<>SM_None then
  933. internalerror(200308294);
  934. ref.base:=ref.index;
  935. ref.index:=NR_NO;
  936. end;
  937. { absolute symbols can't be handled directly, we've to store the symbol reference
  938. in the text segment and access it pc relative
  939. For now, we assume that references where base or index equals to PC are already
  940. relative, all other references are assumed to be absolute and thus they need
  941. to be handled extra.
  942. A proper solution would be to change refoptions to a set and store the information
  943. if the symbol is absolute or relative there.
  944. }
  945. if (assigned(ref.symbol) and
  946. not(is_pc(ref.base)) and
  947. not(is_pc(ref.index))
  948. ) or
  949. { [#xxx] isn't a valid address operand }
  950. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  951. (ref.offset<-4095) or
  952. (ref.offset>4095) or
  953. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  954. ((ref.offset<-255) or
  955. (ref.offset>255)
  956. )
  957. ) or
  958. ((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) and
  959. ((ref.offset<-1020) or
  960. (ref.offset>1020) or
  961. ((abs(ref.offset) mod 4)<>0)
  962. )
  963. ) then
  964. begin
  965. fixref(list,ref);
  966. end;
  967. { fold if there is base, index and offset, however, don't fold
  968. for vfp memory instructions because we later fold the index }
  969. if not(op in [A_FLDS,A_FLDD,A_FSTS,A_FSTD]) and
  970. (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  971. begin
  972. if tmpreg<>NR_NO then
  973. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg,tmpreg)
  974. else
  975. begin
  976. tmpreg:=getintregister(list,OS_ADDR);
  977. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg);
  978. ref.base:=tmpreg;
  979. end;
  980. ref.offset:=0;
  981. end;
  982. { floating point operations have only limited references
  983. we expect here, that a base is already set }
  984. if (op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) and (ref.index<>NR_NO) then
  985. begin
  986. if ref.shiftmode<>SM_none then
  987. internalerror(200309121);
  988. if tmpreg<>NR_NO then
  989. begin
  990. if ref.base=tmpreg then
  991. begin
  992. if ref.signindex<0 then
  993. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,tmpreg,ref.index))
  994. else
  995. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,tmpreg,ref.index));
  996. ref.index:=NR_NO;
  997. end
  998. else
  999. begin
  1000. if ref.index<>tmpreg then
  1001. internalerror(200403161);
  1002. if ref.signindex<0 then
  1003. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,ref.base,tmpreg))
  1004. else
  1005. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  1006. ref.base:=tmpreg;
  1007. ref.index:=NR_NO;
  1008. end;
  1009. end
  1010. else
  1011. begin
  1012. tmpreg:=getintregister(list,OS_ADDR);
  1013. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,ref.index));
  1014. ref.base:=tmpreg;
  1015. ref.index:=NR_NO;
  1016. end;
  1017. end;
  1018. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  1019. Result := ref;
  1020. end;
  1021. procedure tcgarm.a_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference);
  1022. var
  1023. oppostfix:toppostfix;
  1024. usedtmpref: treference;
  1025. tmpreg : tregister;
  1026. so : tshifterop;
  1027. dir : integer;
  1028. begin
  1029. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  1030. FromSize := ToSize;
  1031. case ToSize of
  1032. { signed integer registers }
  1033. OS_8,
  1034. OS_S8:
  1035. oppostfix:=PF_B;
  1036. OS_16,
  1037. OS_S16:
  1038. oppostfix:=PF_H;
  1039. OS_32,
  1040. OS_S32,
  1041. { for vfp value stored in integer register }
  1042. OS_F32:
  1043. oppostfix:=PF_None;
  1044. else
  1045. InternalError(200308299);
  1046. end;
  1047. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[tosize]) then
  1048. begin
  1049. if target_info.endian=endian_big then
  1050. dir:=-1
  1051. else
  1052. dir:=1;
  1053. case FromSize of
  1054. OS_16,OS_S16:
  1055. begin
  1056. shifterop_reset(so);so.shiftmode:=SM_LSR;so.shiftimm:=8;
  1057. tmpreg:=getintregister(list,OS_INT);
  1058. usedtmpref:=ref;
  1059. if target_info.endian=endian_big then
  1060. inc(usedtmpref.offset,1);
  1061. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,usedtmpref);
  1062. inc(usedtmpref.offset,dir);
  1063. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,reg,so));
  1064. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1065. end;
  1066. OS_32,OS_S32:
  1067. begin
  1068. tmpreg:=getintregister(list,OS_INT);
  1069. usedtmpref:=ref;
  1070. shifterop_reset(so);so.shiftmode:=SM_LSR;
  1071. if ref.alignment=2 then
  1072. begin
  1073. so.shiftimm:=16;
  1074. if target_info.endian=endian_big then
  1075. inc(usedtmpref.offset,2);
  1076. usedtmpref:=a_internal_load_reg_ref(list,OS_16,OS_16,reg,usedtmpref);
  1077. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,reg,so));
  1078. inc(usedtmpref.offset,dir*2);
  1079. a_internal_load_reg_ref(list,OS_16,OS_16,tmpreg,usedtmpref);
  1080. end
  1081. else
  1082. begin
  1083. so.shiftimm:=8;
  1084. if target_info.endian=endian_big then
  1085. inc(usedtmpref.offset,3);
  1086. usedtmpref:=a_internal_load_reg_ref(list,OS_8,OS_8,reg,usedtmpref);
  1087. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,reg,so));
  1088. inc(usedtmpref.offset,dir);
  1089. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1090. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,tmpreg,so));
  1091. inc(usedtmpref.offset,dir);
  1092. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1093. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,tmpreg,tmpreg,so));
  1094. inc(usedtmpref.offset,dir);
  1095. a_internal_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref);
  1096. end;
  1097. end
  1098. else
  1099. handle_load_store(list,A_STR,oppostfix,reg,ref);
  1100. end;
  1101. end
  1102. else
  1103. handle_load_store(list,A_STR,oppostfix,reg,ref);
  1104. end;
  1105. function tcgarm.a_internal_load_reg_ref(list : TAsmList; fromsize, tosize: tcgsize; reg : tregister;const ref : treference):treference;
  1106. var
  1107. oppostfix:toppostfix;
  1108. begin
  1109. case ToSize of
  1110. { signed integer registers }
  1111. OS_8,
  1112. OS_S8:
  1113. oppostfix:=PF_B;
  1114. OS_16,
  1115. OS_S16:
  1116. oppostfix:=PF_H;
  1117. OS_32,
  1118. OS_S32:
  1119. oppostfix:=PF_None;
  1120. else
  1121. InternalError(2003082910);
  1122. end;
  1123. result:=handle_load_store(list,A_STR,oppostfix,reg,ref);
  1124. end;
  1125. function tcgarm.a_internal_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister):treference;
  1126. var
  1127. oppostfix:toppostfix;
  1128. begin
  1129. case FromSize of
  1130. { signed integer registers }
  1131. OS_8:
  1132. oppostfix:=PF_B;
  1133. OS_S8:
  1134. oppostfix:=PF_SB;
  1135. OS_16:
  1136. oppostfix:=PF_H;
  1137. OS_S16:
  1138. oppostfix:=PF_SH;
  1139. OS_32,
  1140. OS_S32:
  1141. oppostfix:=PF_None;
  1142. else
  1143. InternalError(200308291);
  1144. end;
  1145. result:=handle_load_store(list,A_LDR,oppostfix,reg,ref);
  1146. end;
  1147. procedure tcgarm.a_load_reg_reg(list : TAsmList; fromsize, tosize : tcgsize;reg1,reg2 : tregister);
  1148. var
  1149. so : tshifterop;
  1150. procedure do_shift(shiftmode : tshiftmode; shiftimm : byte; reg : tregister);
  1151. begin
  1152. so.shiftmode:=shiftmode;
  1153. so.shiftimm:=shiftimm;
  1154. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,reg2,reg,so));
  1155. end;
  1156. var
  1157. instr: taicpu;
  1158. conv_done: boolean;
  1159. begin
  1160. if (tcgsize2size[fromsize]>32) or (tcgsize2size[tosize]>32) or (fromsize=OS_NO) or (tosize=OS_NO) then
  1161. internalerror(2002090901);
  1162. conv_done:=false;
  1163. if tosize<>fromsize then
  1164. begin
  1165. shifterop_reset(so);
  1166. conv_done:=true;
  1167. if tcgsize2size[tosize]<=tcgsize2size[fromsize] then
  1168. fromsize:=tosize;
  1169. if current_settings.cputype<cpu_armv6 then
  1170. case fromsize of
  1171. OS_8:
  1172. list.concat(taicpu.op_reg_reg_const(A_AND,reg2,reg1,$ff));
  1173. OS_S8:
  1174. begin
  1175. do_shift(SM_LSL,24,reg1);
  1176. if tosize=OS_16 then
  1177. begin
  1178. do_shift(SM_ASR,8,reg2);
  1179. do_shift(SM_LSR,16,reg2);
  1180. end
  1181. else
  1182. do_shift(SM_ASR,24,reg2);
  1183. end;
  1184. OS_16:
  1185. begin
  1186. do_shift(SM_LSL,16,reg1);
  1187. do_shift(SM_LSR,16,reg2);
  1188. end;
  1189. OS_S16:
  1190. begin
  1191. do_shift(SM_LSL,16,reg1);
  1192. do_shift(SM_ASR,16,reg2)
  1193. end;
  1194. else
  1195. conv_done:=false;
  1196. end
  1197. else
  1198. case fromsize of
  1199. OS_8:
  1200. list.concat(taicpu.op_reg_reg_const(A_AND,reg2,reg1,$ff));
  1201. OS_S8:
  1202. begin
  1203. if tosize=OS_16 then
  1204. begin
  1205. so.shiftmode:=SM_ROR;
  1206. so.shiftimm:=16;
  1207. list.concat(taicpu.op_reg_reg_shifterop(A_SXTB16,reg2,reg1,so));
  1208. do_shift(SM_LSR,16,reg2);
  1209. end
  1210. else
  1211. list.concat(taicpu.op_reg_reg(A_SXTB,reg2,reg1));
  1212. end;
  1213. OS_16:
  1214. list.concat(taicpu.op_reg_reg(A_UXTH,reg2,reg1));
  1215. OS_S16:
  1216. list.concat(taicpu.op_reg_reg(A_SXTH,reg2,reg1));
  1217. else
  1218. conv_done:=false;
  1219. end
  1220. end;
  1221. if not conv_done and (reg1<>reg2) then
  1222. begin
  1223. { same size, only a register mov required }
  1224. instr:=taicpu.op_reg_reg(A_MOV,reg2,reg1);
  1225. list.Concat(instr);
  1226. { Notify the register allocator that we have written a move instruction so
  1227. it can try to eliminate it. }
  1228. add_move_instruction(instr);
  1229. end;
  1230. end;
  1231. procedure tcgarm.a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const paraloc : TCGPara);
  1232. var
  1233. href,href2 : treference;
  1234. hloc : pcgparalocation;
  1235. begin
  1236. href:=ref;
  1237. hloc:=paraloc.location;
  1238. while assigned(hloc) do
  1239. begin
  1240. case hloc^.loc of
  1241. LOC_FPUREGISTER,LOC_CFPUREGISTER:
  1242. begin
  1243. paramanager.allocparaloc(list,paraloc.location);
  1244. a_loadfpu_ref_reg(list,size,size,ref,hloc^.register);
  1245. end;
  1246. LOC_REGISTER :
  1247. case hloc^.size of
  1248. OS_32,
  1249. OS_F32:
  1250. begin
  1251. paramanager.allocparaloc(list,paraloc.location);
  1252. a_load_ref_reg(list,OS_32,OS_32,href,hloc^.register);
  1253. end;
  1254. OS_64,
  1255. OS_F64:
  1256. cg64.a_load64_ref_cgpara(list,href,paraloc);
  1257. else
  1258. a_load_ref_reg(list,hloc^.size,hloc^.size,href,hloc^.register);
  1259. end;
  1260. LOC_REFERENCE :
  1261. begin
  1262. reference_reset_base(href2,hloc^.reference.index,hloc^.reference.offset,paraloc.alignment);
  1263. { concatcopy should choose the best way to copy the data }
  1264. g_concatcopy(list,href,href2,tcgsize2size[hloc^.size]);
  1265. end;
  1266. else
  1267. internalerror(200408241);
  1268. end;
  1269. inc(href.offset,tcgsize2size[hloc^.size]);
  1270. hloc:=hloc^.next;
  1271. end;
  1272. end;
  1273. procedure tcgarm.a_loadfpu_reg_reg(list: TAsmList; fromsize,tosize: tcgsize; reg1, reg2: tregister);
  1274. begin
  1275. list.concat(setoppostfix(taicpu.op_reg_reg(A_MVF,reg2,reg1),cgsize2fpuoppostfix[tosize]));
  1276. end;
  1277. procedure tcgarm.a_loadfpu_ref_reg(list: TAsmList; fromsize,tosize: tcgsize; const ref: treference; reg: tregister);
  1278. var
  1279. oppostfix:toppostfix;
  1280. begin
  1281. case fromsize of
  1282. OS_32,
  1283. OS_F32:
  1284. oppostfix:=PF_S;
  1285. OS_64,
  1286. OS_F64:
  1287. oppostfix:=PF_D;
  1288. OS_F80:
  1289. oppostfix:=PF_E;
  1290. else
  1291. InternalError(200309021);
  1292. end;
  1293. handle_load_store(list,A_LDF,oppostfix,reg,ref);
  1294. if fromsize<>tosize then
  1295. a_loadfpu_reg_reg(list,fromsize,tosize,reg,reg);
  1296. end;
  1297. procedure tcgarm.a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference);
  1298. var
  1299. oppostfix:toppostfix;
  1300. begin
  1301. case tosize of
  1302. OS_F32:
  1303. oppostfix:=PF_S;
  1304. OS_F64:
  1305. oppostfix:=PF_D;
  1306. OS_F80:
  1307. oppostfix:=PF_E;
  1308. else
  1309. InternalError(200309022);
  1310. end;
  1311. handle_load_store(list,A_STF,oppostfix,reg,ref);
  1312. end;
  1313. { comparison operations }
  1314. procedure tcgarm.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1315. l : tasmlabel);
  1316. var
  1317. tmpreg : tregister;
  1318. b : byte;
  1319. begin
  1320. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1321. if is_shifter_const(a,b) then
  1322. list.concat(taicpu.op_reg_const(A_CMP,reg,a))
  1323. { CMN reg,0 and CMN reg,$80000000 are different from CMP reg,$ffffffff
  1324. and CMP reg,$7fffffff regarding the flags according to the ARM manual }
  1325. else if (a<>$7fffffff) and (a<>-1) and is_shifter_const(-a,b) then
  1326. list.concat(taicpu.op_reg_const(A_CMN,reg,-a))
  1327. else
  1328. begin
  1329. tmpreg:=getintregister(list,size);
  1330. a_load_const_reg(list,size,a,tmpreg);
  1331. list.concat(taicpu.op_reg_reg(A_CMP,reg,tmpreg));
  1332. end;
  1333. a_jmp_cond(list,cmp_op,l);
  1334. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1335. end;
  1336. procedure tcgarm.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: TCGSize; src, dst: TRegister);
  1337. begin
  1338. if reverse then
  1339. begin
  1340. list.Concat(taicpu.op_reg_reg(A_CLZ,dst,src));
  1341. list.Concat(taicpu.op_reg_reg_const(A_RSB,dst,dst,31));
  1342. list.Concat(taicpu.op_reg_reg_const(A_AND,dst,dst,255));
  1343. end
  1344. else
  1345. internalerror(201209041);
  1346. end;
  1347. procedure tcgarm.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel);
  1348. begin
  1349. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1350. list.concat(taicpu.op_reg_reg(A_CMP,reg2,reg1));
  1351. a_jmp_cond(list,cmp_op,l);
  1352. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1353. end;
  1354. procedure tcgarm.a_jmp_name(list : TAsmList;const s : string);
  1355. var
  1356. ai : taicpu;
  1357. begin
  1358. ai:=taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(s));
  1359. ai.is_jmp:=true;
  1360. list.concat(ai);
  1361. end;
  1362. procedure tcgarm.a_jmp_always(list : TAsmList;l: tasmlabel);
  1363. var
  1364. ai : taicpu;
  1365. begin
  1366. ai:=taicpu.op_sym(A_B,l);
  1367. ai.is_jmp:=true;
  1368. list.concat(ai);
  1369. end;
  1370. procedure tcgarm.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1371. var
  1372. ai : taicpu;
  1373. begin
  1374. ai:=setcondition(taicpu.op_sym(A_B,l),flags_to_cond(f));
  1375. ai.is_jmp:=true;
  1376. list.concat(ai);
  1377. end;
  1378. procedure tcgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  1379. begin
  1380. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  1381. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  1382. end;
  1383. procedure tcgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1384. var
  1385. ref : treference;
  1386. shift : byte;
  1387. firstfloatreg,lastfloatreg,
  1388. r : byte;
  1389. mmregs,
  1390. regs, saveregs : tcpuregisterset;
  1391. r7offset,
  1392. stackmisalignment : pint;
  1393. postfix: toppostfix;
  1394. imm1, imm2: DWord;
  1395. begin
  1396. LocalSize:=align(LocalSize,4);
  1397. { call instruction does not put anything on the stack }
  1398. stackmisalignment:=0;
  1399. if not(nostackframe) then
  1400. begin
  1401. firstfloatreg:=RS_NO;
  1402. mmregs:=[];
  1403. case current_settings.fputype of
  1404. fpu_fpa,
  1405. fpu_fpa10,
  1406. fpu_fpa11:
  1407. begin
  1408. { save floating point registers? }
  1409. regs:=rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  1410. for r:=RS_F0 to RS_F7 do
  1411. if r in regs then
  1412. begin
  1413. if firstfloatreg=RS_NO then
  1414. firstfloatreg:=r;
  1415. lastfloatreg:=r;
  1416. inc(stackmisalignment,12);
  1417. end;
  1418. end;
  1419. fpu_vfpv2,
  1420. fpu_vfpv3,
  1421. fpu_vfpv3_d16:
  1422. begin;
  1423. mmregs:=rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall);
  1424. end;
  1425. end;
  1426. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1427. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1428. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  1429. { save int registers }
  1430. reference_reset(ref,4);
  1431. ref.index:=NR_STACK_POINTER_REG;
  1432. ref.addressmode:=AM_PREINDEXED;
  1433. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  1434. if not(target_info.system in systems_darwin) then
  1435. begin
  1436. a_reg_alloc(list,NR_STACK_POINTER_REG);
  1437. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1438. begin
  1439. a_reg_alloc(list,NR_R12);
  1440. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  1441. end;
  1442. { the (old) ARM APCS requires saving both the stack pointer (to
  1443. crawl the stack) and the PC (to identify the function this
  1444. stack frame belongs to) -> also save R12 (= copy of R13 on entry)
  1445. and R15 -- still needs updating for EABI and Darwin, they don't
  1446. need that }
  1447. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1448. regs:=regs+[RS_FRAME_POINTER_REG,RS_R12,RS_R14,RS_R15]
  1449. else
  1450. if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  1451. include(regs,RS_R14);
  1452. if regs<>[] then
  1453. begin
  1454. for r:=RS_R0 to RS_R15 do
  1455. if r in regs then
  1456. inc(stackmisalignment,4);
  1457. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  1458. end;
  1459. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  1460. begin
  1461. { the framepointer now points to the saved R15, so the saved
  1462. framepointer is at R11-12 (for get_caller_frame) }
  1463. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  1464. a_reg_dealloc(list,NR_R12);
  1465. end;
  1466. end
  1467. else
  1468. begin
  1469. { always save r14 if we use r7 as the framepointer, because
  1470. the parameter offsets are hardcoded in advance and always
  1471. assume that r14 sits on the stack right behind the saved r7
  1472. }
  1473. if current_procinfo.framepointer=NR_FRAME_POINTER_REG then
  1474. include(regs,RS_FRAME_POINTER_REG);
  1475. if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  1476. include(regs,RS_R14);
  1477. if regs<>[] then
  1478. begin
  1479. { on Darwin, you first have to save [r4-r7,lr], and then
  1480. [r8,r10,r11] and make r7 point to the previously saved
  1481. r7 so that you can perform a stack crawl based on it
  1482. ([r7] is previous stack frame, [r7+4] is return address
  1483. }
  1484. include(regs,RS_FRAME_POINTER_REG);
  1485. saveregs:=regs-[RS_R8,RS_R10,RS_R11];
  1486. r7offset:=0;
  1487. for r:=RS_R0 to RS_R15 do
  1488. if r in saveregs then
  1489. begin
  1490. inc(stackmisalignment,4);
  1491. if r<RS_FRAME_POINTER_REG then
  1492. inc(r7offset,4);
  1493. end;
  1494. { save the registers }
  1495. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  1496. { make r7 point to the saved r7 (regardless of whether this
  1497. frame uses the framepointer, for backtrace purposes) }
  1498. if r7offset<>0 then
  1499. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_FRAME_POINTER_REG,NR_R13,r7offset))
  1500. else
  1501. list.concat(taicpu.op_reg_reg(A_MOV,NR_R7,NR_R13));
  1502. { now save the rest (if any) }
  1503. saveregs:=regs-saveregs;
  1504. if saveregs<>[] then
  1505. begin
  1506. for r:=RS_R8 to RS_R11 do
  1507. if r in saveregs then
  1508. inc(stackmisalignment,4);
  1509. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  1510. end;
  1511. end;
  1512. end;
  1513. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  1514. if (LocalSize<>0) or
  1515. ((stackmisalignment<>0) and
  1516. ((pi_do_call in current_procinfo.flags) or
  1517. (po_assembler in current_procinfo.procdef.procoptions))) then
  1518. begin
  1519. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  1520. if is_shifter_const(localsize,shift) then
  1521. begin
  1522. a_reg_dealloc(list,NR_R12);
  1523. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  1524. end
  1525. else if split_into_shifter_const(localsize, imm1, imm2) then
  1526. begin
  1527. a_reg_dealloc(list,NR_R12);
  1528. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm1));
  1529. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm2));
  1530. end
  1531. else
  1532. begin
  1533. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1534. a_reg_alloc(list,NR_R12);
  1535. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  1536. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1537. a_reg_dealloc(list,NR_R12);
  1538. end;
  1539. end;
  1540. if (mmregs<>[]) or
  1541. (firstfloatreg<>RS_NO) then
  1542. begin
  1543. reference_reset(ref,4);
  1544. if (tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023) or
  1545. (current_settings.fputype in [fpu_vfpv2,fpu_vfpv3,fpu_vfpv3_d16]) then
  1546. begin
  1547. if not is_shifter_const(tarmprocinfo(current_procinfo).floatregstart,shift) then
  1548. begin
  1549. a_reg_alloc(list,NR_R12);
  1550. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1551. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  1552. a_reg_dealloc(list,NR_R12);
  1553. end
  1554. else
  1555. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_R12,current_procinfo.framepointer,-tarmprocinfo(current_procinfo).floatregstart));
  1556. ref.base:=NR_R12;
  1557. end
  1558. else
  1559. begin
  1560. ref.base:=current_procinfo.framepointer;
  1561. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1562. end;
  1563. case current_settings.fputype of
  1564. fpu_fpa,
  1565. fpu_fpa10,
  1566. fpu_fpa11:
  1567. begin
  1568. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1569. lastfloatreg-firstfloatreg+1,ref));
  1570. end;
  1571. fpu_vfpv2,
  1572. fpu_vfpv3,
  1573. fpu_vfpv3_d16:
  1574. begin
  1575. ref.index:=ref.base;
  1576. ref.base:=NR_NO;
  1577. { FSTMX is deprecated on ARMv6 and later }
  1578. if (current_settings.cputype<cpu_armv6) then
  1579. postfix:=PF_IAX
  1580. else
  1581. postfix:=PF_IAD;
  1582. list.concat(setoppostfix(taicpu.op_ref_regset(A_FSTM,ref,R_MMREGISTER,R_SUBFD,mmregs),postfix));
  1583. end;
  1584. end;
  1585. end;
  1586. end;
  1587. end;
  1588. procedure tcgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  1589. var
  1590. ref : treference;
  1591. LocalSize : longint;
  1592. firstfloatreg,lastfloatreg,
  1593. r,
  1594. shift : byte;
  1595. mmregs,
  1596. saveregs,
  1597. regs : tcpuregisterset;
  1598. stackmisalignment: pint;
  1599. mmpostfix: toppostfix;
  1600. imm1, imm2: DWord;
  1601. begin
  1602. if not(nostackframe) then
  1603. begin
  1604. stackmisalignment:=0;
  1605. firstfloatreg:=RS_NO;
  1606. mmregs:=[];
  1607. case current_settings.fputype of
  1608. fpu_fpa,
  1609. fpu_fpa10,
  1610. fpu_fpa11:
  1611. begin
  1612. { restore floating point registers? }
  1613. regs:=rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall);
  1614. for r:=RS_F0 to RS_F7 do
  1615. if r in regs then
  1616. begin
  1617. if firstfloatreg=RS_NO then
  1618. firstfloatreg:=r;
  1619. lastfloatreg:=r;
  1620. { floating point register space is already included in
  1621. localsize below by calc_stackframe_size
  1622. inc(stackmisalignment,12);
  1623. }
  1624. end;
  1625. end;
  1626. fpu_vfpv2,
  1627. fpu_vfpv3,
  1628. fpu_vfpv3_d16:
  1629. begin;
  1630. { restore vfp registers? }
  1631. mmregs:=rg[R_MMREGISTER].used_in_proc-paramanager.get_volatile_registers_mm(pocall_stdcall);
  1632. end;
  1633. end;
  1634. if (firstfloatreg<>RS_NO) or
  1635. (mmregs<>[]) then
  1636. begin
  1637. reference_reset(ref,4);
  1638. if (tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023) or
  1639. (current_settings.fputype in [fpu_vfpv2,fpu_vfpv3,fpu_vfpv3_d16]) then
  1640. begin
  1641. if not is_shifter_const(tarmprocinfo(current_procinfo).floatregstart,shift) then
  1642. begin
  1643. a_reg_alloc(list,NR_R12);
  1644. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  1645. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  1646. a_reg_dealloc(list,NR_R12);
  1647. end
  1648. else
  1649. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_R12,current_procinfo.framepointer,-tarmprocinfo(current_procinfo).floatregstart));
  1650. ref.base:=NR_R12;
  1651. end
  1652. else
  1653. begin
  1654. ref.base:=current_procinfo.framepointer;
  1655. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  1656. end;
  1657. case current_settings.fputype of
  1658. fpu_fpa,
  1659. fpu_fpa10,
  1660. fpu_fpa11:
  1661. begin
  1662. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  1663. lastfloatreg-firstfloatreg+1,ref));
  1664. end;
  1665. fpu_vfpv2,
  1666. fpu_vfpv3,
  1667. fpu_vfpv3_d16:
  1668. begin
  1669. ref.index:=ref.base;
  1670. ref.base:=NR_NO;
  1671. { FLDMX is deprecated on ARMv6 and later }
  1672. if (current_settings.cputype<cpu_armv6) then
  1673. mmpostfix:=PF_IAX
  1674. else
  1675. mmpostfix:=PF_IAD;
  1676. list.concat(setoppostfix(taicpu.op_ref_regset(A_FLDM,ref,R_MMREGISTER,R_SUBFD,mmregs),mmpostfix));
  1677. end;
  1678. end;
  1679. end;
  1680. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall) ;
  1681. if (pi_do_call in current_procinfo.flags) or
  1682. (regs<>[]) or
  1683. ((target_info.system in systems_darwin) and
  1684. (current_procinfo.framepointer<>NR_STACK_POINTER_REG)) then
  1685. begin
  1686. exclude(regs,RS_R14);
  1687. include(regs,RS_R15);
  1688. if (target_info.system in systems_darwin) then
  1689. include(regs,RS_FRAME_POINTER_REG);
  1690. end;
  1691. if not(target_info.system in systems_darwin) then
  1692. begin
  1693. { restore saved stack pointer to SP (R13) and saved lr to PC (R15).
  1694. The saved PC came after that but is discarded, since we restore
  1695. the stack pointer }
  1696. if (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  1697. regs:=regs+[RS_FRAME_POINTER_REG,RS_R13,RS_R15];
  1698. end
  1699. else
  1700. begin
  1701. { restore R8-R11 already if necessary (they've been stored
  1702. before the others) }
  1703. saveregs:=regs*[RS_R8,RS_R10,RS_R11];
  1704. if saveregs<>[] then
  1705. begin
  1706. reference_reset(ref,4);
  1707. ref.index:=NR_STACK_POINTER_REG;
  1708. ref.addressmode:=AM_PREINDEXED;
  1709. for r:=RS_R8 to RS_R11 do
  1710. if r in saveregs then
  1711. inc(stackmisalignment,4);
  1712. regs:=regs-saveregs;
  1713. end;
  1714. end;
  1715. for r:=RS_R0 to RS_R15 do
  1716. if r in regs then
  1717. inc(stackmisalignment,4);
  1718. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  1719. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) or
  1720. (target_info.system in systems_darwin) then
  1721. begin
  1722. LocalSize:=current_procinfo.calc_stackframe_size;
  1723. if (LocalSize<>0) or
  1724. ((stackmisalignment<>0) and
  1725. ((pi_do_call in current_procinfo.flags) or
  1726. (po_assembler in current_procinfo.procdef.procoptions))) then
  1727. begin
  1728. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  1729. if is_shifter_const(LocalSize,shift) then
  1730. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize))
  1731. else if split_into_shifter_const(localsize, imm1, imm2) then
  1732. begin
  1733. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm1));
  1734. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,imm2));
  1735. end
  1736. else
  1737. begin
  1738. a_reg_alloc(list,NR_R12);
  1739. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  1740. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  1741. a_reg_dealloc(list,NR_R12);
  1742. end;
  1743. end;
  1744. if (target_info.system in systems_darwin) and
  1745. (saveregs<>[]) then
  1746. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,saveregs),PF_FD));
  1747. if regs=[] then
  1748. begin
  1749. if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  1750. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  1751. else
  1752. list.concat(taicpu.op_reg(A_BX,NR_R14))
  1753. end
  1754. else
  1755. begin
  1756. reference_reset(ref,4);
  1757. ref.index:=NR_STACK_POINTER_REG;
  1758. ref.addressmode:=AM_PREINDEXED;
  1759. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  1760. end;
  1761. end
  1762. else
  1763. begin
  1764. { restore int registers and return }
  1765. reference_reset(ref,4);
  1766. ref.index:=NR_FRAME_POINTER_REG;
  1767. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_EA));
  1768. end;
  1769. end
  1770. else if not(CPUARM_HAS_BX in cpu_capabilities[current_settings.cputype]) then
  1771. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14))
  1772. else
  1773. list.concat(taicpu.op_reg(A_BX,NR_R14))
  1774. end;
  1775. procedure tcgarm.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  1776. var
  1777. b : byte;
  1778. tmpref : treference;
  1779. instr : taicpu;
  1780. begin
  1781. if ref.addressmode<>AM_OFFSET then
  1782. internalerror(200309071);
  1783. tmpref:=ref;
  1784. { Be sure to have a base register }
  1785. if (tmpref.base=NR_NO) then
  1786. begin
  1787. if tmpref.shiftmode<>SM_None then
  1788. internalerror(200308294);
  1789. if tmpref.signindex<0 then
  1790. internalerror(200312023);
  1791. tmpref.base:=tmpref.index;
  1792. tmpref.index:=NR_NO;
  1793. end;
  1794. if assigned(tmpref.symbol) or
  1795. not((is_shifter_const(tmpref.offset,b)) or
  1796. (is_shifter_const(-tmpref.offset,b))
  1797. ) then
  1798. fixref(list,tmpref);
  1799. { expect a base here if there is an index }
  1800. if (tmpref.base=NR_NO) and (tmpref.index<>NR_NO) then
  1801. internalerror(200312022);
  1802. if tmpref.index<>NR_NO then
  1803. begin
  1804. if tmpref.shiftmode<>SM_None then
  1805. internalerror(200312021);
  1806. if tmpref.signindex<0 then
  1807. a_op_reg_reg_reg(list,OP_SUB,OS_ADDR,tmpref.base,tmpref.index,r)
  1808. else
  1809. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,tmpref.base,tmpref.index,r);
  1810. if tmpref.offset<>0 then
  1811. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,r,r);
  1812. end
  1813. else
  1814. begin
  1815. if tmpref.base=NR_NO then
  1816. a_load_const_reg(list,OS_ADDR,tmpref.offset,r)
  1817. else
  1818. if tmpref.offset<>0 then
  1819. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,tmpref.offset,tmpref.base,r)
  1820. else
  1821. begin
  1822. instr:=taicpu.op_reg_reg(A_MOV,r,tmpref.base);
  1823. list.concat(instr);
  1824. add_move_instruction(instr);
  1825. end;
  1826. end;
  1827. end;
  1828. procedure tcgarm.fixref(list : TAsmList;var ref : treference);
  1829. var
  1830. tmpreg : tregister;
  1831. tmpref : treference;
  1832. l : tasmlabel;
  1833. indirection_done : boolean;
  1834. begin
  1835. { absolute symbols can't be handled directly, we've to store the symbol reference
  1836. in the text segment and access it pc relative
  1837. For now, we assume that references where base or index equals to PC are already
  1838. relative, all other references are assumed to be absolute and thus they need
  1839. to be handled extra.
  1840. A proper solution would be to change refoptions to a set and store the information
  1841. if the symbol is absolute or relative there.
  1842. }
  1843. { create consts entry }
  1844. reference_reset(tmpref,4);
  1845. current_asmdata.getjumplabel(l);
  1846. cg.a_label(current_procinfo.aktlocaldata,l);
  1847. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  1848. indirection_done:=false;
  1849. if assigned(ref.symbol) then
  1850. begin
  1851. if (target_info.system=system_arm_darwin) and
  1852. (ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN,AB_COMMON]) then
  1853. begin
  1854. tmpreg:=g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol));
  1855. if ref.offset<>0 then
  1856. a_op_const_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg);
  1857. indirection_done:=true;
  1858. end
  1859. else
  1860. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset))
  1861. end
  1862. else
  1863. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(ref.offset));
  1864. { load consts entry }
  1865. if not indirection_done then
  1866. begin
  1867. tmpreg:=getintregister(list,OS_INT);
  1868. tmpref.symbol:=l;
  1869. tmpref.base:=NR_PC;
  1870. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  1871. end;
  1872. { This routine can be called with PC as base/index in case the offset
  1873. was too large to encode in a load/store. In that case, the entire
  1874. absolute expression has been re-encoded in a new constpool entry, and
  1875. we have to remove the use of PC from the original reference (the code
  1876. above made everything relative to the value loaded from the new
  1877. constpool entry) }
  1878. if is_pc(ref.base) then
  1879. ref.base:=NR_NO;
  1880. if is_pc(ref.index) then
  1881. ref.index:=NR_NO;
  1882. if (ref.base<>NR_NO) then
  1883. begin
  1884. if ref.index<>NR_NO then
  1885. begin
  1886. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  1887. ref.base:=tmpreg;
  1888. end
  1889. else
  1890. if ref.base<>NR_PC then
  1891. begin
  1892. ref.index:=tmpreg;
  1893. ref.shiftimm:=0;
  1894. ref.signindex:=1;
  1895. ref.shiftmode:=SM_None;
  1896. end
  1897. else
  1898. ref.base:=tmpreg;
  1899. end
  1900. else
  1901. ref.base:=tmpreg;
  1902. ref.offset:=0;
  1903. ref.symbol:=nil;
  1904. end;
  1905. procedure tcgarm.g_concatcopy_move(list : TAsmList;const source,dest : treference;len : tcgint);
  1906. var
  1907. paraloc1,paraloc2,paraloc3 : TCGPara;
  1908. begin
  1909. paraloc1.init;
  1910. paraloc2.init;
  1911. paraloc3.init;
  1912. paramanager.getintparaloc(pocall_default,1,voidpointertype,paraloc1);
  1913. paramanager.getintparaloc(pocall_default,2,voidpointertype,paraloc2);
  1914. paramanager.getintparaloc(pocall_default,3,ptrsinttype,paraloc3);
  1915. a_load_const_cgpara(list,OS_SINT,len,paraloc3);
  1916. a_loadaddr_ref_cgpara(list,dest,paraloc2);
  1917. a_loadaddr_ref_cgpara(list,source,paraloc1);
  1918. paramanager.freecgpara(list,paraloc3);
  1919. paramanager.freecgpara(list,paraloc2);
  1920. paramanager.freecgpara(list,paraloc1);
  1921. alloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1922. alloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1923. a_call_name(list,'FPC_MOVE',false);
  1924. dealloccpuregisters(list,R_FPUREGISTER,paramanager.get_volatile_registers_fpu(pocall_default));
  1925. dealloccpuregisters(list,R_INTREGISTER,paramanager.get_volatile_registers_int(pocall_default));
  1926. paraloc3.done;
  1927. paraloc2.done;
  1928. paraloc1.done;
  1929. end;
  1930. procedure tcgarm.g_concatcopy_internal(list : TAsmList;const source,dest : treference;len : tcgint;aligned : boolean);
  1931. const
  1932. maxtmpreg=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}
  1933. var
  1934. srcref,dstref,usedtmpref,usedtmpref2:treference;
  1935. srcreg,destreg,countreg,r,tmpreg:tregister;
  1936. helpsize:aint;
  1937. copysize:byte;
  1938. cgsize:Tcgsize;
  1939. tmpregisters:array[1..maxtmpreg] of tregister;
  1940. tmpregi,tmpregi2:byte;
  1941. { will never be called with count<=4 }
  1942. procedure genloop(count : aword;size : byte);
  1943. const
  1944. size2opsize : array[1..4] of tcgsize = (OS_8,OS_16,OS_NO,OS_32);
  1945. var
  1946. l : tasmlabel;
  1947. begin
  1948. current_asmdata.getjumplabel(l);
  1949. if count<size then size:=1;
  1950. a_load_const_reg(list,OS_INT,count div size,countreg);
  1951. cg.a_label(list,l);
  1952. srcref.addressmode:=AM_POSTINDEXED;
  1953. dstref.addressmode:=AM_POSTINDEXED;
  1954. srcref.offset:=size;
  1955. dstref.offset:=size;
  1956. r:=getintregister(list,size2opsize[size]);
  1957. a_load_ref_reg(list,size2opsize[size],size2opsize[size],srcref,r);
  1958. a_reg_alloc(list,NR_DEFAULTFLAGS);
  1959. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,countreg,countreg,1),PF_S));
  1960. a_load_reg_ref(list,size2opsize[size],size2opsize[size],r,dstref);
  1961. a_jmp_flags(list,F_NE,l);
  1962. a_reg_dealloc(list,NR_DEFAULTFLAGS);
  1963. srcref.offset:=1;
  1964. dstref.offset:=1;
  1965. case count mod size of
  1966. 1:
  1967. begin
  1968. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1969. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1970. end;
  1971. 2:
  1972. if aligned then
  1973. begin
  1974. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  1975. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  1976. end
  1977. else
  1978. begin
  1979. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1980. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1981. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1982. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1983. end;
  1984. 3:
  1985. if aligned then
  1986. begin
  1987. srcref.offset:=2;
  1988. dstref.offset:=2;
  1989. a_load_ref_reg(list,OS_16,OS_16,srcref,r);
  1990. a_load_reg_ref(list,OS_16,OS_16,r,dstref);
  1991. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1992. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1993. end
  1994. else
  1995. begin
  1996. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1997. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  1998. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  1999. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2000. a_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2001. a_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2002. end;
  2003. end;
  2004. { keep the registers alive }
  2005. list.concat(taicpu.op_reg_reg(A_MOV,countreg,countreg));
  2006. list.concat(taicpu.op_reg_reg(A_MOV,srcreg,srcreg));
  2007. list.concat(taicpu.op_reg_reg(A_MOV,destreg,destreg));
  2008. end;
  2009. begin
  2010. if len=0 then
  2011. exit;
  2012. helpsize:=12+maxtmpreg*4;//52 with maxtmpreg=10
  2013. dstref:=dest;
  2014. srcref:=source;
  2015. if cs_opt_size in current_settings.optimizerswitches then
  2016. helpsize:=8;
  2017. if aligned and (len=4) then
  2018. begin
  2019. tmpreg:=getintregister(list,OS_32);
  2020. a_load_ref_reg(list,OS_32,OS_32,source,tmpreg);
  2021. a_load_reg_ref(list,OS_32,OS_32,tmpreg,dest);
  2022. end
  2023. else if (len<=helpsize) and aligned then
  2024. begin
  2025. tmpregi:=0;
  2026. srcreg:=getintregister(list,OS_ADDR);
  2027. { explicit pc relative addressing, could be
  2028. e.g. a floating point constant }
  2029. if source.base=NR_PC then
  2030. begin
  2031. { ... then we don't need a loadaddr }
  2032. srcref:=source;
  2033. end
  2034. else
  2035. begin
  2036. a_loadaddr_ref_reg(list,source,srcreg);
  2037. reference_reset_base(srcref,srcreg,0,source.alignment);
  2038. end;
  2039. while (len div 4 <> 0) and (tmpregi<maxtmpreg) do
  2040. begin
  2041. inc(tmpregi);
  2042. tmpregisters[tmpregi]:=getintregister(list,OS_32);
  2043. a_load_ref_reg(list,OS_32,OS_32,srcref,tmpregisters[tmpregi]);
  2044. inc(srcref.offset,4);
  2045. dec(len,4);
  2046. end;
  2047. destreg:=getintregister(list,OS_ADDR);
  2048. a_loadaddr_ref_reg(list,dest,destreg);
  2049. reference_reset_base(dstref,destreg,0,dest.alignment);
  2050. tmpregi2:=1;
  2051. while (tmpregi2<=tmpregi) do
  2052. begin
  2053. a_load_reg_ref(list,OS_32,OS_32,tmpregisters[tmpregi2],dstref);
  2054. inc(dstref.offset,4);
  2055. inc(tmpregi2);
  2056. end;
  2057. copysize:=4;
  2058. cgsize:=OS_32;
  2059. while len<>0 do
  2060. begin
  2061. if len<2 then
  2062. begin
  2063. copysize:=1;
  2064. cgsize:=OS_8;
  2065. end
  2066. else if len<4 then
  2067. begin
  2068. copysize:=2;
  2069. cgsize:=OS_16;
  2070. end;
  2071. dec(len,copysize);
  2072. r:=getintregister(list,cgsize);
  2073. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2074. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2075. inc(srcref.offset,copysize);
  2076. inc(dstref.offset,copysize);
  2077. end;{end of while}
  2078. end
  2079. else
  2080. begin
  2081. cgsize:=OS_32;
  2082. if (len<=4) then{len<=4 and not aligned}
  2083. begin
  2084. r:=getintregister(list,cgsize);
  2085. usedtmpref:=a_internal_load_ref_reg(list,OS_8,OS_8,srcref,r);
  2086. if Len=1 then
  2087. a_load_reg_ref(list,OS_8,OS_8,r,dstref)
  2088. else
  2089. begin
  2090. tmpreg:=getintregister(list,cgsize);
  2091. usedtmpref2:=a_internal_load_reg_ref(list,OS_8,OS_8,r,dstref);
  2092. inc(usedtmpref.offset,1);
  2093. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2094. inc(usedtmpref2.offset,1);
  2095. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2096. if len>2 then
  2097. begin
  2098. inc(usedtmpref.offset,1);
  2099. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2100. inc(usedtmpref2.offset,1);
  2101. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2102. if len>3 then
  2103. begin
  2104. inc(usedtmpref.offset,1);
  2105. a_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2106. inc(usedtmpref2.offset,1);
  2107. a_load_reg_ref(list,OS_8,OS_8,tmpreg,usedtmpref2);
  2108. end;
  2109. end;
  2110. end;
  2111. end{end of if len<=4}
  2112. else
  2113. begin{unaligned & 4<len<helpsize **or** aligned/unaligned & len>helpsize}
  2114. destreg:=getintregister(list,OS_ADDR);
  2115. a_loadaddr_ref_reg(list,dest,destreg);
  2116. reference_reset_base(dstref,destreg,0,dest.alignment);
  2117. srcreg:=getintregister(list,OS_ADDR);
  2118. a_loadaddr_ref_reg(list,source,srcreg);
  2119. reference_reset_base(srcref,srcreg,0,source.alignment);
  2120. countreg:=getintregister(list,OS_32);
  2121. // if cs_opt_size in current_settings.optimizerswitches then
  2122. { roozbeh : it seems loading 1 byte is faster becouse of caching/fetching(?) }
  2123. {if aligned then
  2124. genloop(len,4)
  2125. else}
  2126. genloop(len,1);
  2127. end;
  2128. end;
  2129. end;
  2130. procedure tcgarm.g_concatcopy_unaligned(list : TAsmList;const source,dest : treference;len : tcgint);
  2131. begin
  2132. g_concatcopy_internal(list,source,dest,len,false);
  2133. end;
  2134. procedure tcgarm.g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);
  2135. begin
  2136. if (source.alignment in [1..3]) or
  2137. (dest.alignment in [1..3]) then
  2138. g_concatcopy_internal(list,source,dest,len,false)
  2139. else
  2140. g_concatcopy_internal(list,source,dest,len,true);
  2141. end;
  2142. procedure tcgarm.g_overflowCheck(list : TAsmList;const l : tlocation;def : tdef);
  2143. var
  2144. ovloc : tlocation;
  2145. begin
  2146. ovloc.loc:=LOC_VOID;
  2147. g_overflowCheck_loc(list,l,def,ovloc);
  2148. end;
  2149. procedure tcgarm.g_overflowCheck_loc(List:TAsmList;const Loc:TLocation;def:TDef;ovloc : tlocation);
  2150. var
  2151. hl : tasmlabel;
  2152. ai:TAiCpu;
  2153. hflags : tresflags;
  2154. begin
  2155. if not(cs_check_overflow in current_settings.localswitches) then
  2156. exit;
  2157. current_asmdata.getjumplabel(hl);
  2158. case ovloc.loc of
  2159. LOC_VOID:
  2160. begin
  2161. ai:=taicpu.op_sym(A_B,hl);
  2162. ai.is_jmp:=true;
  2163. if not((def.typ=pointerdef) or
  2164. ((def.typ=orddef) and
  2165. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  2166. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  2167. ai.SetCondition(C_VC)
  2168. else
  2169. if TAiCpu(List.Last).opcode in [A_RSB,A_RSC,A_SBC,A_SUB] then
  2170. ai.SetCondition(C_CS)
  2171. else
  2172. ai.SetCondition(C_CC);
  2173. list.concat(ai);
  2174. end;
  2175. LOC_FLAGS:
  2176. begin
  2177. hflags:=ovloc.resflags;
  2178. inverse_flags(hflags);
  2179. cg.a_jmp_flags(list,hflags,hl);
  2180. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2181. end;
  2182. else
  2183. internalerror(200409281);
  2184. end;
  2185. a_call_name(list,'FPC_OVERFLOW',false);
  2186. a_label(list,hl);
  2187. end;
  2188. procedure tcgarm.g_save_registers(list : TAsmList);
  2189. begin
  2190. { this work is done in g_proc_entry }
  2191. end;
  2192. procedure tcgarm.g_restore_registers(list : TAsmList);
  2193. begin
  2194. { this work is done in g_proc_exit }
  2195. end;
  2196. procedure tcgarm.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  2197. var
  2198. ai : taicpu;
  2199. begin
  2200. ai:=Taicpu.Op_sym(A_B,l);
  2201. ai.SetCondition(OpCmp2AsmCond[cond]);
  2202. ai.is_jmp:=true;
  2203. list.concat(ai);
  2204. end;
  2205. procedure tcgarm.g_adjust_self_value(list:TAsmList;procdef: tprocdef;ioffset: tcgint);
  2206. var
  2207. hsym : tsym;
  2208. href : treference;
  2209. paraloc : Pcgparalocation;
  2210. shift : byte;
  2211. begin
  2212. { calculate the parameter info for the procdef }
  2213. procdef.init_paraloc_info(callerside);
  2214. hsym:=tsym(procdef.parast.Find('self'));
  2215. if not(assigned(hsym) and
  2216. (hsym.typ=paravarsym)) then
  2217. internalerror(200305251);
  2218. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  2219. while paraloc<>nil do
  2220. with paraloc^ do
  2221. begin
  2222. case loc of
  2223. LOC_REGISTER:
  2224. begin
  2225. if is_shifter_const(ioffset,shift) then
  2226. a_op_const_reg(list,OP_SUB,size,ioffset,register)
  2227. else
  2228. begin
  2229. a_load_const_reg(list,OS_ADDR,ioffset,NR_R12);
  2230. a_op_reg_reg(list,OP_SUB,size,NR_R12,register);
  2231. end;
  2232. end;
  2233. LOC_REFERENCE:
  2234. begin
  2235. { offset in the wrapper needs to be adjusted for the stored
  2236. return address }
  2237. reference_reset_base(href,reference.index,reference.offset+sizeof(aint),sizeof(pint));
  2238. if is_shifter_const(ioffset,shift) then
  2239. a_op_const_ref(list,OP_SUB,size,ioffset,href)
  2240. else
  2241. begin
  2242. a_load_const_reg(list,OS_ADDR,ioffset,NR_R12);
  2243. a_op_reg_ref(list,OP_SUB,size,NR_R12,href);
  2244. end;
  2245. end
  2246. else
  2247. internalerror(200309189);
  2248. end;
  2249. paraloc:=next;
  2250. end;
  2251. end;
  2252. procedure tcgarm.g_stackpointer_alloc(list: TAsmList; size: longint);
  2253. begin
  2254. internalerror(200807237);
  2255. end;
  2256. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  2257. const
  2258. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  2259. (A_FCPYS,A_FCVTSD,A_NONE,A_NONE,A_NONE),
  2260. (A_FCVTDS,A_FCPYD,A_NONE,A_NONE,A_NONE),
  2261. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  2262. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  2263. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  2264. begin
  2265. result:=convertop[fromsize,tosize];
  2266. if result=A_NONE then
  2267. internalerror(200312205);
  2268. end;
  2269. procedure tcgarm.a_loadmm_reg_reg(list: tasmlist; fromsize,tosize: tcgsize; reg1,reg2: tregister; shuffle: pmmshuffle);
  2270. var
  2271. instr: taicpu;
  2272. begin
  2273. if shuffle=nil then
  2274. begin
  2275. if fromsize=tosize then
  2276. { needs correct size in case of spilling }
  2277. case fromsize of
  2278. OS_F32:
  2279. instr:=taicpu.op_reg_reg(A_FCPYS,reg2,reg1);
  2280. OS_F64:
  2281. instr:=taicpu.op_reg_reg(A_FCPYD,reg2,reg1);
  2282. else
  2283. internalerror(2009112405);
  2284. end
  2285. else
  2286. internalerror(2009112406);
  2287. end
  2288. else if shufflescalar(shuffle) then
  2289. instr:=taicpu.op_reg_reg(get_scalar_mm_op(tosize,fromsize),reg2,reg1)
  2290. else
  2291. internalerror(2009112407);
  2292. list.concat(instr);
  2293. case instr.opcode of
  2294. A_FCPYS,
  2295. A_FCPYD:
  2296. add_move_instruction(instr);
  2297. end;
  2298. end;
  2299. procedure tcgarm.a_loadmm_ref_reg(list: tasmlist; fromsize,tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2300. var
  2301. intreg,
  2302. tmpmmreg : tregister;
  2303. reg64 : tregister64;
  2304. op : tasmop;
  2305. begin
  2306. if assigned(shuffle) and
  2307. not(shufflescalar(shuffle)) then
  2308. internalerror(2009112413);
  2309. case fromsize of
  2310. OS_32,OS_S32:
  2311. begin
  2312. fromsize:=OS_F32;
  2313. { since we are loading an integer, no conversion may be required }
  2314. if (fromsize<>tosize) then
  2315. internalerror(2009112801);
  2316. end;
  2317. OS_64,OS_S64:
  2318. begin
  2319. fromsize:=OS_F64;
  2320. { since we are loading an integer, no conversion may be required }
  2321. if (fromsize<>tosize) then
  2322. internalerror(2009112901);
  2323. end;
  2324. end;
  2325. if (fromsize<>tosize) then
  2326. tmpmmreg:=getmmregister(list,fromsize)
  2327. else
  2328. tmpmmreg:=reg;
  2329. if (ref.alignment in [1,2]) then
  2330. begin
  2331. case fromsize of
  2332. OS_F32:
  2333. begin
  2334. intreg:=getintregister(list,OS_32);
  2335. a_load_ref_reg(list,OS_32,OS_32,ref,intreg);
  2336. a_loadmm_intreg_reg(list,OS_32,OS_F32,intreg,tmpmmreg,mms_movescalar);
  2337. end;
  2338. OS_F64:
  2339. begin
  2340. reg64.reglo:=getintregister(list,OS_32);
  2341. reg64.reghi:=getintregister(list,OS_32);
  2342. cg64.a_load64_ref_reg(list,ref,reg64);
  2343. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,tmpmmreg);
  2344. end;
  2345. else
  2346. internalerror(2009112412);
  2347. end;
  2348. end
  2349. else
  2350. begin
  2351. case fromsize of
  2352. OS_F32:
  2353. op:=A_FLDS;
  2354. OS_F64:
  2355. op:=A_FLDD;
  2356. else
  2357. internalerror(2009112415);
  2358. end;
  2359. handle_load_store(list,op,PF_None,tmpmmreg,ref);
  2360. end;
  2361. if (tmpmmreg<>reg) then
  2362. a_loadmm_reg_reg(list,fromsize,tosize,tmpmmreg,reg,shuffle);
  2363. end;
  2364. procedure tcgarm.a_loadmm_reg_ref(list: tasmlist; fromsize,tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2365. var
  2366. intreg,
  2367. tmpmmreg : tregister;
  2368. reg64 : tregister64;
  2369. op : tasmop;
  2370. begin
  2371. if assigned(shuffle) and
  2372. not(shufflescalar(shuffle)) then
  2373. internalerror(2009112416);
  2374. case tosize of
  2375. OS_32,OS_S32:
  2376. begin
  2377. tosize:=OS_F32;
  2378. { since we are loading an integer, no conversion may be required }
  2379. if (fromsize<>tosize) then
  2380. internalerror(2009112801);
  2381. end;
  2382. OS_64,OS_S64:
  2383. begin
  2384. tosize:=OS_F64;
  2385. { since we are loading an integer, no conversion may be required }
  2386. if (fromsize<>tosize) then
  2387. internalerror(2009112901);
  2388. end;
  2389. end;
  2390. if (fromsize<>tosize) then
  2391. begin
  2392. tmpmmreg:=getmmregister(list,tosize);
  2393. a_loadmm_reg_reg(list,fromsize,tosize,reg,tmpmmreg,shuffle);
  2394. end
  2395. else
  2396. tmpmmreg:=reg;
  2397. if (ref.alignment in [1,2]) then
  2398. begin
  2399. case tosize of
  2400. OS_F32:
  2401. begin
  2402. intreg:=getintregister(list,OS_32);
  2403. a_loadmm_reg_intreg(list,OS_F32,OS_32,tmpmmreg,intreg,shuffle);
  2404. a_load_reg_ref(list,OS_32,OS_32,intreg,ref);
  2405. end;
  2406. OS_F64:
  2407. begin
  2408. reg64.reglo:=getintregister(list,OS_32);
  2409. reg64.reghi:=getintregister(list,OS_32);
  2410. cg64.a_loadmm_reg_intreg64(list,OS_F64,tmpmmreg,reg64);
  2411. cg64.a_load64_reg_ref(list,reg64,ref);
  2412. end;
  2413. else
  2414. internalerror(2009112417);
  2415. end;
  2416. end
  2417. else
  2418. begin
  2419. case fromsize of
  2420. OS_F32:
  2421. op:=A_FSTS;
  2422. OS_F64:
  2423. op:=A_FSTD;
  2424. else
  2425. internalerror(2009112418);
  2426. end;
  2427. handle_load_store(list,op,PF_None,tmpmmreg,ref);
  2428. end;
  2429. end;
  2430. procedure tcgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  2431. begin
  2432. { this code can only be used to transfer raw data, not to perform
  2433. conversions }
  2434. if (tosize<>OS_F32) then
  2435. internalerror(2009112419);
  2436. if not(fromsize in [OS_32,OS_S32]) then
  2437. internalerror(2009112420);
  2438. if assigned(shuffle) and
  2439. not shufflescalar(shuffle) then
  2440. internalerror(2009112516);
  2441. list.concat(taicpu.op_reg_reg(A_FMSR,mmreg,intreg));
  2442. end;
  2443. procedure tcgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister;shuffle : pmmshuffle);
  2444. begin
  2445. { this code can only be used to transfer raw data, not to perform
  2446. conversions }
  2447. if (fromsize<>OS_F32) then
  2448. internalerror(2009112430);
  2449. if not(tosize in [OS_32,OS_S32]) then
  2450. internalerror(2009112420);
  2451. if assigned(shuffle) and
  2452. not shufflescalar(shuffle) then
  2453. internalerror(2009112514);
  2454. list.concat(taicpu.op_reg_reg(A_FMRS,intreg,mmreg));
  2455. end;
  2456. procedure tcgarm.a_opmm_reg_reg(list: tasmlist; op: topcg; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2457. var
  2458. tmpreg: tregister;
  2459. begin
  2460. { the vfp doesn't support xor nor any other logical operation, but
  2461. this routine is used to initialise global mm regvars. We can
  2462. easily initialise an mm reg with 0 though. }
  2463. case op of
  2464. OP_XOR:
  2465. begin
  2466. if (src<>dst) or
  2467. (reg_cgsize(src)<>size) or
  2468. assigned(shuffle) then
  2469. internalerror(2009112907);
  2470. tmpreg:=getintregister(list,OS_32);
  2471. a_load_const_reg(list,OS_32,0,tmpreg);
  2472. case size of
  2473. OS_F32:
  2474. list.concat(taicpu.op_reg_reg(A_FMSR,dst,tmpreg));
  2475. OS_F64:
  2476. list.concat(taicpu.op_reg_reg_reg(A_FMDRR,dst,tmpreg,tmpreg));
  2477. else
  2478. internalerror(2009112908);
  2479. end;
  2480. end
  2481. else
  2482. internalerror(2009112906);
  2483. end;
  2484. end;
  2485. procedure tcgarm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  2486. procedure loadvmttor12;
  2487. var
  2488. href : treference;
  2489. begin
  2490. reference_reset_base(href,NR_R0,0,sizeof(pint));
  2491. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2492. end;
  2493. procedure op_onr12methodaddr;
  2494. var
  2495. href : treference;
  2496. begin
  2497. if (procdef.extnumber=$ffff) then
  2498. Internalerror(200006139);
  2499. { call/jmp vmtoffs(%eax) ; method offs }
  2500. reference_reset_base(href,NR_R12,tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber),sizeof(pint));
  2501. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2502. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  2503. end;
  2504. var
  2505. make_global : boolean;
  2506. begin
  2507. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  2508. Internalerror(200006137);
  2509. if not assigned(procdef.struct) or
  2510. (procdef.procoptions*[po_classmethod, po_staticmethod,
  2511. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  2512. Internalerror(200006138);
  2513. if procdef.owner.symtabletype<>ObjectSymtable then
  2514. Internalerror(200109191);
  2515. make_global:=false;
  2516. if (not current_module.is_unit) or
  2517. create_smartlink or
  2518. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  2519. make_global:=true;
  2520. if make_global then
  2521. list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  2522. else
  2523. list.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  2524. { the wrapper might need aktlocaldata for the additional data to
  2525. load the constant }
  2526. current_procinfo:=cprocinfo.create(nil);
  2527. { set param1 interface to self }
  2528. g_adjust_self_value(list,procdef,ioffset);
  2529. { case 4 }
  2530. if (po_virtualmethod in procdef.procoptions) and
  2531. not is_objectpascal_helper(procdef.struct) then
  2532. begin
  2533. loadvmttor12;
  2534. op_onr12methodaddr;
  2535. end
  2536. { case 0 }
  2537. else
  2538. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  2539. list.concatlist(current_procinfo.aktlocaldata);
  2540. current_procinfo.Free;
  2541. current_procinfo:=nil;
  2542. list.concat(Tai_symbol_end.Createname(labelname));
  2543. end;
  2544. procedure tcgarm.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  2545. const
  2546. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NEG];
  2547. begin
  2548. if (op in overflowops) and
  2549. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  2550. a_load_reg_reg(list,OS_32,size,dst,dst);
  2551. end;
  2552. function tcgarm.get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  2553. var
  2554. stubname: string;
  2555. l1: tasmsymbol;
  2556. href: treference;
  2557. begin
  2558. stubname := 'L'+s+'$stub';
  2559. result := current_asmdata.getasmsymbol(stubname);
  2560. if assigned(result) then
  2561. exit;
  2562. if current_asmdata.asmlists[al_imports]=nil then
  2563. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  2564. new_section(current_asmdata.asmlists[al_imports],sec_stub,'',4);
  2565. result := current_asmdata.RefAsmSymbol(stubname);
  2566. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  2567. { register as a weak symbol if necessary }
  2568. if weak then
  2569. current_asmdata.weakrefasmsymbol(s);
  2570. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  2571. if not(cs_create_pic in current_settings.moduleswitches) then
  2572. begin
  2573. l1 := current_asmdata.RefAsmSymbol('L'+s+'$slp');
  2574. reference_reset_symbol(href,l1,0,sizeof(pint));
  2575. href.refaddr:=addr_full;
  2576. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LDR,NR_R12,href));
  2577. reference_reset_base(href,NR_R12,0,sizeof(pint));
  2578. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LDR,NR_R15,href));
  2579. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(l1,0));
  2580. l1 := current_asmdata.RefAsmSymbol('L'+s+'$lazy_ptr');
  2581. current_asmdata.asmlists[al_imports].concat(tai_const.create_sym(l1));
  2582. end
  2583. else
  2584. internalerror(2008100401);
  2585. new_section(current_asmdata.asmlists[al_imports],sec_data_lazy,'',sizeof(pint));
  2586. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(l1,0));
  2587. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  2588. current_asmdata.asmlists[al_imports].concat(tai_const.createname('dyld_stub_binding_helper',0));
  2589. end;
  2590. procedure tcg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  2591. begin
  2592. case op of
  2593. OP_NEG:
  2594. begin
  2595. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2596. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  2597. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  2598. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2599. end;
  2600. OP_NOT:
  2601. begin
  2602. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  2603. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  2604. end;
  2605. else
  2606. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  2607. end;
  2608. end;
  2609. procedure tcg64farm.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  2610. begin
  2611. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  2612. end;
  2613. procedure tcg64farm.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  2614. var
  2615. ovloc : tlocation;
  2616. begin
  2617. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,ovloc);
  2618. end;
  2619. procedure tcg64farm.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2620. var
  2621. ovloc : tlocation;
  2622. begin
  2623. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,ovloc);
  2624. end;
  2625. procedure tcg64farm.a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister);
  2626. begin
  2627. { this code can only be used to transfer raw data, not to perform
  2628. conversions }
  2629. if (mmsize<>OS_F64) then
  2630. internalerror(2009112405);
  2631. list.concat(taicpu.op_reg_reg_reg(A_FMDRR,mmreg,intreg.reglo,intreg.reghi));
  2632. end;
  2633. procedure tcg64farm.a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64);
  2634. begin
  2635. { this code can only be used to transfer raw data, not to perform
  2636. conversions }
  2637. if (mmsize<>OS_F64) then
  2638. internalerror(2009112406);
  2639. list.concat(taicpu.op_reg_reg_reg(A_FMRRD,intreg.reglo,intreg.reghi,mmreg));
  2640. end;
  2641. procedure tcg64farm.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2642. var
  2643. tmpreg : tregister;
  2644. b : byte;
  2645. begin
  2646. ovloc.loc:=LOC_VOID;
  2647. case op of
  2648. OP_NEG,
  2649. OP_NOT :
  2650. internalerror(200306017);
  2651. end;
  2652. if (setflags or tcgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  2653. begin
  2654. case op of
  2655. OP_ADD:
  2656. begin
  2657. if is_shifter_const(lo(value),b) then
  2658. begin
  2659. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2660. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  2661. end
  2662. else
  2663. begin
  2664. tmpreg:=cg.getintregister(list,OS_32);
  2665. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  2666. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2667. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  2668. end;
  2669. if is_shifter_const(hi(value),b) then
  2670. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  2671. else
  2672. begin
  2673. tmpreg:=cg.getintregister(list,OS_32);
  2674. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  2675. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  2676. end;
  2677. end;
  2678. OP_SUB:
  2679. begin
  2680. if is_shifter_const(lo(value),b) then
  2681. begin
  2682. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2683. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  2684. end
  2685. else
  2686. begin
  2687. tmpreg:=cg.getintregister(list,OS_32);
  2688. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  2689. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2690. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  2691. end;
  2692. if is_shifter_const(hi(value),b) then
  2693. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))),PF_S))
  2694. else
  2695. begin
  2696. tmpreg:=cg.getintregister(list,OS_32);
  2697. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  2698. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  2699. end;
  2700. end;
  2701. else
  2702. internalerror(200502131);
  2703. end;
  2704. if size=OS_64 then
  2705. begin
  2706. { the arm has an weired opinion how flags for SUB/ADD are handled }
  2707. ovloc.loc:=LOC_FLAGS;
  2708. case op of
  2709. OP_ADD:
  2710. ovloc.resflags:=F_CS;
  2711. OP_SUB:
  2712. ovloc.resflags:=F_CC;
  2713. end;
  2714. end;
  2715. end
  2716. else
  2717. begin
  2718. case op of
  2719. OP_AND,OP_OR,OP_XOR:
  2720. begin
  2721. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  2722. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  2723. end;
  2724. OP_ADD:
  2725. begin
  2726. if is_shifter_const(aint(lo(value)),b) then
  2727. begin
  2728. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2729. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  2730. end
  2731. else
  2732. begin
  2733. tmpreg:=cg.getintregister(list,OS_32);
  2734. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  2735. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2736. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  2737. end;
  2738. if is_shifter_const(aint(hi(value)),b) then
  2739. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  2740. else
  2741. begin
  2742. tmpreg:=cg.getintregister(list,OS_32);
  2743. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  2744. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  2745. end;
  2746. end;
  2747. OP_SUB:
  2748. begin
  2749. if is_shifter_const(aint(lo(value)),b) then
  2750. begin
  2751. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2752. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  2753. end
  2754. else
  2755. begin
  2756. tmpreg:=cg.getintregister(list,OS_32);
  2757. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  2758. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2759. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  2760. end;
  2761. if is_shifter_const(aint(hi(value)),b) then
  2762. list.concat(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  2763. else
  2764. begin
  2765. tmpreg:=cg.getintregister(list,OS_32);
  2766. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  2767. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  2768. end;
  2769. end;
  2770. else
  2771. internalerror(2003083101);
  2772. end;
  2773. end;
  2774. end;
  2775. procedure tcg64farm.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2776. begin
  2777. ovloc.loc:=LOC_VOID;
  2778. case op of
  2779. OP_NEG,
  2780. OP_NOT :
  2781. internalerror(200306017);
  2782. end;
  2783. if (setflags or tcgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  2784. begin
  2785. case op of
  2786. OP_ADD:
  2787. begin
  2788. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2789. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  2790. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi),PF_S));
  2791. end;
  2792. OP_SUB:
  2793. begin
  2794. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2795. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  2796. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi),PF_S));
  2797. end;
  2798. else
  2799. internalerror(2003083101);
  2800. end;
  2801. if size=OS_64 then
  2802. begin
  2803. { the arm has an weired opinion how flags for SUB/ADD are handled }
  2804. ovloc.loc:=LOC_FLAGS;
  2805. case op of
  2806. OP_ADD:
  2807. ovloc.resflags:=F_CS;
  2808. OP_SUB:
  2809. ovloc.resflags:=F_CC;
  2810. end;
  2811. end;
  2812. end
  2813. else
  2814. begin
  2815. case op of
  2816. OP_AND,OP_OR,OP_XOR:
  2817. begin
  2818. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2819. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2820. end;
  2821. OP_ADD:
  2822. begin
  2823. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2824. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  2825. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2826. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2827. end;
  2828. OP_SUB:
  2829. begin
  2830. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2831. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  2832. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  2833. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2834. end;
  2835. else
  2836. internalerror(2003083101);
  2837. end;
  2838. end;
  2839. end;
  2840. procedure Tthumb2cgarm.init_register_allocators;
  2841. begin
  2842. inherited init_register_allocators;
  2843. { currently, we save R14 always, so we can use it }
  2844. if (target_info.system<>system_arm_darwin) then
  2845. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  2846. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  2847. RS_R9,RS_R10,RS_R12,RS_R14],first_int_imreg,[])
  2848. else
  2849. { r9 is not available on Darwin according to the llvm code generator }
  2850. rg[R_INTREGISTER]:=trgintcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  2851. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  2852. RS_R10,RS_R12,RS_R14],first_int_imreg,[]);
  2853. rg[R_FPUREGISTER]:=trgcpu.create(R_FPUREGISTER,R_SUBNONE,
  2854. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  2855. if current_settings.fputype=fpu_fpv4_s16 then
  2856. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBFD,
  2857. [RS_D0,RS_D1,RS_D2,RS_D3,RS_D4,RS_D5,RS_D6,RS_D7,
  2858. RS_D8,RS_D9,RS_D10,RS_D11,RS_D12,RS_D13,RS_D14,RS_D15
  2859. ],first_mm_imreg,[])
  2860. else
  2861. rg[R_MMREGISTER]:=trgcpu.create(R_MMREGISTER,R_SUBNONE,
  2862. [RS_S0,RS_S1,RS_R2,RS_R3,RS_R4,RS_S31],first_mm_imreg,[]);
  2863. end;
  2864. procedure Tthumb2cgarm.done_register_allocators;
  2865. begin
  2866. rg[R_INTREGISTER].free;
  2867. rg[R_FPUREGISTER].free;
  2868. rg[R_MMREGISTER].free;
  2869. inherited done_register_allocators;
  2870. end;
  2871. procedure Tthumb2cgarm.a_call_reg(list : TAsmList;reg: tregister);
  2872. begin
  2873. list.concat(taicpu.op_reg(A_BLX, reg));
  2874. {
  2875. the compiler does not properly set this flag anymore in pass 1, and
  2876. for now we only need it after pass 2 (I hope) (JM)
  2877. if not(pi_do_call in current_procinfo.flags) then
  2878. internalerror(2003060703);
  2879. }
  2880. include(current_procinfo.flags,pi_do_call);
  2881. end;
  2882. procedure Tthumb2cgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : tcgint;reg : tregister);
  2883. var
  2884. imm_shift : byte;
  2885. l : tasmlabel;
  2886. hr : treference;
  2887. begin
  2888. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  2889. internalerror(2002090902);
  2890. if is_shifter_const(a,imm_shift) then
  2891. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  2892. { loading of constants with mov and orr }
  2893. else if (is_shifter_const(a-byte(a),imm_shift)) then
  2894. begin
  2895. list.concat(taicpu.op_reg_const(A_MOV,reg,a-byte(a)));
  2896. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg,byte(a)));
  2897. end
  2898. else if (is_shifter_const(a-word(a),imm_shift)) and (is_shifter_const(word(a),imm_shift)) then
  2899. begin
  2900. list.concat(taicpu.op_reg_const(A_MOV,reg,a-word(a)));
  2901. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg,word(a)));
  2902. end
  2903. else if (is_shifter_const(a-(dword(a) shl 8) shr 8,imm_shift)) and (is_shifter_const((dword(a) shl 8) shr 8,imm_shift)) then
  2904. begin
  2905. list.concat(taicpu.op_reg_const(A_MOV,reg,a-(dword(a) shl 8) shr 8));
  2906. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg,(dword(a) shl 8) shr 8));
  2907. end
  2908. else
  2909. begin
  2910. reference_reset(hr,4);
  2911. current_asmdata.getjumplabel(l);
  2912. cg.a_label(current_procinfo.aktlocaldata,l);
  2913. hr.symboldata:=current_procinfo.aktlocaldata.last;
  2914. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  2915. hr.symbol:=l;
  2916. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  2917. end;
  2918. end;
  2919. procedure Tthumb2cgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  2920. var
  2921. oppostfix:toppostfix;
  2922. usedtmpref: treference;
  2923. tmpreg,tmpreg2 : tregister;
  2924. so : tshifterop;
  2925. dir : integer;
  2926. begin
  2927. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  2928. FromSize := ToSize;
  2929. case FromSize of
  2930. { signed integer registers }
  2931. OS_8:
  2932. oppostfix:=PF_B;
  2933. OS_S8:
  2934. oppostfix:=PF_SB;
  2935. OS_16:
  2936. oppostfix:=PF_H;
  2937. OS_S16:
  2938. oppostfix:=PF_SH;
  2939. OS_32,
  2940. OS_S32:
  2941. oppostfix:=PF_None;
  2942. else
  2943. InternalError(200308297);
  2944. end;
  2945. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  2946. begin
  2947. if target_info.endian=endian_big then
  2948. dir:=-1
  2949. else
  2950. dir:=1;
  2951. case FromSize of
  2952. OS_16,OS_S16:
  2953. begin
  2954. { only complicated references need an extra loadaddr }
  2955. if assigned(ref.symbol) or
  2956. (ref.index<>NR_NO) or
  2957. (ref.offset<-255) or
  2958. (ref.offset>4094) or
  2959. { sometimes the compiler reused registers }
  2960. (reg=ref.index) or
  2961. (reg=ref.base) then
  2962. begin
  2963. tmpreg2:=getintregister(list,OS_INT);
  2964. a_loadaddr_ref_reg(list,ref,tmpreg2);
  2965. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  2966. end
  2967. else
  2968. usedtmpref:=ref;
  2969. if target_info.endian=endian_big then
  2970. inc(usedtmpref.offset,1);
  2971. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  2972. tmpreg:=getintregister(list,OS_INT);
  2973. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  2974. inc(usedtmpref.offset,dir);
  2975. if FromSize=OS_16 then
  2976. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  2977. else
  2978. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  2979. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  2980. end;
  2981. OS_32,OS_S32:
  2982. begin
  2983. tmpreg:=getintregister(list,OS_INT);
  2984. { only complicated references need an extra loadaddr }
  2985. if assigned(ref.symbol) or
  2986. (ref.index<>NR_NO) or
  2987. (ref.offset<-255) or
  2988. (ref.offset>4092) or
  2989. { sometimes the compiler reused registers }
  2990. (reg=ref.index) or
  2991. (reg=ref.base) then
  2992. begin
  2993. tmpreg2:=getintregister(list,OS_INT);
  2994. a_loadaddr_ref_reg(list,ref,tmpreg2);
  2995. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  2996. end
  2997. else
  2998. usedtmpref:=ref;
  2999. shifterop_reset(so);so.shiftmode:=SM_LSL;
  3000. if ref.alignment=2 then
  3001. begin
  3002. if target_info.endian=endian_big then
  3003. inc(usedtmpref.offset,2);
  3004. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  3005. inc(usedtmpref.offset,dir*2);
  3006. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  3007. so.shiftimm:=16;
  3008. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  3009. end
  3010. else
  3011. begin
  3012. if target_info.endian=endian_big then
  3013. inc(usedtmpref.offset,3);
  3014. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  3015. inc(usedtmpref.offset,dir);
  3016. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3017. so.shiftimm:=8;
  3018. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  3019. inc(usedtmpref.offset,dir);
  3020. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3021. so.shiftimm:=16;
  3022. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  3023. inc(usedtmpref.offset,dir);
  3024. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  3025. so.shiftimm:=24;
  3026. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  3027. end;
  3028. end
  3029. else
  3030. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3031. end;
  3032. end
  3033. else
  3034. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  3035. if (fromsize=OS_S8) and (tosize = OS_16) then
  3036. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  3037. end;
  3038. procedure Tthumb2cgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: tcgint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  3039. var
  3040. shift : byte;
  3041. tmpreg : tregister;
  3042. so : tshifterop;
  3043. l1 : longint;
  3044. begin
  3045. ovloc.loc:=LOC_VOID;
  3046. if {$ifopt R+}(a<>-2147483648) and{$endif} is_shifter_const(-a,shift) then
  3047. case op of
  3048. OP_ADD:
  3049. begin
  3050. op:=OP_SUB;
  3051. a:=aint(dword(-a));
  3052. end;
  3053. OP_SUB:
  3054. begin
  3055. op:=OP_ADD;
  3056. a:=aint(dword(-a));
  3057. end
  3058. end;
  3059. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  3060. case op of
  3061. OP_NEG,OP_NOT,
  3062. OP_DIV,OP_IDIV:
  3063. internalerror(200308281);
  3064. OP_SHL:
  3065. begin
  3066. if a>32 then
  3067. internalerror(200308294);
  3068. if a<>0 then
  3069. begin
  3070. shifterop_reset(so);
  3071. so.shiftmode:=SM_LSL;
  3072. so.shiftimm:=a;
  3073. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  3074. end
  3075. else
  3076. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  3077. end;
  3078. OP_ROL:
  3079. begin
  3080. if a>32 then
  3081. internalerror(200308294);
  3082. if a<>0 then
  3083. begin
  3084. shifterop_reset(so);
  3085. so.shiftmode:=SM_ROR;
  3086. so.shiftimm:=32-a;
  3087. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  3088. end
  3089. else
  3090. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  3091. end;
  3092. OP_ROR:
  3093. begin
  3094. if a>32 then
  3095. internalerror(200308294);
  3096. if a<>0 then
  3097. begin
  3098. shifterop_reset(so);
  3099. so.shiftmode:=SM_ROR;
  3100. so.shiftimm:=a;
  3101. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  3102. end
  3103. else
  3104. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  3105. end;
  3106. OP_SHR:
  3107. begin
  3108. if a>32 then
  3109. internalerror(200308292);
  3110. shifterop_reset(so);
  3111. if a<>0 then
  3112. begin
  3113. so.shiftmode:=SM_LSR;
  3114. so.shiftimm:=a;
  3115. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  3116. end
  3117. else
  3118. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  3119. end;
  3120. OP_SAR:
  3121. begin
  3122. if a>32 then
  3123. internalerror(200308295);
  3124. if a<>0 then
  3125. begin
  3126. shifterop_reset(so);
  3127. so.shiftmode:=SM_ASR;
  3128. so.shiftimm:=a;
  3129. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  3130. end
  3131. else
  3132. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  3133. end;
  3134. else
  3135. if (op in [OP_SUB, OP_ADD]) and
  3136. ((a < 0) or
  3137. (a > 4095)) then
  3138. begin
  3139. tmpreg:=getintregister(list,size);
  3140. a_load_const_reg(list, size, a, tmpreg);
  3141. if cgsetflags or setflags then
  3142. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3143. list.concat(setoppostfix(
  3144. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src,tmpreg),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  3145. end
  3146. else
  3147. begin
  3148. if cgsetflags or setflags then
  3149. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3150. list.concat(setoppostfix(
  3151. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  3152. end;
  3153. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  3154. begin
  3155. ovloc.loc:=LOC_FLAGS;
  3156. case op of
  3157. OP_ADD:
  3158. ovloc.resflags:=F_CS;
  3159. OP_SUB:
  3160. ovloc.resflags:=F_CC;
  3161. end;
  3162. end;
  3163. end
  3164. else
  3165. begin
  3166. { there could be added some more sophisticated optimizations }
  3167. if (op in [OP_MUL,OP_IMUL]) and (a=1) then
  3168. a_load_reg_reg(list,size,size,src,dst)
  3169. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  3170. a_load_const_reg(list,size,0,dst)
  3171. else if (op in [OP_IMUL]) and (a=-1) then
  3172. a_op_reg_reg(list,OP_NEG,size,src,dst)
  3173. { we do this here instead in the peephole optimizer because
  3174. it saves us a register }
  3175. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  3176. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  3177. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  3178. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  3179. begin
  3180. if l1>32 then{roozbeh does this ever happen?}
  3181. internalerror(200308296);
  3182. shifterop_reset(so);
  3183. so.shiftmode:=SM_LSL;
  3184. so.shiftimm:=l1;
  3185. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  3186. end
  3187. else
  3188. begin
  3189. tmpreg:=getintregister(list,size);
  3190. a_load_const_reg(list,size,a,tmpreg);
  3191. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  3192. end;
  3193. end;
  3194. maybeadjustresult(list,op,size,dst);
  3195. end;
  3196. const
  3197. op_reg_reg_opcg2asmopThumb2: array[TOpCG] of tasmop =
  3198. (A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_MUL,A_MUL,A_NONE,A_MVN,A_ORR,
  3199. A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_NONE,A_ROR);
  3200. procedure Tthumb2cgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  3201. var
  3202. so : tshifterop;
  3203. tmpreg,overflowreg : tregister;
  3204. asmop : tasmop;
  3205. begin
  3206. ovloc.loc:=LOC_VOID;
  3207. case op of
  3208. OP_NEG,OP_NOT:
  3209. internalerror(200308281);
  3210. OP_ROL:
  3211. begin
  3212. if not(size in [OS_32,OS_S32]) then
  3213. internalerror(2008072801);
  3214. { simulate ROL by ror'ing 32-value }
  3215. tmpreg:=getintregister(list,OS_32);
  3216. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,32));
  3217. list.concat(taicpu.op_reg_reg_reg(A_SUB,src1,tmpreg,src1));
  3218. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  3219. end;
  3220. OP_ROR:
  3221. begin
  3222. if not(size in [OS_32,OS_S32]) then
  3223. internalerror(2008072802);
  3224. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  3225. end;
  3226. OP_IMUL,
  3227. OP_MUL:
  3228. begin
  3229. if cgsetflags or setflags then
  3230. begin
  3231. overflowreg:=getintregister(list,size);
  3232. if op=OP_IMUL then
  3233. asmop:=A_SMULL
  3234. else
  3235. asmop:=A_UMULL;
  3236. { the arm doesn't allow that rd and rm are the same }
  3237. if dst=src2 then
  3238. begin
  3239. if dst<>src1 then
  3240. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  3241. else
  3242. begin
  3243. tmpreg:=getintregister(list,size);
  3244. a_load_reg_reg(list,size,size,src2,dst);
  3245. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  3246. end;
  3247. end
  3248. else
  3249. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  3250. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3251. if op=OP_IMUL then
  3252. begin
  3253. shifterop_reset(so);
  3254. so.shiftmode:=SM_ASR;
  3255. so.shiftimm:=31;
  3256. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  3257. end
  3258. else
  3259. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  3260. ovloc.loc:=LOC_FLAGS;
  3261. ovloc.resflags:=F_NE;
  3262. end
  3263. else
  3264. begin
  3265. { the arm doesn't allow that rd and rm are the same }
  3266. if dst=src2 then
  3267. begin
  3268. if dst<>src1 then
  3269. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  3270. else
  3271. begin
  3272. tmpreg:=getintregister(list,size);
  3273. a_load_reg_reg(list,size,size,src2,dst);
  3274. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  3275. end;
  3276. end
  3277. else
  3278. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  3279. end;
  3280. end;
  3281. else
  3282. begin
  3283. if cgsetflags or setflags then
  3284. a_reg_alloc(list,NR_DEFAULTFLAGS);
  3285. list.concat(setoppostfix(
  3286. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmopThumb2[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))));
  3287. end;
  3288. end;
  3289. maybeadjustresult(list,op,size,dst);
  3290. end;
  3291. procedure Tthumb2cgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  3292. var item: taicpu;
  3293. begin
  3294. list.concat(taicpu.op_cond(A_ITE, flags_to_cond(f)));
  3295. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f)));
  3296. list.concat(setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f))));
  3297. end;
  3298. procedure Tthumb2cgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  3299. var
  3300. ref : treference;
  3301. shift : byte;
  3302. firstfloatreg,lastfloatreg,
  3303. r : byte;
  3304. regs : tcpuregisterset;
  3305. stackmisalignment: pint;
  3306. begin
  3307. LocalSize:=align(LocalSize,4);
  3308. { call instruction does not put anything on the stack }
  3309. stackmisalignment:=0;
  3310. if not(nostackframe) then
  3311. begin
  3312. firstfloatreg:=RS_NO;
  3313. { save floating point registers? }
  3314. for r:=RS_F0 to RS_F7 do
  3315. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  3316. begin
  3317. if firstfloatreg=RS_NO then
  3318. firstfloatreg:=r;
  3319. lastfloatreg:=r;
  3320. inc(stackmisalignment,12);
  3321. end;
  3322. a_reg_alloc(list,NR_STACK_POINTER_REG);
  3323. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3324. begin
  3325. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  3326. a_reg_alloc(list,NR_R12);
  3327. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  3328. end;
  3329. { save int registers }
  3330. reference_reset(ref,4);
  3331. ref.index:=NR_STACK_POINTER_REG;
  3332. ref.addressmode:=AM_PREINDEXED;
  3333. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3334. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3335. regs:=regs+[RS_FRAME_POINTER_REG,RS_R14]
  3336. else if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  3337. include(regs,RS_R14);
  3338. if regs<>[] then
  3339. begin
  3340. for r:=RS_R0 to RS_R15 do
  3341. if (r in regs) then
  3342. inc(stackmisalignment,4);
  3343. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  3344. end;
  3345. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3346. begin
  3347. { the framepointer now points to the saved R15, so the saved
  3348. framepointer is at R11-12 (for get_caller_frame) }
  3349. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_FRAME_POINTER_REG,NR_R12,4));
  3350. a_reg_dealloc(list,NR_R12);
  3351. end;
  3352. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  3353. if (LocalSize<>0) or
  3354. ((stackmisalignment<>0) and
  3355. ((pi_do_call in current_procinfo.flags) or
  3356. (po_assembler in current_procinfo.procdef.procoptions))) then
  3357. begin
  3358. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3359. if not(is_shifter_const(localsize,shift)) then
  3360. begin
  3361. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3362. a_reg_alloc(list,NR_R12);
  3363. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  3364. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  3365. a_reg_dealloc(list,NR_R12);
  3366. end
  3367. else
  3368. begin
  3369. a_reg_dealloc(list,NR_R12);
  3370. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  3371. end;
  3372. end;
  3373. if firstfloatreg<>RS_NO then
  3374. begin
  3375. reference_reset(ref,4);
  3376. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  3377. begin
  3378. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  3379. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  3380. ref.base:=NR_R12;
  3381. end
  3382. else
  3383. begin
  3384. ref.base:=current_procinfo.framepointer;
  3385. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  3386. end;
  3387. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  3388. lastfloatreg-firstfloatreg+1,ref));
  3389. end;
  3390. end;
  3391. end;
  3392. procedure Tthumb2cgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  3393. var
  3394. ref : treference;
  3395. firstfloatreg,lastfloatreg,
  3396. r : byte;
  3397. shift : byte;
  3398. regs : tcpuregisterset;
  3399. LocalSize : longint;
  3400. stackmisalignment: pint;
  3401. begin
  3402. if not(nostackframe) then
  3403. begin
  3404. stackmisalignment:=0;
  3405. { restore floating point register }
  3406. firstfloatreg:=RS_NO;
  3407. { save floating point registers? }
  3408. for r:=RS_F0 to RS_F7 do
  3409. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  3410. begin
  3411. if firstfloatreg=RS_NO then
  3412. firstfloatreg:=r;
  3413. lastfloatreg:=r;
  3414. { floating point register space is already included in
  3415. localsize below by calc_stackframe_size
  3416. inc(stackmisalignment,12);
  3417. }
  3418. end;
  3419. if firstfloatreg<>RS_NO then
  3420. begin
  3421. reference_reset(ref,4);
  3422. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  3423. begin
  3424. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  3425. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  3426. ref.base:=NR_R12;
  3427. end
  3428. else
  3429. begin
  3430. ref.base:=current_procinfo.framepointer;
  3431. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  3432. end;
  3433. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  3434. lastfloatreg-firstfloatreg+1,ref));
  3435. end;
  3436. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3437. if (pi_do_call in current_procinfo.flags) or (regs<>[]) then
  3438. begin
  3439. exclude(regs,RS_R14);
  3440. include(regs,RS_R15);
  3441. end;
  3442. if (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  3443. regs:=regs+[RS_FRAME_POINTER_REG,RS_R15];
  3444. for r:=RS_R0 to RS_R15 do
  3445. if (r in regs) then
  3446. inc(stackmisalignment,4);
  3447. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  3448. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  3449. begin
  3450. LocalSize:=current_procinfo.calc_stackframe_size;
  3451. if (LocalSize<>0) or
  3452. ((stackmisalignment<>0) and
  3453. ((pi_do_call in current_procinfo.flags) or
  3454. (po_assembler in current_procinfo.procdef.procoptions))) then
  3455. begin
  3456. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3457. if not(is_shifter_const(LocalSize,shift)) then
  3458. begin
  3459. a_reg_alloc(list,NR_R12);
  3460. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  3461. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  3462. a_reg_dealloc(list,NR_R12);
  3463. end
  3464. else
  3465. begin
  3466. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  3467. end;
  3468. end;
  3469. if regs=[] then
  3470. list.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14))
  3471. else
  3472. begin
  3473. reference_reset(ref,4);
  3474. ref.index:=NR_STACK_POINTER_REG;
  3475. ref.addressmode:=AM_PREINDEXED;
  3476. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  3477. end;
  3478. end
  3479. else
  3480. begin
  3481. { restore int registers and return }
  3482. list.concat(taicpu.op_reg_reg(A_MOV, NR_STACK_POINTER_REG, NR_FRAME_POINTER_REG));
  3483. { Add 4 to SP to make it point to an "imaginary PC" which the paramanager assumes is there(for normal ARM) }
  3484. list.concat(taicpu.op_reg_const(A_ADD, NR_STACK_POINTER_REG, 4));
  3485. reference_reset(ref,4);
  3486. ref.index:=NR_STACK_POINTER_REG;
  3487. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_DB));
  3488. end;
  3489. end
  3490. else
  3491. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14));
  3492. end;
  3493. function Tthumb2cgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  3494. var
  3495. tmpreg : tregister;
  3496. tmpref : treference;
  3497. l : tasmlabel;
  3498. so: tshifterop;
  3499. begin
  3500. tmpreg:=NR_NO;
  3501. { Be sure to have a base register }
  3502. if (ref.base=NR_NO) then
  3503. begin
  3504. if ref.shiftmode<>SM_None then
  3505. internalerror(200308294);
  3506. ref.base:=ref.index;
  3507. ref.index:=NR_NO;
  3508. end;
  3509. { absolute symbols can't be handled directly, we've to store the symbol reference
  3510. in the text segment and access it pc relative
  3511. For now, we assume that references where base or index equals to PC are already
  3512. relative, all other references are assumed to be absolute and thus they need
  3513. to be handled extra.
  3514. A proper solution would be to change refoptions to a set and store the information
  3515. if the symbol is absolute or relative there.
  3516. }
  3517. if (assigned(ref.symbol) and
  3518. not(is_pc(ref.base)) and
  3519. not(is_pc(ref.index))
  3520. ) or
  3521. { [#xxx] isn't a valid address operand }
  3522. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  3523. //(ref.offset<-4095) or
  3524. (ref.offset<-255) or
  3525. (ref.offset>4095) or
  3526. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  3527. ((ref.offset<-255) or
  3528. (ref.offset>255)
  3529. )
  3530. ) or
  3531. ((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) and
  3532. ((ref.offset<-1020) or
  3533. (ref.offset>1020) or
  3534. { the usual pc relative symbol handling assumes possible offsets of +/- 4095 }
  3535. assigned(ref.symbol)
  3536. )
  3537. ) then
  3538. begin
  3539. reference_reset(tmpref,4);
  3540. { load symbol }
  3541. tmpreg:=getintregister(list,OS_INT);
  3542. if assigned(ref.symbol) then
  3543. begin
  3544. current_asmdata.getjumplabel(l);
  3545. cg.a_label(current_procinfo.aktlocaldata,l);
  3546. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3547. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset));
  3548. { load consts entry }
  3549. tmpref.symbol:=l;
  3550. tmpref.base:=NR_R15;
  3551. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  3552. { in case of LDF/STF, we got rid of the NR_R15 }
  3553. if is_pc(ref.base) then
  3554. ref.base:=NR_NO;
  3555. if is_pc(ref.index) then
  3556. ref.index:=NR_NO;
  3557. end
  3558. else
  3559. a_load_const_reg(list,OS_ADDR,ref.offset,tmpreg);
  3560. if (ref.base<>NR_NO) then
  3561. begin
  3562. if ref.index<>NR_NO then
  3563. begin
  3564. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  3565. ref.base:=tmpreg;
  3566. end
  3567. else
  3568. begin
  3569. ref.index:=tmpreg;
  3570. ref.shiftimm:=0;
  3571. ref.signindex:=1;
  3572. ref.shiftmode:=SM_None;
  3573. end;
  3574. end
  3575. else
  3576. ref.base:=tmpreg;
  3577. ref.offset:=0;
  3578. ref.symbol:=nil;
  3579. end;
  3580. if (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  3581. begin
  3582. if tmpreg<>NR_NO then
  3583. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg,tmpreg)
  3584. else
  3585. begin
  3586. tmpreg:=getintregister(list,OS_ADDR);
  3587. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg);
  3588. ref.base:=tmpreg;
  3589. end;
  3590. ref.offset:=0;
  3591. end;
  3592. { Hack? Thumb2 doesn't allow PC indexed addressing modes(although it does in the specification) }
  3593. if (ref.base=NR_R15) and (ref.index<>NR_NO) and (ref.shiftmode <> sm_none) then
  3594. begin
  3595. tmpreg:=getintregister(list,OS_ADDR);
  3596. list.concat(taicpu.op_reg_reg(A_MOV, tmpreg, NR_R15));
  3597. ref.base := tmpreg;
  3598. end;
  3599. { floating point operations have only limited references
  3600. we expect here, that a base is already set }
  3601. if (op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) and (ref.index<>NR_NO) then
  3602. begin
  3603. if ref.shiftmode<>SM_none then
  3604. internalerror(200309121);
  3605. if tmpreg<>NR_NO then
  3606. begin
  3607. if ref.base=tmpreg then
  3608. begin
  3609. if ref.signindex<0 then
  3610. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,tmpreg,ref.index))
  3611. else
  3612. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,tmpreg,ref.index));
  3613. ref.index:=NR_NO;
  3614. end
  3615. else
  3616. begin
  3617. if ref.index<>tmpreg then
  3618. internalerror(200403161);
  3619. if ref.signindex<0 then
  3620. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,ref.base,tmpreg))
  3621. else
  3622. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  3623. ref.base:=tmpreg;
  3624. ref.index:=NR_NO;
  3625. end;
  3626. end
  3627. else
  3628. begin
  3629. tmpreg:=getintregister(list,OS_ADDR);
  3630. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,ref.index));
  3631. ref.base:=tmpreg;
  3632. ref.index:=NR_NO;
  3633. end;
  3634. end;
  3635. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  3636. Result := ref;
  3637. end;
  3638. procedure Tthumb2cgarm.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister; shuffle: pmmshuffle);
  3639. var
  3640. instr: taicpu;
  3641. begin
  3642. if (fromsize=OS_F32) and
  3643. (tosize=OS_F32) then
  3644. begin
  3645. instr:=setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32);
  3646. list.Concat(instr);
  3647. add_move_instruction(instr);
  3648. end
  3649. else if (fromsize=OS_F64) and
  3650. (tosize=OS_F64) then
  3651. begin
  3652. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,tregister(longint(reg2)+1),tregister(longint(reg1)+1)), PF_F32));
  3653. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VMOV,reg2,reg1), PF_F32));
  3654. end
  3655. else if (fromsize=OS_F32) and
  3656. (tosize=OS_F64) then
  3657. //list.Concat(setoppostfix(taicpu.op_reg_reg(A_VCVT,reg2,reg1), PF_F32))
  3658. begin
  3659. //list.concat(nil);
  3660. end;
  3661. end;
  3662. procedure Tthumb2cgarm.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  3663. var
  3664. href: treference;
  3665. tmpreg: TRegister;
  3666. so: tshifterop;
  3667. begin
  3668. href:=ref;
  3669. if (href.base<>NR_NO) and
  3670. (href.index<>NR_NO) then
  3671. begin
  3672. tmpreg:=getintregister(list,OS_INT);
  3673. if href.shiftmode<>SM_None then
  3674. begin
  3675. so.rs:=href.index;
  3676. so.shiftimm:=href.shiftimm;
  3677. so.shiftmode:=href.shiftmode;
  3678. list.concat(taicpu.op_reg_reg_shifterop(A_ADD,tmpreg,href.base,so));
  3679. end
  3680. else
  3681. a_op_reg_reg_reg(list,OP_ADD,OS_INT,href.index,href.base,tmpreg);
  3682. reference_reset_base(href,tmpreg,href.offset,0);
  3683. end;
  3684. if assigned(href.symbol) then
  3685. begin
  3686. tmpreg:=getintregister(list,OS_INT);
  3687. a_loadaddr_ref_reg(list,href,tmpreg);
  3688. reference_reset_base(href,tmpreg,0,0);
  3689. end;
  3690. if fromsize=OS_F32 then
  3691. list.Concat(setoppostfix(taicpu.op_reg_ref(A_VLDR,reg,href), PF_F32))
  3692. else
  3693. list.Concat(setoppostfix(taicpu.op_reg_ref(A_VLDR,reg,href), PF_F64));
  3694. end;
  3695. procedure Tthumb2cgarm.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  3696. var
  3697. href: treference;
  3698. so: tshifterop;
  3699. tmpreg: TRegister;
  3700. begin
  3701. href:=ref;
  3702. if (href.base<>NR_NO) and
  3703. (href.index<>NR_NO) then
  3704. begin
  3705. tmpreg:=getintregister(list,OS_INT);
  3706. if href.shiftmode<>SM_None then
  3707. begin
  3708. so.rs:=href.index;
  3709. so.shiftimm:=href.shiftimm;
  3710. so.shiftmode:=href.shiftmode;
  3711. list.concat(taicpu.op_reg_reg_shifterop(A_ADD,tmpreg,href.base,so));
  3712. end
  3713. else
  3714. a_op_reg_reg_reg(list,OP_ADD,OS_INT,href.index,href.base,tmpreg);
  3715. reference_reset_base(href,tmpreg,href.offset,0);
  3716. end;
  3717. if assigned(href.symbol) then
  3718. begin
  3719. tmpreg:=getintregister(list,OS_INT);
  3720. a_loadaddr_ref_reg(list,href,tmpreg);
  3721. reference_reset_base(href,tmpreg,0,0);
  3722. end;
  3723. if fromsize=OS_F32 then
  3724. list.Concat(setoppostfix(taicpu.op_reg_ref(A_VSTR,reg,href), PF_32))
  3725. else
  3726. list.Concat(setoppostfix(taicpu.op_reg_ref(A_VSTR,reg,href), PF_64));
  3727. end;
  3728. procedure Tthumb2cgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize: tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  3729. begin
  3730. if //(shuffle=nil) and
  3731. (tosize=OS_F32) then
  3732. list.Concat(taicpu.op_reg_reg(A_VMOV,mmreg,intreg))
  3733. else
  3734. internalerror(2012100813);
  3735. end;
  3736. procedure Tthumb2cgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize: tcgsize; mmreg, intreg: tregister; shuffle: pmmshuffle);
  3737. begin
  3738. if //(shuffle=nil) and
  3739. (fromsize=OS_F32) then
  3740. list.Concat(taicpu.op_reg_reg(A_VMOV,intreg,mmreg))
  3741. else
  3742. internalerror(2012100814);
  3743. end;
  3744. procedure tthumb2cg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  3745. var tmpreg: tregister;
  3746. begin
  3747. case op of
  3748. OP_NEG:
  3749. begin
  3750. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  3751. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  3752. tmpreg:=cg.getintregister(list,OS_32);
  3753. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,0));
  3754. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,tmpreg,regsrc.reghi));
  3755. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  3756. end;
  3757. else
  3758. inherited a_op64_reg_reg(list, op, size, regsrc, regdst);
  3759. end;
  3760. end;
  3761. procedure create_codegen;
  3762. begin
  3763. if current_settings.cputype in cpu_thumb2 then
  3764. begin
  3765. cg:=tthumb2cgarm.create;
  3766. cg64:=tthumb2cg64farm.create;
  3767. casmoptimizer:=TCpuThumb2AsmOptimizer;
  3768. end
  3769. else
  3770. begin
  3771. cg:=tarmcgarm.create;
  3772. cg64:=tcg64farm.create;
  3773. casmoptimizer:=TCpuAsmOptimizer;
  3774. end;
  3775. end;
  3776. end.