ncpumat.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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,cgutils;
  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.a_op_const_reg_reg(exprasmlist,OP_SAR,OS_INT,aword(power),numerator,resultreg);
  93. end
  94. else
  95. begin
  96. { load divider in a register if necessary }
  97. location_force_reg(exprasmlist,right.location,
  98. def_cgsize(right.resulttype.def),true);
  99. divider := right.location.register;
  100. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  101. { And on Sparc, the only way to catch a div-by-0 is by checking }
  102. { the overflow flag (JM) }
  103. { Fill %y with the -1 or 0 depending on the highest bit }
  104. if is_signed(left.resulttype.def) then
  105. begin
  106. tmpreg:=cg.GetIntRegister(exprasmlist,OS_INT);
  107. exprasmlist.concat(taicpu.op_reg_const_reg(A_SRA,numerator,31,tmpreg));
  108. exprasmlist.concat(taicpu.op_reg_reg(A_MOV,tmpreg,NR_Y));
  109. end
  110. else
  111. exprasmlist.concat(taicpu.op_reg_reg(A_MOV,NR_G0,NR_Y));
  112. { wait 3 instructions slots before we can read %y }
  113. exprasmlist.concat(taicpu.op_none(A_NOP));
  114. exprasmlist.concat(taicpu.op_none(A_NOP));
  115. exprasmlist.concat(taicpu.op_none(A_NOP));
  116. op := divops[is_signed(right.resulttype.def),
  117. cs_check_overflow in aktlocalswitches];
  118. exprasmlist.concat(taicpu.op_reg_reg_reg(op,numerator,divider,resultreg));
  119. if (nodetype = modn) then
  120. begin
  121. objectlibrary.getlabel(overflowlabel);
  122. ai:=taicpu.op_cond_sym(A_Bxx,C_O,overflowlabel);
  123. ai.delayslot_annulled:=true;
  124. exprasmlist.concat(ai);
  125. exprasmlist.concat(taicpu.op_reg(A_NOT,resultreg));
  126. cg.a_label(exprasmlist,overflowlabel);
  127. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SMUL,resultreg,divider,resultreg));
  128. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,numerator,resultreg,resultreg));
  129. end;
  130. end;
  131. { set result location }
  132. location.loc:=LOC_REGISTER;
  133. location.register:=resultreg;
  134. cg.g_overflowcheck(exprasmlist,Location,ResultType.Def);
  135. end;
  136. {*****************************************************************************
  137. TSparcSHLRSHRNODE
  138. *****************************************************************************}
  139. function TSparcShlShrNode.first_shlshr64bitint:TNode;
  140. begin
  141. { 64bit without constants need a helper }
  142. if is_64bit(left.resulttype.def) and
  143. (right.nodetype<>ordconstn) then
  144. begin
  145. result:=inherited first_shlshr64bitint;
  146. exit;
  147. end;
  148. result := nil;
  149. end;
  150. procedure tSparcshlshrnode.pass_2;
  151. var
  152. hregister,resultreg,hregister1,
  153. hreg64hi,hreg64lo : tregister;
  154. op : topcg;
  155. shiftval: aword;
  156. begin
  157. { 64bit without constants need a helper, and is
  158. already replaced in pass1 }
  159. if is_64bit(left.resulttype.def) and
  160. (right.nodetype<>ordconstn) then
  161. internalerror(200405301);
  162. secondpass(left);
  163. secondpass(right);
  164. if is_64bit(left.resulttype.def) then
  165. begin
  166. location_reset(location,LOC_REGISTER,OS_64);
  167. { load left operator in a register }
  168. location_force_reg(exprasmlist,left.location,OS_64,false);
  169. hreg64hi:=left.location.register64.reghi;
  170. hreg64lo:=left.location.register64.reglo;
  171. shiftval := tordconstnode(right).value and 63;
  172. if shiftval > 31 then
  173. begin
  174. if nodetype = shln then
  175. begin
  176. cg.a_load_const_reg(exprasmlist,OS_32,0,hreg64hi);
  177. if (shiftval and 31) <> 0 then
  178. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,shiftval and 31,hreg64lo,hreg64lo);
  179. end
  180. else
  181. begin
  182. cg.a_load_const_reg(exprasmlist,OS_32,0,hreg64lo);
  183. if (shiftval and 31) <> 0 then
  184. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,shiftval and 31,hreg64hi,hreg64hi);
  185. end;
  186. location.register64.reglo:=hreg64hi;
  187. location.register64.reghi:=hreg64lo;
  188. end
  189. else
  190. begin
  191. hregister:=cg.getintregister(exprasmlist,OS_32);
  192. if nodetype = shln then
  193. begin
  194. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,32-shiftval,hreg64lo,hregister);
  195. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,shiftval,hreg64hi,hreg64hi);
  196. cg.a_op_reg_reg_reg(exprasmlist,OP_OR,OS_32,hregister,hreg64hi,hreg64hi);
  197. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,shiftval,hreg64lo,hreg64lo);
  198. end
  199. else
  200. begin
  201. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,32-shiftval,hreg64hi,hregister);
  202. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,shiftval,hreg64lo,hreg64lo);
  203. cg.a_op_reg_reg_reg(exprasmlist,OP_OR,OS_32,hregister,hreg64lo,hreg64lo);
  204. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,shiftval,hreg64hi,hreg64hi);
  205. end;
  206. location.register64.reghi:=hreg64hi;
  207. location.register64.reglo:=hreg64lo;
  208. end;
  209. end
  210. else
  211. begin
  212. { load left operators in a register }
  213. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  214. location_copy(location,left.location);
  215. resultreg := location.register;
  216. hregister1 := location.register;
  217. if (location.loc = LOC_CREGISTER) then
  218. begin
  219. location.loc := LOC_REGISTER;
  220. resultreg := cg.GetIntRegister(exprasmlist,OS_INT);
  221. location.register := resultreg;
  222. end;
  223. { determine operator }
  224. if nodetype=shln then
  225. op:=OP_SHL
  226. else
  227. op:=OP_SHR;
  228. { shifting by a constant directly coded: }
  229. if (right.nodetype=ordconstn) then
  230. begin
  231. if tordconstnode(right).value and 31<>0 then
  232. cg.a_op_const_reg_reg(exprasmlist,op,OS_32,tordconstnode(right).value and 31,hregister1,resultreg)
  233. end
  234. else
  235. begin
  236. { load shift count in a register if necessary }
  237. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  238. cg.a_op_reg_reg_reg(exprasmlist,op,OS_32,right.location.register,hregister1,resultreg);
  239. end;
  240. end;
  241. end;
  242. {*****************************************************************************
  243. TSPARCNOTNODE
  244. *****************************************************************************}
  245. procedure tsparcnotnode.second_boolean;
  246. var
  247. hl : tasmlabel;
  248. begin
  249. { if the location is LOC_JUMP, we do the secondpass after the
  250. labels are allocated
  251. }
  252. if left.expectloc=LOC_JUMP then
  253. begin
  254. hl:=truelabel;
  255. truelabel:=falselabel;
  256. falselabel:=hl;
  257. secondpass(left);
  258. maketojumpbool(exprasmlist,left,lr_load_regvars);
  259. hl:=truelabel;
  260. truelabel:=falselabel;
  261. falselabel:=hl;
  262. location.loc:=LOC_JUMP;
  263. end
  264. else
  265. begin
  266. secondpass(left);
  267. case left.location.loc of
  268. LOC_FLAGS :
  269. begin
  270. location_copy(location,left.location);
  271. inverse_flags(location.resflags);
  272. end;
  273. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE :
  274. begin
  275. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  276. exprasmlist.concat(taicpu.op_reg_const_reg(A_SUBcc,left.location.register,0,NR_G0));
  277. location_reset(location,LOC_FLAGS,OS_NO);
  278. location.resflags:=F_E;
  279. end;
  280. else
  281. internalerror(2003042401);
  282. end;
  283. end;
  284. end;
  285. begin
  286. cmoddivnode:=tSparcmoddivnode;
  287. cshlshrnode:=tSparcshlshrnode;
  288. cnotnode:=tSparcnotnode;
  289. end.
  290. {
  291. $Log$
  292. Revision 1.23 2005-02-14 17:13:10 peter
  293. * truncate log
  294. }