navradd.pas 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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,regvars,procinfo,
  41. cpupara,
  42. ncon,nset,nadd,
  43. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32;
  44. {*****************************************************************************
  45. TAVRAddNode
  46. *****************************************************************************}
  47. function tavraddnode.GetResFlags(unsigned:Boolean):TResFlags;
  48. begin
  49. case NodeType of
  50. equaln:
  51. GetResFlags:=F_EQ;
  52. unequaln:
  53. GetResFlags:=F_NE;
  54. else
  55. if not(unsigned) then
  56. begin
  57. if nf_swapped in flags then
  58. case NodeType of
  59. ltn:
  60. GetResFlags:=F_NotPossible;
  61. lten:
  62. GetResFlags:=F_GE;
  63. gtn:
  64. GetResFlags:=F_LT;
  65. gten:
  66. GetResFlags:=F_NotPossible;
  67. else
  68. internalerror(2014082020);
  69. end
  70. else
  71. case NodeType of
  72. ltn:
  73. GetResFlags:=F_LT;
  74. lten:
  75. GetResFlags:=F_NotPossible;
  76. gtn:
  77. GetResFlags:=F_NotPossible;
  78. gten:
  79. GetResFlags:=F_GE;
  80. else
  81. internalerror(2014082021);
  82. end;
  83. end
  84. else
  85. begin
  86. if nf_swapped in Flags then
  87. case NodeType of
  88. ltn:
  89. GetResFlags:=F_NotPossible;
  90. lten:
  91. GetResFlags:=F_CS;
  92. gtn:
  93. GetResFlags:=F_CC;
  94. gten:
  95. GetResFlags:=F_NotPossible;
  96. else
  97. internalerror(2014082022);
  98. end
  99. else
  100. case NodeType of
  101. ltn:
  102. GetResFlags:=F_CC;
  103. lten:
  104. GetResFlags:=F_NotPossible;
  105. gtn:
  106. GetResFlags:=F_NotPossible;
  107. gten:
  108. GetResFlags:=F_CS;
  109. else
  110. internalerror(2014082023);
  111. end;
  112. end;
  113. end;
  114. end;
  115. procedure tavraddnode.second_cmpsmallset;
  116. procedure gencmp(tmpreg1,tmpreg2 : tregister);
  117. var
  118. i : byte;
  119. begin
  120. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,tmpreg1,tmpreg2));
  121. for i:=2 to tcgsize2size[left.location.size] do
  122. begin
  123. tmpreg1:=GetNextReg(tmpreg1);
  124. tmpreg2:=GetNextReg(tmpreg2);
  125. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,tmpreg2));
  126. end;
  127. end;
  128. var
  129. tmpreg : tregister;
  130. begin
  131. pass_left_right;
  132. location_reset(location,LOC_FLAGS,OS_NO);
  133. force_reg_left_right(false,false);
  134. case nodetype of
  135. equaln:
  136. begin
  137. gencmp(left.location.register,right.location.register);
  138. location.resflags:=F_EQ;
  139. end;
  140. unequaln:
  141. begin
  142. gencmp(left.location.register,right.location.register);
  143. location.resflags:=F_NE;
  144. end;
  145. lten,
  146. gten:
  147. begin
  148. if (not(nf_swapped in flags) and
  149. (nodetype = lten)) or
  150. ((nf_swapped in flags) and
  151. (nodetype = gten)) then
  152. swapleftright;
  153. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  154. cg.a_op_reg_reg_reg(current_asmdata.CurrAsmList,OP_AND,location.size,
  155. left.location.register,right.location.register,tmpreg);
  156. gencmp(tmpreg,right.location.register);
  157. location.resflags:=F_EQ;
  158. end;
  159. else
  160. internalerror(2004012401);
  161. end;
  162. end;
  163. procedure tavraddnode.second_cmp;
  164. var
  165. unsigned : boolean;
  166. tmpreg1,tmpreg2 : tregister;
  167. i : longint;
  168. begin
  169. pass_left_right;
  170. force_reg_left_right(true,false);
  171. unsigned:=not(is_signed(left.resultdef)) or
  172. not(is_signed(right.resultdef));
  173. if getresflags(unsigned)=F_NotPossible then
  174. swapleftright;
  175. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CP,left.location.register,right.location.register));
  176. tmpreg1:=left.location.register;
  177. tmpreg2:=right.location.register;
  178. for i:=2 to tcgsize2size[left.location.size] do
  179. begin
  180. if i=5 then
  181. begin
  182. tmpreg1:=left.location.registerhi;
  183. tmpreg2:=right.location.registerhi;
  184. end
  185. else
  186. begin
  187. tmpreg1:=GetNextReg(tmpreg1);
  188. tmpreg2:=GetNextReg(tmpreg2);
  189. end;
  190. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CPC,tmpreg1,tmpreg2));
  191. end;
  192. location_reset(location,LOC_FLAGS,OS_NO);
  193. location.resflags:=getresflags(unsigned);
  194. end;
  195. procedure tavraddnode.second_cmp64bit;
  196. begin
  197. second_cmp;
  198. end;
  199. function tavraddnode.pass_1 : tnode;
  200. begin
  201. result:=inherited pass_1;
  202. {$ifdef dummy}
  203. if not(assigned(result)) then
  204. begin
  205. unsigned:=not(is_signed(left.resultdef)) or
  206. not(is_signed(right.resultdef));
  207. if is_64bit(left.resultdef) and
  208. ((nodetype in [equaln,unequaln]) or
  209. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  210. ) then
  211. expectloc:=LOC_FLAGS;
  212. end;
  213. { handling boolean expressions }
  214. if not(assigned(result)) and
  215. (
  216. not(is_boolean(left.resultdef)) or
  217. not(is_boolean(right.resultdef)) or
  218. is_dynamic_array(left.resultdef)
  219. ) then
  220. expectloc:=LOC_FLAGS;
  221. {$endif dummy}
  222. end;
  223. procedure tavraddnode.second_cmpordinal;
  224. begin
  225. second_cmp;
  226. end;
  227. begin
  228. caddnode:=tavraddnode;
  229. end.