ncpuadd.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. begin
  228. pass_left_right;
  229. force_reg_left_right(true,true);
  230. if right.location.loc = LOC_CONSTANT then
  231. tcgsparc(cg).handle_reg_const_reg(current_asmdata.CurrAsmList,A_SUBcc,left.location.register,right.location.value,NR_G0)
  232. else
  233. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBcc,left.location.register,right.location.register,NR_G0));
  234. location_reset(location,LOC_FLAGS,OS_NO);
  235. location.resflags:=getresflags(true);
  236. end;
  237. procedure tsparcaddnode.second_cmp64bit;
  238. var
  239. unsigned : boolean;
  240. procedure firstjmp64bitcmp;
  241. var
  242. oldnodetype : tnodetype;
  243. begin
  244. { the jump the sequence is a little bit hairy }
  245. case nodetype of
  246. ltn,gtn:
  247. begin
  248. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  249. { cheat a little bit for the negative test }
  250. toggleflag(nf_swapped);
  251. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  252. toggleflag(nf_swapped);
  253. end;
  254. lten,gten:
  255. begin
  256. oldnodetype:=nodetype;
  257. if nodetype=lten then
  258. nodetype:=ltn
  259. else
  260. nodetype:=gtn;
  261. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrTrueLabel);
  262. { cheat for the negative test }
  263. if nodetype=ltn then
  264. nodetype:=gtn
  265. else
  266. nodetype:=ltn;
  267. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(unsigned),current_procinfo.CurrFalseLabel);
  268. nodetype:=oldnodetype;
  269. end;
  270. equaln:
  271. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  272. unequaln:
  273. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  274. end;
  275. end;
  276. procedure secondjmp64bitcmp;
  277. begin
  278. { the jump the sequence is a little bit hairy }
  279. case nodetype of
  280. ltn,gtn,lten,gten:
  281. begin
  282. { the comparisaion of the low dword have to be }
  283. { always unsigned! }
  284. cg.a_jmp_flags(current_asmdata.CurrAsmList,getresflags(true),current_procinfo.CurrTrueLabel);
  285. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  286. end;
  287. equaln:
  288. begin
  289. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrFalseLabel);
  290. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrTrueLabel);
  291. end;
  292. unequaln:
  293. begin
  294. cg.a_jmp_flags(current_asmdata.CurrAsmList,F_NE,current_procinfo.CurrTrueLabel);
  295. cg.a_jmp_always(current_asmdata.CurrAsmList,current_procinfo.CurrFalseLabel);
  296. end;
  297. end;
  298. end;
  299. begin
  300. pass_left_right;
  301. force_reg_left_right(false,false);
  302. unsigned:=not(is_signed(left.resultdef)) or
  303. not(is_signed(right.resultdef));
  304. location_reset(location,LOC_JUMP,OS_NO);
  305. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reghi,right.location.register64.reghi));
  306. firstjmp64bitcmp;
  307. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg(A_CMP,left.location.register64.reglo,right.location.register64.reglo));
  308. secondjmp64bitcmp;
  309. end;
  310. procedure tsparcaddnode.second_cmpordinal;
  311. var
  312. unsigned : boolean;
  313. begin
  314. pass_left_right;
  315. force_reg_left_right(true,true);
  316. unsigned:=not(is_signed(left.resultdef)) or
  317. not(is_signed(right.resultdef));
  318. if right.location.loc = LOC_CONSTANT then
  319. tcgsparc(cg).handle_reg_const_reg(current_asmdata.CurrAsmList,A_SUBcc,left.location.register,right.location.value,NR_G0)
  320. else
  321. current_asmdata.CurrAsmList.concat(taicpu.op_reg_reg_reg(A_SUBcc,left.location.register,right.location.register,NR_G0));
  322. location_reset(location,LOC_FLAGS,OS_NO);
  323. location.resflags:=getresflags(unsigned);
  324. end;
  325. begin
  326. caddnode:=tsparcaddnode;
  327. end.