cgx86.pas 136 KB

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