ncpuadd.pas 13 KB

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