nrvinl.pas 16 KB

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