ncpuadd.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. {
  2. Copyright (c) 2000-2002 by Florian Klaempfl
  3. Code generation for add nodes on the SPARC
  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 ncpuadd;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,ncgadd,cpubase;
  22. type
  23. tsparcaddnode = class(tcgaddnode)
  24. private
  25. function GetResFlags(unsigned:Boolean):TResFlags;
  26. function GetFPUResFlags:TResFlags;
  27. protected
  28. procedure second_addfloat;override;
  29. procedure second_cmpfloat;override;
  30. procedure second_cmpboolean;override;
  31. procedure second_cmpsmallset;override;
  32. procedure second_cmp64bit;override;
  33. procedure second_add64bit;override;
  34. procedure second_cmpordinal;override;
  35. procedure second_addordinal;override;
  36. public
  37. function pass_1: tnode; override;
  38. function use_generic_mul32to64: boolean; override;
  39. end;
  40. implementation
  41. uses
  42. systems,
  43. cutils,verbose,
  44. paramgr,procinfo,
  45. aasmtai,aasmdata,aasmcpu,defutil,
  46. cgbase,cgsparc,cgcpu,cgutils,
  47. cpupara,
  48. ncon,nset,nadd,
  49. hlcgobj,ncgutil,cgobj;
  50. {*****************************************************************************
  51. TSparcAddNode
  52. *****************************************************************************}
  53. function TSparcAddNode.GetResFlags(unsigned:Boolean):TResFlags;
  54. begin
  55. case NodeType of
  56. equaln:
  57. GetResFlags:=F_E;
  58. unequaln:
  59. GetResFlags:=F_NE;
  60. else
  61. if not(unsigned) then
  62. begin
  63. if nf_swapped in flags then
  64. case NodeType of
  65. ltn:
  66. GetResFlags:=F_G;
  67. lten:
  68. GetResFlags:=F_GE;
  69. gtn:
  70. GetResFlags:=F_L;
  71. gten:
  72. GetResFlags:=F_LE;
  73. else
  74. internalerror(2014082010);
  75. end
  76. else
  77. case NodeType of
  78. ltn:
  79. GetResFlags:=F_L;
  80. lten:
  81. GetResFlags:=F_LE;
  82. gtn:
  83. GetResFlags:=F_G;
  84. gten:
  85. GetResFlags:=F_GE;
  86. else
  87. internalerror(2014082011);
  88. end;
  89. end
  90. else
  91. begin
  92. if nf_swapped in Flags then
  93. case NodeType of
  94. ltn:
  95. GetResFlags:=F_A;
  96. lten:
  97. GetResFlags:=F_AE;
  98. gtn:
  99. GetResFlags:=F_B;
  100. gten:
  101. GetResFlags:=F_BE;
  102. else
  103. internalerror(2014082012);
  104. end
  105. else
  106. case NodeType of
  107. ltn:
  108. GetResFlags:=F_B;
  109. lten:
  110. GetResFlags:=F_BE;
  111. gtn:
  112. GetResFlags:=F_A;
  113. gten:
  114. GetResFlags:=F_AE;
  115. else
  116. internalerror(2014082013);
  117. end;
  118. end;
  119. end;
  120. end;
  121. function TSparcAddNode.GetFPUResFlags:TResFlags;
  122. begin
  123. case NodeType of
  124. equaln:
  125. result:=F_FE;
  126. unequaln:
  127. result:=F_FNE;
  128. else
  129. begin
  130. if nf_swapped in Flags then
  131. case NodeType of
  132. ltn:
  133. result:=F_FG;
  134. lten:
  135. result:=F_FGE;
  136. gtn:
  137. result:=F_FL;
  138. gten:
  139. result:=F_FLE;
  140. else
  141. internalerror(2014082014);
  142. end
  143. else
  144. case NodeType of
  145. ltn:
  146. result:=F_FL;
  147. lten:
  148. result:=F_FLE;
  149. gtn:
  150. result:=F_FG;
  151. gten:
  152. result:=F_FGE;
  153. else
  154. internalerror(2014082015);
  155. end;
  156. end;
  157. end;
  158. end;
  159. procedure tsparcaddnode.second_addfloat;
  160. var
  161. op : TAsmOp;
  162. begin
  163. pass_left_right;
  164. if (nf_swapped in flags) then
  165. swapleftright;
  166. { force fpureg as location, left right doesn't matter
  167. as both will be in a fpureg }
  168. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  169. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,(left.location.loc<>LOC_CFPUREGISTER));
  170. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  171. if left.location.loc<>LOC_CFPUREGISTER then
  172. location.register:=left.location.register
  173. else
  174. location.register:=right.location.register;
  175. case nodetype of
  176. addn :
  177. begin
  178. if location.size=OS_F64 then
  179. op:=A_FADDd
  180. else
  181. op:=A_FADDs;
  182. end;
  183. muln :
  184. begin
  185. if location.size=OS_F64 then
  186. op:=A_FMULd
  187. else
  188. op:=A_FMULs;
  189. end;
  190. subn :
  191. begin
  192. if location.size=OS_F64 then
  193. op:=A_FSUBd
  194. else
  195. op:=A_FSUBs;
  196. end;
  197. slashn :
  198. begin
  199. if location.size=OS_F64 then
  200. op:=A_FDIVd
  201. else
  202. op:=A_FDIVs;
  203. end;
  204. else
  205. internalerror(200306014);
  206. end;
  207. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  208. left.location.register,right.location.register,location.register));
  209. end;
  210. procedure tsparcaddnode.second_cmpfloat;
  211. var
  212. op : tasmop;
  213. begin
  214. pass_left_right;
  215. if (nf_swapped in flags) then
  216. swapleftright;
  217. { force fpureg as location, left right doesn't matter
  218. as both will be in a fpureg }
  219. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,left.location,left.resultdef,true);
  220. hlcg.location_force_fpureg(current_asmdata.CurrAsmList,right.location,right.resultdef,true);
  221. location_reset(location,LOC_FLAGS,OS_NO);
  222. location.resflags:=getfpuresflags;
  223. if left.location.size=OS_F64 then
  224. op:=A_FCMPd
  225. else
  226. op:=A_FCMPs;
  227. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,
  228. left.location.register,right.location.register));
  229. { Delay slot (can only contain integer operation) }
  230. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  231. end;
  232. procedure tsparcaddnode.second_cmpboolean;
  233. begin
  234. pass_left_right;
  235. force_reg_left_right(true,true);
  236. if right.location.loc = LOC_CONSTANT then
  237. tcgsparcgen(cg).handle_reg_const_reg(current_asmdata.CurrAsmList,A_SUBcc,left.location.register,right.location.value,NR_G0)
  238. else
  239. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBcc,left.location.register,right.location.register,NR_G0));
  240. location_reset(location,LOC_FLAGS,OS_NO);
  241. location.resflags:=getresflags(true);
  242. end;
  243. procedure tsparcaddnode.second_cmpsmallset;
  244. var
  245. tmpreg : tregister;
  246. begin
  247. pass_left_right;
  248. location_reset(location,LOC_FLAGS,OS_NO);
  249. force_reg_left_right(false,false);
  250. case nodetype of
  251. equaln,
  252. unequaln:
  253. begin
  254. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBcc,left.location.register,right.location.register,NR_G0));
  255. location.resflags:=getresflags(true);
  256. end;
  257. lten,
  258. gten:
  259. begin
  260. if (not(nf_swapped in flags) and
  261. (nodetype = lten)) or
  262. ((nf_swapped in flags) and
  263. (nodetype = gten)) then
  264. swapleftright;
  265. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,left.location.size);
  266. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_AND,left.location.register,right.location.register,tmpreg));
  267. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBcc,tmpreg,right.location.register,NR_G0));
  268. location.resflags:=F_E;
  269. end;
  270. else
  271. internalerror(2012042701);
  272. end;
  273. end;
  274. procedure tsparcaddnode.second_add64bit;
  275. begin
  276. {$ifdef SPARC64}
  277. second_addordinal;
  278. {$else SPARC64}
  279. inherited second_add64bit;
  280. {$endif SPARC64}
  281. end;
  282. procedure tsparcaddnode.second_cmp64bit;
  283. {$ifdef SPARC64}
  284. begin
  285. second_cmpordinal;
  286. end;
  287. {$else SPARC64}
  288. var
  289. unsigned : boolean;
  290. hreg1,hreg2: tregister;
  291. procedure emit_compare(list:tasmlist; ls,rs:tnode);
  292. var
  293. lreg: tregister64;
  294. begin
  295. if (ls.location.loc=LOC_CONSTANT) then
  296. begin
  297. lreg.reghi:=NR_G0;
  298. lreg.reglo:=NR_G0;
  299. if lo(ls.location.value64)<>0 then
  300. begin
  301. lreg.reglo:=cg.GetIntRegister(list,OS_INT);
  302. cg.a_load_const_reg(list,OS_INT,lo(ls.location.value64),lreg.reglo);
  303. end;
  304. if hi(ls.location.value64)<>0 then
  305. begin
  306. lreg.reghi:=cg.GetIntRegister(list,OS_INT);
  307. cg.a_load_const_reg(list,OS_INT,hi(ls.location.value64),lreg.reghi);
  308. end;
  309. end
  310. else
  311. lreg:=ls.location.register64;
  312. if (rs.location.loc=LOC_CONSTANT) then
  313. begin
  314. tcgsparcgen(cg).handle_reg_const_reg(list,A_SUBcc,lreg.reglo,lo(rs.location.value64),NR_G0);
  315. tcgsparcgen(cg).handle_reg_const_reg(list,A_SUBXcc,lreg.reghi,hi(rs.location.value64),NR_G0);
  316. end
  317. else
  318. begin
  319. list.concat(taicpu.op_reg_reg_reg(A_SUBcc,lreg.reglo,rs.location.register64.reglo,NR_G0));
  320. list.concat(taicpu.op_reg_reg_reg(A_SUBXcc,lreg.reghi,rs.location.register64.reghi,NR_G0));
  321. end;
  322. end;
  323. begin
  324. pass_left_right;
  325. force_reg_left_right(true,true);
  326. unsigned:=not(is_signed(left.resultdef)) or
  327. not(is_signed(right.resultdef));
  328. location_reset(location,LOC_FLAGS,OS_NO);
  329. if (nodetype in [equaln,unequaln]) then
  330. begin
  331. location.resflags:=getresflags(unsigned);
  332. if (right.location.loc=LOC_CONSTANT) then
  333. begin
  334. if hi(right.location.value64)<>0 then
  335. begin
  336. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  337. tcgsparcgen(cg).handle_reg_const_reg(current_asmdata.CurrAsmList,A_XOR,left.location.register64.reghi,hi(right.location.value64),hreg1);
  338. end
  339. else
  340. hreg1:=left.location.register64.reghi;
  341. if lo(right.location.value64)<>0 then
  342. begin
  343. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  344. tcgsparcgen(cg).handle_reg_const_reg(current_asmdata.CurrAsmList,A_XOR,left.location.register64.reglo,lo(right.location.value64),hreg2);
  345. end
  346. else
  347. hreg2:=left.location.register64.reglo;
  348. end
  349. else
  350. begin
  351. hreg1:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  352. hreg2:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  353. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_XOR,left.location.register64.reghi,right.location.register64.reghi,hreg1));
  354. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_XOR,left.location.register64.reglo,right.location.register64.reglo,hreg2));
  355. end;
  356. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_ORcc,hreg1,hreg2,NR_G0));
  357. end
  358. else
  359. begin
  360. { undo possible swapped state }
  361. if (nf_swapped in flags) then
  362. swapleftright;
  363. { Subtracting sides sets N,V and C flags correctly, but not Z flag
  364. (which ends up depending only on upper dword). So don't use conditions
  365. that test Z flag:
  366. unsigned signed
  367. a < b => F_B F_L
  368. a >= b => F_AE F_GE
  369. a <= b => swap, F_AE F_GE
  370. a > b => swap, F_B F_L }
  371. if (nodetype in [ltn,gten]) then
  372. begin
  373. emit_compare(current_asmdata.CurrAsmList,left,right);
  374. location.resflags:=getresflags(unsigned);
  375. end
  376. else if (nodetype in [lten,gtn]) then
  377. begin
  378. emit_compare(current_asmdata.CurrAsmList,right,left);
  379. toggleflag(nf_swapped);
  380. location.resflags:=getresflags(unsigned);
  381. toggleflag(nf_swapped);
  382. end
  383. else
  384. InternalError(2014011001);
  385. end;
  386. end;
  387. {$endif SPARC64}
  388. procedure tsparcaddnode.second_cmpordinal;
  389. var
  390. unsigned : boolean;
  391. begin
  392. pass_left_right;
  393. force_reg_left_right(true,true);
  394. unsigned:=not(is_signed(left.resultdef)) or
  395. not(is_signed(right.resultdef));
  396. if right.location.loc = LOC_CONSTANT then
  397. tcgsparcgen(cg).handle_reg_const_reg(current_asmdata.CurrAsmList,A_SUBcc,left.location.register,right.location.value,NR_G0)
  398. else
  399. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBcc,left.location.register,right.location.register,NR_G0));
  400. location_reset(location,LOC_FLAGS,OS_NO);
  401. location.resflags:=getresflags(unsigned);
  402. end;
  403. const
  404. multops: array[boolean] of TAsmOp = (A_SMUL, A_UMUL);
  405. procedure tsparcaddnode.second_addordinal;
  406. var
  407. unsigned: boolean;
  408. begin
  409. unsigned:=not(is_signed(left.resultdef)) or
  410. not(is_signed(right.resultdef));
  411. if (nodetype=muln) and is_64bit(resultdef) then
  412. begin
  413. pass_left_right;
  414. force_reg_left_right(true,false);
  415. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  416. {$ifdef SPARC64}
  417. location.register:=cg.getintregister(current_asmdata.CurrAsmList,location.size);
  418. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(multops[unsigned],left.location.register,right.location.register,location.register));
  419. {$else SPARC64}
  420. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  421. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  422. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(multops[unsigned],left.location.register,right.location.register,location.register64.reglo));
  423. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg(A_MOV,NR_Y,location.register64.reghi));
  424. {$endif SPARC64}
  425. end
  426. else
  427. inherited second_addordinal;
  428. end;
  429. function tsparcaddnode.use_generic_mul32to64: boolean;
  430. begin
  431. result:=false;
  432. end;
  433. function tsparcaddnode.pass_1: tnode;
  434. begin
  435. result:=inherited pass_1;
  436. if not assigned(result) then
  437. begin
  438. if is_64bitint(left.resultdef) and
  439. (nodetype in [equaln,unequaln,ltn,gtn,lten,gten]) then
  440. expectloc:=LOC_FLAGS;
  441. end;
  442. end;
  443. begin
  444. caddnode:=tsparcaddnode;
  445. end.