nx86inl.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. procedure second_abs_long;override;
  52. private
  53. procedure load_fpu_location;
  54. end;
  55. implementation
  56. uses
  57. systems,
  58. globtype,globals,
  59. cutils,verbose,
  60. symconst,
  61. defutil,
  62. aasmbase,aasmtai,aasmdata,aasmcpu,
  63. symdef,
  64. cgbase,pass_2,
  65. cpuinfo,cpubase,paramgr,
  66. nbas,ncon,ncal,ncnv,nld,ncgutil,
  67. tgobj,
  68. cga,cgutils,cgx86,cgobj;
  69. {*****************************************************************************
  70. TX86INLINENODE
  71. *****************************************************************************}
  72. function tx86inlinenode.first_pi : tnode;
  73. begin
  74. expectloc:=LOC_FPUREGISTER;
  75. first_pi := nil;
  76. end;
  77. function tx86inlinenode.first_arctan_real : tnode;
  78. begin
  79. expectloc:=LOC_FPUREGISTER;
  80. first_arctan_real := nil;
  81. end;
  82. function tx86inlinenode.first_abs_real : tnode;
  83. begin
  84. if use_sse(resultdef) then
  85. expectloc:=LOC_MMREGISTER
  86. else
  87. expectloc:=LOC_FPUREGISTER;
  88. first_abs_real := nil;
  89. end;
  90. function tx86inlinenode.first_sqr_real : tnode;
  91. begin
  92. expectloc:=LOC_FPUREGISTER;
  93. first_sqr_real := nil;
  94. end;
  95. function tx86inlinenode.first_sqrt_real : tnode;
  96. begin
  97. expectloc:=LOC_FPUREGISTER;
  98. first_sqrt_real := nil;
  99. end;
  100. function tx86inlinenode.first_ln_real : tnode;
  101. begin
  102. expectloc:=LOC_FPUREGISTER;
  103. first_ln_real := nil;
  104. end;
  105. function tx86inlinenode.first_cos_real : tnode;
  106. begin
  107. expectloc:=LOC_FPUREGISTER;
  108. first_cos_real := nil;
  109. end;
  110. function tx86inlinenode.first_sin_real : tnode;
  111. begin
  112. expectloc:=LOC_FPUREGISTER;
  113. first_sin_real := nil;
  114. end;
  115. function tx86inlinenode.first_round_real : tnode;
  116. begin
  117. {$ifdef x86_64}
  118. if use_sse(left.resultdef) then
  119. expectloc:=LOC_REGISTER
  120. else
  121. {$endif x86_64}
  122. expectloc:=LOC_REFERENCE;
  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. {$endif x86_64}
  140. expectloc:=LOC_REFERENCE;
  141. result:=nil;
  142. end;
  143. end;
  144. procedure tx86inlinenode.second_Pi;
  145. begin
  146. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  147. emit_none(A_FLDPI,S_NO);
  148. tcgx86(cg).inc_fpu_stack;
  149. location.register:=NR_FPU_RESULT_REG;
  150. end;
  151. { load the FPU into the an fpu register }
  152. procedure tx86inlinenode.load_fpu_location;
  153. begin
  154. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  155. location.register:=NR_FPU_RESULT_REG;
  156. secondpass(left);
  157. case left.location.loc of
  158. LOC_FPUREGISTER:
  159. ;
  160. LOC_CFPUREGISTER:
  161. begin
  162. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.location.size,
  163. left.location.size,left.location.register,location.register);
  164. end;
  165. LOC_REFERENCE,LOC_CREFERENCE:
  166. begin
  167. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  168. left.location.size,left.location.size,
  169. left.location.reference,location.register);
  170. end;
  171. LOC_MMREGISTER,LOC_CMMREGISTER:
  172. begin
  173. location:=left.location;
  174. location_force_fpureg(current_asmdata.CurrAsmList,location,false);
  175. end;
  176. else
  177. internalerror(309991);
  178. end;
  179. end;
  180. procedure tx86inlinenode.second_arctan_real;
  181. begin
  182. load_fpu_location;
  183. emit_none(A_FLD1,S_NO);
  184. emit_none(A_FPATAN,S_NO);
  185. end;
  186. procedure tx86inlinenode.second_abs_real;
  187. var
  188. href : treference;
  189. begin
  190. if use_sse(resultdef) then
  191. begin
  192. secondpass(left);
  193. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  194. location:=left.location;
  195. case tfloatdef(resultdef).floattype of
  196. s32real:
  197. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_ABSMASK_SINGLE'),0,4);
  198. s64real:
  199. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_ABSMASK_DOUBLE'),0,4);
  200. else
  201. internalerror(200506081);
  202. end;
  203. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList, href);
  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. 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_ref(location,LOC_REFERENCE,OS_S64,0);
  235. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,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. oldcw,newcw : treference;
  243. begin
  244. {$ifdef x86_64}
  245. if use_sse(left.resultdef) and
  246. not((left.location.loc=LOC_FPUREGISTER) and (current_settings.fputype>=fpu_sse3)) then
  247. begin
  248. secondpass(left);
  249. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  250. location_reset(location,LOC_REGISTER,OS_S64);
  251. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_S64);
  252. case left.location.size of
  253. OS_F32:
  254. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTTSS2SI,S_Q,left.location.register,location.register));
  255. OS_F64:
  256. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTTSD2SI,S_Q,left.location.register,location.register));
  257. else
  258. internalerror(2007031401);
  259. end;
  260. end
  261. else
  262. {$endif x86_64}
  263. begin
  264. if (current_settings.fputype>=fpu_sse3) then
  265. begin
  266. load_fpu_location;
  267. location_reset_ref(location,LOC_REFERENCE,OS_S64,0);
  268. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  269. emit_ref(A_FISTTP,S_IQ,location.reference);
  270. end
  271. else
  272. begin
  273. tg.GetTemp(current_asmdata.CurrAsmList,2,2,tt_normal,oldcw);
  274. tg.GetTemp(current_asmdata.CurrAsmList,2,2,tt_normal,newcw);
  275. emit_ref(A_FNSTCW,S_NO,newcw);
  276. emit_ref(A_FNSTCW,S_NO,oldcw);
  277. emit_const_ref(A_OR,S_W,$0f00,newcw);
  278. load_fpu_location;
  279. emit_ref(A_FLDCW,S_NO,newcw);
  280. location_reset_ref(location,LOC_REFERENCE,OS_S64,0);
  281. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  282. emit_ref(A_FISTP,S_IQ,location.reference);
  283. emit_ref(A_FLDCW,S_NO,oldcw);
  284. emit_none(A_FWAIT,S_NO);
  285. tg.UnGetTemp(current_asmdata.CurrAsmList,oldcw);
  286. tg.UnGetTemp(current_asmdata.CurrAsmList,newcw);
  287. end;
  288. end;
  289. end;
  290. procedure tx86inlinenode.second_sqr_real;
  291. begin
  292. if use_sse(resultdef) then
  293. begin
  294. secondpass(left);
  295. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  296. location:=left.location;
  297. cg.a_opmm_loc_reg(current_asmdata.CurrAsmList,OP_MUL,left.location.size,left.location,left.location.register,mms_movescalar);
  298. end
  299. else
  300. begin
  301. load_fpu_location;
  302. emit_reg_reg(A_FMUL,S_NO,NR_ST0,NR_ST0);
  303. end;
  304. end;
  305. procedure tx86inlinenode.second_sqrt_real;
  306. begin
  307. if use_sse(resultdef) then
  308. begin
  309. secondpass(left);
  310. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  311. location:=left.location;
  312. case tfloatdef(resultdef).floattype of
  313. s32real:
  314. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SQRTSS,S_XMM,location.register,location.register));
  315. s64real:
  316. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SQRTSD,S_XMM,location.register,location.register));
  317. else
  318. internalerror(200510031);
  319. end;
  320. end
  321. else
  322. begin
  323. load_fpu_location;
  324. emit_none(A_FSQRT,S_NO);
  325. end;
  326. end;
  327. procedure tx86inlinenode.second_ln_real;
  328. begin
  329. load_fpu_location;
  330. emit_none(A_FLDLN2,S_NO);
  331. emit_none(A_FXCH,S_NO);
  332. emit_none(A_FYL2X,S_NO);
  333. end;
  334. procedure tx86inlinenode.second_cos_real;
  335. begin
  336. load_fpu_location;
  337. emit_none(A_FCOS,S_NO);
  338. end;
  339. procedure tx86inlinenode.second_sin_real;
  340. begin
  341. load_fpu_location;
  342. emit_none(A_FSIN,S_NO)
  343. end;
  344. procedure tx86inlinenode.second_prefetch;
  345. var
  346. ref : treference;
  347. r : tregister;
  348. begin
  349. {$ifdef i386}
  350. if current_settings.cputype>=cpu_Pentium3 then
  351. {$endif i386}
  352. begin
  353. secondpass(left);
  354. case left.location.loc of
  355. LOC_CREFERENCE,
  356. LOC_REFERENCE:
  357. begin
  358. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  359. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,r);
  360. reference_reset_base(ref,r,0,left.location.reference.alignment);
  361. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_PREFETCHNTA,S_NO,ref));
  362. end;
  363. else
  364. internalerror(200402021);
  365. end;
  366. end;
  367. end;
  368. procedure tx86inlinenode.second_abs_long;
  369. var
  370. hregister : tregister;
  371. opsize : tcgsize;
  372. hp : taicpu;
  373. begin
  374. {$ifdef i386}
  375. if current_settings.cputype<cpu_Pentium2 then
  376. begin
  377. opsize:=def_cgsize(left.resultdef);
  378. secondpass(left);
  379. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,false);
  380. location:=left.location;
  381. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  382. emit_reg_reg(A_MOV,S_L,left.location.register,location.register);
  383. emit_const_reg(A_SAR,tcgsize2opsize[opsize],31,left.location.register);
  384. emit_reg_reg(A_XOR,S_L,left.location.register,location.register);
  385. emit_reg_reg(A_SUB,S_L,left.location.register,location.register);
  386. end
  387. else
  388. {$endif i386}
  389. begin
  390. opsize:=def_cgsize(left.resultdef);
  391. secondpass(left);
  392. location_force_reg(current_asmdata.CurrAsmList,left.location,opsize,true);
  393. hregister:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  394. location:=left.location;
  395. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  396. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,hregister);
  397. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,location.register);
  398. emit_reg(A_NEG,tcgsize2opsize[opsize],hregister);
  399. hp:=taicpu.op_reg_reg(A_CMOVcc,tcgsize2opsize[opsize],hregister,location.register);
  400. hp.condition:=C_NS;
  401. current_asmdata.CurrAsmList.concat(hp);
  402. end;
  403. end;
  404. {*****************************************************************************
  405. INCLUDE/EXCLUDE GENERIC HANDLING
  406. *****************************************************************************}
  407. procedure tx86inlinenode.second_IncludeExclude;
  408. var
  409. hregister,
  410. hregister2: tregister;
  411. setbase : aint;
  412. bitsperop,l : longint;
  413. cgop : topcg;
  414. asmop : tasmop;
  415. opsize,
  416. orgsize: tcgsize;
  417. begin
  418. if is_smallset(tcallparanode(left).resultdef) then
  419. opsize:=int_cgsize(tcallparanode(left).resultdef.size)
  420. else
  421. opsize:=OS_32;
  422. bitsperop:=(8*tcgsize2size[opsize]);
  423. secondpass(tcallparanode(left).left);
  424. secondpass(tcallparanode(tcallparanode(left).right).left);
  425. setbase:=tsetdef(tcallparanode(left).left.resultdef).setbase;
  426. if tcallparanode(tcallparanode(left).right).left.location.loc=LOC_CONSTANT then
  427. begin
  428. { calculate bit position }
  429. l:=1 shl ((tcallparanode(tcallparanode(left).right).left.location.value-setbase) mod bitsperop);
  430. { determine operator }
  431. if inlinenumber=in_include_x_y then
  432. cgop:=OP_OR
  433. else
  434. begin
  435. cgop:=OP_AND;
  436. l:=not(l);
  437. end;
  438. case tcallparanode(left).left.location.loc of
  439. LOC_REFERENCE :
  440. begin
  441. inc(tcallparanode(left).left.location.reference.offset,
  442. ((tcallparanode(tcallparanode(left).right).left.location.value-setbase) div bitsperop)*tcgsize2size[opsize]);
  443. cg.a_op_const_ref(current_asmdata.CurrAsmList,cgop,opsize,l,tcallparanode(left).left.location.reference);
  444. end;
  445. LOC_CREGISTER :
  446. cg.a_op_const_reg(current_asmdata.CurrAsmList,cgop,tcallparanode(left).left.location.size,l,tcallparanode(left).left.location.register);
  447. else
  448. internalerror(200405022);
  449. end;
  450. end
  451. else
  452. begin
  453. orgsize:=opsize;
  454. if opsize in [OS_8,OS_S8] then
  455. begin
  456. opsize:=OS_32;
  457. end;
  458. { determine asm operator }
  459. if inlinenumber=in_include_x_y then
  460. asmop:=A_BTS
  461. else
  462. asmop:=A_BTR;
  463. location_force_reg(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,opsize,true);
  464. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,setbase);
  465. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  466. if (tcallparanode(left).left.location.loc=LOC_REFERENCE) then
  467. emit_reg_ref(asmop,tcgsize2opsize[opsize],hregister,tcallparanode(left).left.location.reference)
  468. else
  469. begin
  470. { second argument can't be an 8 bit register either }
  471. hregister2:=tcallparanode(left).left.location.register;
  472. if (orgsize in [OS_8,OS_S8]) then
  473. hregister2:=cg.makeregsize(current_asmdata.CurrAsmList,hregister2,opsize);
  474. emit_reg_reg(asmop,tcgsize2opsize[opsize],hregister,hregister2);
  475. end;
  476. end;
  477. end;
  478. end.