nx86inl.pas 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. protected
  25. procedure maybe_remove_round_trunc_typeconv; virtual;
  26. public
  27. { first pass override
  28. so that the code generator will actually generate
  29. these nodes.
  30. }
  31. function first_pi: tnode ; override;
  32. function first_arctan_real: tnode; override;
  33. function first_abs_real: tnode; override;
  34. function first_sqr_real: tnode; override;
  35. function first_sqrt_real: tnode; override;
  36. function first_ln_real: tnode; override;
  37. function first_cos_real: tnode; override;
  38. function first_sin_real: tnode; override;
  39. function first_round_real: tnode; override;
  40. function first_trunc_real: tnode; override;
  41. function first_popcnt: tnode; override;
  42. function first_fma: tnode; override;
  43. function first_frac_real : tnode; override;
  44. function first_int_real : tnode; override;
  45. { second pass override to generate these nodes }
  46. procedure second_IncludeExclude;override;
  47. procedure second_pi; override;
  48. procedure second_arctan_real; override;
  49. procedure second_abs_real; override;
  50. procedure second_round_real; override;
  51. procedure second_sqr_real; override;
  52. procedure second_sqrt_real; override;
  53. procedure second_ln_real; override;
  54. procedure second_cos_real; override;
  55. procedure second_sin_real; override;
  56. procedure second_trunc_real; override;
  57. procedure second_prefetch;override;
  58. {$ifndef i8086}
  59. procedure second_abs_long;override;
  60. {$endif not i8086}
  61. procedure second_popcnt;override;
  62. procedure second_fma;override;
  63. procedure second_frac_real;override;
  64. procedure second_int_real;override;
  65. private
  66. procedure load_fpu_location(lnode: tnode);
  67. end;
  68. implementation
  69. uses
  70. systems,
  71. globtype,globals,
  72. verbose,compinnr,
  73. defutil,
  74. aasmbase,aasmdata,aasmcpu,
  75. symtype,symdef,symcpu,
  76. cgbase,pass_2,
  77. cpuinfo,cpubase,nutils,
  78. ncal,ncgutil,
  79. tgobj,
  80. cga,cgutils,cgx86,cgobj,hlcgobj;
  81. {*****************************************************************************
  82. TX86INLINENODE
  83. *****************************************************************************}
  84. procedure tx86inlinenode.maybe_remove_round_trunc_typeconv;
  85. begin
  86. { only makes a difference for x86_64 }
  87. end;
  88. function tx86inlinenode.first_pi : tnode;
  89. begin
  90. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  91. begin
  92. expectloc:=LOC_FPUREGISTER;
  93. first_pi := nil;
  94. end
  95. else
  96. result:=inherited;
  97. end;
  98. function tx86inlinenode.first_arctan_real : tnode;
  99. begin
  100. {$ifdef i8086}
  101. { FPATAN's range is limited to (0 <= value < 1) on the 8087 and 80287,
  102. so we need to use the RTL helper on these FPUs }
  103. if current_settings.cputype < cpu_386 then
  104. begin
  105. result := inherited;
  106. exit;
  107. end;
  108. {$endif i8086}
  109. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  110. begin
  111. expectloc:=LOC_FPUREGISTER;
  112. first_arctan_real := nil;
  113. end
  114. else
  115. result:=inherited;
  116. end;
  117. function tx86inlinenode.first_abs_real : tnode;
  118. begin
  119. if use_vectorfpu(resultdef) then
  120. expectloc:=LOC_MMREGISTER
  121. else
  122. expectloc:=LOC_FPUREGISTER;
  123. first_abs_real := nil;
  124. end;
  125. function tx86inlinenode.first_sqr_real : tnode;
  126. begin
  127. if use_vectorfpu(resultdef) then
  128. expectloc:=LOC_MMREGISTER
  129. else
  130. expectloc:=LOC_FPUREGISTER;
  131. first_sqr_real := nil;
  132. end;
  133. function tx86inlinenode.first_sqrt_real : tnode;
  134. begin
  135. if use_vectorfpu(resultdef) then
  136. expectloc:=LOC_MMREGISTER
  137. else
  138. expectloc:=LOC_FPUREGISTER;
  139. first_sqrt_real := nil;
  140. end;
  141. function tx86inlinenode.first_ln_real : tnode;
  142. begin
  143. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  144. begin
  145. expectloc:=LOC_FPUREGISTER;
  146. first_ln_real := nil;
  147. end
  148. else
  149. result:=inherited;
  150. end;
  151. function tx86inlinenode.first_cos_real : tnode;
  152. begin
  153. {$ifdef i8086}
  154. { FCOS is 387+ }
  155. if current_settings.cputype < cpu_386 then
  156. begin
  157. result := inherited;
  158. exit;
  159. end;
  160. {$endif i8086}
  161. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  162. begin
  163. expectloc:=LOC_FPUREGISTER;
  164. result:=nil;
  165. end
  166. else
  167. result:=inherited;
  168. end;
  169. function tx86inlinenode.first_sin_real : tnode;
  170. begin
  171. {$ifdef i8086}
  172. { FSIN is 387+ }
  173. if current_settings.cputype < cpu_386 then
  174. begin
  175. result := inherited;
  176. exit;
  177. end;
  178. {$endif i8086}
  179. if (tfloatdef(pbestrealtype^).floattype=s80real) then
  180. begin
  181. expectloc:=LOC_FPUREGISTER;
  182. result:=nil;
  183. end
  184. else
  185. result:=inherited;
  186. end;
  187. function tx86inlinenode.first_round_real : tnode;
  188. begin
  189. maybe_remove_round_trunc_typeconv;
  190. {$ifdef x86_64}
  191. if use_vectorfpu(left.resultdef) then
  192. expectloc:=LOC_REGISTER
  193. else
  194. {$endif x86_64}
  195. expectloc:=LOC_REFERENCE;
  196. result:=nil;
  197. end;
  198. function tx86inlinenode.first_trunc_real: tnode;
  199. begin
  200. maybe_remove_round_trunc_typeconv;
  201. if (cs_opt_size in current_settings.optimizerswitches)
  202. {$ifdef x86_64}
  203. and not(use_vectorfpu(left.resultdef))
  204. {$endif x86_64}
  205. then
  206. result:=inherited
  207. else
  208. begin
  209. {$ifdef x86_64}
  210. if use_vectorfpu(left.resultdef) then
  211. expectloc:=LOC_REGISTER
  212. else
  213. {$endif x86_64}
  214. expectloc:=LOC_REFERENCE;
  215. result:=nil;
  216. end;
  217. end;
  218. function tx86inlinenode.first_popcnt: tnode;
  219. begin
  220. Result:=nil;
  221. {$ifndef i8086}
  222. if (CPUX86_HAS_POPCNT in cpu_capabilities[current_settings.cputype])
  223. {$ifdef i386}
  224. and not is_64bit(left.resultdef)
  225. {$endif i386}
  226. then
  227. expectloc:=LOC_REGISTER
  228. else
  229. {$endif not i8086}
  230. Result:=inherited first_popcnt
  231. end;
  232. function tx86inlinenode.first_fma : tnode;
  233. begin
  234. {$ifndef i8086}
  235. if ((cpu_capabilities[current_settings.cputype]*[CPUX86_HAS_FMA,CPUX86_HAS_FMA4])<>[]) and
  236. ((is_double(resultdef)) or (is_single(resultdef))) then
  237. begin
  238. expectloc:=LOC_MMREGISTER;
  239. Result:=nil;
  240. end
  241. else
  242. {$endif i8086}
  243. Result:=inherited first_fma;
  244. end;
  245. function tx86inlinenode.first_frac_real : tnode;
  246. begin
  247. if (current_settings.fputype>=fpu_sse41) and
  248. ((is_double(resultdef)) or (is_single(resultdef))) then
  249. begin
  250. expectloc:=LOC_MMREGISTER;
  251. Result:=nil;
  252. end
  253. else
  254. Result:=inherited first_frac_real;
  255. end;
  256. function tx86inlinenode.first_int_real : tnode;
  257. begin
  258. if (current_settings.fputype>=fpu_sse41) and
  259. ((is_double(resultdef)) or (is_single(resultdef))) then
  260. begin
  261. expectloc:=LOC_MMREGISTER;
  262. Result:=nil;
  263. end
  264. else
  265. Result:=inherited first_int_real;
  266. end;
  267. procedure tx86inlinenode.second_pi;
  268. begin
  269. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  270. emit_none(A_FLDPI,S_NO);
  271. tcgx86(cg).inc_fpu_stack;
  272. location.register:=NR_FPU_RESULT_REG;
  273. end;
  274. { load the FPU into the an fpu register }
  275. procedure tx86inlinenode.load_fpu_location(lnode: tnode);
  276. begin
  277. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  278. location.register:=NR_FPU_RESULT_REG;
  279. secondpass(lnode);
  280. case lnode.location.loc of
  281. LOC_FPUREGISTER:
  282. ;
  283. LOC_CFPUREGISTER:
  284. begin
  285. cg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,lnode.location.size,
  286. lnode.location.size,lnode.location.register,location.register);
  287. end;
  288. LOC_REFERENCE,LOC_CREFERENCE:
  289. begin
  290. cg.a_loadfpu_ref_reg(current_asmdata.CurrAsmList,
  291. lnode.location.size,lnode.location.size,
  292. lnode.location.reference,location.register);
  293. end;
  294. LOC_MMREGISTER,LOC_CMMREGISTER:
  295. begin
  296. location:=lnode.location;
  297. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,location,resultdef,false);
  298. end;
  299. else
  300. internalerror(309991);
  301. end;
  302. end;
  303. procedure tx86inlinenode.second_arctan_real;
  304. begin
  305. load_fpu_location(left);
  306. emit_none(A_FLD1,S_NO);
  307. emit_none(A_FPATAN,S_NO);
  308. end;
  309. procedure tx86inlinenode.second_abs_real;
  310. var
  311. href : treference;
  312. begin
  313. if use_vectorfpu(resultdef) then
  314. begin
  315. secondpass(left);
  316. if left.location.loc<>LOC_MMREGISTER then
  317. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,UseAVX);
  318. if UseAVX then
  319. begin
  320. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  321. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  322. end
  323. else
  324. location:=left.location;
  325. case tfloatdef(resultdef).floattype of
  326. s32real:
  327. begin
  328. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(target_info.cprefix+'FPC_ABSMASK_SINGLE',AT_DATA),0,4,[]);
  329. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList, href);
  330. if UseAVX then
  331. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg_reg(
  332. A_VANDPS,S_XMM,href,left.location.register,location.register))
  333. else
  334. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ANDPS,S_XMM,href,location.register));
  335. end;
  336. s64real:
  337. begin
  338. reference_reset_symbol(href,current_asmdata.RefAsmSymbol(target_info.cprefix+'FPC_ABSMASK_DOUBLE',AT_DATA),0,4,[]);
  339. tcgx86(cg).make_simple_ref(current_asmdata.CurrAsmList, href);
  340. if UseAVX then
  341. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg_reg(
  342. A_VANDPD,S_XMM,href,left.location.register,location.register))
  343. else
  344. current_asmdata.CurrAsmList.concat(taicpu.op_ref_reg(A_ANDPD,S_XMM,href,location.register))
  345. end;
  346. else
  347. internalerror(200506081);
  348. end;
  349. end
  350. else
  351. begin
  352. load_fpu_location(left);
  353. emit_none(A_FABS,S_NO);
  354. end;
  355. end;
  356. procedure tx86inlinenode.second_round_real;
  357. begin
  358. {$ifdef x86_64}
  359. if use_vectorfpu(left.resultdef) then
  360. begin
  361. secondpass(left);
  362. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  363. location_reset(location,LOC_REGISTER,OS_S64);
  364. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_S64);
  365. if UseAVX then
  366. case left.location.size of
  367. OS_F32:
  368. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_VCVTSS2SI,S_NO,left.location.register,location.register));
  369. OS_F64:
  370. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_VCVTSD2SI,S_NO,left.location.register,location.register));
  371. else
  372. internalerror(2007031402);
  373. end
  374. else
  375. case left.location.size of
  376. OS_F32:
  377. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTSS2SI,S_NO,left.location.register,location.register));
  378. OS_F64:
  379. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTSD2SI,S_NO,left.location.register,location.register));
  380. else
  381. internalerror(2007031402);
  382. end;
  383. end
  384. else
  385. {$endif x86_64}
  386. begin
  387. load_fpu_location(left);
  388. location_reset_ref(location,LOC_REFERENCE,OS_S64,0,[]);
  389. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  390. emit_ref(A_FISTP,S_IQ,location.reference);
  391. tcgx86(cg).dec_fpu_stack;
  392. emit_none(A_FWAIT,S_NO);
  393. end;
  394. end;
  395. procedure tx86inlinenode.second_trunc_real;
  396. var
  397. oldcw,newcw : treference;
  398. begin
  399. {$ifdef x86_64}
  400. if use_vectorfpu(left.resultdef) and
  401. not((left.location.loc=LOC_FPUREGISTER) and (current_settings.fputype>=fpu_sse3)) then
  402. begin
  403. secondpass(left);
  404. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  405. location_reset(location,LOC_REGISTER,OS_S64);
  406. location.register:=cg.getintregister(current_asmdata.CurrAsmList,OS_S64);
  407. if UseAVX then
  408. case left.location.size of
  409. OS_F32:
  410. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_VCVTTSS2SI,S_NO,left.location.register,location.register));
  411. OS_F64:
  412. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_VCVTTSD2SI,S_NO,left.location.register,location.register));
  413. else
  414. internalerror(2007031401);
  415. end
  416. else
  417. case left.location.size of
  418. OS_F32:
  419. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTTSS2SI,S_NO,left.location.register,location.register));
  420. OS_F64:
  421. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CVTTSD2SI,S_NO,left.location.register,location.register));
  422. else
  423. internalerror(2007031401);
  424. end;
  425. end
  426. else
  427. {$endif x86_64}
  428. begin
  429. if (current_settings.fputype>=fpu_sse3) then
  430. begin
  431. load_fpu_location(left);
  432. location_reset_ref(location,LOC_REFERENCE,OS_S64,0,[]);
  433. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  434. emit_ref(A_FISTTP,S_IQ,location.reference);
  435. tcgx86(cg).dec_fpu_stack;
  436. end
  437. else
  438. begin
  439. tg.GetTemp(current_asmdata.CurrAsmList,2,2,tt_normal,oldcw);
  440. tg.GetTemp(current_asmdata.CurrAsmList,2,2,tt_normal,newcw);
  441. {$ifdef i8086}
  442. if current_settings.cputype<=cpu_286 then
  443. begin
  444. emit_ref(A_FSTCW,S_NO,newcw);
  445. emit_ref(A_FSTCW,S_NO,oldcw);
  446. emit_none(A_FWAIT,S_NO);
  447. end
  448. else
  449. {$endif i8086}
  450. begin
  451. emit_ref(A_FNSTCW,S_NO,newcw);
  452. emit_ref(A_FNSTCW,S_NO,oldcw);
  453. end;
  454. emit_const_ref(A_OR,S_W,$0f00,newcw);
  455. load_fpu_location(left);
  456. emit_ref(A_FLDCW,S_NO,newcw);
  457. location_reset_ref(location,LOC_REFERENCE,OS_S64,0,[]);
  458. tg.GetTemp(current_asmdata.CurrAsmList,resultdef.size,resultdef.alignment,tt_normal,location.reference);
  459. emit_ref(A_FISTP,S_IQ,location.reference);
  460. tcgx86(cg).dec_fpu_stack;
  461. emit_ref(A_FLDCW,S_NO,oldcw);
  462. emit_none(A_FWAIT,S_NO);
  463. tg.UnGetTemp(current_asmdata.CurrAsmList,oldcw);
  464. tg.UnGetTemp(current_asmdata.CurrAsmList,newcw);
  465. end;
  466. end;
  467. end;
  468. procedure tx86inlinenode.second_sqr_real;
  469. begin
  470. if use_vectorfpu(resultdef) then
  471. begin
  472. secondpass(left);
  473. location_reset(location,LOC_MMREGISTER,left.location.size);
  474. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  475. if UseAVX then
  476. begin
  477. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  478. cg.a_opmm_reg_reg_reg(current_asmdata.CurrAsmList,OP_MUL,left.location.size,left.location.register,left.location.register,location.register,mms_movescalar);
  479. end
  480. else
  481. begin
  482. if left.location.loc in [LOC_CFPUREGISTER,LOC_FPUREGISTER] then
  483. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  484. cg.a_loadmm_loc_reg(current_asmdata.CurrAsmList,location.size,left.location,location.register,mms_movescalar);
  485. cg.a_opmm_reg_reg(current_asmdata.CurrAsmList,OP_MUL,left.location.size,location.register,location.register,mms_movescalar);
  486. end;
  487. end
  488. else
  489. begin
  490. load_fpu_location(left);
  491. emit_reg_reg(A_FMUL,S_NO,NR_ST0,NR_ST0);
  492. end;
  493. end;
  494. procedure tx86inlinenode.second_sqrt_real;
  495. begin
  496. if use_vectorfpu(resultdef) then
  497. begin
  498. secondpass(left);
  499. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  500. location_reset(location,LOC_MMREGISTER,left.location.size);
  501. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  502. if UseAVX then
  503. case tfloatdef(resultdef).floattype of
  504. s32real:
  505. { we use S_NO instead of S_XMM here, regardless of the register size, as the size of the memory location is 32/64 bit }
  506. { using left.location.register here as 2nd parameter is crucial to break dependency chains }
  507. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_VSQRTSS,S_NO,left.location.register,left.location.register,location.register));
  508. s64real:
  509. { we use S_NO instead of S_XMM here, regardless of the register size, as the size of the memory location is 32/64 bit }
  510. { using left.location.register here as 2nd parameter is crucial to break dependency chains }
  511. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_VSQRTSD,S_NO,left.location.register,left.location.register,location.register));
  512. else
  513. internalerror(200510031);
  514. end
  515. else
  516. case tfloatdef(resultdef).floattype of
  517. s32real:
  518. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SQRTSS,S_NO,left.location.register,location.register));
  519. s64real:
  520. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SQRTSD,S_NO,left.location.register,location.register));
  521. else
  522. internalerror(200510031);
  523. end;
  524. end
  525. else
  526. begin
  527. load_fpu_location(left);
  528. emit_none(A_FSQRT,S_NO);
  529. end;
  530. end;
  531. procedure tx86inlinenode.second_ln_real;
  532. begin
  533. load_fpu_location(left);
  534. emit_none(A_FLDLN2,S_NO);
  535. emit_none(A_FXCH,S_NO);
  536. emit_none(A_FYL2X,S_NO);
  537. end;
  538. procedure tx86inlinenode.second_cos_real;
  539. begin
  540. {$ifdef i8086}
  541. { FCOS is 387+ }
  542. if current_settings.cputype < cpu_386 then
  543. begin
  544. inherited;
  545. exit;
  546. end;
  547. {$endif i8086}
  548. load_fpu_location(left);
  549. emit_none(A_FCOS,S_NO);
  550. end;
  551. procedure tx86inlinenode.second_sin_real;
  552. begin
  553. {$ifdef i8086}
  554. { FSIN is 387+ }
  555. if current_settings.cputype < cpu_386 then
  556. begin
  557. inherited;
  558. exit;
  559. end;
  560. {$endif i8086}
  561. load_fpu_location(left);
  562. emit_none(A_FSIN,S_NO)
  563. end;
  564. procedure tx86inlinenode.second_prefetch;
  565. var
  566. ref : treference;
  567. r : tregister;
  568. checkpointer_used : boolean;
  569. begin
  570. {$if defined(i386) or defined(i8086)}
  571. if current_settings.cputype>=cpu_Pentium3 then
  572. {$endif i386 or i8086}
  573. begin
  574. { do not call Checkpointer for left node }
  575. checkpointer_used:=(cs_checkpointer in current_settings.localswitches);
  576. if checkpointer_used then
  577. node_change_local_switch(left,cs_checkpointer,false);
  578. secondpass(left);
  579. if checkpointer_used then
  580. node_change_local_switch(left,cs_checkpointer,false);
  581. case left.location.loc of
  582. LOC_CREFERENCE,
  583. LOC_REFERENCE:
  584. begin
  585. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  586. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,r);
  587. reference_reset_base(ref,r,0,left.location.reference.alignment,left.location.reference.volatility);
  588. current_asmdata.CurrAsmList.concat(taicpu.op_ref(A_PREFETCHNTA,S_NO,ref));
  589. end;
  590. else
  591. { nothing to prefetch };
  592. end;
  593. end;
  594. end;
  595. {$ifndef i8086}
  596. procedure tx86inlinenode.second_abs_long;
  597. var
  598. hregister : tregister;
  599. opsize : tcgsize;
  600. hp : taicpu;
  601. begin
  602. {$ifdef i386}
  603. if not(CPUX86_HAS_CMOV in cpu_capabilities[current_settings.cputype]) then
  604. begin
  605. opsize:=def_cgsize(left.resultdef);
  606. secondpass(left);
  607. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  608. location:=left.location;
  609. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  610. emit_reg_reg(A_MOV,S_L,left.location.register,location.register);
  611. emit_const_reg(A_SAR,tcgsize2opsize[opsize],31,left.location.register);
  612. emit_reg_reg(A_XOR,S_L,left.location.register,location.register);
  613. emit_reg_reg(A_SUB,S_L,left.location.register,location.register);
  614. end
  615. else
  616. {$endif i386}
  617. begin
  618. opsize:=def_cgsize(left.resultdef);
  619. secondpass(left);
  620. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  621. hregister:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  622. location:=left.location;
  623. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  624. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,hregister);
  625. cg.a_load_reg_reg(current_asmdata.CurrAsmList,opsize,opsize,left.location.register,location.register);
  626. emit_reg(A_NEG,tcgsize2opsize[opsize],hregister);
  627. hp:=taicpu.op_reg_reg(A_CMOVcc,tcgsize2opsize[opsize],hregister,location.register);
  628. hp.condition:=C_NS;
  629. current_asmdata.CurrAsmList.concat(hp);
  630. end;
  631. end;
  632. {$endif not i8086}
  633. {*****************************************************************************
  634. INCLUDE/EXCLUDE GENERIC HANDLING
  635. *****************************************************************************}
  636. procedure tx86inlinenode.second_IncludeExclude;
  637. var
  638. hregister,
  639. hregister2: tregister;
  640. setbase : aint;
  641. bitsperop,l : longint;
  642. cgop : topcg;
  643. asmop : tasmop;
  644. opdef : tdef;
  645. opsize,
  646. orgsize: tcgsize;
  647. begin
  648. {$ifdef i8086}
  649. { BTS and BTR are 386+ }
  650. if current_settings.cputype < cpu_386 then
  651. begin
  652. inherited;
  653. exit;
  654. end;
  655. {$endif i8086}
  656. if is_smallset(tcallparanode(left).resultdef) then
  657. begin
  658. opdef:=tcallparanode(left).resultdef;
  659. opsize:=int_cgsize(opdef.size)
  660. end
  661. else
  662. begin
  663. opdef:=u32inttype;
  664. opsize:=OS_32;
  665. end;
  666. bitsperop:=(8*tcgsize2size[opsize]);
  667. secondpass(tcallparanode(left).left);
  668. secondpass(tcallparanode(tcallparanode(left).right).left);
  669. setbase:=tsetdef(tcallparanode(left).left.resultdef).setbase;
  670. if tcallparanode(tcallparanode(left).right).left.location.loc=LOC_CONSTANT then
  671. begin
  672. { calculate bit position }
  673. l:=1 shl ((tcallparanode(tcallparanode(left).right).left.location.value-setbase) mod bitsperop);
  674. { determine operator }
  675. if inlinenumber=in_include_x_y then
  676. cgop:=OP_OR
  677. else
  678. begin
  679. cgop:=OP_AND;
  680. l:=not(l);
  681. end;
  682. case tcallparanode(left).left.location.loc of
  683. LOC_REFERENCE :
  684. begin
  685. inc(tcallparanode(left).left.location.reference.offset,
  686. ((tcallparanode(tcallparanode(left).right).left.location.value-setbase) div bitsperop)*tcgsize2size[opsize]);
  687. cg.a_op_const_ref(current_asmdata.CurrAsmList,cgop,opsize,l,tcallparanode(left).left.location.reference);
  688. end;
  689. LOC_CREGISTER :
  690. cg.a_op_const_reg(current_asmdata.CurrAsmList,cgop,tcallparanode(left).left.location.size,l,tcallparanode(left).left.location.register);
  691. else
  692. internalerror(200405022);
  693. end;
  694. end
  695. else
  696. begin
  697. orgsize:=opsize;
  698. if opsize in [OS_8,OS_S8] then
  699. begin
  700. opdef:=u32inttype;
  701. opsize:=OS_32;
  702. end;
  703. { determine asm operator }
  704. if inlinenumber=in_include_x_y then
  705. asmop:=A_BTS
  706. else
  707. asmop:=A_BTR;
  708. hlcg.location_force_reg(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.location,tcallparanode(tcallparanode(left).right).left.resultdef,opdef,true);
  709. register_maybe_adjust_setbase(current_asmdata.CurrAsmList,tcallparanode(tcallparanode(left).right).left.resultdef,tcallparanode(tcallparanode(left).right).left.location,setbase);
  710. hregister:=tcallparanode(tcallparanode(left).right).left.location.register;
  711. if (tcallparanode(left).left.location.loc=LOC_REFERENCE) then
  712. emit_reg_ref(asmop,tcgsize2opsize[opsize],hregister,tcallparanode(left).left.location.reference)
  713. else
  714. begin
  715. { second argument can't be an 8 bit register either }
  716. hregister2:=tcallparanode(left).left.location.register;
  717. if (orgsize in [OS_8,OS_S8]) then
  718. hregister2:=cg.makeregsize(current_asmdata.CurrAsmList,hregister2,opsize);
  719. emit_reg_reg(asmop,tcgsize2opsize[opsize],hregister,hregister2);
  720. end;
  721. end;
  722. end;
  723. procedure tx86inlinenode.second_popcnt;
  724. var
  725. opsize: tcgsize;
  726. begin
  727. secondpass(left);
  728. opsize:=tcgsize2unsigned[left.location.size];
  729. { no 8 Bit popcont }
  730. if opsize=OS_8 then
  731. opsize:=OS_16;
  732. if not(left.location.loc in [LOC_REGISTER,LOC_CREGISTER,LOC_REFERENCE,LOC_CREFERENCE]) or
  733. (left.location.size<>opsize) then
  734. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,cgsize_orddef(opsize),true);
  735. location_reset(location,LOC_REGISTER,opsize);
  736. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  737. if left.location.loc in [LOC_REGISTER,LOC_CREGISTER] then
  738. emit_reg_reg(A_POPCNT,TCGSize2OpSize[opsize],left.location.register,location.register)
  739. else
  740. emit_ref_reg(A_POPCNT,TCGSize2OpSize[opsize],left.location.reference,location.register);
  741. end;
  742. procedure tx86inlinenode.second_fma;
  743. const
  744. op : array[false..true,false..true,s32real..s64real,0..3] of TAsmOp =
  745. (
  746. { positive product }
  747. (
  748. { positive third operand }
  749. ((A_VFMADD231SS,A_VFMADD231SS,A_VFMADD231SS,A_VFMADD213SS),
  750. (A_VFMADD231SD,A_VFMADD231SD,A_VFMADD231SD,A_VFMADD213SD)
  751. ),
  752. { negative third operand }
  753. ((A_VFMSUB231SS,A_VFMSUB231SS,A_VFMSUB231SS,A_VFMSUB213SS),
  754. (A_VFMSUB231SD,A_VFMSUB231SD,A_VFMSUB231SD,A_VFMSUB213SD)
  755. )
  756. ),
  757. { negative product }
  758. (
  759. { positive third operand }
  760. ((A_VFNMADD231SS,A_VFNMADD231SS,A_VFNMADD231SS,A_VFNMADD213SS),
  761. (A_VFNMADD231SD,A_VFNMADD231SD,A_VFNMADD231SD,A_VFNMADD213SD)
  762. ),
  763. { negative third operand }
  764. ((A_VFNMSUB231SS,A_VFNMSUB231SS,A_VFNMSUB231SS,A_VFNMSUB213SS),
  765. (A_VFNMSUB231SD,A_VFNMSUB231SD,A_VFNMSUB231SD,A_VFNMSUB213SD)
  766. )
  767. )
  768. );
  769. var
  770. paraarray : array[1..3] of tnode;
  771. memop,
  772. i : integer;
  773. negop3,
  774. negproduct,
  775. gotmem : boolean;
  776. begin
  777. {$ifndef i8086}
  778. if (cpu_capabilities[current_settings.cputype]*[CPUX86_HAS_FMA,CPUX86_HAS_FMA4])<>[] then
  779. begin
  780. negop3:=false;
  781. negproduct:=false;
  782. paraarray[1]:=tcallparanode(tcallparanode(tcallparanode(parameters).nextpara).nextpara).paravalue;
  783. paraarray[2]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  784. paraarray[3]:=tcallparanode(parameters).paravalue;
  785. { check if a neg. node can be removed
  786. this is possible because changing the sign of
  787. a floating point number does not affect its absolute
  788. value in any way
  789. }
  790. if paraarray[1].nodetype=unaryminusn then
  791. begin
  792. paraarray[1]:=tunarynode(paraarray[1]).left;
  793. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  794. only no code is generated for it }
  795. negproduct:=not(negproduct);
  796. end;
  797. if paraarray[2].nodetype=unaryminusn then
  798. begin
  799. paraarray[2]:=tunarynode(paraarray[2]).left;
  800. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  801. only no code is generated for it }
  802. negproduct:=not(negproduct);
  803. end;
  804. if paraarray[3].nodetype=unaryminusn then
  805. begin
  806. paraarray[3]:=tunarynode(paraarray[3]).left;
  807. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  808. only no code is generated for it }
  809. negop3:=true;
  810. end;
  811. for i:=1 to 3 do
  812. secondpass(paraarray[i]);
  813. { only one memory operand is allowed }
  814. gotmem:=false;
  815. memop:=0;
  816. for i:=1 to 3 do
  817. begin
  818. if not(paraarray[i].location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  819. begin
  820. if (paraarray[i].location.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and not(gotmem) then
  821. begin
  822. memop:=i;
  823. gotmem:=true;
  824. end
  825. else
  826. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,paraarray[i].location,paraarray[i].resultdef,true);
  827. end;
  828. end;
  829. location_reset(location,LOC_MMREGISTER,paraarray[1].location.size);
  830. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  831. if gotmem then
  832. begin
  833. case memop of
  834. 1:
  835. begin
  836. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,paraarray[3].resultdef,resultdef,
  837. paraarray[3].location.register,location.register,mms_movescalar);
  838. emit_ref_reg_reg(op[negproduct,negop3,tfloatdef(resultdef).floattype,memop],S_NO,
  839. paraarray[1].location.reference,paraarray[2].location.register,location.register);
  840. end;
  841. 2:
  842. begin
  843. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,paraarray[3].resultdef,resultdef,
  844. paraarray[3].location.register,location.register,mms_movescalar);
  845. emit_ref_reg_reg(op[negproduct,negop3,tfloatdef(resultdef).floattype,memop],S_NO,
  846. paraarray[2].location.reference,paraarray[1].location.register,location.register);
  847. end;
  848. 3:
  849. begin
  850. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,paraarray[1].resultdef,resultdef,
  851. paraarray[1].location.register,location.register,mms_movescalar);
  852. emit_ref_reg_reg(op[negproduct,negop3,tfloatdef(resultdef).floattype,memop],S_NO,
  853. paraarray[3].location.reference,paraarray[2].location.register,location.register);
  854. end
  855. else
  856. internalerror(2014041301);
  857. end;
  858. end
  859. else
  860. begin
  861. { try to use the location which is already in a temp. mm register as destination,
  862. so the compiler might be able to re-use the register }
  863. if paraarray[1].location.loc=LOC_MMREGISTER then
  864. begin
  865. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,paraarray[1].resultdef,resultdef,
  866. paraarray[1].location.register,location.register,mms_movescalar);
  867. emit_reg_reg_reg(op[negproduct,negop3,tfloatdef(resultdef).floattype,3],S_NO,
  868. paraarray[3].location.register,paraarray[2].location.register,location.register);
  869. end
  870. else if paraarray[2].location.loc=LOC_MMREGISTER then
  871. begin
  872. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,paraarray[2].resultdef,resultdef,
  873. paraarray[2].location.register,location.register,mms_movescalar);
  874. emit_reg_reg_reg(op[negproduct,negop3,tfloatdef(resultdef).floattype,3],S_NO,
  875. paraarray[3].location.register,paraarray[1].location.register,location.register);
  876. end
  877. else
  878. begin
  879. hlcg.a_loadmm_reg_reg(current_asmdata.CurrAsmList,paraarray[3].resultdef,resultdef,
  880. paraarray[3].location.register,location.register,mms_movescalar);
  881. emit_reg_reg_reg(op[negproduct,negop3,tfloatdef(resultdef).floattype,0],S_NO,
  882. paraarray[1].location.register,paraarray[2].location.register,location.register);
  883. end;
  884. end;
  885. end
  886. else
  887. {$endif i8086}
  888. internalerror(2014032301);
  889. end;
  890. procedure tx86inlinenode.second_frac_real;
  891. var
  892. extrareg : TRegister;
  893. begin
  894. if use_vectorfpu(resultdef) then
  895. begin
  896. secondpass(left);
  897. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  898. location_reset(location,LOC_MMREGISTER,left.location.size);
  899. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  900. if UseAVX then
  901. case tfloatdef(resultdef).floattype of
  902. s32real:
  903. begin
  904. { using left.location.register here as 3rd parameter is crucial to break dependency chains }
  905. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg_reg(A_VROUNDSS,S_NO,3,left.location.register,left.location.register,location.register));
  906. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_VSUBSS,S_NO,location.register,left.location.register,location.register));
  907. end;
  908. s64real:
  909. begin
  910. { using left.location.register here as 3rd parameter is crucial to break dependency chains }
  911. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg_reg(A_VROUNDSD,S_NO,3,left.location.register,left.location.register,location.register));
  912. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_VSUBSD,S_NO,location.register,left.location.register,location.register));
  913. end;
  914. else
  915. internalerror(2017052102);
  916. end
  917. else
  918. begin
  919. extrareg:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  920. cg.a_loadmm_loc_reg(current_asmdata.CurrAsmList,location.size,left.location,location.register,mms_movescalar);
  921. case tfloatdef(resultdef).floattype of
  922. s32real:
  923. begin
  924. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_ROUNDSS,S_NO,3,left.location.register,extrareg));
  925. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SUBSS,S_NO,extrareg,location.register));
  926. end;
  927. s64real:
  928. begin
  929. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_ROUNDSD,S_NO,3,left.location.register,extrareg));
  930. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_SUBSD,S_NO,extrareg,location.register));
  931. end;
  932. else
  933. internalerror(2017052103);
  934. end;
  935. end;
  936. end
  937. else
  938. internalerror(2017052101);
  939. end;
  940. procedure tx86inlinenode.second_int_real;
  941. var
  942. extrareg : TRegister;
  943. begin
  944. if use_vectorfpu(resultdef) then
  945. begin
  946. secondpass(left);
  947. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  948. location_reset(location,LOC_MMREGISTER,left.location.size);
  949. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  950. if UseAVX then
  951. case tfloatdef(resultdef).floattype of
  952. s32real:
  953. { using left.location.register here as 3rd parameter is crucial to break dependency chains }
  954. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg_reg(A_VROUNDSS,S_NO,3,left.location.register,left.location.register,location.register));
  955. s64real:
  956. { using left.location.register here as 3rd parameter is crucial to break dependency chains }
  957. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg_reg(A_VROUNDSD,S_NO,3,left.location.register,left.location.register,location.register));
  958. else
  959. internalerror(2017052105);
  960. end
  961. else
  962. begin
  963. case tfloatdef(resultdef).floattype of
  964. s32real:
  965. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_ROUNDSS,S_NO,3,left.location.register,location.register));
  966. s64real:
  967. current_asmdata.CurrAsmList.concat(taicpu.op_const_reg_reg(A_ROUNDSD,S_NO,3,left.location.register,location.register));
  968. else
  969. internalerror(2017052106);
  970. end;
  971. end;
  972. end
  973. else
  974. internalerror(2017052107);
  975. end;
  976. end.