ncpuadd.pas 18 KB

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