2
0

ncpuadd.pas 16 KB

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