ncpuadd.pas 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. cg.a_jmp_always(current_asmdata.CurrAsmList,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. cg.a_jmp_always(current_asmdata.CurrAsmList,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. if not(assigned(result)) and (nodetype in [equaln,unequaln,ltn,lten,gtn,gten]) and
  129. not((FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) and
  130. is_single(left.resultdef) and (nodetype<>slashn)) then
  131. expectloc:=LOC_JUMP;
  132. {$ifdef dummy}
  133. if not(assigned(result)) then
  134. begin
  135. unsigned:=not(is_signed(left.resultdef)) or
  136. not(is_signed(right.resultdef));
  137. if is_64bit(left.resultdef) and
  138. ((nodetype in [equaln,unequaln]) or
  139. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  140. ) then
  141. expectloc:=LOC_FLAGS;
  142. end;
  143. { handling boolean expressions }
  144. if not(assigned(result)) and
  145. (
  146. not(is_boolean(left.resultdef)) or
  147. not(is_boolean(right.resultdef)) or
  148. is_dynamic_array(left.resultdef)
  149. ) then
  150. expectloc:=LOC_FLAGS;
  151. {$endif dummy}
  152. end;
  153. procedure TCPUAddNode.second_cmpordinal;
  154. begin
  155. second_cmp;
  156. end;
  157. procedure TCPUAddNode.pass_left_and_right;
  158. begin
  159. { calculate the operator which is more difficult }
  160. firstcomplex(self);
  161. { in case of constant put it to the left }
  162. if (left.nodetype=ordconstn) then
  163. swapleftright;
  164. secondpass(left);
  165. secondpass(right);
  166. end;
  167. function TCPUAddNode.first_addfloat: tnode;
  168. begin
  169. result := nil;
  170. if (FPUXTENSA_SINGLE in fpu_capabilities[current_settings.fputype]) and
  171. (tfloatdef(left.resultdef).floattype=s32real) and (nodetype<>slashn) then
  172. begin
  173. if nodetype in [equaln,unequaln,lten,ltn,gten,gtn] then
  174. expectloc:=LOC_FLAGS
  175. else
  176. expectloc:=LOC_FPUREGISTER;
  177. end
  178. else
  179. result:=first_addfloat_soft;
  180. end;
  181. procedure TCPUAddNode.second_addfloat;
  182. var
  183. op : TAsmOp;
  184. cmpop,
  185. singleprec , inv: boolean;
  186. ai : taicpu;
  187. begin
  188. pass_left_and_right;
  189. if (nf_swapped in flags) then
  190. swapleftright;
  191. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  192. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  193. cmpop:=false;
  194. inv:=false;
  195. case nodetype of
  196. addn :
  197. op:=A_ADD;
  198. muln :
  199. op:=A_MUL;
  200. subn :
  201. op:=A_SUB;
  202. unequaln,
  203. equaln:
  204. begin
  205. op:=A_OEQ;
  206. cmpop:=true;
  207. end;
  208. ltn:
  209. begin
  210. op:=A_OLT;
  211. cmpop:=true;
  212. end;
  213. lten:
  214. begin
  215. op:=A_OLE;
  216. cmpop:=true;
  217. end;
  218. gtn:
  219. begin
  220. op:=A_OLT;
  221. swapleftright;
  222. cmpop:=true;
  223. end;
  224. gten:
  225. begin
  226. op:=A_OLE;
  227. swapleftright;
  228. cmpop:=true;
  229. end;
  230. else
  231. internalerror(2020032601);
  232. end;
  233. { initialize de result }
  234. if cmpop then
  235. begin
  236. location_reset(location,LOC_FLAGS,OS_NO);
  237. location.resflags.register:=NR_B0;
  238. location.resflags.flag:=F_NZ;
  239. end
  240. else
  241. begin
  242. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  243. location.register:=cg.getfpuregister(current_asmdata.CurrAsmList,location.size);
  244. end;
  245. { emit the actual operation }
  246. if cmpop then
  247. begin
  248. cg.getcpuregister(current_asmdata.CurrAsmList,location.resflags.register);
  249. ai:=taicpu.op_reg_reg_reg(op,location.resflags.register,left.location.register,right.location.register);
  250. ai.oppostfix:=PF_S;
  251. current_asmdata.CurrAsmList.concat(ai);
  252. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  253. if inv then
  254. location.resflags.flag:=F_Z;
  255. end
  256. else
  257. begin
  258. ai:=taicpu.op_reg_reg_reg(op,location.register,left.location.register,right.location.register);
  259. ai.oppostfix := PF_S;
  260. current_asmdata.CurrAsmList.concat(ai);
  261. cg.maybe_check_for_fpu_exception(current_asmdata.CurrAsmList);
  262. end;
  263. end;
  264. procedure TCPUAddNode.second_cmpfloat;
  265. begin
  266. second_addfloat;
  267. end;
  268. begin
  269. caddnode:=tcpuaddnode;
  270. end.