cgx86.pas 136 KB

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