ncpumat.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Generate SPARC assembler for math nodes
  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 ncpumat;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. node,nmat,ncgmat;
  22. type
  23. tSparcmoddivnode = class(tmoddivnode)
  24. procedure pass_2;override;
  25. end;
  26. tSparcshlshrnode = class(tshlshrnode)
  27. procedure pass_2;override;
  28. { everything will be handled in pass_2 }
  29. function first_shlshr64bitint: tnode; override;
  30. end;
  31. tSparcnotnode = class(tcgnotnode)
  32. procedure second_boolean;override;
  33. end;
  34. implementation
  35. uses
  36. globtype,systems,
  37. cutils,verbose,globals,
  38. symconst,
  39. aasmbase,aasmcpu,aasmtai,
  40. defutil,
  41. cgbase,cgobj,pass_2,
  42. ncon,
  43. cpubase,
  44. ncgutil,cgcpu,cgutils;
  45. {*****************************************************************************
  46. TSparcMODDIVNODE
  47. *****************************************************************************}
  48. procedure tSparcmoddivnode.pass_2;
  49. const
  50. { signed overflow }
  51. divops: array[boolean, boolean] of tasmop =
  52. ((A_UDIV,A_UDIVcc),(A_SDIV,A_SDIVcc));
  53. var
  54. power : longint;
  55. op : tasmop;
  56. tmpreg,
  57. numerator,
  58. divider,
  59. resultreg : tregister;
  60. overflowlabel : tasmlabel;
  61. ai : taicpu;
  62. begin
  63. secondpass(left);
  64. secondpass(right);
  65. location_copy(location,left.location);
  66. { put numerator in register }
  67. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  68. location_copy(location,left.location);
  69. numerator := location.register;
  70. if (nodetype = modn) then
  71. resultreg := cg.GetIntRegister(exprasmlist,OS_INT)
  72. else
  73. begin
  74. if (location.loc = LOC_CREGISTER) then
  75. begin
  76. location.loc := LOC_REGISTER;
  77. location.register := cg.GetIntRegister(exprasmlist,OS_INT);
  78. end;
  79. resultreg := location.register;
  80. end;
  81. if (nodetype = divn) and
  82. (right.nodetype = ordconstn) and
  83. ispowerof2(tordconstnode(right).value,power) then
  84. begin
  85. tmpreg:=cg.GetIntRegister(exprasmlist,OS_INT);
  86. cg.a_op_const_reg_reg(exprasmlist,OP_SAR,OS_INT,31,numerator,tmpreg);
  87. { if signed, tmpreg=right value-1, otherwise 0 }
  88. cg.a_op_const_reg(exprasmlist,OP_AND,OS_INT,tordconstnode(right).value-1,tmpreg);
  89. { add to the left value }
  90. cg.a_op_reg_reg(exprasmlist,OP_ADD,OS_INT,tmpreg,numerator);
  91. cg.a_op_const_reg_reg(exprasmlist,OP_SAR,OS_INT,aword(power),numerator,resultreg);
  92. end
  93. else
  94. begin
  95. { load divider in a register if necessary }
  96. location_force_reg(exprasmlist,right.location,
  97. def_cgsize(right.resulttype.def),true);
  98. divider := right.location.register;
  99. { needs overflow checking, (-maxlongint-1) div (-1) overflows! }
  100. { And on Sparc, the only way to catch a div-by-0 is by checking }
  101. { the overflow flag (JM) }
  102. { Fill %y with the -1 or 0 depending on the highest bit }
  103. if is_signed(left.resulttype.def) then
  104. begin
  105. tmpreg:=cg.GetIntRegister(exprasmlist,OS_INT);
  106. exprasmlist.concat(taicpu.op_reg_const_reg(A_SRA,numerator,31,tmpreg));
  107. exprasmlist.concat(taicpu.op_reg_reg(A_MOV,tmpreg,NR_Y));
  108. end
  109. else
  110. exprasmlist.concat(taicpu.op_reg_reg(A_MOV,NR_G0,NR_Y));
  111. { wait 3 instructions slots before we can read %y }
  112. exprasmlist.concat(taicpu.op_none(A_NOP));
  113. exprasmlist.concat(taicpu.op_none(A_NOP));
  114. exprasmlist.concat(taicpu.op_none(A_NOP));
  115. op := divops[is_signed(right.resulttype.def),
  116. cs_check_overflow in aktlocalswitches];
  117. exprasmlist.concat(taicpu.op_reg_reg_reg(op,numerator,divider,resultreg));
  118. if (nodetype = modn) then
  119. begin
  120. objectlibrary.getjumplabel(overflowlabel);
  121. ai:=taicpu.op_cond_sym(A_Bxx,C_O,overflowlabel);
  122. ai.delayslot_annulled:=true;
  123. exprasmlist.concat(ai);
  124. exprasmlist.concat(taicpu.op_reg(A_NOT,resultreg));
  125. cg.a_label(exprasmlist,overflowlabel);
  126. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SMUL,resultreg,divider,resultreg));
  127. exprasmlist.concat(taicpu.op_reg_reg_reg(A_SUB,numerator,resultreg,resultreg));
  128. end;
  129. end;
  130. { set result location }
  131. location.loc:=LOC_REGISTER;
  132. location.register:=resultreg;
  133. cg.g_overflowcheck(exprasmlist,Location,ResultType.Def);
  134. end;
  135. {*****************************************************************************
  136. TSparcSHLRSHRNODE
  137. *****************************************************************************}
  138. function TSparcShlShrNode.first_shlshr64bitint:TNode;
  139. begin
  140. { 64bit without constants need a helper }
  141. if is_64bit(left.resulttype.def) and
  142. (right.nodetype<>ordconstn) then
  143. begin
  144. result:=inherited first_shlshr64bitint;
  145. exit;
  146. end;
  147. result := nil;
  148. end;
  149. procedure tSparcshlshrnode.pass_2;
  150. var
  151. hregister,resultreg,hregister1,
  152. hreg64hi,hreg64lo : tregister;
  153. op : topcg;
  154. shiftval: aword;
  155. begin
  156. { 64bit without constants need a helper, and is
  157. already replaced in pass1 }
  158. if is_64bit(left.resulttype.def) and
  159. (right.nodetype<>ordconstn) then
  160. internalerror(200405301);
  161. secondpass(left);
  162. secondpass(right);
  163. if is_64bit(left.resulttype.def) then
  164. begin
  165. location_reset(location,LOC_REGISTER,OS_64);
  166. { load left operator in a register }
  167. location_force_reg(exprasmlist,left.location,OS_64,false);
  168. hreg64hi:=left.location.register64.reghi;
  169. hreg64lo:=left.location.register64.reglo;
  170. shiftval := tordconstnode(right).value and 63;
  171. if shiftval > 31 then
  172. begin
  173. if nodetype = shln then
  174. begin
  175. cg.a_load_const_reg(exprasmlist,OS_32,0,hreg64hi);
  176. if (shiftval and 31) <> 0 then
  177. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,shiftval and 31,hreg64lo,hreg64lo);
  178. end
  179. else
  180. begin
  181. cg.a_load_const_reg(exprasmlist,OS_32,0,hreg64lo);
  182. if (shiftval and 31) <> 0 then
  183. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,shiftval and 31,hreg64hi,hreg64hi);
  184. end;
  185. location.register64.reglo:=hreg64hi;
  186. location.register64.reghi:=hreg64lo;
  187. end
  188. else
  189. begin
  190. hregister:=cg.getintregister(exprasmlist,OS_32);
  191. if nodetype = shln then
  192. begin
  193. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,32-shiftval,hreg64lo,hregister);
  194. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,shiftval,hreg64hi,hreg64hi);
  195. cg.a_op_reg_reg_reg(exprasmlist,OP_OR,OS_32,hregister,hreg64hi,hreg64hi);
  196. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,shiftval,hreg64lo,hreg64lo);
  197. end
  198. else
  199. begin
  200. cg.a_op_const_reg_reg(exprasmlist,OP_SHL,OS_32,32-shiftval,hreg64hi,hregister);
  201. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,shiftval,hreg64lo,hreg64lo);
  202. cg.a_op_reg_reg_reg(exprasmlist,OP_OR,OS_32,hregister,hreg64lo,hreg64lo);
  203. cg.a_op_const_reg_reg(exprasmlist,OP_SHR,OS_32,shiftval,hreg64hi,hreg64hi);
  204. end;
  205. location.register64.reghi:=hreg64hi;
  206. location.register64.reglo:=hreg64lo;
  207. end;
  208. end
  209. else
  210. begin
  211. { load left operators in a register }
  212. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  213. location_copy(location,left.location);
  214. resultreg := location.register;
  215. hregister1 := location.register;
  216. if (location.loc = LOC_CREGISTER) then
  217. begin
  218. location.loc := LOC_REGISTER;
  219. resultreg := cg.GetIntRegister(exprasmlist,OS_INT);
  220. location.register := resultreg;
  221. end;
  222. { determine operator }
  223. if nodetype=shln then
  224. op:=OP_SHL
  225. else
  226. op:=OP_SHR;
  227. { shifting by a constant directly coded: }
  228. if (right.nodetype=ordconstn) then
  229. begin
  230. if tordconstnode(right).value and 31<>0 then
  231. cg.a_op_const_reg_reg(exprasmlist,op,OS_32,tordconstnode(right).value and 31,hregister1,resultreg)
  232. end
  233. else
  234. begin
  235. { load shift count in a register if necessary }
  236. location_force_reg(exprasmlist,right.location,def_cgsize(right.resulttype.def),true);
  237. cg.a_op_reg_reg_reg(exprasmlist,op,OS_32,right.location.register,hregister1,resultreg);
  238. end;
  239. end;
  240. end;
  241. {*****************************************************************************
  242. TSPARCNOTNODE
  243. *****************************************************************************}
  244. procedure tsparcnotnode.second_boolean;
  245. var
  246. hl : tasmlabel;
  247. begin
  248. { if the location is LOC_JUMP, we do the secondpass after the
  249. labels are allocated
  250. }
  251. if left.expectloc=LOC_JUMP then
  252. begin
  253. hl:=truelabel;
  254. truelabel:=falselabel;
  255. falselabel:=hl;
  256. secondpass(left);
  257. maketojumpbool(exprasmlist,left,lr_load_regvars);
  258. hl:=truelabel;
  259. truelabel:=falselabel;
  260. falselabel:=hl;
  261. location.loc:=LOC_JUMP;
  262. end
  263. else
  264. begin
  265. secondpass(left);
  266. case left.location.loc of
  267. LOC_FLAGS :
  268. begin
  269. location_copy(location,left.location);
  270. inverse_flags(location.resflags);
  271. end;
  272. LOC_REGISTER, LOC_CREGISTER, LOC_REFERENCE, LOC_CREFERENCE :
  273. begin
  274. location_force_reg(exprasmlist,left.location,def_cgsize(left.resulttype.def),true);
  275. exprasmlist.concat(taicpu.op_reg_const_reg(A_SUBcc,left.location.register,0,NR_G0));
  276. location_reset(location,LOC_FLAGS,OS_NO);
  277. location.resflags:=F_E;
  278. end;
  279. else
  280. internalerror(2003042401);
  281. end;
  282. end;
  283. end;
  284. begin
  285. cmoddivnode:=tSparcmoddivnode;
  286. cshlshrnode:=tSparcshlshrnode;
  287. cnotnode:=tSparcnotnode;
  288. end.