nx86inl.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate x86 inline nodes
  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. unit nx86inl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ninl,ncginl;
  22. type
  23. tx86inlinenode = class(tcginlinenode)
  24. { first pass override
  25. so that the code generator will actually generate
  26. these nodes.
  27. }
  28. function first_pi: tnode ; override;
  29. function first_arctan_real: tnode; override;
  30. function first_abs_real: tnode; override;
  31. function first_sqr_real: tnode; override;
  32. function first_sqrt_real: tnode; override;
  33. function first_ln_real: tnode; override;
  34. function first_cos_real: tnode; override;
  35. function first_sin_real: tnode; override;
  36. function first_round_real: tnode; override;
  37. function first_trunc_real: tnode; override;
  38. { second pass override to generate these nodes }
  39. procedure second_IncludeExclude;override;
  40. procedure second_pi; override;
  41. procedure second_arctan_real; override;
  42. procedure second_abs_real; override;
  43. procedure second_round_real; override;
  44. procedure second_sqr_real; override;
  45. procedure second_sqrt_real; override;
  46. procedure second_ln_real; override;
  47. procedure second_cos_real; override;
  48. procedure second_sin_real; override;
  49. procedure second_trunc_real; override;
  50. procedure second_prefetch;override;
  51. private
  52. procedure load_fpu_location;
  53. end;
  54. implementation
  55. uses
  56. systems,
  57. globtype,globals,
  58. cutils,verbose,
  59. symconst,
  60. defutil,
  61. aasmbase,aasmtai,aasmdata,aasmcpu,
  62. symdef,
  63. cgbase,pass_2,
  64. cpuinfo,cpubase,paramgr,
  65. nbas,ncon,ncal,ncnv,nld,ncgutil,
  66. tgobj,
  67. cga,cgutils,cgx86,cgobj;
  68. {*****************************************************************************
  69. TX86INLINENODE
  70. *****************************************************************************}
  71. function tx86inlinenode.first_pi : tnode;
  72. begin
  73. expectloc:=LOC_FPUREGISTER;
  74. registersfpu:=1;
  75. first_pi := nil;
  76. end;
  77. function tx86inlinenode.first_arctan_real : tnode;
  78. begin
  79. expectloc:=LOC_FPUREGISTER;
  80. registersint:=left.registersint;
  81. registersfpu:=max(left.registersfpu,2);
  82. {$ifdef SUPPORT_MMX}
  83. registersmmx:=left.registersmmx;
  84. {$endif SUPPORT_MMX}
  85. first_arctan_real := nil;
  86. end;
  87. function tx86inlinenode.first_abs_real : tnode;
  88. begin
  89. if use_sse(resultdef) then
  90. begin
  91. expectloc:=LOC_MMREGISTER;
  92. registersmm:=max(left.registersmm,1);
  93. end
  94. else
  95. begin
  96. expectloc:=LOC_FPUREGISTER;
  97. registersfpu:=max(left.registersfpu,1);
  98. end;
  99. registersint:=left.registersint;
  100. {$ifdef SUPPORT_MMX}
  101. registersmmx:=left.registersmmx;
  102. {$endif SUPPORT_MMX}
  103. first_abs_real := nil;
  104. end;
  105. function tx86inlinenode.first_sqr_real : tnode;
  106. begin
  107. expectloc:=LOC_FPUREGISTER;
  108. registersint:=left.registersint;
  109. registersfpu:=max(left.registersfpu,1);
  110. {$ifdef SUPPORT_MMX}
  111. registersmmx:=left.registersmmx;
  112. {$endif SUPPORT_MMX}
  113. first_sqr_real := nil;
  114. end;
  115. function tx86inlinenode.first_sqrt_real : tnode;
  116. begin
  117. expectloc:=LOC_FPUREGISTER;
  118. registersint:=left.registersint;
  119. registersfpu:=max(left.registersfpu,1);
  120. {$ifdef SUPPORT_MMX}
  121. registersmmx:=left.registersmmx;
  122. {$endif SUPPORT_MMX}
  123. first_sqrt_real := nil;
  124. end;
  125. function tx86inlinenode.first_ln_real : tnode;
  126. begin
  127. expectloc:=LOC_FPUREGISTER;
  128. registersint:=left.registersint;
  129. registersfpu:=max(left.registersfpu,2);
  130. {$ifdef SUPPORT_MMX}
  131. registersmmx:=left.registersmmx;
  132. {$endif SUPPORT_MMX}
  133. first_ln_real := nil;
  134. end;
  135. function tx86inlinenode.first_cos_real : tnode;
  136. begin
  137. expectloc:=LOC_FPUREGISTER;
  138. registersint:=left.registersint;
  139. registersfpu:=max(left.registersfpu,1);
  140. {$ifdef SUPPORT_MMX}
  141. registersmmx:=left.registersmmx;
  142. {$endif SUPPORT_MMX}
  143. first_cos_real := nil;
  144. end;
  145. function tx86inlinenode.first_sin_real : tnode;
  146. begin
  147. expectloc:=LOC_FPUREGISTER;
  148. registersint:=left.registersint;
  149. registersfpu:=max(left.registersfpu,1);
  150. {$ifdef SUPPORT_MMX}
  151. registersmmx:=left.registersmmx;
  152. {$endif SUPPORT_MMX}
  153. first_sin_real := nil;
  154. end;
  155. function tx86inlinenode.first_round_real : tnode;
  156. begin
  157. expectloc:=LOC_REFERENCE;
  158. registersint:=left.registersint;
  159. registersfpu:=max(left.registersfpu,1);
  160. {$ifdef SUPPORT_MMX}
  161. registersmmx:=left.registersmmx;
  162. {$endif SUPPORT_MMX}
  163. result:=nil;
  164. end;
  165. function tx86inlinenode.first_trunc_real: tnode;
  166. begin
  167. if cs_opt_size in current_settings.optimizerswitches then
  168. result:=inherited
  169. else
  170. begin
  171. expectloc:=LOC_REFERENCE;
  172. registersint:=left.registersint;
  173. registersfpu:=max(left.registersfpu,1);
  174. {$ifdef SUPPORT_MMX}
  175. registersmmx:=left.registersmmx;
  176. {$endif SUPPORT_MMX}
  177. result:=nil;
  178. end;
  179. end;
  180. procedure tx86inlinenode.second_Pi;
  181. begin
  182. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  183. emit_none(A_FLDPI,S_NO);
  184. tcgx86(cg).inc_fpu_stack;
  185. location.register:=NR_FPU_RESULT_REG;
  186. end;
  187. { load the FPU into the an fpu register }
  188. procedure tx86inlinenode.load_fpu_location;
  189. begin
  190. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  191. location.register:=NR_FPU_RESULT_REG;
  192. secondpass(left);
  193. case left.location.loc of
  194. LOC_FPUREGISTER:
  195. ;
  196. LOC_CFPUREGISTER:
  197. begin
  198. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.location.size,
  199. left.location.size,left.location.register,location.register);
  200. end;
  201. LOC_REFERENCE,LOC_CREFERENCE:
  202. begin
  203. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  204. left.location.size,left.location.size,
  205. left.location.reference,location.register);
  206. end;
  207. LOC_MMREGISTER,LOC_CMMREGISTER:
  208. begin
  209. location:=left.location;
  210. location_force_fpureg(current_asmdata.CurrAsmList,location,false);
  211. end;
  212. else
  213. internalerror(309991);
  214. end;
  215. end;
  216. procedure tx86inlinenode.second_arctan_real;
  217. begin
  218. load_fpu_location;
  219. emit_none(A_FLD1,S_NO);
  220. emit_none(A_FPATAN,S_NO);
  221. end;
  222. procedure tx86inlinenode.second_abs_real;
  223. var
  224. href : treference;
  225. begin
  226. if use_sse(resultdef) then
  227. begin
  228. secondpass(left);
  229. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  230. location:=left.location;
  231. case tfloatdef(resultdef).floattype of
  232. s32real:
  233. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_ABSMASK_SINGLE'),0);
  234. s64real:
  235. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_ABSMASK_DOUBLE'),0);
  236. else
  237. internalerror(200506081);
  238. end;
  239. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ANDPS,S_XMM,href,location.register))
  240. end
  241. else
  242. begin
  243. load_fpu_location;
  244. emit_none(A_FABS,S_NO);
  245. end;
  246. end;
  247. procedure tx86inlinenode.second_round_real;
  248. var
  249. href : treference;
  250. begin
  251. {
  252. if use_sse(left.resultdef) then
  253. begin
  254. secondpass(left);
  255. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  256. location.loc:=LOC_REFERENCE;
  257. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg_const(A_PSHUFD,S_XMM,location.left.register,location.left.register))
  258. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg_const(A_PSHUFD,S_XMM,location.left.register,location.left.register))
  259. tg.GetTempTyped(current_asmdata.CurrAsmList,left.resultdef,tt_normal,location.reference);
  260. end
  261. else
  262. }
  263. begin
  264. load_fpu_location;
  265. location_reset(location,LOC_REFERENCE,OS_64);
  266. tg.GetTempTyped(current_asmdata.CurrAsmList,resultdef,tt_normal,location.reference);
  267. emit_ref(A_FISTP,S_Q,location.reference);
  268. emit_none(A_FWAIT,S_NO);
  269. end;
  270. end;
  271. procedure tx86inlinenode.second_trunc_real;
  272. var
  273. href : treference;
  274. oldcw : treference;
  275. tempreg : tregister;
  276. begin
  277. {
  278. if use_sse(left.resultdef) then
  279. begin
  280. secondpass(left);
  281. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  282. location.loc:=LOC_REFERENCE;
  283. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg_const(A_PSHUFD,S_XMM,location.left.register,location.left.register))
  284. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg_const(A_PSHUFD,S_XMM,location.left.register,location.left.register))
  285. tg.GetTempTyped(current_asmdata.CurrAsmList,left.resultdef,tt_normal,location.reference);
  286. end
  287. else
  288. }
  289. begin
  290. tg.GetTemp(current_asmdata.CurrAsmList,2,tt_normal,oldcw);
  291. emit_ref(A_FNSTCW,S_NO,oldcw);
  292. tempreg:=cg.GetIntRegister(current_asmdata.CurrAsmList,OS_16);
  293. cg.a_load_ref_reg(current_asmdata.CurrAsmList,OS_16,OS_16,oldcw,tempreg);
  294. emit_const_ref(A_OR,S_W,$0f00,oldcw);
  295. load_fpu_location;
  296. emit_ref(A_FLDCW,S_NO,oldcw);
  297. location_reset(location,LOC_REFERENCE,OS_64);
  298. tg.GetTempTyped(current_asmdata.CurrAsmList,resultdef,tt_normal,location.reference);
  299. cg.a_load_reg_ref(current_asmdata.CurrAsmList,OS_16,OS_16,tempreg,oldcw);
  300. emit_ref(A_FISTP,S_Q,location.reference);
  301. emit_ref(A_FLDCW,S_NO,oldcw);
  302. emit_none(A_FWAIT,S_NO);
  303. tg.UnGetTemp(current_asmdata.CurrAsmList,oldcw);
  304. end;
  305. end;
  306. procedure tx86inlinenode.second_sqr_real;
  307. begin
  308. if use_sse(resultdef) then
  309. begin
  310. secondpass(left);
  311. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  312. location:=left.location;
  313. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,OP_MUL,left.location.size,left.location,left.location.register,mms_movescalar);
  314. end
  315. else
  316. begin
  317. load_fpu_location;
  318. emit_reg_reg(A_FMUL,S_NO,NR_ST0,NR_ST0);
  319. end;
  320. end;
  321. procedure tx86inlinenode.second_sqrt_real;
  322. begin
  323. if use_sse(resultdef) then
  324. begin
  325. secondpass(left);
  326. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  327. location:=left.location;
  328. case tfloatdef(resultdef).floattype of
  329. s32real:
  330. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SQRTSS,S_XMM,location.register,location.register));
  331. s64real:
  332. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SQRTSD,S_XMM,location.register,location.register));
  333. else
  334. internalerror(200510031);
  335. end;
  336. end
  337. else
  338. begin
  339. load_fpu_location;
  340. emit_none(A_FSQRT,S_NO);
  341. end;
  342. end;
  343. procedure tx86inlinenode.second_ln_real;
  344. begin
  345. load_fpu_location;
  346. emit_none(A_FLDLN2,S_NO);
  347. emit_none(A_FXCH,S_NO);
  348. emit_none(A_FYL2X,S_NO);
  349. end;
  350. procedure tx86inlinenode.second_cos_real;
  351. begin
  352. load_fpu_location;
  353. emit_none(A_FCOS,S_NO);
  354. end;
  355. procedure tx86inlinenode.second_sin_real;
  356. begin
  357. load_fpu_location;
  358. emit_none(A_FSIN,S_NO)
  359. end;
  360. procedure tx86inlinenode.second_prefetch;
  361. var
  362. ref : treference;
  363. r : tregister;
  364. begin
  365. {$ifdef i386}
  366. if current_settings.cputype>=cpu_Pentium3 then
  367. {$endif i386}
  368. begin
  369. secondpass(left);
  370. case left.location.loc of
  371. LOC_CREFERENCE,
  372. LOC_REFERENCE:
  373. begin
  374. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  375. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,r);
  376. reference_reset_base(ref,r,0);
  377. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_PREFETCHNTA,S_NO,ref));
  378. end;
  379. else
  380. internalerror(200402021);
  381. end;
  382. end;
  383. end;
  384. {*****************************************************************************
  385. INCLUDE/EXCLUDE GENERIC HANDLING
  386. *****************************************************************************}
  387. procedure tx86inlinenode.second_IncludeExclude;
  388. var
  389. hregister : tregister;
  390. asmop : tasmop;
  391. bitsperop,l : longint;
  392. cgop : topcg;
  393. opsize : tcgsize;
  394. begin
  395. if not(is_varset(tcallparanode(left).resultdef)) then
  396. opsize:=int_cgsize(tcallparanode(left).resultdef.size)
  397. else
  398. opsize:=OS_32;
  399. bitsperop:=(8*tcgsize2size[opsize]);
  400. secondpass(tcallparanode(left).left);
  401. if tcallparanode(tcallparanode(left).right).left.nodetype=ordconstn then
  402. begin
  403. { calculate bit position }
  404. l:=1 shl (tordconstnode(tcallparanode(tcallparanode(left).right).left).value mod bitsperop);
  405. { determine operator }
  406. if inlinenumber=in_include_x_y then
  407. cgop:=OP_OR
  408. else
  409. begin
  410. cgop:=OP_AND;
  411. l:=not(l);
  412. end;
  413. case tcallparanode(left).left.location.loc of
  414. LOC_REFERENCE :
  415. begin
  416. inc(tcallparanode(left).left.location.reference.offset,
  417. (tordconstnode(tcallparanode(tcallparanode(left).right).left).value div bitsperop)*tcgsize2size[opsize]);
  418. cg.a_op_const_ref(current_asmdata.CurrAsmList,cgop,opsize,l,tcallparanode(left).left.location.reference);
  419. end;
  420. LOC_CREGISTER :
  421. cg.a_op_const_reg(current_asmdata.CurrAsmList,cgop,tcallparanode(left).left.location.size,l,tcallparanode(left).left.location.register);
  422. else
  423. internalerror(200405022);
  424. end;
  425. end
  426. else
  427. begin
  428. if opsize=OS_8 then
  429. opsize:=OS_32;
  430. { generate code for the element to set }
  431. secondpass(tcallparanode(tcallparanode(left).right).left);
  432. { determine asm operator }
  433. if inlinenumber=in_include_x_y then
  434. asmop:=A_BTS
  435. else
  436. asmop:=A_BTR;
  437. if tcallparanode(tcallparanode(left).right).left.location.loc in [LOC_CREGISTER,LOC_REGISTER] then
  438. { we don't need a mod 32 because this is done automatically }
  439. { by the bts instruction. For proper checking we would }
  440. { note: bts doesn't do any mod'ing, that's why we can also use }
  441. { it for normalsets! (JM) }
  442. { need a cmp and jmp, but this should be done by the }
  443. { type cast code which does range checking if necessary (FK) }
  444. hregister:=cg.makeregsize(current_asmdata.CurrAsmList,Tcallparanode(Tcallparanode(left).right).left.location.register,opsize)
  445. else
  446. hregister:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  447. cg.a_load_loc_reg(current_asmdata.CurrAsmList,opsize,tcallparanode(tcallparanode(left).right).left.location,hregister);
  448. if (tcallparanode(left).left.location.loc=LOC_REFERENCE) then
  449. emit_reg_ref(asmop,tcgsize2opsize[opsize],hregister,tcallparanode(left).left.location.reference)
  450. else
  451. emit_reg_reg(asmop,tcgsize2opsize[opsize],hregister,tcallparanode(left).left.location.register);
  452. end;
  453. end;
  454. end.