2
0

cgx86.pas 140 KB

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