ncpuadd.pas 16 KB

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