cgx86.pas 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038
  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. if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1409. not(cs_check_overflow in current_settings.localswitches) and
  1410. (a>1) and ispowerof2(int64(a-1),power) and (power in [1..3]) then
  1411. begin
  1412. reference_reset_base(href,src,0,0);
  1413. href.index:=src;
  1414. href.scalefactor:=a-1;
  1415. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1416. end
  1417. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1418. not(cs_check_overflow in current_settings.localswitches) and
  1419. (a>1) and ispowerof2(int64(a),power) and (power in [1..3]) then
  1420. begin
  1421. reference_reset_base(href,NR_NO,0,0);
  1422. href.index:=src;
  1423. href.scalefactor:=a;
  1424. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1425. end
  1426. else if (op in [OP_MUL,OP_IMUL]) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1427. (a>1) and not ispowerof2(int64(a),power) then
  1428. begin
  1429. { MUL with overflow checking should be handled specifically in the code generator }
  1430. if (op=OP_MUL) and (cs_check_overflow in current_settings.localswitches) then
  1431. internalerror(2014011801);
  1432. list.concat(taicpu.op_const_reg_reg(A_IMUL,TCgSize2OpSize[size],a,src,dst));
  1433. end
  1434. else if (op=OP_ADD) and
  1435. ((size in [OS_32,OS_S32]) or
  1436. { lea supports only 32 bit signed displacments }
  1437. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1438. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1439. ) and
  1440. not(cs_check_overflow in current_settings.localswitches) then
  1441. begin
  1442. { a might still be in the range 0x80000000 to 0xffffffff
  1443. which might trigger a range check error as
  1444. reference_reset_base expects a longint value. }
  1445. {$push} {$R-}{$Q-}
  1446. al := longint (a);
  1447. {$pop}
  1448. reference_reset_base(href,src,al,0);
  1449. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1450. end
  1451. else if (op=OP_SUB) and
  1452. ((size in [OS_32,OS_S32]) or
  1453. { lea supports only 32 bit signed displacments }
  1454. ((size=OS_64) and (a>=0) and (a<=maxLongint)) or
  1455. ((size=OS_S64) and (a>=-maxLongint) and (a<=maxLongint))
  1456. ) and
  1457. not(cs_check_overflow in current_settings.localswitches) then
  1458. begin
  1459. reference_reset_base(href,src,-a,0);
  1460. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1461. end
  1462. else if (op in [OP_ROR,OP_ROL]) and
  1463. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1464. (size in [OS_32,OS_S32
  1465. {$ifdef x86_64}
  1466. ,OS_64,OS_S64
  1467. {$endif x86_64}
  1468. ]) then
  1469. begin
  1470. if op=OP_ROR then
  1471. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size], a,src,dst))
  1472. else
  1473. list.concat(taicpu.op_const_reg_reg(A_RORX,TCgSize2OpSize[size],TCgSize2Size[size]*8-a,src,dst));
  1474. end
  1475. else
  1476. inherited a_op_const_reg_reg(list,op,size,a,src,dst);
  1477. end;
  1478. procedure tcgx86.a_op_reg_reg_reg(list: TAsmList; op: TOpCg;
  1479. size: tcgsize; src1, src2, dst: tregister);
  1480. var
  1481. href : treference;
  1482. begin
  1483. if (op=OP_ADD) and (size in [OS_32,OS_S32,OS_64,OS_S64]) and
  1484. not(cs_check_overflow in current_settings.localswitches) then
  1485. begin
  1486. reference_reset_base(href,src1,0,0);
  1487. href.index:=src2;
  1488. list.concat(taicpu.op_ref_reg(A_LEA,TCgSize2OpSize[size],href,dst));
  1489. end
  1490. else if (op in [OP_SHR,OP_SHL]) and
  1491. (CPUX86_HAS_BMI2 in cpu_capabilities[current_settings.cputype]) and
  1492. (size in [OS_32,OS_S32
  1493. {$ifdef x86_64}
  1494. ,OS_64,OS_S64
  1495. {$endif x86_64}
  1496. ]) then
  1497. begin
  1498. if op=OP_SHL then
  1499. list.concat(taicpu.op_reg_reg_reg(A_SHLX,TCgSize2OpSize[size],src1,src2,dst))
  1500. else
  1501. list.concat(taicpu.op_reg_reg_reg(A_SHRX,TCgSize2OpSize[size],src1,src2,dst));
  1502. end
  1503. else
  1504. inherited a_op_reg_reg_reg(list,op,size,src1,src2,dst);
  1505. end;
  1506. {$endif not i8086}
  1507. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; reg: TRegister);
  1508. var
  1509. opcode : tasmop;
  1510. power : longint;
  1511. href : treference;
  1512. {$ifdef x86_64}
  1513. tmpreg : tregister;
  1514. {$endif x86_64}
  1515. begin
  1516. optimize_op_const(size, op, a);
  1517. {$ifdef x86_64}
  1518. { x86_64 only supports signed 32 bits constants directly }
  1519. if not(op in [OP_NONE,OP_MOVE]) and
  1520. (size in [OS_S64,OS_64]) and
  1521. ((a<low(longint)) or (a>high(longint))) then
  1522. begin
  1523. tmpreg:=getintregister(list,size);
  1524. a_load_const_reg(list,size,a,tmpreg);
  1525. a_op_reg_reg(list,op,size,tmpreg,reg);
  1526. exit;
  1527. end;
  1528. {$endif x86_64}
  1529. check_register_size(size,reg);
  1530. case op of
  1531. OP_NONE :
  1532. begin
  1533. { Opcode is optimized away }
  1534. end;
  1535. OP_MOVE :
  1536. begin
  1537. { Optimized, replaced with a simple load }
  1538. a_load_const_reg(list,size,a,reg);
  1539. end;
  1540. OP_DIV, OP_IDIV:
  1541. begin
  1542. { should be handled specifically in the code }
  1543. { generator because of the silly register usage restraints }
  1544. internalerror(200109224);
  1545. end;
  1546. OP_MUL,OP_IMUL:
  1547. begin
  1548. if not (cs_check_overflow in current_settings.localswitches) then
  1549. op:=OP_IMUL;
  1550. if op = OP_IMUL then
  1551. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  1552. else
  1553. { OP_MUL should be handled specifically in the code }
  1554. { generator because of the silly register usage restraints }
  1555. internalerror(200109225);
  1556. end;
  1557. OP_ADD, OP_SUB:
  1558. if not(cs_check_overflow in current_settings.localswitches) and
  1559. (a = 1) and
  1560. UseIncDec then
  1561. begin
  1562. if op = OP_ADD then
  1563. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  1564. else
  1565. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  1566. end
  1567. else
  1568. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],aint(a),reg));
  1569. OP_AND,OP_OR:
  1570. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],aint(a),reg));
  1571. OP_XOR:
  1572. if (aword(a)=high(aword)) then
  1573. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg))
  1574. else
  1575. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],aint(a),reg));
  1576. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1577. begin
  1578. {$if defined(x86_64)}
  1579. if (a and 63) <> 0 Then
  1580. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,reg));
  1581. if (a shr 6) <> 0 Then
  1582. internalerror(200609073);
  1583. {$elseif defined(i386)}
  1584. if (a and 31) <> 0 Then
  1585. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1586. if (a shr 5) <> 0 Then
  1587. internalerror(200609071);
  1588. {$elseif defined(i8086)}
  1589. if (a shr 5) <> 0 Then
  1590. internalerror(2013043002);
  1591. a := a and 31;
  1592. if a <> 0 Then
  1593. begin
  1594. if (current_settings.cputype < cpu_186) and (a <> 1) then
  1595. begin
  1596. getcpuregister(list,NR_CL);
  1597. a_load_const_reg(list,OS_8,a,NR_CL);
  1598. list.concat(taicpu.op_reg_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,reg));
  1599. ungetcpuregister(list,NR_CL);
  1600. end
  1601. else
  1602. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  1603. end;
  1604. {$endif}
  1605. end
  1606. else internalerror(200609072);
  1607. end;
  1608. end;
  1609. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: tcgint; const ref: TReference);
  1610. var
  1611. opcode: tasmop;
  1612. power: longint;
  1613. {$ifdef x86_64}
  1614. tmpreg : tregister;
  1615. {$endif x86_64}
  1616. tmpref : treference;
  1617. begin
  1618. optimize_op_const(size, op, a);
  1619. if op in [OP_NONE,OP_MOVE] then
  1620. begin
  1621. if (op=OP_MOVE) then
  1622. a_load_const_ref(list,size,a,ref);
  1623. exit;
  1624. end;
  1625. {$ifdef x86_64}
  1626. { x86_64 only supports signed 32 bits constants directly }
  1627. if (size in [OS_S64,OS_64]) and
  1628. ((a<low(longint)) or (a>high(longint))) then
  1629. begin
  1630. tmpreg:=getintregister(list,size);
  1631. a_load_const_reg(list,size,a,tmpreg);
  1632. a_op_reg_ref(list,op,size,tmpreg,ref);
  1633. exit;
  1634. end;
  1635. {$endif x86_64}
  1636. tmpref:=ref;
  1637. make_simple_ref(list,tmpref);
  1638. Case Op of
  1639. OP_DIV, OP_IDIV:
  1640. Begin
  1641. { should be handled specifically in the code }
  1642. { generator because of the silly register usage restraints }
  1643. internalerror(200109231);
  1644. End;
  1645. OP_MUL,OP_IMUL:
  1646. begin
  1647. if not (cs_check_overflow in current_settings.localswitches) then
  1648. op:=OP_IMUL;
  1649. { can't multiply a memory location directly with a constant }
  1650. if op = OP_IMUL then
  1651. inherited a_op_const_ref(list,op,size,a,tmpref)
  1652. else
  1653. { OP_MUL should be handled specifically in the code }
  1654. { generator because of the silly register usage restraints }
  1655. internalerror(200109232);
  1656. end;
  1657. OP_ADD, OP_SUB:
  1658. if not(cs_check_overflow in current_settings.localswitches) and
  1659. (a = 1) and
  1660. UseIncDec then
  1661. begin
  1662. if op = OP_ADD then
  1663. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  1664. else
  1665. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  1666. end
  1667. else
  1668. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1669. OP_AND,OP_OR:
  1670. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1671. OP_XOR:
  1672. if (aword(a)=high(aword)) then
  1673. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref))
  1674. else
  1675. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1676. OP_SHL,OP_SHR,OP_SAR,OP_ROL,OP_ROR:
  1677. begin
  1678. {$if defined(x86_64)}
  1679. if (a and 63) <> 0 Then
  1680. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,tmpref));
  1681. if (a shr 6) <> 0 Then
  1682. internalerror(2013111003);
  1683. {$elseif defined(i386)}
  1684. if (a and 31) <> 0 Then
  1685. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  1686. if (a shr 5) <> 0 Then
  1687. internalerror(2013111002);
  1688. {$elseif defined(i8086)}
  1689. if (a shr 5) <> 0 Then
  1690. internalerror(2013111001);
  1691. a := a and 31;
  1692. if a <> 0 Then
  1693. begin
  1694. if (current_settings.cputype < cpu_186) and (a <> 1) then
  1695. begin
  1696. getcpuregister(list,NR_CL);
  1697. a_load_const_reg(list,OS_8,a,NR_CL);
  1698. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],NR_CL,tmpref));
  1699. ungetcpuregister(list,NR_CL);
  1700. end
  1701. else
  1702. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,tmpref));
  1703. end;
  1704. {$endif}
  1705. end
  1706. else internalerror(68992);
  1707. end;
  1708. end;
  1709. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1710. const
  1711. {$if defined(cpu64bitalu)}
  1712. REGCX=NR_RCX;
  1713. REGCX_Size = OS_64;
  1714. {$elseif defined(cpu32bitalu)}
  1715. REGCX=NR_ECX;
  1716. REGCX_Size = OS_32;
  1717. {$elseif defined(cpu16bitalu)}
  1718. REGCX=NR_CX;
  1719. REGCX_Size = OS_16;
  1720. {$endif}
  1721. var
  1722. dstsize: topsize;
  1723. instr:Taicpu;
  1724. begin
  1725. check_register_size(size,src);
  1726. check_register_size(size,dst);
  1727. dstsize := tcgsize2opsize[size];
  1728. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  1729. op:=OP_IMUL;
  1730. case op of
  1731. OP_NEG,OP_NOT:
  1732. begin
  1733. if src<>dst then
  1734. a_load_reg_reg(list,size,size,src,dst);
  1735. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  1736. end;
  1737. OP_MUL,OP_DIV,OP_IDIV:
  1738. { special stuff, needs separate handling inside code }
  1739. { generator }
  1740. internalerror(200109233);
  1741. OP_SHR,OP_SHL,OP_SAR,OP_ROL,OP_ROR:
  1742. begin
  1743. { Use ecx to load the value, that allows better coalescing }
  1744. getcpuregister(list,REGCX);
  1745. a_load_reg_reg(list,size,REGCX_Size,src,REGCX);
  1746. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  1747. ungetcpuregister(list,REGCX);
  1748. end;
  1749. else
  1750. begin
  1751. if reg2opsize(src) <> dstsize then
  1752. internalerror(200109226);
  1753. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  1754. list.concat(instr);
  1755. end;
  1756. end;
  1757. end;
  1758. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1759. var
  1760. tmpref : treference;
  1761. begin
  1762. tmpref:=ref;
  1763. make_simple_ref(list,tmpref);
  1764. check_register_size(size,reg);
  1765. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  1766. op:=OP_IMUL;
  1767. case op of
  1768. OP_NEG,OP_NOT,OP_IMUL:
  1769. begin
  1770. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1771. end;
  1772. OP_MUL,OP_DIV,OP_IDIV:
  1773. { special stuff, needs separate handling inside code }
  1774. { generator }
  1775. internalerror(200109239);
  1776. else
  1777. begin
  1778. reg := makeregsize(list,reg,size);
  1779. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  1780. end;
  1781. end;
  1782. end;
  1783. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1784. var
  1785. tmpref : treference;
  1786. begin
  1787. tmpref:=ref;
  1788. make_simple_ref(list,tmpref);
  1789. check_register_size(size,reg);
  1790. if (op=OP_MUL) and not (cs_check_overflow in current_settings.localswitches) then
  1791. op:=OP_IMUL;
  1792. case op of
  1793. OP_NEG,OP_NOT:
  1794. begin
  1795. if reg<>NR_NO then
  1796. internalerror(200109237);
  1797. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  1798. end;
  1799. OP_IMUL:
  1800. begin
  1801. { this one needs a load/imul/store, which is the default }
  1802. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1803. end;
  1804. OP_MUL,OP_DIV,OP_IDIV:
  1805. { special stuff, needs separate handling inside code }
  1806. { generator }
  1807. internalerror(200109238);
  1808. else
  1809. begin
  1810. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  1811. end;
  1812. end;
  1813. end;
  1814. procedure tcgx86.a_bit_scan_reg_reg(list: TAsmList; reverse: boolean; size: TCGSize; src, dst: TRegister);
  1815. var
  1816. opsize: topsize;
  1817. l : TAsmLabel;
  1818. begin
  1819. opsize:=tcgsize2opsize[size];
  1820. if not reverse then
  1821. list.concat(taicpu.op_reg_reg(A_BSF,opsize,src,dst))
  1822. else
  1823. list.concat(taicpu.op_reg_reg(A_BSR,opsize,src,dst));
  1824. current_asmdata.getjumplabel(l);
  1825. a_jmp_cond(list,OC_NE,l);
  1826. list.concat(taicpu.op_const_reg(A_MOV,opsize,$ff,dst));
  1827. a_label(list,l);
  1828. end;
  1829. {*************** compare instructructions ****************}
  1830. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;reg : tregister;
  1831. l : tasmlabel);
  1832. {$ifdef x86_64}
  1833. var
  1834. tmpreg : tregister;
  1835. {$endif x86_64}
  1836. begin
  1837. {$ifdef x86_64}
  1838. { x86_64 only supports signed 32 bits constants directly }
  1839. if (size in [OS_S64,OS_64]) and
  1840. ((a<low(longint)) or (a>high(longint))) then
  1841. begin
  1842. tmpreg:=getintregister(list,size);
  1843. a_load_const_reg(list,size,a,tmpreg);
  1844. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1845. exit;
  1846. end;
  1847. {$endif x86_64}
  1848. if (a = 0) then
  1849. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  1850. else
  1851. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  1852. a_jmp_cond(list,cmp_op,l);
  1853. end;
  1854. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : tcgint;const ref : treference;
  1855. l : tasmlabel);
  1856. var
  1857. {$ifdef x86_64}
  1858. tmpreg : tregister;
  1859. {$endif x86_64}
  1860. tmpref : treference;
  1861. begin
  1862. tmpref:=ref;
  1863. make_simple_ref(list,tmpref);
  1864. {$ifdef x86_64}
  1865. { x86_64 only supports signed 32 bits constants directly }
  1866. if (size in [OS_S64,OS_64]) and
  1867. ((a<low(longint)) or (a>high(longint))) then
  1868. begin
  1869. tmpreg:=getintregister(list,size);
  1870. a_load_const_reg(list,size,a,tmpreg);
  1871. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  1872. exit;
  1873. end;
  1874. {$endif x86_64}
  1875. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  1876. a_jmp_cond(list,cmp_op,l);
  1877. end;
  1878. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  1879. reg1,reg2 : tregister;l : tasmlabel);
  1880. begin
  1881. check_register_size(size,reg1);
  1882. check_register_size(size,reg2);
  1883. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  1884. a_jmp_cond(list,cmp_op,l);
  1885. end;
  1886. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  1887. var
  1888. tmpref : treference;
  1889. begin
  1890. tmpref:=ref;
  1891. make_simple_ref(list,tmpref);
  1892. check_register_size(size,reg);
  1893. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  1894. a_jmp_cond(list,cmp_op,l);
  1895. end;
  1896. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  1897. var
  1898. tmpref : treference;
  1899. begin
  1900. tmpref:=ref;
  1901. make_simple_ref(list,tmpref);
  1902. check_register_size(size,reg);
  1903. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  1904. a_jmp_cond(list,cmp_op,l);
  1905. end;
  1906. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1907. var
  1908. ai : taicpu;
  1909. begin
  1910. if cond=OC_None then
  1911. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1912. else
  1913. begin
  1914. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  1915. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1916. end;
  1917. ai.is_jmp:=true;
  1918. list.concat(ai);
  1919. end;
  1920. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1921. var
  1922. ai : taicpu;
  1923. hl : tasmlabel;
  1924. f2 : tresflags;
  1925. begin
  1926. hl:=nil;
  1927. f2:=f;
  1928. case f of
  1929. F_FNE:
  1930. begin
  1931. ai:=Taicpu.op_sym(A_Jcc,S_NO,l);
  1932. ai.SetCondition(C_P);
  1933. ai.is_jmp:=true;
  1934. list.concat(ai);
  1935. f2:=F_NE;
  1936. end;
  1937. F_FE,F_FA,F_FAE,F_FB,F_FBE:
  1938. begin
  1939. { JP before JA/JAE is redundant, but it must be generated here
  1940. and left for peephole optimizer to remove. }
  1941. current_asmdata.getjumplabel(hl);
  1942. ai:=Taicpu.op_sym(A_Jcc,S_NO,hl);
  1943. ai.SetCondition(C_P);
  1944. ai.is_jmp:=true;
  1945. list.concat(ai);
  1946. f2:=FPUFlags2Flags[f];
  1947. end;
  1948. end;
  1949. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  1950. ai.SetCondition(flags_to_cond(f2));
  1951. ai.is_jmp := true;
  1952. list.concat(ai);
  1953. if assigned(hl) then
  1954. a_label(list,hl);
  1955. end;
  1956. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1957. var
  1958. ai : taicpu;
  1959. f2 : tresflags;
  1960. hreg,hreg2 : tregister;
  1961. op: tasmop;
  1962. begin
  1963. hreg2:=NR_NO;
  1964. op:=A_AND;
  1965. f2:=f;
  1966. case f of
  1967. F_FE,F_FNE,F_FB,F_FBE:
  1968. begin
  1969. hreg2:=getintregister(list,OS_8);
  1970. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg2);
  1971. if (f=F_FNE) then { F_FNE means "PF or (not ZF)" }
  1972. begin
  1973. ai.setcondition(C_P);
  1974. op:=A_OR;
  1975. end
  1976. else
  1977. ai.setcondition(C_NP);
  1978. list.concat(ai);
  1979. f2:=FPUFlags2Flags[f];
  1980. end;
  1981. F_FA,F_FAE: { These do not need PF check }
  1982. f2:=FPUFlags2Flags[f];
  1983. end;
  1984. hreg:=makeregsize(list,reg,OS_8);
  1985. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  1986. ai.setcondition(flags_to_cond(f2));
  1987. list.concat(ai);
  1988. if (hreg2<>NR_NO) then
  1989. list.concat(taicpu.op_reg_reg(op,S_B,hreg2,hreg));
  1990. if reg<>hreg then
  1991. a_load_reg_reg(list,OS_8,size,hreg,reg);
  1992. end;
  1993. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  1994. var
  1995. ai : taicpu;
  1996. tmpref : treference;
  1997. f2 : tresflags;
  1998. begin
  1999. f2:=f;
  2000. case f of
  2001. F_FE,F_FNE,F_FB,F_FBE:
  2002. begin
  2003. inherited g_flags2ref(list,size,f,ref);
  2004. exit;
  2005. end;
  2006. F_FA,F_FAE:
  2007. f2:=FPUFlags2Flags[f];
  2008. end;
  2009. tmpref:=ref;
  2010. make_simple_ref(list,tmpref);
  2011. if not(size in [OS_8,OS_S8]) then
  2012. a_load_const_ref(list,size,0,tmpref);
  2013. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  2014. ai.setcondition(flags_to_cond(f2));
  2015. list.concat(ai);
  2016. {$ifndef cpu64bitalu}
  2017. if size in [OS_S64,OS_64] then
  2018. begin
  2019. inc(tmpref.offset,4);
  2020. a_load_const_ref(list,OS_32,0,tmpref);
  2021. end;
  2022. {$endif cpu64bitalu}
  2023. end;
  2024. { ************* concatcopy ************ }
  2025. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:tcgint);
  2026. const
  2027. {$if defined(cpu64bitalu)}
  2028. REGCX=NR_RCX;
  2029. REGSI=NR_RSI;
  2030. REGDI=NR_RDI;
  2031. copy_len_sizes = [1, 2, 4, 8];
  2032. push_segment_size = S_L;
  2033. {$elseif defined(cpu32bitalu)}
  2034. REGCX=NR_ECX;
  2035. REGSI=NR_ESI;
  2036. REGDI=NR_EDI;
  2037. copy_len_sizes = [1, 2, 4];
  2038. push_segment_size = S_L;
  2039. {$elseif defined(cpu16bitalu)}
  2040. REGCX=NR_CX;
  2041. REGSI=NR_SI;
  2042. REGDI=NR_DI;
  2043. copy_len_sizes = [1, 2];
  2044. push_segment_size = S_W;
  2045. {$endif}
  2046. type copymode=(copy_move,copy_mmx,copy_string,copy_mm,copy_avx);
  2047. var srcref,dstref,tmpref:Treference;
  2048. r,r0,r1,r2,r3:Tregister;
  2049. helpsize:tcgint;
  2050. copysize:byte;
  2051. cgsize:Tcgsize;
  2052. cm:copymode;
  2053. saved_ds,saved_es: Boolean;
  2054. begin
  2055. cm:=copy_move;
  2056. helpsize:=3*sizeof(aword);
  2057. if cs_opt_size in current_settings.optimizerswitches then
  2058. helpsize:=2*sizeof(aword);
  2059. {$ifndef i8086}
  2060. { avx helps only to reduce size, using it in general does at least not help on
  2061. an i7-4770 (FK) }
  2062. if (CPUX86_HAS_AVXUNIT in cpu_capabilities[current_settings.cputype]) and
  2063. // (cs_opt_size in current_settings.optimizerswitches) and
  2064. ((len=8) or (len=16) or (len=24) or (len=32) { or (len=40) or (len=48)}) then
  2065. cm:=copy_avx
  2066. else
  2067. {$ifdef dummy}
  2068. { I'am not sure what CPUs would benefit from using sse instructions for moves (FK) }
  2069. if
  2070. {$ifdef x86_64}
  2071. ((current_settings.fputype>=fpu_sse64)
  2072. {$else x86_64}
  2073. ((current_settings.fputype>=fpu_sse)
  2074. {$endif x86_64}
  2075. or (CPUX86_HAS_SSEUNIT in cpu_capabilities[current_settings.cputype])) and
  2076. ((len=8) or (len=16) or (len=24) or (len=32) or (len=40) or (len=48)) then
  2077. cm:=copy_mm
  2078. else
  2079. {$endif dummy}
  2080. {$endif i8086}
  2081. if (cs_mmx in current_settings.localswitches) and
  2082. not(pi_uses_fpu in current_procinfo.flags) and
  2083. ((len=8) or (len=16) or (len=24) or (len=32)) then
  2084. cm:=copy_mmx;
  2085. if (len>helpsize) then
  2086. cm:=copy_string;
  2087. if (cs_opt_size in current_settings.optimizerswitches) and
  2088. not((len<=16) and (cm in [copy_mmx,copy_mm,copy_avx])) and
  2089. not(len in copy_len_sizes) then
  2090. cm:=copy_string;
  2091. {$ifndef i8086}
  2092. if (source.segment<>NR_NO) or
  2093. (dest.segment<>NR_NO) then
  2094. cm:=copy_string;
  2095. {$endif not i8086}
  2096. case cm of
  2097. copy_move:
  2098. begin
  2099. dstref:=dest;
  2100. srcref:=source;
  2101. copysize:=sizeof(aint);
  2102. cgsize:=int_cgsize(copysize);
  2103. while len<>0 do
  2104. begin
  2105. if len<2 then
  2106. begin
  2107. copysize:=1;
  2108. cgsize:=OS_8;
  2109. end
  2110. else if len<4 then
  2111. begin
  2112. copysize:=2;
  2113. cgsize:=OS_16;
  2114. end
  2115. {$if defined(cpu32bitalu) or defined(cpu64bitalu)}
  2116. else if len<8 then
  2117. begin
  2118. copysize:=4;
  2119. cgsize:=OS_32;
  2120. end
  2121. {$endif cpu32bitalu or cpu64bitalu}
  2122. {$ifdef cpu64bitalu}
  2123. else if len<16 then
  2124. begin
  2125. copysize:=8;
  2126. cgsize:=OS_64;
  2127. end
  2128. {$endif}
  2129. ;
  2130. dec(len,copysize);
  2131. r:=getintregister(list,cgsize);
  2132. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  2133. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  2134. inc(srcref.offset,copysize);
  2135. inc(dstref.offset,copysize);
  2136. end;
  2137. end;
  2138. copy_mmx:
  2139. begin
  2140. dstref:=dest;
  2141. srcref:=source;
  2142. r0:=getmmxregister(list);
  2143. r1:=NR_NO;
  2144. r2:=NR_NO;
  2145. r3:=NR_NO;
  2146. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  2147. if len>=16 then
  2148. begin
  2149. inc(srcref.offset,8);
  2150. r1:=getmmxregister(list);
  2151. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  2152. end;
  2153. if len>=24 then
  2154. begin
  2155. inc(srcref.offset,8);
  2156. r2:=getmmxregister(list);
  2157. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  2158. end;
  2159. if len>=32 then
  2160. begin
  2161. inc(srcref.offset,8);
  2162. r3:=getmmxregister(list);
  2163. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2164. end;
  2165. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  2166. if len>=16 then
  2167. begin
  2168. inc(dstref.offset,8);
  2169. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  2170. end;
  2171. if len>=24 then
  2172. begin
  2173. inc(dstref.offset,8);
  2174. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  2175. end;
  2176. if len>=32 then
  2177. begin
  2178. inc(dstref.offset,8);
  2179. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2180. end;
  2181. end;
  2182. copy_mm:
  2183. begin
  2184. dstref:=dest;
  2185. srcref:=source;
  2186. r0:=NR_NO;
  2187. r1:=NR_NO;
  2188. r2:=NR_NO;
  2189. r3:=NR_NO;
  2190. if len>=16 then
  2191. begin
  2192. r0:=getmmregister(list,OS_M128);
  2193. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r0,nil);
  2194. inc(srcref.offset,16);
  2195. end;
  2196. if len>=32 then
  2197. begin
  2198. r1:=getmmregister(list,OS_M128);
  2199. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r1,nil);
  2200. inc(srcref.offset,16);
  2201. end;
  2202. if len>=48 then
  2203. begin
  2204. r2:=getmmregister(list,OS_M128);
  2205. a_loadmm_ref_reg(list,OS_M128,OS_M128,srcref,r2,nil);
  2206. inc(srcref.offset,16);
  2207. end;
  2208. if (len=8) or (len=24) or (len=40) then
  2209. begin
  2210. r3:=getmmregister(list,OS_M64);
  2211. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  2212. end;
  2213. if len>=16 then
  2214. begin
  2215. a_loadmm_reg_ref(list,OS_M128,OS_M128,r0,dstref,nil);
  2216. inc(dstref.offset,16);
  2217. end;
  2218. if len>=32 then
  2219. begin
  2220. a_loadmm_reg_ref(list,OS_M128,OS_M128,r1,dstref,nil);
  2221. inc(dstref.offset,16);
  2222. end;
  2223. if len>=48 then
  2224. begin
  2225. a_loadmm_reg_ref(list,OS_M128,OS_M128,r2,dstref,nil);
  2226. inc(dstref.offset,16);
  2227. end;
  2228. if (len=8) or (len=24) or (len=40) then
  2229. begin
  2230. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  2231. end;
  2232. end;
  2233. copy_avx:
  2234. begin
  2235. dstref:=dest;
  2236. srcref:=source;
  2237. r0:=NR_NO;
  2238. r1:=NR_NO;
  2239. r2:=NR_NO;
  2240. r3:=NR_NO;
  2241. if len>=16 then
  2242. begin
  2243. r0:=getmmregister(list,OS_M128);
  2244. { we want to force the use of vmovups, so do not use a_loadmm_ref_reg }
  2245. tmpref:=srcref;
  2246. make_simple_ref(list,tmpref);
  2247. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,tmpref,r0));
  2248. inc(srcref.offset,16);
  2249. end;
  2250. if len>=32 then
  2251. begin
  2252. r1:=getmmregister(list,OS_M128);
  2253. tmpref:=srcref;
  2254. make_simple_ref(list,tmpref);
  2255. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,tmpref,r1));
  2256. inc(srcref.offset,16);
  2257. end;
  2258. if len>=48 then
  2259. begin
  2260. r2:=getmmregister(list,OS_M128);
  2261. tmpref:=srcref;
  2262. make_simple_ref(list,tmpref);
  2263. list.concat(taicpu.op_ref_reg(A_VMOVUPS,S_NO,tmpref,r2));
  2264. inc(srcref.offset,16);
  2265. end;
  2266. if (len=8) or (len=24) or (len=40) then
  2267. begin
  2268. r3:=getmmregister(list,OS_M64);
  2269. tmpref:=srcref;
  2270. make_simple_ref(list,tmpref);
  2271. list.concat(taicpu.op_ref_reg(A_VMOVSD,S_NO,tmpref,r3));
  2272. end;
  2273. if len>=16 then
  2274. begin
  2275. tmpref:=dstref;
  2276. make_simple_ref(list,tmpref);
  2277. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r0,tmpref));
  2278. inc(dstref.offset,16);
  2279. end;
  2280. if len>=32 then
  2281. begin
  2282. tmpref:=dstref;
  2283. make_simple_ref(list,tmpref);
  2284. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r1,tmpref));
  2285. inc(dstref.offset,16);
  2286. end;
  2287. if len>=48 then
  2288. begin
  2289. tmpref:=dstref;
  2290. make_simple_ref(list,tmpref);
  2291. list.concat(taicpu.op_reg_ref(A_VMOVUPS,S_NO,r2,tmpref));
  2292. inc(dstref.offset,16);
  2293. end;
  2294. if (len=8) or (len=24) or (len=40) then
  2295. begin
  2296. tmpref:=dstref;
  2297. make_simple_ref(list,tmpref);
  2298. list.concat(taicpu.op_reg_ref(A_VMOVSD,S_NO,r3,tmpref));
  2299. end;
  2300. end
  2301. else {copy_string, should be a good fallback in case of unhandled}
  2302. begin
  2303. getcpuregister(list,REGDI);
  2304. if (dest.segment=NR_NO) and
  2305. (segment_regs_equal(NR_SS,NR_DS) or ((dest.base<>NR_BP) and (dest.base<>NR_SP))) then
  2306. begin
  2307. a_loadaddr_ref_reg(list,dest,REGDI);
  2308. saved_es:=false;
  2309. {$ifdef volatile_es}
  2310. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2311. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2312. {$endif volatile_es}
  2313. end
  2314. else
  2315. begin
  2316. dstref:=dest;
  2317. dstref.segment:=NR_NO;
  2318. a_loadaddr_ref_reg(list,dstref,REGDI);
  2319. {$ifdef volatile_es}
  2320. saved_es:=false;
  2321. {$else volatile_es}
  2322. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_ES));
  2323. saved_es:=true;
  2324. {$endif volatile_es}
  2325. if dest.segment<>NR_NO then
  2326. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,dest.segment))
  2327. else if (dest.base=NR_BP) or (dest.base=NR_SP) then
  2328. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2329. else
  2330. internalerror(2014040401);
  2331. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2332. end;
  2333. getcpuregister(list,REGSI);
  2334. if (source.segment=NR_NO) and
  2335. (segment_regs_equal(NR_SS,NR_DS) or ((source.base<>NR_BP) and (source.base<>NR_SP))) then
  2336. begin
  2337. a_loadaddr_ref_reg(list,source,REGSI);
  2338. saved_ds:=false;
  2339. end
  2340. else
  2341. begin
  2342. srcref:=source;
  2343. srcref.segment:=NR_NO;
  2344. a_loadaddr_ref_reg(list,srcref,REGSI);
  2345. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_DS));
  2346. saved_ds:=true;
  2347. if source.segment<>NR_NO then
  2348. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,source.segment))
  2349. else if (source.base=NR_BP) or (source.base=NR_SP) then
  2350. list.concat(taicpu.op_reg(A_PUSH,push_segment_size,NR_SS))
  2351. else
  2352. internalerror(2014040402);
  2353. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2354. end;
  2355. getcpuregister(list,REGCX);
  2356. if ts_cld in current_settings.targetswitches then
  2357. list.concat(Taicpu.op_none(A_CLD,S_NO));
  2358. if (cs_opt_size in current_settings.optimizerswitches) and
  2359. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  2360. begin
  2361. a_load_const_reg(list,OS_INT,len,REGCX);
  2362. list.concat(Taicpu.op_none(A_REP,S_NO));
  2363. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2364. end
  2365. else
  2366. begin
  2367. helpsize:=len div sizeof(aint);
  2368. len:=len mod sizeof(aint);
  2369. if helpsize>1 then
  2370. begin
  2371. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  2372. list.concat(Taicpu.op_none(A_REP,S_NO));
  2373. end;
  2374. if helpsize>0 then
  2375. begin
  2376. {$if defined(cpu64bitalu)}
  2377. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  2378. {$elseif defined(cpu32bitalu)}
  2379. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2380. {$elseif defined(cpu16bitalu)}
  2381. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2382. {$endif}
  2383. end;
  2384. if len>=4 then
  2385. begin
  2386. dec(len,4);
  2387. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  2388. end;
  2389. if len>=2 then
  2390. begin
  2391. dec(len,2);
  2392. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  2393. end;
  2394. if len=1 then
  2395. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  2396. end;
  2397. ungetcpuregister(list,REGCX);
  2398. ungetcpuregister(list,REGSI);
  2399. ungetcpuregister(list,REGDI);
  2400. if saved_ds then
  2401. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_DS));
  2402. if saved_es then
  2403. list.concat(taicpu.op_reg(A_POP,push_segment_size,NR_ES));
  2404. end;
  2405. end;
  2406. end;
  2407. {****************************************************************************
  2408. Entry/Exit Code Helpers
  2409. ****************************************************************************}
  2410. procedure tcgx86.g_profilecode(list : TAsmList);
  2411. var
  2412. pl : tasmlabel;
  2413. mcountprefix : String[4];
  2414. begin
  2415. case target_info.system of
  2416. {$ifndef NOTARGETWIN}
  2417. system_i386_win32,
  2418. {$endif}
  2419. system_i386_freebsd,
  2420. system_i386_netbsd,
  2421. // system_i386_openbsd,
  2422. system_i386_wdosx :
  2423. begin
  2424. Case target_info.system Of
  2425. system_i386_freebsd : mcountprefix:='.';
  2426. system_i386_netbsd : mcountprefix:='__';
  2427. // system_i386_openbsd : mcountprefix:='.';
  2428. else
  2429. mcountPrefix:='';
  2430. end;
  2431. current_asmdata.getaddrlabel(pl);
  2432. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(pint));
  2433. list.concat(Tai_label.Create(pl));
  2434. list.concat(Tai_const.Create_32bit(0));
  2435. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  2436. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2437. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  2438. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount',false);
  2439. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  2440. end;
  2441. system_i386_linux:
  2442. a_call_name(list,target_info.Cprefix+'mcount',false);
  2443. system_i386_go32v2,system_i386_watcom:
  2444. begin
  2445. a_call_name(list,'MCOUNT',false);
  2446. end;
  2447. system_x86_64_linux,
  2448. system_x86_64_darwin:
  2449. begin
  2450. a_call_name(list,'mcount',false);
  2451. end;
  2452. end;
  2453. end;
  2454. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  2455. procedure decrease_sp(a : tcgint);
  2456. var
  2457. href : treference;
  2458. begin
  2459. reference_reset_base(href,NR_STACK_POINTER_REG,-a,0);
  2460. { normally, lea is a better choice than a sub to adjust the stack pointer }
  2461. list.concat(Taicpu.op_ref_reg(A_LEA,TCGSize2OpSize[OS_ADDR],href,NR_STACK_POINTER_REG));
  2462. end;
  2463. {$ifdef x86}
  2464. {$ifndef NOTARGETWIN}
  2465. var
  2466. href : treference;
  2467. i : integer;
  2468. again : tasmlabel;
  2469. {$endif NOTARGETWIN}
  2470. {$endif x86}
  2471. begin
  2472. if localsize>0 then
  2473. begin
  2474. {$ifdef i386}
  2475. {$ifndef NOTARGETWIN}
  2476. { windows guards only a few pages for stack growing,
  2477. so we have to access every page first }
  2478. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  2479. (localsize>=winstackpagesize) then
  2480. begin
  2481. if localsize div winstackpagesize<=5 then
  2482. begin
  2483. decrease_sp(localsize-4);
  2484. for i:=1 to localsize div winstackpagesize do
  2485. begin
  2486. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize,4);
  2487. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2488. end;
  2489. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2490. end
  2491. else
  2492. begin
  2493. current_asmdata.getjumplabel(again);
  2494. { Using a_reg_alloc instead of getcpuregister, so this procedure
  2495. does not change "used_in_proc" state of EDI and therefore can be
  2496. called after saving registers with "push" instruction
  2497. without creating an unbalanced "pop edi" in epilogue }
  2498. a_reg_alloc(list,NR_EDI);
  2499. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  2500. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  2501. a_label(list,again);
  2502. decrease_sp(winstackpagesize-4);
  2503. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  2504. if UseIncDec then
  2505. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI))
  2506. else
  2507. list.concat(Taicpu.op_const_reg(A_SUB,S_L,1,NR_EDI));
  2508. a_jmp_cond(list,OC_NE,again);
  2509. decrease_sp(localsize mod winstackpagesize-4);
  2510. reference_reset_base(href,NR_ESP,localsize-4,4);
  2511. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  2512. a_reg_dealloc(list,NR_EDI);
  2513. end
  2514. end
  2515. else
  2516. {$endif NOTARGETWIN}
  2517. {$endif i386}
  2518. {$ifdef x86_64}
  2519. {$ifndef NOTARGETWIN}
  2520. { windows guards only a few pages for stack growing,
  2521. so we have to access every page first }
  2522. if (target_info.system=system_x86_64_win64) and
  2523. (localsize>=winstackpagesize) then
  2524. begin
  2525. if localsize div winstackpagesize<=5 then
  2526. begin
  2527. decrease_sp(localsize);
  2528. for i:=1 to localsize div winstackpagesize do
  2529. begin
  2530. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4,4);
  2531. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2532. end;
  2533. reference_reset_base(href,NR_RSP,0,4);
  2534. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2535. end
  2536. else
  2537. begin
  2538. current_asmdata.getjumplabel(again);
  2539. getcpuregister(list,NR_R10);
  2540. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  2541. a_label(list,again);
  2542. decrease_sp(winstackpagesize);
  2543. reference_reset_base(href,NR_RSP,0,4);
  2544. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  2545. if UseIncDec then
  2546. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10))
  2547. else
  2548. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,1,NR_R10));
  2549. a_jmp_cond(list,OC_NE,again);
  2550. decrease_sp(localsize mod winstackpagesize);
  2551. ungetcpuregister(list,NR_R10);
  2552. end
  2553. end
  2554. else
  2555. {$endif NOTARGETWIN}
  2556. {$endif x86_64}
  2557. decrease_sp(localsize);
  2558. end;
  2559. end;
  2560. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  2561. var
  2562. stackmisalignment: longint;
  2563. regsize: longint;
  2564. {$ifdef i8086}
  2565. dgroup: treference;
  2566. {$endif i8086}
  2567. procedure push_regs;
  2568. var
  2569. r: longint;
  2570. begin
  2571. regsize:=0;
  2572. for r := low(saved_standard_registers) to high(saved_standard_registers) do
  2573. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2574. begin
  2575. inc(regsize,sizeof(aint));
  2576. list.concat(Taicpu.Op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE)));
  2577. end;
  2578. end;
  2579. begin
  2580. {$ifdef i8086}
  2581. { interrupt support for i8086 }
  2582. if po_interrupt in current_procinfo.procdef.procoptions then
  2583. begin
  2584. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_AX));
  2585. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_BX));
  2586. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CX));
  2587. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DX));
  2588. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_SI));
  2589. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DI));
  2590. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  2591. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  2592. if current_settings.x86memorymodel=mm_tiny then
  2593. begin
  2594. { in the tiny memory model, we can't use dgroup, because that
  2595. adds a relocation entry to the .exe and we can't produce a
  2596. .com file (because they don't support relactions), so instead
  2597. we initialize DS from CS. }
  2598. if cs_opt_size in current_settings.optimizerswitches then
  2599. begin
  2600. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_CS));
  2601. list.concat(Taicpu.Op_reg(A_POP,S_W,NR_DS));
  2602. end
  2603. else
  2604. begin
  2605. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_CS,NR_AX));
  2606. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2607. end;
  2608. end
  2609. else
  2610. begin
  2611. reference_reset(dgroup,0);
  2612. dgroup.refaddr:=addr_dgroup;
  2613. list.concat(Taicpu.Op_ref_reg(A_MOV,S_W,dgroup,NR_AX));
  2614. list.concat(Taicpu.Op_reg_reg(A_MOV,S_W,NR_AX,NR_DS));
  2615. end;
  2616. end;
  2617. {$endif i8086}
  2618. {$ifdef i386}
  2619. { interrupt support for i386 }
  2620. if (po_interrupt in current_procinfo.procdef.procoptions) and
  2621. { this messes up stack alignment }
  2622. not(target_info.system in [system_i386_darwin,system_i386_iphonesim,system_i386_android]) then
  2623. begin
  2624. { .... also the segment registers }
  2625. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  2626. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  2627. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  2628. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  2629. { save the registers of an interrupt procedure }
  2630. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  2631. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  2632. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  2633. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  2634. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  2635. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  2636. end;
  2637. {$endif i386}
  2638. { save old framepointer }
  2639. if not nostackframe then
  2640. begin
  2641. { return address }
  2642. stackmisalignment := sizeof(pint);
  2643. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  2644. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  2645. begin
  2646. {$ifdef i386}
  2647. if (not paramanager.use_fixed_stack) then
  2648. push_regs;
  2649. {$endif i386}
  2650. CGmessage(cg_d_stackframe_omited);
  2651. end
  2652. else
  2653. begin
  2654. { push <frame_pointer> }
  2655. inc(stackmisalignment,sizeof(pint));
  2656. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  2657. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  2658. { Return address and FP are both on stack }
  2659. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(pint));
  2660. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(pint)));
  2661. if current_procinfo.procdef.proctypeoption<>potype_exceptfilter then
  2662. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG))
  2663. else
  2664. begin
  2665. push_regs;
  2666. gen_load_frame_for_exceptfilter(list);
  2667. { Need only as much stack space as necessary to do the calls.
  2668. Exception filters don't have own local vars, and temps are 'mapped'
  2669. to the parent procedure.
  2670. maxpushedparasize is already aligned at least on x86_64. }
  2671. localsize:=current_procinfo.maxpushedparasize;
  2672. end;
  2673. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  2674. end;
  2675. { allocate stackframe space }
  2676. if (localsize<>0) or
  2677. ((target_info.stackalign>sizeof(pint)) and
  2678. (stackmisalignment <> 0) and
  2679. ((pi_do_call in current_procinfo.flags) or
  2680. (po_assembler in current_procinfo.procdef.procoptions))) then
  2681. begin
  2682. if target_info.stackalign>sizeof(pint) then
  2683. localsize := align(localsize+stackmisalignment,target_info.stackalign)-stackmisalignment;
  2684. g_stackpointer_alloc(list,localsize);
  2685. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  2686. current_asmdata.asmcfi.cfa_def_cfa_offset(list,localsize+sizeof(pint));
  2687. current_procinfo.final_localsize:=localsize;
  2688. end;
  2689. {$ifdef i386}
  2690. if (not paramanager.use_fixed_stack) and
  2691. (current_procinfo.framepointer<>NR_STACK_POINTER_REG) and
  2692. (current_procinfo.procdef.proctypeoption<>potype_exceptfilter) then
  2693. begin
  2694. regsize:=0;
  2695. push_regs;
  2696. reference_reset_base(current_procinfo.save_regs_ref,
  2697. current_procinfo.framepointer,
  2698. -(localsize+regsize),sizeof(aint));
  2699. end;
  2700. {$endif i386}
  2701. end;
  2702. end;
  2703. procedure tcgx86.g_save_registers(list: TAsmList);
  2704. begin
  2705. {$ifdef i386}
  2706. if paramanager.use_fixed_stack then
  2707. {$endif i386}
  2708. inherited g_save_registers(list);
  2709. end;
  2710. procedure tcgx86.g_restore_registers(list: TAsmList);
  2711. begin
  2712. {$ifdef i386}
  2713. if paramanager.use_fixed_stack then
  2714. {$endif i386}
  2715. inherited g_restore_registers(list);
  2716. end;
  2717. procedure tcgx86.internal_restore_regs(list: TAsmList; use_pop: boolean);
  2718. var
  2719. r: longint;
  2720. hreg: tregister;
  2721. href: treference;
  2722. begin
  2723. href:=current_procinfo.save_regs_ref;
  2724. for r:=high(saved_standard_registers) downto low(saved_standard_registers) do
  2725. if saved_standard_registers[r] in rg[R_INTREGISTER].used_in_proc then
  2726. begin
  2727. hreg:=newreg(R_INTREGISTER,saved_standard_registers[r],R_SUBWHOLE);
  2728. { Allocate register so the optimizer does not remove the load }
  2729. a_reg_alloc(list,hreg);
  2730. if use_pop then
  2731. list.concat(Taicpu.Op_reg(A_POP,tcgsize2opsize[OS_ADDR],hreg))
  2732. else
  2733. begin
  2734. a_load_ref_reg(list,OS_ADDR,OS_ADDR,href,hreg);
  2735. inc(href.offset,sizeof(aint));
  2736. end;
  2737. end;
  2738. end;
  2739. { produces if necessary overflowcode }
  2740. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  2741. var
  2742. hl : tasmlabel;
  2743. ai : taicpu;
  2744. cond : TAsmCond;
  2745. begin
  2746. if not(cs_check_overflow in current_settings.localswitches) then
  2747. exit;
  2748. current_asmdata.getjumplabel(hl);
  2749. if not ((def.typ=pointerdef) or
  2750. ((def.typ=orddef) and
  2751. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  2752. pasbool8,pasbool16,pasbool32,pasbool64]))) then
  2753. cond:=C_NO
  2754. else
  2755. cond:=C_NB;
  2756. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  2757. ai.SetCondition(cond);
  2758. ai.is_jmp:=true;
  2759. list.concat(ai);
  2760. a_call_name(list,'FPC_OVERFLOW',false);
  2761. a_label(list,hl);
  2762. end;
  2763. procedure tcgx86.g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string);
  2764. var
  2765. ref : treference;
  2766. sym : tasmsymbol;
  2767. begin
  2768. if (target_info.system = system_i386_darwin) then
  2769. begin
  2770. { a_jmp_name jumps to a stub which is always pic-safe on darwin }
  2771. inherited g_external_wrapper(list,procdef,externalname);
  2772. exit;
  2773. end;
  2774. sym:=current_asmdata.RefAsmSymbol(externalname);
  2775. reference_reset_symbol(ref,sym,0,sizeof(pint));
  2776. { create pic'ed? }
  2777. if (cs_create_pic in current_settings.moduleswitches) and
  2778. { darwin/x86_64's assembler doesn't want @PLT after call symbols }
  2779. not(target_info.system in [system_x86_64_darwin,system_i386_iphonesim]) then
  2780. ref.refaddr:=addr_pic
  2781. else
  2782. ref.refaddr:=addr_full;
  2783. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  2784. end;
  2785. end.