cgx86.pas 140 KB

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