ncpuinl.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generates AAarch64 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 ncpuinl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ninl,ncginl;
  22. type
  23. taarch64inlinenode = class(tcgInlineNode)
  24. function first_abs_real: tnode; override;
  25. function first_sqr_real: tnode; override;
  26. function first_sqrt_real: tnode; override;
  27. function first_round_real: tnode; override;
  28. function first_trunc_real: tnode; override;
  29. function first_int_real: tnode; override;
  30. function first_frac_real: tnode; override;
  31. function first_fma : tnode; override;
  32. function first_minmax : tnode; override;
  33. procedure second_abs_real; override;
  34. procedure second_sqr_real; override;
  35. procedure second_sqrt_real; override;
  36. procedure second_abs_long; override;
  37. procedure second_round_real; override;
  38. procedure second_trunc_real; override;
  39. procedure second_int_real; override;
  40. procedure second_frac_real; override;
  41. procedure second_get_frame; override;
  42. procedure second_fma; override;
  43. procedure second_prefetch; override;
  44. procedure second_minmax; override;
  45. private
  46. procedure load_fpu_location;
  47. end;
  48. implementation
  49. uses
  50. globtype,verbose,globals,
  51. compinnr,
  52. cpuinfo, defutil,symdef,aasmdata,aasmcpu,
  53. cgbase,cgutils,pass_1,pass_2,
  54. procinfo,
  55. ncal,nutils,
  56. cpubase,ncgutil,cgobj,cgcpu,hlcgobj;
  57. {*****************************************************************************
  58. taarch64inlinenode
  59. *****************************************************************************}
  60. procedure taarch64inlinenode.load_fpu_location;
  61. begin
  62. secondpass(left);
  63. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  64. location_copy(location,left.location);
  65. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  66. location.loc:=LOC_MMREGISTER;
  67. end;
  68. function taarch64inlinenode.first_abs_real : tnode;
  69. begin
  70. expectloc:=LOC_MMREGISTER;
  71. result:=nil;
  72. if needs_check_for_fpu_exceptions then
  73. Include(current_procinfo.flags,pi_do_call);
  74. end;
  75. function taarch64inlinenode.first_sqr_real : tnode;
  76. begin
  77. expectloc:=LOC_MMREGISTER;
  78. result:=nil;
  79. if needs_check_for_fpu_exceptions then
  80. Include(current_procinfo.flags,pi_do_call);
  81. end;
  82. function taarch64inlinenode.first_sqrt_real : tnode;
  83. begin
  84. expectloc:=LOC_MMREGISTER;
  85. result:=nil;
  86. if needs_check_for_fpu_exceptions then
  87. Include(current_procinfo.flags,pi_do_call);
  88. end;
  89. function taarch64inlinenode.first_round_real: tnode;
  90. begin
  91. expectloc:=LOC_MMREGISTER;
  92. result:=nil;
  93. if needs_check_for_fpu_exceptions then
  94. Include(current_procinfo.flags,pi_do_call);
  95. end;
  96. function taarch64inlinenode.first_trunc_real: tnode;
  97. begin
  98. expectloc:=LOC_MMREGISTER;
  99. result:=nil;
  100. if needs_check_for_fpu_exceptions then
  101. Include(current_procinfo.flags,pi_do_call);
  102. end;
  103. function taarch64inlinenode.first_int_real : tnode;
  104. begin
  105. expectloc:=LOC_MMREGISTER;
  106. result:=nil;
  107. if needs_check_for_fpu_exceptions then
  108. Include(current_procinfo.flags,pi_do_call);
  109. end;
  110. function taarch64inlinenode.first_frac_real : tnode;
  111. begin
  112. expectloc:=LOC_MMREGISTER;
  113. result:=nil;
  114. if needs_check_for_fpu_exceptions then
  115. Include(current_procinfo.flags,pi_do_call);
  116. end;
  117. function taarch64inlinenode.first_fma : tnode;
  118. begin
  119. if ((is_double(resultdef)) or (is_single(resultdef))) then
  120. begin
  121. expectloc:=LOC_MMREGISTER;
  122. Result:=nil;
  123. end
  124. else
  125. Result:=inherited first_fma;
  126. end;
  127. procedure taarch64inlinenode.second_abs_real;
  128. begin
  129. load_fpu_location;
  130. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FABS,location.register,left.location.register));
  131. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  132. end;
  133. procedure taarch64inlinenode.second_sqr_real;
  134. begin
  135. load_fpu_location;
  136. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_FMUL,location.register,left.location.register,left.location.register));
  137. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  138. end;
  139. procedure taarch64inlinenode.second_sqrt_real;
  140. begin
  141. load_fpu_location;
  142. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSQRT,location.register,left.location.register));
  143. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  144. end;
  145. procedure taarch64inlinenode.second_abs_long;
  146. var
  147. opsize : tcgsize;
  148. begin
  149. secondpass(left);
  150. opsize:=def_cgsize(left.resultdef);
  151. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  152. location:=left.location;
  153. location.register:=cg.getintregister(current_asmdata.CurrAsmList,opsize);
  154. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_NEG,location.register,left.location.register),PF_S));
  155. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_cond(A_CSEL,location.register,location.register,left.location.register,C_GE));
  156. end;
  157. procedure taarch64inlinenode.second_round_real;
  158. var
  159. hreg: tregister;
  160. begin
  161. secondpass(left);
  162. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  163. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  164. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  165. hreg:=cg.getmmregister(current_asmdata.CurrAsmList,left.location.size);
  166. { round as floating point using current rounding mode }
  167. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FRINTX,hreg,left.location.register));
  168. { convert to signed integer rounding towards zero (there's no "round to
  169. integer using current rounding mode") }
  170. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FCVTZS,location.register,hreg));
  171. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  172. end;
  173. procedure taarch64inlinenode.second_trunc_real;
  174. begin
  175. secondpass(left);
  176. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  177. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  178. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  179. { convert to signed integer rounding towards zero }
  180. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FCVTZS,location.register,left.location.register));
  181. end;
  182. procedure taarch64inlinenode.second_int_real;
  183. var
  184. hreg: tregister;
  185. begin
  186. secondpass(left);
  187. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  188. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  189. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  190. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FRINTZ,location.register,left.location.register));
  191. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  192. end;
  193. procedure taarch64inlinenode.second_frac_real;
  194. var
  195. hreg: tregister;
  196. begin
  197. secondpass(left);
  198. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  199. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  200. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  201. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FRINTZ,location.register,left.location.register));
  202. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_FSUB,location.register,left.location.register,location.register));
  203. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  204. end;
  205. procedure taarch64inlinenode.second_get_frame;
  206. begin
  207. location_reset(location,LOC_CREGISTER,OS_ADDR);
  208. { this routine is used to get the frame pointer for backtracing
  209. purposes. current_procinfo.framepointer is set to SP because that one
  210. is used to access temps. On most platforms these two frame pointers
  211. are the same, but not on AArch64. }
  212. location.register:=NR_FRAME_POINTER_REG;
  213. end;
  214. procedure taarch64inlinenode.second_fma;
  215. const
  216. op : array[false..true,false..true] of TAsmOp =
  217. { positive product }
  218. (
  219. { positive third operand }
  220. (A_FMADD,
  221. { negative third operand }
  222. A_FNMSUB),
  223. { negative product }
  224. { positive third operand }
  225. (A_FMSUB,
  226. A_FNMADD)
  227. );
  228. var
  229. paraarray : array[1..3] of tnode;
  230. i : integer;
  231. negop3,
  232. negproduct : boolean;
  233. begin
  234. negop3:=false;
  235. negproduct:=false;
  236. paraarray[1]:=tcallparanode(tcallparanode(tcallparanode(parameters).nextpara).nextpara).paravalue;
  237. paraarray[2]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  238. paraarray[3]:=tcallparanode(parameters).paravalue;
  239. { check if a neg. node can be removed
  240. this is possible because changing the sign of
  241. a floating point number does not affect its absolute
  242. value in any way
  243. }
  244. if paraarray[1].nodetype=unaryminusn then
  245. begin
  246. paraarray[1]:=tunarynode(paraarray[1]).left;
  247. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  248. only no code is generated for it }
  249. negproduct:=not(negproduct);
  250. end;
  251. if paraarray[2].nodetype=unaryminusn then
  252. begin
  253. paraarray[2]:=tunarynode(paraarray[2]).left;
  254. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  255. only no code is generated for it }
  256. negproduct:=not(negproduct);
  257. end;
  258. if paraarray[3].nodetype=unaryminusn then
  259. begin
  260. paraarray[3]:=tunarynode(paraarray[3]).left;
  261. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  262. only no code is generated for it }
  263. negop3:=true;
  264. end;
  265. for i:=1 to 3 do
  266. secondpass(paraarray[i]);
  267. { no memory operand is allowed }
  268. for i:=1 to 3 do
  269. begin
  270. if not(paraarray[i].location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  271. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,paraarray[i].location,paraarray[i].resultdef,true);
  272. end;
  273. location_reset(location,LOC_MMREGISTER,paraarray[1].location.size);
  274. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  275. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_reg(op[negproduct,negop3],
  276. location.register,paraarray[1].location.register,paraarray[2].location.register,paraarray[3].location.register));
  277. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  278. end;
  279. procedure taarch64inlinenode.second_prefetch;
  280. var
  281. ref : treference;
  282. r : tregister;
  283. checkpointer_used : boolean;
  284. begin
  285. { do not call Checkpointer for left node }
  286. checkpointer_used:=(cs_checkpointer in current_settings.localswitches);
  287. if checkpointer_used then
  288. node_change_local_switch(left,cs_checkpointer,false);
  289. secondpass(left);
  290. if checkpointer_used then
  291. node_change_local_switch(left,cs_checkpointer,false);
  292. case left.location.loc of
  293. LOC_CREFERENCE,
  294. LOC_REFERENCE:
  295. begin
  296. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  297. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,r);
  298. reference_reset_base(ref,r,0,location.reference.temppos,left.location.reference.alignment,location.reference.volatility);
  299. current_asmdata.CurrAsmList.concat(taicpu.op_const_ref(A_PRFM,0,ref));
  300. end;
  301. else
  302. { nothing to prefetch };
  303. end;
  304. end;
  305. function taarch64inlinenode.first_minmax : tnode;
  306. begin
  307. if is_single(resultdef) or is_double(resultdef) then
  308. begin
  309. expectloc:=LOC_MMREGISTER;
  310. Result:=nil;
  311. if needs_check_for_fpu_exceptions then
  312. Include(current_procinfo.flags,pi_do_call);
  313. end
  314. else if is_32bitint(resultdef) then
  315. begin
  316. expectloc:=LOC_REGISTER;
  317. Result:=nil;
  318. end
  319. else
  320. Result:=inherited first_minmax;
  321. end;
  322. procedure taarch64inlinenode.second_minmax;
  323. var
  324. paraarray : array[1..2] of tnode;
  325. i: Integer;
  326. ai: taicpu;
  327. op: TAsmOp;
  328. begin
  329. paraarray[1]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  330. paraarray[2]:=tcallparanode(parameters).paravalue;
  331. for i:=low(paraarray) to high(paraarray) do
  332. secondpass(paraarray[i]);
  333. if is_single(resultdef) or is_double(resultdef) then
  334. begin
  335. { no memory operand is allowed }
  336. for i:=low(paraarray) to high(paraarray) do
  337. begin
  338. if not(paraarray[i].location.loc in [LOC_MMREGISTER,LOC_CMMREGISTER]) then
  339. hlcg.location_force_mmregscalar(current_asmdata.CurrAsmList,paraarray[i].location,
  340. paraarray[i].resultdef,true);
  341. end;
  342. location_reset(location,LOC_MMREGISTER,paraarray[1].location.size);
  343. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  344. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FCMP,
  345. paraarray[1].location.register,paraarray[2].location.register));
  346. case inlinenumber of
  347. in_min_single,
  348. in_min_double:
  349. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_cond(A_FCSEL,
  350. location.register,paraarray[1].location.register,paraarray[2].location.register,C_MI));
  351. in_max_single,
  352. in_max_double:
  353. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_cond(A_FCSEL,
  354. location.register,paraarray[1].location.register,paraarray[2].location.register,C_GT));
  355. else
  356. Internalerror(2021121802);
  357. end;
  358. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  359. end
  360. else if is_32bitint(resultdef) then
  361. begin
  362. { no memory operand is allowed }
  363. for i:=low(paraarray) to high(paraarray) do
  364. begin
  365. if not(paraarray[i].location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  366. hlcg.location_force_reg(current_asmdata.CurrAsmList,paraarray[i].location,
  367. paraarray[i].resultdef,paraarray[i].resultdef,true);
  368. end;
  369. location_reset(location,LOC_REGISTER,paraarray[1].location.size);
  370. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  371. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,
  372. paraarray[1].location.register,paraarray[2].location.register));
  373. case inlinenumber of
  374. in_min_dword,
  375. in_min_longint:
  376. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_cond(A_CSEL,
  377. location.register,paraarray[1].location.register,paraarray[2].location.register,C_LT));
  378. in_max_dword,
  379. in_max_longint:
  380. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_cond(A_CSEL,
  381. location.register,paraarray[1].location.register,paraarray[2].location.register,C_GT));
  382. else
  383. Internalerror(2021121901);
  384. end;
  385. end
  386. else
  387. internalerror(2021121801);
  388. end;
  389. begin
  390. cinlinenode:=taarch64inlinenode;
  391. end.