cgx86.pas 113 KB

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