navradd.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 (not unsigned) and
  183. (right.location.loc=LOC_CONSTANT) and
  184. (right.location.value=0) and
  185. (getresflags(unsigned) in [F_LT,F_GE]) then
  186. begin
  187. { This is a simple sign test, where we can just test the msb }
  188. tmpreg1:=left.location.register;
  189. for i:=2 to tcgsize2size[left.location.size] do
  190. begin
  191. if i=5 then
  192. tmpreg1:=left.location.registerhi
  193. else
  194. tmpreg1:=cg.GetNextReg(tmpreg1);
  195. end;
  196. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,tmpreg1,GetDefaultZeroReg));
  197. location_reset(location,LOC_FLAGS,OS_NO);
  198. location.resflags:=getresflags(unsigned);
  199. exit;
  200. end;
  201. if right.location.loc=LOC_CONSTANT then
  202. begin
  203. { decrease register pressure on registers >= r16 }
  204. if (right.location.value and $ff)=0 then
  205. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,left.location.register,GetDefaultZeroReg))
  206. else
  207. begin
  208. cg.getcpuregister(current_asmdata.CurrAsmList,NR_R26);
  209. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_LDI,NR_R26,right.location.value and $ff));
  210. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,left.location.register,NR_R26));
  211. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_R26);
  212. end;
  213. end
  214. { on the left side, we allow only a constant if it is 0 }
  215. else if (left.location.loc=LOC_CONSTANT) and (left.location.value=0) then
  216. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,GetDefaultZeroReg,right.location.register))
  217. else
  218. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,left.location.register,right.location.register));
  219. tmpreg1:=left.location.register;
  220. tmpreg2:=right.location.register;
  221. for i:=2 to tcgsize2size[left.location.size] do
  222. begin
  223. if i=5 then
  224. begin
  225. if left.location.loc<>LOC_CONSTANT then
  226. tmpreg1:=left.location.registerhi;
  227. if right.location.loc<>LOC_CONSTANT then
  228. tmpreg2:=right.location.registerhi;
  229. end
  230. else
  231. begin
  232. if left.location.loc<>LOC_CONSTANT then
  233. tmpreg1:=cg.GetNextReg(tmpreg1);
  234. if right.location.loc<>LOC_CONSTANT then
  235. tmpreg2:=cg.GetNextReg(tmpreg2);
  236. end;
  237. if right.location.loc=LOC_CONSTANT then
  238. begin
  239. { just use R1? }
  240. if ((right.location.value64 shr ((i-1)*8)) and $ff)=0 then
  241. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,GetDefaultZeroReg))
  242. else
  243. begin
  244. tmpreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_8);
  245. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_8,(right.location.value64 shr ((i-1)*8)) and $ff,tmpreg2);
  246. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,tmpreg2));
  247. end;
  248. end
  249. { above it is checked, if left=0, then a constant is allowed }
  250. else if (left.location.loc=LOC_CONSTANT) and (left.location.value=0) then
  251. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,GetDefaultZeroReg,tmpreg2))
  252. else
  253. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,tmpreg2));
  254. end;
  255. location_reset(location,LOC_FLAGS,OS_NO);
  256. location.resflags:=getresflags(unsigned);
  257. end;
  258. procedure tavraddnode.second_cmp64bit;
  259. begin
  260. second_cmp;
  261. end;
  262. function tavraddnode.pass_1 : tnode;
  263. begin
  264. result:=inherited pass_1;
  265. {$ifdef dummy}
  266. if not(assigned(result)) then
  267. begin
  268. unsigned:=not(is_signed(left.resultdef)) or
  269. not(is_signed(right.resultdef));
  270. if is_64bit(left.resultdef) and
  271. ((nodetype in [equaln,unequaln]) or
  272. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  273. ) then
  274. expectloc:=LOC_FLAGS;
  275. end;
  276. { handling boolean expressions }
  277. if not(assigned(result)) and
  278. (
  279. not(is_boolean(left.resultdef)) or
  280. not(is_boolean(right.resultdef)) or
  281. is_dynamic_array(left.resultdef)
  282. ) then
  283. expectloc:=LOC_FLAGS;
  284. {$endif dummy}
  285. end;
  286. procedure tavraddnode.second_cmpordinal;
  287. begin
  288. second_cmp;
  289. end;
  290. begin
  291. caddnode:=tavraddnode;
  292. end.