2
0

navradd.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  123. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,tmpreg1,tmpreg2));
  124. for i:=2 to tcgsize2size[left.location.size] do
  125. begin
  126. tmpreg1:=cg.GetNextReg(tmpreg1);
  127. tmpreg2:=cg.GetNextReg(tmpreg2);
  128. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,tmpreg2));
  129. end;
  130. end;
  131. var
  132. tmpreg : tregister;
  133. begin
  134. pass_left_right;
  135. location_reset(location,LOC_FLAGS,OS_NO);
  136. force_reg_left_right(false,false);
  137. case nodetype of
  138. equaln:
  139. begin
  140. gencmp(left.location.register,right.location.register);
  141. location.resflags:=F_EQ;
  142. end;
  143. unequaln:
  144. begin
  145. gencmp(left.location.register,right.location.register);
  146. location.resflags:=F_NE;
  147. end;
  148. lten,
  149. gten:
  150. begin
  151. if (not(nf_swapped in flags) and
  152. (nodetype = lten)) or
  153. ((nf_swapped in flags) and
  154. (nodetype = gten)) then
  155. swapleftright;
  156. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  157. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,location.size,
  158. left.location.register,right.location.register,tmpreg);
  159. gencmp(tmpreg,right.location.register);
  160. location.resflags:=F_EQ;
  161. end;
  162. else
  163. internalerror(2004012401);
  164. end;
  165. end;
  166. procedure tavraddnode.second_cmp;
  167. var
  168. unsigned : boolean;
  169. tmpreg1,tmpreg2 : tregister;
  170. i : longint;
  171. begin
  172. pass_left_right;
  173. force_reg_left_right(true,true);
  174. unsigned:=not(is_signed(left.resultdef)) or
  175. not(is_signed(right.resultdef));
  176. if getresflags(unsigned)=F_NotPossible then
  177. begin
  178. swapleftright;
  179. { if we have to swap back and left is a constant, force it to a register because we cannot generate
  180. the needed code using a constant }
  181. if (left.location.loc=LOC_CONSTANT) and (left.location.value<>0) then
  182. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,false);
  183. end;
  184. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  185. if (not unsigned) and
  186. (right.location.loc=LOC_CONSTANT) and
  187. (right.location.value=0) and
  188. (getresflags(unsigned) in [F_LT,F_GE]) then
  189. begin
  190. { This is a simple sign test, where we can just test the msb }
  191. tmpreg1:=left.location.register;
  192. for i:=2 to tcgsize2size[left.location.size] do
  193. begin
  194. if i=5 then
  195. tmpreg1:=left.location.registerhi
  196. else
  197. tmpreg1:=cg.GetNextReg(tmpreg1);
  198. end;
  199. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,tmpreg1,GetDefaultZeroReg));
  200. location_reset(location,LOC_FLAGS,OS_NO);
  201. location.resflags:=getresflags(unsigned);
  202. exit;
  203. end;
  204. if right.location.loc=LOC_CONSTANT then
  205. begin
  206. { decrease register pressure on registers >= r16 }
  207. if (right.location.value and $ff)=0 then
  208. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,left.location.register,GetDefaultZeroReg))
  209. else
  210. begin
  211. cg.getcpuregister(current_asmdata.CurrAsmList,NR_R26);
  212. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_LDI,NR_R26,right.location.value and $ff));
  213. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,left.location.register,NR_R26));
  214. cg.ungetcpuregister(current_asmdata.CurrAsmList,NR_R26);
  215. end;
  216. end
  217. { on the left side, we allow only a constant if it is 0 }
  218. else if (left.location.loc=LOC_CONSTANT) and (left.location.value=0) then
  219. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,GetDefaultZeroReg,right.location.register))
  220. else
  221. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,left.location.register,right.location.register));
  222. tmpreg1:=left.location.register;
  223. tmpreg2:=right.location.register;
  224. for i:=2 to tcgsize2size[left.location.size] do
  225. begin
  226. if i=5 then
  227. begin
  228. if left.location.loc<>LOC_CONSTANT then
  229. tmpreg1:=left.location.registerhi;
  230. if right.location.loc<>LOC_CONSTANT then
  231. tmpreg2:=right.location.registerhi;
  232. end
  233. else
  234. begin
  235. if left.location.loc<>LOC_CONSTANT then
  236. tmpreg1:=cg.GetNextReg(tmpreg1);
  237. if right.location.loc<>LOC_CONSTANT then
  238. tmpreg2:=cg.GetNextReg(tmpreg2);
  239. end;
  240. if right.location.loc=LOC_CONSTANT then
  241. begin
  242. { just use R1? }
  243. if ((right.location.value64 shr ((i-1)*8)) and $ff)=0 then
  244. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,GetDefaultZeroReg))
  245. else
  246. begin
  247. tmpreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_8);
  248. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_8,(right.location.value64 shr ((i-1)*8)) and $ff,tmpreg2);
  249. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,tmpreg2));
  250. end;
  251. end
  252. { above it is checked, if left=0, then a constant is allowed }
  253. else if (left.location.loc=LOC_CONSTANT) and (left.location.value=0) then
  254. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,GetDefaultZeroReg,tmpreg2))
  255. else
  256. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,tmpreg2));
  257. end;
  258. location_reset(location,LOC_FLAGS,OS_NO);
  259. location.resflags:=getresflags(unsigned);
  260. end;
  261. procedure tavraddnode.second_cmp64bit;
  262. begin
  263. second_cmp;
  264. end;
  265. function tavraddnode.pass_1 : tnode;
  266. begin
  267. result:=inherited pass_1;
  268. {$ifdef dummy}
  269. if not(assigned(result)) then
  270. begin
  271. unsigned:=not(is_signed(left.resultdef)) or
  272. not(is_signed(right.resultdef));
  273. if is_64bit(left.resultdef) and
  274. ((nodetype in [equaln,unequaln]) or
  275. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  276. ) then
  277. expectloc:=LOC_FLAGS;
  278. end;
  279. { handling boolean expressions }
  280. if not(assigned(result)) and
  281. (
  282. not(is_boolean(left.resultdef)) or
  283. not(is_boolean(right.resultdef)) or
  284. is_dynamic_array(left.resultdef)
  285. ) then
  286. expectloc:=LOC_FLAGS;
  287. {$endif dummy}
  288. end;
  289. procedure tavraddnode.second_cmpordinal;
  290. begin
  291. second_cmp;
  292. end;
  293. begin
  294. caddnode:=tavraddnode;
  295. end.