nrvinl.pas 11 KB

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