cgx86.pas 135 KB

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