2
0

cgx86.pas 68 KB

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