nx86inl.pas 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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. function first_popcnt: tnode; override;
  39. { second pass override to generate these nodes }
  40. procedure second_IncludeExclude;override;
  41. procedure second_pi; override;
  42. procedure second_arctan_real; override;
  43. procedure second_abs_real; override;
  44. procedure second_round_real; override;
  45. procedure second_sqr_real; override;
  46. procedure second_sqrt_real; override;
  47. procedure second_ln_real; override;
  48. procedure second_cos_real; override;
  49. procedure second_sin_real; override;
  50. procedure second_trunc_real; override;
  51. procedure second_prefetch;override;
  52. procedure second_abs_long;override;
  53. procedure second_popcnt;override;
  54. private
  55. procedure load_fpu_location;
  56. end;
  57. implementation
  58. uses
  59. systems,
  60. globtype,globals,
  61. cutils,verbose,
  62. symconst,
  63. defutil,
  64. aasmbase,aasmtai,aasmdata,aasmcpu,
  65. symtype,symdef,
  66. cgbase,pass_2,
  67. cpuinfo,cpubase,paramgr,
  68. nbas,ncon,ncal,ncnv,nld,ncgutil,
  69. tgobj,
  70. cga,cgutils,cgx86,cgobj,hlcgobj;
  71. {*****************************************************************************
  72. TX86INLINENODE
  73. *****************************************************************************}
  74. function tx86inlinenode.first_pi : tnode;
  75. begin
  76. expectloc:=LOC_FPUREGISTER;
  77. first_pi := nil;
  78. end;
  79. function tx86inlinenode.first_arctan_real : tnode;
  80. begin
  81. expectloc:=LOC_FPUREGISTER;
  82. first_arctan_real := nil;
  83. end;
  84. function tx86inlinenode.first_abs_real : tnode;
  85. begin
  86. if use_vectorfpu(resultdef) then
  87. expectloc:=LOC_MMREGISTER
  88. else
  89. expectloc:=LOC_FPUREGISTER;
  90. first_abs_real := nil;
  91. end;
  92. function tx86inlinenode.first_sqr_real : tnode;
  93. begin
  94. expectloc:=LOC_FPUREGISTER;
  95. first_sqr_real := nil;
  96. end;
  97. function tx86inlinenode.first_sqrt_real : tnode;
  98. begin
  99. expectloc:=LOC_FPUREGISTER;
  100. first_sqrt_real := nil;
  101. end;
  102. function tx86inlinenode.first_ln_real : tnode;
  103. begin
  104. expectloc:=LOC_FPUREGISTER;
  105. first_ln_real := nil;
  106. end;
  107. function tx86inlinenode.first_cos_real : tnode;
  108. begin
  109. expectloc:=LOC_FPUREGISTER;
  110. first_cos_real := nil;
  111. end;
  112. function tx86inlinenode.first_sin_real : tnode;
  113. begin
  114. expectloc:=LOC_FPUREGISTER;
  115. first_sin_real := nil;
  116. end;
  117. function tx86inlinenode.first_round_real : tnode;
  118. begin
  119. {$ifdef x86_64}
  120. if use_vectorfpu(left.resultdef) then
  121. expectloc:=LOC_REGISTER
  122. else
  123. {$endif x86_64}
  124. expectloc:=LOC_REFERENCE;
  125. result:=nil;
  126. end;
  127. function tx86inlinenode.first_trunc_real: tnode;
  128. begin
  129. if (cs_opt_size in current_settings.optimizerswitches)
  130. {$ifdef x86_64}
  131. and not(use_vectorfpu(left.resultdef))
  132. {$endif x86_64}
  133. then
  134. result:=inherited
  135. else
  136. begin
  137. {$ifdef x86_64}
  138. if use_vectorfpu(left.resultdef) then
  139. expectloc:=LOC_REGISTER
  140. else
  141. {$endif x86_64}
  142. expectloc:=LOC_REFERENCE;
  143. result:=nil;
  144. end;
  145. end;
  146. function tx86inlinenode.first_popcnt: tnode;
  147. begin
  148. Result:=nil;
  149. if (current_settings.fputype<fpu_sse42)
  150. {$ifdef i386}
  151. or is_64bit(left.resultdef) then
  152. {$endif i386}
  153. Result:=inherited first_popcnt
  154. else
  155. expectloc:=LOC_REGISTER;
  156. end;
  157. procedure tx86inlinenode.second_Pi;
  158. begin
  159. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  160. emit_none(A_FLDPI,S_NO);
  161. tcgx86(cg).inc_fpu_stack;
  162. location.register:=NR_FPU_RESULT_REG;
  163. end;
  164. { load the FPU into the an fpu register }
  165. procedure tx86inlinenode.load_fpu_location;
  166. begin
  167. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  168. location.register:=NR_FPU_RESULT_REG;
  169. secondpass(left);
  170. case left.location.loc of
  171. LOC_FPUREGISTER:
  172. ;
  173. LOC_CFPUREGISTER:
  174. begin
  175. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,left.location.size,
  176. left.location.size,left.location.register,location.register);
  177. end;
  178. LOC_REFERENCE,LOC_CREFERENCE:
  179. begin
  180. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  181. left.location.size,left.location.size,
  182. left.location.reference,location.register);
  183. end;
  184. LOC_MMREGISTER,LOC_CMMREGISTER:
  185. begin
  186. location:=left.location;
  187. location_force_fpureg(current_asmdata.CurrAsmList,location,false);
  188. end;
  189. else
  190. internalerror(309991);
  191. end;
  192. end;
  193. procedure tx86inlinenode.second_arctan_real;
  194. begin
  195. load_fpu_location;
  196. emit_none(A_FLD1,S_NO);
  197. emit_none(A_FPATAN,S_NO);
  198. end;
  199. procedure tx86inlinenode.second_abs_real;
  200. var
  201. href : treference;
  202. begin
  203. if use_vectorfpu(resultdef) then
  204. begin
  205. secondpass(left);
  206. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  207. location:=left.location;
  208. case tfloatdef(resultdef).floattype of
  209. s32real:
  210. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_ABSMASK_SINGLE'),0,4);
  211. s64real:
  212. reference_reset_symbol(href,current_asmdata.RefAsmSymbol('FPC_ABSMASK_DOUBLE'),0,4);
  213. else
  214. internalerror(200506081);
  215. end;
  216. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList, href);
  217. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ANDPS,S_XMM,href,location.register))
  218. end
  219. else
  220. begin
  221. load_fpu_location;
  222. emit_none(A_FABS,S_NO);
  223. end;
  224. end;
  225. procedure tx86inlinenode.second_round_real;
  226. begin
  227. {$ifdef x86_64}
  228. if use_vectorfpu(left.resultdef) then
  229. begin
  230. secondpass(left);
  231. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  232. location_reset(location,LOC_REGISTER,OS_S64);
  233. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_S64);
  234. case left.location.size of
  235. OS_F32:
  236. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTSS2SI,S_Q,left.location.register,location.register));
  237. OS_F64:
  238. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTSD2SI,S_Q,left.location.register,location.register));
  239. else
  240. internalerror(2007031402);
  241. end;
  242. end
  243. else
  244. {$endif x86_64}
  245. begin
  246. load_fpu_location;
  247. location_reset_ref(location,LOC_REFERENCE,OS_S64,0);
  248. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  249. emit_ref(A_FISTP,S_IQ,location.reference);
  250. tcgx86(cg).dec_fpu_stack;
  251. emit_none(A_FWAIT,S_NO);
  252. end;
  253. end;
  254. procedure tx86inlinenode.second_trunc_real;
  255. var
  256. oldcw,newcw : treference;
  257. begin
  258. {$ifdef x86_64}
  259. if use_vectorfpu(left.resultdef) and
  260. not((left.location.loc=LOC_FPUREGISTER) and (current_settings.fputype>=fpu_sse3)) then
  261. begin
  262. secondpass(left);
  263. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,false);
  264. location_reset(location,LOC_REGISTER,OS_S64);
  265. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_S64);
  266. case left.location.size of
  267. OS_F32:
  268. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTTSS2SI,S_Q,left.location.register,location.register));
  269. OS_F64:
  270. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTTSD2SI,S_Q,left.location.register,location.register));
  271. else
  272. internalerror(2007031401);
  273. end;
  274. end
  275. else
  276. {$endif x86_64}
  277. begin
  278. if (current_settings.fputype>=fpu_sse3) then
  279. begin
  280. load_fpu_location;
  281. location_reset_ref(location,LOC_REFERENCE,OS_S64,0);
  282. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  283. emit_ref(A_FISTTP,S_IQ,location.reference);
  284. tcgx86(cg).dec_fpu_stack;
  285. end
  286. else
  287. begin
  288. tg.GetTemp(current_asmdata.CurrAsmList,2,2,tt_normal,oldcw);
  289. tg.GetTemp(current_asmdata.CurrAsmList,2,2,tt_normal,newcw);
  290. emit_ref(A_FNSTCW,S_NO,newcw);
  291. emit_ref(A_FNSTCW,S_NO,oldcw);
  292. emit_const_ref(A_OR,S_W,$0f00,newcw);
  293. load_fpu_location;
  294. emit_ref(A_FLDCW,S_NO,newcw);
  295. location_reset_ref(location,LOC_REFERENCE,OS_S64,0);
  296. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  297. emit_ref(A_FISTP,S_IQ,location.reference);
  298. tcgx86(cg).dec_fpu_stack;
  299. emit_ref(A_FLDCW,S_NO,oldcw);
  300. emit_none(A_FWAIT,S_NO);
  301. tg.UnGetTemp(current_asmdata.CurrAsmList,oldcw);
  302. tg.UnGetTemp(current_asmdata.CurrAsmList,newcw);
  303. end;
  304. end;
  305. end;
  306. procedure tx86inlinenode.second_sqr_real;
  307. begin
  308. if use_vectorfpu(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_vectorfpu(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,left.location.reference.alignment);
  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. procedure tx86inlinenode.second_abs_long;
  385. var
  386. hregister : tregister;
  387. opsize : tcgsize;
  388. hp : taicpu;
  389. begin
  390. {$ifdef i386}
  391. if current_settings.cputype<cpu_Pentium2 then
  392. begin
  393. opsize:=def_cgsize(left.resultdef);
  394. secondpass(left);
  395. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  396. location:=left.location;
  397. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  398. emit_reg_reg(A_MOV,S_L,left.location.register,location.register);
  399. emit_const_reg(A_SAR,tcgsize2opsize[opsize],31,left.location.register);
  400. emit_reg_reg(A_XOR,S_L,left.location.register,location.register);
  401. emit_reg_reg(A_SUB,S_L,left.location.register,location.register);
  402. end
  403. else
  404. {$endif i386}
  405. begin
  406. opsize:=def_cgsize(left.resultdef);
  407. secondpass(left);
  408. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  409. hregister:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  410. location:=left.location;
  411. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  412. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,hregister);
  413. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,location.register);
  414. emit_reg(A_NEG,tcgsize2opsize[opsize],hregister);
  415. hp:=taicpu.op_reg_reg(A_CMOVcc,tcgsize2opsize[opsize],hregister,location.register);
  416. hp.condition:=C_NS;
  417. current_asmdata.CurrAsmList.concat(hp);
  418. end;
  419. end;
  420. {*****************************************************************************
  421. INCLUDE/EXCLUDE GENERIC HANDLING
  422. *****************************************************************************}
  423. procedure tx86inlinenode.second_IncludeExclude;
  424. var
  425. hregister,
  426. hregister2: tregister;
  427. setbase : aint;
  428. bitsperop,l : longint;
  429. cgop : topcg;
  430. asmop : tasmop;
  431. opdef : tdef;
  432. opsize,
  433. orgsize: tcgsize;
  434. begin
  435. if is_smallset(tcallparanode(left).resultdef) then
  436. begin
  437. opdef:=tcallparanode(left).resultdef;
  438. opsize:=int_cgsize(opdef.size)
  439. end
  440. else
  441. begin
  442. opdef:=u32inttype;
  443. opsize:=OS_32;
  444. end;
  445. bitsperop:=(8*tcgsize2size[opsize]);
  446. secondpass(tcallparanode(left).left);
  447. secondpass(tcallparanode(tcallparanode(left).right).left);
  448. setbase:=tsetdef(tcallparanode(left).left.resultdef).setbase;
  449. if tcallparanode(tcallparanode(left).right).left.location.loc=LOC_CONSTANT then
  450. begin
  451. { calculate bit position }
  452. l:=1 shl ((tcallparanode(tcallparanode(left).right).left.location.value-setbase) mod bitsperop);
  453. { determine operator }
  454. if inlinenumber=in_include_x_y then
  455. cgop:=OP_OR
  456. else
  457. begin
  458. cgop:=OP_AND;
  459. l:=not(l);
  460. end;
  461. case tcallparanode(left).left.location.loc of
  462. LOC_REFERENCE :
  463. begin
  464. inc(tcallparanode(left).left.location.reference.offset,
  465. ((tcallparanode(tcallparanode(left).right).left.location.value-setbase) div bitsperop)*tcgsize2size[opsize]);
  466. cg.a_op_const_ref(current_asmdata.CurrAsmList,cgop,opsize,l,tcallparanode(left).left.location.reference);
  467. end;
  468. LOC_CREGISTER :
  469. cg.a_op_const_reg(current_asmdata.CurrAsmList,cgop,tcallparanode(left).left.location.size,l,tcallparanode(left).left.location.register);
  470. else
  471. internalerror(200405022);
  472. end;
  473. end
  474. else
  475. begin
  476. orgsize:=opsize;
  477. if opsize in [OS_8,OS_S8] then
  478. begin
  479. opdef:=u32inttype;
  480. opsize:=OS_32;
  481. end;
  482. { determine asm operator }
  483. if inlinenumber=in_include_x_y then
  484. asmop:=A_BTS
  485. else
  486. asmop:=A_BTR;
  487. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,tcallparanode(tcallparanode(left).right).left.resultdef,opdef,true);
  488. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,setbase);
  489. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  490. if (tcallparanode(left).left.location.loc=LOC_REFERENCE) then
  491. emit_reg_ref(asmop,tcgsize2opsize[opsize],hregister,tcallparanode(left).left.location.reference)
  492. else
  493. begin
  494. { second argument can't be an 8 bit register either }
  495. hregister2:=tcallparanode(left).left.location.register;
  496. if (orgsize in [OS_8,OS_S8]) then
  497. hregister2:=cg.makeregsize(current_asmdata.CurrAsmList,hregister2,opsize);
  498. emit_reg_reg(asmop,tcgsize2opsize[opsize],hregister,hregister2);
  499. end;
  500. end;
  501. end;
  502. procedure tx86inlinenode.second_popcnt;
  503. var
  504. opsize: tcgsize;
  505. begin
  506. secondpass(left);
  507. opsize:=tcgsize2unsigned[left.location.size];
  508. { no 8 Bit popcont }
  509. if opsize=OS_8 then
  510. opsize:=OS_16;
  511. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) or
  512. (left.location.size<>opsize) then
  513. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,hlcg.tcgsize2orddef(opsize),true);
  514. location_reset(location,LOC_REGISTER,opsize);
  515. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  516. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  517. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_POPCNT,TCGSize2OpSize[opsize],left.location.register,location.register))
  518. else
  519. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_POPCNT,TCGSize2OpSize[opsize],left.location.reference,location.register));
  520. end;
  521. end.