cgcpu.pas 164 KB

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