cgx86.pas 123 KB

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