ncpuadd.pas 16 KB

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