ncpuadd.pas 17 KB

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