cgx86.pas 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710
  1. {
  2. Copyright (c) 1998-2005 by Florian Klaempfl
  3. This unit implements the common parts of the code generator for the i386 and the x86-64.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. { This unit implements the common parts of the code generator for the i386 and the x86-64.
  18. }
  19. unit cgx86;
  20. {$i fpcdefs.inc}
  21. interface
  22. uses
  23. globtype,
  24. cgbase,cgutils,cgobj,
  25. aasmbase,aasmtai,aasmdata,aasmcpu,
  26. cpubase,cpuinfo,rgx86,
  27. symconst,symtype,symdef,
  28. parabase;
  29. type
  30. { tcgx86 }
  31. tcgx86 = class(tcg)
  32. rgfpu : Trgx86fpu;
  33. procedure done_register_allocators;override;
  34. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;override;
  35. function getmmxregister(list:TAsmList):Tregister;
  36. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;override;
  37. procedure getcpuregister(list:TAsmList;r:Tregister);override;
  38. procedure ungetcpuregister(list:TAsmList;r:Tregister);override;
  39. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  40. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  41. function uses_registers(rt:Tregistertype):boolean;override;
  42. procedure add_reg_instruction(instr:Tai;r:tregister);override;
  43. procedure dec_fpu_stack;
  44. procedure inc_fpu_stack;
  45. procedure a_call_name(list : TAsmList;const s : string; weak: boolean);override;
  46. procedure a_call_name_near(list : TAsmList;const s : string; weak: boolean);
  47. procedure a_call_name_static(list : TAsmList;const s : string);override;
  48. procedure a_call_name_static_near(list : TAsmList;const s : string);
  49. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  50. procedure a_call_reg_near(list : TAsmList;reg : tregister);
  51. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister); override;
  52. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference); override;
  53. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  54. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  55. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  56. procedure a_op_ref(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference); override;
  57. {$ifndef i8086}
  58. procedure a_op_const_reg_reg(list : TAsmList; op : Topcg; size : Tcgsize; a : tcgint; src,dst : Tregister); override;
  59. procedure a_op_reg_reg_reg(list : TAsmList; op : TOpCg; size : tcgsize; src1,src2,dst : tregister); override;
  60. {$endif not i8086}
  61. { move instructions }
  62. procedure a_load_const_reg(list : TAsmList; tosize: tcgsize; a : tcgint;reg : tregister);override;
  63. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);override;
  64. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);override;
  65. { final as a_load_ref_reg_internal() should be overridden instead }
  66. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);override;final;
  67. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize: tcgsize;reg1,reg2 : tregister);override;
  68. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  69. { bit scan instructions }
  70. procedure a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister); override;
  71. { fpu move instructions }
  72. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  73. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  74. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  75. procedure a_loadfpu_ref_cgpara(list : TAsmList;size : tcgsize;const ref : treference;const cgpara : TCGPara); override;
  76. { vector register move instructions }
  77. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  78. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  79. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  80. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  81. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);override;
  82. procedure a_opmm_ref_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;const ref : treference;src,dst : tregister;shuffle : pmmshuffle);override;
  83. procedure a_opmm_reg_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;src1,src2,dst : tregister;shuffle : pmmshuffle);override;
  84. { comparison operations }
  85. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  86. l : tasmlabel);override;
  87. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  88. l : tasmlabel);override;
  89. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  90. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister; l : tasmlabel); override;
  91. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); override;
  92. procedure a_jmp_name(list : TAsmList;const s : string);override;
  93. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  94. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  95. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  96. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference); override;
  97. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : tcgint);override;
  98. { entry/exit code helpers }
  99. procedure g_profilecode(list : TAsmList);override;
  100. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  101. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  102. procedure g_save_registers(list: TAsmList); override;
  103. procedure g_restore_registers(list: TAsmList); override;
  104. procedure g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);override;
  105. procedure make_simple_ref(list:TAsmList;var ref: treference);inline;
  106. procedure make_direct_ref(list:TAsmList;var ref: treference);
  107. function get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  108. procedure generate_leave(list : TAsmList);
  109. protected
  110. procedure a_load_ref_reg_internal(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister;isdirect:boolean);virtual;
  111. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  112. procedure check_register_size(size:tcgsize;reg:tregister);
  113. procedure opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  114. procedure opmm_loc_reg_reg(list : TAsmList;Op : TOpCG;size : tcgsize;loc : tlocation;src,dst : tregister;shuffle : pmmshuffle);
  115. procedure sizes2load(s1,s2 : tcgsize;var op: tasmop; var s3: topsize);
  116. procedure floatload(list: TAsmList; t : tcgsize;const ref : treference);
  117. procedure floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  118. procedure floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  119. procedure floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  120. procedure internal_restore_regs(list: TAsmList; use_pop: boolean);
  121. procedure make_simple_ref(list:TAsmList;var ref: treference;isdirect:boolean);
  122. end;
  123. const
  124. {$if defined(x86_64)}
  125. TCGSize2OpSize: Array[tcgsize] of topsize =
  126. (S_NO,S_B,S_W,S_L,S_Q,S_XMM,S_B,S_W,S_L,S_Q,S_XMM,
  127. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  128. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,S_ZMM);
  129. {$elseif defined(i386)}
  130. TCGSize2OpSize: Array[tcgsize] of topsize =
  131. (S_NO,S_B,S_W,S_L,S_L,S_T,S_B,S_W,S_L,S_L,S_L,
  132. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  133. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,S_ZMM);
  134. {$elseif defined(i8086)}
  135. TCGSize2OpSize: Array[tcgsize] of topsize =
  136. (S_NO,S_B,S_W,S_W,S_W,S_T,S_B,S_W,S_W,S_W,S_W,
  137. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  138. S_NO,S_NO,S_NO,S_MD,S_XMM,S_YMM,S_ZMM);
  139. {$endif}
  140. {$ifndef NOTARGETWIN}
  141. winstackpagesize = 4096;
  142. {$endif NOTARGETWIN}
  143. function UseIncDec: boolean;
  144. { returns true, if the compiler should use leave instead of mov/pop }
  145. function UseLeave: boolean;
  146. { Gets the byte alignment of a reference }
  147. function GetRefAlignment(ref: treference): Byte;
  148. implementation
  149. uses
  150. globals,verbose,systems,cutils,
  151. symcpu,
  152. paramgr,procinfo,
  153. tgobj,ncgutil;
  154. { modern CPUs prefer add/sub over inc/dec because add/sub break instructions dependencies on flags
  155. because they modify all flags }
  156. function UseIncDec: boolean;
  157. begin
  158. {$if defined(x86_64)}
  159. Result:=cs_opt_size in current_settings.optimizerswitches;
  160. {$elseif defined(i386)}
  161. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.cputype in [cpu_386]);
  162. {$elseif defined(i8086)}
  163. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.cputype in [cpu_8086..cpu_386]);
  164. {$endif}
  165. end;
  166. function UseLeave: boolean;
  167. begin
  168. {$if defined(x86_64)}
  169. { Modern processors should be happy with mov;pop, maybe except older AMDs }
  170. Result:=cs_opt_size in current_settings.optimizerswitches;
  171. {$elseif defined(i386)}
  172. Result:=(cs_opt_size in current_settings.optimizerswitches) or (current_settings.optimizecputype<cpu_Pentium2);
  173. {$elseif defined(i8086)}
  174. Result:=current_settings.cputype>=cpu_186;
  175. {$endif}
  176. end;
  177. function GetRefAlignment(ref: treference): Byte; {$IFDEF USEINLINE}inline;{$ENDIF}
  178. begin
  179. {$ifdef x86_64}
  180. { The stack pointer and base pointer will be aligned to 16-byte boundaries if the machine code is well-behaved }
  181. if (ref.base = NR_RSP) or (ref.base = NR_RBP) then
  182. begin
  183. if (ref.index = NR_NO) and ((ref.offset mod 16) = 0) then
  184. Result := 16
  185. else
  186. Result := ref.alignment;
  187. end
  188. else
  189. {$endif x86_64}
  190. Result := ref.alignment;
  191. end;
  192. const
  193. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_MOV,A_ADD,A_AND,A_DIV,
  194. A_IDIV,A_IMUL,A_MUL,A_NEG,A_NOT,A_OR,
  195. A_SAR,A_SHL,A_SHR,A_SUB,A_XOR,A_ROL,A_ROR);
  196. TOpCmp2AsmCond: Array[topcmp] of TAsmCond = (C_NONE,
  197. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  198. procedure Tcgx86.done_register_allocators;
  199. begin
  200. rg[R_INTREGISTER].free;
  201. rg[R_MMREGISTER].free;
  202. rg[R_MMXREGISTER].free;
  203. rgfpu.free;
  204. inherited done_register_allocators;
  205. end;
  206. function Tcgx86.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  207. begin
  208. result:=rgfpu.getregisterfpu(list);
  209. end;
  210. function Tcgx86.getmmxregister(list:TAsmList):Tregister;
  211. begin
  212. if not assigned(rg[R_MMXREGISTER]) then
  213. internalerror(2003121204);
  214. result:=rg[R_MMXREGISTER].getregister(list,R_SUBNONE);
  215. end;
  216. function Tcgx86.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  217. begin
  218. if not assigned(rg[R_MMREGISTER]) then
  219. internalerror(2003121234);
  220. case size of
  221. OS_F64:
  222. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMD);
  223. OS_F32:
  224. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMS);
  225. OS_M64:
  226. result:=rg[R_MMREGISTER].getregister(list,R_SUBQ);
  227. OS_128,
  228. OS_M128,
  229. OS_F128:
  230. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMX); { R_SUBMMWHOLE seems a bit dangerous and ambiguous, so changed to R_SUBMMX. [Kit] }
  231. OS_M256:
  232. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMY);
  233. OS_M512:
  234. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMZ);
  235. else
  236. internalerror(200506041);
  237. end;
  238. end;
  239. procedure Tcgx86.getcpuregister(list:TAsmList;r:Tregister);
  240. begin
  241. if getregtype(r)=R_FPUREGISTER then
  242. internalerror(2003121210)
  243. else
  244. inherited getcpuregister(list,r);
  245. end;
  246. procedure tcgx86.ungetcpuregister(list:TAsmList;r:Tregister);
  247. begin
  248. if getregtype(r)=R_FPUREGISTER then
  249. rgfpu.ungetregisterfpu(list,r)
  250. else
  251. inherited ungetcpuregister(list,r);
  252. end;
  253. procedure Tcgx86.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  254. begin
  255. if rt<>R_FPUREGISTER then
  256. inherited alloccpuregisters(list,rt,r);
  257. end;
  258. procedure Tcgx86.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  259. begin
  260. if rt<>R_FPUREGISTER then
  261. inherited dealloccpuregisters(list,rt,r);
  262. end;
  263. function Tcgx86.uses_registers(rt:Tregistertype):boolean;
  264. begin
  265. if rt=R_FPUREGISTER then
  266. result:=false
  267. else
  268. result:=inherited uses_registers(rt);
  269. end;
  270. procedure tcgx86.add_reg_instruction(instr:Tai;r:tregister);
  271. begin
  272. if getregtype(r)<>R_FPUREGISTER then
  273. inherited add_reg_instruction(instr,r);
  274. end;
  275. procedure tcgx86.dec_fpu_stack;
  276. begin
  277. if rgfpu.fpuvaroffset<=0 then
  278. internalerror(200604201);
  279. dec(rgfpu.fpuvaroffset);
  280. end;
  281. procedure tcgx86.inc_fpu_stack;
  282. begin
  283. if rgfpu.fpuvaroffset>=7 then
  284. internalerror(2012062901);
  285. inc(rgfpu.fpuvaroffset);
  286. end;
  287. { Range check must be disabled explicitly as the code serves
  288. on three different architecture sizes }
  289. {$R-}
  290. {****************************************************************************
  291. This is private property, keep out! :)
  292. ****************************************************************************}
  293. procedure tcgx86.sizes2load(s1,s2 : tcgsize; var op: tasmop; var s3: topsize);
  294. begin
  295. { ensure to have always valid sizes }
  296. if s1=OS_NO then
  297. s1:=s2;
  298. if s2=OS_NO then
  299. s2:=s1;
  300. case s2 of
  301. OS_8,OS_S8 :
  302. if S1 in [OS_8,OS_S8] then
  303. s3 := S_B
  304. else
  305. internalerror(200109221);
  306. OS_16,OS_S16:
  307. case s1 of
  308. OS_8,OS_S8:
  309. s3 := S_BW;
  310. OS_16,OS_S16:
  311. s3 := S_W;
  312. else
  313. internalerror(200109222);
  314. end;
  315. OS_32,OS_S32:
  316. case s1 of
  317. OS_8,OS_S8:
  318. s3 := S_BL;
  319. OS_16,OS_S16:
  320. s3 := S_WL;
  321. OS_32,OS_S32:
  322. s3 := S_L;
  323. else
  324. internalerror(200109223);
  325. end;
  326. {$ifdef x86_64}
  327. OS_64,OS_S64:
  328. case s1 of
  329. OS_8:
  330. s3 := S_BL;
  331. OS_S8:
  332. s3 := S_BQ;
  333. OS_16:
  334. s3 := S_WL;
  335. OS_S16:
  336. s3 := S_WQ;
  337. OS_32:
  338. s3 := S_L;
  339. OS_S32:
  340. s3 := S_LQ;
  341. OS_64,OS_S64:
  342. s3 := S_Q;
  343. else
  344. internalerror(200304302);
  345. end;
  346. {$endif x86_64}
  347. else
  348. internalerror(200109227);
  349. end;
  350. if s3 in [S_B,S_W,S_L,S_Q] then
  351. op := A_MOV
  352. else if s1 in [OS_8,OS_16,OS_32,OS_64] then
  353. op := A_MOVZX
  354. else
  355. {$ifdef x86_64}
  356. if s3 in [S_LQ] then
  357. op := A_MOVSXD
  358. else
  359. {$endif x86_64}
  360. op := A_MOVSX;
  361. end;
  362. procedure tcgx86.make_simple_ref(list:TAsmList;var ref: treference);
  363. begin
  364. make_simple_ref(list,ref,false);
  365. end;
  366. procedure tcgx86.make_simple_ref(list:TAsmList;var ref: treference;isdirect:boolean);
  367. var
  368. hreg : tregister;
  369. href : treference;
  370. {$ifndef x86_64}
  371. add_hreg: boolean;
  372. {$endif not x86_64}
  373. begin
  374. hreg:=NR_NO;
  375. { make_simple_ref() may have already been called earlier, and in that
  376. case make sure we don't perform the PIC-simplifications twice }
  377. if (ref.refaddr in [addr_pic,addr_pic_no_got]) then
  378. exit;
  379. { handle indirect symbols first }
  380. if not isdirect then
  381. make_direct_ref(list,ref);
  382. {$if defined(x86_64)}
  383. { Only 32bit is allowed }
  384. { Note that this isn't entirely correct: for RIP-relative targets/memory models,
  385. it is actually (offset+@symbol-RIP) that should fit into 32 bits. Since two last
  386. members aren't known until link time, ABIs place very pessimistic limits
  387. on offset values, e.g. SysV AMD64 allows +/-$1000000 (16 megabytes) }
  388. if ((ref.offset<low(longint)) or (ref.offset>high(longint))) or
  389. ((cs_large in current_settings.globalswitches) and assigned(ref.symbol)) or
  390. { absolute address is not a common thing in x64, but nevertheless a possible one }
  391. ((ref.base=NR_NO) and (ref.index=NR_NO) and (ref.symbol=nil)) then
  392. begin
  393. { Load constant value to register }
  394. hreg:=GetAddressRegister(list);
  395. if (cs_large in current_settings.globalswitches) and assigned(ref.symbol) then
  396. begin
  397. list.concat(taicpu.op_sym_ofs_reg(A_MOVABS,S_Q,ref.symbol,ref.offset+10,hreg));
  398. ref.symbol:=nil;
  399. end
  400. else
  401. list.concat(taicpu.op_const_reg(A_MOV,S_Q,ref.offset,hreg));
  402. ref.offset:=0;
  403. {if assigned(ref.symbol) then
  404. begin
  405. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_Q,ref.symbol,0,hreg));
  406. ref.symbol:=nil;
  407. end;}
  408. { Add register to reference }
  409. if ref.base=NR_NO then
  410. ref.base:=hreg
  411. else if ref.index=NR_NO then
  412. ref.index:=hreg
  413. else
  414. begin
  415. { don't use add, as the flags may contain a value }
  416. reference_reset_base(href,hreg,0,ref.temppos,ref.alignment,[]);
  417. href.index:=ref.index;
  418. href.scalefactor:=ref.scalefactor;
  419. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  420. ref.index:=hreg;
  421. ref.scalefactor:=1;
  422. end;
  423. end;
  424. if assigned(ref.symbol) then
  425. begin
  426. if cs_create_pic in current_settings.moduleswitches then
  427. begin
  428. { Local symbols must not be accessed via the GOT }
  429. if (ref.symbol.bind=AB_LOCAL) then
  430. begin
  431. { unfortunately, RIP-based addresses don't support an index }
  432. if (ref.base<>NR_NO) or
  433. (ref.index<>NR_NO) then
  434. begin
  435. reference_reset_symbol(href,ref.symbol,0,ref.alignment,[]);
  436. hreg:=getaddressregister(list);
  437. href.refaddr:=addr_pic_no_got;
  438. href.base:=NR_RIP;
  439. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  440. ref.symbol:=nil;
  441. end
  442. else
  443. begin
  444. ref.refaddr:=addr_pic_no_got;
  445. hreg:=NR_NO;
  446. ref.base:=NR_RIP;
  447. end;
  448. end
  449. else
  450. begin
  451. reference_reset_symbol(href,ref.symbol,0,ref.alignment,[]);
  452. hreg:=getaddressregister(list);
  453. href.refaddr:=addr_pic;
  454. href.base:=NR_RIP;
  455. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,href,hreg));
  456. ref.symbol:=nil;
  457. end;
  458. if ref.base=NR_NO then
  459. ref.base:=hreg
  460. else if ref.index=NR_NO then
  461. begin
  462. ref.index:=hreg;
  463. ref.scalefactor:=1;
  464. end
  465. else
  466. begin
  467. { don't use add, as the flags may contain a value }
  468. reference_reset_base(href,ref.base,0,ref.temppos,ref.alignment,[]);
  469. href.index:=hreg;
  470. ref.base:=getaddressregister(list);
  471. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,ref.base));
  472. end;
  473. end
  474. else
  475. { Always use RIP relative symbol addressing for Windows and Darwin targets. }
  476. if (target_info.system in (systems_all_windows+[system_x86_64_darwin,system_x86_64_iphonesim])) and (ref.base<>NR_RIP) then
  477. begin
  478. if (ref.refaddr=addr_no) and (ref.base=NR_NO) and (ref.index=NR_NO) then
  479. begin
  480. { Set RIP relative addressing for simple symbol references }
  481. ref.base:=NR_RIP;
  482. ref.refaddr:=addr_pic_no_got
  483. end
  484. else
  485. begin
  486. { Use temp register to load calculated 64-bit symbol address for complex references }
  487. reference_reset_symbol(href,ref.symbol,0,sizeof(pint),[]);
  488. href.base:=NR_RIP;
  489. href.refaddr:=addr_pic_no_got;
  490. hreg:=GetAddressRegister(list);
  491. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,hreg));
  492. ref.symbol:=nil;
  493. if ref.base=NR_NO then
  494. ref.base:=hreg
  495. else if ref.index=NR_NO then
  496. begin
  497. ref.index:=hreg;
  498. ref.scalefactor:=0;
  499. end
  500. else
  501. begin
  502. { don't use add, as the flags may contain a value }
  503. reference_reset_base(href,ref.base,0,ref.temppos,ref.alignment,[]);
  504. href.index:=hreg;
  505. ref.base:=getaddressregister(list);
  506. list.concat(taicpu.op_ref_reg(A_LEA,S_Q,href,ref.base));
  507. end;
  508. end;
  509. end;
  510. end;
  511. {$elseif defined(i386)}
  512. add_hreg:=false;
  513. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) then
  514. begin
  515. if assigned(ref.symbol) and
  516. not(assigned(ref.relsymbol)) and
  517. ((ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN]) or
  518. (cs_create_pic in current_settings.moduleswitches)) then
  519. begin
  520. if ref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL,AB_PRIVATE_EXTERN] then
  521. begin
  522. hreg:=g_indirect_sym_load(list,ref.symbol.name,asmsym2indsymflags(ref.symbol));
  523. ref.symbol:=nil;
  524. end
  525. else
  526. begin
  527. include(current_procinfo.flags,pi_needs_got);
  528. { make a copy of the got register, hreg can get modified }
  529. hreg:=getaddressregister(list);
  530. a_load_reg_reg(list,OS_ADDR,OS_ADDR,current_procinfo.got,hreg);
  531. ref.relsymbol:=current_procinfo.CurrGOTLabel;
  532. end;
  533. add_hreg:=true
  534. end
  535. end
  536. else if (cs_create_pic in current_settings.moduleswitches) and
  537. assigned(ref.symbol) then
  538. begin
  539. reference_reset_symbol(href,ref.symbol,0,sizeof(pint),[]);
  540. href.base:=current_procinfo.got;
  541. href.refaddr:=addr_pic;
  542. include(current_procinfo.flags,pi_needs_got);
  543. hreg:=getaddressregister(list);
  544. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,hreg));
  545. ref.symbol:=nil;
  546. add_hreg:=true;
  547. end;
  548. if add_hreg then
  549. begin
  550. if ref.base=NR_NO then
  551. ref.base:=hreg
  552. else if ref.index=NR_NO then
  553. begin
  554. ref.index:=hreg;
  555. ref.scalefactor:=1;
  556. end
  557. else
  558. begin
  559. { don't use add, as the flags may contain a value }
  560. reference_reset_base(href,ref.base,0,ref.temppos,ref.alignment,[]);
  561. href.index:=hreg;
  562. list.concat(taicpu.op_ref_reg(A_LEA,S_L,href,hreg));
  563. ref.base:=hreg;
  564. end;
  565. end;
  566. {$elseif defined(i8086)}
  567. { i8086 does not support stack relative addressing }
  568. if ref.base = NR_STACK_POINTER_REG then
  569. begin
  570. href:=ref;
  571. href.base:=getaddressregister(list);
  572. { let the register allocator find a suitable register for the reference }
  573. list.Concat(Taicpu.op_reg_reg(A_MOV, S_W, NR_SP, href.base));
  574. { if DS<>SS in the current memory model, we need to add an SS: segment override as well }
  575. if (ref.segment=NR_NO) and not segment_regs_equal(NR_DS,NR_SS) then
  576. href.segment:=NR_SS;
  577. ref:=href;
  578. end;
  579. { if there is a segment in an int register, move it to ES }
  580. if (ref.segment<>NR_NO) and (not is_segment_reg(ref.segment)) then
  581. begin
  582. list.concat(taicpu.op_reg_reg(A_MOV,S_W,ref.segment,NR_ES));
  583. ref.segment:=NR_ES;
  584. end;
  585. { can the segment override be dropped? }
  586. if ref.segment<>NR_NO then
  587. begin
  588. if (ref.base=NR_BP) and segment_regs_equal(ref.segment,NR_SS) then
  589. ref.segment:=NR_NO;
  590. if (ref.base<>NR_BP) and segment_regs_equal(ref.segment,NR_DS) then
  591. ref.segment:=NR_NO;
  592. end;
  593. {$endif}
  594. end;
  595. procedure tcgx86.make_direct_ref(list:tasmlist;var ref:treference);
  596. var
  597. href : treference;
  598. hreg : tregister;
  599. begin
  600. if assigned(ref.symbol) and (ref.symbol.bind in asmsymbindindirect) then
  601. begin
  602. { load the symbol into a register }
  603. hreg:=getaddressregister(list);
  604. reference_reset_symbol(href,ref.symbol,0,sizeof(pint),[]);
  605. { tell make_simple_ref that we are loading the symbol address via an indirect
  606. symbol and that hence it should not call make_direct_ref() again }
  607. a_load_ref_reg_internal(list,OS_ADDR,OS_ADDR,href,hreg,true);
  608. if ref.base<>NR_NO then
  609. begin
  610. { fold symbol register into base register }
  611. reference_reset_base(href,hreg,0,ctempposinvalid,ref.alignment,[]);
  612. href.index:=ref.base;
  613. hreg:=getaddressregister(list);
  614. a_loadaddr_ref_reg(list,href,hreg);
  615. end;
  616. { we're done }
  617. ref.symbol:=nil;
  618. ref.base:=hreg;
  619. end;
  620. end;
  621. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  622. begin
  623. case t of
  624. OS_F32 :
  625. begin
  626. op:=A_FLD;
  627. s:=S_FS;
  628. end;
  629. OS_F64 :
  630. begin
  631. op:=A_FLD;
  632. s:=S_FL;
  633. end;
  634. OS_F80 :
  635. begin
  636. op:=A_FLD;
  637. s:=S_FX;
  638. end;
  639. OS_C64 :
  640. begin
  641. op:=A_FILD;
  642. s:=S_IQ;
  643. end;
  644. else
  645. internalerror(200204043);
  646. end;
  647. end;
  648. procedure tcgx86.floatload(list: TAsmList; t : tcgsize;const ref : treference);
  649. var
  650. op : tasmop;
  651. s : topsize;
  652. tmpref : treference;
  653. begin
  654. tmpref:=ref;
  655. make_simple_ref(list,tmpref);
  656. floatloadops(t,op,s);
  657. list.concat(Taicpu.Op_ref(op,s,tmpref));
  658. inc_fpu_stack;
  659. end;
  660. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  661. begin
  662. case t of
  663. OS_F32 :
  664. begin
  665. op:=A_FSTP;
  666. s:=S_FS;
  667. end;
  668. OS_F64 :
  669. begin
  670. op:=A_FSTP;
  671. s:=S_FL;
  672. end;
  673. OS_F80 :
  674. begin
  675. op:=A_FSTP;
  676. s:=S_FX;
  677. end;
  678. OS_C64 :
  679. begin
  680. op:=A_FISTP;
  681. s:=S_IQ;
  682. end;
  683. else
  684. internalerror(200204042);
  685. end;
  686. end;
  687. procedure tcgx86.floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  688. var
  689. op : tasmop;
  690. s : topsize;
  691. tmpref : treference;
  692. begin
  693. tmpref:=ref;
  694. make_simple_ref(list,tmpref);
  695. floatstoreops(t,op,s);
  696. list.concat(Taicpu.Op_ref(op,s,tmpref));
  697. { storing non extended floats can cause a floating point overflow }
  698. if ((t<>OS_F80) and (cs_fpu_fwait in current_settings.localswitches))
  699. {$ifdef i8086}
  700. { 8087 and 80287 need a FWAIT after a memory store, before it can be
  701. read with the integer unit }
  702. or (current_settings.cputype<=cpu_286)
  703. {$endif i8086}
  704. then
  705. list.concat(Taicpu.Op_none(A_FWAIT,S_NO));
  706. dec_fpu_stack;
  707. end;
  708. procedure tcgx86.check_register_size(size:tcgsize;reg:tregister);
  709. begin
  710. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  711. internalerror(200306031);
  712. end;
  713. {****************************************************************************
  714. Assembler code
  715. ****************************************************************************}
  716. procedure tcgx86.a_jmp_name(list : TAsmList;const s : string);
  717. var
  718. r: treference;
  719. begin
  720. if (target_info.system <> system_i386_darwin) then
  721. list.concat(taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s,AT_FUNCTION)))
  722. else
  723. begin
  724. reference_reset_symbol(r,get_darwin_call_stub(s,false),0,sizeof(pint),[]);
  725. r.refaddr:=addr_full;
  726. list.concat(taicpu.op_ref(A_JMP,S_NO,r));
  727. end;
  728. end;
  729. procedure tcgx86.a_jmp_always(list : TAsmList;l: tasmlabel);
  730. begin
  731. a_jmp_cond(list, OC_NONE, l);
  732. end;
  733. function tcgx86.get_darwin_call_stub(const s: string; weak: boolean): tasmsymbol;
  734. var
  735. stubname: string;
  736. begin
  737. stubname := 'L'+s+'$stub';
  738. result := current_asmdata.getasmsymbol(stubname);
  739. if assigned(result) then
  740. exit;
  741. if current_asmdata.asmlists[al_imports]=nil then
  742. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  743. new_section(current_asmdata.asmlists[al_imports],sec_stub,'',0);
  744. result := current_asmdata.DefineAsmSymbol(stubname,AB_LOCAL,AT_FUNCTION,voidcodepointertype);
  745. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  746. { register as a weak symbol if necessary }
  747. if weak then
  748. current_asmdata.weakrefasmsymbol(s,AT_FUNCTION);
  749. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  750. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  751. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  752. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  753. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  754. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  755. end;
  756. procedure tcgx86.a_call_name(list : TAsmList;const s : string; weak: boolean);
  757. begin
  758. a_call_name_near(list,s,weak);
  759. end;
  760. procedure tcgx86.a_call_name_near(list : TAsmList;const s : string; weak: boolean);
  761. var
  762. sym : tasmsymbol;
  763. r : treference;
  764. begin
  765. if (target_info.system <> system_i386_darwin) then
  766. begin
  767. if not(weak) then
  768. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION)
  769. else
  770. sym:=current_asmdata.WeakRefAsmSymbol(s,AT_FUNCTION);
  771. reference_reset_symbol(r,sym,0,sizeof(pint),[]);
  772. if (cs_create_pic in current_settings.moduleswitches) and
  773. { darwin's assembler doesn't want @PLT after call symbols }
  774. not(target_info.system in [system_x86_64_darwin,system_i386_iphonesim,system_x86_64_iphonesim]) then
  775. begin
  776. r.refaddr:=addr_pic;
  777. end
  778. else
  779. r.refaddr:=addr_full;
  780. end
  781. else
  782. begin
  783. reference_reset_symbol(r,get_darwin_call_stub(s,weak),0,sizeof(pint),[]);
  784. r.refaddr:=addr_full;
  785. end;
  786. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  787. end;
  788. procedure tcgx86.a_call_name_static(list : TAsmList;const s : string);
  789. begin
  790. a_call_name_static_near(list,s);
  791. end;
  792. procedure tcgx86.a_call_name_static_near(list : TAsmList;const s : string);
  793. var
  794. sym : tasmsymbol;
  795. r : treference;
  796. begin
  797. sym:=current_asmdata.RefAsmSymbol(s,AT_FUNCTION);
  798. reference_reset_symbol(r,sym,0,sizeof(pint),[]);
  799. r.refaddr:=addr_full;
  800. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  801. end;
  802. procedure tcgx86.a_call_reg(list : TAsmList;reg : tregister);
  803. begin
  804. a_call_reg_near(list,reg);
  805. end;
  806. procedure tcgx86.a_call_reg_near(list: TAsmList; reg: tregister);
  807. begin
  808. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  809. end;
  810. {********************** load instructions ********************}
  811. procedure tcgx86.a_load_const_reg(list : TAsmList; tosize: TCGSize; a : tcgint; reg : TRegister);
  812. begin
  813. check_register_size(tosize,reg);
  814. { the optimizer will change it to "xor reg,reg" when loading zero, }
  815. { no need to do it here too (JM) }
  816. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg))
  817. end;
  818. procedure tcgx86.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : tcgint;const ref : treference);
  819. var
  820. tmpref : treference;
  821. begin
  822. tmpref:=ref;
  823. make_simple_ref(list,tmpref);
  824. {$ifdef x86_64}
  825. { x86_64 only supports signed 32 bits constants directly }
  826. if (tosize in [OS_S64,OS_64]) and
  827. ((a<low(longint)) or (a>high(longint))) then
  828. begin
  829. a_load_const_ref(list,OS_32,longint(a and $ffffffff),tmpref);
  830. inc(tmpref.offset,4);
  831. a_load_const_ref(list,OS_32,longint(a shr 32),tmpref);
  832. end
  833. else
  834. {$endif x86_64}
  835. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  836. end;
  837. procedure tcgx86.a_load_reg_ref(list : TAsmList; fromsize,tosize: TCGSize; reg : tregister;const ref : treference);
  838. var
  839. op: tasmop;
  840. s: topsize;
  841. tmpsize : tcgsize;
  842. tmpreg : tregister;
  843. tmpref : treference;
  844. begin
  845. tmpref:=ref;
  846. make_simple_ref(list,tmpref);
  847. if TCGSize2Size[fromsize]>TCGSize2Size[tosize] then
  848. begin
  849. fromsize:=tosize;
  850. reg:=makeregsize(list,reg,fromsize);
  851. end;
  852. check_register_size(fromsize,reg);
  853. sizes2load(fromsize,tosize,op,s);
  854. case s of
  855. {$ifdef x86_64}
  856. S_BQ,S_WQ,S_LQ,
  857. {$endif x86_64}
  858. S_BW,S_BL,S_WL :
  859. begin
  860. tmpreg:=getintregister(list,tosize);
  861. {$ifdef x86_64}
  862. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  863. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  864. 64 bit (FK) }
  865. if s in [S_BL,S_WL,S_L] then
  866. begin
  867. tmpreg:=makeregsize(list,tmpreg,OS_32);
  868. tmpsize:=OS_32;
  869. end
  870. else
  871. {$endif x86_64}
  872. tmpsize:=tosize;
  873. list.concat(taicpu.op_reg_reg(op,s,reg,tmpreg));
  874. a_load_reg_ref(list,tmpsize,tosize,tmpreg,tmpref);
  875. end;
  876. else
  877. list.concat(taicpu.op_reg_ref(op,s,reg,tmpref));
  878. end;
  879. end;
  880. procedure tcgx86.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  881. begin
  882. a_load_ref_reg_internal(list,fromsize,tosize,ref,reg,false);
  883. end;
  884. procedure tcgx86.a_load_ref_reg_internal(list : TAsmList;fromsize,tosize : tcgsize;const ref: treference;reg : tregister;isdirect:boolean);
  885. var
  886. op: tasmop;
  887. s: topsize;
  888. tmpref : treference;
  889. begin
  890. tmpref:=ref;
  891. make_simple_ref(list,tmpref,isdirect);
  892. check_register_size(tosize,reg);
  893. sizes2load(fromsize,tosize,op,s);
  894. {$ifdef x86_64}
  895. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  896. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  897. 64 bit (FK) }
  898. if s in [S_BL,S_WL,S_L] then
  899. reg:=makeregsize(list,reg,OS_32);
  900. {$endif x86_64}
  901. list.concat(taicpu.op_ref_reg(op,s,tmpref,reg));
  902. end;
  903. procedure tcgx86.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  904. var
  905. op: tasmop;
  906. s: topsize;
  907. instr:Taicpu;
  908. begin
  909. check_register_size(fromsize,reg1);
  910. check_register_size(tosize,reg2);
  911. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  912. begin
  913. reg1:=makeregsize(list,reg1,tosize);
  914. s:=tcgsize2opsize[tosize];
  915. op:=A_MOV;
  916. end
  917. else
  918. sizes2load(fromsize,tosize,op,s);
  919. {$ifdef x86_64}
  920. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  921. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  922. 64 bit (FK)
  923. }
  924. if s in [S_BL,S_WL,S_L] then
  925. reg2:=makeregsize(list,reg2,OS_32);
  926. {$endif x86_64}
  927. if (reg1<>reg2) then
  928. begin
  929. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  930. { Notify the register allocator that we have written a move instruction so
  931. it can try to eliminate it. }
  932. if (reg1<>current_procinfo.framepointer) and (reg1<>NR_STACK_POINTER_REG) then
  933. add_move_instruction(instr);
  934. list.concat(instr);
  935. end;
  936. {$ifdef x86_64}
  937. { avoid merging of registers and killing the zero extensions (FK) }
  938. if (tosize in [OS_64,OS_S64]) and (s=S_L) then
  939. list.concat(taicpu.op_const_reg(A_AND,S_L,$ffffffff,reg2));
  940. {$endif x86_64}
  941. end;
  942. procedure tcgx86.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  943. var
  944. dirref,tmpref : treference;
  945. tmpreg : TRegister;
  946. begin
  947. dirref:=ref;
  948. { this could probably done in a more optimized way, but for now this
  949. is sufficent }
  950. make_direct_ref(list,dirref);
  951. with dirref do
  952. begin
  953. {$ifdef i386}
  954. if refaddr=addr_ntpoff then
  955. begin
  956. { Convert thread local address to a process global addres
  957. as we cannot handle far pointers.}
  958. case target_info.system of
  959. system_i386_linux,system_i386_android:
  960. if segment=NR_GS then
  961. begin
  962. reference_reset(tmpref,1,[]);
  963. tmpref.segment:=NR_GS;
  964. tmpreg:=getaddressregister(list);
  965. a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,tmpreg);
  966. reference_reset(tmpref,1,[]);
  967. tmpref.symbol:=symbol;
  968. tmpref.refaddr:=refaddr;
  969. tmpref.base:=tmpreg;
  970. if base<>NR_NO then
  971. tmpref.index:=base;
  972. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,tmpreg));
  973. segment:=NR_NO;
  974. base:=tmpreg;
  975. symbol:=nil;
  976. refaddr:=addr_no;
  977. end
  978. else
  979. Internalerror(2018110402);
  980. else
  981. Internalerror(2018110403);
  982. end;
  983. end;
  984. {$endif i386}
  985. {$ifdef x86_64}
  986. if refaddr=addr_tpoff then
  987. begin
  988. { Convert thread local address to a process global addres
  989. as we cannot handle far pointers.}
  990. case target_info.system of
  991. system_x86_64_linux:
  992. if segment=NR_FS then
  993. begin
  994. reference_reset(tmpref,1,[]);
  995. tmpref.segment:=NR_FS;
  996. tmpreg:=getaddressregister(list);
  997. a_load_ref_reg(list,OS_ADDR,OS_ADDR,tmpref,tmpreg);
  998. reference_reset(tmpref,1,[]);
  999. tmpref.symbol:=symbol;
  1000. tmpref.refaddr:=refaddr;
  1001. tmpref.base:=tmpreg;
  1002. if base<>NR_NO then
  1003. tmpref.index:=base;
  1004. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,tmpreg));
  1005. segment:=NR_NO;
  1006. base:=tmpreg;
  1007. symbol:=nil;
  1008. refaddr:=addr_no;
  1009. end
  1010. else
  1011. Internalerror(2019012003);
  1012. else
  1013. Internalerror(2019012004);
  1014. end;
  1015. end;
  1016. {$endif x86_64}
  1017. if (base=NR_NO) and (index=NR_NO) then
  1018. begin
  1019. if assigned(dirref.symbol) then
  1020. begin
  1021. if (target_info.system in [system_i386_darwin,system_i386_iphonesim]) and
  1022. ((dirref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  1023. (cs_create_pic in current_settings.moduleswitches)) then
  1024. begin
  1025. if (dirref.symbol.bind in [AB_EXTERNAL,AB_WEAK_EXTERNAL]) or
  1026. ((cs_create_pic in current_settings.moduleswitches) and
  1027. (dirref.symbol.bind in [AB_COMMON,AB_GLOBAL,AB_PRIVATE_EXTERN])) then
  1028. begin
  1029. reference_reset_base(tmpref,
  1030. g_indirect_sym_load(list,dirref.symbol.name,asmsym2indsymflags(dirref.symbol)),
  1031. offset,ctempposinvalid,sizeof(pint),[]);
  1032. a_loadaddr_ref_reg(list,tmpref,r);
  1033. end
  1034. else
  1035. begin
  1036. include(current_procinfo.flags,pi_needs_got);
  1037. reference_reset_base(tmpref,current_procinfo.got,offset,dirref.temppos,dirref.alignment,[]);
  1038. tmpref.symbol:=symbol;
  1039. tmpref.relsymbol:=current_procinfo.CurrGOTLabel;
  1040. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  1041. end;
  1042. end
  1043. else if (cs_create_pic in current_settings.moduleswitches)
  1044. {$ifdef x86_64}
  1045. and not(dirref.symbol.bind=AB_LOCAL)
  1046. {$endif x86_64}
  1047. then
  1048. begin
  1049. {$ifdef x86_64}
  1050. reference_reset_symbol(tmpref,dirref.symbol,0,sizeof(pint),[]);
  1051. tmpref.refaddr:=addr_pic;
  1052. tmpref.base:=NR_RIP;
  1053. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,tmpref,r));
  1054. {$else x86_64}
  1055. reference_reset_symbol(tmpref,dirref.symbol,0,sizeof(pint),[]);
  1056. tmpref.refaddr:=addr_pic;
  1057. tmpref.base:=current_procinfo.got;
  1058. include(current_procinfo.flags,pi_needs_got);
  1059. list.concat(taicpu.op_ref_reg(A_MOV,S_L,tmpref,r));
  1060. {$endif x86_64}
  1061. if offset<>0 then
  1062. a_op_const_reg(list,OP_ADD,OS_ADDR,offset,r);
  1063. end
  1064. {$ifdef x86_64}
  1065. else if (target_info.system in (systems_all_windows+[system_x86_64_darwin,system_x86_64_iphonesim]))
  1066. or (cs_create_pic in current_settings.moduleswitches)
  1067. then
  1068. begin
  1069. { Win64 and Darwin/x86_64 always require RIP-relative addressing }
  1070. tmpref:=dirref;
  1071. tmpref.base:=NR_RIP;
  1072. tmpref.refaddr:=addr_pic_no_got;
  1073. list.concat(Taicpu.op_ref_reg(A_LEA,S_Q,tmpref,r));
  1074. end
  1075. {$endif x86_64}
  1076. else
  1077. begin
  1078. tmpref:=dirref;
  1079. tmpref.refaddr:=ADDR_FULL;
  1080. list.concat(Taicpu.op_ref_reg(A_MOV,tcgsize2opsize[OS_ADDR],tmpref,r));
  1081. end
  1082. end
  1083. else
  1084. a_load_const_reg(list,OS_ADDR,offset,r)
  1085. end
  1086. else if (base=NR_NO) and (index<>NR_NO) and
  1087. (offset=0) and (scalefactor=0) and (symbol=nil) then
  1088. a_load_reg_reg(list,OS_ADDR,OS_ADDR,index,r)
  1089. else if (base<>NR_NO) and (index=NR_NO) and
  1090. (offset=0) and (symbol=nil) then
  1091. a_load_reg_reg(list,OS_ADDR,OS_ADDR,base,r)
  1092. else
  1093. begin
  1094. tmpref:=dirref;
  1095. make_simple_ref(list,tmpref);
  1096. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  1097. end;
  1098. if segment<>NR_NO then
  1099. begin
  1100. {$ifdef i8086}
  1101. if is_segment_reg(segment) then
  1102. list.concat(Taicpu.op_reg_reg(A_MOV,S_W,segment,GetNextReg(r)))
  1103. else
  1104. a_load_reg_reg(list,OS_16,OS_16,segment,GetNextReg(r));
  1105. {$else i8086}
  1106. cgmessage(cg_e_cant_use_far_pointer_there);
  1107. {$endif i8086}
  1108. end;
  1109. end;
  1110. end;
  1111. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  1112. { R_ST means "the current value at the top of the fpu stack" (JM) }
  1113. procedure tcgx86.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  1114. var
  1115. href: treference;
  1116. op: tasmop;
  1117. s: topsize;
  1118. begin
  1119. if (reg1<>NR_ST) then
  1120. begin
  1121. floatloadops(tosize,op,s);
  1122. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  1123. inc_fpu_stack;
  1124. end;
  1125. if (reg2<>NR_ST) then
  1126. begin
  1127. floatstoreops(tosize,op,s);
  1128. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  1129. dec_fpu_stack;
  1130. end;
  1131. { OS_F80 < OS_C64, but OS_C64 fits perfectly in OS_F80 }
  1132. if (reg1=NR_ST) and
  1133. (reg2=NR_ST) and
  1134. (tosize<>OS_F80) and
  1135. (tosize<fromsize) then
  1136. begin
  1137. { can't round down to lower precision in x87 :/ }
  1138. tg.gettemp(list,tcgsize2size[tosize],tcgsize2size[tosize],tt_normal,href);
  1139. a_loadfpu_reg_ref(list,fromsize,tosize,NR_ST,href);
  1140. a_loadfpu_ref_reg(list,tosize,tosize,href,NR_ST);
  1141. tg.ungettemp(list,href);
  1142. end;
  1143. end;
  1144. procedure tcgx86.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  1145. var
  1146. tmpref : treference;
  1147. begin
  1148. tmpref:=ref;
  1149. make_simple_ref(list,tmpref);
  1150. floatload(list,fromsize,tmpref);
  1151. a_loadfpu_reg_reg(list,fromsize,tosize,NR_ST,reg);
  1152. end;
  1153. procedure tcgx86.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  1154. var
  1155. tmpref : treference;
  1156. begin
  1157. tmpref:=ref;
  1158. make_simple_ref(list,tmpref);
  1159. { in case a record returned in a floating point register
  1160. (LOC_FPUREGISTER with OS_F32/OS_F64) is stored in memory
  1161. (LOC_REFERENCE with OS_32/OS_64), we have to adjust the
  1162. tosize }
  1163. if (fromsize in [OS_F32,OS_F64]) and
  1164. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1165. case tosize of
  1166. OS_32:
  1167. tosize:=OS_F32;
  1168. OS_64:
  1169. tosize:=OS_F64;
  1170. else
  1171. ;
  1172. end;
  1173. if reg<>NR_ST then
  1174. a_loadfpu_reg_reg(list,fromsize,tosize,reg,NR_ST);
  1175. floatstore(list,tosize,tmpref);
  1176. end;
  1177. procedure tcgx86.a_loadfpu_ref_cgpara(list: TAsmList; size: tcgsize; const ref: treference; const cgpara: TCGPara);
  1178. var
  1179. href: treference;
  1180. begin
  1181. if cgpara.location^.loc in [LOC_REFERENCE,LOC_CREFERENCE] then
  1182. begin
  1183. cgpara.check_simple_location;
  1184. reference_reset_base(href,cgpara.location^.reference.index,cgpara.location^.reference.offset,ctempposinvalid,cgpara.alignment,[]);
  1185. floatload(list,size,ref);
  1186. floatstore(list,size,href);
  1187. end
  1188. else
  1189. inherited a_loadfpu_ref_cgpara(list, size, ref, cgpara);
  1190. end;
  1191. function get_scalar_mm_op(fromsize,tosize : tcgsize;aligned : boolean) : tasmop;
  1192. const
  1193. convertopsse : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1194. (A_MOVSS,A_CVTSS2SD,A_NONE,A_NONE,A_NONE),
  1195. (A_CVTSD2SS,A_MOVSD,A_NONE,A_NONE,A_NONE),
  1196. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1197. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1198. (A_NONE,A_NONE,A_NONE,A_NONE,A_MOVAPS));
  1199. convertopavx : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  1200. (A_VMOVSS,A_VCVTSS2SD,A_NONE,A_NONE,A_NONE),
  1201. (A_VCVTSD2SS,A_VMOVSD,A_NONE,A_NONE,A_NONE),
  1202. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  1203. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  1204. (A_NONE,A_NONE,A_NONE,A_NONE,A_VMOVAPS));
  1205. begin
  1206. { we can have OS_F32/OS_F64 (record in function result/LOC_MMREGISTER) to
  1207. OS_32/OS_64 (record in memory/LOC_REFERENCE) }
  1208. if (fromsize in [OS_F32,OS_F64]) and
  1209. (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1210. case tosize of
  1211. OS_32:
  1212. tosize:=OS_F32;
  1213. OS_64:
  1214. tosize:=OS_F64;
  1215. else
  1216. ;
  1217. end;
  1218. if (fromsize in [low(convertopsse)..high(convertopsse)]) and
  1219. (tosize in [low(convertopsse)..high(convertopsse)]) then
  1220. begin
  1221. if UseAVX then
  1222. result:=convertopavx[fromsize,tosize]
  1223. else
  1224. result:=convertopsse[fromsize,tosize];
  1225. end
  1226. { we can have OS_M64 (record in function result/LOC_MMREGISTER) to
  1227. OS_64 (record in memory/LOC_REFERENCE) }
  1228. else if (tcgsize2size[fromsize]=tcgsize2size[tosize]) then
  1229. begin
  1230. case fromsize of
  1231. OS_M64:
  1232. { we can have OS_M64 (record in function result/LOC_MMREGISTER) to
  1233. OS_64 (record in memory/LOC_REFERENCE) }
  1234. if UseAVX then
  1235. result:=A_VMOVQ
  1236. else
  1237. result:=A_MOVQ;
  1238. OS_M128:
  1239. { 128-bit aligned vector }
  1240. if UseAVX then
  1241. begin
  1242. if aligned then
  1243. result:=A_VMOVAPS
  1244. else
  1245. result:=A_VMOVUPS;
  1246. end
  1247. else if aligned then
  1248. result:=A_MOVAPS
  1249. else
  1250. result:=A_MOVUPS;
  1251. OS_M256,
  1252. OS_M512:
  1253. { 256-bit aligned vector }
  1254. if UseAVX then
  1255. begin
  1256. if aligned then
  1257. result:=A_VMOVAPS
  1258. else
  1259. result:=A_VMOVUPS;
  1260. end
  1261. else
  1262. { SSE does not support 256-bit or 512-bit vectors }
  1263. InternalError(2018012930);
  1264. else
  1265. InternalError(2018012920);
  1266. end;
  1267. end
  1268. else if (tcgsize2size[fromsize]=tcgsize2size[tosize]) and
  1269. (fromsize=OS_M128) then
  1270. begin
  1271. if UseAVX then
  1272. result:=A_VMOVDQU
  1273. else
  1274. result:=A_MOVDQU;
  1275. end
  1276. else
  1277. internalerror(2010060104);
  1278. if result=A_NONE then
  1279. internalerror(200312205);
  1280. end;
  1281. procedure tcgx86.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  1282. var
  1283. instr : taicpu;
  1284. op : TAsmOp;
  1285. begin
  1286. if shuffle=nil then
  1287. begin
  1288. if fromsize=tosize then
  1289. { needs correct size in case of spilling }
  1290. case fromsize of
  1291. OS_F32:
  1292. if UseAVX then
  1293. instr:=taicpu.op_reg_reg(A_VMOVAPS,S_NO,reg1,reg2)
  1294. else
  1295. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2);
  1296. OS_F64:
  1297. if UseAVX then
  1298. instr:=taicpu.op_reg_reg(A_VMOVAPD,S_NO,reg1,reg2)
  1299. else
  1300. instr:=taicpu.op_reg_reg(A_MOVAPD,S_NO,reg1,reg2);
  1301. OS_M64:
  1302. if UseAVX then
  1303. instr:=taicpu.op_reg_reg(A_VMOVQ,S_NO,reg1,reg2)
  1304. else
  1305. instr:=taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2);
  1306. OS_M128:
  1307. if UseAVX then
  1308. instr:=taicpu.op_reg_reg(A_VMOVDQA,S_NO,reg1,reg2)
  1309. else
  1310. instr:=taicpu.op_reg_reg(A_MOVDQA,S_NO,reg1,reg2);
  1311. OS_M256,
  1312. OS_M512:
  1313. if UseAVX then
  1314. instr:=taicpu.op_reg_reg(A_VMOVDQA,S_NO,reg1,reg2)
  1315. else
  1316. { SSE doesn't support 512-bit vectors }
  1317. InternalError(2018012933);
  1318. else
  1319. internalerror(2006091201);
  1320. end
  1321. else
  1322. internalerror(200312202);
  1323. add_move_instruction(instr);
  1324. end
  1325. else if shufflescalar(shuffle) then
  1326. begin
  1327. op:=get_scalar_mm_op(fromsize,tosize,true);
  1328. { MOVAPD/MOVAPS are normally faster }
  1329. if op=A_MOVSD then
  1330. op:=A_MOVAPD
  1331. else if op=A_MOVSS then
  1332. op:=A_MOVAPS
  1333. { VMOVSD/SS is not available with two register operands }
  1334. else if op=A_VMOVSD then
  1335. op:=A_VMOVAPD
  1336. else if op=A_VMOVSS then
  1337. op:=A_VMOVAPS;
  1338. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1339. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1340. instr:=taicpu.op_reg_reg_reg(op,S_NO,reg1,reg2,reg2)
  1341. else
  1342. instr:=taicpu.op_reg_reg(op,S_NO,reg1,reg2);
  1343. case op of
  1344. A_VMOVAPD,
  1345. A_VMOVAPS,
  1346. A_VMOVSS,
  1347. A_VMOVSD,
  1348. A_VMOVQ,
  1349. A_MOVAPD,
  1350. A_MOVAPS,
  1351. A_MOVSS,
  1352. A_MOVSD,
  1353. A_MOVQ:
  1354. add_move_instruction(instr);
  1355. else
  1356. ;
  1357. end;
  1358. end
  1359. else
  1360. internalerror(200312201);
  1361. list.concat(instr);
  1362. end;
  1363. procedure tcgx86.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1364. var
  1365. tmpref : treference;
  1366. op : tasmop;
  1367. begin
  1368. tmpref:=ref;
  1369. make_simple_ref(list,tmpref);
  1370. if shuffle=nil then
  1371. begin
  1372. case fromsize of
  1373. OS_F32:
  1374. if UseAVX then
  1375. op := A_VMOVSS
  1376. else
  1377. op := A_MOVSS;
  1378. OS_F64:
  1379. if UseAVX then
  1380. op := A_VMOVSD
  1381. else
  1382. op := A_MOVSD;
  1383. OS_M32, OS_32, OS_S32:
  1384. if UseAVX then
  1385. op := A_VMOVD
  1386. else
  1387. op := A_MOVD;
  1388. OS_M64, OS_64, OS_S64:
  1389. { there is no VMOVQ for MMX registers }
  1390. if UseAVX and (getregtype(reg)<>R_MMXREGISTER) then
  1391. op := A_VMOVQ
  1392. else
  1393. op := A_MOVQ;
  1394. OS_128,
  1395. OS_M128:
  1396. { Use XMM integer transfer }
  1397. if UseAVX then
  1398. begin
  1399. if GetRefAlignment(tmpref) = 16 then
  1400. op := A_VMOVDQA
  1401. else
  1402. op := A_VMOVDQU;
  1403. end
  1404. else
  1405. begin
  1406. if GetRefAlignment(tmpref) = 16 then
  1407. op := A_MOVDQA
  1408. else
  1409. op := A_MOVDQU;
  1410. end;
  1411. OS_M256:
  1412. { Use YMM integer transfer }
  1413. if UseAVX then
  1414. begin
  1415. if GetRefAlignment(tmpref) = 32 then
  1416. op := A_VMOVDQA
  1417. else
  1418. op := A_VMOVDQU;
  1419. end
  1420. else
  1421. { SSE doesn't support 256-bit vectors }
  1422. Internalerror(2020010401);
  1423. OS_M512:
  1424. { Use ZMM integer transfer }
  1425. if UseAVX then
  1426. begin
  1427. if GetRefAlignment(tmpref) = 64 then
  1428. op := A_VMOVDQA64
  1429. else
  1430. op := A_VMOVDQU64;
  1431. end
  1432. else
  1433. { SSE doesn't support 512-bit vectors }
  1434. InternalError(2018012939);
  1435. else
  1436. { No valid transfer command available }
  1437. internalerror(2017121410);
  1438. end;
  1439. list.concat(taicpu.op_ref_reg(op,S_NO,tmpref,reg));
  1440. end
  1441. else if shufflescalar(shuffle) then
  1442. begin
  1443. op:=get_scalar_mm_op(fromsize,tosize,tcgsize2size[fromsize]=ref.alignment);
  1444. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1445. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1446. list.concat(taicpu.op_ref_reg_reg(op,S_NO,tmpref,reg,reg))
  1447. else
  1448. list.concat(taicpu.op_ref_reg(op,S_NO,tmpref,reg))
  1449. end
  1450. else
  1451. internalerror(200312252);
  1452. end;
  1453. procedure tcgx86.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  1454. var
  1455. hreg : tregister;
  1456. tmpref : treference;
  1457. op : tasmop;
  1458. begin
  1459. tmpref:=ref;
  1460. make_simple_ref(list,tmpref);
  1461. if shuffle=nil then
  1462. begin
  1463. case fromsize of
  1464. OS_F32:
  1465. if UseAVX then
  1466. op := A_VMOVSS
  1467. else
  1468. op := A_MOVSS;
  1469. OS_F64:
  1470. if UseAVX then
  1471. op := A_VMOVSD
  1472. else
  1473. op := A_MOVSD;
  1474. OS_M32, OS_32, OS_S32:
  1475. if UseAVX then
  1476. op := A_VMOVD
  1477. else
  1478. op := A_MOVD;
  1479. OS_M64, OS_64, OS_S64:
  1480. { there is no VMOVQ for MMX registers }
  1481. if UseAVX and (getregtype(reg)<>R_MMXREGISTER) then
  1482. op := A_VMOVQ
  1483. else
  1484. op := A_MOVQ;
  1485. OS_M128:
  1486. { Use XMM integer transfer }
  1487. if UseAVX then
  1488. begin
  1489. if GetRefAlignment(tmpref) = 16 then
  1490. op := A_VMOVDQA
  1491. else
  1492. op := A_VMOVDQU;
  1493. end else
  1494. begin
  1495. if GetRefAlignment(tmpref) = 16 then
  1496. op := A_MOVDQA
  1497. else
  1498. op := A_MOVDQU;
  1499. end;
  1500. OS_M256:
  1501. { Use XMM integer transfer }
  1502. if UseAVX then
  1503. begin
  1504. if GetRefAlignment(tmpref) = 32 then
  1505. op := A_VMOVDQA
  1506. else
  1507. op := A_VMOVDQU;
  1508. end else
  1509. { SSE doesn't support 256-bit vectors }
  1510. InternalError(2018012942);
  1511. OS_M512:
  1512. { Use XMM integer transfer }
  1513. if UseAVX then
  1514. begin
  1515. if GetRefAlignment(tmpref) = 64 then
  1516. op := A_VMOVDQA64
  1517. else
  1518. op := A_VMOVDQU64;
  1519. end else
  1520. { SSE doesn't support 512-bit vectors }
  1521. InternalError(2018012945);
  1522. else
  1523. { No valid transfer command available }
  1524. internalerror(2017121411);
  1525. end;
  1526. list.concat(taicpu.op_reg_ref(op,S_NO,reg,tmpref));
  1527. end
  1528. else if shufflescalar(shuffle) then
  1529. begin
  1530. if tcgsize2size[tosize]<>tcgsize2size[fromsize] then
  1531. begin
  1532. hreg:=getmmregister(list,tosize);
  1533. op:=get_scalar_mm_op(fromsize,tosize,tcgsize2size[tosize]=ref.alignment);
  1534. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1535. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1536. list.concat(taicpu.op_reg_reg_reg(op,S_NO,reg,hreg,hreg))
  1537. else
  1538. list.concat(taicpu.op_reg_reg(op,S_NO,reg,hreg));
  1539. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize,tcgsize2size[tosize]=tmpref.alignment),S_NO,hreg,tmpref))
  1540. end
  1541. else
  1542. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize,tcgsize2size[tosize]=tmpref.alignment),S_NO,reg,tmpref));
  1543. end
  1544. else
  1545. internalerror(2003122501);
  1546. end;
  1547. procedure tcgx86.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1548. var
  1549. l : tlocation;
  1550. begin
  1551. l.loc:=LOC_REFERENCE;
  1552. l.reference:=ref;
  1553. l.size:=size;
  1554. opmm_loc_reg(list,op,size,l,reg,shuffle);
  1555. end;
  1556. procedure tcgx86.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  1557. var
  1558. l : tlocation;
  1559. begin
  1560. l.loc:=LOC_MMREGISTER;
  1561. l.register:=src;
  1562. l.size:=size;
  1563. opmm_loc_reg(list,op,size,l,dst,shuffle);
  1564. end;
  1565. procedure tcgx86.opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;src,dst: tregister; shuffle : pmmshuffle);
  1566. const
  1567. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1568. ( { scalar }
  1569. ( { OS_F32 }
  1570. A_NOP,A_NOP,A_VADDSS,A_NOP,A_VDIVSS,A_NOP,A_NOP,A_VMULSS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSS,A_NOP,A_NOP,A_NOP
  1571. ),
  1572. ( { OS_F64 }
  1573. A_NOP,A_NOP,A_VADDSD,A_NOP,A_VDIVSD,A_NOP,A_NOP,A_VMULSD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSD,A_NOP,A_NOP,A_NOP
  1574. )
  1575. ),
  1576. ( { vectorized/packed }
  1577. { because the logical packed single instructions have shorter op codes, we use always
  1578. these
  1579. }
  1580. ( { OS_F32 }
  1581. A_NOP,A_NOP,A_VADDPS,A_NOP,A_VDIVPS,A_NOP,A_NOP,A_VMULPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPS,A_VXORPS,A_NOP,A_NOP
  1582. ),
  1583. ( { OS_F64 }
  1584. A_NOP,A_NOP,A_VADDPD,A_NOP,A_VDIVPD,A_NOP,A_NOP,A_VMULPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPD,A_VXORPD,A_NOP,A_NOP
  1585. )
  1586. )
  1587. );
  1588. var
  1589. resultreg : tregister;
  1590. asmop : tasmop;
  1591. begin
  1592. { this is an internally used procedure so the parameters have
  1593. some constrains
  1594. }
  1595. if loc.size<>size then
  1596. internalerror(2013061108);
  1597. resultreg:=dst;
  1598. { deshuffle }
  1599. //!!!
  1600. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1601. begin
  1602. internalerror(2013061107);
  1603. end
  1604. else if (shuffle=nil) then
  1605. asmop:=opmm2asmop[1,size,op]
  1606. else if shufflescalar(shuffle) then
  1607. begin
  1608. asmop:=opmm2asmop[0,size,op];
  1609. { no scalar operation available? }
  1610. if asmop=A_NOP then
  1611. begin
  1612. { do vectorized and shuffle finally }
  1613. internalerror(2010060103);
  1614. end;
  1615. end
  1616. else
  1617. internalerror(2013061106);
  1618. if asmop=A_NOP then
  1619. internalerror(2013061105);
  1620. case loc.loc of
  1621. LOC_CREFERENCE,LOC_REFERENCE:
  1622. begin
  1623. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1624. list.concat(taicpu.op_ref_reg_reg(asmop,S_NO,loc.reference,src,resultreg));
  1625. end;
  1626. LOC_CMMREGISTER,LOC_MMREGISTER:
  1627. list.concat(taicpu.op_reg_reg_reg(asmop,S_NO,loc.register,src,resultreg));
  1628. else
  1629. internalerror(2013061104);
  1630. end;
  1631. { shuffle }
  1632. if resultreg<>dst then
  1633. begin
  1634. internalerror(2013061103);
  1635. end;
  1636. end;
  1637. procedure tcgx86.a_opmm_reg_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src1,src2,dst: tregister;shuffle : pmmshuffle);
  1638. var
  1639. l : tlocation;
  1640. begin
  1641. l.loc:=LOC_MMREGISTER;
  1642. l.register:=src1;
  1643. l.size:=size;
  1644. opmm_loc_reg_reg(list,op,size,l,src2,dst,shuffle);
  1645. end;
  1646. procedure tcgx86.a_opmm_ref_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; src,dst: tregister;shuffle : pmmshuffle);
  1647. var
  1648. l : tlocation;
  1649. begin
  1650. l.loc:=LOC_REFERENCE;
  1651. l.reference:=ref;
  1652. l.size:=size;
  1653. opmm_loc_reg_reg(list,op,size,l,src,dst,shuffle);
  1654. end;
  1655. procedure tcgx86.opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  1656. const
  1657. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1658. ( { scalar }
  1659. ( { OS_F32 }
  1660. A_NOP,A_NOP,A_ADDSS,A_NOP,A_DIVSS,A_NOP,A_NOP,A_MULSS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBSS,A_XORPS,A_NOP,A_NOP
  1661. ),
  1662. ( { OS_F64 }
  1663. A_NOP,A_NOP,A_ADDSD,A_NOP,A_DIVSD,A_NOP,A_NOP,A_MULSD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBSD,A_XORPD,A_NOP,A_NOP
  1664. )
  1665. ),
  1666. ( { vectorized/packed }
  1667. { because the logical packed single instructions have shorter op codes, we use always
  1668. these
  1669. }
  1670. ( { OS_F32 }
  1671. A_NOP,A_NOP,A_ADDPS,A_NOP,A_DIVPS,A_NOP,A_NOP,A_MULPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBPS,A_XORPS,A_NOP,A_NOP
  1672. ),
  1673. ( { OS_F64 }
  1674. A_NOP,A_NOP,A_ADDPD,A_NOP,A_DIVPD,A_NOP,A_NOP,A_MULPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_SUBPD,A_XORPD,A_NOP,A_NOP
  1675. )
  1676. )
  1677. );
  1678. opmm2asmop_avx : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1679. ( { scalar }
  1680. ( { OS_F32 }
  1681. A_NOP,A_NOP,A_VADDSS,A_NOP,A_VDIVSS,A_NOP,A_NOP,A_VMULSS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSS,A_VXORPS,A_NOP,A_NOP
  1682. ),
  1683. ( { OS_F64 }
  1684. A_NOP,A_NOP,A_VADDSD,A_NOP,A_VDIVSD,A_NOP,A_NOP,A_VMULSD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBSD,A_VXORPD,A_NOP,A_NOP
  1685. )
  1686. ),
  1687. ( { vectorized/packed }
  1688. { because the logical packed single instructions have shorter op codes, we use always
  1689. these
  1690. }
  1691. ( { OS_F32 }
  1692. A_NOP,A_NOP,A_VADDPS,A_NOP,A_VDIVPS,A_NOP,A_NOP,A_VMULPS,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPS,A_VXORPS,A_NOP,A_NOP
  1693. ),
  1694. ( { OS_F64 }
  1695. A_NOP,A_NOP,A_VADDPD,A_NOP,A_VDIVPD,A_NOP,A_NOP,A_VMULPD,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VSUBPD,A_VXORPD,A_NOP,A_NOP
  1696. )
  1697. )
  1698. );
  1699. opmm2asmop_full : array[topcg] of tasmop = (
  1700. A_NOP,A_NOP,A_NOP,A_PAND,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_POR,A_NOP,A_NOP,A_NOP,A_NOP,A_PXOR,A_NOP,A_NOP
  1701. );
  1702. opmm2asmop_full_avx : array[topcg] of tasmop = (
  1703. A_NOP,A_NOP,A_NOP,A_VPAND,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_NOP,A_VPOR,A_NOP,A_NOP,A_NOP,A_NOP,A_VPXOR,A_NOP,A_NOP
  1704. );
  1705. var
  1706. resultreg : tregister;
  1707. asmop : tasmop;
  1708. begin
  1709. { this is an internally used procedure so the parameters have
  1710. some constrains
  1711. }
  1712. if loc.size<>size then
  1713. internalerror(200312213);
  1714. resultreg:=dst;
  1715. { deshuffle }
  1716. //!!!
  1717. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1718. begin
  1719. internalerror(2010060101);
  1720. end
  1721. else if shuffle=nil then
  1722. begin
  1723. if UseAVX then
  1724. begin
  1725. asmop:=opmm2asmop_full_avx[op];
  1726. {$ifdef x86_64}
  1727. { A_VPXOR does not support the upper 16 registers }
  1728. if (asmop=A_VPXOR) and (FPUX86_HAS_32MMREGS in fpu_capabilities[current_settings.fputype]) then
  1729. asmop:=A_VPXORD;
  1730. {$endif x86_64}
  1731. if size in [OS_M256,OS_M512] then
  1732. Include(current_procinfo.flags,pi_uses_ymm);
  1733. end
  1734. else
  1735. asmop:=opmm2asmop_full[op];
  1736. end
  1737. else if shufflescalar(shuffle) then
  1738. begin
  1739. if UseAVX then
  1740. begin
  1741. asmop:=opmm2asmop_avx[0,size,op];
  1742. if size in [OS_M256,OS_M512] then
  1743. Include(current_procinfo.flags,pi_uses_ymm);
  1744. end
  1745. else
  1746. asmop:=opmm2asmop[0,size,op];
  1747. end
  1748. else
  1749. internalerror(200312211);
  1750. if asmop=A_NOP then
  1751. internalerror(200312216);
  1752. case loc.loc of
  1753. LOC_CREFERENCE,LOC_REFERENCE:
  1754. begin
  1755. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1756. if UseAVX then
  1757. list.concat(taicpu.op_ref_reg_reg(asmop,S_NO,loc.reference,resultreg,resultreg))
  1758. else
  1759. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  1760. end;
  1761. LOC_CMMREGISTER,LOC_MMREGISTER:
  1762. if UseAVX then
  1763. list.concat(taicpu.op_reg_reg_reg(asmop,S_NO,loc.register,resultreg,resultreg))
  1764. else
  1765. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  1766. else
  1767. internalerror(200312214);
  1768. end;
  1769. { shuffle }
  1770. if resultreg<>dst then
  1771. begin
  1772. internalerror(200312212);
  1773. end;
  1774. end;
  1775. {$ifndef i8086}
  1776. procedure tcgx86.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1777. a:tcgint;src,dst:Tregister);
  1778. var
  1779. power,al : longint;
  1780. href : treference;
  1781. begin
  1782. power:=0;
  1783. optimize_op_const(size,op,a);
  1784. case op of
  1785. OP_NONE:
  1786. begin
  1787. a_load_reg_reg(list,size,size,src,dst);
  1788. exit;
  1789. end;
  1790. OP_MOVE:
  1791. begin
  1792. a_load_const_reg(list,size,a,dst);
  1793. exit;
  1794. end;
  1795. else
  1796. ;
  1797. end;
  1798. if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1799. not(cs_check_overflow in current_settings.localswitches) and
  1800. (a>1) and ispowerof2(int64(a-1),power) and (power in [1..3]) then
  1801. begin
  1802. reference_reset_base(href,src,0,ctempposinvalid,0,[]);
  1803. href.index:=src;
  1804. href.scalefactor:=a-1;
  1805. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1806. end
  1807. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1808. not(cs_check_overflow in current_settings.localswitches) and
  1809. (a>1) and ispowerof2(int64(a),power) and (power in [1..3]) then
  1810. begin
  1811. reference_reset_base(href,NR_NO,0,ctempposinvalid,0,[]);
  1812. href.index:=src;
  1813. href.scalefactor:=a;
  1814. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1815. end
  1816. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_16,OS_S16,OS_32,OS_S32,OS_64,OS_S64]) and
  1817. (a>1) and (a<=maxLongint) and not ispowerof2(int64(a),power) then
  1818. begin
  1819. { MUL with overflow checking should be handled specifically in the code generator }
  1820. if (op=OP_MUL) and (cs_check_overflow in current_settings.localswitches) then
  1821. internalerror(2014011801);
  1822. list.concat(taicpu.op_const_reg_reg(A_IMUL,TCgSize2OpSize[size],a,src,dst));
  1823. end
  1824. else if (op=OP_ADD) and
  1825. ((size in [OS_32,OS_S32]) or
  1826. { lea supports only 32 bit signed displacments }
  1827. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1828. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1829. ) and
  1830. not(cs_check_overflow in current_settings.localswitches) then
  1831. begin
  1832. { a might still be in the range 0x80000000 to 0xffffffff
  1833. which might trigger a range check error as
  1834. reference_reset_base expects a longint value. }
  1835. {$push} {$R-}{$Q-}
  1836. al := longint (a);
  1837. {$pop}
  1838. reference_reset_base(href,src,al,ctempposinvalid,0,[]);
  1839. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1840. end
  1841. else if (op=OP_SHL) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1842. (int64(a)>=1) and (int64(a)<=3) then
  1843. begin
  1844. reference_reset_base(href,NR_NO,0,ctempposinvalid,0,[]);
  1845. href.index:=src;
  1846. href.scalefactor:=1 shl longint(a);
  1847. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1848. end
  1849. else if (op=OP_SUB) and
  1850. ((size in [OS_32,OS_S32]) or
  1851. { lea supports only 32 bit signed displacments }
  1852. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1853. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1854. ) and
  1855. not(cs_check_overflow in current_settings.localswitches) then
  1856. begin
  1857. reference_reset_base(href,src,-a,ctempposinvalid,0,[]);
  1858. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1859. end
  1860. else if (op in [OP_ROR,OP_ROL]) and
  1861. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1862. (size in [OS_32,OS_S32
  1863. {$ifdef x86_64}
  1864. ,OS_64,OS_S64
  1865. {$endif x86_64}
  1866. ]) then
  1867. begin
  1868. if op=OP_ROR then
  1869. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size], a,src,dst))
  1870. else
  1871. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size],TCgSize2Size[size]*8-a,src,dst));
  1872. end
  1873. else
  1874. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1875. end;
  1876. procedure tcgx86.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1877. size: tcgsize; src1, src2, dst: tregister);
  1878. var
  1879. href : treference;
  1880. begin
  1881. if (op=OP_ADD) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1882. not(cs_check_overflow in current_settings.localswitches) then
  1883. begin
  1884. reference_reset_base(href,src1,0,ctempposinvalid,0,[]);
  1885. href.index:=src2;
  1886. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1887. end
  1888. else if (op in [OP_SHR,OP_SHL]) and
  1889. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1890. (size in [OS_32,OS_S32
  1891. {$ifdef x86_64}
  1892. ,OS_64,OS_S64
  1893. {$endif x86_64}
  1894. ]) then
  1895. begin
  1896. if op=OP_SHL then
  1897. list.concat(taicpu.op_reg_reg_reg(A_SHLX,TCgSize2OpSize[size],src1,src2,dst))
  1898. else
  1899. list.concat(taicpu.op_reg_reg_reg(A_SHRX,TCgSize2OpSize[size],src1,src2,dst));
  1900. end
  1901. else
  1902. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1903. end;
  1904. {$endif not i8086}
  1905. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  1906. {$ifdef x86_64}
  1907. var
  1908. tmpreg : tregister;
  1909. {$endif x86_64}
  1910. begin
  1911. optimize_op_const(size, op, a);
  1912. {$ifdef x86_64}
  1913. { x86_64 only supports signed 32 bits constants directly }
  1914. if not(op in [OP_NONE,OP_MOVE]) and
  1915. (size in [OS_S64,OS_64]) and
  1916. ((a<low(longint)) or (a>high(longint))) then
  1917. begin
  1918. tmpreg:=getintregister(list,size);
  1919. a_load_const_reg(list,size,a,tmpreg);
  1920. a_op_reg_reg(list,op,size,tmpreg,reg);
  1921. exit;
  1922. end;
  1923. {$endif x86_64}
  1924. check_register_size(size,reg);
  1925. case op of
  1926. OP_NONE :
  1927. begin
  1928. { Opcode is optimized away }
  1929. end;
  1930. OP_MOVE :
  1931. begin
  1932. { Optimized, replaced with a simple load }
  1933. a_load_const_reg(list,size,a,reg);
  1934. end;
  1935. OP_DIV, OP_IDIV:
  1936. begin
  1937. { should be handled specifically in the code }
  1938. { generator because of the silly register usage restraints }
  1939. internalerror(200109224);
  1940. end;
  1941. OP_MUL,OP_IMUL:
  1942. begin
  1943. if not (cs_check_overflow in current_settings.localswitches) then
  1944. op:=OP_IMUL;
  1945. if op = OP_IMUL then
  1946. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  1947. else
  1948. { OP_MUL should be handled specifically in the code }
  1949. { generator because of the silly register usage restraints }
  1950. internalerror(200109225);
  1951. end;
  1952. OP_ADD, OP_SUB:
  1953. if not(cs_check_overflow in current_settings.localswitches) and
  1954. (a = 1) and
  1955. UseIncDec then
  1956. begin
  1957. if op = OP_ADD then
  1958. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  1959. else
  1960. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  1961. end
  1962. else
  1963. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],ImmInt(a),reg));
  1964. OP_AND,OP_OR:
  1965. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],ImmInt(a),reg));
  1966. OP_XOR:
  1967. if (aword(a)=high(aword)) then
  1968. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg))
  1969. else
  1970. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],ImmInt(a),reg));
  1971. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1972. begin
  1973. {$if defined(x86_64)}
  1974. if (a and 63) <> 0 Then
  1975. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,reg));
  1976. if (a shr 6) <> 0 Then
  1977. internalerror(200609073);
  1978. {$elseif defined(i386)}
  1979. if (a and 31) <> 0 Then
  1980. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1981. if (a shr 5) <> 0 Then
  1982. internalerror(200609071);
  1983. {$elseif defined(i8086)}
  1984. if (a shr 5) <> 0 Then
  1985. internalerror(2013043002);
  1986. a := a and 31;
  1987. if a <> 0 Then
  1988. begin
  1989. if (current_settings.cputype < cpu_186) and (a <> 1) then
  1990. begin
  1991. getcpuregister(list,NR_CL);
  1992. a_load_const_reg(list,OS_8,a,NR_CL);
  1993. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,reg));
  1994. ungetcpuregister(list,NR_CL);
  1995. end
  1996. else
  1997. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  1998. end;
  1999. {$endif}
  2000. end
  2001. else internalerror(200609072);
  2002. end;
  2003. end;
  2004. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  2005. var
  2006. {$ifdef x86_64}
  2007. tmpreg : tregister;
  2008. {$endif x86_64}
  2009. tmpref : treference;
  2010. begin
  2011. optimize_op_const(size, op, a);
  2012. if op in [OP_NONE,OP_MOVE] then
  2013. begin
  2014. if (op=OP_MOVE) then
  2015. a_load_const_ref(list,size,a,ref);
  2016. exit;
  2017. end;
  2018. {$ifdef x86_64}
  2019. { x86_64 only supports signed 32 bits constants directly }
  2020. if (size in [OS_S64,OS_64]) and
  2021. ((a<low(longint)) or (a>high(longint))) then
  2022. begin
  2023. tmpreg:=getintregister(list,size);
  2024. a_load_const_reg(list,size,a,tmpreg);
  2025. a_op_reg_ref(list,op,size,tmpreg,ref);
  2026. exit;
  2027. end;
  2028. {$endif x86_64}
  2029. tmpref:=ref;
  2030. make_simple_ref(list,tmpref);
  2031. Case Op of
  2032. OP_DIV, OP_IDIV:
  2033. Begin
  2034. { should be handled specifically in the code }
  2035. { generator because of the silly register usage restraints }
  2036. internalerror(200109231);
  2037. End;
  2038. OP_MUL,OP_IMUL:
  2039. begin
  2040. if not (cs_check_overflow in current_settings.localswitches) then
  2041. op:=OP_IMUL;
  2042. { can't multiply a memory location directly with a constant }
  2043. if op = OP_IMUL then
  2044. inherited a_op_const_ref(list,op,size,a,tmpref)
  2045. else
  2046. { OP_MUL should be handled specifically in the code }
  2047. { generator because of the silly register usage restraints }
  2048. internalerror(200109232);
  2049. end;
  2050. OP_ADD, OP_SUB:
  2051. if not(cs_check_overflow in current_settings.localswitches) and
  2052. (a = 1) and
  2053. UseIncDec then
  2054. begin
  2055. if op = OP_ADD then
  2056. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  2057. else
  2058. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  2059. end
  2060. else
  2061. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  2062. OP_AND,OP_OR:
  2063. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  2064. OP_XOR:
  2065. if (aword(a)=high(aword)) then
  2066. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref))
  2067. else
  2068. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  2069. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  2070. begin
  2071. {$if defined(x86_64)}
  2072. if (a and 63) <> 0 Then
  2073. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,tmpref));
  2074. if (a shr 6) <> 0 Then
  2075. internalerror(2013111003);
  2076. {$elseif defined(i386)}
  2077. if (a and 31) <> 0 Then
  2078. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  2079. if (a shr 5) <> 0 Then
  2080. internalerror(2013111002);
  2081. {$elseif defined(i8086)}
  2082. if (a shr 5) <> 0 Then
  2083. internalerror(2013111001);
  2084. a := a and 31;
  2085. if a <> 0 Then
  2086. begin
  2087. if (current_settings.cputype < cpu_186) and (a <> 1) then
  2088. begin
  2089. getcpuregister(list,NR_CL);
  2090. a_load_const_reg(list,OS_8,a,NR_CL);
  2091. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,tmpref));
  2092. ungetcpuregister(list,NR_CL);
  2093. end
  2094. else
  2095. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  2096. end;
  2097. {$endif}
  2098. end
  2099. else internalerror(68992);
  2100. end;
  2101. end;
  2102. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  2103. const
  2104. {$if defined(cpu64bitalu)}
  2105. REGCX=NR_RCX;
  2106. REGCX_Size = OS_64;
  2107. {$elseif defined(cpu32bitalu)}
  2108. REGCX=NR_ECX;
  2109. REGCX_Size = OS_32;
  2110. {$elseif defined(cpu16bitalu)}
  2111. REGCX=NR_CX;
  2112. REGCX_Size = OS_16;
  2113. {$endif}
  2114. var
  2115. dstsize: topsize;
  2116. instr:Taicpu;
  2117. begin
  2118. if not(Op in [OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR]) then
  2119. check_register_size(size,src);
  2120. check_register_size(size,dst);
  2121. dstsize := tcgsize2opsize[size];
  2122. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  2123. op:=OP_IMUL;
  2124. case op of
  2125. OP_NEG,OP_NOT:
  2126. begin
  2127. if src<>dst then
  2128. a_load_reg_reg(list,size,size,src,dst);
  2129. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  2130. end;
  2131. OP_MUL,OP_DIV,OP_IDIV:
  2132. { special stuff, needs separate handling inside code }
  2133. { generator }
  2134. internalerror(200109233);
  2135. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  2136. begin
  2137. { Use ecx to load the value, that allows better coalescing }
  2138. getcpuregister(list,REGCX);
  2139. a_load_reg_reg(list,reg_cgsize(src),REGCX_Size,src,REGCX);
  2140. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  2141. ungetcpuregister(list,REGCX);
  2142. end;
  2143. else
  2144. begin
  2145. if reg2opsize(src) <> dstsize then
  2146. internalerror(200109226);
  2147. { x86 does not have an 8 Bit imul, so do 16 Bit multiplication
  2148. we do not need to zero/sign extend as we discard the upper bits anyways }
  2149. if (TOpCG2AsmOp[op]=A_IMUL) and (size in [OS_8,OS_S8]) then
  2150. begin
  2151. { this might only happen if no overflow checking is done }
  2152. if cs_check_overflow in current_settings.localswitches then
  2153. Internalerror(2021011601);
  2154. src:=makeregsize(list,src,OS_16);
  2155. dst:=makeregsize(list,dst,OS_16);
  2156. dstsize:=S_W;
  2157. end;
  2158. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  2159. list.concat(instr);
  2160. end;
  2161. end;
  2162. end;
  2163. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  2164. var
  2165. tmpref : treference;
  2166. begin
  2167. tmpref:=ref;
  2168. make_simple_ref(list,tmpref);
  2169. check_register_size(size,reg);
  2170. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  2171. op:=OP_IMUL;
  2172. case op of
  2173. OP_NEG,OP_NOT:
  2174. begin
  2175. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  2176. end;
  2177. OP_MUL,OP_DIV,OP_IDIV:
  2178. { special stuff, needs separate handling inside code }
  2179. { generator }
  2180. internalerror(200109239);
  2181. else
  2182. begin
  2183. reg := makeregsize(list,reg,size);
  2184. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  2185. end;
  2186. end;
  2187. end;
  2188. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  2189. const
  2190. {$if defined(cpu64bitalu)}
  2191. REGCX=NR_RCX;
  2192. REGCX_Size = OS_64;
  2193. {$elseif defined(cpu32bitalu)}
  2194. REGCX=NR_ECX;
  2195. REGCX_Size = OS_32;
  2196. {$elseif defined(cpu16bitalu)}
  2197. REGCX=NR_CX;
  2198. REGCX_Size = OS_16;
  2199. {$endif}
  2200. var
  2201. tmpref : treference;
  2202. begin
  2203. tmpref:=ref;
  2204. make_simple_ref(list,tmpref);
  2205. { we don't check the register size for some operations, for the following reasons:
  2206. SHR,SHL,SAR,ROL,ROR:
  2207. We allow the register size to differ from the destination size.
  2208. This allows generating better code when performing, for example, a
  2209. shift/rotate in place (x:=x shl y) of a byte variable. In this case,
  2210. we allow the shift count (y) to be located in a 32-bit register,
  2211. even though x is a byte. This:
  2212. - reduces register pressure on i386 (because only EAX,EBX,ECX and
  2213. EDX have 8-bit subregisters)
  2214. - avoids partial register writes, which can cause various
  2215. performance issues on modern out-of-order execution x86 CPUs }
  2216. if not (op in [OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR]) then
  2217. check_register_size(size,reg);
  2218. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  2219. op:=OP_IMUL;
  2220. case op of
  2221. OP_NEG,OP_NOT:
  2222. inherited;
  2223. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  2224. begin
  2225. { Use ecx to load the value, that allows better coalescing }
  2226. getcpuregister(list,REGCX);
  2227. a_load_reg_reg(list,reg_cgsize(reg),REGCX_Size,reg,REGCX);
  2228. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],NR_CL,tmpref));
  2229. ungetcpuregister(list,REGCX);
  2230. end;
  2231. OP_IMUL:
  2232. begin
  2233. { this one needs a load/imul/store, which is the default }
  2234. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  2235. end;
  2236. OP_MUL,OP_DIV,OP_IDIV:
  2237. { special stuff, needs separate handling inside code }
  2238. { generator }
  2239. internalerror(200109238);
  2240. else
  2241. begin
  2242. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  2243. end;
  2244. end;
  2245. end;
  2246. procedure tcgx86.a_op_ref(list: TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference);
  2247. var
  2248. tmpref: treference;
  2249. begin
  2250. if not (Op in [OP_NOT,OP_NEG]) then
  2251. internalerror(2020050705);
  2252. tmpref:=ref;
  2253. make_simple_ref(list,tmpref);
  2254. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  2255. end;
  2256. procedure tcgx86.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister);
  2257. var
  2258. tmpreg: tregister;
  2259. opsize: topsize;
  2260. l : TAsmLabel;
  2261. begin
  2262. { no bsf/bsr for byte }
  2263. if srcsize in [OS_8,OS_S8] then
  2264. begin
  2265. tmpreg:=getintregister(list,OS_INT);
  2266. a_load_reg_reg(list,srcsize,OS_INT,src,tmpreg);
  2267. src:=tmpreg;
  2268. srcsize:=OS_INT;
  2269. end;
  2270. { source and destination register must have the same size }
  2271. if tcgsize2size[srcsize]<>tcgsize2size[dstsize] then
  2272. tmpreg:=getintregister(list,srcsize)
  2273. else
  2274. tmpreg:=dst;
  2275. opsize:=tcgsize2opsize[srcsize];
  2276. if not reverse then
  2277. list.concat(taicpu.op_reg_reg(A_BSF,opsize,src,tmpreg))
  2278. else
  2279. list.concat(taicpu.op_reg_reg(A_BSR,opsize,src,tmpreg));
  2280. current_asmdata.getjumplabel(l);
  2281. a_jmp_cond(list,OC_NE,l);
  2282. list.concat(taicpu.op_const_reg(A_MOV,opsize,$ff,tmpreg));
  2283. a_label(list,l);
  2284. if tmpreg<>dst then
  2285. a_load_reg_reg(list,srcsize,dstsize,tmpreg,dst);
  2286. end;
  2287. {*************** compare instructructions ****************}
  2288. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  2289. l : tasmlabel);
  2290. {$ifdef x86_64}
  2291. var
  2292. tmpreg : tregister;
  2293. {$endif x86_64}
  2294. begin
  2295. {$ifdef x86_64}
  2296. { x86_64 only supports signed 32 bits constants directly }
  2297. if (size in [OS_S64,OS_64]) and
  2298. ((a<low(longint)) or (a>high(longint))) then
  2299. begin
  2300. tmpreg:=getintregister(list,size);
  2301. a_load_const_reg(list,size,a,tmpreg);
  2302. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2303. exit;
  2304. end;
  2305. {$endif x86_64}
  2306. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2307. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  2308. a_jmp_cond(list,cmp_op,l);
  2309. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2310. end;
  2311. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  2312. l : tasmlabel);
  2313. var
  2314. {$ifdef x86_64}
  2315. tmpreg : tregister;
  2316. {$endif x86_64}
  2317. tmpref : treference;
  2318. begin
  2319. tmpref:=ref;
  2320. make_simple_ref(list,tmpref);
  2321. {$ifdef x86_64}
  2322. { x86_64 only supports signed 32 bits constants directly }
  2323. if (size in [OS_S64,OS_64]) and
  2324. ((a<low(longint)) or (a>high(longint))) then
  2325. begin
  2326. tmpreg:=getintregister(list,size);
  2327. a_load_const_reg(list,size,a,tmpreg);
  2328. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  2329. exit;
  2330. end;
  2331. {$endif x86_64}
  2332. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  2333. a_jmp_cond(list,cmp_op,l);
  2334. end;
  2335. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  2336. reg1,reg2 : tregister;l : tasmlabel);
  2337. begin
  2338. check_register_size(size,reg1);
  2339. check_register_size(size,reg2);
  2340. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  2341. a_jmp_cond(list,cmp_op,l);
  2342. end;
  2343. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  2344. var
  2345. tmpref : treference;
  2346. begin
  2347. tmpref:=ref;
  2348. make_simple_ref(list,tmpref);
  2349. check_register_size(size,reg);
  2350. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  2351. a_jmp_cond(list,cmp_op,l);
  2352. end;
  2353. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  2354. var
  2355. tmpref : treference;
  2356. begin
  2357. tmpref:=ref;
  2358. make_simple_ref(list,tmpref);
  2359. check_register_size(size,reg);
  2360. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  2361. a_jmp_cond(list,cmp_op,l);
  2362. end;
  2363. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  2364. var
  2365. ai : taicpu;
  2366. begin
  2367. if cond=OC_None then
  2368. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  2369. else
  2370. begin
  2371. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  2372. ai.SetCondition(TOpCmp2AsmCond[cond]);
  2373. end;
  2374. ai.is_jmp:=true;
  2375. list.concat(ai);
  2376. end;
  2377. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  2378. var
  2379. ai : taicpu;
  2380. hl : tasmlabel;
  2381. f2 : tresflags;
  2382. begin
  2383. hl:=nil;
  2384. f2:=f;
  2385. case f of
  2386. F_FNE:
  2387. begin
  2388. ai:=Taicpu.op_sym(A_Jcc,S_NO,l);
  2389. ai.SetCondition(C_P);
  2390. ai.is_jmp:=true;
  2391. list.concat(ai);
  2392. f2:=F_NE;
  2393. end;
  2394. F_FE,F_FA,F_FAE,F_FB,F_FBE:
  2395. begin
  2396. { JP before JA/JAE is redundant, but it must be generated here
  2397. and left for peephole optimizer to remove. }
  2398. current_asmdata.getjumplabel(hl);
  2399. ai:=Taicpu.op_sym(A_Jcc,S_NO,hl);
  2400. ai.SetCondition(C_P);
  2401. ai.is_jmp:=true;
  2402. list.concat(ai);
  2403. f2:=FPUFlags2Flags[f];
  2404. end;
  2405. else
  2406. ;
  2407. end;
  2408. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  2409. ai.SetCondition(flags_to_cond(f2));
  2410. ai.is_jmp := true;
  2411. list.concat(ai);
  2412. if assigned(hl) then
  2413. a_label(list,hl);
  2414. end;
  2415. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  2416. var
  2417. ai : taicpu;
  2418. f2 : tresflags;
  2419. hreg,hreg2 : tregister;
  2420. op: tasmop;
  2421. begin
  2422. hreg2:=NR_NO;
  2423. op:=A_AND;
  2424. f2:=f;
  2425. case f of
  2426. F_FE,F_FNE,F_FB,F_FBE:
  2427. begin
  2428. hreg2:=getintregister(list,OS_8);
  2429. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg2);
  2430. if (f=F_FNE) then { F_FNE means "PF or (not ZF)" }
  2431. begin
  2432. ai.setcondition(C_P);
  2433. op:=A_OR;
  2434. end
  2435. else
  2436. ai.setcondition(C_NP);
  2437. list.concat(ai);
  2438. f2:=FPUFlags2Flags[f];
  2439. end;
  2440. F_FA,F_FAE: { These do not need PF check }
  2441. f2:=FPUFlags2Flags[f];
  2442. else
  2443. ;
  2444. end;
  2445. hreg:=makeregsize(list,reg,OS_8);
  2446. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  2447. ai.setcondition(flags_to_cond(f2));
  2448. list.concat(ai);
  2449. if (hreg2<>NR_NO) then
  2450. list.concat(taicpu.op_reg_reg(op,S_B,hreg2,hreg));
  2451. if reg<>hreg then
  2452. a_load_reg_reg(list,OS_8,size,hreg,reg);
  2453. end;
  2454. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  2455. var
  2456. ai : taicpu;
  2457. tmpref : treference;
  2458. f2 : tresflags;
  2459. begin
  2460. f2:=f;
  2461. case f of
  2462. F_FE,F_FNE,F_FB,F_FBE:
  2463. begin
  2464. inherited g_flags2ref(list,size,f,ref);
  2465. exit;
  2466. end;
  2467. F_FA,F_FAE:
  2468. f2:=FPUFlags2Flags[f];
  2469. else
  2470. ;
  2471. end;
  2472. tmpref:=ref;
  2473. make_simple_ref(list,tmpref);
  2474. if not(size in [OS_8,OS_S8]) then
  2475. a_load_const_ref(list,size,0,tmpref);
  2476. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  2477. ai.setcondition(flags_to_cond(f2));
  2478. list.concat(ai);
  2479. {$ifndef cpu64bitalu}
  2480. if size in [OS_S64,OS_64] then
  2481. begin
  2482. inc(tmpref.offset,4);
  2483. a_load_const_ref(list,OS_32,0,tmpref);
  2484. end;
  2485. {$endif cpu64bitalu}
  2486. end;
  2487. { ************* concatcopy ************ }
  2488. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:tcgint);
  2489. const
  2490. {$if defined(cpu64bitalu)}
  2491. REGCX=NR_RCX;
  2492. REGSI=NR_RSI;
  2493. REGDI=NR_RDI;
  2494. copy_len_sizes = [1, 2, 4, 8];
  2495. push_segment_size = S_L;
  2496. {$elseif defined(cpu32bitalu)}
  2497. REGCX=NR_ECX;
  2498. REGSI=NR_ESI;
  2499. REGDI=NR_EDI;
  2500. copy_len_sizes = [1, 2, 4];
  2501. push_segment_size = S_L;
  2502. {$elseif defined(cpu16bitalu)}
  2503. REGCX=NR_CX;
  2504. REGSI=NR_SI;
  2505. REGDI=NR_DI;
  2506. copy_len_sizes = [1, 2, 4]; { 4 is included here, because it's still more
  2507. efficient to use copy_move instead of copy_string for copying 4 bytes }
  2508. push_segment_size = S_W;
  2509. {$endif}
  2510. type
  2511. copymode=(copy_move,copy_mmx,copy_string,copy_mm,copy_avx,copy_avx512);
  2512. var srcref,dstref,tmpref:Treference;
  2513. r,r0,r1,r2,r3:Tregister;
  2514. helpsize:tcgint;
  2515. copysize:byte;
  2516. cgsize:Tcgsize;
  2517. cm:copymode;
  2518. saved_ds,saved_es: Boolean;
  2519. hlist: TAsmList;
  2520. begin
  2521. srcref:=source;
  2522. dstref:=dest;
  2523. {$ifndef i8086}
  2524. make_simple_ref(list,srcref);
  2525. make_simple_ref(list,dstref);
  2526. {$endif not i8086}
  2527. {$ifdef i386}
  2528. { we could handle "far" pointers here, but reloading es/ds is probably much slower
  2529. than just resolving the tls segment }
  2530. if (srcref.refaddr=addr_ntpoff) and (srcref.segment=NR_GS) then
  2531. begin
  2532. r:=getaddressregister(list);
  2533. a_loadaddr_ref_reg(list,srcref,r);
  2534. reference_reset(srcref,srcref.alignment,srcref.volatility);
  2535. srcref.base:=r;
  2536. end;
  2537. if (dstref.refaddr=addr_ntpoff) and (dstref.segment=NR_GS) then
  2538. begin
  2539. r:=getaddressregister(list);
  2540. a_loadaddr_ref_reg(list,dstref,r);
  2541. reference_reset(dstref,dstref.alignment,dstref.volatility);
  2542. dstref.base:=r;
  2543. end;
  2544. {$endif i386}
  2545. {$ifdef x86_64}
  2546. { we could handle "far" pointers here, but reloading es/ds is probably much slower
  2547. than just resolving the tls segment }
  2548. if (srcref.refaddr=addr_tpoff) and (srcref.segment=NR_FS) then
  2549. begin
  2550. r:=getaddressregister(list);
  2551. a_loadaddr_ref_reg(list,srcref,r);
  2552. reference_reset(srcref,srcref.alignment,srcref.volatility);
  2553. srcref.base:=r;
  2554. end;
  2555. if (dstref.refaddr=addr_tpoff) and (dstref.segment=NR_FS) then
  2556. begin
  2557. r:=getaddressregister(list);
  2558. a_loadaddr_ref_reg(list,dstref,r);
  2559. reference_reset(dstref,dstref.alignment,dstref.volatility);
  2560. dstref.base:=r;
  2561. end;
  2562. {$endif x86_64}
  2563. cm:=copy_move;
  2564. helpsize:=3*sizeof(aword);
  2565. if cs_opt_size in current_settings.optimizerswitches then
  2566. helpsize:=2*sizeof(aword);
  2567. {$ifndef i8086}
  2568. { avx helps only to reduce size, using it in general does at least not help on
  2569. an i7-4770
  2570. but using the xmm registers reduces register pressure (FK) }
  2571. if (FPUX86_HAS_AVXUNIT in fpu_capabilities[current_settings.fputype]) and
  2572. ((len mod 4)=0) and (len<=48) {$ifndef i386}and (len>=16){$endif i386} then
  2573. cm:=copy_avx
  2574. else if (FPUX86_HAS_AVX512F in fpu_capabilities[current_settings.fputype]) and
  2575. ((len mod 4)=0) and (len<=128) {$ifndef i386}and (len>=16){$endif i386} then
  2576. cm:=copy_avx512
  2577. else
  2578. { I'am not sure what CPUs would benefit from using sse instructions for moves
  2579. but using the xmm registers reduces register pressure (FK) }
  2580. if
  2581. {$ifdef x86_64}
  2582. ((current_settings.fputype>=fpu_sse64)
  2583. {$else x86_64}
  2584. ((current_settings.fputype>=fpu_sse)
  2585. {$endif x86_64}
  2586. or (CPUX86_HAS_SSE2 in cpu_capabilities[current_settings.cputype])) and
  2587. ({$ifdef i386}(len=8) or {$endif i386}(len=16) or (len=24) or (len=32) or (len=40) or (len=48)) then
  2588. cm:=copy_mm
  2589. else
  2590. {$endif i8086}
  2591. if (cs_mmx in current_settings.localswitches) and
  2592. not(pi_uses_fpu in current_procinfo.flags) and
  2593. ((len=8) or (len=16) or (len=24) or (len=32)) then
  2594. cm:=copy_mmx
  2595. else
  2596. if len>helpsize then
  2597. cm:=copy_string;
  2598. if (cs_opt_size in current_settings.optimizerswitches) and
  2599. not((len<=16) and (cm in [copy_mmx,copy_mm,copy_avx])) and
  2600. not(len in copy_len_sizes) then
  2601. cm:=copy_string;
  2602. {$ifndef i8086}
  2603. { using %fs and %gs as segment prefixes is perfectly valid }
  2604. if ((srcref.segment<>NR_NO) and (srcref.segment<>NR_FS) and (srcref.segment<>NR_GS)) or
  2605. ((dstref.segment<>NR_NO) and (dstref.segment<>NR_FS) and (dstref.segment<>NR_GS)) then
  2606. cm:=copy_string;
  2607. {$endif not i8086}
  2608. case cm of
  2609. copy_move:
  2610. begin
  2611. copysize:=sizeof(aint);
  2612. cgsize:=int_cgsize(copysize);
  2613. while len<>0 do
  2614. begin
  2615. if len<2 then
  2616. begin
  2617. copysize:=1;
  2618. cgsize:=OS_8;
  2619. end
  2620. else if len<4 then
  2621. begin
  2622. copysize:=2;
  2623. cgsize:=OS_16;
  2624. end
  2625. {$if defined(cpu32bitalu) or defined(cpu64bitalu)}
  2626. else if len<8 then
  2627. begin
  2628. copysize:=4;
  2629. cgsize:=OS_32;
  2630. end
  2631. {$endif cpu32bitalu or cpu64bitalu}
  2632. {$ifdef cpu64bitalu}
  2633. else if len<16 then
  2634. begin
  2635. copysize:=8;
  2636. cgsize:=OS_64;
  2637. end
  2638. {$endif}
  2639. ;
  2640. dec(len,copysize);
  2641. r:=getintregister(list,cgsize);
  2642. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2643. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2644. inc(srcref.offset,copysize);
  2645. inc(dstref.offset,copysize);
  2646. end;
  2647. end;
  2648. copy_mmx:
  2649. begin
  2650. r0:=getmmxregister(list);
  2651. r1:=NR_NO;
  2652. r2:=NR_NO;
  2653. r3:=NR_NO;
  2654. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  2655. if len>=16 then
  2656. begin
  2657. inc(srcref.offset,8);
  2658. r1:=getmmxregister(list);
  2659. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  2660. end;
  2661. if len>=24 then
  2662. begin
  2663. inc(srcref.offset,8);
  2664. r2:=getmmxregister(list);
  2665. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  2666. end;
  2667. if len>=32 then
  2668. begin
  2669. inc(srcref.offset,8);
  2670. r3:=getmmxregister(list);
  2671. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2672. end;
  2673. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  2674. if len>=16 then
  2675. begin
  2676. inc(dstref.offset,8);
  2677. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  2678. end;
  2679. if len>=24 then
  2680. begin
  2681. inc(dstref.offset,8);
  2682. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  2683. end;
  2684. if len>=32 then
  2685. begin
  2686. inc(dstref.offset,8);
  2687. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2688. end;
  2689. end;
  2690. copy_mm:
  2691. begin
  2692. r0:=NR_NO;
  2693. r1:=NR_NO;
  2694. r2:=NR_NO;
  2695. r3:=NR_NO;
  2696. if len>=16 then
  2697. begin
  2698. r0:=getmmregister(list,OS_M128);
  2699. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r0,nil);
  2700. inc(srcref.offset,16);
  2701. end;
  2702. if len>=32 then
  2703. begin
  2704. r1:=getmmregister(list,OS_M128);
  2705. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r1,nil);
  2706. inc(srcref.offset,16);
  2707. end;
  2708. if len>=48 then
  2709. begin
  2710. r2:=getmmregister(list,OS_M128);
  2711. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r2,nil);
  2712. inc(srcref.offset,16);
  2713. end;
  2714. if (len=8) or (len=24) or (len=40) then
  2715. begin
  2716. r3:=getmmregister(list,OS_M64);
  2717. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2718. end;
  2719. if len>=16 then
  2720. begin
  2721. a_loadmm_reg_ref(list,OS_M128,OS_M128,r0,dstref,nil);
  2722. inc(dstref.offset,16);
  2723. end;
  2724. if len>=32 then
  2725. begin
  2726. a_loadmm_reg_ref(list,OS_M128,OS_M128,r1,dstref,nil);
  2727. inc(dstref.offset,16);
  2728. end;
  2729. if len>=48 then
  2730. begin
  2731. a_loadmm_reg_ref(list,OS_M128,OS_M128,r2,dstref,nil);
  2732. inc(dstref.offset,16);
  2733. end;
  2734. if (len=8) or (len=24) or (len=40) then
  2735. begin
  2736. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2737. end;
  2738. end;
  2739. copy_avx512,
  2740. copy_avx:
  2741. begin
  2742. hlist:=TAsmList.create;
  2743. if cm=copy_avx512 then
  2744. while len>=64 do
  2745. begin
  2746. r0:=getmmregister(list,OS_M512);
  2747. a_loadmm_ref_reg(list,OS_M512,OS_M512,srcref,r0,nil);
  2748. a_loadmm_reg_ref(hlist,OS_M512,OS_M512,r0,dstref,nil);
  2749. inc(srcref.offset,64);
  2750. inc(dstref.offset,64);
  2751. dec(len,64);
  2752. Include(current_procinfo.flags,pi_uses_ymm);
  2753. end;
  2754. while len>=32 do
  2755. begin
  2756. r0:=getmmregister(list,OS_M256);
  2757. a_loadmm_ref_reg(list,OS_M256,OS_M256,srcref,r0,nil);
  2758. a_loadmm_reg_ref(hlist,OS_M256,OS_M256,r0,dstref,nil);
  2759. inc(srcref.offset,32);
  2760. inc(dstref.offset,32);
  2761. dec(len,32);
  2762. Include(current_procinfo.flags,pi_uses_ymm);
  2763. end;
  2764. while len>=16 do
  2765. begin
  2766. r0:=getmmregister(list,OS_M128);
  2767. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r0,nil);
  2768. a_loadmm_reg_ref(hlist,OS_M128,OS_M128,r0,dstref,nil);
  2769. inc(srcref.offset,16);
  2770. inc(dstref.offset,16);
  2771. dec(len,16);
  2772. end;
  2773. if len>=8 then
  2774. begin
  2775. r0:=getmmregister(list,OS_M64);
  2776. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  2777. a_loadmm_reg_ref(hlist,OS_M64,OS_M64,r0,dstref,nil);
  2778. inc(srcref.offset,8);
  2779. inc(dstref.offset,8);
  2780. dec(len,8);
  2781. end;
  2782. if len>=4 then
  2783. begin
  2784. r0:=getintregister(list,OS_32);
  2785. a_load_ref_reg(list,OS_32,OS_32,srcref,r0);
  2786. a_load_reg_ref(hlist,OS_32,OS_32,r0,dstref);
  2787. inc(srcref.offset,4);
  2788. inc(dstref.offset,4);
  2789. dec(len,4);
  2790. end;
  2791. list.concatList(hlist);
  2792. hlist.free;
  2793. end
  2794. else {copy_string, should be a good fallback in case of unhandled}
  2795. begin
  2796. getcpuregister(list,REGDI);
  2797. if (dstref.segment=NR_NO) and
  2798. (segment_regs_equal(NR_SS,NR_DS) or ((dstref.base<>NR_BP) and (dstref.base<>NR_SP))) then
  2799. begin
  2800. a_loadaddr_ref_reg(list,dstref,REGDI);
  2801. saved_es:=false;
  2802. {$ifdef volatile_es}
  2803. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2804. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2805. {$endif volatile_es}
  2806. end
  2807. else
  2808. begin
  2809. { load offset of dest. reference }
  2810. tmpref:=dstref;
  2811. tmpref.segment:=NR_NO;
  2812. a_loadaddr_ref_reg(list,tmpref,REGDI);
  2813. {$ifdef volatile_es}
  2814. saved_es:=false;
  2815. {$else volatile_es}
  2816. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_ES));
  2817. saved_es:=true;
  2818. {$endif volatile_es}
  2819. if dstref.segment<>NR_NO then
  2820. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,dstref.segment))
  2821. else if (dstref.base=NR_BP) or (dstref.base=NR_SP) then
  2822. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2823. else
  2824. internalerror(2014040401);
  2825. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2826. end;
  2827. getcpuregister(list,REGSI);
  2828. {$ifdef i8086}
  2829. { at this point, si and di are allocated, so no register is available as index =>
  2830. compiler will hang/ie during spilling, so avoid that srcref has base and index, see also tests/tbs/tb0637.pp }
  2831. if (srcref.base<>NR_NO) and (srcref.index<>NR_NO) then
  2832. begin
  2833. r:=getaddressregister(list);
  2834. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,srcref.base,srcref.index,r);
  2835. srcref.base:=r;
  2836. srcref.index:=NR_NO;
  2837. end;
  2838. {$endif i8086}
  2839. if ((srcref.segment=NR_NO) and (segment_regs_equal(NR_SS,NR_DS) or ((srcref.base<>NR_BP) and (srcref.base<>NR_SP)))) or
  2840. (is_segment_reg(srcref.segment) and segment_regs_equal(srcref.segment,NR_DS)) then
  2841. begin
  2842. srcref.segment:=NR_NO;
  2843. a_loadaddr_ref_reg(list,srcref,REGSI);
  2844. saved_ds:=false;
  2845. end
  2846. else
  2847. begin
  2848. { load offset of source reference }
  2849. tmpref:=srcref;
  2850. tmpref.segment:=NR_NO;
  2851. a_loadaddr_ref_reg(list,tmpref,REGSI);
  2852. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2853. saved_ds:=true;
  2854. if srcref.segment<>NR_NO then
  2855. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,srcref.segment))
  2856. else if (srcref.base=NR_BP) or (srcref.base=NR_SP) then
  2857. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2858. else
  2859. internalerror(2014040402);
  2860. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2861. end;
  2862. getcpuregister(list,REGCX);
  2863. if ts_cld in current_settings.targetswitches then
  2864. list.concat(Taicpu.op_none(A_CLD,S_NO));
  2865. if (cs_opt_size in current_settings.optimizerswitches) and
  2866. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  2867. begin
  2868. a_load_const_reg(list,OS_INT,len,REGCX);
  2869. list.concat(Taicpu.op_none(A_REP,S_NO));
  2870. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2871. end
  2872. else
  2873. begin
  2874. helpsize:=len div sizeof(aint);
  2875. len:=len mod sizeof(aint);
  2876. if helpsize>1 then
  2877. begin
  2878. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  2879. list.concat(Taicpu.op_none(A_REP,S_NO));
  2880. end;
  2881. if helpsize>0 then
  2882. begin
  2883. {$if defined(cpu64bitalu)}
  2884. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  2885. {$elseif defined(cpu32bitalu)}
  2886. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2887. {$elseif defined(cpu16bitalu)}
  2888. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2889. {$endif}
  2890. end;
  2891. if len>=4 then
  2892. begin
  2893. dec(len,4);
  2894. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2895. end;
  2896. if len>=2 then
  2897. begin
  2898. dec(len,2);
  2899. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2900. end;
  2901. if len=1 then
  2902. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2903. end;
  2904. ungetcpuregister(list,REGCX);
  2905. ungetcpuregister(list,REGSI);
  2906. ungetcpuregister(list,REGDI);
  2907. if saved_ds then
  2908. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2909. if saved_es then
  2910. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2911. end;
  2912. end;
  2913. end;
  2914. {****************************************************************************
  2915. Entry/Exit Code Helpers
  2916. ****************************************************************************}
  2917. procedure tcgx86.g_profilecode(list : TAsmList);
  2918. var
  2919. pl : tasmlabel;
  2920. mcountprefix : String[4];
  2921. begin
  2922. case target_info.system of
  2923. {$ifndef NOTARGETWIN}
  2924. system_i386_win32,
  2925. {$endif}
  2926. system_i386_freebsd,
  2927. system_i386_netbsd,
  2928. system_i386_wdosx :
  2929. begin
  2930. Case target_info.system Of
  2931. system_i386_freebsd : mcountprefix:='.';
  2932. system_i386_netbsd : mcountprefix:='__';
  2933. else
  2934. mcountPrefix:='';
  2935. end;
  2936. current_asmdata.getaddrlabel(pl);
  2937. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(pint));
  2938. list.concat(Tai_label.Create(pl));
  2939. list.concat(Tai_const.Create_32bit(0));
  2940. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  2941. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2942. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  2943. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount',false);
  2944. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  2945. end;
  2946. system_i386_linux:
  2947. a_call_name(list,target_info.Cprefix+'mcount',false);
  2948. system_i386_go32v2,system_i386_watcom:
  2949. begin
  2950. a_call_name(list,'MCOUNT',false);
  2951. end;
  2952. system_x86_64_linux,
  2953. system_x86_64_darwin,
  2954. system_x86_64_iphonesim:
  2955. begin
  2956. a_call_name(list,'mcount',false);
  2957. end;
  2958. system_i386_openbsd,
  2959. system_x86_64_openbsd:
  2960. begin
  2961. a_call_name(list,'__mcount',false);
  2962. end;
  2963. else
  2964. internalerror(2019050701);
  2965. end;
  2966. end;
  2967. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2968. procedure decrease_sp(a : tcgint);
  2969. var
  2970. href : treference;
  2971. begin
  2972. {$ifdef x86_64}
  2973. if localsize=8 then
  2974. list.concat(Taicpu.op_reg(A_PUSH,TCGSize2OpSize[OS_ADDR],NR_RAX))
  2975. else
  2976. {$endif x86_64}
  2977. begin
  2978. reference_reset_base(href,NR_STACK_POINTER_REG,-a,ctempposinvalid,0,[]);
  2979. { normally, lea is a better choice than a sub to adjust the stack pointer }
  2980. list.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_STACK_POINTER_REG));
  2981. end;
  2982. end;
  2983. {$ifdef x86}
  2984. {$ifndef NOTARGETWIN}
  2985. var
  2986. href : treference;
  2987. i : integer;
  2988. again : tasmlabel;
  2989. {$endif NOTARGETWIN}
  2990. {$endif x86}
  2991. begin
  2992. if localsize>0 then
  2993. begin
  2994. {$ifdef i386}
  2995. {$ifndef NOTARGETWIN}
  2996. { windows guards only a few pages for stack growing,
  2997. so we have to access every page first }
  2998. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  2999. (localsize>=winstackpagesize) then
  3000. begin
  3001. if localsize div winstackpagesize<=5 then
  3002. begin
  3003. decrease_sp(localsize-4);
  3004. for i:=1 to localsize div winstackpagesize do
  3005. begin
  3006. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize,ctempposinvalid,4,[]);
  3007. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3008. end;
  3009. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  3010. end
  3011. else
  3012. begin
  3013. current_asmdata.getjumplabel(again);
  3014. { Using a_reg_alloc instead of getcpuregister, so this procedure
  3015. does not change "used_in_proc" state of EDI and therefore can be
  3016. called after saving registers with "push" instruction
  3017. without creating an unbalanced "pop edi" in epilogue }
  3018. a_reg_alloc(list,NR_EDI);
  3019. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  3020. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  3021. a_label(list,again);
  3022. decrease_sp(winstackpagesize-4);
  3023. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  3024. if UseIncDec then
  3025. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI))
  3026. else
  3027. list.concat(Taicpu.op_const_reg(A_SUB,S_L,1,NR_EDI));
  3028. a_jmp_cond(list,OC_NE,again);
  3029. decrease_sp(localsize mod winstackpagesize-4);
  3030. reference_reset_base(href,NR_ESP,localsize-4,ctempposinvalid,4,[]);
  3031. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  3032. a_reg_dealloc(list,NR_EDI);
  3033. end
  3034. end
  3035. else
  3036. {$endif NOTARGETWIN}
  3037. {$endif i386}
  3038. {$ifdef x86_64}
  3039. {$ifndef NOTARGETWIN}
  3040. { windows guards only a few pages for stack growing,
  3041. so we have to access every page first }
  3042. if (target_info.system=system_x86_64_win64) and
  3043. (localsize>=winstackpagesize) then
  3044. begin
  3045. if localsize div winstackpagesize<=5 then
  3046. begin
  3047. decrease_sp(localsize);
  3048. for i:=1 to localsize div winstackpagesize do
  3049. begin
  3050. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4,ctempposinvalid,4,[]);
  3051. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3052. end;
  3053. reference_reset_base(href,NR_RSP,0,ctempposinvalid,4,[]);
  3054. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3055. end
  3056. else
  3057. begin
  3058. current_asmdata.getjumplabel(again);
  3059. getcpuregister(list,NR_R10);
  3060. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  3061. a_label(list,again);
  3062. decrease_sp(winstackpagesize);
  3063. reference_reset_base(href,NR_RSP,0,ctempposinvalid,4,[]);
  3064. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  3065. if UseIncDec then
  3066. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10))
  3067. else
  3068. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,1,NR_R10));
  3069. a_jmp_cond(list,OC_NE,again);
  3070. decrease_sp(localsize mod winstackpagesize);
  3071. ungetcpuregister(list,NR_R10);
  3072. end
  3073. end
  3074. else
  3075. {$endif NOTARGETWIN}
  3076. {$endif x86_64}
  3077. decrease_sp(localsize);
  3078. end;
  3079. end;
  3080. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  3081. var
  3082. stackmisalignment: longint;
  3083. regsize: longint;
  3084. {$ifdef i8086}
  3085. dgroup: treference;
  3086. fardataseg: treference;
  3087. {$endif i8086}
  3088. procedure push_regs;
  3089. var
  3090. r: longint;
  3091. usedregs: tcpuregisterset;
  3092. regs_to_save_int: tcpuregisterarray;
  3093. hreg: TRegister;
  3094. begin
  3095. regsize:=0;
  3096. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  3097. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  3098. for r := low(regs_to_save_int) to high(regs_to_save_int) do
  3099. if regs_to_save_int[r] in usedregs then
  3100. begin
  3101. inc(regsize,sizeof(aint));
  3102. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  3103. list.concat(Taicpu.Op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],hreg));
  3104. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3105. current_asmdata.asmcfi.cfa_offset(list,hreg,-(regsize+sizeof(pint)*2+localsize))
  3106. else
  3107. begin
  3108. current_asmdata.asmcfi.cfa_offset(list,hreg,-(regsize+sizeof(pint)+localsize));
  3109. current_asmdata.asmcfi.cfa_def_cfa_offset(list,regsize+localsize+sizeof(pint));
  3110. end;
  3111. end;
  3112. end;
  3113. begin
  3114. regsize:=0;
  3115. stackmisalignment:=0;
  3116. {$ifdef i8086}
  3117. { Win16 callback/exported proc prologue support.
  3118. Since callbacks can be called from different modules, DS on entry may be
  3119. initialized with the data segment of a different module, so we need to
  3120. get ours. But we can't do
  3121. push ds
  3122. mov ax, dgroup
  3123. mov ds, ax
  3124. because code segments are shared between different instances of the same
  3125. module (which have different instances of the current program's data segment),
  3126. so the same 'mov ax, dgroup' instruction will be used for all instances
  3127. of the program and it will load the same segment into ax.
  3128. So, the standard win16 prologue looks like this:
  3129. mov ax, ds
  3130. nop
  3131. inc bp
  3132. push bp
  3133. mov bp, sp
  3134. push ds
  3135. mov ds, ax
  3136. By default, this does nothing, except wasting a few extra machine cycles and
  3137. destroying ax in the process. However, Windows checks the first three bytes
  3138. of every exported function and if they are 'mov ax,ds/nop', they are replaced
  3139. with nop/nop/nop. Then the MakeProcInstance api call should be used to create
  3140. a thunk that loads ds for the current program instance in ax before calling
  3141. the routine.
  3142. And now the fun part comes: somebody (Michael Geary) figured out that all this
  3143. crap was unnecessary, because in Win16 exe modules, we always have DS=SS, so we
  3144. can simply initialize DS from SS :) And then calling MakeProcInstance becomes
  3145. unnecessary. This is what "smart callbacks" (cs_win16_smartcallbacks) do. However,
  3146. this only works for exe files, not for dlls, because dlls run with DS<>SS. There's
  3147. another solution for dlls - since win16 dlls only have a single instance of their
  3148. data segment, we can initialize ds from dgroup. However, there's not a single
  3149. solution for both exe and dlls, so we don't know what to use e.g. in a unit. So,
  3150. that's why there's still an option to turn smart callbacks off and go the
  3151. MakeProcInstance way.
  3152. Additional details here: http://www.geary.com/fixds.html }
  3153. if (current_settings.x86memorymodel<>mm_huge) and
  3154. (po_exports in current_procinfo.procdef.procoptions) and
  3155. (target_info.system=system_i8086_win16) then
  3156. begin
  3157. if cs_win16_smartcallbacks in current_settings.moduleswitches then
  3158. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_SS,NR_AX))
  3159. else
  3160. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_DS,NR_AX));
  3161. list.concat(Taicpu.op_none(A_NOP));
  3162. end
  3163. { interrupt support for i8086 }
  3164. else if po_interrupt in current_procinfo.procdef.procoptions then
  3165. begin
  3166. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_AX));
  3167. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_BX));
  3168. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CX));
  3169. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DX));
  3170. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  3171. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  3172. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  3173. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  3174. if current_settings.x86memorymodel=mm_tiny then
  3175. begin
  3176. { in the tiny memory model, we can't use dgroup, because that
  3177. adds a relocation entry to the .exe and we can't produce a
  3178. .com file (because they don't support relactions), so instead
  3179. we initialize DS from CS. }
  3180. if cs_opt_size in current_settings.optimizerswitches then
  3181. begin
  3182. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CS));
  3183. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  3184. end
  3185. else
  3186. begin
  3187. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_CS,NR_AX));
  3188. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3189. end;
  3190. end
  3191. else if current_settings.x86memorymodel=mm_huge then
  3192. begin
  3193. reference_reset(fardataseg,0,[]);
  3194. fardataseg.refaddr:=addr_fardataseg;
  3195. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  3196. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3197. end
  3198. else
  3199. begin
  3200. reference_reset(dgroup,0,[]);
  3201. dgroup.refaddr:=addr_dgroup;
  3202. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,dgroup,NR_AX));
  3203. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3204. end;
  3205. end;
  3206. {$endif i8086}
  3207. {$ifdef i386}
  3208. { interrupt support for i386 }
  3209. if (po_interrupt in current_procinfo.procdef.procoptions) then
  3210. begin
  3211. { .... also the segment registers }
  3212. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  3213. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  3214. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  3215. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  3216. { save the registers of an interrupt procedure }
  3217. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  3218. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  3219. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  3220. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  3221. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  3222. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  3223. { pushf, push %cs, 4*selector registers, 6*general purpose registers }
  3224. inc(stackmisalignment,4+4+4*2+6*4);
  3225. end;
  3226. {$endif i386}
  3227. { save old framepointer }
  3228. if not nostackframe then
  3229. begin
  3230. { return address }
  3231. inc(stackmisalignment,sizeof(pint));
  3232. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  3233. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3234. begin
  3235. {$ifdef i386}
  3236. if (not paramanager.use_fixed_stack) then
  3237. push_regs;
  3238. {$endif i386}
  3239. CGmessage(cg_d_stackframe_omited);
  3240. end
  3241. else
  3242. begin
  3243. {$ifdef i8086}
  3244. if ((ts_x86_far_procs_push_odd_bp in current_settings.targetswitches) or
  3245. ((po_exports in current_procinfo.procdef.procoptions) and
  3246. (target_info.system=system_i8086_win16))) and
  3247. is_proc_far(current_procinfo.procdef) then
  3248. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,1,current_procinfo.framepointer);
  3249. {$endif i8086}
  3250. { push <frame_pointer> }
  3251. inc(stackmisalignment,sizeof(pint));
  3252. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  3253. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  3254. { Return address and FP are both on stack }
  3255. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(pint));
  3256. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(pint)));
  3257. if current_procinfo.procdef.proctypeoption<>potype_exceptfilter then
  3258. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG))
  3259. else
  3260. begin
  3261. push_regs;
  3262. gen_load_frame_for_exceptfilter(list);
  3263. { Need only as much stack space as necessary to do the calls.
  3264. Exception filters don't have own local vars, and temps are 'mapped'
  3265. to the parent procedure.
  3266. maxpushedparasize is already aligned at least on x86_64. }
  3267. localsize:=current_procinfo.maxpushedparasize;
  3268. end;
  3269. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  3270. end;
  3271. { allocate stackframe space }
  3272. if (localsize<>0) or
  3273. ((target_info.stackalign>sizeof(pint)) and
  3274. (stackmisalignment <> 0) and
  3275. ((pi_do_call in current_procinfo.flags) or
  3276. (po_assembler in current_procinfo.procdef.procoptions))) then
  3277. begin
  3278. if target_info.stackalign>sizeof(pint) then
  3279. localsize := align(localsize+stackmisalignment,target_info.stackalign)-stackmisalignment;
  3280. g_stackpointer_alloc(list,localsize);
  3281. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3282. current_asmdata.asmcfi.cfa_def_cfa_offset(list,regsize+localsize+sizeof(pint));
  3283. current_procinfo.final_localsize:=localsize;
  3284. end
  3285. {$ifdef i8086}
  3286. else
  3287. { on i8086 we always call g_stackpointer_alloc, even with a zero size,
  3288. because it will generate code for stack checking, if stack checking is on }
  3289. g_stackpointer_alloc(list,0)
  3290. {$endif i8086}
  3291. ;
  3292. {$ifdef i8086}
  3293. { win16 exported proc prologue follow-up (see the huge comment above for details) }
  3294. if (current_settings.x86memorymodel<>mm_huge) and
  3295. (po_exports in current_procinfo.procdef.procoptions) and
  3296. (target_info.system=system_i8086_win16) then
  3297. begin
  3298. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  3299. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3300. end
  3301. else if (current_settings.x86memorymodel=mm_huge) and
  3302. not (po_interrupt in current_procinfo.procdef.procoptions) then
  3303. begin
  3304. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  3305. reference_reset(fardataseg,0,[]);
  3306. fardataseg.refaddr:=addr_fardataseg;
  3307. if current_procinfo.procdef.proccalloption=pocall_register then
  3308. begin
  3309. { Use CX register if using register convention
  3310. as it is not a register used to store parameters }
  3311. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_CX));
  3312. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_CX,NR_DS));
  3313. end
  3314. else
  3315. begin
  3316. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  3317. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3318. end;
  3319. end;
  3320. { SI and DI are volatile in the BP7 and FPC's pascal calling convention,
  3321. but must be preserved in Microsoft C's pascal calling convention, and
  3322. since Windows is compiled with Microsoft compilers, these registers
  3323. must be saved for exported procedures (BP7 for Win16 also does this). }
  3324. if (po_exports in current_procinfo.procdef.procoptions) and
  3325. (target_info.system=system_i8086_win16) then
  3326. begin
  3327. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  3328. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  3329. end;
  3330. {$endif i8086}
  3331. {$ifdef i386}
  3332. if (not paramanager.use_fixed_stack) and
  3333. (current_procinfo.framepointer<>NR_STACK_POINTER_REG) and
  3334. (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  3335. begin
  3336. regsize:=0;
  3337. push_regs;
  3338. reference_reset_base(current_procinfo.save_regs_ref,
  3339. current_procinfo.framepointer,
  3340. -(localsize+regsize),ctempposinvalid,sizeof(aint),[]);
  3341. end;
  3342. {$endif i386}
  3343. end;
  3344. end;
  3345. procedure tcgx86.g_save_registers(list: TAsmList);
  3346. begin
  3347. {$ifdef i386}
  3348. if paramanager.use_fixed_stack then
  3349. {$endif i386}
  3350. inherited g_save_registers(list);
  3351. end;
  3352. procedure tcgx86.g_restore_registers(list: TAsmList);
  3353. begin
  3354. {$ifdef i386}
  3355. if paramanager.use_fixed_stack then
  3356. {$endif i386}
  3357. inherited g_restore_registers(list);
  3358. end;
  3359. procedure tcgx86.internal_restore_regs(list: TAsmList; use_pop: boolean);
  3360. var
  3361. r: longint;
  3362. hreg: tregister;
  3363. href: treference;
  3364. usedregs: tcpuregisterset;
  3365. regs_to_save_int: tcpuregisterarray;
  3366. begin
  3367. href:=current_procinfo.save_regs_ref;
  3368. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  3369. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  3370. for r:=high(regs_to_save_int) downto low(regs_to_save_int) do
  3371. if regs_to_save_int[r] in usedregs then
  3372. begin
  3373. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  3374. { Allocate register so the optimizer does not remove the load }
  3375. a_reg_alloc(list,hreg);
  3376. if use_pop then
  3377. list.concat(Taicpu.Op_reg(A_POP,tcgsize2opsize[OS_ADDR],hreg))
  3378. else
  3379. begin
  3380. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  3381. inc(href.offset,sizeof(aint));
  3382. end;
  3383. current_asmdata.asmcfi.cfa_restore(list,hreg);
  3384. end;
  3385. end;
  3386. procedure tcgx86.generate_leave(list: TAsmList);
  3387. begin
  3388. if UseLeave then
  3389. list.concat(taicpu.op_none(A_LEAVE,S_NO))
  3390. else
  3391. begin
  3392. {$if defined(x86_64)}
  3393. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_RSP);
  3394. list.Concat(taicpu.op_reg_reg(A_MOV,S_Q,NR_RBP,NR_RSP));
  3395. current_asmdata.asmcfi.cfa_restore(list,NR_RBP);
  3396. current_asmdata.asmcfi.cfa_def_cfa_offset(list,8);
  3397. list.Concat(taicpu.op_reg(A_POP,S_Q,NR_RBP));
  3398. {$elseif defined(i386)}
  3399. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_ESP);
  3400. list.Concat(taicpu.op_reg_reg(A_MOV,S_L,NR_EBP,NR_ESP));
  3401. current_asmdata.asmcfi.cfa_restore(list,NR_EBP);
  3402. current_asmdata.asmcfi.cfa_def_cfa_offset(list,4);
  3403. list.Concat(taicpu.op_reg(A_POP,S_L,NR_EBP));
  3404. {$elseif defined(i8086)}
  3405. list.Concat(taicpu.op_reg_reg(A_MOV,S_W,NR_BP,NR_SP));
  3406. list.Concat(taicpu.op_reg(A_POP,S_W,NR_BP));
  3407. {$endif}
  3408. end;
  3409. end;
  3410. { produces if necessary overflowcode }
  3411. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  3412. var
  3413. hl : tasmlabel;
  3414. ai : taicpu;
  3415. cond : TAsmCond;
  3416. begin
  3417. if not(cs_check_overflow in current_settings.localswitches) then
  3418. exit;
  3419. current_asmdata.getjumplabel(hl);
  3420. if not ((def.typ=pointerdef) or
  3421. ((def.typ=orddef) and
  3422. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  3423. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  3424. cond:=C_NO
  3425. else
  3426. cond:=C_NB;
  3427. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  3428. ai.SetCondition(cond);
  3429. ai.is_jmp:=true;
  3430. list.concat(ai);
  3431. a_call_name(list,'FPC_OVERFLOW',false);
  3432. a_label(list,hl);
  3433. end;
  3434. end.