cgx86.pas 135 KB

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