ncpuadd.pas 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. {
  2. Copyright (c) 2008 by Florian Klaempfl
  3. Code generation for add nodes on the Xtensa
  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 ncpuadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncgadd,cpubase;
  22. type
  23. TCPUAddNode = class(tcgaddnode)
  24. private
  25. procedure pass_left_and_right;
  26. protected
  27. function pass_1 : tnode;override;
  28. function first_addfloat: tnode;override;
  29. procedure second_cmpordinal;override;
  30. procedure second_cmpsmallset;override;
  31. procedure second_cmp64bit;override;
  32. procedure second_cmpfloat;override;
  33. procedure second_addfloat;override;
  34. procedure second_cmp;
  35. end;
  36. implementation
  37. uses
  38. globtype,systems,
  39. cutils,verbose,globals,
  40. symconst,symdef,paramgr,
  41. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  42. cgbase,cgutils,cgcpu,
  43. cpuinfo,pass_1,pass_2,procinfo,
  44. cpupara,
  45. ncon,nset,nadd,
  46. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32,
  47. hlcgobj;
  48. {*****************************************************************************
  49. TCPUAddNode
  50. *****************************************************************************}
  51. procedure TCPUAddNode.second_cmpsmallset;
  52. var
  53. tmpreg : tregister;
  54. cond: TOpCmp;
  55. instr: taicpu;
  56. truelab, falselab: TAsmLabel;
  57. begin
  58. pass_left_right;
  59. current_asmdata.getjumplabel(truelab);
  60. current_asmdata.getjumplabel(falselab);
  61. location_reset_jump(location,truelab,falselab);
  62. force_reg_left_right(false,false);
  63. case nodetype of
  64. equaln: cond:=OC_EQ;
  65. unequaln: cond:=OC_NE;
  66. ltn: cond:=OC_LT;
  67. lten: cond:=OC_LT;
  68. gtn: cond:=OC_GT;
  69. gten: cond:=OC_GTE;
  70. else
  71. internalerror(2020030801);
  72. end;
  73. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cond,left.location.register,right.location.register,location.truelabel);
  74. current_asmdata.CurrAsmList.concat(taicpu.op_sym(A_J,location.falselabel));
  75. end;
  76. procedure TCPUAddNode.second_cmp;
  77. var
  78. cond: TOpCmp;
  79. instr: taicpu;
  80. truelab, falselab: TAsmLabel;
  81. begin
  82. pass_left_right;
  83. current_asmdata.getjumplabel(truelab);
  84. current_asmdata.getjumplabel(falselab);
  85. location_reset_jump(location,truelab,falselab);
  86. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  87. if is_signed(left.resultdef) then
  88. case nodetype of
  89. equaln: cond:=OC_EQ;
  90. unequaln: cond:=OC_NE;
  91. ltn: cond:=OC_LT;
  92. lten: cond:=OC_LTE;
  93. gtn: cond:=OC_GT;
  94. gten: cond:=OC_GTE;
  95. else
  96. internalerror(2020030801);
  97. end
  98. else
  99. case nodetype of
  100. equaln: cond:=OC_EQ;
  101. unequaln: cond:=OC_NE;
  102. ltn: cond:=OC_B;
  103. lten: cond:=OC_BE;
  104. gtn: cond:=OC_A;
  105. gten: cond:=OC_AE;
  106. else
  107. internalerror(2020030801);
  108. end;
  109. if (right.nodetype=ordconstn) and not(nf_swapped in flags) then
  110. cg.a_cmp_const_reg_label(current_asmdata.CurrAsmList,OS_INT,cond,right.location.value,left.location.register,location.truelabel)
  111. else
  112. begin
  113. force_reg_left_right(false,false);
  114. if nf_swapped in flags then
  115. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cond,left.location.register,right.location.register,location.truelabel)
  116. else
  117. cg.a_cmp_reg_reg_label(current_asmdata.CurrAsmList,OS_INT,cond,right.location.register,left.location.register,location.truelabel);
  118. end;
  119. current_asmdata.CurrAsmList.concat(taicpu.op_sym(A_J,location.falselabel));
  120. end;
  121. procedure TCPUAddNode.second_cmp64bit;
  122. begin
  123. second_cmp;
  124. end;
  125. function TCPUAddNode.pass_1 : tnode;
  126. begin
  127. result:=inherited pass_1;
  128. {$ifdef dummy}
  129. if not(assigned(result)) then
  130. begin
  131. unsigned:=not(is_signed(left.resultdef)) or
  132. not(is_signed(right.resultdef));
  133. if is_64bit(left.resultdef) and
  134. ((nodetype in [equaln,unequaln]) or
  135. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  136. ) then
  137. expectloc:=LOC_FLAGS;
  138. end;
  139. { handling boolean expressions }
  140. if not(assigned(result)) and
  141. (
  142. not(is_boolean(left.resultdef)) or
  143. not(is_boolean(right.resultdef)) or
  144. is_dynamic_array(left.resultdef)
  145. ) then
  146. expectloc:=LOC_FLAGS;
  147. {$endif dummy}
  148. end;
  149. procedure TCPUAddNode.second_cmpordinal;
  150. begin
  151. second_cmp;
  152. end;
  153. procedure TCPUAddNode.pass_left_and_right;
  154. begin
  155. { calculate the operator which is more difficult }
  156. firstcomplex(self);
  157. { in case of constant put it to the left }
  158. if (left.nodetype=ordconstn) then
  159. swapleftright;
  160. secondpass(left);
  161. secondpass(right);
  162. end;
  163. function TCPUAddNode.first_addfloat: tnode;
  164. begin
  165. result := nil;
  166. if (FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) and
  167. (tfloatdef(left.resultdef).floattype=s32real) and (nodetype<>slashn) then
  168. begin
  169. if nodetype in [equaln,unequaln,lten,ltn,gten,gtn] then
  170. expectloc:=LOC_FLAGS
  171. else
  172. expectloc:=LOC_FPUREGISTER;
  173. end
  174. else
  175. result:=first_addfloat_soft;
  176. end;
  177. procedure TCPUAddNode.second_addfloat;
  178. var
  179. op : TAsmOp;
  180. cmpop,
  181. singleprec , inv: boolean;
  182. begin
  183. pass_left_and_right;
  184. if (nf_swapped in flags) then
  185. swapleftright;
  186. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  187. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  188. cmpop:=false;
  189. inv:=false;
  190. case nodetype of
  191. addn :
  192. op:=A_ADD_S;
  193. muln :
  194. op:=A_MUL_S;
  195. subn :
  196. op:=A_SUB_S;
  197. unequaln,
  198. equaln:
  199. begin
  200. op:=A_OEQ_S;
  201. cmpop:=true;
  202. end;
  203. ltn:
  204. begin
  205. op:=A_OLT_S;
  206. cmpop:=true;
  207. end;
  208. lten:
  209. begin
  210. op:=A_OLE_S;
  211. cmpop:=true;
  212. end;
  213. gtn:
  214. begin
  215. op:=A_OLT_S;
  216. swapleftright;
  217. cmpop:=true;
  218. end;
  219. gten:
  220. begin
  221. op:=A_OLE_S;
  222. swapleftright;
  223. cmpop:=true;
  224. end;
  225. else
  226. internalerror(2020032601);
  227. end;
  228. { initialize de result }
  229. if cmpop then
  230. begin
  231. location_reset(location,LOC_FLAGS,OS_NO);
  232. location.resflags.register:=NR_B0;
  233. location.resflags.flag:=F_NZ;
  234. end
  235. else
  236. begin
  237. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  238. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  239. end;
  240. { emit the actual operation }
  241. if cmpop then
  242. begin
  243. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  244. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  245. if inv then
  246. location.resflags.flag:=F_Z;
  247. end
  248. else
  249. begin
  250. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register));
  251. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  252. end;
  253. end;
  254. procedure TCPUAddNode.second_cmpfloat;
  255. begin
  256. second_addfloat;
  257. end;
  258. begin
  259. caddnode:=tcpuaddnode;
  260. end.