ncpuinl.pas 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. {
  2. Copyright (c) 1998-2017 by Florian Klaempfl
  3. Generates Xtensa 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 ncpuinl;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ninl,ncginl,aasmbase;
  22. type
  23. tcpuinlinenode = class(tcginlinenode)
  24. function first_abs_real: tnode; override;
  25. procedure second_abs_long; override;
  26. procedure second_abs_real; override;
  27. function first_fma: tnode; override;
  28. procedure second_fma; override;
  29. function first_minmax: tnode; override;
  30. procedure second_minmax; override;
  31. procedure second_prefetch; override;
  32. end;
  33. implementation
  34. uses
  35. cpuinfo,
  36. verbose,globals,globtype,
  37. compinnr,
  38. aasmdata,
  39. aasmcpu,
  40. symdef,
  41. defutil,
  42. hlcgobj,
  43. pass_2,
  44. procinfo,
  45. cgbase, cgobj, cgutils,
  46. ncal,nutils,
  47. cpubase;
  48. procedure tcpuinlinenode.second_abs_long;
  49. begin
  50. secondpass(left);
  51. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  52. location:=left.location;
  53. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  54. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_ABS,location.register,left.location.register));
  55. end;
  56. function tcpuinlinenode.first_abs_real : tnode;
  57. begin
  58. result:=nil;
  59. if is_single(left.resultdef) and (FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) then
  60. expectloc:=LOC_FPUREGISTER
  61. else
  62. result:=inherited first_abs_real;
  63. end;
  64. procedure tcpuinlinenode.second_abs_real;
  65. begin
  66. if not(is_single(resultdef)) then
  67. InternalError(2020091101);
  68. secondpass(left);
  69. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  70. location_reset(location,LOC_FPUREGISTER,OS_F32);
  71. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  72. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_ABS,location.register,left.location.register),PF_S));
  73. end;
  74. function tcpuinlinenode.first_fma : tnode;
  75. begin
  76. if is_single(resultdef) then
  77. begin
  78. expectloc:=LOC_FPUREGISTER;
  79. Result:=nil;
  80. if needs_check_for_fpu_exceptions then
  81. Include(current_procinfo.flags,pi_do_call);
  82. end
  83. else
  84. Result:=inherited first_fma;
  85. end;
  86. procedure tcpuinlinenode.second_fma;
  87. const
  88. op : array[false..true] of TAsmOp =
  89. (A_MADD,
  90. A_MSUB);
  91. var
  92. paraarray : array[1..3] of tnode;
  93. i : integer;
  94. negproduct : boolean;
  95. oppostfix : TOpPostfix;
  96. ai: taicpu;
  97. begin
  98. if is_single(resultdef)and
  99. (FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) then
  100. begin
  101. negproduct:=false;
  102. paraarray[1]:=tcallparanode(tcallparanode(tcallparanode(parameters).nextpara).nextpara).paravalue;
  103. paraarray[2]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  104. paraarray[3]:=tcallparanode(parameters).paravalue;
  105. { check if a neg. node can be removed
  106. this is possible because changing the sign of
  107. a floating point number does not affect its absolute
  108. value in any way
  109. }
  110. if paraarray[1].nodetype=unaryminusn then
  111. begin
  112. paraarray[1]:=tunarynode(paraarray[1]).left;
  113. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  114. only no code is generated for it }
  115. negproduct:=not(negproduct);
  116. end;
  117. if paraarray[2].nodetype=unaryminusn then
  118. begin
  119. paraarray[2]:=tunarynode(paraarray[2]).left;
  120. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  121. only no code is generated for it }
  122. negproduct:=not(negproduct);
  123. end;
  124. for i:=1 to 3 do
  125. secondpass(paraarray[i]);
  126. { no memory operand is allowed }
  127. for i:=1 to 3 do
  128. begin
  129. if not(paraarray[i].location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  130. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,paraarray[i].location,paraarray[i].resultdef,true);
  131. end;
  132. location_reset(location,LOC_FPUREGISTER,paraarray[1].location.size);
  133. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  134. hlcg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,paraarray[3].resultdef,resultdef,
  135. paraarray[3].location.register,location.register);
  136. ai:=taicpu.op_reg_reg_reg(op[negproduct],
  137. location.register,paraarray[1].location.register,paraarray[2].location.register);
  138. ai.oppostfix:=PF_S;
  139. current_asmdata.CurrAsmList.concat(ai);
  140. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  141. end
  142. else
  143. internalerror(2020112401);
  144. end;
  145. function tcpuinlinenode.first_minmax : tnode;
  146. begin
  147. if is_32bitint(resultdef) then
  148. begin
  149. expectloc:=LOC_REGISTER;
  150. Result:=nil;
  151. if needs_check_for_fpu_exceptions then
  152. Include(current_procinfo.flags,pi_do_call);
  153. end
  154. else
  155. Result:=inherited first_minmax;
  156. end;
  157. procedure tcpuinlinenode.second_minmax;
  158. var
  159. paraarray : array[1..2] of tnode;
  160. i: Integer;
  161. ai: taicpu;
  162. op: TAsmOp;
  163. begin
  164. if is_32bitint(resultdef) then
  165. begin
  166. paraarray[1]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  167. paraarray[2]:=tcallparanode(parameters).paravalue;
  168. for i:=low(paraarray) to high(paraarray) do
  169. secondpass(paraarray[i]);
  170. { no memory operand is allowed }
  171. for i:=low(paraarray) to high(paraarray) do
  172. begin
  173. if not(paraarray[i].location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  174. hlcg.location_force_reg(current_asmdata.CurrAsmList,paraarray[i].location,
  175. paraarray[i].resultdef,resultdef,true);
  176. end;
  177. location_reset(location,LOC_REGISTER,paraarray[1].location.size);
  178. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  179. case inlinenumber of
  180. in_min_dword:
  181. op:=A_MINU;
  182. in_max_dword:
  183. op:=A_MAXU;
  184. in_min_longint:
  185. op:=A_MIN;
  186. in_max_longint:
  187. op:=A_MAX;
  188. else
  189. Internalerror(2020120505);
  190. end;
  191. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  192. location.register,paraarray[1].location.register,paraarray[2].location.register));
  193. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  194. end
  195. else
  196. internalerror(2020120502);
  197. end;
  198. procedure tcpuinlinenode.second_prefetch;
  199. var
  200. ref : treference;
  201. r : tregister;
  202. checkpointer_used : boolean;
  203. begin
  204. { do not call Checkpointer for left node }
  205. checkpointer_used:=(cs_checkpointer in current_settings.localswitches);
  206. if checkpointer_used then
  207. node_change_local_switch(left,cs_checkpointer,false);
  208. secondpass(left);
  209. if checkpointer_used then
  210. node_change_local_switch(left,cs_checkpointer,false);
  211. case left.location.loc of
  212. LOC_CREFERENCE,
  213. LOC_REFERENCE:
  214. begin
  215. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  216. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,r);
  217. reference_reset_base(ref,r,0,location.reference.temppos,left.location.reference.alignment,location.reference.volatility);
  218. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_DPFR,ref.base,ref.offset));
  219. end;
  220. else
  221. { nothing to prefetch };
  222. end;
  223. end;
  224. begin
  225. cinlinenode:=tcpuinlinenode;
  226. end.