narmadd.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. {
  2. $Id$
  3. Copyright (c) 2000-2002 by Florian Klaempfl
  4. Code generation for add nodes on the ARM
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit narmadd;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,ncgadd,cpubase;
  23. type
  24. tarmaddnode = class(tcgaddnode)
  25. private
  26. function GetResFlags(unsigned:Boolean):TResFlags;
  27. protected
  28. procedure second_addfloat;override;
  29. procedure second_cmpfloat;override;
  30. procedure second_cmpordinal;override;
  31. procedure second_cmpsmallset;override;
  32. procedure second_cmp64bit;override;
  33. end;
  34. implementation
  35. uses
  36. globtype,systems,
  37. cutils,verbose,globals,
  38. symconst,symdef,paramgr,
  39. aasmbase,aasmtai,aasmcpu,defutil,htypechk,
  40. cgbase,cpuinfo,pass_1,pass_2,regvars,cgcpu,
  41. cpupara,
  42. ncon,nset,nadd,
  43. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32;
  44. {*****************************************************************************
  45. TSparcAddNode
  46. *****************************************************************************}
  47. function tarmaddnode.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_swaped in flags then
  58. case NodeType of
  59. ltn:
  60. GetResFlags:=F_GT;
  61. lten:
  62. GetResFlags:=F_GE;
  63. gtn:
  64. GetResFlags:=F_LT;
  65. gten:
  66. GetResFlags:=F_LE;
  67. end
  68. else
  69. case NodeType of
  70. ltn:
  71. GetResFlags:=F_LT;
  72. lten:
  73. GetResFlags:=F_LE;
  74. gtn:
  75. GetResFlags:=F_GT;
  76. gten:
  77. GetResFlags:=F_GE;
  78. end;
  79. end
  80. else
  81. begin
  82. if nf_swaped in Flags then
  83. case NodeType of
  84. ltn:
  85. GetResFlags:=F_HI;
  86. lten:
  87. GetResFlags:=F_CS;
  88. gtn:
  89. GetResFlags:=F_CC;
  90. gten:
  91. GetResFlags:=F_LS;
  92. end
  93. else
  94. case NodeType of
  95. ltn:
  96. GetResFlags:=F_CC;
  97. lten:
  98. GetResFlags:=F_LS;
  99. gtn:
  100. GetResFlags:=F_HI;
  101. gten:
  102. GetResFlags:=F_CS;
  103. end;
  104. end;
  105. end;
  106. end;
  107. procedure tarmaddnode.second_addfloat;
  108. var
  109. op : TAsmOp;
  110. instr : taicpu;
  111. begin
  112. case aktfputype of
  113. fpu_fpa,
  114. fpu_fpa10,
  115. fpu_fpa11:
  116. begin
  117. pass_left_right;
  118. if (nf_swaped in flags) then
  119. swapleftright;
  120. case nodetype of
  121. addn :
  122. op:=A_ADF;
  123. muln :
  124. op:=A_MUF;
  125. subn :
  126. op:=A_SUF;
  127. slashn :
  128. op:=A_DVF;
  129. else
  130. internalerror(200308313);
  131. end;
  132. { force fpureg as location, left right doesn't matter
  133. as both will be in a fpureg }
  134. location_force_fpureg(exprasmlist,left.location,true);
  135. location_force_fpureg(exprasmlist,right.location,(left.location.loc<>LOC_CFPUREGISTER));
  136. location_reset(location,LOC_FPUREGISTER,def_cgsize(resulttype.def));
  137. if left.location.loc<>LOC_CFPUREGISTER then
  138. location.register:=left.location.register
  139. else
  140. location.register:=right.location.register;
  141. instr:=taicpu.op_reg_reg_reg(op,
  142. location.register,left.location.register,right.location.register);
  143. instr.oppostfix:=cgsize2fpuoppostfix[def_cgsize(resulttype.def)];
  144. exprasmlist.concat(instr);
  145. release_reg_left_right;
  146. location.loc:=LOC_FPUREGISTER;
  147. end;
  148. fpu_soft:
  149. { this case should be handled already by pass1 }
  150. internalerror(200308252);
  151. else
  152. internalerror(200308251);
  153. end;
  154. end;
  155. procedure tarmaddnode.second_cmpfloat;
  156. begin
  157. { we will see what instruction set we'll use on the arm for FP
  158. pass_left_right;
  159. if (nf_swaped in flags) then
  160. swapleftright;
  161. { force fpureg as location, left right doesn't matter
  162. as both will be in a fpureg }
  163. location_force_fpureg(exprasmlist,left.location,true);
  164. location_force_fpureg(exprasmlist,right.location,true);
  165. location_reset(location,LOC_FLAGS,OS_NO);
  166. location.resflags:=getresflags(true);
  167. exprasmlist.concat(taicpu.op_reg_reg(A_FCMPs,
  168. left.location.register,right.location.register));
  169. { Delay slot (can only contain integer operation) }
  170. exprasmlist.concat(taicpu.op_none(A_NOP));
  171. release_reg_left_right;
  172. }
  173. //!!!!
  174. location_reset(location,LOC_FLAGS,OS_NO);
  175. location.resflags:=getresflags(true);
  176. end;
  177. procedure tarmaddnode.second_cmpsmallset;
  178. var
  179. tmpreg : tregister;
  180. begin
  181. pass_left_right;
  182. location_reset(location,LOC_FLAGS,OS_NO);
  183. force_reg_left_right(false,false);
  184. case nodetype of
  185. equaln:
  186. begin
  187. exprasmlist.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  188. location.resflags:=F_EQ;
  189. end;
  190. unequaln:
  191. begin
  192. exprasmlist.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  193. location.resflags:=F_NE;
  194. end;
  195. lten,
  196. gten:
  197. begin
  198. if (not(nf_swaped in flags) and
  199. (nodetype = lten)) or
  200. ((nf_swaped in flags) and
  201. (nodetype = gten)) then
  202. swapleftright;
  203. tmpreg:=cg.getintregister(exprasmlist,location.size);
  204. exprasmlist.concat(taicpu.op_reg_reg_reg(A_AND,tmpreg,left.location.register,right.location.register));
  205. exprasmlist.concat(taicpu.op_reg_reg(A_CMP,tmpreg,right.location.register));
  206. cg.ungetregister(exprasmlist,tmpreg);
  207. location.resflags:=F_EQ;
  208. end;
  209. else
  210. internalerror(2004012401);
  211. end;
  212. release_reg_left_right;
  213. end;
  214. procedure tarmaddnode.second_cmp64bit;
  215. var
  216. unsigned : boolean;
  217. tmpreg : tregister;
  218. begin
  219. pass_left_right;
  220. force_reg_left_right(false,false);
  221. unsigned:=not(is_signed(left.resulttype.def)) or
  222. not(is_signed(right.resulttype.def));
  223. location_reset(location,LOC_FLAGS,OS_NO);
  224. location.resflags:=getresflags(unsigned);
  225. { operation requiring proper N, Z and C flags ? }
  226. if unsigned or (nodetype in [equaln,unequaln]) then
  227. begin
  228. exprasmlist.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo));
  229. exprasmlist.concat(setcondition(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi),C_EQ));
  230. end
  231. { operation requiring proper N, V and C flags ? }
  232. else if nodetype in [gten,ltn] then
  233. begin
  234. tmpreg:=cg.getintregister(exprasmlist,location.size);
  235. exprasmlist.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,tmpreg,left.location.register64.reglo,right.location.register64.reglo),PF_S));
  236. exprasmlist.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,tmpreg,left.location.register64.reghi,right.location.register64.reghi),PF_S));
  237. cg.ungetregister(exprasmlist,tmpreg);
  238. end
  239. else
  240. { operation requiring proper N, Z and V flags ? }
  241. begin
  242. { this isn't possible so swap operands and use the "reverse" operation }
  243. tmpreg:=cg.getintregister(exprasmlist,location.size);
  244. exprasmlist.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SUB,tmpreg,right.location.register64.reglo,left.location.register64.reglo),PF_S));
  245. exprasmlist.concat(setoppostfix(taicpu.op_reg_reg_reg(A_SBC,tmpreg,right.location.register64.reghi,left.location.register64.reghi),PF_S));
  246. cg.ungetregister(exprasmlist,tmpreg);
  247. if nf_swaped in flags then
  248. begin
  249. if location.resflags=F_LT then
  250. location.resflags:=F_GT
  251. else if location.resflags=F_GE then
  252. location.resflags:=F_LE
  253. else
  254. internalerror(200401221);
  255. end
  256. else
  257. begin
  258. if location.resflags=F_GT then
  259. location.resflags:=F_LT
  260. else if location.resflags=F_LE then
  261. location.resflags:=F_GE
  262. else
  263. internalerror(200401221);
  264. end;
  265. end;
  266. release_reg_left_right;
  267. end;
  268. procedure tarmaddnode.second_cmpordinal;
  269. var
  270. unsigned : boolean;
  271. tmpreg : tregister;
  272. b : byte;
  273. begin
  274. pass_left_right;
  275. force_reg_left_right(true,true);
  276. unsigned:=not(is_signed(left.resulttype.def)) or
  277. not(is_signed(right.resulttype.def));
  278. if right.location.loc = LOC_CONSTANT then
  279. begin
  280. if is_shifter_const(right.location.value,b) then
  281. exprasmlist.concat(taicpu.op_reg_const(A_CMP,left.location.register,right.location.value))
  282. else
  283. begin
  284. tmpreg:=cg.getintregister(exprasmlist,location.size);
  285. cg.a_load_const_reg(exprasmlist,OS_INT,
  286. aword(right.location.value),tmpreg);
  287. exprasmlist.concat(taicpu.op_reg_reg(A_CMP,left.location.register,tmpreg));
  288. cg.ungetregister(exprasmlist,tmpreg);
  289. end;
  290. end
  291. else
  292. exprasmlist.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  293. location_reset(location,LOC_FLAGS,OS_NO);
  294. location.resflags:=getresflags(unsigned);
  295. release_reg_left_right;
  296. end;
  297. begin
  298. caddnode:=tarmaddnode;
  299. end.
  300. {
  301. $Log$
  302. Revision 1.11 2004-01-26 19:05:56 florian
  303. * fixed several arm issues
  304. Revision 1.10 2004/01/24 20:19:46 florian
  305. * fixed some spilling stuff
  306. + not(<int64>) implemented
  307. + small set comparisations implemented
  308. Revision 1.9 2004/01/24 18:12:40 florian
  309. * fixed several arm floating point issues
  310. Revision 1.8 2004/01/23 00:01:48 florian
  311. * another fix to flag handling
  312. Revision 1.7 2004/01/22 20:13:18 florian
  313. * fixed several issues with flags
  314. Revision 1.6 2004/01/22 01:47:15 florian
  315. * improved register usage
  316. + implemented second_cmp64bit
  317. Revision 1.5 2003/11/02 14:30:03 florian
  318. * fixed ARM for new reg. allocation scheme
  319. Revision 1.4 2003/09/01 15:11:16 florian
  320. * fixed reference handling
  321. * fixed operand postfix for floating point instructions
  322. * fixed wrong shifter constant handling
  323. Revision 1.3 2003/09/01 09:54:57 florian
  324. * results of work on arm port last weekend
  325. Revision 1.2 2003/08/25 23:20:38 florian
  326. + started to implement FPU support for the ARM
  327. * fixed a lot of other things
  328. Revision 1.1 2003/08/21 03:14:00 florian
  329. * arm compiler can be compiled; far from being working
  330. }