ncpuinl.pas 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. var
  50. hl: TAsmLabel;
  51. begin
  52. if is_64bitint(resultdef) then
  53. begin
  54. inherited second_abs_long;
  55. exit;
  56. end;
  57. secondpass(left);
  58. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  59. location:=left.location;
  60. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  61. if cs_check_overflow in current_settings.localswitches then
  62. begin
  63. current_asmdata.getjumplabel(hl);
  64. hlcg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,resultdef,OC_NE,$80000000,left.location.register,hl);
  65. hlcg.g_call_system_proc(current_asmdata.CurrAsmList,'fpc_overflow',[],nil).resetiftemp;
  66. hlcg.a_label(current_asmdata.CurrAsmList,hl);
  67. end;
  68. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_ABS,location.register,left.location.register));
  69. end;
  70. function tcpuinlinenode.first_abs_real : tnode;
  71. begin
  72. result:=nil;
  73. if is_single(left.resultdef) and (FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) then
  74. expectloc:=LOC_FPUREGISTER
  75. else
  76. result:=inherited first_abs_real;
  77. end;
  78. procedure tcpuinlinenode.second_abs_real;
  79. begin
  80. if not(is_single(resultdef)) then
  81. InternalError(2020091101);
  82. secondpass(left);
  83. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  84. location_reset(location,LOC_FPUREGISTER,OS_F32);
  85. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  86. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_ABS,location.register,left.location.register),PF_S));
  87. end;
  88. function tcpuinlinenode.first_fma : tnode;
  89. begin
  90. if is_single(resultdef) then
  91. begin
  92. expectloc:=LOC_FPUREGISTER;
  93. Result:=nil;
  94. if needs_check_for_fpu_exceptions then
  95. Include(current_procinfo.flags,pi_do_call);
  96. end
  97. else
  98. Result:=inherited first_fma;
  99. end;
  100. procedure tcpuinlinenode.second_fma;
  101. const
  102. op : array[false..true] of TAsmOp =
  103. (A_MADD,
  104. A_MSUB);
  105. var
  106. paraarray : array[1..3] of tnode;
  107. i : integer;
  108. negproduct : boolean;
  109. oppostfix : TOpPostfix;
  110. ai: taicpu;
  111. begin
  112. if is_single(resultdef)and
  113. (FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) then
  114. begin
  115. negproduct:=false;
  116. paraarray[1]:=tcallparanode(tcallparanode(tcallparanode(parameters).nextpara).nextpara).paravalue;
  117. paraarray[2]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  118. paraarray[3]:=tcallparanode(parameters).paravalue;
  119. { check if a neg. node can be removed
  120. this is possible because changing the sign of
  121. a floating point number does not affect its absolute
  122. value in any way
  123. }
  124. if paraarray[1].nodetype=unaryminusn then
  125. begin
  126. paraarray[1]:=tunarynode(paraarray[1]).left;
  127. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  128. only no code is generated for it }
  129. negproduct:=not(negproduct);
  130. end;
  131. if paraarray[2].nodetype=unaryminusn then
  132. begin
  133. paraarray[2]:=tunarynode(paraarray[2]).left;
  134. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  135. only no code is generated for it }
  136. negproduct:=not(negproduct);
  137. end;
  138. for i:=1 to 3 do
  139. secondpass(paraarray[i]);
  140. { no memory operand is allowed }
  141. for i:=1 to 3 do
  142. begin
  143. if not(paraarray[i].location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  144. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,paraarray[i].location,paraarray[i].resultdef,true);
  145. end;
  146. location_reset(location,LOC_FPUREGISTER,paraarray[1].location.size);
  147. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  148. hlcg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,paraarray[3].resultdef,resultdef,
  149. paraarray[3].location.register,location.register);
  150. ai:=taicpu.op_reg_reg_reg(op[negproduct],
  151. location.register,paraarray[1].location.register,paraarray[2].location.register);
  152. ai.oppostfix:=PF_S;
  153. current_asmdata.CurrAsmList.concat(ai);
  154. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  155. end
  156. else
  157. internalerror(2020112401);
  158. end;
  159. function tcpuinlinenode.first_minmax : tnode;
  160. begin
  161. if is_32bitint(resultdef) then
  162. begin
  163. expectloc:=LOC_REGISTER;
  164. Result:=nil;
  165. if needs_check_for_fpu_exceptions then
  166. Include(current_procinfo.flags,pi_do_call);
  167. end
  168. else
  169. Result:=inherited first_minmax;
  170. end;
  171. procedure tcpuinlinenode.second_minmax;
  172. var
  173. paraarray : array[1..2] of tnode;
  174. i: Integer;
  175. ai: taicpu;
  176. op: TAsmOp;
  177. begin
  178. if is_32bitint(resultdef) then
  179. begin
  180. paraarray[1]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  181. paraarray[2]:=tcallparanode(parameters).paravalue;
  182. for i:=low(paraarray) to high(paraarray) do
  183. secondpass(paraarray[i]);
  184. { no memory operand is allowed }
  185. for i:=low(paraarray) to high(paraarray) do
  186. begin
  187. if not(paraarray[i].location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  188. hlcg.location_force_reg(current_asmdata.CurrAsmList,paraarray[i].location,
  189. paraarray[i].resultdef,resultdef,true);
  190. end;
  191. location_reset(location,LOC_REGISTER,paraarray[1].location.size);
  192. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  193. case inlinenumber of
  194. in_min_dword:
  195. op:=A_MINU;
  196. in_max_dword:
  197. op:=A_MAXU;
  198. in_min_longint:
  199. op:=A_MIN;
  200. in_max_longint:
  201. op:=A_MAX;
  202. else
  203. Internalerror(2020120505);
  204. end;
  205. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  206. location.register,paraarray[1].location.register,paraarray[2].location.register));
  207. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  208. end
  209. else
  210. internalerror(2020120502);
  211. end;
  212. procedure tcpuinlinenode.second_prefetch;
  213. var
  214. ref : treference;
  215. r : tregister;
  216. checkpointer_used : boolean;
  217. begin
  218. { do not call Checkpointer for left node }
  219. checkpointer_used:=(cs_checkpointer in current_settings.localswitches);
  220. if checkpointer_used then
  221. node_change_local_switch(left,cs_checkpointer,false);
  222. secondpass(left);
  223. if checkpointer_used then
  224. node_change_local_switch(left,cs_checkpointer,false);
  225. case left.location.loc of
  226. LOC_CREFERENCE,
  227. LOC_REFERENCE:
  228. begin
  229. r:=cg.getintregister(current_asmdata.CurrAsmList,OS_ADDR);
  230. cg.a_loadaddr_ref_reg(current_asmdata.CurrAsmList,left.location.reference,r);
  231. reference_reset_base(ref,r,0,location.reference.temppos,left.location.reference.alignment,location.reference.volatility);
  232. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_DPFR,ref.base,ref.offset));
  233. end;
  234. else
  235. { nothing to prefetch };
  236. end;
  237. end;
  238. begin
  239. cinlinenode:=tcpuinlinenode;
  240. end.