cgcpu.pas 144 KB

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