narmadd.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. public
  27. function pass_1 : tnode;override;
  28. protected
  29. procedure second_addfloat;override;
  30. procedure second_cmpfloat;override;
  31. procedure second_cmpordinal;override;
  32. procedure second_cmpsmallset;override;
  33. procedure second_cmp64bit;override;
  34. end;
  35. implementation
  36. uses
  37. globtype,systems,
  38. cutils,verbose,globals,
  39. symconst,symdef,paramgr,
  40. aasmbase,aasmtai,aasmdata,aasmcpu,defutil,htypechk,
  41. cgbase,cgutils,cgcpu,
  42. cpuinfo,pass_1,pass_2,regvars,procinfo,
  43. cpupara,
  44. ncon,nset,nadd,
  45. ncgutil,tgobj,rgobj,rgcpu,cgobj,cg64f32;
  46. {*****************************************************************************
  47. TSparcAddNode
  48. *****************************************************************************}
  49. function tarmaddnode.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_GT;
  63. lten:
  64. GetResFlags:=F_GE;
  65. gtn:
  66. GetResFlags:=F_LT;
  67. gten:
  68. GetResFlags:=F_LE;
  69. end
  70. else
  71. case NodeType of
  72. ltn:
  73. GetResFlags:=F_LT;
  74. lten:
  75. GetResFlags:=F_LE;
  76. gtn:
  77. GetResFlags:=F_GT;
  78. gten:
  79. GetResFlags:=F_GE;
  80. end;
  81. end
  82. else
  83. begin
  84. if nf_swapped in Flags then
  85. case NodeType of
  86. ltn:
  87. GetResFlags:=F_HI;
  88. lten:
  89. GetResFlags:=F_CS;
  90. gtn:
  91. GetResFlags:=F_CC;
  92. gten:
  93. GetResFlags:=F_LS;
  94. end
  95. else
  96. case NodeType of
  97. ltn:
  98. GetResFlags:=F_CC;
  99. lten:
  100. GetResFlags:=F_LS;
  101. gtn:
  102. GetResFlags:=F_HI;
  103. gten:
  104. GetResFlags:=F_CS;
  105. end;
  106. end;
  107. end;
  108. end;
  109. procedure tarmaddnode.second_addfloat;
  110. var
  111. op : TAsmOp;
  112. singleprec: boolean;
  113. begin
  114. pass_left_right;
  115. if (nf_swapped in flags) then
  116. swapleftright;
  117. case current_settings.fputype of
  118. fpu_fpa,
  119. fpu_fpa10,
  120. fpu_fpa11:
  121. begin
  122. { force fpureg as location, left right doesn't matter
  123. as both will be in a fpureg }
  124. location_force_fpureg(current_asmdata.CurrAsmList,left.location,true);
  125. location_force_fpureg(current_asmdata.CurrAsmList,right.location,(left.location.loc<>LOC_CFPUREGISTER));
  126. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  127. if left.location.loc<>LOC_CFPUREGISTER then
  128. location.register:=left.location.register
  129. else
  130. location.register:=right.location.register;
  131. case nodetype of
  132. addn :
  133. op:=A_ADF;
  134. muln :
  135. op:=A_MUF;
  136. subn :
  137. op:=A_SUF;
  138. slashn :
  139. op:=A_DVF;
  140. else
  141. internalerror(200308313);
  142. end;
  143. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(op,
  144. location.register,left.location.register,right.location.register),
  145. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  146. end;
  147. fpu_vfpv2,
  148. fpu_vfpv3:
  149. begin
  150. { force mmreg as location, left right doesn't matter
  151. as both will be in a fpureg }
  152. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,true);
  153. location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,true);
  154. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  155. if left.location.loc<>LOC_CMMREGISTER then
  156. location.register:=left.location.register
  157. else if right.location.loc<>LOC_CMMREGISTER then
  158. location.register:=right.location.register
  159. else
  160. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  161. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  162. case nodetype of
  163. addn :
  164. if singleprec then
  165. op:=A_FADDS
  166. else
  167. op:=A_FADDD;
  168. muln :
  169. if singleprec then
  170. op:=A_FMULS
  171. else
  172. op:=A_FMULD;
  173. subn :
  174. if singleprec then
  175. op:=A_FSUBS
  176. else
  177. op:=A_FSUBD;
  178. slashn :
  179. if singleprec then
  180. op:=A_FDIVS
  181. else
  182. op:=A_FDIVD;
  183. else
  184. internalerror(2009111401);
  185. end;
  186. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  187. location.register,left.location.register,right.location.register));
  188. end;
  189. fpu_soft:
  190. { this case should be handled already by pass1 }
  191. internalerror(200308252);
  192. else
  193. internalerror(200308251);
  194. end;
  195. end;
  196. procedure tarmaddnode.second_cmpfloat;
  197. var
  198. op: TAsmOp;
  199. begin
  200. pass_left_right;
  201. if (nf_swapped in flags) then
  202. swapleftright;
  203. location_reset(location,LOC_FLAGS,OS_NO);
  204. location.resflags:=getresflags(true);
  205. case current_settings.fputype of
  206. fpu_fpa,
  207. fpu_fpa10,
  208. fpu_fpa11:
  209. begin
  210. { force fpureg as location, left right doesn't matter
  211. as both will be in a fpureg }
  212. location_force_fpureg(current_asmdata.CurrAsmList,left.location,true);
  213. location_force_fpureg(current_asmdata.CurrAsmList,right.location,true);
  214. if nodetype in [equaln,unequaln] then
  215. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMF,
  216. left.location.register,right.location.register),
  217. cgsize2fpuoppostfix[def_cgsize(resultdef)]))
  218. else
  219. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMFE,
  220. left.location.register,right.location.register),
  221. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  222. end;
  223. fpu_vfpv2,
  224. fpu_vfpv3:
  225. begin
  226. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,true);
  227. location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,true);
  228. if (tfloatdef(left.resultdef).floattype=s32real) then
  229. if nodetype in [equaln,unequaln] then
  230. op:=A_FCMPS
  231. else
  232. op:=A_FCMPES
  233. else if nodetype in [equaln,unequaln] then
  234. op:=A_FCMPD
  235. else
  236. op:=A_FCMPED;
  237. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,
  238. left.location.register,right.location.register));
  239. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_FMSTAT));
  240. end;
  241. fpu_soft:
  242. { this case should be handled already by pass1 }
  243. internalerror(2009112404);
  244. end;
  245. location_reset(location,LOC_FLAGS,OS_NO);
  246. location.resflags:=getresflags(false);
  247. end;
  248. procedure tarmaddnode.second_cmpsmallset;
  249. var
  250. tmpreg : tregister;
  251. begin
  252. pass_left_right;
  253. location_reset(location,LOC_FLAGS,OS_NO);
  254. force_reg_left_right(false,false);
  255. case nodetype of
  256. equaln:
  257. begin
  258. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  259. location.resflags:=F_EQ;
  260. end;
  261. unequaln:
  262. begin
  263. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  264. location.resflags:=F_NE;
  265. end;
  266. lten,
  267. gten:
  268. begin
  269. if (not(nf_swapped in flags) and
  270. (nodetype = lten)) or
  271. ((nf_swapped in flags) and
  272. (nodetype = gten)) then
  273. swapleftright;
  274. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  275. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_AND,tmpreg,left.location.register,right.location.register));
  276. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,tmpreg,right.location.register));
  277. location.resflags:=F_EQ;
  278. end;
  279. else
  280. internalerror(2004012401);
  281. end;
  282. end;
  283. procedure tarmaddnode.second_cmp64bit;
  284. var
  285. unsigned : boolean;
  286. oldnodetype : tnodetype;
  287. begin
  288. pass_left_right;
  289. force_reg_left_right(false,false);
  290. unsigned:=not(is_signed(left.resultdef)) or
  291. not(is_signed(right.resultdef));
  292. { operation requiring proper N, Z and C flags ? }
  293. if unsigned or (nodetype in [equaln,unequaln]) then
  294. begin
  295. location_reset(location,LOC_FLAGS,OS_NO);
  296. location.resflags:=getresflags(unsigned);
  297. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi));
  298. if current_settings.cputype in cpu_thumb2 then
  299. current_asmdata.CurrAsmList.concat(taicpu.op_cond(A_IT, C_EQ));
  300. current_asmdata.CurrAsmList.concat(setcondition(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo),C_EQ));
  301. end
  302. else
  303. { operation requiring proper N, Z and V flags ? }
  304. begin
  305. location_reset(location,LOC_JUMP,OS_NO);
  306. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi));
  307. { the jump the sequence is a little bit hairy }
  308. case nodetype of
  309. ltn,gtn:
  310. begin
  311. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(false),current_procinfo.CurrTrueLabel);
  312. { cheat a little bit for the negative test }
  313. toggleflag(nf_swapped);
  314. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(false),current_procinfo.CurrFalseLabel);
  315. toggleflag(nf_swapped);
  316. end;
  317. lten,gten:
  318. begin
  319. oldnodetype:=nodetype;
  320. if nodetype=lten then
  321. nodetype:=ltn
  322. else
  323. nodetype:=gtn;
  324. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  325. { cheat for the negative test }
  326. if nodetype=ltn then
  327. nodetype:=gtn
  328. else
  329. nodetype:=ltn;
  330. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  331. nodetype:=oldnodetype;
  332. end;
  333. end;
  334. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo));
  335. { the comparisaion of the low dword have to be
  336. always unsigned! }
  337. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  338. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  339. end;
  340. end;
  341. function tarmaddnode.pass_1 : tnode;
  342. var
  343. unsigned : boolean;
  344. begin
  345. result:=inherited pass_1;
  346. if not(assigned(result)) then
  347. begin
  348. unsigned:=not(is_signed(left.resultdef)) or
  349. not(is_signed(right.resultdef));
  350. if is_64bit(left.resultdef) and
  351. ((nodetype in [equaln,unequaln]) or
  352. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  353. ) then
  354. expectloc:=LOC_FLAGS;
  355. end;
  356. end;
  357. procedure tarmaddnode.second_cmpordinal;
  358. var
  359. unsigned : boolean;
  360. tmpreg : tregister;
  361. b : byte;
  362. begin
  363. pass_left_right;
  364. force_reg_left_right(true,true);
  365. unsigned:=not(is_signed(left.resultdef)) or
  366. not(is_signed(right.resultdef));
  367. if right.location.loc = LOC_CONSTANT then
  368. begin
  369. if is_shifter_const(right.location.value,b) then
  370. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,right.location.value))
  371. else
  372. begin
  373. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  374. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  375. right.location.value,tmpreg);
  376. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,tmpreg));
  377. end;
  378. end
  379. else
  380. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  381. location_reset(location,LOC_FLAGS,OS_NO);
  382. location.resflags:=getresflags(unsigned);
  383. end;
  384. begin
  385. caddnode:=tarmaddnode;
  386. end.