narmadd.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Code generation for add nodes on the ARM
  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 narmadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncgadd,cpubase;
  22. type
  23. tarmaddnode = class(tcgaddnode)
  24. private
  25. function GetResFlags(unsigned:Boolean):TResFlags;
  26. protected
  27. function pass_1 : tnode;override;
  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,aasmdata,aasmcpu,defutil,htypechk,
  40. cgbase,cgutils,cgcpu,
  41. cpuinfo,pass_1,pass_2,regvars,procinfo,
  42. cpupara,
  43. ncon,nset,nadd,
  44. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32;
  45. {*****************************************************************************
  46. TSparcAddNode
  47. *****************************************************************************}
  48. function tarmaddnode.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_GT;
  62. lten:
  63. GetResFlags:=F_GE;
  64. gtn:
  65. GetResFlags:=F_LT;
  66. gten:
  67. GetResFlags:=F_LE;
  68. end
  69. else
  70. case NodeType of
  71. ltn:
  72. GetResFlags:=F_LT;
  73. lten:
  74. GetResFlags:=F_LE;
  75. gtn:
  76. GetResFlags:=F_GT;
  77. gten:
  78. GetResFlags:=F_GE;
  79. end;
  80. end
  81. else
  82. begin
  83. if nf_swapped in Flags then
  84. case NodeType of
  85. ltn:
  86. GetResFlags:=F_HI;
  87. lten:
  88. GetResFlags:=F_CS;
  89. gtn:
  90. GetResFlags:=F_CC;
  91. gten:
  92. GetResFlags:=F_LS;
  93. end
  94. else
  95. case NodeType of
  96. ltn:
  97. GetResFlags:=F_CC;
  98. lten:
  99. GetResFlags:=F_LS;
  100. gtn:
  101. GetResFlags:=F_HI;
  102. gten:
  103. GetResFlags:=F_CS;
  104. end;
  105. end;
  106. end;
  107. end;
  108. procedure tarmaddnode.second_addfloat;
  109. var
  110. op : TAsmOp;
  111. begin
  112. case current_settings.fputype of
  113. fpu_fpa,
  114. fpu_fpa10,
  115. fpu_fpa11:
  116. begin
  117. pass_left_right;
  118. if (nf_swapped 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(current_asmdata.CurrAsmList,left.location,true);
  135. location_force_fpureg(current_asmdata.CurrAsmList,right.location,(left.location.loc<>LOC_CFPUREGISTER));
  136. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  137. if left.location.loc<>LOC_CFPUREGISTER then
  138. location.register:=left.location.register
  139. else
  140. location.register:=right.location.register;
  141. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(op,
  142. location.register,left.location.register,right.location.register),
  143. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  144. location.loc:=LOC_FPUREGISTER;
  145. end;
  146. fpu_soft:
  147. { this case should be handled already by pass1 }
  148. internalerror(200308252);
  149. else
  150. internalerror(200308251);
  151. end;
  152. end;
  153. procedure tarmaddnode.second_cmpfloat;
  154. begin
  155. pass_left_right;
  156. if (nf_swapped in flags) then
  157. swapleftright;
  158. { force fpureg as location, left right doesn't matter
  159. as both will be in a fpureg }
  160. location_force_fpureg(current_asmdata.CurrAsmList,left.location,true);
  161. location_force_fpureg(current_asmdata.CurrAsmList,right.location,true);
  162. location_reset(location,LOC_FLAGS,OS_NO);
  163. location.resflags:=getresflags(true);
  164. if nodetype in [equaln,unequaln] then
  165. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMF,
  166. left.location.register,right.location.register),
  167. cgsize2fpuoppostfix[def_cgsize(resultdef)]))
  168. else
  169. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMFE,
  170. left.location.register,right.location.register),
  171. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  172. location_reset(location,LOC_FLAGS,OS_NO);
  173. location.resflags:=getresflags(false);
  174. end;
  175. procedure tarmaddnode.second_cmpsmallset;
  176. var
  177. tmpreg : tregister;
  178. begin
  179. pass_left_right;
  180. location_reset(location,LOC_FLAGS,OS_NO);
  181. force_reg_left_right(false,false);
  182. case nodetype of
  183. equaln:
  184. begin
  185. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  186. location.resflags:=F_EQ;
  187. end;
  188. unequaln:
  189. begin
  190. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  191. location.resflags:=F_NE;
  192. end;
  193. lten,
  194. gten:
  195. begin
  196. if (not(nf_swapped in flags) and
  197. (nodetype = lten)) or
  198. ((nf_swapped in flags) and
  199. (nodetype = gten)) then
  200. swapleftright;
  201. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  202. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_AND,tmpreg,left.location.register,right.location.register));
  203. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,tmpreg,right.location.register));
  204. location.resflags:=F_EQ;
  205. end;
  206. else
  207. internalerror(2004012401);
  208. end;
  209. end;
  210. procedure tarmaddnode.second_cmp64bit;
  211. var
  212. unsigned : boolean;
  213. oldnodetype : tnodetype;
  214. begin
  215. pass_left_right;
  216. force_reg_left_right(false,false);
  217. unsigned:=not(is_signed(left.resultdef)) or
  218. not(is_signed(right.resultdef));
  219. { operation requiring proper N, Z and C flags ? }
  220. if unsigned or (nodetype in [equaln,unequaln]) then
  221. begin
  222. location_reset(location,LOC_FLAGS,OS_NO);
  223. location.resflags:=getresflags(unsigned);
  224. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi));
  225. if current_settings.cputype in cpu_thumb2 then
  226. current_asmdata.CurrAsmList.concat(taicpu.op_cond(A_IT, C_EQ));
  227. current_asmdata.CurrAsmList.concat(setcondition(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo),C_EQ));
  228. end
  229. else
  230. { operation requiring proper N, Z and V flags ? }
  231. begin
  232. location_reset(location,LOC_JUMP,OS_NO);
  233. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi));
  234. { the jump the sequence is a little bit hairy }
  235. case nodetype of
  236. ltn,gtn:
  237. begin
  238. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(false),current_procinfo.CurrTrueLabel);
  239. { cheat a little bit for the negative test }
  240. toggleflag(nf_swapped);
  241. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(false),current_procinfo.CurrFalseLabel);
  242. toggleflag(nf_swapped);
  243. end;
  244. lten,gten:
  245. begin
  246. oldnodetype:=nodetype;
  247. if nodetype=lten then
  248. nodetype:=ltn
  249. else
  250. nodetype:=gtn;
  251. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  252. { cheat for the negative test }
  253. if nodetype=ltn then
  254. nodetype:=gtn
  255. else
  256. nodetype:=ltn;
  257. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  258. nodetype:=oldnodetype;
  259. end;
  260. end;
  261. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo));
  262. { the comparisaion of the low dword have to be
  263. always unsigned! }
  264. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  265. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  266. end;
  267. end;
  268. function tarmaddnode.pass_1 : tnode;
  269. var
  270. unsigned : boolean;
  271. begin
  272. result:=inherited pass_1;
  273. if not(assigned(result)) then
  274. begin
  275. unsigned:=not(is_signed(left.resultdef)) or
  276. not(is_signed(right.resultdef));
  277. if is_64bit(left.resultdef) and
  278. ((nodetype in [equaln,unequaln]) or
  279. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  280. ) then
  281. expectloc:=LOC_FLAGS;
  282. end;
  283. end;
  284. procedure tarmaddnode.second_cmpordinal;
  285. var
  286. unsigned : boolean;
  287. tmpreg : tregister;
  288. b : byte;
  289. begin
  290. pass_left_right;
  291. force_reg_left_right(true,true);
  292. unsigned:=not(is_signed(left.resultdef)) or
  293. not(is_signed(right.resultdef));
  294. if right.location.loc = LOC_CONSTANT then
  295. begin
  296. if is_shifter_const(right.location.value,b) then
  297. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,right.location.value))
  298. else
  299. begin
  300. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  301. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  302. right.location.value,tmpreg);
  303. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,tmpreg));
  304. end;
  305. end
  306. else
  307. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  308. location_reset(location,LOC_FLAGS,OS_NO);
  309. location.resflags:=getresflags(unsigned);
  310. end;
  311. begin
  312. caddnode:=tarmaddnode;
  313. end.