narmadd.pas 17 KB

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