narmadd.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  219. if nodetype in [equaln,unequaln] then
  220. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMF,
  221. left.location.register,right.location.register),
  222. cgsize2fpuoppostfix[def_cgsize(resultdef)]))
  223. else
  224. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg(A_CMFE,
  225. left.location.register,right.location.register),
  226. cgsize2fpuoppostfix[def_cgsize(resultdef)]));
  227. end;
  228. fpu_vfpv2,
  229. fpu_vfpv3,
  230. fpu_vfpv3_d16:
  231. begin
  232. location_force_mmregscalar(current_asmdata.CurrAsmList,left.location,true);
  233. location_force_mmregscalar(current_asmdata.CurrAsmList,right.location,true);
  234. if (tfloatdef(left.resultdef).floattype=s32real) then
  235. if nodetype in [equaln,unequaln] then
  236. op:=A_FCMPS
  237. else
  238. op:=A_FCMPES
  239. else if nodetype in [equaln,unequaln] then
  240. op:=A_FCMPD
  241. else
  242. op:=A_FCMPED;
  243. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,
  244. left.location.register,right.location.register));
  245. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  246. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_FMSTAT));
  247. end;
  248. fpu_soft:
  249. { this case should be handled already by pass1 }
  250. internalerror(2009112404);
  251. end;
  252. location_reset(location,LOC_FLAGS,OS_NO);
  253. location.resflags:=getresflags(false);
  254. end;
  255. procedure tarmaddnode.second_cmpsmallset;
  256. var
  257. tmpreg : tregister;
  258. b: byte;
  259. begin
  260. pass_left_right;
  261. location_reset(location,LOC_FLAGS,OS_NO);
  262. if (not(nf_swapped in flags) and
  263. (nodetype = lten)) or
  264. ((nf_swapped in flags) and
  265. (nodetype = gten)) then
  266. swapleftright;
  267. (* Try to keep right as a constant *)
  268. if (right.location.loc <> LOC_CONSTANT) or
  269. not(is_shifter_const(right.location.value, b)) then
  270. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  271. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  272. case nodetype of
  273. equaln,
  274. unequaln:
  275. begin
  276. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  277. if right.location.loc = LOC_CONSTANT then
  278. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,right.location.value))
  279. else
  280. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  281. if nodetype = equaln then
  282. location.resflags:=F_EQ
  283. else
  284. location.resflags:=F_NE;
  285. end;
  286. lten,
  287. gten:
  288. begin
  289. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  290. if right.location.loc = LOC_CONSTANT then
  291. begin
  292. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_const(A_AND,tmpreg,left.location.register,right.location.value));
  293. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  294. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,tmpreg,right.location.value));
  295. end
  296. else
  297. begin
  298. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_AND,tmpreg,left.location.register,right.location.register));
  299. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  300. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,tmpreg,right.location.register));
  301. end;
  302. location.resflags:=F_EQ;
  303. end;
  304. else
  305. internalerror(2004012401);
  306. end;
  307. end;
  308. procedure tarmaddnode.second_cmp64bit;
  309. var
  310. unsigned : boolean;
  311. oldnodetype : tnodetype;
  312. dummyreg : tregister;
  313. l: tasmlabel;
  314. begin
  315. unsigned:=not(is_signed(left.resultdef)) or
  316. not(is_signed(right.resultdef));
  317. pass_left_right;
  318. if (nodetype in [equaln,unequaln]) and
  319. (left.nodetype=ordconstn) and (tordconstnode(left).value=0) then
  320. begin
  321. location_reset(location,LOC_FLAGS,OS_NO);
  322. location.resflags:=getresflags(unsigned);
  323. if not(right.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  324. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  325. dummyreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  326. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  327. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ORR,dummyreg,right.location.register64.reglo,right.location.register64.reghi),PF_S));
  328. end
  329. else if (nodetype in [equaln,unequaln]) and
  330. (right.nodetype=ordconstn) and (tordconstnode(right).value=0) then
  331. begin
  332. location_reset(location,LOC_FLAGS,OS_NO);
  333. location.resflags:=getresflags(unsigned);
  334. if not(left.location.loc in [LOC_CREGISTER,LOC_REGISTER]) then
  335. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  336. dummyreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  337. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  338. current_asmdata.CurrAsmList.concat(setoppostfix(taicpu.op_reg_reg_reg(A_ORR,dummyreg,left.location.register64.reglo,left.location.register64.reghi),PF_S));
  339. end
  340. else
  341. begin
  342. hlcg.location_force_reg(current_asmdata.CurrAsmList,left.location,left.resultdef,left.resultdef,true);
  343. hlcg.location_force_reg(current_asmdata.CurrAsmList,right.location,right.resultdef,right.resultdef,true);
  344. { operation requiring proper N, Z and C flags ? }
  345. if unsigned or (nodetype in [equaln,unequaln]) then
  346. begin
  347. location_reset(location,LOC_FLAGS,OS_NO);
  348. location.resflags:=getresflags(unsigned);
  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. if current_settings.cputype in cpu_thumb2 then
  352. begin
  353. current_asmdata.getjumplabel(l);
  354. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,l);
  355. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo));
  356. cg.a_label(current_asmdata.CurrAsmList,l);
  357. end
  358. else
  359. current_asmdata.CurrAsmList.concat(setcondition(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo),C_EQ));
  360. end
  361. else
  362. { operation requiring proper N, Z and V flags ? }
  363. begin
  364. location_reset(location,LOC_JUMP,OS_NO);
  365. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  366. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi));
  367. { the jump the sequence is a little bit hairy }
  368. case nodetype of
  369. ltn,gtn:
  370. begin
  371. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(false),current_procinfo.CurrTrueLabel);
  372. { cheat a little bit for the negative test }
  373. toggleflag(nf_swapped);
  374. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(false),current_procinfo.CurrFalseLabel);
  375. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  376. toggleflag(nf_swapped);
  377. end;
  378. lten,gten:
  379. begin
  380. oldnodetype:=nodetype;
  381. if nodetype=lten then
  382. nodetype:=ltn
  383. else
  384. nodetype:=gtn;
  385. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  386. { cheat for the negative test }
  387. if nodetype=ltn then
  388. nodetype:=gtn
  389. else
  390. nodetype:=ltn;
  391. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  392. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  393. nodetype:=oldnodetype;
  394. end;
  395. end;
  396. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  397. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo));
  398. { the comparisaion of the low dword have to be
  399. always unsigned! }
  400. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  401. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  402. cg.a_reg_dealloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  403. end;
  404. end;
  405. end;
  406. function tarmaddnode.pass_1 : tnode;
  407. var
  408. unsigned : boolean;
  409. begin
  410. result:=inherited pass_1;
  411. if not(assigned(result)) then
  412. begin
  413. unsigned:=not(is_signed(left.resultdef)) or
  414. not(is_signed(right.resultdef));
  415. if is_64bit(left.resultdef) and
  416. ((nodetype in [equaln,unequaln]) or
  417. (unsigned and (nodetype in [ltn,lten,gtn,gten]))
  418. ) then
  419. expectloc:=LOC_FLAGS;
  420. end;
  421. end;
  422. procedure tarmaddnode.second_cmpordinal;
  423. var
  424. unsigned : boolean;
  425. tmpreg : tregister;
  426. b : byte;
  427. begin
  428. pass_left_right;
  429. force_reg_left_right(true,true);
  430. unsigned:=not(is_signed(left.resultdef)) or
  431. not(is_signed(right.resultdef));
  432. cg.a_reg_alloc(current_asmdata.CurrAsmList,NR_DEFAULTFLAGS);
  433. if right.location.loc = LOC_CONSTANT then
  434. begin
  435. if is_shifter_const(right.location.value,b) then
  436. current_asmdata.CurrAsmList.concat(taicpu.op_reg_const(A_CMP,left.location.register,right.location.value))
  437. else
  438. begin
  439. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  440. cg.a_load_const_reg(current_asmdata.CurrAsmList,OS_INT,
  441. right.location.value,tmpreg);
  442. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,tmpreg));
  443. end;
  444. end
  445. else
  446. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register,right.location.register));
  447. location_reset(location,LOC_FLAGS,OS_NO);
  448. location.resflags:=getresflags(unsigned);
  449. end;
  450. begin
  451. caddnode:=tarmaddnode;
  452. end.