narmadd.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  265. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  266. location.resflags:=F_EQ;
  267. end;
  268. unequaln:
  269. begin
  270. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  271. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  272. location.resflags:=F_NE;
  273. end;
  274. lten,
  275. gten:
  276. begin
  277. if (not(nf_swapped in flags) and
  278. (nodetype = lten)) or
  279. ((nf_swapped in flags) and
  280. (nodetype = gten)) then
  281. swapleftright;
  282. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  283. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_AND,tmpreg,left.location.register,right.location.register));
  284. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  285. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,tmpreg,right.location.register));
  286. location.resflags:=F_EQ;
  287. end;
  288. else
  289. internalerror(2004012401);
  290. end;
  291. end;
  292. procedure tarmaddnode.second_cmp64bit;
  293. var
  294. unsigned : boolean;
  295. oldnodetype : tnodetype;
  296. dummyreg : tregister;
  297. l: tasmlabel;
  298. begin
  299. unsigned:=not(is_signed(left.resultdef)) or
  300. not(is_signed(right.resultdef));
  301. pass_left_right;
  302. if (nodetype in [equaln,unequaln]) and
  303. (left.nodetype=ordconstn) and (tordconstnode(left).value=0) then
  304. begin
  305. location_reset(location,LOC_FLAGS,OS_NO);
  306. location.resflags:=getresflags(unsigned);
  307. if not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  308. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  309. dummyreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  310. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  311. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ORR,dummyreg,right.location.register64.reglo,right.location.register64.reghi),PF_S));
  312. end
  313. else if (nodetype in [equaln,unequaln]) and
  314. (right.nodetype=ordconstn) and (tordconstnode(right).value=0) then
  315. begin
  316. location_reset(location,LOC_FLAGS,OS_NO);
  317. location.resflags:=getresflags(unsigned);
  318. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  319. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  320. dummyreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  321. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  322. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ORR,dummyreg,left.location.register64.reglo,left.location.register64.reghi),PF_S));
  323. end
  324. else
  325. begin
  326. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  327. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  328. { operation requiring proper N, Z and C flags ? }
  329. if unsigned or (nodetype in [equaln,unequaln]) then
  330. begin
  331. location_reset(location,LOC_FLAGS,OS_NO);
  332. location.resflags:=getresflags(unsigned);
  333. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  334. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi));
  335. if current_settings.cputype in cpu_thumb2 then
  336. begin
  337. current_asmdata.getjumplabel(l);
  338. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,l);
  339. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo));
  340. cg.a_label(current_asmdata.CurrAsmList,l);
  341. end
  342. else
  343. current_asmdata.CurrAsmList.concat(setcondition(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo),C_EQ));
  344. end
  345. else
  346. { operation requiring proper N, Z and V flags ? }
  347. begin
  348. location_reset(location,LOC_JUMP,OS_NO);
  349. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  350. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi));
  351. { the jump the sequence is a little bit hairy }
  352. case nodetype of
  353. ltn,gtn:
  354. begin
  355. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(false),current_procinfo.CurrTrueLabel);
  356. { cheat a little bit for the negative test }
  357. toggleflag(nf_swapped);
  358. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(false),current_procinfo.CurrFalseLabel);
  359. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  360. toggleflag(nf_swapped);
  361. end;
  362. lten,gten:
  363. begin
  364. oldnodetype:=nodetype;
  365. if nodetype=lten then
  366. nodetype:=ltn
  367. else
  368. nodetype:=gtn;
  369. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  370. { cheat for the negative test }
  371. if nodetype=ltn then
  372. nodetype:=gtn
  373. else
  374. nodetype:=ltn;
  375. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  376. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  377. nodetype:=oldnodetype;
  378. end;
  379. end;
  380. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  381. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo));
  382. { the comparisaion of the low dword have to be
  383. always unsigned! }
  384. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  385. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  386. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  387. end;
  388. end;
  389. end;
  390. function tarmaddnode.pass_1 : tnode;
  391. var
  392. unsigned : boolean;
  393. begin
  394. result:=inherited pass_1;
  395. if not(assigned(result)) then
  396. begin
  397. unsigned:=not(is_signed(left.resultdef)) or
  398. not(is_signed(right.resultdef));
  399. if is_64bit(left.resultdef) and
  400. ((nodetype in [equaln,unequaln]) or
  401. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  402. ) then
  403. expectloc:=LOC_FLAGS;
  404. end;
  405. end;
  406. procedure tarmaddnode.second_cmpordinal;
  407. var
  408. unsigned : boolean;
  409. tmpreg : tregister;
  410. b : byte;
  411. begin
  412. pass_left_right;
  413. force_reg_left_right(true,true);
  414. unsigned:=not(is_signed(left.resultdef)) or
  415. not(is_signed(right.resultdef));
  416. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  417. if right.location.loc = LOC_CONSTANT then
  418. begin
  419. if is_shifter_const(right.location.value,b) then
  420. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,right.location.value))
  421. else
  422. begin
  423. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  424. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  425. right.location.value,tmpreg);
  426. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,tmpreg));
  427. end;
  428. end
  429. else
  430. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  431. location_reset(location,LOC_FLAGS,OS_NO);
  432. location.resflags:=getresflags(unsigned);
  433. end;
  434. begin
  435. caddnode:=tarmaddnode;
  436. end.