nx86inl.pas 16 KB

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