cgx86.pas 130 KB

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