navradd.pas 11 KB

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