cgx86.pas 74 KB

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