narmadd.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. fpu_vfpv3_d16:
  150. begin
  151. { force mmreg as location, left right doesn't matter
  152. as both will be in a fpureg }
  153. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,true);
  154. location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,true);
  155. location_reset(location,LOC_MMREGISTER,def_cgsize(resultdef));
  156. if left.location.loc<>LOC_CMMREGISTER then
  157. location.register:=left.location.register
  158. else if right.location.loc<>LOC_CMMREGISTER then
  159. location.register:=right.location.register
  160. else
  161. location.register:=cg.getmmregister(current_asmdata.CurrAsmList,location.size);
  162. singleprec:=tfloatdef(left.resultdef).floattype=s32real;
  163. case nodetype of
  164. addn :
  165. if singleprec then
  166. op:=A_FADDS
  167. else
  168. op:=A_FADDD;
  169. muln :
  170. if singleprec then
  171. op:=A_FMULS
  172. else
  173. op:=A_FMULD;
  174. subn :
  175. if singleprec then
  176. op:=A_FSUBS
  177. else
  178. op:=A_FSUBD;
  179. slashn :
  180. if singleprec then
  181. op:=A_FDIVS
  182. else
  183. op:=A_FDIVD;
  184. else
  185. internalerror(2009111401);
  186. end;
  187. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  188. location.register,left.location.register,right.location.register));
  189. end;
  190. fpu_soft:
  191. { this case should be handled already by pass1 }
  192. internalerror(200308252);
  193. else
  194. internalerror(200308251);
  195. end;
  196. end;
  197. procedure tarmaddnode.second_cmpfloat;
  198. var
  199. op: TAsmOp;
  200. begin
  201. pass_left_right;
  202. if (nf_swapped in flags) then
  203. swapleftright;
  204. location_reset(location,LOC_FLAGS,OS_NO);
  205. location.resflags:=getresflags(true);
  206. case current_settings.fputype of
  207. fpu_fpa,
  208. fpu_fpa10,
  209. fpu_fpa11:
  210. begin
  211. { force fpureg as location, left right doesn't matter
  212. as both will be in a fpureg }
  213. location_force_fpureg(current_asmdata.CurrAsmList,left.location,true);
  214. location_force_fpureg(current_asmdata.CurrAsmList,right.location,true);
  215. if nodetype in [equaln,unequaln] then
  216. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMF,
  217. left.location.register,right.location.register),
  218. cgsize2fpuoppostfix[def_cgsize(resultdef)]))
  219. else
  220. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMFE,
  221. left.location.register,right.location.register),
  222. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  223. end;
  224. fpu_vfpv2,
  225. fpu_vfpv3,
  226. fpu_vfpv3_d16:
  227. begin
  228. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,true);
  229. location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,true);
  230. if (tfloatdef(left.resultdef).floattype=s32real) then
  231. if nodetype in [equaln,unequaln] then
  232. op:=A_FCMPS
  233. else
  234. op:=A_FCMPES
  235. else if nodetype in [equaln,unequaln] then
  236. op:=A_FCMPD
  237. else
  238. op:=A_FCMPED;
  239. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,
  240. left.location.register,right.location.register));
  241. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_FMSTAT));
  242. end;
  243. fpu_soft:
  244. { this case should be handled already by pass1 }
  245. internalerror(2009112404);
  246. end;
  247. location_reset(location,LOC_FLAGS,OS_NO);
  248. location.resflags:=getresflags(false);
  249. end;
  250. procedure tarmaddnode.second_cmpsmallset;
  251. var
  252. tmpreg : tregister;
  253. begin
  254. pass_left_right;
  255. location_reset(location,LOC_FLAGS,OS_NO);
  256. force_reg_left_right(false,false);
  257. case nodetype of
  258. equaln:
  259. begin
  260. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  261. location.resflags:=F_EQ;
  262. end;
  263. unequaln:
  264. begin
  265. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  266. location.resflags:=F_NE;
  267. end;
  268. lten,
  269. gten:
  270. begin
  271. if (not(nf_swapped in flags) and
  272. (nodetype = lten)) or
  273. ((nf_swapped in flags) and
  274. (nodetype = gten)) then
  275. swapleftright;
  276. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  277. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_AND,tmpreg,left.location.register,right.location.register));
  278. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,tmpreg,right.location.register));
  279. location.resflags:=F_EQ;
  280. end;
  281. else
  282. internalerror(2004012401);
  283. end;
  284. end;
  285. procedure tarmaddnode.second_cmp64bit;
  286. var
  287. unsigned : boolean;
  288. oldnodetype : tnodetype;
  289. begin
  290. pass_left_right;
  291. force_reg_left_right(false,false);
  292. unsigned:=not(is_signed(left.resultdef)) or
  293. not(is_signed(right.resultdef));
  294. { operation requiring proper N, Z and C flags ? }
  295. if unsigned or (nodetype in [equaln,unequaln]) then
  296. begin
  297. location_reset(location,LOC_FLAGS,OS_NO);
  298. location.resflags:=getresflags(unsigned);
  299. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi));
  300. if current_settings.cputype in cpu_thumb2 then
  301. current_asmdata.CurrAsmList.concat(taicpu.op_cond(A_IT, C_EQ));
  302. current_asmdata.CurrAsmList.concat(setcondition(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo),C_EQ));
  303. end
  304. else
  305. { operation requiring proper N, Z and V flags ? }
  306. begin
  307. location_reset(location,LOC_JUMP,OS_NO);
  308. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi));
  309. { the jump the sequence is a little bit hairy }
  310. case nodetype of
  311. ltn,gtn:
  312. begin
  313. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(false),current_procinfo.CurrTrueLabel);
  314. { cheat a little bit for the negative test }
  315. toggleflag(nf_swapped);
  316. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(false),current_procinfo.CurrFalseLabel);
  317. toggleflag(nf_swapped);
  318. end;
  319. lten,gten:
  320. begin
  321. oldnodetype:=nodetype;
  322. if nodetype=lten then
  323. nodetype:=ltn
  324. else
  325. nodetype:=gtn;
  326. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  327. { cheat for the negative test }
  328. if nodetype=ltn then
  329. nodetype:=gtn
  330. else
  331. nodetype:=ltn;
  332. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  333. nodetype:=oldnodetype;
  334. end;
  335. end;
  336. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo));
  337. { the comparisaion of the low dword have to be
  338. always unsigned! }
  339. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  340. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  341. end;
  342. end;
  343. function tarmaddnode.pass_1 : tnode;
  344. var
  345. unsigned : boolean;
  346. begin
  347. result:=inherited pass_1;
  348. if not(assigned(result)) then
  349. begin
  350. unsigned:=not(is_signed(left.resultdef)) or
  351. not(is_signed(right.resultdef));
  352. if is_64bit(left.resultdef) and
  353. ((nodetype in [equaln,unequaln]) or
  354. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  355. ) then
  356. expectloc:=LOC_FLAGS;
  357. end;
  358. end;
  359. procedure tarmaddnode.second_cmpordinal;
  360. var
  361. unsigned : boolean;
  362. tmpreg : tregister;
  363. b : byte;
  364. begin
  365. pass_left_right;
  366. force_reg_left_right(true,true);
  367. unsigned:=not(is_signed(left.resultdef)) or
  368. not(is_signed(right.resultdef));
  369. if right.location.loc = LOC_CONSTANT then
  370. begin
  371. if is_shifter_const(right.location.value,b) then
  372. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,right.location.value))
  373. else
  374. begin
  375. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  376. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  377. right.location.value,tmpreg);
  378. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,tmpreg));
  379. end;
  380. end
  381. else
  382. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  383. location_reset(location,LOC_FLAGS,OS_NO);
  384. location.resflags:=getresflags(unsigned);
  385. end;
  386. begin
  387. caddnode:=tarmaddnode;
  388. end.