cgx86.pas 138 KB

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