cgx86.pas 111 KB

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