cgx86.pas 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608
  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) : 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. result:=A_VMOVAPS
  1238. else
  1239. result:=A_MOVAPS;
  1240. OS_M256,
  1241. OS_M512:
  1242. { 256-bit aligned vector }
  1243. if UseAVX then
  1244. result:=A_VMOVAPS
  1245. else
  1246. { SSE does not support 256-bit or 512-bit vectors }
  1247. InternalError(2018012930);
  1248. else
  1249. InternalError(2018012920);
  1250. end;
  1251. end
  1252. else
  1253. internalerror(2010060104);
  1254. if result=A_NONE then
  1255. internalerror(200312205);
  1256. end;
  1257. procedure tcgx86.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  1258. var
  1259. instr : taicpu;
  1260. op : TAsmOp;
  1261. begin
  1262. if shuffle=nil then
  1263. begin
  1264. if fromsize=tosize then
  1265. { needs correct size in case of spilling }
  1266. case fromsize of
  1267. OS_F32:
  1268. if UseAVX then
  1269. instr:=taicpu.op_reg_reg(A_VMOVAPS,S_NO,reg1,reg2)
  1270. else
  1271. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2);
  1272. OS_F64:
  1273. if UseAVX then
  1274. instr:=taicpu.op_reg_reg(A_VMOVAPD,S_NO,reg1,reg2)
  1275. else
  1276. instr:=taicpu.op_reg_reg(A_MOVAPD,S_NO,reg1,reg2);
  1277. OS_M64:
  1278. if UseAVX then
  1279. instr:=taicpu.op_reg_reg(A_VMOVQ,S_NO,reg1,reg2)
  1280. else
  1281. instr:=taicpu.op_reg_reg(A_MOVQ,S_NO,reg1,reg2);
  1282. OS_M128:
  1283. if UseAVX then
  1284. instr:=taicpu.op_reg_reg(A_VMOVDQA,S_NO,reg1,reg2)
  1285. else
  1286. instr:=taicpu.op_reg_reg(A_MOVDQA,S_NO,reg1,reg2);
  1287. OS_M256,
  1288. OS_M512:
  1289. if UseAVX then
  1290. instr:=taicpu.op_reg_reg(A_VMOVDQA,S_NO,reg1,reg2)
  1291. else
  1292. { SSE doesn't support 512-bit vectors }
  1293. InternalError(2018012933);
  1294. else
  1295. internalerror(2006091201);
  1296. end
  1297. else
  1298. internalerror(200312202);
  1299. add_move_instruction(instr);
  1300. end
  1301. else if shufflescalar(shuffle) then
  1302. begin
  1303. op:=get_scalar_mm_op(fromsize,tosize);
  1304. { MOVAPD/MOVAPS are normally faster }
  1305. if op=A_MOVSD then
  1306. op:=A_MOVAPD
  1307. else if op=A_MOVSS then
  1308. op:=A_MOVAPS
  1309. { VMOVSD/SS is not available with two register operands }
  1310. else if op=A_VMOVSD then
  1311. op:=A_VMOVAPD
  1312. else if op=A_VMOVSS then
  1313. op:=A_VMOVAPS;
  1314. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1315. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1316. instr:=taicpu.op_reg_reg_reg(op,S_NO,reg1,reg2,reg2)
  1317. else
  1318. instr:=taicpu.op_reg_reg(op,S_NO,reg1,reg2);
  1319. case op of
  1320. A_VMOVAPD,
  1321. A_VMOVAPS,
  1322. A_VMOVSS,
  1323. A_VMOVSD,
  1324. A_VMOVQ,
  1325. A_MOVAPD,
  1326. A_MOVAPS,
  1327. A_MOVSS,
  1328. A_MOVSD,
  1329. A_MOVQ:
  1330. add_move_instruction(instr);
  1331. else
  1332. ;
  1333. end;
  1334. end
  1335. else
  1336. internalerror(200312201);
  1337. list.concat(instr);
  1338. end;
  1339. procedure tcgx86.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1340. var
  1341. tmpref : treference;
  1342. op : tasmop;
  1343. begin
  1344. tmpref:=ref;
  1345. make_simple_ref(list,tmpref);
  1346. if shuffle=nil then
  1347. begin
  1348. case fromsize of
  1349. OS_F32:
  1350. if UseAVX then
  1351. op := A_VMOVSS
  1352. else
  1353. op := A_MOVSS;
  1354. OS_F64:
  1355. if UseAVX then
  1356. op := A_VMOVSD
  1357. else
  1358. op := A_MOVSD;
  1359. OS_M32, OS_32, OS_S32:
  1360. if UseAVX then
  1361. op := A_VMOVD
  1362. else
  1363. op := A_MOVD;
  1364. OS_M64, OS_64, OS_S64:
  1365. { there is no VMOVQ for MMX registers }
  1366. if UseAVX and (getregtype(reg)<>R_MMXREGISTER) then
  1367. op := A_VMOVQ
  1368. else
  1369. op := A_MOVQ;
  1370. OS_M128:
  1371. { Use XMM integer transfer }
  1372. if UseAVX then
  1373. begin
  1374. if GetRefAlignment(tmpref) = 16 then
  1375. op := A_VMOVDQA
  1376. else
  1377. op := A_VMOVDQU
  1378. end
  1379. else
  1380. begin
  1381. if GetRefAlignment(tmpref) = 16 then
  1382. op := A_MOVDQA
  1383. else
  1384. op := A_MOVDQU;
  1385. end;
  1386. OS_M256:
  1387. { Use YMM integer transfer }
  1388. if UseAVX then
  1389. begin
  1390. if GetRefAlignment(tmpref) = 32 then
  1391. op := A_VMOVDQA
  1392. else
  1393. op := A_VMOVDQU
  1394. end
  1395. else
  1396. { SSE doesn't support 256-bit vectors }
  1397. Internalerror(2020010401);
  1398. OS_M512:
  1399. { Use ZMM integer transfer }
  1400. if UseAVX then
  1401. begin
  1402. if GetRefAlignment(tmpref) = 64 then
  1403. op := A_VMOVDQA
  1404. else
  1405. op := A_VMOVDQU
  1406. end
  1407. else
  1408. { SSE doesn't support 512-bit vectors }
  1409. InternalError(2018012939);
  1410. else
  1411. { No valid transfer command available }
  1412. internalerror(2017121410);
  1413. end;
  1414. list.concat(taicpu.op_ref_reg(op,S_NO,tmpref,reg));
  1415. end
  1416. else if shufflescalar(shuffle) then
  1417. begin
  1418. op:=get_scalar_mm_op(fromsize,tosize);
  1419. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1420. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1421. list.concat(taicpu.op_ref_reg_reg(op,S_NO,tmpref,reg,reg))
  1422. else
  1423. list.concat(taicpu.op_ref_reg(op,S_NO,tmpref,reg))
  1424. end
  1425. else
  1426. internalerror(200312252);
  1427. end;
  1428. procedure tcgx86.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  1429. var
  1430. hreg : tregister;
  1431. tmpref : treference;
  1432. op : tasmop;
  1433. begin
  1434. tmpref:=ref;
  1435. make_simple_ref(list,tmpref);
  1436. if shuffle=nil then
  1437. begin
  1438. case fromsize of
  1439. OS_F32:
  1440. if UseAVX then
  1441. op := A_VMOVSS
  1442. else
  1443. op := A_MOVSS;
  1444. OS_F64:
  1445. if UseAVX then
  1446. op := A_VMOVSD
  1447. else
  1448. op := A_MOVSD;
  1449. OS_M32, OS_32, OS_S32:
  1450. if UseAVX then
  1451. op := A_VMOVD
  1452. else
  1453. op := A_MOVD;
  1454. OS_M64, OS_64, OS_S64:
  1455. { there is no VMOVQ for MMX registers }
  1456. if UseAVX and (getregtype(reg)<>R_MMXREGISTER) then
  1457. op := A_VMOVQ
  1458. else
  1459. op := A_MOVQ;
  1460. OS_M128:
  1461. { Use XMM integer transfer }
  1462. if UseAVX then
  1463. begin
  1464. if GetRefAlignment(tmpref) = 16 then
  1465. op := A_VMOVDQA
  1466. else
  1467. op := A_VMOVDQU
  1468. end else
  1469. begin
  1470. if GetRefAlignment(tmpref) = 16 then
  1471. op := A_MOVDQA
  1472. else
  1473. op := A_MOVDQU
  1474. end;
  1475. OS_M256:
  1476. { Use XMM integer transfer }
  1477. if UseAVX then
  1478. begin
  1479. if GetRefAlignment(tmpref) = 32 then
  1480. op := A_VMOVDQA
  1481. else
  1482. op := A_VMOVDQU
  1483. end else
  1484. { SSE doesn't support 256-bit vectors }
  1485. InternalError(2018012942);
  1486. OS_M512:
  1487. { Use XMM integer transfer }
  1488. if UseAVX then
  1489. begin
  1490. if GetRefAlignment(tmpref) = 64 then
  1491. op := A_VMOVDQA
  1492. else
  1493. op := A_VMOVDQU
  1494. end else
  1495. { SSE doesn't support 512-bit vectors }
  1496. InternalError(2018012945);
  1497. else
  1498. { No valid transfer command available }
  1499. internalerror(2017121411);
  1500. end;
  1501. list.concat(taicpu.op_reg_ref(op,S_NO,reg,tmpref));
  1502. end
  1503. else if shufflescalar(shuffle) then
  1504. begin
  1505. if tcgsize2size[tosize]<>tcgsize2size[fromsize] then
  1506. begin
  1507. hreg:=getmmregister(list,tosize);
  1508. op:=get_scalar_mm_op(fromsize,tosize);
  1509. { A_VCVTSD2SS and A_VCVTSS2SD require always three operands }
  1510. if (op=A_VCVTSD2SS) or (op=A_VCVTSS2SD) then
  1511. list.concat(taicpu.op_reg_reg_reg(op,S_NO,reg,hreg,hreg))
  1512. else
  1513. list.concat(taicpu.op_reg_reg(op,S_NO,reg,hreg));
  1514. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize),S_NO,hreg,tmpref))
  1515. end
  1516. else
  1517. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize),S_NO,reg,tmpref));
  1518. end
  1519. else
  1520. internalerror(200312252);
  1521. end;
  1522. procedure tcgx86.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  1523. var
  1524. l : tlocation;
  1525. begin
  1526. l.loc:=LOC_REFERENCE;
  1527. l.reference:=ref;
  1528. l.size:=size;
  1529. opmm_loc_reg(list,op,size,l,reg,shuffle);
  1530. end;
  1531. procedure tcgx86.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  1532. var
  1533. l : tlocation;
  1534. begin
  1535. l.loc:=LOC_MMREGISTER;
  1536. l.register:=src;
  1537. l.size:=size;
  1538. opmm_loc_reg(list,op,size,l,dst,shuffle);
  1539. end;
  1540. procedure tcgx86.opmm_loc_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;src,dst: tregister; shuffle : pmmshuffle);
  1541. const
  1542. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1543. ( { scalar }
  1544. ( { OS_F32 }
  1545. 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
  1546. ),
  1547. ( { OS_F64 }
  1548. 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
  1549. )
  1550. ),
  1551. ( { vectorized/packed }
  1552. { because the logical packed single instructions have shorter op codes, we use always
  1553. these
  1554. }
  1555. ( { OS_F32 }
  1556. 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
  1557. ),
  1558. ( { OS_F64 }
  1559. 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
  1560. )
  1561. )
  1562. );
  1563. var
  1564. resultreg : tregister;
  1565. asmop : tasmop;
  1566. begin
  1567. { this is an internally used procedure so the parameters have
  1568. some constrains
  1569. }
  1570. if loc.size<>size then
  1571. internalerror(2013061108);
  1572. resultreg:=dst;
  1573. { deshuffle }
  1574. //!!!
  1575. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1576. begin
  1577. internalerror(2013061107);
  1578. end
  1579. else if (shuffle=nil) then
  1580. asmop:=opmm2asmop[1,size,op]
  1581. else if shufflescalar(shuffle) then
  1582. begin
  1583. asmop:=opmm2asmop[0,size,op];
  1584. { no scalar operation available? }
  1585. if asmop=A_NOP then
  1586. begin
  1587. { do vectorized and shuffle finally }
  1588. internalerror(2010060102);
  1589. end;
  1590. end
  1591. else
  1592. internalerror(2013061106);
  1593. if asmop=A_NOP then
  1594. internalerror(2013061105);
  1595. case loc.loc of
  1596. LOC_CREFERENCE,LOC_REFERENCE:
  1597. begin
  1598. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1599. list.concat(taicpu.op_ref_reg_reg(asmop,S_NO,loc.reference,src,resultreg));
  1600. end;
  1601. LOC_CMMREGISTER,LOC_MMREGISTER:
  1602. list.concat(taicpu.op_reg_reg_reg(asmop,S_NO,loc.register,src,resultreg));
  1603. else
  1604. internalerror(2013061104);
  1605. end;
  1606. { shuffle }
  1607. if resultreg<>dst then
  1608. begin
  1609. internalerror(2013061103);
  1610. end;
  1611. end;
  1612. procedure tcgx86.a_opmm_reg_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src1,src2,dst: tregister;shuffle : pmmshuffle);
  1613. var
  1614. l : tlocation;
  1615. begin
  1616. l.loc:=LOC_MMREGISTER;
  1617. l.register:=src1;
  1618. l.size:=size;
  1619. opmm_loc_reg_reg(list,op,size,l,src2,dst,shuffle);
  1620. end;
  1621. procedure tcgx86.a_opmm_ref_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; src,dst: tregister;shuffle : pmmshuffle);
  1622. var
  1623. l : tlocation;
  1624. begin
  1625. l.loc:=LOC_REFERENCE;
  1626. l.reference:=ref;
  1627. l.size:=size;
  1628. opmm_loc_reg_reg(list,op,size,l,src,dst,shuffle);
  1629. end;
  1630. procedure tcgx86.opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  1631. const
  1632. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  1633. ( { scalar }
  1634. ( { OS_F32 }
  1635. 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
  1636. ),
  1637. ( { OS_F64 }
  1638. 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
  1639. )
  1640. ),
  1641. ( { vectorized/packed }
  1642. { because the logical packed single instructions have shorter op codes, we use always
  1643. these
  1644. }
  1645. ( { OS_F32 }
  1646. 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
  1647. ),
  1648. ( { OS_F64 }
  1649. 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
  1650. )
  1651. )
  1652. );
  1653. var
  1654. resultreg : tregister;
  1655. asmop : tasmop;
  1656. begin
  1657. { this is an internally used procedure so the parameters have
  1658. some constrains
  1659. }
  1660. if loc.size<>size then
  1661. internalerror(200312213);
  1662. resultreg:=dst;
  1663. { deshuffle }
  1664. //!!!
  1665. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  1666. begin
  1667. internalerror(2010060101);
  1668. end
  1669. else if (shuffle=nil) then
  1670. asmop:=opmm2asmop[1,size,op]
  1671. else if shufflescalar(shuffle) then
  1672. begin
  1673. asmop:=opmm2asmop[0,size,op];
  1674. { no scalar operation available? }
  1675. if asmop=A_NOP then
  1676. begin
  1677. { do vectorized and shuffle finally }
  1678. internalerror(2010060102);
  1679. end;
  1680. end
  1681. else
  1682. internalerror(200312211);
  1683. if asmop=A_NOP then
  1684. internalerror(200312216);
  1685. case loc.loc of
  1686. LOC_CREFERENCE,LOC_REFERENCE:
  1687. begin
  1688. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1689. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  1690. end;
  1691. LOC_CMMREGISTER,LOC_MMREGISTER:
  1692. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  1693. else
  1694. internalerror(200312214);
  1695. end;
  1696. { shuffle }
  1697. if resultreg<>dst then
  1698. begin
  1699. internalerror(200312212);
  1700. end;
  1701. end;
  1702. {$ifndef i8086}
  1703. procedure tcgx86.a_op_const_reg_reg(list:TAsmList;op:Topcg;size:Tcgsize;
  1704. a:tcgint;src,dst:Tregister);
  1705. var
  1706. power,al : longint;
  1707. href : treference;
  1708. begin
  1709. power:=0;
  1710. optimize_op_const(size,op,a);
  1711. case op of
  1712. OP_NONE:
  1713. begin
  1714. a_load_reg_reg(list,size,size,src,dst);
  1715. exit;
  1716. end;
  1717. OP_MOVE:
  1718. begin
  1719. a_load_const_reg(list,size,a,dst);
  1720. exit;
  1721. end;
  1722. else
  1723. ;
  1724. end;
  1725. if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1726. not(cs_check_overflow in current_settings.localswitches) and
  1727. (a>1) and ispowerof2(int64(a-1),power) and (power in [1..3]) then
  1728. begin
  1729. reference_reset_base(href,src,0,ctempposinvalid,0,[]);
  1730. href.index:=src;
  1731. href.scalefactor:=a-1;
  1732. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1733. end
  1734. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1735. not(cs_check_overflow in current_settings.localswitches) and
  1736. (a>1) and ispowerof2(int64(a),power) and (power in [1..3]) then
  1737. begin
  1738. reference_reset_base(href,NR_NO,0,ctempposinvalid,0,[]);
  1739. href.index:=src;
  1740. href.scalefactor:=a;
  1741. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1742. end
  1743. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1744. (a>1) and (a<=maxLongint) and not ispowerof2(int64(a),power) then
  1745. begin
  1746. { MUL with overflow checking should be handled specifically in the code generator }
  1747. if (op=OP_MUL) and (cs_check_overflow in current_settings.localswitches) then
  1748. internalerror(2014011801);
  1749. list.concat(taicpu.op_const_reg_reg(A_IMUL,TCgSize2OpSize[size],a,src,dst));
  1750. end
  1751. else if (op=OP_ADD) and
  1752. ((size in [OS_32,OS_S32]) or
  1753. { lea supports only 32 bit signed displacments }
  1754. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1755. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1756. ) and
  1757. not(cs_check_overflow in current_settings.localswitches) then
  1758. begin
  1759. { a might still be in the range 0x80000000 to 0xffffffff
  1760. which might trigger a range check error as
  1761. reference_reset_base expects a longint value. }
  1762. {$push} {$R-}{$Q-}
  1763. al := longint (a);
  1764. {$pop}
  1765. reference_reset_base(href,src,al,ctempposinvalid,0,[]);
  1766. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1767. end
  1768. else if (op=OP_SHL) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1769. (int64(a)>=1) and (int64(a)<=3) then
  1770. begin
  1771. reference_reset_base(href,NR_NO,0,ctempposinvalid,0,[]);
  1772. href.index:=src;
  1773. href.scalefactor:=1 shl longint(a);
  1774. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1775. end
  1776. else if (op=OP_SUB) and
  1777. ((size in [OS_32,OS_S32]) or
  1778. { lea supports only 32 bit signed displacments }
  1779. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1780. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1781. ) and
  1782. not(cs_check_overflow in current_settings.localswitches) then
  1783. begin
  1784. reference_reset_base(href,src,-a,ctempposinvalid,0,[]);
  1785. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1786. end
  1787. else if (op in [OP_ROR,OP_ROL]) and
  1788. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1789. (size in [OS_32,OS_S32
  1790. {$ifdef x86_64}
  1791. ,OS_64,OS_S64
  1792. {$endif x86_64}
  1793. ]) then
  1794. begin
  1795. if op=OP_ROR then
  1796. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size], a,src,dst))
  1797. else
  1798. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size],TCgSize2Size[size]*8-a,src,dst));
  1799. end
  1800. else
  1801. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1802. end;
  1803. procedure tcgx86.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1804. size: tcgsize; src1, src2, dst: tregister);
  1805. var
  1806. href : treference;
  1807. begin
  1808. if (op=OP_ADD) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1809. not(cs_check_overflow in current_settings.localswitches) then
  1810. begin
  1811. reference_reset_base(href,src1,0,ctempposinvalid,0,[]);
  1812. href.index:=src2;
  1813. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1814. end
  1815. else if (op in [OP_SHR,OP_SHL]) and
  1816. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1817. (size in [OS_32,OS_S32
  1818. {$ifdef x86_64}
  1819. ,OS_64,OS_S64
  1820. {$endif x86_64}
  1821. ]) then
  1822. begin
  1823. if op=OP_SHL then
  1824. list.concat(taicpu.op_reg_reg_reg(A_SHLX,TCgSize2OpSize[size],src1,src2,dst))
  1825. else
  1826. list.concat(taicpu.op_reg_reg_reg(A_SHRX,TCgSize2OpSize[size],src1,src2,dst));
  1827. end
  1828. else
  1829. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1830. end;
  1831. {$endif not i8086}
  1832. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  1833. {$ifdef x86_64}
  1834. var
  1835. tmpreg : tregister;
  1836. {$endif x86_64}
  1837. begin
  1838. optimize_op_const(size, op, a);
  1839. {$ifdef x86_64}
  1840. { x86_64 only supports signed 32 bits constants directly }
  1841. if not(op in [OP_NONE,OP_MOVE]) and
  1842. (size in [OS_S64,OS_64]) and
  1843. ((a<low(longint)) or (a>high(longint))) then
  1844. begin
  1845. tmpreg:=getintregister(list,size);
  1846. a_load_const_reg(list,size,a,tmpreg);
  1847. a_op_reg_reg(list,op,size,tmpreg,reg);
  1848. exit;
  1849. end;
  1850. {$endif x86_64}
  1851. check_register_size(size,reg);
  1852. case op of
  1853. OP_NONE :
  1854. begin
  1855. { Opcode is optimized away }
  1856. end;
  1857. OP_MOVE :
  1858. begin
  1859. { Optimized, replaced with a simple load }
  1860. a_load_const_reg(list,size,a,reg);
  1861. end;
  1862. OP_DIV, OP_IDIV:
  1863. begin
  1864. { should be handled specifically in the code }
  1865. { generator because of the silly register usage restraints }
  1866. internalerror(200109224);
  1867. end;
  1868. OP_MUL,OP_IMUL:
  1869. begin
  1870. if not (cs_check_overflow in current_settings.localswitches) then
  1871. op:=OP_IMUL;
  1872. if op = OP_IMUL then
  1873. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  1874. else
  1875. { OP_MUL should be handled specifically in the code }
  1876. { generator because of the silly register usage restraints }
  1877. internalerror(200109225);
  1878. end;
  1879. OP_ADD, OP_SUB:
  1880. if not(cs_check_overflow in current_settings.localswitches) and
  1881. (a = 1) and
  1882. UseIncDec then
  1883. begin
  1884. if op = OP_ADD then
  1885. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  1886. else
  1887. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  1888. end
  1889. else
  1890. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],ImmInt(a),reg));
  1891. OP_AND,OP_OR:
  1892. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],ImmInt(a),reg));
  1893. OP_XOR:
  1894. if (aword(a)=high(aword)) then
  1895. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg))
  1896. else
  1897. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],ImmInt(a),reg));
  1898. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1899. begin
  1900. {$if defined(x86_64)}
  1901. if (a and 63) <> 0 Then
  1902. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,reg));
  1903. if (a shr 6) <> 0 Then
  1904. internalerror(200609073);
  1905. {$elseif defined(i386)}
  1906. if (a and 31) <> 0 Then
  1907. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1908. if (a shr 5) <> 0 Then
  1909. internalerror(200609071);
  1910. {$elseif defined(i8086)}
  1911. if (a shr 5) <> 0 Then
  1912. internalerror(2013043002);
  1913. a := a and 31;
  1914. if a <> 0 Then
  1915. begin
  1916. if (current_settings.cputype < cpu_186) and (a <> 1) then
  1917. begin
  1918. getcpuregister(list,NR_CL);
  1919. a_load_const_reg(list,OS_8,a,NR_CL);
  1920. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,reg));
  1921. ungetcpuregister(list,NR_CL);
  1922. end
  1923. else
  1924. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  1925. end;
  1926. {$endif}
  1927. end
  1928. else internalerror(200609072);
  1929. end;
  1930. end;
  1931. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1932. var
  1933. {$ifdef x86_64}
  1934. tmpreg : tregister;
  1935. {$endif x86_64}
  1936. tmpref : treference;
  1937. begin
  1938. optimize_op_const(size, op, a);
  1939. if op in [OP_NONE,OP_MOVE] then
  1940. begin
  1941. if (op=OP_MOVE) then
  1942. a_load_const_ref(list,size,a,ref);
  1943. exit;
  1944. end;
  1945. {$ifdef x86_64}
  1946. { x86_64 only supports signed 32 bits constants directly }
  1947. if (size in [OS_S64,OS_64]) and
  1948. ((a<low(longint)) or (a>high(longint))) then
  1949. begin
  1950. tmpreg:=getintregister(list,size);
  1951. a_load_const_reg(list,size,a,tmpreg);
  1952. a_op_reg_ref(list,op,size,tmpreg,ref);
  1953. exit;
  1954. end;
  1955. {$endif x86_64}
  1956. tmpref:=ref;
  1957. make_simple_ref(list,tmpref);
  1958. Case Op of
  1959. OP_DIV, OP_IDIV:
  1960. Begin
  1961. { should be handled specifically in the code }
  1962. { generator because of the silly register usage restraints }
  1963. internalerror(200109231);
  1964. End;
  1965. OP_MUL,OP_IMUL:
  1966. begin
  1967. if not (cs_check_overflow in current_settings.localswitches) then
  1968. op:=OP_IMUL;
  1969. { can't multiply a memory location directly with a constant }
  1970. if op = OP_IMUL then
  1971. inherited a_op_const_ref(list,op,size,a,tmpref)
  1972. else
  1973. { OP_MUL should be handled specifically in the code }
  1974. { generator because of the silly register usage restraints }
  1975. internalerror(200109232);
  1976. end;
  1977. OP_ADD, OP_SUB:
  1978. if not(cs_check_overflow in current_settings.localswitches) and
  1979. (a = 1) and
  1980. UseIncDec then
  1981. begin
  1982. if op = OP_ADD then
  1983. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  1984. else
  1985. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  1986. end
  1987. else
  1988. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1989. OP_AND,OP_OR:
  1990. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1991. OP_XOR:
  1992. if (aword(a)=high(aword)) then
  1993. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref))
  1994. else
  1995. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1996. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1997. begin
  1998. {$if defined(x86_64)}
  1999. if (a and 63) <> 0 Then
  2000. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,tmpref));
  2001. if (a shr 6) <> 0 Then
  2002. internalerror(2013111003);
  2003. {$elseif defined(i386)}
  2004. if (a and 31) <> 0 Then
  2005. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  2006. if (a shr 5) <> 0 Then
  2007. internalerror(2013111002);
  2008. {$elseif defined(i8086)}
  2009. if (a shr 5) <> 0 Then
  2010. internalerror(2013111001);
  2011. a := a and 31;
  2012. if a <> 0 Then
  2013. begin
  2014. if (current_settings.cputype < cpu_186) and (a <> 1) then
  2015. begin
  2016. getcpuregister(list,NR_CL);
  2017. a_load_const_reg(list,OS_8,a,NR_CL);
  2018. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,tmpref));
  2019. ungetcpuregister(list,NR_CL);
  2020. end
  2021. else
  2022. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  2023. end;
  2024. {$endif}
  2025. end
  2026. else internalerror(68992);
  2027. end;
  2028. end;
  2029. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  2030. const
  2031. {$if defined(cpu64bitalu)}
  2032. REGCX=NR_RCX;
  2033. REGCX_Size = OS_64;
  2034. {$elseif defined(cpu32bitalu)}
  2035. REGCX=NR_ECX;
  2036. REGCX_Size = OS_32;
  2037. {$elseif defined(cpu16bitalu)}
  2038. REGCX=NR_CX;
  2039. REGCX_Size = OS_16;
  2040. {$endif}
  2041. var
  2042. dstsize: topsize;
  2043. instr:Taicpu;
  2044. begin
  2045. if not(Op in [OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR]) then
  2046. check_register_size(size,src);
  2047. check_register_size(size,dst);
  2048. dstsize := tcgsize2opsize[size];
  2049. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  2050. op:=OP_IMUL;
  2051. case op of
  2052. OP_NEG,OP_NOT:
  2053. begin
  2054. if src<>dst then
  2055. a_load_reg_reg(list,size,size,src,dst);
  2056. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  2057. end;
  2058. OP_MUL,OP_DIV,OP_IDIV:
  2059. { special stuff, needs separate handling inside code }
  2060. { generator }
  2061. internalerror(200109233);
  2062. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  2063. begin
  2064. { Use ecx to load the value, that allows better coalescing }
  2065. getcpuregister(list,REGCX);
  2066. a_load_reg_reg(list,reg_cgsize(src),REGCX_Size,src,REGCX);
  2067. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  2068. ungetcpuregister(list,REGCX);
  2069. end;
  2070. else
  2071. begin
  2072. if reg2opsize(src) <> dstsize then
  2073. internalerror(200109226);
  2074. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  2075. list.concat(instr);
  2076. end;
  2077. end;
  2078. end;
  2079. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  2080. var
  2081. tmpref : treference;
  2082. begin
  2083. tmpref:=ref;
  2084. make_simple_ref(list,tmpref);
  2085. check_register_size(size,reg);
  2086. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  2087. op:=OP_IMUL;
  2088. case op of
  2089. OP_NEG,OP_NOT,OP_IMUL:
  2090. begin
  2091. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  2092. end;
  2093. OP_MUL,OP_DIV,OP_IDIV:
  2094. { special stuff, needs separate handling inside code }
  2095. { generator }
  2096. internalerror(200109239);
  2097. else
  2098. begin
  2099. reg := makeregsize(list,reg,size);
  2100. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  2101. end;
  2102. end;
  2103. end;
  2104. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  2105. const
  2106. {$if defined(cpu64bitalu)}
  2107. REGCX=NR_RCX;
  2108. REGCX_Size = OS_64;
  2109. {$elseif defined(cpu32bitalu)}
  2110. REGCX=NR_ECX;
  2111. REGCX_Size = OS_32;
  2112. {$elseif defined(cpu16bitalu)}
  2113. REGCX=NR_CX;
  2114. REGCX_Size = OS_16;
  2115. {$endif}
  2116. var
  2117. tmpref : treference;
  2118. begin
  2119. tmpref:=ref;
  2120. make_simple_ref(list,tmpref);
  2121. { we don't check the register size for some operations, for the following reasons:
  2122. NEG,NOT:
  2123. reg isn't used in these operations (they are unary and use only ref)
  2124. SHR,SHL,SAR,ROL,ROR:
  2125. We allow the register size to differ from the destination size.
  2126. This allows generating better code when performing, for example, a
  2127. shift/rotate in place (x:=x shl y) of a byte variable. In this case,
  2128. we allow the shift count (y) to be located in a 32-bit register,
  2129. even though x is a byte. This:
  2130. - reduces register pressure on i386 (because only EAX,EBX,ECX and
  2131. EDX have 8-bit subregisters)
  2132. - avoids partial register writes, which can cause various
  2133. performance issues on modern out-of-order execution x86 CPUs }
  2134. if not (op in [OP_NEG,OP_NOT,OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR]) then
  2135. check_register_size(size,reg);
  2136. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  2137. op:=OP_IMUL;
  2138. case op of
  2139. OP_NEG,OP_NOT:
  2140. begin
  2141. if reg<>NR_NO then
  2142. internalerror(200109237);
  2143. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  2144. end;
  2145. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  2146. begin
  2147. { Use ecx to load the value, that allows better coalescing }
  2148. getcpuregister(list,REGCX);
  2149. a_load_reg_reg(list,reg_cgsize(reg),REGCX_Size,reg,REGCX);
  2150. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],NR_CL,tmpref));
  2151. ungetcpuregister(list,REGCX);
  2152. end;
  2153. OP_IMUL:
  2154. begin
  2155. { this one needs a load/imul/store, which is the default }
  2156. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  2157. end;
  2158. OP_MUL,OP_DIV,OP_IDIV:
  2159. { special stuff, needs separate handling inside code }
  2160. { generator }
  2161. internalerror(200109238);
  2162. else
  2163. begin
  2164. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  2165. end;
  2166. end;
  2167. end;
  2168. procedure tcgx86.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; srcsize, dstsize: TCGSize; src, dst: TRegister);
  2169. var
  2170. tmpreg: tregister;
  2171. opsize: topsize;
  2172. l : TAsmLabel;
  2173. begin
  2174. { no bsf/bsr for byte }
  2175. if srcsize in [OS_8,OS_S8] then
  2176. begin
  2177. tmpreg:=getintregister(list,OS_INT);
  2178. a_load_reg_reg(list,srcsize,OS_INT,src,tmpreg);
  2179. src:=tmpreg;
  2180. srcsize:=OS_INT;
  2181. end;
  2182. { source and destination register must have the same size }
  2183. if tcgsize2size[srcsize]<>tcgsize2size[dstsize] then
  2184. tmpreg:=getintregister(list,srcsize)
  2185. else
  2186. tmpreg:=dst;
  2187. opsize:=tcgsize2opsize[srcsize];
  2188. if not reverse then
  2189. list.concat(taicpu.op_reg_reg(A_BSF,opsize,src,tmpreg))
  2190. else
  2191. list.concat(taicpu.op_reg_reg(A_BSR,opsize,src,tmpreg));
  2192. current_asmdata.getjumplabel(l);
  2193. a_jmp_cond(list,OC_NE,l);
  2194. list.concat(taicpu.op_const_reg(A_MOV,opsize,$ff,tmpreg));
  2195. a_label(list,l);
  2196. if tmpreg<>dst then
  2197. a_load_reg_reg(list,srcsize,dstsize,tmpreg,dst);
  2198. end;
  2199. {*************** compare instructructions ****************}
  2200. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  2201. l : tasmlabel);
  2202. {$ifdef x86_64}
  2203. var
  2204. tmpreg : tregister;
  2205. {$endif x86_64}
  2206. begin
  2207. {$ifdef x86_64}
  2208. { x86_64 only supports signed 32 bits constants directly }
  2209. if (size in [OS_S64,OS_64]) and
  2210. ((a<low(longint)) or (a>high(longint))) then
  2211. begin
  2212. tmpreg:=getintregister(list,size);
  2213. a_load_const_reg(list,size,a,tmpreg);
  2214. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  2215. exit;
  2216. end;
  2217. {$endif x86_64}
  2218. cg.a_reg_alloc(list,NR_DEFAULTFLAGS);
  2219. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  2220. a_jmp_cond(list,cmp_op,l);
  2221. cg.a_reg_dealloc(list,NR_DEFAULTFLAGS);
  2222. end;
  2223. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  2224. l : tasmlabel);
  2225. var
  2226. {$ifdef x86_64}
  2227. tmpreg : tregister;
  2228. {$endif x86_64}
  2229. tmpref : treference;
  2230. begin
  2231. tmpref:=ref;
  2232. make_simple_ref(list,tmpref);
  2233. {$ifdef x86_64}
  2234. { x86_64 only supports signed 32 bits constants directly }
  2235. if (size in [OS_S64,OS_64]) and
  2236. ((a<low(longint)) or (a>high(longint))) then
  2237. begin
  2238. tmpreg:=getintregister(list,size);
  2239. a_load_const_reg(list,size,a,tmpreg);
  2240. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  2241. exit;
  2242. end;
  2243. {$endif x86_64}
  2244. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  2245. a_jmp_cond(list,cmp_op,l);
  2246. end;
  2247. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  2248. reg1,reg2 : tregister;l : tasmlabel);
  2249. begin
  2250. check_register_size(size,reg1);
  2251. check_register_size(size,reg2);
  2252. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  2253. a_jmp_cond(list,cmp_op,l);
  2254. end;
  2255. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  2256. var
  2257. tmpref : treference;
  2258. begin
  2259. tmpref:=ref;
  2260. make_simple_ref(list,tmpref);
  2261. check_register_size(size,reg);
  2262. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  2263. a_jmp_cond(list,cmp_op,l);
  2264. end;
  2265. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  2266. var
  2267. tmpref : treference;
  2268. begin
  2269. tmpref:=ref;
  2270. make_simple_ref(list,tmpref);
  2271. check_register_size(size,reg);
  2272. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  2273. a_jmp_cond(list,cmp_op,l);
  2274. end;
  2275. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  2276. var
  2277. ai : taicpu;
  2278. begin
  2279. if cond=OC_None then
  2280. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  2281. else
  2282. begin
  2283. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  2284. ai.SetCondition(TOpCmp2AsmCond[cond]);
  2285. end;
  2286. ai.is_jmp:=true;
  2287. list.concat(ai);
  2288. end;
  2289. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  2290. var
  2291. ai : taicpu;
  2292. hl : tasmlabel;
  2293. f2 : tresflags;
  2294. begin
  2295. hl:=nil;
  2296. f2:=f;
  2297. case f of
  2298. F_FNE:
  2299. begin
  2300. ai:=Taicpu.op_sym(A_Jcc,S_NO,l);
  2301. ai.SetCondition(C_P);
  2302. ai.is_jmp:=true;
  2303. list.concat(ai);
  2304. f2:=F_NE;
  2305. end;
  2306. F_FE,F_FA,F_FAE,F_FB,F_FBE:
  2307. begin
  2308. { JP before JA/JAE is redundant, but it must be generated here
  2309. and left for peephole optimizer to remove. }
  2310. current_asmdata.getjumplabel(hl);
  2311. ai:=Taicpu.op_sym(A_Jcc,S_NO,hl);
  2312. ai.SetCondition(C_P);
  2313. ai.is_jmp:=true;
  2314. list.concat(ai);
  2315. f2:=FPUFlags2Flags[f];
  2316. end;
  2317. else
  2318. ;
  2319. end;
  2320. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  2321. ai.SetCondition(flags_to_cond(f2));
  2322. ai.is_jmp := true;
  2323. list.concat(ai);
  2324. if assigned(hl) then
  2325. a_label(list,hl);
  2326. end;
  2327. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  2328. var
  2329. ai : taicpu;
  2330. f2 : tresflags;
  2331. hreg,hreg2 : tregister;
  2332. op: tasmop;
  2333. begin
  2334. hreg2:=NR_NO;
  2335. op:=A_AND;
  2336. f2:=f;
  2337. case f of
  2338. F_FE,F_FNE,F_FB,F_FBE:
  2339. begin
  2340. hreg2:=getintregister(list,OS_8);
  2341. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg2);
  2342. if (f=F_FNE) then { F_FNE means "PF or (not ZF)" }
  2343. begin
  2344. ai.setcondition(C_P);
  2345. op:=A_OR;
  2346. end
  2347. else
  2348. ai.setcondition(C_NP);
  2349. list.concat(ai);
  2350. f2:=FPUFlags2Flags[f];
  2351. end;
  2352. F_FA,F_FAE: { These do not need PF check }
  2353. f2:=FPUFlags2Flags[f];
  2354. else
  2355. ;
  2356. end;
  2357. hreg:=makeregsize(list,reg,OS_8);
  2358. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  2359. ai.setcondition(flags_to_cond(f2));
  2360. list.concat(ai);
  2361. if (hreg2<>NR_NO) then
  2362. list.concat(taicpu.op_reg_reg(op,S_B,hreg2,hreg));
  2363. if reg<>hreg then
  2364. a_load_reg_reg(list,OS_8,size,hreg,reg);
  2365. end;
  2366. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  2367. var
  2368. ai : taicpu;
  2369. tmpref : treference;
  2370. f2 : tresflags;
  2371. begin
  2372. f2:=f;
  2373. case f of
  2374. F_FE,F_FNE,F_FB,F_FBE:
  2375. begin
  2376. inherited g_flags2ref(list,size,f,ref);
  2377. exit;
  2378. end;
  2379. F_FA,F_FAE:
  2380. f2:=FPUFlags2Flags[f];
  2381. else
  2382. ;
  2383. end;
  2384. tmpref:=ref;
  2385. make_simple_ref(list,tmpref);
  2386. if not(size in [OS_8,OS_S8]) then
  2387. a_load_const_ref(list,size,0,tmpref);
  2388. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  2389. ai.setcondition(flags_to_cond(f2));
  2390. list.concat(ai);
  2391. {$ifndef cpu64bitalu}
  2392. if size in [OS_S64,OS_64] then
  2393. begin
  2394. inc(tmpref.offset,4);
  2395. a_load_const_ref(list,OS_32,0,tmpref);
  2396. end;
  2397. {$endif cpu64bitalu}
  2398. end;
  2399. { ************* concatcopy ************ }
  2400. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:tcgint);
  2401. const
  2402. {$if defined(cpu64bitalu)}
  2403. REGCX=NR_RCX;
  2404. REGSI=NR_RSI;
  2405. REGDI=NR_RDI;
  2406. copy_len_sizes = [1, 2, 4, 8];
  2407. push_segment_size = S_L;
  2408. {$elseif defined(cpu32bitalu)}
  2409. REGCX=NR_ECX;
  2410. REGSI=NR_ESI;
  2411. REGDI=NR_EDI;
  2412. copy_len_sizes = [1, 2, 4];
  2413. push_segment_size = S_L;
  2414. {$elseif defined(cpu16bitalu)}
  2415. REGCX=NR_CX;
  2416. REGSI=NR_SI;
  2417. REGDI=NR_DI;
  2418. copy_len_sizes = [1, 2, 4]; { 4 is included here, because it's still more
  2419. efficient to use copy_move instead of copy_string for copying 4 bytes }
  2420. push_segment_size = S_W;
  2421. {$endif}
  2422. type copymode=(copy_move,copy_mmx,copy_string,copy_mm,copy_avx);
  2423. var srcref,dstref,tmpref:Treference;
  2424. r,r0,r1,r2,r3:Tregister;
  2425. helpsize:tcgint;
  2426. copysize:byte;
  2427. cgsize:Tcgsize;
  2428. cm:copymode;
  2429. saved_ds,saved_es: Boolean;
  2430. begin
  2431. srcref:=source;
  2432. dstref:=dest;
  2433. {$ifndef i8086}
  2434. make_simple_ref(list,srcref);
  2435. make_simple_ref(list,dstref);
  2436. {$endif not i8086}
  2437. {$ifdef i386}
  2438. { we could handle "far" pointers here, but reloading es/ds is probably much slower
  2439. than just resolving the tls segment }
  2440. if (srcref.refaddr=addr_ntpoff) and (srcref.segment=NR_GS) then
  2441. begin
  2442. r:=getaddressregister(list);
  2443. a_loadaddr_ref_reg(list,srcref,r);
  2444. reference_reset(srcref,srcref.alignment,srcref.volatility);
  2445. srcref.base:=r;
  2446. end;
  2447. if (dstref.refaddr=addr_ntpoff) and (dstref.segment=NR_GS) then
  2448. begin
  2449. r:=getaddressregister(list);
  2450. a_loadaddr_ref_reg(list,dstref,r);
  2451. reference_reset(dstref,dstref.alignment,dstref.volatility);
  2452. dstref.base:=r;
  2453. end;
  2454. {$endif i386}
  2455. {$ifdef x86_64}
  2456. { we could handle "far" pointers here, but reloading es/ds is probably much slower
  2457. than just resolving the tls segment }
  2458. if (srcref.refaddr=addr_tpoff) and (srcref.segment=NR_FS) then
  2459. begin
  2460. r:=getaddressregister(list);
  2461. a_loadaddr_ref_reg(list,srcref,r);
  2462. reference_reset(srcref,srcref.alignment,srcref.volatility);
  2463. srcref.base:=r;
  2464. end;
  2465. if (dstref.refaddr=addr_tpoff) and (dstref.segment=NR_FS) then
  2466. begin
  2467. r:=getaddressregister(list);
  2468. a_loadaddr_ref_reg(list,dstref,r);
  2469. reference_reset(dstref,dstref.alignment,dstref.volatility);
  2470. dstref.base:=r;
  2471. end;
  2472. {$endif x86_64}
  2473. cm:=copy_move;
  2474. helpsize:=3*sizeof(aword);
  2475. if cs_opt_size in current_settings.optimizerswitches then
  2476. helpsize:=2*sizeof(aword);
  2477. {$ifndef i8086}
  2478. { avx helps only to reduce size, using it in general does at least not help on
  2479. an i7-4770 (FK) }
  2480. if (FPUX86_HAS_AVXUNIT in fpu_capabilities[current_settings.fputype]) and
  2481. // (cs_opt_size in current_settings.optimizerswitches) and
  2482. ({$ifdef i386}(len=8) or{$endif i386}(len=16) or (len=24) or (len=32) { or (len=40) or (len=48)}) then
  2483. cm:=copy_avx
  2484. else
  2485. {$ifdef dummy}
  2486. { I'am not sure what CPUs would benefit from using sse instructions for moves (FK) }
  2487. if
  2488. {$ifdef x86_64}
  2489. ((current_settings.fputype>=fpu_sse64)
  2490. {$else x86_64}
  2491. ((current_settings.fputype>=fpu_sse)
  2492. {$endif x86_64}
  2493. or (CPUX86_HAS_SSE2 in cpu_capabilities[current_settings.cputype])) and
  2494. ((len=8) or (len=16) or (len=24) or (len=32) or (len=40) or (len=48)) then
  2495. cm:=copy_mm
  2496. else
  2497. {$endif dummy}
  2498. {$endif i8086}
  2499. if (cs_mmx in current_settings.localswitches) and
  2500. not(pi_uses_fpu in current_procinfo.flags) and
  2501. ((len=8) or (len=16) or (len=24) or (len=32)) then
  2502. cm:=copy_mmx;
  2503. if (len>helpsize) then
  2504. cm:=copy_string;
  2505. if (cs_opt_size in current_settings.optimizerswitches) and
  2506. not((len<=16) and (cm in [copy_mmx,copy_mm,copy_avx])) and
  2507. not(len in copy_len_sizes) then
  2508. cm:=copy_string;
  2509. {$ifndef i8086}
  2510. { using %fs and %gs as segment prefixes is perfectly valid }
  2511. if ((srcref.segment<>NR_NO) and (srcref.segment<>NR_FS) and (srcref.segment<>NR_GS)) or
  2512. ((dstref.segment<>NR_NO) and (dstref.segment<>NR_FS) and (dstref.segment<>NR_GS)) then
  2513. cm:=copy_string;
  2514. {$endif not i8086}
  2515. case cm of
  2516. copy_move:
  2517. begin
  2518. copysize:=sizeof(aint);
  2519. cgsize:=int_cgsize(copysize);
  2520. while len<>0 do
  2521. begin
  2522. if len<2 then
  2523. begin
  2524. copysize:=1;
  2525. cgsize:=OS_8;
  2526. end
  2527. else if len<4 then
  2528. begin
  2529. copysize:=2;
  2530. cgsize:=OS_16;
  2531. end
  2532. {$if defined(cpu32bitalu) or defined(cpu64bitalu)}
  2533. else if len<8 then
  2534. begin
  2535. copysize:=4;
  2536. cgsize:=OS_32;
  2537. end
  2538. {$endif cpu32bitalu or cpu64bitalu}
  2539. {$ifdef cpu64bitalu}
  2540. else if len<16 then
  2541. begin
  2542. copysize:=8;
  2543. cgsize:=OS_64;
  2544. end
  2545. {$endif}
  2546. ;
  2547. dec(len,copysize);
  2548. r:=getintregister(list,cgsize);
  2549. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2550. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2551. inc(srcref.offset,copysize);
  2552. inc(dstref.offset,copysize);
  2553. end;
  2554. end;
  2555. copy_mmx:
  2556. begin
  2557. r0:=getmmxregister(list);
  2558. r1:=NR_NO;
  2559. r2:=NR_NO;
  2560. r3:=NR_NO;
  2561. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  2562. if len>=16 then
  2563. begin
  2564. inc(srcref.offset,8);
  2565. r1:=getmmxregister(list);
  2566. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  2567. end;
  2568. if len>=24 then
  2569. begin
  2570. inc(srcref.offset,8);
  2571. r2:=getmmxregister(list);
  2572. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  2573. end;
  2574. if len>=32 then
  2575. begin
  2576. inc(srcref.offset,8);
  2577. r3:=getmmxregister(list);
  2578. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2579. end;
  2580. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  2581. if len>=16 then
  2582. begin
  2583. inc(dstref.offset,8);
  2584. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  2585. end;
  2586. if len>=24 then
  2587. begin
  2588. inc(dstref.offset,8);
  2589. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  2590. end;
  2591. if len>=32 then
  2592. begin
  2593. inc(dstref.offset,8);
  2594. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2595. end;
  2596. end;
  2597. copy_mm:
  2598. begin
  2599. r0:=NR_NO;
  2600. r1:=NR_NO;
  2601. r2:=NR_NO;
  2602. r3:=NR_NO;
  2603. if len>=16 then
  2604. begin
  2605. r0:=getmmregister(list,OS_M128);
  2606. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r0,nil);
  2607. inc(srcref.offset,16);
  2608. end;
  2609. if len>=32 then
  2610. begin
  2611. r1:=getmmregister(list,OS_M128);
  2612. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r1,nil);
  2613. inc(srcref.offset,16);
  2614. end;
  2615. if len>=48 then
  2616. begin
  2617. r2:=getmmregister(list,OS_M128);
  2618. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r2,nil);
  2619. inc(srcref.offset,16);
  2620. end;
  2621. if (len=8) or (len=24) or (len=40) then
  2622. begin
  2623. r3:=getmmregister(list,OS_M64);
  2624. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2625. end;
  2626. if len>=16 then
  2627. begin
  2628. a_loadmm_reg_ref(list,OS_M128,OS_M128,r0,dstref,nil);
  2629. inc(dstref.offset,16);
  2630. end;
  2631. if len>=32 then
  2632. begin
  2633. a_loadmm_reg_ref(list,OS_M128,OS_M128,r1,dstref,nil);
  2634. inc(dstref.offset,16);
  2635. end;
  2636. if len>=48 then
  2637. begin
  2638. a_loadmm_reg_ref(list,OS_M128,OS_M128,r2,dstref,nil);
  2639. inc(dstref.offset,16);
  2640. end;
  2641. if (len=8) or (len=24) or (len=40) then
  2642. begin
  2643. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2644. end;
  2645. end;
  2646. copy_avx:
  2647. begin
  2648. r0:=NR_NO;
  2649. r1:=NR_NO;
  2650. r2:=NR_NO;
  2651. r3:=NR_NO;
  2652. if len>=16 then
  2653. begin
  2654. r0:=getmmregister(list,OS_M128);
  2655. { we want to force the use of vmovups, so do not use a_loadmm_ref_reg }
  2656. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r0));
  2657. inc(srcref.offset,16);
  2658. end;
  2659. if len>=32 then
  2660. begin
  2661. r1:=getmmregister(list,OS_M128);
  2662. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r1));
  2663. inc(srcref.offset,16);
  2664. end;
  2665. if len>=48 then
  2666. begin
  2667. r2:=getmmregister(list,OS_M128);
  2668. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,srcref,r2));
  2669. inc(srcref.offset,16);
  2670. end;
  2671. if (len=8) or (len=24) or (len=40) then
  2672. begin
  2673. r3:=getmmregister(list,OS_M64);
  2674. list.concat(taicpu.op_ref_reg(A_VMOVSD,S_NO,srcref,r3));
  2675. end;
  2676. if len>=16 then
  2677. begin
  2678. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r0,dstref));
  2679. inc(dstref.offset,16);
  2680. end;
  2681. if len>=32 then
  2682. begin
  2683. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r1,dstref));
  2684. inc(dstref.offset,16);
  2685. end;
  2686. if len>=48 then
  2687. begin
  2688. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r2,dstref));
  2689. inc(dstref.offset,16);
  2690. end;
  2691. if (len=8) or (len=24) or (len=40) then
  2692. begin
  2693. list.concat(taicpu.op_reg_ref(A_VMOVSD,S_NO,r3,dstref));
  2694. end;
  2695. end
  2696. else {copy_string, should be a good fallback in case of unhandled}
  2697. begin
  2698. getcpuregister(list,REGDI);
  2699. if (dstref.segment=NR_NO) and
  2700. (segment_regs_equal(NR_SS,NR_DS) or ((dstref.base<>NR_BP) and (dstref.base<>NR_SP))) then
  2701. begin
  2702. a_loadaddr_ref_reg(list,dstref,REGDI);
  2703. saved_es:=false;
  2704. {$ifdef volatile_es}
  2705. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2706. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2707. {$endif volatile_es}
  2708. end
  2709. else
  2710. begin
  2711. { load offset of dest. reference }
  2712. tmpref:=dstref;
  2713. tmpref.segment:=NR_NO;
  2714. a_loadaddr_ref_reg(list,tmpref,REGDI);
  2715. {$ifdef volatile_es}
  2716. saved_es:=false;
  2717. {$else volatile_es}
  2718. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_ES));
  2719. saved_es:=true;
  2720. {$endif volatile_es}
  2721. if dstref.segment<>NR_NO then
  2722. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,dstref.segment))
  2723. else if (dstref.base=NR_BP) or (dstref.base=NR_SP) then
  2724. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2725. else
  2726. internalerror(2014040401);
  2727. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2728. end;
  2729. getcpuregister(list,REGSI);
  2730. {$ifdef i8086}
  2731. { at this point, si and di are allocated, so no register is available as index =>
  2732. compiler will hang/ie during spilling, so avoid that srcref has base and index, see also tests/tbs/tb0637.pp }
  2733. if (srcref.base<>NR_NO) and (srcref.index<>NR_NO) then
  2734. begin
  2735. r:=getaddressregister(list);
  2736. a_op_reg_reg_reg(list,OP_ADD,OS_ADDR,srcref.base,srcref.index,r);
  2737. srcref.base:=r;
  2738. srcref.index:=NR_NO;
  2739. end;
  2740. {$endif i8086}
  2741. if ((srcref.segment=NR_NO) and (segment_regs_equal(NR_SS,NR_DS) or ((srcref.base<>NR_BP) and (srcref.base<>NR_SP)))) or
  2742. (is_segment_reg(srcref.segment) and segment_regs_equal(srcref.segment,NR_DS)) then
  2743. begin
  2744. srcref.segment:=NR_NO;
  2745. a_loadaddr_ref_reg(list,srcref,REGSI);
  2746. saved_ds:=false;
  2747. end
  2748. else
  2749. begin
  2750. { load offset of source reference }
  2751. tmpref:=srcref;
  2752. tmpref.segment:=NR_NO;
  2753. a_loadaddr_ref_reg(list,tmpref,REGSI);
  2754. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2755. saved_ds:=true;
  2756. if srcref.segment<>NR_NO then
  2757. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,srcref.segment))
  2758. else if (srcref.base=NR_BP) or (srcref.base=NR_SP) then
  2759. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2760. else
  2761. internalerror(2014040402);
  2762. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2763. end;
  2764. getcpuregister(list,REGCX);
  2765. if ts_cld in current_settings.targetswitches then
  2766. list.concat(Taicpu.op_none(A_CLD,S_NO));
  2767. if (cs_opt_size in current_settings.optimizerswitches) and
  2768. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  2769. begin
  2770. a_load_const_reg(list,OS_INT,len,REGCX);
  2771. list.concat(Taicpu.op_none(A_REP,S_NO));
  2772. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2773. end
  2774. else
  2775. begin
  2776. helpsize:=len div sizeof(aint);
  2777. len:=len mod sizeof(aint);
  2778. if helpsize>1 then
  2779. begin
  2780. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  2781. list.concat(Taicpu.op_none(A_REP,S_NO));
  2782. end;
  2783. if helpsize>0 then
  2784. begin
  2785. {$if defined(cpu64bitalu)}
  2786. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  2787. {$elseif defined(cpu32bitalu)}
  2788. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2789. {$elseif defined(cpu16bitalu)}
  2790. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2791. {$endif}
  2792. end;
  2793. if len>=4 then
  2794. begin
  2795. dec(len,4);
  2796. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2797. end;
  2798. if len>=2 then
  2799. begin
  2800. dec(len,2);
  2801. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2802. end;
  2803. if len=1 then
  2804. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2805. end;
  2806. ungetcpuregister(list,REGCX);
  2807. ungetcpuregister(list,REGSI);
  2808. ungetcpuregister(list,REGDI);
  2809. if saved_ds then
  2810. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2811. if saved_es then
  2812. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2813. end;
  2814. end;
  2815. end;
  2816. {****************************************************************************
  2817. Entry/Exit Code Helpers
  2818. ****************************************************************************}
  2819. procedure tcgx86.g_profilecode(list : TAsmList);
  2820. var
  2821. pl : tasmlabel;
  2822. mcountprefix : String[4];
  2823. begin
  2824. case target_info.system of
  2825. {$ifndef NOTARGETWIN}
  2826. system_i386_win32,
  2827. {$endif}
  2828. system_i386_freebsd,
  2829. system_i386_netbsd,
  2830. system_i386_wdosx :
  2831. begin
  2832. Case target_info.system Of
  2833. system_i386_freebsd : mcountprefix:='.';
  2834. system_i386_netbsd : mcountprefix:='__';
  2835. else
  2836. mcountPrefix:='';
  2837. end;
  2838. current_asmdata.getaddrlabel(pl);
  2839. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(pint));
  2840. list.concat(Tai_label.Create(pl));
  2841. list.concat(Tai_const.Create_32bit(0));
  2842. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  2843. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2844. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  2845. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount',false);
  2846. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  2847. end;
  2848. system_i386_linux:
  2849. a_call_name(list,target_info.Cprefix+'mcount',false);
  2850. system_i386_go32v2,system_i386_watcom:
  2851. begin
  2852. a_call_name(list,'MCOUNT',false);
  2853. end;
  2854. system_x86_64_linux,
  2855. system_x86_64_darwin,
  2856. system_x86_64_iphonesim:
  2857. begin
  2858. a_call_name(list,'mcount',false);
  2859. end;
  2860. system_i386_openbsd,
  2861. system_x86_64_openbsd:
  2862. begin
  2863. a_call_name(list,'__mcount',false);
  2864. end;
  2865. else
  2866. internalerror(2019050701);
  2867. end;
  2868. end;
  2869. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2870. procedure decrease_sp(a : tcgint);
  2871. var
  2872. href : treference;
  2873. begin
  2874. reference_reset_base(href,NR_STACK_POINTER_REG,-a,ctempposinvalid,0,[]);
  2875. { normally, lea is a better choice than a sub to adjust the stack pointer }
  2876. list.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_STACK_POINTER_REG));
  2877. end;
  2878. {$ifdef x86}
  2879. {$ifndef NOTARGETWIN}
  2880. var
  2881. href : treference;
  2882. i : integer;
  2883. again : tasmlabel;
  2884. {$endif NOTARGETWIN}
  2885. {$endif x86}
  2886. begin
  2887. if localsize>0 then
  2888. begin
  2889. {$ifdef i386}
  2890. {$ifndef NOTARGETWIN}
  2891. { windows guards only a few pages for stack growing,
  2892. so we have to access every page first }
  2893. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  2894. (localsize>=winstackpagesize) then
  2895. begin
  2896. if localsize div winstackpagesize<=5 then
  2897. begin
  2898. decrease_sp(localsize-4);
  2899. for i:=1 to localsize div winstackpagesize do
  2900. begin
  2901. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize,ctempposinvalid,4,[]);
  2902. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2903. end;
  2904. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2905. end
  2906. else
  2907. begin
  2908. current_asmdata.getjumplabel(again);
  2909. { Using a_reg_alloc instead of getcpuregister, so this procedure
  2910. does not change "used_in_proc" state of EDI and therefore can be
  2911. called after saving registers with "push" instruction
  2912. without creating an unbalanced "pop edi" in epilogue }
  2913. a_reg_alloc(list,NR_EDI);
  2914. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  2915. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  2916. a_label(list,again);
  2917. decrease_sp(winstackpagesize-4);
  2918. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2919. if UseIncDec then
  2920. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI))
  2921. else
  2922. list.concat(Taicpu.op_const_reg(A_SUB,S_L,1,NR_EDI));
  2923. a_jmp_cond(list,OC_NE,again);
  2924. decrease_sp(localsize mod winstackpagesize-4);
  2925. reference_reset_base(href,NR_ESP,localsize-4,ctempposinvalid,4,[]);
  2926. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  2927. a_reg_dealloc(list,NR_EDI);
  2928. end
  2929. end
  2930. else
  2931. {$endif NOTARGETWIN}
  2932. {$endif i386}
  2933. {$ifdef x86_64}
  2934. {$ifndef NOTARGETWIN}
  2935. { windows guards only a few pages for stack growing,
  2936. so we have to access every page first }
  2937. if (target_info.system=system_x86_64_win64) and
  2938. (localsize>=winstackpagesize) then
  2939. begin
  2940. if localsize div winstackpagesize<=5 then
  2941. begin
  2942. decrease_sp(localsize);
  2943. for i:=1 to localsize div winstackpagesize do
  2944. begin
  2945. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4,ctempposinvalid,4,[]);
  2946. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2947. end;
  2948. reference_reset_base(href,NR_RSP,0,ctempposinvalid,4,[]);
  2949. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2950. end
  2951. else
  2952. begin
  2953. current_asmdata.getjumplabel(again);
  2954. getcpuregister(list,NR_R10);
  2955. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  2956. a_label(list,again);
  2957. decrease_sp(winstackpagesize);
  2958. reference_reset_base(href,NR_RSP,0,ctempposinvalid,4,[]);
  2959. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2960. if UseIncDec then
  2961. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10))
  2962. else
  2963. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,1,NR_R10));
  2964. a_jmp_cond(list,OC_NE,again);
  2965. decrease_sp(localsize mod winstackpagesize);
  2966. ungetcpuregister(list,NR_R10);
  2967. end
  2968. end
  2969. else
  2970. {$endif NOTARGETWIN}
  2971. {$endif x86_64}
  2972. decrease_sp(localsize);
  2973. end;
  2974. end;
  2975. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  2976. var
  2977. stackmisalignment: longint;
  2978. regsize: longint;
  2979. {$ifdef i8086}
  2980. dgroup: treference;
  2981. fardataseg: treference;
  2982. {$endif i8086}
  2983. procedure push_regs;
  2984. var
  2985. r: longint;
  2986. usedregs: tcpuregisterset;
  2987. regs_to_save_int: tcpuregisterarray;
  2988. hreg: TRegister;
  2989. begin
  2990. regsize:=0;
  2991. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  2992. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  2993. for r := low(regs_to_save_int) to high(regs_to_save_int) do
  2994. if regs_to_save_int[r] in usedregs then
  2995. begin
  2996. inc(regsize,sizeof(aint));
  2997. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  2998. list.concat(Taicpu.Op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],hreg));
  2999. if current_procinfo.framepointer<>NR_STACK_POINTER_REG then
  3000. current_asmdata.asmcfi.cfa_offset(list,hreg,-(regsize+sizeof(pint)*2+localsize))
  3001. else
  3002. begin
  3003. current_asmdata.asmcfi.cfa_offset(list,hreg,-(regsize+sizeof(pint)+localsize));
  3004. current_asmdata.asmcfi.cfa_def_cfa_offset(list,regsize+localsize+sizeof(pint));
  3005. end;
  3006. end;
  3007. end;
  3008. begin
  3009. regsize:=0;
  3010. stackmisalignment:=0;
  3011. {$ifdef i8086}
  3012. { Win16 callback/exported proc prologue support.
  3013. Since callbacks can be called from different modules, DS on entry may be
  3014. initialized with the data segment of a different module, so we need to
  3015. get ours. But we can't do
  3016. push ds
  3017. mov ax, dgroup
  3018. mov ds, ax
  3019. because code segments are shared between different instances of the same
  3020. module (which have different instances of the current program's data segment),
  3021. so the same 'mov ax, dgroup' instruction will be used for all instances
  3022. of the program and it will load the same segment into ax.
  3023. So, the standard win16 prologue looks like this:
  3024. mov ax, ds
  3025. nop
  3026. inc bp
  3027. push bp
  3028. mov bp, sp
  3029. push ds
  3030. mov ds, ax
  3031. By default, this does nothing, except wasting a few extra machine cycles and
  3032. destroying ax in the process. However, Windows checks the first three bytes
  3033. of every exported function and if they are 'mov ax,ds/nop', they are replaced
  3034. with nop/nop/nop. Then the MakeProcInstance api call should be used to create
  3035. a thunk that loads ds for the current program instance in ax before calling
  3036. the routine.
  3037. And now the fun part comes: somebody (Michael Geary) figured out that all this
  3038. crap was unnecessary, because in Win16 exe modules, we always have DS=SS, so we
  3039. can simply initialize DS from SS :) And then calling MakeProcInstance becomes
  3040. unnecessary. This is what "smart callbacks" (cs_win16_smartcallbacks) do. However,
  3041. this only works for exe files, not for dlls, because dlls run with DS<>SS. There's
  3042. another solution for dlls - since win16 dlls only have a single instance of their
  3043. data segment, we can initialize ds from dgroup. However, there's not a single
  3044. solution for both exe and dlls, so we don't know what to use e.g. in a unit. So,
  3045. that's why there's still an option to turn smart callbacks off and go the
  3046. MakeProcInstance way.
  3047. Additional details here: http://www.geary.com/fixds.html }
  3048. if (current_settings.x86memorymodel<>mm_huge) and
  3049. (po_exports in current_procinfo.procdef.procoptions) and
  3050. (target_info.system=system_i8086_win16) then
  3051. begin
  3052. if cs_win16_smartcallbacks in current_settings.moduleswitches then
  3053. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_SS,NR_AX))
  3054. else
  3055. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_DS,NR_AX));
  3056. list.concat(Taicpu.op_none(A_NOP));
  3057. end
  3058. { interrupt support for i8086 }
  3059. else if po_interrupt in current_procinfo.procdef.procoptions then
  3060. begin
  3061. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_AX));
  3062. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_BX));
  3063. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CX));
  3064. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DX));
  3065. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  3066. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  3067. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  3068. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  3069. if current_settings.x86memorymodel=mm_tiny then
  3070. begin
  3071. { in the tiny memory model, we can't use dgroup, because that
  3072. adds a relocation entry to the .exe and we can't produce a
  3073. .com file (because they don't support relactions), so instead
  3074. we initialize DS from CS. }
  3075. if cs_opt_size in current_settings.optimizerswitches then
  3076. begin
  3077. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CS));
  3078. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  3079. end
  3080. else
  3081. begin
  3082. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_CS,NR_AX));
  3083. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3084. end;
  3085. end
  3086. else if current_settings.x86memorymodel=mm_huge then
  3087. begin
  3088. reference_reset(fardataseg,0,[]);
  3089. fardataseg.refaddr:=addr_fardataseg;
  3090. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  3091. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3092. end
  3093. else
  3094. begin
  3095. reference_reset(dgroup,0,[]);
  3096. dgroup.refaddr:=addr_dgroup;
  3097. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,dgroup,NR_AX));
  3098. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3099. end;
  3100. end;
  3101. {$endif i8086}
  3102. {$ifdef i386}
  3103. { interrupt support for i386 }
  3104. if (po_interrupt in current_procinfo.procdef.procoptions) then
  3105. begin
  3106. { .... also the segment registers }
  3107. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  3108. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  3109. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  3110. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  3111. { save the registers of an interrupt procedure }
  3112. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  3113. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  3114. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  3115. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  3116. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  3117. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  3118. { pushf, push %cs, 4*selector registers, 6*general purpose registers }
  3119. inc(stackmisalignment,4+4+4*2+6*4);
  3120. end;
  3121. {$endif i386}
  3122. { save old framepointer }
  3123. if not nostackframe then
  3124. begin
  3125. { return address }
  3126. inc(stackmisalignment,sizeof(pint));
  3127. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  3128. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3129. begin
  3130. {$ifdef i386}
  3131. if (not paramanager.use_fixed_stack) then
  3132. push_regs;
  3133. {$endif i386}
  3134. CGmessage(cg_d_stackframe_omited);
  3135. end
  3136. else
  3137. begin
  3138. {$ifdef i8086}
  3139. if ((ts_x86_far_procs_push_odd_bp in current_settings.targetswitches) or
  3140. ((po_exports in current_procinfo.procdef.procoptions) and
  3141. (target_info.system=system_i8086_win16))) and
  3142. is_proc_far(current_procinfo.procdef) then
  3143. cg.a_op_const_reg(list,OP_ADD,OS_ADDR,1,current_procinfo.framepointer);
  3144. {$endif i8086}
  3145. { push <frame_pointer> }
  3146. inc(stackmisalignment,sizeof(pint));
  3147. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  3148. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  3149. { Return address and FP are both on stack }
  3150. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(pint));
  3151. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(pint)));
  3152. if current_procinfo.procdef.proctypeoption<>potype_exceptfilter then
  3153. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG))
  3154. else
  3155. begin
  3156. push_regs;
  3157. gen_load_frame_for_exceptfilter(list);
  3158. { Need only as much stack space as necessary to do the calls.
  3159. Exception filters don't have own local vars, and temps are 'mapped'
  3160. to the parent procedure.
  3161. maxpushedparasize is already aligned at least on x86_64. }
  3162. localsize:=current_procinfo.maxpushedparasize;
  3163. end;
  3164. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  3165. end;
  3166. { allocate stackframe space }
  3167. if (localsize<>0) or
  3168. ((target_info.stackalign>sizeof(pint)) and
  3169. (stackmisalignment <> 0) and
  3170. ((pi_do_call in current_procinfo.flags) or
  3171. (po_assembler in current_procinfo.procdef.procoptions))) then
  3172. begin
  3173. if target_info.stackalign>sizeof(pint) then
  3174. localsize := align(localsize+stackmisalignment,target_info.stackalign)-stackmisalignment;
  3175. g_stackpointer_alloc(list,localsize);
  3176. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  3177. current_asmdata.asmcfi.cfa_def_cfa_offset(list,regsize+localsize+sizeof(pint));
  3178. current_procinfo.final_localsize:=localsize;
  3179. end
  3180. {$ifdef i8086}
  3181. else
  3182. { on i8086 we always call g_stackpointer_alloc, even with a zero size,
  3183. because it will generate code for stack checking, if stack checking is on }
  3184. g_stackpointer_alloc(list,0)
  3185. {$endif i8086}
  3186. ;
  3187. {$ifdef i8086}
  3188. { win16 exported proc prologue follow-up (see the huge comment above for details) }
  3189. if (current_settings.x86memorymodel<>mm_huge) and
  3190. (po_exports in current_procinfo.procdef.procoptions) and
  3191. (target_info.system=system_i8086_win16) then
  3192. begin
  3193. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  3194. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3195. end
  3196. else if (current_settings.x86memorymodel=mm_huge) and
  3197. not (po_interrupt in current_procinfo.procdef.procoptions) then
  3198. begin
  3199. list.concat(Taicpu.op_reg(A_PUSH,S_W,NR_DS));
  3200. reference_reset(fardataseg,0,[]);
  3201. fardataseg.refaddr:=addr_fardataseg;
  3202. if current_procinfo.procdef.proccalloption=pocall_register then
  3203. begin
  3204. { Use BX register if using register convention
  3205. as it is not a register used to store parameters }
  3206. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_BX));
  3207. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_BX,NR_DS));
  3208. end
  3209. else
  3210. begin
  3211. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,fardataseg,NR_AX));
  3212. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  3213. end;
  3214. end;
  3215. { SI and DI are volatile in the BP7 and FPC's pascal calling convention,
  3216. but must be preserved in Microsoft C's pascal calling convention, and
  3217. since Windows is compiled with Microsoft compilers, these registers
  3218. must be saved for exported procedures (BP7 for Win16 also does this). }
  3219. if (po_exports in current_procinfo.procdef.procoptions) and
  3220. (target_info.system=system_i8086_win16) then
  3221. begin
  3222. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  3223. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  3224. end;
  3225. {$endif i8086}
  3226. {$ifdef i386}
  3227. if (not paramanager.use_fixed_stack) and
  3228. (current_procinfo.framepointer<>NR_STACK_POINTER_REG) and
  3229. (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  3230. begin
  3231. regsize:=0;
  3232. push_regs;
  3233. reference_reset_base(current_procinfo.save_regs_ref,
  3234. current_procinfo.framepointer,
  3235. -(localsize+regsize),ctempposinvalid,sizeof(aint),[]);
  3236. end;
  3237. {$endif i386}
  3238. end;
  3239. end;
  3240. procedure tcgx86.g_save_registers(list: TAsmList);
  3241. begin
  3242. {$ifdef i386}
  3243. if paramanager.use_fixed_stack then
  3244. {$endif i386}
  3245. inherited g_save_registers(list);
  3246. end;
  3247. procedure tcgx86.g_restore_registers(list: TAsmList);
  3248. begin
  3249. {$ifdef i386}
  3250. if paramanager.use_fixed_stack then
  3251. {$endif i386}
  3252. inherited g_restore_registers(list);
  3253. end;
  3254. procedure tcgx86.internal_restore_regs(list: TAsmList; use_pop: boolean);
  3255. var
  3256. r: longint;
  3257. hreg: tregister;
  3258. href: treference;
  3259. usedregs: tcpuregisterset;
  3260. regs_to_save_int: tcpuregisterarray;
  3261. begin
  3262. href:=current_procinfo.save_regs_ref;
  3263. usedregs:=rg[R_INTREGISTER].used_in_proc-paramanager.get_volatile_registers_int(current_procinfo.procdef.proccalloption);
  3264. regs_to_save_int:=paramanager.get_saved_registers_int(current_procinfo.procdef.proccalloption);
  3265. for r:=high(regs_to_save_int) downto low(regs_to_save_int) do
  3266. if regs_to_save_int[r] in usedregs then
  3267. begin
  3268. hreg:=newreg(R_INTREGISTER,regs_to_save_int[r],R_SUBWHOLE);
  3269. { Allocate register so the optimizer does not remove the load }
  3270. a_reg_alloc(list,hreg);
  3271. if use_pop then
  3272. list.concat(Taicpu.Op_reg(A_POP,tcgsize2opsize[OS_ADDR],hreg))
  3273. else
  3274. begin
  3275. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  3276. inc(href.offset,sizeof(aint));
  3277. end;
  3278. current_asmdata.asmcfi.cfa_restore(list,hreg);
  3279. end;
  3280. end;
  3281. procedure tcgx86.generate_leave(list: TAsmList);
  3282. begin
  3283. if UseLeave then
  3284. list.concat(taicpu.op_none(A_LEAVE,S_NO))
  3285. else
  3286. begin
  3287. {$if defined(x86_64)}
  3288. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_RSP);
  3289. list.Concat(taicpu.op_reg_reg(A_MOV,S_Q,NR_RBP,NR_RSP));
  3290. current_asmdata.asmcfi.cfa_restore(list,NR_RBP);
  3291. current_asmdata.asmcfi.cfa_def_cfa_offset(list,8);
  3292. list.Concat(taicpu.op_reg(A_POP,S_Q,NR_RBP));
  3293. {$elseif defined(i386)}
  3294. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_ESP);
  3295. list.Concat(taicpu.op_reg_reg(A_MOV,S_L,NR_EBP,NR_ESP));
  3296. current_asmdata.asmcfi.cfa_restore(list,NR_EBP);
  3297. current_asmdata.asmcfi.cfa_def_cfa_offset(list,4);
  3298. list.Concat(taicpu.op_reg(A_POP,S_L,NR_EBP));
  3299. {$elseif defined(i8086)}
  3300. list.Concat(taicpu.op_reg_reg(A_MOV,S_W,NR_BP,NR_SP));
  3301. list.Concat(taicpu.op_reg(A_POP,S_W,NR_BP));
  3302. {$endif}
  3303. end;
  3304. end;
  3305. { produces if necessary overflowcode }
  3306. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  3307. var
  3308. hl : tasmlabel;
  3309. ai : taicpu;
  3310. cond : TAsmCond;
  3311. begin
  3312. if not(cs_check_overflow in current_settings.localswitches) then
  3313. exit;
  3314. current_asmdata.getjumplabel(hl);
  3315. if not ((def.typ=pointerdef) or
  3316. ((def.typ=orddef) and
  3317. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  3318. pasbool1,pasbool8,pasbool16,pasbool32,pasbool64]))) then
  3319. cond:=C_NO
  3320. else
  3321. cond:=C_NB;
  3322. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  3323. ai.SetCondition(cond);
  3324. ai.is_jmp:=true;
  3325. list.concat(ai);
  3326. a_call_name(list,'FPC_OVERFLOW',false);
  3327. a_label(list,hl);
  3328. end;
  3329. end.