cgcpu.pas 158 KB

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