nrvinl.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate Risc-V32/64 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 nrvinl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cpubase,
  22. node,ninl,ncginl;
  23. type
  24. trvinlinenode = class(tcginlinenode)
  25. { first pass override
  26. so that the code generator will actually generate
  27. these nodes.
  28. }
  29. function first_sqrt_real: tnode; override;
  30. function first_abs_real: tnode; override;
  31. function first_sqr_real: tnode; override;
  32. function first_round_real: tnode; override;
  33. function first_trunc_real: tnode; override;
  34. function first_fma: tnode; override;
  35. function first_minmax: tnode; override;
  36. procedure second_sqrt_real; override;
  37. procedure second_abs_real; override;
  38. procedure second_sqr_real; override;
  39. procedure second_round_real; override;
  40. procedure second_trunc_real; override;
  41. procedure second_fma; override;
  42. procedure second_minmax; override;
  43. protected
  44. procedure load_fpu_location;
  45. end;
  46. implementation
  47. uses
  48. ncal,
  49. cutils,globals,verbose,globtype,
  50. compinnr,
  51. aasmtai,aasmdata,aasmcpu,
  52. symconst,symdef,
  53. defutil,
  54. procinfo,
  55. cgbase,pass_2,
  56. cpuinfo,ncgutil,
  57. hlcgobj,cgutils,cgobj,rgobj,tgobj;
  58. {*****************************************************************************
  59. trvinlinenode
  60. *****************************************************************************}
  61. function trvinlinenode.first_sqrt_real : tnode;
  62. begin
  63. if (current_settings.fputype >= fpu_fd) then
  64. begin
  65. expectloc:=LOC_FPUREGISTER;
  66. first_sqrt_real := nil;
  67. if needs_check_for_fpu_exceptions then
  68. Include(current_procinfo.flags,pi_do_call);
  69. end
  70. else
  71. result:=inherited first_sqrt_real;
  72. end;
  73. function trvinlinenode.first_abs_real : tnode;
  74. begin
  75. if (current_settings.fputype >= fpu_fd) then
  76. begin
  77. expectloc:=LOC_FPUREGISTER;
  78. first_abs_real := nil;
  79. end
  80. else
  81. result:=inherited first_abs_real;
  82. end;
  83. function trvinlinenode.first_sqr_real : tnode;
  84. begin
  85. if (current_settings.fputype >= fpu_fd) then
  86. begin
  87. expectloc:=LOC_FPUREGISTER;
  88. first_sqr_real := nil;
  89. if needs_check_for_fpu_exceptions then
  90. Include(current_procinfo.flags,pi_do_call);
  91. end
  92. else
  93. result:=inherited first_sqr_real;
  94. end;
  95. function trvinlinenode.first_round_real: tnode;
  96. begin
  97. if
  98. {$ifdef RISCV32}
  99. is_32bitint(resultdef) and
  100. {$endif RISCV32}
  101. (current_settings.fputype >= fpu_fd) then
  102. begin
  103. expectloc:=LOC_FPUREGISTER;
  104. first_round_real := nil;
  105. if needs_check_for_fpu_exceptions then
  106. Include(current_procinfo.flags,pi_do_call);
  107. end
  108. else
  109. result:=inherited first_round_real;
  110. end;
  111. function trvinlinenode.first_trunc_real: tnode;
  112. begin
  113. if
  114. {$ifdef RISCV32}
  115. is_32bitint(resultdef) and
  116. {$endif RISCV32}
  117. (current_settings.fputype >= fpu_fd) then
  118. begin
  119. expectloc:=LOC_FPUREGISTER;
  120. first_trunc_real := nil;
  121. if needs_check_for_fpu_exceptions then
  122. Include(current_procinfo.flags,pi_do_call);
  123. end
  124. else
  125. result:=inherited first_trunc_real;
  126. end;
  127. function trvinlinenode.first_fma: tnode;
  128. begin
  129. if needs_check_for_fpu_exceptions then
  130. Include(current_procinfo.flags,pi_do_call);
  131. Result:=nil;
  132. end;
  133. function trvinlinenode.first_minmax : tnode;
  134. begin
  135. if is_single(resultdef) or is_double(resultdef) or is_quad(resultdef) then
  136. begin
  137. expectloc:=LOC_FPUREGISTER;
  138. Result:=nil;
  139. if needs_check_for_fpu_exceptions then
  140. Include(current_procinfo.flags,pi_do_call);
  141. end
  142. else
  143. Result:=inherited first_minmax;
  144. end;
  145. { load the FPU into the an fpu register }
  146. procedure trvinlinenode.load_fpu_location;
  147. begin
  148. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  149. secondpass(left);
  150. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  151. location.loc := LOC_FPUREGISTER;
  152. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  153. end;
  154. procedure trvinlinenode.second_sqrt_real;
  155. begin
  156. location.loc:=LOC_FPUREGISTER;
  157. load_fpu_location;
  158. case left.location.size of
  159. OS_F32:
  160. begin
  161. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSQRT_S,location.register,
  162. left.location.register));
  163. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  164. end;
  165. OS_F64:
  166. begin
  167. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSQRT_D,location.register,
  168. left.location.register));
  169. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  170. end
  171. else
  172. inherited;
  173. end;
  174. end;
  175. procedure trvinlinenode.second_abs_real;
  176. var
  177. op: TAsmOp;
  178. begin
  179. location.loc:=LOC_FPUREGISTER;
  180. load_fpu_location;
  181. if (left.location.size = OS_F32) then
  182. op := A_FSGNJX_S
  183. else
  184. op := A_FSGNJX_D;
  185. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,left.location.register));
  186. end;
  187. procedure trvinlinenode.second_sqr_real;
  188. var
  189. op: tasmop;
  190. begin
  191. location.loc:=LOC_FPUREGISTER;
  192. load_fpu_location;
  193. if (left.location.size = OS_F32) then
  194. op := A_FMUL_S
  195. else
  196. op := A_FMUL_D;
  197. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,left.location.register));
  198. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  199. end;
  200. procedure trvinlinenode.second_round_real;
  201. var
  202. op: TAsmOp;
  203. begin
  204. secondpass(left);
  205. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  206. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  207. {$ifdef RISCV32}
  208. if (location.size in [OS_S64,OS_64]) then
  209. begin
  210. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  211. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  212. end
  213. else
  214. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  215. {$else}
  216. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  217. {$endif}
  218. { convert to signed integer rounding towards zero (there's no "round to
  219. integer using current rounding mode") }
  220. {$ifdef RISCV32}
  221. if (left.location.size = OS_F32) then
  222. op := A_FCVT_W_S
  223. else
  224. op := A_FCVT_W_D;
  225. {$else}
  226. if (left.location.size = OS_F32) then
  227. op := A_FCVT_L_S
  228. else
  229. op := A_FCVT_L_D;
  230. {$endif}
  231. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,location.register,left.location.register));
  232. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  233. end;
  234. procedure trvinlinenode.second_trunc_real;
  235. var
  236. op: TAsmOp;
  237. begin
  238. secondpass(left);
  239. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  240. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  241. {$ifdef RISCV32}
  242. if (location.size in [OS_S64,OS_64]) then
  243. begin
  244. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  245. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  246. end
  247. else
  248. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  249. {$else}
  250. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  251. {$endif}
  252. { convert to signed integer rounding towards zero (there's no "round to
  253. integer using current rounding mode") }
  254. {$ifdef RISCV32}
  255. if (left.location.size = OS_F32) then
  256. op := A_FCVT_W_S
  257. else
  258. op := A_FCVT_W_D;
  259. {$else}
  260. if (left.location.size = OS_F32) then
  261. op := A_FCVT_L_S
  262. else
  263. op := A_FCVT_L_D;
  264. {$endif}
  265. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_roundingmode(op,location.register,left.location.register,RM_RTZ));
  266. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  267. end;
  268. procedure trvinlinenode.second_fma;
  269. const
  270. op : array[os_f32..os_f64,false..true,false..true] of TAsmOp =
  271. (
  272. (
  273. (A_FMADD_S,A_FMSUB_S),
  274. (A_FNMADD_S,A_FNMSUB_S)
  275. ),
  276. (
  277. (A_FMADD_D,A_FMSUB_D),
  278. (A_FNMADD_D,A_FNMSUB_D)
  279. )
  280. );
  281. var
  282. paraarray : array[1..3] of tnode;
  283. i : integer;
  284. negop3,
  285. negproduct : boolean;
  286. begin
  287. if current_settings.fputype in [fpu_fd] then
  288. begin
  289. negop3:=false;
  290. negproduct:=false;
  291. paraarray[1]:=tcallparanode(tcallparanode(tcallparanode(parameters).nextpara).nextpara).paravalue;
  292. paraarray[2]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  293. paraarray[3]:=tcallparanode(parameters).paravalue;
  294. { check if a neg. node can be removed
  295. this is possible because changing the sign of
  296. a floating point number does not affect its absolute
  297. value in any way
  298. }
  299. if paraarray[1].nodetype=unaryminusn then
  300. begin
  301. paraarray[1]:=tunarynode(paraarray[1]).left;
  302. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  303. only no code is generated for it }
  304. negproduct:=not(negproduct);
  305. end;
  306. if paraarray[2].nodetype=unaryminusn then
  307. begin
  308. paraarray[2]:=tunarynode(paraarray[2]).left;
  309. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  310. only no code is generated for it }
  311. negproduct:=not(negproduct);
  312. end;
  313. if paraarray[3].nodetype=unaryminusn then
  314. begin
  315. paraarray[3]:=tunarynode(paraarray[3]).left;
  316. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  317. only no code is generated for it }
  318. negop3:=true;
  319. end;
  320. for i:=1 to 3 do
  321. secondpass(paraarray[i]);
  322. { no memory operand is allowed }
  323. for i:=1 to 3 do
  324. begin
  325. if not(paraarray[i].location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  326. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,paraarray[i].location,paraarray[i].resultdef,true);
  327. end;
  328. location_reset(location,LOC_FPUREGISTER,paraarray[1].location.size);
  329. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  330. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg_reg(op[def_cgsize(resultdef), negproduct,negop3],
  331. location.register,paraarray[1].location.register,paraarray[2].location.register,paraarray[3].location.register));
  332. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  333. end
  334. else
  335. internalerror(2014032301);
  336. end;
  337. procedure trvinlinenode.second_minmax;
  338. var
  339. paraarray : array[1..2] of tnode;
  340. i: Integer;
  341. ai: taicpu;
  342. opcode: TAsmOp;
  343. cond: TAsmCond;
  344. begin
  345. paraarray[1]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  346. paraarray[2]:=tcallparanode(parameters).paravalue;
  347. for i:=low(paraarray) to high(paraarray) do
  348. secondpass(paraarray[i]);
  349. if is_single(resultdef) or is_double(resultdef) then
  350. begin
  351. { no memory operand is allowed }
  352. for i:=low(paraarray) to high(paraarray) do
  353. begin
  354. if not(paraarray[i].location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  355. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,paraarray[i].location,
  356. paraarray[i].resultdef,true);
  357. end;
  358. location_reset(location,LOC_FPUREGISTER,paraarray[1].location.size);
  359. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  360. case inlinenumber of
  361. in_min_single:
  362. opcode:=A_FMIN_S;
  363. in_min_double:
  364. opcode:=A_FMIN_D;
  365. in_min_quad:
  366. opcode:=A_FMAX_Q;
  367. in_max_single:
  368. opcode:=A_FMAX_S;
  369. in_max_double:
  370. opcode:=A_FMAX_D;
  371. in_max_quad:
  372. opcode:=A_FMAX_Q;
  373. else
  374. Internalerror(2025010502);
  375. end;
  376. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(opcode,
  377. location.register,paraarray[1].location.register,paraarray[2].location.register));
  378. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  379. end
  380. else
  381. internalerror(2025010501);
  382. end;
  383. begin
  384. cinlinenode:=trvinlinenode;
  385. end.