navradd.pas 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. end;
  32. implementation
  33. uses
  34. globtype,systems,
  35. cutils,verbose,globals,
  36. symconst,symdef,paramgr,
  37. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  38. cgbase,cgutils,cgcpu,
  39. cpuinfo,pass_1,pass_2,regvars,procinfo,
  40. cpupara,
  41. ncon,nset,nadd,
  42. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32;
  43. {*****************************************************************************
  44. TSparcAddNode
  45. *****************************************************************************}
  46. function tavraddnode.GetResFlags(unsigned:Boolean):TResFlags;
  47. begin
  48. case NodeType of
  49. equaln:
  50. GetResFlags:=F_EQ;
  51. unequaln:
  52. GetResFlags:=F_NE;
  53. else
  54. if not(unsigned) then
  55. begin
  56. if nf_swapped in flags then
  57. case NodeType of
  58. ltn:
  59. GetResFlags:=F_GT;
  60. lten:
  61. GetResFlags:=F_GE;
  62. gtn:
  63. GetResFlags:=F_LT;
  64. gten:
  65. GetResFlags:=F_LE;
  66. end
  67. else
  68. case NodeType of
  69. ltn:
  70. GetResFlags:=F_LT;
  71. lten:
  72. GetResFlags:=F_LE;
  73. gtn:
  74. GetResFlags:=F_GT;
  75. gten:
  76. GetResFlags:=F_GE;
  77. end;
  78. end
  79. else
  80. begin
  81. if nf_swapped in Flags then
  82. case NodeType of
  83. ltn:
  84. GetResFlags:=F_HI;
  85. lten:
  86. GetResFlags:=F_CS;
  87. gtn:
  88. GetResFlags:=F_CC;
  89. gten:
  90. GetResFlags:=F_LS;
  91. end
  92. else
  93. case NodeType of
  94. ltn:
  95. GetResFlags:=F_CC;
  96. lten:
  97. GetResFlags:=F_LS;
  98. gtn:
  99. GetResFlags:=F_HI;
  100. gten:
  101. GetResFlags:=F_CS;
  102. end;
  103. end;
  104. end;
  105. end;
  106. procedure tavraddnode.second_cmpsmallset;
  107. var
  108. tmpreg : tregister;
  109. begin
  110. {
  111. pass_left_right;
  112. location_reset(location,LOC_FLAGS,OS_NO);
  113. force_reg_left_right(false,false);
  114. case nodetype of
  115. equaln:
  116. begin
  117. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  118. location.resflags:=F_EQ;
  119. end;
  120. unequaln:
  121. begin
  122. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  123. location.resflags:=F_NE;
  124. end;
  125. lten,
  126. gten:
  127. begin
  128. if (not(nf_swapped in flags) and
  129. (nodetype = lten)) or
  130. ((nf_swapped in flags) and
  131. (nodetype = gten)) then
  132. swapleftright;
  133. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  134. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_AND,tmpreg,left.location.register,right.location.register));
  135. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,tmpreg,right.location.register));
  136. location.resflags:=F_EQ;
  137. end;
  138. else
  139. internalerror(2004012401);
  140. end;
  141. }
  142. end;
  143. procedure tavraddnode.second_cmp64bit;
  144. var
  145. unsigned : boolean;
  146. oldnodetype : tnodetype;
  147. begin
  148. {
  149. pass_left_right;
  150. force_reg_left_right(false,false);
  151. unsigned:=not(is_signed(left.resultdef)) or
  152. not(is_signed(right.resultdef));
  153. { operation requiring proper N, Z and C flags ? }
  154. if unsigned or (nodetype in [equaln,unequaln]) then
  155. begin
  156. location_reset(location,LOC_FLAGS,OS_NO);
  157. location.resflags:=getresflags(unsigned);
  158. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi));
  159. current_asmdata.CurrAsmList.concat(setcondition(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo),C_EQ));
  160. end
  161. else
  162. { operation requiring proper N, Z and V flags ? }
  163. begin
  164. location_reset(location,LOC_JUMP,OS_NO);
  165. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi));
  166. { the jump the sequence is a little bit hairy }
  167. case nodetype of
  168. ltn,gtn:
  169. begin
  170. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(false),current_procinfo.CurrTrueLabel);
  171. { cheat a little bit for the negative test }
  172. toggleflag(nf_swapped);
  173. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(false),current_procinfo.CurrFalseLabel);
  174. toggleflag(nf_swapped);
  175. end;
  176. lten,gten:
  177. begin
  178. oldnodetype:=nodetype;
  179. if nodetype=lten then
  180. nodetype:=ltn
  181. else
  182. nodetype:=gtn;
  183. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  184. { cheat for the negative test }
  185. if nodetype=ltn then
  186. nodetype:=gtn
  187. else
  188. nodetype:=ltn;
  189. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  190. nodetype:=oldnodetype;
  191. end;
  192. end;
  193. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo));
  194. { the comparisaion of the low dword have to be
  195. always unsigned! }
  196. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  197. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  198. end;
  199. }
  200. end;
  201. function tavraddnode.pass_1 : tnode;
  202. begin
  203. result:=inherited pass_1;
  204. { handling boolean expressions }
  205. if not(assigned(result)) and
  206. (
  207. not(is_boolean(left.resultdef)) or
  208. not(is_boolean(right.resultdef)) or
  209. is_dynamic_array(left.resultdef)
  210. ) then
  211. expectloc:=LOC_FLAGS;
  212. end;
  213. procedure tavraddnode.second_cmpordinal;
  214. var
  215. unsigned : boolean;
  216. tmpreg : tregister;
  217. b : byte;
  218. begin
  219. {
  220. pass_left_right;
  221. force_reg_left_right(true,true);
  222. unsigned:=not(is_signed(left.resultdef)) or
  223. not(is_signed(right.resultdef));
  224. if right.location.loc = LOC_CONSTANT then
  225. begin
  226. if is_shifter_const(right.location.value,b) then
  227. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,right.location.value))
  228. else
  229. begin
  230. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  231. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  232. right.location.value,tmpreg);
  233. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,tmpreg));
  234. end;
  235. end
  236. else
  237. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  238. location_reset(location,LOC_FLAGS,OS_NO);
  239. location.resflags:=getresflags(unsigned);
  240. }
  241. end;
  242. begin
  243. caddnode:=tavraddnode;
  244. end.