cgx86.pas 137 KB

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