ncpuinl.pas 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. end;
  32. implementation
  33. uses
  34. cpuinfo,
  35. verbose,globals,
  36. compinnr,
  37. aasmdata,
  38. aasmcpu,
  39. symdef,
  40. defutil,
  41. hlcgobj,
  42. pass_2,
  43. cgbase, cgobj, cgutils,
  44. ncal,
  45. cpubase;
  46. procedure tcpuinlinenode.second_abs_long;
  47. begin
  48. secondpass(left);
  49. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  50. location:=left.location;
  51. location.register:=hlcg.getintregister(current_asmdata.CurrAsmList,left.resultdef);
  52. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_ABS,location.register,left.location.register));
  53. end;
  54. function tcpuinlinenode.first_abs_real : tnode;
  55. begin
  56. result:=nil;
  57. if is_single(left.resultdef) and (FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) then
  58. expectloc:=LOC_FPUREGISTER
  59. else
  60. result:=inherited first_abs_real;
  61. end;
  62. procedure tcpuinlinenode.second_abs_real;
  63. begin
  64. if not(is_single(resultdef)) then
  65. InternalError(2020091101);
  66. secondpass(left);
  67. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  68. location_reset(location,LOC_FPUREGISTER,OS_F32);
  69. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  70. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_ABS,location.register,left.location.register),PF_S));
  71. end;
  72. function tcpuinlinenode.first_fma : tnode;
  73. begin
  74. if is_single(resultdef) then
  75. begin
  76. expectloc:=LOC_FPUREGISTER;
  77. Result:=nil;
  78. end
  79. else
  80. Result:=inherited first_fma;
  81. end;
  82. procedure tcpuinlinenode.second_fma;
  83. const
  84. op : array[false..true] of TAsmOp =
  85. (A_MADD,
  86. A_MSUB);
  87. var
  88. paraarray : array[1..3] of tnode;
  89. i : integer;
  90. negproduct : boolean;
  91. oppostfix : TOpPostfix;
  92. ai: taicpu;
  93. begin
  94. if is_single(resultdef)and
  95. (FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) then
  96. begin
  97. negproduct:=false;
  98. paraarray[1]:=tcallparanode(tcallparanode(tcallparanode(parameters).nextpara).nextpara).paravalue;
  99. paraarray[2]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  100. paraarray[3]:=tcallparanode(parameters).paravalue;
  101. { check if a neg. node can be removed
  102. this is possible because changing the sign of
  103. a floating point number does not affect its absolute
  104. value in any way
  105. }
  106. if paraarray[1].nodetype=unaryminusn then
  107. begin
  108. paraarray[1]:=tunarynode(paraarray[1]).left;
  109. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  110. only no code is generated for it }
  111. negproduct:=not(negproduct);
  112. end;
  113. if paraarray[2].nodetype=unaryminusn then
  114. begin
  115. paraarray[2]:=tunarynode(paraarray[2]).left;
  116. { do not release the unused unary minus node, it is kept and release together with the other nodes,
  117. only no code is generated for it }
  118. negproduct:=not(negproduct);
  119. end;
  120. for i:=1 to 3 do
  121. secondpass(paraarray[i]);
  122. { no memory operand is allowed }
  123. for i:=1 to 3 do
  124. begin
  125. if not(paraarray[i].location.loc in [LOC_FPUREGISTER,LOC_CFPUREGISTER]) then
  126. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,paraarray[i].location,paraarray[i].resultdef,true);
  127. end;
  128. location_reset(location,LOC_FPUREGISTER,paraarray[1].location.size);
  129. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  130. hlcg.a_loadfpu_reg_reg(current_asmdata.CurrAsmList,paraarray[3].resultdef,resultdef,
  131. paraarray[3].location.register,location.register);
  132. ai:=taicpu.op_reg_reg_reg(op[negproduct],
  133. location.register,paraarray[1].location.register,paraarray[2].location.register);
  134. ai.oppostfix:=PF_S;
  135. current_asmdata.CurrAsmList.concat(ai);
  136. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  137. end
  138. else
  139. internalerror(2020112401);
  140. end;
  141. function tcpuinlinenode.first_minmax : tnode;
  142. begin
  143. if is_32bitint(resultdef) then
  144. begin
  145. expectloc:=LOC_REGISTER;
  146. Result:=nil;
  147. end
  148. else
  149. Result:=inherited first_minmax;
  150. end;
  151. procedure tcpuinlineNode.second_minmax;
  152. var
  153. paraarray : array[1..2] of tnode;
  154. i: Integer;
  155. ai: taicpu;
  156. op: TAsmOp;
  157. begin
  158. if is_32bitint(resultdef) then
  159. begin
  160. paraarray[1]:=tcallparanode(tcallparanode(parameters).nextpara).paravalue;
  161. paraarray[2]:=tcallparanode(parameters).paravalue;
  162. for i:=low(paraarray) to high(paraarray) do
  163. secondpass(paraarray[i]);
  164. { no memory operand is allowed }
  165. for i:=low(paraarray) to high(paraarray) do
  166. begin
  167. if not(paraarray[i].location.loc in [LOC_REGISTER,LOC_CREGISTER]) then
  168. hlcg.location_force_reg(current_asmdata.CurrAsmList,paraarray[i].location,
  169. paraarray[i].resultdef,resultdef,true);
  170. end;
  171. location_reset(location,LOC_REGISTER,paraarray[1].location.size);
  172. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  173. case inlinenumber of
  174. in_min_dword:
  175. op:=A_MINU;
  176. in_max_dword:
  177. op:=A_MAXU;
  178. in_min_longint:
  179. op:=A_MIN;
  180. in_max_longint:
  181. op:=A_MAX;
  182. else
  183. Internalerror(2020120505);
  184. end;
  185. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  186. location.register,paraarray[1].location.register,paraarray[2].location.register));
  187. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  188. end
  189. else
  190. internalerror(2020120502);
  191. end;
  192. begin
  193. cinlinenode:=tcpuinlinenode;
  194. end.