cgx86.pas 134 KB

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