nrvinl.pas 13 KB

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