nx86inl.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. first_pi := nil;
  75. end;
  76. function tx86inlinenode.first_arctan_real : tnode;
  77. begin
  78. expectloc:=LOC_FPUREGISTER;
  79. first_arctan_real := nil;
  80. end;
  81. function tx86inlinenode.first_abs_real : tnode;
  82. begin
  83. if use_sse(resultdef) then
  84. expectloc:=LOC_MMREGISTER
  85. else
  86. expectloc:=LOC_FPUREGISTER;
  87. first_abs_real := nil;
  88. end;
  89. function tx86inlinenode.first_sqr_real : tnode;
  90. begin
  91. expectloc:=LOC_FPUREGISTER;
  92. first_sqr_real := nil;
  93. end;
  94. function tx86inlinenode.first_sqrt_real : tnode;
  95. begin
  96. expectloc:=LOC_FPUREGISTER;
  97. first_sqrt_real := nil;
  98. end;
  99. function tx86inlinenode.first_ln_real : tnode;
  100. begin
  101. expectloc:=LOC_FPUREGISTER;
  102. first_ln_real := nil;
  103. end;
  104. function tx86inlinenode.first_cos_real : tnode;
  105. begin
  106. expectloc:=LOC_FPUREGISTER;
  107. first_cos_real := nil;
  108. end;
  109. function tx86inlinenode.first_sin_real : tnode;
  110. begin
  111. expectloc:=LOC_FPUREGISTER;
  112. first_sin_real := nil;
  113. end;
  114. function tx86inlinenode.first_round_real : tnode;
  115. begin
  116. {$ifdef x86_64}
  117. if use_sse(left.resultdef) then
  118. expectloc:=LOC_REGISTER
  119. else
  120. {$else x86_64}
  121. expectloc:=LOC_REFERENCE;
  122. {$endif x86_64}
  123. result:=nil;
  124. end;
  125. function tx86inlinenode.first_trunc_real: tnode;
  126. begin
  127. if (cs_opt_size in current_settings.optimizerswitches)
  128. {$ifdef x86_64}
  129. and not(use_sse(left.resultdef))
  130. {$endif x86_64}
  131. then
  132. result:=inherited
  133. else
  134. begin
  135. {$ifdef x86_64}
  136. if use_sse(left.resultdef) then
  137. expectloc:=LOC_REGISTER
  138. else
  139. {$else x86_64}
  140. expectloc:=LOC_REFERENCE;
  141. {$endif x86_64}
  142. result:=nil;
  143. end;
  144. end;
  145. procedure tx86inlinenode.second_Pi;
  146. begin
  147. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  148. emit_none(A_FLDPI,S_NO);
  149. tcgx86(cg).inc_fpu_stack;
  150. location.register:=NR_FPU_RESULT_REG;
  151. end;
  152. { load the FPU into the an fpu register }
  153. procedure tx86inlinenode.load_fpu_location;
  154. begin
  155. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  156. location.register:=NR_FPU_RESULT_REG;
  157. secondpass(left);
  158. case left.location.loc of
  159. LOC_FPUREGISTER:
  160. ;
  161. LOC_CFPUREGISTER:
  162. begin
  163. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.location.size,
  164. left.location.size,left.location.register,location.register);
  165. end;
  166. LOC_REFERENCE,LOC_CREFERENCE:
  167. begin
  168. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  169. left.location.size,left.location.size,
  170. left.location.reference,location.register);
  171. end;
  172. LOC_MMREGISTER,LOC_CMMREGISTER:
  173. begin
  174. location:=left.location;
  175. location_force_fpureg(current_asmdata.CurrAsmList,location,false);
  176. end;
  177. else
  178. internalerror(309991);
  179. end;
  180. end;
  181. procedure tx86inlinenode.second_arctan_real;
  182. begin
  183. load_fpu_location;
  184. emit_none(A_FLD1,S_NO);
  185. emit_none(A_FPATAN,S_NO);
  186. end;
  187. procedure tx86inlinenode.second_abs_real;
  188. var
  189. href : treference;
  190. begin
  191. if use_sse(resultdef) then
  192. begin
  193. secondpass(left);
  194. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  195. location:=left.location;
  196. case tfloatdef(resultdef).floattype of
  197. s32real:
  198. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_ABSMASK_SINGLE'),0);
  199. s64real:
  200. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_ABSMASK_DOUBLE'),0);
  201. else
  202. internalerror(200506081);
  203. end;
  204. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ANDPS,S_XMM,href,location.register))
  205. end
  206. else
  207. begin
  208. load_fpu_location;
  209. emit_none(A_FABS,S_NO);
  210. end;
  211. end;
  212. procedure tx86inlinenode.second_round_real;
  213. var
  214. href : treference;
  215. begin
  216. {$ifdef x86_64}
  217. if use_sse(left.resultdef) then
  218. begin
  219. secondpass(left);
  220. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  221. location_reset(location,LOC_REGISTER,OS_S64);
  222. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_S64);
  223. case left.location.size of
  224. OS_F32:
  225. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTSS2SI,S_Q,left.location.register,location.register));
  226. OS_F64:
  227. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTSD2SI,S_Q,left.location.register,location.register));
  228. else
  229. internalerror(2007031402);
  230. end;
  231. end
  232. else
  233. {$endif x86_64}
  234. begin
  235. load_fpu_location;
  236. location_reset(location,LOC_REFERENCE,OS_S64);
  237. tg.GetTempTyped(current_asmdata.CurrAsmList,resultdef,tt_normal,location.reference);
  238. emit_ref(A_FISTP,S_IQ,location.reference);
  239. emit_none(A_FWAIT,S_NO);
  240. end;
  241. end;
  242. procedure tx86inlinenode.second_trunc_real;
  243. var
  244. href : treference;
  245. oldcw,newcw : treference;
  246. tempreg : tregister;
  247. begin
  248. {$ifdef x86_64}
  249. if use_sse(left.resultdef) then
  250. begin
  251. secondpass(left);
  252. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  253. location_reset(location,LOC_REGISTER,OS_S64);
  254. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_S64);
  255. case left.location.size of
  256. OS_F32:
  257. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTTSS2SI,S_Q,left.location.register,location.register));
  258. OS_F64:
  259. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTTSD2SI,S_Q,left.location.register,location.register));
  260. else
  261. internalerror(2007031401);
  262. end;
  263. end
  264. else
  265. {$endif x86_64}
  266. begin
  267. tg.GetTemp(current_asmdata.CurrAsmList,2,tt_normal,oldcw);
  268. tg.GetTemp(current_asmdata.CurrAsmList,2,tt_normal,newcw);
  269. emit_ref(A_FNSTCW,S_NO,newcw);
  270. emit_ref(A_FNSTCW,S_NO,oldcw);
  271. emit_const_ref(A_OR,S_W,$0f00,newcw);
  272. load_fpu_location;
  273. emit_ref(A_FLDCW,S_NO,newcw);
  274. location_reset(location,LOC_REFERENCE,OS_S64);
  275. tg.GetTempTyped(current_asmdata.CurrAsmList,resultdef,tt_normal,location.reference);
  276. emit_ref(A_FISTP,S_IQ,location.reference);
  277. emit_ref(A_FLDCW,S_NO,oldcw);
  278. emit_none(A_FWAIT,S_NO);
  279. tg.UnGetTemp(current_asmdata.CurrAsmList,oldcw);
  280. tg.UnGetTemp(current_asmdata.CurrAsmList,newcw);
  281. end;
  282. end;
  283. procedure tx86inlinenode.second_sqr_real;
  284. begin
  285. if use_sse(resultdef) then
  286. begin
  287. secondpass(left);
  288. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  289. location:=left.location;
  290. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,OP_MUL,left.location.size,left.location,left.location.register,mms_movescalar);
  291. end
  292. else
  293. begin
  294. load_fpu_location;
  295. emit_reg_reg(A_FMUL,S_NO,NR_ST0,NR_ST0);
  296. end;
  297. end;
  298. procedure tx86inlinenode.second_sqrt_real;
  299. begin
  300. if use_sse(resultdef) then
  301. begin
  302. secondpass(left);
  303. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  304. location:=left.location;
  305. case tfloatdef(resultdef).floattype of
  306. s32real:
  307. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SQRTSS,S_XMM,location.register,location.register));
  308. s64real:
  309. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SQRTSD,S_XMM,location.register,location.register));
  310. else
  311. internalerror(200510031);
  312. end;
  313. end
  314. else
  315. begin
  316. load_fpu_location;
  317. emit_none(A_FSQRT,S_NO);
  318. end;
  319. end;
  320. procedure tx86inlinenode.second_ln_real;
  321. begin
  322. load_fpu_location;
  323. emit_none(A_FLDLN2,S_NO);
  324. emit_none(A_FXCH,S_NO);
  325. emit_none(A_FYL2X,S_NO);
  326. end;
  327. procedure tx86inlinenode.second_cos_real;
  328. begin
  329. load_fpu_location;
  330. emit_none(A_FCOS,S_NO);
  331. end;
  332. procedure tx86inlinenode.second_sin_real;
  333. begin
  334. load_fpu_location;
  335. emit_none(A_FSIN,S_NO)
  336. end;
  337. procedure tx86inlinenode.second_prefetch;
  338. var
  339. ref : treference;
  340. r : tregister;
  341. begin
  342. {$ifdef i386}
  343. if current_settings.cputype>=cpu_Pentium3 then
  344. {$endif i386}
  345. begin
  346. secondpass(left);
  347. case left.location.loc of
  348. LOC_CREFERENCE,
  349. LOC_REFERENCE:
  350. begin
  351. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  352. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,r);
  353. reference_reset_base(ref,r,0);
  354. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_PREFETCHNTA,S_NO,ref));
  355. end;
  356. else
  357. internalerror(200402021);
  358. end;
  359. end;
  360. end;
  361. {*****************************************************************************
  362. INCLUDE/EXCLUDE GENERIC HANDLING
  363. *****************************************************************************}
  364. procedure tx86inlinenode.second_IncludeExclude;
  365. var
  366. hregister : tregister;
  367. setbase : aint;
  368. bitsperop,l : longint;
  369. cgop : topcg;
  370. asmop : tasmop;
  371. opsize : tcgsize;
  372. begin
  373. if not(is_varset(tcallparanode(left).resultdef)) then
  374. opsize:=int_cgsize(tcallparanode(left).resultdef.size)
  375. else
  376. opsize:=OS_32;
  377. bitsperop:=(8*tcgsize2size[opsize]);
  378. secondpass(tcallparanode(left).left);
  379. secondpass(tcallparanode(tcallparanode(left).right).left);
  380. setbase:=tsetdef(tcallparanode(left).left.resultdef).setbase;
  381. if tcallparanode(tcallparanode(left).right).left.location.loc=LOC_CONSTANT then
  382. begin
  383. { calculate bit position }
  384. l:=1 shl ((tcallparanode(tcallparanode(left).right).left.location.value-setbase) mod bitsperop);
  385. { determine operator }
  386. if inlinenumber=in_include_x_y then
  387. cgop:=OP_OR
  388. else
  389. begin
  390. cgop:=OP_AND;
  391. l:=not(l);
  392. end;
  393. case tcallparanode(left).left.location.loc of
  394. LOC_REFERENCE :
  395. begin
  396. inc(tcallparanode(left).left.location.reference.offset,
  397. ((tcallparanode(tcallparanode(left).right).left.location.value-setbase) div bitsperop)*tcgsize2size[opsize]);
  398. cg.a_op_const_ref(current_asmdata.CurrAsmList,cgop,opsize,l,tcallparanode(left).left.location.reference);
  399. end;
  400. LOC_CREGISTER :
  401. cg.a_op_const_reg(current_asmdata.CurrAsmList,cgop,tcallparanode(left).left.location.size,l,tcallparanode(left).left.location.register);
  402. else
  403. internalerror(200405022);
  404. end;
  405. end
  406. else
  407. begin
  408. if opsize in [OS_8,OS_S8] then
  409. opsize:=OS_32;
  410. { determine asm operator }
  411. if inlinenumber=in_include_x_y then
  412. asmop:=A_BTS
  413. else
  414. asmop:=A_BTR;
  415. location_force_reg(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,opsize,true);
  416. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,setbase);
  417. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  418. if (tcallparanode(left).left.location.loc=LOC_REFERENCE) then
  419. emit_reg_ref(asmop,tcgsize2opsize[opsize],hregister,tcallparanode(left).left.location.reference)
  420. else
  421. emit_reg_reg(asmop,tcgsize2opsize[opsize],hregister,tcallparanode(left).left.location.register);
  422. end;
  423. end;
  424. end.