cgcpu.pas 143 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689
  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_armv6) 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. if not procdef.has_paraloc_info then
  1953. begin
  1954. procdef.requiredargarea:=paramanager.create_paraloc_info(procdef,callerside);
  1955. procdef.has_paraloc_info:=true;
  1956. end;
  1957. hsym:=tsym(procdef.parast.Find('self'));
  1958. if not(assigned(hsym) and
  1959. (hsym.typ=paravarsym)) then
  1960. internalerror(200305251);
  1961. paraloc:=tparavarsym(hsym).paraloc[callerside].location;
  1962. while paraloc<>nil do
  1963. with paraloc^ do
  1964. begin
  1965. case loc of
  1966. LOC_REGISTER:
  1967. begin
  1968. if is_shifter_const(ioffset,shift) then
  1969. a_op_const_reg(list,OP_SUB,size,ioffset,register)
  1970. else
  1971. begin
  1972. a_load_const_reg(list,OS_ADDR,ioffset,NR_R12);
  1973. a_op_reg_reg(list,OP_SUB,size,NR_R12,register);
  1974. end;
  1975. end;
  1976. LOC_REFERENCE:
  1977. begin
  1978. { offset in the wrapper needs to be adjusted for the stored
  1979. return address }
  1980. reference_reset_base(href,reference.index,reference.offset+sizeof(aint),sizeof(pint));
  1981. if is_shifter_const(ioffset,shift) then
  1982. a_op_const_ref(list,OP_SUB,size,ioffset,href)
  1983. else
  1984. begin
  1985. a_load_const_reg(list,OS_ADDR,ioffset,NR_R12);
  1986. a_op_reg_ref(list,OP_SUB,size,NR_R12,href);
  1987. end;
  1988. end
  1989. else
  1990. internalerror(200309189);
  1991. end;
  1992. paraloc:=next;
  1993. end;
  1994. end;
  1995. procedure tcgarm.g_stackpointer_alloc(list: TAsmList; size: longint);
  1996. begin
  1997. internalerror(200807237);
  1998. end;
  1999. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  2000. const
  2001. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  2002. (A_FCPYS,A_FCVTSD,A_NONE,A_NONE,A_NONE),
  2003. (A_FCVTDS,A_FCPYD,A_NONE,A_NONE,A_NONE),
  2004. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  2005. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  2006. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  2007. begin
  2008. result:=convertop[fromsize,tosize];
  2009. if result=A_NONE then
  2010. internalerror(200312205);
  2011. end;
  2012. procedure tcgarm.a_loadmm_reg_reg(list: tasmlist; fromsize,tosize: tcgsize; reg1,reg2: tregister; shuffle: pmmshuffle);
  2013. var
  2014. instr: taicpu;
  2015. begin
  2016. if shuffle=nil then
  2017. begin
  2018. if fromsize=tosize then
  2019. { needs correct size in case of spilling }
  2020. case fromsize of
  2021. OS_F32:
  2022. instr:=taicpu.op_reg_reg(A_FCPYS,reg2,reg1);
  2023. OS_F64:
  2024. instr:=taicpu.op_reg_reg(A_FCPYD,reg2,reg1);
  2025. else
  2026. internalerror(2009112405);
  2027. end
  2028. else
  2029. internalerror(2009112406);
  2030. end
  2031. else if shufflescalar(shuffle) then
  2032. instr:=taicpu.op_reg_reg(get_scalar_mm_op(tosize,fromsize),reg2,reg1)
  2033. else
  2034. internalerror(2009112407);
  2035. list.concat(instr);
  2036. case instr.opcode of
  2037. A_FCPYS,
  2038. A_FCPYD:
  2039. add_move_instruction(instr);
  2040. end;
  2041. end;
  2042. procedure tcgarm.a_loadmm_ref_reg(list: tasmlist; fromsize,tosize: tcgsize; const ref: treference; reg: tregister; shuffle: pmmshuffle);
  2043. var
  2044. intreg,
  2045. tmpmmreg : tregister;
  2046. reg64 : tregister64;
  2047. op : tasmop;
  2048. begin
  2049. if assigned(shuffle) and
  2050. not(shufflescalar(shuffle)) then
  2051. internalerror(2009112413);
  2052. case fromsize of
  2053. OS_32,OS_S32:
  2054. begin
  2055. fromsize:=OS_F32;
  2056. { since we are loading an integer, no conversion may be required }
  2057. if (fromsize<>tosize) then
  2058. internalerror(2009112801);
  2059. end;
  2060. OS_64,OS_S64:
  2061. begin
  2062. fromsize:=OS_F64;
  2063. { since we are loading an integer, no conversion may be required }
  2064. if (fromsize<>tosize) then
  2065. internalerror(2009112901);
  2066. end;
  2067. end;
  2068. if (fromsize<>tosize) then
  2069. tmpmmreg:=getmmregister(list,fromsize)
  2070. else
  2071. tmpmmreg:=reg;
  2072. if (ref.alignment in [1,2]) then
  2073. begin
  2074. case fromsize of
  2075. OS_F32:
  2076. begin
  2077. intreg:=getintregister(list,OS_32);
  2078. a_load_ref_reg(list,OS_32,OS_32,ref,intreg);
  2079. a_loadmm_intreg_reg(list,OS_32,OS_F32,intreg,tmpmmreg,mms_movescalar);
  2080. end;
  2081. OS_F64:
  2082. begin
  2083. reg64.reglo:=getintregister(list,OS_32);
  2084. reg64.reghi:=getintregister(list,OS_32);
  2085. cg64.a_load64_ref_reg(list,ref,reg64);
  2086. cg64.a_loadmm_intreg64_reg(list,OS_F64,reg64,tmpmmreg);
  2087. end;
  2088. else
  2089. internalerror(2009112412);
  2090. end;
  2091. end
  2092. else
  2093. begin
  2094. case fromsize of
  2095. OS_F32:
  2096. op:=A_FLDS;
  2097. OS_F64:
  2098. op:=A_FLDD;
  2099. else
  2100. internalerror(2009112415);
  2101. end;
  2102. handle_load_store(list,op,PF_None,tmpmmreg,ref);
  2103. end;
  2104. if (tmpmmreg<>reg) then
  2105. a_loadmm_reg_reg(list,fromsize,tosize,tmpmmreg,reg,shuffle);
  2106. end;
  2107. procedure tcgarm.a_loadmm_reg_ref(list: tasmlist; fromsize,tosize: tcgsize; reg: tregister; const ref: treference; shuffle: pmmshuffle);
  2108. var
  2109. intreg,
  2110. tmpmmreg : tregister;
  2111. reg64 : tregister64;
  2112. op : tasmop;
  2113. begin
  2114. if assigned(shuffle) and
  2115. not(shufflescalar(shuffle)) then
  2116. internalerror(2009112416);
  2117. case tosize of
  2118. OS_32,OS_S32:
  2119. begin
  2120. tosize:=OS_F32;
  2121. { since we are loading an integer, no conversion may be required }
  2122. if (fromsize<>tosize) then
  2123. internalerror(2009112801);
  2124. end;
  2125. OS_64,OS_S64:
  2126. begin
  2127. tosize:=OS_F64;
  2128. { since we are loading an integer, no conversion may be required }
  2129. if (fromsize<>tosize) then
  2130. internalerror(2009112901);
  2131. end;
  2132. end;
  2133. if (fromsize<>tosize) then
  2134. begin
  2135. tmpmmreg:=getmmregister(list,tosize);
  2136. a_loadmm_reg_reg(list,fromsize,tosize,reg,tmpmmreg,shuffle);
  2137. end
  2138. else
  2139. tmpmmreg:=reg;
  2140. if (ref.alignment in [1,2]) then
  2141. begin
  2142. case tosize of
  2143. OS_F32:
  2144. begin
  2145. intreg:=getintregister(list,OS_32);
  2146. a_loadmm_reg_intreg(list,OS_F32,OS_32,tmpmmreg,intreg,shuffle);
  2147. a_load_reg_ref(list,OS_32,OS_32,intreg,ref);
  2148. end;
  2149. OS_F64:
  2150. begin
  2151. reg64.reglo:=getintregister(list,OS_32);
  2152. reg64.reghi:=getintregister(list,OS_32);
  2153. cg64.a_loadmm_reg_intreg64(list,OS_F64,tmpmmreg,reg64);
  2154. cg64.a_load64_reg_ref(list,reg64,ref);
  2155. end;
  2156. else
  2157. internalerror(2009112417);
  2158. end;
  2159. end
  2160. else
  2161. begin
  2162. case fromsize of
  2163. OS_F32:
  2164. op:=A_FSTS;
  2165. OS_F64:
  2166. op:=A_FSTD;
  2167. else
  2168. internalerror(2009112418);
  2169. end;
  2170. handle_load_store(list,op,PF_None,tmpmmreg,ref);
  2171. end;
  2172. end;
  2173. procedure tcgarm.a_loadmm_intreg_reg(list: TAsmList; fromsize, tosize : tcgsize; intreg, mmreg: tregister; shuffle: pmmshuffle);
  2174. begin
  2175. { this code can only be used to transfer raw data, not to perform
  2176. conversions }
  2177. if (tosize<>OS_F32) then
  2178. internalerror(2009112419);
  2179. if not(fromsize in [OS_32,OS_S32]) then
  2180. internalerror(2009112420);
  2181. if assigned(shuffle) and
  2182. not shufflescalar(shuffle) then
  2183. internalerror(2009112516);
  2184. list.concat(taicpu.op_reg_reg(A_FMSR,mmreg,intreg));
  2185. end;
  2186. procedure tcgarm.a_loadmm_reg_intreg(list: TAsmList; fromsize, tosize : tcgsize; mmreg, intreg: tregister;shuffle : pmmshuffle);
  2187. begin
  2188. { this code can only be used to transfer raw data, not to perform
  2189. conversions }
  2190. if (fromsize<>OS_F32) then
  2191. internalerror(2009112430);
  2192. if not(tosize in [OS_32,OS_S32]) then
  2193. internalerror(2009112420);
  2194. if assigned(shuffle) and
  2195. not shufflescalar(shuffle) then
  2196. internalerror(2009112514);
  2197. list.concat(taicpu.op_reg_reg(A_FMRS,intreg,mmreg));
  2198. end;
  2199. procedure tcgarm.a_opmm_reg_reg(list: tasmlist; op: topcg; size: tcgsize; src, dst: tregister; shuffle: pmmshuffle);
  2200. var
  2201. tmpreg: tregister;
  2202. begin
  2203. { the vfp doesn't support xor nor any other logical operation, but
  2204. this routine is used to initialise global mm regvars. We can
  2205. easily initialise an mm reg with 0 though. }
  2206. case op of
  2207. OP_XOR:
  2208. begin
  2209. if (src<>dst) or
  2210. (reg_cgsize(src)<>size) or
  2211. assigned(shuffle) then
  2212. internalerror(2009112907);
  2213. tmpreg:=getintregister(list,OS_32);
  2214. a_load_const_reg(list,OS_32,0,tmpreg);
  2215. case size of
  2216. OS_F32:
  2217. list.concat(taicpu.op_reg_reg(A_FMSR,dst,tmpreg));
  2218. OS_F64:
  2219. list.concat(taicpu.op_reg_reg_reg(A_FMDRR,dst,tmpreg,tmpreg));
  2220. else
  2221. internalerror(2009112908);
  2222. end;
  2223. end
  2224. else
  2225. internalerror(2009112906);
  2226. end;
  2227. end;
  2228. procedure tcgarm.g_intf_wrapper(list: TAsmList; procdef: tprocdef; const labelname: string; ioffset: longint);
  2229. procedure loadvmttor12;
  2230. var
  2231. href : treference;
  2232. begin
  2233. reference_reset_base(href,NR_R0,0,sizeof(pint));
  2234. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2235. end;
  2236. procedure op_onr12methodaddr;
  2237. var
  2238. href : treference;
  2239. begin
  2240. if (procdef.extnumber=$ffff) then
  2241. Internalerror(200006139);
  2242. { call/jmp vmtoffs(%eax) ; method offs }
  2243. reference_reset_base(href,NR_R12,procdef._class.vmtmethodoffset(procdef.extnumber),sizeof(pint));
  2244. cg.a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,NR_R12);
  2245. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R12));
  2246. end;
  2247. var
  2248. make_global : boolean;
  2249. begin
  2250. if not(procdef.proctypeoption in [potype_function,potype_procedure]) then
  2251. Internalerror(200006137);
  2252. if not assigned(procdef._class) or
  2253. (procdef.procoptions*[po_classmethod, po_staticmethod,
  2254. po_methodpointer, po_interrupt, po_iocheck]<>[]) then
  2255. Internalerror(200006138);
  2256. if procdef.owner.symtabletype<>ObjectSymtable then
  2257. Internalerror(200109191);
  2258. make_global:=false;
  2259. if (not current_module.is_unit) or
  2260. create_smartlink or
  2261. (procdef.owner.defowner.owner.symtabletype=globalsymtable) then
  2262. make_global:=true;
  2263. if make_global then
  2264. list.concat(Tai_symbol.Createname_global(labelname,AT_FUNCTION,0))
  2265. else
  2266. list.concat(Tai_symbol.Createname(labelname,AT_FUNCTION,0));
  2267. { the wrapper might need aktlocaldata for the additional data to
  2268. load the constant }
  2269. current_procinfo:=cprocinfo.create(nil);
  2270. { set param1 interface to self }
  2271. g_adjust_self_value(list,procdef,ioffset);
  2272. { case 4 }
  2273. if po_virtualmethod in procdef.procoptions then
  2274. begin
  2275. loadvmttor12;
  2276. op_onr12methodaddr;
  2277. end
  2278. { case 0 }
  2279. else
  2280. list.concat(taicpu.op_sym(A_B,current_asmdata.RefAsmSymbol(procdef.mangledname)));
  2281. list.concatlist(current_procinfo.aktlocaldata);
  2282. current_procinfo.Free;
  2283. current_procinfo:=nil;
  2284. list.concat(Tai_symbol_end.Createname(labelname));
  2285. end;
  2286. procedure tcgarm.maybeadjustresult(list: TAsmList; op: TOpCg; size: tcgsize; dst: tregister);
  2287. const
  2288. overflowops = [OP_MUL,OP_SHL,OP_ADD,OP_SUB,OP_NOT,OP_NEG];
  2289. begin
  2290. if (op in overflowops) and
  2291. (size in [OS_8,OS_S8,OS_16,OS_S16]) then
  2292. a_load_reg_reg(list,OS_32,size,dst,dst);
  2293. end;
  2294. function tcgarm.get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  2295. var
  2296. stubname: string;
  2297. l1: tasmsymbol;
  2298. href: treference;
  2299. begin
  2300. stubname := 'L'+s+'$stub';
  2301. result := current_asmdata.getasmsymbol(stubname);
  2302. if assigned(result) then
  2303. exit;
  2304. if current_asmdata.asmlists[al_imports]=nil then
  2305. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  2306. new_section(current_asmdata.asmlists[al_imports],sec_stub,'',4);
  2307. result := current_asmdata.RefAsmSymbol(stubname);
  2308. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  2309. { register as a weak symbol if necessary }
  2310. if weak then
  2311. current_asmdata.weakrefasmsymbol(s);
  2312. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  2313. if not(cs_create_pic in current_settings.moduleswitches) then
  2314. begin
  2315. l1 := current_asmdata.RefAsmSymbol('L'+s+'$slp');
  2316. reference_reset_symbol(href,l1,0,sizeof(pint));
  2317. href.refaddr:=addr_full;
  2318. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LDR,NR_R12,href));
  2319. reference_reset_base(href,NR_R12,0,sizeof(pint));
  2320. current_asmdata.asmlists[al_imports].concat(taicpu.op_reg_ref(A_LDR,NR_R15,href));
  2321. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(l1,0));
  2322. l1 := current_asmdata.RefAsmSymbol('L'+s+'$lazy_ptr');
  2323. current_asmdata.asmlists[al_imports].concat(tai_const.create_sym(l1));
  2324. end
  2325. else
  2326. internalerror(2008100401);
  2327. new_section(current_asmdata.asmlists[al_imports],sec_data_lazy,'',sizeof(pint));
  2328. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(l1,0));
  2329. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  2330. current_asmdata.asmlists[al_imports].concat(tai_const.createname('dyld_stub_binding_helper',0));
  2331. end;
  2332. procedure tcg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  2333. begin
  2334. case op of
  2335. OP_NEG:
  2336. begin
  2337. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  2338. list.concat(taicpu.op_reg_reg_const(A_RSC,regdst.reghi,regsrc.reghi,0));
  2339. end;
  2340. OP_NOT:
  2341. begin
  2342. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reglo,regdst.reglo);
  2343. cg.a_op_reg_reg(list,OP_NOT,OS_INT,regsrc.reghi,regdst.reghi);
  2344. end;
  2345. else
  2346. a_op64_reg_reg_reg(list,op,size,regsrc,regdst,regdst);
  2347. end;
  2348. end;
  2349. procedure tcg64farm.a_op64_const_reg(list : TAsmList;op:TOpCG;size : tcgsize;value : int64;reg : tregister64);
  2350. begin
  2351. a_op64_const_reg_reg(list,op,size,value,reg,reg);
  2352. end;
  2353. procedure tcg64farm.a_op64_const_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64);
  2354. var
  2355. ovloc : tlocation;
  2356. begin
  2357. a_op64_const_reg_reg_checkoverflow(list,op,size,value,regsrc,regdst,false,ovloc);
  2358. end;
  2359. procedure tcg64farm.a_op64_reg_reg_reg(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64);
  2360. var
  2361. ovloc : tlocation;
  2362. begin
  2363. a_op64_reg_reg_reg_checkoverflow(list,op,size,regsrc1,regsrc2,regdst,false,ovloc);
  2364. end;
  2365. procedure tcg64farm.a_loadmm_intreg64_reg(list: TAsmList; mmsize: tcgsize; intreg: tregister64; mmreg: tregister);
  2366. begin
  2367. { this code can only be used to transfer raw data, not to perform
  2368. conversions }
  2369. if (mmsize<>OS_F64) then
  2370. internalerror(2009112405);
  2371. list.concat(taicpu.op_reg_reg_reg(A_FMDRR,mmreg,intreg.reglo,intreg.reghi));
  2372. end;
  2373. procedure tcg64farm.a_loadmm_reg_intreg64(list: TAsmList; mmsize: tcgsize; mmreg: tregister; intreg: tregister64);
  2374. begin
  2375. { this code can only be used to transfer raw data, not to perform
  2376. conversions }
  2377. if (mmsize<>OS_F64) then
  2378. internalerror(2009112406);
  2379. list.concat(taicpu.op_reg_reg_reg(A_FMRRD,intreg.reglo,intreg.reghi,mmreg));
  2380. end;
  2381. procedure tcg64farm.a_op64_const_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;value : int64;regsrc,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2382. var
  2383. tmpreg : tregister;
  2384. b : byte;
  2385. begin
  2386. ovloc.loc:=LOC_VOID;
  2387. case op of
  2388. OP_NEG,
  2389. OP_NOT :
  2390. internalerror(200306017);
  2391. end;
  2392. if (setflags or tcgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  2393. begin
  2394. case op of
  2395. OP_ADD:
  2396. begin
  2397. if is_shifter_const(lo(value),b) then
  2398. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  2399. else
  2400. begin
  2401. tmpreg:=cg.getintregister(list,OS_32);
  2402. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  2403. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  2404. end;
  2405. if is_shifter_const(hi(value),b) then
  2406. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,hi(value)),PF_S))
  2407. else
  2408. begin
  2409. tmpreg:=cg.getintregister(list,OS_32);
  2410. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  2411. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  2412. end;
  2413. end;
  2414. OP_SUB:
  2415. begin
  2416. if is_shifter_const(lo(value),b) then
  2417. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,lo(value)),PF_S))
  2418. else
  2419. begin
  2420. tmpreg:=cg.getintregister(list,OS_32);
  2421. cg.a_load_const_reg(list,OS_32,lo(value),tmpreg);
  2422. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  2423. end;
  2424. if is_shifter_const(hi(value),b) then
  2425. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))),PF_S))
  2426. else
  2427. begin
  2428. tmpreg:=cg.getintregister(list,OS_32);
  2429. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  2430. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg),PF_S));
  2431. end;
  2432. end;
  2433. else
  2434. internalerror(200502131);
  2435. end;
  2436. if size=OS_64 then
  2437. begin
  2438. { the arm has an weired opinion how flags for SUB/ADD are handled }
  2439. ovloc.loc:=LOC_FLAGS;
  2440. case op of
  2441. OP_ADD:
  2442. ovloc.resflags:=F_CS;
  2443. OP_SUB:
  2444. ovloc.resflags:=F_CC;
  2445. end;
  2446. end;
  2447. end
  2448. else
  2449. begin
  2450. case op of
  2451. OP_AND,OP_OR,OP_XOR:
  2452. begin
  2453. cg.a_op_const_reg_reg(list,op,OS_32,aint(lo(value)),regsrc.reglo,regdst.reglo);
  2454. cg.a_op_const_reg_reg(list,op,OS_32,aint(hi(value)),regsrc.reghi,regdst.reghi);
  2455. end;
  2456. OP_ADD:
  2457. begin
  2458. if is_shifter_const(aint(lo(value)),b) then
  2459. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_ADD,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  2460. else
  2461. begin
  2462. tmpreg:=cg.getintregister(list,OS_32);
  2463. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  2464. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  2465. end;
  2466. if is_shifter_const(aint(hi(value)),b) then
  2467. list.concat(taicpu.op_reg_reg_const(A_ADC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  2468. else
  2469. begin
  2470. tmpreg:=cg.getintregister(list,OS_32);
  2471. cg.a_load_const_reg(list,OS_32,aint(hi(value)),tmpreg);
  2472. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc.reghi,tmpreg));
  2473. end;
  2474. end;
  2475. OP_SUB:
  2476. begin
  2477. if is_shifter_const(aint(lo(value)),b) then
  2478. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_SUB,regdst.reglo,regsrc.reglo,aint(lo(value))),PF_S))
  2479. else
  2480. begin
  2481. tmpreg:=cg.getintregister(list,OS_32);
  2482. cg.a_load_const_reg(list,OS_32,aint(lo(value)),tmpreg);
  2483. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc.reglo,tmpreg),PF_S));
  2484. end;
  2485. if is_shifter_const(aint(hi(value)),b) then
  2486. list.concat(taicpu.op_reg_reg_const(A_SBC,regdst.reghi,regsrc.reghi,aint(hi(value))))
  2487. else
  2488. begin
  2489. tmpreg:=cg.getintregister(list,OS_32);
  2490. cg.a_load_const_reg(list,OS_32,hi(value),tmpreg);
  2491. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc.reghi,tmpreg));
  2492. end;
  2493. end;
  2494. else
  2495. internalerror(2003083101);
  2496. end;
  2497. end;
  2498. end;
  2499. procedure tcg64farm.a_op64_reg_reg_reg_checkoverflow(list: TAsmList;op:TOpCG;size : tcgsize;regsrc1,regsrc2,regdst : tregister64;setflags : boolean;var ovloc : tlocation);
  2500. begin
  2501. ovloc.loc:=LOC_VOID;
  2502. case op of
  2503. OP_NEG,
  2504. OP_NOT :
  2505. internalerror(200306017);
  2506. end;
  2507. if (setflags or tcgarm(cg).cgsetflags) and (op in [OP_ADD,OP_SUB]) then
  2508. begin
  2509. case op of
  2510. OP_ADD:
  2511. begin
  2512. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  2513. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi),PF_S));
  2514. end;
  2515. OP_SUB:
  2516. begin
  2517. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  2518. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi),PF_S));
  2519. end;
  2520. else
  2521. internalerror(2003083101);
  2522. end;
  2523. if size=OS_64 then
  2524. begin
  2525. { the arm has an weired opinion how flags for SUB/ADD are handled }
  2526. ovloc.loc:=LOC_FLAGS;
  2527. case op of
  2528. OP_ADD:
  2529. ovloc.resflags:=F_CS;
  2530. OP_SUB:
  2531. ovloc.resflags:=F_CC;
  2532. end;
  2533. end;
  2534. end
  2535. else
  2536. begin
  2537. case op of
  2538. OP_AND,OP_OR,OP_XOR:
  2539. begin
  2540. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reglo,regsrc2.reglo,regdst.reglo);
  2541. cg.a_op_reg_reg_reg(list,op,OS_32,regsrc1.reghi,regsrc2.reghi,regdst.reghi);
  2542. end;
  2543. OP_ADD:
  2544. begin
  2545. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ADD,regdst.reglo,regsrc1.reglo,regsrc2.reglo),PF_S));
  2546. list.concat(taicpu.op_reg_reg_reg(A_ADC,regdst.reghi,regsrc1.reghi,regsrc2.reghi));
  2547. end;
  2548. OP_SUB:
  2549. begin
  2550. list.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,regdst.reglo,regsrc2.reglo,regsrc1.reglo),PF_S));
  2551. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,regsrc2.reghi,regsrc1.reghi));
  2552. end;
  2553. else
  2554. internalerror(2003083101);
  2555. end;
  2556. end;
  2557. end;
  2558. procedure Tthumb2cgarm.init_register_allocators;
  2559. begin
  2560. inherited init_register_allocators;
  2561. { currently, we save R14 always, so we can use it }
  2562. if (target_info.system<>system_arm_darwin) then
  2563. rg[R_INTREGISTER]:=trgcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  2564. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  2565. RS_R9,RS_R10,RS_R12,RS_R14],first_int_imreg,[])
  2566. else
  2567. { r9 is not available on Darwin according to the llvm code generator }
  2568. rg[R_INTREGISTER]:=trgcputhumb2.create(R_INTREGISTER,R_SUBWHOLE,
  2569. [RS_R0,RS_R1,RS_R2,RS_R3,RS_R4,RS_R5,RS_R6,RS_R7,RS_R8,
  2570. RS_R10,RS_R12,RS_R14],first_int_imreg,[]);
  2571. rg[R_FPUREGISTER]:=trgcputhumb2.create(R_FPUREGISTER,R_SUBNONE,
  2572. [RS_F0,RS_F1,RS_F2,RS_F3,RS_F4,RS_F5,RS_F6,RS_F7],first_fpu_imreg,[]);
  2573. rg[R_MMREGISTER]:=trgcputhumb2.create(R_MMREGISTER,R_SUBNONE,
  2574. [RS_S0,RS_S1,RS_R2,RS_R3,RS_R4,RS_S31],first_mm_imreg,[]);
  2575. end;
  2576. procedure Tthumb2cgarm.done_register_allocators;
  2577. begin
  2578. rg[R_INTREGISTER].free;
  2579. rg[R_FPUREGISTER].free;
  2580. rg[R_MMREGISTER].free;
  2581. inherited done_register_allocators;
  2582. end;
  2583. procedure Tthumb2cgarm.a_call_reg(list : TAsmList;reg: tregister);
  2584. begin
  2585. list.concat(taicpu.op_reg(A_BLX, reg));
  2586. {
  2587. the compiler does not properly set this flag anymore in pass 1, and
  2588. for now we only need it after pass 2 (I hope) (JM)
  2589. if not(pi_do_call in current_procinfo.flags) then
  2590. internalerror(2003060703);
  2591. }
  2592. include(current_procinfo.flags,pi_do_call);
  2593. end;
  2594. procedure Tthumb2cgarm.a_load_const_reg(list : TAsmList; size: tcgsize; a : aint;reg : tregister);
  2595. var
  2596. imm_shift : byte;
  2597. l : tasmlabel;
  2598. hr : treference;
  2599. begin
  2600. if not(size in [OS_8,OS_S8,OS_16,OS_S16,OS_32,OS_S32]) then
  2601. internalerror(2002090902);
  2602. if is_shifter_const(a,imm_shift) then
  2603. list.concat(taicpu.op_reg_const(A_MOV,reg,a))
  2604. { loading of constants with mov and orr }
  2605. else if (is_shifter_const(a-byte(a),imm_shift)) then
  2606. begin
  2607. list.concat(taicpu.op_reg_const(A_MOV,reg,a-byte(a)));
  2608. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg,byte(a)));
  2609. end
  2610. else if (is_shifter_const(a-word(a),imm_shift)) and (is_shifter_const(word(a),imm_shift)) then
  2611. begin
  2612. list.concat(taicpu.op_reg_const(A_MOV,reg,a-word(a)));
  2613. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg,word(a)));
  2614. end
  2615. 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
  2616. begin
  2617. list.concat(taicpu.op_reg_const(A_MOV,reg,a-(dword(a) shl 8) shr 8));
  2618. list.concat(taicpu.op_reg_reg_const(A_ORR,reg,reg,(dword(a) shl 8) shr 8));
  2619. end
  2620. else
  2621. begin
  2622. reference_reset(hr,4);
  2623. current_asmdata.getjumplabel(l);
  2624. cg.a_label(current_procinfo.aktlocaldata,l);
  2625. hr.symboldata:=current_procinfo.aktlocaldata.last;
  2626. current_procinfo.aktlocaldata.concat(tai_const.Create_32bit(longint(a)));
  2627. hr.symbol:=l;
  2628. list.concat(taicpu.op_reg_ref(A_LDR,reg,hr));
  2629. end;
  2630. end;
  2631. procedure Tthumb2cgarm.a_load_ref_reg(list : TAsmList; fromsize, tosize : tcgsize;const Ref : treference;reg : tregister);
  2632. var
  2633. oppostfix:toppostfix;
  2634. usedtmpref: treference;
  2635. tmpreg,tmpreg2 : tregister;
  2636. so : tshifterop;
  2637. dir : integer;
  2638. begin
  2639. if (TCGSize2Size[FromSize] >= TCGSize2Size[ToSize]) then
  2640. FromSize := ToSize;
  2641. case FromSize of
  2642. { signed integer registers }
  2643. OS_8:
  2644. oppostfix:=PF_B;
  2645. OS_S8:
  2646. oppostfix:=PF_SB;
  2647. OS_16:
  2648. oppostfix:=PF_H;
  2649. OS_S16:
  2650. oppostfix:=PF_SH;
  2651. OS_32,
  2652. OS_S32:
  2653. oppostfix:=PF_None;
  2654. else
  2655. InternalError(200308297);
  2656. end;
  2657. if (ref.alignment in [1,2]) and (ref.alignment<tcgsize2size[fromsize]) then
  2658. begin
  2659. if target_info.endian=endian_big then
  2660. dir:=-1
  2661. else
  2662. dir:=1;
  2663. case FromSize of
  2664. OS_16,OS_S16:
  2665. begin
  2666. { only complicated references need an extra loadaddr }
  2667. if assigned(ref.symbol) or
  2668. (ref.index<>NR_NO) or
  2669. (ref.offset<-255) or
  2670. (ref.offset>4094) or
  2671. { sometimes the compiler reused registers }
  2672. (reg=ref.index) or
  2673. (reg=ref.base) then
  2674. begin
  2675. tmpreg2:=getintregister(list,OS_INT);
  2676. a_loadaddr_ref_reg(list,ref,tmpreg2);
  2677. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  2678. end
  2679. else
  2680. usedtmpref:=ref;
  2681. if target_info.endian=endian_big then
  2682. inc(usedtmpref.offset,1);
  2683. shifterop_reset(so);so.shiftmode:=SM_LSL;so.shiftimm:=8;
  2684. tmpreg:=getintregister(list,OS_INT);
  2685. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  2686. inc(usedtmpref.offset,dir);
  2687. if FromSize=OS_16 then
  2688. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg)
  2689. else
  2690. a_internal_load_ref_reg(list,OS_S8,OS_S8,usedtmpref,tmpreg);
  2691. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  2692. end;
  2693. OS_32,OS_S32:
  2694. begin
  2695. tmpreg:=getintregister(list,OS_INT);
  2696. { only complicated references need an extra loadaddr }
  2697. if assigned(ref.symbol) or
  2698. (ref.index<>NR_NO) or
  2699. (ref.offset<-255) or
  2700. (ref.offset>4092) or
  2701. { sometimes the compiler reused registers }
  2702. (reg=ref.index) or
  2703. (reg=ref.base) then
  2704. begin
  2705. tmpreg2:=getintregister(list,OS_INT);
  2706. a_loadaddr_ref_reg(list,ref,tmpreg2);
  2707. reference_reset_base(usedtmpref,tmpreg2,0,ref.alignment);
  2708. end
  2709. else
  2710. usedtmpref:=ref;
  2711. shifterop_reset(so);so.shiftmode:=SM_LSL;
  2712. if ref.alignment=2 then
  2713. begin
  2714. if target_info.endian=endian_big then
  2715. inc(usedtmpref.offset,2);
  2716. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,reg);
  2717. inc(usedtmpref.offset,dir*2);
  2718. a_internal_load_ref_reg(list,OS_16,OS_16,usedtmpref,tmpreg);
  2719. so.shiftimm:=16;
  2720. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  2721. end
  2722. else
  2723. begin
  2724. if target_info.endian=endian_big then
  2725. inc(usedtmpref.offset,3);
  2726. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,reg);
  2727. inc(usedtmpref.offset,dir);
  2728. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2729. so.shiftimm:=8;
  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:=16;
  2734. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  2735. inc(usedtmpref.offset,dir);
  2736. a_internal_load_ref_reg(list,OS_8,OS_8,usedtmpref,tmpreg);
  2737. so.shiftimm:=24;
  2738. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ORR,reg,reg,tmpreg,so));
  2739. end;
  2740. end
  2741. else
  2742. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  2743. end;
  2744. end
  2745. else
  2746. handle_load_store(list,A_LDR,oppostfix,reg,ref);
  2747. if (fromsize=OS_S8) and (tosize = OS_16) then
  2748. a_load_reg_reg(list,OS_16,OS_32,reg,reg);
  2749. end;
  2750. procedure Tthumb2cgarm.a_op_const_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; a: aint; src, dst: tregister;setflags : boolean;var ovloc : tlocation);
  2751. var
  2752. shift : byte;
  2753. tmpreg : tregister;
  2754. so : tshifterop;
  2755. l1 : longint;
  2756. begin
  2757. ovloc.loc:=LOC_VOID;
  2758. if {$ifopt R+}(a<>-2147483648) and{$endif} is_shifter_const(-a,shift) then
  2759. case op of
  2760. OP_ADD:
  2761. begin
  2762. op:=OP_SUB;
  2763. a:=aint(dword(-a));
  2764. end;
  2765. OP_SUB:
  2766. begin
  2767. op:=OP_ADD;
  2768. a:=aint(dword(-a));
  2769. end
  2770. end;
  2771. if is_shifter_const(a,shift) and not(op in [OP_IMUL,OP_MUL]) then
  2772. case op of
  2773. OP_NEG,OP_NOT,
  2774. OP_DIV,OP_IDIV:
  2775. internalerror(200308281);
  2776. OP_SHL:
  2777. begin
  2778. if a>32 then
  2779. internalerror(200308294);
  2780. if a<>0 then
  2781. begin
  2782. shifterop_reset(so);
  2783. so.shiftmode:=SM_LSL;
  2784. so.shiftimm:=a;
  2785. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  2786. end
  2787. else
  2788. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  2789. end;
  2790. OP_ROL:
  2791. begin
  2792. if a>32 then
  2793. internalerror(200308294);
  2794. if a<>0 then
  2795. begin
  2796. shifterop_reset(so);
  2797. so.shiftmode:=SM_ROR;
  2798. so.shiftimm:=32-a;
  2799. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  2800. end
  2801. else
  2802. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  2803. end;
  2804. OP_ROR:
  2805. begin
  2806. if a>32 then
  2807. internalerror(200308294);
  2808. if a<>0 then
  2809. begin
  2810. shifterop_reset(so);
  2811. so.shiftmode:=SM_ROR;
  2812. so.shiftimm:=a;
  2813. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  2814. end
  2815. else
  2816. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  2817. end;
  2818. OP_SHR:
  2819. begin
  2820. if a>32 then
  2821. internalerror(200308292);
  2822. shifterop_reset(so);
  2823. if a<>0 then
  2824. begin
  2825. so.shiftmode:=SM_LSR;
  2826. so.shiftimm:=a;
  2827. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  2828. end
  2829. else
  2830. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  2831. end;
  2832. OP_SAR:
  2833. begin
  2834. if a>32 then
  2835. internalerror(200308295);
  2836. if a<>0 then
  2837. begin
  2838. shifterop_reset(so);
  2839. so.shiftmode:=SM_ASR;
  2840. so.shiftimm:=a;
  2841. list.concat(taicpu.op_reg_reg_shifterop(A_MOV,dst,src,so));
  2842. end
  2843. else
  2844. list.concat(taicpu.op_reg_reg(A_MOV,dst,src));
  2845. end;
  2846. else
  2847. if (op in [OP_SUB, OP_ADD]) and
  2848. ((a < 0) or
  2849. (a > 4095)) then
  2850. begin
  2851. tmpreg:=getintregister(list,size);
  2852. a_load_const_reg(list, size, a, tmpreg);
  2853. list.concat(setoppostfix(taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmop[op],dst,src,tmpreg),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))
  2854. ));
  2855. end
  2856. else
  2857. list.concat(setoppostfix(
  2858. taicpu.op_reg_reg_const(op_reg_reg_opcg2asmop[op],dst,src,a),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))
  2859. ));
  2860. if (cgsetflags or setflags) and (size in [OS_8,OS_16,OS_32]) then
  2861. begin
  2862. ovloc.loc:=LOC_FLAGS;
  2863. case op of
  2864. OP_ADD:
  2865. ovloc.resflags:=F_CS;
  2866. OP_SUB:
  2867. ovloc.resflags:=F_CC;
  2868. end;
  2869. end;
  2870. end
  2871. else
  2872. begin
  2873. { there could be added some more sophisticated optimizations }
  2874. if (op in [OP_MUL,OP_IMUL]) and (a=1) then
  2875. a_load_reg_reg(list,size,size,src,dst)
  2876. else if (op in [OP_MUL,OP_IMUL]) and (a=0) then
  2877. a_load_const_reg(list,size,0,dst)
  2878. else if (op in [OP_IMUL]) and (a=-1) then
  2879. a_op_reg_reg(list,OP_NEG,size,src,dst)
  2880. { we do this here instead in the peephole optimizer because
  2881. it saves us a register }
  2882. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a,l1) and not(cgsetflags or setflags) then
  2883. a_op_const_reg_reg(list,OP_SHL,size,l1,src,dst)
  2884. { for example : b=a*5 -> b=a*4+a with add instruction and shl }
  2885. else if (op in [OP_MUL,OP_IMUL]) and ispowerof2(a-1,l1) and not(cgsetflags or setflags) then
  2886. begin
  2887. if l1>32 then{roozbeh does this ever happen?}
  2888. internalerror(200308296);
  2889. shifterop_reset(so);
  2890. so.shiftmode:=SM_LSL;
  2891. so.shiftimm:=l1;
  2892. list.concat(taicpu.op_reg_reg_reg_shifterop(A_ADD,dst,src,src,so));
  2893. end
  2894. else
  2895. begin
  2896. tmpreg:=getintregister(list,size);
  2897. a_load_const_reg(list,size,a,tmpreg);
  2898. a_op_reg_reg_reg_checkoverflow(list,op,size,tmpreg,src,dst,setflags,ovloc);
  2899. end;
  2900. end;
  2901. maybeadjustresult(list,op,size,dst);
  2902. end;
  2903. const
  2904. op_reg_reg_opcg2asmopThumb2: array[TOpCG] of tasmop =
  2905. (A_NONE,A_MOV,A_ADD,A_AND,A_UDIV,A_SDIV,A_MUL,A_MUL,A_NONE,A_MVN,A_ORR,
  2906. A_ASR,A_LSL,A_LSR,A_SUB,A_EOR,A_NONE,A_ROR);
  2907. procedure Tthumb2cgarm.a_op_reg_reg_reg_checkoverflow(list: TAsmList; op: TOpCg; size: tcgsize; src1, src2, dst: tregister;setflags : boolean;var ovloc : tlocation);
  2908. var
  2909. so : tshifterop;
  2910. tmpreg,overflowreg : tregister;
  2911. asmop : tasmop;
  2912. begin
  2913. ovloc.loc:=LOC_VOID;
  2914. case op of
  2915. OP_NEG,OP_NOT,
  2916. OP_DIV,OP_IDIV:
  2917. internalerror(200308281);
  2918. OP_ROL:
  2919. begin
  2920. if not(size in [OS_32,OS_S32]) then
  2921. internalerror(2008072801);
  2922. { simulate ROL by ror'ing 32-value }
  2923. tmpreg:=getintregister(list,OS_32);
  2924. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,32));
  2925. list.concat(taicpu.op_reg_reg_reg(A_SUB,src1,tmpreg,src1));
  2926. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  2927. end;
  2928. OP_ROR:
  2929. begin
  2930. if not(size in [OS_32,OS_S32]) then
  2931. internalerror(2008072802);
  2932. list.concat(taicpu.op_reg_reg_reg(A_ROR, dst, src2, src1));
  2933. end;
  2934. OP_IMUL,
  2935. OP_MUL:
  2936. begin
  2937. if cgsetflags or setflags then
  2938. begin
  2939. overflowreg:=getintregister(list,size);
  2940. if op=OP_IMUL then
  2941. asmop:=A_SMULL
  2942. else
  2943. asmop:=A_UMULL;
  2944. { the arm doesn't allow that rd and rm are the same }
  2945. if dst=src2 then
  2946. begin
  2947. if dst<>src1 then
  2948. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src1,src2))
  2949. else
  2950. begin
  2951. tmpreg:=getintregister(list,size);
  2952. a_load_reg_reg(list,size,size,src2,dst);
  2953. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,tmpreg,src1));
  2954. end;
  2955. end
  2956. else
  2957. list.concat(taicpu.op_reg_reg_reg_reg(asmop,dst,overflowreg,src2,src1));
  2958. if op=OP_IMUL then
  2959. begin
  2960. shifterop_reset(so);
  2961. so.shiftmode:=SM_ASR;
  2962. so.shiftimm:=31;
  2963. list.concat(taicpu.op_reg_reg_shifterop(A_CMP,overflowreg,dst,so));
  2964. end
  2965. else
  2966. list.concat(taicpu.op_reg_const(A_CMP,overflowreg,0));
  2967. ovloc.loc:=LOC_FLAGS;
  2968. ovloc.resflags:=F_NE;
  2969. end
  2970. else
  2971. begin
  2972. { the arm doesn't allow that rd and rm are the same }
  2973. if dst=src2 then
  2974. begin
  2975. if dst<>src1 then
  2976. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src1,src2))
  2977. else
  2978. begin
  2979. tmpreg:=getintregister(list,size);
  2980. a_load_reg_reg(list,size,size,src2,dst);
  2981. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,tmpreg,src1));
  2982. end;
  2983. end
  2984. else
  2985. list.concat(taicpu.op_reg_reg_reg(A_MUL,dst,src2,src1));
  2986. end;
  2987. end;
  2988. else
  2989. list.concat(setoppostfix(
  2990. taicpu.op_reg_reg_reg(op_reg_reg_opcg2asmopThumb2[op],dst,src2,src1),toppostfix(ord(cgsetflags or setflags)*ord(PF_S))
  2991. ));
  2992. end;
  2993. maybeadjustresult(list,op,size,dst);
  2994. end;
  2995. procedure Tthumb2cgarm.g_flags2reg(list: TAsmList; size: TCgSize; const f: TResFlags; reg: TRegister);
  2996. var item: taicpu;
  2997. begin
  2998. item := setcondition(taicpu.op_reg_const(A_MOV,reg,1),flags_to_cond(f));
  2999. list.concat(item);
  3000. list.insertbefore(taicpu.op_cond(A_IT, flags_to_cond(f)), item);
  3001. item := setcondition(taicpu.op_reg_const(A_MOV,reg,0),inverse_cond(flags_to_cond(f)));
  3002. list.concat(item);
  3003. list.insertbefore(taicpu.op_cond(A_IT, inverse_cond(flags_to_cond(f))), item);
  3004. end;
  3005. procedure Tthumb2cgarm.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  3006. var
  3007. ref : treference;
  3008. shift : byte;
  3009. firstfloatreg,lastfloatreg,
  3010. r : byte;
  3011. regs : tcpuregisterset;
  3012. stackmisalignment: pint;
  3013. begin
  3014. LocalSize:=align(LocalSize,4);
  3015. { call instruction does not put anything on the stack }
  3016. stackmisalignment:=0;
  3017. if not(nostackframe) then
  3018. begin
  3019. firstfloatreg:=RS_NO;
  3020. { save floating point registers? }
  3021. for r:=RS_F0 to RS_F7 do
  3022. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  3023. begin
  3024. if firstfloatreg=RS_NO then
  3025. firstfloatreg:=r;
  3026. lastfloatreg:=r;
  3027. inc(stackmisalignment,12);
  3028. end;
  3029. a_reg_alloc(list,NR_STACK_POINTER_REG);
  3030. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3031. begin
  3032. a_reg_alloc(list,NR_FRAME_POINTER_REG);
  3033. a_reg_alloc(list,NR_R12);
  3034. list.concat(taicpu.op_reg_reg(A_MOV,NR_R12,NR_STACK_POINTER_REG));
  3035. end;
  3036. { save int registers }
  3037. reference_reset(ref,4);
  3038. ref.index:=NR_STACK_POINTER_REG;
  3039. ref.addressmode:=AM_PREINDEXED;
  3040. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3041. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3042. regs:=regs+[RS_FRAME_POINTER_REG,RS_R14]
  3043. else if (regs<>[]) or (pi_do_call in current_procinfo.flags) then
  3044. include(regs,RS_R14);
  3045. if regs<>[] then
  3046. begin
  3047. for r:=RS_R0 to RS_R15 do
  3048. if (r in regs) then
  3049. inc(stackmisalignment,4);
  3050. list.concat(setoppostfix(taicpu.op_ref_regset(A_STM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  3051. end;
  3052. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3053. list.concat(taicpu.op_reg_reg(A_MOV,NR_FRAME_POINTER_REG,NR_R12));
  3054. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  3055. if (LocalSize<>0) or
  3056. ((stackmisalignment<>0) and
  3057. ((pi_do_call in current_procinfo.flags) or
  3058. (po_assembler in current_procinfo.procdef.procoptions))) then
  3059. begin
  3060. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3061. if not(is_shifter_const(localsize,shift)) then
  3062. begin
  3063. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3064. a_reg_alloc(list,NR_R12);
  3065. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  3066. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  3067. a_reg_dealloc(list,NR_R12);
  3068. end
  3069. else
  3070. begin
  3071. a_reg_dealloc(list,NR_R12);
  3072. list.concat(taicpu.op_reg_reg_const(A_SUB,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  3073. end;
  3074. end;
  3075. if firstfloatreg<>RS_NO then
  3076. begin
  3077. reference_reset(ref,4);
  3078. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  3079. begin
  3080. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  3081. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  3082. ref.base:=NR_R12;
  3083. end
  3084. else
  3085. begin
  3086. ref.base:=current_procinfo.framepointer;
  3087. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  3088. end;
  3089. list.concat(taicpu.op_reg_const_ref(A_SFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  3090. lastfloatreg-firstfloatreg+1,ref));
  3091. end;
  3092. end;
  3093. end;
  3094. procedure Tthumb2cgarm.g_proc_exit(list : TAsmList;parasize : longint;nostackframe:boolean);
  3095. var
  3096. ref : treference;
  3097. firstfloatreg,lastfloatreg,
  3098. r : byte;
  3099. shift : byte;
  3100. regs : tcpuregisterset;
  3101. LocalSize : longint;
  3102. stackmisalignment: pint;
  3103. begin
  3104. if not(nostackframe) then
  3105. begin
  3106. stackmisalignment:=0;
  3107. { restore floating point register }
  3108. firstfloatreg:=RS_NO;
  3109. { save floating point registers? }
  3110. for r:=RS_F0 to RS_F7 do
  3111. if r in rg[R_FPUREGISTER].used_in_proc-paramanager.get_volatile_registers_fpu(pocall_stdcall) then
  3112. begin
  3113. if firstfloatreg=RS_NO then
  3114. firstfloatreg:=r;
  3115. lastfloatreg:=r;
  3116. { floating point register space is already included in
  3117. localsize below by calc_stackframe_size
  3118. inc(stackmisalignment,12);
  3119. }
  3120. end;
  3121. if firstfloatreg<>RS_NO then
  3122. begin
  3123. reference_reset(ref,4);
  3124. if tg.direction*tarmprocinfo(current_procinfo).floatregstart>=1023 then
  3125. begin
  3126. a_load_const_reg(list,OS_ADDR,-tarmprocinfo(current_procinfo).floatregstart,NR_R12);
  3127. list.concat(taicpu.op_reg_reg_reg(A_SUB,NR_R12,current_procinfo.framepointer,NR_R12));
  3128. ref.base:=NR_R12;
  3129. end
  3130. else
  3131. begin
  3132. ref.base:=current_procinfo.framepointer;
  3133. ref.offset:=tarmprocinfo(current_procinfo).floatregstart;
  3134. end;
  3135. list.concat(taicpu.op_reg_const_ref(A_LFM,newreg(R_FPUREGISTER,firstfloatreg,R_SUBWHOLE),
  3136. lastfloatreg-firstfloatreg+1,ref));
  3137. end;
  3138. regs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(pocall_stdcall);
  3139. if (pi_do_call in current_procinfo.flags) or (regs<>[]) then
  3140. begin
  3141. exclude(regs,RS_R14);
  3142. include(regs,RS_R15);
  3143. end;
  3144. if (current_procinfo.framepointer<>NR_STACK_POINTER_REG) then
  3145. regs:=regs+[RS_FRAME_POINTER_REG,RS_R15];
  3146. for r:=RS_R0 to RS_R15 do
  3147. if (r in regs) then
  3148. inc(stackmisalignment,4);
  3149. stackmisalignment:=stackmisalignment mod current_settings.alignment.localalignmax;
  3150. if (current_procinfo.framepointer=NR_STACK_POINTER_REG) then
  3151. begin
  3152. LocalSize:=current_procinfo.calc_stackframe_size;
  3153. if (LocalSize<>0) or
  3154. ((stackmisalignment<>0) and
  3155. ((pi_do_call in current_procinfo.flags) or
  3156. (po_assembler in current_procinfo.procdef.procoptions))) then
  3157. begin
  3158. localsize:=align(localsize+stackmisalignment,current_settings.alignment.localalignmax)-stackmisalignment;
  3159. if not(is_shifter_const(LocalSize,shift)) then
  3160. begin
  3161. a_reg_alloc(list,NR_R12);
  3162. a_load_const_reg(list,OS_ADDR,LocalSize,NR_R12);
  3163. list.concat(taicpu.op_reg_reg_reg(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,NR_R12));
  3164. a_reg_dealloc(list,NR_R12);
  3165. end
  3166. else
  3167. begin
  3168. list.concat(taicpu.op_reg_reg_const(A_ADD,NR_STACK_POINTER_REG,NR_STACK_POINTER_REG,LocalSize));
  3169. end;
  3170. end;
  3171. if regs=[] then
  3172. list.concat(taicpu.op_reg_reg(A_MOV,NR_R15,NR_R14))
  3173. else
  3174. begin
  3175. reference_reset(ref,4);
  3176. ref.index:=NR_STACK_POINTER_REG;
  3177. ref.addressmode:=AM_PREINDEXED;
  3178. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_FD));
  3179. end;
  3180. end
  3181. else
  3182. begin
  3183. { restore int registers and return }
  3184. list.concat(taicpu.op_reg_reg(A_MOV, NR_STACK_POINTER_REG, NR_FRAME_POINTER_REG));
  3185. reference_reset(ref,4);
  3186. ref.index:=NR_STACK_POINTER_REG;
  3187. list.concat(setoppostfix(taicpu.op_ref_regset(A_LDM,ref,R_INTREGISTER,R_SUBWHOLE,regs),PF_DB));
  3188. end;
  3189. end
  3190. else
  3191. list.concat(taicpu.op_reg_reg(A_MOV,NR_PC,NR_R14));
  3192. end;
  3193. function Tthumb2cgarm.handle_load_store(list:TAsmList;op: tasmop;oppostfix : toppostfix;reg:tregister;ref: treference):treference;
  3194. var
  3195. tmpreg : tregister;
  3196. tmpref : treference;
  3197. l : tasmlabel;
  3198. so: tshifterop;
  3199. begin
  3200. tmpreg:=NR_NO;
  3201. { Be sure to have a base register }
  3202. if (ref.base=NR_NO) then
  3203. begin
  3204. if ref.shiftmode<>SM_None then
  3205. internalerror(200308294);
  3206. ref.base:=ref.index;
  3207. ref.index:=NR_NO;
  3208. end;
  3209. { absolute symbols can't be handled directly, we've to store the symbol reference
  3210. in the text segment and access it pc relative
  3211. For now, we assume that references where base or index equals to PC are already
  3212. relative, all other references are assumed to be absolute and thus they need
  3213. to be handled extra.
  3214. A proper solution would be to change refoptions to a set and store the information
  3215. if the symbol is absolute or relative there.
  3216. }
  3217. if (assigned(ref.symbol) and
  3218. not(is_pc(ref.base)) and
  3219. not(is_pc(ref.index))
  3220. ) or
  3221. { [#xxx] isn't a valid address operand }
  3222. ((ref.base=NR_NO) and (ref.index=NR_NO)) or
  3223. //(ref.offset<-4095) or
  3224. (ref.offset<-255) or
  3225. (ref.offset>4095) or
  3226. ((oppostfix in [PF_SB,PF_H,PF_SH]) and
  3227. ((ref.offset<-255) or
  3228. (ref.offset>255)
  3229. )
  3230. ) or
  3231. ((op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) and
  3232. ((ref.offset<-1020) or
  3233. (ref.offset>1020) or
  3234. { the usual pc relative symbol handling assumes possible offsets of +/- 4095 }
  3235. assigned(ref.symbol)
  3236. )
  3237. ) then
  3238. begin
  3239. reference_reset(tmpref,4);
  3240. { load symbol }
  3241. tmpreg:=getintregister(list,OS_INT);
  3242. if assigned(ref.symbol) then
  3243. begin
  3244. current_asmdata.getjumplabel(l);
  3245. cg.a_label(current_procinfo.aktlocaldata,l);
  3246. tmpref.symboldata:=current_procinfo.aktlocaldata.last;
  3247. current_procinfo.aktlocaldata.concat(tai_const.create_sym_offset(ref.symbol,ref.offset));
  3248. { load consts entry }
  3249. tmpref.symbol:=l;
  3250. tmpref.base:=NR_R15;
  3251. list.concat(taicpu.op_reg_ref(A_LDR,tmpreg,tmpref));
  3252. { in case of LDF/STF, we got rid of the NR_R15 }
  3253. if is_pc(ref.base) then
  3254. ref.base:=NR_NO;
  3255. if is_pc(ref.index) then
  3256. ref.index:=NR_NO;
  3257. end
  3258. else
  3259. a_load_const_reg(list,OS_ADDR,ref.offset,tmpreg);
  3260. if (ref.base<>NR_NO) then
  3261. begin
  3262. if ref.index<>NR_NO then
  3263. begin
  3264. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  3265. ref.base:=tmpreg;
  3266. end
  3267. else
  3268. begin
  3269. ref.index:=tmpreg;
  3270. ref.shiftimm:=0;
  3271. ref.signindex:=1;
  3272. ref.shiftmode:=SM_None;
  3273. end;
  3274. end
  3275. else
  3276. ref.base:=tmpreg;
  3277. ref.offset:=0;
  3278. ref.symbol:=nil;
  3279. end;
  3280. if (ref.base<>NR_NO) and (ref.index<>NR_NO) and (ref.offset<>0) then
  3281. begin
  3282. if tmpreg<>NR_NO then
  3283. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,tmpreg,tmpreg)
  3284. else
  3285. begin
  3286. tmpreg:=getintregister(list,OS_ADDR);
  3287. a_op_const_reg_reg(list,OP_ADD,OS_ADDR,ref.offset,ref.base,tmpreg);
  3288. ref.base:=tmpreg;
  3289. end;
  3290. ref.offset:=0;
  3291. end;
  3292. { Hack? Thumb2 doesn't allow PC indexed addressing modes(although it does in the specification) }
  3293. if (ref.base=NR_R15) and (ref.index<>NR_NO) and (ref.shiftmode <> sm_none) then
  3294. begin
  3295. tmpreg:=getintregister(list,OS_ADDR);
  3296. list.concat(taicpu.op_reg_reg(A_MOV, tmpreg, NR_R15));
  3297. ref.base := tmpreg;
  3298. end;
  3299. { floating point operations have only limited references
  3300. we expect here, that a base is already set }
  3301. if (op in [A_LDF,A_STF,A_FLDS,A_FLDD,A_FSTS,A_FSTD]) and (ref.index<>NR_NO) then
  3302. begin
  3303. if ref.shiftmode<>SM_none then
  3304. internalerror(200309121);
  3305. if tmpreg<>NR_NO then
  3306. begin
  3307. if ref.base=tmpreg then
  3308. begin
  3309. if ref.signindex<0 then
  3310. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,tmpreg,ref.index))
  3311. else
  3312. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,tmpreg,ref.index));
  3313. ref.index:=NR_NO;
  3314. end
  3315. else
  3316. begin
  3317. if ref.index<>tmpreg then
  3318. internalerror(200403161);
  3319. if ref.signindex<0 then
  3320. list.concat(taicpu.op_reg_reg_reg(A_SUB,tmpreg,ref.base,tmpreg))
  3321. else
  3322. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,tmpreg));
  3323. ref.base:=tmpreg;
  3324. ref.index:=NR_NO;
  3325. end;
  3326. end
  3327. else
  3328. begin
  3329. tmpreg:=getintregister(list,OS_ADDR);
  3330. list.concat(taicpu.op_reg_reg_reg(A_ADD,tmpreg,ref.base,ref.index));
  3331. ref.base:=tmpreg;
  3332. ref.index:=NR_NO;
  3333. end;
  3334. end;
  3335. list.concat(setoppostfix(taicpu.op_reg_ref(op,reg,ref),oppostfix));
  3336. Result := ref;
  3337. end;
  3338. procedure tthumb2cg64farm.a_op64_reg_reg(list : TAsmList;op:TOpCG;size : tcgsize;regsrc,regdst : tregister64);
  3339. var tmpreg: tregister;
  3340. begin
  3341. case op of
  3342. OP_NEG:
  3343. begin
  3344. list.concat(setoppostfix(taicpu.op_reg_reg_const(A_RSB,regdst.reglo,regsrc.reglo,0),PF_S));
  3345. tmpreg:=cg.getintregister(list,OS_32);
  3346. list.concat(taicpu.op_reg_const(A_MOV,tmpreg,0));
  3347. list.concat(taicpu.op_reg_reg_reg(A_SBC,regdst.reghi,tmpreg,regsrc.reghi));
  3348. end;
  3349. else
  3350. inherited a_op64_reg_reg(list, op, size, regsrc, regdst);
  3351. end;
  3352. end;
  3353. procedure create_codegen;
  3354. begin
  3355. if current_settings.cputype in cpu_thumb2 then
  3356. begin
  3357. cg:=tthumb2cgarm.create;
  3358. cg64:=tthumb2cg64farm.create;
  3359. casmoptimizer:=TCpuThumb2AsmOptimizer;
  3360. end
  3361. else
  3362. begin
  3363. cg:=tarmcgarm.create;
  3364. cg64:=tcg64farm.create;
  3365. casmoptimizer:=TCpuAsmOptimizer;
  3366. end;
  3367. end;
  3368. end.