cgx86.pas 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102
  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 = class(tcg)
  30. rgfpu : Trgx86fpu;
  31. procedure done_register_allocators;override;
  32. function getfpuregister(list:TAsmList;size:Tcgsize):Tregister;override;
  33. function getmmxregister(list:TAsmList):Tregister;
  34. function getmmregister(list:TAsmList;size:Tcgsize):Tregister;override;
  35. procedure getcpuregister(list:TAsmList;r:Tregister);override;
  36. procedure ungetcpuregister(list:TAsmList;r:Tregister);override;
  37. procedure alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  38. procedure dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);override;
  39. function uses_registers(rt:Tregistertype):boolean;override;
  40. procedure add_reg_instruction(instr:Tai;r:tregister);override;
  41. procedure dec_fpu_stack;
  42. procedure inc_fpu_stack;
  43. procedure a_call_name(list : TAsmList;const s : string);override;
  44. procedure a_call_reg(list : TAsmList;reg : tregister);override;
  45. procedure a_call_ref(list : TAsmList;ref : treference);override;
  46. procedure a_call_name_static(list : TAsmList;const s : string);override;
  47. procedure a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister); override;
  48. procedure a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference); override;
  49. procedure a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister); override;
  50. procedure a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister); override;
  51. procedure a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference); override;
  52. { move instructions }
  53. procedure a_load_const_reg(list : TAsmList; tosize: tcgsize; a : aint;reg : tregister);override;
  54. procedure a_load_const_ref(list : TAsmList; tosize: tcgsize; a : aint;const ref : treference);override;
  55. procedure a_load_reg_ref(list : TAsmList;fromsize,tosize: tcgsize; reg : tregister;const ref : treference);override;
  56. procedure a_load_ref_reg(list : TAsmList;fromsize,tosize: tcgsize;const ref : treference;reg : tregister);override;
  57. procedure a_load_reg_reg(list : TAsmList;fromsize,tosize: tcgsize;reg1,reg2 : tregister);override;
  58. procedure a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);override;
  59. { fpu move instructions }
  60. procedure a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister); override;
  61. procedure a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister); override;
  62. procedure a_loadfpu_reg_ref(list: TAsmList; fromsize, tosize: tcgsize; reg: tregister; const ref: treference); override;
  63. { vector register move instructions }
  64. procedure a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle); override;
  65. procedure a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  66. procedure a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle); override;
  67. procedure a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle); override;
  68. procedure a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);override;
  69. { comparison operations }
  70. procedure a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  71. l : tasmlabel);override;
  72. procedure a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  73. l : tasmlabel);override;
  74. procedure a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg1,reg2 : tregister;l : tasmlabel); override;
  75. procedure a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister; l : tasmlabel); override;
  76. procedure a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister; const ref: treference; l : tasmlabel); override;
  77. procedure a_jmp_name(list : TAsmList;const s : string);override;
  78. procedure a_jmp_always(list : TAsmList;l: tasmlabel); override;
  79. procedure a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel); override;
  80. procedure g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister); override;
  81. procedure g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference); override;
  82. procedure g_concatcopy(list : TAsmList;const source,dest : treference;len : aint);override;
  83. { entry/exit code helpers }
  84. procedure g_profilecode(list : TAsmList);override;
  85. procedure g_stackpointer_alloc(list : TAsmList;localsize : longint);override;
  86. procedure g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);override;
  87. procedure g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);override;
  88. procedure g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string); override;
  89. procedure make_simple_ref(list:TAsmList;var ref: treference);
  90. protected
  91. procedure a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  92. procedure check_register_size(size:tcgsize;reg:tregister);
  93. procedure opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  94. function get_darwin_call_stub(const s: string): tasmsymbol;
  95. private
  96. procedure sizes2load(s1,s2 : tcgsize;var op: tasmop; var s3: topsize);
  97. procedure floatload(list: TAsmList; t : tcgsize;const ref : treference);
  98. procedure floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  99. procedure floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  100. procedure floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  101. end;
  102. const
  103. {$ifdef x86_64}
  104. TCGSize2OpSize: Array[tcgsize] of topsize =
  105. (S_NO,S_B,S_W,S_L,S_Q,S_T,S_B,S_W,S_L,S_Q,S_Q,
  106. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  107. S_NO,S_NO,S_NO,S_MD,S_T,
  108. S_NO,S_NO,S_NO,S_NO,S_T);
  109. {$else x86_64}
  110. TCGSize2OpSize: Array[tcgsize] of topsize =
  111. (S_NO,S_B,S_W,S_L,S_L,S_T,S_B,S_W,S_L,S_L,S_L,
  112. S_FS,S_FL,S_FX,S_IQ,S_FXX,
  113. S_NO,S_NO,S_NO,S_MD,S_T,
  114. S_NO,S_NO,S_NO,S_NO,S_T);
  115. {$endif x86_64}
  116. {$ifndef NOTARGETWIN}
  117. winstackpagesize = 4096;
  118. {$endif NOTARGETWIN}
  119. implementation
  120. uses
  121. globals,verbose,systems,cutils,
  122. defutil,paramgr,procinfo,
  123. tgobj,ncgutil,
  124. fmodule;
  125. const
  126. TOpCG2AsmOp: Array[topcg] of TAsmOp = (A_NONE,A_MOV,A_ADD,A_AND,A_DIV,
  127. A_IDIV,A_IMUL,A_MUL,A_NEG,A_NOT,A_OR,
  128. A_SAR,A_SHL,A_SHR,A_SUB,A_XOR);
  129. TOpCmp2AsmCond: Array[topcmp] of TAsmCond = (C_NONE,
  130. C_E,C_G,C_L,C_GE,C_LE,C_NE,C_BE,C_B,C_AE,C_A);
  131. procedure Tcgx86.done_register_allocators;
  132. begin
  133. rg[R_INTREGISTER].free;
  134. rg[R_MMREGISTER].free;
  135. rg[R_MMXREGISTER].free;
  136. rgfpu.free;
  137. inherited done_register_allocators;
  138. end;
  139. function Tcgx86.getfpuregister(list:TAsmList;size:Tcgsize):Tregister;
  140. begin
  141. result:=rgfpu.getregisterfpu(list);
  142. end;
  143. function Tcgx86.getmmxregister(list:TAsmList):Tregister;
  144. begin
  145. if not assigned(rg[R_MMXREGISTER]) then
  146. internalerror(2003121214);
  147. result:=rg[R_MMXREGISTER].getregister(list,R_SUBNONE);
  148. end;
  149. function Tcgx86.getmmregister(list:TAsmList;size:Tcgsize):Tregister;
  150. begin
  151. if not assigned(rg[R_MMREGISTER]) then
  152. internalerror(2003121234);
  153. case size of
  154. OS_F64:
  155. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMD);
  156. OS_F32:
  157. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMS);
  158. OS_M128:
  159. result:=rg[R_MMREGISTER].getregister(list,R_SUBMMWHOLE);
  160. else
  161. internalerror(200506041);
  162. end;
  163. end;
  164. procedure Tcgx86.getcpuregister(list:TAsmList;r:Tregister);
  165. begin
  166. if getregtype(r)=R_FPUREGISTER then
  167. internalerror(2003121210)
  168. else
  169. inherited getcpuregister(list,r);
  170. end;
  171. procedure tcgx86.ungetcpuregister(list:TAsmList;r:Tregister);
  172. begin
  173. if getregtype(r)=R_FPUREGISTER then
  174. rgfpu.ungetregisterfpu(list,r)
  175. else
  176. inherited ungetcpuregister(list,r);
  177. end;
  178. procedure Tcgx86.alloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  179. begin
  180. if rt<>R_FPUREGISTER then
  181. inherited alloccpuregisters(list,rt,r);
  182. end;
  183. procedure Tcgx86.dealloccpuregisters(list:TAsmList;rt:Tregistertype;const r:Tcpuregisterset);
  184. begin
  185. if rt<>R_FPUREGISTER then
  186. inherited dealloccpuregisters(list,rt,r);
  187. end;
  188. function Tcgx86.uses_registers(rt:Tregistertype):boolean;
  189. begin
  190. if rt=R_FPUREGISTER then
  191. result:=false
  192. else
  193. result:=inherited uses_registers(rt);
  194. end;
  195. procedure tcgx86.add_reg_instruction(instr:Tai;r:tregister);
  196. begin
  197. if getregtype(r)<>R_FPUREGISTER then
  198. inherited add_reg_instruction(instr,r);
  199. end;
  200. procedure tcgx86.dec_fpu_stack;
  201. begin
  202. if rgfpu.fpuvaroffset<=0 then
  203. internalerror(200604201);
  204. dec(rgfpu.fpuvaroffset);
  205. end;
  206. procedure tcgx86.inc_fpu_stack;
  207. begin
  208. inc(rgfpu.fpuvaroffset);
  209. end;
  210. {****************************************************************************
  211. This is private property, keep out! :)
  212. ****************************************************************************}
  213. procedure tcgx86.sizes2load(s1,s2 : tcgsize; var op: tasmop; var s3: topsize);
  214. begin
  215. { ensure to have always valid sizes }
  216. if s1=OS_NO then
  217. s1:=s2;
  218. if s2=OS_NO then
  219. s2:=s1;
  220. case s2 of
  221. OS_8,OS_S8 :
  222. if S1 in [OS_8,OS_S8] then
  223. s3 := S_B
  224. else
  225. internalerror(200109221);
  226. OS_16,OS_S16:
  227. case s1 of
  228. OS_8,OS_S8:
  229. s3 := S_BW;
  230. OS_16,OS_S16:
  231. s3 := S_W;
  232. else
  233. internalerror(200109222);
  234. end;
  235. OS_32,OS_S32:
  236. case s1 of
  237. OS_8,OS_S8:
  238. s3 := S_BL;
  239. OS_16,OS_S16:
  240. s3 := S_WL;
  241. OS_32,OS_S32:
  242. s3 := S_L;
  243. else
  244. internalerror(200109223);
  245. end;
  246. {$ifdef x86_64}
  247. OS_64,OS_S64:
  248. case s1 of
  249. OS_8:
  250. s3 := S_BL;
  251. OS_S8:
  252. s3 := S_BQ;
  253. OS_16:
  254. s3 := S_WL;
  255. OS_S16:
  256. s3 := S_WQ;
  257. OS_32:
  258. s3 := S_L;
  259. OS_S32:
  260. s3 := S_LQ;
  261. OS_64,OS_S64:
  262. s3 := S_Q;
  263. else
  264. internalerror(200304302);
  265. end;
  266. {$endif x86_64}
  267. else
  268. internalerror(200109227);
  269. end;
  270. if s3 in [S_B,S_W,S_L,S_Q] then
  271. op := A_MOV
  272. else if s1 in [OS_8,OS_16,OS_32,OS_64] then
  273. op := A_MOVZX
  274. else
  275. {$ifdef x86_64}
  276. if s3 in [S_LQ] then
  277. op := A_MOVSXD
  278. else
  279. {$endif x86_64}
  280. op := A_MOVSX;
  281. end;
  282. procedure tcgx86.make_simple_ref(list:TAsmList;var ref: treference);
  283. var
  284. hreg : tregister;
  285. href : treference;
  286. {$ifndef x86_64}
  287. add_hreg: boolean;
  288. {$endif not x86_64}
  289. begin
  290. {$ifdef x86_64}
  291. { Only 32bit is allowed }
  292. if ((ref.offset<low(longint)) or (ref.offset>high(longint))) then
  293. begin
  294. { Load constant value to register }
  295. hreg:=GetAddressRegister(list);
  296. list.concat(taicpu.op_const_reg(A_MOV,S_Q,ref.offset,hreg));
  297. ref.offset:=0;
  298. {if assigned(ref.symbol) then
  299. begin
  300. list.concat(taicpu.op_sym_ofs_reg(A_ADD,S_Q,ref.symbol,0,hreg));
  301. ref.symbol:=nil;
  302. end;}
  303. { Add register to reference }
  304. if ref.index=NR_NO then
  305. ref.index:=hreg
  306. else
  307. begin
  308. if ref.scalefactor<>0 then
  309. begin
  310. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.base,hreg));
  311. ref.base:=hreg;
  312. end
  313. else
  314. begin
  315. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.index,hreg));
  316. ref.index:=hreg;
  317. end;
  318. end;
  319. end;
  320. if (cs_create_pic in current_settings.moduleswitches) and
  321. assigned(ref.symbol) and not((ref.symbol.bind=AB_LOCAL) and (ref.symbol.typ in [AT_LABEL,AT_FUNCTION])) then
  322. begin
  323. reference_reset_symbol(href,ref.symbol,0);
  324. hreg:=getaddressregister(list);
  325. href.refaddr:=addr_pic;
  326. href.base:=NR_RIP;
  327. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,href,hreg));
  328. ref.symbol:=nil;
  329. if ref.base=NR_NO then
  330. ref.base:=hreg
  331. else if ref.index=NR_NO then
  332. begin
  333. ref.index:=hreg;
  334. ref.scalefactor:=1;
  335. end
  336. else
  337. begin
  338. list.concat(taicpu.op_reg_reg(A_ADD,S_Q,ref.base,hreg));
  339. ref.base:=hreg;
  340. end;
  341. end;
  342. {$else x86_64}
  343. add_hreg:=false;
  344. if (target_info.system=system_i386_darwin) then
  345. begin
  346. if assigned(ref.symbol) and
  347. not(assigned(ref.relsymbol)) and
  348. ((ref.symbol.bind = AB_EXTERNAL) or
  349. (cs_create_pic in current_settings.moduleswitches)) then
  350. begin
  351. if (ref.symbol.bind = AB_EXTERNAL) or
  352. ((cs_create_pic in current_settings.moduleswitches) and
  353. (ref.symbol.bind in [AB_COMMON,AB_GLOBAL])) then
  354. begin
  355. hreg:=g_indirect_sym_load(list,ref.symbol.name);
  356. ref.symbol:=nil;
  357. end
  358. else
  359. begin
  360. include(current_procinfo.flags,pi_needs_got);
  361. hreg:=current_procinfo.got;
  362. ref.relsymbol:=current_procinfo.CurrGOTLabel;
  363. ref.refaddr:=addr_pic;
  364. end;
  365. add_hreg:=true
  366. end
  367. end
  368. else if (cs_create_pic in current_settings.moduleswitches) and
  369. assigned(ref.symbol) and
  370. not((ref.symbol.bind=AB_LOCAL) and
  371. (ref.symbol.typ in [AT_LABEL,AT_FUNCTION])) then
  372. begin
  373. reference_reset_symbol(href,ref.symbol,0);
  374. href.base:=current_procinfo.got;
  375. href.refaddr:=addr_pic;
  376. include(current_procinfo.flags,pi_needs_got);
  377. hreg:=cg.getaddressregister(list);
  378. list.concat(taicpu.op_ref_reg(A_MOV,S_L,href,hreg));
  379. ref.symbol:=nil;
  380. add_hreg:=true;
  381. end;
  382. if add_hreg then
  383. begin
  384. if ref.base=NR_NO then
  385. ref.base:=hreg
  386. else if ref.index=NR_NO then
  387. begin
  388. ref.index:=hreg;
  389. ref.scalefactor:=1;
  390. end
  391. else
  392. begin
  393. list.concat(taicpu.op_reg_reg(A_ADD,S_L,ref.base,hreg));
  394. ref.base:=hreg;
  395. end;
  396. end;
  397. {$endif x86_64}
  398. end;
  399. procedure tcgx86.floatloadops(t : tcgsize;var op : tasmop;var s : topsize);
  400. begin
  401. case t of
  402. OS_F32 :
  403. begin
  404. op:=A_FLD;
  405. s:=S_FS;
  406. end;
  407. OS_F64 :
  408. begin
  409. op:=A_FLD;
  410. s:=S_FL;
  411. end;
  412. OS_F80 :
  413. begin
  414. op:=A_FLD;
  415. s:=S_FX;
  416. end;
  417. OS_C64 :
  418. begin
  419. op:=A_FILD;
  420. s:=S_IQ;
  421. end;
  422. else
  423. internalerror(200204043);
  424. end;
  425. end;
  426. procedure tcgx86.floatload(list: TAsmList; t : tcgsize;const ref : treference);
  427. var
  428. op : tasmop;
  429. s : topsize;
  430. tmpref : treference;
  431. begin
  432. tmpref:=ref;
  433. make_simple_ref(list,tmpref);
  434. floatloadops(t,op,s);
  435. list.concat(Taicpu.Op_ref(op,s,tmpref));
  436. inc_fpu_stack;
  437. end;
  438. procedure tcgx86.floatstoreops(t : tcgsize;var op : tasmop;var s : topsize);
  439. begin
  440. case t of
  441. OS_F32 :
  442. begin
  443. op:=A_FSTP;
  444. s:=S_FS;
  445. end;
  446. OS_F64 :
  447. begin
  448. op:=A_FSTP;
  449. s:=S_FL;
  450. end;
  451. OS_F80 :
  452. begin
  453. op:=A_FSTP;
  454. s:=S_FX;
  455. end;
  456. OS_C64 :
  457. begin
  458. op:=A_FISTP;
  459. s:=S_IQ;
  460. end;
  461. else
  462. internalerror(200204042);
  463. end;
  464. end;
  465. procedure tcgx86.floatstore(list: TAsmList; t : tcgsize;const ref : treference);
  466. var
  467. op : tasmop;
  468. s : topsize;
  469. tmpref : treference;
  470. begin
  471. tmpref:=ref;
  472. make_simple_ref(list,tmpref);
  473. floatstoreops(t,op,s);
  474. list.concat(Taicpu.Op_ref(op,s,tmpref));
  475. { storing non extended floats can cause a floating point overflow }
  476. if (t<>OS_F80) and
  477. (cs_fpu_fwait in current_settings.localswitches) then
  478. list.concat(Taicpu.Op_none(A_FWAIT,S_NO));
  479. dec_fpu_stack;
  480. end;
  481. procedure tcgx86.check_register_size(size:tcgsize;reg:tregister);
  482. begin
  483. if TCGSize2OpSize[size]<>TCGSize2OpSize[reg_cgsize(reg)] then
  484. internalerror(200306031);
  485. end;
  486. {****************************************************************************
  487. Assembler code
  488. ****************************************************************************}
  489. procedure tcgx86.a_jmp_name(list : TAsmList;const s : string);
  490. var
  491. r: treference;
  492. begin
  493. if (target_info.system<>system_i386_darwin) then
  494. list.concat(taicpu.op_sym(A_JMP,S_NO,current_asmdata.RefAsmSymbol(s)))
  495. else
  496. begin
  497. reference_reset_symbol(r,get_darwin_call_stub(s),0);
  498. r.refaddr:=addr_full;
  499. list.concat(taicpu.op_ref(A_JMP,S_NO,r));
  500. end;
  501. end;
  502. procedure tcgx86.a_jmp_always(list : TAsmList;l: tasmlabel);
  503. begin
  504. a_jmp_cond(list, OC_NONE, l);
  505. end;
  506. function tcgx86.get_darwin_call_stub(const s: string): tasmsymbol;
  507. var
  508. stubname: string;
  509. begin
  510. stubname := 'L'+s+'$stub';
  511. result := current_asmdata.getasmsymbol(stubname);
  512. if assigned(result) then
  513. exit;
  514. if current_asmdata.asmlists[al_imports]=nil then
  515. current_asmdata.asmlists[al_imports]:=TAsmList.create;
  516. current_asmdata.asmlists[al_imports].concat(Tai_section.create(sec_stub,'',0));
  517. result := current_asmdata.RefAsmSymbol(stubname);
  518. current_asmdata.asmlists[al_imports].concat(Tai_symbol.Create(result,0));
  519. current_asmdata.asmlists[al_imports].concat(tai_directive.create(asd_indirect_symbol,s));
  520. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  521. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  522. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  523. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  524. current_asmdata.asmlists[al_imports].concat(taicpu.op_none(A_HLT));
  525. end;
  526. procedure tcgx86.a_call_name(list : TAsmList;const s : string);
  527. var
  528. sym : tasmsymbol;
  529. r : treference;
  530. begin
  531. if (target_info.system <> system_i386_darwin) then
  532. begin
  533. sym:=current_asmdata.RefAsmSymbol(s);
  534. reference_reset_symbol(r,sym,0);
  535. if (cs_create_pic in current_settings.moduleswitches) and
  536. { darwin/x86_64's assembler doesn't want @PLT after call symbols }
  537. (target_info.system<>system_x86_64_darwin) then
  538. begin
  539. {$ifdef i386}
  540. include(current_procinfo.flags,pi_needs_got);
  541. {$endif i386}
  542. r.refaddr:=addr_pic
  543. end
  544. else
  545. r.refaddr:=addr_full;
  546. end
  547. else
  548. begin
  549. reference_reset_symbol(r,get_darwin_call_stub(s),0);
  550. r.refaddr:=addr_full;
  551. end;
  552. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  553. end;
  554. procedure tcgx86.a_call_name_static(list : TAsmList;const s : string);
  555. var
  556. sym : tasmsymbol;
  557. r : treference;
  558. begin
  559. sym:=current_asmdata.RefAsmSymbol(s);
  560. reference_reset_symbol(r,sym,0);
  561. r.refaddr:=addr_full;
  562. list.concat(taicpu.op_ref(A_CALL,S_NO,r));
  563. end;
  564. procedure tcgx86.a_call_reg(list : TAsmList;reg : tregister);
  565. begin
  566. list.concat(taicpu.op_reg(A_CALL,S_NO,reg));
  567. end;
  568. procedure tcgx86.a_call_ref(list : TAsmList;ref : treference);
  569. begin
  570. list.concat(taicpu.op_ref(A_CALL,S_NO,ref));
  571. end;
  572. {********************** load instructions ********************}
  573. procedure tcgx86.a_load_const_reg(list : TAsmList; tosize: TCGSize; a : aint; reg : TRegister);
  574. begin
  575. check_register_size(tosize,reg);
  576. { the optimizer will change it to "xor reg,reg" when loading zero, }
  577. { no need to do it here too (JM) }
  578. list.concat(taicpu.op_const_reg(A_MOV,TCGSize2OpSize[tosize],a,reg))
  579. end;
  580. procedure tcgx86.a_load_const_ref(list : TAsmList; tosize: tcgsize; a : aint;const ref : treference);
  581. var
  582. tmpref : treference;
  583. begin
  584. tmpref:=ref;
  585. make_simple_ref(list,tmpref);
  586. {$ifdef x86_64}
  587. { x86_64 only supports signed 32 bits constants directly }
  588. if (tosize in [OS_S64,OS_64]) and
  589. ((a<low(longint)) or (a>high(longint))) then
  590. begin
  591. a_load_const_ref(list,OS_32,longint(a and $ffffffff),tmpref);
  592. inc(tmpref.offset,4);
  593. a_load_const_ref(list,OS_32,longint(a shr 32),tmpref);
  594. end
  595. else
  596. {$endif x86_64}
  597. list.concat(taicpu.op_const_ref(A_MOV,TCGSize2OpSize[tosize],a,tmpref));
  598. end;
  599. procedure tcgx86.a_load_reg_ref(list : TAsmList; fromsize,tosize: TCGSize; reg : tregister;const ref : treference);
  600. var
  601. op: tasmop;
  602. s: topsize;
  603. tmpsize : tcgsize;
  604. tmpreg : tregister;
  605. tmpref : treference;
  606. begin
  607. tmpref:=ref;
  608. make_simple_ref(list,tmpref);
  609. check_register_size(fromsize,reg);
  610. sizes2load(fromsize,tosize,op,s);
  611. case s of
  612. {$ifdef x86_64}
  613. S_BQ,S_WQ,S_LQ,
  614. {$endif x86_64}
  615. S_BW,S_BL,S_WL :
  616. begin
  617. tmpreg:=getintregister(list,tosize);
  618. {$ifdef x86_64}
  619. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  620. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  621. 64 bit (FK) }
  622. if s in [S_BL,S_WL,S_L] then
  623. begin
  624. tmpreg:=makeregsize(list,tmpreg,OS_32);
  625. tmpsize:=OS_32;
  626. end
  627. else
  628. {$endif x86_64}
  629. tmpsize:=tosize;
  630. list.concat(taicpu.op_reg_reg(op,s,reg,tmpreg));
  631. a_load_reg_ref(list,tmpsize,tosize,tmpreg,tmpref);
  632. end;
  633. else
  634. list.concat(taicpu.op_reg_ref(op,s,reg,tmpref));
  635. end;
  636. end;
  637. procedure tcgx86.a_load_ref_reg(list : TAsmList;fromsize,tosize : tcgsize;const ref: treference;reg : tregister);
  638. var
  639. op: tasmop;
  640. s: topsize;
  641. tmpref : treference;
  642. begin
  643. tmpref:=ref;
  644. make_simple_ref(list,tmpref);
  645. check_register_size(tosize,reg);
  646. sizes2load(fromsize,tosize,op,s);
  647. {$ifdef x86_64}
  648. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  649. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  650. 64 bit (FK) }
  651. if s in [S_BL,S_WL,S_L] then
  652. reg:=makeregsize(list,reg,OS_32);
  653. {$endif x86_64}
  654. list.concat(taicpu.op_ref_reg(op,s,tmpref,reg));
  655. end;
  656. procedure tcgx86.a_load_reg_reg(list : TAsmList;fromsize,tosize : tcgsize;reg1,reg2 : tregister);
  657. var
  658. op: tasmop;
  659. s: topsize;
  660. instr:Taicpu;
  661. begin
  662. check_register_size(fromsize,reg1);
  663. check_register_size(tosize,reg2);
  664. if tcgsize2size[fromsize]>tcgsize2size[tosize] then
  665. begin
  666. reg1:=makeregsize(list,reg1,tosize);
  667. s:=tcgsize2opsize[tosize];
  668. op:=A_MOV;
  669. end
  670. else
  671. sizes2load(fromsize,tosize,op,s);
  672. {$ifdef x86_64}
  673. { zero extensions to 64 bit on the x86_64 are simply done by writting to the lower 32 bit
  674. which clears the upper 64 bit too, so it could be that s is S_L while the reg is
  675. 64 bit (FK)
  676. }
  677. if s in [S_BL,S_WL,S_L] then
  678. reg2:=makeregsize(list,reg2,OS_32);
  679. {$endif x86_64}
  680. if (reg1<>reg2) then
  681. begin
  682. instr:=taicpu.op_reg_reg(op,s,reg1,reg2);
  683. { Notify the register allocator that we have written a move instruction so
  684. it can try to eliminate it. }
  685. if (reg1<>current_procinfo.framepointer) and (reg1<>NR_STACK_POINTER_REG) then
  686. add_move_instruction(instr);
  687. list.concat(instr);
  688. end;
  689. {$ifdef x86_64}
  690. { avoid merging of registers and killing the zero extensions (FK) }
  691. if (tosize in [OS_64,OS_S64]) and (s=S_L) then
  692. list.concat(taicpu.op_const_reg(A_AND,S_L,$ffffffff,reg2));
  693. {$endif x86_64}
  694. end;
  695. procedure tcgx86.a_loadaddr_ref_reg(list : TAsmList;const ref : treference;r : tregister);
  696. var
  697. tmpref : treference;
  698. begin
  699. with ref do
  700. begin
  701. if (base=NR_NO) and (index=NR_NO) then
  702. begin
  703. if assigned(ref.symbol) then
  704. begin
  705. if (target_info.system=system_i386_darwin) and
  706. ((ref.symbol.bind = AB_EXTERNAL) or
  707. (cs_create_pic in current_settings.moduleswitches)) then
  708. begin
  709. if (ref.symbol.bind = AB_EXTERNAL) or
  710. ((cs_create_pic in current_settings.moduleswitches) and
  711. (ref.symbol.bind in [AB_COMMON,AB_GLOBAL])) then
  712. begin
  713. reference_reset_base(tmpref,
  714. g_indirect_sym_load(list,ref.symbol.name),
  715. offset);
  716. a_loadaddr_ref_reg(list,tmpref,r);
  717. end
  718. else
  719. begin
  720. include(current_procinfo.flags,pi_needs_got);
  721. reference_reset_base(tmpref,current_procinfo.got,offset);
  722. tmpref.symbol:=symbol;
  723. tmpref.relsymbol:=current_procinfo.CurrGOTLabel;
  724. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  725. end;
  726. end
  727. else if (cs_create_pic in current_settings.moduleswitches) then
  728. begin
  729. {$ifdef x86_64}
  730. reference_reset_symbol(tmpref,ref.symbol,0);
  731. tmpref.refaddr:=addr_pic;
  732. tmpref.base:=NR_RIP;
  733. list.concat(taicpu.op_ref_reg(A_MOV,S_Q,tmpref,r));
  734. {$else x86_64}
  735. reference_reset_symbol(tmpref,ref.symbol,0);
  736. tmpref.refaddr:=addr_pic;
  737. tmpref.base:=current_procinfo.got;
  738. include(current_procinfo.flags,pi_needs_got);
  739. list.concat(taicpu.op_ref_reg(A_MOV,S_L,tmpref,r));
  740. {$endif x86_64}
  741. if offset<>0 then
  742. a_op_const_reg(list,OP_ADD,OS_ADDR,offset,r);
  743. end
  744. else
  745. begin
  746. tmpref:=ref;
  747. tmpref.refaddr:=ADDR_FULL;
  748. list.concat(Taicpu.op_ref_reg(A_MOV,tcgsize2opsize[OS_ADDR],tmpref,r));
  749. end
  750. end
  751. else
  752. a_load_const_reg(list,OS_ADDR,offset,r)
  753. end
  754. else if (base=NR_NO) and (index<>NR_NO) and
  755. (offset=0) and (scalefactor=0) and (symbol=nil) then
  756. a_load_reg_reg(list,OS_ADDR,OS_ADDR,index,r)
  757. else if (base<>NR_NO) and (index=NR_NO) and
  758. (offset=0) and (symbol=nil) then
  759. a_load_reg_reg(list,OS_ADDR,OS_ADDR,base,r)
  760. else
  761. begin
  762. tmpref:=ref;
  763. make_simple_ref(list,tmpref);
  764. list.concat(Taicpu.op_ref_reg(A_LEA,tcgsize2opsize[OS_ADDR],tmpref,r));
  765. end;
  766. if segment<>NR_NO then
  767. begin
  768. if (tf_section_threadvars in target_info.flags) then
  769. begin
  770. { Convert thread local address to a process global addres
  771. as we cannot handle far pointers.}
  772. case target_info.system of
  773. system_i386_linux:
  774. if segment=NR_GS then
  775. begin
  776. reference_reset_symbol(tmpref,current_asmdata.RefAsmSymbol('___fpc_threadvar_offset'),0);
  777. tmpref.segment:=NR_GS;
  778. list.concat(Taicpu.op_ref_reg(A_ADD,tcgsize2opsize[OS_ADDR],tmpref,r));
  779. end
  780. else
  781. cgmessage(cg_e_cant_use_far_pointer_there);
  782. system_i386_win32:
  783. if segment=NR_FS then
  784. begin
  785. allocallcpuregisters(list);
  786. a_call_name(list,'GetTls');
  787. deallocallcpuregisters(list);
  788. list.concat(Taicpu.op_reg_reg(A_ADD,tcgsize2opsize[OS_ADDR],NR_EAX,r));
  789. end
  790. else
  791. cgmessage(cg_e_cant_use_far_pointer_there);
  792. else
  793. cgmessage(cg_e_cant_use_far_pointer_there);
  794. end;
  795. end
  796. else
  797. cgmessage(cg_e_cant_use_far_pointer_there);
  798. end;
  799. end;
  800. end;
  801. { all fpu load routines expect that R_ST[0-7] means an fpu regvar and }
  802. { R_ST means "the current value at the top of the fpu stack" (JM) }
  803. procedure tcgx86.a_loadfpu_reg_reg(list: TAsmList; fromsize, tosize: tcgsize; reg1, reg2: tregister);
  804. var
  805. href: treference;
  806. op: tasmop;
  807. s: topsize;
  808. begin
  809. if (reg1<>NR_ST) then
  810. begin
  811. floatloadops(tosize,op,s);
  812. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg1,rgfpu.fpuvaroffset)));
  813. inc_fpu_stack;
  814. end;
  815. if (reg2<>NR_ST) then
  816. begin
  817. floatstoreops(tosize,op,s);
  818. list.concat(taicpu.op_reg(op,s,rgfpu.correct_fpuregister(reg2,rgfpu.fpuvaroffset)));
  819. dec_fpu_stack;
  820. end;
  821. { OS_F80 < OS_C64, but OS_C64 fits perfectly in OS_F80 }
  822. if (reg1=NR_ST) and
  823. (reg2=NR_ST) and
  824. (tosize<>OS_F80) and
  825. (tosize<fromsize) then
  826. begin
  827. { can't round down to lower precision in x87 :/ }
  828. tg.gettemp(list,tcgsize2size[tosize],tt_normal,href);
  829. a_loadfpu_reg_ref(list,fromsize,tosize,NR_ST,href);
  830. a_loadfpu_ref_reg(list,tosize,tosize,href,NR_ST);
  831. tg.ungettemp(list,href);
  832. end;
  833. end;
  834. procedure tcgx86.a_loadfpu_ref_reg(list: TAsmList; fromsize, tosize: tcgsize; const ref: treference; reg: tregister);
  835. begin
  836. floatload(list,fromsize,ref);
  837. a_loadfpu_reg_reg(list,fromsize,tosize,NR_ST,reg);
  838. end;
  839. procedure tcgx86.a_loadfpu_reg_ref(list: TAsmList; fromsize,tosize: tcgsize; reg: tregister; const ref: treference);
  840. begin
  841. if reg<>NR_ST then
  842. a_loadfpu_reg_reg(list,fromsize,tosize,reg,NR_ST);
  843. floatstore(list,tosize,ref);
  844. end;
  845. function get_scalar_mm_op(fromsize,tosize : tcgsize) : tasmop;
  846. const
  847. convertop : array[OS_F32..OS_F128,OS_F32..OS_F128] of tasmop = (
  848. (A_MOVSS,A_CVTSS2SD,A_NONE,A_NONE,A_NONE),
  849. (A_CVTSD2SS,A_MOVSD,A_NONE,A_NONE,A_NONE),
  850. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE),
  851. (A_NONE,A_NONE,A_NONE,A_MOVQ,A_NONE),
  852. (A_NONE,A_NONE,A_NONE,A_NONE,A_NONE));
  853. begin
  854. result:=convertop[fromsize,tosize];
  855. if result=A_NONE then
  856. internalerror(200312205);
  857. end;
  858. procedure tcgx86.a_loadmm_reg_reg(list: TAsmList; fromsize, tosize : tcgsize;reg1, reg2: tregister;shuffle : pmmshuffle);
  859. var
  860. instr : taicpu;
  861. begin
  862. if shuffle=nil then
  863. begin
  864. if fromsize=tosize then
  865. { needs correct size in case of spilling }
  866. case fromsize of
  867. OS_F32:
  868. instr:=taicpu.op_reg_reg(A_MOVAPS,S_NO,reg1,reg2);
  869. OS_F64:
  870. instr:=taicpu.op_reg_reg(A_MOVAPD,S_NO,reg1,reg2);
  871. else
  872. internalerror(2006091201);
  873. end
  874. else
  875. internalerror(200312202);
  876. end
  877. else if shufflescalar(shuffle) then
  878. instr:=taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg1,reg2)
  879. else
  880. internalerror(200312201);
  881. case get_scalar_mm_op(fromsize,tosize) of
  882. A_MOVSS,
  883. A_MOVSD,
  884. A_MOVQ:
  885. add_move_instruction(instr);
  886. end;
  887. list.concat(instr);
  888. end;
  889. procedure tcgx86.a_loadmm_ref_reg(list: TAsmList; fromsize, tosize : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  890. var
  891. tmpref : treference;
  892. begin
  893. tmpref:=ref;
  894. make_simple_ref(list,tmpref);
  895. if shuffle=nil then
  896. begin
  897. if fromsize=OS_M64 then
  898. list.concat(taicpu.op_ref_reg(A_MOVQ,S_NO,tmpref,reg))
  899. else
  900. {$ifdef x86_64}
  901. { x86-64 has always properly aligned data }
  902. list.concat(taicpu.op_ref_reg(A_MOVDQA,S_NO,tmpref,reg));
  903. {$else x86_64}
  904. list.concat(taicpu.op_ref_reg(A_MOVDQU,S_NO,tmpref,reg));
  905. {$endif x86_64}
  906. end
  907. else if shufflescalar(shuffle) then
  908. list.concat(taicpu.op_ref_reg(get_scalar_mm_op(fromsize,tosize),S_NO,tmpref,reg))
  909. else
  910. internalerror(200312252);
  911. end;
  912. procedure tcgx86.a_loadmm_reg_ref(list: TAsmList; fromsize, tosize : tcgsize;reg: tregister; const ref: treference;shuffle : pmmshuffle);
  913. var
  914. hreg : tregister;
  915. tmpref : treference;
  916. begin
  917. tmpref:=ref;
  918. make_simple_ref(list,tmpref);
  919. if shuffle=nil then
  920. begin
  921. if fromsize=OS_M64 then
  922. list.concat(taicpu.op_reg_ref(A_MOVQ,S_NO,reg,tmpref))
  923. else
  924. {$ifdef x86_64}
  925. { x86-64 has always properly aligned data }
  926. list.concat(taicpu.op_reg_ref(A_MOVDQA,S_NO,reg,tmpref))
  927. {$else x86_64}
  928. list.concat(taicpu.op_reg_ref(A_MOVDQU,S_NO,reg,tmpref))
  929. {$endif x86_64}
  930. end
  931. else if shufflescalar(shuffle) then
  932. begin
  933. if tosize<>fromsize then
  934. begin
  935. hreg:=getmmregister(list,tosize);
  936. list.concat(taicpu.op_reg_reg(get_scalar_mm_op(fromsize,tosize),S_NO,reg,hreg));
  937. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(tosize,tosize),S_NO,hreg,tmpref));
  938. end
  939. else
  940. list.concat(taicpu.op_reg_ref(get_scalar_mm_op(fromsize,tosize),S_NO,reg,tmpref));
  941. end
  942. else
  943. internalerror(200312252);
  944. end;
  945. procedure tcgx86.a_opmm_ref_reg(list: TAsmList; Op: TOpCG; size : tcgsize;const ref: treference; reg: tregister;shuffle : pmmshuffle);
  946. var
  947. l : tlocation;
  948. begin
  949. l.loc:=LOC_REFERENCE;
  950. l.reference:=ref;
  951. l.size:=size;
  952. opmm_loc_reg(list,op,size,l,reg,shuffle);
  953. end;
  954. procedure tcgx86.a_opmm_reg_reg(list: TAsmList; Op: TOpCG; size : tcgsize;src,dst: tregister;shuffle : pmmshuffle);
  955. var
  956. l : tlocation;
  957. begin
  958. l.loc:=LOC_MMREGISTER;
  959. l.register:=src;
  960. l.size:=size;
  961. opmm_loc_reg(list,op,size,l,dst,shuffle);
  962. end;
  963. procedure tcgx86.opmm_loc_reg(list: TAsmList; Op: TOpCG; size : tcgsize;loc : tlocation;dst: tregister; shuffle : pmmshuffle);
  964. const
  965. opmm2asmop : array[0..1,OS_F32..OS_F64,topcg] of tasmop = (
  966. ( { scalar }
  967. ( { OS_F32 }
  968. 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
  969. ),
  970. ( { OS_F64 }
  971. 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
  972. )
  973. ),
  974. ( { vectorized/packed }
  975. { because the logical packed single instructions have shorter op codes, we use always
  976. these
  977. }
  978. ( { OS_F32 }
  979. 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
  980. ),
  981. ( { OS_F64 }
  982. 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
  983. )
  984. )
  985. );
  986. var
  987. resultreg : tregister;
  988. asmop : tasmop;
  989. begin
  990. { this is an internally used procedure so the parameters have
  991. some constrains
  992. }
  993. if loc.size<>size then
  994. internalerror(200312213);
  995. resultreg:=dst;
  996. { deshuffle }
  997. //!!!
  998. if (shuffle<>nil) and not(shufflescalar(shuffle)) then
  999. begin
  1000. end
  1001. else if (shuffle=nil) then
  1002. asmop:=opmm2asmop[1,size,op]
  1003. else if shufflescalar(shuffle) then
  1004. begin
  1005. asmop:=opmm2asmop[0,size,op];
  1006. { no scalar operation available? }
  1007. if asmop=A_NOP then
  1008. begin
  1009. { do vectorized and shuffle finally }
  1010. //!!!
  1011. end;
  1012. end
  1013. else
  1014. internalerror(200312211);
  1015. if asmop=A_NOP then
  1016. internalerror(200312216);
  1017. case loc.loc of
  1018. LOC_CREFERENCE,LOC_REFERENCE:
  1019. begin
  1020. make_simple_ref(current_asmdata.CurrAsmList,loc.reference);
  1021. list.concat(taicpu.op_ref_reg(asmop,S_NO,loc.reference,resultreg));
  1022. end;
  1023. LOC_CMMREGISTER,LOC_MMREGISTER:
  1024. list.concat(taicpu.op_reg_reg(asmop,S_NO,loc.register,resultreg));
  1025. else
  1026. internalerror(200312214);
  1027. end;
  1028. { shuffle }
  1029. if resultreg<>dst then
  1030. begin
  1031. internalerror(200312212);
  1032. end;
  1033. end;
  1034. procedure tcgx86.a_op_const_reg(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; reg: TRegister);
  1035. var
  1036. opcode : tasmop;
  1037. power : longint;
  1038. {$ifdef x86_64}
  1039. tmpreg : tregister;
  1040. {$endif x86_64}
  1041. begin
  1042. optimize_op_const(op, a);
  1043. {$ifdef x86_64}
  1044. { x86_64 only supports signed 32 bits constants directly }
  1045. if not(op in [OP_NONE,OP_MOVE]) and
  1046. (size in [OS_S64,OS_64]) and
  1047. ((a<low(longint)) or (a>high(longint))) then
  1048. begin
  1049. tmpreg:=getintregister(list,size);
  1050. a_load_const_reg(list,size,a,tmpreg);
  1051. a_op_reg_reg(list,op,size,tmpreg,reg);
  1052. exit;
  1053. end;
  1054. {$endif x86_64}
  1055. check_register_size(size,reg);
  1056. case op of
  1057. OP_NONE :
  1058. begin
  1059. { Opcode is optimized away }
  1060. end;
  1061. OP_MOVE :
  1062. begin
  1063. { Optimized, replaced with a simple load }
  1064. a_load_const_reg(list,size,a,reg);
  1065. end;
  1066. OP_DIV, OP_IDIV:
  1067. begin
  1068. if ispowerof2(int64(a),power) then
  1069. begin
  1070. case op of
  1071. OP_DIV:
  1072. opcode := A_SHR;
  1073. OP_IDIV:
  1074. opcode := A_SAR;
  1075. end;
  1076. list.concat(taicpu.op_const_reg(opcode,TCgSize2OpSize[size],power,reg));
  1077. exit;
  1078. end;
  1079. { the rest should be handled specifically in the code }
  1080. { generator because of the silly register usage restraints }
  1081. internalerror(200109224);
  1082. end;
  1083. OP_MUL,OP_IMUL:
  1084. begin
  1085. if not(cs_check_overflow in current_settings.localswitches) and
  1086. ispowerof2(int64(a),power) then
  1087. begin
  1088. list.concat(taicpu.op_const_reg(A_SHL,TCgSize2OpSize[size],power,reg));
  1089. exit;
  1090. end;
  1091. if op = OP_IMUL then
  1092. list.concat(taicpu.op_const_reg(A_IMUL,TCgSize2OpSize[size],a,reg))
  1093. else
  1094. { OP_MUL should be handled specifically in the code }
  1095. { generator because of the silly register usage restraints }
  1096. internalerror(200109225);
  1097. end;
  1098. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  1099. if not(cs_check_overflow in current_settings.localswitches) and
  1100. (a = 1) and
  1101. (op in [OP_ADD,OP_SUB]) then
  1102. if op = OP_ADD then
  1103. list.concat(taicpu.op_reg(A_INC,TCgSize2OpSize[size],reg))
  1104. else
  1105. list.concat(taicpu.op_reg(A_DEC,TCgSize2OpSize[size],reg))
  1106. else if (a = 0) then
  1107. if (op <> OP_AND) then
  1108. exit
  1109. else
  1110. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],0,reg))
  1111. else if (aword(a) = high(aword)) and
  1112. (op in [OP_AND,OP_OR,OP_XOR]) then
  1113. begin
  1114. case op of
  1115. OP_AND:
  1116. exit;
  1117. OP_OR:
  1118. list.concat(taicpu.op_const_reg(A_MOV,TCgSize2OpSize[size],aint(high(aword)),reg));
  1119. OP_XOR:
  1120. list.concat(taicpu.op_reg(A_NOT,TCgSize2OpSize[size],reg));
  1121. end
  1122. end
  1123. else
  1124. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a,reg));
  1125. OP_SHL,OP_SHR,OP_SAR:
  1126. begin
  1127. {$ifdef x86_64}
  1128. if (a and 63) <> 0 Then
  1129. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 63,reg));
  1130. if (a shr 6) <> 0 Then
  1131. internalerror(200609073);
  1132. {$else x86_64}
  1133. if (a and 31) <> 0 Then
  1134. list.concat(taicpu.op_const_reg(TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,reg));
  1135. if (a shr 5) <> 0 Then
  1136. internalerror(200609071);
  1137. {$endif x86_64}
  1138. end
  1139. else internalerror(200609072);
  1140. end;
  1141. end;
  1142. procedure tcgx86.a_op_const_ref(list : TAsmList; Op: TOpCG; size: TCGSize; a: aint; const ref: TReference);
  1143. var
  1144. opcode: tasmop;
  1145. power: longint;
  1146. {$ifdef x86_64}
  1147. tmpreg : tregister;
  1148. {$endif x86_64}
  1149. tmpref : treference;
  1150. begin
  1151. optimize_op_const(op, a);
  1152. tmpref:=ref;
  1153. make_simple_ref(list,tmpref);
  1154. {$ifdef x86_64}
  1155. { x86_64 only supports signed 32 bits constants directly }
  1156. if not(op in [OP_NONE,OP_MOVE]) and
  1157. (size in [OS_S64,OS_64]) and
  1158. ((a<low(longint)) or (a>high(longint))) then
  1159. begin
  1160. tmpreg:=getintregister(list,size);
  1161. a_load_const_reg(list,size,a,tmpreg);
  1162. a_op_reg_ref(list,op,size,tmpreg,tmpref);
  1163. exit;
  1164. end;
  1165. {$endif x86_64}
  1166. Case Op of
  1167. OP_NONE :
  1168. begin
  1169. { Opcode is optimized away }
  1170. end;
  1171. OP_MOVE :
  1172. begin
  1173. { Optimized, replaced with a simple load }
  1174. a_load_const_ref(list,size,a,ref);
  1175. end;
  1176. OP_DIV, OP_IDIV:
  1177. Begin
  1178. if ispowerof2(int64(a),power) then
  1179. begin
  1180. case op of
  1181. OP_DIV:
  1182. opcode := A_SHR;
  1183. OP_IDIV:
  1184. opcode := A_SAR;
  1185. end;
  1186. list.concat(taicpu.op_const_ref(opcode,
  1187. TCgSize2OpSize[size],power,tmpref));
  1188. exit;
  1189. end;
  1190. { the rest should be handled specifically in the code }
  1191. { generator because of the silly register usage restraints }
  1192. internalerror(200109231);
  1193. End;
  1194. OP_MUL,OP_IMUL:
  1195. begin
  1196. if not(cs_check_overflow in current_settings.localswitches) and
  1197. ispowerof2(int64(a),power) then
  1198. begin
  1199. list.concat(taicpu.op_const_ref(A_SHL,TCgSize2OpSize[size],
  1200. power,tmpref));
  1201. exit;
  1202. end;
  1203. { can't multiply a memory location directly with a constant }
  1204. if op = OP_IMUL then
  1205. inherited a_op_const_ref(list,op,size,a,tmpref)
  1206. else
  1207. { OP_MUL should be handled specifically in the code }
  1208. { generator because of the silly register usage restraints }
  1209. internalerror(200109232);
  1210. end;
  1211. OP_ADD, OP_AND, OP_OR, OP_SUB, OP_XOR:
  1212. if not(cs_check_overflow in current_settings.localswitches) and
  1213. (a = 1) and
  1214. (op in [OP_ADD,OP_SUB]) then
  1215. if op = OP_ADD then
  1216. list.concat(taicpu.op_ref(A_INC,TCgSize2OpSize[size],tmpref))
  1217. else
  1218. list.concat(taicpu.op_ref(A_DEC,TCgSize2OpSize[size],tmpref))
  1219. else if (a = 0) then
  1220. if (op <> OP_AND) then
  1221. exit
  1222. else
  1223. a_load_const_ref(list,size,0,tmpref)
  1224. else if (aword(a) = high(aword)) and
  1225. (op in [OP_AND,OP_OR,OP_XOR]) then
  1226. begin
  1227. case op of
  1228. OP_AND:
  1229. exit;
  1230. OP_OR:
  1231. list.concat(taicpu.op_const_ref(A_MOV,TCgSize2OpSize[size],aint(high(aword)),tmpref));
  1232. OP_XOR:
  1233. list.concat(taicpu.op_ref(A_NOT,TCgSize2OpSize[size],tmpref));
  1234. end
  1235. end
  1236. else
  1237. list.concat(taicpu.op_const_ref(TOpCG2AsmOp[op],
  1238. TCgSize2OpSize[size],a,tmpref));
  1239. OP_SHL,OP_SHR,OP_SAR:
  1240. begin
  1241. if (a and 31) <> 0 then
  1242. list.concat(taicpu.op_const_ref(
  1243. TOpCG2AsmOp[op],TCgSize2OpSize[size],a and 31,tmpref));
  1244. if (a shr 5) <> 0 Then
  1245. internalerror(68991);
  1246. end
  1247. else internalerror(68992);
  1248. end;
  1249. end;
  1250. procedure tcgx86.a_op_reg_reg(list : TAsmList; Op: TOpCG; size: TCGSize; src, dst: TRegister);
  1251. var
  1252. dstsize: topsize;
  1253. instr:Taicpu;
  1254. begin
  1255. check_register_size(size,src);
  1256. check_register_size(size,dst);
  1257. dstsize := tcgsize2opsize[size];
  1258. case op of
  1259. OP_NEG,OP_NOT:
  1260. begin
  1261. if src<>dst then
  1262. a_load_reg_reg(list,size,size,src,dst);
  1263. list.concat(taicpu.op_reg(TOpCG2AsmOp[op],dstsize,dst));
  1264. end;
  1265. OP_MUL,OP_DIV,OP_IDIV:
  1266. { special stuff, needs separate handling inside code }
  1267. { generator }
  1268. internalerror(200109233);
  1269. OP_SHR,OP_SHL,OP_SAR:
  1270. begin
  1271. { Use ecx to load the value, that allows beter coalescing }
  1272. getcpuregister(list,NR_ECX);
  1273. a_load_reg_reg(list,size,OS_32,src,NR_ECX);
  1274. list.concat(taicpu.op_reg_reg(Topcg2asmop[op],tcgsize2opsize[size],NR_CL,dst));
  1275. ungetcpuregister(list,NR_ECX);
  1276. end;
  1277. else
  1278. begin
  1279. if reg2opsize(src) <> dstsize then
  1280. internalerror(200109226);
  1281. instr:=taicpu.op_reg_reg(TOpCG2AsmOp[op],dstsize,src,dst);
  1282. list.concat(instr);
  1283. end;
  1284. end;
  1285. end;
  1286. procedure tcgx86.a_op_ref_reg(list : TAsmList; Op: TOpCG; size: TCGSize; const ref: TReference; reg: TRegister);
  1287. var
  1288. tmpref : treference;
  1289. begin
  1290. tmpref:=ref;
  1291. make_simple_ref(list,tmpref);
  1292. check_register_size(size,reg);
  1293. case op of
  1294. OP_NEG,OP_NOT,OP_IMUL:
  1295. begin
  1296. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1297. end;
  1298. OP_MUL,OP_DIV,OP_IDIV:
  1299. { special stuff, needs separate handling inside code }
  1300. { generator }
  1301. internalerror(200109239);
  1302. else
  1303. begin
  1304. reg := makeregsize(list,reg,size);
  1305. list.concat(taicpu.op_ref_reg(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref,reg));
  1306. end;
  1307. end;
  1308. end;
  1309. procedure tcgx86.a_op_reg_ref(list : TAsmList; Op: TOpCG; size: TCGSize;reg: TRegister; const ref: TReference);
  1310. var
  1311. tmpref : treference;
  1312. begin
  1313. tmpref:=ref;
  1314. make_simple_ref(list,tmpref);
  1315. check_register_size(size,reg);
  1316. case op of
  1317. OP_NEG,OP_NOT:
  1318. begin
  1319. if reg<>NR_NO then
  1320. internalerror(200109237);
  1321. list.concat(taicpu.op_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],tmpref));
  1322. end;
  1323. OP_IMUL:
  1324. begin
  1325. { this one needs a load/imul/store, which is the default }
  1326. inherited a_op_ref_reg(list,op,size,tmpref,reg);
  1327. end;
  1328. OP_MUL,OP_DIV,OP_IDIV:
  1329. { special stuff, needs separate handling inside code }
  1330. { generator }
  1331. internalerror(200109238);
  1332. else
  1333. begin
  1334. list.concat(taicpu.op_reg_ref(TOpCG2AsmOp[op],tcgsize2opsize[size],reg,tmpref));
  1335. end;
  1336. end;
  1337. end;
  1338. {*************** compare instructructions ****************}
  1339. procedure tcgx86.a_cmp_const_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;reg : tregister;
  1340. l : tasmlabel);
  1341. {$ifdef x86_64}
  1342. var
  1343. tmpreg : tregister;
  1344. {$endif x86_64}
  1345. begin
  1346. {$ifdef x86_64}
  1347. { x86_64 only supports signed 32 bits constants directly }
  1348. if (size in [OS_S64,OS_64]) and
  1349. ((a<low(longint)) or (a>high(longint))) then
  1350. begin
  1351. tmpreg:=getintregister(list,size);
  1352. a_load_const_reg(list,size,a,tmpreg);
  1353. a_cmp_reg_reg_label(list,size,cmp_op,tmpreg,reg,l);
  1354. exit;
  1355. end;
  1356. {$endif x86_64}
  1357. if (a = 0) then
  1358. list.concat(taicpu.op_reg_reg(A_TEST,tcgsize2opsize[size],reg,reg))
  1359. else
  1360. list.concat(taicpu.op_const_reg(A_CMP,tcgsize2opsize[size],a,reg));
  1361. a_jmp_cond(list,cmp_op,l);
  1362. end;
  1363. procedure tcgx86.a_cmp_const_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;a : aint;const ref : treference;
  1364. l : tasmlabel);
  1365. var
  1366. {$ifdef x86_64}
  1367. tmpreg : tregister;
  1368. {$endif x86_64}
  1369. tmpref : treference;
  1370. begin
  1371. tmpref:=ref;
  1372. make_simple_ref(list,tmpref);
  1373. {$ifdef x86_64}
  1374. { x86_64 only supports signed 32 bits constants directly }
  1375. if (size in [OS_S64,OS_64]) and
  1376. ((a<low(longint)) or (a>high(longint))) then
  1377. begin
  1378. tmpreg:=getintregister(list,size);
  1379. a_load_const_reg(list,size,a,tmpreg);
  1380. a_cmp_reg_ref_label(list,size,cmp_op,tmpreg,tmpref,l);
  1381. exit;
  1382. end;
  1383. {$endif x86_64}
  1384. list.concat(taicpu.op_const_ref(A_CMP,TCgSize2OpSize[size],a,tmpref));
  1385. a_jmp_cond(list,cmp_op,l);
  1386. end;
  1387. procedure tcgx86.a_cmp_reg_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;
  1388. reg1,reg2 : tregister;l : tasmlabel);
  1389. begin
  1390. check_register_size(size,reg1);
  1391. check_register_size(size,reg2);
  1392. list.concat(taicpu.op_reg_reg(A_CMP,TCgSize2OpSize[size],reg1,reg2));
  1393. a_jmp_cond(list,cmp_op,l);
  1394. end;
  1395. procedure tcgx86.a_cmp_ref_reg_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;const ref: treference; reg : tregister;l : tasmlabel);
  1396. var
  1397. tmpref : treference;
  1398. begin
  1399. tmpref:=ref;
  1400. make_simple_ref(list,tmpref);
  1401. check_register_size(size,reg);
  1402. list.concat(taicpu.op_ref_reg(A_CMP,TCgSize2OpSize[size],tmpref,reg));
  1403. a_jmp_cond(list,cmp_op,l);
  1404. end;
  1405. procedure tcgx86.a_cmp_reg_ref_label(list : TAsmList;size : tcgsize;cmp_op : topcmp;reg : tregister;const ref: treference; l : tasmlabel);
  1406. var
  1407. tmpref : treference;
  1408. begin
  1409. tmpref:=ref;
  1410. make_simple_ref(list,tmpref);
  1411. check_register_size(size,reg);
  1412. list.concat(taicpu.op_reg_ref(A_CMP,TCgSize2OpSize[size],reg,tmpref));
  1413. a_jmp_cond(list,cmp_op,l);
  1414. end;
  1415. procedure tcgx86.a_jmp_cond(list : TAsmList;cond : TOpCmp;l: tasmlabel);
  1416. var
  1417. ai : taicpu;
  1418. begin
  1419. if cond=OC_None then
  1420. ai := Taicpu.Op_sym(A_JMP,S_NO,l)
  1421. else
  1422. begin
  1423. ai:=Taicpu.Op_sym(A_Jcc,S_NO,l);
  1424. ai.SetCondition(TOpCmp2AsmCond[cond]);
  1425. end;
  1426. ai.is_jmp:=true;
  1427. list.concat(ai);
  1428. end;
  1429. procedure tcgx86.a_jmp_flags(list : TAsmList;const f : TResFlags;l: tasmlabel);
  1430. var
  1431. ai : taicpu;
  1432. begin
  1433. ai := Taicpu.op_sym(A_Jcc,S_NO,l);
  1434. ai.SetCondition(flags_to_cond(f));
  1435. ai.is_jmp := true;
  1436. list.concat(ai);
  1437. end;
  1438. procedure tcgx86.g_flags2reg(list: TAsmList; size: TCgSize; const f: tresflags; reg: TRegister);
  1439. var
  1440. ai : taicpu;
  1441. hreg : tregister;
  1442. begin
  1443. hreg:=makeregsize(list,reg,OS_8);
  1444. ai:=Taicpu.op_reg(A_SETcc,S_B,hreg);
  1445. ai.setcondition(flags_to_cond(f));
  1446. list.concat(ai);
  1447. if (reg<>hreg) then
  1448. a_load_reg_reg(list,OS_8,size,hreg,reg);
  1449. end;
  1450. procedure tcgx86.g_flags2ref(list: TAsmList; size: TCgSize; const f: tresflags; const ref: TReference);
  1451. var
  1452. ai : taicpu;
  1453. tmpref : treference;
  1454. begin
  1455. tmpref:=ref;
  1456. make_simple_ref(list,tmpref);
  1457. if not(size in [OS_8,OS_S8]) then
  1458. a_load_const_ref(list,size,0,tmpref);
  1459. ai:=Taicpu.op_ref(A_SETcc,S_B,tmpref);
  1460. ai.setcondition(flags_to_cond(f));
  1461. list.concat(ai);
  1462. end;
  1463. { ************* concatcopy ************ }
  1464. procedure Tcgx86.g_concatcopy(list:TAsmList;const source,dest:Treference;len:aint);
  1465. const
  1466. {$ifdef cpu64bit}
  1467. REGCX=NR_RCX;
  1468. REGSI=NR_RSI;
  1469. REGDI=NR_RDI;
  1470. {$else cpu64bit}
  1471. REGCX=NR_ECX;
  1472. REGSI=NR_ESI;
  1473. REGDI=NR_EDI;
  1474. {$endif cpu64bit}
  1475. type copymode=(copy_move,copy_mmx,copy_string);
  1476. var srcref,dstref:Treference;
  1477. r,r0,r1,r2,r3:Tregister;
  1478. helpsize:aint;
  1479. copysize:byte;
  1480. cgsize:Tcgsize;
  1481. cm:copymode;
  1482. begin
  1483. cm:=copy_move;
  1484. helpsize:=3*sizeof(aword);
  1485. if cs_opt_size in current_settings.optimizerswitches then
  1486. helpsize:=2*sizeof(aword);
  1487. if (cs_mmx in current_settings.localswitches) and
  1488. not(pi_uses_fpu in current_procinfo.flags) and
  1489. ((len=8) or (len=16) or (len=24) or (len=32)) then
  1490. cm:=copy_mmx;
  1491. if (len>helpsize) then
  1492. cm:=copy_string;
  1493. if (cs_opt_size in current_settings.optimizerswitches) and
  1494. not((len<=16) and (cm=copy_mmx)) then
  1495. cm:=copy_string;
  1496. if (source.segment<>NR_NO) or
  1497. (dest.segment<>NR_NO) then
  1498. cm:=copy_string;
  1499. case cm of
  1500. copy_move:
  1501. begin
  1502. dstref:=dest;
  1503. srcref:=source;
  1504. copysize:=sizeof(aint);
  1505. cgsize:=int_cgsize(copysize);
  1506. while len<>0 do
  1507. begin
  1508. if len<2 then
  1509. begin
  1510. copysize:=1;
  1511. cgsize:=OS_8;
  1512. end
  1513. else if len<4 then
  1514. begin
  1515. copysize:=2;
  1516. cgsize:=OS_16;
  1517. end
  1518. else if len<8 then
  1519. begin
  1520. copysize:=4;
  1521. cgsize:=OS_32;
  1522. end
  1523. {$ifdef cpu64bit}
  1524. else if len<16 then
  1525. begin
  1526. copysize:=8;
  1527. cgsize:=OS_64;
  1528. end
  1529. {$endif}
  1530. ;
  1531. dec(len,copysize);
  1532. r:=getintregister(list,cgsize);
  1533. a_load_ref_reg(list,cgsize,cgsize,srcref,r);
  1534. a_load_reg_ref(list,cgsize,cgsize,r,dstref);
  1535. inc(srcref.offset,copysize);
  1536. inc(dstref.offset,copysize);
  1537. end;
  1538. end;
  1539. copy_mmx:
  1540. begin
  1541. dstref:=dest;
  1542. srcref:=source;
  1543. r0:=getmmxregister(list);
  1544. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r0,nil);
  1545. if len>=16 then
  1546. begin
  1547. inc(srcref.offset,8);
  1548. r1:=getmmxregister(list);
  1549. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r1,nil);
  1550. end;
  1551. if len>=24 then
  1552. begin
  1553. inc(srcref.offset,8);
  1554. r2:=getmmxregister(list);
  1555. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r2,nil);
  1556. end;
  1557. if len>=32 then
  1558. begin
  1559. inc(srcref.offset,8);
  1560. r3:=getmmxregister(list);
  1561. a_loadmm_ref_reg(list,OS_M64,OS_M64,srcref,r3,nil);
  1562. end;
  1563. a_loadmm_reg_ref(list,OS_M64,OS_M64,r0,dstref,nil);
  1564. if len>=16 then
  1565. begin
  1566. inc(dstref.offset,8);
  1567. a_loadmm_reg_ref(list,OS_M64,OS_M64,r1,dstref,nil);
  1568. end;
  1569. if len>=24 then
  1570. begin
  1571. inc(dstref.offset,8);
  1572. a_loadmm_reg_ref(list,OS_M64,OS_M64,r2,dstref,nil);
  1573. end;
  1574. if len>=32 then
  1575. begin
  1576. inc(dstref.offset,8);
  1577. a_loadmm_reg_ref(list,OS_M64,OS_M64,r3,dstref,nil);
  1578. end;
  1579. end
  1580. else {copy_string, should be a good fallback in case of unhandled}
  1581. begin
  1582. getcpuregister(list,REGDI);
  1583. if (dest.segment=NR_NO) then
  1584. a_loadaddr_ref_reg(list,dest,REGDI)
  1585. else
  1586. begin
  1587. dstref:=dest;
  1588. dstref.segment:=NR_NO;
  1589. a_loadaddr_ref_reg(list,dstref,REGDI);
  1590. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_ES));
  1591. list.concat(taicpu.op_reg(A_PUSH,S_L,dest.segment));
  1592. list.concat(taicpu.op_reg(A_POP,S_L,NR_ES));
  1593. end;
  1594. getcpuregister(list,REGSI);
  1595. if (source.segment=NR_NO) then
  1596. a_loadaddr_ref_reg(list,source,REGSI)
  1597. else
  1598. begin
  1599. srcref:=source;
  1600. srcref.segment:=NR_NO;
  1601. a_loadaddr_ref_reg(list,srcref,REGSI);
  1602. list.concat(taicpu.op_reg(A_PUSH,S_L,NR_DS));
  1603. list.concat(taicpu.op_reg(A_PUSH,S_L,source.segment));
  1604. list.concat(taicpu.op_reg(A_POP,S_L,NR_DS));
  1605. end;
  1606. getcpuregister(list,REGCX);
  1607. {$ifdef i386}
  1608. list.concat(Taicpu.op_none(A_CLD,S_NO));
  1609. {$endif i386}
  1610. if (cs_opt_size in current_settings.optimizerswitches) and
  1611. (len>sizeof(aint)+(sizeof(aint) div 2)) then
  1612. begin
  1613. a_load_const_reg(list,OS_INT,len,REGCX);
  1614. list.concat(Taicpu.op_none(A_REP,S_NO));
  1615. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1616. end
  1617. else
  1618. begin
  1619. helpsize:=len div sizeof(aint);
  1620. len:=len mod sizeof(aint);
  1621. if helpsize>1 then
  1622. begin
  1623. a_load_const_reg(list,OS_INT,helpsize,REGCX);
  1624. list.concat(Taicpu.op_none(A_REP,S_NO));
  1625. end;
  1626. if helpsize>0 then
  1627. begin
  1628. {$ifdef cpu64bit}
  1629. if sizeof(aint)=8 then
  1630. list.concat(Taicpu.op_none(A_MOVSQ,S_NO))
  1631. else
  1632. {$endif cpu64bit}
  1633. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1634. end;
  1635. if len>=4 then
  1636. begin
  1637. dec(len,4);
  1638. list.concat(Taicpu.op_none(A_MOVSD,S_NO));
  1639. end;
  1640. if len>=2 then
  1641. begin
  1642. dec(len,2);
  1643. list.concat(Taicpu.op_none(A_MOVSW,S_NO));
  1644. end;
  1645. if len=1 then
  1646. list.concat(Taicpu.op_none(A_MOVSB,S_NO));
  1647. end;
  1648. ungetcpuregister(list,REGCX);
  1649. ungetcpuregister(list,REGSI);
  1650. ungetcpuregister(list,REGDI);
  1651. if (source.segment<>NR_NO) then
  1652. list.concat(taicpu.op_reg(A_POP,S_L,NR_DS));
  1653. if (dest.segment<>NR_NO) then
  1654. list.concat(taicpu.op_reg(A_POP,S_L,NR_ES));
  1655. end;
  1656. end;
  1657. end;
  1658. {****************************************************************************
  1659. Entry/Exit Code Helpers
  1660. ****************************************************************************}
  1661. procedure tcgx86.g_profilecode(list : TAsmList);
  1662. var
  1663. pl : tasmlabel;
  1664. mcountprefix : String[4];
  1665. begin
  1666. case target_info.system of
  1667. {$ifndef NOTARGETWIN}
  1668. system_i386_win32,
  1669. {$endif}
  1670. system_i386_freebsd,
  1671. system_i386_netbsd,
  1672. // system_i386_openbsd,
  1673. system_i386_wdosx :
  1674. begin
  1675. Case target_info.system Of
  1676. system_i386_freebsd : mcountprefix:='.';
  1677. system_i386_netbsd : mcountprefix:='__';
  1678. // system_i386_openbsd : mcountprefix:='.';
  1679. else
  1680. mcountPrefix:='';
  1681. end;
  1682. current_asmdata.getaddrlabel(pl);
  1683. new_section(list,sec_data,lower(current_procinfo.procdef.mangledname),sizeof(aint));
  1684. list.concat(Tai_label.Create(pl));
  1685. list.concat(Tai_const.Create_32bit(0));
  1686. new_section(list,sec_code,lower(current_procinfo.procdef.mangledname),0);
  1687. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1688. list.concat(Taicpu.Op_sym_ofs_reg(A_MOV,S_L,pl,0,NR_EDX));
  1689. a_call_name(list,target_info.Cprefix+mcountprefix+'mcount');
  1690. list.concat(Taicpu.Op_reg(A_POP,S_L,NR_EDX));
  1691. end;
  1692. system_i386_linux:
  1693. a_call_name(list,target_info.Cprefix+'mcount');
  1694. system_i386_go32v2,system_i386_watcom:
  1695. begin
  1696. a_call_name(list,'MCOUNT');
  1697. end;
  1698. system_x86_64_linux,
  1699. system_x86_64_darwin:
  1700. begin
  1701. a_call_name(list,'mcount');
  1702. end;
  1703. end;
  1704. end;
  1705. procedure tcgx86.g_stackpointer_alloc(list : TAsmList;localsize : longint);
  1706. {$ifdef x86}
  1707. {$ifndef NOTARGETWIN}
  1708. var
  1709. href : treference;
  1710. i : integer;
  1711. again : tasmlabel;
  1712. {$endif NOTARGETWIN}
  1713. {$endif x86}
  1714. begin
  1715. if localsize>0 then
  1716. begin
  1717. {$ifdef i386}
  1718. {$ifndef NOTARGETWIN}
  1719. { windows guards only a few pages for stack growing,
  1720. so we have to access every page first }
  1721. if (target_info.system in [system_i386_win32,system_i386_wince]) and
  1722. (localsize>=winstackpagesize) then
  1723. begin
  1724. if localsize div winstackpagesize<=5 then
  1725. begin
  1726. list.concat(Taicpu.Op_const_reg(A_SUB,S_L,localsize-4,NR_ESP));
  1727. for i:=1 to localsize div winstackpagesize do
  1728. begin
  1729. reference_reset_base(href,NR_ESP,localsize-i*winstackpagesize);
  1730. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1731. end;
  1732. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1733. end
  1734. else
  1735. begin
  1736. current_asmdata.getjumplabel(again);
  1737. getcpuregister(list,NR_EDI);
  1738. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EDI));
  1739. list.concat(Taicpu.op_const_reg(A_MOV,S_L,localsize div winstackpagesize,NR_EDI));
  1740. a_label(list,again);
  1741. list.concat(Taicpu.op_const_reg(A_SUB,S_L,winstackpagesize-4,NR_ESP));
  1742. list.concat(Taicpu.op_reg(A_PUSH,S_L,NR_EAX));
  1743. list.concat(Taicpu.op_reg(A_DEC,S_L,NR_EDI));
  1744. a_jmp_cond(list,OC_NE,again);
  1745. list.concat(Taicpu.op_const_reg(A_SUB,S_L,localsize mod winstackpagesize - 4,NR_ESP));
  1746. reference_reset_base(href,NR_ESP,localsize-4);
  1747. list.concat(Taicpu.op_ref_reg(A_MOV,S_L,href,NR_EDI));
  1748. ungetcpuregister(list,NR_EDI);
  1749. end
  1750. end
  1751. else
  1752. {$endif NOTARGETWIN}
  1753. {$endif i386}
  1754. {$ifdef x86_64}
  1755. {$ifndef NOTARGETWIN}
  1756. { windows guards only a few pages for stack growing,
  1757. so we have to access every page first }
  1758. if (target_info.system=system_x86_64_win64) and
  1759. (localsize>=winstackpagesize) then
  1760. begin
  1761. if localsize div winstackpagesize<=5 then
  1762. begin
  1763. list.concat(Taicpu.Op_const_reg(A_SUB,S_Q,localsize,NR_RSP));
  1764. for i:=1 to localsize div winstackpagesize do
  1765. begin
  1766. reference_reset_base(href,NR_RSP,localsize-i*winstackpagesize+4);
  1767. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1768. end;
  1769. reference_reset_base(href,NR_RSP,0);
  1770. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1771. end
  1772. else
  1773. begin
  1774. current_asmdata.getjumplabel(again);
  1775. getcpuregister(list,NR_R10);
  1776. list.concat(Taicpu.op_const_reg(A_MOV,S_Q,localsize div winstackpagesize,NR_R10));
  1777. a_label(list,again);
  1778. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,winstackpagesize,NR_RSP));
  1779. reference_reset_base(href,NR_RSP,0);
  1780. list.concat(Taicpu.op_reg_ref(A_MOV,S_L,NR_EAX,href));
  1781. list.concat(Taicpu.op_reg(A_DEC,S_Q,NR_R10));
  1782. a_jmp_cond(list,OC_NE,again);
  1783. list.concat(Taicpu.op_const_reg(A_SUB,S_Q,localsize mod winstackpagesize,NR_RSP));
  1784. ungetcpuregister(list,NR_R10);
  1785. end
  1786. end
  1787. else
  1788. {$endif NOTARGETWIN}
  1789. {$endif x86_64}
  1790. list.concat(Taicpu.Op_const_reg(A_SUB,tcgsize2opsize[OS_ADDR],localsize,NR_STACK_POINTER_REG));
  1791. end;
  1792. end;
  1793. procedure tcgx86.g_proc_entry(list : TAsmList;localsize : longint;nostackframe:boolean);
  1794. var
  1795. stackmisalignment: longint;
  1796. begin
  1797. {$ifdef i386}
  1798. { interrupt support for i386 }
  1799. if (po_interrupt in current_procinfo.procdef.procoptions) and
  1800. { this messes up stack alignment }
  1801. (target_info.system <> system_i386_darwin) then
  1802. begin
  1803. { .... also the segment registers }
  1804. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_GS));
  1805. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_FS));
  1806. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_ES));
  1807. list.concat(Taicpu.Op_reg(A_PUSH,S_W,NR_DS));
  1808. { save the registers of an interrupt procedure }
  1809. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDI));
  1810. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ESI));
  1811. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EDX));
  1812. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_ECX));
  1813. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EBX));
  1814. list.concat(Taicpu.Op_reg(A_PUSH,S_L,NR_EAX));
  1815. end;
  1816. {$endif i386}
  1817. { save old framepointer }
  1818. if not nostackframe then
  1819. begin
  1820. { return address }
  1821. stackmisalignment := sizeof(aint);
  1822. list.concat(tai_regalloc.alloc(current_procinfo.framepointer,nil));
  1823. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1824. CGmessage(cg_d_stackframe_omited)
  1825. else
  1826. begin
  1827. { push <frame_pointer> }
  1828. inc(stackmisalignment,sizeof(aint));
  1829. include(rg[R_INTREGISTER].preserved_by_proc,RS_FRAME_POINTER_REG);
  1830. list.concat(Taicpu.op_reg(A_PUSH,tcgsize2opsize[OS_ADDR],NR_FRAME_POINTER_REG));
  1831. { Return address and FP are both on stack }
  1832. current_asmdata.asmcfi.cfa_def_cfa_offset(list,2*sizeof(aint));
  1833. current_asmdata.asmcfi.cfa_offset(list,NR_FRAME_POINTER_REG,-(2*sizeof(aint)));
  1834. list.concat(Taicpu.op_reg_reg(A_MOV,tcgsize2opsize[OS_ADDR],NR_STACK_POINTER_REG,NR_FRAME_POINTER_REG));
  1835. current_asmdata.asmcfi.cfa_def_cfa_register(list,NR_FRAME_POINTER_REG);
  1836. end;
  1837. { allocate stackframe space }
  1838. if (localsize<>0) or
  1839. ((target_info.system in [system_i386_darwin,system_x86_64_darwin,
  1840. system_x86_64_win64,system_x86_64_linux,system_x86_64_freebsd]) and
  1841. (stackmisalignment <> 0) and
  1842. ((pi_do_call in current_procinfo.flags) or
  1843. (po_assembler in current_procinfo.procdef.procoptions))) then
  1844. begin
  1845. if (target_info.system in [system_i386_darwin,system_x86_64_darwin,
  1846. system_x86_64_win64,system_x86_64_linux,system_x86_64_freebsd]) then
  1847. localsize := align(localsize+stackmisalignment,16)-stackmisalignment;
  1848. cg.g_stackpointer_alloc(list,localsize);
  1849. if current_procinfo.framepointer=NR_STACK_POINTER_REG then
  1850. current_asmdata.asmcfi.cfa_def_cfa_offset(list,localsize+sizeof(aint));
  1851. end;
  1852. end;
  1853. end;
  1854. { produces if necessary overflowcode }
  1855. procedure tcgx86.g_overflowcheck(list: TAsmList; const l:tlocation;def:tdef);
  1856. var
  1857. hl : tasmlabel;
  1858. ai : taicpu;
  1859. cond : TAsmCond;
  1860. begin
  1861. if not(cs_check_overflow in current_settings.localswitches) then
  1862. exit;
  1863. current_asmdata.getjumplabel(hl);
  1864. if not ((def.typ=pointerdef) or
  1865. ((def.typ=orddef) and
  1866. (torddef(def).ordtype in [u64bit,u16bit,u32bit,u8bit,uchar,
  1867. bool8bit,bool16bit,bool32bit,bool64bit]))) then
  1868. cond:=C_NO
  1869. else
  1870. cond:=C_NB;
  1871. ai:=Taicpu.Op_Sym(A_Jcc,S_NO,hl);
  1872. ai.SetCondition(cond);
  1873. ai.is_jmp:=true;
  1874. list.concat(ai);
  1875. a_call_name(list,'FPC_OVERFLOW');
  1876. a_label(list,hl);
  1877. end;
  1878. procedure tcgx86.g_external_wrapper(list: TAsmList; procdef: tprocdef; const externalname: string);
  1879. var
  1880. ref : treference;
  1881. sym : tasmsymbol;
  1882. begin
  1883. if (target_info.system=system_i386_darwin) then
  1884. begin
  1885. { a_jmp_name jumps to a stub which is always pic-safe on darwin }
  1886. inherited g_external_wrapper(list,procdef,externalname);
  1887. exit;
  1888. end;
  1889. sym:=current_asmdata.RefAsmSymbol(externalname);
  1890. reference_reset_symbol(ref,sym,0);
  1891. { create pic'ed? }
  1892. if (cs_create_pic in current_settings.moduleswitches) and
  1893. { darwin/x86_64's assembler doesn't want @PLT after call symbols }
  1894. (target_info.system<>system_x86_64_darwin) then
  1895. begin
  1896. { it could be that we're called from a procedure not having the
  1897. got loaded
  1898. }
  1899. g_maybe_got_init(list);
  1900. ref.refaddr:=addr_pic
  1901. end
  1902. else
  1903. ref.refaddr:=addr_full;
  1904. list.concat(taicpu.op_ref(A_JMP,S_NO,ref));
  1905. end;
  1906. end.