cgx86.pas 136 KB

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