2
0

ncpuinl.pas 16 KB

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