ncpumat.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Generate SPARC assembler for math nodes
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit ncpumat;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node,nmat,ncgmat;
  23. type
  24. tSparcmoddivnode = class(tmoddivnode)
  25. procedure pass_2;override;
  26. end;
  27. tSparcshlshrnode = class(tshlshrnode)
  28. procedure pass_2;override;
  29. { everything will be handled in pass_2 }
  30. function first_shlshr64bitint: tnode; override;
  31. end;
  32. tSparcnotnode = class(tcgnotnode)
  33. procedure second_boolean;override;
  34. end;
  35. implementation
  36. uses
  37. globtype,systems,
  38. cutils,verbose,globals,
  39. symconst,
  40. aasmbase,aasmcpu,aasmtai,
  41. defutil,
  42. cgbase,cgobj,pass_2,
  43. ncon,
  44. cpubase,
  45. ncgutil,cgcpu;
  46. {*****************************************************************************
  47. TSparcMODDIVNODE
  48. *****************************************************************************}
  49. procedure tSparcmoddivnode.pass_2;
  50. const
  51. { signed overflow }
  52. divops: array[boolean, boolean] of tasmop =
  53. ((A_UDIV,A_UDIVcc),(A_SDIV,A_SDIVcc));
  54. var
  55. power : longint;
  56. op : tasmop;
  57. tmpreg,
  58. numerator,
  59. divider,
  60. resultreg : tregister;
  61. overflowlabel : tasmlabel;
  62. ai : taicpu;
  63. begin
  64. secondpass(left);
  65. secondpass(right);
  66. location_copy(location,left.location);
  67. { put numerator in register }
  68. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  69. location_copy(location,left.location);
  70. numerator := location.register;
  71. if (nodetype = modn) then
  72. resultreg := cg.GetIntRegister(exprasmlist,OS_INT)
  73. else
  74. begin
  75. if (location.loc = LOC_CREGISTER) then
  76. begin
  77. location.loc := LOC_REGISTER;
  78. location.register := cg.GetIntRegister(exprasmlist,OS_INT);
  79. end;
  80. resultreg := location.register;
  81. end;
  82. if (nodetype = divn) and
  83. (right.nodetype = ordconstn) and
  84. ispowerof2(tordconstnode(right).value,power) then
  85. begin
  86. tmpreg:=cg.GetIntRegister(exprasmlist,OS_INT);
  87. cg.a_op_const_reg_reg(exprasmlist,OP_SAR,OS_INT,31,numerator,tmpreg);
  88. { if signed, tmpreg=right value-1, otherwise 0 }
  89. cg.a_op_const_reg(exprasmlist,OP_AND,OS_INT,tordconstnode(right).value-1,tmpreg);
  90. { add to the left value }
  91. cg.a_op_reg_reg(exprasmlist,OP_ADD,OS_INT,tmpreg,numerator);
  92. cg.UngetRegister(exprasmlist,tmpreg);
  93. cg.a_op_const_reg_reg(exprasmlist,OP_SAR,OS_INT,aword(power),numerator,resultreg);
  94. end
  95. else
  96. begin
  97. { load divider in a register if necessary }
  98. location_force_reg(exprasmlist,right.location,
  99. def_cgsize(right.resulttype.def),true);
  100. divider := right.location.register;
  101. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  102. { And on Sparc, the only way to catch a div-by-0 is by checking }
  103. { the overflow flag (JM) }
  104. { Fill %y with the -1 or 0 depending on the highest bit }
  105. if is_signed(left.resulttype.def) then
  106. begin
  107. tmpreg:=cg.GetIntRegister(exprasmlist,OS_INT);
  108. exprasmlist.concat(taicpu.op_reg_const_reg(A_SRA,numerator,31,tmpreg));
  109. exprasmlist.concat(taicpu.op_reg_reg(A_MOV,tmpreg,NR_Y));
  110. end
  111. else
  112. exprasmlist.concat(taicpu.op_reg_reg(A_MOV,NR_G0,NR_Y));
  113. { wait 3 instructions slots before we can read %y }
  114. exprasmlist.concat(taicpu.op_none(A_NOP));
  115. exprasmlist.concat(taicpu.op_none(A_NOP));
  116. exprasmlist.concat(taicpu.op_none(A_NOP));
  117. op := divops[is_signed(right.resulttype.def),
  118. cs_check_overflow in aktlocalswitches];
  119. exprasmlist.concat(taicpu.op_reg_reg_reg(op,numerator,divider,resultreg));
  120. if (nodetype = modn) then
  121. begin
  122. objectlibrary.getlabel(overflowlabel);
  123. ai:=taicpu.op_cond_sym(A_Bxx,C_O,overflowlabel);
  124. ai.delayslot_annulled:=true;
  125. exprasmlist.concat(ai);
  126. exprasmlist.concat(taicpu.op_reg(A_NOT,resultreg));
  127. cg.a_label(exprasmlist,overflowlabel);
  128. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SMUL,resultreg,divider,resultreg));
  129. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,numerator,resultreg,resultreg));
  130. end
  131. else
  132. cg.UngetRegister(exprasmlist,divider);
  133. end;
  134. { free used registers }
  135. if numerator<>resultreg then
  136. cg.UngetRegister(exprasmlist,numerator);
  137. { set result location }
  138. location.loc:=LOC_REGISTER;
  139. location.register:=resultreg;
  140. cg.g_overflowcheck(exprasmlist,Location,ResultType.Def);
  141. end;
  142. {*****************************************************************************
  143. TSparcSHLRSHRNODE
  144. *****************************************************************************}
  145. function TSparcShlShrNode.first_shlshr64bitint:TNode;
  146. begin
  147. { 64bit without constants need a helper }
  148. if is_64bit(left.resulttype.def) and
  149. (right.nodetype<>ordconstn) then
  150. begin
  151. result:=inherited first_shlshr64bitint;
  152. exit;
  153. end;
  154. result := nil;
  155. end;
  156. procedure tSparcshlshrnode.pass_2;
  157. var
  158. hregister,resultreg,hregister1,
  159. hregisterhigh,hregisterlow : tregister;
  160. op : topcg;
  161. shiftval: aword;
  162. begin
  163. { 64bit without constants need a helper, and is
  164. already replaced in pass1 }
  165. if is_64bit(left.resulttype.def) and
  166. (right.nodetype<>ordconstn) then
  167. internalerror(200405301);
  168. secondpass(left);
  169. secondpass(right);
  170. if is_64bit(left.resulttype.def) then
  171. begin
  172. location_reset(location,LOC_REGISTER,OS_64);
  173. { load left operator in a register }
  174. location_force_reg(exprasmlist,left.location,OS_64,false);
  175. hregisterhigh:=left.location.registerhigh;
  176. hregisterlow:=left.location.registerlow;
  177. shiftval := tordconstnode(right).value and 63;
  178. if shiftval > 31 then
  179. begin
  180. if nodetype = shln then
  181. begin
  182. if (shiftval and 31) <> 0 then
  183. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,shiftval and 31,hregisterlow,hregisterhigh);
  184. cg.a_load_const_reg(exprasmlist,OS_32,0,hregisterlow);
  185. end
  186. else
  187. begin
  188. if (shiftval and 31) <> 0 then
  189. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,shiftval and 31,hregisterhigh,hregisterlow);
  190. cg.a_load_const_reg(exprasmlist,OS_32,0,hregisterhigh);
  191. end;
  192. location.registerhigh:=hregisterlow;
  193. location.registerlow:=hregisterhigh;
  194. end
  195. else
  196. begin
  197. hregister:=cg.getintregister(exprasmlist,OS_32);
  198. if nodetype = shln then
  199. begin
  200. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,32-shiftval,hregisterlow,hregister);
  201. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,shiftval,hregisterhigh,hregisterhigh);
  202. cg.a_op_reg_reg_reg(exprasmlist,OP_OR,OS_32,hregister,hregisterhigh,hregisterhigh);
  203. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,shiftval,hregisterlow,hregisterlow);
  204. end
  205. else
  206. begin
  207. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,32-shiftval,hregisterhigh,hregister);
  208. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,shiftval,hregisterlow,hregisterlow);
  209. cg.a_op_reg_reg_reg(exprasmlist,OP_OR,OS_32,hregister,hregisterlow,hregisterlow);
  210. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,shiftval,hregisterhigh,hregisterhigh);
  211. end;
  212. location.registerhigh:=hregisterhigh;
  213. location.registerlow:=hregisterlow;
  214. end;
  215. end
  216. else
  217. begin
  218. { load left operators in a register }
  219. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  220. location_copy(location,left.location);
  221. resultreg := location.register;
  222. hregister1 := location.register;
  223. if (location.loc = LOC_CREGISTER) then
  224. begin
  225. location.loc := LOC_REGISTER;
  226. resultreg := cg.GetIntRegister(exprasmlist,OS_INT);
  227. location.register := resultreg;
  228. end;
  229. { determine operator }
  230. if nodetype=shln then
  231. op:=OP_SHL
  232. else
  233. op:=OP_SHR;
  234. { shifting by a constant directly coded: }
  235. if (right.nodetype=ordconstn) then
  236. begin
  237. if tordconstnode(right).value and 31<>0 then
  238. cg.a_op_const_reg_reg(exprasmlist,op,OS_32,tordconstnode(right).value and 31,hregister1,resultreg)
  239. end
  240. else
  241. begin
  242. { load shift count in a register if necessary }
  243. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  244. cg.a_op_reg_reg_reg(exprasmlist,op,OS_32,right.location.register,hregister1,resultreg);
  245. end;
  246. end;
  247. end;
  248. {*****************************************************************************
  249. TSPARCNOTNODE
  250. *****************************************************************************}
  251. procedure tsparcnotnode.second_boolean;
  252. var
  253. hl : tasmlabel;
  254. begin
  255. { if the location is LOC_JUMP, we do the secondpass after the
  256. labels are allocated
  257. }
  258. if left.expectloc=LOC_JUMP then
  259. begin
  260. hl:=truelabel;
  261. truelabel:=falselabel;
  262. falselabel:=hl;
  263. secondpass(left);
  264. maketojumpbool(exprasmlist,left,lr_load_regvars);
  265. hl:=truelabel;
  266. truelabel:=falselabel;
  267. falselabel:=hl;
  268. location.loc:=LOC_JUMP;
  269. end
  270. else
  271. begin
  272. secondpass(left);
  273. case left.location.loc of
  274. LOC_FLAGS :
  275. begin
  276. location_copy(location,left.location);
  277. inverse_flags(location.resflags);
  278. end;
  279. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE :
  280. begin
  281. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  282. exprasmlist.concat(taicpu.op_reg_const_reg(A_SUBcc,left.location.register,0,NR_G0));
  283. location_release(exprasmlist,left.location);
  284. location_reset(location,LOC_FLAGS,OS_NO);
  285. location.resflags:=F_E;
  286. end;
  287. else
  288. internalerror(2003042401);
  289. end;
  290. end;
  291. end;
  292. begin
  293. cmoddivnode:=tSparcmoddivnode;
  294. cshlshrnode:=tSparcshlshrnode;
  295. cnotnode:=tSparcnotnode;
  296. end.
  297. {
  298. $Log$
  299. Revision 1.18 2004-06-20 08:55:32 florian
  300. * logs truncated
  301. Revision 1.17 2004/06/16 20:07:11 florian
  302. * dwarf branch merged
  303. Revision 1.16.2.3 2004/05/30 17:07:08 peter
  304. * fix shl shr for sparc
  305. Revision 1.16.2.2 2004/05/30 13:45:36 florian
  306. * fixed unsigned division
  307. Revision 1.16.2.1 2004/05/27 23:35:12 peter
  308. * div fixed
  309. }