navradd.pas 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. {
  2. Copyright (c) 2008 by Florian Klaempfl
  3. Code generation for add nodes on the AVR
  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 navradd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncgadd,cpubase;
  22. type
  23. TAVRAddNode = class(tcgaddnode)
  24. private
  25. function GetResFlags(unsigned:Boolean):TResFlags;
  26. protected
  27. function pass_1 : tnode;override;
  28. procedure second_cmpordinal;override;
  29. procedure second_cmpsmallset;override;
  30. procedure second_cmp64bit;override;
  31. procedure second_cmp;
  32. end;
  33. implementation
  34. uses
  35. globtype,systems,
  36. cutils,verbose,globals,
  37. symconst,symdef,paramgr,
  38. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  39. cgbase,cgutils,cgcpu,
  40. cpuinfo,pass_1,pass_2,procinfo,
  41. cpupara,
  42. ncon,nset,nadd,
  43. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32,
  44. hlcgobj;
  45. {*****************************************************************************
  46. TAVRAddNode
  47. *****************************************************************************}
  48. function tavraddnode.GetResFlags(unsigned:Boolean):TResFlags;
  49. begin
  50. case NodeType of
  51. equaln:
  52. GetResFlags:=F_EQ;
  53. unequaln:
  54. GetResFlags:=F_NE;
  55. else
  56. if not(unsigned) then
  57. begin
  58. if nf_swapped in flags then
  59. case NodeType of
  60. ltn:
  61. GetResFlags:=F_NotPossible;
  62. lten:
  63. GetResFlags:=F_GE;
  64. gtn:
  65. GetResFlags:=F_LT;
  66. gten:
  67. GetResFlags:=F_NotPossible;
  68. else
  69. internalerror(2014082020);
  70. end
  71. else
  72. case NodeType of
  73. ltn:
  74. GetResFlags:=F_LT;
  75. lten:
  76. GetResFlags:=F_NotPossible;
  77. gtn:
  78. GetResFlags:=F_NotPossible;
  79. gten:
  80. GetResFlags:=F_GE;
  81. else
  82. internalerror(2014082021);
  83. end;
  84. end
  85. else
  86. begin
  87. if nf_swapped in Flags then
  88. case NodeType of
  89. ltn:
  90. GetResFlags:=F_NotPossible;
  91. lten:
  92. GetResFlags:=F_SH;
  93. gtn:
  94. GetResFlags:=F_LO;
  95. gten:
  96. GetResFlags:=F_NotPossible;
  97. else
  98. internalerror(2014082022);
  99. end
  100. else
  101. case NodeType of
  102. ltn:
  103. GetResFlags:=F_LO;
  104. lten:
  105. GetResFlags:=F_NotPossible;
  106. gtn:
  107. GetResFlags:=F_NotPossible;
  108. gten:
  109. GetResFlags:=F_SH;
  110. else
  111. internalerror(2014082023);
  112. end;
  113. end;
  114. end;
  115. end;
  116. procedure tavraddnode.second_cmpsmallset;
  117. procedure gencmp(tmpreg1,tmpreg2 : tregister);
  118. var
  119. i : byte;
  120. begin
  121. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,tmpreg1,tmpreg2));
  122. for i:=2 to tcgsize2size[left.location.size] do
  123. begin
  124. tmpreg1:=cg.GetNextReg(tmpreg1);
  125. tmpreg2:=cg.GetNextReg(tmpreg2);
  126. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,tmpreg2));
  127. end;
  128. end;
  129. var
  130. tmpreg : tregister;
  131. begin
  132. pass_left_right;
  133. location_reset(location,LOC_FLAGS,OS_NO);
  134. force_reg_left_right(false,false);
  135. case nodetype of
  136. equaln:
  137. begin
  138. gencmp(left.location.register,right.location.register);
  139. location.resflags:=F_EQ;
  140. end;
  141. unequaln:
  142. begin
  143. gencmp(left.location.register,right.location.register);
  144. location.resflags:=F_NE;
  145. end;
  146. lten,
  147. gten:
  148. begin
  149. if (not(nf_swapped in flags) and
  150. (nodetype = lten)) or
  151. ((nf_swapped in flags) and
  152. (nodetype = gten)) then
  153. swapleftright;
  154. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  155. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,location.size,
  156. left.location.register,right.location.register,tmpreg);
  157. gencmp(tmpreg,right.location.register);
  158. location.resflags:=F_EQ;
  159. end;
  160. else
  161. internalerror(2004012401);
  162. end;
  163. end;
  164. procedure tavraddnode.second_cmp;
  165. var
  166. unsigned : boolean;
  167. tmpreg1,tmpreg2 : tregister;
  168. i : longint;
  169. begin
  170. pass_left_right;
  171. force_reg_left_right(true,true);
  172. unsigned:=not(is_signed(left.resultdef)) or
  173. not(is_signed(right.resultdef));
  174. if getresflags(unsigned)=F_NotPossible then
  175. begin
  176. swapleftright;
  177. { if we have to swap back and left is a constant, force it to a register because we cannot generate
  178. the needed code using a constant }
  179. if (left.location.loc=LOC_CONSTANT) and (left.location.value<>0) then
  180. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  181. end;
  182. if right.location.loc=LOC_CONSTANT then
  183. begin
  184. { decrease register pressure on registers >= r16 }
  185. if (right.location.value and $ff)=0 then
  186. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,left.location.register,NR_R1))
  187. else
  188. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CPI,left.location.register,right.location.value and $ff))
  189. end
  190. { on the left side, we allow only a constant if it is 0 }
  191. else if (left.location.loc=LOC_CONSTANT) and (left.location.value=0) then
  192. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,NR_R1,right.location.register))
  193. else
  194. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,left.location.register,right.location.register));
  195. tmpreg1:=left.location.register;
  196. tmpreg2:=right.location.register;
  197. for i:=2 to tcgsize2size[left.location.size] do
  198. begin
  199. if i=5 then
  200. begin
  201. if left.location.loc<>LOC_CONSTANT then
  202. tmpreg1:=left.location.registerhi;
  203. if right.location.loc<>LOC_CONSTANT then
  204. tmpreg2:=right.location.registerhi;
  205. end
  206. else
  207. begin
  208. if left.location.loc<>LOC_CONSTANT then
  209. tmpreg1:=cg.GetNextReg(tmpreg1);
  210. if right.location.loc<>LOC_CONSTANT then
  211. tmpreg2:=cg.GetNextReg(tmpreg2);
  212. end;
  213. if right.location.loc=LOC_CONSTANT then
  214. begin
  215. { just use R1? }
  216. if ((right.location.value64 shr ((i-1)*8)) and $ff)=0 then
  217. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,NR_R1))
  218. else
  219. begin
  220. tmpreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_8);
  221. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_8,(right.location.value64 shr ((i-1)*8)) and $ff,tmpreg2);
  222. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,tmpreg2));
  223. end;
  224. end
  225. { above it is checked, if left=0, then a constant is allowed }
  226. else if (left.location.loc=LOC_CONSTANT) and (left.location.value=0) then
  227. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,NR_R1,tmpreg2))
  228. else
  229. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,tmpreg2));
  230. end;
  231. location_reset(location,LOC_FLAGS,OS_NO);
  232. location.resflags:=getresflags(unsigned);
  233. end;
  234. procedure tavraddnode.second_cmp64bit;
  235. begin
  236. second_cmp;
  237. end;
  238. function tavraddnode.pass_1 : tnode;
  239. begin
  240. result:=inherited pass_1;
  241. {$ifdef dummy}
  242. if not(assigned(result)) then
  243. begin
  244. unsigned:=not(is_signed(left.resultdef)) or
  245. not(is_signed(right.resultdef));
  246. if is_64bit(left.resultdef) and
  247. ((nodetype in [equaln,unequaln]) or
  248. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  249. ) then
  250. expectloc:=LOC_FLAGS;
  251. end;
  252. { handling boolean expressions }
  253. if not(assigned(result)) and
  254. (
  255. not(is_boolean(left.resultdef)) or
  256. not(is_boolean(right.resultdef)) or
  257. is_dynamic_array(left.resultdef)
  258. ) then
  259. expectloc:=LOC_FLAGS;
  260. {$endif dummy}
  261. end;
  262. procedure tavraddnode.second_cmpordinal;
  263. begin
  264. second_cmp;
  265. end;
  266. begin
  267. caddnode:=tavraddnode;
  268. end.