nrvinl.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 (current_settings.fputype >= fpu_fd) then
  96. begin
  97. expectloc:=LOC_FPUREGISTER;
  98. first_round_real := nil;
  99. if needs_check_for_fpu_exceptions then
  100. Include(current_procinfo.flags,pi_do_call);
  101. end
  102. else
  103. result:=inherited first_round_real;
  104. end;
  105. function trvinlinenode.first_trunc_real: tnode;
  106. begin
  107. if (current_settings.fputype >= fpu_fd) then
  108. begin
  109. expectloc:=LOC_FPUREGISTER;
  110. first_trunc_real := nil;
  111. if needs_check_for_fpu_exceptions then
  112. Include(current_procinfo.flags,pi_do_call);
  113. end
  114. else
  115. result:=inherited first_trunc_real;
  116. end;
  117. function trvinlinenode.first_fma: tnode;
  118. begin
  119. if needs_check_for_fpu_exceptions then
  120. Include(current_procinfo.flags,pi_do_call);
  121. Result:=nil;
  122. end;
  123. { load the FPU into the an fpu register }
  124. procedure trvinlinenode.load_fpu_location;
  125. begin
  126. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  127. secondpass(left);
  128. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  129. location.loc := LOC_FPUREGISTER;
  130. location.register := cg.getfpuregister(current_asmdata.CurrAsmList,def_cgsize(resultdef));
  131. end;
  132. procedure trvinlinenode.second_sqrt_real;
  133. begin
  134. location.loc:=LOC_FPUREGISTER;
  135. load_fpu_location;
  136. case left.location.size of
  137. OS_F32:
  138. begin
  139. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSQRT_S,location.register,
  140. left.location.register));
  141. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  142. end;
  143. OS_F64:
  144. begin
  145. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_FSQRT_D,location.register,
  146. left.location.register));
  147. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  148. end
  149. else
  150. inherited;
  151. end;
  152. end;
  153. procedure trvinlinenode.second_abs_real;
  154. var
  155. op: TAsmOp;
  156. begin
  157. location.loc:=LOC_FPUREGISTER;
  158. load_fpu_location;
  159. if (left.location.size = OS_F32) then
  160. op := A_FSGNJX_S
  161. else
  162. op := A_FSGNJX_D;
  163. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,left.location.register));
  164. end;
  165. procedure trvinlinenode.second_sqr_real;
  166. var
  167. op: tasmop;
  168. begin
  169. location.loc:=LOC_FPUREGISTER;
  170. load_fpu_location;
  171. if (left.location.size = OS_F32) then
  172. op := A_FMUL_S
  173. else
  174. op := A_FMUL_D;
  175. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,left.location.register));
  176. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  177. end;
  178. procedure trvinlinenode.second_round_real;
  179. var
  180. op: TAsmOp;
  181. begin
  182. secondpass(left);
  183. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  184. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  185. {$ifdef RISCV32}
  186. if (location.size in [OS_S64,OS_64]) then
  187. begin
  188. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  189. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  190. end
  191. else
  192. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  193. {$else}
  194. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  195. {$endif}
  196. { convert to signed integer rounding towards zero (there's no "round to
  197. integer using current rounding mode") }
  198. {$ifdef RISCV32}
  199. if (left.location.size = OS_F32) then
  200. op := A_FCVT_W_S
  201. else
  202. op := A_FCVT_W_D;
  203. {$else}
  204. if (left.location.size = OS_F32) then
  205. op := A_FCVT_L_S
  206. else
  207. op := A_FCVT_L_D;
  208. {$endif}
  209. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,location.register,left.location.register));
  210. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  211. end;
  212. procedure trvinlinenode.second_trunc_real;
  213. var
  214. op: TAsmOp;
  215. begin
  216. secondpass(left);
  217. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  218. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  219. {$ifdef RISCV32}
  220. if (location.size in [OS_S64,OS_64]) then
  221. begin
  222. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  223. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_32);
  224. end
  225. else
  226. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  227. {$else}
  228. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  229. {$endif}
  230. { convert to signed integer rounding towards zero (there's no "round to
  231. integer using current rounding mode") }
  232. {$ifdef RISCV32}
  233. if (left.location.size = OS_F32) then
  234. op := A_FCVT_W_S
  235. else
  236. op := A_FCVT_W_D;
  237. {$else}
  238. if (left.location.size = OS_F32) then
  239. op := A_FCVT_L_S
  240. else
  241. op := A_FCVT_L_D;
  242. {$endif}
  243. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_roundingmode(op,location.register,left.location.register,RM_RTZ));
  244. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  245. end;
  246. procedure trvinlinenode.second_fma;
  247. const
  248. op : array[os_f32..os_f64,false..true,false..true] of TAsmOp =
  249. (
  250. (
  251. (A_FMADD_S,A_FMSUB_S),
  252. (A_FNMADD_S,A_FNMSUB_S)
  253. ),
  254. (
  255. (A_FMADD_D,A_FMSUB_D),
  256. (A_FNMADD_D,A_FNMSUB_D)
  257. )
  258. );
  259. var
  260. paraarray : array[1..3] of tnode;
  261. i : integer;
  262. negop3,
  263. negproduct : boolean;
  264. begin
  265. if current_settings.fputype in [fpu_fd] then
  266. begin
  267. negop3:=false;
  268. negproduct:=false;
  269. paraarray[1]:=tcallparanode(tcallparanode(tcallparanode(parameters).nextpara).nextpara).paravalue;
  270. paraarray[2]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  271. paraarray[3]:=tcallparanode(parameters).paravalue;
  272. { check if a neg. node can be removed
  273. this is possible because changing the sign of
  274. a floating point number does not affect its absolute
  275. value in any way
  276. }
  277. if paraarray[1].nodetype=unaryminusn then
  278. begin
  279. paraarray[1]:=tunarynode(paraarray[1]).left;
  280. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  281. only no code is generated for it }
  282. negproduct:=not(negproduct);
  283. end;
  284. if paraarray[2].nodetype=unaryminusn then
  285. begin
  286. paraarray[2]:=tunarynode(paraarray[2]).left;
  287. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  288. only no code is generated for it }
  289. negproduct:=not(negproduct);
  290. end;
  291. if paraarray[3].nodetype=unaryminusn then
  292. begin
  293. paraarray[3]:=tunarynode(paraarray[3]).left;
  294. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  295. only no code is generated for it }
  296. negop3:=true;
  297. end;
  298. for i:=1 to 3 do
  299. secondpass(paraarray[i]);
  300. { no memory operand is allowed }
  301. for i:=1 to 3 do
  302. begin
  303. if not(paraarray[i].location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  304. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,paraarray[i].location,paraarray[i].resultdef,true);
  305. end;
  306. location_reset(location,LOC_FPUREGISTER,paraarray[1].location.size);
  307. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  308. 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));
  309. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  310. end
  311. else
  312. internalerror(2014032301);
  313. end;
  314. begin
  315. cinlinenode:=trvinlinenode;
  316. end.