ncpuadd.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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 use_generic_mul32to64: boolean; override;
  37. end;
  38. implementation
  39. uses
  40. systems,
  41. cutils,verbose,
  42. paramgr,procinfo,
  43. aasmtai,aasmdata,aasmcpu,defutil,
  44. cgbase,cgcpu,cgutils,
  45. cpupara,
  46. ncon,nset,nadd,
  47. ncgutil,cgobj;
  48. {*****************************************************************************
  49. TSparcAddNode
  50. *****************************************************************************}
  51. function TSparcAddNode.GetResFlags(unsigned:Boolean):TResFlags;
  52. begin
  53. case NodeType of
  54. equaln:
  55. GetResFlags:=F_E;
  56. unequaln:
  57. GetResFlags:=F_NE;
  58. else
  59. if not(unsigned) then
  60. begin
  61. if nf_swapped in flags then
  62. case NodeType of
  63. ltn:
  64. GetResFlags:=F_G;
  65. lten:
  66. GetResFlags:=F_GE;
  67. gtn:
  68. GetResFlags:=F_L;
  69. gten:
  70. GetResFlags:=F_LE;
  71. end
  72. else
  73. case NodeType of
  74. ltn:
  75. GetResFlags:=F_L;
  76. lten:
  77. GetResFlags:=F_LE;
  78. gtn:
  79. GetResFlags:=F_G;
  80. gten:
  81. GetResFlags:=F_GE;
  82. end;
  83. end
  84. else
  85. begin
  86. if nf_swapped in Flags then
  87. case NodeType of
  88. ltn:
  89. GetResFlags:=F_A;
  90. lten:
  91. GetResFlags:=F_AE;
  92. gtn:
  93. GetResFlags:=F_B;
  94. gten:
  95. GetResFlags:=F_BE;
  96. end
  97. else
  98. case NodeType of
  99. ltn:
  100. GetResFlags:=F_B;
  101. lten:
  102. GetResFlags:=F_BE;
  103. gtn:
  104. GetResFlags:=F_A;
  105. gten:
  106. GetResFlags:=F_AE;
  107. end;
  108. end;
  109. end;
  110. end;
  111. function TSparcAddNode.GetFPUResFlags:TResFlags;
  112. begin
  113. case NodeType of
  114. equaln:
  115. result:=F_FE;
  116. unequaln:
  117. result:=F_FNE;
  118. else
  119. begin
  120. if nf_swapped in Flags then
  121. case NodeType of
  122. ltn:
  123. result:=F_FG;
  124. lten:
  125. result:=F_FGE;
  126. gtn:
  127. result:=F_FL;
  128. gten:
  129. result:=F_FLE;
  130. end
  131. else
  132. case NodeType of
  133. ltn:
  134. result:=F_FL;
  135. lten:
  136. result:=F_FLE;
  137. gtn:
  138. result:=F_FG;
  139. gten:
  140. result:=F_FGE;
  141. end;
  142. end;
  143. end;
  144. end;
  145. procedure tsparcaddnode.second_addfloat;
  146. var
  147. op : TAsmOp;
  148. begin
  149. pass_left_right;
  150. if (nf_swapped in flags) then
  151. swapleftright;
  152. { force fpureg as location, left right doesn't matter
  153. as both will be in a fpureg }
  154. location_force_fpureg(current_asmdata.CurrAsmList,left.location,true);
  155. location_force_fpureg(current_asmdata.CurrAsmList,right.location,(left.location.loc<>LOC_CFPUREGISTER));
  156. location_reset(location,LOC_FPUREGISTER,def_cgsize(resultdef));
  157. if left.location.loc<>LOC_CFPUREGISTER then
  158. location.register:=left.location.register
  159. else
  160. location.register:=right.location.register;
  161. case nodetype of
  162. addn :
  163. begin
  164. if location.size=OS_F64 then
  165. op:=A_FADDd
  166. else
  167. op:=A_FADDs;
  168. end;
  169. muln :
  170. begin
  171. if location.size=OS_F64 then
  172. op:=A_FMULd
  173. else
  174. op:=A_FMULs;
  175. end;
  176. subn :
  177. begin
  178. if location.size=OS_F64 then
  179. op:=A_FSUBd
  180. else
  181. op:=A_FSUBs;
  182. end;
  183. slashn :
  184. begin
  185. if location.size=OS_F64 then
  186. op:=A_FDIVd
  187. else
  188. op:=A_FDIVs;
  189. end;
  190. else
  191. internalerror(200306014);
  192. end;
  193. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(op,
  194. left.location.register,right.location.register,location.register));
  195. end;
  196. procedure tsparcaddnode.second_cmpfloat;
  197. var
  198. op : tasmop;
  199. begin
  200. pass_left_right;
  201. if (nf_swapped in flags) then
  202. swapleftright;
  203. { force fpureg as location, left right doesn't matter
  204. as both will be in a fpureg }
  205. location_force_fpureg(current_asmdata.CurrAsmList,left.location,true);
  206. location_force_fpureg(current_asmdata.CurrAsmList,right.location,true);
  207. location_reset(location,LOC_FLAGS,OS_NO);
  208. location.resflags:=getfpuresflags;
  209. if left.location.size=OS_F64 then
  210. op:=A_FCMPd
  211. else
  212. op:=A_FCMPs;
  213. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(op,
  214. left.location.register,right.location.register));
  215. { Delay slot (can only contain integer operation) }
  216. current_asmdata.CurrAsmList.concat(taicpu.op_none(A_NOP));
  217. end;
  218. procedure tsparcaddnode.second_cmpboolean;
  219. begin
  220. pass_left_right;
  221. force_reg_left_right(true,true);
  222. if right.location.loc = LOC_CONSTANT then
  223. tcgsparc(cg).handle_reg_const_reg(current_asmdata.CurrAsmList,A_SUBcc,left.location.register,right.location.value,NR_G0)
  224. else
  225. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBcc,left.location.register,right.location.register,NR_G0));
  226. location_reset(location,LOC_FLAGS,OS_NO);
  227. location.resflags:=getresflags(true);
  228. end;
  229. procedure tsparcaddnode.second_cmpsmallset;
  230. var
  231. tmpreg : tregister;
  232. begin
  233. pass_left_right;
  234. location_reset(location,LOC_FLAGS,OS_NO);
  235. force_reg_left_right(false,false);
  236. case nodetype of
  237. equaln,
  238. unequaln:
  239. begin
  240. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBcc,left.location.register,right.location.register,NR_G0));
  241. location.resflags:=getresflags(true);
  242. end;
  243. lten,
  244. gten:
  245. begin
  246. if (not(nf_swapped in flags) and
  247. (nodetype = lten)) or
  248. ((nf_swapped in flags) and
  249. (nodetype = gten)) then
  250. swapleftright;
  251. tmpreg:=cg.getintregister(current_asmdata.CurrAsmList,left.location.size);
  252. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_AND,left.location.register,right.location.register,tmpreg));
  253. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBcc,tmpreg,right.location.register,NR_G0));
  254. location.resflags:=F_E;
  255. end;
  256. else
  257. internalerror(2012042701);
  258. end;
  259. end;
  260. procedure tsparcaddnode.second_cmp64bit;
  261. var
  262. unsigned : boolean;
  263. procedure firstjmp64bitcmp;
  264. var
  265. oldnodetype : tnodetype;
  266. begin
  267. { the jump the sequence is a little bit hairy }
  268. case nodetype of
  269. ltn,gtn:
  270. begin
  271. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  272. { cheat a little bit for the negative test }
  273. toggleflag(nf_swapped);
  274. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  275. toggleflag(nf_swapped);
  276. end;
  277. lten,gten:
  278. begin
  279. oldnodetype:=nodetype;
  280. if nodetype=lten then
  281. nodetype:=ltn
  282. else
  283. nodetype:=gtn;
  284. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  285. { cheat for the negative test }
  286. if nodetype=ltn then
  287. nodetype:=gtn
  288. else
  289. nodetype:=ltn;
  290. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  291. nodetype:=oldnodetype;
  292. end;
  293. equaln:
  294. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  295. unequaln:
  296. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  297. end;
  298. end;
  299. procedure secondjmp64bitcmp;
  300. begin
  301. { the jump the sequence is a little bit hairy }
  302. case nodetype of
  303. ltn,gtn,lten,gten:
  304. begin
  305. { the comparisaion of the low dword have to be }
  306. { always unsigned! }
  307. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  308. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  309. end;
  310. equaln:
  311. begin
  312. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  313. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  314. end;
  315. unequaln:
  316. begin
  317. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  318. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  319. end;
  320. end;
  321. end;
  322. begin
  323. pass_left_right;
  324. force_reg_left_right(true,true);
  325. unsigned:=not(is_signed(left.resultdef)) or
  326. not(is_signed(right.resultdef));
  327. location_reset(location,LOC_JUMP,OS_NO);
  328. if (right.location.loc<>LOC_CONSTANT) then
  329. begin
  330. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi));
  331. firstjmp64bitcmp;
  332. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo));
  333. secondjmp64bitcmp;
  334. end
  335. else
  336. begin
  337. tcgsparc(cg).handle_reg_const_reg(current_asmdata.CurrAsmList,A_SUBcc,left.location.register64.reghi,hi(right.location.value64),NR_G0);
  338. firstjmp64bitcmp;
  339. tcgsparc(cg).handle_reg_const_reg(current_asmdata.CurrAsmList,A_SUBcc,left.location.register64.reglo,lo(right.location.value64),NR_G0);
  340. secondjmp64bitcmp;
  341. end;
  342. end;
  343. procedure tsparcaddnode.second_cmpordinal;
  344. var
  345. unsigned : boolean;
  346. begin
  347. pass_left_right;
  348. force_reg_left_right(true,true);
  349. unsigned:=not(is_signed(left.resultdef)) or
  350. not(is_signed(right.resultdef));
  351. if right.location.loc = LOC_CONSTANT then
  352. tcgsparc(cg).handle_reg_const_reg(current_asmdata.CurrAsmList,A_SUBcc,left.location.register,right.location.value,NR_G0)
  353. else
  354. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBcc,left.location.register,right.location.register,NR_G0));
  355. location_reset(location,LOC_FLAGS,OS_NO);
  356. location.resflags:=getresflags(unsigned);
  357. end;
  358. const
  359. multops: array[boolean] of TAsmOp = (A_SMUL, A_UMUL);
  360. procedure tsparcaddnode.second_addordinal;
  361. var
  362. unsigned: boolean;
  363. begin
  364. unsigned:=not(is_signed(left.resultdef)) or
  365. not(is_signed(right.resultdef));
  366. if (nodetype=muln) and is_64bit(resultdef) then
  367. begin
  368. pass_left_right;
  369. force_reg_left_right(true,false);
  370. location_reset(location,LOC_REGISTER,def_cgsize(resultdef));
  371. location.register64.reglo:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  372. location.register64.reghi:=cg.getintregister(current_asmdata.CurrAsmList,OS_INT);
  373. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg_reg(multops[unsigned],left.location.register,right.location.register,location.register64.reglo));
  374. current_asmdata.CurrAsmList.Concat(taicpu.op_reg_reg(A_MOV,NR_Y,location.register64.reghi));
  375. end
  376. else
  377. inherited second_addordinal;
  378. end;
  379. function tsparcaddnode.use_generic_mul32to64: boolean;
  380. begin
  381. result:=false;
  382. end;
  383. begin
  384. caddnode:=tsparcaddnode;
  385. end.