narmadd.pas 19 KB

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