cgcpu.pas 143 KB

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