cgx86.pas 94 KB

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