narmadd.pas 18 KB

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